FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
asfenc.c
Go to the documentation of this file.
1 /*
2  * ASF muxer
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/avassert.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/mathematics.h"
25 #include "libavutil/parseutils.h"
26 #include "libavutil/opt.h"
27 #include "avformat.h"
28 #include "avlanguage.h"
29 #include "avio_internal.h"
30 #include "internal.h"
31 #include "riff.h"
32 #include "asf.h"
33 
34 #define ASF_INDEXED_INTERVAL 10000000
35 #define ASF_INDEX_BLOCK (1<<9)
36 #define ASF_PAYLOADS_PER_PACKET 63
37 
38 #define ASF_PACKET_ERROR_CORRECTION_DATA_SIZE 0x2
39 #define ASF_PACKET_ERROR_CORRECTION_FLAGS \
40  (ASF_PACKET_FLAG_ERROR_CORRECTION_PRESENT | \
41  ASF_PACKET_ERROR_CORRECTION_DATA_SIZE)
42 
43 #if (ASF_PACKET_ERROR_CORRECTION_FLAGS != 0)
44 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 1
45 #else
46 # define ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE 0
47 #endif
48 
49 #define ASF_PPI_PROPERTY_FLAGS \
50  (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE | \
51  ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD | \
52  ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE | \
53  ASF_PL_FLAG_STREAM_NUMBER_LENGTH_FIELD_IS_BYTE)
54 
55 #define ASF_PPI_LENGTH_TYPE_FLAGS 0
56 
57 #define ASF_PAYLOAD_FLAGS ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD
58 
59 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
60 # define ASF_PPI_SEQUENCE_FIELD_SIZE 1
61 #endif
62 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
63 # define ASF_PPI_SEQUENCE_FIELD_SIZE 2
64 #endif
65 #if (ASF_PPI_FLAG_SEQUENCE_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_SEQUENCE_FIELD_SIZE))
66 # define ASF_PPI_SEQUENCE_FIELD_SIZE 4
67 #endif
68 #ifndef ASF_PPI_SEQUENCE_FIELD_SIZE
69 # define ASF_PPI_SEQUENCE_FIELD_SIZE 0
70 #endif
71 
72 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
73 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 1
74 #endif
75 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
76 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 2
77 #endif
78 #if (ASF_PPI_FLAG_PACKET_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PACKET_LENGTH_FIELD_SIZE))
79 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 4
80 #endif
81 #ifndef ASF_PPI_PACKET_LENGTH_FIELD_SIZE
82 # define ASF_PPI_PACKET_LENGTH_FIELD_SIZE 0
83 #endif
84 
85 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
86 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 1
87 #endif
88 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
89 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 2
90 #endif
91 #if (ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_DWORD == (ASF_PPI_LENGTH_TYPE_FLAGS & ASF_PPI_MASK_PADDING_LENGTH_FIELD_SIZE))
92 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 4
93 #endif
94 #ifndef ASF_PPI_PADDING_LENGTH_FIELD_SIZE
95 # define ASF_PPI_PADDING_LENGTH_FIELD_SIZE 0
96 #endif
97 
98 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
99 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 1
100 #endif
101 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
102 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 2
103 #endif
104 #if (ASF_PL_FLAG_REPLICATED_DATA_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_REPLICATED_DATA_LENGTH_FIELD_SIZE))
105 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 4
106 #endif
107 #ifndef ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE
108 # define ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE 0
109 #endif
110 
111 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
112 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 1
113 #endif
114 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
115 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 2
116 #endif
117 #if (ASF_PL_FLAG_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_OFFSET_INTO_MEDIA_OBJECT_LENGTH_FIELD_SIZE))
118 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 4
119 #endif
120 #ifndef ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE
121 # define ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE 0
122 #endif
123 
124 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_BYTE == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
125 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 1
126 #endif
127 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_WORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
128 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 2
129 #endif
130 #if (ASF_PL_FLAG_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_IS_DWORD == (ASF_PPI_PROPERTY_FLAGS & ASF_PL_MASK_MEDIA_OBJECT_NUMBER_LENGTH_FIELD_SIZE))
131 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 4
132 #endif
133 #ifndef ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE
134 # define ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE 0
135 #endif
136 
137 #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_BYTE == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
138 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 1
139 #endif
140 #if (ASF_PL_FLAG_PAYLOAD_LENGTH_FIELD_IS_WORD == (ASF_PAYLOAD_FLAGS & ASF_PL_MASK_PAYLOAD_LENGTH_FIELD_SIZE))
141 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 2
142 #endif
143 #ifndef ASF_PAYLOAD_LENGTH_FIELD_SIZE
144 # define ASF_PAYLOAD_LENGTH_FIELD_SIZE 0
145 #endif
146 
147 #define PACKET_HEADER_MIN_SIZE \
148  (ASF_PACKET_ERROR_CORRECTION_FLAGS_FIELD_SIZE + \
149  ASF_PACKET_ERROR_CORRECTION_DATA_SIZE + \
150  1 + /* Length Type Flags */ \
151  1 + /* Property Flags */ \
152  ASF_PPI_PACKET_LENGTH_FIELD_SIZE + \
153  ASF_PPI_SEQUENCE_FIELD_SIZE + \
154  ASF_PPI_PADDING_LENGTH_FIELD_SIZE + \
155  4 + /* Send Time Field */ \
156  2) /* Duration Field */
157 
158 // Replicated Data shall be at least 8 bytes long.
159 #define ASF_PAYLOAD_REPLICATED_DATA_LENGTH 0x08
160 
161 #define PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD \
162  (1 + /* Stream Number */ \
163  ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
164  ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
165  ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
166  ASF_PAYLOAD_REPLICATED_DATA_LENGTH)
167 
168 #define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS \
169  (1 + /* Stream Number */ \
170  ASF_PAYLOAD_MEDIA_OBJECT_NUMBER_FIELD_SIZE + \
171  ASF_PAYLOAD_OFFSET_INTO_MEDIA_OBJECT_FIELD_SIZE + \
172  ASF_PAYLOAD_REPLICATED_DATA_LENGTH_FIELD_SIZE + \
173  ASF_PAYLOAD_REPLICATED_DATA_LENGTH + \
174  ASF_PAYLOAD_LENGTH_FIELD_SIZE)
175 
176 #define SINGLE_PAYLOAD_HEADERS \
177  (PACKET_HEADER_MIN_SIZE + \
178  PAYLOAD_HEADER_SIZE_SINGLE_PAYLOAD)
179 
180 #define MULTI_PAYLOAD_HEADERS \
181  (PACKET_HEADER_MIN_SIZE + \
182  1 + /* Payload Flags */ \
183  2 * PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS)
184 
185 #define DATA_HEADER_SIZE 50
186 
187 #define PACKET_SIZE_MAX 65536
188 #define PACKET_SIZE_MIN 100
189 
190 typedef struct ASFPayload {
191  uint8_t type;
192  uint16_t size;
193 } ASFPayload;
194 
195 typedef struct ASFStream {
196  int num;
197  unsigned char seq;
198  /* use for reading */
199  AVPacket pkt;
200  int frag_offset;
201  int packet_obj_size;
202  int timestamp;
203  int64_t duration;
204  int skip_to_key;
205  int pkt_clean;
206 
207  int ds_span; /* descrambling */
208  int ds_packet_size;
209  int ds_chunk_size;
210 
211  int64_t packet_pos;
212 
213  uint16_t stream_language_index;
214 
215  int palette_changed;
216  uint32_t palette[256];
217 
218  int payload_ext_ct;
219  ASFPayload payload[8];
220 } ASFStream;
221 
222 typedef struct ASFContext {
224  uint32_t seqno;
226  ASFStream streams[128]; ///< it's max number and it's not that big
227  const char *languages[128];
229  int64_t creation_time;
230  /* non-streamed additional info */
231  uint64_t nb_packets; ///< how many packets are there in the file, invalid if broadcasting
232  int64_t duration; ///< in 100ns units
233  /* packet filling */
234  unsigned char multi_payloads_present;
235  int packet_size_left;
238  unsigned int packet_nb_payloads;
241  /* only for reading */
242  uint64_t data_offset; ///< beginning of the first data packet
243 
246  uint16_t maximum_packet;
251  int end_sec;
253 } ASFContext;
254 
255 static const AVCodecTag codec_asf_bmp_tags[] = {
256  { AV_CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
257  { AV_CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
258  { AV_CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') },
259  { AV_CODEC_ID_NONE, 0 },
260 };
261 
262 #define PREROLL_TIME 3100
263 
264 static void put_str16(AVIOContext *s, const char *tag)
265 {
266  int len;
267  uint8_t *pb;
268  AVIOContext *dyn_buf;
269  if (avio_open_dyn_buf(&dyn_buf) < 0)
270  return;
271 
272  avio_put_str16le(dyn_buf, tag);
273  len = avio_close_dyn_buf(dyn_buf, &pb);
274  avio_wl16(s, len);
275  avio_write(s, pb, len);
276  av_freep(&pb);
277 }
278 
279 static int64_t put_header(AVIOContext *pb, const ff_asf_guid *g)
280 {
281  int64_t pos;
282 
283  pos = avio_tell(pb);
284  ff_put_guid(pb, g);
285  avio_wl64(pb, 24);
286  return pos;
287 }
288 
289 /* update header size */
290 static void end_header(AVIOContext *pb, int64_t pos)
291 {
292  int64_t pos1;
293 
294  pos1 = avio_tell(pb);
295  avio_seek(pb, pos + 16, SEEK_SET);
296  avio_wl64(pb, pos1 - pos);
297  avio_seek(pb, pos1, SEEK_SET);
298 }
299 
300 /* write an asf chunk (only used in streaming case) */
301 static void put_chunk(AVFormatContext *s, int type,
302  int payload_length, int flags)
303 {
304  ASFContext *asf = s->priv_data;
305  AVIOContext *pb = s->pb;
306  int length;
307 
308  length = payload_length + 8;
309  avio_wl16(pb, type);
310  avio_wl16(pb, length); // size
311  avio_wl32(pb, asf->seqno); // sequence number
312  avio_wl16(pb, flags); // unknown bytes
313  avio_wl16(pb, length); // size_confirm
314  asf->seqno++;
315 }
316 
317 /* convert from av time to windows time */
318 static int64_t unix_to_file_time(int64_t ti)
319 {
320  int64_t t;
321 
322  t = ti * INT64_C(10);
323  t += INT64_C(116444736000000000);
324  return t;
325 }
326 
327 static int32_t get_send_time(ASFContext *asf, int64_t pres_time, uint64_t *offset)
328 {
329  int i;
330  int32_t send_time = 0;
331  *offset = asf->data_offset + DATA_HEADER_SIZE;
332  for (i = 0; i < asf->next_start_sec; i++) {
333  if (pres_time <= asf->index_ptr[i].send_time)
334  break;
335  send_time = asf->index_ptr[i].send_time;
336  *offset = asf->index_ptr[i].offset;
337  }
338 
339  return send_time / 10000;
340 }
341 
343 {
344  ASFContext *asf = s->priv_data;
345  AVIOContext *pb = s->pb;
346  int i;
347  AVRational scale = {1, 10000000};
348  int64_t hpos = put_header(pb, &ff_asf_marker_header);
349 
350  ff_put_guid(pb, &ff_asf_reserved_4);// ASF spec mandates this reserved value
351  avio_wl32(pb, s->nb_chapters); // markers count
352  avio_wl16(pb, 0); // ASF spec mandates 0 for this
353  avio_wl16(pb, 0); // name length 0, no name given
354 
355  for (i = 0; i < s->nb_chapters; i++) {
356  AVChapter *c = s->chapters[i];
357  AVDictionaryEntry *t = av_dict_get(c->metadata, "title", NULL, 0);
358  int64_t pres_time = av_rescale_q(c->start, c->time_base, scale);
359  uint64_t offset;
360  int32_t send_time = get_send_time(asf, pres_time, &offset);
361  int len = 0;
362  uint8_t *buf;
363  AVIOContext *dyn_buf;
364  if (t) {
365  if (avio_open_dyn_buf(&dyn_buf) < 0)
366  return AVERROR(ENOMEM);
367  avio_put_str16le(dyn_buf, t->value);
368  len = avio_close_dyn_buf(dyn_buf, &buf);
369  }
370  avio_wl64(pb, offset); // offset of the packet with send_time
371  avio_wl64(pb, pres_time + PREROLL_TIME * 10000); // presentation time
372  avio_wl16(pb, 12 + len); // entry length
373  avio_wl32(pb, send_time); // send time
374  avio_wl32(pb, 0); // flags, should be 0
375  avio_wl32(pb, len / 2); // marker desc length in WCHARS!
376  if (t) {
377  avio_write(pb, buf, len); // marker desc
378  av_freep(&buf);
379  }
380  }
381  end_header(pb, hpos);
382  return 0;
383 }
384 
385 /* write the header (used two times if non streamed) */
386 static int asf_write_header1(AVFormatContext *s, int64_t file_size,
387  int64_t data_chunk_size)
388 {
389  ASFContext *asf = s->priv_data;
390  AVIOContext *pb = s->pb;
391  AVDictionaryEntry *tags[5];
392  int header_size, n, extra_size, extra_size2, wav_extra_size;
393  int has_title, has_aspect_ratio = 0;
394  int metadata_count;
395  AVCodecParameters *par;
396  int64_t header_offset, cur_pos, hpos;
397  int bit_rate;
398  int64_t duration;
399  int audio_language_counts[128] = { 0 };
400 
402 
403  tags[0] = av_dict_get(s->metadata, "title", NULL, 0);
404  tags[1] = av_dict_get(s->metadata, "author", NULL, 0);
405  tags[2] = av_dict_get(s->metadata, "copyright", NULL, 0);
406  tags[3] = av_dict_get(s->metadata, "comment", NULL, 0);
407  tags[4] = av_dict_get(s->metadata, "rating", NULL, 0);
408 
409  duration = asf->duration + PREROLL_TIME * 10000;
410  has_title = tags[0] || tags[1] || tags[2] || tags[3] || tags[4];
411 
412  if (!file_size) {
413  if (ff_parse_creation_time_metadata(s, &asf->creation_time, 0) != 0)
414  av_dict_set(&s->metadata, "creation_time", NULL, 0);
415  }
416 
417  metadata_count = av_dict_count(s->metadata);
418 
419  bit_rate = 0;
420  for (n = 0; n < s->nb_streams; n++) {
421  AVDictionaryEntry *entry;
422  par = s->streams[n]->codecpar;
423 
424  avpriv_set_pts_info(s->streams[n], 32, 1, 1000); /* 32 bit pts in ms */
425 
426  bit_rate += par->bit_rate;
427  if ( par->codec_type == AVMEDIA_TYPE_VIDEO
428  && par->sample_aspect_ratio.num > 0
429  && par->sample_aspect_ratio.den > 0)
430  has_aspect_ratio++;
431 
432  entry = av_dict_get(s->streams[n]->metadata, "language", NULL, 0);
433  if (entry) {
434  const char *iso6391lang = ff_convert_lang_to(entry->value, AV_LANG_ISO639_1);
435  if (iso6391lang) {
436  int i;
437  for (i = 0; i < asf->nb_languages; i++) {
438  if (!strcmp(asf->languages[i], iso6391lang)) {
439  asf->streams[n].stream_language_index = i;
440  break;
441  }
442  }
443  if (i >= asf->nb_languages) {
444  asf->languages[asf->nb_languages] = iso6391lang;
446  asf->nb_languages++;
447  }
448  if (par->codec_type == AVMEDIA_TYPE_AUDIO)
449  audio_language_counts[asf->streams[n].stream_language_index]++;
450  }
451  } else {
452  asf->streams[n].stream_language_index = 128;
453  }
454  }
455 
456  if (asf->is_streamed) {
457  put_chunk(s, 0x4824, 0, 0xc00); /* start of stream (length will be patched later) */
458  }
459 
461  avio_wl64(pb, -1); /* header length, will be patched after */
462  avio_wl32(pb, 3 + has_title + !!metadata_count + s->nb_streams); /* number of chunks in header */
463  avio_w8(pb, 1); /* ??? */
464  avio_w8(pb, 2); /* ??? */
465 
466  /* file header */
467  header_offset = avio_tell(pb);
468  hpos = put_header(pb, &ff_asf_file_header);
470  avio_wl64(pb, file_size);
472  avio_wl64(pb, asf->nb_packets); /* number of packets */
473  avio_wl64(pb, duration); /* end time stamp (in 100ns units) */
474  avio_wl64(pb, asf->duration); /* duration (in 100ns units) */
475  avio_wl64(pb, PREROLL_TIME); /* start time stamp */
476  avio_wl32(pb, (asf->is_streamed || !(pb->seekable & AVIO_SEEKABLE_NORMAL)) ? 3 : 2); /* ??? */
477  avio_wl32(pb, s->packet_size); /* packet size */
478  avio_wl32(pb, s->packet_size); /* packet size */
479  avio_wl32(pb, bit_rate ? bit_rate : -1); /* Maximum data rate in bps */
480  end_header(pb, hpos);
481 
482  /* header_extension */
483  hpos = put_header(pb, &ff_asf_head1_guid);
485  avio_wl16(pb, 6);
486  avio_wl32(pb, 0); /* length, to be filled later */
487  if (asf->nb_languages) {
488  int64_t hpos2;
489  int i;
490  int nb_audio_languages = 0;
491 
492  hpos2 = put_header(pb, &ff_asf_language_guid);
493  avio_wl16(pb, asf->nb_languages);
494  for (i = 0; i < asf->nb_languages; i++) {
495  avio_w8(pb, 6);
496  avio_put_str16le(pb, asf->languages[i]);
497  }
498  end_header(pb, hpos2);
499 
500  for (i = 0; i < asf->nb_languages; i++)
501  if (audio_language_counts[i])
502  nb_audio_languages++;
503 
504  if (nb_audio_languages > 1) {
507  avio_wl16(pb, nb_audio_languages);
508  for (i = 0; i < asf->nb_languages; i++) {
509  if (audio_language_counts[i]) {
510  avio_wl16(pb, audio_language_counts[i]);
511  for (n = 0; n < s->nb_streams; n++)
513  avio_wl16(pb, n + 1);
514  }
515  }
516  end_header(pb, hpos2);
517  }
518 
519  for (n = 0; n < s->nb_streams; n++) {
520  int64_t es_pos;
521  if (asf->streams[n].stream_language_index > 127)
522  continue;
524  avio_wl64(pb, 0); /* start time */
525  avio_wl64(pb, 0); /* end time */
526  avio_wl32(pb, s->streams[n]->codecpar->bit_rate); /* data bitrate bps */
527  avio_wl32(pb, 5000); /* buffer size ms */
528  avio_wl32(pb, 0); /* initial buffer fullness */
529  avio_wl32(pb, s->streams[n]->codecpar->bit_rate); /* peak data bitrate */
530  avio_wl32(pb, 5000); /* maximum buffer size ms */
531  avio_wl32(pb, 0); /* max initial buffer fullness */
532  avio_wl32(pb, 0); /* max object size */
533  avio_wl32(pb, (!asf->is_streamed && (pb->seekable & AVIO_SEEKABLE_NORMAL)) << 1); /* flags - seekable */
534  avio_wl16(pb, n + 1); /* stream number */
535  avio_wl16(pb, asf->streams[n].stream_language_index); /* language id index */
536  avio_wl64(pb, 0); /* avg time per frame */
537  avio_wl16(pb, 0); /* stream name count */
538  avio_wl16(pb, 0); /* payload extension system count */
539  end_header(pb, es_pos);
540  }
541  }
542  if (has_aspect_ratio) {
543  int64_t hpos2;
544  hpos2 = put_header(pb, &ff_asf_metadata_header);
545  avio_wl16(pb, 2 * has_aspect_ratio);
546  for (n = 0; n < s->nb_streams; n++) {
547  par = s->streams[n]->codecpar;
548  if ( par->codec_type == AVMEDIA_TYPE_VIDEO
549  && par->sample_aspect_ratio.num > 0
550  && par->sample_aspect_ratio.den > 0) {
551  AVRational sar = par->sample_aspect_ratio;
552  avio_wl16(pb, 0);
553  // the stream number is set like this below
554  avio_wl16(pb, n + 1);
555  avio_wl16(pb, 26); // name_len
556  avio_wl16(pb, 3); // value_type
557  avio_wl32(pb, 4); // value_len
558  avio_put_str16le(pb, "AspectRatioX");
559  avio_wl32(pb, sar.num);
560  avio_wl16(pb, 0);
561  // the stream number is set like this below
562  avio_wl16(pb, n + 1);
563  avio_wl16(pb, 26); // name_len
564  avio_wl16(pb, 3); // value_type
565  avio_wl32(pb, 4); // value_len
566  avio_put_str16le(pb, "AspectRatioY");
567  avio_wl32(pb, sar.den);
568  }
569  }
570  end_header(pb, hpos2);
571  }
572  {
573  int64_t pos1;
574  pos1 = avio_tell(pb);
575  avio_seek(pb, hpos + 42, SEEK_SET);
576  avio_wl32(pb, pos1 - hpos - 46);
577  avio_seek(pb, pos1, SEEK_SET);
578  }
579  end_header(pb, hpos);
580 
581  /* title and other info */
582  if (has_title) {
583  int len;
584  uint8_t *buf;
585  AVIOContext *dyn_buf;
586 
587  if (avio_open_dyn_buf(&dyn_buf) < 0)
588  return AVERROR(ENOMEM);
589 
590  hpos = put_header(pb, &ff_asf_comment_header);
591 
592  for (n = 0; n < FF_ARRAY_ELEMS(tags); n++) {
593  len = tags[n] ? avio_put_str16le(dyn_buf, tags[n]->value) : 0;
594  avio_wl16(pb, len);
595  }
596  len = avio_close_dyn_buf(dyn_buf, &buf);
597  avio_write(pb, buf, len);
598  av_freep(&buf);
599  end_header(pb, hpos);
600  }
601  if (metadata_count) {
604  avio_wl16(pb, metadata_count);
605  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
606  put_str16(pb, tag->key);
607  avio_wl16(pb, 0);
608  put_str16(pb, tag->value);
609  }
610  end_header(pb, hpos);
611  }
612  /* chapters using ASF markers */
613  if (!asf->is_streamed && s->nb_chapters) {
614  int ret;
615  if ((ret = asf_write_markers(s)) < 0)
616  return ret;
617  }
618  /* stream headers */
619  for (n = 0; n < s->nb_streams; n++) {
620  int64_t es_pos;
621  // ASFStream *stream = &asf->streams[n];
622 
623  par = s->streams[n]->codecpar;
624  asf->streams[n].num = n + 1;
625  asf->streams[n].seq = 1;
626 
627  switch (par->codec_type) {
628  case AVMEDIA_TYPE_AUDIO:
629  wav_extra_size = 0;
630  extra_size = 18 + wav_extra_size;
631  extra_size2 = 8;
632  break;
633  default:
634  case AVMEDIA_TYPE_VIDEO:
635  wav_extra_size = par->extradata_size;
636  extra_size = 0x33 + wav_extra_size;
637  extra_size2 = 0;
638  break;
639  }
640 
641  hpos = put_header(pb, &ff_asf_stream_header);
642  if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
645  } else {
648  }
649  avio_wl64(pb, 0); /* ??? */
650  es_pos = avio_tell(pb);
651  avio_wl32(pb, extra_size); /* wav header len */
652  avio_wl32(pb, extra_size2); /* additional data len */
653  avio_wl16(pb, n + 1); /* stream number */
654  avio_wl32(pb, 0); /* ??? */
655 
656  if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
657  /* WAVEFORMATEX header */
658  int wavsize = ff_put_wav_header(s, pb, par, FF_PUT_WAV_HEADER_FORCE_WAVEFORMATEX);
659 
660  if (wavsize < 0)
661  return -1;
662  if (wavsize != extra_size) {
663  cur_pos = avio_tell(pb);
664  avio_seek(pb, es_pos, SEEK_SET);
665  avio_wl32(pb, wavsize); /* wav header len */
666  avio_seek(pb, cur_pos, SEEK_SET);
667  }
668  /* ERROR Correction */
669  avio_w8(pb, 0x01);
670  if (par->codec_id == AV_CODEC_ID_ADPCM_G726 || !par->block_align) {
671  avio_wl16(pb, 0x0190);
672  avio_wl16(pb, 0x0190);
673  } else {
674  avio_wl16(pb, par->block_align);
675  avio_wl16(pb, par->block_align);
676  }
677  avio_wl16(pb, 0x01);
678  avio_w8(pb, 0x00);
679  } else {
680  avio_wl32(pb, par->width);
681  avio_wl32(pb, par->height);
682  avio_w8(pb, 2); /* ??? */
683  avio_wl16(pb, 40 + par->extradata_size); /* size */
684 
685  /* BITMAPINFOHEADER header */
686  ff_put_bmp_header(pb, par, 1, 0);
687  }
688  end_header(pb, hpos);
689  }
690 
691  /* media comments */
692 
695  avio_wl32(pb, s->nb_streams);
696  for (n = 0; n < s->nb_streams; n++) {
697  const AVCodecDescriptor *codec_desc;
698  const char *desc;
699 
700  par = s->streams[n]->codecpar;
701  codec_desc = avcodec_descriptor_get(par->codec_id);
702 
703  if (par->codec_type == AVMEDIA_TYPE_AUDIO)
704  avio_wl16(pb, 2);
705  else if (par->codec_type == AVMEDIA_TYPE_VIDEO)
706  avio_wl16(pb, 1);
707  else
708  avio_wl16(pb, -1);
709 
710  if (par->codec_id == AV_CODEC_ID_WMAV2)
711  desc = "Windows Media Audio V8";
712  else
713  desc = codec_desc ? codec_desc->name : NULL;
714 
715  if (desc) {
716  AVIOContext *dyn_buf;
717  uint8_t *buf;
718  int len;
719 
720  if (avio_open_dyn_buf(&dyn_buf) < 0)
721  return AVERROR(ENOMEM);
722 
723  avio_put_str16le(dyn_buf, desc);
724  len = avio_close_dyn_buf(dyn_buf, &buf);
725  avio_wl16(pb, len / 2); // "number of characters" = length in bytes / 2
726 
727  avio_write(pb, buf, len);
728  av_freep(&buf);
729  } else
730  avio_wl16(pb, 0);
731 
732  avio_wl16(pb, 0); /* no parameters */
733 
734  /* id */
735  if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
736  avio_wl16(pb, 2);
737  avio_wl16(pb, par->codec_tag);
738  } else {
739  avio_wl16(pb, 4);
740  avio_wl32(pb, par->codec_tag);
741  }
742  if (!par->codec_tag)
743  return -1;
744  }
745  end_header(pb, hpos);
746 
747  /* patch the header size fields */
748 
749  cur_pos = avio_tell(pb);
750  header_size = cur_pos - header_offset;
751  if (asf->is_streamed) {
752  header_size += 8 + 30 + DATA_HEADER_SIZE;
753 
754  avio_seek(pb, header_offset - 10 - 30, SEEK_SET);
755  avio_wl16(pb, header_size);
756  avio_seek(pb, header_offset - 2 - 30, SEEK_SET);
757  avio_wl16(pb, header_size);
758 
759  header_size -= 8 + 30 + DATA_HEADER_SIZE;
760  }
761  header_size += 24 + 6;
762  avio_seek(pb, header_offset - 14, SEEK_SET);
763  avio_wl64(pb, header_size);
764  avio_seek(pb, cur_pos, SEEK_SET);
765 
766  /* movie chunk, followed by packets of packet_size */
767  asf->data_offset = cur_pos;
769  avio_wl64(pb, data_chunk_size);
771  avio_wl64(pb, asf->nb_packets); /* nb packets */
772  avio_w8(pb, 1); /* ??? */
773  avio_w8(pb, 1); /* ??? */
774  return 0;
775 }
776 
778 {
779  ASFContext *asf = s->priv_data;
780 
781  s->packet_size = asf->packet_size;
782  s->max_interleave_delta = 0;
783  asf->nb_packets = 0;
784 
785  if (s->nb_streams > 127) {
786  av_log(s, AV_LOG_ERROR, "ASF can only handle 127 streams\n");
787  return AVERROR(EINVAL);
788  }
789 
790  asf->index_ptr = av_malloc(sizeof(ASFIndex) * ASF_INDEX_BLOCK);
791  if (!asf->index_ptr)
792  return AVERROR(ENOMEM);
794  asf->maximum_packet = 0;
795 
796  /* the data-chunk-size has to be 50 (DATA_HEADER_SIZE), which is
797  * data_size - asf->data_offset at the moment this function is done.
798  * It is needed to use asf as a streamable format. */
799  if (asf_write_header1(s, 0, DATA_HEADER_SIZE) < 0) {
800  //av_free(asf);
801  av_freep(&asf->index_ptr);
802  return -1;
803  }
804 
805  avio_flush(s->pb);
806 
807  asf->packet_nb_payloads = 0;
808  asf->packet_timestamp_start = -1;
809  asf->packet_timestamp_end = -1;
810  ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
811  NULL, NULL, NULL, NULL);
812 
813  if (s->avoid_negative_ts < 0)
814  s->avoid_negative_ts = 1;
815 
816  return 0;
817 }
818 
820 {
821  ASFContext *asf = s->priv_data;
822 
823  asf->is_streamed = 1;
824 
825  return asf_write_header(s);
826 }
827 
829  unsigned sendtime, unsigned duration,
830  int nb_payloads, int padsize)
831 {
832  ASFContext *asf = s->priv_data;
833  AVIOContext *pb = s->pb;
834  int ppi_size, i;
835  int64_t start = avio_tell(pb);
836 
837  int iLengthTypeFlags = ASF_PPI_LENGTH_TYPE_FLAGS;
838 
839  padsize -= PACKET_HEADER_MIN_SIZE;
840  if (asf->multi_payloads_present)
841  padsize--;
842  av_assert0(padsize >= 0);
843 
845  for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++)
846  avio_w8(pb, 0x0);
847 
848  if (asf->multi_payloads_present)
849  iLengthTypeFlags |= ASF_PPI_FLAG_MULTIPLE_PAYLOADS_PRESENT;
850 
851  if (padsize > 0) {
852  if (padsize < 256)
853  iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE;
854  else
855  iLengthTypeFlags |= ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD;
856  }
857  avio_w8(pb, iLengthTypeFlags);
858 
860 
861  if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD)
862  avio_wl16(pb, padsize - 2);
863  if (iLengthTypeFlags & ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_BYTE)
864  avio_w8(pb, padsize - 1);
865 
866  avio_wl32(pb, sendtime);
867  avio_wl16(pb, duration);
868  if (asf->multi_payloads_present)
869  avio_w8(pb, nb_payloads | ASF_PAYLOAD_FLAGS);
870 
871  ppi_size = avio_tell(pb) - start;
872 
873  return ppi_size;
874 }
875 
877 {
878  ASFContext *asf = s->priv_data;
879  int packet_hdr_size, packet_filled_size;
880 
882 
883  if (asf->is_streamed)
884  put_chunk(s, 0x4424, s->packet_size, 0);
885 
886  packet_hdr_size = put_payload_parsing_info(s,
889  asf->packet_nb_payloads,
890  asf->packet_size_left);
891 
892  packet_filled_size = asf->packet_size - asf->packet_size_left;
893  av_assert0(packet_hdr_size <= asf->packet_size_left);
894  memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
895 
896  avio_write(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
897 
898  avio_flush(s->pb);
899  asf->nb_packets++;
900  asf->packet_nb_payloads = 0;
901  asf->packet_timestamp_start = -1;
902  asf->packet_timestamp_end = -1;
903  ffio_init_context(&asf->pb, asf->packet_buf, s->packet_size, 1,
904  NULL, NULL, NULL, NULL);
905 }
906 
908  int64_t presentation_time, int m_obj_size,
909  int m_obj_offset, int payload_len, int flags)
910 {
911  ASFContext *asf = s->priv_data;
912  AVIOContext *pb = &asf->pb;
913  int val;
914 
915  val = stream->num;
916  if (flags & AV_PKT_FLAG_KEY)
917  val |= ASF_PL_FLAG_KEY_FRAME;
918  avio_w8(pb, val);
919 
920  avio_w8(pb, stream->seq); // Media object number
921  avio_wl32(pb, m_obj_offset); // Offset Into Media Object
922 
923  // Replicated Data shall be at least 8 bytes long.
924  // The first 4 bytes of data shall contain the
925  // Size of the Media Object that the payload belongs to.
926  // The next 4 bytes of data shall contain the
927  // Presentation Time for the media object that the payload belongs to.
929 
930  avio_wl32(pb, m_obj_size); // Replicated Data - Media Object Size
931  avio_wl32(pb, (uint32_t) presentation_time); // Replicated Data - Presentation Time
932 
933  if (asf->multi_payloads_present) {
934  avio_wl16(pb, payload_len); // payload length
935  }
936 }
937 
938 static void put_frame(AVFormatContext *s, ASFStream *stream, AVStream *avst,
939  int64_t timestamp, const uint8_t *buf,
940  int m_obj_size, int flags)
941 {
942  ASFContext *asf = s->priv_data;
943  int m_obj_offset, payload_len, frag_len1;
944 
945  m_obj_offset = 0;
946  while (m_obj_offset < m_obj_size) {
947  payload_len = m_obj_size - m_obj_offset;
948  if (asf->packet_timestamp_start == -1) {
949  const int multi_payload_constant = (asf->packet_size - MULTI_PAYLOAD_HEADERS);
950  asf->multi_payloads_present = (payload_len < multi_payload_constant);
951 
952  asf->packet_size_left = asf->packet_size;
953  if (asf->multi_payloads_present) {
954  frag_len1 = multi_payload_constant - 1;
955  } else {
956  frag_len1 = asf->packet_size - SINGLE_PAYLOAD_HEADERS;
957  }
958  asf->packet_timestamp_start = timestamp;
959  } else {
960  // multi payloads
961  frag_len1 = asf->packet_size_left -
964 
965  if (frag_len1 < payload_len &&
967  flush_packet(s);
968  continue;
969  }
970  if (asf->packet_timestamp_start > INT64_MAX - UINT16_MAX ||
971  timestamp > asf->packet_timestamp_start + UINT16_MAX) {
972  flush_packet(s);
973  continue;
974  }
975  }
976  if (frag_len1 > 0) {
977  if (payload_len > frag_len1)
978  payload_len = frag_len1;
979  else if (payload_len == (frag_len1 - 1))
980  payload_len = frag_len1 - 2; // additional byte need to put padding length
981 
982  put_payload_header(s, stream, timestamp + PREROLL_TIME,
983  m_obj_size, m_obj_offset, payload_len, flags);
984  avio_write(&asf->pb, buf, payload_len);
985 
986  if (asf->multi_payloads_present)
988  else
990  asf->packet_timestamp_end = timestamp;
991 
992  asf->packet_nb_payloads++;
993  } else {
994  payload_len = 0;
995  }
996  m_obj_offset += payload_len;
997  buf += payload_len;
998 
999  if (!asf->multi_payloads_present)
1000  flush_packet(s);
1002  flush_packet(s);
1004  flush_packet(s);
1005  }
1006  stream->seq++;
1007 }
1008 
1009 static int update_index(AVFormatContext *s, int start_sec,
1010  uint32_t packet_number, uint16_t packet_count,
1011  uint64_t packet_offset)
1012 {
1013  ASFContext *asf = s->priv_data;
1014 
1015  if (start_sec > asf->next_start_sec) {
1016  int i;
1017 
1018  if (!asf->next_start_sec) {
1019  asf->next_packet_number = packet_number;
1020  asf->next_packet_count = packet_count;
1021  asf->next_packet_offset = packet_offset;
1022  }
1023 
1024  if (start_sec > asf->nb_index_memory_alloc) {
1025  int err;
1026  asf->nb_index_memory_alloc = (start_sec + ASF_INDEX_BLOCK) & ~(ASF_INDEX_BLOCK - 1);
1027  if ((err = av_reallocp_array(&asf->index_ptr,
1028  asf->nb_index_memory_alloc,
1029  sizeof(*asf->index_ptr))) < 0) {
1030  asf->nb_index_memory_alloc = 0;
1031  return err;
1032  }
1033  }
1034  for (i = asf->next_start_sec; i < start_sec; i++) {
1036  asf->index_ptr[i].packet_count = asf->next_packet_count;
1037  asf->index_ptr[i].send_time = asf->next_start_sec * INT64_C(10000000);
1038  asf->index_ptr[i].offset = asf->next_packet_offset;
1039 
1040  }
1041  }
1042  asf->maximum_packet = FFMAX(asf->maximum_packet, packet_count);
1043  asf->next_packet_number = packet_number;
1044  asf->next_packet_count = packet_count;
1045  asf->next_packet_offset = packet_offset;
1046  asf->next_start_sec = start_sec;
1047 
1048  return 0;
1049 }
1050 
1052 {
1053  ASFContext *asf = s->priv_data;
1054  AVIOContext *pb = s->pb;
1055  ASFStream *stream;
1056  AVCodecParameters *par;
1057  uint32_t packet_number;
1058  int64_t pts;
1059  int start_sec;
1060  int flags = pkt->flags;
1061  int ret;
1062  uint64_t offset = avio_tell(pb);
1063 
1064  par = s->streams[pkt->stream_index]->codecpar;
1065  stream = &asf->streams[pkt->stream_index];
1066 
1067  if (par->codec_type == AVMEDIA_TYPE_AUDIO)
1068  flags &= ~AV_PKT_FLAG_KEY;
1069 
1070  pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
1071  av_assert0(pts != AV_NOPTS_VALUE);
1072  if ( pts < - PREROLL_TIME
1073  || pts > (INT_MAX-3)/10000LL * ASF_INDEXED_INTERVAL - PREROLL_TIME) {
1074  av_log(s, AV_LOG_ERROR, "input pts %"PRId64" is invalid\n", pts);
1075  return AVERROR(EINVAL);
1076  }
1077  pts *= 10000;
1078  asf->duration = FFMAX(asf->duration, pts + pkt->duration * 10000);
1079 
1080  packet_number = asf->nb_packets;
1081  put_frame(s, stream, s->streams[pkt->stream_index],
1082  pkt->dts, pkt->data, pkt->size, flags);
1083 
1084  start_sec = (int)((PREROLL_TIME * 10000 + pts + ASF_INDEXED_INTERVAL - 1)
1086 
1087  /* check index */
1088  if ((!asf->is_streamed) && (flags & AV_PKT_FLAG_KEY)) {
1089  uint16_t packet_count = asf->nb_packets - packet_number;
1090  ret = update_index(s, start_sec, packet_number, packet_count, offset);
1091  if (ret < 0)
1092  return ret;
1093  }
1094  asf->end_sec = start_sec;
1095 
1096  return 0;
1097 }
1098 
1100  uint16_t max, uint32_t count)
1101 {
1102  AVIOContext *pb = s->pb;
1103  int i;
1104 
1106  avio_wl64(pb, 24 + 16 + 8 + 4 + 4 + (4 + 2) * count);
1109  avio_wl32(pb, max);
1110  avio_wl32(pb, count);
1111  for (i = 0; i < count; i++) {
1112  avio_wl32(pb, index[i].packet_number);
1113  avio_wl16(pb, index[i].packet_count);
1114  }
1115 
1116  return 0;
1117 }
1118 
1120 {
1121  ASFContext *asf = s->priv_data;
1122  int64_t file_size, data_size;
1123  int ret;
1124 
1125  /* flush the current packet */
1126  if (asf->pb.buf_ptr > asf->pb.buffer)
1127  flush_packet(s);
1128 
1129  /* write index */
1130  data_size = avio_tell(s->pb);
1131  if (!asf->is_streamed && asf->next_start_sec) {
1132  if ((ret = update_index(s, asf->end_sec + 1, 0, 0, 0)) < 0)
1133  return ret;
1135  }
1136  avio_flush(s->pb);
1137 
1138  if (asf->is_streamed || !(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
1139  put_chunk(s, 0x4524, 0, 0); /* end of stream */
1140  } else {
1141  /* rewrite an updated header */
1142  file_size = avio_tell(s->pb);
1143  avio_seek(s->pb, 0, SEEK_SET);
1144  asf_write_header1(s, file_size, data_size - asf->data_offset);
1145  }
1146 
1147  av_freep(&asf->index_ptr);
1148  return 0;
1149 }
1150 
1151 static const AVOption asf_options[] = {
1152  { "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 },
1153  { NULL },
1154 };
1155 
1156 #if CONFIG_ASF_MUXER
1157 static const AVClass asf_muxer_class = {
1158  .class_name = "ASF muxer",
1159  .item_name = av_default_item_name,
1160  .option = asf_options,
1161  .version = LIBAVUTIL_VERSION_INT,
1162 };
1163 
1164 AVOutputFormat ff_asf_muxer = {
1165  .name = "asf",
1166  .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1167  .mime_type = "video/x-ms-asf",
1168  .extensions = "asf,wmv,wma",
1169  .priv_data_size = sizeof(ASFContext),
1170  .audio_codec = AV_CODEC_ID_WMAV2,
1171  .video_codec = AV_CODEC_ID_MSMPEG4V3,
1176  .codec_tag = (const AVCodecTag * const []) {
1178  },
1179  .priv_class = &asf_muxer_class,
1180 };
1181 #endif /* CONFIG_ASF_MUXER */
1182 
1183 #if CONFIG_ASF_STREAM_MUXER
1184 static const AVClass asf_stream_muxer_class = {
1185  .class_name = "ASF stream muxer",
1186  .item_name = av_default_item_name,
1187  .option = asf_options,
1188  .version = LIBAVUTIL_VERSION_INT,
1189 };
1190 
1191 AVOutputFormat ff_asf_stream_muxer = {
1192  .name = "asf_stream",
1193  .long_name = NULL_IF_CONFIG_SMALL("ASF (Advanced / Active Streaming Format)"),
1194  .mime_type = "video/x-ms-asf",
1195  .extensions = "asf,wmv,wma",
1196  .priv_data_size = sizeof(ASFContext),
1197  .audio_codec = AV_CODEC_ID_WMAV2,
1198  .video_codec = AV_CODEC_ID_MSMPEG4V3,
1203  .codec_tag = (const AVCodecTag * const []) {
1205  },
1206  .priv_class = &asf_stream_muxer_class,
1207 };
1208 #endif /* CONFIG_ASF_STREAM_MUXER */
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1556
uint32_t palette[256]
Definition: asfdec_f.c:68
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:671
unsigned int packet_size
Definition: avformat.h:1453
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:771
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:474
static int asf_write_markers(AVFormatContext *s)
Definition: asfenc.c:342
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define ASF_PAYLOAD_FLAGS
Definition: asfenc.c:57
const char * languages[128]
Definition: asfenc.c:227
int packet_size
Definition: asfenc.c:252
int packet_obj_size
Definition: asfdec_f.c:53
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1342
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:161
#define PACKET_SIZE_MIN
Definition: asfenc.c:188
void ff_put_guid(AVIOContext *s, const ff_asf_guid *g)
Definition: riffenc.c:349
#define LIBAVUTIL_VERSION_INT
Definition: version.h:86
unsigned char * buf_ptr
Current position in the buffer.
Definition: avio.h:228
unsigned int packet_nb_payloads
Definition: asfenc.c:238
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:4737
const char * g
Definition: vf_curves.c:112
const char * desc
Definition: nvenc.c:60
int is_streamed
Definition: asfenc.c:225
#define PAYLOAD_HEADER_SIZE_MULTIPLE_PAYLOADS
Definition: asfenc.c:168
int num
Definition: asfdec_f.c:48
AVRational sample_aspect_ratio
Video only.
Definition: avcodec.h:4228
Definition: asf.h:65
static int asf_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: asfenc.c:1051
uint64_t data_offset
beginning of the first data packet
Definition: asfdec_f.c:85
int64_t creation_time
Definition: asfenc.c:229
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4152
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:1099
int size
Definition: avcodec.h:1680
uint32_t next_packet_number
Definition: asfenc.c:247
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:244
int skip_to_key
Definition: asfdec_f.c:56
int palette_changed
Definition: asfdec_f.c:67
unsigned char * buffer
Start of the buffer.
Definition: avio.h:226
static int put_payload_parsing_info(AVFormatContext *s, unsigned sendtime, unsigned duration, int nb_payloads, int padsize)
Definition: asfenc.c:828
uint16_t maximum_packet
Definition: asfenc.c:246
static AVPacket pkt
static int asf_write_trailer(AVFormatContext *s)
Definition: asfenc.c:1119
uint64_t offset
Definition: asf.h:69
AVDictionary * metadata
Definition: avformat.h:1310
uint16_t next_packet_count
Definition: asfenc.c:248
uint32_t packet_number
Definition: asf.h:66
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1313
This struct describes the properties of an encoded stream.
Definition: avcodec.h:4144
#define SINGLE_PAYLOAD_HEADERS
Definition: asfenc.c:176
const ff_asf_guid ff_asf_reserved_4
Definition: asf.c:120
Format I/O context.
Definition: avformat.h:1349
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
int64_t duration
Definition: asfdec_f.c:55
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVClass * av_class
Definition: asfenc.c:223
Public dictionary API.
#define ASF_INDEX_BLOCK
Definition: asfenc.c:35
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:374
AVIOContext pb
Definition: asfenc.c:240
uint8_t
#define av_malloc(s)
int width
Video only.
Definition: avcodec.h:4218
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.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1697
static int asf_write_header(AVFormatContext *s)
Definition: asfenc.c:777
ASFStream streams[128]
it's max number and it's not that big
Definition: asfdec_f.c:77
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
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:1679
int64_t max_interleave_delta
Maximum buffering duration for interleaving.
Definition: avformat.h:1629
static int flags
Definition: log.c:57
uint32_t tag
Definition: movenc.c:1409
AVPacket pkt
Definition: asfdec_f.c:51
#define ASF_PPI_LENGTH_TYPE_FLAGS
Definition: asfenc.c:55
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:556
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:216
#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:4181
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1711
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:462
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:224
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:5456
#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:1567
#define PREROLL_TIME
Definition: asfenc.c:262
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
av_default_item_name
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:907
#define DATA_HEADER_SIZE
Definition: asfenc.c:185
#define AVERROR(e)
Definition: error.h:43
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:179
int64_t packet_timestamp_start
Definition: asfenc.c:236
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4148
const ff_asf_guid ff_asf_video_conceal_none
Definition: asf.c:61
int ds_chunk_size
Definition: asfdec_f.c:61
static const AVOption asf_options[]
Definition: asfenc.c:1151
AVChapter ** chapters
Definition: avformat.h:1557
simple assert() macros that are a bit more flexible than ISO C assert().
GLsizei GLsizei * length
Definition: opengl_enc.c:115
#define ASF_PPI_FLAG_PADDING_LENGTH_FIELD_IS_WORD
Definition: asf.h:135
int pkt_clean
Definition: asfdec_f.c:57
int frag_offset
Definition: asfdec_f.c:52
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:469
#define FFMAX(a, b)
Definition: common.h:94
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1685
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:3121
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:4170
#define MULTI_PAYLOAD_HEADERS
Definition: asfenc.c:180
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:191
#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:1405
int block_align
Audio only.
Definition: avcodec.h:4269
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:261
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:236
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:301
#define ASF_PACKET_ERROR_CORRECTION_FLAGS
Definition: asfenc.c:39
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
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:485
static int32_t get_send_time(ASFContext *asf, int64_t pres_time, uint64_t *offset)
Definition: asfenc.c:327
#define ASF_PAYLOADS_PER_PACKET
Definition: asfenc.c:36
const char * name
Definition: avformat.h:524
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int timestamp
Definition: asfdec_f.c:54
int32_t
#define ASF_PAYLOAD_REPLICATED_DATA_LENGTH
Definition: asfenc.c:159
int next_start_sec
Definition: asfenc.c:250
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1658
int n
Definition: avisynth_c.h:684
AVDictionary * metadata
Definition: avformat.h:961
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:90
Stream structure.
Definition: avformat.h:889
#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:38
#define ASF_PPI_PROPERTY_FLAGS
Definition: asfenc.c:49
int ds_span
Definition: asfdec_f.c:59
const ff_asf_guid ff_asf_file_header
Definition: asf.c:27
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
const ff_asf_guid ff_asf_video_stream
Definition: asf.c:53
int64_t packet_timestamp_end
Definition: asfenc.c:237
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:194
uint16_t stream_language_index
Definition: asfdec_f.c:65
void * buf
Definition: avisynth_c.h:690
static void flush_packet(AVFormatContext *s)
Definition: asfenc.c:876
GLint GLenum type
Definition: opengl_enc.c:105
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
int ds_packet_size
Definition: asfdec_f.c:60
const ff_asf_guid ff_asf_stream_header
Definition: asf.c:31
static const AVCodecTag codec_asf_bmp_tags[]
Definition: asfenc.c:255
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:711
static int64_t put_header(AVIOContext *pb, const ff_asf_guid *g)
Definition: asfenc.c:279
#define PACKET_SIZE_MAX
Definition: asfenc.c:187
int64_t duration
in 100ns units
Definition: asfenc.c:232
misc parsing utilities
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:318
This struct describes the properties of a single codec described by an AVCodecID. ...
Definition: avcodec.h:703
#define ASF_INDEXED_INTERVAL
Definition: asfenc.c:34
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:938
ASFPayload payload[8]
Definition: asfdec_f.c:71
static int64_t pts
Global timestamp for the audio frames.
ASFIndex * index_ptr
Definition: asfenc.c:244
uint8_t type
Definition: asfdec_f.c:43
int64_t start
Definition: avformat.h:1309
int nb_languages
Definition: asfenc.c:228
int64_t packet_pos
Definition: asfdec_f.c:63
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:290
uint8_t packet_buf[PACKET_SIZE_MAX]
Definition: asfenc.c:239
#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
static double c[64]
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:1308
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
int payload_ext_ct
Definition: asfdec_f.c:70
static int asf_write_header1(AVFormatContext *s, int64_t file_size, int64_t data_chunk_size)
Definition: asfenc.c:386
char * value
Definition: dict.h:87
int len
void * priv_data
Format private data.
Definition: avformat.h:1377
uint32_t nb_index_memory_alloc
Definition: asfenc.c:245
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:337
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1678
int duration
Definition: asfdec_o.c:103
static int asf_write_stream_header(AVFormatContext *s)
Definition: asfenc.c:819
#define av_freep(p)
static void put_str16(AVIOContext *s, const char *tag)
Definition: asfenc.c:264
void INT64 start
Definition: avisynth_c.h:690
#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
Definition: avformat.h:1252
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:4156
#define PACKET_HEADER_MIN_SIZE
Definition: asfenc.c:147
const ff_asf_guid ff_asf_marker_header
Definition: asf.c:116
int stream_index
Definition: avcodec.h:1681
int end_sec
Definition: asfenc.c:251
static int update_index(AVFormatContext *s, int start_sec, uint32_t packet_number, uint16_t packet_count, uint64_t packet_offset)
Definition: asfenc.c:1009
unsigned char multi_payloads_present
Definition: asfenc.c:234
#define MKTAG(a, b, c, d)
Definition: common.h:342
This structure stores compressed data.
Definition: avcodec.h:1656
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1672
uint64_t next_packet_offset
Definition: asfenc.c:249
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