2023-03-30:用Go语言改写FFmpeg示例decode_audio.c,实现高效音频解码。

2023-06-25,,

2023-03-30:用Go语言改写FFmpeg示例decode_audio.c,实现高效音频解码。

答案2023-03-30:

这个程序的主要功能是将 MP2 音频文件解码为 PCM 格式,并输出到指定的输出文件中。下面是该程序的详细步骤:

1.导入所需的包
通过import语句导入了一些第三方库和FFmpeg相关的包。

2.定义变量
定义了一些必要的变量和常量,如输入和输出文件名、音频编解码器、编解码器上下文、音频解析器上下文、解析缓冲区、音频数据帧、采样格式等。

3.解析命令行参数
读取命令行传入的输入文件名和输出文件名。

4.初始化解析器和编码器
通过 AVCodecFindDecoder() 函数查找 MPEG 音频解码器并得到其指针,如果为空则表示未找到对应的解码器。接着调用 AVParserInit() 函数初始化一个解析器,用于从输入文件中解析出音频数据帧。同时也需要分配一个编解码器上下文(AVCodecContext)对象,并调用 AVCodecOpen2() 函数打开编解码器。

5.打开输入文件和输出文件
使用 os.Open() 函数打开输入文件,如果失败则退出程序。使用 os.Create() 函数创建输出文件,如果失败则需要释放相关资源并退出程序。

6.逐帧解码
循环读取输入文件,每次读取 AUDIO_INBUF_SIZE 大小的数据,然后使用 AVParserParse2() 函数将数据解析成音频数据帧 AVPacket,并调用解码函数 decode() 进行解码,将解码后的 PCM 数据输出到输出文件中。读取结束时需要调用 AVCodecSendPacket() 函数和 AVCodecReceiveFrame() 函数进行“flush”,以确保所有剩余的音频数据帧都被解码。

7.输出 PCM 文件信息
在程序结束前,输出 PCM 文件的格式信息(包括采样率、声道数、采样格式等),以供用户使用 ffplay 命令播放该文件。

8.释放资源
关闭输入文件和输出文件,释放编解码器上下文、解析器上下文、解析缓冲区、音频数据帧以及 AVPacket 等资源。

总体来说,这个程序通过FFmpeg库提供的API从输入文件中逐帧解码音频数据,并将解码后的PCM数据输出到指定的输出文件中。此外,它还提供了一些基本的错误处理和输出格式信息的功能。

执行命令:

./lib/ffmpeg -i ./resources/big_buck_bunny.mp4 -c:a mp2 ./out/big_buck_bunny.mp2

go run ./examples/internalexamples/decode_audio/main.go ./out/big_buck_bunny.mp2 ./out/big_buck_bunny.pcm

./lib/ffplay -f s16le -ac 2 -ar 22050 ./out/big_buck_bunny.pcm

golang代码如下:

package main

import (
"fmt"
"os"
"unsafe" "github.com/moonfdd/ffmpeg-go/ffcommon"
"github.com/moonfdd/ffmpeg-go/libavcodec"
"github.com/moonfdd/ffmpeg-go/libavutil"
) func main0() (ret ffcommon.FInt) {
// ./lib/ffmpeg -i ./resources/big_buck_bunny.mp4 -c:a mp2 ./out/big_buck_bunny.mp2
// go run ./examples/internalexamples/decode_audio/main.go ./out/big_buck_bunny.mp2 ./out/big_buck_bunny.pcm
// ./lib/ffplay -f s16le -ac 2 -ar 22050 ./out/big_buck_bunny.pcm var outfilename, filename string
var codec *libavcodec.AVCodec
var c *libavcodec.AVCodecContext
var parser *libavcodec.AVCodecParserContext
var len0 ffcommon.FInt
var f, outfile *os.File
var inbuf [AUDIO_INBUF_SIZE + libavcodec.AV_INPUT_BUFFER_PADDING_SIZE]ffcommon.FUint8T
var data *ffcommon.FUint8T
var data_size ffcommon.FSizeT
var pkt *libavcodec.AVPacket
var decoded_frame *libavutil.AVFrame
var sfmt libavutil.AVSampleFormat
var n_channels ffcommon.FInt = 0
var fmt0 string if len(os.Args) <= 2 {
fmt.Printf("Usage: %s <input file> <output file>\n", os.Args[0])
os.Exit(0)
}
filename = os.Args[1]
outfilename = os.Args[2] pkt = libavcodec.AvPacketAlloc() /* find the MPEG audio decoder */
codec = libavcodec.AvcodecFindDecoder(libavcodec.AV_CODEC_ID_MP2)
if codec == nil {
fmt.Printf("Codec not found\n")
os.Exit(1)
} parser = libavcodec.AvParserInit(int32(codec.Id))
if parser == nil {
fmt.Printf("Parser not found\n")
os.Exit(1)
} c = codec.AvcodecAllocContext3()
if c == nil {
fmt.Printf("Could not allocate audio codec context\n")
os.Exit(1)
} /* open it */
if c.AvcodecOpen2(codec, nil) < 0 {
fmt.Printf("Could not open codec\n")
os.Exit(1)
} var err error
f, err = os.Open(filename)
if err != nil {
fmt.Printf("Could not open %s\n", filename)
os.Exit(1)
} outfile, err = os.Create(outfilename)
if err != nil {
libavutil.AvFree(uintptr(unsafe.Pointer(c)))
os.Exit(1)
} /* decode until eof */
data = (*byte)(unsafe.Pointer(&inbuf))
var n int
n, _ = f.Read(inbuf[0:AUDIO_INBUF_SIZE])
data_size = uint64(n) for data_size > 0 {
if decoded_frame == nil {
decoded_frame = libavutil.AvFrameAlloc()
if decoded_frame == nil {
fmt.Printf("Could not allocate audio frame\n")
os.Exit(1)
}
} ret = parser.AvParserParse2(c, &pkt.Data, (*int32)(unsafe.Pointer(&pkt.Size)),
data, int32(data_size),
libavutil.AV_NOPTS_VALUE, libavutil.AV_NOPTS_VALUE, 0)
if ret < 0 {
fmt.Printf("Error while parsing\n")
os.Exit(1)
}
data = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(data)) + uintptr(ret)))
data_size -= uint64(ret) if pkt.Size != 0 {
decode(c, pkt, decoded_frame, outfile)
} if data_size < AUDIO_REFILL_THRESH {
for i := uint64(0); i < data_size; i++ {
inbuf[i] = *(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(data)) + uintptr(i)))
}
data = (*byte)(unsafe.Pointer(&inbuf))
n, _ = f.Read(inbuf[data_size:AUDIO_INBUF_SIZE])
len0 = int32(n)
if len0 > 0 {
data_size += uint64(len0)
}
}
} /* flush the decoder */
pkt.Data = nil
pkt.Size = 0
decode(c, pkt, decoded_frame, outfile) /* print output pcm infomations, because there have no metadata of pcm */
sfmt = c.SampleFmt if libavutil.AvSampleFmtIsPlanar(sfmt) != 0 {
packed := libavutil.AvGetSampleFmtName(sfmt)
pa := ""
if packed == "" {
pa = "?"
} else {
pa = packed
}
fmt.Printf("Warning: the sample format the decoder produced is planar (%s). This example will output the first channel only.\n", pa)
sfmt = libavutil.AvGetPackedSampleFmt(sfmt)
} n_channels = c.Channels
for {
ret = get_format_from_sample_fmt(&fmt0, sfmt)
if ret < 0 {
break
} fmt.Printf("Play the output audio file with the command:\nffplay -f %s -ac %d -ar %d %s\n",
fmt0, n_channels, c.SampleRate,
outfilename)
break
}
// end:
outfile.Close()
f.Close() libavcodec.AvcodecFreeContext(&c)
parser.AvParserClose()
libavutil.AvFrameFree(&decoded_frame)
libavcodec.AvPacketFree(&pkt) return 0
} const AUDIO_INBUF_SIZE = 20480
const AUDIO_REFILL_THRESH = 4096 func get_format_from_sample_fmt(fmt0 *string, sample_fmt libavutil.AVSampleFormat) (ret ffcommon.FInt) {
switch sample_fmt {
case libavutil.AV_SAMPLE_FMT_U8:
*fmt0 = "u8"
case libavutil.AV_SAMPLE_FMT_S16:
*fmt0 = "s16le"
case libavutil.AV_SAMPLE_FMT_S32:
*fmt0 = "s32le"
case libavutil.AV_SAMPLE_FMT_FLT:
*fmt0 = "f32le"
case libavutil.AV_SAMPLE_FMT_DBL:
*fmt0 = "f64le"
default:
fmt.Printf("sample format %s is not supported as output format\n",
libavutil.AvGetSampleFmtName(sample_fmt))
ret = -1
}
return
} func decode(dec_ctx *libavcodec.AVCodecContext, pkt *libavcodec.AVPacket, frame *libavutil.AVFrame, outfile *os.File) {
var i, ch ffcommon.FInt
var ret, data_size ffcommon.FInt /* send the packet with the compressed data to the decoder */
ret = dec_ctx.AvcodecSendPacket(pkt)
if ret < 0 {
fmt.Printf("Error submitting the packet to the decoder\n")
os.Exit(1)
} /* read all the output frames (in general there may be any number of them */
for ret >= 0 {
ret = dec_ctx.AvcodecReceiveFrame(frame)
if ret == -libavutil.EAGAIN || ret == libavutil.AVERROR_EOF {
return
} else if ret < 0 {
fmt.Printf("Error during decoding\n")
os.Exit(1)
}
data_size = libavutil.AvGetBytesPerSample(dec_ctx.SampleFmt)
if data_size < 0 {
/* This should not occur, checking just for paranoia */
fmt.Printf("Failed to calculate data size\n")
os.Exit(1)
}
bytes := []byte{}
for i = 0; i < frame.NbSamples; i++ {
for ch = 0; ch < dec_ctx.Channels; ch++ {
ptr := uintptr(unsafe.Pointer(frame.Data[ch])) + uintptr(data_size*i)
for k := int32(0); k < data_size; k++ {
bytes = append(bytes, *(*byte)(unsafe.Pointer(ptr)))
ptr++
}
}
}
outfile.Write(bytes)
}
} func main() {
os.Setenv("Path", os.Getenv("Path")+";./lib")
ffcommon.SetAvutilPath("./lib/avutil-56.dll")
ffcommon.SetAvcodecPath("./lib/avcodec-58.dll")
ffcommon.SetAvdevicePath("./lib/avdevice-58.dll")
ffcommon.SetAvfilterPath("./lib/avfilter-56.dll")
ffcommon.SetAvformatPath("./lib/avformat-58.dll")
ffcommon.SetAvpostprocPath("./lib/postproc-55.dll")
ffcommon.SetAvswresamplePath("./lib/swresample-3.dll")
ffcommon.SetAvswscalePath("./lib/swscale-5.dll") genDir := "./out"
_, err := os.Stat(genDir)
if err != nil {
if os.IsNotExist(err) {
os.Mkdir(genDir, 0777) // Everyone can read write and execute
}
} main0()
}

2023-03-30:用Go语言改写FFmpeg示例decode_audio.c,实现高效音频解码。的相关教程结束。

《2023-03-30:用Go语言改写FFmpeg示例decode_audio.c,实现高效音频解码。.doc》

下载本文的Word格式文档,以方便收藏与打印。