changeset 15199:980910eb6f0c

assign correct tag, dwScale and dwBlockAlign to mpeg audio; optionally assign layer and samples_per_frame when parsing mpa header
author nicodvb
date Sun, 17 Apr 2005 09:42:51 +0000
parents 847a9a75baa0
children 94c2be52269d
files libmpdemux/demux_audio.c libmpdemux/mp3_hdr.c libmpdemux/mp3_hdr.h
diffstat 3 files changed, 25 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/libmpdemux/demux_audio.c	Sun Apr 17 09:13:48 2005 +0000
+++ b/libmpdemux/demux_audio.c	Sun Apr 17 09:42:51 2005 +0000
@@ -117,7 +117,7 @@
   stream_t *s;
   sh_audio_t* sh_audio;
   uint8_t hdr[HDR_SIZE];
-  int frmt = 0, n = 0, step, mp3_freq, mp3_chans, mp3_flen;
+  int frmt = 0, n = 0, step, mp3_freq, mp3_chans, mp3_flen, mpa_layer = 3, mpa_spf = 1152;
   off_t st_pos = 0, next_frame_pos = 0;
   // mp3_hdrs list is sorted first by next_frame_pos and then by frame_pos
   mp3_hdr_t *mp3_hdrs = NULL, *mp3_found = NULL;
@@ -157,7 +157,7 @@
     } else if( hdr[0] == 'f' && hdr[1] == 'm' && hdr[2] == 't' && hdr[3] == ' ' ) {
       frmt = WAV;
       break;      
-    } else if((mp3_flen = mp_get_mp3_header(hdr,&mp3_chans,&mp3_freq)) > 0) {
+    } else if((mp3_flen = mp_get_mp3_header(hdr,&mp3_chans,&mp3_freq,&mpa_spf,&mpa_layer)) > 0) {
       mp3_found = add_mp3_hdr(&mp3_hdrs, st_pos, mp3_chans, mp3_freq, mp3_flen);
       if (mp3_found) {
         frmt = MP3;
@@ -184,17 +184,17 @@
 
   switch(frmt) {
   case MP3:
-    sh_audio->format = 0x55;
+    sh_audio->format = (mpa_layer < 3 ? 0x50 : 0x55);
     demuxer->movi_start = mp3_found->frame_pos;
     next_frame_pos = mp3_found->next_frame_pos;
     sh_audio->audio.dwSampleSize= 0;
-    sh_audio->audio.dwScale = 1152;
+    sh_audio->audio.dwScale = mpa_spf;
     sh_audio->audio.dwRate = mp3_found->mp3_freq;
     sh_audio->wf = malloc(sizeof(WAVEFORMATEX));
     sh_audio->wf->wFormatTag = sh_audio->format;
     sh_audio->wf->nChannels = mp3_found->mp3_chans;
     sh_audio->wf->nSamplesPerSec = mp3_found->mp3_freq;
-    sh_audio->wf->nBlockAlign = 1152;
+    sh_audio->wf->nBlockAlign = mpa_spf;
     sh_audio->wf->wBitsPerSample = 16;
     sh_audio->wf->cbSize = 0;    
     free(mp3_found);
@@ -397,7 +397,7 @@
 	dp = new_demux_packet(l);
 	memcpy(dp->buffer,hdr,4);
 	stream_read(s,dp->buffer + 4,l-4);
-	priv->last_pts = priv->last_pts < 0 ? 0 : priv->last_pts + 1152/(float)sh_audio->samplerate; // FIXME: 1152->576 if MPEG-2
+	priv->last_pts = priv->last_pts < 0 ? 0 : priv->last_pts + sh_audio->audio.dwScale/(float)sh_audio->samplerate;
 	break;
       }
     } break;
@@ -433,7 +433,7 @@
   da_priv_t* priv = demuxer->priv;
   sh_audio_t* sh = (sh_audio_t*)demuxer->audio->sh;
 
-  nf = time*sh->samplerate/1152;
+  nf = time*sh->samplerate/sh->audio.dwScale;
   while(nf > 0) {
     stream_read(demuxer->stream,hdr,4);
     len = mp_decode_mp3_header(hdr);
@@ -442,7 +442,7 @@
       continue;
     }
     stream_skip(demuxer->stream,len-4);
-    priv->last_pts += 1152/(float)sh->samplerate;
+    priv->last_pts += sh->audio.dwScale/(float)sh->samplerate;
     nf--;
   }
 }
--- a/libmpdemux/mp3_hdr.c	Sun Apr 17 09:13:48 2005 +0000
+++ b/libmpdemux/mp3_hdr.c	Sun Apr 17 09:42:51 2005 +0000
@@ -34,7 +34,7 @@
 /*
  * return frame size or -1 (bad frame)
  */
-int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* srate){
+int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* srate, int* spf, int* mpa_layer){
     int stereo,ssize,lsf,framesize,padding,bitrate_index,sampling_frequency;
     int layer, mult[3] = { 12000, 144000, 144000 };
     unsigned long newhead = 
@@ -115,7 +115,20 @@
       framesize += padding;
 
 //    if(framesize<=0 || framesize>MAXFRAMESIZE) return FALSE;
-    if(srate) *srate = freqs[sampling_frequency];
+    if(srate) {
+      *srate = freqs[sampling_frequency];
+      if(spf) {
+        if(layer == 1)
+	  *spf = 384;
+        else if(layer == 2)
+	  *spf = 1152;
+        else if(*srate < 32000)
+          *spf = 576;
+        else
+	  *spf = 1152;
+      }
+    }
+    if(mpa_layer) *mpa_layer = layer;
     if(chans) *chans = stereo;
 
     return framesize;
--- a/libmpdemux/mp3_hdr.h	Sun Apr 17 09:13:48 2005 +0000
+++ b/libmpdemux/mp3_hdr.h	Sun Apr 17 09:42:51 2005 +0000
@@ -1,7 +1,7 @@
 
-int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* freq);
+int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* freq, int* spf, int* mpa_layer);
 
-#define mp_decode_mp3_header(hbuf)  mp_get_mp3_header(hbuf,NULL,NULL)
+#define mp_decode_mp3_header(hbuf)  mp_get_mp3_header(hbuf,NULL,NULL,NULL,NULL)
 
 static inline int mp_check_mp3_header(unsigned int head){
     if( (head & 0x0000e0ff) != 0x0000e0ff ||