New API wrapper for the encoder.
[xiph/unicode.git] / theora-exp / lib / internal.h
blobe4d33314c527c84f00de009633152ddd9f376eab
1 #if !defined(_internal_H)
2 # define _internal_H (1)
3 # include <stdlib.h>
4 # if defined(HAVE_CONFIG_H)
5 # include <config.h>
6 # endif
7 # include "theora/codec.h"
8 # include "ocintrin.h"
9 # include "huffman.h"
10 # include "quant.h"
12 /*Thank you Microsoft, I know the order of operations.*/
13 # if defined(_MSCVER)
14 # pragma warning(disable:4554)
15 # endif
17 /*This library's version.*/
18 # define OC_VENDOR_STRING "derf's experimental encoder library " __DATE__
20 /*Theora bitstream version.*/
21 # define TH_VERSION_MAJOR (3)
22 # define TH_VERSION_MINOR (2)
23 # define TH_VERSION_SUB (0)
25 /*A keyframe.*/
26 #define OC_INTRA_FRAME (0)
27 /*A predicted frame.*/
28 #define OC_INTER_FRAME (1)
29 /*A frame of unknown type (frame type decision has not yet been made).*/
30 #define OC_UNKWN_FRAME (-1)
32 /*The amount of padding to add to the reconstructed frame buffers on all
33 sides.
34 This is used to allow unrestricted motion vectors without special casing.
35 This must be a multiple of 2.*/
36 #define OC_UMV_PADDING (16)
38 /*Frame classification indices.*/
39 /*The previous golden frame.*/
40 #define OC_FRAME_GOLD (0)
41 /*The previous frame.*/
42 #define OC_FRAME_PREV (1)
43 /*The current frame.*/
44 #define OC_FRAME_SELF (2)
46 /*The input or output buffer.*/
47 #define OC_FRAME_IO (3)
49 /*Macroblock modes.*/
50 /*Macro block is invalid: It is never coded.*/
51 #define OC_MODE_INVALID (-1)
53 /*Encoded with no motion compensated prediction.*/
54 #define OC_MODE_INTRA (0)
55 /*Encoded difference from the same macro block in the previous frame.*/
56 #define OC_MODE_INTER_NOMV (1)
57 /*Encoded difference from the previous frame offset by the given motion
58 vector.*/
59 #define OC_MODE_INTER_MV (2)
60 /*Encoded difference from the previous frame offset by the last coded motion
61 vector.*/
62 #define OC_MODE_INTER_MV_LAST (3)
63 /*Encoded difference from the previous frame offset by the second to last
64 coded motion vector.*/
65 #define OC_MODE_INTER_MV_LAST2 (4)
66 /*Encoded difference from the previous frame offset by the individual motion
67 vectors given for each block.*/
68 #define OC_MODE_INTER_MV_FOUR (5)
69 /*Encoded difference from the same macro block in the previous golden frame.*/
70 #define OC_MODE_GOLDEN_NOMV (6)
71 /*Encoded difference from the previous golden frame offset by the given motion
72 vector.*/
73 #define OC_MODE_GOLDEN_MV (7)
74 /*The number of (coded) modes.*/
75 #define OC_NMODES (8)
77 /*Macro block is not coded.*/
78 #define OC_MODE_NOT_CODED (8)
80 /*Predictor bit flags.*/
81 /*Left.*/
82 #define OC_PL (1)
83 /*Upper-left.*/
84 #define OC_PUL (2)
85 /*Up.*/
86 #define OC_PU (4)
87 /*Upper-right.*/
88 #define OC_PUR (8)
90 /*Constants for the packet state machine common between encoder and decoder.*/
92 /*Next packet to emit/read: Codec info header.*/
93 #define OC_PACKET_INFO_HDR (-3)
94 /*Next packet to emit/read: Comment header.*/
95 #define OC_PACKET_COMMENT_HDR (-2)
96 /*Next packet to emit/read: Codec setup header.*/
97 #define OC_PACKET_SETUP_HDR (-1)
98 /*No more packets to emit/read.*/
99 #define OC_PACKET_DONE (INT_MAX)
103 typedef struct oc_theora_state oc_theora_state;
107 /*A map from a super block to fragment numbers.*/
108 typedef int oc_sb_map[4][4];
109 /*A map from a macro block to fragment numbers.*/
110 typedef int oc_mb_map[3][4];
111 /*A motion vector.*/
112 typedef char oc_mv[2];
116 /*Super block information.
117 Super blocks are 32x32 segments of pixels in a single color plane indexed
118 in image order.
119 Internally, super blocks are broken up into four quadrants, each of which
120 contains a 2x2 pattern of blocks, each of which is an 8x8 block of pixels.
121 Quadrants, and the blocks within them, are indexed in a special order called
122 a "Hilbert curve" within the super block.
124 In order to differentiate between the Hilbert-curve indexing strategy and
125 the regular image order indexing strategy, blocks indexed in image order
126 are called "fragments".
127 Fragments are indexed in image order, left to right, then bottom to top,
128 from Y plane to Cb plane to Cr plane.*/
129 typedef struct{
130 unsigned coded_fully:1;
131 unsigned coded_partially:1;
132 unsigned quad_valid:4;
133 oc_sb_map map;
134 }oc_sb;
138 /*Macro block information.
139 The co-located fragments in all image planes corresponding to the location of
140 a single luma plane super block quadrant forms a macro block.
141 Thus there is only a single set of macro blocks for all planes, which
142 contains between 6 and 12 fragments, depending on the pixel format.
143 Therefore macro block information is kept in a separate array from super
144 blocks, to avoid unused space in the other planes.*/
145 typedef struct{
146 /*The current macro block mode.
147 A negative number indicates the macro block lies entirely outside the
148 coded frame.*/
149 int mode;
150 /*The X location of the macro block's upper-left hand pixel.*/
151 int x;
152 /*The Y location of the macro block's upper-right hand pixel.*/
153 int y;
154 /*The fragments that belong to this macro block in each color plane.
155 Fragments are stored in image order (left to right then top to bottom).
156 When chroma components are decimated, the extra fragments have an index of
157 -1.*/
158 oc_mb_map map;
159 }oc_mb;
163 /*Information about a fragment which intersects the border of the displayable
164 region.
165 This marks which pixels belong to the displayable region, and is used to
166 ensure that pixels outside of this region are never referenced.
167 This allows applications to pass in buffers that are really the size of the
168 displayable region without causing a seg fault.*/
169 typedef struct{
170 /*A bit mask marking which pixels are in the displayable region.
171 Pixel (x,y) corresponds to bit (y<<3|x).*/
172 ogg_int64_t mask;
173 /*The number of pixels in the displayable region.
174 This is always positive, and always less than 64.*/
175 int npixels;
176 }oc_border_info;
180 /*Fragment information.*/
181 typedef struct{
182 /*A flag indicating whether or not this fragment is coded.*/
183 unsigned coded:1;
184 /*A flag indicating that all of this fragment lies outside the displayable
185 region of the frame.
186 Note the contrast with an invalid macro block, which is outside the coded
187 frame, not just the displayable one.*/
188 unsigned invalid:1;
189 /*The quality index used for this fragment's AC coefficients.*/
190 unsigned qi:6;
191 /*The mode of the macroblock this fragment belongs to.*/
192 int mbmode:8;
193 /*The prediction-corrected DC component.*/
194 int dc:16;
195 /*A pointer to the portion of an image covered by this fragment in several
196 images.
197 The first three are reconstructed frame buffers, while the last is the
198 input image buffer.
199 The appropriate stride value is determined by the color plane the fragment
200 belongs in.*/
201 unsigned char *buffer[4];
202 /*Information for fragments which lie partially outside the displayable
203 region.
204 For fragments completely inside or outside this region, this is NULL.*/
205 oc_border_info *border;
206 /*The motion vector used for this fragment.*/
207 oc_mv mv;
208 }oc_fragment;
212 /*A description of each fragment plane.*/
213 typedef struct{
214 /*The number of fragments in the horizontal direction.*/
215 int nhfrags;
216 /*The number of fragments in the vertical direction.*/
217 int nvfrags;
218 /*The offset of the first fragment in the plane.*/
219 int froffset;
220 /*The total number of fragments in the plane.*/
221 int nfrags;
222 /*The number of super blocks in the horizontal direction.*/
223 int nhsbs;
224 /*The number of super blocks in the vertical direction.*/
225 int nvsbs;
226 /*The offset of the first super block in the plane.*/
227 int sboffset;
228 /*The total number of super blocks in the plane.*/
229 int nsbs;
230 }oc_fragment_plane;
234 /*The shared (encoder and decoder) functions that have accelerated variants.*/
235 typedef struct{
236 void (*frag_recon_intra)(unsigned char *_dst,int _dst_ystride,
237 const ogg_int16_t *_residue);
238 void (*frag_recon_inter)(unsigned char *_dst,int _dst_ystride,
239 const unsigned char *_src,int _src_ystride,const ogg_int16_t *_residue);
240 void (*frag_recon_inter2)(unsigned char *_dst,int _dst_ystride,
241 const unsigned char *_src1,int _src1_ystride,const unsigned char *_src2,
242 int _src2_ystride,const ogg_int16_t *_residue);
243 void (*state_frag_copy)(const oc_theora_state *_state,
244 const int *_fragis,int _nfragis,int _dst_frame,int _src_frame,int _pli);
245 void (*state_frag_recon)(oc_theora_state *_state,const oc_fragment *_frag,
246 int _pli,ogg_int16_t _dct_coeffs[128],int _last_zzi,int _ncoefs,
247 ogg_uint16_t _dc_iquant,const ogg_uint16_t _ac_iquant[64]);
248 void (*state_loop_filter_frag_rows)(oc_theora_state *_state,int *_bv,
249 int _refi,int _pli,int _fragy0,int _fragy_end);
250 void (*restore_fpu)(void);
251 }oc_base_opt_vtable;
255 /*Common state information between the encoder and decoder.*/
256 struct oc_theora_state{
257 /*The stream information.*/
258 th_info info;
259 /*Table for shared accelerated functions.*/
260 oc_base_opt_vtable opt_vtable;
261 /*CPU flags to detect the presence of extended instruction sets.*/
262 ogg_uint32_t cpu_flags;
263 /*The fragment plane descriptions.*/
264 oc_fragment_plane fplanes[3];
265 /*The total number of fragments in a single frame.*/
266 int nfrags;
267 /*The list of fragments, indexed in image order.*/
268 oc_fragment *frags;
269 /*The total number of super blocks in a single frame.*/
270 int nsbs;
271 /*The list of super blocks, indexed in image order.*/
272 oc_sb *sbs;
273 /*The number of macro blocks in the X direction.*/
274 int nhmbs;
275 /*The number of macro blocks in the Y direction.*/
276 int nvmbs;
277 /*The total number of macro blocks.*/
278 int nmbs;
279 /*The list of macro blocks, indexed in super block order.
280 That is, the macro block corresponding to the macro block mbi in (luma
281 plane) super block sbi is (sbi<<2|mbi).*/
282 oc_mb *mbs;
283 /*The list of coded fragments, in coded order.*/
284 int *coded_fragis;
285 /*The number of coded fragments in each plane.*/
286 int ncoded_fragis[3];
287 /*The list of uncoded fragments.
288 This just past the end of the list, which is in reverse order, and
289 uses the same block of allocated storage as the coded_fragis list.*/
290 int *uncoded_fragis;
291 /*The number of uncoded fragments in each plane.*/
292 int nuncoded_fragis[3];
293 /*The list of coded macro blocks in the Y plane, in coded order.*/
294 int *coded_mbis;
295 /*The number of coded macro blocks in the Y plane.*/
296 int ncoded_mbis;
297 /*A copy of the image data used to fill the input pointers in each fragment.
298 If the data pointers or strides change, these input pointers must be
299 re-populated.*/
300 th_ycbcr_buffer input;
301 /*The number of unique border patterns.*/
302 int nborders;
303 /*The storage for the border info for all border fragments.
304 This data is pointed to from the appropriate fragments.*/
305 oc_border_info borders[16];
306 /*The index of the buffers being used for each OC_FRAME_* reference frame.*/
307 int ref_frame_idx[3];
308 /*The actual buffers used for the previously decoded frames.*/
309 th_ycbcr_buffer ref_frame_bufs[3];
310 /*The storage for the reference frame buffers.*/
311 unsigned char *ref_frame_data;
312 /*The frame number of the last keyframe.*/
313 ogg_int64_t keyframe_num;
314 /*The frame number of the current frame.*/
315 ogg_int64_t curframe_num;
316 /*The granpos of the current frame.*/
317 ogg_int64_t granpos;
318 /*The type of the current frame.*/
319 int frame_type;
320 /*The quality indices of the current frame.*/
321 int qis[3];
322 /*The number of quality indices used in the current frame.*/
323 int nqis;
324 /*The dequantization tables.*/
325 oc_quant_table *dequant_tables[2][3];
326 oc_quant_tables dequant_table_data[2][3];
327 /*Loop filter strength parameters.*/
328 unsigned char loop_filter_limits[64];
333 /*The function type used to fill in the chroma plane motion vectors for a
334 macro block when 4 different motion vectors are specified in the luma
335 plane.
336 _cbmvs: The chroma block-level motion vectors to fill in.
337 _lmbmv: The luma macro-block level motion vector to fill in for use in
338 prediction.
339 _lbmvs: The luma block-level motion vectors.*/
340 typedef void (*oc_set_chroma_mvs_func)(oc_mv _cbmvs[4],
341 const oc_mv _lbmvs[4]);
345 /*A map from the index in the zig zag scan to the coefficient number in a
346 block.
347 The extra 64 entries send out of bounds indexes to index 64.
348 This is used to safely ignore invalid zero runs when decoding
349 coefficients.*/
350 extern const int OC_FZIG_ZAG[128];
351 /*A map from the coefficient number in a block to its index in the zig zag
352 scan.*/
353 extern const int OC_IZIG_ZAG[64];
354 /*The predictor frame to use for each macro block mode.*/
355 extern const int OC_FRAME_FOR_MODE[OC_NMODES];
356 /*A map from physical macro block ordering to bitstream macro block
357 ordering within a super block.*/
358 extern const int OC_MB_MAP[2][2];
359 /*A list of the indices in the oc_mb.map array that can be valid for each of
360 the various chroma decimation types.*/
361 extern const int OC_MB_MAP_IDXS[TH_PF_NFORMATS][12];
362 /*The number of indices in the oc_mb.map array that can be valid for each of
363 the various chroma decimation types.*/
364 extern const int OC_MB_MAP_NIDXS[TH_PF_NFORMATS];
365 /*A table of functions used to fill in the Cb,Cr plane motion vectors for a
366 macro block when 4 different motion vectors are specified in the luma
367 plane.*/
368 extern const oc_set_chroma_mvs_func OC_SET_CHROMA_MVS_TABLE[TH_PF_NFORMATS];
372 int oc_ilog(unsigned _v);
373 void **oc_malloc_2d(size_t _height,size_t _width,size_t _sz);
374 void **oc_calloc_2d(size_t _height,size_t _width,size_t _sz);
375 void oc_free_2d(void *_ptr);
377 void oc_ycbcr_buffer_flip(th_ycbcr_buffer _dst,const th_ycbcr_buffer _src);
379 int oc_dct_token_skip(int _token,int _extra_bits);
381 int oc_frag_pred_dc(const oc_fragment *_frag,
382 const oc_fragment_plane *_fplane,int _x,int _y,int _pred_last[3]);
384 int oc_state_init(oc_theora_state *_state,const th_info *_info);
385 void oc_state_clear(oc_theora_state *_state);
386 void oc_state_vtable_init_c(oc_theora_state *_state);
387 void oc_state_borders_fill_rows(oc_theora_state *_state,int _refi,int _pli,
388 int _y0,int _yend);
389 void oc_state_borders_fill_caps(oc_theora_state *_state,int _refi,int _pli);
390 void oc_state_borders_fill(oc_theora_state *_state,int _refi);
391 void oc_state_fill_buffer_ptrs(oc_theora_state *_state,int _buf_idx,
392 th_ycbcr_buffer _img);
393 int oc_state_mbi_for_pos(oc_theora_state *_state,int _mbx,int _mby);
394 int oc_state_get_mv_offsets(oc_theora_state *_state,int *_offset0,
395 int *_offset1,int _dx,int _dy,int _ystride,int _pli);
397 int oc_state_loop_filter_init(oc_theora_state *_state,int *_bv);
398 void oc_state_loop_filter(oc_theora_state *_state,int _frame);
399 #if defined(OC_DUMP_IMAGES)
400 int oc_state_dump_frame(const oc_theora_state *_state,int _frame,
401 const char *_suf);
402 #endif
404 /*Shared accelerated functions.*/
405 void oc_frag_recon_intra(const oc_theora_state *_state,unsigned char *_dst,int _dst_ystride,
406 const ogg_int16_t *_residue);
407 void oc_frag_recon_inter(const oc_theora_state *_state,unsigned char *_dst,int _dst_ystride,
408 const unsigned char *_src,int _src_ystride,const ogg_int16_t *_residue);
409 void oc_frag_recon_inter2(const oc_theora_state *_state,unsigned char *_dst,int _dst_ystride,
410 const unsigned char *_src1,int _src1_ystride,const unsigned char *_src2,
411 int _src2_ystride,const ogg_int16_t *_residue);
412 void oc_state_frag_copy(const oc_theora_state *_state,const int *_fragis,
413 int _nfragis,int _dst_frame,int _src_frame,int _pli);
414 void oc_state_frag_recon(oc_theora_state *_state,const oc_fragment *_frag,
415 int _pli,ogg_int16_t _dct_coeffs[128],int _last_zzi,int _ncoefs,
416 ogg_uint16_t _dc_iquant,const ogg_uint16_t _ac_iquant[64]);
417 void oc_state_loop_filter_frag_rows(oc_theora_state *_state,int *_bv,
418 int _refi,int _pli,int _fragy0,int _fragy_end);
419 void oc_restore_fpu(const oc_theora_state *_state);
421 /*Default pure-C implementations.*/
422 void oc_frag_recon_intra_c(unsigned char *_dst,int _dst_ystride,
423 const ogg_int16_t *_residue);
424 void oc_frag_recon_inter_c(unsigned char *_dst,int _dst_ystride,
425 const unsigned char *_src,int _src_ystride,const ogg_int16_t *_residue);
426 void oc_frag_recon_inter2_c(unsigned char *_dst,int _dst_ystride,
427 const unsigned char *_src1,int _src1_ystride,const unsigned char *_src2,
428 int _src2_ystride,const ogg_int16_t *_residue);
429 void oc_state_frag_copy_c(const oc_theora_state *_state,const int *_fragis,
430 int _nfragis,int _dst_frame,int _src_frame,int _pli);
431 void oc_state_frag_recon_c(oc_theora_state *_state,const oc_fragment *_frag,
432 int _pli,ogg_int16_t _dct_coeffs[128],int _last_zzi,int _ncoefs,
433 ogg_uint16_t _dc_iquant,const ogg_uint16_t _ac_iquant[64]);
434 void oc_state_loop_filter_frag_rows_c(oc_theora_state *_state,int *_bv,
435 int _refi,int _pli,int _fragy0,int _fragy_end);
436 void oc_restore_fpu_c(void);
438 /*We need a way to call a few enocder functions without introducing a link-time
439 dependency into the decoder, while still allowing the old alpha API which
440 does not distinguish between encoder and decoder objects to be used.
441 We do this by placing a function table at the start of the encoder object
442 which can dispatch into the encoder library.*/
443 /*We don't ship theora.h, so we don't want to include it to force theora_state
444 to be defined; thus it is replaced by void * below.*/
445 typedef void (*oc_state_clear_func)(void *_th);
446 typedef int (*oc_state_control_func)(void *th,int req,
447 void *buf,size_t buf_sz);
448 typedef ogg_int64_t (*oc_state_granule_frame_func)(void *_th,
449 ogg_int64_t _granulepos);
450 typedef double (*oc_state_granule_time_func)(void *_th,
451 ogg_int64_t _granulepos);
453 typedef struct oc_state_dispatch_vtbl oc_state_dispatch_vtbl;
455 struct oc_state_dispatch_vtbl{
456 oc_state_clear_func clear;
457 oc_state_control_func control;
458 oc_state_granule_frame_func granule_frame;
459 oc_state_granule_time_func granule_time;
462 #endif