00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "avcodec.h"
00031 #include "dsputil.h"
00032 #include "mpegvideo.h"
00033 #include "mpegvideo_common.h"
00034 #include "mjpegenc.h"
00035 #include "msmpeg4.h"
00036 #include "faandct.h"
00037 #include <limits.h>
00038
00039
00040
00041
00042 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
00043 DCTELEM *block, int n, int qscale);
00044 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
00045 DCTELEM *block, int n, int qscale);
00046 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
00047 DCTELEM *block, int n, int qscale);
00048 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
00049 DCTELEM *block, int n, int qscale);
00050 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
00051 DCTELEM *block, int n, int qscale);
00052 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
00053 DCTELEM *block, int n, int qscale);
00054 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
00055 DCTELEM *block, int n, int qscale);
00056
00057 extern int XVMC_field_start(MpegEncContext*s, AVCodecContext *avctx);
00058 extern void XVMC_field_end(MpegEncContext *s);
00059 extern void XVMC_decode_mb(MpegEncContext *s);
00060
00061
00062
00063
00064
00065
00066
00067
00068 static const uint8_t ff_default_chroma_qscale_table[32]={
00069
00070 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
00071 };
00072
00073 const uint8_t ff_mpeg1_dc_scale_table[128]={
00074
00075 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00076 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00077 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00078 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00079 };
00080
00081
00082 const uint8_t *ff_find_start_code(const uint8_t * restrict p, const uint8_t *end, uint32_t * restrict state){
00083 int i;
00084
00085 assert(p<=end);
00086 if(p>=end)
00087 return end;
00088
00089 for(i=0; i<3; i++){
00090 uint32_t tmp= *state << 8;
00091 *state= tmp + *(p++);
00092 if(tmp == 0x100 || p==end)
00093 return p;
00094 }
00095
00096 while(p<end){
00097 if (p[-1] > 1 ) p+= 3;
00098 else if(p[-2] ) p+= 2;
00099 else if(p[-3]|(p[-1]-1)) p++;
00100 else{
00101 p++;
00102 break;
00103 }
00104 }
00105
00106 p= FFMIN(p, end)-4;
00107 *state= AV_RB32(p);
00108
00109 return p+4;
00110 }
00111
00112
00113 int ff_dct_common_init(MpegEncContext *s)
00114 {
00115 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
00116 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
00117 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
00118 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
00119 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
00120 if(s->flags & CODEC_FLAG_BITEXACT)
00121 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
00122 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
00123
00124 #if defined(HAVE_MMX)
00125 MPV_common_init_mmx(s);
00126 #elif defined(ARCH_ALPHA)
00127 MPV_common_init_axp(s);
00128 #elif defined(CONFIG_MLIB)
00129 MPV_common_init_mlib(s);
00130 #elif defined(HAVE_MMI)
00131 MPV_common_init_mmi(s);
00132 #elif defined(ARCH_ARMV4L)
00133 MPV_common_init_armv4l(s);
00134 #elif defined(HAVE_ALTIVEC)
00135 MPV_common_init_altivec(s);
00136 #elif defined(ARCH_BFIN)
00137 MPV_common_init_bfin(s);
00138 #endif
00139
00140
00141
00142
00143 if(s->alternate_scan){
00144 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
00145 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
00146 }else{
00147 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
00148 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
00149 }
00150 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
00151 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
00152
00153 return 0;
00154 }
00155
00156 void copy_picture(Picture *dst, Picture *src){
00157 *dst = *src;
00158 dst->type= FF_BUFFER_TYPE_COPY;
00159 }
00160
00161
00162
00163
00164
00165 int alloc_picture(MpegEncContext *s, Picture *pic, int shared){
00166 const int big_mb_num= s->mb_stride*(s->mb_height+1) + 1;
00167 const int mb_array_size= s->mb_stride*s->mb_height;
00168 const int b8_array_size= s->b8_stride*s->mb_height*2;
00169 const int b4_array_size= s->b4_stride*s->mb_height*4;
00170 int i;
00171 int r= -1;
00172
00173 if(shared){
00174 assert(pic->data[0]);
00175 assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED);
00176 pic->type= FF_BUFFER_TYPE_SHARED;
00177 }else{
00178 assert(!pic->data[0]);
00179
00180 r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic);
00181
00182 if(r<0 || !pic->age || !pic->type || !pic->data[0]){
00183 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %d %p)\n", r, pic->age, pic->type, pic->data[0]);
00184 return -1;
00185 }
00186
00187 if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){
00188 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (stride changed)\n");
00189 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
00190 return -1;
00191 }
00192
00193 if(pic->linesize[1] != pic->linesize[2]){
00194 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (uv stride mismatch)\n");
00195 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
00196 return -1;
00197 }
00198
00199 s->linesize = pic->linesize[0];
00200 s->uvlinesize= pic->linesize[1];
00201 }
00202
00203 if(pic->qscale_table==NULL){
00204 if (s->encoding) {
00205 CHECKED_ALLOCZ(pic->mb_var , mb_array_size * sizeof(int16_t))
00206 CHECKED_ALLOCZ(pic->mc_mb_var, mb_array_size * sizeof(int16_t))
00207 CHECKED_ALLOCZ(pic->mb_mean , mb_array_size * sizeof(int8_t))
00208 }
00209
00210 CHECKED_ALLOCZ(pic->mbskip_table , mb_array_size * sizeof(uint8_t)+2)
00211 CHECKED_ALLOCZ(pic->qscale_table , mb_array_size * sizeof(uint8_t))
00212 CHECKED_ALLOCZ(pic->mb_type_base , (big_mb_num + s->mb_stride) * sizeof(uint32_t))
00213 pic->mb_type= pic->mb_type_base + 2*s->mb_stride+1;
00214 if(s->out_format == FMT_H264){
00215 for(i=0; i<2; i++){
00216 CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b4_array_size+4) * sizeof(int16_t))
00217 pic->motion_val[i]= pic->motion_val_base[i]+4;
00218 CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
00219 }
00220 pic->motion_subsample_log2= 2;
00221 }else if(s->out_format == FMT_H263 || s->encoding || (s->avctx->debug&FF_DEBUG_MV) || (s->avctx->debug_mv)){
00222 for(i=0; i<2; i++){
00223 CHECKED_ALLOCZ(pic->motion_val_base[i], 2 * (b8_array_size+4) * sizeof(int16_t))
00224 pic->motion_val[i]= pic->motion_val_base[i]+4;
00225 CHECKED_ALLOCZ(pic->ref_index[i], b8_array_size * sizeof(uint8_t))
00226 }
00227 pic->motion_subsample_log2= 3;
00228 }
00229 if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
00230 CHECKED_ALLOCZ(pic->dct_coeff, 64 * mb_array_size * sizeof(DCTELEM)*6)
00231 }
00232 pic->qstride= s->mb_stride;
00233 CHECKED_ALLOCZ(pic->pan_scan , 1 * sizeof(AVPanScan))
00234 }
00235
00236
00237
00238 memmove(s->prev_pict_types+1, s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE-1);
00239 s->prev_pict_types[0]= s->dropable ? FF_B_TYPE : s->pict_type;
00240 if(pic->age < PREV_PICT_TYPES_BUFFER_SIZE && s->prev_pict_types[pic->age] == FF_B_TYPE)
00241 pic->age= INT_MAX;
00242
00243 return 0;
00244 fail:
00245 if(r>=0)
00246 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
00247 return -1;
00248 }
00249
00250
00251
00252
00253 static void free_picture(MpegEncContext *s, Picture *pic){
00254 int i;
00255
00256 if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){
00257 s->avctx->release_buffer(s->avctx, (AVFrame*)pic);
00258 }
00259
00260 av_freep(&pic->mb_var);
00261 av_freep(&pic->mc_mb_var);
00262 av_freep(&pic->mb_mean);
00263 av_freep(&pic->mbskip_table);
00264 av_freep(&pic->qscale_table);
00265 av_freep(&pic->mb_type_base);
00266 av_freep(&pic->dct_coeff);
00267 av_freep(&pic->pan_scan);
00268 pic->mb_type= NULL;
00269 for(i=0; i<2; i++){
00270 av_freep(&pic->motion_val_base[i]);
00271 av_freep(&pic->ref_index[i]);
00272 }
00273
00274 if(pic->type == FF_BUFFER_TYPE_SHARED){
00275 for(i=0; i<4; i++){
00276 pic->base[i]=
00277 pic->data[i]= NULL;
00278 }
00279 pic->type= 0;
00280 }
00281 }
00282
00283 static int init_duplicate_context(MpegEncContext *s, MpegEncContext *base){
00284 int i;
00285
00286
00287 CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*21*2);
00288 s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*21;
00289
00290
00291 CHECKED_ALLOCZ(s->me.scratchpad, (s->width+64)*4*16*2*sizeof(uint8_t))
00292 s->rd_scratchpad= s->me.scratchpad;
00293 s->b_scratchpad= s->me.scratchpad;
00294 s->obmc_scratchpad= s->me.scratchpad + 16;
00295 if (s->encoding) {
00296 CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t))
00297 CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t))
00298 if(s->avctx->noise_reduction){
00299 CHECKED_ALLOCZ(s->dct_error_sum, 2 * 64 * sizeof(int))
00300 }
00301 }
00302 CHECKED_ALLOCZ(s->blocks, 64*12*2 * sizeof(DCTELEM))
00303 s->block= s->blocks[0];
00304
00305 for(i=0;i<12;i++){
00306 s->pblocks[i] = (short *)(&s->block[i]);
00307 }
00308 return 0;
00309 fail:
00310 return -1;
00311 }
00312
00313 static void free_duplicate_context(MpegEncContext *s){
00314 if(s==NULL) return;
00315
00316 av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL;
00317 av_freep(&s->me.scratchpad);
00318 s->rd_scratchpad=
00319 s->b_scratchpad=
00320 s->obmc_scratchpad= NULL;
00321
00322 av_freep(&s->dct_error_sum);
00323 av_freep(&s->me.map);
00324 av_freep(&s->me.score_map);
00325 av_freep(&s->blocks);
00326 s->block= NULL;
00327 }
00328
00329 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src){
00330 #define COPY(a) bak->a= src->a
00331 COPY(allocated_edge_emu_buffer);
00332 COPY(edge_emu_buffer);
00333 COPY(me.scratchpad);
00334 COPY(rd_scratchpad);
00335 COPY(b_scratchpad);
00336 COPY(obmc_scratchpad);
00337 COPY(me.map);
00338 COPY(me.score_map);
00339 COPY(blocks);
00340 COPY(block);
00341 COPY(start_mb_y);
00342 COPY(end_mb_y);
00343 COPY(me.map_generation);
00344 COPY(pb);
00345 COPY(dct_error_sum);
00346 COPY(dct_count[0]);
00347 COPY(dct_count[1]);
00348 #undef COPY
00349 }
00350
00351 void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src){
00352 MpegEncContext bak;
00353 int i;
00354
00355
00356 backup_duplicate_context(&bak, dst);
00357 memcpy(dst, src, sizeof(MpegEncContext));
00358 backup_duplicate_context(dst, &bak);
00359 for(i=0;i<12;i++){
00360 dst->pblocks[i] = (short *)(&dst->block[i]);
00361 }
00362
00363 }
00364
00365
00366
00367
00368
00369 void MPV_common_defaults(MpegEncContext *s){
00370 s->y_dc_scale_table=
00371 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
00372 s->chroma_qscale_table= ff_default_chroma_qscale_table;
00373 s->progressive_frame= 1;
00374 s->progressive_sequence= 1;
00375 s->picture_structure= PICT_FRAME;
00376
00377 s->coded_picture_number = 0;
00378 s->picture_number = 0;
00379 s->input_picture_number = 0;
00380
00381 s->picture_in_gop_number = 0;
00382
00383 s->f_code = 1;
00384 s->b_code = 1;
00385 }
00386
00387
00388
00389
00390
00391 void MPV_decode_defaults(MpegEncContext *s){
00392 MPV_common_defaults(s);
00393 }
00394
00395
00396
00397
00398
00399 int MPV_common_init(MpegEncContext *s)
00400 {
00401 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y, threads;
00402
00403 s->mb_height = (s->height + 15) / 16;
00404
00405 if(s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height)){
00406 av_log(s->avctx, AV_LOG_ERROR, "too many threads\n");
00407 return -1;
00408 }
00409
00410 if((s->width || s->height) && avcodec_check_dimensions(s->avctx, s->width, s->height))
00411 return -1;
00412
00413 dsputil_init(&s->dsp, s->avctx);
00414 ff_dct_common_init(s);
00415
00416 s->flags= s->avctx->flags;
00417 s->flags2= s->avctx->flags2;
00418
00419 s->mb_width = (s->width + 15) / 16;
00420 s->mb_stride = s->mb_width + 1;
00421 s->b8_stride = s->mb_width*2 + 1;
00422 s->b4_stride = s->mb_width*4 + 1;
00423 mb_array_size= s->mb_height * s->mb_stride;
00424 mv_table_size= (s->mb_height+2) * s->mb_stride + 1;
00425
00426
00427 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),
00428 &(s->chroma_y_shift) );
00429
00430
00431 s->h_edge_pos= s->mb_width*16;
00432 s->v_edge_pos= s->mb_height*16;
00433
00434 s->mb_num = s->mb_width * s->mb_height;
00435
00436 s->block_wrap[0]=
00437 s->block_wrap[1]=
00438 s->block_wrap[2]=
00439 s->block_wrap[3]= s->b8_stride;
00440 s->block_wrap[4]=
00441 s->block_wrap[5]= s->mb_stride;
00442
00443 y_size = s->b8_stride * (2 * s->mb_height + 1);
00444 c_size = s->mb_stride * (s->mb_height + 1);
00445 yc_size = y_size + 2 * c_size;
00446
00447
00448 s->codec_tag= toupper( s->avctx->codec_tag &0xFF)
00449 + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )
00450 + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)
00451 + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);
00452
00453 s->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF)
00454 + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )
00455 + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)
00456 + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);
00457
00458 s->avctx->coded_frame= (AVFrame*)&s->current_picture;
00459
00460 CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int))
00461 for(y=0; y<s->mb_height; y++){
00462 for(x=0; x<s->mb_width; x++){
00463 s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;
00464 }
00465 }
00466 s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width;
00467
00468 if (s->encoding) {
00469
00470 CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00471 CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00472 CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00473 CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00474 CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00475 CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))
00476 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
00477 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
00478 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
00479 s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
00480 s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
00481 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
00482
00483 if(s->msmpeg4_version){
00484 CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));
00485 }
00486 CHECKED_ALLOCZ(s->avctx->stats_out, 256);
00487
00488
00489 CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint16_t))
00490
00491 CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))
00492
00493 CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))
00494 CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))
00495 CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))
00496 CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))
00497 CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
00498 CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))
00499
00500 if(s->avctx->noise_reduction){
00501 CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))
00502 }
00503 }
00504 CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))
00505
00506 CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))
00507
00508 if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
00509
00510 for(i=0; i<2; i++){
00511 int j, k;
00512 for(j=0; j<2; j++){
00513 for(k=0; k<2; k++){
00514 CHECKED_ALLOCZ(s->b_field_mv_table_base[i][j][k] , mv_table_size * 2 * sizeof(int16_t))
00515 s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
00516 }
00517 CHECKED_ALLOCZ(s->b_field_select_table[i][j] , mb_array_size * 2 * sizeof(uint8_t))
00518 CHECKED_ALLOCZ(s->p_field_mv_table_base[i][j] , mv_table_size * 2 * sizeof(int16_t))
00519 s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
00520 }
00521 CHECKED_ALLOCZ(s->p_field_select_table[i] , mb_array_size * 2 * sizeof(uint8_t))
00522 }
00523 }
00524 if (s->out_format == FMT_H263) {
00525
00526 CHECKED_ALLOCZ(s->ac_val_base, yc_size * sizeof(int16_t) * 16);
00527 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
00528 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
00529 s->ac_val[2] = s->ac_val[1] + c_size;
00530
00531
00532 CHECKED_ALLOCZ(s->coded_block_base, y_size);
00533 s->coded_block= s->coded_block_base + s->b8_stride + 1;
00534
00535
00536 CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))
00537 CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))
00538 }
00539
00540 if (s->h263_pred || s->h263_plus || !s->encoding) {
00541
00542
00543 CHECKED_ALLOCZ(s->dc_val_base, yc_size * sizeof(int16_t));
00544 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
00545 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
00546 s->dc_val[2] = s->dc_val[1] + c_size;
00547 for(i=0;i<yc_size;i++)
00548 s->dc_val_base[i] = 1024;
00549 }
00550
00551
00552 CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);
00553 memset(s->mbintra_table, 1, mb_array_size);
00554
00555
00556 CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);
00557
00558 CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);
00559
00560 s->parse_context.state= -1;
00561 if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){
00562 s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);
00563 s->visualization_buffer[1] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
00564 s->visualization_buffer[2] = av_malloc((s->mb_width*8 + EDGE_WIDTH) * s->mb_height*8 + EDGE_WIDTH);
00565 }
00566
00567 s->context_initialized = 1;
00568
00569 s->thread_context[0]= s;
00570 threads = s->avctx->thread_count;
00571
00572 for(i=1; i<threads; i++){
00573 s->thread_context[i]= av_malloc(sizeof(MpegEncContext));
00574 memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
00575 }
00576
00577 for(i=0; i<threads; i++){
00578 if(init_duplicate_context(s->thread_context[i], s) < 0)
00579 goto fail;
00580 s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count;
00581 s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;
00582 }
00583
00584 return 0;
00585 fail:
00586 MPV_common_end(s);
00587 return -1;
00588 }
00589
00590
00591 void MPV_common_end(MpegEncContext *s)
00592 {
00593 int i, j, k;
00594
00595 for(i=0; i<s->avctx->thread_count; i++){
00596 free_duplicate_context(s->thread_context[i]);
00597 }
00598 for(i=1; i<s->avctx->thread_count; i++){
00599 av_freep(&s->thread_context[i]);
00600 }
00601
00602 av_freep(&s->parse_context.buffer);
00603 s->parse_context.buffer_size=0;
00604
00605 av_freep(&s->mb_type);
00606 av_freep(&s->p_mv_table_base);
00607 av_freep(&s->b_forw_mv_table_base);
00608 av_freep(&s->b_back_mv_table_base);
00609 av_freep(&s->b_bidir_forw_mv_table_base);
00610 av_freep(&s->b_bidir_back_mv_table_base);
00611 av_freep(&s->b_direct_mv_table_base);
00612 s->p_mv_table= NULL;
00613 s->b_forw_mv_table= NULL;
00614 s->b_back_mv_table= NULL;
00615 s->b_bidir_forw_mv_table= NULL;
00616 s->b_bidir_back_mv_table= NULL;
00617 s->b_direct_mv_table= NULL;
00618 for(i=0; i<2; i++){
00619 for(j=0; j<2; j++){
00620 for(k=0; k<2; k++){
00621 av_freep(&s->b_field_mv_table_base[i][j][k]);
00622 s->b_field_mv_table[i][j][k]=NULL;
00623 }
00624 av_freep(&s->b_field_select_table[i][j]);
00625 av_freep(&s->p_field_mv_table_base[i][j]);
00626 s->p_field_mv_table[i][j]=NULL;
00627 }
00628 av_freep(&s->p_field_select_table[i]);
00629 }
00630
00631 av_freep(&s->dc_val_base);
00632 av_freep(&s->ac_val_base);
00633 av_freep(&s->coded_block_base);
00634 av_freep(&s->mbintra_table);
00635 av_freep(&s->cbp_table);
00636 av_freep(&s->pred_dir_table);
00637
00638 av_freep(&s->mbskip_table);
00639 av_freep(&s->prev_pict_types);
00640 av_freep(&s->bitstream_buffer);
00641 s->allocated_bitstream_buffer_size=0;
00642
00643 av_freep(&s->avctx->stats_out);
00644 av_freep(&s->ac_stats);
00645 av_freep(&s->error_status_table);
00646 av_freep(&s->mb_index2xy);
00647 av_freep(&s->lambda_table);
00648 av_freep(&s->q_intra_matrix);
00649 av_freep(&s->q_inter_matrix);
00650 av_freep(&s->q_intra_matrix16);
00651 av_freep(&s->q_inter_matrix16);
00652 av_freep(&s->input_picture);
00653 av_freep(&s->reordered_input_picture);
00654 av_freep(&s->dct_offset);
00655
00656 if(s->picture){
00657 for(i=0; i<MAX_PICTURE_COUNT; i++){
00658 free_picture(s, &s->picture[i]);
00659 }
00660 }
00661 av_freep(&s->picture);
00662 s->context_initialized = 0;
00663 s->last_picture_ptr=
00664 s->next_picture_ptr=
00665 s->current_picture_ptr= NULL;
00666 s->linesize= s->uvlinesize= 0;
00667
00668 for(i=0; i<3; i++)
00669 av_freep(&s->visualization_buffer[i]);
00670
00671 avcodec_default_free_buffers(s->avctx);
00672 }
00673
00674 void init_rl(RLTable *rl, uint8_t static_store[2][2*MAX_RUN + MAX_LEVEL + 3])
00675 {
00676 int8_t max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
00677 uint8_t index_run[MAX_RUN+1];
00678 int last, run, level, start, end, i;
00679
00680
00681 if(static_store && rl->max_level[0])
00682 return;
00683
00684
00685 for(last=0;last<2;last++) {
00686 if (last == 0) {
00687 start = 0;
00688 end = rl->last;
00689 } else {
00690 start = rl->last;
00691 end = rl->n;
00692 }
00693
00694 memset(max_level, 0, MAX_RUN + 1);
00695 memset(max_run, 0, MAX_LEVEL + 1);
00696 memset(index_run, rl->n, MAX_RUN + 1);
00697 for(i=start;i<end;i++) {
00698 run = rl->table_run[i];
00699 level = rl->table_level[i];
00700 if (index_run[run] == rl->n)
00701 index_run[run] = i;
00702 if (level > max_level[run])
00703 max_level[run] = level;
00704 if (run > max_run[level])
00705 max_run[level] = run;
00706 }
00707 if(static_store)
00708 rl->max_level[last] = static_store[last];
00709 else
00710 rl->max_level[last] = av_malloc(MAX_RUN + 1);
00711 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
00712 if(static_store)
00713 rl->max_run[last] = static_store[last] + MAX_RUN + 1;
00714 else
00715 rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
00716 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
00717 if(static_store)
00718 rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
00719 else
00720 rl->index_run[last] = av_malloc(MAX_RUN + 1);
00721 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
00722 }
00723 }
00724
00725 void init_vlc_rl(RLTable *rl)
00726 {
00727 int i, q;
00728
00729 for(q=0; q<32; q++){
00730 int qmul= q*2;
00731 int qadd= (q-1)|1;
00732
00733 if(q==0){
00734 qmul=1;
00735 qadd=0;
00736 }
00737 for(i=0; i<rl->vlc.table_size; i++){
00738 int code= rl->vlc.table[i][0];
00739 int len = rl->vlc.table[i][1];
00740 int level, run;
00741
00742 if(len==0){
00743 run= 66;
00744 level= MAX_LEVEL;
00745 }else if(len<0){
00746 run= 0;
00747 level= code;
00748 }else{
00749 if(code==rl->n){
00750 run= 66;
00751 level= 0;
00752 }else{
00753 run= rl->table_run [code] + 1;
00754 level= rl->table_level[code] * qmul + qadd;
00755 if(code >= rl->last) run+=192;
00756 }
00757 }
00758 rl->rl_vlc[q][i].len= len;
00759 rl->rl_vlc[q][i].level= level;
00760 rl->rl_vlc[q][i].run= run;
00761 }
00762 }
00763 }
00764
00765 int ff_find_unused_picture(MpegEncContext *s, int shared){
00766 int i;
00767
00768 if(shared){
00769 for(i=0; i<MAX_PICTURE_COUNT; i++){
00770 if(s->picture[i].data[0]==NULL && s->picture[i].type==0) return i;
00771 }
00772 }else{
00773 for(i=0; i<MAX_PICTURE_COUNT; i++){
00774 if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i;
00775 }
00776 for(i=0; i<MAX_PICTURE_COUNT; i++){
00777 if(s->picture[i].data[0]==NULL) return i;
00778 }
00779 }
00780
00781 av_log(s->avctx, AV_LOG_FATAL, "Internal error, picture buffer overflow\n");
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793 abort();
00794 return -1;
00795 }
00796
00797 static void update_noise_reduction(MpegEncContext *s){
00798 int intra, i;
00799
00800 for(intra=0; intra<2; intra++){
00801 if(s->dct_count[intra] > (1<<16)){
00802 for(i=0; i<64; i++){
00803 s->dct_error_sum[intra][i] >>=1;
00804 }
00805 s->dct_count[intra] >>= 1;
00806 }
00807
00808 for(i=0; i<64; i++){
00809 s->dct_offset[intra][i]= (s->avctx->noise_reduction * s->dct_count[intra] + s->dct_error_sum[intra][i]/2) / (s->dct_error_sum[intra][i]+1);
00810 }
00811 }
00812 }
00813
00814
00815
00816
00817 int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
00818 {
00819 int i;
00820 AVFrame *pic;
00821 s->mb_skipped = 0;
00822
00823 assert(s->last_picture_ptr==NULL || s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3);
00824
00825
00826 if (s->pict_type != FF_B_TYPE && s->last_picture_ptr && s->last_picture_ptr != s->next_picture_ptr && s->last_picture_ptr->data[0]) {
00827 if(s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3){
00828 avctx->release_buffer(avctx, (AVFrame*)s->last_picture_ptr);
00829
00830
00831
00832 if(!s->encoding){
00833 for(i=0; i<MAX_PICTURE_COUNT; i++){
00834 if(s->picture[i].data[0] && &s->picture[i] != s->next_picture_ptr && s->picture[i].reference){
00835 av_log(avctx, AV_LOG_ERROR, "releasing zombie picture\n");
00836 avctx->release_buffer(avctx, (AV