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.

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