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.c 14385 2008-01-09 19:53:18Z giles $
16 ********************************************************************/
21 #include "../internal.h"
26 /*A map from the index in the zig zag scan to the coefficient number in a
28 All zig zag indices beyond 63 are sent to coefficient 64, so that zero runs
29 past the end of a block in bogus streams get mapped to a known location.*/
30 const int OC_FZIG_ZAG
[128]={
31 0, 1, 8,16, 9, 2, 3,10,
32 17,24,32,25,18,11, 4, 5,
33 12,19,26,33,40,48,41,34,
34 27,20,13, 6, 7,14,21,28,
35 35,42,49,56,57,50,43,36,
36 29,22,15,23,30,37,44,51,
37 58,59,52,45,38,31,39,46,
38 53,60,61,54,47,55,62,63,
39 64,64,64,64,64,64,64,64,
40 64,64,64,64,64,64,64,64,
41 64,64,64,64,64,64,64,64,
42 64,64,64,64,64,64,64,64,
43 64,64,64,64,64,64,64,64,
44 64,64,64,64,64,64,64,64,
45 64,64,64,64,64,64,64,64,
46 64,64,64,64,64,64,64,64
49 /*A map from the coefficient number in a block to its index in the zig zag
51 const int OC_IZIG_ZAG
[64]={
52 0, 1, 5, 6,14,15,27,28,
53 2, 4, 7,13,16,26,29,42,
54 3, 8,12,17,25,30,41,43,
55 9,11,18,24,31,40,44,53,
56 10,19,23,32,39,45,52,54,
57 20,22,33,38,46,51,55,60,
58 21,34,37,47,50,56,59,61,
59 35,36,48,49,57,58,62,63
62 /*The predictor frame to use for each macro block mode.*/
63 const int OC_FRAME_FOR_MODE
[8]={
64 /*OC_MODE_INTER_NOMV*/
70 /*OC_MODE_INTER_MV_LAST*/
72 /*OC_MODE_INTER_MV_LAST2*/
78 /*OC_MODE_INTER_MV_FOUR*/
82 /*A map from physical macro block ordering to bitstream macro block
83 ordering within a super block.*/
84 const int OC_MB_MAP
[2][2]={{0,3},{1,2}};
86 /*A list of the indices in the oc_mb.map array that can be valid for each of
87 the various chroma decimation types.*/
88 const int OC_MB_MAP_IDXS
[TH_PF_NFORMATS
][12]={
92 {0,1,2,3,4,5,6,7,8,9,10,11}
95 /*The number of indices in the oc_mb.map array that can be valid for each of
96 the various chroma decimation types.*/
97 const int OC_MB_MAP_NIDXS
[TH_PF_NFORMATS
]={6,8,8,12};
99 /*The number of extra bits that are coded with each of the DCT tokens.
100 Each DCT token has some fixed number of additional bits (possibly 0) stored
101 after the token itself, containing, for example, coefficient magnitude,
103 const int OC_DCT_TOKEN_EXTRA_BITS
[TH_NDCT_TOKENS
]={
106 1,1,1,1,2,3,4,5,6,10,
113 int oc_ilog(unsigned _v
){
115 for(ret
=0;_v
;ret
++)_v
>>=1;
121 /*Determines the number of blocks or coefficients to be skipped for a given
123 _token: The token value to skip.
124 _extra_bits: The extra bits attached to this token.
125 Return: A positive value indicates that number of coefficients are to be
126 skipped in the current block.
127 Otherwise, the negative of the return value indicates that number of
128 blocks are to be ended.*/
129 typedef int (*oc_token_skip_func
)(int _token
,int _extra_bits
);
131 /*Handles the simple end of block tokens.*/
132 static int oc_token_skip_eob(int _token
,int _extra_bits
){
133 static const int NBLOCKS_ADJUST
[OC_NDCT_EOB_TOKEN_MAX
]={1,2,3,4,8,16,0};
134 return -_extra_bits
-NBLOCKS_ADJUST
[_token
];
137 /*The last EOB token has a special case, where an EOB run of size zero ends all
138 the remaining blocks in the frame.*/
139 static int oc_token_skip_eob6(int _token
,int _extra_bits
){
140 if(!_extra_bits
)return -INT_MAX
;
144 /*Handles the pure zero run tokens.*/
145 static int oc_token_skip_zrl(int _token
,int _extra_bits
){
146 return _extra_bits
+1;
149 /*Handles a normal coefficient value token.*/
150 static int oc_token_skip_val(void){
154 /*Handles a category 1A zero run/coefficient value combo token.*/
155 static int oc_token_skip_run_cat1a(int _token
){
156 return _token
-OC_DCT_RUN_CAT1A
+2;
159 /*Handles category 1b and 2 zero run/coefficient value combo tokens.*/
160 static int oc_token_skip_run(int _token
,int _extra_bits
){
161 static const int NCOEFFS_ADJUST
[OC_NDCT_RUN_MAX
-OC_DCT_RUN_CAT1B
]={
164 static const int NCOEFFS_MASK
[OC_NDCT_RUN_MAX
-OC_DCT_RUN_CAT1B
]={
167 _token
-=OC_DCT_RUN_CAT1B
;
168 return (_extra_bits
&NCOEFFS_MASK
[_token
])+NCOEFFS_ADJUST
[_token
];
171 /*A jump table for computing the number of coefficients or blocks to skip for
173 This reduces all the conditional branches, etc., needed to parse these token
174 values down to one indirect jump.*/
175 static const oc_token_skip_func OC_TOKEN_SKIP_TABLE
[TH_NDCT_TOKENS
]={
185 (oc_token_skip_func
)oc_token_skip_val
,
186 (oc_token_skip_func
)oc_token_skip_val
,
187 (oc_token_skip_func
)oc_token_skip_val
,
188 (oc_token_skip_func
)oc_token_skip_val
,
189 (oc_token_skip_func
)oc_token_skip_val
,
190 (oc_token_skip_func
)oc_token_skip_val
,
191 (oc_token_skip_func
)oc_token_skip_val
,
192 (oc_token_skip_func
)oc_token_skip_val
,
193 (oc_token_skip_func
)oc_token_skip_val
,
194 (oc_token_skip_func
)oc_token_skip_val
,
195 (oc_token_skip_func
)oc_token_skip_val
,
196 (oc_token_skip_func
)oc_token_skip_val
,
197 (oc_token_skip_func
)oc_token_skip_val
,
198 (oc_token_skip_func
)oc_token_skip_val
,
199 (oc_token_skip_func
)oc_token_skip_run_cat1a
,
200 (oc_token_skip_func
)oc_token_skip_run_cat1a
,
201 (oc_token_skip_func
)oc_token_skip_run_cat1a
,
202 (oc_token_skip_func
)oc_token_skip_run_cat1a
,
203 (oc_token_skip_func
)oc_token_skip_run_cat1a
,
210 /*Determines the number of blocks or coefficients to be skipped for a given
212 _token: The token value to skip.
213 _extra_bits: The extra bits attached to this token.
214 Return: A positive value indicates that number of coefficients are to be
215 skipped in the current block.
216 Otherwise, the negative of the return value indicates that number of
217 blocks are to be ended.
218 0 will never be returned, so that at least one coefficient in one
219 block will always be decoded for every token.*/
220 int oc_dct_token_skip(int _token
,int _extra_bits
){
221 return (*OC_TOKEN_SKIP_TABLE
[_token
])(_token
,_extra_bits
);
225 /*The function used to fill in the chroma plane motion vectors for a macro
226 block when 4 different motion vectors are specified in the luma plane.
227 This version is for use with chroma decimated in the X and Y directions.
228 _cbmvs: The chroma block-level motion vectors to fill in.
229 _lbmvs: The luma block-level motion vectors.*/
230 static void oc_set_chroma_mvs00(oc_mv _cbmvs
[4],const oc_mv _lbmvs
[4]){
233 dx
=_lbmvs
[0][0]+_lbmvs
[1][0]+_lbmvs
[2][0]+_lbmvs
[3][0];
234 dy
=_lbmvs
[0][1]+_lbmvs
[1][1]+_lbmvs
[2][1]+_lbmvs
[3][1];
235 _cbmvs
[0][0]=(signed char)OC_DIV_ROUND_POW2(dx
,2,2);
236 _cbmvs
[0][1]=(signed char)OC_DIV_ROUND_POW2(dy
,2,2);
239 /*The function used to fill in the chroma plane motion vectors for a macro
240 block when 4 different motion vectors are specified in the luma plane.
241 This version is for use with chroma decimated in the Y direction.
242 _cbmvs: The chroma block-level motion vectors to fill in.
243 _lbmvs: The luma block-level motion vectors.*/
244 static void oc_set_chroma_mvs01(oc_mv _cbmvs
[4],const oc_mv _lbmvs
[4]){
247 dx
=_lbmvs
[0][0]+_lbmvs
[2][0];
248 dy
=_lbmvs
[0][1]+_lbmvs
[2][1];
249 _cbmvs
[0][0]=(signed char)OC_DIV_ROUND_POW2(dx
,1,1);
250 _cbmvs
[0][1]=(signed char)OC_DIV_ROUND_POW2(dy
,1,1);
251 dx
=_lbmvs
[1][0]+_lbmvs
[3][0];
252 dy
=_lbmvs
[1][1]+_lbmvs
[3][1];
253 _cbmvs
[1][0]=(signed char)OC_DIV_ROUND_POW2(dx
,1,1);
254 _cbmvs
[1][1]=(signed char)OC_DIV_ROUND_POW2(dy
,1,1);
257 /*The function used to fill in the chroma plane motion vectors for a macro
258 block when 4 different motion vectors are specified in the luma plane.
259 This version is for use with chroma decimated in the X direction.
260 _cbmvs: The chroma block-level motion vectors to fill in.
261 _lbmvs: The luma block-level motion vectors.*/
262 static void oc_set_chroma_mvs10(oc_mv _cbmvs
[4],const oc_mv _lbmvs
[4]){
265 dx
=_lbmvs
[0][0]+_lbmvs
[1][0];
266 dy
=_lbmvs
[0][1]+_lbmvs
[1][1];
267 _cbmvs
[0][0]=(signed char)OC_DIV_ROUND_POW2(dx
,1,1);
268 _cbmvs
[0][1]=(signed char)OC_DIV_ROUND_POW2(dy
,1,1);
269 dx
=_lbmvs
[2][0]+_lbmvs
[3][0];
270 dy
=_lbmvs
[2][1]+_lbmvs
[3][1];
271 _cbmvs
[2][0]=(signed char)OC_DIV_ROUND_POW2(dx
,1,1);
272 _cbmvs
[2][1]=(signed char)OC_DIV_ROUND_POW2(dy
,1,1);
275 /*The function used to fill in the chroma plane motion vectors for a macro
276 block when 4 different motion vectors are specified in the luma plane.
277 This version is for use with no chroma decimation.
278 _cbmvs: The chroma block-level motion vectors to fill in.
279 _lmbmv: The luma macro-block level motion vector to fill in for use in
281 _lbmvs: The luma block-level motion vectors.*/
282 static void oc_set_chroma_mvs11(oc_mv _cbmvs
[4],const oc_mv _lbmvs
[4]){
283 memcpy(_cbmvs
,_lbmvs
,4*sizeof(_lbmvs
[0]));
286 /*A table of functions used to fill in the chroma plane motion vectors for a
287 macro block when 4 different motion vectors are specified in the luma
289 const oc_set_chroma_mvs_func OC_SET_CHROMA_MVS_TABLE
[TH_PF_NFORMATS
]={
290 (oc_set_chroma_mvs_func
)oc_set_chroma_mvs00
,
291 (oc_set_chroma_mvs_func
)oc_set_chroma_mvs01
,
292 (oc_set_chroma_mvs_func
)oc_set_chroma_mvs10
,
293 (oc_set_chroma_mvs_func
)oc_set_chroma_mvs11
298 void **oc_malloc_2d(size_t _height
,size_t _width
,size_t _sz
){
303 colsz
=_height
*sizeof(void *);
306 /*Alloc array and row pointers.*/
307 ret
=(char *)_ogg_malloc(datsz
+colsz
);
308 /*Initialize the array.*/
315 for(datptr
=ret
+colsz
;i
-->0;p
++,datptr
+=rowsz
)*p
=(void *)datptr
;
320 void **oc_calloc_2d(size_t _height
,size_t _width
,size_t _sz
){
325 colsz
=_height
*sizeof(void *);
328 /*Alloc array and row pointers.*/
329 ret
=(char *)_ogg_calloc(datsz
+colsz
,1);
330 /*Initialize the array.*/
337 for(datptr
=ret
+colsz
;i
-->0;p
++,datptr
+=rowsz
)*p
=(void *)datptr
;
342 void oc_free_2d(void *_ptr
){
346 /*Fills in a Y'CbCr buffer with a pointer to the image data in the first
347 buffer, but with the opposite vertical orientation.
348 _dst: The destination buffer.
349 This can be the same as _src.
350 _src: The source buffer.*/
351 void oc_ycbcr_buffer_flip(th_ycbcr_buffer _dst
,
352 const th_ycbcr_buffer _src
){
354 for(pli
=0;pli
<3;pli
++){
355 _dst
[pli
].width
=_src
[pli
].width
;
356 _dst
[pli
].height
=_src
[pli
].height
;
357 _dst
[pli
].stride
=-_src
[pli
].stride
;
358 _dst
[pli
].data
=_src
[pli
].data
+(1-_dst
[pli
].height
)*_dst
[pli
].stride
;
362 const char *th_version_string(void){
363 return OC_VENDOR_STRING
;
366 ogg_uint32_t
th_version_number(void){
367 return (TH_VERSION_MAJOR
<<16)+(TH_VERSION_MINOR
<<8)+(TH_VERSION_SUB
);
370 /*Determines the packet type.
371 Note that this correctly interprets a 0-byte packet as a video data packet.
372 Return: 1 for a header packet, 0 for a data packet.*/
373 int th_packet_isheader(ogg_packet
*_op
){
374 return _op
->bytes
>0?_op
->packet
[0]>>7:0;
377 /*Determines the frame type of a video data packet.
378 Note that this correctly interprets a 0-byte packet as a delta frame.
379 Return: 1 for a key frame, 0 for a delta frame, and -1 for a header
381 int th_packet_iskeyframe(ogg_packet
*_op
){
382 return _op
->bytes
<=0?0:_op
->packet
[0]&0x80?-1:!(_op
->packet
[0]&0x40);