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