DecodeVedio.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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. * @brief 对生成的图像进行格式转换,并添加到环形队列中
  892. * 1、YUVJ420P格式是全色YUV格式,范围是[0-255],而YUV420P格式是限色YUV格式,范围是[16-235]。
  893. * 2、判断视频格式,如果是RGB类型的转换成RGBA格式,如果是yuv类型的,转换成YUVJ420P格式
  894. * @param pFrame
  895. */
  896. void DecodeVedio::convertImageFormatAndPushToQueue(AVFrame* pFrame)
  897. {
  898. /* 判断视频帧格式 */
  899. if((pFrame->format == AV_PIX_FMT_YUV420P) || (pFrame->format == AV_PIX_FMT_YUVJ420P))
  900. {
  901. /* 是yuv420p格式, */
  902. Frame_YUV420P* frame = new Frame_YUV420P();
  903. convertToYUV420P(pFrame, frame->frameData);
  904. m_queueFrame.push(frame);
  905. return;
  906. }
  907. /* 判断视频格式,如果是RGB类型的转换成RGBA格式,如果是yuv类型的,转换成YUVJ420P格式 */
  908. /* 不是420p,转换成RGBA格式 */
  909. Frame_RGBA8888* frame = new Frame_RGBA8888();
  910. convertToRGBA(pFrame, frame->frameData);
  911. if(frame->frameData == nullptr)
  912. {
  913. SPDLOG_ERROR("转换成RGBA格式失败,可能是解码器不支持该格式: {}", av_get_pix_fmt_name((AVPixelFormat)pFrame->format));
  914. delete frame; /* 释放空间 */
  915. return;
  916. }
  917. /* 如果队列满,线程会阻塞在这里 */
  918. m_queueFrame.push(frame);
  919. }
  920. /* 将yuv420p的uint8_t数组缓存转换成Image_YUV420P结构体 */
  921. void DecodeVedio::convertToYUV420P(AVFrame* pFrame, Image_YUV420P* image)
  922. {
  923. if(pFrame == nullptr || image == nullptr)
  924. {
  925. return;
  926. }
  927. if(pFrame->data[0] == nullptr || pFrame->data[1] == nullptr || pFrame->data[2] == nullptr)
  928. {
  929. return;
  930. }
  931. image->width = pFrame->width;
  932. image->height = pFrame->height;
  933. uint32_t ySzie = pFrame->width * pFrame->height; /* Y分量的大小 */
  934. image->yData.append(reinterpret_cast<const char*>(pFrame->data[0]), ySzie);
  935. image->uData.append(reinterpret_cast<const char*>(pFrame->data[1]), ySzie / 4);
  936. image->vData.append(reinterpret_cast<const char*>(pFrame->data[2]), ySzie / 4);
  937. }
  938. /* 转换成RGBA格式 */
  939. void DecodeVedio::convertToRGBA(AVFrame* pFrame, QImage* image)
  940. {
  941. /* 转换解码后的帧格式,转换成RGBA格式,Qt可以识别 */
  942. if(m_sws_ctx == nullptr)
  943. {
  944. /* 选择在这里创建,为了兼容硬件解码,硬件解码出来的格式可能和解码器传出的不一样 */
  945. m_sws_ctx = sws_getCachedContext(m_sws_ctx,
  946. pFrame->width, pFrame->height, /* 原图像大小和格式 */
  947. m_pCodecContext->pix_fmt, /* 输入图像的像素格式 */
  948. m_srcSize.width(), m_srcSize.height(), /* 目标图像的大小 */
  949. AV_PIX_FMT_RGBA, /* 目标图像的格式 */
  950. SWS_BILINEAR, /* 图像缩放算法,双线性 */
  951. nullptr, /* 输入图像的滤波器信息,不需要传NULL */
  952. nullptr, /* 输出图像的滤波器信息,不需要传NULL */
  953. nullptr); /* 特定缩放算法需要的参数,不需要传NULL */
  954. if(m_sws_ctx == nullptr)
  955. {
  956. SPDLOG_ERROR("创建SwsContext错误...");
  957. /* 释放空间 */
  958. av_packet_free(&m_packet);
  959. m_decodeState.store(DecodeState::DecodeExit);
  960. m_threadRuning = false;
  961. }
  962. SPDLOG_INFO("创建SwsContext成功...");
  963. }
  964. /* 转换成RGBA格式 */
  965. // uint8_t* data[1] = { m_buffer };
  966. int lines[4];
  967. /* 使用像素格式pix_fmt和宽度填充图像的平面线条的大小(一行大小?) */
  968. av_image_fill_linesizes(lines, AV_PIX_FMT_RGBA, pFrame->width);
  969. sws_scale( m_sws_ctx, /* 转换上下文 */
  970. pFrame->data, /* 源图像数据指针数组 */
  971. pFrame->linesize, /* 源图像每行字节数数组 */
  972. 0, /* 源图像起始行(通常为0) */
  973. pFrame->height, /* 处理的行数(通常为图像高度) */
  974. &m_buffer, /* 目标图像数据指针数组(packed格式只用[0]) */
  975. lines); /* 目标图像每行字节数数组 */
  976. if(m_buffer != nullptr)
  977. {
  978. /* 将数据拷贝到QImage中 */
  979. *image = QImage(m_buffer, m_srcSize.width(), m_srcSize.height(), QImage::Format_RGBA8888);
  980. }
  981. }
  982. /* 暂停解码,会阻塞到线程暂停为止 */
  983. void DecodeVedio::pauseDecode()
  984. {
  985. if(!m_threadRuning)
  986. {
  987. return;
  988. }
  989. if( (m_decodeState.load() == DecodeState::DecodeExit)
  990. || (m_decodeState.load() == DecodeState::DecodePause) )
  991. {
  992. return;
  993. }
  994. /* 设置成运行状态,唤醒可能阻塞在了解码结束的位置 */
  995. m_decodeState.store(DecodeState::DecodeRun);
  996. m_pauseDecode = true;
  997. /* 队列出队两张图片,防止解码线程阻塞到环形队列满上面 */
  998. deleteOneImage();
  999. deleteOneImage();
  1000. /* 等待线程状态变为暂停为止 */
  1001. while (m_decodeState.load() != DecodeState::DecodePause)
  1002. {
  1003. std::this_thread::sleep_for(std::chrono::microseconds(100));
  1004. }
  1005. }
  1006. /* 继续解码 */
  1007. void DecodeVedio::continueDecode()
  1008. {
  1009. m_pauseDecode = false;
  1010. m_decodeState.store(DecodeState::DecodeRun);
  1011. }
  1012. /**
  1013. * @brief AVRational存储的是分子和分母,这里相除转换为double,用于计算帧率
  1014. * 这个函数就等同于av_q2d()
  1015. * @param rational
  1016. * @return
  1017. */
  1018. qreal DecodeVedio::rationalToDouble(AVRational* rational)
  1019. {
  1020. return ( (rational->den == 0) ? 0 : (qreal(rational->num) / rational->den) );
  1021. }
  1022. /* 开启解码 */
  1023. void DecodeVedio::do_startDecodeVedio()
  1024. {
  1025. SPDLOG_DEBUG("解码线程ID:{}",QThread::currentThreadId());
  1026. // if(!m_initFFmpeg)
  1027. // {
  1028. // initFFmpeg();
  1029. // }
  1030. m_threadRuning = true;
  1031. m_pauseDecode = false;
  1032. /* 进入解码,直到播放完成或者手动退出 */
  1033. if(m_supportHWDecoder)
  1034. {
  1035. SPDLOG_TRACE("使用硬件解码器进行解码...");
  1036. threadDecodeUsingGPU();
  1037. }else
  1038. {
  1039. SPDLOG_INFO("使用CPU进行解码...");
  1040. threadDecodeUsingCPU();
  1041. }
  1042. SPDLOG_TRACE("Decode解码结束。");
  1043. }
  1044. /* 释放所有资源 */
  1045. void DecodeVedio::freeAll()
  1046. {
  1047. if(m_sws_ctx)
  1048. {
  1049. sws_freeContext(m_sws_ctx);
  1050. m_sws_ctx = nullptr;
  1051. }
  1052. // if(m_pFrameRGB)
  1053. // {
  1054. // av_frame_free(&m_pFrameRGB);
  1055. // }
  1056. if(m_pFrameSRC)
  1057. {
  1058. av_frame_free(&m_pFrameSRC);
  1059. }
  1060. if(m_pFrameHW)
  1061. {
  1062. av_frame_free(&m_pFrameHW);
  1063. }
  1064. if(m_packet)
  1065. {
  1066. av_packet_free(&m_packet);
  1067. }
  1068. if(m_pCodecContext)
  1069. {
  1070. avcodec_free_context(&m_pCodecContext);
  1071. }
  1072. if(m_pFormatContext)
  1073. {
  1074. avformat_close_input(&m_pFormatContext);
  1075. }
  1076. if(m_buffer)
  1077. {
  1078. av_free(m_buffer);
  1079. m_buffer = nullptr;
  1080. }
  1081. if(m_hw_device_ctx)
  1082. {
  1083. av_buffer_unref(&m_hw_device_ctx);
  1084. }
  1085. clearQueueFrame();
  1086. m_queueFrame.clearQueue();
  1087. }
  1088. /* 打印出错误信息 */
  1089. void DecodeVedio::printErrorStr(int errRet)
  1090. {
  1091. if(errRet < 0)
  1092. {
  1093. char errBuf[4096] = {0};
  1094. av_strerror(errRet, errBuf, sizeof(errBuf));
  1095. SPDLOG_WARN("错误信息:{}", errBuf);
  1096. }
  1097. }