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