Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libtheora/encint.h
9903 views
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
#if !defined(_encint_H)
17
# define _encint_H (1)
18
# include "theora/theoraenc.h"
19
# include "state.h"
20
# include "mathops.h"
21
# include "enquant.h"
22
# include "huffenc.h"
23
/*# define OC_COLLECT_METRICS*/
24
25
26
27
typedef oc_mv oc_mv2[2];
28
29
typedef struct oc_enc_opt_vtable oc_enc_opt_vtable;
30
typedef struct oc_enc_opt_data oc_enc_opt_data;
31
typedef struct oc_mb_enc_info oc_mb_enc_info;
32
typedef struct oc_mode_scheme_chooser oc_mode_scheme_chooser;
33
typedef struct oc_fr_state oc_fr_state;
34
typedef struct oc_qii_state oc_qii_state;
35
typedef struct oc_enc_pipeline_state oc_enc_pipeline_state;
36
typedef struct oc_mode_rd oc_mode_rd;
37
typedef struct oc_iir_filter oc_iir_filter;
38
typedef struct oc_frame_metrics oc_frame_metrics;
39
typedef struct oc_rc_state oc_rc_state;
40
typedef struct th_enc_ctx oc_enc_ctx;
41
typedef struct oc_token_checkpoint oc_token_checkpoint;
42
43
44
45
/*Encoder-specific accelerated functions.*/
46
# if defined(OC_X86_ASM)
47
# if defined(_MSC_VER)
48
# include "x86_vc/x86enc.h"
49
# else
50
# include "x86/x86enc.h"
51
# endif
52
# endif
53
# if defined(OC_ARM_ASM)
54
# include "arm/armenc.h"
55
# endif
56
57
# if !defined(oc_enc_accel_init)
58
# define oc_enc_accel_init oc_enc_accel_init_c
59
# endif
60
# if defined(OC_ENC_USE_VTABLE)
61
# if !defined(oc_enc_frag_sub)
62
# define oc_enc_frag_sub(_enc,_diff,_src,_ref,_ystride) \
63
((*(_enc)->opt_vtable.frag_sub)(_diff,_src,_ref,_ystride))
64
# endif
65
# if !defined(oc_enc_frag_sub_128)
66
# define oc_enc_frag_sub_128(_enc,_diff,_src,_ystride) \
67
((*(_enc)->opt_vtable.frag_sub_128)(_diff,_src,_ystride))
68
# endif
69
# if !defined(oc_enc_frag_sad)
70
# define oc_enc_frag_sad(_enc,_src,_ref,_ystride) \
71
((*(_enc)->opt_vtable.frag_sad)(_src,_ref,_ystride))
72
# endif
73
# if !defined(oc_enc_frag_sad_thresh)
74
# define oc_enc_frag_sad_thresh(_enc,_src,_ref,_ystride,_thresh) \
75
((*(_enc)->opt_vtable.frag_sad_thresh)(_src,_ref,_ystride,_thresh))
76
# endif
77
# if !defined(oc_enc_frag_sad2_thresh)
78
# define oc_enc_frag_sad2_thresh(_enc,_src,_ref1,_ref2,_ystride,_thresh) \
79
((*(_enc)->opt_vtable.frag_sad2_thresh)(_src,_ref1,_ref2,_ystride,_thresh))
80
# endif
81
# if !defined(oc_enc_frag_intra_sad)
82
# define oc_enc_frag_intra_sad(_enc,_src,_ystride) \
83
((*(_enc)->opt_vtable.frag_intra_sad)(_src,_ystride))
84
# endif
85
# if !defined(oc_enc_frag_satd)
86
# define oc_enc_frag_satd(_enc,_dc,_src,_ref,_ystride) \
87
((*(_enc)->opt_vtable.frag_satd)(_dc,_src,_ref,_ystride))
88
# endif
89
# if !defined(oc_enc_frag_satd2)
90
# define oc_enc_frag_satd2(_enc,_dc,_src,_ref1,_ref2,_ystride) \
91
((*(_enc)->opt_vtable.frag_satd2)(_dc,_src,_ref1,_ref2,_ystride))
92
# endif
93
# if !defined(oc_enc_frag_intra_satd)
94
# define oc_enc_frag_intra_satd(_enc,_dc,_src,_ystride) \
95
((*(_enc)->opt_vtable.frag_intra_satd)(_dc,_src,_ystride))
96
# endif
97
# if !defined(oc_enc_frag_ssd)
98
# define oc_enc_frag_ssd(_enc,_src,_ref,_ystride) \
99
((*(_enc)->opt_vtable.frag_ssd)(_src,_ref,_ystride))
100
# endif
101
# if !defined(oc_enc_frag_border_ssd)
102
# define oc_enc_frag_border_ssd(_enc,_src,_ref,_ystride,_mask) \
103
((*(_enc)->opt_vtable.frag_border_ssd)(_src,_ref,_ystride,_mask))
104
# endif
105
# if !defined(oc_enc_frag_copy2)
106
# define oc_enc_frag_copy2(_enc,_dst,_src1,_src2,_ystride) \
107
((*(_enc)->opt_vtable.frag_copy2)(_dst,_src1,_src2,_ystride))
108
# endif
109
# if !defined(oc_enc_enquant_table_init)
110
# define oc_enc_enquant_table_init(_enc,_enquant,_dequant) \
111
((*(_enc)->opt_vtable.enquant_table_init)(_enquant,_dequant))
112
# endif
113
# if !defined(oc_enc_enquant_table_fixup)
114
# define oc_enc_enquant_table_fixup(_enc,_enquant,_nqis) \
115
((*(_enc)->opt_vtable.enquant_table_fixup)(_enquant,_nqis))
116
# endif
117
# if !defined(oc_enc_quantize)
118
# define oc_enc_quantize(_enc,_qdct,_dct,_dequant,_enquant) \
119
((*(_enc)->opt_vtable.quantize)(_qdct,_dct,_dequant,_enquant))
120
# endif
121
# if !defined(oc_enc_frag_recon_intra)
122
# define oc_enc_frag_recon_intra(_enc,_dst,_ystride,_residue) \
123
((*(_enc)->opt_vtable.frag_recon_intra)(_dst,_ystride,_residue))
124
# endif
125
# if !defined(oc_enc_frag_recon_inter)
126
# define oc_enc_frag_recon_inter(_enc,_dst,_src,_ystride,_residue) \
127
((*(_enc)->opt_vtable.frag_recon_inter)(_dst,_src,_ystride,_residue))
128
# endif
129
# if !defined(oc_enc_fdct8x8)
130
# define oc_enc_fdct8x8(_enc,_y,_x) \
131
((*(_enc)->opt_vtable.fdct8x8)(_y,_x))
132
# endif
133
# else
134
# if !defined(oc_enc_frag_sub)
135
# define oc_enc_frag_sub(_enc,_diff,_src,_ref,_ystride) \
136
oc_enc_frag_sub_c(_diff,_src,_ref,_ystride)
137
# endif
138
# if !defined(oc_enc_frag_sub_128)
139
# define oc_enc_frag_sub_128(_enc,_diff,_src,_ystride) \
140
oc_enc_frag_sub_128_c(_diff,_src,_ystride)
141
# endif
142
# if !defined(oc_enc_frag_sad)
143
# define oc_enc_frag_sad(_enc,_src,_ref,_ystride) \
144
oc_enc_frag_sad_c(_src,_ref,_ystride)
145
# endif
146
# if !defined(oc_enc_frag_sad_thresh)
147
# define oc_enc_frag_sad_thresh(_enc,_src,_ref,_ystride,_thresh) \
148
oc_enc_frag_sad_thresh_c(_src,_ref,_ystride,_thresh)
149
# endif
150
# if !defined(oc_enc_frag_sad2_thresh)
151
# define oc_enc_frag_sad2_thresh(_enc,_src,_ref1,_ref2,_ystride,_thresh) \
152
oc_enc_frag_sad2_thresh_c(_src,_ref1,_ref2,_ystride,_thresh)
153
# endif
154
# if !defined(oc_enc_frag_intra_sad)
155
# define oc_enc_frag_intra_sad(_enc,_src,_ystride) \
156
oc_enc_frag_intra_sad_c(_src,_ystride)
157
# endif
158
# if !defined(oc_enc_frag_satd)
159
# define oc_enc_frag_satd(_enc,_dc,_src,_ref,_ystride) \
160
oc_enc_frag_satd_c(_dc,_src,_ref,_ystride)
161
# endif
162
# if !defined(oc_enc_frag_satd2)
163
# define oc_enc_frag_satd2(_enc,_dc,_src,_ref1,_ref2,_ystride) \
164
oc_enc_frag_satd2_c(_dc,_src,_ref1,_ref2,_ystride)
165
# endif
166
# if !defined(oc_enc_frag_intra_satd)
167
# define oc_enc_frag_intra_satd(_enc,_dc,_src,_ystride) \
168
oc_enc_frag_intra_satd_c(_dc,_src,_ystride)
169
# endif
170
# if !defined(oc_enc_frag_ssd)
171
# define oc_enc_frag_ssd(_enc,_src,_ref,_ystride) \
172
oc_enc_frag_ssd_c(_src,_ref,_ystride)
173
# endif
174
# if !defined(oc_enc_frag_border_ssd)
175
# define oc_enc_frag_border_ssd(_enc,_src,_ref,_ystride,_mask) \
176
oc_enc_frag_border_ssd_c(_src,_ref,_ystride,_mask)
177
# endif
178
# if !defined(oc_enc_frag_copy2)
179
# define oc_enc_frag_copy2(_enc,_dst,_src1,_src2,_ystride) \
180
oc_enc_frag_copy2_c(_dst,_src1,_src2,_ystride)
181
# endif
182
# if !defined(oc_enc_enquant_table_init)
183
# define oc_enc_enquant_table_init(_enc,_enquant,_dequant) \
184
oc_enc_enquant_table_init_c(_enquant,_dequant)
185
# endif
186
# if !defined(oc_enc_enquant_table_fixup)
187
# define oc_enc_enquant_table_fixup(_enc,_enquant,_nqis) \
188
oc_enc_enquant_table_fixup_c(_enquant,_nqis)
189
# endif
190
# if !defined(oc_enc_quantize)
191
# define oc_enc_quantize(_enc,_qdct,_dct,_dequant,_enquant) \
192
oc_enc_quantize_c(_qdct,_dct,_dequant,_enquant)
193
# endif
194
# if !defined(oc_enc_frag_recon_intra)
195
# define oc_enc_frag_recon_intra(_enc,_dst,_ystride,_residue) \
196
oc_frag_recon_intra_c(_dst,_ystride,_residue)
197
# endif
198
# if !defined(oc_enc_frag_recon_inter)
199
# define oc_enc_frag_recon_inter(_enc,_dst,_src,_ystride,_residue) \
200
oc_frag_recon_inter_c(_dst,_src,_ystride,_residue)
201
# endif
202
# if !defined(oc_enc_fdct8x8)
203
# define oc_enc_fdct8x8(_enc,_y,_x) oc_enc_fdct8x8_c(_y,_x)
204
# endif
205
# endif
206
207
208
209
/*Constants for the packet-out state machine specific to the encoder.*/
210
211
/*Next packet to emit: Data packet, but none are ready yet.*/
212
#define OC_PACKET_EMPTY (0)
213
/*Next packet to emit: Data packet, and one is ready.*/
214
#define OC_PACKET_READY (1)
215
216
/*All features enabled.*/
217
#define OC_SP_LEVEL_SLOW (0)
218
/*Enable early skip.*/
219
#define OC_SP_LEVEL_EARLY_SKIP (1)
220
/*Use analysis shortcuts, single quantizer, and faster tokenization.*/
221
#define OC_SP_LEVEL_FAST_ANALYSIS (2)
222
/*Use SAD instead of SATD*/
223
#define OC_SP_LEVEL_NOSATD (3)
224
/*Disable motion compensation.*/
225
#define OC_SP_LEVEL_NOMC (4)
226
/*Maximum valid speed level.*/
227
#define OC_SP_LEVEL_MAX (4)
228
229
230
/*The number of extra bits of precision at which to store rate metrics.*/
231
# define OC_BIT_SCALE (6)
232
/*The number of extra bits of precision at which to store RMSE metrics.
233
This must be at least half OC_BIT_SCALE (rounded up).*/
234
# define OC_RMSE_SCALE (5)
235
/*The number of quantizer bins to partition statistics into.*/
236
# define OC_LOGQ_BINS (8)
237
/*The number of SAD/SATD bins to partition statistics into.*/
238
# define OC_COMP_BINS (24)
239
/*The number of bits of precision to drop from SAD and SATD scores
240
to assign them to a bin.*/
241
# define OC_SAD_SHIFT (6)
242
# define OC_SATD_SHIFT (9)
243
244
/*Masking is applied by scaling the D used in R-D optimization (via rd_scale)
245
or the lambda parameter (via rd_iscale).
246
These are only equivalent within a single block; when more than one block is
247
being considered, the former is the interpretation used.*/
248
249
/*This must be at least 4 for OC_RD_SKIP_SCALE() to work below.*/
250
# define OC_RD_SCALE_BITS (12-OC_BIT_SCALE)
251
# define OC_RD_ISCALE_BITS (11)
252
253
/*This macro is applied to _ssd values with just 4 bits of headroom
254
((15-OC_RMSE_SCALE)*2+OC_BIT_SCALE+2); since we want to allow rd_scales as
255
large as 16, and need additional fractional bits, our only recourse that
256
doesn't lose precision on blocks with very small SSDs is to use a wider
257
multiply.*/
258
# if LONG_MAX>2147483647
259
# define OC_RD_SCALE(_ssd,_rd_scale) \
260
((unsigned)((unsigned long)(_ssd)*(_rd_scale) \
261
+((1<<OC_RD_SCALE_BITS)>>1)>>OC_RD_SCALE_BITS))
262
# else
263
# define OC_RD_SCALE(_ssd,_rd_scale) \
264
(((_ssd)>>OC_RD_SCALE_BITS)*(_rd_scale) \
265
+(((_ssd)&(1<<OC_RD_SCALE_BITS)-1)*(_rd_scale) \
266
+((1<<OC_RD_SCALE_BITS)>>1)>>OC_RD_SCALE_BITS))
267
# endif
268
# define OC_RD_SKIP_SCALE(_ssd,_rd_scale) \
269
((_ssd)*(_rd_scale)+((1<<OC_RD_SCALE_BITS-4)>>1)>>OC_RD_SCALE_BITS-4)
270
# define OC_RD_ISCALE(_lambda,_rd_iscale) \
271
((_lambda)*(_rd_iscale)+((1<<OC_RD_ISCALE_BITS)>>1)>>OC_RD_ISCALE_BITS)
272
273
274
/*The bits used for each of the MB mode codebooks.*/
275
extern const unsigned char OC_MODE_BITS[2][OC_NMODES];
276
277
/*The bits used for each of the MV codebooks.*/
278
extern const unsigned char OC_MV_BITS[2][64];
279
280
/*The minimum value that can be stored in a SB run for each codeword.
281
The last entry is the upper bound on the length of a single SB run.*/
282
extern const ogg_uint16_t OC_SB_RUN_VAL_MIN[8];
283
/*The bits used for each SB run codeword.*/
284
extern const unsigned char OC_SB_RUN_CODE_NBITS[7];
285
286
/*The bits used for each block run length (starting with 1).*/
287
extern const unsigned char OC_BLOCK_RUN_CODE_NBITS[30];
288
289
290
291
/*Encoder specific functions with accelerated variants.*/
292
struct oc_enc_opt_vtable{
293
void (*frag_sub)(ogg_int16_t _diff[64],const unsigned char *_src,
294
const unsigned char *_ref,int _ystride);
295
void (*frag_sub_128)(ogg_int16_t _diff[64],
296
const unsigned char *_src,int _ystride);
297
unsigned (*frag_sad)(const unsigned char *_src,
298
const unsigned char *_ref,int _ystride);
299
unsigned (*frag_sad_thresh)(const unsigned char *_src,
300
const unsigned char *_ref,int _ystride,unsigned _thresh);
301
unsigned (*frag_sad2_thresh)(const unsigned char *_src,
302
const unsigned char *_ref1,const unsigned char *_ref2,int _ystride,
303
unsigned _thresh);
304
unsigned (*frag_intra_sad)(const unsigned char *_src,int _ystride);
305
unsigned (*frag_satd)(int *_dc,const unsigned char *_src,
306
const unsigned char *_ref,int _ystride);
307
unsigned (*frag_satd2)(int *_dc,const unsigned char *_src,
308
const unsigned char *_ref1,const unsigned char *_ref2,int _ystride);
309
unsigned (*frag_intra_satd)(int *_dc,const unsigned char *_src,int _ystride);
310
unsigned (*frag_ssd)(const unsigned char *_src,
311
const unsigned char *_ref,int _ystride);
312
unsigned (*frag_border_ssd)(const unsigned char *_src,
313
const unsigned char *_ref,int _ystride,ogg_int64_t _mask);
314
void (*frag_copy2)(unsigned char *_dst,
315
const unsigned char *_src1,const unsigned char *_src2,int _ystride);
316
void (*enquant_table_init)(void *_enquant,
317
const ogg_uint16_t _dequant[64]);
318
void (*enquant_table_fixup)(void *_enquant[3][3][2],int _nqis);
319
int (*quantize)(ogg_int16_t _qdct[64],const ogg_int16_t _dct[64],
320
const ogg_uint16_t _dequant[64],const void *_enquant);
321
void (*frag_recon_intra)(unsigned char *_dst,int _ystride,
322
const ogg_int16_t _residue[64]);
323
void (*frag_recon_inter)(unsigned char *_dst,
324
const unsigned char *_src,int _ystride,const ogg_int16_t _residue[64]);
325
void (*fdct8x8)(ogg_int16_t _y[64],const ogg_int16_t _x[64]);
326
};
327
328
329
/*Encoder specific data that varies according to which variants of the above
330
functions are used.*/
331
struct oc_enc_opt_data{
332
/*The size of a single quantizer table.
333
This must be a multiple of enquant_table_alignment.*/
334
size_t enquant_table_size;
335
/*The alignment required for the quantizer tables.
336
This must be a positive power of two.*/
337
int enquant_table_alignment;
338
};
339
340
341
void oc_enc_accel_init(oc_enc_ctx *_enc);
342
343
344
345
/*Encoder-specific macroblock information.*/
346
struct oc_mb_enc_info{
347
/*Neighboring macro blocks that have MVs available from the current frame.*/
348
unsigned cneighbors[4];
349
/*Neighboring macro blocks to use for MVs from the previous frame.*/
350
unsigned pneighbors[4];
351
/*The number of current-frame neighbors.*/
352
unsigned char ncneighbors;
353
/*The number of previous-frame neighbors.*/
354
unsigned char npneighbors;
355
/*Flags indicating which MB modes have been refined.*/
356
unsigned char refined;
357
/*Motion vectors for a macro block for the current frame and the
358
previous two frames.
359
Each is a set of 2 vectors against OC_FRAME_GOLD and OC_FRAME_PREV, which
360
can be used to estimate constant velocity and constant acceleration
361
predictors.
362
Uninitialized MVs are (0,0).*/
363
oc_mv2 analysis_mv[3];
364
/*Current unrefined analysis MVs.*/
365
oc_mv unref_mv[2];
366
/*Unrefined block MVs.*/
367
oc_mv block_mv[4];
368
/*Refined block MVs.*/
369
oc_mv ref_mv[4];
370
/*Minimum motion estimation error from the analysis stage.*/
371
ogg_uint16_t error[2];
372
/*MB error for half-pel refinement for each frame type.*/
373
unsigned satd[2];
374
/*Block error for half-pel refinement.*/
375
unsigned block_satd[4];
376
};
377
378
379
380
/*State machine to estimate the opportunity cost of coding a MB mode.*/
381
struct oc_mode_scheme_chooser{
382
/*Pointers to the a list containing the index of each mode in the mode
383
alphabet used by each scheme.
384
The first entry points to the dynamic scheme0_ranks, while the remaining 7
385
point to the constant entries stored in OC_MODE_SCHEMES.*/
386
const unsigned char *mode_ranks[8];
387
/*The ranks for each mode when coded with scheme 0.
388
These are optimized so that the more frequent modes have lower ranks.*/
389
unsigned char scheme0_ranks[OC_NMODES];
390
/*The list of modes, sorted in descending order of frequency, that
391
corresponds to the ranks above.*/
392
unsigned char scheme0_list[OC_NMODES];
393
/*The number of times each mode has been chosen so far.*/
394
unsigned mode_counts[OC_NMODES];
395
/*The list of mode coding schemes, sorted in ascending order of bit cost.*/
396
unsigned char scheme_list[8];
397
/*The number of bits used by each mode coding scheme.*/
398
ptrdiff_t scheme_bits[8];
399
};
400
401
402
void oc_mode_scheme_chooser_init(oc_mode_scheme_chooser *_chooser);
403
404
405
406
/*State to track coded block flags and their bit cost.
407
We use opportunity cost to measure the bits required to code or skip the next
408
block, using the cheaper of the cost to code it fully or partially, so long
409
as both are possible.*/
410
struct oc_fr_state{
411
/*The number of bits required for the coded block flags so far this frame.*/
412
ptrdiff_t bits;
413
/*The length of the current run for the partial super block flag, not
414
including the current super block.*/
415
unsigned sb_partial_count:16;
416
/*The length of the current run for the full super block flag, not
417
including the current super block.*/
418
unsigned sb_full_count:16;
419
/*The length of the coded block flag run when the current super block
420
started.*/
421
unsigned b_coded_count_prev:6;
422
/*The coded block flag when the current super block started.*/
423
signed int b_coded_prev:2;
424
/*The length of the current coded block flag run.*/
425
unsigned b_coded_count:6;
426
/*The current coded block flag.*/
427
signed int b_coded:2;
428
/*The number of blocks processed in the current super block.*/
429
unsigned b_count:5;
430
/*Whether or not it is cheaper to code the current super block partially,
431
even if it could still be coded fully.*/
432
unsigned sb_prefer_partial:1;
433
/*Whether the last super block was coded partially.*/
434
signed int sb_partial:2;
435
/*The number of bits required for the flags for the current super block.*/
436
unsigned sb_bits:6;
437
/*Whether the last non-partial super block was coded fully.*/
438
signed int sb_full:2;
439
};
440
441
442
443
struct oc_qii_state{
444
ptrdiff_t bits;
445
unsigned qi01_count:14;
446
signed int qi01:2;
447
unsigned qi12_count:14;
448
signed int qi12:2;
449
};
450
451
452
453
/*Temporary encoder state for the analysis pipeline.*/
454
struct oc_enc_pipeline_state{
455
/*DCT coefficient storage.
456
This is kept off the stack because a) gcc can't align things on the stack
457
reliably on ARM, and b) it avoids (unintentional) data hazards between
458
ARM and NEON code.*/
459
OC_ALIGN16(ogg_int16_t dct_data[64*3]);
460
OC_ALIGN16(signed char bounding_values[256]);
461
oc_fr_state fr[3];
462
oc_qii_state qs[3];
463
/*Skip SSD storage for the current MCU in each plane.*/
464
unsigned *skip_ssd[3];
465
/*Coded/uncoded fragment lists for each plane for the current MCU.*/
466
ptrdiff_t *coded_fragis[3];
467
ptrdiff_t *uncoded_fragis[3];
468
ptrdiff_t ncoded_fragis[3];
469
ptrdiff_t nuncoded_fragis[3];
470
/*The starting fragment for the current MCU in each plane.*/
471
ptrdiff_t froffset[3];
472
/*The starting row for the current MCU in each plane.*/
473
int fragy0[3];
474
/*The ending row for the current MCU in each plane.*/
475
int fragy_end[3];
476
/*The starting superblock for the current MCU in each plane.*/
477
unsigned sbi0[3];
478
/*The ending superblock for the current MCU in each plane.*/
479
unsigned sbi_end[3];
480
/*The number of tokens for zzi=1 for each color plane.*/
481
int ndct_tokens1[3];
482
/*The outstanding eob_run count for zzi=1 for each color plane.*/
483
int eob_run1[3];
484
/*Whether or not the loop filter is enabled.*/
485
int loop_filter;
486
};
487
488
489
490
/*Statistics used to estimate R-D cost of a block in a given coding mode.
491
See modedec.h for more details.*/
492
struct oc_mode_rd{
493
/*The expected bits used by the DCT tokens, shifted by OC_BIT_SCALE.*/
494
ogg_int16_t rate;
495
/*The expected square root of the sum of squared errors, shifted by
496
OC_RMSE_SCALE.*/
497
ogg_int16_t rmse;
498
};
499
500
# if defined(OC_COLLECT_METRICS)
501
# include "collect.h"
502
# endif
503
504
505
506
/*A 2nd order low-pass Bessel follower.
507
We use this for rate control because it has fast reaction time, but is
508
critically damped.*/
509
struct oc_iir_filter{
510
ogg_int32_t c[2];
511
ogg_int64_t g;
512
ogg_int32_t x[2];
513
ogg_int32_t y[2];
514
};
515
516
517
518
/*The 2-pass metrics associated with a single frame.*/
519
struct oc_frame_metrics{
520
/*The log base 2 of the scale factor for this frame in Q24 format.*/
521
ogg_int32_t log_scale;
522
/*The number of application-requested duplicates of this frame.*/
523
unsigned dup_count:31;
524
/*The frame type from pass 1.*/
525
unsigned frame_type:1;
526
/*The frame activity average from pass 1.*/
527
unsigned activity_avg;
528
};
529
530
531
532
/*Rate control state information.*/
533
struct oc_rc_state{
534
/*The target average bits per frame.*/
535
ogg_int64_t bits_per_frame;
536
/*The current buffer fullness (bits available to be used).*/
537
ogg_int64_t fullness;
538
/*The target buffer fullness.
539
This is where we'd like to be by the last keyframe the appears in the next
540
buf_delay frames.*/
541
ogg_int64_t target;
542
/*The maximum buffer fullness (total size of the buffer).*/
543
ogg_int64_t max;
544
/*The log of the number of pixels in a frame in Q57 format.*/
545
ogg_int64_t log_npixels;
546
/*The exponent used in the rate model in Q8 format.*/
547
unsigned exp[2];
548
/*The number of frames to distribute the buffer usage over.*/
549
int buf_delay;
550
/*The total drop count from the previous frame.
551
This includes duplicates explicitly requested via the
552
TH_ENCCTL_SET_DUP_COUNT API as well as frames we chose to drop ourselves.*/
553
ogg_uint32_t prev_drop_count;
554
/*The log of an estimated scale factor used to obtain the real framerate, for
555
VFR sources or, e.g., 12 fps content doubled to 24 fps, etc.*/
556
ogg_int64_t log_drop_scale;
557
/*The log of estimated scale factor for the rate model in Q57 format.*/
558
ogg_int64_t log_scale[2];
559
/*The log of the target quantizer level in Q57 format.*/
560
ogg_int64_t log_qtarget;
561
/*Will we drop frames to meet bitrate target?*/
562
unsigned char drop_frames;
563
/*Do we respect the maximum buffer fullness?*/
564
unsigned char cap_overflow;
565
/*Can the reservoir go negative?*/
566
unsigned char cap_underflow;
567
/*Second-order lowpass filters to track scale and VFR.*/
568
oc_iir_filter scalefilter[2];
569
int inter_count;
570
int inter_delay;
571
int inter_delay_target;
572
oc_iir_filter vfrfilter;
573
/*Two-pass mode state.
574
0 => 1-pass encoding.
575
1 => 1st pass of 2-pass encoding.
576
2 => 2nd pass of 2-pass encoding.*/
577
int twopass;
578
/*Buffer for current frame metrics.*/
579
unsigned char twopass_buffer[48];
580
/*The number of bytes in the frame metrics buffer.
581
When 2-pass encoding is enabled, this is set to 0 after each frame is
582
submitted, and must be non-zero before the next frame will be accepted.*/
583
int twopass_buffer_bytes;
584
int twopass_buffer_fill;
585
/*Whether or not to force the next frame to be a keyframe.*/
586
unsigned char twopass_force_kf;
587
/*The metrics for the previous frame.*/
588
oc_frame_metrics prev_metrics;
589
/*The metrics for the current frame.*/
590
oc_frame_metrics cur_metrics;
591
/*The buffered metrics for future frames.*/
592
oc_frame_metrics *frame_metrics;
593
int nframe_metrics;
594
int cframe_metrics;
595
/*The index of the current frame in the circular metric buffer.*/
596
int frame_metrics_head;
597
/*The frame count of each type (keyframes, delta frames, and dup frames);
598
32 bits limits us to 2.268 years at 60 fps.*/
599
ogg_uint32_t frames_total[3];
600
/*The number of frames of each type yet to be processed.*/
601
ogg_uint32_t frames_left[3];
602
/*The sum of the scale values for each frame type.*/
603
ogg_int64_t scale_sum[2];
604
/*The start of the window over which the current scale sums are taken.*/
605
int scale_window0;
606
/*The end of the window over which the current scale sums are taken.*/
607
int scale_window_end;
608
/*The frame count of each type in the current 2-pass window; this does not
609
include dup frames.*/
610
int nframes[3];
611
/*The total accumulated estimation bias.*/
612
ogg_int64_t rate_bias;
613
};
614
615
616
void oc_rc_state_init(oc_rc_state *_rc,oc_enc_ctx *_enc);
617
void oc_rc_state_clear(oc_rc_state *_rc);
618
619
void oc_enc_rc_resize(oc_enc_ctx *_enc);
620
int oc_enc_select_qi(oc_enc_ctx *_enc,int _qti,int _clamp);
621
void oc_enc_calc_lambda(oc_enc_ctx *_enc,int _frame_type);
622
int oc_enc_update_rc_state(oc_enc_ctx *_enc,
623
long _bits,int _qti,int _qi,int _trial,int _droppable);
624
int oc_enc_rc_2pass_out(oc_enc_ctx *_enc,unsigned char **_buf);
625
int oc_enc_rc_2pass_in(oc_enc_ctx *_enc,unsigned char *_buf,size_t _bytes);
626
627
628
629
/*The internal encoder state.*/
630
struct th_enc_ctx{
631
/*Shared encoder/decoder state.*/
632
oc_theora_state state;
633
/*Buffer in which to assemble packets.*/
634
oggpack_buffer opb;
635
/*Encoder-specific macroblock information.*/
636
oc_mb_enc_info *mb_info;
637
/*DC coefficients after prediction.*/
638
ogg_int16_t *frag_dc;
639
/*The list of coded macro blocks, in coded order.*/
640
unsigned *coded_mbis;
641
/*The number of coded macro blocks.*/
642
size_t ncoded_mbis;
643
/*Whether or not packets are ready to be emitted.
644
This takes on negative values while there are remaining header packets to
645
be emitted, reaches 0 when the codec is ready for input, and becomes
646
positive when a frame has been processed and data packets are ready.*/
647
int packet_state;
648
/*The maximum distance between keyframes.*/
649
ogg_uint32_t keyframe_frequency_force;
650
/*The number of duplicates to produce for the next frame.*/
651
ogg_uint32_t dup_count;
652
/*The number of duplicates remaining to be emitted for the current frame.*/
653
ogg_uint32_t nqueued_dups;
654
/*The number of duplicates emitted for the last frame.*/
655
ogg_uint32_t prev_dup_count;
656
/*The current speed level.*/
657
int sp_level;
658
/*Whether or not VP3 compatibility mode has been enabled.*/
659
unsigned char vp3_compatible;
660
/*Whether or not any INTER frames have been coded.*/
661
unsigned char coded_inter_frame;
662
/*Whether or not previous frame was dropped.*/
663
unsigned char prevframe_dropped;
664
/*Stores most recently chosen Huffman tables for each frame type, DC and AC
665
coefficients, and luma and chroma tokens.
666
The actual Huffman table used for a given coefficient depends not only on
667
the choice made here, but also its index in the zig-zag ordering.*/
668
unsigned char huff_idxs[2][2][2];
669
/*Current count of bits used by each MV coding mode.*/
670
size_t mv_bits[2];
671
/*The mode scheme chooser for estimating mode coding costs.*/
672
oc_mode_scheme_chooser chooser;
673
/*Temporary encoder state for the analysis pipeline.*/
674
oc_enc_pipeline_state pipe;
675
/*The number of vertical super blocks in an MCU.*/
676
int mcu_nvsbs;
677
/*The SSD error for skipping each fragment in the current MCU.*/
678
unsigned *mcu_skip_ssd;
679
/*The masking scale factors for chroma blocks in the current MCU.*/
680
ogg_uint16_t *mcu_rd_scale;
681
ogg_uint16_t *mcu_rd_iscale;
682
/*The DCT token lists for each coefficient and each plane.*/
683
unsigned char **dct_tokens[3];
684
/*The extra bits associated with each DCT token.*/
685
ogg_uint16_t **extra_bits[3];
686
/*The number of DCT tokens for each coefficient for each plane.*/
687
ptrdiff_t ndct_tokens[3][64];
688
/*Pending EOB runs for each coefficient for each plane.*/
689
ogg_uint16_t eob_run[3][64];
690
/*The offset of the first DCT token for each coefficient for each plane.*/
691
unsigned char dct_token_offs[3][64];
692
/*The last DC coefficient for each plane and reference frame.*/
693
int dc_pred_last[3][4];
694
#if defined(OC_COLLECT_METRICS)
695
/*Fragment SAD statistics for MB mode estimation metrics.*/
696
unsigned *frag_sad;
697
/*Fragment SATD statistics for MB mode estimation metrics.*/
698
unsigned *frag_satd;
699
/*Fragment SSD statistics for MB mode estimation metrics.*/
700
unsigned *frag_ssd;
701
#endif
702
/*The R-D optimization parameter.*/
703
int lambda;
704
/*The average block "activity" of the previous frame.*/
705
unsigned activity_avg;
706
/*The average MB luma of the previous frame.*/
707
unsigned luma_avg;
708
/*The huffman tables in use.*/
709
th_huff_code huff_codes[TH_NHUFFMAN_TABLES][TH_NDCT_TOKENS];
710
/*The quantization parameters in use.*/
711
th_quant_info qinfo;
712
/*The original DC coefficients saved off from the dequatization tables.*/
713
ogg_uint16_t dequant_dc[64][3][2];
714
/*Condensed dequantization tables.*/
715
const ogg_uint16_t *dequant[3][3][2];
716
/*Condensed quantization tables.*/
717
void *enquant[3][3][2];
718
/*The full set of quantization tables.*/
719
void *enquant_tables[64][3][2];
720
/*Storage for the quantization tables.*/
721
unsigned char *enquant_table_data;
722
/*An "average" quantizer for each frame type (INTRA or INTER) and qi value.
723
This is used to parameterize the rate control decisions.
724
They are kept in the log domain to simplify later processing.
725
These are DCT domain quantizers, and so are scaled by an additional factor
726
of 4 from the pixel domain.*/
727
ogg_int64_t log_qavg[2][64];
728
/*The "average" quantizer further partitioned by color plane.
729
This is used to parameterize mode decision.
730
These are DCT domain quantizers, and so are scaled by an additional factor
731
of 4 from the pixel domain.*/
732
ogg_int16_t log_plq[64][3][2];
733
/*The R-D scale factors to apply to chroma blocks for a given frame type
734
(INTRA or INTER) and qi value.
735
The first is the "D" modifier (rd_scale), while the second is the "lambda"
736
modifier (rd_iscale).*/
737
ogg_uint16_t chroma_rd_scale[2][64][2];
738
/*The interpolated mode decision R-D lookup tables for the current
739
quantizers, color plane, and quantization type.*/
740
oc_mode_rd mode_rd[3][3][2][OC_COMP_BINS];
741
/*The buffer state used to drive rate control.*/
742
oc_rc_state rc;
743
# if defined(OC_ENC_USE_VTABLE)
744
/*Table for encoder acceleration functions.*/
745
oc_enc_opt_vtable opt_vtable;
746
# endif
747
/*Table for encoder data used by accelerated functions.*/
748
oc_enc_opt_data opt_data;
749
};
750
751
752
void oc_enc_analyze_intra(oc_enc_ctx *_enc,int _recode);
753
int oc_enc_analyze_inter(oc_enc_ctx *_enc,int _allow_keyframe,int _recode);
754
755
756
757
/*Perform fullpel motion search for a single MB against both reference frames.*/
758
void oc_mcenc_search(oc_enc_ctx *_enc,int _mbi);
759
/*Refine a MB MV for one frame.*/
760
void oc_mcenc_refine1mv(oc_enc_ctx *_enc,int _mbi,int _frame);
761
/*Refine the block MVs.*/
762
void oc_mcenc_refine4mv(oc_enc_ctx *_enc,int _mbi);
763
764
765
766
/*Used to rollback a tokenlog transaction when we retroactively decide to skip
767
a fragment.
768
A checkpoint is taken right before each token is added.*/
769
struct oc_token_checkpoint{
770
/*The color plane the token was added to.*/
771
unsigned char pli;
772
/*The zig-zag index the token was added to.*/
773
unsigned char zzi;
774
/*The outstanding EOB run count before the token was added.*/
775
ogg_uint16_t eob_run;
776
/*The token count before the token was added.*/
777
ptrdiff_t ndct_tokens;
778
};
779
780
781
782
void oc_enc_tokenize_start(oc_enc_ctx *_enc);
783
int oc_enc_tokenize_ac(oc_enc_ctx *_enc,int _pli,ptrdiff_t _fragi,
784
ogg_int16_t *_qdct_out,const ogg_int16_t *_qdct_in,
785
const ogg_uint16_t *_dequant,const ogg_int16_t *_dct,
786
int _zzi,oc_token_checkpoint **_stack,int _lambda,int _acmin);
787
int oc_enc_tokenize_ac_fast(oc_enc_ctx *_enc,int _pli,ptrdiff_t _fragi,
788
ogg_int16_t *_qdct_out,const ogg_int16_t *_qdct_in,
789
const ogg_uint16_t *_dequant,const ogg_int16_t *_dct,
790
int _zzi,oc_token_checkpoint **_stack,int _lambda,int _acmin);
791
void oc_enc_tokenlog_rollback(oc_enc_ctx *_enc,
792
const oc_token_checkpoint *_stack,int _n);
793
void oc_enc_pred_dc_frag_rows(oc_enc_ctx *_enc,
794
int _pli,int _fragy0,int _frag_yend);
795
void oc_enc_tokenize_dc_frag_list(oc_enc_ctx *_enc,int _pli,
796
const ptrdiff_t *_coded_fragis,ptrdiff_t _ncoded_fragis,
797
int _prev_ndct_tokens1,int _prev_eob_run1);
798
void oc_enc_tokenize_finish(oc_enc_ctx *_enc);
799
800
801
802
/*Utility routine to encode one of the header packets.*/
803
int oc_state_flushheader(oc_theora_state *_state,int *_packet_state,
804
oggpack_buffer *_opb,const th_quant_info *_qinfo,
805
const th_huff_code _codes[TH_NHUFFMAN_TABLES][TH_NDCT_TOKENS],
806
const char *_vendor,th_comment *_tc,ogg_packet *_op);
807
808
809
810
/*Default pure-C implementations of encoder-specific accelerated functions.*/
811
void oc_enc_accel_init_c(oc_enc_ctx *_enc);
812
813
void oc_enc_frag_sub_c(ogg_int16_t _diff[64],
814
const unsigned char *_src,const unsigned char *_ref,int _ystride);
815
void oc_enc_frag_sub_128_c(ogg_int16_t _diff[64],
816
const unsigned char *_src,int _ystride);
817
unsigned oc_enc_frag_sad_c(const unsigned char *_src,
818
const unsigned char *_ref,int _ystride);
819
unsigned oc_enc_frag_sad_thresh_c(const unsigned char *_src,
820
const unsigned char *_ref,int _ystride,unsigned _thresh);
821
unsigned oc_enc_frag_sad2_thresh_c(const unsigned char *_src,
822
const unsigned char *_ref1,const unsigned char *_ref2,int _ystride,
823
unsigned _thresh);
824
unsigned oc_enc_frag_intra_sad_c(const unsigned char *_src, int _ystride);
825
unsigned oc_enc_frag_satd_c(int *_dc,const unsigned char *_src,
826
const unsigned char *_ref,int _ystride);
827
unsigned oc_enc_frag_satd2_c(int *_dc,const unsigned char *_src,
828
const unsigned char *_ref1,const unsigned char *_ref2,int _ystride);
829
unsigned oc_enc_frag_intra_satd_c(int *_dc,
830
const unsigned char *_src,int _ystride);
831
unsigned oc_enc_frag_ssd_c(const unsigned char *_src,
832
const unsigned char *_ref,int _ystride);
833
unsigned oc_enc_frag_border_ssd_c(const unsigned char *_src,
834
const unsigned char *_ref,int _ystride,ogg_int64_t _mask);
835
void oc_enc_frag_copy2_c(unsigned char *_dst,
836
const unsigned char *_src1,const unsigned char *_src2,int _ystride);
837
void oc_enc_enquant_table_init_c(void *_enquant,
838
const ogg_uint16_t _dequant[64]);
839
void oc_enc_enquant_table_fixup_c(void *_enquant[3][3][2],int _nqis);
840
int oc_enc_quantize_c(ogg_int16_t _qdct[64],const ogg_int16_t _dct[64],
841
const ogg_uint16_t _dequant[64],const void *_enquant);
842
void oc_enc_fdct8x8_c(ogg_int16_t _y[64],const ogg_int16_t _x[64]);
843
844
#endif
845
846