avconv.c 85.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * avconv main
 * Copyright (c) 2000-2011 The libav developers.
 *
 * This file is part of Libav.
 *
 * Libav is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Libav is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Libav; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "config.h"
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <limits.h>
30 31
#include <stdint.h>

32 33 34
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libswscale/swscale.h"
Justin Ruggles's avatar
Justin Ruggles committed
35
#include "libavresample/avresample.h"
36
#include "libavutil/opt.h"
37
#include "libavutil/channel_layout.h"
38 39 40 41 42 43 44 45 46
#include "libavutil/parseutils.h"
#include "libavutil/samplefmt.h"
#include "libavutil/fifo.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/dict.h"
#include "libavutil/mathematics.h"
#include "libavutil/pixdesc.h"
#include "libavutil/avstring.h"
#include "libavutil/libm.h"
47
#include "libavutil/imgutils.h"
48
#include "libavutil/time.h"
49 50 51
#include "libavformat/os_support.h"

# include "libavfilter/avfilter.h"
52
# include "libavfilter/buffersrc.h"
53
# include "libavfilter/buffersink.h"
54 55

#if HAVE_SYS_RESOURCE_H
56
#include <sys/time.h>
57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include <sys/types.h>
#include <sys/resource.h>
#elif HAVE_GETPROCESSTIMES
#include <windows.h>
#endif
#if HAVE_GETPROCESSMEMORYINFO
#include <windows.h>
#include <psapi.h>
#endif

#if HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

71 72 73 74
#if HAVE_PTHREADS
#include <pthread.h>
#endif

75 76
#include <time.h>

77
#include "avconv.h"
78 79 80 81 82 83 84 85 86 87 88 89
#include "cmdutils.h"

#include "libavutil/avassert.h"

const char program_name[] = "avconv";
const int program_birth_year = 2000;

static FILE *vstats_file;

static int nb_frames_drop = 0;


90

91
#if HAVE_PTHREADS
92 93 94 95
/* signal to input threads that they should exit; set by the main thread */
static int transcoding_finished;
#endif

96 97
#define DEFAULT_PASS_LOGFILENAME_PREFIX "av2pass"

98 99 100 101
InputStream **input_streams = NULL;
int        nb_input_streams = 0;
InputFile   **input_files   = NULL;
int        nb_input_files   = 0;
102

103 104 105 106
OutputStream **output_streams = NULL;
int         nb_output_streams = 0;
OutputFile   **output_files   = NULL;
int         nb_output_files   = 0;
107

108 109
FilterGraph **filtergraphs;
int        nb_filtergraphs;
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static void term_exit(void)
{
    av_log(NULL, AV_LOG_QUIET, "");
}

static volatile int received_sigterm = 0;
static volatile int received_nb_signals = 0;

static void
sigterm_handler(int sig)
{
    received_sigterm = sig;
    received_nb_signals++;
    term_exit();
}

static void term_init(void)
{
Aneesh Dogra's avatar
Aneesh Dogra committed
129
    signal(SIGINT , sigterm_handler); /* Interrupt (ANSI).    */
130 131 132 133 134 135
    signal(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
#ifdef SIGXCPU
    signal(SIGXCPU, sigterm_handler);
#endif
}

136
static int decode_interrupt_cb(void *ctx)
137 138 139 140
{
    return received_nb_signals > 1;
}

141
const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
142

Luca Barbato's avatar
Luca Barbato committed
143
static void avconv_cleanup(int ret)
144
{
145 146 147 148
    int i, j;

    for (i = 0; i < nb_filtergraphs; i++) {
        avfilter_graph_free(&filtergraphs[i]->graph);
149 150
        for (j = 0; j < filtergraphs[i]->nb_inputs; j++) {
            av_freep(&filtergraphs[i]->inputs[j]->name);
151
            av_freep(&filtergraphs[i]->inputs[j]);
152
        }
153
        av_freep(&filtergraphs[i]->inputs);
154 155
        for (j = 0; j < filtergraphs[i]->nb_outputs; j++) {
            av_freep(&filtergraphs[i]->outputs[j]->name);
156
            av_freep(&filtergraphs[i]->outputs[j]);
157
        }
158
        av_freep(&filtergraphs[i]->outputs);
159
        av_freep(&filtergraphs[i]->graph_desc);
160 161 162
        av_freep(&filtergraphs[i]);
    }
    av_freep(&filtergraphs);
163 164

    /* close files */
Aneesh Dogra's avatar
Aneesh Dogra committed
165
    for (i = 0; i < nb_output_files; i++) {
166
        AVFormatContext *s = output_files[i]->ctx;
167
        if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE) && s->pb)
168 169
            avio_close(s->pb);
        avformat_free_context(s);
170 171
        av_dict_free(&output_files[i]->opts);
        av_freep(&output_files[i]);
172
    }
Janne Grunau's avatar
Janne Grunau committed
173
    for (i = 0; i < nb_output_streams; i++) {
174
        AVBitStreamFilterContext *bsfc = output_streams[i]->bitstream_filters;
Janne Grunau's avatar
Janne Grunau committed
175 176 177 178 179
        while (bsfc) {
            AVBitStreamFilterContext *next = bsfc->next;
            av_bitstream_filter_close(bsfc);
            bsfc = next;
        }
180
        output_streams[i]->bitstream_filters = NULL;
181
        av_frame_free(&output_streams[i]->filtered_frame);
182

183 184
        av_parser_close(output_streams[i]->parser);

Anton Khirnov's avatar
Anton Khirnov committed
185
        av_freep(&output_streams[i]->forced_keyframes);
186
        av_freep(&output_streams[i]->avfilter);
187
        av_freep(&output_streams[i]->logfile_prefix);
188
        av_freep(&output_streams[i]);
Janne Grunau's avatar
Janne Grunau committed
189
    }
Aneesh Dogra's avatar
Aneesh Dogra committed
190
    for (i = 0; i < nb_input_files; i++) {
191 192
        avformat_close_input(&input_files[i]->ctx);
        av_freep(&input_files[i]);
193
    }
194
    for (i = 0; i < nb_input_streams; i++) {
195 196
        av_frame_free(&input_streams[i]->decoded_frame);
        av_frame_free(&input_streams[i]->filter_frame);
197
        av_dict_free(&input_streams[i]->opts);
198
        av_freep(&input_streams[i]->filters);
199
        av_freep(&input_streams[i]->hwaccel_device);
200
        av_freep(&input_streams[i]);
201
    }
202 203 204 205 206 207 208

    if (vstats_file)
        fclose(vstats_file);
    av_free(vstats_filename);

    av_freep(&input_streams);
    av_freep(&input_files);
209
    av_freep(&output_streams);
210
    av_freep(&output_files);
211 212 213

    uninit_opts();

214
    avformat_network_deinit();
215 216

    if (received_sigterm) {
217 218
        av_log(NULL, AV_LOG_INFO, "Received signal %d: terminating.\n",
               (int) received_sigterm);
219 220 221 222
        exit (255);
    }
}

223
void assert_avoptions(AVDictionary *m)
224 225 226
{
    AVDictionaryEntry *t;
    if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
227
        av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
Luca Barbato's avatar
Luca Barbato committed
228
        exit_program(1);
229 230 231
    }
}

232
static void abort_codec_experimental(AVCodec *c, int encoder)
233 234 235
{
    const char *codec_string = encoder ? "encoder" : "decoder";
    AVCodec *codec;
236 237 238 239 240 241 242
    av_log(NULL, AV_LOG_FATAL, "%s '%s' is experimental and might produce bad "
            "results.\nAdd '-strict experimental' if you want to use it.\n",
            codec_string, c->name);
    codec = encoder ? avcodec_find_encoder(c->id) : avcodec_find_decoder(c->id);
    if (!(codec->capabilities & CODEC_CAP_EXPERIMENTAL))
        av_log(NULL, AV_LOG_FATAL, "Or use the non experimental %s '%s'.\n",
               codec_string, codec->name);
Luca Barbato's avatar
Luca Barbato committed
243
    exit_program(1);
244 245
}

246
/*
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
 * Update the requested input sample format based on the output sample format.
 * This is currently only used to request float output from decoders which
 * support multiple sample formats, one of which is AV_SAMPLE_FMT_FLT.
 * Ideally this will be removed in the future when decoders do not do format
 * conversion and only output in their native format.
 */
static void update_sample_fmt(AVCodecContext *dec, AVCodec *dec_codec,
                              AVCodecContext *enc)
{
    /* if sample formats match or a decoder sample format has already been
       requested, just return */
    if (enc->sample_fmt == dec->sample_fmt ||
        dec->request_sample_fmt > AV_SAMPLE_FMT_NONE)
        return;

    /* if decoder supports more than one output format */
    if (dec_codec && dec_codec->sample_fmts &&
        dec_codec->sample_fmts[0] != AV_SAMPLE_FMT_NONE &&
        dec_codec->sample_fmts[1] != AV_SAMPLE_FMT_NONE) {
        const enum AVSampleFormat *p;
267 268 269
        int min_dec = INT_MAX, min_inc = INT_MAX;
        enum AVSampleFormat dec_fmt = AV_SAMPLE_FMT_NONE;
        enum AVSampleFormat inc_fmt = AV_SAMPLE_FMT_NONE;
270 271 272 273 274 275

        /* find a matching sample format in the encoder */
        for (p = dec_codec->sample_fmts; *p != AV_SAMPLE_FMT_NONE; p++) {
            if (*p == enc->sample_fmt) {
                dec->request_sample_fmt = *p;
                return;
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
            } else {
                enum AVSampleFormat dfmt = av_get_packed_sample_fmt(*p);
                enum AVSampleFormat efmt = av_get_packed_sample_fmt(enc->sample_fmt);
                int fmt_diff = 32 * abs(dfmt - efmt);
                if (av_sample_fmt_is_planar(*p) !=
                    av_sample_fmt_is_planar(enc->sample_fmt))
                    fmt_diff++;
                if (dfmt == efmt) {
                    min_inc = fmt_diff;
                    inc_fmt = *p;
                } else if (dfmt > efmt) {
                    if (fmt_diff < min_inc) {
                        min_inc = fmt_diff;
                        inc_fmt = *p;
                    }
                } else {
                    if (fmt_diff < min_dec) {
                        min_dec = fmt_diff;
                        dec_fmt = *p;
                    }
                }
            }
298 299 300
        }

        /* if none match, provide the one that matches quality closest */
301
        dec->request_sample_fmt = min_inc != INT_MAX ? inc_fmt : dec_fmt;
302 303 304
    }
}

305
static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
Aneesh Dogra's avatar
Aneesh Dogra committed
306
{
307 308
    AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
    AVCodecContext          *avctx = ost->st->codec;
309 310
    int ret;

311 312 313 314 315 316 317 318
    /*
     * Audio encoders may split the packets --  #frames in != #packets out.
     * But there is no reordering, so we can limit the number of output packets
     * by simply dropping them here.
     * Counting encoded video frames needs to be done separately because of
     * reordering, see do_video_out()
     */
    if (!(avctx->codec_type == AVMEDIA_TYPE_VIDEO && avctx->codec)) {
319 320
        if (ost->frame_number >= ost->max_frames) {
            av_free_packet(pkt);
321
            return;
322
        }
323 324 325
        ost->frame_number++;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
326 327 328 329 330 331 332
    while (bsfc) {
        AVPacket new_pkt = *pkt;
        int a = av_bitstream_filter_filter(bsfc, avctx, NULL,
                                           &new_pkt.data, &new_pkt.size,
                                           pkt->data, pkt->size,
                                           pkt->flags & AV_PKT_FLAG_KEY);
        if (a > 0) {
333
            av_free_packet(pkt);
334 335 336
            new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size,
                                           av_buffer_default_free, NULL, 0);
            if (!new_pkt.buf)
Luca Barbato's avatar
Luca Barbato committed
337
                exit_program(1);
Aneesh Dogra's avatar
Aneesh Dogra committed
338
        } else if (a < 0) {
339 340 341
            av_log(NULL, AV_LOG_ERROR, "%s failed for stream %d, codec %s",
                   bsfc->filter->name, pkt->stream_index,
                   avctx->codec ? avctx->codec->name : "copy");
342 343
            print_error("", a);
            if (exit_on_error)
Luca Barbato's avatar
Luca Barbato committed
344
                exit_program(1);
345
        }
Aneesh Dogra's avatar
Aneesh Dogra committed
346
        *pkt = new_pkt;
347

Aneesh Dogra's avatar
Aneesh Dogra committed
348
        bsfc = bsfc->next;
349 350
    }

351 352 353 354 355 356 357 358
    if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS) &&
        ost->last_mux_dts != AV_NOPTS_VALUE &&
        pkt->dts < ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT)) {
        av_log(NULL, AV_LOG_WARNING, "Non-monotonous DTS in output stream "
               "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
               ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts);
        if (exit_on_error) {
            av_log(NULL, AV_LOG_FATAL, "aborting.\n");
Luca Barbato's avatar
Luca Barbato committed
359
            exit_program(1);
360 361 362 363 364 365 366 367 368 369
        }
        av_log(NULL, AV_LOG_WARNING, "changing to %"PRId64". This may result "
               "in incorrect timestamps in the output file.\n",
               ost->last_mux_dts + 1);
        pkt->dts = ost->last_mux_dts + 1;
        if (pkt->pts != AV_NOPTS_VALUE)
            pkt->pts = FFMAX(pkt->pts, pkt->dts);
    }
    ost->last_mux_dts = pkt->dts;

370
    ost->data_size += pkt->size;
371
    ost->packets_written++;
372

373
    pkt->stream_index = ost->index;
Aneesh Dogra's avatar
Aneesh Dogra committed
374 375
    ret = av_interleaved_write_frame(s, pkt);
    if (ret < 0) {
376
        print_error("av_interleaved_write_frame()", ret);
Luca Barbato's avatar
Luca Barbato committed
377
        exit_program(1);
378 379 380
    }
}

381 382
static int check_recording_time(OutputStream *ost)
{
383
    OutputFile *of = output_files[ost->file_index];
384 385 386 387

    if (of->recording_time != INT64_MAX &&
        av_compare_ts(ost->sync_opts - ost->first_pts, ost->st->codec->time_base, of->recording_time,
                      AV_TIME_BASE_Q) >= 0) {
388
        ost->finished = 1;
389 390 391 392 393
        return 0;
    }
    return 1;
}

394 395
static void do_audio_out(AVFormatContext *s, OutputStream *ost,
                         AVFrame *frame)
396 397 398
{
    AVCodecContext *enc = ost->st->codec;
    AVPacket pkt;
399
    int got_packet = 0;
400 401 402 403 404

    av_init_packet(&pkt);
    pkt.data = NULL;
    pkt.size = 0;

405
    if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
406
        frame->pts = ost->sync_opts;
407
    ost->sync_opts = frame->pts + frame->nb_samples;
408

409 410 411
    ost->samples_encoded += frame->nb_samples;
    ost->frames_encoded++;

412 413
    if (avcodec_encode_audio2(enc, &pkt, frame, &got_packet) < 0) {
        av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
Luca Barbato's avatar
Luca Barbato committed
414
        exit_program(1);
415 416 417 418 419
    }

    if (got_packet) {
        if (pkt.pts != AV_NOPTS_VALUE)
            pkt.pts      = av_rescale_q(pkt.pts,      enc->time_base, ost->st->time_base);
420 421
        if (pkt.dts != AV_NOPTS_VALUE)
            pkt.dts      = av_rescale_q(pkt.dts,      enc->time_base, ost->st->time_base);
422 423 424 425 426
        if (pkt.duration > 0)
            pkt.duration = av_rescale_q(pkt.duration, enc->time_base, ost->st->time_base);

        write_frame(s, &pkt, ost);
    }
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
}

static void do_subtitle_out(AVFormatContext *s,
                            OutputStream *ost,
                            InputStream *ist,
                            AVSubtitle *sub,
                            int64_t pts)
{
    static uint8_t *subtitle_out = NULL;
    int subtitle_out_max_size = 1024 * 1024;
    int subtitle_out_size, nb, i;
    AVCodecContext *enc;
    AVPacket pkt;

    if (pts == AV_NOPTS_VALUE) {
442
        av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
443
        if (exit_on_error)
Luca Barbato's avatar
Luca Barbato committed
444
            exit_program(1);
445 446 447 448 449 450 451 452 453 454 455 456
        return;
    }

    enc = ost->st->codec;

    if (!subtitle_out) {
        subtitle_out = av_malloc(subtitle_out_max_size);
    }

    /* Note: DVB subtitle need one packet to draw them and one other
       packet to clear them */
    /* XXX: signal it in the codec context ? */
457
    if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
458 459 460 461
        nb = 2;
    else
        nb = 1;

Aneesh Dogra's avatar
Aneesh Dogra committed
462
    for (i = 0; i < nb; i++) {
463 464 465 466
        ost->sync_opts = av_rescale_q(pts, ist->st->time_base, enc->time_base);
        if (!check_recording_time(ost))
            return;

467 468
        sub->pts = av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q);
        // start_display_time is required to be 0
Aneesh Dogra's avatar
Aneesh Dogra committed
469 470
        sub->pts               += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
        sub->end_display_time  -= sub->start_display_time;
471
        sub->start_display_time = 0;
472 473 474

        ost->frames_encoded++;

475 476 477
        subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
                                                    subtitle_out_max_size, sub);
        if (subtitle_out_size < 0) {
478
            av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
Luca Barbato's avatar
Luca Barbato committed
479
            exit_program(1);
480 481 482 483 484
        }

        av_init_packet(&pkt);
        pkt.data = subtitle_out;
        pkt.size = subtitle_out_size;
Aneesh Dogra's avatar
Aneesh Dogra committed
485
        pkt.pts  = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base);
486
        if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
487 488 489 490 491 492 493
            /* XXX: the pts correction is handled here. Maybe handling
               it in the codec would be better */
            if (i == 0)
                pkt.pts += 90 * sub->start_display_time;
            else
                pkt.pts += 90 * sub->end_display_time;
        }
494
        write_frame(s, &pkt, ost);
495 496 497
    }
}

498 499 500
static void do_video_out(AVFormatContext *s,
                         OutputStream *ost,
                         AVFrame *in_picture,
Anton Khirnov's avatar
Anton Khirnov committed
501
                         int *frame_size)
502
{
503 504 505
    int ret, format_video_sync;
    AVPacket pkt;
    AVCodecContext *enc = ost->st->codec;
506 507 508

    *frame_size = 0;

509
    format_video_sync = video_sync_method;
510 511 512
    if (format_video_sync == VSYNC_AUTO)
        format_video_sync = (s->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH :
                            (s->oformat->flags & AVFMT_VARIABLE_FPS) ? VSYNC_VFR : VSYNC_CFR;
513 514 515 516
    if (format_video_sync != VSYNC_PASSTHROUGH &&
        ost->frame_number &&
        in_picture->pts != AV_NOPTS_VALUE &&
        in_picture->pts < ost->sync_opts) {
Anton Khirnov's avatar
Anton Khirnov committed
517
        nb_frames_drop++;
518 519 520
        av_log(NULL, AV_LOG_WARNING,
               "*** dropping frame %d from stream %d at ts %"PRId64"\n",
               ost->frame_number, ost->st->index, in_picture->pts);
521
        return;
Anton Khirnov's avatar
Anton Khirnov committed
522
    }
523

524 525 526 527 528
    if (in_picture->pts == AV_NOPTS_VALUE)
        in_picture->pts = ost->sync_opts;
    ost->sync_opts = in_picture->pts;


529
    if (!ost->frame_number)
530
        ost->first_pts = in_picture->pts;
531

Anton Khirnov's avatar
Anton Khirnov committed
532 533 534
    av_init_packet(&pkt);
    pkt.data = NULL;
    pkt.size = 0;
535

536
    if (ost->frame_number >= ost->max_frames)
Anton Khirnov's avatar
Anton Khirnov committed
537
        return;
538

Anton Khirnov's avatar
Anton Khirnov committed
539
    if (s->oformat->flags & AVFMT_RAWPICTURE &&
540
        enc->codec->id == AV_CODEC_ID_RAWVIDEO) {
Anton Khirnov's avatar
Anton Khirnov committed
541 542 543 544 545 546 547 548 549
        /* raw pictures are written as AVPicture structure to
           avoid any copies. We support temporarily the older
           method. */
        enc->coded_frame->interlaced_frame = in_picture->interlaced_frame;
        enc->coded_frame->top_field_first  = in_picture->top_field_first;
        pkt.data   = (uint8_t *)in_picture;
        pkt.size   =  sizeof(AVPicture);
        pkt.pts    = av_rescale_q(in_picture->pts, enc->time_base, ost->st->time_base);
        pkt.flags |= AV_PKT_FLAG_KEY;
550

Anton Khirnov's avatar
Anton Khirnov committed
551 552 553
        write_frame(s, &pkt, ost);
    } else {
        int got_packet;
554

555 556 557 558 559
        if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME) &&
            ost->top_field_first >= 0)
            in_picture->top_field_first = !!ost->top_field_first;

        in_picture->quality = ost->st->codec->global_quality;
Anton Khirnov's avatar
Anton Khirnov committed
560
        if (!enc->me_threshold)
561
            in_picture->pict_type = 0;
Anton Khirnov's avatar
Anton Khirnov committed
562
        if (ost->forced_kf_index < ost->forced_kf_count &&
563 564
            in_picture->pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
            in_picture->pict_type = AV_PICTURE_TYPE_I;
Anton Khirnov's avatar
Anton Khirnov committed
565 566
            ost->forced_kf_index++;
        }
567 568 569

        ost->frames_encoded++;

570
        ret = avcodec_encode_video2(enc, &pkt, in_picture, &got_packet);
Anton Khirnov's avatar
Anton Khirnov committed
571 572
        if (ret < 0) {
            av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
Luca Barbato's avatar
Luca Barbato committed
573
            exit_program(1);
Anton Khirnov's avatar
Anton Khirnov committed
574
        }
575

Anton Khirnov's avatar
Anton Khirnov committed
576 577 578 579 580
        if (got_packet) {
            if (pkt.pts != AV_NOPTS_VALUE)
                pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
            if (pkt.dts != AV_NOPTS_VALUE)
                pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
581

Anton Khirnov's avatar
Anton Khirnov committed
582 583
            write_frame(s, &pkt, ost);
            *frame_size = pkt.size;
584

Anton Khirnov's avatar
Anton Khirnov committed
585 586 587
            /* if two pass, output log */
            if (ost->logfile && enc->stats_out) {
                fprintf(ost->logfile, "%s", enc->stats_out);
588 589
            }
        }
Anton Khirnov's avatar
Anton Khirnov committed
590 591 592 593 594 595 596 597
    }
    ost->sync_opts++;
    /*
     * For video, number of frames in == number of packets out.
     * But there may be reordering, so we can't throw away frames on encoder
     * flush, we need to limit them here, before they go into encoder.
     */
    ost->frame_number++;
598 599
}

Aneesh Dogra's avatar
Aneesh Dogra committed
600 601 602
static double psnr(double d)
{
    return -10.0 * log(d) / log(10.0);
603 604
}

605
static void do_video_stats(OutputStream *ost, int frame_size)
606 607 608 609 610 611 612 613 614 615
{
    AVCodecContext *enc;
    int frame_number;
    double ti1, bitrate, avg_bitrate;

    /* this is executed just the first time do_video_stats is called */
    if (!vstats_file) {
        vstats_file = fopen(vstats_filename, "w");
        if (!vstats_file) {
            perror("fopen");
Luca Barbato's avatar
Luca Barbato committed
616
            exit_program(1);
617 618 619 620 621 622
        }
    }

    enc = ost->st->codec;
    if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
        frame_number = ost->frame_number;
Aneesh Dogra's avatar
Aneesh Dogra committed
623
        fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality / (float)FF_QP2LAMBDA);
624
        if (enc->flags&CODEC_FLAG_PSNR)
Aneesh Dogra's avatar
Aneesh Dogra committed
625
            fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
626 627 628 629 630 631 632

        fprintf(vstats_file,"f_size= %6d ", frame_size);
        /* compute pts value */
        ti1 = ost->sync_opts * av_q2d(enc->time_base);
        if (ti1 < 0.01)
            ti1 = 0.01;

Aneesh Dogra's avatar
Aneesh Dogra committed
633
        bitrate     = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
634
        avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
635
        fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
636
               (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
637 638 639 640
        fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type));
    }
}

641
/*
642 643 644
 * Read one frame for lavfi output for ost and encode it.
 */
static int poll_filter(OutputStream *ost)
645
{
646
    OutputFile    *of = output_files[ost->file_index];
647
    AVFrame *filtered_frame = NULL;
648 649
    int frame_size, ret;

650
    if (!ost->filtered_frame && !(ost->filtered_frame = av_frame_alloc())) {
651
        return AVERROR(ENOMEM);
652
    }
653 654 655 656
    filtered_frame = ost->filtered_frame;

    if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
        !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
657
        ret = av_buffersink_get_samples(ost->filter->filter, filtered_frame,
658 659
                                         ost->st->codec->frame_size);
    else
660
        ret = av_buffersink_get_frame(ost->filter->filter, filtered_frame);
661

662 663
    if (ret < 0)
        return ret;
664

665
    if (filtered_frame->pts != AV_NOPTS_VALUE) {
666
        int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
667
        filtered_frame->pts = av_rescale_q(filtered_frame->pts,
668 669
                                           ost->filter->filter->inputs[0]->time_base,
                                           ost->st->codec->time_base) -
670
                              av_rescale_q(start_time,
671 672
                                           AV_TIME_BASE_Q,
                                           ost->st->codec->time_base);
673
    }
674 675 676 677

    switch (ost->filter->filter->inputs[0]->type) {
    case AVMEDIA_TYPE_VIDEO:
        if (!ost->frame_aspect_ratio)
678
            ost->st->codec->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
679

Anton Khirnov's avatar
Anton Khirnov committed
680
        do_video_out(of->ctx, ost, filtered_frame, &frame_size);
681
        if (vstats_filename && frame_size)
682
            do_video_stats(ost, frame_size);
683 684 685 686 687 688 689 690 691
        break;
    case AVMEDIA_TYPE_AUDIO:
        do_audio_out(of->ctx, ost, filtered_frame);
        break;
    default:
        // TODO support subtitle filters
        av_assert0(0);
    }

692
    av_frame_unref(filtered_frame);
693

694 695 696
    return 0;
}

697
/*
698 699 700 701 702 703 704 705 706
 * Read as many frames from possible from lavfi and encode them.
 *
 * Always read from the active stream with the lowest timestamp. If no frames
 * are available for it then return EAGAIN and wait for more input. This way we
 * can use lavfi sources that generate unlimited amount of frames without memory
 * usage exploding.
 */
static int poll_filters(void)
{
707
    int i, j, ret = 0;
708 709 710 711 712 713 714 715 716

    while (ret >= 0 && !received_sigterm) {
        OutputStream *ost = NULL;
        int64_t min_pts = INT64_MAX;

        /* choose output stream with the lowest timestamp */
        for (i = 0; i < nb_output_streams; i++) {
            int64_t pts = output_streams[i]->sync_opts;

717
            if (!output_streams[i]->filter || output_streams[i]->finished)
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
                continue;

            pts = av_rescale_q(pts, output_streams[i]->st->codec->time_base,
                               AV_TIME_BASE_Q);
            if (pts < min_pts) {
                min_pts = pts;
                ost = output_streams[i];
            }
        }

        if (!ost)
            break;

        ret = poll_filter(ost);

        if (ret == AVERROR_EOF) {
734 735
            OutputFile *of = output_files[ost->file_index];

736
            ost->finished = 1;
737

738 739 740 741
            if (of->shortest) {
                for (j = 0; j < of->ctx->nb_streams; j++)
                    output_streams[of->ost_index + j]->finished = 1;
            }
742 743 744 745 746 747 748 749 750

            ret = 0;
        } else if (ret == AVERROR(EAGAIN))
            return 0;
    }

    return ret;
}

751 752 753 754 755
static void print_final_stats(int64_t total_size)
{
    uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
    uint64_t data_size = 0;
    float percent = -1.0;
756
    int i, j;
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782

    for (i = 0; i < nb_output_streams; i++) {
        OutputStream *ost = output_streams[i];
        switch (ost->st->codec->codec_type) {
            case AVMEDIA_TYPE_VIDEO: video_size += ost->data_size; break;
            case AVMEDIA_TYPE_AUDIO: audio_size += ost->data_size; break;
            default:                 other_size += ost->data_size; break;
        }
        extra_size += ost->st->codec->extradata_size;
        data_size  += ost->data_size;
    }

    if (data_size && total_size >= data_size)
        percent = 100.0 * (total_size - data_size) / data_size;

    av_log(NULL, AV_LOG_INFO, "\n");
    av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB other streams:%1.0fkB global headers:%1.0fkB muxing overhead: ",
           video_size / 1024.0,
           audio_size / 1024.0,
           other_size / 1024.0,
           extra_size / 1024.0);
    if (percent >= 0.0)
        av_log(NULL, AV_LOG_INFO, "%f%%", percent);
    else
        av_log(NULL, AV_LOG_INFO, "unknown");
    av_log(NULL, AV_LOG_INFO, "\n");
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851

    /* print verbose per-stream stats */
    for (i = 0; i < nb_input_files; i++) {
        InputFile *f = input_files[i];
        uint64_t total_packets = 0, total_size = 0;

        av_log(NULL, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
               i, f->ctx->filename);

        for (j = 0; j < f->nb_streams; j++) {
            InputStream *ist = input_streams[f->ist_index + j];
            enum AVMediaType type = ist->st->codec->codec_type;

            total_size    += ist->data_size;
            total_packets += ist->nb_packets;

            av_log(NULL, AV_LOG_VERBOSE, "  Input stream #%d:%d (%s): ",
                   i, j, media_type_string(type));
            av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
                   ist->nb_packets, ist->data_size);

            if (ist->decoding_needed) {
                av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames decoded",
                       ist->frames_decoded);
                if (type == AVMEDIA_TYPE_AUDIO)
                    av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
                av_log(NULL, AV_LOG_VERBOSE, "; ");
            }

            av_log(NULL, AV_LOG_VERBOSE, "\n");
        }

        av_log(NULL, AV_LOG_VERBOSE, "  Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
               total_packets, total_size);
    }

    for (i = 0; i < nb_output_files; i++) {
        OutputFile *of = output_files[i];
        uint64_t total_packets = 0, total_size = 0;

        av_log(NULL, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
               i, of->ctx->filename);

        for (j = 0; j < of->ctx->nb_streams; j++) {
            OutputStream *ost = output_streams[of->ost_index + j];
            enum AVMediaType type = ost->st->codec->codec_type;

            total_size    += ost->data_size;
            total_packets += ost->packets_written;

            av_log(NULL, AV_LOG_VERBOSE, "  Output stream #%d:%d (%s): ",
                   i, j, media_type_string(type));
            if (ost->encoding_needed) {
                av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
                       ost->frames_encoded);
                if (type == AVMEDIA_TYPE_AUDIO)
                    av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
                av_log(NULL, AV_LOG_VERBOSE, "; ");
            }

            av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
                   ost->packets_written, ost->data_size);

            av_log(NULL, AV_LOG_VERBOSE, "\n");
        }

        av_log(NULL, AV_LOG_VERBOSE, "  Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
               total_packets, total_size);
    }
852 853
}

854
static void print_report(int is_last_report, int64_t timer_start)
855 856 857 858 859 860 861 862 863 864 865
{
    char buf[1024];
    OutputStream *ost;
    AVFormatContext *oc;
    int64_t total_size;
    AVCodecContext *enc;
    int frame_number, vid, i;
    double bitrate, ti1, pts;
    static int64_t last_time = -1;
    static int qp_histogram[52];

866 867 868
    if (!print_stats && !is_last_report)
        return;

869 870 871 872 873 874 875 876 877 878 879 880 881 882
    if (!is_last_report) {
        int64_t cur_time;
        /* display the report every 0.5 seconds */
        cur_time = av_gettime();
        if (last_time == -1) {
            last_time = cur_time;
            return;
        }
        if ((cur_time - last_time) < 500000)
            return;
        last_time = cur_time;
    }


883
    oc = output_files[0]->ctx;
884 885

    total_size = avio_size(oc->pb);
886
    if (total_size <= 0) // FIXME improve avio_size() so it works with non seekable output too
Aneesh Dogra's avatar
Aneesh Dogra committed
887
        total_size = avio_tell(oc->pb);
888 889 890 891 892 893 894
    if (total_size < 0) {
        char errbuf[128];
        av_strerror(total_size, errbuf, sizeof(errbuf));
        av_log(NULL, AV_LOG_VERBOSE, "Bitrate not available, "
               "avio_tell() failed: %s\n", errbuf);
        total_size = 0;
    }
895 896 897 898

    buf[0] = '\0';
    ti1 = 1e10;
    vid = 0;
899
    for (i = 0; i < nb_output_streams; i++) {
900
        float q = -1;
901
        ost = output_streams[i];
902
        enc = ost->st->codec;
903
        if (!ost->stream_copy && enc->coded_frame)
Aneesh Dogra's avatar
Aneesh Dogra committed
904
            q = enc->coded_frame->quality / (float)FF_QP2LAMBDA;
905 906 907 908
        if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
            snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q);
        }
        if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
Aneesh Dogra's avatar
Aneesh Dogra committed
909
            float t = (av_gettime() - timer_start) / 1000000.0;
910 911 912

            frame_number = ost->frame_number;
            snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3d q=%3.1f ",
Aneesh Dogra's avatar
Aneesh Dogra committed
913 914
                     frame_number, (t > 1) ? (int)(frame_number / t + 0.5) : 0, q);
            if (is_last_report)
915
                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
Aneesh Dogra's avatar
Aneesh Dogra committed
916
            if (qp_hist) {
917 918
                int j;
                int qp = lrintf(q);
Aneesh Dogra's avatar
Aneesh Dogra committed
919
                if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
920
                    qp_histogram[qp]++;
Aneesh Dogra's avatar
Aneesh Dogra committed
921
                for (j = 0; j < 32; j++)
922
                    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log2(qp_histogram[j] + 1)));
923
            }
Aneesh Dogra's avatar
Aneesh Dogra committed
924
            if (enc->flags&CODEC_FLAG_PSNR) {
925
                int j;
Aneesh Dogra's avatar
Aneesh Dogra committed
926 927 928
                double error, error_sum = 0;
                double scale, scale_sum = 0;
                char type[3] = { 'Y','U','V' };
929
                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR=");
Aneesh Dogra's avatar
Aneesh Dogra committed
930 931 932 933 934 935 936
                for (j = 0; j < 3; j++) {
                    if (is_last_report) {
                        error = enc->error[j];
                        scale = enc->width * enc->height * 255.0 * 255.0 * frame_number;
                    } else {
                        error = enc->coded_frame->error[j];
                        scale = enc->width * enc->height * 255.0 * 255.0;
937
                    }
Aneesh Dogra's avatar
Aneesh Dogra committed
938 939
                    if (j)
                        scale /= 4;
940 941
                    error_sum += error;
                    scale_sum += scale;
Aneesh Dogra's avatar
Aneesh Dogra committed
942
                    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], psnr(error / scale));
943
                }
Aneesh Dogra's avatar
Aneesh Dogra committed
944
                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum / scale_sum));
945 946 947 948 949 950 951 952 953 954 955
            }
            vid = 1;
        }
        /* compute min output value */
        pts = (double)ost->st->pts.val * av_q2d(ost->st->time_base);
        if ((pts < ti1) && (pts > 0))
            ti1 = pts;
    }
    if (ti1 < 0.01)
        ti1 = 0.01;

956
    bitrate = (double)(total_size * 8) / ti1 / 1000.0;
957

958
    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
959 960 961
            "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
            (double)total_size / 1024, ti1, bitrate);

962 963 964
    if (nb_frames_drop)
        snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " drop=%d",
                 nb_frames_drop);
965

966
    av_log(NULL, AV_LOG_INFO, "%s    \r", buf);
967

968
    fflush(stderr);
969

970 971
    if (is_last_report)
        print_final_stats(total_size);
972

973 974
}

975
static void flush_encoders(void)
976 977 978
{
    int i, ret;

979
    for (i = 0; i < nb_output_streams; i++) {
980
        OutputStream   *ost = output_streams[i];
981
        AVCodecContext *enc = ost->st->codec;
982
        AVFormatContext *os = output_files[ost->file_index]->ctx;
983
        int stop_encoding = 0;
984

985
        if (!ost->encoding_needed)
986 987
            continue;

Aneesh Dogra's avatar
Aneesh Dogra committed
988
        if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
989
            continue;
990
        if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == AV_CODEC_ID_RAWVIDEO)