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