NetworkVideoDecode.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #include "NetworkVideoDecode.h"
  2. #include "videodecode.h"
  3. #include <QDebug>
  4. #include <QDir>
  5. #include <QImage>
  6. #include <QMutex>
  7. #include <qdatetime.h>
  8. extern "C" { // 用C规则编译指定的代码
  9. #include "libavcodec/avcodec.h"
  10. #include "libavformat/avformat.h"
  11. #include "libavutil/avutil.h"
  12. #include "libswscale/swscale.h"
  13. #include "libavutil/imgutils.h"
  14. }
  15. #define ERROR_LEN 1024 // 异常信息数组长度
  16. #define PRINT_LOG 1
  17. VideoDecode::VideoDecode()
  18. {
  19. // initFFmpeg(); // 5.1.2版本不需要调用了
  20. m_error = new char[ERROR_LEN];
  21. }
  22. VideoDecode::~VideoDecode()
  23. {
  24. close();
  25. }
  26. /**
  27. * @brief 初始化ffmpeg库(整个程序中只需加载一次)
  28. * 旧版本的ffmpeg需要注册各种文件格式、解复用器、对网络库进行全局初始化。
  29. * 在新版本的ffmpeg中纷纷弃用了,不需要注册了
  30. */
  31. void VideoDecode::initFFmpeg()
  32. {
  33. static bool isFirst = true;
  34. static QMutex mutex;
  35. QMutexLocker locker(&mutex);
  36. if(isFirst)
  37. {
  38. // av_register_all(); // 已经从源码中删除
  39. /**
  40. * 初始化网络库,用于打开网络流媒体,此函数仅用于解决旧GnuTLS或OpenSSL库的线程安全问题。
  41. * 一旦删除对旧GnuTLS和OpenSSL库的支持,此函数将被弃用,并且此函数不再有任何用途。
  42. */
  43. avformat_network_init();
  44. isFirst = false;
  45. }
  46. }
  47. /**
  48. * @brief 打开媒体文件,或者流媒体,例如rtmp、strp、http
  49. * @param url 视频地址
  50. * @return true:成功 false:失败
  51. */
  52. bool VideoDecode::open(const QString &url)
  53. {
  54. if(url.isNull()) return false;
  55. AVDictionary* dict = nullptr;
  56. av_dict_set(&dict, "rtsp_transport", "tcp", 0); // 设置rtsp流使用tcp打开,如果打开失败错误信息为【Error number -135 occurred】可以切换(UDP、tcp、udp_multicast、http),比如vlc推流就需要使用udp打开
  57. // av_dict_set(&dict, "max_delay", "3", 0); // 设置最大复用或解复用延迟(以微秒为单位)。当通过【UDP】 接收数据时,解复用器尝试重新排序接收到的数据包(因为它们可能无序到达,或者数据包可能完全丢失)。这可以通过将最大解复用延迟设置为零(通过max_delayAVFormatContext 字段)来禁用。
  58. // av_dict_set(&dict, "timeout", "1000000", 0); // 以微秒为单位设置套接字 TCP I/O 超时,如果等待时间过短,也可能会还没连接就返回了。
  59. // 打开输入流并返回解封装上下文
  60. int ret = avformat_open_input(&m_formatContext, // 返回解封装上下文
  61. url.toStdString().data(), // 打开视频地址
  62. nullptr, // 如果非null,此参数强制使用特定的输入格式。自动选择解封装器(文件格式)
  63. &dict); // 参数设置
  64. // 释放参数字典
  65. if(dict)
  66. {
  67. av_dict_free(&dict);
  68. }
  69. // 打开视频失败
  70. if(ret < 0)
  71. {
  72. showError(ret);
  73. free();
  74. return false;
  75. }
  76. // 读取媒体文件的数据包以获取流信息。
  77. ret = avformat_find_stream_info(m_formatContext, nullptr);
  78. if(ret < 0)
  79. {
  80. showError(ret);
  81. free();
  82. return false;
  83. }
  84. m_totalTime = m_formatContext->duration / (AV_TIME_BASE / 1000); // 计算视频总时长(毫秒)
  85. #if PRINT_LOG
  86. qDebug() << QString("视频总时长:%1 ms,[%2]").arg(m_totalTime).arg(QTime::fromMSecsSinceStartOfDay(int(m_totalTime)).toString("HH:mm:ss zzz"));
  87. #endif
  88. // 通过AVMediaType枚举查询视频流ID(也可以通过遍历查找),最后一个参数无用
  89. m_videoIndex = av_find_best_stream(m_formatContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
  90. if(m_videoIndex < 0)
  91. {
  92. showError(m_videoIndex);
  93. free();
  94. return false;
  95. }
  96. AVStream* videoStream = m_formatContext->streams[m_videoIndex]; // 通过查询到的索引获取视频流
  97. // 获取视频图像分辨率(AVStream中的AVCodecContext在新版本中弃用,改为使用AVCodecParameters)
  98. m_size.setWidth(videoStream->codecpar->width);
  99. m_size.setHeight(videoStream->codecpar->height);
  100. m_frameRate = rationalToDouble(&videoStream->avg_frame_rate); // 视频帧率
  101. // 通过解码器ID获取视频解码器(新版本返回值必须使用const)
  102. const AVCodec* codec = avcodec_find_decoder(videoStream->codecpar->codec_id);
  103. m_totalFrames = videoStream->nb_frames;
  104. m_strCodecName = codec->name;
  105. #if PRINT_LOG
  106. qDebug() << QString("分辨率:[w:%1,h:%2] 帧率:%3 总帧数:%4 解码器:%5")
  107. .arg(m_size.width()).arg(m_size.height()).arg(m_frameRate).arg(m_totalFrames).arg(codec->name);
  108. #endif
  109. // 分配AVCodecContext并将其字段设置为默认值。
  110. m_codecContext = avcodec_alloc_context3(codec);
  111. if(!m_codecContext)
  112. {
  113. #if PRINT_LOG
  114. qWarning() << "创建视频解码器上下文失败!";
  115. #endif
  116. free();
  117. return false;
  118. }
  119. // 使用视频流的codecpar为解码器上下文赋值
  120. ret = avcodec_parameters_to_context(m_codecContext, videoStream->codecpar);
  121. if(ret < 0)
  122. {
  123. showError(ret);
  124. free();
  125. return false;
  126. }
  127. m_codecContext->flags2 |= AV_CODEC_FLAG2_FAST; // 允许不符合规范的加速技巧。
  128. m_codecContext->thread_count = 8; // 使用8线程解码
  129. // 初始化解码器上下文,如果之前avcodec_alloc_context3传入了解码器,这里设置NULL就可以
  130. ret = avcodec_open2(m_codecContext, nullptr, nullptr);
  131. if(ret < 0)
  132. {
  133. showError(ret);
  134. free();
  135. return false;
  136. }
  137. // 分配AVPacket并将其字段设置为默认值。
  138. m_packet = av_packet_alloc();
  139. if(!m_packet)
  140. {
  141. #if PRINT_LOG
  142. qWarning() << "av_packet_alloc() Error!";
  143. #endif
  144. free();
  145. return false;
  146. }
  147. // 分配AVFrame并将其字段设置为默认值。
  148. m_frame = av_frame_alloc();
  149. if(!m_frame)
  150. {
  151. #if PRINT_LOG
  152. qWarning() << "av_frame_alloc() Error!";
  153. #endif
  154. free();
  155. return false;
  156. }
  157. // 分配图像空间
  158. int size = av_image_get_buffer_size(AV_PIX_FMT_RGBA, m_size.width(), m_size.height(), 4);
  159. /**
  160. * 【注意:】这里可以多分配一些,否则如果只是安装size分配,大部分视频图像数据拷贝没有问题,
  161. * 但是少部分视频图像在使用sws_scale()拷贝时会超出数组长度,在使用使用msvc debug模式时delete[] m_buffer会报错(HEAP CORRUPTION DETECTED: after Normal block(#32215) at 0x000001AC442830370.CRT delected that the application wrote to memory after end of heap buffer)
  162. * 特别是这个视频流http://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4
  163. */
  164. m_buffer = new uchar[size + 1000]; // 这里多分配1000个字节就基本不会出现拷贝超出的情况了,反正不缺这点内存
  165. // m_image = new QImage(m_buffer, m_size.width(), m_size.height(), QImage::Format_RGBA8888); // 这种方式分配内存大部分情况下也可以,但是因为存在拷贝超出数组的情况,delete时也会报错
  166. m_end = false;
  167. return openSave();
  168. }
  169. /**
  170. * @brief
  171. * @return
  172. */
  173. QImage VideoDecode::read()
  174. {
  175. // 如果没有打开则返回
  176. if(!m_formatContext)
  177. {
  178. return QImage();
  179. }
  180. // 读取下一帧数据
  181. int readRet = av_read_frame(m_formatContext, m_packet);
  182. if(readRet < 0)
  183. {
  184. avcodec_send_packet(m_codecContext, m_packet); // 读取完成后向解码器中传如空AVPacket,否则无法读取出最后几帧
  185. }
  186. else
  187. {
  188. if(m_packet->stream_index == m_videoIndex) // 如果是图像数据则进行解码
  189. {
  190. if(m_formatContextSave)
  191. {
  192. // 由于保存的m_formatContextSave只创建了一个视频流,而读取到的图像的流索引不一定为0,可能会出现错误【Invalid packet stream index: 1】
  193. // 所以这里需要将stream_index指定为和m_formatContextSave中视频流索引相同,因为就一个流,所以直接设置为0
  194. m_packet->stream_index = 0;
  195. av_write_frame(m_formatContextSave, m_packet); // 将数据包写入输出媒体文件
  196. }
  197. // 计算当前帧时间(毫秒)
  198. #if 1 // 方法一:适用于所有场景,但是存在一定误差
  199. m_packet->pts = qRound64(m_packet->pts * (1000 * rationalToDouble(&m_formatContext->streams[m_videoIndex]->time_base)));
  200. m_packet->dts = qRound64(m_packet->dts * (1000 * rationalToDouble(&m_formatContext->streams[m_videoIndex]->time_base)));
  201. #else // 方法二:适用于播放本地视频文件,计算每一帧时间较准,但是由于网络视频流无法获取总帧数,所以无法适用
  202. m_obtainFrames++;
  203. m_packet->pts = qRound64(m_obtainFrames * (qreal(m_totalTime) / m_totalFrames));
  204. #endif
  205. // 将读取到的原始数据包传入解码器
  206. int ret = avcodec_send_packet(m_codecContext, m_packet);
  207. if(ret < 0)
  208. {
  209. showError(ret);
  210. }
  211. }
  212. }
  213. av_packet_unref(m_packet); // 释放数据包,引用计数-1,为0时释放空间
  214. int ret = avcodec_receive_frame(m_codecContext, m_frame);
  215. if(ret < 0)
  216. {
  217. av_frame_unref(m_frame);
  218. if(readRet < 0)
  219. {
  220. m_end = true; // 当无法读取到AVPacket并且解码器中也没有数据时表示读取完成
  221. }
  222. return QImage();
  223. }
  224. m_pts = m_frame->pts;
  225. // 为什么图像转换上下文要放在这里初始化呢,是因为m_frame->format,如果使用硬件解码,解码出来的图像格式和m_codecContext->pix_fmt的图像格式不一样,就会导致无法转换为QImage
  226. if(!m_swsContext)
  227. {
  228. // 获取缓存的图像转换上下文。首先校验参数是否一致,如果校验不通过就释放资源;然后判断上下文是否存在,如果存在直接复用,如不存在进行分配、初始化操作
  229. m_swsContext = sws_getCachedContext(m_swsContext,
  230. m_frame->width, // 输入图像的宽度
  231. m_frame->height, // 输入图像的高度
  232. (AVPixelFormat)m_frame->format, // 输入图像的像素格式
  233. m_size.width(), // 输出图像的宽度
  234. m_size.height(), // 输出图像的高度
  235. AV_PIX_FMT_RGBA, // 输出图像的像素格式
  236. SWS_BILINEAR, // 选择缩放算法(只有当输入输出图像大小不同时有效),一般选择SWS_FAST_BILINEAR
  237. nullptr, // 输入图像的滤波器信息, 若不需要传NULL
  238. nullptr, // 输出图像的滤波器信息, 若不需要传NULL
  239. nullptr); // 特定缩放算法需要的参数(?),默认为NULL
  240. if(!m_swsContext)
  241. {
  242. #if PRINT_LOG
  243. qWarning() << "sws_getCachedContext() Error!";
  244. #endif
  245. free();
  246. return QImage();
  247. }
  248. }
  249. // AVFrame转QImage
  250. uchar* data[] = {m_buffer};
  251. int lines[4];
  252. av_image_fill_linesizes(lines, AV_PIX_FMT_RGBA, m_frame->width); // 使用像素格式pix_fmt和宽度填充图像的平面线条大小。
  253. ret = sws_scale(m_swsContext, // 缩放上下文
  254. m_frame->data, // 原图像数组
  255. m_frame->linesize, // 包含源图像每个平面步幅的数组
  256. 0, // 开始位置
  257. m_frame->height, // 行数
  258. data, // 目标图像数组
  259. lines); // 包含目标图像每个平面的步幅的数组
  260. QImage image(m_buffer, m_frame->width, m_frame->height, QImage::Format_RGBA8888);
  261. av_frame_unref(m_frame);
  262. return image;
  263. }
  264. /**
  265. * @brief 关闭视频播放并释放内存
  266. */
  267. void VideoDecode::close()
  268. {
  269. clear();
  270. free();
  271. m_totalTime = 0;
  272. m_videoIndex = 0;
  273. m_totalFrames = 0;
  274. m_obtainFrames = 0;
  275. m_pts = 0;
  276. m_frameRate = 0;
  277. m_size = QSize(0, 0);
  278. }
  279. /**
  280. * @brief 视频是否读取完成
  281. * @return
  282. */
  283. bool VideoDecode::isEnd()
  284. {
  285. return m_end;
  286. }
  287. /**
  288. * @brief 返回当前帧图像播放时间
  289. * @return
  290. */
  291. const qint64 &VideoDecode::pts()
  292. {
  293. return m_pts;
  294. }
  295. /**
  296. * @brief 显示ffmpeg函数调用异常信息
  297. * @param err
  298. */
  299. void VideoDecode::showError(int err)
  300. {
  301. #if PRINT_LOG
  302. memset(m_error, 0, ERROR_LEN); // 将数组置零
  303. av_strerror(err, m_error, ERROR_LEN);
  304. qWarning() << "DecodeVideo Error:" << m_error;
  305. #else
  306. Q_UNUSED(err)
  307. #endif
  308. }
  309. /**
  310. * @brief 将AVRational转换为double,用于计算帧率
  311. * @param rational
  312. * @return
  313. */
  314. qreal VideoDecode::rationalToDouble(AVRational* rational)
  315. {
  316. qreal frameRate = (rational->den == 0) ? 0 : (qreal(rational->num) / rational->den);
  317. return frameRate;
  318. }
  319. /**
  320. * @brief 清空读取缓冲
  321. */
  322. void VideoDecode::clear()
  323. {
  324. if(m_formatContextSave && m_writeHeader)
  325. {
  326. av_write_trailer(m_formatContextSave); // 写入文件尾
  327. m_writeHeader = false;
  328. avformat_free_context(m_formatContextSave);
  329. m_formatContext = nullptr;
  330. m_videoStream = nullptr;
  331. }
  332. // 因为avformat_flush不会刷新AVIOContext (s->pb)。如果有必要,在调用此函数之前调用avio_flush(s->pb)。
  333. if(m_formatContext && m_formatContext->pb)
  334. {
  335. avio_flush(m_formatContext->pb);
  336. }
  337. if(m_formatContext)
  338. {
  339. avformat_flush(m_formatContext); // 清理读取缓冲
  340. }
  341. }
  342. void VideoDecode::free()
  343. {
  344. // 释放上下文swsContext。
  345. if(m_swsContext)
  346. {
  347. sws_freeContext(m_swsContext);
  348. m_swsContext = nullptr; // sws_freeContext不会把上下文置NULL
  349. }
  350. // 释放编解码器上下文和与之相关的所有内容,并将NULL写入提供的指针
  351. if(m_codecContext)
  352. {
  353. avcodec_free_context(&m_codecContext);
  354. }
  355. // 关闭并失败m_formatContext,并将指针置为null
  356. if(m_formatContext)
  357. {
  358. avformat_close_input(&m_formatContext);
  359. }
  360. if(m_packet)
  361. {
  362. av_packet_free(&m_packet);
  363. }
  364. if(m_frame)
  365. {
  366. av_frame_free(&m_frame);
  367. }
  368. if(m_buffer)
  369. {
  370. delete [] m_buffer;
  371. m_buffer = nullptr;
  372. }
  373. }
  374. /**
  375. * @brief 打开输出文件
  376. * @return
  377. */
  378. bool VideoDecode::openSave()
  379. {
  380. QDir dir;
  381. if(!dir.exists("./Videos"))
  382. {
  383. dir.mkdir("./Videos");
  384. }
  385. QString strName = QString("./Videos/%1.h264").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH-mm-ss"));
  386. int ret = avformat_alloc_output_context2(&m_formatContextSave, nullptr, m_strCodecName.toStdString().data(), strName.toStdString().data()); // 这里使用和解码一样的编码器,防止保存的图像颜色出问题
  387. if(ret < 0)
  388. {
  389. free();
  390. showError(ret);
  391. return false;
  392. }
  393. // 创建并初始化AVIOContext以访问url所指示的资源。
  394. ret = avio_open(&m_formatContextSave->pb, strName.toStdString().data(), AVIO_FLAG_WRITE);
  395. if(ret < 0)
  396. {
  397. free();
  398. showError(ret);
  399. return false;
  400. }
  401. // 向媒体文件添加新流
  402. m_videoStream = avformat_new_stream(m_formatContextSave, nullptr);
  403. if(!m_videoStream)
  404. {
  405. free();
  406. showError(AVERROR(ENOMEM));
  407. return false;
  408. }
  409. //拷贝一些参数,给codecpar赋值(这里使用编码器上下文进行赋值)
  410. ret = avcodec_parameters_from_context(m_videoStream->codecpar, m_codecContext);
  411. if(ret < 0)
  412. {
  413. free();
  414. showError(ret);
  415. return false;
  416. }
  417. // 写入文件头
  418. ret = avformat_write_header(m_formatContextSave, nullptr);
  419. if(ret < 0)
  420. {
  421. free();
  422. showError(ret);
  423. return false;
  424. }
  425. m_writeHeader = true;
  426. qDebug() << "开始录制视频!";
  427. return true;
  428. }