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