1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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
)
54 uint8_t tmp8
, packet_flags
, packet_property
;
56 int ec_length
, opaque_data
, ec_length_type
;
61 uint32_t padding_length
;
64 uint16_t payload_count
;
65 int payload_length_type
;
66 uint32_t payload_hdrlen
;
69 uint32_t replicated_length
;
70 uint32_t media_object_number
;
71 uint32_t media_object_offset
;
72 uint32_t bytesread
= 0;
76 /*DEBUGF("Reading new packet at %d bytes ", (int)ci->curpos);*/
78 if (ci
->read_filebuf(&tmp8
, 1) == 0) {
83 /* TODO: We need a better way to detect endofstream */
85 DEBUGF("Read failed: packet did not sync\n");
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
;
101 ci
->advance_buffer(ec_length
);
102 bytesread
+= ec_length
;
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
; }
111 datalen
= GETLEN2b((packet_flags
>> 1) & 0x03) +
112 GETLEN2b((packet_flags
>> 3) & 0x03) +
113 GETLEN2b((packet_flags
>> 5) & 0x03) + 6;
116 if (datalen
> sizeof(data
)) {
117 DEBUGF("Unexpectedly long datalen in data - %d\n",datalen
);
118 return ASF_ERROR_OUTOFMEM
;
122 if (ci
->read_filebuf(data
, datalen
) == 0) {
123 return ASF_ERROR_EOF
;
126 bytesread
+= datalen
;
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
);
138 duration
= get_short_le(datap
);
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
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;
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.
189 *packetlength
= length
- bytesread
;
191 buf
= ci
->request_buffer(&bufsize
, length
);
194 #define ASF_MAX_REQUEST (1L<<15) /* 32KB */
195 if (bufsize
!= length
&& length
>= ASF_MAX_REQUEST
) {
196 /* This should only happen with packets larger than 32KB (the
197 guard buffer size). All the streams I've seen have
198 relatively small packets less than about 8KB), but I don't
199 know what is expected.
201 DEBUGF("Could not read packet (requested %d bytes, received %d), curpos=%d, aborting\n",
202 (int)length
,(int)bufsize
,(int)ci
->curpos
);
206 for (i
=0; i
<payload_count
; i
++) {
207 stream_id
= datap
[0]&0x7f;
211 payload_hdrlen
= GETLEN2b(packet_property
& 0x03) +
212 GETLEN2b((packet_property
>> 2) & 0x03) +
213 GETLEN2b((packet_property
>> 4) & 0x03);
215 //DEBUGF("payload_hdrlen = %d\n",payload_hdrlen);
218 if (payload_hdrlen
> size
) {
219 return ASF_ERROR_INVALID_LENGTH
;
222 if (payload_hdrlen
> sizeof(data
)) {
223 DEBUGF("Unexpectedly long datalen in data - %d\n",datalen
);
224 return ASF_ERROR_OUTOFMEM
;
227 bytesread
+= payload_hdrlen
;
228 media_object_number
= GETVALUE2b((packet_property
>> 4) & 0x03, datap
);
229 datap
+= GETLEN2b((packet_property
>> 4) & 0x03);
230 media_object_offset
= GETVALUE2b((packet_property
>> 2) & 0x03, datap
);
231 datap
+= GETLEN2b((packet_property
>> 2) & 0x03);
232 replicated_length
= GETVALUE2b(packet_property
& 0x03, datap
);
233 datap
+= GETLEN2b(packet_property
& 0x03);
235 /* TODO: Validate replicated_length */
236 /* TODO: Is the content of this important for us? */
237 datap
+= replicated_length
;
238 bytesread
+= replicated_length
;
240 multiple
= packet_flags
& 0x01;
246 x
= GETLEN2b(payload_length_type
);
249 /* in multiple payloads datalen should be a word */
250 return ASF_ERROR_INVALID_VALUE
;
254 if (skip
+ tmp
> datalen
) {
255 /* not enough data */
256 return ASF_ERROR_INVALID_LENGTH
;
259 payload_datalen
= GETVALUE2b(payload_length_type
, datap
);
263 payload_datalen
= length
- bytesread
- padding_length
;
266 if (replicated_length
==1)
269 if (stream_id
== wfx
->audiostream
)
271 if (*audiobuf
== NULL
) {
272 /* The first payload can stay where it is */
274 *audiobufsize
= payload_datalen
;
276 /* The second and subsequent payloads in this packet
277 that belong to the audio stream need to be moved to be
278 contiguous with the first payload.
280 memmove(*audiobuf
+ *audiobufsize
, datap
, payload_datalen
);
281 *audiobufsize
+= payload_datalen
;
284 datap
+= payload_datalen
;
285 bytesread
+= payload_datalen
;
288 if (*audiobuf
!= NULL
)
295 int asf_get_timestamp(int *duration
)
297 uint8_t tmp8
, packet_flags
, packet_property
;
298 int ec_length
, opaque_data
, ec_length_type
;
303 uint32_t padding_length
;
305 static int packet_count
= 0;
307 uint32_t bytesread
= 0;
309 if (ci
->read_filebuf(&tmp8
, 1) == 0) {
310 DEBUGF("ASF ERROR (EOF?)\n");
311 return ASF_ERROR_EOF
;
315 /* TODO: We need a better way to detect endofstream */
317 DEBUGF("Get timestamp: Detected end of stream\n");
318 return ASF_ERROR_EOF
;
323 ec_length
= tmp8
& 0x0f;
324 opaque_data
= (tmp8
>> 4) & 0x01;
325 ec_length_type
= (tmp8
>> 5) & 0x03;
327 if (ec_length_type
!= 0x00 || opaque_data
!= 0 || ec_length
!= 0x02) {
328 DEBUGF("incorrect error correction flags\n");
329 return ASF_ERROR_INVALID_VALUE
;
333 ci
->advance_buffer(ec_length
);
334 bytesread
+= ec_length
;
339 if (ci
->read_filebuf(&packet_flags
, 1) == 0) {
340 DEBUGF("Detected end of stream 2\n");
341 return ASF_ERROR_EOF
;
344 if (ci
->read_filebuf(&packet_property
, 1) == 0) {
345 DEBUGF("Detected end of stream3\n");
346 return ASF_ERROR_EOF
;
350 datalen
= GETLEN2b((packet_flags
>> 1) & 0x03) +
351 GETLEN2b((packet_flags
>> 3) & 0x03) +
352 GETLEN2b((packet_flags
>> 5) & 0x03) + 6;
354 if (ci
->read_filebuf(data
, datalen
) == 0) {
355 DEBUGF("Detected end of stream4\n");
356 return ASF_ERROR_EOF
;
359 bytesread
+= datalen
;
362 length
= GETVALUE2b((packet_flags
>> 5) & 0x03, datap
);
363 datap
+= GETLEN2b((packet_flags
>> 5) & 0x03);
365 /* sequence value is not used */
366 GETVALUE2b((packet_flags
>> 1) & 0x03, datap
);
367 datap
+= GETLEN2b((packet_flags
>> 1) & 0x03);
368 padding_length
= GETVALUE2b((packet_flags
>> 3) & 0x03, datap
);
369 datap
+= GETLEN2b((packet_flags
>> 3) & 0x03);
370 send_time
= get_long_le(datap
);
372 *duration
= get_short_le(datap
);
374 /*the asf_get_timestamp function advances us 12-13 bytes past the packet start,
375 need to undo this here so that we stay synced with the packet*/
376 ci
->seek_buffer(ci
->curpos
-bytesread
);
381 /*entry point for seeks*/
382 int asf_seek(int ms
, asf_waveformatex_t
* wfx
)
384 int time
, duration
, delta
, temp
, count
=0;
386 /*estimate packet number from bitrate*/
387 int initial_packet
= ci
->curpos
/wfx
->packet_size
;
388 int packet_num
= (((int64_t)ms
)*(wfx
->bitrate
>>3))/wfx
->packet_size
/1000;
389 int last_packet
= ci
->id3
->filesize
/ wfx
->packet_size
;
391 if (packet_num
> last_packet
) {
392 packet_num
= last_packet
;
395 /*calculate byte address of the start of that packet*/
396 int packet_offset
= packet_num
*wfx
->packet_size
;
398 /*seek to estimated packet*/
399 ci
->seek_buffer(ci
->id3
->first_frame_offset
+packet_offset
);
403 /*for very large files it can be difficult and unimportant to find the exact packet*/
406 /*check the time stamp of our packet*/
407 time
= asf_get_timestamp(&duration
);
408 /*DEBUGF("seeked to %d ms with duration %d\n", time, duration);*/
411 /*unknown error, try to recover*/
412 DEBUGF("UKNOWN SEEK ERROR\n");
413 ci
->seek_buffer(ci
->id3
->first_frame_offset
+initial_packet
*wfx
->packet_size
);
414 /*seek failed so return time stamp of the initial packet*/
415 return asf_get_timestamp(&duration
);
418 if ((time
+duration
>=ms
&& time
<=ms
) || count
> 10) {
419 /*DEBUGF("Found our packet! Now at %d packet\n", packet_num);*/
424 /*estimate new packet number from bitrate and our current position*/
426 packet_num
= ((temp
/1000)*(wfx
->bitrate
>>3) - (wfx
->packet_size
>>1))/wfx
->packet_size
; //round down!
427 packet_offset
= packet_num
*wfx
->packet_size
;
428 ci
->seek_buffer(ci
->id3
->first_frame_offset
+packet_offset
);