[Libav-user] about mp3 convert to aac

dboyaoao yiikai1987910 at gmail.com
Wed Apr 24 07:33:00 CEST 2013


Hello,
      I had covert mp3 to aac , but the saved aac file is not correct ,
there are a lot of noise and the human sound is very quickly. somebody can
help me?
   here is my code :



#include <stdio.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/audio_fifo.h>
const char* infile = NULL;
AVFormatContext *ic = NULL;
int videoindex = -1;
int audioindex = -1;
AVPacket videopacket;
AVPacket audiopacket;
AVPacket encodeaudiopacket;
AVCodec *codec = NULL;
AVCodecContext *codecctx = NULL;
AVFrame pictureFrame;
AVFrame audioFrame;
AVFrame *pFrameRGB;
int             numBytes;
uint8_t         *buffer;

AVOutputFormat *ofmt = NULL;

AVCodec *encode = NULL;
AVCodecContext *encodecctx = NULL;
AVAudioFifo *fifo = NULL;



int main(int argc, char **argv)
{
   infile = argv[1];
   av_register_all();
   if(avformat_open_input(&ic,infile,NULL,NULL))
   {
       printf("open input file error\n");
       return 0;
   }
   
   if(avformat_find_stream_info(ic,NULL) < 0)
   {
       printf("no stream in the file\n");
       return 0;
   }
    int streamnum = ic->nb_streams;
    int i = 0;
    for(;i<streamnum;i++)
    {
        if(ic->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            videoindex = i;
            break;
        }
        if(ic->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
        {
            audioindex = i;
            break;
        }
    }

    if(audioindex != -1 && videoindex != -1)
    {
        printf("the input file is AV\n");
    }
    else if(audioindex != -1){
        printf("the input file is audio\n");
    }
    else if(videoindex != -1)
    {
        printf("the input file is video\n");
    }
    
    
    ofmt = av_guess_format(NULL,argv[2],NULL);
    if(!ofmt)
    {
        printf("no found exit format\n");
        return 0;
    }

    encode = avcodec_find_encoder(ofmt->audio_codec);
    if(!encode)
    {
        printf("can't find encode\n");
        return 0;
    }
    
    encodecctx = avcodec_alloc_context3(encode);
    if(!encodecctx)
    {
        printf("can't allocate encode context\n");
        return 0;
    }

    codec = avcodec_find_decoder(ic->streams[audioindex]->codec->codec_id);
    if(!codec)
    {
        printf("codec not found\n");
    }
   
    codecctx = ic->streams[audioindex]->codec;
    
    encodecctx->sample_fmt = AV_SAMPLE_FMT_S16;
    encodecctx->channels = codecctx->channels;
    encodecctx->channel_layout = codecctx->channel_layout;
    encodecctx->sample_rate = codecctx->sample_rate; 

    if(avcodec_open2(codecctx,codec,NULL) < 0)
    {
        printf("couldn't allocate video codeccontext\n");
        return 0;
    }

    if(avcodec_open2(encodecctx,encode,NULL) < 0)
    {
        printf("caouldn't open encode\n");
        return 0;
    }
    
    if(!(fifo =
av_audio_fifo_alloc(AV_SAMPLE_FMT_S16,codecctx->channels,codecctx->frame_size)))
    {
        printf("can't alloc fifo\n");
        return 0;
    }
    
    FILE *outfile = fopen(argv[2],"wb");
    if(!outfile)
    {
        printf("can't open out file\n");
        return 0;
    }

    int nRet;
    int j = 0;
    while(1)
    {
        nRet = av_read_frame(ic,&audiopacket);
        if(nRet < 0)
        {
            printf("no packet read\n");
            break;
        }
        if(audiopacket.stream_index == audioindex)
        {
            int gotptr = -1;
            int ret =
avcodec_decode_audio4(codecctx,&audioFrame,&gotptr,&audiopacket);

           
if(av_audio_fifo_realloc(fifo,av_audio_fifo_size(fifo)+codecctx->frame_size)
< 0)
            {
                printf("realloc fifo error\n");
                return 0;
            }

           
if(av_audio_fifo_write(fifo,audioFrame.data,codecctx->frame_size) !=
codecctx->frame_size)
            {
                printf("write fifo error\n");
                return 0;
            }

            printf("fifo size is %d\n",av_audio_fifo_size(fifo));
            while(av_audio_fifo_size(fifo) < encodecctx->frame_size)
            {
                int ret =
avcodec_decode_audio4(codecctx,&audioFrame,&gotptr,&audiopacket);

               
if(av_audio_fifo_realloc(fifo,av_audio_fifo_size(fifo)+codecctx->frame_size)
< 0)
                {
                    printf("realloc fifo error\n");
                    return 0;
                }

               
if(av_audio_fifo_write(fifo,audioFrame.data,codecctx->frame_size) !=
codecctx->frame_size)
                {
                    printf("write fifo error\n");
                    return 0;
                }
            }

            if(gotptr)
            {
                int gotencodeptr = -1;
                av_init_packet(&encodeaudiopacket);

                while(av_audio_fifo_size(fifo) >= encodecctx->frame_size)
                {
                    audioFrame.nb_samples = encodecctx->frame_size;
                   
if(av_audio_fifo_read(fifo,audioFrame.data,encodecctx->frame_size) !=
encodecctx->frame_size)
                    {
                        printf("read data from fifo error\n");
                        return 0;
                    }

                    printf("audioFrame size is %d\n",audioFrame.nb_samples);
                    printf("encodectx size is %d\n",encodecctx->frame_size);

                   
if(avcodec_encode_audio2(encodecctx,&encodeaudiopacket,&audioFrame,&gotencodeptr)
< 0 )
                    {
                        printf("encode audio error\n");
                        return 0;
                    }
                    else{
                        if(gotencodeptr == 1)
                        {
                            printf("encode sucess\n");
                           
fwrite(encodeaudiopacket.data,1,encodeaudiopacket.size,outfile);
                        }
                        else{
                            printf("no encode data\n");
                        }
                    }

                    av_free_packet(&encodeaudiopacket);
                }
            }
            else{
                printf("decode error\n");
            }
            if(ret < 0)
            {
                printf("decode error\n");
                return 0;
            }
        }
        else{
        }
    }
    
}

THANKS!



--
View this message in context: http://libav-users.943685.n4.nabble.com/about-mp3-convert-to-aac-tp4657360.html
Sent from the libav-users mailing list archive at Nabble.com.


More information about the Libav-user mailing list