FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg_enc.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 338 523 64.6%
Functions: 17 19 89.5%
Branches: 203 376 54.0%

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