h264pred.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2015 Henrik Gramner
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <string.h>
  21. #include "checkasm.h"
  22. #include "libavcodec/codec_id.h"
  23. #include "libavcodec/h264pred.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/internal.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/mem_internal.h"
  28. static const int codec_ids[4] = { AV_CODEC_ID_H264, AV_CODEC_ID_VP8, AV_CODEC_ID_RV40, AV_CODEC_ID_SVQ3 };
  29. static const char * const pred4x4_modes[4][15] = {
  30. { /* H.264 */
  31. [VERT_PRED ] = "vertical",
  32. [HOR_PRED ] = "horizontal",
  33. [DC_PRED ] = "dc",
  34. [DIAG_DOWN_LEFT_PRED ] = "down_left",
  35. [DIAG_DOWN_RIGHT_PRED] = "down_right",
  36. [VERT_RIGHT_PRED ] = "vertical_right",
  37. [HOR_DOWN_PRED ] = "horizontal_right",
  38. [VERT_LEFT_PRED ] = "vertical_left",
  39. [HOR_UP_PRED ] = "horizontal_up",
  40. [LEFT_DC_PRED ] = "left_dc",
  41. [TOP_DC_PRED ] = "top_dc",
  42. [DC_128_PRED ] = "dc_128",
  43. },
  44. { /* VP8 */
  45. [VERT_PRED ] = "vertical_vp8",
  46. [HOR_PRED ] = "horizontal_vp8",
  47. [VERT_LEFT_PRED] = "vertical_left_vp8",
  48. [TM_VP8_PRED ] = "tm_vp8",
  49. [DC_127_PRED ] = "dc_127_vp8",
  50. [DC_129_PRED ] = "dc_129_vp8",
  51. },
  52. { /* RV40 */
  53. [DIAG_DOWN_LEFT_PRED ] = "down_left_rv40",
  54. [VERT_LEFT_PRED ] = "vertical_left_rv40",
  55. [HOR_UP_PRED ] = "horizontal_up_rv40",
  56. [DIAG_DOWN_LEFT_PRED_RV40_NODOWN] = "down_left_nodown_rv40",
  57. [HOR_UP_PRED_RV40_NODOWN ] = "horizontal_up_nodown_rv40",
  58. [VERT_LEFT_PRED_RV40_NODOWN ] = "vertical_left_nodown_rv40",
  59. },
  60. { /* SVQ3 */
  61. [DIAG_DOWN_LEFT_PRED] = "down_left_svq3",
  62. },
  63. };
  64. static const char * const pred8x8_modes[4][11] = {
  65. { /* H.264 */
  66. [DC_PRED8x8 ] = "dc",
  67. [HOR_PRED8x8 ] = "horizontal",
  68. [VERT_PRED8x8 ] = "vertical",
  69. [PLANE_PRED8x8 ] = "plane",
  70. [LEFT_DC_PRED8x8 ] = "left_dc",
  71. [TOP_DC_PRED8x8 ] = "top_dc",
  72. [DC_128_PRED8x8 ] = "dc_128",
  73. [ALZHEIMER_DC_L0T_PRED8x8] = "mad_cow_dc_l0t",
  74. [ALZHEIMER_DC_0LT_PRED8x8] = "mad_cow_dc_0lt",
  75. [ALZHEIMER_DC_L00_PRED8x8] = "mad_cow_dc_l00",
  76. [ALZHEIMER_DC_0L0_PRED8x8] = "mad_cow_dc_0l0",
  77. },
  78. { /* VP8 */
  79. [PLANE_PRED8x8 ] = "tm_vp8",
  80. [DC_127_PRED8x8] = "dc_127_vp8",
  81. [DC_129_PRED8x8] = "dc_129_vp8",
  82. },
  83. { /* RV40 */
  84. [DC_PRED8x8 ] = "dc_rv40",
  85. [LEFT_DC_PRED8x8] = "left_dc_rv40",
  86. [TOP_DC_PRED8x8 ] = "top_dc_rv40",
  87. },
  88. /* nothing for SVQ3 */
  89. };
  90. static const char * const pred16x16_modes[4][9] = {
  91. { /* H.264 */
  92. [DC_PRED8x8 ] = "dc",
  93. [HOR_PRED8x8 ] = "horizontal",
  94. [VERT_PRED8x8 ] = "vertical",
  95. [PLANE_PRED8x8 ] = "plane",
  96. [LEFT_DC_PRED8x8] = "left_dc",
  97. [TOP_DC_PRED8x8 ] = "top_dc",
  98. [DC_128_PRED8x8 ] = "dc_128",
  99. },
  100. { /* VP8 */
  101. [PLANE_PRED8x8 ] = "tm_vp8",
  102. [DC_127_PRED8x8] = "dc_127_vp8",
  103. [DC_129_PRED8x8] = "dc_129_vp8",
  104. },
  105. { /* RV40 */
  106. [PLANE_PRED8x8] = "plane_rv40",
  107. },
  108. { /* SVQ3 */
  109. [PLANE_PRED8x8] = "plane_svq3",
  110. },
  111. };
  112. static const uint32_t pixel_mask[3] = { 0xffffffff, 0x01ff01ff, 0x03ff03ff };
  113. #define SIZEOF_PIXEL ((bit_depth + 7) / 8)
  114. #define BUF_SIZE (3 * 16 * 17)
  115. #define check_pred_func(func, name, mode_name) \
  116. (mode_name && ((codec_ids[codec] == AV_CODEC_ID_H264) ? \
  117. check_func(func, "pred%s_%s_%d", name, mode_name, bit_depth) : \
  118. check_func(func, "pred%s_%s", name, mode_name)))
  119. #define randomize_buffers() \
  120. do { \
  121. uint32_t mask = pixel_mask[bit_depth - 8]; \
  122. int i; \
  123. for (i = 0; i < BUF_SIZE; i += 4) { \
  124. uint32_t r = rnd() & mask; \
  125. AV_WN32A(buf0 + i, r); \
  126. AV_WN32A(buf1 + i, r); \
  127. } \
  128. } while (0)
  129. #define src0 (buf0 + 4 * 16) /* Offset to allow room for top and left */
  130. #define src1 (buf1 + 4 * 16)
  131. static void check_pred4x4(H264PredContext *h, uint8_t *buf0, uint8_t *buf1,
  132. int codec, int chroma_format, int bit_depth)
  133. {
  134. if (chroma_format == 1) {
  135. uint8_t *topright = buf0 + 2*16;
  136. int pred_mode;
  137. declare_func_emms(AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT, void, uint8_t *src, const uint8_t *topright, ptrdiff_t stride);
  138. for (pred_mode = 0; pred_mode < 15; pred_mode++) {
  139. if (check_pred_func(h->pred4x4[pred_mode], "4x4", pred4x4_modes[codec][pred_mode])) {
  140. randomize_buffers();
  141. call_ref(src0, topright, 12*SIZEOF_PIXEL);
  142. call_new(src1, topright, 12*SIZEOF_PIXEL);
  143. if (memcmp(buf0, buf1, BUF_SIZE))
  144. fail();
  145. bench_new(src1, topright, 12*SIZEOF_PIXEL);
  146. }
  147. }
  148. }
  149. }
  150. static void check_pred8x8(H264PredContext *h, uint8_t *buf0, uint8_t *buf1,
  151. int codec, int chroma_format, int bit_depth)
  152. {
  153. int pred_mode;
  154. declare_func_emms(AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT, void, uint8_t *src, ptrdiff_t stride);
  155. for (pred_mode = 0; pred_mode < 11; pred_mode++) {
  156. if (check_pred_func(h->pred8x8[pred_mode], (chroma_format == 2) ? "8x16" : "8x8",
  157. pred8x8_modes[codec][pred_mode])) {
  158. randomize_buffers();
  159. call_ref(src0, 24*SIZEOF_PIXEL);
  160. call_new(src1, 24*SIZEOF_PIXEL);
  161. if (memcmp(buf0, buf1, BUF_SIZE))
  162. fail();
  163. bench_new(src1, 24*SIZEOF_PIXEL);
  164. }
  165. }
  166. }
  167. static void check_pred16x16(H264PredContext *h, uint8_t *buf0, uint8_t *buf1,
  168. int codec, int chroma_format, int bit_depth)
  169. {
  170. if (chroma_format == 1) {
  171. int pred_mode;
  172. declare_func_emms(AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT, void, uint8_t *src, ptrdiff_t stride);
  173. for (pred_mode = 0; pred_mode < 9; pred_mode++) {
  174. if (check_pred_func(h->pred16x16[pred_mode], "16x16", pred16x16_modes[codec][pred_mode])) {
  175. randomize_buffers();
  176. call_ref(src0, 48);
  177. call_new(src1, 48);
  178. if (memcmp(buf0, buf1, BUF_SIZE))
  179. fail();
  180. bench_new(src1, 48);
  181. }
  182. }
  183. }
  184. }
  185. static void check_pred8x8l(H264PredContext *h, uint8_t *buf0, uint8_t *buf1,
  186. int codec, int chroma_format, int bit_depth)
  187. {
  188. if (chroma_format == 1 && codec_ids[codec] == AV_CODEC_ID_H264) {
  189. int pred_mode;
  190. declare_func_emms(AV_CPU_FLAG_MMXEXT, void, uint8_t *src, int topleft, int topright, ptrdiff_t stride);
  191. for (pred_mode = 0; pred_mode < 12; pred_mode++) {
  192. if (check_pred_func(h->pred8x8l[pred_mode], "8x8l", pred4x4_modes[codec][pred_mode])) {
  193. int neighbors;
  194. for (neighbors = 0; neighbors <= 0xc000; neighbors += 0x4000) {
  195. int has_topleft = neighbors & 0x8000;
  196. int has_topright = neighbors & 0x4000;
  197. if ((pred_mode == DIAG_DOWN_RIGHT_PRED || pred_mode == VERT_RIGHT_PRED) && !has_topleft)
  198. continue; /* Those aren't allowed according to the spec */
  199. randomize_buffers();
  200. call_ref(src0, has_topleft, has_topright, 24*SIZEOF_PIXEL);
  201. call_new(src1, has_topleft, has_topright, 24*SIZEOF_PIXEL);
  202. if (memcmp(buf0, buf1, BUF_SIZE))
  203. fail();
  204. bench_new(src1, has_topleft, has_topright, 24*SIZEOF_PIXEL);
  205. }
  206. }
  207. }
  208. }
  209. }
  210. /* TODO: Add tests for H.264 lossless H/V prediction */
  211. void checkasm_check_h264pred(void)
  212. {
  213. static const struct {
  214. void (*func)(H264PredContext*, uint8_t*, uint8_t*, int, int, int);
  215. const char *name;
  216. } tests[] = {
  217. { check_pred4x4, "pred4x4" },
  218. { check_pred8x8, "pred8x8" },
  219. { check_pred16x16, "pred16x16" },
  220. { check_pred8x8l, "pred8x8l" },
  221. };
  222. LOCAL_ALIGNED_16(uint8_t, buf0, [BUF_SIZE]);
  223. LOCAL_ALIGNED_16(uint8_t, buf1, [BUF_SIZE]);
  224. H264PredContext h;
  225. int test, codec, chroma_format, bit_depth;
  226. for (test = 0; test < FF_ARRAY_ELEMS(tests); test++) {
  227. for (codec = 0; codec < 4; codec++) {
  228. int codec_id = codec_ids[codec];
  229. for (bit_depth = 8; bit_depth <= (codec_id == AV_CODEC_ID_H264 ? 10 : 8); bit_depth++)
  230. for (chroma_format = 1; chroma_format <= (codec_id == AV_CODEC_ID_H264 ? 2 : 1); chroma_format++) {
  231. ff_h264_pred_init(&h, codec_id, bit_depth, chroma_format);
  232. tests[test].func(&h, buf0, buf1, codec, chroma_format, bit_depth);
  233. }
  234. }
  235. report("%s", tests[test].name);
  236. }
  237. }