[FFmpeg-devel] doc/examples/muxing.c

JULIAN GARDNER joolzg at btinternet.com
Wed Nov 5 13:12:32 CET 2014


>________________________________
> From: Stefano Sabatini <stefasab at gmail.com>
>To: FFmpeg development discussions and patches <ffmpeg-devel at ffmpeg.org> 
>Sent: Wednesday, 5 November 2014, 12:23
>Subject: Re: [FFmpeg-devel] doc/examples/muxing.c
> 
>
>On date Tuesday 2014-11-04 17:39:22 +0000, JULIAN GARDNER encoded:
>> I have been working on a program which needs to do audio resampling, and used muxing.c as a base but now i am coming up against a problem.
>> 
>
>> Changing muxing.c so that the sound produced by the routine
>> get_audio_frame is not the same audio rate as the output causes an
>> assert in the write_audio_frame routine, normally the program
>> prodces the same audio rate that the output format is going to use,
>> so the resampler really does nothing.
>
>No the resampler is used to change sample format, in case the output
>format doesn't support the input format.


Yes but the sample_count stays the same as the input rate==output rate, so no errors crop up


>
>> 
>> Modifying the audio tmp_frame definition and the associated tables for the resampler was easy but i have 1 thing which im struggling to understand
>> 
>> line 310 muxing.c
>> 
>> if (frame) { 
>
>>         /* convert samples from native format to destination codec format, using the resampler */ 
>>         /* compute destination number of samples */ 
>
>>         dst_nb_samples = av_rescale_rnd(swr_get_delay(ost->swr_ctx, c->sample_rate) + frame->nb_samples, 
>>                                                                 c->sample_rate, c->sample_rate, AV_ROUND_UP); 
>>         av_assert0(dst_nb_samples == frame->nb_samples); 
>
>This is suspect (the reason of this change is lost in the merges, so I
>can't help).


Anyone

>> 
>> 
>> line 335 muxing.c
>>         frame->pts = av_rescale_q(ost->samples_count, (AVRational){1, c->sample_rate}, c->time_base);
>> 
>> 
>
>> As the current code stands the input and output sample_rate is the
>> same, so this code really does nothing, but now i have it so that
>> the output is 44100 and the input is 48000, this code needs
>> changing, my initial thought was this will use the output sample
>> rate, but i still get the assert
>> 
>
>> Can someone who knows more about this fix the demo so that the input
>> sample rate != output sample rate
>
>How are you using the code? How can you force the code to change
>output sample rate? If you have a patch please share.


thats the problem i have patch 4 lines of code which forces the self produced sound to be different from the output stream

example
#define SAMPLE_RATE_OTHER( X)        (X==44100 ? 48000:44100)


routine open_audio

find the line
    ost->t      = 0;
change next 2 lines
    ost->tincr = 2 * M_PI * 110.0 / SAMPLE_RATE_OTHER(c->sample_rate);
    ost->tincr2 = 2 * M_PI * 110.0 / SAMPLE_RATE_OTHER(c->sample_rate) / SAMPLE_RATE_OTHER(c->sample_rate);

then find the line
    ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c-:channel_layout,
                                               c->sample_rate, nb_samples); 

change to

    ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c-:channel_layout,
                                               SAMPLE_RATE_OTHER(c->sample_rate), nb_samples);


then 10 lines down
    av_opt_set_int          (ost->swr_ctx, "in_sample_rate",        c->sample_rate, 0);change to 

    av_opt_set_int          (ost->swr_ctx, "in_sample_rate",        SAMPLE_RATE_OTHER(c->sample_rate), 0);

         
This then states that the self produced audio frame will have a different sample_rate to that of the output format.


If you make these changes and nothing else you get the assert in write_audio_frame

As an example

  in        out     values from assert

22050  44100  2304 1152            ASSERT
32000  44100  1588 1152            ASSERT

44100  44100  1152 1152            OK

48000  44100  1168 1152            ASSERT


22050  48000  2508 1152            ASSERT
32000  48000  1728 1152            ASSERT

44100  48000  1254 1152            ASSERT

48000  48000  1152 1152            OK


joolz



More information about the ffmpeg-devel mailing list