mjpeg_decoder.cc 18 KB

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