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