FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/ffmpeg.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 2113 2831 74.6%
Branches: 1570 2261 69.4%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2003 Fabrice Bellard
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * multimedia converter based on the FFmpeg libraries
24 */
25
26 #include "config.h"
27 #include <ctype.h>
28 #include <string.h>
29 #include <math.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdatomic.h>
34 #include <stdint.h>
35
36 #if HAVE_IO_H
37 #include <io.h>
38 #endif
39 #if HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42
43 #include "libavformat/avformat.h"
44 #include "libavdevice/avdevice.h"
45 #include "libswresample/swresample.h"
46 #include "libavutil/opt.h"
47 #include "libavutil/channel_layout.h"
48 #include "libavutil/parseutils.h"
49 #include "libavutil/samplefmt.h"
50 #include "libavutil/fifo.h"
51 #include "libavutil/hwcontext.h"
52 #include "libavutil/internal.h"
53 #include "libavutil/intreadwrite.h"
54 #include "libavutil/dict.h"
55 #include "libavutil/display.h"
56 #include "libavutil/mathematics.h"
57 #include "libavutil/pixdesc.h"
58 #include "libavutil/avstring.h"
59 #include "libavutil/libm.h"
60 #include "libavutil/imgutils.h"
61 #include "libavutil/timestamp.h"
62 #include "libavutil/bprint.h"
63 #include "libavutil/time.h"
64 #include "libavutil/thread.h"
65 #include "libavutil/threadmessage.h"
66 #include "libavcodec/mathops.h"
67 #include "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 213 void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub)
242 {
243 213 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 33 times.
✓ Branch 1 taken 180 times.
213 if (!frame)
250 33 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 394315 static void sub2video_heartbeat(InputStream *ist, int64_t pts)
282 {
283 394315 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 437344 times.
✓ Branch 1 taken 394315 times.
831659 for (i = 0; i < infile->nb_streams; i++) {
292 437344 InputStream *ist2 = input_streams[infile->ist_index + i];
293
2/2
✓ Branch 0 taken 436399 times.
✓ Branch 1 taken 945 times.
437344 if (!ist2->sub2video.frame)
294 436399 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 394315 }
312
313 37 static void sub2video_flush(InputStream *ist)
314 {
315 int i;
316 int ret;
317
318
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 if (ist->sub2video.end_pts < INT64_MAX)
319 37 sub2video_update(ist, INT64_MAX, NULL);
320
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 37 times.
41 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 37 }
326
327 /* end of sub2video hack */
328
329 12032 static void term_exit_sigsafe(void)
330 {
331 #if HAVE_TERMIOS_H
332
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12028 times.
12032 if(restore_tty)
333 4 tcsetattr (0, TCSANOW, &oldtty);
334 #endif
335 12032 }
336
337 12032 void term_exit(void)
338 {
339 12032 av_log(NULL, AV_LOG_QUIET, "%s", "");
340 12032 term_exit_sigsafe();
341 12032 }
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 6017 void term_init(void)
409 {
410 #if defined __linux__
411 6017 struct sigaction action = {0};
412 6017 action.sa_handler = sigterm_handler;
413
414 /* block other interrupts while processing this one */
415 6017 sigfillset(&action.sa_mask);
416
417 /* restart interruptible functions (i.e. don't fail with EINTR) */
418 6017 action.sa_flags = SA_RESTART;
419 #endif
420
421 #if HAVE_TERMIOS_H
422
3/4
✓ Branch 0 taken 6017 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 5993 times.
6017 if (!run_as_daemon && stdin_interaction) {
423 struct termios tty;
424
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 22 times.
24 if (tcgetattr (0, &tty) == 0) {
425 2 oldtty = tty;
426 2 restore_tty = 1;
427
428 2 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
429 |INLCR|IGNCR|ICRNL|IXON);
430 2 tty.c_oflag |= OPOST;
431 2 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
432 2 tty.c_cflag &= ~(CSIZE|PARENB);
433 2 tty.c_cflag |= CS8;
434 2 tty.c_cc[VMIN] = 1;
435 2 tty.c_cc[VTIME] = 0;
436
437 2 tcsetattr (0, TCSANOW, &tty);
438 }
439 24 SIGNAL(SIGQUIT, sigterm_handler); /* Quit (POSIX). */
440 }
441 #endif
442
443 6017 SIGNAL(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
444 6017 SIGNAL(SIGTERM, sigterm_handler); /* Termination (ANSI). */
445 #ifdef SIGXCPU
446 6017 SIGNAL(SIGXCPU, sigterm_handler);
447 #endif
448 #ifdef SIGPIPE
449 6017 signal(SIGPIPE, SIG_IGN); /* Broken pipe (POSIX). */
450 #endif
451 #if HAVE_SETCONSOLECTRLHANDLER
452 SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE);
453 #endif
454 6017 }
455
456 /* read a key without blocking */
457 174 static int read_key(void)
458 {
459 unsigned char ch;
460 #if HAVE_TERMIOS_H
461 174 int n = 1;
462 struct timeval tv;
463 fd_set rfds;
464
465
2/2
✓ Branch 0 taken 2784 times.
✓ Branch 1 taken 174 times.
2958 FD_ZERO(&rfds);
466 174 FD_SET(0, &rfds);
467 174 tv.tv_sec = 0;
468 174 tv.tv_usec = 0;
469 174 n = select(1, &rfds, NULL, NULL, &tv);
470
2/2
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 13 times.
174 if (n > 0) {
471 161 n = read(0, &ch, 1);
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 161 times.
161 if (n == 1)
473 return ch;
474
475 161 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 13 return -1;
506 }
507
508 596707 static int decode_interrupt_cb(void *ctx)
509 {
510 596707 return received_nb_signals > atomic_load(&transcode_init_done);
511 }
512
513 const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
514
515 6018 static void ffmpeg_cleanup(int ret)
516 {
517 int i, j;
518
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6018 times.
6018 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 5819 times.
✓ Branch 1 taken 6018 times.
11837 for (i = 0; i < nb_filtergraphs; i++) {
525 5819 FilterGraph *fg = filtergraphs[i];
526 5819 avfilter_graph_free(&fg->graph);
527
2/2
✓ Branch 0 taken 5799 times.
✓ Branch 1 taken 5819 times.
11618 for (j = 0; j < fg->nb_inputs; j++) {
528 5799 InputFilter *ifilter = fg->inputs[j];
529 5799 struct InputStream *ist = ifilter->ist;
530
531
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5799 times.
5799 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 5799 av_fifo_freep(&ifilter->frame_queue);
538 5799 av_freep(&ifilter->displaymatrix);
539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5799 times.
5799 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 5799 av_buffer_unref(&ifilter->hw_frames_ctx);
549 5799 av_freep(&ifilter->name);
550 5799 av_freep(&fg->inputs[j]);
551 }
552 5819 av_freep(&fg->inputs);
553
2/2
✓ Branch 0 taken 5822 times.
✓ Branch 1 taken 5819 times.
11641 for (j = 0; j < fg->nb_outputs; j++) {
554 5822 OutputFilter *ofilter = fg->outputs[j];
555
556 5822 avfilter_inout_free(&ofilter->out_tmp);
557 5822 av_freep(&ofilter->name);
558 5822 av_freep(&ofilter->formats);
559 5822 av_freep(&ofilter->channel_layouts);
560 5822 av_freep(&ofilter->sample_rates);
561 5822 av_freep(&fg->outputs[j]);
562 }
563 5819 av_freep(&fg->outputs);
564 5819 av_freep(&fg->graph_desc);
565
566 5819 av_freep(&filtergraphs[i]);
567 }
568 6018 av_freep(&filtergraphs);
569
570 6018 av_freep(&subtitle_out);
571
572 /* close files */
573
2/2
✓ Branch 0 taken 6017 times.
✓ Branch 1 taken 6018 times.
12035 for (i = 0; i < nb_output_files; i++) {
574 6017 OutputFile *of = output_files[i];
575 AVFormatContext *s;
576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6017 times.
6017 if (!of)
577 continue;
578 6017 s = of->ctx;
579
4/6
✓ Branch 0 taken 6017 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6017 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5963 times.
✓ Branch 5 taken 54 times.
6017 if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE))
580 5963 avio_closep(&s->pb);
581 6017 avformat_free_context(s);
582 6017 av_dict_free(&of->opts);
583
584 6017 av_freep(&output_files[i]);
585 }
586
2/2
✓ Branch 0 taken 6197 times.
✓ Branch 1 taken 6018 times.
12215 for (i = 0; i < nb_output_streams; i++) {
587 6197 OutputStream *ost = output_streams[i];
588
589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6197 times.
6197 if (!ost)
590 continue;
591
592 6197 av_bsf_free(&ost->bsf_ctx);
593
594 6197 av_frame_free(&ost->filtered_frame);
595 6197 av_frame_free(&ost->last_frame);
596 6197 av_packet_free(&ost->pkt);
597 6197 av_dict_free(&ost->encoder_opts);
598
599 6197 av_freep(&ost->forced_keyframes);
600 6197 av_expr_free(ost->forced_keyframes_pexpr);
601 6197 av_freep(&ost->avfilter);
602 6197 av_freep(&ost->logfile_prefix);
603
604 6197 av_freep(&ost->audio_channels_map);
605 6197 ost->audio_channels_mapped = 0;
606
607 6197 av_dict_free(&ost->sws_dict);
608 6197 av_dict_free(&ost->swr_opts);
609
610 6197 avcodec_free_context(&ost->enc_ctx);
611 6197 avcodec_parameters_free(&ost->ref_par);
612
613
1/2
✓ Branch 0 taken 6197 times.
✗ Branch 1 not taken.
6197 if (ost->muxing_queue) {
614
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6197 times.
6197 while (av_fifo_size(ost->muxing_queue)) {
615 AVPacket *pkt;
616 av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL);
617 av_packet_free(&pkt);
618 }
619 6197 av_fifo_freep(&ost->muxing_queue);
620 }
621
622 6197 av_freep(&output_streams[i]);
623 }
624 #if HAVE_THREADS
625 6018 free_input_threads();
626 #endif
627
2/2
✓ Branch 0 taken 6044 times.
✓ Branch 1 taken 6018 times.
12062 for (i = 0; i < nb_input_files; i++) {
628 6044 avformat_close_input(&input_files[i]->ctx);
629 6044 av_packet_free(&input_files[i]->pkt);
630 6044 av_freep(&input_files[i]);
631 }
632
2/2
✓ Branch 0 taken 6408 times.
✓ Branch 1 taken 6018 times.
12426 for (i = 0; i < nb_input_streams; i++) {
633 6408 InputStream *ist = input_streams[i];
634
635 6408 av_frame_free(&ist->decoded_frame);
636 6408 av_frame_free(&ist->filter_frame);
637 6408 av_packet_free(&ist->pkt);
638 6408 av_dict_free(&ist->decoder_opts);
639 6408 avsubtitle_free(&ist->prev_sub.subtitle);
640 6408 av_frame_free(&ist->sub2video.frame);
641 6408 av_freep(&ist->filters);
642 6408 av_freep(&ist->hwaccel_device);
643 6408 av_freep(&ist->dts_buffer);
644
645 6408 avcodec_free_context(&ist->dec_ctx);
646
647 6408 av_freep(&input_streams[i]);
648 }
649
650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6018 times.
6018 if (vstats_file) {
651 if (fclose(vstats_file))
652 av_log(NULL, AV_LOG_ERROR,
653 "Error closing vstats file, loss of information possible: %s\n",
654 av_err2str(AVERROR(errno)));
655 }
656 6018 av_freep(&vstats_filename);
657 6018 av_freep(&filter_nbthreads);
658
659 6018 av_freep(&input_streams);
660 6018 av_freep(&input_files);
661 6018 av_freep(&output_streams);
662 6018 av_freep(&output_files);
663
664 6018 uninit_opts();
665
666 6018 avformat_network_deinit();
667
668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6018 times.
6018 if (received_sigterm) {
669 av_log(NULL, AV_LOG_INFO, "Exiting normally, received signal %d.\n",
670 (int) received_sigterm);
671
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6015 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
6018 } else if (ret && atomic_load(&transcode_init_done)) {
672 2 av_log(NULL, AV_LOG_INFO, "Conversion failed!\n");
673 }
674 6018 term_exit();
675 6018 ffmpeg_exited = 1;
676 6018 }
677
678 6044 void remove_avoptions(AVDictionary **a, AVDictionary *b)
679 {
680 6044 AVDictionaryEntry *t = NULL;
681
682
2/2
✓ Branch 1 taken 19111 times.
✓ Branch 2 taken 6044 times.
25155 while ((t = av_dict_get(b, "", t, AV_DICT_IGNORE_SUFFIX))) {
683 19111 av_dict_set(a, t->key, NULL, AV_DICT_MATCH_CASE);
684 }
685 6044 }
686
687 17726 void assert_avoptions(AVDictionary *m)
688 {
689 AVDictionaryEntry *t;
690
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17726 times.
17726 if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
691 av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
692 exit_program(1);
693 }
694 17726 }
695
696 static void abort_codec_experimental(const AVCodec *c, int encoder)
697 {
698 exit_program(1);
699 }
700
701 2392197 static void update_benchmark(const char *fmt, ...)
702 {
703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2392197 times.
2392197 if (do_benchmark_all) {
704 BenchmarkTimeStamps t = get_benchmark_time_stamps();
705 va_list va;
706 char buf[1024];
707
708 if (fmt) {
709 va_start(va, fmt);
710 vsnprintf(buf, sizeof(buf), fmt, va);
711 va_end(va);
712 av_log(NULL, AV_LOG_INFO,
713 "bench: %8" PRIu64 " user %8" PRIu64 " sys %8" PRIu64 " real %s \n",
714 t.user_usec - current_time.user_usec,
715 t.sys_usec - current_time.sys_usec,
716 t.real_usec - current_time.real_usec, buf);
717 }
718 current_time = t;
719 }
720 2392197 }
721
722 static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others)
723 {
724 int i;
725 for (i = 0; i < nb_output_streams; i++) {
726 OutputStream *ost2 = output_streams[i];
727 ost2->finished |= ost == ost2 ? this_stream : others;
728 }
729 }
730
731 429078 static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
732 {
733 429078 AVFormatContext *s = of->ctx;
734 429078 AVStream *st = ost->st;
735 int ret;
736
737 /*
738 * Audio encoders may split the packets -- #frames in != #packets out.
739 * But there is no reordering, so we can limit the number of output packets
740 * by simply dropping them here.
741 * Counting encoded video frames needs to be done separately because of
742 * reordering, see do_video_out().
743 * Do not count the packet when unqueued because it has been counted when queued.
744 */
745
6/6
✓ Branch 0 taken 109570 times.
✓ Branch 1 taken 319508 times.
✓ Branch 2 taken 9856 times.
✓ Branch 3 taken 99714 times.
✓ Branch 4 taken 329136 times.
✓ Branch 5 taken 228 times.
429078 if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed) && !unqueue) {
746
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 329130 times.
329136 if (ost->frame_number >= ost->max_frames) {
747 6 av_packet_unref(pkt);
748 6 return;
749 }
750 329130 ost->frame_number++;
751 }
752
753
2/2
✓ Branch 0 taken 309 times.
✓ Branch 1 taken 428763 times.
429072 if (!of->header_written) {
754 AVPacket *tmp_pkt;
755 /* the muxer is not initialized yet, buffer the packet */
756
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 309 times.
309 if (!av_fifo_space(ost->muxing_queue)) {
757 unsigned int are_we_over_size =
758 (ost->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold;
759 int new_size = are_we_over_size ?
760 FFMIN(2 * av_fifo_size(ost->muxing_queue),
761 ost->max_muxing_queue_size) :
762 2 * av_fifo_size(ost->muxing_queue);
763
764 if (new_size <= av_fifo_size(ost->muxing_queue)) {
765 av_log(NULL, AV_LOG_ERROR,
766 "Too many packets buffered for output stream %d:%d.\n",
767 ost->file_index, ost->st->index);
768 exit_program(1);
769 }
770 ret = av_fifo_realloc2(ost->muxing_queue, new_size);
771 if (ret < 0)
772 exit_program(1);
773 }
774 309 ret = av_packet_make_refcounted(pkt);
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 309 times.
309 if (ret < 0)
776 exit_program(1);
777 309 tmp_pkt = av_packet_alloc();
778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 309 times.
309 if (!tmp_pkt)
779 exit_program(1);
780 309 av_packet_move_ref(tmp_pkt, pkt);
781 309 ost->muxing_queue_data_size += tmp_pkt->size;
782 309 av_fifo_generic_write(ost->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL);
783 309 return;
784 }
785
786
3/4
✓ Branch 0 taken 109484 times.
✓ Branch 1 taken 319279 times.
✓ Branch 2 taken 109484 times.
✗ Branch 3 not taken.
428763 if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method == VSYNC_DROP) ||
787
3/4
✓ Branch 0 taken 317896 times.
✓ Branch 1 taken 110867 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 317896 times.
428763 (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0))
788 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
789
790
2/2
✓ Branch 0 taken 109484 times.
✓ Branch 1 taken 319279 times.
428763 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
791 int i;
792 109484 uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
793 NULL);
794
2/2
✓ Branch 0 taken 9333 times.
✓ Branch 1 taken 100151 times.
109484 ost->quality = sd ? AV_RL32(sd) : -1;
795
2/2
✓ Branch 0 taken 9333 times.
✓ Branch 1 taken 100151 times.
109484 ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
796
797
2/2
✓ Branch 0 taken 437936 times.
✓ Branch 1 taken 109484 times.
547420 for (i = 0; i<FF_ARRAY_ELEMS(ost->error); i++) {
798
3/4
✓ Branch 0 taken 37332 times.
✓ Branch 1 taken 400604 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 37332 times.
437936 if (sd && i < sd[5])
799 ost->error[i] = AV_RL64(sd + 8 + 8*i);
800 else
801 437936 ost->error[i] = -1;
802 }
803
804
4/4
✓ Branch 0 taken 99713 times.
✓ Branch 1 taken 9771 times.
✓ Branch 2 taken 9821 times.
✓ Branch 3 taken 89892 times.
109484 if (ost->frame_rate.num && ost->is_cfr) {
805
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9821 times.
9821 if (pkt->duration > 0)
806 av_log(NULL, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n");
807 9821 pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate),
808 ost->mux_timebase);
809 }
810 }
811
812 428763 av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base);
813
814
2/2
✓ Branch 0 taken 187306 times.
✓ Branch 1 taken 241457 times.
428763 if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
815
1/2
✓ Branch 0 taken 187306 times.
✗ Branch 1 not taken.
187306 if (pkt->dts != AV_NOPTS_VALUE &&
816
2/2
✓ Branch 0 taken 186327 times.
✓ Branch 1 taken 979 times.
187306 pkt->pts != AV_NOPTS_VALUE &&
817
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 186327 times.
186327 pkt->dts > pkt->pts) {
818 av_log(s, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64" in output stream %d:%d, replacing by guess\n",
819 pkt->dts, pkt->pts,
820 ost->file_index, ost->st->index);
821 pkt->pts =
822 pkt->dts = pkt->pts + pkt->dts + ost->last_mux_dts + 1
823 - FFMIN3(pkt->pts, pkt->dts, ost->last_mux_dts + 1)
824 - FFMAX3(pkt->pts, pkt->dts, ost->last_mux_dts + 1);
825 }
826
6/6
✓ Branch 0 taken 80851 times.
✓ Branch 1 taken 106455 times.
✓ Branch 2 taken 888 times.
✓ Branch 3 taken 79963 times.
✓ Branch 4 taken 873 times.
✓ Branch 5 taken 15 times.
187306 if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) &&
827
1/2
✓ Branch 0 taken 187291 times.
✗ Branch 1 not taken.
187291 pkt->dts != AV_NOPTS_VALUE &&
828
3/4
✓ Branch 0 taken 233 times.
✓ Branch 1 taken 187058 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 233 times.
187291 !(st->codecpar->codec_id == AV_CODEC_ID_VP9 && ost->stream_copy) &&
829
2/2
✓ Branch 0 taken 182166 times.
✓ Branch 1 taken 4892 times.
187058 ost->last_mux_dts != AV_NOPTS_VALUE) {
830 182166 int64_t max = ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
831
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 182148 times.
182166 if (pkt->dts < max) {
832
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;
833
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (exit_on_error)
834 loglevel = AV_LOG_ERROR;
835 18 av_log(s, loglevel, "Non-monotonous DTS in output stream "
836 "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
837 18 ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts);
838
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (exit_on_error) {
839 av_log(NULL, AV_LOG_FATAL, "aborting.\n");
840 exit_program(1);
841 }
842 18 av_log(s, loglevel, "changing to %"PRId64". This may result "
843 "in incorrect timestamps in the output file.\n",
844 max);
845
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (pkt->pts >= pkt->dts)
846 18 pkt->pts = FFMAX(pkt->pts, max);
847 18 pkt->dts = max;
848 }
849 }
850 }
851 428763 ost->last_mux_dts = pkt->dts;
852
853 428763 ost->data_size += pkt->size;
854 428763 ost->packets_written++;
855
856 428763 pkt->stream_index = ost->index;
857
858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 428763 times.
428763 if (debug_ts) {
859 av_log(NULL, AV_LOG_INFO, "muxer <- type:%s "
860 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n",
861 av_get_media_type_string(ost->enc_ctx->codec_type),
862 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
863 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
864 pkt->size
865 );
866 }
867
868 428763 ret = av_interleaved_write_frame(s, pkt);
869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 428763 times.
428763 if (ret < 0) {
870 print_error("av_interleaved_write_frame()", ret);
871 main_return_code = 1;
872 close_all_output_streams(ost, MUXER_FINISHED | ENCODER_FINISHED, ENCODER_FINISHED);
873 }
874 428763 av_packet_unref(pkt);
875 }
876
877 5842 static void close_output_stream(OutputStream *ost)
878 {
879 5842 OutputFile *of = output_files[ost->file_index];
880
881 5842 ost->finished |= ENCODER_FINISHED;
882
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5842 times.
5842 if (of->shortest) {
883 int64_t end = av_rescale_q(ost->sync_opts - ost->first_pts, ost->enc_ctx->time_base, AV_TIME_BASE_Q);
884 of->recording_time = FFMIN(of->recording_time, end);
885 }
886 5842 }
887
888 /*
889 * Send a single packet to the output, applying any bitstream filters
890 * associated with the output stream. This may result in any number
891 * of packets actually being written, depending on what bitstream
892 * filters are applied. The supplied packet is consumed and will be
893 * blank (as if newly-allocated) when this function returns.
894 *
895 * If eof is set, instead indicate EOF to all bitstream filters and
896 * therefore flush any delayed packets to the output. A blank packet
897 * must be supplied in this case.
898 */
899 434597 static void output_packet(OutputFile *of, AVPacket *pkt,
900 OutputStream *ost, int eof)
901 {
902 434597 int ret = 0;
903
904 /* apply the output bitstream filters */
905
2/2
✓ Branch 0 taken 6820 times.
✓ Branch 1 taken 427777 times.
434597 if (ost->bsf_ctx) {
906
2/2
✓ Branch 0 taken 6816 times.
✓ Branch 1 taken 4 times.
6820 ret = av_bsf_send_packet(ost->bsf_ctx, eof ? NULL : pkt);
907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6820 times.
6820 if (ret < 0)
908 goto finish;
909
2/2
✓ Branch 1 taken 6813 times.
✓ Branch 2 taken 6820 times.
13633 while ((ret = av_bsf_receive_packet(ost->bsf_ctx, pkt)) >= 0)
910 6813 write_packet(of, pkt, ost, 0);
911
2/2
✓ Branch 0 taken 6814 times.
✓ Branch 1 taken 6 times.
6820 if (ret == AVERROR(EAGAIN))
912 6814 ret = 0;
913
2/2
✓ Branch 0 taken 5821 times.
✓ Branch 1 taken 421956 times.
427777 } else if (!eof)
914 421956 write_packet(of, pkt, ost, 0);
915
916 5821 finish:
917
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 434591 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
434597 if (ret < 0 && ret != AVERROR_EOF) {
918 2 av_log(NULL, AV_LOG_ERROR, "Error applying bitstream filters to an output "
919 "packet for stream #%d:%d.\n", ost->file_index, ost->index);
920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if(exit_on_error)
921 exit_program(1);
922 }
923 434597 }
924
925 401462 static int check_recording_time(OutputStream *ost)
926 {
927 401462 OutputFile *of = output_files[ost->file_index];
928
929
4/4
✓ Branch 0 taken 7475 times.
✓ Branch 1 taken 393987 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 7474 times.
408937 if (of->recording_time != INT64_MAX &&
930 7475 av_compare_ts(ost->sync_opts - ost->first_pts, ost->enc_ctx->time_base, of->recording_time,
931 7475 AV_TIME_BASE_Q) >= 0) {
932 1 close_output_stream(ost);
933 1 return 0;
934 }
935 401461 return 1;
936 }
937
938 403116 static double adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost,
939 AVFrame *frame)
940 {
941 403116 double float_pts = AV_NOPTS_VALUE; // this is identical to frame.pts but with higher precision
942 403116 AVCodecContext *enc = ost->enc_ctx;
943
4/6
✓ Branch 0 taken 400665 times.
✓ Branch 1 taken 2451 times.
✓ Branch 2 taken 400665 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 400665 times.
✗ Branch 5 not taken.
403116 if (!frame || frame->pts == AV_NOPTS_VALUE ||
944
2/4
✓ Branch 0 taken 400665 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 400665 times.
400665 !enc || !ost->filter || !ost->filter->graph->graph)
945 2451 goto early_exit;
946
947 {
948 400665 AVFilterContext *filter = ost->filter->filter;
949
950
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 400530 times.
400665 int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
951 400665 AVRational filter_tb = av_buffersink_get_time_base(filter);
952 400665 AVRational tb = enc->time_base;
953 400665 int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
954
955 400665 tb.den <<= extra_bits;
956 400665 float_pts =
957 400665 av_rescale_q(frame->pts, filter_tb, tb) -
958 400665 av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
959 400665 float_pts /= 1 << extra_bits;
960 // 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
961
2/2
✓ Branch 0 taken 394731 times.
✓ Branch 1 taken 5934 times.
400665 float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
962
963 400665 frame->pts =
964 400665 av_rescale_q(frame->pts, filter_tb, enc->time_base) -
965 400665 av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
966 }
967
968 403116 early_exit:
969
970
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403116 times.
403116 if (debug_ts) {
971 av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n",
972 frame ? av_ts2str(frame->pts) : "NULL",
973 frame ? av_ts2timestr(frame->pts, &enc->time_base) : "NULL",
974 float_pts,
975 enc ? enc->time_base.num : -1,
976 enc ? enc->time_base.den : -1);
977 }
978
979 403116 return float_pts;
980 }
981
982 static int init_output_stream(OutputStream *ost, AVFrame *frame,
983 char *error, int error_len);
984
985 620150 static int init_output_stream_wrapper(OutputStream *ost, AVFrame *frame,
986 unsigned int fatal)
987 {
988 620150 int ret = AVERROR_BUG;
989 620150 char error[1024] = {0};
990
991
2/2
✓ Branch 0 taken 613955 times.
✓ Branch 1 taken 6195 times.
620150 if (ost->initialized)
992 613955 return 0;
993
994 6195 ret = init_output_stream(ost, frame, error, sizeof(error));
995
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6195 times.
6195 if (ret < 0) {
996 av_log(NULL, AV_LOG_ERROR, "Error initializing output stream %d:%d -- %s\n",
997 ost->file_index, ost->index, error);
998
999 if (fatal)
1000 exit_program(1);
1001 }
1002
1003 6195 return ret;
1004 }
1005
1006 301072 static void do_audio_out(OutputFile *of, OutputStream *ost,
1007 AVFrame *frame)
1008 {
1009 301072 AVCodecContext *enc = ost->enc_ctx;
1010 301072 AVPacket *pkt = ost->pkt;
1011 int ret;
1012
1013 301072 adjust_frame_pts_to_encoder_tb(of, ost, frame);
1014
1015
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 301072 times.
301072 if (!check_recording_time(ost))
1016 return;
1017
1018
2/4
✓ Branch 0 taken 301072 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 301072 times.
301072 if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
1019 frame->pts = ost->sync_opts;
1020 301072 ost->sync_opts = frame->pts + frame->nb_samples;
1021 301072 ost->samples_encoded += frame->nb_samples;
1022 301072 ost->frames_encoded++;
1023
1024 301072 update_benchmark(NULL);
1025
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 301072 times.
301072 if (debug_ts) {
1026 av_log(NULL, AV_LOG_INFO, "encoder <- type:audio "
1027 "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
1028 av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
1029 enc->time_base.num, enc->time_base.den);
1030 }
1031
1032 301072 ret = avcodec_send_frame(enc, frame);
1033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 301072 times.
301072 if (ret < 0)
1034 goto error;
1035
1036 while (1) {
1037 595988 av_packet_unref(pkt);
1038 595988 ret = avcodec_receive_packet(enc, pkt);
1039
2/2
✓ Branch 0 taken 301072 times.
✓ Branch 1 taken 294916 times.
595988 if (ret == AVERROR(EAGAIN))
1040 301072 break;
1041
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 294916 times.
294916 if (ret < 0)
1042 goto error;
1043
1044 294916 update_benchmark("encode_audio %d.%d", ost->file_index, ost->index);
1045
1046 294916 av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
1047
1048
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 294916 times.
294916 if (debug_ts) {
1049 av_log(NULL, AV_LOG_INFO, "encoder -> type:audio "
1050 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1051 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
1052 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base));
1053 }
1054
1055 294916 output_packet(of, pkt, ost, 0);
1056 }
1057
1058 301072 return;
1059 error:
1060 av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
1061 exit_program(1);
1062 }
1063
1064 720 static void do_subtitle_out(OutputFile *of,
1065 OutputStream *ost,
1066 AVSubtitle *sub)
1067 {
1068 720 int subtitle_out_max_size = 1024 * 1024;
1069 int subtitle_out_size, nb, i;
1070 AVCodecContext *enc;
1071 720 AVPacket *pkt = ost->pkt;
1072 int64_t pts;
1073
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 720 times.
720 if (sub->pts == AV_NOPTS_VALUE) {
1075 av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
1076 if (exit_on_error)
1077 exit_program(1);
1078 return;
1079 }
1080
1081 720 enc = ost->enc_ctx;
1082
1083
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 686 times.
720 if (!subtitle_out) {
1084 34 subtitle_out = av_malloc(subtitle_out_max_size);
1085
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (!subtitle_out) {
1086 av_log(NULL, AV_LOG_FATAL, "Failed to allocate subtitle_out\n");
1087 exit_program(1);
1088 }
1089 }
1090
1091 /* Note: DVB subtitle need one packet to draw them and one other
1092 packet to clear them */
1093 /* XXX: signal it in the codec context ? */
1094
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 684 times.
720 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
1095 36 nb = 2;
1096 else
1097 684 nb = 1;
1098
1099 /* shift timestamp to honor -ss and make check_recording_time() work with -t */
1100 720 pts = sub->pts;
1101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 720 times.
720 if (output_files[ost->file_index]->start_time != AV_NOPTS_VALUE)
1102 pts -= output_files[ost->file_index]->start_time;
1103
2/2
✓ Branch 0 taken 756 times.
✓ Branch 1 taken 720 times.
1476 for (i = 0; i < nb; i++) {
1104 756 unsigned save_num_rects = sub->num_rects;
1105
1106 756 ost->sync_opts = av_rescale_q(pts, AV_TIME_BASE_Q, enc->time_base);
1107
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 756 times.
756 if (!check_recording_time(ost))
1108 return;
1109
1110 756 sub->pts = pts;
1111 // start_display_time is required to be 0
1112 756 sub->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
1113 756 sub->end_display_time -= sub->start_display_time;
1114 756 sub->start_display_time = 0;
1115
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 720 times.
756 if (i == 1)
1116 36 sub->num_rects = 0;
1117
1118 756 ost->frames_encoded++;
1119
1120 756 subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
1121 subtitle_out_max_size, sub);
1122
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 720 times.
756 if (i == 1)
1123 36 sub->num_rects = save_num_rects;
1124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 756 times.
756 if (subtitle_out_size < 0) {
1125 av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
1126 exit_program(1);
1127 }
1128
1129 756 av_packet_unref(pkt);
1130 756 pkt->data = subtitle_out;
1131 756 pkt->size = subtitle_out_size;
1132 756 pkt->pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->mux_timebase);
1133 756 pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
1134
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 684 times.
756 if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
1135 /* XXX: the pts correction is handled here. Maybe handling
1136 it in the codec would be better */
1137
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
72 if (i == 0)
1138 36 pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
1139 else
1140 36 pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
1141 }
1142 756 pkt->dts = pkt->pts;
1143 756 output_packet(of, pkt, ost, 0);
1144 }
1145 }
1146
1147 102044 static void do_video_out(OutputFile *of,
1148 OutputStream *ost,
1149 AVFrame *next_picture)
1150 {
1151 int ret, format_video_sync;
1152 102044 AVPacket *pkt = ost->pkt;
1153 102044 AVCodecContext *enc = ost->enc_ctx;
1154 AVRational frame_rate;
1155 int nb_frames, nb0_frames, i;
1156 double delta, delta0;
1157 102044 double duration = 0;
1158 102044 double sync_ipts = AV_NOPTS_VALUE;
1159 102044 int frame_size = 0;
1160 102044 InputStream *ist = NULL;
1161 102044 AVFilterContext *filter = ost->filter->filter;
1162
1163 102044 init_output_stream_wrapper(ost, next_picture, 1);
1164 102044 sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, next_picture);
1165
1166
2/2
✓ Branch 0 taken 100528 times.
✓ Branch 1 taken 1516 times.
102044 if (ost->source_index >= 0)
1167 100528 ist = input_streams[ost->source_index];
1168
1169 102044 frame_rate = av_buffersink_get_frame_rate(filter);
1170
4/4
✓ Branch 0 taken 101949 times.
✓ Branch 1 taken 95 times.
✓ Branch 2 taken 101915 times.
✓ Branch 3 taken 34 times.
102044 if (frame_rate.num > 0 && frame_rate.den > 0)
1171 101915 duration = 1/(av_q2d(frame_rate) * av_q2d(enc->time_base));
1172
1173
6/8
✓ Branch 0 taken 100528 times.
✓ Branch 1 taken 1516 times.
✓ Branch 2 taken 68275 times.
✓ Branch 3 taken 32253 times.
✓ Branch 4 taken 68275 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 68275 times.
✗ Branch 7 not taken.
102044 if(ist && ist->st->start_time != AV_NOPTS_VALUE && ist->first_dts != AV_NOPTS_VALUE && ost->frame_rate.num)
1174
2/2
✓ Branch 2 taken 276 times.
✓ Branch 3 taken 67999 times.
68275 duration = FFMIN(duration, 1/(av_q2d(ost->frame_rate) * av_q2d(enc->time_base)));
1175
1176
2/2
✓ Branch 0 taken 101947 times.
✓ Branch 1 taken 97 times.
102044 if (!ost->filters_script &&
1177
2/2
✓ Branch 0 taken 85672 times.
✓ Branch 1 taken 16275 times.
101947 !ost->filters &&
1178
5/6
✓ Branch 0 taken 85672 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83522 times.
✓ Branch 3 taken 2150 times.
✓ Branch 4 taken 81486 times.
✓ Branch 5 taken 2036 times.
85672 (nb_filtergraphs == 0 || !filtergraphs[0]->graph_desc) &&
1179
1/2
✓ Branch 0 taken 81486 times.
✗ Branch 1 not taken.
81486 next_picture &&
1180 81486 ist &&
1181
2/2
✓ Branch 2 taken 77161 times.
✓ Branch 3 taken 4325 times.
81486 lrintf(next_picture->pkt_duration * av_q2d(ist->st->time_base) / av_q2d(enc->time_base)) > 0) {
1182 77161 duration = lrintf(next_picture->pkt_duration * av_q2d(ist->st->time_base) / av_q2d(enc->time_base));
1183 }
1184
1185
2/2
✓ Branch 0 taken 2451 times.
✓ Branch 1 taken 99593 times.
102044 if (!next_picture) {
1186 //end, flushing
1187 2451 nb0_frames = nb_frames = mid_pred(ost->last_nb0_frames[0],
1188 ost->last_nb0_frames[1],
1189 ost->last_nb0_frames[2]);
1190 } else {
1191 99593 delta0 = sync_ipts - ost->sync_opts; // delta0 is the "drift" between the input frame (next_picture) and where it would fall in the output.
1192 99593 delta = delta0 + duration;
1193
1194 /* by default, we output a single frame */
1195 99593 nb0_frames = 0; // tracks the number of times the PREVIOUS frame should be duplicated, mostly for variable framerate (VFR)
1196 99593 nb_frames = 1;
1197
1198 99593 format_video_sync = video_sync_method;
1199
2/2
✓ Branch 0 taken 79579 times.
✓ Branch 1 taken 20014 times.
99593 if (format_video_sync == VSYNC_AUTO) {
1200
2/2
✓ Branch 0 taken 12650 times.
✓ Branch 1 taken 66929 times.
79579 if(!strcmp(of->ctx->oformat->name, "avi")) {
1201 12650 format_video_sync = VSYNC_VFR;
1202 } else
1203
4/4
✓ Branch 0 taken 57473 times.
✓ Branch 1 taken 9456 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 57463 times.
66929 format_video_sync = (of->ctx->oformat->flags & AVFMT_VARIABLE_FPS) ? ((of->ctx->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH : VSYNC_VFR) : VSYNC_CFR;
1204
2/2
✓ Branch 0 taken 78108 times.
✓ Branch 1 taken 1471 times.
79579 if ( ist
1205
2/2
✓ Branch 0 taken 9456 times.
✓ Branch 1 taken 68652 times.
78108 && format_video_sync == VSYNC_CFR
1206
2/2
✓ Branch 0 taken 8351 times.
✓ Branch 1 taken 1105 times.
9456 && input_files[ist->file_index]->ctx->nb_streams == 1
1207
1/2
✓ Branch 0 taken 8351 times.
✗ Branch 1 not taken.
8351 && input_files[ist->file_index]->input_ts_offset == 0) {
1208 8351 format_video_sync = VSYNC_VSCFR;
1209 }
1210
3/4
✓ Branch 0 taken 1105 times.
✓ Branch 1 taken 78474 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1105 times.
79579 if (format_video_sync == VSYNC_CFR && copy_ts) {
1211 format_video_sync = VSYNC_VSCFR;
1212 }
1213 }
1214
4/4
✓ Branch 0 taken 98334 times.
✓ Branch 1 taken 1259 times.
✓ Branch 2 taken 8351 times.
✓ Branch 3 taken 89983 times.
99593 ost->is_cfr = (format_video_sync == VSYNC_CFR || format_video_sync == VSYNC_VSCFR);
1215
1216
4/4
✓ Branch 0 taken 12076 times.
✓ Branch 1 taken 87517 times.
✓ Branch 2 taken 11810 times.
✓ Branch 3 taken 266 times.
99593 if (delta0 < 0 &&
1217
2/2
✓ Branch 0 taken 11217 times.
✓ Branch 1 taken 593 times.
11810 delta > 0 &&
1218
1/2
✓ Branch 0 taken 11217 times.
✗ Branch 1 not taken.
11217 format_video_sync != VSYNC_PASSTHROUGH &&
1219 format_video_sync != VSYNC_DROP) {
1220
2/2
✓ Branch 0 taken 691 times.
✓ Branch 1 taken 10526 times.
11217 if (delta0 < -0.6) {
1221 691 av_log(NULL, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0);
1222 } else
1223 10526 av_log(NULL, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0);
1224 11217 sync_ipts = ost->sync_opts;
1225 11217 duration += delta0;
1226 11217 delta0 = 0;
1227 }
1228
1229
4/5
✓ Branch 0 taken 8351 times.
✓ Branch 1 taken 1259 times.
✓ Branch 2 taken 70116 times.
✓ Branch 3 taken 19867 times.
✗ Branch 4 not taken.
99593 switch (format_video_sync) {
1230 8351 case VSYNC_VSCFR:
1231
3/4
✓ Branch 0 taken 379 times.
✓ Branch 1 taken 7972 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 379 times.
8351 if (ost->frame_number == 0 && delta0 >= 0.5) {
1232 av_log(NULL, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0));
1233 delta = duration;
1234 delta0 = 0;
1235 ost->sync_opts = llrint(sync_ipts);
1236 }
1237 case VSYNC_CFR:
1238 // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
1239
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 9610 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
9610 if (frame_drop_threshold && delta < frame_drop_threshold && ost->frame_number) {
1240 nb_frames = 0;
1241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9610 times.
9610 } else if (delta < -1.1)
1242 nb_frames = 0;
1243
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 9260 times.
9610 else if (delta > 1.1) {
1244 350 nb_frames = lrintf(delta);
1245
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 348 times.
350 if (delta0 > 1.1)
1246 2 nb0_frames = llrintf(delta0 - 0.6);
1247 }
1248 9610 break;
1249 70116 case VSYNC_VFR:
1250
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 69948 times.
70116 if (delta <= -0.6)
1251 168 nb_frames = 0;
1252
2/2
✓ Branch 0 taken 66821 times.
✓ Branch 1 taken 3127 times.
69948 else if (delta > 0.6)
1253 66821 ost->sync_opts = llrint(sync_ipts);
1254 70116 break;
1255 19867 case VSYNC_DROP:
1256 case VSYNC_PASSTHROUGH:
1257 19867 ost->sync_opts = llrint(sync_ipts);
1258 19867 break;
1259 default:
1260 av_assert0(0);
1261 }
1262 }
1263
1264 102044 nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number);
1265 102044 nb0_frames = FFMIN(nb0_frames, nb_frames);
1266
1267 102044 memmove(ost->last_nb0_frames + 1,
1268 102044 ost->last_nb0_frames,
1269 sizeof(ost->last_nb0_frames[0]) * (FF_ARRAY_ELEMS(ost->last_nb0_frames) - 1));
1270 102044 ost->last_nb0_frames[0] = nb0_frames;
1271
1272
4/4
✓ Branch 0 taken 102041 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 168 times.
✓ Branch 3 taken 101873 times.
102044 if (nb0_frames == 0 && ost->last_dropped) {
1273 168 nb_frames_drop++;
1274 168 av_log(NULL, AV_LOG_VERBOSE,
1275 "*** dropping frame %d from stream %d at ts %"PRId64"\n",
1276 168 ost->frame_number, ost->st->index, ost->last_frame->pts);
1277 }
1278
5/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 102041 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 49 times.
✓ Branch 5 taken 101995 times.
102044 if (nb_frames > (nb0_frames && ost->last_dropped) + (nb_frames > nb0_frames)) {
1279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (nb_frames > dts_error_threshold * 30) {
1280 av_log(NULL, AV_LOG_ERROR, "%d frame duplication too large, skipping\n", nb_frames - 1);
1281 nb_frames_drop++;
1282 return;
1283 }
1284
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);
1285 49 av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", nb_frames - 1);
1286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (nb_frames_dup > dup_warning) {
1287 av_log(NULL, AV_LOG_WARNING, "More than %d frames duplicated\n", dup_warning);
1288 dup_warning *= 10;
1289 }
1290 }
1291
4/4
✓ Branch 0 taken 2621 times.
✓ Branch 1 taken 99423 times.
✓ Branch 2 taken 170 times.
✓ Branch 3 taken 2451 times.
102044 ost->last_dropped = nb_frames == nb0_frames && next_picture;
1292
5/6
✓ Branch 0 taken 170 times.
✓ Branch 1 taken 101874 times.
✓ Branch 2 taken 170 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 46 times.
✓ Branch 5 taken 124 times.
102044 ost->dropped_keyframe = ost->last_dropped && next_picture && next_picture->key_frame;
1293
1294 /* duplicates frame if needed */
1295
2/2
✓ Branch 0 taken 99634 times.
✓ Branch 1 taken 102043 times.
201677 for (i = 0; i < nb_frames; i++) {
1296 AVFrame *in_picture;
1297 99634 int forced_keyframe = 0;
1298 double pts_time;
1299
1300
3/4
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 99565 times.
✓ Branch 2 taken 69 times.
✗ Branch 3 not taken.
99634 if (i < nb0_frames && ost->last_frame) {
1301 69 in_picture = ost->last_frame;
1302 } else
1303 99565 in_picture = next_picture;
1304
1305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99634 times.
99634 if (!in_picture)
1306 return;
1307
1308 99634 in_picture->pts = ost->sync_opts;
1309
1310
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 99633 times.
99634 if (!check_recording_time(ost))
1311 1 return;
1312
1313 99633 in_picture->quality = enc->global_quality;
1314 99633 in_picture->pict_type = 0;
1315
1316
2/2
✓ Branch 0 taken 4688 times.
✓ Branch 1 taken 94945 times.
99633 if (ost->forced_kf_ref_pts == AV_NOPTS_VALUE &&
1317
1/2
✓ Branch 0 taken 4688 times.
✗ Branch 1 not taken.
4688 in_picture->pts != AV_NOPTS_VALUE)
1318 4688 ost->forced_kf_ref_pts = in_picture->pts;
1319
1320 199266 pts_time = in_picture->pts != AV_NOPTS_VALUE ?
1321
1/2
✓ Branch 0 taken 99633 times.
✗ Branch 1 not taken.
99633 (in_picture->pts - ost->forced_kf_ref_pts) * av_q2d(enc->time_base) : NAN;
1322
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 99594 times.
99633 if (ost->forced_kf_index < ost->forced_kf_count &&
1323
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 37 times.
39 in_picture->pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
1324 2 ost->forced_kf_index++;
1325 2 forced_keyframe = 1;
1326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99631 times.
99631 } else if (ost->forced_keyframes_pexpr) {
1327 double res;
1328 ost->forced_keyframes_expr_const_values[FKF_T] = pts_time;
1329 res = av_expr_eval(ost->forced_keyframes_pexpr,
1330 ost->forced_keyframes_expr_const_values, NULL);
1331 ff_dlog(NULL, "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
1332 ost->forced_keyframes_expr_const_values[FKF_N],
1333 ost->forced_keyframes_expr_const_values[FKF_N_FORCED],
1334 ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N],
1335 ost->forced_keyframes_expr_const_values[FKF_T],
1336 ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T],
1337 res);
1338 if (res) {
1339 forced_keyframe = 1;
1340 ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] =
1341 ost->forced_keyframes_expr_const_values[FKF_N];
1342 ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] =
1343 ost->forced_keyframes_expr_const_values[FKF_T];
1344 ost->forced_keyframes_expr_const_values[FKF_N_FORCED] += 1;
1345 }
1346
1347 ost->forced_keyframes_expr_const_values[FKF_N] += 1;
1348
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 99583 times.
99631 } else if ( ost->forced_keyframes
1349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 && !strncmp(ost->forced_keyframes, "source", 6)
1350 && in_picture->key_frame==1
1351 && !i) {
1352 forced_keyframe = 1;
1353
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 99583 times.
99631 } else if ( ost->forced_keyframes
1354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 && !strncmp(ost->forced_keyframes, "source_no_drop", 14)
1355 && !i) {
1356 forced_keyframe = (in_picture->key_frame == 1) || ost->dropped_keyframe;
1357 ost->dropped_keyframe = 0;
1358 }
1359
1360
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 99631 times.
99633 if (forced_keyframe) {
1361 2 in_picture->pict_type = AV_PICTURE_TYPE_I;
1362 2 av_log(NULL, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
1363 }
1364
1365 99633 update_benchmark(NULL);
1366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99633 times.
99633 if (debug_ts) {
1367 av_log(NULL, AV_LOG_INFO, "encoder <- type:video "
1368 "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
1369 av_ts2str(in_picture->pts), av_ts2timestr(in_picture->pts, &enc->time_base),
1370 enc->time_base.num, enc->time_base.den);
1371 }
1372
1373 99633 ost->frames_encoded++;
1374
1375 99633 ret = avcodec_send_frame(enc, in_picture);
1376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99633 times.
99633 if (ret < 0)
1377 goto error;
1378 // Make sure Closed Captions will not be duplicated
1379 99633 av_frame_remove_side_data(in_picture, AV_FRAME_DATA_A53_CC);
1380
1381 while (1) {
1382 199018 av_packet_unref(pkt);
1383 199018 ret = avcodec_receive_packet(enc, pkt);
1384 199018 update_benchmark("encode_video %d.%d", ost->file_index, ost->index);
1385
2/2
✓ Branch 0 taken 99633 times.
✓ Branch 1 taken 99385 times.
199018 if (ret == AVERROR(EAGAIN))
1386 99633 break;
1387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99385 times.
99385 if (ret < 0)
1388 goto error;
1389
1390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99385 times.
99385 if (debug_ts) {
1391 av_log(NULL, AV_LOG_INFO, "encoder -> type:video "
1392 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1393 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
1394 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base));
1395 }
1396
1397
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 99385 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
99385 if (pkt->pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & AV_CODEC_CAP_DELAY))
1398 pkt->pts = ost->sync_opts;
1399
1400 99385 av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
1401
1402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99385 times.
99385 if (debug_ts) {
1403 av_log(NULL, AV_LOG_INFO, "encoder -> type:video "
1404 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1405 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->mux_timebase),
1406 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->mux_timebase));
1407 }
1408
1409 99385 frame_size = pkt->size;
1410 99385 output_packet(of, pkt, ost, 0);
1411
1412 /* if two pass, output log */
1413
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 99385 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
99385 if (ost->logfile && enc->stats_out) {
1414 fprintf(ost->logfile, "%s", enc->stats_out);
1415 }
1416 }
1417 99633 ost->sync_opts++;
1418 /*
1419 * For video, number of frames in == number of packets out.
1420 * But there may be reordering, so we can't throw away frames on encoder
1421 * flush, we need to limit them here, before they go into encoder.
1422 */
1423 99633 ost->frame_number++;
1424
1425
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 99633 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
99633 if (vstats_filename && frame_size)
1426 do_video_stats(ost, frame_size);
1427 }
1428
1429
2/2
✓ Branch 0 taken 4780 times.
✓ Branch 1 taken 97263 times.
102043 if (!ost->last_frame)
1430 4780 ost->last_frame = av_frame_alloc();
1431 102043 av_frame_unref(ost->last_frame);
1432
3/4
✓ Branch 0 taken 99592 times.
✓ Branch 1 taken 2451 times.
✓ Branch 2 taken 99592 times.
✗ Branch 3 not taken.
102043 if (next_picture && ost->last_frame)
1433 99592 av_frame_ref(ost->last_frame, next_picture);
1434 else
1435 2451 av_frame_free(&ost->last_frame);
1436
1437 102043 return;
1438 error:
1439 av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
1440 exit_program(1);
1441 }
1442
1443 static double psnr(double d)
1444 {
1445 return -10.0 * log10(d);
1446 }
1447
1448 static void do_video_stats(OutputStream *ost, int frame_size)
1449 {
1450 AVCodecContext *enc;
1451 int frame_number;
1452 double ti1, bitrate, avg_bitrate;
1453
1454 /* this is executed just the first time do_video_stats is called */
1455 if (!vstats_file) {
1456 vstats_file = fopen(vstats_filename, "w");
1457 if (!vstats_file) {
1458 perror("fopen");
1459 exit_program(1);
1460 }
1461 }
1462
1463 enc = ost->enc_ctx;
1464 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1465 frame_number = ost->st->nb_frames;
1466 if (vstats_version <= 1) {
1467 fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
1468 ost->quality / (float)FF_QP2LAMBDA);
1469 } else {
1470 fprintf(vstats_file, "out= %2d st= %2d frame= %5d q= %2.1f ", ost->file_index, ost->index, frame_number,
1471 ost->quality / (float)FF_QP2LAMBDA);
1472 }
1473
1474 if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
1475 fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
1476
1477 fprintf(vstats_file,"f_size= %6d ", frame_size);
1478 /* compute pts value */
1479 ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base);
1480 if (ti1 < 0.01)
1481 ti1 = 0.01;
1482
1483 bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
1484 avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
1485 fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1486 (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
1487 fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type));
1488 }
1489 }
1490
1491 354 static void finish_output_stream(OutputStream *ost)
1492 {
1493 354 OutputFile *of = output_files[ost->file_index];
1494 int i;
1495
1496 354 ost->finished = ENCODER_FINISHED | MUXER_FINISHED;
1497
1498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 354 times.
354 if (of->shortest) {
1499 for (i = 0; i < of->ctx->nb_streams; i++)
1500 output_streams[of->ost_index + i]->finished = ENCODER_FINISHED | MUXER_FINISHED;
1501 }
1502 354 }
1503
1504 /**
1505 * Get and encode new output from any of the filtergraphs, without causing
1506 * activity.
1507 *
1508 * @return 0 for success, <0 for severe errors
1509 */
1510 413328 static int reap_filters(int flush)
1511 {
1512 413328 AVFrame *filtered_frame = NULL;
1513 int i;
1514
1515 /* Reap all buffers present in the buffer sinks */
1516
2/2
✓ Branch 0 taken 433680 times.
✓ Branch 1 taken 413328 times.
847008 for (i = 0; i < nb_output_streams; i++) {
1517 433680 OutputStream *ost = output_streams[i];
1518 433680 OutputFile *of = output_files[ost->file_index];
1519 AVFilterContext *filter;
1520 433680 AVCodecContext *enc = ost->enc_ctx;
1521 433680 int ret = 0;
1522
1523
4/4
✓ Branch 0 taken 387244 times.
✓ Branch 1 taken 46436 times.
✓ Branch 2 taken 8532 times.
✓ Branch 3 taken 378712 times.
433680 if (!ost->filter || !ost->filter->graph->graph)
1524 54968 continue;
1525 378712 filter = ost->filter->filter;
1526
1527 /*
1528 * Unlike video, with audio the audio frame size matters.
1529 * Currently we are fully reliant on the lavfi filter chain to
1530 * do the buffering deed for us, and thus the frame size parameter
1531 * needs to be set accordingly. Where does one get the required
1532 * frame size? From the initialized AVCodecContext of an audio
1533 * encoder. Thus, if we have gotten to an audio stream, initialize
1534 * the encoder earlier than receiving the first AVFrame.
1535 */
1536
2/2
✓ Branch 1 taken 261165 times.
✓ Branch 2 taken 117547 times.
378712 if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_AUDIO)
1537 261165 init_output_stream_wrapper(ost, NULL, 1);
1538
1539
3/4
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 378667 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45 times.
378712 if (!ost->pkt && !(ost->pkt = av_packet_alloc())) {
1540 return AVERROR(ENOMEM);
1541 }
1542
3/4
✓ Branch 0 taken 5820 times.
✓ Branch 1 taken 372892 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5820 times.
378712 if (!ost->filtered_frame && !(ost->filtered_frame = av_frame_alloc())) {
1543 return AVERROR(ENOMEM);
1544 }
1545 378712 filtered_frame = ost->filtered_frame;
1546
1547 while (1) {
1548 779377 ret = av_buffersink_get_frame_flags(filter, filtered_frame,
1549 AV_BUFFERSINK_FLAG_NO_REQUEST);
1550
2/2
✓ Branch 0 taken 378712 times.
✓ Branch 1 taken 400665 times.
779377 if (ret < 0) {
1551
3/4
✓ Branch 0 taken 3999 times.
✓ Branch 1 taken 374713 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3999 times.
378712 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
1552 av_log(NULL, AV_LOG_WARNING,
1553 "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(ret));
1554
4/4
✓ Branch 0 taken 3788 times.
✓ Branch 1 taken 374924 times.
✓ Branch 2 taken 3628 times.
✓ Branch 3 taken 160 times.
378712 } else if (flush && ret == AVERROR_EOF) {
1555
2/2
✓ Branch 1 taken 2451 times.
✓ Branch 2 taken 1177 times.
3628 if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_VIDEO)
1556 2451 do_video_out(of, ost, NULL);
1557 }
1558 378712 break;
1559 }
1560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400665 times.
400665 if (ost->finished) {
1561 av_frame_unref(filtered_frame);
1562 continue;
1563 }
1564
1565
2/3
✓ Branch 1 taken 99593 times.
✓ Branch 2 taken 301072 times.
✗ Branch 3 not taken.
400665 switch (av_buffersink_get_type(filter)) {
1566 99593 case AVMEDIA_TYPE_VIDEO:
1567
1/2
✓ Branch 0 taken 99593 times.
✗ Branch 1 not taken.
99593 if (!ost->frame_aspect_ratio.num)
1568 99593 enc->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
1569
1570 99593 do_video_out(of, ost, filtered_frame);
1571 99593 break;
1572 301072 case AVMEDIA_TYPE_AUDIO:
1573
1/2
✓ Branch 0 taken 301072 times.
✗ Branch 1 not taken.
301072 if (!(enc->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
1574
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 301072 times.
301072 enc->channels != filtered_frame->channels) {
1575 av_log(NULL, AV_LOG_ERROR,
1576 "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
1577 break;
1578 }
1579 301072 do_audio_out(of, ost, filtered_frame);
1580 301072 break;
1581 default:
1582 // TODO support subtitle filters
1583 av_assert0(0);
1584 }
1585
1586 400665 av_frame_unref(filtered_frame);
1587 }
1588 }
1589
1590 413328 return 0;
1591 }
1592
1593 6014 static void print_final_stats(int64_t total_size)
1594 {
1595 6014 uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
1596 6014 uint64_t subtitle_size = 0;
1597 6014 uint64_t data_size = 0;
1598 6014 float percent = -1.0;
1599 int i, j;
1600 6014 int pass1_used = 1;
1601
1602
2/2
✓ Branch 0 taken 6195 times.
✓ Branch 1 taken 6014 times.
12209 for (i = 0; i < nb_output_streams; i++) {
1603 6195 OutputStream *ost = output_streams[i];
1604
4/4
✓ Branch 0 taken 4880 times.
✓ Branch 1 taken 1253 times.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 13 times.
6195 switch (ost->enc_ctx->codec_type) {
1605 4880 case AVMEDIA_TYPE_VIDEO: video_size += ost->data_size; break;
1606 1253 case AVMEDIA_TYPE_AUDIO: audio_size += ost->data_size; break;
1607 49 case AVMEDIA_TYPE_SUBTITLE: subtitle_size += ost->data_size; break;
1608 13 default: other_size += ost->data_size; break;
1609 }
1610 6195 extra_size += ost->enc_ctx->extradata_size;
1611 6195 data_size += ost->data_size;
1612
1/2
✓ Branch 0 taken 6195 times.
✗ Branch 1 not taken.
6195 if ( (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
1613 != AV_CODEC_FLAG_PASS1)
1614 6195 pass1_used = 0;
1615 }
1616
1617
6/6
✓ Branch 0 taken 6004 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 5952 times.
✓ Branch 3 taken 52 times.
✓ Branch 4 taken 4160 times.
✓ Branch 5 taken 1792 times.
6014 if (data_size && total_size>0 && total_size >= data_size)
1618 4160 percent = 100.0 * (total_size - data_size) / data_size;
1619
1620 6014 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: ",
1621 video_size / 1024.0,
1622 audio_size / 1024.0,
1623 subtitle_size / 1024.0,
1624 other_size / 1024.0,
1625 extra_size / 1024.0);
1626
2/2
✓ Branch 0 taken 4160 times.
✓ Branch 1 taken 1854 times.
6014 if (percent >= 0.0)
1627 4160 av_log(NULL, AV_LOG_INFO, "%f%%", percent);
1628 else
1629 1854 av_log(NULL, AV_LOG_INFO, "unknown");
1630 6014 av_log(NULL, AV_LOG_INFO, "\n");
1631
1632 /* print verbose per-stream stats */
1633
2/2
✓ Branch 0 taken 6044 times.
✓ Branch 1 taken 6014 times.
12058 for (i = 0; i < nb_input_files; i++) {
1634 6044 InputFile *f = input_files[i];
1635 6044 uint64_t total_packets = 0, total_size = 0;
1636
1637 6044 av_log(NULL, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
1638 6044 i, f->ctx->url);
1639
1640
2/2
✓ Branch 0 taken 6408 times.
✓ Branch 1 taken 6044 times.
12452 for (j = 0; j < f->nb_streams; j++) {
1641 6408 InputStream *ist = input_streams[f->ist_index + j];
1642 6408 enum AVMediaType type = ist->dec_ctx->codec_type;
1643
1644 6408 total_size += ist->data_size;
1645 6408 total_packets += ist->nb_packets;
1646
1647 6408 av_log(NULL, AV_LOG_VERBOSE, " Input stream #%d:%d (%s): ",
1648 i, j, media_type_string(type));
1649 6408 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
1650 ist->nb_packets, ist->data_size);
1651
1652
2/2
✓ Branch 0 taken 5828 times.
✓ Branch 1 taken 580 times.
6408 if (ist->decoding_needed) {
1653 5828 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames decoded",
1654 ist->frames_decoded);
1655
2/2
✓ Branch 0 taken 1132 times.
✓ Branch 1 taken 4696 times.
5828 if (type == AVMEDIA_TYPE_AUDIO)
1656 1132 av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
1657 5828 av_log(NULL, AV_LOG_VERBOSE, "; ");
1658 }
1659
1660 6408 av_log(NULL, AV_LOG_VERBOSE, "\n");
1661 }
1662
1663 6044 av_log(NULL, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
1664 total_packets, total_size);
1665 }
1666
1667
2/2
✓ Branch 0 taken 6015 times.
✓ Branch 1 taken 6014 times.
12029 for (i = 0; i < nb_output_files; i++) {
1668 6015 OutputFile *of = output_files[i];
1669 6015 uint64_t total_packets = 0, total_size = 0;
1670
1671 6015 av_log(NULL, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
1672 6015 i, of->ctx->url);
1673
1674
2/2
✓ Branch 0 taken 6195 times.
✓ Branch 1 taken 6015 times.
12210 for (j = 0; j < of->ctx->nb_streams; j++) {
1675 6195 OutputStream *ost = output_streams[of->ost_index + j];
1676 6195 enum AVMediaType type = ost->enc_ctx->codec_type;
1677
1678 6195 total_size += ost->data_size;
1679 6195 total_packets += ost->packets_written;
1680
1681 6195 av_log(NULL, AV_LOG_VERBOSE, " Output stream #%d:%d (%s): ",
1682 i, j, media_type_string(type));
1683
2/2
✓ Branch 0 taken 5854 times.
✓ Branch 1 taken 341 times.
6195 if (ost->encoding_needed) {
1684 5854 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
1685 ost->frames_encoded);
1686
2/2
✓ Branch 0 taken 1131 times.
✓ Branch 1 taken 4723 times.
5854 if (type == AVMEDIA_TYPE_AUDIO)
1687 1131 av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
1688 5854 av_log(NULL, AV_LOG_VERBOSE, "; ");
1689 }
1690
1691 6195 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
1692 ost->packets_written, ost->data_size);
1693
1694 6195 av_log(NULL, AV_LOG_VERBOSE, "\n");
1695 }
1696
1697 6015 av_log(NULL, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
1698 total_packets, total_size);
1699 }
1700
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6008 times.
6014 if(video_size + data_size + audio_size + subtitle_size + extra_size == 0){
1701 6 av_log(NULL, AV_LOG_WARNING, "Output file is empty, nothing was encoded ");
1702
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 if (pass1_used) {
1703 1 av_log(NULL, AV_LOG_WARNING, "\n");
1704 } else {
1705 5 av_log(NULL, AV_LOG_WARNING, "(check -ss / -t / -frames parameters if used)\n");
1706 }
1707 }
1708 6014 }
1709
1710 417742 static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time)
1711 {
1712 AVBPrint buf, buf_script;
1713 OutputStream *ost;
1714 AVFormatContext *oc;
1715 int64_t total_size;
1716 AVCodecContext *enc;
1717 int frame_number, vid, i;
1718 double bitrate;
1719 double speed;
1720 417742 int64_t pts = INT64_MIN + 1;
1721 static int64_t last_time = -1;
1722 static int first_report = 1;
1723 static int qp_histogram[52];
1724 int hours, mins, secs, us;
1725 const char *hours_sign;
1726 int ret;
1727 float t;
1728
1729
5/6
✓ Branch 0 taken 407341 times.
✓ Branch 1 taken 10401 times.
✓ Branch 2 taken 401359 times.
✓ Branch 3 taken 5982 times.
✓ Branch 4 taken 401359 times.
✗ Branch 5 not taken.
417742 if (!print_stats && !is_last_report && !progress_avio)
1730 411669 return;
1731
1732
2/2
✓ Branch 0 taken 10369 times.
✓ Branch 1 taken 6014 times.
16383 if (!is_last_report) {
1733
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 10337 times.
10369 if (last_time == -1) {
1734 32 last_time = cur_time;
1735 }
1736
4/4
✓ Branch 0 taken 10342 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 10304 times.
10369 if (((cur_time - last_time) < stats_period && !first_report) ||
1737
4/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 32 times.
65 (first_report && nb_output_dumped < nb_output_files))
1738 10310 return;
1739 59 last_time = cur_time;
1740 }
1741
1742 6073 t = (cur_time-timer_start) / 1000000.0;
1743
1744
1745 6073 oc = output_files[0]->ctx;
1746
1747 6073 total_size = avio_size(oc->pb);
1748
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 5972 times.
6073 if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too
1749 101 total_size = avio_tell(oc->pb);
1750
1751 6073 vid = 0;
1752 6073 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
1753 6073 av_bprint_init(&buf_script, 0, AV_BPRINT_SIZE_AUTOMATIC);
1754
2/2
✓ Branch 0 taken 6265 times.
✓ Branch 1 taken 6073 times.
12338 for (i = 0; i < nb_output_streams; i++) {
1755 6265 float q = -1;
1756 6265 ost = output_streams[i];
1757 6265 enc = ost->enc_ctx;
1758
2/2
✓ Branch 0 taken 5902 times.
✓ Branch 1 taken 363 times.
6265 if (!ost->stream_copy)
1759 5902 q = ost->quality / (float) FF_QP2LAMBDA;
1760
1761
4/4
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 6099 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 136 times.
6265 if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1762 30 av_bprintf(&buf, "q=%2.1f ", q);
1763 30 av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
1764 ost->file_index, ost->index, q);
1765 }
1766
4/4
✓ Branch 0 taken 6099 times.
✓ Branch 1 taken 166 times.
✓ Branch 2 taken 4873 times.
✓ Branch 3 taken 1226 times.
6265 if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1767 float fps;
1768
1769 4873 frame_number = ost->frame_number;
1770
2/2
✓ Branch 0 taken 871 times.
✓ Branch 1 taken 4002 times.
4873 fps = t > 1 ? frame_number / t : 0;
1771 4873 av_bprintf(&buf, "frame=%5d fps=%3.*f q=%3.1f ",
1772 frame_number, fps < 9.95, fps, q);
1773 4873 av_bprintf(&buf_script, "frame=%d\n", frame_number);
1774 4873 av_bprintf(&buf_script, "fps=%.2f\n", fps);
1775 4873 av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
1776 ost->file_index, ost->index, q);
1777
2/2
✓ Branch 0 taken 4854 times.
✓ Branch 1 taken 19 times.
4873 if (is_last_report)
1778 4854 av_bprintf(&buf, "L");
1779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4873 times.
4873 if (qp_hist) {
1780 int j;
1781 int qp = lrintf(q);
1782 if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
1783 qp_histogram[qp]++;
1784 for (j = 0; j < 32; j++)
1785 av_bprintf(&buf, "%X", av_log2(qp_histogram[j] + 1));
1786 }
1787
1788
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4873 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4873 if ((enc->flags & AV_CODEC_FLAG_PSNR) && (ost->pict_type != AV_PICTURE_TYPE_NONE || is_last_report)) {
1789 int j;
1790 double error, error_sum = 0;
1791 double scale, scale_sum = 0;
1792 double p;
1793 char type[3] = { 'Y','U','V' };
1794 av_bprintf(&buf, "PSNR=");
1795 for (j = 0; j < 3; j++) {
1796 if (is_last_report) {
1797 error = enc->error[j];
1798 scale = enc->width * enc->height * 255.0 * 255.0 * frame_number;
1799 } else {
1800 error = ost->error[j];
1801 scale = enc->width * enc->height * 255.0 * 255.0;
1802 }
1803 if (j)
1804 scale /= 4;
1805 error_sum += error;
1806 scale_sum += scale;
1807 p = psnr(error / scale);
1808 av_bprintf(&buf, "%c:%2.2f ", type[j], p);
1809 av_bprintf(&buf_script, "stream_%d_%d_psnr_%c=%2.2f\n",
1810 ost->file_index, ost->index, type[j] | 32, p);
1811 }
1812 p = psnr(error_sum / scale_sum);
1813 av_bprintf(&buf, "*:%2.2f ", psnr(error_sum / scale_sum));
1814 av_bprintf(&buf_script, "stream_%d_%d_psnr_all=%2.2f\n",
1815 ost->file_index, ost->index, p);
1816 }
1817 4873 vid = 1;
1818 }
1819 /* compute min output value */
1820
1/2
✓ Branch 1 taken 6265 times.
✗ Branch 2 not taken.
6265 if (av_stream_get_end_pts(ost->st) != AV_NOPTS_VALUE) {
1821
2/2
✓ Branch 1 taken 6208 times.
✓ Branch 2 taken 57 times.
6265 pts = FFMAX(pts, av_rescale_q(av_stream_get_end_pts(ost->st),
1822 ost->st->time_base, AV_TIME_BASE_Q));
1823
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6262 times.
6265 if (copy_ts) {
1824
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)
1825 3 copy_ts_first_pts = pts;
1826
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (copy_ts_first_pts != AV_NOPTS_VALUE)
1827 3 pts -= copy_ts_first_pts;
1828 }
1829 }
1830
1831
2/2
✓ Branch 0 taken 6195 times.
✓ Branch 1 taken 70 times.
6265 if (is_last_report)
1832 6195 nb_frames_drop += ost->last_dropped;
1833 }
1834
1835 6073 secs = FFABS(pts) / AV_TIME_BASE;
1836 6073 us = FFABS(pts) % AV_TIME_BASE;
1837 6073 mins = secs / 60;
1838 6073 secs %= 60;
1839 6073 hours = mins / 60;
1840 6073 mins %= 60;
1841
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6067 times.
6073 hours_sign = (pts < 0) ? "-" : "";
1842
1843
4/4
✓ Branch 0 taken 6041 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 5960 times.
✓ Branch 3 taken 81 times.
6073 bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
1844
2/2
✓ Branch 0 taken 6059 times.
✓ Branch 1 taken 14 times.
6073 speed = t != 0.0 ? (double)pts / AV_TIME_BASE / t : -1;
1845
1846
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 5981 times.
6073 if (total_size < 0) av_bprintf(&buf, "size=N/A time=");
1847 5981 else av_bprintf(&buf, "size=%8.0fkB time=", total_size / 1024.0);
1848
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6073 times.
6073 if (pts == AV_NOPTS_VALUE) {
1849 av_bprintf(&buf, "N/A ");
1850 } else {
1851 6073 av_bprintf(&buf, "%s%02d:%02d:%02d.%02d ",
1852 hours_sign, hours, mins, secs, (100 * us) / AV_TIME_BASE);
1853 }
1854
1855
2/2
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 5957 times.
6073 if (bitrate < 0) {
1856 116 av_bprintf(&buf, "bitrate=N/A");
1857 116 av_bprintf(&buf_script, "bitrate=N/A\n");
1858 }else{
1859 5957 av_bprintf(&buf, "bitrate=%6.1fkbits/s", bitrate);
1860 5957 av_bprintf(&buf_script, "bitrate=%6.1fkbits/s\n", bitrate);
1861 }
1862
1863
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 5981 times.
6073 if (total_size < 0) av_bprintf(&buf_script, "total_size=N/A\n");
1864 5981 else av_bprintf(&buf_script, "total_size=%"PRId64"\n", total_size);
1865
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6073 times.
6073 if (pts == AV_NOPTS_VALUE) {
1866 av_bprintf(&buf_script, "out_time_us=N/A\n");
1867 av_bprintf(&buf_script, "out_time_ms=N/A\n");
1868 av_bprintf(&buf_script, "out_time=N/A\n");
1869 } else {
1870 6073 av_bprintf(&buf_script, "out_time_us=%"PRId64"\n", pts);
1871 6073 av_bprintf(&buf_script, "out_time_ms=%"PRId64"\n", pts);
1872 6073 av_bprintf(&buf_script, "out_time=%s%02d:%02d:%02d.%06d\n",
1873 hours_sign, hours, mins, secs, us);
1874 }
1875
1876
4/4
✓ Branch 0 taken 6058 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 6048 times.
6073 if (nb_frames_dup || nb_frames_drop)
1877 25 av_bprintf(&buf, " dup=%d drop=%d", nb_frames_dup, nb_frames_drop);
1878 6073 av_bprintf(&buf_script, "dup_frames=%d\n", nb_frames_dup);
1879 6073 av_bprintf(&buf_script, "drop_frames=%d\n", nb_frames_drop);
1880
1881
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 6056 times.
6073 if (speed < 0) {
1882 17 av_bprintf(&buf, " speed=N/A");
1883 17 av_bprintf(&buf_script, "speed=N/A\n");
1884 } else {
1885 6056 av_bprintf(&buf, " speed=%4.3gx", speed);
1886 6056 av_bprintf(&buf_script, "speed=%4.3gx\n", speed);
1887 }
1888
1889
3/4
✓ Branch 0 taken 5982 times.
✓ Branch 1 taken 91 times.
✓ Branch 2 taken 5982 times.
✗ Branch 3 not taken.
6073 if (print_stats || is_last_report) {
1890
2/2
✓ Branch 0 taken 6014 times.
✓ Branch 1 taken 59 times.
6073 const char end = is_last_report ? '\n' : '\r';
1891
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6073 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6073 if (print_stats==1 && AV_LOG_INFO > av_log_get_level()) {
1892 fprintf(stderr, "%s %c", buf.str, end);
1893 } else
1894 6073 av_log(NULL, AV_LOG_INFO, "%s %c", buf.str, end);
1895
1896 6073 fflush(stderr);
1897 }
1898 6073 av_bprint_finalize(&buf, NULL);
1899
1900
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6073 times.
6073 if (progress_avio) {
1901 av_bprintf(&buf_script, "progress=%s\n",
1902 is_last_report ? "end" : "continue");
1903 avio_write(progress_avio, buf_script.str,
1904 FFMIN(buf_script.len, buf_script.size - 1));
1905 avio_flush(progress_avio);
1906 av_bprint_finalize(&buf_script, NULL);
1907 if (is_last_report) {
1908 if ((ret = avio_closep(&progress_avio)) < 0)
1909 av_log(NULL, AV_LOG_ERROR,
1910 "Error closing progress log, loss of information possible: %s\n", av_err2str(ret));
1911 }
1912 }
1913
1914 6073 first_report = 0;
1915
1916
2/2
✓ Branch 0 taken 6014 times.
✓ Branch 1 taken 59 times.
6073 if (is_last_report)
1917 6014 print_final_stats(total_size);
1918 }
1919
1920 1 static void ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParameters *par)
1921 {
1922 // We never got any input. Set a fake format, which will
1923 // come from libavformat.
1924 1 ifilter->format = par->format;
1925 1 ifilter->sample_rate = par->sample_rate;
1926 1 ifilter->channels = par->channels;
1927 1 ifilter->channel_layout = par->channel_layout;
1928 1 ifilter->width = par->width;
1929 1 ifilter->height = par->height;
1930 1 ifilter->sample_aspect_ratio = par->sample_aspect_ratio;
1931 1 }
1932
1933 6016 static void flush_encoders(void)
1934 {
1935 int i, ret;
1936
1937
2/2
✓ Branch 0 taken 6197 times.
✓ Branch 1 taken 6014 times.
12211 for (i = 0; i < nb_output_streams; i++) {
1938 6197 OutputStream *ost = output_streams[i];
1939 6197 AVCodecContext *enc = ost->enc_ctx;
1940 6197 OutputFile *of = output_files[ost->file_index];
1941
1942
2/2
✓ Branch 0 taken 341 times.
✓ Branch 1 taken 5856 times.
6197 if (!ost->encoding_needed)
1943 341 continue;
1944
1945 // Try to enable encoding with no input frames.
1946 // Maybe we should just let encoding fail instead.
1947
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5854 times.
5856 if (!ost->initialized) {
1948 2 FilterGraph *fg = ost->filter->graph;
1949
1950 2 av_log(NULL, AV_LOG_WARNING,
1951 "Finishing stream %d:%d without any data written to it.\n",
1952 2 ost->file_index, ost->st->index);
1953
1954
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) {
1955 int x;
1956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 for (x = 0; x < fg->nb_inputs; x++) {
1957 InputFilter *ifilter = fg->inputs[x];
1958 if (ifilter->format < 0)
1959 ifilter_parameters_from_codecpar(ifilter, ifilter->ist->st->codecpar);
1960 }
1961
1962
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!ifilter_has_all_input_formats(fg))
1963 continue;
1964
1965 2 ret = configure_filtergraph(fg);
1966
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ret < 0) {
1967 2 av_log(NULL, AV_LOG_ERROR, "Error configuring filter graph\n");
1968 2 exit_program(1);
1969 }
1970
1971 finish_output_stream(ost);
1972 }
1973
1974 init_output_stream_wrapper(ost, NULL, 1);
1975 }
1976
1977
4/4
✓ Branch 0 taken 1165 times.
✓ Branch 1 taken 4689 times.
✓ Branch 2 taken 34 times.
✓ Branch 3 taken 1131 times.
5854 if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
1978 34 continue;
1979
1980 291 for (;;) {
1981 6111 const char *desc = NULL;
1982 6111 AVPacket *pkt = ost->pkt;
1983 int pkt_size;
1984
1985
2/3
✓ Branch 0 taken 1174 times.
✓ Branch 1 taken 4937 times.
✗ Branch 2 not taken.
6111 switch (enc->codec_type) {
1986 1174 case AVMEDIA_TYPE_AUDIO:
1987 1174 desc = "audio";
1988 1174 break;
1989 4937 case AVMEDIA_TYPE_VIDEO:
1990 4937 desc = "video";
1991 4937 break;
1992 default:
1993 av_assert0(0);
1994 }
1995
1996 6111 update_benchmark(NULL);
1997
1998 6111 av_packet_unref(pkt);
1999
2/2
✓ Branch 1 taken 5820 times.
✓ Branch 2 taken 6111 times.
11931 while ((ret = avcodec_receive_packet(enc, pkt)) == AVERROR(EAGAIN)) {
2000 5820 ret = avcodec_send_frame(enc, NULL);
2001
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5820 times.
5820 if (ret < 0) {
2002 av_log(NULL, AV_LOG_FATAL, "%s encoding failed: %s\n",
2003 desc,
2004 av_err2str(ret));
2005 exit_program(1);
2006 }
2007 }
2008
2009 6111 update_benchmark("flush_%s %d.%d", desc, ost->file_index, ost->index);
2010
3/4
✓ Branch 0 taken 5820 times.
✓ Branch 1 taken 291 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5820 times.
6111 if (ret < 0 && ret != AVERROR_EOF) {
2011 av_log(NULL, AV_LOG_FATAL, "%s encoding failed: %s\n",
2012 desc,
2013 av_err2str(ret));
2014 exit_program(1);
2015 }
2016
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6111 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6111 if (ost->logfile && enc->stats_out) {
2017 fprintf(ost->logfile, "%s", enc->stats_out);
2018 }
2019
2/2
✓ Branch 0 taken 5820 times.
✓ Branch 1 taken 291 times.
6111 if (ret == AVERROR_EOF) {
2020 5820 output_packet(of, pkt, ost, 1);
2021 5820 break;
2022 }
2023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 291 times.
291 if (ost->finished & MUXER_FINISHED) {
2024 av_packet_unref(pkt);
2025 continue;
2026 }
2027 291 av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
2028 291 pkt_size = pkt->size;
2029 291 output_packet(of, pkt, ost, 0);
2030
3/4
✓ Branch 0 taken 248 times.
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 248 times.
291 if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename) {
2031 do_video_stats(ost, pkt_size);
2032 }
2033 }
2034 }
2035 6014 }
2036
2037 /*
2038 * Check whether a packet from ist should be written into ost at this time
2039 */
2040 421188 static int check_output_constraints(InputStream *ist, OutputStream *ost)
2041 {
2042 421188 OutputFile *of = output_files[ost->file_index];
2043 421188 int ist_index = input_files[ist->file_index]->ist_index + ist->st->index;
2044
2045
2/2
✓ Branch 0 taken 22316 times.
✓ Branch 1 taken 398872 times.
421188 if (ost->source_index != ist_index)
2046 22316 return 0;
2047
2048
2/2
✓ Branch 0 taken 4155 times.
✓ Branch 1 taken 394717 times.
398872 if (ost->finished)
2049 4155 return 0;
2050
2051
4/4
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 394632 times.
✓ Branch 2 taken 55 times.
✓ Branch 3 taken 30 times.
394717 if (of->start_time != AV_NOPTS_VALUE && ist->pts < of->start_time)
2052 55 return 0;
2053
2054 394662 return 1;
2055 }
2056
2057 33481 static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
2058 {
2059 33481 OutputFile *of = output_files[ost->file_index];
2060 33481 InputFile *f = input_files [ist->file_index];
2061
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 33455 times.
33481 int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
2062 33481 int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase);
2063 33481 AVPacket *opkt = ost->pkt;
2064
2065 33481 av_packet_unref(opkt);
2066 // EOF: flush output bitstream filters.
2067
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 33476 times.
33481 if (!pkt) {
2068 5 output_packet(of, opkt, ost, 1);
2069 57 return;
2070 }
2071
2072
4/4
✓ Branch 0 taken 356 times.
✓ Branch 1 taken 33120 times.
✓ Branch 2 taken 36 times.
✓ Branch 3 taken 320 times.
33476 if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
2073
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 !ost->copy_initial_nonkeyframes)
2074 36 return;
2075
2076
3/4
✓ Branch 0 taken 320 times.
✓ Branch 1 taken 33120 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 320 times.
33440 if (!ost->frame_number && !ost->copy_prior_start) {
2077 int64_t comp_start = start_time;
2078 if (copy_ts && f->start_time != AV_NOPTS_VALUE)
2079 comp_start = FFMAX(start_time, f->start_time + f->ts_offset);
2080 if (pkt->pts == AV_NOPTS_VALUE ?
2081 ist->pts < comp_start :
2082 pkt->pts < av_rescale_q(comp_start, AV_TIME_BASE_Q, ist->st->time_base))
2083 return;
2084 }
2085
2086
2/2
✓ Branch 0 taken 523 times.
✓ Branch 1 taken 32917 times.
33440 if (of->recording_time != INT64_MAX &&
2087
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 509 times.
523 ist->pts >= of->recording_time + start_time) {
2088 14 close_output_stream(ost);
2089 14 return;
2090 }
2091
2092
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 33278 times.
33426 if (f->recording_time != INT64_MAX) {
2093 148 start_time = 0;
2094
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 74 times.
148 if (copy_ts) {
2095
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 start_time += f->start_time != AV_NOPTS_VALUE ? f->start_time : 0;
2096
1/2
✓ Branch 0 taken 74 times.
✗ Branch 1 not taken.
74 start_time += start_at_zero ? 0 : f->ctx->start_time;
2097 }
2098
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 146 times.
148 if (ist->pts >= f->recording_time + start_time) {
2099 2 close_output_stream(ost);
2100 2 return;
2101 }
2102 }
2103
2104 /* force the input stream PTS */
2105
2/2
✓ Branch 0 taken 9853 times.
✓ Branch 1 taken 23571 times.
33424 if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
2106 9853 ost->sync_opts++;
2107
2108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33424 times.
33424 if (av_packet_ref(opkt, pkt) < 0)
2109 exit_program(1);
2110
2111
2/2
✓ Branch 0 taken 27959 times.
✓ Branch 1 taken 5465 times.
33424 if (pkt->pts != AV_NOPTS_VALUE)
2112 27959 opkt->pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->mux_timebase) - ost_tb_start_time;
2113
2114
2/2
✓ Branch 0 taken 5221 times.
✓ Branch 1 taken 28203 times.
33424 if (pkt->dts == AV_NOPTS_VALUE) {
2115 5221 opkt->dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->mux_timebase);
2116
2/2
✓ Branch 0 taken 22944 times.
✓ Branch 1 taken 5259 times.
28203 } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2117 22944 int duration = av_get_audio_frame_duration(ist->dec_ctx, pkt->size);
2118
2/2
✓ Branch 0 taken 7780 times.
✓ Branch 1 taken 15164 times.
22944 if(!duration)
2119 7780 duration = ist->dec_ctx->frame_size;
2120 45888 opkt->dts = av_rescale_delta(ist->st->time_base, pkt->dts,
2121 22944 (AVRational){1, ist->dec_ctx->sample_rate}, duration,
2122 &ist->filter_in_rescale_delta_last, ost->mux_timebase);
2123 /* dts will be set immediately afterwards to what pts is now */
2124 22944 opkt->pts = opkt->dts - ost_tb_start_time;
2125 } else
2126 5259 opkt->dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->mux_timebase);
2127 33424 opkt->dts -= ost_tb_start_time;
2128
2129 33424 opkt->duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->mux_timebase);
2130
2131 33424 output_packet(of, opkt, ost, 0);
2132 }
2133
2134 1406 int guess_input_channel_layout(InputStream *ist)
2135 {
2136 1406 AVCodecContext *dec = ist->dec_ctx;
2137
2138
2/2
✓ Branch 0 taken 760 times.
✓ Branch 1 taken 646 times.
1406 if (!dec->channel_layout) {
2139 char layout_name[256];
2140
2141
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 756 times.
760 if (dec->channels > ist->guess_layout_max)
2142 9 return 0;
2143 756 dec->channel_layout = av_get_default_channel_layout(dec->channels);
2144
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 751 times.
756 if (!dec->channel_layout)
2145 5 return 0;
2146 751 av_get_channel_layout_string(layout_name, sizeof(layout_name),
2147 dec->channels, dec->channel_layout);
2148 751 av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for Input Stream "
2149 751 "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name);
2150 }
2151 1397 return 1;
2152 }
2153
2154 738276 static void check_decode_result(InputStream *ist, int *got_output, int ret)
2155 {
2156
4/4
✓ Branch 0 taken 357516 times.
✓ Branch 1 taken 380760 times.
✓ Branch 2 taken 465 times.
✓ Branch 3 taken 357051 times.
738276 if (*got_output || ret<0)
2157 381225 decode_error_stat[ret<0] ++;
2158
2159
3/4
✓ Branch 0 taken 465 times.
✓ Branch 1 taken 737811 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 465 times.
738276 if (ret < 0 && exit_on_error)
2160 exit_program(1);
2161
2162
4/4
✓ Branch 0 taken 380760 times.
✓ Branch 1 taken 357516 times.
✓ Branch 2 taken 379986 times.
✓ Branch 3 taken 774 times.
738276 if (*got_output && ist) {
2163
3/4
✓ Branch 0 taken 379950 times.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 379950 times.
379986 if (ist->decoded_frame->decode_error_flags || (ist->decoded_frame->flags & AV_FRAME_FLAG_CORRUPT)) {
2164 36 av_log(NULL, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
2165
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);
2166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (exit_on_error)
2167 exit_program(1);
2168 }
2169 }
2170 738276 }
2171
2172 // Filters can be configured only if the formats of all inputs are known.
2173 14176 static int ifilter_has_all_input_formats(FilterGraph *fg)
2174 {
2175 int i;
2176
2/2
✓ Branch 0 taken 14221 times.
✓ Branch 1 taken 5879 times.
20100 for (i = 0; i < fg->nb_inputs; i++) {
2177
4/4
✓ Branch 0 taken 8303 times.
✓ Branch 1 taken 5918 times.
✓ Branch 2 taken 8278 times.
✓ Branch 3 taken 25 times.
14221 if (fg->inputs[i]->format < 0 && (fg->inputs[i]->type == AVMEDIA_TYPE_AUDIO ||
2178
2/2
✓ Branch 0 taken 8272 times.
✓ Branch 1 taken 6 times.
8278 fg->inputs[i]->type == AVMEDIA_TYPE_VIDEO))
2179 8297 return 0;
2180 }
2181 5879 return 1;
2182 }
2183
2184 380388 static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame)
2185 {
2186 380388 FilterGraph *fg = ifilter->graph;
2187 AVFrameSideData *sd;
2188 int need_reinit, ret, i;
2189
2190 /* determine if the parameters for this input changed */
2191 380388 need_reinit = ifilter->format != frame->format;
2192
2193
2/3
✓ Branch 0 taken 281080 times.
✓ Branch 1 taken 99308 times.
✗ Branch 2 not taken.
380388 switch (ifilter->ist->st->codecpar->codec_type) {
2194 281080 case AVMEDIA_TYPE_AUDIO:
2195 842107 need_reinit |= ifilter->sample_rate != frame->sample_rate ||
2196
4/4
✓ Branch 0 taken 279947 times.
✓ Branch 1 taken 1133 times.
✓ Branch 2 taken 279946 times.
✓ Branch 3 taken 1 times.
561026 ifilter->channels != frame->channels ||
2197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 279946 times.
279946 ifilter->channel_layout != frame->channel_layout;
2198 281080 break;
2199 99308 case AVMEDIA_TYPE_VIDEO:
2200
2/2
✓ Branch 0 taken 94624 times.
✓ Branch 1 taken 4684 times.
193932 need_reinit |= ifilter->width != frame->width ||
2201
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 94613 times.
94624 ifilter->height != frame->height;
2202 99308 break;
2203 }
2204
2205
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 380388 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
380388 if (!ifilter->ist->reinit_filters && fg->graph)
2206 need_reinit = 0;
2207
2208
1/2
✓ Branch 0 taken 380388 times.
✗ Branch 1 not taken.
380388 if (!!ifilter->hw_frames_ctx != !!frame->hw_frames_ctx ||
2209
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 380388 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
380388 (ifilter->hw_frames_ctx && ifilter->hw_frames_ctx->data != frame->hw_frames_ctx->data))
2210 need_reinit = 1;
2211
2212
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 380388 times.
380388 if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX)) {
2213 if (!ifilter->displaymatrix || memcmp(sd->data, ifilter->displaymatrix, sizeof(int32_t) * 9))
2214 need_reinit = 1;
2215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 380388 times.
380388 } else if (ifilter->displaymatrix)
2216 need_reinit = 1;
2217
2218
2/2
✓ Branch 0 taken 5831 times.
✓ Branch 1 taken 374557 times.
380388 if (need_reinit) {
2219 5831 ret = ifilter_parameters_from_frame(ifilter, frame);
2220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5831 times.
5831 if (ret < 0)
2221 return ret;
2222 }
2223
2224 /* (re)init the graph if possible, otherwise buffer the frame and return */
2225
3/4
✓ Branch 0 taken 374557 times.
✓ Branch 1 taken 5831 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 374557 times.
380388 if (need_reinit || !fg->graph) {
2226
2/2
✓ Branch 0 taken 5852 times.
✓ Branch 1 taken 5812 times.
11664 for (i = 0; i < fg->nb_inputs; i++) {
2227
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 5833 times.
5852 if (!ifilter_has_all_input_formats(fg)) {
2228 19 AVFrame *tmp = av_frame_clone(frame);
2229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!tmp)
2230 return AVERROR(ENOMEM);
2231 19 av_frame_unref(frame);
2232
2233
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (!av_fifo_space(ifilter->frame_queue)) {
2234 ret = av_fifo_realloc2(ifilter->frame_queue, 2 * av_fifo_size(ifilter->frame_queue));
2235 if (ret < 0) {
2236 av_frame_free(&tmp);
2237 return ret;
2238 }
2239 }
2240 19 av_fifo_generic_write(ifilter->frame_queue, &tmp, sizeof(tmp), NULL);
2241 19 return 0;
2242 }
2243 }
2244
2245 5812 ret = reap_filters(1);
2246
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5812 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5812 if (ret < 0 && ret != AVERROR_EOF) {
2247 av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
2248 return ret;
2249 }
2250
2251 5812 ret = configure_filtergraph(fg);
2252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5812 times.
5812 if (ret < 0) {
2253 av_log(NULL, AV_LOG_ERROR, "Error reinitializing filters!\n");
2254 return ret;
2255 }
2256 }
2257
2258 380369 ret = av_buffersrc_add_frame_flags(ifilter->filter, frame, AV_BUFFERSRC_FLAG_PUSH);
2259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 380369 times.
380369 if (ret < 0) {
2260 if (ret != AVERROR_EOF)
2261 av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
2262 return ret;
2263 }
2264
2265 380369 return 0;
2266 }
2267
2268 5786 static int ifilter_send_eof(InputFilter *ifilter, int64_t pts)
2269 {
2270 int ret;
2271
2272 5786 ifilter->eof = 1;
2273
2274
2/2
✓ Branch 0 taken 5785 times.
✓ Branch 1 taken 1 times.
5786 if (ifilter->filter) {
2275 5785 ret = av_buffersrc_close(ifilter->filter, pts, AV_BUFFERSRC_FLAG_PUSH);
2276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5785 times.
5785 if (ret < 0)
2277 return ret;
2278 } else {
2279 // the filtergraph was never configured
2280
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ifilter->format < 0)
2281 1 ifilter_parameters_from_codecpar(ifilter, ifilter->ist->st->codecpar);
2282
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)) {
2283 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);
2284 return AVERROR_INVALIDDATA;
2285 }
2286 }
2287
2288 5786 return 0;
2289 }
2290
2291 // This does not quite work like avcodec_decode_audio4/avcodec_decode_video2.
2292 // There is the following difference: if you got a frame, you must call
2293 // it again with pkt=NULL. pkt==NULL is treated differently from pkt->size==0
2294 // (pkt==NULL means get more output, pkt->size==0 is a flush/drain packet)
2295 742668 static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
2296 {
2297 int ret;
2298
2299 742668 *got_frame = 0;
2300
2301
2/2
✓ Branch 0 taken 363360 times.
✓ Branch 1 taken 379308 times.
742668 if (pkt) {
2302 363360 ret = avcodec_send_packet(avctx, pkt);
2303 // In particular, we don't expect AVERROR(EAGAIN), because we read all
2304 // decoded frames with avcodec_receive_frame() until done.
2305
4/4
✓ Branch 0 taken 1047 times.
✓ Branch 1 taken 362313 times.
✓ Branch 2 taken 381 times.
✓ Branch 3 taken 666 times.
363360 if (ret < 0 && ret != AVERROR_EOF)
2306 381 return ret;
2307 }
2308
2309 742287 ret = avcodec_receive_frame(avctx, frame);
2310
4/4
✓ Branch 0 taken 362301 times.
✓ Branch 1 taken 379986 times.
✓ Branch 2 taken 5783 times.
✓ Branch 3 taken 356518 times.
742287 if (ret < 0 && ret != AVERROR(EAGAIN))
2311 5783 return ret;
2312
2/2
✓ Branch 0 taken 379986 times.
✓ Branch 1 taken 356518 times.
736504 if (ret >= 0)
2313 379986 *got_frame = 1;
2314
2315 736504 return 0;
2316 }
2317
2318 379986 static int send_frame_to_filters(InputStream *ist, AVFrame *decoded_frame)
2319 {
2320 int i, ret;
2321 AVFrame *f;
2322
2323 av_assert1(ist->nb_filters > 0); /* ensure ret is initialized */
2324
2/2
✓ Branch 0 taken 380388 times.
✓ Branch 1 taken 379986 times.
760374 for (i = 0; i < ist->nb_filters; i++) {
2325
2/2
✓ Branch 0 taken 402 times.
✓ Branch 1 taken 379986 times.
380388 if (i < ist->nb_filters - 1) {
2326 402 f = ist->filter_frame;
2327 402 ret = av_frame_ref(f, decoded_frame);
2328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 402 times.
402 if (ret < 0)
2329 break;
2330 } else
2331 379986 f = decoded_frame;
2332 380388 ret = ifilter_send_frame(ist->filters[i], f);
2333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 380388 times.
380388 if (ret == AVERROR_EOF)
2334 ret = 0; /* ignore */
2335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 380388 times.
380388 if (ret < 0) {
2336 av_log(NULL, AV_LOG_ERROR,
2337 "Failed to inject frame into filter network: %s\n", av_err2str(ret));
2338 break;
2339 }
2340 }
2341 379986 return ret;
2342 }
2343
2344 535272 static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output,
2345 int *decode_failed)
2346 {
2347 AVFrame *decoded_frame;
2348 535272 AVCodecContext *avctx = ist->dec_ctx;
2349 535272 int ret, err = 0;
2350 AVRational decoded_frame_tb;
2351
2352
3/4
✓ Branch 0 taken 1132 times.
✓ Branch 1 taken 534140 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1132 times.
535272 if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
2353 return AVERROR(ENOMEM);
2354
3/4
✓ Branch 0 taken 1132 times.
✓ Branch 1 taken 534140 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1132 times.
535272 if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
2355 return AVERROR(ENOMEM);
2356 535272 decoded_frame = ist->decoded_frame;
2357
2358 535272 update_benchmark(NULL);
2359 535272 ret = decode(avctx, decoded_frame, got_output, pkt);
2360 535272 update_benchmark("decode_audio %d.%d", ist->file_index, ist->st->index);
2361
2/2
✓ Branch 0 taken 1142 times.
✓ Branch 1 taken 534130 times.
535272 if (ret < 0)
2362 1142 *decode_failed = 1;
2363
2364
3/4
✓ Branch 0 taken 534130 times.
✓ Branch 1 taken 1142 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 534130 times.
535272 if (ret >= 0 && avctx->sample_rate <= 0) {
2365 av_log(avctx, AV_LOG_ERROR, "Sample rate %d invalid\n", avctx->sample_rate);
2366 ret = AVERROR_INVALIDDATA;
2367 }
2368
2369
2/2
✓ Branch 0 taken 534141 times.
✓ Branch 1 taken 1131 times.
535272 if (ret != AVERROR_EOF)
2370 534141 check_decode_result(ist, got_output, ret);
2371
2372
3/4
✓ Branch 0 taken 280680 times.
✓ Branch 1 taken 254592 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 280680 times.
535272 if (!*got_output || ret < 0)
2373 254592 return ret;
2374
2375 280680 ist->samples_decoded += decoded_frame->nb_samples;
2376 280680 ist->frames_decoded++;
2377
2378 /* increment next_dts to use for the case where the input stream does not
2379 have timestamps or there are multiple frames in the packet */
2380 280680 ist->next_pts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
2381 280680 avctx->sample_rate;
2382 280680 ist->next_dts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
2383 280680 avctx->sample_rate;
2384
2385
2/2
✓ Branch 0 taken 252173 times.
✓ Branch 1 taken 28507 times.
280680 if (decoded_frame->pts != AV_NOPTS_VALUE) {
2386 252173 decoded_frame_tb = ist->st->time_base;
2387
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) {
2388 3 decoded_frame->pts = pkt->pts;
2389 3 decoded_frame_tb = ist->st->time_base;
2390 }else {
2391 28504 decoded_frame->pts = ist->dts;
2392 28504 decoded_frame_tb = AV_TIME_BASE_Q;
2393 }
2394
1/2
✓ Branch 0 taken 280680 times.
✗ Branch 1 not taken.
280680 if (decoded_frame->pts != AV_NOPTS_VALUE)
2395 280680 decoded_frame->pts = av_rescale_delta(decoded_frame_tb, decoded_frame->pts,
2396 280680 (AVRational){1, avctx->sample_rate}, decoded_frame->nb_samples, &ist->filter_in_rescale_delta_last,
2397 280680 (AVRational){1, avctx->sample_rate});
2398 280680 ist->nb_samples = decoded_frame->nb_samples;
2399 280680 err = send_frame_to_filters(ist, decoded_frame);
2400
2401 280680 av_frame_unref(ist->filter_frame);
2402 280680 av_frame_unref(decoded_frame);
2403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 280680 times.
280680 return err < 0 ? err : ret;
2404 }
2405
2406 208869 static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output, int64_t *duration_pts, int eof,
2407 int *decode_failed)
2408 {
2409 AVFrame *decoded_frame;
2410 208869 int i, ret = 0, err = 0;
2411 int64_t best_effort_timestamp;
2412 208869 int64_t dts = AV_NOPTS_VALUE;
2413
2414 // With fate-indeo3-2, we're getting 0-sized packets before EOF for some
2415 // reason. This seems like a semi-critical bug. Don't trigger EOF, and
2416 // skip the packet.
2417
6/6
✓ Branch 0 taken 203574 times.
✓ Branch 1 taken 5295 times.
✓ Branch 2 taken 104916 times.
✓ Branch 3 taken 98658 times.
✓ Branch 4 taken 1473 times.
✓ Branch 5 taken 103443 times.
208869 if (!eof && pkt && pkt->size == 0)
2418 1473 return 0;
2419
2420
3/4
✓ Branch 0 taken 4659 times.
✓ Branch 1 taken 202737 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4659 times.
207396 if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
2421 return AVERROR(ENOMEM);
2422
3/4
✓ Branch 0 taken 4659 times.
✓ Branch 1 taken 202737 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4659 times.
207396 if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
2423 return AVERROR(ENOMEM);
2424 207396 decoded_frame = ist->decoded_frame;
2425
2/2
✓ Branch 0 taken 206897 times.
✓ Branch 1 taken 499 times.
207396 if (ist->dts != AV_NOPTS_VALUE)
2426 206897 dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ist->st->time_base);
2427
2/2
✓ Branch 0 taken 108738 times.
✓ Branch 1 taken 98658 times.
207396 if (pkt) {
2428 108738 pkt->dts = dts; // ffmpeg.c probably shouldn't do this
2429 }
2430
2431 // The old code used to set dts on the drain packet, which does not work
2432 // with the new API anymore.
2433
2/2
✓ Branch 0 taken 5295 times.
✓ Branch 1 taken 202101 times.
207396 if (eof) {
2434 5295 void *new = av_realloc_array(ist->dts_buffer, ist->nb_dts_buffer + 1, sizeof(ist->dts_buffer[0]));
2435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5295 times.
5295 if (!new)
2436 return AVERROR(ENOMEM);
2437 5295 ist->dts_buffer = new;
2438 5295 ist->dts_buffer[ist->nb_dts_buffer++] = dts;
2439 }
2440
2441 207396 update_benchmark(NULL);
2442 207396 ret = decode(ist->dec_ctx, decoded_frame, got_output, pkt);
2443 207396 update_benchmark("decode_video %d.%d", ist->file_index, ist->st->index);
2444
2/2
✓ Branch 0 taken 5022 times.
✓ Branch 1 taken 202374 times.
207396 if (ret < 0)
2445 5022 *decode_failed = 1;
2446
2447 // The following line may be required in some cases where there is no parser
2448 // or the parser does not has_b_frames correctly
2449
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 207395 times.
207396 if (ist->st->codecpar->video_delay < ist->dec_ctx->has_b_frames) {
2450
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ist->dec_ctx->codec_id == AV_CODEC_ID_H264) {
2451 1 ist->st->codecpar->video_delay = ist->dec_ctx->has_b_frames;
2452 } else
2453 av_log(ist->dec_ctx, AV_LOG_WARNING,
2454 "video_delay is larger in decoder than demuxer %d > %d.\n"
2455 "If you want to help, upload a sample "
2456 "of this file to https://streams.videolan.org/upload/ "
2457 "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n",
2458 ist->dec_ctx->has_b_frames,
2459 ist->st->codecpar->video_delay);
2460 }
2461
2462
2/2
✓ Branch 0 taken 202749 times.
✓ Branch 1 taken 4647 times.
207396 if (ret != AVERROR_EOF)
2463 202749 check_decode_result(ist, got_output, ret);
2464
2465
3/4
✓ Branch 0 taken 99306 times.
✓ Branch 1 taken 108090 times.
✓ Branch 2 taken 99306 times.
✗ Branch 3 not taken.
207396 if (*got_output && ret >= 0) {
2466
2/2
✓ Branch 0 taken 99297 times.
✓ Branch 1 taken 9 times.
99306 if (ist->dec_ctx->width != decoded_frame->width ||
2467
2/2
✓ Branch 0 taken 99295 times.
✓ Branch 1 taken 2 times.
99297 ist->dec_ctx->height != decoded_frame->height ||
2468
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 99291 times.
99295 ist->dec_ctx->pix_fmt != decoded_frame->format) {
2469 15 av_log(NULL, AV_LOG_DEBUG, "Frame parameters mismatch context %d,%d,%d != %d,%d,%d\n",
2470 decoded_frame->width,
2471 decoded_frame->height,
2472 decoded_frame->format,
2473 15 ist->dec_ctx->width,
2474 15 ist->dec_ctx->height,
2475 15 ist->dec_ctx->pix_fmt);
2476 }
2477 }
2478
2479
3/4
✓ Branch 0 taken 99306 times.
✓ Branch 1 taken 108090 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 99306 times.
207396 if (!*got_output || ret < 0)
2480 108090 return ret;
2481
2482
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99306 times.
99306 if(ist->top_field_first>=0)
2483 decoded_frame->top_field_first = ist->top_field_first;
2484
2485 99306 ist->frames_decoded++;
2486
2487
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 99306 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
99306 if (ist->hwaccel_retrieve_data && decoded_frame->format == ist->hwaccel_pix_fmt) {
2488 err = ist->hwaccel_retrieve_data(ist->dec_ctx, decoded_frame);
2489 if (err < 0)
2490 goto fail;
2491 }
2492 99306 ist->hwaccel_retrieved_pix_fmt = decoded_frame->format;
2493
2494 99306 best_effort_timestamp= decoded_frame->best_effort_timestamp;
2495 99306 *duration_pts = decoded_frame->pkt_duration;
2496
2497
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 99201 times.
99306 if (ist->framerate.num)
2498 105 best_effort_timestamp = ist->cfr_next_pts++;
2499
2500
5/6
✓ Branch 0 taken 648 times.
✓ Branch 1 taken 98658 times.
✓ Branch 2 taken 528 times.
✓ Branch 3 taken 120 times.
✓ Branch 4 taken 528 times.
✗ Branch 5 not taken.
99306 if (eof && best_effort_timestamp == AV_NOPTS_VALUE && ist->nb_dts_buffer > 0) {
2501 528 best_effort_timestamp = ist->dts_buffer[0];
2502
2503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 528 times.
528 for (i = 0; i < ist->nb_dts_buffer - 1; i++)
2504 ist->dts_buffer[i] = ist->dts_buffer[i + 1];
2505 528 ist->nb_dts_buffer--;
2506 }
2507
2508
1/2
✓ Branch 0 taken 99306 times.
✗ Branch 1 not taken.
99306 if(best_effort_timestamp != AV_NOPTS_VALUE) {
2509 99306 int64_t ts = av_rescale_q(decoded_frame->pts = best_effort_timestamp, ist->st->time_base, AV_TIME_BASE_Q);
2510
2511
1/2
✓ Branch 0 taken 99306 times.
✗ Branch 1 not taken.
99306 if (ts != AV_NOPTS_VALUE)
2512 99306 ist->next_pts = ist->pts = ts;
2513 }
2514
2515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99306 times.
99306 if (debug_ts) {
2516 av_log(NULL, AV_LOG_INFO, "decoder -> ist_index:%d type:video "
2517 "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",
2518 ist->st->index, av_ts2str(decoded_frame->pts),
2519 av_ts2timestr(decoded_frame->pts, &ist->st->time_base),
2520 best_effort_timestamp,
2521 av_ts2timestr(best_effort_timestamp, &ist->st->time_base),
2522 decoded_frame->key_frame, decoded_frame->pict_type,
2523 ist->st->time_base.num, ist->st->time_base.den);
2524 }
2525
2526
2/2
✓ Branch 0 taken 5010 times.
✓ Branch 1 taken 94296 times.
99306 if (ist->st->sample_aspect_ratio.num)
2527 5010 decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
2528
2529 99306 err = send_frame_to_filters(ist, decoded_frame);
2530
2531 99306 fail:
2532 99306 av_frame_unref(ist->filter_frame);
2533 99306 av_frame_unref(decoded_frame);
2534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99306 times.
99306 return err < 0 ? err : ret;
2535 }
2536
2537 1386 static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
2538 int *decode_failed)
2539 {
2540 AVSubtitle subtitle;
2541 1386 int free_sub = 1;
2542 1386 int i, ret = avcodec_decode_subtitle2(ist->dec_ctx,
2543 &subtitle, got_output, pkt);
2544
2545 1386 check_decode_result(NULL, got_output, ret);
2546
2547
4/4
✓ Branch 0 taken 1307 times.
✓ Branch 1 taken 79 times.
✓ Branch 2 taken 533 times.
✓ Branch 3 taken 774 times.
1386 if (ret < 0 || !*got_output) {
2548 612 *decode_failed = 1;
2549
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 575 times.
612 if (!pkt->size)
2550 37 sub2video_flush(ist);
2551 612 return ret;
2552 }
2553
2554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 774 times.
774 if (ist->fix_sub_duration) {
2555 int end = 1;
2556 if (ist->prev_sub.got_output) {
2557 end = av_rescale(subtitle.pts - ist->prev_sub.subtitle.pts,
2558 1000, AV_TIME_BASE);
2559 if (end < ist->prev_sub.subtitle.end_display_time) {
2560 av_log(ist->dec_ctx, AV_LOG_DEBUG,
2561 "Subtitle duration reduced from %"PRId32" to %d%s\n",
2562 ist->prev_sub.subtitle.end_display_time, end,
2563 end <= 0 ? ", dropping it" : "");
2564 ist->prev_sub.subtitle.end_display_time = end;
2565 }
2566 }
2567 FFSWAP(int, *got_output, ist->prev_sub.got_output);
2568 FFSWAP(int, ret, ist->prev_sub.ret);
2569 FFSWAP(AVSubtitle, subtitle, ist->prev_sub.subtitle);
2570 if (end <= 0)
2571 goto out;
2572 }
2573
2574
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 774 times.
774 if (!*got_output)
2575 return ret;
2576
2577
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 686 times.
774 if (ist->sub2video.frame) {
2578 88 sub2video_update(ist, INT64_MIN, &subtitle);
2579
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 686 times.
686 } else if (ist->nb_filters) {
2580 if (!ist->sub2video.sub_queue)
2581 ist->sub2video.sub_queue = av_fifo_alloc(8 * sizeof(AVSubtitle));
2582 if (!ist->sub2video.sub_queue)
2583 exit_program(1);
2584 if (!av_fifo_space(ist->sub2video.sub_queue)) {
2585 ret = av_fifo_realloc2(ist->sub2video.sub_queue, 2 * av_fifo_size(ist->sub2video.sub_queue));
2586 if (ret < 0)
2587 exit_program(1);
2588 }
2589 av_fifo_generic_write(ist->sub2video.sub_queue, &subtitle, sizeof(subtitle), NULL);
2590 free_sub = 0;
2591 }
2592
2593
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 765 times.
774 if (!subtitle.num_rects)
2594 9 goto out;
2595
2596 765 ist->frames_decoded++;
2597
2598
2/2
✓ Branch 0 taken 809 times.
✓ Branch 1 taken 765 times.
1574 for (i = 0; i < nb_output_streams; i++) {
2599 809 OutputStream *ost = output_streams[i];
2600
2601
3/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 783 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
809 if (!ost->pkt && !(ost->pkt = av_packet_alloc()))
2602 exit_program(1);
2603
3/4
✓ Branch 1 taken 764 times.
✓ Branch 2 taken 45 times.
✓ Branch 3 taken 764 times.
✗ Branch 4 not taken.
809 if (!check_output_constraints(ist, ost) || !ost->encoding_needed
2604
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 720 times.
764 || ost->enc->type != AVMEDIA_TYPE_SUBTITLE)
2605 89 continue;
2606
2607 720 do_subtitle_out(output_files[ost->file_index], ost, &subtitle);
2608 }
2609
2610 765 out:
2611
1/2
✓ Branch 0 taken 774 times.
✗ Branch 1 not taken.
774 if (free_sub)
2612 774 avsubtitle_free(&subtitle);
2613 774 return ret;
2614 }
2615
2616 5815 static int send_filter_eof(InputStream *ist)
2617 {
2618 int i, ret;
2619 /* TODO keep pts also in stream time base to avoid converting back */
2620 5815 int64_t pts = av_rescale_q_rnd(ist->pts, AV_TIME_BASE_Q, ist->st->time_base,
2621 AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
2622
2623
2/2
✓ Branch 0 taken 5786 times.
✓ Branch 1 taken 5815 times.
11601 for (i = 0; i < ist->nb_filters; i++) {
2624 5786 ret = ifilter_send_eof(ist->filters[i], pts);
2625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5786 times.
5786 if (ret < 0)
2626 return ret;
2627 }
2628 5815 return 0;
2629 }
2630
2631 /* pkt = NULL means EOF (needed to flush decoder buffers) */
2632 400879 static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
2633 {
2634 400879 int ret = 0, i;
2635 400879 int repeating = 0;
2636 400879 int eof_reached = 0;
2637
2638 AVPacket *avpkt;
2639
2640
3/4
✓ Branch 0 taken 6187 times.
✓ Branch 1 taken 394692 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6187 times.
400879 if (!ist->pkt && !(ist->pkt = av_packet_alloc()))
2641 return AVERROR(ENOMEM);
2642 400879 avpkt = ist->pkt;
2643
2644
2/2
✓ Branch 0 taken 6187 times.
✓ Branch 1 taken 394692 times.
400879 if (!ist->saw_first_ts) {
2645 6187 ist->first_dts =
2646
2/2
✓ Branch 0 taken 4199 times.
✓ Branch 1 taken 1988 times.
6187 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;
2647 6187 ist->pts = 0;
2648
6/6
✓ Branch 0 taken 6136 times.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 5638 times.
✓ Branch 3 taken 498 times.
✓ Branch 4 taken 241 times.
✓ Branch 5 taken 5397 times.
6187 if (pkt && pkt->pts != AV_NOPTS_VALUE && !ist->decoding_needed) {
2649 241 ist->first_dts =
2650 241 ist->dts += av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
2651 241 ist->pts = ist->dts; //unused but better to set it to a value thats not totally wrong
2652 }
2653 6187 ist->saw_first_ts = 1;
2654 }
2655
2656
2/2
✓ Branch 0 taken 6681 times.
✓ Branch 1 taken 394198 times.
400879 if (ist->next_dts == AV_NOPTS_VALUE)
2657 6681 ist->next_dts = ist->dts;
2658
2/2
✓ Branch 0 taken 6187 times.
✓ Branch 1 taken 394692 times.
400879 if (ist->next_pts == AV_NOPTS_VALUE)
2659 6187 ist->next_pts = ist->pts;
2660
2661
2/2
✓ Branch 0 taken 394315 times.
✓ Branch 1 taken 6564 times.
400879 if (pkt) {
2662 394315 av_packet_unref(avpkt);
2663 394315 ret = av_packet_ref(avpkt, pkt);
2664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 394315 times.
394315 if (ret < 0)
2665 return ret;
2666 }
2667
2668
4/4
✓ Branch 0 taken 394315 times.
✓ Branch 1 taken 6564 times.
✓ Branch 2 taken 357754 times.
✓ Branch 3 taken 36561 times.
400879 if (pkt && pkt->dts != AV_NOPTS_VALUE) {
2669 357754 ist->next_dts = ist->dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
2670
4/4
✓ Branch 0 taken 79213 times.
✓ Branch 1 taken 278541 times.
✓ Branch 2 taken 4497 times.
✓ Branch 3 taken 74716 times.
357754 if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_VIDEO || !ist->decoding_needed)
2671 283038 ist->next_pts = ist->pts = ist->dts;
2672 }
2673
2674 // while we have more to decode or while the decoder did output something on EOF
2675
2/2
✓ Branch 0 taken 746301 times.
✓ Branch 1 taken 34660 times.
780961 while (ist->decoding_needed) {
2676 746301 int64_t duration_dts = 0;
2677 746301 int64_t duration_pts = 0;
2678 746301 int got_output = 0;
2679 746301 int decode_failed = 0;
2680
2681 746301 ist->pts = ist->next_pts;
2682 746301 ist->dts = ist->next_dts;
2683
2684
3/4
✓ Branch 0 taken 535272 times.
✓ Branch 1 taken 208869 times.
✓ Branch 2 taken 2160 times.
✗ Branch 3 not taken.
746301 switch (ist->dec_ctx->codec_type) {
2685 535272 case AVMEDIA_TYPE_AUDIO:
2686
2/2
✓ Branch 0 taken 254622 times.
✓ Branch 1 taken 280650 times.
535272 ret = decode_audio (ist, repeating ? NULL : avpkt, &got_output,
2687 &decode_failed);
2688 535272 av_packet_unref(avpkt);
2689 535272 break;
2690 208869 case AVMEDIA_TYPE_VIDEO:
2691
2/2
✓ Branch 0 taken 110211 times.
✓ Branch 1 taken 98658 times.
208869 ret = decode_video (ist, repeating ? NULL : avpkt, &got_output, &duration_pts, !pkt,
2692 &decode_failed);
2693
5/6
✓ Branch 0 taken 98658 times.
✓ Branch 1 taken 110211 times.
✓ Branch 2 taken 98658 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 98648 times.
208869 if (!repeating || !pkt || got_output) {
2694
4/4
✓ Branch 0 taken 104926 times.
✓ Branch 1 taken 5295 times.
✓ Branch 2 taken 103227 times.
✓ Branch 3 taken 1699 times.
110221 if (pkt && pkt->duration) {
2695 103227 duration_dts = av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
2696
3/4
✓ Branch 0 taken 5900 times.
✓ Branch 1 taken 1094 times.
✓ Branch 2 taken 5900 times.
✗ Branch 3 not taken.
6994 } else if(ist->dec_ctx->framerate.num != 0 && ist->dec_ctx->framerate.den != 0) {
2697
2/2
✓ Branch 1 taken 2813 times.
✓ Branch 2 taken 3087 times.
5900 int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict+1 : ist->dec_ctx->ticks_per_frame;
2698 5900 duration_dts = ((int64_t)AV_TIME_BASE *
2699 5900 ist->dec_ctx->framerate.den * ticks) /
2700 5900 ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame;
2701 }
2702
2703
4/4
✓ Branch 0 taken 110206 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 109127 times.
✓ Branch 3 taken 1079 times.
110221 if(ist->dts != AV_NOPTS_VALUE && duration_dts) {
2704 109127 ist->next_dts += duration_dts;
2705 }else
2706 1094 ist->next_dts = AV_NOPTS_VALUE;
2707 }
2708
2709
2/2
✓ Branch 0 taken 99306 times.
✓ Branch 1 taken 109563 times.
208869 if (got_output) {
2710
2/2
✓ Branch 0 taken 97621 times.
✓ Branch 1 taken 1685 times.
99306 if (duration_pts > 0) {
2711 97621 ist->next_pts += av_rescale_q(duration_pts, ist->st->time_base, AV_TIME_BASE_Q);
2712 } else {
2713 1685 ist->next_pts += duration_dts;
2714 }
2715 }
2716 208869 av_packet_unref(avpkt);
2717 208869 break;
2718 2160 case AVMEDIA_TYPE_SUBTITLE:
2719
2/2
✓ Branch 0 taken 774 times.
✓ Branch 1 taken 1386 times.
2160 if (repeating)
2720 774 break;
2721 1386 ret = transcode_subtitles(ist, avpkt, &got_output, &decode_failed);
2722
3/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1349 times.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
1386 if (!pkt && ret >= 0)
2723 37 ret = AVERROR_EOF;
2724 1386 av_packet_unref(avpkt);
2725 1386 break;
2726 default:
2727 return -1;
2728 }
2729
2730
2/2
✓ Branch 0 taken 5815 times.
✓ Branch 1 taken 740486 times.
746301 if (ret == AVERROR_EOF) {
2731 5815 eof_reached = 1;
2732 366219 break;
2733 }
2734
2735
2/2
✓ Branch 0 taken 465 times.
✓ Branch 1 taken 740021 times.
740486 if (ret < 0) {
2736
1/2
✓ Branch 0 taken 465 times.
✗ Branch 1 not taken.
465 if (decode_failed) {
2737 930 av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d: %s\n",
2738 465 ist->file_index, ist->st->index, av_err2str(ret));
2739 } else {
2740 av_log(NULL, AV_LOG_FATAL, "Error while processing the decoded "
2741 "data for stream #%d:%d\n", ist->file_index, ist->st->index);
2742 }
2743
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)
2744 exit_program(1);
2745 465 break;
2746 }
2747
2748
2/2
✓ Branch 0 taken 380760 times.
✓ Branch 1 taken 359261 times.
740021 if (got_output)
2749 380760 ist->got_output = 1;
2750
2751
2/2
✓ Branch 0 taken 359261 times.
✓ Branch 1 taken 380760 times.
740021 if (!got_output)
2752 359261 break;
2753
2754 // During draining, we might get multiple output frames in this loop.
2755 // ffmpeg.c does not drain the filter chain on configuration changes,
2756 // which means if we send multiple frames at once to the filters, and
2757 // one of those frames changes configuration, the buffered frames will
2758 // be lost. This can upset certain FATE tests.
2759 // Decode only 1 frame per call on EOF to appease these FATE tests.
2760 // The ideal solution would be to rewrite decoding to use the new
2761 // decoding API in a better way.
2762
2/2
✓ Branch 0 taken 678 times.
✓ Branch 1 taken 380082 times.
380760 if (!pkt)
2763 678 break;
2764
2765 380082 repeating = 1;
2766 }
2767
2768 /* after flushing, send an EOF on all the filter inputs attached to the stream */
2769 /* except when looping we need to flush but not to send an EOF */
2770
7/8
✓ Branch 0 taken 6564 times.
✓ Branch 1 taken 394315 times.
✓ Branch 2 taken 6494 times.
✓ Branch 3 taken 70 times.
✓ Branch 4 taken 5815 times.
✓ Branch 5 taken 679 times.
✓ Branch 6 taken 5815 times.
✗ Branch 7 not taken.
400879 if (!pkt && ist->decoding_needed && eof_reached && !no_eof) {
2771 5815 int ret = send_filter_eof(ist);
2772
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5815 times.
5815 if (ret < 0) {
2773 av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\n");
2774 exit_program(1);
2775 }
2776 }
2777
2778 /* handle stream copy */
2779
4/4
✓ Branch 0 taken 34660 times.
✓ Branch 1 taken 366219 times.
✓ Branch 2 taken 34590 times.
✓ Branch 3 taken 70 times.
400879 if (!ist->decoding_needed && pkt) {
2780 34590 ist->dts = ist->next_dts;
2781
3/3
✓ Branch 0 taken 24295 times.
✓ Branch 1 taken 9713 times.
✓ Branch 2 taken 582 times.
34590 switch (ist->dec_ctx->codec_type) {
2782 24295 case AVMEDIA_TYPE_AUDIO:
2783 av_assert1(pkt->duration >= 0);
2784
1/2
✓ Branch 0 taken 24295 times.
✗ Branch 1 not taken.
24295 if (ist->dec_ctx->sample_rate) {
2785 24295 ist->next_dts += ((int64_t)AV_TIME_BASE * ist->dec_ctx->frame_size) /
2786 24295 ist->dec_ctx->sample_rate;
2787 } else {
2788 ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
2789 }
2790 24295 break;
2791 9713 case AVMEDIA_TYPE_VIDEO:
2792
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 9634 times.
9713 if (ist->framerate.num) {
2793 // TODO: Remove work-around for c99-to-c89 issue 7
2794 79 AVRational time_base_q = AV_TIME_BASE_Q;
2795 79 int64_t next_dts = av_rescale_q(ist->next_dts, time_base_q, av_inv_q(ist->framerate));
2796 79 ist->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
2797
2/2
✓ Branch 0 taken 9222 times.
✓ Branch 1 taken 412 times.
9634 } else if (pkt->duration) {
2798 9222 ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
2799
2/2
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 136 times.
412 } else if(ist->dec_ctx->framerate.num != 0) {
2800
2/2
✓ Branch 1 taken 94 times.
✓ Branch 2 taken 182 times.
276 int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame;
2801 276 ist->next_dts += ((int64_t)AV_TIME_BASE *
2802 276 ist->dec_ctx->framerate.den * ticks) /
2803 276 ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame;
2804 }
2805 9713 break;
2806 }
2807 34590 ist->pts = ist->dts;
2808 34590 ist->next_pts = ist->next_dts;
2809 }
2810
2/2
✓ Branch 0 taken 420379 times.
✓ Branch 1 taken 400879 times.
821258 for (i = 0; i < nb_output_streams; i++) {
2811 420379 OutputStream *ost = output_streams[i];
2812
2813
3/4
✓ Branch 0 taken 6104 times.
✓ Branch 1 taken 414275 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6104 times.
420379 if (!ost->pkt && !(ost->pkt = av_packet_alloc()))
2814 exit_program(1);
2815
4/4
✓ Branch 1 taken 393898 times.
✓ Branch 2 taken 26481 times.
✓ Branch 3 taken 360417 times.
✓ Branch 4 taken 33481 times.
420379 if (!check_output_constraints(ist, ost) || ost->encoding_needed)
2816 386898 continue;
2817
2818 33481 do_streamcopy(ist, ost, pkt);
2819 }
2820
2821 400879 return !eof_reached;
2822 }
2823
2824 static void print_sdp(void)
2825 {
2826 char sdp[16384];
2827 int i;
2828 int j;
2829 AVIOContext *sdp_pb;
2830 AVFormatContext **avc;
2831
2832 for (i = 0; i < nb_output_files; i++) {
2833 if (!output_files[i]->header_written)
2834 return;
2835 }
2836
2837 avc = av_malloc_array(nb_output_files, sizeof(*avc));
2838 if (!avc)
2839 exit_program(1);
2840 for (i = 0, j = 0; i < nb_output_files; i++) {
2841 if (!strcmp(output_files[i]->ctx->oformat->name, "rtp")) {
2842 avc[j] = output_files[i]->ctx;
2843 j++;
2844 }
2845 }
2846
2847 if (!j)
2848 goto fail;
2849
2850 av_sdp_create(avc, j, sdp, sizeof(sdp));
2851
2852 if (!sdp_filename) {
2853 printf("SDP:\n%s\n", sdp);
2854 fflush(stdout);
2855 } else {
2856 if (avio_open2(&sdp_pb, sdp_filename, AVIO_FLAG_WRITE, &int_cb, NULL) < 0) {
2857 av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename);
2858 } else {
2859 avio_print(sdp_pb, sdp);
2860 avio_closep(&sdp_pb);
2861 av_freep(&sdp_filename);
2862 }
2863 }
2864
2865 fail:
2866 av_freep(&avc);
2867 }
2868
2869 933 static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
2870 {
2871 933 InputStream *ist = s->opaque;
2872 const enum AVPixelFormat *p;
2873 int ret;
2874
2875
1/2
✓ Branch 0 taken 2556 times.
✗ Branch 1 not taken.
2556 for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
2876 2556 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
2877 2556 const AVCodecHWConfig *config = NULL;
2878 int i;
2879
2880
2/2
✓ Branch 0 taken 933 times.
✓ Branch 1 taken 1623 times.
2556 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
2881 933 break;
2882
2883
1/2
✓ Branch 0 taken 1623 times.
✗ Branch 1 not taken.
1623 if (ist->hwaccel_id == HWACCEL_GENERIC ||
2884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1623 times.
1623 ist->hwaccel_id == HWACCEL_AUTO) {
2885 for (i = 0;; i++) {
2886 config = avcodec_get_hw_config(s->codec, i);
2887 if (!config)
2888 break;
2889 if (!(config->methods &
2890 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
2891 continue;
2892 if (config->pix_fmt == *p)
2893 break;
2894 }
2895 }
2896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1623 times.
1623 if (config) {
2897 if (config->device_type != ist->hwaccel_device_type) {
2898 // Different hwaccel offered, ignore.
2899 continue;
2900 }
2901
2902 ret = hwaccel_decode_init(s);
2903 if (ret < 0) {
2904 if (ist->hwaccel_id == HWACCEL_GENERIC) {
2905 av_log(NULL, AV_LOG_FATAL,
2906 "%s hwaccel requested for input stream #%d:%d, "
2907 "but cannot be initialized.\n",
2908 av_hwdevice_get_type_name(config->device_type),
2909 ist->file_index, ist->st->index);
2910 return AV_PIX_FMT_NONE;
2911 }
2912 continue;
2913 }
2914 } else {
2915 1623 const HWAccel *hwaccel = NULL;
2916 int i;
2917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1623 times.
1623 for (i = 0; hwaccels[i].name; i++) {
2918 if (hwaccels[i].pix_fmt == *p) {
2919 hwaccel = &hwaccels[i];
2920 break;
2921 }
2922 }
2923
1/2
✓ Branch 0 taken 1623 times.
✗ Branch 1 not taken.
1623 if (!hwaccel) {
2924 // No hwaccel supporting this pixfmt.
2925 1623 continue;
2926 }
2927 if (hwaccel->id != ist->hwaccel_id) {
2928 // Does not match requested hwaccel.
2929 continue;
2930 }
2931
2932 ret = hwaccel->init(s);
2933 if (ret < 0) {
2934 av_log(NULL, AV_LOG_FATAL,
2935 "%s hwaccel requested for input stream #%d:%d, "
2936 "but cannot be initialized.\n", hwaccel->name,
2937 ist->file_index, ist->st->index);
2938 return AV_PIX_FMT_NONE;
2939 }
2940 }
2941
2942 if (ist->hw_frames_ctx) {
2943 s->hw_frames_ctx = av_buffer_ref(ist->hw_frames_ctx);
2944 if (!s->hw_frames_ctx)
2945 return AV_PIX_FMT_NONE;
2946 }
2947
2948 ist->hwaccel_pix_fmt = *p;
2949 break;
2950 }
2951
2952 933 return *p;
2953 }
2954
2955 354763 static int get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
2956 {
2957 354763 InputStream *ist = s->opaque;
2958
2959
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 354763 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
354763 if (ist->hwaccel_get_buffer && frame->format == ist->hwaccel_pix_fmt)
2960 return ist->hwaccel_get_buffer(s, frame, flags);
2961
2962 354763 return avcodec_default_get_buffer2(s, frame, flags);
2963 }
2964
2965 6408 static int init_input_stream(int ist_index, char *error, int error_len)
2966 {
2967 int ret;
2968 6408 InputStream *ist = input_streams[ist_index];
2969
2970
2/2
✓ Branch 0 taken 5828 times.
✓ Branch 1 taken 580 times.
6408 if (ist->decoding_needed) {
2971 5828 const AVCodec *codec = ist->dec;
2972
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5828 times.
5828 if (!codec) {
2973 snprintf(error, error_len, "Decoder (codec %s) not found for input stream #%d:%d",
2974 avcodec_get_name(ist->dec_ctx->codec_id), ist->file_index, ist->st->index);
2975 return AVERROR(EINVAL);
2976 }
2977
2978 5828 ist->dec_ctx->opaque = ist;
2979 5828 ist->dec_ctx->get_format = get_format;
2980 5828 ist->dec_ctx->get_buffer2 = get_buffer;
2981 #if LIBAVCODEC_VERSION_MAJOR < 60
2982 FF_DISABLE_DEPRECATION_WARNINGS
2983 5828 ist->dec_ctx->thread_safe_callbacks = 1;
2984 FF_ENABLE_DEPRECATION_WARNINGS
2985 #endif
2986
2987
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5827 times.
5828 if (ist->dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
2988
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (ist->decoding_needed & DECODING_FOR_OST)) {
2989 1 av_dict_set(&ist->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
2990
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ist->decoding_needed & DECODING_FOR_FILTER)
2991 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");
2992 }
2993
2994 /* Useful for subtitles retiming by lavf (FIXME), skipping samples in
2995 * audio, and video decoders such as cuvid or mediacodec */
2996 5828 ist->dec_ctx->pkt_timebase = ist->st->time_base;
2997
2998
2/2
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 5771 times.
5828 if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
2999 57 av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
3000 /* Attached pics are sparse, therefore we would not want to delay their decoding till EOF. */
3001
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5824 times.
5828 if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
3002 4 av_dict_set(&ist->decoder_opts, "threads", "1", 0);
3003
3004 5828 ret = hw_device_setup_for_decode(ist);
3005
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5828 times.
5828 if (ret < 0) {
3006 snprintf(error, error_len, "Device setup failed for "
3007 "decoder on input stream #%d:%d : %s",
3008 ist->file_index, ist->st->index, av_err2str(ret));
3009 return ret;
3010 }
3011
3012
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5828 times.
5828 if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
3013 if (ret == AVERROR_EXPERIMENTAL)
3014 abort_codec_experimental(codec, 0);
3015
3016 snprintf(error, error_len,
3017 "Error while opening decoder for input stream "
3018 "#%d:%d : %s",
3019 ist->file_index, ist->st->index, av_err2str(ret));
3020 return ret;
3021 }
3022 5828 assert_avoptions(ist->decoder_opts);
3023 }
3024
3025 6408 ist->next_pts = AV_NOPTS_VALUE;
3026 6408 ist->next_dts = AV_NOPTS_VALUE;
3027
3028 6408 return 0;
3029 }
3030
3031 17868 static InputStream *get_input_stream(OutputStream *ost)
3032 {
3033
2/2
✓ Branch 0 taken 17685 times.
✓ Branch 1 taken 183 times.
17868 if (ost->source_index >= 0)
3034 17685 return input_streams[ost->source_index];
3035 183 return NULL;
3036 }
3037
3038 1 static int compare_int64(const void *a, const void *b)
3039 {
3040 1 return FFDIFFSIGN(*(const int64_t *)a, *(const int64_t *)b);
3041 }
3042
3043 /* open the muxer when all the streams are initialized */
3044 6196 static int check_init_output_file(OutputFile *of, int file_index)
3045 {
3046 int ret, i;
3047
3048
2/2
✓ Branch 0 taken 6552 times.
✓ Branch 1 taken 6015 times.
12567 for (i = 0; i < of->ctx->nb_streams; i++) {
3049 6552 OutputStream *ost = output_streams[of->ost_index + i];
3050
2/2
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 6371 times.
6552 if (!ost->initialized)
3051 181 return 0;
3052 }
3053
3054 6015 of->ctx->interrupt_callback = int_cb;
3055
3056 6015 ret = avformat_write_header(of->ctx, &of->opts);
3057
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6015 times.
6015 if (ret < 0) {
3058 av_log(NULL, AV_LOG_ERROR,
3059 "Could not write header for output file #%d "
3060 "(incorrect codec parameters ?): %s\n",
3061 file_index, av_err2str(ret));
3062 return ret;
3063 }
3064 //assert_avoptions(of->opts);
3065 6015 of->header_written = 1;
3066
3067 6015 av_dump_format(of->ctx, file_index, of->ctx->url, 1);
3068 6015 nb_output_dumped++;
3069
3070
2/4
✓ Branch 0 taken 6015 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6015 times.
6015 if (sdp_filename || want_sdp)
3071 print_sdp();
3072
3073 /* flush the muxing queues */
3074
2/2
✓ Branch 0 taken 6195 times.
✓ Branch 1 taken 6015 times.
12210 for (i = 0; i < of->ctx->nb_streams; i++) {
3075 6195 OutputStream *ost = output_streams[of->ost_index + i];
3076
3077 /* try to improve muxing time_base (only possible if nothing has been written yet) */
3078
2/2
✓ Branch 1 taken 6102 times.
✓ Branch 2 taken 93 times.
6195 if (!av_fifo_size(ost->muxing_queue))
3079 6102 ost->mux_timebase = ost->st->time_base;
3080
3081
2/2
✓ Branch 1 taken 309 times.
✓ Branch 2 taken 6195 times.
6504 while (av_fifo_size(ost->muxing_queue)) {
3082 AVPacket *pkt;
3083 309 av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL);
3084 309 ost->muxing_queue_data_size -= pkt->size;
3085 309 write_packet(of, pkt, ost, 1);
3086 309 av_packet_free(&pkt);
3087 }
3088 }
3089
3090 6015 return 0;
3091 }
3092
3093 6195 static int init_output_bsfs(OutputStream *ost)
3094 {
3095 6195 AVBSFContext *ctx = ost->bsf_ctx;
3096 int ret;
3097
3098
2/2
✓ Branch 0 taken 6107 times.
✓ Branch 1 taken 88 times.
6195 if (!ctx)
3099 6107 return 0;
3100
3101 88 ret = avcodec_parameters_copy(ctx->par_in, ost->st->codecpar);
3102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 if (ret < 0)
3103 return ret;
3104
3105 88 ctx->time_base_in = ost->st->time_base;
3106
3107 88 ret = av_bsf_init(ctx);
3108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 if (ret < 0) {
3109 av_log(NULL, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n",
3110 ctx->filter->name);
3111 return ret;
3112 }
3113
3114 88 ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
3115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 if (ret < 0)
3116 return ret;
3117 88 ost->st->time_base = ctx->time_base_out;
3118
3119 88 return 0;
3120 }
3121
3122 340 static int init_output_stream_streamcopy(OutputStream *ost)
3123 {
3124 340 OutputFile *of = output_files[ost->file_index];
3125 340 InputStream *ist = get_input_stream(ost);
3126 340 AVCodecParameters *par_dst = ost->st->codecpar;
3127 340 AVCodecParameters *par_src = ost->ref_par;
3128 AVRational sar;
3129 int i, ret;
3130 340 uint32_t codec_tag = par_dst->codec_tag;
3131
3132
2/4
✓ Branch 0 taken 340 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 340 times.
340 av_assert0(ist && !ost->filter);
3133
3134 340 ret = avcodec_parameters_to_context(ost->enc_ctx, ist->st->codecpar);
3135
1/2
✓ Branch 0 taken 340 times.
✗ Branch 1 not taken.
340 if (ret >= 0)
3136 340 ret = av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
3137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 340 times.
340 if (ret < 0) {
3138 av_log(NULL, AV_LOG_FATAL,
3139 "Error setting up codec context options.\n");
3140 return ret;
3141 }
3142
3143 340 ret = avcodec_parameters_from_context(par_src, ost->enc_ctx);
3144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 340 times.
340 if (ret < 0) {
3145 av_log(NULL, AV_LOG_FATAL,
3146 "Error getting reference codec parameters.\n");
3147 return ret;
3148 }
3149
3150
2/2
✓ Branch 0 taken 334 times.
✓ Branch 1 taken 6 times.
340 if (!codec_tag) {
3151 unsigned int codec_tag_tmp;
3152
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 274 times.
334 if (!of->ctx->oformat->codec_tag ||
3153
4/4
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 33 times.
100 av_codec_get_id (of->ctx->oformat->codec_tag, par_src->codec_tag) == par_src->codec_id ||
3154 40 !av_codec_get_tag2(of->ctx->oformat->codec_tag, par_src->codec_id, &codec_tag_tmp))
3155 301 codec_tag = par_src->codec_tag;
3156 }
3157
3158 340 ret = avcodec_parameters_copy(par_dst, par_src);
3159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 340 times.
340 if (ret < 0)
3160 return ret;
3161
3162 340 par_dst->codec_tag = codec_tag;
3163
3164
2/2
✓ Branch 0 taken 339 times.
✓ Branch 1 taken 1 times.
340 if (!ost->frame_rate.num)
3165 339 ost->frame_rate = ist->framerate;
3166
3167
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 338 times.
340 if (ost->frame_rate.num)
3168 2 ost->st->avg_frame_rate = ost->frame_rate;
3169 else
3170 338 ost->st->avg_frame_rate = ist->st->avg_frame_rate;
3171
3172 340 ret = avformat_transfer_internal_stream_timing_info(of->ctx->oformat, ost->st, ist->st, copy_tb);
3173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 340 times.
340 if (ret < 0)
3174 return ret;
3175
3176 // copy timebase while removing common factors
3177
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 339 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
340 if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) {
3178
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 338 times.
339 if (ost->frame_rate.num)
3179 1 ost->st->time_base = av_inv_q(ost->frame_rate);
3180 else
3181 338 ost->st->time_base = av_add_q(av_stream_get_codec_timebase(ost->st), (AVRational){0, 1});
3182 }
3183
3184 // copy estimated duration as a hint to the muxer
3185
3/4
✓ Branch 0 taken 340 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 201 times.
✓ Branch 3 taken 139 times.
340 if (ost->st->duration <= 0 && ist->st->duration > 0)
3186 201 ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
3187
3188 // copy disposition
3189 340 ost->st->disposition = ist->st->disposition;
3190
3191
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 310 times.
340 if (ist->st->nb_side_data) {
3192
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 30 times.
67 for (i = 0; i < ist->st->nb_side_data; i++) {
3193 37 const AVPacketSideData *sd_src = &ist->st->side_data[i];
3194 uint8_t *dst_data;
3195
3196 37 dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
3197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (!dst_data)
3198 return AVERROR(ENOMEM);
3199 37 memcpy(dst_data, sd_src->data, sd_src->size);
3200 }
3201 }
3202
3203
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 339 times.
340 if (ost->rotate_overridden) {
3204 1 uint8_t *sd = av_stream_new_side_data(ost->st, AV_PKT_DATA_DISPLAYMATRIX,
3205 sizeof(int32_t) * 9);
3206
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (sd)
3207 1 av_display_rotation_set((int32_t *)sd, -ost->rotate_override_value);
3208 }
3209
3210
3/3
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 191 times.
✓ Branch 2 taken 27 times.
340 switch (par_dst->codec_type) {
3211 122 case AVMEDIA_TYPE_AUDIO:
3212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 122 times.
122 if (audio_volume != 256) {
3213 av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
3214 exit_program(1);
3215 }
3216
5/8
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 119 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 119 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
122 if((par_dst->block_align == 1 || par_dst->block_align == 1152 || par_dst->block_align == 576) && par_dst->codec_id == AV_CODEC_ID_MP3)
3217 par_dst->block_align= 0;
3218
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 116 times.
122 if(par_dst->codec_id == AV_CODEC_ID_AC3)
3219 6 par_dst->block_align= 0;
3220 122 break;
3221 191 case AVMEDIA_TYPE_VIDEO:
3222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 191 times.
191 if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
3223 sar =
3224 av_mul_q(ost->frame_aspect_ratio,
3225 (AVRational){ par_dst->height, par_dst->width });
3226 av_log(NULL, AV_LOG_WARNING, "Overriding aspect ratio "
3227 "with stream copy may produce invalid files\n");
3228 }
3229
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 131 times.
191 else if (ist->st->sample_aspect_ratio.num)
3230 60 sar = ist->st->sample_aspect_ratio;
3231 else
3232 131 sar = par_src->sample_aspect_ratio;
3233 191 ost->st->sample_aspect_ratio = par_dst->sample_aspect_ratio = sar;
3234 191 ost->st->avg_frame_rate = ist->st->avg_frame_rate;
3235 191 ost->st->r_frame_rate = ist->st->r_frame_rate;
3236 191 break;
3237 }
3238
3239 340 ost->mux_timebase = ist->st->time_base;
3240
3241 340 return 0;
3242 }
3243
3244 5854 static void set_encoder_id(OutputFile *of, OutputStream *ost)
3245 {
3246 AVDictionaryEntry *e;
3247
3248 uint8_t *encoder_string;
3249 int encoder_string_len;
3250 5854 int format_flags = 0;
3251 5854 int codec_flags = ost->enc_ctx->flags;
3252
3253
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5854 times.
5854 if (av_dict_get(ost->st->metadata, "encoder", NULL, 0))
3254 return;
3255
3256 5854 e = av_dict_get(of->opts, "fflags", NULL, 0);
3257
2/2
✓ Branch 0 taken 3408 times.
✓ Branch 1 taken 2446 times.
5854 if (e) {
3258 3408 const AVOption *o = av_opt_find(of->ctx, "fflags", NULL, 0, 0);
3259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3408 times.
3408 if (!o)
3260 return;
3261 3408 av_opt_eval_flags(of->ctx, o, e->value, &format_flags);
3262 }
3263 5854 e = av_dict_get(ost->encoder_opts, "flags", NULL, 0);
3264
2/2
✓ Branch 0 taken 3407 times.
✓ Branch 1 taken 2447 times.
5854 if (e) {
3265 3407 const AVOption *o = av_opt_find(ost->enc_ctx, "flags", NULL, 0, 0);
3266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3407 times.
3407 if (!o)
3267 return;
3268 3407 av_opt_eval_flags(ost->enc_ctx, o, e->value, &codec_flags);
3269 }
3270
3271 5854 encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2;
3272 5854 encoder_string = av_mallocz(encoder_string_len);
3273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5854 times.
5854 if (!encoder_string)
3274 exit_program(1);
3275
3276
4/4
✓ Branch 0 taken 2446 times.
✓ Branch 1 taken 3408 times.
✓ Branch 2 taken 831 times.
✓ Branch 3 taken 1615 times.
5854 if (!(format_flags & AVFMT_FLAG_BITEXACT) && !(codec_flags & AV_CODEC_FLAG_BITEXACT))
3277 831 av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
3278 else
3279 5023 av_strlcpy(encoder_string, "Lavc ", encoder_string_len);
3280 5854 av_strlcat(encoder_string, ost->enc->name, encoder_string_len);
3281 5854 av_dict_set(&ost->st->metadata, "encoder", encoder_string,
3282 AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
3283 }
3284
3285 1 static void parse_forced_key_frames(char *kf, OutputStream *ost,
3286 AVCodecContext *avctx)
3287 {
3288 char *p;
3289 1 int n = 1, i, size, index = 0;
3290 int64_t t, *pts;
3291
3292
2/2