GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/fftools/ffmpeg.c Lines: 2104 2814 74.8 %
Date: 2021-04-20 04:37:23 Branches: 1551 2221 69.8 %

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
180
    ist->sub2video.frame->width  = ist->dec_ctx->width  ? ist->dec_ctx->width  : ist->sub2video.w;
184
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
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
89
    if (r->type != SUBTITLE_BITMAP) {
200
        av_log(NULL, AV_LOG_WARNING, "sub2video: non-bitmap subtitle\n");
201
        return;
202
    }
203


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
3380
    for (y = 0; y < r->h; y++) {
214
3291
        dst2 = (uint32_t *)dst;
215
3291
        src2 = src;
216
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
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

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
211
void sub2video_update(InputStream *ist, int64_t heartbeat_pts, AVSubtitle *sub)
242
{
243
211
    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
211
    if (!frame)
250
31
        return;
251
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
92
                    heartbeat_pts : ist->sub2video.end_pts;
264
92
        end_pts   = INT64_MAX;
265
92
        num_rects = 0;
266
    }
267
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
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
395125
static void sub2video_heartbeat(InputStream *ist, int64_t pts)
282
{
283
395125
    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
833910
    for (i = 0; i < infile->nb_streams; i++) {
292
438785
        InputStream *ist2 = input_streams[infile->ist_index + i];
293
438785
        if (!ist2->sub2video.frame)
294
437840
            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
945
        if (pts2 <= ist2->sub2video.last_pts)
300
8
            continue;
301

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
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
937
        if (nb_reqs)
309
249
            sub2video_push_ref(ist2, pts2);
310
    }
311
395125
}
312
313
35
static void sub2video_flush(InputStream *ist)
314
{
315
    int i;
316
    int ret;
317
318
35
    if (ist->sub2video.end_pts < INT64_MAX)
319
35
        sub2video_update(ist, INT64_MAX, NULL);
320
39
    for (i = 0; i < ist->nb_filters; i++) {
321
4
        ret = av_buffersrc_add_frame(ist->filters[i]->filter, NULL);
322

4
        if (ret != AVERROR_EOF && ret < 0)
323
            av_log(NULL, AV_LOG_WARNING, "Flush the frame error.\n");
324
    }
325
35
}
326
327
/* end of sub2video hack */
328
329
12020
static void term_exit_sigsafe(void)
330
{
331
#if HAVE_TERMIOS_H
332
12020
    if(restore_tty)
333
4
        tcsetattr (0, TCSANOW, &oldtty);
334
#endif
335
12020
}
336
337
12020
void term_exit(void)
338
{
339
12020
    av_log(NULL, AV_LOG_QUIET, "%s", "");
340
12020
    term_exit_sigsafe();
341
12020
}
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
6011
void term_init(void)
409
{
410
#if defined __linux__
411
6011
    struct sigaction action = {0};
412
6011
    action.sa_handler = sigterm_handler;
413
414
    /* block other interrupts while processing this one */
415
6011
    sigfillset(&action.sa_mask);
416
417
    /* restart interruptible functions (i.e. don't fail with EINTR)  */
418
6011
    action.sa_flags = SA_RESTART;
419
#endif
420
421
#if HAVE_TERMIOS_H
422

6011
    if (!run_as_daemon && stdin_interaction) {
423
        struct termios tty;
424
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
6011
    SIGNAL(SIGINT , sigterm_handler); /* Interrupt (ANSI).    */
444
6011
    SIGNAL(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
445
#ifdef SIGXCPU
446
6011
    SIGNAL(SIGXCPU, sigterm_handler);
447
#endif
448
#ifdef SIGPIPE
449
6011
    signal(SIGPIPE, SIG_IGN); /* Broken pipe (POSIX). */
450
#endif
451
#if HAVE_SETCONSOLECTRLHANDLER
452
    SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE);
453
#endif
454
6011
}
455
456
/* read a key without blocking */
457
171
static int read_key(void)
458
{
459
    unsigned char ch;
460
#if HAVE_TERMIOS_H
461
171
    int n = 1;
462
    struct timeval tv;
463
    fd_set rfds;
464
465
171
    FD_ZERO(&rfds);
466
171
    FD_SET(0, &rfds);
467
171
    tv.tv_sec = 0;
468
171
    tv.tv_usec = 0;
469
171
    n = select(1, &rfds, NULL, NULL, &tv);
470
171
    if (n > 0) {
471
154
        n = read(0, &ch, 1);
472
154
        if (n == 1)
473
            return ch;
474
475
154
        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
17
    return -1;
506
}
507
508
598718
static int decode_interrupt_cb(void *ctx)
509
{
510
598718
    return received_nb_signals > atomic_load(&transcode_init_done);
511
}
512
513
const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
514
515
6012
static void ffmpeg_cleanup(int ret)
516
{
517
    int i, j;
518
519
6012
    if (do_benchmark) {
520
        int maxrss = getmaxrss() / 1024;
521
        av_log(NULL, AV_LOG_INFO, "bench: maxrss=%ikB\n", maxrss);
522
    }
523
524
11823
    for (i = 0; i < nb_filtergraphs; i++) {
525
5811
        FilterGraph *fg = filtergraphs[i];
526
5811
        avfilter_graph_free(&fg->graph);
527
11602
        for (j = 0; j < fg->nb_inputs; j++) {
528
5791
            InputFilter *ifilter = fg->inputs[j];
529
5791
            struct InputStream *ist = ifilter->ist;
530
531
5791
            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
5791
            av_fifo_freep(&ifilter->frame_queue);
538
5791
            if (ist->sub2video.sub_queue) {
539
                while (av_fifo_size(ist->sub2video.sub_queue)) {
540
                    AVSubtitle sub;
541
                    av_fifo_generic_read(ist->sub2video.sub_queue,
542
                                         &sub, sizeof(sub), NULL);
543
                    avsubtitle_free(&sub);
544
                }
545
                av_fifo_freep(&ist->sub2video.sub_queue);
546
            }
547
5791
            av_buffer_unref(&ifilter->hw_frames_ctx);
548
5791
            av_freep(&ifilter->name);
549
5791
            av_freep(&fg->inputs[j]);
550
        }
551
5811
        av_freep(&fg->inputs);
552
11625
        for (j = 0; j < fg->nb_outputs; j++) {
553
5814
            OutputFilter *ofilter = fg->outputs[j];
554
555
5814
            avfilter_inout_free(&ofilter->out_tmp);
556
5814
            av_freep(&ofilter->name);
557
5814
            av_freep(&ofilter->formats);
558
5814
            av_freep(&ofilter->channel_layouts);
559
5814
            av_freep(&ofilter->sample_rates);
560
5814
            av_freep(&fg->outputs[j]);
561
        }
562
5811
        av_freep(&fg->outputs);
563
5811
        av_freep(&fg->graph_desc);
564
565
5811
        av_freep(&filtergraphs[i]);
566
    }
567
6012
    av_freep(&filtergraphs);
568
569
6012
    av_freep(&subtitle_out);
570
571
    /* close files */
572
12023
    for (i = 0; i < nb_output_files; i++) {
573
6011
        OutputFile *of = output_files[i];
574
        AVFormatContext *s;
575
6011
        if (!of)
576
            continue;
577
6011
        s = of->ctx;
578

6011
        if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE))
579
5957
            avio_closep(&s->pb);
580
6011
        avformat_free_context(s);
581
6011
        av_dict_free(&of->opts);
582
583
6011
        av_freep(&output_files[i]);
584
    }
585
12205
    for (i = 0; i < nb_output_streams; i++) {
586
6193
        OutputStream *ost = output_streams[i];
587
588
6193
        if (!ost)
589
            continue;
590
591
6193
        av_bsf_free(&ost->bsf_ctx);
592
593
6193
        av_frame_free(&ost->filtered_frame);
594
6193
        av_frame_free(&ost->last_frame);
595
6193
        av_packet_free(&ost->pkt);
596
6193
        av_dict_free(&ost->encoder_opts);
597
598
6193
        av_freep(&ost->forced_keyframes);
599
6193
        av_expr_free(ost->forced_keyframes_pexpr);
600
6193
        av_freep(&ost->avfilter);
601
6193
        av_freep(&ost->logfile_prefix);
602
603
6193
        av_freep(&ost->audio_channels_map);
604
6193
        ost->audio_channels_mapped = 0;
605
606
6193
        av_dict_free(&ost->sws_dict);
607
6193
        av_dict_free(&ost->swr_opts);
608
609
6193
        avcodec_free_context(&ost->enc_ctx);
610
6193
        avcodec_parameters_free(&ost->ref_par);
611
612
6193
        if (ost->muxing_queue) {
613
6193
            while (av_fifo_size(ost->muxing_queue)) {
614
                AVPacket *pkt;
615
                av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL);
616
                av_packet_free(&pkt);
617
            }
618
6193
            av_fifo_freep(&ost->muxing_queue);
619
        }
620
621
6193
        av_freep(&output_streams[i]);
622
    }
623
#if HAVE_THREADS
624
6012
    free_input_threads();
625
#endif
626
12050
    for (i = 0; i < nb_input_files; i++) {
627
6038
        avformat_close_input(&input_files[i]->ctx);
628
6038
        av_packet_free(&input_files[i]->pkt);
629
6038
        av_freep(&input_files[i]);
630
    }
631
12416
    for (i = 0; i < nb_input_streams; i++) {
632
6404
        InputStream *ist = input_streams[i];
633
634
6404
        av_frame_free(&ist->decoded_frame);
635
6404
        av_frame_free(&ist->filter_frame);
636
6404
        av_packet_free(&ist->pkt);
637
6404
        av_dict_free(&ist->decoder_opts);
638
6404
        avsubtitle_free(&ist->prev_sub.subtitle);
639
6404
        av_frame_free(&ist->sub2video.frame);
640
6404
        av_freep(&ist->filters);
641
6404
        av_freep(&ist->hwaccel_device);
642
6404
        av_freep(&ist->dts_buffer);
643
644
6404
        avcodec_free_context(&ist->dec_ctx);
645
646
6404
        av_freep(&input_streams[i]);
647
    }
648
649
6012
    if (vstats_file) {
650
        if (fclose(vstats_file))
651
            av_log(NULL, AV_LOG_ERROR,
652
                   "Error closing vstats file, loss of information possible: %s\n",
653
                   av_err2str(AVERROR(errno)));
654
    }
655
6012
    av_freep(&vstats_filename);
656
657
6012
    av_freep(&input_streams);
658
6012
    av_freep(&input_files);
659
6012
    av_freep(&output_streams);
660
6012
    av_freep(&output_files);
661
662
6012
    uninit_opts();
663
664
6012
    avformat_network_deinit();
665
666
6012
    if (received_sigterm) {
667
        av_log(NULL, AV_LOG_INFO, "Exiting normally, received signal %d.\n",
668
               (int) received_sigterm);
669

6012
    } else if (ret && atomic_load(&transcode_init_done)) {
670
2
        av_log(NULL, AV_LOG_INFO, "Conversion failed!\n");
671
    }
672
6012
    term_exit();
673
6012
    ffmpeg_exited = 1;
674
6012
}
675
676
6038
void remove_avoptions(AVDictionary **a, AVDictionary *b)
677
{
678
6038
    AVDictionaryEntry *t = NULL;
679
680
25125
    while ((t = av_dict_get(b, "", t, AV_DICT_IGNORE_SUFFIX))) {
681
19087
        av_dict_set(a, t->key, NULL, AV_DICT_MATCH_CASE);
682
    }
683
6038
}
684
685
17700
void assert_avoptions(AVDictionary *m)
686
{
687
    AVDictionaryEntry *t;
688
17700
    if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
689
        av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
690
        exit_program(1);
691
    }
692
17700
}
693
694
static void abort_codec_experimental(const AVCodec *c, int encoder)
695
{
696
    exit_program(1);
697
}
698
699
2389886
static void update_benchmark(const char *fmt, ...)
700
{
701
2389886
    if (do_benchmark_all) {
702
        BenchmarkTimeStamps t = get_benchmark_time_stamps();
703
        va_list va;
704
        char buf[1024];
705
706
        if (fmt) {
707
            va_start(va, fmt);
708
            vsnprintf(buf, sizeof(buf), fmt, va);
709
            va_end(va);
710
            av_log(NULL, AV_LOG_INFO,
711
                   "bench: %8" PRIu64 " user %8" PRIu64 " sys %8" PRIu64 " real %s \n",
712
                   t.user_usec - current_time.user_usec,
713
                   t.sys_usec - current_time.sys_usec,
714
                   t.real_usec - current_time.real_usec, buf);
715
        }
716
        current_time = t;
717
    }
718
2389886
}
719
720
static void close_all_output_streams(OutputStream *ost, OSTFinished this_stream, OSTFinished others)
721
{
722
    int i;
723
    for (i = 0; i < nb_output_streams; i++) {
724
        OutputStream *ost2 = output_streams[i];
725
        ost2->finished |= ost == ost2 ? this_stream : others;
726
    }
727
}
728
729
430229
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
730
{
731
430229
    AVFormatContext *s = of->ctx;
732
430229
    AVStream *st = ost->st;
733
    int ret;
734
735
    /*
736
     * Audio encoders may split the packets --  #frames in != #packets out.
737
     * But there is no reordering, so we can limit the number of output packets
738
     * by simply dropping them here.
739
     * Counting encoded video frames needs to be done separately because of
740
     * reordering, see do_video_out().
741
     * Do not count the packet when unqueued because it has been counted when queued.
742
     */
743

430229
    if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed) && !unqueue) {
744
330234
        if (ost->frame_number >= ost->max_frames) {
745
317
            av_packet_unref(pkt);
746
317
            return;
747
        }
748
329917
        ost->frame_number++;
749
    }
750
751
429912
    if (!of->header_written) {
752
        AVPacket *tmp_pkt;
753
        /* the muxer is not initialized yet, buffer the packet */
754
309
        if (!av_fifo_space(ost->muxing_queue)) {
755
            unsigned int are_we_over_size =
756
                (ost->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold;
757
            int new_size = are_we_over_size ?
758
                           FFMIN(2 * av_fifo_size(ost->muxing_queue),
759
                                 ost->max_muxing_queue_size) :
760
                           2 * av_fifo_size(ost->muxing_queue);
761
762
            if (new_size <= av_fifo_size(ost->muxing_queue)) {
763
                av_log(NULL, AV_LOG_ERROR,
764
                       "Too many packets buffered for output stream %d:%d.\n",
765
                       ost->file_index, ost->st->index);
766
                exit_program(1);
767
            }
768
            ret = av_fifo_realloc2(ost->muxing_queue, new_size);
769
            if (ret < 0)
770
                exit_program(1);
771
        }
772
309
        ret = av_packet_make_refcounted(pkt);
773
309
        if (ret < 0)
774
            exit_program(1);
775
309
        tmp_pkt = av_packet_alloc();
776
309
        if (!tmp_pkt)
777
            exit_program(1);
778
309
        av_packet_move_ref(tmp_pkt, pkt);
779
309
        ost->muxing_queue_data_size += tmp_pkt->size;
780
309
        av_fifo_generic_write(ost->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL);
781
309
        return;
782
    }
783
784

429603
    if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method == VSYNC_DROP) ||
785

429603
        (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 0))
786
        pkt->pts = pkt->dts = AV_NOPTS_VALUE;
787
788
429603
    if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
789
        int i;
790
110097
        uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
791
                                              NULL);
792
110097
        ost->quality = sd ? AV_RL32(sd) : -1;
793
110097
        ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
794
795
550485
        for (i = 0; i<FF_ARRAY_ELEMS(ost->error); i++) {
796

440388
            if (sd && i < sd[5])
797
                ost->error[i] = AV_RL64(sd + 8 + 8*i);
798
            else
799
440388
                ost->error[i] = -1;
800
        }
801
802

110097
        if (ost->frame_rate.num && ost->is_cfr) {
803
9819
            if (pkt->duration > 0)
804
                av_log(NULL, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n");
805
9819
            pkt->duration = av_rescale_q(1, av_inv_q(ost->frame_rate),
806
                                         ost->mux_timebase);
807
        }
808
    }
809
810
429603
    av_packet_rescale_ts(pkt, ost->mux_timebase, ost->st->time_base);
811
812
429603
    if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
813
188126
        if (pkt->dts != AV_NOPTS_VALUE &&
814
188126
            pkt->pts != AV_NOPTS_VALUE &&
815
187147
            pkt->dts > pkt->pts) {
816
            av_log(s, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64" in output stream %d:%d, replacing by guess\n",
817
                   pkt->dts, pkt->pts,
818
                   ost->file_index, ost->st->index);
819
            pkt->pts =
820
            pkt->dts = pkt->pts + pkt->dts + ost->last_mux_dts + 1
821
                     - FFMIN3(pkt->pts, pkt->dts, ost->last_mux_dts + 1)
822
                     - FFMAX3(pkt->pts, pkt->dts, ost->last_mux_dts + 1);
823
        }
824

188126
        if ((st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO || st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) &&
825
188113
            pkt->dts != AV_NOPTS_VALUE &&
826

188113
            !(st->codecpar->codec_id == AV_CODEC_ID_VP9 && ost->stream_copy) &&
827
187880
            ost->last_mux_dts != AV_NOPTS_VALUE) {
828
182988
            int64_t max = ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
829
182988
            if (pkt->dts < max) {
830

2
                int loglevel = max - pkt->dts > 2 || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
831
2
                if (exit_on_error)
832
                    loglevel = AV_LOG_ERROR;
833
2
                av_log(s, loglevel, "Non-monotonous DTS in output stream "
834
                       "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
835
2
                       ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts);
836
2
                if (exit_on_error) {
837
                    av_log(NULL, AV_LOG_FATAL, "aborting.\n");
838
                    exit_program(1);
839
                }
840
2
                av_log(s, loglevel, "changing to %"PRId64". This may result "
841
                       "in incorrect timestamps in the output file.\n",
842
                       max);
843
2
                if (pkt->pts >= pkt->dts)
844
2
                    pkt->pts = FFMAX(pkt->pts, max);
845
2
                pkt->dts = max;
846
            }
847
        }
848
    }
849
429603
    ost->last_mux_dts = pkt->dts;
850
851
429603
    ost->data_size += pkt->size;
852
429603
    ost->packets_written++;
853
854
429603
    pkt->stream_index = ost->index;
855
856
429603
    if (debug_ts) {
857
        av_log(NULL, AV_LOG_INFO, "muxer <- type:%s "
858
                "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n",
859
                av_get_media_type_string(ost->enc_ctx->codec_type),
860
                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
861
                av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
862
                pkt->size
863
              );
864
    }
865
866
429603
    ret = av_interleaved_write_frame(s, pkt);
867
429603
    if (ret < 0) {
868
        print_error("av_interleaved_write_frame()", ret);
869
        main_return_code = 1;
870
        close_all_output_streams(ost, MUXER_FINISHED | ENCODER_FINISHED, ENCODER_FINISHED);
871
    }
872
429603
    av_packet_unref(pkt);
873
}
874
875
5834
static void close_output_stream(OutputStream *ost)
876
{
877
5834
    OutputFile *of = output_files[ost->file_index];
878
879
5834
    ost->finished |= ENCODER_FINISHED;
880
5834
    if (of->shortest) {
881
        int64_t end = av_rescale_q(ost->sync_opts - ost->first_pts, ost->enc_ctx->time_base, AV_TIME_BASE_Q);
882
        of->recording_time = FFMIN(of->recording_time, end);
883
    }
884
5834
}
885
886
/*
887
 * Send a single packet to the output, applying any bitstream filters
888
 * associated with the output stream.  This may result in any number
889
 * of packets actually being written, depending on what bitstream
890
 * filters are applied.  The supplied packet is consumed and will be
891
 * blank (as if newly-allocated) when this function returns.
892
 *
893
 * If eof is set, instead indicate EOF to all bitstream filters and
894
 * therefore flush any delayed packets to the output.  A blank packet
895
 * must be supplied in this case.
896
 */
897
435740
static void output_packet(OutputFile *of, AVPacket *pkt,
898
                          OutputStream *ost, int eof)
899
{
900
435740
    int ret = 0;
901
902
    /* apply the output bitstream filters */
903
435740
    if (ost->bsf_ctx) {
904
6820
        ret = av_bsf_send_packet(ost->bsf_ctx, eof ? NULL : pkt);
905
6820
        if (ret < 0)
906
            goto finish;
907
13633
        while ((ret = av_bsf_receive_packet(ost->bsf_ctx, pkt)) >= 0)
908
6813
            write_packet(of, pkt, ost, 0);
909
6820
        if (ret == AVERROR(EAGAIN))
910
6814
            ret = 0;
911
428920
    } else if (!eof)
912
423107
        write_packet(of, pkt, ost, 0);
913
914
5813
finish:
915

435740
    if (ret < 0 && ret != AVERROR_EOF) {
916
2
        av_log(NULL, AV_LOG_ERROR, "Error applying bitstream filters to an output "
917
               "packet for stream #%d:%d.\n", ost->file_index, ost->index);
918
2
        if(exit_on_error)
919
            exit_program(1);
920
    }
921
435740
}
922
923
401246
static int check_recording_time(OutputStream *ost)
924
{
925
401246
    OutputFile *of = output_files[ost->file_index];
926
927

408720
    if (of->recording_time != INT64_MAX &&
928
7474
        av_compare_ts(ost->sync_opts - ost->first_pts, ost->enc_ctx->time_base, of->recording_time,
929
7474
                      AV_TIME_BASE_Q) >= 0) {
930
1
        close_output_stream(ost);
931
1
        return 0;
932
    }
933
401245
    return 1;
934
}
935
936
402944
static double adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost,
937
                                             AVFrame *frame)
938
{
939
402944
    double float_pts = AV_NOPTS_VALUE; // this is identical to frame.pts but with higher precision
940
402944
    AVCodecContext *enc = ost->enc_ctx;
941

402944
    if (!frame || frame->pts == AV_NOPTS_VALUE ||
942

400503
        !enc || !ost->filter || !ost->filter->graph->graph)
943
2441
        goto early_exit;
944
945
    {
946
400503
        AVFilterContext *filter = ost->filter->filter;
947
948
400503
        int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
949
400503
        AVRational filter_tb = av_buffersink_get_time_base(filter);
950
400503
        AVRational tb = enc->time_base;
951
400503
        int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
952
953
400503
        tb.den <<= extra_bits;
954
400503
        float_pts =
955
400503
            av_rescale_q(frame->pts, filter_tb, tb) -
956
400503
            av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
957
400503
        float_pts /= 1 << extra_bits;
958
        // 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
959
400503
        float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
960
961
400503
        frame->pts =
962
400503
            av_rescale_q(frame->pts, filter_tb, enc->time_base) -
963
400503
            av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
964
    }
965
966
402944
early_exit:
967
968
402944
    if (debug_ts) {
969
        av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n",
970
               frame ? av_ts2str(frame->pts) : "NULL",
971
               frame ? av_ts2timestr(frame->pts, &enc->time_base) : "NULL",
972
               float_pts,
973
               enc ? enc->time_base.num : -1,
974
               enc ? enc->time_base.den : -1);
975
    }
976
977
402944
    return float_pts;
978
}
979
980
static int init_output_stream(OutputStream *ost, AVFrame *frame,
981
                              char *error, int error_len);
982
983
619076
static int init_output_stream_wrapper(OutputStream *ost, AVFrame *frame,
984
                                      unsigned int fatal)
985
{
986
619076
    int ret = AVERROR_BUG;
987
619076
    char error[1024] = {0};
988
989
619076
    if (ost->initialized)
990
612885
        return 0;
991
992
6191
    ret = init_output_stream(ost, frame, error, sizeof(error));
993
6191
    if (ret < 0) {
994
        av_log(NULL, AV_LOG_ERROR, "Error initializing output stream %d:%d -- %s\n",
995
               ost->file_index, ost->index, error);
996
997
        if (fatal)
998
            exit_program(1);
999
    }
1000
1001
6191
    return ret;
1002
}
1003
1004
300855
static void do_audio_out(OutputFile *of, OutputStream *ost,
1005
                         AVFrame *frame)
1006
{
1007
300855
    AVCodecContext *enc = ost->enc_ctx;
1008
300855
    AVPacket *pkt = ost->pkt;
1009
    int ret;
1010
1011
300855
    adjust_frame_pts_to_encoder_tb(of, ost, frame);
1012
1013
300855
    if (!check_recording_time(ost))
1014
        return;
1015
1016

300855
    if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
1017
        frame->pts = ost->sync_opts;
1018
300855
    ost->sync_opts = frame->pts + frame->nb_samples;
1019
300855
    ost->samples_encoded += frame->nb_samples;
1020
300855
    ost->frames_encoded++;
1021
1022
300855
    update_benchmark(NULL);
1023
300855
    if (debug_ts) {
1024
        av_log(NULL, AV_LOG_INFO, "encoder <- type:audio "
1025
               "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
1026
               av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
1027
               enc->time_base.num, enc->time_base.den);
1028
    }
1029
1030
300855
    ret = avcodec_send_frame(enc, frame);
1031
300855
    if (ret < 0)
1032
        goto error;
1033
1034
    while (1) {
1035
595554
        av_packet_unref(pkt);
1036
595554
        ret = avcodec_receive_packet(enc, pkt);
1037
595554
        if (ret == AVERROR(EAGAIN))
1038
300855
            break;
1039
294699
        if (ret < 0)
1040
            goto error;
1041
1042
294699
        update_benchmark("encode_audio %d.%d", ost->file_index, ost->index);
1043
1044
294699
        av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
1045
1046
294699
        if (debug_ts) {
1047
            av_log(NULL, AV_LOG_INFO, "encoder -> type:audio "
1048
                   "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1049
                   av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
1050
                   av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base));
1051
        }
1052
1053
294699
        output_packet(of, pkt, ost, 0);
1054
    }
1055
1056
300855
    return;
1057
error:
1058
    av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
1059
    exit_program(1);
1060
}
1061
1062
668
static void do_subtitle_out(OutputFile *of,
1063
                            OutputStream *ost,
1064
                            AVSubtitle *sub)
1065
{
1066
668
    int subtitle_out_max_size = 1024 * 1024;
1067
    int subtitle_out_size, nb, i;
1068
    AVCodecContext *enc;
1069
668
    AVPacket *pkt = ost->pkt;
1070
    int64_t pts;
1071
1072
668
    if (sub->pts == AV_NOPTS_VALUE) {
1073
        av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
1074
        if (exit_on_error)
1075
            exit_program(1);
1076
        return;
1077
    }
1078
1079
668
    enc = ost->enc_ctx;
1080
1081
668
    if (!subtitle_out) {
1082
32
        subtitle_out = av_malloc(subtitle_out_max_size);
1083
32
        if (!subtitle_out) {
1084
            av_log(NULL, AV_LOG_FATAL, "Failed to allocate subtitle_out\n");
1085
            exit_program(1);
1086
        }
1087
    }
1088
1089
    /* Note: DVB subtitle need one packet to draw them and one other
1090
       packet to clear them */
1091
    /* XXX: signal it in the codec context ? */
1092
668
    if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
1093
36
        nb = 2;
1094
    else
1095
632
        nb = 1;
1096
1097
    /* shift timestamp to honor -ss and make check_recording_time() work with -t */
1098
668
    pts = sub->pts;
1099
668
    if (output_files[ost->file_index]->start_time != AV_NOPTS_VALUE)
1100
        pts -= output_files[ost->file_index]->start_time;
1101
1372
    for (i = 0; i < nb; i++) {
1102
704
        unsigned save_num_rects = sub->num_rects;
1103
1104
704
        ost->sync_opts = av_rescale_q(pts, AV_TIME_BASE_Q, enc->time_base);
1105
704
        if (!check_recording_time(ost))
1106
            return;
1107
1108
704
        sub->pts = pts;
1109
        // start_display_time is required to be 0
1110
704
        sub->pts               += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
1111
704
        sub->end_display_time  -= sub->start_display_time;
1112
704
        sub->start_display_time = 0;
1113
704
        if (i == 1)
1114
36
            sub->num_rects = 0;
1115
1116
704
        ost->frames_encoded++;
1117
1118
704
        subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
1119
                                                    subtitle_out_max_size, sub);
1120
704
        if (i == 1)
1121
36
            sub->num_rects = save_num_rects;
1122
704
        if (subtitle_out_size < 0) {
1123
            av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
1124
            exit_program(1);
1125
        }
1126
1127
704
        av_packet_unref(pkt);
1128
704
        pkt->data = subtitle_out;
1129
704
        pkt->size = subtitle_out_size;
1130
704
        pkt->pts  = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->mux_timebase);
1131
704
        pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
1132
704
        if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
1133
            /* XXX: the pts correction is handled here. Maybe handling
1134
               it in the codec would be better */
1135
72
            if (i == 0)
1136
36
                pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
1137
            else
1138
36
                pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, ost->mux_timebase);
1139
        }
1140
704
        pkt->dts = pkt->pts;
1141
704
        output_packet(of, pkt, ost, 0);
1142
    }
1143
}
1144
1145
102089
static void do_video_out(OutputFile *of,
1146
                         OutputStream *ost,
1147
                         AVFrame *next_picture)
1148
{
1149
    int ret, format_video_sync;
1150
102089
    AVPacket *pkt = ost->pkt;
1151
102089
    AVCodecContext *enc = ost->enc_ctx;
1152
    AVRational frame_rate;
1153
    int nb_frames, nb0_frames, i;
1154
    double delta, delta0;
1155
102089
    double duration = 0;
1156
102089
    double sync_ipts = AV_NOPTS_VALUE;
1157
102089
    int frame_size = 0;
1158
102089
    InputStream *ist = NULL;
1159
102089
    AVFilterContext *filter = ost->filter->filter;
1160
1161
102089
    init_output_stream_wrapper(ost, next_picture, 1);
1162
102089
    sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, next_picture);
1163
1164
102089
    if (ost->source_index >= 0)
1165
100573
        ist = input_streams[ost->source_index];
1166
1167
102089
    frame_rate = av_buffersink_get_frame_rate(filter);
1168

102089
    if (frame_rate.num > 0 && frame_rate.den > 0)
1169
101960
        duration = 1/(av_q2d(frame_rate) * av_q2d(enc->time_base));
1170
1171


102089
    if(ist && ist->st->start_time != AV_NOPTS_VALUE && ist->st->first_dts != AV_NOPTS_VALUE && ost->frame_rate.num)
1172
65504
        duration = FFMIN(duration, 1/(av_q2d(ost->frame_rate) * av_q2d(enc->time_base)));
1173
1174
102089
    if (!ost->filters_script &&
1175
101992
        !ost->filters &&
1176

85657
        (nb_filtergraphs == 0 || !filtergraphs[0]->graph_desc) &&
1177
81481
        next_picture &&
1178
81481
        ist &&
1179
81481
        lrintf(next_picture->pkt_duration * av_q2d(ist->st->time_base) / av_q2d(enc->time_base)) > 0) {
1180
76919
        duration = lrintf(next_picture->pkt_duration * av_q2d(ist->st->time_base) / av_q2d(enc->time_base));
1181
    }
1182
1183
102089
    if (!next_picture) {
1184
        //end, flushing
1185
2441
        nb0_frames = nb_frames = mid_pred(ost->last_nb0_frames[0],
1186
                                          ost->last_nb0_frames[1],
1187
                                          ost->last_nb0_frames[2]);
1188
    } else {
1189
99648
        delta0 = sync_ipts - ost->sync_opts; // delta0 is the "drift" between the input frame (next_picture) and where it would fall in the output.
1190
99648
        delta  = delta0 + duration;
1191
1192
        /* by default, we output a single frame */
1193
99648
        nb0_frames = 0; // tracks the number of times the PREVIOUS frame should be duplicated, mostly for variable framerate (VFR)
1194
99648
        nb_frames = 1;
1195
1196
99648
        format_video_sync = video_sync_method;
1197
99648
        if (format_video_sync == VSYNC_AUTO) {
1198
79634
            if(!strcmp(of->ctx->oformat->name, "avi")) {
1199
12650
                format_video_sync = VSYNC_VFR;
1200
            } else
1201

66984
                format_video_sync = (of->ctx->oformat->flags & AVFMT_VARIABLE_FPS) ? ((of->ctx->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH : VSYNC_VFR) : VSYNC_CFR;
1202
79634
            if (   ist
1203
78163
                && format_video_sync == VSYNC_CFR
1204
9454
                && input_files[ist->file_index]->ctx->nb_streams == 1
1205
8349
                && input_files[ist->file_index]->input_ts_offset == 0) {
1206
8349
                format_video_sync = VSYNC_VSCFR;
1207
            }
1208

79634
            if (format_video_sync == VSYNC_CFR && copy_ts) {
1209
                format_video_sync = VSYNC_VSCFR;
1210
            }
1211
        }
1212

99648
        ost->is_cfr = (format_video_sync == VSYNC_CFR || format_video_sync == VSYNC_VSCFR);
1213
1214

99648
        if (delta0 < 0 &&
1215
11495
            delta > 0 &&
1216
10902
            format_video_sync != VSYNC_PASSTHROUGH &&
1217
            format_video_sync != VSYNC_DROP) {
1218
10902
            if (delta0 < -0.6) {
1219
711
                av_log(NULL, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0);
1220
            } else
1221
10191
                av_log(NULL, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0);
1222
10902
            sync_ipts = ost->sync_opts;
1223
10902
            duration += delta0;
1224
10902
            delta0 = 0;
1225
        }
1226
1227

99648
        switch (format_video_sync) {
1228
8349
        case VSYNC_VSCFR:
1229

8349
            if (ost->frame_number == 0 && delta0 >= 0.5) {
1230
                av_log(NULL, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0));
1231
                delta = duration;
1232
                delta0 = 0;
1233
                ost->sync_opts = llrint(sync_ipts);
1234
            }
1235
        case VSYNC_CFR:
1236
            // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
1237

9608
            if (frame_drop_threshold && delta < frame_drop_threshold && ost->frame_number) {
1238
                nb_frames = 0;
1239
9608
            } else if (delta < -1.1)
1240
                nb_frames = 0;
1241
9608
            else if (delta > 1.1) {
1242
350
                nb_frames = lrintf(delta);
1243
350
                if (delta0 > 1.1)
1244
2
                    nb0_frames = llrintf(delta0 - 0.6);
1245
            }
1246
9608
            break;
1247
70173
        case VSYNC_VFR:
1248
70173
            if (delta <= -0.6)
1249
170
                nb_frames = 0;
1250
70003
            else if (delta > 0.6)
1251
66856
                ost->sync_opts = llrint(sync_ipts);
1252
70173
            break;
1253
19867
        case VSYNC_DROP:
1254
        case VSYNC_PASSTHROUGH:
1255
19867
            ost->sync_opts = llrint(sync_ipts);
1256
19867
            break;
1257
        default:
1258
            av_assert0(0);
1259
        }
1260
    }
1261
1262
102089
    nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number);
1263
102089
    nb0_frames = FFMIN(nb0_frames, nb_frames);
1264
1265
102089
    memmove(ost->last_nb0_frames + 1,
1266
102089
            ost->last_nb0_frames,
1267
            sizeof(ost->last_nb0_frames[0]) * (FF_ARRAY_ELEMS(ost->last_nb0_frames) - 1));
1268
102089
    ost->last_nb0_frames[0] = nb0_frames;
1269
1270

102089
    if (nb0_frames == 0 && ost->last_dropped) {
1271
170
        nb_frames_drop++;
1272
170
        av_log(NULL, AV_LOG_VERBOSE,
1273
               "*** dropping frame %d from stream %d at ts %"PRId64"\n",
1274
170
               ost->frame_number, ost->st->index, ost->last_frame->pts);
1275
    }
1276

102089
    if (nb_frames > (nb0_frames && ost->last_dropped) + (nb_frames > nb0_frames)) {
1277
49
        if (nb_frames > dts_error_threshold * 30) {
1278
            av_log(NULL, AV_LOG_ERROR, "%d frame duplication too large, skipping\n", nb_frames - 1);
1279
            nb_frames_drop++;
1280
            return;
1281
        }
1282

49
        nb_frames_dup += nb_frames - (nb0_frames && ost->last_dropped) - (nb_frames > nb0_frames);
1283
49
        av_log(NULL, AV_LOG_VERBOSE, "*** %d dup!\n", nb_frames - 1);
1284
49
        if (nb_frames_dup > dup_warning) {
1285
            av_log(NULL, AV_LOG_WARNING, "More than %d frames duplicated\n", dup_warning);
1286
            dup_warning *= 10;
1287
        }
1288
    }
1289

102089
    ost->last_dropped = nb_frames == nb0_frames && next_picture;
1290
1291
    /* duplicates frame if needed */
1292
201775
    for (i = 0; i < nb_frames; i++) {
1293
        AVFrame *in_picture;
1294
99687
        int forced_keyframe = 0;
1295
        double pts_time;
1296
1297

99687
        if (i < nb0_frames && ost->last_frame) {
1298
69
            in_picture = ost->last_frame;
1299
        } else
1300
99618
            in_picture = next_picture;
1301
1302
99687
        if (!in_picture)
1303
            return;
1304
1305
99687
        in_picture->pts = ost->sync_opts;
1306
1307
99687
        if (!check_recording_time(ost))
1308
1
            return;
1309
1310
99686
        in_picture->quality = enc->global_quality;
1311
99686
        in_picture->pict_type = 0;
1312
1313
99686
        if (ost->forced_kf_ref_pts == AV_NOPTS_VALUE &&
1314
4683
            in_picture->pts != AV_NOPTS_VALUE)
1315
4683
            ost->forced_kf_ref_pts = in_picture->pts;
1316
1317
199372
        pts_time = in_picture->pts != AV_NOPTS_VALUE ?
1318
99686
            (in_picture->pts - ost->forced_kf_ref_pts) * av_q2d(enc->time_base) : NAN;
1319
99686
        if (ost->forced_kf_index < ost->forced_kf_count &&
1320
39
            in_picture->pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
1321
2
            ost->forced_kf_index++;
1322
2
            forced_keyframe = 1;
1323
99684
        } else if (ost->forced_keyframes_pexpr) {
1324
            double res;
1325
            ost->forced_keyframes_expr_const_values[FKF_T] = pts_time;
1326
            res = av_expr_eval(ost->forced_keyframes_pexpr,
1327
                               ost->forced_keyframes_expr_const_values, NULL);
1328
            ff_dlog(NULL, "force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
1329
                    ost->forced_keyframes_expr_const_values[FKF_N],
1330
                    ost->forced_keyframes_expr_const_values[FKF_N_FORCED],
1331
                    ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N],
1332
                    ost->forced_keyframes_expr_const_values[FKF_T],
1333
                    ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T],
1334
                    res);
1335
            if (res) {
1336
                forced_keyframe = 1;
1337
                ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_N] =
1338
                    ost->forced_keyframes_expr_const_values[FKF_N];
1339
                ost->forced_keyframes_expr_const_values[FKF_PREV_FORCED_T] =
1340
                    ost->forced_keyframes_expr_const_values[FKF_T];
1341
                ost->forced_keyframes_expr_const_values[FKF_N_FORCED] += 1;
1342
            }
1343
1344
            ost->forced_keyframes_expr_const_values[FKF_N] += 1;
1345
99684
        } else if (   ost->forced_keyframes
1346
48
                   && !strncmp(ost->forced_keyframes, "source", 6)
1347
                   && in_picture->key_frame==1
1348
                   && !i) {
1349
            forced_keyframe = 1;
1350
        }
1351
1352
99686
        if (forced_keyframe) {
1353
2
            in_picture->pict_type = AV_PICTURE_TYPE_I;
1354
2
            av_log(NULL, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
1355
        }
1356
1357
99686
        update_benchmark(NULL);
1358
99686
        if (debug_ts) {
1359
            av_log(NULL, AV_LOG_INFO, "encoder <- type:video "
1360
                   "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
1361
                   av_ts2str(in_picture->pts), av_ts2timestr(in_picture->pts, &enc->time_base),
1362
                   enc->time_base.num, enc->time_base.den);
1363
        }
1364
1365
99686
        ost->frames_encoded++;
1366
1367
99686
        ret = avcodec_send_frame(enc, in_picture);
1368
99686
        if (ret < 0)
1369
            goto error;
1370
        // Make sure Closed Captions will not be duplicated
1371
99686
        av_frame_remove_side_data(in_picture, AV_FRAME_DATA_A53_CC);
1372
1373
        while (1) {
1374
199152
            av_packet_unref(pkt);
1375
199152
            ret = avcodec_receive_packet(enc, pkt);
1376
199152
            update_benchmark("encode_video %d.%d", ost->file_index, ost->index);
1377
199152
            if (ret == AVERROR(EAGAIN))
1378
99686
                break;
1379
99466
            if (ret < 0)
1380
                goto error;
1381
1382
99466
            if (debug_ts) {
1383
                av_log(NULL, AV_LOG_INFO, "encoder -> type:video "
1384
                       "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1385
                       av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
1386
                       av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base));
1387
            }
1388
1389

99466
            if (pkt->pts == AV_NOPTS_VALUE && !(enc->codec->capabilities & AV_CODEC_CAP_DELAY))
1390
                pkt->pts = ost->sync_opts;
1391
1392
99466
            av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
1393
1394
99466
            if (debug_ts) {
1395
                av_log(NULL, AV_LOG_INFO, "encoder -> type:video "
1396
                    "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s\n",
1397
                    av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->mux_timebase),
1398
                    av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->mux_timebase));
1399
            }
1400
1401
99466
            frame_size = pkt->size;
1402
99466
            output_packet(of, pkt, ost, 0);
1403
1404
            /* if two pass, output log */
1405

99466
            if (ost->logfile && enc->stats_out) {
1406
                fprintf(ost->logfile, "%s", enc->stats_out);
1407
            }
1408
        }
1409
99686
        ost->sync_opts++;
1410
        /*
1411
         * For video, number of frames in == number of packets out.
1412
         * But there may be reordering, so we can't throw away frames on encoder
1413
         * flush, we need to limit them here, before they go into encoder.
1414
         */
1415
99686
        ost->frame_number++;
1416
1417

99686
        if (vstats_filename && frame_size)
1418
            do_video_stats(ost, frame_size);
1419
    }
1420
1421
102088
    if (!ost->last_frame)
1422
4772
        ost->last_frame = av_frame_alloc();
1423
102088
    av_frame_unref(ost->last_frame);
1424

102088
    if (next_picture && ost->last_frame)
1425
99647
        av_frame_ref(ost->last_frame, next_picture);
1426
    else
1427
2441
        av_frame_free(&ost->last_frame);
1428
1429
102088
    return;
1430
error:
1431
    av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
1432
    exit_program(1);
1433
}
1434
1435
static double psnr(double d)
1436
{
1437
    return -10.0 * log10(d);
1438
}
1439
1440
static void do_video_stats(OutputStream *ost, int frame_size)
1441
{
1442
    AVCodecContext *enc;
1443
    int frame_number;
1444
    double ti1, bitrate, avg_bitrate;
1445
1446
    /* this is executed just the first time do_video_stats is called */
1447
    if (!vstats_file) {
1448
        vstats_file = fopen(vstats_filename, "w");
1449
        if (!vstats_file) {
1450
            perror("fopen");
1451
            exit_program(1);
1452
        }
1453
    }
1454
1455
    enc = ost->enc_ctx;
1456
    if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1457
        frame_number = ost->st->nb_frames;
1458
        if (vstats_version <= 1) {
1459
            fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
1460
                    ost->quality / (float)FF_QP2LAMBDA);
1461
        } else  {
1462
            fprintf(vstats_file, "out= %2d st= %2d frame= %5d q= %2.1f ", ost->file_index, ost->index, frame_number,
1463
                    ost->quality / (float)FF_QP2LAMBDA);
1464
        }
1465
1466
        if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
1467
            fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
1468
1469
        fprintf(vstats_file,"f_size= %6d ", frame_size);
1470
        /* compute pts value */
1471
        ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base);
1472
        if (ti1 < 0.01)
1473
            ti1 = 0.01;
1474
1475
        bitrate     = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
1476
        avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
1477
        fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1478
               (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
1479
        fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type));
1480
    }
1481
}
1482
1483
358
static void finish_output_stream(OutputStream *ost)
1484
{
1485
358
    OutputFile *of = output_files[ost->file_index];
1486
    int i;
1487
1488
358
    ost->finished = ENCODER_FINISHED | MUXER_FINISHED;
1489
1490
358
    if (of->shortest) {
1491
        for (i = 0; i < of->ctx->nb_streams; i++)
1492
            output_streams[of->ost_index + i]->finished = ENCODER_FINISHED | MUXER_FINISHED;
1493
    }
1494
358
}
1495
1496
/**
1497
 * Get and encode new output from any of the filtergraphs, without causing
1498
 * activity.
1499
 *
1500
 * @return  0 for success, <0 for severe errors
1501
 */
1502
414105
static int reap_filters(int flush)
1503
{
1504
414105
    AVFrame *filtered_frame = NULL;
1505
    int i;
1506
1507
    /* Reap all buffers present in the buffer sinks */
1508
849185
    for (i = 0; i < nb_output_streams; i++) {
1509
435080
        OutputStream *ost = output_streams[i];
1510
435080
        OutputFile    *of = output_files[ost->file_index];
1511
        AVFilterContext *filter;
1512
435080
        AVCodecContext *enc = ost->enc_ctx;
1513
435080
        int ret = 0;
1514
1515

435080
        if (!ost->filter || !ost->filter->graph->graph)
1516
56895
            continue;
1517
378185
        filter = ost->filter->filter;
1518
1519
        /*
1520
         * Unlike video, with audio the audio frame size matters.
1521
         * Currently we are fully reliant on the lavfi filter chain to
1522
         * do the buffering deed for us, and thus the frame size parameter
1523
         * needs to be set accordingly. Where does one get the required
1524
         * frame size? From the initialized AVCodecContext of an audio
1525
         * encoder. Thus, if we have gotten to an audio stream, initialize
1526
         * the encoder earlier than receiving the first AVFrame.
1527
         */
1528
378185
        if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_AUDIO)
1529
260591
            init_output_stream_wrapper(ost, NULL, 1);
1530
1531

378185
        if (!ost->pkt && !(ost->pkt = av_packet_alloc())) {
1532
            return AVERROR(ENOMEM);
1533
        }
1534

378185
        if (!ost->filtered_frame && !(ost->filtered_frame = av_frame_alloc())) {
1535
            return AVERROR(ENOMEM);
1536
        }
1537
378185
        filtered_frame = ost->filtered_frame;
1538
1539
        while (1) {
1540
778688
            ret = av_buffersink_get_frame_flags(filter, filtered_frame,
1541
                                               AV_BUFFERSINK_FLAG_NO_REQUEST);
1542
778688
            if (ret < 0) {
1543

378185
                if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
1544
                    av_log(NULL, AV_LOG_WARNING,
1545
                           "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(ret));
1546

378185
                } else if (flush && ret == AVERROR_EOF) {
1547
3617
                    if (av_buffersink_get_type(filter) == AVMEDIA_TYPE_VIDEO)
1548
2441
                        do_video_out(of, ost, NULL);
1549
                }
1550
378185
                break;
1551
            }
1552
400503
            if (ost->finished) {
1553
                av_frame_unref(filtered_frame);
1554
                continue;
1555
            }
1556
1557
400503
            switch (av_buffersink_get_type(filter)) {
1558
99648
            case AVMEDIA_TYPE_VIDEO:
1559
99648
                if (!ost->frame_aspect_ratio.num)
1560
99648
                    enc->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
1561
1562
99648
                do_video_out(of, ost, filtered_frame);
1563
99648
                break;
1564
300855
            case AVMEDIA_TYPE_AUDIO:
1565
300855
                if (!(enc->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
1566
300855
                    enc->channels != filtered_frame->channels) {
1567
                    av_log(NULL, AV_LOG_ERROR,
1568
                           "Audio filter graph output is not normalized and encoder does not support parameter changes\n");
1569
                    break;
1570
                }
1571
300855
                do_audio_out(of, ost, filtered_frame);
1572
300855
                break;
1573
            default:
1574
                // TODO support subtitle filters
1575
                av_assert0(0);
1576
            }
1577
1578
400503
            av_frame_unref(filtered_frame);
1579
        }
1580
    }
1581
1582
414105
    return 0;
1583
}
1584
1585
6008
static void print_final_stats(int64_t total_size)
1586
{
1587
6008
    uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
1588
6008
    uint64_t subtitle_size = 0;
1589
6008
    uint64_t data_size = 0;
1590
6008
    float percent = -1.0;
1591
    int i, j;
1592
6008
    int pass1_used = 1;
1593
1594
12199
    for (i = 0; i < nb_output_streams; i++) {
1595
6191
        OutputStream *ost = output_streams[i];
1596

6191
        switch (ost->enc_ctx->codec_type) {
1597
4879
            case AVMEDIA_TYPE_VIDEO: video_size += ost->data_size; break;
1598
1254
            case AVMEDIA_TYPE_AUDIO: audio_size += ost->data_size; break;
1599
47
            case AVMEDIA_TYPE_SUBTITLE: subtitle_size += ost->data_size; break;
1600
11
            default:                 other_size += ost->data_size; break;
1601
        }
1602
6191
        extra_size += ost->enc_ctx->extradata_size;
1603
6191
        data_size  += ost->data_size;
1604
6191
        if (   (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
1605
            != AV_CODEC_FLAG_PASS1)
1606
6191
            pass1_used = 0;
1607
    }
1608
1609

6008
    if (data_size && total_size>0 && total_size >= data_size)
1610
4156
        percent = 100.0 * (total_size - data_size) / data_size;
1611
1612
6008
    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: ",
1613
           video_size / 1024.0,
1614
           audio_size / 1024.0,
1615
           subtitle_size / 1024.0,
1616
           other_size / 1024.0,
1617
           extra_size / 1024.0);
1618
6008
    if (percent >= 0.0)
1619
4156
        av_log(NULL, AV_LOG_INFO, "%f%%", percent);
1620
    else
1621
1852
        av_log(NULL, AV_LOG_INFO, "unknown");
1622
6008
    av_log(NULL, AV_LOG_INFO, "\n");
1623
1624
    /* print verbose per-stream stats */
1625
12046
    for (i = 0; i < nb_input_files; i++) {
1626
6038
        InputFile *f = input_files[i];
1627
6038
        uint64_t total_packets = 0, total_size = 0;
1628
1629
6038
        av_log(NULL, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
1630
6038
               i, f->ctx->url);
1631
1632
12442
        for (j = 0; j < f->nb_streams; j++) {
1633
6404
            InputStream *ist = input_streams[f->ist_index + j];
1634
6404
            enum AVMediaType type = ist->dec_ctx->codec_type;
1635
1636
6404
            total_size    += ist->data_size;
1637
6404
            total_packets += ist->nb_packets;
1638
1639
6404
            av_log(NULL, AV_LOG_VERBOSE, "  Input stream #%d:%d (%s): ",
1640
                   i, j, media_type_string(type));
1641
6404
            av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
1642
                   ist->nb_packets, ist->data_size);
1643
1644
6404
            if (ist->decoding_needed) {
1645
5818
                av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames decoded",
1646
                       ist->frames_decoded);
1647
5818
                if (type == AVMEDIA_TYPE_AUDIO)
1648
1129
                    av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
1649
5818
                av_log(NULL, AV_LOG_VERBOSE, "; ");
1650
            }
1651
1652
6404
            av_log(NULL, AV_LOG_VERBOSE, "\n");
1653
        }
1654
1655
6038
        av_log(NULL, AV_LOG_VERBOSE, "  Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
1656
               total_packets, total_size);
1657
    }
1658
1659
12017
    for (i = 0; i < nb_output_files; i++) {
1660
6009
        OutputFile *of = output_files[i];
1661
6009
        uint64_t total_packets = 0, total_size = 0;
1662
1663
6009
        av_log(NULL, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
1664
6009
               i, of->ctx->url);
1665
1666
12200
        for (j = 0; j < of->ctx->nb_streams; j++) {
1667
6191
            OutputStream *ost = output_streams[of->ost_index + j];
1668
6191
            enum AVMediaType type = ost->enc_ctx->codec_type;
1669
1670
6191
            total_size    += ost->data_size;
1671
6191
            total_packets += ost->packets_written;
1672
1673
6191
            av_log(NULL, AV_LOG_VERBOSE, "  Output stream #%d:%d (%s): ",
1674
                   i, j, media_type_string(type));
1675
6191
            if (ost->encoding_needed) {
1676
5844
                av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
1677
                       ost->frames_encoded);
1678
5844
                if (type == AVMEDIA_TYPE_AUDIO)
1679
1128
                    av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
1680
5844
                av_log(NULL, AV_LOG_VERBOSE, "; ");
1681
            }
1682
1683
6191
            av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
1684
                   ost->packets_written, ost->data_size);
1685
1686
6191
            av_log(NULL, AV_LOG_VERBOSE, "\n");
1687
        }
1688
1689
6009
        av_log(NULL, AV_LOG_VERBOSE, "  Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
1690
               total_packets, total_size);
1691
    }
1692
6008
    if(video_size + data_size + audio_size + subtitle_size + extra_size == 0){
1693
6
        av_log(NULL, AV_LOG_WARNING, "Output file is empty, nothing was encoded ");
1694
6
        if (pass1_used) {
1695
1
            av_log(NULL, AV_LOG_WARNING, "\n");
1696
        } else {
1697
5
            av_log(NULL, AV_LOG_WARNING, "(check -ss / -t / -frames parameters if used)\n");
1698
        }
1699
    }
1700
6008
}
1701
1702
1040842
static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time)
1703
{
1704
    AVBPrint buf, buf_script;
1705
    OutputStream *ost;
1706
    AVFormatContext *oc;
1707
    int64_t total_size;
1708
    AVCodecContext *enc;
1709
    int frame_number, vid, i;
1710
    double bitrate;
1711
    double speed;
1712
1040842
    int64_t pts = INT64_MIN + 1;
1713
    static int64_t last_time = -1;
1714
    static int first_report = 1;
1715
    static int qp_histogram[52];
1716
    int hours, mins, secs, us;
1717
    const char *hours_sign;
1718
    int ret;
1719
    float t;
1720
1721

1040842
    if (!print_stats && !is_last_report && !progress_avio)
1722
1034772
        return;
1723
1724
16415
    if (!is_last_report) {
1725
10407
        if (last_time == -1) {
1726
32
            last_time = cur_time;
1727
        }
1728

10407
        if (((cur_time - last_time) < stats_period && !first_report) ||
1729

68
            (first_report && nb_output_dumped < nb_output_files))
1730
10345
            return;
1731
62
        last_time = cur_time;
1732
    }
1733
1734
6070
    t = (cur_time-timer_start) / 1000000.0;
1735
1736
1737
6070
    oc = output_files[0]->ctx;
1738
1739
6070
    total_size = avio_size(oc->pb);
1740
6070
    if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too
1741
103
        total_size = avio_tell(oc->pb);
1742
1743
6070
    vid = 0;
1744
6070
    av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
1745
6070
    av_bprint_init(&buf_script, 0, AV_BPRINT_SIZE_AUTOMATIC);
1746
12334
    for (i = 0; i < nb_output_streams; i++) {
1747
6264
        float q = -1;
1748
6264
        ost = output_streams[i];
1749
6264
        enc = ost->enc_ctx;
1750
6264
        if (!ost->stream_copy)
1751
5895
            q = ost->quality / (float) FF_QP2LAMBDA;
1752
1753

6264
        if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1754
30
            av_bprintf(&buf, "q=%2.1f ", q);
1755
30
            av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
1756
                       ost->file_index, ost->index, q);
1757
        }
1758

6264
        if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1759
            float fps;
1760
1761
4872
            frame_number = ost->frame_number;
1762
4872
            fps = t > 1 ? frame_number / t : 0;
1763
4872
            av_bprintf(&buf, "frame=%5d fps=%3.*f q=%3.1f ",
1764
                     frame_number, fps < 9.95, fps, q);
1765
4872
            av_bprintf(&buf_script, "frame=%d\n", frame_number);
1766
4872
            av_bprintf(&buf_script, "fps=%.2f\n", fps);
1767
4872
            av_bprintf(&buf_script, "stream_%d_%d_q=%.1f\n",
1768
                       ost->file_index, ost->index, q);
1769
4872
            if (is_last_report)
1770
4853
                av_bprintf(&buf, "L");
1771
4872
            if (qp_hist) {
1772
                int j;
1773
                int qp = lrintf(q);
1774
                if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
1775
                    qp_histogram[qp]++;
1776
                for (j = 0; j < 32; j++)
1777
                    av_bprintf(&buf, "%X", av_log2(qp_histogram[j] + 1));
1778
            }
1779
1780

4872
            if ((enc->flags & AV_CODEC_FLAG_PSNR) && (ost->pict_type != AV_PICTURE_TYPE_NONE || is_last_report)) {
1781
                int j;
1782
                double error, error_sum = 0;
1783
                double scale, scale_sum = 0;
1784
                double p;
1785
                char type[3] = { 'Y','U','V' };
1786
                av_bprintf(&buf, "PSNR=");
1787
                for (j = 0; j < 3; j++) {
1788
                    if (is_last_report) {
1789
                        error = enc->error[j];
1790
                        scale = enc->width * enc->height * 255.0 * 255.0 * frame_number;
1791
                    } else {
1792
                        error = ost->error[j];
1793
                        scale = enc->width * enc->height * 255.0 * 255.0;
1794
                    }
1795
                    if (j)
1796
                        scale /= 4;
1797
                    error_sum += error;
1798
                    scale_sum += scale;
1799
                    p = psnr(error / scale);
1800
                    av_bprintf(&buf, "%c:%2.2f ", type[j], p);
1801
                    av_bprintf(&buf_script, "stream_%d_%d_psnr_%c=%2.2f\n",
1802
                               ost->file_index, ost->index, type[j] | 32, p);
1803
                }
1804
                p = psnr(error_sum / scale_sum);
1805
                av_bprintf(&buf, "*:%2.2f ", psnr(error_sum / scale_sum));
1806
                av_bprintf(&buf_script, "stream_%d_%d_psnr_all=%2.2f\n",
1807
                           ost->file_index, ost->index, p);
1808
            }
1809
4872
            vid = 1;
1810
        }
1811
        /* compute min output value */
1812
6264
        if (av_stream_get_end_pts(ost->st) != AV_NOPTS_VALUE) {
1813
6264
            pts = FFMAX(pts, av_rescale_q(av_stream_get_end_pts(ost->st),
1814
                                          ost->st->time_base, AV_TIME_BASE_Q));
1815
6264
            if (copy_ts) {
1816

3
                if (copy_ts_first_pts == AV_NOPTS_VALUE && pts > 1)
1817
3
                    copy_ts_first_pts = pts;
1818
3
                if (copy_ts_first_pts != AV_NOPTS_VALUE)
1819
3
                    pts -= copy_ts_first_pts;
1820
            }
1821
        }
1822
1823
6264
        if (is_last_report)
1824
6191
            nb_frames_drop += ost->last_dropped;
1825
    }
1826
1827
6070
    secs = FFABS(pts) / AV_TIME_BASE;
1828
6070
    us = FFABS(pts) % AV_TIME_BASE;
1829
6070
    mins = secs / 60;
1830
6070
    secs %= 60;
1831
6070
    hours = mins / 60;
1832
6070
    mins %= 60;
1833
6070
    hours_sign = (pts < 0) ? "-" : "";
1834
1835

6070
    bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
1836
6070
    speed = t != 0.0 ? (double)pts / AV_TIME_BASE / t : -1;
1837
1838
6070
    if (total_size < 0) av_bprintf(&buf, "size=N/A time=");
1839
5975
    else                av_bprintf(&buf, "size=%8.0fkB time=", total_size / 1024.0);
1840
6070
    if (pts == AV_NOPTS_VALUE) {
1841
        av_bprintf(&buf, "N/A ");
1842
    } else {
1843
6070
        av_bprintf(&buf, "%s%02d:%02d:%02d.%02d ",
1844
                   hours_sign, hours, mins, secs, (100 * us) / AV_TIME_BASE);
1845
    }
1846
1847
6070
    if (bitrate < 0) {
1848
117
        av_bprintf(&buf, "bitrate=N/A");
1849
117
        av_bprintf(&buf_script, "bitrate=N/A\n");
1850
    }else{
1851
5953
        av_bprintf(&buf, "bitrate=%6.1fkbits/s", bitrate);
1852
5953
        av_bprintf(&buf_script, "bitrate=%6.1fkbits/s\n", bitrate);
1853
    }
1854
1855
6070
    if (total_size < 0) av_bprintf(&buf_script, "total_size=N/A\n");
1856
5975
    else                av_bprintf(&buf_script, "total_size=%"PRId64"\n", total_size);
1857
6070
    if (pts == AV_NOPTS_VALUE) {
1858
        av_bprintf(&buf_script, "out_time_us=N/A\n");
1859
        av_bprintf(&buf_script, "out_time_ms=N/A\n");
1860
        av_bprintf(&buf_script, "out_time=N/A\n");
1861
    } else {
1862
6070
        av_bprintf(&buf_script, "out_time_us=%"PRId64"\n", pts);
1863
6070
        av_bprintf(&buf_script, "out_time_ms=%"PRId64"\n", pts);
1864
6070
        av_bprintf(&buf_script, "out_time=%s%02d:%02d:%02d.%06d\n",
1865
                   hours_sign, hours, mins, secs, us);
1866
    }
1867
1868

6070
    if (nb_frames_dup || nb_frames_drop)
1869
26
        av_bprintf(&buf, " dup=%d drop=%d", nb_frames_dup, nb_frames_drop);
1870
6070
    av_bprintf(&buf_script, "dup_frames=%d\n", nb_frames_dup);
1871
6070
    av_bprintf(&buf_script, "drop_frames=%d\n", nb_frames_drop);
1872
1873
6070
    if (speed < 0) {
1874
16
        av_bprintf(&buf, " speed=N/A");
1875
16
        av_bprintf(&buf_script, "speed=N/A\n");
1876
    } else {
1877
6054
        av_bprintf(&buf, " speed=%4.3gx", speed);
1878
6054
        av_bprintf(&buf_script, "speed=%4.3gx\n", speed);
1879
    }
1880
1881

6070
    if (print_stats || is_last_report) {
1882
6070
        const char end = is_last_report ? '\n' : '\r';
1883

6070
        if (print_stats==1 && AV_LOG_INFO > av_log_get_level()) {
1884
            fprintf(stderr, "%s    %c", buf.str, end);
1885
        } else
1886
6070
            av_log(NULL, AV_LOG_INFO, "%s    %c", buf.str, end);
1887
1888
6070
        fflush(stderr);
1889
    }
1890
6070
    av_bprint_finalize(&buf, NULL);
1891
1892
6070
    if (progress_avio) {
1893
        av_bprintf(&buf_script, "progress=%s\n",
1894
                   is_last_report ? "end" : "continue");
1895
        avio_write(progress_avio, buf_script.str,
1896
                   FFMIN(buf_script.len, buf_script.size - 1));
1897
        avio_flush(progress_avio);
1898
        av_bprint_finalize(&buf_script, NULL);
1899
        if (is_last_report) {
1900
            if ((ret = avio_closep(&progress_avio)) < 0)
1901
                av_log(NULL, AV_LOG_ERROR,
1902
                       "Error closing progress log, loss of information possible: %s\n", av_err2str(ret));
1903
        }
1904
    }
1905
1906
6070
    first_report = 0;
1907
1908
6070
    if (is_last_report)
1909
6008
        print_final_stats(total_size);
1910
}
1911
1912
1
static void ifilter_parameters_from_codecpar(InputFilter *ifilter, AVCodecParameters *par)
1913
{
1914
    // We never got any input. Set a fake format, which will
1915
    // come from libavformat.
1916
1
    ifilter->format                 = par->format;
1917
1
    ifilter->sample_rate            = par->sample_rate;
1918
1
    ifilter->channels               = par->channels;
1919
1
    ifilter->channel_layout         = par->channel_layout;
1920
1
    ifilter->width                  = par->width;
1921
1
    ifilter->height                 = par->height;
1922
1
    ifilter->sample_aspect_ratio    = par->sample_aspect_ratio;
1923
1
}
1924
1925
6010
static void flush_encoders(void)
1926
{
1927
    int i, ret;
1928
1929
12201
    for (i = 0; i < nb_output_streams; i++) {
1930
6193
        OutputStream   *ost = output_streams[i];
1931
6193
        AVCodecContext *enc = ost->enc_ctx;
1932
6193
        OutputFile      *of = output_files[ost->file_index];
1933
1934
6193
        if (!ost->encoding_needed)
1935
347
            continue;
1936
1937
        // Try to enable encoding with no input frames.
1938
        // Maybe we should just let encoding fail instead.
1939
5846
        if (!ost->initialized) {
1940
2
            FilterGraph *fg = ost->filter->graph;
1941
1942
2
            av_log(NULL, AV_LOG_WARNING,
1943
                   "Finishing stream %d:%d without any data written to it.\n",
1944
2
                   ost->file_index, ost->st->index);
1945
1946

2
            if (ost->filter && !fg->graph) {
1947
                int x;
1948
2
                for (x = 0; x < fg->nb_inputs; x++) {
1949
                    InputFilter *ifilter = fg->inputs[x];
1950
                    if (ifilter->format < 0)
1951
                        ifilter_parameters_from_codecpar(ifilter, ifilter->ist->st->codecpar);
1952
                }
1953
1954
2
                if (!ifilter_has_all_input_formats(fg))
1955
                    continue;
1956
1957
2
                ret = configure_filtergraph(fg);
1958
2
                if (ret < 0) {
1959
2
                    av_log(NULL, AV_LOG_ERROR, "Error configuring filter graph\n");
1960
2
                    exit_program(1);
1961
                }
1962
1963
                finish_output_stream(ost);
1964
            }
1965
1966
            init_output_stream_wrapper(ost, NULL, 1);
1967
        }
1968
1969

5844
        if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
1970
32
            continue;
1971
1972
262
        for (;;) {
1973
6074
            const char *desc = NULL;
1974
6074
            AVPacket *pkt = ost->pkt;
1975
            int pkt_size;
1976
1977
6074
            switch (enc->codec_type) {
1978
1170
            case AVMEDIA_TYPE_AUDIO:
1979
1170
                desc   = "audio";
1980
1170
                break;
1981
4904
            case AVMEDIA_TYPE_VIDEO:
1982
4904
                desc   = "video";
1983
4904
                break;
1984
            default:
1985
                av_assert0(0);
1986
            }
1987
1988
6074
            update_benchmark(NULL);
1989
1990
6074
            av_packet_unref(pkt);
1991
11886
            while ((ret = avcodec_receive_packet(enc, pkt)) == AVERROR(EAGAIN)) {
1992
5812
                ret = avcodec_send_frame(enc, NULL);
1993
5812
                if (ret < 0) {
1994
                    av_log(NULL, AV_LOG_FATAL, "%s encoding failed: %s\n",
1995
                           desc,
1996
                           av_err2str(ret));
1997
                    exit_program(1);
1998
                }
1999
            }
2000
2001
6074
            update_benchmark("flush_%s %d.%d", desc, ost->file_index, ost->index);
2002

6074
            if (ret < 0 && ret != AVERROR_EOF) {
2003
                av_log(NULL, AV_LOG_FATAL, "%s encoding failed: %s\n",
2004
                       desc,
2005
                       av_err2str(ret));
2006
                exit_program(1);
2007
            }
2008

6074
            if (ost->logfile && enc->stats_out) {
2009
                fprintf(ost->logfile, "%s", enc->stats_out);
2010
            }
2011
6074
            if (ret == AVERROR_EOF) {
2012
5812
                output_packet(of, pkt, ost, 1);
2013
5812
                break;
2014
            }
2015
262
            if (ost->finished & MUXER_FINISHED) {
2016
                av_packet_unref(pkt);
2017
                continue;
2018
            }
2019
262
            av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
2020
262
            pkt_size = pkt->size;
2021
262
            output_packet(of, pkt, ost, 0);
2022

262
            if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename) {
2023
                do_video_stats(ost, pkt_size);
2024
            }
2025
        }
2026
    }
2027
6008
}
2028
2029
/*
2030
 * Check whether a packet from ist should be written into ost at this time
2031
 */
2032
422558
static int check_output_constraints(InputStream *ist, OutputStream *ost)
2033
{
2034
422558
    OutputFile *of = output_files[ost->file_index];
2035
422558
    int ist_index  = input_files[ist->file_index]->ist_index + ist->st->index;
2036
2037
422558
    if (ost->source_index != ist_index)
2038
22939
        return 0;
2039
2040
399619
    if (ost->finished)
2041
4157
        return 0;
2042
2043

395462
    if (of->start_time != AV_NOPTS_VALUE && ist->pts < of->start_time)
2044
55
        return 0;
2045
2046
395407
    return 1;
2047
}
2048
2049
34849
static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
2050
{
2051
34849
    OutputFile *of = output_files[ost->file_index];
2052
34849
    InputFile   *f = input_files [ist->file_index];
2053
34849
    int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
2054
34849
    int64_t ost_tb_start_time = av_rescale_q(start_time, AV_TIME_BASE_Q, ost->mux_timebase);
2055
34849
    AVPacket *opkt = ost->pkt;
2056
2057
34849
    av_packet_unref(opkt);
2058
    // EOF: flush output bitstream filters.
2059
34849
    if (!pkt) {
2060
5
        output_packet(of, opkt, ost, 1);
2061
57
        return;
2062
    }
2063
2064

34844
    if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
2065
36
        !ost->copy_initial_nonkeyframes)
2066
36
        return;
2067
2068

34808
    if (!ost->frame_number && !ost->copy_prior_start) {
2069
        int64_t comp_start = start_time;
2070
        if (copy_ts && f->start_time != AV_NOPTS_VALUE)
2071
            comp_start = FFMAX(start_time, f->start_time + f->ts_offset);
2072
        if (pkt->pts == AV_NOPTS_VALUE ?
2073
            ist->pts < comp_start :
2074
            pkt->pts < av_rescale_q(comp_start, AV_TIME_BASE_Q, ist->st->time_base))
2075
            return;
2076
    }
2077
2078
34808
    if (of->recording_time != INT64_MAX &&
2079
523
        ist->pts >= of->recording_time + start_time) {
2080
14
        close_output_stream(ost);
2081
14
        return;
2082
    }
2083
2084
34794
    if (f->recording_time != INT64_MAX) {
2085
148
        start_time = f->ctx->start_time;
2086

148
        if (f->start_time != AV_NOPTS_VALUE && copy_ts)
2087
74
            start_time += f->start_time;
2088
148
        if (ist->pts >= f->recording_time + start_time) {
2089
2
            close_output_stream(ost);
2090
2
            return;
2091
        }
2092
    }
2093
2094
    /* force the input stream PTS */
2095
34792
    if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
2096
10413
        ost->sync_opts++;
2097
2098
34792
    if (av_packet_ref(opkt, pkt) < 0)
2099
        exit_program(1);
2100
2101
34792
    if (pkt->pts != AV_NOPTS_VALUE)
2102
29327
        opkt->pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->mux_timebase) - ost_tb_start_time;
2103
2104
34792
    if (pkt->dts == AV_NOPTS_VALUE) {
2105
5221
        opkt->dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ost->mux_timebase);
2106
29571
    } else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2107
23754
        int duration = av_get_audio_frame_duration(ist->dec_ctx, pkt->size);
2108
23754
        if(!duration)
2109
8333
            duration = ist->dec_ctx->frame_size;
2110
47508
        opkt->dts = av_rescale_delta(ist->st->time_base, pkt->dts,
2111
23754
                                    (AVRational){1, ist->dec_ctx->sample_rate}, duration,
2112
                                    &ist->filter_in_rescale_delta_last, ost->mux_timebase);
2113
        /* dts will be set immediately afterwards to what pts is now */
2114
23754
        opkt->pts = opkt->dts - ost_tb_start_time;
2115
    } else
2116
5817
        opkt->dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->mux_timebase);
2117
34792
    opkt->dts -= ost_tb_start_time;
2118
2119
34792
    opkt->duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->mux_timebase);
2120
2121
34792
    output_packet(of, opkt, ost, 0);
2122
}
2123
2124
1408
int guess_input_channel_layout(InputStream *ist)
2125
{
2126
1408
    AVCodecContext *dec = ist->dec_ctx;
2127
2128
1408
    if (!dec->channel_layout) {
2129
        char layout_name[256];
2130
2131
759
        if (dec->channels > ist->guess_layout_max)
2132
9
            return 0;
2133
755
        dec->channel_layout = av_get_default_channel_layout(dec->channels);
2134
755
        if (!dec->channel_layout)
2135
5
            return 0;
2136
750
        av_get_channel_layout_string(layout_name, sizeof(layout_name),
2137
                                     dec->channels, dec->channel_layout);
2138
750
        av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for Input Stream "
2139
750
               "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name);
2140
    }
2141
1399
    return 1;
2142
}
2143
2144
737213
static void check_decode_result(InputStream *ist, int *got_output, int ret)
2145
{
2146

737213
    if (*got_output || ret<0)
2147
380670
        decode_error_stat[ret<0] ++;
2148
2149

737213
    if (ret < 0 && exit_on_error)
2150
        exit_program(1);
2151
2152

737213
    if (*got_output && ist) {
2153

379483
        if (ist->decoded_frame->decode_error_flags || (ist->decoded_frame->flags & AV_FRAME_FLAG_CORRUPT)) {
2154
36
            av_log(NULL, exit_on_error ? AV_LOG_FATAL : AV_LOG_WARNING,
2155
36
                   "%s: corrupt decoded frame in stream %d\n", input_files[ist->file_index]->ctx->url, ist->st->index);
2156
36
            if (exit_on_error)
2157
                exit_program(1);
2158
        }
2159
    }
2160
737213
}
2161
2162
// Filters can be configured only if the formats of all inputs are known.
2163
636540
static int ifilter_has_all_input_formats(FilterGraph *fg)
2164
{
2165
    int i;
2166
686922
    for (i = 0; i < fg->nb_inputs; i++) {
2167

681051
        if (fg->inputs[i]->format < 0 && (fg->inputs[i]->type == AVMEDIA_TYPE_AUDIO ||
2168
630650
                                          fg->inputs[i]->type == AVMEDIA_TYPE_VIDEO))
2169
630669
            return 0;
2170
    }
2171
5871
    return 1;
2172
}
2173
2174
379885
static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame)
2175
{
2176
379885
    FilterGraph *fg = ifilter->graph;
2177
    int need_reinit, ret, i;
2178
2179
    /* determine if the parameters for this input changed */
2180
379885
    need_reinit = ifilter->format != frame->format;
2181
2182
379885
    switch (ifilter->ist->st->codecpar->codec_type) {
2183
280523
    case AVMEDIA_TYPE_AUDIO:
2184
840439
        need_reinit |= ifilter->sample_rate    != frame->sample_rate ||
2185

559915
                       ifilter->channels       != frame->channels ||
2186
279392
                       ifilter->channel_layout != frame->channel_layout;
2187
280523
        break;
2188
99362
    case AVMEDIA_TYPE_VIDEO:
2189
194045
        need_reinit |= ifilter->width  != frame->width ||
2190
94683
                       ifilter->height != frame->height;
2191
99362
        break;
2192
    }
2193
2194

379885
    if (!ifilter->ist->reinit_filters && fg->graph)
2195
        need_reinit = 0;
2196
2197
379885
    if (!!ifilter->hw_frames_ctx != !!frame->hw_frames_ctx ||
2198

379885
        (ifilter->hw_frames_ctx && ifilter->hw_frames_ctx->data != frame->hw_frames_ctx->data))
2199
        need_reinit = 1;
2200
2201
379885
    if (need_reinit) {
2202
5823
        ret = ifilter_parameters_from_frame(ifilter, frame);
2203
5823
        if (ret < 0)
2204
            return ret;
2205
    }
2206
2207
    /* (re)init the graph if possible, otherwise buffer the frame and return */
2208

379885
    if (need_reinit || !fg->graph) {
2209
11648
        for (i = 0; i < fg->nb_inputs; i++) {
2210
5844
            if (!ifilter_has_all_input_formats(fg)) {
2211
19
                AVFrame *tmp = av_frame_clone(frame);
2212
19
                if (!tmp)
2213
                    return AVERROR(ENOMEM);
2214
19
                av_frame_unref(frame);
2215
2216
19
                if (!av_fifo_space(ifilter->frame_queue)) {
2217
                    ret = av_fifo_realloc2(ifilter->frame_queue, 2 * av_fifo_size(ifilter->frame_queue));
2218
                    if (ret < 0) {
2219
                        av_frame_free(&tmp);
2220
                        return ret;
2221
                    }
2222
                }
2223
19
                av_fifo_generic_write(ifilter->frame_queue, &tmp, sizeof(tmp), NULL);
2224
19
                return 0;
2225
            }
2226
        }
2227
2228
5804
        ret = reap_filters(1);
2229

5804
        if (ret < 0 && ret != AVERROR_EOF) {
2230
            av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
2231
            return ret;
2232
        }
2233
2234
5804
        ret = configure_filtergraph(fg);
2235
5804
        if (ret < 0) {
2236
            av_log(NULL, AV_LOG_ERROR, "Error reinitializing filters!\n");
2237
            return ret;
2238
        }
2239
    }
2240
2241
379866
    ret = av_buffersrc_add_frame_flags(ifilter->filter, frame, AV_BUFFERSRC_FLAG_PUSH);
2242
379866
    if (ret < 0) {
2243
        if (ret != AVERROR_EOF)
2244
            av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
2245
        return ret;
2246
    }
2247
2248
379866
    return 0;
2249
}
2250
2251
5778
static int ifilter_send_eof(InputFilter *ifilter, int64_t pts)
2252
{
2253
    int ret;
2254
2255
5778
    ifilter->eof = 1;
2256
2257
5778
    if (ifilter->filter) {
2258
5777
        ret = av_buffersrc_close(ifilter->filter, pts, AV_BUFFERSRC_FLAG_PUSH);
2259
5777
        if (ret < 0)
2260
            return ret;
2261
    } else {
2262
        // the filtergraph was never configured
2263
1
        if (ifilter->format < 0)
2264
1
            ifilter_parameters_from_codecpar(ifilter, ifilter->ist->st->codecpar);
2265

1
        if (ifilter->format < 0 && (ifilter->type == AVMEDIA_TYPE_AUDIO || ifilter->type == AVMEDIA_TYPE_VIDEO)) {
2266
            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);
2267
            return AVERROR_INVALIDDATA;
2268
        }
2269
    }
2270
2271
5778
    return 0;
2272
}
2273
2274
// This does not quite work like avcodec_decode_audio4/avcodec_decode_video2.
2275
// There is the following difference: if you got a frame, you must call
2276
// it again with pkt=NULL. pkt==NULL is treated differently from pkt->size==0
2277
// (pkt==NULL means get more output, pkt->size==0 is a flush/drain packet)
2278
741673
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
2279
{
2280
    int ret;
2281
2282
741673
    *got_frame = 0;
2283
2284
741673
    if (pkt) {
2285
362867
        ret = avcodec_send_packet(avctx, pkt);
2286
        // In particular, we don't expect AVERROR(EAGAIN), because we read all
2287
        // decoded frames with avcodec_receive_frame() until done.
2288

362867
        if (ret < 0 && ret != AVERROR_EOF)
2289
381
            return ret;
2290
    }
2291
2292
741292
    ret = avcodec_receive_frame(avctx, frame);
2293

741292
    if (ret < 0 && ret != AVERROR(EAGAIN))
2294
5775
        return ret;
2295
735517
    if (ret >= 0)
2296
379483
        *got_frame = 1;
2297
2298
735517
    return 0;
2299
}
2300
2301
379483
static int send_frame_to_filters(InputStream *ist, AVFrame *decoded_frame)
2302
{
2303
    int i, ret;
2304
    AVFrame *f;
2305
2306
    av_assert1(ist->nb_filters > 0); /* ensure ret is initialized */
2307
759368
    for (i = 0; i < ist->nb_filters; i++) {
2308
379885
        if (i < ist->nb_filters - 1) {
2309
402
            f = ist->filter_frame;
2310
402
            ret = av_frame_ref(f, decoded_frame);
2311
402
            if (ret < 0)
2312
                break;
2313
        } else
2314
379483
            f = decoded_frame;
2315
379885
        ret = ifilter_send_frame(ist->filters[i], f);
2316
379885
        if (ret == AVERROR_EOF)
2317
            ret = 0; /* ignore */
2318
379885
        if (ret < 0) {
2319
            av_log(NULL, AV_LOG_ERROR,
2320
                   "Failed to inject frame into filter network: %s\n", av_err2str(ret));
2321
            break;
2322
        }
2323
    }
2324
379483
    return ret;
2325
}
2326
2327
534153
static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output,
2328
                        int *decode_failed)
2329
{
2330
    AVFrame *decoded_frame;
2331
534153
    AVCodecContext *avctx = ist->dec_ctx;
2332
534153
    int ret, err = 0;
2333
    AVRational decoded_frame_tb;
2334
2335

534153
    if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
2336
        return AVERROR(ENOMEM);
2337

534153
    if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
2338
        return AVERROR(ENOMEM);
2339
534153
    decoded_frame = ist->decoded_frame;
2340
2341
534153
    update_benchmark(NULL);
2342
534153
    ret = decode(avctx, decoded_frame, got_output, pkt);
2343
534153
    update_benchmark("decode_audio %d.%d", ist->file_index, ist->st->index);
2344
534153
    if (ret < 0)
2345
1139
        *decode_failed = 1;
2346
2347

534153
    if (ret >= 0 && avctx->sample_rate <= 0) {
2348
        av_log(avctx, AV_LOG_ERROR, "Sample rate %d invalid\n", avctx->sample_rate);
2349
        ret = AVERROR_INVALIDDATA;
2350
    }
2351
2352
534153
    if (ret != AVERROR_EOF)
2353
533025
        check_decode_result(ist, got_output, ret);
2354
2355

534153
    if (!*got_output || ret < 0)
2356
254030
        return ret;
2357
2358
280123
    ist->samples_decoded += decoded_frame->nb_samples;
2359
280123
    ist->frames_decoded++;
2360
2361
    /* increment next_dts to use for the case where the input stream does not
2362
       have timestamps or there are multiple frames in the packet */
2363
280123
    ist->next_pts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
2364
280123
                     avctx->sample_rate;
2365
280123
    ist->next_dts += ((int64_t)AV_TIME_BASE * decoded_frame->nb_samples) /
2366
280123
                     avctx->sample_rate;
2367
2368
280123
    if (decoded_frame->pts != AV_NOPTS_VALUE) {
2369
251618
        decoded_frame_tb   = ist->st->time_base;
2370

28505
    } else if (pkt && pkt->pts != AV_NOPTS_VALUE) {
2371
3
        decoded_frame->pts = pkt->pts;
2372
3
        decoded_frame_tb   = ist->st->time_base;
2373
    }else {
2374
28502
        decoded_frame->pts = ist->dts;
2375
28502
        decoded_frame_tb   = AV_TIME_BASE_Q;
2376
    }
2377
280123
    if (decoded_frame->pts != AV_NOPTS_VALUE)
2378
280123
        decoded_frame->pts = av_rescale_delta(decoded_frame_tb, decoded_frame->pts,
2379
280123
                                              (AVRational){1, avctx->sample_rate}, decoded_frame->nb_samples, &ist->filter_in_rescale_delta_last,
2380
280123
                                              (AVRational){1, avctx->sample_rate});
2381
280123
    ist->nb_samples = decoded_frame->nb_samples;
2382
280123
    err = send_frame_to_filters(ist, decoded_frame);
2383
2384
280123
    av_frame_unref(ist->filter_frame);
2385
280123
    av_frame_unref(decoded_frame);
2386
280123
    return err < 0 ? err : ret;
2387
}
2388
2389
208993
static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output, int64_t *duration_pts, int eof,
2390
                        int *decode_failed)
2391
{
2392
    AVFrame *decoded_frame;
2393
208993
    int i, ret = 0, err = 0;
2394
    int64_t best_effort_timestamp;
2395
208993
    int64_t dts = AV_NOPTS_VALUE;
2396
2397
    // With fate-indeo3-2, we're getting 0-sized packets before EOF for some
2398
    // reason. This seems like a semi-critical bug. Don't trigger EOF, and
2399
    // skip the packet.
2400

208993
    if (!eof && pkt && pkt->size == 0)
2401
1473
        return 0;
2402
2403

207520
    if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
2404
        return AVERROR(ENOMEM);
2405

207520
    if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
2406
        return AVERROR(ENOMEM);
2407
207520
    decoded_frame = ist->decoded_frame;
2408
207520
    if (ist->dts != AV_NOPTS_VALUE)
2409
207103
        dts = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ist->st->time_base);
2410
207520
    if (pkt) {
2411
108809
        pkt->dts = dts; // ffmpeg.c probably shouldn't do this
2412
    }
2413
2414
    // The old code used to set dts on the drain packet, which does not work
2415
    // with the new API anymore.
2416
207520
    if (eof) {
2417
5291
        void *new = av_realloc_array(ist->dts_buffer, ist->nb_dts_buffer + 1, sizeof(ist->dts_buffer[0]));
2418
5291
        if (!new)
2419
            return AVERROR(ENOMEM);
2420
5291
        ist->dts_buffer = new;
2421
5291
        ist->dts_buffer[ist->nb_dts_buffer++] = dts;
2422
    }
2423
2424
207520
    update_benchmark(NULL);
2425
207520
    ret = decode(ist->dec_ctx, decoded_frame, got_output, pkt);
2426
207520
    update_benchmark("decode_video %d.%d", ist->file_index, ist->st->index);
2427
207520
    if (ret < 0)
2428
5017
        *decode_failed = 1;
2429
2430
    // The following line may be required in some cases where there is no parser
2431
    // or the parser does not has_b_frames correctly
2432
207520
    if (ist->st->codecpar->video_delay < ist->dec_ctx->has_b_frames) {
2433
1
        if (ist->dec_ctx->codec_id == AV_CODEC_ID_H264) {
2434
1
            ist->st->codecpar->video_delay = ist->dec_ctx->has_b_frames;
2435
        } else
2436
            av_log(ist->dec_ctx, AV_LOG_WARNING,
2437
                   "video_delay is larger in decoder than demuxer %d > %d.\n"
2438
                   "If you want to help, upload a sample "
2439
                   "of this file to https://streams.videolan.org/upload/ "
2440
                   "and contact the ffmpeg-devel mailing list. (ffmpeg-devel@ffmpeg.org)\n",
2441
                   ist->dec_ctx->has_b_frames,
2442
                   ist->st->codecpar->video_delay);
2443
    }
2444
2445
207520
    if (ret != AVERROR_EOF)
2446
202878
        check_decode_result(ist, got_output, ret);
2447
2448

207520
    if (*got_output && ret >= 0) {
2449
99360
        if (ist->dec_ctx->width  != decoded_frame->width ||
2450
99351
            ist->dec_ctx->height != decoded_frame->height ||
2451
99349
            ist->dec_ctx->pix_fmt != decoded_frame->format) {
2452
15
            av_log(NULL, AV_LOG_DEBUG, "Frame parameters mismatch context %d,%d,%d != %d,%d,%d\n",
2453
                decoded_frame->width,
2454
                decoded_frame->height,
2455
                decoded_frame->format,
2456
15
                ist->dec_ctx->width,
2457
15
                ist->dec_ctx->height,
2458
15
                ist->dec_ctx->pix_fmt);
2459
        }
2460
    }
2461
2462

207520
    if (!*got_output || ret < 0)
2463
108160
        return ret;
2464
2465
99360
    if(ist->top_field_first>=0)
2466
        decoded_frame->top_field_first = ist->top_field_first;
2467
2468
99360
    ist->frames_decoded++;
2469
2470

99360
    if (ist->hwaccel_retrieve_data && decoded_frame->format == ist->hwaccel_pix_fmt) {
2471
        err = ist->hwaccel_retrieve_data(ist->dec_ctx, decoded_frame);
2472
        if (err < 0)
2473
            goto fail;
2474
    }
2475
99360
    ist->hwaccel_retrieved_pix_fmt = decoded_frame->format;
2476
2477
99360
    best_effort_timestamp= decoded_frame->best_effort_timestamp;
2478
99360
    *duration_pts = decoded_frame->pkt_duration;
2479
2480
99360
    if (ist->framerate.num)
2481
103
        best_effort_timestamp = ist->cfr_next_pts++;
2482
2483

99360
    if (eof && best_effort_timestamp == AV_NOPTS_VALUE && ist->nb_dts_buffer > 0) {
2484
528
        best_effort_timestamp = ist->dts_buffer[0];
2485
2486
528
        for (i = 0; i < ist->nb_dts_buffer - 1; i++)
2487
            ist->dts_buffer[i] = ist->dts_buffer[i + 1];
2488
528
        ist->nb_dts_buffer--;
2489
    }
2490
2491
99360
    if(best_effort_timestamp != AV_NOPTS_VALUE) {
2492
99360
        int64_t ts = av_rescale_q(decoded_frame->pts = best_effort_timestamp, ist->st->time_base, AV_TIME_BASE_Q);
2493
2494
99360
        if (ts != AV_NOPTS_VALUE)
2495
99360
            ist->next_pts = ist->pts = ts;
2496
    }
2497
2498
99360
    if (debug_ts) {
2499
        av_log(NULL, AV_LOG_INFO, "decoder -> ist_index:%d type:video "
2500
               "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",
2501
               ist->st->index, av_ts2str(decoded_frame->pts),
2502
               av_ts2timestr(decoded_frame->pts, &ist->st->time_base),
2503
               best_effort_timestamp,
2504
               av_ts2timestr(best_effort_timestamp, &ist->st->time_base),
2505
               decoded_frame->key_frame, decoded_frame->pict_type,
2506
               ist->st->time_base.num, ist->st->time_base.den);
2507
    }
2508
2509
99360
    if (ist->st->sample_aspect_ratio.num)
2510
5010
        decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
2511
2512
99360
    err = send_frame_to_filters(ist, decoded_frame);
2513
2514
99360
fail:
2515
99360
    av_frame_unref(ist->filter_frame);
2516
99360
    av_frame_unref(decoded_frame);
2517
99360
    return err < 0 ? err : ret;
2518
}
2519
2520
1310
static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
2521
                               int *decode_failed)
2522
{
2523
    AVSubtitle subtitle;
2524
1310
    int free_sub = 1;
2525
1310
    int i, ret = avcodec_decode_subtitle2(ist->dec_ctx,
2526
                                          &subtitle, got_output, pkt);
2527
2528
1310
    check_decode_result(NULL, got_output, ret);
2529
2530

1310
    if (ret < 0 || !*got_output) {
2531
588
        *decode_failed = 1;
2532
588
        if (!pkt->size)
2533
35
            sub2video_flush(ist);
2534
588
        return ret;
2535
    }
2536
2537
722
    if (ist->fix_sub_duration) {
2538
        int end = 1;
2539
        if (ist->prev_sub.got_output) {
2540
            end = av_rescale(subtitle.pts - ist->prev_sub.subtitle.pts,
2541
                             1000, AV_TIME_BASE);
2542
            if (end < ist->prev_sub.subtitle.end_display_time) {
2543
                av_log(ist->dec_ctx, AV_LOG_DEBUG,
2544
                       "Subtitle duration reduced from %"PRId32" to %d%s\n",
2545
                       ist->prev_sub.subtitle.end_display_time, end,
2546
                       end <= 0 ? ", dropping it" : "");
2547
                ist->prev_sub.subtitle.end_display_time = end;
2548
            }
2549
        }
2550
        FFSWAP(int,        *got_output, ist->prev_sub.got_output);
2551
        FFSWAP(int,        ret,         ist->prev_sub.ret);
2552
        FFSWAP(AVSubtitle, subtitle,    ist->prev_sub.subtitle);
2553
        if (end <= 0)
2554
            goto out;
2555
    }
2556
2557
722
    if (!*got_output)
2558
        return ret;
2559
2560
722
    if (ist->sub2video.frame) {
2561
88
        sub2video_update(ist, INT64_MIN, &subtitle);
2562
634
    } else if (ist->nb_filters) {
2563
        if (!ist->sub2video.sub_queue)
2564
            ist->sub2video.sub_queue = av_fifo_alloc(8 * sizeof(AVSubtitle));
2565
        if (!ist->sub2video.sub_queue)
2566
            exit_program(1);
2567
        if (!av_fifo_space(ist->sub2video.sub_queue)) {
2568
            ret = av_fifo_realloc2(ist->sub2video.sub_queue, 2 * av_fifo_size(ist->sub2video.sub_queue));
2569
            if (ret < 0)
2570
                exit_program(1);
2571
        }
2572
        av_fifo_generic_write(ist->sub2video.sub_queue, &subtitle, sizeof(subtitle), NULL);
2573
        free_sub = 0;
2574
    }
2575
2576
722
    if (!subtitle.num_rects)
2577
9
        goto out;
2578
2579
713
    ist->frames_decoded++;
2580
2581
1470
    for (i = 0; i < nb_output_streams; i++) {
2582
757
        OutputStream *ost = output_streams[i];
2583
2584

757
        if (!ost->pkt && !(ost->pkt = av_packet_alloc()))
2585
            exit_program(1);
2586

757
        if (!check_output_constraints(ist, ost) || !ost->encoding_needed
2587
712
            || ost->enc->type != AVMEDIA_TYPE_SUBTITLE)
2588
89
            continue;
2589
2590
668
        do_subtitle_out(output_files[ost->file_index], ost, &subtitle);
2591
    }
2592
2593
713
out:
2594
722
    if (free_sub)
2595
722
        avsubtitle_free(&subtitle);
2596
722
    return ret;
2597
}
2598
2599
5805
static int send_filter_eof(InputStream *ist)
2600
{
2601
    int i, ret;
2602
    /* TODO keep pts also in stream time base to avoid converting back */
2603
5805
    int64_t pts = av_rescale_q_rnd(ist->pts, AV_TIME_BASE_Q, ist->st->time_base,
2604
                                   AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
2605
2606
11583
    for (i = 0; i < ist->nb_filters; i++) {
2607
5778
        ret = ifilter_send_eof(ist->filters[i], pts);
2608
5778
        if (ret < 0)
2609
            return ret;
2610
    }
2611
5805
    return 0;
2612
}
2613
2614
/* pkt = NULL means EOF (needed to flush decoder buffers) */
2615
401678
static int process_input_packet(InputStream *ist, const AVPacket *pkt, int no_eof)
2616
{
2617
401678
    int ret = 0, i;
2618
401678
    int repeating = 0;
2619
401678
    int eof_reached = 0;
2620
2621
    AVPacket *avpkt;
2622
2623

401678
    if (!ist->pkt && !(ist->pkt = av_packet_alloc()))
2624
        return AVERROR(ENOMEM);
2625
401678
    avpkt = ist->pkt;
2626
2627
401678
    if (!ist->saw_first_ts) {
2628
6183
        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;
2629
6183
        ist->pts = 0;
2630

6183
        if (pkt && pkt->pts != AV_NOPTS_VALUE && !ist->decoding_needed) {
2631
247
            ist->dts += av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q);
2632
247
            ist->pts = ist->dts; //unused but better to set it to a value thats not totally wrong
2633
        }
2634
6183
        ist->saw_first_ts = 1;
2635
    }
2636
2637
401678
    if (ist->next_dts == AV_NOPTS_VALUE)
2638
6596
        ist->next_dts = ist->dts;
2639
401678
    if (ist->next_pts == AV_NOPTS_VALUE)
2640
6183
        ist->next_pts = ist->pts;
2641
2642
401678
    if (pkt) {
2643
395125
        av_packet_unref(avpkt);
2644
395125
        ret = av_packet_ref(avpkt, pkt);
2645
395125
        if (ret < 0)
2646
            return ret;
2647
    }
2648
2649

401678
    if (pkt && pkt->dts != AV_NOPTS_VALUE) {
2650
358564
        ist->next_dts = ist->dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
2651

358564
        if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_VIDEO || !ist->decoding_needed)
2652
283773
            ist->next_pts = ist->pts = ist->dts;
2653
    }
2654
2655
    // while we have more to decode or while the decoder did output something on EOF
2656
781206
    while (ist->decoding_needed) {
2657
745178
        int64_t duration_dts = 0;
2658
745178
        int64_t duration_pts = 0;
2659
745178
        int got_output = 0;
2660
745178
        int decode_failed = 0;
2661
2662
745178
        ist->pts = ist->next_pts;
2663
745178
        ist->dts = ist->next_dts;
2664
2665

745178
        switch (ist->dec_ctx->codec_type) {
2666
534153
        case AVMEDIA_TYPE_AUDIO:
2667
534153
            ret = decode_audio    (ist, repeating ? NULL : avpkt, &got_output,
2668
                                   &decode_failed);
2669
534153
            av_packet_unref(avpkt);
2670
534153
            break;
2671
208993
        case AVMEDIA_TYPE_VIDEO:
2672
208993
            ret = decode_video    (ist, repeating ? NULL : avpkt, &got_output, &duration_pts, !pkt,
2673
                                   &decode_failed);
2674

208993
            if (!repeating || !pkt || got_output) {
2675

110292
                if (pkt && pkt->duration) {
2676
102708
                    duration_dts = av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
2677

7584
                } else if(ist->dec_ctx->framerate.num != 0 && ist->dec_ctx->framerate.den != 0) {
2678
6572
                    int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict+1 : ist->dec_ctx->ticks_per_frame;
2679
6572
                    duration_dts = ((int64_t)AV_TIME_BASE *
2680
6572
                                    ist->dec_ctx->framerate.den * ticks) /
2681
6572
                                    ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame;
2682
                }
2683
2684

110292
                if(ist->dts != AV_NOPTS_VALUE && duration_dts) {
2685
109280
                    ist->next_dts += duration_dts;
2686
                }else
2687
1012
                    ist->next_dts = AV_NOPTS_VALUE;
2688
            }
2689
2690
208993
            if (got_output) {
2691
99360
                if (duration_pts > 0) {
2692
97081
                    ist->next_pts += av_rescale_q(duration_pts, ist->st->time_base, AV_TIME_BASE_Q);
2693
                } else {
2694
2279
                    ist->next_pts += duration_dts;
2695
                }
2696
            }
2697
208993
            av_packet_unref(avpkt);
2698
208993
            break;
2699
2032
        case AVMEDIA_TYPE_SUBTITLE:
2700
2032
            if (repeating)
2701
722
                break;
2702
1310
            ret = transcode_subtitles(ist, avpkt, &got_output, &decode_failed);
2703

1310
            if (!pkt && ret >= 0)
2704
35
                ret = AVERROR_EOF;
2705
1310
            av_packet_unref(avpkt);
2706
1310
            break;
2707
        default:
2708
            return -1;
2709
        }
2710
2711
745178
        if (ret == AVERROR_EOF) {
2712
5805
            eof_reached = 1;
2713
365650
            break;
2714
        }
2715
2716
739373
        if (ret < 0) {
2717
465
            if (decode_failed) {
2718
930
                av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d: %s\n",
2719
465
                       ist->file_index, ist->st->index, av_err2str(ret));
2720
            } else {
2721
                av_log(NULL, AV_LOG_FATAL, "Error while processing the decoded "
2722
                       "data for stream #%d:%d\n", ist->file_index, ist->st->index);
2723
            }
2724

465
            if (!decode_failed || exit_on_error)
2725
                exit_program(1);
2726
465
            break;
2727
        }
2728
2729
738908
        if (got_output)
2730
380205
            ist->got_output = 1;
2731
2732
738908
        if (!got_output)
2733
358703
            break;
2734
2735
        // During draining, we might get multiple output frames in this loop.
2736
        // ffmpeg.c does not drain the filter chain on configuration changes,
2737
        // which means if we send multiple frames at once to the filters, and
2738
        // one of those frames changes configuration, the buffered frames will
2739
        // be lost. This can upset certain FATE tests.
2740
        // Decode only 1 frame per call on EOF to appease these FATE tests.
2741
        // The ideal solution would be to rewrite decoding to use the new
2742
        // decoding API in a better way.
2743
380205
        if (!pkt)
2744
677
            break;
2745
2746
379528
        repeating = 1;
2747
    }
2748
2749
    /* after flushing, send an EOF on all the filter inputs attached to the stream */
2750
    /* except when looping we need to flush but not to send an EOF */
2751


401678
    if (!pkt && ist->decoding_needed && eof_reached && !no_eof) {
2752
5805
        int ret = send_filter_eof(ist);
2753
5805
        if (ret < 0) {
2754
            av_log(NULL, AV_LOG_FATAL, "Error marking filters as finished\n");
2755
            exit_program(1);
2756
        }
2757
    }
2758
2759
    /* handle stream copy */
2760

401678
    if (!ist->decoding_needed && pkt) {
2761
35958
        ist->dts = ist->next_dts;
2762
35958
        switch (ist->dec_ctx->codec_type) {
2763
25105
        case AVMEDIA_TYPE_AUDIO:
2764
            av_assert1(pkt->duration >= 0);
2765
25105
            if (ist->dec_ctx->sample_rate) {
2766
25105
                ist->next_dts += ((int64_t)AV_TIME_BASE * ist->dec_ctx->frame_size) /
2767
25105
                                  ist->dec_ctx->sample_rate;
2768
            } else {
2769
                ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
2770
            }
2771
25105
            break;
2772
10273
        case AVMEDIA_TYPE_VIDEO:
2773
10273
            if (ist->framerate.num) {
2774
                // TODO: Remove work-around for c99-to-c89 issue 7
2775
79
                AVRational time_base_q = AV_TIME_BASE_Q;
2776
79
                int64_t next_dts = av_rescale_q(ist->next_dts, time_base_q, av_inv_q(ist->framerate));
2777
79
                ist->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
2778
10194
            } else if (pkt->duration) {
2779
9782
                ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
2780
412
            } else if(ist->dec_ctx->framerate.num != 0) {
2781
276
                int ticks= av_stream_get_parser(ist->st) ? av_stream_get_parser(ist->st)->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame;
2782
276
                ist->next_dts += ((int64_t)AV_TIME_BASE *
2783
276
                                  ist->dec_ctx->framerate.den * ticks) /
2784
276
                                  ist->dec_ctx->framerate.num / ist->dec_ctx->ticks_per_frame;
2785
            }
2786
10273
            break;
2787
        }
2788
35958
        ist->pts = ist->dts;
2789
35958
        ist->next_pts = ist->next_dts;
2790
    }
2791
823479
    for (i = 0; i < nb_output_streams; i++) {
2792
421801
        OutputStream *ost = output_streams[i];
2793
2794

421801
        if (!ost->pkt && !(ost->pkt = av_packet_alloc()))
2795
            exit_program(1);
2796

421801
        if (!check_output_constraints(ist, ost) || ost->encoding_needed)
2797
386952
            continue;
2798
2799
34849
        do_streamcopy(ist, ost, pkt);
2800
    }
2801
2802
401678
    return !eof_reached;
2803
}
2804
2805
static void print_sdp(void)
2806
{
2807
    char sdp[16384];
2808
    int i;
2809
    int j;
2810
    AVIOContext *sdp_pb;
2811
    AVFormatContext **avc;
2812
2813
    for (i = 0; i < nb_output_files; i++) {
2814
        if (!output_files[i]->header_written)
2815
            return;
2816
    }
2817
2818
    avc = av_malloc_array(nb_output_files, sizeof(*avc));
2819
    if (!avc)
2820
        exit_program(1);
2821
    for (i = 0, j = 0; i < nb_output_files; i++) {
2822
        if (!strcmp(output_files[i]->ctx->oformat->name, "rtp")) {
2823
            avc[j] = output_files[i]->ctx;
2824
            j++;
2825
        }
2826
    }
2827
2828
    if (!j)
2829
        goto fail;
2830
2831
    av_sdp_create(avc, j, sdp, sizeof(sdp));
2832
2833
    if (!sdp_filename) {
2834
        printf("SDP:\n%s\n", sdp);
2835
        fflush(stdout);
2836
    } else {
2837
        if (avio_open2(&sdp_pb, sdp_filename, AVIO_FLAG_WRITE, &int_cb, NULL) < 0) {
2838
            av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", sdp_filename);
2839
        } else {
2840
            avio_print(sdp_pb, sdp);
2841
            avio_closep(&sdp_pb);
2842
            av_freep(&sdp_filename);
2843
        }
2844
    }
2845
2846
fail:
2847
    av_freep(&avc);
2848
}
2849
2850
928
static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
2851
{
2852
928
    InputStream *ist = s->opaque;
2853
    const enum AVPixelFormat *p;
2854
    int ret;
2855
2856
2549
    for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
2857
2549
        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
2858
2549
        const AVCodecHWConfig  *config = NULL;
2859
        int i;
2860
2861
2549
        if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
2862
928
            break;
2863
2864
1621
        if (ist->hwaccel_id == HWACCEL_GENERIC ||
2865
1621
            ist->hwaccel_id == HWACCEL_AUTO) {
2866
            for (i = 0;; i++) {
2867
                config = avcodec_get_hw_config(s->codec, i);
2868
                if (!config)
2869
                    break;
2870
                if (!(config->methods &
2871
                      AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX))
2872
                    continue;
2873
                if (config->pix_fmt == *p)
2874
                    break;
2875
            }
2876
        }
2877
1621
        if (config) {
2878
            if (config->device_type != ist->hwaccel_device_type) {
2879
                // Different hwaccel offered, ignore.
2880
                continue;
2881
            }
2882
2883
            ret = hwaccel_decode_init(s);
2884
            if (ret < 0) {
2885
                if (ist->hwaccel_id == HWACCEL_GENERIC) {
2886
                    av_log(NULL, AV_LOG_FATAL,
2887
                           "%s hwaccel requested for input stream #%d:%d, "
2888
                           "but cannot be initialized.\n",
2889
                           av_hwdevice_get_type_name(config->device_type),
2890
                           ist->file_index, ist->st->index);
2891
                    return AV_PIX_FMT_NONE;
2892
                }
2893
                continue;
2894
            }
2895
        } else {
2896
1621
            const HWAccel *hwaccel = NULL;
2897
            int i;
2898
1621
            for (i = 0; hwaccels[i].name; i++) {
2899
                if (hwaccels[i].pix_fmt == *p) {
2900
                    hwaccel = &hwaccels[i];
2901
                    break;
2902
                }
2903
            }
2904
1621
            if (!hwaccel) {
2905
                // No hwaccel supporting this pixfmt.
2906
1621
                continue;
2907
            }
2908
            if (hwaccel->id != ist->hwaccel_id) {
2909
                // Does not match requested hwaccel.
2910
                continue;
2911
            }
2912
2913
            ret = hwaccel->init(s);
2914
            if (ret < 0) {
2915
                av_log(NULL, AV_LOG_FATAL,
2916
                       "%s hwaccel requested for input stream #%d:%d, "
2917
                       "but cannot be initialized.\n", hwaccel->name,
2918
                       ist->file_index, ist->st->index);
2919
                return AV_PIX_FMT_NONE;
2920
            }
2921
        }
2922
2923
        if (ist->hw_frames_ctx) {
2924
            s->hw_frames_ctx = av_buffer_ref(ist->hw_frames_ctx);
2925
            if (!s->hw_frames_ctx)
2926
                return AV_PIX_FMT_NONE;
2927
        }
2928
2929
        ist->hwaccel_pix_fmt = *p;
2930
        break;
2931
    }
2932
2933
928
    return *p;
2934
}
2935
2936
354253
static int get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
2937
{
2938
354253
    InputStream *ist = s->opaque;
2939
2940

354253
    if (ist->hwaccel_get_buffer && frame->format == ist->hwaccel_pix_fmt)
2941
        return ist->hwaccel_get_buffer(s, frame, flags);
2942
2943
354253
    return avcodec_default_get_buffer2(s, frame, flags);
2944
}
2945
2946
6404
static int init_input_stream(int ist_index, char *error, int error_len)
2947
{
2948
    int ret;
2949
6404
    InputStream *ist = input_streams[ist_index];
2950
2951
6404
    if (ist->decoding_needed) {
2952
5818
        const AVCodec *codec = ist->dec;
2953
5818
        if (!codec) {
2954
            snprintf(error, error_len, "Decoder (codec %s) not found for input stream #%d:%d",
2955
                    avcodec_get_name(ist->dec_ctx->codec_id), ist->file_index, ist->st->index);
2956
            return AVERROR(EINVAL);
2957
        }
2958
2959
5818
        ist->dec_ctx->opaque                = ist;
2960
5818
        ist->dec_ctx->get_format            = get_format;
2961
5818
        ist->dec_ctx->get_buffer2           = get_buffer;
2962
#if LIBAVCODEC_VERSION_MAJOR < 60
2963
5818
        ist->dec_ctx->thread_safe_callbacks = 1;
2964
#endif
2965
2966
5818
        if (ist->dec_ctx->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
2967
1
           (ist->decoding_needed & DECODING_FOR_OST)) {
2968
1
            av_dict_set(&ist->decoder_opts, "compute_edt", "1", AV_DICT_DONT_OVERWRITE);
2969
1
            if (ist->decoding_needed & DECODING_FOR_FILTER)
2970
                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");
2971
        }
2972
2973
5818
        av_dict_set(&ist->decoder_opts, "sub_text_format", "ass", AV_DICT_DONT_OVERWRITE);
2974
2975
        /* Useful for subtitles retiming by lavf (FIXME), skipping samples in
2976
         * audio, and video decoders such as cuvid or mediacodec */
2977
5818
        ist->dec_ctx->pkt_timebase = ist->st->time_base;
2978
2979
5818
        if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
2980
55
            av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
2981
        /* Attached pics are sparse, therefore we would not want to delay their decoding till EOF. */
2982
5818
        if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
2983
4
            av_dict_set(&ist->decoder_opts, "threads", "1", 0);
2984
2985
5818
        ret = hw_device_setup_for_decode(ist);
2986
5818
        if (ret < 0) {
2987
            snprintf(error, error_len, "Device setup failed for "
2988
                     "decoder on input stream #%d:%d : %s",
2989
                     ist->file_index, ist->st->index, av_err2str(ret));
2990
            return ret;
2991
        }
2992
2993
5818
        if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
2994
            if (ret == AVERROR_EXPERIMENTAL)
2995
                abort_codec_experimental(codec, 0);
2996
2997
            snprintf(error, error_len,
2998
                     "Error while opening decoder for input stream "
2999
                     "#%d:%d : %s",
3000
                     ist->file_index, ist->st->index, av_err2str(ret));
3001
            return ret;
3002
        }
3003
5818
        assert_avoptions(ist->decoder_opts);
3004
    }
3005
3006
6404
    ist->next_pts = AV_NOPTS_VALUE;
3007
6404
    ist->next_dts = AV_NOPTS_VALUE;
3008
3009
6404
    return 0;
3010
}
3011
3012
17846
static InputStream *get_input_stream(OutputStream *ost)
3013
{
3014
17846
    if (ost->source_index >= 0)
3015
17663
        return input_streams[ost->source_index];
3016
183
    return NULL;
3017
}
3018
3019
1
static int compare_int64(const void *a, const void *b)
3020
{
3021
1
    return FFDIFFSIGN(*(const int64_t *)a, *(const int64_t *)b);
3022
}
3023
3024
/* open the muxer when all the streams are initialized */
3025
6192
static int check_init_output_file(OutputFile *of, int file_index)
3026
{
3027
    int ret, i;
3028
3029
12561
    for (i = 0; i < of->ctx->nb_streams; i++) {
3030
6552
        OutputStream *ost = output_streams[of->ost_index + i];
3031
6552
        if (!ost->initialized)
3032
183
            return 0;
3033
    }
3034
3035
6009
    of->ctx->interrupt_callback = int_cb;
3036
3037
6009
    ret = avformat_write_header(of->ctx, &of->opts);
3038
6009
    if (ret < 0) {
3039
        av_log(NULL, AV_LOG_ERROR,
3040
               "Could not write header for output file #%d "
3041
               "(incorrect codec parameters ?): %s\n",
3042
               file_index, av_err2str(ret));
3043
        return ret;
3044
    }
3045
    //assert_avoptions(of->opts);
3046
6009
    of->header_written = 1;
3047
3048
6009
    av_dump_format(of->ctx, file_index, of->ctx->url, 1);
3049
6009
    nb_output_dumped++;
3050
3051

6009
    if (sdp_filename || want_sdp)
3052
        print_sdp();
3053
3054
    /* flush the muxing queues */
3055
12200
    for (i = 0; i < of->ctx->nb_streams; i++) {
3056
6191
        OutputStream *ost = output_streams[of->ost_index + i];
3057
3058
        /* try to improve muxing time_base (only possible if nothing has been written yet) */
3059
6191
        if (!av_fifo_size(ost->muxing_queue))
3060
6098
            ost->mux_timebase = ost->st->time_base;
3061
3062
6500
        while (av_fifo_size(ost->muxing_queue)) {
3063
            AVPacket *pkt;
3064
309
            av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), NULL);
3065
309
            ost->muxing_queue_data_size -= pkt->size;
3066
309
            write_packet(of, pkt, ost, 1);
3067
309
            av_packet_free(&pkt);
3068
        }
3069
    }
3070
3071
6009
    return 0;
3072
}
3073
3074
6191
static int init_output_bsfs(OutputStream *ost)
3075
{
3076
6191
    AVBSFContext *ctx = ost->bsf_ctx;
3077
    int ret;
3078
3079
6191
    if (!ctx)
3080
6103
        return 0;
3081
3082
88
    ret = avcodec_parameters_copy(ctx->par_in, ost->st->codecpar);
3083
88
    if (ret < 0)
3084
        return ret;
3085
3086
88
    ctx->time_base_in = ost->st->time_base;
3087
3088
88
    ret = av_bsf_init(ctx);
3089
88
    if (ret < 0) {
3090
        av_log(NULL, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n",
3091
               ctx->filter->name);
3092
        return ret;
3093
    }
3094
3095
88
    ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
3096
88
    if (ret < 0)
3097
        return ret;
3098
88
    ost->st->time_base = ctx->time_base_out;
3099
3100
88
    return 0;
3101
}
3102
3103
346
static int init_output_stream_streamcopy(OutputStream *ost)
3104
{
3105
346
    OutputFile *of = output_files[ost->file_index];
3106
346
    InputStream *ist = get_input_stream(ost);
3107
346
    AVCodecParameters *par_dst = ost->st->codecpar;
3108
346
    AVCodecParameters *par_src = ost->ref_par;
3109
    AVRational sar;
3110
    int i, ret;
3111
346
    uint32_t codec_tag = par_dst->codec_tag;
3112
3113

346
    av_assert0(ist && !ost->filter);
3114
3115
346
    ret = avcodec_parameters_to_context(ost->enc_ctx, ist->st->codecpar);
3116
346
    if (ret >= 0)
3117
346
        ret = av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
3118
346
    if (ret < 0) {
3119
        av_log(NULL, AV_LOG_FATAL,
3120
               "Error setting up codec context options.\n");
3121
        return ret;
3122
    }
3123
3124
346
    ret = avcodec_parameters_from_context(par_src, ost->enc_ctx);
3125
346
    if (ret < 0) {
3126
        av_log(NULL, AV_LOG_FATAL,
3127
               "Error getting reference codec parameters.\n");
3128
        return ret;
3129
    }
3130
3131
346
    if (!codec_tag) {
3132
        unsigned int codec_tag_tmp;
3133
340
        if (!of->ctx->oformat->codec_tag ||
3134

100
            av_codec_get_id (of->ctx->oformat->codec_tag, par_src->codec_tag) == par_src->codec_id ||
3135
40
            !av_codec_get_tag2(of->ctx->oformat->codec_tag, par_src->codec_id, &codec_tag_tmp))
3136
307
            codec_tag = par_src->codec_tag;
3137
    }
3138
3139
346
    ret = avcodec_parameters_copy(par_dst, par_src);
3140
346
    if (ret < 0)
3141
        return ret;
3142
3143
346
    par_dst->codec_tag = codec_tag;
3144
3145
346
    if (!ost->frame_rate.num)
3146
345
        ost->frame_rate = ist->framerate;
3147
3148
346
    if (ost->frame_rate.num)
3149
2
        ost->st->avg_frame_rate = ost->frame_rate;
3150
    else
3151
344
        ost->st->avg_frame_rate = ist->st->avg_frame_rate;
3152
3153
346
    ret = avformat_transfer_internal_stream_timing_info(of->ctx->oformat, ost->st, ist->st, copy_tb);
3154
346
    if (ret < 0)
3155
        return ret;
3156
3157
    // copy timebase while removing common factors
3158

346
    if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0) {
3159
345
        if (ost->frame_rate.num)
3160
1
            ost->st->time_base = av_inv_q(ost->frame_rate);
3161
        else
3162
344
            ost->st->time_base = av_add_q(av_stream_get_codec_timebase(ost->st), (AVRational){0, 1});
3163
    }
3164
3165
    // copy estimated duration as a hint to the muxer
3166

346
    if (ost->st->duration <= 0 && ist->st->duration > 0)
3167
204
        ost->st->duration = av_rescale_q(ist->st->duration, ist->st->time_base, ost->st->time_base);
3168
3169
    // copy disposition
3170
346
    ost->st->disposition = ist->st->disposition;
3171
3172
346
    if (ist->st->nb_side_data) {
3173
71
        for (i = 0; i < ist->st->nb_side_data; i++) {
3174
39
            const AVPacketSideData *sd_src = &ist->st->side_data[i];
3175
            uint8_t *dst_data;
3176
3177
39
            dst_data = av_stream_new_side_data(ost->st, sd_src->type, sd_src->size);
3178
39
            if (!dst_data)
3179
                return AVERROR(ENOMEM);
3180
39
            memcpy(dst_data, sd_src->data, sd_src->size);
3181
        }
3182
    }
3183
3184
346
    if (ost->rotate_overridden) {
3185
1
        uint8_t *sd = av_stream_new_side_data(ost->st, AV_PKT_DATA_DISPLAYMATRIX,
3186
                                              sizeof(int32_t) * 9);
3187
1
        if (sd)
3188
1
            av_display_rotation_set((int32_t *)sd, -ost->rotate_override_value);
3189
    }
3190
3191
346
    switch (par_dst->codec_type) {
3192
126
    case AVMEDIA_TYPE_AUDIO:
3193
126
        if (audio_volume != 256) {
3194
            av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
3195
            exit_program(1);
3196
        }
3197


126
        if((par_dst->block_align == 1 || par_dst->block_align == 1152 || par_dst->block_align == 576) && par_dst->codec_id == AV_CODEC_ID_MP3)
3198
            par_dst->block_align= 0;
3199
126
        if(par_dst->codec_id == AV_CODEC_ID_AC3)
3200
7
            par_dst->block_align= 0;
3201
126
        break;
3202
195
    case AVMEDIA_TYPE_VIDEO:
3203
195
        if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
3204
            sar =
3205
                av_mul_q(ost->frame_aspect_ratio,
3206
                         (AVRational){ par_dst->height, par_dst->width });
3207
            av_log(NULL, AV_LOG_WARNING, "Overriding aspect ratio "
3208
                   "with stream copy may produce invalid files\n");
3209
            }
3210
195
        else if (ist->st->sample_aspect_ratio.num)
3211
61
            sar = ist->st->sample_aspect_ratio;
3212
        else
3213
134
            sar = par_src->sample_aspect_ratio;
3214
195
        ost->st->sample_aspect_ratio = par_dst->sample_aspect_ratio = sar;
3215
195
        ost->st->avg_frame_rate = ist->st->avg_frame_rate;
3216
195
        ost->st->r_frame_rate = ist->st->r_frame_rate;
3217
195
        break;
3218
    }
3219
3220
346
    ost->mux_timebase = ist->st->time_base;
3221
3222
346
    return 0;
3223
}
3224
3225
5844
static void set_encoder_id(OutputFile *of, OutputStream *ost)
3226
{
3227
    AVDictionaryEntry *e;
3228
3229
    uint8_t *encoder_string;
3230
    int encoder_string_len;
3231
5844
    int format_flags = 0;
3232
5844
    int codec_flags = ost->enc_ctx->flags;
3233
3234
5844
    if (av_dict_get(ost->st->metadata, "encoder",  NULL, 0))
3235
        return;
3236
3237
5844
    e = av_dict_get(of->opts, "fflags", NULL, 0);
3238
5844
    if (e) {
3239
3402
        const AVOption *o = av_opt_find(of->ctx, "fflags", NULL, 0, 0);
3240
3402
        if (!o)
3241
            return;
3242
3402
        av_opt_eval_flags(of->ctx, o, e->value, &format_flags);
3243
    }
3244
5844
    e = av_dict_get(ost->encoder_opts, "flags", NULL, 0);
3245
5844
    if (e) {
3246
3401
        const AVOption *o = av_opt_find(ost->enc_ctx, "flags", NULL, 0, 0);
3247
3401
        if (!o)
3248
            return;
3249
3401
        av_opt_eval_flags(ost->enc_ctx, o, e->value, &codec_flags);
3250
    }
3251
3252
5844
    encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2;
3253
5844
    encoder_string     = av_mallocz(encoder_string_len);
3254
5844
    if (!encoder_string)
3255
        exit_program(1);
3256
3257

5844
    if (!(format_flags & AVFMT_FLAG_BITEXACT) && !(codec_flags & AV_CODEC_FLAG_BITEXACT))
3258
831
        av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
3259
    else
3260
5013
        av_strlcpy(encoder_string, "Lavc ", encoder_string_len);
3261
5844
    av_strlcat(encoder_string, ost->enc->name, encoder_string_len);
3262
5844
    av_dict_set(&ost->st->metadata, "encoder",  encoder_string,
3263
                AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
3264
}
3265
3266
1
static void parse_forced_key_frames(char *kf, OutputStream *ost,
3267
                                    AVCodecContext *avctx)
3268
{
3269
    char *p;
3270
1
    int n = 1, i, size, index = 0;
3271
    int64_t t, *pts;
3272
3273
14
    for (p = kf; *p; p++)
3274
13
        if (*p == ',')
3275
1
            n++;
3276
1
    size = n;
3277
1
    pts = av_malloc_array(size, sizeof(*pts));
3278
1
    if (!pts) {
3279
        av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
3280
        exit_program(1);
3281
    }
3282
3283
1
    p = kf;
3284
3
    for (i = 0; i < n; i++) {
3285
2
        char *next = strchr(p, ',');
3286
3287
2
        if (next)
3288
1
            *next++ = 0;
3289
3290
2
        if (!memcmp(p, "chapters", 8)) {
3291
3292
            AVFormatContext *avf = output_files[ost->file_index]->ctx;
3293
            int j;
3294
3295
            if (avf->nb_chapters > INT_MAX - size ||
3296
                !(pts = av_realloc_f(pts, size += avf->nb_chapters - 1,
3297
                                     sizeof(*pts)))) {
3298
                av_log(NULL, AV_LOG_FATAL,
3299
                       "Could not allocate forced key frames array.\n");
3300
                exit_program(1);
3301
            }
3302
            t = p[8] ? parse_time_or_die("force_key_frames", p + 8, 1) : 0;
3303
            t = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
3304
3305
            for (j = 0; j < avf->nb_chapters; j++) {
3306
                AVChapter *c = avf->chapters[j];
3307
                av_assert1(index < size);
3308
                pts[index++] = av_rescale_q(c->start, c->time_base,
3309
                                            avctx->time_base) + t;
3310
            }
3311
3312
        } else {
3313
3314
2
            t = parse_time_or_die("force_key_frames", p, 1);
3315
            av_assert1(index < size);
3316
2
            pts[index++] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
3317
3318
        }
3319
3320
2
        p = next;
3321
    }
3322
3323
1
    av_assert0(index == size);
3324
1
    qsort(pts, size, sizeof(*pts), compare_int64);
3325
1
    ost->forced_kf_count = size;
3326
1
    ost->forced_kf_pts   = pts;
3327
1
}
3328
3329
5812
static void init_encoder_time_base(OutputStream *ost, AVRational default_time_base)
3330
{
3331
5812
    InputStream *ist = get_input_stream(ost);
3332
5812
    AVCodecContext *enc_ctx = ost->enc_ctx;
3333
    AVFormatContext *oc;
3334
3335
5812
    if (ost->enc_timebase.num > 0) {
3336
        enc_ctx->time_base = ost->enc_timebase;
3337
        return;
3338
    }
3339
3340
5812
    if (ost->enc_timebase.num < 0) {
3341
        if (ist) {
3342
            enc_ctx->time_base = ist->st->time_base;
3343
            return;
3344
        }
3345
3346
        oc = output_files[ost->file_index]->ctx;
3347
        av_log(oc, AV_LOG_WARNING, "Input stream data not available, using default time base\n");
3348
    }
3349
3350
5812
    enc_ctx->time_base = default_time_base;
3351
}
3352
3353
5844
static int init_output_stream_encode(OutputStream *ost, AVFrame *frame)
3354
{
3355
5844
    InputStream *ist = get_input_stream(ost);
3356
5844
    AVCodecContext *enc_ctx = ost->enc_ctx;
3357
5844
    AVCodecContext *dec_ctx = NULL;
3358
5844
    AVFormatContext *oc = output_files[ost->file_index]->ctx;
3359
    int j, ret;
3360
3361
5844
    set_encoder_id(output_files[ost->file_index], ost);
3362
3363
    // Muxers use AV_PKT_DATA_DISPLAYMATRIX to signal rotation. On the other
3364
    // hand, the legacy API makes demuxers set "rotate" metadata entries,
3365
    // which have to be filtered out to prevent leaking them to output files.
3366
5844
<