5 /* Read an unaligned 32-bit little endian long from buffer. */
6 static unsigned long get_long_le(void* buf
)
8 unsigned char* p
= (unsigned char*) buf
;
10 return p
[0] | (p
[1] << 8) | (p
[2] << 16) | (p
[3] << 24);
13 /* Read an unaligned 16-bit little endian short from buffer. */
14 static unsigned short get_short_le(void* buf
)
16 unsigned char* p
= (unsigned char*) buf
;
18 return p
[0] | (p
[1] << 8);
21 #define GETLEN2b(bits) (((bits) == 0x03) ? 4 : bits)
23 #define GETVALUE2b(bits, data) \
24 (((bits) != 0x03) ? ((bits) != 0x02) ? ((bits) != 0x01) ? \
25 0 : *(data) : get_short_le(data) : get_long_le(data))
27 int asf_read_packet(uint8_t** audiobuf
, int* audiobufsize
, int* packetlength
,
28 asf_waveformatex_t
* wfx
, struct codec_api
* ci
)
30 uint8_t tmp8
, packet_flags
, packet_property
;
32 int ec_length
, opaque_data
, ec_length_type
;
37 uint32_t padding_length
;
40 uint16_t payload_count
;
41 int payload_length_type
;
42 uint32_t payload_hdrlen
;
45 uint32_t replicated_length
;
46 uint32_t media_object_number
;
47 uint32_t media_object_offset
;
48 uint32_t bytesread
= 0;
52 /*DEBUGF("Reading new packet at %d bytes ", (int)ci->curpos);*/
54 if (ci
->read_filebuf(&tmp8
, 1) == 0) {
59 /* TODO: We need a better way to detect endofstream */
61 DEBUGF("Read failed: packet did not sync\n");
67 ec_length
= tmp8
& 0x0f;
68 opaque_data
= (tmp8
>> 4) & 0x01;
69 ec_length_type
= (tmp8
>> 5) & 0x03;
71 if (ec_length_type
!= 0x00 || opaque_data
!= 0 || ec_length
!= 0x02) {
72 DEBUGF("incorrect error correction flags\n");
73 return ASF_ERROR_INVALID_VALUE
;
77 ci
->advance_buffer(ec_length
);
78 bytesread
+= ec_length
;
83 if (ci
->read_filebuf(&packet_flags
, 1) == 0) { return ASF_ERROR_EOF
; }
84 if (ci
->read_filebuf(&packet_property
, 1) == 0) { return ASF_ERROR_EOF
; }
87 datalen
= GETLEN2b((packet_flags
>> 1) & 0x03) +
88 GETLEN2b((packet_flags
>> 3) & 0x03) +
89 GETLEN2b((packet_flags
>> 5) & 0x03) + 6;
92 if (datalen
> sizeof(data
)) {
93 DEBUGF("Unexpectedly long datalen in data - %d\n",datalen
);
94 return ASF_ERROR_OUTOFMEM
;
98 if (ci
->read_filebuf(data
, datalen
) == 0) {
102 bytesread
+= datalen
;
105 length
= GETVALUE2b((packet_flags
>> 5) & 0x03, datap
);
106 datap
+= GETLEN2b((packet_flags
>> 5) & 0x03);
107 /* sequence value is not used */
108 GETVALUE2b((packet_flags
>> 1) & 0x03, datap
);
109 datap
+= GETLEN2b((packet_flags
>> 1) & 0x03);
110 padding_length
= GETVALUE2b((packet_flags
>> 3) & 0x03, datap
);
111 datap
+= GETLEN2b((packet_flags
>> 3) & 0x03);
112 send_time
= get_long_le(datap
);
114 duration
= get_short_le(datap
);
116 /*DEBUGF("and duration %d ms\n", duration);*/
118 /* this is really idiotic, packet length can (and often will) be
119 * undefined and we just have to use the header packet size as the size
121 if (!((packet_flags
>> 5) & 0x03)) {
122 length
= wfx
->packet_size
;
125 /* this is also really idiotic, if packet length is smaller than packet
126 * size, we need to manually add the additional bytes into padding length
128 if (length
< wfx
->packet_size
) {
129 padding_length
+= wfx
->packet_size
- length
;
130 length
= wfx
->packet_size
;
133 if (length
> wfx
->packet_size
) {
134 DEBUGF("packet with too big length value\n");
135 return ASF_ERROR_INVALID_LENGTH
;
138 /* check if we have multiple payloads */
139 if (packet_flags
& 0x01) {
140 if (ci
->read_filebuf(&tmp8
, 1) == 0) {
141 return ASF_ERROR_EOF
;
143 payload_count
= tmp8
& 0x3f;
144 payload_length_type
= (tmp8
>> 6) & 0x03;
148 payload_length_type
= 0x02; /* not used */
151 if (length
< bytesread
) {
152 DEBUGF("header exceeded packet size, invalid file - length=%d, bytesread=%d\n",(int)length
,(int)bytesread
);
153 /* FIXME: should this be checked earlier? */
154 return ASF_ERROR_INVALID_LENGTH
;
158 /* We now parse the individual payloads, and move all payloads
159 belonging to our audio stream to a contiguous block, starting at
160 the location of the first payload.
165 *packetlength
= length
- bytesread
;
167 buf
= ci
->request_buffer(&bufsize
, length
);
170 if (bufsize
!= length
) {
171 /* This should only happen with packets larger than 32KB (the
172 guard buffer size). All the streams I've seen have
173 relatively small packets less than about 8KB), but I don't
174 know what is expected.
176 DEBUGF("Could not read packet (requested %d bytes, received %d), curpos=%d, aborting\n",
177 (int)length
,(int)bufsize
,(int)ci
->curpos
);
181 for (i
=0; i
<payload_count
; i
++) {
182 stream_id
= datap
[0]&0x7f;
186 payload_hdrlen
= GETLEN2b(packet_property
& 0x03) +
187 GETLEN2b((packet_property
>> 2) & 0x03) +
188 GETLEN2b((packet_property
>> 4) & 0x03);
190 //DEBUGF("payload_hdrlen = %d\n",payload_hdrlen);
193 if (payload_hdrlen
> size
) {
194 return ASF_ERROR_INVALID_LENGTH
;
197 if (payload_hdrlen
> sizeof(data
)) {
198 DEBUGF("Unexpectedly long datalen in data - %d\n",datalen
);
199 return ASF_ERROR_OUTOFMEM
;
202 bytesread
+= payload_hdrlen
;
203 media_object_number
= GETVALUE2b((packet_property
>> 4) & 0x03, datap
);
204 datap
+= GETLEN2b((packet_property
>> 4) & 0x03);
205 media_object_offset
= GETVALUE2b((packet_property
>> 2) & 0x03, datap
);
206 datap
+= GETLEN2b((packet_property
>> 2) & 0x03);
207 replicated_length
= GETVALUE2b(packet_property
& 0x03, datap
);
208 datap
+= GETLEN2b(packet_property
& 0x03);
210 /* TODO: Validate replicated_length */
211 /* TODO: Is the content of this important for us? */
212 datap
+= replicated_length
;
213 bytesread
+= replicated_length
;
215 multiple
= packet_flags
& 0x01;
221 x
= GETLEN2b(payload_length_type
);
224 /* in multiple payloads datalen should be a word */
225 return ASF_ERROR_INVALID_VALUE
;
229 if (skip
+ tmp
> datalen
) {
230 /* not enough data */
231 return ASF_ERROR_INVALID_LENGTH
;
234 payload_datalen
= GETVALUE2b(payload_length_type
, datap
);
238 payload_datalen
= length
- bytesread
- padding_length
;
241 if (replicated_length
==1)
244 if (stream_id
== wfx
->audiostream
)
246 if (*audiobuf
== NULL
) {
247 /* The first payload can stay where it is */
249 *audiobufsize
= payload_datalen
;
251 /* The second and subsequent payloads in this packet
252 that belong to the audio stream need to be moved to be
253 contiguous with the first payload.
255 memmove(*audiobuf
+ *audiobufsize
, datap
, payload_datalen
);
256 *audiobufsize
+= payload_datalen
;
259 datap
+= payload_datalen
;
260 bytesread
+= payload_datalen
;
263 if (*audiobuf
!= NULL
)
270 int asf_get_timestamp(int *duration
, struct codec_api
* ci
)
272 uint8_t tmp8
, packet_flags
, packet_property
;
273 int ec_length
, opaque_data
, ec_length_type
;
278 uint32_t padding_length
;
280 static int packet_count
= 0;
282 uint32_t bytesread
= 0;
284 if (ci
->read_filebuf(&tmp8
, 1) == 0) {
285 DEBUGF("ASF ERROR (EOF?)\n");
286 return ASF_ERROR_EOF
;
290 /* TODO: We need a better way to detect endofstream */
292 DEBUGF("Get timestamp: Detected end of stream\n");
293 return ASF_ERROR_EOF
;
298 ec_length
= tmp8
& 0x0f;
299 opaque_data
= (tmp8
>> 4) & 0x01;
300 ec_length_type
= (tmp8
>> 5) & 0x03;
302 if (ec_length_type
!= 0x00 || opaque_data
!= 0 || ec_length
!= 0x02) {
303 DEBUGF("incorrect error correction flags\n");
304 return ASF_ERROR_INVALID_VALUE
;
308 ci
->advance_buffer(ec_length
);
309 bytesread
+= ec_length
;
314 if (ci
->read_filebuf(&packet_flags
, 1) == 0) {
315 DEBUGF("Detected end of stream 2\n");
316 return ASF_ERROR_EOF
;
319 if (ci
->read_filebuf(&packet_property
, 1) == 0) {
320 DEBUGF("Detected end of stream3\n");
321 return ASF_ERROR_EOF
;
325 datalen
= GETLEN2b((packet_flags
>> 1) & 0x03) +
326 GETLEN2b((packet_flags
>> 3) & 0x03) +
327 GETLEN2b((packet_flags
>> 5) & 0x03) + 6;
329 if (ci
->read_filebuf(data
, datalen
) == 0) {
330 DEBUGF("Detected end of stream4\n");
331 return ASF_ERROR_EOF
;
334 bytesread
+= datalen
;
337 length
= GETVALUE2b((packet_flags
>> 5) & 0x03, datap
);
338 datap
+= GETLEN2b((packet_flags
>> 5) & 0x03);
340 /* sequence value is not used */
341 GETVALUE2b((packet_flags
>> 1) & 0x03, datap
);
342 datap
+= GETLEN2b((packet_flags
>> 1) & 0x03);
343 padding_length
= GETVALUE2b((packet_flags
>> 3) & 0x03, datap
);
344 datap
+= GETLEN2b((packet_flags
>> 3) & 0x03);
345 send_time
= get_long_le(datap
);
347 *duration
= get_short_le(datap
);
349 /*the asf_get_timestamp function advances us 12-13 bytes past the packet start,
350 need to undo this here so that we stay synced with the packet*/
351 ci
->seek_buffer(ci
->curpos
-bytesread
);