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