2641
|
1 /*
|
|
2 * divx4_vbr.c
|
|
3 *
|
|
4 * Copyright (C) Thomas Östreich - June 2001
|
|
5 *
|
|
6 * 2-pass code OpenDivX port:
|
|
7 * Copyright (C) 2001 Christoph Lampert <gruel@gmx.de>
|
|
8 *
|
|
9 * This file is part of transcode, a linux video stream processing tool
|
|
10 *
|
|
11 * transcode is free software; you can redistribute it and/or modify
|
|
12 * it under the terms of the GNU General Public License as published by
|
|
13 * the Free Software Foundation; either version 2, or (at your option)
|
|
14 * any later version.
|
|
15 *
|
|
16 * transcode is distributed in the hope that it will be useful,
|
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 * GNU General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * along with GNU Make; see the file COPYING. If not, write to
|
|
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
24 *
|
|
25 */
|
|
26
|
|
27
|
|
28 /**********************************************************
|
|
29 * Two-pass-code from OpenDivX *
|
|
30 * *
|
|
31 * Large parts of this code were taken from VbrControl() *
|
|
32 * from the OpenDivX project, (C) divxnetworks, *
|
|
33 * this code is published under DivX Open license, which *
|
|
34 * can be found... somewhere... oh, whatever... *
|
|
35 **********************************************************/
|
|
36
|
|
37 #include <stdio.h>
|
|
38 #include <stdlib.h>
|
2642
|
39 #include <string.h>
|
2641
|
40 #include <sys/stat.h>
|
|
41 #include <unistd.h>
|
|
42 #include <fcntl.h>
|
|
43 #include <dlfcn.h>
|
|
44 #include <math.h>
|
|
45
|
|
46 #include <stdint.h>
|
|
47
|
|
48 #include "config.h"
|
2642
|
49
|
|
50 #include "divx4_vbr.h"
|
|
51
|
|
52 //#include "transcode.h"
|
2641
|
53
|
|
54 #define FALSE 0
|
|
55 #define TRUE 1
|
|
56
|
|
57 /* Absolute maximum and minimum quantizers used in VBR modes */
|
|
58 static const int min_quantizer=1;
|
|
59 static const int max_quantizer=31;
|
|
60
|
|
61 /* Limits on frame-level deviation of quantizer ( higher values
|
|
62 correspond to frames with more changes and vice versa ) */
|
|
63 static const float min_quant_delta=-10.f;
|
|
64 static const float max_quant_delta=5.f;
|
|
65 /* Limits on stream-level deviation of quantizer ( used to make
|
|
66 overall bitrate of stream close to requested value ) */
|
|
67 static const float min_rc_quant_delta=.6f;
|
|
68 static const float max_rc_quant_delta=1.5f;
|
|
69
|
|
70 /* Crispness parameter controls threshold for decision whether
|
|
71 to skip the frame or to code it. */
|
|
72 //static const float max_crispness=100.f;
|
|
73 /* Maximum allowed number of skipped frames in a line. */
|
|
74 //static const int max_drops_line=0; // CHL We don't drop frames at the moment!
|
|
75
|
|
76
|
|
77 typedef struct entry_s
|
|
78 /* max 28 bytes/frame or 5 Mb for 2-hour movie */
|
|
79 {
|
|
80 int quant;
|
|
81 int text_bits;
|
|
82 int motion_bits;
|
|
83 int total_bits;
|
|
84 float mult;
|
|
85 short is_key_frame;
|
|
86 short drop;
|
|
87 } entry;
|
|
88
|
2642
|
89 static int m_iCount;
|
|
90 static int m_iQuant;
|
|
91 static int m_iCrispness;
|
|
92 static short m_bDrop;
|
|
93 static float m_fQuant;
|
2641
|
94
|
2642
|
95 static int64_t m_lEncodedBits;
|
|
96 static int64_t m_lExpectedBits;
|
2641
|
97
|
2642
|
98 static FILE *m_pFile;
|
2641
|
99
|
2642
|
100 static entry vFrame;
|
|
101 static entry *m_vFrames;
|
|
102 static long lFrameStart;
|
2641
|
103
|
2642
|
104 static int iNumFrames;
|
|
105 static int dummy;
|
2641
|
106
|
|
107
|
|
108 void VbrControl_init_1pass_vbr(int quality, int crispness)
|
|
109 {
|
|
110 m_fQuant=min_quantizer+((max_quantizer-min_quantizer)/6.)*(6-quality);
|
|
111 m_iCount=0;
|
|
112 m_bDrop=FALSE;
|
|
113 VbrControl_update_1pass_vbr();
|
|
114 }
|
|
115
|
|
116 int VbrControl_init_2pass_vbr_analysis(const char *filename, int quality)
|
|
117 {
|
|
118 m_pFile=fopen(filename, "wb");
|
|
119 if(m_pFile==0)
|
|
120 return -1;
|
|
121 m_iCount=0;
|
|
122 m_bDrop=FALSE;
|
|
123 fprintf(m_pFile, "##version 1\n");
|
|
124 fprintf(m_pFile, "quality %d\n", quality);
|
|
125 return 0;
|
|
126 }
|
|
127
|
|
128 int VbrControl_init_2pass_vbr_encoding(const char *filename, int bitrate, double framerate, int crispness, int quality)
|
|
129 {
|
|
130 int i;
|
|
131
|
|
132 int64_t text_bits=0;
|
|
133 int64_t total_bits=0;
|
|
134 int64_t complexity=0;
|
|
135 int64_t new_complexity=0;
|
|
136 int64_t motion_bits=0;
|
|
137 int64_t denominator=0;
|
|
138 float qual_multiplier=1.;
|
|
139 char head[20];
|
|
140
|
|
141 int64_t desired_bits;
|
|
142 int64_t non_text_bits;
|
|
143
|
|
144 float average_complexity;
|
|
145
|
|
146 m_pFile=fopen(filename, "rb");
|
|
147 if(m_pFile==0)
|
|
148 return -1;
|
|
149 m_bDrop=FALSE;
|
|
150 m_iCount=0;
|
|
151
|
|
152 fread(head, 10, 1, m_pFile);
|
|
153 if(!strncmp("##version ", head, 10))
|
|
154 {
|
|
155 int version;
|
|
156 int iOldQual;
|
|
157 float old_qual, new_qual;
|
|
158 fscanf(m_pFile, "%d\n", &version);
|
|
159 fscanf(m_pFile, "quality %d\n", &iOldQual);
|
|
160 switch(iOldQual)
|
|
161 {
|
|
162 case 5:
|
|
163 old_qual=1.f;
|
|
164 break;
|
|
165 case 4:
|
|
166 old_qual=1.1f;
|
|
167 break;
|
|
168 case 3:
|
|
169 old_qual=1.25f;
|
|
170 break;
|
|
171 case 2:
|
|
172 old_qual=1.4f;
|
|
173 break;
|
|
174 case 1:
|
|
175 old_qual=2.f;
|
|
176 break;
|
|
177 }
|
|
178 switch(quality)
|
|
179 {
|
|
180 case 5:
|
|
181 new_qual=1.f;
|
|
182 break;
|
|
183 case 4:
|
|
184 new_qual=1.1f;
|
|
185 break;
|
|
186 case 3:
|
|
187 new_qual=1.25f;
|
|
188 break;
|
|
189 case 2:
|
|
190 new_qual=1.4f;
|
|
191 break;
|
|
192 case 1:
|
|
193 new_qual=2.f;
|
|
194 break;
|
|
195 }
|
|
196 qual_multiplier=new_qual/old_qual;
|
|
197 }
|
|
198 else
|
|
199 fseek(m_pFile, 0, SEEK_SET);
|
|
200
|
|
201 lFrameStart=ftell(m_pFile); // save current position
|
|
202
|
|
203 /* removed C++ dependencies, now read file twice :-( */
|
|
204
|
|
205
|
|
206 while(!feof(m_pFile))
|
|
207 { fscanf(m_pFile, "Frame %d: intra %d, quant %d, texture %d, motion %d, total %d\n",
|
|
208 &iNumFrames, &(vFrame.is_key_frame), &(vFrame.quant), &(vFrame.text_bits), &(vFrame.motion_bits), &(vFrame.total_bits));
|
|
209
|
|
210 vFrame.total_bits+=vFrame.text_bits*(qual_multiplier-1);
|
|
211 vFrame.text_bits*=qual_multiplier;
|
|
212 text_bits +=(int64_t)vFrame.text_bits;
|
|
213 motion_bits += (int64_t)vFrame.motion_bits;
|
|
214 total_bits +=(int64_t)vFrame.total_bits;
|
|
215 complexity +=(int64_t)vFrame.text_bits*vFrame.quant;
|
|
216
|
|
217 // printf("Frames %d, texture %d, motion %d, quant %d total %d ",
|
|
218 // iNumFrames, vFrame.text_bits, vFrame.motion_bits, vFrame.quant, vFrame.total_bits);
|
|
219 // printf("texture %d, total %d, complexity %lld \n",vFrame.text_bits,vFrame.total_bits, complexity);
|
|
220 }
|
|
221 iNumFrames++;
|
|
222 average_complexity=complexity/iNumFrames;
|
|
223
|
2642
|
224 // if (verbose & TC_DEBUG) {
|
|
225 // fprintf(stderr, "(%s) frames %d, texture %lld, motion %lld, total %lld, complexity %lld\n", __FILE__, iNumFrames, text_bits, motion_bits, total_bits, complexity);
|
|
226 // }
|
2641
|
227
|
|
228 m_vFrames = (entry*)malloc(iNumFrames*sizeof(entry));
|
|
229 if (!m_vFrames)
|
|
230 { printf("out of memory");
|
2642
|
231 return -2; //TC_EXPORT_ERROR;
|
2641
|
232 }
|
|
233
|
|
234 fseek(m_pFile, lFrameStart, SEEK_SET); // start again
|
|
235
|
|
236 for (i=0;i<iNumFrames;i++)
|
|
237 { fscanf(m_pFile, "Frame %d: intra %d, quant %d, texture %d, motion %d, total %d\n",
|
|
238 &dummy, &(m_vFrames[i].is_key_frame), &(m_vFrames[i].quant),
|
|
239 &(m_vFrames[i].text_bits), &(m_vFrames[i].motion_bits),
|
|
240 &(m_vFrames[i].total_bits));
|
|
241
|
|
242 m_vFrames[i].total_bits += m_vFrames[i].text_bits*(qual_multiplier-1);
|
|
243 m_vFrames[i].text_bits *= qual_multiplier;
|
|
244 }
|
|
245
|
|
246 if (m_pFile)
|
|
247 { fclose(m_pFile);
|
|
248 m_pFile=NULL;
|
|
249 }
|
|
250
|
|
251 desired_bits=(int64_t)bitrate*(int64_t)iNumFrames/framerate;
|
|
252 non_text_bits=total_bits-text_bits;
|
|
253
|
|
254 if(desired_bits<=non_text_bits)
|
|
255 {
|
|
256 char s[200];
|
|
257 printf("Specified bitrate is too low for this clip.\n"
|
|
258 "Minimum possible bitrate for the clip is %.0f kbps. Overriding\n"
|
|
259 "user-specified value.\n",
|
|
260 (float)(non_text_bits*framerate/(int64_t)iNumFrames));
|
|
261
|
|
262 desired_bits=non_text_bits*3/2;
|
|
263 /*
|
|
264 m_fQuant=max_quantizer;
|
|
265 for(int i=0; i<iNumFrames; i++)
|
|
266 {
|
|
267 m_vFrames[i].drop=0;
|
|
268 m_vFrames[i].mult=1;
|
|
269 }
|
|
270 VbrControl_set_quant(m_fQuant);
|
|
271 return 0;
|
|
272 */
|
|
273 }
|
|
274
|
|
275 desired_bits -= non_text_bits;
|
|
276 /**
|
|
277 BRIEF EXPLANATION OF WHAT'S GOING ON HERE.
|
|
278 We assume that
|
|
279 text_bits=complexity / quantizer
|
|
280 total_bits-text_bits = const(complexity)
|
|
281 where 'complexity' is a characteristic of the frame
|
|
282 and does not depend much on quantizer dynamics.
|
|
283 Using this equation, we calculate 'average' quantizer
|
|
284 to be used for encoding ( 1st order effect ).
|
|
285 Having constant quantizer for the entire stream is not
|
|
286 very convenient - reconstruction errors are
|
|
287 more noticeable in low-motion scenes. To compensate
|
|
288 this effect, we multiply quantizer for each frame by
|
|
289 (complexity/average_complexity)^k,
|
|
290 ( k - parameter of adjustment ). k=0 means 'no compensation'
|
|
291 and k=1 is 'constant bitrate mode'. We choose something in
|
|
292 between, like 0.5 ( 2nd order effect ).
|
|
293 **/
|
|
294
|
|
295 average_complexity=complexity/iNumFrames;
|
|
296
|
|
297 for(i=0; i<iNumFrames; i++)
|
|
298 {
|
|
299 float mult;
|
|
300 if(m_vFrames[i].is_key_frame)
|
|
301 {
|
|
302 if((i+1<iNumFrames) && (m_vFrames[i+1].is_key_frame))
|
|
303 mult=1.25;
|
|
304 else
|
|
305 mult=.75;
|
|
306 }
|
|
307 else
|
|
308 {
|
|
309 mult=m_vFrames[i].text_bits*m_vFrames[i].quant;
|
|
310 mult=(float)sqrt(mult/average_complexity);
|
|
311
|
|
312 // if(i && m_vFrames[i-1].is_key_frame)
|
|
313 // mult *= 0.75;
|
|
314 if(mult<0.5)
|
|
315 mult=0.5;
|
|
316 if(mult>1.5)
|
|
317 mult=1.5;
|
|
318 }
|
|
319
|
|
320 m_vFrames[i].mult=mult;
|
|
321 m_vFrames[i].drop=FALSE;
|
|
322 new_complexity+=m_vFrames[i].text_bits*m_vFrames[i].quant;
|
|
323
|
|
324 denominator+=desired_bits*m_vFrames[i].mult/iNumFrames;
|
|
325 }
|
|
326
|
|
327 m_fQuant=((double)new_complexity)/(double)denominator;
|
|
328
|
|
329 if(m_fQuant<min_quantizer) m_fQuant=min_quantizer;
|
|
330 if(m_fQuant>max_quantizer) m_fQuant=max_quantizer;
|
|
331 m_pFile=fopen("analyse.log", "wb");
|
|
332 if(m_pFile)
|
|
333 {
|
|
334 fprintf(m_pFile, "Total frames: %d Avg quantizer: %f\n",
|
|
335 iNumFrames, m_fQuant);
|
|
336 fprintf(m_pFile, "Expecting %12lld bits\n", desired_bits+non_text_bits);
|
|
337 fflush(m_pFile);
|
|
338 }
|
|
339 VbrControl_set_quant(m_fQuant*m_vFrames[0].mult);
|
|
340 m_lEncodedBits=m_lExpectedBits=0;
|
|
341 return 0;
|
|
342 }
|
|
343
|
|
344 int VbrControl_get_intra()
|
|
345 {
|
|
346 return m_vFrames[m_iCount].is_key_frame;
|
|
347 }
|
|
348
|
|
349 short VbrControl_get_drop()
|
|
350 {
|
|
351 return m_bDrop;
|
|
352 }
|
|
353
|
|
354 int VbrControl_get_quant()
|
|
355 {
|
|
356 return m_iQuant;
|
|
357 }
|
|
358
|
|
359 void VbrControl_set_quant(float quant)
|
|
360 {
|
|
361 m_iQuant=quant;
|
|
362 if((rand() % 10)<((quant-m_iQuant) * 10))
|
|
363 m_iQuant++;
|
|
364 if(m_iQuant<min_quantizer) m_iQuant=min_quantizer;
|
|
365 if(m_iQuant>max_quantizer) m_iQuant=max_quantizer;
|
|
366 }
|
|
367
|
|
368 void VbrControl_update_1pass_vbr()
|
|
369 {
|
|
370 VbrControl_set_quant(m_fQuant);
|
|
371 m_iCount++;
|
|
372 }
|
|
373
|
|
374 void VbrControl_update_2pass_vbr_analysis(int is_key_frame, int motion_bits, int texture_bits, int total_bits, int quant)
|
|
375 {
|
|
376 if(!m_pFile)
|
|
377 return;
|
|
378 fprintf(m_pFile, "Frame %d: intra %d, quant %d, texture %d, motion %d, total %d\n",
|
|
379 m_iCount, is_key_frame, quant, texture_bits, motion_bits, total_bits);
|
|
380 m_iCount++;
|
|
381 }
|
|
382
|
|
383 void VbrControl_update_2pass_vbr_encoding(int motion_bits, int texture_bits, int total_bits)
|
|
384 {
|
|
385 double q;
|
|
386 double dq;
|
|
387
|
|
388 if(m_iCount>=iNumFrames)
|
|
389 return;
|
|
390
|
|
391 m_lExpectedBits+=(m_vFrames[m_iCount].total_bits-m_vFrames[m_iCount].text_bits)
|
|
392 + m_vFrames[m_iCount].text_bits*m_vFrames[m_iCount].quant/m_fQuant;
|
|
393 m_lEncodedBits+=(int64_t)total_bits;
|
|
394
|
|
395 if(m_pFile)
|
|
396 fprintf(m_pFile, "Frame %d: PRESENT, complexity %d, quant multiplier %f, texture %d, total %d ",
|
|
397 m_iCount, m_vFrames[m_iCount].text_bits*m_vFrames[m_iCount].quant,
|
|
398 m_vFrames[m_iCount].mult, texture_bits, total_bits);
|
|
399
|
|
400 m_iCount++;
|
|
401
|
|
402 q = m_fQuant * m_vFrames[m_iCount].mult;
|
|
403 if(q<m_fQuant+min_quant_delta) q=m_fQuant+min_quant_delta;
|
|
404 if(q>m_fQuant+max_quant_delta) q=m_fQuant+max_quant_delta;
|
|
405
|
|
406 dq = (double)m_lEncodedBits/(double)m_lExpectedBits;
|
|
407 dq*=dq;
|
|
408 if(dq<min_rc_quant_delta)
|
|
409 dq=min_rc_quant_delta;
|
|
410 if(dq>max_rc_quant_delta)
|
|
411 dq=max_rc_quant_delta;
|
|
412 if(m_iCount<20) // no framerate corrections in first frames
|
|
413 dq=1;
|
|
414 if(m_pFile)
|
|
415 fprintf(m_pFile, "Progress: expected %12lld, achieved %12lld, dq %f",
|
|
416 m_lExpectedBits, m_lEncodedBits, dq);
|
|
417 q *= dq;
|
|
418 VbrControl_set_quant(q);
|
|
419 if(m_pFile)
|
|
420 fprintf(m_pFile, ", new quant %d\n", m_iQuant);
|
|
421 }
|
|
422
|
|
423 void VbrControl_close()
|
|
424 {
|
|
425 if(m_pFile)
|
|
426 {
|
|
427 fclose(m_pFile);
|
|
428 m_pFile=NULL;
|
|
429 }
|
|
430 free(m_vFrames);
|
|
431 }
|