1 /*
2 * H.26L/H.264/AVC/JVT/14496-10/... direct mb/block decoding
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 / MPEG-4 part10 direct mb/block decoding.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
34
35 #include <assert.h>
36
38 int poc,
int poc1,
int i)
39 {
43
44 if (pocdiff != (int)pocdiff)
46
48 return 256;
49 } else {
52 int tx = (16384 + (
FFABS(td) >> 1)) / td;
53
54 if (pocdiff0 != (int)pocdiff0)
56
58 }
59 }
60
63 {
65 :
h->cur_pic_ptr->poc;
68
71 const int poc =
h->cur_pic_ptr->field_poc[
field];
76 }
77
80 }
81
84 int field,
int colfield,
int mbafi)
85 {
87 int j, old_ref, rfield;
88 int start = mbafi ? 16 : 0;
90 int interl = mbafi ||
h->picture_structure !=
PICT_FRAME;
91
92 /* bogus; fills in for missing frames */
94
95 for (rfield = 0; rfield < 2; rfield++) {
96 for (old_ref = 0; old_ref < ref1->
ref_count[colfield][
list]; old_ref++) {
98
99 if (!interl)
100 poc |= 3;
101 // FIXME: store all MBAFF references so this is not needed
102 else if (interl && (poc & 3) == 3)
103 poc = (poc & ~3) + rfield + 1;
104
105 for (j = start; j < end; j++) {
108 int cur_ref = mbafi ? (j - 16) ^
field : j;
110 map[
list][2 * old_ref + (rfield ^
field) + 16] = cur_ref;
111 if (rfield ==
field || !interl)
113 break;
114 }
115 }
116 }
117 }
118 }
119
121 {
125 int sidx = (
h->picture_structure & 1) ^ 1;
126 int ref1sidx = (ref1->
reference & 1) ^ 1;
127
133 }
134
138 }
139
140 if (
h->current_slice == 0) {
142 } else {
144 }
145
147
149 return;
150
152 int cur_poc =
h->cur_pic_ptr->poc;
154 if (col_poc[0] == INT_MAX && col_poc[1] == INT_MAX) {
157 } else
160 ref1sidx =
162 // FL -> FL & differ parity
166 }
167
169 return;
170
177 }
178 }
179
181 int mb_y)
182 {
183 int ref_field =
ref->reference - 1;
184 int ref_field_picture =
ref->parent->field_picture;
185 int ref_height = 16 *
h->mb_height >> ref_field_picture;
186
188 return;
189
190 /* FIXME: It can be safe to access mb stuff
191 * even if pixels aren't deblocked yet. */
192
194 FFMIN(16 * mb_y >> ref_field_picture,
195 ref_height - 1),
196 ref_field_picture && ref_field);
197 }
198
200 int *mb_type)
201 {
202 int b8_stride = 2;
203 int b4_stride =
h->b_stride;
205 int mb_type_col[2];
206 const int16_t (*l1mv0)[2], (*l1mv1)[2];
207 const int8_t *l1ref0, *l1ref1;
208 const int is_b8x8 =
IS_8X8(*mb_type);
210 int i8, i4;
214
216
219
220 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16 | MB_TYPE_INTRA4x4 | \
221 MB_TYPE_INTRA16x16 | MB_TYPE_INTRA_PCM)
222
223 /* ref = min(neighbors) */
232 }
234 (unsigned)top_ref,
235 (unsigned)refc);
237 /* This is just pred_motion() but with the cases removed that
238 * cannot happen for direct blocks. */
241
242 int match_count = (left_ref ==
ref[
list]) +
245
246 if (match_count > 1) { // most common
249 } else {
250 assert(match_count == 1);
255 else
257 }
259 } else {
263 if (!is_b8x8)
266 }
267 }
268 if (
ref[0] < 0 &&
ref[1] < 0) {
270 if (!is_b8x8)
273 }
274
275 if (!(is_b8x8 |
mv[0] |
mv[1])) {
283 return;
284 }
285
291 b8_stride = 0;
292 } else {
294 mb_xy +=
h->mb_stride * sl->
col_fieldoff;
// non-zero for FL -> FL & differ parity
295 }
296 goto single_col;
297 } else { // AFL/AFR/FR/FL -> AFR/FR
299 mb_y = sl->
mb_y & ~1;
300 mb_xy = (sl->
mb_y & ~1) *
h->mb_stride + sl->
mb_x;
303 b8_stride = 2 + 4 *
h->mb_stride;
304 b4_stride *= 6;
309 }
310
314 !is_b8x8) {
316 } else {
318 }
319 } else { // AFR/FR -> AFR/FR
320 single_col:
321 mb_type_col[0] =
323
327 } else if (!is_b8x8 &&
331 } else {
332 if (!
h->ps.sps->direct_8x8_inference_flag) {
333 /* FIXME: Save sub mb types from previous frames (or derive
334 * from MVs) so we know exactly what block size to use. */
336 }
338 }
339 }
340 }
341
343
348 if (!b8_stride) {
350 l1ref0 += 2;
351 l1ref1 += 2;
352 l1mv0 += 2 * b4_stride;
353 l1mv1 += 2 * b4_stride;
354 }
355 }
356
358 int n = 0;
359 for (i8 = 0; i8 < 4; i8++) {
360 int x8 = i8 & 1;
361 int y8 = i8 >> 1;
362 int xy8 = x8 + y8 * b8_stride;
363 int xy4 = x8 * 3 + y8 * b4_stride;
365
367 continue;
369
375 ((l1ref0[xy8] == 0 &&
376 FFABS(l1mv0[xy4][0]) <= 1 &&
377 FFABS(l1mv0[xy4][1]) <= 1) ||
378 (l1ref0[xy8] < 0 &&
379 l1ref1[xy8] == 0 &&
380 FFABS(l1mv1[xy4][0]) <= 1 &&
381 FFABS(l1mv1[xy4][1]) <= 1))) {
388 n++;
389 } else {
392 }
395 }
396 if (!is_b8x8 && !(n & 3))
402
406 ((l1ref0[0] == 0 &&
407 FFABS(l1mv0[0][0]) <= 1 &&
408 FFABS(l1mv0[0][1]) <= 1) ||
409 (l1ref0[0] < 0 && !l1ref1[0] &&
410 FFABS(l1mv1[0][0]) <= 1 &&
411 FFABS(l1mv1[0][1]) <= 1 &&
412 h->x264_build > 33
U))) {
418 } else {
421 }
424 } else {
425 int n = 0;
426 for (i8 = 0; i8 < 4; i8++) {
427 const int x8 = i8 & 1;
428 const int y8 = i8 >> 1;
429
431 continue;
433
440
441 assert(b8_stride == 2);
442 /* col_zero_flag */
444 (l1ref0[i8] == 0 ||
445 (l1ref0[i8] < 0 &&
446 l1ref1[i8] == 0 &&
447 h->x264_build > 33
U))) {
448 const int16_t (*l1mv)[2] = l1ref0[i8] == 0 ? l1mv0 : l1mv1;
450 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
451 if (
FFABS(mv_col[0]) <= 1 &&
FFABS(mv_col[1]) <= 1) {
454 8, 0, 4);
457 8, 0, 4);
458 n += 4;
459 }
460 } else {
461 int m = 0;
462 for (i4 = 0; i4 < 4; i4++) {
463 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
464 (y8 * 2 + (i4 >> 1)) * b4_stride];
465 if (
FFABS(mv_col[0]) <= 1 &&
FFABS(mv_col[1]) <= 1) {
470 m++;
471 }
472 }
473 if (!(m & 3))
475 n += m;
476 }
477 }
478 }
479 if (!is_b8x8 && !(n & 15))
483 }
484 }
485
487 int *mb_type)
488 {
489 int b8_stride = 2;
490 int b4_stride =
h->b_stride;
492 int mb_type_col[2];
493 const int16_t (*l1mv0)[2], (*l1mv1)[2];
494 const int8_t *l1ref0, *l1ref1;
495 const int is_b8x8 =
IS_8X8(*mb_type);
496 unsigned int sub_mb_type;
497 int i8, i4;
498
500
503
509 b8_stride = 0;
510 } else {
512 mb_xy +=
h->mb_stride * sl->
col_fieldoff;
// non-zero for FL -> FL & differ parity
513 }
514 goto single_col;
515 } else { // AFL/AFR/FR/FL -> AFR/FR
517 mb_y = sl->
mb_y & ~1;
518 mb_xy = sl->
mb_x + (sl->
mb_y & ~1) *
h->mb_stride;
521 b8_stride = 2 + 4 *
h->mb_stride;
522 b4_stride *= 6;
527 }
528
531
534 !is_b8x8) {
537 } else {
539 }
540 } else { // AFR/FR -> AFR/FR
541 single_col:
542 mb_type_col[0] =
544
550 } else if (!is_b8x8 &&
554 } else {
555 if (!
h->ps.sps->direct_8x8_inference_flag) {
556 /* FIXME: save sub mb types from previous frames (or derive
557 * from MVs) so we know exactly what block size to use */
560 }
562 }
563 }
564 }
565
567
572 if (!b8_stride) {
574 l1ref0 += 2;
575 l1ref1 += 2;
576 l1mv0 += 2 * b4_stride;
577 l1mv1 += 2 * b4_stride;
578 }
579 }
580
581 {
585 int ref_offset;
586
591 }
593
596 assert(
h->ps.sps->direct_8x8_inference_flag);
597
598 for (i8 = 0; i8 < 4; i8++) {
599 const int x8 = i8 & 1;
600 const int y8 = i8 >> 1;
602 const int16_t (*l1mv)[2] = l1mv0;
603
605 continue;
607
613 continue;
614 }
615
616 ref0 = l1ref0[x8 + y8 * b8_stride];
617 if (ref0 >= 0)
618 ref0 = map_col_to_list0[0][ref0 + ref_offset];
619 else {
620 ref0 = map_col_to_list0[1][l1ref1[x8 + y8 * b8_stride] +
621 ref_offset];
622 l1mv = l1mv1;
623 }
624 scale = dist_scale_factor[ref0];
626 ref0, 1);
627
628 {
629 const int16_t *mv_col = l1mv[x8 * 3 + y8 * b4_stride];
630 int my_col = (mv_col[1] * (1 << y_shift)) / 2;
631 int mx = (
scale * mv_col[0] + 128) >> 8;
632 int my = (
scale * my_col + 128) >> 8;
637 }
638 }
639 return;
640 }
641
642 /* one-to-one mv scaling */
643
646
650 } else {
651 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
652 : map_col_to_list0[1][l1ref1[0] + ref_offset];
653 const int scale = dist_scale_factor[ref0];
654 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
655 int mv_l0[2];
656 mv_l0[0] = (
scale * mv_col[0] + 128) >> 8;
657 mv_l0[1] = (
scale * mv_col[1] + 128) >> 8;
660 mv1 =
pack16to32(mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1]);
661 }
665 } else {
666 for (i8 = 0; i8 < 4; i8++) {
667 const int x8 = i8 & 1;
668 const int y8 = i8 >> 1;
670 const int16_t (*l1mv)[2] = l1mv0;
671
673 continue;
680 continue;
681 }
682
683 assert(b8_stride == 2);
684 ref0 = l1ref0[i8];
685 if (ref0 >= 0)
686 ref0 = map_col_to_list0[0][ref0 + ref_offset];
687 else {
688 ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
689 l1mv = l1mv1;
690 }
691 scale = dist_scale_factor[ref0];
692
694 ref0, 1);
696 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
697 int mx = (
scale * mv_col[0] + 128) >> 8;
698 int my = (
scale * mv_col[1] + 128) >> 8;
703 } else {
704 for (i4 = 0; i4 < 4; i4++) {
705 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
706 (y8 * 2 + (i4 >> 1)) * b4_stride];
708 mv_l0[0] = (
scale * mv_col[0] + 128) >> 8;
709 mv_l0[1] = (
scale * mv_col[1] + 128) >> 8;
712 mv_l0[1] - mv_col[1]));
713 }
714 }
715 }
716 }
717 }
718 }
719
721 int *mb_type)
722 {
725 else
727 }