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