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