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