mjpeg_decoder.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * Copyright 2012 The LibYuv Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "libyuv/mjpeg_decoder.h"
  11. #ifdef HAVE_JPEG
  12. #include <assert.h>
  13. #if !defined(__pnacl__) && !defined(__CLR_VER) && \
  14. !defined(COVERAGE_ENABLED) && !defined(TARGET_IPHONE_SIMULATOR)
  15. // Must be included before jpeglib.
  16. #include <setjmp.h>
  17. #define HAVE_SETJMP
  18. #if defined(_MSC_VER)
  19. // disable warning 4324: structure was padded due to __declspec(align())
  20. #pragma warning(disable:4324)
  21. #endif
  22. #endif
  23. struct FILE; // For jpeglib.h.
  24. // C++ build requires extern C for jpeg internals.
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #include <jpeglib.h>
  29. #ifdef __cplusplus
  30. } // extern "C"
  31. #endif
  32. #include "libyuv/planar_functions.h" // For CopyPlane().
  33. namespace libyuv {
  34. #ifdef HAVE_SETJMP
  35. struct SetJmpErrorMgr {
  36. jpeg_error_mgr base; // Must be at the top
  37. jmp_buf setjmp_buffer;
  38. };
  39. #endif
  40. const int MJpegDecoder::kColorSpaceUnknown = JCS_UNKNOWN;
  41. const int MJpegDecoder::kColorSpaceGrayscale = JCS_GRAYSCALE;
  42. const int MJpegDecoder::kColorSpaceRgb = JCS_RGB;
  43. const int MJpegDecoder::kColorSpaceYCbCr = JCS_YCbCr;
  44. const int MJpegDecoder::kColorSpaceCMYK = JCS_CMYK;
  45. const int MJpegDecoder::kColorSpaceYCCK = JCS_YCCK;
  46. // Methods that are passed to jpeglib.
  47. boolean fill_input_buffer(jpeg_decompress_struct* cinfo);
  48. void init_source(jpeg_decompress_struct* cinfo);
  49. void skip_input_data(jpeg_decompress_struct* cinfo, long num_bytes); // NOLINT
  50. void term_source(jpeg_decompress_struct* cinfo);
  51. void ErrorHandler(jpeg_common_struct* cinfo);
  52. void OutputHandler(jpeg_common_struct* cinfo);
  53. MJpegDecoder::MJpegDecoder()
  54. : has_scanline_padding_(LIBYUV_FALSE),
  55. num_outbufs_(0),
  56. scanlines_(NULL),
  57. scanlines_sizes_(NULL),
  58. databuf_(NULL),
  59. databuf_strides_(NULL) {
  60. decompress_struct_ = new jpeg_decompress_struct;
  61. source_mgr_ = new jpeg_source_mgr;
  62. #ifdef HAVE_SETJMP
  63. error_mgr_ = new SetJmpErrorMgr;
  64. decompress_struct_->err = jpeg_std_error(&error_mgr_->base);
  65. // Override standard exit()-based error handler.
  66. error_mgr_->base.error_exit = &ErrorHandler;
  67. #ifndef DEBUG_MJPEG
  68. error_mgr_->base.output_message = &OutputHandler;
  69. #endif
  70. #endif
  71. decompress_struct_->client_data = NULL;
  72. source_mgr_->init_source = &init_source;
  73. source_mgr_->fill_input_buffer = &fill_input_buffer;
  74. source_mgr_->skip_input_data = &skip_input_data;
  75. source_mgr_->resync_to_restart = &jpeg_resync_to_restart;
  76. source_mgr_->term_source = &term_source;
  77. jpeg_create_decompress(decompress_struct_);
  78. decompress_struct_->src = source_mgr_;
  79. buf_vec_.buffers = &buf_;
  80. buf_vec_.len = 1;
  81. }
  82. MJpegDecoder::~MJpegDecoder() {
  83. jpeg_destroy_decompress(decompress_struct_);
  84. delete decompress_struct_;
  85. delete source_mgr_;
  86. #ifdef HAVE_SETJMP
  87. delete error_mgr_;
  88. #endif
  89. DestroyOutputBuffers();
  90. }
  91. LIBYUV_BOOL MJpegDecoder::LoadFrame(const uint8* src, size_t src_len) {
  92. if (!ValidateJpeg(src, src_len)) {
  93. return LIBYUV_FALSE;
  94. }
  95. buf_.data = src;
  96. buf_.len = static_cast<int>(src_len);
  97. buf_vec_.pos = 0;
  98. decompress_struct_->client_data = &buf_vec_;
  99. #ifdef HAVE_SETJMP
  100. if (setjmp(error_mgr_->setjmp_buffer)) {
  101. // We called jpeg_read_header, it experienced an error, and we called
  102. // longjmp() and rewound the stack to here. Return error.
  103. return LIBYUV_FALSE;
  104. }
  105. #endif
  106. if (jpeg_read_header(decompress_struct_, TRUE) != JPEG_HEADER_OK) {
  107. // ERROR: Bad MJPEG header
  108. return LIBYUV_FALSE;
  109. }
  110. AllocOutputBuffers(GetNumComponents());
  111. for (int i = 0; i < num_outbufs_; ++i) {
  112. int scanlines_size = GetComponentScanlinesPerImcuRow(i);
  113. if (scanlines_sizes_[i] != scanlines_size) {
  114. if (scanlines_[i]) {
  115. delete scanlines_[i];
  116. }
  117. scanlines_[i] = new uint8* [scanlines_size];
  118. scanlines_sizes_[i] = scanlines_size;
  119. }
  120. // We allocate padding for the final scanline to pad it up to DCTSIZE bytes
  121. // to avoid memory errors, since jpeglib only reads full MCUs blocks. For
  122. // the preceding scanlines, the padding is not needed/wanted because the
  123. // following addresses will already be valid (they are the initial bytes of
  124. // the next scanline) and will be overwritten when jpeglib writes out that
  125. // next scanline.
  126. int databuf_stride = GetComponentStride(i);
  127. int databuf_size = scanlines_size * databuf_stride;
  128. if (databuf_strides_[i] != databuf_stride) {
  129. if (databuf_[i]) {
  130. delete databuf_[i];
  131. }
  132. databuf_[i] = new uint8[databuf_size];
  133. databuf_strides_[i] = databuf_stride;
  134. }
  135. if (GetComponentStride(i) != GetComponentWidth(i)) {
  136. has_scanline_padding_ = LIBYUV_TRUE;
  137. }
  138. }
  139. return LIBYUV_TRUE;
  140. }
  141. static int DivideAndRoundUp(int numerator, int denominator) {
  142. return (numerator + denominator - 1) / denominator;
  143. }
  144. static int DivideAndRoundDown(int numerator, int denominator) {
  145. return numerator / denominator;
  146. }
  147. // Returns width of the last loaded frame.
  148. int MJpegDecoder::GetWidth() {
  149. return decompress_struct_->image_width;
  150. }
  151. // Returns height of the last loaded frame.
  152. int MJpegDecoder::GetHeight() {
  153. return decompress_struct_->image_height;
  154. }
  155. // Returns format of the last loaded frame. The return value is one of the
  156. // kColorSpace* constants.
  157. int MJpegDecoder::GetColorSpace() {
  158. return decompress_struct_->jpeg_color_space;
  159. }
  160. // Number of color components in the color space.
  161. int MJpegDecoder::GetNumComponents() {
  162. return decompress_struct_->num_components;
  163. }
  164. // Sample factors of the n-th component.
  165. int MJpegDecoder::GetHorizSampFactor(int component) {
  166. return decompress_struct_->comp_info[component].h_samp_factor;
  167. }
  168. int MJpegDecoder::GetVertSampFactor(int component) {
  169. return decompress_struct_->comp_info[component].v_samp_factor;
  170. }
  171. int MJpegDecoder::GetHorizSubSampFactor(int component) {
  172. return decompress_struct_->max_h_samp_factor /
  173. GetHorizSampFactor(component);
  174. }
  175. int MJpegDecoder::GetVertSubSampFactor(int component) {
  176. return decompress_struct_->max_v_samp_factor /
  177. GetVertSampFactor(component);
  178. }
  179. int MJpegDecoder::GetImageScanlinesPerImcuRow() {
  180. return decompress_struct_->max_v_samp_factor * DCTSIZE;
  181. }
  182. int MJpegDecoder::GetComponentScanlinesPerImcuRow(int component) {
  183. int vs = GetVertSubSampFactor(component);
  184. return DivideAndRoundUp(GetImageScanlinesPerImcuRow(), vs);
  185. }
  186. int MJpegDecoder::GetComponentWidth(int component) {
  187. int hs = GetHorizSubSampFactor(component);
  188. return DivideAndRoundUp(GetWidth(), hs);
  189. }
  190. int MJpegDecoder::GetComponentHeight(int component) {
  191. int vs = GetVertSubSampFactor(component);
  192. return DivideAndRoundUp(GetHeight(), vs);
  193. }
  194. // Get width in bytes padded out to a multiple of DCTSIZE
  195. int MJpegDecoder::GetComponentStride(int component) {
  196. return (GetComponentWidth(component) + DCTSIZE - 1) & ~(DCTSIZE - 1);
  197. }
  198. int MJpegDecoder::GetComponentSize(int component) {
  199. return GetComponentWidth(component) * GetComponentHeight(component);
  200. }
  201. LIBYUV_BOOL MJpegDecoder::UnloadFrame() {
  202. #ifdef HAVE_SETJMP
  203. if (setjmp(error_mgr_->setjmp_buffer)) {
  204. // We called jpeg_abort_decompress, it experienced an error, and we called
  205. // longjmp() and rewound the stack to here. Return error.
  206. return LIBYUV_FALSE;
  207. }
  208. #endif
  209. jpeg_abort_decompress(decompress_struct_);
  210. return LIBYUV_TRUE;
  211. }
  212. // TODO(fbarchard): Allow rectangle to be specified: x, y, width, height.
  213. LIBYUV_BOOL MJpegDecoder::DecodeToBuffers(
  214. uint8** planes, int dst_width, int dst_height) {
  215. if (dst_width != GetWidth() ||
  216. dst_height > GetHeight()) {
  217. // ERROR: Bad dimensions
  218. return LIBYUV_FALSE;
  219. }
  220. #ifdef HAVE_SETJMP
  221. if (setjmp(error_mgr_->setjmp_buffer)) {
  222. // We called into jpeglib, it experienced an error sometime during this
  223. // function call, and we called longjmp() and rewound the stack to here.
  224. // Return error.
  225. return LIBYUV_FALSE;
  226. }
  227. #endif
  228. if (!StartDecode()) {
  229. return LIBYUV_FALSE;
  230. }
  231. SetScanlinePointers(databuf_);
  232. int lines_left = dst_height;
  233. // Compute amount of lines to skip to implement vertical crop.
  234. // TODO(fbarchard): Ensure skip is a multiple of maximum component
  235. // subsample. ie 2
  236. int skip = (GetHeight() - dst_height) / 2;
  237. if (skip > 0) {
  238. // There is no API to skip lines in the output data, so we read them
  239. // into the temp buffer.
  240. while (skip >= GetImageScanlinesPerImcuRow()) {
  241. if (!DecodeImcuRow()) {
  242. FinishDecode();
  243. return LIBYUV_FALSE;
  244. }
  245. skip -= GetImageScanlinesPerImcuRow();
  246. }
  247. if (skip > 0) {
  248. // Have a partial iMCU row left over to skip. Must read it and then
  249. // copy the parts we want into the destination.
  250. if (!DecodeImcuRow()) {
  251. FinishDecode();
  252. return LIBYUV_FALSE;
  253. }
  254. for (int i = 0; i < num_outbufs_; ++i) {
  255. // TODO(fbarchard): Compute skip to avoid this
  256. assert(skip % GetVertSubSampFactor(i) == 0);
  257. int rows_to_skip =
  258. DivideAndRoundDown(skip, GetVertSubSampFactor(i));
  259. int scanlines_to_copy = GetComponentScanlinesPerImcuRow(i) -
  260. rows_to_skip;
  261. int data_to_skip = rows_to_skip * GetComponentStride(i);
  262. CopyPlane(databuf_[i] + data_to_skip, GetComponentStride(i),
  263. planes[i], GetComponentWidth(i),
  264. GetComponentWidth(i), scanlines_to_copy);
  265. planes[i] += scanlines_to_copy * GetComponentWidth(i);
  266. }
  267. lines_left -= (GetImageScanlinesPerImcuRow() - skip);
  268. }
  269. }
  270. // Read full MCUs but cropped horizontally
  271. for (; lines_left > GetImageScanlinesPerImcuRow();
  272. lines_left -= GetImageScanlinesPerImcuRow()) {
  273. if (!DecodeImcuRow()) {
  274. FinishDecode();
  275. return LIBYUV_FALSE;
  276. }
  277. for (int i = 0; i < num_outbufs_; ++i) {
  278. int scanlines_to_copy = GetComponentScanlinesPerImcuRow(i);
  279. CopyPlane(databuf_[i], GetComponentStride(i),
  280. planes[i], GetComponentWidth(i),
  281. GetComponentWidth(i), scanlines_to_copy);
  282. planes[i] += scanlines_to_copy * GetComponentWidth(i);
  283. }
  284. }
  285. if (lines_left > 0) {
  286. // Have a partial iMCU row left over to decode.
  287. if (!DecodeImcuRow()) {
  288. FinishDecode();
  289. return LIBYUV_FALSE;
  290. }
  291. for (int i = 0; i < num_outbufs_; ++i) {
  292. int scanlines_to_copy =
  293. DivideAndRoundUp(lines_left, GetVertSubSampFactor(i));
  294. CopyPlane(databuf_[i], GetComponentStride(i),
  295. planes[i], GetComponentWidth(i),
  296. GetComponentWidth(i), scanlines_to_copy);
  297. planes[i] += scanlines_to_copy * GetComponentWidth(i);
  298. }
  299. }
  300. return FinishDecode();
  301. }
  302. LIBYUV_BOOL MJpegDecoder::DecodeToCallback(CallbackFunction fn, void* opaque,
  303. int dst_width, int dst_height) {
  304. if (dst_width != GetWidth() ||
  305. dst_height > GetHeight()) {
  306. // ERROR: Bad dimensions
  307. return LIBYUV_FALSE;
  308. }
  309. #ifdef HAVE_SETJMP
  310. if (setjmp(error_mgr_->setjmp_buffer)) {
  311. // We called into jpeglib, it experienced an error sometime during this
  312. // function call, and we called longjmp() and rewound the stack to here.
  313. // Return error.
  314. return LIBYUV_FALSE;
  315. }
  316. #endif
  317. if (!StartDecode()) {
  318. return LIBYUV_FALSE;
  319. }
  320. SetScanlinePointers(databuf_);
  321. int lines_left = dst_height;
  322. // TODO(fbarchard): Compute amount of lines to skip to implement vertical crop
  323. int skip = (GetHeight() - dst_height) / 2;
  324. if (skip > 0) {
  325. while (skip >= GetImageScanlinesPerImcuRow()) {
  326. if (!DecodeImcuRow()) {
  327. FinishDecode();
  328. return LIBYUV_FALSE;
  329. }
  330. skip -= GetImageScanlinesPerImcuRow();
  331. }
  332. if (skip > 0) {
  333. // Have a partial iMCU row left over to skip.
  334. if (!DecodeImcuRow()) {
  335. FinishDecode();
  336. return LIBYUV_FALSE;
  337. }
  338. for (int i = 0; i < num_outbufs_; ++i) {
  339. // TODO(fbarchard): Compute skip to avoid this
  340. assert(skip % GetVertSubSampFactor(i) == 0);
  341. int rows_to_skip = DivideAndRoundDown(skip, GetVertSubSampFactor(i));
  342. int data_to_skip = rows_to_skip * GetComponentStride(i);
  343. // Change our own data buffer pointers so we can pass them to the
  344. // callback.
  345. databuf_[i] += data_to_skip;
  346. }
  347. int scanlines_to_copy = GetImageScanlinesPerImcuRow() - skip;
  348. (*fn)(opaque, databuf_, databuf_strides_, scanlines_to_copy);
  349. // Now change them back.
  350. for (int i = 0; i < num_outbufs_; ++i) {
  351. int rows_to_skip = DivideAndRoundDown(skip, GetVertSubSampFactor(i));
  352. int data_to_skip = rows_to_skip * GetComponentStride(i);
  353. databuf_[i] -= data_to_skip;
  354. }
  355. lines_left -= scanlines_to_copy;
  356. }
  357. }
  358. // Read full MCUs until we get to the crop point.
  359. for (; lines_left >= GetImageScanlinesPerImcuRow();
  360. lines_left -= GetImageScanlinesPerImcuRow()) {
  361. if (!DecodeImcuRow()) {
  362. FinishDecode();
  363. return LIBYUV_FALSE;
  364. }
  365. (*fn)(opaque, databuf_, databuf_strides_, GetImageScanlinesPerImcuRow());
  366. }
  367. if (lines_left > 0) {
  368. // Have a partial iMCU row left over to decode.
  369. if (!DecodeImcuRow()) {
  370. FinishDecode();
  371. return LIBYUV_FALSE;
  372. }
  373. (*fn)(opaque, databuf_, databuf_strides_, lines_left);
  374. }
  375. return FinishDecode();
  376. }
  377. void init_source(j_decompress_ptr cinfo) {
  378. fill_input_buffer(cinfo);
  379. }
  380. boolean fill_input_buffer(j_decompress_ptr cinfo) {
  381. BufferVector* buf_vec = reinterpret_cast<BufferVector*>(cinfo->client_data);
  382. if (buf_vec->pos >= buf_vec->len) {
  383. assert(0 && "No more data");
  384. // ERROR: No more data
  385. return FALSE;
  386. }
  387. cinfo->src->next_input_byte = buf_vec->buffers[buf_vec->pos].data;
  388. cinfo->src->bytes_in_buffer = buf_vec->buffers[buf_vec->pos].len;
  389. ++buf_vec->pos;
  390. return TRUE;
  391. }
  392. void skip_input_data(j_decompress_ptr cinfo, long num_bytes) { // NOLINT
  393. cinfo->src->next_input_byte += num_bytes;
  394. }
  395. void term_source(j_decompress_ptr cinfo) {
  396. // Nothing to do.
  397. }
  398. #ifdef HAVE_SETJMP
  399. void ErrorHandler(j_common_ptr cinfo) {
  400. // This is called when a jpeglib command experiences an error. Unfortunately
  401. // jpeglib's error handling model is not very flexible, because it expects the
  402. // error handler to not return--i.e., it wants the program to terminate. To
  403. // recover from errors we use setjmp() as shown in their example. setjmp() is
  404. // C's implementation for the "call with current continuation" functionality
  405. // seen in some functional programming languages.
  406. // A formatted message can be output, but is unsafe for release.
  407. #ifdef DEBUG
  408. char buf[JMSG_LENGTH_MAX];
  409. (*cinfo->err->format_message)(cinfo, buf);
  410. // ERROR: Error in jpeglib: buf
  411. #endif
  412. SetJmpErrorMgr* mgr = reinterpret_cast<SetJmpErrorMgr*>(cinfo->err);
  413. // This rewinds the call stack to the point of the corresponding setjmp()
  414. // and causes it to return (for a second time) with value 1.
  415. longjmp(mgr->setjmp_buffer, 1);
  416. }
  417. #ifndef DEBUG_MJPEG
  418. void OutputHandler(j_common_ptr cinfo) {
  419. // silently eat messages
  420. }
  421. #endif
  422. #endif // HAVE_SETJMP
  423. void MJpegDecoder::AllocOutputBuffers(int num_outbufs) {
  424. if (num_outbufs != num_outbufs_) {
  425. // We could perhaps optimize this case to resize the output buffers without
  426. // necessarily having to delete and recreate each one, but it's not worth
  427. // it.
  428. DestroyOutputBuffers();
  429. scanlines_ = new uint8** [num_outbufs];
  430. scanlines_sizes_ = new int[num_outbufs];
  431. databuf_ = new uint8* [num_outbufs];
  432. databuf_strides_ = new int[num_outbufs];
  433. for (int i = 0; i < num_outbufs; ++i) {
  434. scanlines_[i] = NULL;
  435. scanlines_sizes_[i] = 0;
  436. databuf_[i] = NULL;
  437. databuf_strides_[i] = 0;
  438. }
  439. num_outbufs_ = num_outbufs;
  440. }
  441. }
  442. void MJpegDecoder::DestroyOutputBuffers() {
  443. for (int i = 0; i < num_outbufs_; ++i) {
  444. delete [] scanlines_[i];
  445. delete [] databuf_[i];
  446. }
  447. delete [] scanlines_;
  448. delete [] databuf_;
  449. delete [] scanlines_sizes_;
  450. delete [] databuf_strides_;
  451. scanlines_ = NULL;
  452. databuf_ = NULL;
  453. scanlines_sizes_ = NULL;
  454. databuf_strides_ = NULL;
  455. num_outbufs_ = 0;
  456. }
  457. // JDCT_IFAST and do_block_smoothing improve performance substantially.
  458. LIBYUV_BOOL MJpegDecoder::StartDecode() {
  459. decompress_struct_->raw_data_out = TRUE;
  460. decompress_struct_->dct_method = JDCT_IFAST; // JDCT_ISLOW is default
  461. decompress_struct_->dither_mode = JDITHER_NONE;
  462. // Not applicable to 'raw':
  463. decompress_struct_->do_fancy_upsampling = (boolean)(LIBYUV_FALSE);
  464. // Only for buffered mode:
  465. decompress_struct_->enable_2pass_quant = (boolean)(LIBYUV_FALSE);
  466. // Blocky but fast:
  467. decompress_struct_->do_block_smoothing = (boolean)(LIBYUV_FALSE);
  468. if (!jpeg_start_decompress(decompress_struct_)) {
  469. // ERROR: Couldn't start JPEG decompressor";
  470. return LIBYUV_FALSE;
  471. }
  472. return LIBYUV_TRUE;
  473. }
  474. LIBYUV_BOOL MJpegDecoder::FinishDecode() {
  475. // jpeglib considers it an error if we finish without decoding the whole
  476. // image, so we call "abort" rather than "finish".
  477. jpeg_abort_decompress(decompress_struct_);
  478. return LIBYUV_TRUE;
  479. }
  480. void MJpegDecoder::SetScanlinePointers(uint8** data) {
  481. for (int i = 0; i < num_outbufs_; ++i) {
  482. uint8* data_i = data[i];
  483. for (int j = 0; j < scanlines_sizes_[i]; ++j) {
  484. scanlines_[i][j] = data_i;
  485. data_i += GetComponentStride(i);
  486. }
  487. }
  488. }
  489. inline LIBYUV_BOOL MJpegDecoder::DecodeImcuRow() {
  490. return (unsigned int)(GetImageScanlinesPerImcuRow()) ==
  491. jpeg_read_raw_data(decompress_struct_,
  492. scanlines_,
  493. GetImageScanlinesPerImcuRow());
  494. }
  495. // The helper function which recognizes the jpeg sub-sampling type.
  496. JpegSubsamplingType MJpegDecoder::JpegSubsamplingTypeHelper(
  497. int* subsample_x, int* subsample_y, int number_of_components) {
  498. if (number_of_components == 3) { // Color images.
  499. if (subsample_x[0] == 1 && subsample_y[0] == 1 &&
  500. subsample_x[1] == 2 && subsample_y[1] == 2 &&
  501. subsample_x[2] == 2 && subsample_y[2] == 2) {
  502. return kJpegYuv420;
  503. } else if (subsample_x[0] == 1 && subsample_y[0] == 1 &&
  504. subsample_x[1] == 2 && subsample_y[1] == 1 &&
  505. subsample_x[2] == 2 && subsample_y[2] == 1) {
  506. return kJpegYuv422;
  507. } else if (subsample_x[0] == 1 && subsample_y[0] == 1 &&
  508. subsample_x[1] == 1 && subsample_y[1] == 1 &&
  509. subsample_x[2] == 1 && subsample_y[2] == 1) {
  510. return kJpegYuv444;
  511. }
  512. } else if (number_of_components == 1) { // Grey-scale images.
  513. if (subsample_x[0] == 1 && subsample_y[0] == 1) {
  514. return kJpegYuv400;
  515. }
  516. }
  517. return kJpegUnknown;
  518. }
  519. } // namespace libyuv
  520. #endif // HAVE_JPEG