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