apetag: reindent
[libav.git] / libavformat / asfdec.c
CommitLineData
de6d9b64 1/*
7fbde343 2 * ASF compatible demuxer
406792e7 3 * Copyright (c) 2000, 2001 Fabrice Bellard
de6d9b64 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav 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 *
2912e87a 12 * Libav 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
2912e87a 18 * License along with Libav; 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
b122c651 24#include "libavutil/bswap.h"
245976da 25#include "libavutil/common.h"
5f1ac36d 26#include "libavutil/avstring.h"
d2d67e42 27#include "libavutil/dict.h"
0ebcdf5c 28#include "libavutil/mathematics.h"
75b7feae 29#include "libavutil/opt.h"
de6d9b64 30#include "avformat.h"
c3f9ebf7 31#include "internal.h"
933e90a6 32#include "avio_internal.h"
9d9f4119 33#include "riff.h"
542993b0 34#include "asf.h"
72be7db4 35#include "asfcrypt.h"
5f1ac36d 36#include "avlanguage.h"
de6d9b64 37
4bc328a2 38typedef struct {
75b7feae 39 const AVClass *class;
4bc328a2
AK
40 int asfid2avid[128]; ///< conversion table from asf ID 2 AVStream ID
41 ASFStream streams[128]; ///< it's max number and it's not that big
42 uint32_t stream_bitrates[128]; ///< max number of streams, bitrate for each (for streaming)
d42b0972 43 AVRational dar[128];
4bc328a2
AK
44 char stream_languages[128][6]; ///< max number of streams, language for each (RFC1766, e.g. en-US)
45 /* non streamed additonnal info */
4bc328a2
AK
46 /* packet filling */
47 int packet_size_left;
4bc328a2
AK
48 /* only for reading */
49 uint64_t data_offset; ///< beginning of the first data packet
50 uint64_t data_object_offset; ///< data object offset (excl. GUID & size)
51 uint64_t data_object_size; ///< size of the data object
52 int index_read;
53
54 ASFMainHeader hdr;
55
56 int packet_flags;
57 int packet_property;
58 int packet_timestamp;
59 int packet_segsizetype;
60 int packet_segments;
61 int packet_seq;
62 int packet_replic_size;
63 int packet_key_frame;
64 int packet_padsize;
65 unsigned int packet_frag_offset;
66 unsigned int packet_frag_size;
67 int64_t packet_frag_timestamp;
68 int packet_multi_size;
69 int packet_obj_size;
70 int packet_time_delta;
71 int packet_time_start;
72 int64_t packet_pos;
73
74 int stream_index;
75
76 ASFStream* asf_st; ///< currently decoded stream
75b7feae
MS
77
78 int no_resync_search;
4bc328a2
AK
79} ASFContext;
80
75b7feae
MS
81static const AVOption options[] = {
82 {"no_resync_search", "Don't try to resynchronize by looking for a certain optional start code", offsetof(ASFContext, no_resync_search), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
83 { NULL },
84};
85
86static const AVClass asf_class = {
87 .class_name = "asf demuxer",
88 .item_name = av_default_item_name,
89 .option = options,
90 .version = LIBAVUTIL_VERSION_INT,
91};
92
580fb5e7
MN
93#undef NDEBUG
94#include <assert.h>
95
30cdfb49 96#define ASF_MAX_STREAMS 127
615b92fd 97#define FRAME_HEADER_SIZE 17
115329f1 98// Fix Me! FRAME_HEADER_SIZE may be different.
615b92fd 99
b8b00d94 100static const ff_asf_guid index_guid = {
24c14d6d 101 0x90, 0x08, 0x00, 0x33, 0xb1, 0xe5, 0xcf, 0x11, 0x89, 0xf4, 0x00, 0xa0, 0xc9, 0x03, 0x49, 0xcb
de6d9b64
FB
102};
103
91c9aa09 104#ifdef DEBUG
b8b00d94 105static const ff_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
0e3b6a6f
RM
106 0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
107};
de6d9b64 108
5082f759 109#define PRINT_IF_GUID(g,cmp) \
a750050f 110if (!ff_guidcmp(g, &cmp)) \
dfd2a005 111 av_dlog(NULL, "(GUID: %s) ", #cmp)
5082f759 112
b8b00d94 113static void print_guid(const ff_asf_guid *g)
de6d9b64
FB
114{
115 int i;
17af0525
AJ
116 PRINT_IF_GUID(g, ff_asf_header);
117 else PRINT_IF_GUID(g, ff_asf_file_header);
118 else PRINT_IF_GUID(g, ff_asf_stream_header);
119 else PRINT_IF_GUID(g, ff_asf_audio_stream);
120 else PRINT_IF_GUID(g, ff_asf_audio_conceal_none);
121 else PRINT_IF_GUID(g, ff_asf_video_stream);
122 else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
123 else PRINT_IF_GUID(g, ff_asf_command_stream);
124 else PRINT_IF_GUID(g, ff_asf_comment_header);
125 else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
126 else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
127 else PRINT_IF_GUID(g, ff_asf_data_header);
5082f759 128 else PRINT_IF_GUID(g, index_guid);
17af0525
AJ
129 else PRINT_IF_GUID(g, ff_asf_head1_guid);
130 else PRINT_IF_GUID(g, ff_asf_head2_guid);
131 else PRINT_IF_GUID(g, ff_asf_my_guid);
132 else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
133 else PRINT_IF_GUID(g, ff_asf_extended_content_header);
134 else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
135 else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
136 else PRINT_IF_GUID(g, ff_asf_metadata_header);
88b51ea9 137 else PRINT_IF_GUID(g, ff_asf_marker_header);
0e3b6a6f 138 else PRINT_IF_GUID(g, stream_bitrate_guid);
5f1ac36d 139 else PRINT_IF_GUID(g, ff_asf_language_guid);
5082f759 140 else
dfd2a005 141 av_dlog(NULL, "(GUID: unknown) ");
24c14d6d 142 for(i=0;i<16;i++)
dfd2a005
LB
143 av_dlog(NULL, " 0x%02x,", (*g)[i]);
144 av_dlog(NULL, "}\n");
de6d9b64 145}
6df84c3a 146#undef PRINT_IF_GUID
f94981ea
MR
147#else
148#define print_guid(g)
de6d9b64
FB
149#endif
150
ae628ec1 151void ff_get_guid(AVIOContext *s, ff_asf_guid *g)
de6d9b64 152{
24c14d6d 153 assert(sizeof(*g) == 16);
b7effd4e 154 avio_read(s, *g, sizeof(*g));
de6d9b64
FB
155}
156
c9a65ca8
FB
157static int asf_probe(AVProbeData *pd)
158{
c9a65ca8 159 /* check file header */
a750050f 160 if (!ff_guidcmp(pd->buf, &ff_asf_header))
c9a65ca8
FB
161 return AVPROBE_SCORE_MAX;
162 else
163 return 0;
164}
165
ae628ec1 166static int get_value(AVIOContext *pb, int type){
30e88789 167 switch(type){
b7effd4e
AK
168 case 2: return avio_rl32(pb);
169 case 3: return avio_rl32(pb);
170 case 4: return avio_rl64(pb);
171 case 5: return avio_rl16(pb);
30e88789
MN
172 default:return INT_MIN;
173 }
174}
175
20e2a7ae
AJ
176static void get_tag(AVFormatContext *s, const char *key, int type, int len)
177{
12ad6671 178 char *value;
a2704c97 179 int64_t off = avio_tell(s->pb);
12ad6671 180
fce44489 181 if ((unsigned)len >= (UINT_MAX - 1)/2)
12ad6671
MN
182 return;
183
fce44489 184 value = av_malloc(2*len+1);
12ad6671 185 if (!value)
04295054 186 goto finish;
12ad6671 187
6538c0f1 188 if (type == 0) { // UTF16-LE
93b78d12 189 avio_get_str16le(s->pb, len, value, 2*len + 1);
6538c0f1 190 } else if (type > 1 && type <= 5) { // boolean or DWORD or QWORD or WORD
20e2a7ae 191 uint64_t num = get_value(s->pb, type);
12ad6671 192 snprintf(value, len, "%"PRIu64, num);
20e2a7ae 193 } else {
27e74927 194 av_log(s, AV_LOG_DEBUG, "Unsupported value type %d in tag %s.\n", type, key);
04295054 195 goto finish;
20e2a7ae 196 }
5cbd3b5d 197 if (*value)
d2d67e42 198 av_dict_set(&s->metadata, key, value, 0);
04295054 199finish:
fce44489 200 av_freep(&value);
6b4aa5da 201 avio_seek(s->pb, off + len, SEEK_SET);
20e2a7ae
AJ
202}
203
c1fea230
AK
204static int asf_read_file_properties(AVFormatContext *s, int64_t size)
205{
206 ASFContext *asf = s->priv_data;
ae628ec1 207 AVIOContext *pb = s->pb;
c1fea230
AK
208
209 ff_get_guid(pb, &asf->hdr.guid);
b7effd4e
AK
210 asf->hdr.file_size = avio_rl64(pb);
211 asf->hdr.create_time = avio_rl64(pb);
212 avio_rl64(pb); /* number of packets */
213 asf->hdr.play_time = avio_rl64(pb);
214 asf->hdr.send_time = avio_rl64(pb);
215 asf->hdr.preroll = avio_rl32(pb);
216 asf->hdr.ignore = avio_rl32(pb);
217 asf->hdr.flags = avio_rl32(pb);
218 asf->hdr.min_pktsize = avio_rl32(pb);
219 asf->hdr.max_pktsize = avio_rl32(pb);
6e57a02b
RB
220 if (asf->hdr.min_pktsize >= (1U<<29))
221 return AVERROR_INVALIDDATA;
b7effd4e 222 asf->hdr.max_bitrate = avio_rl32(pb);
c1fea230
AK
223 s->packet_size = asf->hdr.max_pktsize;
224
225 return 0;
226}
227
8bf6db1b
AK
228static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
229{
230 ASFContext *asf = s->priv_data;
ae628ec1 231 AVIOContext *pb = s->pb;
8bf6db1b
AK
232 AVStream *st;
233 ASFStream *asf_st;
234 ff_asf_guid g;
235 enum AVMediaType type;
236 int type_specific_size, sizeX;
8bf6db1b
AK
237 unsigned int tag1;
238 int64_t pos1, pos2, start_time;
239 int test_for_ext_stream_audio, is_dvr_ms_audio=0;
240
241 if (s->nb_streams == ASF_MAX_STREAMS) {
242 av_log(s, AV_LOG_ERROR, "too many streams\n");
243 return AVERROR(EINVAL);
244 }
245
a2704c97 246 pos1 = avio_tell(pb);
8bf6db1b 247
3b3bbdd3 248 st = avformat_new_stream(s, NULL);
8bf6db1b
AK
249 if (!st)
250 return AVERROR(ENOMEM);
c3f9ebf7 251 avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
8bf6db1b
AK
252 asf_st = av_mallocz(sizeof(ASFStream));
253 if (!asf_st)
254 return AVERROR(ENOMEM);
255 st->priv_data = asf_st;
de11ee90 256 st->start_time = 0;
8bf6db1b
AK
257 start_time = asf->hdr.preroll;
258
259 asf_st->stream_language_index = 128; // invalid stream index means no language info
260
261 if(!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
262 st->duration = asf->hdr.play_time /
263 (10000000 / 1000) - start_time;
264 }
265 ff_get_guid(pb, &g);
266
267 test_for_ext_stream_audio = 0;
268 if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
269 type = AVMEDIA_TYPE_AUDIO;
270 } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
271 type = AVMEDIA_TYPE_VIDEO;
272 } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
273 type = AVMEDIA_TYPE_VIDEO;
274 st->codec->codec_id = CODEC_ID_MJPEG;
275 } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
276 type = AVMEDIA_TYPE_DATA;
277 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_embed_stream_header)) {
278 test_for_ext_stream_audio = 1;
279 type = AVMEDIA_TYPE_UNKNOWN;
280 } else {
281 return -1;
282 }
283 ff_get_guid(pb, &g);
e65ab9d9 284 avio_skip(pb, 8); /* total_size */
b7effd4e
AK
285 type_specific_size = avio_rl32(pb);
286 avio_rl32(pb);
287 st->id = avio_rl16(pb) & 0x7f; /* stream id */
8bf6db1b
AK
288 // mapping of asf ID to AV stream ID;
289 asf->asfid2avid[st->id] = s->nb_streams - 1;
290
b7effd4e 291 avio_rl32(pb);
8bf6db1b
AK
292
293 if (test_for_ext_stream_audio) {
294 ff_get_guid(pb, &g);
295 if (!ff_guidcmp(&g, &ff_asf_ext_stream_audio_stream)) {
296 type = AVMEDIA_TYPE_AUDIO;
297 is_dvr_ms_audio=1;
298 ff_get_guid(pb, &g);
b7effd4e
AK
299 avio_rl32(pb);
300 avio_rl32(pb);
301 avio_rl32(pb);
8bf6db1b 302 ff_get_guid(pb, &g);
b7effd4e 303 avio_rl32(pb);
8bf6db1b
AK
304 }
305 }
306
307 st->codec->codec_type = type;
308 if (type == AVMEDIA_TYPE_AUDIO) {
ca402f32
MH
309 int ret = ff_get_wav_header(pb, st->codec, type_specific_size);
310 if (ret < 0)
311 return ret;
8bf6db1b
AK
312 if (is_dvr_ms_audio) {
313 // codec_id and codec_tag are unreliable in dvr_ms
314 // files. Set them later by probing stream.
315 st->codec->codec_id = CODEC_ID_PROBE;
316 st->codec->codec_tag = 0;
317 }
318 if (st->codec->codec_id == CODEC_ID_AAC) {
319 st->need_parsing = AVSTREAM_PARSE_NONE;
320 } else {
321 st->need_parsing = AVSTREAM_PARSE_FULL;
322 }
323 /* We have to init the frame size at some point .... */
a2704c97 324 pos2 = avio_tell(pb);
8bf6db1b 325 if (size >= (pos2 + 8 - pos1 + 24)) {
b7effd4e
AK
326 asf_st->ds_span = avio_r8(pb);
327 asf_st->ds_packet_size = avio_rl16(pb);
328 asf_st->ds_chunk_size = avio_rl16(pb);
329 avio_rl16(pb); //ds_data_size
330 avio_r8(pb); //ds_silence_data
8bf6db1b
AK
331 }
332 //printf("Descrambling: ps:%d cs:%d ds:%d s:%d sd:%d\n",
333 // asf_st->ds_packet_size, asf_st->ds_chunk_size,
334 // asf_st->ds_data_size, asf_st->ds_span, asf_st->ds_silence_data);
335 if (asf_st->ds_span > 1) {
336 if (!asf_st->ds_chunk_size
337 || (asf_st->ds_packet_size/asf_st->ds_chunk_size <= 1)
338 || asf_st->ds_packet_size % asf_st->ds_chunk_size)
339 asf_st->ds_span = 0; // disable descrambling
340 }
8bf6db1b 341 } else if (type == AVMEDIA_TYPE_VIDEO &&
a2704c97 342 size - (avio_tell(pb) - pos1 + 24) >= 51) {
b7effd4e
AK
343 avio_rl32(pb);
344 avio_rl32(pb);
345 avio_r8(pb);
346 avio_rl16(pb); /* size */
347 sizeX= avio_rl32(pb); /* size */
348 st->codec->width = avio_rl32(pb);
349 st->codec->height = avio_rl32(pb);
8bf6db1b 350 /* not available for asf */
b7effd4e
AK
351 avio_rl16(pb); /* panes */
352 st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
353 tag1 = avio_rl32(pb);
45a8a02a 354 avio_skip(pb, 20);
8bf6db1b
AK
355 // av_log(s, AV_LOG_DEBUG, "size:%d tsize:%d sizeX:%d\n", size, total_size, sizeX);
356 if (sizeX > 40) {
357 st->codec->extradata_size = sizeX - 40;
358 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
b7effd4e 359 avio_read(pb, st->codec->extradata, st->codec->extradata_size);
8bf6db1b
AK
360 }
361
362 /* Extract palette from extradata if bpp <= 8 */
363 /* This code assumes that extradata contains only palette */
2f5df0b1 364 /* This is true for all paletted codecs implemented in libavcodec */
8bf6db1b 365 if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
8bf6db1b 366#if HAVE_BIGENDIAN
6df5c528 367 int i;
8bf6db1b 368 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
2d8591c2 369 asf_st->palette[i] = av_bswap32(((uint32_t*)st->codec->extradata)[i]);
8bf6db1b 370#else
2d8591c2
KS
371 memcpy(asf_st->palette, st->codec->extradata,
372 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
8bf6db1b 373#endif
2d8591c2 374 asf_st->palette_changed = 1;
8bf6db1b
AK
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 }
a2704c97 390 pos2 = avio_tell(pb);
45a8a02a 391 avio_skip(pb, size - (pos2 - pos1 + 24));
8bf6db1b
AK
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;
e65ab9d9 402 uint32_t leak_rate, stream_num;
c1fea230
AK
403 unsigned int stream_languageid_index;
404
b7effd4e
AK
405 avio_rl64(pb); // starttime
406 avio_rl64(pb); // endtime
407 leak_rate = avio_rl32(pb); // leak-datarate
408 avio_rl32(pb); // bucket-datasize
409 avio_rl32(pb); // init-bucket-fullness
410 avio_rl32(pb); // alt-leak-datarate
411 avio_rl32(pb); // alt-bucket-datasize
412 avio_rl32(pb); // alt-init-bucket-fullness
413 avio_rl32(pb); // max-object-size
414 avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
415 stream_num = avio_rl16(pb); // stream-num
416
417 stream_languageid_index = avio_rl16(pb); // stream-language-id-index
c1fea230
AK
418 if (stream_num < 128)
419 asf->streams[stream_num].stream_language_index = stream_languageid_index;
420
b7effd4e
AK
421 avio_rl64(pb); // avg frametime in 100ns units
422 stream_ct = avio_rl16(pb); //stream-name-count
423 payload_ext_ct = avio_rl16(pb); //payload-extension-system-count
c1fea230
AK
424
425 if (stream_num < 128)
426 asf->stream_bitrates[stream_num] = leak_rate;
427
428 for (i=0; i<stream_ct; i++){
b7effd4e
AK
429 avio_rl16(pb);
430 ext_len = avio_rl16(pb);
45a8a02a 431 avio_skip(pb, ext_len);
c1fea230
AK
432 }
433
434 for (i=0; i<payload_ext_ct; i++){
435 ff_get_guid(pb, &g);
e65ab9d9 436 avio_skip(pb, 2);
b7effd4e 437 ext_len=avio_rl32(pb);
45a8a02a 438 avio_skip(pb, ext_len);
c1fea230
AK
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
b7effd4e
AK
449 len1 = avio_rl16(pb);
450 len2 = avio_rl16(pb);
451 len3 = avio_rl16(pb);
452 len4 = avio_rl16(pb);
453 len5 = avio_rl16(pb);
c1fea230
AK
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);
45a8a02a 458 avio_skip(pb, len5);
c1fea230
AK
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
b7effd4e 469 desc_count = avio_rl16(pb);
c1fea230
AK
470 for(i=0;i<desc_count;i++) {
471 int name_len,value_type,value_len;
472 char name[1024];
473
b7effd4e 474 name_len = avio_rl16(pb);
c1fea230
AK
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)
45a8a02a 478 avio_skip(pb, name_len - ret);
b7effd4e
AK
479 value_type = avio_rl16(pb);
480 value_len = avio_rl16(pb);
c1fea230
AK
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;
b7effd4e 503 int stream_count = avio_rl16(pb);
c1fea230
AK
504 for(j = 0; j < stream_count; j++) {
505 char lang[6];
b7effd4e 506 unsigned int lang_len = avio_r8(pb);
c1fea230 507 if ((ret = avio_get_str16le(pb, lang_len, lang, sizeof(lang))) < lang_len)
45a8a02a 508 avio_skip(pb, lang_len - ret);
c1fea230
AK
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 519 ASFContext *asf = s->priv_data;
e65ab9d9 520 int n, stream_num, name_len, value_len, value_num;
c1fea230 521 int ret, i;
b7effd4e 522 n = avio_rl16(pb);
c1fea230
AK
523
524 for(i=0;i<n;i++) {
525 char name[1024];
526
b7effd4e
AK
527 avio_rl16(pb); //lang_list_index
528 stream_num= avio_rl16(pb);
529 name_len= avio_rl16(pb);
e65ab9d9 530 avio_skip(pb, 2); /* value_type */
b7effd4e 531 value_len= avio_rl32(pb);
c1fea230
AK
532
533 if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
45a8a02a 534 avio_skip(pb, name_len - ret);
c1fea230 535 //av_log(s, AV_LOG_ERROR, "%d %d %d %d %d <%s>\n", i, stream_num, name_len, value_type, value_len, name);
b7effd4e 536 value_num= avio_rl16(pb);//we should use get_value() here but it does not work 2 is le16 here but le32 elsewhere
45a8a02a 537 avio_skip(pb, value_len - 2);
c1fea230
AK
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
b7effd4e
AK
554 avio_rl64(pb); // reserved 16 bytes
555 avio_rl64(pb); // ...
556 count = avio_rl32(pb); // markers count
557 avio_rl16(pb); // reserved 2 bytes
558 name_len = avio_rl16(pb); // name length
c1fea230 559 for(i=0;i<name_len;i++){
b7effd4e 560 avio_r8(pb); // skip the name
c1fea230
AK
561 }
562
563 for(i=0;i<count;i++){
564 int64_t pres_time;
565 int name_len;
566
b7effd4e
AK
567 avio_rl64(pb); // offset, 8 bytes
568 pres_time = avio_rl64(pb); // presentation time
569 avio_rl16(pb); // entry length
570 avio_rl32(pb); // send time
571 avio_rl32(pb); // flags
572 name_len = avio_rl32(pb); // name length
c1fea230 573 if ((ret = avio_get_str16le(pb, name_len * 2, name, sizeof(name))) < name_len)
45a8a02a 574 avio_skip(pb, name_len - ret);
1fa395e4 575 avpriv_new_chapter(s, i, (AVRational){1, 10000000}, pres_time, AV_NOPTS_VALUE, name );
c1fea230
AK
576 }
577
578 return 0;
579}
580
6e9651d1 581static int asf_read_header(AVFormatContext *s)
de6d9b64 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;
b7effd4e
AK
592 avio_rl64(pb);
593 avio_rl32(pb);
594 avio_r8(pb);
595 avio_r8(pb);
2141dc37 596 memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
de6d9b64 597 for(;;) {
a2704c97 598 uint64_t gpos= avio_tell(pb);
a750050f 599 ff_get_guid(pb, &g);
b7effd4e 600 gsize = avio_rl64(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)) {
a2704c97 605 asf->data_object_offset = avio_tell(pb);
09d9672a
MN
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)) {
6e57a02b
RB
617 int ret = asf_read_file_properties(s, gsize);
618 if (ret < 0)
619 return ret;
a750050f 620 } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
8bf6db1b 621 asf_read_stream_properties(s, gsize);
a750050f 622 } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
c1fea230 623 asf_read_content_desc(s, gsize);
a750050f 624 } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
c1fea230 625 asf_read_language_list(s, gsize);
a750050f 626 } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
c1fea230 627 asf_read_ext_content_desc(s, gsize);
a750050f 628 } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
c1fea230 629 asf_read_metadata(s, gsize);
a750050f 630 } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
c1fea230 631 asf_read_ext_stream_properties(s, gsize);
88141c91
MN
632
633 // there could be a optional stream properties object to follow
634 // if so the next iteration will pick it up
1d4c1c3b 635 continue;
a750050f 636 } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
a750050f 637 ff_get_guid(pb, &g);
e65ab9d9 638 avio_skip(pb, 6);
1d4c1c3b 639 continue;
a750050f 640 } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
c1fea230 641 asf_read_marker(s, gsize);
66e5b1df 642 } else if (pb->eof_reached) {
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 }
a2704c97
AK
655 if(avio_tell(pb) != gpos + gsize)
656 av_log(s, AV_LOG_DEBUG, "gpos mismatch our pos=%"PRIu64", end=%"PRIu64"\n", avio_tell(pb)-gpos, gsize);
6b4aa5da 657 avio_seek(pb, gpos + gsize, SEEK_SET);
de6d9b64 658 }
a750050f 659 ff_get_guid(pb, &g);
b7effd4e
AK
660 avio_rl64(pb);
661 avio_r8(pb);
662 avio_r8(pb);
66e5b1df 663 if (pb->eof_reached)
ccafd472 664 return -1;
a2704c97 665 asf->data_offset = avio_tell(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)
d2d67e42 693 av_dict_set(&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 { \
b7effd4e
AK
707 case 3: var = avio_rl32(pb); rsize += 4; break; \
708 case 2: var = avio_rl16(pb); rsize += 2; break; \
709 case 1: var = avio_r8(pb); rsize++; break; \
2a10020b
ZK
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;
75b7feae
MS
728 if (asf->no_resync_search)
729 off = 3;
730 else if (s->packet_size > 0)
a2704c97 731 off= (avio_tell(pb) - s->data_offset) % s->packet_size + 3;
0fd9e6f3
MN
732
733 c=d=e=-1;
734 while(off-- > 0){
735 c=d; d=e;
b7effd4e 736 e= avio_r8(pb);
0fd9e6f3
MN
737 if(c == 0x82 && !d && !e)
738 break;
739 }
115329f1 740
1359c2d4 741 if (c != 0x82) {
2078ca16
RB
742 /**
743 * This code allows handling of -EAGAIN at packet boundaries (i.e.
744 * if the packet sync code above triggers -EAGAIN). This does not
745 * imply complete -EAGAIN handling support at random positions in
746 * the stream.
747 */
3e68b3ba 748 if (pb->error == AVERROR(EAGAIN))
2078ca16 749 return AVERROR(EAGAIN);
66e5b1df 750 if (!pb->eof_reached)
a2704c97 751 av_log(s, AV_LOG_ERROR, "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
2a10020b 752 }
0c0d17f3 753 if ((c & 0x8f) == 0x82) {
0fd9e6f3 754 if (d || e) {
66e5b1df 755 if (!pb->eof_reached)
bb270c08 756 av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
f7835507 757 return -1;
bb270c08 758 }
b7effd4e
AK
759 c= avio_r8(pb);
760 d= avio_r8(pb);
0c0d17f3 761 rsize+=3;
bb6d5411 762 } else if (!pb->eof_reached) {
6b4aa5da 763 avio_seek(pb, -1, SEEK_CUR); //FIXME
de6d9b64 764 }
2a10020b 765
0c0d17f3
MN
766 asf->packet_flags = c;
767 asf->packet_property = d;
2a10020b 768
91d19d47 769 DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
2a10020b
ZK
770 DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
771 DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
772
5b11e081 773 //the following checks prevent overflows and infinite loops
96f0731a 774 if(!packet_length || packet_length >= (1U<<29)){
a2704c97 775 av_log(s, AV_LOG_ERROR, "invalid packet_length %d at:%"PRId64"\n", packet_length, avio_tell(pb));
ceba96cb 776 return -1;
5b11e081 777 }
92216453 778 if(padsize >= packet_length){
a2704c97 779 av_log(s, AV_LOG_ERROR, "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
ceba96cb 780 return -1;
5b11e081
MN
781 }
782
b7effd4e
AK
783 asf->packet_timestamp = avio_rl32(pb);
784 avio_rl16(pb); /* duration */
2141dc37 785 // rsize has at least 11 bytes which have to be present
2a10020b
ZK
786
787 if (asf->packet_flags & 0x01) {
b7effd4e 788 asf->packet_segsizetype = avio_r8(pb); rsize++;
2a10020b
ZK
789 asf->packet_segments = asf->packet_segsizetype & 0x3f;
790 } else {
bb270c08 791 asf->packet_segments = 1;
2a10020b
ZK
792 asf->packet_segsizetype = 0x80;
793 }
41afac7f
RB
794 if (rsize > packet_length - padsize) {
795 asf->packet_size_left = 0;
796 av_log(s, AV_LOG_ERROR,
797 "invalid packet header length %d for pktlen %d-%d at %"PRId64"\n",
798 rsize, packet_length, padsize, avio_tell(pb));
799 return -1;
800 }
2a10020b 801 asf->packet_size_left = packet_length - padsize - rsize;
acbe6cfa
ZK
802 if (packet_length < asf->hdr.min_pktsize)
803 padsize += asf->hdr.min_pktsize - packet_length;
2a10020b 804 asf->packet_padsize = padsize;
dfd2a005 805 av_dlog(s, "packet: size=%d padsize=%d left=%d\n", s->packet_size, asf->packet_padsize, asf->packet_size_left);
de6d9b64
FB
806 return 0;
807}
808
ae60a857
MN
809/**
810 *
811 * @return <0 if error
812 */
ae628ec1 813static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb){
ae60a857 814 ASFContext *asf = s->priv_data;
ae60a857 815 int rsize = 1;
b7effd4e 816 int num = avio_r8(pb);
e65ab9d9 817 int64_t ts0;
ae60a857
MN
818
819 asf->packet_segments--;
820 asf->packet_key_frame = num >> 7;
821 asf->stream_index = asf->asfid2avid[num & 0x7f];
822 // sequence should be ignored!
823 DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
824 DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
825 DO_2BITS(asf->packet_property, asf->packet_replic_size, 0);
826//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);
827 if (asf->packet_replic_size >= 8) {
b7effd4e 828 asf->packet_obj_size = avio_rl32(pb);
ae60a857
MN
829 if(asf->packet_obj_size >= (1<<24) || asf->packet_obj_size <= 0){
830 av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
831 return -1;
832 }
b7effd4e 833 asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
559a55ad
MN
834 if(asf->packet_replic_size >= 8+38+4){
835// for(i=0; i<asf->packet_replic_size-8; i++)
b7effd4e 836// av_log(s, AV_LOG_DEBUG, "%02X ",avio_r8(pb));
559a55ad 837// av_log(s, AV_LOG_DEBUG, "\n");
45a8a02a 838 avio_skip(pb, 10);
b7effd4e 839 ts0= avio_rl64(pb);
e65ab9d9 840 avio_skip(pb, 8);;
45a8a02a 841 avio_skip(pb, 12);
b7effd4e 842 avio_rl32(pb);
45a8a02a 843 avio_skip(pb, asf->packet_replic_size - 8 - 38 - 4);
559a55ad
MN
844 if(ts0!= -1) asf->packet_frag_timestamp= ts0/10000;
845 else asf->packet_frag_timestamp= AV_NOPTS_VALUE;
846 }else
45a8a02a 847 avio_skip(pb, asf->packet_replic_size - 8);
ae60a857
MN
848 rsize += asf->packet_replic_size; // FIXME - check validity
849 } else if (asf->packet_replic_size==1){
efc90ebf 850 // multipacket - frag_offset is beginning timestamp
ae60a857
MN
851 asf->packet_time_start = asf->packet_frag_offset;
852 asf->packet_frag_offset = 0;
853 asf->packet_frag_timestamp = asf->packet_timestamp;
854
b7effd4e 855 asf->packet_time_delta = avio_r8(pb);
ae60a857
MN
856 rsize++;
857 }else if(asf->packet_replic_size!=0){
858 av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n", asf->packet_replic_size);
859 return -1;
860 }
861 if (asf->packet_flags & 0x01) {
862 DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
29fa570d
UU
863 if (rsize > asf->packet_size_left) {
864 av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
865 return -1;
866 } else if(asf->packet_frag_size > asf->packet_size_left - rsize){
f8b12459
RB
867 if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
868 av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n", asf->packet_size_left, rsize);
869 return -1;
870 } else {
871 int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
872 asf->packet_size_left += diff;
873 asf->packet_padsize -= diff;
874 }
13f7e4b9 875 }
ae60a857
MN
876 //printf("Fragsize %d\n", asf->packet_frag_size);
877 } else {
0bd433a9
UU
878 if (rsize > asf->packet_size_left) {
879 av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
880 return -1;
881 }
ae60a857
MN
882 asf->packet_frag_size = asf->packet_size_left - rsize;
883 //printf("Using rest %d %d %d\n", asf->packet_frag_size, asf->packet_size_left, rsize);
884 }
885 if (asf->packet_replic_size == 1) {
886 asf->packet_multi_size = asf->packet_frag_size;
887 if (asf->packet_multi_size > asf->packet_size_left)
888 return -1;
889 }
890 asf->packet_size_left -= rsize;
891 //printf("___objsize____ %d %d rs:%d\n", asf->packet_obj_size, asf->packet_frag_offset, rsize);
892
893 return 0;
894}
895
277a28e8
RB
896/**
897 * Parse data from individual ASF packets (which were previously loaded
898 * with asf_get_packet()).
899 * @param s demux context
900 * @param pb context to read data from
901 * @param pkt pointer to store packet data into
32e543f8 902 * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
277a28e8
RB
903 * packets need to be loaded (through asf_get_packet())
904 */
ae628ec1 905static int ff_asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
de6d9b64
FB
906{
907 ASFContext *asf = s->priv_data;
2a10020b 908 ASFStream *asf_st = 0;
2a10020b 909 for (;;) {
4172951b 910 int ret;
66e5b1df 911 if(pb->eof_reached)
799a0722 912 return AVERROR_EOF;
bb270c08
DB
913 if (asf->packet_size_left < FRAME_HEADER_SIZE
914 || asf->packet_segments < 1) {
915 //asf->packet_size_left <= asf->packet_padsize) {
916 int ret = asf->packet_size_left + asf->packet_padsize;
a2704c97 917 //printf("PacketLeftSize:%d Pad:%d Pos:%"PRId64"\n", asf->packet_size_left, asf->packet_padsize, avio_tell(pb));
5b11e081 918 assert(ret>=0);
bb270c08 919 /* fail safe */
45a8a02a 920 avio_skip(pb, ret);
b04d5d31 921
a2704c97 922 asf->packet_pos= avio_tell(pb);
b6eaae39
KED
923 if (asf->data_object_size != (uint64_t)-1 &&
924 (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
1d0036b0 925 return AVERROR_EOF; /* Do not exceed the size of the data object */
f27544fd 926 return 1;
bb270c08
DB
927 }
928 if (asf->packet_time_start == 0) {
b0d75d60 929 if(asf_read_frame_header(s, pb) < 0){
ae60a857 930 asf->packet_segments= 0;
236116cb 931 continue;
1305a9d5 932 }
bb270c08 933 if (asf->stream_index < 0
f3356e9c
MN
934 || s->streams[asf->stream_index]->discard >= AVDISCARD_ALL
935 || (!asf->packet_key_frame && s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)
936 ) {
1359c2d4 937 asf->packet_time_start = 0;
bb270c08 938 /* unhandled packet (should not happen) */
45a8a02a 939 avio_skip(pb, asf->packet_frag_size);
bb270c08 940 asf->packet_size_left -= asf->packet_frag_size;
b9866ebc 941 if(asf->stream_index < 0)
ae60a857 942 av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n", asf->packet_frag_size);
1359c2d4 943 continue;
bb270c08
DB
944 }
945 asf->asf_st = s->streams[asf->stream_index]->priv_data;
946 }
947 asf_st = asf->asf_st;
948
bb270c08 949 if (asf->packet_replic_size == 1) {
efc90ebf 950 // frag_offset is here used as the beginning timestamp
bb270c08
DB
951 asf->packet_frag_timestamp = asf->packet_time_start;
952 asf->packet_time_start += asf->packet_time_delta;
b7effd4e 953 asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
bb270c08 954 asf->packet_size_left--;
2141dc37 955 asf->packet_multi_size--;
bb270c08
DB
956 if (asf->packet_multi_size < asf->packet_obj_size)
957 {
958 asf->packet_time_start = 0;
45a8a02a 959 avio_skip(pb, asf->packet_multi_size);
bb270c08 960 asf->packet_size_left -= asf->packet_multi_size;
2141dc37 961 continue;
bb270c08
DB
962 }
963 asf->packet_multi_size -= asf->packet_obj_size;
964 //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);
965 }
60ff1c44
MN
966 if( /*asf->packet_frag_size == asf->packet_obj_size*/
967 asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size
968 && asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size){
969 av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
970 asf_st->frag_offset, asf->packet_frag_size,
971 asf->packet_obj_size, asf_st->pkt.size);
972 asf->packet_obj_size= asf_st->pkt.size;
973 }
974
f8f0a0bd
MN
975 if ( asf_st->pkt.size != asf->packet_obj_size
976 || asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) { //FIXME is this condition sufficient?
200026aa
MN
977 if(asf_st->pkt.data){
978 av_log(s, AV_LOG_INFO, "freeing incomplete packet size %d, new %d\n", asf_st->pkt.size, asf->packet_obj_size);
979 asf_st->frag_offset = 0;
980 av_free_packet(&asf_st->pkt);
981 }
bb270c08
DB
982 /* new packet */
983 av_new_packet(&asf_st->pkt, asf->packet_obj_size);
984 asf_st->seq = asf->packet_seq;
de11ee90 985 asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
bb270c08 986 asf_st->pkt.stream_index = asf->stream_index;
115329f1
DB
987 asf_st->pkt.pos =
988 asf_st->packet_pos= asf->packet_pos;
2d8591c2
KS
989 if (asf_st->pkt.data && asf_st->palette_changed) {
990 uint8_t *pal;
e9dc9201 991 pal = av_packet_new_side_data(&asf_st->pkt, AV_PKT_DATA_PALETTE,
2d8591c2
KS
992 AVPALETTE_SIZE);
993 if (!pal) {
994 av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
995 } else {
996 memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
997 asf_st->palette_changed = 0;
998 }
999 }
115329f1 1000//printf("new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
cc947f04 1001//asf->stream_index, asf->packet_key_frame, asf_st->pkt.flags & AV_PKT_FLAG_KEY,
72415b2a
SS
1002//s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO, asf->packet_obj_size);
1003 if (s->streams[asf->stream_index]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
bb270c08
DB
1004 asf->packet_key_frame = 1;
1005 if (asf->packet_key_frame)
cc947f04 1006 asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
bb270c08
DB
1007 }
1008
1009 /* read data */
1010 //printf("READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
91d19d47 1011 // s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
bb270c08
DB
1012 // asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
1013 asf->packet_size_left -= asf->packet_frag_size;
1014 if (asf->packet_size_left < 0)
2141dc37 1015 continue;
4c71d727
MN
1016
1017 if( asf->packet_frag_offset >= asf_st->pkt.size
1018 || asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset){
1019 av_log(s, AV_LOG_ERROR, "packet fragment position invalid %u,%u not in %u\n",
1020 asf->packet_frag_offset, asf->packet_frag_size, asf_st->pkt.size);
1021 continue;
1022 }
1023
b7effd4e 1024 ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
4172951b 1025 asf->packet_frag_size);
bf09a019
RD
1026 if (ret != asf->packet_frag_size) {
1027 if (ret < 0 || asf->packet_frag_offset + ret == 0)
1028 return ret < 0 ? ret : AVERROR_EOF;
1029 if (asf_st->ds_span > 1) {
1030 // scrambling, we can either drop it completely or fill the remainder
1031 // TODO: should we fill the whole packet instead of just the current
1032 // fragment?
1033 memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
1034 asf->packet_frag_size - ret);
1035 ret = asf->packet_frag_size;
1036 } else
1037 // no scrambling, so we can return partial packets
1038 av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
1039 }
72be7db4
RD
1040 if (s->key && s->keylen == 20)
1041 ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
bf09a019
RD
1042 ret);
1043 asf_st->frag_offset += ret;
bb270c08
DB
1044 /* test if whole packet is read */
1045 if (asf_st->frag_offset == asf_st->pkt.size) {
da0b94f5
MN
1046 //workaround for macroshit radio DVR-MS files
1047 if( s->streams[asf->stream_index]->codec->codec_id == CODEC_ID_MPEG2VIDEO
1048 && asf_st->pkt.size > 100){
1049 int i;
1050 for(i=0; i<asf_st->pkt.size && !asf_st->pkt.data[i]; i++);
1051 if(i == asf_st->pkt.size){
1052 av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1053 asf_st->frag_offset = 0;
1054 av_free_packet(&asf_st->pkt);
1055 continue;
1056 }
1057 }
1058
bb270c08
DB
1059 /* return packet */
1060 if (asf_st->ds_span > 1) {
05219463 1061 if(asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span){
b43387d4 1062 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 1063 }else{
bb270c08 1064 /* packet descrambling */
3c3ef81b 1065 uint8_t *newdata = av_malloc(asf_st->pkt.size + FF_INPUT_BUFFER_PADDING_SIZE);
bb270c08
DB
1066 if (newdata) {
1067 int offset = 0;
3c3ef81b 1068 memset(newdata + asf_st->pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
bb270c08
DB
1069 while (offset < asf_st->pkt.size) {
1070 int off = offset / asf_st->ds_chunk_size;
1071 int row = off / asf_st->ds_span;
1072 int col = off % asf_st->ds_span;
1073 int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1074 //printf("off:%d row:%d col:%d idx:%d\n", off, row, col, idx);
05219463
MN
1075
1076 assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
1077 assert(idx+1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
bb270c08
DB
1078 memcpy(newdata + offset,
1079 asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1080 asf_st->ds_chunk_size);
1081 offset += asf_st->ds_chunk_size;
1082 }
1083 av_free(asf_st->pkt.data);
1084 asf_st->pkt.data = newdata;
1085 }
05219463 1086 }
bb270c08
DB
1087 }
1088 asf_st->frag_offset = 0;
b83e83b9 1089 *pkt= asf_st->pkt;
bb270c08
DB
1090 //printf("packet %d %d\n", asf_st->pkt.size, asf->packet_frag_size);
1091 asf_st->pkt.size = 0;
1092 asf_st->pkt.data = 0;
e73c6aaa
RB
1093 asf_st->pkt.side_data_elems = 0;
1094 asf_st->pkt.side_data = NULL;
bb270c08
DB
1095 break; // packet completed
1096 }
de6d9b64 1097 }
de6d9b64
FB
1098 return 0;
1099}
1100
f27544fd
RB
1101static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1102{
1103 ASFContext *asf = s->priv_data;
1104
1105 for (;;) {
1106 int ret;
1107
1108 /* parse cached packets, if any */
1d217cdb 1109 if ((ret = ff_asf_parse_packet(s, s->pb, pkt)) <= 0)
f27544fd 1110 return ret;
1d217cdb 1111 if ((ret = ff_asf_get_packet(s, s->pb)) < 0)
f27544fd
RB
1112 assert(asf->packet_size_left < FRAME_HEADER_SIZE || asf->packet_segments < 1);
1113 asf->packet_time_start = 0;
1114 }
f27544fd
RB
1115}
1116
38376f00
KK
1117// Added to support seeking after packets have been read
1118// If information is not reset, read_packet fails due to
1119// leftover information from previous reads
1120static void asf_reset_header(AVFormatContext *s)
1121{
1122 ASFContext *asf = s->priv_data;
82b9e4a2
MN
1123 ASFStream *asf_st;
1124 int i;
38376f00 1125
38376f00
KK
1126 asf->packet_size_left = 0;
1127 asf->packet_segments = 0;
1128 asf->packet_flags = 0;
1129 asf->packet_property = 0;
1130 asf->packet_timestamp = 0;
1131 asf->packet_segsizetype = 0;
1132 asf->packet_segments = 0;
1133 asf->packet_seq = 0;
1134 asf->packet_replic_size = 0;
1135 asf->packet_key_frame = 0;
1136 asf->packet_padsize = 0;
1137 asf->packet_frag_offset = 0;
1138 asf->packet_frag_size = 0;
1139 asf->packet_frag_timestamp = 0;
1140 asf->packet_multi_size = 0;
1141 asf->packet_obj_size = 0;
1142 asf->packet_time_delta = 0;
1143 asf->packet_time_start = 0;
115329f1 1144
82b9e4a2
MN
1145 for(i=0; i<s->nb_streams; i++){
1146 asf_st= s->streams[i]->priv_data;
1147 av_free_packet(&asf_st->pkt);
1148 asf_st->frag_offset=0;
1149 asf_st->seq=0;
1150 }
1151 asf->asf_st= NULL;
38376f00
KK
1152}
1153
5d25d2a9
DB
1154static int asf_read_close(AVFormatContext *s)
1155{
5d25d2a9 1156 asf_reset_header(s);
0b5ae1fd 1157
5d25d2a9
DB
1158 return 0;
1159}
1160
8d14a25c 1161static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
38376f00 1162{
38376f00 1163 AVPacket pkt1, *pkt = &pkt1;
82b9e4a2 1164 ASFStream *asf_st;
580fb5e7 1165 int64_t pts;
82b9e4a2 1166 int64_t pos= *ppos;
3e9245a9 1167 int i;
30cdfb49 1168 int64_t start_pos[ASF_MAX_STREAMS];
115329f1 1169
3e9245a9
MN
1170 for(i=0; i<s->nb_streams; i++){
1171 start_pos[i]= pos;
1172 }
115329f1 1173
68a10b40 1174 if (s->packet_size > 0)
b098b309 1175 pos= (pos+s->packet_size-1-s->data_offset)/s->packet_size*s->packet_size+ s->data_offset;
8d14a25c 1176 *ppos= pos;
6b4aa5da 1177 avio_seek(s->pb, pos, SEEK_SET);
115329f1 1178
82b9e4a2 1179//printf("asf_read_pts\n");
82b9e4a2 1180 asf_reset_header(s);
3e9245a9 1181 for(;;){
73f027c1 1182 if (asf_read_packet(s, pkt) < 0){
0b93103a 1183 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
bb270c08 1184 return AV_NOPTS_VALUE;
82b9e4a2 1185 }
115329f1 1186
b88eb876 1187 pts = pkt->dts;
580fb5e7
MN
1188
1189 av_free_packet(pkt);
cc947f04 1190 if(pkt->flags&AV_PKT_FLAG_KEY){
3e9245a9
MN
1191 i= pkt->stream_index;
1192
1193 asf_st= s->streams[i]->priv_data;
1194
91d19d47 1195// assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
8d14a25c 1196 pos= asf_st->packet_pos;
09646bab 1197
52e54612 1198 av_add_index_entry(s->streams[i], pos, pts, pkt->size, pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
8d14a25c 1199 start_pos[i]= asf_st->packet_pos + 1;
115329f1 1200
3e9245a9
MN
1201 if(pkt->stream_index == stream_index)
1202 break;
1203 }
1204 }
1205
1206 *ppos= pos;
949b1a13 1207//printf("found keyframe at %"PRId64" stream %d stamp:%"PRId64"\n", *ppos, stream_index, pts);
580fb5e7
MN
1208
1209 return pts;
38376f00
KK
1210}
1211
56c96a22
MN
1212static void asf_build_simple_index(AVFormatContext *s, int stream_index)
1213{
b8b00d94 1214 ff_asf_guid g;
56c96a22 1215 ASFContext *asf = s->priv_data;
a2704c97 1216 int64_t current_pos= avio_tell(s->pb);
56c96a22 1217 int i;
56c96a22 1218
6b4aa5da 1219 avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET);
a750050f 1220 ff_get_guid(s->pb, &g);
77b7c334
SG
1221
1222 /* the data object can be followed by other top-level objects,
1223 skip them until the simple index object is reached */
a750050f 1224 while (ff_guidcmp(&g, &index_guid)) {
b7effd4e 1225 int64_t gsize= avio_rl64(s->pb);
66e5b1df 1226 if (gsize < 24 || s->pb->eof_reached) {
6b4aa5da 1227 avio_seek(s->pb, current_pos, SEEK_SET);
77b7c334
SG
1228 return;
1229 }
45a8a02a 1230 avio_skip(s->pb, gsize-24);
a750050f 1231 ff_get_guid(s->pb, &g);
77b7c334
SG
1232 }
1233
1234 {
134271c2 1235 int64_t itime, last_pos=-1;
d728e3cd 1236 int pct, ict;
b7effd4e 1237 int64_t av_unused gsize= avio_rl64(s->pb);
a750050f 1238 ff_get_guid(s->pb, &g);
b7effd4e
AK
1239 itime=avio_rl64(s->pb);
1240 pct=avio_rl32(s->pb);
1241 ict=avio_rl32(s->pb);
871e62e5 1242 av_log(s, AV_LOG_DEBUG, "itime:0x%"PRIx64", pct:%d, ict:%d\n",itime,pct,ict);
56c96a22
MN
1243
1244 for (i=0;i<ict;i++){
b7effd4e
AK
1245 int pktnum=avio_rl32(s->pb);
1246 int pktct =avio_rl16(s->pb);
91d19d47 1247 int64_t pos = s->data_offset + s->packet_size*(int64_t)pktnum;
4377fafd 1248 int64_t index_pts= FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
56c96a22 1249
134271c2 1250 if(pos != last_pos){
4377fafd 1251 av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n", pktnum, pktct, index_pts);
91d19d47 1252 av_add_index_entry(s->streams[stream_index], pos, index_pts, s->packet_size, 0, AVINDEX_KEYFRAME);
134271c2
MN
1253 last_pos=pos;
1254 }
56c96a22 1255 }
44257ef4 1256 asf->index_read= ict > 0;
56c96a22 1257 }
6b4aa5da 1258 avio_seek(s->pb, current_pos, SEEK_SET);
56c96a22
MN
1259}
1260
3ba1438d 1261static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
2a10020b 1262{
afda223c 1263 ASFContext *asf = s->priv_data;
56c96a22
MN
1264 AVStream *st = s->streams[stream_index];
1265 int64_t pos;
1266 int index;
115329f1 1267
91d19d47 1268 if (s->packet_size <= 0)
38376f00
KK
1269 return -1;
1270
fa13095a 1271 /* Try using the protocol's read_seek if available */
fd2982a0 1272 if(s->pb) {
ff1ec0c3 1273 int ret = avio_seek_time(s->pb, stream_index, pts, flags);
fa13095a
BA
1274 if(ret >= 0)
1275 asf_reset_header(s);
fd2982a0 1276 if (ret != AVERROR(ENOSYS))
cd31f8dc 1277 return ret;
fa13095a
BA
1278 }
1279
52e54612 1280 if (!asf->index_read)
56c96a22
MN
1281 asf_build_simple_index(s, stream_index);
1282
c7bd5eda 1283 if((asf->index_read && st->index_entries)){
56c96a22 1284 index= av_index_search_timestamp(st, pts, flags);
c7bd5eda 1285 if(index >= 0) {
b58bc17c
VP
1286 /* find the position */
1287 pos = st->index_entries[index].pos;
56c96a22 1288
b58bc17c
VP
1289 /* do the seek */
1290 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1291 avio_seek(s->pb, pos, SEEK_SET);
1292 asf_reset_header(s);
1293 return 0;
c7bd5eda 1294 }
56c96a22 1295 }
c7bd5eda 1296 /* no index or seeking by index failed */
a2faa951 1297 if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
c7bd5eda 1298 return -1;
38376f00
KK
1299 asf_reset_header(s);
1300 return 0;
2a10020b
ZK
1301}
1302
c6610a21 1303AVInputFormat ff_asf_demuxer = {
dfc2c4d9
AK
1304 .name = "asf",
1305 .long_name = NULL_IF_CONFIG_SMALL("ASF format"),
1306 .priv_data_size = sizeof(ASFContext),
1307 .read_probe = asf_probe,
1308 .read_header = asf_read_header,
1309 .read_packet = asf_read_packet,
1310 .read_close = asf_read_close,
1311 .read_seek = asf_read_seek,
1312 .read_timestamp = asf_read_pts,
20234a4b 1313 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
75b7feae 1314 .priv_class = &asf_class,
c9a65ca8 1315};