1 /********************************************************************
2 * *
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. *
7 * *
8 * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
9 * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
10 * *
11 ********************************************************************
12
13 function:
14 last mod: $Id$
15
16 ********************************************************************/
17
18 #include <stdlib.h>
19 #include <limits.h>
20 #include <string.h>
21 #include "internal.h"
22
23
24
25 /*A map from the index in the zig zag scan to the coefficient number in a
26 block.
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
46 };
47
48 /*A map from the coefficient number in a block to its index in the zig zag
49 scan.*/
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
59 };
60
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}};
64
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]={
68 {0,1,2,3,4,8},
69 {0,1,2,3,4,5,8,9},
70 {0,1,2,3,4,6,8,10},
71 {0,1,2,3,4,5,6,7,8,9,10,11}
72 };
73
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};
77
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,
81 sign bits, etc.*/
82 const unsigned char OC_DCT_TOKEN_EXTRA_BITS[TH_NDCT_TOKENS]={
83 0,0,0,2,3,4,12,3,6,
84 0,0,0,0,
85 1,1,1,1,2,3,4,5,6,10,
86 1,1,1,1,1,3,4,
87 2,3
88 };
89
90
91
oc_ilog(unsigned _v)92 int oc_ilog(unsigned _v){
93 int ret;
94 for(ret=0;_v;ret++)_v>>=1;
95 return ret;
96 }
97
98
99
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
103 (4:2:0).
104 _cbmvs: The chroma block-level motion vectors to fill in.
105 _lbmvs: The luma block-level motion vectors.*/
oc_set_chroma_mvs00(oc_mv _cbmvs[4],const oc_mv _lbmvs[4])106 static void oc_set_chroma_mvs00(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
107 int dx;
108 int dy;
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);
113 }
114
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.*/
oc_set_chroma_mvs01(oc_mv _cbmvs[4],const oc_mv _lbmvs[4])120 static void oc_set_chroma_mvs01(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
121 int dx;
122 int dy;
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);
131 }
132
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.*/
oc_set_chroma_mvs10(oc_mv _cbmvs[4],const oc_mv _lbmvs[4])138 static void oc_set_chroma_mvs10(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
139 int dx;
140 int dy;
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);
149 }
150
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
156 prediction.
157 _lbmvs: The luma block-level motion vectors.*/
oc_set_chroma_mvs11(oc_mv _cbmvs[4],const oc_mv _lbmvs[4])158 static void oc_set_chroma_mvs11(oc_mv _cbmvs[4],const oc_mv _lbmvs[4]){
159 memcpy(_cbmvs,_lbmvs,4*sizeof(_lbmvs[0]));
160 }
161
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
164 plane.*/
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
170 };
171
172
173
oc_malloc_2d(size_t _height,size_t _width,size_t _sz)174 void **oc_malloc_2d(size_t _height,size_t _width,size_t _sz){
175 size_t rowsz;
176 size_t colsz;
177 size_t datsz;
178 char *ret;
179 colsz=_height*sizeof(void *);
180 rowsz=_sz*_width;
181 datsz=rowsz*_height;
182 /*Alloc array and row pointers.*/
183 ret=(char *)_ogg_malloc(datsz+colsz);
184 if(ret==NULL)return NULL;
185 /*Initialize the array.*/
186 if(ret!=NULL){
187 size_t i;
188 void **p;
189 char *datptr;
190 p=(void **)ret;
191 i=_height;
192 for(datptr=ret+colsz;i-->0;p++,datptr+=rowsz)*p=(void *)datptr;
193 }
194 return (void **)ret;
195 }
196
oc_calloc_2d(size_t _height,size_t _width,size_t _sz)197 void **oc_calloc_2d(size_t _height,size_t _width,size_t _sz){
198 size_t colsz;
199 size_t rowsz;
200 size_t datsz;
201 char *ret;
202 colsz=_height*sizeof(void *);
203 rowsz=_sz*_width;
204 datsz=rowsz*_height;
205 /*Alloc array and row pointers.*/
206 ret=(char *)_ogg_calloc(datsz+colsz,1);
207 if(ret==NULL)return NULL;
208 /*Initialize the array.*/
209 if(ret!=NULL){
210 size_t i;
211 void **p;
212 char *datptr;
213 p=(void **)ret;
214 i=_height;
215 for(datptr=ret+colsz;i-->0;p++,datptr+=rowsz)*p=(void *)datptr;
216 }
217 return (void **)ret;
218 }
219
oc_free_2d(void * _ptr)220 void oc_free_2d(void *_ptr){
221 _ogg_free(_ptr);
222 }
223
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.*/
oc_ycbcr_buffer_flip(th_ycbcr_buffer _dst,const th_ycbcr_buffer _src)229 void oc_ycbcr_buffer_flip(th_ycbcr_buffer _dst,
230 const th_ycbcr_buffer _src){
231 int pli;
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;
238 }
239 }
240
th_version_string(void)241 const char *th_version_string(void){
242 return OC_VENDOR_STRING;
243 }
244
th_version_number(void)245 ogg_uint32_t th_version_number(void){
246 return (TH_VERSION_MAJOR<<16)+(TH_VERSION_MINOR<<8)+TH_VERSION_SUB;
247 }
248
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.*/
th_packet_isheader(ogg_packet * _op)252 int th_packet_isheader(ogg_packet *_op){
253 return _op->bytes>0?_op->packet[0]>>7:0;
254 }
255
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
259 packet.*/
th_packet_iskeyframe(ogg_packet * _op)260 int th_packet_iskeyframe(ogg_packet *_op){
261 return _op->bytes<=0?0:_op->packet[0]&0x80?-1:!(_op->packet[0]&0x40);
262 }
263