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