00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <xvid.h>
00024 #include <unistd.h>
00025 #include "libavutil/file.h"
00026 #include "avcodec.h"
00027 #include "libxvid_internal.h"
00028
00029 #include "mpegvideo.h"
00030
00031 #undef NDEBUG
00032 #include <assert.h>
00033
00034 extern unsigned int xvid_debug;
00035
00036 int ff_xvid_rate_control_init(MpegEncContext *s){
00037 char *tmp_name;
00038 int fd, i;
00039 xvid_plg_create_t xvid_plg_create;
00040 xvid_plugin_2pass2_t xvid_2pass2;
00041
00042
00043
00044 fd=av_tempfile("xvidrc.", &tmp_name, 0, s->avctx);
00045 if (fd == -1) {
00046 av_log(NULL, AV_LOG_ERROR, "Can't create temporary pass2 file.\n");
00047 return -1;
00048 }
00049
00050 for(i=0; i<s->rc_context.num_entries; i++){
00051 static const char *frame_types = " ipbs";
00052 char tmp[256];
00053 RateControlEntry *rce;
00054
00055 rce= &s->rc_context.entry[i];
00056
00057 snprintf(tmp, sizeof(tmp), "%c %d %d %d %d %d %d\n",
00058 frame_types[rce->pict_type], (int)lrintf(rce->qscale / FF_QP2LAMBDA), rce->i_count, s->mb_num - rce->i_count - rce->skip_count,
00059 rce->skip_count, (rce->i_tex_bits + rce->p_tex_bits + rce->misc_bits+7)/8, (rce->header_bits+rce->mv_bits+7)/8);
00060
00061
00062 write(fd, tmp, strlen(tmp));
00063 }
00064
00065 close(fd);
00066
00067 memset(&xvid_2pass2, 0, sizeof(xvid_2pass2));
00068 xvid_2pass2.version= XVID_MAKE_VERSION(1,1,0);
00069 xvid_2pass2.filename= tmp_name;
00070 xvid_2pass2.bitrate= s->avctx->bit_rate;
00071 xvid_2pass2.vbv_size= s->avctx->rc_buffer_size;
00072 xvid_2pass2.vbv_maxrate= s->avctx->rc_max_rate;
00073 xvid_2pass2.vbv_initial= s->avctx->rc_initial_buffer_occupancy;
00074
00075 memset(&xvid_plg_create, 0, sizeof(xvid_plg_create));
00076 xvid_plg_create.version= XVID_MAKE_VERSION(1,1,0);
00077 xvid_plg_create.fbase= s->avctx->time_base.den;
00078 xvid_plg_create.fincr= s->avctx->time_base.num;
00079 xvid_plg_create.param= &xvid_2pass2;
00080
00081 if(xvid_plugin_2pass2(NULL, XVID_PLG_CREATE, &xvid_plg_create, &s->rc_context.non_lavc_opaque)<0){
00082 av_log(NULL, AV_LOG_ERROR, "xvid_plugin_2pass2 failed\n");
00083 return -1;
00084 }
00085 return 0;
00086 }
00087
00088 float ff_xvid_rate_estimate_qscale(MpegEncContext *s, int dry_run){
00089 xvid_plg_data_t xvid_plg_data;
00090
00091 memset(&xvid_plg_data, 0, sizeof(xvid_plg_data));
00092 xvid_plg_data.version= XVID_MAKE_VERSION(1,1,0);
00093 xvid_plg_data.width = s->width;
00094 xvid_plg_data.height= s->height;
00095 xvid_plg_data.mb_width = s->mb_width;
00096 xvid_plg_data.mb_height= s->mb_height;
00097 xvid_plg_data.fbase= s->avctx->time_base.den;
00098 xvid_plg_data.fincr= s->avctx->time_base.num;
00099 xvid_plg_data.min_quant[0]= s->avctx->qmin;
00100 xvid_plg_data.min_quant[1]= s->avctx->qmin;
00101 xvid_plg_data.min_quant[2]= s->avctx->qmin;
00102 xvid_plg_data.max_quant[0]= s->avctx->qmax;
00103 xvid_plg_data.max_quant[1]= s->avctx->qmax;
00104 xvid_plg_data.max_quant[2]= s->avctx->qmax;
00105 xvid_plg_data.bquant_offset = 0;
00106 xvid_plg_data.bquant_ratio = 100;
00107
00108 #if 0
00109 xvid_plg_data.stats.hlength= X
00110 #endif
00111
00112 if(!s->rc_context.dry_run_qscale){
00113 if(s->picture_number){
00114 xvid_plg_data.length=
00115 xvid_plg_data.stats.length= (s->frame_bits + 7)/8;
00116 xvid_plg_data.frame_num= s->rc_context.last_picture_number;
00117 xvid_plg_data.quant= s->qscale;
00118
00119 xvid_plg_data.type= s->last_pict_type;
00120 if(xvid_plugin_2pass2(s->rc_context.non_lavc_opaque, XVID_PLG_AFTER, &xvid_plg_data, NULL)){
00121 av_log(s->avctx, AV_LOG_ERROR, "xvid_plugin_2pass2(handle, XVID_PLG_AFTER, ...) FAILED\n");
00122 return -1;
00123 }
00124 }
00125 s->rc_context.last_picture_number=
00126 xvid_plg_data.frame_num= s->picture_number;
00127 xvid_plg_data.quant= 0;
00128 if(xvid_plugin_2pass2(s->rc_context.non_lavc_opaque, XVID_PLG_BEFORE, &xvid_plg_data, NULL)){
00129 av_log(s->avctx, AV_LOG_ERROR, "xvid_plugin_2pass2(handle, XVID_PLG_BEFORE, ...) FAILED\n");
00130 return -1;
00131 }
00132 s->rc_context.dry_run_qscale= xvid_plg_data.quant;
00133 }
00134 xvid_plg_data.quant= s->rc_context.dry_run_qscale;
00135 if(!dry_run)
00136 s->rc_context.dry_run_qscale= 0;
00137
00138 if(s->pict_type == AV_PICTURE_TYPE_B)
00139 return xvid_plg_data.quant * FF_QP2LAMBDA * s->avctx->b_quant_factor + s->avctx->b_quant_offset;
00140 else
00141 return xvid_plg_data.quant * FF_QP2LAMBDA;
00142 }
00143
00144 void ff_xvid_rate_control_uninit(MpegEncContext *s){
00145 xvid_plg_destroy_t xvid_plg_destroy;
00146
00147 xvid_plugin_2pass2(s->rc_context.non_lavc_opaque, XVID_PLG_DESTROY, &xvid_plg_destroy, NULL);
00148 }
00149