最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】Mplayer播放时,显示出的VBR MP3的播放时间不正确,附Mplayer的VBR的Patch

工作和技术 crifan 691浏览 0评论

这里用的是之前某个版本的Mplayer,打了patch之后,可以正确计算VBR 的MP3的实际播放时间了,但是如果用Mad库播放的话,mad库在Init函数中,又重新将sh->i_bps重写了,导致Mplayer播放时的显示输出播放总长,显示错误。

【Mplayer的VBR patch + 修复了Lib Mad的重写变量导致Mplayer显示播放总长不正确问题】

diff -ruNa ./MPlayer-1.0rc2/libmpcodecs/ad_hwmpa.c ./MPlayer-1.0rc2_VBR/libmpcodecs/ad_hwmpa.c
— ./MPlayer-1.0rc2/libmpcodecs/ad_hwmpa.c 2009-09-02 18:00:08.000000000 +0800
+++ ./MPlayer-1.0rc2_VBR/libmpcodecs/ad_hwmpa.c 2009-09-02 17:59:40.000000000 +0800
@@ -35,7 +35,7 @@
   {
    while(cnt + 4 < sh->a_in_buffer_len)
    {
–    x = mp_get_mp3_header(&(sh->a_in_buffer[cnt]), chans, srate, spf, mpa_layer, br);
+    x = mp_get_mp3_header(&(sh->a_in_buffer[cnt]), chans, srate, spf, mpa_layer, br, NULL);
     if(x > 0)
     {
      frames_count++;
diff -ruNa ./MPlayer-1.0rc2/libmpcodecs/ad_libmad.c ./MPlayer-1.0rc2_VBR/libmpcodecs/ad_libmad.c
— ./MPlayer-1.0rc2/libmpcodecs/ad_libmad.c 2009-09-02 18:00:07.000000000 +0800
+++ ./MPlayer-1.0rc2_VBR/libmpcodecs/ad_libmad.c 2009-09-02 17:59:39.000000000 +0800
@@ -85,7 +85,10 @@
  
   sh->channels=(this->frame.header.mode == MAD_MODE_SINGLE_CHANNEL) ? 1 : 2;
   sh->samplerate=this->frame.header.samplerate;
– sh->i_bps=this->frame.header.bitrate/8;
+ /* only recaculate the i_bps if the Mplayer don’t calc it
+ when VBR the i_bsp is already calculated by Mplayer */
+ if(sh->i_bps < 0)
+   sh->i_bps=this->frame.header.bitrate/8;
   sh->samplesize=2;
  
   return 1;
diff -ruNa ./MPlayer-1.0rc2/libmpcodecs/ad_mp3lib.c ./MPlayer-1.0rc2_VBR/libmpcodecs/ad_mp3lib.c
— ./MPlayer-1.0rc2/libmpcodecs/ad_mp3lib.c 2009-09-02 18:00:08.000000000 +0800
+++ ./MPlayer-1.0rc2_VBR/libmpcodecs/ad_mp3lib.c 2009-09-02 17:59:40.000000000 +0800
@@ -47,10 +47,10 @@
   MP3_samplerate=MP3_channels=0;
   sh->a_buffer_len=MP3_DecodeFrame(sh->a_buffer,-1);
   if(!sh->a_buffer_len) return 0; // unsupported layer/format
– sh->channels=2; // hack
+ sh->channels=sh->wf->nChannels;
   sh->samplesize=2;
   sh->samplerate=MP3_samplerate;
– sh->i_bps=MP3_bitrate*(1000/8);
+ sh->i_bps=sh->wf->nAvgBytesPerSec;
   MP3_PrintHeader();
   return 1;
}
diff -ruNa ./MPlayer-1.0rc2/libmpdemux/demux_audio.c ./MPlayer-1.0rc2_VBR/libmpdemux/demux_audio.c
— ./MPlayer-1.0rc2/libmpdemux/demux_audio.c 2009-09-02 18:00:04.000000000 +0800
+++ ./MPlayer-1.0rc2_VBR/libmpdemux/demux_audio.c 2009-09-02 17:59:38.000000000 +0800
@@ -41,6 +41,7 @@
   int mpa_spf;
   int mpa_layer;
   int mpa_br;
+ int mpa_ssize;
   int cons_hdrs; // if this reaches MIN_MP3_HDRS we accept as MP3 file
   struct mp3_hdr *next;
} mp3_hdr_t;
@@ -86,7 +87,7 @@
*/
static mp3_hdr_t *add_mp3_hdr(mp3_hdr_t **list, off_t st_pos,
                                int mp3_chans, int mp3_freq, int mpa_spf,
–                               int mpa_layer, int mpa_br, int mp3_flen) {
+                               int mpa_layer, int mpa_br, int mp3_flen, int mpa_ssize) {
   mp3_hdr_t *tmp;
   int in_list = 0;
   while (*list && (*list)->next_frame_pos <= st_pos) {
@@ -123,6 +124,7 @@
     tmp->mp3_freq = mp3_freq;
     tmp->mpa_spf = mpa_spf;
     tmp->mpa_layer = mpa_layer;
+    tmp->mpa_ssize = mpa_ssize;
     tmp->mpa_br = mpa_br;
     tmp->cons_hdrs = 1;
     tmp->next = *list;
@@ -277,6 +279,7 @@
   sh_audio_t* sh_audio;
   uint8_t hdr[HDR_SIZE];
   int frmt = 0, n = 0, step;
+ int mpeghdr_pos;
   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;
@@ -284,16 +287,15 @@
  
   s = demuxer->stream;

+ mpeghdr_pos = stream_tell(s);
   stream_read(s, hdr, HDR_SIZE);
   while(n < 30000 && !s->eof) {
–    int mp3_freq, mp3_chans, mp3_flen, mpa_layer, mpa_spf, mpa_br;
+    int mp3_freq, mp3_chans, mp3_flen, mpa_layer, mpa_spf, mpa_br, mpa_ssize;
     st_pos = stream_tell(s) – HDR_SIZE;
     step = 1;

     if( hdr[0] == ‘R’ && hdr[1] == ‘I’ && hdr[2] == ‘F’ && hdr[3] == ‘F’ ) {
       stream_skip(s,4);
–      if(s->eof)
– break;
       stream_read(s,hdr,4);
       if(s->eof)
   break;
@@ -307,16 +309,19 @@
       int len;
       stream_skip(s,2);
       stream_read(s,hdr,4);
+      if(s->eof)
+ break;
       len = (hdr[0]<<21) | (hdr[1]<<14) | (hdr[2]<<7) | hdr[3];
       stream_skip(s,len);
+      mpeghdr_pos = stream_tell(s);
       step = 4;
     } 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,
–                                &mpa_spf, &mpa_layer, &mpa_br)) > 0) {
+                                &mpa_spf, &mpa_layer, &mpa_br, &mpa_ssize)) > 0) {
       mp3_found = add_mp3_hdr(&mp3_hdrs, st_pos, mp3_chans, mp3_freq,
–                              mpa_spf, mpa_layer, mpa_br, mp3_flen);
+                              mpa_spf, mpa_layer, mpa_br, mp3_flen, mpa_ssize);
       if (mp3_found) {
         frmt = MP3;
         break;
@@ -340,7 +345,40 @@
   sh_audio = new_sh_audio(demuxer,0);

   switch(frmt) {
– case MP3:
+ case MP3: {
+    // Search for VBR header and compute correct bitrate if necessary
+    uint8_t vbrhdr[4];
+    uint8_t framesnb_hdr[4];
+    int frames_nb = -1;
+    uint8_t frames_field;
+
+    stream_seek(s,mpeghdr_pos+4+mp3_found->mpa_ssize);
+    stream_read(s,vbrhdr,4);
+    if(!s->eof)
+      if( (vbrhdr[0] == ‘X’ && vbrhdr[1] == ‘i’ && vbrhdr[2] == ‘n’ && vbrhdr[3] == ‘g’)
+   || (vbrhdr[0] == ‘I’ && vbrhdr[1] == ‘n’ && vbrhdr[2] == ‘f’ && vbrhdr[3] == ‘o’) ) { // We found a XING header
+ stream_seek(s,mpeghdr_pos+11+mp3_found->mpa_ssize);
+ stream_read(s,&frames_field,1);
+ if( frames_field & 1 ) {
+   stream_seek(s,mpeghdr_pos+12+mp3_found->mpa_ssize); // We jump to Frames number
+   stream_read(s,framesnb_hdr,4);
+   if(s->eof)
+     break;
+   frames_nb = (framesnb_hdr[0]<<24) | (framesnb_hdr[1]<<16) | (framesnb_hdr[2]<<8) | (framesnb_hdr[3]);
+ }
+      }
+      else {
+ stream_seek(s,mpeghdr_pos+36);
+ stream_read(s,vbrhdr,4);
+ if( vbrhdr[0] == ‘V’ && vbrhdr[1] == ‘B’ && vbrhdr[2] == ‘R’ && vbrhdr[3] == ‘I’ ) { // We found a VBRI header
+   stream_seek(s,mpeghdr_pos+50); // We jump to Frames number
+   stream_read(s,framesnb_hdr,4);
+   if(s->eof)
+     break;
+   frames_nb = (framesnb_hdr[0]<<24) | (framesnb_hdr[1]<<16) | (framesnb_hdr[2]<<8) | (framesnb_hdr[3]);
+ }
+      }
+   
     sh_audio->format = (mp3_found->mpa_layer < 3 ? 0x50 : 0x55);
     demuxer->movi_start = mp3_found->frame_pos;
     next_frame_pos = mp3_found->next_frame_pos;
@@ -351,7 +389,17 @@
     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->nAvgBytesPerSec = mp3_found->mpa_br * (1000 / 8);
+    if (frames_nb > 0)
+    {
+      sh_audio->wf->nAvgBytesPerSec = ((demuxer->movi_end-demuxer->movi_start)*mp3_found->mp3_freq)/(frames_nb*mp3_found->mpa_spf);
+
+
+ mp_msg(MSGT_DEMUX, MSGL_INFO, " -1-1-1-1-: end=%lld start=%lld freq/rate=%d, frames=%d, spf=%d AvgRate=%.2fn",
+   demuxer->movi_end, demuxer->movi_start, mp3_found->mp3_freq, frames_nb, mp3_found->mpa_spf, sh_audio->wf->nAvgBytesPerSec);
+
+    }
+    else
+      sh_audio->wf->nAvgBytesPerSec = mp3_found->mpa_br * (1000 / 8);
     sh_audio->wf->nBlockAlign = mp3_found->mpa_spf;
     sh_audio->wf->wBitsPerSample = 16;
     sh_audio->wf->cbSize = 0;   
@@ -393,7 +441,7 @@
   demux_info_add(demuxer,"Genre",genres[g]);
       }
     }
–    break;
+ } break;
   case WAV: {
     unsigned int chunk_type;
     unsigned int chunk_size;
@@ -691,13 +739,13 @@

static int demux_audio_control(demuxer_t *demuxer,int cmd, void *arg){
     sh_audio_t *sh_audio=demuxer->audio->sh;
–    int audio_length = demuxer->movi_end / sh_audio->i_bps;
+    double audio_length = (double)(demuxer->movi_end-demuxer->movi_start) / (double)sh_audio->i_bps;
     da_priv_t* priv = demuxer->priv;
–    
+
     switch(cmd) {
   case DEMUXER_CTRL_GET_TIME_LENGTH:
      if (audio_length<=0) return DEMUXER_CTRL_DONTKNOW;
–     *((double *)arg)=(double)audio_length;
+     *((double *)arg)=audio_length;
      return DEMUXER_CTRL_GUESS;

   case DEMUXER_CTRL_GET_PERCENT_POS:
diff -ruNa ./MPlayer-1.0rc2/libmpdemux/mp3_hdr.c ./MPlayer-1.0rc2_VBR/libmpdemux/mp3_hdr.c
— ./MPlayer-1.0rc2/libmpdemux/mp3_hdr.c 2009-09-02 18:00:04.000000000 +0800
+++ ./MPlayer-1.0rc2_VBR/libmpdemux/mp3_hdr.c 2009-09-02 17:59:38.000000000 +0800
@@ -34,7 +34,7 @@
/*
* return frame size or -1 (bad frame)
*/
-int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* srate, int* spf, int* mpa_layer, int* br){
+int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* srate, int* spf, int* mpa_layer, int* br, int* side_info_size){
     int stereo,ssize,lsf,framesize,padding,bitrate_index,sampling_frequency, divisor;
     int bitrate;
     int layer, mult[3] = { 12000, 144000, 144000 };
@@ -99,6 +99,7 @@
     else
       ssize = (stereo == 1) ? 17 : 32;
     if(!((newhead>>16)&0x1)) ssize += 2; // CRC
+    if(side_info_size) *side_info_size = ssize;

     bitrate = tabsel_123[lsf][layer-1][bitrate_index];
     framesize = bitrate * mult[layer-1];
diff -ruNa ./MPlayer-1.0rc2/libmpdemux/mp3_hdr.h ./MPlayer-1.0rc2_VBR/libmpdemux/mp3_hdr.h
— ./MPlayer-1.0rc2/libmpdemux/mp3_hdr.h 2009-09-02 18:00:04.000000000 +0800
+++ ./MPlayer-1.0rc2_VBR/libmpdemux/mp3_hdr.h 2009-09-02 17:59:38.000000000 +0800
@@ -1,7 +1,7 @@

-int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* freq, int* spf, int* mpa_layer, int* br);
+int mp_get_mp3_header(unsigned char* hbuf,int* chans, int* freq, int* spf, int* mpa_layer, int* br, int* side_info_size);

-#define mp_decode_mp3_header(hbuf) mp_get_mp3_header(hbuf,NULL,NULL,NULL,NULL,NULL)
+#define mp_decode_mp3_header(hbuf) mp_get_mp3_header(hbuf,NULL,NULL,NULL,NULL,NULL,NULL)

static inline int mp_check_mp3_header(unsigned int head){
     if( (head & 0x0000e0ff) != 0x0000e0ff ||
diff -ruNa ./MPlayer-1.0rc2/libmpdemux/muxer_mpeg.c ./MPlayer-1.0rc2_VBR/libmpdemux/muxer_mpeg.c
— ./MPlayer-1.0rc2/libmpdemux/muxer_mpeg.c 2009-09-02 18:00:03.000000000 +0800
+++ ./MPlayer-1.0rc2_VBR/libmpdemux/muxer_mpeg.c 2009-09-02 17:59:37.000000000 +0800
@@ -2028,7 +2028,7 @@
   {
    if(s->b_buffer[i] == 0xFF && ((s->b_buffer[i+1] & 0xE0) == 0xE0))
    {
–    len = mp_get_mp3_header(&(s->b_buffer[i]), &chans, &srate, &spf, &layer, NULL);
+    len = mp_get_mp3_header(&(s->b_buffer[i]), &chans, &srate, &spf, &layer, NULL, NULL);
     if(len > 0 && (srate == s->wf->nSamplesPerSec) && (i + len <= s->b_buffer_len))
     {
      score[layer]++;
@@ -2081,7 +2081,7 @@
    
      if(s->b_buffer[i] == 0xFF && ((s->b_buffer[i+1] & 0xE0) == 0xE0))
      {
–      len = mp_get_mp3_header(&(s->b_buffer[i]), &chans, &srate, &spf, &layer, NULL);
+      len = mp_get_mp3_header(&(s->b_buffer[i]), &chans, &srate, &spf, &layer, NULL, NULL);
       if(len > 0 && (srate == s->wf->nSamplesPerSec) && (i + len <= s->b_buffer_len)
        && layer == spriv->mpa_layer)
       {

———————————————————–无敌分割线————————————————

另外,由于Mad中,和Mplayer没有匹配好,其在:

libmpcodecsad_libmad.c 中:

static int init(sh_audio_t *sh){
。。。
   sh->i_bps=this->frame.header.bitrate/8;
。。。
}

直接将i_bps,这个之前在Mplayer中已经计算好的,很精确的值,重写了。导致后面的播放显示输入总时长有问题,所以,此处改为:

static int init(sh_audio_t *sh){
。。。
if(sh->i_bps < 0)
   sh->i_bps=this->frame.header.bitrate/8;
。。。
}

以避免重写i_bps。

关于MP3的Xing Tag的具体含义以及如何计算MP3的播放时间,请参考:

1.MPEG Audio Frame Header

http://www.codeproject.com/KB/audio-video/mpegaudioinfo.aspx

里面介绍足够详细了。而且注册后,可以下载代码,具体自己看看就明白了。

2.Xing’s Variable Bitrate MP3 Playback SDK : – VBR Layer 3 – C – 216 k

Xing’s code which allows to seek easily in their variable bitrate files.

http://www.mp3-tech.org/programmer/decoding.html

里面有Xing Tag的解析代码。供参考。

转载请注明:在路上 » 【已解决】Mplayer播放时,显示出的VBR MP3的播放时间不正确,附Mplayer的VBR的Patch

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
53 queries in 0.223 seconds, using 18.85MB memory