FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_enc.c
Date: 2026-10-04 22:42:07
Exec Total Coverage
Lines: 378 622 60.8%
Functions: 20 24 83.3%
Branches: 224 422 53.1%

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/mem.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/rational.h"
37 #include "libavutil/time.h"
38 #include "libavutil/timestamp.h"
39
40 #include "libavcodec/avcodec.h"
41
42 typedef struct EncoderPriv {
43 Encoder e;
44
45 void *log_parent;
46 char log_name[32];
47
48 // combined size of all the packets received from the encoder
49 uint64_t data_size;
50
51 // number of packets received from the encoder
52 uint64_t packets_encoded;
53 int got_first_packet;
54
55 int opened;
56 int attach_par;
57
58 Scheduler *sch;
59 unsigned sch_idx;
60 } EncoderPriv;
61
62 719467 static EncoderPriv *ep_from_enc(Encoder *enc)
63 {
64 719467 return (EncoderPriv*)enc;
65 }
66
67 // data that is local to the decoder thread and not visible outside of it
68 typedef struct EncoderThread {
69 AVFrame *frame;
70 AVPacket *pkt;
71 } EncoderThread;
72
73 9551 void enc_free(Encoder **penc)
74 {
75 9551 Encoder *enc = *penc;
76
77
2/2
✓ Branch 0 taken 968 times.
✓ Branch 1 taken 8583 times.
9551 if (!enc)
78 968 return;
79
80
1/2
✓ Branch 0 taken 8583 times.
✗ Branch 1 not taken.
8583 if (enc->enc_ctx)
81 8583 av_freep(&enc->enc_ctx->stats_in);
82 8583 avcodec_free_context(&enc->enc_ctx);
83 8583 av_dict_free(&enc->encoder_opts);
84
85 8583 av_freep(penc);
86 }
87
88 10 static const char *enc_item_name(void *obj)
89 {
90 10 const EncoderPriv *ep = obj;
91
92 10 return ep->log_name;
93 }
94
95 static const AVClass enc_class = {
96 .class_name = "Encoder",
97 .version = LIBAVUTIL_VERSION_INT,
98 .parent_log_context_offset = offsetof(EncoderPriv, log_parent),
99 .item_name = enc_item_name,
100 };
101
102 ✗ static int enc_realloc(Encoder *enc, const AVCodec *codec)
103 {
104 ✗ EncoderPriv *ep = ep_from_enc(enc);
105 ✗ char *stats_in = NULL;
106
107 ✗ if (enc->enc_ctx)
108 ✗ stats_in = enc->enc_ctx->stats_in;
109 ✗ avcodec_free_context(&enc->enc_ctx);
110
111 ✗ ep->opened = 0;
112 ✗ ep->got_first_packet = 0;
113
114 ✗ enc->enc_ctx = avcodec_alloc_context3(codec);
115 ✗ if (!enc->enc_ctx) {
116 ✗ av_freep(&stats_in);
117 ✗ return AVERROR(ENOMEM);
118 }
119
120 ✗ enc->enc_ctx->stats_in = stats_in;
121
122 ✗ return 0;
123 }
124
125 8583 int enc_alloc(Encoder **penc, const AVCodec *codec,
126 Scheduler *sch, unsigned sch_idx, void *log_parent)
127 {
128 EncoderPriv *ep;
129 8583 int ret = 0;
130
131 8583 *penc = NULL;
132
133 8583 ep = av_mallocz(sizeof(*ep));
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8583 times.
8583 if (!ep)
135 ✗ return AVERROR(ENOMEM);
136
137 8583 ep->e.class = &enc_class;
138 8583 ep->log_parent = log_parent;
139
140 8583 ep->sch = sch;
141 8583 ep->sch_idx = sch_idx;
142
143 8583 snprintf(ep->log_name, sizeof(ep->log_name), "enc:%s", codec->name);
144
145 8583 ep->e.enc_ctx = avcodec_alloc_context3(codec);
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8583 times.
8583 if (!ep->e.enc_ctx) {
147 ✗ ret = AVERROR(ENOMEM);
148 ✗ goto fail;
149 }
150
151 8583 *penc = &ep->e;
152
153 8583 return 0;
154 ✗ fail:
155 ✗ enc_free((Encoder**)&ep);
156 ✗ return ret;
157 }
158
159 8580 static int hw_device_setup_for_encode(Encoder *e, AVCodecContext *enc_ctx,
160 AVBufferRef *frames_ref)
161 {
162 const AVCodecHWConfig *config;
163 8580 HWDevice *dev = NULL;
164
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8580 times.
8580 if (frames_ref &&
166 ✗ ((AVHWFramesContext*)frames_ref->data)->format ==
167 ✗ enc_ctx->pix_fmt) {
168 // Matching format, will try to use hw_frames_ctx.
169 } else {
170 8580 frames_ref = NULL;
171 }
172
173 8580 for (int i = 0;; i++) {
174 8580 config = avcodec_get_hw_config(enc_ctx->codec, i);
175
1/2
✓ Branch 0 taken 8580 times.
✗ Branch 1 not taken.
8580 if (!config)
176 8580 break;
177
178 ✗ if (frames_ref &&
179 ✗ config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
180 ✗ (config->pix_fmt == AV_PIX_FMT_NONE ||
181 ✗ config->pix_fmt == enc_ctx->pix_fmt)) {
182 ✗ av_log(e, AV_LOG_VERBOSE, "Using input "
183 "frames context (format %s) with %s encoder.\n",
184 av_get_pix_fmt_name(enc_ctx->pix_fmt),
185 ✗ enc_ctx->codec->name);
186 ✗ enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
187 ✗ if (!enc_ctx->hw_frames_ctx)
188 ✗ return AVERROR(ENOMEM);
189 ✗ return 0;
190 }
191
192 ✗ if (!dev &&
193 ✗ config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
194 ✗ dev = hw_device_get_by_type(config->device_type);
195 }
196
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8580 times.
8580 if (dev) {
198 ✗ av_log(e, AV_LOG_VERBOSE, "Using device %s "
199 "(type %s) with %s encoder.\n", dev->name,
200 ✗ av_hwdevice_get_type_name(dev->type), enc_ctx->codec->name);
201 ✗ enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
202 ✗ if (!enc_ctx->hw_device_ctx)
203 ✗ return AVERROR(ENOMEM);
204 } else {
205 // No device required, or no device available.
206 }
207 8580 return 0;
208 }
209
210 8580 static int apply_enc_options(Encoder *e, AVDictionary **opts)
211 {
212 8580 AVCodecContext *enc_ctx = e->enc_ctx;
213
214 8580 int ret = av_opt_set_dict2(enc_ctx, opts, AV_OPT_SEARCH_CHILDREN);
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8580 times.
8580 if (ret < 0) {
216 ✗ av_log(e, AV_LOG_ERROR, "Error applying encoder options: %s\n",
217 ✗ av_err2str(ret));
218 ✗ return ret;
219 }
220
221 8580 ret = check_avoptions(*opts);
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8580 times.
8580 if (ret < 0)
223 ✗ return ret;
224
225 8580 return 0;
226 }
227
228 8580 static int enc_reopen(void *opaque, const AVFrame *frame,
229 AVDictionary **extra_encoder_opts)
230 {
231 8580 OutputStream *ost = opaque;
232 8580 InputStream *ist = ost->ist;
233 8580 Encoder *e = ost->enc;
234 8580 EncoderPriv *ep = ep_from_enc(e);
235 8580 AVCodecContext *enc_ctx = e->enc_ctx;
236 8580 Decoder *dec = NULL;
237 8580 const AVCodec *enc = enc_ctx->codec;
238 8580 AVDictionary *encoder_opts = NULL;
239 FrameData *fd;
240 int threads_manual;
241 int ret;
242
243 8580 ret = av_dict_copy(&encoder_opts, ost->enc->encoder_opts, 0);
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8580 times.
8580 if (ret < 0)
245 ✗ return ret;
246
247 8580 threads_manual = !!av_dict_get(encoder_opts, "threads", NULL, 0);
248 8580 ret = apply_enc_options(e, &encoder_opts);
249 8580 av_dict_free(&encoder_opts);
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8580 times.
8580 if (ret < 0)
251 ✗ return ret;
252
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8580 times.
8580 if (extra_encoder_opts) {
254 ✗ threads_manual |= !!av_dict_get(*extra_encoder_opts, "threads", NULL, 0);
255 ✗ ret = apply_enc_options(e, extra_encoder_opts);
256 ✗ if (ret < 0)
257 ✗ return ret;
258 }
259
260 // default to automatic thread count
261
2/2
✓ Branch 0 taken 3062 times.
✓ Branch 1 taken 5518 times.
8580 if (!threads_manual)
262 3062 enc_ctx->thread_count = 0;
263
264 // frame is always non-NULL for audio and video
265
4/6
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 8536 times.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 44 times.
8580 av_assert0(frame || (enc->type != AVMEDIA_TYPE_VIDEO && enc->type != AVMEDIA_TYPE_AUDIO));
266
267
2/2
✓ Branch 0 taken 8536 times.
✓ Branch 1 taken 44 times.
8580 if (frame) {
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8536 times.
8536 av_assert0(frame->opaque_ref);
269 8536 fd = (FrameData*)frame->opaque_ref->data;
270
271 8536 ret = clone_side_data(&enc_ctx->decoded_side_data, &enc_ctx->nb_decoded_side_data,
272 8536 fd->side_data, fd->nb_side_data, AV_FRAME_SIDE_DATA_FLAG_UNIQUE);
273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8536 times.
8536 if (ret < 0)
274 ✗ return ret;
275 }
276
277
2/2
✓ Branch 0 taken 7036 times.
✓ Branch 1 taken 1544 times.
8580 if (ist)
278 7036 dec = ist->decoder;
279
280
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8578 times.
8580 if (ost->enc->codec_tag)
281 2 enc_ctx->codec_tag = e->codec_tag;
282 8580 enc_ctx->flags |= e->flags;
283 8580 enc_ctx->flags2 |= e->flags2;
284 8580 enc_ctx->global_quality = e->global_quality;
285
286 // the timebase is chosen by filtering code
287
4/4
✓ Branch 0 taken 7016 times.
✓ Branch 1 taken 1564 times.
✓ Branch 2 taken 6972 times.
✓ Branch 3 taken 44 times.
8580 if (ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO) {
288 8536 enc_ctx->time_base = frame->time_base;
289 8536 enc_ctx->framerate = fd->frame_rate_filter;
290 }
291
292
3/4
✓ Branch 0 taken 1564 times.
✓ Branch 1 taken 6972 times.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
8580 switch (enc_ctx->codec_type) {
293 1564 case AVMEDIA_TYPE_AUDIO:
294
3/6
✓ Branch 0 taken 1564 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1564 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1564 times.
1564 av_assert0(frame->format != AV_SAMPLE_FMT_NONE &&
295 frame->sample_rate > 0 &&
296 frame->ch_layout.nb_channels > 0);
297 1564 enc_ctx->sample_fmt = frame->format;
298 1564 enc_ctx->sample_rate = frame->sample_rate;
299
4/4
✓ Branch 0 taken 1563 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1237 times.
✓ Branch 3 taken 326 times.
1564 if (!enc_ctx->frame_size && (!(enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE) ||
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1237 times.
1237 (enc_ctx->flags2 & AV_CODEC_FLAG2_FIXED_FRAME_SIZE)))
301 326 enc_ctx->frame_size = frame->nb_samples;
302 1564 ret = av_channel_layout_copy(&enc_ctx->ch_layout, &frame->ch_layout);
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1564 times.
1564 if (ret < 0)
304 ✗ return ret;
305
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1564 times.
1564 if (ost->bits_per_raw_sample)
307 ✗ enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
308 else
309
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1564 times.
1564 enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample,
310 av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
311 1564 break;
312
313 6972 case AVMEDIA_TYPE_VIDEO: {
314
3/6
✓ Branch 0 taken 6972 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6972 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6972 times.
6972 av_assert0(frame->format != AV_PIX_FMT_NONE &&
315 frame->width > 0 &&
316 frame->height > 0);
317 6972 enc_ctx->width = frame->width;
318 6972 enc_ctx->height = frame->height;
319 6972 enc_ctx->sample_aspect_ratio =
320 6972 ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6972 times.
6972 av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) :
322 frame->sample_aspect_ratio;
323
324 6972 enc_ctx->pix_fmt = frame->format;
325
326
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6971 times.
6972 if (ost->bits_per_raw_sample)
327 1 enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
328 else
329
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6971 times.
6971 enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample,
330 av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
331
332 /**
333 * The video color properties should always be in sync with the user-
334 * requested values, since we forward them to the filter graph.
335 */
336 6972 enc_ctx->color_range = frame->color_range;
337 6972 enc_ctx->color_primaries = frame->color_primaries;
338 6972 enc_ctx->color_trc = frame->color_trc;
339 6972 enc_ctx->colorspace = frame->colorspace;
340 6972 enc_ctx->alpha_mode = frame->alpha_mode;
341 6972 enc_ctx->chroma_sample_location = frame->chroma_location;
342
343
2/2
✓ Branch 0 taken 6936 times.
✓ Branch 1 taken 36 times.
6972 if (enc_ctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) ||
344
2/2
✓ Branch 0 taken 435 times.
✓ Branch 1 taken 6501 times.
7407 (frame->flags & AV_FRAME_FLAG_INTERLACED)) {
345 471 int top_field_first = !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST);
346
347
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 470 times.
471 if (enc->id == AV_CODEC_ID_MJPEG)
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 enc_ctx->field_order = top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
349 else
350
2/2
✓ Branch 0 taken 362 times.
✓ Branch 1 taken 108 times.
470 enc_ctx->field_order = top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
351 } else
352 6501 enc_ctx->field_order = AV_FIELD_PROGRESSIVE;
353
354 6972 break;
355 }
356 44 case AVMEDIA_TYPE_SUBTITLE:
357 44 enc_ctx->time_base = AV_TIME_BASE_Q;
358
359
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (!enc_ctx->width) {
360 44 enc_ctx->width = ost->ist->par->width;
361 44 enc_ctx->height = ost->ist->par->height;
362 }
363
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 av_assert0(dec);
365
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 2 times.
44 if (dec->subtitle_header) {
366 /* ASS code assumes this buffer is null terminated so add extra byte. */
367 42 enc_ctx->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!enc_ctx->subtitle_header)
369 ✗ return AVERROR(ENOMEM);
370 42 memcpy(enc_ctx->subtitle_header, dec->subtitle_header,
371 42 dec->subtitle_header_size);
372 42 enc_ctx->subtitle_header_size = dec->subtitle_header_size;
373 }
374
375 44 break;
376 ✗ default:
377 ✗ av_assert0(0);
378 break;
379 }
380
381
2/2
✓ Branch 0 taken 7608 times.
✓ Branch 1 taken 972 times.
8580 if (ost->bitexact)
382 7608 enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
383
384
2/2
✓ Branch 0 taken 8491 times.
✓ Branch 1 taken 89 times.
8580 if (enc->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE)
385 8491 enc_ctx->flags |= AV_CODEC_FLAG_COPY_OPAQUE;
386
387 8580 enc_ctx->flags |= AV_CODEC_FLAG_FRAME_DURATION;
388
389
2/2
✓ Branch 0 taken 8536 times.
✓ Branch 1 taken 44 times.
8580 ret = hw_device_setup_for_encode(e, enc_ctx, frame ? frame->hw_frames_ctx : NULL);
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8580 times.
8580 if (ret < 0) {
391 ✗ av_log(e, AV_LOG_ERROR,
392 ✗ "Encoding hardware device setup failed: %s\n", av_err2str(ret));
393 ✗ return ret;
394 }
395
396
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8580 times.
8580 if ((ret = avcodec_open2(enc_ctx, enc, NULL)) < 0) {
397 ✗ if (ret != AVERROR_EXPERIMENTAL)
398 ✗ av_log(e, AV_LOG_ERROR, "Error while opening encoder - maybe "
399 "incorrect parameters such as bit_rate, rate, width or height.\n");
400 ✗ return ret;
401 }
402
403 8580 ep->opened = 1;
404
405
4/4
✓ Branch 0 taken 8535 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 8529 times.
8580 if (enc_ctx->bit_rate && enc_ctx->bit_rate < 1000 &&
406
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
407 6 av_log(e, AV_LOG_WARNING, "The bitrate parameter is set too low."
408 " It takes bits/s as argument, not kbits/s\n");
409
410 8580 return 0;
411 }
412
413 8580 int enc_open(void *opaque, const AVFrame *frame)
414 {
415 8580 OutputStream *ost = opaque;
416 8580 Encoder *e = ost->enc;
417 8580 EncoderPriv *ep = ep_from_enc(e);
418 8580 AVCodecContext *enc_ctx = e->enc_ctx;
419 8580 OutputFile *of = ost->file;
420 8580 int frame_samples = 0;
421 int ret;
422
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8580 times.
8580 if (ep->opened)
424 ✗ return 0;
425
426 8580 ret = enc_reopen(opaque, frame, NULL);
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8580 times.
8580 if (ret < 0)
428 ✗ return ret;
429
430
2/2
✓ Branch 0 taken 327 times.
✓ Branch 1 taken 8253 times.
8580 if (enc_ctx->frame_size)
431 327 frame_samples = enc_ctx->frame_size;
432
433 8580 ret = of_stream_init(of, ost, enc_ctx);
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8580 times.
8580 if (ret < 0)
435 ✗ return ret;
436
437 8580 return frame_samples;
438 }
439
440 540734 static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
441 {
442 540734 OutputFile *of = ost->file;
443
444
4/4
✓ Branch 0 taken 35949 times.
✓ Branch 1 taken 504785 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 35945 times.
576683 if (of->recording_time != INT64_MAX &&
445 35949 av_compare_ts(ts, tb, of->recording_time, AV_TIME_BASE_Q) >= 0) {
446 4 return 0;
447 }
448 540730 return 1;
449 }
450
451 956 static int do_subtitle_out(OutputFile *of, OutputStream *ost, const AVSubtitle *sub,
452 AVPacket *pkt)
453 {
454 956 Encoder *e = ost->enc;
455 956 EncoderPriv *ep = ep_from_enc(e);
456 956 int subtitle_out_max_size = 1024 * 1024;
457 int subtitle_out_size, nb, i, ret;
458 AVCodecContext *enc;
459 int64_t pts;
460
461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 956 times.
956 if (sub->pts == AV_NOPTS_VALUE) {
462 ✗ av_log(e, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
463 ✗ return exit_on_error ? AVERROR(EINVAL) : 0;
464 }
465
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 952 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
956 if ((of->start_time != AV_NOPTS_VALUE && sub->pts < of->start_time))
466 3 return 0;
467
468 953 enc = e->enc_ctx;
469
470 /* Note: DVB subtitle need one packet to draw them and one other
471 packet to clear them */
472 /* XXX: signal it in the codec context ? */
473
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 917 times.
953 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
474 36 nb = 2;
475
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 467 times.
917 else if (enc->codec_id == AV_CODEC_ID_ASS)
476 450 nb = FFMAX(sub->num_rects, 1);
477 else
478 467 nb = 1;
479
480 /* shift timestamp to honor -ss and make check_recording_time() work with -t */
481 953 pts = sub->pts;
482
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 952 times.
953 if (of->start_time != AV_NOPTS_VALUE)
483 1 pts -= of->start_time;
484
2/2
✓ Branch 0 taken 989 times.
✓ Branch 1 taken 953 times.
1942 for (i = 0; i < nb; i++) {
485 989 AVSubtitle local_sub = *sub;
486
487
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 989 times.
989 if (!check_recording_time(ost, pts, AV_TIME_BASE_Q))
488 ✗ return AVERROR_EOF;
489
490 989 ret = av_new_packet(pkt, subtitle_out_max_size);
491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 989 times.
989 if (ret < 0)
492 ✗ return AVERROR(ENOMEM);
493
494 989 local_sub.pts = pts;
495 // start_display_time is required to be 0
496 989 local_sub.pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
497 989 local_sub.end_display_time -= sub->start_display_time;
498 989 local_sub.start_display_time = 0;
499
500
4/4
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 917 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 36 times.
989 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE && i == 1)
501 36 local_sub.num_rects = 0;
502
3/4
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 503 times.
✓ Branch 2 taken 450 times.
✗ Branch 3 not taken.
953 else if (enc->codec_id == AV_CODEC_ID_ASS && sub->num_rects > 0) {
503 450 local_sub.num_rects = 1;
504 450 local_sub.rects += i;
505 }
506
507 989 e->frames_encoded++;
508
509 989 subtitle_out_size = avcodec_encode_subtitle(enc, pkt->data, pkt->size, &local_sub);
510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 989 times.
989 if (subtitle_out_size < 0) {
511 ✗ av_log(e, AV_LOG_FATAL, "Subtitle encoding failed\n");
512 ✗ return subtitle_out_size;
513 }
514
515 989 av_shrink_packet(pkt, subtitle_out_size);
516 989 pkt->time_base = AV_TIME_BASE_Q;
517 989 pkt->pts = pts;
518 989 pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
519
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 917 times.
989 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
520 /* XXX: the pts correction is handled here. Maybe handling
521 it in the codec would be better */
522
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
72 if (i == 0)
523 36 pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
524 else
525 36 pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
526 }
527 989 pkt->dts = pkt->pts;
528
529 989 ret = sch_enc_send(ep->sch, ep->sch_idx, pkt);
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 989 times.
989 if (ret < 0) {
531 ✗ av_packet_unref(pkt);
532 ✗ return ret;
533 }
534 }
535
536 953 return 0;
537 }
538
539 ✗ void enc_stats_write(OutputStream *ost, EncStats *es,
540 const AVFrame *frame, const AVPacket *pkt,
541 uint64_t frame_num)
542 {
543 ✗ Encoder *e = ost->enc;
544 ✗ EncoderPriv *ep = ep_from_enc(e);
545 ✗ AVIOContext *io = es->io;
546 ✗ AVRational tb = frame ? frame->time_base : pkt->time_base;
547 ✗ int64_t pts = frame ? frame->pts : pkt->pts;
548
549 ✗ AVRational tbi = (AVRational){ 0, 1};
550 ✗ int64_t ptsi = INT64_MAX;
551
552 ✗ const FrameData *fd = NULL;
553
554 ✗ if (frame ? frame->opaque_ref : pkt->opaque_ref) {
555 ✗ fd = (const FrameData*)(frame ? frame->opaque_ref->data : pkt->opaque_ref->data);
556 ✗ tbi = fd->dec.tb;
557 ✗ ptsi = fd->dec.pts;
558 }
559
560 ✗ pthread_mutex_lock(&es->lock);
561
562 ✗ for (size_t i = 0; i < es->nb_components; i++) {
563 ✗ const EncStatsComponent *c = &es->components[i];
564
565 ✗ switch (c->type) {
566 ✗ case ENC_STATS_LITERAL: avio_write (io, c->str, c->str_len); continue;
567 ✗ case ENC_STATS_FILE_IDX: avio_printf(io, "%d", ost->file->index); continue;
568 ✗ case ENC_STATS_STREAM_IDX: avio_printf(io, "%d", ost->index); continue;
569 ✗ case ENC_STATS_TIMEBASE: avio_printf(io, "%d/%d", tb.num, tb.den); continue;
570 ✗ case ENC_STATS_TIMEBASE_IN: avio_printf(io, "%d/%d", tbi.num, tbi.den); continue;
571 ✗ case ENC_STATS_PTS: avio_printf(io, "%"PRId64, pts); continue;
572 ✗ case ENC_STATS_PTS_IN: avio_printf(io, "%"PRId64, ptsi); continue;
573 ✗ case ENC_STATS_PTS_TIME: avio_printf(io, "%g", pts * av_q2d(tb)); continue;
574 ✗ case ENC_STATS_PTS_TIME_IN: avio_printf(io, "%g", ptsi == INT64_MAX ?
575 ✗ INFINITY : ptsi * av_q2d(tbi)); continue;
576 ✗ case ENC_STATS_FRAME_NUM: avio_printf(io, "%"PRIu64, frame_num); continue;
577 ✗ case ENC_STATS_FRAME_NUM_IN: avio_printf(io, "%"PRIu64, fd ? fd->dec.frame_num : -1); continue;
578 }
579
580 ✗ if (frame) {
581 ✗ switch (c->type) {
582 ✗ case ENC_STATS_SAMPLE_NUM: avio_printf(io, "%"PRIu64, e->samples_encoded); continue;
583 ✗ case ENC_STATS_NB_SAMPLES: avio_printf(io, "%d", frame->nb_samples); continue;
584 ✗ default: av_assert0(0);
585 }
586 } else {
587 ✗ switch (c->type) {
588 ✗ case ENC_STATS_DTS: avio_printf(io, "%"PRId64, pkt->dts); continue;
589 ✗ case ENC_STATS_DTS_TIME: avio_printf(io, "%g", pkt->dts * av_q2d(tb)); continue;
590 ✗ case ENC_STATS_PKT_SIZE: avio_printf(io, "%d", pkt->size); continue;
591 ✗ case ENC_STATS_KEYFRAME: avio_write(io, (pkt->flags & AV_PKT_FLAG_KEY) ?
592 ✗ "K" : "N", 1); continue;
593 ✗ case ENC_STATS_BITRATE: {
594 ✗ double duration = FFMAX(pkt->duration, 1) * av_q2d(tb);
595 ✗ avio_printf(io, "%g", 8.0 * pkt->size / duration);
596 ✗ continue;
597 }
598 ✗ case ENC_STATS_AVG_BITRATE: {
599 ✗ double duration = pkt->dts * av_q2d(tb);
600 ✗ avio_printf(io, "%g", duration > 0 ? 8.0 * ep->data_size / duration : -1.);
601 ✗ continue;
602 }
603 ✗ default: av_assert0(0);
604 }
605 }
606 }
607 ✗ avio_w8(io, '\n');
608 ✗ avio_flush(io);
609
610 ✗ pthread_mutex_unlock(&es->lock);
611 ✗ }
612
613 ✗ static inline double psnr(double d)
614 {
615 ✗ return -10.0 * log10(d);
616 }
617
618 144492 static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
619 {
620 144492 Encoder *e = ost->enc;
621 144492 EncoderPriv *ep = ep_from_enc(e);
622 144492 const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
623 NULL);
624 144492 AVCodecContext *enc = e->enc_ctx;
625 enum AVPictureType pict_type;
626 int64_t frame_number;
627 double ti1, bitrate, avg_bitrate;
628 144492 double psnr_val = -1;
629 int quality;
630
631
2/2
✓ Branch 0 taken 13109 times.
✓ Branch 1 taken 131383 times.
144492 quality = sd ? AV_RL32(sd) : -1;
632
2/2
✓ Branch 0 taken 13109 times.
✓ Branch 1 taken 131383 times.
144492 pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
633
634 144492 atomic_store(&ost->quality, quality);
635
636
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 144492 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
144492 if ((enc->flags & AV_CODEC_FLAG_PSNR) && sd && sd[5]) {
637 // FIXME the scaling assumes 8bit
638 ✗ double error = AV_RL64(sd + 8) / (enc->width * enc->height * 255.0 * 255.0);
639 ✗ if (error >= 0 && error <= 1)
640 ✗ psnr_val = psnr(error);
641 }
642
643
1/2
✓ Branch 0 taken 144492 times.
✗ Branch 1 not taken.
144492 if (!write_vstats)
644 144492 return 0;
645
646 /* this is executed just the first time update_video_stats is called */
647 ✗ if (!vstats_file) {
648 ✗ vstats_file = fopen(vstats_filename, "w");
649 ✗ if (!vstats_file) {
650 ✗ perror("fopen");
651 ✗ return AVERROR(errno);
652 }
653 }
654
655 ✗ frame_number = ep->packets_encoded;
656 ✗ if (vstats_version <= 1) {
657 ✗ fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number,
658 ✗ quality / (float)FF_QP2LAMBDA);
659 } else {
660 ✗ fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ",
661 ✗ ost->file->index, ost->index, frame_number,
662 ✗ quality / (float)FF_QP2LAMBDA);
663 }
664
665 ✗ if (psnr_val >= 0)
666 ✗ fprintf(vstats_file, "PSNR= %6.2f ", psnr_val);
667
668 ✗ fprintf(vstats_file,"f_size= %6d ", pkt->size);
669 /* compute pts value */
670 ✗ ti1 = pkt->dts * av_q2d(pkt->time_base);
671 ✗ if (ti1 < 0.01)
672 ✗ ti1 = 0.01;
673
674 ✗ bitrate = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0;
675 ✗ avg_bitrate = (double)(ep->data_size * 8) / ti1 / 1000.0;
676 ✗ fprintf(vstats_file, "s_size= %8.0fKiB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
677 ✗ (double)ep->data_size / 1024, ti1, bitrate, avg_bitrate);
678 ✗ fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(pict_type));
679
680 ✗ return 0;
681 }
682
683 548277 static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame,
684 AVPacket *pkt)
685 {
686 548277 Encoder *e = ost->enc;
687 548277 EncoderPriv *ep = ep_from_enc(e);
688 548277 AVCodecContext *enc = e->enc_ctx;
689 548277 const char *type_desc = av_get_media_type_string(enc->codec_type);
690
2/2
✓ Branch 0 taken 539741 times.
✓ Branch 1 taken 8536 times.
548277 const char *action = frame ? "encode" : "flush";
691 int ret;
692
693
2/2
✓ Branch 0 taken 539741 times.
✓ Branch 1 taken 8536 times.
548277 if (frame) {
694 539741 FrameData *fd = frame_data(frame);
695
696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 539741 times.
539741 if (!fd)
697 ✗ return AVERROR(ENOMEM);
698
699 539741 fd->wallclock[LATENCY_PROBE_ENC_PRE] = av_gettime_relative();
700
701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 539741 times.
539741 if (ost->enc_stats_pre.io)
702 ✗ enc_stats_write(ost, &ost->enc_stats_pre, frame, NULL,
703 e->frames_encoded);
704
705 539741 e->frames_encoded++;
706 539741 e->samples_encoded += frame->nb_samples;
707
708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 539741 times.
539741 if (debug_ts) {
709 ✗ av_log(e, AV_LOG_INFO, "encoder <- type:%s "
710 "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
711 type_desc,
712 ✗ av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
713 enc->time_base.num, enc->time_base.den);
714 }
715
716
3/4
✓ Branch 0 taken 45701 times.
✓ Branch 1 taken 494040 times.
✓ Branch 2 taken 45701 times.
✗ Branch 3 not taken.
539741 if (frame->sample_aspect_ratio.num && !ost->frame_aspect_ratio.num)
717 45701 enc->sample_aspect_ratio = frame->sample_aspect_ratio;
718 }
719
720 548277 update_benchmark(NULL);
721
722 548277 ret = avcodec_send_frame(enc, frame);
723
1/6
✓ Branch 0 taken 548277 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
548277 if (ret < 0 && !(ret == AVERROR_EOF && !frame)) {
724 ✗ av_log(e, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n",
725 type_desc);
726 ✗ return ret;
727 }
728
729 533704 while (1) {
730 FrameData *fd;
731
732 1081981 av_packet_unref(pkt);
733
734 1081981 ret = avcodec_receive_packet(enc, pkt);
735 1081981 update_benchmark("%s_%s %d.%d", action, type_desc,
736 of->index, ost->index);
737
738 1081981 pkt->time_base = enc->time_base;
739
740 /* if two pass, output log on success and EOF */
741
7/8
✓ Branch 0 taken 548277 times.
✓ Branch 1 taken 533704 times.
✓ Branch 2 taken 8536 times.
✓ Branch 3 taken 539741 times.
✓ Branch 4 taken 408 times.
✓ Branch 5 taken 541832 times.
✓ Branch 6 taken 408 times.
✗ Branch 7 not taken.
1081981 if ((ret >= 0 || ret == AVERROR_EOF) && ost->logfile && enc->stats_out)
742 408 fprintf(ost->logfile, "%s", enc->stats_out);
743
744
2/2
✓ Branch 0 taken 539741 times.
✓ Branch 1 taken 542240 times.
1081981 if (ret == AVERROR(EAGAIN)) {
745
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 539741 times.
539741 av_assert0(frame); // should never happen during flushing
746 539741 return 0;
747
2/2
✓ Branch 0 taken 8536 times.
✓ Branch 1 taken 533704 times.
542240 } else if (ret < 0) {
748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8536 times.
8536 if (ret != AVERROR_EOF)
749 ✗ av_log(e, AV_LOG_ERROR, "%s encoding failed\n", type_desc);
750 8536 return ret;
751 }
752
753 533704 fd = packet_data(pkt);
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 533704 times.
533704 if (!fd)
755 ✗ return AVERROR(ENOMEM);
756 533704 fd->wallclock[LATENCY_PROBE_ENC_POST] = av_gettime_relative();
757
758 // attach extradata to first packet if the encoder was reinitialized
759
3/6
✓ Branch 0 taken 8528 times.
✓ Branch 1 taken 525176 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8528 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
533704 if (!ep->got_first_packet && ep->packets_encoded && enc->extradata_size) {
760 ✗ uint8_t *extradata = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
761 ✗ enc->extradata_size);
762 ✗ if (!extradata)
763 ✗ return AVERROR(ENOMEM);
764 ✗ memcpy(extradata, enc->extradata, enc->extradata_size);
765 ✗ ep->got_first_packet = 1;
766 }
767 // attach stream parameters to first packet if requested
768 533704 avcodec_parameters_free(&fd->par_enc);
769
2/2
✓ Branch 0 taken 8528 times.
✓ Branch 1 taken 525176 times.
533704 if (!ep->packets_encoded) {
770
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8527 times.
8528 if (ep->attach_par) {
771 1 fd->par_enc = avcodec_parameters_alloc();
772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!fd->par_enc)
773 ✗ return AVERROR(ENOMEM);
774
775 1 ret = avcodec_parameters_from_context(fd->par_enc, enc);
776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
777 ✗ return ret;
778 }
779 8528 ep->got_first_packet = 1;
780 }
781
782 533704 pkt->flags |= AV_PKT_FLAG_TRUSTED;
783
784
2/2
✓ Branch 0 taken 144492 times.
✓ Branch 1 taken 389212 times.
533704 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
785 144492 ret = update_video_stats(ost, pkt, !!vstats_filename);
786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 144492 times.
144492 if (ret < 0)
787 ✗ return ret;
788 }
789
790
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 533704 times.
533704 if (ost->enc_stats_post.io)
791 ✗ enc_stats_write(ost, &ost->enc_stats_post, NULL, pkt,
792 ep->packets_encoded);
793
794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 533704 times.
533704 if (debug_ts) {
795 ✗ av_log(e, AV_LOG_INFO, "encoder -> type:%s "
796 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
797 "duration:%s duration_time:%s\n",
798 type_desc,
799 ✗ av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
800 ✗ av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
801 ✗ av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
802 }
803
804 533704 ep->data_size += pkt->size;
805
806 533704 ep->packets_encoded++;
807
808 533704 ret = sch_enc_send(ep->sch, ep->sch_idx, pkt);
809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 533704 times.
533704 if (ret < 0) {
810 ✗ av_packet_unref(pkt);
811 ✗ return ret;
812 }
813 }
814
815 av_unreachable("encode_frame() loop should return");
816 }
817
818 144492 static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf,
819 const AVFrame *frame)
820 {
821 double pts_time;
822
823
2/2
✓ Branch 0 taken 6969 times.
✓ Branch 1 taken 137523 times.
144492 if (kf->ref_pts == AV_NOPTS_VALUE)
824 6969 kf->ref_pts = frame->pts;
825
826 144492 pts_time = (frame->pts - kf->ref_pts) * av_q2d(frame->time_base);
827
4/4
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 144453 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 37 times.
144531 if (kf->index < kf->nb_pts &&
828 39 av_compare_ts(frame->pts, frame->time_base, kf->pts[kf->index], AV_TIME_BASE_Q) >= 0) {
829 2 kf->index++;
830 16 goto force_keyframe;
831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 144490 times.
144490 } else if (kf->pexpr) {
832 double res;
833 ✗ kf->expr_const_values[FKF_T] = pts_time;
834 ✗ res = av_expr_eval(kf->pexpr,
835 ✗ kf->expr_const_values, NULL);
836 ✗ av_log(logctx, AV_LOG_TRACE,
837 "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
838 kf->expr_const_values[FKF_N],
839 kf->expr_const_values[FKF_N_FORCED],
840 kf->expr_const_values[FKF_PREV_FORCED_N],
841 kf->expr_const_values[FKF_T],
842 kf->expr_const_values[FKF_PREV_FORCED_T],
843 res);
844
845 ✗ kf->expr_const_values[FKF_N] += 1;
846
847 ✗ if (res) {
848 ✗ kf->expr_const_values[FKF_PREV_FORCED_N] = kf->expr_const_values[FKF_N] - 1;
849 ✗ kf->expr_const_values[FKF_PREV_FORCED_T] = kf->expr_const_values[FKF_T];
850 ✗ kf->expr_const_values[FKF_N_FORCED] += 1;
851 ✗ goto force_keyframe;
852 }
853
4/4
✓ Branch 0 taken 1021 times.
✓ Branch 1 taken 143469 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 1012 times.
144490 } else if (kf->type == KF_FORCE_SOURCE && (frame->flags & AV_FRAME_FLAG_KEY)) {
854 9 goto force_keyframe;
855
4/4
✓ Branch 0 taken 392 times.
✓ Branch 1 taken 144089 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 387 times.
144873 } else if (kf->type == KF_FORCE_SCD_METADATA &&
856 392 av_dict_get(frame->metadata, "lavfi.scd.time", NULL, 0)) {
857 5 goto force_keyframe;
858 }
859
860 144476 return AV_PICTURE_TYPE_NONE;
861
862 16 force_keyframe:
863 16 av_log(logctx, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
864 16 return AV_PICTURE_TYPE_I;
865 }
866
867 549380 static int frame_encode(OutputStream *ost, AVFrame *frame, AVPacket *pkt)
868 {
869 549380 Encoder *e = ost->enc;
870 549380 OutputFile *of = ost->file;
871 549380 enum AVMediaType type = ost->type;
872
873
2/2
✓ Branch 0 taken 1099 times.
✓ Branch 1 taken 548281 times.
549380 if (type == AVMEDIA_TYPE_SUBTITLE) {
874
2/2
✓ Branch 0 taken 965 times.
✓ Branch 1 taken 90 times.
1055 const AVSubtitle *subtitle = frame && frame->buf[0] ?
875
2/2
✓ Branch 0 taken 1055 times.
✓ Branch 1 taken 44 times.
2154 (AVSubtitle*)frame->buf[0]->data : NULL;
876
877 // no flushing for subtitles
878
2/2
✓ Branch 0 taken 956 times.
✓ Branch 1 taken 9 times.
965 return subtitle && subtitle->num_rects ?
879
2/2
✓ Branch 0 taken 965 times.
✓ Branch 1 taken 134 times.
2064 do_subtitle_out(of, ost, subtitle, pkt) : 0;
880 }
881
882
2/2
✓ Branch 0 taken 539745 times.
✓ Branch 1 taken 8536 times.
548281 if (frame) {
883
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 539741 times.
539745 if (!check_recording_time(ost, frame->pts, frame->time_base))
884 4 return AVERROR_EOF;
885
886
2/2
✓ Branch 0 taken 144492 times.
✓ Branch 1 taken 395249 times.
539741 if (type == AVMEDIA_TYPE_VIDEO) {
887 144492 frame->quality = e->enc_ctx->global_quality;
888 144492 frame->pict_type = forced_kf_apply(e, &ost->kf, frame);
889 } else {
890
1/2
✓ Branch 0 taken 395249 times.
✗ Branch 1 not taken.
395249 if (!(e->enc_ctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 395249 times.
395249 e->enc_ctx->ch_layout.nb_channels != frame->ch_layout.nb_channels) {
892 ✗ av_log(e, AV_LOG_ERROR,
893 "Audio channel count changed and encoder does not support parameter changes\n");
894 ✗ return 0;
895 }
896 }
897 }
898
899 548277 return encode_frame(of, ost, frame, pkt);
900 }
901
902 8575 static void enc_thread_set_name(const OutputStream *ost)
903 {
904 char name[16];
905 8575 snprintf(name, sizeof(name), "enc%d:%d:%s", ost->file->index, ost->index,
906 8575 ost->enc->enc_ctx->codec->name);
907 8575 ff_thread_setname(name);
908 8575 }
909
910 8581 static void enc_thread_uninit(EncoderThread *et)
911 {
912 8581 av_packet_free(&et->pkt);
913 8581 av_frame_free(&et->frame);
914
915 8581 memset(et, 0, sizeof(*et));
916 8581 }
917
918 8581 static int enc_thread_init(EncoderThread *et)
919 {
920 8581 memset(et, 0, sizeof(*et));
921
922 8581 et->frame = av_frame_alloc();
923
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8581 times.
8581 if (!et->frame)
924 ✗ goto fail;
925
926 8581 et->pkt = av_packet_alloc();
927
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8581 times.
8581 if (!et->pkt)
928 ✗ goto fail;
929
930 8581 return 0;
931
932 ✗ fail:
933 ✗ enc_thread_uninit(et);
934 ✗ return AVERROR(ENOMEM);
935 }
936
937 8580 static int flush_encoder(OutputStream *ost, EncoderThread *et)
938 {
939 8580 Encoder *e = ost->enc;
940 int ret;
941
942 8580 ret = frame_encode(ost, NULL, et->pkt);
943
3/4
✓ Branch 0 taken 8536 times.
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8536 times.
8580 if (ret < 0 && ret != AVERROR_EOF)
944 ✗ av_log(e, AV_LOG_ERROR, "Error flushing encoder: %s\n",
945 ✗ av_err2str(ret));
946
947 8580 return ret;
948 }
949
950 ✗ static int reinit_encoder(OutputStream *ost, EncoderThread *et)
951 {
952 ✗ Encoder *e = ost->enc;
953 ✗ AVDictionary *copy = NULL;
954 ✗ const FrameData *fd = frame_data_c(et->frame);
955 ✗ int force_reinit, ret = AVERROR_BUG;
956
957 ✗ ret = av_dict_copy(&copy, fd->reinit_opts, 0);
958 ✗ if (ret < 0)
959 ✗ return ret;
960
961 ✗ force_reinit = !!av_dict_get(copy, "force_reinit", NULL, 0);
962 ✗ if (force_reinit)
963 ✗ av_dict_set(&copy, "force_reinit", NULL, 0);
964 // Lets try a graceful reconfiguration first
965 ✗ else if (e->enc_ctx->codec->capabilities & AV_CODEC_CAP_ENCODER_RECONF) {
966 ✗ ret = avcodec_encode_reconfigure(e->enc_ctx, &copy);
967 ✗ if (!ret)
968 ✗ goto end;
969
970 ✗ ret = av_dict_copy(&copy, fd->reinit_opts, AV_DICT_DONT_OVERWRITE);
971 ✗ if (ret < 0)
972 ✗ goto end;
973 ✗ av_dict_set(&copy, "force_reinit", NULL, 0);
974
975 ✗ av_log(e, AV_LOG_INFO, "Could not reconfigure the encoder."
976 " Trying to restart it instead\n");
977 }
978
979 // Go ahead and do a full restart of the encoder
980 ✗ ret = flush_encoder(ost, et);
981 ✗ if (ret < 0 && ret != AVERROR_EOF)
982 ✗ goto end;
983
984 ✗ ret = enc_realloc(e, e->enc_ctx->codec);
985 ✗ if (ret < 0)
986 ✗ goto end;
987 ✗ av_log(e, AV_LOG_DEBUG, "Restarting encoder\n");
988 ✗ ret = enc_reopen(ost, et->frame, &copy);
989 ✗ if (ret < 0)
990 ✗ goto end;
991
992 ✗ ret = 0;
993 ✗ end:
994 ✗ av_dict_free(&copy);
995 ✗ return ret;
996 }
997
998 8581 int encoder_thread(void *arg)
999 {
1000 8581 OutputStream *ost = arg;
1001 8581 Encoder *e = ost->enc;
1002 8581 EncoderPriv *ep = ep_from_enc(e);
1003 EncoderThread et;
1004 const FrameData *fd;
1005 8581 int ret = 0, input_status = 0;
1006 8581 int name_set = 0;
1007
1008 8581 ret = enc_thread_init(&et);
1009
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8581 times.
8581 if (ret < 0)
1010 ✗ goto finish;
1011
1012 /* Open the subtitle encoders immediately. AVFrame-based encoders
1013 * are opened through a callback from the scheduler once they get
1014 * their first frame
1015 *
1016 * N.B.: because the callback is called from a different thread,
1017 * enc_ctx MUST NOT be accessed before sch_enc_receive() returns
1018 * for the first time for audio/video. */
1019
4/4
✓ Branch 0 taken 1608 times.
✓ Branch 1 taken 6973 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 1564 times.
8581 if (ost->type != AVMEDIA_TYPE_VIDEO && ost->type != AVMEDIA_TYPE_AUDIO) {
1020 44 ret = enc_open(ost, NULL);
1021
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (ret < 0)
1022 ✗ goto finish;
1023 }
1024
1025
1/2
✓ Branch 0 taken 549377 times.
✗ Branch 1 not taken.
549377 while (!input_status) {
1026 549377 input_status = sch_enc_receive(ep->sch, ep->sch_idx, et.frame);
1027
2/2
✓ Branch 0 taken 8577 times.
✓ Branch 1 taken 540800 times.
549377 if (input_status < 0) {
1028
1/2
✓ Branch 0 taken 8577 times.
✗ Branch 1 not taken.
8577 if (input_status == AVERROR_EOF) {
1029 8577 av_log(e, AV_LOG_VERBOSE, "Encoder thread received EOF\n");
1030
2/2
✓ Branch 0 taken 8576 times.
✓ Branch 1 taken 1 times.
8577 if (ep->opened)
1031 8576 break;
1032
1033 1 av_log(e, AV_LOG_ERROR, "Could not open encoder before EOF\n");
1034 1 ret = AVERROR(EINVAL);
1035 } else {
1036 ✗ av_log(e, AV_LOG_ERROR, "Error receiving a frame for encoding: %s\n",
1037 ✗ av_err2str(ret));
1038 ✗ ret = input_status;
1039 }
1040 1 goto finish;
1041 }
1042
1043
2/2
✓ Branch 0 taken 8575 times.
✓ Branch 1 taken 532225 times.
540800 if (!name_set) {
1044 8575 enc_thread_set_name(ost);
1045 8575 name_set = 1;
1046 }
1047
1048 540800 fd = frame_data_c(et.frame);
1049
2/4
✓ Branch 0 taken 540800 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 540800 times.
540800 if (fd && fd->reinit_opts) {
1050 ✗ ret = reinit_encoder(ost, &et);
1051 ✗ if (ret < 0) {
1052 ✗ av_log(e, AV_LOG_ERROR, "Error reconfiguring or restarting encoder: %s\n",
1053 ✗ av_err2str(ret));
1054 ✗ goto finish;
1055 }
1056 }
1057
1058 540800 ret = frame_encode(ost, et.frame, et.pkt);
1059
1060 540800 av_packet_unref(et.pkt);
1061 540800 av_frame_unref(et.frame);
1062
1063
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 540796 times.
540800 if (ret < 0) {
1064
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (ret == AVERROR_EOF)
1065 4 av_log(e, AV_LOG_VERBOSE, "Encoder returned EOF, finishing\n");
1066 else
1067 ✗ av_log(e, AV_LOG_ERROR, "Error encoding a frame: %s\n",
1068 ✗ av_err2str(ret));
1069 4 break;
1070 }
1071 }
1072
1073 // flush the encoder
1074
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8576 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8580 if (ret == 0 || ret == AVERROR_EOF)
1075 8580 ret = flush_encoder(ost, &et);
1076
1077 // EOF is normal thread termination
1078
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 8536 times.
8580 if (ret == AVERROR_EOF)
1079 8536 ret = 0;
1080
1081 44 finish:
1082 8581 enc_thread_uninit(&et);
1083
1084 8581 return ret;
1085 }
1086
1087 1 int enc_loopback(Encoder *enc)
1088 {
1089 1 EncoderPriv *ep = ep_from_enc(enc);
1090 1 ep->attach_par = 1;
1091 1 return ep->sch_idx;
1092 }
1093