2 This file borrowed from liboggz
5 Copyright (C) 2003 Commonwealth Scientific and Industrial Research
6 Organisation (CSIRO) Australia
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
12 - Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
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.
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.
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.
39 * Conrad Parker <conrad@annodex.net>
47 #include "gstoggstream.h"
48 #include "vorbis_parse.h"
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
67 * The previous and next packet types can be inferred from the current packet
68 * (additional information is not required)
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.
74 * (see http://xiph.org/vorbis/doc/Vorbis_I_spec.html for specification)
79 parse_vorbis_header_packet (GstOggStream * pad, ogg_packet * packet)
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
88 long_size = 1 << (packet->packet[28] >> 4);
89 short_size = 1 << (packet->packet[28] & 0xF);
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;
101 parse_vorbis_setup_packet (GstOggStream * pad, ogg_packet * op)
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).
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
117 unsigned char *current_pos = &op->packet[op->bytes - 1];
126 * This is the format of the mode data at the end of the packet for all
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 ]
155 * i.e. each entry is an important bit, 32 bits of 0, 8 bits of blah, a
157 * Let's find our last 1 bit first.
164 while (!((1 << --offset) & *current_pos)) {
174 * from current_pos-5:(offset+1) to current_pos-1:(offset+1) should
177 offset = (offset + 7) % 8;
181 if (((current_pos[-5] & ~((1 << (offset + 1)) - 1)) != 0)
188 || ((current_pos[-1] & ((1 << (offset + 1)) - 1)) != 0)
199 /* Give ourselves a chance to recover if we went back too far by using
201 for (ii = 0; ii < 2; ii++) {
203 size_check = (current_pos[0] >> (offset - 5)) & 0x3F;
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)) >>
215 if (size_check == size) {
218 offset = (offset + 1) % 8;
225 /* Store mode size information in our info struct */
227 while ((1 << (++i)) < size);
228 pad->vorbis_log2_num_modes = i;
230 mode_size_ptr = pad->vorbis_mode_sizes;
232 for (i = 0; i < size; i++) {
233 offset = (offset + 1) % 8;
236 *mode_size_ptr++ = (current_pos[0] >> offset) & 0x1;