annotate divx4_vbr.c @ 2834:b7d3b450f8fd

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