asfdec: Account for different Format Data sizes
[libav.git] / libavformat / asfdec.c
CommitLineData
de6d9b64 1/*
b08569a2
AH
2 * Microsoft Advanced Streaming Format demuxer
3 * Copyright (c) 2014 Alexandra Hájková
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
72eaba5e 22#include "libavutil/attributes.h"
48a4ffa7 23#include "libavutil/avstring.h"
b122c651 24#include "libavutil/bswap.h"
245976da 25#include "libavutil/common.h"
d2d67e42 26#include "libavutil/dict.h"
7950e519 27#include "libavutil/internal.h"
0ebcdf5c 28#include "libavutil/mathematics.h"
75b7feae 29#include "libavutil/opt.h"
b08569a2
AH
30#include "libavutil/time_internal.h"
31
de6d9b64 32#include "avformat.h"
933e90a6 33#include "avio_internal.h"
48a4ffa7 34#include "avlanguage.h"
5e745cef 35#include "id3v2.h"
48a4ffa7 36#include "internal.h"
9d9f4119 37#include "riff.h"
542993b0 38#include "asf.h"
72be7db4 39#include "asfcrypt.h"
de6d9b64 40
b08569a2
AH
41#define ASF_BOOL 0x2
42#define ASF_WORD 0x5
43#define ASF_GUID 0x6
44#define ASF_DWORD 0x3
45#define ASF_QWORD 0x4
46#define ASF_UNICODE 0x0
47#define ASF_FLAG_BROADCAST 0x1
48#define ASF_BYTE_ARRAY 0x1
49#define ASF_TYPE_AUDIO 0x2
50#define ASF_TYPE_VIDEO 0x1
51#define ASF_STREAM_NUM 0x7F
52#define ASF_MAX_STREAMS 128
53#define BMP_HEADER_SIZE 40
54#define ASF_NUM_OF_PAYLOADS 0x3F
55#define ASF_ERROR_CORRECTION_LENGTH_TYPE 0x60
56#define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
57
58typedef struct GUIDParseTable {
59 const char *name;
60 ff_asf_guid guid;
61 int (*read_object)(AVFormatContext *, const struct GUIDParseTable *);
62 int is_subobject;
63} GUIDParseTable;
64
65typedef struct ASFPacket {
66 AVPacket avpkt;
67 int64_t dts;
68 uint32_t frame_num; // ASF payloads with the same number are parts of the same frame
69 int flags;
70 int data_size;
71 int duration;
72 int size_left;
73 uint8_t stream_index;
74} ASFPacket;
75
76typedef struct ASFStream {
77 uint8_t stream_index; // from packet header
78 int index; // stream index in AVFormatContext, set in asf_read_stream_properties
79 int type;
80 int indexed; // added index entries from the Simple Index Object or not
81 int8_t span; // for deinterleaving
82 uint16_t virtual_pkt_len;
83 uint16_t virtual_chunk_len;
84 int16_t lang_idx;
85 ASFPacket pkt;
86} ASFStream;
87
88typedef struct ASFStreamData{
89 char langs[32];
90 AVDictionary *asf_met; // for storing per-stream metadata
91 AVRational aspect_ratio;
92} ASFStreamData;
93
daf8cf35 94typedef struct ASFContext {
b08569a2
AH
95 int data_reached;
96 int is_simple_index; // is simple index present or not 1/0
97 int is_header;
98
99 uint64_t preroll;
100 uint64_t nb_packets; // ASF packets
101 uint32_t packet_size;
102 int64_t send_time;
103 int duration;
104
105 uint32_t b_flags; // flags with broadcast flag
106 uint32_t prop_flags; // file properties object flags
107
108 uint64_t data_size; // data object size
109 uint64_t unknown_size; // size of the unknown object
110
111 int64_t offset; // offset of the current object
112
113 int64_t data_offset;
114 int64_t first_packet_offset; // packet offset
115 int64_t unknown_offset; // for top level header objects or subobjects without specified behavior
116
117 // ASF file must not contain more than 128 streams according to the specification
118 ASFStream *asf_st[ASF_MAX_STREAMS];
119 ASFStreamData asf_sd[ASF_MAX_STREAMS];
120 int nb_streams;
121
122 int stream_index; // from packet header, for the subpayload case
123
41ed7ab4
VG
124 // packet parameters
125 uint64_t sub_header_offset; // offset of subpayload header
b08569a2
AH
126 int64_t sub_dts;
127 uint8_t dts_delta; // for subpayloads
128 uint32_t packet_size_internal; // packet size stored inside ASFPacket, can be 0
b08569a2
AH
129 int64_t packet_offset; // offset of the current packet inside Data Object
130 uint32_t pad_len; // padding after payload
131 uint32_t rep_data_len;
132
133 // packet state
134 uint64_t sub_left; // subpayloads left or not
e61f3984 135 unsigned int nb_sub; // number of subpayloads read so far from the current ASF packet
b08569a2
AH
136 uint16_t mult_sub_len; // total length of subpayloads array inside multiple payload
137 uint64_t nb_mult_left; // multiple payloads left
138 int return_subpayload;
139 enum {
140 PARSE_PACKET_HEADER,
141 READ_SINGLE,
142 READ_MULTI,
143 READ_MULTI_SUB
144 } state;
4bc328a2
AK
145} ASFContext;
146
b08569a2
AH
147static int detect_unknown_subobject(AVFormatContext *s, int64_t offset, int64_t size);
148static const GUIDParseTable *find_guid(ff_asf_guid guid);
75b7feae 149
b08569a2
AH
150static int asf_probe(AVProbeData *pd)
151{
152 /* check file header */
153 if (!ff_guidcmp(pd->buf, &ff_asf_header))
154 return AVPROBE_SCORE_MAX;
155 else
156 return 0;
157}
75b7feae 158
b08569a2
AH
159static void swap_guid(ff_asf_guid guid)
160{
161 FFSWAP(unsigned char, guid[0], guid[3]);
162 FFSWAP(unsigned char, guid[1], guid[2]);
163 FFSWAP(unsigned char, guid[4], guid[5]);
164 FFSWAP(unsigned char, guid[6], guid[7]);
165}
580fb5e7 166
b08569a2
AH
167static void align_position(AVIOContext *pb, int64_t offset, uint64_t size)
168{
169 if (avio_tell(pb) != offset + size)
170 avio_seek(pb, offset + size, SEEK_SET);
171}
615b92fd 172
b08569a2
AH
173static int asf_read_unknown(AVFormatContext *s, const GUIDParseTable *g)
174{
175 ASFContext *asf = s->priv_data;
176 AVIOContext *pb = s->pb;
177 uint64_t size = avio_rl64(pb);
178 int ret;
179
2aec600a
AC
180 if (size > INT64_MAX)
181 return AVERROR_INVALIDDATA;
182
b08569a2
AH
183 if (asf->is_header)
184 asf->unknown_size = size;
185 asf->is_header = 0;
186 if (!g->is_subobject) {
187 if (!(ret = strcmp(g->name, "Header Extension")))
188 avio_skip(pb, 22); // skip reserved fields and Data Size
189 if ((ret = detect_unknown_subobject(s, asf->unknown_offset,
190 asf->unknown_size)) < 0)
191 return ret;
bf50607a
AC
192 } else {
193 if (size < 24) {
194 av_log(s, AV_LOG_ERROR, "Too small size %"PRIu64" (< 24).\n", size);
195 return AVERROR_INVALIDDATA;
196 }
b08569a2 197 avio_skip(pb, size - 24);
bf50607a 198 }
de6d9b64 199
b08569a2
AH
200 return 0;
201}
202
203static int get_asf_string(AVIOContext *pb, int maxlen, char *buf, int buflen)
204{
205 char *q = buf;
206 int ret = 0;
207 if (buflen <= 0)
208 return AVERROR(EINVAL);
209 while (ret + 1 < maxlen) {
210 uint8_t tmp;
211 uint32_t ch;
212 GET_UTF16(ch, (ret += 2) <= maxlen ? avio_rl16(pb) : 0, break;);
213 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)
214 }
215 *q = 0;
de6d9b64 216
b08569a2
AH
217 return ret;
218}
5082f759 219
b08569a2 220static int asf_read_marker(AVFormatContext *s, const GUIDParseTable *g)
de6d9b64 221{
b08569a2
AH
222 ASFContext *asf = s->priv_data;
223 AVIOContext *pb = s->pb;
224 uint64_t size = avio_rl64(pb);
225 int i, nb_markers, ret;
226 size_t len;
227 char name[1024];
228
229 avio_skip(pb, 8);
230 avio_skip(pb, 8); // skip reserved GUID
231 nb_markers = avio_rl32(pb);
232 avio_skip(pb, 2); // skip reserved field
233 len = avio_rl16(pb);
234 for (i = 0; i < len; i++)
235 avio_skip(pb, 1);
236
237 for (i = 0; i < nb_markers; i++) {
238 int64_t pts;
239
240 avio_skip(pb, 8);
241 pts = avio_rl64(pb);
242 pts -= asf->preroll * 10000;
243 avio_skip(pb, 2); // entry length
244 avio_skip(pb, 4); // send time
245 avio_skip(pb, 4); // flags
246 len = avio_rl32(pb);
247
248 if ((ret = avio_get_str16le(pb, len, name,
249 sizeof(name))) < len)
250 avio_skip(pb, len - ret);
251 avpriv_new_chapter(s, i, (AVRational) { 1, 10000000 }, pts,
252 AV_NOPTS_VALUE, name);
253 }
254 align_position(pb, asf->offset, size);
255
256 return 0;
de6d9b64 257}
de6d9b64 258
b08569a2
AH
259static int asf_read_metadata(AVFormatContext *s, const char *title, uint16_t len,
260 unsigned char *ch, uint16_t buflen)
c9a65ca8 261{
b08569a2
AH
262 AVIOContext *pb = s->pb;
263
264 avio_get_str16le(pb, len, ch, buflen);
78491fe8
AH
265 if (ch[0]) {
266 if (av_dict_set(&s->metadata, title, ch, 0) < 0)
267 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
268 }
b08569a2
AH
269
270 return 0;
c9a65ca8
FB
271}
272
06628137 273static int asf_read_value(AVFormatContext *s, const uint8_t *name,
b08569a2 274 uint16_t val_len, int type, AVDictionary **met)
48a4ffa7 275{
b08569a2
AH
276 int ret;
277 uint8_t *value;
278 uint16_t buflen = 2 * val_len + 1;
279 AVIOContext *pb = s->pb;
280
281 value = av_malloc(buflen);
282 if (!value)
283 return AVERROR(ENOMEM);
284 if (type == ASF_UNICODE) {
285 // get_asf_string reads UTF-16 and converts it to UTF-8 which needs longer buffer
286 if ((ret = get_asf_string(pb, val_len, value, buflen)) < 0)
287 goto failed;
288 if (av_dict_set(met, name, value, 0) < 0)
289 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
290 } else {
291 char buf[256];
c571424c
AH
292 if (val_len > sizeof(buf)) {
293 ret = AVERROR_INVALIDDATA;
294 goto failed;
295 }
b08569a2
AH
296 if ((ret = avio_read(pb, value, val_len)) < 0)
297 goto failed;
298 if (ret < 2 * val_len)
299 value[ret] = '\0';
300 else
301 value[2 * val_len - 1] = '\0';
302 snprintf(buf, sizeof(buf), "%s", value);
303 if (av_dict_set(met, name, buf, 0) < 0)
304 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
305 }
306 av_freep(&value);
307
308 return 0;
309
310failed:
311 av_freep(&value);
312 return ret;
313}
944f6086 314static int asf_read_generic_value(AVIOContext *pb, int type, uint64_t *value)
b08569a2 315{
b08569a2 316
48a4ffa7 317 switch (type) {
b08569a2 318 case ASF_BOOL:
944f6086 319 *value = avio_rl16(pb);
b08569a2
AH
320 break;
321 case ASF_DWORD:
944f6086 322 *value = avio_rl32(pb);
b08569a2
AH
323 break;
324 case ASF_QWORD:
944f6086 325 *value = avio_rl64(pb);
b08569a2
AH
326 break;
327 case ASF_WORD:
944f6086 328 *value = avio_rl16(pb);
b08569a2 329 break;
48a4ffa7 330 default:
b08569a2 331 return AVERROR_INVALIDDATA;
30e88789 332 }
944f6086
AH
333
334 return 0;
335}
336
337static int asf_set_metadata(AVFormatContext *s, const uint8_t *name,
06628137 338 int type, AVDictionary **met)
944f6086
AH
339{
340 AVIOContext *pb = s->pb;
341 uint64_t value;
342 char buf[32];
343 int ret;
344
345 ret = asf_read_generic_value(pb, type, &value);
346 if (ret < 0)
347 return ret;
348
b08569a2
AH
349 snprintf(buf, sizeof(buf), "%"PRIu64, value);
350 if (av_dict_set(met, name, buf, 0) < 0)
351 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
352
353 return 0;
30e88789
MN
354}
355
5e745cef
AK
356/* MSDN claims that this should be "compatible with the ID3 frame, APIC",
357 * but in reality this is only loosely similar */
358static int asf_read_picture(AVFormatContext *s, int len)
359{
b08569a2 360 ASFContext *asf = s->priv_data;
48a4ffa7 361 AVPacket pkt = { 0 };
5e745cef 362 const CodecMime *mime = ff_id3v2_mime_tags;
48a4ffa7 363 enum AVCodecID id = AV_CODEC_ID_NONE;
5e745cef
AK
364 char mimetype[64];
365 uint8_t *desc = NULL;
5e745cef
AK
366 AVStream *st = NULL;
367 int ret, type, picsize, desc_len;
b08569a2 368 ASFStream *asf_st;
5e745cef
AK
369
370 /* type + picsize + mime + desc */
371 if (len < 1 + 4 + 2 + 2) {
372 av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
373 return AVERROR_INVALIDDATA;
374 }
375
376 /* picture type */
377 type = avio_r8(s->pb);
378 len--;
379 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
380 av_log(s, AV_LOG_WARNING, "Unknown attached picture type: %d.\n", type);
381 type = 0;
382 }
383
384 /* picture data size */
385 picsize = avio_rl32(s->pb);
48a4ffa7 386 len -= 4;
5e745cef
AK
387
388 /* picture MIME type */
389 len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
36ef5369 390 while (mime->id != AV_CODEC_ID_NONE) {
5e745cef
AK
391 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
392 id = mime->id;
393 break;
394 }
395 mime++;
396 }
36ef5369 397 if (id == AV_CODEC_ID_NONE) {
5e745cef
AK
398 av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
399 mimetype);
400 return 0;
401 }
402
403 if (picsize >= len) {
404 av_log(s, AV_LOG_ERROR, "Invalid attached picture data size: %d >= %d.\n",
405 picsize, len);
406 return AVERROR_INVALIDDATA;
407 }
408
409 /* picture description */
410 desc_len = (len - picsize) * 2 + 1;
411 desc = av_malloc(desc_len);
412 if (!desc)
413 return AVERROR(ENOMEM);
414 len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
415
416 ret = av_get_packet(s->pb, &pkt, picsize);
417 if (ret < 0)
418 goto fail;
419
48a4ffa7 420 st = avformat_new_stream(s, NULL);
1ef0e8a6 421 if (!st) {
5e745cef
AK
422 ret = AVERROR(ENOMEM);
423 goto fail;
424 }
b08569a2
AH
425 asf->asf_st[asf->nb_streams] = av_mallocz(sizeof(*asf_st));
426 asf_st = asf->asf_st[asf->nb_streams];
c571424c
AH
427 if (!asf_st) {
428 ret = AVERROR(ENOMEM);
429 goto fail;
430 }
b08569a2 431
48a4ffa7 432 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
9200514a
AK
433 st->codecpar->codec_type = asf_st->type = AVMEDIA_TYPE_VIDEO;
434 st->codecpar->codec_id = id;
48a4ffa7 435 st->attached_pic = pkt;
b08569a2 436 st->attached_pic.stream_index = asf_st->index = st->index;
48a4ffa7 437 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
5e745cef 438
b08569a2
AH
439 asf->nb_streams++;
440
441 if (*desc) {
442 if (av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL) < 0)
443 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
444 } else
5e745cef
AK
445 av_freep(&desc);
446
b08569a2
AH
447 if (av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0) < 0)
448 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
5e745cef
AK
449
450 return 0;
451
452fail:
5e745cef 453 av_freep(&desc);
ce70f28a 454 av_packet_unref(&pkt);
5e745cef
AK
455 return ret;
456}
457
06628137 458static void get_id3_tag(AVFormatContext *s)
f5fac6f7
VP
459{
460 ID3v2ExtraMeta *id3v2_extra_meta = NULL;
461
462 ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
463 if (id3v2_extra_meta)
464 ff_id3v2_parse_apic(s, &id3v2_extra_meta);
465 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
466}
467
fdbc544d 468static int process_metadata(AVFormatContext *s, const uint8_t *name, uint16_t name_len,
b08569a2 469 uint16_t val_len, uint16_t type, AVDictionary **met)
20e2a7ae 470{
b08569a2
AH
471 int ret;
472 ff_asf_guid guid;
0af3b658 473
b08569a2
AH
474 if (val_len) {
475 switch (type) {
476 case ASF_UNICODE:
06628137 477 asf_read_value(s, name, val_len, type, met);
b08569a2
AH
478 break;
479 case ASF_BYTE_ARRAY:
480 if (!strcmp(name, "WM/Picture")) // handle cover art
481 asf_read_picture(s, val_len);
482 else if (!strcmp(name, "ID3")) // handle ID3 tag
06628137 483 get_id3_tag(s);
b08569a2 484 else
06628137 485 asf_read_value(s, name, val_len, type, met);
b08569a2
AH
486 break;
487 case ASF_GUID:
488 ff_get_guid(s->pb, &guid);
489 break;
490 default:
06628137 491 if ((ret = asf_set_metadata(s, name, type, met)) < 0)
b08569a2
AH
492 return ret;
493 break;
f5fac6f7 494 }
20e2a7ae 495 }
48a4ffa7 496
b08569a2 497 return 0;
20e2a7ae
AJ
498}
499
b08569a2 500static int asf_read_ext_content(AVFormatContext *s, const GUIDParseTable *g)
c1fea230 501{
b08569a2
AH
502 ASFContext *asf = s->priv_data;
503 AVIOContext *pb = s->pb;
504 uint64_t size = avio_rl64(pb);
505 uint16_t nb_desc = avio_rl16(pb);
506 int i, ret;
c1fea230 507
b08569a2
AH
508 for (i = 0; i < nb_desc; i++) {
509 uint16_t name_len, type, val_len;
510 uint8_t *name = NULL;
c1fea230 511
b08569a2
AH
512 name_len = avio_rl16(pb);
513 if (!name_len)
514 return AVERROR_INVALIDDATA;
515 name = av_malloc(name_len);
516 if (!name)
517 return AVERROR(ENOMEM);
518 avio_get_str16le(pb, name_len, name,
519 name_len);
520 type = avio_rl16(pb);
944f6086
AH
521 // BOOL values are 16 bits long in the Metadata Object
522 // but 32 bits long in the Extended Content Description Object
523 if (type == ASF_BOOL)
524 type = ASF_DWORD;
b08569a2
AH
525 val_len = avio_rl16(pb);
526
fdbc544d
AH
527 ret = process_metadata(s, name, name_len, val_len, type, &s->metadata);
528 av_freep(&name);
529 if (ret < 0)
b08569a2
AH
530 return ret;
531 }
532
533 align_position(pb, asf->offset, size);
c1fea230
AK
534 return 0;
535}
536
b08569a2 537static AVStream *find_stream(AVFormatContext *s, uint16_t st_num)
8bf6db1b 538{
b08569a2 539 AVStream *st = NULL;
8bf6db1b 540 ASFContext *asf = s->priv_data;
b08569a2 541 int i;
8bf6db1b 542
b08569a2
AH
543 for (i = 0; i < asf->nb_streams; i++) {
544 if (asf->asf_st[i]->stream_index == st_num) {
545 st = s->streams[asf->asf_st[i]->index];
546 break;
547 }
8bf6db1b
AK
548 }
549
b08569a2
AH
550 return st;
551}
8bf6db1b 552
944f6086 553static int asf_store_aspect_ratio(AVFormatContext *s, uint8_t st_num, uint8_t *name, int type)
b08569a2
AH
554{
555 ASFContext *asf = s->priv_data;
556 AVIOContext *pb = s->pb;
944f6086
AH
557 uint64_t value = 0;
558 int ret;
8bf6db1b 559
944f6086
AH
560 ret = asf_read_generic_value(pb, type, &value);
561 if (ret < 0)
562 return ret;
8bf6db1b 563
b08569a2
AH
564 if (st_num < ASF_MAX_STREAMS) {
565 if (!strcmp(name, "AspectRatioX"))
566 asf->asf_sd[st_num].aspect_ratio.num = value;
567 else
568 asf->asf_sd[st_num].aspect_ratio.den = value;
8bf6db1b 569 }
944f6086 570 return 0;
b08569a2 571}
8bf6db1b 572
b08569a2
AH
573static int asf_read_metadata_obj(AVFormatContext *s, const GUIDParseTable *g)
574{
575 ASFContext *asf = s->priv_data;
576 AVIOContext *pb = s->pb;
577 uint64_t size = avio_rl64(pb);
578 uint16_t nb_recs = avio_rl16(pb); // number of records in the Description Records list
579 int i, ret;
8bf6db1b 580
b08569a2
AH
581 for (i = 0; i < nb_recs; i++) {
582 uint16_t name_len, buflen, type, val_len, st_num;
583 uint8_t *name = NULL;
8bf6db1b 584
b08569a2
AH
585 avio_skip(pb, 2); // skip reserved field
586 st_num = avio_rl16(pb);
587 name_len = avio_rl16(pb);
588 buflen = 2 * name_len + 1;
589 if (!name_len)
590 break;
591 type = avio_rl16(pb);
592 val_len = avio_rl32(pb);
77cf2366 593 name = av_malloc(buflen);
b08569a2
AH
594 if (!name)
595 return AVERROR(ENOMEM);
596 avio_get_str16le(pb, name_len, name,
597 buflen);
b08569a2 598 if (!strcmp(name, "AspectRatioX") || !strcmp(name, "AspectRatioY")) {
944f6086 599 ret = asf_store_aspect_ratio(s, st_num, name, type);
317cfaa5
AH
600 if (ret < 0) {
601 av_freep(&name);
602 break;
603 }
b08569a2
AH
604 } else {
605 if (st_num < ASF_MAX_STREAMS) {
606 if ((ret = process_metadata(s, name, name_len, val_len, type,
fdbc544d
AH
607 &asf->asf_sd[st_num].asf_met)) < 0) {
608 av_freep(&name);
b08569a2 609 break;
fdbc544d
AH
610 }
611 }
8bf6db1b 612 }
fdbc544d 613 av_freep(&name);
8bf6db1b 614 }
8bf6db1b 615
b08569a2 616 align_position(pb, asf->offset, size);
8bf6db1b
AK
617 return 0;
618}
619
b08569a2 620static int asf_read_content_desc(AVFormatContext *s, const GUIDParseTable *g)
c1fea230
AK
621{
622 ASFContext *asf = s->priv_data;
ae628ec1 623 AVIOContext *pb = s->pb;
b08569a2
AH
624 int i;
625 static const char *const titles[] =
626 { "Title", "Author", "Copyright", "Description", "Rate" };
627 uint16_t len[5], buflen[5] = { 0 };
628 uint8_t *ch;
629 uint64_t size = avio_rl64(pb);
630
631 for (i = 0; i < 5; i++) {
632 len[i] = avio_rl16(pb);
633 // utf8 string should be <= 2 * utf16 string, extra byte for the terminator
634 buflen[i] = 2 * len[i] + 1;
c1fea230
AK
635 }
636
b08569a2
AH
637 for (i = 0; i < 5; i++) {
638 ch = av_malloc(buflen[i]);
639 if (!ch)
640 return(AVERROR(ENOMEM));
641 asf_read_metadata(s, titles[i], len[i], ch, buflen[i]);
642 av_freep(&ch);
c1fea230 643 }
b08569a2 644 align_position(pb, asf->offset, size);
c1fea230
AK
645
646 return 0;
647}
648
b08569a2 649static int asf_read_properties(AVFormatContext *s, const GUIDParseTable *g)
c1fea230 650{
b08569a2 651 ASFContext *asf = s->priv_data;
ae628ec1 652 AVIOContext *pb = s->pb;
e5997152 653 time_t creation_time;
b08569a2
AH
654
655 avio_rl64(pb); // read object size
656 avio_skip(pb, 16); // skip File ID
657 avio_skip(pb, 8); // skip File size
658 creation_time = avio_rl64(pb);
659 if (!(asf->b_flags & ASF_FLAG_BROADCAST)) {
660 struct tm tmbuf;
661 struct tm *tm;
662 char buf[64];
663
664 // creation date is in 100 ns units from 1 Jan 1601, conversion to s
665 creation_time /= 10000000;
666 // there are 11644473600 seconds between 1 Jan 1601 and 1 Jan 1970
667 creation_time -= 11644473600;
668 tm = gmtime_r(&creation_time, &tmbuf);
669 if (tm) {
670 if (!strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm))
671 buf[0] = '\0';
672 } else
673 buf[0] = '\0';
674 if (buf[0]) {
675 if (av_dict_set(&s->metadata, "creation_time", buf, 0) < 0)
676 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
677 }
678 }
679 asf->nb_packets = avio_rl64(pb);
680 asf->duration = avio_rl64(pb) / 10000; // stream duration
681 avio_skip(pb, 8); // skip send duration
682 asf->preroll = avio_rl64(pb);
683 asf->duration -= asf->preroll;
684 asf->b_flags = avio_rl32(pb);
685 avio_skip(pb, 4); // skip minimal packet size
686 asf->packet_size = avio_rl32(pb);
687 avio_skip(pb, 4); // skip max_bitrate
c1fea230
AK
688
689 return 0;
690}
691
b08569a2
AH
692static int parse_video_info(AVIOContext *pb, AVStream *st)
693{
0539d84d
AH
694 uint16_t size_asf; // ASF-specific Format Data size
695 uint32_t size_bmp; // BMP_HEADER-specific Format Data size
b08569a2
AH
696 unsigned int tag;
697
9200514a
AK
698 st->codecpar->width = avio_rl32(pb);
699 st->codecpar->height = avio_rl32(pb);
b08569a2 700 avio_skip(pb, 1); // skip reserved flags
0539d84d
AH
701 size_asf = avio_rl16(pb);
702 tag = ff_get_bmp_header(pb, st, &size_bmp);
9200514a
AK
703 st->codecpar->codec_tag = tag;
704 st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags, tag);
0539d84d 705 size_bmp = FFMAX(size_asf, size_bmp);
b08569a2 706
0539d84d 707 if (size_bmp > BMP_HEADER_SIZE) {
b08569a2 708 int ret;
0539d84d 709 st->codecpar->extradata_size = size_bmp - BMP_HEADER_SIZE;
9200514a 710 if (!(st->codecpar->extradata = av_malloc(st->codecpar->extradata_size +
059a9348 711 AV_INPUT_BUFFER_PADDING_SIZE))) {
9200514a 712 st->codecpar->extradata_size = 0;
b08569a2
AH
713 return AVERROR(ENOMEM);
714 }
9200514a 715 memset(st->codecpar->extradata + st->codecpar->extradata_size , 0,
059a9348 716 AV_INPUT_BUFFER_PADDING_SIZE);
9200514a
AK
717 if ((ret = avio_read(pb, st->codecpar->extradata,
718 st->codecpar->extradata_size)) < 0)
b08569a2
AH
719 return ret;
720 }
721 return 0;
722}
723
724static int asf_read_stream_properties(AVFormatContext *s, const GUIDParseTable *g)
c1fea230 725{
c1fea230 726 ASFContext *asf = s->priv_data;
b08569a2
AH
727 AVIOContext *pb = s->pb;
728 uint64_t size;
729 uint32_t err_data_len, ts_data_len; // type specific data length
730 uint16_t flags;
731 ff_asf_guid stream_type;
732 enum AVMediaType type;
733 int i, ret;
734 uint8_t stream_index;
735 AVStream *st;
736 ASFStream *asf_st;
c1fea230 737
b08569a2
AH
738 // ASF file must not contain more than 128 streams according to the specification
739 if (asf->nb_streams >= ASF_MAX_STREAMS)
740 return AVERROR_INVALIDDATA;
c1fea230 741
b08569a2
AH
742 size = avio_rl64(pb);
743 ff_get_guid(pb, &stream_type);
744 if (!ff_guidcmp(&stream_type, &ff_asf_audio_stream))
745 type = AVMEDIA_TYPE_AUDIO;
746 else if (!ff_guidcmp(&stream_type, &ff_asf_video_stream))
747 type = AVMEDIA_TYPE_VIDEO;
748 else if (!ff_guidcmp(&stream_type, &ff_asf_jfif_media))
749 type = AVMEDIA_TYPE_VIDEO;
750 else if (!ff_guidcmp(&stream_type, &ff_asf_command_stream))
751 type = AVMEDIA_TYPE_DATA;
752 else if (!ff_guidcmp(&stream_type,
753 &ff_asf_ext_stream_embed_stream_header))
754 type = AVMEDIA_TYPE_UNKNOWN;
755 else
756 return AVERROR_INVALIDDATA;
757
758 ff_get_guid(pb, &stream_type); // error correction type
759 avio_skip(pb, 8); // skip the time offset
760 ts_data_len = avio_rl32(pb);
761 err_data_len = avio_rl32(pb);
762 flags = avio_rl16(pb); // bit 15 - Encrypted Content
763
764 stream_index = flags & ASF_STREAM_NUM;
765 for (i = 0; i < asf->nb_streams; i++)
766 if (stream_index == asf->asf_st[i]->stream_index) {
767 av_log(s, AV_LOG_WARNING,
768 "Duplicate stream found, this stream will be ignored.\n");
769 align_position(pb, asf->offset, size);
770 return 0;
771 }
772
773 st = avformat_new_stream(s, NULL);
774 if (!st)
775 return AVERROR(ENOMEM);
776 avpriv_set_pts_info(st, 32, 1, 1000); // pts should be dword, in milliseconds
9200514a 777 st->codecpar->codec_type = type;
b08569a2
AH
778 asf->asf_st[asf->nb_streams] = av_mallocz(sizeof(*asf_st));
779 if (!asf->asf_st[asf->nb_streams])
780 return AVERROR(ENOMEM);
781 asf_st = asf->asf_st[asf->nb_streams];
aed7715b 782 asf->nb_streams++;
b08569a2
AH
783 asf_st->stream_index = stream_index;
784 asf_st->index = st->index;
785 asf_st->indexed = 0;
786 st->id = flags & ASF_STREAM_NUM;
787 av_init_packet(&asf_st->pkt.avpkt);
788 asf_st->pkt.data_size = 0;
789 avio_skip(pb, 4); // skip reserved field
c1fea230 790
b08569a2
AH
791 switch (type) {
792 case AVMEDIA_TYPE_AUDIO:
793 asf_st->type = AVMEDIA_TYPE_AUDIO;
9200514a 794 if ((ret = ff_get_wav_header(s, pb, st->codecpar, ts_data_len)) < 0)
b08569a2
AH
795 return ret;
796 break;
797 case AVMEDIA_TYPE_VIDEO:
798 asf_st->type = AVMEDIA_TYPE_VIDEO;
799 if ((ret = parse_video_info(pb, st)) < 0)
800 return ret;
801 break;
802 default:
803 avio_skip(pb, ts_data_len);
804 break;
805 }
806
807 if (err_data_len) {
808 if (type == AVMEDIA_TYPE_AUDIO) {
809 uint8_t span = avio_r8(pb);
810 if (span > 1) {
811 asf_st->span = span;
812 asf_st->virtual_pkt_len = avio_rl16(pb);
813 asf_st->virtual_chunk_len = avio_rl16(pb);
2a187a07
AH
814 if (!asf_st->virtual_chunk_len || !asf_st->virtual_pkt_len)
815 return AVERROR_INVALIDDATA;
b08569a2
AH
816 avio_skip(pb, err_data_len - 5);
817 } else
818 avio_skip(pb, err_data_len - 1);
819 } else
820 avio_skip(pb, err_data_len);
821 }
822
b08569a2
AH
823 align_position(pb, asf->offset, size);
824
c1fea230
AK
825 return 0;
826}
827
b08569a2
AH
828static void set_language(AVFormatContext *s, const char *rfc1766, AVDictionary **met)
829{
830 // language abbr should contain at least 2 chars
831 if (rfc1766 && strlen(rfc1766) > 1) {
832 const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
833 const char *iso6392 = av_convert_lang_to(primary_tag,
834 AV_LANG_ISO639_2_BIBL);
835 if (iso6392)
836 if (av_dict_set(met, "language", iso6392, 0) < 0)
837 av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
838 }
839}
840
841static int asf_read_ext_stream_properties(AVFormatContext *s, const GUIDParseTable *g)
c1fea230 842{
c1fea230 843 ASFContext *asf = s->priv_data;
b08569a2
AH
844 AVIOContext *pb = s->pb;
845 AVStream *st = NULL;
846 ff_asf_guid guid;
847 uint16_t nb_st_name, nb_pay_exts, st_num, lang_idx;
848 int i, ret;
849 uint32_t bitrate;
850 uint64_t start_time, end_time, time_per_frame;
851 uint64_t size = avio_rl64(pb);
852
853 start_time = avio_rl64(pb);
854 end_time = avio_rl64(pb);
855 bitrate = avio_rl32(pb);
856 avio_skip(pb, 28); // skip some unused values
857 st_num = avio_rl16(pb);
858 st_num &= ASF_STREAM_NUM;
859 lang_idx = avio_rl16(pb); // Stream Language ID Index
860 for (i = 0; i < asf->nb_streams; i++) {
861 if (st_num == asf->asf_st[i]->stream_index) {
862 st = s->streams[asf->asf_st[i]->index];
863 asf->asf_st[i]->lang_idx = lang_idx;
864 break;
865 }
866 }
867 time_per_frame = avio_rl64(pb); // average time per frame
868 if (st) {
869 st->start_time = start_time;
870 st->duration = end_time - start_time;
9200514a 871 st->codecpar->bit_rate = bitrate;
b08569a2
AH
872 st->avg_frame_rate.num = 10000000;
873 st->avg_frame_rate.den = time_per_frame;
874 }
875 nb_st_name = avio_rl16(pb);
876 nb_pay_exts = avio_rl16(pb);
877 for (i = 0; i < nb_st_name; i++) {
878 uint16_t len;
879
880 avio_rl16(pb); // Language ID Index
881 len = avio_rl16(pb);
882 avio_skip(pb, len);
883 }
884
885 for (i = 0; i < nb_pay_exts; i++) {
886 uint32_t len;
887 avio_skip(pb, 16); // Extension System ID
888 avio_skip(pb, 2); // Extension Data Size
889 len = avio_rl32(pb);
890 avio_skip(pb, len);
891 }
892
893 if ((ret = ff_get_guid(pb, &guid)) < 0) {
894 align_position(pb, asf->offset, size);
895
896 return 0;
c1fea230
AK
897 }
898
b08569a2
AH
899 g = find_guid(guid);
900 if (g && !(strcmp(g->name, "Stream Properties"))) {
901 if ((ret = g->read_object(s, g)) < 0)
902 return ret;
903 }
904
905 align_position(pb, asf->offset, size);
c1fea230
AK
906 return 0;
907}
908
b08569a2 909static int asf_read_language_list(AVFormatContext *s, const GUIDParseTable *g)
c1fea230 910{
b08569a2
AH
911 ASFContext *asf = s->priv_data;
912 AVIOContext *pb = s->pb;
913 int i, ret;
914 uint64_t size = avio_rl64(pb);
915 uint16_t nb_langs = avio_rl16(pb);
916
917 if (nb_langs < ASF_MAX_STREAMS) {
918 for (i = 0; i < nb_langs; i++) {
919 size_t len;
920 len = avio_r8(pb);
921 if (!len)
922 len = 6;
923 if ((ret = get_asf_string(pb, len, asf->asf_sd[i].langs,
924 sizeof(asf->asf_sd[i].langs))) < 0) {
925 return ret;
926 }
c1fea230
AK
927 }
928 }
929
b08569a2 930 align_position(pb, asf->offset, size);
c1fea230
AK
931 return 0;
932}
933
b08569a2
AH
934// returns data object offset when reading this object for the first time
935static int asf_read_data(AVFormatContext *s, const GUIDParseTable *g)
c1fea230 936{
5f408333 937 ASFContext *asf = s->priv_data;
b08569a2
AH
938 AVIOContext *pb = s->pb;
939 uint64_t size = asf->data_size = avio_rl64(pb);
940 int i;
c1fea230 941
7f388c0f 942 if (!asf->data_reached) {
b08569a2
AH
943 asf->data_reached = 1;
944 asf->data_offset = asf->offset;
945 }
946
947 for (i = 0; i < asf->nb_streams; i++) {
948 if (!(asf->b_flags & ASF_FLAG_BROADCAST))
949 s->streams[i]->duration = asf->duration;
c1fea230 950 }
b08569a2
AH
951 asf->nb_mult_left = 0;
952 asf->sub_left = 0;
953 asf->state = PARSE_PACKET_HEADER;
954 asf->return_subpayload = 0;
955 asf->packet_size_internal = 0;
956 avio_skip(pb, 16); // skip File ID
957 size = avio_rl64(pb); // Total Data Packets
958 if (size != asf->nb_packets)
959 av_log(s, AV_LOG_WARNING,
960 "Number of Packets from File Properties Object is not equal to Total"
961 "Datapackets value! num of packets %"PRIu64" total num %"PRIu64".\n",
962 size, asf->nb_packets);
963 avio_skip(pb, 2); // skip reserved field
964 asf->first_packet_offset = avio_tell(pb);
83548fe8 965 if ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !(asf->b_flags & ASF_FLAG_BROADCAST))
872fab4a 966 align_position(pb, asf->offset, asf->data_size);
c1fea230
AK
967
968 return 0;
969}
970
b08569a2 971static int asf_read_simple_index(AVFormatContext *s, const GUIDParseTable *g)
de6d9b64 972{
c9a65ca8 973 ASFContext *asf = s->priv_data;
ae628ec1 974 AVIOContext *pb = s->pb;
b08569a2
AH
975 AVStream *st = NULL;
976 uint64_t interval; // index entry time interval in 100 ns units, usually it's 1s
977 uint32_t pkt_num, nb_entries;
978 int32_t prev_pkt_num = -1;
e4d1621c 979 int i, ret;
b08569a2
AH
980 uint64_t size = avio_rl64(pb);
981
982 // simple index objects should be ordered by stream number, this loop tries to find
983 // the first not indexed video stream
984 for (i = 0; i < asf->nb_streams; i++) {
985 if ((asf->asf_st[i]->type == AVMEDIA_TYPE_VIDEO) && !asf->asf_st[i]->indexed) {
986 asf->asf_st[i]->indexed = 1;
987 st = s->streams[asf->asf_st[i]->index];
09d9672a
MN
988 break;
989 }
de6d9b64 990 }
b08569a2
AH
991 if (!st) {
992 avio_skip(pb, size - 24); // if there's no video stream, skip index object
993 return 0;
994 }
995 avio_skip(pb, 16); // skip File ID
996 interval = avio_rl64(pb);
997 avio_skip(pb, 4);
998 nb_entries = avio_rl32(pb);
999 for (i = 0; i < nb_entries; i++) {
1000 pkt_num = avio_rl32(pb);
e4d1621c
AC
1001 ret = avio_skip(pb, 2);
1002 if (ret < 0) {
1003 av_log(s, AV_LOG_ERROR, "Skipping failed in asf_read_simple_index.\n");
1004 return ret;
1005 }
b08569a2
AH
1006 if (prev_pkt_num != pkt_num) {
1007 av_add_index_entry(st, asf->first_packet_offset + asf->packet_size *
1008 pkt_num, av_rescale(interval, i, 10000),
1009 asf->packet_size, 0, AVINDEX_KEYFRAME);
1010 prev_pkt_num = pkt_num;
30e88789
MN
1011 }
1012 }
b08569a2
AH
1013 asf->is_simple_index = 1;
1014 align_position(pb, asf->offset, size);
03700d39 1015
de6d9b64 1016 return 0;
de6d9b64
FB
1017}
1018
b08569a2
AH
1019static const GUIDParseTable gdef[] = {
1020 { "Data", { 0x75, 0xB2, 0x26, 0x36, 0x66, 0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_data, 1 },
1021 { "Simple Index", { 0x33, 0x00, 0x08, 0x90, 0xE5, 0xB1, 0x11, 0xCF, 0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB }, asf_read_simple_index, 1 },
1022 { "Content Description", { 0x75, 0xB2, 0x26, 0x33, 0x66 ,0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_content_desc, 1 },
1023 { "Extended Content Description", { 0xD2, 0xD0, 0xA4, 0x40, 0xE3, 0x07, 0x11, 0xD2, 0x97, 0xF0, 0x00, 0xA0, 0xC9, 0x5e, 0xA8, 0x50 }, asf_read_ext_content, 1 },
1024 { "Stream Bitrate Properties", { 0x7B, 0xF8, 0x75, 0xCE, 0x46, 0x8D, 0x11, 0xD1, 0x8D, 0x82, 0x00, 0x60, 0x97, 0xC9, 0xA2, 0xB2 }, asf_read_unknown, 1 },
1025 { "File Properties", { 0x8C, 0xAB, 0xDC, 0xA1, 0xA9, 0x47, 0x11, 0xCF, 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_properties, 1 },
1026 { "Header Extension", { 0x5F, 0xBF, 0x03, 0xB5, 0xA9, 0x2E, 0x11, 0xCF, 0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_unknown, 0 },
1027 { "Stream Properties", { 0xB7, 0xDC, 0x07, 0x91, 0xA9, 0xB7, 0x11, 0xCF, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_stream_properties, 1 },
1028 { "Codec List", { 0x86, 0xD1, 0x52, 0x40, 0x31, 0x1D, 0x11, 0xD0, 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 }, asf_read_unknown, 1 },
1029 { "Marker", { 0xF4, 0x87, 0xCD, 0x01, 0xA9, 0x51, 0x11, 0xCF, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_marker, 1 },
1030 { "Script Command", { 0x1E, 0xFB, 0x1A, 0x30, 0x0B, 0x62, 0x11, 0xD0, 0xA3, 0x9B, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 }, asf_read_unknown, 1 },
1031 { "Language List", { 0x7C, 0x43, 0x46, 0xa9, 0xef, 0xe0, 0x4B, 0xFC, 0xB2, 0x29, 0x39, 0x3e, 0xde, 0x41, 0x5c, 0x85 }, asf_read_language_list, 1},
1032 { "Padding", { 0x18, 0x06, 0xD4, 0x74, 0xCA, 0xDF, 0x45, 0x09, 0xA4, 0xBA, 0x9A, 0xAB, 0xCB, 0x96, 0xAA, 0xE8 }, asf_read_unknown, 1 },
1033 { "DRMv1 Header", { 0x22, 0x11, 0xB3, 0xFB, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1034 { "DRMv2 Header", { 0x29, 0x8A, 0xE6, 0x14, 0x26, 0x22, 0x4C, 0x17, 0xB9, 0x35, 0xDA, 0xE0, 0x7E, 0xE9, 0x28, 0x9c }, asf_read_unknown, 1 },
1035 { "Index", { 0xD6, 0xE2, 0x29, 0xD3, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1036 { "Media Object Index", { 0xFE, 0xB1, 0x03, 0xF8, 0x12, 0xAD, 0x4C, 0x64, 0x84, 0x0F, 0x2A, 0x1D, 0x2F, 0x7A, 0xD4, 0x8C }, asf_read_unknown, 1 },
1037 { "Timecode Index", { 0x3C, 0xB7, 0x3F, 0xD0, 0x0C, 0x4A, 0x48, 0x03, 0x95, 0x3D, 0xED, 0xF7, 0xB6, 0x22, 0x8F, 0x0C }, asf_read_unknown, 0 },
1038 { "Bitrate_Mutual_Exclusion", { 0xD6, 0xE2, 0x29, 0xDC, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1039 { "Error Correction", { 0x75, 0xB2, 0x26, 0x35, 0x66, 0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_unknown, 1 },
1040 { "Content Branding", { 0x22, 0x11, 0xB3, 0xFA, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1041 { "Content Encryption", { 0x22, 0x11, 0xB3, 0xFB, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1042 { "Extended Content Encryption", { 0x29, 0x8A, 0xE6, 0x14, 0x26, 0x22, 0x4C, 0x17, 0xB9, 0x35, 0xDA, 0xE0, 0x7E, 0xE9, 0x28, 0x9C }, asf_read_unknown, 1 },
1043 { "Digital Signature", { 0x22, 0x11, 0xB3, 0xFC, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1044 { "Extended Stream Properties", { 0x14, 0xE6, 0xA5, 0xCB, 0xC6, 0x72, 0x43, 0x32, 0x83, 0x99, 0xA9, 0x69, 0x52, 0x06, 0x5B, 0x5A }, asf_read_ext_stream_properties, 1 },
1045 { "Advanced Mutual Exclusion", { 0xA0, 0x86, 0x49, 0xCF, 0x47, 0x75, 0x46, 0x70, 0x8A, 0x16, 0x6E, 0x35, 0x35, 0x75, 0x66, 0xCD }, asf_read_unknown, 1 },
1046 { "Group Mutual Exclusion", { 0xD1, 0x46, 0x5A, 0x40, 0x5A, 0x79, 0x43, 0x38, 0xB7, 0x1B, 0xE3, 0x6B, 0x8F, 0xD6, 0xC2, 0x49 }, asf_read_unknown, 1},
1047 { "Stream Prioritization", { 0xD4, 0xFE, 0xD1, 0x5B, 0x88, 0xD3, 0x45, 0x4F, 0x81, 0xF0, 0xED, 0x5C, 0x45, 0x99, 0x9E, 0x24 }, asf_read_unknown, 1 },
1048 { "Bandwidth Sharing Object", { 0xA6, 0x96, 0x09, 0xE6, 0x51, 0x7B, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 },
1049 { "Metadata", { 0xC5, 0xF8, 0xCB, 0xEA, 0x5B, 0xAF, 0x48, 0x77, 0x84, 0x67, 0xAA, 0x8C, 0x44, 0xFA, 0x4C, 0xCA }, asf_read_metadata_obj, 1 },
1050 { "Metadata Library", { 0x44, 0x23, 0x1C, 0x94, 0x94, 0x98, 0x49, 0xD1, 0xA1, 0x41, 0x1D, 0x13, 0x4E, 0x45, 0x70, 0x54 }, asf_read_metadata_obj, 1 },
1051 { "Audio Spread", { 0xBF, 0xC3, 0xCD, 0x50, 0x61, 0x8F, 0x11, 0xCF, 0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20 }, asf_read_unknown, 1 },
1052 { "Index Parameters", { 0xD6, 0xE2, 0x29, 0xDF, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1053 { "Content Encryption System Windows Media DRM Network Devices",
1054 { 0x7A, 0x07, 0x9B, 0xB6, 0xDA, 0XA4, 0x4e, 0x12, 0xA5, 0xCA, 0x91, 0xD3, 0x8D, 0xC1, 0x1A, 0x8D }, asf_read_unknown, 1 },
1055 { "Mutex Language", { 0xD6, 0xE2, 0x2A, 0x00, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1056 { "Mutex Bitrate", { 0xD6, 0xE2, 0x2A, 0x01, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1057 { "Mutex Unknown", { 0xD6, 0xE2, 0x2A, 0x02, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1058 { "Bandwith Sharing Exclusive", { 0xAF, 0x60, 0x60, 0xAA, 0x51, 0x97, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 },
1059 { "Bandwith Sharing Partial", { 0xAF, 0x60, 0x60, 0xAB, 0x51, 0x97, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 },
1060 { "Payload Extension System Timecode", { 0x39, 0x95, 0x95, 0xEC, 0x86, 0x67, 0x4E, 0x2D, 0x8F, 0xDB, 0x98, 0x81, 0x4C, 0xE7, 0x6C, 0x1E }, asf_read_unknown, 1 },
1061 { "Payload Extension System File Name", { 0xE1, 0x65, 0xEC, 0x0E, 0x19, 0xED, 0x45, 0xD7, 0xB4, 0xA7, 0x25, 0xCB, 0xD1, 0xE2, 0x8E, 0x9B }, asf_read_unknown, 1 },
1062 { "Payload Extension System Content Type", { 0xD5, 0x90, 0xDC, 0x20, 0x07, 0xBC, 0x43, 0x6C, 0x9C, 0xF7, 0xF3, 0xBB, 0xFB, 0xF1, 0xA4, 0xDC }, asf_read_unknown, 1 },
1063 { "Payload Extension System Pixel Aspect Ratio", { 0x1, 0x1E, 0xE5, 0x54, 0xF9, 0xEA, 0x4B, 0xC8, 0x82, 0x1A, 0x37, 0x6B, 0x74, 0xE4, 0xC4, 0xB8 }, asf_read_unknown, 1 },
1064 { "Payload Extension System Sample Duration", { 0xC6, 0xBD, 0x94, 0x50, 0x86, 0x7F, 0x49, 0x07, 0x83, 0xA3, 0xC7, 0x79, 0x21, 0xB7, 0x33, 0xAD }, asf_read_unknown, 1 },
1065 { "Payload Extension System Encryption Sample ID", { 0x66, 0x98, 0xB8, 0x4E, 0x0A, 0xFA, 0x43, 0x30, 0xAE, 0xB2, 0x1C, 0x0A, 0x98, 0xD7, 0xA4, 0x4D }, asf_read_unknown, 1 },
1066 { "Payload Extension System Degradable JPEG", { 0x00, 0xE1, 0xAF, 0x06, 0x7B, 0xEC, 0x11, 0xD1, 0xA5, 0x82, 0x00, 0xC0, 0x4F, 0xC2, 0x9C, 0xFB }, asf_read_unknown, 1 },
1067};
2a10020b 1068
b08569a2
AH
1069#define READ_LEN(flag, name, len) \
1070 do { \
1071 if ((flag) == name ## IS_BYTE) \
1072 len = avio_r8(pb); \
1073 else if ((flag) == name ## IS_WORD) \
1074 len = avio_rl16(pb); \
1075 else if ((flag) == name ## IS_DWORD) \
1076 len = avio_rl32(pb); \
1077 else \
1078 len = 0; \
1079 } while(0)
1080
1081static int asf_read_subpayload(AVFormatContext *s, AVPacket *pkt, int is_header)
de6d9b64
FB
1082{
1083 ASFContext *asf = s->priv_data;
b08569a2
AH
1084 AVIOContext *pb = s->pb;
1085 uint8_t sub_len;
1086 int ret, i;
115329f1 1087
b08569a2
AH
1088 if (is_header) {
1089 asf->dts_delta = avio_r8(pb);
1090 if (asf->nb_mult_left) {
1091 asf->mult_sub_len = avio_rl16(pb); // total
1092 }
1093 asf->sub_header_offset = avio_tell(pb);
1094 asf->nb_sub = 0;
1095 asf->sub_left = 1;
2a10020b 1096 }
b08569a2
AH
1097 sub_len = avio_r8(pb);
1098 if ((ret = av_get_packet(pb, pkt, sub_len)) < 0) // each subpayload is entire frame
1099 return ret;
1100 for (i = 0; i < asf->nb_streams; i++) {
1101 if (asf->stream_index == asf->asf_st[i]->stream_index) {
1102 pkt->stream_index = asf->asf_st[i]->index;
1103 break;
bb270c08 1104 }
de6d9b64 1105 }
b08569a2
AH
1106 asf->return_subpayload = 1;
1107 if (!sub_len)
1108 asf->return_subpayload = 0;
1109
1110 if (sub_len)
1111 asf->nb_sub++;
1112 pkt->dts = asf->sub_dts + (asf->nb_sub - 1) * asf->dts_delta - asf->preroll;
1113 if (asf->nb_mult_left && (avio_tell(pb) >=
1114 (asf->sub_header_offset + asf->mult_sub_len))) {
1115 asf->sub_left = 0;
1116 asf->nb_mult_left--;
5b11e081 1117 }
b08569a2
AH
1118 if (avio_tell(pb) >= asf->packet_offset + asf->packet_size - asf->pad_len) {
1119 asf->sub_left = 0;
1120 if (!asf->nb_mult_left) {
1121 avio_skip(pb, asf->pad_len);
1122 if (avio_tell(pb) != asf->packet_offset + asf->packet_size) {
1123 if (!asf->packet_size)
1124 return AVERROR_INVALIDDATA;
1125 av_log(s, AV_LOG_WARNING,
1126 "Position %"PRId64" wrong, should be %"PRId64"\n",
1127 avio_tell(pb), asf->packet_offset + asf->packet_size);
1128 avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1129 }
1130 }
5b11e081
MN
1131 }
1132
de6d9b64
FB
1133 return 0;
1134}
1135
b08569a2
AH
1136static void reset_packet(ASFPacket *asf_pkt)
1137{
1138 asf_pkt->size_left = 0;
1139 asf_pkt->data_size = 0;
1140 asf_pkt->duration = 0;
1141 asf_pkt->flags = 0;
1142 asf_pkt->dts = 0;
1143 asf_pkt->duration = 0;
ce70f28a 1144 av_packet_unref(&asf_pkt->avpkt);
b08569a2
AH
1145 av_init_packet(&asf_pkt->avpkt);
1146}
1147
2883ef34
AH
1148static int asf_read_replicated_data(AVFormatContext *s, ASFPacket *asf_pkt)
1149{
1150 ASFContext *asf = s->priv_data;
1151 AVIOContext *pb = s->pb;
c69461d7 1152 int ret, data_size;
2883ef34
AH
1153
1154 if (!asf_pkt->data_size) {
c69461d7
AC
1155 data_size = avio_rl32(pb); // read media object size
1156 if (data_size <= 0)
c0a49077 1157 return AVERROR_INVALIDDATA;
c69461d7 1158 if ((ret = av_new_packet(&asf_pkt->avpkt, data_size)) < 0)
2883ef34 1159 return ret;
c69461d7 1160 asf_pkt->data_size = asf_pkt->size_left = data_size;
2883ef34
AH
1161 } else
1162 avio_skip(pb, 4); // reading of media object size is already done
1163 asf_pkt->dts = avio_rl32(pb); // read presentation time
1164 if (asf->rep_data_len && (asf->rep_data_len >= 8))
1165 avio_skip(pb, asf->rep_data_len - 8); // skip replicated data
1166
1167 return 0;
1168}
1169
b08569a2
AH
1170static int asf_read_multiple_payload(AVFormatContext *s, AVPacket *pkt,
1171 ASFPacket *asf_pkt)
48a4ffa7 1172{
ae60a857 1173 ASFContext *asf = s->priv_data;
b08569a2
AH
1174 AVIOContext *pb = s->pb;
1175 uint16_t pay_len;
1176 unsigned char *p;
1177 int ret;
1178 int skip = 0;
1179
41ed7ab4 1180 // if replicated length is 1, subpayloads are present
b08569a2
AH
1181 if (asf->rep_data_len == 1) {
1182 asf->sub_left = 1;
1183 asf->state = READ_MULTI_SUB;
1184 pkt->flags = asf_pkt->flags;
1185 if ((ret = asf_read_subpayload(s, pkt, 1)) < 0)
1186 return ret;
1187 } else {
2883ef34
AH
1188 if (asf->rep_data_len)
1189 if ((ret = asf_read_replicated_data(s, asf_pkt)) < 0)
1190 return ret;
b08569a2
AH
1191 pay_len = avio_rl16(pb); // payload length should be WORD
1192 if (pay_len > asf->packet_size) {
1193 av_log(s, AV_LOG_ERROR,
1194 "Error: invalid data packet size, pay_len %"PRIu16", "
1195 "asf->packet_size %"PRIu32", offset %"PRId64".\n",
1196 pay_len, asf->packet_size, avio_tell(pb));
1197 return AVERROR_INVALIDDATA;
13f7e4b9 1198 }
b08569a2
AH
1199 p = asf_pkt->avpkt.data + asf_pkt->data_size - asf_pkt->size_left;
1200 if (pay_len > asf_pkt->size_left) {
1201 av_log(s, AV_LOG_ERROR,
1202 "Error: invalid buffer size, pay_len %d, data size left %d.\n",
1203 pay_len, asf_pkt->size_left);
1204 skip = pay_len - asf_pkt->size_left;
1205 pay_len = asf_pkt->size_left;
0bd433a9 1206 }
c0a49077
AH
1207 if (asf_pkt->size_left <= 0)
1208 return AVERROR_INVALIDDATA;
b08569a2
AH
1209 if ((ret = avio_read(pb, p, pay_len)) < 0)
1210 return ret;
1211 if (s->key && s->keylen == 20)
1212 ff_asfcrypt_dec(s->key, p, ret);
1213 avio_skip(pb, skip);
1214 asf_pkt->size_left -= pay_len;
1215 asf->nb_mult_left--;
ae60a857 1216 }
ae60a857
MN
1217
1218 return 0;
1219}
1220
06628137 1221static int asf_read_single_payload(AVFormatContext *s, ASFPacket *asf_pkt)
de6d9b64 1222{
b08569a2
AH
1223 ASFContext *asf = s->priv_data;
1224 AVIOContext *pb = s->pb;
1225 int64_t offset;
1226 uint64_t size;
1227 unsigned char *p;
c69461d7 1228 int ret, data_size;
b08569a2
AH
1229
1230 if (!asf_pkt->data_size) {
34d45b36 1231 data_size = avio_rl32(pb); // read media object size
c69461d7 1232 if (data_size <= 0)
799a0722 1233 return AVERROR_EOF;
c69461d7 1234 if ((ret = av_new_packet(&asf_pkt->avpkt, data_size)) < 0)
b08569a2 1235 return ret;
c69461d7 1236 asf_pkt->data_size = asf_pkt->size_left = data_size;
b08569a2
AH
1237 } else
1238 avio_skip(pb, 4); // skip media object size
1239 asf_pkt->dts = avio_rl32(pb); // read presentation time
0989d3ad 1240 if (asf->rep_data_len >= 8)
b08569a2
AH
1241 avio_skip(pb, asf->rep_data_len - 8); // skip replicated data
1242 offset = avio_tell(pb);
1243
1244 // size of the payload - size of the packet without header and padding
1245 if (asf->packet_size_internal)
1246 size = asf->packet_size_internal - offset + asf->packet_offset - asf->pad_len;
1247 else
1248 size = asf->packet_size - offset + asf->packet_offset - asf->pad_len;
1249 if (size > asf->packet_size) {
1250 av_log(s, AV_LOG_ERROR,
1251 "Error: invalid data packet size, offset %"PRId64".\n",
1252 avio_tell(pb));
1253 return AVERROR_INVALIDDATA;
1254 }
1255 p = asf_pkt->avpkt.data + asf_pkt->data_size - asf_pkt->size_left;
c0a49077 1256 if (size > asf_pkt->size_left || asf_pkt->size_left <= 0)
b08569a2
AH
1257 return AVERROR_INVALIDDATA;
1258 if (asf_pkt->size_left > size)
1259 asf_pkt->size_left -= size;
1260 else
1261 asf_pkt->size_left = 0;
1262 if ((ret = avio_read(pb, p, size)) < 0)
1263 return ret;
1264 if (s->key && s->keylen == 20)
1265 ff_asfcrypt_dec(s->key, p, ret);
1266 if (asf->packet_size_internal)
1267 avio_skip(pb, asf->packet_size - asf->packet_size_internal);
1268 avio_skip(pb, asf->pad_len); // skip padding
292a08cb 1269
b08569a2
AH
1270 return 0;
1271}
b04d5d31 1272
b08569a2
AH
1273static int asf_read_payload(AVFormatContext *s, AVPacket *pkt)
1274{
1275 ASFContext *asf = s->priv_data;
1276 AVIOContext *pb = s->pb;
1277 int ret, i;
1278 ASFPacket *asf_pkt = NULL;
1279
1280 if (!asf->sub_left) {
1281 uint32_t off_len, media_len;
1282 uint8_t stream_num;
1283
1284 stream_num = avio_r8(pb);
1285 asf->stream_index = stream_num & ASF_STREAM_NUM;
1286 for (i = 0; i < asf->nb_streams; i++) {
1287 if (asf->stream_index == asf->asf_st[i]->stream_index) {
1288 asf_pkt = &asf->asf_st[i]->pkt;
1289 asf_pkt->stream_index = asf->asf_st[i]->index;
b08569a2 1290 break;
bb270c08 1291 }
bb270c08 1292 }
8375dc1d
AH
1293 if (!asf_pkt) {
1294 if (asf->packet_offset + asf->packet_size <= asf->data_offset + asf->data_size) {
2e6ba199
AC
1295 if (!asf->packet_size) {
1296 av_log(s, AV_LOG_ERROR, "Invalid packet size 0.\n");
1297 return AVERROR_INVALIDDATA;
1298 }
8375dc1d
AH
1299 avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1300 av_log(s, AV_LOG_WARNING, "Skipping the stream with the invalid stream index %d.\n",
1301 asf->stream_index);
1302 return AVERROR(EAGAIN);
1303 } else
1304 return AVERROR_INVALIDDATA;
1305 }
1306
b08569a2
AH
1307 if (stream_num >> 7)
1308 asf_pkt->flags |= AV_PKT_FLAG_KEY;
1309 READ_LEN(asf->prop_flags & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE,
1310 ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_, media_len);
1311 READ_LEN(asf->prop_flags & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE,
1312 ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_, off_len);
1313 READ_LEN(asf->prop_flags & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE,
1314 ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_, asf->rep_data_len);
1315 if (asf_pkt->size_left && (asf_pkt->frame_num != media_len)) {
1316 av_log(s, AV_LOG_WARNING, "Unfinished frame will be ignored\n");
1317 reset_packet(asf_pkt);
bb270c08 1318 }
b08569a2
AH
1319 asf_pkt->frame_num = media_len;
1320 asf->sub_dts = off_len;
1321 if (asf->nb_mult_left) {
1322 if ((ret = asf_read_multiple_payload(s, pkt, asf_pkt)) < 0)
1323 return ret;
1324 } else if (asf->rep_data_len == 1) {
1325 asf->sub_left = 1;
1326 asf->state = READ_SINGLE;
1327 pkt->flags = asf_pkt->flags;
1328 if ((ret = asf_read_subpayload(s, pkt, 1)) < 0)
1329 return ret;
1330 } else {
06628137 1331 if ((ret = asf_read_single_payload(s, asf_pkt)) < 0)
b08569a2 1332 return ret;
60ff1c44 1333 }
b08569a2
AH
1334 } else {
1335 for (i = 0; i <= asf->nb_streams; i++) {
1336 if (asf->stream_index == asf->asf_st[i]->stream_index) {
1337 asf_pkt = &asf->asf_st[i]->pkt;
1338 break;
2d8591c2 1339 }
bb270c08 1340 }
b08569a2
AH
1341 if (!asf_pkt)
1342 return AVERROR_INVALIDDATA;
1343 pkt->flags = asf_pkt->flags;
1344 pkt->dts = asf_pkt->dts;
1345 pkt->stream_index = asf->asf_st[i]->index;
1346 if ((ret = asf_read_subpayload(s, pkt, 0)) < 0) // read subpayload without its header
1347 return ret;
1348 }
bb270c08 1349
b08569a2
AH
1350 return 0;
1351}
4c71d727 1352
b08569a2
AH
1353static int asf_read_packet_header(AVFormatContext *s)
1354{
1355 ASFContext *asf = s->priv_data;
1356 AVIOContext *pb = s->pb;
1357 uint64_t size;
1358 uint32_t av_unused seq;
1359 unsigned char error_flags, len_flags, pay_flags;
1360
1361 asf->packet_offset = avio_tell(pb);
1362 error_flags = avio_r8(pb); // read Error Correction Flags
9e8627a1 1363 if (error_flags & ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT) {
b08569a2
AH
1364 if (!(error_flags & ASF_ERROR_CORRECTION_LENGTH_TYPE)) {
1365 size = error_flags & ASF_PACKET_ERROR_CORRECTION_DATA_SIZE;
1366 avio_skip(pb, size);
4c71d727 1367 }
9e8627a1
AH
1368 len_flags = avio_r8(pb);
1369 } else
1370 len_flags = error_flags;
b08569a2
AH
1371 asf->prop_flags = avio_r8(pb);
1372 READ_LEN(len_flags & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE,
1373 ASF_PPI_FLAG_PACKET_LENGTH_FIELD_, asf->packet_size_internal);
1374 READ_LEN(len_flags & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE,
1375 ASF_PPI_FLAG_SEQUENCE_FIELD_, seq);
1376 READ_LEN(len_flags & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE,
1377 ASF_PPI_FLAG_PADDING_LENGTH_FIELD_, asf->pad_len );
1378 asf->send_time = avio_rl32(pb); // send time
1379 avio_skip(pb, 2); // skip duration
1380 if (len_flags & ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT) { // Multiple Payloads present
1381 pay_flags = avio_r8(pb);
1382 asf->nb_mult_left = (pay_flags & ASF_NUM_OF_PAYLOADS);
1383 }
4c71d727 1384
b08569a2
AH
1385 return 0;
1386}
1387
1388static int asf_deinterleave(AVFormatContext *s, ASFPacket *asf_pkt, int st_num)
1389{
1390 ASFContext *asf = s->priv_data;
1391 ASFStream *asf_st = asf->asf_st[st_num];
1392 unsigned char *p = asf_pkt->avpkt.data;
1393 uint16_t pkt_len = asf->asf_st[st_num]->virtual_pkt_len;
1394 uint16_t chunk_len = asf->asf_st[st_num]->virtual_chunk_len;
1395 int nchunks = pkt_len / chunk_len;
1396 AVPacket pkt;
1397 int pos = 0, j, l, ret;
1398
1399
1400 if ((ret = av_new_packet(&pkt, asf_pkt->data_size)) < 0)
1401 return ret;
1402
1403 while (asf_pkt->data_size >= asf_st->span * pkt_len + pos) {
1404 if (pos >= asf_pkt->data_size) {
1405 break;
bf09a019 1406 }
b08569a2
AH
1407 for (l = 0; l < pkt_len; l++) {
1408 if (pos >= asf_pkt->data_size) {
1409 break;
da0b94f5 1410 }
b08569a2
AH
1411 for (j = 0; j < asf_st->span; j++) {
1412 if ((pos + chunk_len) >= asf_pkt->data_size)
1413 break;
1414 memcpy(pkt.data + pos,
1415 p + (j * nchunks + l) * chunk_len,
1416 chunk_len);
1417 pos += chunk_len;
bb270c08 1418 }
bb270c08 1419 }
b08569a2
AH
1420 p += asf_st->span * pkt_len;
1421 if (p > asf_pkt->avpkt.data + asf_pkt->data_size)
1422 break;
de6d9b64 1423 }
ce70f28a 1424 av_packet_unref(&asf_pkt->avpkt);
b08569a2
AH
1425 asf_pkt->avpkt = pkt;
1426
de6d9b64
FB
1427 return 0;
1428}
1429
f27544fd
RB
1430static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
1431{
1432 ASFContext *asf = s->priv_data;
b08569a2
AH
1433 AVIOContext *pb = s->pb;
1434 int ret, i;
f27544fd 1435
b08569a2
AH
1436 if ((avio_tell(pb) >= asf->data_offset + asf->data_size) &&
1437 !(asf->b_flags & ASF_FLAG_BROADCAST))
1438 return AVERROR_EOF;
1439 while (!pb->eof_reached) {
1440 if (asf->state == PARSE_PACKET_HEADER) {
1441 asf_read_packet_header(s);
a32dbf2a
AC
1442 if (pb->eof_reached)
1443 break;
b08569a2
AH
1444 if (!asf->nb_mult_left)
1445 asf->state = READ_SINGLE;
1446 else
1447 asf->state = READ_MULTI;
1448 }
8375dc1d
AH
1449 ret = asf_read_payload(s, pkt);
1450 if (ret == AVERROR(EAGAIN)) {
1451 asf->state = PARSE_PACKET_HEADER;
1452 continue;
1453 }
1454 else if (ret < 0)
f27544fd 1455 return ret;
8375dc1d 1456
b08569a2
AH
1457 switch (asf->state) {
1458 case READ_SINGLE:
1459 if (!asf->sub_left)
1460 asf->state = PARSE_PACKET_HEADER;
1461 break;
1462 case READ_MULTI_SUB:
1463 if (!asf->sub_left && !asf->nb_mult_left) {
1464 asf->state = PARSE_PACKET_HEADER;
00cc10ae
AH
1465 if (!asf->return_subpayload &&
1466 (avio_tell(pb) <= asf->packet_offset +
1467 asf->packet_size - asf->pad_len))
b08569a2
AH
1468 avio_skip(pb, asf->pad_len); // skip padding
1469 if (asf->packet_offset + asf->packet_size > avio_tell(pb))
1470 avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1471 } else if (!asf->sub_left)
1472 asf->state = READ_MULTI;
1473 break;
1474 case READ_MULTI:
1475 if (!asf->nb_mult_left) {
1476 asf->state = PARSE_PACKET_HEADER;
00cc10ae
AH
1477 if (!asf->return_subpayload &&
1478 (avio_tell(pb) <= asf->packet_offset +
1479 asf->packet_size - asf->pad_len))
b08569a2 1480 avio_skip(pb, asf->pad_len); // skip padding
b08569a2
AH
1481 if (asf->packet_offset + asf->packet_size > avio_tell(pb))
1482 avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1483 }
1484 break;
1485 }
1486 if (asf->return_subpayload) {
1487 asf->return_subpayload = 0;
1488 return 0;
1489 }
8e67039c 1490 for (i = 0; i < asf->nb_streams; i++) {
b08569a2
AH
1491 ASFPacket *asf_pkt = &asf->asf_st[i]->pkt;
1492 if (asf_pkt && !asf_pkt->size_left && asf_pkt->data_size) {
1493 if (asf->asf_st[i]->span > 1 &&
1494 asf->asf_st[i]->type == AVMEDIA_TYPE_AUDIO)
1495 if ((ret = asf_deinterleave(s, asf_pkt, i)) < 0)
1496 return ret;
1497 av_packet_move_ref(pkt, &asf_pkt->avpkt);
1498 pkt->stream_index = asf->asf_st[i]->index;
1499 pkt->flags = asf_pkt->flags;
1500 pkt->dts = asf_pkt->dts - asf->preroll;
1501 asf_pkt->data_size = 0;
1502 asf_pkt->frame_num = 0;
1503 return 0;
1504 }
1505 }
f27544fd 1506 }
b08569a2
AH
1507
1508 if (pb->eof_reached)
1509 return AVERROR_EOF;
1510
1511 return 0;
f27544fd
RB
1512}
1513
b08569a2 1514static int asf_read_close(AVFormatContext *s)
38376f00
KK
1515{
1516 ASFContext *asf = s->priv_data;
82b9e4a2 1517 int i;
38376f00 1518
cd4d9df2 1519 for (i = 0; i < ASF_MAX_STREAMS; i++) {
b08569a2 1520 av_dict_free(&asf->asf_sd[i].asf_met);
cd4d9df2 1521 if (i < asf->nb_streams) {
ce70f28a 1522 av_packet_unref(&asf->asf_st[i]->pkt.avpkt);
cd4d9df2
AH
1523 av_freep(&asf->asf_st[i]);
1524 }
82b9e4a2 1525 }
b08569a2 1526
ee80f834 1527 asf->nb_streams = 0;
b08569a2 1528 return 0;
38376f00
KK
1529}
1530
b08569a2 1531static void reset_packet_state(AVFormatContext *s)
5d25d2a9 1532{
b08569a2
AH
1533 ASFContext *asf = s->priv_data;
1534 int i;
0b5ae1fd 1535
b08569a2
AH
1536 asf->state = PARSE_PACKET_HEADER;
1537 asf->offset = 0;
1538 asf->return_subpayload = 0;
1539 asf->sub_left = 0;
1540 asf->sub_header_offset = 0;
1541 asf->packet_offset = asf->first_packet_offset;
1542 asf->pad_len = 0;
1543 asf->rep_data_len = 0;
1544 asf->dts_delta = 0;
1545 asf->mult_sub_len = 0;
1546 asf->nb_mult_left = 0;
1547 asf->nb_sub = 0;
1548 asf->prop_flags = 0;
1549 asf->sub_dts = 0;
b08569a2
AH
1550 for (i = 0; i < asf->nb_streams; i++) {
1551 ASFPacket *pkt = &asf->asf_st[i]->pkt;
1552 pkt->size_left = 0;
1553 pkt->data_size = 0;
1554 pkt->duration = 0;
1555 pkt->flags = 0;
1556 pkt->dts = 0;
1557 pkt->duration = 0;
ce70f28a 1558 av_packet_unref(&pkt->avpkt);
b08569a2
AH
1559 av_init_packet(&pkt->avpkt);
1560 }
5d25d2a9
DB
1561}
1562
b08569a2
AH
1563/*
1564 * Find a timestamp for the requested position within the payload
1565 * where the pos (position) is the offset inside the Data Object.
1566 * When position is not on the packet boundary, asf_read_timestamp tries
1567 * to find the closest packet offset after this position. If this packet
1568 * is a key frame, this packet timestamp is read and an index entry is created
1569 * for the packet. If this packet belongs to the requested stream,
1570 * asf_read_timestamp upgrades pos to the packet beginning offset and
1571 * returns this packet's dts. So returned dts is the dts of the first key frame with
1572 * matching stream number after given position.
1573 */
1574static int64_t asf_read_timestamp(AVFormatContext *s, int stream_index,
1575 int64_t *pos, int64_t pos_limit)
38376f00 1576{
b08569a2
AH
1577 ASFContext *asf = s->priv_data;
1578 int64_t pkt_pos = *pos, pkt_offset, dts = AV_NOPTS_VALUE, data_end;
1579 AVPacket pkt;
1580 int n;
115329f1 1581
b08569a2 1582 data_end = asf->data_offset + asf->data_size;
580fb5e7 1583
b08569a2
AH
1584 n = (pkt_pos - asf->first_packet_offset + asf->packet_size - 1) /
1585 asf->packet_size;
1586 n = av_clip(n, 0, ((data_end - asf->first_packet_offset) / asf->packet_size - 1));
1587 pkt_pos = asf->first_packet_offset + n * asf->packet_size;
3e9245a9 1588
b08569a2
AH
1589 avio_seek(s->pb, pkt_pos, SEEK_SET);
1590 pkt_offset = pkt_pos;
3e9245a9 1591
b08569a2
AH
1592 reset_packet_state(s);
1593 while (avio_tell(s->pb) < data_end) {
09646bab 1594
b08569a2 1595 int i, ret, st_found;
115329f1 1596
b08569a2
AH
1597 av_init_packet(&pkt);
1598 pkt_offset = avio_tell(s->pb);
1599 if ((ret = asf_read_packet(s, &pkt)) < 0) {
1600 dts = AV_NOPTS_VALUE;
1601 return ret;
1602 }
1603 // ASFPacket may contain fragments of packets belonging to different streams,
1604 // pkt_offset is the offset of the first fragment within it.
1605 if ((pkt_offset >= (pkt_pos + asf->packet_size)))
1606 pkt_pos += asf->packet_size;
1607 for (i = 0; i < asf->nb_streams; i++) {
1608 ASFStream *st = asf->asf_st[i];
1609
1610 st_found = 0;
1611 if (pkt.flags & AV_PKT_FLAG_KEY) {
1612 dts = pkt.dts;
1613 if (dts) {
1614 av_add_index_entry(s->streams[pkt.stream_index], pkt_pos,
1615 dts, pkt.size, 0, AVINDEX_KEYFRAME);
1616 if (stream_index == st->index) {
1617 st_found = 1;
1618 break;
1619 }
1620 }
1621 }
3e9245a9 1622 }
b08569a2
AH
1623 if (st_found)
1624 break;
ce70f28a 1625 av_packet_unref(&pkt);
3e9245a9 1626 }
b08569a2 1627 *pos = pkt_pos;
3e9245a9 1628
ce70f28a 1629 av_packet_unref(&pkt);
b08569a2 1630 return dts;
38376f00
KK
1631}
1632
b08569a2
AH
1633static int asf_read_seek(AVFormatContext *s, int stream_index,
1634 int64_t timestamp, int flags)
56c96a22 1635{
b08569a2
AH
1636 ASFContext *asf = s->priv_data;
1637 int idx, ret;
1638
1639 if (s->streams[stream_index]->nb_index_entries && asf->is_simple_index) {
1640 idx = av_index_search_timestamp(s->streams[stream_index], timestamp, flags);
1641 if (idx < 0 || idx >= s->streams[stream_index]->nb_index_entries)
1642 return AVERROR_INVALIDDATA;
1643 avio_seek(s->pb, s->streams[stream_index]->index_entries[idx].pos, SEEK_SET);
1644 } else {
1645 if ((ret = ff_seek_frame_binary(s, stream_index, timestamp, flags)) < 0)
1646 return ret;
77b7c334
SG
1647 }
1648
79626865
HL
1649 reset_packet_state(s);
1650
b08569a2
AH
1651 return 0;
1652}
1653
1654static const GUIDParseTable *find_guid(ff_asf_guid guid)
1655{
1656 int j, ret;
1657 const GUIDParseTable *g;
1658
1659 swap_guid(guid);
1660 g = gdef;
1661 for (j = 0; j < FF_ARRAY_ELEMS(gdef); j++) {
1662 if (!(ret = memcmp(guid, g->guid, sizeof(g->guid))))
1663 return g;
1664 g++;
56c96a22 1665 }
b08569a2
AH
1666
1667 return NULL;
56c96a22
MN
1668}
1669
b08569a2 1670static int detect_unknown_subobject(AVFormatContext *s, int64_t offset, int64_t size)
2a10020b 1671{
afda223c 1672 ASFContext *asf = s->priv_data;
b08569a2
AH
1673 AVIOContext *pb = s->pb;
1674 const GUIDParseTable *g = NULL;
1675 ff_asf_guid guid;
1676 int ret;
1677
1678 while (avio_tell(pb) <= offset + size) {
016cac75
AH
1679 if (avio_tell(pb) == asf->offset)
1680 break;
b08569a2
AH
1681 asf->offset = avio_tell(pb);
1682 if ((ret = ff_get_guid(pb, &guid)) < 0)
cd31f8dc 1683 return ret;
b08569a2
AH
1684 g = find_guid(guid);
1685 if (g) {
1686 if ((ret = g->read_object(s, g)) < 0)
1687 return ret;
1688 } else {
1689 GUIDParseTable g2;
1690
1691 g2.name = "Unknown";
1692 g2.is_subobject = 1;
1693 asf_read_unknown(s, &g2);
1694 }
fa13095a
BA
1695 }
1696
b08569a2
AH
1697 return 0;
1698}
1699
1700static int asf_read_header(AVFormatContext *s)
1701{
1702 ASFContext *asf = s->priv_data;
1703 AVIOContext *pb = s->pb;
1704 const GUIDParseTable *g = NULL;
1705 ff_asf_guid guid;
1706 int i, ret;
1707 uint64_t size;
1708
1709 asf->preroll = 0;
1710 asf->is_simple_index = 0;
1711 ff_get_guid(pb, &guid);
1712 if (ff_guidcmp(&guid, &ff_asf_header))
1713 return AVERROR_INVALIDDATA;
1714 avio_skip(pb, 8); // skip header object size
1715 avio_skip(pb, 6); // skip number of header objects and 2 reserved bytes
1716 asf->data_reached = 0;
1717
1718 /* 1 is here instead of pb->eof_reached because (when not streaming), Data are skipped
1719 * for the first time,
1720 * Index object is processed and got eof and then seeking back to the Data is performed.
1721 */
1722 while (1) {
1723 // for the cases when object size is invalid
5655236a 1724 if (avio_tell(pb) == asf->offset)
b08569a2 1725 break;
b08569a2
AH
1726 asf->offset = avio_tell(pb);
1727 if ((ret = ff_get_guid(pb, &guid)) < 0) {
5655236a 1728 if (ret == AVERROR_EOF && asf->data_reached)
b08569a2 1729 break;
5655236a 1730 else
93f16f33 1731 goto failed;
b08569a2
AH
1732 }
1733 g = find_guid(guid);
1734 if (g) {
1735 asf->unknown_offset = asf->offset;
1736 asf->is_header = 1;
1737 if ((ret = g->read_object(s, g)) < 0)
93f16f33 1738 goto failed;
b08569a2
AH
1739 } else {
1740 size = avio_rl64(pb);
1741 align_position(pb, asf->offset, size);
1742 }
83548fe8
AK
1743 if (asf->data_reached &&
1744 (!(pb->seekable & AVIO_SEEKABLE_NORMAL) ||
1745 (asf->b_flags & ASF_FLAG_BROADCAST)))
b08569a2 1746 break;
0c082565
AK
1747 }
1748
5655236a
AH
1749 if (!asf->data_reached) {
1750 av_log(s, AV_LOG_ERROR, "Data Object was not found.\n");
93f16f33
AH
1751 ret = AVERROR_INVALIDDATA;
1752 goto failed;
5655236a 1753 }
83548fe8 1754 if (pb->seekable & AVIO_SEEKABLE_NORMAL)
5655236a
AH
1755 avio_seek(pb, asf->first_packet_offset, SEEK_SET);
1756
b08569a2
AH
1757 for (i = 0; i < asf->nb_streams; i++) {
1758 const char *rfc1766 = asf->asf_sd[asf->asf_st[i]->lang_idx].langs;
1759 AVStream *st = s->streams[asf->asf_st[i]->index];
1760 set_language(s, rfc1766, &st->metadata);
1761 }
56c96a22 1762
b08569a2
AH
1763 for (i = 0; i < ASF_MAX_STREAMS; i++) {
1764 AVStream *st = NULL;
56c96a22 1765
b08569a2
AH
1766 st = find_stream(s, i);
1767 if (st) {
1768 av_dict_copy(&st->metadata, asf->asf_sd[i].asf_met, AV_DICT_IGNORE_SUFFIX);
1769 if (asf->asf_sd[i].aspect_ratio.num > 0 && asf->asf_sd[i].aspect_ratio.den > 0) {
1770 st->sample_aspect_ratio.num = asf->asf_sd[i].aspect_ratio.num;
1771 st->sample_aspect_ratio.den = asf->asf_sd[i].aspect_ratio.den;
1772 }
c7bd5eda 1773 }
56c96a22 1774 }
b08569a2 1775
38376f00 1776 return 0;
93f16f33
AH
1777
1778failed:
1779 asf_read_close(s);
1780 return ret;
2a10020b
ZK
1781}
1782
c6610a21 1783AVInputFormat ff_asf_demuxer = {
dfc2c4d9 1784 .name = "asf",
0177b7d2 1785 .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
dfc2c4d9
AK
1786 .priv_data_size = sizeof(ASFContext),
1787 .read_probe = asf_probe,
1788 .read_header = asf_read_header,
1789 .read_packet = asf_read_packet,
1790 .read_close = asf_read_close,
b08569a2 1791 .read_timestamp = asf_read_timestamp,
dfc2c4d9 1792 .read_seek = asf_read_seek,
20234a4b 1793 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH,
c9a65ca8 1794};