I pulled apart the api_example.c file provided with the distribution
and created the source below to just decode an mp3 file into raw
samples. It seems to do something but reports...
[mp3 @ 0x101809200] Header missing
Error while decoding
The mp3 I'm using seems to play fine in itunes / vlc etc. so I don't
think there's a problem with it.
It must be something to do with the way I'm using the library. Anyone
able to work it out ?
Cheers,
Emyr
note I use
g++ -o decode decode.c -lavcodec -lavdevice -lavformat -lavutil -lz
-lvorbis -lvorbisenc -lvorbisfile -lmp3lame -lbz2
to compile it.
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
#ifdef HAVE_AV_CONFIG_H
#undef HAVE_AV_CONFIG_H
#endif
extern"C" {
#include <libavcodec/avcodec.h>
#include <libavutil/mathematics.h>
#include <libavformat/avformat.h>
}
#define INBUF_SIZE 4096
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096
using namespace std;
/*
* Audio decoding.
*/
static void audio_decode_example(const char *outfilename, const char
*filename)
{
AVCodec *codec;
int out_size, len;
FILE *f, *outfile;
uint8_t *outbuf;
uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
AVPacket avpkt;
// initialise libavformat /libavcodec
avcodec_init();
avcodec_register_all();
av_register_all();
// data structures
AVFormatContext *pFormatCtx;
// open input file
av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL);
// get streams info
av_find_stream_info(pFormatCtx);
// dump the format
dump_format(pFormatCtx, 0, filename, false);
// get first audio stream
AVCodecContext *pCodecCtx;
int audioStream=-1;
for(int i=0; i<pFormatCtx->nb_streams; i++) {
pCodecCtx=pFormatCtx->streams[i]->codec;
if(pCodecCtx->codec_type == AVMEDIA_TYPE_AUDIO) {
audioStream=i;
break;
}
}
if(audioStream==-1) {
cout << "No audio stream found" << endl;
}
// try to find it's codec and open it
codec=avcodec_find_decoder(pCodecCtx->codec_id);
if(codec==NULL) {
cout << "can't find codec" << endl;
}
int open_code=avcodec_open(pCodecCtx,codec);
cout << "open code was " << open_code << endl;
cout << "got the codec for the input stream" << endl;
av_init_packet(&avpkt);
printf("Audio decoding\n");
outbuf = static_cast<uint8_t*>(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(pCodecCtx);
exit(1);
}
/* decode until eof */
avpkt.data = inbuf;
avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
while (avpkt.size > 0) {
out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
len = avcodec_decode_audio3(pCodecCtx, (short *)outbuf,
&out_size, &avpkt);
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);
}
avpkt.size -= len;
avpkt.data += len;
if (avpkt.size < AUDIO_REFILL_THRESH) {
/* Refill the input buffer, to avoid trying to decode
* incomplete frames. Instead of this, one could also use
* a parser, or use a proper container format through
* libavformat. */
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);
free(outbuf);
avcodec_close(pCodecCtx);
av_free(pCodecCtx);
}
int main(int argc, char **argv)
{
/* must be called before using avcodec lib */
if (argc !=3) {
printf("Usage : decode <input mp3 filename> <output raw sample
filename>\n");
return 1;
}
audio_decode_example(argv[2], argv[1]);
return 0;
}
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user