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
35
36 #include <assert.h>
37
39 int poc, int poc1, int i)
40 {
42 int64_t pocdiff = poc1 - (int64_t)poc0;
43 int td = av_clip_int8(pocdiff);
44
45 if (pocdiff != (int)pocdiff)
47
49 return 256;
50 } else {
51 int64_t pocdiff0 = poc - (int64_t)poc0;
52 int tb = av_clip_int8(pocdiff0);
53 int tx = (16384 + (
FFABS(td) >> 1)) /
td;
54
55 if (pocdiff0 != (int)pocdiff0)
57
58 return av_clip_intp2((tb * tx + 32) >> 6, 10);
59 }
60 }
61
64 {
68 int i, field;
69
71 for (field = 0; field < 2; field++) {
74 for (i = 0; i < 2 * sl->
ref_count[0]; i++)
77 }
78
81 }
82
84 int map[2][16 + 32],
int list,
85 int field, int colfield, int mbafi)
86 {
88 int j, old_ref, rfield;
89 int start = mbafi ? 16 : 0;
92
93 /* bogus; fills in for missing frames */
94 memset(
map[list], 0,
sizeof(
map[list]));
95
96 for (rfield = 0; rfield < 2; rfield++) {
97 for (old_ref = 0; old_ref < ref1->
ref_count[colfield][list]; old_ref++) {
98 int poc = ref1->
ref_poc[colfield][list][old_ref];
99
100 if (!interl)
101 poc |= 3;
102 // FIXME: store all MBAFF references so this is not needed
103 else if (interl && (poc & 3) == 3)
104 poc = (poc & ~3) + rfield + 1;
105
106 for (j = start; j <
end; j++) {
109 int cur_ref = mbafi ? (j - 16) ^ field : j;
111 map[list][2 * old_ref + (rfield ^ field) + 16] = cur_ref;
112 if (rfield == field || !interl)
113 map[list][old_ref] = cur_ref;
114 break;
115 }
116 }
117 }
118 }
119 }
120
122 {
125 int list, j, field;
127 int ref1sidx = (ref1->
reference & 1) ^ 1;
128
129 for (list = 0; list < sl->
list_count; list++) {
131 for (j = 0; j < sl->
ref_count[list]; j++)
134 }
135
139 }
140
143 } else {
145 }
146
148
150 return;
151
155 if (col_poc[0] == INT_MAX && col_poc[1] == INT_MAX) {
158 } else
160 FFABS(col_poc[1] - cur_poc));
161 ref1sidx =
163 // FL -> FL & differ parity
167 }
168
170 return;
171
172 for (list = 0; list < 2; list++) {
175 for (field = 0; field < 2; field++)
177 field, 1);
178 }
179 }
180
182 int mb_y)
183 {
186 int ref_height = 16 * h->
mb_height >> ref_field_picture;
187
189 return;
190
191 /* FIXME: It can be safe to access mb stuff
192 * even if pixels aren't deblocked yet. */
193
195 FFMIN(16 * mb_y >> ref_field_picture,
196 ref_height - 1),
197 ref_field_picture && ref_field);
198 }
199
201 int *mb_type)
202 {
203 int b8_stride = 2;
206 int mb_type_col[2];
207 const int16_t (*l1mv0)[2], (*l1mv1)[2];
208 const int8_t *l1ref0, *l1ref1;
209 const int is_b8x8 =
IS_8X8(*mb_type);
211 int i8, i4;
214 int list;
215
217
220
221 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16 | MB_TYPE_INTRA4x4 | \
222 MB_TYPE_INTRA16x16 | MB_TYPE_INTRA_PCM)
223
224 /* ref = min(neighbors) */
225 for (list = 0; list < 2; list++) {
233 }
234 ref[list] =
FFMIN3((
unsigned)left_ref,
235 (unsigned)top_ref,
236 (unsigned)refc);
237 if (ref[list] >= 0) {
238 /* This is just pred_motion() but with the cases removed that
239 * cannot happen for direct blocks. */
242
243 int match_count = (left_ref == ref[list]) +
244 (top_ref == ref[list]) +
245 (refc == ref[list]);
246
247 if (match_count > 1) { // most common
250 } else {
251 assert(match_count == 1);
252 if (left_ref == ref[list])
254 else if (top_ref == ref[list])
256 else
258 }
260 } else {
262 mv[list] = 0;
263 ref[list] = -1;
264 if (!is_b8x8)
267 }
268 }
269 if (ref[0] < 0 && ref[1] < 0) {
270 ref[0] = ref[1] = 0;
271 if (!is_b8x8)
274 }
275
276 if (!(is_b8x8 | mv[0] | mv[1])) {
284 return;
285 }
286
292 b8_stride = 0;
293 } else {
296 }
297 goto single_col;
298 } else { // AFL/AFR/FR/FL -> AFR/FR
300 mb_y = sl->
mb_y & ~1;
305 b4_stride *= 6;
310 }
311
315 !is_b8x8) {
317 } else {
319 }
320 } else { // AFR/FR -> AFR/FR
321 single_col:
322 mb_type_col[0] =
324
328 } else if (!is_b8x8 &&
332 } else {
334 /* FIXME: Save sub mb types from previous frames (or derive
335 * from MVs) so we know exactly what block size to use. */
337 }
339 }
340 }
341 }
342
344
349 if (!b8_stride) {
351 l1ref0 += 2;
352 l1ref1 += 2;
353 l1mv0 += 2 * b4_stride;
354 l1mv1 += 2 * b4_stride;
355 }
356 }
357
360 for (i8 = 0; i8 < 4; i8++) {
361 int x8 = i8 & 1;
362 int y8 = i8 >> 1;
363 int xy8 = x8 + y8 * b8_stride;
364 int xy4 = x8 * 3 + y8 * b4_stride;
366
368 continue;
370
376 ((l1ref0[xy8] == 0 &&
377 FFABS(l1mv0[xy4][0]) <= 1 &&
378 FFABS(l1mv0[xy4][1]) <= 1) ||
379 (l1ref0[xy8] < 0 &&
380 l1ref1[xy8] == 0 &&
381 FFABS(l1mv1[xy4][0]) <= 1 &&
382 FFABS(l1mv1[xy4][1]) <= 1))) {
383 a =
384 b = 0;
385 if (ref[0] > 0)
386 a = mv[0];
387 if (ref[1] > 0)
388 b = mv[1];
389 n++;
390 } else {
391 a = mv[0];
392 b = mv[1];
393 }
396 }
397 if (!is_b8x8 && !(n & 3))
403
407 ((l1ref0[0] == 0 &&
408 FFABS(l1mv0[0][0]) <= 1 &&
409 FFABS(l1mv0[0][1]) <= 1) ||
410 (l1ref0[0] < 0 && !l1ref1[0] &&
411 FFABS(l1mv1[0][0]) <= 1 &&
412 FFABS(l1mv1[0][1]) <= 1 &&
414 a = b = 0;
415 if (ref[0] > 0)
416 a = mv[0];
417 if (ref[1] > 0)
418 b = mv[1];
419 } else {
420 a = mv[0];
421 b = mv[1];
422 }
425 } else {
427 for (i8 = 0; i8 < 4; i8++) {
428 const int x8 = i8 & 1;
429 const int y8 = i8 >> 1;
430
432 continue;
434
441
442 assert(b8_stride == 2);
443 /* col_zero_flag */
445 (l1ref0[i8] == 0 ||
446 (l1ref0[i8] < 0 &&
447 l1ref1[i8] == 0 &&
449 const int16_t (*l1mv)[2] = l1ref0[i8] == 0 ? l1mv0 : l1mv1;
451 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
452 if (
FFABS(mv_col[0]) <= 1 &&
FFABS(mv_col[1]) <= 1) {
453 if (ref[0] == 0)
455 8, 0, 4);
456 if (ref[1] == 0)
458 8, 0, 4);
459 n += 4;
460 }
461 } else {
462 int m = 0;
463 for (i4 = 0; i4 < 4; i4++) {
464 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
465 (y8 * 2 + (i4 >> 1)) * b4_stride];
466 if (
FFABS(mv_col[0]) <= 1 &&
FFABS(mv_col[1]) <= 1) {
467 if (ref[0] == 0)
469 if (ref[1] == 0)
471 m++;
472 }
473 }
474 if (!(m & 3))
476 n += m;
477 }
478 }
479 }
480 if (!is_b8x8 && !(n & 15))
484 }
485 }
486
488 int *mb_type)
489 {
490 int b8_stride = 2;
493 int mb_type_col[2];
494 const int16_t (*l1mv0)[2], (*l1mv1)[2];
495 const int8_t *l1ref0, *l1ref1;
496 const int is_b8x8 =
IS_8X8(*mb_type);
497 unsigned int sub_mb_type;
498 int i8, i4;
499
501
504
510 b8_stride = 0;
511 } else {
514 }
515 goto single_col;
516 } else { // AFL/AFR/FR/FL -> AFR/FR
518 mb_y = sl->
mb_y & ~1;
523 b4_stride *= 6;
528 }
529
532
535 !is_b8x8) {
538 } else {
540 }
541 } else { // AFR/FR -> AFR/FR
542 single_col:
543 mb_type_col[0] =
545
551 } else if (!is_b8x8 &&
555 } else {
557 /* FIXME: save sub mb types from previous frames (or derive
558 * from MVs) so we know exactly what block size to use */
561 }
563 }
564 }
565 }
566
568
573 if (!b8_stride) {
575 l1ref0 += 2;
576 l1ref1 += 2;
577 l1mv0 += 2 * b4_stride;
578 l1mv1 += 2 * b4_stride;
579 }
580 }
581
582 {
586 int ref_offset;
587
592 }
594
598
599 for (i8 = 0; i8 < 4; i8++) {
600 const int x8 = i8 & 1;
601 const int y8 = i8 >> 1;
602 int ref0, scale;
603 const int16_t (*l1mv)[2] = l1mv0;
604
606 continue;
608
614 continue;
615 }
616
617 ref0 = l1ref0[x8 + y8 * b8_stride];
618 if (ref0 >= 0)
619 ref0 = map_col_to_list0[0][ref0 + ref_offset];
620 else {
621 ref0 = map_col_to_list0[1][l1ref1[x8 + y8 * b8_stride] +
622 ref_offset];
623 l1mv = l1mv1;
624 }
625 scale = dist_scale_factor[ref0];
627 ref0, 1);
628
629 {
630 const int16_t *mv_col = l1mv[x8 * 3 + y8 * b4_stride];
631 int my_col = (mv_col[1] * (1 << y_shift)) / 2;
632 int mx = (scale * mv_col[0] + 128) >> 8;
633 int my = (scale * my_col + 128) >> 8;
638 }
639 }
640 return;
641 }
642
643 /* one-to-one mv scaling */
644
647
650 ref = mv0 = mv1 = 0;
651 } else {
652 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
653 : map_col_to_list0[1][l1ref1[0] + ref_offset];
654 const int scale = dist_scale_factor[ref0];
655 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
656 int mv_l0[2];
657 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
658 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
659 ref = ref0;
661 mv1 =
pack16to32(mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1]);
662 }
666 } else {
667 for (i8 = 0; i8 < 4; i8++) {
668 const int x8 = i8 & 1;
669 const int y8 = i8 >> 1;
670 int ref0, scale;
671 const int16_t (*l1mv)[2] = l1mv0;
672
674 continue;
681 continue;
682 }
683
684 assert(b8_stride == 2);
685 ref0 = l1ref0[i8];
686 if (ref0 >= 0)
687 ref0 = map_col_to_list0[0][ref0 + ref_offset];
688 else {
689 ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
690 l1mv = l1mv1;
691 }
692 scale = dist_scale_factor[ref0];
693
695 ref0, 1);
697 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
698 int mx = (scale * mv_col[0] + 128) >> 8;
699 int my = (scale * mv_col[1] + 128) >> 8;
703 pack16to32(mx - mv_col[0], my - mv_col[1]), 4);
704 } else {
705 for (i4 = 0; i4 < 4; i4++) {
706 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
707 (y8 * 2 + (i4 >> 1)) * b4_stride];
709 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
710 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
713 mv_l0[1] - mv_col[1]));
714 }
715 }
716 }
717 }
718 }
719 }
720
722 int *mb_type)
723 {
726 else
728 }
static void pred_spatial_direct_motion(const H264Context *const h, H264SliceContext *sl, int *mb_type)
struct H264Context * h264
int long_ref
1->long term reference 0->short term reference
int16_t mv_cache[2][5 *8][2]
Motion vector cache.
int dist_scale_factor[32]
int16_t(*[2] motion_val)[2]
void ff_h264_pred_direct_motion(const H264Context *const h, H264SliceContext *sl, int *mb_type)
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
#define av_assert0(cond)
assert() equivalent, that is always enabled.
uint16_t sub_mb_type[4]
as a DCT coefficient is int32_t in high depth, we need to reserve twice the space.
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
int field_picture
whether or not picture was encoded in separate fields
static av_cold int end(AVCodecContext *avctx)
Multithreading support functions.
int map_col_to_list0_field[2][2][16+32]
#define PICT_BOTTOM_FIELD
void ff_h264_direct_ref_list_init(const H264Context *const h, H264SliceContext *sl)
static void await_reference_mb_row(const H264Context *const h, H264Ref *ref, int mb_y)
H.264 parameter set handling.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static const uint16_t mask[17]
int map_col_to_list0[2][16+32]
int active_thread_type
Which multithreading methods are in use by the codec.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int ref_poc[2][2][32]
POCs of the frames/fields used as reference (FIXME need per slice)
int direct_spatial_mv_pred
int frame_num
frame_num (raw frame_num from slice header)
#define MB_TYPE_16x16_OR_INTRA
int slice_type_nos
S free slice type (SI/SP are remapped to I/P)
useful rectangle filling function
#define FF_THREAD_FRAME
Decode more than one frame at once.
static void pred_temp_direct_motion(const H264Context *const h, H264SliceContext *sl, int *mb_type)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
H.264 / AVC / MPEG-4 part10 codec.
int direct_8x8_inference_flag
static void fill_rectangle(int x, int y, int w, int h)
#define MB_TYPE_INTERLACED
static int get_scale_factor(H264SliceContext *sl, int poc, int poc1, int i)
static const int8_t mv[256][2]
Libavcodec external API header.
int field_poc[2]
top/bottom POC
static const uint8_t scan8[16 *3+3]
static av_always_inline uint32_t pack16to32(unsigned a, unsigned b)
H264Picture * cur_pic_ptr
const VDPAUPixFmtMap * map
static void fill_colmap(const H264Context *h, H264SliceContext *sl, int map[2][16+32], int list, int field, int colfield, int mbafi)
int mbaff
1 -> MBAFF frame 0-> not MBAFF
common internal api header.
static int ref[MAX_W *MAX_W]
int8_t ref_cache[2][5 *8]
#define PART_NOT_AVAILABLE
int current_slice
current slice number, used to initialize slice_num of each thread/context
H264Ref ref_list[2][48]
0..15: frame refs, 16..47: mbaff field refs.
int ref_count[2][2]
number of entries in ref_poc (FIXME need per slice)
int dist_scale_factor_field[2][32]
void ff_h264_direct_dist_scale_factor(const H264Context *const h, H264SliceContext *sl)