codec_audio_encoder.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #pragma once
  2. #include "codec_abstract_codec.h"
  3. #include "../base/media_common.h"
  4. #include <mutex>
  5. #include <memory>
  6. extern "C" {
  7. #include <libavcodec/avcodec.h>
  8. #include <libavutil/channel_layout.h>
  9. #include <libavutil/opt.h>
  10. #include <libavformat/avformat.h>
  11. #include <libswresample/swresample.h>
  12. }
  13. namespace av {
  14. namespace codec {
  15. /**
  16. * 音频编码器参数
  17. */
  18. struct AudioEncoderParams : public CodecParams {
  19. int bitRate = 128000; // 比特率 (bps)
  20. int sampleRate = 44100; // 采样率
  21. int channels = 2; // 声道数
  22. AVChannelLayout channelLayout = AV_CHANNEL_LAYOUT_STEREO; // 声道布局
  23. AVSampleFormat sampleFormat = AV_SAMPLE_FMT_FLTP; // 采样格式
  24. int frameSize = 1024; // 帧大小
  25. std::string profile = ""; // 编码配置文件
  26. AudioEncoderParams() {
  27. type = MediaType::AUDIO;
  28. codecName = "aac"; // 默认使用AAC编码器
  29. }
  30. };
  31. /**
  32. * 音频重采样器
  33. */
  34. class AudioResampler {
  35. public:
  36. AudioResampler();
  37. ~AudioResampler();
  38. /**
  39. * 初始化重采样器
  40. */
  41. bool init(const AVChannelLayout& srcLayout, AVSampleFormat srcFormat, int srcSampleRate,
  42. const AVChannelLayout& dstLayout, AVSampleFormat dstFormat, int dstSampleRate);
  43. /**
  44. * 重采样音频帧
  45. */
  46. AVFramePtr resample(const AVFramePtr& srcFrame);
  47. /**
  48. * 刷新重采样器缓冲区
  49. */
  50. std::vector<AVFramePtr> flush();
  51. /**
  52. * 获取输出帧大小
  53. */
  54. int getOutputFrameSize() const { return dstFrameSize_; }
  55. private:
  56. struct SwrContext* swrCtx_;
  57. AVChannelLayout srcLayout_;
  58. AVChannelLayout dstLayout_;
  59. AVSampleFormat srcFormat_;
  60. AVSampleFormat dstFormat_;
  61. int srcSampleRate_;
  62. int dstSampleRate_;
  63. int dstFrameSize_;
  64. AVFramePtr dstFrame_;
  65. bool initialized_;
  66. };
  67. /**
  68. * 音频编码器实现
  69. */
  70. class AudioEncoder : public AbstractEncoder {
  71. public:
  72. AudioEncoder();
  73. ~AudioEncoder() override;
  74. // AbstractCodec 接口实现
  75. ErrorCode open(const CodecParams& params) override;
  76. void close() override;
  77. ErrorCode flush() override;
  78. // AbstractEncoder 接口实现
  79. ErrorCode encode(const AVFramePtr& frame, std::vector<AVPacketPtr>& packets) override;
  80. ErrorCode finishEncode(std::vector<AVPacketPtr>& packets) override;
  81. // 音频编码器特有功能
  82. const AudioEncoderParams& getAudioParams() const { return audioParams_; }
  83. // 获取支持的编码器列表
  84. static std::vector<std::string> getSupportedEncoders();
  85. // 获取推荐的编码器
  86. static std::string getRecommendedEncoder();
  87. // 检查编码器是否支持指定的采样格式
  88. static bool isSampleFormatSupported(const std::string& codecName, AVSampleFormat format);
  89. // 检查编码器是否支持指定的采样率
  90. static bool isSampleRateSupported(const std::string& codecName, int sampleRate);
  91. // 帧验证工具
  92. static bool isValidFrame(const AVFramePtr& frame);
  93. static bool isValidFramePointer(const AVFrame* frame);
  94. // 检查编码器是否支持指定的声道布局
  95. static bool isChannelLayoutSupported(const std::string& codecName, const AVChannelLayout& layout);
  96. protected:
  97. bool validateParams(const CodecParams& params) override;
  98. private:
  99. // 初始化编码器
  100. ErrorCode initEncoder();
  101. // 设置编码器参数
  102. ErrorCode setupEncoderParams();
  103. // 设置声道布局
  104. ErrorCode setupChannelLayout();
  105. // 转换音频帧格式
  106. AVFramePtr convertFrame(const AVFramePtr& frame);
  107. // 帧缓冲和拆分相关方法
  108. ErrorCode initFrameBuffer();
  109. ErrorCode addToBuffer(const AVFramePtr& frame);
  110. AVFramePtr extractFrameFromBuffer();
  111. void clearBuffer();
  112. // 编码单个帧
  113. ErrorCode encodeFrame(const AVFramePtr& frame, std::vector<AVPacketPtr>& packets);
  114. // 接收编码后的包
  115. ErrorCode receivePackets(std::vector<AVPacketPtr>& packets);
  116. // 获取编码器支持的最佳采样格式
  117. AVSampleFormat getBestSampleFormat() const;
  118. // 获取编码器支持的最佳采样率
  119. int getBestSampleRate() const;
  120. // 查找可用的编码器
  121. static void findSupportedEncoders();
  122. private:
  123. AudioEncoderParams audioParams_; // 音频编码参数
  124. // 音频重采样
  125. std::unique_ptr<AudioResampler> resampler_;
  126. AVFramePtr convertedFrame_; // 转换后的帧
  127. // 帧缓冲机制(用于处理大帧拆分)
  128. std::vector<uint8_t> frameBuffer_; // 音频数据缓冲区
  129. int bufferedSamples_; // 缓冲区中的样本数
  130. AVFramePtr tempFrame_; // 临时帧(用于拆分)
  131. // 线程安全
  132. std::mutex encodeMutex_;
  133. // 支持的编码器列表
  134. static std::vector<std::string> supportedEncoders_;
  135. static std::once_flag encodersInitFlag_;
  136. // 常用音频编码器
  137. static constexpr const char* AUDIO_ENCODERS[] = {
  138. "aac",
  139. "libfdk_aac",
  140. "mp3",
  141. "libmp3lame",
  142. "opus",
  143. "libopus",
  144. "vorbis",
  145. "libvorbis",
  146. "flac",
  147. "pcm_s16le",
  148. "pcm_s24le",
  149. "pcm_s32le",
  150. };
  151. };
  152. /**
  153. * 音频编码器工厂
  154. */
  155. class AudioEncoderFactory {
  156. public:
  157. /**
  158. * 创建音频编码器
  159. */
  160. static std::unique_ptr<AudioEncoder> create(const std::string& codecName = "");
  161. /**
  162. * 创建最佳音频编码器(自动选择)
  163. */
  164. static std::unique_ptr<AudioEncoder> createBest();
  165. /**
  166. * 创建无损音频编码器
  167. */
  168. static std::unique_ptr<AudioEncoder> createLossless();
  169. };
  170. } // namespace codec
  171. } // namespace av