Line | Count | Source (jump to first uncovered line) |
1 | | /******************************************************************** |
2 | | * * |
3 | | * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. * |
4 | | * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * |
5 | | * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * |
6 | | * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
7 | | * * |
8 | | * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 * |
9 | | * by the Xiph.Org Foundation http://www.xiph.org/ * |
10 | | * * |
11 | | ******************************************************************** |
12 | | |
13 | | function: |
14 | | last mod: $Id$ |
15 | | |
16 | | ********************************************************************/ |
17 | | #include <stdlib.h> |
18 | | #include <string.h> |
19 | | #include "encint.h" |
20 | | #include "dequant.h" |
21 | | |
22 | | |
23 | | |
24 | | /*The default quantization parameters used by VP3.1.*/ |
25 | | static const int OC_VP31_RANGE_SIZES[1]={63}; |
26 | | static const th_quant_base OC_VP31_BASES_INTRA_Y[2]={ |
27 | | { |
28 | | 16, 11, 10, 16, 24, 40, 51, 61, |
29 | | 12, 12, 14, 19, 26, 58, 60, 55, |
30 | | 14, 13, 16, 24, 40, 57, 69, 56, |
31 | | 14, 17, 22, 29, 51, 87, 80, 62, |
32 | | 18, 22, 37, 58, 68,109,103, 77, |
33 | | 24, 35, 55, 64, 81,104,113, 92, |
34 | | 49, 64, 78, 87,103,121,120,101, |
35 | | 72, 92, 95, 98,112,100,103, 99 |
36 | | }, |
37 | | { |
38 | | 16, 11, 10, 16, 24, 40, 51, 61, |
39 | | 12, 12, 14, 19, 26, 58, 60, 55, |
40 | | 14, 13, 16, 24, 40, 57, 69, 56, |
41 | | 14, 17, 22, 29, 51, 87, 80, 62, |
42 | | 18, 22, 37, 58, 68,109,103, 77, |
43 | | 24, 35, 55, 64, 81,104,113, 92, |
44 | | 49, 64, 78, 87,103,121,120,101, |
45 | | 72, 92, 95, 98,112,100,103, 99 |
46 | | } |
47 | | }; |
48 | | static const th_quant_base OC_VP31_BASES_INTRA_C[2]={ |
49 | | { |
50 | | 17, 18, 24, 47, 99, 99, 99, 99, |
51 | | 18, 21, 26, 66, 99, 99, 99, 99, |
52 | | 24, 26, 56, 99, 99, 99, 99, 99, |
53 | | 47, 66, 99, 99, 99, 99, 99, 99, |
54 | | 99, 99, 99, 99, 99, 99, 99, 99, |
55 | | 99, 99, 99, 99, 99, 99, 99, 99, |
56 | | 99, 99, 99, 99, 99, 99, 99, 99, |
57 | | 99, 99, 99, 99, 99, 99, 99, 99 |
58 | | }, |
59 | | { |
60 | | 17, 18, 24, 47, 99, 99, 99, 99, |
61 | | 18, 21, 26, 66, 99, 99, 99, 99, |
62 | | 24, 26, 56, 99, 99, 99, 99, 99, |
63 | | 47, 66, 99, 99, 99, 99, 99, 99, |
64 | | 99, 99, 99, 99, 99, 99, 99, 99, |
65 | | 99, 99, 99, 99, 99, 99, 99, 99, |
66 | | 99, 99, 99, 99, 99, 99, 99, 99, |
67 | | 99, 99, 99, 99, 99, 99, 99, 99 |
68 | | } |
69 | | }; |
70 | | static const th_quant_base OC_VP31_BASES_INTER[2]={ |
71 | | { |
72 | | 16, 16, 16, 20, 24, 28, 32, 40, |
73 | | 16, 16, 20, 24, 28, 32, 40, 48, |
74 | | 16, 20, 24, 28, 32, 40, 48, 64, |
75 | | 20, 24, 28, 32, 40, 48, 64, 64, |
76 | | 24, 28, 32, 40, 48, 64, 64, 64, |
77 | | 28, 32, 40, 48, 64, 64, 64, 96, |
78 | | 32, 40, 48, 64, 64, 64, 96,128, |
79 | | 40, 48, 64, 64, 64, 96,128,128 |
80 | | }, |
81 | | { |
82 | | 16, 16, 16, 20, 24, 28, 32, 40, |
83 | | 16, 16, 20, 24, 28, 32, 40, 48, |
84 | | 16, 20, 24, 28, 32, 40, 48, 64, |
85 | | 20, 24, 28, 32, 40, 48, 64, 64, |
86 | | 24, 28, 32, 40, 48, 64, 64, 64, |
87 | | 28, 32, 40, 48, 64, 64, 64, 96, |
88 | | 32, 40, 48, 64, 64, 64, 96,128, |
89 | | 40, 48, 64, 64, 64, 96,128,128 |
90 | | } |
91 | | }; |
92 | | |
93 | | const th_quant_info TH_VP31_QUANT_INFO={ |
94 | | { |
95 | | 220,200,190,180,170,170,160,160, |
96 | | 150,150,140,140,130,130,120,120, |
97 | | 110,110,100,100, 90, 90, 90, 80, |
98 | | 80, 80, 70, 70, 70, 60, 60, 60, |
99 | | 60, 50, 50, 50, 50, 40, 40, 40, |
100 | | 40, 40, 30, 30, 30, 30, 30, 30, |
101 | | 30, 20, 20, 20, 20, 20, 20, 20, |
102 | | 20, 10, 10, 10, 10, 10, 10, 10 |
103 | | }, |
104 | | { |
105 | | 500,450,400,370,340,310,285,265, |
106 | | 245,225,210,195,185,180,170,160, |
107 | | 150,145,135,130,125,115,110,107, |
108 | | 100, 96, 93, 89, 85, 82, 75, 74, |
109 | | 70, 68, 64, 60, 57, 56, 52, 50, |
110 | | 49, 45, 44, 43, 40, 38, 37, 35, |
111 | | 33, 32, 30, 29, 28, 25, 24, 22, |
112 | | 21, 19, 18, 17, 15, 13, 12, 10 |
113 | | }, |
114 | | { |
115 | | 30,25,20,20,15,15,14,14, |
116 | | 13,13,12,12,11,11,10,10, |
117 | | 9, 9, 8, 8, 7, 7, 7, 7, |
118 | | 6, 6, 6, 6, 5, 5, 5, 5, |
119 | | 4, 4, 4, 4, 3, 3, 3, 3, |
120 | | 2, 2, 2, 2, 2, 2, 2, 2, |
121 | | 0, 0, 0, 0, 0, 0, 0, 0, |
122 | | 0, 0, 0, 0, 0, 0, 0, 0 |
123 | | }, |
124 | | { |
125 | | { |
126 | | {1,OC_VP31_RANGE_SIZES,OC_VP31_BASES_INTRA_Y}, |
127 | | {1,OC_VP31_RANGE_SIZES,OC_VP31_BASES_INTRA_C}, |
128 | | {1,OC_VP31_RANGE_SIZES,OC_VP31_BASES_INTRA_C} |
129 | | }, |
130 | | { |
131 | | {1,OC_VP31_RANGE_SIZES,OC_VP31_BASES_INTER}, |
132 | | {1,OC_VP31_RANGE_SIZES,OC_VP31_BASES_INTER}, |
133 | | {1,OC_VP31_RANGE_SIZES,OC_VP31_BASES_INTER} |
134 | | } |
135 | | } |
136 | | }; |
137 | | |
138 | | /*The current default quantization parameters.*/ |
139 | | static const int OC_DEF_QRANGE_SIZES[3]={32,16,15}; |
140 | | static const th_quant_base OC_DEF_BASES_INTRA_Y[4]={ |
141 | | { |
142 | | 15, 15, 15, 15, 15, 15, 15, 15, |
143 | | 15, 15, 15, 15, 15, 15, 15, 15, |
144 | | 15, 15, 15, 15, 15, 15, 15, 15, |
145 | | 15, 15, 15, 15, 15, 15, 15, 15, |
146 | | 15, 15, 15, 15, 15, 15, 15, 15, |
147 | | 15, 15, 15, 15, 15, 15, 15, 15, |
148 | | 15, 15, 15, 15, 15, 15, 15, 15, |
149 | | 15, 15, 15, 15, 15, 15, 15, 15, |
150 | | }, |
151 | | { |
152 | | 15, 12, 12, 15, 18, 20, 20, 21, |
153 | | 13, 13, 14, 17, 18, 21, 21, 20, |
154 | | 14, 14, 15, 18, 20, 21, 21, 21, |
155 | | 14, 16, 17, 19, 20, 21, 21, 21, |
156 | | 16, 17, 20, 21, 21, 21, 21, 21, |
157 | | 18, 19, 20, 21, 21, 21, 21, 21, |
158 | | 20, 21, 21, 21, 21, 21, 21, 21, |
159 | | 21, 21, 21, 21, 21, 21, 21, 21 |
160 | | }, |
161 | | { |
162 | | 16, 12, 11, 16, 20, 25, 27, 28, |
163 | | 13, 13, 14, 18, 21, 28, 28, 27, |
164 | | 14, 13, 16, 20, 25, 28, 28, 28, |
165 | | 14, 16, 19, 22, 27, 29, 29, 28, |
166 | | 17, 19, 25, 28, 28, 30, 30, 29, |
167 | | 20, 24, 27, 28, 29, 30, 30, 29, |
168 | | 27, 28, 29, 29, 30, 30, 30, 30, |
169 | | 29, 29, 29, 29, 30, 30, 30, 29 |
170 | | }, |
171 | | { |
172 | | 16, 11, 10, 16, 24, 40, 51, 61, |
173 | | 12, 12, 14, 19, 26, 58, 60, 55, |
174 | | 14, 13, 16, 24, 40, 57, 69, 56, |
175 | | 14, 17, 22, 29, 51, 87, 80, 62, |
176 | | 18, 22, 37, 58, 68,109,103, 77, |
177 | | 24, 35, 55, 64, 81,104,113, 92, |
178 | | 49, 64, 78, 87,103,121,120,101, |
179 | | 72, 92, 95, 98,112,100,103, 99 |
180 | | } |
181 | | }; |
182 | | static const th_quant_base OC_DEF_BASES_INTRA_C[4]={ |
183 | | { |
184 | | 19, 19, 19, 19, 19, 19, 19, 19, |
185 | | 19, 19, 19, 19, 19, 19, 19, 19, |
186 | | 19, 19, 19, 19, 19, 19, 19, 19, |
187 | | 19, 19, 19, 19, 19, 19, 19, 19, |
188 | | 19, 19, 19, 19, 19, 19, 19, 19, |
189 | | 19, 19, 19, 19, 19, 19, 19, 19, |
190 | | 19, 19, 19, 19, 19, 19, 19, 19, |
191 | | 19, 19, 19, 19, 19, 19, 19, 19 |
192 | | }, |
193 | | { |
194 | | 18, 18, 21, 25, 26, 26, 26, 26, |
195 | | 18, 20, 22, 26, 26, 26, 26, 26, |
196 | | 21, 22, 25, 26, 26, 26, 26, 26, |
197 | | 25, 26, 26, 26, 26, 26, 26, 26, |
198 | | 26, 26, 26, 26, 26, 26, 26, 26, |
199 | | 26, 26, 26, 26, 26, 26, 26, 26, |
200 | | 26, 26, 26, 26, 26, 26, 26, 26, |
201 | | 26, 26, 26, 26, 26, 26, 26, 26 |
202 | | }, |
203 | | { |
204 | | 17, 18, 22, 31, 36, 36, 36, 36, |
205 | | 18, 20, 24, 34, 36, 36, 36, 36, |
206 | | 22, 24, 33, 36, 36, 36, 36, 36, |
207 | | 31, 34, 36, 36, 36, 36, 36, 36, |
208 | | 36, 36, 36, 36, 36, 36, 36, 36, |
209 | | 36, 36, 36, 36, 36, 36, 36, 36, |
210 | | 36, 36, 36, 36, 36, 36, 36, 36, |
211 | | 36, 36, 36, 36, 36, 36, 36, 36 |
212 | | }, |
213 | | { |
214 | | 17, 18, 24, 47, 99, 99, 99, 99, |
215 | | 18, 21, 26, 66, 99, 99, 99, 99, |
216 | | 24, 26, 56, 99, 99, 99, 99, 99, |
217 | | 47, 66, 99, 99, 99, 99, 99, 99, |
218 | | 99, 99, 99, 99, 99, 99, 99, 99, |
219 | | 99, 99, 99, 99, 99, 99, 99, 99, |
220 | | 99, 99, 99, 99, 99, 99, 99, 99, |
221 | | 99, 99, 99, 99, 99, 99, 99, 99 |
222 | | } |
223 | | }; |
224 | | static const th_quant_base OC_DEF_BASES_INTER[4]={ |
225 | | { |
226 | | 21, 21, 21, 21, 21, 21, 21, 21, |
227 | | 21, 21, 21, 21, 21, 21, 21, 21, |
228 | | 21, 21, 21, 21, 21, 21, 21, 21, |
229 | | 21, 21, 21, 21, 21, 21, 21, 21, |
230 | | 21, 21, 21, 21, 21, 21, 21, 21, |
231 | | 21, 21, 21, 21, 21, 21, 21, 21, |
232 | | 21, 21, 21, 21, 21, 21, 21, 21, |
233 | | 21, 21, 21, 21, 21, 21, 21, 21 |
234 | | }, |
235 | | { |
236 | | 18, 18, 18, 21, 23, 24, 25, 27, |
237 | | 18, 18, 21, 23, 24, 25, 27, 28, |
238 | | 18, 21, 23, 24, 25, 27, 28, 29, |
239 | | 21, 23, 24, 25, 27, 28, 29, 29, |
240 | | 23, 24, 25, 27, 28, 29, 29, 29, |
241 | | 24, 25, 27, 28, 29, 29, 29, 30, |
242 | | 25, 27, 28, 29, 29, 29, 30, 30, |
243 | | 27, 28, 29, 29, 29, 30, 30, 30 |
244 | | }, |
245 | | { |
246 | | 17, 17, 17, 20, 23, 26, 28, 32, |
247 | | 17, 17, 20, 23, 26, 28, 32, 34, |
248 | | 17, 20, 23, 26, 28, 32, 34, 37, |
249 | | 20, 23, 26, 28, 32, 34, 37, 37, |
250 | | 23, 26, 28, 32, 34, 37, 37, 37, |
251 | | 26, 28, 32, 34, 37, 37, 37, 41, |
252 | | 28, 32, 34, 37, 37, 37, 41, 42, |
253 | | 32, 34, 37, 37, 37, 41, 42, 42 |
254 | | }, |
255 | | { |
256 | | 16, 16, 16, 20, 24, 28, 32, 40, |
257 | | 16, 16, 20, 24, 28, 32, 40, 48, |
258 | | 16, 20, 24, 28, 32, 40, 48, 64, |
259 | | 20, 24, 28, 32, 40, 48, 64, 64, |
260 | | 24, 28, 32, 40, 48, 64, 64, 64, |
261 | | 28, 32, 40, 48, 64, 64, 64, 96, |
262 | | 32, 40, 48, 64, 64, 64, 96,128, |
263 | | 40, 48, 64, 64, 64, 96,128,128 |
264 | | } |
265 | | }; |
266 | | |
267 | | const th_quant_info TH_DEF_QUANT_INFO={ |
268 | | { |
269 | | 365,348,333,316,300,287,277,265, |
270 | | 252,240,229,219,206,197,189,180, |
271 | | 171,168,160,153,146,139,132,127, |
272 | | 121,115,110,107,101, 97, 94, 89, |
273 | | 85, 83, 78, 73, 72, 67, 66, 62, |
274 | | 60, 59, 56, 53, 52, 48, 47, 43, |
275 | | 42, 40, 36, 35, 34, 33, 31, 30, |
276 | | 28, 25, 24, 22, 20, 17, 14, 10 |
277 | | }, |
278 | | { |
279 | | 365,348,333,316,300,287,277,265, |
280 | | 252,240,229,219,206,197,189,180, |
281 | | 171,168,160,153,146,139,132,127, |
282 | | 121,115,110,107,101, 97, 94, 89, |
283 | | 85, 83, 78, 73, 72, 67, 66, 62, |
284 | | 60, 59, 56, 53, 52, 48, 47, 43, |
285 | | 42, 40, 36, 35, 34, 33, 31, 30, |
286 | | 28, 25, 24, 22, 20, 17, 14, 10 |
287 | | }, |
288 | | { |
289 | | 15,12, 9, 8, 6, 6, 5, 5, |
290 | | 5, 5, 5, 5, 5, 5, 5, 5, |
291 | | 4, 4, 4, 4, 4, 4, 3, 3, |
292 | | 3, 3, 3, 3, 3, 3, 3, 3, |
293 | | 2, 2, 2, 2, 2, 2, 2, 2, |
294 | | 2, 2, 2, 2, 2, 2, 2, 0, |
295 | | 0, 0, 0, 0, 0, 0, 0, 0, |
296 | | 0, 0, 0, 0, 0, 0, 0, 0 |
297 | | }, |
298 | | { |
299 | | { |
300 | | {3,OC_DEF_QRANGE_SIZES,OC_DEF_BASES_INTRA_Y}, |
301 | | {3,OC_DEF_QRANGE_SIZES,OC_DEF_BASES_INTRA_C}, |
302 | | {3,OC_DEF_QRANGE_SIZES,OC_DEF_BASES_INTRA_C} |
303 | | }, |
304 | | { |
305 | | {3,OC_DEF_QRANGE_SIZES,OC_DEF_BASES_INTER}, |
306 | | {3,OC_DEF_QRANGE_SIZES,OC_DEF_BASES_INTER}, |
307 | | {3,OC_DEF_QRANGE_SIZES,OC_DEF_BASES_INTER} |
308 | | } |
309 | | } |
310 | | }; |
311 | | |
312 | | |
313 | | |
314 | | /*The Huffman codes used for macro block modes.*/ |
315 | | |
316 | | const unsigned char OC_MODE_BITS[2][OC_NMODES]={ |
317 | | /*Codebook 0: a maximally skewed prefix code.*/ |
318 | | {1,2,3,4,5,6,7,7}, |
319 | | /*Codebook 1: a fixed-length code.*/ |
320 | | {3,3,3,3,3,3,3,3} |
321 | | }; |
322 | | |
323 | | static const unsigned char OC_MODE_CODES[2][OC_NMODES]={ |
324 | | /*Codebook 0: a maximally skewed prefix code.*/ |
325 | | {0x00,0x02,0x06,0x0E,0x1E,0x3E,0x7E,0x7F}, |
326 | | /*Codebook 1: a fixed-length code.*/ |
327 | | {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07} |
328 | | }; |
329 | | |
330 | | |
331 | | /*The Huffman codes used for motion vectors.*/ |
332 | | |
333 | | const unsigned char OC_MV_BITS[2][64]={ |
334 | | /*Codebook 0: VLC code.*/ |
335 | | { |
336 | | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
337 | | 8,7,7,7,7,7,7,7,7,6,6,6,6,4,4,3, |
338 | | 3, |
339 | | 3,4,4,6,6,6,6,7,7,7,7,7,7,7,7,8, |
340 | | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 |
341 | | }, |
342 | | /*Codebook 1: (5 bit magnitude, 1 bit sign). |
343 | | This wastes a code word (0x01, negative zero), or a bit (0x00, positive |
344 | | zero, requires only 5 bits to uniquely decode), but is hopefully not used |
345 | | very often.*/ |
346 | | { |
347 | | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
348 | | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
349 | | 6, |
350 | | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
351 | | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6 |
352 | | } |
353 | | }; |
354 | | |
355 | | static const unsigned char OC_MV_CODES[2][64]={ |
356 | | /*Codebook 0: VLC code.*/ |
357 | | { |
358 | | 0xFF,0xFD,0xFB,0xF9,0xF7,0xF5,0xF3, |
359 | | 0xF1,0xEF,0xED,0xEB,0xE9,0xE7,0xE5,0xE3, |
360 | | 0xE1,0x6F,0x6D,0x6B,0x69,0x67,0x65,0x63, |
361 | | 0x61,0x2F,0x2D,0x2B,0x29,0x09,0x07,0x02, |
362 | | 0x00, |
363 | | 0x01,0x06,0x08,0x28,0x2A,0x2C,0x2E,0x60, |
364 | | 0x62,0x64,0x66,0x68,0x6A,0x6C,0x6E,0xE0, |
365 | | 0xE2,0xE4,0xE6,0xE8,0xEA,0xEC,0xEE,0xF0, |
366 | | 0xF2,0xF4,0xF6,0xF8,0xFA,0xFC,0xFE |
367 | | }, |
368 | | /*Codebook 1: (5 bit magnitude, 1 bit sign).*/ |
369 | | { |
370 | | 0x3F,0x3D,0x3B,0x39,0x37,0x35,0x33, |
371 | | 0x31,0x2F,0x2D,0x2B,0x29,0x27,0x25,0x23, |
372 | | 0x21,0x1F,0x1D,0x1B,0x19,0x17,0x15,0x13, |
373 | | 0x11,0x0F,0x0D,0x0B,0x09,0x07,0x05,0x03, |
374 | | 0x00, |
375 | | 0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,0x10, |
376 | | 0x12,0x14,0x16,0x18,0x1A,0x1C,0x1E,0x20, |
377 | | 0x22,0x24,0x26,0x28,0x2A,0x2C,0x2E,0x30, |
378 | | 0x32,0x34,0x36,0x38,0x3A,0x3C,0x3E |
379 | | } |
380 | | }; |
381 | | |
382 | | |
383 | | |
384 | | /*Super block run coding scheme: |
385 | | Codeword Run Length |
386 | | 0 1 |
387 | | 10x 2-3 |
388 | | 110x 4-5 |
389 | | 1110xx 6-9 |
390 | | 11110xxx 10-17 |
391 | | 111110xxxx 18-33 |
392 | | 111111xxxxxxxxxxxx 34-4129*/ |
393 | | const ogg_uint16_t OC_SB_RUN_VAL_MIN[8]={1,2,4,6,10,18,34,4130}; |
394 | | static const unsigned OC_SB_RUN_CODE_PREFIX[7]={ |
395 | | 0,4,0xC,0x38,0xF0,0x3E0,0x3F000 |
396 | | }; |
397 | | const unsigned char OC_SB_RUN_CODE_NBITS[7]={1,3,4,6,8,10,18}; |
398 | | |
399 | | |
400 | | /*Writes the bit pattern for the run length of a super block run to the given |
401 | | oggpack_buffer. |
402 | | _opb: The buffer to write to. |
403 | | _run_count: The length of the run, which must be positive. |
404 | | _flag: The current flag. |
405 | | _done: Whether or not more flags are to be encoded.*/ |
406 | | static void oc_sb_run_pack(oggpack_buffer *_opb,ptrdiff_t _run_count, |
407 | 271k | int _flag,int _done){ |
408 | 271k | int i; |
409 | 271k | if(_run_count>=4129){ |
410 | 1.21k | do{ |
411 | 1.21k | oggpackB_write(_opb,0x3FFFF,18); |
412 | 1.21k | _run_count-=4129; |
413 | 1.21k | if(_run_count>0)oggpackB_write(_opb,_flag,1); |
414 | 18 | else if(!_done)oggpackB_write(_opb,!_flag,1); |
415 | 1.21k | } |
416 | 1.21k | while(_run_count>=4129); |
417 | 579 | if(_run_count<=0)return; |
418 | 579 | } |
419 | 679k | for(i=0;_run_count>=OC_SB_RUN_VAL_MIN[i+1];i++); |
420 | 271k | oggpackB_write(_opb,OC_SB_RUN_CODE_PREFIX[i]+_run_count-OC_SB_RUN_VAL_MIN[i], |
421 | 271k | OC_SB_RUN_CODE_NBITS[i]); |
422 | 271k | } |
423 | | |
424 | | |
425 | | |
426 | | /*Block run coding scheme: |
427 | | Codeword Run Length |
428 | | 0x 1-2 |
429 | | 10x 3-4 |
430 | | 110x 5-6 |
431 | | 1110xx 7-10 |
432 | | 11110xx 11-14 |
433 | | 11111xxxx 15-30*/ |
434 | | const unsigned char OC_BLOCK_RUN_CODE_NBITS[30]={ |
435 | | 2,2,3,3,4,4,6,6,6,6,7,7,7,7,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9 |
436 | | }; |
437 | | static const ogg_uint16_t OC_BLOCK_RUN_CODE_PATTERN[30]={ |
438 | | 0x000,0x001,0x004,0x005,0x00C,0x00D,0x038, |
439 | | 0x039,0x03A,0x03B,0x078,0x079,0x07A,0x07B,0x1F0, |
440 | | 0x1F1,0x1F2,0x1F3,0x1F4,0x1F5,0x1F6,0x1F7,0x1F8, |
441 | | 0x1F9,0x1FA,0x1FB,0x1FC,0x1FD,0x1FE,0x1FF |
442 | | }; |
443 | | |
444 | | |
445 | | /*Writes the bit pattern for the run length of a block run to the given |
446 | | oggpack_buffer. |
447 | | _opb: The buffer to write to. |
448 | | _run_count: The length of the run. |
449 | | This must be positive, and no more than 30.*/ |
450 | 217k | static void oc_block_run_pack(oggpack_buffer *_opb,int _run_count){ |
451 | 217k | oggpackB_write(_opb,OC_BLOCK_RUN_CODE_PATTERN[_run_count-1], |
452 | 217k | OC_BLOCK_RUN_CODE_NBITS[_run_count-1]); |
453 | 217k | } |
454 | | |
455 | | |
456 | | |
457 | 41.7k | static void oc_enc_frame_header_pack(oc_enc_ctx *_enc){ |
458 | | /*Mark this as a data packet.*/ |
459 | 41.7k | oggpackB_write(&_enc->opb,0,1); |
460 | | /*Output the frame type (key frame or delta frame).*/ |
461 | 41.7k | oggpackB_write(&_enc->opb,_enc->state.frame_type,1); |
462 | | /*Write out the current qi list.*/ |
463 | 41.7k | oggpackB_write(&_enc->opb,_enc->state.qis[0],6); |
464 | 41.7k | if(_enc->state.nqis>1){ |
465 | 17.7k | oggpackB_write(&_enc->opb,1,1); |
466 | 17.7k | oggpackB_write(&_enc->opb,_enc->state.qis[1],6); |
467 | 17.7k | if(_enc->state.nqis>2){ |
468 | 4.73k | oggpackB_write(&_enc->opb,1,1); |
469 | 4.73k | oggpackB_write(&_enc->opb,_enc->state.qis[2],6); |
470 | 4.73k | } |
471 | 13.0k | else oggpackB_write(&_enc->opb,0,1); |
472 | 17.7k | } |
473 | 23.9k | else oggpackB_write(&_enc->opb,0,1); |
474 | 41.7k | if(_enc->state.frame_type==OC_INTRA_FRAME){ |
475 | | /*Key frames have 3 unused configuration bits, holdovers from the VP3 days. |
476 | | Most of the other unused bits in the VP3 headers were eliminated. |
477 | | Monty kept these to leave us some wiggle room for future expansion, |
478 | | though a single bit in all frames would have been far more useful.*/ |
479 | 22.6k | oggpackB_write(&_enc->opb,0,3); |
480 | 22.6k | } |
481 | 41.7k | } |
482 | | |
483 | | /*Writes the bit flags for whether or not each super block is partially coded |
484 | | or not. |
485 | | These flags are run-length encoded, with the flag value alternating between |
486 | | each run. |
487 | | Return: The number partially coded SBs.*/ |
488 | 19.0k | static unsigned oc_enc_partial_sb_flags_pack(oc_enc_ctx *_enc){ |
489 | 19.0k | const oc_sb_flags *sb_flags; |
490 | 19.0k | unsigned nsbs; |
491 | 19.0k | unsigned sbi; |
492 | 19.0k | unsigned npartial; |
493 | 19.0k | int flag; |
494 | 19.0k | sb_flags=_enc->state.sb_flags; |
495 | 19.0k | nsbs=_enc->state.nsbs; |
496 | 19.0k | flag=sb_flags[0].coded_partially; |
497 | 19.0k | oggpackB_write(&_enc->opb,flag,1); |
498 | 19.0k | sbi=npartial=0; |
499 | 46.5k | do{ |
500 | 46.5k | unsigned run_count; |
501 | 210k | for(run_count=0;sbi<nsbs;sbi++){ |
502 | 190k | if(sb_flags[sbi].coded_partially!=flag)break; |
503 | 163k | run_count++; |
504 | 163k | npartial+=flag; |
505 | 163k | } |
506 | 46.5k | oc_sb_run_pack(&_enc->opb,run_count,flag,sbi>=nsbs); |
507 | 46.5k | flag=!flag; |
508 | 46.5k | } |
509 | 46.5k | while(sbi<nsbs); |
510 | 19.0k | return npartial; |
511 | 19.0k | } |
512 | | |
513 | | /*Writes the coded/not coded flags for each super block that is not partially |
514 | | coded. |
515 | | These flags are run-length encoded, with the flag value altenating between |
516 | | each run.*/ |
517 | 16.5k | static void oc_enc_coded_sb_flags_pack(oc_enc_ctx *_enc){ |
518 | 16.5k | const oc_sb_flags *sb_flags; |
519 | 16.5k | unsigned nsbs; |
520 | 16.5k | unsigned sbi; |
521 | 16.5k | int flag; |
522 | 16.5k | sb_flags=_enc->state.sb_flags; |
523 | 16.5k | nsbs=_enc->state.nsbs; |
524 | | /*Skip partially coded super blocks; their flags have already been coded.*/ |
525 | 30.7k | for(sbi=0;sb_flags[sbi].coded_partially;sbi++); |
526 | 16.5k | flag=sb_flags[sbi].coded_fully; |
527 | 16.5k | oggpackB_write(&_enc->opb,flag,1); |
528 | 19.9k | do{ |
529 | 19.9k | unsigned run_count; |
530 | 159k | for(run_count=0;sbi<nsbs;sbi++){ |
531 | 142k | if(sb_flags[sbi].coded_partially)continue; |
532 | 98.7k | if(sb_flags[sbi].coded_fully!=flag)break; |
533 | 95.3k | run_count++; |
534 | 95.3k | } |
535 | 19.9k | oc_sb_run_pack(&_enc->opb,run_count,flag,sbi>=nsbs); |
536 | 19.9k | flag=!flag; |
537 | 19.9k | } |
538 | 19.9k | while(sbi<nsbs); |
539 | 16.5k | } |
540 | | |
541 | 19.0k | static void oc_enc_coded_flags_pack(oc_enc_ctx *_enc){ |
542 | 19.0k | const oc_sb_map *sb_maps; |
543 | 19.0k | const oc_sb_flags *sb_flags; |
544 | 19.0k | unsigned nsbs; |
545 | 19.0k | const oc_fragment *frags; |
546 | 19.0k | unsigned npartial; |
547 | 19.0k | int run_count; |
548 | 19.0k | int flag; |
549 | 19.0k | int pli; |
550 | 19.0k | unsigned sbi; |
551 | 19.0k | npartial=oc_enc_partial_sb_flags_pack(_enc); |
552 | 19.0k | if(npartial<_enc->state.nsbs)oc_enc_coded_sb_flags_pack(_enc); |
553 | 19.0k | sb_maps=(const oc_sb_map *)_enc->state.sb_maps; |
554 | 19.0k | sb_flags=_enc->state.sb_flags; |
555 | 19.0k | nsbs=_enc->state.nsbs; |
556 | 19.0k | frags=_enc->state.frags; |
557 | 60.8k | for(sbi=0;sbi<nsbs&&!sb_flags[sbi].coded_partially;sbi++); |
558 | | /*If there's at least one partial SB, store individual coded block flags.*/ |
559 | 19.0k | if(sbi<nsbs){ |
560 | 13.8k | flag=frags[sb_maps[sbi][0][0]].coded; |
561 | 13.8k | oggpackB_write(&_enc->opb,flag,1); |
562 | 13.8k | run_count=0; |
563 | 13.8k | nsbs=sbi=0; |
564 | 55.4k | for(pli=0;pli<3;pli++){ |
565 | 41.6k | nsbs+=_enc->state.fplanes[pli].nsbs; |
566 | 181k | for(;sbi<nsbs;sbi++){ |
567 | 139k | int quadi; |
568 | 139k | int bi; |
569 | 139k | ptrdiff_t fragi; |
570 | 139k | if(sb_flags[sbi].coded_partially){ |
571 | 341k | for(quadi=0;quadi<4;quadi++){ |
572 | 1.36M | for(bi=0;bi<4;bi++){ |
573 | 1.09M | fragi=sb_maps[sbi][quadi][bi]; |
574 | 1.09M | if(fragi>=0){ |
575 | 805k | if(frags[fragi].coded!=flag){ |
576 | 203k | oc_block_run_pack(&_enc->opb,run_count); |
577 | 203k | flag=!flag; |
578 | 203k | run_count=1; |
579 | 203k | } |
580 | 602k | else run_count++; |
581 | 805k | } |
582 | 1.09M | } |
583 | 272k | } |
584 | 68.2k | } |
585 | 139k | } |
586 | 41.6k | } |
587 | | /*Flush any trailing block coded run.*/ |
588 | 13.8k | if(run_count>0)oc_block_run_pack(&_enc->opb,run_count); |
589 | 13.8k | } |
590 | 19.0k | } |
591 | | |
592 | 19.0k | static void oc_enc_mb_modes_pack(oc_enc_ctx *_enc){ |
593 | 19.0k | const unsigned char *mode_codes; |
594 | 19.0k | const unsigned char *mode_bits; |
595 | 19.0k | const unsigned char *mode_ranks; |
596 | 19.0k | unsigned *coded_mbis; |
597 | 19.0k | size_t ncoded_mbis; |
598 | 19.0k | const signed char *mb_modes; |
599 | 19.0k | unsigned mbii; |
600 | 19.0k | int scheme; |
601 | 19.0k | int mb_mode; |
602 | 19.0k | scheme=_enc->chooser.scheme_list[0]; |
603 | | /*Encode the best scheme.*/ |
604 | 19.0k | oggpackB_write(&_enc->opb,scheme,3); |
605 | | /*If the chosen scheme is scheme 0, send the mode frequency ordering.*/ |
606 | 19.0k | if(scheme==0){ |
607 | 13.0k | for(mb_mode=0;mb_mode<OC_NMODES;mb_mode++){ |
608 | 11.5k | oggpackB_write(&_enc->opb,_enc->chooser.scheme0_ranks[mb_mode],3); |
609 | 11.5k | } |
610 | 1.44k | } |
611 | 19.0k | mode_ranks=_enc->chooser.mode_ranks[scheme]; |
612 | 19.0k | mode_bits=OC_MODE_BITS[scheme+1>>3]; |
613 | 19.0k | mode_codes=OC_MODE_CODES[scheme+1>>3]; |
614 | 19.0k | coded_mbis=_enc->coded_mbis; |
615 | 19.0k | ncoded_mbis=_enc->ncoded_mbis; |
616 | 19.0k | mb_modes=_enc->state.mb_modes; |
617 | 273k | for(mbii=0;mbii<ncoded_mbis;mbii++){ |
618 | 254k | int rank; |
619 | 254k | rank=mode_ranks[mb_modes[coded_mbis[mbii]]]; |
620 | 254k | oggpackB_write(&_enc->opb,mode_codes[rank],mode_bits[rank]); |
621 | 254k | } |
622 | 19.0k | } |
623 | | |
624 | 48.9k | static void oc_enc_mv_pack(oc_enc_ctx *_enc,int _mv_scheme,oc_mv _mv){ |
625 | 48.9k | int dx; |
626 | 48.9k | int dy; |
627 | 48.9k | dx=OC_MV_X(_mv); |
628 | 48.9k | dy=OC_MV_Y(_mv); |
629 | 48.9k | oggpackB_write(&_enc->opb, |
630 | 48.9k | OC_MV_CODES[_mv_scheme][dx+31],OC_MV_BITS[_mv_scheme][dx+31]); |
631 | 48.9k | oggpackB_write(&_enc->opb, |
632 | 48.9k | OC_MV_CODES[_mv_scheme][dy+31],OC_MV_BITS[_mv_scheme][dy+31]); |
633 | 48.9k | } |
634 | | |
635 | 19.0k | static void oc_enc_mvs_pack(oc_enc_ctx *_enc){ |
636 | 19.0k | const unsigned *coded_mbis; |
637 | 19.0k | size_t ncoded_mbis; |
638 | 19.0k | const oc_mb_map *mb_maps; |
639 | 19.0k | const signed char *mb_modes; |
640 | 19.0k | const oc_fragment *frags; |
641 | 19.0k | const oc_mv *frag_mvs; |
642 | 19.0k | unsigned mbii; |
643 | 19.0k | int mv_scheme; |
644 | | /*Choose the coding scheme.*/ |
645 | 19.0k | mv_scheme=_enc->mv_bits[1]<_enc->mv_bits[0]; |
646 | 19.0k | oggpackB_write(&_enc->opb,mv_scheme,1); |
647 | | /*Encode the motion vectors. |
648 | | Macro blocks are iterated in Hilbert scan order, but the MVs within the |
649 | | macro block are coded in raster order.*/ |
650 | 19.0k | coded_mbis=_enc->coded_mbis; |
651 | 19.0k | ncoded_mbis=_enc->ncoded_mbis; |
652 | 19.0k | mb_modes=_enc->state.mb_modes; |
653 | 19.0k | mb_maps=(const oc_mb_map *)_enc->state.mb_maps; |
654 | 19.0k | frags=_enc->state.frags; |
655 | 19.0k | frag_mvs=_enc->state.frag_mvs; |
656 | 273k | for(mbii=0;mbii<ncoded_mbis;mbii++){ |
657 | 254k | ptrdiff_t fragi; |
658 | 254k | unsigned mbi; |
659 | 254k | int bi; |
660 | 254k | mbi=coded_mbis[mbii]; |
661 | 254k | switch(mb_modes[mbi]){ |
662 | 14.6k | case OC_MODE_INTER_MV: |
663 | 29.8k | case OC_MODE_GOLDEN_MV:{ |
664 | 32.1k | for(bi=0;;bi++){ |
665 | 32.1k | fragi=mb_maps[mbi][0][bi]; |
666 | 32.1k | if(frags[fragi].coded){ |
667 | 29.8k | oc_enc_mv_pack(_enc,mv_scheme,frag_mvs[fragi]); |
668 | | /*Only code a single MV for this macro block.*/ |
669 | 29.8k | break; |
670 | 29.8k | } |
671 | 32.1k | } |
672 | 29.8k | }break; |
673 | 5.14k | case OC_MODE_INTER_MV_FOUR:{ |
674 | 25.7k | for(bi=0;bi<4;bi++){ |
675 | 20.5k | fragi=mb_maps[mbi][0][bi]; |
676 | 20.5k | if(frags[fragi].coded){ |
677 | 19.1k | oc_enc_mv_pack(_enc,mv_scheme,frag_mvs[fragi]); |
678 | | /*Keep coding all the MVs for this macro block.*/ |
679 | 19.1k | } |
680 | 20.5k | } |
681 | 5.14k | }break; |
682 | 254k | } |
683 | 254k | } |
684 | 19.0k | } |
685 | | |
686 | 41.7k | static void oc_enc_block_qis_pack(oc_enc_ctx *_enc){ |
687 | 41.7k | const oc_fragment *frags; |
688 | 41.7k | ptrdiff_t *coded_fragis; |
689 | 41.7k | ptrdiff_t ncoded_fragis; |
690 | 41.7k | ptrdiff_t fragii; |
691 | 41.7k | ptrdiff_t run_count; |
692 | 41.7k | ptrdiff_t nqi0; |
693 | 41.7k | int flag; |
694 | 41.7k | if(_enc->state.nqis<=1)return; |
695 | 17.7k | ncoded_fragis=_enc->state.ntotal_coded_fragis; |
696 | 17.7k | if(ncoded_fragis<=0)return; |
697 | 17.7k | coded_fragis=_enc->state.coded_fragis; |
698 | 17.7k | frags=_enc->state.frags; |
699 | 17.7k | flag=!!frags[coded_fragis[0]].qii; |
700 | 17.7k | oggpackB_write(&_enc->opb,flag,1); |
701 | 17.7k | nqi0=0; |
702 | 126k | for(fragii=0;fragii<ncoded_fragis;){ |
703 | 6.86M | for(run_count=0;fragii<ncoded_fragis;fragii++){ |
704 | 6.85M | if(!!frags[coded_fragis[fragii]].qii!=flag)break; |
705 | 6.75M | run_count++; |
706 | 6.75M | nqi0+=!flag; |
707 | 6.75M | } |
708 | 108k | oc_sb_run_pack(&_enc->opb,run_count,flag,fragii>=ncoded_fragis); |
709 | 108k | flag=!flag; |
710 | 108k | } |
711 | 17.7k | if(_enc->state.nqis<3||nqi0>=ncoded_fragis)return; |
712 | 334k | for(fragii=0;!frags[coded_fragis[fragii]].qii;fragii++); |
713 | 2.92k | flag=frags[coded_fragis[fragii]].qii-1; |
714 | 2.92k | oggpackB_write(&_enc->opb,flag,1); |
715 | 98.8k | while(fragii<ncoded_fragis){ |
716 | 4.55M | for(run_count=0;fragii<ncoded_fragis;fragii++){ |
717 | 4.54M | int qii; |
718 | 4.54M | qii=frags[coded_fragis[fragii]].qii; |
719 | 4.54M | if(!qii)continue; |
720 | 2.10M | if(qii-1!=flag)break; |
721 | 2.00M | run_count++; |
722 | 2.00M | } |
723 | 95.8k | oc_sb_run_pack(&_enc->opb,run_count,flag,fragii>=ncoded_fragis); |
724 | 95.8k | flag=!flag; |
725 | 95.8k | } |
726 | 2.92k | } |
727 | | |
728 | | /*Counts the tokens of each type used for the given range of coefficient |
729 | | indices in zig-zag order. |
730 | | _zzi_start: The first zig-zag index to include. |
731 | | _zzi_end: The first zig-zag index to not include. |
732 | | _token_counts_y: Returns the token counts for the Y' plane. |
733 | | _token_counts_c: Returns the token counts for the Cb and Cr planes.*/ |
734 | | static void oc_enc_count_tokens(oc_enc_ctx *_enc,int _zzi_start,int _zzi_end, |
735 | 208k | ptrdiff_t _token_counts_y[32],ptrdiff_t _token_counts_c[32]){ |
736 | 208k | const unsigned char *dct_tokens; |
737 | 208k | ptrdiff_t ndct_tokens; |
738 | 208k | int pli; |
739 | 208k | int zzi; |
740 | 208k | ptrdiff_t ti; |
741 | 208k | memset(_token_counts_y,0,32*sizeof(*_token_counts_y)); |
742 | 208k | memset(_token_counts_c,0,32*sizeof(*_token_counts_c)); |
743 | 2.88M | for(zzi=_zzi_start;zzi<_zzi_end;zzi++){ |
744 | 2.67M | dct_tokens=_enc->dct_tokens[0][zzi]; |
745 | 2.67M | ndct_tokens=_enc->ndct_tokens[0][zzi]; |
746 | 106M | for(ti=_enc->dct_token_offs[0][zzi];ti<ndct_tokens;ti++){ |
747 | 103M | _token_counts_y[dct_tokens[ti]]++; |
748 | 103M | } |
749 | 2.67M | } |
750 | 626k | for(pli=1;pli<3;pli++){ |
751 | 5.76M | for(zzi=_zzi_start;zzi<_zzi_end;zzi++){ |
752 | 5.34M | dct_tokens=_enc->dct_tokens[pli][zzi]; |
753 | 5.34M | ndct_tokens=_enc->ndct_tokens[pli][zzi]; |
754 | 33.1M | for(ti=_enc->dct_token_offs[pli][zzi];ti<ndct_tokens;ti++){ |
755 | 27.8M | _token_counts_c[dct_tokens[ti]]++; |
756 | 27.8M | } |
757 | 5.34M | } |
758 | 417k | } |
759 | 208k | } |
760 | | |
761 | | /*Computes the number of bits used for each of the potential Huffman code for |
762 | | the given list of token counts. |
763 | | The bits are added to whatever the current bit counts are.*/ |
764 | | static void oc_enc_count_bits(oc_enc_ctx *_enc,int _hgi, |
765 | 417k | const ptrdiff_t _token_counts[32],size_t _bit_counts[16]){ |
766 | 417k | int huffi; |
767 | 417k | int huff_offs; |
768 | 417k | int token; |
769 | 417k | huff_offs=_hgi<<4; |
770 | 7.09M | for(huffi=0;huffi<16;huffi++){ |
771 | 220M | for(token=0;token<32;token++){ |
772 | 213M | _bit_counts[huffi]+= |
773 | 213M | _token_counts[token]*_enc->huff_codes[huffi+huff_offs][token].nbits; |
774 | 213M | } |
775 | 6.68M | } |
776 | 417k | } |
777 | | |
778 | | /*Returns the Huffman index using the fewest number of bits.*/ |
779 | 167k | static int oc_select_huff_idx(size_t _bit_counts[16]){ |
780 | 167k | int best_huffi; |
781 | 167k | int huffi; |
782 | 167k | best_huffi=0; |
783 | 2.67M | for(huffi=1;huffi<16;huffi++)if(_bit_counts[huffi]<_bit_counts[best_huffi]){ |
784 | 333k | best_huffi=huffi; |
785 | 333k | } |
786 | 167k | return best_huffi; |
787 | 167k | } |
788 | | |
789 | | static void oc_enc_huff_group_pack(oc_enc_ctx *_enc, |
790 | 208k | int _zzi_start,int _zzi_end,const int _huff_idxs[2]){ |
791 | 208k | int zzi; |
792 | 2.88M | for(zzi=_zzi_start;zzi<_zzi_end;zzi++){ |
793 | 2.67M | int pli; |
794 | 10.6M | for(pli=0;pli<3;pli++){ |
795 | 8.01M | const unsigned char *dct_tokens; |
796 | 8.01M | const ogg_uint16_t *extra_bits; |
797 | 8.01M | ptrdiff_t ndct_tokens; |
798 | 8.01M | const th_huff_code *huff_codes; |
799 | 8.01M | ptrdiff_t ti; |
800 | 8.01M | dct_tokens=_enc->dct_tokens[pli][zzi]; |
801 | 8.01M | extra_bits=_enc->extra_bits[pli][zzi]; |
802 | 8.01M | ndct_tokens=_enc->ndct_tokens[pli][zzi]; |
803 | 8.01M | huff_codes=_enc->huff_codes[_huff_idxs[pli+1>>1]]; |
804 | 139M | for(ti=_enc->dct_token_offs[pli][zzi];ti<ndct_tokens;ti++){ |
805 | 131M | int token; |
806 | 131M | int neb; |
807 | 131M | token=dct_tokens[ti]; |
808 | 131M | oggpackB_write(&_enc->opb,huff_codes[token].pattern, |
809 | 131M | huff_codes[token].nbits); |
810 | 131M | neb=OC_DCT_TOKEN_EXTRA_BITS[token]; |
811 | 131M | if(neb)oggpackB_write(&_enc->opb,extra_bits[ti],neb); |
812 | 131M | } |
813 | 8.01M | } |
814 | 2.67M | } |
815 | 208k | } |
816 | | |
817 | 41.7k | static void oc_enc_residual_tokens_pack(oc_enc_ctx *_enc){ |
818 | 41.7k | static const unsigned char OC_HUFF_GROUP_MIN[6]={0,1,6,15,28,64}; |
819 | 41.7k | static const unsigned char *OC_HUFF_GROUP_MAX=OC_HUFF_GROUP_MIN+1; |
820 | 41.7k | ptrdiff_t token_counts_y[32]; |
821 | 41.7k | ptrdiff_t token_counts_c[32]; |
822 | 41.7k | size_t bits_y[16]; |
823 | 41.7k | size_t bits_c[16]; |
824 | 41.7k | int huff_idxs[2]; |
825 | 41.7k | int frame_type; |
826 | 41.7k | int hgi; |
827 | 41.7k | frame_type=_enc->state.frame_type; |
828 | | /*Choose which Huffman tables to use for the DC token list.*/ |
829 | 41.7k | oc_enc_count_tokens(_enc,0,1,token_counts_y,token_counts_c); |
830 | 41.7k | memset(bits_y,0,sizeof(bits_y)); |
831 | 41.7k | memset(bits_c,0,sizeof(bits_c)); |
832 | 41.7k | oc_enc_count_bits(_enc,0,token_counts_y,bits_y); |
833 | 41.7k | oc_enc_count_bits(_enc,0,token_counts_c,bits_c); |
834 | 41.7k | huff_idxs[0]=oc_select_huff_idx(bits_y); |
835 | 41.7k | huff_idxs[1]=oc_select_huff_idx(bits_c); |
836 | | /*Write the DC token list with the chosen tables.*/ |
837 | 41.7k | oggpackB_write(&_enc->opb,huff_idxs[0],4); |
838 | 41.7k | oggpackB_write(&_enc->opb,huff_idxs[1],4); |
839 | 41.7k | _enc->huff_idxs[frame_type][0][0]=(unsigned char)huff_idxs[0]; |
840 | 41.7k | _enc->huff_idxs[frame_type][0][1]=(unsigned char)huff_idxs[1]; |
841 | 41.7k | oc_enc_huff_group_pack(_enc,0,1,huff_idxs); |
842 | | /*Choose which Huffman tables to use for the AC token lists.*/ |
843 | 41.7k | memset(bits_y,0,sizeof(bits_y)); |
844 | 41.7k | memset(bits_c,0,sizeof(bits_c)); |
845 | 208k | for(hgi=1;hgi<5;hgi++){ |
846 | 167k | oc_enc_count_tokens(_enc,OC_HUFF_GROUP_MIN[hgi],OC_HUFF_GROUP_MAX[hgi], |
847 | 167k | token_counts_y,token_counts_c); |
848 | 167k | oc_enc_count_bits(_enc,hgi,token_counts_y,bits_y); |
849 | 167k | oc_enc_count_bits(_enc,hgi,token_counts_c,bits_c); |
850 | 167k | } |
851 | 41.7k | huff_idxs[0]=oc_select_huff_idx(bits_y); |
852 | 41.7k | huff_idxs[1]=oc_select_huff_idx(bits_c); |
853 | | /*Write the AC token lists using the chosen tables.*/ |
854 | 41.7k | oggpackB_write(&_enc->opb,huff_idxs[0],4); |
855 | 41.7k | oggpackB_write(&_enc->opb,huff_idxs[1],4); |
856 | 41.7k | _enc->huff_idxs[frame_type][1][0]=(unsigned char)huff_idxs[0]; |
857 | 41.7k | _enc->huff_idxs[frame_type][1][1]=(unsigned char)huff_idxs[1]; |
858 | 208k | for(hgi=1;hgi<5;hgi++){ |
859 | 167k | huff_idxs[0]+=16; |
860 | 167k | huff_idxs[1]+=16; |
861 | 167k | oc_enc_huff_group_pack(_enc, |
862 | 167k | OC_HUFF_GROUP_MIN[hgi],OC_HUFF_GROUP_MAX[hgi],huff_idxs); |
863 | 167k | } |
864 | 41.7k | } |
865 | | |
866 | | /*Packs an explicit drop frame, instead of using the more efficient 0-byte |
867 | | packet. |
868 | | This is only enabled in VP3-compatibility mode, even though it is not |
869 | | strictly required for VP3 compatibility (VP3 could be encoded in AVI, which |
870 | | also supports dropping frames by inserting 0 byte packets). |
871 | | However, almost every _Theora_ player used to get this wrong (and many still |
872 | | do), and it wasn't until we started shipping a post-VP3 encoder that |
873 | | actually used non-VP3 features that this began to be discovered and fixed, |
874 | | despite being in the standard since 2004. |
875 | | The pack buffer must be reset before calling this function.*/ |
876 | 0 | static void oc_enc_drop_frame_pack(oc_enc_ctx *_enc){ |
877 | 0 | unsigned nsbs; |
878 | | /*Mark this as a data packet.*/ |
879 | 0 | oggpackB_write(&_enc->opb,0,1); |
880 | | /*Output the frame type (key frame or delta frame).*/ |
881 | 0 | oggpackB_write(&_enc->opb,OC_INTER_FRAME,1); |
882 | | /*Write out the current qi list. |
883 | | We always use just 1 qi, to avoid wasting bits on the others.*/ |
884 | 0 | oggpackB_write(&_enc->opb,_enc->state.qis[0],6); |
885 | 0 | oggpackB_write(&_enc->opb,0,1); |
886 | | /*Coded block flags: everything is uncoded.*/ |
887 | 0 | nsbs=_enc->state.nsbs; |
888 | | /*No partially coded SBs.*/ |
889 | 0 | oggpackB_write(&_enc->opb,0,1); |
890 | 0 | oc_sb_run_pack(&_enc->opb,nsbs,0,1); |
891 | | /*No fully coded SBs.*/ |
892 | 0 | oggpackB_write(&_enc->opb,0,1); |
893 | 0 | oc_sb_run_pack(&_enc->opb,nsbs,0,1); |
894 | | /*MB modes: just need write which scheme to use. |
895 | | Since we have no coded MBs, we can pick any of them except 0, which would |
896 | | require writing out an additional mode list.*/ |
897 | 0 | oggpackB_write(&_enc->opb,7,3); |
898 | | /*MVs: just need write which scheme to use. |
899 | | We can pick either one, since we have no MVs.*/ |
900 | 0 | oggpackB_write(&_enc->opb,1,1); |
901 | | /*Write the chosen DC token tables.*/ |
902 | 0 | oggpackB_write(&_enc->opb,_enc->huff_idxs[OC_INTER_FRAME][0][0],4); |
903 | 0 | oggpackB_write(&_enc->opb,_enc->huff_idxs[OC_INTER_FRAME][0][1],4); |
904 | | /*Write the chosen AC token tables.*/ |
905 | 0 | oggpackB_write(&_enc->opb,_enc->huff_idxs[OC_INTER_FRAME][1][0],4); |
906 | 0 | oggpackB_write(&_enc->opb,_enc->huff_idxs[OC_INTER_FRAME][1][1],4); |
907 | 0 | } |
908 | | |
909 | 47.3k | static void oc_enc_frame_pack(oc_enc_ctx *_enc){ |
910 | | /*musl libc malloc()/realloc() calls might use floating point, so make sure |
911 | | we've cleared the MMX state for them.*/ |
912 | 47.3k | oc_restore_fpu(&_enc->state); |
913 | 47.3k | oggpackB_reset(&_enc->opb); |
914 | | /*Only proceed if we have some coded blocks.*/ |
915 | 47.3k | if(_enc->state.ntotal_coded_fragis>0){ |
916 | 41.7k | oc_enc_frame_header_pack(_enc); |
917 | 41.7k | if(_enc->state.frame_type==OC_INTER_FRAME){ |
918 | | /*Coded block flags, MB modes, and MVs are only needed for delta frames.*/ |
919 | 19.0k | oc_enc_coded_flags_pack(_enc); |
920 | 19.0k | oc_enc_mb_modes_pack(_enc); |
921 | 19.0k | oc_enc_mvs_pack(_enc); |
922 | 19.0k | } |
923 | 41.7k | oc_enc_block_qis_pack(_enc); |
924 | 41.7k | oc_enc_tokenize_finish(_enc); |
925 | 41.7k | oc_enc_residual_tokens_pack(_enc); |
926 | 41.7k | } |
927 | | /*If there are no coded blocks, we can drop this frame simply by emitting a |
928 | | 0 byte packet. |
929 | | We emit an inter frame with no coded blocks in VP3-compatibility mode.*/ |
930 | 5.63k | else if(_enc->vp3_compatible)oc_enc_drop_frame_pack(_enc); |
931 | | /*Success: Mark the packet as ready to be flushed.*/ |
932 | 47.3k | _enc->packet_state=OC_PACKET_READY; |
933 | | #if defined(OC_COLLECT_METRICS) |
934 | | oc_enc_mode_metrics_collect(_enc); |
935 | | #endif |
936 | 47.3k | } |
937 | | |
938 | | |
939 | 3.44k | void oc_enc_accel_init_c(oc_enc_ctx *_enc){ |
940 | | /*The implementations prefixed with oc_enc_ are encoder-specific. |
941 | | The rest we re-use from the decoder.*/ |
942 | | # if defined(OC_ENC_USE_VTABLE) |
943 | | _enc->opt_vtable.frag_sub=oc_enc_frag_sub_c; |
944 | | _enc->opt_vtable.frag_sub_128=oc_enc_frag_sub_128_c; |
945 | | _enc->opt_vtable.frag_sad=oc_enc_frag_sad_c; |
946 | | _enc->opt_vtable.frag_sad_thresh=oc_enc_frag_sad_thresh_c; |
947 | | _enc->opt_vtable.frag_sad2_thresh=oc_enc_frag_sad2_thresh_c; |
948 | | _enc->opt_vtable.frag_intra_sad=oc_enc_frag_intra_sad_c; |
949 | | _enc->opt_vtable.frag_satd=oc_enc_frag_satd_c; |
950 | | _enc->opt_vtable.frag_satd2=oc_enc_frag_satd2_c; |
951 | | _enc->opt_vtable.frag_intra_satd=oc_enc_frag_intra_satd_c; |
952 | | _enc->opt_vtable.frag_ssd=oc_enc_frag_ssd_c; |
953 | | _enc->opt_vtable.frag_border_ssd=oc_enc_frag_border_ssd_c; |
954 | | _enc->opt_vtable.frag_copy2=oc_enc_frag_copy2_c; |
955 | | _enc->opt_vtable.enquant_table_init=oc_enc_enquant_table_init_c; |
956 | | _enc->opt_vtable.enquant_table_fixup=oc_enc_enquant_table_fixup_c; |
957 | | _enc->opt_vtable.quantize=oc_enc_quantize_c; |
958 | | _enc->opt_vtable.frag_recon_intra=oc_frag_recon_intra_c; |
959 | | _enc->opt_vtable.frag_recon_inter=oc_frag_recon_inter_c; |
960 | | _enc->opt_vtable.fdct8x8=oc_enc_fdct8x8_c; |
961 | | # endif |
962 | 3.44k | _enc->opt_data.enquant_table_size=64*sizeof(oc_iquant); |
963 | 3.44k | _enc->opt_data.enquant_table_alignment=16; |
964 | 3.44k | } |
965 | | |
966 | | /*Initialize the macro block neighbor lists for MC analysis. |
967 | | This assumes that the entire mb_info memory region has been initialized with |
968 | | zeros.*/ |
969 | 3.44k | static void oc_enc_mb_info_init(oc_enc_ctx *_enc){ |
970 | 3.44k | oc_mb_enc_info *embs; |
971 | 3.44k | const signed char *mb_modes; |
972 | 3.44k | unsigned nhsbs; |
973 | 3.44k | unsigned nvsbs; |
974 | 3.44k | unsigned nhmbs; |
975 | 3.44k | unsigned nvmbs; |
976 | 3.44k | unsigned sby; |
977 | 3.44k | mb_modes=_enc->state.mb_modes; |
978 | 3.44k | embs=_enc->mb_info; |
979 | 3.44k | nhsbs=_enc->state.fplanes[0].nhsbs; |
980 | 3.44k | nvsbs=_enc->state.fplanes[0].nvsbs; |
981 | 3.44k | nhmbs=_enc->state.nhmbs; |
982 | 3.44k | nvmbs=_enc->state.nvmbs; |
983 | 110k | for(sby=0;sby<nvsbs;sby++){ |
984 | 107k | unsigned sbx; |
985 | 746k | for(sbx=0;sbx<nhsbs;sbx++){ |
986 | 639k | int quadi; |
987 | 3.19M | for(quadi=0;quadi<4;quadi++){ |
988 | | /*Because of the Hilbert curve ordering the macro blocks are |
989 | | visited in, the available neighbors change depending on where in |
990 | | a super block the macro block is located. |
991 | | Only the first three vectors are used in the median calculation |
992 | | for the optimal predictor, and so the most important should be |
993 | | listed first. |
994 | | Additional vectors are used, so there will always be at least 3, |
995 | | except for in the upper-left most macro block.*/ |
996 | | /*The number of current neighbors for each macro block position.*/ |
997 | 2.55M | static const unsigned char NCNEIGHBORS[4]={4,3,2,4}; |
998 | | /*The offset of each current neighbor in the X direction.*/ |
999 | 2.55M | static const signed char CDX[4][4]={ |
1000 | 2.55M | {-1,0,1,-1}, |
1001 | 2.55M | {-1,0,-1,}, |
1002 | 2.55M | {-1,-1}, |
1003 | 2.55M | {-1,0,0,1} |
1004 | 2.55M | }; |
1005 | | /*The offset of each current neighbor in the Y direction.*/ |
1006 | 2.55M | static const signed char CDY[4][4]={ |
1007 | 2.55M | {0,-1,-1,-1}, |
1008 | 2.55M | {0,-1,-1}, |
1009 | 2.55M | {0,-1}, |
1010 | 2.55M | {0,-1,1,-1} |
1011 | 2.55M | }; |
1012 | | /*The offset of each previous neighbor in the X direction.*/ |
1013 | 2.55M | static const signed char PDX[4]={-1,0,1,0}; |
1014 | | /*The offset of each previous neighbor in the Y direction.*/ |
1015 | 2.55M | static const signed char PDY[4]={0,-1,0,1}; |
1016 | 2.55M | unsigned mbi; |
1017 | 2.55M | int mbx; |
1018 | 2.55M | int mby; |
1019 | 2.55M | unsigned nmbi; |
1020 | 2.55M | int nmbx; |
1021 | 2.55M | int nmby; |
1022 | 2.55M | int ni; |
1023 | 2.55M | mbi=(sby*nhsbs+sbx<<2)+quadi; |
1024 | 2.55M | if(mb_modes[mbi]==OC_MODE_INVALID)continue; |
1025 | 1.36M | mbx=2*sbx+(quadi>>1); |
1026 | 1.36M | mby=2*sby+(quadi+1>>1&1); |
1027 | | /*Fill in the neighbors with current motion vectors available.*/ |
1028 | 6.58M | for(ni=0;ni<NCNEIGHBORS[quadi];ni++){ |
1029 | 5.22M | nmbx=mbx+CDX[quadi][ni]; |
1030 | 5.22M | nmby=mby+CDY[quadi][ni]; |
1031 | 5.22M | if(nmbx<0||nmbx>=nhmbs||nmby<0||nmby>=nvmbs)continue; |
1032 | 2.22M | nmbi=(nmby&~1)*nhmbs+((nmbx&~1)<<1)+OC_MB_MAP[nmby&1][nmbx&1]; |
1033 | 2.22M | if(mb_modes[nmbi]==OC_MODE_INVALID)continue; |
1034 | 1.62M | embs[mbi].cneighbors[embs[mbi].ncneighbors++]=nmbi; |
1035 | 1.62M | } |
1036 | | /*Fill in the neighbors with previous motion vectors available.*/ |
1037 | 6.80M | for(ni=0;ni<4;ni++){ |
1038 | 5.44M | nmbx=mbx+PDX[ni]; |
1039 | 5.44M | nmby=mby+PDY[ni]; |
1040 | 5.44M | if(nmbx<0||nmbx>=nhmbs||nmby<0||nmby>=nvmbs)continue; |
1041 | 4.16M | nmbi=(nmby&~1)*nhmbs+((nmbx&~1)<<1)+OC_MB_MAP[nmby&1][nmbx&1]; |
1042 | 4.16M | if(mb_modes[nmbi]==OC_MODE_INVALID)continue; |
1043 | 2.96M | embs[mbi].pneighbors[embs[mbi].npneighbors++]=nmbi; |
1044 | 2.96M | } |
1045 | 1.36M | } |
1046 | 639k | } |
1047 | 107k | } |
1048 | 3.44k | } |
1049 | | |
1050 | | static int oc_enc_set_huffman_codes(oc_enc_ctx *_enc, |
1051 | 0 | const th_huff_code _codes[TH_NHUFFMAN_TABLES][TH_NDCT_TOKENS]){ |
1052 | 0 | int ret; |
1053 | 0 | if(_enc==NULL)return TH_EFAULT; |
1054 | 0 | if(_enc->packet_state>OC_PACKET_SETUP_HDR)return TH_EINVAL; |
1055 | 0 | if(_codes==NULL)_codes=TH_VP31_HUFF_CODES; |
1056 | | /*Validate the codes.*/ |
1057 | 0 | oggpackB_reset(&_enc->opb); |
1058 | 0 | ret=oc_huff_codes_pack(&_enc->opb,_codes); |
1059 | 0 | if(ret<0)return ret; |
1060 | 0 | memcpy(_enc->huff_codes,_codes,sizeof(_enc->huff_codes)); |
1061 | 0 | return 0; |
1062 | 0 | } |
1063 | | |
1064 | | static void oc_enc_enquant_tables_init(oc_enc_ctx *_enc, |
1065 | 3.44k | const th_quant_info *_qinfo){ |
1066 | 3.44k | unsigned char *etd; |
1067 | 3.44k | size_t ets; |
1068 | 3.44k | int align; |
1069 | 3.44k | int qii; |
1070 | 3.44k | int qi; |
1071 | 3.44k | int pli; |
1072 | 3.44k | int qti; |
1073 | 1.98M | for(qi=0;qi<64;qi++)for(pli=0;pli<3;pli++)for(qti=0;qti<2;qti++){ |
1074 | 1.32M | _enc->state.dequant_tables[qi][pli][qti]= |
1075 | 1.32M | _enc->state.dequant_table_data[qi][pli][qti]; |
1076 | 1.32M | } |
1077 | | /*Initialize the dequantization tables.*/ |
1078 | 3.44k | oc_dequant_tables_init(_enc->state.dequant_tables,NULL,_qinfo); |
1079 | | /*And save off the DC values.*/ |
1080 | 1.98M | for(qi=0;qi<64;qi++)for(pli=0;pli<3;pli++)for(qti=0;qti<2;qti++){ |
1081 | 1.32M | _enc->dequant_dc[qi][pli][qti]=_enc->state.dequant_tables[qi][pli][qti][0]; |
1082 | 1.32M | } |
1083 | | /*Set up storage for the quantization tables.*/ |
1084 | 3.44k | etd=_enc->enquant_table_data; |
1085 | 3.44k | ets=_enc->opt_data.enquant_table_size; |
1086 | 3.44k | align=-(etd-(unsigned char *)0)&_enc->opt_data.enquant_table_alignment-1; |
1087 | 3.44k | etd+=align; |
1088 | | /*Set up the main tables.*/ |
1089 | 1.98M | for(qi=0;qi<64;qi++)for(pli=0;pli<3;pli++)for(qti=0;qti<2;qti++){ |
1090 | 1.32M | _enc->enquant_tables[qi][pli][qti]=etd; |
1091 | 1.32M | oc_enc_enquant_table_init(_enc,etd, |
1092 | 1.32M | _enc->state.dequant_tables[qi][pli][qti]); |
1093 | 1.32M | etd+=ets; |
1094 | 1.32M | } |
1095 | | /*Set up storage for the local copies we modify for each frame.*/ |
1096 | 93.0k | for(pli=0;pli<3;pli++)for(qii=0;qii<3;qii++)for(qti=0;qti<2;qti++){ |
1097 | 62.0k | _enc->enquant[pli][qii][qti]=etd; |
1098 | 62.0k | etd+=ets; |
1099 | 62.0k | } |
1100 | 3.44k | } |
1101 | | |
1102 | | /*Updates the encoder state after the quantization parameters have been |
1103 | | changed.*/ |
1104 | | static void oc_enc_quant_params_updated(oc_enc_ctx *_enc, |
1105 | 3.44k | const th_quant_info *_qinfo){ |
1106 | 3.44k | oc_enc_enquant_tables_init(_enc,_qinfo); |
1107 | 3.44k | memcpy(_enc->state.loop_filter_limits,_qinfo->loop_filter_limits, |
1108 | 3.44k | sizeof(_enc->state.loop_filter_limits)); |
1109 | 3.44k | oc_enquant_qavg_init(_enc->log_qavg,_enc->log_plq,_enc->chroma_rd_scale, |
1110 | 3.44k | _enc->state.dequant_tables,_enc->state.info.pixel_fmt); |
1111 | 3.44k | } |
1112 | | |
1113 | | /*Sets the quantization parameters to use. |
1114 | | This may only be called before the setup header is written. |
1115 | | If it is called multiple times, only the last call has any effect. |
1116 | | _qinfo: The quantization parameters. |
1117 | | These are described in more detail in theoraenc.h. |
1118 | | This can be NULL, in which case the default quantization parameters |
1119 | | will be used.*/ |
1120 | | static int oc_enc_set_quant_params(oc_enc_ctx *_enc, |
1121 | 3.44k | const th_quant_info *_qinfo){ |
1122 | 3.44k | th_quant_info old_qinfo; |
1123 | 3.44k | int ret; |
1124 | 3.44k | if(_enc==NULL)return TH_EFAULT; |
1125 | 3.44k | if(_enc->packet_state>OC_PACKET_SETUP_HDR)return TH_EINVAL; |
1126 | 3.44k | if(_qinfo==NULL)_qinfo=&TH_DEF_QUANT_INFO; |
1127 | 3.44k | memcpy(&old_qinfo,&_enc->qinfo,sizeof(old_qinfo)); |
1128 | 3.44k | ret=oc_quant_params_clone(&_enc->qinfo,_qinfo); |
1129 | 3.44k | if(ret<0){ |
1130 | 0 | oc_quant_params_clear(&_enc->qinfo); |
1131 | 0 | memcpy(&_enc->qinfo,&old_qinfo,sizeof(old_qinfo)); |
1132 | 0 | return ret; |
1133 | 0 | } |
1134 | 3.44k | else oc_quant_params_clear(&old_qinfo); |
1135 | 3.44k | oc_enc_quant_params_updated(_enc,_qinfo); |
1136 | 3.44k | return 0; |
1137 | 3.44k | } |
1138 | | |
1139 | | static void oc_enc_clear(oc_enc_ctx *_enc); |
1140 | | |
1141 | 3.44k | static int oc_enc_init(oc_enc_ctx *_enc,const th_info *_info){ |
1142 | 3.44k | th_info info; |
1143 | 3.44k | size_t mcu_nmbs; |
1144 | 3.44k | ptrdiff_t mcu_ncfrags; |
1145 | 3.44k | ptrdiff_t mcu_nfrags; |
1146 | 3.44k | int hdec; |
1147 | 3.44k | int vdec; |
1148 | 3.44k | int ret; |
1149 | 3.44k | int pli; |
1150 | | /*Clean up the requested settings.*/ |
1151 | 3.44k | memcpy(&info,_info,sizeof(info)); |
1152 | 3.44k | info.version_major=TH_VERSION_MAJOR; |
1153 | 3.44k | info.version_minor=TH_VERSION_MINOR; |
1154 | 3.44k | info.version_subminor=TH_VERSION_SUB; |
1155 | 3.44k | if(info.quality>63)info.quality=63; |
1156 | 3.44k | if(info.quality<0)info.quality=32; |
1157 | 3.44k | if(info.target_bitrate<0)info.target_bitrate=0; |
1158 | | /*Initialize the shared encoder/decoder state.*/ |
1159 | 3.44k | ret=oc_state_init(&_enc->state,&info,6); |
1160 | 3.44k | if(ret<0)return ret; |
1161 | 3.44k | oc_enc_accel_init(_enc); |
1162 | 3.44k | _enc->mb_info=_ogg_calloc(_enc->state.nmbs,sizeof(*_enc->mb_info)); |
1163 | 3.44k | _enc->frag_dc=_ogg_calloc(_enc->state.nfrags,sizeof(*_enc->frag_dc)); |
1164 | 3.44k | _enc->coded_mbis= |
1165 | 3.44k | (unsigned *)_ogg_malloc(_enc->state.nmbs*sizeof(*_enc->coded_mbis)); |
1166 | 3.44k | hdec=!(_enc->state.info.pixel_fmt&1); |
1167 | 3.44k | vdec=!(_enc->state.info.pixel_fmt&2); |
1168 | | /*If chroma is sub-sampled in the vertical direction, we have to encode two |
1169 | | super block rows of Y' for each super block row of Cb and Cr.*/ |
1170 | 3.44k | _enc->mcu_nvsbs=1<<vdec; |
1171 | 3.44k | mcu_nmbs=_enc->mcu_nvsbs*_enc->state.fplanes[0].nhsbs*(size_t)4; |
1172 | 3.44k | mcu_ncfrags=mcu_nmbs<<3-(hdec+vdec); |
1173 | 3.44k | mcu_nfrags=4*mcu_nmbs+mcu_ncfrags; |
1174 | 3.44k | _enc->mcu_skip_ssd=(unsigned *)_ogg_malloc( |
1175 | 3.44k | mcu_nfrags*sizeof(*_enc->mcu_skip_ssd)); |
1176 | 3.44k | _enc->mcu_rd_scale=(ogg_uint16_t *)_ogg_malloc( |
1177 | 3.44k | (mcu_ncfrags>>1)*sizeof(*_enc->mcu_rd_scale)); |
1178 | 3.44k | _enc->mcu_rd_iscale=(ogg_uint16_t *)_ogg_malloc( |
1179 | 3.44k | (mcu_ncfrags>>1)*sizeof(*_enc->mcu_rd_iscale)); |
1180 | 13.7k | for(pli=0;pli<3;pli++){ |
1181 | 10.3k | _enc->dct_tokens[pli]=(unsigned char **)oc_malloc_2d(64, |
1182 | 10.3k | _enc->state.fplanes[pli].nfrags,sizeof(**_enc->dct_tokens)); |
1183 | 10.3k | _enc->extra_bits[pli]=(ogg_uint16_t **)oc_malloc_2d(64, |
1184 | 10.3k | _enc->state.fplanes[pli].nfrags,sizeof(**_enc->extra_bits)); |
1185 | 10.3k | } |
1186 | | #if defined(OC_COLLECT_METRICS) |
1187 | | _enc->frag_sad=_ogg_calloc(_enc->state.nfrags,sizeof(*_enc->frag_sad)); |
1188 | | _enc->frag_satd=_ogg_calloc(_enc->state.nfrags,sizeof(*_enc->frag_satd)); |
1189 | | _enc->frag_ssd=_ogg_calloc(_enc->state.nfrags,sizeof(*_enc->frag_ssd)); |
1190 | | #endif |
1191 | 3.44k | _enc->enquant_table_data=(unsigned char *)_ogg_malloc( |
1192 | 3.44k | (64+3)*3*2*_enc->opt_data.enquant_table_size |
1193 | 3.44k | +_enc->opt_data.enquant_table_alignment-1); |
1194 | 3.44k | _enc->keyframe_frequency_force=1<<_enc->state.info.keyframe_granule_shift; |
1195 | 3.44k | _enc->state.qis[0]=_enc->state.info.quality; |
1196 | 3.44k | _enc->state.nqis=1; |
1197 | 3.44k | _enc->activity_avg=90<<12; |
1198 | 3.44k | _enc->luma_avg=128<<8; |
1199 | 3.44k | oc_rc_state_init(&_enc->rc,_enc); |
1200 | 3.44k | oggpackB_writeinit(&_enc->opb); |
1201 | 3.44k | memcpy(_enc->huff_codes,TH_VP31_HUFF_CODES,sizeof(_enc->huff_codes)); |
1202 | 3.44k | memset(_enc->qinfo.qi_ranges,0,sizeof(_enc->qinfo.qi_ranges)); |
1203 | | /*Reset the packet-out state machine.*/ |
1204 | 3.44k | _enc->packet_state=OC_PACKET_INFO_HDR; |
1205 | 3.44k | _enc->dup_count=0; |
1206 | 3.44k | _enc->nqueued_dups=0; |
1207 | 3.44k | _enc->prev_dup_count=0; |
1208 | | /*Enable speed optimizations up through early skip by default.*/ |
1209 | 3.44k | _enc->sp_level=OC_SP_LEVEL_EARLY_SKIP; |
1210 | | /*Disable VP3 compatibility by default.*/ |
1211 | 3.44k | _enc->vp3_compatible=0; |
1212 | | /*No INTER frames coded yet.*/ |
1213 | 3.44k | _enc->coded_inter_frame=0; |
1214 | 3.44k | if(_enc->mb_info==NULL||_enc->frag_dc==NULL||_enc->coded_mbis==NULL |
1215 | 3.44k | ||_enc->mcu_skip_ssd==NULL||_enc->dct_tokens[0]==NULL |
1216 | 3.44k | ||_enc->dct_tokens[1]==NULL||_enc->dct_tokens[2]==NULL |
1217 | 3.44k | ||_enc->extra_bits[0]==NULL||_enc->extra_bits[1]==NULL |
1218 | 3.44k | ||_enc->extra_bits[2]==NULL |
1219 | | #if defined(OC_COLLECT_METRICS) |
1220 | | ||_enc->frag_sad==NULL||_enc->frag_satd==NULL||_enc->frag_ssd==NULL |
1221 | | #endif |
1222 | 3.44k | ||oc_enc_set_quant_params(_enc,NULL)<0){ |
1223 | 0 | oc_enc_clear(_enc); |
1224 | 0 | return TH_EFAULT; |
1225 | 0 | } |
1226 | 3.44k | oc_mode_scheme_chooser_init(&_enc->chooser); |
1227 | 3.44k | oc_enc_mb_info_init(_enc); |
1228 | 3.44k | memset(_enc->huff_idxs,0,sizeof(_enc->huff_idxs)); |
1229 | 3.44k | return 0; |
1230 | 3.44k | } |
1231 | | |
1232 | 3.44k | static void oc_enc_clear(oc_enc_ctx *_enc){ |
1233 | 3.44k | int pli; |
1234 | 3.44k | oc_rc_state_clear(&_enc->rc); |
1235 | 3.44k | oggpackB_writeclear(&_enc->opb); |
1236 | 3.44k | oc_quant_params_clear(&_enc->qinfo); |
1237 | 3.44k | _ogg_free(_enc->enquant_table_data); |
1238 | | #if defined(OC_COLLECT_METRICS) |
1239 | | /*Save the collected metrics from this run. |
1240 | | Use tools/process_modedec_stats to actually generate modedec.h from the |
1241 | | resulting file.*/ |
1242 | | oc_mode_metrics_dump(); |
1243 | | _ogg_free(_enc->frag_ssd); |
1244 | | _ogg_free(_enc->frag_satd); |
1245 | | _ogg_free(_enc->frag_sad); |
1246 | | #endif |
1247 | 13.7k | for(pli=3;pli-->0;){ |
1248 | 10.3k | oc_free_2d(_enc->extra_bits[pli]); |
1249 | 10.3k | oc_free_2d(_enc->dct_tokens[pli]); |
1250 | 10.3k | } |
1251 | 3.44k | _ogg_free(_enc->mcu_rd_iscale); |
1252 | 3.44k | _ogg_free(_enc->mcu_rd_scale); |
1253 | 3.44k | _ogg_free(_enc->mcu_skip_ssd); |
1254 | 3.44k | _ogg_free(_enc->coded_mbis); |
1255 | 3.44k | _ogg_free(_enc->frag_dc); |
1256 | 3.44k | _ogg_free(_enc->mb_info); |
1257 | 3.44k | oc_state_clear(&_enc->state); |
1258 | 3.44k | } |
1259 | | |
1260 | 3.27k | static void oc_enc_drop_frame(th_enc_ctx *_enc){ |
1261 | | /*Use the previous frame's reconstruction.*/ |
1262 | 3.27k | _enc->state.ref_frame_idx[OC_FRAME_SELF]= |
1263 | 3.27k | _enc->state.ref_frame_idx[OC_FRAME_PREV]; |
1264 | 3.27k | _enc->state.ref_frame_data[OC_FRAME_SELF]= |
1265 | 3.27k | _enc->state.ref_frame_data[OC_FRAME_PREV]; |
1266 | | /*Flag motion vector analysis about the frame drop.*/ |
1267 | 3.27k | _enc->prevframe_dropped=1; |
1268 | | /*Zero the packet.*/ |
1269 | 3.27k | oggpackB_reset(&_enc->opb); |
1270 | | /*Emit an inter frame with no coded blocks in VP3-compatibility mode.*/ |
1271 | 3.27k | if(_enc->vp3_compatible)oc_enc_drop_frame_pack(_enc); |
1272 | 3.27k | } |
1273 | | |
1274 | 22.6k | static void oc_enc_compress_keyframe(oc_enc_ctx *_enc,int _recode){ |
1275 | 22.6k | if(_enc->state.info.target_bitrate>0){ |
1276 | 20.0k | _enc->state.qis[0]=oc_enc_select_qi(_enc,OC_INTRA_FRAME, |
1277 | 20.0k | _enc->state.curframe_num>0); |
1278 | 20.0k | _enc->state.nqis=1; |
1279 | 20.0k | } |
1280 | 22.6k | oc_enc_calc_lambda(_enc,OC_INTRA_FRAME); |
1281 | 22.6k | oc_enc_analyze_intra(_enc,_recode); |
1282 | 22.6k | oc_enc_frame_pack(_enc); |
1283 | | /*On the first frame, the previous call was an initial dry-run to prime |
1284 | | feed-forward statistics.*/ |
1285 | 22.6k | if(!_recode&&_enc->state.curframe_num==0){ |
1286 | 3.44k | if(_enc->state.info.target_bitrate>0){ |
1287 | 2.77k | oc_enc_update_rc_state(_enc,oggpackB_bytes(&_enc->opb)<<3, |
1288 | 2.77k | OC_INTRA_FRAME,_enc->state.qis[0],1,0); |
1289 | 2.77k | } |
1290 | 3.44k | oc_enc_compress_keyframe(_enc,1); |
1291 | 3.44k | } |
1292 | 22.6k | } |
1293 | | |
1294 | 39.4k | static void oc_enc_compress_frame(oc_enc_ctx *_enc,int _recode){ |
1295 | 39.4k | if(_enc->state.info.target_bitrate>0){ |
1296 | 29.7k | _enc->state.qis[0]=oc_enc_select_qi(_enc,OC_INTER_FRAME,1); |
1297 | 29.7k | _enc->state.nqis=1; |
1298 | 29.7k | } |
1299 | 39.4k | oc_enc_calc_lambda(_enc,OC_INTER_FRAME); |
1300 | 39.4k | if(oc_enc_analyze_inter(_enc,_enc->rc.twopass!=2,_recode)){ |
1301 | | /*Mode analysis thinks this should have been a keyframe; start over.*/ |
1302 | 14.7k | oc_enc_compress_keyframe(_enc,1); |
1303 | 14.7k | } |
1304 | 24.7k | else{ |
1305 | 24.7k | oc_enc_frame_pack(_enc); |
1306 | 24.7k | if(!_enc->coded_inter_frame){ |
1307 | | /*On the first INTER frame, the previous call was an initial dry-run to |
1308 | | prime feed-forward statistics.*/ |
1309 | 1.75k | _enc->coded_inter_frame=1; |
1310 | 1.75k | if(_enc->state.info.target_bitrate>0){ |
1311 | | /*Rate control also needs to prime.*/ |
1312 | 1.35k | oc_enc_update_rc_state(_enc,oggpackB_bytes(&_enc->opb)<<3, |
1313 | 1.35k | OC_INTER_FRAME,_enc->state.qis[0],1,0); |
1314 | 1.35k | } |
1315 | 1.75k | oc_enc_compress_frame(_enc,1); |
1316 | 1.75k | } |
1317 | 24.7k | } |
1318 | 39.4k | } |
1319 | | |
1320 | | /*Set the granule position for the next packet to output based on the current |
1321 | | internal state.*/ |
1322 | 42.1k | static void oc_enc_set_granpos(oc_enc_ctx *_enc){ |
1323 | 42.1k | unsigned dup_offs; |
1324 | | /*Add an offset for the number of duplicate frames we've emitted so far.*/ |
1325 | 42.1k | dup_offs=_enc->prev_dup_count-_enc->nqueued_dups; |
1326 | | /*If the current frame was a keyframe, use it for the high part.*/ |
1327 | 42.1k | if(_enc->state.frame_type==OC_INTRA_FRAME){ |
1328 | 19.2k | _enc->state.granpos=(_enc->state.curframe_num+_enc->state.granpos_bias<< |
1329 | 19.2k | _enc->state.info.keyframe_granule_shift)+dup_offs; |
1330 | 19.2k | } |
1331 | | /*Otherwise use the last keyframe in the high part and put the current frame |
1332 | | in the low part.*/ |
1333 | 22.9k | else{ |
1334 | 22.9k | _enc->state.granpos= |
1335 | 22.9k | (_enc->state.keyframe_num+_enc->state.granpos_bias<< |
1336 | 22.9k | _enc->state.info.keyframe_granule_shift) |
1337 | 22.9k | +_enc->state.curframe_num-_enc->state.keyframe_num+dup_offs; |
1338 | 22.9k | } |
1339 | 42.1k | } |
1340 | | |
1341 | | |
1342 | 3.44k | th_enc_ctx *th_encode_alloc(const th_info *_info){ |
1343 | 3.44k | oc_enc_ctx *enc; |
1344 | 3.44k | if(_info==NULL)return NULL; |
1345 | 3.44k | enc=oc_aligned_malloc(sizeof(*enc),16); |
1346 | 3.44k | if(enc==NULL||oc_enc_init(enc,_info)<0){ |
1347 | 0 | oc_aligned_free(enc); |
1348 | 0 | return NULL; |
1349 | 0 | } |
1350 | 3.44k | return enc; |
1351 | 3.44k | } |
1352 | | |
1353 | 3.44k | void th_encode_free(th_enc_ctx *_enc){ |
1354 | 3.44k | if(_enc!=NULL){ |
1355 | 3.44k | oc_enc_clear(_enc); |
1356 | 3.44k | oc_aligned_free(_enc); |
1357 | 3.44k | } |
1358 | 3.44k | } |
1359 | | |
1360 | 3.44k | int th_encode_ctl(th_enc_ctx *_enc,int _req,void *_buf,size_t _buf_sz){ |
1361 | 3.44k | switch(_req){ |
1362 | 0 | case TH_ENCCTL_SET_HUFFMAN_CODES:{ |
1363 | 0 | if(_buf==NULL&&_buf_sz!=0|| |
1364 | 0 | _buf!=NULL&&_buf_sz!=sizeof(th_huff_table)*TH_NHUFFMAN_TABLES){ |
1365 | 0 | return TH_EINVAL; |
1366 | 0 | } |
1367 | 0 | return oc_enc_set_huffman_codes(_enc,(const th_huff_table *)_buf); |
1368 | 0 | }break; |
1369 | 0 | case TH_ENCCTL_SET_QUANT_PARAMS:{ |
1370 | 0 | if(_buf==NULL&&_buf_sz!=0|| |
1371 | 0 | _buf!=NULL&&_buf_sz!=sizeof(th_quant_info)){ |
1372 | 0 | return TH_EINVAL; |
1373 | 0 | } |
1374 | 0 | return oc_enc_set_quant_params(_enc,(th_quant_info *)_buf); |
1375 | 0 | }break; |
1376 | 3.44k | case TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE:{ |
1377 | 3.44k | ogg_uint32_t keyframe_frequency_force; |
1378 | 3.44k | if(_enc==NULL||_buf==NULL)return TH_EFAULT; |
1379 | 3.44k | if(_buf_sz!=sizeof(keyframe_frequency_force))return TH_EINVAL; |
1380 | 3.44k | keyframe_frequency_force=*(ogg_uint32_t *)_buf; |
1381 | 3.44k | if(keyframe_frequency_force<=0)keyframe_frequency_force=1; |
1382 | 3.44k | if(_enc->packet_state==OC_PACKET_INFO_HDR){ |
1383 | | /*It's still early enough to enlarge keyframe_granule_shift.*/ |
1384 | 3.44k | _enc->state.info.keyframe_granule_shift=OC_CLAMPI( |
1385 | 3.44k | _enc->state.info.keyframe_granule_shift, |
1386 | 3.44k | OC_ILOG_32(keyframe_frequency_force-1),31); |
1387 | 3.44k | } |
1388 | 3.44k | _enc->keyframe_frequency_force=OC_MINI(keyframe_frequency_force, |
1389 | 3.44k | (ogg_uint32_t)1U<<_enc->state.info.keyframe_granule_shift); |
1390 | 3.44k | *(ogg_uint32_t *)_buf=_enc->keyframe_frequency_force; |
1391 | 3.44k | return 0; |
1392 | 3.44k | }break; |
1393 | 0 | case TH_ENCCTL_SET_VP3_COMPATIBLE:{ |
1394 | 0 | int vp3_compatible; |
1395 | 0 | int ret; |
1396 | 0 | if(_enc==NULL||_buf==NULL)return TH_EFAULT; |
1397 | 0 | if(_buf_sz!=sizeof(vp3_compatible))return TH_EINVAL; |
1398 | | /*Try this before we change anything else, because it can fail.*/ |
1399 | 0 | ret=oc_enc_set_quant_params(_enc,&TH_VP31_QUANT_INFO); |
1400 | | /*If we can't allocate enough memory, don't change any of the state.*/ |
1401 | 0 | if(ret==TH_EFAULT)return ret; |
1402 | 0 | vp3_compatible=*(int *)_buf; |
1403 | 0 | _enc->vp3_compatible=vp3_compatible; |
1404 | 0 | if(oc_enc_set_huffman_codes(_enc,TH_VP31_HUFF_CODES)<0)vp3_compatible=0; |
1405 | 0 | if(ret<0)vp3_compatible=0; |
1406 | 0 | if(_enc->state.info.pixel_fmt!=TH_PF_420|| |
1407 | 0 | _enc->state.info.pic_width<_enc->state.info.frame_width|| |
1408 | 0 | _enc->state.info.pic_height<_enc->state.info.frame_height|| |
1409 | | /*If we have more than 4095 super blocks, VP3's RLE coding might |
1410 | | overflow. |
1411 | | We could overcome this by ensuring we flip the coded/not-coded flags on |
1412 | | at least one super block in the frame, but we pick the simple solution |
1413 | | of just telling the user the stream will be incompatible instead. |
1414 | | It's unlikely the old VP3 codec would be able to decode streams at this |
1415 | | resolution in real time in the first place.*/ |
1416 | 0 | _enc->state.nsbs>4095){ |
1417 | 0 | vp3_compatible=0; |
1418 | 0 | } |
1419 | 0 | *(int *)_buf=vp3_compatible; |
1420 | 0 | return 0; |
1421 | 0 | }break; |
1422 | 0 | case TH_ENCCTL_GET_SPLEVEL_MAX:{ |
1423 | 0 | if(_enc==NULL||_buf==NULL)return TH_EFAULT; |
1424 | 0 | if(_buf_sz!=sizeof(int))return TH_EINVAL; |
1425 | 0 | *(int *)_buf=OC_SP_LEVEL_MAX; |
1426 | 0 | return 0; |
1427 | 0 | }break; |
1428 | 0 | case TH_ENCCTL_SET_SPLEVEL:{ |
1429 | 0 | int speed; |
1430 | 0 | if(_enc==NULL||_buf==NULL)return TH_EFAULT; |
1431 | 0 | if(_buf_sz!=sizeof(speed))return TH_EINVAL; |
1432 | 0 | speed=*(int *)_buf; |
1433 | 0 | if(speed<0||speed>OC_SP_LEVEL_MAX)return TH_EINVAL; |
1434 | 0 | _enc->sp_level=speed; |
1435 | 0 | return 0; |
1436 | 0 | }break; |
1437 | 0 | case TH_ENCCTL_GET_SPLEVEL:{ |
1438 | 0 | if(_enc==NULL||_buf==NULL)return TH_EFAULT; |
1439 | 0 | if(_buf_sz!=sizeof(int))return TH_EINVAL; |
1440 | 0 | *(int *)_buf=_enc->sp_level; |
1441 | 0 | return 0; |
1442 | 0 | } |
1443 | 0 | case TH_ENCCTL_SET_DUP_COUNT:{ |
1444 | 0 | int dup_count; |
1445 | 0 | if(_enc==NULL||_buf==NULL)return TH_EFAULT; |
1446 | 0 | if(_buf_sz!=sizeof(dup_count))return TH_EINVAL; |
1447 | 0 | dup_count=*(int *)_buf; |
1448 | 0 | if(dup_count>=_enc->keyframe_frequency_force)return TH_EINVAL; |
1449 | 0 | _enc->dup_count=OC_MAXI(dup_count,0); |
1450 | 0 | return 0; |
1451 | 0 | }break; |
1452 | 0 | case TH_ENCCTL_SET_QUALITY:{ |
1453 | 0 | int qi; |
1454 | 0 | if(_enc==NULL||_buf==NULL)return TH_EFAULT; |
1455 | 0 | if(_enc->state.info.target_bitrate>0)return TH_EINVAL; |
1456 | 0 | qi=*(int *)_buf; |
1457 | 0 | if(qi<0||qi>63)return TH_EINVAL; |
1458 | 0 | _enc->state.info.quality=qi; |
1459 | 0 | _enc->state.qis[0]=(unsigned char)qi; |
1460 | 0 | _enc->state.nqis=1; |
1461 | 0 | return 0; |
1462 | 0 | }break; |
1463 | 0 | case TH_ENCCTL_SET_BITRATE:{ |
1464 | 0 | long bitrate; |
1465 | 0 | int reset; |
1466 | 0 | if(_enc==NULL||_buf==NULL)return TH_EFAULT; |
1467 | 0 | bitrate=*(long *)_buf; |
1468 | 0 | if(bitrate<=0)return TH_EINVAL; |
1469 | 0 | reset=_enc->state.info.target_bitrate<=0; |
1470 | 0 | _enc->state.info.target_bitrate=bitrate>INT_MAX?INT_MAX:bitrate; |
1471 | 0 | if(reset)oc_rc_state_init(&_enc->rc,_enc); |
1472 | 0 | else oc_enc_rc_resize(_enc); |
1473 | 0 | return 0; |
1474 | 0 | }break; |
1475 | 0 | case TH_ENCCTL_SET_RATE_FLAGS:{ |
1476 | 0 | int set; |
1477 | 0 | if(_enc==NULL||_buf==NULL)return TH_EFAULT; |
1478 | 0 | if(_buf_sz!=sizeof(set))return TH_EINVAL; |
1479 | 0 | if(_enc->state.info.target_bitrate<=0)return TH_EINVAL; |
1480 | 0 | set=*(int *)_buf; |
1481 | 0 | _enc->rc.drop_frames=set&TH_RATECTL_DROP_FRAMES; |
1482 | 0 | _enc->rc.cap_overflow=set&TH_RATECTL_CAP_OVERFLOW; |
1483 | 0 | _enc->rc.cap_underflow=set&TH_RATECTL_CAP_UNDERFLOW; |
1484 | 0 | return 0; |
1485 | 0 | }break; |
1486 | 0 | case TH_ENCCTL_SET_RATE_BUFFER:{ |
1487 | 0 | int set; |
1488 | 0 | if(_enc==NULL||_buf==NULL)return TH_EFAULT; |
1489 | 0 | if(_buf_sz!=sizeof(set))return TH_EINVAL; |
1490 | 0 | if(_enc->state.info.target_bitrate<=0)return TH_EINVAL; |
1491 | 0 | set=*(int *)_buf; |
1492 | 0 | _enc->rc.buf_delay=set; |
1493 | 0 | oc_enc_rc_resize(_enc); |
1494 | 0 | *(int *)_buf=_enc->rc.buf_delay; |
1495 | 0 | return 0; |
1496 | 0 | }break; |
1497 | 0 | case TH_ENCCTL_2PASS_OUT:{ |
1498 | 0 | if(_enc==NULL||_buf==NULL)return TH_EFAULT; |
1499 | 0 | if(_enc->state.info.target_bitrate<=0|| |
1500 | 0 | _enc->state.curframe_num>=0&&_enc->rc.twopass!=1|| |
1501 | 0 | _buf_sz!=sizeof(unsigned char *)){ |
1502 | 0 | return TH_EINVAL; |
1503 | 0 | } |
1504 | 0 | return oc_enc_rc_2pass_out(_enc,(unsigned char **)_buf); |
1505 | 0 | }break; |
1506 | 0 | case TH_ENCCTL_2PASS_IN:{ |
1507 | 0 | if(_enc==NULL)return TH_EFAULT; |
1508 | 0 | if(_enc->state.info.target_bitrate<=0|| |
1509 | 0 | _enc->state.curframe_num>=0&&_enc->rc.twopass!=2){ |
1510 | 0 | return TH_EINVAL; |
1511 | 0 | } |
1512 | 0 | return oc_enc_rc_2pass_in(_enc,_buf,_buf_sz); |
1513 | 0 | }break; |
1514 | 0 | case TH_ENCCTL_SET_COMPAT_CONFIG:{ |
1515 | 0 | unsigned char buf[7]; |
1516 | 0 | oc_pack_buf opb; |
1517 | 0 | th_quant_info qinfo; |
1518 | 0 | th_huff_code huff_codes[TH_NHUFFMAN_TABLES][TH_NDCT_TOKENS]; |
1519 | 0 | int ret; |
1520 | 0 | int i; |
1521 | 0 | if(_enc==NULL||_buf==NULL)return TH_EFAULT; |
1522 | 0 | if(_enc->packet_state>OC_PACKET_SETUP_HDR)return TH_EINVAL; |
1523 | 0 | oc_pack_readinit(&opb,_buf,_buf_sz); |
1524 | | /*Validate the setup packet header.*/ |
1525 | 0 | for(i=0;i<7;i++)buf[i]=(unsigned char)oc_pack_read(&opb,8); |
1526 | 0 | if(!(buf[0]&0x80)||memcmp(buf+1,"theora",6)!=0)return TH_ENOTFORMAT; |
1527 | 0 | if(buf[0]!=0x82)return TH_EBADHEADER; |
1528 | | /*Reads its contents.*/ |
1529 | 0 | ret=oc_quant_params_unpack(&opb,&qinfo); |
1530 | 0 | if(ret<0){ |
1531 | 0 | oc_quant_params_clear(&qinfo); |
1532 | 0 | return ret; |
1533 | 0 | } |
1534 | 0 | ret=oc_huff_codes_unpack(&opb,huff_codes); |
1535 | 0 | if(ret<0){ |
1536 | 0 | oc_quant_params_clear(&qinfo); |
1537 | 0 | return ret; |
1538 | 0 | } |
1539 | | /*Install the new state.*/ |
1540 | 0 | oc_quant_params_clear(&_enc->qinfo); |
1541 | 0 | memcpy(&_enc->qinfo,&qinfo,sizeof(qinfo)); |
1542 | 0 | oc_enc_quant_params_updated(_enc,&qinfo); |
1543 | 0 | memcpy(_enc->huff_codes,huff_codes,sizeof(_enc->huff_codes)); |
1544 | 0 | return 0; |
1545 | 0 | } |
1546 | | #if defined(OC_COLLECT_METRICS) |
1547 | | case TH_ENCCTL_SET_METRICS_FILE:{ |
1548 | | OC_MODE_METRICS_FILENAME=(const char *)_buf; |
1549 | | return 0; |
1550 | | } |
1551 | | #endif |
1552 | 0 | default:return TH_EIMPL; |
1553 | 3.44k | } |
1554 | 3.44k | } |
1555 | | |
1556 | 13.7k | int th_encode_flushheader(th_enc_ctx *_enc,th_comment *_tc,ogg_packet *_op){ |
1557 | 13.7k | if(_enc==NULL)return TH_EFAULT; |
1558 | 13.7k | return oc_state_flushheader(&_enc->state,&_enc->packet_state,&_enc->opb, |
1559 | 13.7k | &_enc->qinfo,(const th_huff_table *)_enc->huff_codes,th_version_string(), |
1560 | 13.7k | _tc,_op); |
1561 | 13.7k | } |
1562 | | |
1563 | | static void oc_img_plane_copy_pad(th_img_plane *_dst,th_img_plane *_src, |
1564 | | ogg_int32_t _pic_x,ogg_int32_t _pic_y, |
1565 | 126k | ogg_int32_t _pic_width,ogg_int32_t _pic_height){ |
1566 | 126k | unsigned char *dst; |
1567 | 126k | int dstride; |
1568 | 126k | ogg_uint32_t frame_width; |
1569 | 126k | ogg_uint32_t frame_height; |
1570 | 126k | ogg_uint32_t y; |
1571 | 126k | frame_width=_dst->width; |
1572 | 126k | frame_height=_dst->height; |
1573 | | /*If we have _no_ data, just encode a dull green.*/ |
1574 | 126k | if(_pic_width==0||_pic_height==0){ |
1575 | 0 | dst=_dst->data; |
1576 | 0 | dstride=_dst->stride; |
1577 | 0 | for(y=0;y<frame_height;y++){ |
1578 | 0 | memset(dst,0,frame_width*sizeof(*dst)); |
1579 | 0 | dst+=dstride; |
1580 | 0 | } |
1581 | 0 | } |
1582 | | /*Otherwise, copy what we do have, and add our own padding.*/ |
1583 | 126k | else{ |
1584 | 126k | unsigned char *dst_data; |
1585 | 126k | unsigned char *src_data; |
1586 | 126k | unsigned char *src; |
1587 | 126k | int sstride; |
1588 | 126k | ogg_uint32_t x; |
1589 | | /*Step 1: Copy the data we do have.*/ |
1590 | 126k | dstride=_dst->stride; |
1591 | 126k | sstride=_src->stride; |
1592 | 126k | dst_data=_dst->data; |
1593 | 126k | src_data=_src->data; |
1594 | 126k | dst=dst_data+_pic_y*(ptrdiff_t)dstride+_pic_x; |
1595 | 126k | src=src_data+_pic_y*(ptrdiff_t)sstride+_pic_x; |
1596 | 10.3M | for(y=0;y<_pic_height;y++){ |
1597 | 10.2M | memcpy(dst,src,_pic_width); |
1598 | 10.2M | dst+=dstride; |
1599 | 10.2M | src+=sstride; |
1600 | 10.2M | } |
1601 | | /*Step 2: Perform a low-pass extension into the padding region.*/ |
1602 | | /*Left side.*/ |
1603 | 126k | for(x=_pic_x;x-->0;){ |
1604 | 0 | dst=dst_data+_pic_y*(ptrdiff_t)dstride+x; |
1605 | 0 | for(y=0;y<_pic_height;y++){ |
1606 | 0 | dst[0]=(dst[1]<<1)+(dst-(dstride&-(y>0)))[1] |
1607 | 0 | +(dst+(dstride&-(y+1<_pic_height)))[1]+2>>2; |
1608 | 0 | dst+=dstride; |
1609 | 0 | } |
1610 | 0 | } |
1611 | | /*Right side.*/ |
1612 | 836k | for(x=_pic_x+_pic_width;x<frame_width;x++){ |
1613 | 709k | dst=dst_data+_pic_y*(ptrdiff_t)dstride+x-1; |
1614 | 97.1M | for(y=0;y<_pic_height;y++){ |
1615 | 96.4M | dst[1]=(dst[0]<<1)+(dst-(dstride&-(y>0)))[0] |
1616 | 96.4M | +(dst+(dstride&-(y+1<_pic_height)))[0]+2>>2; |
1617 | 96.4M | dst+=dstride; |
1618 | 96.4M | } |
1619 | 709k | } |
1620 | | /*Top.*/ |
1621 | 126k | dst=dst_data+_pic_y*(ptrdiff_t)dstride; |
1622 | 781k | for(y=_pic_y;y-->0;){ |
1623 | 453M | for(x=0;x<frame_width;x++){ |
1624 | 452M | (dst-dstride)[x]=(dst[x]<<1)+dst[x-(x>0)] |
1625 | 452M | +dst[x+(x+1<frame_width)]+2>>2; |
1626 | 452M | } |
1627 | 654k | dst-=dstride; |
1628 | 654k | } |
1629 | | /*Bottom.*/ |
1630 | 126k | dst=dst_data+(_pic_y+_pic_height)*(ptrdiff_t)dstride; |
1631 | 126k | for(y=_pic_y+_pic_height;y<frame_height;y++){ |
1632 | 0 | for(x=0;x<frame_width;x++){ |
1633 | 0 | dst[x]=((dst-dstride)[x]<<1)+(dst-dstride)[x-(x>0)] |
1634 | 0 | +(dst-dstride)[x+(x+1<frame_width)]+2>>2; |
1635 | 0 | } |
1636 | 0 | dst+=dstride; |
1637 | 0 | } |
1638 | 126k | } |
1639 | 126k | } |
1640 | | |
1641 | 42.1k | int th_encode_ycbcr_in(th_enc_ctx *_enc,th_ycbcr_buffer _img){ |
1642 | 42.1k | th_ycbcr_buffer img; |
1643 | 42.1k | int frame_width; |
1644 | 42.1k | int frame_height; |
1645 | 42.1k | int pic_width; |
1646 | 42.1k | int pic_height; |
1647 | 42.1k | int pic_x; |
1648 | 42.1k | int pic_y; |
1649 | 42.1k | int cframe_width; |
1650 | 42.1k | int cframe_height; |
1651 | 42.1k | int cpic_width; |
1652 | 42.1k | int cpic_height; |
1653 | 42.1k | int cpic_x; |
1654 | 42.1k | int cpic_y; |
1655 | 42.1k | int hdec; |
1656 | 42.1k | int vdec; |
1657 | 42.1k | int pli; |
1658 | 42.1k | int refi; |
1659 | 42.1k | int drop; |
1660 | | /*Step 1: validate parameters.*/ |
1661 | 42.1k | if(_enc==NULL||_img==NULL)return TH_EFAULT; |
1662 | 42.1k | if(_enc->packet_state==OC_PACKET_DONE)return TH_EINVAL; |
1663 | 42.1k | if(_enc->rc.twopass&&_enc->rc.twopass_buffer_bytes==0)return TH_EINVAL; |
1664 | 42.1k | hdec=!(_enc->state.info.pixel_fmt&1); |
1665 | 42.1k | vdec=!(_enc->state.info.pixel_fmt&2); |
1666 | 42.1k | frame_width=_enc->state.info.frame_width; |
1667 | 42.1k | frame_height=_enc->state.info.frame_height; |
1668 | 42.1k | pic_x=_enc->state.info.pic_x; |
1669 | 42.1k | pic_y=_enc->state.info.pic_y; |
1670 | 42.1k | pic_width=_enc->state.info.pic_width; |
1671 | 42.1k | pic_height=_enc->state.info.pic_height; |
1672 | 42.1k | cframe_width=frame_width>>hdec; |
1673 | 42.1k | cframe_height=frame_height>>vdec; |
1674 | 42.1k | cpic_x=pic_x>>hdec; |
1675 | 42.1k | cpic_y=pic_y>>vdec; |
1676 | 42.1k | cpic_width=(pic_x+pic_width+hdec>>hdec)-cpic_x; |
1677 | 42.1k | cpic_height=(pic_y+pic_height+vdec>>vdec)-cpic_y; |
1678 | | /*Flip the input buffer upside down.*/ |
1679 | 42.1k | oc_ycbcr_buffer_flip(img,_img); |
1680 | 42.1k | if(img[0].width!=frame_width||img[0].height!=frame_height|| |
1681 | 42.1k | img[1].width!=cframe_width||img[2].width!=cframe_width|| |
1682 | 42.1k | img[1].height!=cframe_height||img[2].height!=cframe_height){ |
1683 | | /*The buffer does not match the frame size. |
1684 | | Check to see if it matches the picture size.*/ |
1685 | 0 | if(img[0].width!=pic_width||img[0].height!=pic_height|| |
1686 | 0 | img[1].width!=cpic_width||img[2].width!=cpic_width|| |
1687 | 0 | img[1].height!=cpic_height||img[2].height!=cpic_height){ |
1688 | | /*It doesn't; we don't know how to handle it.*/ |
1689 | 0 | return TH_EINVAL; |
1690 | 0 | } |
1691 | | /*Adjust the pointers to address a full frame. |
1692 | | We still only use the picture region, however.*/ |
1693 | 0 | img[0].data-=pic_y*(ptrdiff_t)img[0].stride+pic_x; |
1694 | 0 | img[1].data-=cpic_y*(ptrdiff_t)img[1].stride+cpic_x; |
1695 | 0 | img[2].data-=cpic_y*(ptrdiff_t)img[2].stride+cpic_x; |
1696 | 0 | } |
1697 | | /*Step 2: Update the buffer state.*/ |
1698 | 42.1k | if(_enc->state.ref_frame_idx[OC_FRAME_SELF]>=0){ |
1699 | 38.7k | _enc->state.ref_frame_idx[OC_FRAME_PREV]= |
1700 | 38.7k | _enc->state.ref_frame_idx[OC_FRAME_SELF]; |
1701 | 38.7k | _enc->state.ref_frame_data[OC_FRAME_PREV]= |
1702 | 38.7k | _enc->state.ref_frame_data[OC_FRAME_SELF]; |
1703 | 38.7k | if(_enc->state.frame_type==OC_INTRA_FRAME){ |
1704 | | /*The new frame becomes both the previous and gold reference frames.*/ |
1705 | 16.9k | _enc->state.keyframe_num=_enc->state.curframe_num; |
1706 | 16.9k | _enc->state.ref_frame_idx[OC_FRAME_GOLD]= |
1707 | 16.9k | _enc->state.ref_frame_idx[OC_FRAME_SELF]; |
1708 | 16.9k | _enc->state.ref_frame_data[OC_FRAME_GOLD]= |
1709 | 16.9k | _enc->state.ref_frame_data[OC_FRAME_SELF]; |
1710 | 16.9k | } |
1711 | 38.7k | } |
1712 | 42.1k | if(_enc->state.ref_frame_idx[OC_FRAME_IO]>=0&&_enc->prevframe_dropped==0){ |
1713 | 35.9k | _enc->state.ref_frame_idx[OC_FRAME_PREV_ORIG]= |
1714 | 35.9k | _enc->state.ref_frame_idx[OC_FRAME_IO]; |
1715 | 35.9k | _enc->state.ref_frame_data[OC_FRAME_PREV_ORIG]= |
1716 | 35.9k | _enc->state.ref_frame_data[OC_FRAME_IO]; |
1717 | 35.9k | if(_enc->state.frame_type==OC_INTRA_FRAME){ |
1718 | | /*The new input frame becomes both the previous and gold |
1719 | | original-reference frames.*/ |
1720 | 16.2k | _enc->state.ref_frame_idx[OC_FRAME_GOLD_ORIG]= |
1721 | 16.2k | _enc->state.ref_frame_idx[OC_FRAME_IO]; |
1722 | 16.2k | _enc->state.ref_frame_data[OC_FRAME_GOLD_ORIG]= |
1723 | 16.2k | _enc->state.ref_frame_data[OC_FRAME_IO]; |
1724 | 16.2k | } |
1725 | 35.9k | } |
1726 | | /*Select a free buffer to use for the incoming frame*/ |
1727 | 79.6k | for(refi=3;refi==_enc->state.ref_frame_idx[OC_FRAME_GOLD_ORIG]|| |
1728 | 79.6k | refi==_enc->state.ref_frame_idx[OC_FRAME_PREV_ORIG];refi++); |
1729 | 42.1k | _enc->state.ref_frame_idx[OC_FRAME_IO]=refi; |
1730 | 42.1k | _enc->state.ref_frame_data[OC_FRAME_IO]= |
1731 | 42.1k | _enc->state.ref_frame_bufs[refi][0].data; |
1732 | | /*Step 3: Copy the input to our internal buffer. |
1733 | | This lets us add padding, so we don't have to worry about dereferencing |
1734 | | possibly invalid addresses, and allows us to use the same strides and |
1735 | | fragment offsets for both the input frame and the reference frames.*/ |
1736 | 42.1k | oc_img_plane_copy_pad(_enc->state.ref_frame_bufs[refi]+0,img+0, |
1737 | 42.1k | pic_x,pic_y,pic_width,pic_height); |
1738 | 42.1k | oc_state_borders_fill_rows(&_enc->state,refi,0,0,frame_height); |
1739 | 42.1k | oc_state_borders_fill_caps(&_enc->state,refi,0); |
1740 | 126k | for(pli=1;pli<3;pli++){ |
1741 | 84.3k | oc_img_plane_copy_pad(_enc->state.ref_frame_bufs[refi]+pli,img+pli, |
1742 | 84.3k | cpic_x,cpic_y,cpic_width,cpic_height); |
1743 | 84.3k | oc_state_borders_fill_rows(&_enc->state,refi,pli,0,cframe_height); |
1744 | 84.3k | oc_state_borders_fill_caps(&_enc->state,refi,pli); |
1745 | 84.3k | } |
1746 | | /*Select a free buffer to use for the reconstructed version of this frame.*/ |
1747 | 79.4k | for(refi=0;refi==_enc->state.ref_frame_idx[OC_FRAME_GOLD]|| |
1748 | 79.4k | refi==_enc->state.ref_frame_idx[OC_FRAME_PREV];refi++); |
1749 | 42.1k | _enc->state.ref_frame_idx[OC_FRAME_SELF]=refi; |
1750 | 42.1k | _enc->state.ref_frame_data[OC_FRAME_SELF]= |
1751 | 42.1k | _enc->state.ref_frame_bufs[refi][0].data; |
1752 | 42.1k | _enc->state.curframe_num+=_enc->prev_dup_count+1; |
1753 | | /*Step 4: Compress the frame.*/ |
1754 | | /*Start with a keyframe, and don't allow the generation of invalid files that |
1755 | | overflow the keyframe_granule_shift.*/ |
1756 | 42.1k | if(_enc->rc.twopass_force_kf||_enc->state.curframe_num==0|| |
1757 | 42.1k | _enc->state.curframe_num-_enc->state.keyframe_num+_enc->dup_count>= |
1758 | 38.7k | _enc->keyframe_frequency_force){ |
1759 | 4.51k | oc_enc_compress_keyframe(_enc,0); |
1760 | 4.51k | drop=0; |
1761 | 4.51k | } |
1762 | 37.6k | else{ |
1763 | 37.6k | oc_enc_compress_frame(_enc,0); |
1764 | 37.6k | drop=1; |
1765 | 37.6k | } |
1766 | 42.1k | oc_restore_fpu(&_enc->state); |
1767 | | /*drop currently indicates if the frame is droppable.*/ |
1768 | 42.1k | if(_enc->state.info.target_bitrate>0){ |
1769 | 31.7k | drop=oc_enc_update_rc_state(_enc,oggpackB_bytes(&_enc->opb)<<3, |
1770 | 31.7k | _enc->state.frame_type,_enc->state.qis[0],0,drop); |
1771 | 31.7k | } |
1772 | 10.4k | else drop=0; |
1773 | | /*drop now indicates if the frame was dropped.*/ |
1774 | 42.1k | if(drop)oc_enc_drop_frame(_enc); |
1775 | 38.9k | else _enc->prevframe_dropped=0; |
1776 | 42.1k | _enc->packet_state=OC_PACKET_READY; |
1777 | 42.1k | _enc->prev_dup_count=_enc->nqueued_dups=_enc->dup_count; |
1778 | 42.1k | _enc->dup_count=0; |
1779 | | #if defined(OC_DUMP_IMAGES) |
1780 | | oc_enc_set_granpos(_enc); |
1781 | | oc_state_dump_frame(&_enc->state,OC_FRAME_IO,"src"); |
1782 | | oc_state_dump_frame(&_enc->state,OC_FRAME_SELF,"rec"); |
1783 | | #endif |
1784 | 42.1k | return 0; |
1785 | 42.1k | } |
1786 | | |
1787 | 45.6k | int th_encode_packetout(th_enc_ctx *_enc,int _last_p,ogg_packet *_op){ |
1788 | 45.6k | unsigned char *packet; |
1789 | 45.6k | if(_enc==NULL||_op==NULL)return TH_EFAULT; |
1790 | 45.6k | if(_enc->packet_state==OC_PACKET_READY){ |
1791 | 42.1k | _enc->packet_state=OC_PACKET_EMPTY; |
1792 | 42.1k | if(_enc->rc.twopass!=1){ |
1793 | 42.1k | packet=oggpackB_get_buffer(&_enc->opb); |
1794 | | /*If there's no packet, malloc failed while writing; it's lost forever.*/ |
1795 | 42.1k | if(packet==NULL)return TH_EFAULT; |
1796 | 42.1k | _op->packet=packet; |
1797 | 42.1k | _op->bytes=oggpackB_bytes(&_enc->opb); |
1798 | 42.1k | } |
1799 | | /*For the first pass in 2-pass mode, don't emit any packet data.*/ |
1800 | 0 | else{ |
1801 | 0 | _op->packet=NULL; |
1802 | 0 | _op->bytes=0; |
1803 | 0 | } |
1804 | 42.1k | } |
1805 | 3.44k | else if(_enc->packet_state==OC_PACKET_EMPTY){ |
1806 | 3.44k | if(_enc->nqueued_dups>0){ |
1807 | 0 | _enc->nqueued_dups--; |
1808 | | /*Emit an inter frame with no coded blocks in VP3-compatibility mode.*/ |
1809 | 0 | if(_enc->vp3_compatible){ |
1810 | 0 | oggpackB_reset(&_enc->opb); |
1811 | 0 | oc_enc_drop_frame_pack(_enc); |
1812 | 0 | packet=oggpackB_get_buffer(&_enc->opb); |
1813 | | /*If there's no packet, malloc failed while writing; it's lost |
1814 | | forever.*/ |
1815 | 0 | if(packet==NULL)return TH_EFAULT; |
1816 | 0 | _op->packet=packet; |
1817 | 0 | _op->bytes=oggpackB_bytes(&_enc->opb); |
1818 | 0 | } |
1819 | | /*Otherwise emit a 0-byte packet.*/ |
1820 | 0 | else{ |
1821 | 0 | _op->packet=NULL; |
1822 | 0 | _op->bytes=0; |
1823 | 0 | } |
1824 | 0 | } |
1825 | 3.44k | else{ |
1826 | 3.44k | if(_last_p)_enc->packet_state=OC_PACKET_DONE; |
1827 | 3.44k | return 0; |
1828 | 3.44k | } |
1829 | 3.44k | } |
1830 | 0 | else return 0; |
1831 | 42.1k | _last_p=_last_p&&_enc->nqueued_dups<=0; |
1832 | 42.1k | _op->b_o_s=0; |
1833 | 42.1k | _op->e_o_s=_last_p; |
1834 | 42.1k | oc_enc_set_granpos(_enc); |
1835 | 42.1k | _op->packetno=th_granule_frame(_enc,_enc->state.granpos)+3; |
1836 | 42.1k | _op->granulepos=_enc->state.granpos; |
1837 | 42.1k | if(_last_p)_enc->packet_state=OC_PACKET_DONE; |
1838 | 42.1k | return 1+_enc->nqueued_dups; |
1839 | 45.6k | } |