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