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 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
);
205 for (i
=0; i
<payload_count
; i
++) {
206 stream_id
= datap
[0]&0x7f;
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);
217 if (payload_hdrlen
> size
) {
218 return ASF_ERROR_INVALID_LENGTH
;
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;
245 x
= GETLEN2b(payload_length_type
);
248 /* in multiple payloads datalen should be a word */
249 return ASF_ERROR_INVALID_VALUE
;
253 if (skip
+ tmp
> datalen
) {
254 /* not enough data */
255 return ASF_ERROR_INVALID_LENGTH
;
258 payload_datalen
= GETVALUE2b(payload_length_type
, datap
);
262 payload_datalen
= length
- bytesread
- padding_length
;
265 if (replicated_length
==1)
268 if (stream_id
== wfx
->audiostream
)
270 if (*audiobuf
== NULL
) {
271 /* The first payload can stay where it is */
273 *audiobufsize
= payload_datalen
;
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
)
294 int asf_get_timestamp(int *duration
)
296 uint8_t tmp8
, packet_flags
, packet_property
;
297 int ec_length
, opaque_data
, ec_length_type
;
302 uint32_t padding_length
;
304 static int packet_count
= 0;
306 uint32_t bytesread
= 0;
308 if (ci
->read_filebuf(&tmp8
, 1) == 0) {
309 DEBUGF("ASF ERROR (EOF?)\n");
310 return ASF_ERROR_EOF
;
314 /* TODO: We need a better way to detect endofstream */
316 DEBUGF("Get timestamp: Detected end of stream\n");
317 return ASF_ERROR_EOF
;
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
;
332 ci
->advance_buffer(ec_length
);
333 bytesread
+= ec_length
;
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
;
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
;
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
);
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
);
380 /*entry point for seeks*/
381 int seek(int ms
, asf_waveformatex_t
* wfx
)
383 int time
, duration
, delta
, temp
, count
=0;
385 /*estimate packet number from bitrate*/
386 int initial_packet
= ci
->curpos
/wfx
->packet_size
;
387 int packet_num
= (((int64_t)ms
)*(wfx
->bitrate
>>3))/wfx
->packet_size
/1000;
388 int last_packet
= ci
->id3
->filesize
/ wfx
->packet_size
;
390 if (packet_num
> last_packet
) {
391 packet_num
= last_packet
;
394 /*calculate byte address of the start of that packet*/
395 int packet_offset
= packet_num
*wfx
->packet_size
;
397 /*seek to estimated packet*/
398 ci
->seek_buffer(ci
->id3
->first_frame_offset
+packet_offset
);
402 /*for very large files it can be difficult and unimportant to find the exact packet*/
405 /*check the time stamp of our packet*/
406 time
= asf_get_timestamp(&duration
);
407 DEBUGF("seeked to %d ms with duration %d\n", time
, duration
);
410 /*unknown error, try to recover*/
411 DEBUGF("UKNOWN SEEK ERROR\n");
412 ci
->seek_buffer(ci
->id3
->first_frame_offset
+initial_packet
*wfx
->packet_size
);
413 /*seek failed so return time stamp of the initial packet*/
414 return asf_get_timestamp(&duration
);
417 if ((time
+duration
>=ms
&& time
<=ms
) || count
> 10) {
418 DEBUGF("Found our packet! Now at %d packet\n", packet_num
);
423 /*estimate new packet number from bitrate and our current position*/
425 packet_num
= ((temp
/1000)*(wfx
->bitrate
>>3) - (wfx
->packet_size
>>1))/wfx
->packet_size
; //round down!
426 packet_offset
= packet_num
*wfx
->packet_size
;
427 ci
->seek_buffer(ci
->id3
->first_frame_offset
+packet_offset
);