• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/h264.c

Go to the documentation of this file.
00001 /*
00002  * H.26L/H.264/AVC/JVT/14496-10/... decoder
00003  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00028 #define UNCHECKED_BITSTREAM_READER 1
00029 
00030 #include "libavutil/imgutils.h"
00031 #include "libavutil/opt.h"
00032 #include "internal.h"
00033 #include "cabac.h"
00034 #include "cabac_functions.h"
00035 #include "dsputil.h"
00036 #include "avcodec.h"
00037 #include "mpegvideo.h"
00038 #include "h264.h"
00039 #include "h264data.h"
00040 #include "h264_mvpred.h"
00041 #include "golomb.h"
00042 #include "mathops.h"
00043 #include "rectangle.h"
00044 #include "thread.h"
00045 #include "vdpau_internal.h"
00046 #include "libavutil/avassert.h"
00047 
00048 //#undef NDEBUG
00049 #include <assert.h>
00050 
00051 static const uint8_t rem6[QP_MAX_NUM+1]={
00052 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, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
00053 };
00054 
00055 static const uint8_t div6[QP_MAX_NUM+1]={
00056 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, 8, 8, 9, 9, 9, 9, 9, 9,10,10,10,10,
00057 };
00058 
00059 static const enum PixelFormat hwaccel_pixfmt_list_h264_jpeg_420[] = {
00060     PIX_FMT_DXVA2_VLD,
00061     PIX_FMT_VAAPI_VLD,
00062     PIX_FMT_VDA_VLD,
00063     PIX_FMT_YUVJ420P,
00064     PIX_FMT_NONE
00065 };
00066 
00071 int ff_h264_check_intra4x4_pred_mode(H264Context *h){
00072     MpegEncContext * const s = &h->s;
00073     static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
00074     static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
00075     int i;
00076 
00077     if(!(h->top_samples_available&0x8000)){
00078         for(i=0; i<4; i++){
00079             int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
00080             if(status<0){
00081                 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);
00082                 return -1;
00083             } else if(status){
00084                 h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
00085             }
00086         }
00087     }
00088 
00089     if((h->left_samples_available&0x8888)!=0x8888){
00090         static const int mask[4]={0x8000,0x2000,0x80,0x20};
00091         for(i=0; i<4; i++){
00092             if(!(h->left_samples_available&mask[i])){
00093                 int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
00094                 if(status<0){
00095                     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);
00096                     return -1;
00097                 } else if(status){
00098                     h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
00099                 }
00100             }
00101         }
00102     }
00103 
00104     return 0;
00105 } //FIXME cleanup like check_intra_pred_mode
00106 
00107 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma){
00108     MpegEncContext * const s = &h->s;
00109     static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
00110     static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
00111 
00112     if(mode > 6U) {
00113         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);
00114         return -1;
00115     }
00116 
00117     if(!(h->top_samples_available&0x8000)){
00118         mode= top[ mode ];
00119         if(mode<0){
00120             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);
00121             return -1;
00122         }
00123     }
00124 
00125     if((h->left_samples_available&0x8080) != 0x8080){
00126         mode= left[ mode ];
00127         if(is_chroma && (h->left_samples_available&0x8080)){ //mad cow disease mode, aka MBAFF + constrained_intra_pred
00128             mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
00129         }
00130         if(mode<0){
00131             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);
00132             return -1;
00133         }
00134     }
00135 
00136     return mode;
00137 }
00138 
00139 
00140 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
00141     int i, si, di;
00142     uint8_t *dst;
00143     int bufidx;
00144 
00145 //    src[0]&0x80;                //forbidden bit
00146     h->nal_ref_idc= src[0]>>5;
00147     h->nal_unit_type= src[0]&0x1F;
00148 
00149     src++; length--;
00150 
00151 #if HAVE_FAST_UNALIGNED
00152 # if HAVE_FAST_64BIT
00153 #   define RS 7
00154     for(i=0; i+1<length; i+=9){
00155         if(!((~AV_RN64A(src+i) & (AV_RN64A(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
00156 # else
00157 #   define RS 3
00158     for(i=0; i+1<length; i+=5){
00159         if(!((~AV_RN32A(src+i) & (AV_RN32A(src+i) - 0x01000101U)) & 0x80008080U))
00160 # endif
00161             continue;
00162         if(i>0 && !src[i]) i--;
00163         while(src[i]) i++;
00164 #else
00165 #   define RS 0
00166     for(i=0; i+1<length; i+=2){
00167         if(src[i]) continue;
00168         if(i>0 && src[i-1]==0) i--;
00169 #endif
00170         if(i+2<length && src[i+1]==0 && src[i+2]<=3){
00171             if(src[i+2]!=3){
00172                 /* startcode, so we must be past the end */
00173                 length=i;
00174             }
00175             break;
00176         }
00177         i-= RS;
00178     }
00179 
00180     bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0; // use second escape buffer for inter data
00181     si=h->rbsp_buffer_size[bufidx];
00182     av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE+MAX_MBPAIR_SIZE);
00183     dst= h->rbsp_buffer[bufidx];
00184     if(si != h->rbsp_buffer_size[bufidx])
00185         memset(dst + length, 0, FF_INPUT_BUFFER_PADDING_SIZE+MAX_MBPAIR_SIZE);
00186 
00187     if (dst == NULL){
00188         return NULL;
00189     }
00190 
00191     if(i>=length-1){ //no escaped 0
00192         *dst_length= length;
00193         *consumed= length+1; //+1 for the header
00194         if(h->s.avctx->flags2 & CODEC_FLAG2_FAST){
00195             return src;
00196         }else{
00197             memcpy(dst, src, length);
00198             return dst;
00199         }
00200     }
00201 
00202 //printf("decoding esc\n");
00203     memcpy(dst, src, i);
00204     si=di=i;
00205     while(si+2<length){
00206         //remove escapes (very rare 1:2^22)
00207         if(src[si+2]>3){
00208             dst[di++]= src[si++];
00209             dst[di++]= src[si++];
00210         }else if(src[si]==0 && src[si+1]==0){
00211             if(src[si+2]==3){ //escape
00212                 dst[di++]= 0;
00213                 dst[di++]= 0;
00214                 si+=3;
00215                 continue;
00216             }else //next start code
00217                 goto nsc;
00218         }
00219 
00220         dst[di++]= src[si++];
00221     }
00222     while(si<length)
00223         dst[di++]= src[si++];
00224 nsc:
00225 
00226     memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00227 
00228     *dst_length= di;
00229     *consumed= si + 1;//+1 for the header
00230 //FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
00231     return dst;
00232 }
00233 
00238 static int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
00239     int v= *src;
00240     int r;
00241 
00242     tprintf(h->s.avctx, "rbsp trailing %X\n", v);
00243 
00244     for(r=1; r<9; r++){
00245         if(v&1) return r;
00246         v>>=1;
00247     }
00248     return 0;
00249 }
00250 
00251 static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n, int height,
00252                                  int y_offset, int list){
00253     int raw_my= h->mv_cache[list][ scan8[n] ][1];
00254     int filter_height= (raw_my&3) ? 2 : 0;
00255     int full_my= (raw_my>>2) + y_offset;
00256     int top = full_my - filter_height, bottom = full_my + height + filter_height;
00257 
00258     return FFMAX(abs(top), bottom);
00259 }
00260 
00261 static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n, int height,
00262                                int y_offset, int list0, int list1, int *nrefs){
00263     MpegEncContext * const s = &h->s;
00264     int my;
00265 
00266     y_offset += 16*(s->mb_y >> MB_FIELD);
00267 
00268     if(list0){
00269         int ref_n = h->ref_cache[0][ scan8[n] ];
00270         Picture *ref= &h->ref_list[0][ref_n];
00271 
00272         // Error resilience puts the current picture in the ref list.
00273         // Don't try to wait on these as it will cause a deadlock.
00274         // Fields can wait on each other, though.
00275         if (ref->f.thread_opaque != s->current_picture.f.thread_opaque ||
00276            (ref->f.reference & 3) != s->picture_structure) {
00277             my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
00278             if (refs[0][ref_n] < 0) nrefs[0] += 1;
00279             refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
00280         }
00281     }
00282 
00283     if(list1){
00284         int ref_n = h->ref_cache[1][ scan8[n] ];
00285         Picture *ref= &h->ref_list[1][ref_n];
00286 
00287         if (ref->f.thread_opaque != s->current_picture.f.thread_opaque ||
00288            (ref->f.reference & 3) != s->picture_structure) {
00289             my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
00290             if (refs[1][ref_n] < 0) nrefs[1] += 1;
00291             refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
00292         }
00293     }
00294 }
00295 
00301 static void await_references(H264Context *h){
00302     MpegEncContext * const s = &h->s;
00303     const int mb_xy= h->mb_xy;
00304     const int mb_type = s->current_picture.f.mb_type[mb_xy];
00305     int refs[2][48];
00306     int nrefs[2] = {0};
00307     int ref, list;
00308 
00309     memset(refs, -1, sizeof(refs));
00310 
00311     if(IS_16X16(mb_type)){
00312         get_lowest_part_y(h, refs, 0, 16, 0,
00313                   IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
00314     }else if(IS_16X8(mb_type)){
00315         get_lowest_part_y(h, refs, 0, 8, 0,
00316                   IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
00317         get_lowest_part_y(h, refs, 8, 8, 8,
00318                   IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
00319     }else if(IS_8X16(mb_type)){
00320         get_lowest_part_y(h, refs, 0, 16, 0,
00321                   IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
00322         get_lowest_part_y(h, refs, 4, 16, 0,
00323                   IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
00324     }else{
00325         int i;
00326 
00327         assert(IS_8X8(mb_type));
00328 
00329         for(i=0; i<4; i++){
00330             const int sub_mb_type= h->sub_mb_type[i];
00331             const int n= 4*i;
00332             int y_offset= (i&2)<<2;
00333 
00334             if(IS_SUB_8X8(sub_mb_type)){
00335                 get_lowest_part_y(h, refs, n  , 8, y_offset,
00336                           IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
00337             }else if(IS_SUB_8X4(sub_mb_type)){
00338                 get_lowest_part_y(h, refs, n  , 4, y_offset,
00339                           IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
00340                 get_lowest_part_y(h, refs, n+2, 4, y_offset+4,
00341                           IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
00342             }else if(IS_SUB_4X8(sub_mb_type)){
00343                 get_lowest_part_y(h, refs, n  , 8, y_offset,
00344                           IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
00345                 get_lowest_part_y(h, refs, n+1, 8, y_offset,
00346                           IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
00347             }else{
00348                 int j;
00349                 assert(IS_SUB_4X4(sub_mb_type));
00350                 for(j=0; j<4; j++){
00351                     int sub_y_offset= y_offset + 2*(j&2);
00352                     get_lowest_part_y(h, refs, n+j, 4, sub_y_offset,
00353                               IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);
00354                 }
00355             }
00356         }
00357     }
00358 
00359     for(list=h->list_count-1; list>=0; list--){
00360         for(ref=0; ref<48 && nrefs[list]; ref++){
00361             int row = refs[list][ref];
00362             if(row >= 0){
00363                 Picture *ref_pic = &h->ref_list[list][ref];
00364                 int ref_field = ref_pic->f.reference - 1;
00365                 int ref_field_picture = ref_pic->field_picture;
00366                 int pic_height = 16*s->mb_height >> ref_field_picture;
00367 
00368                 row <<= MB_MBAFF;
00369                 nrefs[list]--;
00370 
00371                 if(!FIELD_PICTURE && ref_field_picture){ // frame referencing two fields
00372                     ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) - !(row&1), pic_height-1), 1);
00373                     ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1)           , pic_height-1), 0);
00374                 }else if(FIELD_PICTURE && !ref_field_picture){ // field referencing one field of a frame
00375                     ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row*2 + ref_field    , pic_height-1), 0);
00376                 }else if(FIELD_PICTURE){
00377                     ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), ref_field);
00378                 }else{
00379                     ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), 0);
00380                 }
00381             }
00382         }
00383     }
00384 }
00385 
00386 #if 0
00387 
00391 static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
00392 //    const int qmul= dequant_coeff[qp][0];
00393     int i;
00394     int temp[16]; //FIXME check if this is a good idea
00395     static const int x_offset[4]={0, 1*stride, 4* stride,  5*stride};
00396     static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
00397 
00398     for(i=0; i<4; i++){
00399         const int offset= y_offset[i];
00400         const int z0= block[offset+stride*0] + block[offset+stride*4];
00401         const int z1= block[offset+stride*0] - block[offset+stride*4];
00402         const int z2= block[offset+stride*1] - block[offset+stride*5];
00403         const int z3= block[offset+stride*1] + block[offset+stride*5];
00404 
00405         temp[4*i+0]= z0+z3;
00406         temp[4*i+1]= z1+z2;
00407         temp[4*i+2]= z1-z2;
00408         temp[4*i+3]= z0-z3;
00409     }
00410 
00411     for(i=0; i<4; i++){
00412         const int offset= x_offset[i];
00413         const int z0= temp[4*0+i] + temp[4*2+i];
00414         const int z1= temp[4*0+i] - temp[4*2+i];
00415         const int z2= temp[4*1+i] - temp[4*3+i];
00416         const int z3= temp[4*1+i] + temp[4*3+i];
00417 
00418         block[stride*0 +offset]= (z0 + z3)>>1;
00419         block[stride*2 +offset]= (z1 + z2)>>1;
00420         block[stride*8 +offset]= (z1 - z2)>>1;
00421         block[stride*10+offset]= (z0 - z3)>>1;
00422     }
00423 }
00424 #endif
00425 
00426 #undef xStride
00427 #undef stride
00428 
00429 #if 0
00430 static void chroma_dc_dct_c(DCTELEM *block){
00431     const int stride= 16*2;
00432     const int xStride= 16;
00433     int a,b,c,d,e;
00434 
00435     a= block[stride*0 + xStride*0];
00436     b= block[stride*0 + xStride*1];
00437     c= block[stride*1 + xStride*0];
00438     d= block[stride*1 + xStride*1];
00439 
00440     e= a-b;
00441     a= a+b;
00442     b= c-d;
00443     c= c+d;
00444 
00445     block[stride*0 + xStride*0]= (a+c);
00446     block[stride*0 + xStride*1]= (e+b);
00447     block[stride*1 + xStride*0]= (a-c);
00448     block[stride*1 + xStride*1]= (e-b);
00449 }
00450 #endif
00451 
00452 static av_always_inline void
00453 mc_dir_part(H264Context *h, Picture *pic, int n, int square,
00454             int height, int delta, int list,
00455             uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00456             int src_x_offset, int src_y_offset,
00457             qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op,
00458             int pixel_shift, int chroma_idc)
00459 {
00460     MpegEncContext * const s = &h->s;
00461     const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
00462     int my=       h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
00463     const int luma_xy= (mx&3) + ((my&3)<<2);
00464     int offset = ((mx>>2) << pixel_shift) + (my>>2)*h->mb_linesize;
00465     uint8_t * src_y = pic->f.data[0] + offset;
00466     uint8_t * src_cb, * src_cr;
00467     int extra_width= h->emu_edge_width;
00468     int extra_height= h->emu_edge_height;
00469     int emu=0;
00470     const int full_mx= mx>>2;
00471     const int full_my= my>>2;
00472     const int pic_width  = 16*s->mb_width;
00473     const int pic_height = 16*s->mb_height >> MB_FIELD;
00474     int ysh;
00475 
00476     if(mx&7) extra_width -= 3;
00477     if(my&7) extra_height -= 3;
00478 
00479     if(   full_mx < 0-extra_width
00480        || full_my < 0-extra_height
00481        || full_mx + 16/*FIXME*/ > pic_width + extra_width
00482        || full_my + 16/*FIXME*/ > pic_height + extra_height){
00483         s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_y - (2 << pixel_shift) - 2*h->mb_linesize, h->mb_linesize,
00484                                 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
00485             src_y= s->edge_emu_buffer + (2 << pixel_shift) + 2*h->mb_linesize;
00486         emu=1;
00487     }
00488 
00489     qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); //FIXME try variable height perhaps?
00490     if(!square){
00491         qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
00492     }
00493 
00494     if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
00495 
00496     if(chroma_idc == 3 /* yuv444 */){
00497         src_cb = pic->f.data[1] + offset;
00498         if(emu){
00499             s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cb - (2 << pixel_shift) - 2*h->mb_linesize, h->mb_linesize,
00500                                     16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
00501             src_cb= s->edge_emu_buffer + (2 << pixel_shift) + 2*h->mb_linesize;
00502         }
00503         qpix_op[luma_xy](dest_cb, src_cb, h->mb_linesize); //FIXME try variable height perhaps?
00504         if(!square){
00505             qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize);
00506         }
00507 
00508         src_cr = pic->f.data[2] + offset;
00509         if(emu){
00510             s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cr - (2 << pixel_shift) - 2*h->mb_linesize, h->mb_linesize,
00511                                     16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
00512             src_cr= s->edge_emu_buffer + (2 << pixel_shift) + 2*h->mb_linesize;
00513         }
00514         qpix_op[luma_xy](dest_cr, src_cr, h->mb_linesize); //FIXME try variable height perhaps?
00515         if(!square){
00516             qpix_op[luma_xy](dest_cr + delta, src_cr + delta, h->mb_linesize);
00517         }
00518         return;
00519     }
00520 
00521     ysh = 3 - (chroma_idc == 2 /* yuv422 */);
00522     if(chroma_idc == 1 /* yuv420 */ && MB_FIELD){
00523         // chroma offset when predicting from a field of opposite parity
00524         my += 2 * ((s->mb_y & 1) - (pic->f.reference - 1));
00525         emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
00526     }
00527 
00528     src_cb = pic->f.data[1] + ((mx >> 3) << pixel_shift) + (my >> ysh) * h->mb_uvlinesize;
00529     src_cr = pic->f.data[2] + ((mx >> 3) << pixel_shift) + (my >> ysh) * h->mb_uvlinesize;
00530 
00531     if(emu){
00532         s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize,
00533                                 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
00534                                 pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
00535             src_cb= s->edge_emu_buffer;
00536     }
00537     chroma_op(dest_cb, src_cb, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
00538               mx&7, (my << (chroma_idc == 2 /* yuv422 */)) &7);
00539 
00540     if(emu){
00541         s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize,
00542                                 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
00543                                 pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
00544             src_cr= s->edge_emu_buffer;
00545     }
00546     chroma_op(dest_cr, src_cr, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
00547               mx&7, (my << (chroma_idc == 2 /* yuv422 */)) &7);
00548 }
00549 
00550 static av_always_inline void
00551 mc_part_std(H264Context *h, int n, int square, int height, int delta,
00552             uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00553             int x_offset, int y_offset,
00554             qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
00555             qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
00556             int list0, int list1, int pixel_shift, int chroma_idc)
00557 {
00558     MpegEncContext * const s = &h->s;
00559     qpel_mc_func *qpix_op=  qpix_put;
00560     h264_chroma_mc_func chroma_op= chroma_put;
00561 
00562     dest_y  += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
00563     if (chroma_idc == 3 /* yuv444 */) {
00564         dest_cb += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
00565         dest_cr += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
00566     } else if (chroma_idc == 2 /* yuv422 */) {
00567         dest_cb += (  x_offset << pixel_shift) + 2*y_offset*h->mb_uvlinesize;
00568         dest_cr += (  x_offset << pixel_shift) + 2*y_offset*h->mb_uvlinesize;
00569     } else /* yuv420 */ {
00570         dest_cb += (  x_offset << pixel_shift) +   y_offset*h->mb_uvlinesize;
00571         dest_cr += (  x_offset << pixel_shift) +   y_offset*h->mb_uvlinesize;
00572     }
00573     x_offset += 8*s->mb_x;
00574     y_offset += 8*(s->mb_y >> MB_FIELD);
00575 
00576     if(list0){
00577         Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
00578         mc_dir_part(h, ref, n, square, height, delta, 0,
00579                            dest_y, dest_cb, dest_cr, x_offset, y_offset,
00580                            qpix_op, chroma_op, pixel_shift, chroma_idc);
00581 
00582         qpix_op=  qpix_avg;
00583         chroma_op= chroma_avg;
00584     }
00585 
00586     if(list1){
00587         Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
00588         mc_dir_part(h, ref, n, square, height, delta, 1,
00589                            dest_y, dest_cb, dest_cr, x_offset, y_offset,
00590                            qpix_op, chroma_op, pixel_shift, chroma_idc);
00591     }
00592 }
00593 
00594 static av_always_inline void
00595 mc_part_weighted(H264Context *h, int n, int square, int height, int delta,
00596                  uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00597                  int x_offset, int y_offset,
00598                  qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
00599                  h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
00600                  h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
00601                  int list0, int list1, int pixel_shift, int chroma_idc){
00602     MpegEncContext * const s = &h->s;
00603     int chroma_height;
00604 
00605     dest_y += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
00606     if (chroma_idc == 3 /* yuv444 */) {
00607         chroma_height = height;
00608         chroma_weight_avg = luma_weight_avg;
00609         chroma_weight_op = luma_weight_op;
00610         dest_cb += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
00611         dest_cr += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
00612     } else if (chroma_idc == 2 /* yuv422 */) {
00613         chroma_height = height;
00614         dest_cb += (  x_offset << pixel_shift) + 2*y_offset*h->mb_uvlinesize;
00615         dest_cr += (  x_offset << pixel_shift) + 2*y_offset*h->mb_uvlinesize;
00616     } else /* yuv420 */ {
00617         chroma_height = height >> 1;
00618         dest_cb += (  x_offset << pixel_shift) +   y_offset*h->mb_uvlinesize;
00619         dest_cr += (  x_offset << pixel_shift) +   y_offset*h->mb_uvlinesize;
00620     }
00621     x_offset += 8*s->mb_x;
00622     y_offset += 8*(s->mb_y >> MB_FIELD);
00623 
00624     if(list0 && list1){
00625         /* don't optimize for luma-only case, since B-frames usually
00626          * use implicit weights => chroma too. */
00627         uint8_t *tmp_cb = s->obmc_scratchpad;
00628         uint8_t *tmp_cr = s->obmc_scratchpad + (16 << pixel_shift);
00629         uint8_t *tmp_y  = s->obmc_scratchpad + 16*h->mb_uvlinesize;
00630         int refn0 = h->ref_cache[0][ scan8[n] ];
00631         int refn1 = h->ref_cache[1][ scan8[n] ];
00632 
00633         mc_dir_part(h, &h->ref_list[0][refn0], n, square, height, delta, 0,
00634                     dest_y, dest_cb, dest_cr,
00635                     x_offset, y_offset, qpix_put, chroma_put,
00636                     pixel_shift, chroma_idc);
00637         mc_dir_part(h, &h->ref_list[1][refn1], n, square, height, delta, 1,
00638                     tmp_y, tmp_cb, tmp_cr,
00639                     x_offset, y_offset, qpix_put, chroma_put,
00640                     pixel_shift, chroma_idc);
00641 
00642         if(h->use_weight == 2){
00643             int weight0 = h->implicit_weight[refn0][refn1][s->mb_y&1];
00644             int weight1 = 64 - weight0;
00645             luma_weight_avg(  dest_y,  tmp_y,  h->  mb_linesize,
00646                               height,        5, weight0, weight1, 0);
00647             chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize,
00648                               chroma_height, 5, weight0, weight1, 0);
00649             chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize,
00650                               chroma_height, 5, weight0, weight1, 0);
00651         }else{
00652             luma_weight_avg(dest_y, tmp_y, h->mb_linesize, height, h->luma_log2_weight_denom,
00653                             h->luma_weight[refn0][0][0] , h->luma_weight[refn1][1][0],
00654                             h->luma_weight[refn0][0][1] + h->luma_weight[refn1][1][1]);
00655             chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, chroma_height, h->chroma_log2_weight_denom,
00656                             h->chroma_weight[refn0][0][0][0] , h->chroma_weight[refn1][1][0][0],
00657                             h->chroma_weight[refn0][0][0][1] + h->chroma_weight[refn1][1][0][1]);
00658             chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, chroma_height, h->chroma_log2_weight_denom,
00659                             h->chroma_weight[refn0][0][1][0] , h->chroma_weight[refn1][1][1][0],
00660                             h->chroma_weight[refn0][0][1][1] + h->chroma_weight[refn1][1][1][1]);
00661         }
00662     }else{
00663         int list = list1 ? 1 : 0;
00664         int refn = h->ref_cache[list][ scan8[n] ];
00665         Picture *ref= &h->ref_list[list][refn];
00666         mc_dir_part(h, ref, n, square, height, delta, list,
00667                     dest_y, dest_cb, dest_cr, x_offset, y_offset,
00668                     qpix_put, chroma_put, pixel_shift, chroma_idc);
00669 
00670         luma_weight_op(dest_y, h->mb_linesize, height, h->luma_log2_weight_denom,
00671                        h->luma_weight[refn][list][0], h->luma_weight[refn][list][1]);
00672         if(h->use_weight_chroma){
00673             chroma_weight_op(dest_cb, h->mb_uvlinesize, chroma_height, h->chroma_log2_weight_denom,
00674                              h->chroma_weight[refn][list][0][0], h->chroma_weight[refn][list][0][1]);
00675             chroma_weight_op(dest_cr, h->mb_uvlinesize, chroma_height, h->chroma_log2_weight_denom,
00676                              h->chroma_weight[refn][list][1][0], h->chroma_weight[refn][list][1][1]);
00677         }
00678     }
00679 }
00680 
00681 static av_always_inline void
00682 mc_part(H264Context *h, int n, int square, int height, int delta,
00683         uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00684         int x_offset, int y_offset,
00685         qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
00686         qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
00687         h264_weight_func *weight_op, h264_biweight_func *weight_avg,
00688         int list0, int list1, int pixel_shift, int chroma_idc)
00689 {
00690     if((h->use_weight==2 && list0 && list1
00691         && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ][h->s.mb_y&1] != 32))
00692        || h->use_weight==1)
00693         mc_part_weighted(h, n, square, height, delta, dest_y, dest_cb, dest_cr,
00694                          x_offset, y_offset, qpix_put, chroma_put,
00695                          weight_op[0], weight_op[1], weight_avg[0],
00696                          weight_avg[1], list0, list1, pixel_shift, chroma_idc);
00697     else
00698         mc_part_std(h, n, square, height, delta, dest_y, dest_cb, dest_cr,
00699                     x_offset, y_offset, qpix_put, chroma_put, qpix_avg,
00700                     chroma_avg, list0, list1, pixel_shift, chroma_idc);
00701 }
00702 
00703 static av_always_inline void
00704 prefetch_motion(H264Context *h, int list, int pixel_shift, int chroma_idc)
00705 {
00706     /* fetch pixels for estimated mv 4 macroblocks ahead
00707      * optimized for 64byte cache lines */
00708     MpegEncContext * const s = &h->s;
00709     const int refn = h->ref_cache[list][scan8[0]];
00710     if(refn >= 0){
00711         const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
00712         const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
00713         uint8_t **src = h->ref_list[list][refn].f.data;
00714         int off= (mx << pixel_shift) + (my + (s->mb_x&3)*4)*h->mb_linesize + (64 << pixel_shift);
00715         s->dsp.prefetch(src[0]+off, s->linesize, 4);
00716         if (chroma_idc == 3 /* yuv444 */) {
00717             s->dsp.prefetch(src[1]+off, s->linesize, 4);
00718             s->dsp.prefetch(src[2]+off, s->linesize, 4);
00719         }else{
00720             off= (((mx>>1)+64)<<pixel_shift) + ((my>>1) + (s->mb_x&7))*s->uvlinesize;
00721             s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
00722         }
00723     }
00724 }
00725 
00726 static av_always_inline void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00727                       qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
00728                       qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
00729                       h264_weight_func *weight_op, h264_biweight_func *weight_avg,
00730                       int pixel_shift, int chroma_idc)
00731 {
00732     MpegEncContext * const s = &h->s;
00733     const int mb_xy= h->mb_xy;
00734     const int mb_type = s->current_picture.f.mb_type[mb_xy];
00735 
00736     assert(IS_INTER(mb_type));
00737 
00738     if(HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
00739         await_references(h);
00740     prefetch_motion(h, 0, pixel_shift, chroma_idc);
00741 
00742     if(IS_16X16(mb_type)){
00743         mc_part(h, 0, 1, 16, 0, dest_y, dest_cb, dest_cr, 0, 0,
00744                 qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
00745                 weight_op, weight_avg,
00746                 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
00747                 pixel_shift, chroma_idc);
00748     }else if(IS_16X8(mb_type)){
00749         mc_part(h, 0, 0, 8, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 0,
00750                 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
00751                 weight_op, weight_avg,
00752                 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
00753                 pixel_shift, chroma_idc);
00754         mc_part(h, 8, 0, 8, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 4,
00755                 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
00756                 weight_op, weight_avg,
00757                 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),
00758                 pixel_shift, chroma_idc);
00759     }else if(IS_8X16(mb_type)){
00760         mc_part(h, 0, 0, 16, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
00761                 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
00762                 &weight_op[1], &weight_avg[1],
00763                 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
00764                 pixel_shift, chroma_idc);
00765         mc_part(h, 4, 0, 16, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
00766                 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
00767                 &weight_op[1], &weight_avg[1],
00768                 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),
00769                 pixel_shift, chroma_idc);
00770     }else{
00771         int i;
00772 
00773         assert(IS_8X8(mb_type));
00774 
00775         for(i=0; i<4; i++){
00776             const int sub_mb_type= h->sub_mb_type[i];
00777             const int n= 4*i;
00778             int x_offset= (i&1)<<2;
00779             int y_offset= (i&2)<<1;
00780 
00781             if(IS_SUB_8X8(sub_mb_type)){
00782                 mc_part(h, n, 1, 8, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
00783                     qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
00784                     &weight_op[1], &weight_avg[1],
00785                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
00786                     pixel_shift, chroma_idc);
00787             }else if(IS_SUB_8X4(sub_mb_type)){
00788                 mc_part(h, n  , 0, 4, 4 << pixel_shift, dest_y, dest_cb, dest_cr, x_offset, y_offset,
00789                     qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
00790                     &weight_op[1], &weight_avg[1],
00791                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
00792                     pixel_shift, chroma_idc);
00793                 mc_part(h, n+2, 0, 4, 4 << pixel_shift, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
00794                     qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
00795                     &weight_op[1], &weight_avg[1],
00796                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
00797                     pixel_shift, chroma_idc);
00798             }else if(IS_SUB_4X8(sub_mb_type)){
00799                 mc_part(h, n  , 0, 8, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
00800                     qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
00801                     &weight_op[2], &weight_avg[2],
00802                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
00803                     pixel_shift, chroma_idc);
00804                 mc_part(h, n+1, 0, 8, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
00805                     qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
00806                     &weight_op[2], &weight_avg[2],
00807                     IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
00808                     pixel_shift, chroma_idc);
00809             }else{
00810                 int j;
00811                 assert(IS_SUB_4X4(sub_mb_type));
00812                 for(j=0; j<4; j++){
00813                     int sub_x_offset= x_offset + 2*(j&1);
00814                     int sub_y_offset= y_offset +   (j&2);
00815                     mc_part(h, n+j, 1, 4, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
00816                         qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
00817                         &weight_op[2], &weight_avg[2],
00818                         IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
00819                         pixel_shift, chroma_idc);
00820                 }
00821             }
00822         }
00823     }
00824 
00825     prefetch_motion(h, 1, pixel_shift, chroma_idc);
00826 }
00827 
00828 static av_always_inline void
00829 hl_motion_420(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00830               qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
00831               qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
00832               h264_weight_func *weight_op, h264_biweight_func *weight_avg,
00833               int pixel_shift)
00834 {
00835     hl_motion(h, dest_y, dest_cb, dest_cr, qpix_put, chroma_put,
00836               qpix_avg, chroma_avg, weight_op, weight_avg, pixel_shift, 1);
00837 }
00838 
00839 static av_always_inline void
00840 hl_motion_422(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00841               qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
00842               qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
00843               h264_weight_func *weight_op, h264_biweight_func *weight_avg,
00844               int pixel_shift)
00845 {
00846     hl_motion(h, dest_y, dest_cb, dest_cr, qpix_put, chroma_put,
00847               qpix_avg, chroma_avg, weight_op, weight_avg, pixel_shift, 2);
00848 }
00849 
00850 static void free_tables(H264Context *h, int free_rbsp){
00851     int i;
00852     H264Context *hx;
00853 
00854     av_freep(&h->intra4x4_pred_mode);
00855     av_freep(&h->chroma_pred_mode_table);
00856     av_freep(&h->cbp_table);
00857     av_freep(&h->mvd_table[0]);
00858     av_freep(&h->mvd_table[1]);
00859     av_freep(&h->direct_table);
00860     av_freep(&h->non_zero_count);
00861     av_freep(&h->slice_table_base);
00862     h->slice_table= NULL;
00863     av_freep(&h->list_counts);
00864 
00865     av_freep(&h->mb2b_xy);
00866     av_freep(&h->mb2br_xy);
00867 
00868     for(i = 0; i < MAX_THREADS; i++) {
00869         hx = h->thread_context[i];
00870         if(!hx) continue;
00871         av_freep(&hx->top_borders[1]);
00872         av_freep(&hx->top_borders[0]);
00873         av_freep(&hx->s.obmc_scratchpad);
00874         if (free_rbsp){
00875             av_freep(&hx->rbsp_buffer[1]);
00876             av_freep(&hx->rbsp_buffer[0]);
00877             hx->rbsp_buffer_size[0] = 0;
00878             hx->rbsp_buffer_size[1] = 0;
00879         }
00880         if (i) av_freep(&h->thread_context[i]);
00881     }
00882 }
00883 
00884 static void init_dequant8_coeff_table(H264Context *h){
00885     int i,j,q,x;
00886     const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);
00887 
00888     for(i=0; i<6; i++ ){
00889         h->dequant8_coeff[i] = h->dequant8_buffer[i];
00890         for(j=0; j<i; j++){
00891             if(!memcmp(h->pps.scaling_matrix8[j], h->pps.scaling_matrix8[i], 64*sizeof(uint8_t))){
00892                 h->dequant8_coeff[i] = h->dequant8_buffer[j];
00893                 break;
00894             }
00895         }
00896         if(j<i)
00897             continue;
00898 
00899         for(q=0; q<max_qp+1; q++){
00900             int shift = div6[q];
00901             int idx = rem6[q];
00902             for(x=0; x<64; x++)
00903                 h->dequant8_coeff[i][q][(x>>3)|((x&7)<<3)] =
00904                     ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
00905                     h->pps.scaling_matrix8[i][x]) << shift;
00906         }
00907     }
00908 }
00909 
00910 static void init_dequant4_coeff_table(H264Context *h){
00911     int i,j,q,x;
00912     const int max_qp = 51 + 6*(h->sps.bit_depth_luma-8);
00913     for(i=0; i<6; i++ ){
00914         h->dequant4_coeff[i] = h->dequant4_buffer[i];
00915         for(j=0; j<i; j++){
00916             if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
00917                 h->dequant4_coeff[i] = h->dequant4_buffer[j];
00918                 break;
00919             }
00920         }
00921         if(j<i)
00922             continue;
00923 
00924         for(q=0; q<max_qp+1; q++){
00925             int shift = div6[q] + 2;
00926             int idx = rem6[q];
00927             for(x=0; x<16; x++)
00928                 h->dequant4_coeff[i][q][(x>>2)|((x<<2)&0xF)] =
00929                     ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
00930                     h->pps.scaling_matrix4[i][x]) << shift;
00931         }
00932     }
00933 }
00934 
00935 static void init_dequant_tables(H264Context *h){
00936     int i,x;
00937     init_dequant4_coeff_table(h);
00938     if(h->pps.transform_8x8_mode)
00939         init_dequant8_coeff_table(h);
00940     if(h->sps.transform_bypass){
00941         for(i=0; i<6; i++)
00942             for(x=0; x<16; x++)
00943                 h->dequant4_coeff[i][0][x] = 1<<6;
00944         if(h->pps.transform_8x8_mode)
00945             for(i=0; i<6; i++)
00946                 for(x=0; x<64; x++)
00947                     h->dequant8_coeff[i][0][x] = 1<<6;
00948     }
00949 }
00950 
00951 
00952 int ff_h264_alloc_tables(H264Context *h){
00953     MpegEncContext * const s = &h->s;
00954     const int big_mb_num= s->mb_stride * (s->mb_height+1);
00955     const int row_mb_num= 2*s->mb_stride*FFMAX(s->avctx->thread_count, 1);
00956     int x,y;
00957 
00958     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, row_mb_num * 8  * sizeof(uint8_t), fail)
00959 
00960     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count    , big_mb_num * 48 * sizeof(uint8_t), fail)
00961     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base  , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
00962     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
00963 
00964     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
00965     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 16*row_mb_num * sizeof(uint8_t), fail);
00966     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 16*row_mb_num * sizeof(uint8_t), fail);
00967     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 4*big_mb_num * sizeof(uint8_t) , fail);
00968     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->list_counts, big_mb_num * sizeof(uint8_t), fail)
00969 
00970     memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride)  * sizeof(*h->slice_table_base));
00971     h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
00972 
00973     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy  , big_mb_num * sizeof(uint32_t), fail);
00974     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2br_xy , big_mb_num * sizeof(uint32_t), fail);
00975     for(y=0; y<s->mb_height; y++){
00976         for(x=0; x<s->mb_width; x++){
00977             const int mb_xy= x + y*s->mb_stride;
00978             const int b_xy = 4*x + 4*y*h->b_stride;
00979 
00980             h->mb2b_xy [mb_xy]= b_xy;
00981             h->mb2br_xy[mb_xy]= 8*(FMO ? mb_xy : (mb_xy % (2*s->mb_stride)));
00982         }
00983     }
00984 
00985     s->obmc_scratchpad = NULL;
00986 
00987     if(!h->dequant4_coeff[0])
00988         init_dequant_tables(h);
00989 
00990     return 0;
00991 fail:
00992     free_tables(h, 1);
00993     return -1;
00994 }
00995 
00999 static void clone_tables(H264Context *dst, H264Context *src, int i){
01000     MpegEncContext * const s = &src->s;
01001     dst->intra4x4_pred_mode       = src->intra4x4_pred_mode + i*8*2*s->mb_stride;
01002     dst->non_zero_count           = src->non_zero_count;
01003     dst->slice_table              = src->slice_table;
01004     dst->cbp_table                = src->cbp_table;
01005     dst->mb2b_xy                  = src->mb2b_xy;
01006     dst->mb2br_xy                 = src->mb2br_xy;
01007     dst->chroma_pred_mode_table   = src->chroma_pred_mode_table;
01008     dst->mvd_table[0]             = src->mvd_table[0] + i*8*2*s->mb_stride;
01009     dst->mvd_table[1]             = src->mvd_table[1] + i*8*2*s->mb_stride;
01010     dst->direct_table             = src->direct_table;
01011     dst->list_counts              = src->list_counts;
01012 
01013     dst->s.obmc_scratchpad = NULL;
01014     ff_h264_pred_init(&dst->hpc, src->s.codec_id, src->sps.bit_depth_luma, src->sps.chroma_format_idc);
01015 }
01016 
01021 static int context_init(H264Context *h){
01022     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * 16*3 * sizeof(uint8_t)*2, fail)
01023     FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * 16*3 * sizeof(uint8_t)*2, fail)
01024 
01025     h->ref_cache[0][scan8[5 ]+1] = h->ref_cache[0][scan8[7 ]+1] = h->ref_cache[0][scan8[13]+1] =
01026     h->ref_cache[1][scan8[5 ]+1] = h->ref_cache[1][scan8[7 ]+1] = h->ref_cache[1][scan8[13]+1] = PART_NOT_AVAILABLE;
01027 
01028     return 0;
01029 fail:
01030     return -1; // free_tables will clean up for us
01031 }
01032 
01033 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size);
01034 
01035 static av_cold void common_init(H264Context *h){
01036     MpegEncContext * const s = &h->s;
01037 
01038     s->width = s->avctx->width;
01039     s->height = s->avctx->height;
01040     s->codec_id= s->avctx->codec->id;
01041 
01042     s->avctx->bits_per_raw_sample = 8;
01043     h->cur_chroma_format_idc = 1;
01044 
01045     ff_h264dsp_init(&h->h264dsp,
01046                     s->avctx->bits_per_raw_sample, h->cur_chroma_format_idc);
01047     ff_h264_pred_init(&h->hpc, s->codec_id,
01048                       s->avctx->bits_per_raw_sample, h->cur_chroma_format_idc);
01049 
01050     h->dequant_coeff_pps= -1;
01051     s->unrestricted_mv=1;
01052 
01053     s->dsp.dct_bits = 16;
01054     dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
01055 
01056     memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
01057     memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
01058 }
01059 
01060 int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size)
01061 {
01062     AVCodecContext *avctx = h->s.avctx;
01063 
01064     if(!buf || size <= 0)
01065         return -1;
01066 
01067     if(buf[0] == 1){
01068         int i, cnt, nalsize;
01069         const unsigned char *p = buf;
01070 
01071         h->is_avc = 1;
01072 
01073         if(size < 7) {
01074             av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
01075             return -1;
01076         }
01077         /* sps and pps in the avcC always have length coded with 2 bytes,
01078            so put a fake nal_length_size = 2 while parsing them */
01079         h->nal_length_size = 2;
01080         // Decode sps from avcC
01081         cnt = *(p+5) & 0x1f; // Number of sps
01082         p += 6;
01083         for (i = 0; i < cnt; i++) {
01084             nalsize = AV_RB16(p) + 2;
01085             if(nalsize > size - (p-buf))
01086                 return -1;
01087             if(decode_nal_units(h, p, nalsize) < 0) {
01088                 av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
01089                 return -1;
01090             }
01091             p += nalsize;
01092         }
01093         // Decode pps from avcC
01094         cnt = *(p++); // Number of pps
01095         for (i = 0; i < cnt; i++) {
01096             nalsize = AV_RB16(p) + 2;
01097             if(nalsize > size - (p-buf))
01098                 return -1;
01099             if (decode_nal_units(h, p, nalsize) < 0) {
01100                 av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
01101                 return -1;
01102             }
01103             p += nalsize;
01104         }
01105         // Now store right nal length size, that will be use to parse all other nals
01106         h->nal_length_size = (buf[4] & 0x03) + 1;
01107     } else {
01108         h->is_avc = 0;
01109         if(decode_nal_units(h, buf, size) < 0)
01110             return -1;
01111     }
01112     return 0;
01113 }
01114 
01115 av_cold int ff_h264_decode_init(AVCodecContext *avctx){
01116     H264Context *h= avctx->priv_data;
01117     MpegEncContext * const s = &h->s;
01118     int i;
01119 
01120     MPV_decode_defaults(s);
01121 
01122     s->avctx = avctx;
01123     common_init(h);
01124 
01125     s->out_format = FMT_H264;
01126     s->workaround_bugs= avctx->workaround_bugs;
01127 
01128     // set defaults
01129 //    s->decode_mb= ff_h263_decode_mb;
01130     s->quarter_sample = 1;
01131     if(!avctx->has_b_frames)
01132     s->low_delay= 1;
01133 
01134     avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
01135 
01136     ff_h264_decode_init_vlc();
01137 
01138     h->pixel_shift = 0;
01139     h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
01140 
01141     h->thread_context[0] = h;
01142     h->outputed_poc = h->next_outputed_poc = INT_MIN;
01143     for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
01144         h->last_pocs[i] = INT_MIN;
01145     h->prev_poc_msb= 1<<16;
01146     h->prev_frame_num= -1;
01147     h->x264_build = -1;
01148     ff_h264_reset_sei(h);
01149     if(avctx->codec_id == CODEC_ID_H264){
01150         if(avctx->ticks_per_frame == 1){
01151             s->avctx->time_base.den *=2;
01152         }
01153         avctx->ticks_per_frame = 2;
01154     }
01155 
01156     if(avctx->extradata_size > 0 && avctx->extradata &&
01157         ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size))
01158         return -1;
01159 
01160     if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames < h->sps.num_reorder_frames){
01161         s->avctx->has_b_frames = h->sps.num_reorder_frames;
01162         s->low_delay = 0;
01163     }
01164 
01165     return 0;
01166 }
01167 
01168 #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b)+(size))))
01169 static void copy_picture_range(Picture **to, Picture **from, int count, MpegEncContext *new_base, MpegEncContext *old_base)
01170 {
01171     int i;
01172 
01173     for (i=0; i<count; i++){
01174         assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
01175                 IN_RANGE(from[i], old_base->picture, sizeof(Picture) * old_base->picture_count) ||
01176                 !from[i]));
01177         to[i] = REBASE_PICTURE(from[i], new_base, old_base);
01178     }
01179 }
01180 
01181 static void copy_parameter_set(void **to, void **from, int count, int size)
01182 {
01183     int i;
01184 
01185     for (i=0; i<count; i++){
01186         if (to[i] && !from[i]) av_freep(&to[i]);
01187         else if (from[i] && !to[i]) to[i] = av_malloc(size);
01188 
01189         if (from[i]) memcpy(to[i], from[i], size);
01190     }
01191 }
01192 
01193 static int decode_init_thread_copy(AVCodecContext *avctx){
01194     H264Context *h= avctx->priv_data;
01195 
01196     if (!avctx->internal->is_copy)
01197         return 0;
01198     memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
01199     memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
01200 
01201     return 0;
01202 }
01203 
01204 #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
01205 static int decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src){
01206     H264Context *h= dst->priv_data, *h1= src->priv_data;
01207     MpegEncContext * const s = &h->s, * const s1 = &h1->s;
01208     int inited = s->context_initialized, err;
01209     int i;
01210 
01211     if(dst == src || !s1->context_initialized) return 0;
01212 
01213     err = ff_mpeg_update_thread_context(dst, src);
01214     if(err) return err;
01215 
01216     //FIXME handle width/height changing
01217     if(!inited){
01218         for(i = 0; i < MAX_SPS_COUNT; i++)
01219             av_freep(h->sps_buffers + i);
01220 
01221         for(i = 0; i < MAX_PPS_COUNT; i++)
01222             av_freep(h->pps_buffers + i);
01223 
01224         memcpy(&h->s + 1, &h1->s + 1, sizeof(H264Context) - sizeof(MpegEncContext)); //copy all fields after MpegEnc
01225         memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
01226         memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
01227         if (ff_h264_alloc_tables(h) < 0) {
01228             av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n");
01229             return AVERROR(ENOMEM);
01230         }
01231         context_init(h);
01232 
01233         for(i=0; i<2; i++){
01234             h->rbsp_buffer[i] = NULL;
01235             h->rbsp_buffer_size[i] = 0;
01236         }
01237 
01238         h->thread_context[0] = h;
01239 
01240         // frame_start may not be called for the next thread (if it's decoding a bottom field)
01241         // so this has to be allocated here
01242         h->s.obmc_scratchpad = av_malloc(16*6*s->linesize);
01243 
01244         s->dsp.clear_blocks(h->mb);
01245         s->dsp.clear_blocks(h->mb+(24*16<<h->pixel_shift));
01246     }
01247 
01248     //extradata/NAL handling
01249     h->is_avc          = h1->is_avc;
01250 
01251     //SPS/PPS
01252     copy_parameter_set((void**)h->sps_buffers, (void**)h1->sps_buffers, MAX_SPS_COUNT, sizeof(SPS));
01253     h->sps             = h1->sps;
01254     copy_parameter_set((void**)h->pps_buffers, (void**)h1->pps_buffers, MAX_PPS_COUNT, sizeof(PPS));
01255     h->pps             = h1->pps;
01256 
01257     //Dequantization matrices
01258     //FIXME these are big - can they be only copied when PPS changes?
01259     copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
01260 
01261     for(i=0; i<6; i++)
01262         h->dequant4_coeff[i] = h->dequant4_buffer[0] + (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
01263 
01264     for(i=0; i<6; i++)
01265         h->dequant8_coeff[i] = h->dequant8_buffer[0] + (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
01266 
01267     h->dequant_coeff_pps = h1->dequant_coeff_pps;
01268 
01269     //POC timing
01270     copy_fields(h, h1, poc_lsb, redundant_pic_count);
01271 
01272     //reference lists
01273     copy_fields(h, h1, ref_count, list_count);
01274     copy_fields(h, h1, ref_list,  intra_gb);
01275     copy_fields(h, h1, short_ref, cabac_init_idc);
01276 
01277     copy_picture_range(h->short_ref,   h1->short_ref,   32, s, s1);
01278     copy_picture_range(h->long_ref,    h1->long_ref,    32, s, s1);
01279     copy_picture_range(h->delayed_pic, h1->delayed_pic, MAX_DELAYED_PIC_COUNT+2, s, s1);
01280 
01281     h->last_slice_type = h1->last_slice_type;
01282     h->sync            = h1->sync;
01283 
01284     if(!s->current_picture_ptr) return 0;
01285 
01286     if(!s->dropable) {
01287         err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
01288         h->prev_poc_msb     = h->poc_msb;
01289         h->prev_poc_lsb     = h->poc_lsb;
01290     }
01291     h->prev_frame_num_offset= h->frame_num_offset;
01292     h->prev_frame_num       = h->frame_num;
01293     h->outputed_poc         = h->next_outputed_poc;
01294 
01295     return err;
01296 }
01297 
01298 int ff_h264_frame_start(H264Context *h){
01299     MpegEncContext * const s = &h->s;
01300     int i;
01301     const int pixel_shift = h->pixel_shift;
01302 
01303     if(MPV_frame_start(s, s->avctx) < 0)
01304         return -1;
01305     ff_er_frame_start(s);
01306     /*
01307      * MPV_frame_start uses pict_type to derive key_frame.
01308      * This is incorrect for H.264; IDR markings must be used.
01309      * Zero here; IDR markings per slice in frame or fields are ORed in later.
01310      * See decode_nal_units().
01311      */
01312     s->current_picture_ptr->f.key_frame = 0;
01313     s->current_picture_ptr->sync = 0;
01314     s->current_picture_ptr->mmco_reset= 0;
01315 
01316     assert(s->linesize && s->uvlinesize);
01317 
01318     for(i=0; i<16; i++){
01319         h->block_offset[i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
01320         h->block_offset[48+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
01321     }
01322     for(i=0; i<16; i++){
01323         h->block_offset[16+i]=
01324         h->block_offset[32+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
01325         h->block_offset[48+16+i]=
01326         h->block_offset[48+32+i]= (4*((scan8[i] - scan8[0])&7) << pixel_shift) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
01327     }
01328 
01329     /* can't be in alloc_tables because linesize isn't known there.
01330      * FIXME: redo bipred weight to not require extra buffer? */
01331     for(i = 0; i < s->slice_context_count; i++)
01332         if(h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
01333             h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*6*s->linesize);
01334 
01335     /* some macroblocks can be accessed before they're available in case of lost slices, mbaff or threading*/
01336     memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
01337 
01338 //    s->decode = (s->flags & CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.f.reference /*|| h->contains_intra*/ || 1;
01339 
01340     // We mark the current picture as non-reference after allocating it, so
01341     // that if we break out due to an error it can be released automatically
01342     // in the next MPV_frame_start().
01343     // SVQ3 as well as most other codecs have only last/next/current and thus
01344     // get released even with set reference, besides SVQ3 and others do not
01345     // mark frames as reference later "naturally".
01346     if(s->codec_id != CODEC_ID_SVQ3)
01347         s->current_picture_ptr->f.reference = 0;
01348 
01349     s->current_picture_ptr->field_poc[0]=
01350     s->current_picture_ptr->field_poc[1]= INT_MAX;
01351 
01352     h->next_output_pic = NULL;
01353 
01354     assert(s->current_picture_ptr->long_ref==0);
01355 
01356     return 0;
01357 }
01358 
01367 static void decode_postinit(H264Context *h, int setup_finished){
01368     MpegEncContext * const s = &h->s;
01369     Picture *out = s->current_picture_ptr;
01370     Picture *cur = s->current_picture_ptr;
01371     int i, pics, out_of_order, out_idx;
01372 
01373     s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_H264;
01374     s->current_picture_ptr->f.pict_type   = s->pict_type;
01375 
01376     if (h->next_output_pic) return;
01377 
01378     if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
01379         //FIXME: if we have two PAFF fields in one packet, we can't start the next thread here.
01380         //If we have one field per packet, we can. The check in decode_nal_units() is not good enough
01381         //to find this yet, so we assume the worst for now.
01382         //if (setup_finished)
01383         //    ff_thread_finish_setup(s->avctx);
01384         return;
01385     }
01386 
01387     cur->f.interlaced_frame = 0;
01388     cur->f.repeat_pict      = 0;
01389 
01390     /* Signal interlacing information externally. */
01391     /* Prioritize picture timing SEI information over used decoding process if it exists. */
01392 
01393     if(h->sps.pic_struct_present_flag){
01394         switch (h->sei_pic_struct)
01395         {
01396         case SEI_PIC_STRUCT_FRAME:
01397             break;
01398         case SEI_PIC_STRUCT_TOP_FIELD:
01399         case SEI_PIC_STRUCT_BOTTOM_FIELD:
01400             cur->f.interlaced_frame = 1;
01401             break;
01402         case SEI_PIC_STRUCT_TOP_BOTTOM:
01403         case SEI_PIC_STRUCT_BOTTOM_TOP:
01404             if (FIELD_OR_MBAFF_PICTURE)
01405                 cur->f.interlaced_frame = 1;
01406             else
01407                 // try to flag soft telecine progressive
01408                 cur->f.interlaced_frame = h->prev_interlaced_frame;
01409             break;
01410         case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
01411         case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
01412             // Signal the possibility of telecined film externally (pic_struct 5,6)
01413             // From these hints, let the applications decide if they apply deinterlacing.
01414             cur->f.repeat_pict = 1;
01415             break;
01416         case SEI_PIC_STRUCT_FRAME_DOUBLING:
01417             // Force progressive here, as doubling interlaced frame is a bad idea.
01418             cur->f.repeat_pict = 2;
01419             break;
01420         case SEI_PIC_STRUCT_FRAME_TRIPLING:
01421             cur->f.repeat_pict = 4;
01422             break;
01423         }
01424 
01425         if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
01426             cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
01427     }else{
01428         /* Derive interlacing flag from used decoding process. */
01429         cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE;
01430     }
01431     h->prev_interlaced_frame = cur->f.interlaced_frame;
01432 
01433     if (cur->field_poc[0] != cur->field_poc[1]){
01434         /* Derive top_field_first from field pocs. */
01435         cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
01436     }else{
01437         if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
01438             /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
01439             if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
01440               || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
01441                 cur->f.top_field_first = 1;
01442             else
01443                 cur->f.top_field_first = 0;
01444         }else{
01445             /* Most likely progressive */
01446             cur->f.top_field_first = 0;
01447         }
01448     }
01449 
01450     cur->mmco_reset = h->mmco_reset;
01451     h->mmco_reset = 0;
01452     //FIXME do something with unavailable reference frames
01453 
01454     /* Sort B-frames into display order */
01455 
01456     if(h->sps.bitstream_restriction_flag
01457        && s->avctx->has_b_frames < h->sps.num_reorder_frames){
01458         s->avctx->has_b_frames = h->sps.num_reorder_frames;
01459         s->low_delay = 0;
01460     }
01461 
01462     if(   s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
01463        && !h->sps.bitstream_restriction_flag){
01464         s->avctx->has_b_frames = MAX_DELAYED_PIC_COUNT - 1;
01465         s->low_delay= 0;
01466     }
01467 
01468     for (i = 0; 1; i++) {
01469         if(i == MAX_DELAYED_PIC_COUNT || cur->poc < h->last_pocs[i]){
01470             if(i)
01471                 h->last_pocs[i-1] = cur->poc;
01472             break;
01473         } else if(i) {
01474             h->last_pocs[i-1]= h->last_pocs[i];
01475         }
01476     }
01477     out_of_order = MAX_DELAYED_PIC_COUNT - i;
01478     if(   cur->f.pict_type == AV_PICTURE_TYPE_B
01479        || (h->last_pocs[MAX_DELAYED_PIC_COUNT-2] > INT_MIN && h->last_pocs[MAX_DELAYED_PIC_COUNT-1] - h->last_pocs[MAX_DELAYED_PIC_COUNT-2] > 2))
01480         out_of_order = FFMAX(out_of_order, 1);
01481     if(s->avctx->has_b_frames < out_of_order && !h->sps.bitstream_restriction_flag){
01482         av_log(s->avctx, AV_LOG_WARNING, "Increasing reorder buffer to %d\n", out_of_order);
01483         s->avctx->has_b_frames = out_of_order;
01484         s->low_delay = 0;
01485     }
01486 
01487     pics = 0;
01488     while(h->delayed_pic[pics]) pics++;
01489 
01490     av_assert0(pics <= MAX_DELAYED_PIC_COUNT);
01491 
01492     h->delayed_pic[pics++] = cur;
01493     if (cur->f.reference == 0)
01494         cur->f.reference = DELAYED_PIC_REF;
01495 
01496     out = h->delayed_pic[0];
01497     out_idx = 0;
01498     for (i = 1; h->delayed_pic[i] && !h->delayed_pic[i]->f.key_frame && !h->delayed_pic[i]->mmco_reset; i++)
01499         if(h->delayed_pic[i]->poc < out->poc){
01500             out = h->delayed_pic[i];
01501             out_idx = i;
01502         }
01503     if (s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset))
01504         h->next_outputed_poc= INT_MIN;
01505     out_of_order = out->poc < h->next_outputed_poc;
01506 
01507     if(out_of_order || pics > s->avctx->has_b_frames){
01508         out->f.reference &= ~DELAYED_PIC_REF;
01509         out->owner2 = s; // for frame threading, the owner must be the second field's thread
01510                          // or else the first thread can release the picture and reuse it unsafely
01511         for(i=out_idx; h->delayed_pic[i]; i++)
01512             h->delayed_pic[i] = h->delayed_pic[i+1];
01513     }
01514     if(!out_of_order && pics > s->avctx->has_b_frames){
01515         h->next_output_pic = out;
01516         if (out_idx == 0 && h->delayed_pic[0] && (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset)) {
01517             h->next_outputed_poc = INT_MIN;
01518         } else
01519             h->next_outputed_poc = out->poc;
01520     }else{
01521         av_log(s->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
01522     }
01523 
01524     if (h->next_output_pic && h->next_output_pic->sync) {
01525         h->sync |= 2;
01526     }
01527 
01528     if (setup_finished)
01529         ff_thread_finish_setup(s->avctx);
01530 }
01531 
01532 static av_always_inline void backup_mb_border(H264Context *h, uint8_t *src_y,
01533                                               uint8_t *src_cb, uint8_t *src_cr,
01534                                               int linesize, int uvlinesize, int simple)
01535 {
01536     MpegEncContext * const s = &h->s;
01537     uint8_t *top_border;
01538     int top_idx = 1;
01539     const int pixel_shift = h->pixel_shift;
01540     int chroma444 = CHROMA444;
01541     int chroma422 = CHROMA422;
01542 
01543     src_y  -=   linesize;
01544     src_cb -= uvlinesize;
01545     src_cr -= uvlinesize;
01546 
01547     if(!simple && FRAME_MBAFF){
01548         if(s->mb_y&1){
01549             if(!MB_MBAFF){
01550                 top_border = h->top_borders[0][s->mb_x];
01551                 AV_COPY128(top_border, src_y + 15*linesize);
01552                 if (pixel_shift)
01553                     AV_COPY128(top_border+16, src_y+15*linesize+16);
01554                 if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
01555                     if(chroma444){
01556                         if (pixel_shift){
01557                             AV_COPY128(top_border+32, src_cb + 15*uvlinesize);
01558                             AV_COPY128(top_border+48, src_cb + 15*uvlinesize+16);
01559                             AV_COPY128(top_border+64, src_cr + 15*uvlinesize);
01560                             AV_COPY128(top_border+80, src_cr + 15*uvlinesize+16);
01561                         } else {
01562                             AV_COPY128(top_border+16, src_cb + 15*uvlinesize);
01563                             AV_COPY128(top_border+32, src_cr + 15*uvlinesize);
01564                         }
01565                     } else if(chroma422){
01566                         if (pixel_shift) {
01567                             AV_COPY128(top_border+32, src_cb + 15*uvlinesize);
01568                             AV_COPY128(top_border+48, src_cr + 15*uvlinesize);
01569                         } else {
01570                             AV_COPY64(top_border+16, src_cb +  15*uvlinesize);
01571                             AV_COPY64(top_border+24, src_cr +  15*uvlinesize);
01572                         }
01573                     } else {
01574                         if (pixel_shift) {
01575                             AV_COPY128(top_border+32, src_cb+7*uvlinesize);
01576                             AV_COPY128(top_border+48, src_cr+7*uvlinesize);
01577                         } else {
01578                             AV_COPY64(top_border+16, src_cb+7*uvlinesize);
01579                             AV_COPY64(top_border+24, src_cr+7*uvlinesize);
01580                         }
01581                     }
01582                 }
01583             }
01584         }else if(MB_MBAFF){
01585             top_idx = 0;
01586         }else
01587             return;
01588     }
01589 
01590     top_border = h->top_borders[top_idx][s->mb_x];
01591     // There are two lines saved, the line above the the top macroblock of a pair,
01592     // and the line above the bottom macroblock
01593     AV_COPY128(top_border, src_y + 16*linesize);
01594     if (pixel_shift)
01595         AV_COPY128(top_border+16, src_y+16*linesize+16);
01596 
01597     if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
01598         if(chroma444){
01599             if (pixel_shift){
01600                 AV_COPY128(top_border+32, src_cb + 16*linesize);
01601                 AV_COPY128(top_border+48, src_cb + 16*linesize+16);
01602                 AV_COPY128(top_border+64, src_cr + 16*linesize);
01603                 AV_COPY128(top_border+80, src_cr + 16*linesize+16);
01604             } else {
01605                 AV_COPY128(top_border+16, src_cb + 16*linesize);
01606                 AV_COPY128(top_border+32, src_cr + 16*linesize);
01607             }
01608         } else if(chroma422) {
01609             if (pixel_shift) {
01610                 AV_COPY128(top_border+32, src_cb+16*uvlinesize);
01611                 AV_COPY128(top_border+48, src_cr+16*uvlinesize);
01612             } else {
01613                 AV_COPY64(top_border+16, src_cb+16*uvlinesize);
01614                 AV_COPY64(top_border+24, src_cr+16*uvlinesize);
01615             }
01616         } else {
01617             if (pixel_shift) {
01618                 AV_COPY128(top_border+32, src_cb+8*uvlinesize);
01619                 AV_COPY128(top_border+48, src_cr+8*uvlinesize);
01620             } else {
01621                 AV_COPY64(top_border+16, src_cb+8*uvlinesize);
01622                 AV_COPY64(top_border+24, src_cr+8*uvlinesize);
01623             }
01624         }
01625     }
01626 }
01627 
01628 static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
01629                                   uint8_t *src_cb, uint8_t *src_cr,
01630                                   int linesize, int uvlinesize,
01631                                   int xchg, int chroma444,
01632                                   int simple, int pixel_shift){
01633     MpegEncContext * const s = &h->s;
01634     int deblock_topleft;
01635     int deblock_top;
01636     int top_idx = 1;
01637     uint8_t *top_border_m1;
01638     uint8_t *top_border;
01639 
01640     if(!simple && FRAME_MBAFF){
01641         if(s->mb_y&1){
01642             if(!MB_MBAFF)
01643                 return;
01644         }else{
01645             top_idx = MB_MBAFF ? 0 : 1;
01646         }
01647     }
01648 
01649     if(h->deblocking_filter == 2) {
01650         deblock_topleft = h->slice_table[h->mb_xy - 1 - s->mb_stride] == h->slice_num;
01651         deblock_top     = h->top_type;
01652     } else {
01653         deblock_topleft = (s->mb_x > 0);
01654         deblock_top     = (s->mb_y > !!MB_FIELD);
01655     }
01656 
01657     src_y  -=   linesize + 1 + pixel_shift;
01658     src_cb -= uvlinesize + 1 + pixel_shift;
01659     src_cr -= uvlinesize + 1 + pixel_shift;
01660 
01661     top_border_m1 = h->top_borders[top_idx][s->mb_x-1];
01662     top_border    = h->top_borders[top_idx][s->mb_x];
01663 
01664 #define XCHG(a,b,xchg)\
01665     if (pixel_shift) {\
01666         if (xchg) {\
01667             AV_SWAP64(b+0,a+0);\
01668             AV_SWAP64(b+8,a+8);\
01669         } else {\
01670             AV_COPY128(b,a); \
01671         }\
01672     } else \
01673 if (xchg) AV_SWAP64(b,a);\
01674 else      AV_COPY64(b,a);
01675 
01676     if(deblock_top){
01677         if(deblock_topleft){
01678             XCHG(top_border_m1 + (8 << pixel_shift), src_y - (7 << pixel_shift), 1);
01679         }
01680         XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
01681         XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
01682         if(s->mb_x+1 < s->mb_width){
01683             XCHG(h->top_borders[top_idx][s->mb_x+1], src_y + (17 << pixel_shift), 1);
01684         }
01685     }
01686     if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
01687         if(chroma444){
01688             if(deblock_topleft){
01689                 XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
01690                 XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
01691             }
01692             XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
01693             XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
01694             XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
01695             XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
01696             if(s->mb_x+1 < s->mb_width){
01697                 XCHG(h->top_borders[top_idx][s->mb_x+1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
01698                 XCHG(h->top_borders[top_idx][s->mb_x+1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
01699             }
01700         } else {
01701             if(deblock_top){
01702                 if(deblock_topleft){
01703                     XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
01704                     XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
01705                 }
01706                 XCHG(top_border + (16 << pixel_shift), src_cb+1+pixel_shift, 1);
01707                 XCHG(top_border + (24 << pixel_shift), src_cr+1+pixel_shift, 1);
01708             }
01709         }
01710     }
01711 }
01712 
01713 static av_always_inline int dctcoef_get(DCTELEM *mb, int high_bit_depth, int index) {
01714     if (high_bit_depth) {
01715         return AV_RN32A(((int32_t*)mb) + index);
01716     } else
01717         return AV_RN16A(mb + index);
01718 }
01719 
01720 static av_always_inline void dctcoef_set(DCTELEM *mb, int high_bit_depth, int index, int value) {
01721     if (high_bit_depth) {
01722         AV_WN32A(((int32_t*)mb) + index, value);
01723     } else
01724         AV_WN16A(mb + index, value);
01725 }
01726 
01727 static av_always_inline void hl_decode_mb_predict_luma(H264Context *h, int mb_type, int is_h264, int simple, int transform_bypass,
01728                                                        int pixel_shift, int *block_offset, int linesize, uint8_t *dest_y, int p)
01729 {
01730     MpegEncContext * const s = &h->s;
01731     void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
01732     void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
01733     int i;
01734     int qscale = p == 0 ? s->qscale : h->chroma_qp[p-1];
01735     block_offset += 16*p;
01736     if(IS_INTRA4x4(mb_type)){
01737         if(simple || !s->encoding){
01738             if(IS_8x8DCT(mb_type)){
01739                 if(transform_bypass){
01740                     idct_dc_add =
01741                     idct_add    = s->dsp.add_pixels8;
01742                 }else{
01743                     idct_dc_add = h->h264dsp.h264_idct8_dc_add;
01744                     idct_add    = h->h264dsp.h264_idct8_add;
01745                 }
01746                 for(i=0; i<16; i+=4){
01747                     uint8_t * const ptr= dest_y + block_offset[i];
01748                     const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
01749                     if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
01750                         h->hpc.pred8x8l_add[dir](ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
01751                     }else{
01752                         const int nnz = h->non_zero_count_cache[ scan8[i+p*16] ];
01753                         h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
01754                                                     (h->topright_samples_available<<i)&0x4000, linesize);
01755                         if(nnz){
01756                             if(nnz == 1 && dctcoef_get(h->mb, pixel_shift, i*16+p*256))
01757                                 idct_dc_add(ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
01758                             else
01759                                 idct_add   (ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
01760                         }
01761                     }
01762                 }
01763             }else{
01764                 if(transform_bypass){
01765                     idct_dc_add =
01766                     idct_add    = s->dsp.add_pixels4;
01767                 }else{
01768                     idct_dc_add = h->h264dsp.h264_idct_dc_add;
01769                     idct_add    = h->h264dsp.h264_idct_add;
01770                 }
01771                 for(i=0; i<16; i++){
01772                     uint8_t * const ptr= dest_y + block_offset[i];
01773                     const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
01774 
01775                     if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
01776                         h->hpc.pred4x4_add[dir](ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
01777                     }else{
01778                         uint8_t *topright;
01779                         int nnz, tr;
01780                         uint64_t tr_high;
01781                         if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
01782                             const int topright_avail= (h->topright_samples_available<<i)&0x8000;
01783                             assert(s->mb_y || linesize <= block_offset[i]);
01784                             if(!topright_avail){
01785                                 if (pixel_shift) {
01786                                     tr_high= ((uint16_t*)ptr)[3 - linesize/2]*0x0001000100010001ULL;
01787                                     topright= (uint8_t*) &tr_high;
01788                                 } else {
01789                                     tr= ptr[3 - linesize]*0x01010101u;
01790                                     topright= (uint8_t*) &tr;
01791                                 }
01792                             }else
01793                                 topright= ptr + (4 << pixel_shift) - linesize;
01794                         }else
01795                             topright= NULL;
01796 
01797                         h->hpc.pred4x4[ dir ](ptr, topright, linesize);
01798                         nnz = h->non_zero_count_cache[ scan8[i+p*16] ];
01799                         if(nnz){
01800                             if(is_h264){
01801                                 if(nnz == 1 && dctcoef_get(h->mb, pixel_shift, i*16+p*256))
01802                                     idct_dc_add(ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
01803                                 else
01804                                     idct_add   (ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
01805                             } else if (CONFIG_SVQ3_DECODER)
01806                                 ff_svq3_add_idct_c(ptr, h->mb + i*16+p*256, linesize, qscale, 0);
01807                         }
01808                     }
01809                 }
01810             }
01811         }
01812     }else{
01813         h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
01814         if(is_h264){
01815             if(h->non_zero_count_cache[ scan8[LUMA_DC_BLOCK_INDEX+p] ]){
01816                 if(!transform_bypass)
01817                     h->h264dsp.h264_luma_dc_dequant_idct(h->mb+(p*256 << pixel_shift), h->mb_luma_dc[p], h->dequant4_coeff[p][qscale][0]);
01818                 else{
01819                     static const uint8_t dc_mapping[16] = { 0*16, 1*16, 4*16, 5*16, 2*16, 3*16, 6*16, 7*16,
01820                                                             8*16, 9*16,12*16,13*16,10*16,11*16,14*16,15*16};
01821                     for(i = 0; i < 16; i++)
01822                         dctcoef_set(h->mb+(p*256 << pixel_shift), pixel_shift, dc_mapping[i], dctcoef_get(h->mb_luma_dc[p], pixel_shift, i));
01823                 }
01824             }
01825         } else if (CONFIG_SVQ3_DECODER)
01826             ff_svq3_luma_dc_dequant_idct_c(h->mb+p*256, h->mb_luma_dc[p], qscale);
01827     }
01828 }
01829 
01830 static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type, int is_h264, int simple, int transform_bypass,
01831                                                     int pixel_shift, int *block_offset, int linesize, uint8_t *dest_y, int p)
01832 {
01833     MpegEncContext * const s = &h->s;
01834     void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
01835     int i;
01836     block_offset += 16*p;
01837     if(!IS_INTRA4x4(mb_type)){
01838         if(is_h264){
01839             if(IS_INTRA16x16(mb_type)){
01840                 if(transform_bypass){
01841                     if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
01842                         h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize);
01843                     }else{
01844                         for(i=0; i<16; i++){
01845                             if(h->non_zero_count_cache[ scan8[i+p*16] ] || dctcoef_get(h->mb, pixel_shift, i*16+p*256))
01846                                 s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + (i*16+p*256 << pixel_shift), linesize);
01847                         }
01848                     }
01849                 }else{
01850                     h->h264dsp.h264_idct_add16intra(dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize, h->non_zero_count_cache+p*5*8);
01851                 }
01852             }else if(h->cbp&15){
01853                 if(transform_bypass){
01854                     const int di = IS_8x8DCT(mb_type) ? 4 : 1;
01855                     idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
01856                     for(i=0; i<16; i+=di){
01857                         if(h->non_zero_count_cache[ scan8[i+p*16] ]){
01858                             idct_add(dest_y + block_offset[i], h->mb + (i*16+p*256 << pixel_shift), linesize);
01859                         }
01860                     }
01861                 }else{
01862                     if(IS_8x8DCT(mb_type)){
01863                         h->h264dsp.h264_idct8_add4(dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize, h->non_zero_count_cache+p*5*8);
01864                     }else{
01865                         h->h264dsp.h264_idct_add16(dest_y, block_offset, h->mb + (p*256 << pixel_shift), linesize, h->non_zero_count_cache+p*5*8);
01866                     }
01867                 }
01868             }
01869         } else if (CONFIG_SVQ3_DECODER) {
01870             for(i=0; i<16; i++){
01871                 if(h->non_zero_count_cache[ scan8[i+p*16] ] || h->mb[i*16+p*256]){ //FIXME benchmark weird rule, & below
01872                     uint8_t * const ptr= dest_y + block_offset[i];
01873                     ff_svq3_add_idct_c(ptr, h->mb + i*16 + p*256, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
01874                 }
01875             }
01876         }
01877     }
01878 }
01879 
01880 static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple, int pixel_shift)
01881 {
01882     MpegEncContext * const s = &h->s;
01883     const int mb_x= s->mb_x;
01884     const int mb_y= s->mb_y;
01885     const int mb_xy= h->mb_xy;
01886     const int mb_type = s->current_picture.f.mb_type[mb_xy];
01887     uint8_t  *dest_y, *dest_cb, *dest_cr;
01888     int linesize, uvlinesize /*dct_offset*/;
01889     int i, j;
01890     int *block_offset = &h->block_offset[0];
01891     const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
01892     /* is_h264 should always be true if SVQ3 is disabled. */
01893     const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
01894     void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
01895     const int block_h = 16 >> s->chroma_y_shift;
01896     const int chroma422 = CHROMA422;
01897 
01898     dest_y  = s->current_picture.f.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize  ) * 16;
01899     dest_cb = s->current_picture.f.data[1] + (mb_x << pixel_shift)*8 + mb_y * s->uvlinesize * block_h;
01900     dest_cr = s->current_picture.f.data[2] + (mb_x << pixel_shift)*8 + mb_y * s->uvlinesize * block_h;
01901 
01902     s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + (64 << pixel_shift), s->linesize, 4);
01903     s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + (64 << pixel_shift), dest_cr - dest_cb, 2);
01904 
01905     h->list_counts[mb_xy]= h->list_count;
01906 
01907     if (!simple && MB_FIELD) {
01908         linesize   = h->mb_linesize   = s->linesize * 2;
01909         uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
01910         block_offset = &h->block_offset[48];
01911         if(mb_y&1){ //FIXME move out of this function?
01912             dest_y -= s->linesize*15;
01913             dest_cb-= s->uvlinesize * (block_h - 1);
01914             dest_cr-= s->uvlinesize * (block_h - 1);
01915         }
01916         if(FRAME_MBAFF) {
01917             int list;
01918             for(list=0; list<h->list_count; list++){
01919                 if(!USES_LIST(mb_type, list))
01920                     continue;
01921                 if(IS_16X16(mb_type)){
01922                     int8_t *ref = &h->ref_cache[list][scan8[0]];
01923                     fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
01924                 }else{
01925                     for(i=0; i<16; i+=4){
01926                         int ref = h->ref_cache[list][scan8[i]];
01927                         if(ref >= 0)
01928                             fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
01929                     }
01930                 }
01931             }
01932         }
01933     } else {
01934         linesize   = h->mb_linesize   = s->linesize;
01935         uvlinesize = h->mb_uvlinesize = s->uvlinesize;
01936 //        dct_offset = s->linesize * 16;
01937     }
01938 
01939     if (!simple && IS_INTRA_PCM(mb_type)) {
01940         const int bit_depth = h->sps.bit_depth_luma;
01941         if (pixel_shift) {
01942             int j;
01943             GetBitContext gb;
01944             init_get_bits(&gb, (uint8_t*)h->mb, 384*bit_depth);
01945 
01946             for (i = 0; i < 16; i++) {
01947                 uint16_t *tmp_y  = (uint16_t*)(dest_y  + i*linesize);
01948                 for (j = 0; j < 16; j++)
01949                     tmp_y[j] = get_bits(&gb, bit_depth);
01950             }
01951             if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
01952                 if (!h->sps.chroma_format_idc) {
01953                     for (i = 0; i < block_h; i++) {
01954                         uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
01955                         uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
01956                         for (j = 0; j < 8; j++) {
01957                             tmp_cb[j] = tmp_cr[j] = 1 << (bit_depth - 1);
01958                         }
01959                     }
01960                 } else {
01961                     for (i = 0; i < block_h; i++) {
01962                         uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
01963                         for (j = 0; j < 8; j++)
01964                             tmp_cb[j] = get_bits(&gb, bit_depth);
01965                     }
01966                     for (i = 0; i < block_h; i++) {
01967                         uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
01968                         for (j = 0; j < 8; j++)
01969                             tmp_cr[j] = get_bits(&gb, bit_depth);
01970                     }
01971                 }
01972             }
01973         } else {
01974             for (i=0; i<16; i++) {
01975                 memcpy(dest_y + i*  linesize, h->mb       + i*8, 16);
01976             }
01977             if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
01978                 if (!h->sps.chroma_format_idc) {
01979                     for (i=0; i<8; i++) {
01980                         memset(dest_cb + i*uvlinesize, 1 << (bit_depth - 1), 8);
01981                         memset(dest_cr + i*uvlinesize, 1 << (bit_depth - 1), 8);
01982                     }
01983                 } else {
01984                     for (i=0; i<block_h; i++) {
01985                         memcpy(dest_cb + i*uvlinesize, h->mb + 128 + i*4,  8);
01986                         memcpy(dest_cr + i*uvlinesize, h->mb + 160 + i*4,  8);
01987                     }
01988                 }
01989             }
01990         }
01991     } else {
01992         if(IS_INTRA(mb_type)){
01993             if(h->deblocking_filter)
01994                 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, 0, simple, pixel_shift);
01995 
01996             if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
01997                 h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
01998                 h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
01999             }
02000 
02001             hl_decode_mb_predict_luma(h, mb_type, is_h264, simple, transform_bypass, pixel_shift, block_offset, linesize, dest_y, 0);
02002 
02003             if(h->deblocking_filter)
02004                 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, 0, simple, pixel_shift);
02005         }else if(is_h264){
02006             if (chroma422) {
02007                 hl_motion_422(h, dest_y, dest_cb, dest_cr,
02008                               s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
02009                               s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
02010                               h->h264dsp.weight_h264_pixels_tab,
02011                               h->h264dsp.biweight_h264_pixels_tab,
02012                               pixel_shift);
02013             } else {
02014                 hl_motion_420(h, dest_y, dest_cb, dest_cr,
02015                               s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
02016                               s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
02017                               h->h264dsp.weight_h264_pixels_tab,
02018                               h->h264dsp.biweight_h264_pixels_tab,
02019                               pixel_shift);
02020             }
02021         }
02022 
02023         hl_decode_mb_idct_luma(h, mb_type, is_h264, simple, transform_bypass, pixel_shift, block_offset, linesize, dest_y, 0);
02024 
02025         if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
02026             uint8_t *dest[2] = {dest_cb, dest_cr};
02027             if(transform_bypass){
02028                 if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
02029                     h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + (16*16*1 << pixel_shift), uvlinesize);
02030                     h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 32, h->mb + (16*16*2 << pixel_shift), uvlinesize);
02031                 }else{
02032                     idct_add = s->dsp.add_pixels4;
02033                     for(j=1; j<3; j++){
02034                         for(i=j*16; i<j*16+4; i++){
02035                             if(h->non_zero_count_cache[ scan8[i] ] || dctcoef_get(h->mb, pixel_shift, i*16))
02036                                 idct_add   (dest[j-1] + block_offset[i], h->mb + (i*16 << pixel_shift), uvlinesize);
02037                         }
02038                         if (chroma422) {
02039                             for(i=j*16+4; i<j*16+8; i++){
02040                                 if(h->non_zero_count_cache[ scan8[i+4] ] || dctcoef_get(h->mb, pixel_shift, i*16))
02041                                     idct_add   (dest[j-1] + block_offset[i+4], h->mb + (i*16 << pixel_shift), uvlinesize);
02042                             }
02043                         }
02044                     }
02045                 }
02046             }else{
02047                 if(is_h264){
02048                     int qp[2];
02049                     if (chroma422) {
02050                         qp[0] = h->chroma_qp[0] + 3;
02051                         qp[1] = h->chroma_qp[1] + 3;
02052                     } else {
02053                         qp[0] = h->chroma_qp[0];
02054                         qp[1] = h->chroma_qp[1];
02055                     }
02056                     if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+0] ])
02057                         h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16*16*1 << pixel_shift), h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][qp[0]][0]);
02058                     if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+1] ])
02059                         h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16*16*2 << pixel_shift), h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][qp[1]][0]);
02060                     h->h264dsp.h264_idct_add8(dest, block_offset,
02061                                               h->mb, uvlinesize,
02062                                               h->non_zero_count_cache);
02063                 } else if (CONFIG_SVQ3_DECODER) {
02064                     h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16*1, h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
02065                     h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16*2, h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
02066                     for(j=1; j<3; j++){
02067                         for(i=j*16; i<j*16+4; i++){
02068                             if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
02069                                 uint8_t * const ptr= dest[j-1] + block_offset[i];
02070                                 ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
02071                             }
02072                         }
02073                     }
02074                 }
02075             }
02076         }
02077     }
02078     if(h->cbp || IS_INTRA(mb_type))
02079     {
02080         s->dsp.clear_blocks(h->mb);
02081         s->dsp.clear_blocks(h->mb+(24*16<<pixel_shift));
02082     }
02083 }
02084 
02085 static av_always_inline void hl_decode_mb_444_internal(H264Context *h, int simple, int pixel_shift){
02086     MpegEncContext * const s = &h->s;
02087     const int mb_x= s->mb_x;
02088     const int mb_y= s->mb_y;
02089     const int mb_xy= h->mb_xy;
02090     const int mb_type = s->current_picture.f.mb_type[mb_xy];
02091     uint8_t  *dest[3];
02092     int linesize;
02093     int i, j, p;
02094     int *block_offset = &h->block_offset[0];
02095     const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
02096     const int plane_count = (simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) ? 3 : 1;
02097 
02098     for (p = 0; p < plane_count; p++)
02099     {
02100         dest[p] = s->current_picture.f.data[p] + ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;
02101         s->dsp.prefetch(dest[p] + (s->mb_x&3)*4*s->linesize + (64 << pixel_shift), s->linesize, 4);
02102     }
02103 
02104     h->list_counts[mb_xy]= h->list_count;
02105 
02106     if (!simple && MB_FIELD) {
02107         linesize   = h->mb_linesize = h->mb_uvlinesize = s->linesize * 2;
02108         block_offset = &h->block_offset[48];
02109         if(mb_y&1) //FIXME move out of this function?
02110             for (p = 0; p < 3; p++)
02111                 dest[p] -= s->linesize*15;
02112         if(FRAME_MBAFF) {
02113             int list;
02114             for(list=0; list<h->list_count; list++){
02115                 if(!USES_LIST(mb_type, list))
02116                     continue;
02117                 if(IS_16X16(mb_type)){
02118                     int8_t *ref = &h->ref_cache[list][scan8[0]];
02119                     fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
02120                 }else{
02121                     for(i=0; i<16; i+=4){
02122                         int ref = h->ref_cache[list][scan8[i]];
02123                         if(ref >= 0)
02124                             fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
02125                     }
02126                 }
02127             }
02128         }
02129     } else {
02130         linesize   = h->mb_linesize = h->mb_uvlinesize = s->linesize;
02131     }
02132 
02133     if (!simple && IS_INTRA_PCM(mb_type)) {
02134         if (pixel_shift) {
02135             const int bit_depth = h->sps.bit_depth_luma;
02136             GetBitContext gb;
02137             init_get_bits(&gb, (uint8_t*)h->mb, 768*bit_depth);
02138 
02139             for (p = 0; p < plane_count; p++) {
02140                 for (i = 0; i < 16; i++) {
02141                     uint16_t *tmp = (uint16_t*)(dest[p] + i*linesize);
02142                     for (j = 0; j < 16; j++)
02143                         tmp[j] = get_bits(&gb, bit_depth);
02144                 }
02145             }
02146         } else {
02147             for (p = 0; p < plane_count; p++) {
02148                 for (i = 0; i < 16; i++) {
02149                     memcpy(dest[p] + i*linesize, h->mb + p*128 + i*8, 16);
02150                 }
02151             }
02152         }
02153     } else {
02154         if(IS_INTRA(mb_type)){
02155             if(h->deblocking_filter)
02156                 xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 1, 1, simple, pixel_shift);
02157 
02158             for (p = 0; p < plane_count; p++)
02159                 hl_decode_mb_predict_luma(h, mb_type, 1, simple, transform_bypass, pixel_shift, block_offset, linesize, dest[p], p);
02160 
02161             if(h->deblocking_filter)
02162                 xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 0, 1, simple, pixel_shift);
02163         }else{
02164             hl_motion(h, dest[0], dest[1], dest[2],
02165                       s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
02166                       s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
02167                       h->h264dsp.weight_h264_pixels_tab,
02168                       h->h264dsp.biweight_h264_pixels_tab, pixel_shift, 3);
02169         }
02170 
02171         for (p = 0; p < plane_count; p++)
02172             hl_decode_mb_idct_luma(h, mb_type, 1, simple, transform_bypass, pixel_shift, block_offset, linesize, dest[p], p);
02173     }
02174     if(h->cbp || IS_INTRA(mb_type))
02175     {
02176         s->dsp.clear_blocks(h->mb);
02177         s->dsp.clear_blocks(h->mb+(24*16<<pixel_shift));
02178     }
02179 }
02180 
02184 #define hl_decode_mb_simple(sh, bits) \
02185 static void hl_decode_mb_simple_ ## bits(H264Context *h){ \
02186     hl_decode_mb_internal(h, 1, sh); \
02187 }
02188 hl_decode_mb_simple(0, 8)
02189 hl_decode_mb_simple(1, 16)
02190 
02194 static void av_noinline hl_decode_mb_complex(H264Context *h){
02195     hl_decode_mb_internal(h, 0, h->pixel_shift);
02196 }
02197 
02198 static void av_noinline hl_decode_mb_444_complex(H264Context *h){
02199     hl_decode_mb_444_internal(h, 0, h->pixel_shift);
02200 }
02201 
02202 static void av_noinline hl_decode_mb_444_simple(H264Context *h){
02203     hl_decode_mb_444_internal(h, 1, 0);
02204 }
02205 
02206 void ff_h264_hl_decode_mb(H264Context *h){
02207     MpegEncContext * const s = &h->s;
02208     const int mb_xy= h->mb_xy;
02209     const int mb_type = s->current_picture.f.mb_type[mb_xy];
02210     int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
02211 
02212     if (CHROMA444) {
02213         if(is_complex || h->pixel_shift)
02214             hl_decode_mb_444_complex(h);
02215         else
02216             hl_decode_mb_444_simple(h);
02217     } else if (is_complex) {
02218         hl_decode_mb_complex(h);
02219     } else if (h->pixel_shift) {
02220         hl_decode_mb_simple_16(h);
02221     } else
02222         hl_decode_mb_simple_8(h);
02223 }
02224 
02225 static int pred_weight_table(H264Context *h){
02226     MpegEncContext * const s = &h->s;
02227     int list, i;
02228     int luma_def, chroma_def;
02229 
02230     h->use_weight= 0;
02231     h->use_weight_chroma= 0;
02232     h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
02233     if(h->sps.chroma_format_idc)
02234         h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
02235     luma_def = 1<<h->luma_log2_weight_denom;
02236     chroma_def = 1<<h->chroma_log2_weight_denom;
02237 
02238     for(list=0; list<2; list++){
02239         h->luma_weight_flag[list]   = 0;
02240         h->chroma_weight_flag[list] = 0;
02241         for(i=0; i<h->ref_count[list]; i++){
02242             int luma_weight_flag, chroma_weight_flag;
02243 
02244             luma_weight_flag= get_bits1(&s->gb);
02245             if(luma_weight_flag){
02246                 h->luma_weight[i][list][0]= get_se_golomb(&s->gb);
02247                 h->luma_weight[i][list][1]= get_se_golomb(&s->gb);
02248                 if(   h->luma_weight[i][list][0] != luma_def
02249                    || h->luma_weight[i][list][1] != 0) {
02250                     h->use_weight= 1;
02251                     h->luma_weight_flag[list]= 1;
02252                 }
02253             }else{
02254                 h->luma_weight[i][list][0]= luma_def;
02255                 h->luma_weight[i][list][1]= 0;
02256             }
02257 
02258             if(h->sps.chroma_format_idc){
02259                 chroma_weight_flag= get_bits1(&s->gb);
02260                 if(chroma_weight_flag){
02261                     int j;
02262                     for(j=0; j<2; j++){
02263                         h->chroma_weight[i][list][j][0]= get_se_golomb(&s->gb);
02264                         h->chroma_weight[i][list][j][1]= get_se_golomb(&s->gb);
02265                         if(   h->chroma_weight[i][list][j][0] != chroma_def
02266                            || h->chroma_weight[i][list][j][1] != 0) {
02267                             h->use_weight_chroma= 1;
02268                             h->chroma_weight_flag[list]= 1;
02269                         }
02270                     }
02271                 }else{
02272                     int j;
02273                     for(j=0; j<2; j++){
02274                         h->chroma_weight[i][list][j][0]= chroma_def;
02275                         h->chroma_weight[i][list][j][1]= 0;
02276                     }
02277                 }
02278             }
02279         }
02280         if(h->slice_type_nos != AV_PICTURE_TYPE_B) break;
02281     }
02282     h->use_weight= h->use_weight || h->use_weight_chroma;
02283     return 0;
02284 }
02285 
02291 static void implicit_weight_table(H264Context *h, int field){
02292     MpegEncContext * const s = &h->s;
02293     int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
02294 
02295     for (i = 0; i < 2; i++) {
02296         h->luma_weight_flag[i]   = 0;
02297         h->chroma_weight_flag[i] = 0;
02298     }
02299 
02300     if(field < 0){
02301         if (s->picture_structure == PICT_FRAME) {
02302             cur_poc = s->current_picture_ptr->poc;
02303         } else {
02304             cur_poc = s->current_picture_ptr->field_poc[s->picture_structure - 1];
02305         }
02306     if(   h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF
02307        && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
02308         h->use_weight= 0;
02309         h->use_weight_chroma= 0;
02310         return;
02311     }
02312         ref_start= 0;
02313         ref_count0= h->ref_count[0];
02314         ref_count1= h->ref_count[1];
02315     }else{
02316         cur_poc = s->current_picture_ptr->field_poc[field];
02317         ref_start= 16;
02318         ref_count0= 16+2*h->ref_count[0];
02319         ref_count1= 16+2*h->ref_count[1];
02320     }
02321 
02322     h->use_weight= 2;
02323     h->use_weight_chroma= 2;
02324     h->luma_log2_weight_denom= 5;
02325     h->chroma_log2_weight_denom= 5;
02326 
02327     for(ref0=ref_start; ref0 < ref_count0; ref0++){
02328         int poc0 = h->ref_list[0][ref0].poc;
02329         for(ref1=ref_start; ref1 < ref_count1; ref1++){
02330             int w = 32;
02331             if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
02332                 int poc1 = h->ref_list[1][ref1].poc;
02333                 int td = av_clip(poc1 - poc0, -128, 127);
02334                 if(td){
02335                     int tb = av_clip(cur_poc - poc0, -128, 127);
02336                     int tx = (16384 + (FFABS(td) >> 1)) / td;
02337                     int dist_scale_factor = (tb*tx + 32) >> 8;
02338                     if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
02339                         w = 64 - dist_scale_factor;
02340                 }
02341             }
02342             if(field<0){
02343                 h->implicit_weight[ref0][ref1][0]=
02344                 h->implicit_weight[ref0][ref1][1]= w;
02345             }else{
02346                 h->implicit_weight[ref0][ref1][field]=w;
02347             }
02348         }
02349     }
02350 }
02351 
02355 static void idr(H264Context *h){
02356     int i;
02357     ff_h264_remove_all_refs(h);
02358     h->prev_frame_num= 0;
02359     h->prev_frame_num_offset= 0;
02360     h->prev_poc_msb= 1<<16;
02361     h->prev_poc_lsb= 0;
02362     for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
02363         h->last_pocs[i] = INT_MIN;
02364 }
02365 
02366 /* forget old pics after a seek */
02367 static void flush_dpb(AVCodecContext *avctx){
02368     H264Context *h= avctx->priv_data;
02369     int i;
02370     for(i=0; i<=MAX_DELAYED_PIC_COUNT; i++) {
02371         if(h->delayed_pic[i])
02372             h->delayed_pic[i]->f.reference = 0;
02373         h->delayed_pic[i]= NULL;
02374     }
02375     h->outputed_poc=h->next_outputed_poc= INT_MIN;
02376     h->prev_interlaced_frame = 1;
02377     idr(h);
02378     h->prev_frame_num= -1;
02379     if(h->s.current_picture_ptr)
02380         h->s.current_picture_ptr->f.reference = 0;
02381     h->s.first_field= 0;
02382     ff_h264_reset_sei(h);
02383     ff_mpeg_flush(avctx);
02384     h->recovery_frame= -1;
02385     h->sync= 0;
02386 }
02387 
02388 static int init_poc(H264Context *h){
02389     MpegEncContext * const s = &h->s;
02390     const int max_frame_num= 1<<h->sps.log2_max_frame_num;
02391     int field_poc[2];
02392     Picture *cur = s->current_picture_ptr;
02393 
02394     h->frame_num_offset= h->prev_frame_num_offset;
02395     if(h->frame_num < h->prev_frame_num)
02396         h->frame_num_offset += max_frame_num;
02397 
02398     if(h->sps.poc_type==0){
02399         const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
02400 
02401         if     (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
02402             h->poc_msb = h->prev_poc_msb + max_poc_lsb;
02403         else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
02404             h->poc_msb = h->prev_poc_msb - max_poc_lsb;
02405         else
02406             h->poc_msb = h->prev_poc_msb;
02407 //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
02408         field_poc[0] =
02409         field_poc[1] = h->poc_msb + h->poc_lsb;
02410         if(s->picture_structure == PICT_FRAME)
02411             field_poc[1] += h->delta_poc_bottom;
02412     }else if(h->sps.poc_type==1){
02413         int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
02414         int i;
02415 
02416         if(h->sps.poc_cycle_length != 0)
02417             abs_frame_num = h->frame_num_offset + h->frame_num;
02418         else
02419             abs_frame_num = 0;
02420 
02421         if(h->nal_ref_idc==0 && abs_frame_num > 0)
02422             abs_frame_num--;
02423 
02424         expected_delta_per_poc_cycle = 0;
02425         for(i=0; i < h->sps.poc_cycle_length; i++)
02426             expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
02427 
02428         if(abs_frame_num > 0){
02429             int poc_cycle_cnt          = (abs_frame_num - 1) / h->sps.poc_cycle_length;
02430             int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
02431 
02432             expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
02433             for(i = 0; i <= frame_num_in_poc_cycle; i++)
02434                 expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
02435         } else
02436             expectedpoc = 0;
02437 
02438         if(h->nal_ref_idc == 0)
02439             expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
02440 
02441         field_poc[0] = expectedpoc + h->delta_poc[0];
02442         field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
02443 
02444         if(s->picture_structure == PICT_FRAME)
02445             field_poc[1] += h->delta_poc[1];
02446     }else{
02447         int poc= 2*(h->frame_num_offset + h->frame_num);
02448 
02449         if(!h->nal_ref_idc)
02450             poc--;
02451 
02452         field_poc[0]= poc;
02453         field_poc[1]= poc;
02454     }
02455 
02456     if(s->picture_structure != PICT_BOTTOM_FIELD)
02457         s->current_picture_ptr->field_poc[0]= field_poc[0];
02458     if(s->picture_structure != PICT_TOP_FIELD)
02459         s->current_picture_ptr->field_poc[1]= field_poc[1];
02460     cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
02461 
02462     return 0;
02463 }
02464 
02465 
02469 static void init_scan_tables(H264Context *h){
02470     int i;
02471     for(i=0; i<16; i++){
02472 #define T(x) (x>>2) | ((x<<2) & 0xF)
02473         h->zigzag_scan[i] = T(zigzag_scan[i]);
02474         h-> field_scan[i] = T( field_scan[i]);
02475 #undef T
02476     }
02477     for(i=0; i<64; i++){
02478 #define T(x) (x>>3) | ((x&7)<<3)
02479         h->zigzag_scan8x8[i]       = T(ff_zigzag_direct[i]);
02480         h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
02481         h->field_scan8x8[i]        = T(field_scan8x8[i]);
02482         h->field_scan8x8_cavlc[i]  = T(field_scan8x8_cavlc[i]);
02483 #undef T
02484     }
02485     if(h->sps.transform_bypass){ //FIXME same ugly
02486         h->zigzag_scan_q0          = zigzag_scan;
02487         h->zigzag_scan8x8_q0       = ff_zigzag_direct;
02488         h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
02489         h->field_scan_q0           = field_scan;
02490         h->field_scan8x8_q0        = field_scan8x8;
02491         h->field_scan8x8_cavlc_q0  = field_scan8x8_cavlc;
02492     }else{
02493         h->zigzag_scan_q0          = h->zigzag_scan;
02494         h->zigzag_scan8x8_q0       = h->zigzag_scan8x8;
02495         h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
02496         h->field_scan_q0           = h->field_scan;
02497         h->field_scan8x8_q0        = h->field_scan8x8;
02498         h->field_scan8x8_cavlc_q0  = h->field_scan8x8_cavlc;
02499     }
02500 }
02501 
02502 static int field_end(H264Context *h, int in_setup){
02503     MpegEncContext * const s = &h->s;
02504     AVCodecContext * const avctx= s->avctx;
02505     int err = 0;
02506     s->mb_y= 0;
02507 
02508     if (!in_setup && !s->dropable)
02509         ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX,
02510                                   s->picture_structure == PICT_BOTTOM_FIELD);
02511 
02512     if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
02513         ff_vdpau_h264_set_reference_frames(s);
02514 
02515     if(in_setup || !(avctx->active_thread_type&FF_THREAD_FRAME)){
02516         if(!s->dropable) {
02517             err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
02518             h->prev_poc_msb= h->poc_msb;
02519             h->prev_poc_lsb= h->poc_lsb;
02520         }
02521         h->prev_frame_num_offset= h->frame_num_offset;
02522         h->prev_frame_num= h->frame_num;
02523         h->outputed_poc = h->next_outputed_poc;
02524     }
02525 
02526     if (avctx->hwaccel) {
02527         if (avctx->hwaccel->end_frame(avctx) < 0)
02528             av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
02529     }
02530 
02531     if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
02532         ff_vdpau_h264_picture_complete(s);
02533 
02534     /*
02535      * FIXME: Error handling code does not seem to support interlaced
02536      * when slices span multiple rows
02537      * The ff_er_add_slice calls don't work right for bottom
02538      * fields; they cause massive erroneous error concealing
02539      * Error marking covers both fields (top and bottom).
02540      * This causes a mismatched s->error_count
02541      * and a bad error table. Further, the error count goes to
02542      * INT_MAX when called for bottom field, because mb_y is
02543      * past end by one (callers fault) and resync_mb_y != 0
02544      * causes problems for the first MB line, too.
02545      */
02546     if (!FIELD_PICTURE)
02547         ff_er_frame_end(s);
02548 
02549     MPV_frame_end(s);
02550 
02551     h->current_slice=0;
02552 
02553     return err;
02554 }
02555 
02559 static void clone_slice(H264Context *dst, H264Context *src)
02560 {
02561     memcpy(dst->block_offset,     src->block_offset, sizeof(dst->block_offset));
02562     dst->s.current_picture_ptr  = src->s.current_picture_ptr;
02563     dst->s.current_picture      = src->s.current_picture;
02564     dst->s.linesize             = src->s.linesize;
02565     dst->s.uvlinesize           = src->s.uvlinesize;
02566     dst->s.first_field          = src->s.first_field;
02567 
02568     dst->prev_poc_msb           = src->prev_poc_msb;
02569     dst->prev_poc_lsb           = src->prev_poc_lsb;
02570     dst->prev_frame_num_offset  = src->prev_frame_num_offset;
02571     dst->prev_frame_num         = src->prev_frame_num;
02572     dst->short_ref_count        = src->short_ref_count;
02573 
02574     memcpy(dst->short_ref,        src->short_ref,        sizeof(dst->short_ref));
02575     memcpy(dst->long_ref,         src->long_ref,         sizeof(dst->long_ref));
02576     memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
02577     memcpy(dst->ref_list,         src->ref_list,         sizeof(dst->ref_list));
02578 
02579     memcpy(dst->dequant4_coeff,   src->dequant4_coeff,   sizeof(src->dequant4_coeff));
02580     memcpy(dst->dequant8_coeff,   src->dequant8_coeff,   sizeof(src->dequant8_coeff));
02581 }
02582 
02590 int ff_h264_get_profile(SPS *sps)
02591 {
02592     int profile = sps->profile_idc;
02593 
02594     switch(sps->profile_idc) {
02595     case FF_PROFILE_H264_BASELINE:
02596         // constraint_set1_flag set to 1
02597         profile |= (sps->constraint_set_flags & 1<<1) ? FF_PROFILE_H264_CONSTRAINED : 0;
02598         break;
02599     case FF_PROFILE_H264_HIGH_10:
02600     case FF_PROFILE_H264_HIGH_422:
02601     case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
02602         // constraint_set3_flag set to 1
02603         profile |= (sps->constraint_set_flags & 1<<3) ? FF_PROFILE_H264_INTRA : 0;
02604         break;
02605     }
02606 
02607     return profile;
02608 }
02609 
02619 static int decode_slice_header(H264Context *h, H264Context *h0){
02620     MpegEncContext * const s = &h->s;
02621     MpegEncContext * const s0 = &h0->s;
02622     unsigned int first_mb_in_slice;
02623     unsigned int pps_id;
02624     int num_ref_idx_active_override_flag;
02625     unsigned int slice_type, tmp, i, j;
02626     int default_ref_list_done = 0;
02627     int last_pic_structure, last_pic_dropable;
02628 
02629     /* FIXME: 2tap qpel isn't implemented for high bit depth. */
02630     if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc && !h->pixel_shift){
02631         s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
02632         s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
02633     }else{
02634         s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
02635         s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
02636     }
02637 
02638     first_mb_in_slice= get_ue_golomb_long(&s->gb);
02639 
02640     if(first_mb_in_slice == 0){ //FIXME better field boundary detection
02641         if(h0->current_slice && FIELD_PICTURE){
02642             field_end(h, 1);
02643         }
02644 
02645         h0->current_slice = 0;
02646         if (!s0->first_field) {
02647             if (s->current_picture_ptr && !s->dropable &&
02648                 s->current_picture_ptr->owner2 == s) {
02649                 ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX,
02650                                           s->picture_structure == PICT_BOTTOM_FIELD);
02651             }
02652             s->current_picture_ptr = NULL;
02653         }
02654     }
02655 
02656     slice_type= get_ue_golomb_31(&s->gb);
02657     if(slice_type > 9){
02658         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);
02659         return -1;
02660     }
02661     if(slice_type > 4){
02662         slice_type -= 5;
02663         h->slice_type_fixed=1;
02664     }else
02665         h->slice_type_fixed=0;
02666 
02667     slice_type= golomb_to_pict_type[ slice_type ];
02668     if (slice_type == AV_PICTURE_TYPE_I
02669         || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
02670         default_ref_list_done = 1;
02671     }
02672     h->slice_type= slice_type;
02673     h->slice_type_nos= slice_type & 3;
02674 
02675     s->pict_type= h->slice_type; // to make a few old functions happy, it's wrong though
02676 
02677     pps_id= get_ue_golomb(&s->gb);
02678     if(pps_id>=MAX_PPS_COUNT){
02679         av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
02680         return -1;
02681     }
02682     if(!h0->pps_buffers[pps_id]) {
02683         av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
02684         return -1;
02685     }
02686     h->pps= *h0->pps_buffers[pps_id];
02687 
02688     if(!h0->sps_buffers[h->pps.sps_id]) {
02689         av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
02690         return -1;
02691     }
02692     h->sps = *h0->sps_buffers[h->pps.sps_id];
02693 
02694     s->avctx->profile = ff_h264_get_profile(&h->sps);
02695     s->avctx->level   = h->sps.level_idc;
02696     s->avctx->refs    = h->sps.ref_frame_count;
02697 
02698     s->mb_width= h->sps.mb_width;
02699     s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
02700 
02701     h->b_stride=  s->mb_width*4;
02702 
02703     s->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
02704 
02705     s->width = 16*s->mb_width;
02706     s->height= 16*s->mb_height;
02707 
02708     if (s->context_initialized
02709         && (   s->width != s->avctx->coded_width || s->height != s->avctx->coded_height
02710             || s->avctx->bits_per_raw_sample != h->sps.bit_depth_luma
02711             || h->cur_chroma_format_idc != h->sps.chroma_format_idc
02712             || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) {
02713         if(h != h0 || (HAVE_THREADS && h->s.avctx->active_thread_type & FF_THREAD_FRAME)) {
02714             av_log_missing_feature(s->avctx, "Width/height/bit depth/chroma idc changing with threads is", 0);
02715             return AVERROR_PATCHWELCOME;   // width / height changed during parallelized decoding
02716         }
02717         free_tables(h, 0);
02718         flush_dpb(s->avctx);
02719         MPV_common_end(s);
02720         h->list_count = 0;
02721     }
02722     if (!s->context_initialized) {
02723         if (h != h0) {
02724             av_log(h->s.avctx, AV_LOG_ERROR, "Cannot (re-)initialize context during parallel decoding.\n");
02725             return -1;
02726         }
02727         avcodec_set_dimensions(s->avctx, s->width, s->height);
02728         s->avctx->width  -= (2>>CHROMA444)*FFMIN(h->sps.crop_right, (8<<CHROMA444)-1);
02729         s->avctx->height -= (1<<s->chroma_y_shift)*FFMIN(h->sps.crop_bottom, (16>>s->chroma_y_shift)-1) * (2 - h->sps.frame_mbs_only_flag);
02730         s->avctx->sample_aspect_ratio= h->sps.sar;
02731         av_assert0(s->avctx->sample_aspect_ratio.den);
02732 
02733         if (s->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
02734             h->cur_chroma_format_idc != h->sps.chroma_format_idc) {
02735             if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10 &&
02736                 (h->sps.bit_depth_luma != 9 || !CHROMA422)) {
02737                 s->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
02738                 h->cur_chroma_format_idc = h->sps.chroma_format_idc;
02739                 h->pixel_shift = h->sps.bit_depth_luma > 8;
02740 
02741                 ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma, h->sps.chroma_format_idc);
02742                 ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma, h->sps.chroma_format_idc);
02743                 s->dsp.dct_bits = h->sps.bit_depth_luma > 8 ? 32 : 16;
02744                 dsputil_init(&s->dsp, s->avctx);
02745             } else {
02746                 av_log(s->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d chroma_idc: %d\n",
02747                        h->sps.bit_depth_luma, h->sps.chroma_format_idc);
02748                 return -1;
02749             }
02750         }
02751 
02752         if(h->sps.video_signal_type_present_flag){
02753             s->avctx->color_range = h->sps.full_range>0 ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
02754             if(h->sps.colour_description_present_flag){
02755                 s->avctx->color_primaries = h->sps.color_primaries;
02756                 s->avctx->color_trc       = h->sps.color_trc;
02757                 s->avctx->colorspace      = h->sps.colorspace;
02758             }
02759         }
02760 
02761         if(h->sps.timing_info_present_flag){
02762             int64_t den= h->sps.time_scale;
02763             if(h->x264_build < 44U)
02764                 den *= 2;
02765             av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
02766                       h->sps.num_units_in_tick, den, 1<<30);
02767         }
02768 
02769         switch (h->sps.bit_depth_luma) {
02770             case 9 :
02771                 if (CHROMA444) {
02772                     if (s->avctx->colorspace == AVCOL_SPC_RGB) {
02773                         s->avctx->pix_fmt = PIX_FMT_GBRP9;
02774                     } else
02775                         s->avctx->pix_fmt = PIX_FMT_YUV444P9;
02776                 } else if (CHROMA422)
02777                     s->avctx->pix_fmt = PIX_FMT_YUV422P9;
02778                 else
02779                     s->avctx->pix_fmt = PIX_FMT_YUV420P9;
02780                 break;
02781             case 10 :
02782                 if (CHROMA444) {
02783                     if (s->avctx->colorspace == AVCOL_SPC_RGB) {
02784                         s->avctx->pix_fmt = PIX_FMT_GBRP10;
02785                     } else
02786                         s->avctx->pix_fmt = PIX_FMT_YUV444P10;
02787                 } else if (CHROMA422)
02788                     s->avctx->pix_fmt = PIX_FMT_YUV422P10;
02789                 else
02790                     s->avctx->pix_fmt = PIX_FMT_YUV420P10;
02791                 break;
02792             case 8:
02793                 if (CHROMA444){
02794                     s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ444P : PIX_FMT_YUV444P;
02795                     if (s->avctx->colorspace == AVCOL_SPC_RGB) {
02796                        s->avctx->pix_fmt = PIX_FMT_GBR24P;
02797                        av_log(h->s.avctx, AV_LOG_DEBUG, "Detected GBR colorspace.\n");
02798                     } else if (s->avctx->colorspace == AVCOL_SPC_YCGCO) {
02799                         av_log(h->s.avctx, AV_LOG_WARNING, "Detected unsupported YCgCo colorspace.\n");
02800                     }
02801                 } else if (CHROMA422) {
02802                     s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ422P : PIX_FMT_YUV422P;
02803                 }else{
02804                     s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
02805                                                              s->avctx->codec->pix_fmts ?
02806                                                              s->avctx->codec->pix_fmts :
02807                                                              s->avctx->color_range == AVCOL_RANGE_JPEG ?
02808                                                              hwaccel_pixfmt_list_h264_jpeg_420 :
02809                                                              ff_hwaccel_pixfmt_list_420);
02810                 }
02811                 break;
02812             default:
02813                 av_log(s->avctx, AV_LOG_ERROR,
02814                        "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
02815                 return AVERROR_INVALIDDATA;
02816         }
02817 
02818         s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
02819 
02820         if (MPV_common_init(s) < 0) {
02821             av_log(h->s.avctx, AV_LOG_ERROR, "MPV_common_init() failed.\n");
02822             return -1;
02823         }
02824         s->first_field = 0;
02825         h->prev_interlaced_frame = 1;
02826 
02827         init_scan_tables(h);
02828         if (ff_h264_alloc_tables(h) < 0) {
02829             av_log(h->s.avctx, AV_LOG_ERROR, "Could not allocate memory for h264\n");
02830             return AVERROR(ENOMEM);
02831         }
02832 
02833         if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_SLICE)) {
02834             if (context_init(h) < 0) {
02835                 av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed.\n");
02836                 return -1;
02837             }
02838         } else {
02839             for(i = 1; i < s->slice_context_count; i++) {
02840                 H264Context *c;
02841                 c = h->thread_context[i] = av_malloc(sizeof(H264Context));
02842                 memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
02843                 memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
02844                 c->h264dsp = h->h264dsp;
02845                 c->sps = h->sps;
02846                 c->pps = h->pps;
02847                 c->pixel_shift = h->pixel_shift;
02848                 c->cur_chroma_format_idc = h->cur_chroma_format_idc;
02849                 init_scan_tables(c);
02850                 clone_tables(c, h, i);
02851             }
02852 
02853             for(i = 0; i < s->slice_context_count; i++)
02854                 if (context_init(h->thread_context[i]) < 0) {
02855                     av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed.\n");
02856                     return -1;
02857                 }
02858         }
02859     }
02860 
02861     if(h == h0 && h->dequant_coeff_pps != pps_id){
02862         h->dequant_coeff_pps = pps_id;
02863         init_dequant_tables(h);
02864     }
02865 
02866     h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
02867 
02868     h->mb_mbaff = 0;
02869     h->mb_aff_frame = 0;
02870     last_pic_structure = s0->picture_structure;
02871     last_pic_dropable  = s->dropable;
02872     s->dropable        = h->nal_ref_idc == 0;
02873     if(h->sps.frame_mbs_only_flag){
02874         s->picture_structure= PICT_FRAME;
02875     }else{
02876         if(!h->sps.direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B){
02877             av_log(h->s.avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
02878             return -1;
02879         }
02880         if(get_bits1(&s->gb)) { //field_pic_flag
02881             s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
02882         } else {
02883             s->picture_structure= PICT_FRAME;
02884             h->mb_aff_frame = h->sps.mb_aff;
02885         }
02886     }
02887     h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
02888 
02889     if (h0->current_slice != 0) {
02890         if (last_pic_structure != s->picture_structure ||
02891             last_pic_dropable  != s->dropable) {
02892             av_log(h->s.avctx, AV_LOG_ERROR,
02893                    "Changing field mode (%d -> %d) between slices is not allowed\n",
02894                    last_pic_structure, s->picture_structure);
02895             s->picture_structure = last_pic_structure;
02896             s->dropable          = last_pic_dropable;
02897             return AVERROR_INVALIDDATA;
02898         }
02899     } else {
02900         /* Shorten frame num gaps so we don't have to allocate reference
02901          * frames just to throw them away */
02902         if (h->frame_num != h->prev_frame_num && h->prev_frame_num >= 0) {
02903             int unwrap_prev_frame_num = h->prev_frame_num;
02904             int max_frame_num         = 1 << h->sps.log2_max_frame_num;
02905 
02906             if (unwrap_prev_frame_num > h->frame_num) unwrap_prev_frame_num -= max_frame_num;
02907 
02908             if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
02909                 unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
02910                 if (unwrap_prev_frame_num < 0)
02911                     unwrap_prev_frame_num += max_frame_num;
02912 
02913                 h->prev_frame_num = unwrap_prev_frame_num;
02914             }
02915         }
02916 
02917         /* See if we have a decoded first field looking for a pair...
02918          * Here, we're using that to see if we should mark previously
02919          * decode frames as "finished".
02920          * We have to do that before the "dummy" in-between frame allocation,
02921          * since that can modify s->current_picture_ptr. */
02922         if (s0->first_field) {
02923             assert(s0->current_picture_ptr);
02924             assert(s0->current_picture_ptr->f.data[0]);
02925             assert(s0->current_picture_ptr->f.reference != DELAYED_PIC_REF);
02926 
02927             /* Mark old field/frame as completed */
02928             if (!last_pic_dropable && s0->current_picture_ptr->owner2 == s0) {
02929                 ff_thread_report_progress(&s0->current_picture_ptr->f, INT_MAX,
02930                                           last_pic_structure == PICT_BOTTOM_FIELD);
02931             }
02932 
02933             /* figure out if we have a complementary field pair */
02934             if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
02935                 /* Previous field is unmatched. Don't display it, but let it
02936                  * remain for reference if marked as such. */
02937                 if (!last_pic_dropable && last_pic_structure != PICT_FRAME) {
02938                     ff_thread_report_progress(&s0->current_picture_ptr->f, INT_MAX,
02939                                               last_pic_structure == PICT_TOP_FIELD);
02940                 }
02941             } else {
02942                 if (s0->current_picture_ptr->frame_num != h->frame_num) {
02943                     /* This and previous field were reference, but had
02944                      * different frame_nums. Consider this field first in
02945                      * pair. Throw away previous field except for reference
02946                      * purposes. */
02947                     if (!last_pic_dropable && last_pic_structure != PICT_FRAME) {
02948                         ff_thread_report_progress(&s0->current_picture_ptr->f, INT_MAX,
02949                                                   last_pic_structure == PICT_TOP_FIELD);
02950                     }
02951                 } else {
02952                     /* Second field in complementary pair */
02953                     if (!((last_pic_structure   == PICT_TOP_FIELD &&
02954                            s->picture_structure == PICT_BOTTOM_FIELD) ||
02955                           (last_pic_structure   == PICT_BOTTOM_FIELD &&
02956                            s->picture_structure == PICT_TOP_FIELD))) {
02957                         av_log(s->avctx, AV_LOG_ERROR,
02958                                "Invalid field mode combination %d/%d\n",
02959                                last_pic_structure, s->picture_structure);
02960                         s->picture_structure = last_pic_structure;
02961                         s->dropable          = last_pic_dropable;
02962                         return AVERROR_INVALIDDATA;
02963                     } else if (last_pic_dropable != s->dropable) {
02964                         av_log(s->avctx, AV_LOG_ERROR,
02965                                "Cannot combine reference and non-reference fields in the same frame\n");
02966                         av_log_ask_for_sample(s->avctx, NULL);
02967                         s->picture_structure = last_pic_structure;
02968                         s->dropable          = last_pic_dropable;
02969                         return AVERROR_INVALIDDATA;
02970                     }
02971 
02972                     /* Take ownership of this buffer. Note that if another thread owned
02973                      * the first field of this buffer, we're not operating on that pointer,
02974                      * so the original thread is still responsible for reporting progress
02975                      * on that first field (or if that was us, we just did that above).
02976                      * By taking ownership, we assign responsibility to ourselves to
02977                      * report progress on the second field. */
02978                     s0->current_picture_ptr->owner2 = s0;
02979                 }
02980             }
02981         }
02982 
02983         while (h->frame_num != h->prev_frame_num && h->prev_frame_num >= 0 &&
02984                h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {
02985             Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
02986             av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
02987             if (ff_h264_frame_start(h) < 0)
02988                 return -1;
02989             h->prev_frame_num++;
02990             h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
02991             s->current_picture_ptr->frame_num= h->prev_frame_num;
02992             ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 0);
02993             ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 1);
02994             ff_generate_sliding_window_mmcos(h);
02995             if (ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index) < 0 &&
02996                 (s->avctx->err_recognition & AV_EF_EXPLODE))
02997                 return AVERROR_INVALIDDATA;
02998             /* Error concealment: if a ref is missing, copy the previous ref in its place.
02999              * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
03000              * about there being no actual duplicates.
03001              * FIXME: this doesn't copy padding for out-of-frame motion vectors.  Given we're
03002              * concealing a lost frame, this probably isn't noticeable by comparison, but it should
03003              * be fixed. */
03004             if (h->short_ref_count) {
03005                 if (prev) {
03006                     av_image_copy(h->short_ref[0]->f.data, h->short_ref[0]->f.linesize,
03007                                   (const uint8_t**)prev->f.data, prev->f.linesize,
03008                                   s->avctx->pix_fmt, s->mb_width*16, s->mb_height*16);
03009                     h->short_ref[0]->poc = prev->poc+2;
03010                 }
03011                 h->short_ref[0]->frame_num = h->prev_frame_num;
03012             }
03013         }
03014 
03015         /* See if we have a decoded first field looking for a pair...
03016          * We're using that to see whether to continue decoding in that
03017          * frame, or to allocate a new one. */
03018         if (s0->first_field) {
03019             assert(s0->current_picture_ptr);
03020             assert(s0->current_picture_ptr->f.data[0]);
03021             assert(s0->current_picture_ptr->f.reference != DELAYED_PIC_REF);
03022 
03023             /* figure out if we have a complementary field pair */
03024             if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
03025                 /*
03026                  * Previous field is unmatched. Don't display it, but let it
03027                  * remain for reference if marked as such.
03028                  */
03029                 s0->current_picture_ptr = NULL;
03030                 s0->first_field = FIELD_PICTURE;
03031 
03032             } else {
03033                 if (s0->current_picture_ptr->frame_num != h->frame_num) {
03034                     /* This and the previous field had different frame_nums.
03035                      * Consider this field first in pair. Throw away previous
03036                      * one except for reference purposes. */
03037                     s0->first_field         = 1;
03038                     s0->current_picture_ptr = NULL;
03039 
03040                 } else {
03041                     /* Second field in complementary pair */
03042                     s0->first_field = 0;
03043                 }
03044             }
03045 
03046         } else {
03047             /* Frame or first field in a potentially complementary pair */
03048             assert(!s0->current_picture_ptr);
03049             s0->first_field = FIELD_PICTURE;
03050         }
03051 
03052         if(!FIELD_PICTURE || s0->first_field) {
03053             if (ff_h264_frame_start(h) < 0) {
03054                 s0->first_field = 0;
03055                 return -1;
03056             }
03057         } else {
03058             ff_release_unused_pictures(s, 0);
03059         }
03060     }
03061     if(h != h0)
03062         clone_slice(h, h0);
03063 
03064     s->current_picture_ptr->frame_num= h->frame_num; //FIXME frame_num cleanup
03065 
03066     assert(s->mb_num == s->mb_width * s->mb_height);
03067     if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
03068        first_mb_in_slice                    >= s->mb_num){
03069         av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
03070         return -1;
03071     }
03072     s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
03073     s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
03074     if (s->picture_structure == PICT_BOTTOM_FIELD)
03075         s->resync_mb_y = s->mb_y = s->mb_y + 1;
03076     assert(s->mb_y < s->mb_height);
03077 
03078     if(s->picture_structure==PICT_FRAME){
03079         h->curr_pic_num=   h->frame_num;
03080         h->max_pic_num= 1<< h->sps.log2_max_frame_num;
03081     }else{
03082         h->curr_pic_num= 2*h->frame_num + 1;
03083         h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
03084     }
03085 
03086     if(h->nal_unit_type == NAL_IDR_SLICE){
03087         get_ue_golomb(&s->gb); /* idr_pic_id */
03088     }
03089 
03090     if(h->sps.poc_type==0){
03091         h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
03092 
03093         if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
03094             h->delta_poc_bottom= get_se_golomb(&s->gb);
03095         }
03096     }
03097 
03098     if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
03099         h->delta_poc[0]= get_se_golomb(&s->gb);
03100 
03101         if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
03102             h->delta_poc[1]= get_se_golomb(&s->gb);
03103     }
03104 
03105     init_poc(h);
03106 
03107     if(h->pps.redundant_pic_cnt_present){
03108         h->redundant_pic_count= get_ue_golomb(&s->gb);
03109     }
03110 
03111     //set defaults, might be overridden a few lines later
03112     h->ref_count[0]= h->pps.ref_count[0];
03113     h->ref_count[1]= h->pps.ref_count[1];
03114 
03115     if(h->slice_type_nos != AV_PICTURE_TYPE_I){
03116         unsigned max= s->picture_structure == PICT_FRAME ? 15 : 31;
03117 
03118         if(h->slice_type_nos == AV_PICTURE_TYPE_B){
03119             h->direct_spatial_mv_pred= get_bits1(&s->gb);
03120         }
03121         num_ref_idx_active_override_flag= get_bits1(&s->gb);
03122 
03123         if(num_ref_idx_active_override_flag){
03124             h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
03125             if(h->slice_type_nos==AV_PICTURE_TYPE_B)
03126                 h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
03127         }
03128 
03129         if (h->ref_count[0]-1 > max || h->ref_count[1]-1 > max){
03130             av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
03131             h->ref_count[0] = h->ref_count[1] = 1;
03132             return AVERROR_INVALIDDATA;
03133         }
03134 
03135         if(h->slice_type_nos == AV_PICTURE_TYPE_B)
03136             h->list_count= 2;
03137         else
03138             h->list_count= 1;
03139     }else
03140         h->ref_count[1]= h->ref_count[0]= h->list_count= 0;
03141 
03142     if(!default_ref_list_done){
03143         ff_h264_fill_default_ref_list(h);
03144     }
03145 
03146     if(h->slice_type_nos!=AV_PICTURE_TYPE_I && ff_h264_decode_ref_pic_list_reordering(h) < 0) {
03147         h->ref_count[1]= h->ref_count[0]= 0;
03148         return -1;
03149     }
03150 
03151     if(h->slice_type_nos!=AV_PICTURE_TYPE_I){
03152         s->last_picture_ptr= &h->ref_list[0][0];
03153         ff_copy_picture(&s->last_picture, s->last_picture_ptr);
03154     }
03155     if(h->slice_type_nos==AV_PICTURE_TYPE_B){
03156         s->next_picture_ptr= &h->ref_list[1][0];
03157         ff_copy_picture(&s->next_picture, s->next_picture_ptr);
03158     }
03159 
03160     if(   (h->pps.weighted_pred          && h->slice_type_nos == AV_PICTURE_TYPE_P )
03161        ||  (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== AV_PICTURE_TYPE_B ) )
03162         pred_weight_table(h);
03163     else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== AV_PICTURE_TYPE_B){
03164         implicit_weight_table(h, -1);
03165     }else {
03166         h->use_weight = 0;
03167         for (i = 0; i < 2; i++) {
03168             h->luma_weight_flag[i]   = 0;
03169             h->chroma_weight_flag[i] = 0;
03170         }
03171     }
03172 
03173     if(h->nal_ref_idc && ff_h264_decode_ref_pic_marking(h0, &s->gb) < 0 &&
03174        (s->avctx->err_recognition & AV_EF_EXPLODE))
03175         return AVERROR_INVALIDDATA;
03176 
03177     if(FRAME_MBAFF){
03178         ff_h264_fill_mbaff_ref_list(h);
03179 
03180         if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== AV_PICTURE_TYPE_B){
03181             implicit_weight_table(h, 0);
03182             implicit_weight_table(h, 1);
03183         }
03184     }
03185 
03186     if(h->slice_type_nos==AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
03187         ff_h264_direct_dist_scale_factor(h);
03188     ff_h264_direct_ref_list_init(h);
03189 
03190     if( h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac ){
03191         tmp = get_ue_golomb_31(&s->gb);
03192         if(tmp > 2){
03193             av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
03194             return -1;
03195         }
03196         h->cabac_init_idc= tmp;
03197     }
03198 
03199     h->last_qscale_diff = 0;
03200     tmp = h->pps.init_qp + get_se_golomb(&s->gb);
03201     if(tmp>51+6*(h->sps.bit_depth_luma-8)){
03202         av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
03203         return -1;
03204     }
03205     s->qscale= tmp;
03206     h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
03207     h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
03208     //FIXME qscale / qp ... stuff
03209     if(h->slice_type == AV_PICTURE_TYPE_SP){
03210         get_bits1(&s->gb); /* sp_for_switch_flag */
03211     }
03212     if(h->slice_type==AV_PICTURE_TYPE_SP || h->slice_type == AV_PICTURE_TYPE_SI){
03213         get_se_golomb(&s->gb); /* slice_qs_delta */
03214     }
03215 
03216     h->deblocking_filter = 1;
03217     h->slice_alpha_c0_offset = 52;
03218     h->slice_beta_offset = 52;
03219     if( h->pps.deblocking_filter_parameters_present ) {
03220         tmp= get_ue_golomb_31(&s->gb);
03221         if(tmp > 2){
03222             av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
03223             return -1;
03224         }
03225         h->deblocking_filter= tmp;
03226         if(h->deblocking_filter < 2)
03227             h->deblocking_filter^= 1; // 1<->0
03228 
03229         if( h->deblocking_filter ) {
03230             h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
03231             h->slice_beta_offset     += get_se_golomb(&s->gb) << 1;
03232             if(   h->slice_alpha_c0_offset > 104U
03233                || h->slice_beta_offset     > 104U){
03234                 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);
03235                 return -1;
03236             }
03237         }
03238     }
03239 
03240     if(   s->avctx->skip_loop_filter >= AVDISCARD_ALL
03241        ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != AV_PICTURE_TYPE_I)
03242        ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR  && h->slice_type_nos == AV_PICTURE_TYPE_B)
03243        ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
03244         h->deblocking_filter= 0;
03245 
03246     if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
03247         if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
03248             /* Cheat slightly for speed:
03249                Do not bother to deblock across slices. */
03250             h->deblocking_filter = 2;
03251         } else {
03252             h0->max_contexts = 1;
03253             if(!h0->single_decode_warning) {
03254                 av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
03255                 h0->single_decode_warning = 1;
03256             }
03257             if (h != h0) {
03258                 av_log(h->s.avctx, AV_LOG_ERROR, "Deblocking switched inside frame.\n");
03259                 return 1;
03260             }
03261         }
03262     }
03263     h->qp_thresh = 15 + 52 - FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset)
03264                  - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1])
03265                  + 6 * (h->sps.bit_depth_luma - 8);
03266 
03267 #if 0 //FMO
03268     if( h->pps.num_slice_groups > 1  && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
03269         slice_group_change_cycle= get_bits(&s->gb, ?);
03270 #endif
03271 
03272     h0->last_slice_type = slice_type;
03273     h->slice_num = ++h0->current_slice;
03274 
03275     if(h->slice_num)
03276         h0->slice_row[(h->slice_num-1)&(MAX_SLICES-1)]= s->resync_mb_y;
03277     if (   h0->slice_row[h->slice_num&(MAX_SLICES-1)] + 3 >= s->resync_mb_y
03278         && h0->slice_row[h->slice_num&(MAX_SLICES-1)] <= s->resync_mb_y
03279         && h->slice_num >= MAX_SLICES) {
03280         //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
03281         av_log(s->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", h->slice_num, MAX_SLICES);
03282     }
03283 
03284     for(j=0; j<2; j++){
03285         int id_list[16];
03286         int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
03287         for(i=0; i<16; i++){
03288             id_list[i]= 60;
03289             if (h->ref_list[j][i].f.data[0]) {
03290                 int k;
03291                 uint8_t *base = h->ref_list[j][i].f.base[0];
03292                 for(k=0; k<h->short_ref_count; k++)
03293                     if (h->short_ref[k]->f.base[0] == base) {
03294                         id_list[i]= k;
03295                         break;
03296                     }
03297                 for(k=0; k<h->long_ref_count; k++)
03298                     if (h->long_ref[k] && h->long_ref[k]->f.base[0] == base) {
03299                         id_list[i]= h->short_ref_count + k;
03300                         break;
03301                     }
03302             }
03303         }
03304 
03305         ref2frm[0]=
03306         ref2frm[1]= -1;
03307         for(i=0; i<16; i++)
03308             ref2frm[i+2]= 4*id_list[i]
03309                           + (h->ref_list[j][i].f.reference & 3);
03310         ref2frm[18+0]=
03311         ref2frm[18+1]= -1;
03312         for(i=16; i<48; i++)
03313             ref2frm[i+4]= 4*id_list[(i-16)>>1]
03314                           + (h->ref_list[j][i].f.reference & 3);
03315     }
03316 
03317     //FIXME: fix draw_edges+PAFF+frame threads
03318     h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE || (!h->sps.frame_mbs_only_flag && s->avctx->active_thread_type)) ? 0 : 16;
03319     h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
03320 
03321     if(s->avctx->debug&FF_DEBUG_PICT_INFO){
03322         av_log(h->s.avctx, AV_LOG_DEBUG, "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
03323                h->slice_num,
03324                (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
03325                first_mb_in_slice,
03326                av_get_picture_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
03327                pps_id, h->frame_num,
03328                s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
03329                h->ref_count[0], h->ref_count[1],
03330                s->qscale,
03331                h->deblocking_filter, h->slice_alpha_c0_offset/2-26, h->slice_beta_offset/2-26,
03332                h->use_weight,
03333                h->use_weight==1 && h->use_weight_chroma ? "c" : "",
03334                h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
03335                );
03336     }
03337 
03338     return 0;
03339 }
03340 
03341 int ff_h264_get_slice_type(const H264Context *h)
03342 {
03343     switch (h->slice_type) {
03344     case AV_PICTURE_TYPE_P:  return 0;
03345     case AV_PICTURE_TYPE_B:  return 1;
03346     case AV_PICTURE_TYPE_I:  return 2;
03347     case AV_PICTURE_TYPE_SP: return 3;
03348     case AV_PICTURE_TYPE_SI: return 4;
03349     default:         return -1;
03350     }
03351 }
03352 
03353 static av_always_inline void fill_filter_caches_inter(H264Context *h, MpegEncContext * const s, int mb_type, int top_xy,
03354                                                       int left_xy[LEFT_MBS], int top_type, int left_type[LEFT_MBS], int mb_xy, int list)
03355 {
03356     int b_stride = h->b_stride;
03357     int16_t (*mv_dst)[2] = &h->mv_cache[list][scan8[0]];
03358     int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
03359     if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
03360         if(USES_LIST(top_type, list)){
03361             const int b_xy= h->mb2b_xy[top_xy] + 3*b_stride;
03362             const int b8_xy= 4*top_xy + 2;
03363             int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
03364             AV_COPY128(mv_dst - 1*8, s->current_picture.f.motion_val[list][b_xy + 0]);
03365             ref_cache[0 - 1*8]=
03366             ref_cache[1 - 1*8]= ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 0]];
03367             ref_cache[2 - 1*8]=
03368             ref_cache[3 - 1*8]= ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 1]];
03369         }else{
03370             AV_ZERO128(mv_dst - 1*8);
03371             AV_WN32A(&ref_cache[0 - 1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
03372         }
03373 
03374         if(!IS_INTERLACED(mb_type^left_type[LTOP])){
03375             if(USES_LIST(left_type[LTOP], list)){
03376                 const int b_xy= h->mb2b_xy[left_xy[LTOP]] + 3;
03377                 const int b8_xy= 4*left_xy[LTOP] + 1;
03378                 int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[LTOP]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
03379                 AV_COPY32(mv_dst - 1 +  0, s->current_picture.f.motion_val[list][b_xy + b_stride*0]);
03380                 AV_COPY32(mv_dst - 1 +  8, s->current_picture.f.motion_val[list][b_xy + b_stride*1]);
03381                 AV_COPY32(mv_dst - 1 + 16, s->current_picture.f.motion_val[list][b_xy + b_stride*2]);
03382                 AV_COPY32(mv_dst - 1 + 24, s->current_picture.f.motion_val[list][b_xy + b_stride*3]);
03383                 ref_cache[-1 +  0]=
03384                 ref_cache[-1 +  8]= ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 2*0]];
03385                 ref_cache[-1 + 16]=
03386                 ref_cache[-1 + 24]= ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 2*1]];
03387             }else{
03388                 AV_ZERO32(mv_dst - 1 + 0);
03389                 AV_ZERO32(mv_dst - 1 + 8);
03390                 AV_ZERO32(mv_dst - 1 +16);
03391                 AV_ZERO32(mv_dst - 1 +24);
03392                 ref_cache[-1 +  0]=
03393                 ref_cache[-1 +  8]=
03394                 ref_cache[-1 + 16]=
03395                 ref_cache[-1 + 24]= LIST_NOT_USED;
03396             }
03397         }
03398     }
03399 
03400     if(!USES_LIST(mb_type, list)){
03401         fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0,0), 4);
03402         AV_WN32A(&ref_cache[0*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
03403         AV_WN32A(&ref_cache[1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
03404         AV_WN32A(&ref_cache[2*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
03405         AV_WN32A(&ref_cache[3*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
03406         return;
03407     }
03408 
03409     {
03410         int8_t *ref = &s->current_picture.f.ref_index[list][4*mb_xy];
03411         int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
03412         uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101;
03413         uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]],ref2frm[list][ref[3]])&0x00FF00FF)*0x0101;
03414         AV_WN32A(&ref_cache[0*8], ref01);
03415         AV_WN32A(&ref_cache[1*8], ref01);
03416         AV_WN32A(&ref_cache[2*8], ref23);
03417         AV_WN32A(&ref_cache[3*8], ref23);
03418     }
03419 
03420     {
03421         int16_t (*mv_src)[2] = &s->current_picture.f.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
03422         AV_COPY128(mv_dst + 8*0, mv_src + 0*b_stride);
03423         AV_COPY128(mv_dst + 8*1, mv_src + 1*b_stride);
03424         AV_COPY128(mv_dst + 8*2, mv_src + 2*b_stride);
03425         AV_COPY128(mv_dst + 8*3, mv_src + 3*b_stride);
03426     }
03427 }
03428 
03433 static int fill_filter_caches(H264Context *h, int mb_type){
03434     MpegEncContext * const s = &h->s;
03435     const int mb_xy= h->mb_xy;
03436     int top_xy, left_xy[LEFT_MBS];
03437     int top_type, left_type[LEFT_MBS];
03438     uint8_t *nnz;
03439     uint8_t *nnz_cache;
03440 
03441     top_xy     = mb_xy  - (s->mb_stride << MB_FIELD);
03442 
03443     /* Wow, what a mess, why didn't they simplify the interlacing & intra
03444      * stuff, I can't imagine that these complex rules are worth it. */
03445 
03446     left_xy[LBOT] = left_xy[LTOP] = mb_xy-1;
03447     if(FRAME_MBAFF){
03448         const int left_mb_field_flag     = IS_INTERLACED(s->current_picture.f.mb_type[mb_xy - 1]);
03449         const int curr_mb_field_flag     = IS_INTERLACED(mb_type);
03450         if(s->mb_y&1){
03451             if (left_mb_field_flag != curr_mb_field_flag) {
03452                 left_xy[LTOP] -= s->mb_stride;
03453             }
03454         }else{
03455             if(curr_mb_field_flag){
03456                 top_xy += s->mb_stride & (((s->current_picture.f.mb_type[top_xy] >> 7) & 1) - 1);
03457             }
03458             if (left_mb_field_flag != curr_mb_field_flag) {
03459                 left_xy[LBOT] += s->mb_stride;
03460             }
03461         }
03462     }
03463 
03464     h->top_mb_xy = top_xy;
03465     h->left_mb_xy[LTOP] = left_xy[LTOP];
03466     h->left_mb_xy[LBOT] = left_xy[LBOT];
03467     {
03468         //for sufficiently low qp, filtering wouldn't do anything
03469         //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
03470         int qp_thresh = h->qp_thresh; //FIXME strictly we should store qp_thresh for each mb of a slice
03471         int qp = s->current_picture.f.qscale_table[mb_xy];
03472         if(qp <= qp_thresh
03473            && (left_xy[LTOP] < 0 || ((qp + s->current_picture.f.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh)
03474            && (top_xy        < 0 || ((qp + s->current_picture.f.qscale_table[top_xy       ] + 1) >> 1) <= qp_thresh)) {
03475             if(!FRAME_MBAFF)
03476                 return 1;
03477             if ((left_xy[LTOP] < 0            || ((qp + s->current_picture.f.qscale_table[left_xy[LBOT]        ] + 1) >> 1) <= qp_thresh) &&
03478                 (top_xy        < s->mb_stride || ((qp + s->current_picture.f.qscale_table[top_xy - s->mb_stride] + 1) >> 1) <= qp_thresh))
03479                 return 1;
03480         }
03481     }
03482 
03483     top_type        = s->current_picture.f.mb_type[top_xy];
03484     left_type[LTOP] = s->current_picture.f.mb_type[left_xy[LTOP]];
03485     left_type[LBOT] = s->current_picture.f.mb_type[left_xy[LBOT]];
03486     if(h->deblocking_filter == 2){
03487         if(h->slice_table[top_xy       ] != h->slice_num) top_type= 0;
03488         if(h->slice_table[left_xy[LBOT]] != h->slice_num) left_type[LTOP]= left_type[LBOT]= 0;
03489     }else{
03490         if(h->slice_table[top_xy       ] == 0xFFFF) top_type= 0;
03491         if(h->slice_table[left_xy[LBOT]] == 0xFFFF) left_type[LTOP]= left_type[LBOT] =0;
03492     }
03493     h->top_type       = top_type;
03494     h->left_type[LTOP]= left_type[LTOP];
03495     h->left_type[LBOT]= left_type[LBOT];
03496 
03497     if(IS_INTRA(mb_type))
03498         return 0;
03499 
03500     fill_filter_caches_inter(h, s, mb_type, top_xy, left_xy, top_type, left_type, mb_xy, 0);
03501     if(h->list_count == 2)
03502         fill_filter_caches_inter(h, s, mb_type, top_xy, left_xy, top_type, left_type, mb_xy, 1);
03503 
03504     nnz = h->non_zero_count[mb_xy];
03505     nnz_cache = h->non_zero_count_cache;
03506     AV_COPY32(&nnz_cache[4+8*1], &nnz[ 0]);
03507     AV_COPY32(&nnz_cache[4+8*2], &nnz[ 4]);
03508     AV_COPY32(&nnz_cache[4+8*3], &nnz[ 8]);
03509     AV_COPY32(&nnz_cache[4+8*4], &nnz[12]);
03510     h->cbp= h->cbp_table[mb_xy];
03511 
03512     if(top_type){
03513         nnz = h->non_zero_count[top_xy];
03514         AV_COPY32(&nnz_cache[4+8*0], &nnz[3*4]);
03515     }
03516 
03517     if(left_type[LTOP]){
03518         nnz = h->non_zero_count[left_xy[LTOP]];
03519         nnz_cache[3+8*1]= nnz[3+0*4];
03520         nnz_cache[3+8*2]= nnz[3+1*4];
03521         nnz_cache[3+8*3]= nnz[3+2*4];
03522         nnz_cache[3+8*4]= nnz[3+3*4];
03523     }
03524 
03525     // CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
03526     if(!CABAC && h->pps.transform_8x8_mode){
03527         if(IS_8x8DCT(top_type)){
03528             nnz_cache[4+8*0]=
03529             nnz_cache[5+8*0]= (h->cbp_table[top_xy] & 0x4000) >> 12;
03530             nnz_cache[6+8*0]=
03531             nnz_cache[7+8*0]= (h->cbp_table[top_xy] & 0x8000) >> 12;
03532         }
03533         if(IS_8x8DCT(left_type[LTOP])){
03534             nnz_cache[3+8*1]=
03535             nnz_cache[3+8*2]= (h->cbp_table[left_xy[LTOP]]&0x2000) >> 12; //FIXME check MBAFF
03536         }
03537         if(IS_8x8DCT(left_type[LBOT])){
03538             nnz_cache[3+8*3]=
03539             nnz_cache[3+8*4]= (h->cbp_table[left_xy[LBOT]]&0x8000) >> 12; //FIXME check MBAFF
03540         }
03541 
03542         if(IS_8x8DCT(mb_type)){
03543             nnz_cache[scan8[0   ]]= nnz_cache[scan8[1   ]]=
03544             nnz_cache[scan8[2   ]]= nnz_cache[scan8[3   ]]= (h->cbp & 0x1000) >> 12;
03545 
03546             nnz_cache[scan8[0+ 4]]= nnz_cache[scan8[1+ 4]]=
03547             nnz_cache[scan8[2+ 4]]= nnz_cache[scan8[3+ 4]]= (h->cbp & 0x2000) >> 12;
03548 
03549             nnz_cache[scan8[0+ 8]]= nnz_cache[scan8[1+ 8]]=
03550             nnz_cache[scan8[2+ 8]]= nnz_cache[scan8[3+ 8]]= (h->cbp & 0x4000) >> 12;
03551 
03552             nnz_cache[scan8[0+12]]= nnz_cache[scan8[1+12]]=
03553             nnz_cache[scan8[2+12]]= nnz_cache[scan8[3+12]]= (h->cbp & 0x8000) >> 12;
03554         }
03555     }
03556 
03557     return 0;
03558 }
03559 
03560 static void loop_filter(H264Context *h, int start_x, int end_x){
03561     MpegEncContext * const s = &h->s;
03562     uint8_t  *dest_y, *dest_cb, *dest_cr;
03563     int linesize, uvlinesize, mb_x, mb_y;
03564     const int end_mb_y= s->mb_y + FRAME_MBAFF;
03565     const int old_slice_type= h->slice_type;
03566     const int pixel_shift = h->pixel_shift;
03567     const int block_h = 16 >> s->chroma_y_shift;
03568 
03569     if(h->deblocking_filter) {
03570         for(mb_x= start_x; mb_x<end_x; mb_x++){
03571             for(mb_y=end_mb_y - FRAME_MBAFF; mb_y<= end_mb_y; mb_y++){
03572                 int mb_xy, mb_type;
03573                 mb_xy = h->mb_xy = mb_x + mb_y*s->mb_stride;
03574                 h->slice_num= h->slice_table[mb_xy];
03575                 mb_type = s->current_picture.f.mb_type[mb_xy];
03576                 h->list_count= h->list_counts[mb_xy];
03577 
03578                 if(FRAME_MBAFF)
03579                     h->mb_mbaff = h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
03580 
03581                 s->mb_x= mb_x;
03582                 s->mb_y= mb_y;
03583                 dest_y  = s->current_picture.f.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize  ) * 16;
03584                 dest_cb = s->current_picture.f.data[1] + (mb_x << pixel_shift) * (8 << CHROMA444) + mb_y * s->uvlinesize * block_h;
03585                 dest_cr = s->current_picture.f.data[2] + (mb_x << pixel_shift) * (8 << CHROMA444) + mb_y * s->uvlinesize * block_h;
03586                     //FIXME simplify above
03587 
03588                 if (MB_FIELD) {
03589                     linesize   = h->mb_linesize   = s->linesize * 2;
03590                     uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
03591                     if(mb_y&1){ //FIXME move out of this function?
03592                         dest_y -= s->linesize*15;
03593                         dest_cb-= s->uvlinesize * (block_h - 1);
03594                         dest_cr-= s->uvlinesize * (block_h - 1);
03595                     }
03596                 } else {
03597                     linesize   = h->mb_linesize   = s->linesize;
03598                     uvlinesize = h->mb_uvlinesize = s->uvlinesize;
03599                 }
03600                 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
03601                 if(fill_filter_caches(h, mb_type))
03602                     continue;
03603                 h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.f.qscale_table[mb_xy]);
03604                 h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.f.qscale_table[mb_xy]);
03605 
03606                 if (FRAME_MBAFF) {
03607                     ff_h264_filter_mb     (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
03608                 } else {
03609                     ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
03610                 }
03611             }
03612         }
03613     }
03614     h->slice_type= old_slice_type;
03615     s->mb_x= end_x;
03616     s->mb_y= end_mb_y - FRAME_MBAFF;
03617     h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
03618     h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
03619 }
03620 
03621 static void predict_field_decoding_flag(H264Context *h){
03622     MpegEncContext * const s = &h->s;
03623     const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
03624     int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
03625                 ? s->current_picture.f.mb_type[mb_xy - 1]
03626                 : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
03627                 ? s->current_picture.f.mb_type[mb_xy - s->mb_stride]
03628                 : 0;
03629     h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
03630 }
03631 
03635 static void decode_finish_row(H264Context *h){
03636     MpegEncContext * const s = &h->s;
03637     int top = 16*(s->mb_y >> FIELD_PICTURE);
03638     int height = 16 << FRAME_MBAFF;
03639     int deblock_border = (16 + 4) << FRAME_MBAFF;
03640     int pic_height = 16*s->mb_height >> FIELD_PICTURE;
03641 
03642     if (h->deblocking_filter) {
03643         if((top + height) >= pic_height)
03644             height += deblock_border;
03645 
03646         top -= deblock_border;
03647     }
03648 
03649     if (top >= pic_height || (top + height) < h->emu_edge_height)
03650         return;
03651 
03652     height = FFMIN(height, pic_height - top);
03653     if (top < h->emu_edge_height) {
03654         height = top+height;
03655         top = 0;
03656     }
03657 
03658     ff_draw_horiz_band(s, top, height);
03659 
03660     if (s->dropable) return;
03661 
03662     ff_thread_report_progress((AVFrame*)s->current_picture_ptr, top + height - 1,
03663                              s->picture_structure==PICT_BOTTOM_FIELD);
03664 }
03665 
03666 static int decode_slice(struct AVCodecContext *avctx, void *arg){
03667     H264Context *h = *(void**)arg;
03668     MpegEncContext * const s = &h->s;
03669     const int part_mask= s->partitioned_frame ? (ER_AC_END|ER_AC_ERROR) : 0x7F;
03670     int lf_x_start = s->mb_x;
03671 
03672     s->mb_skip_run= -1;
03673 
03674     h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
03675                     (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
03676 
03677     if( h->pps.cabac ) {
03678         /* realign */
03679         align_get_bits( &s->gb );
03680 
03681         /* init cabac */
03682         ff_init_cabac_states( &h->cabac);
03683         ff_init_cabac_decoder( &h->cabac,
03684                                s->gb.buffer + get_bits_count(&s->gb)/8,
03685                                (get_bits_left(&s->gb) + 7)/8);
03686 
03687         ff_h264_init_cabac_states(h);
03688 
03689         for(;;){
03690 //START_TIMER
03691             int ret = ff_h264_decode_mb_cabac(h);
03692             int eos;
03693 //STOP_TIMER("decode_mb_cabac")
03694 
03695             if(ret>=0) ff_h264_hl_decode_mb(h);
03696 
03697             if( ret >= 0 && FRAME_MBAFF ) { //FIXME optimal? or let mb_decode decode 16x32 ?
03698                 s->mb_y++;
03699 
03700                 ret = ff_h264_decode_mb_cabac(h);
03701 
03702                 if(ret>=0) ff_h264_hl_decode_mb(h);
03703                 s->mb_y--;
03704             }
03705             eos = get_cabac_terminate( &h->cabac );
03706 
03707             if((s->workaround_bugs & FF_BUG_TRUNCATED) && h->cabac.bytestream > h->cabac.bytestream_end + 2){
03708                 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END&part_mask);
03709                 if (s->mb_x >= lf_x_start) loop_filter(h, lf_x_start, s->mb_x + 1);
03710                 return 0;
03711             }
03712             if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
03713                 av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d, bytestream (%td)\n", s->mb_x, s->mb_y, h->cabac.bytestream_end - h->cabac.bytestream);
03714                 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR&part_mask);
03715                 return -1;
03716             }
03717 
03718             if( ++s->mb_x >= s->mb_width ) {
03719                 loop_filter(h, lf_x_start, s->mb_x);
03720                 s->mb_x = lf_x_start = 0;
03721                 decode_finish_row(h);
03722                 ++s->mb_y;
03723                 if(FIELD_OR_MBAFF_PICTURE) {
03724                     ++s->mb_y;
03725                     if(FRAME_MBAFF && s->mb_y < s->mb_height)
03726                         predict_field_decoding_flag(h);
03727                 }
03728             }
03729 
03730             if( eos || s->mb_y >= s->mb_height ) {
03731                 tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
03732                 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END&part_mask);
03733                 if (s->mb_x > lf_x_start) loop_filter(h, lf_x_start, s->mb_x);
03734                 return 0;
03735             }
03736         }
03737 
03738     } else {
03739         for(;;){
03740             int ret = ff_h264_decode_mb_cavlc(h);
03741 
03742             if(ret>=0) ff_h264_hl_decode_mb(h);
03743 
03744             if(ret>=0 && FRAME_MBAFF){ //FIXME optimal? or let mb_decode decode 16x32 ?
03745                 s->mb_y++;
03746                 ret = ff_h264_decode_mb_cavlc(h);
03747 
03748                 if(ret>=0) ff_h264_hl_decode_mb(h);
03749                 s->mb_y--;
03750             }
03751 
03752             if(ret<0){
03753                 av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
03754                 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR&part_mask);
03755                 return -1;
03756             }
03757 
03758             if(++s->mb_x >= s->mb_width){
03759                 loop_filter(h, lf_x_start, s->mb_x);
03760                 s->mb_x = lf_x_start = 0;
03761                 decode_finish_row(h);
03762                 ++s->mb_y;
03763                 if(FIELD_OR_MBAFF_PICTURE) {
03764                     ++s->mb_y;
03765                     if(FRAME_MBAFF && s->mb_y < s->mb_height)
03766                         predict_field_decoding_flag(h);
03767                 }
03768                 if(s->mb_y >= s->mb_height){
03769                     tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
03770 
03771                     if (   get_bits_left(&s->gb) == 0
03772                         || get_bits_left(&s->gb) > 0 && !(s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
03773                         ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END&part_mask);
03774 
03775                         return 0;
03776                     }else{
03777                         ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_END&part_mask);
03778 
03779                         return -1;
03780                     }
03781                 }
03782             }
03783 
03784             if (get_bits_left(&s->gb) <= 0 && s->mb_skip_run <= 0){
03785                 tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
03786                 if (get_bits_left(&s->gb) == 0) {
03787                     ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END&part_mask);
03788                     if (s->mb_x > lf_x_start) loop_filter(h, lf_x_start, s->mb_x);
03789 
03790                     return 0;
03791                 }else{
03792                     ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR&part_mask);
03793 
03794                     return -1;
03795                 }
03796             }
03797         }
03798     }
03799 }
03800 
03807 static int execute_decode_slices(H264Context *h, int context_count){
03808     MpegEncContext * const s = &h->s;
03809     AVCodecContext * const avctx= s->avctx;
03810     H264Context *hx;
03811     int i;
03812 
03813     if (s->avctx->hwaccel || s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
03814         return 0;
03815     if(context_count == 1) {
03816         return decode_slice(avctx, &h);
03817     } else {
03818         for(i = 1; i < context_count; i++) {
03819             hx = h->thread_context[i];
03820             hx->s.err_recognition = avctx->err_recognition;
03821             hx->s.error_count = 0;
03822             hx->x264_build= h->x264_build;
03823         }
03824 
03825         avctx->execute(avctx, decode_slice,
03826                        h->thread_context, NULL, context_count, sizeof(void*));
03827 
03828         /* pull back stuff from slices to master context */
03829         hx = h->thread_context[context_count - 1];
03830         s->mb_x = hx->s.mb_x;
03831         s->mb_y = hx->s.mb_y;
03832         s->dropable = hx->s.dropable;
03833         s->picture_structure = hx->s.picture_structure;
03834         for(i = 1; i < context_count; i++)
03835             h->s.error_count += h->thread_context[i]->s.error_count;
03836     }
03837 
03838     return 0;
03839 }
03840 
03841 
03842 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
03843     MpegEncContext * const s = &h->s;
03844     AVCodecContext * const avctx= s->avctx;
03845     H264Context *hx; 
03846     int buf_index;
03847     int context_count;
03848     int next_avc;
03849     int pass = !(avctx->active_thread_type & FF_THREAD_FRAME);
03850     int nals_needed=0; 
03851     int nal_index;
03852 
03853     h->nal_unit_type= 0;
03854 
03855     if(!s->slice_context_count)
03856          s->slice_context_count= 1;
03857     h->max_contexts = s->slice_context_count;
03858 
03859     if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
03860         h->current_slice = 0;
03861         if (!s->first_field)
03862             s->current_picture_ptr= NULL;
03863         ff_h264_reset_sei(h);
03864     }
03865 
03866     for(;pass <= 1;pass++){
03867         buf_index = 0;
03868         context_count = 0;
03869         next_avc = h->is_avc ? 0 : buf_size;
03870         nal_index = 0;
03871     for(;;){
03872         int consumed;
03873         int dst_length;
03874         int bit_length;
03875         const uint8_t *ptr;
03876         int i, nalsize = 0;
03877         int err;
03878 
03879         if(buf_index >= next_avc) {
03880             if (buf_index >= buf_size - h->nal_length_size) break;
03881             nalsize = 0;
03882             for(i = 0; i < h->nal_length_size; i++)
03883                 nalsize = (nalsize << 8) | buf[buf_index++];
03884             if(nalsize <= 0 || nalsize > buf_size - buf_index){
03885                 av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
03886                 break;
03887             }
03888             next_avc= buf_index + nalsize;
03889         } else {
03890             // start code prefix search
03891             for(; buf_index + 3 < next_avc; buf_index++){
03892                 // This should always succeed in the first iteration.
03893                 if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
03894                     break;
03895             }
03896 
03897 
03898             if (buf_index + 3 >= buf_size) {
03899                 buf_index = buf_size;
03900                 break;
03901             }
03902 
03903             buf_index+=3;
03904             if(buf_index >= next_avc) continue;
03905         }
03906 
03907         hx = h->thread_context[context_count];
03908 
03909         ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);
03910         if (ptr == NULL || dst_length < 0) {
03911             buf_index = -1;
03912             goto end;
03913         }
03914         i= buf_index + consumed;
03915         if((s->workaround_bugs & FF_BUG_AUTODETECT) && i+3<next_avc &&
03916            buf[i]==0x00 && buf[i+1]==0x00 && buf[i+2]==0x01 && buf[i+3]==0xE0)
03917             s->workaround_bugs |= FF_BUG_TRUNCATED;
03918 
03919         if(!(s->workaround_bugs & FF_BUG_TRUNCATED)){
03920         while(dst_length > 0 && ptr[dst_length - 1] == 0)
03921             dst_length--;
03922         }
03923         bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
03924 
03925         if(s->avctx->debug&FF_DEBUG_STARTCODE){
03926             av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d/%d at %d/%d length %d pass %d\n", hx->nal_unit_type, hx->nal_ref_idc, buf_index, buf_size, dst_length, pass);
03927         }
03928 
03929         if (h->is_avc && (nalsize != consumed) && nalsize){
03930             av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
03931         }
03932 
03933         buf_index += consumed;
03934         nal_index++;
03935 
03936         if(pass == 0) {
03937             // packets can sometimes contain multiple PPS/SPS
03938             // e.g. two PAFF field pictures in one packet, or a demuxer which splits NALs strangely
03939             // if so, when frame threading we can't start the next thread until we've read all of them
03940             switch (hx->nal_unit_type) {
03941                 case NAL_SPS:
03942                 case NAL_PPS:
03943                     nals_needed = nal_index;
03944                     break;
03945                 case NAL_IDR_SLICE:
03946                 case NAL_SLICE:
03947                     init_get_bits(&hx->s.gb, ptr, bit_length);
03948                     if (!get_ue_golomb(&hx->s.gb))
03949                         nals_needed = nal_index;
03950             }
03951             continue;
03952         }
03953 
03954         //FIXME do not discard SEI id
03955         if(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc  == 0)
03956             continue;
03957 
03958       again:
03959         err = 0;
03960         switch(hx->nal_unit_type){
03961         case NAL_IDR_SLICE:
03962             if (h->nal_unit_type != NAL_IDR_SLICE) {
03963                 av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices\n");
03964                 buf_index = -1;
03965                 goto end;
03966             }
03967             idr(h); // FIXME ensure we don't lose some frames if there is reordering
03968         case NAL_SLICE:
03969             init_get_bits(&hx->s.gb, ptr, bit_length);
03970             hx->intra_gb_ptr=
03971             hx->inter_gb_ptr= &hx->s.gb;
03972             hx->s.data_partitioning = 0;
03973 
03974             if((err = decode_slice_header(hx, h)))
03975                break;
03976 
03977             if (   h->sei_recovery_frame_cnt >= 0
03978                 && (   h->recovery_frame<0
03979                     || ((h->recovery_frame - h->frame_num) & ((1 << h->sps.log2_max_frame_num)-1)) > h->sei_recovery_frame_cnt)) {
03980                 h->recovery_frame = (h->frame_num + h->sei_recovery_frame_cnt) %
03981                                     (1 << h->sps.log2_max_frame_num);
03982             }
03983 
03984             s->current_picture_ptr->f.key_frame |=
03985                     (hx->nal_unit_type == NAL_IDR_SLICE);
03986 
03987             if (h->recovery_frame == h->frame_num) {
03988                 s->current_picture_ptr->sync |= 1;
03989                 h->recovery_frame = -1;
03990             }
03991 
03992             h->sync |= !!s->current_picture_ptr->f.key_frame;
03993             h->sync |= 3*!!(s->flags2 & CODEC_FLAG2_SHOW_ALL);
03994             s->current_picture_ptr->sync |= h->sync;
03995 
03996             if (h->current_slice == 1) {
03997                 if(!(s->flags2 & CODEC_FLAG2_CHUNKS)) {
03998                     decode_postinit(h, nal_index >= nals_needed);
03999                 }
04000 
04001                 if (s->avctx->hwaccel && s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
04002                     return -1;
04003                 if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
04004                     ff_vdpau_h264_picture_start(s);
04005             }
04006 
04007             if(hx->redundant_pic_count==0
04008                && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
04009                && (avctx->skip_frame < AVDISCARD_BIDIR  || hx->slice_type_nos!=AV_PICTURE_TYPE_B)
04010                && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==AV_PICTURE_TYPE_I)
04011                && avctx->skip_frame < AVDISCARD_ALL){
04012                 if(avctx->hwaccel) {
04013                     if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
04014                         return -1;
04015                 }else
04016                 if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
04017                     static const uint8_t start_code[] = {0x00, 0x00, 0x01};
04018                     ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
04019                     ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
04020                 }else
04021                     context_count++;
04022             }
04023             break;
04024         case NAL_DPA:
04025             init_get_bits(&hx->s.gb, ptr, bit_length);
04026             hx->intra_gb_ptr=
04027             hx->inter_gb_ptr= NULL;
04028 
04029             if ((err = decode_slice_header(hx, h)) < 0)
04030                 break;
04031 
04032             hx->s.data_partitioning = 1;
04033 
04034             break;
04035         case NAL_DPB:
04036             init_get_bits(&hx->intra_gb, ptr, bit_length);
04037             hx->intra_gb_ptr= &hx->intra_gb;
04038             break;
04039         case NAL_DPC:
04040             init_get_bits(&hx->inter_gb, ptr, bit_length);
04041             hx->inter_gb_ptr= &hx->inter_gb;
04042 
04043             if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
04044                && s->context_initialized
04045                && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
04046                && (avctx->skip_frame < AVDISCARD_BIDIR  || hx->slice_type_nos!=AV_PICTURE_TYPE_B)
04047                && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==AV_PICTURE_TYPE_I)
04048                && avctx->skip_frame < AVDISCARD_ALL)
04049                 context_count++;
04050             break;
04051         case NAL_SEI:
04052             init_get_bits(&s->gb, ptr, bit_length);
04053             ff_h264_decode_sei(h);
04054             break;
04055         case NAL_SPS:
04056             init_get_bits(&s->gb, ptr, bit_length);
04057             if (ff_h264_decode_seq_parameter_set(h) < 0 && (h->is_avc ? (nalsize != consumed) && nalsize : 1)){
04058                 av_log(h->s.avctx, AV_LOG_DEBUG, "SPS decoding failure, trying alternative mode\n");
04059                 if(h->is_avc) av_assert0(next_avc - buf_index + consumed == nalsize);
04060                 init_get_bits(&s->gb, &buf[buf_index + 1 - consumed], 8*(next_avc - buf_index + consumed - 1));
04061                 ff_h264_decode_seq_parameter_set(h);
04062             }
04063 
04064             if (s->flags& CODEC_FLAG_LOW_DELAY ||
04065                 (h->sps.bitstream_restriction_flag && !h->sps.num_reorder_frames))
04066                 s->low_delay=1;
04067 
04068             if(avctx->has_b_frames < 2)
04069                 avctx->has_b_frames= !s->low_delay;
04070             break;
04071         case NAL_PPS:
04072             init_get_bits(&s->gb, ptr, bit_length);
04073 
04074             ff_h264_decode_picture_parameter_set(h, bit_length);
04075 
04076             break;
04077         case NAL_AUD:
04078         case NAL_END_SEQUENCE:
04079         case NAL_END_STREAM:
04080         case NAL_FILLER_DATA:
04081         case NAL_SPS_EXT:
04082         case NAL_AUXILIARY_SLICE:
04083             break;
04084         default:
04085             av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", hx->nal_unit_type, bit_length);
04086         }
04087 
04088         if(context_count == h->max_contexts) {
04089             execute_decode_slices(h, context_count);
04090             context_count = 0;
04091         }
04092 
04093         if (err < 0)
04094             av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
04095         else if(err == 1) {
04096             /* Slice could not be decoded in parallel mode, copy down
04097              * NAL unit stuff to context 0 and restart. Note that
04098              * rbsp_buffer is not transferred, but since we no longer
04099              * run in parallel mode this should not be an issue. */
04100             h->nal_unit_type = hx->nal_unit_type;
04101             h->nal_ref_idc   = hx->nal_ref_idc;
04102             hx = h;
04103             goto again;
04104         }
04105     }
04106     }
04107     if(context_count)
04108         execute_decode_slices(h, context_count);
04109 
04110 end:
04111     /* clean up */
04112     if (s->current_picture_ptr && s->current_picture_ptr->owner2 == s &&
04113         !s->dropable) {
04114         ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX,
04115                                   s->picture_structure == PICT_BOTTOM_FIELD);
04116     }
04117 
04118     return buf_index;
04119 }
04120 
04124 static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
04125         if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
04126         if(pos+10>buf_size) pos=buf_size; // oops ;)
04127 
04128         return pos;
04129 }
04130 
04131 static int decode_frame(AVCodecContext *avctx,
04132                              void *data, int *data_size,
04133                              AVPacket *avpkt)
04134 {
04135     const uint8_t *buf = avpkt->data;
04136     int buf_size = avpkt->size;
04137     H264Context *h = avctx->priv_data;
04138     MpegEncContext *s = &h->s;
04139     AVFrame *pict = data;
04140     int buf_index = 0;
04141     Picture *out;
04142     int i, out_idx;
04143 
04144     s->flags= avctx->flags;
04145     s->flags2= avctx->flags2;
04146 
04147    /* end of stream, output what is still in the buffers */
04148     if (buf_size == 0) {
04149  out:
04150 
04151         s->current_picture_ptr = NULL;
04152 
04153 //FIXME factorize this with the output code below
04154         out = h->delayed_pic[0];
04155         out_idx = 0;
04156         for (i = 1; h->delayed_pic[i] && !h->delayed_pic[i]->f.key_frame && !h->delayed_pic[i]->mmco_reset; i++)
04157             if(h->delayed_pic[i]->poc < out->poc){
04158                 out = h->delayed_pic[i];
04159                 out_idx = i;
04160             }
04161 
04162         for(i=out_idx; h->delayed_pic[i]; i++)
04163             h->delayed_pic[i] = h->delayed_pic[i+1];
04164 
04165         if(out){
04166             *data_size = sizeof(AVFrame);
04167             *pict= *(AVFrame*)out;
04168         }
04169 
04170         return buf_index;
04171     }
04172     if(h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC && (buf[5]&0x1F) && buf[8]==0x67){
04173         int cnt= buf[5]&0x1f;
04174         uint8_t *p= buf+6;
04175         while(cnt--){
04176             int nalsize= AV_RB16(p) + 2;
04177             if(nalsize > buf_size - (p-buf) || p[2]!=0x67)
04178                 goto not_extra;
04179             p += nalsize;
04180         }
04181         cnt = *(p++);
04182         if(!cnt)
04183             goto not_extra;
04184         while(cnt--){
04185             int nalsize= AV_RB16(p) + 2;
04186             if(nalsize > buf_size - (p-buf) || p[2]!=0x68)
04187                 goto not_extra;
04188             p += nalsize;
04189         }
04190 
04191         return ff_h264_decode_extradata(h, buf, buf_size);
04192     }
04193 not_extra:
04194 
04195     buf_index=decode_nal_units(h, buf, buf_size);
04196     if(buf_index < 0)
04197         return -1;
04198 
04199     if (!s->current_picture_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
04200         av_assert0(buf_index <= buf_size);
04201         goto out;
04202     }
04203 
04204     if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
04205         if (avctx->skip_frame >= AVDISCARD_NONREF ||
04206             buf_size >= 4 && !memcmp("Q264", buf, 4))
04207             return buf_size;
04208         av_log(avctx, AV_LOG_ERROR, "no frame!\n");
04209         return -1;
04210     }
04211 
04212     if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
04213 
04214         if(s->flags2 & CODEC_FLAG2_CHUNKS) decode_postinit(h, 1);
04215 
04216         field_end(h, 0);
04217 
04218         *data_size = 0; /* Wait for second field. */
04219         if (h->next_output_pic && (h->next_output_pic->sync || h->sync>1)) {
04220                 *data_size = sizeof(AVFrame);
04221                 *pict = *(AVFrame*)h->next_output_pic;
04222         }
04223     }
04224 
04225     assert(pict->data[0] || !*data_size);
04226     ff_print_debug_info(s, pict);
04227 //printf("out %d\n", (int)pict->data[0]);
04228 
04229     return get_consumed_bytes(s, buf_index, buf_size);
04230 }
04231 #if 0
04232 static inline void fill_mb_avail(H264Context *h){
04233     MpegEncContext * const s = &h->s;
04234     const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
04235 
04236     if(s->mb_y){
04237         h->mb_avail[0]= s->mb_x                 && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
04238         h->mb_avail[1]=                            h->slice_table[mb_xy - s->mb_stride    ] == h->slice_num;
04239         h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
04240     }else{
04241         h->mb_avail[0]=
04242         h->mb_avail[1]=
04243         h->mb_avail[2]= 0;
04244     }
04245     h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
04246     h->mb_avail[4]= 1; //FIXME move out
04247     h->mb_avail[5]= 0; //FIXME move out
04248 }
04249 #endif
04250 
04251 #ifdef TEST
04252 #undef printf
04253 #undef random
04254 #define COUNT 8000
04255 #define SIZE (COUNT*40)
04256 extern AVCodec ff_h264_decoder;
04257 int main(void){
04258     int i;
04259     uint8_t temp[SIZE];
04260     PutBitContext pb;
04261     GetBitContext gb;
04262     DSPContext dsp;
04263     AVCodecContext avctx;
04264 
04265     avcodec_get_context_defaults3(&avctx, &ff_h264_decoder);
04266 
04267     dsputil_init(&dsp, &avctx);
04268 
04269     init_put_bits(&pb, temp, SIZE);
04270     printf("testing unsigned exp golomb\n");
04271     for(i=0; i<COUNT; i++){
04272         START_TIMER
04273         set_ue_golomb(&pb, i);
04274         STOP_TIMER("set_ue_golomb");
04275     }
04276     flush_put_bits(&pb);
04277 
04278     init_get_bits(&gb, temp, 8*SIZE);
04279     for(i=0; i<COUNT; i++){
04280         int j, s = show_bits(&gb, 24);
04281 
04282         {START_TIMER
04283         j= get_ue_golomb(&gb);
04284         if(j != i){
04285             printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
04286 //            return -1;
04287         }
04288         STOP_TIMER("get_ue_golomb");}
04289     }
04290 
04291 
04292     init_put_bits(&pb, temp, SIZE);
04293     printf("testing signed exp golomb\n");
04294     for(i=0; i<COUNT; i++){
04295         START_TIMER
04296         set_se_golomb(&pb, i - COUNT/2);
04297         STOP_TIMER("set_se_golomb");
04298     }
04299     flush_put_bits(&pb);
04300 
04301     init_get_bits(&gb, temp, 8*SIZE);
04302     for(i=0; i<COUNT; i++){
04303         int j, s = show_bits(&gb, 24);
04304 
04305         {START_TIMER
04306         j= get_se_golomb(&gb);
04307         if(j != i - COUNT/2){
04308             printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
04309 //            return -1;
04310         }
04311         STOP_TIMER("get_se_golomb");}
04312     }
04313 
04314     printf("Testing RBSP\n");
04315 
04316 
04317     return 0;
04318 }
04319 #endif /* TEST */
04320 
04321 
04322 av_cold void ff_h264_free_context(H264Context *h)
04323 {
04324     int i;
04325 
04326     free_tables(h, 1); //FIXME cleanup init stuff perhaps
04327 
04328     for(i = 0; i < MAX_SPS_COUNT; i++)
04329         av_freep(h->sps_buffers + i);
04330 
04331     for(i = 0; i < MAX_PPS_COUNT; i++)
04332         av_freep(h->pps_buffers + i);
04333 }
04334 
04335 av_cold int ff_h264_decode_end(AVCodecContext *avctx)
04336 {
04337     H264Context *h = avctx->priv_data;
04338     MpegEncContext *s = &h->s;
04339 
04340     ff_h264_remove_all_refs(h);
04341     ff_h264_free_context(h);
04342 
04343     MPV_common_end(s);
04344 
04345 //    memset(h, 0, sizeof(H264Context));
04346 
04347     return 0;
04348 }
04349 
04350 static const AVProfile profiles[] = {
04351     { FF_PROFILE_H264_BASELINE,             "Baseline"              },
04352     { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline"  },
04353     { FF_PROFILE_H264_MAIN,                 "Main"                  },
04354     { FF_PROFILE_H264_EXTENDED,             "Extended"              },
04355     { FF_PROFILE_H264_HIGH,                 "High"                  },
04356     { FF_PROFILE_H264_HIGH_10,              "High 10"               },
04357     { FF_PROFILE_H264_HIGH_10_INTRA,        "High 10 Intra"         },
04358     { FF_PROFILE_H264_HIGH_422,             "High 4:2:2"            },
04359     { FF_PROFILE_H264_HIGH_422_INTRA,       "High 4:2:2 Intra"      },
04360     { FF_PROFILE_H264_HIGH_444,             "High 4:4:4"            },
04361     { FF_PROFILE_H264_HIGH_444_PREDICTIVE,  "High 4:4:4 Predictive" },
04362     { FF_PROFILE_H264_HIGH_444_INTRA,       "High 4:4:4 Intra"      },
04363     { FF_PROFILE_H264_CAVLC_444,            "CAVLC 4:4:4"           },
04364     { FF_PROFILE_UNKNOWN },
04365 };
04366 
04367 static const AVOption h264_options[] = {
04368     {"is_avc", "is avc", offsetof(H264Context, is_avc), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 1, 0},
04369     {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 4, 0},
04370     {NULL}
04371 };
04372 
04373 static const AVClass h264_class = {
04374     "H264 Decoder",
04375     av_default_item_name,
04376     h264_options,
04377     LIBAVUTIL_VERSION_INT,
04378 };
04379 
04380 static const AVClass h264_vdpau_class = {
04381     "H264 VDPAU Decoder",
04382     av_default_item_name,
04383     h264_options,
04384     LIBAVUTIL_VERSION_INT,
04385 };
04386 
04387 AVCodec ff_h264_decoder = {
04388     .name           = "h264",
04389     .type           = AVMEDIA_TYPE_VIDEO,
04390     .id             = CODEC_ID_H264,
04391     .priv_data_size = sizeof(H264Context),
04392     .init           = ff_h264_decode_init,
04393     .close          = ff_h264_decode_end,
04394     .decode         = decode_frame,
04395     .capabilities   = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY |
04396                       CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS,
04397     .flush= flush_dpb,
04398     .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
04399     .init_thread_copy      = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
04400     .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
04401     .profiles = NULL_IF_CONFIG_SMALL(profiles),
04402     .priv_class     = &h264_class,
04403 };
04404 
04405 #if CONFIG_H264_VDPAU_DECODER
04406 AVCodec ff_h264_vdpau_decoder = {
04407     .name           = "h264_vdpau",
04408     .type           = AVMEDIA_TYPE_VIDEO,
04409     .id             = CODEC_ID_H264,
04410     .priv_data_size = sizeof(H264Context),
04411     .init           = ff_h264_decode_init,
04412     .close          = ff_h264_decode_end,
04413     .decode         = decode_frame,
04414     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
04415     .flush= flush_dpb,
04416     .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
04417     .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},
04418     .profiles = NULL_IF_CONFIG_SMALL(profiles),
04419     .priv_class     = &h264_vdpau_class,
04420 };
04421 #endif
Generated on Fri Feb 1 2013 14:34:35 for FFmpeg by doxygen 1.7.1