Coverage Report

Created: 2025-11-16 07:20

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