FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
asfdec_o.c
Go to the documentation of this file.
1 /*
2  * Microsoft Advanced Streaming Format demuxer
3  * Copyright (c) 2014 Alexandra Hájková
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/attributes.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/bswap.h"
25 #include "libavutil/common.h"
26 #include "libavutil/dict.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/opt.h"
31 
32 #include "avformat.h"
33 #include "avio_internal.h"
34 #include "avlanguage.h"
35 #include "id3v2.h"
36 #include "internal.h"
37 #include "riff.h"
38 #include "asf.h"
39 #include "asfcrypt.h"
40 
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 
58 typedef struct GUIDParseTable {
59  const char *name;
64 
65 typedef struct ASFPacket {
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;
74 } ASFPacket;
75 
76 typedef 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;
84  int16_t lang_idx;
86 } ASFStream;
87 
88 typedef struct ASFStreamData{
89  char langs[32];
90  AVDictionary *asf_met; // for storing per-stream metadata
93 
94 typedef struct ASFContext {
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
121 
122  int stream_index; // from packet header, for the subpayload case
123 
124  // packet parameters
125  uint64_t sub_header_offset; // offset of subpayload header
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
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
135  unsigned int nb_sub; // number of subpayloads read so far from the current ASF packet
136  uint16_t mult_sub_len; // total length of subpayloads array inside multiple payload
137  uint64_t nb_mult_left; // multiple payloads left
139  enum {
144  } state;
145 } ASFContext;
146 
147 static int detect_unknown_subobject(AVFormatContext *s, int64_t offset, int64_t size);
148 static const GUIDParseTable *find_guid(ff_asf_guid guid);
149 
150 static int asf_probe(AVProbeData *pd)
151 {
152  /* check file header */
153  if (!ff_guidcmp(pd->buf, &ff_asf_header))
154  return AVPROBE_SCORE_MAX/2;
155  else
156  return 0;
157 }
158 
159 static 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 }
166 
167 static void align_position(AVIOContext *pb, int64_t offset, uint64_t size)
168 {
169  if (size < INT64_MAX - offset && avio_tell(pb) != offset + size)
170  avio_seek(pb, offset + size, SEEK_SET);
171 }
172 
174 {
175  ASFContext *asf = s->priv_data;
176  AVIOContext *pb = s->pb;
177  uint64_t size = avio_rl64(pb);
178  int ret;
179 
180  if (size > INT64_MAX)
181  return AVERROR_INVALIDDATA;
182 
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;
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  }
197  avio_skip(pb, size - 24);
198  }
199 
200  return 0;
201 }
202 
203 static 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;
216 
217  return ret;
218 }
219 
221 {
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,
253  }
254  align_position(pb, asf->offset, size);
255 
256  return 0;
257 }
258 
259 static int asf_read_metadata(AVFormatContext *s, const char *title, uint16_t len,
260  unsigned char *ch, uint16_t buflen)
261 {
262  AVIOContext *pb = s->pb;
263 
264  avio_get_str16le(pb, len, ch, buflen);
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  }
269 
270  return 0;
271 }
272 
274  uint16_t val_len, int type, AVDictionary **met)
275 {
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];
292  if (val_len > sizeof(buf)) {
293  ret = AVERROR_INVALIDDATA;
294  goto failed;
295  }
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 
310 failed:
311  av_freep(&value);
312  return ret;
313 }
314 static int asf_read_generic_value(AVIOContext *pb, int type, uint64_t *value)
315 {
316 
317  switch (type) {
318  case ASF_BOOL:
319  *value = avio_rl16(pb);
320  break;
321  case ASF_DWORD:
322  *value = avio_rl32(pb);
323  break;
324  case ASF_QWORD:
325  *value = avio_rl64(pb);
326  break;
327  case ASF_WORD:
328  *value = avio_rl16(pb);
329  break;
330  default:
331  return AVERROR_INVALIDDATA;
332  }
333 
334  return 0;
335 }
336 
338  int type, AVDictionary **met)
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 
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;
354 }
355 
356 /* MSDN claims that this should be "compatible with the ID3 frame, APIC",
357  * but in reality this is only loosely similar */
359 {
360  ASFContext *asf = s->priv_data;
361  AVPacket pkt = { 0 };
362  const CodecMime *mime = ff_id3v2_mime_tags;
363  enum AVCodecID id = AV_CODEC_ID_NONE;
364  char mimetype[64];
365  uint8_t *desc = NULL;
366  AVStream *st = NULL;
367  int ret, type, picsize, desc_len;
368  ASFStream *asf_st;
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);
386  len -= 4;
387 
388  /* picture MIME type */
389  len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
390  while (mime->id != AV_CODEC_ID_NONE) {
391  if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
392  id = mime->id;
393  break;
394  }
395  mime++;
396  }
397  if (id == AV_CODEC_ID_NONE) {
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 
420  st = avformat_new_stream(s, NULL);
421  if (!st) {
422  ret = AVERROR(ENOMEM);
423  goto fail;
424  }
425  asf->asf_st[asf->nb_streams] = av_mallocz(sizeof(*asf_st));
426  asf_st = asf->asf_st[asf->nb_streams];
427  if (!asf_st) {
428  ret = AVERROR(ENOMEM);
429  goto fail;
430  }
431 
433  st->codecpar->codec_type = asf_st->type = AVMEDIA_TYPE_VIDEO;
434  st->codecpar->codec_id = id;
435  st->attached_pic = pkt;
436  st->attached_pic.stream_index = asf_st->index = st->index;
438 
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
445  av_freep(&desc);
446 
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");
449 
450  return 0;
451 
452 fail:
453  av_freep(&desc);
454  av_packet_unref(&pkt);
455  return ret;
456 }
457 
458 static void get_id3_tag(AVFormatContext *s, int len)
459 {
460  ID3v2ExtraMeta *id3v2_extra_meta = NULL;
461 
462  ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, len);
463  if (id3v2_extra_meta) {
464  ff_id3v2_parse_apic(s, &id3v2_extra_meta);
465  ff_id3v2_parse_chapters(s, &id3v2_extra_meta);
466  }
467  ff_id3v2_free_extra_meta(&id3v2_extra_meta);
468 }
469 
470 static int process_metadata(AVFormatContext *s, const uint8_t *name, uint16_t name_len,
471  uint16_t val_len, uint16_t type, AVDictionary **met)
472 {
473  int ret;
474  ff_asf_guid guid;
475 
476  if (val_len) {
477  switch (type) {
478  case ASF_UNICODE:
479  asf_read_value(s, name, val_len, type, met);
480  break;
481  case ASF_BYTE_ARRAY:
482  if (!strcmp(name, "WM/Picture")) // handle cover art
483  asf_read_picture(s, val_len);
484  else if (!strcmp(name, "ID3")) // handle ID3 tag
485  get_id3_tag(s, val_len);
486  else
487  asf_read_value(s, name, val_len, type, met);
488  break;
489  case ASF_GUID:
490  ff_get_guid(s->pb, &guid);
491  break;
492  default:
493  if ((ret = asf_set_metadata(s, name, type, met)) < 0)
494  return ret;
495  break;
496  }
497  }
498 
499  return 0;
500 }
501 
503 {
504  ASFContext *asf = s->priv_data;
505  AVIOContext *pb = s->pb;
506  uint64_t size = avio_rl64(pb);
507  uint16_t nb_desc = avio_rl16(pb);
508  int i, ret;
509 
510  for (i = 0; i < nb_desc; i++) {
511  uint16_t name_len, type, val_len;
512  uint8_t *name = NULL;
513 
514  name_len = avio_rl16(pb);
515  if (!name_len)
516  return AVERROR_INVALIDDATA;
517  name = av_malloc(name_len);
518  if (!name)
519  return AVERROR(ENOMEM);
520  avio_get_str16le(pb, name_len, name,
521  name_len);
522  type = avio_rl16(pb);
523  // BOOL values are 16 bits long in the Metadata Object
524  // but 32 bits long in the Extended Content Description Object
525  if (type == ASF_BOOL)
526  type = ASF_DWORD;
527  val_len = avio_rl16(pb);
528 
529  ret = process_metadata(s, name, name_len, val_len, type, &s->metadata);
530  av_freep(&name);
531  if (ret < 0)
532  return ret;
533  }
534 
535  align_position(pb, asf->offset, size);
536  return 0;
537 }
538 
539 static AVStream *find_stream(AVFormatContext *s, uint16_t st_num)
540 {
541  AVStream *st = NULL;
542  ASFContext *asf = s->priv_data;
543  int i;
544 
545  for (i = 0; i < asf->nb_streams; i++) {
546  if (asf->asf_st[i]->stream_index == st_num) {
547  st = s->streams[asf->asf_st[i]->index];
548  break;
549  }
550  }
551 
552  return st;
553 }
554 
556 {
557  ASFContext *asf = s->priv_data;
558  AVIOContext *pb = s->pb;
559  uint64_t value = 0;
560  int ret;
561 
562  ret = asf_read_generic_value(pb, type, &value);
563  if (ret < 0)
564  return ret;
565 
566  if (st_num < ASF_MAX_STREAMS) {
567  if (!strcmp(name, "AspectRatioX"))
568  asf->asf_sd[st_num].aspect_ratio.num = value;
569  else
570  asf->asf_sd[st_num].aspect_ratio.den = value;
571  }
572  return 0;
573 }
574 
576 {
577  ASFContext *asf = s->priv_data;
578  AVIOContext *pb = s->pb;
579  uint64_t size = avio_rl64(pb);
580  uint16_t nb_recs = avio_rl16(pb); // number of records in the Description Records list
581  int i, ret;
582 
583  for (i = 0; i < nb_recs; i++) {
584  uint16_t name_len, buflen, type, val_len, st_num;
585  uint8_t *name = NULL;
586 
587  avio_skip(pb, 2); // skip reserved field
588  st_num = avio_rl16(pb);
589  name_len = avio_rl16(pb);
590  buflen = 2 * name_len + 1;
591  if (!name_len)
592  break;
593  type = avio_rl16(pb);
594  val_len = avio_rl32(pb);
595  name = av_malloc(buflen);
596  if (!name)
597  return AVERROR(ENOMEM);
598  avio_get_str16le(pb, name_len, name,
599  buflen);
600  if (!strcmp(name, "AspectRatioX") || !strcmp(name, "AspectRatioY")) {
601  ret = asf_store_aspect_ratio(s, st_num, name, type);
602  if (ret < 0) {
603  av_freep(&name);
604  break;
605  }
606  } else {
607  if (st_num < ASF_MAX_STREAMS) {
608  if ((ret = process_metadata(s, name, name_len, val_len, type,
609  &asf->asf_sd[st_num].asf_met)) < 0) {
610  av_freep(&name);
611  break;
612  }
613  }
614  }
615  av_freep(&name);
616  }
617 
618  align_position(pb, asf->offset, size);
619  return 0;
620 }
621 
623 {
624  ASFContext *asf = s->priv_data;
625  AVIOContext *pb = s->pb;
626  int i;
627  static const char *const titles[] =
628  { "Title", "Author", "Copyright", "Description", "Rate" };
629  uint16_t len[5], buflen[5] = { 0 };
630  uint8_t *ch;
631  uint64_t size = avio_rl64(pb);
632 
633  for (i = 0; i < 5; i++) {
634  len[i] = avio_rl16(pb);
635  // utf8 string should be <= 2 * utf16 string, extra byte for the terminator
636  buflen[i] = 2 * len[i] + 1;
637  }
638 
639  for (i = 0; i < 5; i++) {
640  ch = av_malloc(buflen[i]);
641  if (!ch)
642  return(AVERROR(ENOMEM));
643  asf_read_metadata(s, titles[i], len[i], ch, buflen[i]);
644  av_freep(&ch);
645  }
646  align_position(pb, asf->offset, size);
647 
648  return 0;
649 }
650 
652 {
653  ASFContext *asf = s->priv_data;
654  AVIOContext *pb = s->pb;
655  time_t creation_time;
656 
657  avio_rl64(pb); // read object size
658  avio_skip(pb, 16); // skip File ID
659  avio_skip(pb, 8); // skip File size
660  creation_time = avio_rl64(pb);
661  if (!(asf->b_flags & ASF_FLAG_BROADCAST)) {
662  struct tm tmbuf;
663  struct tm *tm;
664  char buf[64];
665 
666  // creation date is in 100 ns units from 1 Jan 1601, conversion to s
667  creation_time /= 10000000;
668  // there are 11644473600 seconds between 1 Jan 1601 and 1 Jan 1970
669  creation_time -= 11644473600;
670  tm = gmtime_r(&creation_time, &tmbuf);
671  if (tm) {
672  if (!strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm))
673  buf[0] = '\0';
674  } else
675  buf[0] = '\0';
676  if (buf[0]) {
677  if (av_dict_set(&s->metadata, "creation_time", buf, 0) < 0)
678  av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
679  }
680  }
681  asf->nb_packets = avio_rl64(pb);
682  asf->duration = avio_rl64(pb) / 10000; // stream duration
683  avio_skip(pb, 8); // skip send duration
684  asf->preroll = avio_rl64(pb);
685  asf->duration -= asf->preroll;
686  asf->b_flags = avio_rl32(pb);
687  avio_skip(pb, 4); // skip minimal packet size
688  asf->packet_size = avio_rl32(pb);
689  avio_skip(pb, 4); // skip max_bitrate
690 
691  return 0;
692 }
693 
695 {
696  uint16_t size_asf; // ASF-specific Format Data size
697  uint32_t size_bmp; // BMP_HEADER-specific Format Data size
698  unsigned int tag;
699 
700  st->codecpar->width = avio_rl32(pb);
701  st->codecpar->height = avio_rl32(pb);
702  avio_skip(pb, 1); // skip reserved flags
703  size_asf = avio_rl16(pb);
704  tag = ff_get_bmp_header(pb, st, &size_bmp);
705  st->codecpar->codec_tag = tag;
707  size_bmp = FFMAX(size_asf, size_bmp);
708 
709  if (size_bmp > BMP_HEADER_SIZE &&
710  size_bmp < INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
711  int ret;
712  st->codecpar->extradata_size = size_bmp - BMP_HEADER_SIZE;
715  st->codecpar->extradata_size = 0;
716  return AVERROR(ENOMEM);
717  }
718  memset(st->codecpar->extradata + st->codecpar->extradata_size , 0,
720  if ((ret = avio_read(pb, st->codecpar->extradata,
721  st->codecpar->extradata_size)) < 0)
722  return ret;
723  }
724  return 0;
725 }
726 
728 {
729  ASFContext *asf = s->priv_data;
730  AVIOContext *pb = s->pb;
731  uint64_t size;
732  uint32_t err_data_len, ts_data_len; // type specific data length
733  uint16_t flags;
734  ff_asf_guid stream_type;
735  enum AVMediaType type;
736  int i, ret;
737  uint8_t stream_index;
738  AVStream *st;
739  ASFStream *asf_st;
740 
741  // ASF file must not contain more than 128 streams according to the specification
742  if (asf->nb_streams >= ASF_MAX_STREAMS)
743  return AVERROR_INVALIDDATA;
744 
745  size = avio_rl64(pb);
746  ff_get_guid(pb, &stream_type);
747  if (!ff_guidcmp(&stream_type, &ff_asf_audio_stream))
748  type = AVMEDIA_TYPE_AUDIO;
749  else if (!ff_guidcmp(&stream_type, &ff_asf_video_stream))
750  type = AVMEDIA_TYPE_VIDEO;
751  else if (!ff_guidcmp(&stream_type, &ff_asf_jfif_media))
752  type = AVMEDIA_TYPE_VIDEO;
753  else if (!ff_guidcmp(&stream_type, &ff_asf_command_stream))
754  type = AVMEDIA_TYPE_DATA;
755  else if (!ff_guidcmp(&stream_type,
757  type = AVMEDIA_TYPE_UNKNOWN;
758  else
759  return AVERROR_INVALIDDATA;
760 
761  ff_get_guid(pb, &stream_type); // error correction type
762  avio_skip(pb, 8); // skip the time offset
763  ts_data_len = avio_rl32(pb);
764  err_data_len = avio_rl32(pb);
765  flags = avio_rl16(pb); // bit 15 - Encrypted Content
766 
767  stream_index = flags & ASF_STREAM_NUM;
768  for (i = 0; i < asf->nb_streams; i++)
769  if (stream_index == asf->asf_st[i]->stream_index) {
771  "Duplicate stream found, this stream will be ignored.\n");
772  align_position(pb, asf->offset, size);
773  return 0;
774  }
775 
776  st = avformat_new_stream(s, NULL);
777  if (!st)
778  return AVERROR(ENOMEM);
779  avpriv_set_pts_info(st, 32, 1, 1000); // pts should be dword, in milliseconds
780  st->codecpar->codec_type = type;
781  asf->asf_st[asf->nb_streams] = av_mallocz(sizeof(*asf_st));
782  if (!asf->asf_st[asf->nb_streams])
783  return AVERROR(ENOMEM);
784  asf_st = asf->asf_st[asf->nb_streams];
785  asf->nb_streams++;
786  asf_st->stream_index = stream_index;
787  asf_st->index = st->index;
788  asf_st->indexed = 0;
789  st->id = flags & ASF_STREAM_NUM;
790  av_init_packet(&asf_st->pkt.avpkt);
791  asf_st->pkt.data_size = 0;
792  avio_skip(pb, 4); // skip reserved field
793 
794  switch (type) {
795  case AVMEDIA_TYPE_AUDIO:
796  asf_st->type = AVMEDIA_TYPE_AUDIO;
797  if ((ret = ff_get_wav_header(s, pb, st->codecpar, ts_data_len, 0)) < 0)
798  return ret;
799  break;
800  case AVMEDIA_TYPE_VIDEO:
801  asf_st->type = AVMEDIA_TYPE_VIDEO;
802  if ((ret = parse_video_info(pb, st)) < 0)
803  return ret;
804  break;
805  default:
806  avio_skip(pb, ts_data_len);
807  break;
808  }
809 
810  if (err_data_len) {
811  if (type == AVMEDIA_TYPE_AUDIO) {
812  uint8_t span = avio_r8(pb);
813  if (span > 1) {
814  asf_st->span = span;
815  asf_st->virtual_pkt_len = avio_rl16(pb);
816  asf_st->virtual_chunk_len = avio_rl16(pb);
817  if (!asf_st->virtual_chunk_len || !asf_st->virtual_pkt_len)
818  return AVERROR_INVALIDDATA;
819  avio_skip(pb, err_data_len - 5);
820  } else
821  avio_skip(pb, err_data_len - 1);
822  } else
823  avio_skip(pb, err_data_len);
824  }
825 
826  align_position(pb, asf->offset, size);
827 
828  return 0;
829 }
830 
831 static void set_language(AVFormatContext *s, const char *rfc1766, AVDictionary **met)
832 {
833  // language abbr should contain at least 2 chars
834  if (rfc1766 && strlen(rfc1766) > 1) {
835  const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
836  const char *iso6392 = ff_convert_lang_to(primary_tag,
838  if (iso6392)
839  if (av_dict_set(met, "language", iso6392, 0) < 0)
840  av_log(s, AV_LOG_WARNING, "av_dict_set failed.\n");
841  }
842 }
843 
845 {
846  ASFContext *asf = s->priv_data;
847  AVIOContext *pb = s->pb;
848  AVStream *st = NULL;
849  ff_asf_guid guid;
850  uint16_t nb_st_name, nb_pay_exts, st_num, lang_idx;
851  int i, ret;
852  uint32_t bitrate;
853  uint64_t start_time, end_time, time_per_frame;
854  uint64_t size = avio_rl64(pb);
855 
856  start_time = avio_rl64(pb);
857  end_time = avio_rl64(pb);
858  bitrate = avio_rl32(pb);
859  avio_skip(pb, 28); // skip some unused values
860  st_num = avio_rl16(pb);
861  st_num &= ASF_STREAM_NUM;
862  lang_idx = avio_rl16(pb); // Stream Language ID Index
863  for (i = 0; i < asf->nb_streams; i++) {
864  if (st_num == asf->asf_st[i]->stream_index) {
865  st = s->streams[asf->asf_st[i]->index];
866  asf->asf_st[i]->lang_idx = lang_idx;
867  break;
868  }
869  }
870  time_per_frame = avio_rl64(pb); // average time per frame
871  if (st) {
872  st->start_time = start_time;
873  st->duration = end_time - start_time;
874  st->codecpar->bit_rate = bitrate;
875  st->avg_frame_rate.num = 10000000;
876  st->avg_frame_rate.den = time_per_frame;
877  }
878  nb_st_name = avio_rl16(pb);
879  nb_pay_exts = avio_rl16(pb);
880  for (i = 0; i < nb_st_name; i++) {
881  uint16_t len;
882 
883  avio_rl16(pb); // Language ID Index
884  len = avio_rl16(pb);
885  avio_skip(pb, len);
886  }
887 
888  for (i = 0; i < nb_pay_exts; i++) {
889  uint32_t len;
890  avio_skip(pb, 16); // Extension System ID
891  avio_skip(pb, 2); // Extension Data Size
892  len = avio_rl32(pb);
893  avio_skip(pb, len);
894  }
895 
896  if ((ret = ff_get_guid(pb, &guid)) < 0) {
897  align_position(pb, asf->offset, size);
898 
899  return 0;
900  }
901 
902  g = find_guid(guid);
903  if (g && !(strcmp(g->name, "Stream Properties"))) {
904  if ((ret = g->read_object(s, g)) < 0)
905  return ret;
906  }
907 
908  align_position(pb, asf->offset, size);
909  return 0;
910 }
911 
913 {
914  ASFContext *asf = s->priv_data;
915  AVIOContext *pb = s->pb;
916  int i, ret;
917  uint64_t size = avio_rl64(pb);
918  uint16_t nb_langs = avio_rl16(pb);
919 
920  if (nb_langs < ASF_MAX_STREAMS) {
921  for (i = 0; i < nb_langs; i++) {
922  size_t len;
923  len = avio_r8(pb);
924  if (!len)
925  len = 6;
926  if ((ret = get_asf_string(pb, len, asf->asf_sd[i].langs,
927  sizeof(asf->asf_sd[i].langs))) < 0) {
928  return ret;
929  }
930  }
931  }
932 
933  align_position(pb, asf->offset, size);
934  return 0;
935 }
936 
937 // returns data object offset when reading this object for the first time
939 {
940  ASFContext *asf = s->priv_data;
941  AVIOContext *pb = s->pb;
942  uint64_t size = asf->data_size = avio_rl64(pb);
943  int i;
944 
945  if (!asf->data_reached) {
946  asf->data_reached = 1;
947  asf->data_offset = asf->offset;
948  }
949 
950  for (i = 0; i < asf->nb_streams; i++) {
951  if (!(asf->b_flags & ASF_FLAG_BROADCAST))
952  s->streams[i]->duration = asf->duration;
953  }
954  asf->nb_mult_left = 0;
955  asf->sub_left = 0;
956  asf->state = PARSE_PACKET_HEADER;
957  asf->return_subpayload = 0;
958  asf->packet_size_internal = 0;
959  avio_skip(pb, 16); // skip File ID
960  size = avio_rl64(pb); // Total Data Packets
961  if (size != asf->nb_packets)
963  "Number of Packets from File Properties Object is not equal to Total"
964  "Datapackets value! num of packets %"PRIu64" total num %"PRIu64".\n",
965  size, asf->nb_packets);
966  avio_skip(pb, 2); // skip reserved field
967  asf->first_packet_offset = avio_tell(pb);
968  if ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !(asf->b_flags & ASF_FLAG_BROADCAST))
969  align_position(pb, asf->offset, asf->data_size);
970 
971  return 0;
972 }
973 
975 {
976  ASFContext *asf = s->priv_data;
977  AVIOContext *pb = s->pb;
978  AVStream *st = NULL;
979  uint64_t interval; // index entry time interval in 100 ns units, usually it's 1s
980  uint32_t pkt_num, nb_entries;
981  int32_t prev_pkt_num = -1;
982  int i;
983  int64_t offset;
984  uint64_t size = avio_rl64(pb);
985 
986  // simple index objects should be ordered by stream number, this loop tries to find
987  // the first not indexed video stream
988  for (i = 0; i < asf->nb_streams; i++) {
989  if ((asf->asf_st[i]->type == AVMEDIA_TYPE_VIDEO) && !asf->asf_st[i]->indexed) {
990  asf->asf_st[i]->indexed = 1;
991  st = s->streams[asf->asf_st[i]->index];
992  break;
993  }
994  }
995  if (!st) {
996  avio_skip(pb, size - 24); // if there's no video stream, skip index object
997  return 0;
998  }
999  avio_skip(pb, 16); // skip File ID
1000  interval = avio_rl64(pb);
1001  avio_skip(pb, 4);
1002  nb_entries = avio_rl32(pb);
1003  for (i = 0; i < nb_entries; i++) {
1004  pkt_num = avio_rl32(pb);
1005  offset = avio_skip(pb, 2);
1006  if (offset < 0) {
1007  av_log(s, AV_LOG_ERROR, "Skipping failed in asf_read_simple_index.\n");
1008  return offset;
1009  }
1010  if (prev_pkt_num != pkt_num) {
1012  pkt_num, av_rescale(interval, i, 10000),
1013  asf->packet_size, 0, AVINDEX_KEYFRAME);
1014  prev_pkt_num = pkt_num;
1015  }
1016  }
1017  asf->is_simple_index = 1;
1018  align_position(pb, asf->offset, size);
1019 
1020  return 0;
1021 }
1022 
1023 static const GUIDParseTable gdef[] = {
1024  { "Data", { 0x75, 0xB2, 0x26, 0x36, 0x66, 0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_data, 1 },
1025  { "Simple Index", { 0x33, 0x00, 0x08, 0x90, 0xE5, 0xB1, 0x11, 0xCF, 0x89, 0xF4, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xCB }, asf_read_simple_index, 1 },
1026  { "Content Description", { 0x75, 0xB2, 0x26, 0x33, 0x66 ,0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_content_desc, 1 },
1027  { "Extended Content Description", { 0xD2, 0xD0, 0xA4, 0x40, 0xE3, 0x07, 0x11, 0xD2, 0x97, 0xF0, 0x00, 0xA0, 0xC9, 0x5e, 0xA8, 0x50 }, asf_read_ext_content, 1 },
1028  { "Stream Bitrate Properties", { 0x7B, 0xF8, 0x75, 0xCE, 0x46, 0x8D, 0x11, 0xD1, 0x8D, 0x82, 0x00, 0x60, 0x97, 0xC9, 0xA2, 0xB2 }, asf_read_unknown, 1 },
1029  { "File Properties", { 0x8C, 0xAB, 0xDC, 0xA1, 0xA9, 0x47, 0x11, 0xCF, 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_properties, 1 },
1030  { "Header Extension", { 0x5F, 0xBF, 0x03, 0xB5, 0xA9, 0x2E, 0x11, 0xCF, 0x8E, 0xE3, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_unknown, 0 },
1031  { "Stream Properties", { 0xB7, 0xDC, 0x07, 0x91, 0xA9, 0xB7, 0x11, 0xCF, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_stream_properties, 1 },
1032  { "Codec List", { 0x86, 0xD1, 0x52, 0x40, 0x31, 0x1D, 0x11, 0xD0, 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 }, asf_read_unknown, 1 },
1033  { "Marker", { 0xF4, 0x87, 0xCD, 0x01, 0xA9, 0x51, 0x11, 0xCF, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 }, asf_read_marker, 1 },
1034  { "Script Command", { 0x1E, 0xFB, 0x1A, 0x30, 0x0B, 0x62, 0x11, 0xD0, 0xA3, 0x9B, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 }, asf_read_unknown, 1 },
1035  { "Language List", { 0x7C, 0x43, 0x46, 0xa9, 0xef, 0xe0, 0x4B, 0xFC, 0xB2, 0x29, 0x39, 0x3e, 0xde, 0x41, 0x5c, 0x85 }, asf_read_language_list, 1},
1036  { "Padding", { 0x18, 0x06, 0xD4, 0x74, 0xCA, 0xDF, 0x45, 0x09, 0xA4, 0xBA, 0x9A, 0xAB, 0xCB, 0x96, 0xAA, 0xE8 }, asf_read_unknown, 1 },
1037  { "DRMv1 Header", { 0x22, 0x11, 0xB3, 0xFB, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1038  { "DRMv2 Header", { 0x29, 0x8A, 0xE6, 0x14, 0x26, 0x22, 0x4C, 0x17, 0xB9, 0x35, 0xDA, 0xE0, 0x7E, 0xE9, 0x28, 0x9c }, asf_read_unknown, 1 },
1039  { "Index", { 0xD6, 0xE2, 0x29, 0xD3, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1040  { "Media Object Index", { 0xFE, 0xB1, 0x03, 0xF8, 0x12, 0xAD, 0x4C, 0x64, 0x84, 0x0F, 0x2A, 0x1D, 0x2F, 0x7A, 0xD4, 0x8C }, asf_read_unknown, 1 },
1041  { "Timecode Index", { 0x3C, 0xB7, 0x3F, 0xD0, 0x0C, 0x4A, 0x48, 0x03, 0x95, 0x3D, 0xED, 0xF7, 0xB6, 0x22, 0x8F, 0x0C }, asf_read_unknown, 0 },
1042  { "Bitrate_Mutual_Exclusion", { 0xD6, 0xE2, 0x29, 0xDC, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1043  { "Error Correction", { 0x75, 0xB2, 0x26, 0x35, 0x66, 0x8E, 0x11, 0xCF, 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C }, asf_read_unknown, 1 },
1044  { "Content Branding", { 0x22, 0x11, 0xB3, 0xFA, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1045  { "Content Encryption", { 0x22, 0x11, 0xB3, 0xFB, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1046  { "Extended Content Encryption", { 0x29, 0x8A, 0xE6, 0x14, 0x26, 0x22, 0x4C, 0x17, 0xB9, 0x35, 0xDA, 0xE0, 0x7E, 0xE9, 0x28, 0x9C }, asf_read_unknown, 1 },
1047  { "Digital Signature", { 0x22, 0x11, 0xB3, 0xFC, 0xBD, 0x23, 0x11, 0xD2, 0xB4, 0xB7, 0x00, 0xA0, 0xC9, 0x55, 0xFC, 0x6E }, asf_read_unknown, 1 },
1048  { "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 },
1049  { "Advanced Mutual Exclusion", { 0xA0, 0x86, 0x49, 0xCF, 0x47, 0x75, 0x46, 0x70, 0x8A, 0x16, 0x6E, 0x35, 0x35, 0x75, 0x66, 0xCD }, asf_read_unknown, 1 },
1050  { "Group Mutual Exclusion", { 0xD1, 0x46, 0x5A, 0x40, 0x5A, 0x79, 0x43, 0x38, 0xB7, 0x1B, 0xE3, 0x6B, 0x8F, 0xD6, 0xC2, 0x49 }, asf_read_unknown, 1},
1051  { "Stream Prioritization", { 0xD4, 0xFE, 0xD1, 0x5B, 0x88, 0xD3, 0x45, 0x4F, 0x81, 0xF0, 0xED, 0x5C, 0x45, 0x99, 0x9E, 0x24 }, asf_read_unknown, 1 },
1052  { "Bandwidth Sharing Object", { 0xA6, 0x96, 0x09, 0xE6, 0x51, 0x7B, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 },
1053  { "Metadata", { 0xC5, 0xF8, 0xCB, 0xEA, 0x5B, 0xAF, 0x48, 0x77, 0x84, 0x67, 0xAA, 0x8C, 0x44, 0xFA, 0x4C, 0xCA }, asf_read_metadata_obj, 1 },
1054  { "Metadata Library", { 0x44, 0x23, 0x1C, 0x94, 0x94, 0x98, 0x49, 0xD1, 0xA1, 0x41, 0x1D, 0x13, 0x4E, 0x45, 0x70, 0x54 }, asf_read_metadata_obj, 1 },
1055  { "Audio Spread", { 0xBF, 0xC3, 0xCD, 0x50, 0x61, 0x8F, 0x11, 0xCF, 0x8B, 0xB2, 0x00, 0xAA, 0x00, 0xB4, 0xE2, 0x20 }, asf_read_unknown, 1 },
1056  { "Index Parameters", { 0xD6, 0xE2, 0x29, 0xDF, 0x35, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1057  { "Content Encryption System Windows Media DRM Network Devices",
1058  { 0x7A, 0x07, 0x9B, 0xB6, 0xDA, 0XA4, 0x4e, 0x12, 0xA5, 0xCA, 0x91, 0xD3, 0x8D, 0xC1, 0x1A, 0x8D }, asf_read_unknown, 1 },
1059  { "Mutex Language", { 0xD6, 0xE2, 0x2A, 0x00, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1060  { "Mutex Bitrate", { 0xD6, 0xE2, 0x2A, 0x01, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1061  { "Mutex Unknown", { 0xD6, 0xE2, 0x2A, 0x02, 0x25, 0xDA, 0x11, 0xD1, 0x90, 0x34, 0x00, 0xA0, 0xC9, 0x03, 0x49, 0xBE }, asf_read_unknown, 1 },
1062  { "Bandwidth Sharing Exclusive", { 0xAF, 0x60, 0x60, 0xAA, 0x51, 0x97, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 },
1063  { "Bandwidth Sharing Partial", { 0xAF, 0x60, 0x60, 0xAB, 0x51, 0x97, 0x11, 0xD2, 0xB6, 0xAF, 0x00, 0xC0, 0x4F, 0xD9, 0x08, 0xE9 }, asf_read_unknown, 1 },
1064  { "Payload Extension System Timecode", { 0x39, 0x95, 0x95, 0xEC, 0x86, 0x67, 0x4E, 0x2D, 0x8F, 0xDB, 0x98, 0x81, 0x4C, 0xE7, 0x6C, 0x1E }, asf_read_unknown, 1 },
1065  { "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 },
1066  { "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 },
1067  { "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 },
1068  { "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 },
1069  { "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 },
1070  { "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 },
1071 };
1072 
1073 #define READ_LEN(flag, name, len) \
1074  do { \
1075  if ((flag) == name ## IS_BYTE) \
1076  len = avio_r8(pb); \
1077  else if ((flag) == name ## IS_WORD) \
1078  len = avio_rl16(pb); \
1079  else if ((flag) == name ## IS_DWORD) \
1080  len = avio_rl32(pb); \
1081  else \
1082  len = 0; \
1083  } while(0)
1084 
1085 static int asf_read_subpayload(AVFormatContext *s, AVPacket *pkt, int is_header)
1086 {
1087  ASFContext *asf = s->priv_data;
1088  AVIOContext *pb = s->pb;
1089  uint8_t sub_len;
1090  int ret, i;
1091 
1092  if (is_header) {
1093  asf->dts_delta = avio_r8(pb);
1094  if (asf->nb_mult_left) {
1095  asf->mult_sub_len = avio_rl16(pb); // total
1096  }
1097  asf->sub_header_offset = avio_tell(pb);
1098  asf->nb_sub = 0;
1099  asf->sub_left = 1;
1100  }
1101  sub_len = avio_r8(pb);
1102  if ((ret = av_get_packet(pb, pkt, sub_len)) < 0) // each subpayload is entire frame
1103  return ret;
1104  for (i = 0; i < asf->nb_streams; i++) {
1105  if (asf->stream_index == asf->asf_st[i]->stream_index) {
1106  pkt->stream_index = asf->asf_st[i]->index;
1107  break;
1108  }
1109  }
1110  asf->return_subpayload = 1;
1111  if (!sub_len)
1112  asf->return_subpayload = 0;
1113 
1114  if (sub_len)
1115  asf->nb_sub++;
1116  pkt->dts = asf->sub_dts + (asf->nb_sub - 1) * asf->dts_delta - asf->preroll;
1117  if (asf->nb_mult_left && (avio_tell(pb) >=
1118  (asf->sub_header_offset + asf->mult_sub_len))) {
1119  asf->sub_left = 0;
1120  asf->nb_mult_left--;
1121  }
1122  if (avio_tell(pb) >= asf->packet_offset + asf->packet_size - asf->pad_len) {
1123  asf->sub_left = 0;
1124  if (!asf->nb_mult_left) {
1125  avio_skip(pb, asf->pad_len);
1126  if (avio_tell(pb) != asf->packet_offset + asf->packet_size) {
1127  if (!asf->packet_size)
1128  return AVERROR_INVALIDDATA;
1130  "Position %"PRId64" wrong, should be %"PRId64"\n",
1131  avio_tell(pb), asf->packet_offset + asf->packet_size);
1132  avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1133  }
1134  }
1135  }
1136 
1137  return 0;
1138 }
1139 
1140 static void reset_packet(ASFPacket *asf_pkt)
1141 {
1142  asf_pkt->size_left = 0;
1143  asf_pkt->data_size = 0;
1144  asf_pkt->duration = 0;
1145  asf_pkt->flags = 0;
1146  asf_pkt->dts = 0;
1147  asf_pkt->duration = 0;
1148  av_packet_unref(&asf_pkt->avpkt);
1149  av_init_packet(&asf_pkt->avpkt);
1150 }
1151 
1153 {
1154  ASFContext *asf = s->priv_data;
1155  AVIOContext *pb = s->pb;
1156  int ret, data_size;
1157 
1158  if (!asf_pkt->data_size) {
1159  data_size = avio_rl32(pb); // read media object size
1160  if (data_size <= 0)
1161  return AVERROR_INVALIDDATA;
1162  if ((ret = av_new_packet(&asf_pkt->avpkt, data_size)) < 0)
1163  return ret;
1164  asf_pkt->data_size = asf_pkt->size_left = data_size;
1165  } else
1166  avio_skip(pb, 4); // reading of media object size is already done
1167  asf_pkt->dts = avio_rl32(pb); // read presentation time
1168  if (asf->rep_data_len && (asf->rep_data_len >= 8))
1169  avio_skip(pb, asf->rep_data_len - 8); // skip replicated data
1170 
1171  return 0;
1172 }
1173 
1175  ASFPacket *asf_pkt)
1176 {
1177  ASFContext *asf = s->priv_data;
1178  AVIOContext *pb = s->pb;
1179  uint16_t pay_len;
1180  unsigned char *p;
1181  int ret;
1182  int skip = 0;
1183 
1184  // if replicated length is 1, subpayloads are present
1185  if (asf->rep_data_len == 1) {
1186  asf->sub_left = 1;
1187  asf->state = READ_MULTI_SUB;
1188  pkt->flags = asf_pkt->flags;
1189  if ((ret = asf_read_subpayload(s, pkt, 1)) < 0)
1190  return ret;
1191  } else {
1192  if (asf->rep_data_len)
1193  if ((ret = asf_read_replicated_data(s, asf_pkt)) < 0)
1194  return ret;
1195  pay_len = avio_rl16(pb); // payload length should be WORD
1196  if (pay_len > asf->packet_size) {
1197  av_log(s, AV_LOG_ERROR,
1198  "Error: invalid data packet size, pay_len %"PRIu16", "
1199  "asf->packet_size %"PRIu32", offset %"PRId64".\n",
1200  pay_len, asf->packet_size, avio_tell(pb));
1201  return AVERROR_INVALIDDATA;
1202  }
1203  p = asf_pkt->avpkt.data + asf_pkt->data_size - asf_pkt->size_left;
1204  if (pay_len > asf_pkt->size_left) {
1205  av_log(s, AV_LOG_ERROR,
1206  "Error: invalid buffer size, pay_len %d, data size left %d.\n",
1207  pay_len, asf_pkt->size_left);
1208  skip = pay_len - asf_pkt->size_left;
1209  pay_len = asf_pkt->size_left;
1210  }
1211  if (asf_pkt->size_left <= 0)
1212  return AVERROR_INVALIDDATA;
1213  if ((ret = avio_read(pb, p, pay_len)) < 0)
1214  return ret;
1215  if (s->key && s->keylen == 20)
1216  ff_asfcrypt_dec(s->key, p, ret);
1217  avio_skip(pb, skip);
1218  asf_pkt->size_left -= pay_len;
1219  asf->nb_mult_left--;
1220  }
1221 
1222  return 0;
1223 }
1224 
1226 {
1227  ASFContext *asf = s->priv_data;
1228  AVIOContext *pb = s->pb;
1229  int64_t offset;
1230  uint64_t size;
1231  unsigned char *p;
1232  int ret, data_size;
1233 
1234  if (!asf_pkt->data_size) {
1235  data_size = avio_rl32(pb); // read media object size
1236  if (data_size <= 0)
1237  return AVERROR_EOF;
1238  if ((ret = av_new_packet(&asf_pkt->avpkt, data_size)) < 0)
1239  return ret;
1240  asf_pkt->data_size = asf_pkt->size_left = data_size;
1241  } else
1242  avio_skip(pb, 4); // skip media object size
1243  asf_pkt->dts = avio_rl32(pb); // read presentation time
1244  if (asf->rep_data_len >= 8)
1245  avio_skip(pb, asf->rep_data_len - 8); // skip replicated data
1246  offset = avio_tell(pb);
1247 
1248  // size of the payload - size of the packet without header and padding
1249  if (asf->packet_size_internal)
1250  size = asf->packet_size_internal - offset + asf->packet_offset - asf->pad_len;
1251  else
1252  size = asf->packet_size - offset + asf->packet_offset - asf->pad_len;
1253  if (size > asf->packet_size) {
1254  av_log(s, AV_LOG_ERROR,
1255  "Error: invalid data packet size, offset %"PRId64".\n",
1256  avio_tell(pb));
1257  return AVERROR_INVALIDDATA;
1258  }
1259  p = asf_pkt->avpkt.data + asf_pkt->data_size - asf_pkt->size_left;
1260  if (size > asf_pkt->size_left || asf_pkt->size_left <= 0)
1261  return AVERROR_INVALIDDATA;
1262  if (asf_pkt->size_left > size)
1263  asf_pkt->size_left -= size;
1264  else
1265  asf_pkt->size_left = 0;
1266  if ((ret = avio_read(pb, p, size)) < 0)
1267  return ret;
1268  if (s->key && s->keylen == 20)
1269  ff_asfcrypt_dec(s->key, p, ret);
1270  if (asf->packet_size_internal)
1271  avio_skip(pb, asf->packet_size - asf->packet_size_internal);
1272  avio_skip(pb, asf->pad_len); // skip padding
1273 
1274  return 0;
1275 }
1276 
1278 {
1279  ASFContext *asf = s->priv_data;
1280  AVIOContext *pb = s->pb;
1281  int ret, i;
1282  ASFPacket *asf_pkt = NULL;
1283 
1284  if (!asf->sub_left) {
1285  uint32_t off_len, media_len;
1286  uint8_t stream_num;
1287 
1288  stream_num = avio_r8(pb);
1289  asf->stream_index = stream_num & ASF_STREAM_NUM;
1290  for (i = 0; i < asf->nb_streams; i++) {
1291  if (asf->stream_index == asf->asf_st[i]->stream_index) {
1292  asf_pkt = &asf->asf_st[i]->pkt;
1293  asf_pkt->stream_index = asf->asf_st[i]->index;
1294  break;
1295  }
1296  }
1297  if (!asf_pkt) {
1298  if (asf->packet_offset + asf->packet_size <= asf->data_offset + asf->data_size) {
1299  if (!asf->packet_size) {
1300  av_log(s, AV_LOG_ERROR, "Invalid packet size 0.\n");
1301  return AVERROR_INVALIDDATA;
1302  }
1303  avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1304  av_log(s, AV_LOG_WARNING, "Skipping the stream with the invalid stream index %d.\n",
1305  asf->stream_index);
1306  return AVERROR(EAGAIN);
1307  } else
1308  return AVERROR_INVALIDDATA;
1309  }
1310 
1311  if (stream_num >> 7)
1312  asf_pkt->flags |= AV_PKT_FLAG_KEY;
1314  ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_, media_len);
1316  ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_, off_len);
1318  ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_, asf->rep_data_len);
1319  if (asf_pkt->size_left && (asf_pkt->frame_num != media_len)) {
1320  av_log(s, AV_LOG_WARNING, "Unfinished frame will be ignored\n");
1321  reset_packet(asf_pkt);
1322  }
1323  asf_pkt->frame_num = media_len;
1324  asf->sub_dts = off_len;
1325  if (asf->nb_mult_left) {
1326  if ((ret = asf_read_multiple_payload(s, pkt, asf_pkt)) < 0)
1327  return ret;
1328  } else if (asf->rep_data_len == 1) {
1329  asf->sub_left = 1;
1330  asf->state = READ_SINGLE;
1331  pkt->flags = asf_pkt->flags;
1332  if ((ret = asf_read_subpayload(s, pkt, 1)) < 0)
1333  return ret;
1334  } else {
1335  if ((ret = asf_read_single_payload(s, asf_pkt)) < 0)
1336  return ret;
1337  }
1338  } else {
1339  for (i = 0; i <= asf->nb_streams; i++) {
1340  if (asf->stream_index == asf->asf_st[i]->stream_index) {
1341  asf_pkt = &asf->asf_st[i]->pkt;
1342  break;
1343  }
1344  }
1345  if (!asf_pkt)
1346  return AVERROR_INVALIDDATA;
1347  pkt->flags = asf_pkt->flags;
1348  pkt->dts = asf_pkt->dts;
1349  pkt->stream_index = asf->asf_st[i]->index;
1350  if ((ret = asf_read_subpayload(s, pkt, 0)) < 0) // read subpayload without its header
1351  return ret;
1352  }
1353 
1354  return 0;
1355 }
1356 
1358 {
1359  ASFContext *asf = s->priv_data;
1360  AVIOContext *pb = s->pb;
1361  uint64_t size;
1362  uint32_t av_unused seq;
1363  unsigned char error_flags, len_flags, pay_flags;
1364 
1365  asf->packet_offset = avio_tell(pb);
1366  error_flags = avio_r8(pb); // read Error Correction Flags
1367  if (error_flags & ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT) {
1368  if (!(error_flags & ASF_ERROR_CORRECTION_LENGTH_TYPE)) {
1369  size = error_flags & ASF_PACKET_ERROR_CORRECTION_DATA_SIZE;
1370  avio_skip(pb, size);
1371  }
1372  len_flags = avio_r8(pb);
1373  } else
1374  len_flags = error_flags;
1375  asf->prop_flags = avio_r8(pb);
1377  ASF_PPI_FLAG_PACKET_LENGTH_FIELD_, asf->packet_size_internal);
1379  ASF_PPI_FLAG_SEQUENCE_FIELD_, seq);
1381  ASF_PPI_FLAG_PADDING_LENGTH_FIELD_, asf->pad_len );
1382  asf->send_time = avio_rl32(pb); // send time
1383  avio_skip(pb, 2); // skip duration
1384  if (len_flags & ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT) { // Multiple Payloads present
1385  pay_flags = avio_r8(pb);
1386  asf->nb_mult_left = (pay_flags & ASF_NUM_OF_PAYLOADS);
1387  }
1388 
1389  return 0;
1390 }
1391 
1392 static int asf_deinterleave(AVFormatContext *s, ASFPacket *asf_pkt, int st_num)
1393 {
1394  ASFContext *asf = s->priv_data;
1395  ASFStream *asf_st = asf->asf_st[st_num];
1396  unsigned char *p = asf_pkt->avpkt.data;
1397  uint16_t pkt_len = asf->asf_st[st_num]->virtual_pkt_len;
1398  uint16_t chunk_len = asf->asf_st[st_num]->virtual_chunk_len;
1399  int nchunks = pkt_len / chunk_len;
1400  AVPacket pkt;
1401  int pos = 0, j, l, ret;
1402 
1403 
1404  if ((ret = av_new_packet(&pkt, asf_pkt->data_size)) < 0)
1405  return ret;
1406 
1407  while (asf_pkt->data_size >= asf_st->span * pkt_len + pos) {
1408  if (pos >= asf_pkt->data_size) {
1409  break;
1410  }
1411  for (l = 0; l < pkt_len; l++) {
1412  if (pos >= asf_pkt->data_size) {
1413  break;
1414  }
1415  for (j = 0; j < asf_st->span; j++) {
1416  if ((pos + chunk_len) >= asf_pkt->data_size)
1417  break;
1418  memcpy(pkt.data + pos,
1419  p + (j * nchunks + l) * chunk_len,
1420  chunk_len);
1421  pos += chunk_len;
1422  }
1423  }
1424  p += asf_st->span * pkt_len;
1425  if (p > asf_pkt->avpkt.data + asf_pkt->data_size)
1426  break;
1427  }
1428  av_packet_unref(&asf_pkt->avpkt);
1429  asf_pkt->avpkt = pkt;
1430 
1431  return 0;
1432 }
1433 
1435 {
1436  ASFContext *asf = s->priv_data;
1437  AVIOContext *pb = s->pb;
1438  int ret, i;
1439 
1440  if ((avio_tell(pb) >= asf->data_offset + asf->data_size) &&
1441  !(asf->b_flags & ASF_FLAG_BROADCAST))
1442  return AVERROR_EOF;
1443  while (!pb->eof_reached) {
1444  if (asf->state == PARSE_PACKET_HEADER) {
1446  if (pb->eof_reached)
1447  break;
1448  if (!asf->nb_mult_left)
1449  asf->state = READ_SINGLE;
1450  else
1451  asf->state = READ_MULTI;
1452  }
1453  ret = asf_read_payload(s, pkt);
1454  if (ret == AVERROR(EAGAIN)) {
1455  asf->state = PARSE_PACKET_HEADER;
1456  continue;
1457  }
1458  else if (ret < 0)
1459  return ret;
1460 
1461  switch (asf->state) {
1462  case READ_SINGLE:
1463  if (!asf->sub_left)
1464  asf->state = PARSE_PACKET_HEADER;
1465  break;
1466  case READ_MULTI_SUB:
1467  if (!asf->sub_left && !asf->nb_mult_left) {
1468  asf->state = PARSE_PACKET_HEADER;
1469  if (!asf->return_subpayload &&
1470  (avio_tell(pb) <= asf->packet_offset +
1471  asf->packet_size - asf->pad_len))
1472  avio_skip(pb, asf->pad_len); // skip padding
1473  if (asf->packet_offset + asf->packet_size > avio_tell(pb))
1474  avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1475  } else if (!asf->sub_left)
1476  asf->state = READ_MULTI;
1477  break;
1478  case READ_MULTI:
1479  if (!asf->nb_mult_left) {
1480  asf->state = PARSE_PACKET_HEADER;
1481  if (!asf->return_subpayload &&
1482  (avio_tell(pb) <= asf->packet_offset +
1483  asf->packet_size - asf->pad_len))
1484  avio_skip(pb, asf->pad_len); // skip padding
1485  if (asf->packet_offset + asf->packet_size > avio_tell(pb))
1486  avio_seek(pb, asf->packet_offset + asf->packet_size, SEEK_SET);
1487  }
1488  break;
1489  }
1490  if (asf->return_subpayload) {
1491  asf->return_subpayload = 0;
1492  return 0;
1493  }
1494  for (i = 0; i < asf->nb_streams; i++) {
1495  ASFPacket *asf_pkt = &asf->asf_st[i]->pkt;
1496  if (asf_pkt && !asf_pkt->size_left && asf_pkt->data_size) {
1497  if (asf->asf_st[i]->span > 1 &&
1498  asf->asf_st[i]->type == AVMEDIA_TYPE_AUDIO)
1499  if ((ret = asf_deinterleave(s, asf_pkt, i)) < 0)
1500  return ret;
1501  av_packet_move_ref(pkt, &asf_pkt->avpkt);
1502  pkt->stream_index = asf->asf_st[i]->index;
1503  pkt->flags = asf_pkt->flags;
1504  pkt->dts = asf_pkt->dts - asf->preroll;
1505  asf_pkt->data_size = 0;
1506  asf_pkt->frame_num = 0;
1507  return 0;
1508  }
1509  }
1510  }
1511 
1512  if (pb->eof_reached)
1513  return AVERROR_EOF;
1514 
1515  return 0;
1516 }
1517 
1519 {
1520  ASFContext *asf = s->priv_data;
1521  int i;
1522 
1523  for (i = 0; i < ASF_MAX_STREAMS; i++) {
1524  av_dict_free(&asf->asf_sd[i].asf_met);
1525  if (i < asf->nb_streams) {
1526  av_packet_unref(&asf->asf_st[i]->pkt.avpkt);
1527  av_freep(&asf->asf_st[i]);
1528  }
1529  }
1530 
1531  asf->nb_streams = 0;
1532  return 0;
1533 }
1534 
1536 {
1537  ASFContext *asf = s->priv_data;
1538  int i;
1539 
1540  asf->state = PARSE_PACKET_HEADER;
1541  asf->offset = 0;
1542  asf->return_subpayload = 0;
1543  asf->sub_left = 0;
1544  asf->sub_header_offset = 0;
1545  asf->packet_offset = asf->first_packet_offset;
1546  asf->pad_len = 0;
1547  asf->rep_data_len = 0;
1548  asf->dts_delta = 0;
1549  asf->mult_sub_len = 0;
1550  asf->nb_mult_left = 0;
1551  asf->nb_sub = 0;
1552  asf->prop_flags = 0;
1553  asf->sub_dts = 0;
1554  for (i = 0; i < asf->nb_streams; i++) {
1555  ASFPacket *pkt = &asf->asf_st[i]->pkt;
1556  pkt->size_left = 0;
1557  pkt->data_size = 0;
1558  pkt->duration = 0;
1559  pkt->flags = 0;
1560  pkt->dts = 0;
1561  pkt->duration = 0;
1562  av_packet_unref(&pkt->avpkt);
1563  av_init_packet(&pkt->avpkt);
1564  }
1565 }
1566 
1567 /*
1568  * Find a timestamp for the requested position within the payload
1569  * where the pos (position) is the offset inside the Data Object.
1570  * When position is not on the packet boundary, asf_read_timestamp tries
1571  * to find the closest packet offset after this position. If this packet
1572  * is a key frame, this packet timestamp is read and an index entry is created
1573  * for the packet. If this packet belongs to the requested stream,
1574  * asf_read_timestamp upgrades pos to the packet beginning offset and
1575  * returns this packet's dts. So returned dts is the dts of the first key frame with
1576  * matching stream number after given position.
1577  */
1578 static int64_t asf_read_timestamp(AVFormatContext *s, int stream_index,
1579  int64_t *pos, int64_t pos_limit)
1580 {
1581  ASFContext *asf = s->priv_data;
1582  int64_t pkt_pos = *pos, pkt_offset, dts = AV_NOPTS_VALUE, data_end;
1583  AVPacket pkt;
1584  int n;
1585 
1586  data_end = asf->data_offset + asf->data_size;
1587 
1588  n = (pkt_pos - asf->first_packet_offset + asf->packet_size - 1) /
1589  asf->packet_size;
1590  n = av_clip(n, 0, ((data_end - asf->first_packet_offset) / asf->packet_size - 1));
1591  pkt_pos = asf->first_packet_offset + n * asf->packet_size;
1592 
1593  avio_seek(s->pb, pkt_pos, SEEK_SET);
1594  pkt_offset = pkt_pos;
1595 
1596  reset_packet_state(s);
1597  while (avio_tell(s->pb) < data_end) {
1598 
1599  int i, ret, st_found;
1600 
1601  av_init_packet(&pkt);
1602  pkt_offset = avio_tell(s->pb);
1603  if ((ret = asf_read_packet(s, &pkt)) < 0) {
1604  dts = AV_NOPTS_VALUE;
1605  return ret;
1606  }
1607  // ASFPacket may contain fragments of packets belonging to different streams,
1608  // pkt_offset is the offset of the first fragment within it.
1609  if ((pkt_offset >= (pkt_pos + asf->packet_size)))
1610  pkt_pos += asf->packet_size;
1611  for (i = 0; i < asf->nb_streams; i++) {
1612  ASFStream *st = asf->asf_st[i];
1613 
1614  st_found = 0;
1615  if (pkt.flags & AV_PKT_FLAG_KEY) {
1616  dts = pkt.dts;
1617  if (dts) {
1618  av_add_index_entry(s->streams[pkt.stream_index], pkt_pos,
1619  dts, pkt.size, 0, AVINDEX_KEYFRAME);
1620  if (stream_index == st->index) {
1621  st_found = 1;
1622  break;
1623  }
1624  }
1625  }
1626  }
1627  if (st_found)
1628  break;
1629  av_packet_unref(&pkt);
1630  }
1631  *pos = pkt_pos;
1632 
1633  av_packet_unref(&pkt);
1634  return dts;
1635 }
1636 
1637 static int asf_read_seek(AVFormatContext *s, int stream_index,
1638  int64_t timestamp, int flags)
1639 {
1640  ASFContext *asf = s->priv_data;
1641  int idx, ret;
1642 
1643  if (s->streams[stream_index]->nb_index_entries && asf->is_simple_index) {
1644  idx = av_index_search_timestamp(s->streams[stream_index], timestamp, flags);
1645  if (idx < 0 || idx >= s->streams[stream_index]->nb_index_entries)
1646  return AVERROR_INVALIDDATA;
1647  avio_seek(s->pb, s->streams[stream_index]->index_entries[idx].pos, SEEK_SET);
1648  } else {
1649  if ((ret = ff_seek_frame_binary(s, stream_index, timestamp, flags)) < 0)
1650  return ret;
1651  }
1652 
1653  reset_packet_state(s);
1654 
1655  return 0;
1656 }
1657 
1659 {
1660  int j, ret;
1661  const GUIDParseTable *g;
1662 
1663  swap_guid(guid);
1664  g = gdef;
1665  for (j = 0; j < FF_ARRAY_ELEMS(gdef); j++) {
1666  if (!(ret = memcmp(guid, g->guid, sizeof(g->guid))))
1667  return g;
1668  g++;
1669  }
1670 
1671  return NULL;
1672 }
1673 
1674 static int detect_unknown_subobject(AVFormatContext *s, int64_t offset, int64_t size)
1675 {
1676  ASFContext *asf = s->priv_data;
1677  AVIOContext *pb = s->pb;
1678  const GUIDParseTable *g = NULL;
1679  ff_asf_guid guid;
1680  int ret;
1681 
1682  while (avio_tell(pb) <= offset + size) {
1683  if (avio_tell(pb) == asf->offset)
1684  break;
1685  asf->offset = avio_tell(pb);
1686  if ((ret = ff_get_guid(pb, &guid)) < 0)
1687  return ret;
1688  g = find_guid(guid);
1689  if (g) {
1690  if ((ret = g->read_object(s, g)) < 0)
1691  return ret;
1692  } else {
1693  GUIDParseTable g2;
1694 
1695  g2.name = "Unknown";
1696  g2.is_subobject = 1;
1697  asf_read_unknown(s, &g2);
1698  }
1699  }
1700 
1701  return 0;
1702 }
1703 
1705 {
1706  ASFContext *asf = s->priv_data;
1707  AVIOContext *pb = s->pb;
1708  const GUIDParseTable *g = NULL;
1709  ff_asf_guid guid;
1710  int i, ret;
1711  uint64_t size;
1712 
1713  asf->preroll = 0;
1714  asf->is_simple_index = 0;
1715  ff_get_guid(pb, &guid);
1716  if (ff_guidcmp(&guid, &ff_asf_header))
1717  return AVERROR_INVALIDDATA;
1718  avio_skip(pb, 8); // skip header object size
1719  avio_skip(pb, 6); // skip number of header objects and 2 reserved bytes
1720  asf->data_reached = 0;
1721 
1722  /* 1 is here instead of pb->eof_reached because (when not streaming), Data are skipped
1723  * for the first time,
1724  * Index object is processed and got eof and then seeking back to the Data is performed.
1725  */
1726  while (1) {
1727  // for the cases when object size is invalid
1728  if (avio_tell(pb) == asf->offset)
1729  break;
1730  asf->offset = avio_tell(pb);
1731  if ((ret = ff_get_guid(pb, &guid)) < 0) {
1732  if (ret == AVERROR_EOF && asf->data_reached)
1733  break;
1734  else
1735  goto failed;
1736  }
1737  g = find_guid(guid);
1738  if (g) {
1739  asf->unknown_offset = asf->offset;
1740  asf->is_header = 1;
1741  if ((ret = g->read_object(s, g)) < 0)
1742  goto failed;
1743  } else {
1744  size = avio_rl64(pb);
1745  align_position(pb, asf->offset, size);
1746  }
1747  if (asf->data_reached &&
1748  (!(pb->seekable & AVIO_SEEKABLE_NORMAL) ||
1749  (asf->b_flags & ASF_FLAG_BROADCAST)))
1750  break;
1751  }
1752 
1753  if (!asf->data_reached) {
1754  av_log(s, AV_LOG_ERROR, "Data Object was not found.\n");
1755  ret = AVERROR_INVALIDDATA;
1756  goto failed;
1757  }
1758  if (pb->seekable & AVIO_SEEKABLE_NORMAL)
1759  avio_seek(pb, asf->first_packet_offset, SEEK_SET);
1760 
1761  for (i = 0; i < asf->nb_streams; i++) {
1762  const char *rfc1766 = asf->asf_sd[asf->asf_st[i]->lang_idx].langs;
1763  AVStream *st = s->streams[asf->asf_st[i]->index];
1764  set_language(s, rfc1766, &st->metadata);
1765  }
1766 
1767  for (i = 0; i < ASF_MAX_STREAMS; i++) {
1768  AVStream *st = NULL;
1769 
1770  st = find_stream(s, i);
1771  if (st) {
1773  if (asf->asf_sd[i].aspect_ratio.num > 0 && asf->asf_sd[i].aspect_ratio.den > 0) {
1776  }
1777  }
1778  }
1779 
1780  return 0;
1781 
1782 failed:
1783  asf_read_close(s);
1784  return ret;
1785 }
1786 
1788  .name = "asf_o",
1789  .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1790  .priv_data_size = sizeof(ASFContext),
1791  .read_probe = asf_probe,
1795  .read_timestamp = asf_read_timestamp,
1798 };
int nb_streams
Definition: asfdec_o.c:120
int data_reached
Definition: asfdec_o.c:95
const ff_asf_guid ff_asf_header
Definition: asf.c:23
static void swap_guid(ff_asf_guid guid)
Definition: asfdec_o.c:159
static int asf_read_ext_stream_properties(AVFormatContext *s, const GUIDParseTable *g)
Definition: asfdec_o.c:844
#define NULL
Definition: coverity.c:32
#define AVFMT_NOBINSEARCH
Format does not allow to fall back on binary search via read_timestamp.
Definition: avformat.h:475
static int asf_read_close(AVFormatContext *s)
Definition: asfdec_o.c:1518
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define ASF_BYTE_ARRAY
Definition: asfdec_o.c:48
uint8_t stream_index
Definition: asfdec_o.c:77
static int asf_read_packet_header(AVFormatContext *s)
Definition: asfdec_o.c:1357
int64_t data_offset
Definition: asfdec_o.c:113
static int asf_read_unknown(AVFormatContext *s, const GUIDParseTable *g)
Definition: asfdec_o.c:173
#define ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE
Definition: asf.h:137
static void align_position(AVIOContext *pb, int64_t offset, uint64_t size)
Definition: asfdec_o.c:167
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition: utils.c:2039
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:3124
int size_left
Definition: asfdec_o.c:72
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4882
const char * g
Definition: vf_curves.c:115
const char * desc
Definition: nvenc.c:65
int64_t pos
Definition: avformat.h:803
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
uint64_t data_offset
beginning of the first data packet
Definition: asfdec_f.c:85
int data_size
Definition: asfdec_o.c:70
static int asf_read_marker(AVFormatContext *s, const GUIDParseTable *g)
Definition: asfdec_o.c:220
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3900
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:936
int num
Numerator.
Definition: rational.h:59
int index
stream index in AVFormatContext
Definition: avformat.h:875
int size
Definition: avcodec.h:1446
uint32_t packet_size
Definition: asfdec_o.c:101
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1103
uint16_t virtual_pkt_len
Definition: asfdec_o.c:82
#define ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE
Definition: asf.h:153
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
const ff_asf_guid ff_asf_command_stream
Definition: asf.c:65
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
const char * name
Definition: asfdec_o.c:59
static void get_id3_tag(AVFormatContext *s, int len)
Definition: asfdec_o.c:458
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
#define PUT_UTF8(val, tmp, PUT_BYTE)
Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
Definition: common.h:438
static int asf_read_stream_properties(AVFormatContext *s, const GUIDParseTable *g)
Definition: asfdec_o.c:727
static AVPacket pkt
#define ASF_UNICODE
Definition: asfdec_o.c:46
static int asf_read_replicated_data(AVFormatContext *s, ASFPacket *asf_pkt)
Definition: asfdec_o.c:1152
ASFStreamData asf_sd[ASF_MAX_STREAMS]
Definition: asfdec_o.c:119
int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta **extra_meta)
Create a stream for each APIC (attached picture) extracted from the ID3v2 header. ...
Definition: id3v2.c:1140
int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a UTF-16 string from pb and convert it to UTF-8.
int return_subpayload
Definition: asfdec_o.c:138
static int asf_read_language_list(AVFormatContext *s, const GUIDParseTable *g)
Definition: asfdec_o.c:912
Macro definitions for various function/variable attributes.
AVChapter * avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: utils.c:4583
Format I/O context.
Definition: avformat.h:1351
static const GUIDParseTable * find_guid(ff_asf_guid guid)
Definition: asfdec_o.c:1658
char str[32]
Definition: internal.h:50
#define ASF_STREAM_NUM
Definition: asfdec_o.c:51
static int process_metadata(AVFormatContext *s, const uint8_t *name, uint16_t name_len, uint16_t val_len, uint16_t type, AVDictionary **met)
Definition: asfdec_o.c:470
Public dictionary API.
static int64_t start_time
Definition: ffplay.c:330
static int asf_read_metadata_obj(AVFormatContext *s, const GUIDParseTable *g)
Definition: asfdec_o.c:575
uint8_t
static int asf_read_generic_value(AVIOContext *pb, int type, uint64_t *value)
Definition: asfdec_o.c:314
uint8_t stream_index
Definition: asfdec_o.c:73
static int nb_streams
Definition: ffprobe.c:276
#define av_malloc(s)
Opaque data information usually continuous.
Definition: avutil.h:203
int width
Video only.
Definition: avcodec.h:3966
const ff_asf_guid ff_asf_audio_stream
Definition: asf.c:39
static int asf_store_aspect_ratio(AVFormatContext *s, uint8_t st_num, uint8_t *name, int type)
Definition: asfdec_o.c:555
int is_simple_index
Definition: asfdec_o.c:96
AVOptions.
int ff_id3v2_parse_chapters(AVFormatContext *s, ID3v2ExtraMeta **extra_meta)
Create chapters for all CHAP tags found in the ID3v2 header.
Definition: id3v2.c:1180
uint8_t dts_delta
Definition: asfdec_o.c:127
#define BMP_HEADER_SIZE
Definition: asfdec_o.c:53
int id
Format-specific stream ID.
Definition: avformat.h:881
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4455
int64_t send_time
Definition: asfdec_o.c:102
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
static int asf_read_data(AVFormatContext *s, const GUIDParseTable *g)
Definition: asfdec_o.c:938
uint8_t * data
Definition: avcodec.h:1445
int64_t offset
Definition: asfdec_o.c:111
int64_t packet_offset
Definition: asfdec_o.c:129
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:653
static void reset_packet_state(AVFormatContext *s)
Definition: asfdec_o.c:1535
uint32_t tag
Definition: movenc.c:1483
#define AVERROR_EOF
End of file.
Definition: error.h:55
enum AVCodecID id
Definition: internal.h:51
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
AVPacket pkt
Definition: asfdec_f.c:51
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:310
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:648
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size, int big_endian)
Definition: riffdec.c:91
uint32_t pad_len
Definition: asfdec_o.c:130
AVRational aspect_ratio
Definition: asfdec_o.c:91
static int asf_read_content_desc(AVFormatContext *s, const GUIDParseTable *g)
Definition: asfdec_o.c:622
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3929
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1477
#define GET_UTF16(val, GET_16BIT, ERROR)
Convert a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form.
Definition: common.h:410
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
int16_t lang_idx
Definition: asfdec_o.c:84
#define AVINDEX_KEYFRAME
Definition: avformat.h:810
static int asf_probe(AVProbeData *pd)
Definition: asfdec_o.c:150
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1591
int indexed
Definition: asfdec_o.c:80
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:2148
#define ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE
Definition: asf.h:148
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:770
int type
Definition: asfdec_o.c:79
#define AVERROR(e)
Definition: error.h:43
#define ASF_QWORD
Definition: asfdec_o.c:45
uint64_t preroll
Definition: asfdec_o.c:99
const char * ff_convert_lang_to(const char *lang, enum AVLangCodespace target_codespace)
Convert a language code to a target codespace.
Definition: avlanguage.c:736
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void ff_asfcrypt_dec(const uint8_t key[20], uint8_t *data, int len)
Definition: asfcrypt.c:147
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
static int asf_read_ext_content(AVFormatContext *s, const GUIDParseTable *g)
Definition: asfdec_o.c:502
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3896
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition: id3v2.c:1124
int64_t sub_dts
Definition: asfdec_o.c:126
int ff_get_guid(AVIOContext *s, ff_asf_guid *g)
Definition: riffdec.c:32
ff_asf_guid guid
Definition: asfdec_o.c:60
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:947
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:117
int index
Definition: asfdec_o.c:78
static struct tm * gmtime_r(const time_t *clock, struct tm *result)
Definition: time_internal.h:26
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1451
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3918
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:639
static int asf_deinterleave(AVFormatContext *s, ASFPacket *asf_pkt, int st_num)
Definition: asfdec_o.c:1392
#define ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT
Definition: asf.h:127
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:130
common internal API header
static int asf_read_multiple_payload(AVFormatContext *s, AVPacket *pkt, ASFPacket *asf_pkt)
Definition: asfdec_o.c:1174
#define ASF_FLAG_BROADCAST
Definition: asfdec_o.c:47
static int asf_read_single_payload(AVFormatContext *s, ASFPacket *asf_pkt)
Definition: asfdec_o.c:1225
static int asf_read_payload(AVFormatContext *s, AVPacket *pkt)
Definition: asfdec_o.c:1277
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
static int asf_read_value(AVFormatContext *s, const uint8_t *name, uint16_t val_len, int type, AVDictionary **met)
Definition: asfdec_o.c:273
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:32
static int asf_read_simple_index(AVFormatContext *s, const GUIDParseTable *g)
Definition: asfdec_o.c:974
#define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE
Definition: asfdec_o.c:56
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
uint32_t frame_num
Definition: asfdec_o.c:68
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
AVInputFormat ff_asf_o_demuxer
Definition: asfdec_o.c:1787
int64_t dts
Definition: asfdec_o.c:67
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
ASFStream * asf_st
currently decoded stream
Definition: asfdec_f.c:112
static int get_asf_string(AVIOContext *pb, int maxlen, char *buf, int buflen)
Definition: asfdec_o.c:203
uint64_t data_size
Definition: asfdec_o.c:108
#define ASF_WORD
Definition: asfdec_o.c:42
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
#define ASF_ERROR_CORRECTION_LENGTH_TYPE
Definition: asfdec_o.c:55
int32_t
#define READ_LEN(flag, name, len)
Definition: asfdec_o.c:1073
#define s(width, name)
Definition: cbs_vp9.c:257
const ff_asf_guid ff_asf_ext_stream_embed_stream_header
Definition: asf.c:100
static void set_language(AVFormatContext *s, const char *rfc1766, AVDictionary **met)
Definition: asfdec_o.c:831
int n
Definition: avisynth_c.h:684
AVDictionary * metadata
Definition: avformat.h:938
static int64_t asf_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos, int64_t pos_limit)
Definition: asfdec_o.c:1578
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: asfdec_o.c:1637
enum ASFContext::@237 state
#define FF_ARRAY_ELEMS(a)
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:842
unsigned int nb_sub
Definition: asfdec_o.c:135
#define ASF_BOOL
Definition: asfdec_o.c:41
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
#define ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT
Definition: asf.h:108
uint8_t ff_asf_guid[16]
Definition: riff.h:90
Stream structure.
Definition: avformat.h:874
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define ASF_NUM_OF_PAYLOADS
Definition: asfdec_o.c:54
uint32_t rep_data_len
Definition: asfdec_o.c:131
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
uint32_t prop_flags
Definition: asfdec_o.c:106
AVPacket avpkt
Definition: asfdec_o.c:66
uint32_t packet_size_internal
Definition: asfdec_o.c:128
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
const ff_asf_guid ff_asf_video_stream
Definition: asf.c:53
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:598
static int asf_read_picture(AVFormatContext *s, int len)
Definition: asfdec_o.c:358
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition: avformat.h:476
void * buf
Definition: avisynth_c.h:690
int64_t first_packet_offset
Definition: asfdec_o.c:114
GLint GLenum type
Definition: opengl_enc.c:105
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
int nb_index_entries
Definition: avformat.h:1105
static const GUIDParseTable gdef[]
Definition: asfdec_o.c:1023
AVDictionary * asf_met
Definition: asfdec_o.c:90
static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: asfdec_o.c:1434
Rational number (pair of numerator and denominator).
Definition: rational.h:58
#define ASF_PPI_MASK_SEQUENCE_FIELD_SIZE
Definition: asf.h:132
#define ASF_DWORD
Definition: asfdec_o.c:44
byte swapping routines
uint64_t sub_left
Definition: asfdec_o.c:134
uint64_t unknown_size
Definition: asfdec_o.c:109
AVMediaType
Definition: avutil.h:199
#define snprintf
Definition: snprintf.h:34
uint16_t mult_sub_len
Definition: asfdec_o.c:136
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
uint32_t b_flags
Definition: asfdec_o.c:105
ASFPacket pkt
Definition: asfdec_o.c:85
static int64_t pts
int64_t unknown_offset
Definition: asfdec_o.c:115
static void reset_packet(ASFPacket *asf_pkt)
Definition: asfdec_o.c:1140
#define flags(name, subs,...)
Definition: cbs_av1.c:596
int duration
Definition: asfdec_o.c:71
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:923
int is_header
Definition: asfdec_o.c:97
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
const uint8_t * key
Definition: avformat.h:1527
int64_t bitrate
Definition: h264_levels.c:89
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:754
Main libavformat public API header.
int
#define ASF_MAX_STREAMS
Definition: asfdec_o.c:52
uint64_t sub_header_offset
Definition: asfdec_o.c:125
const ff_asf_guid ff_asf_jfif_media
Definition: asf.c:57
#define ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE
Definition: asf.h:142
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:279
static int asf_read_metadata(AVFormatContext *s, const char *title, uint16_t len, unsigned char *ch, uint16_t buflen)
Definition: asfdec_o.c:259
int is_subobject
Definition: asfdec_o.c:62
uint64_t nb_packets
how many packets are there in the file, invalid if broadcasting
Definition: asfdec_o.c:100
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:913
static int asf_read_subpayload(AVFormatContext *s, AVPacket *pkt, int is_header)
Definition: asfdec_o.c:1085
#define ASF_GUID
Definition: asfdec_o.c:43
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:927
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta, unsigned int max_search_size)
Read an ID3v2 tag, including supported extra metadata.
Definition: id3v2.c:1118
int(* read_object)(AVFormatContext *, const struct GUIDParseTable *)
Definition: asfdec_o.c:61
int den
Denominator.
Definition: rational.h:60
#define ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE
Definition: asf.h:158
int ff_get_bmp_header(AVIOContext *pb, AVStream *st, uint32_t *size)
Read BITMAPINFOHEADER structure and set AVStream codec width, height and bits_per_encoded_sample fiel...
Definition: riffdec.c:211
static int asf_read_properties(AVFormatContext *s, const GUIDParseTable *g)
Definition: asfdec_o.c:651
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:782
int eof_reached
true if eof reached
Definition: avio.h:239
int len
static int asf_set_metadata(AVFormatContext *s, const uint8_t *name, int type, AVDictionary **met)
Definition: asfdec_o.c:337
void * priv_data
Format private data.
Definition: avformat.h:1379
static AVStream * find_stream(AVFormatContext *s, uint16_t st_num)
Definition: asfdec_o.c:539
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3914
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1444
int duration
Definition: asfdec_o.c:103
uint64_t nb_mult_left
Definition: asfdec_o.c:137
char langs[32]
Definition: asfdec_o.c:89
int stream_index
Definition: asfdec_f.c:110
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
static int parse_video_info(AVIOContext *pb, AVStream *st)
Definition: asfdec_o.c:694
const char *const ff_id3v2_picture_types[21]
Definition: id3v2.c:106
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3904
#define FFSWAP(type, a, b)
Definition: common.h:99
int stream_index
Definition: avcodec.h:1447
int flags
Definition: asfdec_o.c:69
static av_always_inline int ff_guidcmp(const void *g1, const void *g2)
Definition: riff.h:116
enum AVCodecID id
This structure stores compressed data.
Definition: avcodec.h:1422
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:778
int8_t span
Definition: asfdec_o.c:81
uint16_t virtual_chunk_len
Definition: asfdec_o.c:83
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:956
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
int ff_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
Perform a binary search using av_index_search_timestamp() and AVInputFormat.read_timestamp().
Definition: utils.c:2163
#define av_unused
Definition: attributes.h:125
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
const char * name
Definition: opengl_enc.c:103
static int asf_read_header(AVFormatContext *s)
Definition: asfdec_o.c:1704
static int detect_unknown_subobject(AVFormatContext *s, int64_t offset, int64_t size)
Definition: asfdec_o.c:1674
static uint8_t tmp[11]
Definition: aes_ctr.c:26