FFmpeg  4.4.9
asfdec_f.c
Go to the documentation of this file.
1 /*
2  * ASF compatible demuxer
3  * Copyright (c) 2000, 2001 Fabrice Bellard
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 <inttypes.h>
23 
24 #include "libavutil/attributes.h"
25 #include "libavutil/avassert.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/bswap.h"
28 #include "libavutil/common.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
33 #include "avformat.h"
34 #include "avio_internal.h"
35 #include "avlanguage.h"
36 #include "id3v2.h"
37 #include "internal.h"
38 #include "riff.h"
39 #include "asf.h"
40 #include "asfcrypt.h"
41 
42 typedef struct ASFPayload {
44  uint16_t size;
45 } ASFPayload;
46 
47 typedef struct ASFStream {
48  int num;
49  unsigned char seq;
50  /* use for reading */
54  int timestamp;
57  int pkt_clean;
58 
59  int ds_span; /* descrambling */
62 
64 
66 
68  uint32_t palette[256];
69 
72 } ASFStream;
73 
74 typedef struct ASFContext {
75  const AVClass *class;
76  int asfid2avid[128]; ///< conversion table from asf ID 2 AVStream ID
77  ASFStream streams[128]; ///< it's max number and it's not that big
78  uint32_t stream_bitrates[128]; ///< max number of streams, bitrate for each (for streaming)
80  char stream_languages[128][6]; ///< max number of streams, language for each (RFC1766, e.g. en-US)
81  /* non streamed additonnal info */
82  /* packet filling */
84  /* only for reading */
85  uint64_t data_offset; ///< beginning of the first data packet
86  uint64_t data_object_offset; ///< data object offset (excl. GUID & size)
87  uint64_t data_object_size; ///< size of the data object
89 
91 
101  unsigned int packet_frag_offset;
102  unsigned int packet_frag_size;
109 
111 
112  ASFStream *asf_st; ///< currently decoded stream
113 
116 
118 } ASFContext;
119 
120 static const AVOption options[] = {
121  { "no_resync_search", "Don't try to resynchronize by looking for a certain optional start code", offsetof(ASFContext, no_resync_search), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
122  { "export_xmp", "Export full XMP metadata", offsetof(ASFContext, export_xmp), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
123  { NULL },
124 };
125 
126 static const AVClass asf_class = {
127  .class_name = "asf demuxer",
128  .item_name = av_default_item_name,
129  .option = options,
130  .version = LIBAVUTIL_VERSION_INT,
131 };
132 
133 #undef NDEBUG
134 #include <assert.h>
135 
136 #define ASF_MAX_STREAMS 127
137 #define FRAME_HEADER_SIZE 6
138 // Fix Me! FRAME_HEADER_SIZE may be different.
139 // (7 is known to be too large for GipsyGuitar.wmv)
140 
141 #ifdef DEBUG
142 static const ff_asf_guid stream_bitrate_guid = { /* (http://get.to/sdp) */
143  0xce, 0x75, 0xf8, 0x7b, 0x8d, 0x46, 0xd1, 0x11, 0x8d, 0x82, 0x00, 0x60, 0x97, 0xc9, 0xa2, 0xb2
144 };
145 
146 static const ff_asf_guid asf_audio_conceal_none = {
147  // 0x40, 0xa4, 0xf1, 0x49, 0x4ece, 0x11d0, 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6
148  // New value lifted from avifile
149  0x00, 0x57, 0xfb, 0x20, 0x55, 0x5B, 0xCF, 0x11, 0xa8, 0xfd, 0x00, 0x80, 0x5f, 0x5c, 0x44, 0x2b
150 };
151 
152 #define PRINT_IF_GUID(g, cmp) \
153  if (!ff_guidcmp(g, &cmp)) \
154  av_log(NULL, AV_LOG_TRACE, "(GUID: %s) ", # cmp)
155 
156 static void print_guid(ff_asf_guid *g)
157 {
158  int i;
159  PRINT_IF_GUID(g, ff_asf_header);
160  else PRINT_IF_GUID(g, ff_asf_file_header);
161  else PRINT_IF_GUID(g, ff_asf_stream_header);
162  else PRINT_IF_GUID(g, ff_asf_audio_stream);
163  else PRINT_IF_GUID(g, asf_audio_conceal_none);
164  else PRINT_IF_GUID(g, ff_asf_video_stream);
165  else PRINT_IF_GUID(g, ff_asf_video_conceal_none);
166  else PRINT_IF_GUID(g, ff_asf_command_stream);
167  else PRINT_IF_GUID(g, ff_asf_comment_header);
168  else PRINT_IF_GUID(g, ff_asf_codec_comment_header);
169  else PRINT_IF_GUID(g, ff_asf_codec_comment1_header);
170  else PRINT_IF_GUID(g, ff_asf_data_header);
171  else PRINT_IF_GUID(g, ff_asf_simple_index_header);
172  else PRINT_IF_GUID(g, ff_asf_head1_guid);
173  else PRINT_IF_GUID(g, ff_asf_head2_guid);
174  else PRINT_IF_GUID(g, ff_asf_my_guid);
175  else PRINT_IF_GUID(g, ff_asf_ext_stream_header);
176  else PRINT_IF_GUID(g, ff_asf_extended_content_header);
177  else PRINT_IF_GUID(g, ff_asf_ext_stream_embed_stream_header);
178  else PRINT_IF_GUID(g, ff_asf_ext_stream_audio_stream);
179  else PRINT_IF_GUID(g, ff_asf_metadata_header);
180  else PRINT_IF_GUID(g, ff_asf_metadata_library_header);
181  else PRINT_IF_GUID(g, ff_asf_marker_header);
182  else PRINT_IF_GUID(g, stream_bitrate_guid);
183  else PRINT_IF_GUID(g, ff_asf_language_guid);
184  else
185  av_log(NULL, AV_LOG_TRACE, "(GUID: unknown) ");
186  for (i = 0; i < 16; i++)
187  av_log(NULL, AV_LOG_TRACE, " 0x%02x,", (*g)[i]);
188  av_log(NULL, AV_LOG_TRACE, "}\n");
189 }
190 #undef PRINT_IF_GUID
191 #else
192 #define print_guid(g) while(0)
193 #endif
194 
195 static int asf_probe(const AVProbeData *pd)
196 {
197  /* check file header */
198  if (!ff_guidcmp(pd->buf, &ff_asf_header))
199  return AVPROBE_SCORE_MAX;
200  else
201  return 0;
202 }
203 
204 /* size of type 2 (BOOL) is 32bit for "Extended Content Description Object"
205  * but 16 bit for "Metadata Object" and "Metadata Library Object" */
206 static int get_value(AVIOContext *pb, int type, int type2_size)
207 {
208  switch (type) {
209  case 2:
210  return (type2_size == 32) ? avio_rl32(pb) : avio_rl16(pb);
211  case 3:
212  return avio_rl32(pb);
213  case 4:
214  return avio_rl64(pb);
215  case 5:
216  return avio_rl16(pb);
217  default:
218  return INT_MIN;
219  }
220 }
221 
222 /* MSDN claims that this should be "compatible with the ID3 frame, APIC",
223  * but in reality this is only loosely similar */
225 {
226  AVPacket pkt = { 0 };
227  const CodecMime *mime = ff_id3v2_mime_tags;
228  enum AVCodecID id = AV_CODEC_ID_NONE;
229  char mimetype[64];
230  uint8_t *desc = NULL;
231  AVStream *st = NULL;
232  int ret, type, picsize, desc_len;
233 
234  /* type + picsize + mime + desc */
235  if (len < 1 + 4 + 2 + 2) {
236  av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
237  return AVERROR_INVALIDDATA;
238  }
239 
240  /* picture type */
241  type = avio_r8(s->pb);
242  len--;
244  av_log(s, AV_LOG_WARNING, "Unknown attached picture type: %d.\n", type);
245  type = 0;
246  }
247 
248  /* picture data size */
249  picsize = avio_rl32(s->pb);
250  len -= 4;
251 
252  /* picture MIME type */
253  len -= avio_get_str16le(s->pb, len, mimetype, sizeof(mimetype));
254  while (mime->id != AV_CODEC_ID_NONE) {
255  if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
256  id = mime->id;
257  break;
258  }
259  mime++;
260  }
261  if (id == AV_CODEC_ID_NONE) {
262  av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
263  mimetype);
264  return 0;
265  }
266 
267  if (picsize >= len) {
268  av_log(s, AV_LOG_ERROR, "Invalid attached picture data size: %d >= %d.\n",
269  picsize, len);
270  return AVERROR_INVALIDDATA;
271  }
272 
273  /* picture description */
274  desc_len = (len - picsize) * 2 + 1;
275  desc = av_malloc(desc_len);
276  if (!desc)
277  return AVERROR(ENOMEM);
278  len -= avio_get_str16le(s->pb, len - picsize, desc, desc_len);
279 
280  ret = av_get_packet(s->pb, &pkt, picsize);
281  if (ret < 0)
282  goto fail;
283 
284  st = avformat_new_stream(s, NULL);
285  if (!st) {
286  ret = AVERROR(ENOMEM);
287  goto fail;
288  }
291  st->codecpar->codec_id = id;
292  st->attached_pic = pkt;
293  st->attached_pic.stream_index = st->index;
295 
296  if (*desc)
298  else
299  av_freep(&desc);
300 
301  av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
302 
303  return 0;
304 
305 fail:
306  av_freep(&desc);
308  return ret;
309 }
310 
311 static void get_id3_tag(AVFormatContext *s, int len)
312 {
313  ID3v2ExtraMeta *id3v2_extra_meta = NULL;
314 
315  ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, len);
316  if (id3v2_extra_meta) {
317  ff_id3v2_parse_apic(s, id3v2_extra_meta);
318  ff_id3v2_parse_chapters(s, id3v2_extra_meta);
319  }
320  ff_id3v2_free_extra_meta(&id3v2_extra_meta);
321 }
322 
323 static void get_tag(AVFormatContext *s, const char *key, int type, int len, int type2_size)
324 {
325  ASFContext *asf = s->priv_data;
326  char *value = NULL;
327  int64_t off = avio_tell(s->pb);
328 #define LEN 22
329 
330  av_assert0((unsigned)len < (INT_MAX - LEN) / 2);
331 
332  if (!asf->export_xmp && !strncmp(key, "xmp", 3))
333  goto finish;
334 
335  value = av_malloc(2 * len + LEN);
336  if (!value)
337  goto finish;
338 
339  switch (type) {
340  case ASF_UNICODE:
341  avio_get_str16le(s->pb, len, value, 2 * len + 1);
342  break;
343  case -1:; // ASCII
344  int ret = ffio_read_size(s->pb, value, len);
345  if (ret < 0)
346  goto finish;
347  value[len]=0;
348  break;
349  case ASF_BYTE_ARRAY:
350  if (!strcmp(key, "WM/Picture")) { // handle cover art
352  } else if (!strcmp(key, "ID3")) { // handle ID3 tag
353  get_id3_tag(s, len);
354  } else {
355  av_log(s, AV_LOG_VERBOSE, "Unsupported byte array in tag %s.\n", key);
356  }
357  goto finish;
358  case ASF_BOOL:
359  case ASF_DWORD:
360  case ASF_QWORD:
361  case ASF_WORD: {
362  uint64_t num = get_value(s->pb, type, type2_size);
363  snprintf(value, LEN, "%"PRIu64, num);
364  break;
365  }
366  case ASF_GUID:
367  av_log(s, AV_LOG_DEBUG, "Unsupported GUID value in tag %s.\n", key);
368  goto finish;
369  default:
371  "Unsupported value type %d in tag %s.\n", type, key);
372  goto finish;
373  }
374  if (*value)
375  av_dict_set(&s->metadata, key, value, 0);
376 
377 finish:
378  av_freep(&value);
379  avio_seek(s->pb, off + len, SEEK_SET);
380 }
381 
383 {
384  ASFContext *asf = s->priv_data;
385  AVIOContext *pb = s->pb;
386 
387  ff_get_guid(pb, &asf->hdr.guid);
388  asf->hdr.file_size = avio_rl64(pb);
389  asf->hdr.create_time = avio_rl64(pb);
390  avio_rl64(pb); /* number of packets */
391  asf->hdr.play_time = avio_rl64(pb);
392  asf->hdr.send_time = avio_rl64(pb);
393  asf->hdr.preroll = avio_rl32(pb);
394  asf->hdr.ignore = avio_rl32(pb);
395  asf->hdr.flags = avio_rl32(pb);
396  asf->hdr.min_pktsize = avio_rl32(pb);
397  asf->hdr.max_pktsize = avio_rl32(pb);
398  if (asf->hdr.min_pktsize >= (1U << 29))
399  return AVERROR_INVALIDDATA;
400  asf->hdr.max_bitrate = avio_rl32(pb);
401  s->packet_size = asf->hdr.max_pktsize;
402 
403  return 0;
404 }
405 
407 {
408  ASFContext *asf = s->priv_data;
409  AVIOContext *pb = s->pb;
410  AVStream *st;
411  ASFStream *asf_st;
412  ff_asf_guid g;
413  enum AVMediaType type;
414  int type_specific_size, sizeX;
415  unsigned int tag1;
416  int64_t pos1, pos2, start_time;
417  int test_for_ext_stream_audio, is_dvr_ms_audio = 0;
418 
419  if (s->nb_streams == ASF_MAX_STREAMS) {
420  av_log(s, AV_LOG_ERROR, "too many streams\n");
421  return AVERROR(EINVAL);
422  }
423 
424  pos1 = avio_tell(pb);
425 
426  st = avformat_new_stream(s, NULL);
427  if (!st)
428  return AVERROR(ENOMEM);
429  avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
430  start_time = asf->hdr.preroll;
431 
432  if (!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
433  int64_t fsize = avio_size(pb);
434  if (fsize <= 0 || (int64_t)asf->hdr.file_size <= 0 ||
435  FFABS(fsize - (int64_t)asf->hdr.file_size) < FFMIN(fsize, asf->hdr.file_size)/20)
436  st->duration = asf->hdr.play_time /
437  (10000000 / 1000) - start_time;
438  }
439  ff_get_guid(pb, &g);
440 
441  test_for_ext_stream_audio = 0;
442  if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
444  } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
446  } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
449  } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
452  test_for_ext_stream_audio = 1;
454  } else {
455  return -1;
456  }
457  ff_get_guid(pb, &g);
458  avio_skip(pb, 8); /* total_size */
459  type_specific_size = avio_rl32(pb);
460  avio_rl32(pb);
461  st->id = avio_rl16(pb) & 0x7f; /* stream id */
462  // mapping of asf ID to AV stream ID;
463  asf->asfid2avid[st->id] = s->nb_streams - 1;
464  asf_st = &asf->streams[st->id];
465 
466  avio_rl32(pb);
467 
468  if (test_for_ext_stream_audio) {
469  ff_get_guid(pb, &g);
472  is_dvr_ms_audio = 1;
473  ff_get_guid(pb, &g);
474  avio_rl32(pb);
475  avio_rl32(pb);
476  avio_rl32(pb);
477  ff_get_guid(pb, &g);
478  avio_rl32(pb);
479  }
480  }
481 
482  st->codecpar->codec_type = type;
483  if (type == AVMEDIA_TYPE_AUDIO) {
484  int ret = ff_get_wav_header(s, pb, st->codecpar, type_specific_size, 0);
485  if (ret < 0)
486  return ret;
487  if (is_dvr_ms_audio) {
488  // codec_id and codec_tag are unreliable in dvr_ms
489  // files. Set them later by probing stream.
490  st->internal->request_probe = 1;
491  st->codecpar->codec_tag = 0;
492  }
493  if (st->codecpar->codec_id == AV_CODEC_ID_AAC)
495  else
497  /* We have to init the frame size at some point .... */
498  pos2 = avio_tell(pb);
499  if (size >= (pos2 + 8 - pos1 + 24)) {
500  asf_st->ds_span = avio_r8(pb);
501  asf_st->ds_packet_size = avio_rl16(pb);
502  asf_st->ds_chunk_size = avio_rl16(pb);
503  avio_rl16(pb); // ds_data_size
504  avio_r8(pb); // ds_silence_data
505  }
506  if (asf_st->ds_span > 1) {
507  if (!asf_st->ds_chunk_size ||
508  (asf_st->ds_packet_size / asf_st->ds_chunk_size <= 1) ||
509  asf_st->ds_packet_size % asf_st->ds_chunk_size)
510  asf_st->ds_span = 0; // disable descrambling
511  }
512  } else if (type == AVMEDIA_TYPE_VIDEO &&
513  size - (avio_tell(pb) - pos1 + 24) >= 51) {
514  avio_rl32(pb);
515  avio_rl32(pb);
516  avio_r8(pb);
517  avio_rl16(pb); /* size */
518  sizeX = avio_rl32(pb); /* size */
519  st->codecpar->width = avio_rl32(pb);
520  st->codecpar->height = avio_rl32(pb);
521  /* not available for asf */
522  avio_rl16(pb); /* panes */
523  st->codecpar->bits_per_coded_sample = avio_rl16(pb); /* depth */
524  tag1 = avio_rl32(pb);
525  avio_skip(pb, 20);
526  if (sizeX > 40) {
527  if (size < sizeX - 40 || sizeX - 40 > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
528  return AVERROR_INVALIDDATA;
529  st->codecpar->extradata_size = ffio_limit(pb, sizeX - 40);
532  if (!st->codecpar->extradata)
533  return AVERROR(ENOMEM);
535  }
536 
537  /* Extract palette from extradata if bpp <= 8 */
538  /* This code assumes that extradata contains only palette */
539  /* This is true for all paletted codecs implemented in libavcodec */
540  if (st->codecpar->extradata_size && (st->codecpar->bits_per_coded_sample <= 8)) {
541 #if HAVE_BIGENDIAN
542  int i;
543  for (i = 0; i < FFMIN(st->codecpar->extradata_size, AVPALETTE_SIZE) / 4; i++)
544  asf_st->palette[i] = av_bswap32(((uint32_t *)st->codecpar->extradata)[i]);
545 #else
546  memcpy(asf_st->palette, st->codecpar->extradata,
548 #endif
549  asf_st->palette_changed = 1;
550  }
551 
552  st->codecpar->codec_tag = tag1;
554  if (tag1 == MKTAG('D', 'V', 'R', ' ')) {
556  /* issue658 contains wrong w/h and MS even puts a fake seq header
557  * with wrong w/h in extradata while a correct one is in the stream.
558  * maximum lameness */
559  st->codecpar->width =
560  st->codecpar->height = 0;
561  av_freep(&st->codecpar->extradata);
562  st->codecpar->extradata_size = 0;
563  }
564  if (st->codecpar->codec_id == AV_CODEC_ID_H264)
566  if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4)
568  }
569  pos2 = avio_tell(pb);
570  avio_skip(pb, size - (pos2 - pos1 + 24));
571 
572  return 0;
573 }
574 
576 {
577  ASFContext *asf = s->priv_data;
578  AVIOContext *pb = s->pb;
579  ff_asf_guid g;
580  int ext_len, payload_ext_ct, stream_ct, i;
581  uint32_t leak_rate, stream_num;
582  unsigned int stream_languageid_index;
583 
584  avio_rl64(pb); // starttime
585  avio_rl64(pb); // endtime
586  leak_rate = avio_rl32(pb); // leak-datarate
587  avio_rl32(pb); // bucket-datasize
588  avio_rl32(pb); // init-bucket-fullness
589  avio_rl32(pb); // alt-leak-datarate
590  avio_rl32(pb); // alt-bucket-datasize
591  avio_rl32(pb); // alt-init-bucket-fullness
592  avio_rl32(pb); // max-object-size
593  avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
594  stream_num = avio_rl16(pb); // stream-num
595 
596  stream_languageid_index = avio_rl16(pb); // stream-language-id-index
597  if (stream_num < 128)
598  asf->streams[stream_num].stream_language_index = stream_languageid_index;
599 
600  avio_rl64(pb); // avg frametime in 100ns units
601  stream_ct = avio_rl16(pb); // stream-name-count
602  payload_ext_ct = avio_rl16(pb); // payload-extension-system-count
603 
604  if (stream_num < 128) {
605  asf->stream_bitrates[stream_num] = leak_rate;
606  asf->streams[stream_num].payload_ext_ct = 0;
607  }
608 
609  for (i = 0; i < stream_ct; i++) {
610  avio_rl16(pb);
611  ext_len = avio_rl16(pb);
612  avio_skip(pb, ext_len);
613  }
614 
615  for (i = 0; i < payload_ext_ct; i++) {
616  int size;
617  ff_get_guid(pb, &g);
618  size = avio_rl16(pb);
619  ext_len = avio_rl32(pb);
620  if (ext_len < 0)
621  return AVERROR_INVALIDDATA;
622  avio_skip(pb, ext_len);
623  if (stream_num < 128 && i < FF_ARRAY_ELEMS(asf->streams[stream_num].payload)) {
624  ASFPayload *p = &asf->streams[stream_num].payload[i];
625  p->type = g[0];
626  p->size = size;
627  av_log(s, AV_LOG_DEBUG, "Payload extension %x %d\n", g[0], p->size );
628  asf->streams[stream_num].payload_ext_ct ++;
629  }
630  }
631 
632  return 0;
633 }
634 
636 {
637  AVIOContext *pb = s->pb;
638  int len1, len2, len3, len4, len5;
639 
640  len1 = avio_rl16(pb);
641  len2 = avio_rl16(pb);
642  len3 = avio_rl16(pb);
643  len4 = avio_rl16(pb);
644  len5 = avio_rl16(pb);
645  get_tag(s, "title", 0, len1, 32);
646  get_tag(s, "author", 0, len2, 32);
647  get_tag(s, "copyright", 0, len3, 32);
648  get_tag(s, "comment", 0, len4, 32);
649  avio_skip(pb, len5);
650 
651  return 0;
652 }
653 
655 {
656  AVIOContext *pb = s->pb;
657  ASFContext *asf = s->priv_data;
658  int desc_count, i, ret;
659 
660  desc_count = avio_rl16(pb);
661  for (i = 0; i < desc_count; i++) {
662  int name_len, value_type, value_len;
663  char name[1024];
664 
665  name_len = avio_rl16(pb);
666  if (name_len % 2) // must be even, broken lavf versions wrote len-1
667  name_len += 1;
668  if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
669  avio_skip(pb, name_len - ret);
670  value_type = avio_rl16(pb);
671  value_len = avio_rl16(pb);
672  if (!value_type && value_len % 2)
673  value_len += 1;
674  /* My sample has that stream set to 0 maybe that mean the container.
675  * ASF stream count starts at 1. I am using 0 to the container value
676  * since it's unused. */
677  if (!strcmp(name, "AspectRatioX"))
678  asf->dar[0].num = get_value(s->pb, value_type, 32);
679  else if (!strcmp(name, "AspectRatioY"))
680  asf->dar[0].den = get_value(s->pb, value_type, 32);
681  else
682  get_tag(s, name, value_type, value_len, 32);
683  }
684 
685  return 0;
686 }
687 
689 {
690  AVIOContext *pb = s->pb;
691  ASFContext *asf = s->priv_data;
692  int j, ret;
693  int stream_count = avio_rl16(pb);
694  for (j = 0; j < stream_count; j++) {
695  char lang[6];
696  unsigned int lang_len = avio_r8(pb);
697  if ((ret = avio_get_str16le(pb, lang_len, lang,
698  sizeof(lang))) < lang_len)
699  avio_skip(pb, lang_len - ret);
700  if (j < 128)
701  av_strlcpy(asf->stream_languages[j], lang,
702  sizeof(*asf->stream_languages));
703  }
704 
705  return 0;
706 }
707 
709 {
710  AVIOContext *pb = s->pb;
711  ASFContext *asf = s->priv_data;
712  int n, stream_num, name_len_utf16, name_len_utf8;
713  unsigned int value_len;
714  int ret, i;
715  n = avio_rl16(pb);
716 
717  for (i = 0; i < n; i++) {
718  uint8_t *name;
719  int value_type;
720 
721  avio_rl16(pb); // lang_list_index
722  stream_num = avio_rl16(pb);
723  name_len_utf16 = avio_rl16(pb);
724  value_type = avio_rl16(pb); /* value_type */
725  value_len = avio_rl32(pb);
726 
727  if (value_len >= (INT_MAX - LEN) / 2)
728  return AVERROR_INVALIDDATA;
729 
730  name_len_utf8 = 2*name_len_utf16 + 1;
731  name = av_malloc(name_len_utf8);
732  if (!name)
733  return AVERROR(ENOMEM);
734 
735  if ((ret = avio_get_str16le(pb, name_len_utf16, name, name_len_utf8)) < name_len_utf16)
736  avio_skip(pb, name_len_utf16 - ret);
737  av_log(s, AV_LOG_TRACE, "%d stream %d name_len %2d type %d len %4d <%s>\n",
738  i, stream_num, name_len_utf16, value_type, value_len, name);
739 
740  if (!strcmp(name, "AspectRatioX")){
741  int aspect_x = get_value(s->pb, value_type, 16);
742  if(stream_num < 128)
743  asf->dar[stream_num].num = aspect_x;
744  } else if(!strcmp(name, "AspectRatioY")){
745  int aspect_y = get_value(s->pb, value_type, 16);
746  if(stream_num < 128)
747  asf->dar[stream_num].den = aspect_y;
748  } else {
749  get_tag(s, name, value_type, value_len, 16);
750  }
751  av_freep(&name);
752  }
753 
754  return 0;
755 }
756 
758 {
759  AVIOContext *pb = s->pb;
760  ASFContext *asf = s->priv_data;
761  int i, count, name_len, ret;
762  char name[1024];
763 
764  avio_rl64(pb); // reserved 16 bytes
765  avio_rl64(pb); // ...
766  count = avio_rl32(pb); // markers count
767  avio_rl16(pb); // reserved 2 bytes
768  name_len = avio_rl16(pb); // name length
769  avio_skip(pb, name_len);
770 
771  for (i = 0; i < count; i++) {
772  int64_t pres_time;
773  int name_len;
774 
775  if (avio_feof(pb))
776  return AVERROR_INVALIDDATA;
777 
778  avio_rl64(pb); // offset, 8 bytes
779  pres_time = avio_rl64(pb); // presentation time
780  pres_time = av_sat_sub64(pres_time, asf->hdr.preroll * 10000LL);
781  avio_rl16(pb); // entry length
782  avio_rl32(pb); // send time
783  avio_rl32(pb); // flags
784  name_len = avio_rl32(pb); // name length
785  if ((unsigned)name_len > INT_MAX / 2)
786  return AVERROR_INVALIDDATA;
787  if ((ret = avio_get_str16le(pb, name_len * 2, name,
788  sizeof(name))) < name_len)
789  avio_skip(pb, name_len - ret);
790  avpriv_new_chapter(s, i, (AVRational) { 1, 10000000 }, pres_time,
792  }
793 
794  return 0;
795 }
796 
798 {
799  ASFContext *asf = s->priv_data;
800  ff_asf_guid g;
801  AVIOContext *pb = s->pb;
802  int i;
803  int64_t gsize;
804 
805  ff_get_guid(pb, &g);
806  if (ff_guidcmp(&g, &ff_asf_header))
807  return AVERROR_INVALIDDATA;
808  avio_rl64(pb);
809  avio_rl32(pb);
810  avio_r8(pb);
811  avio_r8(pb);
812  memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
813 
814  for (i = 0; i<128; i++)
815  asf->streams[i].stream_language_index = 128; // invalid stream index means no language info
816 
817  for (;;) {
818  uint64_t gpos = avio_tell(pb);
819  int ret = 0;
820  ff_get_guid(pb, &g);
821  gsize = avio_rl64(pb);
822  print_guid(&g);
823  if (!ff_guidcmp(&g, &ff_asf_data_header)) {
824  asf->data_object_offset = avio_tell(pb);
825  /* If not streaming, gsize is not unlimited (how?),
826  * and there is enough space in the file.. */
827  if (!(asf->hdr.flags & 0x01) && gsize >= 100)
828  asf->data_object_size = gsize - 24;
829  else
830  asf->data_object_size = (uint64_t)-1;
831  break;
832  }
833  if (gsize < 24)
834  return AVERROR_INVALIDDATA;
835  if (!ff_guidcmp(&g, &ff_asf_file_header)) {
836  ret = asf_read_file_properties(s, gsize);
837  } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
838  ret = asf_read_stream_properties(s, gsize);
839  } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
840  asf_read_content_desc(s, gsize);
841  } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
842  asf_read_language_list(s, gsize);
843  } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
845  } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
846  asf_read_metadata(s, gsize);
847  } else if (!ff_guidcmp(&g, &ff_asf_metadata_library_header)) {
848  asf_read_metadata(s, gsize);
849  } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
851 
852  // there could be an optional stream properties object to follow
853  // if so the next iteration will pick it up
854  continue;
855  } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
856  ff_get_guid(pb, &g);
857  avio_skip(pb, 6);
858  continue;
859  } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
860  asf_read_marker(s, gsize);
861  } else if (avio_feof(pb)) {
862  return AVERROR_EOF;
863  } else {
864  if (!s->keylen) {
866  unsigned int len;
867  AVPacket pkt;
869  "DRM protected stream detected, decoding will likely fail!\n");
870  len= avio_rl32(pb);
871  av_log(s, AV_LOG_DEBUG, "Secret data:\n");
872 
873  if ((ret = av_get_packet(pb, &pkt, len)) < 0)
874  return ret;
877 
878  len= avio_rl32(pb);
879  if (len > UINT16_MAX)
880  return AVERROR_INVALIDDATA;
881  get_tag(s, "ASF_Protection_Type", -1, len, 32);
882 
883  len= avio_rl32(pb);
884  if (len > UINT16_MAX)
885  return AVERROR_INVALIDDATA;
886  get_tag(s, "ASF_Key_ID", -1, len, 32);
887 
888  len= avio_rl32(pb);
889  if (len > UINT16_MAX)
890  return AVERROR_INVALIDDATA;
891  get_tag(s, "ASF_License_URL", -1, len, 32);
892  } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
894  "Ext DRM protected stream detected, decoding will likely fail!\n");
895  av_dict_set(&s->metadata, "encryption", "ASF Extended Content Encryption", 0);
896  } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
897  av_log(s, AV_LOG_INFO, "Digital signature detected!\n");
898  }
899  }
900  }
901  if (ret < 0)
902  return ret;
903 
904  if (avio_tell(pb) != gpos + gsize)
906  "gpos mismatch our pos=%"PRIu64", end=%"PRId64"\n",
907  avio_tell(pb) - gpos, gsize);
908  avio_seek(pb, gpos + gsize, SEEK_SET);
909  }
910  ff_get_guid(pb, &g);
911  avio_rl64(pb);
912  avio_r8(pb);
913  avio_r8(pb);
914  if (avio_feof(pb))
915  return AVERROR_EOF;
916  asf->data_offset = avio_tell(pb);
917  asf->packet_size_left = 0;
918 
919  for (i = 0; i < 128; i++) {
920  int stream_num = asf->asfid2avid[i];
921  if (stream_num >= 0) {
922  AVStream *st = s->streams[stream_num];
923  if (!st->codecpar->bit_rate)
924  st->codecpar->bit_rate = asf->stream_bitrates[i];
925  if (asf->dar[i].num > 0 && asf->dar[i].den > 0) {
928  asf->dar[i].num, asf->dar[i].den, INT_MAX);
929  } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) &&
930  // Use ASF container value if the stream doesn't set AR.
934  asf->dar[0].num, asf->dar[0].den, INT_MAX);
935 
936  av_log(s, AV_LOG_TRACE, "i=%d, st->codecpar->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
937  i, st->codecpar->codec_type, asf->dar[i].num, asf->dar[i].den,
939 
940  // copy and convert language codes to the frontend
941  if (asf->streams[i].stream_language_index < 128) {
942  const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
943  if (rfc1766 && strlen(rfc1766) > 1) {
944  const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
945  const char *iso6392 = ff_convert_lang_to(primary_tag,
947  if (iso6392)
948  av_dict_set(&st->metadata, "language", iso6392, 0);
949  }
950  }
951  }
952  }
953 
955 
956  return 0;
957 }
958 
959 #define DO_2BITS(bits, var, defval) \
960  switch (bits & 3) { \
961  case 3: \
962  var = avio_rl32(pb); \
963  rsize += 4; \
964  break; \
965  case 2: \
966  var = avio_rl16(pb); \
967  rsize += 2; \
968  break; \
969  case 1: \
970  var = avio_r8(pb); \
971  rsize++; \
972  break; \
973  default: \
974  var = defval; \
975  break; \
976  }
977 
978 /**
979  * Load a single ASF packet into the demuxer.
980  * @param s demux context
981  * @param pb context to read data from
982  * @return 0 on success, <0 on error
983  */
985 {
986  ASFContext *asf = s->priv_data;
987  uint32_t packet_length, padsize;
988  int rsize = 8;
989  int c, d, e, off;
990 
991  if (asf->uses_std_ecc > 0) {
992  // if we do not know packet size, allow skipping up to 32 kB
993  off = 32768;
994  if (asf->no_resync_search)
995  off = 3;
996 // else if (s->packet_size > 0 && !asf->uses_std_ecc)
997 // off = (avio_tell(pb) - s->internal->data_offset) % s->packet_size + 3;
998 
999  c = d = e = -1;
1000  while (off-- > 0) {
1001  c = d;
1002  d = e;
1003  e = avio_r8(pb);
1004  if (c == 0x82 && !d && !e)
1005  break;
1006  }
1007 
1008  if (c != 0x82) {
1009  /* This code allows handling of -EAGAIN at packet boundaries (i.e.
1010  * if the packet sync code above triggers -EAGAIN). This does not
1011  * imply complete -EAGAIN handling support at random positions in
1012  * the stream. */
1013  if (pb->error == AVERROR(EAGAIN))
1014  return AVERROR(EAGAIN);
1015  if (!avio_feof(pb))
1017  "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
1018  }
1019  if ((c & 0x8f) == 0x82) {
1020  if (d || e) {
1021  if (!avio_feof(pb))
1022  av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
1023  return AVERROR_INVALIDDATA;
1024  }
1025  c = avio_r8(pb);
1026  d = avio_r8(pb);
1027  rsize += 3;
1028  } else if(!avio_feof(pb)) {
1029  avio_seek(pb, -1, SEEK_CUR); // FIXME
1030  }
1031  } else {
1032  c = avio_r8(pb);
1033  if (c & 0x80) {
1034  rsize ++;
1035  if (!(c & 0x60)) {
1036  d = avio_r8(pb);
1037  e = avio_r8(pb);
1038  avio_seek(pb, (c & 0xF) - 2, SEEK_CUR);
1039  rsize += c & 0xF;
1040  }
1041 
1042  if (c != 0x82)
1043  avpriv_request_sample(s, "Invalid ECC byte");
1044 
1045  if (!asf->uses_std_ecc)
1046  asf->uses_std_ecc = (c == 0x82 && !d && !e) ? 1 : -1;
1047 
1048  c = avio_r8(pb);
1049  } else
1050  asf->uses_std_ecc = -1;
1051  d = avio_r8(pb);
1052  }
1053 
1054  asf->packet_flags = c;
1055  asf->packet_property = d;
1056 
1057  DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
1058  DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
1059  DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
1060 
1061  // the following checks prevent overflows and infinite loops
1062  if (!packet_length || packet_length >= (1U << 29)) {
1064  "invalid packet_length %"PRIu32" at:%"PRId64"\n",
1065  packet_length, avio_tell(pb));
1066  return AVERROR_INVALIDDATA;
1067  }
1068  if (padsize >= packet_length) {
1070  "invalid padsize %"PRIu32" at:%"PRId64"\n", padsize, avio_tell(pb));
1071  return AVERROR_INVALIDDATA;
1072  }
1073 
1074  asf->packet_timestamp = avio_rl32(pb);
1075  avio_rl16(pb); /* duration */
1076  // rsize has at least 11 bytes which have to be present
1077 
1078  if (asf->packet_flags & 0x01) {
1079  asf->packet_segsizetype = avio_r8(pb);
1080  rsize++;
1081  asf->packet_segments = asf->packet_segsizetype & 0x3f;
1082  } else {
1083  asf->packet_segments = 1;
1084  asf->packet_segsizetype = 0x80;
1085  }
1086  if (rsize > packet_length - padsize) {
1087  asf->packet_size_left = 0;
1089  "invalid packet header length %d for pktlen %"PRIu32"-%"PRIu32" at %"PRId64"\n",
1090  rsize, packet_length, padsize, avio_tell(pb));
1091  return AVERROR_INVALIDDATA;
1092  }
1093  asf->packet_size_left = packet_length - padsize - rsize;
1094  if (packet_length < asf->hdr.min_pktsize)
1095  padsize += asf->hdr.min_pktsize - packet_length;
1096  asf->packet_padsize = padsize;
1097  av_log(s, AV_LOG_TRACE, "packet: size=%d padsize=%d left=%d\n",
1098  s->packet_size, asf->packet_padsize, asf->packet_size_left);
1099  return 0;
1100 }
1101 
1102 /**
1103  *
1104  * @return <0 if error
1105  */
1107 {
1108  ASFContext *asf = s->priv_data;
1109  ASFStream *asfst;
1110  int rsize = 1;
1111  int num = avio_r8(pb);
1112  int i;
1113  int64_t ts0, ts1 av_unused;
1114 
1115  asf->packet_segments--;
1116  asf->packet_key_frame = num >> 7;
1117  asf->stream_index = asf->asfid2avid[num & 0x7f];
1118  asfst = &asf->streams[num & 0x7f];
1119  // sequence should be ignored!
1120  DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
1121  DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
1123  av_log(asf, AV_LOG_TRACE, "key:%d stream:%d seq:%d offset:%d replic_size:%d num:%X packet_property %X\n",
1124  asf->packet_key_frame, asf->stream_index, asf->packet_seq,
1125  asf->packet_frag_offset, asf->packet_replic_size, num, asf->packet_property);
1126  if (rsize+(int64_t)asf->packet_replic_size > asf->packet_size_left) {
1127  av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
1128  return AVERROR_INVALIDDATA;
1129  }
1130  if (asf->packet_replic_size >= 8) {
1131  int64_t end = avio_tell(pb) + asf->packet_replic_size;
1132  AVRational aspect;
1133  asfst->packet_obj_size = avio_rl32(pb);
1134  if (asfst->packet_obj_size >= (1 << 24) || asfst->packet_obj_size < 0) {
1135  av_log(s, AV_LOG_ERROR, "packet_obj_size %d invalid\n", asfst->packet_obj_size);
1136  asfst->packet_obj_size = 0;
1137  return AVERROR_INVALIDDATA;
1138  }
1139  asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
1140 
1141  for (i = 0; i < asfst->payload_ext_ct; i++) {
1142  ASFPayload *p = &asfst->payload[i];
1143  int size = p->size;
1144  int64_t payend;
1145  if (size == 0xFFFF)
1146  size = avio_rl16(pb);
1147  payend = avio_tell(pb) + size;
1148  if (payend > end) {
1149  av_log(s, AV_LOG_ERROR, "too long payload\n");
1150  break;
1151  }
1152  switch (p->type) {
1153  case 0x50:
1154 // duration = avio_rl16(pb);
1155  break;
1156  case 0x54:
1157  aspect.num = avio_r8(pb);
1158  aspect.den = avio_r8(pb);
1159  if (aspect.num > 0 && aspect.den > 0 && asf->stream_index >= 0) {
1160  s->streams[asf->stream_index]->sample_aspect_ratio = aspect;
1161  }
1162  break;
1163  case 0x2A:
1164  avio_skip(pb, 8);
1165  ts0 = avio_rl64(pb);
1166  ts1 = avio_rl64(pb);
1167  if (ts0!= -1) asf->packet_frag_timestamp = ts0/10000;
1169  asf->ts_is_pts = 1;
1170  break;
1171  case 0x5B:
1172  case 0xB7:
1173  case 0xCC:
1174  case 0xC0:
1175  case 0xA0:
1176  //unknown
1177  break;
1178  }
1179  avio_seek(pb, payend, SEEK_SET);
1180  }
1181 
1182  avio_seek(pb, end, SEEK_SET);
1183  rsize += asf->packet_replic_size; // FIXME - check validity
1184  } else if (asf->packet_replic_size == 1) {
1185  // multipacket - frag_offset is beginning timestamp
1187  asf->packet_frag_offset = 0;
1189 
1190  asf->packet_time_delta = avio_r8(pb);
1191  rsize++;
1192  } else if (asf->packet_replic_size != 0) {
1193  av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n",
1194  asf->packet_replic_size);
1195  return AVERROR_INVALIDDATA;
1196  }
1197  if (asf->packet_flags & 0x01) {
1198  DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
1199  if (rsize > asf->packet_size_left) {
1200  av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
1201  return AVERROR_INVALIDDATA;
1202  } else if (asf->packet_frag_size > asf->packet_size_left - rsize) {
1203  if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
1204  av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d>%d-%d+%d)\n",
1205  asf->packet_frag_size, asf->packet_size_left, rsize, asf->packet_padsize);
1206  return AVERROR_INVALIDDATA;
1207  } else {
1208  int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
1209  asf->packet_size_left += diff;
1210  asf->packet_padsize -= diff;
1211  }
1212  }
1213  } else {
1214  asf->packet_frag_size = asf->packet_size_left - rsize;
1215  }
1216  if (asf->packet_replic_size == 1) {
1217  asf->packet_multi_size = asf->packet_frag_size;
1218  if (asf->packet_multi_size > asf->packet_size_left)
1219  return AVERROR_INVALIDDATA;
1220  }
1221  asf->packet_size_left -= rsize;
1222 
1223  return 0;
1224 }
1225 
1226 /**
1227  * Parse data from individual ASF packets (which were previously loaded
1228  * with asf_get_packet()).
1229  * @param s demux context
1230  * @param pb context to read data from
1231  * @param pkt pointer to store packet data into
1232  * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
1233  * packets need to be loaded (through asf_get_packet())
1234  */
1236 {
1237  ASFContext *asf = s->priv_data;
1238  ASFStream *asf_st = 0;
1239  for (;;) {
1240  int ret;
1241  if (avio_feof(pb))
1242  return AVERROR_EOF;
1243  if (asf->packet_size_left < FRAME_HEADER_SIZE ||
1244  asf->packet_segments < 1 && asf->packet_time_start == 0) {
1245  int ret = asf->packet_size_left + asf->packet_padsize;
1246 
1248  av_log(s, AV_LOG_WARNING, "Skip due to FRAME_HEADER_SIZE\n");
1249 
1250  assert(ret >= 0);
1251  /* fail safe */
1252  avio_skip(pb, ret);
1253 
1254  asf->packet_pos = avio_tell(pb);
1255  if (asf->data_object_size != (uint64_t)-1 &&
1256  (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
1257  return AVERROR_EOF; /* Do not exceed the size of the data object */
1258  return 1;
1259  }
1260  if (asf->packet_time_start == 0) {
1261  if (asf_read_frame_header(s, pb) < 0) {
1262  asf->packet_time_start = asf->packet_segments = 0;
1263  continue;
1264  }
1265  if (asf->stream_index < 0 ||
1266  s->streams[asf->stream_index]->discard >= AVDISCARD_ALL ||
1267  (!asf->packet_key_frame &&
1268  (s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY || asf->streams[s->streams[asf->stream_index]->id].skip_to_key))) {
1269  asf->packet_time_start = 0;
1270  /* unhandled packet (should not happen) */
1271  avio_skip(pb, asf->packet_frag_size);
1272  asf->packet_size_left -= asf->packet_frag_size;
1273  if (asf->stream_index < 0)
1274  av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n",
1275  asf->packet_frag_size);
1276  continue;
1277  }
1278  asf->asf_st = &asf->streams[s->streams[asf->stream_index]->id];
1279  if (!asf->packet_frag_offset)
1280  asf->asf_st->skip_to_key = 0;
1281  }
1282  asf_st = asf->asf_st;
1283  av_assert0(asf_st);
1284 
1285  if (!asf_st->frag_offset && asf->packet_frag_offset) {
1286  av_log(s, AV_LOG_TRACE, "skipping asf data pkt with fragment offset for "
1287  "stream:%d, expected:%d but got %d from pkt)\n",
1288  asf->stream_index, asf_st->frag_offset,
1289  asf->packet_frag_offset);
1290  avio_skip(pb, asf->packet_frag_size);
1291  asf->packet_size_left -= asf->packet_frag_size;
1292  continue;
1293  }
1294 
1295  if (asf->packet_replic_size == 1) {
1296  // frag_offset is here used as the beginning timestamp
1298  asf->packet_time_start += asf->packet_time_delta;
1299  asf_st->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
1300  asf->packet_size_left--;
1301  asf->packet_multi_size--;
1302  if (asf->packet_multi_size < asf_st->packet_obj_size) {
1303  asf->packet_time_start = 0;
1304  avio_skip(pb, asf->packet_multi_size);
1305  asf->packet_size_left -= asf->packet_multi_size;
1306  continue;
1307  }
1308  asf->packet_multi_size -= asf_st->packet_obj_size;
1309  }
1310 
1311  if (asf_st->pkt.size != asf_st->packet_obj_size ||
1312  // FIXME is this condition sufficient?
1313  asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
1314  int ret;
1315 
1316  if (asf_st->pkt.data) {
1317  av_log(s, AV_LOG_INFO,
1318  "freeing incomplete packet size %d, new %d\n",
1319  asf_st->pkt.size, asf_st->packet_obj_size);
1320  asf_st->frag_offset = 0;
1321  av_packet_unref(&asf_st->pkt);
1322  }
1323  /* new packet */
1324  if ((ret = av_new_packet(&asf_st->pkt, asf_st->packet_obj_size)) < 0)
1325  return ret;
1326  asf_st->seq = asf->packet_seq;
1327  if (asf->packet_frag_timestamp != AV_NOPTS_VALUE) {
1328  if (asf->ts_is_pts) {
1329  asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll;
1330  } else
1331  asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
1332  }
1333  asf_st->pkt.stream_index = asf->stream_index;
1334  asf_st->pkt.pos = asf_st->packet_pos = asf->packet_pos;
1335  asf_st->pkt_clean = 0;
1336 
1337  if (asf_st->pkt.data && asf_st->palette_changed) {
1338  uint8_t *pal;
1340  AVPALETTE_SIZE);
1341  if (!pal) {
1342  av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
1343  } else {
1344  memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
1345  asf_st->palette_changed = 0;
1346  }
1347  }
1348  av_log(asf, AV_LOG_TRACE, "new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
1349  asf->stream_index, asf->packet_key_frame,
1350  asf_st->pkt.flags & AV_PKT_FLAG_KEY,
1351  s->streams[asf->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO,
1352  asf_st->packet_obj_size);
1353  if (s->streams[asf->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
1354  asf->packet_key_frame = 1;
1355  if (asf->packet_key_frame)
1356  asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
1357  }
1358 
1359  /* read data */
1360  av_log(asf, AV_LOG_TRACE, "READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
1361  s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
1362  asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
1363  asf->packet_size_left -= asf->packet_frag_size;
1364  if (asf->packet_size_left < 0)
1365  continue;
1366 
1367  if (asf->packet_frag_offset >= asf_st->pkt.size ||
1368  asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset) {
1370  "packet fragment position invalid %u,%u not in %u\n",
1372  asf_st->pkt.size);
1373  continue;
1374  }
1375 
1376  if (asf->packet_frag_offset != asf_st->frag_offset && !asf_st->pkt_clean) {
1377  memset(asf_st->pkt.data + asf_st->frag_offset, 0, asf_st->pkt.size - asf_st->frag_offset);
1378  asf_st->pkt_clean = 1;
1379  }
1380 
1381  ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
1382  asf->packet_frag_size);
1383  if (ret != asf->packet_frag_size) {
1384  if (ret < 0 || asf->packet_frag_offset + ret == 0)
1385  return ret < 0 ? ret : AVERROR_EOF;
1386 
1387  if (asf_st->ds_span > 1) {
1388  // scrambling, we can either drop it completely or fill the remainder
1389  // TODO: should we fill the whole packet instead of just the current
1390  // fragment?
1391  memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
1392  asf->packet_frag_size - ret);
1393  ret = asf->packet_frag_size;
1394  } else {
1395  // no scrambling, so we can return partial packets
1396  av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
1397  }
1398  }
1399  if (s->key && s->keylen == 20)
1400  ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
1401  ret);
1402  asf_st->frag_offset += ret;
1403  /* test if whole packet is read */
1404  if (asf_st->frag_offset == asf_st->pkt.size) {
1405  // workaround for macroshit radio DVR-MS files
1406  if (s->streams[asf->stream_index]->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
1407  asf_st->pkt.size > 100) {
1408  int i;
1409  for (i = 0; i < asf_st->pkt.size && !asf_st->pkt.data[i]; i++)
1410  ;
1411  if (i == asf_st->pkt.size) {
1412  av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1413  asf_st->frag_offset = 0;
1414  av_packet_unref(&asf_st->pkt);
1415  continue;
1416  }
1417  }
1418 
1419  /* return packet */
1420  if (asf_st->ds_span > 1) {
1421  if (asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span) {
1423  "pkt.size != ds_packet_size * ds_span (%d %d %d)\n",
1424  asf_st->pkt.size, asf_st->ds_packet_size,
1425  asf_st->ds_span);
1426  } else {
1427  /* packet descrambling */
1428  AVBufferRef *buf = av_buffer_alloc(asf_st->pkt.size +
1430  if (buf) {
1431  uint8_t *newdata = buf->data;
1432  int offset = 0;
1433  memset(newdata + asf_st->pkt.size, 0,
1435  while (offset < asf_st->pkt.size) {
1436  int off = offset / asf_st->ds_chunk_size;
1437  int row = off / asf_st->ds_span;
1438  int col = off % asf_st->ds_span;
1439  int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1440  assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
1441  assert(idx + 1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
1442  memcpy(newdata + offset,
1443  asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1444  asf_st->ds_chunk_size);
1445  offset += asf_st->ds_chunk_size;
1446  }
1447  av_buffer_unref(&asf_st->pkt.buf);
1448  asf_st->pkt.buf = buf;
1449  asf_st->pkt.data = buf->data;
1450  }
1451  }
1452  }
1453  asf_st->frag_offset = 0;
1454  *pkt = asf_st->pkt;
1455  asf_st->pkt.buf = 0;
1456  asf_st->pkt.size = 0;
1457  asf_st->pkt.data = 0;
1458  asf_st->pkt.side_data_elems = 0;
1459  asf_st->pkt.side_data = NULL;
1460  break; // packet completed
1461  }
1462  }
1463  return 0;
1464 }
1465 
1467 {
1468  ASFContext *asf = s->priv_data;
1469 
1470  for (;;) {
1471  int ret;
1472 
1473  /* parse cached packets, if any */
1474  if ((ret = asf_parse_packet(s, s->pb, pkt)) <= 0)
1475  return ret;
1476  if ((ret = asf_get_packet(s, s->pb)) < 0)
1477  assert(asf->packet_size_left < FRAME_HEADER_SIZE ||
1478  asf->packet_segments < 1);
1479  asf->packet_time_start = 0;
1480  }
1481 }
1482 
1483 // Added to support seeking after packets have been read
1484 // If information is not reset, read_packet fails due to
1485 // leftover information from previous reads
1487 {
1488  ASFContext *asf = s->priv_data;
1489  ASFStream *asf_st;
1490  int i;
1491 
1492  asf->packet_size_left = 0;
1493  asf->packet_flags = 0;
1494  asf->packet_property = 0;
1495  asf->packet_timestamp = 0;
1496  asf->packet_segsizetype = 0;
1497  asf->packet_segments = 0;
1498  asf->packet_seq = 0;
1499  asf->packet_replic_size = 0;
1500  asf->packet_key_frame = 0;
1501  asf->packet_padsize = 0;
1502  asf->packet_frag_offset = 0;
1503  asf->packet_frag_size = 0;
1504  asf->packet_frag_timestamp = 0;
1505  asf->packet_multi_size = 0;
1506  asf->packet_time_delta = 0;
1507  asf->packet_time_start = 0;
1508 
1509  for (i = 0; i < 128; i++) {
1510  asf_st = &asf->streams[i];
1511  av_packet_unref(&asf_st->pkt);
1512  asf_st->packet_obj_size = 0;
1513  asf_st->frag_offset = 0;
1514  asf_st->seq = 0;
1515  }
1516  asf->asf_st = NULL;
1517 }
1518 
1520 {
1521  ASFContext *asf = s->priv_data;
1522  int i;
1523 
1524  for (i = 0; i < 128; i++) {
1525  int j = asf->asfid2avid[i];
1526  ASFStream *asf_st = &asf->streams[i];
1527  if (j < 0 || s->streams[j]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
1528  continue;
1529 
1530  asf_st->skip_to_key = 1;
1531  }
1532 }
1533 
1535 {
1537 
1538  return 0;
1539 }
1540 
1541 static int64_t asf_read_pts(AVFormatContext *s, int stream_index,
1542  int64_t *ppos, int64_t pos_limit)
1543 {
1544  ASFContext *asf = s->priv_data;
1545  AVPacket pkt1, *pkt = &pkt1;
1546  ASFStream *asf_st;
1547  int64_t pts;
1548  int64_t pos = *ppos;
1549  int i;
1550  int64_t start_pos[FF_ARRAY_ELEMS(asf->streams)];
1551 
1552  for (i = 0; i < FF_ARRAY_ELEMS(start_pos); i++)
1553  start_pos[i] = pos;
1554 
1555  if (s->packet_size > 0)
1556  pos = (pos + s->packet_size - 1 - s->internal->data_offset) /
1557  s->packet_size * s->packet_size +
1558  s->internal->data_offset;
1559  *ppos = pos;
1560  if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1561  return AV_NOPTS_VALUE;
1562 
1565  for (;;) {
1566  if (av_read_frame(s, pkt) < 0) {
1567  av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
1568  return AV_NOPTS_VALUE;
1569  }
1570 
1571  pts = pkt->dts;
1572 
1573  if (pkt->flags & AV_PKT_FLAG_KEY) {
1574  AVStream *st = s->streams[pkt->stream_index];
1575 
1576  asf_st = &asf->streams[st->id];
1577 
1578 // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
1579  pos = asf_st->packet_pos;
1580  av_assert1(pkt->pos == asf_st->packet_pos);
1581 
1583  pos - start_pos[st->id] + 1, AVINDEX_KEYFRAME);
1584  start_pos[st->id] = asf_st->packet_pos + 1;
1585 
1586  if (pkt->stream_index == stream_index) {
1588  break;
1589  }
1590  }
1592  }
1593 
1594  *ppos = pos;
1595  return pts;
1596 }
1597 
1598 static int asf_build_simple_index(AVFormatContext *s, int stream_index)
1599 {
1600  ff_asf_guid g;
1601  ASFContext *asf = s->priv_data;
1602  int64_t current_pos = avio_tell(s->pb);
1603  int64_t ret;
1604 
1605  if((ret = avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET)) < 0) {
1606  return ret;
1607  }
1608 
1609  if ((ret = ff_get_guid(s->pb, &g)) < 0)
1610  goto end;
1611 
1612  /* the data object can be followed by other top-level objects,
1613  * skip them until the simple index object is reached */
1615  int64_t gsize = avio_rl64(s->pb);
1616  if (gsize < 24 || avio_feof(s->pb)) {
1617  goto end;
1618  }
1619  avio_skip(s->pb, gsize - 24);
1620  if ((ret = ff_get_guid(s->pb, &g)) < 0)
1621  goto end;
1622  }
1623 
1624  {
1625  int64_t itime, last_pos = -1;
1626  int pct, ict;
1627  int i;
1628  int64_t av_unused gsize = avio_rl64(s->pb);
1629  if ((ret = ff_get_guid(s->pb, &g)) < 0)
1630  goto end;
1631  itime = avio_rl64(s->pb);
1632  pct = avio_rl32(s->pb);
1633  ict = avio_rl32(s->pb);
1635  "itime:0x%"PRIx64", pct:%d, ict:%d\n", itime, pct, ict);
1636 
1637  for (i = 0; i < ict; i++) {
1638  int pktnum = avio_rl32(s->pb);
1639  int pktct = avio_rl16(s->pb);
1640  int64_t pos = s->internal->data_offset + s->packet_size * (int64_t)pktnum;
1641  int64_t index_pts = FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
1642 
1643  if (avio_feof(s->pb)) {
1644  ret = AVERROR_INVALIDDATA;
1645  goto end;
1646  }
1647 
1648  if (pos != last_pos) {
1649  av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n",
1650  pktnum, pktct, index_pts);
1651  av_add_index_entry(s->streams[stream_index], pos, index_pts,
1652  s->packet_size, 0, AVINDEX_KEYFRAME);
1653  last_pos = pos;
1654  }
1655  }
1656  asf->index_read = ict > 1;
1657  }
1658 end:
1659 // if (avio_feof(s->pb)) {
1660 // ret = 0;
1661 // }
1662  avio_seek(s->pb, current_pos, SEEK_SET);
1663  return ret;
1664 }
1665 
1666 static int asf_read_seek(AVFormatContext *s, int stream_index,
1667  int64_t pts, int flags)
1668 {
1669  ASFContext *asf = s->priv_data;
1670  AVStream *st = s->streams[stream_index];
1671  int ret = 0;
1672 
1673  if (s->packet_size <= 0)
1674  return -1;
1675 
1676  /* Try using the protocol's read_seek if available */
1677  if (s->pb) {
1678  int64_t ret = avio_seek_time(s->pb, stream_index, pts, flags);
1679  if (ret >= 0)
1681  if (ret != AVERROR(ENOSYS))
1682  return ret;
1683  }
1684 
1685  /* explicitly handle the case of seeking to 0 */
1686  if (!pts) {
1688  avio_seek(s->pb, s->internal->data_offset, SEEK_SET);
1689  return 0;
1690  }
1691 
1692  if (!asf->index_read) {
1693  ret = asf_build_simple_index(s, stream_index);
1694  if (ret < 0)
1695  asf->index_read = -1;
1696  }
1697 
1698  if (asf->index_read > 0 && st->index_entries) {
1700  if (index >= 0) {
1701  /* find the position */
1702  uint64_t pos = st->index_entries[index].pos;
1703 
1704  /* do the seek */
1705  av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1706  if(avio_seek(s->pb, pos, SEEK_SET) < 0)
1707  return -1;
1709  skip_to_key(s);
1710  return 0;
1711  }
1712  }
1713  /* no index or seeking by index failed */
1714  if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1715  return -1;
1717  skip_to_key(s);
1718  return 0;
1719 }
1720 
1722  .name = "asf",
1723  .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1724  .priv_data_size = sizeof(ASFContext),
1725  .read_probe = asf_probe,
1730  .read_timestamp = asf_read_pts,
1732  .priv_class = &asf_class,
1733 };
const ff_asf_guid ff_asf_video_conceal_none
Definition: asf.c:55
const ff_asf_guid ff_asf_codec_comment1_header
Definition: asf.c:70
const ff_asf_guid ff_asf_video_stream
Definition: asf.c:47
const ff_asf_guid ff_asf_ext_stream_header
Definition: asf.c:35
const AVMetadataConv ff_asf_metadata_conv[]
Definition: asf.c:153
const ff_asf_guid ff_asf_stream_header
Definition: asf.c:31
const ff_asf_guid ff_asf_command_stream
Definition: asf.c:59
const ff_asf_guid ff_asf_ext_stream_audio_stream
Definition: asf.c:98
const ff_asf_guid ff_asf_language_guid
Definition: asf.c:124
const ff_asf_guid ff_asf_header
Definition: asf.c:23
const ff_asf_guid ff_asf_audio_stream
Definition: asf.c:39
const ff_asf_guid ff_asf_simple_index_header
Definition: asf.c:90
const ff_asf_guid ff_asf_ext_stream_embed_stream_header
Definition: asf.c:94
const ff_asf_guid ff_asf_content_encryption
Definition: asf.c:128
const ff_asf_guid ff_asf_extended_content_header
Definition: asf.c:86
const ff_asf_guid ff_asf_head1_guid
Definition: asf.c:78
const ff_asf_guid ff_asf_head2_guid
Definition: asf.c:82
const ff_asf_guid ff_asf_marker_header
Definition: asf.c:110
const ff_asf_guid ff_asf_metadata_library_header
Definition: asf.c:106
const ff_asf_guid ff_asf_data_header
Definition: asf.c:74
const ff_asf_guid ff_asf_jfif_media
Definition: asf.c:51
const ff_asf_guid ff_asf_digital_signature
Definition: asf.c:136
const ff_asf_guid ff_asf_ext_content_encryption
Definition: asf.c:132
const ff_asf_guid ff_asf_codec_comment_header
Definition: asf.c:67
const ff_asf_guid ff_asf_my_guid
Definition: asf.c:120
const ff_asf_guid ff_asf_file_header
Definition: asf.c:27
const ff_asf_guid ff_asf_comment_header
Definition: asf.c:63
const ff_asf_guid ff_asf_metadata_header
Definition: asf.c:102
@ ASF_UNICODE
Definition: asf.h:30
@ ASF_BOOL
Definition: asf.h:32
@ ASF_QWORD
Definition: asf.h:34
@ ASF_BYTE_ARRAY
Definition: asf.h:31
@ ASF_DWORD
Definition: asf.h:33
@ ASF_WORD
Definition: asf.h:35
@ ASF_GUID
Definition: asf.h:36
void ff_asfcrypt_dec(const uint8_t key[20], uint8_t *data, int len)
Definition: asfcrypt.c:147
#define LEN
static int asf_read_picture(AVFormatContext *s, int len)
Definition: asfdec_f.c:224
static void get_id3_tag(AVFormatContext *s, int len)
Definition: asfdec_f.c:311
static int asf_read_stream_properties(AVFormatContext *s, int64_t size)
Definition: asfdec_f.c:406
static int asf_read_frame_header(AVFormatContext *s, AVIOContext *pb)
Definition: asfdec_f.c:1106
static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
Definition: asfdec_f.c:1541
static void asf_reset_header(AVFormatContext *s)
Definition: asfdec_f.c:1486
static const AVOption options[]
Definition: asfdec_f.c:120
static int asf_read_header(AVFormatContext *s)
Definition: asfdec_f.c:797
static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: asfdec_f.c:1466
#define print_guid(g)
Definition: asfdec_f.c:192
static int asf_read_marker(AVFormatContext *s, int64_t size)
Definition: asfdec_f.c:757
static int asf_read_language_list(AVFormatContext *s, int64_t size)
Definition: asfdec_f.c:688
static void get_tag(AVFormatContext *s, const char *key, int type, int len, int type2_size)
Definition: asfdec_f.c:323
static const AVClass asf_class
Definition: asfdec_f.c:126
#define DO_2BITS(bits, var, defval)
Definition: asfdec_f.c:959
#define FRAME_HEADER_SIZE
Definition: asfdec_f.c:137
static int asf_build_simple_index(AVFormatContext *s, int stream_index)
Definition: asfdec_f.c:1598
static int asf_parse_packet(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
Parse data from individual ASF packets (which were previously loaded with asf_get_packet()).
Definition: asfdec_f.c:1235
static int get_value(AVIOContext *pb, int type, int type2_size)
Definition: asfdec_f.c:206
static void skip_to_key(AVFormatContext *s)
Definition: asfdec_f.c:1519
static int asf_get_packet(AVFormatContext *s, AVIOContext *pb)
Load a single ASF packet into the demuxer.
Definition: asfdec_f.c:984
static int asf_probe(const AVProbeData *pd)
Definition: asfdec_f.c:195
static int asf_read_ext_stream_properties(AVFormatContext *s, int64_t size)
Definition: asfdec_f.c:575
static int asf_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition: asfdec_f.c:1666
static int asf_read_ext_content_desc(AVFormatContext *s, int64_t size)
Definition: asfdec_f.c:654
static int asf_read_close(AVFormatContext *s)
Definition: asfdec_f.c:1534
static int asf_read_metadata(AVFormatContext *s, int64_t size)
Definition: asfdec_f.c:708
#define ASF_MAX_STREAMS
Definition: asfdec_f.c:136
static int asf_read_file_properties(AVFormatContext *s, int64_t size)
Definition: asfdec_f.c:382
AVInputFormat ff_asf_demuxer
Definition: asfdec_f.c:1721
static int asf_read_content_desc(AVFormatContext *s, int64_t size)
Definition: asfdec_f.c:635
Macro definitions for various function/variable attributes.
#define av_unused
Definition: attributes.h:131
uint8_t
static int64_t fsize(FILE *f)
Definition: audiomatch.c:29
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition: avformat.h:811
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition: avformat.h:469
#define AVFMT_NOBINSEARCH
Format does not allow to fall back on binary search via read_timestamp.
Definition: avformat.h:468
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
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:841
@ AVSTREAM_PARSE_FULL_ONCE
full parsing and repack of the first frame only, only implemented for H.264 currently
Definition: avformat.h:797
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition: avformat.h:794
@ AVSTREAM_PARSE_NONE
Definition: avformat.h:793
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:349
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 avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:371
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:750
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:344
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:645
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:766
int64_t avio_seek_time(AVIOContext *h, int stream_index, int64_t timestamp, int flags)
Seek to a given timestamp relative to some component stream.
Definition: aviobuf.c:1248
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:636
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:774
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:698
int ffio_limit(AVIOContext *s, int size)
Definition: utils.c:244
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
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
@ AV_LANG_ISO639_2_BIBL
Definition: avlanguage.h:28
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t size)
Definition: avpacket.c:343
#define av_bswap32
Definition: bswap.h:33
byte swapping routines
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define fail()
Definition: checkasm.h:133
common internal and external API header
#define FFMIN(a, b)
Definition: common.h:105
#define MKTAG(a, b, c, d)
Definition: common.h:478
#define av_sat_sub64
Definition: common.h:167
#define FFMAX(a, b)
Definition: common.h:103
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
Public dictionary API.
double value
Definition: eval.c:100
enum AVCodecID id
static int64_t start_time
Definition: ffplay.c:332
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:555
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
@ AV_CODEC_ID_H264
Definition: codec_id.h:76
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
@ AV_CODEC_ID_AAC
Definition: codec_id.h:426
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:61
@ AV_CODEC_ID_MJPEG
Definition: codec_id.h:56
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:51
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition: avcodec.h:215
@ AVDISCARD_ALL
discard all
Definition: avcodec.h:236
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition: avcodec.h:235
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:114
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition: packet.h:46
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4508
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1741
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:2013
void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
Send a nice hexadecimal dump of a buffer to the log.
Definition: dump.c:81
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:2130
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:125
AVBufferRef * av_buffer_alloc(buffer_size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
#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:74
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
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:220
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
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
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
AVMediaType
Definition: avutil.h:199
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int index
Definition: gxfenc.c:89
for(j=16;j >0;--j)
cl_device_type type
const char * key
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:1122
const char *const ff_id3v2_picture_types[21]
Definition: id3v2.c:107
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition: id3v2.c:1128
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:1144
int ff_id3v2_parse_chapters(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Create chapters for all CHAP tags found in the ID3v2 header.
Definition: id3v2.c:1184
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:131
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
int i
Definition: input.c:407
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:2145
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:4944
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition: utils.c:1892
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:4638
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:3131
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:26
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
const char * desc
Definition: libsvtav1.c:83
AVOptions.
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:279
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
const char * name
Definition: qsvenc.c:46
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:33
internal header for RIFF based (de)muxers do NOT include this in end user applications
static av_always_inline int ff_guidcmp(const void *g1, const void *g2)
Definition: riff.h:122
int ff_get_guid(AVIOContext *s, ff_asf_guid *g)
Definition: riffdec.c:32
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size, int big_endian)
Definition: riffdec.c:91
uint8_t ff_asf_guid[16]
Definition: riff.h:96
#define FF_ARRAY_ELEMS(a)
#define snprintf
Definition: snprintf.h:34
unsigned int pos
Definition: spdifenc.c:412
char stream_languages[128][6]
max number of streams, language for each (RFC1766, e.g. en-US)
Definition: asfdec_f.c:80
unsigned int packet_frag_size
Definition: asfdec_f.c:102
int packet_padsize
Definition: asfdec_f.c:100
ASFStream * asf_st
currently decoded stream
Definition: asfdec_f.c:112
int packet_multi_size
Definition: asfdec_f.c:105
ASFStream streams[128]
it's max number and it's not that big
Definition: asfdec_f.c:77
int export_xmp
Definition: asfdec_f.c:115
int packet_replic_size
Definition: asfdec_f.c:98
AVRational dar[128]
Definition: asfdec_f.c:79
int packet_key_frame
Definition: asfdec_f.c:99
int64_t packet_frag_timestamp
Definition: asfdec_f.c:103
int packet_property
Definition: asfdec_f.c:93
uint64_t data_offset
beginning of the first data packet
Definition: asfdec_f.c:85
int packet_flags
Definition: asfdec_f.c:92
ASFMainHeader hdr
Definition: asfdec_f.c:90
uint64_t data_object_offset
data object offset (excl. GUID & size)
Definition: asfdec_f.c:86
int asfid2avid[128]
conversion table from asf ID 2 AVStream ID
Definition: asfdec_f.c:76
int packet_size_left
Definition: asfdec_f.c:83
int packet_timestamp
Definition: asfdec_f.c:94
int index_read
Definition: asfdec_f.c:88
int packet_seq
Definition: asfdec_f.c:97
int stream_index
Definition: asfdec_f.c:110
int64_t packet_time_start
Definition: asfdec_f.c:107
int no_resync_search
Definition: asfdec_f.c:114
uint32_t stream_bitrates[128]
max number of streams, bitrate for each (for streaming)
Definition: asfdec_f.c:78
int ts_is_pts
Definition: asfdec_f.c:104
int packet_segsizetype
Definition: asfdec_f.c:95
unsigned int packet_frag_offset
Definition: asfdec_f.c:101
uint64_t data_object_size
size of the data object
Definition: asfdec_f.c:87
int64_t packet_pos
Definition: asfdec_f.c:108
int uses_std_ecc
Definition: asfdec_f.c:117
int packet_time_delta
Definition: asfdec_f.c:106
int packet_segments
Definition: asfdec_f.c:96
uint64_t create_time
time of creation, in 100-nanosecond units since 1.1.1601 invalid if broadcasting
Definition: asf.h:43
uint32_t min_pktsize
size of a data packet invalid if broadcasting
Definition: asf.h:55
uint32_t ignore
preroll is 64 bits - but let's just ignore it
Definition: asf.h:51
ff_asf_guid guid
generated by client computer
Definition: asf.h:40
uint32_t max_pktsize
shall be the same as for min_pktsize invalid if broadcasting
Definition: asf.h:57
uint64_t send_time
time to send file, in 100-nanosecond units invalid if broadcasting (could be ignored)
Definition: asf.h:47
uint32_t max_bitrate
bandwidth of stream in bps should be the sum of bitrates of the individual media streams
Definition: asf.h:59
uint32_t flags
0x01 - broadcast 0x02 - seekable rest is reserved should be 0
Definition: asf.h:52
uint64_t file_size
in bytes invalid if broadcasting
Definition: asf.h:41
uint64_t play_time
play time, in 100-nanosecond units invalid if broadcasting
Definition: asf.h:45
uint32_t preroll
timestamp of the first packet, in milliseconds if nonzero - subtract from time
Definition: asf.h:49
uint8_t type
Definition: asfdec_f.c:43
uint16_t size
Definition: asfdec_f.c:44
int payload_ext_ct
Definition: asfdec_f.c:70
unsigned char seq
Definition: asfdec_f.c:49
AVPacket pkt
Definition: asfdec_f.c:51
int ds_packet_size
Definition: asfdec_f.c:60
uint16_t stream_language_index
Definition: asfdec_f.c:65
ASFPayload payload[8]
Definition: asfdec_f.c:71
int skip_to_key
Definition: asfdec_f.c:56
int frag_offset
Definition: asfdec_f.c:52
int ds_span
Definition: asfdec_f.c:59
int64_t packet_pos
Definition: asfdec_f.c:63
int ds_chunk_size
Definition: asfdec_f.c:61
uint32_t palette[256]
Definition: asfdec_f.c:68
int64_t duration
Definition: asfdec_f.c:55
int num
Definition: asfdec_f.c:48
int pkt_clean
Definition: asfdec_f.c:57
int palette_changed
Definition: asfdec_f.c:67
int packet_obj_size
Definition: asfdec_f.c:53
int timestamp
Definition: asfdec_f.c:54
A reference to a data buffer.
Definition: buffer.h:84
uint8_t * data
The data buffer.
Definition: buffer.h:92
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
int width
Video only.
Definition: codec_par.h:126
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
Format I/O context.
Definition: avformat.h:1232
Bytestream IO Context.
Definition: avio.h:161
int error
contains the error code or 0 if no error happened
Definition: avio.h:245
int64_t pos
Definition: avformat.h:804
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:352
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:368
uint8_t * data
Definition: packet.h:369
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition: packet.h:380
int side_data_elems
Definition: packet.h:381
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
int request_probe
stream probing state -1 -> probing finished 0 -> no probing requested rest -> perform probing with re...
Definition: internal.h:248
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:935
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:922
AVDictionary * metadata
Definition: avformat.h:937
int id
Format-specific stream ID.
Definition: avformat.h:880
int index
stream index in AVFormatContext
Definition: avformat.h:874
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:955
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1090
enum AVStreamParseType need_parsing
Definition: avformat.h:1081
AVStreamInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1113
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:926
char str[32]
Definition: internal.h:48
enum AVCodecID id
Definition: internal.h:49
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
static void finish(void)
Definition: movenc.c:342
static int64_t pts
int size
const char * g
Definition: vf_curves.c:118
if(ret< 0)
Definition: vf_mcdeint.c:282
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
int len
static double c[64]