Revision ff4905a5

View differences:

ffmpeg.c
276 276
    int64_t       next_pts;  /* synthetic pts for cases where pkt.pts
277 277
                                is not defined */
278 278
    int64_t       pts;       /* current pts */
279
    int is_start;            /* is 1 at the start and after a discontinuity */
279 280
} AVInputStream;
280 281

  
281 282
typedef struct AVInputFile {
......
421 422
    const int audio_out_size= 4*MAX_AUDIO_PACKET_SIZE;
422 423

  
423 424
    int size_out, frame_bytes, ret;
424
    AVCodecContext *enc;
425
    AVCodecContext *enc= &ost->st->codec;
425 426

  
426 427
    /* SC: dynamic allocation of buffers */
427 428
    if (!audio_buf)
......
431 432
    if (!audio_buf || !audio_out)
432 433
        return;               /* Should signal an error ! */
433 434

  
434
    
435
    enc = &ost->st->codec;
436
    
437 435
    if(audio_sync_method){
438 436
        double delta = ost->sync_ipts * enc->sample_rate - ost->sync_opts 
439 437
                - fifo_size(&ost->fifo, ost->fifo.rptr)/(ost->st->codec.channels * 2);
438
        double idelta= delta*ist->st->codec.sample_rate / enc->sample_rate;
439
        int byte_delta= ((int)idelta)*2*ist->st->codec.channels;
440

  
440 441
        //FIXME resample delay
441 442
        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, comp, enc->sample_rate);
448
        }
443
            if(ist->is_start){
444
                if(byte_delta < 0){
445
                    byte_delta= FFMIN(byte_delta, size);
446
                    size += byte_delta;
447
                    buf  -= byte_delta;
448
                    if(verbose > 2)
449
                        fprintf(stderr, "discarding %d audio samples\n", (int)-delta);
450
                    if(!size)
451
                        return;
452
                    ist->is_start=0;
453
                }else{
454
                    static uint8_t *input_tmp= NULL;
455
                    input_tmp= av_realloc(input_tmp, byte_delta + size);
456

  
457
                    if(byte_delta + size <= MAX_AUDIO_PACKET_SIZE)
458
                        ist->is_start=0;
459
                    else
460
                        byte_delta= MAX_AUDIO_PACKET_SIZE - size;
461

  
462
                    memset(input_tmp, 0, byte_delta);
463
                    memcpy(input_tmp + byte_delta, buf, size);
464
                    buf= input_tmp;
465
                    size += byte_delta;
466
                    if(verbose > 2)
467
                        fprintf(stderr, "adding %d audio samples of silence\n", (int)delta);
468
                }
469
            }else if(audio_sync_method>1){
470
                int comp= clip(delta, -audio_sync_method, audio_sync_method);
471
                assert(ost->audio_resample);
472
                if(verbose > 2)
473
                    fprintf(stderr, "compensating audio timestamp drift:%f compensation:%d in:%d\n", delta, comp, enc->sample_rate);
474
                fprintf(stderr, "drift:%f len:%d opts:%lld ipts:%lld fifo:%d\n", delta, -1, ost->sync_opts, (int64_t)(ost->sync_ipts * enc->sample_rate), fifo_size(&ost->fifo, ost->fifo.rptr)/(ost->st->codec.channels * 2));
475
                av_resample_compensate(*(struct AVResampleContext**)ost->resample, comp, enc->sample_rate);
476
            }
477
        } 
449 478
    }else
450 479
        ost->sync_opts= lrintf(ost->sync_ipts * enc->sample_rate)
451 480
                        - fifo_size(&ost->fifo, ost->fifo.rptr)/(ost->st->codec.channels * 2); //FIXME wrong
......
1040 1069
    AVFrame picture;
1041 1070
    short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1042 1071
    void *buffer_to_free;
1043
//fprintf(stderr, "output_packet %d, dts:%lld\n", pkt->stream_index, pkt->dts);
1072

  
1044 1073
    if (pkt && pkt->dts != AV_NOPTS_VALUE) { //FIXME seems redundant, as libavformat does this too
1045 1074
        ist->next_pts = ist->pts = pkt->dts;
1046 1075
    } else {
......
1487 1516
                        ost->audio_resample = 1; 
1488 1517
                    }
1489 1518
                }
1490
                if(audio_sync_method)
1519
                if(audio_sync_method>1)
1491 1520
                    ost->audio_resample = 1;
1492 1521

  
1493 1522
                if(ost->audio_resample){
......
1676 1705
	is = input_files[ist->file_index];
1677 1706
        ist->pts = 0;
1678 1707
        ist->next_pts = 0;
1708
        ist->is_start = 1;
1679 1709
    }
1680 1710
    
1681 1711
    /* compute buffer size max (should use a complete heuristic) */
......
1788 1818
                for(i=0; i<file_table[file_index].nb_streams; i++){
1789 1819
                    int index= file_table[file_index].ist_index + i;
1790 1820
                    ist_table[index]->next_pts += delta;
1821
                    ist_table[index]->is_start=1;
1791 1822
                }
1792 1823
            }
1793 1824
        }

Also available in: Unified diff