FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_enc.c
Date: 2023-06-04 16:45:34
Exec Total Coverage
Lines: 478 680 70.3%
Functions: 18 20 90.0%
Branches: 334 558 59.9%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <math.h>
20 #include <stdint.h>
21
22 #include "ffmpeg.h"
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/avutil.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/display.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/frame.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/log.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/rational.h"
35 #include "libavutil/timestamp.h"
36
37 #include "libavfilter/buffersink.h"
38
39 #include "libavcodec/avcodec.h"
40
41 // FIXME private header, used for mid_pred()
42 #include "libavcodec/mathops.h"
43
44 #include "libavformat/avformat.h"
45
46 struct Encoder {
47 /* predicted pts of the next frame to be encoded */
48 int64_t next_pts;
49
50 AVFrame *last_frame;
51 /* number of frames emitted by the video-encoding sync code */
52 int64_t vsync_frame_number;
53 /* history of nb_frames_prev, i.e. the number of times the
54 * previous frame was duplicated by vsync code in recent
55 * do_video_out() calls */
56 int64_t frames_prev_hist[3];
57
58 AVFrame *sq_frame;
59
60 // combined size of all the packets received from the encoder
61 uint64_t data_size;
62
63 // number of packets received from the encoder
64 uint64_t packets_encoded;
65 };
66
67 static uint64_t dup_warning = 1000;
68
69 6822 void enc_free(Encoder **penc)
70 {
71 6822 Encoder *enc = *penc;
72
73
2/2
✓ Branch 0 taken 496 times.
✓ Branch 1 taken 6326 times.
6822 if (!enc)
74 496 return;
75
76 6326 av_frame_free(&enc->last_frame);
77 6326 av_frame_free(&enc->sq_frame);
78
79 6326 av_freep(penc);
80 }
81
82 6326 int enc_alloc(Encoder **penc, const AVCodec *codec)
83 {
84 Encoder *enc;
85
86 6326 *penc = NULL;
87
88 6326 enc = av_mallocz(sizeof(*enc));
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6326 times.
6326 if (!enc)
90 return AVERROR(ENOMEM);
91
92
2/2
✓ Branch 0 taken 5099 times.
✓ Branch 1 taken 1227 times.
6326 if (codec->type == AVMEDIA_TYPE_VIDEO) {
93 5099 enc->last_frame = av_frame_alloc();
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5099 times.
5099 if (!enc->last_frame)
95 goto fail;
96 }
97
98 6326 *penc = enc;
99
100 6326 return 0;
101 fail:
102 enc_free(&enc);
103 return AVERROR(ENOMEM);
104 }
105
106 6326 static int hw_device_setup_for_encode(OutputStream *ost, AVBufferRef *frames_ref)
107 {
108 const AVCodecHWConfig *config;
109 6326 HWDevice *dev = NULL;
110 int i;
111
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6326 times.
6326 if (frames_ref &&
113 ((AVHWFramesContext*)frames_ref->data)->format ==
114 ost->enc_ctx->pix_fmt) {
115 // Matching format, will try to use hw_frames_ctx.
116 } else {
117 6326 frames_ref = NULL;
118 }
119
120 6326 for (i = 0;; i++) {
121 6326 config = avcodec_get_hw_config(ost->enc_ctx->codec, i);
122
1/2
✓ Branch 0 taken 6326 times.
✗ Branch 1 not taken.
6326 if (!config)
123 6326 break;
124
125 if (frames_ref &&
126 config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
127 (config->pix_fmt == AV_PIX_FMT_NONE ||
128 config->pix_fmt == ost->enc_ctx->pix_fmt)) {
129 av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using input "
130 "frames context (format %s) with %s encoder.\n",
131 av_get_pix_fmt_name(ost->enc_ctx->pix_fmt),
132 ost->enc_ctx->codec->name);
133 ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
134 if (!ost->enc_ctx->hw_frames_ctx)
135 return AVERROR(ENOMEM);
136 return 0;
137 }
138
139 if (!dev &&
140 config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
141 dev = hw_device_get_by_type(config->device_type);
142 }
143
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6326 times.
6326 if (dev) {
145 av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using device %s "
146 "(type %s) with %s encoder.\n", dev->name,
147 av_hwdevice_get_type_name(dev->type), ost->enc_ctx->codec->name);
148 ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
149 if (!ost->enc_ctx->hw_device_ctx)
150 return AVERROR(ENOMEM);
151 } else {
152 // No device required, or no device available.
153 }
154 6326 return 0;
155 }
156
157 6326 static void set_encoder_id(OutputFile *of, OutputStream *ost)
158 {
159 6326 const char *cname = ost->enc_ctx->codec->name;
160 uint8_t *encoder_string;
161 int encoder_string_len;
162
163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6326 times.
6326 if (av_dict_get(ost->st->metadata, "encoder", NULL, 0))
164 return;
165
166 6326 encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(cname) + 2;
167 6326 encoder_string = av_mallocz(encoder_string_len);
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6326 times.
6326 if (!encoder_string)
169 report_and_exit(AVERROR(ENOMEM));
170
171
4/4
✓ Branch 0 taken 836 times.
✓ Branch 1 taken 5490 times.
✓ Branch 2 taken 829 times.
✓ Branch 3 taken 7 times.
6326 if (!of->bitexact && !ost->bitexact)
172 829 av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
173 else
174 5497 av_strlcpy(encoder_string, "Lavc ", encoder_string_len);
175 6326 av_strlcat(encoder_string, cname, encoder_string_len);
176 6326 av_dict_set(&ost->st->metadata, "encoder", encoder_string,
177 AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
178 }
179
180 6288 static void init_encoder_time_base(OutputStream *ost, AVRational default_time_base)
181 {
182 6288 InputStream *ist = ost->ist;
183 6288 AVCodecContext *enc_ctx = ost->enc_ctx;
184
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6288 times.
6288 if (ost->enc_timebase.num > 0) {
186 enc_ctx->time_base = ost->enc_timebase;
187 return;
188 }
189
190
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6287 times.
6288 if (ost->enc_timebase.num < 0) {
191
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ist) {
192 1 enc_ctx->time_base = ist->st->time_base;
193 1 return;
194 }
195
196 av_log(ost, AV_LOG_WARNING,
197 "Input stream data not available, using default time base\n");
198 }
199
200 6287 enc_ctx->time_base = default_time_base;
201 }
202
203 403511 int enc_open(OutputStream *ost, AVFrame *frame)
204 {
205 403511 InputStream *ist = ost->ist;
206 403511 Encoder *e = ost->enc;
207 403511 AVCodecContext *enc_ctx = ost->enc_ctx;
208 403511 AVCodecContext *dec_ctx = NULL;
209 403511 const AVCodec *enc = enc_ctx->codec;
210 403511 OutputFile *of = output_files[ost->file_index];
211 int ret;
212
213
2/2
✓ Branch 0 taken 397185 times.
✓ Branch 1 taken 6326 times.
403511 if (ost->initialized)
214 397185 return 0;
215
216 6326 set_encoder_id(output_files[ost->file_index], ost);
217
218
2/2
✓ Branch 0 taken 6212 times.
✓ Branch 1 taken 114 times.
6326 if (ist) {
219 6212 dec_ctx = ist->dec_ctx;
220 }
221
222
2/2
✓ Branch 0 taken 5099 times.
✓ Branch 1 taken 1227 times.
6326 if (enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
223
2/2
✓ Branch 0 taken 5080 times.
✓ Branch 1 taken 19 times.
5099 if (!ost->frame_rate.num)
224 5080 ost->frame_rate = av_buffersink_get_frame_rate(ost->filter->filter);
225
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5097 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
5099 if (!ost->frame_rate.num && !ost->max_frame_rate.num) {
226 2 ost->frame_rate = (AVRational){25, 1};
227 2 av_log(ost, AV_LOG_WARNING,
228 "No information "
229 "about the input framerate is available. Falling "
230 "back to a default value of 25fps. Use the -r option "
231 "if you want a different framerate.\n");
232 }
233
234
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5099 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5099 if (ost->max_frame_rate.num &&
235 (av_q2d(ost->frame_rate) > av_q2d(ost->max_frame_rate) ||
236 !ost->frame_rate.den))
237 ost->frame_rate = ost->max_frame_rate;
238
239
3/4
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 5048 times.
✓ Branch 2 taken 51 times.
✗ Branch 3 not taken.
5099 if (enc->supported_framerates && !ost->force_fps) {
240 51 int idx = av_find_nearest_q_idx(ost->frame_rate, enc->supported_framerates);
241 51 ost->frame_rate = enc->supported_framerates[idx];
242 }
243 // reduce frame rate for mpeg4 to be within the spec limits
244
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 5039 times.
5099 if (enc_ctx->codec_id == AV_CODEC_ID_MPEG4) {
245 60 av_reduce(&ost->frame_rate.num, &ost->frame_rate.den,
246 60 ost->frame_rate.num, ost->frame_rate.den, 65535);
247 }
248 }
249
250
3/4
✓ Branch 0 taken 1189 times.
✓ Branch 1 taken 5099 times.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
6326 switch (enc_ctx->codec_type) {
251 1189 case AVMEDIA_TYPE_AUDIO:
252 1189 enc_ctx->sample_fmt = av_buffersink_get_format(ost->filter->filter);
253 1189 enc_ctx->sample_rate = av_buffersink_get_sample_rate(ost->filter->filter);
254 1189 ret = av_buffersink_get_ch_layout(ost->filter->filter, &enc_ctx->ch_layout);
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1189 times.
1189 if (ret < 0)
256 return ret;
257
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1189 times.
1189 if (ost->bits_per_raw_sample)
259 enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
260
4/4
✓ Branch 0 taken 1158 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 253 times.
✓ Branch 3 taken 905 times.
1189 else if (dec_ctx && ost->filter->graph->is_meta)
261
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 253 times.
253 enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample,
262 av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
263
264 1189 init_encoder_time_base(ost, av_make_q(1, enc_ctx->sample_rate));
265 1189 break;
266
267 5099 case AVMEDIA_TYPE_VIDEO:
268 5099 init_encoder_time_base(ost, av_inv_q(ost->frame_rate));
269
270
3/4
✓ Branch 0 taken 5098 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5098 times.
5099 if (!(enc_ctx->time_base.num && enc_ctx->time_base.den))
271 1 enc_ctx->time_base = av_buffersink_get_time_base(ost->filter->filter);
272
3/4
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 5088 times.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
5099 if ( av_q2d(enc_ctx->time_base) < 0.001 && ost->vsync_method != VSYNC_PASSTHROUGH
273
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
11 && (ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR ||
274
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 (ost->vsync_method == VSYNC_AUTO && !(of->format->flags & AVFMT_VARIABLE_FPS)))){
275 7 av_log(ost, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n"
276 "Please consider specifying a lower framerate, a different muxer or "
277 "setting vsync/fps_mode to vfr\n");
278 }
279
280 5099 enc_ctx->width = av_buffersink_get_w(ost->filter->filter);
281 5099 enc_ctx->height = av_buffersink_get_h(ost->filter->filter);
282 10198 enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio =
283 5099 ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5099 times.
5099 av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) :
285 5099 av_buffersink_get_sample_aspect_ratio(ost->filter->filter);
286
287 5099 enc_ctx->pix_fmt = av_buffersink_get_format(ost->filter->filter);
288
289
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5098 times.
5099 if (ost->bits_per_raw_sample)
290 1 enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
291
4/4
✓ Branch 0 taken 5015 times.
✓ Branch 1 taken 83 times.
✓ Branch 2 taken 1477 times.
✓ Branch 3 taken 3538 times.
5098 else if (dec_ctx && ost->filter->graph->is_meta)
292
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1477 times.
1477 enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample,
293 av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
294
295
1/2
✓ Branch 0 taken 5099 times.
✗ Branch 1 not taken.
5099 if (frame) {
296 5099 enc_ctx->color_range = frame->color_range;
297 5099 enc_ctx->color_primaries = frame->color_primaries;
298 5099 enc_ctx->color_trc = frame->color_trc;
299 5099 enc_ctx->colorspace = frame->colorspace;
300 5099 enc_ctx->chroma_sample_location = frame->chroma_location;
301 }
302
303 5099 enc_ctx->framerate = ost->frame_rate;
304
305 5099 ost->st->avg_frame_rate = ost->frame_rate;
306
307 // Field order: autodetection
308
1/2
✓ Branch 0 taken 5099 times.
✗ Branch 1 not taken.
5099 if (frame) {
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5099 times.
5099 if (enc_ctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) &&
310 ost->top_field_first >= 0)
311 if (ost->top_field_first)
312 frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
313 else
314 frame->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
315
316
2/2
✓ Branch 0 taken 401 times.
✓ Branch 1 taken 4698 times.
5099 if (frame->flags & AV_FRAME_FLAG_INTERLACED) {
317
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 400 times.
401 if (enc->id == AV_CODEC_ID_MJPEG)
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 enc_ctx->field_order = (frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) ? AV_FIELD_TT:AV_FIELD_BB;
319 else
320
2/2
✓ Branch 0 taken 337 times.
✓ Branch 1 taken 63 times.
400 enc_ctx->field_order = (frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) ? AV_FIELD_TB:AV_FIELD_BT;
321 } else
322 4698 enc_ctx->field_order = AV_FIELD_PROGRESSIVE;
323 }
324
325 // Field order: override
326
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5096 times.
5099 if (ost->top_field_first == 0) {
327 3 enc_ctx->field_order = AV_FIELD_BB;
328
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5095 times.
5096 } else if (ost->top_field_first == 1) {
329 1 enc_ctx->field_order = AV_FIELD_TT;
330 }
331
332 5099 break;
333 38 case AVMEDIA_TYPE_SUBTITLE:
334 38 enc_ctx->time_base = AV_TIME_BASE_Q;
335
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (!enc_ctx->width) {
336 38 enc_ctx->width = ost->ist->par->width;
337 38 enc_ctx->height = ost->ist->par->height;
338 }
339
3/4
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 2 times.
38 if (dec_ctx && dec_ctx->subtitle_header) {
340 /* ASS code assumes this buffer is null terminated so add extra byte. */
341 36 enc_ctx->subtitle_header = av_mallocz(dec_ctx->subtitle_header_size + 1);
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (!enc_ctx->subtitle_header)
343 return AVERROR(ENOMEM);
344 36 memcpy(enc_ctx->subtitle_header, dec_ctx->subtitle_header,
345 36 dec_ctx->subtitle_header_size);
346 36 enc_ctx->subtitle_header_size = dec_ctx->subtitle_header_size;
347 }
348
349 38 break;
350 default:
351 av_assert0(0);
352 break;
353 }
354
355
2/2
✓ Branch 0 taken 5447 times.
✓ Branch 1 taken 879 times.
6326 if (ost->bitexact)
356 5447 enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
357
358
2/2
✓ Branch 1 taken 2583 times.
✓ Branch 2 taken 3743 times.
6326 if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
359 2583 av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
360
361
2/2
✓ Branch 0 taken 6267 times.
✓ Branch 1 taken 59 times.
6326 if (enc->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE) {
362 6267 ret = av_dict_set(&ost->encoder_opts, "flags", "+copy_opaque", AV_DICT_MULTIKEY);
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6267 times.
6267 if (ret < 0)
364 return ret;
365 }
366
367 6326 av_dict_set(&ost->encoder_opts, "flags", "+frame_duration", AV_DICT_MULTIKEY);
368
369
2/2
✓ Branch 0 taken 6287 times.
✓ Branch 1 taken 39 times.
6326 ret = hw_device_setup_for_encode(ost, frame ? frame->hw_frames_ctx : NULL);
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6326 times.
6326 if (ret < 0) {
371 av_log(ost, AV_LOG_ERROR,
372 "Encoding hardware device setup failed: %s\n", av_err2str(ret));
373 return ret;
374 }
375
376
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6326 times.
6326 if ((ret = avcodec_open2(ost->enc_ctx, enc, &ost->encoder_opts)) < 0) {
377 if (ret != AVERROR_EXPERIMENTAL)
378 av_log(ost, AV_LOG_ERROR, "Error while opening encoder - maybe "
379 "incorrect parameters such as bit_rate, rate, width or height.\n");
380 return ret;
381 }
382
383
2/2
✓ Branch 0 taken 2758 times.
✓ Branch 1 taken 3568 times.
6326 if (ost->sq_idx_encode >= 0) {
384 2758 e->sq_frame = av_frame_alloc();
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2758 times.
2758 if (!e->sq_frame)
386 return AVERROR(ENOMEM);
387 }
388
389
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 6206 times.
6326 if (ost->enc_ctx->frame_size) {
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120 times.
120 av_assert0(ost->sq_idx_encode >= 0);
391 120 sq_frame_samples(output_files[ost->file_index]->sq_encode,
392 120 ost->sq_idx_encode, ost->enc_ctx->frame_size);
393 }
394
395 6326 assert_avoptions(ost->encoder_opts);
396
4/4
✓ Branch 0 taken 6287 times.
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 6286 times.
6326 if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
397
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
398 1 av_log(ost, AV_LOG_WARNING, "The bitrate parameter is set too low."
399 " It takes bits/s as argument, not kbits/s\n");
400
401 6326 ret = avcodec_parameters_from_context(ost->par_in, ost->enc_ctx);
402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6326 times.
6326 if (ret < 0) {
403 av_log(ost, AV_LOG_FATAL,
404 "Error initializing the output stream codec context.\n");
405 exit_program(1);
406 }
407
408
2/2
✓ Branch 0 taken 197 times.
✓ Branch 1 taken 6129 times.
6326 if (ost->enc_ctx->nb_coded_side_data) {
409 int i;
410
411
2/2
✓ Branch 0 taken 197 times.
✓ Branch 1 taken 197 times.
394 for (i = 0; i < ost->enc_ctx->nb_coded_side_data; i++) {
412 197 const AVPacketSideData *sd_src = &ost->enc_ctx->coded_side_data[i];
413 uint8_t *dst_data;
414
415 197 dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
197 if (!dst_data)
417 return AVERROR(ENOMEM);
418 197 memcpy(dst_data, sd_src->data, sd_src->size);
419 }
420 }
421
422 /*
423 * Add global input side data. For now this is naive, and copies it
424 * from the input stream's global side data. All side data should
425 * really be funneled over AVFrame and libavfilter, then added back to
426 * packet side data, and then potentially using the first packet for
427 * global side data.
428 */
429
2/2
✓ Branch 0 taken 6212 times.
✓ Branch 1 taken 114 times.
6326 if (ist) {
430 int i;
431
2/2
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 6212 times.
6484 for (i = 0; i < ist->st->nb_side_data; i++) {
432 272 AVPacketSideData *sd = &ist->st->side_data[i];
433
2/2
✓ Branch 0 taken 214 times.
✓ Branch 1 taken 58 times.
272 if (sd->type != AV_PKT_DATA_CPB_PROPERTIES) {
434 214 uint8_t *dst = av_stream_new_side_data(ost->st, sd->type, sd->size);
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 214 times.
214 if (!dst)
436 return AVERROR(ENOMEM);
437 214 memcpy(dst, sd->data, sd->size);
438
3/4
✓ Branch 0 taken 214 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 213 times.
214 if (ist->autorotate && sd->type == AV_PKT_DATA_DISPLAYMATRIX)
439 1 av_display_rotation_set((int32_t *)dst, 0);
440 }
441 }
442 }
443
444 // copy timebase while removing common factors
445
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6324 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
6326 if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0)
446 6324 ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
447
448 // copy estimated duration as a hint to the muxer
449
5/6
✓ Branch 0 taken 6326 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6212 times.
✓ Branch 3 taken 114 times.
✓ Branch 4 taken 5023 times.
✓ Branch 5 taken 1189 times.
6326 if (ost->st->duration <= 0 && ist && ist->st->duration > 0)
450 5023 ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
451
452 6326 ost->mux_timebase = enc_ctx->time_base;
453
454 6326 ret = of_stream_init(of, ost);
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6326 times.
6326 if (ret < 0)
456 return ret;
457
458 6326 return 0;
459 }
460
461 401737 static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
462 {
463 401737 OutputFile *of = output_files[ost->file_index];
464
465
4/4
✓ Branch 0 taken 8182 times.
✓ Branch 1 taken 393555 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 8181 times.
409919 if (of->recording_time != INT64_MAX &&
466 8182 av_compare_ts(ts, tb, of->recording_time, AV_TIME_BASE_Q) >= 0) {
467 1 close_output_stream(ost);
468 1 return 0;
469 }
470 401736 return 1;
471 }
472
473 792 void enc_subtitle(OutputFile *of, OutputStream *ost, AVSubtitle *sub)
474 {
475 792 int subtitle_out_max_size = 1024 * 1024;
476 int subtitle_out_size, nb, i, ret;
477 AVCodecContext *enc;
478 792 AVPacket *pkt = ost->pkt;
479 int64_t pts;
480
481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 792 times.
792 if (sub->pts == AV_NOPTS_VALUE) {
482 av_log(ost, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
483 if (exit_on_error)
484 exit_program(1);
485 return;
486 }
487
1/2
✓ Branch 0 taken 792 times.
✗ Branch 1 not taken.
792 if (ost->finished ||
488
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 792 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
792 (of->start_time != AV_NOPTS_VALUE && sub->pts < of->start_time))
489 return;
490
491 792 enc = ost->enc_ctx;
492
493 /* Note: DVB subtitle need one packet to draw them and one other
494 packet to clear them */
495 /* XXX: signal it in the codec context ? */
496
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 756 times.
792 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
497 36 nb = 2;
498
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 306 times.
756 else if (enc->codec_id == AV_CODEC_ID_ASS)
499 450 nb = FFMAX(sub->num_rects, 1);
500 else
501 306 nb = 1;
502
503 /* shift timestamp to honor -ss and make check_recording_time() work with -t */
504 792 pts = sub->pts;
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 792 times.
792 if (output_files[ost->file_index]->start_time != AV_NOPTS_VALUE)
506 pts -= output_files[ost->file_index]->start_time;
507
2/2
✓ Branch 0 taken 828 times.
✓ Branch 1 taken 792 times.
1620 for (i = 0; i < nb; i++) {
508 828 AVSubtitle local_sub = *sub;
509
510
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 828 times.
828 if (!check_recording_time(ost, pts, AV_TIME_BASE_Q))
511 return;
512
513 828 ret = av_new_packet(pkt, subtitle_out_max_size);
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 828 times.
828 if (ret < 0)
515 report_and_exit(AVERROR(ENOMEM));
516
517 828 local_sub.pts = pts;
518 // start_display_time is required to be 0
519 828 local_sub.pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
520 828 local_sub.end_display_time -= sub->start_display_time;
521 828 local_sub.start_display_time = 0;
522
523
4/4
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 756 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 36 times.
828 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE && i == 1)
524 36 local_sub.num_rects = 0;
525
3/4
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 342 times.
✓ Branch 2 taken 450 times.
✗ Branch 3 not taken.
792 else if (enc->codec_id == AV_CODEC_ID_ASS && sub->num_rects > 0) {
526 450 local_sub.num_rects = 1;
527 450 local_sub.rects += i;
528 }
529
530 828 ost->frames_encoded++;
531
532 828 subtitle_out_size = avcodec_encode_subtitle(enc, pkt->data, pkt->size, &local_sub);
533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 828 times.
828 if (subtitle_out_size < 0) {
534 av_log(ost, AV_LOG_FATAL, "Subtitle encoding failed\n");
535 exit_program(1);
536 }
537
538 828 av_shrink_packet(pkt, subtitle_out_size);
539 828 pkt->time_base = ost->mux_timebase;
540 828 pkt->pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, pkt->time_base);
541 828 pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
542
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 756 times.
828 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
543 /* XXX: the pts correction is handled here. Maybe handling
544 it in the codec would be better */
545
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
72 if (i == 0)
546 36 pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
547 else
548 36 pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
549 }
550 828 pkt->dts = pkt->pts;
551
552 828 of_output_packet(of, pkt, ost, 0);
553 }
554 }
555
556 void enc_stats_write(OutputStream *ost, EncStats *es,
557 const AVFrame *frame, const AVPacket *pkt,
558 uint64_t frame_num)
559 {
560 Encoder *e = ost->enc;
561 AVIOContext *io = es->io;
562 AVRational tb = frame ? frame->time_base : pkt->time_base;
563 int64_t pts = frame ? frame->pts : pkt->pts;
564
565 AVRational tbi = (AVRational){ 0, 1};
566 int64_t ptsi = INT64_MAX;
567
568 const FrameData *fd;
569
570 if ((frame && frame->opaque_ref) || (pkt && pkt->opaque_ref)) {
571 fd = (const FrameData*)(frame ? frame->opaque_ref->data : pkt->opaque_ref->data);
572 tbi = fd->tb;
573 ptsi = fd->pts;
574 }
575
576 for (size_t i = 0; i < es->nb_components; i++) {
577 const EncStatsComponent *c = &es->components[i];
578
579 switch (c->type) {
580 case ENC_STATS_LITERAL: avio_write (io, c->str, c->str_len); continue;
581 case ENC_STATS_FILE_IDX: avio_printf(io, "%d", ost->file_index); continue;
582 case ENC_STATS_STREAM_IDX: avio_printf(io, "%d", ost->index); continue;
583 case ENC_STATS_TIMEBASE: avio_printf(io, "%d/%d", tb.num, tb.den); continue;
584 case ENC_STATS_TIMEBASE_IN: avio_printf(io, "%d/%d", tbi.num, tbi.den); continue;
585 case ENC_STATS_PTS: avio_printf(io, "%"PRId64, pts); continue;
586 case ENC_STATS_PTS_IN: avio_printf(io, "%"PRId64, ptsi); continue;
587 case ENC_STATS_PTS_TIME: avio_printf(io, "%g", pts * av_q2d(tb)); continue;
588 case ENC_STATS_PTS_TIME_IN: avio_printf(io, "%g", ptsi == INT64_MAX ?
589 INFINITY : ptsi * av_q2d(tbi)); continue;
590 case ENC_STATS_FRAME_NUM: avio_printf(io, "%"PRIu64, frame_num); continue;
591 case ENC_STATS_FRAME_NUM_IN: avio_printf(io, "%"PRIu64, fd ? fd->idx : -1); continue;
592 }
593
594 if (frame) {
595 switch (c->type) {
596 case ENC_STATS_SAMPLE_NUM: avio_printf(io, "%"PRIu64, ost->samples_encoded); continue;
597 case ENC_STATS_NB_SAMPLES: avio_printf(io, "%d", frame->nb_samples); continue;
598 default: av_assert0(0);
599 }
600 } else {
601 switch (c->type) {
602 case ENC_STATS_DTS: avio_printf(io, "%"PRId64, pkt->dts); continue;
603 case ENC_STATS_DTS_TIME: avio_printf(io, "%g", pkt->dts * av_q2d(tb)); continue;
604 case ENC_STATS_PKT_SIZE: avio_printf(io, "%d", pkt->size); continue;
605 case ENC_STATS_BITRATE: {
606 double duration = FFMAX(pkt->duration, 1) * av_q2d(tb);
607 avio_printf(io, "%g", 8.0 * pkt->size / duration);
608 continue;
609 }
610 case ENC_STATS_AVG_BITRATE: {
611 double duration = pkt->dts * av_q2d(tb);
612 avio_printf(io, "%g", duration > 0 ? 8.0 * e->data_size / duration : -1.);
613 continue;
614 }
615 default: av_assert0(0);
616 }
617 }
618 }
619 avio_w8(io, '\n');
620 avio_flush(io);
621 }
622
623 static inline double psnr(double d)
624 {
625 return -10.0 * log10(d);
626 }
627
628 104745 static void update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
629 {
630 104745 Encoder *e = ost->enc;
631 104745 const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
632 NULL);
633 104745 AVCodecContext *enc = ost->enc_ctx;
634 enum AVPictureType pict_type;
635 int64_t frame_number;
636 double ti1, bitrate, avg_bitrate;
637 104745 double psnr_val = -1;
638
639
2/2
✓ Branch 0 taken 10124 times.
✓ Branch 1 taken 94621 times.
104745 ost->quality = sd ? AV_RL32(sd) : -1;
640
2/2
✓ Branch 0 taken 10124 times.
✓ Branch 1 taken 94621 times.
104745 pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
641
642
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 104745 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
104745 if ((enc->flags & AV_CODEC_FLAG_PSNR) && sd && sd[5]) {
643 // FIXME the scaling assumes 8bit
644 double error = AV_RL64(sd + 8) / (enc->width * enc->height * 255.0 * 255.0);
645 if (error >= 0 && error <= 1)
646 psnr_val = psnr(error);
647 }
648
649
1/2
✓ Branch 0 taken 104745 times.
✗ Branch 1 not taken.
104745 if (!write_vstats)
650 104745 return;
651
652 /* this is executed just the first time update_video_stats is called */
653 if (!vstats_file) {
654 vstats_file = fopen(vstats_filename, "w");
655 if (!vstats_file) {
656 perror("fopen");
657 exit_program(1);
658 }
659 }
660
661 frame_number = e->packets_encoded;
662 if (vstats_version <= 1) {
663 fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number,
664 ost->quality / (float)FF_QP2LAMBDA);
665 } else {
666 fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ", ost->file_index, ost->index, frame_number,
667 ost->quality / (float)FF_QP2LAMBDA);
668 }
669
670 if (psnr_val >= 0)
671 fprintf(vstats_file, "PSNR= %6.2f ", psnr_val);
672
673 fprintf(vstats_file,"f_size= %6d ", pkt->size);
674 /* compute pts value */
675 ti1 = pkt->dts * av_q2d(pkt->time_base);
676 if (ti1 < 0.01)
677 ti1 = 0.01;
678
679 bitrate = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0;
680 avg_bitrate = (double)(e->data_size * 8) / ti1 / 1000.0;
681 fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
682 (double)e->data_size / 1024, ti1, bitrate, avg_bitrate);
683 fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(pict_type));
684 }
685
686 432410 static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
687 {
688 432410 Encoder *e = ost->enc;
689 432410 AVCodecContext *enc = ost->enc_ctx;
690 432410 AVPacket *pkt = ost->pkt;
691 432410 const char *type_desc = av_get_media_type_string(enc->codec_type);
692
2/2
✓ Branch 0 taken 423497 times.
✓ Branch 1 taken 8913 times.
432410 const char *action = frame ? "encode" : "flush";
693 int ret;
694
695
2/2
✓ Branch 0 taken 423497 times.
✓ Branch 1 taken 8913 times.
432410 if (frame) {
696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 423497 times.
423497 if (ost->enc_stats_pre.io)
697 enc_stats_write(ost, &ost->enc_stats_pre, frame, NULL,
698 ost->frames_encoded);
699
700 423497 ost->frames_encoded++;
701 423497 ost->samples_encoded += frame->nb_samples;
702
703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 423497 times.
423497 if (debug_ts) {
704 av_log(ost, AV_LOG_INFO, "encoder <- type:%s "
705 "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
706 type_desc,
707 av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
708 enc->time_base.num, enc->time_base.den);
709 }
710
711
3/4
✓ Branch 0 taken 14830 times.
✓ Branch 1 taken 408667 times.
✓ Branch 2 taken 14830 times.
✗ Branch 3 not taken.
423497 if (frame->sample_aspect_ratio.num && !ost->frame_aspect_ratio.num)
712 14830 enc->sample_aspect_ratio = frame->sample_aspect_ratio;
713 }
714
715 432410 update_benchmark(NULL);
716
717 432410 ret = avcodec_send_frame(enc, frame);
718
4/6
✓ Branch 0 taken 2625 times.
✓ Branch 1 taken 429785 times.
✓ Branch 2 taken 2625 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2625 times.
✗ Branch 5 not taken.
432410 if (ret < 0 && !(ret == AVERROR_EOF && !frame)) {
719 av_log(ost, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n",
720 type_desc);
721 return ret;
722 }
723
724 while (1) {
725 849795 ret = avcodec_receive_packet(enc, pkt);
726 849795 update_benchmark("%s_%s %d.%d", action, type_desc,
727 ost->file_index, ost->index);
728
729 849795 pkt->time_base = enc->time_base;
730
731 /* if two pass, output log on success and EOF */
732
7/8
✓ Branch 0 taken 432410 times.
✓ Branch 1 taken 417385 times.
✓ Branch 2 taken 8913 times.
✓ Branch 3 taken 423497 times.
✓ Branch 4 taken 204 times.
✓ Branch 5 taken 426094 times.
✓ Branch 6 taken 204 times.
✗ Branch 7 not taken.
849795 if ((ret >= 0 || ret == AVERROR_EOF) && ost->logfile && enc->stats_out)
733 204 fprintf(ost->logfile, "%s", enc->stats_out);
734
735
2/2
✓ Branch 0 taken 423497 times.
✓ Branch 1 taken 426298 times.
849795 if (ret == AVERROR(EAGAIN)) {
736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 423497 times.
423497 av_assert0(frame); // should never happen during flushing
737 423497 return 0;
738
2/2
✓ Branch 0 taken 8913 times.
✓ Branch 1 taken 417385 times.
426298 } else if (ret == AVERROR_EOF) {
739 8913 of_output_packet(of, pkt, ost, 1);
740 8913 return ret;
741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 417385 times.
417385 } else if (ret < 0) {
742 av_log(ost, AV_LOG_ERROR, "%s encoding failed\n", type_desc);
743 return ret;
744 }
745
746
2/2
✓ Branch 0 taken 104745 times.
✓ Branch 1 taken 312640 times.
417385 if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
747 104745 update_video_stats(ost, pkt, !!vstats_filename);
748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 417385 times.
417385 if (ost->enc_stats_post.io)
749 enc_stats_write(ost, &ost->enc_stats_post, NULL, pkt,
750 e->packets_encoded);
751
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 417385 times.
417385 if (debug_ts) {
753 av_log(ost, AV_LOG_INFO, "encoder -> type:%s "
754 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
755 "duration:%s duration_time:%s\n",
756 type_desc,
757 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
758 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
759 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
760 }
761
762 417385 av_packet_rescale_ts(pkt, pkt->time_base, ost->mux_timebase);
763 417385 pkt->time_base = ost->mux_timebase;
764
765
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 417385 times.
417385 if (debug_ts) {
766 av_log(ost, AV_LOG_INFO, "encoder -> type:%s "
767 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
768 "duration:%s duration_time:%s\n",
769 type_desc,
770 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
771 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
772 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
773 }
774
775
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 417385 times.
417385 if ((ret = trigger_fix_sub_duration_heartbeat(ost, pkt)) < 0) {
776 av_log(NULL, AV_LOG_ERROR,
777 "Subtitle heartbeat logic failed in %s! (%s)\n",
778 __func__, av_err2str(ret));
779 exit_program(1);
780 }
781
782 417385 e->data_size += pkt->size;
783
784 417385 e->packets_encoded++;
785
786 417385 of_output_packet(of, pkt, ost, 0);
787 }
788
789 av_assert0(0);
790 }
791
792 407196 static int submit_encode_frame(OutputFile *of, OutputStream *ost,
793 AVFrame *frame)
794 {
795 407196 Encoder *e = ost->enc;
796 int ret;
797
798
2/2
✓ Branch 0 taken 345896 times.
✓ Branch 1 taken 61300 times.
407196 if (ost->sq_idx_encode < 0)
799 345896 return encode_frame(of, ost, frame);
800
801
2/2
✓ Branch 0 taken 58542 times.
✓ Branch 1 taken 2758 times.
61300 if (frame) {
802 58542 ret = av_frame_ref(e->sq_frame, frame);
803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58542 times.
58542 if (ret < 0)
804 return ret;
805 58542 frame = e->sq_frame;
806 }
807
808 61300 ret = sq_send(of->sq_encode, ost->sq_idx_encode,
809 61300 SQFRAME(frame));
810
2/2
✓ Branch 0 taken 61299 times.
✓ Branch 1 taken 1 times.
61300 if (ret < 0) {
811
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (frame)
812 1 av_frame_unref(frame);
813
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ret != AVERROR_EOF)
814 return ret;
815 }
816
817 81131 while (1) {
818 142431 AVFrame *enc_frame = e->sq_frame;
819
820 142431 ret = sq_receive(of->sq_encode, ost->sq_idx_encode,
821 142431 SQFRAME(enc_frame));
822
2/2
✓ Branch 0 taken 5383 times.
✓ Branch 1 taken 137048 times.
142431 if (ret == AVERROR_EOF) {
823 5383 enc_frame = NULL;
824
2/2
✓ Branch 0 taken 55917 times.
✓ Branch 1 taken 81131 times.
137048 } else if (ret < 0) {
825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55917 times.
61300 return (ret == AVERROR(EAGAIN)) ? 0 : ret;
826 }
827
828 86514 ret = encode_frame(of, ost, enc_frame);
829
2/2
✓ Branch 0 taken 81131 times.
✓ Branch 1 taken 5383 times.
86514 if (enc_frame)
830 81131 av_frame_unref(enc_frame);
831
2/2
✓ Branch 0 taken 5383 times.
✓ Branch 1 taken 81131 times.
86514 if (ret < 0) {
832
1/2
✓ Branch 0 taken 5383 times.
✗ Branch 1 not taken.
5383 if (ret == AVERROR_EOF)
833 5383 close_output_stream(ost);
834 5383 return ret;
835 }
836 }
837 }
838
839 296163 static void do_audio_out(OutputFile *of, OutputStream *ost,
840 AVFrame *frame)
841 {
842 296163 Encoder *e = ost->enc;
843 296163 AVCodecContext *enc = ost->enc_ctx;
844 int ret;
845
846
1/2
✓ Branch 0 taken 296163 times.
✗ Branch 1 not taken.
296163 if (!(enc->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 296163 times.
296163 enc->ch_layout.nb_channels != frame->ch_layout.nb_channels) {
848 av_log(ost, AV_LOG_ERROR,
849 "Audio channel count changed and encoder does not support parameter changes\n");
850 return;
851 }
852
853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 296163 times.
296163 if (frame->pts == AV_NOPTS_VALUE)
854 frame->pts = e->next_pts;
855 else {
856
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 296028 times.
296163 int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
857 296163 frame->pts =
858 296163 av_rescale_q(frame->pts, frame->time_base, enc->time_base) -
859 296163 av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
860 }
861 296163 frame->time_base = enc->time_base;
862 296163 frame->duration = av_rescale_q(frame->nb_samples, (AVRational){1, frame->sample_rate},
863 enc->time_base);
864
865
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 296163 times.
296163 if (!check_recording_time(ost, frame->pts, frame->time_base))
866 return;
867
868 296163 e->next_pts = frame->pts + frame->nb_samples;
869
870 296163 ret = submit_encode_frame(of, ost, frame);
871
3/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 296132 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
296163 if (ret < 0 && ret != AVERROR_EOF)
872 exit_program(1);
873 }
874
875 104703 static double adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost,
876 AVFrame *frame)
877 {
878 104703 double float_pts = AV_NOPTS_VALUE; // this is identical to frame.pts but with higher precision
879 209406 const int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ?
880
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104703 times.
104703 0 : of->start_time;
881
882 104703 AVCodecContext *const enc = ost->enc_ctx;
883
884 104703 AVRational tb = enc->time_base;
885 104703 AVRational filter_tb = frame->time_base;
886 104703 const int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
887
888
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104703 times.
104703 if (frame->pts == AV_NOPTS_VALUE)
889 goto early_exit;
890
891 104703 tb.den <<= extra_bits;
892 104703 float_pts = av_rescale_q(frame->pts, filter_tb, tb) -
893 104703 av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
894 104703 float_pts /= 1 << extra_bits;
895 // avoid exact midoints to reduce the chance of rounding differences, this
896 // can be removed in case the fps code is changed to work with integers
897
2/2
✓ Branch 0 taken 99718 times.
✓ Branch 1 taken 4985 times.
104703 float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
898
899 104703 frame->pts = av_rescale_q(frame->pts, filter_tb, enc->time_base) -
900 104703 av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
901 104703 frame->time_base = enc->time_base;
902
903 104703 early_exit:
904
905
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104703 times.
104703 if (debug_ts) {
906 av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n",
907 frame ? av_ts2str(frame->pts) : "NULL",
908 (enc && frame) ? av_ts2timestr(frame->pts, &enc->time_base) : "NULL",
909 float_pts,
910 enc ? enc->time_base.num : -1,
911 enc ? enc->time_base.den : -1);
912 }
913
914 104703 return float_pts;
915 }
916
917 /* Convert frame timestamps to the encoder timebase and decide how many times
918 * should this (and possibly previous) frame be repeated in order to conform to
919 * desired target framerate (if any).
920 */
921 107309 static void video_sync_process(OutputFile *of, OutputStream *ost,
922 AVFrame *frame, double duration,
923 int64_t *nb_frames, int64_t *nb_frames_prev)
924 {
925 107309 Encoder *e = ost->enc;
926 double delta0, delta, sync_ipts;
927
928
2/2
✓ Branch 0 taken 2606 times.
✓ Branch 1 taken 104703 times.
107309 if (!frame) {
929 2606 *nb_frames_prev = *nb_frames = mid_pred(e->frames_prev_hist[0],
930 2606 e->frames_prev_hist[1],
931 2606 e->frames_prev_hist[2]);
932 2606 goto finish;
933 }
934
935 104703 sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, frame);
936 /* delta0 is the "drift" between the input frame and
937 * where it would fall in the output. */
938 104703 delta0 = sync_ipts - e->next_pts;
939 104703 delta = delta0 + duration;
940
941 // tracks the number of times the PREVIOUS frame should be duplicated,
942 // mostly for variable framerate (VFR)
943 104703 *nb_frames_prev = 0;
944 /* by default, we output a single frame */
945 104703 *nb_frames = 1;
946
947
4/4
✓ Branch 0 taken 9165 times.
✓ Branch 1 taken 95538 times.
✓ Branch 2 taken 8900 times.
✓ Branch 3 taken 265 times.
104703 if (delta0 < 0 &&
948 8900 delta > 0 &&
949
2/2
✓ Branch 0 taken 8443 times.
✓ Branch 1 taken 457 times.
8900 ost->vsync_method != VSYNC_PASSTHROUGH &&
950
1/2
✓ Branch 0 taken 8443 times.
✗ Branch 1 not taken.
8443 ost->vsync_method != VSYNC_DROP) {
951
2/2
✓ Branch 0 taken 408 times.
✓ Branch 1 taken 8035 times.
8443 if (delta0 < -0.6) {
952 408 av_log(ost, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0);
953 } else
954 8035 av_log(ost, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0);
955 8443 sync_ipts = e->next_pts;
956 8443 duration += delta0;
957 8443 delta0 = 0;
958 }
959
960
4/5
✓ Branch 0 taken 9052 times.
✓ Branch 1 taken 1470 times.
✓ Branch 2 taken 73415 times.
✓ Branch 3 taken 20766 times.
✗ Branch 4 not taken.
104703 switch (ost->vsync_method) {
961 9052 case VSYNC_VSCFR:
962
3/4
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 8620 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 432 times.
9052 if (e->vsync_frame_number == 0 && delta0 >= 0.5) {
963 av_log(ost, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0));
964 delta = duration;
965 delta0 = 0;
966 e->next_pts = llrint(sync_ipts);
967 }
968 case VSYNC_CFR:
969 // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
970
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 10522 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
10522 if (frame_drop_threshold && delta < frame_drop_threshold && e->vsync_frame_number) {
971 *nb_frames = 0;
972
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10522 times.
10522 } else if (delta < -1.1)
973 *nb_frames = 0;
974
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 10172 times.
10522 else if (delta > 1.1) {
975 350 *nb_frames = llrintf(delta);
976
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 348 times.
350 if (delta0 > 1.1)
977 2 *nb_frames_prev = llrintf(delta0 - 0.6);
978 }
979 10522 frame->duration = 1;
980 10522 break;
981 73415 case VSYNC_VFR:
982
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 73247 times.
73415 if (delta <= -0.6)
983 168 *nb_frames = 0;
984
2/2
✓ Branch 0 taken 72717 times.
✓ Branch 1 taken 530 times.
73247 else if (delta > 0.6)
985 72717 e->next_pts = llrint(sync_ipts);
986 73415 frame->duration = duration;
987 73415 break;
988 20766 case VSYNC_DROP:
989 case VSYNC_PASSTHROUGH:
990 20766 frame->duration = duration;
991 20766 e->next_pts = llrint(sync_ipts);
992 20766 break;
993 default:
994 av_assert0(0);
995 }
996
997 107309 finish:
998 107309 memmove(e->frames_prev_hist + 1,
999 107309 e->frames_prev_hist,
1000 sizeof(e->frames_prev_hist[0]) * (FF_ARRAY_ELEMS(e->frames_prev_hist) - 1));
1001 107309 e->frames_prev_hist[0] = *nb_frames_prev;
1002 107309 }
1003
1004 104745 static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf,
1005 AVRational tb, const AVFrame *in_picture,
1006 int dup_idx)
1007 {
1008 double pts_time;
1009
1010
2/2
✓ Branch 0 taken 5098 times.
✓ Branch 1 taken 99647 times.
104745 if (kf->ref_pts == AV_NOPTS_VALUE)
1011 5098 kf->ref_pts = in_picture->pts;
1012
1013 104745 pts_time = (in_picture->pts - kf->ref_pts) * av_q2d(tb);
1014
4/4
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 104706 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 37 times.
104784 if (kf->index < kf->nb_pts &&
1015 39 av_compare_ts(in_picture->pts, tb, kf->pts[kf->index], AV_TIME_BASE_Q) >= 0) {
1016 2 kf->index++;
1017 2 goto force_keyframe;
1018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104743 times.
104743 } else if (kf->pexpr) {
1019 double res;
1020 kf->expr_const_values[FKF_T] = pts_time;
1021 res = av_expr_eval(kf->pexpr,
1022 kf->expr_const_values, NULL);
1023 av_log(logctx, AV_LOG_TRACE,
1024 "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
1025 kf->expr_const_values[FKF_N],
1026 kf->expr_const_values[FKF_N_FORCED],
1027 kf->expr_const_values[FKF_PREV_FORCED_N],
1028 kf->expr_const_values[FKF_T],
1029 kf->expr_const_values[FKF_PREV_FORCED_T],
1030 res);
1031
1032 kf->expr_const_values[FKF_N] += 1;
1033
1034 if (res) {
1035 kf->expr_const_values[FKF_PREV_FORCED_N] = kf->expr_const_values[FKF_N] - 1;
1036 kf->expr_const_values[FKF_PREV_FORCED_T] = kf->expr_const_values[FKF_T];
1037 kf->expr_const_values[FKF_N_FORCED] += 1;
1038 goto force_keyframe;
1039 }
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104743 times.
104743 } else if (kf->type == KF_FORCE_SOURCE &&
1041 (in_picture->flags & AV_FRAME_FLAG_KEY) && !dup_idx) {
1042 goto force_keyframe;
1043
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 104743 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
104743 } else if (kf->type == KF_FORCE_SOURCE_NO_DROP && !dup_idx) {
1044 kf->dropped_keyframe = 0;
1045 if ((in_picture->flags & AV_FRAME_FLAG_KEY) || kf->dropped_keyframe)
1046 goto force_keyframe;
1047 }
1048
1049 104743 return AV_PICTURE_TYPE_NONE;
1050
1051 2 force_keyframe:
1052 2 av_log(logctx, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
1053 2 return AV_PICTURE_TYPE_I;
1054 }
1055
1056 /* May modify/reset frame */
1057 107309 static void do_video_out(OutputFile *of, OutputStream *ost, AVFrame *frame)
1058 {
1059 int ret;
1060 107309 Encoder *e = ost->enc;
1061 107309 AVCodecContext *enc = ost->enc_ctx;
1062 AVRational frame_rate;
1063 int64_t nb_frames, nb_frames_prev, i;
1064 107309 double duration = 0;
1065 107309 AVFilterContext *filter = ost->filter->filter;
1066
1067
2/2
✓ Branch 0 taken 104703 times.
✓ Branch 1 taken 2606 times.
107309 if (frame)
1068 104703 duration = lrintf(frame->duration * av_q2d(frame->time_base) / av_q2d(enc->time_base));
1069
1070
3/4
✓ Branch 0 taken 21326 times.
✓ Branch 1 taken 85983 times.
✓ Branch 2 taken 21326 times.
✗ Branch 3 not taken.
107309 if (duration <= 0 && ost->frame_rate.num)
1071
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 21326 times.
21326 duration = FFMIN(duration, 1/(av_q2d(ost->frame_rate) * av_q2d(enc->time_base)));
1072
1073 107309 frame_rate = av_buffersink_get_frame_rate(filter);
1074
6/6
✓ Branch 0 taken 21326 times.
✓ Branch 1 taken 85983 times.
✓ Branch 2 taken 21231 times.
✓ Branch 3 taken 95 times.
✓ Branch 4 taken 21230 times.
✓ Branch 5 taken 1 times.
107309 if (duration <= 0 && frame_rate.num > 0 && frame_rate.den > 0)
1075 21230 duration = 1/(av_q2d(frame_rate) * av_q2d(enc->time_base));
1076
1077 107309 video_sync_process(of, ost, frame, duration,
1078 &nb_frames, &nb_frames_prev);
1079
1080
4/4
✓ Branch 0 taken 107306 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 168 times.
✓ Branch 3 taken 107138 times.
107309 if (nb_frames_prev == 0 && ost->last_dropped) {
1081 168 nb_frames_drop++;
1082 168 av_log(ost, AV_LOG_VERBOSE,
1083 "*** dropping frame %"PRId64" at ts %"PRId64"\n",
1084 168 e->vsync_frame_number, e->last_frame->pts);
1085 }
1086
5/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 107306 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 49 times.
✓ Branch 5 taken 107260 times.
107309 if (nb_frames > (nb_frames_prev && ost->last_dropped) + (nb_frames > nb_frames_prev)) {
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (nb_frames > dts_error_threshold * 30) {
1088 av_log(ost, AV_LOG_ERROR, "%"PRId64" frame duplication too large, skipping\n", nb_frames - 1);
1089 nb_frames_drop++;
1090 1 return;
1091 }
1092
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
49 nb_frames_dup += nb_frames - (nb_frames_prev && ost->last_dropped) - (nb_frames > nb_frames_prev);
1093 49 av_log(ost, AV_LOG_VERBOSE, "*** %"PRId64" dup!\n", nb_frames - 1);
1094
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (nb_frames_dup > dup_warning) {
1095 av_log(ost, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", dup_warning);
1096 dup_warning *= 10;
1097 }
1098 }
1099
4/4
✓ Branch 0 taken 2774 times.
✓ Branch 1 taken 104535 times.
✓ Branch 2 taken 168 times.
✓ Branch 3 taken 2606 times.
107309 ost->last_dropped = nb_frames == nb_frames_prev && frame;
1100
5/6
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 107141 times.
✓ Branch 2 taken 168 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 41 times.
✓ Branch 5 taken 127 times.
107309 ost->kf.dropped_keyframe = ost->last_dropped && frame && (frame->flags & AV_FRAME_FLAG_KEY);
1101
1102 /* duplicates frame if needed */
1103
2/2
✓ Branch 0 taken 104746 times.
✓ Branch 1 taken 104714 times.
209460 for (i = 0; i < nb_frames; i++) {
1104 AVFrame *in_picture;
1105
1106
3/4
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 104677 times.
✓ Branch 2 taken 69 times.
✗ Branch 3 not taken.
104746 if (i < nb_frames_prev && e->last_frame->buf[0]) {
1107 69 in_picture = e->last_frame;
1108 } else
1109 104677 in_picture = frame;
1110
1111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104746 times.
104746 if (!in_picture)
1112 return;
1113
1114 104746 in_picture->pts = e->next_pts;
1115
1116
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 104745 times.
104746 if (!check_recording_time(ost, in_picture->pts, ost->enc_ctx->time_base))
1117 1 return;
1118
1119 104745 in_picture->quality = enc->global_quality;
1120 104745 in_picture->pict_type = forced_kf_apply(ost, &ost->kf, enc->time_base, in_picture, i);
1121
1122 104745 ret = submit_encode_frame(of, ost, in_picture);
1123
2/2
✓ Branch 0 taken 2594 times.
✓ Branch 1 taken 102151 times.
104745 if (ret == AVERROR_EOF)
1124 2594 break;
1125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 102151 times.
102151 else if (ret < 0)
1126 exit_program(1);
1127
1128 102151 e->next_pts++;
1129 102151 e->vsync_frame_number++;
1130 }
1131
1132 107308 av_frame_unref(e->last_frame);
1133
2/2
✓ Branch 0 taken 104702 times.
✓ Branch 1 taken 2606 times.
107308 if (frame)
1134 104702 av_frame_move_ref(e->last_frame, frame);
1135 }
1136
1137 403472 void enc_frame(OutputStream *ost, AVFrame *frame)
1138 {
1139 403472 OutputFile *of = output_files[ost->file_index];
1140 int ret;
1141
1142 403472 ret = enc_open(ost, frame);
1143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403472 times.
403472 if (ret < 0)
1144 exit_program(1);
1145
1146
2/2
✓ Branch 0 taken 107309 times.
✓ Branch 1 taken 296163 times.
403472 if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO) do_video_out(of, ost, frame);
1147 296163 else do_audio_out(of, ost, frame);
1148 403472 }
1149
1150 6564 void enc_flush(void)
1151 {
1152 int ret;
1153
1154
2/2
✓ Branch 2 taken 6822 times.
✓ Branch 3 taken 6564 times.
13386 for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
1155 6822 OutputFile *of = output_files[ost->file_index];
1156
2/2
✓ Branch 0 taken 2758 times.
✓ Branch 1 taken 4064 times.
6822 if (ost->sq_idx_encode >= 0)
1157 2758 sq_send(of->sq_encode, ost->sq_idx_encode, SQFRAME(NULL));
1158 }
1159
1160
2/2
✓ Branch 2 taken 6822 times.
✓ Branch 3 taken 6564 times.
13386 for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
1161 6822 AVCodecContext *enc = ost->enc_ctx;
1162 6822 OutputFile *of = output_files[ost->file_index];
1163
1164
2/2
✓ Branch 0 taken 496 times.
✓ Branch 1 taken 6326 times.
6822 if (!enc)
1165 496 continue;
1166
1167 // Try to enable encoding with no input frames.
1168 // Maybe we should just let encoding fail instead.
1169
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6325 times.
6326 if (!ost->initialized) {
1170 1 FilterGraph *fg = ost->filter->graph;
1171
1172 1 av_log(ost, AV_LOG_WARNING,
1173 "Finishing stream without any data written to it.\n");
1174
1175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!fg->graph)
1176 continue;
1177
1178 1 ret = enc_open(ost, NULL);
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1180 exit_program(1);
1181 }
1182
1183
4/4
✓ Branch 0 taken 1227 times.
✓ Branch 1 taken 5099 times.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 1189 times.
6326 if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
1184 38 continue;
1185
1186 6288 ret = submit_encode_frame(of, ost, NULL);
1187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6288 times.
6288 if (ret != AVERROR_EOF)
1188 exit_program(1);
1189 }
1190 6564 }
1191