FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mux.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 570 820 69.5%
Functions: 32 38 84.2%
Branches: 427 648 65.9%

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