FFmpeg coverage


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