FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_enc.c
Date: 2026-08-15 14:54:27
Exec Total Coverage
Lines: 377 627 60.1%
Functions: 20 24 83.3%
Branches: 221 428 51.6%

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