/src/theora/lib/internal.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
92 | 0 | int oc_ilog(unsigned _v){ |
93 | 0 | int ret; |
94 | 0 | for(ret=0;_v;ret++)_v>>=1; |
95 | 0 | return ret; |
96 | 0 | } |
97 | | |
98 | | |
99 | | |
100 | 6.89k | void *oc_aligned_malloc(size_t _sz,size_t _align){ |
101 | 6.89k | unsigned char *p; |
102 | 6.89k | if(_align-1>UCHAR_MAX||(_align&_align-1)||_sz>~(size_t)0-_align)return NULL; |
103 | 6.89k | p=(unsigned char *)_ogg_malloc(_sz+_align); |
104 | 6.89k | if(p!=NULL){ |
105 | 6.89k | int offs; |
106 | 6.89k | offs=((p-(unsigned char *)0)-1&_align-1); |
107 | 6.89k | p[offs]=offs; |
108 | 6.89k | p+=offs+1; |
109 | 6.89k | } |
110 | 6.89k | return p; |
111 | 6.89k | } |
112 | | |
113 | 6.89k | void oc_aligned_free(void *_ptr){ |
114 | 6.89k | unsigned char *p; |
115 | 6.89k | p=(unsigned char *)_ptr; |
116 | 6.89k | if(p!=NULL){ |
117 | 6.89k | int offs; |
118 | 6.89k | offs=*--p; |
119 | 6.89k | _ogg_free(p-offs); |
120 | 6.89k | } |
121 | 6.89k | } |
122 | | |
123 | | |
124 | 20.6k | void **oc_malloc_2d(size_t _height,size_t _width,size_t _sz){ |
125 | 20.6k | size_t rowsz; |
126 | 20.6k | size_t colsz; |
127 | 20.6k | size_t datsz; |
128 | 20.6k | char *ret; |
129 | 20.6k | colsz=_height*sizeof(void *); |
130 | 20.6k | rowsz=_sz*_width; |
131 | 20.6k | datsz=rowsz*_height; |
132 | | /*Alloc array and row pointers.*/ |
133 | 20.6k | ret=(char *)_ogg_malloc(datsz+colsz); |
134 | | /*Initialize the array.*/ |
135 | 20.6k | if(ret!=NULL){ |
136 | 20.6k | size_t i; |
137 | 20.6k | void **p; |
138 | 20.6k | char *datptr; |
139 | 20.6k | p=(void **)ret; |
140 | 20.6k | i=_height; |
141 | 1.34M | for(datptr=ret+colsz;i-->0;p++,datptr+=rowsz)*p=(void *)datptr; |
142 | 20.6k | } |
143 | 20.6k | return (void **)ret; |
144 | 20.6k | } |
145 | | |
146 | 0 | void **oc_calloc_2d(size_t _height,size_t _width,size_t _sz){ |
147 | 0 | size_t colsz; |
148 | 0 | size_t rowsz; |
149 | 0 | size_t datsz; |
150 | 0 | char *ret; |
151 | 0 | colsz=_height*sizeof(void *); |
152 | 0 | rowsz=_sz*_width; |
153 | 0 | datsz=rowsz*_height; |
154 | | /*Alloc array and row pointers.*/ |
155 | 0 | ret=(char *)_ogg_calloc(datsz+colsz,1); |
156 | | /*Initialize the array.*/ |
157 | 0 | if(ret!=NULL){ |
158 | 0 | size_t i; |
159 | 0 | void **p; |
160 | 0 | char *datptr; |
161 | 0 | p=(void **)ret; |
162 | 0 | i=_height; |
163 | 0 | for(datptr=ret+colsz;i-->0;p++,datptr+=rowsz)*p=(void *)datptr; |
164 | 0 | } |
165 | 0 | return (void **)ret; |
166 | 0 | } |
167 | | |
168 | 20.6k | void oc_free_2d(void *_ptr){ |
169 | 20.6k | _ogg_free(_ptr); |
170 | 20.6k | } |
171 | | |
172 | | /*Fills in a Y'CbCr buffer with a pointer to the image data in the first |
173 | | buffer, but with the opposite vertical orientation. |
174 | | _dst: The destination buffer. |
175 | | This can be the same as _src. |
176 | | _src: The source buffer.*/ |
177 | | void oc_ycbcr_buffer_flip(th_ycbcr_buffer _dst, |
178 | 62.8k | const th_ycbcr_buffer _src){ |
179 | 62.8k | int pli; |
180 | 251k | for(pli=0;pli<3;pli++){ |
181 | 188k | _dst[pli].width=_src[pli].width; |
182 | 188k | _dst[pli].height=_src[pli].height; |
183 | 188k | _dst[pli].stride=-_src[pli].stride; |
184 | 188k | _dst[pli].data=_src[pli].data |
185 | 188k | +(1-_dst[pli].height)*(ptrdiff_t)_dst[pli].stride; |
186 | 188k | } |
187 | 62.8k | } |
188 | | |
189 | 13.7k | const char *th_version_string(void){ |
190 | 13.7k | return OC_VENDOR_STRING; |
191 | 13.7k | } |
192 | | |
193 | 0 | ogg_uint32_t th_version_number(void){ |
194 | 0 | return (TH_VERSION_MAJOR<<16)+(TH_VERSION_MINOR<<8)+TH_VERSION_SUB; |
195 | 0 | } |
196 | | |
197 | | /*Determines the packet type. |
198 | | Note that this correctly interprets a 0-byte packet as a video data packet. |
199 | | Return: 1 for a header packet, 0 for a data packet.*/ |
200 | 0 | int th_packet_isheader(ogg_packet *_op){ |
201 | 0 | return _op->bytes>0?_op->packet[0]>>7:0; |
202 | 0 | } |
203 | | |
204 | | /*Determines the frame type of a video data packet. |
205 | | Note that this correctly interprets a 0-byte packet as a delta frame. |
206 | | Return: 1 for a key frame, 0 for a delta frame, and -1 for a header |
207 | | packet.*/ |
208 | 0 | int th_packet_iskeyframe(ogg_packet *_op){ |
209 | 0 | return _op->bytes<=0?0:_op->packet[0]&0x80?-1:!(_op->packet[0]&0x40); |
210 | 0 | } |