DecodeVedio.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. #include "DecodeVedio.h"
  2. #include "FrameFormat.h"
  3. #include "spdlog/spdlog.h"
  4. #include <QThread>
  5. extern "C"
  6. {
  7. // #include <libavcodec/avcodec.h>
  8. // #include <libavformat/avformat.h>
  9. #include <libswscale/swscale.h>
  10. #include <libavutil/imgutils.h>
  11. }
  12. #include "OpenGLWidgetAPI.h"
  13. /*=================================================================================================
  14. * @brief FFmpeg获取GPU硬件解码帧格式的回调函数
  15. ===================================================================================================*/
  16. static enum AVPixelFormat g_hw_pix_fmt;
  17. /**
  18. * @brief 回调函数,获取GPU硬件解码帧的格式
  19. *
  20. * @param ctx
  21. * @param pix_fmts
  22. * @return AVPixelFormat
  23. */
  24. AVPixelFormat get_hw_format(AVCodecContext *ctx, const AVPixelFormat *pix_fmts)
  25. {
  26. Q_UNUSED(ctx)
  27. const AVPixelFormat* p;
  28. for(p = pix_fmts; *p != -1; p++)
  29. {
  30. if(*p == g_hw_pix_fmt)
  31. {
  32. return *p;
  33. }
  34. }
  35. SPDLOG_WARN("无法获取硬件解码器表面格式。");
  36. return AV_PIX_FMT_NONE;
  37. }
  38. /**
  39. * @brief Construct a new Decode Vedio:: Decode Vedio object
  40. *
  41. * @param thread
  42. * @param parent
  43. */
  44. DecodeVedio::DecodeVedio(QThread* thread, QObject* parent) : QObject(parent) , m_thread(thread)
  45. {
  46. /* 在连接之前调用,移动到新的线程 */
  47. this->moveToThread(thread);
  48. connect(this, &DecodeVedio::signal_startDecode, this, &DecodeVedio::do_startDecodeVedio);
  49. thread->start();
  50. findHWDecoder();
  51. // if(m_supportHWDecoder)
  52. // {
  53. // SPDLOG_DEBUG("支持的硬件解码器:");
  54. // for(auto it : m_listDecoderName)
  55. // {
  56. // SPDLOG_DEBUG("{}", it.toStdString());
  57. // }
  58. // }else {
  59. // SPDLOG_WARN("未找到可用的硬件解码器。");
  60. // }
  61. }
  62. DecodeVedio::~DecodeVedio()
  63. {
  64. stopDecodeVedio();
  65. if(m_thread != nullptr)
  66. {
  67. if(m_thread->isRunning())
  68. {
  69. m_thread->quit();
  70. m_thread->wait();
  71. }
  72. }
  73. }
  74. /* 开始解码视频 */
  75. void DecodeVedio::startDecodeVedio()
  76. {
  77. if(m_threadRuning)
  78. {
  79. return;
  80. }
  81. if(!m_initFFmpeg)
  82. {
  83. SPDLOG_WARN("未初始化FFMPEG...");
  84. return;
  85. }
  86. m_threadRuning = true;
  87. // decodeUsingCPU();
  88. /* 发送信号,开启新线程 */
  89. emit signal_startDecode();
  90. }
  91. /* 停止解码视频,退出工作函数,线程未停止 */
  92. void DecodeVedio::stopDecodeVedio()
  93. {
  94. if(!m_threadRuning)
  95. {
  96. return;
  97. }
  98. exitThread();
  99. /* 唤醒阻塞住的解码线程 */
  100. // /* 等待线程执行结束 */
  101. while(m_decodeState.load() != DecodeState::DecodeExit)
  102. {
  103. std::this_thread::sleep_for(std::chrono::milliseconds(5));
  104. }
  105. freeAll();
  106. m_threadRuning = false;
  107. }
  108. /**
  109. * @brief 设置当前播放位置,单位是毫秒
  110. * 这里需要去掉队列中已有的图片数目对应的时长
  111. *
  112. * @param pos 要跳转的位置,范围从0~duration
  113. */
  114. void DecodeVedio::setCurrentPos(qint64 pos)
  115. {
  116. if(!m_threadRuning)
  117. {
  118. return;
  119. }
  120. m_isSeek = true;
  121. /* 先暂停解码 */
  122. pauseDecode();
  123. SPDLOG_DEBUG("跳转到:{}ms",pos);
  124. /*在环形队列中有已解码的视频帧数,需要去掉已有的帧数所占的时间 */
  125. pos = pos - m_queueFrame.QueueSize() * (1000 / m_fps);
  126. if(pos < 0) {
  127. pos = 0;
  128. }
  129. if(pos > m_duration) {
  130. pos = m_duration;
  131. }
  132. pos = pos + m_startPos;
  133. qint64 targetPos = qRound64((double)pos / (1000 * rationalToDouble(&m_pFormatContext->streams[m_videoStream]->time_base)));
  134. /* 开始跳转,这里设置标志为AVSEEK_FLAG_BACKWARD,跳转到目标位置的前一个关键帧中,然后开始解码,直到到达目标位置为止 */
  135. int ret = av_seek_frame(m_pFormatContext, m_videoStream, targetPos, AVSEEK_FLAG_BACKWARD);
  136. if(ret < 0)
  137. {
  138. SPDLOG_ERROR("跳转失败!");
  139. }
  140. m_targetPos = pos;
  141. /* 刷新解码器缓冲区 */
  142. m_flushDecoder.store(true);
  143. /* 清空环形队列中的视频 */
  144. SPDLOG_DEBUG("清空环形队列中的视频。");
  145. clearQueueFrame();
  146. /* 继续解码 */
  147. continueDecode();
  148. }
  149. /* 获取当前播放位置,单位ms */
  150. qint64 DecodeVedio::getCurrentPos()
  151. {
  152. return m_pts.load() - m_startPos;
  153. }
  154. /* 获取视频时长 */
  155. qint64 DecodeVedio::getDuration()
  156. {
  157. return m_duration;
  158. }
  159. /**
  160. * @brief 获取一帧图像,队列为空就返回nullptr,这个函数应该是运行在UI线程中的
  161. * @warning 传出这个指针后,队列就出队了,内存需要外面获取的实例释放
  162. * @return QImage* 一帧图像的指针
  163. */
  164. FrameBase* DecodeVedio::getOneImage()
  165. {
  166. if(!m_threadRuning)
  167. {
  168. return nullptr;
  169. }
  170. // FrameBase* frame = nullptr;
  171. // if(!m_queueFrame.front_pop_noBlock(frame))
  172. // {
  173. // return nullptr;
  174. // }
  175. return m_queueFrame.front_pop_noBlock();
  176. }
  177. /**
  178. * @brief 获取一帧图像,直到有图像为止
  179. *
  180. * @param timeOut 设为-1是一直等待,设置正整数是等待的时间,单位ms
  181. * @return QImage*
  182. */
  183. FrameBase* DecodeVedio::getOneImageUntilHave(int timeOut)
  184. {
  185. if(!m_threadRuning)
  186. {
  187. return nullptr;
  188. }
  189. if(timeOut < 0)
  190. {
  191. auto frame = m_queueFrame.front_pop();
  192. return frame;
  193. }
  194. FrameBase* frame = nullptr;
  195. for(int i = 0; i < timeOut; i++)
  196. {
  197. if(m_queueFrame.front_pop_noBlock(frame))
  198. {
  199. return frame;
  200. }
  201. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  202. }
  203. return nullptr;
  204. }
  205. /* 查找硬件解码器 */
  206. void DecodeVedio::findHWDecoder(QStringList& listDecoderName)
  207. {
  208. AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
  209. listDecoderName.clear();
  210. while( (type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
  211. {
  212. /* 获取硬件解码器的名称 */
  213. const char* typeName = av_hwdevice_get_type_name(type);
  214. if(typeName)
  215. {
  216. listDecoderName.append(QString(typeName));
  217. }
  218. }
  219. }
  220. /* 清空环形队列 */
  221. void DecodeVedio::clearQueueFrame()
  222. {
  223. while(m_queueFrame.QueueSize() > 0)
  224. {
  225. FrameBase* frame = nullptr;
  226. m_queueFrame.front_pop_noBlock(frame);
  227. deleteOneFrame(frame);
  228. }
  229. }
  230. /* 删除一个图片 */
  231. void DecodeVedio::deleteOneImage()
  232. {
  233. if(m_queueFrame.QueueSize() > 0)
  234. {
  235. auto frame = m_queueFrame.front_pop_noBlock();
  236. deleteOneFrame(frame);
  237. }
  238. }
  239. /* 获取硬件解码器 */
  240. void DecodeVedio::findHWDecoder()
  241. {
  242. AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
  243. // QStringList strTypes;
  244. m_listDecoderName.clear();
  245. while( (type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
  246. {
  247. m_listHWDeviceType.append(type);
  248. /* 获取硬件解码器的名称 */
  249. const char* typeName = av_hwdevice_get_type_name(type);
  250. if(typeName)
  251. {
  252. m_listDecoderName.append(QString(typeName));
  253. }
  254. }
  255. if(m_listHWDeviceType.isEmpty())
  256. {
  257. m_supportHWDecoder = false;
  258. }else {
  259. m_supportHWDecoder = true;
  260. }
  261. m_supportHWDecoder = false;
  262. }
  263. /* 初始化硬件解码器 */
  264. void DecodeVedio::initHWDecoder(const AVCodec* codec)
  265. {
  266. if(codec == nullptr)
  267. {
  268. return;
  269. }
  270. for(int i = 0;;i++)
  271. {
  272. /* 获取编解码器支持的硬件配置 */
  273. const AVCodecHWConfig* hwConfig = avcodec_get_hw_config(codec, 0);
  274. if(hwConfig == nullptr)
  275. {
  276. SPDLOG_WARN("没有找到支持{}的硬件配置", codec->name);
  277. return;
  278. }
  279. /* 判断是否是设备类型 */
  280. if(hwConfig->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
  281. {
  282. /* 在已有的解码器列表中查找 */
  283. for(auto i : m_listHWDeviceType)
  284. {
  285. if(hwConfig->device_type == AVHWDeviceType(i))
  286. {
  287. /* 获取像素格式 */
  288. g_hw_pix_fmt = hwConfig->pix_fmt;
  289. /* 打开指定类型的设备,并为其创建AVHWDeviceContext */
  290. int ret = av_hwdevice_ctx_create(&m_hw_device_ctx, hwConfig->device_type, nullptr, nullptr, 0);
  291. if(ret < 0)
  292. {
  293. SPDLOG_ERROR("打开硬件解码器失败!");
  294. return;
  295. }
  296. SPDLOG_INFO("打开硬件解码器:{}", av_hwdevice_get_type_name(hwConfig->device_type));
  297. m_pCodecContext->hw_device_ctx = av_buffer_ref(m_hw_device_ctx);
  298. m_pCodecContext->get_format = get_hw_format;
  299. return;
  300. }
  301. }
  302. }
  303. }
  304. }
  305. /* 拷贝数据,从GPU显存拷贝到内存中 */
  306. bool DecodeVedio::copyDataFromGPU(AVFrame* pFrameHW, AVFrame* pFrameSRC)
  307. {
  308. if(m_pFrameSRC->format != g_hw_pix_fmt)
  309. {
  310. av_frame_unref(m_pFrameSRC);
  311. return false;
  312. }
  313. /* 这一步可能会耗费较长时间 */
  314. int ret = av_hwframe_transfer_data(pFrameHW, pFrameSRC, 0);
  315. if(ret < 0)
  316. {
  317. SPDLOG_ERROR("从GPU拷贝数据失败!");
  318. av_frame_unref(pFrameSRC);
  319. return false;
  320. }
  321. av_frame_copy_props(pFrameHW, pFrameSRC);
  322. return true;
  323. }
  324. /* 打开视频,同时初始化解码器) */
  325. bool DecodeVedio::openVedio(const QString& fileName)
  326. {
  327. if(fileName.isEmpty())
  328. {
  329. SPDLOG_WARN("文件名为空...");
  330. return false;
  331. }
  332. m_fileName = fileName;
  333. if(m_initFFmpeg)
  334. {
  335. freeAll();
  336. }
  337. /* 清空队列 */
  338. clearQueueFrame();
  339. m_queueFrame.setQueueCapacity(30);
  340. m_queueFrame.setDefaultValue(nullptr);
  341. SPDLOG_DEBUG("开始初始化FFMPEG");
  342. /* 设置网络缓冲区大小和错误恢复选项 */
  343. AVDictionary *options = nullptr;
  344. /* 设置接收缓冲区 */
  345. av_dict_set(&options, "buffer_size", "1024000", 0);
  346. /* 设置最大复用或解复用延迟(以微秒为单位)。当通过【UDP】 接收数据时,解复用器尝试重新排序接收到的数据包
  347. * (因为它们可能无序到达,或者数据包可能完全丢失)。这可以通过将最大解复用延迟设置为零
  348. * (通过max_delayAVFormatContext 字段)来禁用。 */
  349. av_dict_set(&options, "max_delay", "500000", 0);
  350. /* 设置rtsp流使用tcp打开,如果打开失败错误信息为【Error number -135 occurred】可以切换(UDP、tcp、udp_multicast、http),比如vlc推流就需要使用udp打开 */
  351. av_dict_set(&options, "rtsp_transport", "tcp", 0);
  352. /* 以微秒为单位设置套接字 TCP I/O 超时,如果等待时间过短,也可能会还没连接就返回了。 */
  353. av_dict_set(&options, "stimeout", "20000000", 0);
  354. /************ 存储文件格式信息 ************/
  355. /* 打开文件,读取视频文件的头信息,放在第一个参数的结构体中 */
  356. int ret = avformat_open_input( &m_pFormatContext, /* 解封装上下文 */
  357. m_fileName.toStdString().data(), /* 打开视频地址 */
  358. nullptr, /* 这个参数强制使用特定的输入格式,可以设置为null */
  359. &options); /* 参数设置 */
  360. if(ret != 0)
  361. {
  362. SPDLOG_WARN("打开视频文件错误,错误代码:{}",ret);
  363. printErrorStr(ret);
  364. freeAll();
  365. return false;
  366. }
  367. /* 释放参数 */
  368. if(options != nullptr)
  369. {
  370. av_dict_free(&options);
  371. }
  372. /************ 找到视频流 ************/
  373. /* 检查视频容器内部的流信息,将所有流存储到了pFormatContext->streams中
  374. * 查找到视频流,并获取视频时长相关的信息 */
  375. ret = 0;
  376. ret = avformat_find_stream_info(m_pFormatContext, nullptr);
  377. if(ret < 0)
  378. {
  379. SPDLOG_WARN("获取视频流错误,错误代码:{}",ret);
  380. printErrorStr(ret);
  381. freeAll();
  382. return false;
  383. }
  384. m_duration = m_pFormatContext->duration / (AV_TIME_BASE / 1000); /* 获取视频时长,单位是毫秒 */
  385. m_startPos = m_pFormatContext->start_time / (AV_TIME_BASE / 1000); /* 获取视频开始时间,单位是毫秒 */
  386. // SPDLOG_DEBUG("开始时间:{} 时长:{}",m_startPos, m_duration);
  387. /* 一个调试函数,将流信息输出到控制台 */
  388. av_dump_format(m_pFormatContext, 0, m_fileName.toStdString().c_str(), 0);
  389. /* 找到视频流 */
  390. m_videoStream = av_find_best_stream(m_pFormatContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
  391. if(m_videoStream < 0)
  392. {
  393. SPDLOG_WARN("没有找到视频流");
  394. freeAll();
  395. return false;
  396. }
  397. SPDLOG_DEBUG("找到视频流");
  398. /* 获取视频流的编解码信息,主要是分辨率等信息 */
  399. AVStream *pStream = m_pFormatContext->streams[m_videoStream]; /* 获取视频流 */
  400. AVCodecParameters* pCodecParams = pStream->codecpar; /* 获取视频流的编解码信息 */
  401. SPDLOG_DEBUG("获取视频流参数成功!");
  402. /* 获取视频相关信息 */
  403. m_srcSize.setWidth(pCodecParams->width);
  404. m_srcSize.setHeight(pCodecParams->height);
  405. m_fps = rationalToDouble(&pStream->avg_frame_rate);
  406. m_totalFrame = m_pFormatContext->streams[m_videoStream]->nb_frames;
  407. m_pts.store(0);
  408. /************ 查找并设置解码器 ************/
  409. /* 找到解码器 */
  410. const AVCodec* pCodec = avcodec_find_decoder(pCodecParams->codec_id);
  411. if(pCodec == nullptr)
  412. {
  413. SPDLOG_WARN("没有找到解码器");
  414. freeAll();
  415. return false;
  416. }
  417. m_decoderName = pCodec->name;
  418. // SPDLOG_INFO("找到解码器:{}",pCodec->name);
  419. /* 获取视频信息的上下文,先分配空间,后面记得释放空间 */
  420. m_pCodecContext = avcodec_alloc_context3(pCodec);
  421. /* 将视频流中的编码器参数拷贝下来,这个函数不是线程安全的 */
  422. if(avcodec_parameters_to_context(m_pCodecContext, pCodecParams) != 0)
  423. {
  424. SPDLOG_WARN("复制上下文错误");
  425. freeAll();
  426. return false;
  427. }
  428. SPDLOG_DEBUG("设置解码器参数成功!");
  429. // m_pCodecContext->flags2 |= AV_CODEC_FLAG2_FAST; /* 使用快速解码(允许使用不符合规范的解码) */
  430. m_pCodecContext->thread_count = 8; /* 使用8线程解码 */
  431. /* 初始化硬件解码器 */
  432. if(m_supportHWDecoder)
  433. {
  434. initHWDecoder(pCodec);
  435. }
  436. /* 打开解码器,(初始化解码器上下文,如果调用了avcodec_alloc_context3,第二个参数可以设置为nullptr) */
  437. if(avcodec_open2(m_pCodecContext, nullptr, nullptr) < 0)
  438. {
  439. SPDLOG_ERROR("打开解码器错误");
  440. freeAll();
  441. return false;
  442. }
  443. SPDLOG_DEBUG("打开解码器成功!");
  444. /************ 初始化数据包 ************/
  445. m_packet = av_packet_alloc();
  446. av_new_packet(m_packet, m_pCodecContext->width * m_pCodecContext->height);
  447. /* 创建两个pFrame,一个存放原始数据,一个存放转换后的RGB数据 */
  448. m_pFrameSRC = av_frame_alloc();
  449. if(m_pFrameSRC == nullptr)
  450. {
  451. SPDLOG_ERROR("创建pFrame错误");
  452. freeAll();
  453. return false;
  454. }
  455. // m_pFrameRGB = av_frame_alloc();
  456. // if(m_pFrameRGB == nullptr)
  457. // {
  458. // SPDLOG_ERROR("创建pFrameRGB错误");
  459. // freeAll();
  460. // return;
  461. // }
  462. m_pFrameHW = av_frame_alloc();
  463. if(m_pFrameHW == nullptr)
  464. {
  465. SPDLOG_ERROR("创建pFrameHW错误");
  466. freeAll();
  467. return false;
  468. }
  469. if(m_buffer != nullptr)
  470. {
  471. av_free(m_buffer);
  472. m_buffer = nullptr;
  473. }
  474. /* 分配图像空间 */
  475. int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGBA, m_pCodecContext->width, m_pCodecContext->height, 1);
  476. /* 这里多分配了一些空间,防止某些视频图像在使用sws_scale()拷贝后超出数组长度 */
  477. m_buffer = (uint8_t *)av_malloc(numBytes + 1000);
  478. m_initFFmpeg = true;
  479. SPDLOG_INFO("FFMPEG初始化完成!");
  480. // SPDLOG_INFO("视频宽度:{} 高度:{} 帧率:{} 总时长:{} 总帧数:{}",m_srcSize.width(), m_srcSize.height(), m_fps, m_duration, m_totalFrame);
  481. /* 再次判断帧数是否正常,如果没读取到,就使用 总帧数 / 时长 */
  482. if(m_fps == 0)
  483. {
  484. if((m_duration > 0) && (m_totalFrame > 0))
  485. {
  486. m_fps = m_totalFrame / (m_duration / 1000.0);
  487. }
  488. /* 到这一步,无法确定帧数了,就按照25帧来计算了 */
  489. if(m_fps == 0)
  490. {
  491. m_fps = 25;
  492. }
  493. }
  494. return true;
  495. }
  496. /**
  497. * @brief 软解码线程,使用CPU解码,使用环境队列存储解码的数据
  498. *
  499. */
  500. void DecodeVedio::threadDecodeUsingCPU()
  501. {
  502. /******** 初始化局部变量 ********/
  503. bool isEnd = false;
  504. // int ret = 0;
  505. // int retFrame = 0;
  506. // int retPacket = 0;
  507. m_pauseDecode = false;
  508. m_decodeStatus = true;
  509. m_decodeState.store(DecodeState::DecodeRun);
  510. /* 开始解码 */
  511. SPDLOG_DEBUG("开始解码...");
  512. while(m_threadRuning)
  513. {
  514. /******** 判断是否在暂停状态 ********/
  515. while(m_pauseDecode)
  516. {
  517. m_decodeState.store(DecodeState::DecodePause);
  518. std::this_thread::sleep_for(std::chrono::microseconds(100));
  519. }
  520. m_decodeState.store(DecodeState::DecodeRun);
  521. /* 刷新解码器缓冲区,清除掉里面残留的解码文件 */
  522. if(m_flushDecoder.load())
  523. {
  524. avcodec_flush_buffers(m_pCodecContext);
  525. m_flushDecoder.store(false);
  526. }
  527. /******** 读取数据包 av_read_frame ********/
  528. int retRead = av_read_frame(m_pFormatContext, m_packet);
  529. if(retRead == AVERROR_EOF)
  530. {
  531. /* 读取到末尾后,需要传入一个空的packet,才能读取到最后几帧 */
  532. avcodec_send_packet(m_pCodecContext, m_packet);
  533. }
  534. else if(retRead < 0)
  535. {
  536. SPDLOG_ERROR("读取帧错误...");
  537. break;
  538. }
  539. else
  540. {
  541. if(m_packet->stream_index == m_videoStream)
  542. {
  543. // SPDLOG_DEBUG("源pts:{}", m_packet->pts);
  544. /* pts 表示显示时间戳(Presentation Timestamp),它指示解码后的帧应该在什么时候显示。pts 是用于同步音视频的关键时间戳。
  545. dts 表示解码时间戳(Decoding Timestamp),它指示解码器应该在什么时候解码这个数据包。dts 用于确保解码器按照正确的顺序解码数据包。 */
  546. #if 1
  547. /* 计算当前帧时间,两种方法,第一种适用于所有场景,但是存在一定的误差 */
  548. m_packet->pts = qRound64(m_packet->pts * (1000 * rationalToDouble(&m_pFormatContext->streams[m_videoStream]->time_base)));
  549. m_packet->dts = qRound64(m_packet->dts * (1000 * rationalToDouble(&m_pFormatContext->streams[m_videoStream]->time_base)));
  550. #else
  551. /* 适用于本地视频,直接计算本地视频文件的每一帧时间 */
  552. m_currentFrame++;
  553. // m_packet->pts = qRound64(m_currentFrame * (qreal(m_duration) / m_totalFrame));
  554. #endif
  555. /* 将数据传给解码器 */
  556. int ret = avcodec_send_packet(m_pCodecContext, m_packet);
  557. if(ret < 0)
  558. {
  559. SPDLOG_ERROR("发送数据包错误...");
  560. av_packet_unref(m_packet);
  561. continue;
  562. }
  563. }else {
  564. // SPDLOG_INFO("不是视频流。");
  565. av_packet_unref(m_packet);
  566. continue;
  567. }
  568. }
  569. // SPDLOG_DEBUG("读取到数据包packet,pts:{}",m_packet->pts);
  570. /* 解码packet包的内容,一个packet包内可能包含好多帧视频 */
  571. while(m_threadRuning)
  572. {
  573. /* 读取出解码器返回的帧 avcodec_receive_frame */
  574. int ret = avcodec_receive_frame(m_pCodecContext, m_pFrameSRC);
  575. if(ret == AVERROR_EOF)
  576. {
  577. SPDLOG_INFO("读取到视频流末尾。");
  578. isEnd = true;
  579. break;
  580. }
  581. else if (ret == AVERROR(EAGAIN))
  582. {
  583. /* packet中的内容无法解码成一帧,需要更多的输入包,可能已经取出了几帧,也肯能就不够一帧
  584. * 这时候就需要往解码器送数据包了 */
  585. // SPDLOG_WARN("packet无法解码成一帧,需要更多的输入包");
  586. av_frame_unref(m_pFrameSRC);
  587. break;
  588. }
  589. else if(ret < 0)
  590. {
  591. av_frame_unref(m_pFrameSRC);
  592. if(retRead < 0)
  593. {
  594. SPDLOG_ERROR("读取错误,错误值:{}",ret);
  595. break;
  596. }
  597. }
  598. /* 解码成功,获取当前时间,现在已经是准的时间了
  599. * 如果在跳转状态,在这里判断是否到了目标位置 */
  600. m_pts = m_pFrameSRC->pts;
  601. if(m_isSeek.load())
  602. {
  603. if(m_pts < m_targetPos)
  604. {
  605. SPDLOG_DEBUG("目标位置:{} 当前位置:{}",m_targetPos, m_pts.load());
  606. av_frame_unref(m_pFrameSRC);
  607. continue;
  608. }else {
  609. m_isSeek = false;
  610. m_targetPos = -1;
  611. SPDLOG_INFO("跳转结束。");
  612. }
  613. }
  614. // SPDLOG_DEBUG("当前帧的pts:{}", m_pts.load());
  615. // /* 转换解码后的帧格式,转换成RGBA格式,Qt可以识别 */
  616. // if(m_sws_ctx == nullptr)
  617. // {
  618. // /* 选择在这里创建,为了兼容硬件解码,硬件解码出来的格式可能和解码器传出的不一样 */
  619. // m_sws_ctx = sws_getCachedContext(m_sws_ctx,
  620. // m_pFrameSRC->width, m_pFrameSRC->height, /* 原图像大小和格式 */
  621. // m_pCodecContext->pix_fmt, /* 输入图像的像素格式 */
  622. // m_srcSize.width(), m_srcSize.height(), /* 目标图像的大小 */
  623. // AV_PIX_FMT_RGBA, /* 目标图像的格式 */
  624. // SWS_BILINEAR, /* 图像缩放算法,双线性 */
  625. // nullptr, /* 输入图像的滤波器信息,不需要传NULL */
  626. // nullptr, /* 输出图像的滤波器信息,不需要传NULL */
  627. // nullptr); /* 特定缩放算法需要的参数,不需要传NULL */
  628. // if(m_sws_ctx == nullptr)
  629. // {
  630. // SPDLOG_ERROR("创建SwsContext错误...");
  631. // goto label_ThreadDecodeExit;
  632. // }
  633. // SPDLOG_INFO("创建SwsContext成功...");
  634. // }
  635. // /* 转换成RGBA格式 */
  636. // // uint8_t* data[1] = { m_buffer };
  637. // int lines[4];
  638. // /* 使用像素格式pix_fmt和宽度填充图像的平面线条的大小(一行大小?) */
  639. // av_image_fill_linesizes(lines, AV_PIX_FMT_RGBA, m_pFrameSRC->width);
  640. // sws_scale( m_sws_ctx, /* 转换上下文 */
  641. // m_pFrameSRC->data, /* 源图像数据指针数组 */
  642. // m_pFrameSRC->linesize, /* 源图像每行字节数数组 */
  643. // 0, /* 源图像起始行(通常为0) */
  644. // m_pFrameSRC->height, /* 处理的行数(通常为图像高度) */
  645. // &m_buffer, /* 目标图像数据指针数组(packed格式只用[0]) */
  646. // lines); /* 目标图像每行字节数数组 */
  647. // if(m_buffer != nullptr)
  648. // {
  649. // /* 将数据拷贝到QImage中 */
  650. // auto image = new QImage(m_buffer, m_srcSize.width(), m_srcSize.height(), QImage::Format_RGBA8888);
  651. // /* 如果队列满,线程会阻塞在这里 */
  652. // m_queueRGBA.push(image);
  653. // // av_frame_unref(m_pFrameRGB);
  654. // // SPDLOG_DEBUG("一帧视频入队");
  655. // }
  656. /* 对生成的图像进行格式转换,并添加到环形队列中 */
  657. convertImageFormatAndPushToQueue(m_pFrameSRC);
  658. av_frame_unref(m_pFrameSRC);
  659. /* 如果在跳转过程中,直接退出,防止一个packet中有多个视频帧,再次阻塞在上面 */
  660. if(m_isSeek)
  661. {
  662. break;
  663. }
  664. }
  665. av_packet_unref(m_packet); /* 释放数据包,引用计数-1,为0时释放空间 */
  666. if(isEnd)
  667. {
  668. emit signal_playCompleted();
  669. m_decodeState.store(DecodeState::DecodeStop);
  670. /* 读取到结尾,但是不退出解码线程,可能还会使用倒退功能,后退读取 */
  671. while(m_decodeState.load() != DecodeState::DecodeRun)
  672. {
  673. std::this_thread::sleep_for(std::chrono::milliseconds(2));
  674. }
  675. isEnd = false;
  676. }
  677. }
  678. label_ThreadDecodeExit:
  679. /* 释放空间 */
  680. av_packet_free(&m_packet);
  681. m_decodeState.store(DecodeState::DecodeExit);
  682. m_threadRuning = false;
  683. }
  684. /* 退出线程,将所有可能暂停线程运行的条件全部唤醒 */
  685. void DecodeVedio::exitThread()
  686. {
  687. if(m_threadRuning)
  688. {
  689. m_threadRuning = false;
  690. }
  691. /* 设置成运行状态,唤醒可能阻塞在了解码结束的位置 */
  692. m_decodeState.store(DecodeState::DecodeRun);
  693. m_pauseDecode = false;
  694. /* 先退出可能阻塞住的解码线程 */
  695. m_queueFrame.exit();
  696. }
  697. /**
  698. * @brief 硬件解码线程,使用GPU解码,使用环境队列存储解码的数据
  699. *
  700. */
  701. void DecodeVedio::threadDecodeUsingGPU()
  702. {
  703. /******** 初始化局部变量 ********/
  704. bool isEnd = false;
  705. // int ret = 0;
  706. // int retFrame = 0;
  707. // int retPacket = 0;
  708. m_pauseDecode = false;
  709. m_decodeStatus = true;
  710. m_decodeState.store(DecodeState::DecodeRun);
  711. /* 开始解码 */
  712. SPDLOG_DEBUG("开始解码...");
  713. while(m_threadRuning)
  714. {
  715. /******** 判断是否在暂停状态 ********/
  716. while(m_pauseDecode)
  717. {
  718. m_decodeState.store(DecodeState::DecodePause);
  719. std::this_thread::sleep_for(std::chrono::microseconds(100));
  720. }
  721. m_decodeState.store(DecodeState::DecodeRun);
  722. /* 刷新解码器缓冲区,清除掉里面残留的解码文件 */
  723. if(m_flushDecoder.load())
  724. {
  725. avcodec_flush_buffers(m_pCodecContext);
  726. m_flushDecoder.store(false);
  727. }
  728. /******** 读取数据包 av_read_frame ********/
  729. int retRead = av_read_frame(m_pFormatContext, m_packet);
  730. if(retRead == AVERROR_EOF)
  731. {
  732. /* 读取到末尾后,需要传入一个空的packet,才能读取到最后几帧 */
  733. avcodec_send_packet(m_pCodecContext, m_packet);
  734. }
  735. else if(retRead < 0)
  736. {
  737. SPDLOG_ERROR("读取帧错误...");
  738. break;
  739. }
  740. else
  741. {
  742. if(m_packet->stream_index == m_videoStream)
  743. {
  744. // SPDLOG_DEBUG("源pts:{}", m_packet->pts);
  745. /* pts 表示显示时间戳(Presentation Timestamp),它指示解码后的帧应该在什么时候显示。pts 是用于同步音视频的关键时间戳。
  746. dts 表示解码时间戳(Decoding Timestamp),它指示解码器应该在什么时候解码这个数据包。dts 用于确保解码器按照正确的顺序解码数据包。 */
  747. #if 1
  748. /* 计算当前帧时间,两种方法,第一种适用于所有场景,但是存在一定的误差 */
  749. m_packet->pts = qRound64(m_packet->pts * (1000 * rationalToDouble(&m_pFormatContext->streams[m_videoStream]->time_base)));
  750. m_packet->dts = qRound64(m_packet->dts * (1000 * rationalToDouble(&m_pFormatContext->streams[m_videoStream]->time_base)));
  751. #else
  752. /* 适用于本地视频,直接计算本地视频文件的每一帧时间 */
  753. m_currentFrame++;
  754. // m_packet->pts = qRound64(m_currentFrame * (qreal(m_duration) / m_totalFrame));
  755. #endif
  756. /* 将数据传给解码器 */
  757. int ret = avcodec_send_packet(m_pCodecContext, m_packet);
  758. if(ret < 0)
  759. {
  760. SPDLOG_ERROR("发送数据包错误...");
  761. av_packet_unref(m_packet);
  762. continue;
  763. }
  764. }else {
  765. // SPDLOG_INFO("不是视频流。");
  766. av_packet_unref(m_packet);
  767. continue;
  768. }
  769. }
  770. // SPDLOG_DEBUG("读取到数据包packet,pts:{}",m_packet->pts);
  771. /* 解码packet包的内容,一个packet包内可能包含好多帧视频 */
  772. while(m_threadRuning)
  773. {
  774. /* 读取出解码器返回的帧 avcodec_receive_frame */
  775. int ret = avcodec_receive_frame(m_pCodecContext, m_pFrameSRC);
  776. if(ret == AVERROR_EOF)
  777. {
  778. SPDLOG_INFO("读取到视频流末尾。");
  779. isEnd = true;
  780. break;
  781. }
  782. else if (ret == AVERROR(EAGAIN))
  783. {
  784. /* packet中的内容无法解码成一帧,需要更多的输入包,可能已经取出了几帧,也肯能就不够一帧
  785. * 这时候就需要往解码器送数据包了 */
  786. // SPDLOG_WARN("packet无法解码成一帧,需要更多的输入包");
  787. av_frame_unref(m_pFrameSRC);
  788. break;
  789. }
  790. else if(ret < 0)
  791. {
  792. av_frame_unref(m_pFrameSRC);
  793. if(retRead < 0)
  794. {
  795. SPDLOG_ERROR("读取错误,错误值:{}",ret);
  796. break;
  797. }
  798. }
  799. /* 硬件解码,上面读取到的帧m_pFrameSRC->data[0] = nullptr
  800. * 数据要从GPU中拷贝出来 */
  801. if(!copyDataFromGPU(m_pFrameHW, m_pFrameSRC))
  802. {
  803. continue;
  804. }
  805. /* 解码成功,获取当前时间,现在已经是准的时间了
  806. * 如果在跳转状态,在这里判断是否到了目标位置 */
  807. m_pts = m_pFrameHW->pts;
  808. if(m_isSeek.load())
  809. {
  810. if(m_pts < m_targetPos)
  811. {
  812. SPDLOG_DEBUG("目标位置:{} 当前位置:{}",m_targetPos, m_pts.load());
  813. av_frame_unref(m_pFrameHW);
  814. continue;
  815. }else {
  816. m_isSeek = false;
  817. m_targetPos = -1;
  818. SPDLOG_INFO("跳转结束。");
  819. }
  820. }
  821. // SPDLOG_DEBUG("当前帧的pts:{}", m_pts.load());
  822. /* 转换解码后的帧格式,转换成RGBA格式,Qt可以识别 */
  823. // if(m_sws_ctx == nullptr)
  824. // {
  825. // /* 选择在这里创建,为了兼容硬件解码,硬件解码出来的格式可能和解码器传出的不一样 */
  826. // m_sws_ctx = sws_getCachedContext(m_sws_ctx,
  827. // m_pFrameHW->width, m_pFrameHW->height, /* 原图像大小和格式 */
  828. // m_pCodecContext->pix_fmt, /* 输入图像的像素格式 */
  829. // m_srcSize.width(), m_srcSize.height(), /* 目标图像的大小 */
  830. // AV_PIX_FMT_RGBA, /* 目标图像的格式 */
  831. // SWS_BILINEAR, /* 图像缩放算法,双线性 */
  832. // nullptr, /* 输入图像的滤波器信息,不需要传NULL */
  833. // nullptr, /* 输出图像的滤波器信息,不需要传NULL */
  834. // nullptr); /* 特定缩放算法需要的参数,不需要传NULL */
  835. // if(m_sws_ctx == nullptr)
  836. // {
  837. // SPDLOG_ERROR("创建SwsContext错误...");
  838. // goto label_ThreadDecodeExit;
  839. // }
  840. // SPDLOG_INFO("创建SwsContext成功...");
  841. // }
  842. // /* 转换成RGBA格式 */
  843. // // uint8_t* data[1] = { m_buffer };
  844. // int lines[4];
  845. // /* 使用像素格式pix_fmt和宽度填充图像的平面线条的大小(一行大小?) */
  846. // av_image_fill_linesizes(lines, AV_PIX_FMT_RGBA, m_pFrameHW->width);
  847. // sws_scale( m_sws_ctx, /* 缩放的上下文 */
  848. // m_pFrameHW->data, /* 源图像数组 */
  849. // m_pFrameHW->linesize, /* 包含源图像每个平面步幅的数组 */
  850. // 0, /* 开始位置 */
  851. // m_pFrameHW->height, /* 行数 */
  852. // &m_buffer, /* 目标图像数组 */
  853. // lines); /* 目标图像行数 */
  854. // if(m_buffer != nullptr)
  855. // {
  856. // /* 将数据拷贝到QImage中 */
  857. // auto image = new QImage(m_buffer, m_srcSize.width(), m_srcSize.height(), QImage::Format_RGBA8888);
  858. // /* 如果队列满,线程会阻塞在这里 */
  859. // m_queueRGBA.push(image);
  860. // // av_frame_unref(m_pFrameRGB);
  861. // // SPDLOG_DEBUG("一帧视频入队");
  862. // }
  863. convertImageFormatAndPushToQueue(m_pFrameHW);
  864. av_frame_unref(m_pFrameSRC);
  865. /* 如果在跳转过程中,直接退出,防止一个packet中有多个视频帧,再次阻塞在上面 */
  866. if(m_isSeek)
  867. {
  868. break;
  869. }
  870. }
  871. av_packet_unref(m_packet); /* 释放数据包,引用计数-1,为0时释放空间 */
  872. if(isEnd)
  873. {
  874. emit signal_playCompleted();
  875. m_decodeState.store(DecodeState::DecodeStop);
  876. /* 读取到结尾,但是不退出解码线程,可能还会使用倒退功能,后退读取 */
  877. while(m_decodeState.load() != DecodeState::DecodeRun)
  878. {
  879. std::this_thread::sleep_for(std::chrono::milliseconds(2));
  880. }
  881. isEnd = false;
  882. }
  883. }
  884. label_ThreadDecodeExit:
  885. /* 释放空间 */
  886. av_packet_free(&m_packet);
  887. m_decodeState.store(DecodeState::DecodeExit);
  888. m_threadRuning = false;
  889. }
  890. /* 对生成的图像进行格式转换,并添加到环形队列中 */
  891. void DecodeVedio::convertImageFormatAndPushToQueue(AVFrame* pFrame)
  892. {
  893. /* 判断视频帧格式 */
  894. if(pFrame->format == AV_PIX_FMT_YUV420P)
  895. {
  896. m_frameFormat = EFrameFormat::Frame_YUV420P;
  897. /* 是yuv420p格式, */
  898. Frame_YUV420P* frame = new Frame_YUV420P();
  899. convertYUV420PToImage(pFrame, frame->frameData);
  900. m_queueFrame.push(frame);
  901. } else
  902. {
  903. /* 不是420p,转换成RGBA格式 */
  904. m_frameFormat = EFrameFormat::Frame_RGBA8888;
  905. convertToRGBA(pFrame);
  906. if(m_buffer != nullptr)
  907. {
  908. Frame_RGBA8888* frame = new Frame_RGBA8888();
  909. /* 将数据拷贝到QImage中 */
  910. frame->frameData = new QImage(m_buffer, m_srcSize.width(), m_srcSize.height(), QImage::Format_RGBA8888);
  911. /* 如果队列满,线程会阻塞在这里 */
  912. m_queueFrame.push(frame);
  913. }
  914. }
  915. }
  916. /* 将yuv420p的uint8_t数组缓存转换成Image_YUV420P结构体 */
  917. void DecodeVedio::convertYUV420PToImage(AVFrame* pFrame, Image_YUV420P* image)
  918. {
  919. if(pFrame == nullptr || image == nullptr)
  920. {
  921. return;
  922. }
  923. if(pFrame->data[0] == nullptr || pFrame->data[1] == nullptr || pFrame->data[2] == nullptr)
  924. {
  925. return;
  926. }
  927. image->width = pFrame->width;
  928. image->height = pFrame->height;
  929. uint32_t ySzie = pFrame->width * pFrame->height; /* Y分量的大小 */
  930. image->yData.append(reinterpret_cast<const char*>(pFrame->data[0]), ySzie);
  931. image->uData.append(reinterpret_cast<const char*>(pFrame->data[1]), ySzie / 4);
  932. image->vData.append(reinterpret_cast<const char*>(pFrame->data[2]), ySzie / 4);
  933. }
  934. /* 转换成RGBA格式 */
  935. void DecodeVedio::convertToRGBA(AVFrame* pFrame)
  936. {
  937. /* 转换解码后的帧格式,转换成RGBA格式,Qt可以识别 */
  938. if(m_sws_ctx == nullptr)
  939. {
  940. /* 选择在这里创建,为了兼容硬件解码,硬件解码出来的格式可能和解码器传出的不一样 */
  941. m_sws_ctx = sws_getCachedContext(m_sws_ctx,
  942. pFrame->width, pFrame->height, /* 原图像大小和格式 */
  943. m_pCodecContext->pix_fmt, /* 输入图像的像素格式 */
  944. m_srcSize.width(), m_srcSize.height(), /* 目标图像的大小 */
  945. AV_PIX_FMT_RGBA, /* 目标图像的格式 */
  946. SWS_BILINEAR, /* 图像缩放算法,双线性 */
  947. nullptr, /* 输入图像的滤波器信息,不需要传NULL */
  948. nullptr, /* 输出图像的滤波器信息,不需要传NULL */
  949. nullptr); /* 特定缩放算法需要的参数,不需要传NULL */
  950. if(m_sws_ctx == nullptr)
  951. {
  952. SPDLOG_ERROR("创建SwsContext错误...");
  953. /* 释放空间 */
  954. av_packet_free(&m_packet);
  955. m_decodeState.store(DecodeState::DecodeExit);
  956. m_threadRuning = false;
  957. }
  958. SPDLOG_INFO("创建SwsContext成功...");
  959. }
  960. /* 转换成RGBA格式 */
  961. // uint8_t* data[1] = { m_buffer };
  962. int lines[4];
  963. /* 使用像素格式pix_fmt和宽度填充图像的平面线条的大小(一行大小?) */
  964. av_image_fill_linesizes(lines, AV_PIX_FMT_RGBA, pFrame->width);
  965. sws_scale( m_sws_ctx, /* 转换上下文 */
  966. pFrame->data, /* 源图像数据指针数组 */
  967. pFrame->linesize, /* 源图像每行字节数数组 */
  968. 0, /* 源图像起始行(通常为0) */
  969. pFrame->height, /* 处理的行数(通常为图像高度) */
  970. &m_buffer, /* 目标图像数据指针数组(packed格式只用[0]) */
  971. lines); /* 目标图像每行字节数数组 */
  972. }
  973. /* 暂停解码,会阻塞到线程暂停为止 */
  974. void DecodeVedio::pauseDecode()
  975. {
  976. if(!m_threadRuning)
  977. {
  978. return;
  979. }
  980. if( (m_decodeState.load() == DecodeState::DecodeExit)
  981. || (m_decodeState.load() == DecodeState::DecodePause) )
  982. {
  983. return;
  984. }
  985. /* 设置成运行状态,唤醒可能阻塞在了解码结束的位置 */
  986. m_decodeState.store(DecodeState::DecodeRun);
  987. m_pauseDecode = true;
  988. /* 队列出队两张图片,防止解码线程阻塞到环形队列满上面 */
  989. deleteOneImage();
  990. deleteOneImage();
  991. /* 等待线程状态变为暂停为止 */
  992. while (m_decodeState.load() != DecodeState::DecodePause)
  993. {
  994. std::this_thread::sleep_for(std::chrono::microseconds(100));
  995. }
  996. }
  997. /* 继续解码 */
  998. void DecodeVedio::continueDecode()
  999. {
  1000. m_pauseDecode = false;
  1001. m_decodeState.store(DecodeState::DecodeRun);
  1002. }
  1003. /**
  1004. * @brief AVRational存储的是分子和分母,这里相除转换为double,用于计算帧率
  1005. * 这个函数就等同于av_q2d()
  1006. * @param rational
  1007. * @return
  1008. */
  1009. qreal DecodeVedio::rationalToDouble(AVRational* rational)
  1010. {
  1011. return ( (rational->den == 0) ? 0 : (qreal(rational->num) / rational->den) );
  1012. }
  1013. /* 开启解码 */
  1014. void DecodeVedio::do_startDecodeVedio()
  1015. {
  1016. SPDLOG_DEBUG("解码线程ID:{}",QThread::currentThreadId());
  1017. // if(!m_initFFmpeg)
  1018. // {
  1019. // initFFmpeg();
  1020. // }
  1021. m_threadRuning = true;
  1022. m_pauseDecode = false;
  1023. /* 进入解码,直到播放完成或者手动退出 */
  1024. if(m_supportHWDecoder)
  1025. {
  1026. SPDLOG_TRACE("使用硬件解码器进行解码...");
  1027. threadDecodeUsingGPU();
  1028. }else
  1029. {
  1030. SPDLOG_INFO("使用CPU进行解码...");
  1031. threadDecodeUsingCPU();
  1032. }
  1033. SPDLOG_TRACE("Decode解码结束。");
  1034. }
  1035. /* 释放所有资源 */
  1036. void DecodeVedio::freeAll()
  1037. {
  1038. if(m_sws_ctx)
  1039. {
  1040. sws_freeContext(m_sws_ctx);
  1041. m_sws_ctx = nullptr;
  1042. }
  1043. // if(m_pFrameRGB)
  1044. // {
  1045. // av_frame_free(&m_pFrameRGB);
  1046. // }
  1047. if(m_pFrameSRC)
  1048. {
  1049. av_frame_free(&m_pFrameSRC);
  1050. }
  1051. if(m_pFrameHW)
  1052. {
  1053. av_frame_free(&m_pFrameHW);
  1054. }
  1055. if(m_packet)
  1056. {
  1057. av_packet_free(&m_packet);
  1058. }
  1059. if(m_pCodecContext)
  1060. {
  1061. avcodec_free_context(&m_pCodecContext);
  1062. }
  1063. if(m_pFormatContext)
  1064. {
  1065. avformat_close_input(&m_pFormatContext);
  1066. }
  1067. if(m_buffer)
  1068. {
  1069. av_free(m_buffer);
  1070. m_buffer = nullptr;
  1071. }
  1072. if(m_hw_device_ctx)
  1073. {
  1074. av_buffer_unref(&m_hw_device_ctx);
  1075. }
  1076. clearQueueFrame();
  1077. m_queueFrame.clearQueue();
  1078. }
  1079. /* 打印出错误信息 */
  1080. void DecodeVedio::printErrorStr(int errRet)
  1081. {
  1082. if(errRet < 0)
  1083. {
  1084. char errBuf[4096] = {0};
  1085. av_strerror(errRet, errBuf, sizeof(errBuf));
  1086. SPDLOG_WARN("错误信息:{}", errBuf);
  1087. }
  1088. }