1 /********************************************************************
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. *
8 * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2007 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
11 ********************************************************************
14 last mod: $Id: internal.h 14714 2008-04-12 01:04:43Z giles $
16 ********************************************************************/
18 #if !defined(_internal_H)
19 # define _internal_H (1)
21 # if defined(HAVE_CONFIG_H)
24 # include "theora/codec.h"
25 # include "theora/theora.h"
26 # include "dec/ocintrin.h"
27 # include "dec/huffman.h"
28 # include "dec/quant.h"
31 #if defined(_MSC_VER) && _MSC_VER < 1400
32 static const dframe
= 0;
33 static void TH_DEBUG(const char *fmt
, ...) {}
34 #elif defined(_TH_DEBUG_)
37 extern FILE *debugout
;
38 #define TH_DEBUG(...) fprintf(debugout, __VA_ARGS__)
43 /*Thank you Microsoft, I know the order of operations.*/
44 # if defined(_MSC_VER)
45 # pragma warning(disable:4554) /* order of operations */
46 # pragma warning(disable:4799) /* disable missing EMMS warnings */
49 /*This library's version.*/
50 # define OC_VENDOR_STRING "Xiph.Org libTheora I 20071025 3 2 1"
52 /*Theora bitstream version.*/
53 # define TH_VERSION_MAJOR (3)
54 # define TH_VERSION_MINOR (2)
55 # define TH_VERSION_SUB (1)
56 # define TH_VERSION_CHECK(_info,_maj,_min,_sub) \
57 ((_info)->version_major>(_maj)||(_info)->version_major==(_maj)&& \
58 ((_info)->version_minor>(_min)||(_info)->version_minor==(_min)&& \
59 (_info)->version_subminor>=(_sub)))
62 #define OC_INTRA_FRAME (0)
63 /*A predicted frame.*/
64 #define OC_INTER_FRAME (1)
65 /*A frame of unknown type (frame type decision has not yet been made).*/
66 #define OC_UNKWN_FRAME (-1)
68 /*The amount of padding to add to the reconstructed frame buffers on all
70 This is used to allow unrestricted motion vectors without special casing.
71 This must be a multiple of 2.*/
72 #define OC_UMV_PADDING (16)
74 /*Frame classification indices.*/
75 /*The previous golden frame.*/
76 #define OC_FRAME_GOLD (0)
77 /*The previous frame.*/
78 #define OC_FRAME_PREV (1)
79 /*The current frame.*/
80 #define OC_FRAME_SELF (2)
82 /*The input or output buffer.*/
83 #define OC_FRAME_IO (3)
86 /*Macro block is invalid: It is never coded.*/
87 #define OC_MODE_INVALID (-1)
88 /*Encoded difference from the same macro block in the previous frame.*/
89 #define OC_MODE_INTER_NOMV (0)
90 /*Encoded with no motion compensated prediction.*/
91 #define OC_MODE_INTRA (1)
92 /*Encoded difference from the previous frame offset by the given motion
94 #define OC_MODE_INTER_MV (2)
95 /*Encoded difference from the previous frame offset by the last coded motion
97 #define OC_MODE_INTER_MV_LAST (3)
98 /*Encoded difference from the previous frame offset by the second to last
99 coded motion vector.*/
100 #define OC_MODE_INTER_MV_LAST2 (4)
101 /*Encoded difference from the same macro block in the previous golden
103 #define OC_MODE_GOLDEN_NOMV (5)
104 /*Encoded difference from the previous golden frame offset by the given motion
106 #define OC_MODE_GOLDEN_MV (6)
107 /*Encoded difference from the previous frame offset by the individual motion
108 vectors given for each block.*/
109 #define OC_MODE_INTER_MV_FOUR (7)
110 /*The number of (coded) modes.*/
111 #define OC_NMODES (8)
113 /*Macro block is not coded.*/
114 #define OC_MODE_NOT_CODED (8)
116 /*Predictor bit flags.*/
126 /*Constants for the packet state machine common between encoder and decoder.*/
128 /*Next packet to emit/read: Codec info header.*/
129 #define OC_PACKET_INFO_HDR (-3)
130 /*Next packet to emit/read: Comment header.*/
131 #define OC_PACKET_COMMENT_HDR (-2)
132 /*Next packet to emit/read: Codec setup header.*/
133 #define OC_PACKET_SETUP_HDR (-1)
134 /*No more packets to emit/read.*/
135 #define OC_PACKET_DONE (INT_MAX)
139 typedef struct oc_theora_state oc_theora_state
;
143 /*A map from a super block to fragment numbers.*/
144 typedef int oc_sb_map
[4][4];
145 /*A map from a macro block to fragment numbers.*/
146 typedef int oc_mb_map
[3][4];
148 typedef signed char oc_mv
[2];
152 /*Super block information.
153 Super blocks are 32x32 segments of pixels in a single color plane indexed
155 Internally, super blocks are broken up into four quadrants, each of which
156 contains a 2x2 pattern of blocks, each of which is an 8x8 block of pixels.
157 Quadrants, and the blocks within them, are indexed in a special order called
158 a "Hilbert curve" within the super block.
160 In order to differentiate between the Hilbert-curve indexing strategy and
161 the regular image order indexing strategy, blocks indexed in image order
162 are called "fragments".
163 Fragments are indexed in image order, left to right, then bottom to top,
164 from Y plane to Cb plane to Cr plane.*/
166 unsigned coded_fully
:1;
167 unsigned coded_partially
:1;
168 unsigned quad_valid
:4;
174 /*Macro block information.
175 The co-located fragments in all image planes corresponding to the location of
176 a single luma plane super block quadrant forms a macro block.
177 Thus there is only a single set of macro blocks for all planes, which
178 contains between 6 and 12 fragments, depending on the pixel format.
179 Therefore macro block information is kept in a separate array from super
180 blocks, to avoid unused space in the other planes.*/
182 /*The current macro block mode.
183 A negative number indicates the macro block lies entirely outside the
186 /*The X location of the macro block's upper-left hand pixel.*/
188 /*The Y location of the macro block's upper-right hand pixel.*/
190 /*The fragments that belong to this macro block in each color plane.
191 Fragments are stored in image order (left to right then top to bottom).
192 When chroma components are decimated, the extra fragments have an index of
199 /*Information about a fragment which intersects the border of the displayable
201 This marks which pixels belong to the displayable region, and is used to
202 ensure that pixels outside of this region are never referenced.
203 This allows applications to pass in buffers that are really the size of the
204 displayable region without causing a seg fault.*/
206 /*A bit mask marking which pixels are in the displayable region.
207 Pixel (x,y) corresponds to bit (y<<3|x).*/
209 /*The number of pixels in the displayable region.
210 This is always positive, and always less than 64.*/
216 /*Fragment information.*/
218 /*A flag indicating whether or not this fragment is coded.*/
220 /*A flag indicating that all of this fragment lies outside the displayable
222 Note the contrast with an invalid macro block, which is outside the coded
223 frame, not just the displayable one.*/
225 /*The quality index used for this fragment's AC coefficients.*/
227 /*The mode of the macroblock this fragment belongs to.*/
229 /*The prediction-corrected DC component.*/
231 /*A pointer to the portion of an image covered by this fragment in several
233 The first three are reconstructed frame buffers, while the last is the
235 The appropriate stride value is determined by the color plane the fragment
237 unsigned char *buffer
[4];
238 /*Information for fragments which lie partially outside the displayable
240 For fragments completely inside or outside this region, this is NULL.*/
241 oc_border_info
*border
;
242 /*The motion vector used for this fragment.*/
256 /*A description of each fragment plane.*/
258 /*The number of fragments in the horizontal direction.*/
260 /*The number of fragments in the vertical direction.*/
262 /*The offset of the first fragment in the plane.*/
264 /*The total number of fragments in the plane.*/
266 /*The number of super blocks in the horizontal direction.*/
268 /*The number of super blocks in the vertical direction.*/
270 /*The offset of the first super block in the plane.*/
272 /*The total number of super blocks in the plane.*/
278 /*The shared (encoder and decoder) functions that have accelerated variants.*/
280 void (*frag_recon_intra
)(unsigned char *_dst
,int _dst_ystride
,
281 const ogg_int16_t
*_residue
);
282 void (*frag_recon_inter
)(unsigned char *_dst
,int _dst_ystride
,
283 const unsigned char *_src
,int _src_ystride
,const ogg_int16_t
*_residue
);
284 void (*frag_recon_inter2
)(unsigned char *_dst
,int _dst_ystride
,
285 const unsigned char *_src1
,int _src1_ystride
,const unsigned char *_src2
,
286 int _src2_ystride
,const ogg_int16_t
*_residue
);
287 void (*state_frag_copy
)(const oc_theora_state
*_state
,
288 const int *_fragis
,int _nfragis
,int _dst_frame
,int _src_frame
,int _pli
);
289 void (*state_frag_recon
)(oc_theora_state
*_state
,oc_fragment
*_frag
,
290 int _pli
,ogg_int16_t _dct_coeffs
[128],int _last_zzi
,int _ncoefs
,
291 ogg_uint16_t _dc_iquant
,const ogg_uint16_t _ac_iquant
[64]);
292 void (*restore_fpu
)(void);
293 void (*state_loop_filter_frag_rows
)(oc_theora_state
*_state
,int *_bv
,
294 int _refi
,int _pli
,int _fragy0
,int _fragy_end
);
299 /*Common state information between the encoder and decoder.*/
300 struct oc_theora_state
{
301 /*The stream information.*/
303 /*Table for shared accelerated functions.*/
304 oc_base_opt_vtable opt_vtable
;
305 /*CPU flags to detect the presence of extended instruction sets.*/
306 ogg_uint32_t cpu_flags
;
307 /*The fragment plane descriptions.*/
308 oc_fragment_plane fplanes
[3];
309 /*The total number of fragments in a single frame.*/
311 /*The list of fragments, indexed in image order.*/
313 /*The total number of super blocks in a single frame.*/
315 /*The list of super blocks, indexed in image order.*/
317 /*The number of macro blocks in the X direction.*/
319 /*The number of macro blocks in the Y direction.*/
321 /*The total number of macro blocks.*/
323 /*The list of macro blocks, indexed in super block order.
324 That is, the macro block corresponding to the macro block mbi in (luma
325 plane) super block sbi is (sbi<<2|mbi).*/
327 /*The list of coded fragments, in coded order.*/
329 /*The number of coded fragments in each plane.*/
330 int ncoded_fragis
[3];
331 /*The list of uncoded fragments.
332 This just past the end of the list, which is in reverse order, and
333 uses the same block of allocated storage as the coded_fragis list.*/
335 /*The number of uncoded fragments in each plane.*/
336 int nuncoded_fragis
[3];
337 /*The list of coded macro blocks in the Y plane, in coded order.*/
339 /*The number of coded macro blocks in the Y plane.*/
341 /*A copy of the image data used to fill the input pointers in each fragment.
342 If the data pointers or strides change, these input pointers must be
344 th_ycbcr_buffer input
;
345 /*The number of unique border patterns.*/
347 /*The storage for the border info for all border fragments.
348 This data is pointed to from the appropriate fragments.*/
349 oc_border_info borders
[16];
350 /*The index of the buffers being used for each OC_FRAME_* reference frame.*/
351 int ref_frame_idx
[3];
352 /*The actual buffers used for the previously decoded frames.*/
353 th_ycbcr_buffer ref_frame_bufs
[3];
354 /*The storage for the reference frame buffers.*/
355 unsigned char *ref_frame_data
;
356 /*The frame number of the last keyframe.*/
357 ogg_int64_t keyframe_num
;
358 /*The frame number of the current frame.*/
359 ogg_int64_t curframe_num
;
360 /*The granpos of the current frame.*/
362 /*The type of the current frame.*/
364 /*The quality indices of the current frame.*/
366 /*The number of quality indices used in the current frame.*/
368 /*The dequantization tables.*/
369 oc_quant_table
*dequant_tables
[2][3];
370 oc_quant_tables dequant_table_data
[2][3];
371 /*Loop filter strength parameters.*/
372 unsigned char loop_filter_limits
[64];
377 /*The function type used to fill in the chroma plane motion vectors for a
378 macro block when 4 different motion vectors are specified in the luma
380 _cbmvs: The chroma block-level motion vectors to fill in.
381 _lmbmv: The luma macro-block level motion vector to fill in for use in
383 _lbmvs: The luma block-level motion vectors.*/
384 typedef void (*oc_set_chroma_mvs_func
)(oc_mv _cbmvs
[4],const oc_mv _lbmvs
[4]);
388 /*A map from the index in the zig zag scan to the coefficient number in a
390 The extra 64 entries send out of bounds indexes to index 64.
391 This is used to safely ignore invalid zero runs when decoding
393 extern const int OC_FZIG_ZAG
[128];
394 /*A map from the coefficient number in a block to its index in the zig zag
396 extern const int OC_IZIG_ZAG
[64];
397 /*The predictor frame to use for each macro block mode.*/
398 extern const int OC_FRAME_FOR_MODE
[OC_NMODES
];
399 /*A map from physical macro block ordering to bitstream macro block
400 ordering within a super block.*/
401 extern const int OC_MB_MAP
[2][2];
402 /*A list of the indices in the oc_mb.map array that can be valid for each of
403 the various chroma decimation types.*/
404 extern const int OC_MB_MAP_IDXS
[TH_PF_NFORMATS
][12];
405 /*The number of indices in the oc_mb.map array that can be valid for each of
406 the various chroma decimation types.*/
407 extern const int OC_MB_MAP_NIDXS
[TH_PF_NFORMATS
];
408 /*A table of functions used to fill in the Cb,Cr plane motion vectors for a
409 macro block when 4 different motion vectors are specified in the luma
411 extern const oc_set_chroma_mvs_func OC_SET_CHROMA_MVS_TABLE
[TH_PF_NFORMATS
];
415 int oc_ilog(unsigned _v
);
416 void **oc_malloc_2d(size_t _height
,size_t _width
,size_t _sz
);
417 void **oc_calloc_2d(size_t _height
,size_t _width
,size_t _sz
);
418 void oc_free_2d(void *_ptr
);
420 void oc_ycbcr_buffer_flip(th_ycbcr_buffer _dst
,
421 const th_ycbcr_buffer _src
);
423 int oc_dct_token_skip(int _token
,int _extra_bits
);
425 int oc_frag_pred_dc(const oc_fragment
*_frag
,
426 const oc_fragment_plane
*_fplane
,int _x
,int _y
,int _pred_last
[3]);
428 int oc_state_init(oc_theora_state
*_state
,const th_info
*_info
);
429 void oc_state_clear(oc_theora_state
*_state
);
430 void oc_state_vtable_init_c(oc_theora_state
*_state
);
431 void oc_state_borders_fill_rows(oc_theora_state
*_state
,int _refi
,int _pli
,
433 void oc_state_borders_fill_caps(oc_theora_state
*_state
,int _refi
,int _pli
);
434 void oc_state_borders_fill(oc_theora_state
*_state
,int _refi
);
435 void oc_state_fill_buffer_ptrs(oc_theora_state
*_state
,int _buf_idx
,
436 th_ycbcr_buffer _img
);
437 int oc_state_mbi_for_pos(oc_theora_state
*_state
,int _mbx
,int _mby
);
438 int oc_state_get_mv_offsets(oc_theora_state
*_state
,int *_offsets
,
439 int _dx
,int _dy
,int _ystride
,int _pli
);
441 int oc_state_loop_filter_init(oc_theora_state
*_state
,int *_bv
);
442 void oc_state_loop_filter(oc_theora_state
*_state
,int _frame
);
443 #if defined(OC_DUMP_IMAGES)
444 int oc_state_dump_frame(const oc_theora_state
*_state
,int _frame
,
448 /*Shared accelerated functions.*/
449 void oc_frag_recon_intra(const oc_theora_state
*_state
,
450 unsigned char *_dst
,int _dst_ystride
,const ogg_int16_t
*_residue
);
451 void oc_frag_recon_inter(const oc_theora_state
*_state
,
452 unsigned char *_dst
,int _dst_ystride
,
453 const unsigned char *_src
,int _src_ystride
,const ogg_int16_t
*_residue
);
454 void oc_frag_recon_inter2(const oc_theora_state
*_state
,
455 unsigned char *_dst
,int _dst_ystride
,
456 const unsigned char *_src1
,int _src1_ystride
,const unsigned char *_src2
,
457 int _src2_ystride
,const ogg_int16_t
*_residue
);
458 void oc_state_frag_copy(const oc_theora_state
*_state
,const int *_fragis
,
459 int _nfragis
,int _dst_frame
,int _src_frame
,int _pli
);
460 void oc_state_frag_recon(oc_theora_state
*_state
,oc_fragment
*_frag
,
461 int _pli
,ogg_int16_t _dct_coeffs
[128],int _last_zzi
,int _ncoefs
,
462 ogg_uint16_t _dc_iquant
,const ogg_uint16_t _ac_iquant
[64]);
463 void oc_state_loop_filter_frag_rows(oc_theora_state
*_state
,int *_bv
,
464 int _refi
,int _pli
,int _fragy0
,int _fragy_end
);
465 void oc_restore_fpu(const oc_theora_state
*_state
);
467 /*Default pure-C implementations.*/
468 void oc_frag_recon_intra_c(unsigned char *_dst
,int _dst_ystride
,
469 const ogg_int16_t
*_residue
);
470 void oc_frag_recon_inter_c(unsigned char *_dst
,int _dst_ystride
,
471 const unsigned char *_src
,int _src_ystride
,const ogg_int16_t
*_residue
);
472 void oc_frag_recon_inter2_c(unsigned char *_dst
,int _dst_ystride
,
473 const unsigned char *_src1
,int _src1_ystride
,const unsigned char *_src2
,
474 int _src2_ystride
,const ogg_int16_t
*_residue
);
475 void oc_state_frag_copy_c(const oc_theora_state
*_state
,const int *_fragis
,
476 int _nfragis
,int _dst_frame
,int _src_frame
,int _pli
);
477 void oc_state_frag_recon_c(oc_theora_state
*_state
,oc_fragment
*_frag
,
478 int _pli
,ogg_int16_t _dct_coeffs
[128],int _last_zzi
,int _ncoefs
,
479 ogg_uint16_t _dc_iquant
,const ogg_uint16_t _ac_iquant
[64]);
480 void oc_state_loop_filter_frag_rows_c(oc_theora_state
*_state
,int *_bv
,
481 int _refi
,int _pli
,int _fragy0
,int _fragy_end
);
482 void oc_restore_fpu_c(void);
484 /*We need a way to call a few encoder functions without introducing a link-time
485 dependency into the decoder, while still allowing the old alpha API which
486 does not distinguish between encoder and decoder objects to be used.
487 We do this by placing a function table at the start of the encoder object
488 which can dispatch into the encoder library.
489 We do a similar thing for the decoder in case we ever decide to split off a
490 common base library.*/
491 typedef void (*oc_state_clear_func
)(theora_state
*_th
);
492 typedef int (*oc_state_control_func
)(theora_state
*th
,int req
,
493 void *buf
,size_t buf_sz
);
494 typedef ogg_int64_t (*oc_state_granule_frame_func
)(theora_state
*_th
,
495 ogg_int64_t _granulepos
);
496 typedef double (*oc_state_granule_time_func
)(theora_state
*_th
,
497 ogg_int64_t _granulepos
);
499 typedef struct oc_state_dispatch_vtbl oc_state_dispatch_vtbl
;
501 struct oc_state_dispatch_vtbl
{
502 oc_state_clear_func clear
;
503 oc_state_control_func control
;
504 oc_state_granule_frame_func granule_frame
;
505 oc_state_granule_time_func granule_time
;