codec_video_decoder.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. #include "codec_video_decoder.h"
  2. #include "../base/logger.h"
  3. #include "../base/media_common.h"
  4. #include <algorithm>
  5. #include <sstream>
  6. #include <thread>
  7. extern "C" {
  8. #include <libavcodec/avcodec.h>
  9. #include <libavutil/hwcontext.h>
  10. #include <libavutil/pixdesc.h>
  11. #include <libavutil/opt.h>
  12. #include <libswscale/swscale.h>
  13. }
  14. namespace av {
  15. namespace codec {
  16. // 静态成员初始化
  17. std::vector<std::string> VideoDecoder::supportedDecoders_;
  18. std::once_flag VideoDecoder::decodersInitFlag_;
  19. VideoDecoder::VideoDecoder() : AbstractDecoder(MediaType::VIDEO) {
  20. AV_LOGGER_DEBUG("创建视频解码器");
  21. }
  22. VideoDecoder::~VideoDecoder() {
  23. close();
  24. AV_LOGGER_DEBUG("视频解码器已销毁");
  25. }
  26. ErrorCode VideoDecoder::initialize(const CodecParams& params) {
  27. if (params.type != MediaType::VIDEO) {
  28. AV_LOGGER_ERROR("参数类型不是视频");
  29. return ErrorCode::INVALID_PARAMS;
  30. }
  31. videoParams_ = static_cast<const VideoDecoderParams&>(params);
  32. if (!validateParams(params)) {
  33. return ErrorCode::INVALID_PARAMS;
  34. }
  35. setState(CodecState::IDLE);
  36. AV_LOGGER_INFOF("视频解码器初始化成功: {}", videoParams_.codecName);
  37. return ErrorCode::SUCCESS;
  38. }
  39. ErrorCode VideoDecoder::setStreamParameters(const AVCodecParameters* codecpar) {
  40. if (!codecpar) {
  41. AV_LOGGER_ERROR("流参数为空");
  42. return ErrorCode::INVALID_PARAMS;
  43. }
  44. if (codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
  45. AV_LOGGER_ERROR("流类型不是视频");
  46. return ErrorCode::INVALID_PARAMS;
  47. }
  48. // 保存流参数以便后续使用
  49. streamCodecpar_ = codecpar;
  50. AV_LOGGER_INFOF("设置流参数: 编解码器ID={}, 尺寸={}x{}, 格式={}",
  51. static_cast<int>(codecpar->codec_id),
  52. codecpar->width, codecpar->height,
  53. static_cast<int>(codecpar->format));
  54. return ErrorCode::SUCCESS;
  55. }
  56. ErrorCode VideoDecoder::open(const CodecParams& params) {
  57. std::lock_guard<std::mutex> lock(decodeMutex_);
  58. // 如果提供了参数,先初始化
  59. if (params.type != MediaType::UNKNOWN) {
  60. ErrorCode initResult = initialize(params);
  61. if (initResult != ErrorCode::SUCCESS) {
  62. return initResult;
  63. }
  64. }
  65. if (state_ != CodecState::IDLE) {
  66. AV_LOGGER_ERROR("解码器状态无效,无法打开");
  67. return ErrorCode::INVALID_STATE;
  68. }
  69. ErrorCode result = initDecoder();
  70. if (result != ErrorCode::SUCCESS) {
  71. return result;
  72. }
  73. setState(CodecState::OPENED);
  74. AV_LOGGER_INFOF("视频解码器已打开: {} ({}x{})",
  75. videoParams_.codecName,
  76. codecCtx_->width, codecCtx_->height);
  77. return ErrorCode::SUCCESS;
  78. }
  79. void VideoDecoder::close() {
  80. std::lock_guard<std::mutex> lock(decodeMutex_);
  81. if (state_ == CodecState::IDLE) {
  82. return;
  83. }
  84. // 清理硬件资源
  85. if (hwDeviceCtx_) {
  86. av_buffer_unref(&hwDeviceCtx_);
  87. hwDeviceCtx_ = nullptr;
  88. }
  89. hwFrame_.reset();
  90. codecCtx_.reset();
  91. codec_ = nullptr;
  92. isHardwareDecoder_ = false;
  93. setState(CodecState::IDLE);
  94. AV_LOGGER_DEBUG("视频解码器已关闭");
  95. }
  96. ErrorCode VideoDecoder::flush() {
  97. std::lock_guard<std::mutex> lock(decodeMutex_);
  98. if (state_ != CodecState::OPENED && state_ != CodecState::RUNNING) {
  99. return ErrorCode::INVALID_STATE;
  100. }
  101. if (codecCtx_) {
  102. avcodec_flush_buffers(codecCtx_.get());
  103. // 视频解码器flush完成,解码器状态已重置
  104. AV_LOGGER_DEBUG("视频解码器缓冲区已清空");
  105. }
  106. setState(CodecState::OPENED);
  107. AV_LOGGER_DEBUG("视频解码器已重置");
  108. return ErrorCode::SUCCESS;
  109. }
  110. ErrorCode VideoDecoder::reset() {
  111. return flush();
  112. }
  113. ErrorCode VideoDecoder::decode(const AVPacketPtr& packet, std::vector<AVFramePtr>& frames) {
  114. // 状态检查(使用基类的线程安全方法)
  115. if (!isOpened()) {
  116. return ErrorCode::INVALID_STATE;
  117. }
  118. // 只在处理过程中锁定,缩小锁的粒度
  119. std::lock_guard<std::mutex> processLock(decodeMutex_);
  120. setState(CodecState::RUNNING);
  121. auto startTime = std::chrono::high_resolution_clock::now();
  122. ErrorCode result = decodeFrame(packet, frames);
  123. auto endTime = std::chrono::high_resolution_clock::now();
  124. double processTime = std::chrono::duration<double, std::milli>(endTime - startTime).count();
  125. updateStats(result == ErrorCode::SUCCESS, processTime,
  126. packet ? packet->size : 0);
  127. if (frameCallback_) {
  128. for (const auto& frame : frames) {
  129. frameCallback_(frame);
  130. }
  131. }
  132. return result;
  133. }
  134. ErrorCode VideoDecoder::finishDecode(std::vector<AVFramePtr>& frames) {
  135. return decode(nullptr, frames); // 发送空包来刷新解码器
  136. }
  137. bool VideoDecoder::validateParams(const CodecParams& params) {
  138. if (params.type != MediaType::VIDEO) {
  139. AV_LOGGER_ERROR("参数媒体类型不是视频");
  140. return false;
  141. }
  142. const auto& videoParams = static_cast<const VideoDecoderParams&>(params);
  143. if (videoParams.codecName.empty()) {
  144. AV_LOGGER_ERROR("解码器名称为空");
  145. return false;
  146. }
  147. return true;
  148. }
  149. ErrorCode VideoDecoder::initDecoder() {
  150. // 查找解码器
  151. codec_ = avcodec_find_decoder_by_name(videoParams_.codecName.c_str());
  152. if (!codec_) {
  153. AV_LOGGER_ERRORF("未找到解码器: {}", videoParams_.codecName);
  154. return ErrorCode::CODEC_NOT_FOUND;
  155. }
  156. if (codec_->type != AVMEDIA_TYPE_VIDEO) {
  157. AV_LOGGER_ERROR("解码器类型不是视频");
  158. return ErrorCode::INVALID_PARAMS;
  159. }
  160. // 创建解码上下文
  161. codecCtx_ = makeAVCodecContext(codec_);
  162. if (!codecCtx_) {
  163. AV_LOGGER_ERROR("分配解码上下文失败");
  164. return ErrorCode::MEMORY_ALLOC_FAILED;
  165. }
  166. // 如果有流参数,复制到解码器上下文
  167. if (streamCodecpar_) {
  168. int ret = avcodec_parameters_to_context(codecCtx_.get(), streamCodecpar_);
  169. if (ret < 0) {
  170. AV_LOGGER_ERRORF("复制流参数到解码器上下文失败: {}", ffmpeg_utils::errorToString(ret));
  171. return static_cast<ErrorCode>(ret);
  172. }
  173. AV_LOGGER_INFO("成功复制流参数到解码器上下文");
  174. }
  175. // 设置硬件加速
  176. if (videoParams_.hardwareAccel && isHardwareDecoder(videoParams_.codecName)) {
  177. ErrorCode result = setupHardwareAcceleration();
  178. if (result != ErrorCode::SUCCESS) {
  179. AV_LOGGER_WARNING("硬件加速设置失败,回退到软件解码");
  180. isHardwareDecoder_ = false;
  181. // 清理硬件资源
  182. if (hwDeviceCtx_) {
  183. av_buffer_unref(&hwDeviceCtx_);
  184. hwDeviceCtx_ = nullptr;
  185. }
  186. }
  187. }
  188. // 设置解码器参数
  189. ErrorCode result = setupDecoderParams();
  190. if (result != ErrorCode::SUCCESS) {
  191. return result;
  192. }
  193. // 打开解码器前的详细日志
  194. AV_LOGGER_INFOF("准备打开解码器: {}", videoParams_.codecName);
  195. AV_LOGGER_INFOF("解码器参数: 线程数: {}, 像素格式: {}",
  196. codecCtx_->thread_count,
  197. static_cast<int>(codecCtx_->pix_fmt));
  198. if (isHardwareDecoder_) {
  199. AV_LOGGER_INFOF("硬件解码器状态: 设备上下文={}",
  200. hwDeviceCtx_ ? "已创建" : "未创建");
  201. }
  202. // 打开解码器
  203. int ret = avcodec_open2(codecCtx_.get(), codec_, nullptr);
  204. if (ret < 0) {
  205. AV_LOGGER_ERRORF("打开解码器失败: {} (错误码: {})",
  206. ffmpeg_utils::errorToString(ret), ret);
  207. // 详细错误分析
  208. if (ret == AVERROR(EINVAL)) {
  209. AV_LOGGER_ERROR("解码器参数无效 - 可能的原因:");
  210. AV_LOGGER_ERROR(" 1. 不支持的像素格式或参数组合");
  211. AV_LOGGER_ERROR(" 2. 硬件解码器参数配置错误");
  212. AV_LOGGER_ERROR(" 3. 硬件设备上下文与解码器不匹配");
  213. } else if (ret == AVERROR(EBUSY)) {
  214. AV_LOGGER_ERROR("硬件设备忙碌 - 可能被其他进程占用");
  215. } else if (ret == AVERROR(ENOMEM)) {
  216. AV_LOGGER_ERROR("内存不足 - 无法分配解码器资源");
  217. }
  218. return static_cast<ErrorCode>(ret);
  219. }
  220. AV_LOGGER_INFOF("解码器打开成功: {}", videoParams_.codecName);
  221. return ErrorCode::SUCCESS;
  222. }
  223. ErrorCode VideoDecoder::setupDecoderParams() {
  224. // 设置视频尺寸
  225. if (videoParams_.width > 0 && videoParams_.height > 0) {
  226. codecCtx_->width = videoParams_.width;
  227. codecCtx_->height = videoParams_.height;
  228. AV_LOGGER_INFOF("设置视频尺寸: {}x{}", videoParams_.width, videoParams_.height);
  229. }
  230. // 设置像素格式(如果指定)
  231. if (videoParams_.pixelFormat != AV_PIX_FMT_NONE) {
  232. codecCtx_->pix_fmt = videoParams_.pixelFormat;
  233. AV_LOGGER_INFOF("设置像素格式: {}", static_cast<int>(videoParams_.pixelFormat));
  234. }
  235. // 设置线程数
  236. if (videoParams_.threadCount > 0) {
  237. codecCtx_->thread_count = videoParams_.threadCount;
  238. } else {
  239. codecCtx_->thread_count = std::min(static_cast<int>(std::thread::hardware_concurrency()), 8);
  240. }
  241. AV_LOGGER_INFOF("设置解码线程数: {}", codecCtx_->thread_count);
  242. // 设置解码器类型
  243. codecCtx_->codec_type = AVMEDIA_TYPE_VIDEO;
  244. // 低延迟设置
  245. if (videoParams_.lowLatency) {
  246. codecCtx_->flags |= AV_CODEC_FLAG_LOW_DELAY;
  247. codecCtx_->flags2 |= AV_CODEC_FLAG2_FAST;
  248. AV_LOGGER_INFO("启用低延迟模式");
  249. }
  250. // 针对不同解码器设置特定参数
  251. if (videoParams_.codecName.find("cuvid") != std::string::npos) {
  252. // NVIDIA CUVID 特定参数
  253. if (codecCtx_->priv_data) {
  254. av_opt_set_int(codecCtx_->priv_data, "surfaces", 8, 0);
  255. if (videoParams_.lowLatency) {
  256. av_opt_set_int(codecCtx_->priv_data, "delay", 0, 0);
  257. }
  258. }
  259. } else if (videoParams_.codecName.find("qsv") != std::string::npos) {
  260. // Intel QSV 特定参数
  261. if (videoParams_.lowLatency && codecCtx_->priv_data) {
  262. av_opt_set(codecCtx_->priv_data, "async_depth", "1", 0);
  263. }
  264. }
  265. AV_LOGGER_INFOF("解码器参数设置完成: 尺寸={}x{}, 格式={}, 线程数={}",
  266. codecCtx_->width, codecCtx_->height,
  267. static_cast<int>(codecCtx_->pix_fmt), codecCtx_->thread_count);
  268. return ErrorCode::SUCCESS;
  269. }
  270. ErrorCode VideoDecoder::setupHardwareAcceleration() {
  271. isHardwareDecoder_ = true;
  272. AVHWDeviceType hwType = getHardwareDeviceType();
  273. if (hwType == AV_HWDEVICE_TYPE_NONE) {
  274. AV_LOGGER_ERRORF("不支持的硬件解码器: {}", videoParams_.codecName);
  275. return ErrorCode::NOT_SUPPORTED;
  276. }
  277. AV_LOGGER_INFOF("开始设置硬件加速: 解码器={}, 设备类型={}",
  278. videoParams_.codecName, static_cast<int>(hwType));
  279. // 创建硬件设备上下文
  280. AV_LOGGER_INFO("创建硬件设备上下文...");
  281. int ret = av_hwdevice_ctx_create(&hwDeviceCtx_, hwType, nullptr, nullptr, 0);
  282. if (ret < 0) {
  283. AV_LOGGER_ERRORF("创建硬件设备上下文失败: {} (解码器: {}, 错误码: {})",
  284. ffmpeg_utils::errorToString(ret), videoParams_.codecName, ret);
  285. // 特定错误处理
  286. if (ret == AVERROR(ENOENT)) {
  287. AV_LOGGER_ERROR("硬件设备不存在或驱动未安装");
  288. if (hwType == AV_HWDEVICE_TYPE_CUDA) {
  289. AV_LOGGER_ERROR("请检查NVIDIA驱动和CUDA是否正确安装");
  290. }
  291. } else if (ret == AVERROR(EBUSY)) {
  292. AV_LOGGER_ERROR("硬件设备正在被其他进程使用");
  293. } else if (ret == AVERROR(EINVAL)) {
  294. AV_LOGGER_ERROR("硬件设备参数无效");
  295. } else if (ret == AVERROR(ENOMEM)) {
  296. AV_LOGGER_ERROR("内存不足,无法创建硬件设备上下文");
  297. }
  298. return static_cast<ErrorCode>(ret);
  299. }
  300. AV_LOGGER_INFOF("硬件设备上下文创建成功: {}", videoParams_.codecName);
  301. // 设置硬件设备上下文到解码器
  302. codecCtx_->hw_device_ctx = av_buffer_ref(hwDeviceCtx_);
  303. return ErrorCode::SUCCESS;
  304. }
  305. ErrorCode VideoDecoder::decodeFrame(const AVPacketPtr& packet, std::vector<AVFramePtr>& frames) {
  306. // 发送包到解码器
  307. int ret = avcodec_send_packet(codecCtx_.get(), packet ? packet.get() : nullptr);
  308. if (ret < 0 && ret != AVERROR_EOF) {
  309. AV_LOGGER_ERRORF("发送包到解码器失败: {}", ffmpeg_utils::errorToString(ret));
  310. return static_cast<ErrorCode>(ret);
  311. }
  312. // 接收解码后的帧
  313. return receiveFrames(frames);
  314. }
  315. ErrorCode VideoDecoder::receiveFrames(std::vector<AVFramePtr>& frames) {
  316. while (true) {
  317. AVFramePtr frame = makeAVFrame();
  318. if (!frame) {
  319. return ErrorCode::MEMORY_ALLOC_FAILED;
  320. }
  321. int ret = avcodec_receive_frame(codecCtx_.get(), frame.get());
  322. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  323. break; // 需要更多输入或已结束
  324. }
  325. if (ret < 0) {
  326. AV_LOGGER_ERRORF("接收解码帧失败: {}", ffmpeg_utils::errorToString(ret));
  327. return static_cast<ErrorCode>(ret);
  328. }
  329. // 处理硬件帧
  330. AVFramePtr processedFrame;
  331. if (isHardwareDecoder_) {
  332. processedFrame = transferFromHardware(std::move(frame));
  333. if (!processedFrame) {
  334. AV_LOGGER_ERROR("硬件帧传输失败");
  335. continue;
  336. }
  337. } else {
  338. processedFrame = std::move(frame);
  339. }
  340. // 格式转换(如果需要)
  341. auto convertedFrame = convertFrame(processedFrame);
  342. if (convertedFrame) {
  343. frames.push_back(std::move(convertedFrame));
  344. } else {
  345. frames.push_back(std::move(processedFrame));
  346. }
  347. }
  348. return ErrorCode::SUCCESS;
  349. }
  350. AVFramePtr VideoDecoder::convertFrame(const AVFramePtr& frame) {
  351. if (!frame) {
  352. return nullptr;
  353. }
  354. // 如果格式已经匹配,直接返回
  355. if (frame->format == videoParams_.pixelFormat) {
  356. return nullptr; // 不需要转换,返回nullptr表示使用原帧
  357. }
  358. // 创建转换后的帧
  359. AVFramePtr convertedFrame = makeAVFrame();
  360. if (!convertedFrame) {
  361. return nullptr;
  362. }
  363. convertedFrame->format = videoParams_.pixelFormat;
  364. convertedFrame->width = frame->width;
  365. convertedFrame->height = frame->height;
  366. if (av_frame_get_buffer(convertedFrame.get(), 32) < 0) {
  367. AV_LOGGER_ERROR("分配转换帧缓冲区失败");
  368. return nullptr;
  369. }
  370. // 使用 swscale 进行格式转换
  371. SwsContext* swsCtx = sws_getContext(
  372. frame->width, frame->height, static_cast<AVPixelFormat>(frame->format),
  373. convertedFrame->width, convertedFrame->height, videoParams_.pixelFormat,
  374. SWS_BILINEAR, nullptr, nullptr, nullptr
  375. );
  376. if (!swsCtx) {
  377. AV_LOGGER_ERROR("创建像素格式转换上下文失败");
  378. return nullptr;
  379. }
  380. sws_scale(swsCtx, frame->data, frame->linesize, 0, frame->height,
  381. convertedFrame->data, convertedFrame->linesize);
  382. sws_freeContext(swsCtx);
  383. // 复制时间戳等信息
  384. av_frame_copy_props(convertedFrame.get(), frame.get());
  385. return convertedFrame;
  386. }
  387. AVFramePtr VideoDecoder::transferFromHardware(AVFramePtr hwFrame) {
  388. if (!hwFrame || !isHardwareDecoder_) {
  389. return std::move(hwFrame);
  390. }
  391. // 创建软件帧
  392. AVFramePtr swFrame = makeAVFrame();
  393. if (!swFrame) {
  394. return nullptr;
  395. }
  396. // 从硬件传输到软件
  397. int ret = av_hwframe_transfer_data(swFrame.get(), hwFrame.get(), 0);
  398. if (ret < 0) {
  399. AV_LOGGER_ERRORF("从硬件传输数据失败: {}", ffmpeg_utils::errorToString(ret));
  400. return nullptr;
  401. }
  402. // 复制时间戳等信息
  403. av_frame_copy_props(swFrame.get(), hwFrame.get());
  404. return swFrame;
  405. }
  406. AVHWDeviceType VideoDecoder::getHardwareDeviceType() const {
  407. if (videoParams_.codecName.find("cuvid") != std::string::npos) {
  408. return AV_HWDEVICE_TYPE_CUDA;
  409. } else if (videoParams_.codecName.find("qsv") != std::string::npos) {
  410. return AV_HWDEVICE_TYPE_QSV;
  411. } else if (videoParams_.codecName.find("d3d11va") != std::string::npos) {
  412. return AV_HWDEVICE_TYPE_D3D11VA;
  413. } else if (videoParams_.codecName.find("videotoolbox") != std::string::npos) {
  414. return AV_HWDEVICE_TYPE_VIDEOTOOLBOX;
  415. }
  416. return AV_HWDEVICE_TYPE_NONE;
  417. }
  418. AVPixelFormat VideoDecoder::getHardwarePixelFormat() const {
  419. if (videoParams_.codecName.find("cuvid") != std::string::npos) {
  420. return AV_PIX_FMT_CUDA;
  421. } else if (videoParams_.codecName.find("qsv") != std::string::npos) {
  422. return AV_PIX_FMT_QSV;
  423. } else if (videoParams_.codecName.find("d3d11va") != std::string::npos) {
  424. return AV_PIX_FMT_D3D11;
  425. } else if (videoParams_.codecName.find("videotoolbox") != std::string::npos) {
  426. return AV_PIX_FMT_VIDEOTOOLBOX;
  427. }
  428. AV_LOGGER_ERRORF("未知的硬件解码器: {}", videoParams_.codecName);
  429. return AV_PIX_FMT_NONE;
  430. }
  431. void VideoDecoder::updateStats(bool success, double decodeTime, size_t dataSize) {
  432. std::lock_guard<std::mutex> lock(statsMutex_);
  433. if (success) {
  434. stats_.decodedFrames++;
  435. stats_.totalBytes += dataSize;
  436. // 更新平均解码时间
  437. if (stats_.decodedFrames == 1) {
  438. stats_.avgDecodeTime = decodeTime;
  439. } else {
  440. stats_.avgDecodeTime = (stats_.avgDecodeTime * (stats_.decodedFrames - 1) + decodeTime) / stats_.decodedFrames;
  441. }
  442. } else {
  443. stats_.errorCount++;
  444. }
  445. }
  446. VideoDecoder::DecoderStats VideoDecoder::getStats() const {
  447. std::lock_guard<std::mutex> lock(statsMutex_);
  448. return stats_;
  449. }
  450. void VideoDecoder::resetStats() {
  451. std::lock_guard<std::mutex> lock(statsMutex_);
  452. stats_ = DecoderStats{};
  453. }
  454. std::string VideoDecoder::getDecoderName() const {
  455. return videoParams_.codecName;
  456. }
  457. std::vector<std::string> VideoDecoder::getSupportedDecoders() {
  458. std::call_once(decodersInitFlag_, findUsableDecoders);
  459. return supportedDecoders_;
  460. }
  461. bool VideoDecoder::isHardwareDecoder(const std::string& codecName) {
  462. for (const char* hwDecoder : HARDWARE_DECODERS) {
  463. if (hwDecoder != nullptr && codecName == hwDecoder) {
  464. return true;
  465. }
  466. }
  467. return false;
  468. }
  469. std::string VideoDecoder::getRecommendedDecoder(const std::string& codecName) {
  470. auto decoders = getSupportedDecoders();
  471. if (!codecName.empty()) {
  472. // 查找指定编解码格式的最佳解码器
  473. std::string baseCodec = codecName;
  474. // 优先选择硬件解码器
  475. for (const char* hwDecoder : HARDWARE_DECODERS) {
  476. if (hwDecoder != nullptr) {
  477. std::string hwDecoderName = hwDecoder;
  478. if (hwDecoderName.find(baseCodec) != std::string::npos &&
  479. std::find(decoders.begin(), decoders.end(), hwDecoderName) != decoders.end()) {
  480. return hwDecoderName;
  481. }
  482. }
  483. }
  484. // 回退到软件解码器
  485. if (std::find(decoders.begin(), decoders.end(), baseCodec) != decoders.end()) {
  486. return baseCodec;
  487. }
  488. }
  489. // 返回第一个可用的硬件解码器
  490. for (const char* hwDecoder : HARDWARE_DECODERS) {
  491. if (hwDecoder != nullptr && std::find(decoders.begin(), decoders.end(), hwDecoder) != decoders.end()) {
  492. return hwDecoder;
  493. }
  494. }
  495. // 回退到软件解码器
  496. for (const char* swDecoder : SOFTWARE_DECODERS) {
  497. if (swDecoder != nullptr && std::find(decoders.begin(), decoders.end(), swDecoder) != decoders.end()) {
  498. return swDecoder;
  499. }
  500. }
  501. return decoders.empty() ? "" : decoders[0];
  502. }
  503. void VideoDecoder::findUsableDecoders() {
  504. AV_LOGGER_INFO("查找可用的视频解码器...");
  505. // 测试硬件解码器
  506. for (const char* decoder : HARDWARE_DECODERS) {
  507. if (decoder != nullptr && CodecFactory::isCodecSupported(decoder, CodecType::DECODER, MediaType::VIDEO)) {
  508. supportedDecoders_.emplace_back(decoder);
  509. AV_LOGGER_INFOF("找到硬件解码器: {}", decoder);
  510. }
  511. }
  512. // 测试软件解码器
  513. for (const char* decoder : SOFTWARE_DECODERS) {
  514. if (decoder != nullptr && CodecFactory::isCodecSupported(decoder, CodecType::DECODER, MediaType::VIDEO)) {
  515. supportedDecoders_.emplace_back(decoder);
  516. AV_LOGGER_INFOF("找到软件解码器: {}", decoder);
  517. }
  518. }
  519. AV_LOGGER_INFOF("总共找到 {} 个可用的视频解码器", supportedDecoders_.size());
  520. }
  521. // VideoDecoderFactory 实现
  522. std::unique_ptr<VideoDecoder> VideoDecoder::VideoDecoderFactory::create(const std::string& codecName) {
  523. auto decoder = std::make_unique<VideoDecoder>();
  524. if (!codecName.empty()) {
  525. if (!CodecFactory::isCodecSupported(codecName, CodecType::DECODER, MediaType::VIDEO)) {
  526. AV_LOGGER_ERRORF("不支持的解码器: {}", codecName);
  527. return nullptr;
  528. }
  529. }
  530. return decoder;
  531. }
  532. std::unique_ptr<VideoDecoder> VideoDecoder::VideoDecoderFactory::createBest(bool preferHardware) {
  533. std::string codecName;
  534. if (preferHardware) {
  535. codecName = VideoDecoder::getRecommendedDecoder();
  536. } else {
  537. // 优先选择软件解码器
  538. auto decoders = VideoDecoder::getSupportedDecoders();
  539. for (const char* swDecoder : VideoDecoder::SOFTWARE_DECODERS) {
  540. if (swDecoder != nullptr && std::find(decoders.begin(), decoders.end(), swDecoder) != decoders.end()) {
  541. codecName = swDecoder;
  542. break;
  543. }
  544. }
  545. }
  546. if (codecName.empty()) {
  547. AV_LOGGER_ERROR("未找到可用的视频解码器");
  548. return nullptr;
  549. }
  550. return create(codecName);
  551. }
  552. } // namespace codec
  553. } // namespace av