~ubuntu-branches/ubuntu/utopic/libav/utopic-proposed

« back to all changes in this revision

Viewing changes to avconv.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler, Reinhard Tartler, Rico Tzschichholz
  • Date: 2014-08-30 11:02:45 UTC
  • mfrom: (1.3.47 sid)
  • Revision ID: package-import@ubuntu.com-20140830110245-io3dg7q85wfr7125
Tags: 6:11~beta1-2
[ Reinhard Tartler ]
* Make libavcodec-dev depend on libavresample-dev

[ Rico Tzschichholz ]
* Some fixes and leftovers from soname bumps

Show diffs side-by-side

added added

removed removed

Lines of Context:
84
84
 
85
85
static FILE *vstats_file;
86
86
 
87
 
static int64_t video_size = 0;
88
 
static int64_t audio_size = 0;
89
 
static int64_t extra_size = 0;
90
 
static int nb_frames_dup = 0;
91
87
static int nb_frames_drop = 0;
92
88
 
93
89
 
149
145
    int i, j;
150
146
 
151
147
    for (i = 0; i < nb_filtergraphs; i++) {
152
 
        avfilter_graph_free(&filtergraphs[i]->graph);
153
 
        for (j = 0; j < filtergraphs[i]->nb_inputs; j++) {
154
 
            av_freep(&filtergraphs[i]->inputs[j]->name);
155
 
            av_freep(&filtergraphs[i]->inputs[j]);
156
 
        }
157
 
        av_freep(&filtergraphs[i]->inputs);
158
 
        for (j = 0; j < filtergraphs[i]->nb_outputs; j++) {
159
 
            av_freep(&filtergraphs[i]->outputs[j]->name);
160
 
            av_freep(&filtergraphs[i]->outputs[j]);
161
 
        }
162
 
        av_freep(&filtergraphs[i]->outputs);
163
 
        av_freep(&filtergraphs[i]->graph_desc);
 
148
        FilterGraph *fg = filtergraphs[i];
 
149
        avfilter_graph_free(&fg->graph);
 
150
        for (j = 0; j < fg->nb_inputs; j++) {
 
151
            av_freep(&fg->inputs[j]->name);
 
152
            av_freep(&fg->inputs[j]);
 
153
        }
 
154
        av_freep(&fg->inputs);
 
155
        for (j = 0; j < fg->nb_outputs; j++) {
 
156
            av_freep(&fg->outputs[j]->name);
 
157
            av_freep(&fg->outputs[j]);
 
158
        }
 
159
        av_freep(&fg->outputs);
 
160
        av_freep(&fg->graph_desc);
 
161
 
164
162
        av_freep(&filtergraphs[i]);
165
163
    }
166
164
    av_freep(&filtergraphs);
167
165
 
168
166
    /* close files */
169
167
    for (i = 0; i < nb_output_files; i++) {
170
 
        AVFormatContext *s = output_files[i]->ctx;
 
168
        OutputFile *of = output_files[i];
 
169
        AVFormatContext *s = of->ctx;
171
170
        if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE) && s->pb)
172
171
            avio_close(s->pb);
173
172
        avformat_free_context(s);
174
 
        av_dict_free(&output_files[i]->opts);
 
173
        av_dict_free(&of->opts);
 
174
 
175
175
        av_freep(&output_files[i]);
176
176
    }
177
177
    for (i = 0; i < nb_output_streams; i++) {
178
 
        AVBitStreamFilterContext *bsfc = output_streams[i]->bitstream_filters;
 
178
        OutputStream *ost = output_streams[i];
 
179
        AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
179
180
        while (bsfc) {
180
181
            AVBitStreamFilterContext *next = bsfc->next;
181
182
            av_bitstream_filter_close(bsfc);
182
183
            bsfc = next;
183
184
        }
184
 
        output_streams[i]->bitstream_filters = NULL;
185
 
        av_frame_free(&output_streams[i]->filtered_frame);
186
 
 
187
 
        av_parser_close(output_streams[i]->parser);
188
 
 
189
 
        av_freep(&output_streams[i]->forced_keyframes);
190
 
        av_freep(&output_streams[i]->avfilter);
191
 
        av_freep(&output_streams[i]->logfile_prefix);
 
185
        ost->bitstream_filters = NULL;
 
186
        av_frame_free(&ost->filtered_frame);
 
187
 
 
188
        av_parser_close(ost->parser);
 
189
 
 
190
        av_freep(&ost->forced_keyframes);
 
191
        av_freep(&ost->avfilter);
 
192
        av_freep(&ost->logfile_prefix);
 
193
 
 
194
        avcodec_free_context(&ost->enc_ctx);
 
195
 
192
196
        av_freep(&output_streams[i]);
193
197
    }
194
198
    for (i = 0; i < nb_input_files; i++) {
196
200
        av_freep(&input_files[i]);
197
201
    }
198
202
    for (i = 0; i < nb_input_streams; i++) {
199
 
        av_frame_free(&input_streams[i]->decoded_frame);
200
 
        av_frame_free(&input_streams[i]->filter_frame);
201
 
        av_dict_free(&input_streams[i]->opts);
202
 
        av_freep(&input_streams[i]->filters);
203
 
        av_freep(&input_streams[i]->hwaccel_device);
 
203
        InputStream *ist = input_streams[i];
 
204
 
 
205
        av_frame_free(&ist->decoded_frame);
 
206
        av_frame_free(&ist->filter_frame);
 
207
        av_dict_free(&ist->decoder_opts);
 
208
        av_freep(&ist->filters);
 
209
        av_freep(&ist->hwaccel_device);
 
210
 
 
211
        avcodec_free_context(&ist->dec_ctx);
 
212
 
204
213
        av_freep(&input_streams[i]);
205
214
    }
206
215
 
309
318
static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
310
319
{
311
320
    AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
312
 
    AVCodecContext          *avctx = ost->st->codec;
 
321
    AVCodecContext          *avctx = ost->enc_ctx;
313
322
    int ret;
314
323
 
315
324
    /*
371
380
    }
372
381
    ost->last_mux_dts = pkt->dts;
373
382
 
 
383
    ost->data_size += pkt->size;
 
384
    ost->packets_written++;
 
385
 
374
386
    pkt->stream_index = ost->index;
375
387
    ret = av_interleaved_write_frame(s, pkt);
376
388
    if (ret < 0) {
384
396
    OutputFile *of = output_files[ost->file_index];
385
397
 
386
398
    if (of->recording_time != INT64_MAX &&
387
 
        av_compare_ts(ost->sync_opts - ost->first_pts, ost->st->codec->time_base, of->recording_time,
 
399
        av_compare_ts(ost->sync_opts - ost->first_pts, ost->enc_ctx->time_base, of->recording_time,
388
400
                      AV_TIME_BASE_Q) >= 0) {
389
401
        ost->finished = 1;
390
402
        return 0;
395
407
static void do_audio_out(AVFormatContext *s, OutputStream *ost,
396
408
                         AVFrame *frame)
397
409
{
398
 
    AVCodecContext *enc = ost->st->codec;
 
410
    AVCodecContext *enc = ost->enc_ctx;
399
411
    AVPacket pkt;
400
412
    int got_packet = 0;
401
413
 
407
419
        frame->pts = ost->sync_opts;
408
420
    ost->sync_opts = frame->pts + frame->nb_samples;
409
421
 
 
422
    ost->samples_encoded += frame->nb_samples;
 
423
    ost->frames_encoded++;
 
424
 
410
425
    if (avcodec_encode_audio2(enc, &pkt, frame, &got_packet) < 0) {
411
426
        av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
412
427
        exit_program(1);
413
428
    }
414
429
 
415
430
    if (got_packet) {
416
 
        if (pkt.pts != AV_NOPTS_VALUE)
417
 
            pkt.pts      = av_rescale_q(pkt.pts,      enc->time_base, ost->st->time_base);
418
 
        if (pkt.dts != AV_NOPTS_VALUE)
419
 
            pkt.dts      = av_rescale_q(pkt.dts,      enc->time_base, ost->st->time_base);
420
 
        if (pkt.duration > 0)
421
 
            pkt.duration = av_rescale_q(pkt.duration, enc->time_base, ost->st->time_base);
422
 
 
 
431
        av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base);
423
432
        write_frame(s, &pkt, ost);
424
 
 
425
 
        audio_size += pkt.size;
426
433
    }
427
434
}
428
435
 
445
452
        return;
446
453
    }
447
454
 
448
 
    enc = ost->st->codec;
 
455
    enc = ost->enc_ctx;
449
456
 
450
457
    if (!subtitle_out) {
451
458
        subtitle_out = av_malloc(subtitle_out_max_size);
469
476
        sub->pts               += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
470
477
        sub->end_display_time  -= sub->start_display_time;
471
478
        sub->start_display_time = 0;
 
479
 
 
480
        ost->frames_encoded++;
 
481
 
472
482
        subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
473
483
                                                    subtitle_out_max_size, sub);
474
484
        if (subtitle_out_size < 0) {
499
509
{
500
510
    int ret, format_video_sync;
501
511
    AVPacket pkt;
502
 
    AVCodecContext *enc = ost->st->codec;
 
512
    AVCodecContext *enc = ost->enc_ctx;
503
513
 
504
514
    *frame_size = 0;
505
515
 
512
522
        in_picture->pts != AV_NOPTS_VALUE &&
513
523
        in_picture->pts < ost->sync_opts) {
514
524
        nb_frames_drop++;
515
 
        av_log(NULL, AV_LOG_VERBOSE, "*** drop!\n");
 
525
        av_log(NULL, AV_LOG_WARNING,
 
526
               "*** dropping frame %d from stream %d at ts %"PRId64"\n",
 
527
               ost->frame_number, ost->st->index, in_picture->pts);
516
528
        return;
517
529
    }
518
530
 
547
559
    } else {
548
560
        int got_packet;
549
561
 
550
 
        if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME) &&
 
562
        if (enc->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME) &&
551
563
            ost->top_field_first >= 0)
552
564
            in_picture->top_field_first = !!ost->top_field_first;
553
565
 
554
 
        in_picture->quality = ost->st->codec->global_quality;
 
566
        in_picture->quality = enc->global_quality;
555
567
        if (!enc->me_threshold)
556
568
            in_picture->pict_type = 0;
557
569
        if (ost->forced_kf_index < ost->forced_kf_count &&
559
571
            in_picture->pict_type = AV_PICTURE_TYPE_I;
560
572
            ost->forced_kf_index++;
561
573
        }
 
574
 
 
575
        ost->frames_encoded++;
 
576
 
562
577
        ret = avcodec_encode_video2(enc, &pkt, in_picture, &got_packet);
563
578
        if (ret < 0) {
564
579
            av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
566
581
        }
567
582
 
568
583
        if (got_packet) {
569
 
            if (pkt.pts != AV_NOPTS_VALUE)
570
 
                pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
571
 
            if (pkt.dts != AV_NOPTS_VALUE)
572
 
                pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
573
 
 
 
584
            av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base);
574
585
            write_frame(s, &pkt, ost);
575
586
            *frame_size = pkt.size;
576
 
            video_size += pkt.size;
577
587
 
578
588
            /* if two pass, output log */
579
589
            if (ost->logfile && enc->stats_out) {
610
620
        }
611
621
    }
612
622
 
613
 
    enc = ost->st->codec;
 
623
    enc = ost->enc_ctx;
614
624
    if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
615
625
        frame_number = ost->frame_number;
616
626
        fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality / (float)FF_QP2LAMBDA);
624
634
            ti1 = 0.01;
625
635
 
626
636
        bitrate     = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
627
 
        avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
 
637
        avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
628
638
        fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
629
 
               (double)video_size / 1024, ti1, bitrate, avg_bitrate);
 
639
               (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
630
640
        fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type));
631
641
    }
632
642
}
648
658
    if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
649
659
        !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
650
660
        ret = av_buffersink_get_samples(ost->filter->filter, filtered_frame,
651
 
                                         ost->st->codec->frame_size);
 
661
                                         ost->enc_ctx->frame_size);
652
662
    else
653
663
        ret = av_buffersink_get_frame(ost->filter->filter, filtered_frame);
654
664
 
659
669
        int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
660
670
        filtered_frame->pts = av_rescale_q(filtered_frame->pts,
661
671
                                           ost->filter->filter->inputs[0]->time_base,
662
 
                                           ost->st->codec->time_base) -
 
672
                                           ost->enc_ctx->time_base) -
663
673
                              av_rescale_q(start_time,
664
674
                                           AV_TIME_BASE_Q,
665
 
                                           ost->st->codec->time_base);
 
675
                                           ost->enc_ctx->time_base);
666
676
    }
667
677
 
668
678
    switch (ost->filter->filter->inputs[0]->type) {
669
679
    case AVMEDIA_TYPE_VIDEO:
670
680
        if (!ost->frame_aspect_ratio)
671
 
            ost->st->codec->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
 
681
            ost->enc_ctx->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
672
682
 
673
683
        do_video_out(of->ctx, ost, filtered_frame, &frame_size);
674
684
        if (vstats_filename && frame_size)
723
733
            if (!output_streams[i]->filter || output_streams[i]->finished)
724
734
                continue;
725
735
 
726
 
            pts = av_rescale_q(pts, output_streams[i]->st->codec->time_base,
 
736
            pts = av_rescale_q(pts, output_streams[i]->enc_ctx->time_base,
727
737
                               AV_TIME_BASE_Q);
728
738
            if (pts < min_pts) {
729
739
                min_pts = pts;
746
756
    return ret;
747
757
}
748
758
 
 
759
static void print_final_stats(int64_t total_size)
 
760
{
 
761
    uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
 
762
    uint64_t data_size = 0;
 
763
    float percent = -1.0;
 
764
    int i, j;
 
765
 
 
766
    for (i = 0; i < nb_output_streams; i++) {
 
767
        OutputStream *ost = output_streams[i];
 
768
        switch (ost->enc_ctx->codec_type) {
 
769
            case AVMEDIA_TYPE_VIDEO: video_size += ost->data_size; break;
 
770
            case AVMEDIA_TYPE_AUDIO: audio_size += ost->data_size; break;
 
771
            default:                 other_size += ost->data_size; break;
 
772
        }
 
773
        extra_size += ost->enc_ctx->extradata_size;
 
774
        data_size  += ost->data_size;
 
775
    }
 
776
 
 
777
    if (data_size && total_size >= data_size)
 
778
        percent = 100.0 * (total_size - data_size) / data_size;
 
779
 
 
780
    av_log(NULL, AV_LOG_INFO, "\n");
 
781
    av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB other streams:%1.0fkB global headers:%1.0fkB muxing overhead: ",
 
782
           video_size / 1024.0,
 
783
           audio_size / 1024.0,
 
784
           other_size / 1024.0,
 
785
           extra_size / 1024.0);
 
786
    if (percent >= 0.0)
 
787
        av_log(NULL, AV_LOG_INFO, "%f%%", percent);
 
788
    else
 
789
        av_log(NULL, AV_LOG_INFO, "unknown");
 
790
    av_log(NULL, AV_LOG_INFO, "\n");
 
791
 
 
792
    /* print verbose per-stream stats */
 
793
    for (i = 0; i < nb_input_files; i++) {
 
794
        InputFile *f = input_files[i];
 
795
        uint64_t total_packets = 0, total_size = 0;
 
796
 
 
797
        av_log(NULL, AV_LOG_VERBOSE, "Input file #%d (%s):\n",
 
798
               i, f->ctx->filename);
 
799
 
 
800
        for (j = 0; j < f->nb_streams; j++) {
 
801
            InputStream *ist = input_streams[f->ist_index + j];
 
802
            enum AVMediaType type = ist->dec_ctx->codec_type;
 
803
 
 
804
            total_size    += ist->data_size;
 
805
            total_packets += ist->nb_packets;
 
806
 
 
807
            av_log(NULL, AV_LOG_VERBOSE, "  Input stream #%d:%d (%s): ",
 
808
                   i, j, media_type_string(type));
 
809
            av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
 
810
                   ist->nb_packets, ist->data_size);
 
811
 
 
812
            if (ist->decoding_needed) {
 
813
                av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames decoded",
 
814
                       ist->frames_decoded);
 
815
                if (type == AVMEDIA_TYPE_AUDIO)
 
816
                    av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ist->samples_decoded);
 
817
                av_log(NULL, AV_LOG_VERBOSE, "; ");
 
818
            }
 
819
 
 
820
            av_log(NULL, AV_LOG_VERBOSE, "\n");
 
821
        }
 
822
 
 
823
        av_log(NULL, AV_LOG_VERBOSE, "  Total: %"PRIu64" packets (%"PRIu64" bytes) demuxed\n",
 
824
               total_packets, total_size);
 
825
    }
 
826
 
 
827
    for (i = 0; i < nb_output_files; i++) {
 
828
        OutputFile *of = output_files[i];
 
829
        uint64_t total_packets = 0, total_size = 0;
 
830
 
 
831
        av_log(NULL, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
 
832
               i, of->ctx->filename);
 
833
 
 
834
        for (j = 0; j < of->ctx->nb_streams; j++) {
 
835
            OutputStream *ost = output_streams[of->ost_index + j];
 
836
            enum AVMediaType type = ost->enc_ctx->codec_type;
 
837
 
 
838
            total_size    += ost->data_size;
 
839
            total_packets += ost->packets_written;
 
840
 
 
841
            av_log(NULL, AV_LOG_VERBOSE, "  Output stream #%d:%d (%s): ",
 
842
                   i, j, media_type_string(type));
 
843
            if (ost->encoding_needed) {
 
844
                av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
 
845
                       ost->frames_encoded);
 
846
                if (type == AVMEDIA_TYPE_AUDIO)
 
847
                    av_log(NULL, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->samples_encoded);
 
848
                av_log(NULL, AV_LOG_VERBOSE, "; ");
 
849
            }
 
850
 
 
851
            av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
 
852
                   ost->packets_written, ost->data_size);
 
853
 
 
854
            av_log(NULL, AV_LOG_VERBOSE, "\n");
 
855
        }
 
856
 
 
857
        av_log(NULL, AV_LOG_VERBOSE, "  Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
 
858
               total_packets, total_size);
 
859
    }
 
860
}
 
861
 
749
862
static void print_report(int is_last_report, int64_t timer_start)
750
863
{
751
864
    char buf[1024];
794
907
    for (i = 0; i < nb_output_streams; i++) {
795
908
        float q = -1;
796
909
        ost = output_streams[i];
797
 
        enc = ost->st->codec;
 
910
        enc = ost->enc_ctx;
798
911
        if (!ost->stream_copy && enc->coded_frame)
799
912
            q = enc->coded_frame->quality / (float)FF_QP2LAMBDA;
800
913
        if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
841
954
            vid = 1;
842
955
        }
843
956
        /* compute min output value */
844
 
        pts = (double)ost->st->pts.val * av_q2d(ost->st->time_base);
 
957
        pts = (double)ost->last_mux_dts * av_q2d(ost->st->time_base);
845
958
        if ((pts < ti1) && (pts > 0))
846
959
            ti1 = pts;
847
960
    }
854
967
            "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
855
968
            (double)total_size / 1024, ti1, bitrate);
856
969
 
857
 
    if (nb_frames_dup || nb_frames_drop)
858
 
        snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
859
 
                nb_frames_dup, nb_frames_drop);
 
970
    if (nb_frames_drop)
 
971
        snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " drop=%d",
 
972
                 nb_frames_drop);
860
973
 
861
974
    av_log(NULL, AV_LOG_INFO, "%s    \r", buf);
862
975
 
863
976
    fflush(stderr);
864
977
 
865
 
    if (is_last_report) {
866
 
        int64_t raw   = audio_size + video_size + extra_size;
867
 
        float percent = 0.0;
868
 
 
869
 
        if (raw)
870
 
            percent = 100.0 * (total_size - raw) / raw;
871
 
 
872
 
        av_log(NULL, AV_LOG_INFO, "\n");
873
 
        av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
874
 
               video_size / 1024.0,
875
 
               audio_size / 1024.0,
876
 
               extra_size / 1024.0,
877
 
               percent);
878
 
    }
 
978
    if (is_last_report)
 
979
        print_final_stats(total_size);
 
980
 
879
981
}
880
982
 
881
983
static void flush_encoders(void)
884
986
 
885
987
    for (i = 0; i < nb_output_streams; i++) {
886
988
        OutputStream   *ost = output_streams[i];
887
 
        AVCodecContext *enc = ost->st->codec;
 
989
        AVCodecContext *enc = ost->enc_ctx;
888
990
        AVFormatContext *os = output_files[ost->file_index]->ctx;
889
991
        int stop_encoding = 0;
890
992
 
891
993
        if (!ost->encoding_needed)
892
994
            continue;
893
995
 
894
 
        if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
 
996
        if (enc->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
895
997
            continue;
896
 
        if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == AV_CODEC_ID_RAWVIDEO)
 
998
        if (enc->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == AV_CODEC_ID_RAWVIDEO)
897
999
            continue;
898
1000
 
899
1001
        for (;;) {
900
1002
            int (*encode)(AVCodecContext*, AVPacket*, const AVFrame*, int*) = NULL;
901
1003
            const char *desc;
902
 
            int64_t *size;
903
1004
 
904
 
            switch (ost->st->codec->codec_type) {
 
1005
            switch (enc->codec_type) {
905
1006
            case AVMEDIA_TYPE_AUDIO:
906
1007
                encode = avcodec_encode_audio2;
907
1008
                desc   = "Audio";
908
 
                size   = &audio_size;
909
1009
                break;
910
1010
            case AVMEDIA_TYPE_VIDEO:
911
1011
                encode = avcodec_encode_video2;
912
1012
                desc   = "Video";
913
 
                size   = &video_size;
914
1013
                break;
915
1014
            default:
916
1015
                stop_encoding = 1;
928
1027
                    av_log(NULL, AV_LOG_FATAL, "%s encoding failed\n", desc);
929
1028
                    exit_program(1);
930
1029
                }
931
 
                *size += ret;
932
1030
                if (ost->logfile && enc->stats_out) {
933
1031
                    fprintf(ost->logfile, "%s", enc->stats_out);
934
1032
                }
936
1034
                    stop_encoding = 1;
937
1035
                    break;
938
1036
                }
939
 
                if (pkt.pts != AV_NOPTS_VALUE)
940
 
                    pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
941
 
                if (pkt.dts != AV_NOPTS_VALUE)
942
 
                    pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
943
 
                if (pkt.duration > 0)
944
 
                    pkt.duration = av_rescale_q(pkt.duration, enc->time_base, ost->st->time_base);
 
1037
                av_packet_rescale_ts(&pkt, enc->time_base, ost->st->time_base);
945
1038
                write_frame(os, &pkt, ost);
946
1039
            }
947
1040
 
999
1092
    }
1000
1093
 
1001
1094
    /* force the input stream PTS */
1002
 
    if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
1003
 
        audio_size += pkt->size;
1004
 
    else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
1005
 
        video_size += pkt->size;
 
1095
    if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
1006
1096
        ost->sync_opts++;
1007
 
    }
1008
1097
 
1009
1098
    if (pkt->pts != AV_NOPTS_VALUE)
1010
1099
        opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time;
1021
1110
    opkt.flags    = pkt->flags;
1022
1111
 
1023
1112
    // FIXME remove the following 2 lines they shall be replaced by the bitstream filters
1024
 
    if (  ost->st->codec->codec_id != AV_CODEC_ID_H264
1025
 
       && ost->st->codec->codec_id != AV_CODEC_ID_MPEG1VIDEO
1026
 
       && ost->st->codec->codec_id != AV_CODEC_ID_MPEG2VIDEO
1027
 
       && ost->st->codec->codec_id != AV_CODEC_ID_VC1
 
1113
    if (  ost->enc_ctx->codec_id != AV_CODEC_ID_H264
 
1114
       && ost->enc_ctx->codec_id != AV_CODEC_ID_MPEG1VIDEO
 
1115
       && ost->enc_ctx->codec_id != AV_CODEC_ID_MPEG2VIDEO
 
1116
       && ost->enc_ctx->codec_id != AV_CODEC_ID_VC1
1028
1117
       ) {
1029
1118
        if (av_parser_change(ost->parser, ost->st->codec,
1030
1119
                             &opkt.data, &opkt.size,
1040
1129
    }
1041
1130
 
1042
1131
    write_frame(of->ctx, &opkt, ost);
1043
 
    ost->st->codec->frame_number++;
1044
1132
}
1045
1133
 
1046
1134
int guess_input_channel_layout(InputStream *ist)
1047
1135
{
1048
 
    AVCodecContext *dec = ist->st->codec;
 
1136
    AVCodecContext *dec = ist->dec_ctx;
1049
1137
 
1050
1138
    if (!dec->channel_layout) {
1051
1139
        char layout_name[256];
1064
1152
static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
1065
1153
{
1066
1154
    AVFrame *decoded_frame, *f;
1067
 
    AVCodecContext *avctx = ist->st->codec;
 
1155
    AVCodecContext *avctx = ist->dec_ctx;
1068
1156
    int i, ret, err = 0, resample_changed;
1069
1157
 
1070
1158
    if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
1082
1170
        return ret;
1083
1171
    }
1084
1172
 
 
1173
    ist->samples_decoded += decoded_frame->nb_samples;
 
1174
    ist->frames_decoded++;
 
1175
 
1085
1176
    /* if the decoder provides a pts, use it instead of the last packet pts.
1086
1177
       the decoder could be delaying output by a packet or more. */
1087
1178
    if (decoded_frame->pts != AV_NOPTS_VALUE)
1088
1179
        ist->next_dts = decoded_frame->pts;
1089
 
    else if (pkt->pts != AV_NOPTS_VALUE) {
 
1180
    else if (pkt->pts != AV_NOPTS_VALUE)
1090
1181
        decoded_frame->pts = pkt->pts;
1091
 
        pkt->pts           = AV_NOPTS_VALUE;
1092
 
    }
 
1182
    pkt->pts           = AV_NOPTS_VALUE;
1093
1183
 
1094
1184
    resample_changed = ist->resample_sample_fmt     != decoded_frame->format         ||
1095
1185
                       ist->resample_channels       != avctx->channels               ||
1135
1225
    if (decoded_frame->pts != AV_NOPTS_VALUE)
1136
1226
        decoded_frame->pts = av_rescale_q(decoded_frame->pts,
1137
1227
                                          ist->st->time_base,
1138
 
                                          (AVRational){1, ist->st->codec->sample_rate});
 
1228
                                          (AVRational){1, avctx->sample_rate});
1139
1229
    for (i = 0; i < ist->nb_filters; i++) {
1140
1230
        if (i < ist->nb_filters - 1) {
1141
1231
            f = ist->filter_frame;
1166
1256
        return AVERROR(ENOMEM);
1167
1257
    decoded_frame = ist->decoded_frame;
1168
1258
 
1169
 
    ret = avcodec_decode_video2(ist->st->codec,
 
1259
    ret = avcodec_decode_video2(ist->dec_ctx,
1170
1260
                                decoded_frame, got_output, pkt);
1171
1261
    if (!*got_output || ret < 0) {
1172
1262
        if (!pkt->size) {
1176
1266
        return ret;
1177
1267
    }
1178
1268
 
 
1269
    ist->frames_decoded++;
 
1270
 
1179
1271
    if (ist->hwaccel_retrieve_data && decoded_frame->format == ist->hwaccel_pix_fmt) {
1180
 
        err = ist->hwaccel_retrieve_data(ist->st->codec, decoded_frame);
 
1272
        err = ist->hwaccel_retrieve_data(ist->dec_ctx, decoded_frame);
1181
1273
        if (err < 0)
1182
1274
            goto fail;
1183
1275
    }
1239
1331
static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
1240
1332
{
1241
1333
    AVSubtitle subtitle;
1242
 
    int i, ret = avcodec_decode_subtitle2(ist->st->codec,
 
1334
    int i, ret = avcodec_decode_subtitle2(ist->dec_ctx,
1243
1335
                                          &subtitle, got_output, pkt);
1244
1336
    if (ret < 0)
1245
1337
        return ret;
1246
1338
    if (!*got_output)
1247
1339
        return ret;
1248
1340
 
 
1341
    ist->frames_decoded++;
 
1342
 
1249
1343
    for (i = 0; i < nb_output_streams; i++) {
1250
1344
        OutputStream *ost = output_streams[i];
1251
1345
 
1260
1354
}
1261
1355
 
1262
1356
/* pkt = NULL means EOF (needed to flush decoder buffers) */
1263
 
static int output_packet(InputStream *ist, const AVPacket *pkt)
 
1357
static int process_input_packet(InputStream *ist, const AVPacket *pkt)
1264
1358
{
1265
1359
    int i;
1266
1360
    int got_output;
1269
1363
    if (ist->next_dts == AV_NOPTS_VALUE)
1270
1364
        ist->next_dts = ist->last_dts;
1271
1365
 
1272
 
    if (pkt == NULL) {
 
1366
    if (!pkt) {
1273
1367
        /* EOF handling */
1274
1368
        av_init_packet(&avpkt);
1275
1369
        avpkt.data = NULL;
1289
1383
 
1290
1384
        ist->last_dts = ist->next_dts;
1291
1385
 
1292
 
        if (avpkt.size && avpkt.size != pkt->size) {
 
1386
        if (avpkt.size && avpkt.size != pkt->size &&
 
1387
            !(ist->dec->capabilities & CODEC_CAP_SUBFRAMES)) {
1293
1388
            av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,
1294
1389
                   "Multiple frames in a packet from stream %d\n", pkt->stream_index);
1295
1390
            ist->showed_multi_packet_warning = 1;
1296
1391
        }
1297
1392
 
1298
 
        switch (ist->st->codec->codec_type) {
 
1393
        switch (ist->dec_ctx->codec_type) {
1299
1394
        case AVMEDIA_TYPE_AUDIO:
1300
1395
            ret = decode_audio    (ist, &avpkt, &got_output);
1301
1396
            break;
1306
1401
            else if (ist->st->avg_frame_rate.num)
1307
1402
                ist->next_dts += av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate),
1308
1403
                                              AV_TIME_BASE_Q);
1309
 
            else if (ist->st->codec->time_base.num != 0) {
 
1404
            else if (ist->dec_ctx->time_base.num != 0) {
1310
1405
                int ticks      = ist->st->parser ? ist->st->parser->repeat_pict + 1 :
1311
 
                                                   ist->st->codec->ticks_per_frame;
1312
 
                ist->next_dts += av_rescale_q(ticks, ist->st->codec->time_base, AV_TIME_BASE_Q);
 
1406
                                                   ist->dec_ctx->ticks_per_frame;
 
1407
                ist->next_dts += av_rescale_q(ticks, ist->dec_ctx->time_base, AV_TIME_BASE_Q);
1313
1408
            }
1314
1409
            break;
1315
1410
        case AVMEDIA_TYPE_SUBTITLE:
1334
1429
    /* handle stream copy */
1335
1430
    if (!ist->decoding_needed) {
1336
1431
        ist->last_dts = ist->next_dts;
1337
 
        switch (ist->st->codec->codec_type) {
 
1432
        switch (ist->dec_ctx->codec_type) {
1338
1433
        case AVMEDIA_TYPE_AUDIO:
1339
 
            ist->next_dts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
1340
 
                             ist->st->codec->sample_rate;
 
1434
            ist->next_dts += ((int64_t)AV_TIME_BASE * ist->dec_ctx->frame_size) /
 
1435
                             ist->dec_ctx->sample_rate;
1341
1436
            break;
1342
1437
        case AVMEDIA_TYPE_VIDEO:
1343
 
            if (ist->st->codec->time_base.num != 0) {
1344
 
                int ticks = ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->st->codec->ticks_per_frame;
 
1438
            if (ist->dec_ctx->time_base.num != 0) {
 
1439
                int ticks = ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->dec_ctx->ticks_per_frame;
1345
1440
                ist->next_dts += ((int64_t)AV_TIME_BASE *
1346
 
                                  ist->st->codec->time_base.num * ticks) /
1347
 
                                  ist->st->codec->time_base.den;
 
1441
                                  ist->dec_ctx->time_base.num * ticks) /
 
1442
                                  ist->dec_ctx->time_base.den;
1348
1443
            }
1349
1444
            break;
1350
1445
        }
1443
1538
        AVCodec *codec = ist->dec;
1444
1539
        if (!codec) {
1445
1540
            snprintf(error, error_len, "Decoder (codec id %d) not found for input stream #%d:%d",
1446
 
                    ist->st->codec->codec_id, ist->file_index, ist->st->index);
 
1541
                    ist->dec_ctx->codec_id, ist->file_index, ist->st->index);
1447
1542
            return AVERROR(EINVAL);
1448
1543
        }
1449
1544
 
1452
1547
        for (i = 0; i < nb_output_streams; i++) {
1453
1548
            OutputStream *ost = output_streams[i];
1454
1549
            if (ost->source_index == ist_index) {
1455
 
                update_sample_fmt(ist->st->codec, codec, ost->st->codec);
 
1550
                update_sample_fmt(ist->dec_ctx, codec, ost->enc_ctx);
1456
1551
                break;
1457
1552
            }
1458
1553
        }
1459
1554
 
1460
 
        ist->st->codec->opaque      = ist;
1461
 
        ist->st->codec->get_format  = get_format;
1462
 
        ist->st->codec->get_buffer2 = get_buffer;
1463
 
        ist->st->codec->thread_safe_callbacks = 1;
1464
 
 
1465
 
        av_opt_set_int(ist->st->codec, "refcounted_frames", 1, 0);
1466
 
 
1467
 
        if (!av_dict_get(ist->opts, "threads", NULL, 0))
1468
 
            av_dict_set(&ist->opts, "threads", "auto", 0);
1469
 
        if ((ret = avcodec_open2(ist->st->codec, codec, &ist->opts)) < 0) {
 
1555
        ist->dec_ctx->opaque                = ist;
 
1556
        ist->dec_ctx->get_format            = get_format;
 
1557
        ist->dec_ctx->get_buffer2           = get_buffer;
 
1558
        ist->dec_ctx->thread_safe_callbacks = 1;
 
1559
 
 
1560
        av_opt_set_int(ist->dec_ctx, "refcounted_frames", 1, 0);
 
1561
 
 
1562
        if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
 
1563
            av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
 
1564
        if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
1470
1565
            char errbuf[128];
1471
1566
            if (ret == AVERROR_EXPERIMENTAL)
1472
1567
                abort_codec_experimental(codec, 0);
1479
1574
                     ist->file_index, ist->st->index, errbuf);
1480
1575
            return ret;
1481
1576
        }
1482
 
        assert_avoptions(ist->opts);
 
1577
        assert_avoptions(ist->decoder_opts);
1483
1578
    }
1484
1579
 
1485
 
    ist->last_dts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
 
1580
    ist->last_dts = ist->st->avg_frame_rate.num ? - ist->dec_ctx->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
1486
1581
    ist->next_dts = AV_NOPTS_VALUE;
1487
1582
    init_pts_correction(&ist->pts_ctx);
1488
 
    ist->is_start = 1;
1489
1583
 
1490
1584
    return 0;
1491
1585
}
1500
1594
        int i;
1501
1595
 
1502
1596
        for (i = 0; i < fg->nb_inputs; i++)
1503
 
            if (fg->inputs[i]->ist->st->codec->codec_type == ost->st->codec->codec_type)
 
1597
            if (fg->inputs[i]->ist->dec_ctx->codec_type == ost->enc_ctx->codec_type)
1504
1598
                return fg->inputs[i]->ist;
1505
1599
    }
1506
1600
 
1538
1632
    }
1539
1633
}
1540
1634
 
 
1635
static void set_encoder_id(OutputFile *of, OutputStream *ost)
 
1636
{
 
1637
    AVDictionaryEntry *e;
 
1638
 
 
1639
    uint8_t *encoder_string;
 
1640
    int encoder_string_len;
 
1641
    int format_flags = 0;
 
1642
 
 
1643
    e = av_dict_get(of->opts, "fflags", NULL, 0);
 
1644
    if (e) {
 
1645
        const AVOption *o = av_opt_find(of->ctx, "fflags", NULL, 0, 0);
 
1646
        if (!o)
 
1647
            return;
 
1648
        av_opt_eval_flags(of->ctx, o, e->value, &format_flags);
 
1649
    }
 
1650
 
 
1651
    encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2;
 
1652
    encoder_string     = av_mallocz(encoder_string_len);
 
1653
    if (!encoder_string)
 
1654
        exit_program(1);
 
1655
 
 
1656
    if (!(format_flags & AVFMT_FLAG_BITEXACT))
 
1657
        av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
 
1658
    av_strlcat(encoder_string, ost->enc->name, encoder_string_len);
 
1659
    av_dict_set(&ost->st->metadata, "encoder",  encoder_string,
 
1660
                AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE);
 
1661
}
 
1662
 
1541
1663
static int transcode_init(void)
1542
1664
{
1543
1665
    int ret = 0, i, j, k;
1544
1666
    AVFormatContext *oc;
1545
 
    AVCodecContext *codec;
1546
1667
    OutputStream *ost;
1547
1668
    InputStream *ist;
1548
1669
    char error[1024];
1573
1694
 
1574
1695
    /* for each output stream, we compute the right encoding parameters */
1575
1696
    for (i = 0; i < nb_output_streams; i++) {
1576
 
        AVCodecContext *icodec = NULL;
 
1697
        AVCodecContext *enc_ctx;
 
1698
        AVCodecContext *dec_ctx = NULL;
1577
1699
        ost = output_streams[i];
1578
1700
        oc  = output_files[ost->file_index]->ctx;
1579
1701
        ist = get_input_stream(ost);
1581
1703
        if (ost->attachment_filename)
1582
1704
            continue;
1583
1705
 
1584
 
        codec  = ost->st->codec;
 
1706
        enc_ctx = ost->enc_ctx;
1585
1707
 
1586
1708
        if (ist) {
1587
 
            icodec = ist->st->codec;
 
1709
            dec_ctx = ist->dec_ctx;
1588
1710
 
1589
1711
            ost->st->disposition          = ist->st->disposition;
1590
 
            codec->bits_per_raw_sample    = icodec->bits_per_raw_sample;
1591
 
            codec->chroma_sample_location = icodec->chroma_sample_location;
 
1712
            enc_ctx->bits_per_raw_sample    = dec_ctx->bits_per_raw_sample;
 
1713
            enc_ctx->chroma_sample_location = dec_ctx->chroma_sample_location;
1592
1714
        }
1593
1715
 
1594
1716
        if (ost->stream_copy) {
1597
1719
 
1598
1720
            av_assert0(ist && !ost->filter);
1599
1721
 
1600
 
            extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
 
1722
            extra_size = (uint64_t)dec_ctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
1601
1723
 
1602
1724
            if (extra_size > INT_MAX) {
1603
1725
                return AVERROR(EINVAL);
1604
1726
            }
1605
1727
 
1606
1728
            /* if stream_copy is selected, no need to decode or encode */
1607
 
            codec->codec_id   = icodec->codec_id;
1608
 
            codec->codec_type = icodec->codec_type;
 
1729
            enc_ctx->codec_id   = dec_ctx->codec_id;
 
1730
            enc_ctx->codec_type = dec_ctx->codec_type;
1609
1731
 
1610
 
            if (!codec->codec_tag) {
 
1732
            if (!enc_ctx->codec_tag) {
1611
1733
                if (!oc->oformat->codec_tag ||
1612
 
                     av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == codec->codec_id ||
1613
 
                     av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0)
1614
 
                    codec->codec_tag = icodec->codec_tag;
 
1734
                     av_codec_get_id (oc->oformat->codec_tag, dec_ctx->codec_tag) == enc_ctx->codec_id ||
 
1735
                     av_codec_get_tag(oc->oformat->codec_tag, dec_ctx->codec_id) <= 0)
 
1736
                    enc_ctx->codec_tag = dec_ctx->codec_tag;
1615
1737
            }
1616
1738
 
1617
 
            codec->bit_rate       = icodec->bit_rate;
1618
 
            codec->rc_max_rate    = icodec->rc_max_rate;
1619
 
            codec->rc_buffer_size = icodec->rc_buffer_size;
1620
 
            codec->field_order    = icodec->field_order;
1621
 
            codec->extradata      = av_mallocz(extra_size);
1622
 
            if (!codec->extradata) {
 
1739
            enc_ctx->bit_rate       = dec_ctx->bit_rate;
 
1740
            enc_ctx->rc_max_rate    = dec_ctx->rc_max_rate;
 
1741
            enc_ctx->rc_buffer_size = dec_ctx->rc_buffer_size;
 
1742
            enc_ctx->field_order    = dec_ctx->field_order;
 
1743
            enc_ctx->extradata      = av_mallocz(extra_size);
 
1744
            if (!enc_ctx->extradata) {
1623
1745
                return AVERROR(ENOMEM);
1624
1746
            }
1625
 
            memcpy(codec->extradata, icodec->extradata, icodec->extradata_size);
1626
 
            codec->extradata_size = icodec->extradata_size;
 
1747
            memcpy(enc_ctx->extradata, dec_ctx->extradata, dec_ctx->extradata_size);
 
1748
            enc_ctx->extradata_size = dec_ctx->extradata_size;
1627
1749
            if (!copy_tb) {
1628
 
                codec->time_base      = icodec->time_base;
1629
 
                codec->time_base.num *= icodec->ticks_per_frame;
1630
 
                av_reduce(&codec->time_base.num, &codec->time_base.den,
1631
 
                          codec->time_base.num, codec->time_base.den, INT_MAX);
 
1750
                enc_ctx->time_base      = dec_ctx->time_base;
 
1751
                enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
 
1752
                av_reduce(&enc_ctx->time_base.num, &enc_ctx->time_base.den,
 
1753
                          enc_ctx->time_base.num, enc_ctx->time_base.den, INT_MAX);
1632
1754
            } else
1633
 
                codec->time_base = ist->st->time_base;
1634
 
 
1635
 
            ost->parser = av_parser_init(codec->codec_id);
1636
 
 
1637
 
            switch (codec->codec_type) {
 
1755
                enc_ctx->time_base = ist->st->time_base;
 
1756
 
 
1757
            ost->parser = av_parser_init(enc_ctx->codec_id);
 
1758
 
 
1759
            switch (enc_ctx->codec_type) {
1638
1760
            case AVMEDIA_TYPE_AUDIO:
1639
1761
                if (audio_volume != 256) {
1640
1762
                    av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
1641
1763
                    exit_program(1);
1642
1764
                }
1643
 
                codec->channel_layout     = icodec->channel_layout;
1644
 
                codec->sample_rate        = icodec->sample_rate;
1645
 
                codec->channels           = icodec->channels;
1646
 
                codec->frame_size         = icodec->frame_size;
1647
 
                codec->audio_service_type = icodec->audio_service_type;
1648
 
                codec->block_align        = icodec->block_align;
 
1765
                enc_ctx->channel_layout     = dec_ctx->channel_layout;
 
1766
                enc_ctx->sample_rate        = dec_ctx->sample_rate;
 
1767
                enc_ctx->channels           = dec_ctx->channels;
 
1768
                enc_ctx->frame_size         = dec_ctx->frame_size;
 
1769
                enc_ctx->audio_service_type = dec_ctx->audio_service_type;
 
1770
                enc_ctx->block_align        = dec_ctx->block_align;
1649
1771
                break;
1650
1772
            case AVMEDIA_TYPE_VIDEO:
1651
 
                codec->pix_fmt            = icodec->pix_fmt;
1652
 
                codec->width              = icodec->width;
1653
 
                codec->height             = icodec->height;
1654
 
                codec->has_b_frames       = icodec->has_b_frames;
 
1773
                enc_ctx->pix_fmt            = dec_ctx->pix_fmt;
 
1774
                enc_ctx->width              = dec_ctx->width;
 
1775
                enc_ctx->height             = dec_ctx->height;
 
1776
                enc_ctx->has_b_frames       = dec_ctx->has_b_frames;
1655
1777
                if (ost->frame_aspect_ratio)
1656
 
                    sar = av_d2q(ost->frame_aspect_ratio * codec->height / codec->width, 255);
 
1778
                    sar = av_d2q(ost->frame_aspect_ratio * enc_ctx->height / enc_ctx->width, 255);
1657
1779
                else if (ist->st->sample_aspect_ratio.num)
1658
1780
                    sar = ist->st->sample_aspect_ratio;
1659
1781
                else
1660
 
                    sar = icodec->sample_aspect_ratio;
1661
 
                ost->st->sample_aspect_ratio = codec->sample_aspect_ratio = sar;
 
1782
                    sar = dec_ctx->sample_aspect_ratio;
 
1783
                ost->st->sample_aspect_ratio = enc_ctx->sample_aspect_ratio = sar;
1662
1784
                break;
1663
1785
            case AVMEDIA_TYPE_SUBTITLE:
1664
 
                codec->width  = icodec->width;
1665
 
                codec->height = icodec->height;
 
1786
                enc_ctx->width  = dec_ctx->width;
 
1787
                enc_ctx->height = dec_ctx->height;
1666
1788
                break;
1667
1789
            case AVMEDIA_TYPE_DATA:
1668
1790
            case AVMEDIA_TYPE_ATTACHMENT:
1686
1808
                ist->decoding_needed = 1;
1687
1809
            ost->encoding_needed = 1;
1688
1810
 
 
1811
            set_encoder_id(output_files[ost->file_index], ost);
 
1812
 
1689
1813
            /*
1690
1814
             * We want CFR output if and only if one of those is true:
1691
1815
             * 1) user specified output framerate with -r
1695
1819
             *
1696
1820
             * in such a case, set ost->frame_rate
1697
1821
             */
1698
 
            if (codec->codec_type == AVMEDIA_TYPE_VIDEO &&
 
1822
            if (enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO &&
1699
1823
                !ost->frame_rate.num && ist &&
1700
1824
                (video_sync_method ==  VSYNC_CFR ||
1701
1825
                 (video_sync_method ==  VSYNC_AUTO &&
1721
1845
            }
1722
1846
 
1723
1847
            if (!ost->filter &&
1724
 
                (codec->codec_type == AVMEDIA_TYPE_VIDEO ||
1725
 
                 codec->codec_type == AVMEDIA_TYPE_AUDIO)) {
 
1848
                (enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO ||
 
1849
                 enc_ctx->codec_type == AVMEDIA_TYPE_AUDIO)) {
1726
1850
                    FilterGraph *fg;
1727
1851
                    fg = init_simple_filtergraph(ist, ost);
1728
1852
                    if (configure_filtergraph(fg)) {
1731
1855
                    }
1732
1856
            }
1733
1857
 
1734
 
            switch (codec->codec_type) {
 
1858
            switch (enc_ctx->codec_type) {
1735
1859
            case AVMEDIA_TYPE_AUDIO:
1736
 
                codec->sample_fmt     = ost->filter->filter->inputs[0]->format;
1737
 
                codec->sample_rate    = ost->filter->filter->inputs[0]->sample_rate;
1738
 
                codec->channel_layout = ost->filter->filter->inputs[0]->channel_layout;
1739
 
                codec->channels       = av_get_channel_layout_nb_channels(codec->channel_layout);
1740
 
                codec->time_base      = (AVRational){ 1, codec->sample_rate };
 
1860
                enc_ctx->sample_fmt     = ost->filter->filter->inputs[0]->format;
 
1861
                enc_ctx->sample_rate    = ost->filter->filter->inputs[0]->sample_rate;
 
1862
                enc_ctx->channel_layout = ost->filter->filter->inputs[0]->channel_layout;
 
1863
                enc_ctx->channels       = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);
 
1864
                enc_ctx->time_base      = (AVRational){ 1, enc_ctx->sample_rate };
1741
1865
                break;
1742
1866
            case AVMEDIA_TYPE_VIDEO:
1743
 
                codec->time_base = ost->filter->filter->inputs[0]->time_base;
 
1867
                enc_ctx->time_base = ost->filter->filter->inputs[0]->time_base;
1744
1868
 
1745
 
                codec->width  = ost->filter->filter->inputs[0]->w;
1746
 
                codec->height = ost->filter->filter->inputs[0]->h;
1747
 
                codec->sample_aspect_ratio = ost->st->sample_aspect_ratio =
 
1869
                enc_ctx->width  = ost->filter->filter->inputs[0]->w;
 
1870
                enc_ctx->height = ost->filter->filter->inputs[0]->h;
 
1871
                enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio =
1748
1872
                    ost->frame_aspect_ratio ? // overridden by the -aspect cli option
1749
 
                    av_d2q(ost->frame_aspect_ratio * codec->height/codec->width, 255) :
 
1873
                    av_d2q(ost->frame_aspect_ratio * enc_ctx->height/enc_ctx->width, 255) :
1750
1874
                    ost->filter->filter->inputs[0]->sample_aspect_ratio;
1751
 
                codec->pix_fmt = ost->filter->filter->inputs[0]->format;
1752
 
 
1753
 
                if (icodec &&
1754
 
                    (codec->width   != icodec->width  ||
1755
 
                     codec->height  != icodec->height ||
1756
 
                     codec->pix_fmt != icodec->pix_fmt)) {
1757
 
                    codec->bits_per_raw_sample = 0;
 
1875
                enc_ctx->pix_fmt = ost->filter->filter->inputs[0]->format;
 
1876
 
 
1877
                ost->st->avg_frame_rate = ost->frame_rate;
 
1878
 
 
1879
                if (dec_ctx &&
 
1880
                    (enc_ctx->width   != dec_ctx->width  ||
 
1881
                     enc_ctx->height  != dec_ctx->height ||
 
1882
                     enc_ctx->pix_fmt != dec_ctx->pix_fmt)) {
 
1883
                    enc_ctx->bits_per_raw_sample = 0;
1758
1884
                }
1759
1885
 
1760
1886
                if (ost->forced_keyframes)
1761
1887
                    parse_forced_key_frames(ost->forced_keyframes, ost,
1762
 
                                            ost->st->codec);
 
1888
                                            ost->enc_ctx);
1763
1889
                break;
1764
1890
            case AVMEDIA_TYPE_SUBTITLE:
1765
 
                codec->time_base = (AVRational){1, 1000};
 
1891
                enc_ctx->time_base = (AVRational){1, 1000};
1766
1892
                break;
1767
1893
            default:
1768
1894
                abort();
1769
1895
                break;
1770
1896
            }
1771
1897
            /* two pass mode */
1772
 
            if ((codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
 
1898
            if ((enc_ctx->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
1773
1899
                char logfilename[1024];
1774
1900
                FILE *f;
1775
1901
 
1778
1904
                                               DEFAULT_PASS_LOGFILENAME_PREFIX,
1779
1905
                         i);
1780
1906
                if (!strcmp(ost->enc->name, "libx264")) {
1781
 
                    av_dict_set(&ost->opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
 
1907
                    av_dict_set(&ost->encoder_opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
1782
1908
                } else {
1783
 
                    if (codec->flags & CODEC_FLAG_PASS1) {
 
1909
                    if (enc_ctx->flags & CODEC_FLAG_PASS1) {
1784
1910
                        f = fopen(logfilename, "wb");
1785
1911
                        if (!f) {
1786
1912
                            av_log(NULL, AV_LOG_FATAL, "Cannot write log file '%s' for pass-1 encoding: %s\n",
1796
1922
                                   logfilename);
1797
1923
                            exit_program(1);
1798
1924
                        }
1799
 
                        codec->stats_in = logbuffer;
 
1925
                        enc_ctx->stats_in = logbuffer;
1800
1926
                    }
1801
1927
                }
1802
1928
            }
1811
1937
            AVCodecContext *dec = NULL;
1812
1938
 
1813
1939
            if ((ist = get_input_stream(ost)))
1814
 
                dec = ist->st->codec;
 
1940
                dec = ist->dec_ctx;
1815
1941
            if (dec && dec->subtitle_header) {
1816
 
                ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size);
1817
 
                if (!ost->st->codec->subtitle_header) {
 
1942
                ost->enc_ctx->subtitle_header = av_malloc(dec->subtitle_header_size);
 
1943
                if (!ost->enc_ctx->subtitle_header) {
1818
1944
                    ret = AVERROR(ENOMEM);
1819
1945
                    goto dump_format;
1820
1946
                }
1821
 
                memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
1822
 
                ost->st->codec->subtitle_header_size = dec->subtitle_header_size;
 
1947
                memcpy(ost->enc_ctx->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
 
1948
                ost->enc_ctx->subtitle_header_size = dec->subtitle_header_size;
1823
1949
            }
1824
 
            if (!av_dict_get(ost->opts, "threads", NULL, 0))
1825
 
                av_dict_set(&ost->opts, "threads", "auto", 0);
1826
 
            if ((ret = avcodec_open2(ost->st->codec, codec, &ost->opts)) < 0) {
 
1950
            if (!av_dict_get(ost->encoder_opts, "threads", NULL, 0))
 
1951
                av_dict_set(&ost->encoder_opts, "threads", "auto", 0);
 
1952
            av_dict_set(&ost->encoder_opts, "side_data_only_packets", "1", 0);
 
1953
 
 
1954
            if ((ret = avcodec_open2(ost->enc_ctx, codec, &ost->encoder_opts)) < 0) {
1827
1955
                if (ret == AVERROR_EXPERIMENTAL)
1828
1956
                    abort_codec_experimental(codec, 1);
1829
1957
                snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
1830
1958
                        ost->file_index, ost->index);
1831
1959
                goto dump_format;
1832
1960
            }
1833
 
            assert_avoptions(ost->opts);
1834
 
            if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
 
1961
            assert_avoptions(ost->encoder_opts);
 
1962
            if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000)
1835
1963
                av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
1836
1964
                                             "It takes bits/s as argument, not kbits/s\n");
1837
 
            extra_size += ost->st->codec->extradata_size;
1838
1965
        } else {
1839
 
            av_opt_set_dict(ost->st->codec, &ost->opts);
1840
 
        }
 
1966
            av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
 
1967
        }
 
1968
 
 
1969
        ret = avcodec_copy_context(ost->st->codec, ost->enc_ctx);
 
1970
        if (ret < 0) {
 
1971
            av_log(NULL, AV_LOG_FATAL,
 
1972
                   "Error initializing the output stream codec context.\n");
 
1973
            exit_program(1);
 
1974
        }
 
1975
 
 
1976
        ost->st->time_base = ost->enc_ctx->time_base;
1841
1977
    }
1842
1978
 
1843
1979
    /* init input streams */
1937
2073
                   ost->sync_ist->st->index);
1938
2074
        if (ost->stream_copy)
1939
2075
            av_log(NULL, AV_LOG_INFO, " (copy)");
1940
 
        else
1941
 
            av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index]->dec ?
1942
 
                   input_streams[ost->source_index]->dec->name : "?",
1943
 
                   ost->enc ? ost->enc->name : "?");
 
2076
        else {
 
2077
            const AVCodec *in_codec    = input_streams[ost->source_index]->dec;
 
2078
            const AVCodec *out_codec   = ost->enc;
 
2079
            const char *decoder_name   = "?";
 
2080
            const char *in_codec_name  = "?";
 
2081
            const char *encoder_name   = "?";
 
2082
            const char *out_codec_name = "?";
 
2083
 
 
2084
            if (in_codec) {
 
2085
                decoder_name  = in_codec->name;
 
2086
                in_codec_name = avcodec_descriptor_get(in_codec->id)->name;
 
2087
                if (!strcmp(decoder_name, in_codec_name))
 
2088
                    decoder_name = "native";
 
2089
            }
 
2090
 
 
2091
            if (out_codec) {
 
2092
                encoder_name   = out_codec->name;
 
2093
                out_codec_name = avcodec_descriptor_get(out_codec->id)->name;
 
2094
                if (!strcmp(encoder_name, out_codec_name))
 
2095
                    encoder_name = "native";
 
2096
            }
 
2097
 
 
2098
            av_log(NULL, AV_LOG_INFO, " (%s (%s) -> %s (%s))",
 
2099
                   in_codec_name, decoder_name,
 
2100
                   out_codec_name, encoder_name);
 
2101
        }
1944
2102
        av_log(NULL, AV_LOG_INFO, "\n");
1945
2103
    }
1946
2104
 
2202
2360
        for (i = 0; i < ifile->nb_streams; i++) {
2203
2361
            ist = input_streams[ifile->ist_index + i];
2204
2362
            if (ist->decoding_needed)
2205
 
                output_packet(ist, NULL);
 
2363
                process_input_packet(ist, NULL);
2206
2364
 
2207
2365
            /* mark all outputs that don't go through lavfi as finished */
2208
2366
            for (j = 0; j < nb_output_streams; j++) {
2229
2387
        goto discard_packet;
2230
2388
 
2231
2389
    ist = input_streams[ifile->ist_index + pkt.stream_index];
 
2390
 
 
2391
    ist->data_size += pkt.size;
 
2392
    ist->nb_packets++;
 
2393
 
2232
2394
    if (ist->discard)
2233
2395
        goto discard_packet;
2234
2396
 
 
2397
    /* add the stream-global side data to the first packet */
 
2398
    if (ist->nb_packets == 1)
 
2399
        for (i = 0; i < ist->st->nb_side_data; i++) {
 
2400
            AVPacketSideData *src_sd = &ist->st->side_data[i];
 
2401
            uint8_t *dst_data;
 
2402
 
 
2403
            if (av_packet_get_side_data(&pkt, src_sd->type, NULL))
 
2404
                continue;
 
2405
 
 
2406
            dst_data = av_packet_new_side_data(&pkt, src_sd->type, src_sd->size);
 
2407
            if (!dst_data)
 
2408
                exit_program(1);
 
2409
 
 
2410
            memcpy(dst_data, src_sd->data, src_sd->size);
 
2411
        }
 
2412
 
2235
2413
    if (pkt.dts != AV_NOPTS_VALUE)
2236
2414
        pkt.dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
2237
2415
    if (pkt.pts != AV_NOPTS_VALUE)
2258
2436
        }
2259
2437
    }
2260
2438
 
2261
 
    ret = output_packet(ist, &pkt);
 
2439
    ret = process_input_packet(ist, &pkt);
2262
2440
    if (ret < 0) {
2263
2441
        av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d\n",
2264
2442
               ist->file_index, ist->st->index);
2331
2509
    for (i = 0; i < nb_input_streams; i++) {
2332
2510
        ist = input_streams[i];
2333
2511
        if (!input_files[ist->file_index]->eof_reached && ist->decoding_needed) {
2334
 
            output_packet(ist, NULL);
 
2512
            process_input_packet(ist, NULL);
2335
2513
        }
2336
2514
    }
2337
2515
    poll_filters();
2352
2530
    for (i = 0; i < nb_output_streams; i++) {
2353
2531
        ost = output_streams[i];
2354
2532
        if (ost->encoding_needed) {
2355
 
            av_freep(&ost->st->codec->stats_in);
2356
 
            avcodec_close(ost->st->codec);
 
2533
            av_freep(&ost->enc_ctx->stats_in);
2357
2534
        }
2358
2535
    }
2359
2536
 
2361
2538
    for (i = 0; i < nb_input_streams; i++) {
2362
2539
        ist = input_streams[i];
2363
2540
        if (ist->decoding_needed) {
2364
 
            avcodec_close(ist->st->codec);
 
2541
            avcodec_close(ist->dec_ctx);
2365
2542
            if (ist->hwaccel_uninit)
2366
 
                ist->hwaccel_uninit(ist->st->codec);
 
2543
                ist->hwaccel_uninit(ist->dec_ctx);
2367
2544
        }
2368
2545
    }
2369
2546
 
2379
2556
        for (i = 0; i < nb_output_streams; i++) {
2380
2557
            ost = output_streams[i];
2381
2558
            if (ost) {
2382
 
                if (ost->stream_copy)
2383
 
                    av_freep(&ost->st->codec->extradata);
2384
2559
                if (ost->logfile) {
2385
2560
                    fclose(ost->logfile);
2386
2561
                    ost->logfile = NULL;
2387
2562
                }
2388
 
                av_freep(&ost->st->codec->subtitle_header);
2389
2563
                av_free(ost->forced_kf_pts);
2390
 
                av_dict_free(&ost->opts);
 
2564
                av_dict_free(&ost->encoder_opts);
2391
2565
                av_dict_free(&ost->resample_opts);
2392
2566
            }
2393
2567
        }