[Libav-user] OPUS transcoding to AAC but 960 sample increase to 1024 with a nomalized blank data.

강구철 kckang at skycom.ne.kr
Mon Oct 29 06:52:44 EET 2018

I already tried using av_fifo for make it size to 1024. but still encoding
api output 960 plus blank 68 samples(total 1024).


//AVCodecContext initialization

AVCodecContext  *opus_context;

AVCodecContext  *aac_context;


        opus_context->channels = 1;

        opus_context->channel_layout = AV_CH_LAYOUT_MONO;

        opus_context->sample_rate    = 48000;//SRATE;

        opus_context->sample_fmt     = AV_SAMPLE_FMT_FLTP;//4BYTE

        opus_context->bit_rate       = 48000;//BITRATE;


        aac_context->channels = 1;

        aac_context->channel_layout = AV_CH_LAYOUT_MONO;

        aac_context->sample_rate    = 48000;//SRATE;

        aac_context->sample_fmt     = AV_SAMPLE_FMT_FLTP;

        //aac_context->sample_fmt   = AV_SAMPLE_FMT_S32;

        aac_context->bit_rate       = 48000;//BITRATE;//64000

                     //aac_context->strict_std_compliance =


        fifo = av_audio_fifo_alloc(aac_context->sample_fmt,
aac_context->channels, 1);


        opus_codec = avcodec_find_decoder( AV_CODEC_ID_OPUS );          

        aac_codec  = avcodec_find_encoder( AV_CODEC_ID_AAC  );


//transcoder function for trans OPUS => ACC


tcode(AVPacket* src, AVPacket* dst){


           decoded_frame->data[0] = (uint8_t*)av_malloc(4*1024);//TESTTEST

           ret = avcodec_decode_audio4(opus_context, decoded_frame,
&data_present, src); // decoded_frame 960 sucess





    //WAVE Generate for 68

    unsigned char _cbuf[100];

    unsigned char *cbuf=NULL;

    cbuf = _cbuf;





    decoded_frame->nb_samples = 1024;//change to 1024 for AAC encoding block


    for(int i=960; i<1024 ; i++){

               decoded_frame->data[0][i*4+0] = (uint8_t)cbuf[0]; //float

               decoded_frame->data[0][i*4+1] = (uint8_t)cbuf[1]; //float

               decoded_frame->data[0][i*4+2] = (uint8_t)cbuf[2]; //float

               decoded_frame->data[0][i*4+3] = (uint8_t)cbuf[3]; //float

               cbuf[2] += 2;        //make saw type wav using range is 0~96

               if(cbuf[2] >= 96)cbuf[2]=0;//mod reset to 0

    }//END OF Generate 68 sample of last WAVE(PCM Float 4byte LittleEndian


ret = avcodec_encode_audio2(aac_context, dst, decoded_frame,
&data_present);//AAC ENCODING Success 1024 sampes compressed but contain
last 68 samples blank.


this mail attached sample result aac before muxing and next figure show you
every 20msec data has 64 nomalized blank samples(marked red pen). thanks for
any recommand or inform.


<abc1.m4a AAC recorded webrtc sample open with audacity>


From: Libav-user [mailto:libav-user-bounces at ffmpeg.org] On Behalf Of He Lei
Sent: Friday, October 26, 2018 6:16 PM
To: This list is about using libavcodec, libavformat, libavutil, libavdevice
and libavfilter.
Subject: Re: [Libav-user] OPUS transcoding to AAC but 960 sample increase to
1024 with a nomalized blank data.


Using “audio_fifo” to cache samples, When the samples number in fifo is
enough to 1024, and then encode it. 

The last, If the number of samples  is less than 1024 in fifo, fill with



look at “doc/examples/transcode_aac.c”



helei0908 at hotmail.com



在 2018年10月26日,下午4:34,강구철 <kckang at skycom.ne.kr> 写道:


Im transcode voice comming from WebRTC through by RTP with h264 video.

received sound unit is 20msec OPUS stereo 48000 2channel sample per second

its good decoded to PCM32 FLTP type and good play. 

after decode I encode to AAC 48000 stereo frame nb_samples is 960. but
encoding ffmpeg aac function 

attach 64 samples every each decoded raw PCM samples. what should I do for
it to improving final aac product quality ?


now I found AAC Context be able to control cypher block size 1024 to 960.
some documents say aac encoder default block is 1024.


AACContext *ac = (AACContext*)aac_context->priv_data;

MPEG4AudioConfig *m4ac = &(ac->oc[0].m4ac);

m4ac->frame_length_short = 1;//1:960, 0:1024


is this right approching ? appriciate any kinds of oppinion of you guys!!



Libav-user mailing list
 <mailto:Libav-user at ffmpeg.org> Libav-user at ffmpeg.org


-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 240628 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20181029/66e70f7d/attachment.bin>

More information about the Libav-user mailing list