FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mux.c
Date: 2026-10-15 02:05:40
Exec Total Coverage
Lines: 588 828 71.0%
Functions: 32 38 84.2%
Branches: 449 664 67.6%

Line Branch Exec Source
1 /*
2 * muxing functions for use within FFmpeg
3 * Copyright (c) 2000, 2001, 2002 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 "avformat.h"
23 #include "avformat_internal.h"
24 #include "internal.h"
25 #include "mux.h"
26 #include "version.h"
27 #include "libavcodec/bsf.h"
28 #include "libavcodec/codec_desc.h"
29 #include "libavcodec/internal.h"
30 #include "packet_internal.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/dict.h"
34 #include "libavutil/timestamp.h"
35 #include "libavutil/avassert.h"
36 #include "libavutil/frame.h"
37 #include "libavutil/internal.h"
38 #include "libavutil/mathematics.h"
39
40 /**
41 * @file
42 * muxing functions for use within libavformat
43 */
44
45 /* fraction handling */
46
47 /**
48 * f = val + (num / den) + 0.5.
49 *
50 * 'num' is normalized so that it is such as 0 <= num < den.
51 *
52 * @param f fractional number
53 * @param val integer value
54 * @param num must be >= 0
55 * @param den must be >= 1
56 */
57 9670 static void frac_init(FFFrac *f, int64_t val, int64_t num, int64_t den)
58 {
59 9670 num += (den >> 1);
60
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9670 times.
9670 if (num >= den) {
61 ✗ val += num / den;
62 ✗ num = num % den;
63 }
64 9670 f->val = val;
65 9670 f->num = num;
66 9670 f->den = den;
67 9670 }
68
69 /**
70 * Fractional addition to f: f = f + (incr / f->den).
71 *
72 * @param f fractional number
73 * @param incr increment, can be positive or negative
74 */
75 630438 static void frac_add(FFFrac *f, int64_t incr)
76 {
77 int64_t num, den;
78
79 630438 num = f->num + incr;
80 630438 den = f->den;
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 630438 times.
630438 if (num < 0) {
82 ✗ f->val += num / den;
83 ✗ num = num % den;
84 ✗ if (num < 0) {
85 ✗ num += den;
86 ✗ f->val--;
87 }
88
2/2
✓ Branch 0 taken 613276 times.
✓ Branch 1 taken 17162 times.
630438 } else if (num >= den) {
89 613276 f->val += num / den;
90 613276 num = num % den;
91 }
92 630438 f->num = num;
93 630438 }
94
95 8989 int avformat_alloc_output_context2(AVFormatContext **avctx, const AVOutputFormat *oformat,
96 const char *format, const char *filename)
97 {
98 8989 AVFormatContext *s = avformat_alloc_context();
99 8989 int ret = 0;
100
101 8989 *avctx = NULL;
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8989 times.
8989 if (!s)
103 ✗ goto nomem;
104
105
2/2
✓ Branch 0 taken 8946 times.
✓ Branch 1 taken 43 times.
8989 if (!oformat) {
106
2/2
✓ Branch 0 taken 8739 times.
✓ Branch 1 taken 207 times.
8946 if (format) {
107 8739 oformat = av_guess_format(format, NULL, NULL);
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8739 times.
8739 if (!oformat) {
109 ✗ av_log(s, AV_LOG_ERROR, "Requested output format '%s' is not known.\n", format);
110 ✗ ret = AVERROR(EINVAL);
111 ✗ goto error;
112 }
113 } else {
114 207 oformat = av_guess_format(NULL, filename, NULL);
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 207 times.
207 if (!oformat) {
116 ✗ ret = AVERROR(EINVAL);
117 ✗ av_log(s, AV_LOG_ERROR,
118 "Unable to choose an output format for '%s'; "
119 "use a standard extension for the filename or specify "
120 "the format manually.\n", filename);
121 ✗ goto error;
122 }
123 }
124 }
125
126 8989 s->oformat = oformat;
127
2/2
✓ Branch 1 taken 4881 times.
✓ Branch 2 taken 4108 times.
8989 if (ffofmt(s->oformat)->priv_data_size > 0) {
128 4881 s->priv_data = av_mallocz(ffofmt(s->oformat)->priv_data_size);
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4881 times.
4881 if (!s->priv_data)
130 ✗ goto nomem;
131
2/2
✓ Branch 0 taken 4547 times.
✓ Branch 1 taken 334 times.
4881 if (s->oformat->priv_class) {
132 4547 *(const AVClass**)s->priv_data= s->oformat->priv_class;
133 4547 av_opt_set_defaults(s->priv_data);
134 }
135 } else
136 4108 s->priv_data = NULL;
137
138
2/2
✓ Branch 0 taken 8924 times.
✓ Branch 1 taken 65 times.
8989 if (filename) {
139
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8924 times.
8924 if (!(s->url = av_strdup(filename)))
140 ✗ goto nomem;
141
142 }
143 8989 *avctx = s;
144 8989 return 0;
145 ✗ nomem:
146 ✗ av_log(s, AV_LOG_ERROR, "Out of memory\n");
147 ✗ ret = AVERROR(ENOMEM);
148 ✗ error:
149 ✗ avformat_free_context(s);
150 ✗ return ret;
151 }
152
153 2326 static int validate_codec_tag(const AVFormatContext *s, const AVStream *st)
154 {
155 const AVCodecTag *avctag;
156 2326 enum AVCodecID id = AV_CODEC_ID_NONE;
157 2326 unsigned uppercase_tag = ff_toupper4(st->codecpar->codec_tag);
158 2326 int64_t tag = -1;
159
160 /**
161 * Check that tag + id is in the table
162 * If neither is in the table -> OK
163 * If tag is in the table with another id -> FAIL
164 * If id is in the table with another tag -> FAIL unless strict < normal
165 */
166
2/2
✓ Branch 0 taken 3382 times.
✓ Branch 1 taken 37 times.
3419 for (int n = 0; s->oformat->codec_tag[n]; n++) {
167 3382 avctag = s->oformat->codec_tag[n];
168
2/2
✓ Branch 0 taken 301356 times.
✓ Branch 1 taken 1093 times.
302449 while (avctag->id != AV_CODEC_ID_NONE) {
169
2/2
✓ Branch 1 taken 2289 times.
✓ Branch 2 taken 299067 times.
301356 if (ff_toupper4(avctag->tag) == uppercase_tag) {
170 2289 id = avctag->id;
171
1/2
✓ Branch 0 taken 2289 times.
✗ Branch 1 not taken.
2289 if (id == st->codecpar->codec_id)
172 2289 return 1;
173 }
174
2/2
✓ Branch 0 taken 191229 times.
✓ Branch 1 taken 107838 times.
299067 if (avctag->id == st->codecpar->codec_id)
175 191229 tag = avctag->tag;
176 299067 avctag++;
177 }
178 }
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (id != AV_CODEC_ID_NONE)
180 ✗ return 0;
181
3/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
37 if (tag >= 0 && (s->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
182 27 return 0;
183 10 return 1;
184 }
185
186
187 9061 static int init_muxer(AVFormatContext *s, AVDictionary **options)
188 {
189 9061 FormatContextInternal *const fci = ff_fc_internal(s);
190 9061 AVDictionary *tmp = NULL;
191 9061 const FFOutputFormat *of = ffofmt(s->oformat);
192 AVDictionaryEntry *e;
193 static const unsigned default_codec_offsets[] = {
194 [AVMEDIA_TYPE_VIDEO] = offsetof(AVOutputFormat, video_codec),
195 [AVMEDIA_TYPE_AUDIO] = offsetof(AVOutputFormat, audio_codec),
196 [AVMEDIA_TYPE_SUBTITLE] = offsetof(AVOutputFormat, subtitle_codec),
197 };
198 9061 unsigned nb_type[FF_ARRAY_ELEMS(default_codec_offsets)] = { 0 };
199 9061 int ret = 0;
200
201
2/2
✓ Branch 0 taken 9033 times.
✓ Branch 1 taken 28 times.
9061 if (options)
202 9033 av_dict_copy(&tmp, *options, 0);
203
204
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9061 times.
9061 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
205 ✗ goto fail;
206
6/8
✓ Branch 0 taken 4880 times.
✓ Branch 1 taken 4181 times.
✓ Branch 2 taken 4546 times.
✓ Branch 3 taken 334 times.
✓ Branch 4 taken 4546 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 4546 times.
13607 if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class &&
207 4546 (ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
208 ✗ goto fail;
209
210
3/4
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 9001 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 60 times.
9061 if (!s->url && !(s->url = av_strdup(""))) {
211 ✗ ret = AVERROR(ENOMEM);
212 ✗ goto fail;
213 }
214
215 // some sanity checks
216
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9061 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
9061 if (s->nb_streams == 0 && !(of->p.flags & AVFMT_NOSTREAMS)) {
217 ✗ av_log(s, AV_LOG_ERROR, "No streams to mux were specified\n");
218 ✗ ret = AVERROR(EINVAL);
219 ✗ goto fail;
220 }
221
222
2/2
✓ Branch 0 taken 9849 times.
✓ Branch 1 taken 9061 times.
18910 for (unsigned i = 0; i < s->nb_streams; i++) {
223 9849 AVStream *const st = s->streams[i];
224 9849 FFStream *const sti = ffstream(st);
225 9849 AVCodecParameters *const par = st->codecpar;
226 const AVCodecDescriptor *desc;
227
228
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9844 times.
9849 if (!st->time_base.num) {
229 /* fall back on the default timebase values */
230
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5 if (par->codec_type == AVMEDIA_TYPE_AUDIO && par->sample_rate)
231 ✗ avpriv_set_pts_info(st, 64, 1, par->sample_rate);
232 else
233 5 avpriv_set_pts_info(st, 33, 1, 90000);
234 }
235
236
3/3
✓ Branch 0 taken 2176 times.
✓ Branch 1 taken 7494 times.
✓ Branch 2 taken 179 times.
9849 switch (par->codec_type) {
237 2176 case AVMEDIA_TYPE_AUDIO:
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2176 times.
2176 if (par->sample_rate <= 0) {
239 ✗ av_log(s, AV_LOG_ERROR, "sample rate not set\n");
240 ✗ ret = AVERROR(EINVAL);
241 ✗ goto fail;
242 }
243
244
2/2
✓ Branch 0 taken 855 times.
✓ Branch 1 taken 1321 times.
2176 if (!par->block_align)
245 1710 par->block_align = par->ch_layout.nb_channels *
246 855 av_get_bits_per_sample(par->codec_id) >> 3;
247 2176 break;
248 7494 case AVMEDIA_TYPE_VIDEO:
249
2/4
✓ Branch 0 taken 7494 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7494 times.
7494 if ((par->width <= 0 || par->height <= 0) &&
250 ✗ !(of->p.flags & AVFMT_NODIMENSIONS)) {
251 ✗ av_log(s, AV_LOG_ERROR, "dimensions not set\n");
252 ✗ ret = AVERROR(EINVAL);
253 ✗ goto fail;
254 }
255
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7494 times.
7494 if (av_cmp_q(st->sample_aspect_ratio, par->sample_aspect_ratio)
256 ✗ && fabs(av_q2d(st->sample_aspect_ratio) - av_q2d(par->sample_aspect_ratio)) > 0.004*av_q2d(st->sample_aspect_ratio)
257 ) {
258 ✗ if (st->sample_aspect_ratio.num != 0 &&
259 ✗ st->sample_aspect_ratio.den != 0 &&
260 ✗ par->sample_aspect_ratio.num != 0 &&
261 ✗ par->sample_aspect_ratio.den != 0) {
262 ✗ av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
263 "(%d/%d) and encoder layer (%d/%d)\n",
264 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
265 par->sample_aspect_ratio.num,
266 par->sample_aspect_ratio.den);
267 ✗ ret = AVERROR(EINVAL);
268 ✗ goto fail;
269 }
270 }
271 7494 break;
272 }
273
2/2
✓ Branch 0 taken 1223 times.
✓ Branch 1 taken 8626 times.
9849 if (of->flags_internal & (FF_OFMT_FLAG_MAX_ONE_OF_EACH | FF_OFMT_FLAG_ONLY_DEFAULT_CODECS)) {
274 1223 enum AVCodecID default_codec_id = AV_CODEC_ID_NONE;
275 unsigned nb;
276
1/2
✓ Branch 0 taken 1223 times.
✗ Branch 1 not taken.
1223 if ((unsigned)par->codec_type < FF_ARRAY_ELEMS(default_codec_offsets)) {
277 1223 nb = ++nb_type[par->codec_type];
278
1/2
✓ Branch 0 taken 1223 times.
✗ Branch 1 not taken.
1223 if (default_codec_offsets[par->codec_type])
279 1223 default_codec_id = *(const enum AVCodecID*)((const char*)of + default_codec_offsets[par->codec_type]);
280 }
281
3/4
✓ Branch 0 taken 637 times.
✓ Branch 1 taken 586 times.
✓ Branch 2 taken 637 times.
✗ Branch 3 not taken.
1223 if (of->flags_internal & FF_OFMT_FLAG_ONLY_DEFAULT_CODECS &&
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 637 times.
637 default_codec_id != AV_CODEC_ID_NONE && par->codec_id != default_codec_id) {
283 ✗ av_log(s, AV_LOG_ERROR, "%s muxer supports only codec %s for type %s\n",
284 ✗ of->p.name, avcodec_get_name(default_codec_id), av_get_media_type_string(par->codec_type));
285 ✗ ret = AVERROR(EINVAL);
286 ✗ goto fail;
287
1/2
✓ Branch 0 taken 1223 times.
✗ Branch 1 not taken.
1223 } else if (default_codec_id == AV_CODEC_ID_NONE ||
288
3/4
✓ Branch 0 taken 1198 times.
✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1198 times.
1223 (of->flags_internal & FF_OFMT_FLAG_MAX_ONE_OF_EACH && nb > 1)) {
289 ✗ const char *type = av_get_media_type_string(par->codec_type);
290 ✗ av_log(s, AV_LOG_ERROR, "%s muxer does not support %s stream of type %s\n",
291 ✗ of->p.name, default_codec_id == AV_CODEC_ID_NONE ? "any" : "more than one",
292 type ? type : "unknown");
293 ✗ ret = AVERROR(EINVAL);
294 ✗ goto fail;
295 }
296 }
297
298 9849 desc = avcodec_descriptor_get(par->codec_id);
299
4/4
✓ Branch 0 taken 9786 times.
✓ Branch 1 taken 63 times.
✓ Branch 2 taken 488 times.
✓ Branch 3 taken 9298 times.
9849 if (desc && desc->props & AV_CODEC_PROP_REORDER)
300 488 sti->reorder = 1;
301
302 9849 sti->is_intra_only = ff_is_intra_only(par->codec_id);
303
304
2/2
✓ Branch 0 taken 4298 times.
✓ Branch 1 taken 5551 times.
9849 if (of->p.codec_tag) {
305
2/2
✓ Branch 0 taken 2309 times.
✓ Branch 1 taken 1989 times.
4298 if ( par->codec_tag
306
2/2
✓ Branch 0 taken 2206 times.
✓ Branch 1 taken 103 times.
2309 && par->codec_id == AV_CODEC_ID_RAWVIDEO
307
2/2
✓ Branch 1 taken 2179 times.
✓ Branch 2 taken 27 times.
2206 && ( av_codec_get_tag(of->p.codec_tag, par->codec_id) == 0
308
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 2162 times.
2179 || av_codec_get_tag(of->p.codec_tag, par->codec_id) == MKTAG('r', 'a', 'w', ' '))
309
2/2
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 17 times.
44 && !validate_codec_tag(s, st)) {
310 // the current rawvideo encoding system ends up setting
311 // the wrong codec_tag for avi/mov, we override it here
312 27 par->codec_tag = 0;
313 }
314
2/2
✓ Branch 0 taken 2282 times.
✓ Branch 1 taken 2016 times.
4298 if (par->codec_tag) {
315
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2282 times.
2282 if (!validate_codec_tag(s, st)) {
316 ✗ const uint32_t otag = av_codec_get_tag(s->oformat->codec_tag, par->codec_id);
317 ✗ av_log(s, AV_LOG_ERROR,
318 "Tag %s incompatible with output codec id '%d' (%s)\n",
319 ✗ av_fourcc2str(par->codec_tag), par->codec_id, av_fourcc2str(otag));
320 ✗ ret = AVERROR_INVALIDDATA;
321 ✗ goto fail;
322 }
323 } else
324 2016 par->codec_tag = av_codec_get_tag(of->p.codec_tag, par->codec_id);
325 }
326
327
2/2
✓ Branch 0 taken 9848 times.
✓ Branch 1 taken 1 times.
9849 if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT &&
328
1/2
✓ Branch 0 taken 9848 times.
✗ Branch 1 not taken.
9848 par->codec_id != AV_CODEC_ID_SMPTE_2038)
329 9848 fci->nb_interleaved_streams++;
330 }
331
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 9061 times.
9091 for (unsigned i = 0; i < s->nb_stream_groups; i++) {
332 30 AVStreamGroup *const stg = s->stream_groups[i];
333
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 switch (stg->type) {
335 ✗ case AV_STREAM_GROUP_PARAMS_TILE_GRID: {
336 #if FF_API_STREAM_GROUP_ENTRIES
337 FF_DISABLE_DEPRECATION_WARNINGS
338 ✗ AVStreamGroupTileGrid *tile_grid = stg->params.tile_grid;
339
340 // If both the global and the deprecated Tile Grid specific
341 // side data arrays are populated, prefer the latter.
342 ✗ if (tile_grid->nb_coded_side_data && stg->nb_coded_side_data)
343 ✗ av_packet_side_data_free(&stg->coded_side_data,
344 &stg->nb_coded_side_data);
345 ✗ if (tile_grid->nb_coded_side_data) {
346 ✗ for (int j = 0; j < tile_grid->nb_coded_side_data; j++) {
347 ✗ const AVPacketSideData *src = &tile_grid->coded_side_data[j];
348 AVPacketSideData *dst;
349
350 ✗ dst = av_packet_side_data_new(&stg->coded_side_data,
351 &stg->nb_coded_side_data,
352 ✗ src->type, src->size, 0);
353 ✗ if (!dst) {
354 ✗ ret = AVERROR(ENOMEM);
355 ✗ goto fail;
356 }
357
358 ✗ memcpy(dst->data, src->data, src->size);
359 }
360 }
361 ✗ break;
362 FF_ENABLE_DEPRECATION_WARNINGS
363 #endif
364 }
365 30 default:
366 30 break;
367 }
368 }
369 9061 fci->interleave_packet = of->interleave_packet;
370
2/2
✓ Branch 0 taken 8993 times.
✓ Branch 1 taken 68 times.
9061 if (!fci->interleave_packet)
371
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 8669 times.
8993 fci->interleave_packet = fci->nb_interleaved_streams > 1 ?
372 ff_interleave_packet_per_dts :
373 ff_interleave_packet_passthrough;
374
375
4/4
✓ Branch 0 taken 4181 times.
✓ Branch 1 taken 4880 times.
✓ Branch 2 taken 77 times.
✓ Branch 3 taken 4104 times.
9061 if (!s->priv_data && of->priv_data_size > 0) {
376 77 s->priv_data = av_mallocz(of->priv_data_size);
377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
77 if (!s->priv_data) {
378 ✗ ret = AVERROR(ENOMEM);
379 ✗ goto fail;
380 }
381
1/2
✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
77 if (of->p.priv_class) {
382 77 *(const AVClass **)s->priv_data = of->p.priv_class;
383 77 av_opt_set_defaults(s->priv_data);
384
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
77 if ((ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
385 ✗ goto fail;
386 }
387 }
388
389 /* set muxer identification string */
390
2/2
✓ Branch 0 taken 1183 times.
✓ Branch 1 taken 7878 times.
9061 if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
391 1183 av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
392 } else {
393 7878 av_dict_set(&s->metadata, "encoder", NULL, 0);
394 }
395
396
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 9061 times.
9065 for (e = NULL; e = av_dict_get(s->metadata, "encoder-", e, AV_DICT_IGNORE_SUFFIX); ) {
397 4 av_dict_set(&s->metadata, e->key, NULL, 0);
398 }
399
400
2/2
✓ Branch 0 taken 9033 times.
✓ Branch 1 taken 28 times.
9061 if (options) {
401 9033 av_dict_free(options);
402 9033 *options = tmp;
403 }
404
405
2/2
✓ Branch 0 taken 1874 times.
✓ Branch 1 taken 7187 times.
9061 if (of->init) {
406
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1874 times.
1874 if ((ret = of->init(s)) < 0) {
407 ✗ if (of->deinit)
408 ✗ of->deinit(s);
409 ✗ return ret;
410 }
411 1874 return ret == 0;
412 }
413
414 7187 return 0;
415
416 ✗ fail:
417 ✗ av_dict_free(&tmp);
418 ✗ return ret;
419 }
420
421 9061 static int init_pts(AVFormatContext *s)
422 {
423 9061 FFFormatContext *const si = ffformatcontext(s);
424
425 /* init PTS generation */
426
2/2
✓ Branch 0 taken 9849 times.
✓ Branch 1 taken 9061 times.
18910 for (unsigned i = 0; i < s->nb_streams; i++) {
427 9849 AVStream *const st = s->streams[i];
428 9849 FFStream *const sti = ffstream(st);
429 9849 int64_t den = AV_NOPTS_VALUE;
430
431
3/3
✓ Branch 0 taken 2176 times.
✓ Branch 1 taken 7494 times.
✓ Branch 2 taken 179 times.
9849 switch (st->codecpar->codec_type) {
432 2176 case AVMEDIA_TYPE_AUDIO:
433 2176 den = (int64_t)st->time_base.num * st->codecpar->sample_rate;
434 2176 break;
435 7494 case AVMEDIA_TYPE_VIDEO:
436 7494 den = (int64_t)st->time_base.num * st->time_base.den;
437 7494 break;
438 179 default:
439 179 break;
440 }
441
442
2/2
✓ Branch 0 taken 9670 times.
✓ Branch 1 taken 179 times.
9849 if (den != AV_NOPTS_VALUE) {
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9670 times.
9670 if (den <= 0)
444 ✗ return AVERROR_INVALIDDATA;
445
446 9670 frac_init(&sti->priv_pts, 0, 0, den);
447 }
448 }
449
450 9061 si->avoid_negative_ts_status = AVOID_NEGATIVE_TS_UNKNOWN;
451
2/2
✓ Branch 0 taken 6135 times.
✓ Branch 1 taken 2926 times.
9061 if (s->avoid_negative_ts < 0) {
452 av_assert2(s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO);
453
2/2
✓ Branch 0 taken 5060 times.
✓ Branch 1 taken 1075 times.
6135 if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) {
454 5060 s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_DISABLED;
455 5060 si->avoid_negative_ts_status = AVOID_NEGATIVE_TS_DISABLED;
456 } else
457 1075 s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_NON_NEGATIVE;
458
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2923 times.
2926 } else if (s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_DISABLED)
459 3 si->avoid_negative_ts_status = AVOID_NEGATIVE_TS_DISABLED;
460
461 9061 return 0;
462 }
463
464 624994 static void flush_if_needed(AVFormatContext *s)
465 {
466
3/4
✓ Branch 0 taken 624841 times.
✓ Branch 1 taken 153 times.
✓ Branch 2 taken 624841 times.
✗ Branch 3 not taken.
624994 if (s->pb && s->pb->error >= 0) {
467
2/4
✓ Branch 0 taken 624841 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 624841 times.
624841 if (s->flush_packets == 1 || s->flags & AVFMT_FLAG_FLUSH_PACKETS)
468 ✗ avio_flush(s->pb);
469
2/4
✓ Branch 0 taken 624841 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 624841 times.
✗ Branch 3 not taken.
624841 else if (s->flush_packets && !(s->oformat->flags & AVFMT_NOFILE))
470 624841 avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_FLUSH_POINT);
471 }
472 624994 }
473
474 9061 static void deinit_muxer(AVFormatContext *s)
475 {
476 9061 FormatContextInternal *const fci = ff_fc_internal(s);
477 9061 const FFOutputFormat *const of = ffofmt(s->oformat);
478
4/6
✓ Branch 0 taken 9061 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4551 times.
✓ Branch 3 taken 4510 times.
✓ Branch 4 taken 4551 times.
✗ Branch 5 not taken.
9061 if (of && of->deinit && fci->initialized)
479 4551 of->deinit(s);
480 9061 fci->initialized =
481 9061 fci->streams_initialized = 0;
482 9061 }
483
484 9061 int avformat_init_output(AVFormatContext *s, AVDictionary **options)
485 {
486 9061 FormatContextInternal *const fci = ff_fc_internal(s);
487 9061 int ret = 0;
488
489
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9061 times.
9061 if ((ret = init_muxer(s, options)) < 0)
490 ✗ return ret;
491
492 9061 fci->initialized = 1;
493 9061 fci->streams_initialized = ret;
494
495
4/4
✓ Branch 1 taken 1874 times.
✓ Branch 2 taken 7187 times.
✓ Branch 3 taken 1552 times.
✓ Branch 4 taken 322 times.
9061 if (ffofmt(s->oformat)->init && ret) {
496
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1552 times.
1552 if ((ret = init_pts(s)) < 0)
497 ✗ return ret;
498
499 1552 return AVSTREAM_INIT_IN_INIT_OUTPUT;
500 }
501
502 7509 return AVSTREAM_INIT_IN_WRITE_HEADER;
503 }
504
505 9061 int avformat_write_header(AVFormatContext *s, AVDictionary **options)
506 {
507 9061 FormatContextInternal *const fci = ff_fc_internal(s);
508 9061 int already_initialized = fci->initialized;
509 9061 int streams_already_initialized = fci->streams_initialized;
510 9061 int ret = 0;
511
512
2/2
✓ Branch 0 taken 9031 times.
✓ Branch 1 taken 30 times.
9061 if (!already_initialized)
513
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9031 times.
9031 if ((ret = avformat_init_output(s, options)) < 0)
514 ✗ return ret;
515
516
2/2
✓ Branch 1 taken 6540 times.
✓ Branch 2 taken 2521 times.
9061 if (ffofmt(s->oformat)->write_header) {
517
3/4
✓ Branch 0 taken 6393 times.
✓ Branch 1 taken 147 times.
✓ Branch 2 taken 6393 times.
✗ Branch 3 not taken.
6540 if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
518 6393 avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_HEADER);
519 6540 ret = ffofmt(s->oformat)->write_header(s);
520
4/6
✓ Branch 0 taken 6540 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6393 times.
✓ Branch 3 taken 147 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6393 times.
6540 if (ret >= 0 && s->pb && s->pb->error < 0)
521 ✗ ret = s->pb->error;
522
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6540 times.
6540 if (ret < 0)
523 ✗ goto fail;
524 6540 flush_if_needed(s);
525 }
526
3/4
✓ Branch 0 taken 8867 times.
✓ Branch 1 taken 194 times.
✓ Branch 2 taken 8867 times.
✗ Branch 3 not taken.
9061 if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
527 8867 avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_UNKNOWN);
528
529
2/2
✓ Branch 0 taken 7509 times.
✓ Branch 1 taken 1552 times.
9061 if (!fci->streams_initialized) {
530
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7509 times.
7509 if ((ret = init_pts(s)) < 0)
531 ✗ goto fail;
532 }
533
534 9061 return streams_already_initialized;
535
536 ✗ fail:
537 ✗ deinit_muxer(s);
538 ✗ return ret;
539 }
540
541 #define AV_PKT_FLAG_UNCODED_FRAME 0x2000
542
543
544 #if FF_API_COMPUTE_PKT_FIELDS2
545 FF_DISABLE_DEPRECATION_WARNINGS
546 //FIXME merge with compute_pkt_fields
547 633117 static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *pkt)
548 {
549 633117 FormatContextInternal *const fci = ff_fc_internal(s);
550 633117 FFStream *const sti = ffstream(st);
551 633117 int delay = st->codecpar->video_delay;
552 int frame_size;
553
554
2/2
✓ Branch 0 taken 631914 times.
✓ Branch 1 taken 1203 times.
633117 if (!fci->missing_ts_warning &&
555
2/2
✓ Branch 0 taken 298424 times.
✓ Branch 1 taken 333490 times.
631914 !(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
556
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 298400 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
298424 (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC) || (st->disposition & AV_DISPOSITION_TIMED_THUMBNAILS)) &&
557
4/4
✓ Branch 0 taken 298378 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 298377 times.
298400 (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE)) {
558 23 av_log(s, AV_LOG_WARNING,
559 "Timestamps are unset in a packet for stream %d. "
560 "This is deprecated and will stop working in the future. "
561 "Fix your code to set the timestamps properly\n", st->index);
562 23 fci->missing_ts_warning = 1;
563 }
564
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 633117 times.
633117 if (s->debug & AV_FDEBUG_TS)
566 ✗ av_log(s, AV_LOG_DEBUG, "compute_muxer_pkt_fields: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
567 ✗ av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), delay, pkt->size, pkt->stream_index);
568
569
6/6
✓ Branch 0 taken 8101 times.
✓ Branch 1 taken 625016 times.
✓ Branch 2 taken 8100 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 3822 times.
✓ Branch 5 taken 4278 times.
633117 if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
570 3822 pkt->pts = pkt->dts;
571
572 //XXX/FIXME this is a temporary hack until all encoders output pts
573
8/8
✓ Branch 0 taken 623683 times.
✓ Branch 1 taken 9434 times.
✓ Branch 2 taken 4279 times.
✓ Branch 3 taken 619404 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 13711 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 1 times.
633117 if ((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay) {
574 static int warned;
575
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!warned) {
576 1 av_log(s, AV_LOG_WARNING, "Encoder did not produce proper pts, making some up.\n");
577 1 warned = 1;
578 }
579 1 pkt->dts =
580 // pkt->pts= st->cur_dts;
581 1 pkt->pts = sti->priv_pts.val;
582 }
583
584 //calculate dts from pts
585
5/6
✓ Branch 0 taken 628839 times.
✓ Branch 1 taken 4278 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 628835 times.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
633117 if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
586 4 sti->pts_buffer[0] = pkt->pts;
587
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3 times.
5 for (int i = 1; i < delay + 1 && sti->pts_buffer[i] == AV_NOPTS_VALUE; i++)
588 1 sti->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
589
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 for (int i = 0; i<delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++)
590 4 FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]);
591
592 4 pkt->dts = sti->pts_buffer[0];
593 }
594
595
4/4
✓ Branch 0 taken 626604 times.
✓ Branch 1 taken 6513 times.
✓ Branch 2 taken 616911 times.
✓ Branch 3 taken 9693 times.
633117 if (sti->cur_dts && sti->cur_dts != AV_NOPTS_VALUE &&
596
2/2
✓ Branch 0 taken 432808 times.
✓ Branch 1 taken 184103 times.
616911 ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
597
2/2
✓ Branch 0 taken 432365 times.
✓ Branch 1 taken 443 times.
432808 st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE &&
598
2/2
✓ Branch 0 taken 432295 times.
✓ Branch 1 taken 70 times.
432365 st->codecpar->codec_type != AVMEDIA_TYPE_DATA &&
599
3/4
✓ Branch 0 taken 432252 times.
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 616868 times.
616911 sti->cur_dts >= pkt->dts) || sti->cur_dts > pkt->dts)) {
600 43 av_log(s, AV_LOG_ERROR,
601 "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
602 43 st->index, av_ts2str(sti->cur_dts), av_ts2str(pkt->dts));
603 43 return AVERROR(EINVAL);
604 }
605
4/6
✓ Branch 0 taken 633074 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 628796 times.
✓ Branch 3 taken 4278 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 628796 times.
633074 if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
606 ✗ av_log(s, AV_LOG_ERROR,
607 "pts (%s) < dts (%s) in stream %d\n",
608 ✗ av_ts2str(pkt->pts), av_ts2str(pkt->dts),
609 st->index);
610 ✗ return AVERROR(EINVAL);
611 }
612
613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 633074 times.
633074 if (s->debug & AV_FDEBUG_TS)
614 ✗ av_log(s, AV_LOG_DEBUG, "av_write_frame: pts2:%s dts2:%s\n",
615 ✗ av_ts2str(pkt->pts), av_ts2str(pkt->dts));
616
617 633074 sti->cur_dts = pkt->dts;
618 633074 sti->priv_pts.val = pkt->dts;
619
620 /* update pts */
621
3/3
✓ Branch 0 taken 465005 times.
✓ Branch 1 taken 165434 times.
✓ Branch 2 taken 2635 times.
633074 switch (st->codecpar->codec_type) {
622 465005 case AVMEDIA_TYPE_AUDIO:
623 930010 frame_size = (pkt->flags & AV_PKT_FLAG_UNCODED_FRAME) ?
624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 465005 times.
465005 (*(AVFrame **)pkt->data)->nb_samples :
625 465005 av_get_audio_frame_duration2(st->codecpar, pkt->size);
626
627 /* HACK/FIXME, we skip the initial 0 size packets as they are most
628 * likely equal to the encoder delay, but it would be better if we
629 * had the real timestamps from the encoder */
630
7/8
✓ Branch 0 taken 465005 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 79 times.
✓ Branch 3 taken 464926 times.
✓ Branch 4 taken 76 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 75 times.
✓ Branch 7 taken 1 times.
465005 if (frame_size >= 0 && (pkt->size || sti->priv_pts.num != sti->priv_pts.den >> 1 || sti->priv_pts.val)) {
631 465004 frac_add(&sti->priv_pts, (int64_t)st->time_base.den * frame_size);
632 }
633 465005 break;
634 165434 case AVMEDIA_TYPE_VIDEO:
635 165434 frac_add(&sti->priv_pts, (int64_t)st->time_base.den * st->time_base.num);
636 165434 break;
637 }
638 633074 return 0;
639 }
640 FF_ENABLE_DEPRECATION_WARNINGS
641 #endif
642
643 633117 static void guess_pkt_duration(AVFormatContext *s, AVStream *st, AVPacket *pkt)
644 {
645
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 633116 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
633117 if (pkt->duration < 0 && st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
646 ✗ av_log(s, AV_LOG_WARNING, "Packet with invalid duration %"PRId64" in stream %d\n",
647 pkt->duration, pkt->stream_index);
648 ✗ pkt->duration = 0;
649 }
650
651
2/2
✓ Branch 0 taken 613122 times.
✓ Branch 1 taken 19995 times.
633117 if (pkt->duration)
652 613122 return;
653
654
3/3
✓ Branch 0 taken 18296 times.
✓ Branch 1 taken 1264 times.
✓ Branch 2 taken 435 times.
19995 switch (st->codecpar->codec_type) {
655 18296 case AVMEDIA_TYPE_VIDEO:
656
3/4
✓ Branch 0 taken 17446 times.
✓ Branch 1 taken 850 times.
✓ Branch 2 taken 17446 times.
✗ Branch 3 not taken.
18296 if (st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0) {
657 17446 pkt->duration = av_rescale_q(1, av_inv_q(st->avg_frame_rate),
658 st->time_base);
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 850 times.
850 } else if (st->time_base.num * 1000LL > st->time_base.den)
660 ✗ pkt->duration = 1;
661 18296 break;
662 1264 case AVMEDIA_TYPE_AUDIO: {
663 1264 int frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
664
3/4
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 985 times.
✓ Branch 2 taken 279 times.
✗ Branch 3 not taken.
1264 if (frame_size && st->codecpar->sample_rate) {
665 279 pkt->duration = av_rescale_q(frame_size,
666 279 (AVRational){1, st->codecpar->sample_rate},
667 st->time_base);
668 }
669 1264 break;
670 }
671 }
672 }
673
674 633104 static void handle_avoid_negative_ts(FFFormatContext *si, FFStream *sti,
675 AVPacket *pkt)
676 {
677 633104 AVFormatContext *const s = &si->pub;
678 int64_t offset;
679
680
2/2
✓ Branch 0 taken 460035 times.
✓ Branch 1 taken 173069 times.
633104 if (!AVOID_NEGATIVE_TS_ENABLED(si->avoid_negative_ts_status))
681 460035 return;
682
683
2/2
✓ Branch 0 taken 3989 times.
✓ Branch 1 taken 169080 times.
173069 if (si->avoid_negative_ts_status == AVOID_NEGATIVE_TS_UNKNOWN) {
684 3989 int use_pts = si->avoid_negative_ts_use_pts;
685
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 3940 times.
3989 int64_t ts = use_pts ? pkt->pts : pkt->dts;
686 3989 AVRational tb = sti->pub.time_base;
687
688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3989 times.
3989 if (ts == AV_NOPTS_VALUE)
689 ✗ return;
690
691 3989 ts -= sti->lowest_ts_allowed;
692
693 /* Peek into the muxing queue to improve our estimate
694 * of the lowest timestamp if av_interleaved_write_frame() is used. */
695 3989 for (const PacketListEntry *pktl = si->packet_buffer.head;
696
2/2
✓ Branch 0 taken 1229 times.
✓ Branch 1 taken 3989 times.
5218 pktl; pktl = pktl->next) {
697 1229 AVRational cmp_tb = s->streams[pktl->pkt.stream_index]->time_base;
698
2/2
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 1068 times.
1229 int64_t cmp_ts = use_pts ? pktl->pkt.pts : pktl->pkt.dts;
699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1229 times.
1229 if (cmp_ts == AV_NOPTS_VALUE)
700 ✗ continue;
701 1229 cmp_ts -= ffstream(s->streams[pktl->pkt.stream_index])->lowest_ts_allowed;
702
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1228 times.
1229 if (s->output_ts_offset)
703 1 cmp_ts += av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, cmp_tb);
704
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1228 times.
1229 if (av_compare_ts(cmp_ts, cmp_tb, ts, tb) < 0) {
705 1 ts = cmp_ts;
706 1 tb = cmp_tb;
707 }
708 }
709
710
4/4
✓ Branch 0 taken 3899 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 3789 times.
3989 if (ts < 0 ||
711
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 109 times.
110 ts > 0 && s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO) {
712
2/2
✓ Branch 0 taken 213 times.
✓ Branch 1 taken 91 times.
304 for (unsigned i = 0; i < s->nb_streams; i++) {
713 213 AVStream *const st2 = s->streams[i];
714 213 FFStream *const sti2 = ffstream(st2);
715 213 sti2->mux_ts_offset = av_rescale_q_rnd(-ts, tb,
716 st2->time_base,
717 AV_ROUND_UP);
718 }
719 }
720 3989 si->avoid_negative_ts_status = AVOID_NEGATIVE_TS_KNOWN;
721 }
722
723 173069 offset = sti->mux_ts_offset;
724
725
1/2
✓ Branch 0 taken 173069 times.
✗ Branch 1 not taken.
173069 if (pkt->dts != AV_NOPTS_VALUE)
726 173069 pkt->dts += offset;
727
2/2
✓ Branch 0 taken 172978 times.
✓ Branch 1 taken 91 times.
173069 if (pkt->pts != AV_NOPTS_VALUE)
728 172978 pkt->pts += offset;
729
730
2/2
✓ Branch 0 taken 5583 times.
✓ Branch 1 taken 167486 times.
173069 if (si->avoid_negative_ts_use_pts) {
731
2/4
✓ Branch 0 taken 5583 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5583 times.
5583 if (pkt->pts != AV_NOPTS_VALUE && pkt->pts < sti->lowest_ts_allowed) {
732 ✗ av_log(s, AV_LOG_WARNING, "failed to avoid negative "
733 "pts %s in stream %d.\n"
734 "Try -avoid_negative_ts 1 as a possible workaround.\n",
735 ✗ av_ts2str(pkt->pts),
736 pkt->stream_index
737 );
738 }
739 } else {
740
2/4
✓ Branch 0 taken 167486 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 167486 times.
167486 if (pkt->dts != AV_NOPTS_VALUE && pkt->dts < sti->lowest_ts_allowed) {
741 ✗ av_log(s, AV_LOG_WARNING,
742 "Packets poorly interleaved, failed to avoid negative "
743 "timestamp %s in stream %d.\n"
744 "Try -max_interleave_delta 0 as a possible workaround.\n",
745 ✗ av_ts2str(pkt->dts),
746 pkt->stream_index
747 );
748 }
749 }
750 }
751
752 /**
753 * Shift timestamps and call muxer; the original pts/dts are not kept.
754 *
755 * FIXME: this function should NEVER get undefined pts/dts beside when the
756 * AVFMT_NOTIMESTAMPS is set.
757 * Those additional safety checks should be dropped once the correct checks
758 * are set in the callers.
759 */
760 633104 static int write_packet(AVFormatContext *s, AVPacket *pkt)
761 {
762 633104 FFFormatContext *const si = ffformatcontext(s);
763 633104 AVStream *const st = s->streams[pkt->stream_index];
764 633104 FFStream *const sti = ffstream(st);
765 int ret;
766
767 // If the timestamp offsetting below is adjusted, adjust
768 // ff_interleaved_peek similarly.
769
2/2
✓ Branch 0 taken 804 times.
✓ Branch 1 taken 632300 times.
633104 if (s->output_ts_offset) {
770 804 int64_t offset = av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base);
771
772
1/2
✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
804 if (pkt->dts != AV_NOPTS_VALUE)
773 804 pkt->dts += offset;
774
1/2
✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
804 if (pkt->pts != AV_NOPTS_VALUE)
775 804 pkt->pts += offset;
776 }
777 633104 handle_avoid_negative_ts(si, sti, pkt);
778
779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 633104 times.
633104 if ((pkt->flags & AV_PKT_FLAG_UNCODED_FRAME)) {
780 ✗ AVFrame **frame = (AVFrame **)pkt->data;
781 ✗ av_assert0(pkt->size == sizeof(*frame));
782 ✗ ret = ffofmt(s->oformat)->write_uncoded_frame(s, pkt->stream_index, frame, 0);
783 } else {
784 633104 ret = ffofmt(s->oformat)->write_packet(s, pkt);
785 }
786
787
3/4
✓ Branch 0 taken 618215 times.
✓ Branch 1 taken 14889 times.
✓ Branch 2 taken 618215 times.
✗ Branch 3 not taken.
633104 if (s->pb && ret >= 0) {
788 618215 flush_if_needed(s);
789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 618215 times.
618215 if (s->pb->error < 0)
790 ✗ ret = s->pb->error;
791 }
792
793
2/2
✓ Branch 0 taken 633059 times.
✓ Branch 1 taken 45 times.
633104 if (ret >= 0)
794 633059 st->nb_frames++;
795
796 633104 return ret;
797 }
798
799 632990 static int check_packet(AVFormatContext *s, AVPacket *pkt)
800 {
801
2/4
✓ Branch 0 taken 632990 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 632990 times.
632990 if (pkt->stream_index < 0 || pkt->stream_index >= s->nb_streams) {
802 ✗ av_log(s, AV_LOG_ERROR, "Invalid packet stream index: %d\n",
803 pkt->stream_index);
804 ✗ return AVERROR(EINVAL);
805 }
806
807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632990 times.
632990 if (s->streams[pkt->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
808 ✗ av_log(s, AV_LOG_ERROR, "Received a packet for an attachment stream.\n");
809 ✗ return AVERROR(EINVAL);
810 }
811
812 632990 return 0;
813 }
814
815 632990 static int prepare_input_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
816 {
817 632990 FFStream *const sti = ffstream(st);
818 #if !FF_API_COMPUTE_PKT_FIELDS2
819 /* sanitize the timestamps */
820 if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
821
822 /* when there is no reordering (so dts is equal to pts), but
823 * only one of them is set, set the other as well */
824 if (!sti->reorder) {
825 if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE)
826 pkt->pts = pkt->dts;
827 if (pkt->dts == AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
828 pkt->dts = pkt->pts;
829 }
830
831 /* check that the timestamps are set */
832 if (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE) {
833 av_log(s, AV_LOG_ERROR,
834 "Timestamps are unset in a packet for stream %d\n", st->index);
835 return AVERROR(EINVAL);
836 }
837
838 /* check that the dts are increasing (or at least non-decreasing,
839 * if the format allows it */
840 if (sti->cur_dts != AV_NOPTS_VALUE &&
841 ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) && sti->cur_dts >= pkt->dts) ||
842 sti->cur_dts > pkt->dts)) {
843 av_log(s, AV_LOG_ERROR,
844 "Application provided invalid, non monotonically increasing "
845 "dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
846 st->index, sti->cur_dts, pkt->dts);
847 return AVERROR(EINVAL);
848 }
849
850 if (pkt->pts < pkt->dts) {
851 av_log(s, AV_LOG_ERROR, "pts %" PRId64 " < dts %" PRId64 " in stream %d\n",
852 pkt->pts, pkt->dts, st->index);
853 return AVERROR(EINVAL);
854 }
855 }
856 #endif
857 /* update flags */
858
2/2
✓ Branch 0 taken 572992 times.
✓ Branch 1 taken 59998 times.
632990 if (sti->is_intra_only)
859 572992 pkt->flags |= AV_PKT_FLAG_KEY;
860
861
4/4
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 632909 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 77 times.
632990 if (!pkt->data && !pkt->side_data_elems) {
862 /* Such empty packets signal EOS for the BSF API; so sanitize
863 * the packet by allocating data of size 0 (+ padding). */
864 4 av_buffer_unref(&pkt->buf);
865 4 return av_packet_make_refcounted(pkt);
866 }
867
868 632986 return 0;
869 }
870
871 #define CHUNK_START 0x1000
872
873 36739 int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
874 int (*compare)(AVFormatContext *, const AVPacket *, const AVPacket *))
875 {
876 int ret;
877 36739 FFFormatContext *const si = ffformatcontext(s);
878 PacketListEntry **next_point, *this_pktl;
879 36739 AVStream *st = s->streams[pkt->stream_index];
880 36739 FFStream *const sti = ffstream(st);
881
2/4
✓ Branch 0 taken 36739 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36739 times.
36739 int chunked = s->max_chunk_size || s->max_chunk_duration;
882
883 36739 this_pktl = av_malloc(sizeof(*this_pktl));
884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36739 times.
36739 if (!this_pktl) {
885 ✗ av_packet_unref(pkt);
886 ✗ return AVERROR(ENOMEM);
887 }
888
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 36739 times.
36739 if ((ret = av_packet_make_refcounted(pkt)) < 0) {
889 ✗ av_free(this_pktl);
890 ✗ av_packet_unref(pkt);
891 ✗ return ret;
892 }
893
894 36739 av_packet_move_ref(&this_pktl->pkt, pkt);
895 36739 pkt = &this_pktl->pkt;
896
897
2/2
✓ Branch 0 taken 18045 times.
✓ Branch 1 taken 18694 times.
36739 if (sti->last_in_packet_buffer) {
898 18045 next_point = &(sti->last_in_packet_buffer->next);
899 } else {
900 18694 next_point = &si->packet_buffer.head;
901 }
902
903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36739 times.
36739 if (chunked) {
904 ✗ uint64_t max= av_rescale_q_rnd(s->max_chunk_duration, AV_TIME_BASE_Q, st->time_base, AV_ROUND_UP);
905 ✗ sti->interleaver_chunk_size += pkt->size;
906 ✗ sti->interleaver_chunk_duration += pkt->duration;
907 ✗ if ( (s->max_chunk_size && sti->interleaver_chunk_size > s->max_chunk_size)
908 ✗ || (max && sti->interleaver_chunk_duration > max)) {
909 ✗ sti->interleaver_chunk_size = 0;
910 ✗ pkt->flags |= CHUNK_START;
911 ✗ if (max && sti->interleaver_chunk_duration > max) {
912 ✗ int64_t syncoffset = (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)*max/2;
913 ✗ int64_t syncto = av_rescale(pkt->dts + syncoffset, 1, max)*max - syncoffset;
914
915 ✗ sti->interleaver_chunk_duration += (pkt->dts - syncto)/8 - max;
916 } else
917 ✗ sti->interleaver_chunk_duration = 0;
918 }
919 }
920
2/2
✓ Branch 0 taken 22085 times.
✓ Branch 1 taken 14654 times.
36739 if (*next_point) {
921
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 22085 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
22085 if (chunked && !(pkt->flags & CHUNK_START))
922 ✗ goto next_non_null;
923
924
2/2
✓ Branch 1 taken 10380 times.
✓ Branch 2 taken 11705 times.
22085 if (compare(s, &si->packet_buffer.tail->pkt, pkt)) {
925 10380 while ( *next_point
926
2/6
✓ Branch 0 taken 197631 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 197631 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
197631 && ((chunked && !((*next_point)->pkt.flags&CHUNK_START))
927
2/2
✓ Branch 1 taken 187251 times.
✓ Branch 2 taken 10380 times.
197631 || !compare(s, &(*next_point)->pkt, pkt)))
928 187251 next_point = &(*next_point)->next;
929
1/2
✓ Branch 0 taken 10380 times.
✗ Branch 1 not taken.
10380 if (*next_point)
930 10380 goto next_non_null;
931 } else {
932 11705 next_point = &(si->packet_buffer.tail->next);
933 }
934 }
935 av_assert1(!*next_point);
936
937 26359 si->packet_buffer.tail = this_pktl;
938 36739 next_non_null:
939
940 36739 this_pktl->next = *next_point;
941
942 36739 sti->last_in_packet_buffer = *next_point = this_pktl;
943
944 36739 return 0;
945 }
946
947 219037 static int interleave_compare_dts(AVFormatContext *s, const AVPacket *next,
948 const AVPacket *pkt)
949 {
950 219037 AVStream *st = s->streams[pkt->stream_index];
951 219037 AVStream *st2 = s->streams[next->stream_index];
952 219037 int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
953 st->time_base);
954
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 219037 times.
219037 if (s->audio_preload) {
955 ✗ int preload = st ->codecpar->codec_type == AVMEDIA_TYPE_AUDIO;
956 ✗ int preload2 = st2->codecpar->codec_type == AVMEDIA_TYPE_AUDIO;
957 ✗ if (preload != preload2) {
958 int64_t ts, ts2;
959 ✗ preload *= s->audio_preload;
960 ✗ preload2 *= s->audio_preload;
961 ✗ ts = av_rescale_q(pkt ->dts, st ->time_base, AV_TIME_BASE_Q) - preload;
962 ✗ ts2= av_rescale_q(next->dts, st2->time_base, AV_TIME_BASE_Q) - preload2;
963 ✗ if (ts == ts2) {
964 ✗ ts = ((uint64_t)pkt ->dts*st ->time_base.num*AV_TIME_BASE - (uint64_t)preload *st ->time_base.den)*st2->time_base.den
965 ✗ - ((uint64_t)next->dts*st2->time_base.num*AV_TIME_BASE - (uint64_t)preload2*st2->time_base.den)*st ->time_base.den;
966 ✗ ts2 = 0;
967 }
968 ✗ comp = (ts2 > ts) - (ts2 < ts);
969 }
970 }
971
972
2/2
✓ Branch 0 taken 100860 times.
✓ Branch 1 taken 118177 times.
219037 if (comp == 0)
973 100860 return pkt->stream_index < next->stream_index;
974 118177 return comp > 0;
975 }
976
977 72715 int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *pkt,
978 int flush, int has_packet)
979 {
980 72715 FormatContextInternal *const fci = ff_fc_internal(s);
981 72715 FFFormatContext *const si = &fci->fc;
982 72715 int stream_count = 0;
983 72715 int noninterleaved_count = 0;
984 int ret;
985
986
2/2
✓ Branch 0 taken 36108 times.
✓ Branch 1 taken 36607 times.
72715 if (has_packet) {
987
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 36108 times.
36108 if ((ret = ff_interleave_add_packet(s, pkt, interleave_compare_dts)) < 0)
988 ✗ return ret;
989 }
990
991
2/2
✓ Branch 0 taken 856212 times.
✓ Branch 1 taken 72715 times.
928927 for (unsigned i = 0; i < s->nb_streams; i++) {
992 856212 const AVStream *const st = s->streams[i];
993 856212 const FFStream *const sti = cffstream(st);
994 856212 const AVCodecParameters *const par = st->codecpar;
995
2/2
✓ Branch 0 taken 796702 times.
✓ Branch 1 taken 59510 times.
856212 if (sti->last_in_packet_buffer) {
996 796702 ++stream_count;
997
2/2
✓ Branch 0 taken 59381 times.
✓ Branch 1 taken 129 times.
59510 } else if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT &&
998
2/2
✓ Branch 0 taken 59010 times.
✓ Branch 1 taken 371 times.
59381 par->codec_id != AV_CODEC_ID_VP8 &&
999
2/2
✓ Branch 0 taken 58934 times.
✓ Branch 1 taken 76 times.
59010 par->codec_id != AV_CODEC_ID_VP9 &&
1000
1/2
✓ Branch 0 taken 58934 times.
✗ Branch 1 not taken.
58934 par->codec_id != AV_CODEC_ID_SMPTE_2038) {
1001 58934 ++noninterleaved_count;
1002 }
1003 }
1004
1005
2/2
✓ Branch 0 taken 31843 times.
✓ Branch 1 taken 40872 times.
72715 if (fci->nb_interleaved_streams == stream_count)
1006 31843 flush = 1;
1007
1008
2/2
✓ Branch 0 taken 72586 times.
✓ Branch 1 taken 129 times.
72715 if (s->max_interleave_delta > 0 &&
1009
2/2
✓ Branch 0 taken 72260 times.
✓ Branch 1 taken 326 times.
72586 si->packet_buffer.head &&
1010
3/4
✓ Branch 0 taken 72260 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37192 times.
✓ Branch 3 taken 35068 times.
72260 si->packet_buffer.head->pkt.dts != AV_NOPTS_VALUE &&
1011 37192 !flush &&
1012
2/2
✓ Branch 0 taken 36781 times.
✓ Branch 1 taken 411 times.
37192 fci->nb_interleaved_streams == stream_count+noninterleaved_count
1013 ) {
1014 36781 AVPacket *const top_pkt = &si->packet_buffer.head->pkt;
1015 36781 int64_t delta_dts = INT64_MIN;
1016 36781 int64_t top_dts = av_rescale_q(top_pkt->dts,
1017 36781 s->streams[top_pkt->stream_index]->time_base,
1018 36781 AV_TIME_BASE_Q);
1019
1020
2/2
✓ Branch 0 taken 429134 times.
✓ Branch 1 taken 36781 times.
465915 for (unsigned i = 0; i < s->nb_streams; i++) {
1021 429134 const AVStream *const st = s->streams[i];
1022 429134 const FFStream *const sti = cffstream(st);
1023 429134 const PacketListEntry *const last = sti->last_in_packet_buffer;
1024 int64_t last_dts;
1025
1026
4/4
✓ Branch 0 taken 383007 times.
✓ Branch 1 taken 46127 times.
✓ Branch 2 taken 1892 times.
✓ Branch 3 taken 381115 times.
429134 if (!last || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
1027 48019 continue;
1028
1029 381115 last_dts = av_rescale_q(last->pkt.dts,
1030 st->time_base,
1031 381115 AV_TIME_BASE_Q);
1032 381115 delta_dts = FFMAX(delta_dts, last_dts - top_dts);
1033 }
1034
1035
2/2
✓ Branch 0 taken 1062 times.
✓ Branch 1 taken 35719 times.
36781 if (delta_dts > s->max_interleave_delta) {
1036 1062 av_log(s, AV_LOG_DEBUG,
1037 "Delay between the first packet and last packet in the "
1038 "muxing queue is %"PRId64" > %"PRId64": forcing output\n",
1039 delta_dts, s->max_interleave_delta);
1040 1062 flush = 1;
1041 }
1042 }
1043
1044
4/4
✓ Branch 0 taken 72388 times.
✓ Branch 1 taken 327 times.
✓ Branch 2 taken 36194 times.
✓ Branch 3 taken 36194 times.
72715 if (stream_count && flush) {
1045 36194 PacketListEntry *pktl = si->packet_buffer.head;
1046 36194 AVStream *const st = s->streams[pktl->pkt.stream_index];
1047 36194 FFStream *const sti = ffstream(st);
1048
1049
2/2
✓ Branch 0 taken 18351 times.
✓ Branch 1 taken 17843 times.
36194 if (sti->last_in_packet_buffer == pktl)
1050 18351 sti->last_in_packet_buffer = NULL;
1051 36194 ff_packet_list_get(&si->packet_buffer, pkt);
1052
1053 36194 return 1;
1054 } else {
1055 36521 return 0;
1056 }
1057 }
1058
1059 1167089 int ff_interleave_packet_passthrough(AVFormatContext *s, AVPacket *pkt,
1060 int flush, int has_packet)
1061 {
1062 1167089 return has_packet;
1063 }
1064
1065 13 int ff_get_muxer_ts_offset(AVFormatContext *s, int stream_index, int64_t *offset)
1066 {
1067 AVStream *st;
1068
1069
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
13 if (stream_index < 0 || stream_index >= s->nb_streams)
1070 ✗ return AVERROR(EINVAL);
1071
1072 13 st = s->streams[stream_index];
1073 13 *offset = ffstream(st)->mux_ts_offset;
1074
1075
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (s->output_ts_offset)
1076 ✗ *offset += av_rescale_q(s->output_ts_offset, AV_TIME_BASE_Q, st->time_base);
1077
1078 13 return 0;
1079 }
1080
1081 577 const AVPacket *ff_interleaved_peek(AVFormatContext *s, int stream)
1082 {
1083 577 FFFormatContext *const si = ffformatcontext(s);
1084 577 PacketListEntry *pktl = si->packet_buffer.head;
1085
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 571 times.
635 while (pktl) {
1086
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 58 times.
64 if (pktl->pkt.stream_index == stream) {
1087 6 return &pktl->pkt;
1088 }
1089 58 pktl = pktl->next;
1090 }
1091 571 return NULL;
1092 }
1093
1094 632990 static int check_bitstream(AVFormatContext *s, FFStream *sti, AVPacket *pkt)
1095 {
1096 int ret;
1097
1098
2/2
✓ Branch 0 taken 10718 times.
✓ Branch 1 taken 622272 times.
632990 if (!(s->flags & AVFMT_FLAG_AUTO_BSF))
1099 10718 return 1;
1100
1101
2/2
✓ Branch 1 taken 45470 times.
✓ Branch 2 taken 576802 times.
622272 if (ffofmt(s->oformat)->check_bitstream) {
1102
2/2
✓ Branch 0 taken 525 times.
✓ Branch 1 taken 44945 times.
45470 if (!sti->bitstream_checked) {
1103
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 525 times.
525 if ((ret = ffofmt(s->oformat)->check_bitstream(s, &sti->pub, pkt)) < 0)
1104 ✗ return ret;
1105
1/2
✓ Branch 0 taken 525 times.
✗ Branch 1 not taken.
525 else if (ret == 1)
1106 525 sti->bitstream_checked = 1;
1107 }
1108 }
1109
1110 622272 return 1;
1111 }
1112
1113 624986 static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt,
1114 int flush, int has_packet)
1115 {
1116 624986 FormatContextInternal *const fci = ff_fc_internal(s);
1117
1118 615912 for (;; ) {
1119 1240898 int ret = fci->interleave_packet(s, pkt, flush, has_packet);
1120
2/2
✓ Branch 0 taken 624986 times.
✓ Branch 1 taken 615912 times.
1240898 if (ret <= 0)
1121 624986 return ret;
1122
1123 615912 has_packet = 0;
1124
1125 615912 ret = write_packet(s, pkt);
1126 615912 av_packet_unref(pkt);
1127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 615912 times.
615912 if (ret < 0)
1128 ✗ return ret;
1129 }
1130 }
1131
1132 633117 static int write_packet_common(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved)
1133 {
1134 int ret;
1135
1136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 633117 times.
633117 if (s->debug & AV_FDEBUG_TS)
1137 ✗ av_log(s, AV_LOG_DEBUG, "%s size:%d dts:%s pts:%s\n", __func__,
1138 ✗ pkt->size, av_ts2str(pkt->dts), av_ts2str(pkt->pts));
1139
1140 633117 guess_pkt_duration(s, st, pkt);
1141
1142 #if FF_API_COMPUTE_PKT_FIELDS2
1143
3/4
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 633074 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 43 times.
633117 if ((ret = compute_muxer_pkt_fields(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
1144 ✗ return ret;
1145 #endif
1146
1147
2/2
✓ Branch 0 taken 615925 times.
✓ Branch 1 taken 17192 times.
633117 if (interleaved) {
1148
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 615925 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
615925 if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
1149 ✗ return AVERROR(EINVAL);
1150 615925 return interleaved_write_packet(s, pkt, 0, 1);
1151 } else {
1152 17192 return write_packet(s, pkt);
1153 }
1154 }
1155
1156 1122 static int write_packets_from_bsfs(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved)
1157 {
1158 1122 FFStream *const sti = ffstream(st);
1159 1122 AVBSFContext *const bsfc = sti->bsfc;
1160 int ret;
1161
1162
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1122 times.
1122 if ((ret = av_bsf_send_packet(bsfc, pkt)) < 0) {
1163 ✗ av_log(s, AV_LOG_ERROR,
1164 "Failed to send packet to filter %s for stream %d\n",
1165 ✗ bsfc->filter->name, st->index);
1166 ✗ return ret;
1167 }
1168
1169 do {
1170 2326 ret = av_bsf_receive_packet(bsfc, pkt);
1171
2/2
✓ Branch 0 taken 1122 times.
✓ Branch 1 taken 1204 times.
2326 if (ret < 0) {
1172
3/4
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 1077 times.
✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
1122 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
1173 1122 return 0;
1174 ✗ av_log(s, AV_LOG_ERROR, "Error applying bitstream filters to an output "
1175 ✗ "packet for stream #%d: %s\n", st->index, av_err2str(ret));
1176 ✗ if (!(s->error_recognition & AV_EF_EXPLODE) && ret != AVERROR(ENOMEM))
1177 ✗ continue;
1178 ✗ return ret;
1179 }
1180 1204 av_packet_rescale_ts(pkt, bsfc->time_base_out, st->time_base);
1181 1204 ret = write_packet_common(s, st, pkt, interleaved);
1182
2/4
✓ Branch 0 taken 1204 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1204 times.
1204 if (ret >= 0 && !interleaved) // a successful write_packet_common already unrefed pkt for interleaved
1183 ✗ av_packet_unref(pkt);
1184
1/2
✓ Branch 0 taken 1204 times.
✗ Branch 1 not taken.
1204 } while (ret >= 0);
1185
1186 ✗ return ret;
1187 }
1188
1189 632990 static int write_packets_common(AVFormatContext *s, AVPacket *pkt, int interleaved)
1190 {
1191 AVStream *st;
1192 FFStream *sti;
1193 632990 int ret = check_packet(s, pkt);
1194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632990 times.
632990 if (ret < 0)
1195 ✗ return ret;
1196 632990 st = s->streams[pkt->stream_index];
1197 632990 sti = ffstream(st);
1198
1199 632990 ret = prepare_input_packet(s, st, pkt);
1200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632990 times.
632990 if (ret < 0)
1201 ✗ return ret;
1202
1203 632990 ret = check_bitstream(s, sti, pkt);
1204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632990 times.
632990 if (ret < 0)
1205 ✗ return ret;
1206
1207
2/2
✓ Branch 0 taken 1077 times.
✓ Branch 1 taken 631913 times.
632990 if (sti->bsfc) {
1208 1077 return write_packets_from_bsfs(s, st, pkt, interleaved);
1209 } else {
1210 631913 return write_packet_common(s, st, pkt, interleaved);
1211 }
1212 }
1213
1214 17431 int av_write_frame(AVFormatContext *s, AVPacket *in)
1215 {
1216 17431 FFFormatContext *const si = ffformatcontext(s);
1217 17431 AVPacket *pkt = si->parse_pkt;
1218 int ret;
1219
1220
2/2
✓ Branch 0 taken 239 times.
✓ Branch 1 taken 17192 times.
17431 if (!in) {
1221
1/2
✓ Branch 1 taken 239 times.
✗ Branch 2 not taken.
239 if (ffofmt(s->oformat)->flags_internal & FF_OFMT_FLAG_ALLOW_FLUSH) {
1222 239 ret = ffofmt(s->oformat)->write_packet(s, NULL);
1223 239 flush_if_needed(s);
1224
4/6
✓ Branch 0 taken 239 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 233 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 233 times.
239 if (ret >= 0 && s->pb && s->pb->error < 0)
1225 ✗ ret = s->pb->error;
1226 239 return ret;
1227 }
1228 ✗ return 1;
1229 }
1230
1231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17192 times.
17192 if (in->flags & AV_PKT_FLAG_UNCODED_FRAME) {
1232 ✗ pkt = in;
1233 } else {
1234 /* We don't own in, so we have to make sure not to modify it.
1235 * (ff_write_chained() relies on this fact.)
1236 * The following avoids copying in's data unnecessarily.
1237 * Copying side data is unavoidable as a bitstream filter
1238 * may change it, e.g. free it on errors. */
1239 17192 pkt->data = in->data;
1240 17192 pkt->size = in->size;
1241 17192 ret = av_packet_copy_props(pkt, in);
1242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17192 times.
17192 if (ret < 0)
1243 ✗ return ret;
1244
2/2
✓ Branch 0 taken 12503 times.
✓ Branch 1 taken 4689 times.
17192 if (in->buf) {
1245 12503 pkt->buf = av_buffer_ref(in->buf);
1246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12503 times.
12503 if (!pkt->buf) {
1247 ✗ ret = AVERROR(ENOMEM);
1248 ✗ goto fail;
1249 }
1250 }
1251 }
1252
1253 17192 ret = write_packets_common(s, pkt, 0/*non-interleaved*/);
1254
1255 17192 fail:
1256 // Uncoded frames using the noninterleaved codepath are also freed here
1257 17192 av_packet_unref(pkt);
1258 17192 return ret;
1259 }
1260
1261 615798 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
1262 {
1263 int ret;
1264
1265
1/2
✓ Branch 0 taken 615798 times.
✗ Branch 1 not taken.
615798 if (pkt) {
1266 615798 ret = write_packets_common(s, pkt, 1/*interleaved*/);
1267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 615798 times.
615798 if (ret < 0)
1268 ✗ av_packet_unref(pkt);
1269 615798 return ret;
1270 } else {
1271 ✗ av_log(s, AV_LOG_TRACE, "av_interleaved_write_frame FLUSH\n");
1272 ✗ return interleaved_write_packet(s, ffformatcontext(s)->parse_pkt, 1/*flush*/, 0);
1273 }
1274 }
1275
1276 9061 int av_write_trailer(AVFormatContext *s)
1277 {
1278 9061 FFFormatContext *const si = ffformatcontext(s);
1279 9061 AVPacket *const pkt = si->parse_pkt;
1280 9061 int ret1, ret = 0;
1281
1282
2/2
✓ Branch 0 taken 9849 times.
✓ Branch 1 taken 9061 times.
18910 for (unsigned i = 0; i < s->nb_streams; i++) {
1283 9849 AVStream *const st = s->streams[i];
1284 9849 FFStream *const sti = ffstream(st);
1285
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 9804 times.
9849 if (sti->bsfc) {
1286 45 ret1 = write_packets_from_bsfs(s, st, pkt, 1/*interleaved*/);
1287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (ret1 < 0)
1288 ✗ av_packet_unref(pkt);
1289
1/2
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
45 if (ret >= 0)
1290 45 ret = ret1;
1291 }
1292 }
1293 9061 ret1 = interleaved_write_packet(s, pkt, 1, 0);
1294
1/2
✓ Branch 0 taken 9061 times.
✗ Branch 1 not taken.
9061 if (ret >= 0)
1295 9061 ret = ret1;
1296
1297
2/2
✓ Branch 1 taken 4518 times.
✓ Branch 2 taken 4543 times.
9061 if (ffofmt(s->oformat)->write_trailer) {
1298
3/4
✓ Branch 0 taken 4434 times.
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 4434 times.
✗ Branch 3 not taken.
4518 if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
1299 4434 avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_TRAILER);
1300 4518 ret1 = ffofmt(s->oformat)->write_trailer(s);
1301
1/2
✓ Branch 0 taken 4518 times.
✗ Branch 1 not taken.
4518 if (ret >= 0)
1302 4518 ret = ret1;
1303 }
1304
1305 9061 deinit_muxer(s);
1306
1307
2/2
✓ Branch 0 taken 8867 times.
✓ Branch 1 taken 194 times.
9061 if (s->pb)
1308 8867 avio_flush(s->pb);
1309
4/6
✓ Branch 0 taken 9061 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8867 times.
✓ Branch 3 taken 194 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 8867 times.
9061 if (ret >= 0 && s->pb && s->pb->error < 0)
1310 ✗ ret = s->pb->error;
1311
2/2
✓ Branch 0 taken 9849 times.
✓ Branch 1 taken 9061 times.
18910 for (unsigned i = 0; i < s->nb_streams; i++) {
1312 9849 av_freep(&s->streams[i]->priv_data);
1313 9849 av_freep(&ffstream(s->streams[i])->index_entries);
1314 }
1315
2/2
✓ Branch 0 taken 4623 times.
✓ Branch 1 taken 4438 times.
9061 if (s->oformat->priv_class)
1316 4623 av_opt_free(s->priv_data);
1317 9061 av_freep(&s->priv_data);
1318 9061 av_packet_unref(si->pkt);
1319 9061 return ret;
1320 }
1321
1322 ✗ int av_get_output_timestamp(struct AVFormatContext *s, int stream,
1323 int64_t *dts, int64_t *wall)
1324 {
1325 ✗ const FFOutputFormat *const of = ffofmt(s->oformat);
1326 ✗ if (!of || !of->get_output_timestamp)
1327 ✗ return AVERROR(ENOSYS);
1328 ✗ of->get_output_timestamp(s, stream, dts, wall);
1329 ✗ return 0;
1330 }
1331
1332 45 int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
1333 {
1334 int ret;
1335 const AVBitStreamFilter *bsf;
1336 45 FFStream *const sti = ffstream(st);
1337 AVBSFContext *bsfc;
1338
1339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 av_assert0(!sti->bsfc);
1340
1341
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
45 if (!(bsf = av_bsf_get_by_name(name))) {
1342 ✗ av_log(NULL, AV_LOG_ERROR, "Unknown bitstream filter '%s'\n", name);
1343 ✗ return AVERROR_BSF_NOT_FOUND;
1344 }
1345
1346
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
45 if ((ret = av_bsf_alloc(bsf, &bsfc)) < 0)
1347 ✗ return ret;
1348
1349 45 bsfc->time_base_in = st->time_base;
1350
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
45 if ((ret = avcodec_parameters_copy(bsfc->par_in, st->codecpar)) < 0) {
1351 ✗ av_bsf_free(&bsfc);
1352 ✗ return ret;
1353 }
1354
1355
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
45 if (args && bsfc->filter->priv_class) {
1356
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if ((ret = av_set_options_string(bsfc->priv_data, args, "=", ":")) < 0) {
1357 ✗ av_bsf_free(&bsfc);
1358 ✗ return ret;
1359 }
1360 }
1361
1362
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
45 if ((ret = av_bsf_init(bsfc)) < 0) {
1363 ✗ av_bsf_free(&bsfc);
1364 ✗ return ret;
1365 }
1366
1367 45 sti->bsfc = bsfc;
1368
1369
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 25 times.
45 av_log(NULL, AV_LOG_VERBOSE,
1370 "Automatically inserted bitstream filter '%s'; args='%s'\n",
1371 name, args ? args : "");
1372 45 return 1;
1373 }
1374
1375 12173 int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
1376 AVFormatContext *src, int interleave)
1377 {
1378 12173 int64_t pts = pkt->pts, dts = pkt->dts, duration = pkt->duration;
1379 12173 int stream_index = pkt->stream_index;
1380 12173 AVRational time_base = pkt->time_base;
1381 int ret;
1382
1383 12173 pkt->stream_index = dst_stream;
1384
1385 12173 av_packet_rescale_ts(pkt,
1386 12173 src->streams[stream_index]->time_base,
1387 12173 dst->streams[dst_stream]->time_base);
1388
1389
1/2
✓ Branch 0 taken 12173 times.
✗ Branch 1 not taken.
12173 if (!interleave) {
1390 12173 ret = av_write_frame(dst, pkt);
1391 /* We only have to backup and restore the fields that
1392 * we changed ourselves, because av_write_frame() does not
1393 * modify the packet given to it. */
1394 12173 pkt->pts = pts;
1395 12173 pkt->dts = dts;
1396 12173 pkt->duration = duration;
1397 12173 pkt->stream_index = stream_index;
1398 12173 pkt->time_base = time_base;
1399 } else
1400 ✗ ret = av_interleaved_write_frame(dst, pkt);
1401
1402 12173 return ret;
1403 }
1404
1405 ✗ static void uncoded_frame_free(void *unused, uint8_t *data)
1406 {
1407 ✗ av_frame_free((AVFrame **)data);
1408 ✗ av_free(data);
1409 ✗ }
1410
1411 ✗ static int write_uncoded_frame_internal(AVFormatContext *s, int stream_index,
1412 AVFrame *frame, int interleaved)
1413 {
1414 ✗ FFFormatContext *const si = ffformatcontext(s);
1415 ✗ AVPacket *pkt = si->parse_pkt;
1416
1417 ✗ av_assert0(s->oformat);
1418 ✗ if (!ffofmt(s->oformat)->write_uncoded_frame) {
1419 ✗ av_frame_free(&frame);
1420 ✗ return AVERROR(ENOSYS);
1421 }
1422
1423 ✗ if (!frame) {
1424 ✗ pkt = NULL;
1425 } else {
1426 ✗ size_t bufsize = sizeof(frame) + AV_INPUT_BUFFER_PADDING_SIZE;
1427 ✗ AVFrame **framep = av_mallocz(bufsize);
1428
1429 ✗ if (!framep)
1430 ✗ goto fail;
1431 ✗ pkt->buf = av_buffer_create((void *)framep, bufsize,
1432 uncoded_frame_free, NULL, 0);
1433 ✗ if (!pkt->buf) {
1434 ✗ av_free(framep);
1435 ✗ fail:
1436 ✗ av_frame_free(&frame);
1437 ✗ return AVERROR(ENOMEM);
1438 }
1439 ✗ *framep = frame;
1440
1441 ✗ pkt->data = (void *)framep;
1442 ✗ pkt->size = sizeof(frame);
1443 ✗ pkt->pts =
1444 ✗ pkt->dts = frame->pts;
1445 ✗ pkt->duration = frame->duration;
1446 ✗ pkt->stream_index = stream_index;
1447 ✗ pkt->flags |= AV_PKT_FLAG_UNCODED_FRAME;
1448 }
1449
1450 ✗ return interleaved ? av_interleaved_write_frame(s, pkt) :
1451 ✗ av_write_frame(s, pkt);
1452 }
1453
1454 ✗ int av_write_uncoded_frame(AVFormatContext *s, int stream_index,
1455 AVFrame *frame)
1456 {
1457 ✗ return write_uncoded_frame_internal(s, stream_index, frame, 0);
1458 }
1459
1460 ✗ int av_interleaved_write_uncoded_frame(AVFormatContext *s, int stream_index,
1461 AVFrame *frame)
1462 {
1463 ✗ return write_uncoded_frame_internal(s, stream_index, frame, 1);
1464 }
1465
1466 ✗ int av_write_uncoded_frame_query(AVFormatContext *s, int stream_index)
1467 {
1468 ✗ const FFOutputFormat *const of = ffofmt(s->oformat);
1469 ✗ av_assert0(of);
1470 ✗ if (!of->write_uncoded_frame)
1471 ✗ return AVERROR(ENOSYS);
1472 ✗ return of->write_uncoded_frame(s, stream_index, NULL,
1473 AV_WRITE_UNCODED_FRAME_QUERY);
1474 }
1475