[FFmpeg-devel] [PATCH] set correct bytespersec value in wav/ima_adpcm header

ucsavl at gmail.com ucsavl
Tue May 4 13:37:59 CEST 2010


On Mon, May 03, 2010 at 01:36:30PM +0200, Michael Niedermayer wrote:
> On Mon, May 03, 2010 at 10:10:13AM +0300, ucsavl at gmail.com wrote:
> > On Mon, May 03, 2010 at 04:09:23AM +0200, Michael Niedermayer wrote:
> > > On Sun, May 02, 2010 at 05:07:38PM +0300, ucsavl at gmail.com wrote:
> > > > I'm using ffmpeg to convert mp3s to wav + ima_idpcm so I could listen to
> > > > them on a mobile phone that doesn't support mp3.
> > > > 
> > > > The problem is the files always appear shorter than they are because of
> > > > the bogus bytespersec value in the header. When using sox instead of
> > > > ffmpeg they're OK, but sox is orders of magnitude slower than ffmpeg,
> > > > so it's of no use to me.
> > > > 
> > > > This patch sets the bytespersec value to
> > > > (packets per second) * (bytes per packet), where (packets per second) 
> > > > is calculated from (samples per second) / (samples per packet).
> > > > 
> > > > For reference, according to libavcodec/avcodec.h:
> > > > enc->sample_rate		samples per second
> > > > enc->frame_size			samples per packet
> > > > enc->block_align, blkalign	bytes per packet
> > > > 
> > > > diff --git a/libavformat/riff.c b/libavformat/riff.c
> > > > index 340fd76..94b1ae2 100644
> > > > --- a/libavformat/riff.c
> > > > +++ b/libavformat/riff.c
> > > > @@ -370,6 +370,9 @@ int ff_put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
> > > >          enc->codec_id == CODEC_ID_PCM_F64LE ||
> > > >          enc->codec_id == CODEC_ID_PCM_S16LE) {
> > > >          bytespersec = enc->sample_rate * blkalign;
> > > > +    } else if(enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
> > > > +	bytespersec = (enc->sample_rate * blkalign + enc->frame_size / 2) /
> > > > +		enc->frame_size;
> > > 
> > > is adding CODEC_ID_ADPCM_IMA_WAV to the previous if() failing?
> > 
> > Yes, of course. The previous if() assumes that enc->frame_size == 1.
> 
> what about fixing it then?

Is there a simple way to check if a codec is pcm or adpcm?
All of them CODEC_ID_PCM_* and CODEC_ID_ADPCM_* should be added there.

diff --git a/libavformat/riff.c b/libavformat/riff.c
index 340fd76..f7cd4c1 100644
--- a/libavformat/riff.c
+++ b/libavformat/riff.c
@@ -368,8 +368,9 @@ int ff_put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
         enc->codec_id == CODEC_ID_PCM_S32LE ||
         enc->codec_id == CODEC_ID_PCM_F32LE ||
         enc->codec_id == CODEC_ID_PCM_F64LE ||
-        enc->codec_id == CODEC_ID_PCM_S16LE) {
-        bytespersec = enc->sample_rate * blkalign;
+        enc->codec_id == CODEC_ID_PCM_S16LE ||
+        enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
+        bytespersec = (enc->sample_rate * blkalign + enc->frame_size / 2) / enc->frame_size;
     } else {
         bytespersec = enc->bit_rate / 8;
     }



More information about the ffmpeg-devel mailing list