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>
31 #include "mpegutils.h"
32 #include "rectangle.h"
37 static int get_scale_factor(H264SliceContext *sl,
38 int poc, int poc1, int i)
40 int poc0 = sl->ref_list[0][i].poc;
41 int td = av_clip_int8(poc1 - poc0);
42 if (td == 0 || sl->ref_list[0][i].parent->long_ref) {
45 int tb = av_clip_int8(poc - poc0);
46 int tx = (16384 + (FFABS(td) >> 1)) / td;
47 return av_clip_intp2((tb * tx + 32) >> 6, 10);
51 void ff_h264_direct_dist_scale_factor(const H264Context *const h,
54 const int poc = FIELD_PICTURE(h) ? h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD]
55 : h->cur_pic_ptr->poc;
56 const int poc1 = sl->ref_list[1][0].poc;
60 for (field = 0; field < 2; field++) {
61 const int poc = h->cur_pic_ptr->field_poc[field];
62 const int poc1 = sl->ref_list[1][0].parent->field_poc[field];
63 for (i = 0; i < 2 * sl->ref_count[0]; i++)
64 sl->dist_scale_factor_field[field][i ^ field] =
65 get_scale_factor(sl, poc, poc1, i + 16);
68 for (i = 0; i < sl->ref_count[0]; i++)
69 sl->dist_scale_factor[i] = get_scale_factor(sl, poc, poc1, i);
72 static void fill_colmap(const H264Context *h, H264SliceContext *sl,
73 int map[2][16 + 32], int list,
74 int field, int colfield, int mbafi)
76 H264Picture *const ref1 = sl->ref_list[1][0].parent;
77 int j, old_ref, rfield;
78 int start = mbafi ? 16 : 0;
79 int end = mbafi ? 16 + 2 * sl->ref_count[0] : sl->ref_count[0];
80 int interl = mbafi || h->picture_structure != PICT_FRAME;
82 /* bogus; fills in for missing frames */
83 memset(map[list], 0, sizeof(map[list]));
85 for (rfield = 0; rfield < 2; rfield++) {
86 for (old_ref = 0; old_ref < ref1->ref_count[colfield][list]; old_ref++) {
87 int poc = ref1->ref_poc[colfield][list][old_ref];
91 // FIXME: store all MBAFF references so this is not needed
92 else if (interl && (poc & 3) == 3)
93 poc = (poc & ~3) + rfield + 1;
95 for (j = start; j < end; j++) {
96 if (4 * sl->ref_list[0][j].parent->frame_num +
97 (sl->ref_list[0][j].reference & 3) == poc) {
98 int cur_ref = mbafi ? (j - 16) ^ field : j;
100 map[list][2 * old_ref + (rfield ^ field) + 16] = cur_ref;
101 if (rfield == field || !interl)
102 map[list][old_ref] = cur_ref;
110 void ff_h264_direct_ref_list_init(const H264Context *const h, H264SliceContext *sl)
112 H264Ref *const ref1 = &sl->ref_list[1][0];
113 H264Picture *const cur = h->cur_pic_ptr;
115 int sidx = (h->picture_structure & 1) ^ 1;
116 int ref1sidx = (ref1->reference & 1) ^ 1;
118 for (list = 0; list < sl->list_count; list++) {
119 cur->ref_count[sidx][list] = sl->ref_count[list];
120 for (j = 0; j < sl->ref_count[list]; j++)
121 cur->ref_poc[sidx][list][j] = 4 * sl->ref_list[list][j].parent->frame_num +
122 (sl->ref_list[list][j].reference & 3);
125 if (h->picture_structure == PICT_FRAME) {
126 memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
127 memcpy(cur->ref_poc[1], cur->ref_poc[0], sizeof(cur->ref_poc[0]));
130 cur->mbaff = FRAME_MBAFF(h);
132 sl->col_fieldoff = 0;
134 if (sl->list_count != 2 || !sl->ref_count[1])
137 if (h->picture_structure == PICT_FRAME) {
138 int cur_poc = h->cur_pic_ptr->poc;
139 int *col_poc = sl->ref_list[1][0].parent->field_poc;
140 if (col_poc[0] == INT_MAX && col_poc[1] == INT_MAX) {
141 av_log(h->avctx, AV_LOG_ERROR, "co located POCs unavailable\n");
144 sl->col_parity = (FFABS(col_poc[0] - cur_poc) >=
145 FFABS(col_poc[1] - cur_poc));
147 sidx = sl->col_parity;
148 // FL -> FL & differ parity
149 } else if (!(h->picture_structure & sl->ref_list[1][0].reference) &&
150 !sl->ref_list[1][0].parent->mbaff) {
151 sl->col_fieldoff = 2 * sl->ref_list[1][0].reference - 3;
154 if (sl->slice_type_nos != AV_PICTURE_TYPE_B || sl->direct_spatial_mv_pred)
157 for (list = 0; list < 2; list++) {
158 fill_colmap(h, sl, sl->map_col_to_list0, list, sidx, ref1sidx, 0);
160 for (field = 0; field < 2; field++)
161 fill_colmap(h, sl, sl->map_col_to_list0_field[field], list, field,
166 static void await_reference_mb_row(const H264Context *const h, H264Ref *ref,
169 int ref_field = ref->reference - 1;
170 int ref_field_picture = ref->parent->field_picture;
171 int ref_height = 16 * h->mb_height >> ref_field_picture;
173 if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_FRAME))
176 /* FIXME: It can be safe to access mb stuff
177 * even if pixels aren't deblocked yet. */
179 ff_thread_await_progress(&ref->parent->tf,
180 FFMIN(16 * mb_y >> ref_field_picture,
182 ref_field_picture && ref_field);
185 static void pred_spatial_direct_motion(const H264Context *const h, H264SliceContext *sl,
189 int b4_stride = h->b_stride;
190 int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
192 const int16_t (*l1mv0)[2], (*l1mv1)[2];
193 const int8_t *l1ref0, *l1ref1;
194 const int is_b8x8 = IS_8X8(*mb_type);
195 unsigned int sub_mb_type = MB_TYPE_L0L1;
201 assert(sl->ref_list[1][0].reference & 3);
203 await_reference_mb_row(h, &sl->ref_list[1][0],
204 sl->mb_y + !!IS_INTERLACED(*mb_type));
206 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16 | MB_TYPE_INTRA4x4 | \
207 MB_TYPE_INTRA16x16 | MB_TYPE_INTRA_PCM)
209 /* ref = min(neighbors) */
210 for (list = 0; list < 2; list++) {
211 int left_ref = sl->ref_cache[list][scan8[0] - 1];
212 int top_ref = sl->ref_cache[list][scan8[0] - 8];
213 int refc = sl->ref_cache[list][scan8[0] - 8 + 4];
214 const int16_t *C = sl->mv_cache[list][scan8[0] - 8 + 4];
215 if (refc == PART_NOT_AVAILABLE) {
216 refc = sl->ref_cache[list][scan8[0] - 8 - 1];
217 C = sl->mv_cache[list][scan8[0] - 8 - 1];
219 ref[list] = FFMIN3((unsigned)left_ref,
222 if (ref[list] >= 0) {
223 /* This is just pred_motion() but with the cases removed that
224 * cannot happen for direct blocks. */
225 const int16_t *const A = sl->mv_cache[list][scan8[0] - 1];
226 const int16_t *const B = sl->mv_cache[list][scan8[0] - 8];
228 int match_count = (left_ref == ref[list]) +
229 (top_ref == ref[list]) +
232 if (match_count > 1) { // most common
233 mv[list] = pack16to32(mid_pred(A[0], B[0], C[0]),
234 mid_pred(A[1], B[1], C[1]));
236 assert(match_count == 1);
237 if (left_ref == ref[list])
238 mv[list] = AV_RN32A(A);
239 else if (top_ref == ref[list])
240 mv[list] = AV_RN32A(B);
242 mv[list] = AV_RN32A(C);
244 av_assert2(ref[list] < (sl->ref_count[list] << !!FRAME_MBAFF(h)));
246 int mask = ~(MB_TYPE_L0 << (2 * list));
254 if (ref[0] < 0 && ref[1] < 0) {
257 *mb_type |= MB_TYPE_L0L1;
258 sub_mb_type |= MB_TYPE_L0L1;
261 if (!(is_b8x8 | mv[0] | mv[1])) {
262 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
263 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
264 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
265 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
266 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
267 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
268 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
272 if (IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
273 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
274 mb_y = (sl->mb_y & ~1) + sl->col_parity;
276 ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
279 mb_y += sl->col_fieldoff;
280 mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
283 } else { // AFL/AFR/FR/FL -> AFR/FR
284 if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
285 mb_y = sl->mb_y & ~1;
286 mb_xy = (sl->mb_y & ~1) * h->mb_stride + sl->mb_x;
287 mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
288 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
289 b8_stride = 2 + 4 * h->mb_stride;
291 if (IS_INTERLACED(mb_type_col[0]) !=
292 IS_INTERLACED(mb_type_col[1])) {
293 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
294 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
297 sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
298 if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
299 (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
301 *mb_type |= MB_TYPE_16x8 | MB_TYPE_DIRECT2; /* B_16x8 */
303 *mb_type |= MB_TYPE_8x8;
305 } else { // AFR/FR -> AFR/FR
308 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
310 sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
311 if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
312 *mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_16x16 */
313 } else if (!is_b8x8 &&
314 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
315 *mb_type |= MB_TYPE_DIRECT2 |
316 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
318 if (!h->ps.sps->direct_8x8_inference_flag) {
319 /* FIXME: Save sub mb types from previous frames (or derive
320 * from MVs) so we know exactly what block size to use. */
321 sub_mb_type += (MB_TYPE_8x8 - MB_TYPE_16x16); /* B_SUB_4x4 */
323 *mb_type |= MB_TYPE_8x8;
328 await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
330 l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
331 l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
332 l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
333 l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
338 l1mv0 += 2 * b4_stride;
339 l1mv1 += 2 * b4_stride;
343 if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
345 for (i8 = 0; i8 < 4; i8++) {
348 int xy8 = x8 + y8 * b8_stride;
349 int xy4 = x8 * 3 + y8 * b4_stride;
352 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
354 sl->sub_mb_type[i8] = sub_mb_type;
356 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
358 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
360 if (!IS_INTRA(mb_type_col[y8]) && !sl->ref_list[1][0].parent->long_ref &&
361 ((l1ref0[xy8] == 0 &&
362 FFABS(l1mv0[xy4][0]) <= 1 &&
363 FFABS(l1mv0[xy4][1]) <= 1) ||
366 FFABS(l1mv1[xy4][0]) <= 1 &&
367 FFABS(l1mv1[xy4][1]) <= 1))) {
379 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, a, 4);
380 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, b, 4);
382 if (!is_b8x8 && !(n & 3))
383 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
384 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
385 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
386 } else if (IS_16X16(*mb_type)) {
389 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
390 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
391 if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
393 FFABS(l1mv0[0][0]) <= 1 &&
394 FFABS(l1mv0[0][1]) <= 1) ||
395 (l1ref0[0] < 0 && !l1ref1[0] &&
396 FFABS(l1mv1[0][0]) <= 1 &&
397 FFABS(l1mv1[0][1]) <= 1 &&
398 h->sei.unregistered.x264_build > 33U))) {
408 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
409 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
412 for (i8 = 0; i8 < 4; i8++) {
413 const int x8 = i8 & 1;
414 const int y8 = i8 >> 1;
416 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
418 sl->sub_mb_type[i8] = sub_mb_type;
420 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, mv[0], 4);
421 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, mv[1], 4);
422 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
424 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
427 assert(b8_stride == 2);
429 if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
433 h->sei.unregistered.x264_build > 33U))) {
434 const int16_t (*l1mv)[2] = l1ref0[i8] == 0 ? l1mv0 : l1mv1;
435 if (IS_SUB_8X8(sub_mb_type)) {
436 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
437 if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
439 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2,
442 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2,
448 for (i4 = 0; i4 < 4; i4++) {
449 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
450 (y8 * 2 + (i4 >> 1)) * b4_stride];
451 if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
453 AV_ZERO32(sl->mv_cache[0][scan8[i8 * 4 + i4]]);
455 AV_ZERO32(sl->mv_cache[1][scan8[i8 * 4 + i4]]);
460 sl->sub_mb_type[i8] += MB_TYPE_16x16 - MB_TYPE_8x8;
465 if (!is_b8x8 && !(n & 15))
466 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
467 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
468 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
472 static void pred_temp_direct_motion(const H264Context *const h, H264SliceContext *sl,
476 int b4_stride = h->b_stride;
477 int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
479 const int16_t (*l1mv0)[2], (*l1mv1)[2];
480 const int8_t *l1ref0, *l1ref1;
481 const int is_b8x8 = IS_8X8(*mb_type);
482 unsigned int sub_mb_type;
485 assert(sl->ref_list[1][0].reference & 3);
487 await_reference_mb_row(h, &sl->ref_list[1][0],
488 sl->mb_y + !!IS_INTERLACED(*mb_type));
490 if (IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
491 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
492 mb_y = (sl->mb_y & ~1) + sl->col_parity;
494 ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
497 mb_y += sl->col_fieldoff;
498 mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
501 } else { // AFL/AFR/FR/FL -> AFR/FR
502 if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
503 mb_y = sl->mb_y & ~1;
504 mb_xy = sl->mb_x + (sl->mb_y & ~1) * h->mb_stride;
505 mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
506 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
507 b8_stride = 2 + 4 * h->mb_stride;
509 if (IS_INTERLACED(mb_type_col[0]) !=
510 IS_INTERLACED(mb_type_col[1])) {
511 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
512 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
515 sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
516 MB_TYPE_DIRECT2; /* B_SUB_8x8 */
518 if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
519 (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
521 *mb_type |= MB_TYPE_16x8 | MB_TYPE_L0L1 |
522 MB_TYPE_DIRECT2; /* B_16x8 */
524 *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
526 } else { // AFR/FR -> AFR/FR
529 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
531 sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
532 MB_TYPE_DIRECT2; /* B_SUB_8x8 */
533 if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
534 *mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
535 MB_TYPE_DIRECT2; /* B_16x16 */
536 } else if (!is_b8x8 &&
537 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
538 *mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 |
539 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
541 if (!h->ps.sps->direct_8x8_inference_flag) {
542 /* FIXME: save sub mb types from previous frames (or derive
543 * from MVs) so we know exactly what block size to use */
544 sub_mb_type = MB_TYPE_8x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
545 MB_TYPE_DIRECT2; /* B_SUB_4x4 */
547 *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
552 await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
554 l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
555 l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
556 l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
557 l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
562 l1mv0 += 2 * b4_stride;
563 l1mv1 += 2 * b4_stride;
568 const int *map_col_to_list0[2] = { sl->map_col_to_list0[0],
569 sl->map_col_to_list0[1] };
570 const int *dist_scale_factor = sl->dist_scale_factor;
573 if (FRAME_MBAFF(h) && IS_INTERLACED(*mb_type)) {
574 map_col_to_list0[0] = sl->map_col_to_list0_field[sl->mb_y & 1][0];
575 map_col_to_list0[1] = sl->map_col_to_list0_field[sl->mb_y & 1][1];
576 dist_scale_factor = sl->dist_scale_factor_field[sl->mb_y & 1];
578 ref_offset = (sl->ref_list[1][0].parent->mbaff << 4) & (mb_type_col[0] >> 3);
580 if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
581 int y_shift = 2 * !IS_INTERLACED(*mb_type);
582 assert(h->ps.sps->direct_8x8_inference_flag);
584 for (i8 = 0; i8 < 4; i8++) {
585 const int x8 = i8 & 1;
586 const int y8 = i8 >> 1;
588 const int16_t (*l1mv)[2] = l1mv0;
590 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
592 sl->sub_mb_type[i8] = sub_mb_type;
594 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
595 if (IS_INTRA(mb_type_col[y8])) {
596 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
597 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
598 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
602 ref0 = l1ref0[x8 + y8 * b8_stride];
604 ref0 = map_col_to_list0[0][ref0 + ref_offset];
606 ref0 = map_col_to_list0[1][l1ref1[x8 + y8 * b8_stride] +
610 scale = dist_scale_factor[ref0];
611 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
615 const int16_t *mv_col = l1mv[x8 * 3 + y8 * b4_stride];
616 int my_col = (mv_col[1] << y_shift) / 2;
617 int mx = (scale * mv_col[0] + 128) >> 8;
618 int my = (scale * my_col + 128) >> 8;
619 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
620 pack16to32(mx, my), 4);
621 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
622 pack16to32(mx - mv_col[0], my - my_col), 4);
628 /* one-to-one mv scaling */
630 if (IS_16X16(*mb_type)) {
633 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
634 if (IS_INTRA(mb_type_col[0])) {
637 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
638 : map_col_to_list0[1][l1ref1[0] + ref_offset];
639 const int scale = dist_scale_factor[ref0];
640 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
642 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
643 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
645 mv0 = pack16to32(mv_l0[0], mv_l0[1]);
646 mv1 = pack16to32(mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1]);
648 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
649 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
650 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
652 for (i8 = 0; i8 < 4; i8++) {
653 const int x8 = i8 & 1;
654 const int y8 = i8 >> 1;
656 const int16_t (*l1mv)[2] = l1mv0;
658 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
660 sl->sub_mb_type[i8] = sub_mb_type;
661 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
662 if (IS_INTRA(mb_type_col[0])) {
663 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
664 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
665 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
669 assert(b8_stride == 2);
672 ref0 = map_col_to_list0[0][ref0 + ref_offset];
674 ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
677 scale = dist_scale_factor[ref0];
679 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
681 if (IS_SUB_8X8(sub_mb_type)) {
682 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
683 int mx = (scale * mv_col[0] + 128) >> 8;
684 int my = (scale * mv_col[1] + 128) >> 8;
685 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
686 pack16to32(mx, my), 4);
687 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
688 pack16to32(mx - mv_col[0], my - mv_col[1]), 4);
690 for (i4 = 0; i4 < 4; i4++) {
691 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
692 (y8 * 2 + (i4 >> 1)) * b4_stride];
693 int16_t *mv_l0 = sl->mv_cache[0][scan8[i8 * 4 + i4]];
694 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
695 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
696 AV_WN32A(sl->mv_cache[1][scan8[i8 * 4 + i4]],
697 pack16to32(mv_l0[0] - mv_col[0],
698 mv_l0[1] - mv_col[1]));
706 void ff_h264_pred_direct_motion(const H264Context *const h, H264SliceContext *sl,
709 if (sl->direct_spatial_mv_pred)
710 pred_spatial_direct_motion(h, sl, mb_type);
712 pred_temp_direct_motion(h, sl, mb_type);