mmst: print packet type with error status code message
[libav.git] / libavformat / asfdec.c
CommitLineData
de6d9b64 1/*
7fbde343 2 * ASF compatible demuxer
406792e7 3 * Copyright (c) 2000, 2001 Fabrice Bellard
de6d9b64 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
245976da 21
c9d7cd6d
BC
22//#define DEBUG
23
245976da 24#include "libavutil/common.h"
5f1ac36d 25#include "libavutil/avstring.h"
245976da 26#include "libavcodec/mpegaudio.h"
de6d9b64 27#include "avformat.h"
9d9f4119 28#include "riff.h"
542993b0 29#include "asf.h"
72be7db4 30#include "asfcrypt.h"
5f1ac36d 31#include "avlanguage.h"
de6d9b64 32
9686df2b 33void ff_mms_set_stream_selection(URLContext *h, AVFormatContext *format);
49ce700a 34
4bc328a2
AK
35typedef struct {
36 int asfid2avid[128]; ///< conversion table from asf ID 2 AVStream ID
37 ASFStream streams[128]; ///< it's max number and it's not that big
38 uint32_t stream_bitrates[128]; ///< max number of streams, bitrate for each (for streaming)
d42b0972 39 AVRational dar[128];
4bc328a2
AK
40 char stream_languages[128][6]; ///< max number of streams, language for each (RFC1766, e.g. en-US)
41 /* non streamed additonnal info */
4bc328a2
AK
42 /* packet filling */
43 int packet_size_left;
4bc328a2
AK
44 /* only for reading */
45 uint64_t data_offset; ///< beginning of the first data packet
46 uint64_t data_object_offset; ///< data object offset (excl. GUID & size)
47 uint64_t data_object_size; ///< size of the data object
48 int index_read;
49
50 ASFMainHeader hdr;
51
52 int packet_flags;
53 int packet_property;
54 int packet_timestamp;
55 int packet_segsizetype;
56 int packet_segments;
57 int packet_seq;
58 int packet_replic_size;
59 int packet_key_frame;
60 int packet_padsize;
61 unsigned int packet_frag_offset;
62 unsigned int packet_frag_size;
63 int64_t packet_frag_timestamp;
64 int packet_multi_size;
65 int packet_obj_size;
66 int packet_time_delta;
67 int packet_time_start;
68 int64_t packet_pos;
69
70 int stream_index;
71
72 ASFStream* asf_st; ///< currently decoded stream
73} ASFContext;
74
580fb5e7
MN
75#undef NDEBUG
76#include <assert.h>
77
30cdfb49 78#define ASF_MAX_STREAMS 127
615b92fd 79#define FRAME_HEADER_SIZE 17
115329f1 80// Fix Me! FRAME_HEADER_SIZE may be different.
615b92fd 81
b8b00d94 82static const ff_asf_guid index_guid = {
24c14d6d 83 0x90, 0x08, 0x00, 0x33, 0xb1, 0xe5, 0xcf, 0x11, 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb
de6d9b64
FB
84};
85
b8b00d94 86static const ff_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
0e3b6a6f
RM
87 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
88};
de6d9b64
FB
89/**********************************/
90/* decoding */
91
de6d9b64 92#ifdef DEBUG
5082f759 93#define PRINT_IF_GUID(g,cmp) \
a750050f 94if (!ff_guidcmp(g, &cmp)) \
dfd2a005 95 av_dlog(NULL, "(GUID: %s) ", #cmp)
5082f759 96
b8b00d94 97static void print_guid(const ff_asf_guid *g)
de6d9b64
FB
98{
99 int i;
17af0525
AJ
100 PRINT_IF_GUID(g, ff_asf_header);
101 else PRINT_IF_GUID(g, ff_asf_file_header);
102 else PRINT_IF_GUID(g, ff_asf_stream_header);
103 else PRINT_IF_GUID(g, ff_asf_audio_stream);
104 else PRINT_IF_GUID(g, ff_asf_audio_conceal_none);
105 else PRINT_IF_GUID(g, ff_asf_video_stream);
106 else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
107 else PRINT_IF_GUID(g, ff_asf_command_stream);
108 else PRINT_IF_GUID(g, ff_asf_comment_header);
109 else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
110 else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
111 else PRINT_IF_GUID(g, ff_asf_data_header);
5082f759 112 else PRINT_IF_GUID(g, index_guid);
17af0525
AJ
113 else PRINT_IF_GUID(g, ff_asf_head1_guid);
114 else PRINT_IF_GUID(g, ff_asf_head2_guid);
115 else PRINT_IF_GUID(g, ff_asf_my_guid);
116 else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
117 else PRINT_IF_GUID(g, ff_asf_extended_content_header);
118 else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
119 else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
120 else PRINT_IF_GUID(g, ff_asf_metadata_header);
88b51ea9 121 else PRINT_IF_GUID(g, ff_asf_marker_header);
0e3b6a6f 122 else PRINT_IF_GUID(g, stream_bitrate_guid);
5f1ac36d 123 else PRINT_IF_GUID(g, ff_asf_language_guid);
5082f759 124 else
dfd2a005 125 av_dlog(NULL, "(GUID: unknown) ");
24c14d6d 126 for(i=0;i<16;i++)
dfd2a005
LB
127 av_dlog(NULL, " 0x%02x,", (*g)[i]);
128 av_dlog(NULL, "}\n");
de6d9b64 129}
6df84c3a 130#undef PRINT_IF_GUID
f94981ea
MR
131#else
132#define print_guid(g)
de6d9b64
FB
133#endif
134
a750050f 135void ff_get_guid(ByteIOContext *s, ff_asf_guid *g)
de6d9b64 136{
24c14d6d 137 assert(sizeof(*g) == 16);
3dc761a4 138 get_buffer(s, *g, sizeof(*g));
de6d9b64
FB
139}
140
c9a65ca8
FB
141static int asf_probe(AVProbeData *pd)
142{
c9a65ca8 143 /* check file header */
a750050f 144 if (!ff_guidcmp(pd->buf, &ff_asf_header))
c9a65ca8
FB
145 return AVPROBE_SCORE_MAX;
146 else
147 return 0;
148}
149
30e88789
MN
150static int get_value(ByteIOContext *pb, int type){
151 switch(type){
152 case 2: return get_le32(pb);
153 case 3: return get_le32(pb);
154 case 4: return get_le64(pb);
155 case 5: return get_le16(pb);
156 default:return INT_MIN;
157 }
158}
159
20e2a7ae
AJ
160static void get_tag(AVFormatContext *s, const char *key, int type, int len)
161{
12ad6671 162 char *value;
04295054 163 int64_t off = url_ftell(s->pb);
12ad6671 164
fce44489 165 if ((unsigned)len >= (UINT_MAX - 1)/2)
12ad6671
MN
166 return;
167
fce44489 168 value = av_malloc(2*len+1);
12ad6671 169 if (!value)
04295054 170 goto finish;
12ad6671 171
6538c0f1 172 if (type == 0) { // UTF16-LE
93b78d12 173 avio_get_str16le(s->pb, len, value, 2*len + 1);
6538c0f1 174 } else if (type > 1 && type <= 5) { // boolean or DWORD or QWORD or WORD
20e2a7ae 175 uint64_t num = get_value(s->pb, type);
12ad6671 176 snprintf(value, len, "%"PRIu64, num);
20e2a7ae 177 } else {
27e74927 178 av_log(s, AV_LOG_DEBUG, "Unsupported value type %d in tag %s.\n", type, key);
04295054 179 goto finish;
20e2a7ae 180 }
fce44489 181 av_metadata_set2(&s->metadata, key, value, 0);
04295054 182finish:
fce44489 183 av_freep(&value);
04295054 184 url_fseek(s->pb, off + len, SEEK_SET);
20e2a7ae
AJ
185}
186
c1fea230
AK
187static int asf_read_file_properties(AVFormatContext *s, int64_t size)
188{
189 ASFContext *asf = s->priv_data;
190 ByteIOContext *pb = s->pb;
191
192 ff_get_guid(pb, &asf->hdr.guid);
193 asf->hdr.file_size = get_le64(pb);
194 asf->hdr.create_time = get_le64(pb);
195 get_le64(pb); /* number of packets */
196 asf->hdr.play_time = get_le64(pb);
197 asf->hdr.send_time = get_le64(pb);
198 asf->hdr.preroll = get_le32(pb);
199 asf->hdr.ignore = get_le32(pb);
200 asf->hdr.flags = get_le32(pb);
201 asf->hdr.min_pktsize = get_le32(pb);
202 asf->hdr.max_pktsize = get_le32(pb);
203 asf->hdr.max_bitrate = get_le32(pb);
204 s->packet_size = asf->hdr.max_pktsize;
205
206 return 0;
207}
208
209static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
210{
211 ASFContext *asf = s->priv_data;
212 ByteIOContext *pb = s->pb;
213 ff_asf_guid g;
214 int ext_len, payload_ext_ct, stream_ct, i;
215 uint32_t ext_d, leak_rate, stream_num;
216 unsigned int stream_languageid_index;
217
218 get_le64(pb); // starttime
219 get_le64(pb); // endtime
220 leak_rate = get_le32(pb); // leak-datarate
221 get_le32(pb); // bucket-datasize
222 get_le32(pb); // init-bucket-fullness
223 get_le32(pb); // alt-leak-datarate
224 get_le32(pb); // alt-bucket-datasize
225 get_le32(pb); // alt-init-bucket-fullness
226 get_le32(pb); // max-object-size
227 get_le32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
228 stream_num = get_le16(pb); // stream-num
229
230 stream_languageid_index = get_le16(pb); // stream-language-id-index
231 if (stream_num < 128)
232 asf->streams[stream_num].stream_language_index = stream_languageid_index;
233
234 get_le64(pb); // avg frametime in 100ns units
235 stream_ct = get_le16(pb); //stream-name-count
236 payload_ext_ct = get_le16(pb); //payload-extension-system-count
237
238 if (stream_num < 128)
239 asf->stream_bitrates[stream_num] = leak_rate;
240
241 for (i=0; i<stream_ct; i++){
242 get_le16(pb);
243 ext_len = get_le16(pb);
244 url_fseek(pb, ext_len, SEEK_CUR);
245 }
246
247 for (i=0; i<payload_ext_ct; i++){
248 ff_get_guid(pb, &g);
249 ext_d=get_le16(pb);
250 ext_len=get_le32(pb);
251 url_fseek(pb, ext_len, SEEK_CUR);
252 }
253
254 return 0;
255}
256
257static int asf_read_content_desc(AVFormatContext *s, int64_t size)
258{
259 ByteIOContext *pb = s->pb;
260 int len1, len2, len3, len4, len5;
261
262 len1 = get_le16(pb);
263 len2 = get_le16(pb);
264 len3 = get_le16(pb);
265 len4 = get_le16(pb);
266 len5 = get_le16(pb);
267 get_tag(s, "title" , 0, len1);
268 get_tag(s, "author" , 0, len2);
269 get_tag(s, "copyright", 0, len3);
270 get_tag(s, "comment" , 0, len4);
271 url_fskip(pb, len5);
272
273 return 0;
274}
275
276static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
277{
278 ByteIOContext *pb = s->pb;
279 ASFContext *asf = s->priv_data;
280 int desc_count, i, ret;
281
282 desc_count = get_le16(pb);
283 for(i=0;i<desc_count;i++) {
284 int name_len,value_type,value_len;
285 char name[1024];
286
287 name_len = get_le16(pb);
288 if (name_len%2) // must be even, broken lavf versions wrote len-1
289 name_len += 1;
290 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
291 url_fskip(pb, name_len - ret);
292 value_type = get_le16(pb);
293 value_len = get_le16(pb);
294 if (!value_type && value_len%2)
295 value_len += 1;
296 /**
297 * My sample has that stream set to 0 maybe that mean the container.
298 * Asf stream count start at 1. I am using 0 to the container value since it's unused
299 */
300 if (!strcmp(name, "AspectRatioX")){
301 asf->dar[0].num= get_value(s->pb, value_type);
302 } else if(!strcmp(name, "AspectRatioY")){
303 asf->dar[0].den= get_value(s->pb, value_type);
304 } else
305 get_tag(s, name, value_type, value_len);
306 }
307
308 return 0;
309}
310
311static int asf_read_language_list(AVFormatContext *s, int64_t size)
312{
313 ByteIOContext *pb = s->pb;
314 ASFContext *asf = s->priv_data;
315 int j, ret;
316 int stream_count = get_le16(pb);
317 for(j = 0; j < stream_count; j++) {
318 char lang[6];
319 unsigned int lang_len = get_byte(pb);
320 if ((ret = avio_get_str16le(pb, lang_len, lang, sizeof(lang))) < lang_len)
321 url_fskip(pb, lang_len - ret);
322 if (j < 128)
323 av_strlcpy(asf->stream_languages[j], lang, sizeof(*asf->stream_languages));
324 }
325
326 return 0;
327}
328
329static int asf_read_metadata(AVFormatContext *s, int64_t size)
330{
331 ByteIOContext *pb = s->pb;
332 ASFContext *asf = s->priv_data;
333 int n, stream_num, name_len, value_len, value_type, value_num;
334 int ret, i;
335 n = get_le16(pb);
336
337 for(i=0;i<n;i++) {
338 char name[1024];
339
340 get_le16(pb); //lang_list_index
341 stream_num= get_le16(pb);
342 name_len= get_le16(pb);
343 value_type= get_le16(pb);
344 value_len= get_le32(pb);
345
346 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
347 url_fskip(pb, name_len - ret);
348 //av_log(s, AV_LOG_ERROR, "%d %d %d %d %d <%s>\n", i, stream_num, name_len, value_type, value_len, name);
349 value_num= get_le16(pb);//we should use get_value() here but it does not work 2 is le16 here but le32 elsewhere
350 url_fskip(pb, value_len - 2);
351
352 if(stream_num<128){
353 if (!strcmp(name, "AspectRatioX")) asf->dar[stream_num].num= value_num;
354 else if(!strcmp(name, "AspectRatioY")) asf->dar[stream_num].den= value_num;
355 }
356 }
357
358 return 0;
359}
360
361static int asf_read_marker(AVFormatContext *s, int64_t size)
362{
363 ByteIOContext *pb = s->pb;
364 int i, count, name_len, ret;
365 char name[1024];
366
367 get_le64(pb); // reserved 16 bytes
368 get_le64(pb); // ...
369 count = get_le32(pb); // markers count
370 get_le16(pb); // reserved 2 bytes
371 name_len = get_le16(pb); // name length
372 for(i=0;i<name_len;i++){
373 get_byte(pb); // skip the name
374 }
375
376 for(i=0;i<count;i++){
377 int64_t pres_time;
378 int name_len;
379
380 get_le64(pb); // offset, 8 bytes
381 pres_time = get_le64(pb); // presentation time
382 get_le16(pb); // entry length
383 get_le32(pb); // send time
384 get_le32(pb); // flags
385 name_len = get_le32(pb); // name length
386 if ((ret = avio_get_str16le(pb, name_len * 2, name, sizeof(name))) < name_len)
387 url_fskip(pb, name_len - ret);
388 ff_new_chapter(s, i, (AVRational){1, 10000000}, pres_time, AV_NOPTS_VALUE, name );
389 }
390
391 return 0;
392}
393
de6d9b64
FB
394static int asf_read_header(AVFormatContext *s, AVFormatParameters *ap)
395{
c9a65ca8 396 ASFContext *asf = s->priv_data;
b8b00d94 397 ff_asf_guid g;
899681cd 398 ByteIOContext *pb = s->pb;
de6d9b64
FB
399 AVStream *st;
400 ASFStream *asf_st;
0b1d291a 401 int i;
0c1a9eda 402 int64_t gsize;
de6d9b64 403
a750050f
PR
404 ff_get_guid(pb, &g);
405 if (ff_guidcmp(&g, &ff_asf_header))
ccafd472 406 return -1;
de6d9b64
FB
407 get_le64(pb);
408 get_le32(pb);
409 get_byte(pb);
410 get_byte(pb);
2141dc37 411 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
de6d9b64 412 for(;;) {
cb0061d4 413 uint64_t gpos= url_ftell(pb);
a750050f 414 ff_get_guid(pb, &g);
de6d9b64 415 gsize = get_le64(pb);
dfd2a005 416 av_dlog(s, "%08"PRIx64": ", gpos);
de6d9b64 417 print_guid(&g);
dfd2a005 418 av_dlog(s, " size=0x%"PRIx64"\n", gsize);
a750050f 419 if (!ff_guidcmp(&g, &ff_asf_data_header)) {
09d9672a
MN
420 asf->data_object_offset = url_ftell(pb);
421 // if not streaming, gsize is not unlimited (how?), and there is enough space in the file..
422 if (!(asf->hdr.flags & 0x01) && gsize >= 100) {
423 asf->data_object_size = gsize - 24;
424 } else {
425 asf->data_object_size = (uint64_t)-1;
426 }
427 break;
428 }
de6d9b64 429 if (gsize < 24)
ccafd472 430 return -1;
a750050f 431 if (!ff_guidcmp(&g, &ff_asf_file_header)) {
c1fea230 432 asf_read_file_properties(s, gsize);
a750050f 433 } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
72415b2a 434 enum AVMediaType type;
fb65d2ca 435 int type_specific_size, sizeX;
7ad5455c 436 uint64_t total_size;
de6d9b64 437 unsigned int tag1;
371bf806 438 int64_t pos1, pos2, start_time;
6fea687e 439 int test_for_ext_stream_audio, is_dvr_ms_audio=0;
2a10020b 440
30cdfb49
MR
441 if (s->nb_streams == ASF_MAX_STREAMS) {
442 av_log(s, AV_LOG_ERROR, "too many streams\n");
443 return AVERROR(EINVAL);
444 }
445
de6d9b64
FB
446 pos1 = url_ftell(pb);
447
247eadca 448 st = av_new_stream(s, 0);
de6d9b64 449 if (!st)
ccafd472 450 return AVERROR(ENOMEM);
9ee91c2f 451 av_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
de6d9b64
FB
452 asf_st = av_mallocz(sizeof(ASFStream));
453 if (!asf_st)
ccafd472 454 return AVERROR(ENOMEM);
de6d9b64 455 st->priv_data = asf_st;
371bf806
MN
456 start_time = asf->hdr.preroll;
457
5f1ac36d
CC
458 asf_st->stream_language_index = 128; // invalid stream index means no language info
459
73fe1052 460 if(!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
f2086fb5 461 st->duration = asf->hdr.play_time /
371bf806 462 (10000000 / 1000) - start_time;
73fe1052 463 }
a750050f 464 ff_get_guid(pb, &g);
88141c91
MN
465
466 test_for_ext_stream_audio = 0;
a750050f 467 if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
72415b2a 468 type = AVMEDIA_TYPE_AUDIO;
a750050f 469 } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
72415b2a 470 type = AVMEDIA_TYPE_VIDEO;
a750050f 471 } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
a3ab7c32
MS
472 type = AVMEDIA_TYPE_VIDEO;
473 st->codec->codec_id = CODEC_ID_MJPEG;
a750050f 474 } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
72415b2a 475 type = AVMEDIA_TYPE_DATA;
a750050f 476 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
88141c91 477 test_for_ext_stream_audio = 1;
72415b2a 478 type = AVMEDIA_TYPE_UNKNOWN;
de6d9b64 479 } else {
ccafd472 480 return -1;
de6d9b64 481 }
a750050f 482 ff_get_guid(pb, &g);
de6d9b64 483 total_size = get_le64(pb);
2e7973bb 484 type_specific_size = get_le32(pb);
de6d9b64 485 get_le32(pb);
bb270c08 486 st->id = get_le16(pb) & 0x7f; /* stream id */
2a10020b 487 // mapping of asf ID to AV stream ID;
247eadca 488 asf->asfid2avid[st->id] = s->nb_streams - 1;
2a10020b 489
de6d9b64 490 get_le32(pb);
88141c91
MN
491
492 if (test_for_ext_stream_audio) {
a750050f
PR
493 ff_get_guid(pb, &g);
494 if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
72415b2a 495 type = AVMEDIA_TYPE_AUDIO;
6fea687e 496 is_dvr_ms_audio=1;
a750050f 497 ff_get_guid(pb, &g);
88141c91
MN
498 get_le32(pb);
499 get_le32(pb);
500 get_le32(pb);
a750050f 501 ff_get_guid(pb, &g);
88141c91
MN
502 get_le32(pb);
503 }
504 }
505
bb270c08 506 st->codec->codec_type = type;
72415b2a 507 if (type == AVMEDIA_TYPE_AUDIO) {
1a40491e 508 ff_get_wav_header(pb, st->codec, type_specific_size);
6fea687e
JD
509 if (is_dvr_ms_audio) {
510 // codec_id and codec_tag are unreliable in dvr_ms
511 // files. Set them later by probing stream.
0bef08e5 512 st->codec->codec_id = CODEC_ID_PROBE;
6fea687e
JD
513 st->codec->codec_tag = 0;
514 }
e7f73adb
AC
515 if (st->codec->codec_id == CODEC_ID_AAC) {
516 st->need_parsing = AVSTREAM_PARSE_NONE;
517 } else {
e9259f8d 518 st->need_parsing = AVSTREAM_PARSE_FULL;
e7f73adb 519 }
bb270c08
DB
520 /* We have to init the frame size at some point .... */
521 pos2 = url_ftell(pb);
b9c6d01b 522 if (gsize >= (pos2 + 8 - pos1 + 24)) {
bb270c08
DB
523 asf_st->ds_span = get_byte(pb);
524 asf_st->ds_packet_size = get_le16(pb);
525 asf_st->ds_chunk_size = get_le16(pb);
6bb2a2df
MN
526 get_le16(pb); //ds_data_size
527 get_byte(pb); //ds_silence_data
bb270c08
DB
528 }
529 //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n",
530 // asf_st->ds_packet_size, asf_st->ds_chunk_size,
531 // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
532 if (asf_st->ds_span > 1) {
533 if (!asf_st->ds_chunk_size
05219463
MN
534 || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)
535 || asf_st->ds_packet_size % asf_st->ds_chunk_size)
bb270c08
DB
536 asf_st->ds_span = 0; // disable descrambling
537 }
01f4895c 538 switch (st->codec->codec_id) {
80783dc2 539 case CODEC_ID_MP3:
01f4895c 540 st->codec->frame_size = MPA_FRAME_SIZE;
f80c1ac0
PG
541 break;
542 case CODEC_ID_PCM_S16LE:
543 case CODEC_ID_PCM_S16BE:
544 case CODEC_ID_PCM_U16LE:
545 case CODEC_ID_PCM_U16BE:
546 case CODEC_ID_PCM_S8:
547 case CODEC_ID_PCM_U8:
548 case CODEC_ID_PCM_ALAW:
549 case CODEC_ID_PCM_MULAW:
01f4895c 550 st->codec->frame_size = 1;
f80c1ac0
PG
551 break;
552 default:
553 /* This is probably wrong, but it prevents a crash later */
01f4895c 554 st->codec->frame_size = 1;
f80c1ac0
PG
555 break;
556 }
fed79592 557 } else if (type == AVMEDIA_TYPE_VIDEO &&
d970ec75 558 gsize - (url_ftell(pb) - pos1 + 24) >= 51) {
bb270c08 559 get_le32(pb);
de6d9b64
FB
560 get_le32(pb);
561 get_byte(pb);
0b1d291a 562 get_le16(pb); /* size */
0ffd77b9 563 sizeX= get_le32(pb); /* size */
01f4895c 564 st->codec->width = get_le32(pb);
bb270c08 565 st->codec->height = get_le32(pb);
2a10020b 566 /* not available for asf */
de6d9b64 567 get_le16(pb); /* panes */
dd1c8f3e 568 st->codec->bits_per_coded_sample = get_le16(pb); /* depth */
de6d9b64 569 tag1 = get_le32(pb);
bb270c08 570 url_fskip(pb, 20);
871e62e5 571// av_log(s, AV_LOG_DEBUG, "size:%d tsize:%d sizeX:%d\n", size, total_size, sizeX);
0b1d291a
AK
572 if (sizeX > 40) {
573 st->codec->extradata_size = sizeX - 40;
bb270c08
DB
574 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
575 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
576 }
5e29abf8 577
6d050dd6 578 /* Extract palette from extradata if bpp <= 8 */
579 /* This code assumes that extradata contains only palette */
580 /* This is true for all paletted codecs implemented in ffmpeg */
581 if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
582 st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
63613fe6 583#if HAVE_BIGENDIAN
6d050dd6 584 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
8fc0162a 585 st->codec->palctrl->palette[i] = av_bswap32(((uint32_t*)st->codec->extradata)[i]);
5e29abf8 586#else
6d050dd6 587 memcpy(st->codec->palctrl->palette, st->codec->extradata,
588 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
5e29abf8 589#endif
6d050dd6 590 st->codec->palctrl->palette_changed = 1;
591 }
5e29abf8 592
01f4895c 593 st->codec->codec_tag = tag1;
1a40491e 594 st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag1);
31247669 595 if(tag1 == MKTAG('D', 'V', 'R', ' ')){
57004ff1 596 st->need_parsing = AVSTREAM_PARSE_FULL;
31247669
MN
597 // issue658 containse wrong w/h and MS even puts a fake seq header with wrong w/h in extradata while a correct one is in te stream. maximum lameness
598 st->codec->width =
599 st->codec->height = 0;
600 av_freep(&st->codec->extradata);
601 st->codec->extradata_size=0;
602 }
1ed87f80
AC
603 if(st->codec->codec_id == CODEC_ID_H264)
604 st->need_parsing = AVSTREAM_PARSE_FULL_ONCE;
de6d9b64
FB
605 }
606 pos2 = url_ftell(pb);
607 url_fskip(pb, gsize - (pos2 - pos1 + 24));
a750050f 608 } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
c1fea230 609 asf_read_content_desc(s, gsize);
a750050f 610 } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
c1fea230 611 asf_read_language_list(s, gsize);
a750050f 612 } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
c1fea230 613 asf_read_ext_content_desc(s, gsize);
a750050f 614 } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
c1fea230 615 asf_read_metadata(s, gsize);
a750050f 616 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
c1fea230 617 asf_read_ext_stream_properties(s, gsize);
88141c91
MN
618
619 // there could be a optional stream properties object to follow
620 // if so the next iteration will pick it up
1d4c1c3b 621 continue;
a750050f 622 } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
de6d9b64 623 int v1, v2;
a750050f 624 ff_get_guid(pb, &g);
de6d9b64
FB
625 v1 = get_le32(pb);
626 v2 = get_le16(pb);
1d4c1c3b 627 continue;
a750050f 628 } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
c1fea230 629 asf_read_marker(s, gsize);
de6d9b64 630 } else if (url_feof(pb)) {
ccafd472 631 return -1;
de6d9b64 632 } else {
febd1c90 633 if (!s->keylen) {
a750050f 634 if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
febd1c90 635 av_log(s, AV_LOG_WARNING, "DRM protected stream detected, decoding will likely fail!\n");
a750050f 636 } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
febd1c90 637 av_log(s, AV_LOG_WARNING, "Ext DRM protected stream detected, decoding will likely fail!\n");
a750050f 638 } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
febd1c90
DT
639 av_log(s, AV_LOG_WARNING, "Digital signature detected, decoding will likely fail!\n");
640 }
641 }
de6d9b64 642 }
cb0061d4
MN
643 if(url_ftell(pb) != gpos + gsize)
644 av_log(s, AV_LOG_DEBUG, "gpos mismatch our pos=%"PRIu64", end=%"PRIu64"\n", url_ftell(pb)-gpos, gsize);
645 url_fseek(pb, gpos + gsize, SEEK_SET);
de6d9b64 646 }
a750050f 647 ff_get_guid(pb, &g);
de6d9b64
FB
648 get_le64(pb);
649 get_byte(pb);
650 get_byte(pb);
2141dc37 651 if (url_feof(pb))
ccafd472 652 return -1;
2141dc37 653 asf->data_offset = url_ftell(pb);
de6d9b64
FB
654 asf->packet_size_left = 0;
655
30e88789
MN
656
657 for(i=0; i<128; i++){
658 int stream_num= asf->asfid2avid[i];
d8470737 659 if(stream_num>=0){
59729451
AJ
660 AVStream *st = s->streams[stream_num];
661 if (!st->codec->bit_rate)
7c725380 662 st->codec->bit_rate = asf->stream_bitrates[i];
d42b0972 663 if (asf->dar[i].num > 0 && asf->dar[i].den > 0){
59729451
AJ
664 av_reduce(&st->sample_aspect_ratio.num,
665 &st->sample_aspect_ratio.den,
d42b0972
AK
666 asf->dar[i].num, asf->dar[i].den, INT_MAX);
667 } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) && (st->codec->codec_type==AVMEDIA_TYPE_VIDEO)) // Use ASF container value if the stream doesn't AR set.
6ac6e3d1
RB
668 av_reduce(&st->sample_aspect_ratio.num,
669 &st->sample_aspect_ratio.den,
d42b0972 670 asf->dar[0].num, asf->dar[0].den, INT_MAX);
6ac6e3d1
RB
671
672//av_log(s, AV_LOG_INFO, "i=%d, st->codec->codec_type:%d, dar %d:%d sar=%d:%d\n", i, st->codec->codec_type, dar[i].num, dar[i].den, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
5f1ac36d
CC
673
674 // copy and convert language codes to the frontend
675 if (asf->streams[i].stream_language_index < 128) {
676 const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
677 if (rfc1766 && strlen(rfc1766) > 1) {
678 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
679 const char *iso6392 = av_convert_lang_to(primary_tag, AV_LANG_ISO639_2_BIBL);
680 if (iso6392)
2ef6c124 681 av_metadata_set2(&st->metadata, "language", iso6392, 0);
5f1ac36d
CC
682 }
683 }
30e88789
MN
684 }
685 }
686
ad7768f4 687 ff_metadata_conv(&s->metadata, NULL, ff_asf_metadata_conv);
03700d39 688
de6d9b64 689 return 0;
de6d9b64
FB
690}
691
2a10020b
ZK
692#define DO_2BITS(bits, var, defval) \
693 switch (bits & 3) \
694 { \
695 case 3: var = get_le32(pb); rsize += 4; break; \
696 case 2: var = get_le16(pb); rsize += 2; break; \
697 case 1: var = get_byte(pb); rsize++; break; \
698 default: var = defval; break; \
699 }
700
277a28e8
RB
701/**
702 * Load a single ASF packet into the demuxer.
703 * @param s demux context
704 * @param pb context to read data from
32e543f8 705 * @return 0 on success, <0 on error
277a28e8
RB
706 */
707static int ff_asf_get_packet(AVFormatContext *s, ByteIOContext *pb)
de6d9b64
FB
708{
709 ASFContext *asf = s->priv_data;
2a10020b 710 uint32_t packet_length, padsize;
0c0d17f3 711 int rsize = 8;
0fd9e6f3
MN
712 int c, d, e, off;
713
68a10b40
RD
714 // if we do not know packet size, allow skipping up to 32 kB
715 off= 32768;
716 if (s->packet_size > 0)
b098b309 717 off= (url_ftell(pb) - s->data_offset) % s->packet_size + 3;
0fd9e6f3
MN
718
719 c=d=e=-1;
720 while(off-- > 0){
721 c=d; d=e;
722 e= get_byte(pb);
723 if(c == 0x82 && !d && !e)
724 break;
725 }
115329f1 726
1359c2d4 727 if (c != 0x82) {
2078ca16
RB
728 /**
729 * This code allows handling of -EAGAIN at packet boundaries (i.e.
730 * if the packet sync code above triggers -EAGAIN). This does not
731 * imply complete -EAGAIN handling support at random positions in
732 * the stream.
733 */
734 if (url_ferror(pb) == AVERROR(EAGAIN))
735 return AVERROR(EAGAIN);
5acdd6e6 736 if (!url_feof(pb))
bb270c08 737 av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, url_ftell(pb));
2a10020b 738 }
0c0d17f3 739 if ((c & 0x8f) == 0x82) {
0fd9e6f3 740 if (d || e) {
5acdd6e6 741 if (!url_feof(pb))
bb270c08 742 av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
f7835507 743 return -1;
bb270c08 744 }
0c0d17f3 745 c= get_byte(pb);
0fd9e6f3 746 d= get_byte(pb);
0c0d17f3
MN
747 rsize+=3;
748 }else{
749 url_fseek(pb, -1, SEEK_CUR); //FIXME
de6d9b64 750 }
2a10020b 751
0c0d17f3
MN
752 asf->packet_flags = c;
753 asf->packet_property = d;
2a10020b 754
91d19d47 755 DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
2a10020b
ZK
756 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
757 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
758
5b11e081 759 //the following checks prevent overflows and infinite loops
96f0731a 760 if(!packet_length || packet_length >= (1U<<29)){
5b11e081 761 av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, url_ftell(pb));
ceba96cb 762 return -1;
5b11e081 763 }
92216453 764 if(padsize >= packet_length){
5b11e081 765 av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, url_ftell(pb));
ceba96cb 766 return -1;
5b11e081
MN
767 }
768
2a10020b 769 asf->packet_timestamp = get_le32(pb);
de6d9b64 770 get_le16(pb); /* duration */
2141dc37 771 // rsize has at least 11 bytes which have to be present
2a10020b
ZK
772
773 if (asf->packet_flags & 0x01) {
bb270c08 774 asf->packet_segsizetype = get_byte(pb); rsize++;
2a10020b
ZK
775 asf->packet_segments = asf->packet_segsizetype & 0x3f;
776 } else {
bb270c08 777 asf->packet_segments = 1;
2a10020b
ZK
778 asf->packet_segsizetype = 0x80;
779 }
780 asf->packet_size_left = packet_length - padsize - rsize;
acbe6cfa
ZK
781 if (packet_length < asf->hdr.min_pktsize)
782 padsize += asf->hdr.min_pktsize - packet_length;
2a10020b 783 asf->packet_padsize = padsize;
dfd2a005 784 av_dlog(s, "packet: size=%d padsize=%d left=%d\n", s->packet_size, asf->packet_padsize, asf->packet_size_left);
de6d9b64
FB
785 return 0;
786}
787
ae60a857
MN
788/**
789 *
790 * @return <0 if error
791 */
d7fb5a18 792static int asf_read_frame_header(AVFormatContext *s, ByteIOContext *pb){
ae60a857 793 ASFContext *asf = s->priv_data;
ae60a857
MN
794 int rsize = 1;
795 int num = get_byte(pb);
559a55ad 796 int64_t ts0, ts1;
ae60a857
MN
797
798 asf->packet_segments--;
799 asf->packet_key_frame = num >> 7;
800 asf->stream_index = asf->asfid2avid[num & 0x7f];
801 // sequence should be ignored!
802 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
803 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
804 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
805//printf("key:%d stream:%d seq:%d offset:%d replic_size:%d\n", asf->packet_key_frame, asf->stream_index, asf->packet_seq, //asf->packet_frag_offset, asf->packet_replic_size);
806 if (asf->packet_replic_size >= 8) {
807 asf->packet_obj_size = get_le32(pb);
808 if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
809 av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
810 return -1;
811 }
812 asf->packet_frag_timestamp = get_le32(pb); // timestamp
559a55ad
MN
813 if(asf->packet_replic_size >= 8+38+4){
814// for(i=0; i<asf->packet_replic_size-8; i++)
815// av_log(s, AV_LOG_DEBUG, "%02X ",get_byte(pb));
816// av_log(s, AV_LOG_DEBUG, "\n");
817 url_fskip(pb, 10);
818 ts0= get_le64(pb);
819 ts1= get_le64(pb);
820 url_fskip(pb, 12);
821 get_le32(pb);
822 url_fskip(pb, asf->packet_replic_size - 8 - 38 - 4);
823 if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
824 else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
825 }else
826 url_fskip(pb, asf->packet_replic_size - 8);
ae60a857
MN
827 rsize += asf->packet_replic_size; // FIXME - check validity
828 } else if (asf->packet_replic_size==1){
efc90ebf 829 // multipacket - frag_offset is beginning timestamp
ae60a857
MN
830 asf->packet_time_start = asf->packet_frag_offset;
831 asf->packet_frag_offset = 0;
832 asf->packet_frag_timestamp = asf->packet_timestamp;
833
834 asf->packet_time_delta = get_byte(pb);
835 rsize++;
836 }else if(asf->packet_replic_size!=0){
837 av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
838 return -1;
839 }
840 if (asf->packet_flags & 0x01) {
841 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
13f7e4b9
MN
842 if(asf->packet_frag_size > asf->packet_size_left - rsize){
843 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid\n");
844 return -1;
845 }
ae60a857
MN
846 //printf("Fragsize %d\n", asf->packet_frag_size);
847 } else {
848 asf->packet_frag_size = asf->packet_size_left - rsize;
849 //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
850 }
851 if (asf->packet_replic_size == 1) {
852 asf->packet_multi_size = asf->packet_frag_size;
853 if (asf->packet_multi_size > asf->packet_size_left)
854 return -1;
855 }
856 asf->packet_size_left -= rsize;
857 //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
858
859 return 0;
860}
861
277a28e8
RB
862/**
863 * Parse data from individual ASF packets (which were previously loaded
864 * with asf_get_packet()).
865 * @param s demux context
866 * @param pb context to read data from
867 * @param pkt pointer to store packet data into
32e543f8 868 * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
277a28e8
RB
869 * packets need to be loaded (through asf_get_packet())
870 */
871static int ff_asf_parse_packet(AVFormatContext *s, ByteIOContext *pb, AVPacket *pkt)
de6d9b64
FB
872{
873 ASFContext *asf = s->priv_data;
2a10020b 874 ASFStream *asf_st = 0;
2a10020b 875 for (;;) {
4172951b 876 int ret;
ceba96cb 877 if(url_feof(pb))
799a0722 878 return AVERROR_EOF;
bb270c08
DB
879 if (asf->packet_size_left < FRAME_HEADER_SIZE
880 || asf->packet_segments < 1) {
881 //asf->packet_size_left <= asf->packet_padsize) {
882 int ret = asf->packet_size_left + asf->packet_padsize;
949b1a13 883 //printf("PacketLeftSize:%d Pad:%d Pos:%"PRId64"\n", asf->packet_size_left, asf->packet_padsize, url_ftell(pb));
5b11e081 884 assert(ret>=0);
bb270c08
DB
885 /* fail safe */
886 url_fskip(pb, ret);
b04d5d31 887
022f0b7c 888 asf->packet_pos= url_ftell(pb);
b6eaae39
KED
889 if (asf->data_object_size != (uint64_t)-1 &&
890 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
1d0036b0 891 return AVERROR_EOF; /* Do not exceed the size of the data object */
f27544fd 892 return 1;
bb270c08
DB
893 }
894 if (asf->packet_time_start == 0) {
b0d75d60 895 if(asf_read_frame_header(s, pb) < 0){
ae60a857 896 asf->packet_segments= 0;
236116cb 897 continue;
1305a9d5 898 }
bb270c08 899 if (asf->stream_index < 0
f3356e9c
MN
900 || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
901 || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
902 ) {
1359c2d4 903 asf->packet_time_start = 0;
bb270c08
DB
904 /* unhandled packet (should not happen) */
905 url_fskip(pb, asf->packet_frag_size);
906 asf->packet_size_left -= asf->packet_frag_size;
b9866ebc 907 if(asf->stream_index < 0)
ae60a857 908 av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
1359c2d4 909 continue;
bb270c08
DB
910 }
911 asf->asf_st = s->streams[asf->stream_index]->priv_data;
912 }
913 asf_st = asf->asf_st;
914
bb270c08 915 if (asf->packet_replic_size == 1) {
efc90ebf 916 // frag_offset is here used as the beginning timestamp
bb270c08
DB
917 asf->packet_frag_timestamp = asf->packet_time_start;
918 asf->packet_time_start += asf->packet_time_delta;
919 asf->packet_obj_size = asf->packet_frag_size = get_byte(pb);
920 asf->packet_size_left--;
2141dc37 921 asf->packet_multi_size--;
bb270c08
DB
922 if (asf->packet_multi_size < asf->packet_obj_size)
923 {
924 asf->packet_time_start = 0;
925 url_fskip(pb, asf->packet_multi_size);
926 asf->packet_size_left -= asf->packet_multi_size;
2141dc37 927 continue;
bb270c08
DB
928 }
929 asf->packet_multi_size -= asf->packet_obj_size;
930 //printf("COMPRESS size %d %d %d ms:%d\n", asf->packet_obj_size, asf->packet_frag_timestamp, asf->packet_size_left, asf->packet_multi_size);
931 }
60ff1c44
MN
932 if( /*asf->packet_frag_size == asf->packet_obj_size*/
933 asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size
934 && asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size){
935 av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
936 asf_st->frag_offset, asf->packet_frag_size,
937 asf->packet_obj_size, asf_st->pkt.size);
938 asf->packet_obj_size= asf_st->pkt.size;
939 }
940
f8f0a0bd
MN
941 if ( asf_st->pkt.size != asf->packet_obj_size
942 || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) { //FIXME is this condition sufficient?
200026aa
MN
943 if(asf_st->pkt.data){
944 av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
945 asf_st->frag_offset = 0;
946 av_free_packet(&asf_st->pkt);
947 }
bb270c08
DB
948 /* new packet */
949 av_new_packet(&asf_st->pkt, asf->packet_obj_size);
950 asf_st->seq = asf->packet_seq;
eb63b051 951 asf_st->pkt.dts = asf->packet_frag_timestamp;
bb270c08 952 asf_st->pkt.stream_index = asf->stream_index;
115329f1
DB
953 asf_st->pkt.pos =
954 asf_st->packet_pos= asf->packet_pos;
955//printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
cc947f04 956//asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & AV_PKT_FLAG_KEY,
72415b2a
SS
957//s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO, asf->packet_obj_size);
958 if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
bb270c08
DB
959 asf->packet_key_frame = 1;
960 if (asf->packet_key_frame)
cc947f04 961 asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
bb270c08
DB
962 }
963
964 /* read data */
965 //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
91d19d47 966 // s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
bb270c08
DB
967 // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
968 asf->packet_size_left -= asf->packet_frag_size;
969 if (asf->packet_size_left < 0)
2141dc37 970 continue;
4c71d727
MN
971
972 if( asf->packet_frag_offset >= asf_st->pkt.size
973 || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
974 av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
975 asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
976 continue;
977 }
978
4172951b
RD
979 ret = get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
980 asf->packet_frag_size);
bf09a019
RD
981 if (ret != asf->packet_frag_size) {
982 if (ret < 0 || asf->packet_frag_offset + ret == 0)
983 return ret < 0 ? ret : AVERROR_EOF;
984 if (asf_st->ds_span > 1) {
985 // scrambling, we can either drop it completely or fill the remainder
986 // TODO: should we fill the whole packet instead of just the current
987 // fragment?
988 memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
989 asf->packet_frag_size - ret);
990 ret = asf->packet_frag_size;
991 } else
992 // no scrambling, so we can return partial packets
993 av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
994 }
72be7db4
RD
995 if (s->key && s->keylen == 20)
996 ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
bf09a019
RD
997 ret);
998 asf_st->frag_offset += ret;
bb270c08
DB
999 /* test if whole packet is read */
1000 if (asf_st->frag_offset == asf_st->pkt.size) {
da0b94f5
MN
1001 //workaround for macroshit radio DVR-MS files
1002 if( s->streams[asf->stream_index]->codec->codec_id == CODEC_ID_MPEG2VIDEO
1003 && asf_st->pkt.size > 100){
1004 int i;
1005 for(i=0; i<asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
1006 if(i == asf_st->pkt.size){
1007 av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1008 asf_st->frag_offset = 0;
1009 av_free_packet(&asf_st->pkt);
1010 continue;
1011 }
1012 }
1013
bb270c08
DB
1014 /* return packet */
1015 if (asf_st->ds_span > 1) {
05219463 1016 if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
b43387d4 1017 av_log(s, AV_LOG_ERROR, "pkt.size != ds_packet_size * ds_span (%d %d %d)\n", asf_st->pkt.size, asf_st->ds_packet_size, asf_st->ds_span);
05219463 1018 }else{
bb270c08 1019 /* packet descrambling */
3c3ef81b 1020 uint8_t *newdata = av_malloc(asf_st->pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
bb270c08
DB
1021 if (newdata) {
1022 int offset = 0;
3c3ef81b 1023 memset(newdata + asf_st->pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
bb270c08
DB
1024 while (offset < asf_st->pkt.size) {
1025 int off = offset / asf_st->ds_chunk_size;
1026 int row = off / asf_st->ds_span;
1027 int col = off % asf_st->ds_span;
1028 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1029 //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
05219463
MN
1030
1031 assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
1032 assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
bb270c08
DB
1033 memcpy(newdata + offset,
1034 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1035 asf_st->ds_chunk_size);
1036 offset += asf_st->ds_chunk_size;
1037 }
1038 av_free(asf_st->pkt.data);
1039 asf_st->pkt.data = newdata;
1040 }
05219463 1041 }
bb270c08
DB
1042 }
1043 asf_st->frag_offset = 0;
b83e83b9 1044 *pkt= asf_st->pkt;
bb270c08
DB
1045 //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
1046 asf_st->pkt.size = 0;
1047 asf_st->pkt.data = 0;
1048 break; // packet completed
1049 }
de6d9b64 1050 }
de6d9b64
FB
1051 return 0;
1052}
1053
f27544fd
RB
1054static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1055{
1056 ASFContext *asf = s->priv_data;
1057
1058 for (;;) {
1059 int ret;
1060
1061 /* parse cached packets, if any */
1d217cdb 1062 if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
f27544fd 1063 return ret;
1d217cdb 1064 if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
f27544fd
RB
1065 assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
1066 asf->packet_time_start = 0;
1067 }
1068
1069 return 0;
1070}
1071
38376f00
KK
1072// Added to support seeking after packets have been read
1073// If information is not reset, read_packet fails due to
1074// leftover information from previous reads
1075static void asf_reset_header(AVFormatContext *s)
1076{
1077 ASFContext *asf = s->priv_data;
82b9e4a2
MN
1078 ASFStream *asf_st;
1079 int i;
38376f00 1080
38376f00
KK
1081 asf->packet_size_left = 0;
1082 asf->packet_segments = 0;
1083 asf->packet_flags = 0;
1084 asf->packet_property = 0;
1085 asf->packet_timestamp = 0;
1086 asf->packet_segsizetype = 0;
1087 asf->packet_segments = 0;
1088 asf->packet_seq = 0;
1089 asf->packet_replic_size = 0;
1090 asf->packet_key_frame = 0;
1091 asf->packet_padsize = 0;
1092 asf->packet_frag_offset = 0;
1093 asf->packet_frag_size = 0;
1094 asf->packet_frag_timestamp = 0;
1095 asf->packet_multi_size = 0;
1096 asf->packet_obj_size = 0;
1097 asf->packet_time_delta = 0;
1098 asf->packet_time_start = 0;
115329f1 1099
82b9e4a2
MN
1100 for(i=0; i<s->nb_streams; i++){
1101 asf_st= s->streams[i]->priv_data;
1102 av_free_packet(&asf_st->pkt);
1103 asf_st->frag_offset=0;
1104 asf_st->seq=0;
1105 }
1106 asf->asf_st= NULL;
38376f00
KK
1107}
1108
5d25d2a9
DB
1109static int asf_read_close(AVFormatContext *s)
1110{
1111 int i;
1112
1113 asf_reset_header(s);
1114 for(i=0;i<s->nb_streams;i++) {
1115 AVStream *st = s->streams[i];
5d25d2a9
DB
1116 av_free(st->codec->palctrl);
1117 }
1118 return 0;
1119}
1120
8d14a25c 1121static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
38376f00 1122{
38376f00 1123 AVPacket pkt1, *pkt = &pkt1;
82b9e4a2 1124 ASFStream *asf_st;
580fb5e7 1125 int64_t pts;
82b9e4a2 1126 int64_t pos= *ppos;
3e9245a9 1127 int i;
30cdfb49 1128 int64_t start_pos[ASF_MAX_STREAMS];
115329f1 1129
3e9245a9
MN
1130 for(i=0; i<s->nb_streams; i++){
1131 start_pos[i]= pos;
1132 }
115329f1 1133
68a10b40 1134 if (s->packet_size > 0)
b098b309 1135 pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
8d14a25c 1136 *ppos= pos;
899681cd 1137 url_fseek(s->pb, pos, SEEK_SET);
115329f1 1138
82b9e4a2 1139//printf("asf_read_pts\n");
82b9e4a2 1140 asf_reset_header(s);
3e9245a9 1141 for(;;){
82b9e4a2 1142 if (av_read_frame(s, pkt) < 0){
0b93103a 1143 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
bb270c08 1144 return AV_NOPTS_VALUE;
82b9e4a2 1145 }
115329f1 1146
38cf2a72 1147 pts= pkt->pts;
580fb5e7
MN
1148
1149 av_free_packet(pkt);
cc947f04 1150 if(pkt->flags&AV_PKT_FLAG_KEY){
3e9245a9
MN
1151 i= pkt->stream_index;
1152
1153 asf_st= s->streams[i]->priv_data;
1154
91d19d47 1155// assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
8d14a25c 1156 pos= asf_st->packet_pos;
09646bab 1157
52e54612 1158 av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
8d14a25c 1159 start_pos[i]= asf_st->packet_pos + 1;
115329f1 1160
3e9245a9
MN
1161 if(pkt->stream_index == stream_index)
1162 break;
1163 }
1164 }
1165
1166 *ppos= pos;
949b1a13 1167//printf("found keyframe at %"PRId64" stream %d stamp:%"PRId64"\n", *ppos, stream_index, pts);
580fb5e7
MN
1168
1169 return pts;
38376f00
KK
1170}
1171
56c96a22
MN
1172static void asf_build_simple_index(AVFormatContext *s, int stream_index)
1173{
b8b00d94 1174 ff_asf_guid g;
56c96a22 1175 ASFContext *asf = s->priv_data;
d728e3cd 1176 int64_t current_pos= url_ftell(s->pb);
56c96a22 1177 int i;
56c96a22 1178
899681cd 1179 url_fseek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
a750050f 1180 ff_get_guid(s->pb, &g);
77b7c334
SG
1181
1182 /* the data object can be followed by other top-level objects,
1183 skip them until the simple index object is reached */
a750050f 1184 while (ff_guidcmp(&g, &index_guid)) {
77b7c334
SG
1185 int64_t gsize= get_le64(s->pb);
1186 if (gsize < 24 || url_feof(s->pb)) {
1187 url_fseek(s->pb, current_pos, SEEK_SET);
1188 return;
1189 }
1190 url_fseek(s->pb, gsize-24, SEEK_CUR);
a750050f 1191 ff_get_guid(s->pb, &g);
77b7c334
SG
1192 }
1193
1194 {
134271c2 1195 int64_t itime, last_pos=-1;
d728e3cd 1196 int pct, ict;
dc1ed614 1197 int64_t av_unused gsize= get_le64(s->pb);
a750050f 1198 ff_get_guid(s->pb, &g);
899681cd
BA
1199 itime=get_le64(s->pb);
1200 pct=get_le32(s->pb);
1201 ict=get_le32(s->pb);
871e62e5 1202 av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
56c96a22
MN
1203
1204 for (i=0;i<ict;i++){
899681cd
BA
1205 int pktnum=get_le32(s->pb);
1206 int pktct =get_le16(s->pb);
91d19d47 1207 int64_t pos = s->data_offset + s->packet_size*(int64_t)pktnum;
d728e3cd 1208 int64_t index_pts= av_rescale(itime, i, 10000);
56c96a22 1209
134271c2 1210 if(pos != last_pos){
d728e3cd 1211 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d\n", pktnum, pktct);
91d19d47 1212 av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
134271c2
MN
1213 last_pos=pos;
1214 }
56c96a22 1215 }
52e54612 1216 asf->index_read= 1;
56c96a22 1217 }
899681cd 1218 url_fseek(s->pb, current_pos, SEEK_SET);
56c96a22
MN
1219}
1220
3ba1438d 1221static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
2a10020b 1222{
afda223c 1223 ASFContext *asf = s->priv_data;
56c96a22
MN
1224 AVStream *st = s->streams[stream_index];
1225 int64_t pos;
1226 int index;
115329f1 1227
91d19d47 1228 if (s->packet_size <= 0)
38376f00
KK
1229 return -1;
1230
fa13095a 1231 /* Try using the protocol's read_seek if available */
fd2982a0 1232 if(s->pb) {
fa13095a
BA
1233 int ret = av_url_read_fseek(s->pb, stream_index, pts, flags);
1234 if(ret >= 0)
1235 asf_reset_header(s);
fd2982a0 1236 if (ret != AVERROR(ENOSYS))
cd31f8dc 1237 return ret;
fa13095a
BA
1238 }
1239
52e54612 1240 if (!asf->index_read)
56c96a22
MN
1241 asf_build_simple_index(s, stream_index);
1242
52e54612 1243 if(!(asf->index_read && st->index_entries)){
56c96a22
MN
1244 if(av_seek_frame_binary(s, stream_index, pts, flags)<0)
1245 return -1;
1246 }else{
1247 index= av_index_search_timestamp(st, pts, flags);
1248 if(index<0)
1249 return -1;
1250
1251 /* find the position */
1252 pos = st->index_entries[index].pos;
56c96a22
MN
1253
1254 // various attempts to find key frame have failed so far
1255 // asf_reset_header(s);
899681cd 1256 // url_fseek(s->pb, pos, SEEK_SET);
56c96a22
MN
1257 // key_pos = pos;
1258 // for(i=0;i<16;i++){
899681cd 1259 // pos = url_ftell(s->pb);
56c96a22
MN
1260 // if (av_read_frame(s, &pkt) < 0){
1261 // av_log(s, AV_LOG_INFO, "seek failed\n");
1262 // return -1;
1263 // }
1264 // asf_st = s->streams[stream_index]->priv_data;
1265 // pos += st->parser->frame_offset;
1266 //
1267 // if (pkt.size > b) {
1268 // b = pkt.size;
1269 // key_pos = pos;
1270 // }
1271 //
1272 // av_free_packet(&pkt);
1273 // }
1274
1275 /* do the seek */
871e62e5 1276 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
899681cd 1277 url_fseek(s->pb, pos, SEEK_SET);
56c96a22 1278 }
38376f00
KK
1279 asf_reset_header(s);
1280 return 0;
2a10020b
ZK
1281}
1282
c6610a21 1283AVInputFormat ff_asf_demuxer = {
c9a65ca8 1284 "asf",
bde15e74 1285 NULL_IF_CONFIG_SMALL("ASF format"),
c9a65ca8
FB
1286 sizeof(ASFContext),
1287 asf_probe,
1288 asf_read_header,
1289 asf_read_packet,
1290 asf_read_close,
2a10020b 1291 asf_read_seek,
8d14a25c 1292 asf_read_pts,
c9a65ca8 1293};