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