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/mathematics.h"
29 #include "libavutil/opt.h"
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "avlanguage.h"
33 #include "id3v2.h"
34 #include "internal.h"
35 #include "riff.h"
36 #include "asf.h"
37 #include "asfcrypt.h"
38 
39 typedef struct {
40  const AVClass *class;
41  int asfid2avid[128]; ///< conversion table from asf ID 2 AVStream ID
42  ASFStream streams[128]; ///< it's max number and it's not that big
43  uint32_t stream_bitrates[128]; ///< max number of streams, bitrate for each (for streaming)
44  AVRational dar[128];
45  char stream_languages[128][6]; ///< max number of streams, language for each (RFC1766, e.g. en-US)
46  /* non streamed additonnal info */
47  /* packet filling */
49  /* only for reading */
50  uint64_t data_offset; ///< beginning of the first data packet
51  uint64_t data_object_offset; ///< data object offset (excl. GUID & size)
52  uint64_t data_object_size; ///< size of the data object
54 
56 
66  unsigned int packet_frag_offset;
67  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  asf_st = av_mallocz(sizeof(ASFStream));
370  if (!asf_st)
371  return AVERROR(ENOMEM);
372  st->priv_data = asf_st;
373  start_time = asf->hdr.preroll;
374 
375  asf_st->stream_language_index = 128; // invalid stream index means no language info
376 
377  if (!(asf->hdr.flags & 0x01)) { // if we aren't streaming...
378  int64_t fsize = avio_size(pb);
379  if (fsize <= 0 || (int64_t)asf->hdr.file_size <= 0 || FFABS(fsize - (int64_t)asf->hdr.file_size) < 10000)
380  st->duration = asf->hdr.play_time /
381  (10000000 / 1000) - start_time;
382  }
383  ff_get_guid(pb, &g);
384 
385  test_for_ext_stream_audio = 0;
386  if (!ff_guidcmp(&g, &ff_asf_audio_stream)) {
387  type = AVMEDIA_TYPE_AUDIO;
388  } else if (!ff_guidcmp(&g, &ff_asf_video_stream)) {
389  type = AVMEDIA_TYPE_VIDEO;
390  } else if (!ff_guidcmp(&g, &ff_asf_jfif_media)) {
391  type = AVMEDIA_TYPE_VIDEO;
393  } else if (!ff_guidcmp(&g, &ff_asf_command_stream)) {
394  type = AVMEDIA_TYPE_DATA;
396  test_for_ext_stream_audio = 1;
397  type = AVMEDIA_TYPE_UNKNOWN;
398  } else {
399  return -1;
400  }
401  ff_get_guid(pb, &g);
402  avio_skip(pb, 8); /* total_size */
403  type_specific_size = avio_rl32(pb);
404  avio_rl32(pb);
405  st->id = avio_rl16(pb) & 0x7f; /* stream id */
406  // mapping of asf ID to AV stream ID;
407  asf->asfid2avid[st->id] = s->nb_streams - 1;
408 
409  avio_rl32(pb);
410 
411  if (test_for_ext_stream_audio) {
412  ff_get_guid(pb, &g);
414  type = AVMEDIA_TYPE_AUDIO;
415  is_dvr_ms_audio = 1;
416  ff_get_guid(pb, &g);
417  avio_rl32(pb);
418  avio_rl32(pb);
419  avio_rl32(pb);
420  ff_get_guid(pb, &g);
421  avio_rl32(pb);
422  }
423  }
424 
425  st->codec->codec_type = type;
426  if (type == AVMEDIA_TYPE_AUDIO) {
427  int ret = ff_get_wav_header(pb, st->codec, type_specific_size);
428  if (ret < 0)
429  return ret;
430  if (is_dvr_ms_audio) {
431  // codec_id and codec_tag are unreliable in dvr_ms
432  // files. Set them later by probing stream.
433  st->request_probe = 1;
434  st->codec->codec_tag = 0;
435  }
436  if (st->codec->codec_id == AV_CODEC_ID_AAC)
438  else
440  /* We have to init the frame size at some point .... */
441  pos2 = avio_tell(pb);
442  if (size >= (pos2 + 8 - pos1 + 24)) {
443  asf_st->ds_span = avio_r8(pb);
444  asf_st->ds_packet_size = avio_rl16(pb);
445  asf_st->ds_chunk_size = avio_rl16(pb);
446  avio_rl16(pb); // ds_data_size
447  avio_r8(pb); // ds_silence_data
448  }
449  if (asf_st->ds_span > 1) {
450  if (!asf_st->ds_chunk_size ||
451  (asf_st->ds_packet_size / asf_st->ds_chunk_size <= 1) ||
452  asf_st->ds_packet_size % asf_st->ds_chunk_size)
453  asf_st->ds_span = 0; // disable descrambling
454  }
455  } else if (type == AVMEDIA_TYPE_VIDEO &&
456  size - (avio_tell(pb) - pos1 + 24) >= 51) {
457  avio_rl32(pb);
458  avio_rl32(pb);
459  avio_r8(pb);
460  avio_rl16(pb); /* size */
461  sizeX = avio_rl32(pb); /* size */
462  st->codec->width = avio_rl32(pb);
463  st->codec->height = avio_rl32(pb);
464  /* not available for asf */
465  avio_rl16(pb); /* panes */
466  st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
467  tag1 = avio_rl32(pb);
468  avio_skip(pb, 20);
469  if (sizeX > 40) {
470  st->codec->extradata_size = ffio_limit(pb, sizeX - 40);
473  avio_read(pb, st->codec->extradata, st->codec->extradata_size);
474  }
475 
476  /* Extract palette from extradata if bpp <= 8 */
477  /* This code assumes that extradata contains only palette */
478  /* This is true for all paletted codecs implemented in libavcodec */
479  if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
480 #if HAVE_BIGENDIAN
481  int i;
482  for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE) / 4; i++)
483  asf_st->palette[i] = av_bswap32(((uint32_t *)st->codec->extradata)[i]);
484 #else
485  memcpy(asf_st->palette, st->codec->extradata,
487 #endif
488  asf_st->palette_changed = 1;
489  }
490 
491  st->codec->codec_tag = tag1;
493  if (tag1 == MKTAG('D', 'V', 'R', ' ')) {
495  /* issue658 contains wrong w/h and MS even puts a fake seq header
496  * with wrong w/h in extradata while a correct one is in the stream.
497  * maximum lameness */
498  st->codec->width =
499  st->codec->height = 0;
500  av_freep(&st->codec->extradata);
501  st->codec->extradata_size = 0;
502  }
503  if (st->codec->codec_id == AV_CODEC_ID_H264)
505  }
506  pos2 = avio_tell(pb);
507  avio_skip(pb, size - (pos2 - pos1 + 24));
508 
509  return 0;
510 }
511 
513 {
514  ASFContext *asf = s->priv_data;
515  AVIOContext *pb = s->pb;
516  ff_asf_guid g;
517  int ext_len, payload_ext_ct, stream_ct, i;
518  uint32_t leak_rate, stream_num;
519  unsigned int stream_languageid_index;
520 
521  avio_rl64(pb); // starttime
522  avio_rl64(pb); // endtime
523  leak_rate = avio_rl32(pb); // leak-datarate
524  avio_rl32(pb); // bucket-datasize
525  avio_rl32(pb); // init-bucket-fullness
526  avio_rl32(pb); // alt-leak-datarate
527  avio_rl32(pb); // alt-bucket-datasize
528  avio_rl32(pb); // alt-init-bucket-fullness
529  avio_rl32(pb); // max-object-size
530  avio_rl32(pb); // flags (reliable,seekable,no_cleanpoints?,resend-live-cleanpoints, rest of bits reserved)
531  stream_num = avio_rl16(pb); // stream-num
532 
533  stream_languageid_index = avio_rl16(pb); // stream-language-id-index
534  if (stream_num < 128)
535  asf->streams[stream_num].stream_language_index = stream_languageid_index;
536 
537  avio_rl64(pb); // avg frametime in 100ns units
538  stream_ct = avio_rl16(pb); // stream-name-count
539  payload_ext_ct = avio_rl16(pb); // payload-extension-system-count
540 
541  if (stream_num < 128) {
542  asf->stream_bitrates[stream_num] = leak_rate;
543  asf->streams[stream_num].payload_ext_ct = 0;
544  }
545 
546  for (i = 0; i < stream_ct; i++) {
547  avio_rl16(pb);
548  ext_len = avio_rl16(pb);
549  avio_skip(pb, ext_len);
550  }
551 
552  for (i = 0; i < payload_ext_ct; i++) {
553  int size;
554  ff_get_guid(pb, &g);
555  size = avio_rl16(pb);
556  ext_len = avio_rl32(pb);
557  avio_skip(pb, ext_len);
558  if (stream_num < 128 && i < FF_ARRAY_ELEMS(asf->streams[stream_num].payload)) {
559  ASFPayload *p = &asf->streams[stream_num].payload[i];
560  p->type = g[0];
561  p->size = size;
562  av_log(s, AV_LOG_DEBUG, "Payload extension %x %d\n", g[0], p->size );
563  asf->streams[stream_num].payload_ext_ct ++;
564  }
565  }
566 
567  return 0;
568 }
569 
571 {
572  AVIOContext *pb = s->pb;
573  int len1, len2, len3, len4, len5;
574 
575  len1 = avio_rl16(pb);
576  len2 = avio_rl16(pb);
577  len3 = avio_rl16(pb);
578  len4 = avio_rl16(pb);
579  len5 = avio_rl16(pb);
580  get_tag(s, "title", 0, len1, 32);
581  get_tag(s, "author", 0, len2, 32);
582  get_tag(s, "copyright", 0, len3, 32);
583  get_tag(s, "comment", 0, len4, 32);
584  avio_skip(pb, len5);
585 
586  return 0;
587 }
588 
590 {
591  AVIOContext *pb = s->pb;
592  ASFContext *asf = s->priv_data;
593  int desc_count, i, ret;
594 
595  desc_count = avio_rl16(pb);
596  for (i = 0; i < desc_count; i++) {
597  int name_len, value_type, value_len;
598  char name[1024];
599 
600  name_len = avio_rl16(pb);
601  if (name_len % 2) // must be even, broken lavf versions wrote len-1
602  name_len += 1;
603  if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
604  avio_skip(pb, name_len - ret);
605  value_type = avio_rl16(pb);
606  value_len = avio_rl16(pb);
607  if (!value_type && value_len % 2)
608  value_len += 1;
609  /* My sample has that stream set to 0 maybe that mean the container.
610  * ASF stream count starts at 1. I am using 0 to the container value
611  * since it's unused. */
612  if (!strcmp(name, "AspectRatioX"))
613  asf->dar[0].num = get_value(s->pb, value_type, 32);
614  else if (!strcmp(name, "AspectRatioY"))
615  asf->dar[0].den = get_value(s->pb, value_type, 32);
616  else
617  get_tag(s, name, value_type, value_len, 32);
618  }
619 
620  return 0;
621 }
622 
624 {
625  AVIOContext *pb = s->pb;
626  ASFContext *asf = s->priv_data;
627  int j, ret;
628  int stream_count = avio_rl16(pb);
629  for (j = 0; j < stream_count; j++) {
630  char lang[6];
631  unsigned int lang_len = avio_r8(pb);
632  if ((ret = avio_get_str16le(pb, lang_len, lang,
633  sizeof(lang))) < lang_len)
634  avio_skip(pb, lang_len - ret);
635  if (j < 128)
636  av_strlcpy(asf->stream_languages[j], lang,
637  sizeof(*asf->stream_languages));
638  }
639 
640  return 0;
641 }
642 
644 {
645  AVIOContext *pb = s->pb;
646  ASFContext *asf = s->priv_data;
647  int n, stream_num, name_len, value_len;
648  int ret, i;
649  n = avio_rl16(pb);
650 
651  for (i = 0; i < n; i++) {
652  char name[1024];
653  int value_type;
654 
655  avio_rl16(pb); // lang_list_index
656  stream_num = avio_rl16(pb);
657  name_len = avio_rl16(pb);
658  value_type = avio_rl16(pb); /* value_type */
659  value_len = avio_rl32(pb);
660 
661  if ((ret = avio_get_str16le(pb, name_len, name, sizeof(name))) < name_len)
662  avio_skip(pb, name_len - ret);
663  av_dlog(s, "%d stream %d name_len %2d type %d len %4d <%s>\n",
664  i, stream_num, name_len, value_type, value_len, name);
665 
666  if (!strcmp(name, "AspectRatioX")){
667  int aspect_x = get_value(s->pb, value_type, 16);
668  if(stream_num < 128)
669  asf->dar[stream_num].num = aspect_x;
670  } else if(!strcmp(name, "AspectRatioY")){
671  int aspect_y = get_value(s->pb, value_type, 16);
672  if(stream_num < 128)
673  asf->dar[stream_num].den = aspect_y;
674  } else {
675  get_tag(s, name, value_type, value_len, 16);
676  }
677  }
678 
679  return 0;
680 }
681 
682 static int asf_read_marker(AVFormatContext *s, int64_t size)
683 {
684  AVIOContext *pb = s->pb;
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  avio_rl16(pb); // entry length
703  avio_rl32(pb); // send time
704  avio_rl32(pb); // flags
705  name_len = avio_rl32(pb); // name length
706  if ((ret = avio_get_str16le(pb, name_len * 2, name,
707  sizeof(name))) < name_len)
708  avio_skip(pb, name_len - ret);
709  avpriv_new_chapter(s, i, (AVRational) { 1, 10000000 }, pres_time,
711  }
712 
713  return 0;
714 }
715 
717 {
718  ASFContext *asf = s->priv_data;
719  ff_asf_guid g;
720  AVIOContext *pb = s->pb;
721  int i;
722  int64_t gsize;
723 
724  ff_get_guid(pb, &g);
725  if (ff_guidcmp(&g, &ff_asf_header))
726  return AVERROR_INVALIDDATA;
727  avio_rl64(pb);
728  avio_rl32(pb);
729  avio_r8(pb);
730  avio_r8(pb);
731  memset(&asf->asfid2avid, -1, sizeof(asf->asfid2avid));
732  for (;;) {
733  uint64_t gpos = avio_tell(pb);
734  ff_get_guid(pb, &g);
735  gsize = avio_rl64(pb);
736  print_guid(&g);
737  if (!ff_guidcmp(&g, &ff_asf_data_header)) {
738  asf->data_object_offset = avio_tell(pb);
739  /* If not streaming, gsize is not unlimited (how?),
740  * and there is enough space in the file.. */
741  if (!(asf->hdr.flags & 0x01) && gsize >= 100)
742  asf->data_object_size = gsize - 24;
743  else
744  asf->data_object_size = (uint64_t)-1;
745  break;
746  }
747  if (gsize < 24)
748  return AVERROR_INVALIDDATA;
749  if (!ff_guidcmp(&g, &ff_asf_file_header)) {
750  int ret = asf_read_file_properties(s, gsize);
751  if (ret < 0)
752  return ret;
753  } else if (!ff_guidcmp(&g, &ff_asf_stream_header)) {
754  asf_read_stream_properties(s, gsize);
755  } else if (!ff_guidcmp(&g, &ff_asf_comment_header)) {
756  asf_read_content_desc(s, gsize);
757  } else if (!ff_guidcmp(&g, &ff_asf_language_guid)) {
758  asf_read_language_list(s, gsize);
759  } else if (!ff_guidcmp(&g, &ff_asf_extended_content_header)) {
760  asf_read_ext_content_desc(s, gsize);
761  } else if (!ff_guidcmp(&g, &ff_asf_metadata_header)) {
762  asf_read_metadata(s, gsize);
763  } else if (!ff_guidcmp(&g, &ff_asf_metadata_library_header)) {
764  asf_read_metadata(s, gsize);
765  } else if (!ff_guidcmp(&g, &ff_asf_ext_stream_header)) {
767 
768  // there could be a optional stream properties object to follow
769  // if so the next iteration will pick it up
770  continue;
771  } else if (!ff_guidcmp(&g, &ff_asf_head1_guid)) {
772  ff_get_guid(pb, &g);
773  avio_skip(pb, 6);
774  continue;
775  } else if (!ff_guidcmp(&g, &ff_asf_marker_header)) {
776  asf_read_marker(s, gsize);
777  } else if (url_feof(pb)) {
778  return AVERROR_EOF;
779  } else {
780  if (!s->keylen) {
782  unsigned int len;
783  AVPacket pkt;
785  "DRM protected stream detected, decoding will likely fail!\n");
786  len= avio_rl32(pb);
787  av_log(s, AV_LOG_DEBUG, "Secret data:\n");
788  av_get_packet(pb, &pkt, len); av_hex_dump_log(s, AV_LOG_DEBUG, pkt.data, pkt.size); av_free_packet(&pkt);
789  len= avio_rl32(pb);
790  get_tag(s, "ASF_Protection_Type", -1, len, 32);
791  len= avio_rl32(pb);
792  get_tag(s, "ASF_Key_ID", -1, len, 32);
793  len= avio_rl32(pb);
794  get_tag(s, "ASF_License_URL", -1, len, 32);
795  } else if (!ff_guidcmp(&g, &ff_asf_ext_content_encryption)) {
797  "Ext DRM protected stream detected, decoding will likely fail!\n");
798  av_dict_set(&s->metadata, "encryption", "ASF Extended Content Encryption", 0);
799  } else if (!ff_guidcmp(&g, &ff_asf_digital_signature)) {
800  av_log(s, AV_LOG_INFO, "Digital signature detected!\n");
801  }
802  }
803  }
804  if (avio_tell(pb) != gpos + gsize)
805  av_log(s, AV_LOG_DEBUG,
806  "gpos mismatch our pos=%"PRIu64", end=%"PRId64"\n",
807  avio_tell(pb) - gpos, gsize);
808  avio_seek(pb, gpos + gsize, SEEK_SET);
809  }
810  ff_get_guid(pb, &g);
811  avio_rl64(pb);
812  avio_r8(pb);
813  avio_r8(pb);
814  if (url_feof(pb))
815  return AVERROR_EOF;
816  asf->data_offset = avio_tell(pb);
817  asf->packet_size_left = 0;
818 
819  for (i = 0; i < 128; i++) {
820  int stream_num = asf->asfid2avid[i];
821  if (stream_num >= 0) {
822  AVStream *st = s->streams[stream_num];
823  if (!st->codec->bit_rate)
824  st->codec->bit_rate = asf->stream_bitrates[i];
825  if (asf->dar[i].num > 0 && asf->dar[i].den > 0) {
828  asf->dar[i].num, asf->dar[i].den, INT_MAX);
829  } else if ((asf->dar[0].num > 0) && (asf->dar[0].den > 0) &&
830  // Use ASF container value if the stream doesn't set AR.
834  asf->dar[0].num, asf->dar[0].den, INT_MAX);
835 
836  av_dlog(s, "i=%d, st->codec->codec_type:%d, asf->dar %d:%d sar=%d:%d\n",
837  i, st->codec->codec_type, asf->dar[i].num, asf->dar[i].den,
839 
840  // copy and convert language codes to the frontend
841  if (asf->streams[i].stream_language_index < 128) {
842  const char *rfc1766 = asf->stream_languages[asf->streams[i].stream_language_index];
843  if (rfc1766 && strlen(rfc1766) > 1) {
844  const char primary_tag[3] = { rfc1766[0], rfc1766[1], '\0' }; // ignore country code if any
845  const char *iso6392 = av_convert_lang_to(primary_tag,
847  if (iso6392)
848  av_dict_set(&st->metadata, "language", iso6392, 0);
849  }
850  }
851  }
852  }
853 
855 
856  return 0;
857 }
858 
859 #define DO_2BITS(bits, var, defval) \
860  switch (bits & 3) { \
861  case 3: \
862  var = avio_rl32(pb); \
863  rsize += 4; \
864  break; \
865  case 2: \
866  var = avio_rl16(pb); \
867  rsize += 2; \
868  break; \
869  case 1: \
870  var = avio_r8(pb); \
871  rsize++; \
872  break; \
873  default: \
874  var = defval; \
875  break; \
876  }
877 
878 /**
879  * Load a single ASF packet into the demuxer.
880  * @param s demux context
881  * @param pb context to read data from
882  * @return 0 on success, <0 on error
883  */
885 {
886  ASFContext *asf = s->priv_data;
887  uint32_t packet_length, padsize;
888  int rsize = 8;
889  int c, d, e, off;
890 
891  // if we do not know packet size, allow skipping up to 32 kB
892  off = 32768;
893  if (asf->no_resync_search)
894  off = 3;
895  else if (s->packet_size > 0)
896  off = (avio_tell(pb) - s->data_offset) % s->packet_size + 3;
897 
898  c = d = e = -1;
899  while (off-- > 0) {
900  c = d;
901  d = e;
902  e = avio_r8(pb);
903  if (c == 0x82 && !d && !e)
904  break;
905  }
906 
907  if (c != 0x82) {
908  /* This code allows handling of -EAGAIN at packet boundaries (i.e.
909  * if the packet sync code above triggers -EAGAIN). This does not
910  * imply complete -EAGAIN handling support at random positions in
911  * the stream. */
912  if (pb->error == AVERROR(EAGAIN))
913  return AVERROR(EAGAIN);
914  if (!url_feof(pb))
915  av_log(s, AV_LOG_ERROR,
916  "ff asf bad header %x at:%"PRId64"\n", c, avio_tell(pb));
917  }
918  if ((c & 0x8f) == 0x82) {
919  if (d || e) {
920  if (!url_feof(pb))
921  av_log(s, AV_LOG_ERROR, "ff asf bad non zero\n");
922  return AVERROR_INVALIDDATA;
923  }
924  c = avio_r8(pb);
925  d = avio_r8(pb);
926  rsize += 3;
927  } else if(!url_feof(pb)) {
928  avio_seek(pb, -1, SEEK_CUR); // FIXME
929  }
930 
931  asf->packet_flags = c;
932  asf->packet_property = d;
933 
934  DO_2BITS(asf->packet_flags >> 5, packet_length, s->packet_size);
935  DO_2BITS(asf->packet_flags >> 1, padsize, 0); // sequence ignored
936  DO_2BITS(asf->packet_flags >> 3, padsize, 0); // padding length
937 
938  // the following checks prevent overflows and infinite loops
939  if (!packet_length || packet_length >= (1U << 29)) {
940  av_log(s, AV_LOG_ERROR,
941  "invalid packet_length %d at:%"PRId64"\n",
942  packet_length, avio_tell(pb));
943  return AVERROR_INVALIDDATA;
944  }
945  if (padsize >= packet_length) {
946  av_log(s, AV_LOG_ERROR,
947  "invalid padsize %d at:%"PRId64"\n", padsize, avio_tell(pb));
948  return AVERROR_INVALIDDATA;
949  }
950 
951  asf->packet_timestamp = avio_rl32(pb);
952  avio_rl16(pb); /* duration */
953  // rsize has at least 11 bytes which have to be present
954 
955  if (asf->packet_flags & 0x01) {
956  asf->packet_segsizetype = avio_r8(pb);
957  rsize++;
958  asf->packet_segments = asf->packet_segsizetype & 0x3f;
959  } else {
960  asf->packet_segments = 1;
961  asf->packet_segsizetype = 0x80;
962  }
963  if (rsize > packet_length - padsize) {
964  asf->packet_size_left = 0;
965  av_log(s, AV_LOG_ERROR,
966  "invalid packet header length %d for pktlen %d-%d at %"PRId64"\n",
967  rsize, packet_length, padsize, avio_tell(pb));
968  return AVERROR_INVALIDDATA;
969  }
970  asf->packet_size_left = packet_length - padsize - rsize;
971  if (packet_length < asf->hdr.min_pktsize)
972  padsize += asf->hdr.min_pktsize - packet_length;
973  asf->packet_padsize = padsize;
974  av_dlog(s, "packet: size=%d padsize=%d left=%d\n",
976  return 0;
977 }
978 
979 /**
980  *
981  * @return <0 if error
982  */
984 {
985  ASFContext *asf = s->priv_data;
986  ASFStream *asfst;
987  int rsize = 1;
988  int num = avio_r8(pb);
989  int i;
990  int64_t ts0, ts1 av_unused;
991 
992  asf->packet_segments--;
993  asf->packet_key_frame = num >> 7;
994  asf->stream_index = asf->asfid2avid[num & 0x7f];
995  asfst = &asf->streams[num & 0x7f];
996  // sequence should be ignored!
997  DO_2BITS(asf->packet_property >> 4, asf->packet_seq, 0);
998  DO_2BITS(asf->packet_property >> 2, asf->packet_frag_offset, 0);
1000  av_dlog(asf, "key:%d stream:%d seq:%d offset:%d replic_size:%d\n",
1001  asf->packet_key_frame, asf->stream_index, asf->packet_seq,
1003  if (rsize+(int64_t)asf->packet_replic_size > asf->packet_size_left) {
1004  av_log(s, AV_LOG_ERROR, "packet_replic_size %d is invalid\n", asf->packet_replic_size);
1005  return AVERROR_INVALIDDATA;
1006  }
1007  if (asf->packet_replic_size >= 8) {
1008  int64_t end = avio_tell(pb) + asf->packet_replic_size;
1009  AVRational aspect;
1010  asf->packet_obj_size = avio_rl32(pb);
1011  if (asf->packet_obj_size >= (1 << 24) || asf->packet_obj_size <= 0) {
1012  av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
1013  asf->packet_obj_size = 0;
1014  return AVERROR_INVALIDDATA;
1015  }
1016  asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
1017 
1018  for (i = 0; i < asfst->payload_ext_ct; i++) {
1019  ASFPayload *p = &asfst->payload[i];
1020  int size = p->size;
1021  int64_t payend;
1022  if (size == 0xFFFF)
1023  size = avio_rl16(pb);
1024  payend = avio_tell(pb) + size;
1025  if (payend > end) {
1026  av_log(s, AV_LOG_ERROR, "too long payload\n");
1027  break;
1028  }
1029  switch (p->type) {
1030  case 0x50:
1031 // duration = avio_rl16(pb);
1032  break;
1033  case 0x54:
1034  aspect.num = avio_r8(pb);
1035  aspect.den = avio_r8(pb);
1036  if (aspect.num > 0 && aspect.den > 0 && asf->stream_index >= 0) {
1037  s->streams[asf->stream_index]->sample_aspect_ratio = aspect;
1038  }
1039  break;
1040  case 0x2A:
1041  avio_skip(pb, 8);
1042  ts0 = avio_rl64(pb);
1043  ts1 = avio_rl64(pb);
1044  if (ts0!= -1) asf->packet_frag_timestamp = ts0/10000;
1046  break;
1047  case 0x5B:
1048  case 0xB7:
1049  case 0xCC:
1050  case 0xC0:
1051  case 0xA0:
1052  //unknown
1053  break;
1054  }
1055  avio_seek(pb, payend, SEEK_SET);
1056  }
1057 
1058  avio_seek(pb, end, SEEK_SET);
1059  rsize += asf->packet_replic_size; // FIXME - check validity
1060  } else if (asf->packet_replic_size == 1) {
1061  // multipacket - frag_offset is beginning timestamp
1063  asf->packet_frag_offset = 0;
1065 
1066  asf->packet_time_delta = avio_r8(pb);
1067  rsize++;
1068  } else if (asf->packet_replic_size != 0) {
1069  av_log(s, AV_LOG_ERROR, "unexpected packet_replic_size of %d\n",
1070  asf->packet_replic_size);
1071  return AVERROR_INVALIDDATA;
1072  }
1073  if (asf->packet_flags & 0x01) {
1074  DO_2BITS(asf->packet_segsizetype >> 6, asf->packet_frag_size, 0); // 0 is illegal
1075  if (rsize > asf->packet_size_left) {
1076  av_log(s, AV_LOG_ERROR, "packet_replic_size is invalid\n");
1077  return AVERROR_INVALIDDATA;
1078  } else if (asf->packet_frag_size > asf->packet_size_left - rsize) {
1079  if (asf->packet_frag_size > asf->packet_size_left - rsize + asf->packet_padsize) {
1080  av_log(s, AV_LOG_ERROR, "packet_frag_size is invalid (%d-%d)\n",
1081  asf->packet_size_left, rsize);
1082  return AVERROR_INVALIDDATA;
1083  } else {
1084  int diff = asf->packet_frag_size - (asf->packet_size_left - rsize);
1085  asf->packet_size_left += diff;
1086  asf->packet_padsize -= diff;
1087  }
1088  }
1089  } else {
1090  asf->packet_frag_size = asf->packet_size_left - rsize;
1091  }
1092  if (asf->packet_replic_size == 1) {
1093  asf->packet_multi_size = asf->packet_frag_size;
1094  if (asf->packet_multi_size > asf->packet_size_left)
1095  return AVERROR_INVALIDDATA;
1096  }
1097  asf->packet_size_left -= rsize;
1098 
1099  return 0;
1100 }
1101 
1102 /**
1103  * Parse data from individual ASF packets (which were previously loaded
1104  * with asf_get_packet()).
1105  * @param s demux context
1106  * @param pb context to read data from
1107  * @param pkt pointer to store packet data into
1108  * @return 0 if data was stored in pkt, <0 on error or 1 if more ASF
1109  * packets need to be loaded (through asf_get_packet())
1110  */
1112 {
1113  ASFContext *asf = s->priv_data;
1114  ASFStream *asf_st = 0;
1115  for (;;) {
1116  int ret;
1117  if (url_feof(pb))
1118  return AVERROR_EOF;
1119 
1120  if (asf->packet_size_left < FRAME_HEADER_SIZE ||
1121  asf->packet_segments < 1) {
1122  int ret = asf->packet_size_left + asf->packet_padsize;
1123 
1124  assert(ret >= 0);
1125  /* fail safe */
1126  avio_skip(pb, ret);
1127 
1128  asf->packet_pos = avio_tell(pb);
1129  if (asf->data_object_size != (uint64_t)-1 &&
1130  (asf->packet_pos - asf->data_object_offset >= asf->data_object_size))
1131  return AVERROR_EOF; /* Do not exceed the size of the data object */
1132  return 1;
1133  }
1134  if (asf->packet_time_start == 0) {
1135  if (asf_read_frame_header(s, pb) < 0) {
1136  asf->packet_segments = 0;
1137  continue;
1138  }
1139  if (asf->stream_index < 0 ||
1140  s->streams[asf->stream_index]->discard >= AVDISCARD_ALL ||
1141  (!asf->packet_key_frame &&
1142  s->streams[asf->stream_index]->discard >= AVDISCARD_NONKEY)) {
1143  asf->packet_time_start = 0;
1144  /* unhandled packet (should not happen) */
1145  avio_skip(pb, asf->packet_frag_size);
1146  asf->packet_size_left -= asf->packet_frag_size;
1147  if (asf->stream_index < 0)
1148  av_log(s, AV_LOG_ERROR, "ff asf skip %d (unknown stream)\n",
1149  asf->packet_frag_size);
1150  continue;
1151  }
1152  asf->asf_st = s->streams[asf->stream_index]->priv_data;
1153  }
1154  asf_st = asf->asf_st;
1155  av_assert0(asf_st);
1156 
1157  if (asf->packet_replic_size == 1) {
1158  // frag_offset is here used as the beginning timestamp
1160  asf->packet_time_start += asf->packet_time_delta;
1161  asf->packet_obj_size = asf->packet_frag_size = avio_r8(pb);
1162  asf->packet_size_left--;
1163  asf->packet_multi_size--;
1164  if (asf->packet_multi_size < asf->packet_obj_size) {
1165  asf->packet_time_start = 0;
1166  avio_skip(pb, asf->packet_multi_size);
1167  asf->packet_size_left -= asf->packet_multi_size;
1168  continue;
1169  }
1170  asf->packet_multi_size -= asf->packet_obj_size;
1171  }
1172  if (asf_st->frag_offset + asf->packet_frag_size <= asf_st->pkt.size &&
1173  asf_st->frag_offset + asf->packet_frag_size > asf->packet_obj_size) {
1174  av_log(s, AV_LOG_INFO, "ignoring invalid packet_obj_size (%d %d %d %d)\n",
1175  asf_st->frag_offset, asf->packet_frag_size,
1176  asf->packet_obj_size, asf_st->pkt.size);
1177  asf->packet_obj_size = asf_st->pkt.size;
1178  }
1179 
1180  if (asf_st->pkt.size != asf->packet_obj_size ||
1181  // FIXME is this condition sufficient?
1182  asf_st->frag_offset + asf->packet_frag_size > asf_st->pkt.size) {
1183  if (asf_st->pkt.data) {
1184  av_log(s, AV_LOG_INFO,
1185  "freeing incomplete packet size %d, new %d\n",
1186  asf_st->pkt.size, asf->packet_obj_size);
1187  asf_st->frag_offset = 0;
1188  av_free_packet(&asf_st->pkt);
1189  }
1190  /* new packet */
1191  av_new_packet(&asf_st->pkt, asf->packet_obj_size);
1192  asf_st->seq = asf->packet_seq;
1193  asf_st->pkt.dts = asf->packet_frag_timestamp - asf->hdr.preroll;
1194  asf_st->pkt.stream_index = asf->stream_index;
1195  asf_st->pkt.pos = asf_st->packet_pos = asf->packet_pos;
1196 
1197  if (asf_st->pkt.data && asf_st->palette_changed) {
1198  uint8_t *pal;
1200  AVPALETTE_SIZE);
1201  if (!pal) {
1202  av_log(s, AV_LOG_ERROR, "Cannot append palette to packet\n");
1203  } else {
1204  memcpy(pal, asf_st->palette, AVPALETTE_SIZE);
1205  asf_st->palette_changed = 0;
1206  }
1207  }
1208  av_dlog(asf, "new packet: stream:%d key:%d packet_key:%d audio:%d size:%d\n",
1209  asf->stream_index, asf->packet_key_frame,
1210  asf_st->pkt.flags & AV_PKT_FLAG_KEY,
1212  asf->packet_obj_size);
1214  asf->packet_key_frame = 1;
1215  if (asf->packet_key_frame)
1216  asf_st->pkt.flags |= AV_PKT_FLAG_KEY;
1217  }
1218 
1219  /* read data */
1220  av_dlog(asf, "READ PACKET s:%d os:%d o:%d,%d l:%d DATA:%p\n",
1221  s->packet_size, asf_st->pkt.size, asf->packet_frag_offset,
1222  asf_st->frag_offset, asf->packet_frag_size, asf_st->pkt.data);
1223  asf->packet_size_left -= asf->packet_frag_size;
1224  if (asf->packet_size_left < 0)
1225  continue;
1226 
1227  if (asf->packet_frag_offset >= asf_st->pkt.size ||
1228  asf->packet_frag_size > asf_st->pkt.size - asf->packet_frag_offset) {
1229  av_log(s, AV_LOG_ERROR,
1230  "packet fragment position invalid %u,%u not in %u\n",
1232  asf_st->pkt.size);
1233  continue;
1234  }
1235 
1236  ret = avio_read(pb, asf_st->pkt.data + asf->packet_frag_offset,
1237  asf->packet_frag_size);
1238  if (ret != asf->packet_frag_size) {
1239  if (ret < 0 || asf->packet_frag_offset + ret == 0)
1240  return ret < 0 ? ret : AVERROR_EOF;
1241 
1242  if (asf_st->ds_span > 1) {
1243  // scrambling, we can either drop it completely or fill the remainder
1244  // TODO: should we fill the whole packet instead of just the current
1245  // fragment?
1246  memset(asf_st->pkt.data + asf->packet_frag_offset + ret, 0,
1247  asf->packet_frag_size - ret);
1248  ret = asf->packet_frag_size;
1249  } else {
1250  // no scrambling, so we can return partial packets
1251  av_shrink_packet(&asf_st->pkt, asf->packet_frag_offset + ret);
1252  }
1253  }
1254  if (s->key && s->keylen == 20)
1255  ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
1256  ret);
1257  asf_st->frag_offset += ret;
1258  /* test if whole packet is read */
1259  if (asf_st->frag_offset == asf_st->pkt.size) {
1260  // workaround for macroshit radio DVR-MS files
1262  asf_st->pkt.size > 100) {
1263  int i;
1264  for (i = 0; i < asf_st->pkt.size && !asf_st->pkt.data[i]; i++)
1265  ;
1266  if (i == asf_st->pkt.size) {
1267  av_log(s, AV_LOG_DEBUG, "discarding ms fart\n");
1268  asf_st->frag_offset = 0;
1269  av_free_packet(&asf_st->pkt);
1270  continue;
1271  }
1272  }
1273 
1274  /* return packet */
1275  if (asf_st->ds_span > 1) {
1276  if (asf_st->pkt.size != asf_st->ds_packet_size * asf_st->ds_span) {
1277  av_log(s, AV_LOG_ERROR,
1278  "pkt.size != ds_packet_size * ds_span (%d %d %d)\n",
1279  asf_st->pkt.size, asf_st->ds_packet_size,
1280  asf_st->ds_span);
1281  } else {
1282  /* packet descrambling */
1283  AVBufferRef *buf = av_buffer_alloc(asf_st->pkt.size +
1285  if (buf) {
1286  uint8_t *newdata = buf->data;
1287  int offset = 0;
1288  memset(newdata + asf_st->pkt.size, 0,
1290  while (offset < asf_st->pkt.size) {
1291  int off = offset / asf_st->ds_chunk_size;
1292  int row = off / asf_st->ds_span;
1293  int col = off % asf_st->ds_span;
1294  int idx = row + col * asf_st->ds_packet_size / asf_st->ds_chunk_size;
1295  assert(offset + asf_st->ds_chunk_size <= asf_st->pkt.size);
1296  assert(idx + 1 <= asf_st->pkt.size / asf_st->ds_chunk_size);
1297  memcpy(newdata + offset,
1298  asf_st->pkt.data + idx * asf_st->ds_chunk_size,
1299  asf_st->ds_chunk_size);
1300  offset += asf_st->ds_chunk_size;
1301  }
1302  av_buffer_unref(&asf_st->pkt.buf);
1303  asf_st->pkt.buf = buf;
1304  asf_st->pkt.data = buf->data;
1305  }
1306  }
1307  }
1308  asf_st->frag_offset = 0;
1309  *pkt = asf_st->pkt;
1310 #if FF_API_DESTRUCT_PACKET
1311  asf_st->pkt.destruct = NULL;
1312 #endif
1313  asf_st->pkt.buf = 0;
1314  asf_st->pkt.size = 0;
1315  asf_st->pkt.data = 0;
1316  asf_st->pkt.side_data_elems = 0;
1317  asf_st->pkt.side_data = NULL;
1318  break; // packet completed
1319  }
1320  }
1321  return 0;
1322 }
1323 
1325 {
1326  ASFContext *asf = s->priv_data;
1327 
1328  for (;;) {
1329  int ret;
1330 
1331  /* parse cached packets, if any */
1332  if ((ret = asf_parse_packet(s, s->pb, pkt)) <= 0)
1333  return ret;
1334  if ((ret = asf_get_packet(s, s->pb)) < 0)
1335  assert(asf->packet_size_left < FRAME_HEADER_SIZE ||
1336  asf->packet_segments < 1);
1337  asf->packet_time_start = 0;
1338  }
1339 }
1340 
1341 // Added to support seeking after packets have been read
1342 // If information is not reset, read_packet fails due to
1343 // leftover information from previous reads
1345 {
1346  ASFContext *asf = s->priv_data;
1347  ASFStream *asf_st;
1348  int i;
1349 
1350  asf->packet_size_left = 0;
1351  asf->packet_segments = 0;
1352  asf->packet_flags = 0;
1353  asf->packet_property = 0;
1354  asf->packet_timestamp = 0;
1355  asf->packet_segsizetype = 0;
1356  asf->packet_segments = 0;
1357  asf->packet_seq = 0;
1358  asf->packet_replic_size = 0;
1359  asf->packet_key_frame = 0;
1360  asf->packet_padsize = 0;
1361  asf->packet_frag_offset = 0;
1362  asf->packet_frag_size = 0;
1363  asf->packet_frag_timestamp = 0;
1364  asf->packet_multi_size = 0;
1365  asf->packet_obj_size = 0;
1366  asf->packet_time_delta = 0;
1367  asf->packet_time_start = 0;
1368 
1369  for (i = 0; i < s->nb_streams; i++) {
1370  asf_st = s->streams[i]->priv_data;
1371  if (!asf_st)
1372  continue;
1373  av_free_packet(&asf_st->pkt);
1374  asf_st->frag_offset = 0;
1375  asf_st->seq = 0;
1376  }
1377  asf->asf_st = NULL;
1378 }
1379 
1381 {
1382  asf_reset_header(s);
1383 
1384  return 0;
1385 }
1386 
1387 static int64_t asf_read_pts(AVFormatContext *s, int stream_index,
1388  int64_t *ppos, int64_t pos_limit)
1389 {
1390  AVPacket pkt1, *pkt = &pkt1;
1391  ASFStream *asf_st;
1392  int64_t pts;
1393  int64_t pos = *ppos;
1394  int i;
1395  int64_t start_pos[ASF_MAX_STREAMS];
1396 
1397  for (i = 0; i < s->nb_streams; i++)
1398  start_pos[i] = pos;
1399 
1400  if (s->packet_size > 0)
1401  pos = (pos + s->packet_size - 1 - s->data_offset) /
1402  s->packet_size * s->packet_size +
1403  s->data_offset;
1404  *ppos = pos;
1405  if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1406  return AV_NOPTS_VALUE;
1407 
1408  asf_reset_header(s);
1409  for (;;) {
1410  if (av_read_frame(s, pkt) < 0) {
1411  av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
1412  return AV_NOPTS_VALUE;
1413  }
1414 
1415  pts = pkt->dts;
1416 
1417  av_free_packet(pkt);
1418  if (pkt->flags & AV_PKT_FLAG_KEY) {
1419  i = pkt->stream_index;
1420 
1421  asf_st = s->streams[i]->priv_data;
1422  av_assert0(asf_st);
1423 
1424 // assert((asf_st->packet_pos - s->data_offset) % s->packet_size == 0);
1425  pos = asf_st->packet_pos;
1426 
1427  av_add_index_entry(s->streams[i], pos, pts, pkt->size,
1428  pos - start_pos[i] + 1, AVINDEX_KEYFRAME);
1429  start_pos[i] = asf_st->packet_pos + 1;
1430 
1431  if (pkt->stream_index == stream_index)
1432  break;
1433  }
1434  }
1435 
1436  *ppos = pos;
1437  return pts;
1438 }
1439 
1440 static void asf_build_simple_index(AVFormatContext *s, int stream_index)
1441 {
1442  ff_asf_guid g;
1443  ASFContext *asf = s->priv_data;
1444  int64_t current_pos = avio_tell(s->pb);
1445 
1446  if(avio_seek(s->pb, asf->data_object_offset + asf->data_object_size, SEEK_SET) < 0) {
1447  asf->index_read= -1;
1448  return;
1449  }
1450 
1451  ff_get_guid(s->pb, &g);
1452 
1453  /* the data object can be followed by other top-level objects,
1454  * skip them until the simple index object is reached */
1455  while (ff_guidcmp(&g, &ff_asf_simple_index_header)) {
1456  int64_t gsize = avio_rl64(s->pb);
1457  if (gsize < 24 || url_feof(s->pb)) {
1458  avio_seek(s->pb, current_pos, SEEK_SET);
1459  asf->index_read= -1;
1460  return;
1461  }
1462  avio_skip(s->pb, gsize - 24);
1463  ff_get_guid(s->pb, &g);
1464  }
1465 
1466  {
1467  int64_t itime, last_pos = -1;
1468  int pct, ict;
1469  int i;
1470  int64_t av_unused gsize = avio_rl64(s->pb);
1471  ff_get_guid(s->pb, &g);
1472  itime = avio_rl64(s->pb);
1473  pct = avio_rl32(s->pb);
1474  ict = avio_rl32(s->pb);
1475  av_log(s, AV_LOG_DEBUG,
1476  "itime:0x%"PRIx64", pct:%d, ict:%d\n", itime, pct, ict);
1477 
1478  for (i = 0; i < ict; i++) {
1479  int pktnum = avio_rl32(s->pb);
1480  int pktct = avio_rl16(s->pb);
1481  int64_t pos = s->data_offset + s->packet_size * (int64_t)pktnum;
1482  int64_t index_pts = FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);
1483 
1484  if (pos != last_pos) {
1485  av_log(s, AV_LOG_DEBUG, "pktnum:%d, pktct:%d pts: %"PRId64"\n",
1486  pktnum, pktct, index_pts);
1487  av_add_index_entry(s->streams[stream_index], pos, index_pts,
1488  s->packet_size, 0, AVINDEX_KEYFRAME);
1489  last_pos = pos;
1490  }
1491  }
1492  asf->index_read = ict > 1;
1493  }
1494  avio_seek(s->pb, current_pos, SEEK_SET);
1495 }
1496 
1497 static int asf_read_seek(AVFormatContext *s, int stream_index,
1498  int64_t pts, int flags)
1499 {
1500  ASFContext *asf = s->priv_data;
1501  AVStream *st = s->streams[stream_index];
1502 
1503  if (s->packet_size <= 0)
1504  return -1;
1505 
1506  /* Try using the protocol's read_seek if available */
1507  if (s->pb) {
1508  int ret = avio_seek_time(s->pb, stream_index, pts, flags);
1509  if (ret >= 0)
1510  asf_reset_header(s);
1511  if (ret != AVERROR(ENOSYS))
1512  return ret;
1513  }
1514 
1515  if (!asf->index_read)
1516  asf_build_simple_index(s, stream_index);
1517 
1518  if ((asf->index_read > 0 && st->index_entries)) {
1519  int index = av_index_search_timestamp(st, pts, flags);
1520  if (index >= 0) {
1521  /* find the position */
1522  uint64_t pos = st->index_entries[index].pos;
1523 
1524  /* do the seek */
1525  av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos);
1526  if(avio_seek(s->pb, pos, SEEK_SET) < 0)
1527  return -1;
1528  asf_reset_header(s);
1529  return 0;
1530  }
1531  }
1532  /* no index or seeking by index failed */
1533  if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
1534  return -1;
1535  asf_reset_header(s);
1536  return 0;
1537 }
1538 
1540  .name = "asf",
1541  .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1542  .priv_data_size = sizeof(ASFContext),
1543  .read_probe = asf_probe,
1548  .read_timestamp = asf_read_pts,
1550  .priv_class = &asf_class,
1551 };