failing to decode mp3

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

failing to decode mp3

Jarek M
Hi. I'm pretty new to ffmpeg, been trying to write a program that will decode mp3.
I checked out ffmpet, compiled it with msys on MS Windows XP and then wrote my own test program, in which I used
a slighlty modified example of the apiexample.c located in libavcodec directory
The only modifications I made was adding

  avcodec_init();
        avcodec_register_all();    

Without it,

codec = avcodec_find_decoder(CODEC_ID_MP3);

returns null.


I compiled the program with MS Visual Studio 2008, linked the avcodec, avformat, avutil,  that were compiled with msys.

So, when actually running the decoder with

len = avcodec_decode_audio(c, (short *)outbuf, &out_size,
                                       inbuf_ptr, size);

I get the following error messages printed:


[mp3 @ 00393110]Header missing skipping one byte
[mp3 @ 00393110]Header missing skipping one byte
[mp3 @ 00393110]Header missing skipping one byte
[mp3 @ 00393110]Header missing skipping one byte
[mp3 @ 00393110]Header missing skipping one byte
[mp3 @ 00393110]Header missing skipping one byte
[mp3 @ 00393110]Header missing skipping one byte
(and more of these) followed by

Error while decoding


The mp3 file is fine, I've tested with several mp3 files and all fail; all of them also work with winamp and MS Media Player 11.
Please let me know what's the problem; I had this running 2 weeks ago and it decoded mp3s fine; I've done an 'update' with the svn of the source,
recompiled (generated lib files) and the recompiled the whole program with Visual C++ 2008, now I cannot decode mp3s any longer.

What could be the problem?
Kind Regards
Jarek Myszewski

P.S.
I used the following code:

#include <stdio.h>
#include <stdlib.h>
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>

#include <libavutil/avutil.h>
}

void
audio_decode(const char *outfilename, const char *filename);


//----------------
#define INBUF_SIZE 4096

void
audio_decode(const char *outfilename, const char *filename)
{

        AVCodec *codec;
    AVCodecContext *c= NULL;
    int out_size, size, len;
    FILE *f, *outfile;
    uint8_t *outbuf;
    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;

    printf("Audio decoding\n");
  avcodec_init();
        avcodec_register_all();    
    /* find the mpeg audio decoder */
    codec = avcodec_find_decoder(CODEC_ID_MP3);
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        exit(1);
    }

    c= avcodec_alloc_context();

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

    //outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
    outbuf = reinterpret_cast<unsigned char*>(malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE));
    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 */
    inbuf_ptr = inbuf;
    for(;;) {
        size = fread(inbuf, 1, INBUF_SIZE, f);
        if (size == 0)
            break;

        inbuf_ptr = inbuf;
        while (size > 0) {
            len = avcodec_decode_audio(c, (short *)outbuf, &out_size,
                                       inbuf_ptr, size);
            if (len < 0) {
                fprintf(stderr, "Error while decoding\n");
                exit(1);
            }
            if (out_size > 0) {
                /* if a frame has been decoded, output it */
                fwrite(outbuf, 1, out_size, outfile);
            }
            size -= len;
            inbuf_ptr += len;
        }
    }

    fclose(outfile);
    fclose(f);
    free(outbuf);

    avcodec_close(c);
    av_free(c);

}

int main(int argc, char *argv[])
{
 audio_decode("C:\\Devel\\msys\\1.0\\home\\jmkoder\\Audio\\ifiwas.pcm","C:\\Devel\\msys\\1.0\\home\\jmkoder\\Audio\\ifiwas.mp3");
       
        return 0;
}

_______________________________________________
libav-user mailing list
[hidden email]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user