FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg.c
Date: 2022-07-04 19:11:22
Exec Total Coverage
Lines: 1924 2552 75.4%
Branches: 1411 2034 69.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2003 Fabrice Bellard
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * multimedia converter based on the FFmpeg libraries
24 */
25
26 #include "config.h"
27 #include <ctype.h>
28 #include <string.h>
29 #include <math.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdatomic.h>
34 #include <stdint.h>
35
36 #if HAVE_IO_H
37 #include <io.h>
38 #endif
39 #if HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42
43 #include "libavformat/avformat.h"
44 #include "libavdevice/avdevice.h"
45 #include "libswresample/swresample.h"
46 #include "libavutil/opt.h"
47 #include "libavutil/channel_layout.h"
48 #include "libavutil/parseutils.h"
49 #include "libavutil/samplefmt.h"
50 #include "libavutil/fifo.h"
51 #include "libavutil/hwcontext.h"
52 #include "libavutil/internal.h"
53 #include "libavutil/intreadwrite.h"
54 #include "libavutil/dict.h"
55 #include "libavutil/display.h"
56 #include "libavutil/mathematics.h"
57 #include "libavutil/pixdesc.h"
58 #include "libavutil/avstring.h"
59 #include "libavutil/libm.h"
60 #include "libavutil/imgutils.h"
61 #include "libavutil/timestamp.h"
62 #include "libavutil/bprint.h"
63 #include "libavutil/time.h"
64 #include "libavutil/thread.h"
65 #include "libavutil/threadmessage.h"
66 #include "libavcodec/mathops.h"
67 #include "libavcodec/version.h"
68 #include "libavformat/os_support.h"
69
70 # include "libavfilter/avfilter.h"
71 # include "libavfilter/buffersrc.h"
72 # include "libavfilter/buffersink.h"
73
74 #if HAVE_SYS_RESOURCE_H
75 #include <sys/time.h>
76 #include <sys/types.h>
77 #include <sys/resource.h>
78 #elif HAVE_GETPROCESSTIMES
79 #include <windows.h>
80 #endif
81 #if HAVE_GETPROCESSMEMORYINFO
82 #include <windows.h>
83 #include <psapi.h>
84 #endif
85 #if HAVE_SETCONSOLECTRLHANDLER
86 #include <windows.h>
87 #endif
88
89
90 #if HAVE_SYS_SELECT_H
91 #include <sys/select.h>
92 #endif
93
94 #if HAVE_TERMIOS_H
95 #include <fcntl.h>
96 #include <sys/ioctl.h>
97 #include <sys/time.h>
98 #include <termios.h>
99 #elif HAVE_KBHIT
100 #include <conio.h>
101 #endif
102
103 #include <time.h>
104
105 #include "ffmpeg.h"
106 #include "cmdutils.h"
107
108 #include "libavutil/avassert.h"
109
110 const char program_name[] = "ffmpeg";
111 const int program_birth_year = 2000;
112
113 static FILE *vstats_file;
114
115 const char *const forced_keyframes_const_names[] = {
116 "n",
117 "n_forced",
118 "prev_forced_n",
119 "prev_forced_t",
120 "t",
121 NULL
122 };
123
124 typedef struct BenchmarkTimeStamps {
125 int64_t real_usec;
126 int64_t user_usec;
127 int64_t sys_usec;
128 } BenchmarkTimeStamps;
129
130 static BenchmarkTimeStamps get_benchmark_time_stamps(void);
131 static int64_t getmaxrss(void);
132 static int ifilter_has_all_input_formats(FilterGraph *fg);
133
134 static int64_t nb_frames_dup = 0;
135 static uint64_t dup_warning = 1000;
136 static int64_t nb_frames_drop = 0;
137 static int64_t decode_error_stat[2];
138 unsigned nb_output_dumped = 0;
139
140 int want_sdp = 1;
141
142 static BenchmarkTimeStamps current_time;
143 AVIOContext *progress_avio = NULL;
144
145 static uint8_t *subtitle_out;
146
147 InputStream **input_streams = NULL;
148 int nb_input_streams = 0;
149 InputFile **input_files = NULL;
150 int nb_input_files = 0;
151
152 OutputStream **output_streams = NULL;
153 int nb_output_streams = 0;
154 OutputFile **output_files = NULL;
155 int nb_output_files = 0;
156
157 FilterGraph **filtergraphs;
158 int nb_filtergraphs;
159
160 #if HAVE_TERMIOS_H
161
162 /* init terminal so that we can grab keys */
163 static struct termios oldtty;
164 static int restore_tty;
165 #endif
166
167 #if HAVE_THREADS
168 static void free_input_threads(void);
169 #endif
170
171 /* sub2video hack:
172 Convert subtitles to video with alpha to insert them in filter graphs.
173 This is a temporary solution until libavfilter gets real subtitles support.
174 */
175
176 180 static int sub2video_get_blank_frame(InputStream *ist)
177 {
178 int ret;
179 180 AVFrame *frame = ist->sub2video.frame;
180
181 180 av_frame_unref(frame);
182
1/2
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
180 ist->sub2video.frame->width = ist->dec_ctx->width ? ist->dec_ctx->width : ist->sub2video.w;
183
1/2
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
180 ist->sub2video.frame->height = ist->dec_ctx->height ? ist->dec_ctx->height : ist->sub2video.h;
184 180 ist->sub2video.frame->format = AV_PIX_FMT_RGB32;
185
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 180 times.
180 if ((ret = av_frame_get_buffer(frame, 0)) < 0)
186 return ret;
187 180 memset(frame->data[0], 0, frame->height * frame->linesize[0]);
188 180 return 0;
189 }
190
191 89 static void sub2video_copy_rect(uint8_t *dst, int dst_linesize, int w, int h,
192 AVSubtitleRect *r)
193 {
194 uint32_t *pal, *dst2;
195 uint8_t *src, *src2;
196 int x, y;
197
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89 times.
89 if (r->type != SUBTITLE_BITMAP) {
199 av_log(NULL, AV_LOG_WARNING, "sub2video: non-bitmap subtitle\n");
200 return;
201 }
202
4/8
✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 89 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 89 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 89 times.
89 if (r->x < 0 || r->x + r->w > w || r->y < 0 || r->y + r->h > h) {
203 av_log(NULL, AV_LOG_WARNING, "sub2video: rectangle (%d %d %d %d) overflowing %d %d\n",
204 r->x, r->y, r->w, r->h, w, h
205 );
206 return;
207 }
208
209 89 dst += r->y * dst_linesize + r->x * 4;
210 89 src = r->data[0];
211 89 pal = (uint32_t *)r->data[1];
212
2/2
✓ Branch 0 taken 3291 times.
✓ Branch 1 taken 89 times.
3380 for (y = 0; y < r->h; y++) {
213 3291 dst2 = (uint32_t *)dst;
214 3291 src2 = src;
215
2/2
✓ Branch 0 taken 1036075 times.
✓ Branch 1 taken 3291 times.
1039366 for (x = 0; x < r->w; x++)
216 1036075 *(dst2++) = pal[*(src2++)];
217 3291 dst += dst_linesize;
218 3291 src += r->linesize[0];
219 }
220 }
221
222 429 static void sub2video_push_ref(InputStream *ist, int64_t pts)
223 {
224 429 AVFrame *frame = ist->sub2video.frame;
225 int i;
226 int ret;
227
228 av_assert1(frame->data[0]);
229 429 ist->sub2video.last_pts = frame->pts = pts;
230
2/2
✓ Branch 0 taken 429 times.
✓ Branch 1 taken 429 times.
858 for (i = 0; i < ist->nb_filters; i++) {
231 429 ret = av_buffersrc_add_frame_flags(ist->filters[i]->filter, frame,
232 AV_BUFFERSRC_FLAG_KEEP_REF |
233 AV_BUFFERSRC_FLAG_PUSH);
234
2/4
✓ Branch 0 taken 429 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 429 times.
429 if (ret != AVERROR_EOF && ret < 0)
235 av_log(NULL, AV_LOG_WARNING, "Error while add the frame to buffer source(%s).\n",
236 av_err2str(ret));
237 }
238 429 }
239
240 215 void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub)
241 {
242 215 AVFrame *frame = ist->sub2video.frame;
243 int8_t *dst;
244 int dst_linesize;
245 int num_rects, i;
246 int64_t pts, end_pts;
247
248
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 180 times.
215 if (!frame)
249 35 return;
250
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 92 times.
180 if (sub) {
251 88 pts = av_rescale_q(sub->pts + sub->start_display_time * 1000LL,
252 88 AV_TIME_BASE_Q, ist->st->time_base);
253 88 end_pts = av_rescale_q(sub->pts + sub->end_display_time * 1000LL,
254 88 AV_TIME_BASE_Q, ist->st->time_base);
255 88 num_rects = sub->num_rects;
256 } else {
257 /* If we are initializing the system, utilize current heartbeat
258 PTS as the start time, and show until the following subpicture
259 is received. Otherwise, utilize the previous subpicture's end time
260 as the fall-back value. */
261 184 pts = ist->sub2video.initialize ?
262
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 4 times.
92 heartbeat_pts : ist->sub2video.end_pts;
263 92 end_pts = INT64_MAX;
264 92 num_rects = 0;
265 }
266
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 180 times.
180 if (sub2video_get_blank_frame(ist) < 0) {
267 av_log(ist->dec_ctx, AV_LOG_ERROR,
268 "Impossible to get a blank canvas.\n");
269 return;
270 }
271 180 dst = frame->data [0];
272 180 dst_linesize = frame->linesize[0];
273
2/2
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 180 times.
269 for (i = 0; i < num_rects; i++)
274 89 sub2video_copy_rect(dst, dst_linesize, frame->width, frame->height, sub->rects[i]);
275 180 sub2video_push_ref(ist, pts);
276 180 ist->sub2video.end_pts = end_pts;
277 180 ist->sub2video.initialize = 0;
278 }
279
280 411817 static void sub2video_heartbeat(InputStream *ist, int64_t pts)
281 {
282 411817 InputFile *infile = input_files[ist->file_index];
283 int i, j, nb_reqs;
284 int64_t pts2;
285
286 /* When a frame is read from a file, examine all sub2video streams in
287 the same file and send the sub2video frame again. Otherwise, decoded
288 video frames could be accumulating in the filter graph while a filter
289 (possibly overlay) is desperately waiting for a subtitle frame. */
290
2/2
✓ Branch 0 taken 460058 times.
✓ Branch 1 taken 411817 times.
871875 for (i = 0; i < infile->nb_streams; i++) {
291 460058 InputStream *ist2 = input_streams[infile->ist_index + i];
292
2/2
✓ Branch 0 taken 459113 times.
✓ Branch 1 taken 945 times.
460058 if (!ist2->sub2video.frame)
293 459113 continue;
294 /* subtitles seem to be usually muxed ahead of other streams;
295 if not, subtracting a larger time here is necessary */
296 945 pts2 = av_rescale_q(pts, ist->st->time_base, ist2->st->time_base) - 1;
297 /* do not send the heartbeat frame if the subtitle is already ahead */
298
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 937 times.
945 if (pts2 <= ist2->sub2video.last_pts)
299 8 continue;
300
3/4
✓ Branch 0 taken 849 times.
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 849 times.
937 if (pts2 >= ist2->sub2video.end_pts || ist2->sub2video.initialize)
301 /* if we have hit the end of the current displayed subpicture,
302 or if we need to initialize the system, update the
303 overlayed subpicture and its start/end times */
304 88 sub2video_update(ist2, pts2 + 1, NULL);
305
2/2
✓ Branch 0 taken 937 times.
✓ Branch 1 taken 937 times.
1874 for (j = 0, nb_reqs = 0; j < ist2->nb_filters; j++)
306 937 nb_reqs += av_buffersrc_get_nb_failed_requests(ist2->filters[j]->filter);
307
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 688 times.
937 if (nb_reqs)
308 249 sub2video_push_ref(ist2, pts2);
309 }
310 411817 }
311
312 39 static void sub2video_flush(InputStream *ist)
313 {
314 int i;
315 int ret;
316
317
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (ist->sub2video.end_pts < INT64_MAX)
318 39 sub2video_update(ist, INT64_MAX, NULL);
319
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 39 times.
43 for (i = 0; i < ist->nb_filters; i++) {
320 4 ret = av_buffersrc_add_frame(ist->filters[i]->filter, NULL);
321
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (ret != AVERROR_EOF && ret < 0)
322 av_log(NULL, AV_LOG_WARNING, "Flush the frame error.\n");
323 }
324 39 }
325
326 /* end of sub2video hack */
327
328 12497 static void term_exit_sigsafe(void)
329 {
330 #if HAVE_TERMIOS_H
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12497 times.
12497 if(restore_tty)
332 tcsetattr (0, TCSANOW, &oldtty);
333 #endif
334 12497 }
335
336 12497 void term_exit(void)
337 {
338 12497 av_log(NULL, AV_LOG_QUIET, "%s", "");
339 12497 term_exit_sigsafe();
340 12497 }
341
342 static volatile int received_sigterm = 0;
343 static volatile int received_nb_signals = 0;
344 static atomic_int transcode_init_done = ATOMIC_VAR_INIT(0);
345 static volatile int ffmpeg_exited = 0;
346 int main_return_code = 0;
347 static int64_t copy_ts_first_pts = AV_NOPTS_VALUE;
348
349 static void
350 sigterm_handler(int sig)
351 {
352 int ret;
353 received_sigterm = sig;
354 received_nb_signals++;
355 term_exit_sigsafe();
356 if(received_nb_signals > 3) {
357 ret = write(2/*STDERR_FILENO*/, "Received > 3 system signals, hard exiting\n",
358 strlen("Received > 3 system signals, hard exiting\n"));
359 if (ret < 0) { /* Do nothing */ };
360 exit(123);
361 }
362 }
363
364 #if HAVE_SETCONSOLECTRLHANDLER
365 static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
366 {
367 av_log(NULL, AV_LOG_DEBUG, "\nReceived windows signal %ld\n", fdwCtrlType);
368
369 switch (fdwCtrlType)
370 {
371 case CTRL_C_EVENT:
372 case CTRL_BREAK_EVENT:
373 sigterm_handler(SIGINT);
374 return TRUE;
375
376 case CTRL_CLOSE_EVENT:
377 case CTRL_LOGOFF_EVENT:
378 case CTRL_SHUTDOWN_EVENT:
379 sigterm_handler(SIGTERM);
380 /* Basically, with these 3 events, when we return from this method the
381 process is hard terminated, so stall as long as we need to
382 to try and let the main thread(s) clean up and gracefully terminate
383 (we have at most 5 seconds, but should be done far before that). */
384 while (!ffmpeg_exited) {
385 Sleep(0);
386 }
387 return TRUE;
388
389 default:
390 av_log(NULL, AV_LOG_ERROR, "Received unknown windows signal %ld\n", fdwCtrlType);
391 return FALSE;
392 }
393 }
394 #endif
395
396 #ifdef __linux__
397 #define SIGNAL(sig, func) \
398 do { \
399 action.sa_handler = func; \
400 sigaction(sig, &action, NULL); \
401 } while (0)
402 #else
403 #define SIGNAL(sig, func) \
404 signal(sig, func)
405 #endif
406
407 6248 void term_init(void)
408 {
409 #if defined __linux__
410 6248 struct sigaction action = {0};
411 6248 action.sa_handler = sigterm_handler;
412
413 /* block other interrupts while processing this one */
414 6248 sigfillset(&action.sa_mask);
415
416 /* restart interruptible functions (i.e. don't fail with EINTR) */
417 6248 action.sa_flags = SA_RESTART;
418 #endif
419
420 #if HAVE_TERMIOS_H
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6248 times.
6248 if (stdin_interaction) {
422 struct termios tty;
423 if (tcgetattr (0, &tty) == 0) {
424 oldtty = tty;
425 restore_tty = 1;
426
427 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
428 |INLCR|IGNCR|ICRNL|IXON);
429 tty.c_oflag |= OPOST;
430 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
431 tty.c_cflag &= ~(CSIZE|PARENB);
432 tty.c_cflag |= CS8;
433 tty.c_cc[VMIN] = 1;
434 tty.c_cc[VTIME] = 0;
435
436 tcsetattr (0, TCSANOW, &tty);
437 }
438 SIGNAL(SIGQUIT, sigterm_handler); /* Quit (POSIX). */
439 }
440 #endif
441
442 6248 SIGNAL(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
443 6248 SIGNAL(SIGTERM, sigterm_handler); /* Termination (ANSI). */
444 #ifdef SIGXCPU
445 6248 SIGNAL(SIGXCPU, sigterm_handler);
446 #endif
447 #ifdef SIGPIPE
448 6248 signal(SIGPIPE, SIG_IGN); /* Broken pipe (POSIX). */
449 #endif
450 #if HAVE_SETCONSOLECTRLHANDLER
451 SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE);
452 #endif
453 6248 }
454
455 /* read a key without blocking */
456 static int read_key(void)
457 {
458 unsigned char ch;
459 #if HAVE_TERMIOS_H
460 int n = 1;
461 struct timeval tv;
462 fd_set rfds;
463
464 FD_ZERO(&rfds);
465 FD_SET(0, &rfds);
466 tv.tv_sec = 0;
467 tv.tv_usec = 0;
468 n = select(1, &rfds, NULL, NULL, &tv);
469 if (n > 0) {
470 n = read(0, &ch, 1);
471 if (n == 1)
472 return ch;
473
474 return n;
475 }
476 #elif HAVE_KBHIT
477 # if HAVE_PEEKNAMEDPIPE
478 static int is_pipe;
479 static HANDLE input_handle;
480 DWORD dw, nchars;
481 if(!input_handle){
482 input_handle = GetStdHandle(STD_INPUT_HANDLE);
483 is_pipe = !GetConsoleMode(input_handle, &dw);
484 }
485
486 if (is_pipe) {
487 /* When running under a GUI, you will end here. */
488 if (!PeekNamedPipe(input_handle, NULL, 0, NULL, &nchars, NULL)) {
489 // input pipe may have been closed by the program that ran ffmpeg
490 return -1;
491 }
492 //Read it
493 if(nchars != 0) {
494 read(0, &ch, 1);
495 return ch;
496 }else{
497 return -1;
498 }
499 }
500 # endif
501 if(kbhit())
502 return(getch());
503 #endif
504 return -1;
505 }
506
507 636083 static int decode_interrupt_cb(void *ctx)
508 {
509 636083 return received_nb_signals > atomic_load(&transcode_init_done);
510 }
511
512 const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
513
514 6249 static void ffmpeg_cleanup(int ret)
515 {
516 int i, j;
517
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6249 times.
6249 if (do_benchmark) {
519 int maxrss = getmaxrss() / 1024;
520 av_log(NULL, AV_LOG_INFO, "bench: maxrss=%ikB\n", maxrss);
521 }
522
523
2/2
✓ Branch 0 taken 6007 times.
✓ Branch 1 taken 6249 times.
12256 for (i = 0; i < nb_filtergraphs; i++) {
524 6007 FilterGraph *fg = filtergraphs[i];
525 6007 avfilter_graph_free(&fg->graph);
526
2/2
✓ Branch 0 taken 5994 times.
✓ Branch 1 taken 6007 times.
12001 for (j = 0; j < fg->nb_inputs; j++) {
527 5994 InputFilter *ifilter = fg->inputs[j];
528 5994 struct InputStream *ist = ifilter->ist;
529
530
1/2
✓ Branch 0 taken 5994 times.
✗ Branch 1 not taken.
5994 if (ifilter->frame_queue) {
531 AVFrame *frame;
532
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5994 times.
5994 while (av_fifo_read(ifilter->frame_queue, &frame, 1) >= 0)
533 av_frame_free(&frame);
534 5994 av_fifo_freep2(&ifilter->frame_queue);
535 }
536 5994 av_freep(&ifilter->displaymatrix);
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5994 times.
5994 if (ist->sub2video.sub_queue) {
538 AVSubtitle sub;
539 while (av_fifo_read(ist->sub2video.sub_queue, &sub, 1) >= 0)
540 avsubtitle_free(&sub);
541 av_fifo_freep2(&ist->sub2video.sub_queue);
542 }
543 5994 av_buffer_unref(&ifilter->hw_frames_ctx);
544 5994 av_freep(&ifilter->name);
545 5994 av_freep(&fg->inputs[j]);
546 }
547 6007 av_freep(&fg->inputs);
548
2/2
✓ Branch 0 taken 6017 times.
✓ Branch 1 taken 6007 times.
12024 for (j = 0; j < fg->nb_outputs; j++) {
549 6017 OutputFilter *ofilter = fg->outputs[j];
550
551 6017 avfilter_inout_free(&ofilter->out_tmp);
552 6017 av_freep(&ofilter->name);
553 6017 av_channel_layout_uninit(&ofilter->ch_layout);
554 6017 av_freep(&fg->outputs[j]);
555 }
556 6007 av_freep(&fg->outputs);
557 6007 av_freep(&fg->graph_desc);
558
559 6007 av_freep(&filtergraphs[i]);
560 }
561 6249 av_freep(&filtergraphs);
562
563 6249 av_freep(&subtitle_out);
564
565 /* close files */
566
2/2
✓ Branch 0 taken 6249 times.
✓ Branch 1 taken 6249 times.
12498 for (i = 0; i < nb_output_files; i++)
567 6249 of_close(&output_files[i]);
568
569
2/2
✓ Branch 0 taken 6479 times.
✓ Branch 1 taken 6249 times.
12728 for (i = 0; i < nb_output_streams; i++) {
570 6479 OutputStream *ost = output_streams[i];
571
572
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6479 times.
6479 if (!ost)
573 continue;
574
575 6479 av_bsf_free(&ost->bsf_ctx);
576
577 6479 av_frame_free(&ost->filtered_frame);
578 6479 av_frame_free(&ost->last_frame);
579 6479 av_packet_free(&ost->pkt);
580 6479 av_dict_free(&ost->encoder_opts);
581
582 6479 av_freep(&ost->forced_keyframes);
583 6479 av_expr_free(ost->forced_keyframes_pexpr);
584 6479 av_freep(&ost->avfilter);
585 6479 av_freep(&ost->logfile_prefix);
586
587 6479 av_freep(&ost->audio_channels_map);
588 6479 ost->audio_channels_mapped = 0;
589
590 6479 av_dict_free(&ost->sws_dict);
591 6479 av_dict_free(&ost->swr_opts);
592
593 6479 avcodec_free_context(&ost->enc_ctx);
594 6479 avcodec_parameters_free(&ost->ref_par);
595
596
1/2
✓ Branch 0 taken 6479 times.
✗ Branch 1 not taken.
6479 if (ost->muxing_queue) {
597 AVPacket *pkt;
598
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6479 times.
6479 while (av_fifo_read(ost->muxing_queue, &pkt, 1) >= 0)
599 av_packet_free(&pkt);
600 6479 av_fifo_freep2(&ost->muxing_queue);
601 }
602
603 6479 av_freep(&output_streams[i]);
604 }
605 #if HAVE_THREADS
606 6249 free_input_threads();
607 #endif
608
2/2
✓ Branch 0 taken 6280 times.
✓ Branch 1 taken 6249 times.
12529 for (i = 0; i < nb_input_files; i++) {
609 6280 avformat_close_input(&input_files[i]->ctx);
610 6280 av_packet_free(&input_files[i]->pkt);
611 6280 av_freep(&input_files[i]);
612 }
613
2/2
✓ Branch 0 taken 6695 times.
✓ Branch 1 taken 6249 times.
12944 for (i = 0; i < nb_input_streams; i++) {
614 6695 InputStream *ist = input_streams[i];
615
616 6695 av_frame_free(&ist->decoded_frame);
617 6695 av_packet_free(&ist->pkt);
618 6695 av_dict_free(&ist->decoder_opts);
619 6695 avsubtitle_free(&ist->prev_sub.subtitle);
620 6695 av_frame_free(&ist->sub2video.frame);
621 6695 av_freep(&ist->filters);
622 6695 av_freep(&ist->hwaccel_device);
623 6695 av_freep(&ist->dts_buffer);
624
625 6695 avcodec_free_context(&ist->dec_ctx);
626
627 6695 av_freep(&input_streams[i]);
628 }
629
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6249 times.
6249 if (vstats_file) {
631 if (fclose(vstats_file))
632 av_log(NULL, AV_LOG_ERROR,
633 "Error closing vstats file, loss of information possible: %s\n",
634 av_err2str(AVERROR(errno)));
635 }
636 6249 av_freep(&vstats_filename);
637 6249 av_freep(&filter_nbthreads);
638
639 6249 av_freep(&input_streams);
640 6249 av_freep(&input_files);
641 6249 av_freep(&output_streams);
642 6249 av_freep(&output_files);
643
644 6249 uninit_opts();
645
646 6249 avformat_network_deinit();
647
648
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6249 times.
6249 if (received_sigterm) {
649 av_log(NULL, AV_LOG_INFO, "Exiting normally, received signal %d.\n",
650 (int) received_sigterm);
651
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6249 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6249 } else if (ret && atomic_load(&transcode_init_done)) {
652 av_log(NULL, AV_LOG_INFO, "Conversion failed!\n");
653 }
654 6249 term_exit();
655 6249 ffmpeg_exited = 1;
656 6249 }
657
658 6280 void remove_avoptions(AVDictionary **a, AVDictionary *b)
659 {
660 6280 const AVDictionaryEntry *t = NULL;
661
662
2/2
✓ Branch 1 taken 20008 times.
✓ Branch 2 taken 6280 times.
26288 while ((t = av_dict_get(b, "", t, AV_DICT_IGNORE_SUFFIX))) {
663 20008 av_dict_set(a, t->key, NULL, AV_DICT_MATCH_CASE);
664 }
665 6280 }
666
667 18350 void assert_avoptions(AVDictionary *m)
668 {
669 const AVDictionaryEntry *t;
670
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18350 times.
18350 if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
671 av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
672 exit_program(1);
673 }
674 18350 }
675
676 static void abort_codec_experimental(const AVCodec *c, int encoder)
677 {
678 exit_program(1);
679 }
680
681 2727425 static void update_benchmark(const char *fmt, ...)
682 {
683
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2727425 times.
2727425 if (do_benchmark_all) {
684 BenchmarkTimeStamps t = get_benchmark_time_stamps();
685 va_list va;
686 char buf[1024];
687
688 if (fmt) {
689 va_start(va, fmt);
690 vsnprintf(buf, sizeof(buf), fmt, va);
691 va_end(va);
692 av_log(NULL, AV_LOG_INFO,
693 "bench: %8" PRIu64 " user %8" PRIu64 " sys %8" PRIu64 " real %s \n",
694 t.user_usec - current_time.user_usec,
695 t.sys_usec - current_time.sys_usec,
696 t.real_usec - current_time.real_usec, buf);
697 }
698 current_time = t;
699 }
700 2727425 }
701
702 7368 static void close_output_stream(OutputStream *ost)
703 {
704 7368 OutputFile *of = output_files[ost->file_index];
705
2/2
✓ Branch 0 taken 1351 times.
✓ Branch 1 taken 6017 times.
7368 AVRational time_base = ost->stream_copy ? ost->mux_timebase : ost->enc_ctx->time_base;
706
707 7368 ost->finished |= ENCODER_FINISHED;
708
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7364 times.
7368 if (of->shortest) {
709 4 int64_t end = av_rescale_q(ost->sync_opts - ost->first_pts, time_base, AV_TIME_BASE_Q);
710 4 of->recording_time = FFMIN(of->recording_time, end);
711 }
712 7368 }
713
714 /*
715 * Send a single packet to the output, applying any bitstream filters
716 * associated with the output stream. This may result in any number
717 * of packets actually being written, depending on what bitstream
718 * filters are applied. The supplied packet is consumed and will be
719 * blank (as if newly-allocated) when this function returns.
720 *
721 * If eof is set, instead indicate EOF to all bitstream filters and
722 * therefore flush any delayed packets to the output. A blank packet
723 * must be supplied in this case.
724 */
725 448033 static void output_packet(OutputFile *of, AVPacket *pkt,
726 OutputStream *ost, int eof)
727 {
728 448033 int ret = 0;
729
730 /* apply the output bitstream filters */
731
2/2
✓ Branch 0 taken 6980 times.
✓ Branch 1 taken 441053 times.
448033 if (ost->bsf_ctx) {
732
2/2
✓ Branch 0 taken 6888 times.
✓ Branch 1 taken 92 times.
6980 ret = av_bsf_send_packet(ost->bsf_ctx, eof ? NULL : pkt);
733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6980 times.
6980 if (ret < 0)
734 goto finish;
735
2/2
✓ Branch 1 taken 6877 times.
✓ Branch 2 taken 6980 times.
13857 while ((ret = av_bsf_receive_packet(ost->bsf_ctx, pkt)) >= 0)
736 6877 of_write_packet(of, pkt, ost, 0);
737
2/2
✓ Branch 0 taken 6886 times.
✓ Branch 1 taken 94 times.
6980 if (ret == AVERROR(EAGAIN))
738 6886 ret = 0;
739
2/2
✓ Branch 0 taken 6356 times.
✓ Branch 1 taken 434697 times.
441053 } else if (!eof)
740 434697 of_write_packet(of, pkt, ost, 0);
741
742 6356 finish:
743
4/4
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 447939 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 92 times.
448033 if (ret < 0 && ret != AVERROR_EOF) {
744 2 av_log(NULL, AV_LOG_ERROR, "Error applying bitstream filters to an output "
745 "packet for stream #%d:%d.\n", ost->file_index, ost->index);
746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if(exit_on_error)
747 exit_program(1);
748 }
749 448033 }
750
751 402202 static int check_recording_time(OutputStream *ost)
752 {
753 402202 OutputFile *of = output_files[ost->file_index];
754
755
4/4
✓ Branch 0 taken 7514 times.
✓ Branch 1 taken 394688 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 7510 times.
409716 if (of->recording_time != INT64_MAX &&
756 7514 av_compare_ts(ost->sync_opts - ost->first_pts, ost->enc_ctx->time_base, of->recording_time,
757 7514 AV_TIME_BASE_Q) >= 0) {
758 4 close_output_stream(ost);
759 4 return 0;
760 }
761 402198 return 1;
762 }
763
764 403874 static double adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost,
765 AVFrame *frame)
766 {
767 403874 double float_pts = AV_NOPTS_VALUE; // this is identical to frame.pts but with higher precision
768 403874 AVCodecContext *enc = ost->enc_ctx;
769
4/6
✓ Branch 0 taken 401355 times.
✓ Branch 1 taken 2519 times.
✓ Branch 2 taken 401355 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 401355 times.
✗ Branch 5 not taken.
403874 if (!frame || frame->pts == AV_NOPTS_VALUE ||
770
2/4
✓ Branch 0 taken 401355 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 401355 times.
401355 !enc || !ost->filter || !ost->filter->graph->graph)
771 2519 goto early_exit;
772
773 {
774 401355 AVFilterContext *filter = ost->filter->filter;
775
776
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 401220 times.
401355 int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
777 401355 AVRational filter_tb = av_buffersink_get_time_base(filter);
778 401355 AVRational tb = enc->time_base;
779 401355 int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
780
781 401355 tb.den <<= extra_bits;
782 401355 float_pts =
783 401355 av_rescale_q(frame->pts, filter_tb, tb) -
784 401355 av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
785 401355 float_pts /= 1 << extra_bits;
786 // avoid exact midoints to reduce the chance of rounding differences, this can be removed in case the fps code is changed to work with integers
787
2/2
✓ Branch 0 taken 395224 times.
✓ Branch 1 taken 6131 times.
401355 float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
788
789 401355 frame->pts =
790 401355 av_rescale_q(frame->pts, filter_tb, enc->time_base) -
791 401355 av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
792 }
793
794 403874 early_exit:
795
796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403874 times.
403874 if (debug_ts) {
797 av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n",
798 frame ? av_ts2str(frame->pts) : "NULL",
799 frame ? av_ts2timestr(frame->pts, &enc->time_base) : "NULL",
800 float_pts,
801 enc ? enc->time_base.num : -1,
802 enc ? enc->time_base.den : -1);
803 }
804
805 403874 return float_pts;
806 }
807
808 static int init_output_stream(OutputStream *ost, AVFrame *frame,
809 char *error, int error_len);
810
811 635830 static int init_output_stream_wrapper(OutputStream *ost, AVFrame *frame,
812 unsigned int fatal)
813 {
814 635830 int ret = AVERROR_BUG;
815 635830 char error[1024] = {0};
816
817
2/2
✓ Branch 0 taken 629351 times.
✓ Branch 1 taken 6479 times.
635830 if (ost->initialized)
818 629351 return 0;
819
820 6479 ret = init_output_stream(ost, frame, error, sizeof(error));
821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6479 times.
6479 if (ret < 0) {
822 av_log(NULL, AV_LOG_ERROR, "Error initializing output stream %d:%d -- %s\n",
823 ost->file_index, ost->index, error);
824
825 if (fatal)
826 exit_program(1);
827 }
828
829 6479 return ret;
830 }
831
832 static double psnr(double d)
833 {
834 return -10.0 * log10(d);
835 }
836
837 101396 static void update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
838 {
839 101396 const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
840 NULL);
841 101396 AVCodecContext *enc = ost->enc_ctx;
842 int64_t frame_number;
843 double ti1, bitrate, avg_bitrate;
844
845
2/2
✓ Branch 0 taken 9539 times.
✓ Branch 1 taken 91857 times.
101396 ost->quality = sd ? AV_RL32(sd) : -1;
846
2/2
✓ Branch 0 taken 9539 times.
✓ Branch 1 taken 91857 times.
101396 ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
847
848
2/2
✓ Branch 0 taken 405584 times.
✓ Branch 1 taken 101396 times.
506980 for (int i = 0; i<FF_ARRAY_ELEMS(ost->error); i++) {
849
3/4
✓ Branch 0 taken 38156 times.
✓ Branch 1 taken 367428 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 38156 times.
405584 if (sd && i < sd[5])
850 ost->error[i] = AV_RL64(sd + 8 + 8*i);
851 else
852 405584 ost->error[i] = -1;
853 }
854
855
1/2
✓ Branch 0 taken 101396 times.
✗ Branch 1 not taken.
101396 if (!write_vstats)
856 101396 return;
857
858 /* this is executed just the first time update_video_stats is called */
859 if (!vstats_file) {
860 vstats_file = fopen(vstats_filename, "w");
861 if (!vstats_file) {
862 perror("fopen");
863 exit_program(1);
864 }
865 }
866
867 frame_number = ost->packets_encoded;
868 if (vstats_version <= 1) {
869 fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number,
870 ost->quality / (float)FF_QP2LAMBDA);
871 } else {
872 fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ", ost->file_index, ost->index, frame_number,
873 ost->quality / (float)FF_QP2LAMBDA);
874 }
875
876 if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
877 fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
878
879 fprintf(vstats_file,"f_size= %6d ", pkt->size);
880 /* compute pts value */
881 ti1 = pkt->dts * av_q2d(ost->mux_timebase);
882 if (ti1 < 0.01)
883 ti1 = 0.01;
884
885 bitrate = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0;
886 avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
887 fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
888 (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
889 fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type));
890 }
891
892 407409 static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
893 {
894 407409 AVCodecContext *enc = ost->enc_ctx;
895 407409 AVPacket *pkt = ost->pkt;
896 407409 const char *type_desc = av_get_media_type_string(enc->codec_type);
897
2/2
✓ Branch 0 taken 401392 times.
✓ Branch 1 taken 6017 times.
407409 const char *action = frame ? "encode" : "flush";
898 int ret;
899
900
2/2
✓ Branch 0 taken 401392 times.
✓ Branch 1 taken 6017 times.
407409 if (frame) {
901 401392 ost->frames_encoded++;
902
903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 401392 times.
401392 if (debug_ts) {
904 av_log(NULL, AV_LOG_INFO, "encoder <- type:%s "
905 "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
906 type_desc,
907 av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
908 enc->time_base.num, enc->time_base.den);
909 }
910 }
911
912 407409 update_benchmark(NULL);
913
914 407409 ret = avcodec_send_frame(enc, frame);
915
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 407409 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
407409 if (ret < 0 && !(ret == AVERROR_EOF && !frame)) {
916 av_log(NULL, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n",
917 type_desc);
918 return ret;
919 }
920
921 while (1) {
922 802688 ret = avcodec_receive_packet(enc, pkt);
923 802688 update_benchmark("%s_%s %d.%d", action, type_desc,
924 ost->file_index, ost->index);
925
926 /* if two pass, output log on success and EOF */
927
7/8
✓ Branch 0 taken 407409 times.
✓ Branch 1 taken 395279 times.
✓ Branch 2 taken 6017 times.
✓ Branch 3 taken 401392 times.
✓ Branch 4 taken 204 times.
✓ Branch 5 taken 401092 times.
✓ Branch 6 taken 204 times.
✗ Branch 7 not taken.
802688 if ((ret >= 0 || ret == AVERROR_EOF) && ost->logfile && enc->stats_out)
928 204 fprintf(ost->logfile, "%s", enc->stats_out);
929
930
2/2
✓ Branch 0 taken 401392 times.
✓ Branch 1 taken 401296 times.
802688 if (ret == AVERROR(EAGAIN)) {
931
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 401392 times.
401392 av_assert0(frame); // should never happen during flushing
932 401392 return 0;
933
2/2
✓ Branch 0 taken 6017 times.
✓ Branch 1 taken 395279 times.
401296 } else if (ret == AVERROR_EOF) {
934 6017 output_packet(of, pkt, ost, 1);
935 6017 return ret;
936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 395279 times.
395279 } else if (ret < 0) {
937 av_log(NULL, AV_LOG_ERROR, "%s encoding failed\n", type_desc);
938 return ret;
939 }
940
941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 395279 times.
395279 if (debug_ts) {
942 av_log(NULL, AV_LOG_INFO, "encoder -> type:%s "
943 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
944 "duration:%s duration_time:%s\n",
945 type_desc,
946 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
947 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
948 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
949 }
950
951 395279 av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
952
953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 395279 times.
395279 if (debug_ts) {
954 av_log(NULL, AV_LOG_INFO, "encoder -> type:%s "
955 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
956 "duration:%s duration_time:%s\n",
957 type_desc,
958 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
959 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
960 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
961 }
962
963
2/2
✓ Branch 0 taken 101396 times.
✓ Branch 1 taken 293883 times.
395279 if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
964 101396 update_video_stats(ost, pkt, !!vstats_filename);
965
966 395279 ost->packets_encoded++;
967
968 395279 output_packet(of, pkt, ost, 0);
969 }
970
971 av_assert0(0);
972 }
973
974 299999 static void do_audio_out(OutputFile *of, OutputStream *ost,
975 AVFrame *frame)
976 {
977 int ret;
978
979 299999 adjust_frame_pts_to_encoder_tb(of, ost, frame);
980
981
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 299996 times.
299999 if (!check_recording_time(ost))
982 3 return;
983
984
2/4
✓ Branch 0 taken 299996 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 299996 times.
299996 if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
985 frame->pts = ost->sync_opts;
986 299996 ost->sync_opts = frame->pts + frame->nb_samples;
987 299996 ost->samples_encoded += frame->nb_samples;
988
989 299996 ret = encode_frame(of, ost, frame);
990
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 299996 times.
299996 if (ret < 0)
991 exit_program(1);
992 }
993
994 770 static void do_subtitle_out(OutputFile *of,
995 OutputStream *ost,
996 AVSubtitle *sub)
997 {
998 770 int subtitle_out_max_size = 1024 * 1024;
999 int subtitle_out_size, nb, i;
1000 AVCodecContext *enc;
1001 770 AVPacket *pkt = ost->pkt;
1002 int64_t pts;
1003
1004
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 770 times.
770 if (sub->pts == AV_NOPTS_VALUE) {
1005 av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
1006 if (exit_on_error)
1007 exit_program(1);
1008 return;
1009 }
1010
1011 770 enc = ost->enc_ctx;
1012
1013
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 734 times.
770 if (!subtitle_out) {
1014 36 subtitle_out = av_malloc(subtitle_out_max_size);
1015
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (!subtitle_out) {
1016 av_log(NULL, AV_LOG_FATAL, "Failed to allocate subtitle_out\n");
1017 exit_program(1);
1018 }
1019 }
1020
1021 /* Note: DVB subtitle need one packet to draw them and one other
1022 packet to clear them */
1023 /* XXX: signal it in the codec context ? */
1024
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 734 times.
770 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
1025 36 nb = 2;
1026 else
1027 734 nb = 1;
1028
1029 /* shift timestamp to honor -ss and make check_recording_time() work with -t */
1030 770 pts = sub->pts;
1031
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 770 times.
770 if (output_files[ost->file_index]->start_time != AV_NOPTS_VALUE)
1032 pts -= output_files[ost->file_index]->start_time;
1033
2/2
✓ Branch 0 taken 806 times.
✓ Branch 1 taken 770 times.
1576 for (i = 0; i < nb; i++) {
1034 806 unsigned save_num_rects = sub->num_rects;
1035
1036 806 ost->sync_opts = av_rescale_q(pts, AV_TIME_BASE_Q, enc->time_base);
1037
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 806 times.
806 if (!check_recording_time(ost))
1038 return;
1039
1040 806 sub->pts = pts;
1041 // start_display_time is required to be 0
1042 806 sub->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
1043 806 sub->end_display_time -= sub->start_display_time;
1044 806 sub->start_display_time = 0;
1045
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 770 times.
806 if (i == 1)
1046 36 sub->num_rects = 0;
1047
1048 806 ost->frames_encoded++;
1049
1050 806 subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
1051 subtitle_out_max_size, sub);
1052
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 770 times.
806 if (i == 1)
1053 36 sub->num_rects = save_num_rects;
1054
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 806 times.
806 if (subtitle_out_size < 0) {
1055 av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
1056 exit_program(1);
1057 }
1058
1059 806 av_packet_unref(pkt);
1060 806 pkt->data = subtitle_out;
1061 806 pkt->size = subtitle_out_size;
1062 806 pkt->pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->mux_timebase);
1063 806 pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
1064
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 734 times.
806 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
1065 /* XXX: the pts correction is handled here. Maybe handling
1066 it in the codec would be better */
1067
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
72 if (i == 0)
1068 36 pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
1069 else
1070 36 pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
1071 }
1072 806 pkt->dts = pkt->pts;
1073 806 output_packet(of, pkt, ost, 0);
1074 }
1075 }
1076
1077 /* May modify/reset next_picture */
1078 103875 static void do_video_out(OutputFile *of,
1079 OutputStream *ost,
1080 AVFrame *next_picture)
1081 {
1082 int ret;
1083 103875 AVCodecContext *enc = ost->enc_ctx;
1084 AVRational frame_rate;
1085 int64_t nb_frames, nb0_frames, i;
1086 double delta, delta0;
1087 103875 double duration = 0;
1088 103875 double sync_ipts = AV_NOPTS_VALUE;
1089 103875 InputStream *ist = NULL;
1090 103875 AVFilterContext *filter = ost->filter->filter;
1091
1092 103875 init_output_stream_wrapper(ost, next_picture, 1);
1093 103875 sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, next_picture);
1094
1095
2/2
✓ Branch 0 taken 101983 times.
✓ Branch 1 taken 1892 times.
103875 if (ost->source_index >= 0)
1096 101983 ist = input_streams[ost->source_index];
1097
1098 103875 frame_rate = av_buffersink_get_frame_rate(filter);
1099
4/4
✓ Branch 0 taken 103780 times.
✓ Branch 1 taken 95 times.
✓ Branch 2 taken 103746 times.
✓ Branch 3 taken 34 times.
103875 if (frame_rate.num > 0 && frame_rate.den > 0)
1100 103746 duration = 1/(av_q2d(frame_rate) * av_q2d(enc->time_base));
1101
1102
6/8
✓ Branch 0 taken 101983 times.
✓ Branch 1 taken 1892 times.
✓ Branch 2 taken 69718 times.
✓ Branch 3 taken 32265 times.
✓ Branch 4 taken 69718 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 69718 times.
✗ Branch 7 not taken.
103875 if(ist && ist->st->start_time != AV_NOPTS_VALUE && ist->first_dts != AV_NOPTS_VALUE && ost->frame_rate.num)
1103
2/2
✓ Branch 2 taken 276 times.
✓ Branch 3 taken 69442 times.
69718 duration = FFMIN(duration, 1/(av_q2d(ost->frame_rate) * av_q2d(enc->time_base)));
1104
1105
2/2
✓ Branch 0 taken 103778 times.
✓ Branch 1 taken 97 times.
103875 if (!ost->filters_script &&
1106
2/2
✓ Branch 0 taken 87100 times.
✓ Branch 1 taken 16678 times.
103778 !ost->filters &&
1107
5/6
✓ Branch 0 taken 87100 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 84523 times.
✓ Branch 3 taken 2577 times.
✓ Branch 4 taken 82401 times.
✓ Branch 5 taken 2122 times.
87100 (nb_filtergraphs == 0 || !filtergraphs[0]->graph_desc) &&
1108
1/2
✓ Branch 0 taken 82401 times.
✗ Branch 1 not taken.
82401 next_picture &&
1109 82401 ist &&
1110
2/2
✓ Branch 2 taken 78076 times.
✓ Branch 3 taken 4325 times.
82401 lrintf(next_picture->pkt_duration * av_q2d(ist->st->time_base) / av_q2d(enc->time_base)) > 0) {
1111 78076 duration = lrintf(next_picture->pkt_duration * av_q2d(ist->st->time_base) / av_q2d(enc->time_base));
1112 }
1113
1114
2/2
✓ Branch 0 taken 2519 times.
✓ Branch 1 taken 101356 times.
103875 if (!next_picture) {
1115 //end, flushing
1116 2519 nb0_frames = nb_frames = mid_pred(ost->last_nb0_frames[0],
1117 2519 ost->last_nb0_frames[1],
1118 2519 ost->last_nb0_frames[2]);
1119 } else {
1120 101356 delta0 = sync_ipts - ost->sync_opts; // delta0 is the "drift" between the input frame (next_picture) and where it would fall in the output.
1121 101356 delta = delta0 + duration;
1122
1123 /* by default, we output a single frame */
1124 101356 nb0_frames = 0; // tracks the number of times the PREVIOUS frame should be duplicated, mostly for variable framerate (VFR)
1125 101356 nb_frames = 1;
1126
1127
4/4
✓ Branch 0 taken 12310 times.
✓ Branch 1 taken 89046 times.
✓ Branch 2 taken 12044 times.
✓ Branch 3 taken 266 times.
101356 if (delta0 < 0 &&
1128 12044 delta > 0 &&
1129
2/2
✓ Branch 0 taken 11438 times.
✓ Branch 1 taken 606 times.
12044 ost->vsync_method != VSYNC_PASSTHROUGH &&
1130
1/2
✓ Branch 0 taken 11438 times.
✗ Branch 1 not taken.
11438 ost->vsync_method != VSYNC_DROP) {
1131
2/2
✓ Branch 0 taken 691 times.
✓ Branch 1 taken 10747 times.
11438 if (delta0 < -0.6) {
1132 691 av_log(NULL, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0);
1133 } else
1134 10747 av_log(NULL, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0);
1135 11438 sync_ipts = ost->sync_opts;
1136 11438 duration += delta0;
1137 11438 delta0 = 0;
1138 }
1139
1140
4/5
✓ Branch 0 taken 8801 times.
✓ Branch 1 taken 1366 times.
✓ Branch 2 taken 71093 times.
✓ Branch 3 taken 20096 times.
✗ Branch 4 not taken.
101356 switch (ost->vsync_method) {
1141 8801 case VSYNC_VSCFR:
1142
3/4
✓ Branch 0 taken 421 times.
✓ Branch 1 taken 8380 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 421 times.
8801 if (ost->frame_number == 0 && delta0 >= 0.5) {
1143 av_log(NULL, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0));
1144 delta = duration;
1145 delta0 = 0;
1146 ost->sync_opts = llrint(sync_ipts);
1147 }
1148 case VSYNC_CFR:
1149 // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
1150
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 10167 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
10167 if (frame_drop_threshold && delta < frame_drop_threshold && ost->frame_number) {
1151 nb_frames = 0;
1152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10167 times.
10167 } else if (delta < -1.1)
1153 nb_frames = 0;
1154
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 9817 times.
10167 else if (delta > 1.1) {
1155 350 nb_frames = llrintf(delta);
1156
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 348 times.
350 if (delta0 > 1.1)
1157 2 nb0_frames = llrintf(delta0 - 0.6);
1158 }
1159 10167 break;
1160 71093 case VSYNC_VFR:
1161
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 70925 times.
71093 if (delta <= -0.6)
1162 168 nb_frames = 0;
1163
2/2
✓ Branch 0 taken 67798 times.
✓ Branch 1 taken 3127 times.
70925 else if (delta > 0.6)
1164 67798 ost->sync_opts = llrint(sync_ipts);
1165 71093 break;
1166 20096 case VSYNC_DROP:
1167 case VSYNC_PASSTHROUGH:
1168 20096 ost->sync_opts = llrint(sync_ipts);
1169 20096 break;
1170 default:
1171 av_assert0(0);
1172 }
1173 }
1174
1175 /*
1176 * For video, number of frames in == number of packets out.
1177 * But there may be reordering, so we can't throw away frames on encoder
1178 * flush, we need to limit them here, before they go into encoder.
1179 */
1180 103875 nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number);
1181 103875 nb0_frames = FFMIN(nb0_frames, nb_frames);
1182
1183 103875 memmove(ost->last_nb0_frames + 1,
1184 103875 ost->last_nb0_frames,
1185 sizeof(ost->last_nb0_frames[0]) * (FF_ARRAY_ELEMS(ost->last_nb0_frames) - 1));
1186 103875 ost->last_nb0_frames[0] = nb0_frames;
1187
1188
4/4
✓ Branch 0 taken 103872 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 168 times.
✓ Branch 3 taken 103704 times.
103875 if (nb0_frames == 0 && ost->last_dropped) {
1189 168 nb_frames_drop++;
1190 168 av_log(NULL, AV_LOG_VERBOSE,
1191 "*** dropping frame %"PRId64" from stream %d at ts %"PRId64"\n",
1192 168 ost->frame_number, ost->st->index, ost->last_frame->pts);
1193 }
1194
5/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 103872 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 49 times.
✓ Branch 5 taken 103826 times.
103875 if (nb_frames > (nb0_frames && ost->last_dropped) + (nb_frames > nb0_frames)) {
1195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (nb_frames > dts_error_threshold * 30) {
1196 av_log(NULL, AV_LOG_ERROR, "%"PRId64" frame duplication too large, skipping\n", nb_frames - 1);
1197 nb_frames_drop++;
1198 1 return;
1199 }
1200
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
49 nb_frames_dup += nb_frames - (nb0_frames && ost->last_dropped) - (nb_frames > nb0_frames);
1201 49 av_log(NULL, AV_LOG_VERBOSE, "*** %"PRId64" dup!\n", nb_frames - 1);
1202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (nb_frames_dup > dup_warning) {
1203 av_log(NULL, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", dup_warning);
1204 dup_warning *= 10;
1205 }
1206 }
1207
4/4
✓ Branch 0 taken 2689 times.
✓ Branch 1 taken 101186 times.
✓ Branch 2 taken 170 times.
✓ Branch 3 taken 2519 times.
103875 ost->last_dropped = nb_frames == nb0_frames && next_picture;
1208
5/6
✓ Branch 0 taken 170 times.
✓ Branch 1 taken 103705 times.
✓ Branch 2 taken 170 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 46 times.
✓ Branch 5 taken 124 times.
103875 ost->dropped_keyframe = ost->last_dropped && next_picture && next_picture->key_frame;
1209
1210 /* duplicates frame if needed */
1211
2/2
✓ Branch 0 taken 101397 times.
✓ Branch 1 taken 103874 times.
205271 for (i = 0; i < nb_frames; i++) {
1212 AVFrame *in_picture;
1213 101397 int forced_keyframe = 0;
1214 double pts_time;
1215
1216
3/4
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 101328 times.
✓ Branch 2 taken 69 times.
✗ Branch 3 not taken.
101397 if (i < nb0_frames && ost->last_frame->buf[0]) {
1217 69 in_picture = ost->last_frame;
1218 } else
1219 101328 in_picture = next_picture;
1220
1221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101397 times.
101397 if (!in_picture)
1222 return;
1223
1224 101397 in_picture->pts = ost->sync_opts;
1225
1226
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 101396 times.
101397 if (!check_recording_time(ost))
1227 1 return;
1228
1229 101396 in_picture->quality = enc->global_quality;
1230 101396 in_picture->pict_type = 0;
1231
1232
2/2
✓ Branch 0 taken 4869 times.
✓ Branch 1 taken 96527 times.
101396 if (ost->forced_kf_ref_pts == AV_NOPTS_VALUE &&
1233
1/2
✓ Branch 0 taken 4869 times.
✗ Branch 1 not taken.
4869 in_picture->pts != AV_NOPTS_VALUE)
1234 4869 ost->forced_kf_ref_pts = in_picture->pts;
1235
1236 202792 pts_time = in_picture->pts != AV_NOPTS_VALUE ?
1237
1/2
✓ Branch 0 taken 101396 times.
✗ Branch 1 not taken.
101396 (in_picture->pts - ost->forced_kf_ref_pts) * av_q2d(enc->time_base) : NAN;
1238
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 101357 times.
101396 if (ost->forced_kf_index < ost->forced_kf_count &&
1239
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 37 times.
39 in_picture->pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
1240 2 ost->forced_kf_index++;
1241 2 forced_keyframe = 1;
1242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101394 times.
101394 } else if (ost->forced_keyframes_pexpr) {
1243 double res;
1244 ost->forced_keyframes_expr_const_values[FKF_T] = pts_time;
1245 res = av_expr_eval(ost->forced_keyframes_pexpr,
1246 ost->forced_keyframes_expr_const_values, NULL);
1247 ff_dlog(NULL, "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
1248 ost->forced_keyframes_expr_const_values[FKF_N],
1249 ost->forced_keyframes_expr_const_values[FKF_N_FORCED],
1250 ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N],
1251 ost->forced_keyframes_expr_const_values[FKF_T],
1252 ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T],
1253 res);
1254 if (res) {
1255 forced_keyframe = 1;
1256 ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] =
1257 ost->forced_keyframes_expr_const_values[FKF_N];
1258 ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] =
1259 ost->forced_keyframes_expr_const_values[FKF_T];
1260 ost->forced_keyframes_expr_const_values[FKF_N_FORCED] += 1;
1261 }
1262
1263 ost->forced_keyframes_expr_const_values[FKF_N] += 1;
1264
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 101346 times.
101394 } else if ( ost->forced_keyframes
1265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 && !strncmp(ost->forced_keyframes, "source", 6)
1266 && in_picture->key_frame==1
1267 && !i) {
1268 forced_keyframe = 1;
1269
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 101346 times.
101394 } else if ( ost->forced_keyframes
1270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 && !strncmp(ost->forced_keyframes, "source_no_drop", 14)
1271 && !i) {
1272 forced_keyframe = (in_picture->key_frame == 1) || ost->dropped_keyframe;
1273 ost->dropped_keyframe = 0;
1274 }
1275
1276
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 101394 times.
101396 if (forced_keyframe) {
1277 2 in_picture->pict_type = AV_PICTURE_TYPE_I;
1278 2 av_log(NULL, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
1279 }
1280
1281 101396 ret = encode_frame(of, ost, in_picture);
1282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101396 times.
101396 if (ret < 0)
1283 exit_program(1);
1284
1285 101396 ost->sync_opts++;
1286 101396 ost->frame_number++;
1287 }
1288
1289 103874 av_frame_unref(ost->last_frame);
1290
2/2
✓ Branch 0 taken 101355 times.
✓ Branch 1 taken 2519 times.
103874 if (next_picture)
1291 101355 av_frame_move_ref(ost->last_frame, next_picture);
1292 }
1293
1294 407 static void finish_output_stream(OutputStream *ost)
1295 {
1296 407 OutputFile *of = output_files[ost->file_index];
1297
2/2
✓ Branch 0 taken 371 times.
✓ Branch 1 taken 36 times.
407 AVRational time_base = ost->stream_copy ? ost->mux_timebase : ost->enc_ctx->time_base;
1298
1299 407 ost->finished = ENCODER_FINISHED | MUXER_FINISHED;
1300
1301
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 405 times.
407 if (of->shortest) {
1302 2 int64_t end = av_rescale_q(ost->sync_opts - ost->first_pts, time_base, AV_TIME_BASE_Q);
1303 2 of->recording_time = FFMIN(of->recording_time, end);
1304 }
1305 407 }
1306
1307 /**
1308 * Get and encode new output from any of the filtergraphs, without causing
1309 * activity.
1310 *
1311 * @return 0 for success, <0 for severe errors
1312 */
1313 432523 static int reap_filters(int flush)
1314 {
1315 432523 AVFrame *filtered_frame = NULL;
1316 int i;
1317
1318 /* Reap all buffers present in the buffer sinks */
1319
2/2
✓ Branch 0 taken 462288 times.
✓ Branch 1 taken 432523 times.
894811 for (i = 0; i < nb_output_streams; i++) {
1320 462288 OutputStream *ost = output_streams[i];
1321 462288 OutputFile *of = output_files[ost->file_index];
1322 AVFilterContext *filter;
1323 462288 AVCodecContext *enc = ost->enc_ctx;
1324 462288 int ret = 0;
1325
1326
4/4
✓ Branch 0 taken 398746 times.
✓ Branch 1 taken 63542 times.
✓ Branch 2 taken 8786 times.
✓ Branch 3 taken 389960 times.
462288 if (!ost->filter || !ost->filter->graph->graph)
1327 72328 continue;
1328 389960 filter = ost->filter->filter;
1329
1330 /*
1331 * Unlike video, with audio the audio frame size matters.
1332 * Currently we are fully reliant on the lavfi filter chain to
1333 * do the buffering deed for us, and thus the frame size parameter
1334 * needs to be set accordingly. Where does one get the required
1335 * frame size? From the initialized AVCodecContext of an audio
1336 * encoder. Thus, if we have gotten to an audio stream, initialize
1337 * the encoder earlier than receiving the first AVFrame.
1338 */
1339
2/2
✓ Branch 1 taken 269441 times.
✓ Branch 2 taken 120519 times.
389960 if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_AUDIO)
1340 269441 init_output_stream_wrapper(ost, NULL, 1);
1341
1342 389960 filtered_frame = ost->filtered_frame;
1343
1344 while (1) {
1345 791315 ret = av_buffersink_get_frame_flags(filter, filtered_frame,
1346 AV_BUFFERSINK_FLAG_NO_REQUEST);
1347
2/2
✓ Branch 0 taken 389960 times.
✓ Branch 1 taken 401355 times.
791315 if (ret < 0) {
1348
3/4
✓ Branch 0 taken 4104 times.
✓ Branch 1 taken 385856 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4104 times.
389960 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
1349 av_log(NULL, AV_LOG_WARNING,
1350 "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(ret));
1351
4/4
✓ Branch 0 taken 3882 times.
✓ Branch 1 taken 386078 times.
✓ Branch 2 taken 3710 times.
✓ Branch 3 taken 172 times.
389960 } else if (flush && ret == AVERROR_EOF) {
1352
2/2
✓ Branch 1 taken 2519 times.
✓ Branch 2 taken 1191 times.
3710 if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_VIDEO)
1353 2519 do_video_out(of, ost, NULL);
1354 }
1355 389960 break;
1356 }
1357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 401355 times.
401355 if (ost->finished) {
1358 av_frame_unref(filtered_frame);
1359 continue;
1360 }
1361
1362
2/3
✓ Branch 1 taken 101356 times.
✓ Branch 2 taken 299999 times.
✗ Branch 3 not taken.
401355 switch (av_buffersink_get_type(filter)) {
1363 101356 case AVMEDIA_TYPE_VIDEO:
1364
1/2
✓ Branch 0 taken 101356 times.
✗ Branch 1 not taken.
101356 if (!ost->frame_aspect_ratio.num)
1365 101356 enc->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
1366
1367 101356 do_video_out(of, ost, filtered_frame);
1368 101356 break;
1369 299999 case AVMEDIA_TYPE_AUDIO:
1370
1/2
✓ Branch 0 taken 299999 times.
✗ Branch 1 not taken.
299999 if (!(enc->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
1371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 299999 times.
299999 enc->ch_layout.nb_channels != filtered_frame->ch_layout.nb_channels) {
1372 av_log(NULL, AV_LOG_ERROR,
1373 "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
1374 break;
1375 }
1376 299999 do_audio_out(of, ost, filtered_frame);
1377 299999 break;
1378 default:
1379 // TODO support subtitle filters
1380 av_assert0(0);
1381 }
1382
1383 401355 av_frame_unref(filtered_frame);
1384 }
1385 }
1386
1387 432523 return 0;
1388 }
1389
1390 6248 static void print_final_stats(int64_t total_size)
1391 {
1392 6248 uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
1393 6248 uint64_t subtitle_size = 0;
1394 6248 uint64_t data_size = 0;
1395 6248 float percent = -1.0;
1396 int i, j;
1397 6248 int pass1_used = 1;
1398
1399
2/2
✓ Branch 0 taken 6479 times.
✓ Branch 1 taken 6248 times.
12727 for (i = 0; i < nb_output_streams; i++) {
1400 6479 OutputStream *ost = output_streams[i];
1401
4/4
✓ Branch 0 taken 5089 times.
✓ Branch 1 taken 1315 times.
✓ Branch 2 taken 62 times.
✓ Branch 3 taken 13 times.
6479 switch (ost->enc_ctx->codec_type) {
1402 5089 case AVMEDIA_TYPE_VIDEO: video_size += ost->data_size; break;
1403 1315 case AVMEDIA_TYPE_AUDIO: audio_size += ost->data_size; break;
1404 62 case AVMEDIA_TYPE_SUBTITLE: subtitle_size += ost->data_size; break;
1405 13 default: other_size += ost->data_size; break;
1406 }
1407 6479 extra_size += ost->enc_ctx->extradata_size;
1408 6479 data_size += ost->data_size;
1409
2/2
✓ Branch 0 taken 6475 times.
✓ Branch 1 taken 4 times.
6479 if ( (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
1410 != AV_CODEC_FLAG_PASS1)
1411 6475 pass1_used = 0;
1412 }
1413
1414
6/6
✓ Branch 0 taken 6238 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 6153 times.
✓ Branch 3 taken 85 times.
✓ Branch 4 taken 4304 times.
✓ Branch 5 taken 1849 times.
6248 if (data_size && total_size>0 && total_size >= data_size)
1415 4304 percent = 100.0 * (total_size - data_size) / data_size;
1416
1417 6248 av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB subtitle:%1.0fkB other streams:%1.0fkB global headers:%1.0fkB muxing overhead: ",
1418 video_size / 1024.0,
1419 audio_size / 1024.0,
1420 subtitle_size / 1024.0,
1421 other_size / 1024.0,
1422 extra_size / 1024.0);
1423
2/2
✓ Branch 0 taken 4304 times.
✓ Branch 1 taken 1944 times.
6248 if (percent >= 0.0)
1424 4304 av_log(NULL, AV_LOG_INFO, "%f%%", percent);
1425 else
1426 1944 av_log(NULL, AV_LOG_INFO, "unknown");
1427 6248 av_log(NULL, AV_LOG_INFO, "\n");
1428
1429 /* print verbose per-stream stats */
1430
2/2
✓ Branch 0 taken 6280 times.
✓ Branch 1 taken 6248 times.
12528 for (i = 0; i < nb_input_files; i++) {
1431 6280 InputFile *f = input_files[i];
1432 6280 uint64_t total_packets = 0, total_size = 0;
1433
1434 6280 av_log(NULL, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
1435 6280 i, f->ctx->url);
1436
1437
2/2
✓ Branch 0 taken 6695 times.
✓ Branch 1 taken 6280 times.
12975 for (j = 0; j < f->nb_streams; j++) {
1438 6695 InputStream *ist = input_streams[f->ist_index + j];
1439 6695 enum AVMediaType type = ist->dec_ctx->codec_type;
1440
1441 6695 total_size += ist->data_size;
1442 6695 total_packets += ist->nb_packets;
1443
1444 6695 av_log(NULL, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
1445 i, j, av_get_media_type_string(type));
1446 6695 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
1447 ist->nb_packets, ist->data_size);
1448
1449
2/2
✓ Branch 0 taken 6017 times.
✓ Branch 1 taken 678 times.
6695 if (ist->decoding_needed) {
1450 6017 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames decoded",
1451 ist->frames_decoded);
1452
2/2
✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 4873 times.
6017 if (type == AVMEDIA_TYPE_AUDIO)
1453 1144 av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
1454 6017 av_log(NULL, AV_LOG_VERBOSE, "; ");
1455 }
1456
1457 6695 av_log(NULL, AV_LOG_VERBOSE, "\n");
1458 }
1459
1460 6280 av_log(NULL, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
1461 total_packets, total_size);
1462 }
1463
1464
2/2
✓ Branch 0 taken 6249 times.
✓ Branch 1 taken 6248 times.
12497 for (i = 0; i < nb_output_files; i++) {
1465 6249 OutputFile *of = output_files[i];
1466 6249 uint64_t total_packets = 0, total_size = 0;
1467
1468 6249 av_log(NULL, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
1469 6249 i, of->ctx->url);
1470
1471
2/2
✓ Branch 0 taken 6479 times.
✓ Branch 1 taken 6249 times.
12728 for (j = 0; j < of->ctx->nb_streams; j++) {
1472 6479 OutputStream *ost = output_streams[of->ost_index + j];
1473 6479 enum AVMediaType type = ost->enc_ctx->codec_type;
1474
1475 6479 total_size += ost->data_size;
1476 6479 total_packets += ost->packets_written;
1477
1478 6479 av_log(NULL, AV_LOG_VERBOSE, " Output stream #%d:%d (%s): ",
1479 i, j, av_get_media_type_string(type));
1480
2/2
✓ Branch 0 taken 6053 times.
✓ Branch 1 taken 426 times.
6479 if (ost->encoding_needed) {
1481 6053 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
1482 ost->frames_encoded);
1483
2/2
✓ Branch 0 taken 1147 times.
✓ Branch 1 taken 4906 times.
6053 if (type == AVMEDIA_TYPE_AUDIO)
1484 1147 av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
1485 6053 av_log(NULL, AV_LOG_VERBOSE, "; ");
1486 }
1487
1488 6479 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
1489 ost->packets_written, ost->data_size);
1490
1491 6479 av_log(NULL, AV_LOG_VERBOSE, "\n");
1492 }
1493
1494 6249 av_log(NULL, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
1495 total_packets, total_size);
1496 }
1497
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6242 times.
6248 if(video_size + data_size + audio_size + subtitle_size + extra_size == 0){
1498 6 av_log(NULL, AV_LOG_WARNING, "Output file is empty, nothing was encoded ");
1499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (pass1_used) {
1500 av_log(NULL, AV_LOG_WARNING, "\n");
1501 } else {
1502 6 av_log(NULL, AV_LOG_WARNING, "(check -ss / -t / -frames parameters if used)\n");
1503 }
1504 }
1505 6248 }
1506
1507 437076 static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time)
1508 {
1509 AVBPrint buf, buf_script;
1510 OutputStream *ost;
1511 AVFormatContext *oc;
1512 int64_t total_size;
1513 AVCodecContext *enc;
1514 int vid, i;
1515 double bitrate;
1516 double speed;
1517 437076 int64_t pts = INT64_MIN + 1;
1518 static int64_t last_time = -1;
1519 static int first_report = 1;
1520 static int qp_histogram[52];
1521 int hours, mins, secs, us;
1522 const char *hours_sign;
1523 int ret;
1524 float t;
1525
1526
5/6
✓ Branch 0 taken 426992 times.
✓ Branch 1 taken 10084 times.
✓ Branch 2 taken 420771 times.
✓ Branch 3 taken 6221 times.
✓ Branch 4 taken 420771 times.
✗ Branch 5 not taken.
437076 if (!print_stats && !is_last_report && !progress_avio)
1527 430780 return;
1528
1529
2/2
✓ Branch 0 taken 10057 times.
✓ Branch 1 taken 6248 times.
16305 if (!is_last_report) {
1530
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 10030 times.
10057 if (last_time == -1) {
1531 27 last_time = cur_time;
1532 }
1533
4/4
✓ Branch 0 taken 10036 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 10006 times.
10057 if (((cur_time - last_time) < stats_period && !first_report) ||
1534
4/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 27 times.
51 (first_report && nb_output_dumped < nb_output_files))
1535 10009 return;
1536 48 last_time = cur_time;
1537 }
1538
1539 6296 t = (cur_time-timer_start) / 1000000.0;
1540
1541
1542 6296 oc = output_files[0]->ctx;
1543
1544 6296 total_size = avio_size(oc->pb);
1545
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 6171 times.
6296 if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too
1546 125 total_size = avio_tell(oc->pb);
1547
1548 6296 vid = 0;
1549 6296 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
1550 6296 av_bprint_init(&buf_script, 0, AV_BPRINT_SIZE_AUTOMATIC);
1551
2/2
✓ Branch 0 taken 6538 times.
✓ Branch 1 taken 6296 times.
12834 for (i = 0; i < nb_output_streams; i++) {
1552 6538 float q = -1;
1553 6538 ost = output_streams[i];
1554 6538 enc = ost->enc_ctx;
1555
2/2
✓ Branch 0 taken 6092 times.
✓ Branch 1 taken 446 times.
6538 if (!ost->stream_copy)
1556 6092 q = ost->quality / (float) FF_QP2LAMBDA;
1557
1558
4/4
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 6345 times.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 151 times.
6538 if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1559 42 av_bprintf(&buf, "q=%2.1f ", q);
1560 42 av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
1561 ost->file_index, ost->index, q);
1562 }
1563
4/4
✓ Branch 0 taken 6345 times.
✓ Branch 1 taken 193 times.
✓ Branch 2 taken 5063 times.
✓ Branch 3 taken 1282 times.
6538 if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1564 float fps;
1565 5063 int64_t frame_number = ost->frame_number;
1566
1567
2/2
✓ Branch 0 taken 857 times.
✓ Branch 1 taken 4206 times.
5063 fps = t > 1 ? frame_number / t : 0;
1568 5063 av_bprintf(&buf, "frame=%5"PRId64" fps=%3.*f q=%3.1f ",
1569 frame_number, fps < 9.95, fps, q);
1570 5063 av_bprintf(&buf_script, "frame=%"PRId64"\n", frame_number);
1571 5063 av_bprintf(&buf_script, "fps=%.2f\n", fps);
1572 5063 av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
1573 ost->file_index, ost->index, q);
1574
2/2
✓ Branch 0 taken 5051 times.
✓ Branch 1 taken 12 times.
5063 if (is_last_report)
1575 5051 av_bprintf(&buf, "L");
1576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5063 times.
5063 if (qp_hist) {
1577 int j;
1578 int qp = lrintf(q);
1579 if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
1580 qp_histogram[qp]++;
1581 for (j = 0; j < 32; j++)
1582 av_bprintf(&buf, "%X", av_log2(qp_histogram[j] + 1));
1583 }
1584
1585
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 5063 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
5063 if ((enc->flags & AV_CODEC_FLAG_PSNR) && (ost->pict_type != AV_PICTURE_TYPE_NONE || is_last_report)) {
1586 int j;
1587 double error, error_sum = 0;
1588 double scale, scale_sum = 0;
1589 double p;
1590 char type[3] = { 'Y','U','V' };
1591 av_bprintf(&buf, "PSNR=");
1592 for (j = 0; j < 3; j++) {
1593 if (is_last_report) {
1594 error = enc->error[j];
1595 scale = enc->width * enc->height * 255.0 * 255.0 * frame_number;
1596 } else {
1597 error = ost->error[j];
1598 scale = enc->width * enc->height * 255.0 * 255.0;
1599 }
1600 if (j)
1601 scale /= 4;
1602 error_sum += error;
1603 scale_sum += scale;
1604 p = psnr(error / scale);
1605 av_bprintf(&buf, "%c:%2.2f ", type[j], p);
1606 av_bprintf(&buf_script, "stream_%d_%d_psnr_%c=%2.2f\n",
1607 ost->file_index, ost->index, type[j] | 32, p);
1608 }
1609 p = psnr(error_sum / scale_sum);
1610 av_bprintf(&buf, "*:%2.2f ", psnr(error_sum / scale_sum));
1611 av_bprintf(&buf_script, "stream_%d_%d_psnr_all=%2.2f\n",
1612 ost->file_index, ost->index, p);
1613 }
1614 5063 vid = 1;
1615 }
1616 /* compute min output value */
1617
1/2
✓ Branch 1 taken 6538 times.
✗ Branch 2 not taken.
6538 if (av_stream_get_end_pts(ost->st) != AV_NOPTS_VALUE) {
1618
2/2
✓ Branch 1 taken 6467 times.
✓ Branch 2 taken 71 times.
6538 pts = FFMAX(pts, av_rescale_q(av_stream_get_end_pts(ost->st),
1619 ost->st->time_base, AV_TIME_BASE_Q));
1620
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 6531 times.
6538 if (copy_ts) {
1621
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 if (copy_ts_first_pts == AV_NOPTS_VALUE && pts > 1)
1622 7 copy_ts_first_pts = pts;
1623
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (copy_ts_first_pts != AV_NOPTS_VALUE)
1624 7 pts -= copy_ts_first_pts;
1625 }
1626 }
1627
1628
2/2
✓ Branch 0 taken 6479 times.
✓ Branch 1 taken 59 times.
6538 if (is_last_report)
1629 6479 nb_frames_drop += ost->last_dropped;
1630 }
1631
1632 6296 secs = FFABS(pts) / AV_TIME_BASE;
1633 6296 us = FFABS(pts) % AV_TIME_BASE;
1634 6296 mins = secs / 60;
1635 6296 secs %= 60;
1636 6296 hours = mins / 60;
1637 6296 mins %= 60;
1638
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 6291 times.
6296 hours_sign = (pts < 0) ? "-" : "";
1639
1640
4/4
✓ Branch 0 taken 6259 times.
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 6149 times.
✓ Branch 3 taken 110 times.
6296 bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
1641
2/2
✓ Branch 0 taken 6280 times.
✓ Branch 1 taken 16 times.
6296 speed = t != 0.0 ? (double)pts / AV_TIME_BASE / t : -1;
1642
1643
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 6175 times.
6296 if (total_size < 0) av_bprintf(&buf, "size=N/A time=");
1644 6175 else av_bprintf(&buf, "size=%8.0fkB time=", total_size / 1024.0);
1645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6296 times.
6296 if (pts == AV_NOPTS_VALUE) {
1646 av_bprintf(&buf, "N/A ");
1647 } else {
1648 6296 av_bprintf(&buf, "%s%02d:%02d:%02d.%02d ",
1649 hours_sign, hours, mins, secs, (100 * us) / AV_TIME_BASE);
1650 }
1651
1652
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 6147 times.
6296 if (bitrate < 0) {
1653 149 av_bprintf(&buf, "bitrate=N/A");
1654 149 av_bprintf(&buf_script, "bitrate=N/A\n");
1655 }else{
1656 6147 av_bprintf(&buf, "bitrate=%6.1fkbits/s", bitrate);
1657 6147 av_bprintf(&buf_script, "bitrate=%6.1fkbits/s\n", bitrate);
1658 }
1659
1660
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 6175 times.
6296 if (total_size < 0) av_bprintf(&buf_script, "total_size=N/A\n");
1661 6175 else av_bprintf(&buf_script, "total_size=%"PRId64"\n", total_size);
1662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6296 times.
6296 if (pts == AV_NOPTS_VALUE) {
1663 av_bprintf(&buf_script, "out_time_us=N/A\n");
1664 av_bprintf(&buf_script, "out_time_ms=N/A\n");
1665 av_bprintf(&buf_script, "out_time=N/A\n");
1666 } else {
1667 6296 av_bprintf(&buf_script, "out_time_us=%"PRId64"\n", pts);
1668 6296 av_bprintf(&buf_script, "out_time_ms=%"PRId64"\n", pts);
1669 6296 av_bprintf(&buf_script, "out_time=%s%02d:%02d:%02d.%06d\n",
1670 hours_sign, hours, mins, secs, us);
1671 }
1672
1673
4/4
✓ Branch 0 taken 6281 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 6271 times.
6296 if (nb_frames_dup || nb_frames_drop)
1674 25 av_bprintf(&buf, " dup=%"PRId64" drop=%"PRId64, nb_frames_dup, nb_frames_drop);
1675 6296 av_bprintf(&buf_script, "dup_frames=%"PRId64"\n", nb_frames_dup);
1676 6296 av_bprintf(&buf_script, "drop_frames=%"PRId64"\n", nb_frames_drop);
1677
1678
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 6279 times.
6296 if (speed < 0) {
1679 17 av_bprintf(&buf, " speed=N/A");
1680 17 av_bprintf(&buf_script, "speed=N/A\n");
1681 } else {
1682 6279 av_bprintf(&buf, " speed=%4.3gx", speed);
1683 6279 av_bprintf(&buf_script, "speed=%4.3gx\n", speed);
1684 }
1685
1686
3/4
✓ Branch 0 taken 6221 times.
✓ Branch 1 taken 75 times.
✓ Branch 2 taken 6221 times.
✗ Branch 3 not taken.
6296 if (print_stats || is_last_report) {
1687
2/2
✓ Branch 0 taken 6248 times.
✓ Branch 1 taken 48 times.
6296 const char end = is_last_report ? '\n' : '\r';
1688
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6296 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6296 if (print_stats==1 && AV_LOG_INFO > av_log_get_level()) {
1689 fprintf(stderr, "%s %c", buf.str, end);
1690 } else
1691 6296 av_log(NULL, AV_LOG_INFO, "%s %c", buf.str, end);
1692
1693 6296 fflush(stderr);
1694 }
1695 6296 av_bprint_finalize(&buf, NULL);
1696
1697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6296 times.
6296 if (progress_avio) {
1698 av_bprintf(&buf_script, "progress=%s\n",
1699 is_last_report ? "end" : "continue");
1700 avio_write(progress_avio, buf_script.str,
1701 FFMIN(buf_script.len, buf_script.size - 1));
1702 avio_flush(progress_avio);
1703 av_bprint_finalize(&buf_script, NULL);
1704 if (is_last_report) {
1705 if ((ret = avio_closep(&progress_avio)) < 0)
1706 av_log(NULL, AV_LOG_ERROR,
1707 "Error closing progress log, loss of information possible: %s\n", av_err2str(ret));
1708 }
1709 }
1710
1711 6296 first_report = 0;
1712
1713
2/2
✓ Branch 0 taken 6248 times.
✓ Branch 1 taken 48 times.
6296 if (is_last_report)
1714 6248 print_final_stats(total_size);
1715 }
1716
1717 1 static int ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParameters *par)
1718 {
1719 int ret;
1720
1721 // We never got any input. Set a fake format, which will
1722 // come from libavformat.
1723 1 ifilter->format = par->format;
1724 1 ifilter->sample_rate = par->sample_rate;
1725 1 ifilter->width = par->width;
1726 1 ifilter->height = par->height;
1727 1 ifilter->sample_aspect_ratio = par->sample_aspect_ratio;
1728 1 ret = av_channel_layout_copy(&ifilter->ch_layout, &par->ch_layout);
1729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1730 return ret;
1731
1732 1 return 0;
1733 }
1734
1735 6248 static void flush_encoders(void)
1736 {
1737 int i, ret;
1738
1739
2/2
✓ Branch 0 taken 6479 times.
✓ Branch 1 taken 6248 times.
12727 for (i = 0; i < nb_output_streams; i++) {
1740 6479 OutputStream *ost = output_streams[i];
1741 6479 AVCodecContext *enc = ost->enc_ctx;
1742 6479 OutputFile *of = output_files[ost->file_index];
1743
1744
2/2
✓ Branch 0 taken 426 times.
✓ Branch 1 taken 6053 times.
6479 if (!ost->encoding_needed)
1745 426 continue;
1746
1747 // Try to enable encoding with no input frames.
1748 // Maybe we should just let encoding fail instead.
1749
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6053 times.
6053 if (!ost->initialized) {
1750 FilterGraph *fg = ost->filter->graph;
1751
1752 av_log(NULL, AV_LOG_WARNING,
1753 "Finishing stream %d:%d without any data written to it.\n",
1754 ost->file_index, ost->st->index);
1755
1756 if (ost->filter && !fg->graph) {
1757 int x;
1758 for (x = 0; x < fg->nb_inputs; x++) {
1759 InputFilter *ifilter = fg->inputs[x];
1760 if (ifilter->format < 0 &&
1761 ifilter_parameters_from_codecpar(ifilter, ifilter->ist->st->codecpar) < 0) {
1762 av_log(NULL, AV_LOG_ERROR, "Error copying paramerets from input stream\n");
1763 exit_program(1);
1764 }
1765 }
1766
1767 if (!ifilter_has_all_input_formats(fg))
1768 continue;
1769
1770 ret = configure_filtergraph(fg);
1771 if (ret < 0) {
1772 av_log(NULL, AV_LOG_ERROR, "Error configuring filter graph\n");
1773 exit_program(1);
1774 }
1775
1776 finish_output_stream(ost);
1777 }
1778
1779 init_output_stream_wrapper(ost, NULL, 1);
1780 }
1781
1782
4/4
✓ Branch 0 taken 1183 times.
✓ Branch 1 taken 4870 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 1147 times.
6053 if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
1783 36 continue;
1784
1785 6017 ret = encode_frame(of, ost, NULL);
1786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6017 times.
6017 if (ret != AVERROR_EOF)
1787 exit_program(1);
1788 }
1789 6248 }
1790
1791 /*
1792 * Check whether a packet from ist should be written into ost at this time
1793 */
1794 448508 static int check_output_constraints(InputStream *ist, OutputStream *ost)
1795 {
1796 448508 OutputFile *of = output_files[ost->file_index];
1797 448508 int ist_index = input_files[ist->file_index]->ist_index + ist->st->index;
1798
1799
2/2
✓ Branch 0 taken 29272 times.
✓ Branch 1 taken 419236 times.
448508 if (ost->source_index != ist_index)
1800 29272 return 0;
1801
1802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 419236 times.
419236 if (ost->finished & MUXER_FINISHED)
1803 return 0;
1804
1805
4/4
✓ Branch 0 taken 1086 times.
✓ Branch 1 taken 418150 times.
✓ Branch 2 taken 216 times.
✓ Branch 3 taken 870 times.
419236 if (of->start_time != AV_NOPTS_VALUE && ist->pts < of->start_time)
1806 216 return 0;
1807
1808 419020 return 1;
1809 }
1810
1811 47304 static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
1812 {
1813 47304 OutputFile *of = output_files[ost->file_index];
1814 47304 InputFile *f = input_files [ist->file_index];
1815
2/2
✓ Branch 0 taken 866 times.
✓ Branch 1 taken 46438 times.
47304 int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
1816 47304 int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase);
1817 47304 AVPacket *opkt = ost->pkt;
1818
1819 47304 av_packet_unref(opkt);
1820 // EOF: flush output bitstream filters.
1821
2/2
✓ Branch 0 taken 431 times.
✓ Branch 1 taken 46873 times.
47304 if (!pkt) {
1822 431 output_packet(of, opkt, ost, 1);
1823 1804 return;
1824 }
1825
1826
4/4
✓ Branch 0 taken 433 times.
✓ Branch 1 taken 46440 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 397 times.
46873 if (!ost->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) &&
1827
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 !ost->copy_initial_nonkeyframes)
1828 36 return;
1829
1830
3/4
✓ Branch 0 taken 397 times.
✓ Branch 1 taken 46440 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 397 times.
46837 if (!ost->streamcopy_started && !ost->copy_prior_start) {
1831 int64_t comp_start = start_time;
1832 if (copy_ts && f->start_time != AV_NOPTS_VALUE)
1833 comp_start = FFMAX(start_time, f->start_time + f->ts_offset);
1834 if (pkt->pts == AV_NOPTS_VALUE ?
1835 ist->pts < comp_start :
1836 pkt->pts < av_rescale_q(comp_start, AV_TIME_BASE_Q, ist->st->time_base))
1837 return;
1838 }
1839
1840
2/2
✓ Branch 0 taken 2397 times.
✓ Branch 1 taken 44440 times.
46837 if (of->recording_time != INT64_MAX &&
1841
2/2
✓ Branch 0 taken 1335 times.
✓ Branch 1 taken 1062 times.
2397 ist->pts >= of->recording_time + start_time) {
1842 1335 close_output_stream(ost);
1843 1335 return;
1844 }
1845
1846
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 45354 times.
45502 if (f->recording_time != INT64_MAX) {
1847 148 start_time = 0;
1848
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 74 times.
148 if (copy_ts) {
1849
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 start_time += f->start_time != AV_NOPTS_VALUE ? f->start_time : 0;
1850
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 start_time += start_at_zero ? 0 : f->ctx->start_time;
1851 }
1852
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 146 times.
148 if (ist->pts >= f->recording_time + start_time) {
1853 2 close_output_stream(ost);
1854 2 return;
1855 }
1856 }
1857
1858
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45500 times.
45500 if (av_packet_ref(opkt, pkt) < 0)
1859 exit_program(1);
1860
1861
2/2
✓ Branch 0 taken 40027 times.
✓ Branch 1 taken 5473 times.
45500 if (pkt->pts != AV_NOPTS_VALUE)
1862 40027 opkt->pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->mux_timebase) - ost_tb_start_time;
1863
1864
2/2
✓ Branch 0 taken 5235 times.
✓ Branch 1 taken 40265 times.
45500 if (pkt->dts == AV_NOPTS_VALUE) {
1865 5235 opkt->dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->mux_timebase);
1866
2/2
✓ Branch 0 taken 33419 times.
✓ Branch 1 taken 6846 times.
40265 } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1867 33419 int duration = av_get_audio_frame_duration(ist->dec_ctx, pkt->size);
1868
2/2
✓ Branch 0 taken 9356 times.
✓ Branch 1 taken 24063 times.
33419 if(!duration)
1869 9356 duration = ist->dec_ctx->frame_size;
1870 66838 opkt->dts = av_rescale_delta(ist->st->time_base, pkt->dts,
1871 33419 (AVRational){1, ist->dec_ctx->sample_rate}, duration,
1872 &ist->filter_in_rescale_delta_last, ost->mux_timebase);
1873 /* dts will be set immediately afterwards to what pts is now */
1874 33419 opkt->pts = opkt->dts - ost_tb_start_time;
1875 } else
1876 6846 opkt->dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->mux_timebase);
1877 45500 opkt->dts -= ost_tb_start_time;
1878
1879 45500 opkt->duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->mux_timebase);
1880
1881 45500 ost->sync_opts += opkt->duration;
1882
1883 45500 output_packet(of, opkt, ost, 0);
1884
1885 45500 ost->streamcopy_started = 1;
1886 }
1887
1888 1469 int guess_input_channel_layout(InputStream *ist)
1889 {
1890 1469 AVCodecContext *dec = ist->dec_ctx;
1891
1892
2/2
✓ Branch 0 taken 775 times.
✓ Branch 1 taken 694 times.
1469 if (dec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
1893 char layout_name[256];
1894
1895
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 771 times.
775 if (dec->ch_layout.nb_channels > ist->guess_layout_max)
1896 12 return 0;
1897 771 av_channel_layout_default(&dec->ch_layout, dec->ch_layout.nb_channels);
1898
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 763 times.
771 if (dec->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
1899 8 return 0;
1900 763 av_channel_layout_describe(&dec->ch_layout, layout_name, sizeof(layout_name));
1901 763 av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for Input Stream "
1902 763 "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name);
1903 }
1904 1457 return 1;
1905 }
1906
1907 754265 static void check_decode_result(InputStream *ist, int *got_output, int ret)
1908 {
1909
4/4
✓ Branch 0 taken 365550 times.
✓ Branch 1 taken 388715 times.
✓ Branch 2 taken 465 times.
✓ Branch 3 taken 365085 times.
754265 if (*got_output || ret<0)
1910 389180 decode_error_stat[ret<0] ++;
1911
1912
3/4
✓ Branch 0 taken 465 times.
✓ Branch 1 taken 753800 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 465 times.
754265 if (ret < 0 && exit_on_error)
1913 exit_program(1);
1914
1915
4/4
✓ Branch 0 taken 388715 times.
✓ Branch 1 taken 365550 times.
✓ Branch 2 taken 387890 times.
✓ Branch 3 taken 825 times.
754265 if (*got_output && ist) {
1916
3/4
✓ Branch 0 taken 387854 times.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 387854 times.
387890 if (ist->decoded_frame->decode_error_flags || (ist->decoded_frame->flags & AV_FRAME_FLAG_CORRUPT)) {
1917 36 av_log(NULL, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
1918
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 "%s: corrupt decoded frame in stream %d\n", input_files[ist->file_index]->ctx->url, ist->st->index);
1919
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (exit_on_error)
1920 exit_program(1);
1921 }
1922 }
1923 754265 }
1924
1925 // Filters can be configured only if the formats of all inputs are known.
1926 14586 static int ifilter_has_all_input_formats(FilterGraph *fg)
1927 {
1928 int i;
1929
2/2
✓ Branch 0 taken 14660 times.
✓ Branch 1 taken 6046 times.
20706 for (i = 0; i < fg->nb_inputs; i++) {
1930
4/4
✓ Branch 0 taken 8544 times.
✓ Branch 1 taken 6116 times.
✓ Branch 2 taken 8501 times.
✓ Branch 3 taken 43 times.
14660 if (fg->inputs[i]->format < 0 && (fg->inputs[i]->type == AVMEDIA_TYPE_AUDIO ||
1931
2/2
✓ Branch 0 taken 8497 times.
✓ Branch 1 taken 4 times.
8501 fg->inputs[i]->type == AVMEDIA_TYPE_VIDEO))
1932 8540 return 0;
1933 }
1934 6046 return 1;
1935 }
1936
1937 389203 static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame, int keep_reference)
1938 {
1939 389203 FilterGraph *fg = ifilter->graph;
1940 AVFrameSideData *sd;
1941 int need_reinit, ret;
1942 389203 int buffersrc_flags = AV_BUFFERSRC_FLAG_PUSH;
1943
1944
2/2
✓ Branch 0 taken 1313 times.
✓ Branch 1 taken 387890 times.
389203 if (keep_reference)
1945 1313 buffersrc_flags |= AV_BUFFERSRC_FLAG_KEEP_REF;
1946
1947 /* determine if the parameters for this input changed */
1948 389203 need_reinit = ifilter->format != frame->format;
1949
1950
2/3
✓ Branch 0 taken 288413 times.
✓ Branch 1 taken 100790 times.
✗ Branch 2 not taken.
389203 switch (ifilter->ist->st->codecpar->codec_type) {
1951 288413 case AVMEDIA_TYPE_AUDIO:
1952
4/4
✓ Branch 0 taken 287261 times.
✓ Branch 1 taken 1152 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 287260 times.
575674 need_reinit |= ifilter->sample_rate != frame->sample_rate ||
1953 287261 av_channel_layout_compare(&ifilter->ch_layout, &frame->ch_layout);
1954 288413 break;
1955 100790 case AVMEDIA_TYPE_VIDEO:
1956
2/2
✓ Branch 0 taken 95930 times.
✓ Branch 1 taken 4860 times.
196720 need_reinit |= ifilter->width != frame->width ||
1957
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 95917 times.
95930 ifilter->height != frame->height;
1958 100790 break;
1959 }
1960
1961
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 389203 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
389203 if (!ifilter->ist->reinit_filters && fg->graph)
1962 need_reinit = 0;
1963
1964
1/2
✓ Branch 0 taken 389203 times.
✗ Branch 1 not taken.
389203 if (!!ifilter->hw_frames_ctx != !!frame->hw_frames_ctx ||
1965
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 389203 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
389203 (ifilter->hw_frames_ctx && ifilter->hw_frames_ctx->data != frame->hw_frames_ctx->data))
1966 need_reinit = 1;
1967
1968
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 389203 times.
389203 if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX)) {
1969 if (!ifilter->displaymatrix || memcmp(sd->data, ifilter->displaymatrix, sizeof(int32_t) * 9))
1970 need_reinit = 1;
1971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 389203 times.
389203 } else if (ifilter->displaymatrix)
1972 need_reinit = 1;
1973
1974
2/2
✓ Branch 0 taken 6028 times.
✓ Branch 1 taken 383175 times.
389203 if (need_reinit) {
1975 6028 ret = ifilter_parameters_from_frame(ifilter, frame);
1976
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6028 times.
6028 if (ret < 0)
1977 return ret;
1978 }
1979
1980 /* (re)init the graph if possible, otherwise buffer the frame and return */
1981
3/4
✓ Branch 0 taken 383175 times.
✓ Branch 1 taken 6028 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 383175 times.
389203 if (need_reinit || !fg->graph) {
1982
2/2
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 5994 times.
6028 if (!ifilter_has_all_input_formats(fg)) {
1983 34 AVFrame *tmp = av_frame_clone(frame);
1984
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (!tmp)
1985 return AVERROR(ENOMEM);
1986
1987 34 ret = av_fifo_write(ifilter->frame_queue, &tmp, 1);
1988
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (ret < 0)
1989 av_frame_free(&tmp);
1990
1991 34 return ret;
1992 }
1993
1994 5994 ret = reap_filters(1);
1995
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5994 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5994 if (ret < 0 && ret != AVERROR_EOF) {
1996 av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
1997 return ret;
1998 }
1999
2000 5994 ret = configure_filtergraph(fg);
2001
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5994 times.
5994 if (ret < 0) {
2002 av_log(NULL, AV_LOG_ERROR, "Error reinitializing filters!\n");
2003 return ret;
2004 }
2005 }
2006
2007 389169 ret = av_buffersrc_add_frame_flags(ifilter->filter, frame, buffersrc_flags);
2008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 389169 times.
389169 if (ret < 0) {
2009 if (ret != AVERROR_EOF)
2010 av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
2011 return ret;
2012 }
2013
2014 389169 return 0;
2015 }
2016
2017 5981 static int ifilter_send_eof(InputFilter *ifilter, int64_t pts)
2018 {
2019 int ret;
2020
2021 5981 ifilter->eof = 1;
2022
2023
2/2
✓ Branch 0 taken 5980 times.
✓ Branch 1 taken 1 times.
5981 if (ifilter->filter) {
2024 5980 ret = av_buffersrc_close(ifilter->filter, pts, AV_BUFFERSRC_FLAG_PUSH);
2025
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5980 times.
5980 if (ret < 0)
2026 return ret;
2027 } else {
2028 // the filtergraph was never configured
2029
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ifilter->format < 0) {
2030 1 ret = ifilter_parameters_from_codecpar(ifilter, ifilter->ist->st->codecpar);
2031
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
2032 return ret;
2033 }
2034
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 if (ifilter->format < 0 && (ifilter->type == AVMEDIA_TYPE_AUDIO || ifilter->type == AVMEDIA_TYPE_VIDEO)) {
2035 av_log(NULL, AV_LOG_ERROR, "Cannot determine format of input stream %d:%d after EOF\n", ifilter->ist->file_index, ifilter->ist->st->index);
2036 return AVERROR_INVALIDDATA;
2037 }
2038 }
2039
2040 5981 return 0;
2041 }
2042
2043 // This does not quite work like avcodec_decode_audio4/avcodec_decode_video2.
2044 // There is the following difference: if you got a frame, you must call
2045 // it again with pkt=NULL. pkt==NULL is treated differently from pkt->size==0
2046 // (pkt==NULL means get more output, pkt->size==0 is a flush/drain packet)
2047 758664 static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
2048 {
2049 int ret;
2050
2051 758664 *got_frame = 0;
2052
2053
2/2
✓ Branch 0 taken 371448 times.
✓ Branch 1 taken 387216 times.
758664 if (pkt) {
2054 371448 ret = avcodec_send_packet(avctx, pkt);
2055 // In particular, we don't expect AVERROR(EAGAIN), because we read all
2056 // decoded frames with avcodec_receive_frame() until done.
2057
4/4
✓ Branch 0 taken 1043 times.
✓ Branch 1 taken 370405 times.
✓ Branch 2 taken 381 times.
✓ Branch 3 taken 662 times.
371448 if (ret < 0 && ret != AVERROR_EOF)
2058 381 return ret;
2059 }
2060
2061 758283 ret = avcodec_receive_frame(avctx, frame);
2062
4/4
✓ Branch 0 taken 370393 times.
✓ Branch 1 taken 387890 times.
✓ Branch 2 taken 5970 times.
✓ Branch 3 taken 364423 times.
758283 if (ret < 0 && ret != AVERROR(EAGAIN))
2063 5970 return ret;
2064
2/2
✓ Branch 0 taken 387890 times.
✓ Branch 1 taken 364423 times.
752313 if (ret >= 0)
2065 387890 *got_frame = 1;
2066
2067 752313 return 0;
2068 }
2069
2070 387890 static int send_frame_to_filters(InputStream *ist, AVFrame *decoded_frame)
2071 {
2072 int i, ret;
2073
2074 av_assert1(ist->nb_filters > 0); /* ensure ret is initialized */
2075
2/2
✓ Branch 0 taken 389203 times.
✓ Branch 1 taken 387890 times.
777093 for (i = 0; i < ist->nb_filters; i++) {
2076 389203 ret = ifilter_send_frame(ist->filters[i], decoded_frame, i < ist->nb_filters - 1);
2077
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 389203 times.
389203 if (ret == AVERROR_EOF)
2078 ret = 0; /* ignore */
2079
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 389203 times.
389203 if (ret < 0) {
2080 av_log(NULL, AV_LOG_ERROR,
2081 "Failed to inject frame into filter network: %s\n", av_err2str(ret));
2082 break;
2083 }
2084 }
2085 387890 return ret;
2086 }
2087
2088 548130 static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output,
2089 int *decode_failed)
2090 {
2091 548130 AVFrame *decoded_frame = ist->decoded_frame;
2092 548130 AVCodecContext *avctx = ist->dec_ctx;
2093 548130 int ret, err = 0;
2094 AVRational decoded_frame_tb;
2095
2096 548130 update_benchmark(NULL);
2097 548130 ret = decode(avctx, decoded_frame, got_output, pkt);
2098 548130 update_benchmark("decode_audio %d.%d", ist->file_index, ist->st->index);
2099
2/2
✓ Branch 0 taken 1154 times.
✓ Branch 1 taken 546976 times.
548130 if (ret < 0)
2100 1154 *decode_failed = 1;
2101
2102
3/4
✓ Branch 0 taken 546976 times.
✓ Branch 1 taken 1154 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 546976 times.
548130 if (ret >= 0 && avctx->sample_rate <= 0) {
2103 av_log(avctx, AV_LOG_ERROR, "Sample rate %d invalid\n", avctx->sample_rate);
2104 ret = AVERROR_INVALIDDATA;
2105 }
2106
2107
2/2
✓ Branch 0 taken 546987 times.
✓ Branch 1 taken 1143 times.
548130 if (ret != AVERROR_EOF)
2108 546987 check_decode_result(ist, got_output, ret);
2109
2110
3/4
✓ Branch 0 taken 287103 times.
✓ Branch 1 taken 261027 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 287103 times.
548130 if (!*got_output || ret < 0)
2111 261027 return ret;
2112
2113 287103 ist->samples_decoded += decoded_frame->nb_samples;
2114 287103 ist->frames_decoded++;
2115
2116 /* increment next_dts to use for the case where the input stream does not
2117 have timestamps or there are multiple frames in the packet */
2118 287103 ist->next_pts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
2119 287103 avctx->sample_rate;
2120 287103 ist->next_dts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
2121 287103 avctx->sample_rate;
2122
2123
2/2
✓ Branch 0 taken 258596 times.
✓ Branch 1 taken 28507 times.
287103 if (decoded_frame->pts != AV_NOPTS_VALUE) {
2124 258596 decoded_frame_tb = ist->st->time_base;
2125
4/4
✓ Branch 0 taken 1167 times.
✓ Branch 1 taken 27340 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1164 times.
28507 } else if (pkt && pkt->pts != AV_NOPTS_VALUE) {
2126 3 decoded_frame->pts = pkt->pts;
2127 3 decoded_frame_tb = ist->st->time_base;
2128 }else {
2129 28504 decoded_frame->pts = ist->dts;
2130 28504 decoded_frame_tb = AV_TIME_BASE_Q;
2131 }
2132
6/6
✓ Branch 0 taken 259763 times.
✓ Branch 1 taken 27340 times.
✓ Branch 2 taken 254748 times.
✓ Branch 3 taken 5015 times.
✓ Branch 4 taken 253644 times.
✓ Branch 5 taken 1104 times.
287103 if (pkt && pkt->duration && ist->prev_pkt_pts != AV_NOPTS_VALUE &&
2133
3/4
✓ Branch 0 taken 253644 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8441 times.
✓ Branch 3 taken 245203 times.
253644 pkt->pts != AV_NOPTS_VALUE && pkt->pts - ist->prev_pkt_pts > pkt->duration)
2134 8441 ist->filter_in_rescale_delta_last = AV_NOPTS_VALUE;
2135
2/2
✓ Branch 0 taken 259763 times.
✓ Branch 1 taken 27340 times.
287103 if (pkt)
2136 259763 ist->prev_pkt_pts = pkt->pts;
2137
1/2
✓ Branch 0 taken 287103 times.
✗ Branch 1 not taken.
287103 if (decoded_frame->pts != AV_NOPTS_VALUE)
2138 287103 decoded_frame->pts = av_rescale_delta(decoded_frame_tb, decoded_frame->pts,
2139 287103 (AVRational){1, avctx->sample_rate}, decoded_frame->nb_samples, &ist->filter_in_rescale_delta_last,
2140 287103 (AVRational){1, avctx->sample_rate});
2141 287103 ist->nb_samples = decoded_frame->nb_samples;
2142 287103 err = send_frame_to_filters(ist, decoded_frame);
2143
2144 287103 av_frame_unref(decoded_frame);
2145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 287103 times.
287103 return err < 0 ? err : ret;
2146 }
2147
2148 212007 static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output, int64_t *duration_pts, int eof,
2149 int *decode_failed)
2150 {
2151 212007 AVFrame *decoded_frame = ist->decoded_frame;
2152 212007 int i, ret = 0, err = 0;
2153 int64_t best_effort_timestamp;
2154 212007 int64_t dts = AV_NOPTS_VALUE;
2155
2156 // With fate-indeo3-2, we're getting 0-sized packets before EOF for some
2157 // reason. This seems like a semi-critical bug. Don't trigger EOF, and
2158 // skip the packet.
2159
6/6
✓ Branch 0 taken 206541 times.
✓ Branch 1 taken 5466 times.
✓ Branch 2 taken 106398 times.
✓ Branch 3 taken 100143 times.
✓ Branch 4 taken 1473 times.
✓ Branch 5 taken 104925 times.
212007 if (!eof && pkt && pkt->size == 0)
2160 1473 return 0;
2161
2162
2/2
✓ Branch 0 taken 210033 times.
✓ Branch 1 taken 501 times.
210534 if (ist->dts != AV_NOPTS_VALUE)
2163 210033 dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ist->st->time_base);
2164
2/2
✓ Branch 0 taken 110391 times.
✓ Branch 1 taken 100143 times.
210534 if (pkt) {
2165 110391 pkt->dts = dts; // ffmpeg.c probably shouldn't do this
2166 }
2167
2168 // The old code used to set dts on the drain packet, which does not work
2169 // with the new API anymore.
2170
2/2
✓ Branch 0 taken 5466 times.
✓ Branch 1 taken 205068 times.
210534 if (eof) {
2171 5466 void *new = av_realloc_array(ist->dts_buffer, ist->nb_dts_buffer + 1, sizeof(ist->dts_buffer[0]));
2172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5466 times.
5466 if (!new)
2173 return AVERROR(ENOMEM);
2174 5466 ist->dts_buffer = new;
2175 5466 ist->dts_buffer[ist->nb_dts_buffer++] = dts;
2176 }
2177
2178 210534 update_benchmark(NULL);
2179 210534 ret = decode(ist->dec_ctx, decoded_frame, got_output, pkt);
2180 210534 update_benchmark("decode_video %d.%d", ist->file_index, ist->st->index);
2181
2/2
✓ Branch 0 taken 5197 times.
✓ Branch 1 taken 205337 times.
210534 if (ret < 0)
2182 5197 *decode_failed = 1;
2183
2184 // The following line may be required in some cases where there is no parser
2185 // or the parser does not has_b_frames correctly
2186
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 210533 times.
210534 if (ist->st->codecpar->video_delay < ist->dec_ctx->has_b_frames) {
2187
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ist->dec_ctx->codec_id == AV_CODEC_ID_H264) {
2188 1 ist->st->codecpar->video_delay = ist->dec_ctx->has_b_frames;
2189 } else
2190 av_log(ist->dec_ctx, AV_LOG_WARNING,
2191 "video_delay is larger in decoder than demuxer %d > %d.\n"
2192 "If you want to help, upload a sample "
2193 "of this file to https://streams.videolan.org/upload/ "
2194 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n",
2195 ist->dec_ctx->has_b_frames,
2196 ist->st->codecpar->video_delay);
2197 }
2198
2199
2/2
✓ Branch 0 taken 205712 times.
✓ Branch 1 taken 4822 times.
210534 if (ret != AVERROR_EOF)
2200 205712 check_decode_result(ist, got_output, ret);
2201
2202
3/4
✓ Branch 0 taken 100787 times.
✓ Branch 1 taken 109747 times.
✓ Branch 2 taken 100787 times.
✗ Branch 3 not taken.
210534 if (*got_output && ret >= 0) {
2203
2/2
✓ Branch 0 taken 100778 times.
✓ Branch 1 taken 9 times.
100787 if (ist->dec_ctx->width != decoded_frame->width ||
2204
2/2
✓ Branch 0 taken 100776 times.
✓ Branch 1 taken 2 times.
100778 ist->dec_ctx->height != decoded_frame->height ||
2205
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 100772 times.
100776 ist->dec_ctx->pix_fmt != decoded_frame->format) {
2206 15 av_log(NULL, AV_LOG_DEBUG, "Frame parameters mismatch context %d,%d,%d != %d,%d,%d\n",
2207 decoded_frame->width,
2208 decoded_frame->height,
2209 decoded_frame->format,
2210 15 ist->dec_ctx->width,
2211 15 ist->dec_ctx->height,
2212 15 ist->dec_ctx->pix_fmt);
2213 }
2214 }
2215
2216
3/4
✓ Branch 0 taken 100787 times.
✓ Branch 1 taken 109747 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 100787 times.
210534 if (!*got_output || ret < 0)
2217 109747 return ret;
2218
2219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100787 times.
100787 if(ist->top_field_first>=0)
2220 decoded_frame->top_field_first = ist->top_field_first;
2221
2222 100787 ist->frames_decoded++;
2223
2224
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 100787 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
100787 if (ist->hwaccel_retrieve_data && decoded_frame->format == ist->hwaccel_pix_fmt) {
2225 err = ist->hwaccel_retrieve_data(ist->dec_ctx, decoded_frame);
2226 if (err < 0)
2227 goto fail;
2228 }
2229 100787 ist->hwaccel_retrieved_pix_fmt = decoded_frame->format;
2230
2231 100787 best_effort_timestamp= decoded_frame->best_effort_timestamp;
2232 100787 *duration_pts = decoded_frame->pkt_duration;
2233
2234
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 100682 times.
100787 if (ist->framerate.num)
2235 105 best_effort_timestamp = ist->cfr_next_pts++;
2236
2237
5/6
✓ Branch 0 taken 644 times.
✓ Branch 1 taken 100143 times.
✓ Branch 2 taken 528 times.
✓ Branch 3 taken 116 times.
✓ Branch 4 taken 528 times.
✗ Branch 5 not taken.
100787 if (eof && best_effort_timestamp == AV_NOPTS_VALUE && ist->nb_dts_buffer > 0) {
2238 528 best_effort_timestamp = ist->dts_buffer[0];
2239
2240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 528 times.
528 for (i = 0; i < ist->nb_dts_buffer - 1; i++)
2241 ist->dts_buffer[i] = ist->dts_buffer[i + 1];
2242 528 ist->nb_dts_buffer--;
2243 }
2244
2245
1/2
✓ Branch 0 taken 100787 times.
✗ Branch 1 not taken.
100787 if(best_effort_timestamp != AV_NOPTS_VALUE) {
2246 100787 int64_t ts = av_rescale_q(decoded_frame->pts = best_effort_timestamp, ist->st->time_base, AV_TIME_BASE_Q);
2247
2248
1/2
✓ Branch 0 taken 100787 times.
✗ Branch 1 not taken.
100787 if (ts != AV_NOPTS_VALUE)
2249 100787 ist->next_pts = ist->pts = ts;
2250 }
2251
2252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100787 times.
100787 if (debug_ts) {
2253 av_log(NULL, AV_LOG_INFO, "decoder -> ist_index:%d type:video "
2254 "frame_pts:%s frame_pts_time:%s best_effort_ts:%"PRId64" best_effort_ts_time:%s keyframe:%d frame_type:%d time_base:%d/%d\n",
2255 ist->st->index, av_ts2str(decoded_frame->pts),
2256 av_ts2timestr(decoded_frame->pts, &ist->st->time_base),
2257 best_effort_timestamp,
2258 av_ts2timestr(best_effort_timestamp, &ist->st->time_base),
2259 decoded_frame->key_frame, decoded_frame->pict_type,
2260 ist->st->time_base.num, ist->st->time_base.den);
2261 }
2262
2263
2/2
✓ Branch 0 taken 5081 times.
✓ Branch 1 taken 95706 times.
100787 if (ist->st->sample_aspect_ratio.num)
2264 5081 decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
2265
2266 100787 err = send_frame_to_filters(ist, decoded_frame);
2267
2268 100787 fail:
2269 100787 av_frame_unref(decoded_frame);
2270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100787 times.
100787 return err < 0 ? err : ret;
2271 }
2272
2273 1566 static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
2274 int *decode_failed)
2275 {
2276 AVSubtitle subtitle;
2277 1566 int free_sub = 1;
2278 1566 int i, ret = avcodec_decode_subtitle2(ist->dec_ctx,
2279 &subtitle, got_output, pkt);
2280
2281 1566 check_decode_result(NULL, got_output, ret);
2282
2283
4/4
✓ Branch 0 taken 1487 times.
✓ Branch 1 taken 79 times.
✓ Branch 2 taken 662 times.
✓ Branch 3 taken 825 times.
1566 if (ret < 0 || !*got_output) {
2284 741 *decode_failed = 1;
2285
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 702 times.
741 if (!pkt->size)
2286 39 sub2video_flush(ist);
2287 741 return ret;
2288 }
2289
2290
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 817 times.
825 if (ist->fix_sub_duration) {
2291 8 int end = 1;
2292
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 if (ist->prev_sub.got_output) {
2293 7 end = av_rescale(subtitle.pts - ist->prev_sub.subtitle.pts,
2294 1000, AV_TIME_BASE);
2295
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (end < ist->prev_sub.subtitle.end_display_time) {
2296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 av_log(ist->dec_ctx, AV_LOG_DEBUG,
2297 "Subtitle duration reduced from %"PRId32" to %d%s\n",
2298 ist->prev_sub.subtitle.end_display_time, end,
2299 end <= 0 ? ", dropping it" : "");
2300 7 ist->prev_sub.subtitle.end_display_time = end;
2301 }
2302 }
2303 8 FFSWAP(int, *got_output, ist->prev_sub.got_output);
2304 8 FFSWAP(int, ret, ist->prev_sub.ret);
2305 8 FFSWAP(AVSubtitle, subtitle, ist->prev_sub.subtitle);
2306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (end <= 0)
2307 goto out;
2308 }
2309
2310
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 824 times.
825 if (!*got_output)
2311 1 return ret;
2312
2313
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 736 times.
824 if (ist->sub2video.frame) {
2314 88 sub2video_update(ist, INT64_MIN, &subtitle);
2315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 736 times.
736 } else if (ist->nb_filters) {
2316 if (!ist->sub2video.sub_queue)
2317 ist->sub2video.sub_queue = av_fifo_alloc2(8, sizeof(AVSubtitle), AV_FIFO_FLAG_AUTO_GROW);
2318 if (!ist->sub2video.sub_queue)
2319 exit_program(1);
2320
2321 ret = av_fifo_write(ist->sub2video.sub_queue, &subtitle, 1);
2322 if (ret < 0)
2323 exit_program(1);
2324 free_sub = 0;
2325 }
2326
2327
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 815 times.
824 if (!subtitle.num_rects)
2328 9 goto out;
2329
2330 815 ist->frames_decoded++;
2331
2332
2/2
✓ Branch 0 taken 859 times.
✓ Branch 1 taken 815 times.
1674 for (i = 0; i < nb_output_streams; i++) {
2333 859 OutputStream *ost = output_streams[i];
2334
2335
3/4
✓ Branch 1 taken 814 times.
✓ Branch 2 taken 45 times.
✓ Branch 3 taken 814 times.
✗ Branch 4 not taken.
859 if (!check_output_constraints(ist, ost) || !ost->encoding_needed
2336
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 770 times.
814 || ost->enc->type != AVMEDIA_TYPE_SUBTITLE)
2337 89 continue;
2338
2339 770 do_subtitle_out(output_files[ost->file_index], ost, &subtitle);
2340 }
2341
2342 815 out:
2343
1/2
✓ Branch 0 taken 824 times.
✗ Branch 1 not taken.
824 if (free_sub)
2344 824 avsubtitle_free(&subtitle);
2345 824 return ret;
2346 }
2347
2348 6004 static int send_filter_eof(InputStream *ist)
2349 {
2350 int i, ret;
2351 /* TODO keep pts also in stream time base to avoid converting back */
2352 6004 int64_t pts = av_rescale_q_rnd(ist->pts, AV_TIME_BASE_Q, ist->st->time_base,
2353 AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
2354
2355
2/2
✓ Branch 0 taken 5981 times.
✓ Branch 1 taken 6004 times.
11985 for (i = 0; i < ist->nb_filters; i++) {
2356 5981 ret = ifilter_send_eof(ist->filters[i], pts);
2357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5981 times.
5981 if (ret < 0)
2358 return ret;
2359 }
2360 6004 return 0;
2361 }
2362
2363 /* pkt = NULL means EOF (needed to flush decoder buffers) */
2364 418962 static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
2365 {
2366 418962 int ret = 0, i;
2367 418962 int repeating = 0;
2368 418962 int eof_reached = 0;
2369
2370 418962 AVPacket *avpkt = ist->pkt;
2371
2372
2/2
✓ Branch 0 taken 6477 times.
✓ Branch 1 taken 412485 times.
418962 if (!ist->saw_first_ts) {
2373 6477 ist->first_dts =
2374
2/2
✓ Branch 0 taken 4381 times.
✓ Branch 1 taken 2096 times.
6477 ist->dts = ist->st->avg_frame_rate.num ? - ist->dec_ctx->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
2375 6477 ist->pts = 0;
2376
6/6
✓ Branch 0 taken 6396 times.
✓ Branch 1 taken 81 times.
✓ Branch 2 taken 5889 times.
✓ Branch 3 taken 507 times.
✓ Branch 4 taken 304 times.
✓ Branch 5 taken 5585 times.
6477 if (pkt && pkt->pts != AV_NOPTS_VALUE && !ist->decoding_needed) {
2377 304 ist->first_dts =
2378 304 ist->dts += av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
2379 304 ist->pts = ist->dts; //unused but better to set it to a value thats not totally wrong
2380 }
2381 6477 ist->saw_first_ts = 1;
2382 }
2383
2384
2/2
✓ Branch 0 taken 6972 times.
✓ Branch 1 taken 411990 times.
418962 if (ist->next_dts == AV_NOPTS_VALUE)
2385 6972 ist->next_dts = ist->dts;
2386
2/2
✓ Branch 0 taken 6477 times.
✓ Branch 1 taken 412485 times.
418962 if (ist->next_pts == AV_NOPTS_VALUE)
2387 6477 ist->next_pts = ist->pts;
2388
2389
2/2
✓ Branch 0 taken 411817 times.
✓ Branch 1 taken 7145 times.
418962 if (pkt) {
2390 411817 av_packet_unref(avpkt);
2391 411817 ret = av_packet_ref(avpkt, pkt);
2392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 411817 times.
411817 if (ret < 0)
2393 return ret;
2394 }
2395
2396
4/4
✓ Branch 0 taken 411817 times.
✓ Branch 1 taken 7145 times.
✓ Branch 2 taken 375241 times.
✓ Branch 3 taken 36576 times.
418962 if (pkt && pkt->dts != AV_NOPTS_VALUE) {
2397 375241 ist->next_dts = ist->dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
2398
4/4
✓ Branch 0 taken 82153 times.
✓ Branch 1 taken 293088 times.
✓ Branch 2 taken 5956 times.
✓ Branch 3 taken 76197 times.
375241 if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_VIDEO || !ist->decoding_needed)
2399 299044 ist->next_pts = ist->pts = ist->dts;
2400 }
2401
2402 // while we have more to decode or while the decoder did output something on EOF
2403
2/2
✓ Branch 0 taken 762527 times.
✓ Branch 1 taken 44475 times.
807002 while (ist->decoding_needed) {
2404 762527 int64_t duration_dts = 0;
2405 762527 int64_t duration_pts = 0;
2406 762527 int got_output = 0;
2407 762527 int decode_failed = 0;
2408
2409 762527 ist->pts = ist->next_pts;
2410 762527 ist->dts = ist->next_dts;
2411
2412
3/4
✓ Branch 0 taken 548130 times.
✓ Branch 1 taken 212007 times.
✓ Branch 2 taken 2390 times.
✗ Branch 3 not taken.
762527 switch (ist->dec_ctx->codec_type) {
2413 548130 case AVMEDIA_TYPE_AUDIO:
2414
2/2
✓ Branch 0 taken 261057 times.
✓ Branch 1 taken 287073 times.
548130 ret = decode_audio (ist, repeating ? NULL : avpkt, &got_output,
2415 &decode_failed);
2416 548130 av_packet_unref(avpkt);
2417 548130 break;
2418 212007 case AVMEDIA_TYPE_VIDEO:
2419
2/2
✓ Branch 0 taken 111864 times.
✓ Branch 1 taken 100143 times.
212007 ret = decode_video (ist, repeating ? NULL : avpkt, &got_output, &duration_pts, !pkt,
2420 &decode_failed);
2421
5/6
✓ Branch 0 taken 100143 times.
✓ Branch 1 taken 111864 times.
✓ Branch 2 taken 100143 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 100133 times.
212007 if (!repeating || !pkt || got_output) {
2422
4/4
✓ Branch 0 taken 106408 times.
✓ Branch 1 taken 5466 times.
✓ Branch 2 taken 104708 times.
✓ Branch 3 taken 1700 times.
111874 if (pkt && pkt->duration) {
2423 104708 duration_dts = av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
2424
3/4
✓ Branch 0 taken 6057 times.
✓ Branch 1 taken 1109 times.
✓ Branch 2 taken 6057 times.
✗ Branch 3 not taken.
7166 } else if(ist->dec_ctx->framerate.num != 0 && ist->dec_ctx->framerate.den != 0) {
2425
2/2
✓ Branch 1 taken 2941 times.
✓ Branch 2 taken 3116 times.
6057 int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict+1 : ist->dec_ctx->ticks_per_frame;
2426 6057 duration_dts = ((int64_t)AV_TIME_BASE *
2427 6057 ist->dec_ctx->framerate.den * ticks) /
2428 6057 ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame;
2429 }
2430
2431
4/4
✓ Branch 0 taken 111858 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 110765 times.
✓ Branch 3 taken 1093 times.
111874 if(ist->dts != AV_NOPTS_VALUE && duration_dts) {
2432 110765 ist->next_dts += duration_dts;
2433 }else
2434 1109 ist->next_dts = AV_NOPTS_VALUE;
2435 }
2436
2437
2/2
✓ Branch 0 taken 100787 times.
✓ Branch 1 taken 111220 times.
212007 if (got_output) {
2438
2/2
✓ Branch 0 taken 99101 times.
✓ Branch 1 taken 1686 times.
100787 if (duration_pts > 0) {
2439 99101 ist->next_pts += av_rescale_q(duration_pts, ist->st->time_base, AV_TIME_BASE_Q);
2440 } else {
2441 1686 ist->next_pts += duration_dts;
2442 }
2443 }
2444 212007 av_packet_unref(avpkt);
2445 212007 break;
2446 2390 case AVMEDIA_TYPE_SUBTITLE:
2447
2/2
✓ Branch 0 taken 824 times.
✓ Branch 1 taken 1566 times.
2390 if (repeating)
2448 824 break;
2449 1566 ret = transcode_subtitles(ist, avpkt, &got_output, &decode_failed);
2450
3/4
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 1527 times.
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
1566 if (!pkt && ret >= 0)
2451 39 ret = AVERROR_EOF;
2452 1566 av_packet_unref(avpkt);
2453 1566 break;
2454 default:
2455 return -1;
2456 }
2457
2458
2/2
✓ Branch 0 taken 6004 times.
✓ Branch 1 taken 756523 times.
762527 if (ret == AVERROR_EOF) {
2459 6004 eof_reached = 1;
2460 374487 break;
2461 }
2462
2463
2/2
✓ Branch 0 taken 465 times.
✓ Branch 1 taken 756058 times.
756523 if (ret < 0) {
2464
1/2
✓ Branch 0 taken 465 times.
✗ Branch 1 not taken.
465 if (decode_failed) {
2465 930 av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d: %s\n",
2466 465 ist->file_index, ist->st->index, av_err2str(ret));
2467 } else {
2468 av_log(NULL, AV_LOG_FATAL, "Error while processing the decoded "
2469 "data for stream #%d:%d\n", ist->file_index, ist->st->index);
2470 }
2471
2/4
✓ Branch 0 taken 465 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 465 times.
465 if (!decode_failed || exit_on_error)
2472 exit_program(1);
2473 465 break;
2474 }
2475
2476
2/2
✓ Branch 0 taken 388714 times.
✓ Branch 1 taken 367344 times.
756058 if (got_output)
2477 388714 ist->got_output = 1;
2478
2479
2/2
✓ Branch 0 taken 367344 times.
✓ Branch 1 taken 388714 times.
756058 if (!got_output)
2480 367344 break;
2481
2482 // During draining, we might get multiple output frames in this loop.
2483 // ffmpeg.c does not drain the filter chain on configuration changes,
2484 // which means if we send multiple frames at once to the filters, and
2485 // one of those frames changes configuration, the buffered frames will
2486 // be lost. This can upset certain FATE tests.
2487 // Decode only 1 frame per call on EOF to appease these FATE tests.
2488 // The ideal solution would be to rewrite decoding to use the new
2489 // decoding API in a better way.
2490
2/2
✓ Branch 0 taken 674 times.
✓ Branch 1 taken 388040 times.
388714 if (!pkt)
2491 674 break;
2492
2493 388040 repeating = 1;
2494 }
2495
2496 /* after flushing, send an EOF on all the filter inputs attached to the stream */
2497 /* except when looping we need to flush but not to send an EOF */
2498
7/8
✓ Branch 0 taken 7145 times.
✓ Branch 1 taken 411817 times.
✓ Branch 2 taken 6679 times.
✓ Branch 3 taken 466 times.
✓ Branch 4 taken 6004 times.
✓ Branch 5 taken 675 times.
✓ Branch 6 taken 6004 times.
✗ Branch 7 not taken.
418962 if (!pkt && ist->decoding_needed && eof_reached && !no_eof) {
2499 6004 int ret = send_filter_eof(ist);
2500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6004 times.
6004 if (ret < 0) {
2501 av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\n");
2502 exit_program(1);
2503 }
2504 }
2505
2506 /* handle stream copy */
2507
4/4
✓ Branch 0 taken 44475 times.
✓ Branch 1 taken 374487 times.
✓ Branch 2 taken 44009 times.
✓ Branch 3 taken 466 times.
418962 if (!ist->decoding_needed && pkt) {
2508 44009 ist->dts = ist->next_dts;
2509
3/3
✓ Branch 0 taken 32012 times.
✓ Branch 1 taken 11185 times.
✓ Branch 2 taken 812 times.
44009 switch (ist->dec_ctx->codec_type) {
2510 32012 case AVMEDIA_TYPE_AUDIO:
2511 av_assert1(pkt->duration >= 0);
2512
1/2
✓ Branch 0 taken 32012 times.
✗ Branch 1 not taken.
32012 if (ist->dec_ctx->sample_rate) {
2513 32012 ist->next_dts += ((int64_t)AV_TIME_BASE * ist->dec_ctx->frame_size) /
2514 32012 ist->dec_ctx->sample_rate;
2515 } else {
2516 ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
2517 }
2518 32012 break;
2519 11185 case AVMEDIA_TYPE_VIDEO:
2520
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 11106 times.
11185 if (ist->framerate.num) {
2521 // TODO: Remove work-around for c99-to-c89 issue 7
2522 79 AVRational time_base_q = AV_TIME_BASE_Q;
2523 79 int64_t next_dts = av_rescale_q(ist->next_dts, time_base_q, av_inv_q(ist->framerate));
2524 79 ist->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
2525
2/2
✓ Branch 0 taken 10715 times.
✓ Branch 1 taken 391 times.
11106 } else if (pkt->duration) {
2526 10715 ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
2527
2/2
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 143 times.
391 } else if(ist->dec_ctx->framerate.num != 0) {
2528
2/2
✓ Branch 1 taken 65 times.
✓ Branch 2 taken 183 times.
248 int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame;
2529 248 ist->next_dts += ((int64_t)AV_TIME_BASE *
2530 248 ist->dec_ctx->framerate.den * ticks) /
2531 248 ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame;
2532 }
2533 11185 break;
2534 }
2535 44009 ist->pts = ist->dts;
2536 44009 ist->next_pts = ist->next_dts;
2537
2/2
✓ Branch 0 taken 466 times.
✓ Branch 1 taken 374487 times.
374953 } else if (!ist->decoding_needed)
2538 466 eof_reached = 1;
2539
2540
2/2
✓ Branch 0 taken 447649 times.
✓ Branch 1 taken 418962 times.
866611 for (i = 0; i < nb_output_streams; i++) {
2541 447649 OutputStream *ost = output_streams[i];
2542
2543
4/4
✓ Branch 1 taken 418206 times.
✓ Branch 2 taken 29443 times.
✓ Branch 3 taken 370902 times.
✓ Branch 4 taken 47304 times.
447649 if (!check_output_constraints(ist, ost) || ost->encoding_needed)
2544 400345 continue;
2545
2546 47304 do_streamcopy(ist, ost, pkt);
2547 }
2548
2549 418962 return !eof_reached;
2550 }
2551
2552 967 static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
2553 {
2554 967 InputStream *ist = s->opaque;
2555 const enum AVPixelFormat *p;
2556 int ret;
2557
2558
1/2
✓ Branch 0 taken 2595 times.
✗ Branch 1 not taken.
2595 for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
2559 2595 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
2560 2595 const AVCodecHWConfig *config = NULL;
2561 int i;
2562
2563
2/2
✓ Branch 0 taken 967 times.
✓ Branch 1 taken 1628 times.
2595 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
2564 967 break;
2565
2566
1/2
✓ Branch 0 taken 1628 times.
✗ Branch 1 not taken.
1628 if (ist->hwaccel_id == HWACCEL_GENERIC ||
2567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1628 times.
1628 ist->hwaccel_id == HWACCEL_AUTO) {
2568 for (i = 0;; i++) {
2569 config = avcodec_get_hw_config(s->codec, i);
2570 if (!config)
2571 break;
2572 if (!(config->methods &
2573 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
2574 continue;
2575 if (config->pix_fmt == *p)
2576 break;
2577 }
2578 }
2579
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1628 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1628 if (config && config->device_type == ist->hwaccel_device_type) {
2580 ret = hwaccel_decode_init(s);
2581 if (ret < 0) {
2582 if (ist->hwaccel_id == HWACCEL_GENERIC) {
2583 av_log(NULL, AV_LOG_FATAL,
2584 "%s hwaccel requested for input stream #%d:%d, "
2585 "but cannot be initialized.\n",
2586 av_hwdevice_get_type_name(config->device_type),
2587 ist->file_index, ist->st->index);
2588 return AV_PIX_FMT_NONE;
2589 }
2590 continue;
2591 }
2592
2593 ist->hwaccel_pix_fmt = *p;
2594 break;
2595 }
2596 }
2597
2598 967 return *p;
2599 }
2600
2601 6695 static int init_input_stream(int ist_index, char *error, int error_len)
2602 {
2603 int ret;
2604 6695 InputStream *ist = input_streams[ist_index];
2605
2606
2/2
✓ Branch 0 taken 6017 times.
✓ Branch 1 taken 678 times.
6695 if (ist->decoding_needed) {
2607 6017 const AVCodec *codec = ist->dec;
2608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6017 times.
6017 if (!codec) {
2609 snprintf(error, error_len, "Decoder (codec %s) not found for input stream #%d:%d",
2610 avcodec_get_name(ist->dec_ctx->codec_id), ist->file_index, ist->st->index);
2611 return AVERROR(EINVAL);
2612 }
2613
2614 6017 ist->dec_ctx->opaque = ist;
2615 6017 ist->dec_ctx->get_format = get_format;
2616 #if LIBAVCODEC_VERSION_MAJOR < 60
2617 6017 AV_NOWARN_DEPRECATED({
2618 ist->dec_ctx->thread_safe_callbacks = 1;
2619 })
2620 #endif
2621
2622
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6016 times.
6017 if (ist->dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
2623
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (ist->decoding_needed & DECODING_FOR_OST)) {
2624 1 av_dict_set(&ist->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
2625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ist->decoding_needed & DECODING_FOR_FILTER)
2626 av_log(NULL, AV_LOG_WARNING, "Warning using DVB subtitles for filtering and output at the same time is not fully supported, also see -compute_edt [0|1]\n");
2627 }
2628
2629 /* Useful for subtitles retiming by lavf (FIXME), skipping samples in
2630 * audio, and video decoders such as cuvid or mediacodec */
2631 6017 ist->dec_ctx->pkt_timebase = ist->st->time_base;
2632
2633
2/2
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 5961 times.
6017 if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
2634 56 av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
2635 /* Attached pics are sparse, therefore we would not want to delay their decoding till EOF. */
2636
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 6012 times.
6017 if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
2637 5 av_dict_set(&ist->decoder_opts, "threads", "1", 0);
2638
2639 6017 ret = hw_device_setup_for_decode(ist);
2640
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6017 times.
6017 if (ret < 0) {
2641 snprintf(error, error_len, "Device setup failed for "
2642 "decoder on input stream #%d:%d : %s",
2643 ist->file_index, ist->st->index, av_err2str(ret));
2644 return ret;
2645 }
2646
2647
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6017 times.
6017 if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
2648 if (ret == AVERROR_EXPERIMENTAL)
2649 abort_codec_experimental(codec, 0);
2650
2651 snprintf(error, error_len,
2652 "Error while opening decoder for input stream "
2653 "#%d:%d : %s",
2654 ist->file_index, ist->st->index, av_err2str(ret));
2655 return ret;
2656 }
2657 6017 assert_avoptions(ist->decoder_opts);
2658 }
2659
2660 6695 ist->next_pts = AV_NOPTS_VALUE;
2661 6695 ist->next_dts = AV_NOPTS_VALUE;
2662
2663 6695 return 0;
2664 }
2665
2666 18548 static InputStream *get_input_stream(OutputStream *ost)
2667 {
2668
2/2
✓ Branch 0 taken 18287 times.
✓ Branch 1 taken 261 times.
18548 if (ost->source_index >= 0)
2669 18287 return input_streams[ost->source_index];
2670 261 return NULL;
2671 }
2672
2673 1 static int compare_int64(const void *a, const void *b)
2674 {
2675 1 return FFDIFFSIGN(*(const int64_t *)a, *(const int64_t *)b);
2676 }
2677
2678 6479 static int init_output_bsfs(OutputStream *ost)
2679 {
2680 6479 AVBSFContext *ctx = ost->bsf_ctx;
2681 int ret;
2682
2683
2/2
✓ Branch 0 taken 6387 times.
✓ Branch 1 taken 92 times.
6479 if (!ctx)
2684 6387 return 0;
2685
2686 92 ret = avcodec_parameters_copy(ctx->par_in, ost->st->codecpar);
2687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
92 if (ret < 0)
2688 return ret;
2689
2690 92 ctx->time_base_in = ost->st->time_base;
2691
2692 92 ret = av_bsf_init(ctx);
2693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
92 if (ret < 0) {
2694 av_log(NULL, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n",
2695 ctx->filter->name);
2696 return ret;
2697 }
2698
2699 92 ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
2700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92 times.
92 if (ret < 0)
2701 return ret;
2702 92 ost->st->time_base = ctx->time_base_out;
2703
2704 92 return 0;
2705 }
2706
2707 425 static int init_output_stream_streamcopy(OutputStream *ost)
2708 {
2709 425 OutputFile *of = output_files[ost->file_index];
2710 425 InputStream *ist = get_input_stream(ost);
2711 425 AVCodecParameters *par_dst = ost->st->codecpar;
2712 425 AVCodecParameters *par_src = ost->ref_par;
2713 AVRational sar;
2714 int i, ret;
2715 425 uint32_t codec_tag = par_dst->codec_tag;
2716
2717
2/4
✓ Branch 0 taken 425 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 425 times.
425 av_assert0(ist && !ost->filter);
2718
2719 425 ret = avcodec_parameters_to_context(ost->enc_ctx, ist->st->codecpar);
2720
1/2
✓ Branch 0 taken 425 times.
✗ Branch 1 not taken.
425 if (ret >= 0)
2721 425 ret = av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
2722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 425 times.
425 if (ret < 0) {
2723 av_log(NULL, AV_LOG_FATAL,
2724 "Error setting up codec context options.\n");
2725 return ret;
2726 }
2727
2728 425 ret = avcodec_parameters_from_context(par_src, ost->enc_ctx);
2729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 425 times.
425 if (ret < 0) {
2730 av_log(NULL, AV_LOG_FATAL,
2731 "Error getting reference codec parameters.\n");
2732 return ret;
2733 }
2734
2735
2/2
✓ Branch 0 taken 419 times.
✓ Branch 1 taken 6 times.
425 if (!codec_tag) {
2736 unsigned int codec_tag_tmp;
2737
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 333 times.
419 if (!of->format->codec_tag ||
2738
4/4
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 48 times.
144 av_codec_get_id (of->format->codec_tag, par_src->codec_tag) == par_src->codec_id ||
2739 58 !av_codec_get_tag2(of->format->codec_tag, par_src->codec_id, &codec_tag_tmp))
2740 371 codec_tag = par_src->codec_tag;
2741 }
2742
2743 425 ret = avcodec_parameters_copy(par_dst, par_src);
2744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 425 times.
425 if (ret < 0)
2745 return ret;
2746
2747 425 par_dst->codec_tag = codec_tag;
2748
2749
2/2
✓ Branch 0 taken 424 times.
✓ Branch 1 taken 1 times.
425 if (!ost->frame_rate.num)
2750 424 ost->frame_rate = ist->framerate;
2751
2752
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 423 times.
425 if (ost->frame_rate.num)
2753 2 ost->st->avg_frame_rate = ost->frame_rate;
2754 else
2755 423 ost->st->avg_frame_rate = ist->st->avg_frame_rate;
2756
2757 425 ret = avformat_transfer_internal_stream_timing_info(of->format, ost->st, ist->st, copy_tb);
2758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 425 times.
425 if (ret < 0)
2759 return ret;
2760
2761 // copy timebase while removing common factors
2762
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 424 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
425 if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) {
2763
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 423 times.
424 if (ost->frame_rate.num)
2764 1 ost->st->time_base = av_inv_q(ost->frame_rate);
2765 else
2766 423 ost->st->time_base = av_add_q(av_stream_get_codec_timebase(ost->st), (AVRational){0, 1});
2767 }
2768
2769 // copy estimated duration as a hint to the muxer
2770
3/4
✓ Branch 0 taken 425 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 258 times.
✓ Branch 3 taken 167 times.
425 if (ost->st->duration <= 0 && ist->st->duration > 0)
2771 258 ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
2772
2773
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 385 times.
425 if (ist->st->nb_side_data) {
2774
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 40 times.
88 for (i = 0; i < ist->st->nb_side_data; i++) {
2775 48 const AVPacketSideData *sd_src = &ist->st->side_data[i];
2776 uint8_t *dst_data;
2777
2778 48 dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
2779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (!dst_data)
2780 return AVERROR(ENOMEM);
2781 48 memcpy(dst_data, sd_src->data, sd_src->size);
2782 }
2783 }
2784
2785
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 424 times.
425 if (ost->rotate_overridden) {
2786 1 uint8_t *sd = av_stream_new_side_data(ost->st, AV_PKT_DATA_DISPLAYMATRIX,
2787 sizeof(int32_t) * 9);
2788
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (sd)
2789 1 av_display_rotation_set((int32_t *)sd, -ost->rotate_override_value);
2790 }
2791
2792
3/3
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 219 times.
✓ Branch 2 taken 38 times.
425 switch (par_dst->codec_type) {
2793 168 case AVMEDIA_TYPE_AUDIO:
2794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 if (audio_volume != 256) {
2795 av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
2796 exit_program(1);
2797 }
2798
5/8
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 163 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 163 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 5 times.
168 if((par_dst->block_align == 1 || par_dst->block_align == 1152 || par_dst->block_align == 576) && par_dst->codec_id == AV_CODEC_ID_MP3)
2799 par_dst->block_align= 0;
2800
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 157 times.
168 if(par_dst->codec_id == AV_CODEC_ID_AC3)
2801 11 par_dst->block_align= 0;
2802 168 break;
2803 219 case AVMEDIA_TYPE_VIDEO:
2804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 219 times.
219 if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
2805 sar =
2806 av_mul_q(ost->frame_aspect_ratio,
2807 (AVRational){ par_dst->height, par_dst->width });
2808 av_log(NULL, AV_LOG_WARNING, "Overriding aspect ratio "
2809 "with stream copy may produce invalid files\n");
2810 }
2811
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 157 times.
219 else if (ist->st->sample_aspect_ratio.num)
2812 62 sar = ist->st->sample_aspect_ratio;
2813 else
2814 157 sar = par_src->sample_aspect_ratio;
2815 219 ost->st->sample_aspect_ratio = par_dst->sample_aspect_ratio = sar;
2816 219 ost->st->avg_frame_rate = ist->st->avg_frame_rate;
2817 219 ost->st->r_frame_rate = ist->st->r_frame_rate;
2818 219 break;
2819 }
2820
2821 425 ost->mux_timebase = ist->st->time_base;
2822
2823 425 return 0;
2824 }
2825
2826 6053 static void set_encoder_id(OutputFile *of, OutputStream *ost)
2827 {
2828 const AVDictionaryEntry *e;
2829
2830 uint8_t *encoder_string;
2831 int encoder_string_len;
2832 6053 int format_flags = 0;
2833 6053 int codec_flags = ost->enc_ctx->flags;
2834
2835
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6053 times.
6053 if (av_dict_get(ost->st->metadata, "encoder", NULL, 0))
2836 return;
2837
2838 6053 e = av_dict_get(of->opts, "fflags", NULL, 0);
2839
2/2
✓ Branch 0 taken 3588 times.
✓ Branch 1 taken 2465 times.
6053 if (e) {
2840 3588 const AVOption *o = av_opt_find(of->ctx, "fflags", NULL, 0, 0);
2841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3588 times.
3588 if (!o)
2842 return;
2843 3588 av_opt_eval_flags(of->ctx, o, e->value, &format_flags);
2844 }
2845 6053 e = av_dict_get(ost->encoder_opts, "flags", NULL, 0);
2846
2/2
✓ Branch 0 taken 3587 times.
✓ Branch 1 taken 2466 times.
6053 if (e) {
2847 3587 const AVOption *o = av_opt_find(ost->enc_ctx, "flags", NULL, 0, 0);
2848
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3587 times.
3587 if (!o)
2849 return;
2850 3587 av_opt_eval_flags(ost->enc_ctx, o, e->value, &codec_flags);
2851 }
2852
2853 6053 encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2;
2854 6053 encoder_string = av_mallocz(encoder_string_len);
2855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6053 times.
6053 if (!encoder_string)
2856 exit_program(1);
2857
2858
4/4
✓ Branch 0 taken 2465 times.
✓ Branch 1 taken 3588 times.
✓ Branch 2 taken 815 times.
✓ Branch 3 taken 1650 times.
6053 if (!(format_flags & AVFMT_FLAG_BITEXACT) && !(codec_flags & AV_CODEC_FLAG_BITEXACT))
2859 815 av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
2860 else
2861 5238 av_strlcpy(encoder_string, "Lavc ", encoder_string_len);
2862 6053 av_strlcat(encoder_string, ost->enc->name, encoder_string_len);
2863 6053 av_dict_set(&ost->st->metadata, "encoder", encoder_string,
2864 AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
2865 }
2866
2867 1 static void parse_forced_key_frames(char *kf, OutputStream *ost,
2868 AVCodecContext *avctx)
2869 {
2870 char *p;
2871 1 int n = 1, i, size, index = 0;
2872 int64_t t, *pts;
2873
2874
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
14 for (p = kf; *p; p++)
2875
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 if (*p == ',')
2876 1 n++;
2877 1 size = n;
2878 1 pts = av_malloc_array(size, sizeof(*pts));
2879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!pts) {
2880 av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
2881 exit_program(1);
2882 }
2883
2884 1 p = kf;
2885
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < n; i++) {
2886 2 char *next = strchr(p, ',');
2887
2888
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (next)
2889 1 *next++ = 0;
2890
2891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!memcmp(p, "chapters", 8)) {
2892
2893 AVFormatContext *avf = output_files[ost->file_index]->ctx;
2894 int j;
2895
2896 if (avf->nb_chapters > INT_MAX - size ||
2897 !(pts = av_realloc_f(pts, size += avf->nb_chapters - 1,
2898 sizeof(*pts)))) {
2899 av_log(NULL, AV_LOG_FATAL,
2900 "Could not allocate forced key frames array.\n");
2901 exit_program(1);
2902 }
2903 t = p[8] ? parse_time_or_die("force_key_frames", p + 8, 1) : 0;
2904 t = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
2905
2906 for (j = 0; j < avf->nb_chapters; j++) {
2907 AVChapter *c = avf->chapters[j];
2908 av_assert1(index < size);
2909 pts[index++] = av_rescale_q(c->start, c->time_base,
2910 avctx->time_base) + t;
2911 }
2912
2913 } else {
2914
2915 2 t = parse_time_or_die("force_key_frames", p, 1);
2916 av_assert1(index < size);
2917 2 pts[index++] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
2918
2919 }
2920
2921 2 p = next;
2922 }
2923
2924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(index == size);
2925 1 qsort(pts, size, sizeof(*pts), compare_int64);
2926 1 ost->forced_kf_count = size;
2927 1 ost->forced_kf_pts = pts;
2928 1 }
2929
2930 6017 static void init_encoder_time_base(OutputStream *ost, AVRational default_time_base)
2931 {
2932 6017 InputStream *ist = get_input_stream(ost);
2933 6017 AVCodecContext *enc_ctx = ost->enc_ctx;
2934 AVFormatContext *oc;
2935
2936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6017 times.
6017 if (ost->enc_timebase.num > 0) {
2937 enc_ctx->time_base = ost->enc_timebase;
2938 return;
2939 }
2940
2941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6017 times.
6017 if (ost->enc_timebase.num < 0) {
2942 if (ist) {
2943 enc_ctx->time_base = ist->st->time_base;
2944 return;
2945 }
2946
2947 oc = output_files[ost->file_index]->ctx;
2948 av_log(oc, AV_LOG_WARNING, "Input stream data not available, using default time base\n");
2949 }
2950
2951 6017 enc_ctx->time_base = default_time_base;
2952 }
2953
2954 6053 static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
2955 {
2956 6053 InputStream *ist = get_input_stream(ost);
2957 6053 AVCodecContext *enc_ctx = ost->enc_ctx;
2958 6053 AVCodecContext *dec_ctx = NULL;
2959 6053 OutputFile *of = output_files[ost->file_index];
2960 6053 AVFormatContext *oc = of->ctx;
2961 int ret;
2962
2963 6053 set_encoder_id(output_files[ost->file_index], ost);
2964
2965
2/2
✓ Branch 0 taken 5966 times.
✓ Branch 1 taken 87 times.
6053 if (ist) {
2966 5966 dec_ctx = ist->dec_ctx;
2967 }
2968
2969
2/2
✓ Branch 0 taken 4870 times.
✓ Branch 1 taken 1183 times.
6053 if (enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
2970
2/2
✓ Branch 0 taken 4850 times.
✓ Branch 1 taken 20 times.
4870 if (!ost->frame_rate.num)
2971 4850 ost->frame_rate = av_buffersink_get_frame_rate(ost->filter->filter);
2972
5/6
✓ Branch 0 taken 4803 times.
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4801 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
4870 if (ist && !ost->frame_rate.num && !ost->max_frame_rate.num) {
2973 2 ost->frame_rate = (AVRational){25, 1};
2974 2 av_log(NULL, AV_LOG_WARNING,
2975 "No information "
2976 "about the input framerate is available. Falling "
2977 "back to a default value of 25fps for output stream #%d:%d. Use the -r option "
2978 "if you want a different framerate.\n",
2979 ost->file_index, ost->index);
2980 }
2981
2982
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4870 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4870 if (ost->max_frame_rate.num &&
2983 (av_q2d(ost->frame_rate) > av_q2d(ost->max_frame_rate) ||
2984 !ost->frame_rate.den))
2985 ost->frame_rate = ost->max_frame_rate;
2986
2987
3/4
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 4820 times.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
4870 if (ost->enc->supported_framerates && !ost->force_fps) {
2988 50 int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
2989 50 ost->frame_rate = ost->enc->supported_framerates[idx];
2990 }
2991 // reduce frame rate for mpeg4 to be within the spec limits
2992
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 4810 times.
4870 if (enc_ctx->codec_id == AV_CODEC_ID_MPEG4) {
2993 60 av_reduce(&ost->frame_rate.num, &ost->frame_rate.den,
2994 60 ost->frame_rate.num, ost->frame_rate.den, 65535);
2995 }
2996 }
2997
2998
3/5
✓ Branch 0 taken 1147 times.
✓ Branch 1 taken 4870 times.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6053 switch (enc_ctx->codec_type) {
2999 1147 case AVMEDIA_TYPE_AUDIO:
3000 1147 enc_ctx->sample_fmt = av_buffersink_get_format(ost->filter->filter);
3001 1147 enc_ctx->sample_rate = av_buffersink_get_sample_rate(ost->filter->filter);
3002 1147 ret = av_buffersink_get_ch_layout(ost->filter->filter, &enc_ctx->ch_layout);
3003
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1147 times.
1147 if (ret < 0)
3004 return ret;
3005
3006
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1147 times.
1147 if (ost->bits_per_raw_sample)
3007 enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
3008
4/4
✓ Branch 0 taken 1127 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 228 times.
✓ Branch 3 taken 899 times.
1147 else if (dec_ctx && ost->filter->graph->is_meta)
3009
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 228 times.
228 enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample,
3010 av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
3011
3012 1147 init_encoder_time_base(ost, av_make_q(1, enc_ctx->sample_rate));
3013 1147 break;
3014
3015 4870 case AVMEDIA_TYPE_VIDEO:
3016 4870 init_encoder_time_base(ost, av_inv_q(ost->frame_rate));
3017
3018
3/4
✓ Branch 0 taken 4869 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4869 times.
4870 if (!(enc_ctx->time_base.num && enc_ctx->time_base.den))
3019 1 enc_ctx->time_base = av_buffersink_get_time_base(ost->filter->filter);
3020
3/4
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 4859 times.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
4870 if ( av_q2d(enc_ctx->time_base) < 0.001 && ost->vsync_method != VSYNC_PASSTHROUGH
3021
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
11 && (ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR ||
3022
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 (ost->vsync_method == VSYNC_AUTO && !(of->format->flags & AVFMT_VARIABLE_FPS)))){
3023 7 av_log(oc, AV_LOG_WARNING, "Frame rate very high for a muxer not efficiently supporting it.\n"
3024 "Please consider specifying a lower framerate, a different muxer or "
3025 "setting vsync/fps_mode to vfr\n");
3026 }
3027
3028 4870 enc_ctx->width = av_buffersink_get_w(ost->filter->filter);
3029 4870 enc_ctx->height = av_buffersink_get_h(ost->filter->filter);
3030 9740 enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio =
3031 4870 ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
3032
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4870 times.
4870 av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) :
3033 4870 av_buffersink_get_sample_aspect_ratio(ost->filter->filter);
3034
3035 4870 enc_ctx->pix_fmt = av_buffersink_get_format(ost->filter->filter);
3036
3037
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4869 times.
4870 if (ost->bits_per_raw_sample)
3038 1 enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
3039
4/4
✓ Branch 0 taken 4802 times.
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 1428 times.
✓ Branch 3 taken 3374 times.
4869 else if (dec_ctx && ost->filter->graph->is_meta)
3040
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1428 times.
1428 enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample,
3041 av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
3042
3043
1/2
✓ Branch 0 taken 4870 times.
✗ Branch 1 not taken.
4870 if (frame) {
3044 4870 enc_ctx->color_range = frame->color_range;
3045 4870 enc_ctx->color_primaries = frame->color_primaries;
3046 4870 enc_ctx->color_trc = frame->color_trc;
3047 4870 enc_ctx->colorspace = frame->colorspace;
3048 4870 enc_ctx->chroma_sample_location = frame->chroma_location;
3049 }
3050
3051 4870 enc_ctx->framerate = ost->frame_rate;
3052
3053 4870 ost->st->avg_frame_rate = ost->frame_rate;
3054
3055 // Field order: autodetection
3056
1/2
✓ Branch 0 taken 4870 times.
✗ Branch 1 not taken.
4870 if (frame) {
3057
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4870 times.
4870 if (enc_ctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) &&
3058 ost->top_field_first >= 0)
3059 frame->top_field_first = !!ost->top_field_first;
3060
3061
2/2
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 4485 times.
4870 if (frame->interlaced_frame) {
3062
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 384 times.
385 if (enc_ctx->codec->id == AV_CODEC_ID_MJPEG)
3063
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 enc_ctx->field_order = frame->top_field_first ? AV_FIELD_TT:AV_FIELD_BB;
3064 else
3065
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 60 times.
384 enc_ctx->field_order = frame->top_field_first ? AV_FIELD_TB:AV_FIELD_BT;
3066 } else
3067 4485 enc_ctx->field_order = AV_FIELD_PROGRESSIVE;
3068 }
3069
3070 // Field order: override
3071
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4868 times.
4870 if (ost->top_field_first == 0) {
3072 2 enc_ctx->field_order = AV_FIELD_BB;
3073
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4867 times.
4868 } else if (ost->top_field_first == 1) {
3074 1 enc_ctx->field_order = AV_FIELD_TT;
3075 }
3076
3077
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4869 times.
4870 if (ost->forced_keyframes) {
3078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!strncmp(ost->forced_keyframes, "expr:", 5)) {
3079 ret = av_expr_parse(&ost->forced_keyframes_pexpr, ost->forced_keyframes+5,
3080 forced_keyframes_const_names, NULL, NULL, NULL, NULL, 0, NULL);
3081 if (ret < 0) {
3082 av_log(NULL, AV_LOG_ERROR,
3083 "Invalid force_key_frames expression '%s'\n", ost->forced_keyframes+5);
3084 return ret;
3085 }
3086 ost->forced_keyframes_expr_const_values[FKF_N] = 0;
3087 ost->forced_keyframes_expr_const_values[FKF_N_FORCED] = 0;
3088 ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] = NAN;
3089 ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] = NAN;
3090
3091 // Don't parse the 'forced_keyframes' in case of 'keep-source-keyframes',
3092 // parse it only for static kf timings
3093
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if(strncmp(ost->forced_keyframes, "source", 6)) {
3094 1 parse_forced_key_frames(ost->forced_keyframes, ost, ost->enc_ctx);
3095 }
3096 }
3097 4870 break;
3098 36 case AVMEDIA_TYPE_SUBTITLE:
3099 36 enc_ctx->time_base = AV_TIME_BASE_Q;
3100
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (!enc_ctx->width) {
3101 36 enc_ctx->width = input_streams[ost->source_index]->st->codecpar->width;
3102 36 enc_ctx->height = input_streams[ost->source_index]->st->codecpar->height;
3103 }
3104 36 break;
3105 case AVMEDIA_TYPE_DATA:
3106 break;
3107 default:
3108 abort();
3109 break;
3110 }
3111
3112 6053 ost->mux_timebase = enc_ctx->time_base;
3113
3114 6053 return 0;
3115 }
3116
3117 6479 static int init_output_stream(OutputStream *ost, AVFrame *frame,
3118 char *error, int error_len)
3119 {
3120 6479 int ret = 0;
3121
3122
2/2
✓ Branch 0 taken 6053 times.
✓ Branch 1 taken 426 times.
6479 if (ost->encoding_needed) {
3123 6053 const AVCodec *codec = ost->enc;
3124 6053 AVCodecContext *dec = NULL;
3125 InputStream *ist;
3126
3127 6053 ret = init_output_stream_encode(ost, frame);
3128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6053 times.
6053 if (ret < 0)
3129 return ret;
3130
3131
2/2
✓ Branch 1 taken 5966 times.
✓ Branch 2 taken 87 times.
6053 if ((ist = get_input_stream(ost)))
3132 5966 dec = ist->dec_ctx;
3133
4/4
✓ Branch 0 taken 5966 times.
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 34 times.
✓ Branch 3 taken 5932 times.
6053 if (dec && dec->subtitle_header) {
3134 /* ASS code assumes this buffer is null terminated so add extra byte. */
3135 34 ost->enc_ctx->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
3136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (!ost->enc_ctx->subtitle_header)
3137 return AVERROR(ENOMEM);
3138 34 memcpy(ost->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
3139 34 ost->enc_ctx->subtitle_header_size = dec->subtitle_header_size;
3140 }
3141
2/2
✓ Branch 1 taken 2530 times.
✓ Branch 2 taken 3523 times.
6053 if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
3142 2530 av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
3143
3144 6053 ret = hw_device_setup_for_encode(ost);
3145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6053 times.
6053 if (ret < 0) {
3146 snprintf(error, error_len, "Device setup failed for "
3147 "encoder on output stream #%d:%d : %s",
3148 ost->file_index, ost->index, av_err2str(ret));
3149 return ret;
3150 }
3151
3152
6/6
✓ Branch 0 taken 5966 times.
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 5928 times.
✓ Branch 4 taken 36 times.
✓ Branch 5 taken 2 times.
6053 if (ist && ist->dec->type == AVMEDIA_TYPE_SUBTITLE && ost->enc->type == AVMEDIA_TYPE_SUBTITLE) {
3153 36 int input_props = 0, output_props = 0;
3154 AVCodecDescriptor const *input_descriptor =
3155 36 avcodec_descriptor_get(dec->codec_id);
3156 AVCodecDescriptor const *output_descriptor =
3157 36 avcodec_descriptor_get(ost->enc_ctx->codec_id);
3158
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (input_descriptor)
3159 36 input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
3160
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (output_descriptor)
3161 36 output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
3162
3/6
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 36 times.
36 if (input_props && output_props && input_props != output_props) {
3163 snprintf(error, error_len,
3164 "Subtitle encoding currently only possible from text to text "
3165 "or bitmap to bitmap");
3166 return AVERROR_INVALIDDATA;
3167 }
3168 }
3169
3170
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6053 times.
6053 if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {
3171 if (ret == AVERROR_EXPERIMENTAL)
3172 abort_codec_experimental(codec, 1);
3173 snprintf(error, error_len,
3174 "Error while opening encoder for output stream #%d:%d - "
3175 "maybe incorrect parameters such as bit_rate, rate, width or height",
3176 ost->file_index, ost->index);
3177 return ret;
3178 }
3179
2/2
✓ Branch 0 taken 1147 times.
✓ Branch 1 taken 4906 times.
6053 if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
3180
2/2
✓ Branch 0 taken 106 times.
✓ Branch 1 taken 1041 times.
1147 !(ost->enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
3181 106 av_buffersink_set_frame_size(ost->filter->filter,
3182 106 ost->enc_ctx->frame_size);
3183 6053 assert_avoptions(ost->encoder_opts);
3184
3/4
✓ Branch 0 taken 6016 times.
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6016 times.
6053 if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
3185 ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
3186 av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
3187 " It takes bits/s as argument, not kbits/s\n");
3188
3189 6053 ret = avcodec_parameters_from_context(ost->st->codecpar, ost->enc_ctx);
3190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6053 times.
6053 if (ret < 0) {
3191 av_log(NULL, AV_LOG_FATAL,
3192 "Error initializing the output stream codec context.\n");
3193 exit_program(1);
3194 }
3195
3196
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 5866 times.
6053 if (ost->enc_ctx->nb_coded_side_data) {
3197 int i;
3198
3199
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 187 times.
374 for (i = 0; i < ost->enc_ctx->nb_coded_side_data; i++) {
3200 187 const AVPacketSideData *sd_src = &ost->enc_ctx->coded_side_data[i];
3201 uint8_t *dst_data;
3202
3203 187 dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
3204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 187 times.
187 if (!dst_data)
3205 return AVERROR(ENOMEM);
3206 187 memcpy(dst_data, sd_src->data, sd_src->size);
3207 }
3208 }
3209
3210 /*
3211 * Add global input side data. For now this is naive, and copies it
3212 * from the input stream's global side data. All side data should
3213 * really be funneled over AVFrame and libavfilter, then added back to
3214 * packet side data, and then potentially using the first packet for
3215 * global side data.
3216 */
3217
2/2
✓ Branch 0 taken 5966 times.
✓ Branch 1 taken 87 times.
6053 if (ist) {
3218 int i;
3219
2/2
✓ Branch 0 taken 266 times.
✓ Branch 1 taken 5966 times.
6232 for (i = 0; i < ist->st->nb_side_data; i++) {
3220 266 AVPacketSideData *sd = &ist->st->side_data[i];
3221
2/2
✓ Branch 0 taken 214 times.
✓ Branch 1 taken 52 times.
266 if (sd->type != AV_PKT_DATA_CPB_PROPERTIES) {
3222 214 uint8_t *dst = av_stream_new_side_data(ost->st, sd->type, sd->size);
3223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 214 times.
214 if (!dst)
3224 return AVERROR(ENOMEM);
3225 214 memcpy(dst, sd->data, sd->size);
3226
3/4
✓ Branch 0 taken 214 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 213 times.
214 if (ist->autorotate && sd->type == AV_PKT_DATA_DISPLAYMATRIX)
3227 1 av_display_rotation_set((uint32_t *)dst, 0);
3228 }
3229 }
3230 }
3231
3232 // copy timebase while removing common factors
3233
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6051 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
6053 if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0)
3234 6051 ost->st->time_base = av_add_q(ost->enc_ctx->time_base, (AVRational){0, 1});
3235
3236 // copy estimated duration as a hint to the muxer
3237
5/6
✓ Branch 0 taken 6053 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5966 times.
✓ Branch 3 taken 87 times.
✓ Branch 4 taken 4814 times.
✓ Branch 5 taken 1152 times.
6053 if (ost->st->duration <= 0 && ist && ist->st->duration > 0)
3238 4814 ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
3239
2/2
✓ Branch 0 taken 425 times.
✓ Branch 1 taken 1 times.
426 } else if (ost->stream_copy) {
3240 425 ret = init_output_stream_streamcopy(ost);
3241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 425 times.
425 if (ret < 0)
3242 return ret;
3243 }
3244
3245 /* initialize bitstream filters for the output stream
3246 * needs to be done here, because the codec id for streamcopy is not
3247 * known until now */
3248 6479 ret = init_output_bsfs(ost);
3249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6479 times.
6479 if (ret < 0)
3250 return ret;
3251
3252 6479 ost->initialized = 1;
3253
3254 6479 ret = of_check_init(output_files[ost->file_index]);
3255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6479 times.
6479 if (ret < 0)
3256 return ret;
3257
3258 6479 return ret;
3259 }
3260
3261 static void report_new_stream(int input_index, AVPacket *pkt)
3262 {
3263 InputFile *file = input_files[input_index];
3264 AVStream *st = file->ctx->streams[pkt->stream_index];
3265
3266 if (pkt->stream_index < file->nb_streams_warn)
3267 return;
3268 av_log(file->ctx, AV_LOG_WARNING,
3269 "New %s stream %d:%d at pos:%"PRId64" and DTS:%ss\n",
3270 av_get_media_type_string(st->codecpar->codec_type),
3271 input_index, pkt->stream_index,
3272 pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
3273 file->nb_streams_warn = pkt->stream_index + 1;
3274 }
3275
3276 6248 static int transcode_init(void)
3277 {
3278