FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/asfenc.c
Date: 2026-08-26 01:27:24
Exec Total Coverage
Lines: 385 552 69.7%
Functions: 14 18 77.8%
Branches: 134 242 55.4%

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