avconv.c 80.3 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 30 31 32
/*
 * 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>
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libswscale/swscale.h"
Justin Ruggles's avatar
Justin Ruggles committed
33
#include "libavresample/avresample.h"
34 35 36 37 38 39 40 41 42 43 44 45
#include "libavutil/opt.h"
#include "libavutil/audioconvert.h"
#include "libavutil/parseutils.h"
#include "libavutil/samplefmt.h"
#include "libavutil/colorspace.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"
46
#include "libavutil/imgutils.h"
47
#include "libavutil/time.h"
48 49 50 51
#include "libavformat/os_support.h"

# include "libavfilter/avfilter.h"
# include "libavfilter/avfiltergraph.h"
52
# include "libavfilter/buffersrc.h"
53
# include "libavfilter/buffersink.h"
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

#if HAVE_SYS_RESOURCE_H
#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

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

74 75
#include <time.h>

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

#include "libavutil/avassert.h"

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

static FILE *vstats_file;

static int64_t video_size = 0;
static int64_t audio_size = 0;
static int64_t extra_size = 0;
static int nb_frames_dup = 0;
static int nb_frames_drop = 0;


93

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

99 100
#define DEFAULT_PASS_LOGFILENAME_PREFIX "av2pass"

101 102 103 104
InputStream **input_streams = NULL;
int        nb_input_streams = 0;
InputFile   **input_files   = NULL;
int        nb_input_files   = 0;
105

106 107 108 109
OutputStream **output_streams = NULL;
int         nb_output_streams = 0;
OutputFile   **output_files   = NULL;
int         nb_output_files   = 0;
110

111 112
FilterGraph **filtergraphs;
int        nb_filtergraphs;
113

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
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
132
    signal(SIGINT , sigterm_handler); /* Interrupt (ANSI).    */
133 134 135 136 137 138
    signal(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
#ifdef SIGXCPU
    signal(SIGXCPU, sigterm_handler);
#endif
}

139
static int decode_interrupt_cb(void *ctx)
140 141 142 143
{
    return received_nb_signals > 1;
}

144
const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
145

146
void exit_program(int ret)
147
{
148 149 150 151
    int i, j;

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

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

Anton Khirnov's avatar
Anton Khirnov committed
184
        av_freep(&output_streams[i]->forced_keyframes);
185
        av_freep(&output_streams[i]->avfilter);
186
        av_freep(&output_streams[i]->logfile_prefix);
187
        av_freep(&output_streams[i]->filtered_frame);
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_freep(&input_streams[i]->decoded_frame);
        av_dict_free(&input_streams[i]->opts);
197
        free_buffer_pool(&input_streams[i]->buffer_pool);
198
        av_freep(&input_streams[i]->filters);
199
        av_freep(&input_streams[i]);
200
    }
201 202 203 204 205 206 207

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

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

    uninit_opts();

    avfilter_uninit();
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
        exit (255);
    }

222
    exit(ret);
223 224
}

225
void assert_avoptions(AVDictionary *m)
226 227 228
{
    AVDictionaryEntry *t;
    if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
229
        av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
230 231 232 233 234 235 236 237 238 239
        exit_program(1);
    }
}

static void assert_codec_experimental(AVCodecContext *c, int encoder)
{
    const char *codec_string = encoder ? "encoder" : "decoder";
    AVCodec *codec;
    if (c->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
        c->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
240
        av_log(NULL, AV_LOG_FATAL, "%s '%s' is experimental and might produce bad "
241 242 243 244
                "results.\nAdd '-strict experimental' if you want to use it.\n",
                codec_string, c->codec->name);
        codec = encoder ? avcodec_find_encoder(c->codec->id) : avcodec_find_decoder(c->codec->id);
        if (!(codec->capabilities & CODEC_CAP_EXPERIMENTAL))
245
            av_log(NULL, AV_LOG_FATAL, "Or use the non experimental %s '%s'.\n",
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
                   codec_string, codec->name);
        exit_program(1);
    }
}

/**
 * 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;
        int min_dec = -1, min_inc = -1;

        /* 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;
            } else if (*p > enc->sample_fmt) {
                min_inc = FFMIN(min_inc, *p - enc->sample_fmt);
            } else
                min_dec = FFMIN(min_dec, enc->sample_fmt - *p);
        }

        /* if none match, provide the one that matches quality closest */
        dec->request_sample_fmt = min_inc > 0 ? enc->sample_fmt + min_inc :
                                  enc->sample_fmt - min_dec;
    }
}

291
static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
Aneesh Dogra's avatar
Aneesh Dogra committed
292
{
293 294
    AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
    AVCodecContext          *avctx = ost->st->codec;
295 296
    int ret;

297 298 299 300 301 302 303 304
    /*
     * 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)) {
305 306
        if (ost->frame_number >= ost->max_frames) {
            av_free_packet(pkt);
307
            return;
308
        }
309 310 311
        ost->frame_number++;
    }

Aneesh Dogra's avatar
Aneesh Dogra committed
312 313 314 315 316 317 318
    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) {
319
            av_free_packet(pkt);
Aneesh Dogra's avatar
Aneesh Dogra committed
320 321
            new_pkt.destruct = av_destruct_packet;
        } else if (a < 0) {
322 323 324
            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");
325 326 327 328
            print_error("", a);
            if (exit_on_error)
                exit_program(1);
        }
Aneesh Dogra's avatar
Aneesh Dogra committed
329
        *pkt = new_pkt;
330

Aneesh Dogra's avatar
Aneesh Dogra committed
331
        bsfc = bsfc->next;
332 333
    }

334
    pkt->stream_index = ost->index;
Aneesh Dogra's avatar
Aneesh Dogra committed
335 336
    ret = av_interleaved_write_frame(s, pkt);
    if (ret < 0) {
337 338 339 340 341
        print_error("av_interleaved_write_frame()", ret);
        exit_program(1);
    }
}

342 343
static int check_recording_time(OutputStream *ost)
{
344
    OutputFile *of = output_files[ost->file_index];
345 346 347 348

    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) {
349
        ost->finished = 1;
350 351 352 353 354
        return 0;
    }
    return 1;
}

355 356
static void do_audio_out(AVFormatContext *s, OutputStream *ost,
                         AVFrame *frame)
357 358 359
{
    AVCodecContext *enc = ost->st->codec;
    AVPacket pkt;
360
    int got_packet = 0;
361 362 363 364 365

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

366 367
    if (!check_recording_time(ost))
        return;
368

369
    if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
370
        frame->pts = ost->sync_opts;
371
    ost->sync_opts = frame->pts + frame->nb_samples;
372 373 374 375 376 377 378 379 380

    if (avcodec_encode_audio2(enc, &pkt, frame, &got_packet) < 0) {
        av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
        exit_program(1);
    }

    if (got_packet) {
        if (pkt.pts != AV_NOPTS_VALUE)
            pkt.pts      = av_rescale_q(pkt.pts,      enc->time_base, ost->st->time_base);
381 382
        if (pkt.dts != AV_NOPTS_VALUE)
            pkt.dts      = av_rescale_q(pkt.dts,      enc->time_base, ost->st->time_base);
383 384 385 386 387 388 389
        if (pkt.duration > 0)
            pkt.duration = av_rescale_q(pkt.duration, enc->time_base, ost->st->time_base);

        write_frame(s, &pkt, ost);

        audio_size += pkt.size;
    }
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
}

static void pre_process_video_frame(InputStream *ist, AVPicture *picture, void **bufp)
{
    AVCodecContext *dec;
    AVPicture *picture2;
    AVPicture picture_tmp;
    uint8_t *buf = 0;

    dec = ist->st->codec;

    /* deinterlace : must be done before any resize */
    if (do_deinterlace) {
        int size;

        /* create temporary picture */
        size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
Aneesh Dogra's avatar
Aneesh Dogra committed
407
        buf  = av_malloc(size);
408 409 410 411 412 413
        if (!buf)
            return;

        picture2 = &picture_tmp;
        avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);

Aneesh Dogra's avatar
Aneesh Dogra committed
414
        if (avpicture_deinterlace(picture2, picture,
415 416
                                 dec->pix_fmt, dec->width, dec->height) < 0) {
            /* if error, do not deinterlace */
417
            av_log(NULL, AV_LOG_WARNING, "Deinterlacing failed\n");
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
            av_free(buf);
            buf = NULL;
            picture2 = picture;
        }
    } else {
        picture2 = picture;
    }

    if (picture != picture2)
        *picture = *picture2;
    *bufp = buf;
}

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) {
444
        av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
445 446 447 448 449 450 451 452 453 454 455 456 457 458
        if (exit_on_error)
            exit_program(1);
        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 ? */
459
    if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
460 461 462 463
        nb = 2;
    else
        nb = 1;

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

469 470
        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
471 472
        sub->pts               += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
        sub->end_display_time  -= sub->start_display_time;
473 474 475 476
        sub->start_display_time = 0;
        subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
                                                    subtitle_out_max_size, sub);
        if (subtitle_out_size < 0) {
477
            av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
478 479 480 481 482 483
            exit_program(1);
        }

        av_init_packet(&pkt);
        pkt.data = subtitle_out;
        pkt.size = subtitle_out_size;
Aneesh Dogra's avatar
Aneesh Dogra committed
484
        pkt.pts  = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base);
485
        if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
486 487 488 489 490 491 492
            /* 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;
        }
493
        write_frame(s, &pkt, ost);
494 495 496
    }
}

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

    *frame_size = 0;

508
    format_video_sync = video_sync_method;
509 510 511
    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;
512 513 514 515
    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
516 517
        nb_frames_drop++;
        av_log(NULL, AV_LOG_VERBOSE, "*** drop!\n");
518
        return;
Anton Khirnov's avatar
Anton Khirnov committed
519
    }
520

521 522 523 524 525
    if (in_picture->pts == AV_NOPTS_VALUE)
        in_picture->pts = ost->sync_opts;
    ost->sync_opts = in_picture->pts;


526
    if (!ost->frame_number)
527
        ost->first_pts = in_picture->pts;
528

Anton Khirnov's avatar
Anton Khirnov committed
529 530 531
    av_init_packet(&pkt);
    pkt.data = NULL;
    pkt.size = 0;
532

Anton Khirnov's avatar
Anton Khirnov committed
533 534 535
    if (!check_recording_time(ost) ||
        ost->frame_number >= ost->max_frames)
        return;
536

Anton Khirnov's avatar
Anton Khirnov committed
537
    if (s->oformat->flags & AVFMT_RAWPICTURE &&
538
        enc->codec->id == AV_CODEC_ID_RAWVIDEO) {
Anton Khirnov's avatar
Anton Khirnov committed
539 540 541 542 543 544 545 546 547
        /* 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;
548

Anton Khirnov's avatar
Anton Khirnov committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
        write_frame(s, &pkt, ost);
    } else {
        int got_packet;
        AVFrame big_picture;

        big_picture = *in_picture;
        /* better than nothing: use input picture interlaced
           settings */
        big_picture.interlaced_frame = in_picture->interlaced_frame;
        if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)) {
            if (ost->top_field_first == -1)
                big_picture.top_field_first = in_picture->top_field_first;
            else
                big_picture.top_field_first = !!ost->top_field_first;
        }
564

Anton Khirnov's avatar
Anton Khirnov committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
        /* handles same_quant here. This is not correct because it may
           not be a global option */
        big_picture.quality = quality;
        if (!enc->me_threshold)
            big_picture.pict_type = 0;
        if (ost->forced_kf_index < ost->forced_kf_count &&
            big_picture.pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
            big_picture.pict_type = AV_PICTURE_TYPE_I;
            ost->forced_kf_index++;
        }
        ret = avcodec_encode_video2(enc, &pkt, &big_picture, &got_packet);
        if (ret < 0) {
            av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
            exit_program(1);
        }
580

Anton Khirnov's avatar
Anton Khirnov committed
581 582 583 584 585
        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);
586

Anton Khirnov's avatar
Anton Khirnov committed
587 588 589
            write_frame(s, &pkt, ost);
            *frame_size = pkt.size;
            video_size += pkt.size;
590

Anton Khirnov's avatar
Anton Khirnov committed
591 592 593
            /* if two pass, output log */
            if (ost->logfile && enc->stats_out) {
                fprintf(ost->logfile, "%s", enc->stats_out);
594 595
            }
        }
Anton Khirnov's avatar
Anton Khirnov committed
596 597 598 599 600 601 602 603
    }
    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++;
604 605
}

Aneesh Dogra's avatar
Aneesh Dogra committed
606 607 608
static double psnr(double d)
{
    return -10.0 * log(d) / log(10.0);
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
}

static void do_video_stats(AVFormatContext *os, OutputStream *ost,
                           int frame_size)
{
    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");
            exit_program(1);
        }
    }

    enc = ost->st->codec;
    if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
        frame_number = ost->frame_number;
Aneesh Dogra's avatar
Aneesh Dogra committed
630
        fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality / (float)FF_QP2LAMBDA);
631
        if (enc->flags&CODEC_FLAG_PSNR)
Aneesh Dogra's avatar
Aneesh Dogra committed
632
            fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
633 634 635 636 637 638 639

        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
640
        bitrate     = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
641 642
        avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
        fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
Aneesh Dogra's avatar
Aneesh Dogra committed
643
               (double)video_size / 1024, ti1, bitrate, avg_bitrate);
644 645 646 647
        fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type));
    }
}

648 649 650 651
/**
 * Read one frame for lavfi output for ost and encode it.
 */
static int poll_filter(OutputStream *ost)
652
{
653
    OutputFile    *of = output_files[ost->file_index];
654 655
    AVFilterBufferRef *picref;
    AVFrame *filtered_frame = NULL;
656 657 658 659 660 661 662 663 664 665 666 667 668 669
    int frame_size, ret;

    if (!ost->filtered_frame && !(ost->filtered_frame = avcodec_alloc_frame())) {
        return AVERROR(ENOMEM);
    } else
        avcodec_get_frame_defaults(ost->filtered_frame);
    filtered_frame = ost->filtered_frame;

    if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
        !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
        ret = av_buffersink_read_samples(ost->filter->filter, &picref,
                                         ost->st->codec->frame_size);
    else
        ret = av_buffersink_read(ost->filter->filter, &picref);
670

671 672
    if (ret < 0)
        return ret;
673

674 675 676 677 678 679 680 681
    avfilter_copy_buf_props(filtered_frame, picref);
    if (picref->pts != AV_NOPTS_VALUE) {
        filtered_frame->pts = av_rescale_q(picref->pts,
                                           ost->filter->filter->inputs[0]->time_base,
                                           ost->st->codec->time_base) -
                              av_rescale_q(of->start_time,
                                           AV_TIME_BASE_Q,
                                           ost->st->codec->time_base);
682

683
        if (of->start_time && filtered_frame->pts < 0) {
684
            avfilter_unref_buffer(picref);
685
            return 0;
686 687
        }
    }
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709

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

        do_video_out(of->ctx, ost, filtered_frame, &frame_size,
                     same_quant ? ost->last_quality :
                                  ost->st->codec->global_quality);
        if (vstats_filename && frame_size)
            do_video_stats(of->ctx, ost, frame_size);
        break;
    case AVMEDIA_TYPE_AUDIO:
        do_audio_out(of->ctx, ost, filtered_frame);
        break;
    default:
        // TODO support subtitle filters
        av_assert0(0);
    }

    avfilter_unref_buffer(picref);

710 711 712
    return 0;
}

713 714 715 716 717 718 719 720 721 722
/**
 * 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)
{
723
    int i, j, ret = 0;
724 725 726 727 728 729 730 731 732

    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;

733
            if (!output_streams[i]->filter || output_streams[i]->finished)
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
                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) {
750 751
            OutputFile *of = output_files[ost->file_index];

752
            ost->finished = 1;
753

754 755 756 757
            if (of->shortest) {
                for (j = 0; j < of->ctx->nb_streams; j++)
                    output_streams[of->ost_index + j]->finished = 1;
            }
758 759 760 761 762 763 764 765 766

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

    return ret;
}

767
static void print_report(int is_last_report, int64_t timer_start)
768 769 770 771 772 773 774 775 776 777 778
{
    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];

779 780 781
    if (!print_stats && !is_last_report)
        return;

782 783 784 785 786 787 788 789 790 791 792 793 794 795
    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;
    }


796
    oc = output_files[0]->ctx;
797 798

    total_size = avio_size(oc->pb);
Aneesh Dogra's avatar
Aneesh Dogra committed
799 800
    if (total_size < 0) // FIXME improve avio_size() so it works with non seekable output too
        total_size = avio_tell(oc->pb);
801 802 803 804

    buf[0] = '\0';
    ti1 = 1e10;
    vid = 0;
805
    for (i = 0; i < nb_output_streams; i++) {
806
        float q = -1;
807
        ost = output_streams[i];
808
        enc = ost->st->codec;
809
        if (!ost->stream_copy && enc->coded_frame)
Aneesh Dogra's avatar
Aneesh Dogra committed
810
            q = enc->coded_frame->quality / (float)FF_QP2LAMBDA;
811 812 813 814
        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
815
            float t = (av_gettime() - timer_start) / 1000000.0;
816 817 818

            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
819 820
                     frame_number, (t > 1) ? (int)(frame_number / t + 0.5) : 0, q);
            if (is_last_report)
821
                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
Aneesh Dogra's avatar
Aneesh Dogra committed
822
            if (qp_hist) {
823 824
                int j;
                int qp = lrintf(q);
Aneesh Dogra's avatar
Aneesh Dogra committed
825
                if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
826
                    qp_histogram[qp]++;
Aneesh Dogra's avatar
Aneesh Dogra committed
827
                for (j = 0; j < 32; j++)
828
                    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log2(qp_histogram[j] + 1)));
829
            }
Aneesh Dogra's avatar
Aneesh Dogra committed
830
            if (enc->flags&CODEC_FLAG_PSNR) {
831
                int j;
Aneesh Dogra's avatar
Aneesh Dogra committed
832 833 834
                double error, error_sum = 0;
                double scale, scale_sum = 0;
                char type[3] = { 'Y','U','V' };
835
                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR=");
Aneesh Dogra's avatar
Aneesh Dogra committed
836 837 838 839 840 841 842
                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;
843
                    }
Aneesh Dogra's avatar
Aneesh Dogra committed
844 845
                    if (j)
                        scale /= 4;
846 847
                    error_sum += error;
                    scale_sum += scale;
Aneesh Dogra's avatar
Aneesh Dogra committed
848
                    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], psnr(error / scale));
849
                }
Aneesh Dogra's avatar
Aneesh Dogra committed
850
                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum / scale_sum));
851 852 853 854 855 856 857 858 859 860 861
            }
            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;

862
    bitrate = (double)(total_size * 8) / ti1 / 1000.0;
863

864
    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
865 866 867
            "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
            (double)total_size / 1024, ti1, bitrate);

868 869 870
    if (nb_frames_dup || nb_frames_drop)
        snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
                nb_frames_dup, nb_frames_drop);
871

872
    av_log(NULL, AV_LOG_INFO, "%s    \r", buf);
873

874
    fflush(stderr);
875

876
    if (is_last_report) {
877
        int64_t raw= audio_size + video_size + extra_size;
878 879
        av_log(NULL, AV_LOG_INFO, "\n");
        av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
Aneesh Dogra's avatar
Aneesh Dogra committed
880 881 882 883
               video_size / 1024.0,
               audio_size / 1024.0,
               extra_size / 1024.0,
               100.0 * (total_size - raw) / raw
884 885 886 887
        );
    }
}

888
static void flush_encoders(void)
889 890 891
{
    int i, ret;

892
    for (i = 0; i < nb_output_streams; i++) {
893
        OutputStream   *ost = output_streams[i];
894
        AVCodecContext *enc = ost->st->codec;
895
        AVFormatContext *os = output_files[ost->file_index]->ctx;
896
        int stop_encoding = 0;
897

898
        if (!ost->encoding_needed)
899 900
            continue;

Aneesh Dogra's avatar
Aneesh Dogra committed
901
        if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
902
            continue;
903
        if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == AV_CODEC_ID_RAWVIDEO)
904 905
            continue;

Aneesh Dogra's avatar
Aneesh Dogra committed
906
        for (;;) {
907 908 909
            int (*encode)(AVCodecContext*, AVPacket*, const AVFrame*, int*) = NULL;
            const char *desc;
            int64_t *size;
910 911 912

            switch (ost->st->codec->codec_type) {
            case AVMEDIA_TYPE_AUDIO:
913 914 915
                encode = avcodec_encode_audio2;
                desc   = "Audio";
                size   = &audio_size;
916 917
                break;
            case AVMEDIA_TYPE_VIDEO:
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
                encode = avcodec_encode_video2;
                desc   = "Video";
                size   = &video_size;
                break;
            default:
                stop_encoding = 1;
            }

            if (encode) {
                AVPacket pkt;
                int got_packet;
                av_init_packet(&pkt);
                pkt.data = NULL;
                pkt.size = 0;

                ret = encode(enc, &pkt, NULL, &got_packet);
934
                if (ret < 0) {
935
                    av_log(NULL, AV_LOG_FATAL, "%s encoding failed\n", desc);
936 937
                    exit_program(1);
                }
938
                *size += ret;
939 940 941
                if (ost->logfile && enc->stats_out) {
                    fprintf(ost->logfile, "%s", enc->stats_out);
                }
942
                if (!got_packet) {
943 944 945
                    stop_encoding = 1;
                    break;
                }
946 947 948 949
                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);
950
                write_frame(os, &pkt, ost);
951
            }
952

953
            if (stop_encoding)
954
                break;
955 956 957 958
        }
    }
}

959 960 961 962 963
/*
 * Check whether a packet from ist should be written into ost at this time
 */
static int check_output_constraints(InputStream *ist, OutputStream *ost)
{
964 965
    OutputFile *of = output_files[ost->file_index];
    int ist_index  = input_files[ist->file_index]->ist_index + ist->st->index;
966 967 968 969

    if (ost->source_index != ist_index)
        return 0;

970
    if (of->start_time && ist->last_dts < of->start_time)
971 972 973 974 975 976 977
        return 0;

    return 1;
}

static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
{
978
    OutputFile *of = output_files[ost->file_index];
979 980 981 982 983 984 985 986 987
    int64_t ost_tb_start_time = av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base);
    AVPacket opkt;

    av_init_packet(&opkt);

    if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
        !ost->copy_initial_nonkeyframes)
        return;

988
    if (of->recording_time != INT64_MAX &&
989
        ist->last_dts >= of->recording_time + of->start_time) {
990
        ost->finished = 1;
991 992 993
        return;
    }

994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
    /* force the input stream PTS */
    if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
        audio_size += pkt->size;
    else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
        video_size += pkt->size;
        ost->sync_opts++;
    }

    if (pkt->pts != AV_NOPTS_VALUE)
        opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time;
    else
        opkt.pts = AV_NOPTS_VALUE;

    if (pkt->dts == AV_NOPTS_VALUE)
1008
        opkt.dts = av_rescale_q(ist->last_dts, AV_TIME_BASE_Q, ost->st->time_base);
1009 1010 1011 1012 1013 1014 1015
    else
        opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base);
    opkt.dts -= ost_tb_start_time;

    opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base);
    opkt.flags    = pkt->flags;

Aneesh Dogra's avatar
Aneesh Dogra committed
1016
    // FIXME remove the following 2 lines they shall be replaced by the bitstream filters
1017 1018 1019 1020
    if (  ost->st->codec->codec_id != AV_CODEC_ID_H264
       && ost->st->codec->codec_id != AV_CODEC_ID_MPEG1VIDEO
       && ost->st->codec->codec_id != AV_CODEC_ID_MPEG2VIDEO
       && ost->st->codec->codec_id != AV_CODEC_ID_VC1
1021 1022 1023 1024 1025 1026 1027 1028
       ) {
        if (av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY))
            opkt.destruct = av_destruct_packet;
    } else {
        opkt.data = pkt->data;
        opkt.size = pkt->size;
    }

1029
    write_frame(of->ctx, &opkt, ost);
1030 1031 1032 1033
    ost->st->codec->frame_number++;
    av_free_packet(&opkt);
}

1034 1035
static void rate_emu_sleep(InputStream *ist)
{
1036
    if (input_files[ist->file_index]->rate_emu) {
1037
        int64_t pts = av_rescale(ist->last_dts, 1000000, AV_TIME_BASE);
1038 1039
        int64_t now = av_gettime() - ist->start;
        if (pts > now)
1040
            av_usleep(pts - now);
1041 1042 1043
    }
}

1044
int guess_input_channel_layout(InputStream *ist)
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
{
    AVCodecContext *dec = ist->st->codec;

    if (!dec->channel_layout) {
        char layout_name[256];

        dec->channel_layout = av_get_default_channel_layout(dec->channels);
        if (!dec->channel_layout)
            return 0;
        av_get_channel_layout_string(layout_name, sizeof(layout_name),
                                     dec->channels, dec->channel_layout);
        av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for  Input Stream "
               "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name);
    }
    return 1;
}

1062
static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
1063
{
1064 1065
    AVFrame *decoded_frame;
    AVCodecContext *avctx = ist->st->codec;
1066
    int bps = av_get_bytes_per_sample(ist->st->codec->sample_fmt);
1067
    int i, ret, resample_changed;
Anton Khirnov's avatar