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-2009 *
9 * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
11 ********************************************************************
14 last mod: $Id: internal.c 16503 2009-08-22 18:14:02Z giles $
16 ********************************************************************/
25 /*A map from the index in the zig zag scan to the coefficient number in a
27 All zig zag indices beyond 63 are sent to coefficient 64, so that zero runs
28 past the end of a block in bogus streams get mapped to a known location.*/
29 const unsigned char OC_FZIG_ZAG[128]={
30 0, 1, 8,16, 9, 2, 3,10,
31 17,24,32,25,18,11, 4, 5,
32 12,19,26,33,40,48,41,34,
33 27,20,13, 6, 7,14,21,28,
34 35,42,49,56,57,50,43,36,
35 29,22,15,23,30,37,44,51,
36 58,59,52,45,38,31,39,46,
37 53,60,61,54,47,55,62,63,
38 64,64,64,64,64,64,64,64,
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
48 /*A map from the coefficient number in a block to its index in the zig zag
50 const unsigned char OC_IZIG_ZAG[64]={
51 0, 1, 5, 6,14,15,27,28,
52 2, 4, 7,13,16,26,29,42,
53 3, 8,12,17,25,30,41,43,
54 9,11,18,24,31,40,44,53,
55 10,19,23,32,39,45,52,54,
56 20,22,33,38,46,51,55,60,
57 21,34,37,47,50,56,59,61,
58 35,36,48,49,57,58,62,63
61 /*A map from physical macro block ordering to bitstream macro block
62 ordering within a super block.*/
63 const unsigned char OC_MB_MAP[2][2]={{0,3},{1,2}};
65 /*A list of the indices in the oc_mb.map array that can be valid for each of
66 the various chroma decimation types.*/
67 const unsigned char OC_MB_MAP_IDXS[TH_PF_NFORMATS][12]={
71 {0,1,2,3,4,5,6,7,8,9,10,11}
74 /*The number of indices in the oc_mb.map array that can be valid for each of
75 the various chroma decimation types.*/
76 const unsigned char OC_MB_MAP_NIDXS[TH_PF_NFORMATS]={6,8,8,12};
78 /*The number of extra bits that are coded with each of the DCT tokens.
79 Each DCT token has some fixed number of additional bits (possibly 0) stored
80 after the token itself, containing, for example, coefficient magnitude,
82 const unsigned char OC_DCT_TOKEN_EXTRA_BITS[TH_NDCT_TOKENS]={
85 1,1,1,1,2,3,4,5,6,10,
92 int oc_ilog(unsigned _v){
94 for(ret=0;_v;ret++)_v>>=1;
100 /*The function used to fill in the chroma plane motion vectors for a macro
101 block when 4 different motion vectors are specified in the luma plane.
102 This version is for use with chroma decimated in the X and Y directions
104 _cbmvs: The chroma block-level motion vectors to fill in.
105 _lbmvs: The luma block-level motion vectors.*/
106 static void oc_set_chroma_mvs00(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
109 dx=_lbmvs[0][0]+_lbmvs[1][0]+_lbmvs[2][0]+_lbmvs[3][0];
110 dy=_lbmvs[0][1]+_lbmvs[1][1]+_lbmvs[2][1]+_lbmvs[3][1];
111 _cbmvs[0][0]=(signed char)OC_DIV_ROUND_POW2(dx,2,2);
112 _cbmvs[0][1]=(signed char)OC_DIV_ROUND_POW2(dy,2,2);
115 /*The function used to fill in the chroma plane motion vectors for a macro
116 block when 4 different motion vectors are specified in the luma plane.
117 This version is for use with chroma decimated in the Y direction.
118 _cbmvs: The chroma block-level motion vectors to fill in.
119 _lbmvs: The luma block-level motion vectors.*/
120 static void oc_set_chroma_mvs01(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
123 dx=_lbmvs[0][0]+_lbmvs[2][0];
124 dy=_lbmvs[0][1]+_lbmvs[2][1];
125 _cbmvs[0][0]=(signed char)OC_DIV_ROUND_POW2(dx,1,1);
126 _cbmvs[0][1]=(signed char)OC_DIV_ROUND_POW2(dy,1,1);
127 dx=_lbmvs[1][0]+_lbmvs[3][0];
128 dy=_lbmvs[1][1]+_lbmvs[3][1];
129 _cbmvs[1][0]=(signed char)OC_DIV_ROUND_POW2(dx,1,1);
130 _cbmvs[1][1]=(signed char)OC_DIV_ROUND_POW2(dy,1,1);
133 /*The function used to fill in the chroma plane motion vectors for a macro
134 block when 4 different motion vectors are specified in the luma plane.
135 This version is for use with chroma decimated in the X direction (4:2:2).
136 _cbmvs: The chroma block-level motion vectors to fill in.
137 _lbmvs: The luma block-level motion vectors.*/
138 static void oc_set_chroma_mvs10(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
141 dx=_lbmvs[0][0]+_lbmvs[1][0];
142 dy=_lbmvs[0][1]+_lbmvs[1][1];
143 _cbmvs[0][0]=(signed char)OC_DIV_ROUND_POW2(dx,1,1);
144 _cbmvs[0][1]=(signed char)OC_DIV_ROUND_POW2(dy,1,1);
145 dx=_lbmvs[2][0]+_lbmvs[3][0];
146 dy=_lbmvs[2][1]+_lbmvs[3][1];
147 _cbmvs[2][0]=(signed char)OC_DIV_ROUND_POW2(dx,1,1);
148 _cbmvs[2][1]=(signed char)OC_DIV_ROUND_POW2(dy,1,1);
151 /*The function used to fill in the chroma plane motion vectors for a macro
152 block when 4 different motion vectors are specified in the luma plane.
153 This version is for use with no chroma decimation (4:4:4).
154 _cbmvs: The chroma block-level motion vectors to fill in.
155 _lmbmv: The luma macro-block level motion vector to fill in for use in
157 _lbmvs: The luma block-level motion vectors.*/
158 static void oc_set_chroma_mvs11(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
159 memcpy(_cbmvs,_lbmvs,4*sizeof(_lbmvs[0]));
162 /*A table of functions used to fill in the chroma plane motion vectors for a
163 macro block when 4 different motion vectors are specified in the luma
165 const oc_set_chroma_mvs_func OC_SET_CHROMA_MVS_TABLE[TH_PF_NFORMATS]={
166 (oc_set_chroma_mvs_func)oc_set_chroma_mvs00,
167 (oc_set_chroma_mvs_func)oc_set_chroma_mvs01,
168 (oc_set_chroma_mvs_func)oc_set_chroma_mvs10,
169 (oc_set_chroma_mvs_func)oc_set_chroma_mvs11
174 void **oc_malloc_2d(size_t _height,size_t _width,size_t _sz){
179 colsz=_height*sizeof(void *);
182 /*Alloc array and row pointers.*/
183 ret=(char *)_ogg_malloc(datsz+colsz);
184 if(ret==NULL)return NULL;
185 /*Initialize the array.*/
192 for(datptr=ret+colsz;i-->0;p++,datptr+=rowsz)*p=(void *)datptr;
197 void **oc_calloc_2d(size_t _height,size_t _width,size_t _sz){
202 colsz=_height*sizeof(void *);
205 /*Alloc array and row pointers.*/
206 ret=(char *)_ogg_calloc(datsz+colsz,1);
207 if(ret==NULL)return NULL;
208 /*Initialize the array.*/
215 for(datptr=ret+colsz;i-->0;p++,datptr+=rowsz)*p=(void *)datptr;
220 void oc_free_2d(void *_ptr){
224 /*Fills in a Y'CbCr buffer with a pointer to the image data in the first
225 buffer, but with the opposite vertical orientation.
226 _dst: The destination buffer.
227 This can be the same as _src.
228 _src: The source buffer.*/
229 void oc_ycbcr_buffer_flip(th_ycbcr_buffer _dst,
230 const th_ycbcr_buffer _src){
232 for(pli=0;pli<3;pli++){
233 _dst[pli].width=_src[pli].width;
234 _dst[pli].height=_src[pli].height;
235 _dst[pli].stride=-_src[pli].stride;
236 _dst[pli].data=_src[pli].data
237 +(1-_dst[pli].height)*(ptrdiff_t)_dst[pli].stride;
241 const char *th_version_string(void){
242 return OC_VENDOR_STRING;
245 ogg_uint32_t th_version_number(void){
246 return (TH_VERSION_MAJOR<<16)+(TH_VERSION_MINOR<<8)+TH_VERSION_SUB;
249 /*Determines the packet type.
250 Note that this correctly interprets a 0-byte packet as a video data packet.
251 Return: 1 for a header packet, 0 for a data packet.*/
252 int th_packet_isheader(ogg_packet *_op){
253 return _op->bytes>0?_op->packet[0]>>7:0;
256 /*Determines the frame type of a video data packet.
257 Note that this correctly interprets a 0-byte packet as a delta frame.
258 Return: 1 for a key frame, 0 for a delta frame, and -1 for a header
260 int th_packet_iskeyframe(ogg_packet *_op){
261 return _op->bytes<=0?0:_op->packet[0]&0x80?-1:!(_op->packet[0]&0x40);