Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h264_direct.c @ cbfcde9f

History | View | Annotate | Download (24.6 KB)

1 943f69a6 Michael Niedermayer
/*
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 488dba70 Michael Niedermayer
    int end  = mbafi ? 16+2*h->ref_count[0]    : h->ref_count[0];
74 943f69a6 Michael Niedermayer
    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 488dba70 Michael Niedermayer
                if(4*h->ref_list[0][j].frame_num + (h->ref_list[0][j].reference&3) == poc){
90 943f69a6 Michael Niedermayer
                    int cur_ref= mbafi ? (j-16)^field : j;
91
                    map[list][2*old_ref + (rfield^field) + 16] = cur_ref;
92 488dba70 Michael Niedermayer
                    if(rfield == field || !interl)
93 943f69a6 Michael Niedermayer
                        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 0a8dca87 Michael Niedermayer
    h->col_fieldoff= 0;
123 d88ea354 Michael Niedermayer
    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 0a8dca87 Michael Niedermayer
        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 d88ea354 Michael Niedermayer
    }
131
132 0a8dca87 Michael Niedermayer
    if(cur->pict_type != FF_B_TYPE || h->direct_spatial_mv_pred)
133
        return;
134
135 943f69a6 Michael Niedermayer
    for(list=0; list<2; list++){
136
        fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
137 33a6c0c9 Michael Niedermayer
        if(FRAME_MBAFF)
138 943f69a6 Michael Niedermayer
        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 edb4df43 Michael Niedermayer
static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){
144 943f69a6 Michael Niedermayer
    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 752cc0dc Michael Niedermayer
    unsigned int sub_mb_type= MB_TYPE_L0L1;;
153 943f69a6 Michael Niedermayer
    int i8, i4;
154 af0d6581 Michael Niedermayer
    int ref[2];
155
    int mv[2];
156
    int list;
157 943f69a6 Michael Niedermayer
158
    assert(h->ref_list[1][0].reference&3);
159
160
#define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
161
162 af0d6581 Michael Niedermayer
163
    /* ref = min(neighbors) */
164
    for(list=0; list<2; list++){
165
        int left_ref = h->ref_cache[list][scan8[0] - 1];
166
        int top_ref  = h->ref_cache[list][scan8[0] - 8];
167
        int refc = h->ref_cache[list][scan8[0] - 8 + 4];
168
        const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
169
        if(refc == PART_NOT_AVAILABLE){
170
            refc = h->ref_cache[list][scan8[0] - 8 - 1];
171
            C    = h-> mv_cache[list][scan8[0] - 8 - 1];
172
        }
173
        ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
174
        if(ref[list] >= 0){
175
            //this is just pred_motion() but with the cases removed that cannot happen for direct blocks
176
            const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
177
            const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
178
179
            int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
180
            if(match_count > 1){ //most common
181 cbfcde9f Michael Niedermayer
                mv[list]= pack16to32(mid_pred(A[0], B[0], C[0]),
182
                                     mid_pred(A[1], B[1], C[1]) );
183 af0d6581 Michael Niedermayer
            }else {
184
                assert(match_count==1);
185
                if(left_ref==ref[list]){
186
                    mv[list]= *(uint32_t*)A;
187
                }else if(top_ref==ref[list]){
188
                    mv[list]= *(uint32_t*)B;
189
                }else{
190
                    mv[list]= *(uint32_t*)C;
191
                }
192
            }
193
        }else{
194
            int mask= ~(MB_TYPE_L0 << (2*list));
195
            mv[list] = 0;
196
            ref[list] = -1;
197
            if(!is_b8x8)
198
                *mb_type &= mask;
199
            sub_mb_type &= mask;
200
        }
201
    }
202
    if(ref[0] < 0 && ref[1] < 0){
203
        ref[0] = ref[1] = 0;
204
        if(!is_b8x8)
205
            *mb_type |= MB_TYPE_L0L1;
206
        sub_mb_type |= MB_TYPE_L0L1;
207
    }
208
209 5134ad53 Michael Niedermayer
    if(!(is_b8x8|mv[0]|mv[1])){
210 af0d6581 Michael Niedermayer
        fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
211
        fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
212
        fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
213
        fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
214
        *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;
215
        return;
216
    }
217
218 943f69a6 Michael Niedermayer
    if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
219
        if(!IS_INTERLACED(*mb_type)){                    //     AFR/FR    -> AFL/FL
220 0a8dca87 Michael Niedermayer
            mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
221 943f69a6 Michael Niedermayer
            b8_stride = 0;
222 0a8dca87 Michael Niedermayer
        }else{
223
            mb_xy += h->col_fieldoff; // non zero for FL -> FL & differ parity
224 943f69a6 Michael Niedermayer
        }
225
        goto single_col;
226
    }else{                                               // AFL/AFR/FR/FL -> AFR/FR
227
        if(IS_INTERLACED(*mb_type)){                     // AFL       /FL -> AFR/FR
228
            mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
229
            mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
230
            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
231
            b8_stride *= 3;
232
            b4_stride *= 6;
233 668fd142 Michael Niedermayer
234 af0d6581 Michael Niedermayer
            sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
235 943f69a6 Michael Niedermayer
            if(    (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
236
                && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
237
                && !is_b8x8){
238 af0d6581 Michael Niedermayer
                *mb_type   |= MB_TYPE_16x8 |MB_TYPE_DIRECT2; /* B_16x8 */
239 943f69a6 Michael Niedermayer
            }else{
240 af0d6581 Michael Niedermayer
                *mb_type   |= MB_TYPE_8x8;
241 943f69a6 Michael Niedermayer
            }
242
        }else{                                           //     AFR/FR    -> AFR/FR
243
single_col:
244
            mb_type_col[0] =
245
            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
246 daaf6e3e Michael Niedermayer
247 af0d6581 Michael Niedermayer
            sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
248 daaf6e3e Michael Niedermayer
            if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
249 af0d6581 Michael Niedermayer
                *mb_type   |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_16x16 */
250 464baa66 Michael Niedermayer
            }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
251 af0d6581 Michael Niedermayer
                *mb_type   |= MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
252 943f69a6 Michael Niedermayer
            }else{
253 daaf6e3e Michael Niedermayer
                if(!h->sps.direct_8x8_inference_flag){
254
                    /* FIXME save sub mb types from previous frames (or derive from MVs)
255
                    * so we know exactly what block size to use */
256 af0d6581 Michael Niedermayer
                    sub_mb_type += (MB_TYPE_8x8-MB_TYPE_16x16); /* B_SUB_4x4 */
257 daaf6e3e Michael Niedermayer
                }
258 af0d6581 Michael Niedermayer
                *mb_type   |= MB_TYPE_8x8;
259 943f69a6 Michael Niedermayer
            }
260
        }
261
    }
262
263
    l1mv0  = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
264
    l1mv1  = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
265
    l1ref0 = &h->ref_list[1][0].ref_index [0][h->mb2b8_xy[mb_xy]];
266
    l1ref1 = &h->ref_list[1][0].ref_index [1][h->mb2b8_xy[mb_xy]];
267
    if(!b8_stride){
268
        if(s->mb_y&1){
269
            l1ref0 += h->b8_stride;
270
            l1ref1 += h->b8_stride;
271
            l1mv0  +=  2*b4_stride;
272
            l1mv1  +=  2*b4_stride;
273
        }
274
    }
275
276
277 af0d6581 Michael Niedermayer
        if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
278 1be49ce5 Michael Niedermayer
            int n=0;
279 943f69a6 Michael Niedermayer
            for(i8=0; i8<4; i8++){
280
                int x8 = i8&1;
281
                int y8 = i8>>1;
282
                int xy8 = x8+y8*b8_stride;
283
                int xy4 = 3*x8+y8*b4_stride;
284 3b9e61e4 Michael Niedermayer
                int a,b;
285 943f69a6 Michael Niedermayer
286
                if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
287
                    continue;
288
                h->sub_mb_type[i8] = sub_mb_type;
289
290
                fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
291
                fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
292 8dcbf4c7 Michael Niedermayer
                if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
293 943f69a6 Michael Niedermayer
                   && (   (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
294
                       || (l1ref0[xy8]  < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
295 3b9e61e4 Michael Niedermayer
                    a=b=0;
296 943f69a6 Michael Niedermayer
                    if(ref[0] > 0)
297 828cfff6 Michael Niedermayer
                        a= mv[0];
298 943f69a6 Michael Niedermayer
                    if(ref[1] > 0)
299 828cfff6 Michael Niedermayer
                        b= mv[1];
300 1be49ce5 Michael Niedermayer
                    n++;
301 943f69a6 Michael Niedermayer
                }else{
302 828cfff6 Michael Niedermayer
                    a= mv[0];
303
                    b= mv[1];
304 943f69a6 Michael Niedermayer
                }
305
                fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
306
                fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
307
            }
308 1be49ce5 Michael Niedermayer
            if(!is_b8x8 && !(n&3))
309
                *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;
310 943f69a6 Michael Niedermayer
        }else if(IS_16X16(*mb_type)){
311 3b9e61e4 Michael Niedermayer
            int a,b;
312 943f69a6 Michael Niedermayer
313
            fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
314
            fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
315 8dcbf4c7 Michael Niedermayer
            if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
316 943f69a6 Michael Niedermayer
               && (   (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
317
                   || (l1ref0[0]  < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
318 055a6aa7 Michael Niedermayer
                       && h->x264_build>33U))){
319 3b9e61e4 Michael Niedermayer
                a=b=0;
320 943f69a6 Michael Niedermayer
                if(ref[0] > 0)
321 828cfff6 Michael Niedermayer
                    a= mv[0];
322 943f69a6 Michael Niedermayer
                if(ref[1] > 0)
323 828cfff6 Michael Niedermayer
                    b= mv[1];
324 943f69a6 Michael Niedermayer
            }else{
325 828cfff6 Michael Niedermayer
                a= mv[0];
326
                b= mv[1];
327 943f69a6 Michael Niedermayer
            }
328
            fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
329
            fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
330
        }else{
331 3babc711 Michael Niedermayer
            int n=0;
332 943f69a6 Michael Niedermayer
            for(i8=0; i8<4; i8++){
333
                const int x8 = i8&1;
334
                const int y8 = i8>>1;
335
336
                if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
337
                    continue;
338
                h->sub_mb_type[i8] = sub_mb_type;
339
340 828cfff6 Michael Niedermayer
                fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, mv[0], 4);
341
                fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, mv[1], 4);
342 943f69a6 Michael Niedermayer
                fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
343
                fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
344
345
                /* col_zero_flag */
346 8dcbf4c7 Michael Niedermayer
                if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && (   l1ref0[x8 + y8*b8_stride] == 0
347 943f69a6 Michael Niedermayer
                                              || (l1ref0[x8 + y8*b8_stride] < 0 && l1ref1[x8 + y8*b8_stride] == 0
348 055a6aa7 Michael Niedermayer
                                                  && h->x264_build>33U))){
349 943f69a6 Michael Niedermayer
                    const int16_t (*l1mv)[2]= l1ref0[x8 + y8*b8_stride] == 0 ? l1mv0 : l1mv1;
350
                    if(IS_SUB_8X8(sub_mb_type)){
351
                        const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
352
                        if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
353
                            if(ref[0] == 0)
354
                                fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
355
                            if(ref[1] == 0)
356
                                fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
357 3babc711 Michael Niedermayer
                            n+=4;
358 943f69a6 Michael Niedermayer
                        }
359 8b9eb2df Michael Niedermayer
                    }else{
360
                        int m=0;
361 943f69a6 Michael Niedermayer
                    for(i4=0; i4<4; i4++){
362
                        const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
363
                        if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
364
                            if(ref[0] == 0)
365
                                *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
366
                            if(ref[1] == 0)
367
                                *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
368 8b9eb2df Michael Niedermayer
                            m++;
369 943f69a6 Michael Niedermayer
                        }
370
                    }
371 8b9eb2df Michael Niedermayer
                    if(!(m&3))
372
                        h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;
373 3babc711 Michael Niedermayer
                    n+=m;
374 8b9eb2df Michael Niedermayer
                    }
375 943f69a6 Michael Niedermayer
                }
376
            }
377 3babc711 Michael Niedermayer
            if(!is_b8x8 && !(n&15))
378
                *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;
379 943f69a6 Michael Niedermayer
        }
380 edb4df43 Michael Niedermayer
}
381
382
static void pred_temp_direct_motion(H264Context * const h, int *mb_type){
383
    MpegEncContext * const s = &h->s;
384
    int b8_stride = h->b8_stride;
385
    int b4_stride = h->b_stride;
386
    int mb_xy = h->mb_xy;
387
    int mb_type_col[2];
388
    const int16_t (*l1mv0)[2], (*l1mv1)[2];
389
    const int8_t *l1ref0, *l1ref1;
390
    const int is_b8x8 = IS_8X8(*mb_type);
391
    unsigned int sub_mb_type;
392
    int i8, i4;
393
394
    assert(h->ref_list[1][0].reference&3);
395
396
    if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
397
        if(!IS_INTERLACED(*mb_type)){                    //     AFR/FR    -> AFL/FL
398
            mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
399
            b8_stride = 0;
400
        }else{
401
            mb_xy += h->col_fieldoff; // non zero for FL -> FL & differ parity
402
        }
403
        goto single_col;
404
    }else{                                               // AFL/AFR/FR/FL -> AFR/FR
405
        if(IS_INTERLACED(*mb_type)){                     // AFL       /FL -> AFR/FR
406
            mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
407
            mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
408
            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
409
            b8_stride *= 3;
410
            b4_stride *= 6;
411
412
            sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
413
414
            if(    (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
415
                && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
416
                && !is_b8x8){
417
                *mb_type   |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
418
            }else{
419
                *mb_type   |= MB_TYPE_8x8|MB_TYPE_L0L1;
420
            }
421
        }else{                                           //     AFR/FR    -> AFR/FR
422
single_col:
423
            mb_type_col[0] =
424
            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
425
426
            sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
427
            if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
428
                *mb_type   |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
429
            }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
430
                *mb_type   |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
431
            }else{
432
                if(!h->sps.direct_8x8_inference_flag){
433
                    /* FIXME save sub mb types from previous frames (or derive from MVs)
434
                    * so we know exactly what block size to use */
435
                    sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
436
                }
437
                *mb_type   |= MB_TYPE_8x8|MB_TYPE_L0L1;
438
            }
439
        }
440
    }
441
442
    l1mv0  = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
443
    l1mv1  = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
444
    l1ref0 = &h->ref_list[1][0].ref_index [0][h->mb2b8_xy[mb_xy]];
445
    l1ref1 = &h->ref_list[1][0].ref_index [1][h->mb2b8_xy[mb_xy]];
446
    if(!b8_stride){
447
        if(s->mb_y&1){
448
            l1ref0 += h->b8_stride;
449
            l1ref1 += h->b8_stride;
450
            l1mv0  +=  2*b4_stride;
451
            l1mv1  +=  2*b4_stride;
452
        }
453
    }
454
455
    {
456 943f69a6 Michael Niedermayer
        const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
457
        const int *dist_scale_factor = h->dist_scale_factor;
458 542d4a75 Michael Niedermayer
        int ref_offset;
459 943f69a6 Michael Niedermayer
460
        if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
461
            map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
462
            map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
463
            dist_scale_factor   =h->dist_scale_factor_field[s->mb_y&1];
464
        }
465 542d4a75 Michael Niedermayer
        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
466 943f69a6 Michael Niedermayer
467
        if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
468
            int y_shift  = 2*!IS_INTERLACED(*mb_type);
469 5aa1d759 Michael Niedermayer
            assert(h->sps.direct_8x8_inference_flag);
470 943f69a6 Michael Niedermayer
471
            for(i8=0; i8<4; i8++){
472
                const int x8 = i8&1;
473
                const int y8 = i8>>1;
474
                int ref0, scale;
475
                const int16_t (*l1mv)[2]= l1mv0;
476
477
                if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
478
                    continue;
479
                h->sub_mb_type[i8] = sub_mb_type;
480
481
                fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
482
                if(IS_INTRA(mb_type_col[y8])){
483
                    fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
484
                    fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
485
                    fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
486
                    continue;
487
                }
488
489
                ref0 = l1ref0[x8 + y8*b8_stride];
490
                if(ref0 >= 0)
491
                    ref0 = map_col_to_list0[0][ref0 + ref_offset];
492
                else{
493
                    ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
494
                    l1mv= l1mv1;
495
                }
496
                scale = dist_scale_factor[ref0];
497
                fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
498
499
                {
500
                    const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
501
                    int my_col = (mv_col[1]<<y_shift)/2;
502
                    int mx = (scale * mv_col[0] + 128) >> 8;
503
                    int my = (scale * my_col + 128) >> 8;
504
                    fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
505
                    fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
506
                }
507
            }
508
            return;
509
        }
510
511
        /* one-to-one mv scaling */
512
513
        if(IS_16X16(*mb_type)){
514
            int ref, mv0, mv1;
515
516
            fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
517
            if(IS_INTRA(mb_type_col[0])){
518
                ref=mv0=mv1=0;
519
            }else{
520
                const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
521
                                                : map_col_to_list0[1][l1ref1[0] + ref_offset];
522
                const int scale = dist_scale_factor[ref0];
523
                const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
524
                int mv_l0[2];
525
                mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
526
                mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
527
                ref= ref0;
528
                mv0= pack16to32(mv_l0[0],mv_l0[1]);
529
                mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
530
            }
531
            fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
532
            fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
533
            fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
534
        }else{
535
            for(i8=0; i8<4; i8++){
536
                const int x8 = i8&1;
537
                const int y8 = i8>>1;
538
                int ref0, scale;
539
                const int16_t (*l1mv)[2]= l1mv0;
540
541
                if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
542
                    continue;
543
                h->sub_mb_type[i8] = sub_mb_type;
544
                fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
545
                if(IS_INTRA(mb_type_col[0])){
546
                    fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
547
                    fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
548
                    fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
549
                    continue;
550
                }
551
552 50f8e985 Michael Niedermayer
                ref0 = l1ref0[x8 + y8*b8_stride];
553 943f69a6 Michael Niedermayer
                if(ref0 >= 0)
554 50f8e985 Michael Niedermayer
                    ref0 = map_col_to_list0[0][ref0 + ref_offset];
555 943f69a6 Michael Niedermayer
                else{
556
                    ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
557
                    l1mv= l1mv1;
558
                }
559
                scale = dist_scale_factor[ref0];
560
561
                fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
562
                if(IS_SUB_8X8(sub_mb_type)){
563
                    const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
564
                    int mx = (scale * mv_col[0] + 128) >> 8;
565
                    int my = (scale * mv_col[1] + 128) >> 8;
566
                    fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
567
                    fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
568
                }else
569
                for(i4=0; i4<4; i4++){
570
                    const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
571
                    int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
572
                    mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
573
                    mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
574
                    *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
575
                        pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
576
                }
577
            }
578
        }
579
    }
580
}
581 edb4df43 Michael Niedermayer
582
void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
583
    if(h->direct_spatial_mv_pred){
584
        pred_spatial_direct_motion(h, mb_type);
585
    }else{
586
        pred_temp_direct_motion(h, mb_type);
587
    }
588
}