- Factor out container specific code from apps/codecs/wma.c.
[kugel-rb.git] / apps / codecs / libasf / asf.c
blobf65627b0577233d9a950d8ac60210582c0de6536
1 #include <inttypes.h>
2 #include "codeclib.h"
3 #include "asf.h"
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;
31 int stream_id;
32 int ec_length, opaque_data, ec_length_type;
33 int datalen;
34 uint8_t data[18];
35 uint8_t* datap;
36 uint32_t length;
37 uint32_t padding_length;
38 uint32_t send_time;
39 uint16_t duration;
40 uint16_t payload_count;
41 int payload_length_type;
42 uint32_t payload_hdrlen;
43 int payload_datalen;
44 int multiple;
45 uint32_t replicated_length;
46 uint32_t media_object_number;
47 uint32_t media_object_offset;
48 uint32_t bytesread = 0;
49 uint8_t* buf;
50 size_t bufsize;
51 int i;
52 /*DEBUGF("Reading new packet at %d bytes ", (int)ci->curpos);*/
54 if (ci->read_filebuf(&tmp8, 1) == 0) {
55 return ASF_ERROR_EOF;
57 bytesread++;
59 /* TODO: We need a better way to detect endofstream */
60 if (tmp8 != 0x82) {
61 DEBUGF("Read failed: packet did not sync\n");
62 return -1;
66 if (tmp8 & 0x80) {
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;
76 /* Skip ec_data */
77 ci->advance_buffer(ec_length);
78 bytesread += ec_length;
79 } else {
80 ec_length = 0;
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; }
85 bytesread += 2;
87 datalen = GETLEN2b((packet_flags >> 1) & 0x03) +
88 GETLEN2b((packet_flags >> 3) & 0x03) +
89 GETLEN2b((packet_flags >> 5) & 0x03) + 6;
91 #if 0
92 if (datalen > sizeof(data)) {
93 DEBUGF("Unexpectedly long datalen in data - %d\n",datalen);
94 return ASF_ERROR_OUTOFMEM;
96 #endif
98 if (ci->read_filebuf(data, datalen) == 0) {
99 return ASF_ERROR_EOF;
102 bytesread += datalen;
104 datap = data;
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);
113 datap += 4;
114 duration = get_short_le(datap);
115 datap += 2;
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
120 * value */
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;
145 bytesread++;
146 } else {
147 payload_count = 1;
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.
163 *audiobuf = NULL;
164 *audiobufsize = 0;
165 *packetlength = length - bytesread;
167 buf = ci->request_buffer(&bufsize, length);
168 datap = buf;
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);
178 return -1;
181 for (i=0; i<payload_count; i++) {
182 stream_id = datap[0]&0x7f;
183 datap++;
184 bytesread++;
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);
191 #if 0
192 /* TODO */
193 if (payload_hdrlen > size) {
194 return ASF_ERROR_INVALID_LENGTH;
196 #endif
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;
218 if (multiple) {
219 int x;
221 x = GETLEN2b(payload_length_type);
223 if (x != 2) {
224 /* in multiple payloads datalen should be a word */
225 return ASF_ERROR_INVALID_VALUE;
228 #if 0
229 if (skip + tmp > datalen) {
230 /* not enough data */
231 return ASF_ERROR_INVALID_LENGTH;
233 #endif
234 payload_datalen = GETVALUE2b(payload_length_type, datap);
235 datap += x;
236 bytesread += x;
237 } else {
238 payload_datalen = length - bytesread - padding_length;
241 if (replicated_length==1)
242 datap++;
244 if (stream_id == wfx->audiostream)
246 if (*audiobuf == NULL) {
247 /* The first payload can stay where it is */
248 *audiobuf = datap;
249 *audiobufsize = payload_datalen;
250 } else {
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)
264 return 1;
265 else
266 return 0;
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;
274 int datalen;
275 uint8_t data[18];
276 uint8_t* datap;
277 uint32_t length;
278 uint32_t padding_length;
279 uint32_t send_time;
280 static int packet_count = 0;
282 uint32_t bytesread = 0;
283 packet_count++;
284 if (ci->read_filebuf(&tmp8, 1) == 0) {
285 DEBUGF("ASF ERROR (EOF?)\n");
286 return ASF_ERROR_EOF;
288 bytesread++;
290 /* TODO: We need a better way to detect endofstream */
291 if (tmp8 != 0x82) {
292 DEBUGF("Get timestamp: Detected end of stream\n");
293 return ASF_ERROR_EOF;
297 if (tmp8 & 0x80) {
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;
307 /* Skip ec_data */
308 ci->advance_buffer(ec_length);
309 bytesread += ec_length;
310 } else {
311 ec_length = 0;
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;
323 bytesread += 2;
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;
336 datap = data;
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);
346 datap += 4;
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);
353 return send_time;