codec_audio_encoder.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 isChannelLayoutSupported(const std::string& codecName, const AVChannelLayout& layout);
  93. protected:
  94. bool validateParams(const CodecParams& params) override;
  95. private:
  96. // 初始化编码器
  97. ErrorCode initEncoder();
  98. // 设置编码器参数
  99. ErrorCode setupEncoderParams();
  100. // 设置声道布局
  101. ErrorCode setupChannelLayout();
  102. // 转换音频帧格式
  103. AVFramePtr convertFrame(const AVFramePtr& frame);
  104. // 编码单个帧
  105. ErrorCode encodeFrame(const AVFramePtr& frame, std::vector<AVPacketPtr>& packets);
  106. // 接收编码后的包
  107. ErrorCode receivePackets(std::vector<AVPacketPtr>& packets);
  108. // 获取编码器支持的最佳采样格式
  109. AVSampleFormat getBestSampleFormat() const;
  110. // 获取编码器支持的最佳采样率
  111. int getBestSampleRate() const;
  112. // 查找可用的编码器
  113. static void findSupportedEncoders();
  114. private:
  115. AudioEncoderParams audioParams_; // 音频编码参数
  116. // 音频重采样
  117. std::unique_ptr<AudioResampler> resampler_;
  118. AVFramePtr convertedFrame_; // 转换后的帧
  119. // 线程安全
  120. std::mutex encodeMutex_;
  121. // 支持的编码器列表
  122. static std::vector<std::string> supportedEncoders_;
  123. static std::once_flag encodersInitFlag_;
  124. // 常用音频编码器
  125. static constexpr const char* AUDIO_ENCODERS[] = {
  126. "aac",
  127. "libfdk_aac",
  128. "mp3",
  129. "libmp3lame",
  130. "opus",
  131. "libopus",
  132. "vorbis",
  133. "libvorbis",
  134. "flac",
  135. "pcm_s16le",
  136. "pcm_s24le",
  137. "pcm_s32le",
  138. };
  139. };
  140. /**
  141. * 音频编码器工厂
  142. */
  143. class AudioEncoderFactory {
  144. public:
  145. /**
  146. * 创建音频编码器
  147. */
  148. static std::unique_ptr<AudioEncoder> create(const std::string& codecName = "");
  149. /**
  150. * 创建最佳音频编码器(自动选择)
  151. */
  152. static std::unique_ptr<AudioEncoder> createBest();
  153. /**
  154. * 创建无损音频编码器
  155. */
  156. static std::unique_ptr<AudioEncoder> createLossless();
  157. };
  158. } // namespace codec
  159. } // namespace av