FFmpeg
 All Data Structures 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 #include "avformat.h"
22 #include "internal.h"
23 #include "riff.h"
24 #include "asf.h"
25 #include "avio_internal.h"
26 #include "libavutil/dict.h"
27 
28 #undef NDEBUG
29 #include <assert.h>
30 
31 
32 #define ASF_INDEXED_INTERVAL 10000000
33 #define ASF_INDEX_BLOCK (1<<9)
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 
41 #if (ASF_PACKET_ERROR_CORRECTION_FLAGS != 0)
42 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 1
43 #else
44 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 0
45 #endif
46 
47 #define ASF_PPI_PROPERTY_FLAGS (\
48  ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE | \
49  ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD | \
50  ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE | \
51  ASF_PL_FLAG_STREAM_NUMBER_LENGTH_FIELD_IS_BYTE \
52  )
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 
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 
159 
160 // Replicated Data shall be at least 8 bytes long.
161 #define ASF_PAYLOAD_REPLICATED_DATA_LENGTH 0x08
162 
163 #define PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD (\
164  1 + /*Stream Number*/ \
165  ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
166  ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
167  ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
168  ASF_PAYLOAD_REPLICATED_DATA_LENGTH \
169  )
170 
171 #define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS (\
172  1 + /*Stream Number*/ \
173  ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
174  ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
175  ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
176  ASF_PAYLOAD_REPLICATED_DATA_LENGTH + \
177  ASF_PAYLOAD_LENGTH_FIELD_SIZE \
178  )
179 
180 #define SINGLE_PAYLOAD_DATA_LENGTH (\
181  PACKET_SIZE - \
182  PACKET_HEADER_MIN_SIZE - \
183  PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD \
184  )
185 
186 #define MULTI_PAYLOAD_CONSTANT (\
187  PACKET_SIZE - \
188  PACKET_HEADER_MIN_SIZE - \
189  1 - /*Payload Flags*/ \
190  2*PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS \
191  )
192 
193 typedef struct {
194  uint32_t seqno;
196  ASFStream streams[128]; ///< it's max number and it's not that big
197  /* non streamed additonnal info */
198  uint64_t nb_packets; ///< how many packets are there in the file, invalid if broadcasting
199  int64_t duration; ///< in 100ns units
200  /* packet filling */
201  unsigned char multi_payloads_present;
202  int packet_size_left;
205  unsigned int packet_nb_payloads;
206  uint8_t packet_buf[PACKET_SIZE];
208  /* only for reading */
209  uint64_t data_offset; ///< beginning of the first data packet
210 
213  uint16_t maximum_packet;
217  int end_sec;
218 } ASFContext;
219 
220 static const AVCodecTag codec_asf_bmp_tags[] = {
221  { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
222  { AV_CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
223  { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
224  { AV_CODEC_ID_NONE, 0 },
225 };
226 
227 #define PREROLL_TIME 3100
228 
230 {
231  assert(sizeof(*g) == 16);
232  avio_write(s, *g, sizeof(*g));
233 }
234 
235 static void put_str16(AVIOContext *s, const char *tag)
236 {
237  int len;
238  uint8_t *pb;
239  AVIOContext *dyn_buf;
240  if (avio_open_dyn_buf(&dyn_buf) < 0)
241  return;
242 
243  avio_put_str16le(dyn_buf, tag);
244  len = avio_close_dyn_buf(dyn_buf, &pb);
245  avio_wl16(s, len);
246  avio_write(s, pb, len);
247  av_freep(&pb);
248 }
249 
250 static int64_t put_header(AVIOContext *pb, const ff_asf_guid *g)
251 {
252  int64_t pos;
253 
254  pos = avio_tell(pb);
255  ff_put_guid(pb, g);
256  avio_wl64(pb, 24);
257  return pos;
258 }
259 
260 /* update header size */
261 static void end_header(AVIOContext *pb, int64_t pos)
262 {
263  int64_t pos1;
264 
265  pos1 = avio_tell(pb);
266  avio_seek(pb, pos + 16, SEEK_SET);
267  avio_wl64(pb, pos1 - pos);
268  avio_seek(pb, pos1, SEEK_SET);
269 }
270 
271 /* write an asf chunk (only used in streaming case) */
272 static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
273 {
274  ASFContext *asf = s->priv_data;
275  AVIOContext *pb = s->pb;
276  int length;
277 
278  length = payload_length + 8;
279  avio_wl16(pb, type);
280  avio_wl16(pb, length); //size
281  avio_wl32(pb, asf->seqno);//sequence number
282  avio_wl16(pb, flags); /* unknown bytes */
283  avio_wl16(pb, length); //size_confirm
284  asf->seqno++;
285 }
286 
287 /* convert from unix to windows time */
288 static int64_t unix_to_file_time(int ti)
289 {
290  int64_t t;
291 
292  t = ti * INT64_C(10000000);
293  t += INT64_C(116444736000000000);
294  return t;
295 }
296 
297 /* write the header (used two times if non streamed) */
298 static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
299 {
300  ASFContext *asf = s->priv_data;
301  AVIOContext *pb = s->pb;
302  AVDictionaryEntry *tags[5];
303  int header_size, n, extra_size, extra_size2, wav_extra_size, file_time;
304  int has_title;
305  int metadata_count;
306  AVCodecContext *enc;
307  int64_t header_offset, cur_pos, hpos;
308  int bit_rate;
309  int64_t duration;
310 
312 
313  tags[0] = av_dict_get(s->metadata, "title" , NULL, 0);
314  tags[1] = av_dict_get(s->metadata, "author" , NULL, 0);
315  tags[2] = av_dict_get(s->metadata, "copyright", NULL, 0);
316  tags[3] = av_dict_get(s->metadata, "comment" , NULL, 0);
317  tags[4] = av_dict_get(s->metadata, "rating" , NULL, 0);
318 
319  duration = asf->duration + PREROLL_TIME * 10000;
320  has_title = tags[0] || tags[1] || tags[2] || tags[3] || tags[4];
321  metadata_count = av_dict_count(s->metadata);
322 
323  bit_rate = 0;
324  for(n=0;n<s->nb_streams;n++) {
325  enc = s->streams[n]->codec;
326 
327  avpriv_set_pts_info(s->streams[n], 32, 1, 1000); /* 32 bit pts in ms */
328 
329  bit_rate += enc->bit_rate;
330  }
331 
332  if (asf->is_streamed) {
333  put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
334  }
335 
337  avio_wl64(pb, -1); /* header length, will be patched after */
338  avio_wl32(pb, 3 + has_title + !!metadata_count + s->nb_streams); /* number of chunks in header */
339  avio_w8(pb, 1); /* ??? */
340  avio_w8(pb, 2); /* ??? */
341 
342  /* file header */
343  header_offset = avio_tell(pb);
344  hpos = put_header(pb, &ff_asf_file_header);
346  avio_wl64(pb, file_size);
347  file_time = 0;
348  avio_wl64(pb, unix_to_file_time(file_time));
349  avio_wl64(pb, asf->nb_packets); /* number of packets */
350  avio_wl64(pb, duration); /* end time stamp (in 100ns units) */
351  avio_wl64(pb, asf->duration); /* duration (in 100ns units) */
352  avio_wl64(pb, PREROLL_TIME); /* start time stamp */
353  avio_wl32(pb, (asf->is_streamed || !pb->seekable ) ? 3 : 2); /* ??? */
354  avio_wl32(pb, s->packet_size); /* packet size */
355  avio_wl32(pb, s->packet_size); /* packet size */
356  avio_wl32(pb, bit_rate); /* Nominal data rate in bps */
357  end_header(pb, hpos);
358 
359  /* unknown headers */
360  hpos = put_header(pb, &ff_asf_head1_guid);
362  avio_wl32(pb, 6);
363  avio_wl16(pb, 0);
364  end_header(pb, hpos);
365 
366  /* title and other infos */
367  if (has_title) {
368  int len;
369  uint8_t *buf;
370  AVIOContext *dyn_buf;
371 
372  if (avio_open_dyn_buf(&dyn_buf) < 0)
373  return AVERROR(ENOMEM);
374 
375  hpos = put_header(pb, &ff_asf_comment_header);
376 
377  for (n = 0; n < FF_ARRAY_ELEMS(tags); n++) {
378  len = tags[n] ? avio_put_str16le(dyn_buf, tags[n]->value) : 0;
379  avio_wl16(pb, len);
380  }
381  len = avio_close_dyn_buf(dyn_buf, &buf);
382  avio_write(pb, buf, len);
383  av_freep(&buf);
384  end_header(pb, hpos);
385  }
386  if (metadata_count) {
389  avio_wl16(pb, metadata_count);
390  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
391  put_str16(pb, tag->key);
392  avio_wl16(pb, 0);
393  put_str16(pb, tag->value);
394  }
395  end_header(pb, hpos);
396  }
397 
398  /* stream headers */
399  for(n=0;n<s->nb_streams;n++) {
400  int64_t es_pos;
401  // ASFStream *stream = &asf->streams[n];
402 
403  enc = s->streams[n]->codec;
404  asf->streams[n].num = n + 1;
405  asf->streams[n].seq = 1;
406 
407 
408  switch(enc->codec_type) {
409  case AVMEDIA_TYPE_AUDIO:
410  wav_extra_size = 0;
411  extra_size = 18 + wav_extra_size;
412  extra_size2 = 8;
413  break;
414  default:
415  case AVMEDIA_TYPE_VIDEO:
416  wav_extra_size = enc->extradata_size;
417  extra_size = 0x33 + wav_extra_size;
418  extra_size2 = 0;
419  break;
420  }
421 
422  hpos = put_header(pb, &ff_asf_stream_header);
423  if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
426  } else {
429  }
430  avio_wl64(pb, 0); /* ??? */
431  es_pos = avio_tell(pb);
432  avio_wl32(pb, extra_size); /* wav header len */
433  avio_wl32(pb, extra_size2); /* additional data len */
434  avio_wl16(pb, n + 1); /* stream number */
435  avio_wl32(pb, 0); /* ??? */
436 
437  if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
438  /* WAVEFORMATEX header */
439  int wavsize = ff_put_wav_header(pb, enc);
440 
441  if (wavsize < 0)
442  return -1;
443  if (wavsize != extra_size) {
444  cur_pos = avio_tell(pb);
445  avio_seek(pb, es_pos, SEEK_SET);
446  avio_wl32(pb, wavsize); /* wav header len */
447  avio_seek(pb, cur_pos, SEEK_SET);
448  }
449  /* ERROR Correction */
450  avio_w8(pb, 0x01);
451  if(enc->codec_id == AV_CODEC_ID_ADPCM_G726 || !enc->block_align){
452  avio_wl16(pb, 0x0190);
453  avio_wl16(pb, 0x0190);
454  }else{
455  avio_wl16(pb, enc->block_align);
456  avio_wl16(pb, enc->block_align);
457  }
458  avio_wl16(pb, 0x01);
459  avio_w8(pb, 0x00);
460  } else {
461  avio_wl32(pb, enc->width);
462  avio_wl32(pb, enc->height);
463  avio_w8(pb, 2); /* ??? */
464  avio_wl16(pb, 40 + enc->extradata_size); /* size */
465 
466  /* BITMAPINFOHEADER header */
468  }
469  end_header(pb, hpos);
470  }
471 
472  /* media comments */
473 
476  avio_wl32(pb, s->nb_streams);
477  for(n=0;n<s->nb_streams;n++) {
478  AVCodec *p;
479  const char *desc;
480  int len;
481  uint8_t *buf;
482  AVIOContext *dyn_buf;
483 
484  enc = s->streams[n]->codec;
485  p = avcodec_find_encoder(enc->codec_id);
486 
487  if(enc->codec_type == AVMEDIA_TYPE_AUDIO)
488  avio_wl16(pb, 2);
489  else if(enc->codec_type == AVMEDIA_TYPE_VIDEO)
490  avio_wl16(pb, 1);
491  else
492  avio_wl16(pb, -1);
493 
494  if(enc->codec_id == AV_CODEC_ID_WMAV2)
495  desc = "Windows Media Audio V8";
496  else
497  desc = p ? p->name : enc->codec_name;
498 
499  if ( avio_open_dyn_buf(&dyn_buf) < 0)
500  return AVERROR(ENOMEM);
501 
502  avio_put_str16le(dyn_buf, desc);
503  len = avio_close_dyn_buf(dyn_buf, &buf);
504  avio_wl16(pb, len / 2); // "number of characters" = length in bytes / 2
505 
506  avio_write(pb, buf, len);
507  av_freep(&buf);
508 
509  avio_wl16(pb, 0); /* no parameters */
510 
511 
512  /* id */
513  if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
514  avio_wl16(pb, 2);
515  avio_wl16(pb, enc->codec_tag);
516  } else {
517  avio_wl16(pb, 4);
518  avio_wl32(pb, enc->codec_tag);
519  }
520  if(!enc->codec_tag)
521  return -1;
522  }
523  end_header(pb, hpos);
524 
525  /* patch the header size fields */
526 
527  cur_pos = avio_tell(pb);
528  header_size = cur_pos - header_offset;
529  if (asf->is_streamed) {
530  header_size += 8 + 30 + 50;
531 
532  avio_seek(pb, header_offset - 10 - 30, SEEK_SET);
533  avio_wl16(pb, header_size);
534  avio_seek(pb, header_offset - 2 - 30, SEEK_SET);
535  avio_wl16(pb, header_size);
536 
537  header_size -= 8 + 30 + 50;
538  }
539  header_size += 24 + 6;
540  avio_seek(pb, header_offset - 14, SEEK_SET);
541  avio_wl64(pb, header_size);
542  avio_seek(pb, cur_pos, SEEK_SET);
543 
544  /* movie chunk, followed by packets of packet_size */
545  asf->data_offset = cur_pos;
547  avio_wl64(pb, data_chunk_size);
549  avio_wl64(pb, asf->nb_packets); /* nb packets */
550  avio_w8(pb, 1); /* ??? */
551  avio_w8(pb, 1); /* ??? */
552  return 0;
553 }
554 
556 {
557  ASFContext *asf = s->priv_data;
558 
560  asf->nb_packets = 0;
561 
562  asf->index_ptr = av_malloc( sizeof(ASFIndex) * ASF_INDEX_BLOCK );
564  asf->maximum_packet = 0;
565 
566  /* the data-chunk-size has to be 50, which is data_size - asf->data_offset
567  * at the moment this function is done. It is needed to use asf as
568  * streamable format. */
569  if (asf_write_header1(s, 0, 50) < 0) {
570  //av_free(asf);
571  return -1;
572  }
573 
574  avio_flush(s->pb);
575 
576  asf->packet_nb_payloads = 0;
577  asf->packet_timestamp_start = -1;
578  asf->packet_timestamp_end = -1;
579  ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
580  NULL, NULL, NULL, NULL);
581 
582  if (s->avoid_negative_ts < 0)
583  s->avoid_negative_ts = 1;
584 
585  return 0;
586 }
587 
589 {
590  ASFContext *asf = s->priv_data;
591 
592  asf->is_streamed = 1;
593 
594  return asf_write_header(s);
595 }
596 
598  AVFormatContext *s,
599  unsigned int sendtime,
600  unsigned int duration,
601  int nb_payloads,
602  int padsize
603  )
604 {
605  ASFContext *asf = s->priv_data;
606  AVIOContext *pb = s->pb;
607  int ppi_size, i;
608  int64_t start= avio_tell(pb);
609 
610  int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
611 
612  padsize -= PACKET_HEADER_MIN_SIZE;
613  if(asf->multi_payloads_present)
614  padsize--;
615  assert(padsize>=0);
616 
618  for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++){
619  avio_w8(pb, 0x0);
620  }
621 
622  if (asf->multi_payloads_present)
623  iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
624 
625  if (padsize > 0) {
626  if (padsize < 256)
627  iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
628  else
629  iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
630  }
631  avio_w8(pb, iLengthTypeFlags);
632 
634 
635  if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
636  avio_wl16(pb, padsize - 2);
637  if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
638  avio_w8(pb, padsize - 1);
639 
640  avio_wl32(pb, sendtime);
641  avio_wl16(pb, duration);
642  if (asf->multi_payloads_present)
643  avio_w8(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
644 
645  ppi_size = avio_tell(pb) - start;
646 
647  return ppi_size;
648 }
649 
651 {
652  ASFContext *asf = s->priv_data;
653  int packet_hdr_size, packet_filled_size;
654 
655  assert(asf->packet_timestamp_end >= asf->packet_timestamp_start);
656 
657  if (asf->is_streamed) {
658  put_chunk(s, 0x4424, s->packet_size, 0);
659  }
660 
661  packet_hdr_size = put_payload_parsing_info(
662  s,
665  asf->packet_nb_payloads,
666  asf->packet_size_left
667  );
668 
669  packet_filled_size = PACKET_SIZE - asf->packet_size_left;
670  assert(packet_hdr_size <= asf->packet_size_left);
671  memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
672 
673  avio_write(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
674 
675  avio_flush(s->pb);
676  asf->nb_packets++;
677  asf->packet_nb_payloads = 0;
678  asf->packet_timestamp_start = -1;
679  asf->packet_timestamp_end = -1;
680  ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
681  NULL, NULL, NULL, NULL);
682 }
683 
684 static void put_payload_header(
685  AVFormatContext *s,
686  ASFStream *stream,
687  int64_t presentation_time,
688  int m_obj_size,
689  int m_obj_offset,
690  int payload_len,
691  int flags
692  )
693 {
694  ASFContext *asf = s->priv_data;
695  AVIOContext *pb = &asf->pb;
696  int val;
697 
698  val = stream->num;
699  if (flags & AV_PKT_FLAG_KEY)
700  val |= ASF_PL_FLAG_KEY_FRAME;
701  avio_w8(pb, val);
702 
703  avio_w8(pb, stream->seq); //Media object number
704  avio_wl32(pb, m_obj_offset); //Offset Into Media Object
705 
706  // Replicated Data shall be at least 8 bytes long.
707  // The first 4 bytes of data shall contain the
708  // Size of the Media Object that the payload belongs to.
709  // The next 4 bytes of data shall contain the
710  // Presentation Time for the media object that the payload belongs to.
712 
713  avio_wl32(pb, m_obj_size); //Replicated Data - Media Object Size
714  avio_wl32(pb, (uint32_t) presentation_time);//Replicated Data - Presentation Time
715 
716  if (asf->multi_payloads_present){
717  avio_wl16(pb, payload_len); //payload length
718  }
719 }
720 
721 static void put_frame(
722  AVFormatContext *s,
723  ASFStream *stream,
724  AVStream *avst,
725  int64_t timestamp,
726  const uint8_t *buf,
727  int m_obj_size,
728  int flags
729  )
730 {
731  ASFContext *asf = s->priv_data;
732  int m_obj_offset, payload_len, frag_len1;
733 
734  m_obj_offset = 0;
735  while (m_obj_offset < m_obj_size) {
736  payload_len = m_obj_size - m_obj_offset;
737  if (asf->packet_timestamp_start == -1) {
738  asf->multi_payloads_present = (payload_len < MULTI_PAYLOAD_CONSTANT);
739 
741  if (asf->multi_payloads_present){
742  frag_len1 = MULTI_PAYLOAD_CONSTANT - 1;
743  }
744  else {
745  frag_len1 = SINGLE_PAYLOAD_DATA_LENGTH;
746  }
747  asf->packet_timestamp_start = timestamp;
748  }
749  else {
750  // multi payloads
752 
753  if(frag_len1 < payload_len && avst->codec->codec_type == AVMEDIA_TYPE_AUDIO){
754  flush_packet(s);
755  continue;
756  }
757  }
758  if (frag_len1 > 0) {
759  if (payload_len > frag_len1)
760  payload_len = frag_len1;
761  else if (payload_len == (frag_len1 - 1))
762  payload_len = frag_len1 - 2; //additional byte need to put padding length
763 
764  put_payload_header(s, stream, timestamp+PREROLL_TIME, m_obj_size, m_obj_offset, payload_len, flags);
765  avio_write(&asf->pb, buf, payload_len);
766 
767  if (asf->multi_payloads_present)
769  else
771  asf->packet_timestamp_end = timestamp;
772 
773  asf->packet_nb_payloads++;
774  } else {
775  payload_len = 0;
776  }
777  m_obj_offset += payload_len;
778  buf += payload_len;
779 
780  if (!asf->multi_payloads_present)
781  flush_packet(s);
783  flush_packet(s);
784  }
785  stream->seq++;
786 }
787 
788 static void update_index(AVFormatContext *s, int start_sec,
789  uint32_t packet_number, uint16_t packet_count)
790 {
791  ASFContext *asf = s->priv_data;
792 
793  if (start_sec > asf->next_start_sec) {
794  int i;
795 
796  if (!asf->next_start_sec) {
797  asf->next_packet_number = packet_number;
798  asf->next_packet_count = packet_count;
799  }
800 
801  if (start_sec > asf->nb_index_memory_alloc) {
802  asf->nb_index_memory_alloc = (start_sec + ASF_INDEX_BLOCK) & ~(ASF_INDEX_BLOCK - 1);
803  asf->index_ptr = av_realloc( asf->index_ptr, sizeof(ASFIndex) * asf->nb_index_memory_alloc );
804  }
805  for (i = asf->next_start_sec; i < start_sec; i++) {
808  }
809  }
810  asf->maximum_packet = FFMAX(asf->maximum_packet, packet_count);
811  asf->next_packet_number = packet_number;
812  asf->next_packet_count = packet_count;
813  asf->next_start_sec = start_sec;
814 }
815 
817 {
818  ASFContext *asf = s->priv_data;
819  ASFStream *stream;
820  AVCodecContext *codec;
821  uint32_t packet_number;
822  int64_t pts;
823  int start_sec;
824  int flags= pkt->flags;
825 
826  codec = s->streams[pkt->stream_index]->codec;
827  stream = &asf->streams[pkt->stream_index];
828 
829  if(codec->codec_type == AVMEDIA_TYPE_AUDIO)
830  flags &= ~AV_PKT_FLAG_KEY;
831 
832  pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
833  assert(pts != AV_NOPTS_VALUE);
834  pts *= 10000;
835  asf->duration= FFMAX(asf->duration, pts + pkt->duration * 10000);
836 
837  packet_number = asf->nb_packets;
838  put_frame(s, stream, s->streams[pkt->stream_index], pkt->dts, pkt->data, pkt->size, flags);
839 
840  start_sec = (int)((PREROLL_TIME * 10000 + pts + ASF_INDEXED_INTERVAL - 1)
842 
843  /* check index */
844  if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
845  uint16_t packet_count = asf->nb_packets - packet_number;
846  update_index(s, start_sec, packet_number, packet_count);
847  }
848  asf->end_sec = start_sec;
849 
850  return 0;
851 }
852 
853 //
854 static int asf_write_index(AVFormatContext *s, ASFIndex *index, uint16_t max, uint32_t count)
855 {
856  AVIOContext *pb = s->pb;
857  int i;
858 
860  avio_wl64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2)*count);
863  avio_wl32(pb, max);
864  avio_wl32(pb, count);
865  for(i=0; i<count; i++) {
866  avio_wl32(pb, index[i].packet_number);
867  avio_wl16(pb, index[i].packet_count);
868  }
869 
870  return 0;
871 }
872 
874 {
875  ASFContext *asf = s->priv_data;
876  int64_t file_size,data_size;
877 
878  /* flush the current packet */
879  if (asf->pb.buf_ptr > asf->pb.buffer)
880  flush_packet(s);
881 
882  /* write index */
883  data_size = avio_tell(s->pb);
884  if (!asf->is_streamed && asf->next_start_sec) {
885  update_index(s, asf->end_sec + 1, 0, 0);
887  }
888  avio_flush(s->pb);
889 
890  if (asf->is_streamed || !s->pb->seekable) {
891  put_chunk(s, 0x4524, 0, 0); /* end of stream */
892  } else {
893  /* rewrite an updated header */
894  file_size = avio_tell(s->pb);
895  avio_seek(s->pb, 0, SEEK_SET);
896  asf_write_header1(s, file_size, data_size - asf->data_offset);
897  }
898 
899  av_free(asf->index_ptr);
900  return 0;
901 }
902 
903 #if CONFIG_ASF_MUXER
904 AVOutputFormat ff_asf_muxer = {
905  .name = "asf",
906  .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
907  .mime_type = "video/x-ms-asf",
908  .extensions = "asf,wmv,wma",
909  .priv_data_size = sizeof(ASFContext),
910  .audio_codec = AV_CODEC_ID_WMAV2,
911  .video_codec = AV_CODEC_ID_MSMPEG4V3,
916  .codec_tag = (const AVCodecTag* const []){
918  },
919 };
920 #endif
921 
922 #if CONFIG_ASF_STREAM_MUXER
923 AVOutputFormat ff_asf_stream_muxer = {
924  .name = "asf_stream",
925  .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
926  .mime_type = "video/x-ms-asf",
927  .extensions = "asf,wmv,wma",
928  .priv_data_size = sizeof(ASFContext),
929  .audio_codec = AV_CODEC_ID_WMAV2,
930  .video_codec = AV_CODEC_ID_MSMPEG4V3,
935  .codec_tag = (const AVCodecTag* const []){
937  },
938 };
939 #endif //CONFIG_ASF_STREAM_MUXER