基于libmad 的简单MP3流媒体播放器的实现

来源: 作者: 2007-10-24 出处:pcdog.com

access  apache  linux  错误代码  数据结构  
上一页 1 2 3 4 5 6 7 下一页 

    如果缓冲区最后一个 MPEG 数据帧只有部分数据包括在缓冲区中,那么 struct mad_stream 中的 next_frame 域指到不完整数据的开始地址。

由于缓冲区的 MPEG 数据帧不一定完整,所以不完整的 MPEG 帧的数据必须拷贝到下一次解码操作的缓冲区中,进行再次解码。这里我们还看到 bufend 指向缓冲区数据的最后地址,也就是最后一字节的地址加 1 的位置。mad_stream.bufend – mad_stream.next_frame 就是剩余的未被解码的 MPEG 帧的数据的字节数量(假设此帧在缓冲区中不完整)。mad_streamerror 域用来记录操作 mad_stream 得到的错误代码。错误代码在 mad.h 中有很详细的定义。


清单 2:错误代码在 mad.h 中的详细定义

struct mad_synth {
  mad_fixed_t filter[2][2][2][16][8]; /* polyphase filterbank outputs */
                              /* [ch][eo][peo][s][v] */
  unsigned int phase;             /* current processing phase */
  struct mad_pcm pcm;           /* PCM output */
}; 
      

mad_synth 中的关键域 pcm 保存解码和合成后得到的 PCM 数据。


清单 3:mad_synth 中的关键域

struct mad_pcm {
  unsigned int samplerate;        /* sampling frequency (Hz) */
  unsigned short channels;        /* number of channels */
  unsigned short length;          /* number of samples per channel */
  mad_fixed_t samples[2][1152];   /* PCM output samples [ch][sample] */
};
      

struct mad_pcm 定义了音频的采样率、每个声道个数以及最后的 PCM 采样数据。这些参数可用来初始化音频设备。


清单 4:struct mad_pcm

struct mad_frame {
  struct mad_header header;              /* MPEG audio header */
  int options;                          /* decoding options (from stream) */
  mad_fixed_t sbsample[2][36][32];       /* synthesis subband filter samples */
  mad_fixed_t (*overlap)[2][32][18];      /* Layer III block overlap data */
};
      

mad_frame 是记录 MPEG 帧解码后的数据的数据结构,其中的 mad_header 尤其重要,其用来记录 MPEG 帧的一些基本信息,比如 MPEG 层数、声道模式、流比特率、采样比特率等等。声道模式包括单声道、双声道、联合立体混音声以及一般立体声。


清单 5:mad_frame

enum mad_mode {
  MAD_MODE_SINGLE_CHANNEL = 0,          /* single channel */
  MAD_MODE_DUAL_CHANNEL   = 1,     /* dual channel */
  MAD_MODE_JOINT_STEREO   = 2,           /* joint (MS/intensity) stereo */
  MAD_MODE_STEREO   = 3                  /* normal LR stereo */
};
struct mad_header {
  enum mad_layer layer;         /* audio layer (1, 2, or 3) */
  enum mad_mode mode;        /* channel mode  */
  int mode_extension;           /* additional mode info */
  enum mad_emphasis emphasis;  /* de-emphasis to use  */
  unsigned long bitrate;          /* stream bitrate (bps) */
  unsigned int samplerate;        /* sampling frequency (Hz) */
  unsigned short crc_check;       /* frame CRC accumulator */
  unsigned short crc_target;       /* final target CRC checksum */
  int flags;                     /* flags  */
  int private_bits;   /* private bits  */
  mad_timer_t duration;  /* audio playing time of frame */
};
      

下面就本文使用的 API 的功能做简单介绍。

在本文中用到的 API 包括:

void mad_stream_init(struct mad_stream *) 
void mad_synth_init(struct mad_synth *);
void mad_frame_init(struct mad_frame *);
      

以上3个 API 初始化解码需要的数据结构。

void mad_stream_buffer(struct mad_stream *, unsigned char const *, unsigned long);
      

此函数把原始的未解码的 MPEG 数据和 mad_stream 数据结构关联,以便使用 mad_frame_decode( ) 来解码 MPEG 帧数据。

int mad_frame_decode(struct mad_frame *, struct mad_stream *);
      

mad_stream 中的 MPEG 帧数据解码。

void mad_synth_frame(struct mad_synth *, struct mad_frame const *);
      

把解码后的音频数据合成 PCM 采样。

void mad_stream_finish(struct mad_stream *);
void mad_frame_finish(struct mad_frame *); 
mad_synth_finish(struct mad_synth);
      

以上 3 个 API 在解码完毕后使用,释放 libmad 占用的资源等。

 


更多内容请看PCdog.com--流媒体文件格式播放技巧  网吧流媒体服务器专题
上一页 1 2 3 4 5 6 7 下一页 
上一篇:离线Flash Adobe发布新媒体播放器
下一篇:Ubuntu Linux下播放Real多媒体的方法