h264.c 114 KB
Newer Older
Michael Niedermayer's avatar
Michael Niedermayer committed
1 2 3 4
/*
 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
Michael Niedermayer's avatar
Michael Niedermayer committed
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Michael Niedermayer's avatar
Michael Niedermayer committed
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Michael Niedermayer's avatar
Michael Niedermayer committed
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Michael Niedermayer's avatar
Michael Niedermayer committed
20
 */
21

Michael Niedermayer's avatar
Michael Niedermayer committed
22
/**
23
 * @file libavcodec/h264.c
Michael Niedermayer's avatar
Michael Niedermayer committed
24 25 26 27
 * H.264 / AVC / MPEG4 part10 codec.
 * @author Michael Niedermayer <michaelni@gmx.at>
 */

28
#include "internal.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
29 30 31
#include "dsputil.h"
#include "avcodec.h"
#include "mpegvideo.h"
32
#include "h264.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
33
#include "h264data.h"
34
#include "h264_mvpred.h"
35
#include "h264_parser.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
36
#include "golomb.h"
37
#include "mathops.h"
38
#include "rectangle.h"
39
#include "vdpau_internal.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
40

41 42
#include "cabac.h"

43
//#undef NDEBUG
Michael Niedermayer's avatar
Michael Niedermayer committed
44 45
#include <assert.h>

46
static const uint8_t rem6[52]={
47 48 49
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
static const uint8_t div6[52]={
51 52 53
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
void ff_h264_write_back_intra_pred_mode(H264Context *h){
55
    const int mb_xy= h->mb_xy;
Michael Niedermayer's avatar
Michael Niedermayer committed
56 57 58 59 60 61 62 63 64 65

    h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
    h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
    h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
    h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
    h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
    h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
    h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/**
 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
 */
int ff_h264_check_intra4x4_pred_mode(H264Context *h){
    MpegEncContext * const s = &h->s;
    static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
    static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
    int i;

    if(!(h->top_samples_available&0x8000)){
        for(i=0; i<4; i++){
            int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
            if(status<0){
                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);
                return -1;
            } else if(status){
                h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
            }
        }
    }

    if((h->left_samples_available&0x8888)!=0x8888){
        static const int mask[4]={0x8000,0x2000,0x80,0x20};
        for(i=0; i<4; i++){
            if(!(h->left_samples_available&mask[i])){
                int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
                if(status<0){
                    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);
                    return -1;
                } else if(status){
                    h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
                }
            }
        }
    }

    return 0;
} //FIXME cleanup like ff_h264_check_intra_pred_mode

Michael Niedermayer's avatar
Michael Niedermayer committed
105 106 107
/**
 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
 */
108
int ff_h264_check_intra_pred_mode(H264Context *h, int mode){
Michael Niedermayer's avatar
Michael Niedermayer committed
109 110 111
    MpegEncContext * const s = &h->s;
    static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
    static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
112

Michael Niedermayer's avatar
Michael Niedermayer committed
113
    if(mode > 6U) {
Loic Le Loarer's avatar
Loic Le Loarer committed
114
        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);
Michael Niedermayer's avatar
Michael Niedermayer committed
115
        return -1;
Loic Le Loarer's avatar
Loic Le Loarer committed
116
    }
117

Michael Niedermayer's avatar
Michael Niedermayer committed
118 119 120
    if(!(h->top_samples_available&0x8000)){
        mode= top[ mode ];
        if(mode<0){
121
            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);
Michael Niedermayer's avatar
Michael Niedermayer committed
122 123 124
            return -1;
        }
    }
125

126
    if((h->left_samples_available&0x8080) != 0x8080){
Michael Niedermayer's avatar
Michael Niedermayer committed
127
        mode= left[ mode ];
128 129 130
        if(h->left_samples_available&0x8080){ //mad cow disease mode, aka MBAFF + constrained_intra_pred
            mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
131
        if(mode<0){
132
            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);
Michael Niedermayer's avatar
Michael Niedermayer committed
133
            return -1;
134
        }
Michael Niedermayer's avatar
Michael Niedermayer committed
135 136 137 138 139
    }

    return mode;
}

140
const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
Michael Niedermayer's avatar
Michael Niedermayer committed
141 142
    int i, si, di;
    uint8_t *dst;
143
    int bufidx;
Michael Niedermayer's avatar
Michael Niedermayer committed
144

145
//    src[0]&0x80;                //forbidden bit
Michael Niedermayer's avatar
Michael Niedermayer committed
146 147 148 149
    h->nal_ref_idc= src[0]>>5;
    h->nal_unit_type= src[0]&0x1F;

    src++; length--;
150
#if 0
Michael Niedermayer's avatar
Michael Niedermayer committed
151 152 153
    for(i=0; i<length; i++)
        printf("%2X ", src[i]);
#endif
154

155 156
#if HAVE_FAST_UNALIGNED
# if HAVE_FAST_64BIT
157 158
#   define RS 7
    for(i=0; i+1<length; i+=9){
Ivan Schreter's avatar
Ivan Schreter committed
159
        if(!((~*(const uint64_t*)(src+i) & (*(const uint64_t*)(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
160 161 162
# else
#   define RS 3
    for(i=0; i+1<length; i+=5){
Ivan Schreter's avatar
Ivan Schreter committed
163
        if(!((~*(const uint32_t*)(src+i) & (*(const uint32_t*)(src+i) - 0x01000101U)) & 0x80008080U))
164 165 166 167 168 169
# endif
            continue;
        if(i>0 && !src[i]) i--;
        while(src[i]) i++;
#else
#   define RS 0
Michael Niedermayer's avatar
Michael Niedermayer committed
170 171 172
    for(i=0; i+1<length; i+=2){
        if(src[i]) continue;
        if(i>0 && src[i-1]==0) i--;
173
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
174 175 176 177 178 179 180
        if(i+2<length && src[i+1]==0 && src[i+2]<=3){
            if(src[i+2]!=3){
                /* startcode, so we must be past the end */
                length=i;
            }
            break;
        }
181
        i-= RS;
Michael Niedermayer's avatar
Michael Niedermayer committed
182 183 184 185 186
    }

    if(i>=length-1){ //no escaped 0
        *dst_length= length;
        *consumed= length+1; //+1 for the header
187
        return src;
Michael Niedermayer's avatar
Michael Niedermayer committed
188 189
    }

190
    bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
191
    av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE);
192
    dst= h->rbsp_buffer[bufidx];
Michael Niedermayer's avatar
Michael Niedermayer committed
193

194 195 196 197
    if (dst == NULL){
        return NULL;
    }

198
//printf("decoding esc\n");
199 200 201
    memcpy(dst, src, i);
    si=di=i;
    while(si+2<length){
Michael Niedermayer's avatar
Michael Niedermayer committed
202
        //remove escapes (very rare 1:2^22)
203 204 205 206
        if(src[si+2]>3){
            dst[di++]= src[si++];
            dst[di++]= src[si++];
        }else if(src[si]==0 && src[si+1]==0){
Michael Niedermayer's avatar
Michael Niedermayer committed
207 208 209 210
            if(src[si+2]==3){ //escape
                dst[di++]= 0;
                dst[di++]= 0;
                si+=3;
211
                continue;
Michael Niedermayer's avatar
Michael Niedermayer committed
212
            }else //next start code
213
                goto nsc;
Michael Niedermayer's avatar
Michael Niedermayer committed
214 215 216 217
        }

        dst[di++]= src[si++];
    }
218 219 220
    while(si<length)
        dst[di++]= src[si++];
nsc:
Michael Niedermayer's avatar
Michael Niedermayer committed
221

222 223
    memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);

Michael Niedermayer's avatar
Michael Niedermayer committed
224 225
    *dst_length= di;
    *consumed= si + 1;//+1 for the header
Diego Biurrun's avatar
Diego Biurrun committed
226
//FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
Michael Niedermayer's avatar
Michael Niedermayer committed
227 228 229
    return dst;
}

230
int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
Michael Niedermayer's avatar
Michael Niedermayer committed
231 232 233
    int v= *src;
    int r;

Michel Bardiaux's avatar
Michel Bardiaux committed
234
    tprintf(h->s.avctx, "rbsp trailing %X\n", v);
Michael Niedermayer's avatar
Michael Niedermayer committed
235 236 237 238 239 240 241 242 243

    for(r=1; r<9; r++){
        if(v&1) return r;
        v>>=1;
    }
    return 0;
}

/**
Diego Biurrun's avatar
Diego Biurrun committed
244
 * IDCT transforms the 16 dc values and dequantizes them.
Michael Niedermayer's avatar
Michael Niedermayer committed
245 246
 * @param qp quantization parameter
 */
Loren Merritt's avatar
Loren Merritt committed
247
static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
Michael Niedermayer's avatar
Michael Niedermayer committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
#define stride 16
    int i;
    int temp[16]; //FIXME check if this is a good idea
    static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};
    static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};

//memset(block, 64, 2*256);
//return;
    for(i=0; i<4; i++){
        const int offset= y_offset[i];
        const int z0= block[offset+stride*0] + block[offset+stride*4];
        const int z1= block[offset+stride*0] - block[offset+stride*4];
        const int z2= block[offset+stride*1] - block[offset+stride*5];
        const int z3= block[offset+stride*1] + block[offset+stride*5];

        temp[4*i+0]= z0+z3;
        temp[4*i+1]= z1+z2;
        temp[4*i+2]= z1-z2;
        temp[4*i+3]= z0-z3;
    }

    for(i=0; i<4; i++){
        const int offset= x_offset[i];
        const int z0= temp[4*0+i] + temp[4*2+i];
        const int z1= temp[4*0+i] - temp[4*2+i];
        const int z2= temp[4*1+i] - temp[4*3+i];
        const int z3= temp[4*1+i] + temp[4*3+i];

Diego Biurrun's avatar
Diego Biurrun committed
276
        block[stride*0 +offset]= ((((z0 + z3)*qmul + 128 ) >> 8)); //FIXME think about merging this into decode_residual
Loren Merritt's avatar
Loren Merritt committed
277 278 279
        block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8));
        block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8));
        block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8));
Michael Niedermayer's avatar
Michael Niedermayer committed
280 281 282
    }
}

283
#if 0
Michael Niedermayer's avatar
Michael Niedermayer committed
284
/**
Diego Biurrun's avatar
Diego Biurrun committed
285
 * DCT transforms the 16 dc values.
Michael Niedermayer's avatar
Michael Niedermayer committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
 * @param qp quantization parameter ??? FIXME
 */
static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
//    const int qmul= dequant_coeff[qp][0];
    int i;
    int temp[16]; //FIXME check if this is a good idea
    static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};
    static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};

    for(i=0; i<4; i++){
        const int offset= y_offset[i];
        const int z0= block[offset+stride*0] + block[offset+stride*4];
        const int z1= block[offset+stride*0] - block[offset+stride*4];
        const int z2= block[offset+stride*1] - block[offset+stride*5];
        const int z3= block[offset+stride*1] + block[offset+stride*5];

        temp[4*i+0]= z0+z3;
        temp[4*i+1]= z1+z2;
        temp[4*i+2]= z1-z2;
        temp[4*i+3]= z0-z3;
    }

    for(i=0; i<4; i++){
        const int offset= x_offset[i];
        const int z0= temp[4*0+i] + temp[4*2+i];
        const int z1= temp[4*0+i] - temp[4*2+i];
        const int z2= temp[4*1+i] - temp[4*3+i];
        const int z3= temp[4*1+i] + temp[4*3+i];

        block[stride*0 +offset]= (z0 + z3)>>1;
        block[stride*2 +offset]= (z1 + z2)>>1;
        block[stride*8 +offset]= (z1 - z2)>>1;
        block[stride*10+offset]= (z0 - z3)>>1;
    }
}
321 322
#endif

Michael Niedermayer's avatar
Michael Niedermayer committed
323 324 325
#undef xStride
#undef stride

Loren Merritt's avatar
Loren Merritt committed
326
static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp, int qmul){
Michael Niedermayer's avatar
Michael Niedermayer committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340
    const int stride= 16*2;
    const int xStride= 16;
    int a,b,c,d,e;

    a= block[stride*0 + xStride*0];
    b= block[stride*0 + xStride*1];
    c= block[stride*1 + xStride*0];
    d= block[stride*1 + xStride*1];

    e= a-b;
    a= a+b;
    b= c-d;
    c= c+d;

Loren Merritt's avatar
Loren Merritt committed
341 342 343 344
    block[stride*0 + xStride*0]= ((a+c)*qmul) >> 7;
    block[stride*0 + xStride*1]= ((e+b)*qmul) >> 7;
    block[stride*1 + xStride*0]= ((a-c)*qmul) >> 7;
    block[stride*1 + xStride*1]= ((e-b)*qmul) >> 7;
Michael Niedermayer's avatar
Michael Niedermayer committed
345 346
}

347
#if 0
Michael Niedermayer's avatar
Michael Niedermayer committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
static void chroma_dc_dct_c(DCTELEM *block){
    const int stride= 16*2;
    const int xStride= 16;
    int a,b,c,d,e;

    a= block[stride*0 + xStride*0];
    b= block[stride*0 + xStride*1];
    c= block[stride*1 + xStride*0];
    d= block[stride*1 + xStride*1];

    e= a-b;
    a= a+b;
    b= c-d;
    c= c+d;

    block[stride*0 + xStride*0]= (a+c);
    block[stride*0 + xStride*1]= (e+b);
    block[stride*1 + xStride*0]= (a-c);
    block[stride*1 + xStride*1]= (e-b);
}
368
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
369 370 371 372 373 374 375

static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
                           int src_x_offset, int src_y_offset,
                           qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
    MpegEncContext * const s = &h->s;
    const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
376
    int my=       h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
Michael Niedermayer's avatar
Michael Niedermayer committed
377
    const int luma_xy= (mx&3) + ((my&3)<<2);
378 379 380 381
    uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
    uint8_t * src_cb, * src_cr;
    int extra_width= h->emu_edge_width;
    int extra_height= h->emu_edge_height;
Michael Niedermayer's avatar
Michael Niedermayer committed
382 383 384
    int emu=0;
    const int full_mx= mx>>2;
    const int full_my= my>>2;
385
    const int pic_width  = 16*s->mb_width;
386
    const int pic_height = 16*s->mb_height >> MB_FIELD;
387

Michael Niedermayer's avatar
Michael Niedermayer committed
388 389
    if(mx&7) extra_width -= 3;
    if(my&7) extra_height -= 3;
390 391 392 393

    if(   full_mx < 0-extra_width
       || full_my < 0-extra_height
       || full_mx + 16/*FIXME*/ > pic_width + extra_width
394
       || full_my + 16/*FIXME*/ > pic_height + extra_height){
395 396
        ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
            src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
Michael Niedermayer's avatar
Michael Niedermayer committed
397 398
        emu=1;
    }
399

400
    qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
Michael Niedermayer's avatar
Michael Niedermayer committed
401
    if(!square){
402
        qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
Michael Niedermayer's avatar
Michael Niedermayer committed
403
    }
404

405
    if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
406

407
    if(MB_FIELD){
408
        // chroma offset when predicting from a field of opposite parity
409
        my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
410 411 412 413 414
        emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
    }
    src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
    src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;

Michael Niedermayer's avatar
Michael Niedermayer committed
415
    if(emu){
416
        ff_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);
Michael Niedermayer's avatar
Michael Niedermayer committed
417 418
            src_cb= s->edge_emu_buffer;
    }
419
    chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
Michael Niedermayer's avatar
Michael Niedermayer committed
420 421

    if(emu){
422
        ff_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);
Michael Niedermayer's avatar
Michael Niedermayer committed
423 424
            src_cr= s->edge_emu_buffer;
    }
425
    chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
Michael Niedermayer's avatar
Michael Niedermayer committed
426 427
}

Loren Merritt's avatar
Loren Merritt committed
428
static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
Michael Niedermayer's avatar
Michael Niedermayer committed
429 430 431 432 433 434 435 436
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
                           int x_offset, int y_offset,
                           qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
                           qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
                           int list0, int list1){
    MpegEncContext * const s = &h->s;
    qpel_mc_func *qpix_op=  qpix_put;
    h264_chroma_mc_func chroma_op= chroma_put;
437

438 439 440
    dest_y  += 2*x_offset + 2*y_offset*h->  mb_linesize;
    dest_cb +=   x_offset +   y_offset*h->mb_uvlinesize;
    dest_cr +=   x_offset +   y_offset*h->mb_uvlinesize;
Michael Niedermayer's avatar
Michael Niedermayer committed
441
    x_offset += 8*s->mb_x;
442
    y_offset += 8*(s->mb_y >> MB_FIELD);
443

Michael Niedermayer's avatar
Michael Niedermayer committed
444
    if(list0){
445
        Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
Michael Niedermayer's avatar
Michael Niedermayer committed
446 447 448 449 450 451 452 453 454
        mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
                           dest_y, dest_cb, dest_cr, x_offset, y_offset,
                           qpix_op, chroma_op);

        qpix_op=  qpix_avg;
        chroma_op= chroma_avg;
    }

    if(list1){
455
        Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
Michael Niedermayer's avatar
Michael Niedermayer committed
456 457 458 459 460 461
        mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
                           dest_y, dest_cb, dest_cr, x_offset, y_offset,
                           qpix_op, chroma_op);
    }
}

Loren Merritt's avatar
Loren Merritt committed
462 463 464 465 466 467 468 469 470
static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
                           int x_offset, int y_offset,
                           qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
                           h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
                           h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
                           int list0, int list1){
    MpegEncContext * const s = &h->s;

471 472 473
    dest_y  += 2*x_offset + 2*y_offset*h->  mb_linesize;
    dest_cb +=   x_offset +   y_offset*h->mb_uvlinesize;
    dest_cr +=   x_offset +   y_offset*h->mb_uvlinesize;
Loren Merritt's avatar
Loren Merritt committed
474
    x_offset += 8*s->mb_x;
475
    y_offset += 8*(s->mb_y >> MB_FIELD);
476

Loren Merritt's avatar
Loren Merritt committed
477 478 479 480
    if(list0 && list1){
        /* don't optimize for luma-only case, since B-frames usually
         * use implicit weights => chroma too. */
        uint8_t *tmp_cb = s->obmc_scratchpad;
481 482
        uint8_t *tmp_cr = s->obmc_scratchpad + 8;
        uint8_t *tmp_y  = s->obmc_scratchpad + 8*h->mb_uvlinesize;
Loren Merritt's avatar
Loren Merritt committed
483 484 485 486 487 488 489 490 491 492 493 494 495
        int refn0 = h->ref_cache[0][ scan8[n] ];
        int refn1 = h->ref_cache[1][ scan8[n] ];

        mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
                    dest_y, dest_cb, dest_cr,
                    x_offset, y_offset, qpix_put, chroma_put);
        mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
                    tmp_y, tmp_cb, tmp_cr,
                    x_offset, y_offset, qpix_put, chroma_put);

        if(h->use_weight == 2){
            int weight0 = h->implicit_weight[refn0][refn1];
            int weight1 = 64 - weight0;
496 497 498
            luma_weight_avg(  dest_y,  tmp_y,  h->  mb_linesize, 5, weight0, weight1, 0);
            chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
            chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
Loren Merritt's avatar
Loren Merritt committed
499
        }else{
500
            luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
501
                            h->luma_weight[0][refn0], h->luma_weight[1][refn1],
Loren Merritt's avatar
Loren Merritt committed
502
                            h->luma_offset[0][refn0] + h->luma_offset[1][refn1]);
503
            chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
504
                            h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
Loren Merritt's avatar
Loren Merritt committed
505
                            h->chroma_offset[0][refn0][0] + h->chroma_offset[1][refn1][0]);
506
            chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
507
                            h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
Loren Merritt's avatar
Loren Merritt committed
508
                            h->chroma_offset[0][refn0][1] + h->chroma_offset[1][refn1][1]);
Loren Merritt's avatar
Loren Merritt committed
509 510 511 512 513 514 515 516 517
        }
    }else{
        int list = list1 ? 1 : 0;
        int refn = h->ref_cache[list][ scan8[n] ];
        Picture *ref= &h->ref_list[list][refn];
        mc_dir_part(h, ref, n, square, chroma_height, delta, list,
                    dest_y, dest_cb, dest_cr, x_offset, y_offset,
                    qpix_put, chroma_put);

518
        luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
Loren Merritt's avatar
Loren Merritt committed
519 520
                       h->luma_weight[list][refn], h->luma_offset[list][refn]);
        if(h->use_weight_chroma){
521
            chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
Loren Merritt's avatar
Loren Merritt committed
522
                             h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
523
            chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
Loren Merritt's avatar
Loren Merritt committed
524 525 526 527 528 529 530 531 532 533
                             h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
        }
    }
}

static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
                           int x_offset, int y_offset,
                           qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
                           qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
534
                           h264_weight_func *weight_op, h264_biweight_func *weight_avg,
Loren Merritt's avatar
Loren Merritt committed
535 536 537 538 539 540 541 542 543 544 545 546
                           int list0, int list1){
    if((h->use_weight==2 && list0 && list1
        && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
       || h->use_weight==1)
        mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
                         x_offset, y_offset, qpix_put, chroma_put,
                         weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
    else
        mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
                    x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
}

547 548 549 550 551 552 553 554 555
static inline void prefetch_motion(H264Context *h, int list){
    /* fetch pixels for estimated mv 4 macroblocks ahead
     * optimized for 64byte cache lines */
    MpegEncContext * const s = &h->s;
    const int refn = h->ref_cache[list][scan8[0]];
    if(refn >= 0){
        const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
        const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
        uint8_t **src= h->ref_list[list][refn].data;
556
        int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
557 558 559 560 561 562
        s->dsp.prefetch(src[0]+off, s->linesize, 4);
        off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
        s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
    }
}

Michael Niedermayer's avatar
Michael Niedermayer committed
563 564
static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
                      qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
Loren Merritt's avatar
Loren Merritt committed
565 566
                      qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
                      h264_weight_func *weight_op, h264_biweight_func *weight_avg){
Michael Niedermayer's avatar
Michael Niedermayer committed
567
    MpegEncContext * const s = &h->s;
568
    const int mb_xy= h->mb_xy;
Michael Niedermayer's avatar
Michael Niedermayer committed
569
    const int mb_type= s->current_picture.mb_type[mb_xy];
570

Michael Niedermayer's avatar
Michael Niedermayer committed
571
    assert(IS_INTER(mb_type));
572

573 574
    prefetch_motion(h, 0);

Michael Niedermayer's avatar
Michael Niedermayer committed
575 576 577
    if(IS_16X16(mb_type)){
        mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
                qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
578
                weight_op, weight_avg,
Michael Niedermayer's avatar
Michael Niedermayer committed
579 580 581 582
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
    }else if(IS_16X8(mb_type)){
        mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
                qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
Loren Merritt's avatar
Loren Merritt committed
583
                &weight_op[1], &weight_avg[1],
Michael Niedermayer's avatar
Michael Niedermayer committed
584 585 586
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
        mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
                qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
Loren Merritt's avatar
Loren Merritt committed
587
                &weight_op[1], &weight_avg[1],
Michael Niedermayer's avatar
Michael Niedermayer committed
588 589
                IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
    }else if(IS_8X16(mb_type)){
590
        mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
Michael Niedermayer's avatar
Michael Niedermayer committed
591
                qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
Loren Merritt's avatar
Loren Merritt committed
592
                &weight_op[2], &weight_avg[2],
Michael Niedermayer's avatar
Michael Niedermayer committed
593
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
594
        mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
Michael Niedermayer's avatar
Michael Niedermayer committed
595
                qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
Loren Merritt's avatar
Loren Merritt committed
596
                &weight_op[2], &weight_avg[2],
Michael Niedermayer's avatar
Michael Niedermayer committed
597 598 599
                IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
    }else{
        int i;
600

Michael Niedermayer's avatar
Michael Niedermayer committed
601 602 603 604 605 606 607 608 609 610 611
        assert(IS_8X8(mb_type));

        for(i=0; i<4; i++){
            const int sub_mb_type= h->sub_mb_type[i];
            const int n= 4*i;
            int x_offset= (i&1)<<2;
            int y_offset= (i&2)<<1;

            if(IS_SUB_8X8(sub_mb_type)){
                mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
                    qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
Loren Merritt's avatar
Loren Merritt committed
612
                    &weight_op[3], &weight_avg[3],
Michael Niedermayer's avatar
Michael Niedermayer committed
613 614 615 616
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
            }else if(IS_SUB_8X4(sub_mb_type)){
                mc_part(h, n  , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
                    qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
Loren Merritt's avatar
Loren Merritt committed
617
                    &weight_op[4], &weight_avg[4],
Michael Niedermayer's avatar
Michael Niedermayer committed
618 619 620
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
                mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
                    qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
Loren Merritt's avatar
Loren Merritt committed
621
                    &weight_op[4], &weight_avg[4],
Michael Niedermayer's avatar
Michael Niedermayer committed
622 623
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
            }else if(IS_SUB_4X8(sub_mb_type)){
624
                mc_part(h, n  , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
Michael Niedermayer's avatar
Michael Niedermayer committed
625
                    qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
Loren Merritt's avatar
Loren Merritt committed
626
                    &weight_op[5], &weight_avg[5],
Michael Niedermayer's avatar
Michael Niedermayer committed
627
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
628
                mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
Michael Niedermayer's avatar
Michael Niedermayer committed
629
                    qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
Loren Merritt's avatar
Loren Merritt committed
630
                    &weight_op[5], &weight_avg[5],
Michael Niedermayer's avatar
Michael Niedermayer committed
631 632 633 634 635 636 637 638 639
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
            }else{
                int j;
                assert(IS_SUB_4X4(sub_mb_type));
                for(j=0; j<4; j++){
                    int sub_x_offset= x_offset + 2*(j&1);
                    int sub_y_offset= y_offset +   (j&2);
                    mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
                        qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
Loren Merritt's avatar
Loren Merritt committed
640
                        &weight_op[6], &weight_avg[6],
Michael Niedermayer's avatar
Michael Niedermayer committed
641 642 643 644 645
                        IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
                }
            }
        }
    }
646 647

    prefetch_motion(h, 1);
Michael Niedermayer's avatar
Michael Niedermayer committed
648 649 650 651
}


static void free_tables(H264Context *h){
652
    int i;
653
    H264Context *hx;
Michael Niedermayer's avatar
Michael Niedermayer committed
654
    av_freep(&h->intra4x4_pred_mode);
655 656
    av_freep(&h->chroma_pred_mode_table);
    av_freep(&h->cbp_table);
657 658
    av_freep(&h->mvd_table[0]);
    av_freep(&h->mvd_table[1]);
659
    av_freep(&h->direct_table);
Michael Niedermayer's avatar
Michael Niedermayer committed
660 661 662
    av_freep(&h->non_zero_count);
    av_freep(&h->slice_table_base);
    h->slice_table= NULL;
663
    av_freep(&h->list_counts);
664

Michael Niedermayer's avatar
Michael Niedermayer committed
665 666
    av_freep(&h->mb2b_xy);
    av_freep(&h->mb2b8_xy);
Loren Merritt's avatar
Loren Merritt committed
667

668
    for(i = 0; i < MAX_THREADS; i++) {
669 670 671 672 673
        hx = h->thread_context[i];
        if(!hx) continue;
        av_freep(&hx->top_borders[1]);
        av_freep(&hx->top_borders[0]);
        av_freep(&hx->s.obmc_scratchpad);
674 675
        av_freep(&hx->rbsp_buffer[1]);
        av_freep(&hx->rbsp_buffer[0]);
676 677
        hx->rbsp_buffer_size[0] = 0;
        hx->rbsp_buffer_size[1] = 0;
678
        if (i) av_freep(&h->thread_context[i]);
679
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
680 681
}

Loren Merritt's avatar
Loren Merritt committed
682 683
static void init_dequant8_coeff_table(H264Context *h){
    int i,q,x;
Loren Merritt's avatar
Loren Merritt committed
684
    const int transpose = (h->s.dsp.h264_idct8_add != ff_h264_idct8_add_c); //FIXME ugly
Loren Merritt's avatar
Loren Merritt committed
685 686 687 688 689 690 691 692 693 694
    h->dequant8_coeff[0] = h->dequant8_buffer[0];
    h->dequant8_coeff[1] = h->dequant8_buffer[1];

    for(i=0; i<2; i++ ){
        if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
            h->dequant8_coeff[1] = h->dequant8_buffer[0];
            break;
        }

        for(q=0; q<52; q++){
695 696
            int shift = div6[q];
            int idx = rem6[q];
Loren Merritt's avatar
Loren Merritt committed
697
            for(x=0; x<64; x++)
Loren Merritt's avatar
Loren Merritt committed
698 699 700
                h->dequant8_coeff[i][q][transpose ? (x>>3)|((x&7)<<3) : x] =
                    ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
                    h->pps.scaling_matrix8[i][x]) << shift;
Loren Merritt's avatar
Loren Merritt committed
701 702 703 704 705 706
        }
    }
}

static void init_dequant4_coeff_table(H264Context *h){
    int i,j,q,x;
707
    const int transpose = (h->s.dsp.h264_idct_add != ff_h264_idct_add_c); //FIXME ugly
Loren Merritt's avatar
Loren Merritt committed
708 709 710 711 712 713 714 715 716 717 718 719
    for(i=0; i<6; i++ ){
        h->dequant4_coeff[i] = h->dequant4_buffer[i];
        for(j=0; j<i; j++){
            if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
                h->dequant4_coeff[i] = h->dequant4_buffer[j];
                break;
            }
        }
        if(j<i)
            continue;

        for(q=0; q<52; q++){
720 721
            int shift = div6[q] + 2;
            int idx = rem6[q];
Loren Merritt's avatar
Loren Merritt committed
722
            for(x=0; x<16; x++)
723 724
                h->dequant4_coeff[i][q][transpose ? (x>>2)|((x<<2)&0xF) : x] =
                    ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
Loren Merritt's avatar
Loren Merritt committed
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
                    h->pps.scaling_matrix4[i][x]) << shift;
        }
    }
}

static void init_dequant_tables(H264Context *h){
    int i,x;
    init_dequant4_coeff_table(h);
    if(h->pps.transform_8x8_mode)
        init_dequant8_coeff_table(h);
    if(h->sps.transform_bypass){
        for(i=0; i<6; i++)
            for(x=0; x<16; x++)
                h->dequant4_coeff[i][0][x] = 1<<6;
        if(h->pps.transform_8x8_mode)
            for(i=0; i<2; i++)
                for(x=0; x<64; x++)
                    h->dequant8_coeff[i][0][x] = 1<<6;
    }
}


747
int ff_h264_alloc_tables(H264Context *h){
Michael Niedermayer's avatar
Michael Niedermayer committed
748
    MpegEncContext * const s = &h->s;
749
    const int big_mb_num= s->mb_stride * (s->mb_height+1);
Loren Merritt's avatar
Loren Merritt committed
750
    int x,y;
Michael Niedermayer's avatar
Michael Niedermayer committed
751

752
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, big_mb_num * 8  * sizeof(uint8_t), fail)
753

754
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count    , big_mb_num * 32 * sizeof(uint8_t), fail)
755 756
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base  , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
Michael Niedermayer's avatar
Michael Niedermayer committed
757

758 759 760 761
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t), fail);
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t), fail);
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 32*big_mb_num * sizeof(uint8_t) , fail);
762
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->list_counts, big_mb_num * sizeof(uint8_t), fail)
763

764
    memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride)  * sizeof(*h->slice_table_base));
765
    h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
Michael Niedermayer's avatar
Michael Niedermayer committed
766

767 768
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy  , big_mb_num * sizeof(uint32_t), fail);
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b8_xy , big_mb_num * sizeof(uint32_t), fail);
Michael Niedermayer's avatar
Michael Niedermayer committed
769 770
    for(y=0; y<s->mb_height; y++){
        for(x=0; x<s->mb_width; x++){
771
            const int mb_xy= x + y*s->mb_stride;
Michael Niedermayer's avatar
Michael Niedermayer committed
772 773
            const int b_xy = 4*x + 4*y*h->b_stride;
            const int b8_xy= 2*x + 2*y*h->b8_stride;
774

Michael Niedermayer's avatar
Michael Niedermayer committed
775 776 777 778
            h->mb2b_xy [mb_xy]= b_xy;
            h->mb2b8_xy[mb_xy]= b8_xy;
        }
    }
Loren Merritt's avatar
Loren Merritt committed
779

780 781
    s->obmc_scratchpad = NULL;

782 783 784
    if(!h->dequant4_coeff[0])
        init_dequant_tables(h);

Michael Niedermayer's avatar
Michael Niedermayer committed
785 786 787 788 789 790
    return 0;
fail:
    free_tables(h);
    return -1;
}

791 792 793 794 795 796 797 798 799 800 801 802 803 804
/**
 * Mimic alloc_tables(), but for every context thread.
 */
static void clone_tables(H264Context *dst, H264Context *src){
    dst->intra4x4_pred_mode       = src->intra4x4_pred_mode;
    dst->non_zero_count           = src->non_zero_count;
    dst->slice_table              = src->slice_table;
    dst->cbp_table                = src->cbp_table;
    dst->mb2b_xy                  = src->mb2b_xy;
    dst->mb2b8_xy                 = src->mb2b8_xy;
    dst->chroma_pred_mode_table   = src->chroma_pred_mode_table;
    dst->mvd_table[0]             = src->mvd_table[0];
    dst->mvd_table[1]             = src->mvd_table[1];
    dst->direct_table             = src->direct_table;
805
    dst->list_counts              = src->list_counts;
806 807 808 809 810 811 812 813 814 815

    dst->s.obmc_scratchpad = NULL;
    ff_h264_pred_init(&dst->hpc, src->s.codec_id);
}

/**
 * Init context
 * Allocate buffers which are not shared amongst multiple threads.
 */
static int context_init(H264Context *h){
816 817
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
    FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
818 819 820 821 822 823

    return 0;
fail:
    return -1; // free_tables will clean up for us
}

824
static av_cold void common_init(H264Context *h){
Michael Niedermayer's avatar
Michael Niedermayer committed
825 826 827 828 829
    MpegEncContext * const s = &h->s;

    s->width = s->avctx->width;
    s->height = s->avctx->height;
    s->codec_id= s->avctx->codec->id;
830

831
    ff_h264_pred_init(&h->hpc, s->codec_id);
Michael Niedermayer's avatar
Michael Niedermayer committed
832

Loren Merritt's avatar
Loren Merritt committed
833
    h->dequant_coeff_pps= -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
834
    s->unrestricted_mv=1;
Michael Niedermayer's avatar
Michael Niedermayer committed
835
    s->decode=1; //FIXME
836

837 838
    dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early

839 840
    memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
    memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
Michael Niedermayer's avatar
Michael Niedermayer committed
841 842
}

843
av_cold int ff_h264_decode_init(AVCodecContext *avctx){
Michael Niedermayer's avatar
Michael Niedermayer committed
844 845 846
    H264Context *h= avctx->priv_data;
    MpegEncContext * const s = &h->s;

Michael Niedermayer's avatar
Michael Niedermayer committed
847
    MPV_decode_defaults(s);
848

Michael Niedermayer's avatar
Michael Niedermayer committed
849 850 851 852 853 854 855 856
    s->avctx = avctx;
    common_init(h);

    s->out_format = FMT_H264;
    s->workaround_bugs= avctx->workaround_bugs;

    // set defaults
//    s->decode_mb= ff_h263_decode_mb;