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 FFmpeg.
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.
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.
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
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(H264Context *h, H264Picture *pic, int n,
39 int height, int y_offset, int list)
41 int raw_my = h->mv_cache[list][scan8[n]][1];
42 int filter_height_down = (raw_my & 3) ? 3 : 0;
43 int full_my = (raw_my >> 2) + y_offset;
44 int bottom = full_my + filter_height_down + height;
46 av_assert2(height >= 0);
48 return FFMAX(0, bottom);
51 static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n,
52 int height, int y_offset, int list0,
53 int list1, int *nrefs)
57 y_offset += 16 * (h->mb_y >> MB_FIELD(h));
60 int ref_n = h->ref_cache[0][scan8[n]];
61 H264Picture *ref = &h->ref_list[0][ref_n];
63 // Error resilience puts the current picture in the ref list.
64 // Don't try to wait on these as it will cause a deadlock.
65 // Fields can wait on each other, though.
66 if (ref->tf.progress->data != h->cur_pic.tf.progress->data ||
67 (ref->reference & 3) != h->picture_structure) {
68 my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
69 if (refs[0][ref_n] < 0)
71 refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
76 int ref_n = h->ref_cache[1][scan8[n]];
77 H264Picture *ref = &h->ref_list[1][ref_n];
79 if (ref->tf.progress->data != h->cur_pic.tf.progress->data ||
80 (ref->reference & 3) != h->picture_structure) {
81 my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
82 if (refs[1][ref_n] < 0)
84 refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
90 * Wait until all reference frames are available for MC operations.
92 * @param h the H264 context
94 static void await_references(H264Context *h)
96 const int mb_xy = h->mb_xy;
97 const int mb_type = h->cur_pic.mb_type[mb_xy];
102 memset(refs, -1, sizeof(refs));
104 if (IS_16X16(mb_type)) {
105 get_lowest_part_y(h, refs, 0, 16, 0,
106 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
107 } else if (IS_16X8(mb_type)) {
108 get_lowest_part_y(h, refs, 0, 8, 0,
109 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
110 get_lowest_part_y(h, refs, 8, 8, 8,
111 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
112 } else if (IS_8X16(mb_type)) {
113 get_lowest_part_y(h, refs, 0, 16, 0,
114 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
115 get_lowest_part_y(h, refs, 4, 16, 0,
116 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
120 av_assert2(IS_8X8(mb_type));
122 for (i = 0; i < 4; i++) {
123 const int sub_mb_type = h->sub_mb_type[i];
125 int y_offset = (i & 2) << 2;
127 if (IS_SUB_8X8(sub_mb_type)) {
128 get_lowest_part_y(h, refs, n, 8, y_offset,
129 IS_DIR(sub_mb_type, 0, 0),
130 IS_DIR(sub_mb_type, 0, 1),
132 } else if (IS_SUB_8X4(sub_mb_type)) {
133 get_lowest_part_y(h, refs, n, 4, y_offset,
134 IS_DIR(sub_mb_type, 0, 0),
135 IS_DIR(sub_mb_type, 0, 1),
137 get_lowest_part_y(h, refs, n + 2, 4, y_offset + 4,
138 IS_DIR(sub_mb_type, 0, 0),
139 IS_DIR(sub_mb_type, 0, 1),
141 } else if (IS_SUB_4X8(sub_mb_type)) {
142 get_lowest_part_y(h, refs, n, 8, y_offset,
143 IS_DIR(sub_mb_type, 0, 0),
144 IS_DIR(sub_mb_type, 0, 1),
146 get_lowest_part_y(h, refs, n + 1, 8, y_offset,
147 IS_DIR(sub_mb_type, 0, 0),
148 IS_DIR(sub_mb_type, 0, 1),
152 av_assert2(IS_SUB_4X4(sub_mb_type));
153 for (j = 0; j < 4; j++) {
154 int sub_y_offset = y_offset + 2 * (j & 2);
155 get_lowest_part_y(h, refs, n + j, 4, sub_y_offset,
156 IS_DIR(sub_mb_type, 0, 0),
157 IS_DIR(sub_mb_type, 0, 1),
164 for (list = h->list_count - 1; list >= 0; list--)
165 for (ref = 0; ref < 48 && nrefs[list]; ref++) {
166 int row = refs[list][ref];
168 H264Picture *ref_pic = &h->ref_list[list][ref];
169 int ref_field = ref_pic->reference - 1;
170 int ref_field_picture = ref_pic->field_picture;
171 int pic_height = 16 * h->mb_height >> ref_field_picture;
176 if (!FIELD_PICTURE(h) && ref_field_picture) { // frame referencing two fields
177 ff_thread_await_progress(&ref_pic->tf,
178 FFMIN((row >> 1) - !(row & 1),
181 ff_thread_await_progress(&ref_pic->tf,
182 FFMIN((row >> 1), pic_height - 1),
184 } else if (FIELD_PICTURE(h) && !ref_field_picture) { // field referencing one field of a frame
185 ff_thread_await_progress(&ref_pic->tf,
186 FFMIN(row * 2 + ref_field,
189 } else if (FIELD_PICTURE(h)) {
190 ff_thread_await_progress(&ref_pic->tf,
191 FFMIN(row, pic_height - 1),
194 ff_thread_await_progress(&ref_pic->tf,
195 FFMIN(row, pic_height - 1),
202 static av_always_inline void mc_dir_part(H264Context *h, H264Picture *pic,
203 int n, int square, int height,
205 uint8_t *dest_y, uint8_t *dest_cb,
207 int src_x_offset, int src_y_offset,
208 qpel_mc_func *qpix_op,
209 h264_chroma_mc_func chroma_op,
210 int pixel_shift, int chroma_idc)
212 const int mx = h->mv_cache[list][scan8[n]][0] + src_x_offset * 8;
213 int my = h->mv_cache[list][scan8[n]][1] + src_y_offset * 8;
214 const int luma_xy = (mx & 3) + ((my & 3) << 2);
215 ptrdiff_t offset = ((mx >> 2) << pixel_shift) + (my >> 2) * h->mb_linesize;
216 uint8_t *src_y = pic->f.data[0] + offset;
217 uint8_t *src_cb, *src_cr;
219 int extra_height = 0;
221 const int full_mx = mx >> 2;
222 const int full_my = my >> 2;
223 const int pic_width = 16 * h->mb_width;
224 const int pic_height = 16 * h->mb_height >> MB_FIELD(h);
232 if (full_mx < 0 - extra_width ||
233 full_my < 0 - extra_height ||
234 full_mx + 16 /*FIXME*/ > pic_width + extra_width ||
235 full_my + 16 /*FIXME*/ > pic_height + extra_height) {
236 h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
237 src_y - (2 << pixel_shift) - 2 * h->mb_linesize,
238 h->mb_linesize, h->mb_linesize,
239 16 + 5, 16 + 5 /*FIXME*/, full_mx - 2,
240 full_my - 2, pic_width, pic_height);
241 src_y = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
245 qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); // FIXME try variable height perhaps?
247 qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
249 if (CONFIG_GRAY && h->flags & CODEC_FLAG_GRAY)
252 if (chroma_idc == 3 /* yuv444 */) {
253 src_cb = pic->f.data[1] + offset;
255 h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
256 src_cb - (2 << pixel_shift) - 2 * h->mb_linesize,
257 h->mb_linesize, h->mb_linesize,
258 16 + 5, 16 + 5 /*FIXME*/,
259 full_mx - 2, full_my - 2,
260 pic_width, pic_height);
261 src_cb = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
263 qpix_op[luma_xy](dest_cb, src_cb, h->mb_linesize); // FIXME try variable height perhaps?
265 qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize);
267 src_cr = pic->f.data[2] + offset;
269 h->vdsp.emulated_edge_mc(h->edge_emu_buffer,
270 src_cr - (2 << pixel_shift) - 2 * h->mb_linesize,
271 h->mb_linesize, h->mb_linesize,
272 16 + 5, 16 + 5 /*FIXME*/,
273 full_mx - 2, full_my - 2,
274 pic_width, pic_height);
275 src_cr = h->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
277 qpix_op[luma_xy](dest_cr, src_cr, h->mb_linesize); // FIXME try variable height perhaps?
279 qpix_op[luma_xy](dest_cr + delta, src_cr + delta, h->mb_linesize);
283 ysh = 3 - (chroma_idc == 2 /* yuv422 */);
284 if (chroma_idc == 1 /* yuv420 */ && MB_FIELD(h)) {
285 // chroma offset when predicting from a field of opposite parity
286 my += 2 * ((h->mb_y & 1) - (pic->reference - 1));
287 emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
290 src_cb = pic->f.data[1] + ((mx >> 3) << pixel_shift) +
291 (my >> ysh) * h->mb_uvlinesize;
292 src_cr = pic->f.data[2] + ((mx >> 3) << pixel_shift) +
293 (my >> ysh) * h->mb_uvlinesize;
296 h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cb,
297 h->mb_uvlinesize, h->mb_uvlinesize,
298 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
299 pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
300 src_cb = h->edge_emu_buffer;
302 chroma_op(dest_cb, src_cb, h->mb_uvlinesize,
303 height >> (chroma_idc == 1 /* yuv420 */),
304 mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
307 h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cr,
308 h->mb_uvlinesize, h->mb_uvlinesize,
309 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
310 pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
311 src_cr = h->edge_emu_buffer;
313 chroma_op(dest_cr, src_cr, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
314 mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
317 static av_always_inline void mc_part_std(H264Context *h, int n, int square,
318 int height, int delta,
319 uint8_t *dest_y, uint8_t *dest_cb,
321 int x_offset, int y_offset,
322 qpel_mc_func *qpix_put,
323 h264_chroma_mc_func chroma_put,
324 qpel_mc_func *qpix_avg,
325 h264_chroma_mc_func chroma_avg,
326 int list0, int list1,
327 int pixel_shift, int chroma_idc)
329 qpel_mc_func *qpix_op = qpix_put;
330 h264_chroma_mc_func chroma_op = chroma_put;
332 dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
333 if (chroma_idc == 3 /* yuv444 */) {
334 dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
335 dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
336 } else if (chroma_idc == 2 /* yuv422 */) {
337 dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
338 dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
339 } else { /* yuv420 */
340 dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
341 dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
343 x_offset += 8 * h->mb_x;
344 y_offset += 8 * (h->mb_y >> MB_FIELD(h));
347 H264Picture *ref = &h->ref_list[0][h->ref_cache[0][scan8[n]]];
348 mc_dir_part(h, ref, n, square, height, delta, 0,
349 dest_y, dest_cb, dest_cr, x_offset, y_offset,
350 qpix_op, chroma_op, pixel_shift, chroma_idc);
353 chroma_op = chroma_avg;
357 H264Picture *ref = &h->ref_list[1][h->ref_cache[1][scan8[n]]];
358 mc_dir_part(h, ref, n, square, height, delta, 1,
359 dest_y, dest_cb, dest_cr, x_offset, y_offset,
360 qpix_op, chroma_op, pixel_shift, chroma_idc);
364 static av_always_inline void mc_part_weighted(H264Context *h, int n, int square,
365 int height, int delta,
366 uint8_t *dest_y, uint8_t *dest_cb,
368 int x_offset, int y_offset,
369 qpel_mc_func *qpix_put,
370 h264_chroma_mc_func chroma_put,
371 h264_weight_func luma_weight_op,
372 h264_weight_func chroma_weight_op,
373 h264_biweight_func luma_weight_avg,
374 h264_biweight_func chroma_weight_avg,
375 int list0, int list1,
376 int pixel_shift, int chroma_idc)
380 dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
381 if (chroma_idc == 3 /* yuv444 */) {
382 chroma_height = height;
383 chroma_weight_avg = luma_weight_avg;
384 chroma_weight_op = luma_weight_op;
385 dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
386 dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
387 } else if (chroma_idc == 2 /* yuv422 */) {
388 chroma_height = height;
389 dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
390 dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
391 } else { /* yuv420 */
392 chroma_height = height >> 1;
393 dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
394 dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
396 x_offset += 8 * h->mb_x;
397 y_offset += 8 * (h->mb_y >> MB_FIELD(h));
399 if (list0 && list1) {
400 /* don't optimize for luma-only case, since B-frames usually
401 * use implicit weights => chroma too. */
402 uint8_t *tmp_cb = h->bipred_scratchpad;
403 uint8_t *tmp_cr = h->bipred_scratchpad + (16 << pixel_shift);
404 uint8_t *tmp_y = h->bipred_scratchpad + 16 * h->mb_uvlinesize;
405 int refn0 = h->ref_cache[0][scan8[n]];
406 int refn1 = h->ref_cache[1][scan8[n]];
408 mc_dir_part(h, &h->ref_list[0][refn0], n, square, height, delta, 0,
409 dest_y, dest_cb, dest_cr,
410 x_offset, y_offset, qpix_put, chroma_put,
411 pixel_shift, chroma_idc);
412 mc_dir_part(h, &h->ref_list[1][refn1], n, square, height, delta, 1,
413 tmp_y, tmp_cb, tmp_cr,
414 x_offset, y_offset, qpix_put, chroma_put,
415 pixel_shift, chroma_idc);
417 if (h->use_weight == 2) {
418 int weight0 = h->implicit_weight[refn0][refn1][h->mb_y & 1];
419 int weight1 = 64 - weight0;
420 luma_weight_avg(dest_y, tmp_y, h->mb_linesize,
421 height, 5, weight0, weight1, 0);
422 chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize,
423 chroma_height, 5, weight0, weight1, 0);
424 chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize,
425 chroma_height, 5, weight0, weight1, 0);
427 luma_weight_avg(dest_y, tmp_y, h->mb_linesize, height,
428 h->luma_log2_weight_denom,
429 h->luma_weight[refn0][0][0],
430 h->luma_weight[refn1][1][0],
431 h->luma_weight[refn0][0][1] +
432 h->luma_weight[refn1][1][1]);
433 chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, chroma_height,
434 h->chroma_log2_weight_denom,
435 h->chroma_weight[refn0][0][0][0],
436 h->chroma_weight[refn1][1][0][0],
437 h->chroma_weight[refn0][0][0][1] +
438 h->chroma_weight[refn1][1][0][1]);
439 chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, chroma_height,
440 h->chroma_log2_weight_denom,
441 h->chroma_weight[refn0][0][1][0],
442 h->chroma_weight[refn1][1][1][0],
443 h->chroma_weight[refn0][0][1][1] +
444 h->chroma_weight[refn1][1][1][1]);
447 int list = list1 ? 1 : 0;
448 int refn = h->ref_cache[list][scan8[n]];
449 H264Picture *ref = &h->ref_list[list][refn];
450 mc_dir_part(h, ref, n, square, height, delta, list,
451 dest_y, dest_cb, dest_cr, x_offset, y_offset,
452 qpix_put, chroma_put, pixel_shift, chroma_idc);
454 luma_weight_op(dest_y, h->mb_linesize, height,
455 h->luma_log2_weight_denom,
456 h->luma_weight[refn][list][0],
457 h->luma_weight[refn][list][1]);
458 if (h->use_weight_chroma) {
459 chroma_weight_op(dest_cb, h->mb_uvlinesize, chroma_height,
460 h->chroma_log2_weight_denom,
461 h->chroma_weight[refn][list][0][0],
462 h->chroma_weight[refn][list][0][1]);
463 chroma_weight_op(dest_cr, h->mb_uvlinesize, chroma_height,
464 h->chroma_log2_weight_denom,
465 h->chroma_weight[refn][list][1][0],
466 h->chroma_weight[refn][list][1][1]);
471 static av_always_inline void prefetch_motion(H264Context *h, int list,
472 int pixel_shift, int chroma_idc)
474 /* fetch pixels for estimated mv 4 macroblocks ahead
475 * optimized for 64byte cache lines */
476 const int refn = h->ref_cache[list][scan8[0]];
478 const int mx = (h->mv_cache[list][scan8[0]][0] >> 2) + 16 * h->mb_x + 8;
479 const int my = (h->mv_cache[list][scan8[0]][1] >> 2) + 16 * h->mb_y;
480 uint8_t **src = h->ref_list[list][refn].f.data;
481 int off = (mx << pixel_shift) +
482 (my + (h->mb_x & 3) * 4) * h->mb_linesize +
484 h->vdsp.prefetch(src[0] + off, h->linesize, 4);
485 if (chroma_idc == 3 /* yuv444 */) {
486 h->vdsp.prefetch(src[1] + off, h->linesize, 4);
487 h->vdsp.prefetch(src[2] + off, h->linesize, 4);
489 off= (((mx>>1)+64)<<pixel_shift) + ((my>>1) + (h->mb_x&7))*h->uvlinesize;
490 h->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
495 static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
496 uint8_t *src_cb, uint8_t *src_cr,
497 int linesize, int uvlinesize,
498 int xchg, int chroma444,
499 int simple, int pixel_shift)
504 uint8_t *top_border_m1;
507 if (!simple && FRAME_MBAFF(h)) {
512 top_idx = MB_MBAFF(h) ? 0 : 1;
516 if (h->deblocking_filter == 2) {
517 deblock_topleft = h->slice_table[h->mb_xy - 1 - h->mb_stride] == h->slice_num;
518 deblock_top = h->top_type;
520 deblock_topleft = (h->mb_x > 0);
521 deblock_top = (h->mb_y > !!MB_FIELD(h));
524 src_y -= linesize + 1 + pixel_shift;
525 src_cb -= uvlinesize + 1 + pixel_shift;
526 src_cr -= uvlinesize + 1 + pixel_shift;
528 top_border_m1 = h->top_borders[top_idx][h->mb_x - 1];
529 top_border = h->top_borders[top_idx][h->mb_x];
531 #define XCHG(a, b, xchg) \
534 AV_SWAP64(b + 0, a + 0); \
535 AV_SWAP64(b + 8, a + 8); \
545 if (deblock_topleft) {
546 XCHG(top_border_m1 + (8 << pixel_shift),
547 src_y - (7 << pixel_shift), 1);
549 XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
550 XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
551 if (h->mb_x + 1 < h->mb_width) {
552 XCHG(h->top_borders[top_idx][h->mb_x + 1],
553 src_y + (17 << pixel_shift), 1);
555 if (simple || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
557 if (deblock_topleft) {
558 XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
559 XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
561 XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
562 XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
563 XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
564 XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
565 if (h->mb_x + 1 < h->mb_width) {
566 XCHG(h->top_borders[top_idx][h->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
567 XCHG(h->top_borders[top_idx][h->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
570 if (deblock_topleft) {
571 XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
572 XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
574 XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
575 XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
581 static av_always_inline int dctcoef_get(int16_t *mb, int high_bit_depth,
584 if (high_bit_depth) {
585 return AV_RN32A(((int32_t *)mb) + index);
587 return AV_RN16A(mb + index);
590 static av_always_inline void dctcoef_set(int16_t *mb, int high_bit_depth,
591 int index, int value)
593 if (high_bit_depth) {
594 AV_WN32A(((int32_t *)mb) + index, value);
596 AV_WN16A(mb + index, value);
599 static av_always_inline void hl_decode_mb_predict_luma(H264Context *h,
600 int mb_type, int is_h264,
602 int transform_bypass,
606 uint8_t *dest_y, int p)
608 void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
609 void (*idct_dc_add)(uint8_t *dst, int16_t *block, int stride);
611 int qscale = p == 0 ? h->qscale : h->chroma_qp[p - 1];
612 block_offset += 16 * p;
613 if (IS_INTRA4x4(mb_type)) {
614 if (IS_8x8DCT(mb_type)) {
615 if (transform_bypass) {
617 idct_add = h->h264dsp.h264_add_pixels8_clear;
619 idct_dc_add = h->h264dsp.h264_idct8_dc_add;
620 idct_add = h->h264dsp.h264_idct8_add;
622 for (i = 0; i < 16; i += 4) {
623 uint8_t *const ptr = dest_y + block_offset[i];
624 const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
625 if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
626 if (h->x264_build != -1) {
627 h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
629 h->hpc.pred8x8l_filter_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift),
630 (h-> topleft_samples_available << i) & 0x8000,
631 (h->topright_samples_available << i) & 0x4000, linesize);
633 const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];
634 h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,
635 (h->topright_samples_available << i) & 0x4000, linesize);
637 if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
638 idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
640 idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
645 if (transform_bypass) {
647 idct_add = h->h264dsp.h264_add_pixels4_clear;
649 idct_dc_add = h->h264dsp.h264_idct_dc_add;
650 idct_add = h->h264dsp.h264_idct_add;
652 for (i = 0; i < 16; i++) {
653 uint8_t *const ptr = dest_y + block_offset[i];
654 const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
656 if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
657 h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
662 if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
663 const int topright_avail = (h->topright_samples_available << i) & 0x8000;
664 av_assert2(h->mb_y || linesize <= block_offset[i]);
665 if (!topright_avail) {
667 tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
668 topright = (uint8_t *)&tr_high;
670 tr = ptr[3 - linesize] * 0x01010101u;
671 topright = (uint8_t *)&tr;
674 topright = ptr + (4 << pixel_shift) - linesize;
678 h->hpc.pred4x4[dir](ptr, topright, linesize);
679 nnz = h->non_zero_count_cache[scan8[i + p * 16]];
682 if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
683 idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
685 idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
686 } else if (CONFIG_SVQ3_DECODER)
687 ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);
693 h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);
695 if (h->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {
696 if (!transform_bypass)
697 h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),
699 h->dequant4_coeff[p][qscale][0]);
701 static const uint8_t dc_mapping[16] = {
702 0 * 16, 1 * 16, 4 * 16, 5 * 16,
703 2 * 16, 3 * 16, 6 * 16, 7 * 16,
704 8 * 16, 9 * 16, 12 * 16, 13 * 16,
705 10 * 16, 11 * 16, 14 * 16, 15 * 16
707 for (i = 0; i < 16; i++)
708 dctcoef_set(h->mb + (p * 256 << pixel_shift),
709 pixel_shift, dc_mapping[i],
710 dctcoef_get(h->mb_luma_dc[p],
714 } else if (CONFIG_SVQ3_DECODER)
715 ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,
716 h->mb_luma_dc[p], qscale);
720 static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type,
721 int is_h264, int simple,
722 int transform_bypass,
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)) {
733 if (IS_INTRA16x16(mb_type)) {
734 if (transform_bypass) {
735 if (h->sps.profile_idc == 244 &&
736 (h->intra16x16_pred_mode == VERT_PRED8x8 ||
737 h->intra16x16_pred_mode == HOR_PRED8x8)) {
738 h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset,
739 h->mb + (p * 256 << pixel_shift),
742 for (i = 0; i < 16; i++)
743 if (h->non_zero_count_cache[scan8[i + p * 16]] ||
744 dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
745 h->h264dsp.h264_add_pixels4_clear(dest_y + block_offset[i],
746 h->mb + (i * 16 + p * 256 << pixel_shift),
750 h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
751 h->mb + (p * 256 << pixel_shift),
753 h->non_zero_count_cache + p * 5 * 8);
755 } else if (h->cbp & 15) {
756 if (transform_bypass) {
757 const int di = IS_8x8DCT(mb_type) ? 4 : 1;
758 idct_add = IS_8x8DCT(mb_type) ? h->h264dsp.h264_add_pixels8_clear
759 : h->h264dsp.h264_add_pixels4_clear;
760 for (i = 0; i < 16; i += di)
761 if (h->non_zero_count_cache[scan8[i + p * 16]])
762 idct_add(dest_y + block_offset[i],
763 h->mb + (i * 16 + p * 256 << pixel_shift),
766 if (IS_8x8DCT(mb_type))
767 h->h264dsp.h264_idct8_add4(dest_y, block_offset,
768 h->mb + (p * 256 << pixel_shift),
770 h->non_zero_count_cache + p * 5 * 8);
772 h->h264dsp.h264_idct_add16(dest_y, block_offset,
773 h->mb + (p * 256 << pixel_shift),
775 h->non_zero_count_cache + p * 5 * 8);
778 } else if (CONFIG_SVQ3_DECODER) {
779 for (i = 0; i < 16; i++)
780 if (h->non_zero_count_cache[scan8[i + p * 16]] || h->mb[i * 16 + p * 256]) {
781 // FIXME benchmark weird rule, & below
782 uint8_t *const ptr = dest_y + block_offset[i];
783 ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize,
784 h->qscale, IS_INTRA(mb_type) ? 1 : 0);
792 #include "h264_mb_template.c"
796 #include "h264_mb_template.c"
800 #include "h264_mb_template.c"
802 void ff_h264_hl_decode_mb(H264Context *h)
804 const int mb_xy = h->mb_xy;
805 const int mb_type = h->cur_pic.mb_type[mb_xy];
806 int is_complex = CONFIG_SMALL || h->is_complex ||
807 IS_INTRA_PCM(mb_type) || h->qscale == 0;
810 if (is_complex || h->pixel_shift)
811 hl_decode_mb_444_complex(h);
813 hl_decode_mb_444_simple_8(h);
814 } else if (is_complex) {
815 hl_decode_mb_complex(h);
816 } else if (h->pixel_shift) {
817 hl_decode_mb_simple_16(h);
819 hl_decode_mb_simple_8(h);