changeset 5458:b3d1348b251f

audio input buffer allocation/free cleanup
author arpi
date Mon, 01 Apr 2002 17:58:04 +0000
parents f248c9e86423
children 779ef929e227
files dll_init.c libmpcodecs/ad_a52.c libmpcodecs/ad_dk3adpcm.c libmpcodecs/ad_dk4adpcm.c libmpcodecs/ad_dshow.c libmpcodecs/ad_faad.c libmpcodecs/ad_hwac3.c libmpcodecs/ad_imaadpcm.c libmpcodecs/ad_msadpcm.c libmpcodecs/ad_roqaudio.c libmpcodecs/dec_audio.c
diffstat 11 files changed, 44 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/dll_init.c	Mon Apr 01 17:12:10 2002 +0000
+++ b/dll_init.c	Mon Apr 01 17:58:04 2002 +0000
@@ -103,15 +103,12 @@
     mp_msg(MSGT_WIN32,MSGL_V,"Audio ACM output buffer min. size: %ld\n",srcsize);
 
     acmStreamSize(sh_audio->srcstream, srcsize, &srcsize, ACM_STREAMSIZEF_DESTINATION);
-    sh_audio->audio_in_minsize=srcsize; // audio input min. size
-    mp_msg(MSGT_WIN32,MSGL_V,"Audio ACM input buffer min. size: %ld\n",srcsize);
-    
-    if(srcsize<in_fmt->nBlockAlign) srcsize=in_fmt->nBlockAlign;
+//    if(srcsize<in_fmt->nBlockAlign) srcsize=in_fmt->nBlockAlign;
 
-    sh_audio->a_in_buffer_size=2*sh_audio->audio_in_minsize;
-    sh_audio->a_in_buffer=malloc(sh_audio->a_in_buffer_size);
-    sh_audio->a_in_buffer_len=0;
+    mp_msg(MSGT_WIN32,MSGL_V,"Audio ACM input buffer min. size: %ld\n",srcsize);
 
+    sh_audio->audio_in_minsize=2*srcsize; // audio input min. size
+    
     return 1;
 }
 
--- a/libmpcodecs/ad_a52.c	Mon Apr 01 17:12:10 2002 +0000
+++ b/libmpcodecs/ad_a52.c	Mon Apr 01 17:58:04 2002 +0000
@@ -96,6 +96,7 @@
   /* Dolby AC3 audio: */
   /* however many channels, 2 bytes in a word, 256 samples in a block, 6 blocks in a frame */
   sh->audio_out_minsize=audio_output_channels*2*256*6;
+  sh->audio_in_minsize=3840;
   return 1;
 }
 
@@ -115,9 +116,6 @@
 	mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 init failed\n");
 	return 0;
   }
-   sh_audio->a_in_buffer_size=3840;
-   sh_audio->a_in_buffer=malloc(sh_audio->a_in_buffer_size);
-   sh_audio->a_in_buffer_len=0;
   if(a52_fillbuff(sh_audio)<0){
 	mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 sync failed\n");
 	return 0;
--- a/libmpcodecs/ad_dk3adpcm.c	Mon Apr 01 17:12:10 2002 +0000
+++ b/libmpcodecs/ad_dk3adpcm.c	Mon Apr 01 17:58:04 2002 +0000
@@ -71,6 +71,7 @@
   sh_audio->audio_out_minsize = sh_audio->wf->nBlockAlign * 6;
   sh_audio->ds->ss_div = 
     (sh_audio->wf->nBlockAlign - DK3_ADPCM_PREAMBLE_SIZE) * 8 / 3;
+  sh_audio->audio_in_minsize=
   sh_audio->ds->ss_mul = sh_audio->wf->nBlockAlign;
   return 1;
 }
@@ -81,17 +82,11 @@
   sh_audio->samplerate = sh_audio->wf->nSamplesPerSec;
   sh_audio->i_bps =
     (sh_audio->ds->ss_mul * sh_audio->samplerate) / sh_audio->ds->ss_div;
-
-  if ((sh_audio->a_in_buffer =
-    (unsigned char *)malloc(sh_audio->ds->ss_mul)) == NULL)
-    return 0;
-
   return 1;
 }
 
 static void uninit(sh_audio_t *sh_audio)
 {
-  free(sh_audio->a_in_buffer);
 }
 
 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
--- a/libmpcodecs/ad_dk4adpcm.c	Mon Apr 01 17:12:10 2002 +0000
+++ b/libmpcodecs/ad_dk4adpcm.c	Mon Apr 01 17:58:04 2002 +0000
@@ -25,8 +25,8 @@
     (((sh_audio->wf->nBlockAlign - DK4_ADPCM_PREAMBLE_SIZE) * 2) + 1) * 4;
   sh_audio->ds->ss_div =
     ((sh_audio->wf->nBlockAlign - DK4_ADPCM_PREAMBLE_SIZE) * 2) + 1;
+  sh_audio->audio_in_minsize=
   sh_audio->ds->ss_mul=sh_audio->wf->nBlockAlign;
-
   return 1;
 }
 
@@ -37,17 +37,11 @@
   sh_audio->i_bps = sh_audio->wf->nBlockAlign *
     (sh_audio->channels*sh_audio->samplerate) /
     (((sh_audio->wf->nBlockAlign - DK4_ADPCM_PREAMBLE_SIZE) * 2) + 1);
-
-  if ((sh_audio->a_in_buffer =
-    (unsigned char *)malloc(sh_audio->ds->ss_mul)) == NULL)
-    return 0;
-
   return 1;
 }
 
 static void uninit(sh_audio_t *sh_audio)
 {
-  free(sh_audio->a_in_buffer);
 }
 
 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
--- a/libmpcodecs/ad_dshow.c	Mon Apr 01 17:12:10 2002 +0000
+++ b/libmpcodecs/ad_dshow.c	Mon Apr 01 17:58:04 2002 +0000
@@ -36,18 +36,14 @@
   {
     mp_msg(MSGT_DECAUDIO,MSGL_ERR,MSGTR_MissingDLLcodec,sh_audio->codec->dll);
     return 0;
-  } else {
+  }
     sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec;
     sh_audio->channels=sh_audio->wf->nChannels;
     sh_audio->samplerate=sh_audio->wf->nSamplesPerSec;
     sh_audio->audio_in_minsize=2*sh_audio->wf->nBlockAlign;
     if(sh_audio->audio_in_minsize<8192) sh_audio->audio_in_minsize=8192;
-    sh_audio->a_in_buffer_size=sh_audio->audio_in_minsize;
-    sh_audio->a_in_buffer=malloc(sh_audio->a_in_buffer_size);
-    sh_audio->a_in_buffer_len=0;
     sh_audio->audio_out_minsize=16384;
     sh_audio->context = ds_adec;
-  }
   mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: Win32/DShow audio codec init OK!\n");
   return 1;
 }
--- a/libmpcodecs/ad_faad.c	Mon Apr 01 17:12:10 2002 +0000
+++ b/libmpcodecs/ad_faad.c	Mon Apr 01 17:58:04 2002 +0000
@@ -39,13 +39,8 @@
 
 static int preinit(sh_audio_t *sh)
 {
+  sh->audio_out_minsize=2048*FAAD_MAX_CHANNELS;
   sh->audio_in_minsize=FAAD_BUFFLEN;
-  sh->audio_out_minsize=2048*FAAD_MAX_CHANNELS;
-
-  // XXX: why is this duplicated in sh struct? ::atmos
-  sh->a_in_buffer_size=sh->audio_in_minsize;
-  sh->a_in_buffer=malloc(sh->a_in_buffer_size);
-  sh->a_in_buffer_len=0;
   return 1;
 }
 
@@ -126,10 +121,6 @@
 {
   mp_msg(MSGT_DECAUDIO,MSGL_V,"FAAD: Closing decoder!\n");
   faacDecClose(faac_hdec);
-  if(sh->a_in_buffer_size) {
-    free(sh->a_in_buffer);
-    sh->a_in_buffer = NULL;
-  } 
 }
 
 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
--- a/libmpcodecs/ad_hwac3.c	Mon Apr 01 17:12:10 2002 +0000
+++ b/libmpcodecs/ad_hwac3.c	Mon Apr 01 17:58:04 2002 +0000
@@ -32,6 +32,7 @@
 {
   /* Dolby AC3 audio: */
   sh->audio_out_minsize=4*256*6;
+  sh->audio_in_minsize=3840;
   sh->channels=2;
   return 1;
 }
@@ -44,9 +45,6 @@
        mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 init failed\n");
        return 0;
   }
-  sh_audio->a_in_buffer_size=3840;
-  sh_audio->a_in_buffer=malloc(sh_audio->a_in_buffer_size);
-  sh_audio->a_in_buffer_len=0;
   if(a52_fillbuff(sh_audio)<0) {
        mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 sync failed\n");
        return 0;
--- a/libmpcodecs/ad_imaadpcm.c	Mon Apr 01 17:12:10 2002 +0000
+++ b/libmpcodecs/ad_imaadpcm.c	Mon Apr 01 17:58:04 2002 +0000
@@ -98,6 +98,7 @@
     sh_audio->ds->ss_div = QT_IMA_ADPCM_SAMPLES_PER_BLOCK;
     sh_audio->ds->ss_mul = QT_IMA_ADPCM_BLOCK_SIZE * sh_audio->wf->nChannels;
   }
+  sh_audio->audio_in_minsize=sh_audio->ds->ss_mul;
   return 1;
 }
 
@@ -110,16 +111,11 @@
   sh_audio->i_bps = 
     (sh_audio->ds->ss_mul * sh_audio->samplerate) / sh_audio->ds->ss_div;
 
-  if ((sh_audio->a_in_buffer =
-    (unsigned char *)malloc(sh_audio->ds->ss_mul)) == NULL)
-    return 0;
-
   return 1;
 }
 
 static void uninit(sh_audio_t *sh_audio)
 {
-  free(sh_audio->a_in_buffer);
 }
 
 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
--- a/libmpcodecs/ad_msadpcm.c	Mon Apr 01 17:12:10 2002 +0000
+++ b/libmpcodecs/ad_msadpcm.c	Mon Apr 01 17:58:04 2002 +0000
@@ -66,8 +66,8 @@
   sh_audio->audio_out_minsize = sh_audio->wf->nBlockAlign * 4;
   sh_audio->ds->ss_div = 
     (sh_audio->wf->nBlockAlign - MS_ADPCM_PREAMBLE_SIZE) * 2;
+  sh_audio->audio_in_minsize =
   sh_audio->ds->ss_mul = sh_audio->wf->nBlockAlign;
-
   return 1;
 }
 
@@ -78,16 +78,11 @@
   sh_audio->i_bps = sh_audio->wf->nBlockAlign *
     (sh_audio->channels*sh_audio->samplerate) / sh_audio->ds->ss_div;
 
-  if ((sh_audio->a_in_buffer =
-    (unsigned char *)malloc(sh_audio->ds->ss_mul)) == NULL)
-    return 0;
-
   return 1;
 }
 
 static void uninit(sh_audio_t *sh_audio)
 {
-  free(sh_audio->a_in_buffer);
 }
 
 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
--- a/libmpcodecs/ad_roqaudio.c	Mon Apr 01 17:12:10 2002 +0000
+++ b/libmpcodecs/ad_roqaudio.c	Mon Apr 01 17:58:04 2002 +0000
@@ -22,6 +22,7 @@
 {
   // minsize was stored in wf->nBlockAlign by the RoQ demuxer
   sh_audio->audio_out_minsize=sh_audio->wf->nBlockAlign;
+  sh_audio->audio_in_minsize=sh_audio->audio_out_minsize / 2; // FIXME?
   sh_audio->context = roq_decode_audio_init();
   return 1;
 }
@@ -31,17 +32,11 @@
   sh_audio->channels=sh_audio->wf->nChannels;
   sh_audio->samplerate=sh_audio->wf->nSamplesPerSec;
   sh_audio->i_bps = 44100;
-
-  if ((sh_audio->a_in_buffer =
-    (unsigned char *)malloc(sh_audio->audio_out_minsize / 2)) == NULL)
-    return 0;
-
   return 1;
 }
 
 static void uninit(sh_audio_t *sh_audio)
 {
-  free(sh_audio->a_in_buffer);
 }
 
 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
--- a/libmpcodecs/dec_audio.c	Mon Apr 01 17:12:10 2002 +0000
+++ b/libmpcodecs/dec_audio.c	Mon Apr 01 17:58:04 2002 +0000
@@ -40,6 +40,14 @@
   }
   
   printf("Selecting Audio Decoder: [%s] %s\n",mpadec->info->short_name,mpadec->info->name);
+
+  // reset in/out buffer size/pointer:
+  sh_audio->a_buffer_size=0;
+  sh_audio->a_buffer=NULL;
+  sh_audio->a_in_buffer_size=0;
+  sh_audio->a_in_buffer=NULL;
+
+  // Set up some common usefull defaults. ad->preinit() can override these:
   
   sh_audio->samplesize=2;
 #ifdef WORDS_BIGENDIAN
@@ -48,15 +56,11 @@
   sh_audio->sample_format=AFMT_S16_LE;
 #endif
   sh_audio->samplerate=0;
-  sh_audio->o_bps=0;
-
-  sh_audio->a_buffer_size=0;
-  sh_audio->a_buffer=NULL;
+  sh_audio->i_bps=0;  // input rate (bytes/sec)
+  sh_audio->o_bps=0;  // output rate (bytes/sec)
 
-  sh_audio->a_in_buffer_len=0;
-
-  /* setup required min. in/out buffer size:*/
   sh_audio->audio_out_minsize=8192;/* default size, maybe not enough for Win32/ACM*/
+  sh_audio->audio_in_minsize=0;
   
   if(!mpadec->preinit(sh_audio))
   {
@@ -64,6 +68,16 @@
       return 0;
   }
 
+/* allocate audio in buffer: */
+  if(sh_audio->audio_in_minsize>0){
+      sh_audio->a_in_buffer_size=sh_audio->audio_in_minsize;
+      mp_msg(MSGT_DECAUDIO,MSGL_V,"dec_audio: Allocating %d bytes for input buffer\n",
+          sh_audio->a_in_buffer_size);
+      sh_audio->a_in_buffer=malloc(sh_audio->a_in_buffer_size);
+      memset(sh_audio->a_in_buffer,0,sh_audio->a_in_buffer_size);
+      sh_audio->a_in_buffer_len=0;
+  }
+
 /* allocate audio out buffer: */
   sh_audio->a_buffer_size=sh_audio->audio_out_minsize+MAX_OUTBURST; /* worst case calc.*/
 
@@ -79,30 +93,34 @@
   sh_audio->a_buffer_len=0;
 
   if(!mpadec->init(sh_audio)){
-      printf("ADecoder init failed :(\n");
+      mp_msg(MSGT_DECAUDIO,MSGL_WARN,"ADecoder init failed :(\n");
+      uninit_audio(sh_audio); // free buffers
       return 0;
   }
+
   sh_audio->inited=1;
   
   if(!sh_audio->channels || !sh_audio->samplerate){
     mp_msg(MSGT_DECAUDIO,MSGL_WARN,MSGTR_UnknownAudio);
-    if(sh_audio->a_buffer) free(sh_audio->a_buffer);
-    sh_audio->a_buffer=NULL;
+    uninit_audio(sh_audio); // free buffers
     return 0;
   }
 
   if(!sh_audio->o_bps)
   sh_audio->o_bps=sh_audio->channels*sh_audio->samplerate*sh_audio->samplesize;
-  return sh_audio->codec->driver;
+  
+  return 1;
 }
 
 void uninit_audio(sh_audio_t *sh_audio)
 {
+    if(sh_audio->a_buffer) free(sh_audio->a_buffer);
+    sh_audio->a_buffer=NULL;
+    if(sh_audio->a_in_buffer) free(sh_audio->a_in_buffer);
+    sh_audio->a_in_buffer=NULL;
     if(!sh_audio->inited) return;
     mp_msg(MSGT_DECAUDIO,MSGL_V,"uninit audio: %d  \n",sh_audio->codec->driver);
     mpadec->uninit(sh_audio);
-    if(sh_audio->a_buffer) free(sh_audio->a_buffer);
-    sh_audio->a_buffer=NULL;
     sh_audio->inited=0;
 }