react-native-vision-camera-spoof-detector 1.0.21 → 1.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/.gitattributes +2 -0
  2. package/.gitignore +27 -0
  3. package/CHANGELOG.md +2 -2
  4. package/CONTRIBUTING.md +2 -2
  5. package/README.md +442 -443
  6. package/android/build.gradle +12 -4
  7. package/android/src/main/cpp/CMakeLists.txt +101 -94
  8. package/android/src/main/cpp/libyuv/.clang-format +6 -6
  9. package/android/src/main/cpp/libyuv/.gn +40 -40
  10. package/android/src/main/cpp/libyuv/.vpython3 +410 -410
  11. package/android/src/main/cpp/libyuv/AUTHORS +7 -7
  12. package/android/src/main/cpp/libyuv/Android.bp +202 -202
  13. package/android/src/main/cpp/libyuv/Android.mk +106 -106
  14. package/android/src/main/cpp/libyuv/BUILD.gn +490 -490
  15. package/android/src/main/cpp/libyuv/CM_linux_packages.cmake +70 -70
  16. package/android/src/main/cpp/libyuv/CMakeLists.txt +259 -252
  17. package/android/src/main/cpp/libyuv/DEPS +935 -935
  18. package/android/src/main/cpp/libyuv/DIR_METADATA +3 -3
  19. package/android/src/main/cpp/libyuv/LICENSE +29 -29
  20. package/android/src/main/cpp/libyuv/OWNERS +11 -11
  21. package/android/src/main/cpp/libyuv/PATENTS +23 -23
  22. package/android/src/main/cpp/libyuv/PRESUBMIT.py +65 -65
  23. package/android/src/main/cpp/libyuv/README.chromium +11 -11
  24. package/android/src/main/cpp/libyuv/README.md +19 -19
  25. package/android/src/main/cpp/libyuv/build_overrides/build.gni +60 -60
  26. package/android/src/main/cpp/libyuv/build_overrides/gtest.gni +19 -19
  27. package/android/src/main/cpp/libyuv/build_overrides/partition_alloc.gni +17 -17
  28. package/android/src/main/cpp/libyuv/codereview.settings +5 -5
  29. package/android/src/main/cpp/libyuv/docs/deprecated_builds.md +409 -409
  30. package/android/src/main/cpp/libyuv/docs/environment_variables.md +64 -64
  31. package/android/src/main/cpp/libyuv/docs/feature_detection.md +108 -108
  32. package/android/src/main/cpp/libyuv/docs/filtering.md +196 -196
  33. package/android/src/main/cpp/libyuv/docs/formats.md +208 -208
  34. package/android/src/main/cpp/libyuv/docs/getting_started.md +296 -296
  35. package/android/src/main/cpp/libyuv/docs/rotation.md +107 -107
  36. package/android/src/main/cpp/libyuv/download_vs_toolchain.py +29 -29
  37. package/android/src/main/cpp/libyuv/include/libyuv/basic_types.h +68 -68
  38. package/android/src/main/cpp/libyuv/include/libyuv/compare.h +111 -111
  39. package/android/src/main/cpp/libyuv/include/libyuv/compare_row.h +112 -112
  40. package/android/src/main/cpp/libyuv/include/libyuv/convert.h +1117 -1117
  41. package/android/src/main/cpp/libyuv/include/libyuv/convert_argb.h +2335 -2335
  42. package/android/src/main/cpp/libyuv/include/libyuv/convert_from.h +203 -203
  43. package/android/src/main/cpp/libyuv/include/libyuv/convert_from_argb.h +394 -394
  44. package/android/src/main/cpp/libyuv/include/libyuv/cpu_id.h +147 -147
  45. package/android/src/main/cpp/libyuv/include/libyuv/cpu_support.h +99 -99
  46. package/android/src/main/cpp/libyuv/include/libyuv/loongson_intrinsics.h +1949 -1949
  47. package/android/src/main/cpp/libyuv/include/libyuv/mjpeg_decoder.h +195 -195
  48. package/android/src/main/cpp/libyuv/include/libyuv/planar_functions.h +1131 -1131
  49. package/android/src/main/cpp/libyuv/include/libyuv/rotate.h +296 -296
  50. package/android/src/main/cpp/libyuv/include/libyuv/rotate_argb.h +37 -37
  51. package/android/src/main/cpp/libyuv/include/libyuv/rotate_row.h +265 -265
  52. package/android/src/main/cpp/libyuv/include/libyuv/row.h +6738 -6738
  53. package/android/src/main/cpp/libyuv/include/libyuv/row_sve.h +2154 -2154
  54. package/android/src/main/cpp/libyuv/include/libyuv/scale.h +336 -336
  55. package/android/src/main/cpp/libyuv/include/libyuv/scale_argb.h +76 -76
  56. package/android/src/main/cpp/libyuv/include/libyuv/scale_rgb.h +42 -42
  57. package/android/src/main/cpp/libyuv/include/libyuv/scale_row.h +1726 -1726
  58. package/android/src/main/cpp/libyuv/include/libyuv/scale_uv.h +51 -51
  59. package/android/src/main/cpp/libyuv/include/libyuv/version.h +16 -16
  60. package/android/src/main/cpp/libyuv/include/libyuv/video_common.h +222 -222
  61. package/android/src/main/cpp/libyuv/include/libyuv.h +33 -33
  62. package/android/src/main/cpp/libyuv/infra/config/OWNERS +3 -3
  63. package/android/src/main/cpp/libyuv/infra/config/PRESUBMIT.py +17 -17
  64. package/android/src/main/cpp/libyuv/infra/config/README.md +2 -2
  65. package/android/src/main/cpp/libyuv/infra/config/codereview.settings +6 -6
  66. package/android/src/main/cpp/libyuv/infra/config/commit-queue.cfg +144 -144
  67. package/android/src/main/cpp/libyuv/infra/config/cr-buildbucket.cfg +1185 -1185
  68. package/android/src/main/cpp/libyuv/infra/config/luci-logdog.cfg +9 -9
  69. package/android/src/main/cpp/libyuv/infra/config/luci-milo.cfg +246 -246
  70. package/android/src/main/cpp/libyuv/infra/config/luci-scheduler.cfg +385 -385
  71. package/android/src/main/cpp/libyuv/infra/config/main.star +402 -402
  72. package/android/src/main/cpp/libyuv/infra/config/project.cfg +16 -16
  73. package/android/src/main/cpp/libyuv/infra/config/realms.cfg +111 -111
  74. package/android/src/main/cpp/libyuv/libyuv.gni +34 -34
  75. package/android/src/main/cpp/libyuv/libyuv.gyp +149 -149
  76. package/android/src/main/cpp/libyuv/libyuv.gypi +87 -87
  77. package/android/src/main/cpp/libyuv/linux.mk +96 -96
  78. package/android/src/main/cpp/libyuv/public.mk +13 -13
  79. package/android/src/main/cpp/libyuv/pylintrc +49 -49
  80. package/android/src/main/cpp/libyuv/riscv_script/prepare_toolchain_qemu.sh +74 -74
  81. package/android/src/main/cpp/libyuv/riscv_script/riscv-clang.cmake +56 -56
  82. package/android/src/main/cpp/libyuv/riscv_script/run_qemu.sh +15 -15
  83. package/android/src/main/cpp/libyuv/source/compare.cc +435 -435
  84. package/android/src/main/cpp/libyuv/source/compare_common.cc +74 -74
  85. package/android/src/main/cpp/libyuv/source/compare_gcc.cc +362 -362
  86. package/android/src/main/cpp/libyuv/source/compare_neon.cc +96 -96
  87. package/android/src/main/cpp/libyuv/source/compare_neon64.cc +223 -223
  88. package/android/src/main/cpp/libyuv/source/compare_win.cc +241 -241
  89. package/android/src/main/cpp/libyuv/source/convert.cc +4746 -4746
  90. package/android/src/main/cpp/libyuv/source/convert_argb.cc +9179 -9179
  91. package/android/src/main/cpp/libyuv/source/convert_from.cc +866 -866
  92. package/android/src/main/cpp/libyuv/source/convert_from_argb.cc +3671 -3671
  93. package/android/src/main/cpp/libyuv/source/convert_jpeg.cc +602 -602
  94. package/android/src/main/cpp/libyuv/source/convert_to_argb.cc +391 -391
  95. package/android/src/main/cpp/libyuv/source/convert_to_i420.cc +288 -288
  96. package/android/src/main/cpp/libyuv/source/cpu_id.cc +496 -496
  97. package/android/src/main/cpp/libyuv/source/mjpeg_decoder.cc +580 -580
  98. package/android/src/main/cpp/libyuv/source/mjpeg_validate.cc +71 -71
  99. package/android/src/main/cpp/libyuv/source/planar_functions.cc +5663 -5663
  100. package/android/src/main/cpp/libyuv/source/rotate.cc +1241 -1241
  101. package/android/src/main/cpp/libyuv/source/rotate_any.cc +76 -76
  102. package/android/src/main/cpp/libyuv/source/rotate_argb.cc +259 -259
  103. package/android/src/main/cpp/libyuv/source/rotate_common.cc +208 -208
  104. package/android/src/main/cpp/libyuv/source/rotate_gcc.cc +505 -505
  105. package/android/src/main/cpp/libyuv/source/rotate_lsx.cc +233 -233
  106. package/android/src/main/cpp/libyuv/source/rotate_neon.cc +219 -219
  107. package/android/src/main/cpp/libyuv/source/rotate_neon64.cc +273 -273
  108. package/android/src/main/cpp/libyuv/source/rotate_sme.cc +174 -174
  109. package/android/src/main/cpp/libyuv/source/rotate_win.cc +253 -253
  110. package/android/src/main/cpp/libyuv/source/row_any.cc +2519 -2519
  111. package/android/src/main/cpp/libyuv/source/row_common.cc +4461 -4461
  112. package/android/src/main/cpp/libyuv/source/row_gcc.cc +9570 -9570
  113. package/android/src/main/cpp/libyuv/source/row_lasx.cc +2343 -2343
  114. package/android/src/main/cpp/libyuv/source/row_lsx.cc +3030 -3030
  115. package/android/src/main/cpp/libyuv/source/row_neon.cc +4026 -4026
  116. package/android/src/main/cpp/libyuv/source/row_neon64.cc +5617 -5617
  117. package/android/src/main/cpp/libyuv/source/row_rvv.cc +2599 -2599
  118. package/android/src/main/cpp/libyuv/source/row_sme.cc +1183 -1183
  119. package/android/src/main/cpp/libyuv/source/row_sve.cc +1088 -1088
  120. package/android/src/main/cpp/libyuv/source/row_win.cc +6453 -6453
  121. package/android/src/main/cpp/libyuv/source/scale.cc +2710 -2710
  122. package/android/src/main/cpp/libyuv/source/scale_any.cc +991 -991
  123. package/android/src/main/cpp/libyuv/source/scale_argb.cc +1158 -1158
  124. package/android/src/main/cpp/libyuv/source/scale_common.cc +1977 -1977
  125. package/android/src/main/cpp/libyuv/source/scale_gcc.cc +2947 -2947
  126. package/android/src/main/cpp/libyuv/source/scale_lsx.cc +739 -739
  127. package/android/src/main/cpp/libyuv/source/scale_neon.cc +1449 -1449
  128. package/android/src/main/cpp/libyuv/source/scale_neon64.cc +1552 -1552
  129. package/android/src/main/cpp/libyuv/source/scale_rgb.cc +82 -82
  130. package/android/src/main/cpp/libyuv/source/scale_rvv.cc +1971 -1971
  131. package/android/src/main/cpp/libyuv/source/scale_sme.cc +555 -555
  132. package/android/src/main/cpp/libyuv/source/scale_uv.cc +1159 -1159
  133. package/android/src/main/cpp/libyuv/source/scale_win.cc +1392 -1392
  134. package/android/src/main/cpp/libyuv/source/test.sh +35 -35
  135. package/android/src/main/cpp/libyuv/source/video_common.cc +62 -62
  136. package/android/src/main/cpp/libyuv/tools_libyuv/OWNERS +4 -4
  137. package/android/src/main/cpp/libyuv/tools_libyuv/autoroller/roll_deps.py +931 -931
  138. package/android/src/main/cpp/libyuv/tools_libyuv/autoroller/unittests/roll_deps_test.py +164 -164
  139. package/android/src/main/cpp/libyuv/tools_libyuv/autoroller/unittests/testdata/DEPS +21 -21
  140. package/android/src/main/cpp/libyuv/tools_libyuv/autoroller/unittests/testdata/DEPS.chromium.new +13 -13
  141. package/android/src/main/cpp/libyuv/tools_libyuv/autoroller/unittests/testdata/DEPS.chromium.old +13 -13
  142. package/android/src/main/cpp/libyuv/tools_libyuv/get_landmines.py +38 -38
  143. package/android/src/main/cpp/libyuv/tools_libyuv/msan/OWNERS +3 -3
  144. package/android/src/main/cpp/libyuv/tools_libyuv/msan/blacklist.txt +9 -9
  145. package/android/src/main/cpp/libyuv/tools_libyuv/ubsan/OWNERS +3 -3
  146. package/android/src/main/cpp/libyuv/tools_libyuv/ubsan/blacklist.txt +15 -15
  147. package/android/src/main/cpp/libyuv/tools_libyuv/ubsan/vptr_blacklist.txt +25 -25
  148. package/android/src/main/cpp/libyuv/unit_test/basictypes_test.cc +43 -43
  149. package/android/src/main/cpp/libyuv/unit_test/color_test.cc +848 -848
  150. package/android/src/main/cpp/libyuv/unit_test/compare_test.cc +739 -739
  151. package/android/src/main/cpp/libyuv/unit_test/convert_argb_test.cc +2867 -2867
  152. package/android/src/main/cpp/libyuv/unit_test/convert_test.cc +2133 -2133
  153. package/android/src/main/cpp/libyuv/unit_test/cpu_test.cc +427 -427
  154. package/android/src/main/cpp/libyuv/unit_test/cpu_thread_test.cc +63 -63
  155. package/android/src/main/cpp/libyuv/unit_test/math_test.cc +160 -160
  156. package/android/src/main/cpp/libyuv/unit_test/planar_test.cc +4731 -4731
  157. package/android/src/main/cpp/libyuv/unit_test/rotate_argb_test.cc +334 -334
  158. package/android/src/main/cpp/libyuv/unit_test/rotate_test.cc +962 -962
  159. package/android/src/main/cpp/libyuv/unit_test/scale_argb_test.cc +590 -590
  160. package/android/src/main/cpp/libyuv/unit_test/scale_plane_test.cc +465 -465
  161. package/android/src/main/cpp/libyuv/unit_test/scale_rgb_test.cc +280 -280
  162. package/android/src/main/cpp/libyuv/unit_test/scale_test.cc +1135 -1135
  163. package/android/src/main/cpp/libyuv/unit_test/scale_uv_test.cc +249 -249
  164. package/android/src/main/cpp/libyuv/unit_test/testdata/arm_v7.txt +12 -12
  165. package/android/src/main/cpp/libyuv/unit_test/testdata/mips.txt +7 -7
  166. package/android/src/main/cpp/libyuv/unit_test/testdata/mips_loongson2k.txt +5 -5
  167. package/android/src/main/cpp/libyuv/unit_test/testdata/mips_loongson3.txt +10 -10
  168. package/android/src/main/cpp/libyuv/unit_test/testdata/mips_loongson_mmi.txt +7 -7
  169. package/android/src/main/cpp/libyuv/unit_test/testdata/riscv64.txt +3 -3
  170. package/android/src/main/cpp/libyuv/unit_test/testdata/riscv64_rvv.txt +3 -3
  171. package/android/src/main/cpp/libyuv/unit_test/testdata/riscv64_rvv_zvfh.txt +3 -3
  172. package/android/src/main/cpp/libyuv/unit_test/testdata/tegra3.txt +23 -23
  173. package/android/src/main/cpp/libyuv/unit_test/unit_test.cc +581 -581
  174. package/android/src/main/cpp/libyuv/unit_test/unit_test.h +232 -232
  175. package/android/src/main/cpp/libyuv/unit_test/video_common_test.cc +112 -112
  176. package/android/src/main/cpp/libyuv/util/Makefile +9 -9
  177. package/android/src/main/cpp/libyuv/util/color.cc +120 -120
  178. package/android/src/main/cpp/libyuv/util/compare.cc +67 -67
  179. package/android/src/main/cpp/libyuv/util/cpuid.c +211 -211
  180. package/android/src/main/cpp/libyuv/util/i444tonv12_eg.cc +27 -27
  181. package/android/src/main/cpp/libyuv/util/psnr.cc +291 -291
  182. package/android/src/main/cpp/libyuv/util/psnr.h +47 -47
  183. package/android/src/main/cpp/libyuv/util/psnr_main.cc +620 -620
  184. package/android/src/main/cpp/libyuv/util/ssim.cc +364 -364
  185. package/android/src/main/cpp/libyuv/util/ssim.h +38 -38
  186. package/android/src/main/cpp/libyuv/util/yuvconstants.c +114 -114
  187. package/android/src/main/cpp/libyuv/util/yuvconvert.cc +367 -367
  188. package/android/src/main/cpp/libyuv/winarm.mk +47 -47
  189. package/package.json +86 -84
  190. package/android/.gradle/8.9/checksums/checksums.lock +0 -0
  191. package/android/.gradle/8.9/dependencies-accessors/gc.properties +0 -0
  192. package/android/.gradle/8.9/fileChanges/last-build.bin +0 -0
  193. package/android/.gradle/8.9/fileHashes/fileHashes.lock +0 -0
  194. package/android/.gradle/8.9/gc.properties +0 -0
  195. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  196. package/android/.gradle/buildOutputCleanup/cache.properties +0 -2
  197. package/android/.gradle/vcs-1/gc.properties +0 -0
@@ -1,580 +1,580 @@
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
-
11
- #include "libyuv/mjpeg_decoder.h"
12
-
13
- #ifdef HAVE_JPEG
14
- #include <assert.h>
15
-
16
- #if !defined(__pnacl__) && !defined(__CLR_VER) && !defined(COVERAGE_ENABLED)
17
- // Must be included before jpeglib.
18
- #include <setjmp.h>
19
- #define HAVE_SETJMP
20
-
21
- #if defined(_MSC_VER)
22
- // disable warning 4324: structure was padded due to __declspec(align())
23
- #pragma warning(disable : 4324)
24
- #endif
25
-
26
- #endif
27
-
28
- #include <stdio.h> // For jpeglib.h.
29
-
30
- // C++ build requires extern C for jpeg internals.
31
- #ifdef __cplusplus
32
- extern "C" {
33
- #endif
34
-
35
- #include <jpeglib.h>
36
-
37
- #ifdef __cplusplus
38
- } // extern "C"
39
- #endif
40
-
41
- #include "libyuv/planar_functions.h" // For CopyPlane().
42
-
43
- namespace libyuv {
44
-
45
- #ifdef HAVE_SETJMP
46
- struct SetJmpErrorMgr {
47
- jpeg_error_mgr base; // Must be at the top
48
- jmp_buf setjmp_buffer;
49
- };
50
- #endif
51
-
52
- const int MJpegDecoder::kColorSpaceUnknown = JCS_UNKNOWN;
53
- const int MJpegDecoder::kColorSpaceGrayscale = JCS_GRAYSCALE;
54
- const int MJpegDecoder::kColorSpaceRgb = JCS_RGB;
55
- const int MJpegDecoder::kColorSpaceYCbCr = JCS_YCbCr;
56
- const int MJpegDecoder::kColorSpaceCMYK = JCS_CMYK;
57
- const int MJpegDecoder::kColorSpaceYCCK = JCS_YCCK;
58
-
59
- // Methods that are passed to jpeglib.
60
- boolean fill_input_buffer(jpeg_decompress_struct* cinfo);
61
- void init_source(jpeg_decompress_struct* cinfo);
62
- void skip_input_data(jpeg_decompress_struct* cinfo, long num_bytes); // NOLINT
63
- void term_source(jpeg_decompress_struct* cinfo);
64
- void ErrorHandler(jpeg_common_struct* cinfo);
65
- void OutputHandler(jpeg_common_struct* cinfo);
66
-
67
- MJpegDecoder::MJpegDecoder()
68
- : has_scanline_padding_(LIBYUV_FALSE),
69
- num_outbufs_(0),
70
- scanlines_(NULL),
71
- scanlines_sizes_(NULL),
72
- databuf_(NULL),
73
- databuf_strides_(NULL) {
74
- decompress_struct_ = new jpeg_decompress_struct;
75
- source_mgr_ = new jpeg_source_mgr;
76
- #ifdef HAVE_SETJMP
77
- error_mgr_ = new SetJmpErrorMgr;
78
- decompress_struct_->err = jpeg_std_error(&error_mgr_->base);
79
- // Override standard exit()-based error handler.
80
- error_mgr_->base.error_exit = &ErrorHandler;
81
- error_mgr_->base.output_message = &OutputHandler;
82
- #endif
83
- decompress_struct_->client_data = NULL;
84
- source_mgr_->init_source = &init_source;
85
- source_mgr_->fill_input_buffer = &fill_input_buffer;
86
- source_mgr_->skip_input_data = &skip_input_data;
87
- source_mgr_->resync_to_restart = &jpeg_resync_to_restart;
88
- source_mgr_->term_source = &term_source;
89
- jpeg_create_decompress(decompress_struct_);
90
- decompress_struct_->src = source_mgr_;
91
- buf_vec_.buffers = &buf_;
92
- buf_vec_.len = 1;
93
- }
94
-
95
- MJpegDecoder::~MJpegDecoder() {
96
- jpeg_destroy_decompress(decompress_struct_);
97
- delete decompress_struct_;
98
- delete source_mgr_;
99
- #ifdef HAVE_SETJMP
100
- delete error_mgr_;
101
- #endif
102
- DestroyOutputBuffers();
103
- }
104
-
105
- LIBYUV_BOOL MJpegDecoder::LoadFrame(const uint8_t* src, size_t src_len) {
106
- if (!ValidateJpeg(src, src_len)) {
107
- return LIBYUV_FALSE;
108
- }
109
-
110
- buf_.data = src;
111
- buf_.len = (int)src_len;
112
- buf_vec_.pos = 0;
113
- decompress_struct_->client_data = &buf_vec_;
114
- #ifdef HAVE_SETJMP
115
- if (setjmp(error_mgr_->setjmp_buffer)) {
116
- // We called jpeg_read_header, it experienced an error, and we called
117
- // longjmp() and rewound the stack to here. Return error.
118
- return LIBYUV_FALSE;
119
- }
120
- #endif
121
- if (jpeg_read_header(decompress_struct_, TRUE) != JPEG_HEADER_OK) {
122
- // ERROR: Bad MJPEG header
123
- return LIBYUV_FALSE;
124
- }
125
- AllocOutputBuffers(GetNumComponents());
126
- for (int i = 0; i < num_outbufs_; ++i) {
127
- int scanlines_size = GetComponentScanlinesPerImcuRow(i);
128
- if (scanlines_sizes_[i] != scanlines_size) {
129
- if (scanlines_[i]) {
130
- delete scanlines_[i];
131
- }
132
- scanlines_[i] = new uint8_t*[scanlines_size];
133
- scanlines_sizes_[i] = scanlines_size;
134
- }
135
-
136
- // We allocate padding for the final scanline to pad it up to DCTSIZE bytes
137
- // to avoid memory errors, since jpeglib only reads full MCUs blocks. For
138
- // the preceding scanlines, the padding is not needed/wanted because the
139
- // following addresses will already be valid (they are the initial bytes of
140
- // the next scanline) and will be overwritten when jpeglib writes out that
141
- // next scanline.
142
- int databuf_stride = GetComponentStride(i);
143
- int databuf_size = scanlines_size * databuf_stride;
144
- if (databuf_strides_[i] != databuf_stride) {
145
- if (databuf_[i]) {
146
- delete databuf_[i];
147
- }
148
- databuf_[i] = new uint8_t[databuf_size];
149
- databuf_strides_[i] = databuf_stride;
150
- }
151
-
152
- if (GetComponentStride(i) != GetComponentWidth(i)) {
153
- has_scanline_padding_ = LIBYUV_TRUE;
154
- }
155
- }
156
- return LIBYUV_TRUE;
157
- }
158
-
159
- static int DivideAndRoundUp(int numerator, int denominator) {
160
- return (numerator + denominator - 1) / denominator;
161
- }
162
-
163
- static int DivideAndRoundDown(int numerator, int denominator) {
164
- return numerator / denominator;
165
- }
166
-
167
- // Returns width of the last loaded frame.
168
- int MJpegDecoder::GetWidth() {
169
- return decompress_struct_->image_width;
170
- }
171
-
172
- // Returns height of the last loaded frame.
173
- int MJpegDecoder::GetHeight() {
174
- return decompress_struct_->image_height;
175
- }
176
-
177
- // Returns format of the last loaded frame. The return value is one of the
178
- // kColorSpace* constants.
179
- int MJpegDecoder::GetColorSpace() {
180
- return decompress_struct_->jpeg_color_space;
181
- }
182
-
183
- // Number of color components in the color space.
184
- int MJpegDecoder::GetNumComponents() {
185
- return decompress_struct_->num_components;
186
- }
187
-
188
- // Sample factors of the n-th component.
189
- int MJpegDecoder::GetHorizSampFactor(int component) {
190
- return decompress_struct_->comp_info[component].h_samp_factor;
191
- }
192
-
193
- int MJpegDecoder::GetVertSampFactor(int component) {
194
- return decompress_struct_->comp_info[component].v_samp_factor;
195
- }
196
-
197
- int MJpegDecoder::GetHorizSubSampFactor(int component) {
198
- return decompress_struct_->max_h_samp_factor / GetHorizSampFactor(component);
199
- }
200
-
201
- int MJpegDecoder::GetVertSubSampFactor(int component) {
202
- return decompress_struct_->max_v_samp_factor / GetVertSampFactor(component);
203
- }
204
-
205
- int MJpegDecoder::GetImageScanlinesPerImcuRow() {
206
- return decompress_struct_->max_v_samp_factor * DCTSIZE;
207
- }
208
-
209
- int MJpegDecoder::GetComponentScanlinesPerImcuRow(int component) {
210
- int vs = GetVertSubSampFactor(component);
211
- return DivideAndRoundUp(GetImageScanlinesPerImcuRow(), vs);
212
- }
213
-
214
- int MJpegDecoder::GetComponentWidth(int component) {
215
- int hs = GetHorizSubSampFactor(component);
216
- return DivideAndRoundUp(GetWidth(), hs);
217
- }
218
-
219
- int MJpegDecoder::GetComponentHeight(int component) {
220
- int vs = GetVertSubSampFactor(component);
221
- return DivideAndRoundUp(GetHeight(), vs);
222
- }
223
-
224
- // Get width in bytes padded out to a multiple of DCTSIZE
225
- int MJpegDecoder::GetComponentStride(int component) {
226
- return (GetComponentWidth(component) + DCTSIZE - 1) & ~(DCTSIZE - 1);
227
- }
228
-
229
- int MJpegDecoder::GetComponentSize(int component) {
230
- return GetComponentWidth(component) * GetComponentHeight(component);
231
- }
232
-
233
- LIBYUV_BOOL MJpegDecoder::UnloadFrame() {
234
- #ifdef HAVE_SETJMP
235
- if (setjmp(error_mgr_->setjmp_buffer)) {
236
- // We called jpeg_abort_decompress, it experienced an error, and we called
237
- // longjmp() and rewound the stack to here. Return error.
238
- return LIBYUV_FALSE;
239
- }
240
- #endif
241
- jpeg_abort_decompress(decompress_struct_);
242
- return LIBYUV_TRUE;
243
- }
244
-
245
- // TODO(fbarchard): Allow rectangle to be specified: x, y, width, height.
246
- LIBYUV_BOOL MJpegDecoder::DecodeToBuffers(uint8_t** planes,
247
- int dst_width,
248
- int dst_height) {
249
- if (dst_width != GetWidth() || dst_height > GetHeight()) {
250
- // ERROR: Bad dimensions
251
- return LIBYUV_FALSE;
252
- }
253
- #ifdef HAVE_SETJMP
254
- if (setjmp(error_mgr_->setjmp_buffer)) {
255
- // We called into jpeglib, it experienced an error sometime during this
256
- // function call, and we called longjmp() and rewound the stack to here.
257
- // Return error.
258
- return LIBYUV_FALSE;
259
- }
260
- #endif
261
- if (!StartDecode()) {
262
- return LIBYUV_FALSE;
263
- }
264
- SetScanlinePointers(databuf_);
265
- int lines_left = dst_height;
266
- // Compute amount of lines to skip to implement vertical crop.
267
- // TODO(fbarchard): Ensure skip is a multiple of maximum component
268
- // subsample. ie 2
269
- int skip = (GetHeight() - dst_height) / 2;
270
- if (skip > 0) {
271
- // There is no API to skip lines in the output data, so we read them
272
- // into the temp buffer.
273
- while (skip >= GetImageScanlinesPerImcuRow()) {
274
- if (!DecodeImcuRow()) {
275
- FinishDecode();
276
- return LIBYUV_FALSE;
277
- }
278
- skip -= GetImageScanlinesPerImcuRow();
279
- }
280
- if (skip > 0) {
281
- // Have a partial iMCU row left over to skip. Must read it and then
282
- // copy the parts we want into the destination.
283
- if (!DecodeImcuRow()) {
284
- FinishDecode();
285
- return LIBYUV_FALSE;
286
- }
287
- for (int i = 0; i < num_outbufs_; ++i) {
288
- // TODO(fbarchard): Compute skip to avoid this
289
- assert(skip % GetVertSubSampFactor(i) == 0);
290
- int rows_to_skip = DivideAndRoundDown(skip, GetVertSubSampFactor(i));
291
- int scanlines_to_copy =
292
- GetComponentScanlinesPerImcuRow(i) - rows_to_skip;
293
- int data_to_skip = rows_to_skip * GetComponentStride(i);
294
- CopyPlane(databuf_[i] + data_to_skip, GetComponentStride(i), planes[i],
295
- GetComponentWidth(i), GetComponentWidth(i),
296
- scanlines_to_copy);
297
- planes[i] += scanlines_to_copy * GetComponentWidth(i);
298
- }
299
- lines_left -= (GetImageScanlinesPerImcuRow() - skip);
300
- }
301
- }
302
-
303
- // Read full MCUs but cropped horizontally
304
- for (; lines_left > GetImageScanlinesPerImcuRow();
305
- lines_left -= GetImageScanlinesPerImcuRow()) {
306
- if (!DecodeImcuRow()) {
307
- FinishDecode();
308
- return LIBYUV_FALSE;
309
- }
310
- for (int i = 0; i < num_outbufs_; ++i) {
311
- int scanlines_to_copy = GetComponentScanlinesPerImcuRow(i);
312
- CopyPlane(databuf_[i], GetComponentStride(i), planes[i],
313
- GetComponentWidth(i), GetComponentWidth(i), scanlines_to_copy);
314
- planes[i] += scanlines_to_copy * GetComponentWidth(i);
315
- }
316
- }
317
-
318
- if (lines_left > 0) {
319
- // Have a partial iMCU row left over to decode.
320
- if (!DecodeImcuRow()) {
321
- FinishDecode();
322
- return LIBYUV_FALSE;
323
- }
324
- for (int i = 0; i < num_outbufs_; ++i) {
325
- int scanlines_to_copy =
326
- DivideAndRoundUp(lines_left, GetVertSubSampFactor(i));
327
- CopyPlane(databuf_[i], GetComponentStride(i), planes[i],
328
- GetComponentWidth(i), GetComponentWidth(i), scanlines_to_copy);
329
- planes[i] += scanlines_to_copy * GetComponentWidth(i);
330
- }
331
- }
332
- return FinishDecode();
333
- }
334
-
335
- LIBYUV_BOOL MJpegDecoder::DecodeToCallback(CallbackFunction fn,
336
- void* opaque,
337
- int dst_width,
338
- int dst_height) {
339
- if (dst_width != GetWidth() || dst_height > GetHeight()) {
340
- // ERROR: Bad dimensions
341
- return LIBYUV_FALSE;
342
- }
343
- #ifdef HAVE_SETJMP
344
- if (setjmp(error_mgr_->setjmp_buffer)) {
345
- // We called into jpeglib, it experienced an error sometime during this
346
- // function call, and we called longjmp() and rewound the stack to here.
347
- // Return error.
348
- return LIBYUV_FALSE;
349
- }
350
- #endif
351
- if (!StartDecode()) {
352
- return LIBYUV_FALSE;
353
- }
354
- SetScanlinePointers(databuf_);
355
- int lines_left = dst_height;
356
- // TODO(fbarchard): Compute amount of lines to skip to implement vertical crop
357
- int skip = (GetHeight() - dst_height) / 2;
358
- if (skip > 0) {
359
- while (skip >= GetImageScanlinesPerImcuRow()) {
360
- if (!DecodeImcuRow()) {
361
- FinishDecode();
362
- return LIBYUV_FALSE;
363
- }
364
- skip -= GetImageScanlinesPerImcuRow();
365
- }
366
- if (skip > 0) {
367
- // Have a partial iMCU row left over to skip.
368
- if (!DecodeImcuRow()) {
369
- FinishDecode();
370
- return LIBYUV_FALSE;
371
- }
372
- for (int i = 0; i < num_outbufs_; ++i) {
373
- // TODO(fbarchard): Compute skip to avoid this
374
- assert(skip % GetVertSubSampFactor(i) == 0);
375
- int rows_to_skip = DivideAndRoundDown(skip, GetVertSubSampFactor(i));
376
- int data_to_skip = rows_to_skip * GetComponentStride(i);
377
- // Change our own data buffer pointers so we can pass them to the
378
- // callback.
379
- databuf_[i] += data_to_skip;
380
- }
381
- int scanlines_to_copy = GetImageScanlinesPerImcuRow() - skip;
382
- (*fn)(opaque, databuf_, databuf_strides_, scanlines_to_copy);
383
- // Now change them back.
384
- for (int i = 0; i < num_outbufs_; ++i) {
385
- int rows_to_skip = DivideAndRoundDown(skip, GetVertSubSampFactor(i));
386
- int data_to_skip = rows_to_skip * GetComponentStride(i);
387
- databuf_[i] -= data_to_skip;
388
- }
389
- lines_left -= scanlines_to_copy;
390
- }
391
- }
392
- // Read full MCUs until we get to the crop point.
393
- for (; lines_left >= GetImageScanlinesPerImcuRow();
394
- lines_left -= GetImageScanlinesPerImcuRow()) {
395
- if (!DecodeImcuRow()) {
396
- FinishDecode();
397
- return LIBYUV_FALSE;
398
- }
399
- (*fn)(opaque, databuf_, databuf_strides_, GetImageScanlinesPerImcuRow());
400
- }
401
- if (lines_left > 0) {
402
- // Have a partial iMCU row left over to decode.
403
- if (!DecodeImcuRow()) {
404
- FinishDecode();
405
- return LIBYUV_FALSE;
406
- }
407
- (*fn)(opaque, databuf_, databuf_strides_, lines_left);
408
- }
409
- return FinishDecode();
410
- }
411
-
412
- void init_source(j_decompress_ptr cinfo) {
413
- fill_input_buffer(cinfo);
414
- }
415
-
416
- boolean fill_input_buffer(j_decompress_ptr cinfo) {
417
- BufferVector* buf_vec = reinterpret_cast<BufferVector*>(cinfo->client_data);
418
- if (buf_vec->pos >= buf_vec->len) {
419
- // ERROR: No more data
420
- return FALSE;
421
- }
422
- cinfo->src->next_input_byte = buf_vec->buffers[buf_vec->pos].data;
423
- cinfo->src->bytes_in_buffer = buf_vec->buffers[buf_vec->pos].len;
424
- ++buf_vec->pos;
425
- return TRUE;
426
- }
427
-
428
- void skip_input_data(j_decompress_ptr cinfo, long num_bytes) { // NOLINT
429
- jpeg_source_mgr* src = cinfo->src;
430
- size_t bytes = (size_t)num_bytes;
431
- if (bytes > src->bytes_in_buffer) {
432
- src->next_input_byte = nullptr;
433
- src->bytes_in_buffer = 0;
434
- } else {
435
- src->next_input_byte += bytes;
436
- src->bytes_in_buffer -= bytes;
437
- }
438
- }
439
-
440
- void term_source(j_decompress_ptr cinfo) {
441
- (void)cinfo; // Nothing to do.
442
- }
443
-
444
- #ifdef HAVE_SETJMP
445
- void ErrorHandler(j_common_ptr cinfo) {
446
- // This is called when a jpeglib command experiences an error. Unfortunately
447
- // jpeglib's error handling model is not very flexible, because it expects the
448
- // error handler to not return--i.e., it wants the program to terminate. To
449
- // recover from errors we use setjmp() as shown in their example. setjmp() is
450
- // C's implementation for the "call with current continuation" functionality
451
- // seen in some functional programming languages.
452
- // A formatted message can be output, but is unsafe for release.
453
- #ifdef DEBUG
454
- char buf[JMSG_LENGTH_MAX];
455
- (*cinfo->err->format_message)(cinfo, buf);
456
- // ERROR: Error in jpeglib: buf
457
- #endif
458
-
459
- SetJmpErrorMgr* mgr = reinterpret_cast<SetJmpErrorMgr*>(cinfo->err);
460
- // This rewinds the call stack to the point of the corresponding setjmp()
461
- // and causes it to return (for a second time) with value 1.
462
- longjmp(mgr->setjmp_buffer, 1);
463
- }
464
-
465
- // Suppress fprintf warnings.
466
- void OutputHandler(j_common_ptr cinfo) {
467
- (void)cinfo;
468
- }
469
-
470
- #endif // HAVE_SETJMP
471
-
472
- void MJpegDecoder::AllocOutputBuffers(int num_outbufs) {
473
- if (num_outbufs != num_outbufs_) {
474
- // We could perhaps optimize this case to resize the output buffers without
475
- // necessarily having to delete and recreate each one, but it's not worth
476
- // it.
477
- DestroyOutputBuffers();
478
-
479
- scanlines_ = new uint8_t**[num_outbufs];
480
- scanlines_sizes_ = new int[num_outbufs];
481
- databuf_ = new uint8_t*[num_outbufs];
482
- databuf_strides_ = new int[num_outbufs];
483
-
484
- for (int i = 0; i < num_outbufs; ++i) {
485
- scanlines_[i] = NULL;
486
- scanlines_sizes_[i] = 0;
487
- databuf_[i] = NULL;
488
- databuf_strides_[i] = 0;
489
- }
490
-
491
- num_outbufs_ = num_outbufs;
492
- }
493
- }
494
-
495
- void MJpegDecoder::DestroyOutputBuffers() {
496
- for (int i = 0; i < num_outbufs_; ++i) {
497
- delete[] scanlines_[i];
498
- delete[] databuf_[i];
499
- }
500
- delete[] scanlines_;
501
- delete[] databuf_;
502
- delete[] scanlines_sizes_;
503
- delete[] databuf_strides_;
504
- scanlines_ = NULL;
505
- databuf_ = NULL;
506
- scanlines_sizes_ = NULL;
507
- databuf_strides_ = NULL;
508
- num_outbufs_ = 0;
509
- }
510
-
511
- // JDCT_IFAST and do_block_smoothing improve performance substantially.
512
- LIBYUV_BOOL MJpegDecoder::StartDecode() {
513
- decompress_struct_->raw_data_out = TRUE;
514
- decompress_struct_->dct_method = JDCT_IFAST; // JDCT_ISLOW is default
515
- decompress_struct_->dither_mode = JDITHER_NONE;
516
- // Not applicable to 'raw':
517
- decompress_struct_->do_fancy_upsampling = (boolean)(LIBYUV_FALSE);
518
- // Only for buffered mode:
519
- decompress_struct_->enable_2pass_quant = (boolean)(LIBYUV_FALSE);
520
- // Blocky but fast:
521
- decompress_struct_->do_block_smoothing = (boolean)(LIBYUV_FALSE);
522
-
523
- if (!jpeg_start_decompress(decompress_struct_)) {
524
- // ERROR: Couldn't start JPEG decompressor";
525
- return LIBYUV_FALSE;
526
- }
527
- return LIBYUV_TRUE;
528
- }
529
-
530
- LIBYUV_BOOL MJpegDecoder::FinishDecode() {
531
- // jpeglib considers it an error if we finish without decoding the whole
532
- // image, so we call "abort" rather than "finish".
533
- jpeg_abort_decompress(decompress_struct_);
534
- return LIBYUV_TRUE;
535
- }
536
-
537
- void MJpegDecoder::SetScanlinePointers(uint8_t** data) {
538
- for (int i = 0; i < num_outbufs_; ++i) {
539
- uint8_t* data_i = data[i];
540
- for (int j = 0; j < scanlines_sizes_[i]; ++j) {
541
- scanlines_[i][j] = data_i;
542
- data_i += GetComponentStride(i);
543
- }
544
- }
545
- }
546
-
547
- inline LIBYUV_BOOL MJpegDecoder::DecodeImcuRow() {
548
- return (unsigned int)(GetImageScanlinesPerImcuRow()) ==
549
- jpeg_read_raw_data(decompress_struct_, scanlines_,
550
- GetImageScanlinesPerImcuRow());
551
- }
552
-
553
- // The helper function which recognizes the jpeg sub-sampling type.
554
- JpegSubsamplingType MJpegDecoder::JpegSubsamplingTypeHelper(
555
- int* subsample_x,
556
- int* subsample_y,
557
- int number_of_components) {
558
- if (number_of_components == 3) { // Color images.
559
- if (subsample_x[0] == 1 && subsample_y[0] == 1 && subsample_x[1] == 2 &&
560
- subsample_y[1] == 2 && subsample_x[2] == 2 && subsample_y[2] == 2) {
561
- return kJpegYuv420;
562
- }
563
- if (subsample_x[0] == 1 && subsample_y[0] == 1 && subsample_x[1] == 2 &&
564
- subsample_y[1] == 1 && subsample_x[2] == 2 && subsample_y[2] == 1) {
565
- return kJpegYuv422;
566
- }
567
- if (subsample_x[0] == 1 && subsample_y[0] == 1 && subsample_x[1] == 1 &&
568
- subsample_y[1] == 1 && subsample_x[2] == 1 && subsample_y[2] == 1) {
569
- return kJpegYuv444;
570
- }
571
- } else if (number_of_components == 1) { // Grey-scale images.
572
- if (subsample_x[0] == 1 && subsample_y[0] == 1) {
573
- return kJpegYuv400;
574
- }
575
- }
576
- return kJpegUnknown;
577
- }
578
-
579
- } // namespace libyuv
580
- #endif // HAVE_JPEG
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
+
11
+ #include "libyuv/mjpeg_decoder.h"
12
+
13
+ #ifdef HAVE_JPEG
14
+ #include <assert.h>
15
+
16
+ #if !defined(__pnacl__) && !defined(__CLR_VER) && !defined(COVERAGE_ENABLED)
17
+ // Must be included before jpeglib.
18
+ #include <setjmp.h>
19
+ #define HAVE_SETJMP
20
+
21
+ #if defined(_MSC_VER)
22
+ // disable warning 4324: structure was padded due to __declspec(align())
23
+ #pragma warning(disable : 4324)
24
+ #endif
25
+
26
+ #endif
27
+
28
+ #include <stdio.h> // For jpeglib.h.
29
+
30
+ // C++ build requires extern C for jpeg internals.
31
+ #ifdef __cplusplus
32
+ extern "C" {
33
+ #endif
34
+
35
+ #include <jpeglib.h>
36
+
37
+ #ifdef __cplusplus
38
+ } // extern "C"
39
+ #endif
40
+
41
+ #include "libyuv/planar_functions.h" // For CopyPlane().
42
+
43
+ namespace libyuv {
44
+
45
+ #ifdef HAVE_SETJMP
46
+ struct SetJmpErrorMgr {
47
+ jpeg_error_mgr base; // Must be at the top
48
+ jmp_buf setjmp_buffer;
49
+ };
50
+ #endif
51
+
52
+ const int MJpegDecoder::kColorSpaceUnknown = JCS_UNKNOWN;
53
+ const int MJpegDecoder::kColorSpaceGrayscale = JCS_GRAYSCALE;
54
+ const int MJpegDecoder::kColorSpaceRgb = JCS_RGB;
55
+ const int MJpegDecoder::kColorSpaceYCbCr = JCS_YCbCr;
56
+ const int MJpegDecoder::kColorSpaceCMYK = JCS_CMYK;
57
+ const int MJpegDecoder::kColorSpaceYCCK = JCS_YCCK;
58
+
59
+ // Methods that are passed to jpeglib.
60
+ boolean fill_input_buffer(jpeg_decompress_struct* cinfo);
61
+ void init_source(jpeg_decompress_struct* cinfo);
62
+ void skip_input_data(jpeg_decompress_struct* cinfo, long num_bytes); // NOLINT
63
+ void term_source(jpeg_decompress_struct* cinfo);
64
+ void ErrorHandler(jpeg_common_struct* cinfo);
65
+ void OutputHandler(jpeg_common_struct* cinfo);
66
+
67
+ MJpegDecoder::MJpegDecoder()
68
+ : has_scanline_padding_(LIBYUV_FALSE),
69
+ num_outbufs_(0),
70
+ scanlines_(NULL),
71
+ scanlines_sizes_(NULL),
72
+ databuf_(NULL),
73
+ databuf_strides_(NULL) {
74
+ decompress_struct_ = new jpeg_decompress_struct;
75
+ source_mgr_ = new jpeg_source_mgr;
76
+ #ifdef HAVE_SETJMP
77
+ error_mgr_ = new SetJmpErrorMgr;
78
+ decompress_struct_->err = jpeg_std_error(&error_mgr_->base);
79
+ // Override standard exit()-based error handler.
80
+ error_mgr_->base.error_exit = &ErrorHandler;
81
+ error_mgr_->base.output_message = &OutputHandler;
82
+ #endif
83
+ decompress_struct_->client_data = NULL;
84
+ source_mgr_->init_source = &init_source;
85
+ source_mgr_->fill_input_buffer = &fill_input_buffer;
86
+ source_mgr_->skip_input_data = &skip_input_data;
87
+ source_mgr_->resync_to_restart = &jpeg_resync_to_restart;
88
+ source_mgr_->term_source = &term_source;
89
+ jpeg_create_decompress(decompress_struct_);
90
+ decompress_struct_->src = source_mgr_;
91
+ buf_vec_.buffers = &buf_;
92
+ buf_vec_.len = 1;
93
+ }
94
+
95
+ MJpegDecoder::~MJpegDecoder() {
96
+ jpeg_destroy_decompress(decompress_struct_);
97
+ delete decompress_struct_;
98
+ delete source_mgr_;
99
+ #ifdef HAVE_SETJMP
100
+ delete error_mgr_;
101
+ #endif
102
+ DestroyOutputBuffers();
103
+ }
104
+
105
+ LIBYUV_BOOL MJpegDecoder::LoadFrame(const uint8_t* src, size_t src_len) {
106
+ if (!ValidateJpeg(src, src_len)) {
107
+ return LIBYUV_FALSE;
108
+ }
109
+
110
+ buf_.data = src;
111
+ buf_.len = (int)src_len;
112
+ buf_vec_.pos = 0;
113
+ decompress_struct_->client_data = &buf_vec_;
114
+ #ifdef HAVE_SETJMP
115
+ if (setjmp(error_mgr_->setjmp_buffer)) {
116
+ // We called jpeg_read_header, it experienced an error, and we called
117
+ // longjmp() and rewound the stack to here. Return error.
118
+ return LIBYUV_FALSE;
119
+ }
120
+ #endif
121
+ if (jpeg_read_header(decompress_struct_, TRUE) != JPEG_HEADER_OK) {
122
+ // ERROR: Bad MJPEG header
123
+ return LIBYUV_FALSE;
124
+ }
125
+ AllocOutputBuffers(GetNumComponents());
126
+ for (int i = 0; i < num_outbufs_; ++i) {
127
+ int scanlines_size = GetComponentScanlinesPerImcuRow(i);
128
+ if (scanlines_sizes_[i] != scanlines_size) {
129
+ if (scanlines_[i]) {
130
+ delete scanlines_[i];
131
+ }
132
+ scanlines_[i] = new uint8_t*[scanlines_size];
133
+ scanlines_sizes_[i] = scanlines_size;
134
+ }
135
+
136
+ // We allocate padding for the final scanline to pad it up to DCTSIZE bytes
137
+ // to avoid memory errors, since jpeglib only reads full MCUs blocks. For
138
+ // the preceding scanlines, the padding is not needed/wanted because the
139
+ // following addresses will already be valid (they are the initial bytes of
140
+ // the next scanline) and will be overwritten when jpeglib writes out that
141
+ // next scanline.
142
+ int databuf_stride = GetComponentStride(i);
143
+ int databuf_size = scanlines_size * databuf_stride;
144
+ if (databuf_strides_[i] != databuf_stride) {
145
+ if (databuf_[i]) {
146
+ delete databuf_[i];
147
+ }
148
+ databuf_[i] = new uint8_t[databuf_size];
149
+ databuf_strides_[i] = databuf_stride;
150
+ }
151
+
152
+ if (GetComponentStride(i) != GetComponentWidth(i)) {
153
+ has_scanline_padding_ = LIBYUV_TRUE;
154
+ }
155
+ }
156
+ return LIBYUV_TRUE;
157
+ }
158
+
159
+ static int DivideAndRoundUp(int numerator, int denominator) {
160
+ return (numerator + denominator - 1) / denominator;
161
+ }
162
+
163
+ static int DivideAndRoundDown(int numerator, int denominator) {
164
+ return numerator / denominator;
165
+ }
166
+
167
+ // Returns width of the last loaded frame.
168
+ int MJpegDecoder::GetWidth() {
169
+ return decompress_struct_->image_width;
170
+ }
171
+
172
+ // Returns height of the last loaded frame.
173
+ int MJpegDecoder::GetHeight() {
174
+ return decompress_struct_->image_height;
175
+ }
176
+
177
+ // Returns format of the last loaded frame. The return value is one of the
178
+ // kColorSpace* constants.
179
+ int MJpegDecoder::GetColorSpace() {
180
+ return decompress_struct_->jpeg_color_space;
181
+ }
182
+
183
+ // Number of color components in the color space.
184
+ int MJpegDecoder::GetNumComponents() {
185
+ return decompress_struct_->num_components;
186
+ }
187
+
188
+ // Sample factors of the n-th component.
189
+ int MJpegDecoder::GetHorizSampFactor(int component) {
190
+ return decompress_struct_->comp_info[component].h_samp_factor;
191
+ }
192
+
193
+ int MJpegDecoder::GetVertSampFactor(int component) {
194
+ return decompress_struct_->comp_info[component].v_samp_factor;
195
+ }
196
+
197
+ int MJpegDecoder::GetHorizSubSampFactor(int component) {
198
+ return decompress_struct_->max_h_samp_factor / GetHorizSampFactor(component);
199
+ }
200
+
201
+ int MJpegDecoder::GetVertSubSampFactor(int component) {
202
+ return decompress_struct_->max_v_samp_factor / GetVertSampFactor(component);
203
+ }
204
+
205
+ int MJpegDecoder::GetImageScanlinesPerImcuRow() {
206
+ return decompress_struct_->max_v_samp_factor * DCTSIZE;
207
+ }
208
+
209
+ int MJpegDecoder::GetComponentScanlinesPerImcuRow(int component) {
210
+ int vs = GetVertSubSampFactor(component);
211
+ return DivideAndRoundUp(GetImageScanlinesPerImcuRow(), vs);
212
+ }
213
+
214
+ int MJpegDecoder::GetComponentWidth(int component) {
215
+ int hs = GetHorizSubSampFactor(component);
216
+ return DivideAndRoundUp(GetWidth(), hs);
217
+ }
218
+
219
+ int MJpegDecoder::GetComponentHeight(int component) {
220
+ int vs = GetVertSubSampFactor(component);
221
+ return DivideAndRoundUp(GetHeight(), vs);
222
+ }
223
+
224
+ // Get width in bytes padded out to a multiple of DCTSIZE
225
+ int MJpegDecoder::GetComponentStride(int component) {
226
+ return (GetComponentWidth(component) + DCTSIZE - 1) & ~(DCTSIZE - 1);
227
+ }
228
+
229
+ int MJpegDecoder::GetComponentSize(int component) {
230
+ return GetComponentWidth(component) * GetComponentHeight(component);
231
+ }
232
+
233
+ LIBYUV_BOOL MJpegDecoder::UnloadFrame() {
234
+ #ifdef HAVE_SETJMP
235
+ if (setjmp(error_mgr_->setjmp_buffer)) {
236
+ // We called jpeg_abort_decompress, it experienced an error, and we called
237
+ // longjmp() and rewound the stack to here. Return error.
238
+ return LIBYUV_FALSE;
239
+ }
240
+ #endif
241
+ jpeg_abort_decompress(decompress_struct_);
242
+ return LIBYUV_TRUE;
243
+ }
244
+
245
+ // TODO(fbarchard): Allow rectangle to be specified: x, y, width, height.
246
+ LIBYUV_BOOL MJpegDecoder::DecodeToBuffers(uint8_t** planes,
247
+ int dst_width,
248
+ int dst_height) {
249
+ if (dst_width != GetWidth() || dst_height > GetHeight()) {
250
+ // ERROR: Bad dimensions
251
+ return LIBYUV_FALSE;
252
+ }
253
+ #ifdef HAVE_SETJMP
254
+ if (setjmp(error_mgr_->setjmp_buffer)) {
255
+ // We called into jpeglib, it experienced an error sometime during this
256
+ // function call, and we called longjmp() and rewound the stack to here.
257
+ // Return error.
258
+ return LIBYUV_FALSE;
259
+ }
260
+ #endif
261
+ if (!StartDecode()) {
262
+ return LIBYUV_FALSE;
263
+ }
264
+ SetScanlinePointers(databuf_);
265
+ int lines_left = dst_height;
266
+ // Compute amount of lines to skip to implement vertical crop.
267
+ // TODO(fbarchard): Ensure skip is a multiple of maximum component
268
+ // subsample. ie 2
269
+ int skip = (GetHeight() - dst_height) / 2;
270
+ if (skip > 0) {
271
+ // There is no API to skip lines in the output data, so we read them
272
+ // into the temp buffer.
273
+ while (skip >= GetImageScanlinesPerImcuRow()) {
274
+ if (!DecodeImcuRow()) {
275
+ FinishDecode();
276
+ return LIBYUV_FALSE;
277
+ }
278
+ skip -= GetImageScanlinesPerImcuRow();
279
+ }
280
+ if (skip > 0) {
281
+ // Have a partial iMCU row left over to skip. Must read it and then
282
+ // copy the parts we want into the destination.
283
+ if (!DecodeImcuRow()) {
284
+ FinishDecode();
285
+ return LIBYUV_FALSE;
286
+ }
287
+ for (int i = 0; i < num_outbufs_; ++i) {
288
+ // TODO(fbarchard): Compute skip to avoid this
289
+ assert(skip % GetVertSubSampFactor(i) == 0);
290
+ int rows_to_skip = DivideAndRoundDown(skip, GetVertSubSampFactor(i));
291
+ int scanlines_to_copy =
292
+ GetComponentScanlinesPerImcuRow(i) - rows_to_skip;
293
+ int data_to_skip = rows_to_skip * GetComponentStride(i);
294
+ CopyPlane(databuf_[i] + data_to_skip, GetComponentStride(i), planes[i],
295
+ GetComponentWidth(i), GetComponentWidth(i),
296
+ scanlines_to_copy);
297
+ planes[i] += scanlines_to_copy * GetComponentWidth(i);
298
+ }
299
+ lines_left -= (GetImageScanlinesPerImcuRow() - skip);
300
+ }
301
+ }
302
+
303
+ // Read full MCUs but cropped horizontally
304
+ for (; lines_left > GetImageScanlinesPerImcuRow();
305
+ lines_left -= GetImageScanlinesPerImcuRow()) {
306
+ if (!DecodeImcuRow()) {
307
+ FinishDecode();
308
+ return LIBYUV_FALSE;
309
+ }
310
+ for (int i = 0; i < num_outbufs_; ++i) {
311
+ int scanlines_to_copy = GetComponentScanlinesPerImcuRow(i);
312
+ CopyPlane(databuf_[i], GetComponentStride(i), planes[i],
313
+ GetComponentWidth(i), GetComponentWidth(i), scanlines_to_copy);
314
+ planes[i] += scanlines_to_copy * GetComponentWidth(i);
315
+ }
316
+ }
317
+
318
+ if (lines_left > 0) {
319
+ // Have a partial iMCU row left over to decode.
320
+ if (!DecodeImcuRow()) {
321
+ FinishDecode();
322
+ return LIBYUV_FALSE;
323
+ }
324
+ for (int i = 0; i < num_outbufs_; ++i) {
325
+ int scanlines_to_copy =
326
+ DivideAndRoundUp(lines_left, GetVertSubSampFactor(i));
327
+ CopyPlane(databuf_[i], GetComponentStride(i), planes[i],
328
+ GetComponentWidth(i), GetComponentWidth(i), scanlines_to_copy);
329
+ planes[i] += scanlines_to_copy * GetComponentWidth(i);
330
+ }
331
+ }
332
+ return FinishDecode();
333
+ }
334
+
335
+ LIBYUV_BOOL MJpegDecoder::DecodeToCallback(CallbackFunction fn,
336
+ void* opaque,
337
+ int dst_width,
338
+ int dst_height) {
339
+ if (dst_width != GetWidth() || dst_height > GetHeight()) {
340
+ // ERROR: Bad dimensions
341
+ return LIBYUV_FALSE;
342
+ }
343
+ #ifdef HAVE_SETJMP
344
+ if (setjmp(error_mgr_->setjmp_buffer)) {
345
+ // We called into jpeglib, it experienced an error sometime during this
346
+ // function call, and we called longjmp() and rewound the stack to here.
347
+ // Return error.
348
+ return LIBYUV_FALSE;
349
+ }
350
+ #endif
351
+ if (!StartDecode()) {
352
+ return LIBYUV_FALSE;
353
+ }
354
+ SetScanlinePointers(databuf_);
355
+ int lines_left = dst_height;
356
+ // TODO(fbarchard): Compute amount of lines to skip to implement vertical crop
357
+ int skip = (GetHeight() - dst_height) / 2;
358
+ if (skip > 0) {
359
+ while (skip >= GetImageScanlinesPerImcuRow()) {
360
+ if (!DecodeImcuRow()) {
361
+ FinishDecode();
362
+ return LIBYUV_FALSE;
363
+ }
364
+ skip -= GetImageScanlinesPerImcuRow();
365
+ }
366
+ if (skip > 0) {
367
+ // Have a partial iMCU row left over to skip.
368
+ if (!DecodeImcuRow()) {
369
+ FinishDecode();
370
+ return LIBYUV_FALSE;
371
+ }
372
+ for (int i = 0; i < num_outbufs_; ++i) {
373
+ // TODO(fbarchard): Compute skip to avoid this
374
+ assert(skip % GetVertSubSampFactor(i) == 0);
375
+ int rows_to_skip = DivideAndRoundDown(skip, GetVertSubSampFactor(i));
376
+ int data_to_skip = rows_to_skip * GetComponentStride(i);
377
+ // Change our own data buffer pointers so we can pass them to the
378
+ // callback.
379
+ databuf_[i] += data_to_skip;
380
+ }
381
+ int scanlines_to_copy = GetImageScanlinesPerImcuRow() - skip;
382
+ (*fn)(opaque, databuf_, databuf_strides_, scanlines_to_copy);
383
+ // Now change them back.
384
+ for (int i = 0; i < num_outbufs_; ++i) {
385
+ int rows_to_skip = DivideAndRoundDown(skip, GetVertSubSampFactor(i));
386
+ int data_to_skip = rows_to_skip * GetComponentStride(i);
387
+ databuf_[i] -= data_to_skip;
388
+ }
389
+ lines_left -= scanlines_to_copy;
390
+ }
391
+ }
392
+ // Read full MCUs until we get to the crop point.
393
+ for (; lines_left >= GetImageScanlinesPerImcuRow();
394
+ lines_left -= GetImageScanlinesPerImcuRow()) {
395
+ if (!DecodeImcuRow()) {
396
+ FinishDecode();
397
+ return LIBYUV_FALSE;
398
+ }
399
+ (*fn)(opaque, databuf_, databuf_strides_, GetImageScanlinesPerImcuRow());
400
+ }
401
+ if (lines_left > 0) {
402
+ // Have a partial iMCU row left over to decode.
403
+ if (!DecodeImcuRow()) {
404
+ FinishDecode();
405
+ return LIBYUV_FALSE;
406
+ }
407
+ (*fn)(opaque, databuf_, databuf_strides_, lines_left);
408
+ }
409
+ return FinishDecode();
410
+ }
411
+
412
+ void init_source(j_decompress_ptr cinfo) {
413
+ fill_input_buffer(cinfo);
414
+ }
415
+
416
+ boolean fill_input_buffer(j_decompress_ptr cinfo) {
417
+ BufferVector* buf_vec = reinterpret_cast<BufferVector*>(cinfo->client_data);
418
+ if (buf_vec->pos >= buf_vec->len) {
419
+ // ERROR: No more data
420
+ return FALSE;
421
+ }
422
+ cinfo->src->next_input_byte = buf_vec->buffers[buf_vec->pos].data;
423
+ cinfo->src->bytes_in_buffer = buf_vec->buffers[buf_vec->pos].len;
424
+ ++buf_vec->pos;
425
+ return TRUE;
426
+ }
427
+
428
+ void skip_input_data(j_decompress_ptr cinfo, long num_bytes) { // NOLINT
429
+ jpeg_source_mgr* src = cinfo->src;
430
+ size_t bytes = (size_t)num_bytes;
431
+ if (bytes > src->bytes_in_buffer) {
432
+ src->next_input_byte = nullptr;
433
+ src->bytes_in_buffer = 0;
434
+ } else {
435
+ src->next_input_byte += bytes;
436
+ src->bytes_in_buffer -= bytes;
437
+ }
438
+ }
439
+
440
+ void term_source(j_decompress_ptr cinfo) {
441
+ (void)cinfo; // Nothing to do.
442
+ }
443
+
444
+ #ifdef HAVE_SETJMP
445
+ void ErrorHandler(j_common_ptr cinfo) {
446
+ // This is called when a jpeglib command experiences an error. Unfortunately
447
+ // jpeglib's error handling model is not very flexible, because it expects the
448
+ // error handler to not return--i.e., it wants the program to terminate. To
449
+ // recover from errors we use setjmp() as shown in their example. setjmp() is
450
+ // C's implementation for the "call with current continuation" functionality
451
+ // seen in some functional programming languages.
452
+ // A formatted message can be output, but is unsafe for release.
453
+ #ifdef DEBUG
454
+ char buf[JMSG_LENGTH_MAX];
455
+ (*cinfo->err->format_message)(cinfo, buf);
456
+ // ERROR: Error in jpeglib: buf
457
+ #endif
458
+
459
+ SetJmpErrorMgr* mgr = reinterpret_cast<SetJmpErrorMgr*>(cinfo->err);
460
+ // This rewinds the call stack to the point of the corresponding setjmp()
461
+ // and causes it to return (for a second time) with value 1.
462
+ longjmp(mgr->setjmp_buffer, 1);
463
+ }
464
+
465
+ // Suppress fprintf warnings.
466
+ void OutputHandler(j_common_ptr cinfo) {
467
+ (void)cinfo;
468
+ }
469
+
470
+ #endif // HAVE_SETJMP
471
+
472
+ void MJpegDecoder::AllocOutputBuffers(int num_outbufs) {
473
+ if (num_outbufs != num_outbufs_) {
474
+ // We could perhaps optimize this case to resize the output buffers without
475
+ // necessarily having to delete and recreate each one, but it's not worth
476
+ // it.
477
+ DestroyOutputBuffers();
478
+
479
+ scanlines_ = new uint8_t**[num_outbufs];
480
+ scanlines_sizes_ = new int[num_outbufs];
481
+ databuf_ = new uint8_t*[num_outbufs];
482
+ databuf_strides_ = new int[num_outbufs];
483
+
484
+ for (int i = 0; i < num_outbufs; ++i) {
485
+ scanlines_[i] = NULL;
486
+ scanlines_sizes_[i] = 0;
487
+ databuf_[i] = NULL;
488
+ databuf_strides_[i] = 0;
489
+ }
490
+
491
+ num_outbufs_ = num_outbufs;
492
+ }
493
+ }
494
+
495
+ void MJpegDecoder::DestroyOutputBuffers() {
496
+ for (int i = 0; i < num_outbufs_; ++i) {
497
+ delete[] scanlines_[i];
498
+ delete[] databuf_[i];
499
+ }
500
+ delete[] scanlines_;
501
+ delete[] databuf_;
502
+ delete[] scanlines_sizes_;
503
+ delete[] databuf_strides_;
504
+ scanlines_ = NULL;
505
+ databuf_ = NULL;
506
+ scanlines_sizes_ = NULL;
507
+ databuf_strides_ = NULL;
508
+ num_outbufs_ = 0;
509
+ }
510
+
511
+ // JDCT_IFAST and do_block_smoothing improve performance substantially.
512
+ LIBYUV_BOOL MJpegDecoder::StartDecode() {
513
+ decompress_struct_->raw_data_out = TRUE;
514
+ decompress_struct_->dct_method = JDCT_IFAST; // JDCT_ISLOW is default
515
+ decompress_struct_->dither_mode = JDITHER_NONE;
516
+ // Not applicable to 'raw':
517
+ decompress_struct_->do_fancy_upsampling = (boolean)(LIBYUV_FALSE);
518
+ // Only for buffered mode:
519
+ decompress_struct_->enable_2pass_quant = (boolean)(LIBYUV_FALSE);
520
+ // Blocky but fast:
521
+ decompress_struct_->do_block_smoothing = (boolean)(LIBYUV_FALSE);
522
+
523
+ if (!jpeg_start_decompress(decompress_struct_)) {
524
+ // ERROR: Couldn't start JPEG decompressor";
525
+ return LIBYUV_FALSE;
526
+ }
527
+ return LIBYUV_TRUE;
528
+ }
529
+
530
+ LIBYUV_BOOL MJpegDecoder::FinishDecode() {
531
+ // jpeglib considers it an error if we finish without decoding the whole
532
+ // image, so we call "abort" rather than "finish".
533
+ jpeg_abort_decompress(decompress_struct_);
534
+ return LIBYUV_TRUE;
535
+ }
536
+
537
+ void MJpegDecoder::SetScanlinePointers(uint8_t** data) {
538
+ for (int i = 0; i < num_outbufs_; ++i) {
539
+ uint8_t* data_i = data[i];
540
+ for (int j = 0; j < scanlines_sizes_[i]; ++j) {
541
+ scanlines_[i][j] = data_i;
542
+ data_i += GetComponentStride(i);
543
+ }
544
+ }
545
+ }
546
+
547
+ inline LIBYUV_BOOL MJpegDecoder::DecodeImcuRow() {
548
+ return (unsigned int)(GetImageScanlinesPerImcuRow()) ==
549
+ jpeg_read_raw_data(decompress_struct_, scanlines_,
550
+ GetImageScanlinesPerImcuRow());
551
+ }
552
+
553
+ // The helper function which recognizes the jpeg sub-sampling type.
554
+ JpegSubsamplingType MJpegDecoder::JpegSubsamplingTypeHelper(
555
+ int* subsample_x,
556
+ int* subsample_y,
557
+ int number_of_components) {
558
+ if (number_of_components == 3) { // Color images.
559
+ if (subsample_x[0] == 1 && subsample_y[0] == 1 && subsample_x[1] == 2 &&
560
+ subsample_y[1] == 2 && subsample_x[2] == 2 && subsample_y[2] == 2) {
561
+ return kJpegYuv420;
562
+ }
563
+ if (subsample_x[0] == 1 && subsample_y[0] == 1 && subsample_x[1] == 2 &&
564
+ subsample_y[1] == 1 && subsample_x[2] == 2 && subsample_y[2] == 1) {
565
+ return kJpegYuv422;
566
+ }
567
+ if (subsample_x[0] == 1 && subsample_y[0] == 1 && subsample_x[1] == 1 &&
568
+ subsample_y[1] == 1 && subsample_x[2] == 1 && subsample_y[2] == 1) {
569
+ return kJpegYuv444;
570
+ }
571
+ } else if (number_of_components == 1) { // Grey-scale images.
572
+ if (subsample_x[0] == 1 && subsample_y[0] == 1) {
573
+ return kJpegYuv400;
574
+ }
575
+ }
576
+ return kJpegUnknown;
577
+ }
578
+
579
+ } // namespace libyuv
580
+ #endif // HAVE_JPEG