sw_range_convert.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <string.h>
  19. #include "libavutil/common.h"
  20. #include "libavutil/intreadwrite.h"
  21. #include "libavutil/mem.h"
  22. #include "libavutil/mem_internal.h"
  23. #include "libswscale/swscale.h"
  24. #include "libswscale/swscale_internal.h"
  25. #include "checkasm.h"
  26. static const enum AVPixelFormat pixel_formats[] = {
  27. AV_PIX_FMT_YUV444P,
  28. AV_PIX_FMT_YUV444P9,
  29. AV_PIX_FMT_YUV444P10,
  30. AV_PIX_FMT_YUV444P12,
  31. AV_PIX_FMT_YUV444P14,
  32. AV_PIX_FMT_YUV444P16,
  33. };
  34. static void randomize_buffers(int16_t *buf0, int16_t *buf1, int bit_depth, int width)
  35. {
  36. int32_t *buf0_32 = (int32_t *) buf0;
  37. int32_t *buf1_32 = (int32_t *) buf1;
  38. int mask = (1 << bit_depth) - 1;
  39. int src_shift = bit_depth <= 14 ? 15 - bit_depth : 19 - bit_depth;
  40. for (int i = 0; i < width; i++) {
  41. int32_t r = rnd() & mask;
  42. if (bit_depth == 16) {
  43. buf0_32[i] = r << src_shift;
  44. buf1_32[i] = r << src_shift;
  45. } else {
  46. buf0[i] = r << src_shift;
  47. buf1[i] = r << src_shift;
  48. }
  49. }
  50. }
  51. static void check_lumConvertRange(int from)
  52. {
  53. const char *func_str = from ? "lumRangeFromJpeg" : "lumRangeToJpeg";
  54. #define LARGEST_INPUT_SIZE 1920
  55. static const int input_sizes[] = {8, LARGEST_INPUT_SIZE};
  56. SwsContext *sws;
  57. SwsInternal *c;
  58. LOCAL_ALIGNED_32(int16_t, dst0, [LARGEST_INPUT_SIZE * 2]);
  59. LOCAL_ALIGNED_32(int16_t, dst1, [LARGEST_INPUT_SIZE * 2]);
  60. int32_t *dst0_32 = (int32_t *) dst0;
  61. int32_t *dst1_32 = (int32_t *) dst1;
  62. declare_func(void, int16_t *dst, int width,
  63. uint32_t coeff, int64_t offset);
  64. sws = sws_alloc_context();
  65. if (sws_init_context(sws, NULL, NULL) < 0)
  66. fail();
  67. c = sws_internal(sws);
  68. sws->src_range = from;
  69. sws->dst_range = !from;
  70. for (int pfi = 0; pfi < FF_ARRAY_ELEMS(pixel_formats); pfi++) {
  71. enum AVPixelFormat pix_fmt = pixel_formats[pfi];
  72. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  73. int bit_depth = desc->comp[0].depth;
  74. int sample_size = bit_depth == 16 ? sizeof(int32_t) : sizeof(int16_t);
  75. int src_shift = bit_depth <= 14 ? 15 - bit_depth : 19 - bit_depth;
  76. int mpeg_min = 16 << (bit_depth - 8);
  77. int mpeg_max = 235 << (bit_depth - 8);
  78. int jpeg_max = (1 << bit_depth) - 1;
  79. sws->src_format = pix_fmt;
  80. sws->dst_format = pix_fmt;
  81. c->dstBpc = bit_depth;
  82. ff_sws_init_scale(c);
  83. for (int dstWi = 0; dstWi < FF_ARRAY_ELEMS(input_sizes); dstWi++) {
  84. int width = input_sizes[dstWi];
  85. if (check_func(c->lumConvertRange, "%s%d_%d", func_str, bit_depth, width)) {
  86. randomize_buffers(dst0, dst1, bit_depth, width);
  87. if (bit_depth == 16) {
  88. if (!from) {
  89. dst1_32[0] = dst0_32[0] = mpeg_min << src_shift;
  90. dst1_32[1] = dst0_32[1] = mpeg_max << src_shift;
  91. }
  92. dst1_32[2] = dst0_32[2] = -1;
  93. } else {
  94. if (!from) {
  95. dst1[0] = dst0[0] = mpeg_min << src_shift;
  96. dst1[1] = dst0[1] = mpeg_max << src_shift;
  97. }
  98. dst1[2] = dst0[2] = -1;
  99. }
  100. call_ref(dst0, width,
  101. c->lumConvertRange_coeff, c->lumConvertRange_offset);
  102. call_new(dst1, width,
  103. c->lumConvertRange_coeff, c->lumConvertRange_offset);
  104. if (memcmp(dst0, dst1, width * sample_size))
  105. fail();
  106. if (!from) {
  107. /* check that the mpeg range is respected */
  108. if (bit_depth == 16) {
  109. if ((dst1_32[0] >> src_shift) > 0 || (dst1_32[1] >> src_shift) != jpeg_max)
  110. fail();
  111. } else {
  112. if ((dst1[0] >> src_shift) > 0 || (dst1[1] >> src_shift) != jpeg_max)
  113. fail();
  114. }
  115. }
  116. if (width == LARGEST_INPUT_SIZE && (bit_depth == 8 || bit_depth == 16))
  117. bench_new(dst1, width,
  118. c->lumConvertRange_coeff, c->lumConvertRange_offset);
  119. }
  120. }
  121. }
  122. sws_freeContext(sws);
  123. }
  124. #undef LARGEST_INPUT_SIZE
  125. static void check_chrConvertRange(int from)
  126. {
  127. const char *func_str = from ? "chrRangeFromJpeg" : "chrRangeToJpeg";
  128. #define LARGEST_INPUT_SIZE 1920
  129. static const int input_sizes[] = {8, LARGEST_INPUT_SIZE};
  130. SwsContext *sws;
  131. SwsInternal *c;
  132. LOCAL_ALIGNED_32(int16_t, dstU0, [LARGEST_INPUT_SIZE * 2]);
  133. LOCAL_ALIGNED_32(int16_t, dstV0, [LARGEST_INPUT_SIZE * 2]);
  134. LOCAL_ALIGNED_32(int16_t, dstU1, [LARGEST_INPUT_SIZE * 2]);
  135. LOCAL_ALIGNED_32(int16_t, dstV1, [LARGEST_INPUT_SIZE * 2]);
  136. int32_t *dstU0_32 = (int32_t *) dstU0;
  137. int32_t *dstU1_32 = (int32_t *) dstU1;
  138. declare_func(void, int16_t *dstU, int16_t *dstV, int width,
  139. uint32_t coeff, int64_t offset);
  140. sws = sws_alloc_context();
  141. if (sws_init_context(sws, NULL, NULL) < 0)
  142. fail();
  143. c = sws_internal(sws);
  144. sws->src_range = from;
  145. sws->dst_range = !from;
  146. for (int pfi = 0; pfi < FF_ARRAY_ELEMS(pixel_formats); pfi++) {
  147. enum AVPixelFormat pix_fmt = pixel_formats[pfi];
  148. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  149. int bit_depth = desc->comp[0].depth;
  150. int sample_size = bit_depth == 16 ? sizeof(int32_t) : sizeof(int16_t);
  151. int src_shift = bit_depth <= 14 ? 15 - bit_depth : 19 - bit_depth;
  152. int mpeg_min = 16 << (bit_depth - 8);
  153. int mpeg_max = 240 << (bit_depth - 8);
  154. int jpeg_max = (1 << bit_depth) - 1;
  155. sws->src_format = pix_fmt;
  156. sws->dst_format = pix_fmt;
  157. c->dstBpc = bit_depth;
  158. ff_sws_init_scale(c);
  159. for (int dstWi = 0; dstWi < FF_ARRAY_ELEMS(input_sizes); dstWi++) {
  160. int width = input_sizes[dstWi];
  161. if (check_func(c->chrConvertRange, "%s%d_%d", func_str, bit_depth, width)) {
  162. randomize_buffers(dstU0, dstU1, bit_depth, width);
  163. randomize_buffers(dstV0, dstV1, bit_depth, width);
  164. if (bit_depth == 16) {
  165. if (!from) {
  166. dstU1_32[0] = dstU0_32[0] = mpeg_min << src_shift;
  167. dstU1_32[1] = dstU0_32[1] = mpeg_max << src_shift;
  168. }
  169. dstU1_32[2] = dstU0_32[2] = -1;
  170. } else {
  171. if (!from) {
  172. dstU1[0] = dstU0[0] = mpeg_min << src_shift;
  173. dstU1[1] = dstU0[1] = mpeg_max << src_shift;
  174. }
  175. dstU1[2] = dstU0[2] = -1;
  176. }
  177. call_ref(dstU0, dstV0, width,
  178. c->chrConvertRange_coeff, c->chrConvertRange_offset);
  179. call_new(dstU1, dstV1, width,
  180. c->chrConvertRange_coeff, c->chrConvertRange_offset);
  181. if (memcmp(dstU0, dstU1, width * sample_size) ||
  182. memcmp(dstV0, dstV1, width * sample_size))
  183. fail();
  184. if (!from) {
  185. /* check that the mpeg range is respected */
  186. if (bit_depth == 16) {
  187. if ((dstU1_32[0] >> src_shift) > 0 || (dstU1_32[1] >> src_shift) != jpeg_max)
  188. fail();
  189. } else {
  190. if ((dstU1[0] >> src_shift) > 0 || (dstU1[1] >> src_shift) != jpeg_max)
  191. fail();
  192. }
  193. }
  194. if (width == LARGEST_INPUT_SIZE && (bit_depth == 8 || bit_depth == 16))
  195. bench_new(dstU1, dstV1, width,
  196. c->chrConvertRange_coeff, c->chrConvertRange_offset);
  197. }
  198. }
  199. }
  200. sws_freeContext(sws);
  201. }
  202. #undef LARGEST_INPUT_SIZE
  203. void checkasm_check_sw_range_convert(void)
  204. {
  205. check_lumConvertRange(1);
  206. report("lumRangeFromJpeg");
  207. check_chrConvertRange(1);
  208. report("chrRangeFromJpeg");
  209. check_lumConvertRange(0);
  210. report("lumRangeToJpeg");
  211. check_chrConvertRange(0);
  212. report("chrRangeToJpeg");
  213. }