[Libav-user] mp3 decoding with ffmpeg

Daniel Glaß danielglass4 at aol.de
Mon Jun 17 09:19:34 CEST 2013



 I want to decode an mp3-File with the ffmpeg library.


    #include <math.h>
    #include <libavutil/opt.h>
    #include <libavcodec/avcodec.h>
    #include <libavutil/channel_layout.h>
    #include <libavutil/common.h>
    #include <libavutil/imgutils.h>
    #include <libavutil/mathematics.h>
    #include <libavutil/samplefmt.h>

    #define INBUF_SIZE 4096
    #define AUDIO_INBUF_SIZE 20480
    #define AUDIO_REFILL_THRESH 4096

    static void audio_decode_example(const char *outfilename, const char *filename)
    {
        AVCodec *codec;
        AVCodecContext *c= NULL;
        int len;
        FILE *f, *outfile;
        uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
        AVPacket avpkt;
        AVFrame *decoded_frame = NULL;

        av_init_packet(&avpkt);

        printf("Decode audio file %s to %s\n", filename, outfilename);

        /* find the mpeg audio decoder */
        codec = avcodec_find_decoder(AV_CODEC_ID_MP3);
        if (!codec) {
            fprintf(stderr, "Codec not found\n");
            exit(1);
        }

        c = avcodec_alloc_context3(codec);
        if (!c) {
            fprintf(stderr, "Could not allocate audio codec context\n");
            exit(1);
        }

        /* open it */
        if (avcodec_open2(c, codec, NULL) < 0) {
            fprintf(stderr, "Could not open codec\n");
            exit(1);
        }

        f = fopen(filename, "rb");
        if (!f) {
            fprintf(stderr, "Could not open %s\n", filename);
            exit(1);
        }
        outfile = fopen(outfilename, "wb");
        if (!outfile) {
            av_free(c);
            exit(1);
        }

        /* decode until eof */
        avpkt.data = inbuf;
        avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);

        while (avpkt.size > 0) {
            int got_frame = 0;

            if (!decoded_frame) {
                if (!(decoded_frame = avcodec_alloc_frame())) {
                    fprintf(stderr, "Could not allocate audio frame\n");
                    exit(1);
                }
            } else
                avcodec_get_frame_defaults(decoded_frame);

            len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
            if (len < 0) {
                fprintf(stderr, "Error while decoding\n");
                exit(1);
            }
            if (got_frame) {
                /* if a frame has been decoded, output it */
                int data_size = av_samples_get_buffer_size(NULL, c->channels,
                                                           decoded_frame->nb_samples,
                                                           c->sample_fmt, 1);
                fwrite(decoded_frame->data[0], 1, data_size, outfile);
            }
            avpkt.size -= len;
            avpkt.data += len;
            avpkt.dts =
            avpkt.pts = AV_NOPTS_VALUE;
            if (avpkt.size < AUDIO_REFILL_THRESH) {
                memmove(inbuf, avpkt.data, avpkt.size);
                avpkt.data = inbuf;
                len = fread(avpkt.data + avpkt.size, 1,
                            AUDIO_INBUF_SIZE - avpkt.size, f);
                if (len > 0)
                    avpkt.size += len;
            }
        }

        fclose(outfile);
        fclose(f);

        avcodec_close(c);
        av_free(c);
        avcodec_free_frame(&decoded_frame);
    }


    int main(int argc, char **argv)
    {
        const char* filename;

        /* register all the codecs */
        avcodec_register_all();

        filename = argv[1];

        audio_decode_example("ausgabe.sw", filename);

        return 0;
    }

And here is my problem: When i start my program, the decoding from the mp3-File into this raw data file succeed with no problem. But when I play this output file with my Alsa driver I have an distorted sound, its the original sound, but i sounds like the smurfs and its distorted.Have anyone here an idea? 
The Code is from the original encoding_decoding.c example from ffmpeg. The only thing I changed is the AV_CODEC_ID, because I need .mp3.Sorry for my partially bad englisch.

Thanks for every help.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20130617/14c3e049/attachment.html>


More information about the Libav-user mailing list