FFmpeg
Loading...
Searching...
No Matches
h264_direct.c
Go to the documentation of this file.
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
28#include "avcodec.h"
29#include "h264dec.h"
30#include "h264_ps.h"
31#include "mpegutils.h"
32#include "rectangle.h"
33#include "threadframe.h"
34
35#include <assert.h>
36
38 int poc, int poc1, int i)
39{
40 int poc0 = sl->ref_list[0][i].poc;
41 int64_t pocdiff = poc1 - (int64_t)poc0;
42 int td = av_clip_int8(pocdiff);
43
44 if (pocdiff != (int)pocdiff)
45 avpriv_request_sample(sl->h264->avctx, "pocdiff overflow");
46
47 if (td == 0 || sl->ref_list[0][i].parent->long_ref) {
48 return 256;
49 } else {
50 int64_t pocdiff0 = poc - (int64_t)poc0;
51 int tb = av_clip_int8(pocdiff0);
52 int tx = (16384 + (FFABS(td) >> 1)) / td;
53
54 if (pocdiff0 != (int)pocdiff0)
55 av_log(sl->h264->avctx, AV_LOG_DEBUG, "pocdiff0 overflow\n");
56
57 return av_clip_intp2((tb * tx + 32) >> 6, 10);
58 }
59}
60
63{
64 const int poc = FIELD_PICTURE(h) ? h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD]
65 : h->cur_pic_ptr->poc;
66 const int poc1 = sl->ref_list[1][0].poc;
67 int i, field;
68
69 if (FRAME_MBAFF(h))
70 for (field = 0; field < 2; field++) {
71 const int poc = h->cur_pic_ptr->field_poc[field];
72 const int poc1 = sl->ref_list[1][0].parent->field_poc[field];
73 for (i = 0; i < 2 * sl->ref_count[0]; i++)
74 sl->dist_scale_factor_field[field][i ^ field] =
75 get_scale_factor(sl, poc, poc1, i + 16);
76 }
77
78 for (i = 0; i < sl->ref_count[0]; i++)
79 sl->dist_scale_factor[i] = get_scale_factor(sl, poc, poc1, i);
80}
81
82static void fill_colmap(const H264Context *h, H264SliceContext *sl,
83 int map[2][16 + 32], int list,
84 int field, int colfield, int mbafi)
85{
86 const H264Picture *const ref1 = sl->ref_list[1][0].parent;
87 int j, old_ref, rfield;
88 int start = mbafi ? 16 : 0;
89 int end = mbafi ? 16 + 2 * sl->ref_count[0] : sl->ref_count[0];
90 int interl = mbafi || h->picture_structure != PICT_FRAME;
91
92 /* bogus; fills in for missing frames */
93 memset(map[list], 0, sizeof(map[list]));
94
95 for (rfield = 0; rfield < 2; rfield++) {
96 for (old_ref = 0; old_ref < ref1->ref_count[colfield][list]; old_ref++) {
97 int poc = ref1->ref_poc[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++) {
106 if (4 * sl->ref_list[0][j].parent->frame_num +
107 (sl->ref_list[0][j].reference & 3) == poc) {
108 int cur_ref = mbafi ? (j - 16) ^ field : j;
109 if (ref1->mbaff)
110 map[list][2 * old_ref + (rfield ^ field) + 16] = cur_ref;
111 if (rfield == field || !interl)
112 map[list][old_ref] = cur_ref;
113 break;
114 }
115 }
116 }
117 }
118}
119
121{
122 H264Ref *const ref1 = &sl->ref_list[1][0];
123 H264Picture *const cur = h->cur_pic_ptr;
124 int list, field;
125 int sidx = (h->picture_structure & 1) ^ 1;
126 int ref1sidx = (ref1->reference & 1) ^ 1;
127
128 /* Updates to cur_pic are not safe once ff_thread_finish_setup() has been
129 * called (other threads may already be reading these fields). */
130 if (!h->setup_finished) {
131 for (list = 0; list < sl->list_count; list++) {
132 cur->ref_count[sidx][list] = sl->ref_count[list];
133 for (int j = 0; j < sl->ref_count[list]; j++)
134 cur->ref_poc[sidx][list][j] = 4 * sl->ref_list[list][j].parent->frame_num +
135 (sl->ref_list[list][j].reference & 3);
136 }
137
138 if (h->picture_structure == PICT_FRAME) {
139 memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
140 memcpy(cur->ref_poc[1], cur->ref_poc[0], sizeof(cur->ref_poc[0]));
141 }
142
143 if (h->current_slice == 0) {
144 cur->mbaff = FRAME_MBAFF(h);
145 } else {
146 av_assert0(cur->mbaff == FRAME_MBAFF(h));
147 }
148 }
149
150 sl->col_fieldoff = 0;
151
152 if (sl->list_count != 2 || !sl->ref_count[1])
153 return;
154
155 if (h->picture_structure == PICT_FRAME) {
156 int cur_poc = h->cur_pic_ptr->poc;
157 const int *col_poc = sl->ref_list[1][0].parent->field_poc;
158 if (col_poc[0] == INT_MAX && col_poc[1] == INT_MAX) {
159 av_log(h->avctx, AV_LOG_ERROR, "co located POCs unavailable\n");
160 sl->col_parity = 1;
161 } else
162 sl->col_parity = (FFABS(col_poc[0] - (int64_t)cur_poc) >=
163 FFABS(col_poc[1] - (int64_t)cur_poc));
164 ref1sidx =
165 sidx = sl->col_parity;
166 // FL -> FL & differ parity
167 } else if (!(h->picture_structure & sl->ref_list[1][0].reference) &&
168 !sl->ref_list[1][0].parent->mbaff) {
169 sl->col_fieldoff = 2 * sl->ref_list[1][0].reference - 3;
170 }
171
173 return;
174
175 for (list = 0; list < 2; list++) {
176 fill_colmap(h, sl, sl->map_col_to_list0, list, sidx, ref1sidx, 0);
177 if (FRAME_MBAFF(h))
178 for (field = 0; field < 2; field++)
179 fill_colmap(h, sl, sl->map_col_to_list0_field[field], list, field,
180 field, 1);
181 }
182}
183
184static void await_reference_mb_row(const H264Context *const h, H264Ref *ref,
185 int mb_y)
186{
187 if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_FRAME))
188 return;
189
190 int ref_field = ref->reference - 1;
191 int ref_field_picture = ref->parent->field_picture;
192 int ref_height = 16 * h->mb_height >> ref_field_picture;
193 int row = FFMIN(16 * mb_y >> ref_field_picture, ref_height - 1);
194
195 /* FIXME: It can be safe to access mb stuff
196 * even if pixels aren't deblocked yet. */
197
198 ff_thread_await_progress(&ref->parent->tf, row,
199 ref_field_picture && ref_field);
200
201 /* A frame references a field pair as a whole, so the wait above covers
202 * its bottom field only, while the colocated data is read from the field
203 * selected by col_parity. The two are decoded by different threads. */
204 if (ref_field_picture && !FIELD_PICTURE(h))
205 ff_thread_await_progress(&ref->parent->tf, row, 0);
206}
207
209 int *mb_type)
210{
211 int b8_stride = 2;
212 int b4_stride = h->b_stride;
213 int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
214 int mb_type_col[2];
215 const int16_t (*l1mv0)[2], (*l1mv1)[2];
216 const int8_t *l1ref0, *l1ref1;
217 const int is_b8x8 = IS_8X8(*mb_type);
218 unsigned int sub_mb_type = MB_TYPE_L0L1;
219 int i8, i4;
220 int ref[2];
221 int mv[2];
222 int list;
223
224 assert(sl->ref_list[1][0].reference & 3);
225
227 sl->mb_y + !!IS_INTERLACED(*mb_type));
228
229#define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16 | MB_TYPE_INTRA4x4 | \
230 MB_TYPE_INTRA16x16 | MB_TYPE_INTRA_PCM)
231
232 /* ref = min(neighbors) */
233 for (list = 0; list < 2; list++) {
234 int left_ref = sl->ref_cache[list][scan8[0] - 1];
235 int top_ref = sl->ref_cache[list][scan8[0] - 8];
236 int refc = sl->ref_cache[list][scan8[0] - 8 + 4];
237 const int16_t *C = sl->mv_cache[list][scan8[0] - 8 + 4];
238 if (refc == PART_NOT_AVAILABLE) {
239 refc = sl->ref_cache[list][scan8[0] - 8 - 1];
240 C = sl->mv_cache[list][scan8[0] - 8 - 1];
241 }
242 ref[list] = FFMIN3((unsigned)left_ref,
243 (unsigned)top_ref,
244 (unsigned)refc);
245 if (ref[list] >= 0) {
246 /* This is just pred_motion() but with the cases removed that
247 * cannot happen for direct blocks. */
248 const int16_t *const A = sl->mv_cache[list][scan8[0] - 1];
249 const int16_t *const B = sl->mv_cache[list][scan8[0] - 8];
250
251 int match_count = (left_ref == ref[list]) +
252 (top_ref == ref[list]) +
253 (refc == ref[list]);
254
255 if (match_count > 1) { // most common
256 mv[list] = pack16to32(mid_pred(A[0], B[0], C[0]),
257 mid_pred(A[1], B[1], C[1]));
258 } else {
259 assert(match_count == 1);
260 if (left_ref == ref[list])
261 mv[list] = AV_RN32A(A);
262 else if (top_ref == ref[list])
263 mv[list] = AV_RN32A(B);
264 else
265 mv[list] = AV_RN32A(C);
266 }
267 av_assert2(ref[list] < (sl->ref_count[list] << !!FRAME_MBAFF(h)));
268 } else {
269 int mask = ~(MB_TYPE_L0 << (2 * list));
270 mv[list] = 0;
271 ref[list] = -1;
272 if (!is_b8x8)
273 *mb_type &= mask;
274 sub_mb_type &= mask;
275 }
276 }
277 if (ref[0] < 0 && ref[1] < 0) {
278 ref[0] = ref[1] = 0;
279 if (!is_b8x8)
280 *mb_type |= MB_TYPE_L0L1;
281 sub_mb_type |= MB_TYPE_L0L1;
282 }
283
284 if (!(is_b8x8 | mv[0] | mv[1])) {
285 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
286 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
287 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
288 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
289 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
292 return;
293 }
294
295 if (sl->ref_list[1][0].parent->field_picture ||
296 IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
297 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
298 mb_y = (sl->mb_y & ~1) + sl->col_parity;
299 mb_xy = sl->mb_x +
300 ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
301 b8_stride = 0;
302 } else {
303 mb_y += sl->col_fieldoff;
304 mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
305 }
306 goto single_col;
307 } else { // AFL/AFR/FR/FL -> AFR/FR
308 if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
309 mb_y = sl->mb_y & ~1;
310 mb_xy = (sl->mb_y & ~1) * h->mb_stride + sl->mb_x;
311 mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
312 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
313 b8_stride = 2 + 4 * h->mb_stride;
314 b4_stride *= 6;
315 if (IS_INTERLACED(mb_type_col[0]) !=
316 IS_INTERLACED(mb_type_col[1])) {
317 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
318 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
319 }
320
321 sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
322 if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
323 (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
324 !is_b8x8) {
325 *mb_type |= MB_TYPE_16x8 | MB_TYPE_DIRECT2; /* B_16x8 */
326 } else {
327 *mb_type |= MB_TYPE_8x8;
328 }
329 } else { // AFR/FR -> AFR/FR
330single_col:
331 mb_type_col[0] =
332 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
333
334 sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
335 if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
336 *mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_16x16 */
337 } else if (!is_b8x8 &&
338 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
339 *mb_type |= MB_TYPE_DIRECT2 |
340 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
341 } else {
342 if (!h->ps.sps->direct_8x8_inference_flag) {
343 /* FIXME: Save sub mb types from previous frames (or derive
344 * from MVs) so we know exactly what block size to use. */
345 sub_mb_type += (MB_TYPE_8x8 - MB_TYPE_16x16); /* B_SUB_4x4 */
346 }
347 *mb_type |= MB_TYPE_8x8;
348 }
349 }
350 }
351
352 await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
353
354 l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
355 l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
356 l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
357 l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
358 if (!b8_stride) {
359 if (sl->mb_y & 1) {
360 l1ref0 += 2;
361 l1ref1 += 2;
362 l1mv0 += 2 * b4_stride;
363 l1mv1 += 2 * b4_stride;
364 }
365 }
366
367 if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
368 int n = 0;
369 for (i8 = 0; i8 < 4; i8++) {
370 int x8 = i8 & 1;
371 int y8 = i8 >> 1;
372 int xy8 = x8 + y8 * b8_stride;
373 int xy4 = x8 * 3 + y8 * b4_stride;
374 int a, b;
375
376 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
377 continue;
378 sl->sub_mb_type[i8] = sub_mb_type;
379
380 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
381 (uint8_t)ref[0], 1);
382 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
383 (uint8_t)ref[1], 1);
384 if (!IS_INTRA(mb_type_col[y8]) && !sl->ref_list[1][0].parent->long_ref &&
385 ((l1ref0[xy8] == 0 &&
386 FFABS(l1mv0[xy4][0]) <= 1 &&
387 FFABS(l1mv0[xy4][1]) <= 1) ||
388 (l1ref0[xy8] < 0 &&
389 l1ref1[xy8] == 0 &&
390 FFABS(l1mv1[xy4][0]) <= 1 &&
391 FFABS(l1mv1[xy4][1]) <= 1))) {
392 a =
393 b = 0;
394 if (ref[0] > 0)
395 a = mv[0];
396 if (ref[1] > 0)
397 b = mv[1];
398 n++;
399 } else {
400 a = mv[0];
401 b = mv[1];
402 }
403 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, a, 4);
404 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, b, 4);
405 }
406 if (!is_b8x8 && !(n & 3))
407 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
410 } else if (IS_16X16(*mb_type)) {
411 int a, b;
412
413 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
414 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
415 if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
416 ((l1ref0[0] == 0 &&
417 FFABS(l1mv0[0][0]) <= 1 &&
418 FFABS(l1mv0[0][1]) <= 1) ||
419 (l1ref0[0] < 0 && !l1ref1[0] &&
420 FFABS(l1mv1[0][0]) <= 1 &&
421 FFABS(l1mv1[0][1]) <= 1 &&
422 h->x264_build > 33U))) {
423 a = b = 0;
424 if (ref[0] > 0)
425 a = mv[0];
426 if (ref[1] > 0)
427 b = mv[1];
428 } else {
429 a = mv[0];
430 b = mv[1];
431 }
432 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
433 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
434 } else {
435 int n = 0;
436 for (i8 = 0; i8 < 4; i8++) {
437 const int x8 = i8 & 1;
438 const int y8 = i8 >> 1;
439
440 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
441 continue;
442 sl->sub_mb_type[i8] = sub_mb_type;
443
444 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, mv[0], 4);
445 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, mv[1], 4);
446 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
447 (uint8_t)ref[0], 1);
448 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
449 (uint8_t)ref[1], 1);
450
451 assert(b8_stride == 2);
452 /* col_zero_flag */
453 if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
454 (l1ref0[i8] == 0 ||
455 (l1ref0[i8] < 0 &&
456 l1ref1[i8] == 0 &&
457 h->x264_build > 33U))) {
458 const int16_t (*l1mv)[2] = l1ref0[i8] == 0 ? l1mv0 : l1mv1;
459 if (IS_SUB_8X8(sub_mb_type)) {
460 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
461 if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
462 if (ref[0] == 0)
463 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2,
464 8, 0, 4);
465 if (ref[1] == 0)
466 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2,
467 8, 0, 4);
468 n += 4;
469 }
470 } else {
471 int m = 0;
472 for (i4 = 0; i4 < 4; i4++) {
473 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
474 (y8 * 2 + (i4 >> 1)) * b4_stride];
475 if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
476 if (ref[0] == 0)
477 AV_ZERO32(sl->mv_cache[0][scan8[i8 * 4 + i4]]);
478 if (ref[1] == 0)
479 AV_ZERO32(sl->mv_cache[1][scan8[i8 * 4 + i4]]);
480 m++;
481 }
482 }
483 if (!(m & 3))
485 n += m;
486 }
487 }
488 }
489 if (!is_b8x8 && !(n & 15))
490 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
493 }
494}
495
497 int *mb_type)
498{
499 int b8_stride = 2;
500 int b4_stride = h->b_stride;
501 int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
502 int mb_type_col[2];
503 const int16_t (*l1mv0)[2], (*l1mv1)[2];
504 const int8_t *l1ref0, *l1ref1;
505 const int is_b8x8 = IS_8X8(*mb_type);
506 unsigned int sub_mb_type;
507 int i8, i4;
508
509 assert(sl->ref_list[1][0].reference & 3);
510
512 sl->mb_y + !!IS_INTERLACED(*mb_type));
513
514 if (sl->ref_list[1][0].parent->field_picture ||
515 IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
516 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
517 mb_y = (sl->mb_y & ~1) + sl->col_parity;
518 mb_xy = sl->mb_x +
519 ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
520 b8_stride = 0;
521 } else {
522 mb_y += sl->col_fieldoff;
523 mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
524 }
525 goto single_col;
526 } else { // AFL/AFR/FR/FL -> AFR/FR
527 if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
528 mb_y = sl->mb_y & ~1;
529 mb_xy = sl->mb_x + (sl->mb_y & ~1) * h->mb_stride;
530 mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
531 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
532 b8_stride = 2 + 4 * h->mb_stride;
533 b4_stride *= 6;
534 if (IS_INTERLACED(mb_type_col[0]) !=
535 IS_INTERLACED(mb_type_col[1])) {
536 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
537 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
538 }
539
540 sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
541 MB_TYPE_DIRECT2; /* B_SUB_8x8 */
542
543 if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
544 (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
545 !is_b8x8) {
546 *mb_type |= MB_TYPE_16x8 | MB_TYPE_L0L1 |
547 MB_TYPE_DIRECT2; /* B_16x8 */
548 } else {
549 *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
550 }
551 } else { // AFR/FR -> AFR/FR
552single_col:
553 mb_type_col[0] =
554 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
555
556 sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
557 MB_TYPE_DIRECT2; /* B_SUB_8x8 */
558 if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
559 *mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
560 MB_TYPE_DIRECT2; /* B_16x16 */
561 } else if (!is_b8x8 &&
562 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
563 *mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 |
564 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
565 } else {
566 if (!h->ps.sps->direct_8x8_inference_flag) {
567 /* FIXME: save sub mb types from previous frames (or derive
568 * from MVs) so we know exactly what block size to use */
569 sub_mb_type = MB_TYPE_8x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
570 MB_TYPE_DIRECT2; /* B_SUB_4x4 */
571 }
572 *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
573 }
574 }
575 }
576
577 await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
578
579 l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
580 l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
581 l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
582 l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
583 if (!b8_stride) {
584 if (sl->mb_y & 1) {
585 l1ref0 += 2;
586 l1ref1 += 2;
587 l1mv0 += 2 * b4_stride;
588 l1mv1 += 2 * b4_stride;
589 }
590 }
591
592 {
593 const int *map_col_to_list0[2] = { sl->map_col_to_list0[0],
594 sl->map_col_to_list0[1] };
595 const int *dist_scale_factor = sl->dist_scale_factor;
596 int ref_offset;
597
598 if (FRAME_MBAFF(h) && IS_INTERLACED(*mb_type)) {
599 map_col_to_list0[0] = sl->map_col_to_list0_field[sl->mb_y & 1][0];
600 map_col_to_list0[1] = sl->map_col_to_list0_field[sl->mb_y & 1][1];
601 dist_scale_factor = sl->dist_scale_factor_field[sl->mb_y & 1];
602 }
603 ref_offset = (sl->ref_list[1][0].parent->mbaff << 4) & (mb_type_col[0] >> 3);
604
605 if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
606 int y_shift = 2 * !IS_INTERLACED(*mb_type);
607 assert(h->ps.sps->direct_8x8_inference_flag);
608
609 for (i8 = 0; i8 < 4; i8++) {
610 const int x8 = i8 & 1;
611 const int y8 = i8 >> 1;
612 int ref0, scale;
613 const int16_t (*l1mv)[2] = l1mv0;
614
615 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
616 continue;
617 sl->sub_mb_type[i8] = sub_mb_type;
618
619 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
620 if (IS_INTRA(mb_type_col[y8])) {
621 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
622 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
623 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
624 continue;
625 }
626
627 ref0 = l1ref0[x8 + y8 * b8_stride];
628 if (ref0 >= 0)
629 ref0 = map_col_to_list0[0][ref0 + ref_offset];
630 else {
631 ref0 = map_col_to_list0[1][l1ref1[x8 + y8 * b8_stride] +
632 ref_offset];
633 l1mv = l1mv1;
634 }
635 scale = dist_scale_factor[ref0];
636 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
637 ref0, 1);
638
639 {
640 const int16_t *mv_col = l1mv[x8 * 3 + y8 * b4_stride];
641 int my_col = (mv_col[1] * (1 << y_shift)) / 2;
642 int mx = (scale * mv_col[0] + 128) >> 8;
643 int my = (scale * my_col + 128) >> 8;
644 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
645 pack16to32(mx, my), 4);
646 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
647 pack16to32(mx - mv_col[0], my - my_col), 4);
648 }
649 }
650 return;
651 }
652
653 /* one-to-one mv scaling */
654
655 if (IS_16X16(*mb_type)) {
656 int ref, mv0, mv1;
657
658 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
659 if (IS_INTRA(mb_type_col[0])) {
660 ref = mv0 = mv1 = 0;
661 } else {
662 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
663 : map_col_to_list0[1][l1ref1[0] + ref_offset];
664 const int scale = dist_scale_factor[ref0];
665 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
666 int mv_l0[2];
667 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
668 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
669 ref = ref0;
670 mv0 = pack16to32(mv_l0[0], mv_l0[1]);
671 mv1 = pack16to32(mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1]);
672 }
673 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
674 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
675 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
676 } else {
677 for (i8 = 0; i8 < 4; i8++) {
678 const int x8 = i8 & 1;
679 const int y8 = i8 >> 1;
680 int ref0, scale;
681 const int16_t (*l1mv)[2] = l1mv0;
682
683 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
684 continue;
685 sl->sub_mb_type[i8] = sub_mb_type;
686 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
687 if (IS_INTRA(mb_type_col[0])) {
688 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
689 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
690 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
691 continue;
692 }
693
694 assert(b8_stride == 2);
695 ref0 = l1ref0[i8];
696 if (ref0 >= 0)
697 ref0 = map_col_to_list0[0][ref0 + ref_offset];
698 else {
699 ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
700 l1mv = l1mv1;
701 }
702 scale = dist_scale_factor[ref0];
703
704 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
705 ref0, 1);
706 if (IS_SUB_8X8(sub_mb_type)) {
707 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
708 int mx = (scale * mv_col[0] + 128) >> 8;
709 int my = (scale * mv_col[1] + 128) >> 8;
710 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
711 pack16to32(mx, my), 4);
712 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
713 pack16to32(mx - mv_col[0], my - mv_col[1]), 4);
714 } else {
715 for (i4 = 0; i4 < 4; i4++) {
716 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
717 (y8 * 2 + (i4 >> 1)) * b4_stride];
718 int16_t *mv_l0 = sl->mv_cache[0][scan8[i8 * 4 + i4]];
719 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
720 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
721 AV_WN32A(sl->mv_cache[1][scan8[i8 * 4 + i4]],
722 pack16to32(mv_l0[0] - mv_col[0],
723 mv_l0[1] - mv_col[1]));
724 }
725 }
726 }
727 }
728 }
729}
730
732 int *mb_type)
733{
735 pred_spatial_direct_motion(h, sl, mb_type);
736 else
737 pred_temp_direct_motion(h, sl, mb_type);
738}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
Definition dsp.h:57
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
Definition dsp.h:57
#define A(x)
Definition vpx_arith.h:28
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition avcodec.h:1590
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define av_clip_intp2
Definition common.h:121
#define av_clip_int8
Definition common.h:109
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
long long int64_t
Definition coverity.c:34
static void fill_rectangle(int x, int y, int w, int h)
Definition ffplay.c:829
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition avutil.h:280
void ff_h264_direct_ref_list_init(const H264Context *const h, H264SliceContext *sl)
static int get_scale_factor(const H264SliceContext *sl, int poc, int poc1, int i)
Definition h264_direct.c:37
static void pred_spatial_direct_motion(const H264Context *const h, H264SliceContext *sl, int *mb_type)
static void await_reference_mb_row(const H264Context *const h, H264Ref *ref, int mb_y)
static void fill_colmap(const H264Context *h, H264SliceContext *sl, int map[2][16+32], int list, int field, int colfield, int mbafi)
Definition h264_direct.c:82
static void pred_temp_direct_motion(const H264Context *const h, H264SliceContext *sl, int *mb_type)
void ff_h264_direct_dist_scale_factor(const H264Context *const h, H264SliceContext *sl)
Definition h264_direct.c:61
void ff_h264_pred_direct_motion(const H264Context *const h, H264SliceContext *sl, int *mb_type)
#define MB_TYPE_16x16_OR_INTRA
static const uint8_t scan8[16 *3+3]
Definition h264_parse.h:40
static av_always_inline uint32_t pack16to32(unsigned a, unsigned b)
Definition h264_parse.h:127
H.264 parameter set handling.
H.264 / AVC / MPEG-4 part10 codec.
#define IS_SUB_8X8(a)
Definition h264dec.h:94
#define FIELD_PICTURE(h)
Definition h264dec.h:65
#define FRAME_MBAFF(h)
Definition h264dec.h:64
#define PART_NOT_AVAILABLE
Definition h264pred.h:89
int a
#define B
Definition huffyuv.h:42
const VDPAUPixFmtMap * map
#define b
Definition input.c:43
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
#define AV_ZERO32(d)
#define AV_WN32A(p, v)
#define AV_RN32A(p)
static const int8_t mv[256][2]
Definition 4xm.c:81
#define C
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
#define FFMIN3(a, b, c)
Definition macros.h:50
#define mid_pred
Definition mathops.h:115
#define MB_TYPE_P0L1
Definition mpegutils.h:55
#define MB_TYPE_8x8
Definition mpegutils.h:44
#define MB_TYPE_8x16
Definition mpegutils.h:43
#define IS_INTERLACED(a)
Definition mpegutils.h:77
#define IS_DIRECT(a)
Definition mpegutils.h:78
#define MB_TYPE_L0L1
Definition mpegutils.h:59
#define MB_TYPE_DIRECT2
Definition mpegutils.h:46
#define MB_TYPE_16x8
Definition mpegutils.h:42
#define IS_16X16(a)
Definition mpegutils.h:80
#define IS_8X8(a)
Definition mpegutils.h:83
#define MB_TYPE_INTERLACED
Definition mpegutils.h:45
#define MB_TYPE_16x16
Definition mpegutils.h:41
#define MB_TYPE_L0
Definition mpegutils.h:57
#define MB_TYPE_P1L0
Definition mpegutils.h:54
#define MB_TYPE_P0L0
Definition mpegutils.h:53
#define PICT_BOTTOM_FIELD
Definition mpegutils.h:32
#define MB_TYPE_P1L1
Definition mpegutils.h:56
#define PICT_FRAME
Definition mpegutils.h:33
#define IS_INTRA(x, y)
void ff_thread_await_progress(const ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
useful rectangle filling function
H264Context.
Definition h264dec.h:338
AVCodecContext * avctx
Definition h264dec.h:340
int ref_count[2][2]
number of entries in ref_poc (FIXME need per slice)
Definition h264dec.h:141
uint32_t * mb_type
Definition h264dec.h:125
int16_t(*[2] motion_val)[2]
Definition h264dec.h:122
int8_t * ref_index[2]
RefStruct reference.
Definition h264dec.h:130
int field_picture
whether or not picture was encoded in separate fields
Definition h264dec.h:143
int frame_num
frame_num (raw frame_num from slice header)
Definition h264dec.h:134
int long_ref
1->long term reference 0->short term reference
Definition h264dec.h:139
int ref_poc[2][2][32]
POCs of the frames/fields used as reference (FIXME need per slice)
Definition h264dec.h:140
int field_poc[2]
top/bottom POC
Definition h264dec.h:132
int mbaff
1 -> MBAFF frame 0-> not MBAFF
Definition h264dec.h:142
int poc
Definition h264dec.h:172
const H264Picture * parent
Definition h264dec.h:175
int reference
Definition h264dec.h:171
unsigned int list_count
Definition h264dec.h:269
int8_t ref_cache[2][5 *8]
Definition h264dec.h:300
int dist_scale_factor_field[2][32]
Definition h264dec.h:261
uint16_t sub_mb_type[4]
Definition h264dec.h:304
int dist_scale_factor[32]
Definition h264dec.h:260
int16_t mv_cache[2][5 *8][2]
Motion vector cache.
Definition h264dec.h:299
int map_col_to_list0[2][16+32]
Definition h264dec.h:262
int slice_type_nos
S free slice type (SI/SP are remapped to I/P)
Definition h264dec.h:185
H264Ref ref_list[2][48]
0..15: frame refs, 16..47: mbaff field refs.
Definition h264dec.h:270
int direct_spatial_mv_pred
Definition h264dec.h:252
int map_col_to_list0_field[2][2][16+32]
Definition h264dec.h:263
const struct H264Context * h264
Definition h264dec.h:179
unsigned int ref_count[2]
num_ref_idx_l0/1_active_minus1 + 1
Definition h264dec.h:268
#define avpriv_request_sample(...)
#define av_log(a,...)
static int ref[MAX_W *MAX_W]