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