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 Libav.
7 * Libav 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 * Libav 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 Libav; 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, H264Picture *ref,
165 int ref_field = ref->reference - 1;
166 int ref_field_picture = ref->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->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].parent,
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);
241 int mask = ~(MB_TYPE_L0 << (2 * list));
249 if (ref[0] < 0 && ref[1] < 0) {
252 *mb_type |= MB_TYPE_L0L1;
253 sub_mb_type |= MB_TYPE_L0L1;
256 if (!(is_b8x8 | mv[0] | mv[1])) {
257 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
258 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
259 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
260 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
261 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
262 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
263 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
267 if (IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
268 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
269 mb_y = (sl->mb_y & ~1) + sl->col_parity;
271 ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
274 mb_y += sl->col_fieldoff;
275 mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
278 } else { // AFL/AFR/FR/FL -> AFR/FR
279 if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
280 mb_y = sl->mb_y & ~1;
281 mb_xy = (sl->mb_y & ~1) * h->mb_stride + sl->mb_x;
282 mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
283 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
284 b8_stride = 2 + 4 * h->mb_stride;
286 if (IS_INTERLACED(mb_type_col[0]) !=
287 IS_INTERLACED(mb_type_col[1])) {
288 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
289 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
292 sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
293 if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
294 (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
296 *mb_type |= MB_TYPE_16x8 | MB_TYPE_DIRECT2; /* B_16x8 */
298 *mb_type |= MB_TYPE_8x8;
300 } else { // AFR/FR -> AFR/FR
303 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
305 sub_mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_SUB_8x8 */
306 if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
307 *mb_type |= MB_TYPE_16x16 | MB_TYPE_DIRECT2; /* B_16x16 */
308 } else if (!is_b8x8 &&
309 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
310 *mb_type |= MB_TYPE_DIRECT2 |
311 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
313 if (!h->sps.direct_8x8_inference_flag) {
314 /* FIXME: Save sub mb types from previous frames (or derive
315 * from MVs) so we know exactly what block size to use. */
316 sub_mb_type += (MB_TYPE_8x8 - MB_TYPE_16x16); /* B_SUB_4x4 */
318 *mb_type |= MB_TYPE_8x8;
323 await_reference_mb_row(h, sl->ref_list[1][0].parent, mb_y);
325 l1mv0 = &sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
326 l1mv1 = &sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
327 l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
328 l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
333 l1mv0 += 2 * b4_stride;
334 l1mv1 += 2 * b4_stride;
338 if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
340 for (i8 = 0; i8 < 4; i8++) {
343 int xy8 = x8 + y8 * b8_stride;
344 int xy4 = x8 * 3 + y8 * b4_stride;
347 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
349 sl->sub_mb_type[i8] = sub_mb_type;
351 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
353 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
355 if (!IS_INTRA(mb_type_col[y8]) && !sl->ref_list[1][0].parent->long_ref &&
356 ((l1ref0[xy8] == 0 &&
357 FFABS(l1mv0[xy4][0]) <= 1 &&
358 FFABS(l1mv0[xy4][1]) <= 1) ||
361 FFABS(l1mv1[xy4][0]) <= 1 &&
362 FFABS(l1mv1[xy4][1]) <= 1))) {
374 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, a, 4);
375 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, b, 4);
377 if (!is_b8x8 && !(n & 3))
378 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
379 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
380 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
381 } else if (IS_16X16(*mb_type)) {
384 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
385 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
386 if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
388 FFABS(l1mv0[0][0]) <= 1 &&
389 FFABS(l1mv0[0][1]) <= 1) ||
390 (l1ref0[0] < 0 && !l1ref1[0] &&
391 FFABS(l1mv1[0][0]) <= 1 &&
392 FFABS(l1mv1[0][1]) <= 1 &&
393 h->x264_build > 33U))) {
403 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
404 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
407 for (i8 = 0; i8 < 4; i8++) {
408 const int x8 = i8 & 1;
409 const int y8 = i8 >> 1;
411 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
413 sl->sub_mb_type[i8] = sub_mb_type;
415 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, mv[0], 4);
416 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, mv[1], 4);
417 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
419 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8,
422 assert(b8_stride == 2);
424 if (!IS_INTRA(mb_type_col[0]) && !sl->ref_list[1][0].parent->long_ref &&
428 h->x264_build > 33U))) {
429 const int16_t (*l1mv)[2] = l1ref0[i8] == 0 ? l1mv0 : l1mv1;
430 if (IS_SUB_8X8(sub_mb_type)) {
431 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
432 if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
434 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2,
437 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2,
443 for (i4 = 0; i4 < 4; i4++) {
444 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
445 (y8 * 2 + (i4 >> 1)) * b4_stride];
446 if (FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1) {
448 AV_ZERO32(sl->mv_cache[0][scan8[i8 * 4 + i4]]);
450 AV_ZERO32(sl->mv_cache[1][scan8[i8 * 4 + i4]]);
455 sl->sub_mb_type[i8] += MB_TYPE_16x16 - MB_TYPE_8x8;
460 if (!is_b8x8 && !(n & 15))
461 *mb_type = (*mb_type & ~(MB_TYPE_8x8 | MB_TYPE_16x8 | MB_TYPE_8x16 |
462 MB_TYPE_P1L0 | MB_TYPE_P1L1)) |
463 MB_TYPE_16x16 | MB_TYPE_DIRECT2;
467 static void pred_temp_direct_motion(const H264Context *const h, H264SliceContext *sl,
471 int b4_stride = h->b_stride;
472 int mb_xy = sl->mb_xy, mb_y = sl->mb_y;
474 const int16_t (*l1mv0)[2], (*l1mv1)[2];
475 const int8_t *l1ref0, *l1ref1;
476 const int is_b8x8 = IS_8X8(*mb_type);
477 unsigned int sub_mb_type;
480 assert(sl->ref_list[1][0].reference & 3);
482 await_reference_mb_row(h, sl->ref_list[1][0].parent,
483 sl->mb_y + !!IS_INTERLACED(*mb_type));
485 if (IS_INTERLACED(sl->ref_list[1][0].parent->mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
486 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
487 mb_y = (sl->mb_y & ~1) + sl->col_parity;
489 ((sl->mb_y & ~1) + sl->col_parity) * h->mb_stride;
492 mb_y += sl->col_fieldoff;
493 mb_xy += h->mb_stride * sl->col_fieldoff; // non-zero for FL -> FL & differ parity
496 } else { // AFL/AFR/FR/FL -> AFR/FR
497 if (IS_INTERLACED(*mb_type)) { // AFL /FL -> AFR/FR
498 mb_y = sl->mb_y & ~1;
499 mb_xy = sl->mb_x + (sl->mb_y & ~1) * h->mb_stride;
500 mb_type_col[0] = sl->ref_list[1][0].parent->mb_type[mb_xy];
501 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy + h->mb_stride];
502 b8_stride = 2 + 4 * h->mb_stride;
504 if (IS_INTERLACED(mb_type_col[0]) !=
505 IS_INTERLACED(mb_type_col[1])) {
506 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
507 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
510 sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
511 MB_TYPE_DIRECT2; /* B_SUB_8x8 */
513 if ((mb_type_col[0] & MB_TYPE_16x16_OR_INTRA) &&
514 (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA) &&
516 *mb_type |= MB_TYPE_16x8 | MB_TYPE_L0L1 |
517 MB_TYPE_DIRECT2; /* B_16x8 */
519 *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
521 } else { // AFR/FR -> AFR/FR
524 mb_type_col[1] = sl->ref_list[1][0].parent->mb_type[mb_xy];
526 sub_mb_type = MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
527 MB_TYPE_DIRECT2; /* B_SUB_8x8 */
528 if (!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)) {
529 *mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
530 MB_TYPE_DIRECT2; /* B_16x16 */
531 } else if (!is_b8x8 &&
532 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16))) {
533 *mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 |
534 (mb_type_col[0] & (MB_TYPE_16x8 | MB_TYPE_8x16));
536 if (!h->sps.direct_8x8_inference_flag) {
537 /* FIXME: save sub mb types from previous frames (or derive
538 * from MVs) so we know exactly what block size to use */
539 sub_mb_type = MB_TYPE_8x8 | MB_TYPE_P0L0 | MB_TYPE_P0L1 |
540 MB_TYPE_DIRECT2; /* B_SUB_4x4 */
542 *mb_type |= MB_TYPE_8x8 | MB_TYPE_L0L1;
547 await_reference_mb_row(h, sl->ref_list[1][0].parent, mb_y);
549 l1mv0 = &sl->ref_list[1][0].parent->motion_val[0][h->mb2b_xy[mb_xy]];
550 l1mv1 = &sl->ref_list[1][0].parent->motion_val[1][h->mb2b_xy[mb_xy]];
551 l1ref0 = &sl->ref_list[1][0].parent->ref_index[0][4 * mb_xy];
552 l1ref1 = &sl->ref_list[1][0].parent->ref_index[1][4 * mb_xy];
557 l1mv0 += 2 * b4_stride;
558 l1mv1 += 2 * b4_stride;
563 const int *map_col_to_list0[2] = { sl->map_col_to_list0[0],
564 sl->map_col_to_list0[1] };
565 const int *dist_scale_factor = sl->dist_scale_factor;
568 if (FRAME_MBAFF(h) && IS_INTERLACED(*mb_type)) {
569 map_col_to_list0[0] = sl->map_col_to_list0_field[sl->mb_y & 1][0];
570 map_col_to_list0[1] = sl->map_col_to_list0_field[sl->mb_y & 1][1];
571 dist_scale_factor = sl->dist_scale_factor_field[sl->mb_y & 1];
573 ref_offset = (sl->ref_list[1][0].parent->mbaff << 4) & (mb_type_col[0] >> 3);
575 if (IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])) {
576 int y_shift = 2 * !IS_INTERLACED(*mb_type);
577 assert(h->sps.direct_8x8_inference_flag);
579 for (i8 = 0; i8 < 4; i8++) {
580 const int x8 = i8 & 1;
581 const int y8 = i8 >> 1;
583 const int16_t (*l1mv)[2] = l1mv0;
585 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
587 sl->sub_mb_type[i8] = sub_mb_type;
589 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
590 if (IS_INTRA(mb_type_col[y8])) {
591 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
592 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
593 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
597 ref0 = l1ref0[x8 + y8 * b8_stride];
599 ref0 = map_col_to_list0[0][ref0 + ref_offset];
601 ref0 = map_col_to_list0[1][l1ref1[x8 + y8 * b8_stride] +
605 scale = dist_scale_factor[ref0];
606 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
610 const int16_t *mv_col = l1mv[x8 * 3 + y8 * b4_stride];
611 int my_col = (mv_col[1] << y_shift) / 2;
612 int mx = (scale * mv_col[0] + 128) >> 8;
613 int my = (scale * my_col + 128) >> 8;
614 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
615 pack16to32(mx, my), 4);
616 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
617 pack16to32(mx - mv_col[0], my - my_col), 4);
623 /* one-to-one mv scaling */
625 if (IS_16X16(*mb_type)) {
628 fill_rectangle(&sl->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
629 if (IS_INTRA(mb_type_col[0])) {
632 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
633 : map_col_to_list0[1][l1ref1[0] + ref_offset];
634 const int scale = dist_scale_factor[ref0];
635 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
637 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
638 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
640 mv0 = pack16to32(mv_l0[0], mv_l0[1]);
641 mv1 = pack16to32(mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1]);
643 fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
644 fill_rectangle(&sl->mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
645 fill_rectangle(&sl->mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
647 for (i8 = 0; i8 < 4; i8++) {
648 const int x8 = i8 & 1;
649 const int y8 = i8 >> 1;
651 const int16_t (*l1mv)[2] = l1mv0;
653 if (is_b8x8 && !IS_DIRECT(sl->sub_mb_type[i8]))
655 sl->sub_mb_type[i8] = sub_mb_type;
656 fill_rectangle(&sl->ref_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 1);
657 if (IS_INTRA(mb_type_col[0])) {
658 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 1);
659 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8, 0, 4);
660 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8, 0, 4);
664 assert(b8_stride == 2);
667 ref0 = map_col_to_list0[0][ref0 + ref_offset];
669 ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
672 scale = dist_scale_factor[ref0];
674 fill_rectangle(&sl->ref_cache[0][scan8[i8 * 4]], 2, 2, 8,
676 if (IS_SUB_8X8(sub_mb_type)) {
677 const int16_t *mv_col = l1mv[x8 * 3 + y8 * 3 * b4_stride];
678 int mx = (scale * mv_col[0] + 128) >> 8;
679 int my = (scale * mv_col[1] + 128) >> 8;
680 fill_rectangle(&sl->mv_cache[0][scan8[i8 * 4]], 2, 2, 8,
681 pack16to32(mx, my), 4);
682 fill_rectangle(&sl->mv_cache[1][scan8[i8 * 4]], 2, 2, 8,
683 pack16to32(mx - mv_col[0], my - mv_col[1]), 4);
685 for (i4 = 0; i4 < 4; i4++) {
686 const int16_t *mv_col = l1mv[x8 * 2 + (i4 & 1) +
687 (y8 * 2 + (i4 >> 1)) * b4_stride];
688 int16_t *mv_l0 = sl->mv_cache[0][scan8[i8 * 4 + i4]];
689 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
690 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
691 AV_WN32A(sl->mv_cache[1][scan8[i8 * 4 + i4]],
692 pack16to32(mv_l0[0] - mv_col[0],
693 mv_l0[1] - mv_col[1]));
701 void ff_h264_pred_direct_motion(const H264Context *const h, H264SliceContext *sl,
704 if (sl->direct_spatial_mv_pred)
705 pred_spatial_direct_motion(h, sl, mb_type);
707 pred_temp_direct_motion(h, sl, mb_type);