annotate ratecontrol.c @ 348:ae9e9af0b627 libavcodec

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