codec_audio_encoder.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. #include "codec_audio_encoder.h"
  2. #include "../base/logger.h"
  3. #include <algorithm>
  4. #include <chrono>
  5. namespace av {
  6. namespace codec {
  7. // 静态成员初始化
  8. std::vector<std::string> AudioEncoder::supportedEncoders_;
  9. std::once_flag AudioEncoder::encodersInitFlag_;
  10. // AudioResampler 实现
  11. AudioResampler::AudioResampler()
  12. : swrCtx_(nullptr)
  13. , srcFormat_(AV_SAMPLE_FMT_NONE)
  14. , dstFormat_(AV_SAMPLE_FMT_NONE)
  15. , srcSampleRate_(0)
  16. , dstSampleRate_(0)
  17. , dstFrameSize_(0)
  18. , initialized_(false) {
  19. av_channel_layout_default(&srcLayout_, 0);
  20. av_channel_layout_default(&dstLayout_, 0);
  21. }
  22. AudioResampler::~AudioResampler() {
  23. if (swrCtx_) {
  24. swr_free(&swrCtx_);
  25. }
  26. av_channel_layout_uninit(&srcLayout_);
  27. av_channel_layout_uninit(&dstLayout_);
  28. }
  29. bool AudioResampler::init(const AVChannelLayout& srcLayout, AVSampleFormat srcFormat, int srcSampleRate,
  30. const AVChannelLayout& dstLayout, AVSampleFormat dstFormat, int dstSampleRate) {
  31. if (swrCtx_) {
  32. swr_free(&swrCtx_);
  33. }
  34. av_channel_layout_uninit(&srcLayout_);
  35. av_channel_layout_uninit(&dstLayout_);
  36. // 复制声道布局
  37. if (av_channel_layout_copy(&srcLayout_, &srcLayout) < 0 ||
  38. av_channel_layout_copy(&dstLayout_, &dstLayout) < 0) {
  39. AV_LOGGER_ERROR("复制声道布局失败");
  40. return false;
  41. }
  42. srcFormat_ = srcFormat;
  43. dstFormat_ = dstFormat;
  44. srcSampleRate_ = srcSampleRate;
  45. dstSampleRate_ = dstSampleRate;
  46. // 创建重采样上下文
  47. int ret = swr_alloc_set_opts2(&swrCtx_,
  48. &dstLayout_, dstFormat_, dstSampleRate_,
  49. &srcLayout_, srcFormat_, srcSampleRate_,
  50. 0, nullptr);
  51. if (ret < 0) {
  52. AV_LOGGER_ERRORF("创建重采样上下文失败: {}", ffmpeg_utils::errorToString(ret));
  53. return false;
  54. }
  55. // 初始化重采样器
  56. ret = swr_init(swrCtx_);
  57. if (ret < 0) {
  58. AV_LOGGER_ERRORF("初始化重采样器失败: {}", ffmpeg_utils::errorToString(ret));
  59. swr_free(&swrCtx_);
  60. return false;
  61. }
  62. // 计算输出帧大小
  63. dstFrameSize_ = av_rescale_rnd(1024, dstSampleRate_, srcSampleRate_, AV_ROUND_UP);
  64. // 创建输出帧
  65. dstFrame_ = makeAVFrame();
  66. if (!dstFrame_) {
  67. AV_LOGGER_ERROR("分配输出帧失败");
  68. return false;
  69. }
  70. dstFrame_->format = dstFormat_;
  71. dstFrame_->sample_rate = dstSampleRate_;
  72. av_channel_layout_copy(&dstFrame_->ch_layout, &dstLayout_);
  73. dstFrame_->nb_samples = dstFrameSize_;
  74. ret = av_frame_get_buffer(dstFrame_.get(), 0);
  75. if (ret < 0) {
  76. AV_LOGGER_ERRORF("分配帧缓冲区失败: {}", ffmpeg_utils::errorToString(ret));
  77. return false;
  78. }
  79. initialized_ = true;
  80. return true;
  81. }
  82. AVFramePtr AudioResampler::resample(const AVFramePtr& srcFrame) {
  83. if (!initialized_ || !srcFrame) {
  84. return nullptr;
  85. }
  86. // 计算输出样本数
  87. int dstSamples = av_rescale_rnd(srcFrame->nb_samples, dstSampleRate_, srcSampleRate_, AV_ROUND_UP);
  88. // 确保输出帧有足够的空间
  89. if (dstFrame_->nb_samples < dstSamples) {
  90. av_frame_unref(dstFrame_.get());
  91. dstFrame_->nb_samples = dstSamples;
  92. int ret = av_frame_get_buffer(dstFrame_.get(), 0);
  93. if (ret < 0) {
  94. AV_LOGGER_ERRORF("重新分配帧缓冲区失败: {}", ffmpeg_utils::errorToString(ret));
  95. return nullptr;
  96. }
  97. }
  98. // 执行重采样
  99. int convertedSamples = swr_convert(swrCtx_,
  100. dstFrame_->data, dstSamples,
  101. const_cast<const uint8_t**>(srcFrame->data), srcFrame->nb_samples);
  102. if (convertedSamples < 0) {
  103. AV_LOGGER_ERRORF("音频重采样失败: {}", ffmpeg_utils::errorToString(convertedSamples));
  104. return nullptr;
  105. }
  106. dstFrame_->nb_samples = convertedSamples;
  107. // 复制时间戳等信息
  108. av_frame_copy_props(dstFrame_.get(), srcFrame.get());
  109. // 调整时间戳
  110. if (srcFrame->pts != AV_NOPTS_VALUE) {
  111. dstFrame_->pts = av_rescale_q(srcFrame->pts, {1, srcSampleRate_}, {1, dstSampleRate_});
  112. }
  113. return std::move(dstFrame_);
  114. }
  115. std::vector<AVFramePtr> AudioResampler::flush() {
  116. std::vector<AVFramePtr> frames;
  117. if (!initialized_) {
  118. return frames;
  119. }
  120. while (true) {
  121. AVFramePtr frame = makeAVFrame();
  122. if (!frame) {
  123. break;
  124. }
  125. frame->format = dstFormat_;
  126. frame->sample_rate = dstSampleRate_;
  127. av_channel_layout_copy(&frame->ch_layout, &dstLayout_);
  128. frame->nb_samples = dstFrameSize_;
  129. int ret = av_frame_get_buffer(frame.get(), 0);
  130. if (ret < 0) {
  131. break;
  132. }
  133. int convertedSamples = swr_convert(swrCtx_,
  134. frame->data, dstFrameSize_,
  135. nullptr, 0);
  136. if (convertedSamples <= 0) {
  137. break;
  138. }
  139. frame->nb_samples = convertedSamples;
  140. frames.push_back(std::move(frame));
  141. }
  142. return frames;
  143. }
  144. // AudioEncoder 实现
  145. AudioEncoder::AudioEncoder()
  146. : AbstractEncoder(MediaType::AUDIO) {
  147. AV_LOGGER_DEBUG("创建音频编码器");
  148. }
  149. AudioEncoder::~AudioEncoder() {
  150. close();
  151. AV_LOGGER_DEBUG("音频编码器已销毁");
  152. }
  153. ErrorCode AudioEncoder::open(const CodecParams& params) {
  154. std::lock_guard<std::mutex> lock(encodeMutex_);
  155. if (state_ != CodecState::IDLE && state_ != CodecState::CLOSED) {
  156. AV_LOGGER_WARNING("编码器已打开,先关闭再重新打开");
  157. close();
  158. }
  159. if (!validateParams(params)) {
  160. return ErrorCode::INVALID_ARGUMENT;
  161. }
  162. audioParams_ = static_cast<const AudioEncoderParams&>(params);
  163. params_ = params;
  164. ErrorCode result = initEncoder();
  165. if (result != ErrorCode::OK) {
  166. close();
  167. return result;
  168. }
  169. setState(CodecState::OPENED);
  170. AV_LOGGER_INFOF("音频编码器已打开: {} ({}Hz, {}ch, {}kbps)",
  171. audioParams_.codecName, audioParams_.sampleRate,
  172. audioParams_.channels, audioParams_.bitRate / 1000);
  173. return ErrorCode::OK;
  174. }
  175. void AudioEncoder::close() {
  176. std::lock_guard<std::mutex> lock(encodeMutex_);
  177. if (state_ == CodecState::CLOSED || state_ == CodecState::IDLE) {
  178. return;
  179. }
  180. // 清理资源
  181. resampler_.reset();
  182. convertedFrame_.reset();
  183. // 清理编解码上下文
  184. codecCtx_.reset();
  185. codec_ = nullptr;
  186. setState(CodecState::CLOSED);
  187. AV_LOGGER_DEBUG("音频编码器已关闭");
  188. }
  189. ErrorCode AudioEncoder::flush() {
  190. std::lock_guard<std::mutex> lock(encodeMutex_);
  191. if (state_ != CodecState::OPENED && state_ != CodecState::RUNNING) {
  192. return ErrorCode::INVALID_STATE;
  193. }
  194. setState(CodecState::FLUSHING);
  195. // 发送空帧来刷新编码器
  196. int ret = avcodec_send_frame(codecCtx_.get(), nullptr);
  197. if (ret < 0 && ret != AVERROR_EOF) {
  198. AV_LOGGER_ERRORF("刷新编码器失败: {}", ffmpeg_utils::errorToString(ret));
  199. reportError(static_cast<ErrorCode>(ret));
  200. return static_cast<ErrorCode>(ret);
  201. }
  202. setState(CodecState::OPENED);
  203. return ErrorCode::OK;
  204. }
  205. ErrorCode AudioEncoder::encode(const AVFramePtr& frame, std::vector<AVPacketPtr>& packets) {
  206. std::lock_guard<std::mutex> lock(encodeMutex_);
  207. if (state_ != CodecState::OPENED && state_ != CodecState::RUNNING) {
  208. return ErrorCode::INVALID_STATE;
  209. }
  210. setState(CodecState::RUNNING);
  211. auto startTime = std::chrono::high_resolution_clock::now();
  212. ErrorCode result = encodeFrame(frame, packets);
  213. auto endTime = std::chrono::high_resolution_clock::now();
  214. double processTime = std::chrono::duration<double, std::milli>(endTime - startTime).count();
  215. updateStats(result == ErrorCode::OK, processTime,
  216. frame ? frame->nb_samples * audioParams_.channels * av_get_bytes_per_sample(static_cast<AVSampleFormat>(frame->format)) : 0);
  217. if (frameCallback_ && frame) {
  218. frameCallback_(frame);
  219. }
  220. for (const auto& packet : packets) {
  221. if (packetCallback_) {
  222. packetCallback_(packet);
  223. }
  224. }
  225. return result;
  226. }
  227. ErrorCode AudioEncoder::finishEncode(std::vector<AVPacketPtr>& packets) {
  228. // 不需要额外的锁,因为调用者已经持有锁
  229. if (state_ != CodecState::OPENED && state_ != CodecState::RUNNING) {
  230. return ErrorCode::INVALID_STATE;
  231. }
  232. // 发送空帧来刷新编码器
  233. int ret = avcodec_send_frame(codecCtx_.get(), nullptr);
  234. if (ret < 0 && ret != AVERROR_EOF) {
  235. AV_LOGGER_ERRORF("刷新编码器失败: {}", ffmpeg_utils::errorToString(ret));
  236. return static_cast<ErrorCode>(ret);
  237. }
  238. // 接收剩余的包
  239. return receivePackets(packets);
  240. }
  241. bool AudioEncoder::validateParams(const CodecParams& params) {
  242. if (params.type != MediaType::AUDIO) {
  243. AV_LOGGER_ERROR("参数媒体类型不是音频");
  244. return false;
  245. }
  246. const auto& audioParams = static_cast<const AudioEncoderParams&>(params);
  247. if (audioParams.sampleRate <= 0) {
  248. AV_LOGGER_ERROR("采样率无效");
  249. return false;
  250. }
  251. if (audioParams.channels <= 0) {
  252. AV_LOGGER_ERROR("声道数无效");
  253. return false;
  254. }
  255. if (audioParams.bitRate <= 0) {
  256. AV_LOGGER_ERROR("比特率无效");
  257. return false;
  258. }
  259. if (audioParams.codecName.empty()) {
  260. AV_LOGGER_ERROR("编码器名称为空");
  261. return false;
  262. }
  263. return true;
  264. }
  265. ErrorCode AudioEncoder::initEncoder() {
  266. // 查找编码器
  267. codec_ = avcodec_find_encoder_by_name(audioParams_.codecName.c_str());
  268. if (!codec_) {
  269. AV_LOGGER_ERRORF("未找到编码器: {}", audioParams_.codecName);
  270. return ErrorCode::CODEC_NOT_FOUND;
  271. }
  272. if (codec_->type != AVMEDIA_TYPE_AUDIO) {
  273. AV_LOGGER_ERROR("编码器类型不是音频");
  274. return ErrorCode::INVALID_ARGUMENT;
  275. }
  276. // 创建编码上下文
  277. codecCtx_ = makeAVCodecContext(codec_);
  278. if (!codecCtx_) {
  279. AV_LOGGER_ERROR("分配编码上下文失败");
  280. return ErrorCode::OUT_OF_MEMORY;
  281. }
  282. // 设置编码器参数
  283. ErrorCode result = setupEncoderParams();
  284. if (result != ErrorCode::OK) {
  285. return result;
  286. }
  287. // 打开编码器
  288. int ret = avcodec_open2(codecCtx_.get(), codec_, nullptr);
  289. if (ret < 0) {
  290. AV_LOGGER_ERRORF("打开编码器失败: {}", ffmpeg_utils::errorToString(ret));
  291. return static_cast<ErrorCode>(ret);
  292. }
  293. return ErrorCode::OK;
  294. }
  295. ErrorCode AudioEncoder::setupEncoderParams() {
  296. codecCtx_->bit_rate = audioParams_.bitRate;
  297. codecCtx_->sample_rate = audioParams_.sampleRate;
  298. // 设置声道布局
  299. ErrorCode result = setupChannelLayout();
  300. if (result != ErrorCode::OK) {
  301. return result;
  302. }
  303. // 设置采样格式
  304. codecCtx_->sample_fmt = getBestSampleFormat();
  305. // 设置帧大小
  306. if (codec_->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE) {
  307. codecCtx_->frame_size = audioParams_.frameSize;
  308. }
  309. // 设置配置文件
  310. if (!audioParams_.profile.empty()) {
  311. av_opt_set(codecCtx_->priv_data, "profile", audioParams_.profile.c_str(), 0);
  312. }
  313. return ErrorCode::OK;
  314. }
  315. ErrorCode AudioEncoder::setupChannelLayout() {
  316. // 设置声道布局
  317. if (audioParams_.channelLayout.nb_channels > 0) {
  318. av_channel_layout_copy(&codecCtx_->ch_layout, &audioParams_.channelLayout);
  319. } else {
  320. // 根据声道数设置默认布局
  321. av_channel_layout_default(&codecCtx_->ch_layout, audioParams_.channels);
  322. }
  323. return ErrorCode::OK;
  324. }
  325. AVFramePtr AudioEncoder::convertFrame(const AVFramePtr& frame) {
  326. if (!frame) {
  327. return nullptr;
  328. }
  329. // 检查是否需要转换
  330. bool needConvert = false;
  331. if (frame->format != codecCtx_->sample_fmt) {
  332. needConvert = true;
  333. }
  334. if (frame->sample_rate != codecCtx_->sample_rate) {
  335. needConvert = true;
  336. }
  337. if (av_channel_layout_compare(&frame->ch_layout, &codecCtx_->ch_layout) != 0) {
  338. needConvert = true;
  339. }
  340. if (!needConvert) {
  341. // 创建一个新的帧来返回,避免拷贝构造
  342. AVFramePtr resultFrame = makeAVFrame();
  343. if (!resultFrame) {
  344. return nullptr;
  345. }
  346. // 复制帧数据
  347. if (av_frame_ref(resultFrame.get(), frame.get()) < 0) {
  348. return nullptr;
  349. }
  350. return resultFrame;
  351. }
  352. // 创建重采样器
  353. if (!resampler_) {
  354. resampler_ = std::make_unique<AudioResampler>();
  355. if (!resampler_->init(frame->ch_layout, static_cast<AVSampleFormat>(frame->format), frame->sample_rate,
  356. codecCtx_->ch_layout, codecCtx_->sample_fmt, codecCtx_->sample_rate)) {
  357. AV_LOGGER_ERROR("初始化音频重采样器失败");
  358. return nullptr;
  359. }
  360. }
  361. return resampler_->resample(frame);
  362. }
  363. ErrorCode AudioEncoder::encodeFrame(const AVFramePtr& frame, std::vector<AVPacketPtr>& packets) {
  364. AVFramePtr processedFrame;
  365. if (frame) {
  366. // 格式转换
  367. processedFrame = convertFrame(frame);
  368. if (!processedFrame) {
  369. AV_LOGGER_ERROR("音频帧格式转换失败");
  370. return ErrorCode::CONVERSION_FAILED;
  371. }
  372. }
  373. // 发送帧到编码器
  374. int ret = avcodec_send_frame(codecCtx_.get(), processedFrame ? processedFrame.get() : nullptr);
  375. if (ret < 0) {
  376. AV_LOGGER_ERRORF("发送帧到编码器失败: {}", ffmpeg_utils::errorToString(ret));
  377. return static_cast<ErrorCode>(ret);
  378. }
  379. // 接收编码后的包
  380. return receivePackets(packets);
  381. }
  382. ErrorCode AudioEncoder::receivePackets(std::vector<AVPacketPtr>& packets) {
  383. while (true) {
  384. AVPacketPtr packet = makeAVPacket();
  385. if (!packet) {
  386. return ErrorCode::OUT_OF_MEMORY;
  387. }
  388. int ret = avcodec_receive_packet(codecCtx_.get(), packet.get());
  389. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  390. break; // 需要更多输入或已结束
  391. }
  392. if (ret < 0) {
  393. AV_LOGGER_ERRORF("接收编码包失败: {}", ffmpeg_utils::errorToString(ret));
  394. return static_cast<ErrorCode>(ret);
  395. }
  396. packets.push_back(std::move(packet));
  397. }
  398. return ErrorCode::OK;
  399. }
  400. AVSampleFormat AudioEncoder::getBestSampleFormat() const {
  401. if (!codec_->sample_fmts) {
  402. return audioParams_.sampleFormat;
  403. }
  404. // 首先尝试使用指定的格式
  405. for (const AVSampleFormat* fmt = codec_->sample_fmts; *fmt != AV_SAMPLE_FMT_NONE; fmt++) {
  406. if (*fmt == audioParams_.sampleFormat) {
  407. return *fmt;
  408. }
  409. }
  410. // 返回第一个支持的格式
  411. return codec_->sample_fmts[0];
  412. }
  413. int AudioEncoder::getBestSampleRate() const {
  414. if (!codec_->supported_samplerates) {
  415. return audioParams_.sampleRate;
  416. }
  417. // 首先尝试使用指定的采样率
  418. for (const int* rate = codec_->supported_samplerates; *rate != 0; rate++) {
  419. if (*rate == audioParams_.sampleRate) {
  420. return *rate;
  421. }
  422. }
  423. // 返回第一个支持的采样率
  424. return codec_->supported_samplerates[0];
  425. }
  426. std::vector<std::string> AudioEncoder::getSupportedEncoders() {
  427. std::call_once(encodersInitFlag_, findSupportedEncoders);
  428. return supportedEncoders_;
  429. }
  430. std::string AudioEncoder::getRecommendedEncoder() {
  431. auto encoders = getSupportedEncoders();
  432. // 优先选择高质量编码器
  433. const char* preferredEncoders[] = {"libfdk_aac", "aac", "libopus", "opus", "libmp3lame", "mp3"};
  434. for (const char* encoder : preferredEncoders) {
  435. if (std::find(encoders.begin(), encoders.end(), encoder) != encoders.end()) {
  436. return encoder;
  437. }
  438. }
  439. return encoders.empty() ? "" : encoders[0];
  440. }
  441. bool AudioEncoder::isSampleFormatSupported(const std::string& codecName, AVSampleFormat format) {
  442. const AVCodec* codec = avcodec_find_encoder_by_name(codecName.c_str());
  443. if (!codec || !codec->sample_fmts) {
  444. return false;
  445. }
  446. for (const AVSampleFormat* fmt = codec->sample_fmts; *fmt != AV_SAMPLE_FMT_NONE; fmt++) {
  447. if (*fmt == format) {
  448. return true;
  449. }
  450. }
  451. return false;
  452. }
  453. bool AudioEncoder::isSampleRateSupported(const std::string& codecName, int sampleRate) {
  454. const AVCodec* codec = avcodec_find_encoder_by_name(codecName.c_str());
  455. if (!codec) {
  456. return false;
  457. }
  458. if (!codec->supported_samplerates) {
  459. return true; // 支持所有采样率
  460. }
  461. for (const int* rate = codec->supported_samplerates; *rate != 0; rate++) {
  462. if (*rate == sampleRate) {
  463. return true;
  464. }
  465. }
  466. return false;
  467. }
  468. bool AudioEncoder::isChannelLayoutSupported(const std::string& codecName, const AVChannelLayout& layout) {
  469. const AVCodec* codec = avcodec_find_encoder_by_name(codecName.c_str());
  470. if (!codec) {
  471. return false;
  472. }
  473. if (!codec->ch_layouts) {
  474. return true; // 支持所有声道布局
  475. }
  476. for (const AVChannelLayout* ch_layout = codec->ch_layouts; ch_layout->nb_channels != 0; ch_layout++) {
  477. if (av_channel_layout_compare(ch_layout, &layout) == 0) {
  478. return true;
  479. }
  480. }
  481. return false;
  482. }
  483. void AudioEncoder::findSupportedEncoders() {
  484. AV_LOGGER_INFO("查找可用的音频编码器...");
  485. for (const char* encoder : AUDIO_ENCODERS) {
  486. if (CodecFactory::isCodecSupported(encoder, CodecType::ENCODER, MediaType::AUDIO)) {
  487. supportedEncoders_.emplace_back(encoder);
  488. AV_LOGGER_INFOF("找到音频编码器: {}", encoder);
  489. }
  490. }
  491. AV_LOGGER_INFOF("总共找到 {} 个可用的音频编码器", supportedEncoders_.size());
  492. }
  493. // AudioEncoderFactory 实现
  494. std::unique_ptr<AudioEncoder> AudioEncoderFactory::create(const std::string& codecName) {
  495. auto encoder = std::make_unique<AudioEncoder>();
  496. if (!codecName.empty()) {
  497. if (!CodecFactory::isCodecSupported(codecName, CodecType::ENCODER, MediaType::AUDIO)) {
  498. AV_LOGGER_ERRORF("不支持的编码器: {}", codecName);
  499. return nullptr;
  500. }
  501. }
  502. return encoder;
  503. }
  504. std::unique_ptr<AudioEncoder> AudioEncoderFactory::createBest() {
  505. std::string codecName = AudioEncoder::getRecommendedEncoder();
  506. if (codecName.empty()) {
  507. AV_LOGGER_ERROR("未找到可用的音频编码器");
  508. return nullptr;
  509. }
  510. return create(codecName);
  511. }
  512. std::unique_ptr<AudioEncoder> AudioEncoderFactory::createLossless() {
  513. auto encoders = AudioEncoder::getSupportedEncoders();
  514. // 优先选择无损编码器
  515. const char* losslessEncoders[] = {"flac", "pcm_s24le", "pcm_s16le"};
  516. for (const char* encoder : losslessEncoders) {
  517. if (std::find(encoders.begin(), encoders.end(), encoder) != encoders.end()) {
  518. return create(encoder);
  519. }
  520. }
  521. AV_LOGGER_WARNING("未找到无损音频编码器,使用默认编码器");
  522. return createBest();
  523. }
  524. } // namespace codec
  525. } // namespace av