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 / MPEG4 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 sl->col_parity = (FFABS(col_poc[0] - cur_poc) >=
141 FFABS(col_poc[1] - cur_poc));
143 sidx = sl->col_parity;
144 // FL -> FL & differ parity
145 } else if (!(h->picture_structure & sl->ref_list[1][0].reference) &&
146 !sl->ref_list[1][0].parent->mbaff) {
147 sl->col_fieldoff = 2 * sl->ref_list[1][0].reference - 3;
150 if (sl->slice_type_nos != AV_PICTURE_TYPE_B || sl->direct_spatial_mv_pred)
153 for (list = 0; list < 2; list++) {
154 fill_colmap(h, sl, sl->map_col_to_list0, list, sidx, ref1sidx, 0);
156 for (field = 0; field < 2; field++)
157 fill_colmap(h, sl, sl->map_col_to_list0_field[field], list, field,
162 static void await_reference_mb_row(const H264Context *const h, H264Ref *ref,
165 int ref_field = ref->reference - 1;
166 int ref_field_picture = ref->parent->field_picture;
167 int ref_height = 16 * h->mb_height >> ref_field_picture;
169 if (!HAVE_THREADS || !(h->avctx->active_thread_type & FF_THREAD_FRAME))
172 /* FIXME: It can be safe to access mb stuff
173 * even if pixels aren't deblocked yet. */
175 ff_thread_await_progress(&ref->parent->tf,
176 FFMIN(16 * mb_y >> ref_field_picture,
178 ref_field_picture && ref_field);
181 static void pred_spatial_direct_motion(const H264Context *const h, H264SliceContext *sl,
185 int b4_stride = h->b_stride;
186 int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
188 const int16_t (*l1mv0)[2], (*l1mv1)[2];
189 const int8_t *l1ref0, *l1ref1;
190 const int is_b8x8 = IS_8X8(*mb_type);
191 unsigned int sub_mb_type = MB_TYPE_L0L1;
197 assert(sl->ref_list[1][0].reference & 3);
199 await_reference_mb_row(h, &sl->ref_list[1][0],
200 sl->mb_y + !!IS_INTERLACED(*mb_type));
202 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16 | MB_TYPE_INTRA4x4 | \
203 MB_TYPE_INTRA16x16 | MB_TYPE_INTRA_PCM)
205 /* ref = min(neighbors) */
206 for (list = 0; list < 2; list++) {
207 int left_ref = sl->ref_cache[list][scan8[0] - 1];
208 int top_ref = sl->ref_cache[list][scan8[0] - 8];
209 int refc = sl->ref_cache[list][scan8[0] - 8 + 4];
210 const int16_t *C = sl->mv_cache[list][scan8[0] - 8 + 4];
211 if (refc == PART_NOT_AVAILABLE) {
212 refc = sl->ref_cache[list][scan8[0] - 8 - 1];
213 C = sl->mv_cache[list][scan8[0] - 8 - 1];
215 ref[list] = FFMIN3((unsigned)left_ref,
218 if (ref[list] >= 0) {
219 /* This is just pred_motion() but with the cases removed that
220 * cannot happen for direct blocks. */
221 const int16_t *const A = sl->mv_cache[list][scan8[0] - 1];
222 const int16_t *const B = sl->mv_cache[list][scan8[0] - 8];
224 int match_count = (left_ref == ref[list]) +
225 (top_ref == ref[list]) +
228 if (match_count > 1) { // most common
229 mv[list] = pack16to32(mid_pred(A[0], B[0], C[0]),
230 mid_pred(A[1], B[1], C[1]));
232 assert(match_count == 1);
233 if (left_ref == ref[list])
234 mv[list] = AV_RN32A(A);
235 else if (top_ref == ref[list])
236 mv[list] = AV_RN32A(B);
238 mv[list] = AV_RN32A(C);
240 av_assert2(ref[list] < (sl->ref_count[list] << !!FRAME_MBAFF(h)));
242 int mask = ~(MB_TYPE_L0 << (2 * list));
250 if (ref[0] < 0 && ref[1] < 0) {
253 *mb_type |= MB_TYPE_L0L1;
254 sub_mb_type |= MB_TYPE_L0L1;
257 if (!(is_b8x8 | mv[0] | mv[1])) {
258 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
259 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
260 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
261 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
262 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
263 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
264 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
268 if (IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
269 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
270 mb_y = (sl->mb_y & ~1) + sl->col_parity;
272 ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
275 mb_y += sl->col_fieldoff;
276 mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
279 } else { // AFL/AFR/FR/FL -> AFR/FR
280 if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
281 mb_y = sl->mb_y & ~1;
282 mb_xy = (sl->mb_y & ~1) * h->mb_stride + sl->mb_x;
283 mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
284 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
285 b8_stride = 2 + 4 * h->mb_stride;
287 if (IS_INTERLACED(mb_type_col[0]) !=
288 IS_INTERLACED(mb_type_col[1])) {
289 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
290 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
293 sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
294 if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
295 (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
297 *mb_type |= MB_TYPE_16x8 | MB_TYPE_DIRECT2; /* B_16x8 */
299 *mb_type |= MB_TYPE_8x8;
301 } else { // AFR/FR -> AFR/FR
304 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
306 sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
307 if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
308 *mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_16x16 */
309 } else if (!is_b8x8 &&
310 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
311 *mb_type |= MB_TYPE_DIRECT2 |
312 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
314 if (!h->sps.direct_8x8_inference_flag) {
315 /* FIXME: Save sub mb types from previous frames (or derive
316 * from MVs) so we know exactly what block size to use. */
317 sub_mb_type += (MB_TYPE_8x8 - MB_TYPE_16x16); /* B_SUB_4x4 */
319 *mb_type |= MB_TYPE_8x8;
324 await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
326 l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
327 l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
328 l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
329 l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
334 l1mv0 += 2 * b4_stride;
335 l1mv1 += 2 * b4_stride;
339 if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
341 for (i8 = 0; i8 < 4; i8++) {
344 int xy8 = x8 + y8 * b8_stride;
345 int xy4 = x8 * 3 + y8 * b4_stride;
348 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
350 sl->sub_mb_type[i8] = sub_mb_type;
352 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
354 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
356 if (!IS_INTRA(mb_type_col[y8]) && !sl->ref_list[1][0].parent->long_ref &&
357 ((l1ref0[xy8] == 0 &&
358 FFABS(l1mv0[xy4][0]) <= 1 &&
359 FFABS(l1mv0[xy4][1]) <= 1) ||
362 FFABS(l1mv1[xy4][0]) <= 1 &&
363 FFABS(l1mv1[xy4][1]) <= 1))) {
375 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, a, 4);
376 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, b, 4);
378 if (!is_b8x8 && !(n & 3))
379 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
380 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
381 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
382 } else if (IS_16X16(*mb_type)) {
385 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
386 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
387 if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
389 FFABS(l1mv0[0][0]) <= 1 &&
390 FFABS(l1mv0[0][1]) <= 1) ||
391 (l1ref0[0] < 0 && !l1ref1[0] &&
392 FFABS(l1mv1[0][0]) <= 1 &&
393 FFABS(l1mv1[0][1]) <= 1 &&
394 h->x264_build > 33U))) {
404 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
405 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
408 for (i8 = 0; i8 < 4; i8++) {
409 const int x8 = i8 & 1;
410 const int y8 = i8 >> 1;
412 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
414 sl->sub_mb_type[i8] = sub_mb_type;
416 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, mv[0], 4);
417 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, mv[1], 4);
418 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
420 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
423 assert(b8_stride == 2);
425 if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
429 h->x264_build > 33U))) {
430 const int16_t (*l1mv)[2] = l1ref0[i8] == 0 ? l1mv0 : l1mv1;
431 if (IS_SUB_8X8(sub_mb_type)) {
432 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
433 if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
435 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2,
438 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2,
444 for (i4 = 0; i4 < 4; i4++) {
445 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
446 (y8 * 2 + (i4 >> 1)) * b4_stride];
447 if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
449 AV_ZERO32(sl->mv_cache[0][scan8[i8 * 4 + i4]]);
451 AV_ZERO32(sl->mv_cache[1][scan8[i8 * 4 + i4]]);
456 sl->sub_mb_type[i8] += MB_TYPE_16x16 - MB_TYPE_8x8;
461 if (!is_b8x8 && !(n & 15))
462 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
463 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
464 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
468 static void pred_temp_direct_motion(const H264Context *const h, H264SliceContext *sl,
472 int b4_stride = h->b_stride;
473 int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
475 const int16_t (*l1mv0)[2], (*l1mv1)[2];
476 const int8_t *l1ref0, *l1ref1;
477 const int is_b8x8 = IS_8X8(*mb_type);
478 unsigned int sub_mb_type;
481 assert(sl->ref_list[1][0].reference & 3);
483 await_reference_mb_row(h, &sl->ref_list[1][0],
484 sl->mb_y + !!IS_INTERLACED(*mb_type));
486 if (IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
487 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
488 mb_y = (sl->mb_y & ~1) + sl->col_parity;
490 ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
493 mb_y += sl->col_fieldoff;
494 mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
497 } else { // AFL/AFR/FR/FL -> AFR/FR
498 if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
499 mb_y = sl->mb_y & ~1;
500 mb_xy = sl->mb_x + (sl->mb_y & ~1) * h->mb_stride;
501 mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
502 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
503 b8_stride = 2 + 4 * h->mb_stride;
505 if (IS_INTERLACED(mb_type_col[0]) !=
506 IS_INTERLACED(mb_type_col[1])) {
507 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
508 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
511 sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
512 MB_TYPE_DIRECT2; /* B_SUB_8x8 */
514 if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
515 (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
517 *mb_type |= MB_TYPE_16x8 | MB_TYPE_L0L1 |
518 MB_TYPE_DIRECT2; /* B_16x8 */
520 *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
522 } else { // AFR/FR -> AFR/FR
525 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
527 sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
528 MB_TYPE_DIRECT2; /* B_SUB_8x8 */
529 if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
530 *mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
531 MB_TYPE_DIRECT2; /* B_16x16 */
532 } else if (!is_b8x8 &&
533 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
534 *mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 |
535 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
537 if (!h->sps.direct_8x8_inference_flag) {
538 /* FIXME: save sub mb types from previous frames (or derive
539 * from MVs) so we know exactly what block size to use */
540 sub_mb_type = MB_TYPE_8x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
541 MB_TYPE_DIRECT2; /* B_SUB_4x4 */
543 *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
548 await_reference_mb_row(h, &sl->ref_list[1][0], mb_y);
550 l1mv0 = (void*)&sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
551 l1mv1 = (void*)&sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
552 l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
553 l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
558 l1mv0 += 2 * b4_stride;
559 l1mv1 += 2 * b4_stride;
564 const int *map_col_to_list0[2] = { sl->map_col_to_list0[0],
565 sl->map_col_to_list0[1] };
566 const int *dist_scale_factor = sl->dist_scale_factor;
569 if (FRAME_MBAFF(h) && IS_INTERLACED(*mb_type)) {
570 map_col_to_list0[0] = sl->map_col_to_list0_field[sl->mb_y & 1][0];
571 map_col_to_list0[1] = sl->map_col_to_list0_field[sl->mb_y & 1][1];
572 dist_scale_factor = sl->dist_scale_factor_field[sl->mb_y & 1];
574 ref_offset = (sl->ref_list[1][0].parent->mbaff << 4) & (mb_type_col[0] >> 3);
576 if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
577 int y_shift = 2 * !IS_INTERLACED(*mb_type);
578 assert(h->sps.direct_8x8_inference_flag);
580 for (i8 = 0; i8 < 4; i8++) {
581 const int x8 = i8 & 1;
582 const int y8 = i8 >> 1;
584 const int16_t (*l1mv)[2] = l1mv0;
586 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
588 sl->sub_mb_type[i8] = sub_mb_type;
590 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
591 if (IS_INTRA(mb_type_col[y8])) {
592 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
593 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
594 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
598 ref0 = l1ref0[x8 + y8 * b8_stride];
600 ref0 = map_col_to_list0[0][ref0 + ref_offset];
602 ref0 = map_col_to_list0[1][l1ref1[x8 + y8 * b8_stride] +
606 scale = dist_scale_factor[ref0];
607 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
611 const int16_t *mv_col = l1mv[x8 * 3 + y8 * b4_stride];
612 int my_col = (mv_col[1] << y_shift) / 2;
613 int mx = (scale * mv_col[0] + 128) >> 8;
614 int my = (scale * my_col + 128) >> 8;
615 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
616 pack16to32(mx, my), 4);
617 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
618 pack16to32(mx - mv_col[0], my - my_col), 4);
624 /* one-to-one mv scaling */
626 if (IS_16X16(*mb_type)) {
629 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
630 if (IS_INTRA(mb_type_col[0])) {
633 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
634 : map_col_to_list0[1][l1ref1[0] + ref_offset];
635 const int scale = dist_scale_factor[ref0];
636 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
638 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
639 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
641 mv0 = pack16to32(mv_l0[0], mv_l0[1]);
642 mv1 = pack16to32(mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1]);
644 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
645 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
646 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
648 for (i8 = 0; i8 < 4; i8++) {
649 const int x8 = i8 & 1;
650 const int y8 = i8 >> 1;
652 const int16_t (*l1mv)[2] = l1mv0;
654 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
656 sl->sub_mb_type[i8] = sub_mb_type;
657 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
658 if (IS_INTRA(mb_type_col[0])) {
659 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
660 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
661 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
665 assert(b8_stride == 2);
668 ref0 = map_col_to_list0[0][ref0 + ref_offset];
670 ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
673 scale = dist_scale_factor[ref0];
675 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
677 if (IS_SUB_8X8(sub_mb_type)) {
678 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
679 int mx = (scale * mv_col[0] + 128) >> 8;
680 int my = (scale * mv_col[1] + 128) >> 8;
681 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
682 pack16to32(mx, my), 4);
683 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
684 pack16to32(mx - mv_col[0], my - mv_col[1]), 4);
686 for (i4 = 0; i4 < 4; i4++) {
687 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
688 (y8 * 2 + (i4 >> 1)) * b4_stride];
689 int16_t *mv_l0 = sl->mv_cache[0][scan8[i8 * 4 + i4]];
690 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
691 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
692 AV_WN32A(sl->mv_cache[1][scan8[i8 * 4 + i4]],
693 pack16to32(mv_l0[0] - mv_col[0],
694 mv_l0[1] - mv_col[1]));
702 void ff_h264_pred_direct_motion(const H264Context *const h, H264SliceContext *sl,
705 if (sl->direct_spatial_mv_pred)
706 pred_spatial_direct_motion(h, sl, mb_type);
708 pred_temp_direct_motion(h, sl, mb_type);