3.1 输入函数 input

Shwneo原创首发CSDN技术专栏,转载请注明出处

在压缩包里提供的minimad.c示例参考中给出的input回调函数如下:

/* * 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) { struct buffer *buffer = data; if (!buffer->length) return MAD_FLOW_STOP; mad_stream_buffer(stream, buffer->start, buffer->length); buffer->length = 0; return MAD_FLOW_CONTINUE; }

其中,buffer的类型定义如下:

/* * This is a private message structure. A generic pointer to this structure * is passed to each of the callback functions. Put here any data you need * to access from within the callbacks. */ struct buffer { unsigned char const *start; unsigned long length; };

这个回调函数的原型有两个参数,一个是用户自定义消息指针data,用于输入用户消息(可以包含任意消息,但消息类型必须定义明确);另一个是输入流结构mad_stream,用于输出填充好的输入流,简言之就是将mp3文件的内存镜像传递给mad_stream,这个mad_stream就是mad_decodersync结构的stream成员。

在上面的代码中,input回调函数调用了mad_stream_buffer()函数将文件的内存镜像映射给了mad_streammad_stream_buffer ()函数原型如下:

/* * NAME: stream->buffer() * DESCRIPTION: set stream buffer pointers */ void mad_stream_buffer(struct mad_stream *stream, unsigned char const *buffer, unsigned long length)

第一个参数stream是个输出参数,将input回调函数的第二个虚参数直接传递就行;第二个和第三个参数分别指定mp3文件内存镜像的起始地址和长度,对于MSVC++,可以这样获取这两个参数的值:

CFile file(FILE_PATH); long length=file.GetLength(); char * file_map=(char*)malloc(length); UINT Byte_Readed=1; while (Byte_Readed) { Byte_Readed=file.Read(file_map,length); }

除此之外在input这个回调函数里我们可以做解码开始之前的任何工作比如读取并显示mp3ID3信息,初始化回放增益等。

你可能感兴趣的:(3.1 输入函数 input)