h264pred.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/avcodec.h"
  23. #include "libavcodec/h264pred.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/intreadwrite.h"
  26. static const int codec_ids[4] = { AV_CODEC_ID_H264, AV_CODEC_ID_VP8, AV_CODEC_ID_RV40, AV_CODEC_ID_SVQ3 };
  27. static const char * const pred4x4_modes[4][15] = {
  28. { /* H264 */
  29. [VERT_PRED ] = "vertical",
  30. [HOR_PRED ] = "horizontal",
  31. [DC_PRED ] = "dc",
  32. [DIAG_DOWN_LEFT_PRED ] = "down_left",
  33. [DIAG_DOWN_RIGHT_PRED] = "down_right",
  34. [VERT_RIGHT_PRED ] = "vertical_right",
  35. [HOR_DOWN_PRED ] = "horizontal_right",
  36. [VERT_LEFT_PRED ] = "vertical_left",
  37. [HOR_UP_PRED ] = "horizontal_up",
  38. [LEFT_DC_PRED ] = "left_dc",
  39. [TOP_DC_PRED ] = "top_dc",
  40. [DC_128_PRED ] = "dc_128",
  41. },
  42. { /* VP8 */
  43. [VERT_PRED ] = "vertical_vp8",
  44. [HOR_PRED ] = "horizontal_vp8",
  45. [VERT_LEFT_PRED] = "vertical_left_vp8",
  46. [TM_VP8_PRED ] = "tm_vp8",
  47. [DC_127_PRED ] = "dc_127_vp8",
  48. [DC_129_PRED ] = "dc_129_vp8",
  49. },
  50. { /* RV40 */
  51. [DIAG_DOWN_LEFT_PRED ] = "down_left_rv40",
  52. [VERT_LEFT_PRED ] = "vertical_left_rv40",
  53. [HOR_UP_PRED ] = "horizontal_up_rv40",
  54. [DIAG_DOWN_LEFT_PRED_RV40_NODOWN] = "down_left_nodown_rv40",
  55. [HOR_UP_PRED_RV40_NODOWN ] = "horizontal_up_nodown_rv40",
  56. [VERT_LEFT_PRED_RV40_NODOWN ] = "vertical_left_nodown_rv40",
  57. },
  58. { /* SVQ3 */
  59. [DIAG_DOWN_LEFT_PRED] = "down_left_svq3",
  60. },
  61. };
  62. static const char * const pred8x8_modes[4][11] = {
  63. { /* H264 */
  64. [DC_PRED8x8 ] = "dc",
  65. [HOR_PRED8x8 ] = "horizontal",
  66. [VERT_PRED8x8 ] = "vertical",
  67. [PLANE_PRED8x8 ] = "plane",
  68. [LEFT_DC_PRED8x8 ] = "left_dc",
  69. [TOP_DC_PRED8x8 ] = "top_dc",
  70. [DC_128_PRED8x8 ] = "dc_128",
  71. [ALZHEIMER_DC_L0T_PRED8x8] = "mad_cow_dc_l0t",
  72. [ALZHEIMER_DC_0LT_PRED8x8] = "mad_cow_dc_0lt",
  73. [ALZHEIMER_DC_L00_PRED8x8] = "mad_cow_dc_l00",
  74. [ALZHEIMER_DC_0L0_PRED8x8] = "mad_cow_dc_0l0",
  75. },
  76. { /* VP8 */
  77. [PLANE_PRED8x8 ] = "tm_vp8",
  78. [DC_127_PRED8x8] = "dc_127_vp8",
  79. [DC_129_PRED8x8] = "dc_129_vp8",
  80. },
  81. { /* RV40 */
  82. [DC_PRED8x8 ] = "dc_rv40",
  83. [LEFT_DC_PRED8x8] = "left_dc_rv40",
  84. [TOP_DC_PRED8x8 ] = "top_dc_rv40",
  85. },
  86. { /* SVQ3 */
  87. },
  88. };
  89. static const char * const pred16x16_modes[4][9] = {
  90. { /* H264 */
  91. [DC_PRED8x8 ] = "dc",
  92. [HOR_PRED8x8 ] = "horizontal",
  93. [VERT_PRED8x8 ] = "vertical",
  94. [PLANE_PRED8x8 ] = "plane",
  95. [LEFT_DC_PRED8x8] = "left_dc",
  96. [TOP_DC_PRED8x8 ] = "top_dc",
  97. [DC_128_PRED8x8 ] = "dc_128",
  98. },
  99. { /* VP8 */
  100. [PLANE_PRED8x8 ] = "tm_vp8",
  101. [DC_127_PRED8x8] = "dc_127_vp8",
  102. [DC_129_PRED8x8] = "dc_129_vp8",
  103. },
  104. { /* RV40 */
  105. [PLANE_PRED8x8] = "plane_rv40",
  106. },
  107. { /* SVQ3 */
  108. [PLANE_PRED8x8] = "plane_svq3",
  109. },
  110. };
  111. static const uint32_t pixel_mask[3] = { 0xffffffff, 0x01ff01ff, 0x03ff03ff };
  112. #define SIZEOF_PIXEL ((bit_depth + 7) / 8)
  113. #define BUF_SIZE (3*16*17)
  114. #define check_pred_func(func, name, mode_name)\
  115. (mode_name && ((codec_ids[codec] == AV_CODEC_ID_H264) ?\
  116. check_func(func, "pred%s_%s_%d", name, mode_name, bit_depth) :\
  117. check_func(func, "pred%s_%s", name, mode_name)))
  118. #define randomize_buffers()\
  119. do {\
  120. uint32_t mask = pixel_mask[bit_depth-8];\
  121. int i;\
  122. for (i = 0; i < BUF_SIZE; i += 4) {\
  123. uint32_t r = rnd() & mask;\
  124. AV_WN32A(buf0+i, r);\
  125. AV_WN32A(buf1+i, r);\
  126. }\
  127. } while (0)
  128. #define src0 (buf0 + 4*16) /* Offset to allow room for top and left */
  129. #define src1 (buf1 + 4*16)
  130. static void check_pred4x4(H264PredContext *h, uint8_t *buf0, uint8_t *buf1,
  131. int codec, int chroma_format, int bit_depth)
  132. {
  133. if (chroma_format == 1) {
  134. uint8_t *topright = buf0 + 2*16;
  135. int pred_mode;
  136. for (pred_mode = 0; pred_mode < 15; pred_mode++) {
  137. if (check_pred_func(h->pred4x4[pred_mode], "4x4", pred4x4_modes[codec][pred_mode])) {
  138. randomize_buffers();
  139. call_ref(src0, topright, (ptrdiff_t)12*SIZEOF_PIXEL);
  140. call_new(src1, topright, (ptrdiff_t)12*SIZEOF_PIXEL);
  141. if (memcmp(buf0, buf1, BUF_SIZE))
  142. fail();
  143. bench_new(src1, topright, (ptrdiff_t)12*SIZEOF_PIXEL);
  144. }
  145. }
  146. }
  147. }
  148. static void check_pred8x8(H264PredContext *h, uint8_t *buf0, uint8_t *buf1,
  149. int codec, int chroma_format, int bit_depth)
  150. {
  151. int pred_mode;
  152. for (pred_mode = 0; pred_mode < 11; pred_mode++) {
  153. if (check_pred_func(h->pred8x8[pred_mode], (chroma_format == 2) ? "8x16" : "8x8",
  154. pred8x8_modes[codec][pred_mode])) {
  155. randomize_buffers();
  156. call_ref(src0, (ptrdiff_t)24*SIZEOF_PIXEL);
  157. call_new(src1, (ptrdiff_t)24*SIZEOF_PIXEL);
  158. if (memcmp(buf0, buf1, BUF_SIZE))
  159. fail();
  160. bench_new(src1, (ptrdiff_t)24*SIZEOF_PIXEL);
  161. }
  162. }
  163. }
  164. static void check_pred16x16(H264PredContext *h, uint8_t *buf0, uint8_t *buf1,
  165. int codec, int chroma_format, int bit_depth)
  166. {
  167. if (chroma_format == 1) {
  168. int pred_mode;
  169. for (pred_mode = 0; pred_mode < 9; pred_mode++) {
  170. if (check_pred_func(h->pred16x16[pred_mode], "16x16", pred16x16_modes[codec][pred_mode])) {
  171. randomize_buffers();
  172. call_ref(src0, (ptrdiff_t)48);
  173. call_new(src1, (ptrdiff_t)48);
  174. if (memcmp(buf0, buf1, BUF_SIZE))
  175. fail();
  176. bench_new(src1, (ptrdiff_t)48);
  177. }
  178. }
  179. }
  180. }
  181. static void check_pred8x8l(H264PredContext *h, uint8_t *buf0, uint8_t *buf1,
  182. int codec, int chroma_format, int bit_depth)
  183. {
  184. if (chroma_format == 1 && codec_ids[codec] == AV_CODEC_ID_H264) {
  185. int pred_mode;
  186. for (pred_mode = 0; pred_mode < 12; pred_mode++) {
  187. if (check_pred_func(h->pred8x8l[pred_mode], "8x8l", pred4x4_modes[codec][pred_mode])) {
  188. int neighbors;
  189. for (neighbors = 0; neighbors <= 0xc000; neighbors += 0x4000) {
  190. int has_topleft = neighbors & 0x8000;
  191. int has_topright = neighbors & 0x4000;
  192. if ((pred_mode == DIAG_DOWN_RIGHT_PRED || pred_mode == VERT_RIGHT_PRED) && !has_topleft)
  193. continue; /* Those aren't allowed according to the spec */
  194. randomize_buffers();
  195. call_ref(src0, has_topleft, has_topright, (ptrdiff_t)24*SIZEOF_PIXEL);
  196. call_new(src1, has_topleft, has_topright, (ptrdiff_t)24*SIZEOF_PIXEL);
  197. if (memcmp(buf0, buf1, BUF_SIZE))
  198. fail();
  199. bench_new(src1, has_topleft, has_topright, (ptrdiff_t)24*SIZEOF_PIXEL);
  200. }
  201. }
  202. }
  203. }
  204. }
  205. /* TODO: Add tests for H.264 lossless H/V prediction */
  206. void checkasm_check_h264pred(void)
  207. {
  208. static const struct {
  209. void (*func)(H264PredContext*, uint8_t*, uint8_t*, int, int, int);
  210. const char *name;
  211. } tests[] = {
  212. { check_pred4x4, "pred4x4" },
  213. { check_pred8x8, "pred8x8" },
  214. { check_pred16x16, "pred16x16" },
  215. { check_pred8x8l, "pred8x8l" },
  216. };
  217. DECLARE_ALIGNED(16, uint8_t, buf0)[BUF_SIZE];
  218. DECLARE_ALIGNED(16, uint8_t, buf1)[BUF_SIZE];
  219. H264PredContext h;
  220. int test, codec, chroma_format, bit_depth;
  221. for (test = 0; test < FF_ARRAY_ELEMS(tests); test++) {
  222. for (codec = 0; codec < 4; codec++) {
  223. int codec_id = codec_ids[codec];
  224. for (bit_depth = 8; bit_depth <= (codec_id == AV_CODEC_ID_H264 ? 10 : 8); bit_depth++)
  225. for (chroma_format = 1; chroma_format <= (codec_id == AV_CODEC_ID_H264 ? 2 : 1); chroma_format++) {
  226. ff_h264_pred_init(&h, codec_id, bit_depth, chroma_format);
  227. tests[test].func(&h, buf0, buf1, codec, chroma_format, bit_depth);
  228. }
  229. }
  230. report("%s", tests[test].name);
  231. }
  232. }