9 /*A map from the index in the zig zag scan to the coefficient number in a
11 All zig zag indices beyond 63 are sent to coefficient 64, so that zero runs
12 past the end of a block in bogus streams get mapped to a known location.*/
13 const int OC_FZIG_ZAG
[128]={
14 0, 1, 8,16, 9, 2, 3,10,
15 17,24,32,25,18,11, 4, 5,
16 12,19,26,33,40,48,41,34,
17 27,20,13, 6, 7,14,21,28,
18 35,42,49,56,57,50,43,36,
19 29,22,15,23,30,37,44,51,
20 58,59,52,45,38,31,39,46,
21 53,60,61,54,47,55,62,63,
22 64,64,64,64,64,64,64,64,
23 64,64,64,64,64,64,64,64,
24 64,64,64,64,64,64,64,64,
25 64,64,64,64,64,64,64,64,
26 64,64,64,64,64,64,64,64,
27 64,64,64,64,64,64,64,64,
28 64,64,64,64,64,64,64,64,
29 64,64,64,64,64,64,64,64
32 /*A map from the coefficient number in a block to its index in the zig zag
34 const int OC_IZIG_ZAG
[64]={
35 0, 1, 5, 6,14,15,27,28,
36 2, 4, 7,13,16,26,29,42,
37 3, 8,12,17,25,30,41,43,
38 9,11,18,24,31,40,44,53,
39 10,19,23,32,39,45,52,54,
40 20,22,33,38,46,51,55,60,
41 21,34,37,47,50,56,59,61,
42 35,36,48,49,57,58,62,63
45 /*The predictor frame to use for each macro block mode.*/
46 const int OC_FRAME_FOR_MODE
[8]={
49 /*OC_MODE_INTER_NOMV*/
53 /*OC_MODE_INTER_MV_LAST*/
55 /*OC_MODE_INTER_MV_LAST2*/
57 /*OC_MODE_INTER_MV_FOUR*/
65 /*A map from physical macro block ordering to bitstream macro block
66 ordering within a super block.*/
67 const int OC_MB_MAP
[2][2]={{0,3},{1,2}};
69 /*A list of the indices in the oc_mb.map array that can be valid for each of
70 the various chroma decimation types.*/
71 const int OC_MB_MAP_IDXS
[TH_PF_NFORMATS
][12]={
75 {0,1,2,3,4,5,6,7,8,9,10,11}
78 /*The number of indices in the oc_mb.map array that can be valid for each of
79 the various chroma decimation types.*/
80 const int OC_MB_MAP_NIDXS
[TH_PF_NFORMATS
]={6,8,8,12};
82 /*The number of extra bits that are coded with each of the DCT tokens.
83 Each DCT token has some fixed number of additional bits (possibly 0) stored
84 after the token itself, containing, for example, coefficient magnitude,
86 const int OC_DCT_TOKEN_EXTRA_BITS
[TH_NDCT_TOKENS
]={
89 1,1,1,1,2,3,4,5,6,10,
96 int oc_ilog(unsigned _v
){
98 for(ret
=0;_v
;ret
++)_v
>>=1;
104 /*Determines the number of blocks or coefficients to be skipped for a given
106 _token: The token value to skip.
107 _extra_bits: The extra bits attached to this token.
108 Return: A positive value indicates that number of coefficients are to be
109 skipped in the current block.
110 Otherwise, the negative of the return value indicates that number of
111 blocks are to be ended.*/
112 typedef int (*oc_token_skip_func
)(int _token
,int _extra_bits
);
114 /*Handles the simple end of block tokens.*/
115 static int oc_token_skip_eob(int _token
,int _extra_bits
){
116 static const int NBLOCKS_ADJUST
[OC_NDCT_EOB_TOKEN_MAX
]={1,2,3,4,8,16,0};
117 return -_extra_bits
-NBLOCKS_ADJUST
[_token
];
120 /*The last EOB token has a special case, where an EOB run of size zero ends all
121 the remaining blocks in the frame.*/
122 static int oc_token_skip_eob6(int _token
,int _extra_bits
){
123 if(!_extra_bits
)return -INT_MAX
;
127 /*Handles the pure zero run tokens.*/
128 static int oc_token_skip_zrl(int _token
,int _extra_bits
){
129 return _extra_bits
+1;
132 /*Handles a normal coefficient value token.*/
133 static int oc_token_skip_val(void){
137 /*Handles a category 1A zero run/coefficient value combo token.*/
138 static int oc_token_skip_run_cat1a(int _token
){
139 return _token
-OC_DCT_RUN_CAT1A
+2;
142 /*Handles category 1b and 2 zero run/coefficient value combo tokens.*/
143 static int oc_token_skip_run(int _token
,int _extra_bits
){
144 static const int NCOEFFS_ADJUST
[OC_NDCT_RUN_MAX
-OC_DCT_RUN_CAT1B
]={
147 static const int NCOEFFS_MASK
[OC_NDCT_RUN_MAX
-OC_DCT_RUN_CAT1B
]={
150 _token
-=OC_DCT_RUN_CAT1B
;
151 return (_extra_bits
&NCOEFFS_MASK
[_token
])+NCOEFFS_ADJUST
[_token
];
154 /*A jump table for computing the number of coefficients or blocks to skip for
156 This reduces all the conditional branches, etc., needed to parse these token
157 values down to one indirect jump.*/
158 static const oc_token_skip_func OC_TOKEN_SKIP_TABLE
[TH_NDCT_TOKENS
]={
168 (oc_token_skip_func
)oc_token_skip_val
,
169 (oc_token_skip_func
)oc_token_skip_val
,
170 (oc_token_skip_func
)oc_token_skip_val
,
171 (oc_token_skip_func
)oc_token_skip_val
,
172 (oc_token_skip_func
)oc_token_skip_val
,
173 (oc_token_skip_func
)oc_token_skip_val
,
174 (oc_token_skip_func
)oc_token_skip_val
,
175 (oc_token_skip_func
)oc_token_skip_val
,
176 (oc_token_skip_func
)oc_token_skip_val
,
177 (oc_token_skip_func
)oc_token_skip_val
,
178 (oc_token_skip_func
)oc_token_skip_val
,
179 (oc_token_skip_func
)oc_token_skip_val
,
180 (oc_token_skip_func
)oc_token_skip_val
,
181 (oc_token_skip_func
)oc_token_skip_val
,
182 (oc_token_skip_func
)oc_token_skip_run_cat1a
,
183 (oc_token_skip_func
)oc_token_skip_run_cat1a
,
184 (oc_token_skip_func
)oc_token_skip_run_cat1a
,
185 (oc_token_skip_func
)oc_token_skip_run_cat1a
,
186 (oc_token_skip_func
)oc_token_skip_run_cat1a
,
193 /*Determines the number of blocks or coefficients to be skipped for a given
195 _token: The token value to skip.
196 _extra_bits: The extra bits attached to this token.
197 Return: A positive value indicates that number of coefficients are to be
198 skipped in the current block.
199 Otherwise, the negative of the return value indicates that number of
200 blocks are to be ended.
201 0 will never be returned, so that at least one coefficient in one
202 block will always be decoded for every token.*/
203 int oc_dct_token_skip(int _token
,int _extra_bits
){
204 return (*OC_TOKEN_SKIP_TABLE
[_token
])(_token
,_extra_bits
);
208 /*The function used to fill in the chroma plane motion vectors for a macro
209 block when 4 different motion vectors are specified in the luma plane.
210 This version is for use with chroma decimated in the X and Y directions.
211 _cbmvs: The chroma block-level motion vectors to fill in.
212 _lbmvs: The luma block-level motion vectors.*/
213 static void oc_set_chroma_mvs00(oc_mv _cbmvs
[4],const oc_mv _lbmvs
[4]){
216 dx
=_lbmvs
[0][0]+_lbmvs
[1][0]+_lbmvs
[2][0]+_lbmvs
[3][0];
217 dy
=_lbmvs
[0][1]+_lbmvs
[1][1]+_lbmvs
[2][1]+_lbmvs
[3][1];
218 _cbmvs
[0][0]=(signed char)OC_DIV_ROUND_POW2(dx
,2,2);
219 _cbmvs
[0][1]=(signed char)OC_DIV_ROUND_POW2(dy
,2,2);
222 /*The function used to fill in the chroma plane motion vectors for a macro
223 block when 4 different motion vectors are specified in the luma plane.
224 This version is for use with chroma decimated in the Y direction.
225 _cbmvs: The chroma block-level motion vectors to fill in.
226 _lbmvs: The luma block-level motion vectors.*/
227 static void oc_set_chroma_mvs01(oc_mv _cbmvs
[4],const oc_mv _lbmvs
[4]){
230 dx
=_lbmvs
[0][0]+_lbmvs
[2][0];
231 dy
=_lbmvs
[0][1]+_lbmvs
[2][1];
232 _cbmvs
[0][0]=(signed char)OC_DIV_ROUND_POW2(dx
,1,1);
233 _cbmvs
[0][1]=(signed char)OC_DIV_ROUND_POW2(dy
,1,1);
234 dx
=_lbmvs
[1][0]+_lbmvs
[3][0];
235 dy
=_lbmvs
[1][1]+_lbmvs
[3][1];
236 _cbmvs
[1][0]=(signed char)OC_DIV_ROUND_POW2(dx
,1,1);
237 _cbmvs
[1][1]=(signed char)OC_DIV_ROUND_POW2(dy
,1,1);
240 /*The function used to fill in the chroma plane motion vectors for a macro
241 block when 4 different motion vectors are specified in the luma plane.
242 This version is for use with chroma decimated in the X direction.
243 _cbmvs: The chroma block-level motion vectors to fill in.
244 _lbmvs: The luma block-level motion vectors.*/
245 static void oc_set_chroma_mvs10(oc_mv _cbmvs
[4],const oc_mv _lbmvs
[4]){
248 dx
=_lbmvs
[0][0]+_lbmvs
[1][0];
249 dy
=_lbmvs
[0][1]+_lbmvs
[1][1];
250 _cbmvs
[0][0]=(signed char)OC_DIV_ROUND_POW2(dx
,1,1);
251 _cbmvs
[0][1]=(signed char)OC_DIV_ROUND_POW2(dy
,1,1);
252 dx
=_lbmvs
[2][0]+_lbmvs
[3][0];
253 dy
=_lbmvs
[2][1]+_lbmvs
[3][1];
254 _cbmvs
[2][0]=(signed char)OC_DIV_ROUND_POW2(dx
,1,1);
255 _cbmvs
[2][1]=(signed char)OC_DIV_ROUND_POW2(dy
,1,1);
258 /*The function used to fill in the chroma plane motion vectors for a macro
259 block when 4 different motion vectors are specified in the luma plane.
260 This version is for use with no chroma decimation.
261 _cbmvs: The chroma block-level motion vectors to fill in.
262 _lmbmv: The luma macro-block level motion vector to fill in for use in
264 _lbmvs: The luma block-level motion vectors.*/
265 static void oc_set_chroma_mvs11(oc_mv _cbmvs
[4],const oc_mv _lbmvs
[4]){
266 memcpy(_cbmvs
,_lbmvs
,4*sizeof(_lbmvs
[0]));
269 /*A table of functions used to fill in the chroma plane motion vectors for a
270 macro block when 4 different motion vectors are specified in the luma
272 const oc_set_chroma_mvs_func OC_SET_CHROMA_MVS_TABLE
[TH_PF_NFORMATS
]={
281 void **oc_malloc_2d(size_t _height
,size_t _width
,size_t _sz
){
286 colsz
=_height
*sizeof(void *);
289 /*Alloc array and row pointers.*/
290 ret
=(char *)_ogg_malloc(datsz
+colsz
);
291 /*Initialize the array.*/
298 for(datptr
=ret
+colsz
;i
-->0;p
++,datptr
+=rowsz
)*p
=(void *)datptr
;
303 void **oc_calloc_2d(size_t _height
,size_t _width
,size_t _sz
){
308 colsz
=_height
*sizeof(void *);
311 /*Alloc array and row pointers.*/
312 ret
=(char *)_ogg_calloc(datsz
+colsz
,1);
313 /*Initialize the array.*/
320 for(datptr
=ret
+colsz
;i
-->0;p
++,datptr
+=rowsz
)*p
=(void *)datptr
;
325 void oc_free_2d(void *_ptr
){
329 /*Fills in a Y'CbCr buffer with a pointer to the image data in the first
330 buffer, but with the opposite vertical orientation.
331 _dst: The destination buffer.
332 This can be the same as _src.
333 _src: The source buffer.*/
334 void oc_ycbcr_buffer_flip(th_ycbcr_buffer _dst
,const th_ycbcr_buffer _src
){
336 for(pli
=0;pli
<3;pli
++){
337 _dst
[pli
].width
=_src
[pli
].width
;
338 _dst
[pli
].height
=_src
[pli
].height
;
339 _dst
[pli
].ystride
=-_src
[pli
].ystride
;
340 _dst
[pli
].data
=_src
[pli
].data
+(1-_dst
[pli
].height
)*_dst
[pli
].ystride
;
344 const char *th_version_string(void){
345 return OC_VENDOR_STRING
;
348 ogg_uint32_t
th_version_number(void){
349 return (TH_VERSION_MAJOR
<<16)+(TH_VERSION_MINOR
<<8)+(TH_VERSION_SUB
);
352 /*Determines the packet type.
353 Note that this correctly interprets a 0-byte packet as a video data packet.
354 Return: 1 for a header packet, 0 for a data packet.*/
355 int th_packet_isheader(ogg_packet
*_op
){
356 return _op
->bytes
>0?_op
->packet
[0]>>7:0;
359 /*Determines the frame type of a video data packet.
360 Note that this correctly interprets a 0-byte packet as a delta frame.
361 Return: 1 for a key frame, 0 for a delta frame, and -1 for a header
363 int th_packet_iskeyframe(ogg_packet
*_op
){
364 return _op
->bytes
<=0?0:_op
->packet
[0]&0x80?-1:!(_op
->packet
[0]&0x40);