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. 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. bool DecodeVedio::openVedio(const QString& fileName)
  314. {
  315. if(fileName.isEmpty())
  316. {
  317. SPDLOG_WARN("文件名为空...");
  318. return false;
  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. printErrorStr(ret);
  362. freeAll();
  363. return false;
  364. }
  365. /* 释放参数 */
  366. if(options != nullptr)
  367. {
  368. av_dict_free(&options);
  369. }
  370. /************ 找到视频流 ************/
  371. /* 检查视频容器内部的流信息,将所有流存储到了pFormatContext->streams中
  372. * 查找到视频流,并获取视频时长相关的信息 */
  373. ret = 0;
  374. ret = avformat_find_stream_info(m_pFormatContext, nullptr);
  375. if(ret < 0)
  376. {
  377. SPDLOG_WARN("获取视频流错误,错误代码:{}",ret);
  378. printErrorStr(ret);
  379. freeAll();
  380. return false;
  381. }
  382. m_duration = m_pFormatContext->duration / (AV_TIME_BASE / 1000); /* 获取视频时长,单位是毫秒 */
  383. m_startPos = m_pFormatContext->start_time / (AV_TIME_BASE / 1000); /* 获取视频开始时间,单位是毫秒 */
  384. // SPDLOG_DEBUG("开始时间:{} 时长:{}",m_startPos, m_duration);
  385. /* 一个调试函数,将流信息输出到控制台 */
  386. av_dump_format(m_pFormatContext, 0, m_fileName.toStdString().c_str(), 0);
  387. /* 找到视频流 */
  388. m_videoStream = av_find_best_stream(m_pFormatContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
  389. if(m_videoStream < 0)
  390. {
  391. SPDLOG_WARN("没有找到视频流");
  392. freeAll();
  393. return false;
  394. }
  395. SPDLOG_DEBUG("找到视频流");
  396. /* 获取视频流的编解码信息,主要是分辨率等信息 */
  397. AVStream *pStream = m_pFormatContext->streams[m_videoStream]; /* 获取视频流 */
  398. AVCodecParameters* pCodecParams = pStream->codecpar; /* 获取视频流的编解码信息 */
  399. SPDLOG_DEBUG("获取视频流参数成功!");
  400. /* 获取视频相关信息 */
  401. m_srcSize.setWidth(pCodecParams->width);
  402. m_srcSize.setHeight(pCodecParams->height);
  403. m_fps = rationalToDouble(&pStream->avg_frame_rate);
  404. m_totalFrame = m_pFormatContext->streams[m_videoStream]->nb_frames;
  405. m_pts.store(0);
  406. /************ 查找并设置解码器 ************/
  407. /* 找到解码器 */
  408. const AVCodec* pCodec = avcodec_find_decoder(pCodecParams->codec_id);
  409. if(pCodec == nullptr)
  410. {
  411. SPDLOG_WARN("没有找到解码器");
  412. freeAll();
  413. return false;
  414. }
  415. m_decoderName = pCodec->name;
  416. // SPDLOG_INFO("找到解码器:{}",pCodec->name);
  417. /* 获取视频信息的上下文,先分配空间,后面记得释放空间 */
  418. m_pCodecContext = avcodec_alloc_context3(pCodec);
  419. /* 将视频流中的编码器参数拷贝下来,这个函数不是线程安全的 */
  420. if(avcodec_parameters_to_context(m_pCodecContext, pCodecParams) != 0)
  421. {
  422. SPDLOG_WARN("复制上下文错误");
  423. freeAll();
  424. return false;
  425. }
  426. SPDLOG_DEBUG("设置解码器参数成功!");
  427. // m_pCodecContext->flags2 |= AV_CODEC_FLAG2_FAST; /* 使用快速解码(允许使用不符合规范的解码) */
  428. m_pCodecContext->thread_count = 8; /* 使用8线程解码 */
  429. /* 初始化硬件解码器 */
  430. if(m_supportHWDecoder)
  431. {
  432. initHWDecoder(pCodec);
  433. }
  434. /* 打开解码器,(初始化解码器上下文,如果调用了avcodec_alloc_context3,第二个参数可以设置为nullptr) */
  435. if(avcodec_open2(m_pCodecContext, nullptr, nullptr) < 0)
  436. {
  437. SPDLOG_ERROR("打开解码器错误");
  438. freeAll();
  439. return false;
  440. }
  441. SPDLOG_DEBUG("打开解码器成功!");
  442. /************ 初始化数据包 ************/
  443. m_packet = av_packet_alloc();
  444. av_new_packet(m_packet, m_pCodecContext->width * m_pCodecContext->height);
  445. /* 创建两个pFrame,一个存放原始数据,一个存放转换后的RGB数据 */
  446. m_pFrameSRC = av_frame_alloc();
  447. if(m_pFrameSRC == nullptr)
  448. {
  449. SPDLOG_ERROR("创建pFrame错误");
  450. freeAll();
  451. return false;
  452. }
  453. // m_pFrameRGB = av_frame_alloc();
  454. // if(m_pFrameRGB == nullptr)
  455. // {
  456. // SPDLOG_ERROR("创建pFrameRGB错误");
  457. // freeAll();
  458. // return;
  459. // }
  460. m_pFrameHW = av_frame_alloc();
  461. if(m_pFrameHW == nullptr)
  462. {
  463. SPDLOG_ERROR("创建pFrameHW错误");
  464. freeAll();
  465. return false;
  466. }
  467. if(m_buffer != nullptr)
  468. {
  469. av_free(m_buffer);
  470. m_buffer = nullptr;
  471. }
  472. /* 分配图像空间 */
  473. int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGBA, m_pCodecContext->width, m_pCodecContext->height, 1);
  474. /* 这里多分配了一些空间,防止某些视频图像在使用sws_scale()拷贝后超出数组长度 */
  475. m_buffer = (uint8_t *)av_malloc(numBytes + 1000);
  476. m_initFFmpeg = true;
  477. SPDLOG_INFO("FFMPEG初始化完成!");
  478. // SPDLOG_INFO("视频宽度:{} 高度:{} 帧率:{} 总时长:{} 总帧数:{}",m_srcSize.width(), m_srcSize.height(), m_fps, m_duration, m_totalFrame);
  479. /* 再次判断帧数是否正常,如果没读取到,就使用 总帧数 / 时长 */
  480. if(m_fps == 0)
  481. {
  482. if((m_duration > 0) && (m_totalFrame > 0))
  483. {
  484. m_fps = m_totalFrame / (m_duration / 1000.0);
  485. }
  486. /* 到这一步,无法确定帧数了,就按照25帧来计算了 */
  487. if(m_fps == 0)
  488. {
  489. m_fps = 25;
  490. }
  491. }
  492. return true;
  493. }
  494. /**
  495. * @brief 软解码线程,使用CPU解码,使用环境队列存储解码的数据
  496. *
  497. */
  498. void DecodeVedio::threadDecodeUsingCPU()
  499. {
  500. /******** 初始化局部变量 ********/
  501. bool isEnd = false;
  502. int ret = 0;
  503. int retFrame = 0;
  504. int retPacket = 0;
  505. m_pauseDecode = false;
  506. m_decodeStatus = true;
  507. m_decodeState.store(DecodeState::DecodeRun);
  508. /* 开始解码 */
  509. SPDLOG_DEBUG("开始解码...");
  510. while(m_threadRuning)
  511. {
  512. /******** 判断是否在暂停状态 ********/
  513. while(m_pauseDecode)
  514. {
  515. m_decodeState.store(DecodeState::DecodePause);
  516. std::this_thread::sleep_for(std::chrono::microseconds(100));
  517. }
  518. m_decodeState.store(DecodeState::DecodeRun);
  519. /* 刷新解码器缓冲区,清除掉里面残留的解码文件 */
  520. if(m_flushDecoder.load())
  521. {
  522. avcodec_flush_buffers(m_pCodecContext);
  523. m_flushDecoder.store(false);
  524. }
  525. /******** 读取数据包 av_read_frame ********/
  526. int retRead = av_read_frame(m_pFormatContext, m_packet);
  527. if(retRead == AVERROR_EOF)
  528. {
  529. /* 读取到末尾后,需要传入一个空的packet,才能读取到最后几帧 */
  530. avcodec_send_packet(m_pCodecContext, m_packet);
  531. }
  532. else if(retRead < 0)
  533. {
  534. SPDLOG_ERROR("读取帧错误...");
  535. break;
  536. }
  537. else
  538. {
  539. if(m_packet->stream_index == m_videoStream)
  540. {
  541. // SPDLOG_DEBUG("源pts:{}", m_packet->pts);
  542. /* pts 表示显示时间戳(Presentation Timestamp),它指示解码后的帧应该在什么时候显示。pts 是用于同步音视频的关键时间戳。
  543. dts 表示解码时间戳(Decoding Timestamp),它指示解码器应该在什么时候解码这个数据包。dts 用于确保解码器按照正确的顺序解码数据包。 */
  544. #if 1
  545. /* 计算当前帧时间,两种方法,第一种适用于所有场景,但是存在一定的误差 */
  546. m_packet->pts = qRound64(m_packet->pts * (1000 * rationalToDouble(&m_pFormatContext->streams[m_videoStream]->time_base)));
  547. m_packet->dts = qRound64(m_packet->dts * (1000 * rationalToDouble(&m_pFormatContext->streams[m_videoStream]->time_base)));
  548. #else
  549. /* 适用于本地视频,直接计算本地视频文件的每一帧时间 */
  550. m_currentFrame++;
  551. // m_packet->pts = qRound64(m_currentFrame * (qreal(m_duration) / m_totalFrame));
  552. #endif
  553. /* 将数据传给解码器 */
  554. int ret = avcodec_send_packet(m_pCodecContext, m_packet);
  555. if(ret < 0)
  556. {
  557. SPDLOG_ERROR("发送数据包错误...");
  558. av_packet_unref(m_packet);
  559. continue;
  560. }
  561. }else {
  562. // SPDLOG_INFO("不是视频流。");
  563. av_packet_unref(m_packet);
  564. continue;
  565. }
  566. }
  567. // SPDLOG_DEBUG("读取到数据包packet,pts:{}",m_packet->pts);
  568. /* 解码packet包的内容,一个packet包内可能包含好多帧视频 */
  569. while(m_threadRuning)
  570. {
  571. /* 读取出解码器返回的帧 avcodec_receive_frame */
  572. int ret = avcodec_receive_frame(m_pCodecContext, m_pFrameSRC);
  573. if(ret == AVERROR_EOF)
  574. {
  575. SPDLOG_INFO("读取到视频流末尾。");
  576. isEnd = true;
  577. break;
  578. }
  579. else if (ret == AVERROR(EAGAIN))
  580. {
  581. /* packet中的内容无法解码成一帧,需要更多的输入包,可能已经取出了几帧,也肯能就不够一帧
  582. * 这时候就需要往解码器送数据包了 */
  583. // SPDLOG_WARN("packet无法解码成一帧,需要更多的输入包");
  584. av_frame_unref(m_pFrameSRC);
  585. break;
  586. }
  587. else if(ret < 0)
  588. {
  589. av_frame_unref(m_pFrameSRC);
  590. if(retRead < 0)
  591. {
  592. SPDLOG_ERROR("读取错误,错误值:{}",ret);
  593. break;
  594. }
  595. }
  596. /* 解码成功,获取当前时间,现在已经是准的时间了
  597. * 如果在跳转状态,在这里判断是否到了目标位置 */
  598. m_pts = m_pFrameSRC->pts;
  599. if(m_isSeek.load())
  600. {
  601. if(m_pts < m_targetPos)
  602. {
  603. SPDLOG_DEBUG("目标位置:{} 当前位置:{}",m_targetPos, m_pts.load());
  604. av_frame_unref(m_pFrameSRC);
  605. continue;
  606. }else {
  607. m_isSeek = false;
  608. m_targetPos = -1;
  609. SPDLOG_INFO("跳转结束。");
  610. }
  611. }
  612. // SPDLOG_DEBUG("当前帧的pts:{}", m_pts.load());
  613. /* 转换解码后的帧格式,转换成RGBA格式,Qt可以识别 */
  614. if(m_sws_ctx == nullptr)
  615. {
  616. /* 选择在这里创建,为了兼容硬件解码,硬件解码出来的格式可能和解码器传出的不一样 */
  617. m_sws_ctx = sws_getCachedContext(m_sws_ctx,
  618. m_pFrameSRC->width, m_pFrameSRC->height, /* 原图像大小和格式 */
  619. m_pCodecContext->pix_fmt, /* 输入图像的像素格式 */
  620. m_srcSize.width(), m_srcSize.height(), /* 目标图像的大小 */
  621. AV_PIX_FMT_RGBA, /* 目标图像的格式 */
  622. SWS_BILINEAR, /* 图像缩放算法,双线性 */
  623. nullptr, /* 输入图像的滤波器信息,不需要传NULL */
  624. nullptr, /* 输出图像的滤波器信息,不需要传NULL */
  625. nullptr); /* 特定缩放算法需要的参数,不需要传NULL */
  626. if(m_sws_ctx == nullptr)
  627. {
  628. SPDLOG_ERROR("创建SwsContext错误...");
  629. goto label_ThreadDecodeExit;
  630. }
  631. SPDLOG_INFO("创建SwsContext成功...");
  632. }
  633. /* 转换成RGBA格式 */
  634. // uint8_t* data[1] = { m_buffer };
  635. int lines[4];
  636. /* 使用像素格式pix_fmt和宽度填充图像的平面线条的大小(一行大小?) */
  637. av_image_fill_linesizes(lines, AV_PIX_FMT_RGBA, m_pFrameSRC->width);
  638. sws_scale( m_sws_ctx, /* 缩放的上下文 */
  639. m_pFrameSRC->data, /* 源图像数组 */
  640. m_pFrameSRC->linesize, /* 包含源图像每个平面步幅的数组 */
  641. 0, /* 开始位置 */
  642. m_pFrameSRC->height, /* 行数 */
  643. &m_buffer, /* 目标图像数组 */
  644. lines); /* 目标图像行数 */
  645. if(m_buffer != nullptr)
  646. {
  647. /* 将数据拷贝到QImage中 */
  648. auto image = new QImage(m_buffer, m_srcSize.width(), m_srcSize.height(), QImage::Format_RGBA8888);
  649. /* 如果队列满,线程会阻塞在这里 */
  650. m_queueImage.push(image);
  651. // av_frame_unref(m_pFrameRGB);
  652. // SPDLOG_DEBUG("一帧视频入队");
  653. }
  654. av_frame_unref(m_pFrameSRC);
  655. /* 如果在跳转过程中,直接退出,防止一个packet中有多个视频帧,再次阻塞在上面 */
  656. if(m_isSeek)
  657. {
  658. break;
  659. }
  660. }
  661. av_packet_unref(m_packet); /* 释放数据包,引用计数-1,为0时释放空间 */
  662. if(isEnd)
  663. {
  664. emit signal_playCompleted();
  665. m_decodeState.store(DecodeState::DecodeStop);
  666. /* 读取到结尾,但是不退出解码线程,可能还会使用倒退功能,后退读取 */
  667. while(m_decodeState.load() != DecodeState::DecodeRun)
  668. {
  669. std::this_thread::sleep_for(std::chrono::milliseconds(2));
  670. }
  671. isEnd = false;
  672. }
  673. }
  674. label_ThreadDecodeExit:
  675. /* 释放空间 */
  676. av_packet_free(&m_packet);
  677. m_decodeState.store(DecodeState::DecodeExit);
  678. m_threadRuning = false;
  679. }
  680. /* 退出线程,将所有可能暂停线程运行的条件全部唤醒 */
  681. void DecodeVedio::exitThread()
  682. {
  683. if(m_threadRuning)
  684. {
  685. m_threadRuning = false;
  686. }
  687. /* 设置成运行状态,唤醒可能阻塞在了解码结束的位置 */
  688. m_decodeState.store(DecodeState::DecodeRun);
  689. m_pauseDecode = false;
  690. /* 先退出可能阻塞住的解码线程 */
  691. m_queueImage.exit();
  692. }
  693. /**
  694. * @brief 硬件解码线程,使用GPU解码,使用环境队列存储解码的数据
  695. *
  696. */
  697. void DecodeVedio::threadDecodeUsingGPU()
  698. {
  699. /******** 初始化局部变量 ********/
  700. bool isEnd = false;
  701. int ret = 0;
  702. int retFrame = 0;
  703. int retPacket = 0;
  704. m_pauseDecode = false;
  705. m_decodeStatus = true;
  706. m_decodeState.store(DecodeState::DecodeRun);
  707. /* 开始解码 */
  708. SPDLOG_DEBUG("开始解码...");
  709. while(m_threadRuning)
  710. {
  711. /******** 判断是否在暂停状态 ********/
  712. while(m_pauseDecode)
  713. {
  714. m_decodeState.store(DecodeState::DecodePause);
  715. std::this_thread::sleep_for(std::chrono::microseconds(100));
  716. }
  717. m_decodeState.store(DecodeState::DecodeRun);
  718. /* 刷新解码器缓冲区,清除掉里面残留的解码文件 */
  719. if(m_flushDecoder.load())
  720. {
  721. avcodec_flush_buffers(m_pCodecContext);
  722. m_flushDecoder.store(false);
  723. }
  724. /******** 读取数据包 av_read_frame ********/
  725. int retRead = av_read_frame(m_pFormatContext, m_packet);
  726. if(retRead == AVERROR_EOF)
  727. {
  728. /* 读取到末尾后,需要传入一个空的packet,才能读取到最后几帧 */
  729. avcodec_send_packet(m_pCodecContext, m_packet);
  730. }
  731. else if(retRead < 0)
  732. {
  733. SPDLOG_ERROR("读取帧错误...");
  734. break;
  735. }
  736. else
  737. {
  738. if(m_packet->stream_index == m_videoStream)
  739. {
  740. // SPDLOG_DEBUG("源pts:{}", m_packet->pts);
  741. /* pts 表示显示时间戳(Presentation Timestamp),它指示解码后的帧应该在什么时候显示。pts 是用于同步音视频的关键时间戳。
  742. dts 表示解码时间戳(Decoding Timestamp),它指示解码器应该在什么时候解码这个数据包。dts 用于确保解码器按照正确的顺序解码数据包。 */
  743. #if 1
  744. /* 计算当前帧时间,两种方法,第一种适用于所有场景,但是存在一定的误差 */
  745. m_packet->pts = qRound64(m_packet->pts * (1000 * rationalToDouble(&m_pFormatContext->streams[m_videoStream]->time_base)));
  746. m_packet->dts = qRound64(m_packet->dts * (1000 * rationalToDouble(&m_pFormatContext->streams[m_videoStream]->time_base)));
  747. #else
  748. /* 适用于本地视频,直接计算本地视频文件的每一帧时间 */
  749. m_currentFrame++;
  750. // m_packet->pts = qRound64(m_currentFrame * (qreal(m_duration) / m_totalFrame));
  751. #endif
  752. /* 将数据传给解码器 */
  753. int ret = avcodec_send_packet(m_pCodecContext, m_packet);
  754. if(ret < 0)
  755. {
  756. SPDLOG_ERROR("发送数据包错误...");
  757. av_packet_unref(m_packet);
  758. continue;
  759. }
  760. }else {
  761. // SPDLOG_INFO("不是视频流。");
  762. av_packet_unref(m_packet);
  763. continue;
  764. }
  765. }
  766. // SPDLOG_DEBUG("读取到数据包packet,pts:{}",m_packet->pts);
  767. /* 解码packet包的内容,一个packet包内可能包含好多帧视频 */
  768. while(m_threadRuning)
  769. {
  770. /* 读取出解码器返回的帧 avcodec_receive_frame */
  771. int ret = avcodec_receive_frame(m_pCodecContext, m_pFrameSRC);
  772. if(ret == AVERROR_EOF)
  773. {
  774. SPDLOG_INFO("读取到视频流末尾。");
  775. isEnd = true;
  776. break;
  777. }
  778. else if (ret == AVERROR(EAGAIN))
  779. {
  780. /* packet中的内容无法解码成一帧,需要更多的输入包,可能已经取出了几帧,也肯能就不够一帧
  781. * 这时候就需要往解码器送数据包了 */
  782. // SPDLOG_WARN("packet无法解码成一帧,需要更多的输入包");
  783. av_frame_unref(m_pFrameSRC);
  784. break;
  785. }
  786. else if(ret < 0)
  787. {
  788. av_frame_unref(m_pFrameSRC);
  789. if(retRead < 0)
  790. {
  791. SPDLOG_ERROR("读取错误,错误值:{}",ret);
  792. break;
  793. }
  794. }
  795. /* 硬件解码,上面读取到的帧m_pFrameSRC->data[0] = nullptr
  796. * 数据要从GPU中拷贝出来 */
  797. if(!copyDataFromGPU(m_pFrameHW, m_pFrameSRC))
  798. {
  799. continue;
  800. }
  801. /* 解码成功,获取当前时间,现在已经是准的时间了
  802. * 如果在跳转状态,在这里判断是否到了目标位置 */
  803. m_pts = m_pFrameHW->pts;
  804. if(m_isSeek.load())
  805. {
  806. if(m_pts < m_targetPos)
  807. {
  808. SPDLOG_DEBUG("目标位置:{} 当前位置:{}",m_targetPos, m_pts.load());
  809. av_frame_unref(m_pFrameHW);
  810. continue;
  811. }else {
  812. m_isSeek = false;
  813. m_targetPos = -1;
  814. SPDLOG_INFO("跳转结束。");
  815. }
  816. }
  817. // SPDLOG_DEBUG("当前帧的pts:{}", m_pts.load());
  818. /* 转换解码后的帧格式,转换成RGBA格式,Qt可以识别 */
  819. if(m_sws_ctx == nullptr)
  820. {
  821. /* 选择在这里创建,为了兼容硬件解码,硬件解码出来的格式可能和解码器传出的不一样 */
  822. m_sws_ctx = sws_getCachedContext(m_sws_ctx,
  823. m_pFrameHW->width, m_pFrameHW->height, /* 原图像大小和格式 */
  824. m_pCodecContext->pix_fmt, /* 输入图像的像素格式 */
  825. m_srcSize.width(), m_srcSize.height(), /* 目标图像的大小 */
  826. AV_PIX_FMT_RGBA, /* 目标图像的格式 */
  827. SWS_BILINEAR, /* 图像缩放算法,双线性 */
  828. nullptr, /* 输入图像的滤波器信息,不需要传NULL */
  829. nullptr, /* 输出图像的滤波器信息,不需要传NULL */
  830. nullptr); /* 特定缩放算法需要的参数,不需要传NULL */
  831. if(m_sws_ctx == nullptr)
  832. {
  833. SPDLOG_ERROR("创建SwsContext错误...");
  834. goto label_ThreadDecodeExit;
  835. }
  836. SPDLOG_INFO("创建SwsContext成功...");
  837. }
  838. /* 转换成RGBA格式 */
  839. // uint8_t* data[1] = { m_buffer };
  840. int lines[4];
  841. /* 使用像素格式pix_fmt和宽度填充图像的平面线条的大小(一行大小?) */
  842. av_image_fill_linesizes(lines, AV_PIX_FMT_RGBA, m_pFrameHW->width);
  843. sws_scale( m_sws_ctx, /* 缩放的上下文 */
  844. m_pFrameHW->data, /* 源图像数组 */
  845. m_pFrameHW->linesize, /* 包含源图像每个平面步幅的数组 */
  846. 0, /* 开始位置 */
  847. m_pFrameHW->height, /* 行数 */
  848. &m_buffer, /* 目标图像数组 */
  849. lines); /* 目标图像行数 */
  850. if(m_buffer != nullptr)
  851. {
  852. /* 将数据拷贝到QImage中 */
  853. auto image = new QImage(m_buffer, m_srcSize.width(), m_srcSize.height(), QImage::Format_RGBA8888);
  854. /* 如果队列满,线程会阻塞在这里 */
  855. m_queueImage.push(image);
  856. // av_frame_unref(m_pFrameRGB);
  857. // SPDLOG_DEBUG("一帧视频入队");
  858. }
  859. av_frame_unref(m_pFrameSRC);
  860. /* 如果在跳转过程中,直接退出,防止一个packet中有多个视频帧,再次阻塞在上面 */
  861. if(m_isSeek)
  862. {
  863. break;
  864. }
  865. }
  866. av_packet_unref(m_packet); /* 释放数据包,引用计数-1,为0时释放空间 */
  867. if(isEnd)
  868. {
  869. emit signal_playCompleted();
  870. m_decodeState.store(DecodeState::DecodeStop);
  871. /* 读取到结尾,但是不退出解码线程,可能还会使用倒退功能,后退读取 */
  872. while(m_decodeState.load() != DecodeState::DecodeRun)
  873. {
  874. std::this_thread::sleep_for(std::chrono::milliseconds(2));
  875. }
  876. isEnd = false;
  877. }
  878. }
  879. label_ThreadDecodeExit:
  880. /* 释放空间 */
  881. av_packet_free(&m_packet);
  882. m_decodeState.store(DecodeState::DecodeExit);
  883. m_threadRuning = false;
  884. }
  885. /* 暂停解码,会阻塞到线程暂停为止 */
  886. void DecodeVedio::pauseDecode()
  887. {
  888. if(!m_threadRuning)
  889. {
  890. return;
  891. }
  892. if( (m_decodeState.load() == DecodeState::DecodeExit)
  893. || (m_decodeState.load() == DecodeState::DecodePause) )
  894. {
  895. return;
  896. }
  897. /* 设置成运行状态,唤醒可能阻塞在了解码结束的位置 */
  898. m_decodeState.store(DecodeState::DecodeRun);
  899. m_pauseDecode = true;
  900. /* 队列出队两张图片,防止解码线程阻塞到环形队列满上面 */
  901. QImage* image = nullptr;
  902. m_queueImage.front_pop_NoBlock(image);
  903. if(image != nullptr)
  904. {
  905. delete image;
  906. image = nullptr;
  907. }
  908. m_queueImage.front_pop_NoBlock(image);
  909. if(image != nullptr)
  910. {
  911. delete image;
  912. image = nullptr;
  913. }
  914. /* 等待线程状态变为暂停为止 */
  915. while (m_decodeState.load() != DecodeState::DecodePause)
  916. {
  917. std::this_thread::sleep_for(std::chrono::microseconds(100));
  918. }
  919. }
  920. /* 继续解码 */
  921. void DecodeVedio::continueDecode()
  922. {
  923. m_pauseDecode = false;
  924. m_decodeState.store(DecodeState::DecodeRun);
  925. }
  926. /**
  927. * @brief AVRational存储的是分子和分母,这里相除转换为double,用于计算帧率
  928. * 这个函数就等同于av_q2d()
  929. * @param rational
  930. * @return
  931. */
  932. qreal DecodeVedio::rationalToDouble(AVRational* rational)
  933. {
  934. return ( (rational->den == 0) ? 0 : (qreal(rational->num) / rational->den) );
  935. }
  936. /* 开启解码 */
  937. void DecodeVedio::do_startDecodeVedio()
  938. {
  939. SPDLOG_DEBUG("解码线程ID:{}",QThread::currentThreadId());
  940. // if(!m_initFFmpeg)
  941. // {
  942. // initFFmpeg();
  943. // }
  944. m_threadRuning = true;
  945. m_pauseDecode = false;
  946. /* 进入解码,直到播放完成或者手动退出 */
  947. threadDecodeUsingCPU();
  948. SPDLOG_TRACE("Decode解码结束。");
  949. }
  950. /* 释放所有资源 */
  951. void DecodeVedio::freeAll()
  952. {
  953. if(m_sws_ctx)
  954. {
  955. sws_freeContext(m_sws_ctx);
  956. m_sws_ctx = nullptr;
  957. }
  958. // if(m_pFrameRGB)
  959. // {
  960. // av_frame_free(&m_pFrameRGB);
  961. // }
  962. if(m_pFrameSRC)
  963. {
  964. av_frame_free(&m_pFrameSRC);
  965. }
  966. if(m_pFrameHW)
  967. {
  968. av_frame_free(&m_pFrameHW);
  969. }
  970. if(m_packet)
  971. {
  972. av_packet_free(&m_packet);
  973. }
  974. if(m_pCodecContext)
  975. {
  976. avcodec_free_context(&m_pCodecContext);
  977. }
  978. if(m_pFormatContext)
  979. {
  980. avformat_close_input(&m_pFormatContext);
  981. }
  982. if(m_buffer)
  983. {
  984. av_free(m_buffer);
  985. m_buffer = nullptr;
  986. }
  987. if(m_hw_device_ctx)
  988. {
  989. av_buffer_unref(&m_hw_device_ctx);
  990. }
  991. for(int i = 0; i < m_queueImage.QueueSize(); i++)
  992. {
  993. QImage* image = nullptr;
  994. if (m_queueImage.front_pop_NoBlock(image))
  995. {
  996. delete image;
  997. }
  998. }
  999. m_queueImage.clearQueue();
  1000. }
  1001. /* 打印出错误信息 */
  1002. void DecodeVedio::printErrorStr(int errRet)
  1003. {
  1004. if(errRet < 0)
  1005. {
  1006. char errBuf[4096] = {0};
  1007. av_strerror(errRet, errBuf, sizeof(errBuf));
  1008. SPDLOG_WARN("错误信息:{}", errBuf);
  1009. }
  1010. }