Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h264_direct.c @ 1be49ce5

History | View | Annotate | Download (21.8 KB)

1
/*
2
 * H.26L/H.264/AVC/JVT/14496-10/... direct mb/block decoding
3
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4
 *
5
 * This file is part of FFmpeg.
6
 *
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.
11
 *
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.
16
 *
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
20
 */
21

    
22
/**
23
 * @file libavcodec/h264_direct.c
24
 * H.264 / AVC / MPEG4 part10 direct mb/block decoding.
25
 * @author Michael Niedermayer <michaelni@gmx.at>
26
 */
27

    
28
#include "internal.h"
29
#include "dsputil.h"
30
#include "avcodec.h"
31
#include "mpegvideo.h"
32
#include "h264.h"
33
#include "rectangle.h"
34

    
35
//#undef NDEBUG
36
#include <assert.h>
37

    
38

    
39
static int get_scale_factor(H264Context * const h, int poc, int poc1, int i){
40
    int poc0 = h->ref_list[0][i].poc;
41
    int td = av_clip(poc1 - poc0, -128, 127);
42
    if(td == 0 || h->ref_list[0][i].long_ref){
43
        return 256;
44
    }else{
45
        int tb = av_clip(poc - poc0, -128, 127);
46
        int tx = (16384 + (FFABS(td) >> 1)) / td;
47
        return av_clip((tb*tx + 32) >> 6, -1024, 1023);
48
    }
49
}
50

    
51
void ff_h264_direct_dist_scale_factor(H264Context * const h){
52
    MpegEncContext * const s = &h->s;
53
    const int poc = h->s.current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
54
    const int poc1 = h->ref_list[1][0].poc;
55
    int i, field;
56
    for(field=0; field<2; field++){
57
        const int poc  = h->s.current_picture_ptr->field_poc[field];
58
        const int poc1 = h->ref_list[1][0].field_poc[field];
59
        for(i=0; i < 2*h->ref_count[0]; i++)
60
            h->dist_scale_factor_field[field][i^field] = get_scale_factor(h, poc, poc1, i+16);
61
    }
62

    
63
    for(i=0; i<h->ref_count[0]; i++){
64
        h->dist_scale_factor[i] = get_scale_factor(h, poc, poc1, i);
65
    }
66
}
67

    
68
static void fill_colmap(H264Context *h, int map[2][16+32], int list, int field, int colfield, int mbafi){
69
    MpegEncContext * const s = &h->s;
70
    Picture * const ref1 = &h->ref_list[1][0];
71
    int j, old_ref, rfield;
72
    int start= mbafi ? 16                      : 0;
73
    int end  = mbafi ? 16+2*h->ref_count[0]    : h->ref_count[0];
74
    int interl= mbafi || s->picture_structure != PICT_FRAME;
75

    
76
    /* bogus; fills in for missing frames */
77
    memset(map[list], 0, sizeof(map[list]));
78

    
79
    for(rfield=0; rfield<2; rfield++){
80
        for(old_ref=0; old_ref<ref1->ref_count[colfield][list]; old_ref++){
81
            int poc = ref1->ref_poc[colfield][list][old_ref];
82

    
83
            if     (!interl)
84
                poc |= 3;
85
            else if( interl && (poc&3) == 3) //FIXME store all MBAFF references so this isnt needed
86
                poc= (poc&~3) + rfield + 1;
87

    
88
            for(j=start; j<end; j++){
89
                if(4*h->ref_list[0][j].frame_num + (h->ref_list[0][j].reference&3) == poc){
90
                    int cur_ref= mbafi ? (j-16)^field : j;
91
                    map[list][2*old_ref + (rfield^field) + 16] = cur_ref;
92
                    if(rfield == field || !interl)
93
                        map[list][old_ref] = cur_ref;
94
                    break;
95
                }
96
            }
97
        }
98
    }
99
}
100

    
101
void ff_h264_direct_ref_list_init(H264Context * const h){
102
    MpegEncContext * const s = &h->s;
103
    Picture * const ref1 = &h->ref_list[1][0];
104
    Picture * const cur = s->current_picture_ptr;
105
    int list, j, field;
106
    int sidx= (s->picture_structure&1)^1;
107
    int ref1sidx= (ref1->reference&1)^1;
108

    
109
    for(list=0; list<2; list++){
110
        cur->ref_count[sidx][list] = h->ref_count[list];
111
        for(j=0; j<h->ref_count[list]; j++)
112
            cur->ref_poc[sidx][list][j] = 4*h->ref_list[list][j].frame_num + (h->ref_list[list][j].reference&3);
113
    }
114

    
115
    if(s->picture_structure == PICT_FRAME){
116
        memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
117
        memcpy(cur->ref_poc  [1], cur->ref_poc  [0], sizeof(cur->ref_poc  [0]));
118
    }
119

    
120
    cur->mbaff= FRAME_MBAFF;
121

    
122
    h->col_fieldoff= 0;
123
    if(s->picture_structure == PICT_FRAME){
124
        int cur_poc = s->current_picture_ptr->poc;
125
        int *col_poc = h->ref_list[1]->field_poc;
126
        h->col_parity= (FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc));
127
        ref1sidx=sidx= h->col_parity;
128
    }else if(!(s->picture_structure & h->ref_list[1][0].reference) && !h->ref_list[1][0].mbaff){ // FL -> FL & differ parity
129
        h->col_fieldoff= s->mb_stride*(2*(h->ref_list[1][0].reference) - 3);
130
    }
131

    
132
    if(cur->pict_type != FF_B_TYPE || h->direct_spatial_mv_pred)
133
        return;
134

    
135
    for(list=0; list<2; list++){
136
        fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
137
        if(FRAME_MBAFF)
138
        for(field=0; field<2; field++)
139
            fill_colmap(h, h->map_col_to_list0_field[field], list, field, field, 1);
140
    }
141
}
142

    
143
void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
144
    MpegEncContext * const s = &h->s;
145
    int b8_stride = h->b8_stride;
146
    int b4_stride = h->b_stride;
147
    int mb_xy = h->mb_xy;
148
    int mb_type_col[2];
149
    const int16_t (*l1mv0)[2], (*l1mv1)[2];
150
    const int8_t *l1ref0, *l1ref1;
151
    const int is_b8x8 = IS_8X8(*mb_type);
152
    unsigned int sub_mb_type;
153
    int i8, i4;
154

    
155
    assert(h->ref_list[1][0].reference&3);
156

    
157
#define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
158

    
159
    if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
160
        if(!IS_INTERLACED(*mb_type)){                    //     AFR/FR    -> AFL/FL
161
            mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
162
            b8_stride = 0;
163
        }else{
164
            mb_xy += h->col_fieldoff; // non zero for FL -> FL & differ parity
165
        }
166
        goto single_col;
167
    }else{                                               // AFL/AFR/FR/FL -> AFR/FR
168
        if(IS_INTERLACED(*mb_type)){                     // AFL       /FL -> AFR/FR
169
            mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
170
            mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
171
            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
172
            b8_stride *= 3;
173
            b4_stride *= 6;
174

    
175
            sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
176
            if(    (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
177
                && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
178
                && !is_b8x8){
179
                *mb_type   |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
180
            }else{
181
                *mb_type   |= MB_TYPE_8x8|MB_TYPE_L0L1;
182
            }
183
        }else{                                           //     AFR/FR    -> AFR/FR
184
single_col:
185
            mb_type_col[0] =
186
            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
187
            if(IS_8X8(mb_type_col[0]) && !h->sps.direct_8x8_inference_flag){
188
                /* FIXME save sub mb types from previous frames (or derive from MVs)
189
                * so we know exactly what block size to use */
190
                sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
191
                *mb_type   |= MB_TYPE_8x8|MB_TYPE_L0L1;
192
            }else if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
193
                sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
194
                *mb_type   |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
195
            }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
196
                sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
197
                *mb_type   |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
198
            }else{
199
                sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
200
                *mb_type   |= MB_TYPE_8x8|MB_TYPE_L0L1;
201
            }
202
        }
203
    }
204

    
205
    l1mv0  = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
206
    l1mv1  = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
207
    l1ref0 = &h->ref_list[1][0].ref_index [0][h->mb2b8_xy[mb_xy]];
208
    l1ref1 = &h->ref_list[1][0].ref_index [1][h->mb2b8_xy[mb_xy]];
209
    if(!b8_stride){
210
        if(s->mb_y&1){
211
            l1ref0 += h->b8_stride;
212
            l1ref1 += h->b8_stride;
213
            l1mv0  +=  2*b4_stride;
214
            l1mv1  +=  2*b4_stride;
215
        }
216
    }
217

    
218
    if(h->direct_spatial_mv_pred){
219
        int ref[2];
220
        int mv[2][2];
221
        int list;
222

    
223
        /* ref = min(neighbors) */
224
        for(list=0; list<2; list++){
225
            int left_ref = h->ref_cache[list][scan8[0] - 1];
226
            int top_ref  = h->ref_cache[list][scan8[0] - 8];
227
            int refc = h->ref_cache[list][scan8[0] - 8 + 4];
228
            const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
229
            if(refc == PART_NOT_AVAILABLE){
230
                refc = h->ref_cache[list][scan8[0] - 8 - 1];
231
                C    = h-> mv_cache[list][scan8[0] - 8 - 1];
232
            }
233
            ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
234
            if(ref[list] >= 0){
235
                //this is just pred_motion() but with the cases removed that cannot happen for direct blocks
236
                const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
237
                const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
238

    
239
                int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
240
                if(match_count > 1){ //most common
241
                    mv[list][0]= mid_pred(A[0], B[0], C[0]);
242
                    mv[list][1]= mid_pred(A[1], B[1], C[1]);
243
                }else {
244
                    assert(match_count==1);
245
                    if(left_ref==ref[list]){
246
                        mv[list][0]= A[0];
247
                        mv[list][1]= A[1];
248
                    }else if(top_ref==ref[list]){
249
                        mv[list][0]= B[0];
250
                        mv[list][1]= B[1];
251
                    }else{
252
                        mv[list][0]= C[0];
253
                        mv[list][1]= C[1];
254
                    }
255
                }
256
            }else{
257
                int mask= ~(MB_TYPE_L0 << (2*list));
258
                mv[list][0] = mv[list][1] = 0;
259
                ref[list] = -1;
260
                if(!is_b8x8)
261
                    *mb_type &= mask;
262
                sub_mb_type &= mask;
263
            }
264
        }
265
        if(ref[0] < 0 && ref[1] < 0){
266
            ref[0] = ref[1] = 0;
267
            if(!is_b8x8)
268
                *mb_type |= MB_TYPE_L0L1;
269
            sub_mb_type |= MB_TYPE_L0L1;
270
        }
271

    
272
        if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
273
            int n=0;
274
            for(i8=0; i8<4; i8++){
275
                int x8 = i8&1;
276
                int y8 = i8>>1;
277
                int xy8 = x8+y8*b8_stride;
278
                int xy4 = 3*x8+y8*b4_stride;
279
                int a,b;
280

    
281
                if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
282
                    continue;
283
                h->sub_mb_type[i8] = sub_mb_type;
284

    
285
                fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
286
                fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
287
                if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
288
                   && (   (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
289
                       || (l1ref0[xy8]  < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
290
                    a=b=0;
291
                    if(ref[0] > 0)
292
                        a= pack16to32(mv[0][0],mv[0][1]);
293
                    if(ref[1] > 0)
294
                        b= pack16to32(mv[1][0],mv[1][1]);
295
                    n++;
296
                }else{
297
                    a= pack16to32(mv[0][0],mv[0][1]);
298
                    b= pack16to32(mv[1][0],mv[1][1]);
299
                }
300
                fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
301
                fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
302
            }
303
            if(!is_b8x8 && !(n&3))
304
                *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;
305
        }else if(IS_16X16(*mb_type)){
306
            int a,b;
307

    
308
            fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
309
            fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
310
            if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
311
               && (   (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
312
                   || (l1ref0[0]  < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
313
                       && h->x264_build>33U))){
314
                a=b=0;
315
                if(ref[0] > 0)
316
                    a= pack16to32(mv[0][0],mv[0][1]);
317
                if(ref[1] > 0)
318
                    b= pack16to32(mv[1][0],mv[1][1]);
319
            }else{
320
                a= pack16to32(mv[0][0],mv[0][1]);
321
                b= pack16to32(mv[1][0],mv[1][1]);
322
            }
323
            fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
324
            fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
325
        }else{
326
            int n=0;
327
            for(i8=0; i8<4; i8++){
328
                const int x8 = i8&1;
329
                const int y8 = i8>>1;
330

    
331
                if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
332
                    continue;
333
                h->sub_mb_type[i8] = sub_mb_type;
334

    
335
                fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
336
                fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
337
                fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
338
                fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
339

    
340
                /* col_zero_flag */
341
                if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && (   l1ref0[x8 + y8*b8_stride] == 0
342
                                              || (l1ref0[x8 + y8*b8_stride] < 0 && l1ref1[x8 + y8*b8_stride] == 0
343
                                                  && h->x264_build>33U))){
344
                    const int16_t (*l1mv)[2]= l1ref0[x8 + y8*b8_stride] == 0 ? l1mv0 : l1mv1;
345
                    if(IS_SUB_8X8(sub_mb_type)){
346
                        const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
347
                        if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
348
                            if(ref[0] == 0)
349
                                fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
350
                            if(ref[1] == 0)
351
                                fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
352
                            n+=4;
353
                        }
354
                    }else{
355
                        int m=0;
356
                    for(i4=0; i4<4; i4++){
357
                        const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
358
                        if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
359
                            if(ref[0] == 0)
360
                                *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
361
                            if(ref[1] == 0)
362
                                *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
363
                            m++;
364
                        }
365
                    }
366
                    if(!(m&3))
367
                        h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;
368
                    n+=m;
369
                    }
370
                }
371
            }
372
            if(!is_b8x8 && !(n&15))
373
                *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;
374
        }
375
    }else{ /* direct temporal mv pred */
376
        const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
377
        const int *dist_scale_factor = h->dist_scale_factor;
378
        int ref_offset;
379

    
380
        if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
381
            map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
382
            map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
383
            dist_scale_factor   =h->dist_scale_factor_field[s->mb_y&1];
384
        }
385
        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
386

    
387
        if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
388
            int y_shift  = 2*!IS_INTERLACED(*mb_type);
389
            assert(h->sps.direct_8x8_inference_flag);
390

    
391
            for(i8=0; i8<4; i8++){
392
                const int x8 = i8&1;
393
                const int y8 = i8>>1;
394
                int ref0, scale;
395
                const int16_t (*l1mv)[2]= l1mv0;
396

    
397
                if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
398
                    continue;
399
                h->sub_mb_type[i8] = sub_mb_type;
400

    
401
                fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
402
                if(IS_INTRA(mb_type_col[y8])){
403
                    fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
404
                    fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
405
                    fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
406
                    continue;
407
                }
408

    
409
                ref0 = l1ref0[x8 + y8*b8_stride];
410
                if(ref0 >= 0)
411
                    ref0 = map_col_to_list0[0][ref0 + ref_offset];
412
                else{
413
                    ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
414
                    l1mv= l1mv1;
415
                }
416
                scale = dist_scale_factor[ref0];
417
                fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
418

    
419
                {
420
                    const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
421
                    int my_col = (mv_col[1]<<y_shift)/2;
422
                    int mx = (scale * mv_col[0] + 128) >> 8;
423
                    int my = (scale * my_col + 128) >> 8;
424
                    fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
425
                    fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
426
                }
427
            }
428
            return;
429
        }
430

    
431
        /* one-to-one mv scaling */
432

    
433
        if(IS_16X16(*mb_type)){
434
            int ref, mv0, mv1;
435

    
436
            fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
437
            if(IS_INTRA(mb_type_col[0])){
438
                ref=mv0=mv1=0;
439
            }else{
440
                const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
441
                                                : map_col_to_list0[1][l1ref1[0] + ref_offset];
442
                const int scale = dist_scale_factor[ref0];
443
                const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
444
                int mv_l0[2];
445
                mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
446
                mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
447
                ref= ref0;
448
                mv0= pack16to32(mv_l0[0],mv_l0[1]);
449
                mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
450
            }
451
            fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
452
            fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
453
            fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
454
        }else{
455
            for(i8=0; i8<4; i8++){
456
                const int x8 = i8&1;
457
                const int y8 = i8>>1;
458
                int ref0, scale;
459
                const int16_t (*l1mv)[2]= l1mv0;
460

    
461
                if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
462
                    continue;
463
                h->sub_mb_type[i8] = sub_mb_type;
464
                fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
465
                if(IS_INTRA(mb_type_col[0])){
466
                    fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
467
                    fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
468
                    fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
469
                    continue;
470
                }
471

    
472
                ref0 = l1ref0[x8 + y8*b8_stride];
473
                if(ref0 >= 0)
474
                    ref0 = map_col_to_list0[0][ref0 + ref_offset];
475
                else{
476
                    ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
477
                    l1mv= l1mv1;
478
                }
479
                scale = dist_scale_factor[ref0];
480

    
481
                fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
482
                if(IS_SUB_8X8(sub_mb_type)){
483
                    const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
484
                    int mx = (scale * mv_col[0] + 128) >> 8;
485
                    int my = (scale * mv_col[1] + 128) >> 8;
486
                    fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
487
                    fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
488
                }else
489
                for(i4=0; i4<4; i4++){
490
                    const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
491
                    int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
492
                    mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
493
                    mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
494
                    *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
495
                        pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
496
                }
497
            }
498
        }
499
    }
500
}