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.

837 lines
38KB

  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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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(H264SliceContext *sl,
  35. int n, int height, int y_offset, int list)
  36. {
  37. int raw_my = sl->mv_cache[list][scan8[n]][1];
  38. int filter_height_down = (raw_my & 3) ? 3 : 0;
  39. int full_my = (raw_my >> 2) + y_offset;
  40. int bottom = full_my + filter_height_down + height;
  41. av_assert2(height >= 0);
  42. return FFMAX(0, bottom);
  43. }
  44. static inline void get_lowest_part_y(const H264Context *h, H264SliceContext *sl,
  45. int16_t 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 * (sl->mb_y >> MB_FIELD(sl));
  51. if (list0) {
  52. int ref_n = sl->ref_cache[0][scan8[n]];
  53. H264Ref *ref = &sl->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->parent->tf.progress->data != h->cur_pic.tf.progress->data ||
  58. (ref->reference & 3) != h->picture_structure) {
  59. my = get_lowest_part_list_y(sl, 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 = sl->ref_cache[1][scan8[n]];
  67. H264Ref *ref = &sl->ref_list[1][ref_n];
  68. if (ref->parent->tf.progress->data != h->cur_pic.tf.progress->data ||
  69. (ref->reference & 3) != h->picture_structure) {
  70. my = get_lowest_part_list_y(sl, 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(const H264Context *h, H264SliceContext *sl)
  83. {
  84. const int mb_xy = sl->mb_xy;
  85. const int mb_type = h->cur_pic.mb_type[mb_xy];
  86. int16_t 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, sl, 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, sl, refs, 0, 8, 0,
  95. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  96. get_lowest_part_y(h, sl, 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, sl, refs, 0, 16, 0,
  100. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  101. get_lowest_part_y(h, sl, refs, 4, 16, 0,
  102. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
  103. } else {
  104. int i;
  105. av_assert2(IS_8X8(mb_type));
  106. for (i = 0; i < 4; i++) {
  107. const int sub_mb_type = sl->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, sl, 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, sl, 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, sl, 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, sl, 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, sl, 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. av_assert2(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, sl, 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 = sl->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. H264Ref *ref_pic = &sl->ref_list[list][ref];
  151. int ref_field = ref_pic->reference - 1;
  152. int ref_field_picture = ref_pic->parent->field_picture;
  153. int pic_height = 16 * h->mb_height >> ref_field_picture;
  154. row <<= MB_MBAFF(sl);
  155. nrefs[list]--;
  156. if (!FIELD_PICTURE(h) && ref_field_picture) { // frame referencing two fields
  157. av_assert2((ref_pic->parent->reference & 3) == 3);
  158. ff_thread_await_progress(&ref_pic->parent->tf,
  159. FFMIN((row >> 1) - !(row & 1),
  160. pic_height - 1),
  161. 1);
  162. ff_thread_await_progress(&ref_pic->parent->tf,
  163. FFMIN((row >> 1), pic_height - 1),
  164. 0);
  165. } else if (FIELD_PICTURE(h) && !ref_field_picture) { // field referencing one field of a frame
  166. ff_thread_await_progress(&ref_pic->parent->tf,
  167. FFMIN(row * 2 + ref_field,
  168. pic_height - 1),
  169. 0);
  170. } else if (FIELD_PICTURE(h)) {
  171. ff_thread_await_progress(&ref_pic->parent->tf,
  172. FFMIN(row, pic_height - 1),
  173. ref_field);
  174. } else {
  175. ff_thread_await_progress(&ref_pic->parent->tf,
  176. FFMIN(row, pic_height - 1),
  177. 0);
  178. }
  179. }
  180. }
  181. }
  182. static av_always_inline void mc_dir_part(const H264Context *h, H264SliceContext *sl,
  183. H264Ref *pic,
  184. int n, int square, int height,
  185. int delta, int list,
  186. uint8_t *dest_y, uint8_t *dest_cb,
  187. uint8_t *dest_cr,
  188. int src_x_offset, int src_y_offset,
  189. const qpel_mc_func *qpix_op,
  190. h264_chroma_mc_func chroma_op,
  191. int pixel_shift, int chroma_idc)
  192. {
  193. const int mx = sl->mv_cache[list][scan8[n]][0] + src_x_offset * 8;
  194. int my = sl->mv_cache[list][scan8[n]][1] + src_y_offset * 8;
  195. const int luma_xy = (mx & 3) + ((my & 3) << 2);
  196. ptrdiff_t offset = (mx >> 2) * (1 << pixel_shift) + (my >> 2) * sl->mb_linesize;
  197. uint8_t *src_y = pic->data[0] + offset;
  198. uint8_t *src_cb, *src_cr;
  199. int extra_width = 0;
  200. int extra_height = 0;
  201. int emu = 0;
  202. const int full_mx = mx >> 2;
  203. const int full_my = my >> 2;
  204. const int pic_width = 16 * h->mb_width;
  205. const int pic_height = 16 * h->mb_height >> MB_FIELD(sl);
  206. int ysh;
  207. if (mx & 7)
  208. extra_width -= 3;
  209. if (my & 7)
  210. extra_height -= 3;
  211. if (full_mx < 0 - extra_width ||
  212. full_my < 0 - extra_height ||
  213. full_mx + 16 /*FIXME*/ > pic_width + extra_width ||
  214. full_my + 16 /*FIXME*/ > pic_height + extra_height) {
  215. h->vdsp.emulated_edge_mc(sl->edge_emu_buffer,
  216. src_y - (2 << pixel_shift) - 2 * sl->mb_linesize,
  217. sl->mb_linesize, sl->mb_linesize,
  218. 16 + 5, 16 + 5 /*FIXME*/, full_mx - 2,
  219. full_my - 2, pic_width, pic_height);
  220. src_y = sl->edge_emu_buffer + (2 << pixel_shift) + 2 * sl->mb_linesize;
  221. emu = 1;
  222. }
  223. qpix_op[luma_xy](dest_y, src_y, sl->mb_linesize); // FIXME try variable height perhaps?
  224. if (!square)
  225. qpix_op[luma_xy](dest_y + delta, src_y + delta, sl->mb_linesize);
  226. if (CONFIG_GRAY && h->flags & CODEC_FLAG_GRAY)
  227. return;
  228. if (chroma_idc == 3 /* yuv444 */) {
  229. src_cb = pic->data[1] + offset;
  230. if (emu) {
  231. h->vdsp.emulated_edge_mc(sl->edge_emu_buffer,
  232. src_cb - (2 << pixel_shift) - 2 * sl->mb_linesize,
  233. sl->mb_linesize, sl->mb_linesize,
  234. 16 + 5, 16 + 5 /*FIXME*/,
  235. full_mx - 2, full_my - 2,
  236. pic_width, pic_height);
  237. src_cb = sl->edge_emu_buffer + (2 << pixel_shift) + 2 * sl->mb_linesize;
  238. }
  239. qpix_op[luma_xy](dest_cb, src_cb, sl->mb_linesize); // FIXME try variable height perhaps?
  240. if (!square)
  241. qpix_op[luma_xy](dest_cb + delta, src_cb + delta, sl->mb_linesize);
  242. src_cr = pic->data[2] + offset;
  243. if (emu) {
  244. h->vdsp.emulated_edge_mc(sl->edge_emu_buffer,
  245. src_cr - (2 << pixel_shift) - 2 * sl->mb_linesize,
  246. sl->mb_linesize, sl->mb_linesize,
  247. 16 + 5, 16 + 5 /*FIXME*/,
  248. full_mx - 2, full_my - 2,
  249. pic_width, pic_height);
  250. src_cr = sl->edge_emu_buffer + (2 << pixel_shift) + 2 * sl->mb_linesize;
  251. }
  252. qpix_op[luma_xy](dest_cr, src_cr, sl->mb_linesize); // FIXME try variable height perhaps?
  253. if (!square)
  254. qpix_op[luma_xy](dest_cr + delta, src_cr + delta, sl->mb_linesize);
  255. return;
  256. }
  257. ysh = 3 - (chroma_idc == 2 /* yuv422 */);
  258. if (chroma_idc == 1 /* yuv420 */ && MB_FIELD(sl)) {
  259. // chroma offset when predicting from a field of opposite parity
  260. my += 2 * ((sl->mb_y & 1) - (pic->reference - 1));
  261. emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
  262. }
  263. src_cb = pic->data[1] + ((mx >> 3) * (1 << pixel_shift)) +
  264. (my >> ysh) * sl->mb_uvlinesize;
  265. src_cr = pic->data[2] + ((mx >> 3) * (1 << pixel_shift)) +
  266. (my >> ysh) * sl->mb_uvlinesize;
  267. if (emu) {
  268. h->vdsp.emulated_edge_mc(sl->edge_emu_buffer, src_cb,
  269. sl->mb_uvlinesize, sl->mb_uvlinesize,
  270. 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
  271. pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
  272. src_cb = sl->edge_emu_buffer;
  273. }
  274. chroma_op(dest_cb, src_cb, sl->mb_uvlinesize,
  275. height >> (chroma_idc == 1 /* yuv420 */),
  276. mx & 7, ((unsigned)my << (chroma_idc == 2 /* yuv422 */)) & 7);
  277. if (emu) {
  278. h->vdsp.emulated_edge_mc(sl->edge_emu_buffer, src_cr,
  279. sl->mb_uvlinesize, sl->mb_uvlinesize,
  280. 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
  281. pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
  282. src_cr = sl->edge_emu_buffer;
  283. }
  284. chroma_op(dest_cr, src_cr, sl->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
  285. mx & 7, ((unsigned)my << (chroma_idc == 2 /* yuv422 */)) & 7);
  286. }
  287. static av_always_inline void mc_part_std(const H264Context *h, H264SliceContext *sl,
  288. int n, int square,
  289. int height, int delta,
  290. uint8_t *dest_y, uint8_t *dest_cb,
  291. uint8_t *dest_cr,
  292. int x_offset, int y_offset,
  293. const qpel_mc_func *qpix_put,
  294. h264_chroma_mc_func chroma_put,
  295. const qpel_mc_func *qpix_avg,
  296. h264_chroma_mc_func chroma_avg,
  297. int list0, int list1,
  298. int pixel_shift, int chroma_idc)
  299. {
  300. const qpel_mc_func *qpix_op = qpix_put;
  301. h264_chroma_mc_func chroma_op = chroma_put;
  302. dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * sl->mb_linesize;
  303. if (chroma_idc == 3 /* yuv444 */) {
  304. dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * sl->mb_linesize;
  305. dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * sl->mb_linesize;
  306. } else if (chroma_idc == 2 /* yuv422 */) {
  307. dest_cb += (x_offset << pixel_shift) + 2 * y_offset * sl->mb_uvlinesize;
  308. dest_cr += (x_offset << pixel_shift) + 2 * y_offset * sl->mb_uvlinesize;
  309. } else { /* yuv420 */
  310. dest_cb += (x_offset << pixel_shift) + y_offset * sl->mb_uvlinesize;
  311. dest_cr += (x_offset << pixel_shift) + y_offset * sl->mb_uvlinesize;
  312. }
  313. x_offset += 8 * sl->mb_x;
  314. y_offset += 8 * (sl->mb_y >> MB_FIELD(sl));
  315. if (list0) {
  316. H264Ref *ref = &sl->ref_list[0][sl->ref_cache[0][scan8[n]]];
  317. mc_dir_part(h, sl, ref, n, square, height, delta, 0,
  318. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  319. qpix_op, chroma_op, pixel_shift, chroma_idc);
  320. qpix_op = qpix_avg;
  321. chroma_op = chroma_avg;
  322. }
  323. if (list1) {
  324. H264Ref *ref = &sl->ref_list[1][sl->ref_cache[1][scan8[n]]];
  325. mc_dir_part(h, sl, ref, n, square, height, delta, 1,
  326. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  327. qpix_op, chroma_op, pixel_shift, chroma_idc);
  328. }
  329. }
  330. static av_always_inline void mc_part_weighted(const H264Context *h, H264SliceContext *sl,
  331. int n, int square,
  332. int height, int delta,
  333. uint8_t *dest_y, uint8_t *dest_cb,
  334. uint8_t *dest_cr,
  335. int x_offset, int y_offset,
  336. const qpel_mc_func *qpix_put,
  337. h264_chroma_mc_func chroma_put,
  338. h264_weight_func luma_weight_op,
  339. h264_weight_func chroma_weight_op,
  340. h264_biweight_func luma_weight_avg,
  341. h264_biweight_func chroma_weight_avg,
  342. int list0, int list1,
  343. int pixel_shift, int chroma_idc)
  344. {
  345. int chroma_height;
  346. dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * sl->mb_linesize;
  347. if (chroma_idc == 3 /* yuv444 */) {
  348. chroma_height = height;
  349. chroma_weight_avg = luma_weight_avg;
  350. chroma_weight_op = luma_weight_op;
  351. dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * sl->mb_linesize;
  352. dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * sl->mb_linesize;
  353. } else if (chroma_idc == 2 /* yuv422 */) {
  354. chroma_height = height;
  355. dest_cb += (x_offset << pixel_shift) + 2 * y_offset * sl->mb_uvlinesize;
  356. dest_cr += (x_offset << pixel_shift) + 2 * y_offset * sl->mb_uvlinesize;
  357. } else { /* yuv420 */
  358. chroma_height = height >> 1;
  359. dest_cb += (x_offset << pixel_shift) + y_offset * sl->mb_uvlinesize;
  360. dest_cr += (x_offset << pixel_shift) + y_offset * sl->mb_uvlinesize;
  361. }
  362. x_offset += 8 * sl->mb_x;
  363. y_offset += 8 * (sl->mb_y >> MB_FIELD(sl));
  364. if (list0 && list1) {
  365. /* don't optimize for luma-only case, since B-frames usually
  366. * use implicit weights => chroma too. */
  367. uint8_t *tmp_cb = sl->bipred_scratchpad;
  368. uint8_t *tmp_cr = sl->bipred_scratchpad + (16 << pixel_shift);
  369. uint8_t *tmp_y = sl->bipred_scratchpad + 16 * sl->mb_uvlinesize;
  370. int refn0 = sl->ref_cache[0][scan8[n]];
  371. int refn1 = sl->ref_cache[1][scan8[n]];
  372. mc_dir_part(h, sl, &sl->ref_list[0][refn0], n, square, height, delta, 0,
  373. dest_y, dest_cb, dest_cr,
  374. x_offset, y_offset, qpix_put, chroma_put,
  375. pixel_shift, chroma_idc);
  376. mc_dir_part(h, sl, &sl->ref_list[1][refn1], n, square, height, delta, 1,
  377. tmp_y, tmp_cb, tmp_cr,
  378. x_offset, y_offset, qpix_put, chroma_put,
  379. pixel_shift, chroma_idc);
  380. if (sl->use_weight == 2) {
  381. int weight0 = sl->implicit_weight[refn0][refn1][sl->mb_y & 1];
  382. int weight1 = 64 - weight0;
  383. luma_weight_avg(dest_y, tmp_y, sl->mb_linesize,
  384. height, 5, weight0, weight1, 0);
  385. if (!CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
  386. chroma_weight_avg(dest_cb, tmp_cb, sl->mb_uvlinesize,
  387. chroma_height, 5, weight0, weight1, 0);
  388. chroma_weight_avg(dest_cr, tmp_cr, sl->mb_uvlinesize,
  389. chroma_height, 5, weight0, weight1, 0);
  390. }
  391. } else {
  392. luma_weight_avg(dest_y, tmp_y, sl->mb_linesize, height,
  393. sl->luma_log2_weight_denom,
  394. sl->luma_weight[refn0][0][0],
  395. sl->luma_weight[refn1][1][0],
  396. sl->luma_weight[refn0][0][1] +
  397. sl->luma_weight[refn1][1][1]);
  398. if (!CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
  399. chroma_weight_avg(dest_cb, tmp_cb, sl->mb_uvlinesize, chroma_height,
  400. sl->chroma_log2_weight_denom,
  401. sl->chroma_weight[refn0][0][0][0],
  402. sl->chroma_weight[refn1][1][0][0],
  403. sl->chroma_weight[refn0][0][0][1] +
  404. sl->chroma_weight[refn1][1][0][1]);
  405. chroma_weight_avg(dest_cr, tmp_cr, sl->mb_uvlinesize, chroma_height,
  406. sl->chroma_log2_weight_denom,
  407. sl->chroma_weight[refn0][0][1][0],
  408. sl->chroma_weight[refn1][1][1][0],
  409. sl->chroma_weight[refn0][0][1][1] +
  410. sl->chroma_weight[refn1][1][1][1]);
  411. }
  412. }
  413. } else {
  414. int list = list1 ? 1 : 0;
  415. int refn = sl->ref_cache[list][scan8[n]];
  416. H264Ref *ref = &sl->ref_list[list][refn];
  417. mc_dir_part(h, sl, ref, n, square, height, delta, list,
  418. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  419. qpix_put, chroma_put, pixel_shift, chroma_idc);
  420. luma_weight_op(dest_y, sl->mb_linesize, height,
  421. sl->luma_log2_weight_denom,
  422. sl->luma_weight[refn][list][0],
  423. sl->luma_weight[refn][list][1]);
  424. if (!CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
  425. if (sl->use_weight_chroma) {
  426. chroma_weight_op(dest_cb, sl->mb_uvlinesize, chroma_height,
  427. sl->chroma_log2_weight_denom,
  428. sl->chroma_weight[refn][list][0][0],
  429. sl->chroma_weight[refn][list][0][1]);
  430. chroma_weight_op(dest_cr, sl->mb_uvlinesize, chroma_height,
  431. sl->chroma_log2_weight_denom,
  432. sl->chroma_weight[refn][list][1][0],
  433. sl->chroma_weight[refn][list][1][1]);
  434. }
  435. }
  436. }
  437. }
  438. static av_always_inline void prefetch_motion(const H264Context *h, H264SliceContext *sl,
  439. int list, int pixel_shift,
  440. int chroma_idc)
  441. {
  442. /* fetch pixels for estimated mv 4 macroblocks ahead
  443. * optimized for 64byte cache lines */
  444. const int refn = sl->ref_cache[list][scan8[0]];
  445. if (refn >= 0) {
  446. const int mx = (sl->mv_cache[list][scan8[0]][0] >> 2) + 16 * sl->mb_x + 8;
  447. const int my = (sl->mv_cache[list][scan8[0]][1] >> 2) + 16 * sl->mb_y;
  448. uint8_t **src = sl->ref_list[list][refn].data;
  449. int off = mx * (1<< pixel_shift) +
  450. (my + (sl->mb_x & 3) * 4) * sl->mb_linesize +
  451. (64 << pixel_shift);
  452. h->vdsp.prefetch(src[0] + off, sl->linesize, 4);
  453. if (chroma_idc == 3 /* yuv444 */) {
  454. h->vdsp.prefetch(src[1] + off, sl->linesize, 4);
  455. h->vdsp.prefetch(src[2] + off, sl->linesize, 4);
  456. } else {
  457. off= ((mx>>1)+64) * (1<<pixel_shift) + ((my>>1) + (sl->mb_x&7))*sl->uvlinesize;
  458. h->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
  459. }
  460. }
  461. }
  462. static av_always_inline void xchg_mb_border(const H264Context *h, H264SliceContext *sl,
  463. uint8_t *src_y,
  464. uint8_t *src_cb, uint8_t *src_cr,
  465. int linesize, int uvlinesize,
  466. int xchg, int chroma444,
  467. int simple, int pixel_shift)
  468. {
  469. int deblock_topleft;
  470. int deblock_top;
  471. int top_idx = 1;
  472. uint8_t *top_border_m1;
  473. uint8_t *top_border;
  474. if (!simple && FRAME_MBAFF(h)) {
  475. if (sl->mb_y & 1) {
  476. if (!MB_MBAFF(sl))
  477. return;
  478. } else {
  479. top_idx = MB_MBAFF(sl) ? 0 : 1;
  480. }
  481. }
  482. if (sl->deblocking_filter == 2) {
  483. deblock_topleft = h->slice_table[sl->mb_xy - 1 - h->mb_stride] == sl->slice_num;
  484. deblock_top = sl->top_type;
  485. } else {
  486. deblock_topleft = (sl->mb_x > 0);
  487. deblock_top = (sl->mb_y > !!MB_FIELD(sl));
  488. }
  489. src_y -= linesize + 1 + pixel_shift;
  490. src_cb -= uvlinesize + 1 + pixel_shift;
  491. src_cr -= uvlinesize + 1 + pixel_shift;
  492. top_border_m1 = sl->top_borders[top_idx][sl->mb_x - 1];
  493. top_border = sl->top_borders[top_idx][sl->mb_x];
  494. #define XCHG(a, b, xchg) \
  495. if (pixel_shift) { \
  496. if (xchg) { \
  497. AV_SWAP64(b + 0, a + 0); \
  498. AV_SWAP64(b + 8, a + 8); \
  499. } else { \
  500. AV_COPY128(b, a); \
  501. } \
  502. } else if (xchg) \
  503. AV_SWAP64(b, a); \
  504. else \
  505. AV_COPY64(b, a);
  506. if (deblock_top) {
  507. if (deblock_topleft) {
  508. XCHG(top_border_m1 + (8 << pixel_shift),
  509. src_y - (7 << pixel_shift), 1);
  510. }
  511. XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
  512. XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
  513. if (sl->mb_x + 1 < h->mb_width) {
  514. XCHG(sl->top_borders[top_idx][sl->mb_x + 1],
  515. src_y + (17 << pixel_shift), 1);
  516. }
  517. if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
  518. if (chroma444) {
  519. if (deblock_topleft) {
  520. XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  521. XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  522. }
  523. XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
  524. XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
  525. XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
  526. XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
  527. if (sl->mb_x + 1 < h->mb_width) {
  528. XCHG(sl->top_borders[top_idx][sl->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
  529. XCHG(sl->top_borders[top_idx][sl->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
  530. }
  531. } else {
  532. if (deblock_topleft) {
  533. XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  534. XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  535. }
  536. XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
  537. XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
  538. }
  539. }
  540. }
  541. }
  542. static av_always_inline int dctcoef_get(int16_t *mb, int high_bit_depth,
  543. int index)
  544. {
  545. if (high_bit_depth) {
  546. return AV_RN32A(((int32_t *)mb) + index);
  547. } else
  548. return AV_RN16A(mb + index);
  549. }
  550. static av_always_inline void dctcoef_set(int16_t *mb, int high_bit_depth,
  551. int index, int value)
  552. {
  553. if (high_bit_depth) {
  554. AV_WN32A(((int32_t *)mb) + index, value);
  555. } else
  556. AV_WN16A(mb + index, value);
  557. }
  558. static av_always_inline void hl_decode_mb_predict_luma(const H264Context *h,
  559. H264SliceContext *sl,
  560. int mb_type, int is_h264,
  561. int simple,
  562. int transform_bypass,
  563. int pixel_shift,
  564. const int *block_offset,
  565. int linesize,
  566. uint8_t *dest_y, int p)
  567. {
  568. void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
  569. void (*idct_dc_add)(uint8_t *dst, int16_t *block, int stride);
  570. int i;
  571. int qscale = p == 0 ? sl->qscale : sl->chroma_qp[p - 1];
  572. block_offset += 16 * p;
  573. if (IS_INTRA4x4(mb_type)) {
  574. if (IS_8x8DCT(mb_type)) {
  575. if (transform_bypass) {
  576. idct_dc_add =
  577. idct_add = h->h264dsp.h264_add_pixels8_clear;
  578. } else {
  579. idct_dc_add = h->h264dsp.h264_idct8_dc_add;
  580. idct_add = h->h264dsp.h264_idct8_add;
  581. }
  582. for (i = 0; i < 16; i += 4) {
  583. uint8_t *const ptr = dest_y + block_offset[i];
  584. const int dir = sl->intra4x4_pred_mode_cache[scan8[i]];
  585. if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
  586. if (h->x264_build != -1) {
  587. h->hpc.pred8x8l_add[dir](ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  588. } else
  589. h->hpc.pred8x8l_filter_add[dir](ptr, sl->mb + (i * 16 + p * 256 << pixel_shift),
  590. (sl-> topleft_samples_available << i) & 0x8000,
  591. (sl->topright_samples_available << i) & 0x4000, linesize);
  592. } else {
  593. const int nnz = sl->non_zero_count_cache[scan8[i + p * 16]];
  594. h->hpc.pred8x8l[dir](ptr, (sl->topleft_samples_available << i) & 0x8000,
  595. (sl->topright_samples_available << i) & 0x4000, linesize);
  596. if (nnz) {
  597. if (nnz == 1 && dctcoef_get(sl->mb, pixel_shift, i * 16 + p * 256))
  598. idct_dc_add(ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  599. else
  600. idct_add(ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  601. }
  602. }
  603. }
  604. } else {
  605. if (transform_bypass) {
  606. idct_dc_add =
  607. idct_add = h->h264dsp.h264_add_pixels4_clear;
  608. } else {
  609. idct_dc_add = h->h264dsp.h264_idct_dc_add;
  610. idct_add = h->h264dsp.h264_idct_add;
  611. }
  612. for (i = 0; i < 16; i++) {
  613. uint8_t *const ptr = dest_y + block_offset[i];
  614. const int dir = sl->intra4x4_pred_mode_cache[scan8[i]];
  615. if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
  616. h->hpc.pred4x4_add[dir](ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  617. } else {
  618. uint8_t *topright;
  619. int nnz, tr;
  620. uint64_t tr_high;
  621. if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
  622. const int topright_avail = (sl->topright_samples_available << i) & 0x8000;
  623. av_assert2(sl->mb_y || linesize <= block_offset[i]);
  624. if (!topright_avail) {
  625. if (pixel_shift) {
  626. tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
  627. topright = (uint8_t *)&tr_high;
  628. } else {
  629. tr = ptr[3 - linesize] * 0x01010101u;
  630. topright = (uint8_t *)&tr;
  631. }
  632. } else
  633. topright = ptr + (4 << pixel_shift) - linesize;
  634. } else
  635. topright = NULL;
  636. h->hpc.pred4x4[dir](ptr, topright, linesize);
  637. nnz = sl->non_zero_count_cache[scan8[i + p * 16]];
  638. if (nnz) {
  639. if (is_h264) {
  640. if (nnz == 1 && dctcoef_get(sl->mb, pixel_shift, i * 16 + p * 256))
  641. idct_dc_add(ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  642. else
  643. idct_add(ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  644. } else if (CONFIG_SVQ3_DECODER)
  645. ff_svq3_add_idct_c(ptr, sl->mb + i * 16 + p * 256, linesize, qscale, 0);
  646. }
  647. }
  648. }
  649. }
  650. } else {
  651. h->hpc.pred16x16[sl->intra16x16_pred_mode](dest_y, linesize);
  652. if (is_h264) {
  653. if (sl->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {
  654. if (!transform_bypass)
  655. h->h264dsp.h264_luma_dc_dequant_idct(sl->mb + (p * 256 << pixel_shift),
  656. sl->mb_luma_dc[p],
  657. h->dequant4_coeff[p][qscale][0]);
  658. else {
  659. static const uint8_t dc_mapping[16] = {
  660. 0 * 16, 1 * 16, 4 * 16, 5 * 16,
  661. 2 * 16, 3 * 16, 6 * 16, 7 * 16,
  662. 8 * 16, 9 * 16, 12 * 16, 13 * 16,
  663. 10 * 16, 11 * 16, 14 * 16, 15 * 16
  664. };
  665. for (i = 0; i < 16; i++)
  666. dctcoef_set(sl->mb + (p * 256 << pixel_shift),
  667. pixel_shift, dc_mapping[i],
  668. dctcoef_get(sl->mb_luma_dc[p],
  669. pixel_shift, i));
  670. }
  671. }
  672. } else if (CONFIG_SVQ3_DECODER)
  673. ff_svq3_luma_dc_dequant_idct_c(sl->mb + p * 256,
  674. sl->mb_luma_dc[p], qscale);
  675. }
  676. }
  677. static av_always_inline void hl_decode_mb_idct_luma(const H264Context *h, H264SliceContext *sl,
  678. int mb_type,
  679. int is_h264, int simple,
  680. int transform_bypass,
  681. int pixel_shift,
  682. const int *block_offset,
  683. int linesize,
  684. uint8_t *dest_y, int p)
  685. {
  686. void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
  687. int i;
  688. block_offset += 16 * p;
  689. if (!IS_INTRA4x4(mb_type)) {
  690. if (is_h264) {
  691. if (IS_INTRA16x16(mb_type)) {
  692. if (transform_bypass) {
  693. if (h->sps.profile_idc == 244 &&
  694. (sl->intra16x16_pred_mode == VERT_PRED8x8 ||
  695. sl->intra16x16_pred_mode == HOR_PRED8x8)) {
  696. h->hpc.pred16x16_add[sl->intra16x16_pred_mode](dest_y, block_offset,
  697. sl->mb + (p * 256 << pixel_shift),
  698. linesize);
  699. } else {
  700. for (i = 0; i < 16; i++)
  701. if (sl->non_zero_count_cache[scan8[i + p * 16]] ||
  702. dctcoef_get(sl->mb, pixel_shift, i * 16 + p * 256))
  703. h->h264dsp.h264_add_pixels4_clear(dest_y + block_offset[i],
  704. sl->mb + (i * 16 + p * 256 << pixel_shift),
  705. linesize);
  706. }
  707. } else {
  708. h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
  709. sl->mb + (p * 256 << pixel_shift),
  710. linesize,
  711. sl->non_zero_count_cache + p * 5 * 8);
  712. }
  713. } else if (sl->cbp & 15) {
  714. if (transform_bypass) {
  715. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  716. idct_add = IS_8x8DCT(mb_type) ? h->h264dsp.h264_add_pixels8_clear
  717. : h->h264dsp.h264_add_pixels4_clear;
  718. for (i = 0; i < 16; i += di)
  719. if (sl->non_zero_count_cache[scan8[i + p * 16]])
  720. idct_add(dest_y + block_offset[i],
  721. sl->mb + (i * 16 + p * 256 << pixel_shift),
  722. linesize);
  723. } else {
  724. if (IS_8x8DCT(mb_type))
  725. h->h264dsp.h264_idct8_add4(dest_y, block_offset,
  726. sl->mb + (p * 256 << pixel_shift),
  727. linesize,
  728. sl->non_zero_count_cache + p * 5 * 8);
  729. else
  730. h->h264dsp.h264_idct_add16(dest_y, block_offset,
  731. sl->mb + (p * 256 << pixel_shift),
  732. linesize,
  733. sl->non_zero_count_cache + p * 5 * 8);
  734. }
  735. }
  736. } else if (CONFIG_SVQ3_DECODER) {
  737. for (i = 0; i < 16; i++)
  738. if (sl->non_zero_count_cache[scan8[i + p * 16]] || sl->mb[i * 16 + p * 256]) {
  739. // FIXME benchmark weird rule, & below
  740. uint8_t *const ptr = dest_y + block_offset[i];
  741. ff_svq3_add_idct_c(ptr, sl->mb + i * 16 + p * 256, linesize,
  742. sl->qscale, IS_INTRA(mb_type) ? 1 : 0);
  743. }
  744. }
  745. }
  746. }
  747. #define BITS 8
  748. #define SIMPLE 1
  749. #include "h264_mb_template.c"
  750. #undef BITS
  751. #define BITS 16
  752. #include "h264_mb_template.c"
  753. #undef SIMPLE
  754. #define SIMPLE 0
  755. #include "h264_mb_template.c"
  756. void ff_h264_hl_decode_mb(const H264Context *h, H264SliceContext *sl)
  757. {
  758. const int mb_xy = sl->mb_xy;
  759. const int mb_type = h->cur_pic.mb_type[mb_xy];
  760. int is_complex = CONFIG_SMALL || sl->is_complex ||
  761. IS_INTRA_PCM(mb_type) || sl->qscale == 0;
  762. if (CHROMA444(h)) {
  763. if (is_complex || h->pixel_shift)
  764. hl_decode_mb_444_complex(h, sl);
  765. else
  766. hl_decode_mb_444_simple_8(h, sl);
  767. } else if (is_complex) {
  768. hl_decode_mb_complex(h, sl);
  769. } else if (h->pixel_shift) {
  770. hl_decode_mb_simple_16(h, sl);
  771. } else
  772. hl_decode_mb_simple_8(h, sl);
  773. }