常用的开源MP3编解码器

Posted ZC_L

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常用的开源MP3编解码器相关的知识,希望对你有一定的参考价值。

前言

由于工作需要,需对MP3进行编解码处理,研究了几款开源的MP3编解码器。相对于FFMPEG来说,这几款都属于轻量级的编解码器,更容易移植。

LAME

源码下载链接:https://sourceforge.net/projects/lame/
支持MP3编解码。编码出来的MP3音色纯厚、空间宽广、低音清晰、细节表现良好,它独创的心理音响模型技术保证了CD音频还原的真实性,配合VBR和ABR参数,音质几乎可以媲美CD音频,但文件体积却非常小,是目前主流的编码器。

MAD

MAD(libmad)是一个开源的高精度MPEG音频解码库,支持MPEG-1标准。libmad提供24-bit的PCM输出,完全定点计算,非常适合在没有浮点支持的嵌入式硬件平台上使用。使用libmad提供的一系列API可以实现MP3文件的解码。
源码下载链接:https://sourceforge.net/projects/mad/
例程minimad.c是在运行前将整个MP3文件读入内存中进行处理,不适合MP3流未知的场景,需改成边解码边写入MP3的形式,即每次读入1K MP3数据,解码完成再读入1K,又不影响播放的连续性,方便在资源紧张的嵌入式系统中运用。
libmad中的mad_decoder_run()进行解码时,首先会检测待解码缓冲区中是否存在数据,有则解码,没有则调用input()函数进行装载数据,并返回MAD_FLOW_CONTINUE表示还存在数据,解码完成后调用output()函数进行处理,如此循环…直到input()函数返回MAD_FLOW_STOP表示该MP3数据流已经完全加载,output()函数输出后,表示该MP3文件已完成全部解码操作。
input()函数如下,每次调用读入FRAME_SIZE_MP3字节数据:

static
enum mad_flow input(void *data,
		    struct mad_stream *stream)

  PT_Mp3Info ptMp3Info = (PT_Mp3Info)data;
  int ret;
  int restLen;   // unprocessed data's size
  int readLen;

  if (!feof(fin)) 
    restLen = stream->bufend - stream->next_frame;
    memcpy(ptMp3Info->inMp3, ptMp3Info->inMp3+ptMp3Info->inLen-restLen, restLen);
    readLen = FRAME_SIZE_MP3 - restLen;
    int readn = fread(ptMp3Info->inMp3+restLen, sizeof(char), readLen, fin);
    ptMp3Info->inLen = restLen + readn;
    mad_stream_buffer(stream, ptMp3Info->inMp3, ptMp3Info->inLen);
    ret = MAD_FLOW_CONTINUE;
  
  else 
    ret = MAD_FLOW_STOP;
  

  return ret;

完整代码如下:


#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/mman.h>

#include "mad.h"

#define FRAME_SIZE_MP3  (1024)

typedef struct _Mp3Info 
  unsigned char inMp3[FRAME_SIZE_MP3];
  unsigned int  inLen;
T_Mp3Info, *PT_Mp3Info;

static FILE *fin  = NULL;
static FILE *fout = NULL;
static int decode(PT_Mp3Info ptMp3Info);


int main(int argc, char *argv[])

  if (argc != 3) 
    printf("%s <inMp3> <outPcm>\\n", argv[0]);
    return -1;
  
  
  fin  = fopen(argv[1], "r");
  fout = fopen(argv[2], "wb+");

  T_Mp3Info tMp3Info; 
  decode(&tMp3Info);

  fclose(fin);
  fclose(fout);

  return 0;



/*
 * This is the input callback. The purpose of this callback is to (re)fill
 * the stream buffer which is to be decoded. In this example, an entire file
 * has been mapped into memory, so we just call mad_stream_buffer() with the
 * address and length of the mapping. When this callback is called a second
 * time, we are finished decoding.
 */

static
enum mad_flow input(void *data,
		    struct mad_stream *stream)

  PT_Mp3Info ptMp3Info = (PT_Mp3Info)data;
  int ret;
  int restLen;   // unprocessed data's size
  int readLen;

  if (!feof(fin)) 
    restLen = stream->bufend - stream->next_frame;
    memcpy(ptMp3Info->inMp3, ptMp3Info->inMp3+ptMp3Info->inLen-restLen, restLen);
    readLen = FRAME_SIZE_MP3 - restLen;
    int readn = fread(ptMp3Info->inMp3+restLen, sizeof(char), readLen, fin);
    ptMp3Info->inLen = restLen + readn;
    mad_stream_buffer(stream, ptMp3Info->inMp3, ptMp3Info->inLen);
    ret = MAD_FLOW_CONTINUE;
  
  else 
    ret = MAD_FLOW_STOP;
  

  return ret;


/*
 * The following utility routine performs simple rounding, clipping, and
 * scaling of MAD's high-resolution samples down to 16 bits. It does not
 * perform any dithering or noise shaping, which would be recommended to
 * obtain any exceptional audio quality. It is therefore not recommended to
 * use this routine if high-quality output is desired.
 */

static inline
signed int scale(mad_fixed_t sample)

  /* round */
  sample += (1L << (MAD_F_FRACBITS - 16));

  /* clip */
  if (sample >= MAD_F_ONE)
    sample = MAD_F_ONE - 1;
  else if (sample < -MAD_F_ONE)
    sample = -MAD_F_ONE;

  /* quantize */
  return sample >> (MAD_F_FRACBITS + 1 - 16);


/*
 * This is the output callback function. It is called after each frame of
 * MPEG audio data has been completely decoded. The purpose of this callback
 * is to output (or play) the decoded PCM audio.
 */

static
enum mad_flow output(void *data,
		     struct mad_header const *header,
		     struct mad_pcm *pcm)

  unsigned int nchannels, nsamples;
  mad_fixed_t const *left_ch, *right_ch;

  /* pcm->samplerate contains the sampling frequency */

  nchannels = pcm->channels;
  nsamples  = pcm->length;
  left_ch   = pcm->samples[0];
  right_ch  = pcm->samples[1];

  while (nsamples--) 
    signed int sample;

    /* output sample(s) in 16-bit signed little-endian PCM */

    sample = scale(*left_ch++);
    char high = (sample >> 0) & 0xff;
    char low  = (sample >> 8) & 0xff;
//    putchar((sample >> 0) & 0xff);
//    putchar((sample >> 8) & 0xff);
    fwrite(&high, sizeof(char), 1, fout);
    fwrite(&low, sizeof(char), 1, fout);

    if (nchannels == 2) 
      sample = scale(*right_ch++);
//      putchar((sample >> 0) & 0xff);
//      putchar((sample >> 8) & 0xff);
      high = (sample >> 0) & 0xff;
      low  = (sample >> 8) & 0xff;
      fwrite(&high, sizeof(char), 1, fout);
      fwrite(&low, sizeof(char), 1, fout);
    
    
  

  return MAD_FLOW_CONTINUE;


/*
 * This is the error callback function. It is called whenever a decoding
 * error occurs. The error is indicated by stream->error; the list of
 * possible MAD_ERROR_* errors can be found in the mad.h (or stream.h)
 * header file.
 */

static
enum mad_flow error(void *data,
		    struct mad_stream *stream,
		    struct mad_frame *frame)

  PT_Mp3Info ptMp3Info = (PT_Mp3Info)data;

  fprintf(stderr, "decoding error 0x%04x (%s) at byte offset %lu\\n",
	  stream->error, mad_stream_errorstr(stream),
	  stream->this_frame - ptMp3Info->inMp3);

  /* return MAD_FLOW_BREAK here to stop decoding (and propagate an error) */

  return MAD_FLOW_CONTINUE;


/*
 * This is the function called by main() above to perform all the decoding.
 * It instantiates a decoder object and configures it with the input,
 * output, and error callback functions above. A single call to
 * mad_decoder_run() continues until a callback function returns
 * MAD_FLOW_STOP (to stop decoding) or MAD_FLOW_BREAK (to stop decoding and
 * signal an error).
 */

static
int decode(PT_Mp3Info ptMp3Info)

  struct mad_decoder decoder;
  int result;

  if (ptMp3Info == NULL) 
    printf("ptMp3Info is NULL\\n");
    return -1;
  

  /* configure input, output, and error functions */

  mad_decoder_init(&decoder, ptMp3Info,
		   input, 0 /* header */, 0 /* filter */, output,
		   error, 0 /* message */);

  /* start decoding */

  result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);

  /* release the decoder */

  mad_decoder_finish(&decoder);

  return result;


tinymp3

支持MP3编解码,代码量少,适合在单片机上移植。
源码下载链接:https://github.com/cpuimage/tinymp3

minimp3

仅支持MP3解码,只有一个头文件,适合在单片机上移植。
源码下载链接:https://github.com/lieff/minimp3
minimp3的使用只需调用一个函数即可实现解码

int mp3dec_decode_frame(mp3dec_t *dec, const uint8_t *mp3, int mp3_bytes, mp3d_sample_t *pcm, mp3dec_frame_info_t *info);

消耗的 MP3 数据的大小在定义的mp3dec_frame_info_t结构中的frame_bytes字段中返回,必须在下一次解码器调用之前从输入缓冲区中删除对应于 frame_bytes 字段的数据。
解码函数返回已解码样本的数量samples。可能出现以下情况:
0: 在输入缓冲区中未找到 MP3 数据
384: Layer 1
576: MPEG 2 Layer 3
1152: Otherwise

samples 和 frame_bytes 字段值:
samples > 0 和 frame_bytes > 0: 成功解码
samples == 0 和 frame_bytes > 0: 解码器跳过了 ID3 或无效数据
samples == 0 和 frame_bytes == 0: 数据不足

参考代码如下:

#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>


#define MINIMP3_IMPLEMENTATION 
#include "minimp3.h"

int main(int argc, char *argv[])

	unsigned char *inMp3 = NULL;
	int totalLen = 0;
	
	if (argc != 3) 
		printf("%s <inMp3> <outPcm>\\n", argv[0]);
		return -1;
	
	//打开MP3文件
	FILE* fin = fopen(argv[1], "r");

	//获取MP3文件长度
	fseek(fin, 0, SEEK_END);
	totalLen = (int)ftell(fin);

	//读取整个MP3文件
	fseek(fin, 0, SEEK_SET);
	inMp3 = malloc(totalLen);
	fread(inMp3, 1, totalLen, fin);
    fclose(fin);
	
	//定义mp3dec_frame_info_t
	mp3dec_frame_info_t info;
	short outPcm[MINIMP3_MAX_SAMPLES_PER_FRAME];
	int inLen = 0;

	//逐帧解码
	int samples = mp3dec_decode_frame(&mp3d, inMp3, totalLen, outPcm, &info);
	while(samples) 
		fwrite(outPcm, sizeof(short), samples, fout);
		inLen += info.frame_bytes;
		samples = mp3dec_decode_frame(&mp3d, inMp3 + inLen, totalLen - inLen, outPcm, &info);
	
    
	free(inMp3);
	inMp3 = NULL;
	
	fclose(fout);
	
	return 0;


OpenCV中的mp4视频编解码器

OpenCV 4.1.1.26中mp4的视频编解码器是什么?

[我正在尝试从两台摄像机编写视频,编写AVI视频可以正常工作,但不能编写mp4视频。

  • Python 3.7
  • OpenCV 4.1.1.26
  • Windows 10

下面是一台摄像机的示例代码

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('output_1.mp4', fourcc, 15, (640, 480))

我使用了不同的编解码器(MPEG,H264,mp4v,('m','p','4','v'),FFmpeg等),如OpenCV 4 DocumentationsFOURCC Codecs Documentations所示,并由不同的专家和专家建议平台上的帮助程序,但是任何解决方案都无法在我的代码中正常工作,mp4视频已保存,但由于编解码器存在问题,即使在OpenCV VideoCapture中也无法在任何媒体播放器中播放。另一方面,当我保存AVI视频时,它可以正常工作。我检查了这些链接以获得帮助。

what is the codec for mp4 videos in python OpenCV

https://medium.com/@iKhushPatel/convert-video-to-images-images-to-video-using-opencv-python-db27a128a481

How to write mp4 video file with H264 codec?

正在寻找有价值的建议。

答案

我也有类似的问题。我发现的是,OpenCV-python通常会在mp4文件中遇到一些意外错误(无法从mp4文件中读取帧等)。

为了解决此问题,我所做的是使用skvideo(scikit-video)重写代码。 scikit-video使用命令行FFmpeg工具,因此在使用scikit-video之前,您需要安装FFmpeg。

以上是关于常用的开源MP3编解码器的主要内容,如果未能解决你的问题,请参考以下文章

技术案例Firefly-RK3399多路视频编解码

鸿蒙开源全场景应用开发——视频编解码

常用视频格式与视频编解码标准介绍 转

iOS视频播放常用重点知识

FFmpeg库视频解码初探(软硬件解码)

走进音视频的世界——Opus编解码协议