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