DecodeVedio.cpp 38 KB

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