| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- #pragma once
- #include "codec_abstract_codec.h"
- #include "../base/media_common.h"
- #include <mutex>
- #include <memory>
- extern "C" {
- #include <libavcodec/avcodec.h>
- #include <libavutil/channel_layout.h>
- #include <libavutil/opt.h>
- #include <libavformat/avformat.h>
- #include <libswresample/swresample.h>
- }
- namespace av {
- namespace codec {
- /**
- * 音频编码器参数
- */
- struct AudioEncoderParams : public CodecParams {
- int bitRate = 128000; // 比特率 (bps)
- int sampleRate = 44100; // 采样率
- int channels = 2; // 声道数
- AVChannelLayout channelLayout = AV_CHANNEL_LAYOUT_STEREO; // 声道布局
- AVSampleFormat sampleFormat = AV_SAMPLE_FMT_FLTP; // 采样格式
- int frameSize = 1024; // 帧大小
- std::string profile = ""; // 编码配置文件
-
- AudioEncoderParams() {
- type = MediaType::AUDIO;
- codecName = "aac"; // 默认使用AAC编码器
- }
- };
- /**
- * 音频重采样器
- */
- class AudioResampler {
- public:
- AudioResampler();
- ~AudioResampler();
-
- /**
- * 初始化重采样器
- */
- bool init(const AVChannelLayout& srcLayout, AVSampleFormat srcFormat, int srcSampleRate,
- const AVChannelLayout& dstLayout, AVSampleFormat dstFormat, int dstSampleRate);
-
- /**
- * 重采样音频帧
- */
- AVFramePtr resample(const AVFramePtr& srcFrame);
-
- /**
- * 刷新重采样器缓冲区
- */
- std::vector<AVFramePtr> flush();
-
- /**
- * 获取输出帧大小
- */
- int getOutputFrameSize() const { return dstFrameSize_; }
-
- private:
- struct SwrContext* swrCtx_;
- AVChannelLayout srcLayout_;
- AVChannelLayout dstLayout_;
- AVSampleFormat srcFormat_;
- AVSampleFormat dstFormat_;
- int srcSampleRate_;
- int dstSampleRate_;
- int dstFrameSize_;
- AVFramePtr dstFrame_;
- bool initialized_;
- };
- /**
- * 音频编码器实现
- */
- class AudioEncoder : public AbstractEncoder {
- public:
- AudioEncoder();
- ~AudioEncoder() override;
- // AbstractCodec 接口实现
- ErrorCode open(const CodecParams& params) override;
- void close() override;
- ErrorCode flush() override;
- // AbstractEncoder 接口实现
- ErrorCode encode(const AVFramePtr& frame, std::vector<AVPacketPtr>& packets) override;
- ErrorCode finishEncode(std::vector<AVPacketPtr>& packets) override;
- // 音频编码器特有功能
- const AudioEncoderParams& getAudioParams() const { return audioParams_; }
-
- // 获取支持的编码器列表
- static std::vector<std::string> getSupportedEncoders();
-
- // 获取推荐的编码器
- static std::string getRecommendedEncoder();
-
- // 检查编码器是否支持指定的采样格式
- static bool isSampleFormatSupported(const std::string& codecName, AVSampleFormat format);
-
- // 检查编码器是否支持指定的采样率
- static bool isSampleRateSupported(const std::string& codecName, int sampleRate);
-
- // 帧验证工具
- static bool isValidFrame(const AVFramePtr& frame);
- static bool isValidFramePointer(const AVFrame* frame);
-
- // 检查编码器是否支持指定的声道布局
- static bool isChannelLayoutSupported(const std::string& codecName, const AVChannelLayout& layout);
- protected:
- bool validateParams(const CodecParams& params) override;
- private:
- // 初始化编码器
- ErrorCode initEncoder();
-
- // 设置编码器参数
- ErrorCode setupEncoderParams();
-
- // 设置声道布局
- ErrorCode setupChannelLayout();
-
- // 转换音频帧格式
- AVFramePtr convertFrame(const AVFramePtr& frame);
-
- // 帧缓冲和拆分相关方法
- ErrorCode initFrameBuffer();
- ErrorCode addToBuffer(const AVFramePtr& frame);
- AVFramePtr extractFrameFromBuffer();
- void clearBuffer();
-
- // 编码单个帧
- ErrorCode encodeFrame(const AVFramePtr& frame, std::vector<AVPacketPtr>& packets);
-
- // 接收编码后的包
- ErrorCode receivePackets(std::vector<AVPacketPtr>& packets);
-
- // 获取编码器支持的最佳采样格式
- AVSampleFormat getBestSampleFormat() const;
-
- // 获取编码器支持的最佳采样率
- int getBestSampleRate() const;
-
- // 查找可用的编码器
- static void findSupportedEncoders();
- private:
- AudioEncoderParams audioParams_; // 音频编码参数
-
- // 音频重采样
- std::unique_ptr<AudioResampler> resampler_;
- AVFramePtr convertedFrame_; // 转换后的帧
-
- // 帧缓冲机制(用于处理大帧拆分)
- std::vector<uint8_t> frameBuffer_; // 音频数据缓冲区
- int bufferedSamples_; // 缓冲区中的样本数
- AVFramePtr tempFrame_; // 临时帧(用于拆分)
-
- // 线程安全
- std::mutex encodeMutex_;
-
- // 支持的编码器列表
- static std::vector<std::string> supportedEncoders_;
- static std::once_flag encodersInitFlag_;
-
- // 常用音频编码器
- static constexpr const char* AUDIO_ENCODERS[] = {
- "aac",
- "libfdk_aac",
- "mp3",
- "libmp3lame",
- "opus",
- "libopus",
- "vorbis",
- "libvorbis",
- "flac",
- "pcm_s16le",
- "pcm_s24le",
- "pcm_s32le",
- };
- };
- /**
- * 音频编码器工厂
- */
- class AudioEncoderFactory {
- public:
- /**
- * 创建音频编码器
- */
- static std::unique_ptr<AudioEncoder> create(const std::string& codecName = "");
-
- /**
- * 创建最佳音频编码器(自动选择)
- */
- static std::unique_ptr<AudioEncoder> createBest();
-
- /**
- * 创建无损音频编码器
- */
- static std::unique_ptr<AudioEncoder> createLossless();
- };
- } // namespace codec
- } // namespace av
|