FFmpeg  4.4.7
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, value_len;
713  int ret, i;
714  n = avio_rl16(pb);
715 
716  for (i = 0; i < n; i++) {
717  uint8_t *name;
718  int value_type;
719 
720  avio_rl16(pb); // lang_list_index
721  stream_num = avio_rl16(pb);
722  name_len_utf16 = avio_rl16(pb);
723  value_type = avio_rl16(pb); /* value_type */
724  value_len = avio_rl32(pb);
725 
726  if (value_len < 0 || value_len > UINT16_MAX)
727  return AVERROR_INVALIDDATA;
728 
729  name_len_utf8 = 2*name_len_utf16 + 1;
730  name = av_malloc(name_len_utf8);
731  if (!name)
732  return AVERROR(ENOMEM);
733 
734  if ((ret = avio_get_str16le(pb, name_len_utf16, name, name_len_utf8)) < name_len_utf16)
735  avio_skip(pb, name_len_utf16 - ret);
736  av_log(s, AV_LOG_TRACE, "%d stream %d name_len %2d type %d len %4d <%s>\n",
737  i, stream_num, name_len_utf16, value_type, value_len, name);
738 
739  if (!strcmp(name, "AspectRatioX")){
740  int aspect_x = get_value(s->pb, value_type, 16);
741  if(stream_num < 128)
742  asf->dar[stream_num].num = aspect_x;
743  } else if(!strcmp(name, "AspectRatioY")){
744  int aspect_y = get_value(s->pb, value_type, 16);
745  if(stream_num < 128)
746  asf->dar[stream_num].den = aspect_y;
747  } else {
748  get_tag(s, name, value_type, value_len, 16);
749  }
750  av_freep(&name);
751  }
752 
753  return 0;
754 }
755 
757 {
758  AVIOContext *pb = s->pb;
759  ASFContext *asf = s->priv_data;
760  int i, count, name_len, ret;
761  char name[1024];
762 
763  avio_rl64(pb); // reserved 16 bytes
764  avio_rl64(pb); // ...
765  count = avio_rl32(pb); // markers count
766  avio_rl16(pb); // reserved 2 bytes
767  name_len = avio_rl16(pb); // name length
768  avio_skip(pb, name_len);
769 
770  for (i = 0; i < count; i++) {
771  int64_t pres_time;
772  int name_len;
773 
774  if (avio_feof(pb))
775  return AVERROR_INVALIDDATA;
776 
777  avio_rl64(pb); // offset, 8 bytes
778  pres_time = avio_rl64(pb); // presentation time
779  pres_time = av_sat_sub64(pres_time, asf->hdr.preroll * 10000LL);
780  avio_rl16(pb); // entry length
781  avio_rl32(pb); // send time
782  avio_rl32(pb); // flags
783  name_len = avio_rl32(pb); // name length
784  if ((unsigned)name_len > INT_MAX / 2)
785  return AVERROR_INVALIDDATA;
786  if ((ret = avio_get_str16le(pb, name_len * 2, name,
787  sizeof(name))) < name_len)
788  avio_skip(pb, name_len - ret);
789  avpriv_new_chapter(s, i, (AVRational) { 1, 10000000 }, pres_time,
791  }
792 
793  return 0;
794 }
795 
797 {
798  ASFContext *asf = s->priv_data;
799  ff_asf_guid g;
800  AVIOContext *pb = s->pb;
801  int i;
802  int64_t gsize;
803 
804  ff_get_guid(pb, &g);
805  if (ff_guidcmp(&g, &ff_asf_header))
806  return AVERROR_INVALIDDATA;
807  avio_rl64(pb);
808  avio_rl32(pb);
809  avio_r8(pb);
810  avio_r8(pb);
811  memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
812 
813  for (i = 0; i<128; i++)
814  asf->streams[i].stream_language_index = 128; // invalid stream index means no language info
815 
816  for (;;) {
817  uint64_t gpos = avio_tell(pb);
818  int ret = 0;
819  ff_get_guid(pb, &g);
820  gsize = avio_rl64(pb);
821  print_guid(&g);
822  if (!ff_guidcmp(&g, &ff_asf_data_header)) {
823  asf->data_object_offset = avio_tell(pb);
824  /* If not streaming, gsize is not unlimited (how?),
825  * and there is enough space in the file.. */
826  if (!(asf->hdr.flags & 0x01) && gsize >= 100)
827  asf->data_object_size = gsize - 24;
828  else
829  asf->data_object_size = (uint64_t)-1;
830  break;
831  }
832  if (gsize < 24)
833  return AVERROR_INVALIDDATA;
834  if (!ff_guidcmp(&g, &ff_asf_file_header)) {
835  ret = asf_read_file_properties(s, gsize);
836  } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
837  ret = asf_read_stream_properties(s, gsize);
838  } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
839  asf_read_content_desc(s, gsize);
840  } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
841  asf_read_language_list(s, gsize);
842  } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
844  } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
845  asf_read_metadata(s, gsize);
846  } else if (!ff_guidcmp(&g, &ff_asf_metadata_library_header)) {
847  asf_read_metadata(s, gsize);
848  } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
850 
851  // there could be an optional stream properties object to follow
852  // if so the next iteration will pick it up
853  continue;
854  } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
855  ff_get_guid(pb, &g);
856  avio_skip(pb, 6);
857  continue;
858  } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
859  asf_read_marker(s, gsize);
860  } else if (avio_feof(pb)) {
861  return AVERROR_EOF;
862  } else {
863  if (!s->keylen) {
865  unsigned int len;
866  AVPacket pkt;
868  "DRM protected stream detected, decoding will likely fail!\n");
869  len= avio_rl32(pb);
870  av_log(s, AV_LOG_DEBUG, "Secret data:\n");
871 
872  if ((ret = av_get_packet(pb, &pkt, len)) < 0)
873  return ret;
876 
877  len= avio_rl32(pb);
878  if (len > UINT16_MAX)
879  return AVERROR_INVALIDDATA;
880  get_tag(s, "ASF_Protection_Type", -1, len, 32);
881 
882  len= avio_rl32(pb);
883  if (len > UINT16_MAX)
884  return AVERROR_INVALIDDATA;
885  get_tag(s, "ASF_Key_ID", -1, len, 32);
886 
887  len= avio_rl32(pb);
888  if (len > UINT16_MAX)
889  return AVERROR_INVALIDDATA;
890  get_tag(s, "ASF_License_URL", -1, len, 32);
891  } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
893  "Ext DRM protected stream detected, decoding will likely fail!\n");
894  av_dict_set(&s->metadata, "encryption", "ASF Extended Content Encryption", 0);
895  } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
896  av_log(s, AV_LOG_INFO, "Digital signature detected!\n");
897  }
898  }
899  }
900  if (ret < 0)
901  return ret;
902 
903  if (avio_tell(pb) != gpos + gsize)
905  "gpos mismatch our pos=%"PRIu64", end=%"PRId64"\n",
906  avio_tell(pb) - gpos, gsize);
907  avio_seek(pb, gpos + gsize, SEEK_SET);
908  }
909  ff_get_guid(pb, &g);
910  avio_rl64(pb);
911  avio_r8(pb);
912  avio_r8(pb);
913  if (avio_feof(pb))
914  return AVERROR_EOF;
915  asf->data_offset = avio_tell(pb);
916  asf->packet_size_left = 0;
917 
918  for (i = 0; i < 128; i++) {
919  int stream_num = asf->asfid2avid[i];
920  if (stream_num >= 0) {
921  AVStream *st = s->streams[stream_num];
922  if (!st->codecpar->bit_rate)
923  st->codecpar->bit_rate = asf->stream_bitrates[i];
924  if (asf->dar[i].num > 0 && asf->dar[i].den > 0) {
927  asf->dar[i].num, asf->dar[i].den, INT_MAX);
928  } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) &&
929  // Use ASF container value if the stream doesn't set AR.
933  asf->dar[0].num, asf->dar[0].den, INT_MAX);
934 
935  av_log(s, AV_LOG_TRACE, "i=%d, st->codecpar->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
936  i, st->codecpar->codec_type, asf->dar[i].num, asf->dar[i].den,
938 
939  // copy and convert language codes to the frontend
940  if (asf->streams[i].stream_language_index < 128) {
941  const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
942  if (rfc1766 && strlen(rfc1766) > 1) {
943  const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
944  const char *iso6392 = ff_convert_lang_to(primary_tag,
946  if (iso6392)
947  av_dict_set(&st->metadata, "language", iso6392, 0);
948  }
949  }
950  }
951  }
952 
954 
955  return 0;
956 }
957 
958 #define DO_2BITS(bits, var, defval) \
959  switch (bits & 3) { \
960  case 3: \
961  var = avio_rl32(pb); \
962  rsize += 4; \
963  break; \
964  case 2: \
965  var = avio_rl16(pb); \
966  rsize += 2; \
967  break; \
968  case 1: \
969  var = avio_r8(pb); \
970  rsize++; \
971  break; \
972  default: \
973  var = defval; \
974  break; \
975  }
976 
977 /**
978  * Load a single ASF packet into the demuxer.
979  * @param s demux context
980  * @param pb context to read data from
981  * @return 0 on success, <0 on error
982  */
984 {
985  ASFContext *asf = s->priv_data;
986  uint32_t packet_length, padsize;
987  int rsize = 8;
988  int c, d, e, off;
989 
990  if (asf->uses_std_ecc > 0) {
991  // if we do not know packet size, allow skipping up to 32 kB
992  off = 32768;
993  if (asf->no_resync_search)
994  off = 3;
995 // else if (s->packet_size > 0 && !asf->uses_std_ecc)
996 // off = (avio_tell(pb) - s->internal->data_offset) % s->packet_size + 3;
997 
998  c = d = e = -1;
999  while (off-- > 0) {
1000  c = d;
1001  d = e;
1002  e = avio_r8(pb);
1003  if (c == 0x82 && !d && !e)
1004  break;
1005  }
1006 
1007  if (c != 0x82) {
1008  /* This code allows handling of -EAGAIN at packet boundaries (i.e.
1009  * if the packet sync code above triggers -EAGAIN). This does not
1010  * imply complete -EAGAIN handling support at random positions in
1011  * the stream. */
1012  if (pb->error == AVERROR(EAGAIN))
1013  return AVERROR(EAGAIN);
1014  if (!avio_feof(pb))
1016  "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
1017  }
1018  if ((c & 0x8f) == 0x82) {
1019  if (d || e) {
1020  if (!avio_feof(pb))
1021  av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
1022  return AVERROR_INVALIDDATA;
1023  }
1024  c = avio_r8(pb);
1025  d = avio_r8(pb);
1026  rsize += 3;
1027  } else if(!avio_feof(pb)) {
1028  avio_seek(pb, -1, SEEK_CUR); // FIXME
1029  }
1030  } else {
1031  c = avio_r8(pb);
1032  if (c & 0x80) {
1033  rsize ++;
1034  if (!(c & 0x60)) {
1035  d = avio_r8(pb);
1036  e = avio_r8(pb);
1037  avio_seek(pb, (c & 0xF) - 2, SEEK_CUR);
1038  rsize += c & 0xF;
1039  }
1040 
1041  if (c != 0x82)
1042  avpriv_request_sample(s, "Invalid ECC byte");
1043 
1044  if (!asf->uses_std_ecc)
1045  asf->uses_std_ecc = (c == 0x82 && !d && !e) ? 1 : -1;
1046 
1047  c = avio_r8(pb);
1048  } else
1049  asf->uses_std_ecc = -1;
1050  d = avio_r8(pb);
1051  }
1052 
1053  asf->packet_flags = c;
1054  asf->packet_property = d;
1055 
1056  DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
1057  DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
1058  DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
1059 
1060  // the following checks prevent overflows and infinite loops
1061  if (!packet_length || packet_length >= (1U << 29)) {
1063  "invalid packet_length %"PRIu32" at:%"PRId64"\n",
1064  packet_length, avio_tell(pb));
1065  return AVERROR_INVALIDDATA;
1066  }
1067  if (padsize >= packet_length) {
1069  "invalid padsize %"PRIu32" at:%"PRId64"\n", padsize, avio_tell(pb));
1070  return AVERROR_INVALIDDATA;
1071  }
1072 
1073  asf->packet_timestamp = avio_rl32(pb);
1074  avio_rl16(pb); /* duration */
1075  // rsize has at least 11 bytes which have to be present
1076 
1077  if (asf->packet_flags & 0x01) {
1078  asf->packet_segsizetype = avio_r8(pb);
1079  rsize++;
1080  asf->packet_segments = asf->packet_segsizetype & 0x3f;
1081  } else {
1082  asf->packet_segments = 1;
1083  asf->packet_segsizetype = 0x80;
1084  }
1085  if (rsize > packet_length - padsize) {
1086  asf->packet_size_left = 0;
1088  "invalid packet header length %d for pktlen %"PRIu32"-%"PRIu32" at %"PRId64"\n",
1089  rsize, packet_length, padsize, avio_tell(pb));
1090  return AVERROR_INVALIDDATA;
1091  }
1092  asf->packet_size_left = packet_length - padsize - rsize;
1093  if (packet_length < asf->hdr.min_pktsize)
1094  padsize += asf->hdr.min_pktsize - packet_length;
1095  asf->packet_padsize = padsize;
1096  av_log(s, AV_LOG_TRACE, "packet: size=%d padsize=%d left=%d\n",
1097  s->packet_size, asf->packet_padsize, asf->packet_size_left);
1098  return 0;
1099 }
1100 
1101 /**
1102  *
1103  * @return <0 if error
1104  */
1106 {
1107  ASFContext *asf = s->priv_data;
1108  ASFStream *asfst;
1109  int rsize = 1;
1110  int num = avio_r8(pb);
1111  int i;
1112  int64_t ts0, ts1 av_unused;
1113 
1114  asf->packet_segments--;
1115  asf->packet_key_frame = num >> 7;
1116  asf->stream_index = asf->asfid2avid[num & 0x7f];
1117  asfst = &asf->streams[num & 0x7f];
1118  // sequence should be ignored!
1119  DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
1120  DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
1122  av_log(asf, AV_LOG_TRACE, "key:%d stream:%d seq:%d offset:%d replic_size:%d num:%X packet_property %X\n",
1123  asf->packet_key_frame, asf->stream_index, asf->packet_seq,
1124  asf->packet_frag_offset, asf->packet_replic_size, num, asf->packet_property);
1125  if (rsize+(int64_t)asf->packet_replic_size > asf->packet_size_left) {
1126  av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
1127  return AVERROR_INVALIDDATA;
1128  }
1129  if (asf->packet_replic_size >= 8) {
1130  int64_t end = avio_tell(pb) + asf->packet_replic_size;
1131  AVRational aspect;
1132  asfst->packet_obj_size = avio_rl32(pb);
1133  if (asfst->packet_obj_size >= (1 << 24) || asfst->packet_obj_size < 0) {
1134  av_log(s, AV_LOG_ERROR, "packet_obj_size %d invalid\n", asfst->packet_obj_size);
1135  asfst->packet_obj_size = 0;
1136  return AVERROR_INVALIDDATA;
1137  }
1138  asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
1139 
1140  for (i = 0; i < asfst->payload_ext_ct; i++) {
1141  ASFPayload *p = &asfst->payload[i];
1142  int size = p->size;
1143  int64_t payend;
1144  if (size == 0xFFFF)
1145  size = avio_rl16(pb);
1146  payend = avio_tell(pb) + size;
1147  if (payend > end) {
1148  av_log(s, AV_LOG_ERROR, "too long payload\n");
1149  break;
1150  }
1151  switch (p->type) {
1152  case 0x50:
1153 // duration = avio_rl16(pb);
1154  break;
1155  case 0x54:
1156  aspect.num = avio_r8(pb);
1157  aspect.den = avio_r8(pb);
1158  if (aspect.num > 0 && aspect.den > 0 && asf->stream_index >= 0) {
1159  s->streams[asf->stream_index]->sample_aspect_ratio = aspect;
1160  }
1161  break;
1162  case 0x2A:
1163  avio_skip(pb, 8);
1164  ts0 = avio_rl64(pb);
1165  ts1 = avio_rl64(pb);
1166  if (ts0!= -1) asf->packet_frag_timestamp = ts0/10000;
1168  asf->ts_is_pts = 1;
1169  break;
1170  case 0x5B:
1171  case 0xB7:
1172  case 0xCC:
1173  case 0xC0:
1174  case 0xA0:
1175  //unknown
1176  break;
1177  }
1178  avio_seek(pb, payend, SEEK_SET);
1179  }
1180 
1181  avio_seek(pb, end, SEEK_SET);
1182  rsize += asf->packet_replic_size; // FIXME - check validity
1183  } else if (asf->packet_replic_size == 1) {
1184  // multipacket - frag_offset is beginning timestamp
1186  asf->packet_frag_offset = 0;
1188 
1189  asf->packet_time_delta = avio_r8(pb);
1190  rsize++;
1191  } else if (asf->packet_replic_size != 0) {
1192  av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n",
1193  asf->packet_replic_size);
1194  return AVERROR_INVALIDDATA;
1195  }
1196  if (asf->packet_flags & 0x01) {
1197  DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
1198  if (rsize > asf->packet_size_left) {
1199  av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
1200  return AVERROR_INVALIDDATA;
1201  } else if (asf->packet_frag_size > asf->packet_size_left - rsize) {
1202  if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
1203  av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d>%d-%d+%d)\n",
1204  asf->packet_frag_size, asf->packet_size_left, rsize, asf->packet_padsize);
1205  return AVERROR_INVALIDDATA;
1206  } else {
1207  int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
1208  asf->packet_size_left += diff;
1209  asf->packet_padsize -= diff;
1210  }
1211  }
1212  } else {
1213  asf->packet_frag_size = asf->packet_size_left - rsize;
1214  }
1215  if (asf->packet_replic_size == 1) {
1216  asf->packet_multi_size = asf->packet_frag_size;
1217  if (asf->packet_multi_size > asf->packet_size_left)
1218  return AVERROR_INVALIDDATA;
1219  }
1220  asf->packet_size_left -= rsize;
1221 
1222  return 0;
1223 }
1224 
1225 /**
1226  * Parse data from individual ASF packets (which were previously loaded
1227  * with asf_get_packet()).
1228  * @param s demux context
1229  * @param pb context to read data from
1230  * @param pkt pointer to store packet data into
1231  * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
1232  * packets need to be loaded (through asf_get_packet())
1233  */
1235 {
1236  ASFContext *asf = s->priv_data;
1237  ASFStream *asf_st = 0;
1238  for (;;) {
1239  int ret;
1240  if (avio_feof(pb))
1241  return AVERROR_EOF;
1242  if (asf->packet_size_left < FRAME_HEADER_SIZE ||
1243  asf->packet_segments < 1 && asf->packet_time_start == 0) {
1244  int ret = asf->packet_size_left + asf->packet_padsize;
1245 
1247  av_log(s, AV_LOG_WARNING, "Skip due to FRAME_HEADER_SIZE\n");
1248 
1249  assert(ret >= 0);
1250  /* fail safe */
1251  avio_skip(pb, ret);
1252 
1253  asf->packet_pos = avio_tell(pb);
1254  if (asf->data_object_size != (uint64_t)-1 &&
1255  (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
1256  return AVERROR_EOF; /* Do not exceed the size of the data object */
1257  return 1;
1258  }
1259  if (asf->packet_time_start == 0) {
1260  if (asf_read_frame_header(s, pb) < 0) {
1261  asf->packet_time_start = asf->packet_segments = 0;
1262  continue;
1263  }
1264  if (asf->stream_index < 0 ||
1265  s->streams[asf->stream_index]->discard >= AVDISCARD_ALL ||
1266  (!asf->packet_key_frame &&
1267  (s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY || asf->streams[s->streams[asf->stream_index]->id].skip_to_key))) {
1268  asf->packet_time_start = 0;
1269  /* unhandled packet (should not happen) */
1270  avio_skip(pb, asf->packet_frag_size);
1271  asf->packet_size_left -= asf->packet_frag_size;
1272  if (asf->stream_index < 0)
1273  av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n",
1274  asf->packet_frag_size);
1275  continue;
1276  }
1277  asf->asf_st = &asf->streams[s->streams[asf->stream_index]->id];
1278  if (!asf->packet_frag_offset)
1279  asf->asf_st->skip_to_key = 0;
1280  }
1281  asf_st = asf->asf_st;
1282  av_assert0(asf_st);
1283 
1284  if (!asf_st->frag_offset && asf->packet_frag_offset) {
1285  av_log(s, AV_LOG_TRACE, "skipping asf data pkt with fragment offset for "
1286  "stream:%d, expected:%d but got %d from pkt)\n",
1287  asf->stream_index, asf_st->frag_offset,
1288  asf->packet_frag_offset);
1289  avio_skip(pb, asf->packet_frag_size);
1290  asf->packet_size_left -= asf->packet_frag_size;
1291  continue;
1292  }
1293 
1294  if (asf->packet_replic_size == 1) {
1295  // frag_offset is here used as the beginning timestamp
1297  asf->packet_time_start += asf->packet_time_delta;
1298  asf_st->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
1299  asf->packet_size_left--;
1300  asf->packet_multi_size--;
1301  if (asf->packet_multi_size < asf_st->packet_obj_size) {
1302  asf->packet_time_start = 0;
1303  avio_skip(pb, asf->packet_multi_size);
1304  asf->packet_size_left -= asf->packet_multi_size;
1305  continue;
1306  }
1307  asf->packet_multi_size -= asf_st->packet_obj_size;
1308  }
1309 
1310  if (asf_st->pkt.size != asf_st->packet_obj_size ||
1311  // FIXME is this condition sufficient?
1312  asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
1313  int ret;
1314 
1315  if (asf_st->pkt.data) {
1316  av_log(s, AV_LOG_INFO,
1317  "freeing incomplete packet size %d, new %d\n",
1318  asf_st->pkt.size, asf_st->packet_obj_size);
1319  asf_st->frag_offset = 0;
1320  av_packet_unref(&asf_st->pkt);
1321  }
1322  /* new packet */
1323  if ((ret = av_new_packet(&asf_st->pkt, asf_st->packet_obj_size)) < 0)
1324  return ret;
1325  asf_st->seq = asf->packet_seq;
1326  if (asf->packet_frag_timestamp != AV_NOPTS_VALUE) {
1327  if (asf->ts_is_pts) {
1328  asf_st->pkt.pts = asf->packet_frag_timestamp - asf->hdr.preroll;
1329  } else
1330  asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
1331  }
1332  asf_st->pkt.stream_index = asf->stream_index;
1333  asf_st->pkt.pos = asf_st->packet_pos = asf->packet_pos;
1334  asf_st->pkt_clean = 0;
1335 
1336  if (asf_st->pkt.data && asf_st->palette_changed) {
1337  uint8_t *pal;
1339  AVPALETTE_SIZE);
1340  if (!pal) {
1341  av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
1342  } else {
1343  memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
1344  asf_st->palette_changed = 0;
1345  }
1346  }
1347  av_log(asf, AV_LOG_TRACE, "new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
1348  asf->stream_index, asf->packet_key_frame,
1349  asf_st->pkt.flags & AV_PKT_FLAG_KEY,
1350  s->streams[asf->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO,
1351  asf_st->packet_obj_size);
1352  if (s->streams[asf->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
1353  asf->packet_key_frame = 1;
1354  if (asf->packet_key_frame)
1355  asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
1356  }
1357 
1358  /* read data */
1359  av_log(asf, AV_LOG_TRACE, "READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
1360  s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
1361  asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
1362  asf->packet_size_left -= asf->packet_frag_size;
1363  if (asf->packet_size_left < 0)
1364  continue;
1365 
1366  if (asf->packet_frag_offset >= asf_st->pkt.size ||
1367  asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset) {
1369  "packet fragment position invalid %u,%u not in %u\n",
1371  asf_st->pkt.size);
1372  continue;
1373  }
1374 
1375  if (asf->packet_frag_offset != asf_st->frag_offset && !asf_st->pkt_clean) {
1376  memset(asf_st->pkt.data + asf_st->frag_offset, 0, asf_st->pkt.size - asf_st->frag_offset);
1377  asf_st->pkt_clean = 1;
1378  }
1379 
1380  ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
1381  asf->packet_frag_size);
1382  if (ret != asf->packet_frag_size) {
1383  if (ret < 0 || asf->packet_frag_offset + ret == 0)
1384  return ret < 0 ? ret : AVERROR_EOF;
1385 
1386  if (asf_st->ds_span > 1) {
1387  // scrambling, we can either drop it completely or fill the remainder
1388  // TODO: should we fill the whole packet instead of just the current
1389  // fragment?
1390  memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
1391  asf->packet_frag_size - ret);
1392  ret = asf->packet_frag_size;
1393  } else {
1394  // no scrambling, so we can return partial packets
1395  av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
1396  }
1397  }
1398  if (s->key && s->keylen == 20)
1399  ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
1400  ret);
1401  asf_st->frag_offset += ret;
1402  /* test if whole packet is read */
1403  if (asf_st->frag_offset == asf_st->pkt.size) {
1404  // workaround for macroshit radio DVR-MS files
1405  if (s->streams[asf->stream_index]->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
1406  asf_st->pkt.size > 100) {
1407  int i;
1408  for (i = 0; i < asf_st->pkt.size && !asf_st->pkt.data[i]; i++)
1409  ;
1410  if (i == asf_st->pkt.size) {
1411  av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1412  asf_st->frag_offset = 0;
1413  av_packet_unref(&asf_st->pkt);
1414  continue;
1415  }
1416  }
1417 
1418  /* return packet */
1419  if (asf_st->ds_span > 1) {
1420  if (asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span) {
1422  "pkt.size != ds_packet_size * ds_span (%d %d %d)\n",
1423  asf_st->pkt.size, asf_st->ds_packet_size,
1424  asf_st->ds_span);
1425  } else {
1426  /* packet descrambling */
1427  AVBufferRef *buf = av_buffer_alloc(asf_st->pkt.size +
1429  if (buf) {
1430  uint8_t *newdata = buf->data;
1431  int offset = 0;
1432  memset(newdata + asf_st->pkt.size, 0,
1434  while (offset < asf_st->pkt.size) {
1435  int off = offset / asf_st->ds_chunk_size;
1436  int row = off / asf_st->ds_span;
1437  int col = off % asf_st->ds_span;
1438  int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1439  assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
1440  assert(idx + 1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
1441  memcpy(newdata + offset,
1442  asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1443  asf_st->ds_chunk_size);
1444  offset += asf_st->ds_chunk_size;
1445  }
1446  av_buffer_unref(&asf_st->pkt.buf);
1447  asf_st->pkt.buf = buf;
1448  asf_st->pkt.data = buf->data;
1449  }
1450  }
1451  }
1452  asf_st->frag_offset = 0;
1453  *pkt = asf_st->pkt;
1454  asf_st->pkt.buf = 0;
1455  asf_st->pkt.size = 0;
1456  asf_st->pkt.data = 0;
1457  asf_st->pkt.side_data_elems = 0;
1458  asf_st->pkt.side_data = NULL;
1459  break; // packet completed
1460  }
1461  }
1462  return 0;
1463 }
1464 
1466 {
1467  ASFContext *asf = s->priv_data;
1468 
1469  for (;;) {
1470  int ret;
1471 
1472  /* parse cached packets, if any */
1473  if ((ret = asf_parse_packet(s, s->pb, pkt)) <= 0)
1474  return ret;
1475  if ((ret = asf_get_packet(s, s->pb)) < 0)
1476  assert(asf->packet_size_left < FRAME_HEADER_SIZE ||
1477  asf->packet_segments < 1);
1478  asf->packet_time_start = 0;
1479  }
1480 }
1481 
1482 // Added to support seeking after packets have been read
1483 // If information is not reset, read_packet fails due to
1484 // leftover information from previous reads
1486 {
1487  ASFContext *asf = s->priv_data;
1488  ASFStream *asf_st;
1489  int i;
1490 
1491  asf->packet_size_left = 0;
1492  asf->packet_flags = 0;
1493  asf->packet_property = 0;
1494  asf->packet_timestamp = 0;
1495  asf->packet_segsizetype = 0;
1496  asf->packet_segments = 0;
1497  asf->packet_seq = 0;
1498  asf->packet_replic_size = 0;
1499  asf->packet_key_frame = 0;
1500  asf->packet_padsize = 0;
1501  asf->packet_frag_offset = 0;
1502  asf->packet_frag_size = 0;
1503  asf->packet_frag_timestamp = 0;
1504  asf->packet_multi_size = 0;
1505  asf->packet_time_delta = 0;
1506  asf->packet_time_start = 0;
1507 
1508  for (i = 0; i < 128; i++) {
1509  asf_st = &asf->streams[i];
1510  av_packet_unref(&asf_st->pkt);
1511  asf_st->packet_obj_size = 0;
1512  asf_st->frag_offset = 0;
1513  asf_st->seq = 0;
1514  }
1515  asf->asf_st = NULL;
1516 }
1517 
1519 {
1520  ASFContext *asf = s->priv_data;
1521  int i;
1522 
1523  for (i = 0; i < 128; i++) {
1524  int j = asf->asfid2avid[i];
1525  ASFStream *asf_st = &asf->streams[i];
1526  if (j < 0 || s->streams[j]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
1527  continue;
1528 
1529  asf_st->skip_to_key = 1;
1530  }
1531 }
1532 
1534 {
1536 
1537  return 0;
1538 }
1539 
1540 static int64_t asf_read_pts(AVFormatContext *s, int stream_index,
1541  int64_t *ppos, int64_t pos_limit)
1542 {
1543  ASFContext *asf = s->priv_data;
1544  AVPacket pkt1, *pkt = &pkt1;
1545  ASFStream *asf_st;
1546  int64_t pts;
1547  int64_t pos = *ppos;
1548  int i;
1549  int64_t start_pos[ASF_MAX_STREAMS];
1550 
1551  for (i = 0; i < s->nb_streams; i++)
1552  start_pos[i] = pos;
1553 
1554  if (s->packet_size > 0)
1555  pos = (pos + s->packet_size - 1 - s->internal->data_offset) /
1556  s->packet_size * s->packet_size +
1557  s->internal->data_offset;
1558  *ppos = pos;
1559  if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1560  return AV_NOPTS_VALUE;
1561 
1564  for (;;) {
1565  if (av_read_frame(s, pkt) < 0) {
1566  av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
1567  return AV_NOPTS_VALUE;
1568  }
1569 
1570  pts = pkt->dts;
1571 
1572  if (pkt->flags & AV_PKT_FLAG_KEY) {
1573  i = pkt->stream_index;
1574 
1575  asf_st = &asf->streams[s->streams[i]->id];
1576 
1577 // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
1578  pos = asf_st->packet_pos;
1579  av_assert1(pkt->pos == asf_st->packet_pos);
1580 
1581  av_add_index_entry(s->streams[i], pos, pts, pkt->size,
1582  pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
1583  start_pos[i] = asf_st->packet_pos + 1;
1584 
1585  if (pkt->stream_index == stream_index) {
1587  break;
1588  }
1589  }
1591  }
1592 
1593  *ppos = pos;
1594  return pts;
1595 }
1596 
1597 static int asf_build_simple_index(AVFormatContext *s, int stream_index)
1598 {
1599  ff_asf_guid g;
1600  ASFContext *asf = s->priv_data;
1601  int64_t current_pos = avio_tell(s->pb);
1602  int64_t ret;
1603 
1604  if((ret = avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET)) < 0) {
1605  return ret;
1606  }
1607 
1608  if ((ret = ff_get_guid(s->pb, &g)) < 0)
1609  goto end;
1610 
1611  /* the data object can be followed by other top-level objects,
1612  * skip them until the simple index object is reached */
1614  int64_t gsize = avio_rl64(s->pb);
1615  if (gsize < 24 || avio_feof(s->pb)) {
1616  goto end;
1617  }
1618  avio_skip(s->pb, gsize - 24);
1619  if ((ret = ff_get_guid(s->pb, &g)) < 0)
1620  goto end;
1621  }
1622 
1623  {
1624  int64_t itime, last_pos = -1;
1625  int pct, ict;
1626  int i;
1627  int64_t av_unused gsize = avio_rl64(s->pb);
1628  if ((ret = ff_get_guid(s->pb, &g)) < 0)
1629  goto end;
1630  itime = avio_rl64(s->pb);
1631  pct = avio_rl32(s->pb);
1632  ict = avio_rl32(s->pb);
1634  "itime:0x%"PRIx64", pct:%d, ict:%d\n", itime, pct, ict);
1635 
1636  for (i = 0; i < ict; i++) {
1637  int pktnum = avio_rl32(s->pb);
1638  int pktct = avio_rl16(s->pb);
1639  int64_t pos = s->internal->data_offset + s->packet_size * (int64_t)pktnum;
1640  int64_t index_pts = FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
1641 
1642  if (avio_feof(s->pb)) {
1643  ret = AVERROR_INVALIDDATA;
1644  goto end;
1645  }
1646 
1647  if (pos != last_pos) {
1648  av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n",
1649  pktnum, pktct, index_pts);
1650  av_add_index_entry(s->streams[stream_index], pos, index_pts,
1651  s->packet_size, 0, AVINDEX_KEYFRAME);
1652  last_pos = pos;
1653  }
1654  }
1655  asf->index_read = ict > 1;
1656  }
1657 end:
1658 // if (avio_feof(s->pb)) {
1659 // ret = 0;
1660 // }
1661  avio_seek(s->pb, current_pos, SEEK_SET);
1662  return ret;
1663 }
1664 
1665 static int asf_read_seek(AVFormatContext *s, int stream_index,
1666  int64_t pts, int flags)
1667 {
1668  ASFContext *asf = s->priv_data;
1669  AVStream *st = s->streams[stream_index];
1670  int ret = 0;
1671 
1672  if (s->packet_size <= 0)
1673  return -1;
1674 
1675  /* Try using the protocol's read_seek if available */
1676  if (s->pb) {
1677  int64_t ret = avio_seek_time(s->pb, stream_index, pts, flags);
1678  if (ret >= 0)
1680  if (ret != AVERROR(ENOSYS))
1681  return ret;
1682  }
1683 
1684  /* explicitly handle the case of seeking to 0 */
1685  if (!pts) {
1687  avio_seek(s->pb, s->internal->data_offset, SEEK_SET);
1688  return 0;
1689  }
1690 
1691  if (!asf->index_read) {
1692  ret = asf_build_simple_index(s, stream_index);
1693  if (ret < 0)
1694  asf->index_read = -1;
1695  }
1696 
1697  if (asf->index_read > 0 && st->index_entries) {
1699  if (index >= 0) {
1700  /* find the position */
1701  uint64_t pos = st->index_entries[index].pos;
1702 
1703  /* do the seek */
1704  av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1705  if(avio_seek(s->pb, pos, SEEK_SET) < 0)
1706  return -1;
1708  skip_to_key(s);
1709  return 0;
1710  }
1711  }
1712  /* no index or seeking by index failed */
1713  if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1714  return -1;
1716  skip_to_key(s);
1717  return 0;
1718 }
1719 
1721  .name = "asf",
1722  .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1723  .priv_data_size = sizeof(ASFContext),
1724  .read_probe = asf_probe,
1729  .read_timestamp = asf_read_pts,
1731  .priv_class = &asf_class,
1732 };
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:1105
static int64_t asf_read_pts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
Definition: asfdec_f.c:1540
static void asf_reset_header(AVFormatContext *s)
Definition: asfdec_f.c:1485
static const AVOption options[]
Definition: asfdec_f.c:120
static int asf_read_header(AVFormatContext *s)
Definition: asfdec_f.c:796
static int asf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: asfdec_f.c:1465
#define print_guid(g)
Definition: asfdec_f.c:192
static int asf_read_marker(AVFormatContext *s, int64_t size)
Definition: asfdec_f.c:756
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:958
#define FRAME_HEADER_SIZE
Definition: asfdec_f.c:137
static int asf_build_simple_index(AVFormatContext *s, int stream_index)
Definition: asfdec_f.c:1597
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:1234
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:1518
static int asf_get_packet(AVFormatContext *s, AVIOContext *pb)
Load a single ASF packet into the demuxer.
Definition: asfdec_f.c:983
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:1665
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:1533
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:1720
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:253
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:342
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:364
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:734
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:337
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:750
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:1234
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:624
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:758
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:682
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:550
@ 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:4509
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:1120
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:1126
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:1142
int ff_id3v2_parse_chapters(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Create chapters for all CHAP tags found in the ID3v2 header.
Definition: id3v2.c:1182
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:4945
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:4639
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:79
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:117
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]