FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg.c
Date: 2022-01-28 07:56:06
Exec Total Coverage
Lines: 2030 2756 73.7%
Branches: 1492 2177 68.5%

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