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 /**
23 * @file
24 * H.264 / AVC / MPEG4 part10 macroblock decoding
25 */
26
27 #include <stdint.h>
28
29 #include "config.h"
30
38
40 int height,
int y_offset,
int list)
41 {
43 int filter_height_down = (raw_my & 3) ? 3 : 0;
44 int full_my = (raw_my >> 2) + y_offset;
45 int bottom = full_my + filter_height_down +
height;
46
48
49 return FFMAX(0, bottom);
50 }
51
53 int height,
int y_offset,
int list0,
54 int list1, int *nrefs)
55 {
56 int my;
57
59
60 if (list0) {
63
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.
70 if (refs[0][ref_n] < 0)
71 nrefs[0] += 1;
72 refs[0][ref_n] =
FFMAX(refs[0][ref_n], my);
73 }
74 }
75
76 if (list1) {
79
83 if (refs[1][ref_n] < 0)
84 nrefs[1] += 1;
85 refs[1][ref_n] =
FFMAX(refs[1][ref_n], my);
86 }
87 }
88 }
89
90 /**
91 * Wait until all reference frames are available for MC operations.
92 *
93 * @param h the H264 context
94 */
96 {
99 int16_t refs[2][48];
100 int nrefs[2] = { 0 };
101 int ref, list;
102
103 memset(refs, -1, sizeof(refs));
104
118 } else {
119 int i;
120
122
123 for (i = 0; i < 4; i++) {
126 int y_offset = (i & 2) << 2;
127
130 IS_DIR(sub_mb_type, 0, 0),
131 IS_DIR(sub_mb_type, 0, 1),
132 nrefs);
135 IS_DIR(sub_mb_type, 0, 0),
136 IS_DIR(sub_mb_type, 0, 1),
137 nrefs);
139 IS_DIR(sub_mb_type, 0, 0),
140 IS_DIR(sub_mb_type, 0, 1),
141 nrefs);
144 IS_DIR(sub_mb_type, 0, 0),
145 IS_DIR(sub_mb_type, 0, 1),
146 nrefs);
148 IS_DIR(sub_mb_type, 0, 0),
149 IS_DIR(sub_mb_type, 0, 1),
150 nrefs);
151 } else {
152 int j;
154 for (j = 0; j < 4; j++) {
155 int sub_y_offset = y_offset + 2 * (j & 2);
157 IS_DIR(sub_mb_type, 0, 0),
158 IS_DIR(sub_mb_type, 0, 1),
159 nrefs);
160 }
161 }
162 }
163 }
164
165 for (list = h->
list_count - 1; list >= 0; list--)
166 for (ref = 0; ref < 48 && nrefs[list]; ref++) {
167 int row = refs[list][ref];
168 if (row >= 0) {
172 int pic_height = 16 * h->
mb_height >> ref_field_picture;
173
175 nrefs[list]--;
176
177 if (!
FIELD_PICTURE(h) && ref_field_picture) {
// frame referencing two fields
179 FFMIN((row >> 1) - !(row & 1),
180 pic_height - 1),
181 1);
183 FFMIN((row >> 1), pic_height - 1),
184 0);
185 }
else if (
FIELD_PICTURE(h) && !ref_field_picture) {
// field referencing one field of a frame
187 FFMIN(row * 2 + ref_field,
188 pic_height - 1),
189 0);
192 FFMIN(row, pic_height - 1),
193 ref_field);
194 } else {
196 FFMIN(row, pic_height - 1),
197 0);
198 }
199 }
200 }
201 }
202
208 int src_x_offset, int src_y_offset,
212 {
215 const int luma_xy = (mx & 3) + ((my & 3) << 2);
219 int extra_width = 0;
220 int extra_height = 0;
221 int emu = 0;
222 const int full_mx = mx >> 2;
223 const int full_my = my >> 2;
224 const int pic_width = 16 * h->
mb_width;
226 int ysh;
227
228 if (mx & 7)
229 extra_width -= 3;
230 if (my & 7)
231 extra_height -= 3;
232
233 if (full_mx < 0 - extra_width ||
234 full_my < 0 - extra_height ||
235 full_mx + 16 /*FIXME*/ > pic_width + extra_width ||
236 full_my + 16 /*FIXME*/ > pic_height + extra_height) {
240 16 + 5, 16 + 5 /*FIXME*/, full_mx - 2,
241 full_my - 2, pic_width, pic_height);
243 emu = 1;
244 }
245
246 qpix_op[luma_xy](dest_y, src_y, h->
mb_linesize);
// FIXME try variable height perhaps?
247 if (!square)
249
251 return;
252
253 if (chroma_idc == 3 /* yuv444 */) {
255 if (emu) {
259 16 + 5, 16 + 5 /*FIXME*/,
260 full_mx - 2, full_my - 2,
261 pic_width, pic_height);
263 }
264 qpix_op[luma_xy](dest_cb, src_cb, h->
mb_linesize);
// FIXME try variable height perhaps?
265 if (!square)
267
269 if (emu) {
273 16 + 5, 16 + 5 /*FIXME*/,
274 full_mx - 2, full_my - 2,
275 pic_width, pic_height);
277 }
278 qpix_op[luma_xy](dest_cr, src_cr, h->
mb_linesize);
// FIXME try variable height perhaps?
279 if (!square)
281 return;
282 }
283
284 ysh = 3 - (chroma_idc == 2 /* yuv422 */);
285 if (chroma_idc == 1
/* yuv420 */ &&
MB_FIELD(h)) {
286 // chroma offset when predicting from a field of opposite parity
288 emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
289 }
290
291 src_cb = pic->
f.
data[1] + ((mx >> 3) << pixel_shift) +
295
296 if (emu) {
299 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
300 pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
302 }
304 height >> (chroma_idc == 1 /* yuv420 */),
305 mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
306
307 if (emu) {
310 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
311 pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
313 }
314 chroma_op(dest_cr, src_cr, h->
mb_uvlinesize, height >> (chroma_idc == 1
/* yuv420 */),
315 mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
316 }
317
322 int x_offset, int y_offset,
327 int list0, int list1,
329 {
332
334 if (chroma_idc == 3
/* yuv444 */) {
336 dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->
mb_linesize;
337 } else if (chroma_idc == 2 /* yuv422 */) {
339 dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->
mb_uvlinesize;
340 } else { /* yuv420 */
342 dest_cr += (x_offset << pixel_shift) + y_offset * h->
mb_uvlinesize;
343 }
344 x_offset += 8 * h->
mb_x;
346
347 if (list0) {
350 dest_y, dest_cb, dest_cr, x_offset, y_offset,
351 qpix_op, chroma_op, pixel_shift, chroma_idc);
352
353 qpix_op = qpix_avg;
354 chroma_op = chroma_avg;
355 }
356
357 if (list1) {
360 dest_y, dest_cb, dest_cr, x_offset, y_offset,
361 qpix_op, chroma_op, pixel_shift, chroma_idc);
362 }
363 }
364
369 int x_offset, int y_offset,
376 int list0, int list1,
378 {
379 int chroma_height;
380
382 if (chroma_idc == 3
/* yuv444 */) {
384 chroma_weight_avg = luma_weight_avg;
385 chroma_weight_op = luma_weight_op;
387 dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->
mb_linesize;
388 } else if (chroma_idc == 2 /* yuv422 */) {
391 dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->
mb_uvlinesize;
392 } else { /* yuv420 */
393 chroma_height = height >> 1;
395 dest_cr += (x_offset << pixel_shift) + y_offset * h->
mb_uvlinesize;
396 }
397 x_offset += 8 * h->
mb_x;
399
400 if (list0 && list1) {
401 /* don't optimize for luma-only case, since B-frames usually
402 * use implicit weights => chroma too. */
408
410 dest_y, dest_cb, dest_cr,
411 x_offset, y_offset, qpix_put, chroma_put,
412 pixel_shift, chroma_idc);
414 tmp_y, tmp_cb, tmp_cr,
415 x_offset, y_offset, qpix_put, chroma_put,
416 pixel_shift, chroma_idc);
417
420 int weight1 = 64 - weight0;
422 height, 5, weight0, weight1, 0);
425 chroma_height, 5, weight0, weight1, 0);
427 chroma_height, 5, weight0, weight1, 0);
428 }
429 } else {
430 luma_weight_avg(dest_y, tmp_y, h->
mb_linesize, height,
437 chroma_weight_avg(dest_cb, tmp_cb, h->
mb_uvlinesize, chroma_height,
443 chroma_weight_avg(dest_cr, tmp_cr, h->
mb_uvlinesize, chroma_height,
449 }
450 }
451 } else {
452 int list = list1 ? 1 : 0;
455 mc_dir_part(h, ref, n, square, height, delta, list,
456 dest_y, dest_cb, dest_cr, x_offset, y_offset,
457 qpix_put, chroma_put, pixel_shift, chroma_idc);
458
473 }
474 }
475 }
476 }
477
480 {
481 /* fetch pixels for estimated mv 4 macroblocks ahead
482 * optimized for 64byte cache lines */
484 if (refn >= 0) {
490 (64 << pixel_shift);
492 if (chroma_idc == 3 /* yuv444 */) {
495 } else {
498 }
499 }
500 }
501
505 int xchg, int chroma444,
507 {
508 int deblock_topleft;
509 int deblock_top;
510 int top_idx = 1;
513
517 return;
518 } else {
520 }
521 }
522
526 } else {
527 deblock_topleft = (h->
mb_x > 0);
529 }
530
534
537
538 #define XCHG(a, b, xchg) \
539 if (pixel_shift) { \
540 if (xchg) { \
541 AV_SWAP64(b + 0, a + 0); \
542 AV_SWAP64(b + 8, a + 8); \
543 } else { \
544 AV_COPY128(b, a); \
545 } \
546 } else if (xchg) \
547 AV_SWAP64(b, a); \
548 else \
549 AV_COPY64(b, a);
550
551 if (deblock_top) {
552 if (deblock_topleft) {
553 XCHG(top_border_m1 + (8 << pixel_shift),
554 src_y - (7 << pixel_shift), 1);
555 }
556 XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
557 XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
560 src_y + (17 << pixel_shift), 1);
561 }
563 if (chroma444) {
564 if (deblock_topleft) {
565 XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
566 XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
567 }
568 XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
569 XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
570 XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
571 XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
573 XCHG(h->
top_borders[top_idx][h->
mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
574 XCHG(h->
top_borders[top_idx][h->
mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
575 }
576 } else {
577 if (deblock_topleft) {
578 XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
579 XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
580 }
581 XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
582 XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
583 }
584 }
585 }
586 }
587
590 {
591 if (high_bit_depth) {
593 } else
595 }
596
599 {
600 if (high_bit_depth) {
602 } else
604 }
605
607 int mb_type, int is_h264,
608 int simple,
609 int transform_bypass,
614 {
617 int i;
619 block_offset += 16 * p;
622 if (transform_bypass) {
623 idct_dc_add =
625 } else {
628 }
629 for (i = 0; i < 16; i += 4) {
630 uint8_t *
const ptr = dest_y + block_offset[i];
635 } else
639 } else {
643 if (nnz) {
644 if (nnz == 1 &&
dctcoef_get(h->
mb, pixel_shift, i * 16 + p * 256))
645 idct_dc_add(ptr, h->
mb + (i * 16 + p * 256 << pixel_shift),
linesize);
646 else
647 idct_add(ptr, h->
mb + (i * 16 + p * 256 << pixel_shift), linesize);
648 }
649 }
650 }
651 } else {
652 if (transform_bypass) {
653 idct_dc_add =
655 } else {
658 }
659 for (i = 0; i < 16; i++) {
660 uint8_t *
const ptr = dest_y + block_offset[i];
662
665 } else {
667 int nnz, tr;
668 uint64_t tr_high;
672 if (!topright_avail) {
673 if (pixel_shift) {
674 tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
675 topright = (
uint8_t *)&tr_high;
676 } else {
679 }
680 } else
682 } else
684
687 if (nnz) {
688 if (is_h264) {
689 if (nnz == 1 &&
dctcoef_get(h->
mb, pixel_shift, i * 16 + p * 256))
690 idct_dc_add(ptr, h->
mb + (i * 16 + p * 256 << pixel_shift),
linesize);
691 else
692 idct_add(ptr, h->
mb + (i * 16 + p * 256 << pixel_shift), linesize);
693 } else if (CONFIG_SVQ3_DECODER)
695 }
696 }
697 }
698 }
699 } else {
701 if (is_h264) {
703 if (!transform_bypass)
707 else {
708 static const uint8_t dc_mapping[16] = {
709 0 * 16, 1 * 16, 4 * 16, 5 * 16,
710 2 * 16, 3 * 16, 6 * 16, 7 * 16,
711 8 * 16, 9 * 16, 12 * 16, 13 * 16,
712 10 * 16, 11 * 16, 14 * 16, 15 * 16
713 };
714 for (i = 0; i < 16; i++)
716 pixel_shift, dc_mapping[i],
718 pixel_shift, i));
719 }
720 }
721 } else if (CONFIG_SVQ3_DECODER)
724 }
725 }
726
728 int is_h264, int simple,
729 int transform_bypass,
734 {
736 int i;
737 block_offset += 16 * p;
739 if (is_h264) {
741 if (transform_bypass) {
747 linesize);
748 } else {
749 for (i = 0; i < 16; i++)
753 h->
mb + (i * 16 + p * 256 << pixel_shift),
754 linesize);
755 }
756 } else {
758 h->
mb + (p * 256 << pixel_shift),
759 linesize,
761 }
762 }
else if (h->
cbp & 15) {
763 if (transform_bypass) {
764 const int di =
IS_8x8DCT(mb_type) ? 4 : 1;
767 for (i = 0; i < 16; i += di)
769 idct_add(dest_y + block_offset[i],
770 h->
mb + (i * 16 + p * 256 << pixel_shift),
771 linesize);
772 } else {
775 h->
mb + (p * 256 << pixel_shift),
776 linesize,
778 else
780 h->
mb + (p * 256 << pixel_shift),
781 linesize,
783 }
784 }
785 } else if (CONFIG_SVQ3_DECODER) {
786 for (i = 0; i < 16; i++)
788 // FIXME benchmark weird rule, & below
789 uint8_t *
const ptr = dest_y + block_offset[i];
792 }
793 }
794 }
795 }
796
797 #define BITS 8
798 #define SIMPLE 1
800
801 #undef BITS
804
805 #undef SIMPLE
808
810 {
815
818 hl_decode_mb_444_complex(h);
819 else
820 hl_decode_mb_444_simple_8(h);
821 } else if (is_complex) {
822 hl_decode_mb_complex(h);
824 hl_decode_mb_simple_16(h);
825 } else
826 hl_decode_mb_simple_8(h);
827 }