00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavcore/imgutils.h"
00029 #include "internal.h"
00030 #include "dsputil.h"
00031 #include "avcodec.h"
00032 #include "mpegvideo.h"
00033 #include "h264.h"
00034 #include "h264data.h"
00035 #include "h264_mvpred.h"
00036 #include "h264_parser.h"
00037 #include "golomb.h"
00038 #include "mathops.h"
00039 #include "rectangle.h"
00040 #include "vdpau_internal.h"
00041 #include "libavutil/avassert.h"
00042
00043 #include "cabac.h"
00044
00045
00046 #include <assert.h>
00047
00048 static const uint8_t rem6[52]={
00049 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,
00050 };
00051
00052 static const uint8_t div6[52]={
00053 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,
00054 };
00055
00056 static const enum PixelFormat hwaccel_pixfmt_list_h264_jpeg_420[] = {
00057 PIX_FMT_DXVA2_VLD,
00058 PIX_FMT_VAAPI_VLD,
00059 PIX_FMT_YUVJ420P,
00060 PIX_FMT_NONE
00061 };
00062
00063 void ff_h264_write_back_intra_pred_mode(H264Context *h){
00064 int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
00065
00066 AV_COPY32(mode, h->intra4x4_pred_mode_cache + 4 + 8*4);
00067 mode[4]= h->intra4x4_pred_mode_cache[7+8*3];
00068 mode[5]= h->intra4x4_pred_mode_cache[7+8*2];
00069 mode[6]= h->intra4x4_pred_mode_cache[7+8*1];
00070 }
00071
00075 int ff_h264_check_intra4x4_pred_mode(H264Context *h){
00076 MpegEncContext * const s = &h->s;
00077 static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
00078 static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
00079 int i;
00080
00081 if(!(h->top_samples_available&0x8000)){
00082 for(i=0; i<4; i++){
00083 int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
00084 if(status<0){
00085 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);
00086 return -1;
00087 } else if(status){
00088 h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
00089 }
00090 }
00091 }
00092
00093 if((h->left_samples_available&0x8888)!=0x8888){
00094 static const int mask[4]={0x8000,0x2000,0x80,0x20};
00095 for(i=0; i<4; i++){
00096 if(!(h->left_samples_available&mask[i])){
00097 int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
00098 if(status<0){
00099 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);
00100 return -1;
00101 } else if(status){
00102 h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
00103 }
00104 }
00105 }
00106 }
00107
00108 return 0;
00109 }
00110
00114 int ff_h264_check_intra_pred_mode(H264Context *h, int mode){
00115 MpegEncContext * const s = &h->s;
00116 static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
00117 static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
00118
00119 if(mode > 6U) {
00120 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);
00121 return -1;
00122 }
00123
00124 if(!(h->top_samples_available&0x8000)){
00125 mode= top[ mode ];
00126 if(mode<0){
00127 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);
00128 return -1;
00129 }
00130 }
00131
00132 if((h->left_samples_available&0x8080) != 0x8080){
00133 mode= left[ mode ];
00134 if(h->left_samples_available&0x8080){
00135 mode= ALZHEIMER_DC_L0T_PRED8x8 + (!(h->left_samples_available&0x8000)) + 2*(mode == DC_128_PRED8x8);
00136 }
00137 if(mode<0){
00138 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);
00139 return -1;
00140 }
00141 }
00142
00143 return mode;
00144 }
00145
00146 const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src, int *dst_length, int *consumed, int length){
00147 int i, si, di;
00148 uint8_t *dst;
00149 int bufidx;
00150
00151
00152 h->nal_ref_idc= src[0]>>5;
00153 h->nal_unit_type= src[0]&0x1F;
00154
00155 src++; length--;
00156 #if 0
00157 for(i=0; i<length; i++)
00158 printf("%2X ", src[i]);
00159 #endif
00160
00161 #if HAVE_FAST_UNALIGNED
00162 # if HAVE_FAST_64BIT
00163 # define RS 7
00164 for(i=0; i+1<length; i+=9){
00165 if(!((~AV_RN64A(src+i) & (AV_RN64A(src+i) - 0x0100010001000101ULL)) & 0x8000800080008080ULL))
00166 # else
00167 # define RS 3
00168 for(i=0; i+1<length; i+=5){
00169 if(!((~AV_RN32A(src+i) & (AV_RN32A(src+i) - 0x01000101U)) & 0x80008080U))
00170 # endif
00171 continue;
00172 if(i>0 && !src[i]) i--;
00173 while(src[i]) i++;
00174 #else
00175 # define RS 0
00176 for(i=0; i+1<length; i+=2){
00177 if(src[i]) continue;
00178 if(i>0 && src[i-1]==0) i--;
00179 #endif
00180 if(i+2<length && src[i+1]==0 && src[i+2]<=3){
00181 if(src[i+2]!=3){
00182
00183 length=i;
00184 }
00185 break;
00186 }
00187 i-= RS;
00188 }
00189
00190 if(i>=length-1){
00191 *dst_length= length;
00192 *consumed= length+1;
00193 return src;
00194 }
00195
00196 bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0;
00197 av_fast_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+FF_INPUT_BUFFER_PADDING_SIZE);
00198 dst= h->rbsp_buffer[bufidx];
00199
00200 if (dst == NULL){
00201 return NULL;
00202 }
00203
00204
00205 memcpy(dst, src, i);
00206 si=di=i;
00207 while(si+2<length){
00208
00209 if(src[si+2]>3){
00210 dst[di++]= src[si++];
00211 dst[di++]= src[si++];
00212 }else if(src[si]==0 && src[si+1]==0){
00213 if(src[si+2]==3){
00214 dst[di++]= 0;
00215 dst[di++]= 0;
00216 si+=3;
00217 continue;
00218 }else
00219 goto nsc;
00220 }
00221
00222 dst[di++]= src[si++];
00223 }
00224 while(si<length)
00225 dst[di++]= src[si++];
00226 nsc:
00227
00228 memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00229
00230 *dst_length= di;
00231 *consumed= si + 1;
00232
00233 return dst;
00234 }
00235
00236 int ff_h264_decode_rbsp_trailing(H264Context *h, const uint8_t *src){
00237 int v= *src;
00238 int r;
00239
00240 tprintf(h->s.avctx, "rbsp trailing %X\n", v);
00241
00242 for(r=1; r<9; r++){
00243 if(v&1) return r;
00244 v>>=1;
00245 }
00246 return 0;
00247 }
00248
00249 static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
00250 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00251 int src_x_offset, int src_y_offset,
00252 qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
00253 MpegEncContext * const s = &h->s;
00254 const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
00255 int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
00256 const int luma_xy= (mx&3) + ((my&3)<<2);
00257 uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->mb_linesize;
00258 uint8_t * src_cb, * src_cr;
00259 int extra_width= h->emu_edge_width;
00260 int extra_height= h->emu_edge_height;
00261 int emu=0;
00262 const int full_mx= mx>>2;
00263 const int full_my= my>>2;
00264 const int pic_width = 16*s->mb_width;
00265 const int pic_height = 16*s->mb_height >> MB_FIELD;
00266
00267 if(mx&7) extra_width -= 3;
00268 if(my&7) extra_height -= 3;
00269
00270 if( full_mx < 0-extra_width
00271 || full_my < 0-extra_height
00272 || full_mx + 16 > pic_width + extra_width
00273 || full_my + 16 > pic_height + extra_height){
00274 ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5, full_mx-2, full_my-2, pic_width, pic_height);
00275 src_y= s->edge_emu_buffer + 2 + 2*h->mb_linesize;
00276 emu=1;
00277 }
00278
00279 qpix_op[luma_xy](dest_y, src_y, h->mb_linesize);
00280 if(!square){
00281 qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
00282 }
00283
00284 if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
00285
00286 if(MB_FIELD){
00287
00288 my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
00289 emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
00290 }
00291 src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
00292 src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->mb_uvlinesize;
00293
00294 if(emu){
00295 ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize, 9, 9, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
00296 src_cb= s->edge_emu_buffer;
00297 }
00298 chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
00299
00300 if(emu){
00301 ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize, 9, 9, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
00302 src_cr= s->edge_emu_buffer;
00303 }
00304 chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
00305 }
00306
00307 static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
00308 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00309 int x_offset, int y_offset,
00310 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
00311 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
00312 int list0, int list1){
00313 MpegEncContext * const s = &h->s;
00314 qpel_mc_func *qpix_op= qpix_put;
00315 h264_chroma_mc_func chroma_op= chroma_put;
00316
00317 dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
00318 dest_cb += x_offset + y_offset*h->mb_uvlinesize;
00319 dest_cr += x_offset + y_offset*h->mb_uvlinesize;
00320 x_offset += 8*s->mb_x;
00321 y_offset += 8*(s->mb_y >> MB_FIELD);
00322
00323 if(list0){
00324 Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
00325 mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
00326 dest_y, dest_cb, dest_cr, x_offset, y_offset,
00327 qpix_op, chroma_op);
00328
00329 qpix_op= qpix_avg;
00330 chroma_op= chroma_avg;
00331 }
00332
00333 if(list1){
00334 Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
00335 mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
00336 dest_y, dest_cb, dest_cr, x_offset, y_offset,
00337 qpix_op, chroma_op);
00338 }
00339 }
00340
00341 static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
00342 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00343 int x_offset, int y_offset,
00344 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
00345 h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
00346 h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
00347 int list0, int list1){
00348 MpegEncContext * const s = &h->s;
00349
00350 dest_y += 2*x_offset + 2*y_offset*h-> mb_linesize;
00351 dest_cb += x_offset + y_offset*h->mb_uvlinesize;
00352 dest_cr += x_offset + y_offset*h->mb_uvlinesize;
00353 x_offset += 8*s->mb_x;
00354 y_offset += 8*(s->mb_y >> MB_FIELD);
00355
00356 if(list0 && list1){
00357
00358
00359 uint8_t *tmp_cb = s->obmc_scratchpad;
00360 uint8_t *tmp_cr = s->obmc_scratchpad + 8;
00361 uint8_t *tmp_y = s->obmc_scratchpad + 8*h->mb_uvlinesize;
00362 int refn0 = h->ref_cache[0][ scan8[n] ];
00363 int refn1 = h->ref_cache[1][ scan8[n] ];
00364
00365 mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
00366 dest_y, dest_cb, dest_cr,
00367 x_offset, y_offset, qpix_put, chroma_put);
00368 mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
00369 tmp_y, tmp_cb, tmp_cr,
00370 x_offset, y_offset, qpix_put, chroma_put);
00371
00372 if(h->use_weight == 2){
00373 int weight0 = h->implicit_weight[refn0][refn1][s->mb_y&1];
00374 int weight1 = 64 - weight0;
00375 luma_weight_avg( dest_y, tmp_y, h-> mb_linesize, 5, weight0, weight1, 0);
00376 chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
00377 chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
00378 }else{
00379 luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
00380 h->luma_weight[refn0][0][0] , h->luma_weight[refn1][1][0],
00381 h->luma_weight[refn0][0][1] + h->luma_weight[refn1][1][1]);
00382 chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
00383 h->chroma_weight[refn0][0][0][0] , h->chroma_weight[refn1][1][0][0],
00384 h->chroma_weight[refn0][0][0][1] + h->chroma_weight[refn1][1][0][1]);
00385 chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
00386 h->chroma_weight[refn0][0][1][0] , h->chroma_weight[refn1][1][1][0],
00387 h->chroma_weight[refn0][0][1][1] + h->chroma_weight[refn1][1][1][1]);
00388 }
00389 }else{
00390 int list = list1 ? 1 : 0;
00391 int refn = h->ref_cache[list][ scan8[n] ];
00392 Picture *ref= &h->ref_list[list][refn];
00393 mc_dir_part(h, ref, n, square, chroma_height, delta, list,
00394 dest_y, dest_cb, dest_cr, x_offset, y_offset,
00395 qpix_put, chroma_put);
00396
00397 luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
00398 h->luma_weight[refn][list][0], h->luma_weight[refn][list][1]);
00399 if(h->use_weight_chroma){
00400 chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
00401 h->chroma_weight[refn][list][0][0], h->chroma_weight[refn][list][0][1]);
00402 chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
00403 h->chroma_weight[refn][list][1][0], h->chroma_weight[refn][list][1][1]);
00404 }
00405 }
00406 }
00407
00408 static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
00409 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00410 int x_offset, int y_offset,
00411 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
00412 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
00413 h264_weight_func *weight_op, h264_biweight_func *weight_avg,
00414 int list0, int list1){
00415 if((h->use_weight==2 && list0 && list1
00416 && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ][h->s.mb_y&1] != 32))
00417 || h->use_weight==1)
00418 mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
00419 x_offset, y_offset, qpix_put, chroma_put,
00420 weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
00421 else
00422 mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
00423 x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
00424 }
00425
00426 static inline void prefetch_motion(H264Context *h, int list){
00427
00428
00429 MpegEncContext * const s = &h->s;
00430 const int refn = h->ref_cache[list][scan8[0]];
00431 if(refn >= 0){
00432 const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
00433 const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
00434 uint8_t **src= h->ref_list[list][refn].data;
00435 int off= mx + (my + (s->mb_x&3)*4)*h->mb_linesize + 64;
00436 s->dsp.prefetch(src[0]+off, s->linesize, 4);
00437 off= (mx>>1) + ((my>>1) + (s->mb_x&7))*s->uvlinesize + 64;
00438 s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
00439 }
00440 }
00441
00442 static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
00443 qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
00444 qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
00445 h264_weight_func *weight_op, h264_biweight_func *weight_avg){
00446 MpegEncContext * const s = &h->s;
00447 const int mb_xy= h->mb_xy;
00448 const int mb_type= s->current_picture.mb_type[mb_xy];
00449
00450 assert(IS_INTER(mb_type));
00451
00452 prefetch_motion(h, 0);
00453
00454 if(IS_16X16(mb_type)){
00455 mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
00456 qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
00457 weight_op, weight_avg,
00458 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
00459 }else if(IS_16X8(mb_type)){
00460 mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
00461 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
00462 &weight_op[1], &weight_avg[1],
00463 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
00464 mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
00465 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
00466 &weight_op[1], &weight_avg[1],
00467 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
00468 }else if(IS_8X16(mb_type)){
00469 mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
00470 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
00471 &weight_op[2], &weight_avg[2],
00472 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
00473 mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
00474 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
00475 &weight_op[2], &weight_avg[2],
00476 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
00477 }else{
00478 int i;
00479
00480 assert(IS_8X8(mb_type));
00481
00482 for(i=0; i<4; i++){
00483 const int sub_mb_type= h->sub_mb_type[i];
00484 const int n= 4*i;
00485 int x_offset= (i&1)<<2;
00486 int y_offset= (i&2)<<1;
00487
00488 if(IS_SUB_8X8(sub_mb_type)){
00489 mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
00490 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
00491 &weight_op[3], &weight_avg[3],
00492 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
00493 }else if(IS_SUB_8X4(sub_mb_type)){
00494 mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
00495 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
00496 &weight_op[4], &weight_avg[4],
00497 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
00498 mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
00499 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
00500 &weight_op[4], &weight_avg[4],
00501 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
00502 }else if(IS_SUB_4X8(sub_mb_type)){
00503 mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
00504 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
00505 &weight_op[5], &weight_avg[5],
00506 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
00507 mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
00508 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
00509 &weight_op[5], &weight_avg[5],
00510 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
00511 }else{
00512 int j;
00513 assert(IS_SUB_4X4(sub_mb_type));
00514 for(j=0; j<4; j++){
00515 int sub_x_offset= x_offset + 2*(j&1);
00516 int sub_y_offset= y_offset + (j&2);
00517 mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
00518 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
00519 &weight_op[6], &weight_avg[6],
00520 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
00521 }
00522 }
00523 }
00524 }
00525
00526 prefetch_motion(h, 1);
00527 }
00528
00529
00530 static void free_tables(H264Context *h){
00531 int i;
00532 H264Context *hx;
00533 av_freep(&h->intra4x4_pred_mode);
00534 av_freep(&h->chroma_pred_mode_table);
00535 av_freep(&h->cbp_table);
00536 av_freep(&h->mvd_table[0]);
00537 av_freep(&h->mvd_table[1]);
00538 av_freep(&h->direct_table);
00539 av_freep(&h->non_zero_count);
00540 av_freep(&h->slice_table_base);
00541 h->slice_table= NULL;
00542 av_freep(&h->list_counts);
00543
00544 av_freep(&h->mb2b_xy);
00545 av_freep(&h->mb2br_xy);
00546
00547 for(i = 0; i < MAX_THREADS; i++) {
00548 hx = h->thread_context[i];
00549 if(!hx) continue;
00550 av_freep(&hx->top_borders[1]);
00551 av_freep(&hx->top_borders[0]);
00552 av_freep(&hx->s.obmc_scratchpad);
00553 av_freep(&hx->rbsp_buffer[1]);
00554 av_freep(&hx->rbsp_buffer[0]);
00555 hx->rbsp_buffer_size[0] = 0;
00556 hx->rbsp_buffer_size[1] = 0;
00557 if (i) av_freep(&h->thread_context[i]);
00558 }
00559 }
00560
00561 static void init_dequant8_coeff_table(H264Context *h){
00562 int i,q,x;
00563 h->dequant8_coeff[0] = h->dequant8_buffer[0];
00564 h->dequant8_coeff[1] = h->dequant8_buffer[1];
00565
00566 for(i=0; i<2; i++ ){
00567 if(i && !memcmp(h->pps.scaling_matrix8[0], h->pps.scaling_matrix8[1], 64*sizeof(uint8_t))){
00568 h->dequant8_coeff[1] = h->dequant8_buffer[0];
00569 break;
00570 }
00571
00572 for(q=0; q<52; q++){
00573 int shift = div6[q];
00574 int idx = rem6[q];
00575 for(x=0; x<64; x++)
00576 h->dequant8_coeff[i][q][(x>>3)|((x&7)<<3)] =
00577 ((uint32_t)dequant8_coeff_init[idx][ dequant8_coeff_init_scan[((x>>1)&12) | (x&3)] ] *
00578 h->pps.scaling_matrix8[i][x]) << shift;
00579 }
00580 }
00581 }
00582
00583 static void init_dequant4_coeff_table(H264Context *h){
00584 int i,j,q,x;
00585 for(i=0; i<6; i++ ){
00586 h->dequant4_coeff[i] = h->dequant4_buffer[i];
00587 for(j=0; j<i; j++){
00588 if(!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i], 16*sizeof(uint8_t))){
00589 h->dequant4_coeff[i] = h->dequant4_buffer[j];
00590 break;
00591 }
00592 }
00593 if(j<i)
00594 continue;
00595
00596 for(q=0; q<52; q++){
00597 int shift = div6[q] + 2;
00598 int idx = rem6[q];
00599 for(x=0; x<16; x++)
00600 h->dequant4_coeff[i][q][(x>>2)|((x<<2)&0xF)] =
00601 ((uint32_t)dequant4_coeff_init[idx][(x&1) + ((x>>2)&1)] *
00602 h->pps.scaling_matrix4[i][x]) << shift;
00603 }
00604 }
00605 }
00606
00607 static void init_dequant_tables(H264Context *h){
00608 int i,x;
00609 init_dequant4_coeff_table(h);
00610 if(h->pps.transform_8x8_mode)
00611 init_dequant8_coeff_table(h);
00612 if(h->sps.transform_bypass){
00613 for(i=0; i<6; i++)
00614 for(x=0; x<16; x++)
00615 h->dequant4_coeff[i][0][x] = 1<<6;
00616 if(h->pps.transform_8x8_mode)
00617 for(i=0; i<2; i++)
00618 for(x=0; x<64; x++)
00619 h->dequant8_coeff[i][0][x] = 1<<6;
00620 }
00621 }
00622
00623
00624 int ff_h264_alloc_tables(H264Context *h){
00625 MpegEncContext * const s = &h->s;
00626 const int big_mb_num= s->mb_stride * (s->mb_height+1);
00627 const int row_mb_num= 2*s->mb_stride*s->avctx->thread_count;
00628 int x,y;
00629
00630 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode, row_mb_num * 8 * sizeof(uint8_t), fail)
00631
00632 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count , big_mb_num * 32 * sizeof(uint8_t), fail)
00633 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base , (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base), fail)
00634 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table, big_mb_num * sizeof(uint16_t), fail)
00635
00636 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t), fail)
00637 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0], 16*row_mb_num * sizeof(uint8_t), fail);
00638 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1], 16*row_mb_num * sizeof(uint8_t), fail);
00639 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table, 4*big_mb_num * sizeof(uint8_t) , fail);
00640 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->list_counts, big_mb_num * sizeof(uint8_t), fail)
00641
00642 memset(h->slice_table_base, -1, (big_mb_num+s->mb_stride) * sizeof(*h->slice_table_base));
00643 h->slice_table= h->slice_table_base + s->mb_stride*2 + 1;
00644
00645 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy , big_mb_num * sizeof(uint32_t), fail);
00646 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2br_xy , big_mb_num * sizeof(uint32_t), fail);
00647 for(y=0; y<s->mb_height; y++){
00648 for(x=0; x<s->mb_width; x++){
00649 const int mb_xy= x + y*s->mb_stride;
00650 const int b_xy = 4*x + 4*y*h->b_stride;
00651
00652 h->mb2b_xy [mb_xy]= b_xy;
00653 h->mb2br_xy[mb_xy]= 8*(FMO ? mb_xy : (mb_xy % (2*s->mb_stride)));
00654 }
00655 }
00656
00657 s->obmc_scratchpad = NULL;
00658
00659 if(!h->dequant4_coeff[0])
00660 init_dequant_tables(h);
00661
00662 return 0;
00663 fail:
00664 free_tables(h);
00665 return -1;
00666 }
00667
00671 static void clone_tables(H264Context *dst, H264Context *src, int i){
00672 MpegEncContext * const s = &src->s;
00673 dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i*8*2*s->mb_stride;
00674 dst->non_zero_count = src->non_zero_count;
00675 dst->slice_table = src->slice_table;
00676 dst->cbp_table = src->cbp_table;
00677 dst->mb2b_xy = src->mb2b_xy;
00678 dst->mb2br_xy = src->mb2br_xy;
00679 dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
00680 dst->mvd_table[0] = src->mvd_table[0] + i*8*2*s->mb_stride;
00681 dst->mvd_table[1] = src->mvd_table[1] + i*8*2*s->mb_stride;
00682 dst->direct_table = src->direct_table;
00683 dst->list_counts = src->list_counts;
00684
00685 dst->s.obmc_scratchpad = NULL;
00686 ff_h264_pred_init(&dst->hpc, src->s.codec_id);
00687 }
00688
00693 static int context_init(H264Context *h){
00694 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
00695 FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1], h->s.mb_width * (16+8+8) * sizeof(uint8_t), fail)
00696
00697 h->ref_cache[0][scan8[5 ]+1] = h->ref_cache[0][scan8[7 ]+1] = h->ref_cache[0][scan8[13]+1] =
00698 h->ref_cache[1][scan8[5 ]+1] = h->ref_cache[1][scan8[7 ]+1] = h->ref_cache[1][scan8[13]+1] = PART_NOT_AVAILABLE;
00699
00700 return 0;
00701 fail:
00702 return -1;
00703 }
00704
00705 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size);
00706
00707 static av_cold void common_init(H264Context *h){
00708 MpegEncContext * const s = &h->s;
00709
00710 s->width = s->avctx->width;
00711 s->height = s->avctx->height;
00712 s->codec_id= s->avctx->codec->id;
00713
00714 ff_h264dsp_init(&h->h264dsp);
00715 ff_h264_pred_init(&h->hpc, s->codec_id);
00716
00717 h->dequant_coeff_pps= -1;
00718 s->unrestricted_mv=1;
00719 s->decode=1;
00720
00721 dsputil_init(&s->dsp, s->avctx);
00722
00723 memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
00724 memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
00725 }
00726
00727 int ff_h264_decode_extradata(H264Context *h)
00728 {
00729 AVCodecContext *avctx = h->s.avctx;
00730
00731 if(*(char *)avctx->extradata == 1){
00732 int i, cnt, nalsize;
00733 unsigned char *p = avctx->extradata;
00734
00735 h->is_avc = 1;
00736
00737 if(avctx->extradata_size < 7) {
00738 av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
00739 return -1;
00740 }
00741
00742
00743 h->nal_length_size = 2;
00744
00745 cnt = *(p+5) & 0x1f;
00746 p += 6;
00747 for (i = 0; i < cnt; i++) {
00748 nalsize = AV_RB16(p) + 2;
00749 if(decode_nal_units(h, p, nalsize) < 0) {
00750 av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
00751 return -1;
00752 }
00753 p += nalsize;
00754 }
00755
00756 cnt = *(p++);
00757 for (i = 0; i < cnt; i++) {
00758 nalsize = AV_RB16(p) + 2;
00759 if(decode_nal_units(h, p, nalsize) != nalsize) {
00760 av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
00761 return -1;
00762 }
00763 p += nalsize;
00764 }
00765
00766 h->nal_length_size = ((*(((char*)(avctx->extradata))+4))&0x03)+1;
00767 } else {
00768 h->is_avc = 0;
00769 if(decode_nal_units(h, avctx->extradata, avctx->extradata_size) < 0)
00770 return -1;
00771 }
00772 return 0;
00773 }
00774
00775 av_cold int ff_h264_decode_init(AVCodecContext *avctx){
00776 H264Context *h= avctx->priv_data;
00777 MpegEncContext * const s = &h->s;
00778
00779 MPV_decode_defaults(s);
00780
00781 s->avctx = avctx;
00782 common_init(h);
00783
00784 s->out_format = FMT_H264;
00785 s->workaround_bugs= avctx->workaround_bugs;
00786
00787
00788
00789 s->quarter_sample = 1;
00790 if(!avctx->has_b_frames)
00791 s->low_delay= 1;
00792
00793 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
00794
00795 ff_h264_decode_init_vlc();
00796
00797 h->thread_context[0] = h;
00798 h->outputed_poc = INT_MIN;
00799 h->prev_poc_msb= 1<<16;
00800 h->x264_build = -1;
00801 ff_h264_reset_sei(h);
00802 if(avctx->codec_id == CODEC_ID_H264){
00803 if(avctx->ticks_per_frame == 1){
00804 s->avctx->time_base.den *=2;
00805 }
00806 avctx->ticks_per_frame = 2;
00807 }
00808
00809 if(avctx->extradata_size > 0 && avctx->extradata &&
00810 ff_h264_decode_extradata(h))
00811 return -1;
00812
00813 if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames < h->sps.num_reorder_frames){
00814 s->avctx->has_b_frames = h->sps.num_reorder_frames;
00815 s->low_delay = 0;
00816 }
00817
00818 return 0;
00819 }
00820
00821 int ff_h264_frame_start(H264Context *h){
00822 MpegEncContext * const s = &h->s;
00823 int i;
00824
00825 if(MPV_frame_start(s, s->avctx) < 0)
00826 return -1;
00827 ff_er_frame_start(s);
00828
00829
00830
00831
00832
00833
00834 s->current_picture_ptr->key_frame= 0;
00835 s->current_picture_ptr->mmco_reset= 0;
00836
00837 assert(s->linesize && s->uvlinesize);
00838
00839 for(i=0; i<16; i++){
00840 h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
00841 h->block_offset[24+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->linesize*((scan8[i] - scan8[0])>>3);
00842 }
00843 for(i=0; i<4; i++){
00844 h->block_offset[16+i]=
00845 h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
00846 h->block_offset[24+16+i]=
00847 h->block_offset[24+20+i]= 4*((scan8[i] - scan8[0])&7) + 8*s->uvlinesize*((scan8[i] - scan8[0])>>3);
00848 }
00849
00850
00851
00852 for(i = 0; i < s->avctx->thread_count; i++)
00853 if(h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
00854 h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*2*s->linesize + 8*2*s->uvlinesize);
00855
00856
00857 memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867 if(s->codec_id != CODEC_ID_SVQ3)
00868 s->current_picture_ptr->reference= 0;
00869
00870 s->current_picture_ptr->field_poc[0]=
00871 s->current_picture_ptr->field_poc[1]= INT_MAX;
00872 assert(s->current_picture_ptr->long_ref==0);
00873
00874 return 0;
00875 }
00876
00877 static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int simple){
00878 MpegEncContext * const s = &h->s;
00879 uint8_t *top_border;
00880 int top_idx = 1;
00881
00882 src_y -= linesize;
00883 src_cb -= uvlinesize;
00884 src_cr -= uvlinesize;
00885
00886 if(!simple && FRAME_MBAFF){
00887 if(s->mb_y&1){
00888 if(!MB_MBAFF){
00889 top_border = h->top_borders[0][s->mb_x];
00890 AV_COPY128(top_border, src_y + 15*linesize);
00891 if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00892 AV_COPY64(top_border+16, src_cb+7*uvlinesize);
00893 AV_COPY64(top_border+24, src_cr+7*uvlinesize);
00894 }
00895 }
00896 }else if(MB_MBAFF){
00897 top_idx = 0;
00898 }else
00899 return;
00900 }
00901
00902 top_border = h->top_borders[top_idx][s->mb_x];
00903
00904
00905 AV_COPY128(top_border, src_y + 16*linesize);
00906
00907 if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00908 AV_COPY64(top_border+16, src_cb+8*uvlinesize);
00909 AV_COPY64(top_border+24, src_cr+8*uvlinesize);
00910 }
00911 }
00912
00913 static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg, int simple){
00914 MpegEncContext * const s = &h->s;
00915 int deblock_left;
00916 int deblock_top;
00917 int top_idx = 1;
00918 uint8_t *top_border_m1;
00919 uint8_t *top_border;
00920
00921 if(!simple && FRAME_MBAFF){
00922 if(s->mb_y&1){
00923 if(!MB_MBAFF)
00924 return;
00925 }else{
00926 top_idx = MB_MBAFF ? 0 : 1;
00927 }
00928 }
00929
00930 if(h->deblocking_filter == 2) {
00931 deblock_left = h->left_type[0];
00932 deblock_top = h->top_type;
00933 } else {
00934 deblock_left = (s->mb_x > 0);
00935 deblock_top = (s->mb_y > !!MB_FIELD);
00936 }
00937
00938 src_y -= linesize + 1;
00939 src_cb -= uvlinesize + 1;
00940 src_cr -= uvlinesize + 1;
00941
00942 top_border_m1 = h->top_borders[top_idx][s->mb_x-1];
00943 top_border = h->top_borders[top_idx][s->mb_x];
00944
00945 #define XCHG(a,b,xchg)\
00946 if (xchg) AV_SWAP64(b,a);\
00947 else AV_COPY64(b,a);
00948
00949 if(deblock_top){
00950 if(deblock_left){
00951 XCHG(top_border_m1+8, src_y -7, 1);
00952 }
00953 XCHG(top_border+0, src_y +1, xchg);
00954 XCHG(top_border+8, src_y +9, 1);
00955 if(s->mb_x+1 < s->mb_width){
00956 XCHG(h->top_borders[top_idx][s->mb_x+1], src_y +17, 1);
00957 }
00958 }
00959
00960 if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
00961 if(deblock_top){
00962 if(deblock_left){
00963 XCHG(top_border_m1+16, src_cb -7, 1);
00964 XCHG(top_border_m1+24, src_cr -7, 1);
00965 }
00966 XCHG(top_border+16, src_cb+1, 1);
00967 XCHG(top_border+24, src_cr+1, 1);
00968 }
00969 }
00970 }
00971
00972 static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple){
00973 MpegEncContext * const s = &h->s;
00974 const int mb_x= s->mb_x;
00975 const int mb_y= s->mb_y;
00976 const int mb_xy= h->mb_xy;
00977 const int mb_type= s->current_picture.mb_type[mb_xy];
00978 uint8_t *dest_y, *dest_cb, *dest_cr;
00979 int linesize, uvlinesize ;
00980 int i;
00981 int *block_offset = &h->block_offset[0];
00982 const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
00983
00984 const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
00985 void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
00986 void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
00987
00988 dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;
00989 dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
00990 dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
00991
00992 s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + 64, s->linesize, 4);
00993 s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + 64, dest_cr - dest_cb, 2);
00994
00995 h->list_counts[mb_xy]= h->list_count;
00996
00997 if (!simple && MB_FIELD) {
00998 linesize = h->mb_linesize = s->linesize * 2;
00999 uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
01000 block_offset = &h->block_offset[24];
01001 if(mb_y&1){
01002 dest_y -= s->linesize*15;
01003 dest_cb-= s->uvlinesize*7;
01004 dest_cr-= s->uvlinesize*7;
01005 }
01006 if(FRAME_MBAFF) {
01007 int list;
01008 for(list=0; list<h->list_count; list++){
01009 if(!USES_LIST(mb_type, list))
01010 continue;
01011 if(IS_16X16(mb_type)){
01012 int8_t *ref = &h->ref_cache[list][scan8[0]];
01013 fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);
01014 }else{
01015 for(i=0; i<16; i+=4){
01016 int ref = h->ref_cache[list][scan8[i]];
01017 if(ref >= 0)
01018 fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);
01019 }
01020 }
01021 }
01022 }
01023 } else {
01024 linesize = h->mb_linesize = s->linesize;
01025 uvlinesize = h->mb_uvlinesize = s->uvlinesize;
01026
01027 }
01028
01029 if (!simple && IS_INTRA_PCM(mb_type)) {
01030 for (i=0; i<16; i++) {
01031 memcpy(dest_y + i* linesize, h->mb + i*8, 16);
01032 }
01033 for (i=0; i<8; i++) {
01034 memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4, 8);
01035 memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4, 8);
01036 }
01037 } else {
01038 if(IS_INTRA(mb_type)){
01039 if(h->deblocking_filter)
01040 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1, simple);
01041
01042 if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
01043 h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
01044 h->hpc.pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
01045 }
01046
01047 if(IS_INTRA4x4(mb_type)){
01048 if(simple || !s->encoding){
01049 if(IS_8x8DCT(mb_type)){
01050 if(transform_bypass){
01051 idct_dc_add =
01052 idct_add = s->dsp.add_pixels8;
01053 }else{
01054 idct_dc_add = h->h264dsp.h264_idct8_dc_add;
01055 idct_add = h->h264dsp.h264_idct8_add;
01056 }
01057 for(i=0; i<16; i+=4){
01058 uint8_t * const ptr= dest_y + block_offset[i];
01059 const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
01060 if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
01061 h->hpc.pred8x8l_add[dir](ptr, h->mb + i*16, linesize);
01062 }else{
01063 const int nnz = h->non_zero_count_cache[ scan8[i] ];
01064 h->hpc.pred8x8l[ dir ](ptr, (h->topleft_samples_available<<i)&0x8000,
01065 (h->topright_samples_available<<i)&0x4000, linesize);
01066 if(nnz){
01067 if(nnz == 1 && h->mb[i*16])
01068 idct_dc_add(ptr, h->mb + i*16, linesize);
01069 else
01070 idct_add (ptr, h->mb + i*16, linesize);
01071 }
01072 }
01073 }
01074 }else{
01075 if(transform_bypass){
01076 idct_dc_add =
01077 idct_add = s->dsp.add_pixels4;
01078 }else{
01079 idct_dc_add = h->h264dsp.h264_idct_dc_add;
01080 idct_add = h->h264dsp.h264_idct_add;
01081 }
01082 for(i=0; i<16; i++){
01083 uint8_t * const ptr= dest_y + block_offset[i];
01084 const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
01085
01086 if(transform_bypass && h->sps.profile_idc==244 && dir<=1){
01087 h->hpc.pred4x4_add[dir](ptr, h->mb + i*16, linesize);
01088 }else{
01089 uint8_t *topright;
01090 int nnz, tr;
01091 if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
01092 const int topright_avail= (h->topright_samples_available<<i)&0x8000;
01093 assert(mb_y || linesize <= block_offset[i]);
01094 if(!topright_avail){
01095 tr= ptr[3 - linesize]*0x01010101;
01096 topright= (uint8_t*) &tr;
01097 }else
01098 topright= ptr + 4 - linesize;
01099 }else
01100 topright= NULL;
01101
01102 h->hpc.pred4x4[ dir ](ptr, topright, linesize);
01103 nnz = h->non_zero_count_cache[ scan8[i] ];
01104 if(nnz){
01105 if(is_h264){
01106 if(nnz == 1 && h->mb[i*16])
01107 idct_dc_add(ptr, h->mb + i*16, linesize);
01108 else
01109 idct_add (ptr, h->mb + i*16, linesize);
01110 }else
01111 ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
01112 }
01113 }
01114 }
01115 }
01116 }
01117 }else{
01118 h->hpc.pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
01119 if(is_h264){
01120 if(h->non_zero_count_cache[ scan8[LUMA_DC_BLOCK_INDEX] ]){
01121 if(!transform_bypass)
01122 h->h264dsp.h264_luma_dc_dequant_idct(h->mb, h->mb_luma_dc, h->dequant4_coeff[0][s->qscale][0]);
01123 else{
01124 static const uint8_t dc_mapping[16] = { 0*16, 1*16, 4*16, 5*16, 2*16, 3*16, 6*16, 7*16,
01125 8*16, 9*16,12*16,13*16,10*16,11*16,14*16,15*16};
01126 for(i = 0; i < 16; i++)
01127 h->mb[dc_mapping[i]] = h->mb_luma_dc[i];
01128 }
01129 }
01130 }else
01131 ff_svq3_luma_dc_dequant_idct_c(h->mb, h->mb_luma_dc, s->qscale);
01132 }
01133 if(h->deblocking_filter)
01134 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, simple);
01135 }else if(is_h264){
01136 hl_motion(h, dest_y, dest_cb, dest_cr,
01137 s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
01138 s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
01139 h->h264dsp.weight_h264_pixels_tab, h->h264dsp.biweight_h264_pixels_tab);
01140 }
01141
01142
01143 if(!IS_INTRA4x4(mb_type)){
01144 if(is_h264){
01145 if(IS_INTRA16x16(mb_type)){
01146 if(transform_bypass){
01147 if(h->sps.profile_idc==244 && (h->intra16x16_pred_mode==VERT_PRED8x8 || h->intra16x16_pred_mode==HOR_PRED8x8)){
01148 h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset, h->mb, linesize);
01149 }else{
01150 for(i=0; i<16; i++){
01151 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
01152 s->dsp.add_pixels4(dest_y + block_offset[i], h->mb + i*16, linesize);
01153 }
01154 }
01155 }else{
01156 h->h264dsp.h264_idct_add16intra(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
01157 }
01158 }else if(h->cbp&15){
01159 if(transform_bypass){
01160 const int di = IS_8x8DCT(mb_type) ? 4 : 1;
01161 idct_add= IS_8x8DCT(mb_type) ? s->dsp.add_pixels8 : s->dsp.add_pixels4;
01162 for(i=0; i<16; i+=di){
01163 if(h->non_zero_count_cache[ scan8[i] ]){
01164 idct_add(dest_y + block_offset[i], h->mb + i*16, linesize);
01165 }
01166 }
01167 }else{
01168 if(IS_8x8DCT(mb_type)){
01169 h->h264dsp.h264_idct8_add4(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
01170 }else{
01171 h->h264dsp.h264_idct_add16(dest_y, block_offset, h->mb, linesize, h->non_zero_count_cache);
01172 }
01173 }
01174 }
01175 }else{
01176 for(i=0; i<16; i++){
01177 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
01178 uint8_t * const ptr= dest_y + block_offset[i];
01179 ff_svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
01180 }
01181 }
01182 }
01183 }
01184
01185 if((simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) && (h->cbp&0x30)){
01186 uint8_t *dest[2] = {dest_cb, dest_cr};
01187 if(transform_bypass){
01188 if(IS_INTRA(mb_type) && h->sps.profile_idc==244 && (h->chroma_pred_mode==VERT_PRED8x8 || h->chroma_pred_mode==HOR_PRED8x8)){
01189 h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0], block_offset + 16, h->mb + 16*16, uvlinesize);
01190 h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1], block_offset + 20, h->mb + 20*16, uvlinesize);
01191 }else{
01192 idct_add = s->dsp.add_pixels4;
01193 for(i=16; i<16+8; i++){
01194 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16])
01195 idct_add (dest[(i&4)>>2] + block_offset[i], h->mb + i*16, uvlinesize);
01196 }
01197 }
01198 }else{
01199 int chroma_qpu = h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0];
01200 int chroma_qpv = h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0];
01201 if(is_h264){
01202 if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+0] ])
01203 h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+0*16, &h->mb_chroma_dc[0], chroma_qpu );
01204 if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+1] ])
01205 h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+4*16, &h->mb_chroma_dc[1], chroma_qpv );
01206 h->h264dsp.h264_idct_add8(dest, block_offset,
01207 h->mb, uvlinesize,
01208 h->non_zero_count_cache);
01209 }else{
01210 h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+0*16, &h->mb_chroma_dc[0], chroma_qpu );
01211 h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16+4*16, &h->mb_chroma_dc[1], chroma_qpv );
01212 for(i=16; i<16+8; i++){
01213 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
01214 uint8_t * const ptr= dest[(i&4)>>2] + block_offset[i];
01215 ff_svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, ff_h264_chroma_qp[s->qscale + 12] - 12, 2);
01216 }
01217 }
01218 }
01219 }
01220 }
01221 }
01222 if(h->cbp || IS_INTRA(mb_type))
01223 s->dsp.clear_blocks(h->mb);
01224 }
01225
01229 static void hl_decode_mb_simple(H264Context *h){
01230 hl_decode_mb_internal(h, 1);
01231 }
01232
01236 static void av_noinline hl_decode_mb_complex(H264Context *h){
01237 hl_decode_mb_internal(h, 0);
01238 }
01239
01240 void ff_h264_hl_decode_mb(H264Context *h){
01241 MpegEncContext * const s = &h->s;
01242 const int mb_xy= h->mb_xy;
01243 const int mb_type= s->current_picture.mb_type[mb_xy];
01244 int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
01245
01246 if (is_complex)
01247 hl_decode_mb_complex(h);
01248 else hl_decode_mb_simple(h);
01249 }
01250
01251 static int pred_weight_table(H264Context *h){
01252 MpegEncContext * const s = &h->s;
01253 int list, i;
01254 int luma_def, chroma_def;
01255
01256 h->use_weight= 0;
01257 h->use_weight_chroma= 0;
01258 h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
01259 if(CHROMA)
01260 h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
01261 luma_def = 1<<h->luma_log2_weight_denom;
01262 chroma_def = 1<<h->chroma_log2_weight_denom;
01263
01264 for(list=0; list<2; list++){
01265 h->luma_weight_flag[list] = 0;
01266 h->chroma_weight_flag[list] = 0;
01267 for(i=0; i<h->ref_count[list]; i++){
01268 int luma_weight_flag, chroma_weight_flag;
01269
01270 luma_weight_flag= get_bits1(&s->gb);
01271 if(luma_weight_flag){
01272 h->luma_weight[i][list][0]= get_se_golomb(&s->gb);
01273 h->luma_weight[i][list][1]= get_se_golomb(&s->gb);
01274 if( h->luma_weight[i][list][0] != luma_def
01275 || h->luma_weight[i][list][1] != 0) {
01276 h->use_weight= 1;
01277 h->luma_weight_flag[list]= 1;
01278 }
01279 }else{
01280 h->luma_weight[i][list][0]= luma_def;
01281 h->luma_weight[i][list][1]= 0;
01282 }
01283
01284 if(CHROMA){
01285 chroma_weight_flag= get_bits1(&s->gb);
01286 if(chroma_weight_flag){
01287 int j;
01288 for(j=0; j<2; j++){
01289 h->chroma_weight[i][list][j][0]= get_se_golomb(&s->gb);
01290 h->chroma_weight[i][list][j][1]= get_se_golomb(&s->gb);
01291 if( h->chroma_weight[i][list][j][0] != chroma_def
01292 || h->chroma_weight[i][list][j][1] != 0) {
01293 h->use_weight_chroma= 1;
01294 h->chroma_weight_flag[list]= 1;
01295 }
01296 }
01297 }else{
01298 int j;
01299 for(j=0; j<2; j++){
01300 h->chroma_weight[i][list][j][0]= chroma_def;
01301 h->chroma_weight[i][list][j][1]= 0;
01302 }
01303 }
01304 }
01305 }
01306 if(h->slice_type_nos != FF_B_TYPE) break;
01307 }
01308 h->use_weight= h->use_weight || h->use_weight_chroma;
01309 return 0;
01310 }
01311
01317 static void implicit_weight_table(H264Context *h, int field){
01318 MpegEncContext * const s = &h->s;
01319 int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
01320
01321 for (i = 0; i < 2; i++) {
01322 h->luma_weight_flag[i] = 0;
01323 h->chroma_weight_flag[i] = 0;
01324 }
01325
01326 if(field < 0){
01327 cur_poc = s->current_picture_ptr->poc;
01328 if( h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF
01329 && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
01330 h->use_weight= 0;
01331 h->use_weight_chroma= 0;
01332 return;
01333 }
01334 ref_start= 0;
01335 ref_count0= h->ref_count[0];
01336 ref_count1= h->ref_count[1];
01337 }else{
01338 cur_poc = s->current_picture_ptr->field_poc[field];
01339 ref_start= 16;
01340 ref_count0= 16+2*h->ref_count[0];
01341 ref_count1= 16+2*h->ref_count[1];
01342 }
01343
01344 h->use_weight= 2;
01345 h->use_weight_chroma= 2;
01346 h->luma_log2_weight_denom= 5;
01347 h->chroma_log2_weight_denom= 5;
01348
01349 for(ref0=ref_start; ref0 < ref_count0; ref0++){
01350 int poc0 = h->ref_list[0][ref0].poc;
01351 for(ref1=ref_start; ref1 < ref_count1; ref1++){
01352 int poc1 = h->ref_list[1][ref1].poc;
01353 int td = av_clip(poc1 - poc0, -128, 127);
01354 int w= 32;
01355 if(td){
01356 int tb = av_clip(cur_poc - poc0, -128, 127);
01357 int tx = (16384 + (FFABS(td) >> 1)) / td;
01358 int dist_scale_factor = (tb*tx + 32) >> 8;
01359 if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
01360 w = 64 - dist_scale_factor;
01361 }
01362 if(field<0){
01363 h->implicit_weight[ref0][ref1][0]=
01364 h->implicit_weight[ref0][ref1][1]= w;
01365 }else{
01366 h->implicit_weight[ref0][ref1][field]=w;
01367 }
01368 }
01369 }
01370 }
01371
01375 static void idr(H264Context *h){
01376 ff_h264_remove_all_refs(h);
01377 h->prev_frame_num= 0;
01378 h->prev_frame_num_offset= 0;
01379 h->prev_poc_msb=
01380 h->prev_poc_lsb= 0;
01381 }
01382
01383
01384 static void flush_dpb(AVCodecContext *avctx){
01385 H264Context *h= avctx->priv_data;
01386 int i;
01387 for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
01388 if(h->delayed_pic[i])
01389 h->delayed_pic[i]->reference= 0;
01390 h->delayed_pic[i]= NULL;
01391 }
01392 h->outputed_poc= INT_MIN;
01393 h->prev_interlaced_frame = 1;
01394 idr(h);
01395 if(h->s.current_picture_ptr)
01396 h->s.current_picture_ptr->reference= 0;
01397 h->s.first_field= 0;
01398 ff_h264_reset_sei(h);
01399 ff_mpeg_flush(avctx);
01400 }
01401
01402 static int init_poc(H264Context *h){
01403 MpegEncContext * const s = &h->s;
01404 const int max_frame_num= 1<<h->sps.log2_max_frame_num;
01405 int field_poc[2];
01406 Picture *cur = s->current_picture_ptr;
01407
01408 h->frame_num_offset= h->prev_frame_num_offset;
01409 if(h->frame_num < h->prev_frame_num)
01410 h->frame_num_offset += max_frame_num;
01411
01412 if(h->sps.poc_type==0){
01413 const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
01414
01415 if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
01416 h->poc_msb = h->prev_poc_msb + max_poc_lsb;
01417 else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
01418 h->poc_msb = h->prev_poc_msb - max_poc_lsb;
01419 else
01420 h->poc_msb = h->prev_poc_msb;
01421
01422 field_poc[0] =
01423 field_poc[1] = h->poc_msb + h->poc_lsb;
01424 if(s->picture_structure == PICT_FRAME)
01425 field_poc[1] += h->delta_poc_bottom;
01426 }else if(h->sps.poc_type==1){
01427 int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
01428 int i;
01429
01430 if(h->sps.poc_cycle_length != 0)
01431 abs_frame_num = h->frame_num_offset + h->frame_num;
01432 else
01433 abs_frame_num = 0;
01434
01435 if(h->nal_ref_idc==0 && abs_frame_num > 0)
01436 abs_frame_num--;
01437
01438 expected_delta_per_poc_cycle = 0;
01439 for(i=0; i < h->sps.poc_cycle_length; i++)
01440 expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ];
01441
01442 if(abs_frame_num > 0){
01443 int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
01444 int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
01445
01446 expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
01447 for(i = 0; i <= frame_num_in_poc_cycle; i++)
01448 expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
01449 } else
01450 expectedpoc = 0;
01451
01452 if(h->nal_ref_idc == 0)
01453 expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
01454
01455 field_poc[0] = expectedpoc + h->delta_poc[0];
01456 field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
01457
01458 if(s->picture_structure == PICT_FRAME)
01459 field_poc[1] += h->delta_poc[1];
01460 }else{
01461 int poc= 2*(h->frame_num_offset + h->frame_num);
01462
01463 if(!h->nal_ref_idc)
01464 poc--;
01465
01466 field_poc[0]= poc;
01467 field_poc[1]= poc;
01468 }
01469
01470 if(s->picture_structure != PICT_BOTTOM_FIELD)
01471 s->current_picture_ptr->field_poc[0]= field_poc[0];
01472 if(s->picture_structure != PICT_TOP_FIELD)
01473 s->current_picture_ptr->field_poc[1]= field_poc[1];
01474 cur->poc= FFMIN(cur->field_poc[0], cur->field_poc[1]);
01475
01476 return 0;
01477 }
01478
01479
01483 static void init_scan_tables(H264Context *h){
01484 int i;
01485 for(i=0; i<16; i++){
01486 #define T(x) (x>>2) | ((x<<2) & 0xF)
01487 h->zigzag_scan[i] = T(zigzag_scan[i]);
01488 h-> field_scan[i] = T( field_scan[i]);
01489 #undef T
01490 }
01491 for(i=0; i<64; i++){
01492 #define T(x) (x>>3) | ((x&7)<<3)
01493 h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
01494 h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
01495 h->field_scan8x8[i] = T(field_scan8x8[i]);
01496 h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
01497 #undef T
01498 }
01499 if(h->sps.transform_bypass){
01500 h->zigzag_scan_q0 = zigzag_scan;
01501 h->zigzag_scan8x8_q0 = ff_zigzag_direct;
01502 h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
01503 h->field_scan_q0 = field_scan;
01504 h->field_scan8x8_q0 = field_scan8x8;
01505 h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
01506 }else{
01507 h->zigzag_scan_q0 = h->zigzag_scan;
01508 h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
01509 h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
01510 h->field_scan_q0 = h->field_scan;
01511 h->field_scan8x8_q0 = h->field_scan8x8;
01512 h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
01513 }
01514 }
01515
01516 static void field_end(H264Context *h){
01517 MpegEncContext * const s = &h->s;
01518 AVCodecContext * const avctx= s->avctx;
01519 s->mb_y= 0;
01520
01521 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
01522 s->current_picture_ptr->pict_type= s->pict_type;
01523
01524 if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
01525 ff_vdpau_h264_set_reference_frames(s);
01526
01527 if(!s->dropable) {
01528 ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
01529 h->prev_poc_msb= h->poc_msb;
01530 h->prev_poc_lsb= h->poc_lsb;
01531 }
01532 h->prev_frame_num_offset= h->frame_num_offset;
01533 h->prev_frame_num= h->frame_num;
01534
01535 if (avctx->hwaccel) {
01536 if (avctx->hwaccel->end_frame(avctx) < 0)
01537 av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
01538 }
01539
01540 if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
01541 ff_vdpau_h264_picture_complete(s);
01542
01543
01544
01545
01546
01547
01548
01549
01550
01551
01552
01553
01554
01555 if (!FIELD_PICTURE)
01556 ff_er_frame_end(s);
01557
01558 MPV_frame_end(s);
01559
01560 h->current_slice=0;
01561 }
01562
01566 static void clone_slice(H264Context *dst, H264Context *src)
01567 {
01568 memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
01569 dst->s.current_picture_ptr = src->s.current_picture_ptr;
01570 dst->s.current_picture = src->s.current_picture;
01571 dst->s.linesize = src->s.linesize;
01572 dst->s.uvlinesize = src->s.uvlinesize;
01573 dst->s.first_field = src->s.first_field;
01574
01575 dst->prev_poc_msb = src->prev_poc_msb;
01576 dst->prev_poc_lsb = src->prev_poc_lsb;
01577 dst->prev_frame_num_offset = src->prev_frame_num_offset;
01578 dst->prev_frame_num = src->prev_frame_num;
01579 dst->short_ref_count = src->short_ref_count;
01580
01581 memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
01582 memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
01583 memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
01584 memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
01585
01586 memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
01587 memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
01588 }
01589
01599 static int decode_slice_header(H264Context *h, H264Context *h0){
01600 MpegEncContext * const s = &h->s;
01601 MpegEncContext * const s0 = &h0->s;
01602 unsigned int first_mb_in_slice;
01603 unsigned int pps_id;
01604 int num_ref_idx_active_override_flag;
01605 unsigned int slice_type, tmp, i, j;
01606 int default_ref_list_done = 0;
01607 int last_pic_structure;
01608
01609 s->dropable= h->nal_ref_idc == 0;
01610
01611 if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
01612 s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
01613 s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
01614 }else{
01615 s->me.qpel_put= s->dsp.put_h264_qpel_pixels_tab;
01616 s->me.qpel_avg= s->dsp.avg_h264_qpel_pixels_tab;
01617 }
01618
01619 first_mb_in_slice= get_ue_golomb(&s->gb);
01620
01621 if(first_mb_in_slice == 0){
01622 if(h0->current_slice && FIELD_PICTURE){
01623 field_end(h);
01624 }
01625
01626 h0->current_slice = 0;
01627 if (!s0->first_field)
01628 s->current_picture_ptr= NULL;
01629 }
01630
01631 slice_type= get_ue_golomb_31(&s->gb);
01632 if(slice_type > 9){
01633 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);
01634 return -1;
01635 }
01636 if(slice_type > 4){
01637 slice_type -= 5;
01638 h->slice_type_fixed=1;
01639 }else
01640 h->slice_type_fixed=0;
01641
01642 slice_type= golomb_to_pict_type[ slice_type ];
01643 if (slice_type == FF_I_TYPE
01644 || (h0->current_slice != 0 && slice_type == h0->last_slice_type) ) {
01645 default_ref_list_done = 1;
01646 }
01647 h->slice_type= slice_type;
01648 h->slice_type_nos= slice_type & 3;
01649
01650 s->pict_type= h->slice_type;
01651
01652 pps_id= get_ue_golomb(&s->gb);
01653 if(pps_id>=MAX_PPS_COUNT){
01654 av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
01655 return -1;
01656 }
01657 if(!h0->pps_buffers[pps_id]) {
01658 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
01659 return -1;
01660 }
01661 h->pps= *h0->pps_buffers[pps_id];
01662
01663 if(!h0->sps_buffers[h->pps.sps_id]) {
01664 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %u referenced\n", h->pps.sps_id);
01665 return -1;
01666 }
01667 h->sps = *h0->sps_buffers[h->pps.sps_id];
01668
01669 s->avctx->profile = h->sps.profile_idc;
01670 s->avctx->level = h->sps.level_idc;
01671 s->avctx->refs = h->sps.ref_frame_count;
01672
01673 if(h == h0 && h->dequant_coeff_pps != pps_id){
01674 h->dequant_coeff_pps = pps_id;
01675 init_dequant_tables(h);
01676 }
01677
01678 s->mb_width= h->sps.mb_width;
01679 s->mb_height= h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
01680
01681 h->b_stride= s->mb_width*4;
01682
01683 s->width = 16*s->mb_width - 2*FFMIN(h->sps.crop_right, 7);
01684 if(h->sps.frame_mbs_only_flag)
01685 s->height= 16*s->mb_height - 2*FFMIN(h->sps.crop_bottom, 7);
01686 else
01687 s->height= 16*s->mb_height - 4*FFMIN(h->sps.crop_bottom, 7);
01688
01689 if (s->context_initialized
01690 && ( s->width != s->avctx->width || s->height != s->avctx->height
01691 || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio))) {
01692 if(h != h0)
01693 return -1;
01694 free_tables(h);
01695 flush_dpb(s->avctx);
01696 MPV_common_end(s);
01697 }
01698 if (!s->context_initialized) {
01699 if(h != h0)
01700 return -1;
01701
01702 avcodec_set_dimensions(s->avctx, s->width, s->height);
01703 s->avctx->sample_aspect_ratio= h->sps.sar;
01704 av_assert0(s->avctx->sample_aspect_ratio.den);
01705
01706 if(h->sps.video_signal_type_present_flag){
01707 s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
01708 if(h->sps.colour_description_present_flag){
01709 s->avctx->color_primaries = h->sps.color_primaries;
01710 s->avctx->color_trc = h->sps.color_trc;
01711 s->avctx->colorspace = h->sps.colorspace;
01712 }
01713 }
01714
01715 if(h->sps.timing_info_present_flag){
01716 int64_t den= h->sps.time_scale;
01717 if(h->x264_build < 44U)
01718 den *= 2;
01719 av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
01720 h->sps.num_units_in_tick, den, 1<<30);
01721 }
01722 s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
01723 s->avctx->codec->pix_fmts ?
01724 s->avctx->codec->pix_fmts :
01725 s->avctx->color_range == AVCOL_RANGE_JPEG ?
01726 hwaccel_pixfmt_list_h264_jpeg_420 :
01727 ff_hwaccel_pixfmt_list_420);
01728 s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id, s->avctx->pix_fmt);
01729
01730 if (MPV_common_init(s) < 0)
01731 return -1;
01732 s->first_field = 0;
01733 h->prev_interlaced_frame = 1;
01734
01735 init_scan_tables(h);
01736 ff_h264_alloc_tables(h);
01737
01738 for(i = 1; i < s->avctx->thread_count; i++) {
01739 H264Context *c;
01740 c = h->thread_context[i] = av_malloc(sizeof(H264Context));
01741 memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
01742 memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
01743 c->h264dsp = h->h264dsp;
01744 c->sps = h->sps;
01745 c->pps = h->pps;
01746 init_scan_tables(c);
01747 clone_tables(c, h, i);
01748 }
01749
01750 for(i = 0; i < s->avctx->thread_count; i++)
01751 if(context_init(h->thread_context[i]) < 0)
01752 return -1;
01753 }
01754
01755 h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
01756
01757 h->mb_mbaff = 0;
01758 h->mb_aff_frame = 0;
01759 last_pic_structure = s0->picture_structure;
01760 if(h->sps.frame_mbs_only_flag){
01761 s->picture_structure= PICT_FRAME;
01762 }else{
01763 if(get_bits1(&s->gb)) {
01764 s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb);
01765 } else {
01766 s->picture_structure= PICT_FRAME;
01767 h->mb_aff_frame = h->sps.mb_aff;
01768 }
01769 }
01770 h->mb_field_decoding_flag= s->picture_structure != PICT_FRAME;
01771
01772 if(h0->current_slice == 0){
01773 while(h->frame_num != h->prev_frame_num &&
01774 h->frame_num != (h->prev_frame_num+1)%(1<<h->sps.log2_max_frame_num)){
01775 Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
01776 av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n", h->frame_num, h->prev_frame_num);
01777 if (ff_h264_frame_start(h) < 0)
01778 return -1;
01779 h->prev_frame_num++;
01780 h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
01781 s->current_picture_ptr->frame_num= h->prev_frame_num;
01782 ff_generate_sliding_window_mmcos(h);
01783 ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
01784
01785
01786
01787
01788
01789
01790 if (h->short_ref_count) {
01791 if (prev) {
01792 av_image_copy(h->short_ref[0]->data, h->short_ref[0]->linesize,
01793 (const uint8_t**)prev->data, prev->linesize,
01794 s->avctx->pix_fmt, s->mb_width*16, s->mb_height*16);
01795 h->short_ref[0]->poc = prev->poc+2;
01796 }
01797 h->short_ref[0]->frame_num = h->prev_frame_num;
01798 }
01799 }
01800
01801
01802 if (s0->first_field) {
01803 assert(s0->current_picture_ptr);
01804 assert(s0->current_picture_ptr->data[0]);
01805 assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
01806
01807
01808 if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
01809
01810
01811
01812
01813 s0->current_picture_ptr = NULL;
01814 s0->first_field = FIELD_PICTURE;
01815
01816 } else {
01817 if (h->nal_ref_idc &&
01818 s0->current_picture_ptr->reference &&
01819 s0->current_picture_ptr->frame_num != h->frame_num) {
01820
01821
01822
01823
01824
01825
01826 s0->first_field = 1;
01827 s0->current_picture_ptr = NULL;
01828
01829 } else {
01830
01831 s0->first_field = 0;
01832 }
01833 }
01834
01835 } else {
01836
01837 assert(!s0->current_picture_ptr);
01838 s0->first_field = FIELD_PICTURE;
01839 }
01840
01841 if((!FIELD_PICTURE || s0->first_field) && ff_h264_frame_start(h) < 0) {
01842 s0->first_field = 0;
01843 return -1;
01844 }
01845 }
01846 if(h != h0)
01847 clone_slice(h, h0);
01848
01849 s->current_picture_ptr->frame_num= h->frame_num;
01850
01851 assert(s->mb_num == s->mb_width * s->mb_height);
01852 if(first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
01853 first_mb_in_slice >= s->mb_num){
01854 av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
01855 return -1;
01856 }
01857 s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
01858 s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
01859 if (s->picture_structure == PICT_BOTTOM_FIELD)
01860 s->resync_mb_y = s->mb_y = s->mb_y + 1;
01861 assert(s->mb_y < s->mb_height);
01862
01863 if(s->picture_structure==PICT_FRAME){
01864 h->curr_pic_num= h->frame_num;
01865 h->max_pic_num= 1<< h->sps.log2_max_frame_num;
01866 }else{
01867 h->curr_pic_num= 2*h->frame_num + 1;
01868 h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1);
01869 }
01870
01871 if(h->nal_unit_type == NAL_IDR_SLICE){
01872 get_ue_golomb(&s->gb);
01873 }
01874
01875 if(h->sps.poc_type==0){
01876 h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb);
01877
01878 if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){
01879 h->delta_poc_bottom= get_se_golomb(&s->gb);
01880 }
01881 }
01882
01883 if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){
01884 h->delta_poc[0]= get_se_golomb(&s->gb);
01885
01886 if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME)
01887 h->delta_poc[1]= get_se_golomb(&s->gb);
01888 }
01889
01890 init_poc(h);
01891
01892 if(h->pps.redundant_pic_cnt_present){
01893 h->redundant_pic_count= get_ue_golomb(&s->gb);
01894 }
01895
01896
01897 h->ref_count[0]= h->pps.ref_count[0];
01898 h->ref_count[1]= h->pps.ref_count[1];
01899
01900 if(h->slice_type_nos != FF_I_TYPE){
01901 if(h->slice_type_nos == FF_B_TYPE){
01902 h->direct_spatial_mv_pred= get_bits1(&s->gb);
01903 }
01904 num_ref_idx_active_override_flag= get_bits1(&s->gb);
01905
01906 if(num_ref_idx_active_override_flag){
01907 h->ref_count[0]= get_ue_golomb(&s->gb) + 1;
01908 if(h->slice_type_nos==FF_B_TYPE)
01909 h->ref_count[1]= get_ue_golomb(&s->gb) + 1;
01910
01911 if(h->ref_count[0]-1 > 32-1 || h->ref_count[1]-1 > 32-1){
01912 av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
01913 h->ref_count[0]= h->ref_count[1]= 1;
01914 return -1;
01915 }
01916 }
01917 if(h->slice_type_nos == FF_B_TYPE)
01918 h->list_count= 2;
01919 else
01920 h->list_count= 1;
01921 }else
01922 h->list_count= 0;
01923
01924 if(!default_ref_list_done){
01925 ff_h264_fill_default_ref_list(h);
01926 }
01927
01928 if(h->slice_type_nos!=FF_I_TYPE && ff_h264_decode_ref_pic_list_reordering(h) < 0)
01929 return -1;
01930
01931 if(h->slice_type_nos!=FF_I_TYPE){
01932 s->last_picture_ptr= &h->ref_list[0][0];
01933 ff_copy_picture(&s->last_picture, s->last_picture_ptr);
01934 }
01935 if(h->slice_type_nos==FF_B_TYPE){
01936 s->next_picture_ptr= &h->ref_list[1][0];
01937 ff_copy_picture(&s->next_picture, s->next_picture_ptr);
01938 }
01939
01940 if( (h->pps.weighted_pred && h->slice_type_nos == FF_P_TYPE )
01941 || (h->pps.weighted_bipred_idc==1 && h->slice_type_nos== FF_B_TYPE ) )
01942 pred_weight_table(h);
01943 else if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE){
01944 implicit_weight_table(h, -1);
01945 }else {
01946 h->use_weight = 0;
01947 for (i = 0; i < 2; i++) {
01948 h->luma_weight_flag[i] = 0;
01949 h->chroma_weight_flag[i] = 0;
01950 }
01951 }
01952
01953 if(h->nal_ref_idc)
01954 ff_h264_decode_ref_pic_marking(h0, &s->gb);
01955
01956 if(FRAME_MBAFF){
01957 ff_h264_fill_mbaff_ref_list(h);
01958
01959 if(h->pps.weighted_bipred_idc==2 && h->slice_type_nos== FF_B_TYPE){
01960 implicit_weight_table(h, 0);
01961 implicit_weight_table(h, 1);
01962 }
01963 }
01964
01965 if(h->slice_type_nos==FF_B_TYPE && !h->direct_spatial_mv_pred)
01966 ff_h264_direct_dist_scale_factor(h);
01967 ff_h264_direct_ref_list_init(h);
01968
01969 if( h->slice_type_nos != FF_I_TYPE && h->pps.cabac ){
01970 tmp = get_ue_golomb_31(&s->gb);
01971 if(tmp > 2){
01972 av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
01973 return -1;
01974 }
01975 h->cabac_init_idc= tmp;
01976 }
01977
01978 h->last_qscale_diff = 0;
01979 tmp = h->pps.init_qp + get_se_golomb(&s->gb);
01980 if(tmp>51){
01981 av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
01982 return -1;
01983 }
01984 s->qscale= tmp;
01985 h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
01986 h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
01987
01988 if(h->slice_type == FF_SP_TYPE){
01989 get_bits1(&s->gb);
01990 }
01991 if(h->slice_type==FF_SP_TYPE || h->slice_type == FF_SI_TYPE){
01992 get_se_golomb(&s->gb);
01993 }
01994
01995 h->deblocking_filter = 1;
01996 h->slice_alpha_c0_offset = 52;
01997 h->slice_beta_offset = 52;
01998 if( h->pps.deblocking_filter_parameters_present ) {
01999 tmp= get_ue_golomb_31(&s->gb);
02000 if(tmp > 2){
02001 av_log(s->avctx, AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
02002 return -1;
02003 }
02004 h->deblocking_filter= tmp;
02005 if(h->deblocking_filter < 2)
02006 h->deblocking_filter^= 1;
02007
02008 if( h->deblocking_filter ) {
02009 h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
02010 h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
02011 if( h->slice_alpha_c0_offset > 104U
02012 || h->slice_beta_offset > 104U){
02013 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);
02014 return -1;
02015 }
02016 }
02017 }
02018
02019 if( s->avctx->skip_loop_filter >= AVDISCARD_ALL
02020 ||(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY && h->slice_type_nos != FF_I_TYPE)
02021 ||(s->avctx->skip_loop_filter >= AVDISCARD_BIDIR && h->slice_type_nos == FF_B_TYPE)
02022 ||(s->avctx->skip_loop_filter >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
02023 h->deblocking_filter= 0;
02024
02025 if(h->deblocking_filter == 1 && h0->max_contexts > 1) {
02026 if(s->avctx->flags2 & CODEC_FLAG2_FAST) {
02027
02028
02029 h->deblocking_filter = 2;
02030 } else {
02031 h0->max_contexts = 1;
02032 if(!h0->single_decode_warning) {
02033 av_log(s->avctx, AV_LOG_INFO, "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
02034 h0->single_decode_warning = 1;
02035 }
02036 if(h != h0)
02037 return 1;
02038 }
02039 }
02040 h->qp_thresh= 15 + 52 - FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1]);
02041
02042 #if 0 //FMO
02043 if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
02044 slice_group_change_cycle= get_bits(&s->gb, ?);
02045 #endif
02046
02047 h0->last_slice_type = slice_type;
02048 h->slice_num = ++h0->current_slice;
02049 if(h->slice_num >= MAX_SLICES){
02050 av_log(s->avctx, AV_LOG_ERROR, "Too many slices, increase MAX_SLICES and recompile\n");
02051 }
02052
02053 for(j=0; j<2; j++){
02054 int id_list[16];
02055 int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
02056 for(i=0; i<16; i++){
02057 id_list[i]= 60;
02058 if(h->ref_list[j][i].data[0]){
02059 int k;
02060 uint8_t *base= h->ref_list[j][i].base[0];
02061 for(k=0; k<h->short_ref_count; k++)
02062 if(h->short_ref[k]->base[0] == base){
02063 id_list[i]= k;
02064 break;
02065 }
02066 for(k=0; k<h->long_ref_count; k++)
02067 if(h->long_ref[k] && h->long_ref[k]->base[0] == base){
02068 id_list[i]= h->short_ref_count + k;
02069 break;
02070 }
02071 }
02072 }
02073
02074 ref2frm[0]=
02075 ref2frm[1]= -1;
02076 for(i=0; i<16; i++)
02077 ref2frm[i+2]= 4*id_list[i]
02078 +(h->ref_list[j][i].reference&3);
02079 ref2frm[18+0]=
02080 ref2frm[18+1]= -1;
02081 for(i=16; i<48; i++)
02082 ref2frm[i+4]= 4*id_list[(i-16)>>1]
02083 +(h->ref_list[j][i].reference&3);
02084 }
02085
02086 h->emu_edge_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
02087 h->emu_edge_height= (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
02088
02089 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
02090 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",
02091 h->slice_num,
02092 (s->picture_structure==PICT_FRAME ? "F" : s->picture_structure==PICT_TOP_FIELD ? "T" : "B"),
02093 first_mb_in_slice,
02094 av_get_pict_type_char(h->slice_type), h->slice_type_fixed ? " fix" : "", h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
02095 pps_id, h->frame_num,
02096 s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1],
02097 h->ref_count[0], h->ref_count[1],
02098 s->qscale,
02099 h->deblocking_filter, h->slice_alpha_c0_offset/2-26, h->slice_beta_offset/2-26,
02100 h->use_weight,
02101 h->use_weight==1 && h->use_weight_chroma ? "c" : "",
02102 h->slice_type == FF_B_TYPE ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : ""
02103 );
02104 }
02105
02106 return 0;
02107 }
02108
02109 int ff_h264_get_slice_type(const H264Context *h)
02110 {
02111 switch (h->slice_type) {
02112 case FF_P_TYPE: return 0;
02113 case FF_B_TYPE: return 1;
02114 case FF_I_TYPE: return 2;
02115 case FF_SP_TYPE: return 3;
02116 case FF_SI_TYPE: return 4;
02117 default: return -1;
02118 }
02119 }
02120
02125 static int fill_filter_caches(H264Context *h, int mb_type){
02126 MpegEncContext * const s = &h->s;
02127 const int mb_xy= h->mb_xy;
02128 int top_xy, left_xy[2];
02129 int top_type, left_type[2];
02130
02131 top_xy = mb_xy - (s->mb_stride << MB_FIELD);
02132
02133
02134
02135
02136
02137
02138 left_xy[1] = left_xy[0] = mb_xy-1;
02139 if(FRAME_MBAFF){
02140 const int left_mb_field_flag = IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]);
02141 const int curr_mb_field_flag = IS_INTERLACED(mb_type);
02142 if(s->mb_y&1){
02143 if (left_mb_field_flag != curr_mb_field_flag) {
02144 left_xy[0] -= s->mb_stride;
02145 }
02146 }else{
02147 if(curr_mb_field_flag){
02148 top_xy += s->mb_stride & (((s->current_picture.mb_type[top_xy ]>>7)&1)-1);
02149 }
02150 if (left_mb_field_flag != curr_mb_field_flag) {
02151 left_xy[1] += s->mb_stride;
02152 }
02153 }
02154 }
02155
02156 h->top_mb_xy = top_xy;
02157 h->left_mb_xy[0] = left_xy[0];
02158 h->left_mb_xy[1] = left_xy[1];
02159 {
02160
02161
02162 int qp_thresh = h->qp_thresh;
02163 int qp = s->current_picture.qscale_table[mb_xy];
02164 if(qp <= qp_thresh
02165 && (left_xy[0]<0 || ((qp + s->current_picture.qscale_table[left_xy[0]] + 1)>>1) <= qp_thresh)
02166 && (top_xy < 0 || ((qp + s->current_picture.qscale_table[top_xy ] + 1)>>1) <= qp_thresh)){
02167 if(!FRAME_MBAFF)
02168 return 1;
02169 if( (left_xy[0]< 0 || ((qp + s->current_picture.qscale_table[left_xy[1] ] + 1)>>1) <= qp_thresh)
02170 && (top_xy < s->mb_stride || ((qp + s->current_picture.qscale_table[top_xy -s->mb_stride] + 1)>>1) <= qp_thresh))
02171 return 1;
02172 }
02173 }
02174
02175 top_type = s->current_picture.mb_type[top_xy] ;
02176 left_type[0] = s->current_picture.mb_type[left_xy[0]];
02177 left_type[1] = s->current_picture.mb_type[left_xy[1]];
02178 if(h->deblocking_filter == 2){
02179 if(h->slice_table[top_xy ] != h->slice_num) top_type= 0;
02180 if(h->slice_table[left_xy[0] ] != h->slice_num) left_type[0]= left_type[1]= 0;
02181 }else{
02182 if(h->slice_table[top_xy ] == 0xFFFF) top_type= 0;
02183 if(h->slice_table[left_xy[0] ] == 0xFFFF) left_type[0]= left_type[1] =0;
02184 }
02185 h->top_type = top_type ;
02186 h->left_type[0]= left_type[0];
02187 h->left_type[1]= left_type[1];
02188
02189 if(IS_INTRA(mb_type))
02190 return 0;
02191
02192 AV_COPY64(&h->non_zero_count_cache[0+8*1], &h->non_zero_count[mb_xy][ 0]);
02193 AV_COPY64(&h->non_zero_count_cache[0+8*2], &h->non_zero_count[mb_xy][ 8]);
02194 AV_COPY32(&h->non_zero_count_cache[0+8*5], &h->non_zero_count[mb_xy][16]);
02195 AV_COPY32(&h->non_zero_count_cache[4+8*3], &h->non_zero_count[mb_xy][20]);
02196 AV_COPY64(&h->non_zero_count_cache[0+8*4], &h->non_zero_count[mb_xy][24]);
02197
02198 h->cbp= h->cbp_table[mb_xy];
02199
02200 {
02201 int list;
02202 for(list=0; list<h->list_count; list++){
02203 int8_t *ref;
02204 int y, b_stride;
02205 int16_t (*mv_dst)[2];
02206 int16_t (*mv_src)[2];
02207
02208 if(!USES_LIST(mb_type, list)){
02209 fill_rectangle( h->mv_cache[list][scan8[0]], 4, 4, 8, pack16to32(0,0), 4);
02210 AV_WN32A(&h->ref_cache[list][scan8[ 0]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
02211 AV_WN32A(&h->ref_cache[list][scan8[ 2]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
02212 AV_WN32A(&h->ref_cache[list][scan8[ 8]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
02213 AV_WN32A(&h->ref_cache[list][scan8[10]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
02214 continue;
02215 }
02216
02217 ref = &s->current_picture.ref_index[list][4*mb_xy];
02218 {
02219 int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
02220 AV_WN32A(&h->ref_cache[list][scan8[ 0]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
02221 AV_WN32A(&h->ref_cache[list][scan8[ 2]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
02222 ref += 2;
02223 AV_WN32A(&h->ref_cache[list][scan8[ 8]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
02224 AV_WN32A(&h->ref_cache[list][scan8[10]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
02225 }
02226
02227 b_stride = h->b_stride;
02228 mv_dst = &h->mv_cache[list][scan8[0]];
02229 mv_src = &s->current_picture.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
02230 for(y=0; y<4; y++){
02231 AV_COPY128(mv_dst + 8*y, mv_src + y*b_stride);
02232 }
02233
02234 }
02235 }
02236
02237
02238
02239
02240
02241
02242
02243
02244
02245
02246
02247 if(top_type){
02248 AV_COPY32(&h->non_zero_count_cache[4+8*0], &h->non_zero_count[top_xy][4+3*8]);
02249 }
02250
02251 if(left_type[0]){
02252 h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][7+0*8];
02253 h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][7+1*8];
02254 h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[0]][7+2*8];
02255 h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[0]][7+3*8];
02256 }
02257
02258
02259 if(!CABAC && h->pps.transform_8x8_mode){
02260 if(IS_8x8DCT(top_type)){
02261 h->non_zero_count_cache[4+8*0]=
02262 h->non_zero_count_cache[5+8*0]= h->cbp_table[top_xy] & 4;
02263 h->non_zero_count_cache[6+8*0]=
02264 h->non_zero_count_cache[7+8*0]= h->cbp_table[top_xy] & 8;
02265 }
02266 if(IS_8x8DCT(left_type[0])){
02267 h->non_zero_count_cache[3+8*1]=
02268 h->non_zero_count_cache[3+8*2]= h->cbp_table[left_xy[0]]&2;
02269 }
02270 if(IS_8x8DCT(left_type[1])){
02271 h->non_zero_count_cache[3+8*3]=
02272 h->non_zero_count_cache[3+8*4]= h->cbp_table[left_xy[1]]&8;
02273 }
02274
02275 if(IS_8x8DCT(mb_type)){
02276 h->non_zero_count_cache[scan8[0 ]]= h->non_zero_count_cache[scan8[1 ]]=
02277 h->non_zero_count_cache[scan8[2 ]]= h->non_zero_count_cache[scan8[3 ]]= h->cbp & 1;
02278
02279 h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
02280 h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= h->cbp & 2;
02281
02282 h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
02283 h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= h->cbp & 4;
02284
02285 h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
02286 h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= h->cbp & 8;
02287 }
02288 }
02289
02290 if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
02291 int list;
02292 for(list=0; list<h->list_count; list++){
02293 if(USES_LIST(top_type, list)){
02294 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
02295 const int b8_xy= 4*top_xy + 2;
02296 int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
02297 AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);
02298 h->ref_cache[list][scan8[0] + 0 - 1*8]=
02299 h->ref_cache[list][scan8[0] + 1 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 0]];
02300 h->ref_cache[list][scan8[0] + 2 - 1*8]=
02301 h->ref_cache[list][scan8[0] + 3 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 1]];
02302 }else{
02303 AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);
02304 AV_WN32A(&h->ref_cache[list][scan8[0] + 0 - 1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
02305 }
02306
02307 if(!IS_INTERLACED(mb_type^left_type[0])){
02308 if(USES_LIST(left_type[0], list)){
02309 const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
02310 const int b8_xy= 4*left_xy[0] + 1;
02311 int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[0]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
02312 AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 0 ], s->current_picture.motion_val[list][b_xy + h->b_stride*0]);
02313 AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 8 ], s->current_picture.motion_val[list][b_xy + h->b_stride*1]);
02314 AV_COPY32(h->mv_cache[list][scan8[0] - 1 +16 ], s->current_picture.motion_val[list][b_xy + h->b_stride*2]);
02315 AV_COPY32(h->mv_cache[list][scan8[0] - 1 +24 ], s->current_picture.motion_val[list][b_xy + h->b_stride*3]);
02316 h->ref_cache[list][scan8[0] - 1 + 0 ]=
02317 h->ref_cache[list][scan8[0] - 1 + 8 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*0]];
02318 h->ref_cache[list][scan8[0] - 1 +16 ]=
02319 h->ref_cache[list][scan8[0] - 1 +24 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*1]];
02320 }else{
02321 AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 0 ]);
02322 AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 8 ]);
02323 AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +16 ]);
02324 AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +24 ]);
02325 h->ref_cache[list][scan8[0] - 1 + 0 ]=
02326 h->ref_cache[list][scan8[0] - 1 + 8 ]=
02327 h->ref_cache[list][scan8[0] - 1 + 16 ]=
02328 h->ref_cache[list][scan8[0] - 1 + 24 ]= LIST_NOT_USED;
02329 }
02330 }
02331 }
02332 }
02333
02334 return 0;
02335 }
02336
02337 static void loop_filter(H264Context *h){
02338 MpegEncContext * const s = &h->s;
02339 uint8_t *dest_y, *dest_cb, *dest_cr;
02340 int linesize, uvlinesize, mb_x, mb_y;
02341 const int end_mb_y= s->mb_y + FRAME_MBAFF;
02342 const int old_slice_type= h->slice_type;
02343
02344 if(h->deblocking_filter) {
02345 for(mb_x= 0; mb_x<s->mb_width; mb_x++){
02346 for(mb_y=end_mb_y - FRAME_MBAFF; mb_y<= end_mb_y; mb_y++){
02347 int mb_xy, mb_type;
02348 mb_xy = h->mb_xy = mb_x + mb_y*s->mb_stride;
02349 h->slice_num= h->slice_table[mb_xy];
02350 mb_type= s->current_picture.mb_type[mb_xy];
02351 h->list_count= h->list_counts[mb_xy];
02352
02353 if(FRAME_MBAFF)
02354 h->mb_mbaff = h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
02355
02356 s->mb_x= mb_x;
02357 s->mb_y= mb_y;
02358 dest_y = s->current_picture.data[0] + (mb_x + mb_y * s->linesize ) * 16;
02359 dest_cb = s->current_picture.data[1] + (mb_x + mb_y * s->uvlinesize) * 8;
02360 dest_cr = s->current_picture.data[2] + (mb_x + mb_y * s->uvlinesize) * 8;
02361
02362
02363 if (MB_FIELD) {
02364 linesize = h->mb_linesize = s->linesize * 2;
02365 uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
02366 if(mb_y&1){
02367 dest_y -= s->linesize*15;
02368 dest_cb-= s->uvlinesize*7;
02369 dest_cr-= s->uvlinesize*7;
02370 }
02371 } else {
02372 linesize = h->mb_linesize = s->linesize;
02373 uvlinesize = h->mb_uvlinesize = s->uvlinesize;
02374 }
02375 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
02376 if(fill_filter_caches(h, mb_type))
02377 continue;
02378 h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
02379 h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
02380
02381 if (FRAME_MBAFF) {
02382 ff_h264_filter_mb (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
02383 } else {
02384 ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
02385 }
02386 }
02387 }
02388 }
02389 h->slice_type= old_slice_type;
02390 s->mb_x= 0;
02391 s->mb_y= end_mb_y - FRAME_MBAFF;
02392 h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
02393 h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
02394 }
02395
02396 static void predict_field_decoding_flag(H264Context *h){
02397 MpegEncContext * const s = &h->s;
02398 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
02399 int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
02400 ? s->current_picture.mb_type[mb_xy-1]
02401 : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
02402 ? s->current_picture.mb_type[mb_xy-s->mb_stride]
02403 : 0;
02404 h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
02405 }
02406
02407 static int decode_slice(struct AVCodecContext *avctx, void *arg){
02408 H264Context *h = *(void**)arg;
02409 MpegEncContext * const s = &h->s;
02410 const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
02411
02412 s->mb_skip_run= -1;
02413
02414 h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME || s->codec_id != CODEC_ID_H264 ||
02415 (CONFIG_GRAY && (s->flags&CODEC_FLAG_GRAY));
02416
02417 if( h->pps.cabac ) {
02418
02419 align_get_bits( &s->gb );
02420
02421
02422 ff_init_cabac_states( &h->cabac);
02423 ff_init_cabac_decoder( &h->cabac,
02424 s->gb.buffer + get_bits_count(&s->gb)/8,
02425 (get_bits_left(&s->gb) + 7)/8);
02426
02427 ff_h264_init_cabac_states(h);
02428
02429 for(;;){
02430
02431 int ret = ff_h264_decode_mb_cabac(h);
02432 int eos;
02433
02434
02435 if(ret>=0) ff_h264_hl_decode_mb(h);
02436
02437 if( ret >= 0 && FRAME_MBAFF ) {
02438 s->mb_y++;
02439
02440 ret = ff_h264_decode_mb_cabac(h);
02441
02442 if(ret>=0) ff_h264_hl_decode_mb(h);
02443 s->mb_y--;
02444 }
02445 eos = get_cabac_terminate( &h->cabac );
02446
02447 if((s->workaround_bugs & FF_BUG_TRUNCATED) && h->cabac.bytestream > h->cabac.bytestream_end + 2){
02448 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
02449 return 0;
02450 }
02451 if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
02452 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);
02453 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
02454 return -1;
02455 }
02456
02457 if( ++s->mb_x >= s->mb_width ) {
02458 s->mb_x = 0;
02459 loop_filter(h);
02460 ff_draw_horiz_band(s, 16*s->mb_y, 16);
02461 ++s->mb_y;
02462 if(FIELD_OR_MBAFF_PICTURE) {
02463 ++s->mb_y;
02464 if(FRAME_MBAFF && s->mb_y < s->mb_height)
02465 predict_field_decoding_flag(h);
02466 }
02467 }
02468
02469 if( eos || s->mb_y >= s->mb_height ) {
02470 tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
02471 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
02472 return 0;
02473 }
02474 }
02475
02476 } else {
02477 for(;;){
02478 int ret = ff_h264_decode_mb_cavlc(h);
02479
02480 if(ret>=0) ff_h264_hl_decode_mb(h);
02481
02482 if(ret>=0 && FRAME_MBAFF){
02483 s->mb_y++;
02484 ret = ff_h264_decode_mb_cavlc(h);
02485
02486 if(ret>=0) ff_h264_hl_decode_mb(h);
02487 s->mb_y--;
02488 }
02489
02490 if(ret<0){
02491 av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
02492 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
02493
02494 return -1;
02495 }
02496
02497 if(++s->mb_x >= s->mb_width){
02498 s->mb_x=0;
02499 loop_filter(h);
02500 ff_draw_horiz_band(s, 16*s->mb_y, 16);
02501 ++s->mb_y;
02502 if(FIELD_OR_MBAFF_PICTURE) {
02503 ++s->mb_y;
02504 if(FRAME_MBAFF && s->mb_y < s->mb_height)
02505 predict_field_decoding_flag(h);
02506 }
02507 if(s->mb_y >= s->mb_height){
02508 tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
02509
02510 if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
02511 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
02512
02513 return 0;
02514 }else{
02515 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
02516
02517 return -1;
02518 }
02519 }
02520 }
02521
02522 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
02523 tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
02524 if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
02525 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
02526
02527 return 0;
02528 }else{
02529 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
02530
02531 return -1;
02532 }
02533 }
02534 }
02535 }
02536
02537 #if 0
02538 for(;s->mb_y < s->mb_height; s->mb_y++){
02539 for(;s->mb_x < s->mb_width; s->mb_x++){
02540 int ret= decode_mb(h);
02541
02542 ff_h264_hl_decode_mb(h);
02543
02544 if(ret<0){
02545 av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
02546 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
02547
02548 return -1;
02549 }
02550
02551 if(++s->mb_x >= s->mb_width){
02552 s->mb_x=0;
02553 if(++s->mb_y >= s->mb_height){
02554 if(get_bits_count(s->gb) == s->gb.size_in_bits){
02555 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
02556
02557 return 0;
02558 }else{
02559 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
02560
02561 return -1;
02562 }
02563 }
02564 }
02565
02566 if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
02567 if(get_bits_count(s->gb) == s->gb.size_in_bits){
02568 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
02569
02570 return 0;
02571 }else{
02572 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
02573
02574 return -1;
02575 }
02576 }
02577 }
02578 s->mb_x=0;
02579 ff_draw_horiz_band(s, 16*s->mb_y, 16);
02580 }
02581 #endif
02582 return -1;
02583 }
02584
02591 static void execute_decode_slices(H264Context *h, int context_count){
02592 MpegEncContext * const s = &h->s;
02593 AVCodecContext * const avctx= s->avctx;
02594 H264Context *hx;
02595 int i;
02596
02597 if (s->avctx->hwaccel)
02598 return;
02599 if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
02600 return;
02601 if(context_count == 1) {
02602 decode_slice(avctx, &h);
02603 } else {
02604 for(i = 1; i < context_count; i++) {
02605 hx = h->thread_context[i];
02606 hx->s.error_recognition = avctx->error_recognition;
02607 hx->s.error_count = 0;
02608 }
02609
02610 avctx->execute(avctx, (void *)decode_slice,
02611 h->thread_context, NULL, context_count, sizeof(void*));
02612
02613
02614 hx = h->thread_context[context_count - 1];
02615 s->mb_x = hx->s.mb_x;
02616 s->mb_y = hx->s.mb_y;
02617 s->dropable = hx->s.dropable;
02618 s->picture_structure = hx->s.picture_structure;
02619 for(i = 1; i < context_count; i++)
02620 h->s.error_count += h->thread_context[i]->s.error_count;
02621 }
02622 }
02623
02624
02625 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size){
02626 MpegEncContext * const s = &h->s;
02627 AVCodecContext * const avctx= s->avctx;
02628 int buf_index=0;
02629 H264Context *hx;
02630 int context_count = 0;
02631 int next_avc= h->is_avc ? 0 : buf_size;
02632
02633 h->max_contexts = avctx->thread_count;
02634 #if 0
02635 int i;
02636 for(i=0; i<50; i++){
02637 av_log(NULL, AV_LOG_ERROR,"%02X ", buf[i]);
02638 }
02639 #endif
02640 if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
02641 h->current_slice = 0;
02642 if (!s->first_field)
02643 s->current_picture_ptr= NULL;
02644 ff_h264_reset_sei(h);
02645 }
02646
02647 for(;;){
02648 int consumed;
02649 int dst_length;
02650 int bit_length;
02651 const uint8_t *ptr;
02652 int i, nalsize = 0;
02653 int err;
02654
02655 if(buf_index >= next_avc) {
02656 if(buf_index >= buf_size) break;
02657 nalsize = 0;
02658 for(i = 0; i < h->nal_length_size; i++)
02659 nalsize = (nalsize << 8) | buf[buf_index++];
02660 if(nalsize <= 0 || nalsize > buf_size - buf_index){
02661 av_log(h->s.avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
02662 break;
02663 }
02664 next_avc= buf_index + nalsize;
02665 } else {
02666
02667 for(; buf_index + 3 < next_avc; buf_index++){
02668
02669 if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
02670 break;
02671 }
02672
02673 if(buf_index+3 >= buf_size) break;
02674
02675 buf_index+=3;
02676 if(buf_index >= next_avc) continue;
02677 }
02678
02679 hx = h->thread_context[context_count];
02680
02681 ptr= ff_h264_decode_nal(hx, buf + buf_index, &dst_length, &consumed, next_avc - buf_index);
02682 if (ptr==NULL || dst_length < 0){
02683 return -1;
02684 }
02685 i= buf_index + consumed;
02686 if((s->workaround_bugs & FF_BUG_AUTODETECT) && i+3<next_avc &&
02687 buf[i]==0x00 && buf[i+1]==0x00 && buf[i+2]==0x01 && buf[i+3]==0xE0)
02688 s->workaround_bugs |= FF_BUG_TRUNCATED;
02689
02690 if(!(s->workaround_bugs & FF_BUG_TRUNCATED)){
02691 while(ptr[dst_length - 1] == 0 && dst_length > 0)
02692 dst_length--;
02693 }
02694 bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(h, ptr + dst_length - 1));
02695
02696 if(s->avctx->debug&FF_DEBUG_STARTCODE){
02697 av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d at %d/%d length %d\n", hx->nal_unit_type, buf_index, buf_size, dst_length);
02698 }
02699
02700 if (h->is_avc && (nalsize != consumed) && nalsize){
02701 av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
02702 }
02703
02704 buf_index += consumed;
02705
02706 if( (s->hurry_up == 1 && h->nal_ref_idc == 0)
02707 ||(avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0))
02708 continue;
02709
02710 again:
02711 err = 0;
02712 switch(hx->nal_unit_type){
02713 case NAL_IDR_SLICE:
02714 if (h->nal_unit_type != NAL_IDR_SLICE) {
02715 av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
02716 return -1;
02717 }
02718 idr(h);
02719 case NAL_SLICE:
02720 init_get_bits(&hx->s.gb, ptr, bit_length);
02721 hx->intra_gb_ptr=
02722 hx->inter_gb_ptr= &hx->s.gb;
02723 hx->s.data_partitioning = 0;
02724
02725 if((err = decode_slice_header(hx, h)))
02726 break;
02727
02728 if (h->current_slice == 1) {
02729 if (s->avctx->hwaccel && s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
02730 return -1;
02731 if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
02732 ff_vdpau_h264_picture_start(s);
02733 }
02734
02735 s->current_picture_ptr->key_frame |=
02736 (hx->nal_unit_type == NAL_IDR_SLICE) ||
02737 (h->sei_recovery_frame_cnt >= 0);
02738 if(hx->redundant_pic_count==0 && hx->s.hurry_up < 5
02739 && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
02740 && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
02741 && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
02742 && avctx->skip_frame < AVDISCARD_ALL){
02743 if(avctx->hwaccel) {
02744 if (avctx->hwaccel->decode_slice(avctx, &buf[buf_index - consumed], consumed) < 0)
02745 return -1;
02746 }else
02747 if(CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
02748 static const uint8_t start_code[] = {0x00, 0x00, 0x01};
02749 ff_vdpau_add_data_chunk(s, start_code, sizeof(start_code));
02750 ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed], consumed );
02751 }else
02752 context_count++;
02753 }
02754 break;
02755 case NAL_DPA:
02756 init_get_bits(&hx->s.gb, ptr, bit_length);
02757 hx->intra_gb_ptr=
02758 hx->inter_gb_ptr= NULL;
02759
02760 if ((err = decode_slice_header(hx, h)) < 0)
02761 break;
02762
02763 hx->s.data_partitioning = 1;
02764
02765 break;
02766 case NAL_DPB:
02767 init_get_bits(&hx->intra_gb, ptr, bit_length);
02768 hx->intra_gb_ptr= &hx->intra_gb;
02769 break;
02770 case NAL_DPC:
02771 init_get_bits(&hx->inter_gb, ptr, bit_length);
02772 hx->inter_gb_ptr= &hx->inter_gb;
02773
02774 if(hx->redundant_pic_count==0 && hx->intra_gb_ptr && hx->s.data_partitioning
02775 && s->context_initialized
02776 && s->hurry_up < 5
02777 && (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc)
02778 && (avctx->skip_frame < AVDISCARD_BIDIR || hx->slice_type_nos!=FF_B_TYPE)
02779 && (avctx->skip_frame < AVDISCARD_NONKEY || hx->slice_type_nos==FF_I_TYPE)
02780 && avctx->skip_frame < AVDISCARD_ALL)
02781 context_count++;
02782 break;
02783 case NAL_SEI:
02784 init_get_bits(&s->gb, ptr, bit_length);
02785 ff_h264_decode_sei(h);
02786 break;
02787 case NAL_SPS:
02788 init_get_bits(&s->gb, ptr, bit_length);
02789 ff_h264_decode_seq_parameter_set(h);
02790
02791 if(s->flags& CODEC_FLAG_LOW_DELAY)
02792 s->low_delay=1;
02793
02794 if(avctx->has_b_frames < 2)
02795 avctx->has_b_frames= !s->low_delay;
02796 break;
02797 case NAL_PPS:
02798 init_get_bits(&s->gb, ptr, bit_length);
02799
02800 ff_h264_decode_picture_parameter_set(h, bit_length);
02801
02802 break;
02803 case NAL_AUD:
02804 case NAL_END_SEQUENCE:
02805 case NAL_END_STREAM:
02806 case NAL_FILLER_DATA:
02807 case NAL_SPS_EXT:
02808 case NAL_AUXILIARY_SLICE:
02809 break;
02810 default:
02811 av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n", hx->nal_unit_type, bit_length);
02812 }
02813
02814 if(context_count == h->max_contexts) {
02815 execute_decode_slices(h, context_count);
02816 context_count = 0;
02817 }
02818
02819 if (err < 0)
02820 av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
02821 else if(err == 1) {
02822
02823
02824
02825
02826 h->nal_unit_type = hx->nal_unit_type;
02827 h->nal_ref_idc = hx->nal_ref_idc;
02828 hx = h;
02829 goto again;
02830 }
02831 }
02832 if(context_count)
02833 execute_decode_slices(h, context_count);
02834 return buf_index;
02835 }
02836
02840 static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
02841 if(pos==0) pos=1;
02842 if(pos+10>buf_size) pos=buf_size;
02843
02844 return pos;
02845 }
02846
02847 static int decode_frame(AVCodecContext *avctx,
02848 void *data, int *data_size,
02849 AVPacket *avpkt)
02850 {
02851 const uint8_t *buf = avpkt->data;
02852 int buf_size = avpkt->size;
02853 H264Context *h = avctx->priv_data;
02854 MpegEncContext *s = &h->s;
02855 AVFrame *pict = data;
02856 int buf_index;
02857
02858 s->flags= avctx->flags;
02859 s->flags2= avctx->flags2;
02860
02861
02862 out:
02863 if (buf_size == 0) {
02864 Picture *out;
02865 int i, out_idx;
02866
02867
02868 out = h->delayed_pic[0];
02869 out_idx = 0;
02870 for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
02871 if(h->delayed_pic[i]->poc < out->poc){
02872 out = h->delayed_pic[i];
02873 out_idx = i;
02874 }
02875
02876 for(i=out_idx; h->delayed_pic[i]; i++)
02877 h->delayed_pic[i] = h->delayed_pic[i+1];
02878
02879 if(out){
02880 *data_size = sizeof(AVFrame);
02881 *pict= *(AVFrame*)out;
02882 }
02883
02884 return 0;
02885 }
02886
02887 buf_index=decode_nal_units(h, buf, buf_size);
02888 if(buf_index < 0)
02889 return -1;
02890
02891 if (!s->current_picture_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
02892 buf_size = 0;
02893 goto out;
02894 }
02895
02896 if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
02897 if (avctx->skip_frame >= AVDISCARD_NONREF || s->hurry_up) return 0;
02898 av_log(avctx, AV_LOG_ERROR, "no frame!\n");
02899 return -1;
02900 }
02901
02902 if(!(s->flags2 & CODEC_FLAG2_CHUNKS) || (s->mb_y >= s->mb_height && s->mb_height)){
02903 Picture *out = s->current_picture_ptr;
02904 Picture *cur = s->current_picture_ptr;
02905 int i, pics, out_of_order, out_idx;
02906
02907 field_end(h);
02908
02909 if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
02910
02911 *data_size = 0;
02912
02913 } else {
02914 cur->interlaced_frame = 0;
02915 cur->repeat_pict = 0;
02916
02917
02918
02919
02920 if(h->sps.pic_struct_present_flag){
02921 switch (h->sei_pic_struct)
02922 {
02923 case SEI_PIC_STRUCT_FRAME:
02924 break;
02925 case SEI_PIC_STRUCT_TOP_FIELD:
02926 case SEI_PIC_STRUCT_BOTTOM_FIELD:
02927 cur->interlaced_frame = 1;
02928 break;
02929 case SEI_PIC_STRUCT_TOP_BOTTOM:
02930 case SEI_PIC_STRUCT_BOTTOM_TOP:
02931 if (FIELD_OR_MBAFF_PICTURE)
02932 cur->interlaced_frame = 1;
02933 else
02934
02935 cur->interlaced_frame = h->prev_interlaced_frame;
02936 break;
02937 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
02938 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
02939
02940
02941 cur->repeat_pict = 1;
02942 break;
02943 case SEI_PIC_STRUCT_FRAME_DOUBLING:
02944
02945 cur->repeat_pict = 2;
02946 break;
02947 case SEI_PIC_STRUCT_FRAME_TRIPLING:
02948 cur->repeat_pict = 4;
02949 break;
02950 }
02951
02952 if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
02953 cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
02954 }else{
02955
02956 cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
02957 }
02958 h->prev_interlaced_frame = cur->interlaced_frame;
02959
02960 if (cur->field_poc[0] != cur->field_poc[1]){
02961
02962 cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
02963 }else{
02964 if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
02965
02966 if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
02967 || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
02968 cur->top_field_first = 1;
02969 else
02970 cur->top_field_first = 0;
02971 }else{
02972
02973 cur->top_field_first = 0;
02974 }
02975 }
02976
02977
02978
02979
02980
02981 if(h->sps.bitstream_restriction_flag
02982 && s->avctx->has_b_frames < h->sps.num_reorder_frames){
02983 s->avctx->has_b_frames = h->sps.num_reorder_frames;
02984 s->low_delay = 0;
02985 }
02986
02987 if( s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
02988 && !h->sps.bitstream_restriction_flag){
02989 s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
02990 s->low_delay= 0;
02991 }
02992
02993 pics = 0;
02994 while(h->delayed_pic[pics]) pics++;
02995
02996 assert(pics <= MAX_DELAYED_PIC_COUNT);
02997
02998 h->delayed_pic[pics++] = cur;
02999 if(cur->reference == 0)
03000 cur->reference = DELAYED_PIC_REF;
03001
03002 out = h->delayed_pic[0];
03003 out_idx = 0;
03004 for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
03005 if(h->delayed_pic[i]->poc < out->poc){
03006 out = h->delayed_pic[i];
03007 out_idx = i;
03008 }
03009 if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
03010 h->outputed_poc= INT_MIN;
03011 out_of_order = out->poc < h->outputed_poc;
03012
03013 if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
03014 { }
03015 else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
03016 || (s->low_delay &&
03017 ((h->outputed_poc != INT_MIN && out->poc > h->outputed_poc + 2)
03018 || cur->pict_type == FF_B_TYPE)))
03019 {
03020 s->low_delay = 0;
03021 s->avctx->has_b_frames++;
03022 }
03023
03024 if(out_of_order || pics > s->avctx->has_b_frames){
03025 out->reference &= ~DELAYED_PIC_REF;
03026 for(i=out_idx; h->delayed_pic[i]; i++)
03027 h->delayed_pic[i] = h->delayed_pic[i+1];
03028 }
03029 if(!out_of_order && pics > s->avctx->has_b_frames){
03030 *data_size = sizeof(AVFrame);
03031
03032 if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
03033 h->outputed_poc = INT_MIN;
03034 } else
03035 h->outputed_poc = out->poc;
03036 *pict= *(AVFrame*)out;
03037 }else{
03038 av_log(avctx, AV_LOG_DEBUG, "no picture\n");
03039 }
03040 }
03041 }
03042
03043 assert(pict->data[0] || !*data_size);
03044 ff_print_debug_info(s, pict);
03045
03046
03047 return get_consumed_bytes(s, buf_index, buf_size);
03048 }
03049 #if 0
03050 static inline void fill_mb_avail(H264Context *h){
03051 MpegEncContext * const s = &h->s;
03052 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
03053
03054 if(s->mb_y){
03055 h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num;
03056 h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num;
03057 h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num;
03058 }else{
03059 h->mb_avail[0]=
03060 h->mb_avail[1]=
03061 h->mb_avail[2]= 0;
03062 }
03063 h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num;
03064 h->mb_avail[4]= 1;
03065 h->mb_avail[5]= 0;
03066 }
03067 #endif
03068
03069 #ifdef TEST
03070 #undef printf
03071 #undef random
03072 #define COUNT 8000
03073 #define SIZE (COUNT*40)
03074 int main(void){
03075 int i;
03076 uint8_t temp[SIZE];
03077 PutBitContext pb;
03078 GetBitContext gb;
03079
03080 DSPContext dsp;
03081 AVCodecContext avctx;
03082
03083 dsputil_init(&dsp, &avctx);
03084
03085 init_put_bits(&pb, temp, SIZE);
03086 printf("testing unsigned exp golomb\n");
03087 for(i=0; i<COUNT; i++){
03088 START_TIMER
03089 set_ue_golomb(&pb, i);
03090 STOP_TIMER("set_ue_golomb");
03091 }
03092 flush_put_bits(&pb);
03093
03094 init_get_bits(&gb, temp, 8*SIZE);
03095 for(i=0; i<COUNT; i++){
03096 int j, s;
03097
03098 s= show_bits(&gb, 24);
03099
03100 START_TIMER
03101 j= get_ue_golomb(&gb);
03102 if(j != i){
03103 printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
03104
03105 }
03106 STOP_TIMER("get_ue_golomb");
03107 }
03108
03109
03110 init_put_bits(&pb, temp, SIZE);
03111 printf("testing signed exp golomb\n");
03112 for(i=0; i<COUNT; i++){
03113 START_TIMER
03114 set_se_golomb(&pb, i - COUNT/2);
03115 STOP_TIMER("set_se_golomb");
03116 }
03117 flush_put_bits(&pb);
03118
03119 init_get_bits(&gb, temp, 8*SIZE);
03120 for(i=0; i<COUNT; i++){
03121 int j, s;
03122
03123 s= show_bits(&gb, 24);
03124
03125 START_TIMER
03126 j= get_se_golomb(&gb);
03127 if(j != i - COUNT/2){
03128 printf("mismatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s);
03129
03130 }
03131 STOP_TIMER("get_se_golomb");
03132 }
03133
03134 #if 0
03135 printf("testing 4x4 (I)DCT\n");
03136
03137 DCTELEM block[16];
03138 uint8_t src[16], ref[16];
03139 uint64_t error= 0, max_error=0;
03140
03141 for(i=0; i<COUNT; i++){
03142 int j;
03143
03144 for(j=0; j<16; j++){
03145 ref[j]= random()%255;
03146 src[j]= random()%255;
03147 }
03148
03149 h264_diff_dct_c(block, src, ref, 4);
03150
03151
03152 for(j=0; j<16; j++){
03153
03154 block[j]= block[j]*4;
03155 if(j&1) block[j]= (block[j]*4 + 2)/5;
03156 if(j&4) block[j]= (block[j]*4 + 2)/5;
03157 }
03158
03159
03160 h->h264dsp.h264_idct_add(ref, block, 4);
03161
03162
03163
03164
03165
03166 for(j=0; j<16; j++){
03167 int diff= FFABS(src[j] - ref[j]);
03168
03169 error+= diff*diff;
03170 max_error= FFMAX(max_error, diff);
03171 }
03172 }
03173 printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
03174 printf("testing quantizer\n");
03175 for(qp=0; qp<52; qp++){
03176 for(i=0; i<16; i++)
03177 src1_block[i]= src2_block[i]= random()%255;
03178
03179 }
03180 printf("Testing NAL layer\n");
03181
03182 uint8_t bitstream[COUNT];
03183 uint8_t nal[COUNT*2];
03184 H264Context h;
03185 memset(&h, 0, sizeof(H264Context));
03186
03187 for(i=0; i<COUNT; i++){
03188 int zeros= i;
03189 int nal_length;
03190 int consumed;
03191 int out_length;
03192 uint8_t *out;
03193 int j;
03194
03195 for(j=0; j<COUNT; j++){
03196 bitstream[j]= (random() % 255) + 1;
03197 }
03198
03199 for(j=0; j<zeros; j++){
03200 int pos= random() % COUNT;
03201 while(bitstream[pos] == 0){
03202 pos++;
03203 pos %= COUNT;
03204 }
03205 bitstream[pos]=0;
03206 }
03207
03208 START_TIMER
03209
03210 nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
03211 if(nal_length<0){
03212 printf("encoding failed\n");
03213 return -1;
03214 }
03215
03216 out= ff_h264_decode_nal(&h, nal, &out_length, &consumed, nal_length);
03217
03218 STOP_TIMER("NAL")
03219
03220 if(out_length != COUNT){
03221 printf("incorrect length %d %d\n", out_length, COUNT);
03222 return -1;
03223 }
03224
03225 if(consumed != nal_length){
03226 printf("incorrect consumed length %d %d\n", nal_length, consumed);
03227 return -1;
03228 }
03229
03230 if(memcmp(bitstream, out, COUNT)){
03231 printf("mismatch\n");
03232 return -1;
03233 }
03234 }
03235 #endif
03236
03237 printf("Testing RBSP\n");
03238
03239
03240 return 0;
03241 }
03242 #endif
03243
03244
03245 av_cold void ff_h264_free_context(H264Context *h)
03246 {
03247 int i;
03248
03249 free_tables(h);
03250
03251 for(i = 0; i < MAX_SPS_COUNT; i++)
03252 av_freep(h->sps_buffers + i);
03253
03254 for(i = 0; i < MAX_PPS_COUNT; i++)
03255 av_freep(h->pps_buffers + i);
03256 }
03257
03258 av_cold int ff_h264_decode_end(AVCodecContext *avctx)
03259 {
03260 H264Context *h = avctx->priv_data;
03261 MpegEncContext *s = &h->s;
03262
03263 ff_h264_free_context(h);
03264
03265 MPV_common_end(s);
03266
03267
03268
03269 return 0;
03270 }
03271
03272
03273 AVCodec h264_decoder = {
03274 "h264",
03275 AVMEDIA_TYPE_VIDEO,
03276 CODEC_ID_H264,
03277 sizeof(H264Context),
03278 ff_h264_decode_init,
03279 NULL,
03280 ff_h264_decode_end,
03281 decode_frame,
03282 CODEC_CAP_DR1 | CODEC_CAP_DELAY,
03283 .flush= flush_dpb,
03284 .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
03285 };
03286
03287 #if CONFIG_H264_VDPAU_DECODER
03288 AVCodec h264_vdpau_decoder = {
03289 "h264_vdpau",
03290 AVMEDIA_TYPE_VIDEO,
03291 CODEC_ID_H264,
03292 sizeof(H264Context),
03293 ff_h264_decode_init,
03294 NULL,
03295 ff_h264_decode_end,
03296 decode_frame,
03297 CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
03298 .flush= flush_dpb,
03299 .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
03300 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},
03301 };
03302 #endif