FFmpeg
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 "libavutil/opt.h"
26 #include "avformat.h"
27 #include "avlanguage.h"
28 #include "avio_internal.h"
29 #include "internal.h"
30 #include "riff.h"
31 #include "asf.h"
32 
33 #define ASF_INDEXED_INTERVAL 10000000
34 #define ASF_INDEX_BLOCK (1<<9)
35 #define ASF_PAYLOADS_PER_PACKET 63
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_HEADERS \
176  (PACKET_HEADER_MIN_SIZE + \
177  PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD)
178 
179 #define MULTI_PAYLOAD_HEADERS \
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 #define PACKET_SIZE_MAX 65536
187 #define PACKET_SIZE_MIN 100
188 
189 typedef struct ASFPayload {
190  uint8_t type;
191  uint16_t size;
192 } ASFPayload;
193 
194 typedef struct ASFStream {
195  int num;
196  unsigned char seq;
197  /* use for reading */
198  AVPacket pkt;
199  int frag_offset;
200  int packet_obj_size;
201  int timestamp;
202  int64_t duration;
203  int skip_to_key;
204  int pkt_clean;
205 
206  int ds_span; /* descrambling */
207  int ds_packet_size;
208  int ds_chunk_size;
209 
210  int64_t packet_pos;
211 
212  uint16_t stream_language_index;
213 
214  int palette_changed;
215  uint32_t palette[256];
216 
217  int payload_ext_ct;
218  ASFPayload payload[8];
219 } ASFStream;
220 
221 typedef struct ASFContext {
223  uint32_t seqno;
225  ASFStream streams[128]; ///< it's max number and it's not that big
226  const char *languages[128];
228  int64_t creation_time;
229  /* non-streamed additional info */
230  uint64_t nb_packets; ///< how many packets are there in the file, invalid if broadcasting
231  int64_t duration; ///< in 100ns units
232  /* packet filling */
233  unsigned char multi_payloads_present;
234  int packet_size_left;
237  unsigned int packet_nb_payloads;
240  /* only for reading */
241  uint64_t data_offset; ///< beginning of the first data packet
242 
245  uint16_t maximum_packet;
250  int end_sec;
252 } ASFContext;
253 
254 static const AVCodecTag codec_asf_bmp_tags[] = {
255  { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
256  { AV_CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
257  { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
258  { AV_CODEC_ID_NONE, 0 },
259 };
260 
261 #define PREROLL_TIME 3100
262 
263 static void put_str16(AVIOContext *s, const char *tag)
264 {
265  int len;
266  uint8_t *pb;
267  AVIOContext *dyn_buf;
268  if (avio_open_dyn_buf(&dyn_buf) < 0)
269  return;
270 
271  avio_put_str16le(dyn_buf, tag);
272  len = avio_close_dyn_buf(dyn_buf, &pb);
273  avio_wl16(s, len);
274  avio_write(s, pb, len);
275  av_freep(&pb);
276 }
277 
278 static int64_t put_header(AVIOContext *pb, const ff_asf_guid *g)
279 {
280  int64_t pos;
281 
282  pos = avio_tell(pb);
283  ff_put_guid(pb, g);
284  avio_wl64(pb, 24);
285  return pos;
286 }
287 
288 /* update header size */
289 static void end_header(AVIOContext *pb, int64_t pos)
290 {
291  int64_t pos1;
292 
293  pos1 = avio_tell(pb);
294  avio_seek(pb, pos + 16, SEEK_SET);
295  avio_wl64(pb, pos1 - pos);
296  avio_seek(pb, pos1, SEEK_SET);
297 }
298 
299 /* write an asf chunk (only used in streaming case) */
300 static void put_chunk(AVFormatContext *s, int type,
301  int payload_length, int flags)
302 {
303  ASFContext *asf = s->priv_data;
304  AVIOContext *pb = s->pb;
305  int length;
306 
307  length = payload_length + 8;
308  avio_wl16(pb, type);
309  avio_wl16(pb, length); // size
310  avio_wl32(pb, asf->seqno); // sequence number
311  avio_wl16(pb, flags); // unknown bytes
312  avio_wl16(pb, length); // size_confirm
313  asf->seqno++;
314 }
315 
316 /* convert from av time to windows time */
317 static int64_t unix_to_file_time(int64_t ti)
318 {
319  int64_t t;
320 
321  t = ti * INT64_C(10);
322  t += INT64_C(116444736000000000);
323  return t;
324 }
325 
326 static int32_t get_send_time(ASFContext *asf, int64_t pres_time, uint64_t *offset)
327 {
328  int i;
329  int32_t send_time = 0;
330  *offset = asf->data_offset + DATA_HEADER_SIZE;
331  for (i = 0; i < asf->next_start_sec; i++) {
332  if (pres_time <= asf->index_ptr[i].send_time)
333  break;
334  send_time = asf->index_ptr[i].send_time;
335  *offset = asf->index_ptr[i].offset;
336  }
337 
338  return send_time / 10000;
339 }
340 
342 {
343  ASFContext *asf = s->priv_data;
344  AVIOContext *pb = s->pb;
345  int i;
346  AVRational scale = {1, 10000000};
347  int64_t hpos = put_header(pb, &ff_asf_marker_header);
348 
349  ff_put_guid(pb, &ff_asf_reserved_4);// ASF spec mandates this reserved value
350  avio_wl32(pb, s->nb_chapters); // markers count
351  avio_wl16(pb, 0); // ASF spec mandates 0 for this
352  avio_wl16(pb, 0); // name length 0, no name given
353 
354  for (i = 0; i < s->nb_chapters; i++) {
355  AVChapter *c = s->chapters[i];
356  AVDictionaryEntry *t = av_dict_get(c->metadata, "title", NULL, 0);
357  int64_t pres_time = av_rescale_q(c->start, c->time_base, scale);
358  uint64_t offset;
359  int32_t send_time = get_send_time(asf, pres_time, &offset);
360  int len = 0;
361  uint8_t *buf;
362  AVIOContext *dyn_buf;
363  if (t) {
364  if (avio_open_dyn_buf(&dyn_buf) < 0)
365  return AVERROR(ENOMEM);
366  avio_put_str16le(dyn_buf, t->value);
367  len = avio_close_dyn_buf(dyn_buf, &buf);
368  }
369  avio_wl64(pb, offset); // offset of the packet with send_time
370  avio_wl64(pb, pres_time + PREROLL_TIME * 10000); // presentation time
371  avio_wl16(pb, 12 + len); // entry length
372  avio_wl32(pb, send_time); // send time
373  avio_wl32(pb, 0); // flags, should be 0
374  avio_wl32(pb, len / 2); // marker desc length in WCHARS!
375  if (t) {
376  avio_write(pb, buf, len); // marker desc
377  av_freep(&buf);
378  }
379  }
380  end_header(pb, hpos);
381  return 0;
382 }
383 
384 /* write the header (used two times if non streamed) */
385 static int asf_write_header1(AVFormatContext *s, int64_t file_size,
386  int64_t data_chunk_size)
387 {
388  ASFContext *asf = s->priv_data;
389  AVIOContext *pb = s->pb;
390  AVDictionaryEntry *tags[5];
391  int header_size, n, extra_size, extra_size2, wav_extra_size;
392  int has_title, has_aspect_ratio = 0;
393  int metadata_count;
394  AVCodecParameters *par;
395  int64_t header_offset, cur_pos, hpos;
396  int bit_rate;
397  int64_t duration;
398  int audio_language_counts[128] = { 0 };
399 
401 
402  tags[0] = av_dict_get(s->metadata, "title", NULL, 0);
403  tags[1] = av_dict_get(s->metadata, "author", NULL, 0);
404  tags[2] = av_dict_get(s->metadata, "copyright", NULL, 0);
405  tags[3] = av_dict_get(s->metadata, "comment", NULL, 0);
406  tags[4] = av_dict_get(s->metadata, "rating", NULL, 0);
407 
408  duration = asf->duration + PREROLL_TIME * 10000;
409  has_title = tags[0] || tags[1] || tags[2] || tags[3] || tags[4];
410 
411  if (!file_size) {
412  if (ff_parse_creation_time_metadata(s, &asf->creation_time, 0) != 0)
413  av_dict_set(&s->metadata, "creation_time", NULL, 0);
414  }
415 
416  metadata_count = av_dict_count(s->metadata);
417 
418  bit_rate = 0;
419  for (n = 0; n < s->nb_streams; n++) {
420  AVDictionaryEntry *entry;
421  par = s->streams[n]->codecpar;
422 
423  avpriv_set_pts_info(s->streams[n], 32, 1, 1000); /* 32 bit pts in ms */
424 
425  bit_rate += par->bit_rate;
426  if ( par->codec_type == AVMEDIA_TYPE_VIDEO
427  && par->sample_aspect_ratio.num > 0
428  && par->sample_aspect_ratio.den > 0)
429  has_aspect_ratio++;
430 
431  entry = av_dict_get(s->streams[n]->metadata, "language", NULL, 0);
432  if (entry) {
433  const char *iso6391lang = ff_convert_lang_to(entry->value, AV_LANG_ISO639_1);
434  if (iso6391lang) {
435  int i;
436  for (i = 0; i < asf->nb_languages; i++) {
437  if (!strcmp(asf->languages[i], iso6391lang)) {
439  break;
440  }
441  }
442  if (i >= asf->nb_languages) {
443  asf->languages[asf->nb_languages] = iso6391lang;
445  asf->nb_languages++;
446  }
447  if (par->codec_type == AVMEDIA_TYPE_AUDIO)
448  audio_language_counts[asf->streams[n].stream_language_index]++;
449  }
450  } else {
451  asf->streams[n].stream_language_index = 128;
452  }
453  }
454 
455  if (asf->is_streamed) {
456  put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
457  }
458 
460  avio_wl64(pb, -1); /* header length, will be patched after */
461  avio_wl32(pb, 3 + has_title + !!metadata_count + s->nb_streams); /* number of chunks in header */
462  avio_w8(pb, 1); /* ??? */
463  avio_w8(pb, 2); /* ??? */
464 
465  /* file header */
466  header_offset = avio_tell(pb);
467  hpos = put_header(pb, &ff_asf_file_header);
469  avio_wl64(pb, file_size);
471  avio_wl64(pb, asf->nb_packets); /* number of packets */
472  avio_wl64(pb, duration); /* end time stamp (in 100ns units) */
473  avio_wl64(pb, asf->duration); /* duration (in 100ns units) */
474  avio_wl64(pb, PREROLL_TIME); /* start time stamp */
475  avio_wl32(pb, (asf->is_streamed || !(pb->seekable & AVIO_SEEKABLE_NORMAL)) ? 3 : 2); /* ??? */
476  avio_wl32(pb, s->packet_size); /* packet size */
477  avio_wl32(pb, s->packet_size); /* packet size */
478  avio_wl32(pb, bit_rate ? bit_rate : -1); /* Maximum data rate in bps */
479  end_header(pb, hpos);
480 
481  /* header_extension */
482  hpos = put_header(pb, &ff_asf_head1_guid);
484  avio_wl16(pb, 6);
485  avio_wl32(pb, 0); /* length, to be filled later */
486  if (asf->nb_languages) {
487  int64_t hpos2;
488  int i;
489  int nb_audio_languages = 0;
490 
491  hpos2 = put_header(pb, &ff_asf_language_guid);
492  avio_wl16(pb, asf->nb_languages);
493  for (i = 0; i < asf->nb_languages; i++) {
494  avio_w8(pb, 6);
495  avio_put_str16le(pb, asf->languages[i]);
496  }
497  end_header(pb, hpos2);
498 
499  for (i = 0; i < asf->nb_languages; i++)
500  if (audio_language_counts[i])
501  nb_audio_languages++;
502 
503  if (nb_audio_languages > 1) {
506  avio_wl16(pb, nb_audio_languages);
507  for (i = 0; i < asf->nb_languages; i++) {
508  if (audio_language_counts[i]) {
509  avio_wl16(pb, audio_language_counts[i]);
510  for (n = 0; n < s->nb_streams; n++)
512  avio_wl16(pb, n + 1);
513  }
514  }
515  end_header(pb, hpos2);
516  }
517 
518  for (n = 0; n < s->nb_streams; n++) {
519  int64_t es_pos;
520  if (asf->streams[n].stream_language_index > 127)
521  continue;
523  avio_wl64(pb, 0); /* start time */
524  avio_wl64(pb, 0); /* end time */
525  avio_wl32(pb, s->streams[n]->codecpar->bit_rate); /* data bitrate bps */
526  avio_wl32(pb, 5000); /* buffer size ms */
527  avio_wl32(pb, 0); /* initial buffer fullness */
528  avio_wl32(pb, s->streams[n]->codecpar->bit_rate); /* peak data bitrate */
529  avio_wl32(pb, 5000); /* maximum buffer size ms */
530  avio_wl32(pb, 0); /* max initial buffer fullness */
531  avio_wl32(pb, 0); /* max object size */
532  avio_wl32(pb, (!asf->is_streamed && (pb->seekable & AVIO_SEEKABLE_NORMAL)) << 1); /* flags - seekable */
533  avio_wl16(pb, n + 1); /* stream number */
534  avio_wl16(pb, asf->streams[n].stream_language_index); /* language id index */
535  avio_wl64(pb, 0); /* avg time per frame */
536  avio_wl16(pb, 0); /* stream name count */
537  avio_wl16(pb, 0); /* payload extension system count */
538  end_header(pb, es_pos);
539  }
540  }
541  if (has_aspect_ratio) {
542  int64_t hpos2;
543  hpos2 = put_header(pb, &ff_asf_metadata_header);
544  avio_wl16(pb, 2 * has_aspect_ratio);
545  for (n = 0; n < s->nb_streams; n++) {
546  par = s->streams[n]->codecpar;
547  if ( par->codec_type == AVMEDIA_TYPE_VIDEO
548  && par->sample_aspect_ratio.num > 0
549  && par->sample_aspect_ratio.den > 0) {
550  AVRational sar = par->sample_aspect_ratio;
551  avio_wl16(pb, 0);
552  // the stream number is set like this below
553  avio_wl16(pb, n + 1);
554  avio_wl16(pb, 26); // name_len
555  avio_wl16(pb, 3); // value_type
556  avio_wl32(pb, 4); // value_len
557  avio_put_str16le(pb, "AspectRatioX");
558  avio_wl32(pb, sar.num);
559  avio_wl16(pb, 0);
560  // the stream number is set like this below
561  avio_wl16(pb, n + 1);
562  avio_wl16(pb, 26); // name_len
563  avio_wl16(pb, 3); // value_type
564  avio_wl32(pb, 4); // value_len
565  avio_put_str16le(pb, "AspectRatioY");
566  avio_wl32(pb, sar.den);
567  }
568  }
569  end_header(pb, hpos2);
570  }
571  {
572  int64_t pos1;
573  pos1 = avio_tell(pb);
574  avio_seek(pb, hpos + 42, SEEK_SET);
575  avio_wl32(pb, pos1 - hpos - 46);
576  avio_seek(pb, pos1, SEEK_SET);
577  }
578  end_header(pb, hpos);
579 
580  /* title and other info */
581  if (has_title) {
582  int len;
583  uint8_t *buf;
584  AVIOContext *dyn_buf;
585 
586  if (avio_open_dyn_buf(&dyn_buf) < 0)
587  return AVERROR(ENOMEM);
588 
589  hpos = put_header(pb, &ff_asf_comment_header);
590 
591  for (n = 0; n < FF_ARRAY_ELEMS(tags); n++) {
592  len = tags[n] ? avio_put_str16le(dyn_buf, tags[n]->value) : 0;
593  avio_wl16(pb, len);
594  }
595  len = avio_close_dyn_buf(dyn_buf, &buf);
596  avio_write(pb, buf, len);
597  av_freep(&buf);
598  end_header(pb, hpos);
599  }
600  if (metadata_count) {
603  avio_wl16(pb, metadata_count);
604  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
605  put_str16(pb, tag->key);
606  avio_wl16(pb, 0);
607  put_str16(pb, tag->value);
608  }
609  end_header(pb, hpos);
610  }
611  /* chapters using ASF markers */
612  if (!asf->is_streamed && s->nb_chapters) {
613  int ret;
614  if ((ret = asf_write_markers(s)) < 0)
615  return ret;
616  }
617  /* stream headers */
618  for (n = 0; n < s->nb_streams; n++) {
619  int64_t es_pos;
620  // ASFStream *stream = &asf->streams[n];
621 
622  par = s->streams[n]->codecpar;
623  asf->streams[n].num = n + 1;
624  asf->streams[n].seq = 1;
625 
626  switch (par->codec_type) {
627  case AVMEDIA_TYPE_AUDIO:
628  wav_extra_size = 0;
629  extra_size = 18 + wav_extra_size;
630  extra_size2 = 8;
631  break;
632  default:
633  case AVMEDIA_TYPE_VIDEO:
634  wav_extra_size = par->extradata_size;
635  extra_size = 0x33 + wav_extra_size;
636  extra_size2 = 0;
637  break;
638  }
639 
640  hpos = put_header(pb, &ff_asf_stream_header);
641  if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
644  } else {
647  }
648  avio_wl64(pb, 0); /* ??? */
649  es_pos = avio_tell(pb);
650  avio_wl32(pb, extra_size); /* wav header len */
651  avio_wl32(pb, extra_size2); /* additional data len */
652  avio_wl16(pb, n + 1); /* stream number */
653  avio_wl32(pb, 0); /* ??? */
654 
655  if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
656  /* WAVEFORMATEX header */
657  int wavsize = ff_put_wav_header(s, pb, par, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX);
658 
659  if (wavsize < 0)
660  return -1;
661  if (wavsize != extra_size) {
662  cur_pos = avio_tell(pb);
663  avio_seek(pb, es_pos, SEEK_SET);
664  avio_wl32(pb, wavsize); /* wav header len */
665  avio_seek(pb, cur_pos, SEEK_SET);
666  }
667  /* ERROR Correction */
668  avio_w8(pb, 0x01);
669  if (par->codec_id == AV_CODEC_ID_ADPCM_G726 || !par->block_align) {
670  avio_wl16(pb, 0x0190);
671  avio_wl16(pb, 0x0190);
672  } else {
673  avio_wl16(pb, par->block_align);
674  avio_wl16(pb, par->block_align);
675  }
676  avio_wl16(pb, 0x01);
677  avio_w8(pb, 0x00);
678  } else {
679  avio_wl32(pb, par->width);
680  avio_wl32(pb, par->height);
681  avio_w8(pb, 2); /* ??? */
682  avio_wl16(pb, 40 + par->extradata_size); /* size */
683 
684  /* BITMAPINFOHEADER header */
685  ff_put_bmp_header(pb, par, 1, 0);
686  }
687  end_header(pb, hpos);
688  }
689 
690  /* media comments */
691 
694  avio_wl32(pb, s->nb_streams);
695  for (n = 0; n < s->nb_streams; n++) {
696  const AVCodecDescriptor *codec_desc;
697  const char *desc;
698 
699  par = s->streams[n]->codecpar;
700  codec_desc = avcodec_descriptor_get(par->codec_id);
701 
702  if (par->codec_type == AVMEDIA_TYPE_AUDIO)
703  avio_wl16(pb, 2);
704  else if (par->codec_type == AVMEDIA_TYPE_VIDEO)
705  avio_wl16(pb, 1);
706  else
707  avio_wl16(pb, -1);
708 
709  if (par->codec_id == AV_CODEC_ID_WMAV2)
710  desc = "Windows Media Audio V8";
711  else
712  desc = codec_desc ? codec_desc->name : NULL;
713 
714  if (desc) {
715  AVIOContext *dyn_buf;
716  uint8_t *buf;
717  int len;
718 
719  if (avio_open_dyn_buf(&dyn_buf) < 0)
720  return AVERROR(ENOMEM);
721 
722  avio_put_str16le(dyn_buf, desc);
723  len = avio_close_dyn_buf(dyn_buf, &buf);
724  avio_wl16(pb, len / 2); // "number of characters" = length in bytes / 2
725 
726  avio_write(pb, buf, len);
727  av_freep(&buf);
728  } else
729  avio_wl16(pb, 0);
730 
731  avio_wl16(pb, 0); /* no parameters */
732 
733  /* id */
734  if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
735  avio_wl16(pb, 2);
736  avio_wl16(pb, par->codec_tag);
737  } else {
738  avio_wl16(pb, 4);
739  avio_wl32(pb, par->codec_tag);
740  }
741  if (!par->codec_tag)
742  return -1;
743  }
744  end_header(pb, hpos);
745 
746  /* patch the header size fields */
747 
748  cur_pos = avio_tell(pb);
749  header_size = cur_pos - header_offset;
750  if (asf->is_streamed) {
751  header_size += 8 + 30 + DATA_HEADER_SIZE;
752 
753  avio_seek(pb, header_offset - 10 - 30, SEEK_SET);
754  avio_wl16(pb, header_size);
755  avio_seek(pb, header_offset - 2 - 30, SEEK_SET);
756  avio_wl16(pb, header_size);
757 
758  header_size -= 8 + 30 + DATA_HEADER_SIZE;
759  }
760  header_size += 24 + 6;
761  avio_seek(pb, header_offset - 14, SEEK_SET);
762  avio_wl64(pb, header_size);
763  avio_seek(pb, cur_pos, SEEK_SET);
764 
765  /* movie chunk, followed by packets of packet_size */
766  asf->data_offset = cur_pos;
768  avio_wl64(pb, data_chunk_size);
770  avio_wl64(pb, asf->nb_packets); /* nb packets */
771  avio_w8(pb, 1); /* ??? */
772  avio_w8(pb, 1); /* ??? */
773  return 0;
774 }
775 
777 {
778  ASFContext *asf = s->priv_data;
779 
780  s->packet_size = asf->packet_size;
781  s->max_interleave_delta = 0;
782  asf->nb_packets = 0;
783 
784  if (s->nb_streams > 127) {
785  av_log(s, AV_LOG_ERROR, "ASF can only handle 127 streams\n");
786  return AVERROR(EINVAL);
787  }
788 
789  asf->index_ptr = av_malloc(sizeof(ASFIndex) * ASF_INDEX_BLOCK);
790  if (!asf->index_ptr)
791  return AVERROR(ENOMEM);
793  asf->maximum_packet = 0;
794 
795  /* the data-chunk-size has to be 50 (DATA_HEADER_SIZE), which is
796  * data_size - asf->data_offset at the moment this function is done.
797  * It is needed to use asf as a streamable format. */
798  if (asf_write_header1(s, 0, DATA_HEADER_SIZE) < 0) {
799  //av_free(asf);
800  av_freep(&asf->index_ptr);
801  return -1;
802  }
803 
804  avio_flush(s->pb);
805 
806  asf->packet_nb_payloads = 0;
807  asf->packet_timestamp_start = -1;
808  asf->packet_timestamp_end = -1;
809  ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
810  NULL, NULL, NULL, NULL);
811 
812  if (s->avoid_negative_ts < 0)
813  s->avoid_negative_ts = 1;
814 
815  return 0;
816 }
817 
819 {
820  ASFContext *asf = s->priv_data;
821 
822  asf->is_streamed = 1;
823 
824  return asf_write_header(s);
825 }
826 
828  unsigned sendtime, unsigned duration,
829  int nb_payloads, int padsize)
830 {
831  ASFContext *asf = s->priv_data;
832  AVIOContext *pb = s->pb;
833  int ppi_size, i;
834  int64_t start = avio_tell(pb);
835 
836  int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
837 
838  padsize -= PACKET_HEADER_MIN_SIZE;
839  if (asf->multi_payloads_present)
840  padsize--;
841  av_assert0(padsize >= 0);
842 
844  for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++)
845  avio_w8(pb, 0x0);
846 
847  if (asf->multi_payloads_present)
848  iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
849 
850  if (padsize > 0) {
851  if (padsize < 256)
852  iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
853  else
854  iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
855  }
856  avio_w8(pb, iLengthTypeFlags);
857 
859 
860  if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
861  avio_wl16(pb, padsize - 2);
862  if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
863  avio_w8(pb, padsize - 1);
864 
865  avio_wl32(pb, sendtime);
866  avio_wl16(pb, duration);
867  if (asf->multi_payloads_present)
868  avio_w8(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
869 
870  ppi_size = avio_tell(pb) - start;
871 
872  return ppi_size;
873 }
874 
876 {
877  ASFContext *asf = s->priv_data;
878  int packet_hdr_size, packet_filled_size;
879 
881 
882  if (asf->is_streamed)
883  put_chunk(s, 0x4424, s->packet_size, 0);
884 
885  packet_hdr_size = put_payload_parsing_info(s,
888  asf->packet_nb_payloads,
889  asf->packet_size_left);
890 
891  packet_filled_size = asf->packet_size - asf->packet_size_left;
892  av_assert0(packet_hdr_size <= asf->packet_size_left);
893  memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
894 
895  avio_write(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
896 
897  avio_flush(s->pb);
898  asf->nb_packets++;
899  asf->packet_nb_payloads = 0;
900  asf->packet_timestamp_start = -1;
901  asf->packet_timestamp_end = -1;
902  ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
903  NULL, NULL, NULL, NULL);
904 }
905 
907  int64_t presentation_time, int m_obj_size,
908  int m_obj_offset, int payload_len, int flags)
909 {
910  ASFContext *asf = s->priv_data;
911  AVIOContext *pb = &asf->pb;
912  int val;
913 
914  val = stream->num;
915  if (flags & AV_PKT_FLAG_KEY)
916  val |= ASF_PL_FLAG_KEY_FRAME;
917  avio_w8(pb, val);
918 
919  avio_w8(pb, stream->seq); // Media object number
920  avio_wl32(pb, m_obj_offset); // Offset Into Media Object
921 
922  // Replicated Data shall be at least 8 bytes long.
923  // The first 4 bytes of data shall contain the
924  // Size of the Media Object that the payload belongs to.
925  // The next 4 bytes of data shall contain the
926  // Presentation Time for the media object that the payload belongs to.
928 
929  avio_wl32(pb, m_obj_size); // Replicated Data - Media Object Size
930  avio_wl32(pb, (uint32_t) presentation_time); // Replicated Data - Presentation Time
931 
932  if (asf->multi_payloads_present) {
933  avio_wl16(pb, payload_len); // payload length
934  }
935 }
936 
937 static void put_frame(AVFormatContext *s, ASFStream *stream, AVStream *avst,
938  int64_t timestamp, const uint8_t *buf,
939  int m_obj_size, int flags)
940 {
941  ASFContext *asf = s->priv_data;
942  int m_obj_offset, payload_len, frag_len1;
943 
944  m_obj_offset = 0;
945  while (m_obj_offset < m_obj_size) {
946  payload_len = m_obj_size - m_obj_offset;
947  if (asf->packet_timestamp_start == -1) {
948  const int multi_payload_constant = (asf->packet_size - MULTI_PAYLOAD_HEADERS);
949  asf->multi_payloads_present = (payload_len < multi_payload_constant);
950 
951  asf->packet_size_left = asf->packet_size;
952  if (asf->multi_payloads_present) {
953  frag_len1 = multi_payload_constant - 1;
954  } else {
955  frag_len1 = asf->packet_size - SINGLE_PAYLOAD_HEADERS;
956  }
957  asf->packet_timestamp_start = timestamp;
958  } else {
959  // multi payloads
960  frag_len1 = asf->packet_size_left -
963 
964  if (frag_len1 < payload_len &&
966  flush_packet(s);
967  continue;
968  }
969  if (asf->packet_timestamp_start > INT64_MAX - UINT16_MAX ||
970  timestamp > asf->packet_timestamp_start + UINT16_MAX) {
971  flush_packet(s);
972  continue;
973  }
974  }
975  if (frag_len1 > 0) {
976  if (payload_len > frag_len1)
977  payload_len = frag_len1;
978  else if (payload_len == (frag_len1 - 1))
979  payload_len = frag_len1 - 2; // additional byte need to put padding length
980 
981  put_payload_header(s, stream, timestamp + PREROLL_TIME,
982  m_obj_size, m_obj_offset, payload_len, flags);
983  avio_write(&asf->pb, buf, payload_len);
984 
985  if (asf->multi_payloads_present)
987  else
989  asf->packet_timestamp_end = timestamp;
990 
991  asf->packet_nb_payloads++;
992  } else {
993  payload_len = 0;
994  }
995  m_obj_offset += payload_len;
996  buf += payload_len;
997 
998  if (!asf->multi_payloads_present)
999  flush_packet(s);
1001  flush_packet(s);
1003  flush_packet(s);
1004  }
1005  stream->seq++;
1006 }
1007 
1008 static int update_index(AVFormatContext *s, int start_sec,
1009  uint32_t packet_number, uint16_t packet_count,
1010  uint64_t packet_offset)
1011 {
1012  ASFContext *asf = s->priv_data;
1013 
1014  if (start_sec > asf->next_start_sec) {
1015  int i;
1016 
1017  if (!asf->next_start_sec) {
1018  asf->next_packet_number = packet_number;
1019  asf->next_packet_count = packet_count;
1020  asf->next_packet_offset = packet_offset;
1021  }
1022 
1023  if (start_sec > asf->nb_index_memory_alloc) {
1024  int err;
1025  asf->nb_index_memory_alloc = (start_sec + ASF_INDEX_BLOCK) & ~(ASF_INDEX_BLOCK - 1);
1026  if ((err = av_reallocp_array(&asf->index_ptr,
1027  asf->nb_index_memory_alloc,
1028  sizeof(*asf->index_ptr))) < 0) {
1029  asf->nb_index_memory_alloc = 0;
1030  return err;
1031  }
1032  }
1033  for (i = asf->next_start_sec; i < start_sec; i++) {
1036  asf->index_ptr[i].send_time = asf->next_start_sec * INT64_C(10000000);
1037  asf->index_ptr[i].offset = asf->next_packet_offset;
1038 
1039  }
1040  }
1041  asf->maximum_packet = FFMAX(asf->maximum_packet, packet_count);
1042  asf->next_packet_number = packet_number;
1043  asf->next_packet_count = packet_count;
1044  asf->next_packet_offset = packet_offset;
1045  asf->next_start_sec = start_sec;
1046 
1047  return 0;
1048 }
1049 
1051 {
1052  ASFContext *asf = s->priv_data;
1053  AVIOContext *pb = s->pb;
1054  ASFStream *stream;
1055  AVCodecParameters *par;
1056  uint32_t packet_number;
1057  int64_t pts;
1058  int start_sec;
1059  int flags = pkt->flags;
1060  int ret;
1061  uint64_t offset = avio_tell(pb);
1062 
1063  par = s->streams[pkt->stream_index]->codecpar;
1064  stream = &asf->streams[pkt->stream_index];
1065 
1066  if (par->codec_type == AVMEDIA_TYPE_AUDIO)
1067  flags &= ~AV_PKT_FLAG_KEY;
1068 
1069  pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
1070  av_assert0(pts != AV_NOPTS_VALUE);
1071  if ( pts < - PREROLL_TIME
1072  || pts > (INT_MAX-3)/10000LL * ASF_INDEXED_INTERVAL - PREROLL_TIME) {
1073  av_log(s, AV_LOG_ERROR, "input pts %"PRId64" is invalid\n", pts);
1074  return AVERROR(EINVAL);
1075  }
1076  pts *= 10000;
1077  asf->duration = FFMAX(asf->duration, pts + pkt->duration * 10000);
1078 
1079  packet_number = asf->nb_packets;
1080  put_frame(s, stream, s->streams[pkt->stream_index],
1081  pkt->dts, pkt->data, pkt->size, flags);
1082 
1083  start_sec = (int)((PREROLL_TIME * 10000 + pts + ASF_INDEXED_INTERVAL - 1)
1085 
1086  /* check index */
1087  if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
1088  uint16_t packet_count = asf->nb_packets - packet_number;
1089  ret = update_index(s, start_sec, packet_number, packet_count, offset);
1090  if (ret < 0)
1091  return ret;
1092  }
1093  asf->end_sec = start_sec;
1094 
1095  return 0;
1096 }
1097 
1099  uint16_t max, uint32_t count)
1100 {
1101  AVIOContext *pb = s->pb;
1102  int i;
1103 
1105  avio_wl64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2) * count);
1108  avio_wl32(pb, max);
1109  avio_wl32(pb, count);
1110  for (i = 0; i < count; i++) {
1111  avio_wl32(pb, index[i].packet_number);
1112  avio_wl16(pb, index[i].packet_count);
1113  }
1114 
1115  return 0;
1116 }
1117 
1119 {
1120  ASFContext *asf = s->priv_data;
1121  int64_t file_size, data_size;
1122  int ret;
1123 
1124  /* flush the current packet */
1125  if (asf->pb.buf_ptr > asf->pb.buffer)
1126  flush_packet(s);
1127 
1128  /* write index */
1129  data_size = avio_tell(s->pb);
1130  if (!asf->is_streamed && asf->next_start_sec) {
1131  if ((ret = update_index(s, asf->end_sec + 1, 0, 0, 0)) < 0)
1132  return ret;
1134  }
1135  avio_flush(s->pb);
1136 
1137  if (asf->is_streamed || !(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
1138  put_chunk(s, 0x4524, 0, 0); /* end of stream */
1139  } else {
1140  /* rewrite an updated header */
1141  file_size = avio_tell(s->pb);
1142  avio_seek(s->pb, 0, SEEK_SET);
1143  asf_write_header1(s, file_size, data_size - asf->data_offset);
1144  }
1145 
1146  av_freep(&asf->index_ptr);
1147  return 0;
1148 }
1149 
1150 static const AVOption asf_options[] = {
1151  { "packet_size", "Packet size", offsetof(ASFContext, packet_size), AV_OPT_TYPE_INT, {.i64 = 3200}, PACKET_SIZE_MIN, PACKET_SIZE_MAX, AV_OPT_FLAG_ENCODING_PARAM },
1152  { NULL },
1153 };
1154 
1155 #if CONFIG_ASF_MUXER
1156 static const AVClass asf_muxer_class = {
1157  .class_name = "ASF muxer",
1158  .item_name = av_default_item_name,
1159  .option = asf_options,
1160  .version = LIBAVUTIL_VERSION_INT,
1161 };
1162 
1164  .name = "asf",
1165  .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1166  .mime_type = "video/x-ms-asf",
1167  .extensions = "asf,wmv,wma",
1168  .priv_data_size = sizeof(ASFContext),
1169  .audio_codec = AV_CODEC_ID_WMAV2,
1170  .video_codec = AV_CODEC_ID_MSMPEG4V3,
1175  .codec_tag = (const AVCodecTag * const []) {
1177  },
1178  .priv_class = &asf_muxer_class,
1179 };
1180 #endif /* CONFIG_ASF_MUXER */
1181 
1182 #if CONFIG_ASF_STREAM_MUXER
1183 static const AVClass asf_stream_muxer_class = {
1184  .class_name = "ASF stream muxer",
1185  .item_name = av_default_item_name,
1186  .option = asf_options,
1187  .version = LIBAVUTIL_VERSION_INT,
1188 };
1189 
1191  .name = "asf_stream",
1192  .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1193  .mime_type = "video/x-ms-asf",
1194  .extensions = "asf,wmv,wma",
1195  .priv_data_size = sizeof(ASFContext),
1196  .audio_codec = AV_CODEC_ID_WMAV2,
1197  .video_codec = AV_CODEC_ID_MSMPEG4V3,
1202  .codec_tag = (const AVCodecTag * const []) {
1204  },
1205  .priv_class = &asf_stream_muxer_class,
1206 };
1207 #endif /* CONFIG_ASF_STREAM_MUXER */
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1587
const ff_asf_guid ff_asf_header
Definition: asf.c:23
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:690
unsigned int packet_size
Definition: avformat.h:1482
#define NULL
Definition: coverity.c:32
AVOutputFormat ff_asf_stream_muxer
const char const char void * val
Definition: avisynth_c.h:863
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:472
static int asf_write_markers(AVFormatContext *s)
Definition: asfenc.c:341
Bytestream IO Context.
Definition: avio.h:161
#define ASF_PAYLOAD_FLAGS
Definition: asfenc.c:56
const char * languages[128]
Definition: asfenc.c:226
int packet_size
Definition: asfenc.c:251
static void skip_to_key(AVFormatContext *s)
Definition: asfdec_f.c:1491
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1464
AVOption.
Definition: opt.h:246
void ff_put_bmp_header(AVIOContext *pb, AVCodecParameters *par, int for_asf, int ignore_extradata)
Definition: riffenc.c:209
int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int flags)
Write WAVEFORMAT header structure.
Definition: riffenc.c:54
const ff_asf_guid ff_asf_codec_comment_header
Definition: asf.c:73
const ff_asf_guid ff_asf_metadata_header
Definition: asf.c:108
#define PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD
Definition: asfenc.c:160
#define PACKET_SIZE_MIN
Definition: asfenc.c:187
void ff_put_guid(AVIOContext *s, const ff_asf_guid *g)
Definition: riffenc.c:349
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:228
unsigned int packet_nb_payloads
Definition: asfenc.c:237
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4929
const char * g
Definition: vf_curves.c:115
const char * desc
Definition: nvenc.c:68
int is_streamed
Definition: asfenc.c:224
#define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS
Definition: asfenc.c:167
int num
Definition: asfdec_f.c:48
AVRational sample_aspect_ratio
Video only.
Definition: avcodec.h:4046
Definition: asf.h:65
static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: asfenc.c:1050
uint64_t data_offset
beginning of the first data packet
Definition: asfdec_f.c:85
int64_t creation_time
Definition: asfenc.c:228
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3970
int num
Numerator.
Definition: rational.h:59
static int asf_write_index(AVFormatContext *s, const ASFIndex *index, uint16_t max, uint32_t count)
Definition: asfenc.c:1098
int size
Definition: avcodec.h:1483
uint32_t next_packet_number
Definition: asfenc.c:246
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition: dict.c:35
uint32_t packet_size
Definition: asfdec_o.c:101
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
unsigned char * buffer
Start of the buffer.
Definition: avio.h:226
GLint GLenum type
Definition: opengl_enc.c:104
static int put_payload_parsing_info(AVFormatContext *s, unsigned sendtime, unsigned duration, int nb_payloads, int padsize)
Definition: asfenc.c:827
uint16_t maximum_packet
Definition: asfenc.c:245
static AVPacket pkt
static int asf_write_trailer(AVFormatContext *s)
Definition: asfenc.c:1118
uint64_t offset
Definition: asf.h:69
AVDictionary * metadata
Definition: avformat.h:1319
uint16_t next_packet_count
Definition: asfenc.c:247
uint32_t packet_number
Definition: asf.h:66
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1430
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3962
#define SINGLE_PAYLOAD_HEADERS
Definition: asfenc.c:175
const ff_asf_guid ff_asf_reserved_4
Definition: asf.c:120
Format I/O context.
Definition: avformat.h:1358
const ff_asf_guid ff_asf_data_header
Definition: asf.c:80
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVClass * av_class
Definition: asfenc.c:222
Public dictionary API.
#define ASF_INDEX_BLOCK
Definition: asfenc.c:34
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:372
AVIOContext pb
Definition: asfenc.c:239
uint8_t
#define av_malloc(s)
int width
Video only.
Definition: avcodec.h:4036
const ff_asf_guid ff_asf_audio_stream
Definition: asf.c:39
const ff_asf_guid ff_asf_mutex_language
Definition: asf.c:154
AVOptions.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1500
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
static int asf_write_header(AVFormatContext *s)
Definition: asfenc.c:776
ASFStream streams[128]
it&#39;s max number and it&#39;s not that big
Definition: asfdec_f.c:77
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1426
int64_t duration
Definition: movenc.c:63
int packet_size_left
Definition: asfdec_f.c:83
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
uint8_t * data
Definition: avcodec.h:1482
int64_t max_interleave_delta
Maximum buffering duration for interleaving.
Definition: avformat.h:1660
uint32_t tag
Definition: movenc.c:1531
#define max(a, b)
Definition: cuda_runtime.h:33
#define ASF_PPI_LENGTH_TYPE_FLAGS
Definition: asfenc.c:54
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
#define av_log(a,...)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:276
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3999
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1514
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:460
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
uint32_t seqno
Definition: asfenc.c:223
int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
Parse creation_time in AVFormatContext metadata if exists and warn if the parsing fails...
Definition: utils.c:5696
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1598
#define PREROLL_TIME
Definition: asfenc.c:261
const ff_asf_guid ff_asf_head1_guid
Definition: asf.c:84
const ff_asf_guid ff_asf_simple_index_header
Definition: asf.c:96
const ff_asf_guid ff_asf_head2_guid
Definition: asf.c:88
static void put_payload_header(AVFormatContext *s, ASFStream *stream, int64_t presentation_time, int m_obj_size, int m_obj_offset, int payload_len, int flags)
Definition: asfenc.c:906
#define DATA_HEADER_SIZE
Definition: asfenc.c:184
const char * ff_convert_lang_to(const char *lang, enum AVLangCodespace target_codespace)
Convert a language code to a target codespace.
Definition: avlanguage.c:736
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
int64_t packet_timestamp_start
Definition: asfenc.c:235
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3966
const ff_asf_guid ff_asf_video_conceal_none
Definition: asf.c:61
static const AVOption asf_options[]
Definition: asfenc.c:1150
AVChapter ** chapters
Definition: avformat.h:1588
simple assert() macros that are a bit more flexible than ISO C assert().
GLsizei GLsizei * length
Definition: opengl_enc.c:114
#define ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD
Definition: asf.h:135
GLsizei count
Definition: opengl_enc.c:108
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:502
#define FFMAX(a, b)
Definition: common.h:94
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1488
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:3291
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3988
#define MULTI_PAYLOAD_HEADERS
Definition: asfenc.c:179
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array through a pointer to a pointer.
Definition: mem.c:205
#define ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT
Definition: asf.h:127
uint16_t size
Definition: asfdec_f.c:44
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1414
int block_align
Audio only.
Definition: avcodec.h:4087
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
uint64_t send_time
Definition: asf.h:68
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:32
static void put_chunk(AVFormatContext *s, int type, int payload_length, int flags)
Definition: asfenc.c:300
#define ASF_PACKET_ERROR_CORRECTION_FLAGS
Definition: asfenc.c:38
const ff_asf_guid ff_asf_extended_content_header
Definition: asf.c:92
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:466
static int32_t get_send_time(ASFContext *asf, int64_t pres_time, uint64_t *offset)
Definition: asfenc.c:326
#define ASF_PAYLOADS_PER_PACKET
Definition: asfenc.c:35
const char * name
Definition: avformat.h:505
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int32_t
#define ASF_PAYLOAD_REPLICATED_DATA_LENGTH
Definition: asfenc.c:158
int next_start_sec
Definition: asfenc.c:249
#define s(width, name)
Definition: cbs_vp9.c:257
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1689
int n
Definition: avisynth_c.h:760
AVDictionary * metadata
Definition: avformat.h:945
const ff_asf_guid ff_asf_my_guid
Definition: asf.c:126
#define FF_ARRAY_ELEMS(a)
#define ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE
Definition: asf.h:134
const ff_asf_guid ff_asf_extended_stream_properties_object
Definition: asf.c:146
uint8_t ff_asf_guid[16]
Definition: riff.h:92
Stream structure.
Definition: avformat.h:881
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
#define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE
Definition: asfenc.c:37
#define ASF_PPI_PROPERTY_FLAGS
Definition: asfenc.c:48
const ff_asf_guid ff_asf_file_header
Definition: asf.c:27
AVIOContext * pb
I/O context.
Definition: avformat.h:1400
const ff_asf_guid ff_asf_video_stream
Definition: asf.c:53
int64_t packet_timestamp_end
Definition: asfenc.c:236
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:196
uint16_t stream_language_index
Definition: asfdec_f.c:65
AVOutputFormat ff_asf_muxer
void * buf
Definition: avisynth_c.h:766
static void flush_packet(AVFormatContext *s)
Definition: asfenc.c:875
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
Describe the class of an AVClass context structure.
Definition: log.h:67
int index
Definition: gxfenc.c:89
Rational number (pair of numerator and denominator).
Definition: rational.h:58
const ff_asf_guid ff_asf_stream_header
Definition: asf.c:31
static const AVCodecTag codec_asf_bmp_tags[]
Definition: asfenc.c:254
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:238
int avio_put_str16le(AVIOContext *s, const char *str)
Convert an UTF-8 string to UTF-16LE and write it.
const AVMetadataConv ff_asf_metadata_conv[]
Definition: asf.c:159
const char * name
Name of the codec described by this descriptor.
Definition: avcodec.h:729
static int64_t put_header(AVIOContext *pb, const ff_asf_guid *g)
Definition: asfenc.c:278
#define PACKET_SIZE_MAX
Definition: asfenc.c:186
int64_t duration
in 100ns units
Definition: asfenc.c:231
const ff_asf_guid ff_asf_group_mutual_exclusion_object
Definition: asf.c:150
static int64_t unix_to_file_time(int64_t ti)
Definition: asfenc.c:317
This struct describes the properties of a single codec described by an AVCodecID. ...
Definition: avcodec.h:721
#define ASF_INDEXED_INTERVAL
Definition: asfenc.c:33
const ff_asf_guid ff_asf_comment_header
Definition: asf.c:69
static void put_frame(AVFormatContext *s, ASFStream *stream, AVStream *avst, int64_t timestamp, const uint8_t *buf, int m_obj_size, int flags)
Definition: asfenc.c:937
static int64_t pts
#define flags(name, subs,...)
Definition: cbs_av1.c:561
ASFIndex * index_ptr
Definition: asfenc.c:243
uint8_t type
Definition: asfdec_f.c:43
int64_t start
Definition: avformat.h:1318
int nb_languages
Definition: asfenc.c:227
const ff_asf_guid ff_asf_language_guid
Definition: asf.c:130
Main libavformat public API header.
int
static void end_header(AVIOContext *pb, int64_t pos)
Definition: asfenc.c:289
uint8_t packet_buf[PACKET_SIZE_MAX]
Definition: asfenc.c:238
#define ASF_PL_FLAG_KEY_FRAME
Definition: asf.h:167
int ffio_init_context(AVIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:81
uint64_t nb_packets
how many packets are there in the file, invalid if broadcasting
Definition: asfdec_o.c:100
unsigned char seq
Definition: asfdec_f.c:49
const ff_asf_guid ff_asf_audio_conceal_spread
Definition: asf.c:49
const ff_asf_guid ff_asf_codec_comment1_header
Definition: asf.c:76
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1317
char * key
Definition: dict.h:86
int den
Denominator.
Definition: rational.h:60
void ff_metadata_conv(AVDictionary **pm, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:26
static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
Definition: asfenc.c:385
char * value
Definition: dict.h:87
int len
void * priv_data
Format private data.
Definition: avformat.h:1386
uint32_t nb_index_memory_alloc
Definition: asfenc.c:244
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:349
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1481
int duration
Definition: asfdec_o.c:103
static int asf_write_stream_header(AVFormatContext *s)
Definition: asfenc.c:818
#define av_freep(p)
static void put_str16(AVIOContext *s, const char *tag)
Definition: asfenc.c:263
void INT64 start
Definition: avisynth_c.h:766
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
uint16_t packet_count
Definition: asf.h:67
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1028
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3974
#define PACKET_HEADER_MIN_SIZE
Definition: asfenc.c:146
const ff_asf_guid ff_asf_marker_header
Definition: asf.c:116
int stream_index
Definition: avcodec.h:1484
int end_sec
Definition: asfenc.c:250
static int update_index(AVFormatContext *s, int start_sec, uint32_t packet_number, uint16_t packet_count, uint64_t packet_offset)
Definition: asfenc.c:1008
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
unsigned char multi_payloads_present
Definition: asfenc.c:233
#define MKTAG(a, b, c, d)
Definition: common.h:366
This structure stores compressed data.
Definition: avcodec.h:1459
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1475
uint64_t next_packet_offset
Definition: asfenc.c:248
3-char terminological language codes as per ISO-IEC 639-2
Definition: avlanguage.h:33
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX
Tell ff_put_wav_header() to use WAVEFORMATEX even for PCM codecs.
Definition: riff.h:54