Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h264.c @ dc172ecc

History | View | Annotate | Download (144 KB)

1 0da71265 Michael Niedermayer
/*
2 d5ec00ba Alex Converse
 * H.26L/H.264/AVC/JVT/14496-10/... decoder
3 0da71265 Michael Niedermayer
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 0da71265 Michael Niedermayer
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 0da71265 Michael Niedermayer
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 0da71265 Michael Niedermayer
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 0da71265 Michael Niedermayer
 */
21 115329f1 Diego Biurrun
22 0da71265 Michael Niedermayer
/**
23 ba87f080 Diego Biurrun
 * @file
24 0da71265 Michael Niedermayer
 * H.264 / AVC / MPEG4 part10 codec.
25
 * @author Michael Niedermayer <michaelni@gmx.at>
26
 */
27
28 7ffe76e5 Reinhard Tartler
#include "libavutil/imgutils.h"
29 40e5d31b Gwenole Beauchesne
#include "internal.h"
30 0da71265 Michael Niedermayer
#include "dsputil.h"
31
#include "avcodec.h"
32
#include "mpegvideo.h"
33 26b4fe82 Aurelien Jacobs
#include "h264.h"
34 0da71265 Michael Niedermayer
#include "h264data.h"
35 188d3c51 Michael Niedermayer
#include "h264_mvpred.h"
36 0da71265 Michael Niedermayer
#include "golomb.h"
37 199436b9 Aurelien Jacobs
#include "mathops.h"
38 626464fb Kostya Shishkov
#include "rectangle.h"
39 d375c104 Michael Niedermayer
#include "thread.h"
40 369122dd NVIDIA Corporation
#include "vdpau_internal.h"
41 cfa5a81e Michael Niedermayer
#include "libavutil/avassert.h"
42 0da71265 Michael Niedermayer
43 e5017ab8 Laurent Aimar
#include "cabac.h"
44
45 2848ce84 Loren Merritt
//#undef NDEBUG
46 0da71265 Michael Niedermayer
#include <assert.h>
47
48 d9ec210b Diego Pettenò
static const uint8_t rem6[52]={
49 acd8d10f Panagiotis Issaris
0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
50
};
51
52 d9ec210b Diego Pettenò
static const uint8_t div6[52]={
53 acd8d10f Panagiotis Issaris
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
54
};
55
56 0435fb16 Baptiste Coudurier
static const enum PixelFormat hwaccel_pixfmt_list_h264_jpeg_420[] = {
57
    PIX_FMT_DXVA2_VLD,
58
    PIX_FMT_VAAPI_VLD,
59
    PIX_FMT_YUVJ420P,
60
    PIX_FMT_NONE
61
};
62
63 903d58f6 Michael Niedermayer
void ff_h264_write_back_intra_pred_mode(H264Context *h){
64 5b0fb524 Michael Niedermayer
    int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
65 0da71265 Michael Niedermayer
66 662a5b23 Michael Niedermayer
    AV_COPY32(mode, h->intra4x4_pred_mode_cache + 4 + 8*4);
67
    mode[4]= h->intra4x4_pred_mode_cache[7+8*3];
68
    mode[5]= h->intra4x4_pred_mode_cache[7+8*2];
69
    mode[6]= h->intra4x4_pred_mode_cache[7+8*1];
70 0da71265 Michael Niedermayer
}
71
72
/**
73
 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
74
 */
75 2bedc0e8 Michael Niedermayer
int ff_h264_check_intra4x4_pred_mode(H264Context *h){
76
    MpegEncContext * const s = &h->s;
77
    static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
78
    static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
79
    int i;
80
81
    if(!(h->top_samples_available&0x8000)){
82
        for(i=0; i<4; i++){
83
            int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
84
            if(status<0){
85
                av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
86
                return -1;
87
            } else if(status){
88
                h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
89
            }
90
        }
91
    }
92
93
    if((h->left_samples_available&0x8888)!=0x8888){
94
        static const int mask[4]={0x8000,0x2000,0x80,0x20};
95
        for(i=0; i<4; i++){
96
            if(!(h->left_samples_available&mask[i])){
97
                int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
98
                if(status<0){
99
                    av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
100
                    return -1;
101
                } else if(status){
102
                    h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
103
                }
104
            }
105
        }
106
    }
107
108
    return 0;
109
} //FIXME cleanup like ff_h264_check_intra_pred_mode
110
111
/**
112
 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
113
 */
114 903d58f6 Michael Niedermayer
int ff_h264_check_intra_pred_mode(H264Context *h, int mode){
115 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
116
    static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
117
    static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
118 115329f1 Diego Biurrun
119 43ff0714 Michael Niedermayer
    if(mode > 6U) {
120 5175b937 Loic Le Loarer
        av_log(h->s.avctx, AV_LOG_ERROR, "out of range intra chroma pred mode at %d %d\n", s->mb_x, s->mb_y);
121 7440fe83 Michael Niedermayer
        return -1;
122 5175b937 Loic Le Loarer
    }
123 115329f1 Diego Biurrun
124 0da71265 Michael Niedermayer
    if(!(h->top_samples_available&0x8000)){
125
        mode= top[ mode ];
126
        if(mode<0){
127 9b879566 Michel Bardiaux
            av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
128 0da71265 Michael Niedermayer
            return -1;
129
        }
130
    }
131 115329f1 Diego Biurrun
132 d1d10e91 Michael Niedermayer
    if((h->left_samples_available&0x8080) != 0x8080){
133 0da71265 Michael Niedermayer
        mode= left[ mode ];
134 d1d10e91 Michael Niedermayer
        if(h->left_samples_available&0x8080){ //mad cow disease mode, aka MBAFF + constrained_intra_pred
135
            mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
136
        }
137 0da71265 Michael Niedermayer
        if(mode<0){
138 9b879566 Michel Bardiaux
            av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
139 0da71265 Michael Niedermayer
            return -1;
140 115329f1 Diego Biurrun
        }
141 0da71265 Michael Niedermayer
    }
142
143
    return mode;
144
}
145
146 1790a5e9 Ivan Schreter
const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
147 0da71265 Michael Niedermayer
    int i, si, di;
148
    uint8_t *dst;
149 24456882 Andreas Öman
    int bufidx;
150 0da71265 Michael Niedermayer
151 bb270c08 Diego Biurrun
//    src[0]&0x80;                //forbidden bit
152 0da71265 Michael Niedermayer
    h->nal_ref_idc= src[0]>>5;
153
    h->nal_unit_type= src[0]&0x1F;
154
155
    src++; length--;
156 115329f1 Diego Biurrun
#if 0
157 0da71265 Michael Niedermayer
    for(i=0; i<length; i++)
158
        printf("%2X ", src[i]);
159
#endif
160 e08715d3 Michael Niedermayer
161 b250f9c6 Aurelien Jacobs
#if HAVE_FAST_UNALIGNED
162
# if HAVE_FAST_64BIT
163 e08715d3 Michael Niedermayer
#   define RS 7
164
    for(i=0; i+1<length; i+=9){
165 19769ece Måns Rullgård
        if(!((~AV_RN64A(src+i) & (AV_RN64A(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
166 e08715d3 Michael Niedermayer
# else
167
#   define RS 3
168
    for(i=0; i+1<length; i+=5){
169 19769ece Måns Rullgård
        if(!((~AV_RN32A(src+i) & (AV_RN32A(src+i) - 0x01000101U)) & 0x80008080U))
170 e08715d3 Michael Niedermayer
# endif
171
            continue;
172
        if(i>0 && !src[i]) i--;
173
        while(src[i]) i++;
174
#else
175
#   define RS 0
176 0da71265 Michael Niedermayer
    for(i=0; i+1<length; i+=2){
177
        if(src[i]) continue;
178
        if(i>0 && src[i-1]==0) i--;
179 e08715d3 Michael Niedermayer
#endif
180 0da71265 Michael Niedermayer
        if(i+2<length && src[i+1]==0 && src[i+2]<=3){
181
            if(src[i+2]!=3){
182
                /* startcode, so we must be past the end */
183
                length=i;
184
            }
185
            break;
186
        }
187 abb27cfb Michael Niedermayer
        i-= RS;
188 0da71265 Michael Niedermayer
    }
189
190
    if(i>=length-1){ //no escaped 0
191
        *dst_length= length;
192
        *consumed= length+1; //+1 for the header
193 115329f1 Diego Biurrun
        return src;
194 0da71265 Michael Niedermayer
    }
195
196 24456882 Andreas Öman
    bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
197 238ef6da Reimar Döffinger
    av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE);
198 24456882 Andreas Öman
    dst= h->rbsp_buffer[bufidx];
199 0da71265 Michael Niedermayer
200 ac658be5 Francois Oligny-Lemieux
    if (dst == NULL){
201
        return NULL;
202
    }
203
204 3b66c4c5 Kevin Baragona
//printf("decoding esc\n");
205 593af7cd Michael Niedermayer
    memcpy(dst, src, i);
206
    si=di=i;
207
    while(si+2<length){
208 0da71265 Michael Niedermayer
        //remove escapes (very rare 1:2^22)
209 593af7cd Michael Niedermayer
        if(src[si+2]>3){
210
            dst[di++]= src[si++];
211
            dst[di++]= src[si++];
212
        }else if(src[si]==0 && src[si+1]==0){
213 0da71265 Michael Niedermayer
            if(src[si+2]==3){ //escape
214
                dst[di++]= 0;
215
                dst[di++]= 0;
216
                si+=3;
217 c8470cc1 Michael Niedermayer
                continue;
218 0da71265 Michael Niedermayer
            }else //next start code
219 593af7cd Michael Niedermayer
                goto nsc;
220 0da71265 Michael Niedermayer
        }
221
222
        dst[di++]= src[si++];
223
    }
224 593af7cd Michael Niedermayer
    while(si<length)
225
        dst[di++]= src[si++];
226
nsc:
227 0da71265 Michael Niedermayer
228 d4369630 Alexander Strange
    memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
229
230 0da71265 Michael Niedermayer
    *dst_length= di;
231
    *consumed= si + 1;//+1 for the header
232 90b5b51e Diego Biurrun
//FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
233 0da71265 Michael Niedermayer
    return dst;
234
}
235
236 12324121 Diego Elio Pettenò
/**
237
 * Identify the exact end of the bitstream
238
 * @return the length of the trailing, or 0 if damaged
239
 */
240
static int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
241 0da71265 Michael Niedermayer
    int v= *src;
242
    int r;
243
244 a9c9a240 Michel Bardiaux
    tprintf(h->s.avctx, "rbsp trailing %X\n", v);
245 0da71265 Michael Niedermayer
246
    for(r=1; r<9; r++){
247
        if(v&1) return r;
248
        v>>=1;
249
    }
250
    return 0;
251
}
252
253 d375c104 Michael Niedermayer
static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n, int height,
254
                                 int y_offset, int list){
255
    int raw_my= h->mv_cache[list][ scan8[n] ][1];
256
    int filter_height= (raw_my&3) ? 2 : 0;
257
    int full_my= (raw_my>>2) + y_offset;
258
    int top = full_my - filter_height, bottom = full_my + height + filter_height;
259
260
    return FFMAX(abs(top), bottom);
261
}
262
263
static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n, int height,
264
                               int y_offset, int list0, int list1, int *nrefs){
265
    MpegEncContext * const s = &h->s;
266
    int my;
267
268
    y_offset += 16*(s->mb_y >> MB_FIELD);
269
270
    if(list0){
271
        int ref_n = h->ref_cache[0][ scan8[n] ];
272
        Picture *ref= &h->ref_list[0][ref_n];
273
274
        // Error resilience puts the current picture in the ref list.
275
        // Don't try to wait on these as it will cause a deadlock.
276
        // Fields can wait on each other, though.
277
        if(ref->thread_opaque != s->current_picture.thread_opaque ||
278
           (ref->reference&3) != s->picture_structure) {
279
            my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
280
            if (refs[0][ref_n] < 0) nrefs[0] += 1;
281
            refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
282
        }
283
    }
284
285
    if(list1){
286
        int ref_n = h->ref_cache[1][ scan8[n] ];
287
        Picture *ref= &h->ref_list[1][ref_n];
288
289
        if(ref->thread_opaque != s->current_picture.thread_opaque ||
290
           (ref->reference&3) != s->picture_structure) {
291
            my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
292
            if (refs[1][ref_n] < 0) nrefs[1] += 1;
293
            refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
294
        }
295
    }
296
}
297
298
/**
299
 * Wait until all reference frames are available for MC operations.
300
 *
301
 * @param h the H264 context
302
 */
303
static void await_references(H264Context *h){
304
    MpegEncContext * const s = &h->s;
305
    const int mb_xy= h->mb_xy;
306
    const int mb_type= s->current_picture.mb_type[mb_xy];
307
    int refs[2][48];
308
    int nrefs[2] = {0};
309
    int ref, list;
310
311
    memset(refs, -1, sizeof(refs));
312
313
    if(IS_16X16(mb_type)){
314
        get_lowest_part_y(h, refs, 0, 16, 0,
315
                  IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
316
    }else if(IS_16X8(mb_type)){
317
        get_lowest_part_y(h, refs, 0, 8, 0,
318
                  IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
319
        get_lowest_part_y(h, refs, 8, 8, 8,
320
                  IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
321
    }else if(IS_8X16(mb_type)){
322
        get_lowest_part_y(h, refs, 0, 16, 0,
323
                  IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
324
        get_lowest_part_y(h, refs, 4, 16, 0,
325
                  IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
326
    }else{
327
        int i;
328
329
        assert(IS_8X8(mb_type));
330
331
        for(i=0; i<4; i++){
332
            const int sub_mb_type= h->sub_mb_type[i];
333
            const int n= 4*i;
334
            int y_offset= (i&2)<<2;
335
336
            if(IS_SUB_8X8(sub_mb_type)){
337
                get_lowest_part_y(h, refs, n  , 8, y_offset,
338
                          IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
339
            }else if(IS_SUB_8X4(sub_mb_type)){
340
                get_lowest_part_y(h, refs, n  , 4, y_offset,
341
                          IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
342
                get_lowest_part_y(h, refs, n+2, 4, y_offset+4,
343
                          IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
344
            }else if(IS_SUB_4X8(sub_mb_type)){
345
                get_lowest_part_y(h, refs, n  , 8, y_offset,
346
                          IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
347
                get_lowest_part_y(h, refs, n+1, 8, y_offset,
348
                          IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
349
            }else{
350
                int j;
351
                assert(IS_SUB_4X4(sub_mb_type));
352
                for(j=0; j<4; j++){
353
                    int sub_y_offset= y_offset + 2*(j&2);
354
                    get_lowest_part_y(h, refs, n+j, 4, sub_y_offset,
355
                              IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
356
                }
357
            }
358
        }
359
    }
360
361
    for(list=h->list_count-1; list>=0; list--){
362
        for(ref=0; ref<48 && nrefs[list]; ref++){
363
            int row = refs[list][ref];
364
            if(row >= 0){
365
                Picture *ref_pic = &h->ref_list[list][ref];
366
                int ref_field = ref_pic->reference - 1;
367
                int ref_field_picture = ref_pic->field_picture;
368
                int pic_height = 16*s->mb_height >> ref_field_picture;
369
370
                row <<= MB_MBAFF;
371
                nrefs[list]--;
372
373
                if(!FIELD_PICTURE && ref_field_picture){ // frame referencing two fields
374
                    ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) - !(row&1), pic_height-1), 1);
375
                    ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1)           , pic_height-1), 0);
376
                }else if(FIELD_PICTURE && !ref_field_picture){ // field referencing one field of a frame
377
                    ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row*2 + ref_field    , pic_height-1), 0);
378
                }else if(FIELD_PICTURE){
379
                    ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), ref_field);
380
                }else{
381
                    ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), 0);
382
                }
383
            }
384
        }
385
    }
386
}
387
388 772225c0 Ronald S. Bultje
#if 0
389
/**
390
 * DCT transforms the 16 dc values.
391
 * @param qp quantization parameter ??? FIXME
392
 */
393
static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
394
//    const int qmul= dequant_coeff[qp][0];
395
    int i;
396
    int temp[16]; //FIXME check if this is a good idea
397
    static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};
398
    static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
399

400
    for(i=0; i<4; i++){
401
        const int offset= y_offset[i];
402
        const int z0= block[offset+stride*0] + block[offset+stride*4];
403
        const int z1= block[offset+stride*0] - block[offset+stride*4];
404
        const int z2= block[offset+stride*1] - block[offset+stride*5];
405
        const int z3= block[offset+stride*1] + block[offset+stride*5];
406

407
        temp[4*i+0]= z0+z3;
408
        temp[4*i+1]= z1+z2;
409
        temp[4*i+2]= z1-z2;
410
        temp[4*i+3]= z0-z3;
411
    }
412

413
    for(i=0; i<4; i++){
414
        const int offset= x_offset[i];
415
        const int z0= temp[4*0+i] + temp[4*2+i];
416
        const int z1= temp[4*0+i] - temp[4*2+i];
417
        const int z2= temp[4*1+i] - temp[4*3+i];
418
        const int z3= temp[4*1+i] + temp[4*3+i];
419

420
        block[stride*0 +offset]= (z0 + z3)>>1;
421
        block[stride*2 +offset]= (z1 + z2)>>1;
422
        block[stride*8 +offset]= (z1 - z2)>>1;
423
        block[stride*10+offset]= (z0 - z3)>>1;
424
    }
425
}
426
#endif
427
428
#undef xStride
429
#undef stride
430
431
#if 0
432
static void chroma_dc_dct_c(DCTELEM *block){
433
    const int stride= 16*2;
434
    const int xStride= 16;
435
    int a,b,c,d,e;
436

437
    a= block[stride*0 + xStride*0];
438
    b= block[stride*0 + xStride*1];
439
    c= block[stride*1 + xStride*0];
440
    d= block[stride*1 + xStride*1];
441

442
    e= a-b;
443
    a= a+b;
444
    b= c-d;
445
    c= c+d;
446

447
    block[stride*0 + xStride*0]= (a+c);
448
    block[stride*0 + xStride*1]= (e+b);
449
    block[stride*1 + xStride*0]= (a-c);
450
    block[stride*1 + xStride*1]= (e-b);
451
}
452
#endif
453
454 0da71265 Michael Niedermayer
static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
455
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
456
                           int src_x_offset, int src_y_offset,
457
                           qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
458
    MpegEncContext * const s = &h->s;
459
    const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
460 5d18eaad Loren Merritt
    int my=       h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
461 0da71265 Michael Niedermayer
    const int luma_xy= (mx&3) + ((my&3)<<2);
462 dc172ecc Oskar Arvidsson
    uint8_t * src_y = pic->data[0] + (mx>>2)*h->pixel_size + (my>>2)*h->mb_linesize;
463 5d18eaad Loren Merritt
    uint8_t * src_cb, * src_cr;
464
    int extra_width= h->emu_edge_width;
465
    int extra_height= h->emu_edge_height;
466 0da71265 Michael Niedermayer
    int emu=0;
467
    const int full_mx= mx>>2;
468
    const int full_my= my>>2;
469 fbd312fd Loren Merritt
    const int pic_width  = 16*s->mb_width;
470 0d43dd8c Jeff Downs
    const int pic_height = 16*s->mb_height >> MB_FIELD;
471 115329f1 Diego Biurrun
472 0da71265 Michael Niedermayer
    if(mx&7) extra_width -= 3;
473
    if(my&7) extra_height -= 3;
474 115329f1 Diego Biurrun
475
    if(   full_mx < 0-extra_width
476
       || full_my < 0-extra_height
477
       || full_mx + 16/*FIXME*/ > pic_width + extra_width
478 fbd312fd Loren Merritt
       || full_my + 16/*FIXME*/ > pic_height + extra_height){
479 dc172ecc Oskar Arvidsson
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_y - 2*h->pixel_size - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
480
            src_y= s->edge_emu_buffer + 2*h->pixel_size + 2*h->mb_linesize;
481 0da71265 Michael Niedermayer
        emu=1;
482
    }
483 115329f1 Diego Biurrun
484 5d18eaad Loren Merritt
    qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
485 0da71265 Michael Niedermayer
    if(!square){
486 5d18eaad Loren Merritt
        qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
487 0da71265 Michael Niedermayer
    }
488 115329f1 Diego Biurrun
489 49fb20cb Aurelien Jacobs
    if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
490 115329f1 Diego Biurrun
491 0d43dd8c Jeff Downs
    if(MB_FIELD){
492 5d18eaad Loren Merritt
        // chroma offset when predicting from a field of opposite parity
493 2143b118 Jeff Downs
        my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
494 5d18eaad Loren Merritt
        emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
495
    }
496 dc172ecc Oskar Arvidsson
    src_cb= pic->data[1] + (mx>>3)*h->pixel_size + (my>>3)*h->mb_uvlinesize;
497
    src_cr= pic->data[2] + (mx>>3)*h->pixel_size + (my>>3)*h->mb_uvlinesize;
498 5d18eaad Loren Merritt
499 0da71265 Michael Niedermayer
    if(emu){
500 d23e3e5f Ronald S. Bultje
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
501 0da71265 Michael Niedermayer
            src_cb= s->edge_emu_buffer;
502
    }
503 5d18eaad Loren Merritt
    chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
504 0da71265 Michael Niedermayer
505
    if(emu){
506 d23e3e5f Ronald S. Bultje
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
507 0da71265 Michael Niedermayer
            src_cr= s->edge_emu_buffer;
508
    }
509 5d18eaad Loren Merritt
    chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
510 0da71265 Michael Niedermayer
}
511
512 9f2d1b4f Loren Merritt
static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
513 0da71265 Michael Niedermayer
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
514
                           int x_offset, int y_offset,
515
                           qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
516
                           qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
517
                           int list0, int list1){
518
    MpegEncContext * const s = &h->s;
519
    qpel_mc_func *qpix_op=  qpix_put;
520
    h264_chroma_mc_func chroma_op= chroma_put;
521 115329f1 Diego Biurrun
522 dc172ecc Oskar Arvidsson
    dest_y  += 2*x_offset*h->pixel_size + 2*y_offset*h->  mb_linesize;
523
    dest_cb +=   x_offset*h->pixel_size +   y_offset*h->mb_uvlinesize;
524
    dest_cr +=   x_offset*h->pixel_size +   y_offset*h->mb_uvlinesize;
525 0da71265 Michael Niedermayer
    x_offset += 8*s->mb_x;
526 0d43dd8c Jeff Downs
    y_offset += 8*(s->mb_y >> MB_FIELD);
527 115329f1 Diego Biurrun
528 0da71265 Michael Niedermayer
    if(list0){
529 1924f3ce Michael Niedermayer
        Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
530 0da71265 Michael Niedermayer
        mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
531
                           dest_y, dest_cb, dest_cr, x_offset, y_offset,
532
                           qpix_op, chroma_op);
533
534
        qpix_op=  qpix_avg;
535
        chroma_op= chroma_avg;
536
    }
537
538
    if(list1){
539 1924f3ce Michael Niedermayer
        Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
540 0da71265 Michael Niedermayer
        mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
541
                           dest_y, dest_cb, dest_cr, x_offset, y_offset,
542
                           qpix_op, chroma_op);
543
    }
544
}
545
546 9f2d1b4f Loren Merritt
static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
547
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
548
                           int x_offset, int y_offset,
549
                           qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
550
                           h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
551
                           h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
552
                           int list0, int list1){
553
    MpegEncContext * const s = &h->s;
554
555 dc172ecc Oskar Arvidsson
    dest_y  += 2*x_offset*h->pixel_size + 2*y_offset*h->  mb_linesize;
556
    dest_cb +=   x_offset*h->pixel_size +   y_offset*h->mb_uvlinesize;
557
    dest_cr +=   x_offset*h->pixel_size +   y_offset*h->mb_uvlinesize;
558 9f2d1b4f Loren Merritt
    x_offset += 8*s->mb_x;
559 0d43dd8c Jeff Downs
    y_offset += 8*(s->mb_y >> MB_FIELD);
560 115329f1 Diego Biurrun
561 9f2d1b4f Loren Merritt
    if(list0 && list1){
562
        /* don't optimize for luma-only case, since B-frames usually
563
         * use implicit weights => chroma too. */
564
        uint8_t *tmp_cb = s->obmc_scratchpad;
565 dc172ecc Oskar Arvidsson
        uint8_t *tmp_cr = s->obmc_scratchpad + 8*h->pixel_size;
566 5d18eaad Loren Merritt
        uint8_t *tmp_y  = s->obmc_scratchpad + 8*h->mb_uvlinesize;
567 9f2d1b4f Loren Merritt
        int refn0 = h->ref_cache[0][ scan8[n] ];
568
        int refn1 = h->ref_cache[1][ scan8[n] ];
569
570
        mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
571
                    dest_y, dest_cb, dest_cr,
572
                    x_offset, y_offset, qpix_put, chroma_put);
573
        mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
574
                    tmp_y, tmp_cb, tmp_cr,
575
                    x_offset, y_offset, qpix_put, chroma_put);
576
577
        if(h->use_weight == 2){
578 1052b76f Michael Niedermayer
            int weight0 = h->implicit_weight[refn0][refn1][s->mb_y&1];
579 9f2d1b4f Loren Merritt
            int weight1 = 64 - weight0;
580 5d18eaad Loren Merritt
            luma_weight_avg(  dest_y,  tmp_y,  h->  mb_linesize, 5, weight0, weight1, 0);
581
            chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
582
            chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
583 9f2d1b4f Loren Merritt
        }else{
584 5d18eaad Loren Merritt
            luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
585 3d9137c8 Michael Niedermayer
                            h->luma_weight[refn0][0][0] , h->luma_weight[refn1][1][0],
586
                            h->luma_weight[refn0][0][1] + h->luma_weight[refn1][1][1]);
587 5d18eaad Loren Merritt
            chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
588 3d9137c8 Michael Niedermayer
                            h->chroma_weight[refn0][0][0][0] , h->chroma_weight[refn1][1][0][0],
589
                            h->chroma_weight[refn0][0][0][1] + h->chroma_weight[refn1][1][0][1]);
590 5d18eaad Loren Merritt
            chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
591 3d9137c8 Michael Niedermayer
                            h->chroma_weight[refn0][0][1][0] , h->chroma_weight[refn1][1][1][0],
592
                            h->chroma_weight[refn0][0][1][1] + h->chroma_weight[refn1][1][1][1]);
593 9f2d1b4f Loren Merritt
        }
594
    }else{
595
        int list = list1 ? 1 : 0;
596
        int refn = h->ref_cache[list][ scan8[n] ];
597
        Picture *ref= &h->ref_list[list][refn];
598
        mc_dir_part(h, ref, n, square, chroma_height, delta, list,
599
                    dest_y, dest_cb, dest_cr, x_offset, y_offset,
600
                    qpix_put, chroma_put);
601
602 5d18eaad Loren Merritt
        luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
603 3d9137c8 Michael Niedermayer
                       h->luma_weight[refn][list][0], h->luma_weight[refn][list][1]);
604 9f2d1b4f Loren Merritt
        if(h->use_weight_chroma){
605 5d18eaad Loren Merritt
            chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
606 3d9137c8 Michael Niedermayer
                             h->chroma_weight[refn][list][0][0], h->chroma_weight[refn][list][0][1]);
607 5d18eaad Loren Merritt
            chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
608 3d9137c8 Michael Niedermayer
                             h->chroma_weight[refn][list][1][0], h->chroma_weight[refn][list][1][1]);
609 9f2d1b4f Loren Merritt
        }
610
    }
611
}
612
613
static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
614
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
615
                           int x_offset, int y_offset,
616
                           qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
617
                           qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
618 115329f1 Diego Biurrun
                           h264_weight_func *weight_op, h264_biweight_func *weight_avg,
619 9f2d1b4f Loren Merritt
                           int list0, int list1){
620
    if((h->use_weight==2 && list0 && list1
621 1052b76f Michael Niedermayer
        && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ][h->s.mb_y&1] != 32))
622 9f2d1b4f Loren Merritt
       || h->use_weight==1)
623
        mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
624
                         x_offset, y_offset, qpix_put, chroma_put,
625
                         weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
626
    else
627
        mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
628
                    x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
629
}
630
631 513fbd8e Loren Merritt
static inline void prefetch_motion(H264Context *h, int list){
632
    /* fetch pixels for estimated mv 4 macroblocks ahead
633
     * optimized for 64byte cache lines */
634
    MpegEncContext * const s = &h->s;
635
    const int refn = h->ref_cache[list][scan8[0]];
636
    if(refn >= 0){
637
        const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
638
        const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
639
        uint8_t **src= h->ref_list[list][refn].data;
640 dc172ecc Oskar Arvidsson
        int off= mx*h->pixel_size + (my + (s->mb_x&3)*4)*h->mb_linesize + 64*h->pixel_size;
641 513fbd8e Loren Merritt
        s->dsp.prefetch(src[0]+off, s->linesize, 4);
642 dc172ecc Oskar Arvidsson
        off= (mx>>1)*h->pixel_size + ((my>>1)*h->pixel_size + (s->mb_x&7))*s->uvlinesize + 64*h->pixel_size;
643 513fbd8e Loren Merritt
        s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
644
    }
645
}
646
647 0da71265 Michael Niedermayer
static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
648
                      qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
649 9f2d1b4f Loren Merritt
                      qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
650
                      h264_weight_func *weight_op, h264_biweight_func *weight_avg){
651 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
652 64514ee8 Alexander Strange
    const int mb_xy= h->mb_xy;
653 0da71265 Michael Niedermayer
    const int mb_type= s->current_picture.mb_type[mb_xy];
654 115329f1 Diego Biurrun
655 0da71265 Michael Niedermayer
    assert(IS_INTER(mb_type));
656 115329f1 Diego Biurrun
657 d375c104 Michael Niedermayer
    if(HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME)
658
        await_references(h);
659 513fbd8e Loren Merritt
    prefetch_motion(h, 0);
660
661 0da71265 Michael Niedermayer
    if(IS_16X16(mb_type)){
662
        mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
663
                qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
664 7231ccf4 Michael Niedermayer
                weight_op, weight_avg,
665 0da71265 Michael Niedermayer
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
666
    }else if(IS_16X8(mb_type)){
667 dc172ecc Oskar Arvidsson
        mc_part(h, 0, 0, 4, 8*h->pixel_size, dest_y, dest_cb, dest_cr, 0, 0,
668 0da71265 Michael Niedermayer
                qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
669 9f2d1b4f Loren Merritt
                &weight_op[1], &weight_avg[1],
670 0da71265 Michael Niedermayer
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
671 dc172ecc Oskar Arvidsson
        mc_part(h, 8, 0, 4, 8*h->pixel_size, dest_y, dest_cb, dest_cr, 0, 4,
672 0da71265 Michael Niedermayer
                qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
673 9f2d1b4f Loren Merritt
                &weight_op[1], &weight_avg[1],
674 0da71265 Michael Niedermayer
                IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
675
    }else if(IS_8X16(mb_type)){
676 5d18eaad Loren Merritt
        mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
677 0da71265 Michael Niedermayer
                qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
678 9f2d1b4f Loren Merritt
                &weight_op[2], &weight_avg[2],
679 0da71265 Michael Niedermayer
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
680 5d18eaad Loren Merritt
        mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
681 0da71265 Michael Niedermayer
                qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
682 9f2d1b4f Loren Merritt
                &weight_op[2], &weight_avg[2],
683 0da71265 Michael Niedermayer
                IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
684
    }else{
685
        int i;
686 115329f1 Diego Biurrun
687 0da71265 Michael Niedermayer
        assert(IS_8X8(mb_type));
688
689
        for(i=0; i<4; i++){
690
            const int sub_mb_type= h->sub_mb_type[i];
691
            const int n= 4*i;
692
            int x_offset= (i&1)<<2;
693
            int y_offset= (i&2)<<1;
694
695
            if(IS_SUB_8X8(sub_mb_type)){
696
                mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
697
                    qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
698 9f2d1b4f Loren Merritt
                    &weight_op[3], &weight_avg[3],
699 0da71265 Michael Niedermayer
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
700
            }else if(IS_SUB_8X4(sub_mb_type)){
701 dc172ecc Oskar Arvidsson
                mc_part(h, n  , 0, 2, 4*h->pixel_size, dest_y, dest_cb, dest_cr, x_offset, y_offset,
702 0da71265 Michael Niedermayer
                    qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
703 9f2d1b4f Loren Merritt
                    &weight_op[4], &weight_avg[4],
704 0da71265 Michael Niedermayer
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
705 dc172ecc Oskar Arvidsson
                mc_part(h, n+2, 0, 2, 4*h->pixel_size, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
706 0da71265 Michael Niedermayer
                    qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
707 9f2d1b4f Loren Merritt
                    &weight_op[4], &weight_avg[4],
708 0da71265 Michael Niedermayer
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
709
            }else if(IS_SUB_4X8(sub_mb_type)){
710 5d18eaad Loren Merritt
                mc_part(h, n  , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
711 0da71265 Michael Niedermayer
                    qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
712 9f2d1b4f Loren Merritt
                    &weight_op[5], &weight_avg[5],
713 0da71265 Michael Niedermayer
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
714 5d18eaad Loren Merritt
                mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
715 0da71265 Michael Niedermayer
                    qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
716 9f2d1b4f Loren Merritt
                    &weight_op[5], &weight_avg[5],
717 0da71265 Michael Niedermayer
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
718
            }else{
719
                int j;
720
                assert(IS_SUB_4X4(sub_mb_type));
721
                for(j=0; j<4; j++){
722
                    int sub_x_offset= x_offset + 2*(j&1);
723
                    int sub_y_offset= y_offset +   (j&2);
724
                    mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
725
                        qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
726 9f2d1b4f Loren Merritt
                        &weight_op[6], &weight_avg[6],
727 0da71265 Michael Niedermayer
                        IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
728
                }
729
            }
730
        }
731
    }
732 513fbd8e Loren Merritt
733
    prefetch_motion(h, 1);
734 0da71265 Michael Niedermayer
}
735
736
737 2ed0f766 Ronald S. Bultje
static void free_tables(H264Context *h, int free_rbsp){
738 7978debd Andreas Öman
    int i;
739 afebe2f7 Andreas Öman
    H264Context *hx;
740 0da71265 Michael Niedermayer
    av_freep(&h->intra4x4_pred_mode);
741 e5017ab8 Laurent Aimar
    av_freep(&h->chroma_pred_mode_table);
742
    av_freep(&h->cbp_table);
743 9e528114 Laurent Aimar
    av_freep(&h->mvd_table[0]);
744
    av_freep(&h->mvd_table[1]);
745 5ad984c9 Loren Merritt
    av_freep(&h->direct_table);
746 0da71265 Michael Niedermayer
    av_freep(&h->non_zero_count);
747
    av_freep(&h->slice_table_base);
748
    h->slice_table= NULL;
749 c988f975 Michael Niedermayer
    av_freep(&h->list_counts);
750 e5017ab8 Laurent Aimar
751 0da71265 Michael Niedermayer
    av_freep(&h->mb2b_xy);
752 d43c1922 Michael Niedermayer
    av_freep(&h->mb2br_xy);
753 9f2d1b4f Loren Merritt
754 6752dd5a Reimar Döffinger
    for(i = 0; i < MAX_THREADS; i++) {
755 afebe2f7 Andreas Öman
        hx = h->thread_context[i];
756
        if(!hx) continue;
757
        av_freep(&hx->top_borders[1]);
758
        av_freep(&hx->top_borders[0]);
759
        av_freep(&hx->s.obmc_scratchpad);
760 2ed0f766 Ronald S. Bultje
        if (free_rbsp){
761 fb397b1a Ronald S. Bultje
            av_freep(&hx->rbsp_buffer[1]);
762
            av_freep(&hx->rbsp_buffer[0]);
763
            hx->rbsp_buffer_size[0] = 0;
764
            hx->rbsp_buffer_size[1] = 0;
765 2ed0f766 Ronald S. Bultje
        }
766 d2d5e067 Alexander Strange
        if (i) av_freep(&h->thread_context[i]);
767 afebe2f7 Andreas Öman
    }
768 0da71265 Michael Niedermayer
}
769
770 239ea04c Loren Merritt
static void init_dequant8_coeff_table(H264Context *h){
771
    int i,q,x;
772
    h->dequant8_coeff[0] = h->dequant8_buffer[0];
773
    h->dequant8_coeff[1] = h->dequant8_buffer[1];
774
775
    for(i=0; i<2; i++ ){
776
        if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
777
            h->dequant8_coeff[1] = h->dequant8_buffer[0];
778
            break;
779
        }
780
781
        for(q=0; q<52; q++){
782 d9ec210b Diego Pettenò
            int shift = div6[q];
783
            int idx = rem6[q];
784 239ea04c Loren Merritt
            for(x=0; x<64; x++)
785 ca32f7f2 Jason Garrett-Glaser
                h->dequant8_coeff[i][q][(x>>3)|((x&7)<<3)] =
786 548a1c8a Loren Merritt
                    ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
787
                    h->pps.scaling_matrix8[i][x]) << shift;
788 239ea04c Loren Merritt
        }
789
    }
790
}
791
792
static void init_dequant4_coeff_table(H264Context *h){
793
    int i,j,q,x;
794
    for(i=0; i<6; i++ ){
795
        h->dequant4_coeff[i] = h->dequant4_buffer[i];
796
        for(j=0; j<i; j++){
797
            if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
798
                h->dequant4_coeff[i] = h->dequant4_buffer[j];
799
                break;
800
            }
801
        }
802
        if(j<i)
803
            continue;
804
805
        for(q=0; q<52; q++){
806 d9ec210b Diego Pettenò
            int shift = div6[q] + 2;
807
            int idx = rem6[q];
808 239ea04c Loren Merritt
            for(x=0; x<16; x++)
809 ca32f7f2 Jason Garrett-Glaser
                h->dequant4_coeff[i][q][(x>>2)|((x<<2)&0xF)] =
810 ab2e3e2c Loren Merritt
                    ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
811 239ea04c Loren Merritt
                    h->pps.scaling_matrix4[i][x]) << shift;
812
        }
813
    }
814
}
815
816
static void init_dequant_tables(H264Context *h){
817
    int i,x;
818
    init_dequant4_coeff_table(h);
819
    if(h->pps.transform_8x8_mode)
820
        init_dequant8_coeff_table(h);
821
    if(h->sps.transform_bypass){
822
        for(i=0; i<6; i++)
823
            for(x=0; x<16; x++)
824
                h->dequant4_coeff[i][0][x] = 1<<6;
825
        if(h->pps.transform_8x8_mode)
826
            for(i=0; i<2; i++)
827
                for(x=0; x<64; x++)
828
                    h->dequant8_coeff[i][0][x] = 1<<6;
829
    }
830
}
831
832
833 903d58f6 Michael Niedermayer
int ff_h264_alloc_tables(H264Context *h){
834 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
835 7bc9090a Michael Niedermayer
    const int big_mb_num= s->mb_stride * (s->mb_height+1);
836 145061a1 Michael Niedermayer
    const int row_mb_num= 2*s->mb_stride*s->avctx->thread_count;
837 239ea04c Loren Merritt
    int x,y;
838 0da71265 Michael Niedermayer
839 145061a1 Michael Niedermayer
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, row_mb_num * 8  * sizeof(uint8_t), fail)
840 e5017ab8 Laurent Aimar
841 c988f975 Michael Niedermayer
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count    , big_mb_num * 32 * sizeof(uint8_t), fail)
842 d31dbec3 Ramiro Polla
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base  , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
843
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
844 0da71265 Michael Niedermayer
845 d31dbec3 Ramiro Polla
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
846 145061a1 Michael Niedermayer
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 16*row_mb_num * sizeof(uint8_t), fail);
847
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 16*row_mb_num * sizeof(uint8_t), fail);
848 36b54927 Michael Niedermayer
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 4*big_mb_num * sizeof(uint8_t) , fail);
849 c988f975 Michael Niedermayer
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->list_counts, big_mb_num * sizeof(uint8_t), fail)
850 e5017ab8 Laurent Aimar
851 b735aeea Michael Niedermayer
    memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride)  * sizeof(*h->slice_table_base));
852 5d18eaad Loren Merritt
    h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
853 0da71265 Michael Niedermayer
854 d31dbec3 Ramiro Polla
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy  , big_mb_num * sizeof(uint32_t), fail);
855 d43c1922 Michael Niedermayer
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2br_xy , big_mb_num * sizeof(uint32_t), fail);
856 0da71265 Michael Niedermayer
    for(y=0; y<s->mb_height; y++){
857
        for(x=0; x<s->mb_width; x++){
858 7bc9090a Michael Niedermayer
            const int mb_xy= x + y*s->mb_stride;
859 0da71265 Michael Niedermayer
            const int b_xy = 4*x + 4*y*h->b_stride;
860 115329f1 Diego Biurrun
861 0da71265 Michael Niedermayer
            h->mb2b_xy [mb_xy]= b_xy;
862 e1c88a21 Michael Niedermayer
            h->mb2br_xy[mb_xy]= 8*(FMO ? mb_xy : (mb_xy % (2*s->mb_stride)));
863 0da71265 Michael Niedermayer
        }
864
    }
865 9f2d1b4f Loren Merritt
866 9c6221ae Gert Vervoort
    s->obmc_scratchpad = NULL;
867
868 56edbd81 Loren Merritt
    if(!h->dequant4_coeff[0])
869
        init_dequant_tables(h);
870
871 0da71265 Michael Niedermayer
    return 0;
872
fail:
873 2ed0f766 Ronald S. Bultje
    free_tables(h, 1);
874 0da71265 Michael Niedermayer
    return -1;
875
}
876
877 afebe2f7 Andreas Öman
/**
878
 * Mimic alloc_tables(), but for every context thread.
879
 */
880 145061a1 Michael Niedermayer
static void clone_tables(H264Context *dst, H264Context *src, int i){
881
    MpegEncContext * const s = &src->s;
882
    dst->intra4x4_pred_mode       = src->intra4x4_pred_mode + i*8*2*s->mb_stride;
883 afebe2f7 Andreas Öman
    dst->non_zero_count           = src->non_zero_count;
884
    dst->slice_table              = src->slice_table;
885
    dst->cbp_table                = src->cbp_table;
886
    dst->mb2b_xy                  = src->mb2b_xy;
887 d43c1922 Michael Niedermayer
    dst->mb2br_xy                 = src->mb2br_xy;
888 afebe2f7 Andreas Öman
    dst->chroma_pred_mode_table   = src->chroma_pred_mode_table;
889 145061a1 Michael Niedermayer
    dst->mvd_table[0]             = src->mvd_table[0] + i*8*2*s->mb_stride;
890
    dst->mvd_table[1]             = src->mvd_table[1] + i*8*2*s->mb_stride;
891 afebe2f7 Andreas Öman
    dst->direct_table             = src->direct_table;
892 fb823b77 Michael Niedermayer
    dst->list_counts              = src->list_counts;
893 afebe2f7 Andreas Öman
894
    dst->s.obmc_scratchpad = NULL;
895
    ff_h264_pred_init(&dst->hpc, src->s.codec_id);
896
}
897
898
/**
899
 * Init context
900
 * Allocate buffers which are not shared amongst multiple threads.
901
 */
902
static int context_init(H264Context *h){
903 dc172ecc Oskar Arvidsson
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t)*2, fail)
904
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t)*2, fail)
905 afebe2f7 Andreas Öman
906 145061a1 Michael Niedermayer
    h->ref_cache[0][scan8[5 ]+1] = h->ref_cache[0][scan8[7 ]+1] = h->ref_cache[0][scan8[13]+1] =
907
    h->ref_cache[1][scan8[5 ]+1] = h->ref_cache[1][scan8[7 ]+1] = h->ref_cache[1][scan8[13]+1] = PART_NOT_AVAILABLE;
908
909 afebe2f7 Andreas Öman
    return 0;
910
fail:
911
    return -1; // free_tables will clean up for us
912
}
913
914 9855b2e3 Michael Niedermayer
static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size);
915
916 98a6fff9 Zuxy Meng
static av_cold void common_init(H264Context *h){
917 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
918
919
    s->width = s->avctx->width;
920
    s->height = s->avctx->height;
921
    s->codec_id= s->avctx->codec->id;
922 115329f1 Diego Biurrun
923 4693b031 Måns Rullgård
    ff_h264dsp_init(&h->h264dsp);
924 c92a30bb Kostya Shishkov
    ff_h264_pred_init(&h->hpc, s->codec_id);
925 0da71265 Michael Niedermayer
926 239ea04c Loren Merritt
    h->dequant_coeff_pps= -1;
927 9a41c2c7 Michael Niedermayer
    s->unrestricted_mv=1;
928 0da71265 Michael Niedermayer
    s->decode=1; //FIXME
929 56edbd81 Loren Merritt
930 a5805aa9 Michael Niedermayer
    dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
931
932 56edbd81 Loren Merritt
    memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
933
    memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
934 0da71265 Michael Niedermayer
}
935
936 05e95319 Howard Chu
int ff_h264_decode_extradata(H264Context *h)
937
{
938
    AVCodecContext *avctx = h->s.avctx;
939 9855b2e3 Michael Niedermayer
940 05e95319 Howard Chu
    if(*(char *)avctx->extradata == 1){
941 9855b2e3 Michael Niedermayer
        int i, cnt, nalsize;
942
        unsigned char *p = avctx->extradata;
943
944
        h->is_avc = 1;
945
946
        if(avctx->extradata_size < 7) {
947
            av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
948
            return -1;
949
        }
950
        /* sps and pps in the avcC always have length coded with 2 bytes,
951
           so put a fake nal_length_size = 2 while parsing them */
952
        h->nal_length_size = 2;
953
        // Decode sps from avcC
954
        cnt = *(p+5) & 0x1f; // Number of sps
955
        p += 6;
956
        for (i = 0; i < cnt; i++) {
957
            nalsize = AV_RB16(p) + 2;
958
            if(decode_nal_units(h, p, nalsize) < 0) {
959
                av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
960
                return -1;
961
            }
962
            p += nalsize;
963
        }
964
        // Decode pps from avcC
965
        cnt = *(p++); // Number of pps
966
        for (i = 0; i < cnt; i++) {
967
            nalsize = AV_RB16(p) + 2;
968 6e77feb2 Michael Niedermayer
            if(decode_nal_units(h, p, nalsize) < 0) {
969 9855b2e3 Michael Niedermayer
                av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
970
                return -1;
971
            }
972
            p += nalsize;
973
        }
974
        // Now store right nal length size, that will be use to parse all other nals
975
        h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
976
    } else {
977
        h->is_avc = 0;
978 05e95319 Howard Chu
        if(decode_nal_units(h, avctx->extradata, avctx->extradata_size) < 0)
979 9855b2e3 Michael Niedermayer
            return -1;
980
    }
981 05e95319 Howard Chu
    return 0;
982
}
983
984
av_cold int ff_h264_decode_init(AVCodecContext *avctx){
985
    H264Context *h= avctx->priv_data;
986
    MpegEncContext * const s = &h->s;
987
988
    MPV_decode_defaults(s);
989
990
    s->avctx = avctx;
991
    common_init(h);
992
993
    s->out_format = FMT_H264;
994
    s->workaround_bugs= avctx->workaround_bugs;
995
996
    // set defaults
997
//    s->decode_mb= ff_h263_decode_mb;
998
    s->quarter_sample = 1;
999
    if(!avctx->has_b_frames)
1000
    s->low_delay= 1;
1001
1002
    avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1003
1004
    ff_h264_decode_init_vlc();
1005
1006 dc172ecc Oskar Arvidsson
    h->pixel_size = 1;
1007
1008 05e95319 Howard Chu
    h->thread_context[0] = h;
1009 d375c104 Michael Niedermayer
    h->outputed_poc = h->next_outputed_poc = INT_MIN;
1010 05e95319 Howard Chu
    h->prev_poc_msb= 1<<16;
1011
    h->x264_build = -1;
1012
    ff_h264_reset_sei(h);
1013
    if(avctx->codec_id == CODEC_ID_H264){
1014
        if(avctx->ticks_per_frame == 1){
1015
            s->avctx->time_base.den *=2;
1016
        }
1017
        avctx->ticks_per_frame = 2;
1018
    }
1019
1020
    if(avctx->extradata_size > 0 && avctx->extradata &&
1021
        ff_h264_decode_extradata(h))
1022
        return -1;
1023
1024 db8cb47d Michael Niedermayer
    if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames < h->sps.num_reorder_frames){
1025
        s->avctx->has_b_frames = h->sps.num_reorder_frames;
1026
        s->low_delay = 0;
1027
    }
1028 9855b2e3 Michael Niedermayer
1029 0da71265 Michael Niedermayer
    return 0;
1030
}
1031
1032 d375c104 Michael Niedermayer
static void copy_picture_range(Picture **to, Picture **from, int count, MpegEncContext *new_base, MpegEncContext *old_base)
1033
{
1034
    int i;
1035
1036
    for (i=0; i<count; i++){
1037
        to[i] = REBASE_PICTURE(from[i], new_base, old_base);
1038
    }
1039
}
1040
1041
static void copy_parameter_set(void **to, void **from, int count, int size)
1042
{
1043
    int i;
1044
1045
    for (i=0; i<count; i++){
1046
        if (to[i] && !from[i]) av_freep(&to[i]);
1047
        else if (from[i] && !to[i]) to[i] = av_malloc(size);
1048
1049
        if (from[i]) memcpy(to[i], from[i], size);
1050
    }
1051
}
1052
1053
static int decode_init_thread_copy(AVCodecContext *avctx){
1054
    H264Context *h= avctx->priv_data;
1055
1056
    if (!avctx->is_copy) return 0;
1057
    memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1058
    memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1059
1060
    return 0;
1061
}
1062
1063
#define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
1064
static int decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src){
1065
    H264Context *h= dst->priv_data, *h1= src->priv_data;
1066
    MpegEncContext * const s = &h->s, * const s1 = &h1->s;
1067
    int inited = s->context_initialized, err;
1068
    int i;
1069
1070
    if(dst == src || !s1->context_initialized) return 0;
1071
1072
    err = ff_mpeg_update_thread_context(dst, src);
1073
    if(err) return err;
1074
1075
    //FIXME handle width/height changing
1076
    if(!inited){
1077
        for(i = 0; i < MAX_SPS_COUNT; i++)
1078
            av_freep(h->sps_buffers + i);
1079
1080
        for(i = 0; i < MAX_PPS_COUNT; i++)
1081
            av_freep(h->pps_buffers + i);
1082
1083
        memcpy(&h->s + 1, &h1->s + 1, sizeof(H264Context) - sizeof(MpegEncContext)); //copy all fields after MpegEnc
1084
        memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1085
        memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1086
        ff_h264_alloc_tables(h);
1087
        context_init(h);
1088
1089
        for(i=0; i<2; i++){
1090
            h->rbsp_buffer[i] = NULL;
1091
            h->rbsp_buffer_size[i] = 0;
1092
        }
1093
1094
        h->thread_context[0] = h;
1095
1096
        // frame_start may not be called for the next thread (if it's decoding a bottom field)
1097
        // so this has to be allocated here
1098
        h->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
1099
1100
        s->dsp.clear_blocks(h->mb);
1101
    }
1102
1103
    //extradata/NAL handling
1104
    h->is_avc          = h1->is_avc;
1105
1106
    //SPS/PPS
1107
    copy_parameter_set((void**)h->sps_buffers, (void**)h1->sps_buffers, MAX_SPS_COUNT, sizeof(SPS));
1108
    h->sps             = h1->sps;
1109
    copy_parameter_set((void**)h->pps_buffers, (void**)h1->pps_buffers, MAX_PPS_COUNT, sizeof(PPS));
1110
    h->pps             = h1->pps;
1111
1112
    //Dequantization matrices
1113
    //FIXME these are big - can they be only copied when PPS changes?
1114
    copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
1115
1116
    for(i=0; i<6; i++)
1117
        h->dequant4_coeff[i] = h->dequant4_buffer[0] + (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
1118
1119
    for(i=0; i<2; i++)
1120
        h->dequant8_coeff[i] = h->dequant8_buffer[0] + (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
1121
1122
    h->dequant_coeff_pps = h1->dequant_coeff_pps;
1123
1124
    //POC timing
1125
    copy_fields(h, h1, poc_lsb, redundant_pic_count);
1126
1127
    //reference lists
1128
    copy_fields(h, h1, ref_count, intra_gb);
1129
    copy_fields(h, h1, short_ref, cabac_init_idc);
1130
1131
    copy_picture_range(h->short_ref,   h1->short_ref,   32, s, s1);
1132
    copy_picture_range(h->long_ref,    h1->long_ref,    32,  s, s1);
1133
    copy_picture_range(h->delayed_pic, h1->delayed_pic, MAX_DELAYED_PIC_COUNT+2, s, s1);
1134
1135
    h->last_slice_type = h1->last_slice_type;
1136
1137
    if(!s->current_picture_ptr) return 0;
1138
1139
    if(!s->dropable) {
1140
        ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
1141
        h->prev_poc_msb     = h->poc_msb;
1142
        h->prev_poc_lsb     = h->poc_lsb;
1143
    }
1144
    h->prev_frame_num_offset= h->frame_num_offset;
1145
    h->prev_frame_num       = h->frame_num;
1146
    h->outputed_poc         = h->next_outputed_poc;
1147
1148
    return 0;
1149
}
1150
1151 903d58f6 Michael Niedermayer
int ff_h264_frame_start(H264Context *h){
1152 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
1153
    int i;
1154
1155 af8aa846 Michael Niedermayer
    if(MPV_frame_start(s, s->avctx) < 0)
1156
        return -1;
1157 0da71265 Michael Niedermayer
    ff_er_frame_start(s);
1158 3a22d7fa Jeff Downs
    /*
1159
     * MPV_frame_start uses pict_type to derive key_frame.
1160
     * This is incorrect for H.264; IDR markings must be used.
1161 1412060e Diego Biurrun
     * Zero here; IDR markings per slice in frame or fields are ORed in later.
1162 3a22d7fa Jeff Downs
     * See decode_nal_units().
1163
     */
1164
    s->current_picture_ptr->key_frame= 0;
1165 c173a088 Haruhiko Yamagata
    s->current_picture_ptr->mmco_reset= 0;
1166 0da71265 Michael Niedermayer
1167
    assert(s->linesize && s->uvlinesize);
1168
1169
    for(i=0; i<16; i++){
1170 dc172ecc Oskar Arvidsson
        h->block_offset[i]= 4*((scan8[i] - scan8[0])&7)*h->pixel_size + 4*s->linesize*((scan8[i] - scan8[0])>>3);
1171
        h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7)*h->pixel_size + 8*s->linesize*((scan8[i] - scan8[0])>>3);
1172 0da71265 Michael Niedermayer
    }
1173
    for(i=0; i<4; i++){
1174
        h->block_offset[16+i]=
1175 dc172ecc Oskar Arvidsson
        h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7)*h->pixel_size + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
1176 6867a90b Loic Le Loarer
        h->block_offset[24+16+i]=
1177 dc172ecc Oskar Arvidsson
        h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7)*h->pixel_size + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
1178 0da71265 Michael Niedermayer
    }
1179
1180 934b0821 Loren Merritt
    /* can't be in alloc_tables because linesize isn't known there.
1181
     * FIXME: redo bipred weight to not require extra buffer? */
1182 afebe2f7 Andreas Öman
    for(i = 0; i < s->avctx->thread_count; i++)
1183 0f016023 Alexander Strange
        if(h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
1184 afebe2f7 Andreas Öman
            h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
1185 5d18eaad Loren Merritt
1186 2ce1c2e0 Michael Niedermayer
    /* some macroblocks can be accessed before they're available in case of lost slices, mbaff or threading*/
1187 5820b90d Michael Niedermayer
    memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
1188 934b0821 Loren Merritt
1189 0da71265 Michael Niedermayer
//    s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
1190 28bb9eb2 Michael Niedermayer
1191 1412060e Diego Biurrun
    // We mark the current picture as non-reference after allocating it, so
1192 28bb9eb2 Michael Niedermayer
    // that if we break out due to an error it can be released automatically
1193
    // in the next MPV_frame_start().
1194
    // SVQ3 as well as most other codecs have only last/next/current and thus
1195
    // get released even with set reference, besides SVQ3 and others do not
1196
    // mark frames as reference later "naturally".
1197
    if(s->codec_id != CODEC_ID_SVQ3)
1198
        s->current_picture_ptr->reference= 0;
1199 357282c6 Michael Niedermayer
1200
    s->current_picture_ptr->field_poc[0]=
1201
    s->current_picture_ptr->field_poc[1]= INT_MAX;
1202 d375c104 Michael Niedermayer
1203
    h->next_output_pic = NULL;
1204
1205 5118c6c7 Michael Niedermayer
    assert(s->current_picture_ptr->long_ref==0);
1206 357282c6 Michael Niedermayer
1207 af8aa846 Michael Niedermayer
    return 0;
1208 0da71265 Michael Niedermayer
}
1209
1210 d375c104 Michael Niedermayer
/**
1211
  * Run setup operations that must be run after slice header decoding.
1212
  * This includes finding the next displayed frame.
1213
  *
1214
  * @param h h264 master context
1215
  */
1216
static void decode_postinit(H264Context *h){
1217
    MpegEncContext * const s = &h->s;
1218
    Picture *out = s->current_picture_ptr;
1219
    Picture *cur = s->current_picture_ptr;
1220
    int i, pics, out_of_order, out_idx;
1221
1222
    s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
1223
    s->current_picture_ptr->pict_type= s->pict_type;
1224
1225
    if (h->next_output_pic) return;
1226
1227
    if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
1228
        //FIXME this allows the next thread to start once we encounter the first field of a PAFF packet
1229
        //This works if the next packet contains the second field. It does not work if both fields are
1230
        //in the same packet.
1231
        //ff_thread_finish_setup(s->avctx);
1232
        return;
1233
    }
1234
1235
    cur->interlaced_frame = 0;
1236
    cur->repeat_pict = 0;
1237
1238
    /* Signal interlacing information externally. */
1239
    /* Prioritize picture timing SEI information over used decoding process if it exists. */
1240
1241
    if(h->sps.pic_struct_present_flag){
1242
        switch (h->sei_pic_struct)
1243
        {
1244
        case SEI_PIC_STRUCT_FRAME:
1245
            break;
1246
        case SEI_PIC_STRUCT_TOP_FIELD:
1247
        case SEI_PIC_STRUCT_BOTTOM_FIELD:
1248
            cur->interlaced_frame = 1;
1249
            break;
1250
        case SEI_PIC_STRUCT_TOP_BOTTOM:
1251
        case SEI_PIC_STRUCT_BOTTOM_TOP:
1252
            if (FIELD_OR_MBAFF_PICTURE)
1253
                cur->interlaced_frame = 1;
1254
            else
1255
                // try to flag soft telecine progressive
1256
                cur->interlaced_frame = h->prev_interlaced_frame;
1257
            break;
1258
        case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
1259
        case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
1260
            // Signal the possibility of telecined film externally (pic_struct 5,6)
1261
            // From these hints, let the applications decide if they apply deinterlacing.
1262
            cur->repeat_pict = 1;
1263
            break;
1264
        case SEI_PIC_STRUCT_FRAME_DOUBLING:
1265
            // Force progressive here, as doubling interlaced frame is a bad idea.
1266
            cur->repeat_pict = 2;
1267
            break;
1268
        case SEI_PIC_STRUCT_FRAME_TRIPLING:
1269
            cur->repeat_pict = 4;
1270
            break;
1271
        }
1272
1273
        if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
1274
            cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
1275
    }else{
1276
        /* Derive interlacing flag from used decoding process. */
1277
        cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
1278
    }
1279
    h->prev_interlaced_frame = cur->interlaced_frame;
1280
1281
    if (cur->field_poc[0] != cur->field_poc[1]){
1282
        /* Derive top_field_first from field pocs. */
1283
        cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
1284
    }else{
1285
        if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
1286
            /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
1287
            if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
1288
              || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
1289
                cur->top_field_first = 1;
1290
            else
1291
                cur->top_field_first = 0;
1292
        }else{
1293
            /* Most likely progressive */
1294
            cur->top_field_first = 0;
1295
        }
1296
    }
1297
1298
    //FIXME do something with unavailable reference frames
1299
1300
    /* Sort B-frames into display order */
1301
1302
    if(h->sps.bitstream_restriction_flag
1303
       && s->avctx->has_b_frames < h->sps.num_reorder_frames){
1304
        s->avctx->has_b_frames = h->sps.num_reorder_frames;
1305
        s->low_delay = 0;
1306
    }
1307
1308
    if(   s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
1309
       && !h->sps.bitstream_restriction_flag){
1310
        s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
1311
        s->low_delay= 0;
1312
    }
1313
1314
    pics = 0;
1315
    while(h->delayed_pic[pics]) pics++;
1316
1317
    assert(pics <= MAX_DELAYED_PIC_COUNT);
1318
1319
    h->delayed_pic[pics++] = cur;
1320
    if(cur->reference == 0)
1321
        cur->reference = DELAYED_PIC_REF;
1322
1323
    out = h->delayed_pic[0];
1324
    out_idx = 0;
1325
    for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
1326
        if(h->delayed_pic[i]->poc < out->poc){
1327
            out = h->delayed_pic[i];
1328
            out_idx = i;
1329
        }
1330
    if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
1331
        h->next_outputed_poc= INT_MIN;
1332
    out_of_order = out->poc < h->next_outputed_poc;
1333
1334
    if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
1335
        { }
1336
    else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
1337
       || (s->low_delay &&
1338
        ((h->next_outputed_poc != INT_MIN && out->poc > h->next_outputed_poc + 2)
1339
         || cur->pict_type == FF_B_TYPE)))
1340
    {
1341
        s->low_delay = 0;
1342
        s->avctx->has_b_frames++;
1343
    }
1344
1345
    if(out_of_order || pics > s->avctx->has_b_frames){
1346
        out->reference &= ~DELAYED_PIC_REF;
1347
        for(i=out_idx; h->delayed_pic[i]; i++)
1348
            h->delayed_pic[i] = h->delayed_pic[i+1];
1349
    }
1350
    if(!out_of_order && pics > s->avctx->has_b_frames){
1351
        h->next_output_pic = out;
1352
        if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
1353
            h->next_outputed_poc = INT_MIN;
1354
        } else
1355
            h->next_outputed_poc = out->poc;
1356
    }else{
1357
        av_log(s->avctx, AV_LOG_DEBUG, "no picture\n");
1358
    }
1359
1360
    ff_thread_finish_setup(s->avctx);
1361
}
1362
1363 93cc10fa Andreas Öman
static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int simple){
1364 53c05b1e Michael Niedermayer
    MpegEncContext * const s = &h->s;
1365 0b69d625 Alexander Strange
    uint8_t *top_border;
1366 5f7f9719 Michael Niedermayer
    int top_idx = 1;
1367 115329f1 Diego Biurrun
1368 53c05b1e Michael Niedermayer
    src_y  -=   linesize;
1369
    src_cb -= uvlinesize;
1370
    src_cr -= uvlinesize;
1371
1372 5f7f9719 Michael Niedermayer
    if(!simple && FRAME_MBAFF){
1373
        if(s->mb_y&1){
1374
            if(!MB_MBAFF){
1375 0b69d625 Alexander Strange
                top_border = h->top_borders[0][s->mb_x];
1376
                AV_COPY128(top_border, src_y + 15*linesize);
1377 dc172ecc Oskar Arvidsson
                if (h->pixel_size == 2)
1378
                    AV_COPY128(top_border+16, src_y+15*linesize+16);
1379 49fb20cb Aurelien Jacobs
                if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1380 dc172ecc Oskar Arvidsson
                    if (h->pixel_size == 2) {
1381
                        AV_COPY128(top_border+32, src_cb+7*uvlinesize);
1382
                        AV_COPY128(top_border+48, src_cr+7*uvlinesize);
1383
                    } else {
1384 0b69d625 Alexander Strange
                    AV_COPY64(top_border+16, src_cb+7*uvlinesize);
1385
                    AV_COPY64(top_border+24, src_cr+7*uvlinesize);
1386 dc172ecc Oskar Arvidsson
                    }
1387 5f7f9719 Michael Niedermayer
                }
1388
            }
1389 c988f975 Michael Niedermayer
        }else if(MB_MBAFF){
1390
            top_idx = 0;
1391
        }else
1392
            return;
1393 5f7f9719 Michael Niedermayer
    }
1394
1395 0b69d625 Alexander Strange
    top_border = h->top_borders[top_idx][s->mb_x];
1396 3b66c4c5 Kevin Baragona
    // There are two lines saved, the line above the the top macroblock of a pair,
1397 6867a90b Loic Le Loarer
    // and the line above the bottom macroblock
1398 0b69d625 Alexander Strange
    AV_COPY128(top_border, src_y + 16*linesize);
1399 dc172ecc Oskar Arvidsson
    if (h->pixel_size == 2)
1400
        AV_COPY128(top_border+16, src_y+16*linesize+16);
1401 53c05b1e Michael Niedermayer
1402 49fb20cb Aurelien Jacobs
    if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1403 dc172ecc Oskar Arvidsson
        if (h->pixel_size == 2) {
1404
            AV_COPY128(top_border+32, src_cb+8*uvlinesize);
1405
            AV_COPY128(top_border+48, src_cr+8*uvlinesize);
1406
        } else {
1407 0b69d625 Alexander Strange
        AV_COPY64(top_border+16, src_cb+8*uvlinesize);
1408
        AV_COPY64(top_border+24, src_cr+8*uvlinesize);
1409 dc172ecc Oskar Arvidsson
        }
1410 53c05b1e Michael Niedermayer
    }
1411
}
1412
1413 93cc10fa Andreas Öman
static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg, int simple){
1414 53c05b1e Michael Niedermayer
    MpegEncContext * const s = &h->s;
1415 b69378e2 Andreas Öman
    int deblock_left;
1416
    int deblock_top;
1417 5f7f9719 Michael Niedermayer
    int top_idx = 1;
1418 1e4f1c56 Alexander Strange
    uint8_t *top_border_m1;
1419
    uint8_t *top_border;
1420 5f7f9719 Michael Niedermayer
1421
    if(!simple && FRAME_MBAFF){
1422
        if(s->mb_y&1){
1423 c988f975 Michael Niedermayer
            if(!MB_MBAFF)
1424
                return;
1425 5f7f9719 Michael Niedermayer
        }else{
1426
            top_idx = MB_MBAFF ? 0 : 1;
1427
        }
1428
    }
1429 b69378e2 Andreas Öman
1430
    if(h->deblocking_filter == 2) {
1431 024bf79f Michael Niedermayer
        deblock_left = h->left_type[0];
1432
        deblock_top  = h->top_type;
1433 b69378e2 Andreas Öman
    } else {
1434
        deblock_left = (s->mb_x > 0);
1435 6c805007 Michael Niedermayer
        deblock_top =  (s->mb_y > !!MB_FIELD);
1436 b69378e2 Andreas Öman
    }
1437 53c05b1e Michael Niedermayer
1438 dc172ecc Oskar Arvidsson
    src_y  -=   linesize + h->pixel_size;
1439
    src_cb -= uvlinesize + h->pixel_size;
1440
    src_cr -= uvlinesize + h->pixel_size;
1441 53c05b1e Michael Niedermayer
1442 1e4f1c56 Alexander Strange
    top_border_m1 = h->top_borders[top_idx][s->mb_x-1];
1443
    top_border    = h->top_borders[top_idx][s->mb_x];
1444
1445 0b69d625 Alexander Strange
#define XCHG(a,b,xchg)\
1446 dc172ecc Oskar Arvidsson
    if (h->pixel_size == 2) {\
1447
        if (xchg) {\
1448
            AV_SWAP64(b+0,a+0);\
1449
            AV_SWAP64(b+8,a+8);\
1450
        } else {\
1451
            AV_COPY128(b,a); \
1452
        }\
1453
    } else \
1454 0b69d625 Alexander Strange
if (xchg) AV_SWAP64(b,a);\
1455
else      AV_COPY64(b,a);
1456 d89dc06a Loren Merritt
1457
    if(deblock_top){
1458 c988f975 Michael Niedermayer
        if(deblock_left){
1459 dc172ecc Oskar Arvidsson
            XCHG(top_border_m1+8*h->pixel_size, src_y -7*h->pixel_size, 1);
1460 c988f975 Michael Niedermayer
        }
1461 dc172ecc Oskar Arvidsson
        XCHG(top_border+0*h->pixel_size, src_y +1*h->pixel_size, xchg);
1462
        XCHG(top_border+8*h->pixel_size, src_y +9*h->pixel_size, 1);
1463 cad4368a Reimar Döffinger
        if(s->mb_x+1 < s->mb_width){
1464 dc172ecc Oskar Arvidsson
            XCHG(h->top_borders[top_idx][s->mb_x+1], src_y +17*h->pixel_size, 1);
1465 43efd19a Loren Merritt
        }
1466 53c05b1e Michael Niedermayer
    }
1467 49fb20cb Aurelien Jacobs
    if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1468 d89dc06a Loren Merritt
        if(deblock_top){
1469 c988f975 Michael Niedermayer
            if(deblock_left){
1470 dc172ecc Oskar Arvidsson
                XCHG(top_border_m1+16*h->pixel_size, src_cb -7*h->pixel_size, 1);
1471
                XCHG(top_border_m1+24*h->pixel_size, src_cr -7*h->pixel_size, 1);
1472 c988f975 Michael Niedermayer
            }
1473 dc172ecc Oskar Arvidsson
            XCHG(top_border+16*h->pixel_size, src_cb+h->pixel_size, 1);
1474
            XCHG(top_border+24*h->pixel_size, src_cr+h->pixel_size, 1);
1475 53c05b1e Michael Niedermayer
        }
1476
    }
1477
}
1478
1479 dc172ecc Oskar Arvidsson
static av_always_inline int dctcoef_get(H264Context *h, DCTELEM *mb, int index) {
1480
    if (h->pixel_size == 1)
1481
        return mb[index];
1482
    else
1483
        return ((int32_t*)mb)[index];
1484
}
1485
1486
static av_always_inline void dctcoef_set(H264Context *h, DCTELEM *mb, int index, int value) {
1487
    if (h->pixel_size == 1)
1488
        mb[index] = value;
1489
    else
1490
        ((int32_t*)mb)[index] = value;
1491
}
1492
1493 5a6a6cc7 Diego Biurrun
static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
1494 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
1495
    const int mb_x= s->mb_x;
1496
    const int mb_y= s->mb_y;
1497 64514ee8 Alexander Strange
    const int mb_xy= h->mb_xy;
1498 0da71265 Michael Niedermayer
    const int mb_type= s->current_picture.mb_type[mb_xy];
1499
    uint8_t  *dest_y, *dest_cb, *dest_cr;
1500
    int linesize, uvlinesize /*dct_offset*/;
1501
    int i;
1502 6867a90b Loic Le Loarer
    int *block_offset = &h->block_offset[0];
1503 41e4055b Michael Niedermayer
    const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
1504 8b6871ed Diego Biurrun
    /* is_h264 should always be true if SVQ3 is disabled. */
1505 49fb20cb Aurelien Jacobs
    const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
1506 36940eca Loren Merritt
    void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
1507 ef9d1d15 Loren Merritt
    void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
1508 0da71265 Michael Niedermayer
1509 dc172ecc Oskar Arvidsson
    dest_y  = s->current_picture.data[0] + (mb_x*h->pixel_size + mb_y * s->linesize  ) * 16;
1510
    dest_cb = s->current_picture.data[1] + (mb_x*h->pixel_size + mb_y * s->uvlinesize) * 8;
1511
    dest_cr = s->current_picture.data[2] + (mb_x*h->pixel_size + mb_y * s->uvlinesize) * 8;
1512 0da71265 Michael Niedermayer
1513 dc172ecc Oskar Arvidsson
    s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64*h->pixel_size, s->linesize, 4);
1514
    s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64*h->pixel_size, dest_cr - dest_cb, 2);
1515 a957c27b Loren Merritt
1516 c988f975 Michael Niedermayer
    h->list_counts[mb_xy]= h->list_count;
1517
1518 bd91fee3 Alexander Strange
    if (!simple && MB_FIELD) {
1519 5d18eaad Loren Merritt
        linesize   = h->mb_linesize   = s->linesize * 2;
1520
        uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
1521 6867a90b Loic Le Loarer
        block_offset = &h->block_offset[24];
1522 1412060e Diego Biurrun
        if(mb_y&1){ //FIXME move out of this function?
1523 0da71265 Michael Niedermayer
            dest_y -= s->linesize*15;
1524 6867a90b Loic Le Loarer
            dest_cb-= s->uvlinesize*7;
1525
            dest_cr-= s->uvlinesize*7;
1526 0da71265 Michael Niedermayer
        }
1527 5d18eaad Loren Merritt
        if(FRAME_MBAFF) {
1528
            int list;
1529 3425501d Michael Niedermayer
            for(list=0; list<h->list_count; list++){
1530 5d18eaad Loren Merritt
                if(!USES_LIST(mb_type, list))
1531
                    continue;
1532
                if(IS_16X16(mb_type)){
1533
                    int8_t *ref = &h->ref_cache[list][scan8[0]];
1534 1710856c Andreas Öman
                    fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
1535 5d18eaad Loren Merritt
                }else{
1536
                    for(i=0; i<16; i+=4){
1537
                        int ref = h->ref_cache[list][scan8[i]];
1538
                        if(ref >= 0)
1539 1710856c Andreas Öman
                            fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
1540 5d18eaad Loren Merritt
                    }
1541
                }
1542
            }
1543
        }
1544 0da71265 Michael Niedermayer
    } else {
1545 5d18eaad Loren Merritt
        linesize   = h->mb_linesize   = s->linesize;
1546
        uvlinesize = h->mb_uvlinesize = s->uvlinesize;
1547 0da71265 Michael Niedermayer
//        dct_offset = s->linesize * 16;
1548
    }
1549 115329f1 Diego Biurrun
1550 bd91fee3 Alexander Strange
    if (!simple && IS_INTRA_PCM(mb_type)) {
1551 dc172ecc Oskar Arvidsson
        if (h->pixel_size == 2) {
1552
            const int bit_depth = h->sps.bit_depth_luma;
1553
            int j;
1554
            GetBitContext gb;
1555
            init_get_bits(&gb, (uint8_t*)h->mb, 384*bit_depth);
1556
1557
            for (i = 0; i < 16; i++) {
1558
                uint16_t *tmp_y  = (uint16_t*)(dest_y  + i*linesize);
1559
                for (j = 0; j < 16; j++)
1560
                    tmp_y[j] = get_bits(&gb, bit_depth);
1561
            }
1562
            for (i = 0; i < 8; i++) {
1563
                uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
1564
                for (j = 0; j < 8; j++)
1565
                    tmp_cb[j] = get_bits(&gb, bit_depth);
1566
            }
1567
            for (i = 0; i < 8; i++) {
1568
                uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
1569
                for (j = 0; j < 8; j++)
1570
                    tmp_cr[j] = get_bits(&gb, bit_depth);
1571
            }
1572
        } else {
1573 c1708e8d Michael Niedermayer
        for (i=0; i<16; i++) {
1574
            memcpy(dest_y + i*  linesize, h->mb       + i*8, 16);
1575 6fbcaaa0 Loic Le Loarer
        }
1576 c1708e8d Michael Niedermayer
        for (i=0; i<8; i++) {
1577
            memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4,  8);
1578
            memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4,  8);
1579 6fbcaaa0 Loic Le Loarer
        }
1580 dc172ecc Oskar Arvidsson
        }
1581 e7e09b49 Loic Le Loarer
    } else {
1582
        if(IS_INTRA(mb_type)){
1583 5f7f9719 Michael Niedermayer
            if(h->deblocking_filter)
1584 93cc10fa Andreas Öman
                xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
1585 53c05b1e Michael Niedermayer
1586 49fb20cb Aurelien Jacobs
            if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1587 c92a30bb Kostya Shishkov
                h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
1588
                h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
1589 e7e09b49 Loic Le Loarer
            }
1590 0da71265 Michael Niedermayer
1591 e7e09b49 Loic Le Loarer
            if(IS_INTRA4x4(mb_type)){
1592 bd91fee3 Alexander Strange
                if(simple || !s->encoding){
1593 43efd19a Loren Merritt
                    if(IS_8x8DCT(mb_type)){
1594 1eb96035 Michael Niedermayer
                        if(transform_bypass){
1595
                            idct_dc_add =
1596
                            idct_add    = s->dsp.add_pixels8;
1597 dae006d7 Michael Niedermayer
                        }else{
1598 4693b031 Måns Rullgård
                            idct_dc_add = h->h264dsp.h264_idct8_dc_add;
1599
                            idct_add    = h->h264dsp.h264_idct8_add;
1600 1eb96035 Michael Niedermayer
                        }
1601 43efd19a Loren Merritt
                        for(i=0; i<16; i+=4){
1602
                            uint8_t * const ptr= dest_y + block_offset[i];
1603
                            const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
1604 41e4055b Michael Niedermayer
                            if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
1605 dc172ecc Oskar Arvidsson
                                h->hpc.pred8x8l_add[dir](ptr, h->mb + i*16*h->pixel_size, linesize);
1606 41e4055b Michael Niedermayer
                            }else{
1607 ac0623b2 Michael Niedermayer
                                const int nnz = h->non_zero_count_cache[ scan8[i] ];
1608
                                h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
1609
                                                            (h->topright_samples_available<<i)&0x4000, linesize);
1610
                                if(nnz){
1611 dc172ecc Oskar Arvidsson
                                    if(nnz == 1 && dctcoef_get(h, h->mb, i*16))
1612
                                        idct_dc_add(ptr, h->mb + i*16*h->pixel_size, linesize);
1613 ac0623b2 Michael Niedermayer
                                    else
1614 dc172ecc Oskar Arvidsson
                                        idct_add   (ptr, h->mb + i*16*h->pixel_size, linesize);
1615 ac0623b2 Michael Niedermayer
                                }
1616 41e4055b Michael Niedermayer
                            }
1617 43efd19a Loren Merritt
                        }
1618 1eb96035 Michael Niedermayer
                    }else{
1619
                        if(transform_bypass){
1620
                            idct_dc_add =
1621
                            idct_add    = s->dsp.add_pixels4;
1622
                        }else{
1623 4693b031 Måns Rullgård
                            idct_dc_add = h->h264dsp.h264_idct_dc_add;
1624
                            idct_add    = h->h264dsp.h264_idct_add;
1625 1eb96035 Michael Niedermayer
                        }
1626 aebb5d6d Michael Niedermayer
                        for(i=0; i<16; i++){
1627
                            uint8_t * const ptr= dest_y + block_offset[i];
1628
                            const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
1629 e7e09b49 Loic Le Loarer
1630 aebb5d6d Michael Niedermayer
                            if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
1631 dc172ecc Oskar Arvidsson
                                h->hpc.pred4x4_add[dir](ptr, h->mb + i*16*h->pixel_size, linesize);
1632 aebb5d6d Michael Niedermayer
                            }else{
1633
                                uint8_t *topright;
1634
                                int nnz, tr;
1635 dc172ecc Oskar Arvidsson
                                uint64_t tr_high;
1636 aebb5d6d Michael Niedermayer
                                if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
1637
                                    const int topright_avail= (h->topright_samples_available<<i)&0x8000;
1638
                                    assert(mb_y || linesize <= block_offset[i]);
1639
                                    if(!topright_avail){
1640 dc172ecc Oskar Arvidsson
                                        if (h->pixel_size == 2) {
1641
                                            tr_high= ((uint16_t*)ptr)[3 - linesize/2]*0x0001000100010001ULL;
1642
                                            topright= (uint8_t*) &tr_high;
1643
                                        } else {
1644 aebb5d6d Michael Niedermayer
                                        tr= ptr[3 - linesize]*0x01010101;
1645
                                        topright= (uint8_t*) &tr;
1646 dc172ecc Oskar Arvidsson
                                        }
1647 aebb5d6d Michael Niedermayer
                                    }else
1648 dc172ecc Oskar Arvidsson
                                        topright= ptr + 4*h->pixel_size - linesize;
1649 ac0623b2 Michael Niedermayer
                                }else
1650 aebb5d6d Michael Niedermayer
                                    topright= NULL;
1651
1652
                                h->hpc.pred4x4[ dir ](ptr, topright, linesize);
1653
                                nnz = h->non_zero_count_cache[ scan8[i] ];
1654
                                if(nnz){
1655
                                    if(is_h264){
1656 dc172ecc Oskar Arvidsson
                                        if(nnz == 1 && dctcoef_get(h, h->mb, i*16))
1657
                                            idct_dc_add(ptr, h->mb + i*16*h->pixel_size, linesize);
1658 aebb5d6d Michael Niedermayer
                                        else
1659 dc172ecc Oskar Arvidsson
                                            idct_add   (ptr, h->mb + i*16*h->pixel_size, linesize);
1660 007b1f04 Peter Ross
                                    }
1661
#if CONFIG_SVQ3_DECODER
1662
                                    else
1663 881b5b80 Rafaël Carré
                                        ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
1664 007b1f04 Peter Ross
#endif
1665 aebb5d6d Michael Niedermayer
                                }
1666 ac0623b2 Michael Niedermayer
                            }
1667 41e4055b Michael Niedermayer
                        }
1668 8b82a956 Michael Niedermayer
                    }
1669 0da71265 Michael Niedermayer
                }
1670 e7e09b49 Loic Le Loarer
            }else{
1671 c92a30bb Kostya Shishkov
                h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
1672 2e186601 Jason Garrett-Glaser
                if(is_h264){
1673
                    if(h->non_zero_count_cache[ scan8[LUMA_DC_BLOCK_INDEX] ]){
1674 0d1d01cf Jason Garrett-Glaser
                        if(!transform_bypass)
1675
                            h->h264dsp.h264_luma_dc_dequant_idct(h->mb, h->mb_luma_dc, h->dequant4_coeff[0][s->qscale][0]);
1676
                        else{
1677
                            static const uint8_t dc_mapping[16] = { 0*16, 1*16, 4*16, 5*16, 2*16, 3*16, 6*16, 7*16,
1678
                                                                    8*16, 9*16,12*16,13*16,10*16,11*16,14*16,15*16};
1679
                            for(i = 0; i < 16; i++)
1680 dc172ecc Oskar Arvidsson
                                dctcoef_set(h, h->mb, dc_mapping[i], dctcoef_get(h, h->mb_luma_dc, i));
1681 0d1d01cf Jason Garrett-Glaser
                        }
1682 2e186601 Jason Garrett-Glaser
                    }
1683 007b1f04 Peter Ross
                }
1684
#if CONFIG_SVQ3_DECODER
1685
                else
1686 290fabc6 Jason Garrett-Glaser
                    ff_svq3_luma_dc_dequant_idct_c(h->mb, h->mb_luma_dc, s->qscale);
1687 007b1f04 Peter Ross
#endif
1688 0da71265 Michael Niedermayer
            }
1689 5f7f9719 Michael Niedermayer
            if(h->deblocking_filter)
1690 93cc10fa Andreas Öman
                xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
1691 bd91fee3 Alexander Strange
        }else if(is_h264){
1692 e7e09b49 Loic Le Loarer
            hl_motion(h, dest_y, dest_cb, dest_cr,
1693 2833fc46 Loren Merritt
                      s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
1694
                      s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
1695 4693b031 Måns Rullgård
                      h->h264dsp.weight_h264_pixels_tab, h->h264dsp.biweight_h264_pixels_tab);
1696 0da71265 Michael Niedermayer
        }
1697 e7e09b49 Loic Le Loarer
1698
1699
        if(!IS_INTRA4x4(mb_type)){
1700 bd91fee3 Alexander Strange
            if(is_h264){
1701 ef9d1d15 Loren Merritt
                if(IS_INTRA16x16(mb_type)){
1702 2fd1f0e0 Michael Niedermayer
                    if(transform_bypass){
1703
                        if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
1704 0a8ca22f Michael Niedermayer
                            h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);
1705
                        }else{
1706
                            for(i=0; i<16; i++){
1707 dc172ecc Oskar Arvidsson
                                if(h->non_zero_count_cache[ scan8[i] ] || dctcoef_get(h, h->mb, i*16))
1708
                                    s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + i*16*h->pixel_size, linesize);
1709 0a8ca22f Michael Niedermayer
                            }
1710 2fd1f0e0 Michael Niedermayer
                        }
1711
                    }else{
1712 4693b031 Måns Rullgård
                         h->h264dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
1713 41e4055b Michael Niedermayer
                    }
1714 49c084a7 Michael Niedermayer
                }else if(h->cbp&15){
1715 2fd1f0e0 Michael Niedermayer
                    if(transform_bypass){
1716 0a8ca22f Michael Niedermayer
                        const int di = IS_8x8DCT(mb_type) ? 4 : 1;
1717 1eb96035 Michael Niedermayer
                        idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
1718 0a8ca22f Michael Niedermayer
                        for(i=0; i<16; i+=di){
1719 62bc966f Michael Niedermayer
                            if(h->non_zero_count_cache[ scan8[i] ]){
1720 dc172ecc Oskar Arvidsson
                                idct_add(dest_y + block_offset[i], h->mb + i*16*h->pixel_size, linesize);
1721 0a8ca22f Michael Niedermayer
                            }
1722 ef9d1d15 Loren Merritt
                        }
1723 2fd1f0e0 Michael Niedermayer
                    }else{
1724
                        if(IS_8x8DCT(mb_type)){
1725 4693b031 Måns Rullgård
                            h->h264dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
1726 2fd1f0e0 Michael Niedermayer
                        }else{
1727 4693b031 Måns Rullgård
                            h->h264dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
1728 2fd1f0e0 Michael Niedermayer
                        }
1729
                    }
1730 4704097a Michael Niedermayer
                }
1731 007b1f04 Peter Ross
            }
1732
#if CONFIG_SVQ3_DECODER
1733
            else{
1734 e7e09b49 Loic Le Loarer
                for(i=0; i<16; i++){
1735
                    if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
1736 6867a90b Loic Le Loarer
                        uint8_t * const ptr= dest_y + block_offset[i];
1737 881b5b80 Rafaël Carré
                        ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
1738 e7e09b49 Loic Le Loarer
                    }
1739 4704097a Michael Niedermayer
                }
1740 0da71265 Michael Niedermayer
            }
1741 007b1f04 Peter Ross
#endif
1742 0da71265 Michael Niedermayer
        }
1743
1744 49fb20cb Aurelien Jacobs
        if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
1745 ef9d1d15 Loren Merritt
            uint8_t *dest[2] = {dest_cb, dest_cr};
1746
            if(transform_bypass){
1747 96465b90 Michael Niedermayer
                if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
1748 dc172ecc Oskar Arvidsson
                    h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + 16*16*h->pixel_size, uvlinesize);
1749
                    h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + 20*16*h->pixel_size, uvlinesize);
1750 96465b90 Michael Niedermayer
                }else{
1751 c25ac15a Michael Niedermayer
                    idct_add = s->dsp.add_pixels4;
1752 96465b90 Michael Niedermayer
                    for(i=16; i<16+8; i++){
1753 dc172ecc Oskar Arvidsson
                        if(h->non_zero_count_cache[ scan8[i] ] || dctcoef_get(h, h->mb, i*16))
1754
                            idct_add   (dest[(i&4)>>2] + block_offset[i], h->mb + i*16*h->pixel_size, uvlinesize);
1755 96465b90 Michael Niedermayer
                    }
1756
                }
1757 ef9d1d15 Loren Merritt
            }else{
1758 aebb5d6d Michael Niedermayer
                if(is_h264){
1759 2e186601 Jason Garrett-Glaser
                    if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+0] ])
1760 dc172ecc Oskar Arvidsson
                        h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16*h->pixel_size       , h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
1761 2e186601 Jason Garrett-Glaser
                    if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+1] ])
1762 dc172ecc Oskar Arvidsson
                        h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16*16+4*16)*h->pixel_size, h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
1763 1d16a1cf Ronald S. Bultje
                    h->h264dsp.h264_idct_add8(dest, block_offset,
1764
                                              h->mb, uvlinesize,
1765
                                              h->non_zero_count_cache);
1766 007b1f04 Peter Ross
                }
1767
#if CONFIG_SVQ3_DECODER
1768
                else{
1769 af0b2d67 Oskar Arvidsson
                    h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16     , h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
1770
                    h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+4*16, h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
1771 aebb5d6d Michael Niedermayer
                    for(i=16; i<16+8; i++){
1772
                        if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
1773
                            uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
1774 881b5b80 Rafaël Carré
                            ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[s->qscale + 12] - 12, 2);
1775 aebb5d6d Michael Niedermayer
                        }
1776 e7e09b49 Loic Le Loarer
                    }
1777 4704097a Michael Niedermayer
                }
1778 007b1f04 Peter Ross
#endif
1779 0da71265 Michael Niedermayer
            }
1780
        }
1781
    }
1782 c212fb0c Michael Niedermayer
    if(h->cbp || IS_INTRA(mb_type))
1783
        s->dsp.clear_blocks(h->mb);
1784 0da71265 Michael Niedermayer
}
1785
1786
/**
1787 bd91fee3 Alexander Strange
 * Process a macroblock; this case avoids checks for expensive uncommon cases.
1788
 */
1789
static void hl_decode_mb_simple(H264Context *h){
1790
    hl_decode_mb_internal(h, 1);
1791
}
1792
1793
/**
1794
 * Process a macroblock; this handles edge cases, such as interlacing.
1795
 */
1796
static void av_noinline hl_decode_mb_complex(H264Context *h){
1797
    hl_decode_mb_internal(h, 0);
1798
}
1799
1800 903d58f6 Michael Niedermayer
void ff_h264_hl_decode_mb(H264Context *h){
1801 bd91fee3 Alexander Strange
    MpegEncContext * const s = &h->s;
1802 64514ee8 Alexander Strange
    const int mb_xy= h->mb_xy;
1803 bd91fee3 Alexander Strange
    const int mb_type= s->current_picture.mb_type[mb_xy];
1804 49fb20cb Aurelien Jacobs
    int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
1805 bd91fee3 Alexander Strange
1806
    if (is_complex)
1807
        hl_decode_mb_complex(h);
1808
    else hl_decode_mb_simple(h);
1809
}
1810
1811 0da71265 Michael Niedermayer
static int pred_weight_table(H264Context *h){
1812
    MpegEncContext * const s = &h->s;
1813
    int list, i;
1814 9f2d1b4f Loren Merritt
    int luma_def, chroma_def;
1815 115329f1 Diego Biurrun
1816 9f2d1b4f Loren Merritt
    h->use_weight= 0;
1817
    h->use_weight_chroma= 0;
1818 0da71265 Michael Niedermayer
    h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
1819 09fffe9b Jason Garrett-Glaser
    if(CHROMA)
1820
        h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
1821 9f2d1b4f Loren Merritt
    luma_def = 1<<h->luma_log2_weight_denom;
1822
    chroma_def = 1<<h->chroma_log2_weight_denom;
1823 0da71265 Michael Niedermayer
1824
    for(list=0; list<2; list++){
1825 cb99c652 Gwenole Beauchesne
        h->luma_weight_flag[list]   = 0;
1826
        h->chroma_weight_flag[list] = 0;
1827 0da71265 Michael Niedermayer
        for(i=0; i<h->ref_count[list]; i++){
1828
            int luma_weight_flag, chroma_weight_flag;
1829 115329f1 Diego Biurrun
1830 0da71265 Michael Niedermayer
            luma_weight_flag= get_bits1(&s->gb);
1831
            if(luma_weight_flag){
1832 3d9137c8 Michael Niedermayer
                h->luma_weight[i][list][0]= get_se_golomb(&s->gb);
1833
                h->luma_weight[i][list][1]= get_se_golomb(&s->gb);
1834
                if(   h->luma_weight[i][list][0] != luma_def
1835
                   || h->luma_weight[i][list][1] != 0) {
1836 9f2d1b4f Loren Merritt
                    h->use_weight= 1;
1837 cb99c652 Gwenole Beauchesne
                    h->luma_weight_flag[list]= 1;
1838
                }
1839 9f2d1b4f Loren Merritt
            }else{
1840 3d9137c8 Michael Niedermayer
                h->luma_weight[i][list][0]= luma_def;
1841
                h->luma_weight[i][list][1]= 0;
1842 0da71265 Michael Niedermayer
            }
1843
1844 0af6967e Michael Niedermayer
            if(CHROMA){
1845 fef744d4 Michael Niedermayer
                chroma_weight_flag= get_bits1(&s->gb);
1846
                if(chroma_weight_flag){
1847
                    int j;
1848
                    for(j=0; j<2; j++){
1849 3d9137c8 Michael Niedermayer
                        h->chroma_weight[i][list][j][0]= get_se_golomb(&s->gb);
1850
                        h->chroma_weight[i][list][j][1]= get_se_golomb(&s->gb);
1851
                        if(   h->chroma_weight[i][list][j][0] != chroma_def
1852
                           || h->chroma_weight[i][list][j][1] != 0) {
1853 fef744d4 Michael Niedermayer
                            h->use_weight_chroma= 1;
1854 cb99c652 Gwenole Beauchesne
                            h->chroma_weight_flag[list]= 1;
1855
                        }
1856 fef744d4 Michael Niedermayer
                    }
1857
                }else{
1858
                    int j;
1859
                    for(j=0; j<2; j++){
1860 3d9137c8 Michael Niedermayer
                        h->chroma_weight[i][list][j][0]= chroma_def;
1861
                        h->chroma_weight[i][list][j][1]= 0;
1862 fef744d4 Michael Niedermayer
                    }
1863 0da71265 Michael Niedermayer
                }
1864
            }
1865
        }
1866 9f5c1037 Michael Niedermayer
        if(h->slice_type_nos != FF_B_TYPE) break;
1867 0da71265 Michael Niedermayer
    }
1868 9f2d1b4f Loren Merritt
    h->use_weight= h->use_weight || h->use_weight_chroma;
1869 0da71265 Michael Niedermayer
    return 0;
1870
}
1871
1872 1052b76f Michael Niedermayer
/**
1873
 * Initialize implicit_weight table.
1874 6da88bd3 Diego Biurrun
 * @param field  0/1 initialize the weight for interlaced MBAFF
1875 1052b76f Michael Niedermayer
 *                -1 initializes the rest
1876
 */
1877
static void implicit_weight_table(H264Context *h, int field){
1878 9f2d1b4f Loren Merritt
    MpegEncContext * const s = &h->s;
1879 1052b76f Michael Niedermayer
    int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
1880 9f2d1b4f Loren Merritt
1881 ce09f927 Gwenole Beauchesne
    for (i = 0; i < 2; i++) {
1882
        h->luma_weight_flag[i]   = 0;
1883
        h->chroma_weight_flag[i] = 0;
1884
    }
1885
1886 1052b76f Michael Niedermayer
    if(field < 0){
1887
        cur_poc = s->current_picture_ptr->poc;
1888
    if(   h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF
1889 9f2d1b4f Loren Merritt
       && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
1890
        h->use_weight= 0;
1891
        h->use_weight_chroma= 0;
1892
        return;
1893
    }
1894 1052b76f Michael Niedermayer
        ref_start= 0;
1895
        ref_count0= h->ref_count[0];
1896
        ref_count1= h->ref_count[1];
1897
    }else{
1898
        cur_poc = s->current_picture_ptr->field_poc[field];
1899
        ref_start= 16;
1900
        ref_count0= 16+2*h->ref_count[0];
1901
        ref_count1= 16+2*h->ref_count[1];
1902
    }
1903 9f2d1b4f Loren Merritt
1904
    h->use_weight= 2;
1905
    h->use_weight_chroma= 2;
1906
    h->luma_log2_weight_denom= 5;
1907
    h->chroma_log2_weight_denom= 5;
1908
1909 1052b76f Michael Niedermayer
    for(ref0=ref_start; ref0 < ref_count0; ref0++){
1910 9f2d1b4f Loren Merritt
        int poc0 = h->ref_list[0][ref0].poc;
1911 1052b76f Michael Niedermayer
        for(ref1=ref_start; ref1 < ref_count1; ref1++){
1912 738386a5 Loren Merritt
            int poc1 = h->ref_list[1][ref1].poc;
1913 f66e4f5f Reimar Döffinger
            int td = av_clip(poc1 - poc0, -128, 127);
1914 1052b76f Michael Niedermayer
            int w= 32;
1915 9f2d1b4f Loren Merritt
            if(td){
1916 f66e4f5f Reimar Döffinger
                int tb = av_clip(cur_poc - poc0, -128, 127);
1917 c26abfa5 Diego Biurrun
                int tx = (16384 + (FFABS(td) >> 1)) / td;
1918 72f86ec0 Michael Niedermayer
                int dist_scale_factor = (tb*tx + 32) >> 8;
1919
                if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
1920 1052b76f Michael Niedermayer
                    w = 64 - dist_scale_factor;
1921
            }
1922
            if(field<0){
1923
                h->implicit_weight[ref0][ref1][0]=
1924
                h->implicit_weight[ref0][ref1][1]= w;
1925
            }else{
1926
                h->implicit_weight[ref0][ref1][field]=w;
1927 72f86ec0 Michael Niedermayer
            }
1928 9f2d1b4f Loren Merritt
        }
1929
    }
1930
}
1931
1932 8fd57a66 Jeff Downs
/**
1933 5175b937 Loic Le Loarer
 * instantaneous decoder refresh.
1934 0da71265 Michael Niedermayer
 */
1935
static void idr(H264Context *h){
1936 ea6f00c4 Michael Niedermayer
    ff_h264_remove_all_refs(h);
1937 a149c1a5 Michael Niedermayer
    h->prev_frame_num= 0;
1938 80f8e035 Michael Niedermayer
    h->prev_frame_num_offset= 0;
1939
    h->prev_poc_msb=
1940
    h->prev_poc_lsb= 0;
1941 0da71265 Michael Niedermayer
}
1942
1943 7c33ad19 Loren Merritt
/* forget old pics after a seek */
1944
static void flush_dpb(AVCodecContext *avctx){
1945
    H264Context *h= avctx->priv_data;
1946
    int i;
1947 64b9d48f Alexander Strange
    for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
1948 285b570f Loren Merritt
        if(h->delayed_pic[i])
1949
            h->delayed_pic[i]->reference= 0;
1950 7c33ad19 Loren Merritt
        h->delayed_pic[i]= NULL;
1951 285b570f Loren Merritt
    }
1952 d375c104 Michael Niedermayer
    h->outputed_poc=h->next_outputed_poc= INT_MIN;
1953 b19d493f Haruhiko Yamagata
    h->prev_interlaced_frame = 1;
1954 7c33ad19 Loren Merritt
    idr(h);
1955 ca159196 Måns Rullgård
    if(h->s.current_picture_ptr)
1956
        h->s.current_picture_ptr->reference= 0;
1957 12d96de3 Jeff Downs
    h->s.first_field= 0;
1958 9c095463 Michael Niedermayer
    ff_h264_reset_sei(h);
1959 e240f898 Jeff Downs
    ff_mpeg_flush(avctx);
1960 7c33ad19 Loren Merritt
}
1961
1962 0da71265 Michael Niedermayer
static int init_poc(H264Context *h){
1963
    MpegEncContext * const s = &h->s;
1964
    const int max_frame_num= 1<<h->sps.log2_max_frame_num;
1965
    int field_poc[2];
1966 357282c6 Michael Niedermayer
    Picture *cur = s->current_picture_ptr;
1967 0da71265 Michael Niedermayer
1968 b78a6baa Michael Niedermayer
    h->frame_num_offset= h->prev_frame_num_offset;
1969 5710b371 Michael Niedermayer
    if(h->frame_num < h->prev_frame_num)
1970 b78a6baa Michael Niedermayer
        h->frame_num_offset += max_frame_num;
1971 0da71265 Michael Niedermayer
1972
    if(h->sps.poc_type==0){
1973
        const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
1974
1975
        if     (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
1976
            h->poc_msb = h->prev_poc_msb + max_poc_lsb;
1977
        else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
1978
            h->poc_msb = h->prev_poc_msb - max_poc_lsb;
1979
        else
1980
            h->poc_msb = h->prev_poc_msb;
1981
//printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
1982 115329f1 Diego Biurrun
        field_poc[0] =
1983 0da71265 Michael Niedermayer
        field_poc[1] = h->poc_msb + h->poc_lsb;
1984 115329f1 Diego Biurrun
        if(s->picture_structure == PICT_FRAME)
1985 0da71265 Michael Niedermayer
            field_poc[1] += h->delta_poc_bottom;
1986
    }else if(h->sps.poc_type==1){
1987
        int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
1988
        int i;
1989
1990
        if(h->sps.poc_cycle_length != 0)
1991
            abs_frame_num = h->frame_num_offset + h->frame_num;
1992
        else
1993
            abs_frame_num = 0;
1994
1995
        if(h->nal_ref_idc==0 && abs_frame_num > 0)
1996
            abs_frame_num--;
1997 115329f1 Diego Biurrun
1998 0da71265 Michael Niedermayer
        expected_delta_per_poc_cycle = 0;
1999
        for(i=0; i < h->sps.poc_cycle_length; i++)
2000
            expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
2001
2002
        if(abs_frame_num > 0){
2003
            int poc_cycle_cnt          = (abs_frame_num - 1) / h->sps.poc_cycle_length;
2004
            int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
2005
2006
            expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
2007
            for(i = 0; i <= frame_num_in_poc_cycle; i++)
2008
                expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
2009
        } else
2010
            expectedpoc = 0;
2011
2012 115329f1 Diego Biurrun
        if(h->nal_ref_idc == 0)
2013 0da71265 Michael Niedermayer
            expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
2014 115329f1 Diego Biurrun
2015 0da71265 Michael Niedermayer
        field_poc[0] = expectedpoc + h->delta_poc[0];
2016
        field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
2017
2018
        if(s->picture_structure == PICT_FRAME)
2019
            field_poc[1] += h->delta_poc[1];
2020
    }else{
2021 b78a6baa Michael Niedermayer
        int poc= 2*(h->frame_num_offset + h->frame_num);
2022 5710b371 Michael Niedermayer
2023 b78a6baa Michael Niedermayer
        if(!h->nal_ref_idc)
2024
            poc--;
2025 5710b371 Michael Niedermayer
2026 0da71265 Michael Niedermayer
        field_poc[0]= poc;
2027
        field_poc[1]= poc;
2028
    }
2029 115329f1 Diego Biurrun
2030 357282c6 Michael Niedermayer
    if(s->picture_structure != PICT_BOTTOM_FIELD)
2031 0da71265 Michael Niedermayer
        s->current_picture_ptr->field_poc[0]= field_poc[0];
2032 357282c6 Michael Niedermayer
    if(s->picture_structure != PICT_TOP_FIELD)
2033 0da71265 Michael Niedermayer
        s->current_picture_ptr->field_poc[1]= field_poc[1];
2034 357282c6 Michael Niedermayer
    cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
2035 0da71265 Michael Niedermayer
2036
    return 0;
2037
}
2038
2039 b41c1db3 Andreas Öman
2040
/**
2041
 * initialize scan tables
2042
 */
2043
static void init_scan_tables(H264Context *h){
2044
    int i;
2045 ca32f7f2 Jason Garrett-Glaser
    for(i=0; i<16; i++){
2046 b41c1db3 Andreas Öman
#define T(x) (x>>2) | ((x<<2) & 0xF)
2047 ca32f7f2 Jason Garrett-Glaser
        h->zigzag_scan[i] = T(zigzag_scan[i]);
2048
        h-> field_scan[i] = T( field_scan[i]);
2049 b41c1db3 Andreas Öman
#undef T
2050
    }
2051 ca32f7f2 Jason Garrett-Glaser
    for(i=0; i<64; i++){
2052 b41c1db3 Andreas Öman
#define T(x) (x>>3) | ((x&7)<<3)
2053 ca32f7f2 Jason Garrett-Glaser
        h->zigzag_scan8x8[i]       = T(ff_zigzag_direct[i]);
2054
        h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
2055
        h->field_scan8x8[i]        = T(field_scan8x8[i]);
2056
        h->field_scan8x8_cavlc[i]  = T(field_scan8x8_cavlc[i]);
2057 b41c1db3 Andreas Öman
#undef T
2058
    }
2059
    if(h->sps.transform_bypass){ //FIXME same ugly
2060
        h->zigzag_scan_q0          = zigzag_scan;
2061 45beb850 Stefan Gehrer
        h->zigzag_scan8x8_q0       = ff_zigzag_direct;
2062 b41c1db3 Andreas Öman
        h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
2063
        h->field_scan_q0           = field_scan;
2064
        h->field_scan8x8_q0        = field_scan8x8;
2065
        h->field_scan8x8_cavlc_q0  = field_scan8x8_cavlc;
2066
    }else{
2067
        h->zigzag_scan_q0          = h->zigzag_scan;
2068
        h->zigzag_scan8x8_q0       = h->zigzag_scan8x8;
2069
        h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
2070
        h->field_scan_q0           = h->field_scan;
2071
        h->field_scan8x8_q0        = h->field_scan8x8;
2072
        h->field_scan8x8_cavlc_q0  = h->field_scan8x8_cavlc;
2073
    }
2074
}
2075 afebe2f7 Andreas Öman
2076 d375c104 Michael Niedermayer
static void field_end(H264Context *h, int in_setup){
2077 256299d3 Michael Niedermayer
    MpegEncContext * const s = &h->s;
2078
    AVCodecContext * const avctx= s->avctx;
2079
    s->mb_y= 0;
2080
2081 d375c104 Michael Niedermayer
    if (!in_setup && !s->dropable)
2082
        ff_thread_report_progress((AVFrame*)s->current_picture_ptr, (16*s->mb_height >> FIELD_PICTURE) - 1,
2083
                                 s->picture_structure==PICT_BOTTOM_FIELD);
2084 256299d3 Michael Niedermayer
2085
    if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
2086
        ff_vdpau_h264_set_reference_frames(s);
2087
2088 d375c104 Michael Niedermayer
    if(in_setup || !(avctx->active_thread_type&FF_THREAD_FRAME)){
2089
        if(!s->dropable) {
2090
            ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
2091
            h->prev_poc_msb= h->poc_msb;
2092
            h->prev_poc_lsb= h->poc_lsb;
2093
        }
2094
        h->prev_frame_num_offset= h->frame_num_offset;
2095
        h->prev_frame_num= h->frame_num;
2096
        h->outputed_poc = h->next_outputed_poc;
2097 256299d3 Michael Niedermayer
    }
2098
2099
    if (avctx->hwaccel) {
2100
        if (avctx->hwaccel->end_frame(avctx) < 0)
2101
            av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
2102
    }
2103
2104
    if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
2105
        ff_vdpau_h264_picture_complete(s);
2106
2107
    /*
2108
     * FIXME: Error handling code does not seem to support interlaced
2109
     * when slices span multiple rows
2110
     * The ff_er_add_slice calls don't work right for bottom
2111
     * fields; they cause massive erroneous error concealing
2112
     * Error marking covers both fields (top and bottom).
2113
     * This causes a mismatched s->error_count
2114
     * and a bad error table. Further, the error count goes to
2115
     * INT_MAX when called for bottom field, because mb_y is
2116
     * past end by one (callers fault) and resync_mb_y != 0
2117
     * causes problems for the first MB line, too.
2118
     */
2119
    if (!FIELD_PICTURE)
2120
        ff_er_frame_end(s);
2121
2122
    MPV_frame_end(s);
2123 d225a1e2 Michael Niedermayer
2124
    h->current_slice=0;
2125 256299d3 Michael Niedermayer
}
2126
2127 afebe2f7 Andreas Öman
/**
2128 49bd8e4b Måns Rullgård
 * Replicate H264 "master" context to thread contexts.
2129 afebe2f7 Andreas Öman
 */
2130
static void clone_slice(H264Context *dst, H264Context *src)
2131
{
2132
    memcpy(dst->block_offset,     src->block_offset, sizeof(dst->block_offset));
2133
    dst->s.current_picture_ptr  = src->s.current_picture_ptr;
2134
    dst->s.current_picture      = src->s.current_picture;
2135
    dst->s.linesize             = src->s.linesize;
2136
    dst->s.uvlinesize           = src->s.uvlinesize;
2137 12d96de3 Jeff Downs
    dst->s.first_field          = src->s.first_field;
2138 afebe2f7 Andreas Öman
2139
    dst->prev_poc_msb           = src->prev_poc_msb;
2140
    dst->prev_poc_lsb           = src->prev_poc_lsb;
2141
    dst->prev_frame_num_offset  = src->prev_frame_num_offset;
2142
    dst->prev_frame_num         = src->prev_frame_num;
2143
    dst->short_ref_count        = src->short_ref_count;
2144
2145
    memcpy(dst->short_ref,        src->short_ref,        sizeof(dst->short_ref));
2146
    memcpy(dst->long_ref,         src->long_ref,         sizeof(dst->long_ref));
2147
    memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
2148
    memcpy(dst->ref_list,         src->ref_list,         sizeof(dst->ref_list));
2149 50c21814 Andreas Öman
2150
    memcpy(dst->dequant4_coeff,   src->dequant4_coeff,   sizeof(src->dequant4_coeff));
2151
    memcpy(dst->dequant8_coeff,   src->dequant8_coeff,   sizeof(src->dequant8_coeff));
2152 afebe2f7 Andreas Öman
}
2153
2154 0da71265 Michael Niedermayer
/**
2155 94e3e83f Janne Grunau
 * computes profile from profile_idc and constraint_set?_flags
2156
 *
2157
 * @param sps SPS
2158
 *
2159
 * @return profile as defined by FF_PROFILE_H264_*
2160
 */
2161
int ff_h264_get_profile(SPS *sps)
2162
{
2163
    int profile = sps->profile_idc;
2164
2165
    switch(sps->profile_idc) {
2166
    case FF_PROFILE_H264_BASELINE:
2167
        // constraint_set1_flag set to 1
2168
        profile |= (sps->constraint_set_flags & 1<<1) ? FF_PROFILE_H264_CONSTRAINED : 0;
2169
        break;
2170
    case FF_PROFILE_H264_HIGH_10:
2171
    case FF_PROFILE_H264_HIGH_422:
2172
    case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
2173
        // constraint_set3_flag set to 1
2174
        profile |= (sps->constraint_set_flags & 1<<3) ? FF_PROFILE_H264_INTRA : 0;
2175
        break;
2176
    }
2177
2178
    return profile;
2179
}
2180
2181
/**
2182 0da71265 Michael Niedermayer
 * decodes a slice header.
2183 9c852bcf Diego Biurrun
 * This will also call MPV_common_init() and frame_start() as needed.
2184 afebe2f7 Andreas Öman
 *
2185
 * @param h h264context
2186
 * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
2187
 *
2188 d9526386 Diego Biurrun
 * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
2189 0da71265 Michael Niedermayer
 */
2190 afebe2f7 Andreas Öman
static int decode_slice_header(H264Context *h, H264Context *h0){
2191 0da71265 Michael Niedermayer
    MpegEncContext * const s = &h->s;
2192 12d96de3 Jeff Downs
    MpegEncContext * const s0 = &h0->s;
2193 88e7a4d1 Michael Niedermayer
    unsigned int first_mb_in_slice;
2194 ac658be5 Francois Oligny-Lemieux
    unsigned int pps_id;
2195 0da71265 Michael Niedermayer
    int num_ref_idx_active_override_flag;
2196 41f5c62f Michael Niedermayer
    unsigned int slice_type, tmp, i, j;
2197 0bf79634 Loic Le Loarer
    int default_ref_list_done = 0;
2198 12d96de3 Jeff Downs
    int last_pic_structure;
2199 0da71265 Michael Niedermayer
2200 2f944356 Loren Merritt
    s->dropable= h->nal_ref_idc == 0;
2201 0da71265 Michael Niedermayer
2202 cf653d08 Jeff Downs
    if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
2203
        s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
2204
        s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
2205
    }else{
2206
        s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
2207
        s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
2208
    }
2209
2210 0da71265 Michael Niedermayer
    first_mb_in_slice= get_ue_golomb(&s->gb);
2211
2212 d225a1e2 Michael Niedermayer
    if(first_mb_in_slice == 0){ //FIXME better field boundary detection
2213
        if(h0->current_slice && FIELD_PICTURE){
2214 d375c104 Michael Niedermayer
            field_end(h, 1);
2215 d225a1e2 Michael Niedermayer
        }
2216
2217 afebe2f7 Andreas Öman
        h0->current_slice = 0;
2218 12d96de3 Jeff Downs
        if (!s0->first_field)
2219 f6e3c460 Andreas Öman
            s->current_picture_ptr= NULL;
2220 66a4b2c1 Michael Niedermayer
    }
2221
2222 9963b332 Michael Niedermayer
    slice_type= get_ue_golomb_31(&s->gb);
2223 0bf79634 Loic Le Loarer
    if(slice_type > 9){
2224 9b879566 Michel Bardiaux
        av_log(h->s.avctx, AV_LOG_ERROR, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y);
2225 5175b937 Loic Le Loarer
        return -1;
2226 0da71265 Michael Niedermayer
    }
2227 0bf79634 Loic Le Loarer
    if(slice_type > 4){
2228
        slice_type -= 5;
2229 0da71265 Michael Niedermayer
        h->slice_type_fixed=1;
2230
    }else
2231
        h->slice_type_fixed=0;
2232 115329f1 Diego Biurrun
2233 ee2a957f Diego Biurrun
    slice_type= golomb_to_pict_type[ slice_type ];
2234 9701840b Aurelien Jacobs
    if (slice_type == FF_I_TYPE
2235 afebe2f7 Andreas Öman
        || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
2236 0bf79634 Loic Le Loarer
        default_ref_list_done = 1;
2237
    }
2238
    h->slice_type= slice_type;
2239 e3e6f18f Michael Niedermayer
    h->slice_type_nos= slice_type & 3;
2240 0bf79634 Loic Le Loarer
2241 1412060e Diego Biurrun
    s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
2242 115329f1 Diego Biurrun
2243 0da71265 Michael Niedermayer
    pps_id= get_ue_golomb(&s->gb);
2244 ac658be5 Francois Oligny-Lemieux
    if(pps_id>=MAX_PPS_COUNT){
2245 9b879566 Michel Bardiaux
        av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
2246 0da71265 Michael Niedermayer
        return -1;
2247
    }
2248 afebe2f7 Andreas Öman
    if(!h0->pps_buffers[pps_id]) {
2249 a0f80050 Panagiotis Issaris
        av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
2250 8b92b792 Michael Niedermayer
        return -1;
2251
    }
2252 afebe2f7 Andreas Öman
    h->pps= *h0->pps_buffers[pps_id];
2253 8b92b792 Michael Niedermayer
2254 afebe2f7 Andreas Öman
    if(!h0->sps_buffers[h->pps.sps_id]) {
2255 a0f80050 Panagiotis Issaris
        av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
2256 8b92b792 Michael Niedermayer
        return -1;
2257
    }
2258 afebe2f7 Andreas Öman
    h->sps = *h0->sps_buffers[h->pps.sps_id];
2259 239ea04c Loren Merritt
2260 94e3e83f Janne Grunau
    s->avctx->profile = ff_h264_get_profile(&h->sps);
2261 fa37cf0d Gwenole Beauchesne
    s->avctx->level   = h->sps.level_idc;
2262 6752a3cc Gwenole Beauchesne
    s->avctx->refs    = h->sps.ref_frame_count;
2263 b08e38e8 Gwenole Beauchesne
2264 50c21814 Andreas Öman
    if(h == h0 && h->dequant_coeff_pps != pps_id){
2265 50eaa857 Guillaume Poirier
        h->dequant_coeff_pps = pps_id;
2266 239ea04c Loren Merritt
        init_dequant_tables(h);
2267
    }
2268 115329f1 Diego Biurrun
2269 0da71265 Michael Niedermayer
    s->mb_width= h->sps.mb_width;
2270 6867a90b Loic Le Loarer
    s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
2271 115329f1 Diego Biurrun
2272 bf4665ee David S. Miller
    h->b_stride=  s->mb_width*4;
2273 0da71265 Michael Niedermayer
2274 faf3dfb9 Michael Niedermayer
    s->width = 16*s->mb_width - 2*FFMIN(h->sps.crop_right, 7);
2275 0da71265 Michael Niedermayer
    if(h->sps.frame_mbs_only_flag)
2276 faf3dfb9 Michael Niedermayer
        s->height= 16*s->mb_height - 2*FFMIN(h->sps.crop_bottom, 7);
2277 0da71265 Michael Niedermayer
    else
2278 00d1e96b Reimar Döffinger
        s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 7);
2279 115329f1 Diego Biurrun
2280
    if (s->context_initialized
2281 5388f0b4 Janusz Krzysztofik
        && (   s->width != s->avctx->width || s->height != s->avctx->height
2282
            || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) {
2283 d375c104 Michael Niedermayer
        if(h != h0) {
2284
            av_log_missing_feature(s->avctx, "Width/height changing with threads is", 0);
2285 afebe2f7 Andreas Öman
            return -1;   // width / height changed during parallelized decoding
2286 d375c104 Michael Niedermayer
        }
2287 2ed0f766 Ronald S. Bultje
        free_tables(h, 0);
2288 ff7f75e1 Michael Niedermayer
        flush_dpb(s->avctx);
2289 0da71265 Michael Niedermayer
        MPV_common_end(s);
2290
    }
2291
    if (!s->context_initialized) {
2292 0d5e6843 Michael Niedermayer
        if(h != h0){
2293
            av_log(h->s.avctx, AV_LOG_ERROR, "we cant (re-)initialize context during parallel decoding\n");
2294
            return -1;
2295
        }
2296 f3bdc3da Reimar Döffinger
2297
        avcodec_set_dimensions(s->avctx, s->width, s->height);
2298
        s->avctx->sample_aspect_ratio= h->sps.sar;
2299 cfa5a81e Michael Niedermayer
        av_assert0(s->avctx->sample_aspect_ratio.den);
2300 f3bdc3da Reimar Döffinger
2301 c4dffe7e David Conrad
        if(h->sps.video_signal_type_present_flag){
2302
            s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
2303
            if(h->sps.colour_description_present_flag){
2304
                s->avctx->color_primaries = h->sps.color_primaries;
2305
                s->avctx->color_trc       = h->sps.color_trc;
2306
                s->avctx->colorspace      = h->sps.colorspace;
2307
            }
2308
        }
2309
2310 f3bdc3da Reimar Döffinger
        if(h->sps.timing_info_present_flag){
2311 3102d180 Michael Niedermayer
            int64_t den= h->sps.time_scale;
2312 055a6aa7 Michael Niedermayer
            if(h->x264_build < 44U)
2313 3102d180 Michael Niedermayer
                den *= 2;
2314 f3bdc3da Reimar Döffinger
            av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
2315 3102d180 Michael Niedermayer
                      h->sps.num_units_in_tick, den, 1<<30);
2316 f3bdc3da Reimar Döffinger
        }
2317 0435fb16 Baptiste Coudurier
        s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
2318 9ad7dfc1 Baptiste Coudurier
                                                 s->avctx->codec->pix_fmts ?
2319
                                                 s->avctx->codec->pix_fmts :
2320 0435fb16 Baptiste Coudurier
                                                 s->avctx->color_range == AVCOL_RANGE_JPEG ?
2321
                                                 hwaccel_pixfmt_list_h264_jpeg_420 :
2322
                                                 ff_hwaccel_pixfmt_list_420);
2323 f3bdc3da Reimar Döffinger
        s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
2324
2325 0d5e6843 Michael Niedermayer
        if (MPV_common_init(s) < 0){
2326
            av_log(h->s.avctx, AV_LOG_ERROR, "MPV_common_init() failed\n");
2327 0da71265 Michael Niedermayer
            return -1;
2328 0d5e6843 Michael Niedermayer
        }
2329 12d96de3 Jeff Downs
        s->first_field = 0;
2330 b19d493f Haruhiko Yamagata
        h->prev_interlaced_frame = 1;
2331 115329f1 Diego Biurrun
2332 b41c1db3 Andreas Öman
        init_scan_tables(h);
2333 903d58f6 Michael Niedermayer
        ff_h264_alloc_tables(h);
2334 0da71265 Michael Niedermayer
2335 d375c104 Michael Niedermayer
        if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_SLICE)) {
2336 0d5e6843 Michael Niedermayer
            if (context_init(h) < 0){
2337
                av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed\n");
2338 afebe2f7 Andreas Öman
                return -1;
2339 0d5e6843 Michael Niedermayer
            }
2340 d375c104 Michael Niedermayer
        } else {
2341
            for(i = 1; i < s->avctx->thread_count; i++) {
2342
                H264Context *c;
2343
                c = h->thread_context[i] = av_malloc(sizeof(H264Context));
2344
                memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
2345
                memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
2346
                c->h264dsp = h->h264dsp;
2347
                c->sps = h->sps;
2348
                c->pps = h->pps;
2349
                init_scan_tables(c);
2350
                clone_tables(c, h, i);
2351
            }
2352
2353
            for(i = 0; i < s->avctx->thread_count; i++)
2354 0d5e6843 Michael Niedermayer
                if(context_init(h->thread_context[i]) < 0){
2355
                    av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed\n");
2356 d375c104 Michael Niedermayer
                    return -1;
2357 0d5e6843 Michael Niedermayer
                }
2358 d375c104 Michael Niedermayer
        }
2359 0da71265 Michael Niedermayer
    }
2360
2361
    h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
2362
2363 5d18eaad Loren Merritt
    h->mb_mbaff = 0;
2364 6ba71fc4 Loïc Le Loarer
    h->mb_aff_frame = 0;
2365 12d96de3 Jeff Downs
    last_pic_structure = s0->picture_structure;
2366 0da71265 Michael Niedermayer
    if(h->sps.frame_mbs_only_flag){
2367
        s->picture_structure= PICT_FRAME;
2368
    }else{
2369 6ba71fc4 Loïc Le Loarer
        if(get_bits1(&s->gb)) { //field_pic_flag
2370 0da71265 Michael Niedermayer
            s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
2371 6ba71fc4 Loïc Le Loarer
        } else {
2372 0da71265 Michael Niedermayer
            s->picture_structure= PICT_FRAME;
2373 6ba71fc4 Loïc Le Loarer
            h->mb_aff_frame = h->sps.mb_aff;
2374 6867a90b Loic Le Loarer
        }
2375 0da71265 Michael Niedermayer
    }
2376 44e9dcf1 Michael Niedermayer
    h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
2377 2ddcf84b Jeff Downs
2378
    if(h0->current_slice == 0){
2379 d375c104 Michael Niedermayer
        if(h->frame_num != h->prev_frame_num &&
2380
          (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num) < (h->frame_num - h->sps.ref_frame_count))
2381
            h->prev_frame_num = h->frame_num - h->sps.ref_frame_count - 1;
2382
2383 26b86e47 Michael Niedermayer
        while(h->frame_num !=  h->prev_frame_num &&
2384
              h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
2385 4dece8c7 Jason Garrett-Glaser
            Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
2386 bb943bb8 Baptiste Coudurier
            av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
2387 903d58f6 Michael Niedermayer
            if (ff_h264_frame_start(h) < 0)
2388 66e6038c Reimar Döffinger
                return -1;
2389 26b86e47 Michael Niedermayer
            h->prev_frame_num++;
2390
            h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
2391
            s->current_picture_ptr->frame_num= h->prev_frame_num;
2392 d375c104 Michael Niedermayer
            ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 0);
2393
            ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 1);
2394 3d542120 Michael Niedermayer
            ff_generate_sliding_window_mmcos(h);
2395
            ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
2396 e2983d6e Jason Garrett-Glaser
            /* Error concealment: if a ref is missing, copy the previous ref in its place.
2397
             * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
2398
             * about there being no actual duplicates.
2399
             * FIXME: this doesn't copy padding for out-of-frame motion vectors.  Given we're
2400
             * concealing a lost frame, this probably isn't noticable by comparison, but it should
2401
             * be fixed. */
2402 4dece8c7 Jason Garrett-Glaser
            if (h->short_ref_count) {
2403
                if (prev) {
2404
                    av_image_copy(h->short_ref[0]->data, h->short_ref[0]->linesize,
2405
                                  (const uint8_t**)prev->data, prev->linesize,
2406 5dd7f994 Carl Eugen Hoyos
                                  s->avctx->pix_fmt, s->mb_width*16, s->mb_height*16);
2407 4dece8c7 Jason Garrett-Glaser
                    h->short_ref[0]->poc = prev->poc+2;
2408
                }
2409
                h->short_ref[0]->frame_num = h->prev_frame_num;
2410
            }
2411 26b86e47 Michael Niedermayer
        }
2412
2413 12d96de3 Jeff Downs
        /* See if we have a decoded first field looking for a pair... */
2414
        if (s0->first_field) {
2415
            assert(s0->current_picture_ptr);
2416
            assert(s0->current_picture_ptr->data[0]);
2417
            assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
2418
2419
            /* figure out if we have a complementary field pair */
2420
            if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
2421
                /*
2422
                 * Previous field is unmatched. Don't display it, but let it
2423
                 * remain for reference if marked as such.
2424
                 */
2425
                s0->current_picture_ptr = NULL;
2426
                s0->first_field = FIELD_PICTURE;
2427
2428
            } else {
2429
                if (h->nal_ref_idc &&
2430
                        s0->current_picture_ptr->reference &&
2431
                        s0->current_picture_ptr->frame_num != h->frame_num) {
2432
                    /*
2433
                     * This and previous field were reference, but had
2434
                     * different frame_nums. Consider this field first in
2435
                     * pair. Throw away previous field except for reference
2436
                     * purposes.
2437
                     */
2438
                    s0->first_field = 1;
2439
                    s0->current_picture_ptr = NULL;
2440
2441
                } else {
2442
                    /* Second field in complementary pair */
2443
                    s0->first_field = 0;
2444
                }
2445
            }
2446
2447
        } else {
2448
            /* Frame or first field in a potentially complementary pair */
2449
            assert(!s0->current_picture_ptr);
2450
            s0->first_field = FIELD_PICTURE;
2451
        }
2452
2453 903d58f6 Michael Niedermayer
        if((!FIELD_PICTURE || s0->first_field) && ff_h264_frame_start(h) < 0) {
2454 12d96de3 Jeff Downs
            s0->first_field = 0;
2455 2ddcf84b Jeff Downs
            return -1;
2456 12d96de3 Jeff Downs
        }
2457 2ddcf84b Jeff Downs
    }
2458
    if(h != h0)
2459
        clone_slice(h, h0);
2460
2461
    s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
2462
2463 88e7a4d1 Michael Niedermayer
    assert(s->mb_num == s->mb_width * s->mb_height);
2464 f3e53d9f Jeff Downs
    if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
2465 88e7a4d1 Michael Niedermayer
       first_mb_in_slice                    >= s->mb_num){
2466
        av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
2467 6b53b87e Michael Niedermayer
        return -1;
2468
    }
2469 88e7a4d1 Michael Niedermayer
    s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
2470 f3e53d9f Jeff Downs
    s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
2471
    if (s->picture_structure == PICT_BOTTOM_FIELD)
2472
        s->resync_mb_y = s->mb_y = s->mb_y + 1;
2473 88e7a4d1 Michael Niedermayer
    assert(s->mb_y < s->mb_height);
2474 115329f1 Diego Biurrun
2475 0da71265 Michael Niedermayer
    if(s->picture_structure==PICT_FRAME){
2476
        h->curr_pic_num=   h->frame_num;
2477
        h->max_pic_num= 1<< h->sps.log2_max_frame_num;
2478
    }else{
2479 f57e2af6 Jeff Downs
        h->curr_pic_num= 2*h->frame_num + 1;
2480 0da71265 Michael Niedermayer
        h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
2481
    }
2482 115329f1 Diego Biurrun
2483 0da71265 Michael Niedermayer
    if(h->nal_unit_type == NAL_IDR_SLICE){
2484 1df1df0b Fabrice Bellard
        get_ue_golomb(&s->gb); /* idr_pic_id */
2485 0da71265 Michael Niedermayer
    }
2486 115329f1 Diego Biurrun
2487 0da71265 Michael Niedermayer
    if(h->sps.poc_type==0){
2488
        h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
2489 115329f1 Diego Biurrun
2490 0da71265 Michael Niedermayer
        if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
2491
            h->delta_poc_bottom= get_se_golomb(&s->gb);
2492
        }
2493
    }
2494 115329f1 Diego Biurrun
2495 0da71265 Michael Niedermayer
    if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
2496
        h->delta_poc[0]= get_se_golomb(&s->gb);
2497 115329f1 Diego Biurrun
2498 0da71265 Michael Niedermayer
        if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
2499
            h->delta_poc[1]= get_se_golomb(&s->gb);
2500
    }
2501 115329f1 Diego Biurrun
2502 0da71265 Michael Niedermayer
    init_poc(h);
2503 115329f1 Diego Biurrun
2504 0da71265 Michael Niedermayer
    if(h->pps.redundant_pic_cnt_present){
2505
        h->redundant_pic_count= get_ue_golomb(&s->gb);
2506
    }
2507
2508 1412060e Diego Biurrun
    //set defaults, might be overridden a few lines later
2509 0da71265 Michael Niedermayer
    h->ref_count[0]= h->pps.ref_count[0];
2510
    h->ref_count[1]= h->pps.ref_count[1];
2511
2512 e3e6f18f Michael Niedermayer
    if(h->slice_type_nos != FF_I_TYPE){
2513 9f5c1037 Michael Niedermayer
        if(h->slice_type_nos == FF_B_TYPE){
2514 0da71265 Michael Niedermayer
            h->direct_spatial_mv_pred= get_bits1(&s->gb);
2515
        }
2516
        num_ref_idx_active_override_flag= get_bits1(&s->gb);
2517 115329f1 Diego Biurrun
2518 0da71265 Michael Niedermayer
        if(num_ref_idx_active_override_flag){
2519
            h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
2520 9f5c1037 Michael Niedermayer
            if(h->slice_type_nos==FF_B_TYPE)
2521 0da71265 Michael Niedermayer
                h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
2522
2523 187696fa Michael Niedermayer
            if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
2524 9b879566 Michel Bardiaux
                av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
2525 88e7a4d1 Michael Niedermayer
                h->ref_count[0]= h->ref_count[1]= 1;
2526 0da71265 Michael Niedermayer
                return -1;
2527
            }
2528
        }
2529 9f5c1037 Michael Niedermayer
        if(h->slice_type_nos == FF_B_TYPE)
2530 187696fa Michael Niedermayer
            h->list_count= 2;
2531
        else
2532
            h->list_count= 1;
2533
    }else
2534
        h->list_count= 0;
2535 0da71265 Michael Niedermayer
2536 0bf79634 Loic Le Loarer
    if(!default_ref_list_done){
2537 ea6f00c4 Michael Niedermayer
        ff_h264_fill_default_ref_list(h);
2538 0da71265 Michael Niedermayer
    }
2539
2540 ea6f00c4 Michael Niedermayer
    if(h->slice_type_nos!=FF_I_TYPE && ff_h264_decode_ref_pic_list_reordering(h) < 0)
2541 806bb93f Michael Niedermayer
        return -1;
2542 0da71265 Michael Niedermayer
2543 07dff5c7 Michael Niedermayer
    if(h->slice_type_nos!=FF_I_TYPE){
2544
        s->last_picture_ptr= &h->ref_list[0][0];
2545 8d2fc163 Carl Eugen Hoyos
        ff_copy_picture(&s->last_picture, s->last_picture_ptr);
2546 07dff5c7 Michael Niedermayer
    }
2547
    if(h->slice_type_nos==FF_B_TYPE){
2548
        s->next_picture_ptr= &h->ref_list[1][0];
2549 8d2fc163 Carl Eugen Hoyos
        ff_copy_picture(&s->next_picture, s->next_picture_ptr);
2550 07dff5c7 Michael Niedermayer
    }
2551
2552 932f396f Michael Niedermayer
    if(   (h->pps.weighted_pred          && h->slice_type_nos == FF_P_TYPE )
2553 9f5c1037 Michael Niedermayer
       ||  (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) )
2554 0da71265 Michael Niedermayer
        pred_weight_table(h);
2555 1a29c6a0 Michael Niedermayer
    else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE){
2556 1052b76f Michael Niedermayer
        implicit_weight_table(h, -1);
2557 1a29c6a0 Michael Niedermayer
    }else {
2558 9f2d1b4f Loren Merritt
        h->use_weight = 0;
2559 cb99c652 Gwenole Beauchesne
        for (i = 0; i < 2; i++) {
2560
            h->luma_weight_flag[i]   = 0;
2561
            h->chroma_weight_flag[i] = 0;
2562
        }
2563
    }
2564 115329f1 Diego Biurrun
2565 2ddcf84b Jeff Downs
    if(h->nal_ref_idc)
2566 ea6f00c4 Michael Niedermayer
        ff_h264_decode_ref_pic_marking(h0, &s->gb);
2567 0da71265 Michael Niedermayer
2568 1052b76f Michael Niedermayer
    if(FRAME_MBAFF){
2569 ea6f00c4 Michael Niedermayer
        ff_h264_fill_mbaff_ref_list(h);
2570 5d18eaad Loren Merritt
2571 1052b76f Michael Niedermayer
        if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE){
2572
            implicit_weight_table(h, 0);
2573
            implicit_weight_table(h, 1);
2574
        }
2575
    }
2576
2577 8f56e219 Michael Niedermayer
    if(h->slice_type_nos==FF_B_TYPE && !h->direct_spatial_mv_pred)
2578 943f69a6 Michael Niedermayer
        ff_h264_direct_dist_scale_factor(h);
2579
    ff_h264_direct_ref_list_init(h);
2580 8f56e219 Michael Niedermayer
2581 e3e6f18f Michael Niedermayer
    if( h->slice_type_nos != FF_I_TYPE && h->pps.cabac ){
2582 9963b332 Michael Niedermayer
        tmp = get_ue_golomb_31(&s->gb);
2583 88e7a4d1 Michael Niedermayer
        if(tmp > 2){
2584
            av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
2585
            return -1;
2586
        }
2587
        h->cabac_init_idc= tmp;
2588
    }
2589 e5017ab8 Laurent Aimar
2590
    h->last_qscale_diff = 0;
2591 88e7a4d1 Michael Niedermayer
    tmp = h->pps.init_qp + get_se_golomb(&s->gb);
2592
    if(tmp>51){
2593
        av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
2594 3ebc7e04 Michael Niedermayer
        return -1;
2595
    }
2596 88e7a4d1 Michael Niedermayer
    s->qscale= tmp;
2597 4691a77d Andreas Öman
    h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
2598
    h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
2599 0da71265 Michael Niedermayer
    //FIXME qscale / qp ... stuff
2600 9701840b Aurelien Jacobs
    if(h->slice_type == FF_SP_TYPE){
2601 1df1df0b Fabrice Bellard
        get_bits1(&s->gb); /* sp_for_switch_flag */
2602 0da71265 Michael Niedermayer
    }
2603 9701840b Aurelien Jacobs
    if(h->slice_type==FF_SP_TYPE || h->slice_type == FF_SI_TYPE){
2604 1df1df0b Fabrice Bellard
        get_se_golomb(&s->gb); /* slice_qs_delta */
2605 0da71265 Michael Niedermayer
    }
2606
2607 53c05b1e Michael Niedermayer
    h->deblocking_filter = 1;
2608 0c32e19d Michael Niedermayer
    h->slice_alpha_c0_offset = 52;
2609
    h->slice_beta_offset = 52;
2610 0da71265 Michael Niedermayer
    if( h->pps.deblocking_filter_parameters_present ) {
2611 9963b332 Michael Niedermayer
        tmp= get_ue_golomb_31(&s->gb);
2612 88e7a4d1 Michael Niedermayer
        if(tmp > 2){
2613
            av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
2614
            return -1;
2615
        }
2616
        h->deblocking_filter= tmp;
2617 115329f1 Diego Biurrun
        if(h->deblocking_filter < 2)
2618 53c05b1e Michael Niedermayer
            h->deblocking_filter^= 1; // 1<->0
2619
2620
        if( h->deblocking_filter ) {
2621 0c32e19d Michael Niedermayer
            h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
2622
            h->slice_beta_offset     += get_se_golomb(&s->gb) << 1;
2623
            if(   h->slice_alpha_c0_offset > 104U
2624
               || h->slice_beta_offset     > 104U){
2625
                av_log(s->avctx, AV_LOG_ERROR, "deblocking filter parameters %d %d out of range\n", h->slice_alpha_c0_offset, h->slice_beta_offset);
2626
                return -1;
2627
            }
2628 0da71265 Michael Niedermayer
        }
2629 980a82b7 Michael Niedermayer
    }
2630 afebe2f7 Andreas Öman
2631 61858a76 Reimar Döffinger
    if(   s->avctx->skip_loop_filter >= AVDISCARD_ALL
2632 4b30289e Michael Niedermayer
       ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != FF_I_TYPE)
2633 9f5c1037 Michael Niedermayer
       ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR  && h->slice_type_nos == FF_B_TYPE)
2634 61858a76 Reimar Döffinger
       ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
2635
        h->deblocking_filter= 0;
2636
2637 afebe2f7 Andreas Öman
    if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
2638 ec970c21 Andreas Öman
        if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
2639
            /* Cheat slightly for speed:
2640 5d81d641 Diego Biurrun
               Do not bother to deblock across slices. */
2641 ec970c21 Andreas Öman
            h->deblocking_filter = 2;
2642
        } else {
2643 7ae94d52 Andreas Öman
            h0->max_contexts = 1;
2644
            if(!h0->single_decode_warning) {
2645
                av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
2646
                h0->single_decode_warning = 1;
2647
            }
2648 0d5e6843 Michael Niedermayer
            if(h != h0){
2649
                av_log(h->s.avctx, AV_LOG_ERROR, "deblocking switched inside frame\n");
2650
                return 1;
2651
            }
2652 ec970c21 Andreas Öman
        }
2653 afebe2f7 Andreas Öman
    }
2654 0c32e19d Michael Niedermayer
    h->qp_thresh= 15 + 52 - FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1]);
2655 afebe2f7 Andreas Öman
2656 0da71265 Michael Niedermayer
#if 0 //FMO
2657
    if( h->pps.num_slice_groups > 1  && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
2658
        slice_group_change_cycle= get_bits(&s->gb, ?);
2659