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 "mpegvideo.h"
33 #include "rectangle.h"
40 static int get_scale_factor(H264Context * const h, int poc, int poc1, int i){
41 int poc0 = h->ref_list[0][i].poc;
42 int td = av_clip(poc1 - poc0, -128, 127);
43 if(td == 0 || h->ref_list[0][i].long_ref){
46 int tb = av_clip(poc - poc0, -128, 127);
47 int tx = (16384 + (FFABS(td) >> 1)) / td;
48 return av_clip((tb*tx + 32) >> 6, -1024, 1023);
52 void ff_h264_direct_dist_scale_factor(H264Context * const h){
53 MpegEncContext * const s = &h->s;
54 const int poc = h->s.current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
55 const int poc1 = h->ref_list[1][0].poc;
59 for (field = 0; field < 2; field++){
60 const int poc = h->s.current_picture_ptr->field_poc[field];
61 const int poc1 = h->ref_list[1][0].field_poc[field];
62 for (i = 0; i < 2 * h->ref_count[0]; i++)
63 h->dist_scale_factor_field[field][i^field] =
64 get_scale_factor(h, poc, poc1, i+16);
67 for (i = 0; i < h->ref_count[0]; i++){
68 h->dist_scale_factor[i] = get_scale_factor(h, poc, poc1, i);
72 static void fill_colmap(H264Context *h, int map[2][16+32], int list, int field, int colfield, int mbafi){
73 MpegEncContext * const s = &h->s;
74 Picture * const ref1 = &h->ref_list[1][0];
75 int j, old_ref, rfield;
76 int start= mbafi ? 16 : 0;
77 int end = mbafi ? 16+2*h->ref_count[0] : h->ref_count[0];
78 int interl= mbafi || s->picture_structure != PICT_FRAME;
80 /* bogus; fills in for missing frames */
81 memset(map[list], 0, sizeof(map[list]));
83 for(rfield=0; rfield<2; rfield++){
84 for(old_ref=0; old_ref<ref1->ref_count[colfield][list]; old_ref++){
85 int poc = ref1->ref_poc[colfield][list][old_ref];
89 else if( interl && (poc&3) == 3) // FIXME: store all MBAFF references so this is not needed
90 poc= (poc&~3) + rfield + 1;
92 for(j=start; j<end; j++){
93 if (4 * h->ref_list[0][j].frame_num + (h->ref_list[0][j].f.reference & 3) == poc) {
94 int cur_ref= mbafi ? (j-16)^field : j;
96 map[list][2 * old_ref + (rfield^field) + 16] = cur_ref;
97 if(rfield == field || !interl)
98 map[list][old_ref] = cur_ref;
106 void ff_h264_direct_ref_list_init(H264Context * const h){
107 MpegEncContext * const s = &h->s;
108 Picture * const ref1 = &h->ref_list[1][0];
109 Picture * const cur = s->current_picture_ptr;
111 int sidx= (s->picture_structure&1)^1;
112 int ref1sidx = (ref1->f.reference&1)^1;
114 for(list=0; list<2; list++){
115 cur->ref_count[sidx][list] = h->ref_count[list];
116 for(j=0; j<h->ref_count[list]; j++)
117 cur->ref_poc[sidx][list][j] = 4 * h->ref_list[list][j].frame_num + (h->ref_list[list][j].f.reference & 3);
120 if(s->picture_structure == PICT_FRAME){
121 memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
122 memcpy(cur->ref_poc [1], cur->ref_poc [0], sizeof(cur->ref_poc [0]));
125 cur->mbaff= FRAME_MBAFF;
128 if(s->picture_structure == PICT_FRAME){
129 int cur_poc = s->current_picture_ptr->poc;
130 int *col_poc = h->ref_list[1]->field_poc;
131 h->col_parity= (FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc));
132 ref1sidx=sidx= h->col_parity;
133 } else if (!(s->picture_structure & h->ref_list[1][0].f.reference) && !h->ref_list[1][0].mbaff) { // FL -> FL & differ parity
134 h->col_fieldoff = 2 * h->ref_list[1][0].f.reference - 3;
137 if (h->slice_type_nos != AV_PICTURE_TYPE_B || h->direct_spatial_mv_pred)
140 for(list=0; list<2; list++){
141 fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
143 for(field=0; field<2; field++)
144 fill_colmap(h, h->map_col_to_list0_field[field], list, field, field, 1);
148 static void await_reference_mb_row(H264Context * const h, Picture *ref, int mb_y)
150 int ref_field = ref->f.reference - 1;
151 int ref_field_picture = ref->field_picture;
152 int ref_height = 16*h->s.mb_height >> ref_field_picture;
154 if(!HAVE_THREADS || !(h->s.avctx->active_thread_type&FF_THREAD_FRAME))
157 //FIXME it can be safe to access mb stuff
158 //even if pixels aren't deblocked yet
160 ff_thread_await_progress(&ref->f,
161 FFMIN(16 * mb_y >> ref_field_picture, ref_height - 1),
162 ref_field_picture && ref_field);
165 static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){
166 MpegEncContext * const s = &h->s;
168 int b4_stride = h->b_stride;
169 int mb_xy = h->mb_xy, mb_y = s->mb_y;
171 const int16_t (*l1mv0)[2], (*l1mv1)[2];
172 const int8_t *l1ref0, *l1ref1;
173 const int is_b8x8 = IS_8X8(*mb_type);
174 unsigned int sub_mb_type= MB_TYPE_L0L1;
180 assert(h->ref_list[1][0].f.reference & 3);
182 await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type));
184 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
187 /* ref = min(neighbors) */
188 for(list=0; list<2; list++){
189 int left_ref = h->ref_cache[list][scan8[0] - 1];
190 int top_ref = h->ref_cache[list][scan8[0] - 8];
191 int refc = h->ref_cache[list][scan8[0] - 8 + 4];
192 const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
193 if(refc == PART_NOT_AVAILABLE){
194 refc = h->ref_cache[list][scan8[0] - 8 - 1];
195 C = h-> mv_cache[list][scan8[0] - 8 - 1];
197 ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
199 //this is just pred_motion() but with the cases removed that cannot happen for direct blocks
200 const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
201 const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
203 int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
204 if(match_count > 1){ //most common
205 mv[list]= pack16to32(mid_pred(A[0], B[0], C[0]),
206 mid_pred(A[1], B[1], C[1]) );
208 assert(match_count==1);
209 if(left_ref==ref[list]){
210 mv[list]= AV_RN32A(A);
211 }else if(top_ref==ref[list]){
212 mv[list]= AV_RN32A(B);
214 mv[list]= AV_RN32A(C);
218 int mask= ~(MB_TYPE_L0 << (2*list));
226 if(ref[0] < 0 && ref[1] < 0){
229 *mb_type |= MB_TYPE_L0L1;
230 sub_mb_type |= MB_TYPE_L0L1;
233 if(!(is_b8x8|mv[0]|mv[1])){
234 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
235 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
236 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
237 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
238 *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2;
242 if (IS_INTERLACED(h->ref_list[1][0].f.mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
243 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
244 mb_y = (s->mb_y&~1) + h->col_parity;
245 mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
248 mb_y += h->col_fieldoff;
249 mb_xy += s->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
252 }else{ // AFL/AFR/FR/FL -> AFR/FR
253 if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
255 mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
256 mb_type_col[0] = h->ref_list[1][0].f.mb_type[mb_xy];
257 mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy + s->mb_stride];
258 b8_stride = 2+4*s->mb_stride;
260 if (IS_INTERLACED(mb_type_col[0]) != IS_INTERLACED(mb_type_col[1])) {
261 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
262 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
265 sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
266 if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
267 && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
269 *mb_type |= MB_TYPE_16x8 |MB_TYPE_DIRECT2; /* B_16x8 */
271 *mb_type |= MB_TYPE_8x8;
273 }else{ // AFR/FR -> AFR/FR
276 mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy];
278 sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
279 if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
280 *mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_16x16 */
281 }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
282 *mb_type |= MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
284 if(!h->sps.direct_8x8_inference_flag){
285 /* FIXME save sub mb types from previous frames (or derive from MVs)
286 * so we know exactly what block size to use */
287 sub_mb_type += (MB_TYPE_8x8-MB_TYPE_16x16); /* B_SUB_4x4 */
289 *mb_type |= MB_TYPE_8x8;
294 await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
296 l1mv0 = &h->ref_list[1][0].f.motion_val[0][h->mb2b_xy [mb_xy]];
297 l1mv1 = &h->ref_list[1][0].f.motion_val[1][h->mb2b_xy [mb_xy]];
298 l1ref0 = &h->ref_list[1][0].f.ref_index [0][4 * mb_xy];
299 l1ref1 = &h->ref_list[1][0].f.ref_index [1][4 * mb_xy];
304 l1mv0 += 2*b4_stride;
305 l1mv1 += 2*b4_stride;
310 if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
312 for(i8=0; i8<4; i8++){
315 int xy8 = x8+y8*b8_stride;
316 int xy4 = 3*x8+y8*b4_stride;
319 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
321 h->sub_mb_type[i8] = sub_mb_type;
323 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
324 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
325 if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
326 && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
327 || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
338 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
339 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
341 if(!is_b8x8 && !(n&3))
342 *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2;
343 }else if(IS_16X16(*mb_type)){
346 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
347 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
348 if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
349 && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
350 || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
351 && h->x264_build>33U))){
361 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
362 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
365 for(i8=0; i8<4; i8++){
367 const int y8 = i8>>1;
369 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
371 h->sub_mb_type[i8] = sub_mb_type;
373 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, mv[0], 4);
374 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, mv[1], 4);
375 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
376 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
378 assert(b8_stride==2);
380 if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && ( l1ref0[i8] == 0
381 || (l1ref0[i8] < 0 && l1ref1[i8] == 0
382 && h->x264_build>33U))){
383 const int16_t (*l1mv)[2]= l1ref0[i8] == 0 ? l1mv0 : l1mv1;
384 if(IS_SUB_8X8(sub_mb_type)){
385 const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
386 if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
388 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
390 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
395 for(i4=0; i4<4; i4++){
396 const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
397 if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
399 AV_ZERO32(h->mv_cache[0][scan8[i8*4+i4]]);
401 AV_ZERO32(h->mv_cache[1][scan8[i8*4+i4]]);
406 h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;
411 if(!is_b8x8 && !(n&15))
412 *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2;
416 static void pred_temp_direct_motion(H264Context * const h, int *mb_type){
417 MpegEncContext * const s = &h->s;
419 int b4_stride = h->b_stride;
420 int mb_xy = h->mb_xy, mb_y = s->mb_y;
422 const int16_t (*l1mv0)[2], (*l1mv1)[2];
423 const int8_t *l1ref0, *l1ref1;
424 const int is_b8x8 = IS_8X8(*mb_type);
425 unsigned int sub_mb_type;
428 assert(h->ref_list[1][0].f.reference & 3);
430 await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type));
432 if (IS_INTERLACED(h->ref_list[1][0].f.mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
433 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
434 mb_y = (s->mb_y&~1) + h->col_parity;
435 mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
438 mb_y += h->col_fieldoff;
439 mb_xy += s->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
442 }else{ // AFL/AFR/FR/FL -> AFR/FR
443 if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
445 mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
446 mb_type_col[0] = h->ref_list[1][0].f.mb_type[mb_xy];
447 mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy + s->mb_stride];
448 b8_stride = 2+4*s->mb_stride;
450 if (IS_INTERLACED(mb_type_col[0]) != IS_INTERLACED(mb_type_col[1])) {
451 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
452 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
455 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
457 if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
458 && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
460 *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
462 *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
464 }else{ // AFR/FR -> AFR/FR
467 mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy];
469 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
470 if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
471 *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
472 }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
473 *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
475 if(!h->sps.direct_8x8_inference_flag){
476 /* FIXME save sub mb types from previous frames (or derive from MVs)
477 * so we know exactly what block size to use */
478 sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
480 *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
485 await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
487 l1mv0 = &h->ref_list[1][0].f.motion_val[0][h->mb2b_xy [mb_xy]];
488 l1mv1 = &h->ref_list[1][0].f.motion_val[1][h->mb2b_xy [mb_xy]];
489 l1ref0 = &h->ref_list[1][0].f.ref_index [0][4 * mb_xy];
490 l1ref1 = &h->ref_list[1][0].f.ref_index [1][4 * mb_xy];
495 l1mv0 += 2*b4_stride;
496 l1mv1 += 2*b4_stride;
501 const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
502 const int *dist_scale_factor = h->dist_scale_factor;
505 if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
506 map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
507 map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
508 dist_scale_factor =h->dist_scale_factor_field[s->mb_y&1];
510 ref_offset = (h->ref_list[1][0].mbaff<<4) & (mb_type_col[0]>>3); //if(h->ref_list[1][0].mbaff && IS_INTERLACED(mb_type_col[0])) ref_offset=16 else 0
512 if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
513 int y_shift = 2*!IS_INTERLACED(*mb_type);
514 assert(h->sps.direct_8x8_inference_flag);
516 for(i8=0; i8<4; i8++){
518 const int y8 = i8>>1;
520 const int16_t (*l1mv)[2]= l1mv0;
522 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
524 h->sub_mb_type[i8] = sub_mb_type;
526 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
527 if(IS_INTRA(mb_type_col[y8])){
528 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
529 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
530 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
534 ref0 = l1ref0[x8 + y8*b8_stride];
536 ref0 = map_col_to_list0[0][ref0 + ref_offset];
538 ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
541 scale = dist_scale_factor[ref0];
542 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
545 const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
546 int my_col = (mv_col[1]<<y_shift)/2;
547 int mx = (scale * mv_col[0] + 128) >> 8;
548 int my = (scale * my_col + 128) >> 8;
549 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
550 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
556 /* one-to-one mv scaling */
558 if(IS_16X16(*mb_type)){
561 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
562 if(IS_INTRA(mb_type_col[0])){
565 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
566 : map_col_to_list0[1][l1ref1[0] + ref_offset];
567 const int scale = dist_scale_factor[ref0];
568 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
570 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
571 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
573 mv0= pack16to32(mv_l0[0],mv_l0[1]);
574 mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
576 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
577 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
578 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
580 for(i8=0; i8<4; i8++){
582 const int y8 = i8>>1;
584 const int16_t (*l1mv)[2]= l1mv0;
586 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
588 h->sub_mb_type[i8] = sub_mb_type;
589 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
590 if(IS_INTRA(mb_type_col[0])){
591 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
592 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
593 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
597 assert(b8_stride == 2);
600 ref0 = map_col_to_list0[0][ref0 + ref_offset];
602 ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
605 scale = dist_scale_factor[ref0];
607 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
608 if(IS_SUB_8X8(sub_mb_type)){
609 const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
610 int mx = (scale * mv_col[0] + 128) >> 8;
611 int my = (scale * mv_col[1] + 128) >> 8;
612 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
613 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
615 for(i4=0; i4<4; i4++){
616 const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
617 int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
618 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
619 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
620 AV_WN32A(h->mv_cache[1][scan8[i8*4+i4]],
621 pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]));
628 void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
629 if(h->direct_spatial_mv_pred){
630 pred_spatial_direct_motion(h, mb_type);
632 pred_temp_direct_motion(h, mb_type);