Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / ext / ogg / vorbis_parse.c
1 /*
2    This file borrowed from liboggz
3  */
4 /*
5    Copyright (C) 2003 Commonwealth Scientific and Industrial Research
6    Organisation (CSIRO) Australia
7
8    Redistribution and use in source and binary forms, with or without
9    modification, are permitted provided that the following conditions
10    are met:
11
12    - Redistributions of source code must retain the above copyright
13    notice, this list of conditions and the following disclaimer.
14
15    - Redistributions in binary form must reproduce the above copyright
16    notice, this list of conditions and the following disclaimer in the
17    documentation and/or other materials provided with the distribution.
18
19    - Neither the name of CSIRO Australia nor the names of its
20    contributors may be used to endorse or promote products derived from
21    this software without specific prior written permission.
22
23    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
27    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37  * oggz_auto.c
38  *
39  * Conrad Parker <conrad@annodex.net>
40  */
41
42 #include "config.h"
43
44 #include <stdlib.h>
45 #include <string.h>
46
47 #include "gstoggstream.h"
48 #include "vorbis_parse.h"
49
50 /*
51  * Vorbis packets can be short or long, and each packet overlaps the previous
52  * and next packets.  The granulepos of a packet is always the last sample
53  * that is completely decoded at the end of decoding that packet - i.e. the
54  * last packet before the first overlapping packet.  If the sizes of packets
55  * are 's' and 'l', then the increment will depend on the previous and next
56  * packet types:
57  *  v                             prev<<1 | next
58  * lll:           l/2             3
59  * lls:           3l/4 - s/4      2
60  * lsl:           s/2
61  * lss:           s/2
62  * sll:           l/4 + s/4       1
63  * sls:           l/2             0
64  * ssl:           s/2
65  * sss:           s/2
66  *
67  * The previous and next packet types can be inferred from the current packet
68  * (additional information is not required)
69  *
70  * The two blocksizes can be determined from the first header packet, by reading
71  * byte 28.  1 << (packet[28] >> 4) == long_size.
72  * 1 << (packet[28] & 0xF) == short_size.
73  *
74  * (see http://xiph.org/vorbis/doc/Vorbis_I_spec.html for specification)
75  */
76
77
78 void
79 parse_vorbis_header_packet (GstOggStream * pad, ogg_packet * packet)
80 {
81   /*
82    * on the first (b_o_s) packet, determine the long and short sizes,
83    * and then calculate l/2, l/4 - s/4, 3 * l/4 - s/4, l/2 - s/2 and s/2
84    */
85   int short_size;
86   int long_size;
87
88   long_size = 1 << (packet->packet[28] >> 4);
89   short_size = 1 << (packet->packet[28] & 0xF);
90
91   pad->nln_increments[3] = long_size >> 1;
92   pad->nln_increments[2] = 3 * (long_size >> 2) - (short_size >> 2);
93   pad->nln_increments[1] = (long_size >> 2) + (short_size >> 2);
94   pad->nln_increments[0] = pad->nln_increments[3];
95   pad->short_size = short_size;
96   pad->long_size = long_size;
97   pad->nsn_increment = short_size >> 1;
98 }
99
100 void
101 parse_vorbis_setup_packet (GstOggStream * pad, ogg_packet * op)
102 {
103   /*
104    * the code pages, a whole bunch of other fairly useless stuff, AND,
105    * RIGHT AT THE END (of a bunch of variable-length compressed rubbish that
106    * basically has only one actual set of values that everyone uses BUT YOU
107    * CAN'T BE SURE OF THAT, OH NO YOU CAN'T) is the only piece of data that's
108    * actually useful to us - the packet modes (because it's inconceivable to
109    * think people might want _just that_ and nothing else, you know, for
110    * seeking and stuff).
111    *
112    * Fortunately, because of the mandate that non-used bits must be zero
113    * at the end of the packet, we might be able to sneakily work backwards
114    * and find out the information we need (namely a mapping of modes to
115    * packet sizes)
116    */
117   unsigned char *current_pos = &op->packet[op->bytes - 1];
118   int offset;
119   int size;
120   int size_check;
121   int *mode_size_ptr;
122   int i;
123   int ii;
124
125   /*
126    * This is the format of the mode data at the end of the packet for all
127    * Vorbis Version 1 :
128    *
129    * [ 6:number_of_modes ]
130    * [ 1:size | 16:window_type(0) | 16:transform_type(0) | 8:mapping ]
131    * [ 1:size | 16:window_type(0) | 16:transform_type(0) | 8:mapping ]
132    * [ 1:size | 16:window_type(0) | 16:transform_type(0) | 8:mapping ]
133    * [ 1:framing(1) ]
134    *
135    * e.g.:
136    *
137    *              <-
138    * 0 0 0 0 0 1 0 0
139    * 0 0 1 0 0 0 0 0
140    * 0 0 1 0 0 0 0 0
141    * 0 0 1|0 0 0 0 0
142    * 0 0 0 0|0|0 0 0
143    * 0 0 0 0 0 0 0 0
144    * 0 0 0 0|0 0 0 0
145    * 0 0 0 0 0 0 0 0
146    * 0 0 0 0|0 0 0 0
147    * 0 0 0|1|0 0 0 0 |
148    * 0 0 0 0 0 0 0 0 V
149    * 0 0 0|0 0 0 0 0
150    * 0 0 0 0 0 0 0 0
151    * 0 0 1|0 0 0 0 0
152    * 0 0|1|0 0 0 0 0
153    *
154    *
155    * i.e. each entry is an important bit, 32 bits of 0, 8 bits of blah, a
156    * bit of 1.
157    * Let's find our last 1 bit first.
158    *
159    */
160
161   size = 0;
162
163   offset = 8;
164   while (!((1 << --offset) & *current_pos)) {
165     if (offset == 0) {
166       offset = 8;
167       current_pos -= 1;
168     }
169   }
170
171   while (1) {
172
173     /*
174      * from current_pos-5:(offset+1) to current_pos-1:(offset+1) should
175      * be zero
176      */
177     offset = (offset + 7) % 8;
178     if (offset == 7)
179       current_pos -= 1;
180
181     if (((current_pos[-5] & ~((1 << (offset + 1)) - 1)) != 0)
182         ||
183         current_pos[-4] != 0
184         ||
185         current_pos[-3] != 0
186         ||
187         current_pos[-2] != 0
188         || ((current_pos[-1] & ((1 << (offset + 1)) - 1)) != 0)
189         ) {
190       break;
191     }
192
193     size += 1;
194
195     current_pos -= 5;
196
197   }
198
199   /* Give ourselves a chance to recover if we went back too far by using
200    * the size check. */
201   for (ii = 0; ii < 2; ii++) {
202     if (offset > 4) {
203       size_check = (current_pos[0] >> (offset - 5)) & 0x3F;
204     } else {
205       /* mask part of byte from current_pos */
206       size_check = (current_pos[0] & ((1 << (offset + 1)) - 1));
207       /* shift to appropriate position */
208       size_check <<= (5 - offset);
209       /* or in part of byte from current_pos - 1 */
210       size_check |= (current_pos[-1] & ~((1 << (offset + 3)) - 1)) >>
211           (offset + 3);
212     }
213
214     size_check += 1;
215     if (size_check == size) {
216       break;
217     }
218     offset = (offset + 1) % 8;
219     if (offset == 0)
220       current_pos += 1;
221     current_pos += 5;
222     size -= 1;
223   }
224
225   /* Store mode size information in our info struct */
226   i = -1;
227   while ((1 << (++i)) < size);
228   pad->vorbis_log2_num_modes = i;
229
230   mode_size_ptr = pad->vorbis_mode_sizes;
231
232   for (i = 0; i < size; i++) {
233     offset = (offset + 1) % 8;
234     if (offset == 0)
235       current_pos += 1;
236     *mode_size_ptr++ = (current_pos[0] >> offset) & 0x1;
237     current_pos += 5;
238   }
239
240 }