FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mux.c
Date: 2024-05-08 06:34:07
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 7114 static void frac_init(FFFrac *f, int64_t val, int64_t num, int64_t den)
57 {
58 7114 num += (den >> 1);
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7114 times.
7114 if (num >= den) {
60 val += num / den;
61 num = num % den;
62 }
63 7114 f->val = val;
64 7114 f->num = num;
65 7114 f->den = den;
66 7114 }
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 479854 static void frac_add(FFFrac *f, int64_t incr)
75 {
76 int64_t num, den;
77
78 479854 num = f->num + incr;
79 479854 den = f->den;
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 479854 times.
479854 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 463277 times.
✓ Branch 1 taken 16577 times.
479854 } else if (num >= den) {
88 463277 f->val += num / den;
89 463277 num = num % den;
90 }
91 479854 f->num = num;
92 479854 }
93
94 6796 int avformat_alloc_output_context2(AVFormatContext **avctx, const AVOutputFormat *oformat,
95 const char *format, const char *filename)
96 {
97 6796 AVFormatContext *s = avformat_alloc_context();
98 6796 int ret = 0;
99
100 6796 *avctx = NULL;
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6796 times.
6796 if (!s)
102 goto nomem;
103
104
2/2
✓ Branch 0 taken 6767 times.
✓ Branch 1 taken 29 times.
6796 if (!oformat) {
105
2/2
✓ Branch 0 taken 6593 times.
✓ Branch 1 taken 174 times.
6767 if (format) {
106 6593 oformat = av_guess_format(format, NULL, NULL);
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6593 times.
6593 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 174 oformat = av_guess_format(NULL, filename, NULL);
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 174 times.
174 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 6796 s->oformat = oformat;
126
2/2
✓ Branch 1 taken 4216 times.
✓ Branch 2 taken 2580 times.
6796 if (ffofmt(s->oformat)->priv_data_size > 0) {
127 4216 s->priv_data = av_mallocz(ffofmt(s->oformat)->priv_data_size);
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4216 times.
4216 if (!s->priv_data)
129 goto nomem;
130
2/2
✓ Branch 0 taken 3928 times.
✓ Branch 1 taken 288 times.
4216 if (s->oformat->priv_class) {
131 3928 *(const AVClass**)s->priv_data= s->oformat->priv_class;
132 3928 av_opt_set_defaults(s->priv_data);
133 }
134 } else
135 2580 s->priv_data = NULL;
136
137
2/2
✓ Branch 0 taken 6771 times.
✓ Branch 1 taken 25 times.
6796 if (filename) {
138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6771 times.
6771 if (!(s->url = av_strdup(filename)))
139 goto nomem;
140
141 }
142 6796 *avctx = s;
143 6796 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 232727 times.
✓ Branch 1 taken 781 times.
233508 while (avctag->id != AV_CODEC_ID_NONE) {
168
2/2
✓ Branch 1 taken 1969 times.
✓ Branch 2 taken 230758 times.
232727 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 79802 times.
230758 if (avctag->id == st->codecpar->codec_id)
174 150956 tag = avctag->tag;
175 230758 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 6871 static int init_muxer(AVFormatContext *s, AVDictionary **options)
187 {
188 6871 FFFormatContext *const si = ffformatcontext(s);
189 6871 AVDictionary *tmp = NULL;
190 6871 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 6871 unsigned nb_type[FF_ARRAY_ELEMS(default_codec_offsets)] = { 0 };
198 6871 int ret = 0;
199
200
2/2
✓ Branch 0 taken 6869 times.
✓ Branch 1 taken 2 times.
6871 if (options)
201 6869 av_dict_copy(&tmp, *options, 0);
202
203
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6871 times.
6871 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
204 goto fail;
205
6/8
✓ Branch 0 taken 4216 times.
✓ Branch 1 taken 2655 times.
✓ Branch 2 taken 3928 times.
✓ Branch 3 taken 288 times.
✓ Branch 4 taken 3928 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 3928 times.
10799 if (s->priv_data && s->oformat->priv_class && *(const AVClass**)s->priv_data==s->oformat->priv_class &&
206 3928 (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 6839 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
6871 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 6871 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6871 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 7252 times.
✓ Branch 1 taken 6871 times.
14123 for (unsigned i = 0; i < s->nb_streams; i++) {
222 7252 AVStream *const st = s->streams[i];
223 7252 FFStream *const sti = ffstream(st);
224 7252 AVCodecParameters *const par = st->codecpar;
225 const AVCodecDescriptor *desc;
226
227
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7247 times.
7252 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 5558 times.
✓ Branch 2 taken 138 times.
7252 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 5558 case AVMEDIA_TYPE_VIDEO:
248
2/4
✓ Branch 0 taken 5558 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5558 times.
5558 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 5558 times.
5558 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 5558 break;
271 }
272
2/2
✓ Branch 0 taken 1079 times.
✓ Branch 1 taken 6173 times.
7252 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 7252 times.
7252 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 7252 desc = avcodec_descriptor_get(par->codec_id);
319
4/4
✓ Branch 0 taken 7189 times.
✓ Branch 1 taken 63 times.
✓ Branch 2 taken 353 times.
✓ Branch 3 taken 6836 times.
7252 if (desc && desc->props & AV_CODEC_PROP_REORDER)
320 353 sti->reorder = 1;
321
322 7252 sti->is_intra_only = ff_is_intra_only(par->codec_id);
323
324
2/2
✓ Branch 0 taken 3586 times.
✓ Branch 1 taken 3666 times.
7252 if (of->p.codec_tag) {
325
2/2
✓ Branch 0 taken 1998 times.
✓ Branch 1 taken 1588 times.
3586 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 1614 times.
3586 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 1614 par->codec_tag = av_codec_get_tag(of->p.codec_tag, par->codec_id);
345 }
346
347
2/2
✓ Branch 0 taken 7251 times.
✓ Branch 1 taken 1 times.
7252 if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT &&
348
1/2
✓ Branch 0 taken 7251 times.
✗ Branch 1 not taken.
7251 par->codec_id != AV_CODEC_ID_SMPTE_2038)
349 7251 si->nb_interleaved_streams++;
350 }
351 6871 si->interleave_packet = of->interleave_packet;
352
2/2
✓ Branch 0 taken 6835 times.
✓ Branch 1 taken 36 times.
6871 if (!si->interleave_packet)
353
2/2
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 6552 times.
6835 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 4216 times.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 2580 times.
6871 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 1044 times.
✓ Branch 1 taken 5827 times.
6871 if (!(s->flags & AVFMT_FLAG_BITEXACT)) {
373 1044 av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
374 } else {
375 5827 av_dict_set(&s->metadata, "encoder", NULL, 0);
376 }
377
378
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 6871 times.
6875 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 6869 times.
✓ Branch 1 taken 2 times.
6871 if (options) {
383 6869 av_dict_free(options);
384 6869 *options = tmp;
385 }
386
387
2/2
✓ Branch 0 taken 1184 times.
✓ Branch 1 taken 5687 times.
6871 if (of->init) {
388
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1184 times.
1184 if ((ret = of->init(s)) < 0) {
389 if (of->deinit)
390 of->deinit(s);
391 return ret;
392 }
393 1184 return ret == 0;
394 }
395
396 5687 return 0;
397
398 fail:
399 av_dict_free(&tmp);
400 return ret;
401 }
402
403 6871 static int init_pts(AVFormatContext *s)
404 {
405 6871 FFFormatContext *const si = ffformatcontext(s);
406
407 /* init PTS generation */
408
2/2
✓ Branch 0 taken 7252 times.
✓ Branch 1 taken 6871 times.
14123 for (unsigned i = 0; i < s->nb_streams; i++) {
409 7252 AVStream *const st = s->streams[i];
410 7252 FFStream *const sti = ffstream(st);
411 7252 int64_t den = AV_NOPTS_VALUE;
412
413
3/3
✓ Branch 0 taken 1556 times.
✓ Branch 1 taken 5558 times.
✓ Branch 2 taken 138 times.
7252 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 5558 case AVMEDIA_TYPE_VIDEO:
418 5558 den = (int64_t)st->time_base.num * st->time_base.den;
419 5558 break;
420 138 default:
421 138 break;
422 }
423
424
2/2
✓ Branch 0 taken 7114 times.
✓ Branch 1 taken 138 times.
7252 if (den != AV_NOPTS_VALUE) {
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7114 times.
7114 if (den <= 0)
426 return AVERROR_INVALIDDATA;
427
428 7114 frac_init(&sti->priv_pts, 0, 0, den);
429 }
430 }
431
432 6871 si->avoid_negative_ts_status = AVOID_NEGATIVE_TS_UNKNOWN;
433
2/2
✓ Branch 0 taken 4391 times.
✓ Branch 1 taken 2480 times.
6871 if (s->avoid_negative_ts < 0) {
434 av_assert2(s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO);
435
2/2
✓ Branch 0 taken 3444 times.
✓ Branch 1 taken 947 times.
4391 if (s->oformat->flags & (AVFMT_TS_NEGATIVE | AVFMT_NOTIMESTAMPS)) {
436 3444 s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_DISABLED;
437 3444 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 6871 return 0;
444 }
445
446 478384 static void flush_if_needed(AVFormatContext *s)
447 {
448
3/4
✓ Branch 0 taken 478250 times.
✓ Branch 1 taken 134 times.
✓ Branch 2 taken 478250 times.
✗ Branch 3 not taken.
478384 if (s->pb && s->pb->error >= 0) {
449
2/4
✓ Branch 0 taken 478250 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 478250 times.
478250 if (s->flush_packets == 1 || s->flags & AVFMT_FLAG_FLUSH_PACKETS)
450 avio_flush(s->pb);
451
2/4
✓ Branch 0 taken 478250 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 478250 times.
✗ Branch 3 not taken.
478250 else if (s->flush_packets && !(s->oformat->flags & AVFMT_NOFILE))
452 478250 avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_FLUSH_POINT);
453 }
454 478384 }
455
456 6871 static void deinit_muxer(AVFormatContext *s)
457 {
458 6871 FFFormatContext *const si = ffformatcontext(s);
459 6871 const FFOutputFormat *const of = ffofmt(s->oformat);
460
4/6
✓ Branch 0 taken 6871 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3932 times.
✓ Branch 3 taken 2939 times.
✓ Branch 4 taken 3932 times.
✗ Branch 5 not taken.
6871 if (of && of->deinit && si->initialized)
461 3932 of->deinit(s);
462 6871 si->initialized =
463 6871 si->streams_initialized = 0;
464 6871 }
465
466 6871 int avformat_init_output(AVFormatContext *s, AVDictionary **options)
467 {
468 6871 FFFormatContext *const si = ffformatcontext(s);
469 6871 int ret = 0;
470
471
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6871 times.
6871 if ((ret = init_muxer(s, options)) < 0)
472 return ret;
473
474 6871 si->initialized = 1;
475 6871 si->streams_initialized = ret;
476
477
4/4
✓ Branch 1 taken 1184 times.
✓ Branch 2 taken 5687 times.
✓ Branch 3 taken 890 times.
✓ Branch 4 taken 294 times.
6871 if (ffofmt(s->oformat)->init && ret) {
478
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 890 times.
890 if ((ret = init_pts(s)) < 0)
479 return ret;
480
481 890 return AVSTREAM_INIT_IN_INIT_OUTPUT;
482 }
483
484 5981 return AVSTREAM_INIT_IN_WRITE_HEADER;
485 }
486
487 6871 int avformat_write_header(AVFormatContext *s, AVDictionary **options)
488 {
489 6871 FFFormatContext *const si = ffformatcontext(s);
490 6871 int already_initialized = si->initialized;
491 6871 int streams_already_initialized = si->streams_initialized;
492 6871 int ret = 0;
493
494
2/2
✓ Branch 0 taken 6856 times.
✓ Branch 1 taken 15 times.
6871 if (!already_initialized)
495
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6856 times.
6856 if ((ret = avformat_init_output(s, options)) < 0)
496 return ret;
497
498
2/2
✓ Branch 1 taken 5599 times.
✓ Branch 2 taken 1272 times.
6871 if (ffofmt(s->oformat)->write_header) {
499
3/4
✓ Branch 0 taken 5471 times.
✓ Branch 1 taken 128 times.
✓ Branch 2 taken 5471 times.
✗ Branch 3 not taken.
5599 if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
500 5471 avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_HEADER);
501 5599 ret = ffofmt(s->oformat)->write_header(s);
502
4/6
✓ Branch 0 taken 5599 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5471 times.
✓ Branch 3 taken 128 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5471 times.
5599 if (ret >= 0 && s->pb && s->pb->error < 0)
503 ret = s->pb->error;
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5599 times.
5599 if (ret < 0)
505 goto fail;
506 5599 flush_if_needed(s);
507 }
508
3/4
✓ Branch 0 taken 6727 times.
✓ Branch 1 taken 144 times.
✓ Branch 2 taken 6727 times.
✗ Branch 3 not taken.
6871 if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
509 6727 avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_UNKNOWN);
510
511
2/2
✓ Branch 0 taken 5981 times.
✓ Branch 1 taken 890 times.
6871 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 6871 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 481939 static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *pkt)
530 {
531 481939 FFFormatContext *const si = ffformatcontext(s);
532 481939 FFStream *const sti = ffstream(st);
533 481939 int delay = st->codecpar->video_delay;
534 int frame_size;
535
536
2/2
✓ Branch 0 taken 480875 times.
✓ Branch 1 taken 1064 times.
481939 if (!si->missing_ts_warning &&
537
2/2
✓ Branch 0 taken 223818 times.
✓ Branch 1 taken 257057 times.
480875 !(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
538
3/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 223795 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 23 times.
223818 (!(st->disposition & AV_DISPOSITION_ATTACHED_PIC) || (st->disposition & AV_DISPOSITION_TIMED_THUMBNAILS)) &&
539
3/4
✓ Branch 0 taken 223782 times.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 223782 times.
223795 (pkt->pts == AV_NOPTS_VALUE || pkt->dts == AV_NOPTS_VALUE)) {
540 13 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 13 si->missing_ts_warning = 1;
545 }
546
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 481939 times.
481939 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 7911 times.
✓ Branch 1 taken 474028 times.
✓ Branch 2 taken 7910 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 3819 times.
✓ Branch 5 taken 4091 times.
481939 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 473601 times.
✓ Branch 1 taken 8338 times.
✓ Branch 2 taken 4092 times.
✓ Branch 3 taken 469509 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 12429 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
481939 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 477848 times.
✓ Branch 1 taken 4091 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 477848 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
481939 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 476168 times.
✓ Branch 1 taken 5771 times.
✓ Branch 2 taken 469017 times.
✓ Branch 3 taken 7151 times.
481939 if (sti->cur_dts && sti->cur_dts != AV_NOPTS_VALUE &&
578
2/2
✓ Branch 0 taken 341560 times.
✓ Branch 1 taken 127457 times.
469017 ((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
579
2/2
✓ Branch 0 taken 341472 times.
✓ Branch 1 taken 88 times.
341560 st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE &&
580
2/2
✓ Branch 0 taken 341464 times.
✓ Branch 1 taken 8 times.
341472 st->codecpar->codec_type != AVMEDIA_TYPE_DATA &&
581
3/4
✓ Branch 0 taken 341423 times.
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 468976 times.
469017 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 481898 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 477807 times.
✓ Branch 3 taken 4091 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 477807 times.
481898 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 481898 times.
481898 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 481898 sti->cur_dts = pkt->dts;
600 481898 sti->priv_pts.val = pkt->dts;
601
602 /* update pts */
603
3/3
✓ Branch 0 taken 353332 times.
✓ Branch 1 taken 126522 times.
✓ Branch 2 taken 2044 times.
481898 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 126522 case AVMEDIA_TYPE_VIDEO:
617 126522 frac_add(&sti->priv_pts, (int64_t)st->time_base.den * st->time_base.num);
618 126522 break;
619 }
620 481898 return 0;
621 }
622 FF_ENABLE_DEPRECATION_WARNINGS
623 #endif
624
625 481939 static void guess_pkt_duration(AVFormatContext *s, AVStream *st, AVPacket *pkt)
626 {
627
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 481938 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
481939 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 461830 times.
✓ Branch 1 taken 20109 times.
481939 if (pkt->duration)
634 461830 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 481926 static void handle_avoid_negative_ts(FFFormatContext *si, FFStream *sti,
657 AVPacket *pkt)
658 {
659 481926 AVFormatContext *const s = &si->pub;
660 int64_t offset;
661
662
2/2
✓ Branch 0 taken 363647 times.
✓ Branch 1 taken 118279 times.
481926 if (!AVOID_NEGATIVE_TS_ENABLED(si->avoid_negative_ts_status))
663 363647 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 1366 times.
✓ Branch 1 taken 3418 times.
4784 pktl; pktl = pktl->next) {
679 1366 AVRational cmp_tb = s->streams[pktl->pkt.stream_index]->time_base;
680
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 1158 times.
1366 int64_t cmp_ts = use_pts ? pktl->pkt.pts : pktl->pkt.dts;
681
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1366 times.
1366 if (cmp_ts == AV_NOPTS_VALUE)
682 continue;
683 1366 cmp_ts -= ffstream(s->streams[pktl->pkt.stream_index])->lowest_ts_allowed;
684
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1365 times.
1366 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 1365 times.
1366 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 481926 static int write_packet(AVFormatContext *s, AVPacket *pkt)
743 {
744 481926 FFFormatContext *const si = ffformatcontext(s);
745 481926 AVStream *const st = s->streams[pkt->stream_index];
746 481926 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 481122 times.
481926 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 481926 handle_avoid_negative_ts(si, sti, pkt);
760
761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 481926 times.
481926 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 481926 ret = ffofmt(s->oformat)->write_packet(s, pkt);
767 }
768
769
3/4
✓ Branch 0 taken 472621 times.
✓ Branch 1 taken 9305 times.
✓ Branch 2 taken 472621 times.
✗ Branch 3 not taken.
481926 if (s->pb && ret >= 0) {
770 472621 flush_if_needed(s);
771
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 472621 times.
472621 if (s->pb->error < 0)
772 ret = s->pb->error;
773 }
774
775
2/2
✓ Branch 0 taken 481881 times.
✓ Branch 1 taken 45 times.
481926 if (ret >= 0)
776 481881 st->nb_frames++;
777
778 481926 return ret;
779 }
780
781 481812 static int check_packet(AVFormatContext *s, AVPacket *pkt)
782 {
783
2/4
✓ Branch 0 taken 481812 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 481812 times.
481812 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 481812 times.
481812 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 481812 return 0;
795 }
796
797 481812 static int prepare_input_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
798 {
799 481812 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 441086 times.
✓ Branch 1 taken 40726 times.
481812 if (sti->is_intra_only)
841 441086 pkt->flags |= AV_PKT_FLAG_KEY;
842
843
4/4
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 481755 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 53 times.
481812 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 481808 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 14273 times.
✓ Branch 1 taken 10814 times.
25087 if (sti->last_in_packet_buffer) {
880 14273 next_point = &(sti->last_in_packet_buffer->next);
881 } else {
882 10814 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 11122 times.
✓ Branch 1 taken 13965 times.
25087 if (*next_point) {
903
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 11122 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
11122 if (chunked && !(pkt->flags & CHUNK_START))
904 goto next_non_null;
905
906
2/2
✓ Branch 1 taken 7073 times.
✓ Branch 2 taken 4049 times.
11122 if (compare(s, &si->packet_buffer.tail->pkt, pkt)) {
907 7073 while ( *next_point
908
2/6
✓ Branch 0 taken 17738 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17738 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
17738 && ((chunked && !((*next_point)->pkt.flags&CHUNK_START))
909
2/2
✓ Branch 1 taken 10665 times.
✓ Branch 2 taken 7073 times.
17738 || !compare(s, &(*next_point)->pkt, pkt)))
910 10665 next_point = &(*next_point)->next;
911
1/2
✓ Branch 0 taken 7073 times.
✗ Branch 1 not taken.
7073 if (*next_point)
912 7073 goto next_non_null;
913 } else {
914 4049 next_point = &(si->packet_buffer.tail->next);
915 }
916 }
917 av_assert1(!*next_point);
918
919 18014 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 28187 static int interleave_compare_dts(AVFormatContext *s, const AVPacket *next,
930 const AVPacket *pkt)
931 {
932 28187 AVStream *st = s->streams[pkt->stream_index];
933 28187 AVStream *st2 = s->streams[next->stream_index];
934 28187 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 28187 times.
28187 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 4378 times.
✓ Branch 1 taken 23809 times.
28187 if (comp == 0)
955 4378 return pkt->stream_index < next->stream_index;
956 23809 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 86634 times.
✓ Branch 1 taken 32730 times.
119364 if (sti->last_in_packet_buffer) {
978 86634 ++stream_count;
979
2/2
✓ Branch 0 taken 32601 times.
✓ Branch 1 taken 129 times.
32730 } else if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT &&
980
2/2
✓ Branch 0 taken 32230 times.
✓ Branch 1 taken 371 times.
32601 par->codec_id != AV_CODEC_ID_VP8 &&
981
2/2
✓ Branch 0 taken 32228 times.
✓ Branch 1 taken 2 times.
32230 par->codec_id != AV_CODEC_ID_VP9 &&
982
1/2
✓ Branch 0 taken 32228 times.
✗ Branch 1 not taken.
32228 par->codec_id != AV_CODEC_ID_SMPTE_2038) {
983 32228 ++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 33684 times.
✓ Branch 1 taken 27789 times.
✓ Branch 2 taken 956 times.
✓ Branch 3 taken 32728 times.
61473 if (!last || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
1009 28745 continue;
1010
1011 32728 last_dts = av_rescale_q(last->pkt.dts,
1012 st->time_base,
1013 32728 AV_TIME_BASE_Q);
1014 32728 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 10469 times.
✓ Branch 1 taken 14073 times.
24542 if (sti->last_in_packet_buffer == pktl)
1070 10469 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 894318 int ff_interleave_packet_passthrough(AVFormatContext *s, AVPacket *pkt,
1080 int flush, int has_packet)
1081 {
1082 894318 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 481812 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 475300 times.
481812 if (!(s->flags & AVFMT_FLAG_AUTO_BSF))
1119 6512 return 1;
1120
1121
2/2
✓ Branch 1 taken 37358 times.
✓ Branch 2 taken 437942 times.
475300 if (ffofmt(s->oformat)->check_bitstream) {
1122
2/2
✓ Branch 0 taken 425 times.
✓ Branch 1 taken 36933 times.
37358 if (!sti->bitstream_checked) {
1123
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 425 times.
425 if ((ret = ffofmt(s->oformat)->check_bitstream(s, &sti->pub, pkt)) < 0)
1124 return ret;
1125
1/2
✓ Branch 0 taken 425 times.
✗ Branch 1 not taken.
425 else if (ret == 1)
1126 425 sti->bitstream_checked = 1;
1127 }
1128 }
1129
1130 475300 return 1;
1131 }
1132
1133 475833 static int interleaved_write_packet(AVFormatContext *s, AVPacket *pkt,
1134 int flush, int has_packet)
1135 {
1136 475833 FFFormatContext *const si = ffformatcontext(s);
1137 468949 for (;; ) {
1138 944782 int ret = si->interleave_packet(s, pkt, flush, has_packet);
1139
2/2
✓ Branch 0 taken 475833 times.
✓ Branch 1 taken 468949 times.
944782 if (ret <= 0)
1140 475833 return ret;
1141
1142 468949 has_packet = 0;
1143
1144 468949 ret = write_packet(s, pkt);
1145 468949 av_packet_unref(pkt);
1146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 468949 times.
468949 if (ret < 0)
1147 return ret;
1148 }
1149 }
1150
1151 481939 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 481939 times.
481939 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 481939 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 481898 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 41 times.
481939 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 468962 times.
✓ Branch 1 taken 12977 times.
481939 if (interleaved) {
1167
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 468962 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
468962 if (pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
1168 return AVERROR(EINVAL);
1169 468962 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 481812 static int write_packets_common(AVFormatContext *s, AVPacket *pkt, int interleaved)
1209 {
1210 AVStream *st;
1211 FFStream *sti;
1212 481812 int ret = check_packet(s, pkt);
1213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 481812 times.
481812 if (ret < 0)
1214 return ret;
1215 481812 st = s->streams[pkt->stream_index];
1216 481812 sti = ffstream(st);
1217
1218 481812 ret = prepare_input_packet(s, st, pkt);
1219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 481812 times.
481812 if (ret < 0)
1220 return ret;
1221
1222 481812 ret = check_bitstream(s, sti, pkt);
1223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 481812 times.
481812 if (ret < 0)
1224 return ret;
1225
1226
2/2
✓ Branch 0 taken 815 times.
✓ Branch 1 taken 480997 times.
481812 if (sti->bsfc) {
1227 815 return write_packets_from_bsfs(s, st, pkt, interleaved);
1228 } else {
1229 480997 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 468835 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt)
1281 {
1282 int ret;
1283
1284
1/2
✓ Branch 0 taken 468835 times.
✗ Branch 1 not taken.
468835 if (pkt) {
1285 468835 ret = write_packets_common(s, pkt, 1/*interleaved*/);
1286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 468835 times.
468835 if (ret < 0)
1287 av_packet_unref(pkt);
1288 468835 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 6871 int av_write_trailer(AVFormatContext *s)
1296 {
1297 6871 FFFormatContext *const si = ffformatcontext(s);
1298 6871 AVPacket *const pkt = si->parse_pkt;
1299 6871 int ret1, ret = 0;
1300
1301
2/2
✓ Branch 0 taken 7252 times.
✓ Branch 1 taken 6871 times.
14123 for (unsigned i = 0; i < s->nb_streams; i++) {
1302 7252 AVStream *const st = s->streams[i];
1303 7252 FFStream *const sti = ffstream(st);
1304
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 7209 times.
7252 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 6871 ret1 = interleaved_write_packet(s, pkt, 1, 0);
1313
1/2
✓ Branch 0 taken 6871 times.
✗ Branch 1 not taken.
6871 if (ret >= 0)
1314 6871 ret = ret1;
1315
1316
2/2
✓ Branch 1 taken 3849 times.
✓ Branch 2 taken 3022 times.
6871 if (ffofmt(s->oformat)->write_trailer) {
1317
3/4
✓ Branch 0 taken 3779 times.
✓ Branch 1 taken 70 times.
✓ Branch 2 taken 3779 times.
✗ Branch 3 not taken.
3849 if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
1318 3779 avio_write_marker(s->pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_TRAILER);
1319 3849 ret1 = ffofmt(s->oformat)->write_trailer(s);
1320
1/2
✓ Branch 0 taken 3849 times.
✗ Branch 1 not taken.
3849 if (ret >= 0)
1321 3849 ret = ret1;
1322 }
1323
1324 6871 deinit_muxer(s);
1325
1326
2/2
✓ Branch 0 taken 6727 times.
✓ Branch 1 taken 144 times.
6871 if (s->pb)
1327 6727 avio_flush(s->pb);
1328
2/2
✓ Branch 0 taken 6838 times.
✓ Branch 1 taken 33 times.
6871 if (ret == 0)
1329
2/2
✓ Branch 0 taken 6694 times.
✓ Branch 1 taken 144 times.
6838 ret = s->pb ? s->pb->error : 0;
1330
2/2
✓ Branch 0 taken 7252 times.
✓ Branch 1 taken 6871 times.
14123 for (unsigned i = 0; i < s->nb_streams; i++) {
1331 7252 av_freep(&s->streams[i]->priv_data);
1332 7252 av_freep(&ffstream(s->streams[i])->index_entries);
1333 }
1334
2/2
✓ Branch 0 taken 4003 times.
✓ Branch 1 taken 2868 times.
6871 if (s->oformat->priv_class)
1335 4003 av_opt_free(s->priv_data);
1336 6871 av_freep(&s->priv_data);
1337 6871 av_packet_unref(si->pkt);
1338 6871 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