2 * H.26L/H.264/AVC/JVT/14496-10/... decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of Libav.
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.
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.
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
24 * H.264 / AVC / MPEG4 part10 macroblock decoding
31 #include "libavutil/common.h"
32 #include "libavutil/intreadwrite.h"
38 static inline int get_lowest_part_list_y(H264SliceContext *sl,
39 int n, int height, int y_offset, int list)
41 int raw_my = sl->mv_cache[list][scan8[n]][1];
42 int filter_height_up = (raw_my & 3) ? 2 : 0;
43 int filter_height_down = (raw_my & 3) ? 3 : 0;
44 int full_my = (raw_my >> 2) + y_offset;
45 int top = full_my - filter_height_up;
46 int bottom = full_my + filter_height_down + height;
48 return FFMAX(abs(top), bottom);
51 static inline void get_lowest_part_y(const H264Context *h, H264SliceContext *sl,
52 int refs[2][48], int n,
53 int height, int y_offset, int list0,
54 int list1, int *nrefs)
58 y_offset += 16 * (sl->mb_y >> MB_FIELD(sl));
61 int ref_n = sl->ref_cache[0][scan8[n]];
62 H264Ref *ref = &sl->ref_list[0][ref_n];
64 // Error resilience puts the current picture in the ref list.
65 // Don't try to wait on these as it will cause a deadlock.
66 // Fields can wait on each other, though.
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, 0);
70 if (refs[0][ref_n] < 0)
72 refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
77 int ref_n = sl->ref_cache[1][scan8[n]];
78 H264Ref *ref = &sl->ref_list[1][ref_n];
80 if (ref->parent->tf.progress->data != h->cur_pic.tf.progress->data ||
81 (ref->reference & 3) != h->picture_structure) {
82 my = get_lowest_part_list_y(sl, n, height, y_offset, 1);
83 if (refs[1][ref_n] < 0)
85 refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
91 * Wait until all reference frames are available for MC operations.
93 * @param h the H264 context
95 static void await_references(const H264Context *h, H264SliceContext *sl)
97 const int mb_xy = sl->mb_xy;
98 const int mb_type = h->cur_pic.mb_type[mb_xy];
100 int nrefs[2] = { 0 };
103 memset(refs, -1, sizeof(refs));
105 if (IS_16X16(mb_type)) {
106 get_lowest_part_y(h, sl, refs, 0, 16, 0,
107 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
108 } else if (IS_16X8(mb_type)) {
109 get_lowest_part_y(h, sl, refs, 0, 8, 0,
110 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
111 get_lowest_part_y(h, sl, refs, 8, 8, 8,
112 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
113 } else if (IS_8X16(mb_type)) {
114 get_lowest_part_y(h, sl, refs, 0, 16, 0,
115 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
116 get_lowest_part_y(h, sl, refs, 4, 16, 0,
117 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
121 assert(IS_8X8(mb_type));
123 for (i = 0; i < 4; i++) {
124 const int sub_mb_type = sl->sub_mb_type[i];
126 int y_offset = (i & 2) << 2;
128 if (IS_SUB_8X8(sub_mb_type)) {
129 get_lowest_part_y(h, sl, refs, n, 8, y_offset,
130 IS_DIR(sub_mb_type, 0, 0),
131 IS_DIR(sub_mb_type, 0, 1),
133 } else if (IS_SUB_8X4(sub_mb_type)) {
134 get_lowest_part_y(h, sl, refs, n, 4, y_offset,
135 IS_DIR(sub_mb_type, 0, 0),
136 IS_DIR(sub_mb_type, 0, 1),
138 get_lowest_part_y(h, sl, refs, n + 2, 4, y_offset + 4,
139 IS_DIR(sub_mb_type, 0, 0),
140 IS_DIR(sub_mb_type, 0, 1),
142 } else if (IS_SUB_4X8(sub_mb_type)) {
143 get_lowest_part_y(h, sl, refs, n, 8, y_offset,
144 IS_DIR(sub_mb_type, 0, 0),
145 IS_DIR(sub_mb_type, 0, 1),
147 get_lowest_part_y(h, sl, refs, n + 1, 8, y_offset,
148 IS_DIR(sub_mb_type, 0, 0),
149 IS_DIR(sub_mb_type, 0, 1),
153 assert(IS_SUB_4X4(sub_mb_type));
154 for (j = 0; j < 4; j++) {
155 int sub_y_offset = y_offset + 2 * (j & 2);
156 get_lowest_part_y(h, sl, refs, n + j, 4, sub_y_offset,
157 IS_DIR(sub_mb_type, 0, 0),
158 IS_DIR(sub_mb_type, 0, 1),
165 for (list = sl->list_count - 1; list >= 0; list--)
166 for (ref = 0; ref < 48 && nrefs[list]; ref++) {
167 int row = refs[list][ref];
169 H264Ref *ref_pic = &sl->ref_list[list][ref];
170 int ref_field = ref_pic->reference - 1;
171 int ref_field_picture = ref_pic->parent->field_picture;
172 int pic_height = 16 * h->mb_height >> ref_field_picture;
174 row <<= MB_MBAFF(sl);
177 if (!FIELD_PICTURE(h) && ref_field_picture) { // frame referencing two fields
178 ff_thread_await_progress(&ref_pic->parent->tf,
179 FFMIN((row >> 1) - !(row & 1),
182 ff_thread_await_progress(&ref_pic->parent->tf,
183 FFMIN((row >> 1), pic_height - 1),
185 } else if (FIELD_PICTURE(h) && !ref_field_picture) { // field referencing one field of a frame
186 ff_thread_await_progress(&ref_pic->parent->tf,
187 FFMIN(row * 2 + ref_field,
190 } else if (FIELD_PICTURE(h)) {
191 ff_thread_await_progress(&ref_pic->parent->tf,
192 FFMIN(row, pic_height - 1),
195 ff_thread_await_progress(&ref_pic->parent->tf,
196 FFMIN(row, pic_height - 1),
203 static av_always_inline void mc_dir_part(const H264Context *h, H264SliceContext *sl,
205 int n, int square, int height,
207 uint8_t *dest_y, uint8_t *dest_cb,
209 int src_x_offset, int src_y_offset,
210 const qpel_mc_func *qpix_op,
211 h264_chroma_mc_func chroma_op,
212 int pixel_shift, int chroma_idc)
214 const int mx = sl->mv_cache[list][scan8[n]][0] + src_x_offset * 8;
215 int my = sl->mv_cache[list][scan8[n]][1] + src_y_offset * 8;
216 const int luma_xy = (mx & 3) + ((my & 3) << 2);
217 ptrdiff_t offset = ((mx >> 2) << pixel_shift) + (my >> 2) * sl->mb_linesize;
218 uint8_t *src_y = pic->data[0] + offset;
219 uint8_t *src_cb, *src_cr;
221 int extra_height = 0;
223 const int full_mx = mx >> 2;
224 const int full_my = my >> 2;
225 const int pic_width = 16 * h->mb_width;
226 const int pic_height = 16 * h->mb_height >> MB_FIELD(sl);
234 if (full_mx < 0 - extra_width ||
235 full_my < 0 - extra_height ||
236 full_mx + 16 /*FIXME*/ > pic_width + extra_width ||
237 full_my + 16 /*FIXME*/ > pic_height + extra_height) {
238 h->vdsp.emulated_edge_mc(sl->edge_emu_buffer,
239 src_y - (2 << pixel_shift) - 2 * sl->mb_linesize,
240 sl->mb_linesize, sl->mb_linesize,
241 16 + 5, 16 + 5 /*FIXME*/, full_mx - 2,
242 full_my - 2, pic_width, pic_height);
243 src_y = sl->edge_emu_buffer + (2 << pixel_shift) + 2 * sl->mb_linesize;
247 qpix_op[luma_xy](dest_y, src_y, sl->mb_linesize); // FIXME try variable height perhaps?
249 qpix_op[luma_xy](dest_y + delta, src_y + delta, sl->mb_linesize);
251 if (CONFIG_GRAY && h->flags & AV_CODEC_FLAG_GRAY)
254 if (chroma_idc == 3 /* yuv444 */) {
255 src_cb = pic->data[1] + offset;
257 h->vdsp.emulated_edge_mc(sl->edge_emu_buffer,
258 src_cb - (2 << pixel_shift) - 2 * sl->mb_linesize,
259 sl->mb_linesize, sl->mb_linesize,
260 16 + 5, 16 + 5 /*FIXME*/,
261 full_mx - 2, full_my - 2,
262 pic_width, pic_height);
263 src_cb = sl->edge_emu_buffer + (2 << pixel_shift) + 2 * sl->mb_linesize;
265 qpix_op[luma_xy](dest_cb, src_cb, sl->mb_linesize); // FIXME try variable height perhaps?
267 qpix_op[luma_xy](dest_cb + delta, src_cb + delta, sl->mb_linesize);
269 src_cr = pic->data[2] + offset;
271 h->vdsp.emulated_edge_mc(sl->edge_emu_buffer,
272 src_cr - (2 << pixel_shift) - 2 * sl->mb_linesize,
273 sl->mb_linesize, sl->mb_linesize,
274 16 + 5, 16 + 5 /*FIXME*/,
275 full_mx - 2, full_my - 2,
276 pic_width, pic_height);
277 src_cr = sl->edge_emu_buffer + (2 << pixel_shift) + 2 * sl->mb_linesize;
279 qpix_op[luma_xy](dest_cr, src_cr, sl->mb_linesize); // FIXME try variable height perhaps?
281 qpix_op[luma_xy](dest_cr + delta, src_cr + delta, sl->mb_linesize);
285 ysh = 3 - (chroma_idc == 2 /* yuv422 */);
286 if (chroma_idc == 1 /* yuv420 */ && MB_FIELD(sl)) {
287 // chroma offset when predicting from a field of opposite parity
288 my += 2 * ((sl->mb_y & 1) - (pic->reference - 1));
289 emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
292 src_cb = pic->data[1] + ((mx >> 3) << pixel_shift) +
293 (my >> ysh) * sl->mb_uvlinesize;
294 src_cr = pic->data[2] + ((mx >> 3) << pixel_shift) +
295 (my >> ysh) * sl->mb_uvlinesize;
298 h->vdsp.emulated_edge_mc(sl->edge_emu_buffer, src_cb,
299 sl->mb_uvlinesize, sl->mb_uvlinesize,
300 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
301 pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
302 src_cb = sl->edge_emu_buffer;
304 chroma_op(dest_cb, src_cb, sl->mb_uvlinesize,
305 height >> (chroma_idc == 1 /* yuv420 */),
306 mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
309 h->vdsp.emulated_edge_mc(sl->edge_emu_buffer, src_cr,
310 sl->mb_uvlinesize, sl->mb_uvlinesize,
311 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
312 pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
313 src_cr = sl->edge_emu_buffer;
315 chroma_op(dest_cr, src_cr, sl->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
316 mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
319 static av_always_inline void mc_part_std(const H264Context *h, H264SliceContext *sl,
321 int height, int delta,
322 uint8_t *dest_y, uint8_t *dest_cb,
324 int x_offset, int y_offset,
325 const qpel_mc_func *qpix_put,
326 h264_chroma_mc_func chroma_put,
327 const qpel_mc_func *qpix_avg,
328 h264_chroma_mc_func chroma_avg,
329 int list0, int list1,
330 int pixel_shift, int chroma_idc)
332 const qpel_mc_func *qpix_op = qpix_put;
333 h264_chroma_mc_func chroma_op = chroma_put;
335 dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * sl->mb_linesize;
336 if (chroma_idc == 3 /* yuv444 */) {
337 dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * sl->mb_linesize;
338 dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * sl->mb_linesize;
339 } else if (chroma_idc == 2 /* yuv422 */) {
340 dest_cb += (x_offset << pixel_shift) + 2 * y_offset * sl->mb_uvlinesize;
341 dest_cr += (x_offset << pixel_shift) + 2 * y_offset * sl->mb_uvlinesize;
342 } else { /* yuv420 */
343 dest_cb += (x_offset << pixel_shift) + y_offset * sl->mb_uvlinesize;
344 dest_cr += (x_offset << pixel_shift) + y_offset * sl->mb_uvlinesize;
346 x_offset += 8 * sl->mb_x;
347 y_offset += 8 * (sl->mb_y >> MB_FIELD(sl));
350 H264Ref *ref = &sl->ref_list[0][sl->ref_cache[0][scan8[n]]];
351 mc_dir_part(h, sl, ref, n, square, height, delta, 0,
352 dest_y, dest_cb, dest_cr, x_offset, y_offset,
353 qpix_op, chroma_op, pixel_shift, chroma_idc);
356 chroma_op = chroma_avg;
360 H264Ref *ref = &sl->ref_list[1][sl->ref_cache[1][scan8[n]]];
361 mc_dir_part(h, sl, ref, n, square, height, delta, 1,
362 dest_y, dest_cb, dest_cr, x_offset, y_offset,
363 qpix_op, chroma_op, pixel_shift, chroma_idc);
367 static av_always_inline void mc_part_weighted(const H264Context *h, H264SliceContext *sl,
369 int height, int delta,
370 uint8_t *dest_y, uint8_t *dest_cb,
372 int x_offset, int y_offset,
373 const qpel_mc_func *qpix_put,
374 h264_chroma_mc_func chroma_put,
375 h264_weight_func luma_weight_op,
376 h264_weight_func chroma_weight_op,
377 h264_biweight_func luma_weight_avg,
378 h264_biweight_func chroma_weight_avg,
379 int list0, int list1,
380 int pixel_shift, int chroma_idc)
384 dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * sl->mb_linesize;
385 if (chroma_idc == 3 /* yuv444 */) {
386 chroma_height = height;
387 chroma_weight_avg = luma_weight_avg;
388 chroma_weight_op = luma_weight_op;
389 dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * sl->mb_linesize;
390 dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * sl->mb_linesize;
391 } else if (chroma_idc == 2 /* yuv422 */) {
392 chroma_height = height;
393 dest_cb += (x_offset << pixel_shift) + 2 * y_offset * sl->mb_uvlinesize;
394 dest_cr += (x_offset << pixel_shift) + 2 * y_offset * sl->mb_uvlinesize;
395 } else { /* yuv420 */
396 chroma_height = height >> 1;
397 dest_cb += (x_offset << pixel_shift) + y_offset * sl->mb_uvlinesize;
398 dest_cr += (x_offset << pixel_shift) + y_offset * sl->mb_uvlinesize;
400 x_offset += 8 * sl->mb_x;
401 y_offset += 8 * (sl->mb_y >> MB_FIELD(sl));
403 if (list0 && list1) {
404 /* don't optimize for luma-only case, since B-frames usually
405 * use implicit weights => chroma too. */
406 uint8_t *tmp_cb = sl->bipred_scratchpad;
407 uint8_t *tmp_cr = sl->bipred_scratchpad + (16 << pixel_shift);
408 uint8_t *tmp_y = sl->bipred_scratchpad + 16 * sl->mb_uvlinesize;
409 int refn0 = sl->ref_cache[0][scan8[n]];
410 int refn1 = sl->ref_cache[1][scan8[n]];
412 mc_dir_part(h, sl, &sl->ref_list[0][refn0], n, square, height, delta, 0,
413 dest_y, dest_cb, dest_cr,
414 x_offset, y_offset, qpix_put, chroma_put,
415 pixel_shift, chroma_idc);
416 mc_dir_part(h, sl, &sl->ref_list[1][refn1], n, square, height, delta, 1,
417 tmp_y, tmp_cb, tmp_cr,
418 x_offset, y_offset, qpix_put, chroma_put,
419 pixel_shift, chroma_idc);
421 if (sl->pwt.use_weight == 2) {
422 int weight0 = sl->pwt.implicit_weight[refn0][refn1][sl->mb_y & 1];
423 int weight1 = 64 - weight0;
424 luma_weight_avg(dest_y, tmp_y, sl->mb_linesize,
425 height, 5, weight0, weight1, 0);
426 chroma_weight_avg(dest_cb, tmp_cb, sl->mb_uvlinesize,
427 chroma_height, 5, weight0, weight1, 0);
428 chroma_weight_avg(dest_cr, tmp_cr, sl->mb_uvlinesize,
429 chroma_height, 5, weight0, weight1, 0);
431 luma_weight_avg(dest_y, tmp_y, sl->mb_linesize, height,
432 sl->pwt.luma_log2_weight_denom,
433 sl->pwt.luma_weight[refn0][0][0],
434 sl->pwt.luma_weight[refn1][1][0],
435 sl->pwt.luma_weight[refn0][0][1] +
436 sl->pwt.luma_weight[refn1][1][1]);
437 chroma_weight_avg(dest_cb, tmp_cb, sl->mb_uvlinesize, chroma_height,
438 sl->pwt.chroma_log2_weight_denom,
439 sl->pwt.chroma_weight[refn0][0][0][0],
440 sl->pwt.chroma_weight[refn1][1][0][0],
441 sl->pwt.chroma_weight[refn0][0][0][1] +
442 sl->pwt.chroma_weight[refn1][1][0][1]);
443 chroma_weight_avg(dest_cr, tmp_cr, sl->mb_uvlinesize, chroma_height,
444 sl->pwt.chroma_log2_weight_denom,
445 sl->pwt.chroma_weight[refn0][0][1][0],
446 sl->pwt.chroma_weight[refn1][1][1][0],
447 sl->pwt.chroma_weight[refn0][0][1][1] +
448 sl->pwt.chroma_weight[refn1][1][1][1]);
451 int list = list1 ? 1 : 0;
452 int refn = sl->ref_cache[list][scan8[n]];
453 H264Ref *ref = &sl->ref_list[list][refn];
454 mc_dir_part(h, sl, ref, n, square, height, delta, list,
455 dest_y, dest_cb, dest_cr, x_offset, y_offset,
456 qpix_put, chroma_put, pixel_shift, chroma_idc);
458 luma_weight_op(dest_y, sl->mb_linesize, height,
459 sl->pwt.luma_log2_weight_denom,
460 sl->pwt.luma_weight[refn][list][0],
461 sl->pwt.luma_weight[refn][list][1]);
462 if (sl->pwt.use_weight_chroma) {
463 chroma_weight_op(dest_cb, sl->mb_uvlinesize, chroma_height,
464 sl->pwt.chroma_log2_weight_denom,
465 sl->pwt.chroma_weight[refn][list][0][0],
466 sl->pwt.chroma_weight[refn][list][0][1]);
467 chroma_weight_op(dest_cr, sl->mb_uvlinesize, chroma_height,
468 sl->pwt.chroma_log2_weight_denom,
469 sl->pwt.chroma_weight[refn][list][1][0],
470 sl->pwt.chroma_weight[refn][list][1][1]);
475 static av_always_inline void prefetch_motion(const H264Context *h, H264SliceContext *sl,
476 int list, int pixel_shift,
479 /* fetch pixels for estimated mv 4 macroblocks ahead
480 * optimized for 64byte cache lines */
481 const int refn = sl->ref_cache[list][scan8[0]];
483 const int mx = (sl->mv_cache[list][scan8[0]][0] >> 2) + 16 * sl->mb_x + 8;
484 const int my = (sl->mv_cache[list][scan8[0]][1] >> 2) + 16 * sl->mb_y;
485 uint8_t **src = sl->ref_list[list][refn].data;
486 int off = (mx << pixel_shift) +
487 (my + (sl->mb_x & 3) * 4) * sl->mb_linesize +
489 h->vdsp.prefetch(src[0] + off, sl->linesize, 4);
490 if (chroma_idc == 3 /* yuv444 */) {
491 h->vdsp.prefetch(src[1] + off, sl->linesize, 4);
492 h->vdsp.prefetch(src[2] + off, sl->linesize, 4);
494 off = ((mx >> 1) << pixel_shift) +
495 ((my >> 1) + (sl->mb_x & 7)) * sl->uvlinesize +
497 h->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
502 static av_always_inline void xchg_mb_border(const H264Context *h, H264SliceContext *sl,
504 uint8_t *src_cb, uint8_t *src_cr,
505 int linesize, int uvlinesize,
506 int xchg, int chroma444,
507 int simple, int pixel_shift)
512 uint8_t *top_border_m1;
515 if (!simple && FRAME_MBAFF(h)) {
520 top_idx = MB_MBAFF(sl) ? 0 : 1;
524 if (sl->deblocking_filter == 2) {
525 deblock_topleft = h->slice_table[sl->mb_xy - 1 - h->mb_stride] == sl->slice_num;
526 deblock_top = sl->top_type;
528 deblock_topleft = (sl->mb_x > 0);
529 deblock_top = (sl->mb_y > !!MB_FIELD(sl));
532 src_y -= linesize + 1 + pixel_shift;
533 src_cb -= uvlinesize + 1 + pixel_shift;
534 src_cr -= uvlinesize + 1 + pixel_shift;
536 top_border_m1 = sl->top_borders[top_idx][sl->mb_x - 1];
537 top_border = sl->top_borders[top_idx][sl->mb_x];
539 #define XCHG(a, b, xchg) \
542 AV_SWAP64(b + 0, a + 0); \
543 AV_SWAP64(b + 8, a + 8); \
553 if (deblock_topleft) {
554 XCHG(top_border_m1 + (8 << pixel_shift),
555 src_y - (7 << pixel_shift), 1);
557 XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
558 XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
559 if (sl->mb_x + 1 < h->mb_width) {
560 XCHG(sl->top_borders[top_idx][sl->mb_x + 1],
561 src_y + (17 << pixel_shift), 1);
564 if (simple || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
567 if (deblock_topleft) {
568 XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
569 XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
571 XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
572 XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
573 XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
574 XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
575 if (sl->mb_x + 1 < h->mb_width) {
576 XCHG(sl->top_borders[top_idx][sl->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
577 XCHG(sl->top_borders[top_idx][sl->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
582 if (deblock_topleft) {
583 XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
584 XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
586 XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
587 XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
593 static av_always_inline int dctcoef_get(int16_t *mb, int high_bit_depth,
596 if (high_bit_depth) {
597 return AV_RN32A(((int32_t *)mb) + index);
599 return AV_RN16A(mb + index);
602 static av_always_inline void dctcoef_set(int16_t *mb, int high_bit_depth,
603 int index, int value)
605 if (high_bit_depth) {
606 AV_WN32A(((int32_t *)mb) + index, value);
608 AV_WN16A(mb + index, value);
611 static av_always_inline void hl_decode_mb_predict_luma(const H264Context *h,
612 H264SliceContext *sl,
613 int mb_type, int simple,
614 int transform_bypass,
616 const int *block_offset,
618 uint8_t *dest_y, int p)
620 void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
621 void (*idct_dc_add)(uint8_t *dst, int16_t *block, int stride);
623 int qscale = p == 0 ? sl->qscale : sl->chroma_qp[p - 1];
624 block_offset += 16 * p;
625 if (IS_INTRA4x4(mb_type)) {
626 if (IS_8x8DCT(mb_type)) {
627 if (transform_bypass) {
629 idct_add = h->h264dsp.h264_add_pixels8_clear;
631 idct_dc_add = h->h264dsp.h264_idct8_dc_add;
632 idct_add = h->h264dsp.h264_idct8_add;
634 for (i = 0; i < 16; i += 4) {
635 uint8_t *const ptr = dest_y + block_offset[i];
636 const int dir = sl->intra4x4_pred_mode_cache[scan8[i]];
637 if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
638 h->hpc.pred8x8l_add[dir](ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
640 const int nnz = sl->non_zero_count_cache[scan8[i + p * 16]];
641 h->hpc.pred8x8l[dir](ptr, (sl->topleft_samples_available << i) & 0x8000,
642 (sl->topright_samples_available << i) & 0x4000, linesize);
644 if (nnz == 1 && dctcoef_get(sl->mb, pixel_shift, i * 16 + p * 256))
645 idct_dc_add(ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
647 idct_add(ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
652 if (transform_bypass) {
654 idct_add = h->h264dsp.h264_add_pixels4_clear;
656 idct_dc_add = h->h264dsp.h264_idct_dc_add;
657 idct_add = h->h264dsp.h264_idct_add;
659 for (i = 0; i < 16; i++) {
660 uint8_t *const ptr = dest_y + block_offset[i];
661 const int dir = sl->intra4x4_pred_mode_cache[scan8[i]];
663 if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
664 h->hpc.pred4x4_add[dir](ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
669 if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
670 const int topright_avail = (sl->topright_samples_available << i) & 0x8000;
671 assert(sl->mb_y || linesize <= block_offset[i]);
672 if (!topright_avail) {
674 tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
675 topright = (uint8_t *)&tr_high;
677 tr = ptr[3 - linesize] * 0x01010101u;
678 topright = (uint8_t *)&tr;
681 topright = ptr + (4 << pixel_shift) - linesize;
685 h->hpc.pred4x4[dir](ptr, topright, linesize);
686 nnz = sl->non_zero_count_cache[scan8[i + p * 16]];
688 if (nnz == 1 && dctcoef_get(sl->mb, pixel_shift, i * 16 + p * 256))
689 idct_dc_add(ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
691 idct_add(ptr, sl->mb + (i * 16 + p * 256 << pixel_shift), linesize);
697 h->hpc.pred16x16[sl->intra16x16_pred_mode](dest_y, linesize);
698 if (sl->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {
699 if (!transform_bypass)
700 h->h264dsp.h264_luma_dc_dequant_idct(sl->mb + (p * 256 << pixel_shift),
702 h->dequant4_coeff[p][qscale][0]);
704 static const uint8_t dc_mapping[16] = {
705 0 * 16, 1 * 16, 4 * 16, 5 * 16,
706 2 * 16, 3 * 16, 6 * 16, 7 * 16,
707 8 * 16, 9 * 16, 12 * 16, 13 * 16,
708 10 * 16, 11 * 16, 14 * 16, 15 * 16
710 for (i = 0; i < 16; i++)
711 dctcoef_set(sl->mb + (p * 256 << pixel_shift),
712 pixel_shift, dc_mapping[i],
713 dctcoef_get(sl->mb_luma_dc[p],
720 static av_always_inline void hl_decode_mb_idct_luma(const H264Context *h, H264SliceContext *sl,
721 int mb_type, int simple,
722 int transform_bypass,
724 const int *block_offset,
726 uint8_t *dest_y, int p)
728 void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
730 block_offset += 16 * p;
731 if (!IS_INTRA4x4(mb_type)) {
732 if (IS_INTRA16x16(mb_type)) {
733 if (transform_bypass) {
734 if (h->sps.profile_idc == 244 &&
735 (sl->intra16x16_pred_mode == VERT_PRED8x8 ||
736 sl->intra16x16_pred_mode == HOR_PRED8x8)) {
737 h->hpc.pred16x16_add[sl->intra16x16_pred_mode](dest_y, block_offset,
738 sl->mb + (p * 256 << pixel_shift),
741 for (i = 0; i < 16; i++)
742 if (sl->non_zero_count_cache[scan8[i + p * 16]] ||
743 dctcoef_get(sl->mb, pixel_shift, i * 16 + p * 256))
744 h->h264dsp.h264_add_pixels4_clear(dest_y + block_offset[i],
745 sl->mb + (i * 16 + p * 256 << pixel_shift),
749 h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
750 sl->mb + (p * 256 << pixel_shift),
752 sl->non_zero_count_cache + p * 5 * 8);
754 } else if (sl->cbp & 15) {
755 if (transform_bypass) {
756 const int di = IS_8x8DCT(mb_type) ? 4 : 1;
757 idct_add = IS_8x8DCT(mb_type) ? h->h264dsp.h264_add_pixels8_clear
758 : h->h264dsp.h264_add_pixels4_clear;
759 for (i = 0; i < 16; i += di)
760 if (sl->non_zero_count_cache[scan8[i + p * 16]])
761 idct_add(dest_y + block_offset[i],
762 sl->mb + (i * 16 + p * 256 << pixel_shift),
765 if (IS_8x8DCT(mb_type))
766 h->h264dsp.h264_idct8_add4(dest_y, block_offset,
767 sl->mb + (p * 256 << pixel_shift),
769 sl->non_zero_count_cache + p * 5 * 8);
771 h->h264dsp.h264_idct_add16(dest_y, block_offset,
772 sl->mb + (p * 256 << pixel_shift),
774 sl->non_zero_count_cache + p * 5 * 8);
782 #include "h264_mb_template.c"
786 #include "h264_mb_template.c"
790 #include "h264_mb_template.c"
792 void ff_h264_hl_decode_mb(const H264Context *h, H264SliceContext *sl)
794 const int mb_xy = sl->mb_xy;
795 const int mb_type = h->cur_pic.mb_type[mb_xy];
796 int is_complex = CONFIG_SMALL || sl->is_complex ||
797 IS_INTRA_PCM(mb_type) || sl->qscale == 0;
800 if (is_complex || h->pixel_shift)
801 hl_decode_mb_444_complex(h, sl);
803 hl_decode_mb_444_simple_8(h, sl);
804 } else if (is_complex) {
805 hl_decode_mb_complex(h, sl);
806 } else if (h->pixel_shift) {
807 hl_decode_mb_simple_16(h, sl);
809 hl_decode_mb_simple_8(h, sl);