FFmpeg coverage


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