Initialize Tizen 2.3
[external/libtheora.git] / lib / encint.h
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
5  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
7  *                                                                  *
8  * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009                *
9  * by the Xiph.Org Foundation http://www.xiph.org/                  *
10  *                                                                  *
11  ********************************************************************
12
13   function:
14   last mod: $Id: encint.h 16503 2009-08-22 18:14:02Z giles $
15
16  ********************************************************************/
17 #if !defined(_encint_H)
18 # define _encint_H (1)
19 # if defined(HAVE_CONFIG_H)
20 #  include "config.h"
21 # endif
22 # include "theora/theoraenc.h"
23 # include "internal.h"
24 # include "ocintrin.h"
25 # include "mathops.h"
26 # include "enquant.h"
27 # include "huffenc.h"
28 /*# define OC_COLLECT_METRICS*/
29
30
31
32 typedef oc_mv                         oc_mv2[2];
33
34 typedef struct oc_enc_opt_vtable      oc_enc_opt_vtable;
35 typedef struct oc_mb_enc_info         oc_mb_enc_info;
36 typedef struct oc_mode_scheme_chooser oc_mode_scheme_chooser;
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 /*Constants for the packet-out state machine specific to the encoder.*/
46
47 /*Next packet to emit: Data packet, but none are ready yet.*/
48 #define OC_PACKET_EMPTY (0)
49 /*Next packet to emit: Data packet, and one is ready.*/
50 #define OC_PACKET_READY (1)
51
52 /*All features enabled.*/
53 #define OC_SP_LEVEL_SLOW       (0)
54 /*Enable early skip.*/
55 #define OC_SP_LEVEL_EARLY_SKIP (1)
56 /*Disable motion compensation.*/
57 #define OC_SP_LEVEL_NOMC       (2)
58 /*Maximum valid speed level.*/
59 #define OC_SP_LEVEL_MAX        (2)
60
61
62 /*The bits used for each of the MB mode codebooks.*/
63 extern const unsigned char OC_MODE_BITS[2][OC_NMODES];
64
65 /*The bits used for each of the MV codebooks.*/
66 extern const unsigned char OC_MV_BITS[2][64];
67
68 /*The minimum value that can be stored in a SB run for each codeword.
69   The last entry is the upper bound on the length of a single SB run.*/
70 extern const ogg_uint16_t  OC_SB_RUN_VAL_MIN[8];
71 /*The bits used for each SB run codeword.*/
72 extern const unsigned char OC_SB_RUN_CODE_NBITS[7];
73
74 /*The bits used for each block run length (starting with 1).*/
75 extern const unsigned char OC_BLOCK_RUN_CODE_NBITS[30];
76
77
78
79 /*Encoder specific functions with accelerated variants.*/
80 struct oc_enc_opt_vtable{
81   unsigned (*frag_sad)(const unsigned char *_src,
82    const unsigned char *_ref,int _ystride);
83   unsigned (*frag_sad_thresh)(const unsigned char *_src,
84    const unsigned char *_ref,int _ystride,unsigned _thresh);
85   unsigned (*frag_sad2_thresh)(const unsigned char *_src,
86    const unsigned char *_ref1,const unsigned char *_ref2,int _ystride,
87    unsigned _thresh);
88   unsigned (*frag_satd_thresh)(const unsigned char *_src,
89    const unsigned char *_ref,int _ystride,unsigned _thresh);
90   unsigned (*frag_satd2_thresh)(const unsigned char *_src,
91    const unsigned char *_ref1,const unsigned char *_ref2,int _ystride,
92    unsigned _thresh);
93   unsigned (*frag_intra_satd)(const unsigned char *_src,int _ystride);
94   void     (*frag_sub)(ogg_int16_t _diff[64],const unsigned char *_src,
95    const unsigned char *_ref,int _ystride);
96   void     (*frag_sub_128)(ogg_int16_t _diff[64],
97    const unsigned char *_src,int _ystride);
98   void     (*frag_copy2)(unsigned char *_dst,
99    const unsigned char *_src1,const unsigned char *_src2,int _ystride);
100   void     (*frag_recon_intra)(unsigned char *_dst,int _ystride,
101    const ogg_int16_t _residue[64]);
102   void     (*frag_recon_inter)(unsigned char *_dst,
103    const unsigned char *_src,int _ystride,const ogg_int16_t _residue[64]);
104   void     (*fdct8x8)(ogg_int16_t _y[64],const ogg_int16_t _x[64]);
105 };
106
107
108 void oc_enc_vtable_init(oc_enc_ctx *_enc);
109
110
111
112 /*Encoder-specific macroblock information.*/
113 struct oc_mb_enc_info{
114   /*Neighboring macro blocks that have MVs available from the current frame.*/
115   unsigned      cneighbors[4];
116   /*Neighboring macro blocks to use for MVs from the previous frame.*/
117   unsigned      pneighbors[4];
118   /*The number of current-frame neighbors.*/
119   unsigned char ncneighbors;
120   /*The number of previous-frame neighbors.*/
121   unsigned char npneighbors;
122   /*Flags indicating which MB modes have been refined.*/
123   unsigned char refined;
124   /*Motion vectors for a macro block for the current frame and the
125      previous two frames.
126     Each is a set of 2 vectors against OC_FRAME_GOLD and OC_FRAME_PREV, which
127      can be used to estimate constant velocity and constant acceleration
128      predictors.
129     Uninitialized MVs are (0,0).*/
130   oc_mv2        analysis_mv[3];
131   /*Current unrefined analysis MVs.*/
132   oc_mv         unref_mv[2];
133   /*Unrefined block MVs.*/
134   oc_mv         block_mv[4];
135   /*Refined block MVs.*/
136   oc_mv         ref_mv[4];
137   /*Minimum motion estimation error from the analysis stage.*/
138   ogg_uint16_t  error[2];
139   /*MB error for half-pel refinement for each frame type.*/
140   unsigned      satd[2];
141   /*Block error for half-pel refinement.*/
142   unsigned      block_satd[4];
143 };
144
145
146
147 /*State machine to estimate the opportunity cost of coding a MB mode.*/
148 struct oc_mode_scheme_chooser{
149   /*Pointers to the a list containing the index of each mode in the mode
150      alphabet used by each scheme.
151     The first entry points to the dynamic scheme0_ranks, while the remaining 7
152      point to the constant entries stored in OC_MODE_SCHEMES.*/
153   const unsigned char *mode_ranks[8];
154   /*The ranks for each mode when coded with scheme 0.
155     These are optimized so that the more frequent modes have lower ranks.*/
156   unsigned char        scheme0_ranks[OC_NMODES];
157   /*The list of modes, sorted in descending order of frequency, that
158     corresponds to the ranks above.*/
159   unsigned char        scheme0_list[OC_NMODES];
160   /*The number of times each mode has been chosen so far.*/
161   int                  mode_counts[OC_NMODES];
162   /*The list of mode coding schemes, sorted in ascending order of bit cost.*/
163   unsigned char        scheme_list[8];
164   /*The number of bits used by each mode coding scheme.*/
165   ptrdiff_t            scheme_bits[8];
166 };
167
168
169 void oc_mode_scheme_chooser_init(oc_mode_scheme_chooser *_chooser);
170
171
172
173 /*A 2nd order low-pass Bessel follower.
174   We use this for rate control because it has fast reaction time, but is
175    critically damped.*/
176 struct oc_iir_filter{
177   ogg_int32_t c[2];
178   ogg_int64_t g;
179   ogg_int32_t x[2];
180   ogg_int32_t y[2];
181 };
182
183
184
185 /*The 2-pass metrics associated with a single frame.*/
186 struct oc_frame_metrics{
187   /*The log base 2 of the scale factor for this frame in Q24 format.*/
188   ogg_int32_t   log_scale;
189   /*The number of application-requested duplicates of this frame.*/
190   unsigned      dup_count:31;
191   /*The frame type from pass 1.*/
192   unsigned      frame_type:1;
193 };
194
195
196
197 /*Rate control state information.*/
198 struct oc_rc_state{
199   /*The target average bits per frame.*/
200   ogg_int64_t        bits_per_frame;
201   /*The current buffer fullness (bits available to be used).*/
202   ogg_int64_t        fullness;
203   /*The target buffer fullness.
204     This is where we'd like to be by the last keyframe the appears in the next
205      buf_delay frames.*/
206   ogg_int64_t        target;
207   /*The maximum buffer fullness (total size of the buffer).*/
208   ogg_int64_t        max;
209   /*The log of the number of pixels in a frame in Q57 format.*/
210   ogg_int64_t        log_npixels;
211   /*The exponent used in the rate model in Q8 format.*/
212   unsigned           exp[2];
213   /*The number of frames to distribute the buffer usage over.*/
214   int                buf_delay;
215   /*The total drop count from the previous frame.
216     This includes duplicates explicitly requested via the
217      TH_ENCCTL_SET_DUP_COUNT API as well as frames we chose to drop ourselves.*/
218   ogg_uint32_t       prev_drop_count;
219   /*The log of an estimated scale factor used to obtain the real framerate, for
220      VFR sources or, e.g., 12 fps content doubled to 24 fps, etc.*/
221   ogg_int64_t        log_drop_scale;
222   /*The log of estimated scale factor for the rate model in Q57 format.*/
223   ogg_int64_t        log_scale[2];
224   /*The log of the target quantizer level in Q57 format.*/
225   ogg_int64_t        log_qtarget;
226   /*Will we drop frames to meet bitrate target?*/
227   unsigned char      drop_frames;
228   /*Do we respect the maximum buffer fullness?*/
229   unsigned char      cap_overflow;
230   /*Can the reservoir go negative?*/
231   unsigned char      cap_underflow;
232   /*Second-order lowpass filters to track scale and VFR.*/
233   oc_iir_filter      scalefilter[2];
234   int                inter_count;
235   int                inter_delay;
236   int                inter_delay_target;
237   oc_iir_filter      vfrfilter;
238   /*Two-pass mode state.
239     0 => 1-pass encoding.
240     1 => 1st pass of 2-pass encoding.
241     2 => 2nd pass of 2-pass encoding.*/
242   int                twopass;
243   /*Buffer for current frame metrics.*/
244   unsigned char      twopass_buffer[48];
245   /*The number of bytes in the frame metrics buffer.
246     When 2-pass encoding is enabled, this is set to 0 after each frame is
247      submitted, and must be non-zero before the next frame will be accepted.*/
248   int                twopass_buffer_bytes;
249   int                twopass_buffer_fill;
250   /*Whether or not to force the next frame to be a keyframe.*/
251   unsigned char      twopass_force_kf;
252   /*The metrics for the previous frame.*/
253   oc_frame_metrics   prev_metrics;
254   /*The metrics for the current frame.*/
255   oc_frame_metrics   cur_metrics;
256   /*The buffered metrics for future frames.*/
257   oc_frame_metrics  *frame_metrics;
258   int                nframe_metrics;
259   int                cframe_metrics;
260   /*The index of the current frame in the circular metric buffer.*/
261   int                frame_metrics_head;
262   /*The frame count of each type (keyframes, delta frames, and dup frames);
263      32 bits limits us to 2.268 years at 60 fps.*/
264   ogg_uint32_t       frames_total[3];
265   /*The number of frames of each type yet to be processed.*/
266   ogg_uint32_t       frames_left[3];
267   /*The sum of the scale values for each frame type.*/
268   ogg_int64_t        scale_sum[2];
269   /*The start of the window over which the current scale sums are taken.*/
270   int                scale_window0;
271   /*The end of the window over which the current scale sums are taken.*/
272   int                scale_window_end;
273   /*The frame count of each type in the current 2-pass window; this does not
274      include dup frames.*/
275   int                nframes[3];
276   /*The total accumulated estimation bias.*/
277   ogg_int64_t        rate_bias;
278 };
279
280
281 void oc_rc_state_init(oc_rc_state *_rc,oc_enc_ctx *_enc);
282 void oc_rc_state_clear(oc_rc_state *_rc);
283
284 void oc_enc_rc_resize(oc_enc_ctx *_enc);
285 int oc_enc_select_qi(oc_enc_ctx *_enc,int _qti,int _clamp);
286 void oc_enc_calc_lambda(oc_enc_ctx *_enc,int _frame_type);
287 int oc_enc_update_rc_state(oc_enc_ctx *_enc,
288  long _bits,int _qti,int _qi,int _trial,int _droppable);
289 int oc_enc_rc_2pass_out(oc_enc_ctx *_enc,unsigned char **_buf);
290 int oc_enc_rc_2pass_in(oc_enc_ctx *_enc,unsigned char *_buf,size_t _bytes);
291
292
293
294 /*The internal encoder state.*/
295 struct th_enc_ctx{
296   /*Shared encoder/decoder state.*/
297   oc_theora_state          state;
298   /*Buffer in which to assemble packets.*/
299   oggpack_buffer           opb;
300   /*Encoder-specific macroblock information.*/
301   oc_mb_enc_info          *mb_info;
302   /*DC coefficients after prediction.*/
303   ogg_int16_t             *frag_dc;
304   /*The list of coded macro blocks, in coded order.*/
305   unsigned                *coded_mbis;
306   /*The number of coded macro blocks.*/
307   size_t                   ncoded_mbis;
308   /*Whether or not packets are ready to be emitted.
309     This takes on negative values while there are remaining header packets to
310      be emitted, reaches 0 when the codec is ready for input, and becomes
311      positive when a frame has been processed and data packets are ready.*/
312   int                      packet_state;
313   /*The maximum distance between keyframes.*/
314   ogg_uint32_t             keyframe_frequency_force;
315   /*The number of duplicates to produce for the next frame.*/
316   ogg_uint32_t             dup_count;
317   /*The number of duplicates remaining to be emitted for the current frame.*/
318   ogg_uint32_t             nqueued_dups;
319   /*The number of duplicates emitted for the last frame.*/
320   ogg_uint32_t             prev_dup_count;
321   /*The current speed level.*/
322   int                      sp_level;
323   /*Whether or not VP3 compatibility mode has been enabled.*/
324   unsigned char            vp3_compatible;
325   /*Whether or not any INTER frames have been coded.*/
326   unsigned char            coded_inter_frame;
327   /*Whether or not previous frame was dropped.*/
328   unsigned char            prevframe_dropped;
329   /*Stores most recently chosen Huffman tables for each frame type, DC and AC
330      coefficients, and luma and chroma tokens.
331     The actual Huffman table used for a given coefficient depends not only on
332      the choice made here, but also its index in the zig-zag ordering.*/
333   unsigned char            huff_idxs[2][2][2];
334   /*Current count of bits used by each MV coding mode.*/
335   size_t                   mv_bits[2];
336   /*The mode scheme chooser for estimating mode coding costs.*/
337   oc_mode_scheme_chooser   chooser;
338   /*The number of vertical super blocks in an MCU.*/
339   int                      mcu_nvsbs;
340   /*The SSD error for skipping each fragment in the current MCU.*/
341   unsigned                *mcu_skip_ssd;
342   /*The DCT token lists for each coefficient and each plane.*/
343   unsigned char          **dct_tokens[3];
344   /*The extra bits associated with each DCT token.*/
345   ogg_uint16_t           **extra_bits[3];
346   /*The number of DCT tokens for each coefficient for each plane.*/
347   ptrdiff_t                ndct_tokens[3][64];
348   /*Pending EOB runs for each coefficient for each plane.*/
349   ogg_uint16_t             eob_run[3][64];
350   /*The offset of the first DCT token for each coefficient for each plane.*/
351   unsigned char            dct_token_offs[3][64];
352   /*The last DC coefficient for each plane and reference frame.*/
353   int                      dc_pred_last[3][3];
354 #if defined(OC_COLLECT_METRICS)
355   /*Fragment SATD statistics for MB mode estimation metrics.*/
356   unsigned                *frag_satd;
357   /*Fragment SSD statistics for MB mode estimation metrics.*/
358   unsigned                *frag_ssd;
359 #endif
360   /*The R-D optimization parameter.*/
361   int                      lambda;
362   /*The huffman tables in use.*/
363   th_huff_code             huff_codes[TH_NHUFFMAN_TABLES][TH_NDCT_TOKENS];
364   /*The quantization parameters in use.*/
365   th_quant_info            qinfo;
366   oc_iquant               *enquant_tables[64][3][2];
367   oc_iquant_table          enquant_table_data[64][3][2];
368   /*An "average" quantizer for each quantizer type (INTRA or INTER) and qi
369      value.
370     This is used to paramterize the rate control decisions.
371     They are kept in the log domain to simplify later processing.
372     Keep in mind these are DCT domain quantizers, and so are scaled by an
373      additional factor of 4 from the pixel domain.*/
374   ogg_int64_t              log_qavg[2][64];
375   /*The buffer state used to drive rate control.*/
376   oc_rc_state              rc;
377   /*Table for encoder acceleration functions.*/
378   oc_enc_opt_vtable        opt_vtable;
379 };
380
381
382 void oc_enc_analyze_intra(oc_enc_ctx *_enc,int _recode);
383 int oc_enc_analyze_inter(oc_enc_ctx *_enc,int _allow_keyframe,int _recode);
384 #if defined(OC_COLLECT_METRICS)
385 void oc_enc_mode_metrics_collect(oc_enc_ctx *_enc);
386 void oc_enc_mode_metrics_dump(oc_enc_ctx *_enc);
387 #endif
388
389
390
391 /*Perform fullpel motion search for a single MB against both reference frames.*/
392 void oc_mcenc_search(oc_enc_ctx *_enc,int _mbi);
393 /*Refine a MB MV for one frame.*/
394 void oc_mcenc_refine1mv(oc_enc_ctx *_enc,int _mbi,int _frame);
395 /*Refine the block MVs.*/
396 void oc_mcenc_refine4mv(oc_enc_ctx *_enc,int _mbi);
397
398
399
400 /*Used to rollback a tokenlog transaction when we retroactively decide to skip
401    a fragment.
402   A checkpoint is taken right before each token is added.*/
403 struct oc_token_checkpoint{
404   /*The color plane the token was added to.*/
405   unsigned char pli;
406   /*The zig-zag index the token was added to.*/
407   unsigned char zzi;
408   /*The outstanding EOB run count before the token was added.*/
409   ogg_uint16_t  eob_run;
410   /*The token count before the token was added.*/
411   ptrdiff_t     ndct_tokens;
412 };
413
414
415
416 void oc_enc_tokenize_start(oc_enc_ctx *_enc);
417 int oc_enc_tokenize_ac(oc_enc_ctx *_enc,int _pli,ptrdiff_t _fragi,
418  ogg_int16_t *_qdct,const ogg_uint16_t *_dequant,const ogg_int16_t *_dct,
419  int _zzi,oc_token_checkpoint **_stack,int _acmin);
420 void oc_enc_tokenlog_rollback(oc_enc_ctx *_enc,
421  const oc_token_checkpoint *_stack,int _n);
422 void oc_enc_pred_dc_frag_rows(oc_enc_ctx *_enc,
423  int _pli,int _fragy0,int _frag_yend);
424 void oc_enc_tokenize_dc_frag_list(oc_enc_ctx *_enc,int _pli,
425  const ptrdiff_t *_coded_fragis,ptrdiff_t _ncoded_fragis,
426  int _prev_ndct_tokens1,int _prev_eob_run1);
427 void oc_enc_tokenize_finish(oc_enc_ctx *_enc);
428
429
430
431 /*Utility routine to encode one of the header packets.*/
432 int oc_state_flushheader(oc_theora_state *_state,int *_packet_state,
433  oggpack_buffer *_opb,const th_quant_info *_qinfo,
434  const th_huff_code _codes[TH_NHUFFMAN_TABLES][TH_NDCT_TOKENS],
435  const char *_vendor,th_comment *_tc,ogg_packet *_op);
436
437
438
439 /*Encoder-specific accelerated functions.*/
440 void oc_enc_frag_sub(const oc_enc_ctx *_enc,ogg_int16_t _diff[64],
441  const unsigned char *_src,const unsigned char *_ref,int _ystride);
442 void oc_enc_frag_sub_128(const oc_enc_ctx *_enc,ogg_int16_t _diff[64],
443  const unsigned char *_src,int _ystride);
444 unsigned oc_enc_frag_sad(const oc_enc_ctx *_enc,const unsigned char *_src,
445  const unsigned char *_ref,int _ystride);
446 unsigned oc_enc_frag_sad_thresh(const oc_enc_ctx *_enc,
447  const unsigned char *_src,const unsigned char *_ref,int _ystride,
448  unsigned _thresh);
449 unsigned oc_enc_frag_sad2_thresh(const oc_enc_ctx *_enc,
450  const unsigned char *_src,const unsigned char *_ref1,
451  const unsigned char *_ref2,int _ystride,unsigned _thresh);
452 unsigned oc_enc_frag_satd_thresh(const oc_enc_ctx *_enc,
453  const unsigned char *_src,const unsigned char *_ref,int _ystride,
454  unsigned _thresh);
455 unsigned oc_enc_frag_satd2_thresh(const oc_enc_ctx *_enc,
456  const unsigned char *_src,const unsigned char *_ref1,
457  const unsigned char *_ref2,int _ystride,unsigned _thresh);
458 unsigned oc_enc_frag_intra_satd(const oc_enc_ctx *_enc,
459  const unsigned char *_src,int _ystride);
460 void oc_enc_frag_copy2(const oc_enc_ctx *_enc,unsigned char *_dst,
461  const unsigned char *_src1,const unsigned char *_src2,int _ystride);
462 void oc_enc_frag_recon_intra(const oc_enc_ctx *_enc,
463  unsigned char *_dst,int _ystride,const ogg_int16_t _residue[64]);
464 void oc_enc_frag_recon_inter(const oc_enc_ctx *_enc,unsigned char *_dst,
465  const unsigned char *_src,int _ystride,const ogg_int16_t _residue[64]);
466 void oc_enc_fdct8x8(const oc_enc_ctx *_enc,ogg_int16_t _y[64],
467  const ogg_int16_t _x[64]);
468
469 /*Default pure-C implementations.*/
470 void oc_enc_vtable_init_c(oc_enc_ctx *_enc);
471
472 void oc_enc_frag_sub_c(ogg_int16_t _diff[64],
473  const unsigned char *_src,const unsigned char *_ref,int _ystride);
474 void oc_enc_frag_sub_128_c(ogg_int16_t _diff[64],
475  const unsigned char *_src,int _ystride);
476 void oc_enc_frag_copy2_c(unsigned char *_dst,
477  const unsigned char *_src1,const unsigned char *_src2,int _ystride);
478 unsigned oc_enc_frag_sad_c(const unsigned char *_src,
479  const unsigned char *_ref,int _ystride);
480 unsigned oc_enc_frag_sad_thresh_c(const unsigned char *_src,
481  const unsigned char *_ref,int _ystride,unsigned _thresh);
482 unsigned oc_enc_frag_sad2_thresh_c(const unsigned char *_src,
483  const unsigned char *_ref1,const unsigned char *_ref2,int _ystride,
484  unsigned _thresh);
485 unsigned oc_enc_frag_satd_thresh_c(const unsigned char *_src,
486  const unsigned char *_ref,int _ystride,unsigned _thresh);
487 unsigned oc_enc_frag_satd2_thresh_c(const unsigned char *_src,
488  const unsigned char *_ref1,const unsigned char *_ref2,int _ystride,
489  unsigned _thresh);
490 unsigned oc_enc_frag_intra_satd_c(const unsigned char *_src,int _ystride);
491 void oc_enc_fdct8x8_c(ogg_int16_t _y[64],const ogg_int16_t _x[64]);
492
493 #endif