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 int td = av_clip_int8(poc1 - poc0);
43 if (td == 0 || sl->ref_list[0][i].parent->long_ref) {
46 int tb = av_clip_int8(poc - poc0);
47 int tx = (16384 + (FFABS(td) >> 1)) / td;
48 return av_clip_intp2((tb * tx + 32) >> 6, 10);
52 void ff_h264_direct_dist_scale_factor(const H264Context *const h,
55 const int poc = FIELD_PICTURE(h) ? h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD]
56 : h->cur_pic_ptr->poc;
57 const int poc1 = sl->ref_list[1][0].poc;
61 for (field = 0; field < 2; field++) {
62 const int poc = h->cur_pic_ptr->field_poc[field];
63 const int poc1 = sl->ref_list[1][0].parent->field_poc[field];
64 for (i = 0; i < 2 * sl->ref_count[0]; i++)
65 sl->dist_scale_factor_field[field][i ^ field] =
66 get_scale_factor(sl, poc, poc1, i + 16);
69 for (i = 0; i < sl->ref_count[0]; i++)
70 sl->dist_scale_factor[i] = get_scale_factor(sl, poc, poc1, i);
73 static void fill_colmap(const H264Context *h, H264SliceContext *sl,
74 int map[2][16 + 32], int list,
75 int field, int colfield, int mbafi)
77 H264Picture *const ref1 = sl->ref_list[1][0].parent;
78 int j, old_ref, rfield;
79 int start = mbafi ? 16 : 0;
80 int end = mbafi ? 16 + 2 * sl->ref_count[0] : sl->ref_count[0];
81 int interl = mbafi || h->picture_structure != PICT_FRAME;
83 /* bogus; fills in for missing frames */
84 memset(map[list], 0, sizeof(map[list]));
86 for (rfield = 0; rfield < 2; rfield++) {
87 for (old_ref = 0; old_ref < ref1->ref_count[colfield][list]; old_ref++) {
88 int poc = ref1->ref_poc[colfield][list][old_ref];
92 // FIXME: store all MBAFF references so this is not needed
93 else if (interl && (poc & 3) == 3)
94 poc = (poc & ~3) + rfield + 1;
96 for (j = start; j < end; j++) {
97 if (4 * sl->ref_list[0][j].parent->frame_num +
98 (sl->ref_list[0][j].reference & 3) == poc) {
99 int cur_ref = mbafi ? (j - 16) ^ field : j;
101 map[list][2 * old_ref + (rfield ^ field) + 16] = cur_ref;
102 if (rfield == field || !interl)
103 map[list][old_ref] = cur_ref;
111 void ff_h264_direct_ref_list_init(const H264Context *const h, H264SliceContext *sl)
113 H264Ref *const ref1 = &sl->ref_list[1][0];
114 H264Picture *const cur = h->cur_pic_ptr;
116 int sidx = (h->picture_structure & 1) ^ 1;
117 int ref1sidx = (ref1->reference & 1) ^ 1;
119 for (list = 0; list < sl->list_count; list++) {
120 cur->ref_count[sidx][list] = sl->ref_count[list];
121 for (j = 0; j < sl->ref_count[list]; j++)
122 cur->ref_poc[sidx][list][j] = 4 * sl->ref_list[list][j].parent->frame_num +
123 (sl->ref_list[list][j].reference & 3);
126 if (h->picture_structure == PICT_FRAME) {
127 memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
128 memcpy(cur->ref_poc[1], cur->ref_poc[0], sizeof(cur->ref_poc[0]));
131 cur->mbaff = FRAME_MBAFF(h);
133 sl->col_fieldoff = 0;
135 if (sl->list_count != 2 || !sl->ref_count[1])
138 if (h->picture_structure == PICT_FRAME) {
139 int cur_poc = h->cur_pic_ptr->poc;
140 int *col_poc = sl->ref_list[1][0].parent->field_poc;
141 if (col_poc[0] == INT_MAX && col_poc[1] == INT_MAX) {
142 av_log(h->avctx, AV_LOG_ERROR, "co located POCs unavailable\n");
145 sl->col_parity = (FFABS(col_poc[0] - cur_poc) >=
146 FFABS(col_poc[1] - cur_poc));
148 sidx = sl->col_parity;
149 // FL -> FL & differ parity
150 } else if (!(h->picture_structure & sl->ref_list[1][0].reference) &&
151 !sl->ref_list[1][0].parent->mbaff) {
152 sl->col_fieldoff = 2 * sl->ref_list[1][0].reference - 3;
155 if (sl->slice_type_nos != AV_PICTURE_TYPE_B || sl->direct_spatial_mv_pred)
158 for (list = 0; list < 2; list++) {
159 fill_colmap(h, sl, sl->map_col_to_list0, list, sidx, ref1sidx, 0);
161 for (field = 0; field < 2; field++)
162 fill_colmap(h, sl, sl->map_col_to_list0_field[field], list, field,
167 static void await_reference_mb_row(const H264Context *const h, H264Ref *ref,
170 int ref_field = ref->reference - 1;
171 int ref_field_picture = ref->parent->field_picture;
172 int ref_height = 16 * h->mb_height >> ref_field_picture;
174 if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_FRAME))
177 /* FIXME: It can be safe to access mb stuff
178 * even if pixels aren't deblocked yet. */
180 ff_thread_await_progress(&ref->parent->tf,
181 FFMIN(16 * mb_y >> ref_field_picture,
183 ref_field_picture && ref_field);
186 static void pred_spatial_direct_motion(const H264Context *const h, H264SliceContext *sl,
190 int b4_stride = h->b_stride;
191 int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
193 const int16_t (*l1mv0)[2], (*l1mv1)[2];
194 const int8_t *l1ref0, *l1ref1;
195 const int is_b8x8 = IS_8X8(*mb_type);
196 unsigned int sub_mb_type = MB_TYPE_L0L1;
202 assert(sl->ref_list[1][0].reference & 3);
204 await_reference_mb_row(h, &sl->ref_list[1][0],
205 sl->mb_y + !!IS_INTERLACED(*mb_type));
207 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16 | MB_TYPE_INTRA4x4 | \
208 MB_TYPE_INTRA16x16 | MB_TYPE_INTRA_PCM)
210 /* ref = min(neighbors) */
211 for (list = 0; list < 2; list++) {
212 int left_ref = sl->ref_cache[list][scan8[0] - 1];
213 int top_ref = sl->ref_cache[list][scan8[0] - 8];
214 int refc = sl->ref_cache[list][scan8[0] - 8 + 4];
215 const int16_t *C = sl->mv_cache[list][scan8[0] - 8 + 4];
216 if (refc == PART_NOT_AVAILABLE) {
217 refc = sl->ref_cache[list][scan8[0] - 8 - 1];
218 C = sl->mv_cache[list][scan8[0] - 8 - 1];
220 ref[list] = FFMIN3((unsigned)left_ref,
223 if (ref[list] >= 0) {
224 /* This is just pred_motion() but with the cases removed that
225 * cannot happen for direct blocks. */
226 const int16_t *const A = sl->mv_cache[list][scan8[0] - 1];
227 const int16_t *const B = sl->mv_cache[list][scan8[0] - 8];
229 int match_count = (left_ref == ref[list]) +
230 (top_ref == ref[list]) +
233 if (match_count > 1) { // most common
234 mv[list] = pack16to32(mid_pred(A[0], B[0], C[0]),
235 mid_pred(A[1], B[1], C[1]));
237 assert(match_count == 1);
238 if (left_ref == ref[list])
239 mv[list] = AV_RN32A(A);
240 else if (top_ref == ref[list])
241 mv[list] = AV_RN32A(B);
243 mv[list] = AV_RN32A(C);
245 av_assert2(ref[list] < (sl->ref_count[list] << !!FRAME_MBAFF(h)));
247 int mask = ~(MB_TYPE_L0 << (2 * list));
255 if (ref[0] < 0 && ref[1] < 0) {
258 *mb_type |= MB_TYPE_L0L1;
259 sub_mb_type |= MB_TYPE_L0L1;
262 if (!(is_b8x8 | mv[0] | mv[1])) {
263 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
264 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
265 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
266 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
267 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
268 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
269 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
273 if (IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
274 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
275 mb_y = (sl->mb_y & ~1) + sl->col_parity;
277 ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
280 mb_y += sl->col_fieldoff;
281 mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
284 } else { // AFL/AFR/FR/FL -> AFR/FR
285 if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
286 mb_y = sl->mb_y & ~1;
287 mb_xy = (sl->mb_y & ~1) * h->mb_stride + sl->mb_x;
288 mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
289 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
290 b8_stride = 2 + 4 * h->mb_stride;
292 if (IS_INTERLACED(mb_type_col[0]) !=
293 IS_INTERLACED(mb_type_col[1])) {
294 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
295 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
298 sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
299 if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
300 (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
302 *mb_type |= MB_TYPE_16x8 | MB_TYPE_DIRECT2; /* B_16x8 */
304 *mb_type |= MB_TYPE_8x8;
306 } else { // AFR/FR -> AFR/FR
309 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
311 sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
312 if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
313 *mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_16x16 */
314 } else if (!is_b8x8 &&
315 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
316 *mb_type |= MB_TYPE_DIRECT2 |
317 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
319 if (!h->ps.sps->direct_8x8_inference_flag) {
320 /* FIXME: Save sub mb types from previous frames (or derive
321 * from MVs) so we know exactly what block size to use. */
322 sub_mb_type += (MB_TYPE_8x8 - MB_TYPE_16x16); /* B_SUB_4x4 */
324 *mb_type |= MB_TYPE_8x8;
329 await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
331 l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
332 l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
333 l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
334 l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
339 l1mv0 += 2 * b4_stride;
340 l1mv1 += 2 * b4_stride;
344 if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
346 for (i8 = 0; i8 < 4; i8++) {
349 int xy8 = x8 + y8 * b8_stride;
350 int xy4 = x8 * 3 + y8 * b4_stride;
353 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
355 sl->sub_mb_type[i8] = sub_mb_type;
357 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
359 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
361 if (!IS_INTRA(mb_type_col[y8]) && !sl->ref_list[1][0].parent->long_ref &&
362 ((l1ref0[xy8] == 0 &&
363 FFABS(l1mv0[xy4][0]) <= 1 &&
364 FFABS(l1mv0[xy4][1]) <= 1) ||
367 FFABS(l1mv1[xy4][0]) <= 1 &&
368 FFABS(l1mv1[xy4][1]) <= 1))) {
380 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, a, 4);
381 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, b, 4);
383 if (!is_b8x8 && !(n & 3))
384 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
385 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
386 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
387 } else if (IS_16X16(*mb_type)) {
390 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
391 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
392 if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
394 FFABS(l1mv0[0][0]) <= 1 &&
395 FFABS(l1mv0[0][1]) <= 1) ||
396 (l1ref0[0] < 0 && !l1ref1[0] &&
397 FFABS(l1mv1[0][0]) <= 1 &&
398 FFABS(l1mv1[0][1]) <= 1 &&
399 h->sei.unregistered.x264_build > 33U))) {
409 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
410 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
413 for (i8 = 0; i8 < 4; i8++) {
414 const int x8 = i8 & 1;
415 const int y8 = i8 >> 1;
417 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
419 sl->sub_mb_type[i8] = sub_mb_type;
421 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, mv[0], 4);
422 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, mv[1], 4);
423 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
425 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
428 assert(b8_stride == 2);
430 if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
434 h->sei.unregistered.x264_build > 33U))) {
435 const int16_t (*l1mv)[2] = l1ref0[i8] == 0 ? l1mv0 : l1mv1;
436 if (IS_SUB_8X8(sub_mb_type)) {
437 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
438 if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
440 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2,
443 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2,
449 for (i4 = 0; i4 < 4; i4++) {
450 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
451 (y8 * 2 + (i4 >> 1)) * b4_stride];
452 if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
454 AV_ZERO32(sl->mv_cache[0][scan8[i8 * 4 + i4]]);
456 AV_ZERO32(sl->mv_cache[1][scan8[i8 * 4 + i4]]);
461 sl->sub_mb_type[i8] += MB_TYPE_16x16 - MB_TYPE_8x8;
466 if (!is_b8x8 && !(n & 15))
467 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
468 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
469 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
473 static void pred_temp_direct_motion(const H264Context *const h, H264SliceContext *sl,
477 int b4_stride = h->b_stride;
478 int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
480 const int16_t (*l1mv0)[2], (*l1mv1)[2];
481 const int8_t *l1ref0, *l1ref1;
482 const int is_b8x8 = IS_8X8(*mb_type);
483 unsigned int sub_mb_type;
486 assert(sl->ref_list[1][0].reference & 3);
488 await_reference_mb_row(h, &sl->ref_list[1][0],
489 sl->mb_y + !!IS_INTERLACED(*mb_type));
491 if (IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
492 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
493 mb_y = (sl->mb_y & ~1) + sl->col_parity;
495 ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
498 mb_y += sl->col_fieldoff;
499 mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
502 } else { // AFL/AFR/FR/FL -> AFR/FR
503 if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
504 mb_y = sl->mb_y & ~1;
505 mb_xy = sl->mb_x + (sl->mb_y & ~1) * h->mb_stride;
506 mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
507 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
508 b8_stride = 2 + 4 * h->mb_stride;
510 if (IS_INTERLACED(mb_type_col[0]) !=
511 IS_INTERLACED(mb_type_col[1])) {
512 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
513 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
516 sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
517 MB_TYPE_DIRECT2; /* B_SUB_8x8 */
519 if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
520 (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
522 *mb_type |= MB_TYPE_16x8 | MB_TYPE_L0L1 |
523 MB_TYPE_DIRECT2; /* B_16x8 */
525 *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
527 } else { // AFR/FR -> AFR/FR
530 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
532 sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
533 MB_TYPE_DIRECT2; /* B_SUB_8x8 */
534 if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
535 *mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
536 MB_TYPE_DIRECT2; /* B_16x16 */
537 } else if (!is_b8x8 &&
538 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
539 *mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 |
540 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
542 if (!h->ps.sps->direct_8x8_inference_flag) {
543 /* FIXME: save sub mb types from previous frames (or derive
544 * from MVs) so we know exactly what block size to use */
545 sub_mb_type = MB_TYPE_8x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
546 MB_TYPE_DIRECT2; /* B_SUB_4x4 */
548 *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
553 await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
555 l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
556 l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
557 l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
558 l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
563 l1mv0 += 2 * b4_stride;
564 l1mv1 += 2 * b4_stride;
569 const int *map_col_to_list0[2] = { sl->map_col_to_list0[0],
570 sl->map_col_to_list0[1] };
571 const int *dist_scale_factor = sl->dist_scale_factor;
574 if (FRAME_MBAFF(h) && IS_INTERLACED(*mb_type)) {
575 map_col_to_list0[0] = sl->map_col_to_list0_field[sl->mb_y & 1][0];
576 map_col_to_list0[1] = sl->map_col_to_list0_field[sl->mb_y & 1][1];
577 dist_scale_factor = sl->dist_scale_factor_field[sl->mb_y & 1];
579 ref_offset = (sl->ref_list[1][0].parent->mbaff << 4) & (mb_type_col[0] >> 3);
581 if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
582 int y_shift = 2 * !IS_INTERLACED(*mb_type);
583 assert(h->ps.sps->direct_8x8_inference_flag);
585 for (i8 = 0; i8 < 4; i8++) {
586 const int x8 = i8 & 1;
587 const int y8 = i8 >> 1;
589 const int16_t (*l1mv)[2] = l1mv0;
591 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
593 sl->sub_mb_type[i8] = sub_mb_type;
595 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
596 if (IS_INTRA(mb_type_col[y8])) {
597 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
598 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
599 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
603 ref0 = l1ref0[x8 + y8 * b8_stride];
605 ref0 = map_col_to_list0[0][ref0 + ref_offset];
607 ref0 = map_col_to_list0[1][l1ref1[x8 + y8 * b8_stride] +
611 scale = dist_scale_factor[ref0];
612 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
616 const int16_t *mv_col = l1mv[x8 * 3 + y8 * b4_stride];
617 int my_col = (mv_col[1] << y_shift) / 2;
618 int mx = (scale * mv_col[0] + 128) >> 8;
619 int my = (scale * my_col + 128) >> 8;
620 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
621 pack16to32(mx, my), 4);
622 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
623 pack16to32(mx - mv_col[0], my - my_col), 4);
629 /* one-to-one mv scaling */
631 if (IS_16X16(*mb_type)) {
634 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
635 if (IS_INTRA(mb_type_col[0])) {
638 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
639 : map_col_to_list0[1][l1ref1[0] + ref_offset];
640 const int scale = dist_scale_factor[ref0];
641 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
643 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
644 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
646 mv0 = pack16to32(mv_l0[0], mv_l0[1]);
647 mv1 = pack16to32(mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1]);
649 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
650 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
651 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
653 for (i8 = 0; i8 < 4; i8++) {
654 const int x8 = i8 & 1;
655 const int y8 = i8 >> 1;
657 const int16_t (*l1mv)[2] = l1mv0;
659 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
661 sl->sub_mb_type[i8] = sub_mb_type;
662 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
663 if (IS_INTRA(mb_type_col[0])) {
664 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
665 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
666 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
670 assert(b8_stride == 2);
673 ref0 = map_col_to_list0[0][ref0 + ref_offset];
675 ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
678 scale = dist_scale_factor[ref0];
680 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
682 if (IS_SUB_8X8(sub_mb_type)) {
683 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
684 int mx = (scale * mv_col[0] + 128) >> 8;
685 int my = (scale * mv_col[1] + 128) >> 8;
686 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
687 pack16to32(mx, my), 4);
688 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
689 pack16to32(mx - mv_col[0], my - mv_col[1]), 4);
691 for (i4 = 0; i4 < 4; i4++) {
692 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
693 (y8 * 2 + (i4 >> 1)) * b4_stride];
694 int16_t *mv_l0 = sl->mv_cache[0][scan8[i8 * 4 + i4]];
695 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
696 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
697 AV_WN32A(sl->mv_cache[1][scan8[i8 * 4 + i4]],
698 pack16to32(mv_l0[0] - mv_col[0],
699 mv_l0[1] - mv_col[1]));
707 void ff_h264_pred_direct_motion(const H264Context *const h, H264SliceContext *sl,
710 if (sl->direct_spatial_mv_pred)
711 pred_spatial_direct_motion(h, sl, mb_type);
713 pred_temp_direct_motion(h, sl, mb_type);