annotate ratecontrol.c @ 448:e8c8ca9106aa libavcodec

* removed MANGLE from macros for setting constants * using MOVQ_WONE/MOVQ_BFE as two instruction instead of static memory value access as its always faster * PAVGB_MMX macro is using now mm6 -> mm7 is unmodified * replaced original pixels_xy2_mmx with new faster and equal implementation * replaced usage of mm7 for other then ZERO contstant in _rnd & _avg file with mm6
author kabi
date Thu, 30 May 2002 15:14:56 +0000
parents dd4f4c3d7171
children 295ce90742ff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
329
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
1 /*
428
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
2 * Rate control for video encoders
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
3 *
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
4 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
5 *
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
6 * This library is free software; you can redistribute it and/or
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
8 * License as published by the Free Software Foundation; either
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
9 * version 2 of the License, or (at your option) any later version.
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
10 *
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
11 * This library is distributed in the hope that it will be useful,
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
14 * Lesser General Public License for more details.
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
15 *
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
17 * License along with this library; if not, write to the Free Software
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
19 */
329
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
20 #include "avcodec.h"
428
dd4f4c3d7171 consistent include usage
glantau
parents: 396
diff changeset
21 #include "dsputil.h"
329
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
22 #include "mpegvideo.h"
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
23
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
24 #define STATS_FILE "lavc_stats.txt"
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
25
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
26 static int init_pass2(MpegEncContext *s);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
27
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
28 void ff_write_pass1_stats(MpegEncContext *s){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
29 RateControlContext *rcc= &s->rc_context;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
30 // fprintf(c->stats_file, "type:%d q:%d icount:%d pcount:%d scount:%d itex:%d ptex%d mv:%d misc:%d fcode:%d bcode:%d\")
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
31 fprintf(rcc->stats_file, "in:%d out:%d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d fcode:%d bcode:%d\n",
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
32 s->picture_number, s->input_picture_number - s->max_b_frames, s->pict_type,
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
33 s->qscale, s->i_tex_bits, s->p_tex_bits, s->mv_bits, s->misc_bits, s->f_code, s->b_code);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
34 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
35
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
36 int ff_rate_control_init(MpegEncContext *s)
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
37 {
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
38 RateControlContext *rcc= &s->rc_context;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
39 emms_c();
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
40
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
41 if(s->flags&CODEC_FLAG_PASS1){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
42 rcc->stats_file= fopen(STATS_FILE, "w");
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
43 if(!rcc->stats_file){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
44 fprintf(stderr, "failed to open " STATS_FILE "\n");
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
45 return -1;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
46 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
47 } else if(s->flags&CODEC_FLAG_PASS2){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
48 int size;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
49 int i;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
50
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
51 rcc->stats_file= fopen(STATS_FILE, "r");
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
52 if(!rcc->stats_file){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
53 fprintf(stderr, "failed to open " STATS_FILE "\n");
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
54 return -1;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
55 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
56
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
57 /* find number of pics without reading the file twice :) */
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
58 fseek(rcc->stats_file, 0, SEEK_END);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
59 size= ftell(rcc->stats_file);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
60 fseek(rcc->stats_file, 0, SEEK_SET);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
61
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
62 size/= 64; // we need at least 64 byte to store a line ...
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
63 rcc->entry = (RateControlEntry*)av_mallocz(size*sizeof(RateControlEntry));
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
64
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
65 for(i=0; !feof(rcc->stats_file); i++){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
66 RateControlEntry *rce;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
67 int picture_number;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
68 int e;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
69
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
70 e= fscanf(rcc->stats_file, "in:%d ", &picture_number);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
71 rce= &rcc->entry[picture_number];
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
72 e+=fscanf(rcc->stats_file, "out:%*d type:%d q:%d itex:%d ptex:%d mv:%d misc:%d fcode:%*d bcode:%*d\n",
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
73 &rce->pict_type, &rce->qscale, &rce->i_tex_bits, &rce->p_tex_bits, &rce->mv_bits, &rce->misc_bits);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
74 if(e!=7){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
75 fprintf(stderr, STATS_FILE " is damaged\n");
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
76 return -1;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
77 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
78 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
79 rcc->num_entries= i;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
80
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
81 if(init_pass2(s) < 0) return -1;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
82 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
83
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
84 /* no 2pass stuff, just normal 1-pass */
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
85 //initial values, they dont really matter as they will be totally different within a few frames
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
86 s->i_pred.coeff= s->p_pred.coeff= 7.0;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
87 s->i_pred.count= s->p_pred.count= 1.0;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
88
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
89 s->i_pred.decay= s->p_pred.decay= 0.4;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
90
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
91 // use more bits at the beginning, otherwise high motion at the begin will look like shit
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
92 s->qsum=100 * s->qmin;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
93 s->qcount=100;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
94
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
95 s->short_term_qsum=0.001;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
96 s->short_term_qcount=0.001;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
97
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
98 return 0;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
99 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
100
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
101 void ff_rate_control_uninit(MpegEncContext *s)
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
102 {
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
103 RateControlContext *rcc= &s->rc_context;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
104 emms_c();
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
105
396
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 388
diff changeset
106 if(rcc->stats_file)
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 388
diff changeset
107 fclose(rcc->stats_file);
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 388
diff changeset
108 rcc->stats_file = NULL;
fce0a2520551 removed useless header includes - use av memory functions
glantau
parents: 388
diff changeset
109 av_freep(&rcc->entry);
329
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
110 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
111
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
112 //----------------------------------
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
113 // 1 Pass Code
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
114
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
115 static double predict(Predictor *p, double q, double var)
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
116 {
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
117 return p->coeff*var / (q*p->count);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
118 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
119
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
120 static void update_predictor(Predictor *p, double q, double var, double size)
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
121 {
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
122 double new_coeff= size*q / (var + 1);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
123 if(var<1000) return;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
124
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
125 p->count*= p->decay;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
126 p->coeff*= p->decay;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
127 p->count++;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
128 p->coeff+= new_coeff;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
129 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
130
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
131 int ff_rate_estimate_qscale(MpegEncContext *s)
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
132 {
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
133 int qmin= s->qmin;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
134 int qmax= s->qmax;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
135 int rate_q=5;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
136 float q;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
137 int qscale;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
138 float br_compensation;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
139 double diff;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
140 double short_term_q;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
141 double long_term_q;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
142 double fps;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
143 int picture_number= s->input_picture_number - s->max_b_frames;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
144 int64_t wanted_bits;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
145 emms_c();
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
146
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
147 fps= (double)s->frame_rate / FRAME_RATE_BASE;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
148 wanted_bits= (uint64_t)(s->bit_rate*(double)picture_number/fps);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
149 // printf("%d %d %d\n", picture_number, (int)wanted_bits, (int)s->total_bits);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
150
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
151 if(s->pict_type==B_TYPE){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
152 qmin= (int)(qmin*s->b_quant_factor+0.5);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
153 qmax= (int)(qmax*s->b_quant_factor+0.5);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
154 }
388
83964a31b319 qmin==1 bugfix
michaelni
parents: 329
diff changeset
155 if(qmin<1) qmin=1;
329
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
156 if(qmax>31) qmax=31;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
157 if(qmax<=qmin) qmax= qmin;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
158
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
159 /* update predictors */
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
160 if(picture_number>2){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
161 if(s->pict_type!=B_TYPE && s->last_non_b_pict_type == P_TYPE){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
162 //printf("%d %d %d %f\n", s->qscale, s->last_mc_mb_var, s->frame_bits, s->p_pred.coeff);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
163 update_predictor(&s->p_pred, s->last_non_b_qscale, s->last_non_b_mc_mb_var, s->pb_frame_bits);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
164 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
165 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
166
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
167 if(s->pict_type == I_TYPE){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
168 short_term_q= s->short_term_qsum/s->short_term_qcount;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
169
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
170 long_term_q= s->qsum/s->qcount*(s->total_bits+1)/(wanted_bits+1); //+1 to avoid nan & 0
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
171
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
172 q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
173 }else if(s->pict_type==B_TYPE){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
174 q= (int)(s->last_non_b_qscale*s->b_quant_factor+0.5);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
175 }else{ //P Frame
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
176 int i;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
177 int diff, best_diff=1000000000;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
178 for(i=1; i<=31; i++){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
179 diff= predict(&s->p_pred, i, s->mc_mb_var) - (double)s->bit_rate/fps;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
180 if(diff<0) diff= -diff;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
181 if(diff<best_diff){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
182 best_diff= diff;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
183 rate_q= i;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
184 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
185 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
186 s->short_term_qsum*=s->qblur;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
187 s->short_term_qcount*=s->qblur;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
188
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
189 s->short_term_qsum+= rate_q;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
190 s->short_term_qcount++;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
191 short_term_q= s->short_term_qsum/s->short_term_qcount;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
192
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
193 long_term_q= s->qsum/s->qcount*(s->total_bits+1)/(wanted_bits+1); //+1 to avoid nan & 0
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
194
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
195 // q= (long_term_q - short_term_q)*s->qcompress + short_term_q;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
196 q= 1/((1/long_term_q - 1/short_term_q)*s->qcompress + 1/short_term_q);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
197 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
198
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
199 diff= s->total_bits - wanted_bits;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
200 br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
201 if(br_compensation<=0.0) br_compensation=0.001;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
202 q/=br_compensation;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
203 //printf("%f %f %f\n", q, br_compensation, short_term_q);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
204 qscale= (int)(q + 0.5);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
205 if (qscale<qmin) qscale=qmin;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
206 else if(qscale>qmax) qscale=qmax;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
207
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
208 if(s->pict_type!=B_TYPE){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
209 s->qsum+= qscale;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
210 s->qcount++;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
211 if (qscale<s->last_non_b_qscale-s->max_qdiff) qscale=s->last_non_b_qscale-s->max_qdiff;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
212 else if(qscale>s->last_non_b_qscale+s->max_qdiff) qscale=s->last_non_b_qscale+s->max_qdiff;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
213 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
214 //printf("q:%d diff:%d comp:%f rate_q:%d st_q:%f fvar:%d last_size:%d\n", qscale, (int)diff, br_compensation,
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
215 // rate_q, short_term_q, s->mc_mb_var, s->frame_bits);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
216 //printf("%d %d\n", s->bit_rate, (int)fps);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
217 return qscale;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
218 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
219
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
220 //----------------------------------------------
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
221 // 2-Pass code
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
222
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
223 static int init_pass2(MpegEncContext *s)
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
224 {
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
225 RateControlContext *rcc= &s->rc_context;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
226 int i;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
227 double fps= (double)s->frame_rate / FRAME_RATE_BASE;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
228 double complexity[5]={0,0,0,0,0}; // aproximate bits at quant=1
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
229 double avg_quantizer[5];
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
230 uint64_t const_bits[5]={0,0,0,0,0}; // quantizer idependant bits
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
231 uint64_t available_bits[5];
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
232 uint64_t all_const_bits;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
233 uint64_t all_available_bits= (uint64_t)(s->bit_rate*(double)rcc->num_entries/fps);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
234 int num_frames[5]={0,0,0,0,0};
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
235 double rate_factor=0;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
236 double step;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
237 int last_i_frame=-10000000;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
238
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
239 /* find complexity & const_bits & decide the pict_types */
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
240 for(i=0; i<rcc->num_entries; i++){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
241 RateControlEntry *rce= &rcc->entry[i];
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
242
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
243 if(s->b_frame_strategy==0 || s->max_b_frames==0){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
244 rce->new_pict_type= rce->pict_type;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
245 }else{
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
246 int j;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
247 int next_non_b_type=P_TYPE;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
248
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
249 switch(rce->pict_type){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
250 case I_TYPE:
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
251 if(i-last_i_frame>s->gop_size/2){ //FIXME this is not optimal
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
252 rce->new_pict_type= I_TYPE;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
253 last_i_frame= i;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
254 }else{
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
255 rce->new_pict_type= P_TYPE; // will be caught by the scene detection anyway
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
256 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
257 break;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
258 case P_TYPE:
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
259 rce->new_pict_type= P_TYPE;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
260 break;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
261 case B_TYPE:
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
262 for(j=i+1; j<i+s->max_b_frames+2 && j<rcc->num_entries; j++){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
263 if(rcc->entry[j].pict_type != B_TYPE){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
264 next_non_b_type= rcc->entry[j].pict_type;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
265 break;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
266 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
267 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
268 if(next_non_b_type==I_TYPE)
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
269 rce->new_pict_type= P_TYPE;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
270 else
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
271 rce->new_pict_type= B_TYPE;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
272 break;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
273 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
274 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
275
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
276 complexity[rce->new_pict_type]+= (rce->i_tex_bits+ rce->p_tex_bits)*(double)rce->qscale;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
277 const_bits[rce->new_pict_type]+= rce->mv_bits + rce->misc_bits;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
278 num_frames[rce->new_pict_type]++;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
279 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
280 all_const_bits= const_bits[I_TYPE] + const_bits[P_TYPE] + const_bits[B_TYPE];
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
281
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
282 if(all_available_bits < all_const_bits){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
283 fprintf(stderr, "requested bitrate is to low\n");
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
284 return -1;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
285 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
286
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
287 // avg_complexity= complexity/rcc->num_entries;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
288 avg_quantizer[P_TYPE]=
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
289 avg_quantizer[I_TYPE]= (complexity[I_TYPE]+complexity[P_TYPE] + complexity[B_TYPE]/s->b_quant_factor)
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
290 / (all_available_bits - all_const_bits);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
291 avg_quantizer[B_TYPE]= avg_quantizer[P_TYPE]*s->b_quant_factor;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
292 //printf("avg quantizer: %f %f\n", avg_quantizer[P_TYPE], avg_quantizer[B_TYPE]);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
293
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
294 for(i=0; i<5; i++){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
295 available_bits[i]= const_bits[i] + complexity[i]/avg_quantizer[i];
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
296 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
297 //printf("%lld %lld %lld %lld\n", available_bits[I_TYPE], available_bits[P_TYPE], available_bits[B_TYPE], all_available_bits);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
298
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
299 for(step=256*256; step>0.0000001; step*=0.5){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
300 uint64_t expected_bits=0;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
301 rate_factor+= step;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
302 /* find qscale */
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
303 for(i=0; i<rcc->num_entries; i++){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
304 RateControlEntry *rce= &rcc->entry[i];
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
305 double short_term_q, q, bits_left;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
306 const int pict_type= rce->new_pict_type;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
307 int qmin= s->qmin;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
308 int qmax= s->qmax;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
309
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
310 if(pict_type==B_TYPE){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
311 qmin= (int)(qmin*s->b_quant_factor+0.5);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
312 qmax= (int)(qmax*s->b_quant_factor+0.5);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
313 }
388
83964a31b319 qmin==1 bugfix
michaelni
parents: 329
diff changeset
314 if(qmin<1) qmin=1;
329
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
315 if(qmax>31) qmax=31;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
316 if(qmax<=qmin) qmax= qmin;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
317
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
318 switch(s->rc_strategy){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
319 case 0:
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
320 bits_left= available_bits[pict_type]/num_frames[pict_type]*rate_factor - rce->misc_bits - rce->mv_bits;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
321 if(bits_left<1.0) bits_left=1.0;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
322 short_term_q= rce->qscale*(rce->i_tex_bits + rce->p_tex_bits)/bits_left;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
323 break;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
324 case 1:
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
325 bits_left= (available_bits[pict_type] - const_bits[pict_type])/num_frames[pict_type]*rate_factor;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
326 if(bits_left<1.0) bits_left=1.0;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
327 short_term_q= rce->qscale*(rce->i_tex_bits + rce->p_tex_bits)/bits_left;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
328 break;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
329 case 2:
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
330 bits_left= available_bits[pict_type]/num_frames[pict_type]*rate_factor;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
331 if(bits_left<1.0) bits_left=1.0;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
332 short_term_q= rce->qscale*(rce->i_tex_bits + rce->p_tex_bits + rce->misc_bits + rce->mv_bits)/bits_left;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
333 break;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
334 default:
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
335 fprintf(stderr, "unknown strategy\n");
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
336 short_term_q=3; //gcc warning fix
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
337 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
338
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
339 if(short_term_q>31.0) short_term_q=31.0;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
340 else if (short_term_q<1.0) short_term_q=1.0;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
341
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
342 q= 1/((1/avg_quantizer[pict_type] - 1/short_term_q)*s->qcompress + 1/short_term_q);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
343 if (q<qmin) q=qmin;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
344 else if(q>qmax) q=qmax;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
345 //printf("lq:%f, sq:%f t:%f q:%f\n", avg_quantizer[rce->pict_type], short_term_q, bits_left, q);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
346 rce->new_qscale= q;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
347 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
348
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
349 /* smooth curve */
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
350
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
351 /* find expected bits */
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
352 for(i=0; i<rcc->num_entries; i++){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
353 RateControlEntry *rce= &rcc->entry[i];
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
354 double factor= rce->qscale / rce->new_qscale;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
355
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
356 rce->expected_bits= expected_bits;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
357 expected_bits += (int)(rce->misc_bits + rce->mv_bits + (rce->i_tex_bits + rce->p_tex_bits)*factor + 0.5);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
358 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
359
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
360 // printf("%d %d %f\n", (int)expected_bits, (int)all_available_bits, rate_factor);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
361 if(expected_bits > all_available_bits) rate_factor-= step;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
362 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
363
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
364 return 0;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
365 }
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
366
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
367 int ff_rate_estimate_qscale_pass2(MpegEncContext *s)
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
368 {
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
369 int qmin= s->qmin;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
370 int qmax= s->qmax;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
371 float q;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
372 int qscale;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
373 float br_compensation;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
374 double diff;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
375 int picture_number= s->picture_number;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
376 RateControlEntry *rce= &s->rc_context.entry[picture_number];
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
377 int64_t wanted_bits= rce->expected_bits;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
378 emms_c();
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
379
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
380 // printf("%d %d %d\n", picture_number, (int)wanted_bits, (int)s->total_bits);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
381
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
382 if(s->pict_type==B_TYPE){
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
383 qmin= (int)(qmin*s->b_quant_factor+0.5);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
384 qmax= (int)(qmax*s->b_quant_factor+0.5);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
385 }
388
83964a31b319 qmin==1 bugfix
michaelni
parents: 329
diff changeset
386 if(qmin<1) qmin=1;
329
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
387 if(qmax>31) qmax=31;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
388 if(qmax<=qmin) qmax= qmin;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
389
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
390 q= rce->new_qscale;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
391
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
392 diff= s->total_bits - wanted_bits;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
393 br_compensation= (s->bit_rate_tolerance - diff)/s->bit_rate_tolerance;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
394 if(br_compensation<=0.0) br_compensation=0.001;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
395 q/=br_compensation;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
396
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
397 qscale= (int)(q + 0.5);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
398 if (qscale<qmin) qscale=qmin;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
399 else if(qscale>qmax) qscale=qmax;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
400 // printf("%d %d %d %d type:%d\n", qmin, qscale, qmax, picture_number, s->pict_type); fflush(stdout);
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
401 return qscale;
5cc47d0ba53e fixed ratecontrol & b-frames
michaelni
parents:
diff changeset
402 }