Add the Rockbox GPL header to apps/codecs/libasf/asf.c and fix the one in apps/codecs...
[kugel-rb.git] / apps / codecs / libasf / asf.c
blobe3fffe6ee0b6aeeaf0a81808c97e36cf7e5c0d67
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Dave Chapman
12 * ASF parsing code based on libasf by Juho Vähä-Herttua
13 * http://code.google.com/p/libasf/ libasf itself was based on the ASF
14 * parser in VLC - http://www.videolan.org/
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
24 ****************************************************************************/
25 #include <inttypes.h>
26 #include "codeclib.h"
27 #include "asf.h"
29 /* Read an unaligned 32-bit little endian long from buffer. */
30 static unsigned long get_long_le(void* buf)
32 unsigned char* p = (unsigned char*) buf;
34 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
37 /* Read an unaligned 16-bit little endian short from buffer. */
38 static unsigned short get_short_le(void* buf)
40 unsigned char* p = (unsigned char*) buf;
42 return p[0] | (p[1] << 8);
45 #define GETLEN2b(bits) (((bits) == 0x03) ? 4 : bits)
47 #define GETVALUE2b(bits, data) \
48 (((bits) != 0x03) ? ((bits) != 0x02) ? ((bits) != 0x01) ? \
49 0 : *(data) : get_short_le(data) : get_long_le(data))
51 int asf_read_packet(uint8_t** audiobuf, int* audiobufsize, int* packetlength,
52 asf_waveformatex_t* wfx, struct codec_api* ci)
54 uint8_t tmp8, packet_flags, packet_property;
55 int stream_id;
56 int ec_length, opaque_data, ec_length_type;
57 int datalen;
58 uint8_t data[18];
59 uint8_t* datap;
60 uint32_t length;
61 uint32_t padding_length;
62 uint32_t send_time;
63 uint16_t duration;
64 uint16_t payload_count;
65 int payload_length_type;
66 uint32_t payload_hdrlen;
67 int payload_datalen;
68 int multiple;
69 uint32_t replicated_length;
70 uint32_t media_object_number;
71 uint32_t media_object_offset;
72 uint32_t bytesread = 0;
73 uint8_t* buf;
74 size_t bufsize;
75 int i;
76 /*DEBUGF("Reading new packet at %d bytes ", (int)ci->curpos);*/
78 if (ci->read_filebuf(&tmp8, 1) == 0) {
79 return ASF_ERROR_EOF;
81 bytesread++;
83 /* TODO: We need a better way to detect endofstream */
84 if (tmp8 != 0x82) {
85 DEBUGF("Read failed: packet did not sync\n");
86 return -1;
90 if (tmp8 & 0x80) {
91 ec_length = tmp8 & 0x0f;
92 opaque_data = (tmp8 >> 4) & 0x01;
93 ec_length_type = (tmp8 >> 5) & 0x03;
95 if (ec_length_type != 0x00 || opaque_data != 0 || ec_length != 0x02) {
96 DEBUGF("incorrect error correction flags\n");
97 return ASF_ERROR_INVALID_VALUE;
100 /* Skip ec_data */
101 ci->advance_buffer(ec_length);
102 bytesread += ec_length;
103 } else {
104 ec_length = 0;
107 if (ci->read_filebuf(&packet_flags, 1) == 0) { return ASF_ERROR_EOF; }
108 if (ci->read_filebuf(&packet_property, 1) == 0) { return ASF_ERROR_EOF; }
109 bytesread += 2;
111 datalen = GETLEN2b((packet_flags >> 1) & 0x03) +
112 GETLEN2b((packet_flags >> 3) & 0x03) +
113 GETLEN2b((packet_flags >> 5) & 0x03) + 6;
115 #if 0
116 if (datalen > sizeof(data)) {
117 DEBUGF("Unexpectedly long datalen in data - %d\n",datalen);
118 return ASF_ERROR_OUTOFMEM;
120 #endif
122 if (ci->read_filebuf(data, datalen) == 0) {
123 return ASF_ERROR_EOF;
126 bytesread += datalen;
128 datap = data;
129 length = GETVALUE2b((packet_flags >> 5) & 0x03, datap);
130 datap += GETLEN2b((packet_flags >> 5) & 0x03);
131 /* sequence value is not used */
132 GETVALUE2b((packet_flags >> 1) & 0x03, datap);
133 datap += GETLEN2b((packet_flags >> 1) & 0x03);
134 padding_length = GETVALUE2b((packet_flags >> 3) & 0x03, datap);
135 datap += GETLEN2b((packet_flags >> 3) & 0x03);
136 send_time = get_long_le(datap);
137 datap += 4;
138 duration = get_short_le(datap);
139 datap += 2;
140 /*DEBUGF("and duration %d ms\n", duration);*/
142 /* this is really idiotic, packet length can (and often will) be
143 * undefined and we just have to use the header packet size as the size
144 * value */
145 if (!((packet_flags >> 5) & 0x03)) {
146 length = wfx->packet_size;
149 /* this is also really idiotic, if packet length is smaller than packet
150 * size, we need to manually add the additional bytes into padding length
152 if (length < wfx->packet_size) {
153 padding_length += wfx->packet_size - length;
154 length = wfx->packet_size;
157 if (length > wfx->packet_size) {
158 DEBUGF("packet with too big length value\n");
159 return ASF_ERROR_INVALID_LENGTH;
162 /* check if we have multiple payloads */
163 if (packet_flags & 0x01) {
164 if (ci->read_filebuf(&tmp8, 1) == 0) {
165 return ASF_ERROR_EOF;
167 payload_count = tmp8 & 0x3f;
168 payload_length_type = (tmp8 >> 6) & 0x03;
169 bytesread++;
170 } else {
171 payload_count = 1;
172 payload_length_type = 0x02; /* not used */
175 if (length < bytesread) {
176 DEBUGF("header exceeded packet size, invalid file - length=%d, bytesread=%d\n",(int)length,(int)bytesread);
177 /* FIXME: should this be checked earlier? */
178 return ASF_ERROR_INVALID_LENGTH;
182 /* We now parse the individual payloads, and move all payloads
183 belonging to our audio stream to a contiguous block, starting at
184 the location of the first payload.
187 *audiobuf = NULL;
188 *audiobufsize = 0;
189 *packetlength = length - bytesread;
191 buf = ci->request_buffer(&bufsize, length);
192 datap = buf;
194 if (bufsize != length) {
195 /* This should only happen with packets larger than 32KB (the
196 guard buffer size). All the streams I've seen have
197 relatively small packets less than about 8KB), but I don't
198 know what is expected.
200 DEBUGF("Could not read packet (requested %d bytes, received %d), curpos=%d, aborting\n",
201 (int)length,(int)bufsize,(int)ci->curpos);
202 return -1;
205 for (i=0; i<payload_count; i++) {
206 stream_id = datap[0]&0x7f;
207 datap++;
208 bytesread++;
210 payload_hdrlen = GETLEN2b(packet_property & 0x03) +
211 GETLEN2b((packet_property >> 2) & 0x03) +
212 GETLEN2b((packet_property >> 4) & 0x03);
214 //DEBUGF("payload_hdrlen = %d\n",payload_hdrlen);
215 #if 0
216 /* TODO */
217 if (payload_hdrlen > size) {
218 return ASF_ERROR_INVALID_LENGTH;
220 #endif
221 if (payload_hdrlen > sizeof(data)) {
222 DEBUGF("Unexpectedly long datalen in data - %d\n",datalen);
223 return ASF_ERROR_OUTOFMEM;
226 bytesread += payload_hdrlen;
227 media_object_number = GETVALUE2b((packet_property >> 4) & 0x03, datap);
228 datap += GETLEN2b((packet_property >> 4) & 0x03);
229 media_object_offset = GETVALUE2b((packet_property >> 2) & 0x03, datap);
230 datap += GETLEN2b((packet_property >> 2) & 0x03);
231 replicated_length = GETVALUE2b(packet_property & 0x03, datap);
232 datap += GETLEN2b(packet_property & 0x03);
234 /* TODO: Validate replicated_length */
235 /* TODO: Is the content of this important for us? */
236 datap += replicated_length;
237 bytesread += replicated_length;
239 multiple = packet_flags & 0x01;
242 if (multiple) {
243 int x;
245 x = GETLEN2b(payload_length_type);
247 if (x != 2) {
248 /* in multiple payloads datalen should be a word */
249 return ASF_ERROR_INVALID_VALUE;
252 #if 0
253 if (skip + tmp > datalen) {
254 /* not enough data */
255 return ASF_ERROR_INVALID_LENGTH;
257 #endif
258 payload_datalen = GETVALUE2b(payload_length_type, datap);
259 datap += x;
260 bytesread += x;
261 } else {
262 payload_datalen = length - bytesread - padding_length;
265 if (replicated_length==1)
266 datap++;
268 if (stream_id == wfx->audiostream)
270 if (*audiobuf == NULL) {
271 /* The first payload can stay where it is */
272 *audiobuf = datap;
273 *audiobufsize = payload_datalen;
274 } else {
275 /* The second and subsequent payloads in this packet
276 that belong to the audio stream need to be moved to be
277 contiguous with the first payload.
279 memmove(*audiobuf + *audiobufsize, datap, payload_datalen);
280 *audiobufsize += payload_datalen;
283 datap += payload_datalen;
284 bytesread += payload_datalen;
287 if (*audiobuf != NULL)
288 return 1;
289 else
290 return 0;
294 int asf_get_timestamp(int *duration, struct codec_api* ci)
296 uint8_t tmp8, packet_flags, packet_property;
297 int ec_length, opaque_data, ec_length_type;
298 int datalen;
299 uint8_t data[18];
300 uint8_t* datap;
301 uint32_t length;
302 uint32_t padding_length;
303 uint32_t send_time;
304 static int packet_count = 0;
306 uint32_t bytesread = 0;
307 packet_count++;
308 if (ci->read_filebuf(&tmp8, 1) == 0) {
309 DEBUGF("ASF ERROR (EOF?)\n");
310 return ASF_ERROR_EOF;
312 bytesread++;
314 /* TODO: We need a better way to detect endofstream */
315 if (tmp8 != 0x82) {
316 DEBUGF("Get timestamp: Detected end of stream\n");
317 return ASF_ERROR_EOF;
321 if (tmp8 & 0x80) {
322 ec_length = tmp8 & 0x0f;
323 opaque_data = (tmp8 >> 4) & 0x01;
324 ec_length_type = (tmp8 >> 5) & 0x03;
326 if (ec_length_type != 0x00 || opaque_data != 0 || ec_length != 0x02) {
327 DEBUGF("incorrect error correction flags\n");
328 return ASF_ERROR_INVALID_VALUE;
331 /* Skip ec_data */
332 ci->advance_buffer(ec_length);
333 bytesread += ec_length;
334 } else {
335 ec_length = 0;
338 if (ci->read_filebuf(&packet_flags, 1) == 0) {
339 DEBUGF("Detected end of stream 2\n");
340 return ASF_ERROR_EOF;
343 if (ci->read_filebuf(&packet_property, 1) == 0) {
344 DEBUGF("Detected end of stream3\n");
345 return ASF_ERROR_EOF;
347 bytesread += 2;
349 datalen = GETLEN2b((packet_flags >> 1) & 0x03) +
350 GETLEN2b((packet_flags >> 3) & 0x03) +
351 GETLEN2b((packet_flags >> 5) & 0x03) + 6;
353 if (ci->read_filebuf(data, datalen) == 0) {
354 DEBUGF("Detected end of stream4\n");
355 return ASF_ERROR_EOF;
358 bytesread += datalen;
360 datap = data;
361 length = GETVALUE2b((packet_flags >> 5) & 0x03, datap);
362 datap += GETLEN2b((packet_flags >> 5) & 0x03);
364 /* sequence value is not used */
365 GETVALUE2b((packet_flags >> 1) & 0x03, datap);
366 datap += GETLEN2b((packet_flags >> 1) & 0x03);
367 padding_length = GETVALUE2b((packet_flags >> 3) & 0x03, datap);
368 datap += GETLEN2b((packet_flags >> 3) & 0x03);
369 send_time = get_long_le(datap);
370 datap += 4;
371 *duration = get_short_le(datap);
373 /*the asf_get_timestamp function advances us 12-13 bytes past the packet start,
374 need to undo this here so that we stay synced with the packet*/
375 ci->seek_buffer(ci->curpos-bytesread);
377 return send_time;