DecodeVedio.cpp 37 KB

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