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