You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

823 lines
37KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * H.264 / AVC / MPEG4 part10 macroblock decoding
  24. */
  25. #include <stdint.h>
  26. #include "config.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "avcodec.h"
  30. #include "h264.h"
  31. #include "qpeldsp.h"
  32. #include "svq3.h"
  33. #include "thread.h"
  34. static inline int get_lowest_part_list_y(H264Context *h, H264Picture *pic, int n,
  35. int height, int y_offset, int list)
  36. {
  37. int raw_my = h->mv_cache[list][scan8[n]][1];
  38. int filter_height_up = (raw_my & 3) ? 2 : 0;
  39. int filter_height_down = (raw_my & 3) ? 3 : 0;
  40. int full_my = (raw_my >> 2) + y_offset;
  41. int top = full_my - filter_height_up;
  42. int bottom = full_my + filter_height_down + height;
  43. return FFMAX(abs(top), bottom);
  44. }
  45. static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n,
  46. int height, int y_offset, int list0,
  47. int list1, int *nrefs)
  48. {
  49. int my;
  50. y_offset += 16 * (h->mb_y >> MB_FIELD(h));
  51. if (list0) {
  52. int ref_n = h->ref_cache[0][scan8[n]];
  53. H264Picture *ref = &h->ref_list[0][ref_n];
  54. // Error resilience puts the current picture in the ref list.
  55. // Don't try to wait on these as it will cause a deadlock.
  56. // Fields can wait on each other, though.
  57. if (ref->tf.progress->data != h->cur_pic.tf.progress->data ||
  58. (ref->reference & 3) != h->picture_structure) {
  59. my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
  60. if (refs[0][ref_n] < 0)
  61. nrefs[0] += 1;
  62. refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
  63. }
  64. }
  65. if (list1) {
  66. int ref_n = h->ref_cache[1][scan8[n]];
  67. H264Picture *ref = &h->ref_list[1][ref_n];
  68. if (ref->tf.progress->data != h->cur_pic.tf.progress->data ||
  69. (ref->reference & 3) != h->picture_structure) {
  70. my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
  71. if (refs[1][ref_n] < 0)
  72. nrefs[1] += 1;
  73. refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
  74. }
  75. }
  76. }
  77. /**
  78. * Wait until all reference frames are available for MC operations.
  79. *
  80. * @param h the H264 context
  81. */
  82. static void await_references(H264Context *h)
  83. {
  84. const int mb_xy = h->mb_xy;
  85. const int mb_type = h->cur_pic.mb_type[mb_xy];
  86. int refs[2][48];
  87. int nrefs[2] = { 0 };
  88. int ref, list;
  89. memset(refs, -1, sizeof(refs));
  90. if (IS_16X16(mb_type)) {
  91. get_lowest_part_y(h, refs, 0, 16, 0,
  92. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  93. } else if (IS_16X8(mb_type)) {
  94. get_lowest_part_y(h, refs, 0, 8, 0,
  95. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  96. get_lowest_part_y(h, refs, 8, 8, 8,
  97. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
  98. } else if (IS_8X16(mb_type)) {
  99. get_lowest_part_y(h, refs, 0, 16, 0,
  100. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  101. get_lowest_part_y(h, refs, 4, 16, 0,
  102. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
  103. } else {
  104. int i;
  105. assert(IS_8X8(mb_type));
  106. for (i = 0; i < 4; i++) {
  107. const int sub_mb_type = h->sub_mb_type[i];
  108. const int n = 4 * i;
  109. int y_offset = (i & 2) << 2;
  110. if (IS_SUB_8X8(sub_mb_type)) {
  111. get_lowest_part_y(h, refs, n, 8, y_offset,
  112. IS_DIR(sub_mb_type, 0, 0),
  113. IS_DIR(sub_mb_type, 0, 1),
  114. nrefs);
  115. } else if (IS_SUB_8X4(sub_mb_type)) {
  116. get_lowest_part_y(h, refs, n, 4, y_offset,
  117. IS_DIR(sub_mb_type, 0, 0),
  118. IS_DIR(sub_mb_type, 0, 1),
  119. nrefs);
  120. get_lowest_part_y(h, refs, n + 2, 4, y_offset + 4,
  121. IS_DIR(sub_mb_type, 0, 0),
  122. IS_DIR(sub_mb_type, 0, 1),
  123. nrefs);
  124. } else if (IS_SUB_4X8(sub_mb_type)) {
  125. get_lowest_part_y(h, refs, n, 8, y_offset,
  126. IS_DIR(sub_mb_type, 0, 0),
  127. IS_DIR(sub_mb_type, 0, 1),
  128. nrefs);
  129. get_lowest_part_y(h, refs, n + 1, 8, y_offset,
  130. IS_DIR(sub_mb_type, 0, 0),
  131. IS_DIR(sub_mb_type, 0, 1),
  132. nrefs);
  133. } else {
  134. int j;
  135. assert(IS_SUB_4X4(sub_mb_type));
  136. for (j = 0; j < 4; j++) {
  137. int sub_y_offset = y_offset + 2 * (j & 2);
  138. get_lowest_part_y(h, refs, n + j, 4, sub_y_offset,
  139. IS_DIR(sub_mb_type, 0, 0),
  140. IS_DIR(sub_mb_type, 0, 1),
  141. nrefs);
  142. }
  143. }
  144. }
  145. }
  146. for (list = h->list_count - 1; list >= 0; list--)
  147. for (ref = 0; ref < 48 && nrefs[list]; ref++) {
  148. int row = refs[list][ref];
  149. if (row >= 0) {
  150. H264Picture *ref_pic = &h->ref_list[list][ref];
  151. int ref_field = ref_pic->reference - 1;
  152. int ref_field_picture = ref_pic->field_picture;
  153. int pic_height = 16 * h->mb_height >> ref_field_picture;
  154. row <<= MB_MBAFF(h);
  155. nrefs[list]--;
  156. if (!FIELD_PICTURE(h) && ref_field_picture) { // frame referencing two fields
  157. ff_thread_await_progress(&ref_pic->tf,
  158. FFMIN((row >> 1) - !(row & 1),
  159. pic_height - 1),
  160. 1);
  161. ff_thread_await_progress(&ref_pic->tf,
  162. FFMIN((row >> 1), pic_height - 1),
  163. 0);
  164. } else if (FIELD_PICTURE(h) && !ref_field_picture) { // field referencing one field of a frame
  165. ff_thread_await_progress(&ref_pic->tf,
  166. FFMIN(row * 2 + ref_field,
  167. pic_height - 1),
  168. 0);
  169. } else if (FIELD_PICTURE(h)) {
  170. ff_thread_await_progress(&ref_pic->tf,
  171. FFMIN(row, pic_height - 1),
  172. ref_field);
  173. } else {
  174. ff_thread_await_progress(&ref_pic->tf,
  175. FFMIN(row, pic_height - 1),
  176. 0);
  177. }
  178. }
  179. }
  180. }
  181. static av_always_inline void mc_dir_part(H264Context *h, H264Picture *pic,
  182. int n, int square, int height,
  183. int delta, int list,
  184. uint8_t *dest_y, uint8_t *dest_cb,
  185. uint8_t *dest_cr,
  186. int src_x_offset, int src_y_offset,
  187. qpel_mc_func *qpix_op,
  188. h264_chroma_mc_func chroma_op,
  189. int pixel_shift, int chroma_idc)
  190. {
  191. const int mx = h->mv_cache[list][scan8[n]][0] + src_x_offset * 8;
  192. int my = h->mv_cache[list][scan8[n]][1] + src_y_offset * 8;
  193. const int luma_xy = (mx & 3) + ((my & 3) << 2);
  194. ptrdiff_t offset = ((mx >> 2) << pixel_shift) + (my >> 2) * h->mb_linesize;
  195. uint8_t *src_y = pic->f.data[0] + offset;
  196. uint8_t *src_cb, *src_cr;
  197. int extra_width = 0;
  198. int extra_height = 0;
  199. int emu = 0;
  200. const int full_mx = mx >> 2;
  201. const int full_my = my >> 2;
  202. const int pic_width = 16 * h->mb_width;
  203. const int pic_height = 16 * h->mb_height >> MB_FIELD(h);
  204. int ysh;
  205. if (mx & 7)
  206. extra_width -= 3;
  207. if (my & 7)
  208. extra_height -= 3;
  209. if (full_mx < 0 - extra_width ||
  210. full_my < 0 - extra_height ||
  211. full_mx + 16 /*FIXME*/ > pic_width + extra_width ||
  212. full_my + 16 /*FIXME*/ > pic_height + extra_height) {
  213. h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
  214. src_y - (2 << pixel_shift) - 2 * h->mb_linesize,
  215. h->mb_linesize, h->mb_linesize,
  216. 16 + 5, 16 + 5 /*FIXME*/, full_mx - 2,
  217. full_my - 2, pic_width, pic_height);
  218. src_y = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
  219. emu = 1;
  220. }
  221. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); // FIXME try variable height perhaps?
  222. if (!square)
  223. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  224. if (CONFIG_GRAY && h->flags & CODEC_FLAG_GRAY)
  225. return;
  226. if (chroma_idc == 3 /* yuv444 */) {
  227. src_cb = pic->f.data[1] + offset;
  228. if (emu) {
  229. h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
  230. src_cb - (2 << pixel_shift) - 2 * h->mb_linesize,
  231. h->mb_linesize, h->mb_linesize,
  232. 16 + 5, 16 + 5 /*FIXME*/,
  233. full_mx - 2, full_my - 2,
  234. pic_width, pic_height);
  235. src_cb = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
  236. }
  237. qpix_op[luma_xy](dest_cb, src_cb, h->mb_linesize); // FIXME try variable height perhaps?
  238. if (!square)
  239. qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize);
  240. src_cr = pic->f.data[2] + offset;
  241. if (emu) {
  242. h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
  243. src_cr - (2 << pixel_shift) - 2 * h->mb_linesize,
  244. h->mb_linesize, h->mb_linesize,
  245. 16 + 5, 16 + 5 /*FIXME*/,
  246. full_mx - 2, full_my - 2,
  247. pic_width, pic_height);
  248. src_cr = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
  249. }
  250. qpix_op[luma_xy](dest_cr, src_cr, h->mb_linesize); // FIXME try variable height perhaps?
  251. if (!square)
  252. qpix_op[luma_xy](dest_cr + delta, src_cr + delta, h->mb_linesize);
  253. return;
  254. }
  255. ysh = 3 - (chroma_idc == 2 /* yuv422 */);
  256. if (chroma_idc == 1 /* yuv420 */ && MB_FIELD(h)) {
  257. // chroma offset when predicting from a field of opposite parity
  258. my += 2 * ((h->mb_y & 1) - (pic->reference - 1));
  259. emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
  260. }
  261. src_cb = pic->f.data[1] + ((mx >> 3) << pixel_shift) +
  262. (my >> ysh) * h->mb_uvlinesize;
  263. src_cr = pic->f.data[2] + ((mx >> 3) << pixel_shift) +
  264. (my >> ysh) * h->mb_uvlinesize;
  265. if (emu) {
  266. h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cb,
  267. h->mb_uvlinesize, h->mb_uvlinesize,
  268. 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
  269. pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
  270. src_cb = h->edge_emu_buffer;
  271. }
  272. chroma_op(dest_cb, src_cb, h->mb_uvlinesize,
  273. height >> (chroma_idc == 1 /* yuv420 */),
  274. mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
  275. if (emu) {
  276. h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cr,
  277. h->mb_uvlinesize, h->mb_uvlinesize,
  278. 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
  279. pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
  280. src_cr = h->edge_emu_buffer;
  281. }
  282. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
  283. mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
  284. }
  285. static av_always_inline void mc_part_std(H264Context *h, int n, int square,
  286. int height, int delta,
  287. uint8_t *dest_y, uint8_t *dest_cb,
  288. uint8_t *dest_cr,
  289. int x_offset, int y_offset,
  290. qpel_mc_func *qpix_put,
  291. h264_chroma_mc_func chroma_put,
  292. qpel_mc_func *qpix_avg,
  293. h264_chroma_mc_func chroma_avg,
  294. int list0, int list1,
  295. int pixel_shift, int chroma_idc)
  296. {
  297. qpel_mc_func *qpix_op = qpix_put;
  298. h264_chroma_mc_func chroma_op = chroma_put;
  299. dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  300. if (chroma_idc == 3 /* yuv444 */) {
  301. dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  302. dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  303. } else if (chroma_idc == 2 /* yuv422 */) {
  304. dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  305. dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  306. } else { /* yuv420 */
  307. dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  308. dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  309. }
  310. x_offset += 8 * h->mb_x;
  311. y_offset += 8 * (h->mb_y >> MB_FIELD(h));
  312. if (list0) {
  313. H264Picture *ref = &h->ref_list[0][h->ref_cache[0][scan8[n]]];
  314. mc_dir_part(h, ref, n, square, height, delta, 0,
  315. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  316. qpix_op, chroma_op, pixel_shift, chroma_idc);
  317. qpix_op = qpix_avg;
  318. chroma_op = chroma_avg;
  319. }
  320. if (list1) {
  321. H264Picture *ref = &h->ref_list[1][h->ref_cache[1][scan8[n]]];
  322. mc_dir_part(h, ref, n, square, height, delta, 1,
  323. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  324. qpix_op, chroma_op, pixel_shift, chroma_idc);
  325. }
  326. }
  327. static av_always_inline void mc_part_weighted(H264Context *h, int n, int square,
  328. int height, int delta,
  329. uint8_t *dest_y, uint8_t *dest_cb,
  330. uint8_t *dest_cr,
  331. int x_offset, int y_offset,
  332. qpel_mc_func *qpix_put,
  333. h264_chroma_mc_func chroma_put,
  334. h264_weight_func luma_weight_op,
  335. h264_weight_func chroma_weight_op,
  336. h264_biweight_func luma_weight_avg,
  337. h264_biweight_func chroma_weight_avg,
  338. int list0, int list1,
  339. int pixel_shift, int chroma_idc)
  340. {
  341. int chroma_height;
  342. dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  343. if (chroma_idc == 3 /* yuv444 */) {
  344. chroma_height = height;
  345. chroma_weight_avg = luma_weight_avg;
  346. chroma_weight_op = luma_weight_op;
  347. dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  348. dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  349. } else if (chroma_idc == 2 /* yuv422 */) {
  350. chroma_height = height;
  351. dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  352. dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  353. } else { /* yuv420 */
  354. chroma_height = height >> 1;
  355. dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  356. dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  357. }
  358. x_offset += 8 * h->mb_x;
  359. y_offset += 8 * (h->mb_y >> MB_FIELD(h));
  360. if (list0 && list1) {
  361. /* don't optimize for luma-only case, since B-frames usually
  362. * use implicit weights => chroma too. */
  363. uint8_t *tmp_cb = h->bipred_scratchpad;
  364. uint8_t *tmp_cr = h->bipred_scratchpad + (16 << pixel_shift);
  365. uint8_t *tmp_y = h->bipred_scratchpad + 16 * h->mb_uvlinesize;
  366. int refn0 = h->ref_cache[0][scan8[n]];
  367. int refn1 = h->ref_cache[1][scan8[n]];
  368. mc_dir_part(h, &h->ref_list[0][refn0], n, square, height, delta, 0,
  369. dest_y, dest_cb, dest_cr,
  370. x_offset, y_offset, qpix_put, chroma_put,
  371. pixel_shift, chroma_idc);
  372. mc_dir_part(h, &h->ref_list[1][refn1], n, square, height, delta, 1,
  373. tmp_y, tmp_cb, tmp_cr,
  374. x_offset, y_offset, qpix_put, chroma_put,
  375. pixel_shift, chroma_idc);
  376. if (h->use_weight == 2) {
  377. int weight0 = h->implicit_weight[refn0][refn1][h->mb_y & 1];
  378. int weight1 = 64 - weight0;
  379. luma_weight_avg(dest_y, tmp_y, h->mb_linesize,
  380. height, 5, weight0, weight1, 0);
  381. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize,
  382. chroma_height, 5, weight0, weight1, 0);
  383. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize,
  384. chroma_height, 5, weight0, weight1, 0);
  385. } else {
  386. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, height,
  387. h->luma_log2_weight_denom,
  388. h->luma_weight[refn0][0][0],
  389. h->luma_weight[refn1][1][0],
  390. h->luma_weight[refn0][0][1] +
  391. h->luma_weight[refn1][1][1]);
  392. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, chroma_height,
  393. h->chroma_log2_weight_denom,
  394. h->chroma_weight[refn0][0][0][0],
  395. h->chroma_weight[refn1][1][0][0],
  396. h->chroma_weight[refn0][0][0][1] +
  397. h->chroma_weight[refn1][1][0][1]);
  398. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, chroma_height,
  399. h->chroma_log2_weight_denom,
  400. h->chroma_weight[refn0][0][1][0],
  401. h->chroma_weight[refn1][1][1][0],
  402. h->chroma_weight[refn0][0][1][1] +
  403. h->chroma_weight[refn1][1][1][1]);
  404. }
  405. } else {
  406. int list = list1 ? 1 : 0;
  407. int refn = h->ref_cache[list][scan8[n]];
  408. H264Picture *ref = &h->ref_list[list][refn];
  409. mc_dir_part(h, ref, n, square, height, delta, list,
  410. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  411. qpix_put, chroma_put, pixel_shift, chroma_idc);
  412. luma_weight_op(dest_y, h->mb_linesize, height,
  413. h->luma_log2_weight_denom,
  414. h->luma_weight[refn][list][0],
  415. h->luma_weight[refn][list][1]);
  416. if (h->use_weight_chroma) {
  417. chroma_weight_op(dest_cb, h->mb_uvlinesize, chroma_height,
  418. h->chroma_log2_weight_denom,
  419. h->chroma_weight[refn][list][0][0],
  420. h->chroma_weight[refn][list][0][1]);
  421. chroma_weight_op(dest_cr, h->mb_uvlinesize, chroma_height,
  422. h->chroma_log2_weight_denom,
  423. h->chroma_weight[refn][list][1][0],
  424. h->chroma_weight[refn][list][1][1]);
  425. }
  426. }
  427. }
  428. static av_always_inline void prefetch_motion(H264Context *h, int list,
  429. int pixel_shift, int chroma_idc)
  430. {
  431. /* fetch pixels for estimated mv 4 macroblocks ahead
  432. * optimized for 64byte cache lines */
  433. const int refn = h->ref_cache[list][scan8[0]];
  434. if (refn >= 0) {
  435. const int mx = (h->mv_cache[list][scan8[0]][0] >> 2) + 16 * h->mb_x + 8;
  436. const int my = (h->mv_cache[list][scan8[0]][1] >> 2) + 16 * h->mb_y;
  437. uint8_t **src = h->ref_list[list][refn].f.data;
  438. int off = (mx << pixel_shift) +
  439. (my + (h->mb_x & 3) * 4) * h->mb_linesize +
  440. (64 << pixel_shift);
  441. h->vdsp.prefetch(src[0] + off, h->linesize, 4);
  442. if (chroma_idc == 3 /* yuv444 */) {
  443. h->vdsp.prefetch(src[1] + off, h->linesize, 4);
  444. h->vdsp.prefetch(src[2] + off, h->linesize, 4);
  445. } else {
  446. off = ((mx >> 1) << pixel_shift) +
  447. ((my >> 1) + (h->mb_x & 7)) * h->uvlinesize +
  448. (64 << pixel_shift);
  449. h->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
  450. }
  451. }
  452. }
  453. static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
  454. uint8_t *src_cb, uint8_t *src_cr,
  455. int linesize, int uvlinesize,
  456. int xchg, int chroma444,
  457. int simple, int pixel_shift)
  458. {
  459. int deblock_topleft;
  460. int deblock_top;
  461. int top_idx = 1;
  462. uint8_t *top_border_m1;
  463. uint8_t *top_border;
  464. if (!simple && FRAME_MBAFF(h)) {
  465. if (h->mb_y & 1) {
  466. if (!MB_MBAFF(h))
  467. return;
  468. } else {
  469. top_idx = MB_MBAFF(h) ? 0 : 1;
  470. }
  471. }
  472. if (h->deblocking_filter == 2) {
  473. deblock_topleft = h->slice_table[h->mb_xy - 1 - h->mb_stride] == h->slice_num;
  474. deblock_top = h->top_type;
  475. } else {
  476. deblock_topleft = (h->mb_x > 0);
  477. deblock_top = (h->mb_y > !!MB_FIELD(h));
  478. }
  479. src_y -= linesize + 1 + pixel_shift;
  480. src_cb -= uvlinesize + 1 + pixel_shift;
  481. src_cr -= uvlinesize + 1 + pixel_shift;
  482. top_border_m1 = h->top_borders[top_idx][h->mb_x - 1];
  483. top_border = h->top_borders[top_idx][h->mb_x];
  484. #define XCHG(a, b, xchg) \
  485. if (pixel_shift) { \
  486. if (xchg) { \
  487. AV_SWAP64(b + 0, a + 0); \
  488. AV_SWAP64(b + 8, a + 8); \
  489. } else { \
  490. AV_COPY128(b, a); \
  491. } \
  492. } else if (xchg) \
  493. AV_SWAP64(b, a); \
  494. else \
  495. AV_COPY64(b, a);
  496. if (deblock_top) {
  497. if (deblock_topleft) {
  498. XCHG(top_border_m1 + (8 << pixel_shift),
  499. src_y - (7 << pixel_shift), 1);
  500. }
  501. XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
  502. XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
  503. if (h->mb_x + 1 < h->mb_width) {
  504. XCHG(h->top_borders[top_idx][h->mb_x + 1],
  505. src_y + (17 << pixel_shift), 1);
  506. }
  507. }
  508. if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
  509. if (chroma444) {
  510. if (deblock_top) {
  511. if (deblock_topleft) {
  512. XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  513. XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  514. }
  515. XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
  516. XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
  517. XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
  518. XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
  519. if (h->mb_x + 1 < h->mb_width) {
  520. XCHG(h->top_borders[top_idx][h->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
  521. XCHG(h->top_borders[top_idx][h->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
  522. }
  523. }
  524. } else {
  525. if (deblock_top) {
  526. if (deblock_topleft) {
  527. XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  528. XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  529. }
  530. XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
  531. XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
  532. }
  533. }
  534. }
  535. }
  536. static av_always_inline int dctcoef_get(int16_t *mb, int high_bit_depth,
  537. int index)
  538. {
  539. if (high_bit_depth) {
  540. return AV_RN32A(((int32_t *)mb) + index);
  541. } else
  542. return AV_RN16A(mb + index);
  543. }
  544. static av_always_inline void dctcoef_set(int16_t *mb, int high_bit_depth,
  545. int index, int value)
  546. {
  547. if (high_bit_depth) {
  548. AV_WN32A(((int32_t *)mb) + index, value);
  549. } else
  550. AV_WN16A(mb + index, value);
  551. }
  552. static av_always_inline void hl_decode_mb_predict_luma(H264Context *h,
  553. int mb_type, int is_h264,
  554. int simple,
  555. int transform_bypass,
  556. int pixel_shift,
  557. int *block_offset,
  558. int linesize,
  559. uint8_t *dest_y, int p)
  560. {
  561. void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
  562. void (*idct_dc_add)(uint8_t *dst, int16_t *block, int stride);
  563. int i;
  564. int qscale = p == 0 ? h->qscale : h->chroma_qp[p - 1];
  565. block_offset += 16 * p;
  566. if (IS_INTRA4x4(mb_type)) {
  567. if (IS_8x8DCT(mb_type)) {
  568. if (transform_bypass) {
  569. idct_dc_add =
  570. idct_add = h->h264dsp.h264_add_pixels8_clear;
  571. } else {
  572. idct_dc_add = h->h264dsp.h264_idct8_dc_add;
  573. idct_add = h->h264dsp.h264_idct8_add;
  574. }
  575. for (i = 0; i < 16; i += 4) {
  576. uint8_t *const ptr = dest_y + block_offset[i];
  577. const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
  578. if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
  579. h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  580. } else {
  581. const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];
  582. h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,
  583. (h->topright_samples_available << i) & 0x4000, linesize);
  584. if (nnz) {
  585. if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  586. idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  587. else
  588. idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  589. }
  590. }
  591. }
  592. } else {
  593. if (transform_bypass) {
  594. idct_dc_add =
  595. idct_add = h->h264dsp.h264_add_pixels4_clear;
  596. } else {
  597. idct_dc_add = h->h264dsp.h264_idct_dc_add;
  598. idct_add = h->h264dsp.h264_idct_add;
  599. }
  600. for (i = 0; i < 16; i++) {
  601. uint8_t *const ptr = dest_y + block_offset[i];
  602. const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
  603. if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
  604. h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  605. } else {
  606. uint8_t *topright;
  607. int nnz, tr;
  608. uint64_t tr_high;
  609. if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
  610. const int topright_avail = (h->topright_samples_available << i) & 0x8000;
  611. assert(h->mb_y || linesize <= block_offset[i]);
  612. if (!topright_avail) {
  613. if (pixel_shift) {
  614. tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
  615. topright = (uint8_t *)&tr_high;
  616. } else {
  617. tr = ptr[3 - linesize] * 0x01010101u;
  618. topright = (uint8_t *)&tr;
  619. }
  620. } else
  621. topright = ptr + (4 << pixel_shift) - linesize;
  622. } else
  623. topright = NULL;
  624. h->hpc.pred4x4[dir](ptr, topright, linesize);
  625. nnz = h->non_zero_count_cache[scan8[i + p * 16]];
  626. if (nnz) {
  627. if (is_h264) {
  628. if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  629. idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  630. else
  631. idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  632. } else if (CONFIG_SVQ3_DECODER)
  633. ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);
  634. }
  635. }
  636. }
  637. }
  638. } else {
  639. h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);
  640. if (is_h264) {
  641. if (h->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {
  642. if (!transform_bypass)
  643. h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),
  644. h->mb_luma_dc[p],
  645. h->dequant4_coeff[p][qscale][0]);
  646. else {
  647. static const uint8_t dc_mapping[16] = {
  648. 0 * 16, 1 * 16, 4 * 16, 5 * 16,
  649. 2 * 16, 3 * 16, 6 * 16, 7 * 16,
  650. 8 * 16, 9 * 16, 12 * 16, 13 * 16,
  651. 10 * 16, 11 * 16, 14 * 16, 15 * 16
  652. };
  653. for (i = 0; i < 16; i++)
  654. dctcoef_set(h->mb + (p * 256 << pixel_shift),
  655. pixel_shift, dc_mapping[i],
  656. dctcoef_get(h->mb_luma_dc[p],
  657. pixel_shift, i));
  658. }
  659. }
  660. } else if (CONFIG_SVQ3_DECODER)
  661. ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,
  662. h->mb_luma_dc[p], qscale);
  663. }
  664. }
  665. static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type,
  666. int is_h264, int simple,
  667. int transform_bypass,
  668. int pixel_shift,
  669. int *block_offset,
  670. int linesize,
  671. uint8_t *dest_y, int p)
  672. {
  673. void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
  674. int i;
  675. block_offset += 16 * p;
  676. if (!IS_INTRA4x4(mb_type)) {
  677. if (is_h264) {
  678. if (IS_INTRA16x16(mb_type)) {
  679. if (transform_bypass) {
  680. if (h->sps.profile_idc == 244 &&
  681. (h->intra16x16_pred_mode == VERT_PRED8x8 ||
  682. h->intra16x16_pred_mode == HOR_PRED8x8)) {
  683. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset,
  684. h->mb + (p * 256 << pixel_shift),
  685. linesize);
  686. } else {
  687. for (i = 0; i < 16; i++)
  688. if (h->non_zero_count_cache[scan8[i + p * 16]] ||
  689. dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  690. h->h264dsp.h264_add_pixels4_clear(dest_y + block_offset[i],
  691. h->mb + (i * 16 + p * 256 << pixel_shift),
  692. linesize);
  693. }
  694. } else {
  695. h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
  696. h->mb + (p * 256 << pixel_shift),
  697. linesize,
  698. h->non_zero_count_cache + p * 5 * 8);
  699. }
  700. } else if (h->cbp & 15) {
  701. if (transform_bypass) {
  702. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  703. idct_add = IS_8x8DCT(mb_type) ? h->h264dsp.h264_add_pixels8_clear
  704. : h->h264dsp.h264_add_pixels4_clear;
  705. for (i = 0; i < 16; i += di)
  706. if (h->non_zero_count_cache[scan8[i + p * 16]])
  707. idct_add(dest_y + block_offset[i],
  708. h->mb + (i * 16 + p * 256 << pixel_shift),
  709. linesize);
  710. } else {
  711. if (IS_8x8DCT(mb_type))
  712. h->h264dsp.h264_idct8_add4(dest_y, block_offset,
  713. h->mb + (p * 256 << pixel_shift),
  714. linesize,
  715. h->non_zero_count_cache + p * 5 * 8);
  716. else
  717. h->h264dsp.h264_idct_add16(dest_y, block_offset,
  718. h->mb + (p * 256 << pixel_shift),
  719. linesize,
  720. h->non_zero_count_cache + p * 5 * 8);
  721. }
  722. }
  723. } else if (CONFIG_SVQ3_DECODER) {
  724. for (i = 0; i < 16; i++)
  725. if (h->non_zero_count_cache[scan8[i + p * 16]] || h->mb[i * 16 + p * 256]) {
  726. // FIXME benchmark weird rule, & below
  727. uint8_t *const ptr = dest_y + block_offset[i];
  728. ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize,
  729. h->qscale, IS_INTRA(mb_type) ? 1 : 0);
  730. }
  731. }
  732. }
  733. }
  734. #define BITS 8
  735. #define SIMPLE 1
  736. #include "h264_mb_template.c"
  737. #undef BITS
  738. #define BITS 16
  739. #include "h264_mb_template.c"
  740. #undef SIMPLE
  741. #define SIMPLE 0
  742. #include "h264_mb_template.c"
  743. void ff_h264_hl_decode_mb(H264Context *h)
  744. {
  745. const int mb_xy = h->mb_xy;
  746. const int mb_type = h->cur_pic.mb_type[mb_xy];
  747. int is_complex = CONFIG_SMALL || h->is_complex ||
  748. IS_INTRA_PCM(mb_type) || h->qscale == 0;
  749. if (CHROMA444(h)) {
  750. if (is_complex || h->pixel_shift)
  751. hl_decode_mb_444_complex(h);
  752. else
  753. hl_decode_mb_444_simple_8(h);
  754. } else if (is_complex) {
  755. hl_decode_mb_complex(h);
  756. } else if (h->pixel_shift) {
  757. hl_decode_mb_simple_16(h);
  758. } else
  759. hl_decode_mb_simple_8(h);
  760. }