2023-03-30:用Go語言改寫FFmpeg示例decode_audio.c,實現(xiàn)高效音頻解碼。
答案2023-03-30:
這個程序的主要功能是將 MP2 音頻文件解碼為 PCM 格式,并輸出到指定的輸出文件中。下面是該程序的詳細步驟:
1.導入所需的包
通過import語句導入了一些第三方庫和FFmpeg相關的包。
2.定義變量
定義了一些必要的變量和常量,如輸入和輸出文件名、音頻編解碼器、編解碼器上下文、音頻解析器上下文、解析緩沖區(qū)、音頻數(shù)據(jù)幀、采樣格式等。
3.解析命令行參數(shù)
讀取命令行傳入的輸入文件名和輸出文件名。
4.初始化解析器和編碼器
通過 AVCodecFindDecoder() 函數(shù)查找 MPEG 音頻解碼器并得到其指針,如果為空則表示未找到對應的解碼器。接著調用 AVParserInit() 函數(shù)初始化一個解析器,用于從輸入文件中解析出音頻數(shù)據(jù)幀。同時也需要分配一個編解碼器上下文(AVCodecContext)對象,并調用 AVCodecOpen2() 函數(shù)打開編解碼器。
5.打開輸入文件和輸出文件
使用 os.Open() 函數(shù)打開輸入文件,如果失敗則退出程序。使用 os.Create() 函數(shù)創(chuàng)建輸出文件,如果失敗則需要釋放相關資源并退出程序。
6.逐幀解碼
循環(huán)讀取輸入文件,每次讀取 AUDIO_INBUF_SIZE 大小的數(shù)據(jù),然后使用 AVParserParse2() 函數(shù)將數(shù)據(jù)解析成音頻數(shù)據(jù)幀 AVPacket,并調用解碼函數(shù) decode() 進行解碼,將解碼后的 PCM 數(shù)據(jù)輸出到輸出文件中。讀取結束時需要調用 AVCodecSendPacket() 函數(shù)和 AVCodecReceiveFrame() 函數(shù)進行“flush”,以確保所有剩余的音頻數(shù)據(jù)幀都被解碼。
7.輸出 PCM 文件信息
在程序結束前,輸出 PCM 文件的格式信息(包括采樣率、聲道數(shù)、采樣格式等),以供用戶使用 ffplay 命令播放該文件。
8.釋放資源
關閉輸入文件和輸出文件,釋放編解碼器上下文、解析器上下文、解析緩沖區(qū)、音頻數(shù)據(jù)幀以及 AVPacket 等資源。
總體來說,這個程序通過FFmpeg庫提供的API從輸入文件中逐幀解碼音頻數(shù)據(jù),并將解碼后的PCM數(shù)據(jù)輸出到指定的輸出文件中。此外,它還提供了一些基本的錯誤處理和輸出格式信息的功能。
執(zhí)行命令:
./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()
}
