comparison mpegvideo.c @ 612:c0005de2be59 libavcodec

new ratecontrol code
author michaelni
date Sun, 25 Aug 2002 21:19:50 +0000
parents 3214d3f4519e
children 4f3d4a07d374
comparison
equal deleted inserted replaced
611:3214d3f4519e 612:c0005de2be59
224 224
225 if(s->codec_id==CODEC_ID_MPEG4){ 225 if(s->codec_id==CODEC_ID_MPEG4){
226 CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE); 226 CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE);
227 CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE); 227 CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE);
228 } 228 }
229
230 CHECKED_ALLOCZ(s->avctx->stats_out, 256);
229 } 231 }
230 232
231 if (s->out_format == FMT_H263 || s->encoding) { 233 if (s->out_format == FMT_H263 || s->encoding) {
232 int size; 234 int size;
233 /* Allocate MB type table */ 235 /* Allocate MB type table */
326 av_freep(&s->bitstream_buffer); 328 av_freep(&s->bitstream_buffer);
327 av_freep(&s->tex_pb_buffer); 329 av_freep(&s->tex_pb_buffer);
328 av_freep(&s->pb2_buffer); 330 av_freep(&s->pb2_buffer);
329 av_freep(&s->edge_emu_buffer); 331 av_freep(&s->edge_emu_buffer);
330 av_freep(&s->non_b_mv4_table); 332 av_freep(&s->non_b_mv4_table);
331 333 av_freep(&s->avctx->stats_out);
334
332 for(i=0;i<3;i++) { 335 for(i=0;i<3;i++) {
333 int j; 336 int j;
334 if(!(s->flags&CODEC_FLAG_DR1)){ 337 if(!(s->flags&CODEC_FLAG_DR1)){
335 av_freep(&s->last_picture_base[i]); 338 av_freep(&s->last_picture_base[i]);
336 av_freep(&s->next_picture_base[i]); 339 av_freep(&s->next_picture_base[i]);
375 s->qmin= avctx->qmin; 378 s->qmin= avctx->qmin;
376 s->qmax= avctx->qmax; 379 s->qmax= avctx->qmax;
377 s->max_qdiff= avctx->max_qdiff; 380 s->max_qdiff= avctx->max_qdiff;
378 s->qcompress= avctx->qcompress; 381 s->qcompress= avctx->qcompress;
379 s->qblur= avctx->qblur; 382 s->qblur= avctx->qblur;
380 s->b_quant_factor= avctx->b_quant_factor;
381 s->b_quant_offset= avctx->b_quant_offset;
382 s->avctx = avctx; 383 s->avctx = avctx;
383 s->aspect_ratio_info= avctx->aspect_ratio_info; 384 s->aspect_ratio_info= avctx->aspect_ratio_info;
384 s->flags= avctx->flags; 385 s->flags= avctx->flags;
385 s->max_b_frames= avctx->max_b_frames; 386 s->max_b_frames= avctx->max_b_frames;
386 s->rc_strategy= avctx->rc_strategy;
387 s->b_frame_strategy= avctx->b_frame_strategy; 387 s->b_frame_strategy= avctx->b_frame_strategy;
388 s->codec_id= avctx->codec->id; 388 s->codec_id= avctx->codec->id;
389 s->luma_elim_threshold = avctx->luma_elim_threshold; 389 s->luma_elim_threshold = avctx->luma_elim_threshold;
390 s->chroma_elim_threshold= avctx->chroma_elim_threshold; 390 s->chroma_elim_threshold= avctx->chroma_elim_threshold;
391 s->strict_std_compliance= avctx->strict_std_compliance; 391 s->strict_std_compliance= avctx->strict_std_compliance;
676 avctx->dr_opaque_frame= s->last_dr_opaque; 676 avctx->dr_opaque_frame= s->last_dr_opaque;
677 else 677 else
678 avctx->dr_opaque_frame= s->next_dr_opaque; 678 avctx->dr_opaque_frame= s->next_dr_opaque;
679 } 679 }
680 } 680 }
681
682 /* set dequantizer, we cant do it during init as it might change for mpeg4 681 /* set dequantizer, we cant do it during init as it might change for mpeg4
683 and we cant do it in the header decode as init isnt called for mpeg4 there yet */ 682 and we cant do it in the header decode as init isnt called for mpeg4 there yet */
684 if(s->out_format == FMT_H263){ 683 if(s->out_format == FMT_H263){
685 if(s->mpeg_quant) 684 if(s->mpeg_quant)
686 s->dct_unquantize = s->dct_unquantize_mpeg2; 685 s->dct_unquantize = s->dct_unquantize_mpeg2;
701 draw_edges(s->current_picture[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2); 700 draw_edges(s->current_picture[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
702 draw_edges(s->current_picture[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2); 701 draw_edges(s->current_picture[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2);
703 } 702 }
704 emms_c(); 703 emms_c();
705 704
705 s->last_pict_type = s->pict_type;
706 if(s->pict_type!=B_TYPE){ 706 if(s->pict_type!=B_TYPE){
707 s->last_non_b_pict_type= s->pict_type; 707 s->last_non_b_pict_type= s->pict_type;
708 s->last_non_b_qscale= s->qscale;
709 s->last_non_b_mc_mb_var= s->mc_mb_var_sum;
710 s->num_available_buffers++; 708 s->num_available_buffers++;
711 if(s->num_available_buffers>2) s->num_available_buffers= 2; 709 if(s->num_available_buffers>2) s->num_available_buffers= 2;
712 } 710 }
713 } 711 }
714 712
871 s->input_picture_number++; 869 s->input_picture_number++;
872 s->input_picture_in_gop_number++; 870 s->input_picture_in_gop_number++;
873 871
874 flush_put_bits(&s->pb); 872 flush_put_bits(&s->pb);
875 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8; 873 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8;
876 if(s->pict_type==B_TYPE) s->pb_frame_bits+= s->frame_bits; 874
877 else s->pb_frame_bits= s->frame_bits;
878
879 s->total_bits += s->frame_bits; 875 s->total_bits += s->frame_bits;
880 avctx->frame_bits = s->frame_bits; 876 avctx->frame_bits = s->frame_bits;
881 //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", 877 //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n",
882 //s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits); 878 //s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits);
879 #if 0 //dump some stats to stats.txt for testing/debuging
880 if(s->max_b_frames==0)
881 {
882 static FILE *f=NULL;
883 if(!f) f= fopen("stats.txt", "wb");
884 get_psnr(pict->data, s->current_picture,
885 pict->linesize, s->linesize, avctx);
886 fprintf(f, "%7d, %7d, %2.4f\n", pbBufPtr(&s->pb) - s->pb.buf, s->qscale, avctx->psnr_y);
887 }
888 #endif
883 889
884 if (avctx->get_psnr) { 890 if (avctx->get_psnr) {
885 /* At this point pict->data should have the original frame */ 891 /* At this point pict->data should have the original frame */
886 /* an s->current_picture should have the coded/decoded frame */ 892 /* an s->current_picture should have the coded/decoded frame */
887 get_psnr(pict->data, s->current_picture, 893 get_psnr(pict->data, s->current_picture,
2008 /* I-Frame */ 2014 /* I-Frame */
2009 //FIXME do we need to zero them? 2015 //FIXME do we need to zero them?
2010 memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2); 2016 memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2);
2011 memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2); 2017 memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2);
2012 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); 2018 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
2019
2020 if(!s->fixed_qscale){
2021 /* finding spatial complexity for I-frame rate control */
2022 for(mb_y=0; mb_y < s->mb_height; mb_y++) {
2023 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2024 int xx = mb_x * 16;
2025 int yy = mb_y * 16;
2026 uint8_t *pix = s->new_picture[0] + (yy * s->linesize) + xx;
2027 int varc;
2028 int sum = pix_sum(pix, s->linesize);
2029
2030 sum= (sum+8)>>4;
2031 varc = (pix_norm1(pix, s->linesize) - sum*sum + 500 + 128)>>8;
2032
2033 s->mb_var[s->mb_width * mb_y + mb_x] = varc;
2034 s->mb_var_sum += varc;
2035 }
2036 }
2037 }
2013 } 2038 }
2014 if(s->scene_change_score > 0 && s->pict_type == P_TYPE){ 2039 if(s->scene_change_score > 0 && s->pict_type == P_TYPE){
2015 s->pict_type= I_TYPE; 2040 s->pict_type= I_TYPE;
2016 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); 2041 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height);
2017 if(s->max_b_frames==0){ 2042 if(s->max_b_frames==0){
2018 s->input_pict_type= I_TYPE; 2043 s->input_pict_type= I_TYPE;
2019 s->input_picture_in_gop_number=0; 2044 s->input_picture_in_gop_number=0;
2020 } 2045 }
2021 //printf("Scene change detected, encoding as I Frame\n"); 2046 //printf("Scene change detected, encoding as I Frame %d %d\n", s->mb_var_sum, s->mc_mb_var_sum);
2022 } 2047 }
2023 2048
2024 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) 2049 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE)
2025 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER); 2050 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
2026 ff_fix_long_p_mvs(s); 2051 ff_fix_long_p_mvs(s);
2035 } 2060 }
2036 2061
2037 //printf("f_code %d ///\n", s->f_code); 2062 //printf("f_code %d ///\n", s->f_code);
2038 2063
2039 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var); 2064 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var);
2040 if(s->flags&CODEC_FLAG_PASS2) 2065 if (!s->fixed_qscale)
2041 s->qscale = ff_rate_estimate_qscale_pass2(s);
2042 else if (!s->fixed_qscale)
2043 s->qscale = ff_rate_estimate_qscale(s); 2066 s->qscale = ff_rate_estimate_qscale(s);
2044 2067
2045 if (s->out_format == FMT_MJPEG) { 2068 if (s->out_format == FMT_MJPEG) {
2046 /* for mjpeg, we do include qscale in the matrix */ 2069 /* for mjpeg, we do include qscale in the matrix */
2047 s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0]; 2070 s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
2673 int mb_y= s->mb_y; 2696 int mb_y= s->mb_y;
2674 int mb_dist=0; 2697 int mb_dist=0;
2675 int i, intra_count=0, inter_count=0; 2698 int i, intra_count=0, inter_count=0;
2676 int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune 2699 int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune
2677 int inter_conceal= s->msmpeg4_version ? 50 : 50; 2700 int inter_conceal= s->msmpeg4_version ? 50 : 50;
2678 2701
2679 // for last block 2702 // for last block
2680 if(mb_x>=s->mb_width) mb_x= s->mb_width -1; 2703 if(mb_x>=s->mb_width) mb_x= s->mb_width -1;
2681 if(mb_y>=s->mb_height) mb_y= s->mb_height-1; 2704 if(mb_y>=s->mb_height) mb_y= s->mb_height-1;
2682 2705
2683 if(s->decoding_error==0 && unknown_pos){ 2706 if(s->decoding_error==0 && unknown_pos){