On Fri, Apr 4, 2008 at 1:23 PM, Michel Bardiaux <[EMAIL PROTECTED]> wrote:
>
> Please dont top-post; trim old sigs and similar cruft when replying; and
> preferrably post complete code that one can compile and try without a
> lot of effort.
>
ok, sorry. Here's a clean post.
My problem: I'm getting twice as many decoded values as expected, and
0s in the second half of every decoded frame...
My easily compilable and triable code is attached. It creates
/tmp/decoded_samples.dat and printf the number of decoded values.
thank you
jul
/*
* copyright (c) 2001 Fabrice Bellard
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
/*
* @file apiexample.c
* avcodec API use example.
*
* Note that this library only handles codecs (mpeg, mpeg4, etc...),
* not file formats (avi, vob, etc...). See library 'libavformat' for the
* format handling
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "ffmpeg/avcodec.h"
#include "ffmpeg/avformat.h"
//
// Audio decoding.
//
int audio_decode_example(const char *filename)
{
AVFormatContext * pFormatCtx;
AVCodec * pAudioCodec;
AVCodecContext * pAudioCodecCtx;
AVPacket avPacket;
avPacket.data=NULL;
av_register_all();
// Open file
if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL)!=0)
{
printf("Couldn't open input file\n");
return -1; //
}
// Retrieve stream information
if(av_find_stream_info(pFormatCtx)<0)
{
printf("Couldn't retrieve stream information\n");
return -1; // Couldn't find stream information
}
long audioStream=-1;
for(unsigned long ix=0; ix<pFormatCtx->nb_streams; ix++)
{
if(pFormatCtx->streams[ix]->codec->codec_type==CODEC_TYPE_AUDIO && audioStream==-1)
{
audioStream=ix;
break;
}
}
if(audioStream < 0)
{
printf("Couldn't find stream\n");
return -1;
}
// Get a pointer to the codec context for the video stream
pAudioCodecCtx=pFormatCtx->streams[audioStream]->codec;
// Find the decoder for the video stream
pAudioCodec=avcodec_find_decoder(pAudioCodecCtx->codec_id);
if(pAudioCodec==NULL)
{
printf("Couldn't find codec\n");
return -1;
}
// Open codec
if(avcodec_open(pAudioCodecCtx, pAudioCodec)<0)
{
printf("Couldn't open codec\n");
return -1;
}
FILE *outfile;
outfile=fopen("/tmp/decoded_samples.dat","w");
int count=0;
//output buffer
int16_t *output_buf;
output_buf= new int16_t[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3)/2];
while(1)//get audio packets only
{
// Free old avPacket
if(avPacket.data!=NULL)
av_free_packet(&avPacket);
// Read new avPacket
if(av_read_frame(pFormatCtx, &avPacket)<0)
{
printf("done\n");
break;//the job is done
}
if(avPacket.stream_index == audioStream)
{
long audioBytesRemaining=avPacket.size;
long audioRawDataSize = avPacket.size;
uint8_t * pAudioRawData=(uint8_t *)avPacket.data;
int bytesDecoded = -1;
while(audioBytesRemaining>=FF_INPUT_BUFFER_PADDING_SIZE)
{
int numAudioBytes = AVCODEC_MAX_AUDIO_FRAME_SIZE;
//padding zeros for avcodec_decode_audio2
long size = audioBytesRemaining + FF_INPUT_BUFFER_PADDING_SIZE;
uint8_t * pIn = new uint8_t[size];
memset(pIn, 0, size);
memcpy(pIn, pAudioRawData, audioBytesRemaining);
bytesDecoded = avcodec_decode_audio2(pAudioCodecCtx, output_buf, &numAudioBytes, pIn , audioBytesRemaining);
delete pIn;
count+=numAudioBytes;
if(bytesDecoded<0)
{
printf("Decoding error\n");
return -1;
}
else
{
for(int j=0;j<numAudioBytes;j++)
fprintf(outfile,"%d\n",output_buf[j]);
}
audioBytesRemaining-=bytesDecoded;
pAudioRawData+=bytesDecoded;
}
}
}
delete output_buf;
printf("count = %d\n",count);
fclose(outfile);
// Close the audio codec
if(pAudioCodecCtx)
avcodec_close(pAudioCodecCtx);
// Close the video file
if(pFormatCtx)
av_close_input_file(pFormatCtx);
}
int main(int argc, char **argv)
{
const char *filename=argv[1];
// register all formats and codecs
av_register_all();
//audio_encode_example("/tmp/test.mp2");
audio_decode_example(filename);
return 0;
}
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user