Revision 986ebcdb ffmpeg.c

View differences:

ffmpeg.c
201 201
static char *pass_logfilename = NULL;
202 202
static int audio_stream_copy = 0;
203 203
static int video_stream_copy = 0;
204
static int sync_method= 1;
204
static int video_sync_method= 1;
205
static int audio_sync_method= 0;
205 206
static int copy_ts= 0;
206 207

  
207 208
static int rate_emu = 0;
......
432 433

  
433 434
    
434 435
    enc = &ost->st->codec;
436
    
437
    if(audio_sync_method){
438
        double delta = ost->sync_ipts * enc->sample_rate - ost->sync_opts 
439
                - fifo_size(&ost->fifo, ost->fifo.rptr)/(ost->st->codec.channels * 2);
440
        //FIXME resample delay
441
        if(fabs(delta) > 50){
442
            int comp= clip(delta, -audio_sync_method, audio_sync_method);
443
            assert(ost->audio_resample);
444
            if(verbose > 2)
445
                fprintf(stderr, "compensating audio timestamp drift:%f compensation:%d in:%d\n", delta, comp, enc->sample_rate);
446
//            fprintf(stderr, "drift:%f len:%d opts:%lld ipts:%lld fifo:%d\n", delta, len/4, ost->sync_opts, (int64_t)(ost->sync_ipts * enc->sample_rate), fifo_size(&ost->fifo, ost->fifo.rptr)/(ost->st->codec.channels * 2));
447
            av_resample_compensate(*(struct AVResampleContext**)ost->resample, delta, enc->sample_rate);
448
        }
449
    }else
450
        ost->sync_opts= lrintf(ost->sync_ipts * enc->sample_rate)
451
                        - fifo_size(&ost->fifo, ost->fifo.rptr)/(ost->st->codec.channels * 2); //FIXME wrong
435 452

  
436 453
    if (ost->audio_resample) {
437 454
        buftmp = audio_buf;
......
467 484
                pkt.pts= enc->coded_frame->pts;
468 485
            pkt.flags |= PKT_FLAG_KEY;
469 486
            av_interleaved_write_frame(s, &pkt);
487
            
488
            ost->sync_opts += enc->frame_size;
470 489
        }
471 490
    } else {
472 491
        AVPacket pkt;
473 492
        av_init_packet(&pkt);
493

  
494
        ost->sync_opts += size_out / enc->channels;
495

  
474 496
        /* output a pcm frame */
475 497
        /* XXX: change encoding codec API to avoid this ? */
476 498
        switch(enc->codec->id) {
......
588 610
                         AVOutputStream *ost, 
589 611
                         AVInputStream *ist,
590 612
                         AVFrame *in_picture,
591
                         int *frame_size, AVOutputStream *audio_sync)
613
                         int *frame_size)
592 614
{
593 615
    int nb_frames, i, ret;
594 616
    AVFrame *final_picture, *formatted_picture;
......
610 632

  
611 633
    *frame_size = 0;
612 634

  
613
    if(sync_method){
635
    if(video_sync_method){
614 636
        double vdelta;
615 637
        vdelta = ost->sync_ipts * enc->frame_rate / enc->frame_rate_base - ost->sync_opts;
616 638
        //FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
......
1018 1040
    AVFrame picture;
1019 1041
    short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1020 1042
    void *buffer_to_free;
1021
    
1043
//fprintf(stderr, "output_packet %d, dts:%lld\n", pkt->stream_index, pkt->dts);
1022 1044
    if (pkt && pkt->dts != AV_NOPTS_VALUE) { //FIXME seems redundant, as libavformat does this too
1023 1045
        ist->next_pts = ist->pts = pkt->dts;
1024 1046
    } else {
1025
        ist->pts = ist->next_pts;
1047
        assert(ist->pts == ist->next_pts);
1026 1048
    }
1027 1049

  
1028 1050
    if (pkt == NULL) {
......
1134 1156
                        printf("%d: got pts=%0.3f %0.3f\n", i, 
1135 1157
                               (double)pkt->pts / AV_TIME_BASE, 
1136 1158
                               ((double)ist->pts / AV_TIME_BASE) - 
1137
                               ((double)ost->st->pts.val * ost->time_base.num / ost->time_base.den));
1159
                               ((double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den));
1138 1160
#endif
1139 1161
                        /* set the input output pts pairs */
1140 1162
                        ost->sync_ipts = (double)(ist->pts + input_files_ts_offset[ist->file_index])/ AV_TIME_BASE;
......
1159 1181
                                        }
1160 1182
                                    }
1161 1183

  
1162
                                    do_video_out(os, ost, ist, &picture, &frame_size, audio_sync);
1184
                                    do_video_out(os, ost, ist, &picture, &frame_size);
1163 1185
                                    video_size += frame_size;
1164 1186
                                    if (do_vstats && frame_size)
1165 1187
                                        do_video_stats(os, ost, frame_size);
......
1460 1482
                            ost->audio_resample = 0;
1461 1483
                        else {
1462 1484
                            ost->audio_resample = 1;
1463
                            ost->resample = audio_resample_init(codec->channels, icodec->channels,
1464
                                                        codec->sample_rate, 
1465
                                                        icodec->sample_rate);
1466
			    if(!ost->resample)
1467
			      {
1468
				printf("Can't resample.  Aborting.\n");
1469
				av_abort();
1470
			      }
1471 1485
                        }
1472
                        /* Request specific number of channels */
1473
                        icodec->channels = codec->channels;
1474 1486
                    } else {
1475 1487
                        ost->audio_resample = 1; 
1476
                        ost->resample = audio_resample_init(codec->channels, icodec->channels,
1477
                                                        codec->sample_rate, 
1478
                                                        icodec->sample_rate);
1479
			if(!ost->resample)
1480
			  {
1481
			    printf("Can't resample.  Aborting.\n");
1482
			    av_abort();
1483
			  }
1488
                    }
1489
                }
1490
                if(audio_sync_method)
1491
                    ost->audio_resample = 1;
1492

  
1493
                if(ost->audio_resample){
1494
                    ost->resample = audio_resample_init(codec->channels, icodec->channels,
1495
                                                    codec->sample_rate, icodec->sample_rate);
1496
                    if(!ost->resample){
1497
                        printf("Can't resample.  Aborting.\n");
1498
                        av_abort();
1484 1499
                    }
1485 1500
                }
1486 1501
                ist->decoding_needed = 1;
......
2010 2025
    av_log_set_level(atoi(arg));
2011 2026
}
2012 2027

  
2013
static void opt_sync_method(const char *arg)
2014
{
2015
    sync_method = atoi(arg);
2016
}
2017

  
2018 2028
static void opt_frame_rate(const char *arg)
2019 2029
{
2020 2030
    if (parse_frame_rate(&frame_rate, &frame_rate_base, arg) < 0) {
......
3626 3636
    { "v", HAS_ARG, {(void*)opt_verbose}, "control amount of logging", "verbose" },
3627 3637
    { "target", HAS_ARG, {(void*)opt_target}, "specify target file type (\"vcd\", \"svcd\" or \"dvd\")", "type" },
3628 3638
    { "threads", HAS_ARG | OPT_EXPERT, {(void*)opt_thread_count}, "thread count", "count" },
3629
    { "sync", HAS_ARG | OPT_EXPERT, {(void*)opt_sync_method}, "sync method", "" },
3639
    { "vsync", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&video_sync_method}, "video sync method", "" },
3640
    { "async", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&audio_sync_method}, "audio sync method", "" },
3630 3641
    { "copyts", OPT_BOOL | OPT_EXPERT, {(void*)&copy_ts}, "copy timestamps" },
3631 3642

  
3632 3643
    /* video options */

Also available in: Unified diff