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 "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;
57 for(field=0; field<2; field++){
58 const int poc = h->s.current_picture_ptr->field_poc[field];
59 const int poc1 = h->ref_list[1][0].field_poc[field];
60 for(i=0; i < 2*h->ref_count[0]; i++)
61 h->dist_scale_factor_field[field][i^field] = get_scale_factor(h, poc, poc1, i+16);
64 for(i=0; i<h->ref_count[0]; i++){
65 h->dist_scale_factor[i] = get_scale_factor(h, poc, poc1, i);
69 static void fill_colmap(H264Context *h, int map[2][16+32], int list, int field, int colfield, int mbafi){
70 MpegEncContext * const s = &h->s;
71 Picture * const ref1 = &h->ref_list[1][0];
72 int j, old_ref, rfield;
73 int start= mbafi ? 16 : 0;
74 int end = mbafi ? 16+2*h->ref_count[0] : h->ref_count[0];
75 int interl= mbafi || s->picture_structure != PICT_FRAME;
77 /* bogus; fills in for missing frames */
78 memset(map[list], 0, sizeof(map[list]));
80 for(rfield=0; rfield<2; rfield++){
81 for(old_ref=0; old_ref<ref1->ref_count[colfield][list]; old_ref++){
82 int poc = ref1->ref_poc[colfield][list][old_ref];
86 else if( interl && (poc&3) == 3) //FIXME store all MBAFF references so this isnt needed
87 poc= (poc&~3) + rfield + 1;
89 for(j=start; j<end; j++){
90 if (4 * h->ref_list[0][j].frame_num + (h->ref_list[0][j].f.reference & 3) == poc) {
91 int cur_ref= mbafi ? (j-16)^field : j;
93 map[list][2*old_ref + (rfield^field) + 16] = cur_ref;
94 if(rfield == field || !interl)
95 map[list][old_ref] = cur_ref;
103 void ff_h264_direct_ref_list_init(H264Context * const h){
104 MpegEncContext * const s = &h->s;
105 Picture * const ref1 = &h->ref_list[1][0];
106 Picture * const cur = s->current_picture_ptr;
108 int sidx= (s->picture_structure&1)^1;
109 int ref1sidx = (ref1->f.reference&1)^1;
111 for(list=0; list<2; list++){
112 cur->ref_count[sidx][list] = h->ref_count[list];
113 for(j=0; j<h->ref_count[list]; j++)
114 cur->ref_poc[sidx][list][j] = 4 * h->ref_list[list][j].frame_num + (h->ref_list[list][j].f.reference & 3);
117 if(s->picture_structure == PICT_FRAME){
118 memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
119 memcpy(cur->ref_poc [1], cur->ref_poc [0], sizeof(cur->ref_poc [0]));
122 cur->mbaff= FRAME_MBAFF;
125 if(s->picture_structure == PICT_FRAME){
126 int cur_poc = s->current_picture_ptr->poc;
127 int *col_poc = h->ref_list[1]->field_poc;
128 h->col_parity= (FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc));
129 ref1sidx=sidx= h->col_parity;
130 } else if (!(s->picture_structure & h->ref_list[1][0].f.reference) && !h->ref_list[1][0].mbaff) { // FL -> FL & differ parity
131 h->col_fieldoff = 2 * h->ref_list[1][0].f.reference - 3;
134 if (cur->f.pict_type != AV_PICTURE_TYPE_B || h->direct_spatial_mv_pred)
137 for(list=0; list<2; list++){
138 fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
140 for(field=0; field<2; field++)
141 fill_colmap(h, h->map_col_to_list0_field[field], list, field, field, 1);
145 static void await_reference_mb_row(H264Context * const h, Picture *ref, int mb_y)
147 int ref_field = ref->f.reference - 1;
148 int ref_field_picture = ref->field_picture;
149 int ref_height = 16*h->s.mb_height >> ref_field_picture;
151 if(!HAVE_THREADS || !(h->s.avctx->active_thread_type&FF_THREAD_FRAME))
154 //FIXME it can be safe to access mb stuff
155 //even if pixels aren't deblocked yet
157 ff_thread_await_progress((AVFrame*)ref, FFMIN(16*mb_y >> ref_field_picture, ref_height-1),
158 ref_field_picture && ref_field);
161 static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){
162 MpegEncContext * const s = &h->s;
164 int b4_stride = h->b_stride;
165 int mb_xy = h->mb_xy, mb_y = s->mb_y;
167 const int16_t (*l1mv0)[2], (*l1mv1)[2];
168 const int8_t *l1ref0, *l1ref1;
169 const int is_b8x8 = IS_8X8(*mb_type);
170 unsigned int sub_mb_type= MB_TYPE_L0L1;
176 assert(h->ref_list[1][0].f.reference & 3);
178 await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type));
180 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
183 /* ref = min(neighbors) */
184 for(list=0; list<2; list++){
185 int left_ref = h->ref_cache[list][scan8[0] - 1];
186 int top_ref = h->ref_cache[list][scan8[0] - 8];
187 int refc = h->ref_cache[list][scan8[0] - 8 + 4];
188 const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
189 if(refc == PART_NOT_AVAILABLE){
190 refc = h->ref_cache[list][scan8[0] - 8 - 1];
191 C = h-> mv_cache[list][scan8[0] - 8 - 1];
193 ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
195 //this is just pred_motion() but with the cases removed that cannot happen for direct blocks
196 const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
197 const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
199 int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
200 if(match_count > 1){ //most common
201 mv[list]= pack16to32(mid_pred(A[0], B[0], C[0]),
202 mid_pred(A[1], B[1], C[1]) );
204 assert(match_count==1);
205 if(left_ref==ref[list]){
206 mv[list]= AV_RN32A(A);
207 }else if(top_ref==ref[list]){
208 mv[list]= AV_RN32A(B);
210 mv[list]= AV_RN32A(C);
214 int mask= ~(MB_TYPE_L0 << (2*list));
222 if(ref[0] < 0 && ref[1] < 0){
225 *mb_type |= MB_TYPE_L0L1;
226 sub_mb_type |= MB_TYPE_L0L1;
229 if(!(is_b8x8|mv[0]|mv[1])){
230 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
231 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
232 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
233 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
234 *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;
238 if (IS_INTERLACED(h->ref_list[1][0].f.mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
239 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
240 mb_y = (s->mb_y&~1) + h->col_parity;
241 mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
244 mb_y += h->col_fieldoff;
245 mb_xy += s->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
248 }else{ // AFL/AFR/FR/FL -> AFR/FR
249 if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
251 mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
252 mb_type_col[0] = h->ref_list[1][0].f.mb_type[mb_xy];
253 mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy + s->mb_stride];
254 b8_stride = 2+4*s->mb_stride;
256 if(IS_INTERLACED(mb_type_col[0]) != IS_INTERLACED(mb_type_col[1])){
257 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
258 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
261 sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
262 if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
263 && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
265 *mb_type |= MB_TYPE_16x8 |MB_TYPE_DIRECT2; /* B_16x8 */
267 *mb_type |= MB_TYPE_8x8;
269 }else{ // AFR/FR -> AFR/FR
272 mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy];
274 sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
275 if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
276 *mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_16x16 */
277 }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
278 *mb_type |= MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
280 if(!h->sps.direct_8x8_inference_flag){
281 /* FIXME save sub mb types from previous frames (or derive from MVs)
282 * so we know exactly what block size to use */
283 sub_mb_type += (MB_TYPE_8x8-MB_TYPE_16x16); /* B_SUB_4x4 */
285 *mb_type |= MB_TYPE_8x8;
290 await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
292 l1mv0 = &h->ref_list[1][0].f.motion_val[0][h->mb2b_xy [mb_xy]];
293 l1mv1 = &h->ref_list[1][0].f.motion_val[1][h->mb2b_xy [mb_xy]];
294 l1ref0 = &h->ref_list[1][0].f.ref_index [0][4 * mb_xy];
295 l1ref1 = &h->ref_list[1][0].f.ref_index [1][4 * mb_xy];
300 l1mv0 += 2*b4_stride;
301 l1mv1 += 2*b4_stride;
306 if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
308 for(i8=0; i8<4; i8++){
311 int xy8 = x8+y8*b8_stride;
312 int xy4 = 3*x8+y8*b4_stride;
315 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
317 h->sub_mb_type[i8] = sub_mb_type;
319 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
320 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
321 if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
322 && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
323 || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
334 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
335 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
337 if(!is_b8x8 && !(n&3))
338 *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;
339 }else if(IS_16X16(*mb_type)){
342 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
343 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
344 if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
345 && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
346 || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
347 && h->x264_build>33U))){
357 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
358 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
361 for(i8=0; i8<4; i8++){
363 const int y8 = i8>>1;
365 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
367 h->sub_mb_type[i8] = sub_mb_type;
369 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, mv[0], 4);
370 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, mv[1], 4);
371 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
372 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
374 assert(b8_stride==2);
376 if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && ( l1ref0[i8] == 0
377 || (l1ref0[i8] < 0 && l1ref1[i8] == 0
378 && h->x264_build>33U))){
379 const int16_t (*l1mv)[2]= l1ref0[i8] == 0 ? l1mv0 : l1mv1;
380 if(IS_SUB_8X8(sub_mb_type)){
381 const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
382 if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
384 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
386 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
391 for(i4=0; i4<4; i4++){
392 const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
393 if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
395 AV_ZERO32(h->mv_cache[0][scan8[i8*4+i4]]);
397 AV_ZERO32(h->mv_cache[1][scan8[i8*4+i4]]);
402 h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;
407 if(!is_b8x8 && !(n&15))
408 *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;
412 static void pred_temp_direct_motion(H264Context * const h, int *mb_type){
413 MpegEncContext * const s = &h->s;
415 int b4_stride = h->b_stride;
416 int mb_xy = h->mb_xy, mb_y = s->mb_y;
418 const int16_t (*l1mv0)[2], (*l1mv1)[2];
419 const int8_t *l1ref0, *l1ref1;
420 const int is_b8x8 = IS_8X8(*mb_type);
421 unsigned int sub_mb_type;
424 assert(h->ref_list[1][0].f.reference & 3);
426 await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type));
428 if (IS_INTERLACED(h->ref_list[1][0].f.mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
429 if (!IS_INTERLACED(*mb_type)) { // AFR/FR -> AFL/FL
430 mb_y = (s->mb_y&~1) + h->col_parity;
431 mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
434 mb_y += h->col_fieldoff;
435 mb_xy += s->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
438 }else{ // AFL/AFR/FR/FL -> AFR/FR
439 if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
441 mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
442 mb_type_col[0] = h->ref_list[1][0].f.mb_type[mb_xy];
443 mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy + s->mb_stride];
444 b8_stride = 2+4*s->mb_stride;
447 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
449 if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
450 && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
452 *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
454 *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
456 }else{ // AFR/FR -> AFR/FR
459 mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy];
461 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
462 if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
463 *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
464 }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
465 *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
467 if(!h->sps.direct_8x8_inference_flag){
468 /* FIXME save sub mb types from previous frames (or derive from MVs)
469 * so we know exactly what block size to use */
470 sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
472 *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
477 await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
479 l1mv0 = &h->ref_list[1][0].f.motion_val[0][h->mb2b_xy [mb_xy]];
480 l1mv1 = &h->ref_list[1][0].f.motion_val[1][h->mb2b_xy [mb_xy]];
481 l1ref0 = &h->ref_list[1][0].f.ref_index [0][4 * mb_xy];
482 l1ref1 = &h->ref_list[1][0].f.ref_index [1][4 * mb_xy];
487 l1mv0 += 2*b4_stride;
488 l1mv1 += 2*b4_stride;
493 const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
494 const int *dist_scale_factor = h->dist_scale_factor;
497 if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
498 map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
499 map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
500 dist_scale_factor =h->dist_scale_factor_field[s->mb_y&1];
502 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
504 if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
505 int y_shift = 2*!IS_INTERLACED(*mb_type);
506 assert(h->sps.direct_8x8_inference_flag);
508 for(i8=0; i8<4; i8++){
510 const int y8 = i8>>1;
512 const int16_t (*l1mv)[2]= l1mv0;
514 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
516 h->sub_mb_type[i8] = sub_mb_type;
518 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
519 if(IS_INTRA(mb_type_col[y8])){
520 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
521 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
522 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
526 ref0 = l1ref0[x8 + y8*b8_stride];
528 ref0 = map_col_to_list0[0][ref0 + ref_offset];
530 ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
533 scale = dist_scale_factor[ref0];
534 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
537 const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
538 int my_col = (mv_col[1]<<y_shift)/2;
539 int mx = (scale * mv_col[0] + 128) >> 8;
540 int my = (scale * my_col + 128) >> 8;
541 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
542 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
548 /* one-to-one mv scaling */
550 if(IS_16X16(*mb_type)){
553 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
554 if(IS_INTRA(mb_type_col[0])){
557 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
558 : map_col_to_list0[1][l1ref1[0] + ref_offset];
559 const int scale = dist_scale_factor[ref0];
560 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
562 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
563 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
565 mv0= pack16to32(mv_l0[0],mv_l0[1]);
566 mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
568 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
569 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
570 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
572 for(i8=0; i8<4; i8++){
574 const int y8 = i8>>1;
576 const int16_t (*l1mv)[2]= l1mv0;
578 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
580 h->sub_mb_type[i8] = sub_mb_type;
581 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
582 if(IS_INTRA(mb_type_col[0])){
583 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
584 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
585 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
589 assert(b8_stride == 2);
592 ref0 = map_col_to_list0[0][ref0 + ref_offset];
594 ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
597 scale = dist_scale_factor[ref0];
599 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
600 if(IS_SUB_8X8(sub_mb_type)){
601 const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
602 int mx = (scale * mv_col[0] + 128) >> 8;
603 int my = (scale * mv_col[1] + 128) >> 8;
604 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
605 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
607 for(i4=0; i4<4; i4++){
608 const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
609 int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
610 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
611 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
612 AV_WN32A(h->mv_cache[1][scan8[i8*4+i4]],
613 pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]));
620 void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
621 if(h->direct_spatial_mv_pred){
622 pred_spatial_direct_motion(h, mb_type);
624 pred_temp_direct_motion(h, mb_type);