2 * H.26L/H.264/AVC/JVT/14496-10/... direct mb/block decoding
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 / MPEG-4 part10 direct mb/block decoding.
25 * @author Michael Niedermayer <michaelni@gmx.at>
32 #include "mpegutils.h"
33 #include "rectangle.h"
38 static int get_scale_factor(H264SliceContext *sl,
39 int poc, int poc1, int i)
41 int poc0 = sl->ref_list[0][i].poc;
42 int64_t pocdiff = poc1 - (int64_t)poc0;
43 int td = av_clip_int8(pocdiff);
45 if (pocdiff != (int)pocdiff)
46 avpriv_request_sample(sl->h264->avctx, "pocdiff overflow\n");
48 if (td == 0 || sl->ref_list[0][i].parent->long_ref) {
51 int64_t pocdiff0 = poc - (int64_t)poc0;
52 int tb = av_clip_int8(pocdiff0);
53 int tx = (16384 + (FFABS(td) >> 1)) / td;
55 if (pocdiff0 != (int)pocdiff0)
56 av_log(sl->h264->avctx, AV_LOG_DEBUG, "pocdiff0 overflow\n");
58 return av_clip_intp2((tb * tx + 32) >> 6, 10);
62 void ff_h264_direct_dist_scale_factor(const H264Context *const h,
65 const int poc = FIELD_PICTURE(h) ? h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD]
66 : h->cur_pic_ptr->poc;
67 const int poc1 = sl->ref_list[1][0].poc;
71 for (field = 0; field < 2; field++) {
72 const int poc = h->cur_pic_ptr->field_poc[field];
73 const int poc1 = sl->ref_list[1][0].parent->field_poc[field];
74 for (i = 0; i < 2 * sl->ref_count[0]; i++)
75 sl->dist_scale_factor_field[field][i ^ field] =
76 get_scale_factor(sl, poc, poc1, i + 16);
79 for (i = 0; i < sl->ref_count[0]; i++)
80 sl->dist_scale_factor[i] = get_scale_factor(sl, poc, poc1, i);
83 static void fill_colmap(const H264Context *h, H264SliceContext *sl,
84 int map[2][16 + 32], int list,
85 int field, int colfield, int mbafi)
87 H264Picture *const ref1 = sl->ref_list[1][0].parent;
88 int j, old_ref, rfield;
89 int start = mbafi ? 16 : 0;
90 int end = mbafi ? 16 + 2 * sl->ref_count[0] : sl->ref_count[0];
91 int interl = mbafi || h->picture_structure != PICT_FRAME;
93 /* bogus; fills in for missing frames */
94 memset(map[list], 0, sizeof(map[list]));
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];
102 // FIXME: store all MBAFF references so this is not needed
103 else if (interl && (poc & 3) == 3)
104 poc = (poc & ~3) + rfield + 1;
106 for (j = start; j < end; j++) {
107 if (4 * sl->ref_list[0][j].parent->frame_num +
108 (sl->ref_list[0][j].reference & 3) == poc) {
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;
121 void ff_h264_direct_ref_list_init(const H264Context *const h, H264SliceContext *sl)
123 H264Ref *const ref1 = &sl->ref_list[1][0];
124 H264Picture *const cur = h->cur_pic_ptr;
126 int sidx = (h->picture_structure & 1) ^ 1;
127 int ref1sidx = (ref1->reference & 1) ^ 1;
129 for (list = 0; list < sl->list_count; list++) {
130 cur->ref_count[sidx][list] = sl->ref_count[list];
131 for (j = 0; j < sl->ref_count[list]; j++)
132 cur->ref_poc[sidx][list][j] = 4 * sl->ref_list[list][j].parent->frame_num +
133 (sl->ref_list[list][j].reference & 3);
136 if (h->picture_structure == PICT_FRAME) {
137 memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
138 memcpy(cur->ref_poc[1], cur->ref_poc[0], sizeof(cur->ref_poc[0]));
141 cur->mbaff = FRAME_MBAFF(h);
143 sl->col_fieldoff = 0;
145 if (sl->list_count != 2 || !sl->ref_count[1])
148 if (h->picture_structure == PICT_FRAME) {
149 int cur_poc = h->cur_pic_ptr->poc;
150 int *col_poc = sl->ref_list[1][0].parent->field_poc;
151 if (col_poc[0] == INT_MAX && col_poc[1] == INT_MAX) {
152 av_log(h->avctx, AV_LOG_ERROR, "co located POCs unavailable\n");
155 sl->col_parity = (FFABS(col_poc[0] - cur_poc) >=
156 FFABS(col_poc[1] - cur_poc));
158 sidx = sl->col_parity;
159 // FL -> FL & differ parity
160 } else if (!(h->picture_structure & sl->ref_list[1][0].reference) &&
161 !sl->ref_list[1][0].parent->mbaff) {
162 sl->col_fieldoff = 2 * sl->ref_list[1][0].reference - 3;
165 if (sl->slice_type_nos != AV_PICTURE_TYPE_B || sl->direct_spatial_mv_pred)
168 for (list = 0; list < 2; list++) {
169 fill_colmap(h, sl, sl->map_col_to_list0, list, sidx, ref1sidx, 0);
171 for (field = 0; field < 2; field++)
172 fill_colmap(h, sl, sl->map_col_to_list0_field[field], list, field,
177 static void await_reference_mb_row(const H264Context *const h, H264Ref *ref,
180 int ref_field = ref->reference - 1;
181 int ref_field_picture = ref->parent->field_picture;
182 int ref_height = 16 * h->mb_height >> ref_field_picture;
184 if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_FRAME))
187 /* FIXME: It can be safe to access mb stuff
188 * even if pixels aren't deblocked yet. */
190 ff_thread_await_progress(&ref->parent->tf,
191 FFMIN(16 * mb_y >> ref_field_picture,
193 ref_field_picture && ref_field);
196 static void pred_spatial_direct_motion(const H264Context *const h, H264SliceContext *sl,
200 int b4_stride = h->b_stride;
201 int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
203 const int16_t (*l1mv0)[2], (*l1mv1)[2];
204 const int8_t *l1ref0, *l1ref1;
205 const int is_b8x8 = IS_8X8(*mb_type);
206 unsigned int sub_mb_type = MB_TYPE_L0L1;
212 assert(sl->ref_list[1][0].reference & 3);
214 await_reference_mb_row(h, &sl->ref_list[1][0],
215 sl->mb_y + !!IS_INTERLACED(*mb_type));
217 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16 | MB_TYPE_INTRA4x4 | \
218 MB_TYPE_INTRA16x16 | MB_TYPE_INTRA_PCM)
220 /* ref = min(neighbors) */
221 for (list = 0; list < 2; list++) {
222 int left_ref = sl->ref_cache[list][scan8[0] - 1];
223 int top_ref = sl->ref_cache[list][scan8[0] - 8];
224 int refc = sl->ref_cache[list][scan8[0] - 8 + 4];
225 const int16_t *C = sl->mv_cache[list][scan8[0] - 8 + 4];
226 if (refc == PART_NOT_AVAILABLE) {
227 refc = sl->ref_cache[list][scan8[0] - 8 - 1];
228 C = sl->mv_cache[list][scan8[0] - 8 - 1];
230 ref[list] = FFMIN3((unsigned)left_ref,
233 if (ref[list] >= 0) {
234 /* This is just pred_motion() but with the cases removed that
235 * cannot happen for direct blocks. */
236 const int16_t *const A = sl->mv_cache[list][scan8[0] - 1];
237 const int16_t *const B = sl->mv_cache[list][scan8[0] - 8];
239 int match_count = (left_ref == ref[list]) +
240 (top_ref == ref[list]) +
243 if (match_count > 1) { // most common
244 mv[list] = pack16to32(mid_pred(A[0], B[0], C[0]),
245 mid_pred(A[1], B[1], C[1]));
247 assert(match_count == 1);
248 if (left_ref == ref[list])
249 mv[list] = AV_RN32A(A);
250 else if (top_ref == ref[list])
251 mv[list] = AV_RN32A(B);
253 mv[list] = AV_RN32A(C);
255 av_assert2(ref[list] < (sl->ref_count[list] << !!FRAME_MBAFF(h)));
257 int mask = ~(MB_TYPE_L0 << (2 * list));
265 if (ref[0] < 0 && ref[1] < 0) {
268 *mb_type |= MB_TYPE_L0L1;
269 sub_mb_type |= MB_TYPE_L0L1;
272 if (!(is_b8x8 | mv[0] | mv[1])) {
273 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
274 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
275 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
276 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
277 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
278 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
279 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
283 if (IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
284 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
285 mb_y = (sl->mb_y & ~1) + sl->col_parity;
287 ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
290 mb_y += sl->col_fieldoff;
291 mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
294 } else { // AFL/AFR/FR/FL -> AFR/FR
295 if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
296 mb_y = sl->mb_y & ~1;
297 mb_xy = (sl->mb_y & ~1) * h->mb_stride + sl->mb_x;
298 mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
299 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
300 b8_stride = 2 + 4 * h->mb_stride;
302 if (IS_INTERLACED(mb_type_col[0]) !=
303 IS_INTERLACED(mb_type_col[1])) {
304 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
305 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
308 sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
309 if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
310 (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
312 *mb_type |= MB_TYPE_16x8 | MB_TYPE_DIRECT2; /* B_16x8 */
314 *mb_type |= MB_TYPE_8x8;
316 } else { // AFR/FR -> AFR/FR
319 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
321 sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
322 if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
323 *mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_16x16 */
324 } else if (!is_b8x8 &&
325 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
326 *mb_type |= MB_TYPE_DIRECT2 |
327 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
329 if (!h->ps.sps->direct_8x8_inference_flag) {
330 /* FIXME: Save sub mb types from previous frames (or derive
331 * from MVs) so we know exactly what block size to use. */
332 sub_mb_type += (MB_TYPE_8x8 - MB_TYPE_16x16); /* B_SUB_4x4 */
334 *mb_type |= MB_TYPE_8x8;
339 await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
341 l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
342 l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
343 l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
344 l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
349 l1mv0 += 2 * b4_stride;
350 l1mv1 += 2 * b4_stride;
354 if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
356 for (i8 = 0; i8 < 4; i8++) {
359 int xy8 = x8 + y8 * b8_stride;
360 int xy4 = x8 * 3 + y8 * b4_stride;
363 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
365 sl->sub_mb_type[i8] = sub_mb_type;
367 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
369 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
371 if (!IS_INTRA(mb_type_col[y8]) && !sl->ref_list[1][0].parent->long_ref &&
372 ((l1ref0[xy8] == 0 &&
373 FFABS(l1mv0[xy4][0]) <= 1 &&
374 FFABS(l1mv0[xy4][1]) <= 1) ||
377 FFABS(l1mv1[xy4][0]) <= 1 &&
378 FFABS(l1mv1[xy4][1]) <= 1))) {
390 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, a, 4);
391 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, b, 4);
393 if (!is_b8x8 && !(n & 3))
394 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
395 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
396 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
397 } else if (IS_16X16(*mb_type)) {
400 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
401 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
402 if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
404 FFABS(l1mv0[0][0]) <= 1 &&
405 FFABS(l1mv0[0][1]) <= 1) ||
406 (l1ref0[0] < 0 && !l1ref1[0] &&
407 FFABS(l1mv1[0][0]) <= 1 &&
408 FFABS(l1mv1[0][1]) <= 1 &&
409 h->sei.unregistered.x264_build > 33U))) {
419 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
420 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
423 for (i8 = 0; i8 < 4; i8++) {
424 const int x8 = i8 & 1;
425 const int y8 = i8 >> 1;
427 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
429 sl->sub_mb_type[i8] = sub_mb_type;
431 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, mv[0], 4);
432 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, mv[1], 4);
433 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
435 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
438 assert(b8_stride == 2);
440 if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
444 h->sei.unregistered.x264_build > 33U))) {
445 const int16_t (*l1mv)[2] = l1ref0[i8] == 0 ? l1mv0 : l1mv1;
446 if (IS_SUB_8X8(sub_mb_type)) {
447 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
448 if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
450 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2,
453 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2,
459 for (i4 = 0; i4 < 4; i4++) {
460 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
461 (y8 * 2 + (i4 >> 1)) * b4_stride];
462 if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
464 AV_ZERO32(sl->mv_cache[0][scan8[i8 * 4 + i4]]);
466 AV_ZERO32(sl->mv_cache[1][scan8[i8 * 4 + i4]]);
471 sl->sub_mb_type[i8] += MB_TYPE_16x16 - MB_TYPE_8x8;
476 if (!is_b8x8 && !(n & 15))
477 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
478 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
479 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
483 static void pred_temp_direct_motion(const H264Context *const h, H264SliceContext *sl,
487 int b4_stride = h->b_stride;
488 int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
490 const int16_t (*l1mv0)[2], (*l1mv1)[2];
491 const int8_t *l1ref0, *l1ref1;
492 const int is_b8x8 = IS_8X8(*mb_type);
493 unsigned int sub_mb_type;
496 assert(sl->ref_list[1][0].reference & 3);
498 await_reference_mb_row(h, &sl->ref_list[1][0],
499 sl->mb_y + !!IS_INTERLACED(*mb_type));
501 if (IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
502 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
503 mb_y = (sl->mb_y & ~1) + sl->col_parity;
505 ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
508 mb_y += sl->col_fieldoff;
509 mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
512 } else { // AFL/AFR/FR/FL -> AFR/FR
513 if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
514 mb_y = sl->mb_y & ~1;
515 mb_xy = sl->mb_x + (sl->mb_y & ~1) * h->mb_stride;
516 mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
517 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
518 b8_stride = 2 + 4 * h->mb_stride;
520 if (IS_INTERLACED(mb_type_col[0]) !=
521 IS_INTERLACED(mb_type_col[1])) {
522 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
523 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
526 sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
527 MB_TYPE_DIRECT2; /* B_SUB_8x8 */
529 if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
530 (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
532 *mb_type |= MB_TYPE_16x8 | MB_TYPE_L0L1 |
533 MB_TYPE_DIRECT2; /* B_16x8 */
535 *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
537 } else { // AFR/FR -> AFR/FR
540 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
542 sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
543 MB_TYPE_DIRECT2; /* B_SUB_8x8 */
544 if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
545 *mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
546 MB_TYPE_DIRECT2; /* B_16x16 */
547 } else if (!is_b8x8 &&
548 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
549 *mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 |
550 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
552 if (!h->ps.sps->direct_8x8_inference_flag) {
553 /* FIXME: save sub mb types from previous frames (or derive
554 * from MVs) so we know exactly what block size to use */
555 sub_mb_type = MB_TYPE_8x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
556 MB_TYPE_DIRECT2; /* B_SUB_4x4 */
558 *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
563 await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
565 l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
566 l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
567 l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
568 l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
573 l1mv0 += 2 * b4_stride;
574 l1mv1 += 2 * b4_stride;
579 const int *map_col_to_list0[2] = { sl->map_col_to_list0[0],
580 sl->map_col_to_list0[1] };
581 const int *dist_scale_factor = sl->dist_scale_factor;
584 if (FRAME_MBAFF(h) && IS_INTERLACED(*mb_type)) {
585 map_col_to_list0[0] = sl->map_col_to_list0_field[sl->mb_y & 1][0];
586 map_col_to_list0[1] = sl->map_col_to_list0_field[sl->mb_y & 1][1];
587 dist_scale_factor = sl->dist_scale_factor_field[sl->mb_y & 1];
589 ref_offset = (sl->ref_list[1][0].parent->mbaff << 4) & (mb_type_col[0] >> 3);
591 if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
592 int y_shift = 2 * !IS_INTERLACED(*mb_type);
593 assert(h->ps.sps->direct_8x8_inference_flag);
595 for (i8 = 0; i8 < 4; i8++) {
596 const int x8 = i8 & 1;
597 const int y8 = i8 >> 1;
599 const int16_t (*l1mv)[2] = l1mv0;
601 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
603 sl->sub_mb_type[i8] = sub_mb_type;
605 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
606 if (IS_INTRA(mb_type_col[y8])) {
607 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
608 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
609 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
613 ref0 = l1ref0[x8 + y8 * b8_stride];
615 ref0 = map_col_to_list0[0][ref0 + ref_offset];
617 ref0 = map_col_to_list0[1][l1ref1[x8 + y8 * b8_stride] +
621 scale = dist_scale_factor[ref0];
622 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
626 const int16_t *mv_col = l1mv[x8 * 3 + y8 * b4_stride];
627 int my_col = (mv_col[1] * (1 << y_shift)) / 2;
628 int mx = (scale * mv_col[0] + 128) >> 8;
629 int my = (scale * my_col + 128) >> 8;
630 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
631 pack16to32(mx, my), 4);
632 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
633 pack16to32(mx - mv_col[0], my - my_col), 4);
639 /* one-to-one mv scaling */
641 if (IS_16X16(*mb_type)) {
644 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
645 if (IS_INTRA(mb_type_col[0])) {
648 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
649 : map_col_to_list0[1][l1ref1[0] + ref_offset];
650 const int scale = dist_scale_factor[ref0];
651 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
653 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
654 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
656 mv0 = pack16to32(mv_l0[0], mv_l0[1]);
657 mv1 = pack16to32(mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1]);
659 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
660 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
661 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
663 for (i8 = 0; i8 < 4; i8++) {
664 const int x8 = i8 & 1;
665 const int y8 = i8 >> 1;
667 const int16_t (*l1mv)[2] = l1mv0;
669 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
671 sl->sub_mb_type[i8] = sub_mb_type;
672 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
673 if (IS_INTRA(mb_type_col[0])) {
674 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
675 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
676 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
680 assert(b8_stride == 2);
683 ref0 = map_col_to_list0[0][ref0 + ref_offset];
685 ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
688 scale = dist_scale_factor[ref0];
690 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
692 if (IS_SUB_8X8(sub_mb_type)) {
693 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
694 int mx = (scale * mv_col[0] + 128) >> 8;
695 int my = (scale * mv_col[1] + 128) >> 8;
696 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
697 pack16to32(mx, my), 4);
698 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
699 pack16to32(mx - mv_col[0], my - mv_col[1]), 4);
701 for (i4 = 0; i4 < 4; i4++) {
702 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
703 (y8 * 2 + (i4 >> 1)) * b4_stride];
704 int16_t *mv_l0 = sl->mv_cache[0][scan8[i8 * 4 + i4]];
705 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
706 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
707 AV_WN32A(sl->mv_cache[1][scan8[i8 * 4 + i4]],
708 pack16to32(mv_l0[0] - mv_col[0],
709 mv_l0[1] - mv_col[1]));
717 void ff_h264_pred_direct_motion(const H264Context *const h, H264SliceContext *sl,
720 if (sl->direct_spatial_mv_pred)
721 pred_spatial_direct_motion(h, sl, mb_type);
723 pred_temp_direct_motion(h, sl, mb_type);