FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mux.c
Date: 2023-12-04 05:51:44
Exec Total Coverage
Lines: 576 836 68.9%
Functions: 32 38 84.2%
Branches: 432 660 65.5%

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