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