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