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.

812 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 / MPEG-4 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 "h264dec.h"
  31. #include "h264_ps.h"
  32. #include "qpeldsp.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_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(const H264Context *h, H264SliceContext *sl,
  46. int refs[2][48], int n,
  47. int height, int y_offset, int list0,
  48. int list1, int *nrefs)
  49. {
  50. int my;
  51. y_offset += 16 * (sl->mb_y >> MB_FIELD(sl));
  52. if (list0) {
  53. int ref_n = sl->ref_cache[0][scan8[n]];
  54. H264Ref *ref = &sl->ref_list[0][ref_n];
  55. // Error resilience puts the current picture in the ref list.
  56. // Don't try to wait on these as it will cause a deadlock.
  57. // Fields can wait on each other, though.
  58. if (ref->parent->tf.progress->data != h->cur_pic.tf.progress->data ||
  59. (ref->reference & 3) != h->picture_structure) {
  60. my = get_lowest_part_list_y(sl, n, height, y_offset, 0);
  61. if (refs[0][ref_n] < 0)
  62. nrefs[0] += 1;
  63. refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
  64. }
  65. }
  66. if (list1) {
  67. int ref_n = sl->ref_cache[1][scan8[n]];
  68. H264Ref *ref = &sl->ref_list[1][ref_n];
  69. if (ref->parent->tf.progress->data != h->cur_pic.tf.progress->data ||
  70. (ref->reference & 3) != h->picture_structure) {
  71. my = get_lowest_part_list_y(sl, n, height, y_offset, 1);
  72. if (refs[1][ref_n] < 0)
  73. nrefs[1] += 1;
  74. refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
  75. }
  76. }
  77. }
  78. /**
  79. * Wait until all reference frames are available for MC operations.
  80. *
  81. * @param h the H.264 context
  82. */
  83. static void await_references(const H264Context *h, H264SliceContext *sl)
  84. {
  85. const int mb_xy = sl->mb_xy;
  86. const int mb_type = h->cur_pic.mb_type[mb_xy];
  87. int refs[2][48];
  88. int nrefs[2] = { 0 };
  89. int ref, list;
  90. memset(refs, -1, sizeof(refs));
  91. if (IS_16X16(mb_type)) {
  92. get_lowest_part_y(h, sl, refs, 0, 16, 0,
  93. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  94. } else if (IS_16X8(mb_type)) {
  95. get_lowest_part_y(h, sl, refs, 0, 8, 0,
  96. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  97. get_lowest_part_y(h, sl, refs, 8, 8, 8,
  98. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
  99. } else if (IS_8X16(mb_type)) {
  100. get_lowest_part_y(h, sl, refs, 0, 16, 0,
  101. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  102. get_lowest_part_y(h, sl, refs, 4, 16, 0,
  103. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
  104. } else {
  105. int i;
  106. assert(IS_8X8(mb_type));
  107. for (i = 0; i < 4; i++) {
  108. const int sub_mb_type = sl->sub_mb_type[i];
  109. const int n = 4 * i;
  110. int y_offset = (i & 2) << 2;
  111. if (IS_SUB_8X8(sub_mb_type)) {
  112. get_lowest_part_y(h, sl, refs, n, 8, y_offset,
  113. IS_DIR(sub_mb_type, 0, 0),
  114. IS_DIR(sub_mb_type, 0, 1),
  115. nrefs);
  116. } else if (IS_SUB_8X4(sub_mb_type)) {
  117. get_lowest_part_y(h, sl, refs, n, 4, y_offset,
  118. IS_DIR(sub_mb_type, 0, 0),
  119. IS_DIR(sub_mb_type, 0, 1),
  120. nrefs);
  121. get_lowest_part_y(h, sl, refs, n + 2, 4, y_offset + 4,
  122. IS_DIR(sub_mb_type, 0, 0),
  123. IS_DIR(sub_mb_type, 0, 1),
  124. nrefs);
  125. } else if (IS_SUB_4X8(sub_mb_type)) {
  126. get_lowest_part_y(h, sl, refs, n, 8, y_offset,
  127. IS_DIR(sub_mb_type, 0, 0),
  128. IS_DIR(sub_mb_type, 0, 1),
  129. nrefs);
  130. get_lowest_part_y(h, sl, refs, n + 1, 8, y_offset,
  131. IS_DIR(sub_mb_type, 0, 0),
  132. IS_DIR(sub_mb_type, 0, 1),
  133. nrefs);
  134. } else {
  135. int j;
  136. assert(IS_SUB_4X4(sub_mb_type));
  137. for (j = 0; j < 4; j++) {
  138. int sub_y_offset = y_offset + 2 * (j & 2);
  139. get_lowest_part_y(h, sl, refs, n + j, 4, sub_y_offset,
  140. IS_DIR(sub_mb_type, 0, 0),
  141. IS_DIR(sub_mb_type, 0, 1),
  142. nrefs);
  143. }
  144. }
  145. }
  146. }
  147. for (list = sl->list_count - 1; list >= 0; list--)
  148. for (ref = 0; ref < 48 && nrefs[list]; ref++) {
  149. int row = refs[list][ref];
  150. if (row >= 0) {
  151. H264Ref *ref_pic = &sl->ref_list[list][ref];
  152. int ref_field = ref_pic->reference - 1;
  153. int ref_field_picture = ref_pic->parent->field_picture;
  154. int pic_height = 16 * h->mb_height >> ref_field_picture;
  155. row <<= MB_MBAFF(sl);
  156. nrefs[list]--;
  157. if (!FIELD_PICTURE(h) && ref_field_picture) { // frame referencing two fields
  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) << 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 & AV_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) << pixel_shift) +
  264. (my >> ysh) * sl->mb_uvlinesize;
  265. src_cr = pic->data[2] + ((mx >> 3) << 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, (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, (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->pwt.use_weight == 2) {
  381. int weight0 = sl->pwt.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. chroma_weight_avg(dest_cb, tmp_cb, sl->mb_uvlinesize,
  386. chroma_height, 5, weight0, weight1, 0);
  387. chroma_weight_avg(dest_cr, tmp_cr, sl->mb_uvlinesize,
  388. chroma_height, 5, weight0, weight1, 0);
  389. } else {
  390. luma_weight_avg(dest_y, tmp_y, sl->mb_linesize, height,
  391. sl->pwt.luma_log2_weight_denom,
  392. sl->pwt.luma_weight[refn0][0][0],
  393. sl->pwt.luma_weight[refn1][1][0],
  394. sl->pwt.luma_weight[refn0][0][1] +
  395. sl->pwt.luma_weight[refn1][1][1]);
  396. chroma_weight_avg(dest_cb, tmp_cb, sl->mb_uvlinesize, chroma_height,
  397. sl->pwt.chroma_log2_weight_denom,
  398. sl->pwt.chroma_weight[refn0][0][0][0],
  399. sl->pwt.chroma_weight[refn1][1][0][0],
  400. sl->pwt.chroma_weight[refn0][0][0][1] +
  401. sl->pwt.chroma_weight[refn1][1][0][1]);
  402. chroma_weight_avg(dest_cr, tmp_cr, sl->mb_uvlinesize, chroma_height,
  403. sl->pwt.chroma_log2_weight_denom,
  404. sl->pwt.chroma_weight[refn0][0][1][0],
  405. sl->pwt.chroma_weight[refn1][1][1][0],
  406. sl->pwt.chroma_weight[refn0][0][1][1] +
  407. sl->pwt.chroma_weight[refn1][1][1][1]);
  408. }
  409. } else {
  410. int list = list1 ? 1 : 0;
  411. int refn = sl->ref_cache[list][scan8[n]];
  412. H264Ref *ref = &sl->ref_list[list][refn];
  413. mc_dir_part(h, sl, ref, n, square, height, delta, list,
  414. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  415. qpix_put, chroma_put, pixel_shift, chroma_idc);
  416. luma_weight_op(dest_y, sl->mb_linesize, height,
  417. sl->pwt.luma_log2_weight_denom,
  418. sl->pwt.luma_weight[refn][list][0],
  419. sl->pwt.luma_weight[refn][list][1]);
  420. if (sl->pwt.use_weight_chroma) {
  421. chroma_weight_op(dest_cb, sl->mb_uvlinesize, chroma_height,
  422. sl->pwt.chroma_log2_weight_denom,
  423. sl->pwt.chroma_weight[refn][list][0][0],
  424. sl->pwt.chroma_weight[refn][list][0][1]);
  425. chroma_weight_op(dest_cr, sl->mb_uvlinesize, chroma_height,
  426. sl->pwt.chroma_log2_weight_denom,
  427. sl->pwt.chroma_weight[refn][list][1][0],
  428. sl->pwt.chroma_weight[refn][list][1][1]);
  429. }
  430. }
  431. }
  432. static av_always_inline void prefetch_motion(const H264Context *h, H264SliceContext *sl,
  433. int list, int pixel_shift,
  434. int chroma_idc)
  435. {
  436. /* fetch pixels for estimated mv 4 macroblocks ahead
  437. * optimized for 64byte cache lines */
  438. const int refn = sl->ref_cache[list][scan8[0]];
  439. if (refn >= 0) {
  440. const int mx = (sl->mv_cache[list][scan8[0]][0] >> 2) + 16 * sl->mb_x + 8;
  441. const int my = (sl->mv_cache[list][scan8[0]][1] >> 2) + 16 * sl->mb_y;
  442. uint8_t **src = sl->ref_list[list][refn].data;
  443. int off = (mx << pixel_shift) +
  444. (my + (sl->mb_x & 3) * 4) * sl->mb_linesize +
  445. (64 << pixel_shift);
  446. h->vdsp.prefetch(src[0] + off, sl->linesize, 4);
  447. if (chroma_idc == 3 /* yuv444 */) {
  448. h->vdsp.prefetch(src[1] + off, sl->linesize, 4);
  449. h->vdsp.prefetch(src[2] + off, sl->linesize, 4);
  450. } else {
  451. off = ((mx >> 1) << pixel_shift) +
  452. ((my >> 1) + (sl->mb_x & 7)) * sl->uvlinesize +
  453. (64 << pixel_shift);
  454. h->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
  455. }
  456. }
  457. }
  458. static av_always_inline void xchg_mb_border(const H264Context *h, H264SliceContext *sl,
  459. uint8_t *src_y,
  460. uint8_t *src_cb, uint8_t *src_cr,
  461. int linesize, int uvlinesize,
  462. int xchg, int chroma444,
  463. int simple, int pixel_shift)
  464. {
  465. int deblock_topleft;
  466. int deblock_top;
  467. int top_idx = 1;
  468. uint8_t *top_border_m1;
  469. uint8_t *top_border;
  470. if (!simple && FRAME_MBAFF(h)) {
  471. if (sl->mb_y & 1) {
  472. if (!MB_MBAFF(sl))
  473. return;
  474. } else {
  475. top_idx = MB_MBAFF(sl) ? 0 : 1;
  476. }
  477. }
  478. if (sl->deblocking_filter == 2) {
  479. deblock_topleft = h->slice_table[sl->mb_xy - 1 - h->mb_stride] == sl->slice_num;
  480. deblock_top = sl->top_type;
  481. } else {
  482. deblock_topleft = (sl->mb_x > 0);
  483. deblock_top = (sl->mb_y > !!MB_FIELD(sl));
  484. }
  485. src_y -= linesize + 1 + pixel_shift;
  486. src_cb -= uvlinesize + 1 + pixel_shift;
  487. src_cr -= uvlinesize + 1 + pixel_shift;
  488. top_border_m1 = sl->top_borders[top_idx][sl->mb_x - 1];
  489. top_border = sl->top_borders[top_idx][sl->mb_x];
  490. #define XCHG(a, b, xchg) \
  491. if (pixel_shift) { \
  492. if (xchg) { \
  493. AV_SWAP64(b + 0, a + 0); \
  494. AV_SWAP64(b + 8, a + 8); \
  495. } else { \
  496. AV_COPY128(b, a); \
  497. } \
  498. } else if (xchg) \
  499. AV_SWAP64(b, a); \
  500. else \
  501. AV_COPY64(b, a);
  502. if (deblock_top) {
  503. if (deblock_topleft) {
  504. XCHG(top_border_m1 + (8 << pixel_shift),
  505. src_y - (7 << pixel_shift), 1);
  506. }
  507. XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
  508. XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
  509. if (sl->mb_x + 1 < h->mb_width) {
  510. XCHG(sl->top_borders[top_idx][sl->mb_x + 1],
  511. src_y + (17 << pixel_shift), 1);
  512. }
  513. }
  514. if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
  515. if (chroma444) {
  516. if (deblock_top) {
  517. if (deblock_topleft) {
  518. XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  519. XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  520. }
  521. XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
  522. XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
  523. XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
  524. XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
  525. if (sl->mb_x + 1 < h->mb_width) {
  526. XCHG(sl->top_borders[top_idx][sl->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
  527. XCHG(sl->top_borders[top_idx][sl->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
  528. }
  529. }
  530. } else {
  531. if (deblock_top) {
  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 simple,
  561. int transform_bypass,
  562. int pixel_shift,
  563. const int *block_offset,
  564. int linesize,
  565. uint8_t *dest_y, int p)
  566. {
  567. void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
  568. void (*idct_dc_add)(uint8_t *dst, int16_t *block, int stride);
  569. int i;
  570. int qscale = p == 0 ? sl->qscale : sl->chroma_qp[p - 1];
  571. block_offset += 16 * p;
  572. if (IS_INTRA4x4(mb_type)) {
  573. if (IS_8x8DCT(mb_type)) {
  574. if (transform_bypass) {
  575. idct_dc_add =
  576. idct_add = h->h264dsp.h264_add_pixels8_clear;
  577. } else {
  578. idct_dc_add = h->h264dsp.h264_idct8_dc_add;
  579. idct_add = h->h264dsp.h264_idct8_add;
  580. }
  581. for (i = 0; i < 16; i += 4) {
  582. uint8_t *const ptr = dest_y + block_offset[i];
  583. const int dir = sl->intra4x4_pred_mode_cache[scan8[i]];
  584. if (transform_bypass && h->ps.sps->profile_idc == 244 && dir <= 1) {
  585. h->hpc.pred8x8l_add[dir](ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  586. } else {
  587. const int nnz = sl->non_zero_count_cache[scan8[i + p * 16]];
  588. h->hpc.pred8x8l[dir](ptr, (sl->topleft_samples_available << i) & 0x8000,
  589. (sl->topright_samples_available << i) & 0x4000, linesize);
  590. if (nnz) {
  591. if (nnz == 1 && dctcoef_get(sl->mb, pixel_shift, i * 16 + p * 256))
  592. idct_dc_add(ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  593. else
  594. idct_add(ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  595. }
  596. }
  597. }
  598. } else {
  599. if (transform_bypass) {
  600. idct_dc_add =
  601. idct_add = h->h264dsp.h264_add_pixels4_clear;
  602. } else {
  603. idct_dc_add = h->h264dsp.h264_idct_dc_add;
  604. idct_add = h->h264dsp.h264_idct_add;
  605. }
  606. for (i = 0; i < 16; i++) {
  607. uint8_t *const ptr = dest_y + block_offset[i];
  608. const int dir = sl->intra4x4_pred_mode_cache[scan8[i]];
  609. if (transform_bypass && h->ps.sps->profile_idc == 244 && dir <= 1) {
  610. h->hpc.pred4x4_add[dir](ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  611. } else {
  612. uint8_t *topright;
  613. int nnz, tr;
  614. uint64_t tr_high;
  615. if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
  616. const int topright_avail = (sl->topright_samples_available << i) & 0x8000;
  617. assert(sl->mb_y || linesize <= block_offset[i]);
  618. if (!topright_avail) {
  619. if (pixel_shift) {
  620. tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
  621. topright = (uint8_t *)&tr_high;
  622. } else {
  623. tr = ptr[3 - linesize] * 0x01010101u;
  624. topright = (uint8_t *)&tr;
  625. }
  626. } else
  627. topright = ptr + (4 << pixel_shift) - linesize;
  628. } else
  629. topright = NULL;
  630. h->hpc.pred4x4[dir](ptr, topright, linesize);
  631. nnz = sl->non_zero_count_cache[scan8[i + p * 16]];
  632. if (nnz) {
  633. if (nnz == 1 && dctcoef_get(sl->mb, pixel_shift, i * 16 + p * 256))
  634. idct_dc_add(ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  635. else
  636. idct_add(ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  637. }
  638. }
  639. }
  640. }
  641. } else {
  642. h->hpc.pred16x16[sl->intra16x16_pred_mode](dest_y, linesize);
  643. if (sl->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {
  644. if (!transform_bypass)
  645. h->h264dsp.h264_luma_dc_dequant_idct(sl->mb + (p * 256 << pixel_shift),
  646. sl->mb_luma_dc[p],
  647. h->ps.pps->dequant4_coeff[p][qscale][0]);
  648. else {
  649. static const uint8_t dc_mapping[16] = {
  650. 0 * 16, 1 * 16, 4 * 16, 5 * 16,
  651. 2 * 16, 3 * 16, 6 * 16, 7 * 16,
  652. 8 * 16, 9 * 16, 12 * 16, 13 * 16,
  653. 10 * 16, 11 * 16, 14 * 16, 15 * 16
  654. };
  655. for (i = 0; i < 16; i++)
  656. dctcoef_set(sl->mb + (p * 256 << pixel_shift),
  657. pixel_shift, dc_mapping[i],
  658. dctcoef_get(sl->mb_luma_dc[p],
  659. pixel_shift, i));
  660. }
  661. }
  662. }
  663. }
  664. static av_always_inline void hl_decode_mb_idct_luma(const H264Context *h, H264SliceContext *sl,
  665. int mb_type, int simple,
  666. int transform_bypass,
  667. int pixel_shift,
  668. const int *block_offset,
  669. int linesize,
  670. uint8_t *dest_y, int p)
  671. {
  672. void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
  673. int i;
  674. block_offset += 16 * p;
  675. if (!IS_INTRA4x4(mb_type)) {
  676. if (IS_INTRA16x16(mb_type)) {
  677. if (transform_bypass) {
  678. if (h->ps.sps->profile_idc == 244 &&
  679. (sl->intra16x16_pred_mode == VERT_PRED8x8 ||
  680. sl->intra16x16_pred_mode == HOR_PRED8x8)) {
  681. h->hpc.pred16x16_add[sl->intra16x16_pred_mode](dest_y, block_offset,
  682. sl->mb + (p * 256 << pixel_shift),
  683. linesize);
  684. } else {
  685. for (i = 0; i < 16; i++)
  686. if (sl->non_zero_count_cache[scan8[i + p * 16]] ||
  687. dctcoef_get(sl->mb, pixel_shift, i * 16 + p * 256))
  688. h->h264dsp.h264_add_pixels4_clear(dest_y + block_offset[i],
  689. sl->mb + (i * 16 + p * 256 << pixel_shift),
  690. linesize);
  691. }
  692. } else {
  693. h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
  694. sl->mb + (p * 256 << pixel_shift),
  695. linesize,
  696. sl->non_zero_count_cache + p * 5 * 8);
  697. }
  698. } else if (sl->cbp & 15) {
  699. if (transform_bypass) {
  700. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  701. idct_add = IS_8x8DCT(mb_type) ? h->h264dsp.h264_add_pixels8_clear
  702. : h->h264dsp.h264_add_pixels4_clear;
  703. for (i = 0; i < 16; i += di)
  704. if (sl->non_zero_count_cache[scan8[i + p * 16]])
  705. idct_add(dest_y + block_offset[i],
  706. sl->mb + (i * 16 + p * 256 << pixel_shift),
  707. linesize);
  708. } else {
  709. if (IS_8x8DCT(mb_type))
  710. h->h264dsp.h264_idct8_add4(dest_y, block_offset,
  711. sl->mb + (p * 256 << pixel_shift),
  712. linesize,
  713. sl->non_zero_count_cache + p * 5 * 8);
  714. else
  715. h->h264dsp.h264_idct_add16(dest_y, block_offset,
  716. sl->mb + (p * 256 << pixel_shift),
  717. linesize,
  718. sl->non_zero_count_cache + p * 5 * 8);
  719. }
  720. }
  721. }
  722. }
  723. #define BITS 8
  724. #define SIMPLE 1
  725. #include "h264_mb_template.c"
  726. #undef BITS
  727. #define BITS 16
  728. #include "h264_mb_template.c"
  729. #undef SIMPLE
  730. #define SIMPLE 0
  731. #include "h264_mb_template.c"
  732. void ff_h264_hl_decode_mb(const H264Context *h, H264SliceContext *sl)
  733. {
  734. const int mb_xy = sl->mb_xy;
  735. const int mb_type = h->cur_pic.mb_type[mb_xy];
  736. int is_complex = CONFIG_SMALL || sl->is_complex ||
  737. IS_INTRA_PCM(mb_type) || sl->qscale == 0;
  738. if (CHROMA444(h)) {
  739. if (is_complex || h->pixel_shift)
  740. hl_decode_mb_444_complex(h, sl);
  741. else
  742. hl_decode_mb_444_simple_8(h, sl);
  743. } else if (is_complex) {
  744. hl_decode_mb_complex(h, sl);
  745. } else if (h->pixel_shift) {
  746. hl_decode_mb_simple_16(h, sl);
  747. } else
  748. hl_decode_mb_simple_8(h, sl);
  749. }