Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h264_direct.c @ d375c104

History | View | Annotate | Download (25.6 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
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
#include "thread.h"
35

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

    
39

    
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){
44
        return 256;
45
    }else{
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);
49
    }
50
}
51

    
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;
56
    int i, field;
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);
62
    }
63

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

    
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;
76

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

    
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];
83

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

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

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

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

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

    
121
    cur->mbaff= FRAME_MBAFF;
122

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

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

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

    
144
static void await_reference_mb_row(H264Context * const h, Picture *ref, int mb_y)
145
{
146
    int ref_field = ref->reference - 1;
147
    int ref_field_picture = ref->field_picture;
148
    int ref_height = 16*h->s.mb_height >> ref_field_picture;
149

    
150
    if(!HAVE_PTHREADS || !(h->s.avctx->active_thread_type&FF_THREAD_FRAME))
151
        return;
152

    
153
    //FIXME it can be safe to access mb stuff
154
    //even if pixels aren't deblocked yet
155

    
156
    ff_thread_await_progress((AVFrame*)ref, FFMIN(16*mb_y >> ref_field_picture, ref_height-1),
157
                             ref_field_picture && ref_field);
158
}
159

    
160
static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){
161
    MpegEncContext * const s = &h->s;
162
    int b8_stride = 2;
163
    int b4_stride = h->b_stride;
164
    int mb_xy = h->mb_xy, mb_y = s->mb_y;
165
    int mb_type_col[2];
166
    const int16_t (*l1mv0)[2], (*l1mv1)[2];
167
    const int8_t *l1ref0, *l1ref1;
168
    const int is_b8x8 = IS_8X8(*mb_type);
169
    unsigned int sub_mb_type= MB_TYPE_L0L1;
170
    int i8, i4;
171
    int ref[2];
172
    int mv[2];
173
    int list;
174

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

    
177
    await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type));
178

    
179
#define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
180

    
181

    
182
    /* ref = min(neighbors) */
183
    for(list=0; list<2; list++){
184
        int left_ref = h->ref_cache[list][scan8[0] - 1];
185
        int top_ref  = h->ref_cache[list][scan8[0] - 8];
186
        int refc = h->ref_cache[list][scan8[0] - 8 + 4];
187
        const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
188
        if(refc == PART_NOT_AVAILABLE){
189
            refc = h->ref_cache[list][scan8[0] - 8 - 1];
190
            C    = h-> mv_cache[list][scan8[0] - 8 - 1];
191
        }
192
        ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
193
        if(ref[list] >= 0){
194
            //this is just pred_motion() but with the cases removed that cannot happen for direct blocks
195
            const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
196
            const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
197

    
198
            int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
199
            if(match_count > 1){ //most common
200
                mv[list]= pack16to32(mid_pred(A[0], B[0], C[0]),
201
                                     mid_pred(A[1], B[1], C[1]) );
202
            }else {
203
                assert(match_count==1);
204
                if(left_ref==ref[list]){
205
                    mv[list]= AV_RN32A(A);
206
                }else if(top_ref==ref[list]){
207
                    mv[list]= AV_RN32A(B);
208
                }else{
209
                    mv[list]= AV_RN32A(C);
210
                }
211
            }
212
        }else{
213
            int mask= ~(MB_TYPE_L0 << (2*list));
214
            mv[list] = 0;
215
            ref[list] = -1;
216
            if(!is_b8x8)
217
                *mb_type &= mask;
218
            sub_mb_type &= mask;
219
        }
220
    }
221
    if(ref[0] < 0 && ref[1] < 0){
222
        ref[0] = ref[1] = 0;
223
        if(!is_b8x8)
224
            *mb_type |= MB_TYPE_L0L1;
225
        sub_mb_type |= MB_TYPE_L0L1;
226
    }
227

    
228
    if(!(is_b8x8|mv[0]|mv[1])){
229
        fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
230
        fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
231
        fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
232
        fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
233
        *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;
234
        return;
235
    }
236

    
237
    if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
238
        if(!IS_INTERLACED(*mb_type)){                    //     AFR/FR    -> AFL/FL
239
            mb_y = (s->mb_y&~1) + h->col_parity;
240
            mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
241
            b8_stride = 0;
242
        }else{
243
            mb_y  += h->col_fieldoff;
244
            mb_xy += s->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
245
        }
246
        goto single_col;
247
    }else{                                               // AFL/AFR/FR/FL -> AFR/FR
248
        if(IS_INTERLACED(*mb_type)){                     // AFL       /FL -> AFR/FR
249
            mb_y = s->mb_y&~1;
250
            mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
251
            mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
252
            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
253
            b8_stride = 2+4*s->mb_stride;
254
            b4_stride *= 6;
255

    
256
            sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
257
            if(    (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
258
                && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
259
                && !is_b8x8){
260
                *mb_type   |= MB_TYPE_16x8 |MB_TYPE_DIRECT2; /* B_16x8 */
261
            }else{
262
                *mb_type   |= MB_TYPE_8x8;
263
            }
264
        }else{                                           //     AFR/FR    -> AFR/FR
265
single_col:
266
            mb_type_col[0] =
267
            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
268

    
269
            sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
270
            if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
271
                *mb_type   |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_16x16 */
272
            }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
273
                *mb_type   |= MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
274
            }else{
275
                if(!h->sps.direct_8x8_inference_flag){
276
                    /* FIXME save sub mb types from previous frames (or derive from MVs)
277
                    * so we know exactly what block size to use */
278
                    sub_mb_type += (MB_TYPE_8x8-MB_TYPE_16x16); /* B_SUB_4x4 */
279
                }
280
                *mb_type   |= MB_TYPE_8x8;
281
            }
282
        }
283
    }
284

    
285
    await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
286

    
287
    l1mv0  = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
288
    l1mv1  = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
289
    l1ref0 = &h->ref_list[1][0].ref_index [0][4*mb_xy];
290
    l1ref1 = &h->ref_list[1][0].ref_index [1][4*mb_xy];
291
    if(!b8_stride){
292
        if(s->mb_y&1){
293
            l1ref0 += 2;
294
            l1ref1 += 2;
295
            l1mv0  +=  2*b4_stride;
296
            l1mv1  +=  2*b4_stride;
297
        }
298
    }
299

    
300

    
301
        if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
302
            int n=0;
303
            for(i8=0; i8<4; i8++){
304
                int x8 = i8&1;
305
                int y8 = i8>>1;
306
                int xy8 = x8+y8*b8_stride;
307
                int xy4 = 3*x8+y8*b4_stride;
308
                int a,b;
309

    
310
                if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
311
                    continue;
312
                h->sub_mb_type[i8] = sub_mb_type;
313

    
314
                fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
315
                fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
316
                if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
317
                   && (   (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
318
                       || (l1ref0[xy8]  < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
319
                    a=b=0;
320
                    if(ref[0] > 0)
321
                        a= mv[0];
322
                    if(ref[1] > 0)
323
                        b= mv[1];
324
                    n++;
325
                }else{
326
                    a= mv[0];
327
                    b= mv[1];
328
                }
329
                fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
330
                fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
331
            }
332
            if(!is_b8x8 && !(n&3))
333
                *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;
334
        }else if(IS_16X16(*mb_type)){
335
            int a,b;
336

    
337
            fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
338
            fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
339
            if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
340
               && (   (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
341
                   || (l1ref0[0]  < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
342
                       && h->x264_build>33U))){
343
                a=b=0;
344
                if(ref[0] > 0)
345
                    a= mv[0];
346
                if(ref[1] > 0)
347
                    b= mv[1];
348
            }else{
349
                a= mv[0];
350
                b= mv[1];
351
            }
352
            fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
353
            fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
354
        }else{
355
            int n=0;
356
            for(i8=0; i8<4; i8++){
357
                const int x8 = i8&1;
358
                const int y8 = i8>>1;
359

    
360
                if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
361
                    continue;
362
                h->sub_mb_type[i8] = sub_mb_type;
363

    
364
                fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, mv[0], 4);
365
                fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, mv[1], 4);
366
                fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
367
                fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
368

    
369
                assert(b8_stride==2);
370
                /* col_zero_flag */
371
                if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && (   l1ref0[i8] == 0
372
                                              || (l1ref0[i8] < 0 && l1ref1[i8] == 0
373
                                                  && h->x264_build>33U))){
374
                    const int16_t (*l1mv)[2]= l1ref0[i8] == 0 ? l1mv0 : l1mv1;
375
                    if(IS_SUB_8X8(sub_mb_type)){
376
                        const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
377
                        if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
378
                            if(ref[0] == 0)
379
                                fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
380
                            if(ref[1] == 0)
381
                                fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
382
                            n+=4;
383
                        }
384
                    }else{
385
                        int m=0;
386
                    for(i4=0; i4<4; i4++){
387
                        const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
388
                        if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
389
                            if(ref[0] == 0)
390
                                AV_ZERO32(h->mv_cache[0][scan8[i8*4+i4]]);
391
                            if(ref[1] == 0)
392
                                AV_ZERO32(h->mv_cache[1][scan8[i8*4+i4]]);
393
                            m++;
394
                        }
395
                    }
396
                    if(!(m&3))
397
                        h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;
398
                    n+=m;
399
                    }
400
                }
401
            }
402
            if(!is_b8x8 && !(n&15))
403
                *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;
404
        }
405
}
406

    
407
static void pred_temp_direct_motion(H264Context * const h, int *mb_type){
408
    MpegEncContext * const s = &h->s;
409
    int b8_stride = 2;
410
    int b4_stride = h->b_stride;
411
    int mb_xy = h->mb_xy, mb_y = s->mb_y;
412
    int mb_type_col[2];
413
    const int16_t (*l1mv0)[2], (*l1mv1)[2];
414
    const int8_t *l1ref0, *l1ref1;
415
    const int is_b8x8 = IS_8X8(*mb_type);
416
    unsigned int sub_mb_type;
417
    int i8, i4;
418

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

    
421
    await_reference_mb_row(h, &h->ref_list[1][0], s->mb_y + !!IS_INTERLACED(*mb_type));
422

    
423
    if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
424
        if(!IS_INTERLACED(*mb_type)){                    //     AFR/FR    -> AFL/FL
425
            mb_y = (s->mb_y&~1) + h->col_parity;
426
            mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
427
            b8_stride = 0;
428
        }else{
429
            mb_y  += h->col_fieldoff;
430
            mb_xy += s->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
431
        }
432
        goto single_col;
433
    }else{                                               // AFL/AFR/FR/FL -> AFR/FR
434
        if(IS_INTERLACED(*mb_type)){                     // AFL       /FL -> AFR/FR
435
            mb_y = s->mb_y&~1;
436
            mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
437
            mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
438
            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
439
            b8_stride = 2+4*s->mb_stride;
440
            b4_stride *= 6;
441

    
442
            sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
443

    
444
            if(    (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
445
                && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
446
                && !is_b8x8){
447
                *mb_type   |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
448
            }else{
449
                *mb_type   |= MB_TYPE_8x8|MB_TYPE_L0L1;
450
            }
451
        }else{                                           //     AFR/FR    -> AFR/FR
452
single_col:
453
            mb_type_col[0] =
454
            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
455

    
456
            sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
457
            if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
458
                *mb_type   |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
459
            }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
460
                *mb_type   |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
461
            }else{
462
                if(!h->sps.direct_8x8_inference_flag){
463
                    /* FIXME save sub mb types from previous frames (or derive from MVs)
464
                    * so we know exactly what block size to use */
465
                    sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
466
                }
467
                *mb_type   |= MB_TYPE_8x8|MB_TYPE_L0L1;
468
            }
469
        }
470
    }
471

    
472
    await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
473

    
474
    l1mv0  = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
475
    l1mv1  = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
476
    l1ref0 = &h->ref_list[1][0].ref_index [0][4*mb_xy];
477
    l1ref1 = &h->ref_list[1][0].ref_index [1][4*mb_xy];
478
    if(!b8_stride){
479
        if(s->mb_y&1){
480
            l1ref0 += 2;
481
            l1ref1 += 2;
482
            l1mv0  +=  2*b4_stride;
483
            l1mv1  +=  2*b4_stride;
484
        }
485
    }
486

    
487
    {
488
        const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
489
        const int *dist_scale_factor = h->dist_scale_factor;
490
        int ref_offset;
491

    
492
        if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
493
            map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
494
            map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
495
            dist_scale_factor   =h->dist_scale_factor_field[s->mb_y&1];
496
        }
497
        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
498

    
499
        if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
500
            int y_shift  = 2*!IS_INTERLACED(*mb_type);
501
            assert(h->sps.direct_8x8_inference_flag);
502

    
503
            for(i8=0; i8<4; i8++){
504
                const int x8 = i8&1;
505
                const int y8 = i8>>1;
506
                int ref0, scale;
507
                const int16_t (*l1mv)[2]= l1mv0;
508

    
509
                if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
510
                    continue;
511
                h->sub_mb_type[i8] = sub_mb_type;
512

    
513
                fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
514
                if(IS_INTRA(mb_type_col[y8])){
515
                    fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
516
                    fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
517
                    fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
518
                    continue;
519
                }
520

    
521
                ref0 = l1ref0[x8 + y8*b8_stride];
522
                if(ref0 >= 0)
523
                    ref0 = map_col_to_list0[0][ref0 + ref_offset];
524
                else{
525
                    ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
526
                    l1mv= l1mv1;
527
                }
528
                scale = dist_scale_factor[ref0];
529
                fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
530

    
531
                {
532
                    const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
533
                    int my_col = (mv_col[1]<<y_shift)/2;
534
                    int mx = (scale * mv_col[0] + 128) >> 8;
535
                    int my = (scale * my_col + 128) >> 8;
536
                    fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
537
                    fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
538
                }
539
            }
540
            return;
541
        }
542

    
543
        /* one-to-one mv scaling */
544

    
545
        if(IS_16X16(*mb_type)){
546
            int ref, mv0, mv1;
547

    
548
            fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
549
            if(IS_INTRA(mb_type_col[0])){
550
                ref=mv0=mv1=0;
551
            }else{
552
                const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
553
                                                : map_col_to_list0[1][l1ref1[0] + ref_offset];
554
                const int scale = dist_scale_factor[ref0];
555
                const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
556
                int mv_l0[2];
557
                mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
558
                mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
559
                ref= ref0;
560
                mv0= pack16to32(mv_l0[0],mv_l0[1]);
561
                mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
562
            }
563
            fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
564
            fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
565
            fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
566
        }else{
567
            for(i8=0; i8<4; i8++){
568
                const int x8 = i8&1;
569
                const int y8 = i8>>1;
570
                int ref0, scale;
571
                const int16_t (*l1mv)[2]= l1mv0;
572

    
573
                if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
574
                    continue;
575
                h->sub_mb_type[i8] = sub_mb_type;
576
                fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
577
                if(IS_INTRA(mb_type_col[0])){
578
                    fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
579
                    fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
580
                    fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
581
                    continue;
582
                }
583

    
584
                assert(b8_stride == 2);
585
                ref0 = l1ref0[i8];
586
                if(ref0 >= 0)
587
                    ref0 = map_col_to_list0[0][ref0 + ref_offset];
588
                else{
589
                    ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
590
                    l1mv= l1mv1;
591
                }
592
                scale = dist_scale_factor[ref0];
593

    
594
                fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
595
                if(IS_SUB_8X8(sub_mb_type)){
596
                    const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
597
                    int mx = (scale * mv_col[0] + 128) >> 8;
598
                    int my = (scale * mv_col[1] + 128) >> 8;
599
                    fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
600
                    fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
601
                }else
602
                for(i4=0; i4<4; i4++){
603
                    const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
604
                    int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
605
                    mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
606
                    mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
607
                    AV_WN32A(h->mv_cache[1][scan8[i8*4+i4]],
608
                        pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]));
609
                }
610
            }
611
        }
612
    }
613
}
614

    
615
void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
616
    if(h->direct_spatial_mv_pred){
617
        pred_spatial_direct_motion(h, mb_type);
618
    }else{
619
        pred_temp_direct_motion(h, mb_type);
620
    }
621
}