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