Home/News | About | Download | Documentation | Forum | Bug Reports | Contact | Donations | Consulting | Projects | Legal | Security | FATE


This forum has not been maintained for a long time and will probably get deleted in the near future.
For faster responses to your questions, please use StackOverflow instead and tag your questions with "FFmpeg".
If you need a backup of the posts from this forum, please contact me directly.

Decode Mp3 fail

Suggestions and feature requests, whislists, etc.

Decode Mp3 fail

Postby tomlee1302 » Sat Oct 03, 2015 2:10 pm

It doesn't work, when I decode mp3 by method "avcodec_decode_audio4", in my iPhone Simulator 8.1
output: [mp3 @ 0xd82cc00] Header missing
len -1094995529

but I use it decode .wav, it works

////////////////////////////code///////////////////////////

/*
* Audio decoding.
*/
static void audio_decode_example(const char *outfilename, const char *filename)
{
AVCodec *codec;
AVCodecContext *c= NULL;
AVFormatContext *pFormatCtx = avformat_alloc_context();
AVCodecContext * aCodecCtx= NULL; // class member for me, needed for following reading
int stream_index_; // class member for me, needed for extra stuff
int len;
int audioStream = 0;
FILE *f, *outfile;
uint8_t inbuf[AUDIO_INBUF_SIZE - AV_INPUT_BUFFER_PADDING_SIZE];
AVPacket avpkt;
AVFrame *decoded_frame = NULL;
int error = 0;
AVPacket packet;

error = avformat_open_input(&pFormatCtx, filename, NULL, NULL);
if(error !=0)
{
return ;
}

error = avformat_find_stream_info(pFormatCtx, NULL);

if( error <0)
{
printf("Couldn't find stream information error :%d\n",error);
return ;
}

for(size_t i=0; i< pFormatCtx->nb_streams; ++i)
{
AVCodecContext* tc = pFormatCtx->streams[i]->codec;
if (tc->codec_type == AVMEDIA_TYPE_AUDIO) {
aCodecCtx = tc;
audioStream = i;
break;
}
}

if(audioStream== -1)
{
return ;
}

aCodecCtx = pFormatCtx->streams[audioStream]->codec;

av_init_packet(&avpkt);

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

/* find the mpeg audio decoder */
AVOutputFormat* fmt = av_guess_format(0, //const char *short_name,
filename, 0); // const char *mime_type);;
codec = fmt ? avcodec_find_decoder(fmt->audio_codec) : 0;
if (!codec)
{
fprintf(stderr, "Codec not found\n");
exit(1);
}

c = aCodecCtx;
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 - 2 * AV_INPUT_BUFFER_PADDING_SIZE, f);

while (av_read_frame(pFormatCtx, &packet) >= 0) {
if(packet.stream_index == audioStream)
{
while(packet.size > 0) {
int i, ch;
int got_frame = 0;

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

len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
if (len < 0) {
fprintf(stderr, "Error while decoding len %d\n ", len);
exit(1);
}
fprintf(stdout, "OK while decoding len %d %d\n \n \n \n \n \n ", len);
if (got_frame) {
/* if a frame has been decoded, output it */
int data_size = av_get_bytes_per_sample(c->sample_fmt);
if (data_size < 0) {
/* This should not occur, checking just for paranoia */
fprintf(stderr, "Failed to calculate data size\n");
exit(1);
}
for (i=0; i<decoded_frame->nb_samples; i++)
for (ch=0; ch<c->channels; ch++)
fwrite(decoded_frame->data[ch] + data_size*i, 1, data_size, outfile);
}
avpkt.size -= len;
avpkt.data += len;
avpkt.dts =
avpkt.pts = AV_NOPTS_VALUE;
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);

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


///////////////////////-end-/////////////////////////////
tomlee1302
 
Posts: 1
Joined: Sat Oct 03, 2015 1:52 pm

Return to Suggestions

Who is online

Users browsing this forum: No registered users and 2 guests