FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
asfenc.c
Go to the documentation of this file.
1 /*
2  * ASF muxer
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/avassert.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/mathematics.h"
25 #include "avformat.h"
26 #include "avio_internal.h"
27 #include "internal.h"
28 #include "riff.h"
29 #include "asf.h"
30 
31 #define ASF_INDEXED_INTERVAL 10000000
32 #define ASF_INDEX_BLOCK (1<<9)
33 #define ASF_PAYLOADS_PER_PACKET 63
34 
35 #define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
36 #define ASF_PACKET_ERROR_CORRECTION_FLAGS \
37  (ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT | \
38  ASF_PACKET_ERROR_CORRECTION_DATA_SIZE)
39 
40 #if (ASF_PACKET_ERROR_CORRECTION_FLAGS != 0)
41 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 1
42 #else
43 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 0
44 #endif
45 
46 #define ASF_PPI_PROPERTY_FLAGS \
47  (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE | \
48  ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD | \
49  ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE | \
50  ASF_PL_FLAG_STREAM_NUMBER_LENGTH_FIELD_IS_BYTE)
51 
52 #define ASF_PPI_LENGTH_TYPE_FLAGS 0
53 
54 #define ASF_PAYLOAD_FLAGS ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD
55 
56 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
57 # define ASF_PPI_SEQUENCE_FIELD_SIZE 1
58 #endif
59 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
60 # define ASF_PPI_SEQUENCE_FIELD_SIZE 2
61 #endif
62 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
63 # define ASF_PPI_SEQUENCE_FIELD_SIZE 4
64 #endif
65 #ifndef ASF_PPI_SEQUENCE_FIELD_SIZE
66 # define ASF_PPI_SEQUENCE_FIELD_SIZE 0
67 #endif
68 
69 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
70 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 1
71 #endif
72 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
73 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 2
74 #endif
75 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
76 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 4
77 #endif
78 #ifndef ASF_PPI_PACKET_LENGTH_FIELD_SIZE
79 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 0
80 #endif
81 
82 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
83 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 1
84 #endif
85 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
86 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 2
87 #endif
88 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
89 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 4
90 #endif
91 #ifndef ASF_PPI_PADDING_LENGTH_FIELD_SIZE
92 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 0
93 #endif
94 
95 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
96 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 1
97 #endif
98 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
99 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 2
100 #endif
101 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
102 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 4
103 #endif
104 #ifndef ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE
105 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 0
106 #endif
107 
108 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
109 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 1
110 #endif
111 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
112 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 2
113 #endif
114 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
115 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 4
116 #endif
117 #ifndef ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE
118 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 0
119 #endif
120 
121 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
122 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 1
123 #endif
124 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
125 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 2
126 #endif
127 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
128 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 4
129 #endif
130 #ifndef ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE
131 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 0
132 #endif
133 
134 #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_BYTE == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
135 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 1
136 #endif
137 #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
138 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 2
139 #endif
140 #ifndef ASF_PAYLOAD_LENGTH_FIELD_SIZE
141 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 0
142 #endif
143 
144 #define PACKET_HEADER_MIN_SIZE \
145  (ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE + \
146  ASF_PACKET_ERROR_CORRECTION_DATA_SIZE + \
147  1 + /* Length Type Flags */ \
148  1 + /* Property Flags */ \
149  ASF_PPI_PACKET_LENGTH_FIELD_SIZE + \
150  ASF_PPI_SEQUENCE_FIELD_SIZE + \
151  ASF_PPI_PADDING_LENGTH_FIELD_SIZE + \
152  4 + /* Send Time Field */ \
153  2) /* Duration Field */
154 
155 // Replicated Data shall be at least 8 bytes long.
156 #define ASF_PAYLOAD_REPLICATED_DATA_LENGTH 0x08
157 
158 #define PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD \
159  (1 + /* Stream Number */ \
160  ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
161  ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
162  ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
163  ASF_PAYLOAD_REPLICATED_DATA_LENGTH)
164 
165 #define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS \
166  (1 + /* Stream Number */ \
167  ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
168  ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
169  ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
170  ASF_PAYLOAD_REPLICATED_DATA_LENGTH + \
171  ASF_PAYLOAD_LENGTH_FIELD_SIZE)
172 
173 #define SINGLE_PAYLOAD_DATA_LENGTH \
174  (PACKET_SIZE - \
175  PACKET_HEADER_MIN_SIZE - \
176  PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD)
177 
178 #define MULTI_PAYLOAD_CONSTANT \
179  (PACKET_SIZE - \
180  PACKET_HEADER_MIN_SIZE - \
181  1 - /* Payload Flags */ \
182  2 * PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS)
183 
184 #define DATA_HEADER_SIZE 50
185 
186 typedef struct ASFContext {
187  uint32_t seqno;
189  ASFStream streams[128]; ///< it's max number and it's not that big
190  /* non streamed additonnal info */
191  uint64_t nb_packets; ///< how many packets are there in the file, invalid if broadcasting
192  int64_t duration; ///< in 100ns units
193  /* packet filling */
194  unsigned char multi_payloads_present;
195  int packet_size_left;
198  unsigned int packet_nb_payloads;
201  /* only for reading */
202  uint64_t data_offset; ///< beginning of the first data packet
203 
206  uint16_t maximum_packet;
211  int end_sec;
212 } ASFContext;
213 
214 static const AVCodecTag codec_asf_bmp_tags[] = {
215  { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
216  { AV_CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
217  { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
218  { AV_CODEC_ID_NONE, 0 },
219 };
220 
221 #define PREROLL_TIME 3100
222 
223 static void put_str16(AVIOContext *s, const char *tag)
224 {
225  int len;
226  uint8_t *pb;
227  AVIOContext *dyn_buf;
228  if (avio_open_dyn_buf(&dyn_buf) < 0)
229  return;
230 
231  avio_put_str16le(dyn_buf, tag);
232  len = avio_close_dyn_buf(dyn_buf, &pb);
233  avio_wl16(s, len);
234  avio_write(s, pb, len);
235  av_freep(&pb);
236 }
237 
238 static int64_t put_header(AVIOContext *pb, const ff_asf_guid *g)
239 {
240  int64_t pos;
241 
242  pos = avio_tell(pb);
243  ff_put_guid(pb, g);
244  avio_wl64(pb, 24);
245  return pos;
246 }
247 
248 /* update header size */
249 static void end_header(AVIOContext *pb, int64_t pos)
250 {
251  int64_t pos1;
252 
253  pos1 = avio_tell(pb);
254  avio_seek(pb, pos + 16, SEEK_SET);
255  avio_wl64(pb, pos1 - pos);
256  avio_seek(pb, pos1, SEEK_SET);
257 }
258 
259 /* write an asf chunk (only used in streaming case) */
260 static void put_chunk(AVFormatContext *s, int type,
261  int payload_length, int flags)
262 {
263  ASFContext *asf = s->priv_data;
264  AVIOContext *pb = s->pb;
265  int length;
266 
267  length = payload_length + 8;
268  avio_wl16(pb, type);
269  avio_wl16(pb, length); // size
270  avio_wl32(pb, asf->seqno); // sequence number
271  avio_wl16(pb, flags); // unknown bytes
272  avio_wl16(pb, length); // size_confirm
273  asf->seqno++;
274 }
275 
276 /* convert from unix to windows time */
277 static int64_t unix_to_file_time(int ti)
278 {
279  int64_t t;
280 
281  t = ti * INT64_C(10000000);
282  t += INT64_C(116444736000000000);
283  return t;
284 }
285 
286 static int32_t get_send_time(ASFContext *asf, int64_t pres_time, uint64_t *offset)
287 {
288  int i;
289  int32_t send_time = 0;
290  *offset = asf->data_offset + DATA_HEADER_SIZE;
291  for (i = 0; i < asf->next_start_sec; i++) {
292  if (pres_time <= asf->index_ptr[i].send_time)
293  break;
294  send_time = asf->index_ptr[i].send_time;
295  *offset = asf->index_ptr[i].offset;
296  }
297 
298  return send_time / 10000;
299 }
300 
302 {
303  ASFContext *asf = s->priv_data;
304  AVIOContext *pb = s->pb;
305  int i;
306  AVRational scale = {1, 10000000};
307  int64_t hpos = put_header(pb, &ff_asf_marker_header);
308 
309  ff_put_guid(pb, &ff_asf_reserved_4);// ASF spec mandates this reserved value
310  avio_wl32(pb, s->nb_chapters); // markers count
311  avio_wl16(pb, 0); // ASF spec mandates 0 for this
312  avio_wl16(pb, 0); // name length 0, no name given
313 
314  for (i = 0; i < s->nb_chapters; i++) {
315  AVChapter *c = s->chapters[i];
316  AVDictionaryEntry *t = av_dict_get(c->metadata, "title", NULL, 0);
317  int64_t pres_time = av_rescale_q(c->start, c->time_base, scale);
318  uint64_t offset;
319  int32_t send_time = get_send_time(asf, pres_time, &offset);
320  int len = 0;
321  uint8_t *buf;
322  AVIOContext *dyn_buf;
323  if (t) {
324  if (avio_open_dyn_buf(&dyn_buf) < 0)
325  return AVERROR(ENOMEM);
326  avio_put_str16le(dyn_buf, t->value);
327  len = avio_close_dyn_buf(dyn_buf, &buf);
328  }
329  avio_wl64(pb, offset); // offset of the packet with send_time
330  avio_wl64(pb, pres_time + PREROLL_TIME * 10000); // presentation time
331  avio_wl16(pb, 12 + len); // entry length
332  avio_wl32(pb, send_time); // send time
333  avio_wl32(pb, 0); // flags, should be 0
334  avio_wl32(pb, len / 2); // marker desc length in WCHARS!
335  if (t) {
336  avio_write(pb, buf, len); // marker desc
337  av_freep(&buf);
338  }
339  }
340  end_header(pb, hpos);
341  return 0;
342 }
343 
344 /* write the header (used two times if non streamed) */
345 static int asf_write_header1(AVFormatContext *s, int64_t file_size,
346  int64_t data_chunk_size)
347 {
348  ASFContext *asf = s->priv_data;
349  AVIOContext *pb = s->pb;
350  AVDictionaryEntry *tags[5];
351  int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
352  int has_title, has_aspect_ratio = 0;
353  int metadata_count;
354  AVCodecContext *enc;
355  int64_t header_offset, cur_pos, hpos;
356  int bit_rate;
357  int64_t duration;
358 
360 
361  tags[0] = av_dict_get(s->metadata, "title", NULL, 0);
362  tags[1] = av_dict_get(s->metadata, "author", NULL, 0);
363  tags[2] = av_dict_get(s->metadata, "copyright", NULL, 0);
364  tags[3] = av_dict_get(s->metadata, "comment", NULL, 0);
365  tags[4] = av_dict_get(s->metadata, "rating", NULL, 0);
366 
367  duration = asf->duration + PREROLL_TIME * 10000;
368  has_title = tags[0] || tags[1] || tags[2] || tags[3] || tags[4];
369  metadata_count = av_dict_count(s->metadata);
370 
371  bit_rate = 0;
372  for (n = 0; n < s->nb_streams; n++) {
373  enc = s->streams[n]->codec;
374 
375  avpriv_set_pts_info(s->streams[n], 32, 1, 1000); /* 32 bit pts in ms */
376 
377  bit_rate += enc->bit_rate;
378  if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
379  && enc->sample_aspect_ratio.num > 0
380  && enc->sample_aspect_ratio.den > 0)
381  has_aspect_ratio++;
382  }
383 
384  if (asf->is_streamed) {
385  put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
386  }
387 
389  avio_wl64(pb, -1); /* header length, will be patched after */
390  avio_wl32(pb, 3 + has_title + !!metadata_count + s->nb_streams); /* number of chunks in header */
391  avio_w8(pb, 1); /* ??? */
392  avio_w8(pb, 2); /* ??? */
393 
394  /* file header */
395  header_offset = avio_tell(pb);
396  hpos = put_header(pb, &ff_asf_file_header);
398  avio_wl64(pb, file_size);
399  file_time = 0;
400  avio_wl64(pb, unix_to_file_time(file_time));
401  avio_wl64(pb, asf->nb_packets); /* number of packets */
402  avio_wl64(pb, duration); /* end time stamp (in 100ns units) */
403  avio_wl64(pb, asf->duration); /* duration (in 100ns units) */
404  avio_wl64(pb, PREROLL_TIME); /* start time stamp */
405  avio_wl32(pb, (asf->is_streamed || !pb->seekable) ? 3 : 2); /* ??? */
406  avio_wl32(pb, s->packet_size); /* packet size */
407  avio_wl32(pb, s->packet_size); /* packet size */
408  avio_wl32(pb, bit_rate ? bit_rate : -1); /* Maximum data rate in bps */
409  end_header(pb, hpos);
410 
411  /* unknown headers */
412  hpos = put_header(pb, &ff_asf_head1_guid);
414  avio_wl16(pb, 6);
415  if (has_aspect_ratio) {
416  int64_t hpos2;
417  avio_wl32(pb, 26 + has_aspect_ratio * 84);
418  hpos2 = put_header(pb, &ff_asf_metadata_header);
419  avio_wl16(pb, 2 * has_aspect_ratio);
420  for (n = 0; n < s->nb_streams; n++) {
421  enc = s->streams[n]->codec;
422  if ( enc->codec_type == AVMEDIA_TYPE_VIDEO
423  && enc->sample_aspect_ratio.num > 0
424  && enc->sample_aspect_ratio.den > 0) {
425  AVRational sar = enc->sample_aspect_ratio;
426  avio_wl16(pb, 0);
427  // the stream number is set like this below
428  avio_wl16(pb, n + 1);
429  avio_wl16(pb, 26); // name_len
430  avio_wl16(pb, 3); // value_type
431  avio_wl32(pb, 4); // value_len
432  avio_put_str16le(pb, "AspectRatioX");
433  avio_wl32(pb, sar.num);
434  avio_wl16(pb, 0);
435  // the stream number is set like this below
436  avio_wl16(pb, n + 1);
437  avio_wl16(pb, 26); // name_len
438  avio_wl16(pb, 3); // value_type
439  avio_wl32(pb, 4); // value_len
440  avio_put_str16le(pb, "AspectRatioY");
441  avio_wl32(pb, sar.den);
442  }
443  }
444  end_header(pb, hpos2);
445  } else {
446  avio_wl32(pb, 0);
447  }
448  end_header(pb, hpos);
449 
450  /* title and other infos */
451  if (has_title) {
452  int len;
453  uint8_t *buf;
454  AVIOContext *dyn_buf;
455 
456  if (avio_open_dyn_buf(&dyn_buf) < 0)
457  return AVERROR(ENOMEM);
458 
459  hpos = put_header(pb, &ff_asf_comment_header);
460 
461  for (n = 0; n < FF_ARRAY_ELEMS(tags); n++) {
462  len = tags[n] ? avio_put_str16le(dyn_buf, tags[n]->value) : 0;
463  avio_wl16(pb, len);
464  }
465  len = avio_close_dyn_buf(dyn_buf, &buf);
466  avio_write(pb, buf, len);
467  av_freep(&buf);
468  end_header(pb, hpos);
469  }
470  if (metadata_count) {
473  avio_wl16(pb, metadata_count);
474  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
475  put_str16(pb, tag->key);
476  avio_wl16(pb, 0);
477  put_str16(pb, tag->value);
478  }
479  end_header(pb, hpos);
480  }
481  /* chapters using ASF markers */
482  if (!asf->is_streamed && s->nb_chapters) {
483  int ret;
484  if ((ret = asf_write_markers(s)) < 0)
485  return ret;
486  }
487  /* stream headers */
488  for (n = 0; n < s->nb_streams; n++) {
489  int64_t es_pos;
490  // ASFStream *stream = &asf->streams[n];
491 
492  enc = s->streams[n]->codec;
493  asf->streams[n].num = n + 1;
494  asf->streams[n].seq = 1;
495 
496  switch (enc->codec_type) {
497  case AVMEDIA_TYPE_AUDIO:
498  wav_extra_size = 0;
499  extra_size = 18 + wav_extra_size;
500  extra_size2 = 8;
501  break;
502  default:
503  case AVMEDIA_TYPE_VIDEO:
504  wav_extra_size = enc->extradata_size;
505  extra_size = 0x33 + wav_extra_size;
506  extra_size2 = 0;
507  break;
508  }
509 
510  hpos = put_header(pb, &ff_asf_stream_header);
511  if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
514  } else {
517  }
518  avio_wl64(pb, 0); /* ??? */
519  es_pos = avio_tell(pb);
520  avio_wl32(pb, extra_size); /* wav header len */
521  avio_wl32(pb, extra_size2); /* additional data len */
522  avio_wl16(pb, n + 1); /* stream number */
523  avio_wl32(pb, 0); /* ??? */
524 
525  if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
526  /* WAVEFORMATEX header */
528 
529  if (wavsize < 0)
530  return -1;
531  if (wavsize != extra_size) {
532  cur_pos = avio_tell(pb);
533  avio_seek(pb, es_pos, SEEK_SET);
534  avio_wl32(pb, wavsize); /* wav header len */
535  avio_seek(pb, cur_pos, SEEK_SET);
536  }
537  /* ERROR Correction */
538  avio_w8(pb, 0x01);
539  if (enc->codec_id == AV_CODEC_ID_ADPCM_G726 || !enc->block_align) {
540  avio_wl16(pb, 0x0190);
541  avio_wl16(pb, 0x0190);
542  } else {
543  avio_wl16(pb, enc->block_align);
544  avio_wl16(pb, enc->block_align);
545  }
546  avio_wl16(pb, 0x01);
547  avio_w8(pb, 0x00);
548  } else {
549  avio_wl32(pb, enc->width);
550  avio_wl32(pb, enc->height);
551  avio_w8(pb, 2); /* ??? */
552  avio_wl16(pb, 40 + enc->extradata_size); /* size */
553 
554  /* BITMAPINFOHEADER header */
555  ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 1, 0);
556  }
557  end_header(pb, hpos);
558  }
559 
560  /* media comments */
561 
564  avio_wl32(pb, s->nb_streams);
565  for (n = 0; n < s->nb_streams; n++) {
566  const AVCodecDescriptor *codec_desc;
567  const char *desc;
568 
569  enc = s->streams[n]->codec;
570  codec_desc = avcodec_descriptor_get(enc->codec_id);
571 
572  if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
573  avio_wl16(pb, 2);
574  else if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
575  avio_wl16(pb, 1);
576  else
577  avio_wl16(pb, -1);
578 
579  if (enc->codec_id == AV_CODEC_ID_WMAV2)
580  desc = "Windows Media Audio V8";
581  else
582  desc = codec_desc ? codec_desc->name : NULL;
583 
584  if (desc) {
585  AVIOContext *dyn_buf;
586  uint8_t *buf;
587  int len;
588 
589  if (avio_open_dyn_buf(&dyn_buf) < 0)
590  return AVERROR(ENOMEM);
591 
592  avio_put_str16le(dyn_buf, desc);
593  len = avio_close_dyn_buf(dyn_buf, &buf);
594  avio_wl16(pb, len / 2); // "number of characters" = length in bytes / 2
595 
596  avio_write(pb, buf, len);
597  av_freep(&buf);
598  } else
599  avio_wl16(pb, 0);
600 
601  avio_wl16(pb, 0); /* no parameters */
602 
603  /* id */
604  if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
605  avio_wl16(pb, 2);
606  avio_wl16(pb, enc->codec_tag);
607  } else {
608  avio_wl16(pb, 4);
609  avio_wl32(pb, enc->codec_tag);
610  }
611  if (!enc->codec_tag)
612  return -1;
613  }
614  end_header(pb, hpos);
615 
616  /* patch the header size fields */
617 
618  cur_pos = avio_tell(pb);
619  header_size = cur_pos - header_offset;
620  if (asf->is_streamed) {
621  header_size += 8 + 30 + DATA_HEADER_SIZE;
622 
623  avio_seek(pb, header_offset - 10 - 30, SEEK_SET);
624  avio_wl16(pb, header_size);
625  avio_seek(pb, header_offset - 2 - 30, SEEK_SET);
626  avio_wl16(pb, header_size);
627 
628  header_size -= 8 + 30 + DATA_HEADER_SIZE;
629  }
630  header_size += 24 + 6;
631  avio_seek(pb, header_offset - 14, SEEK_SET);
632  avio_wl64(pb, header_size);
633  avio_seek(pb, cur_pos, SEEK_SET);
634 
635  /* movie chunk, followed by packets of packet_size */
636  asf->data_offset = cur_pos;
638  avio_wl64(pb, data_chunk_size);
640  avio_wl64(pb, asf->nb_packets); /* nb packets */
641  avio_w8(pb, 1); /* ??? */
642  avio_w8(pb, 1); /* ??? */
643  return 0;
644 }
645 
647 {
648  ASFContext *asf = s->priv_data;
649 
651  s->max_interleave_delta = 0;
652  asf->nb_packets = 0;
653 
654  asf->index_ptr = av_malloc(sizeof(ASFIndex) * ASF_INDEX_BLOCK);
656  asf->maximum_packet = 0;
657 
658  /* the data-chunk-size has to be 50 (DATA_HEADER_SIZE), which is
659  * data_size - asf->data_offset at the moment this function is done.
660  * It is needed to use asf as a streamable format. */
661  if (asf_write_header1(s, 0, DATA_HEADER_SIZE) < 0) {
662  //av_free(asf);
663  return -1;
664  }
665 
666  avio_flush(s->pb);
667 
668  asf->packet_nb_payloads = 0;
669  asf->packet_timestamp_start = -1;
670  asf->packet_timestamp_end = -1;
671  ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
672  NULL, NULL, NULL, NULL);
673 
674  if (s->avoid_negative_ts < 0)
675  s->avoid_negative_ts = 1;
676 
677  return 0;
678 }
679 
681 {
682  ASFContext *asf = s->priv_data;
683 
684  asf->is_streamed = 1;
685 
686  return asf_write_header(s);
687 }
688 
690  unsigned sendtime, unsigned duration,
691  int nb_payloads, int padsize)
692 {
693  ASFContext *asf = s->priv_data;
694  AVIOContext *pb = s->pb;
695  int ppi_size, i;
696  int64_t start = avio_tell(pb);
697 
698  int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
699 
700  padsize -= PACKET_HEADER_MIN_SIZE;
701  if (asf->multi_payloads_present)
702  padsize--;
703  av_assert0(padsize >= 0);
704 
706  for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++)
707  avio_w8(pb, 0x0);
708 
709  if (asf->multi_payloads_present)
710  iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
711 
712  if (padsize > 0) {
713  if (padsize < 256)
714  iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
715  else
716  iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
717  }
718  avio_w8(pb, iLengthTypeFlags);
719 
721 
722  if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
723  avio_wl16(pb, padsize - 2);
724  if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
725  avio_w8(pb, padsize - 1);
726 
727  avio_wl32(pb, sendtime);
728  avio_wl16(pb, duration);
729  if (asf->multi_payloads_present)
730  avio_w8(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
731 
732  ppi_size = avio_tell(pb) - start;
733 
734  return ppi_size;
735 }
736 
738 {
739  ASFContext *asf = s->priv_data;
740  int packet_hdr_size, packet_filled_size;
741 
743 
744  if (asf->is_streamed)
745  put_chunk(s, 0x4424, s->packet_size, 0);
746 
747  packet_hdr_size = put_payload_parsing_info(s,
750  asf->packet_nb_payloads,
751  asf->packet_size_left);
752 
753  packet_filled_size = PACKET_SIZE - asf->packet_size_left;
754  av_assert0(packet_hdr_size <= asf->packet_size_left);
755  memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
756 
757  avio_write(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
758 
759  avio_flush(s->pb);
760  asf->nb_packets++;
761  asf->packet_nb_payloads = 0;
762  asf->packet_timestamp_start = -1;
763  asf->packet_timestamp_end = -1;
764  ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
765  NULL, NULL, NULL, NULL);
766 }
767 
769  int64_t presentation_time, int m_obj_size,
770  int m_obj_offset, int payload_len, int flags)
771 {
772  ASFContext *asf = s->priv_data;
773  AVIOContext *pb = &asf->pb;
774  int val;
775 
776  val = stream->num;
777  if (flags & AV_PKT_FLAG_KEY)
778  val |= ASF_PL_FLAG_KEY_FRAME;
779  avio_w8(pb, val);
780 
781  avio_w8(pb, stream->seq); // Media object number
782  avio_wl32(pb, m_obj_offset); // Offset Into Media Object
783 
784  // Replicated Data shall be at least 8 bytes long.
785  // The first 4 bytes of data shall contain the
786  // Size of the Media Object that the payload belongs to.
787  // The next 4 bytes of data shall contain the
788  // Presentation Time for the media object that the payload belongs to.
790 
791  avio_wl32(pb, m_obj_size); // Replicated Data - Media Object Size
792  avio_wl32(pb, (uint32_t) presentation_time); // Replicated Data - Presentation Time
793 
794  if (asf->multi_payloads_present) {
795  avio_wl16(pb, payload_len); // payload length
796  }
797 }
798 
799 static void put_frame(AVFormatContext *s, ASFStream *stream, AVStream *avst,
800  int64_t timestamp, const uint8_t *buf,
801  int m_obj_size, int flags)
802 {
803  ASFContext *asf = s->priv_data;
804  int m_obj_offset, payload_len, frag_len1;
805 
806  m_obj_offset = 0;
807  while (m_obj_offset < m_obj_size) {
808  payload_len = m_obj_size - m_obj_offset;
809  if (asf->packet_timestamp_start == -1) {
810  asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
811 
813  if (asf->multi_payloads_present) {
814  frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
815  } else {
816  frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
817  }
818  asf->packet_timestamp_start = timestamp;
819  } else {
820  // multi payloads
821  frag_len1 = asf->packet_size_left -
824 
825  if (frag_len1 < payload_len &&
826  avst->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
827  flush_packet(s);
828  continue;
829  }
830  }
831  if (frag_len1 > 0) {
832  if (payload_len > frag_len1)
833  payload_len = frag_len1;
834  else if (payload_len == (frag_len1 - 1))
835  payload_len = frag_len1 - 2; // additional byte need to put padding length
836 
837  put_payload_header(s, stream, timestamp + PREROLL_TIME,
838  m_obj_size, m_obj_offset, payload_len, flags);
839  avio_write(&asf->pb, buf, payload_len);
840 
841  if (asf->multi_payloads_present)
843  else
845  asf->packet_timestamp_end = timestamp;
846 
847  asf->packet_nb_payloads++;
848  } else {
849  payload_len = 0;
850  }
851  m_obj_offset += payload_len;
852  buf += payload_len;
853 
854  if (!asf->multi_payloads_present)
855  flush_packet(s);
857  flush_packet(s);
859  flush_packet(s);
860  }
861  stream->seq++;
862 }
863 
864 static int update_index(AVFormatContext *s, int start_sec,
865  uint32_t packet_number, uint16_t packet_count,
866  uint64_t packet_offset)
867 {
868  ASFContext *asf = s->priv_data;
869 
870  if (start_sec > asf->next_start_sec) {
871  int i;
872 
873  if (!asf->next_start_sec) {
874  asf->next_packet_number = packet_number;
875  asf->next_packet_count = packet_count;
876  asf->next_packet_offset = packet_offset;
877  }
878 
879  if (start_sec > asf->nb_index_memory_alloc) {
880  int err;
881  asf->nb_index_memory_alloc = (start_sec + ASF_INDEX_BLOCK) & ~(ASF_INDEX_BLOCK - 1);
882  if ((err = av_reallocp_array(&asf->index_ptr,
884  sizeof(*asf->index_ptr))) < 0) {
885  asf->nb_index_memory_alloc = 0;
886  return err;
887  }
888  }
889  for (i = asf->next_start_sec; i < start_sec; i++) {
892  asf->index_ptr[i].send_time = asf->next_start_sec * INT64_C(10000000);
893  asf->index_ptr[i].offset = asf->next_packet_offset;
894 
895  }
896  }
897  asf->maximum_packet = FFMAX(asf->maximum_packet, packet_count);
898  asf->next_packet_number = packet_number;
899  asf->next_packet_count = packet_count;
900  asf->next_packet_offset = packet_offset;
901  asf->next_start_sec = start_sec;
902 
903  return 0;
904 }
905 
907 {
908  ASFContext *asf = s->priv_data;
909  AVIOContext *pb = s->pb;
910  ASFStream *stream;
911  AVCodecContext *codec;
912  uint32_t packet_number;
913  int64_t pts;
914  int start_sec;
915  int flags = pkt->flags;
916  int ret;
917  uint64_t offset = avio_tell(pb);
918 
919  codec = s->streams[pkt->stream_index]->codec;
920  stream = &asf->streams[pkt->stream_index];
921 
922  if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
923  flags &= ~AV_PKT_FLAG_KEY;
924 
925  pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
926  av_assert0(pts != AV_NOPTS_VALUE);
927  pts *= 10000;
928  asf->duration = FFMAX(asf->duration, pts + pkt->duration * 10000);
929 
930  packet_number = asf->nb_packets;
931  put_frame(s, stream, s->streams[pkt->stream_index],
932  pkt->dts, pkt->data, pkt->size, flags);
933 
934  start_sec = (int)((PREROLL_TIME * 10000 + pts + ASF_INDEXED_INTERVAL - 1)
936 
937  /* check index */
938  if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
939  uint16_t packet_count = asf->nb_packets - packet_number;
940  ret = update_index(s, start_sec, packet_number, packet_count, offset);
941  if (ret < 0)
942  return ret;
943  }
944  asf->end_sec = start_sec;
945 
946  return 0;
947 }
948 
950  uint16_t max, uint32_t count)
951 {
952  AVIOContext *pb = s->pb;
953  int i;
954 
956  avio_wl64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2) * count);
959  avio_wl32(pb, max);
960  avio_wl32(pb, count);
961  for (i = 0; i < count; i++) {
962  avio_wl32(pb, index[i].packet_number);
963  avio_wl16(pb, index[i].packet_count);
964  }
965 
966  return 0;
967 }
968 
970 {
971  ASFContext *asf = s->priv_data;
972  int64_t file_size, data_size;
973  int ret;
974 
975  /* flush the current packet */
976  if (asf->pb.buf_ptr > asf->pb.buffer)
977  flush_packet(s);
978 
979  /* write index */
980  data_size = avio_tell(s->pb);
981  if (!asf->is_streamed && asf->next_start_sec) {
982  if ((ret = update_index(s, asf->end_sec + 1, 0, 0, 0)) < 0)
983  return ret;
985  }
986  avio_flush(s->pb);
987 
988  if (asf->is_streamed || !s->pb->seekable) {
989  put_chunk(s, 0x4524, 0, 0); /* end of stream */
990  } else {
991  /* rewrite an updated header */
992  file_size = avio_tell(s->pb);
993  avio_seek(s->pb, 0, SEEK_SET);
994  asf_write_header1(s, file_size, data_size - asf->data_offset);
995  }
996 
997  av_freep(&asf->index_ptr);
998  return 0;
999 }
1000 
1001 #if CONFIG_ASF_MUXER
1002 AVOutputFormat ff_asf_muxer = {
1003  .name = "asf",
1004  .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1005  .mime_type = "video/x-ms-asf",
1006  .extensions = "asf,wmv,wma",
1007  .priv_data_size = sizeof(ASFContext),
1008  .audio_codec = AV_CODEC_ID_WMAV2,
1009  .video_codec = AV_CODEC_ID_MSMPEG4V3,
1014  .codec_tag = (const AVCodecTag * const []) {
1016  },
1017 };
1018 #endif /* CONFIG_ASF_MUXER */
1019 
1020 #if CONFIG_ASF_STREAM_MUXER
1021 AVOutputFormat ff_asf_stream_muxer = {
1022  .name = "asf_stream",
1023  .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1024  .mime_type = "video/x-ms-asf",
1025  .extensions = "asf,wmv,wma",
1026  .priv_data_size = sizeof(ASFContext),
1027  .audio_codec = AV_CODEC_ID_WMAV2,
1028  .video_codec = AV_CODEC_ID_MSMPEG4V3,
1033  .codec_tag = (const AVCodecTag * const []) {
1035  },
1036 };
1037 #endif /* CONFIG_ASF_STREAM_MUXER */