video_decode_thread.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // ***********************************************************/
  2. // video_decode_thread.cpp
  3. //
  4. // Copy Right @ Steven Huang. All rights reserved.
  5. //
  6. // Video decode thread. This section includes queues
  7. // and dxva2 hardware transmit decoded frame.
  8. // ***********************************************************/
  9. #include "video_decode_thread.h"
  10. #include <QLoggingCategory>
  11. Q_LOGGING_CATEGORY(playerControllerVideoDecodeThread, "player.controller.VideoDecodeThread")
  12. VideoDecodeThread::VideoDecodeThread(VideoState* pState)
  13. : m_pState(pState)
  14. {}
  15. VideoDecodeThread::~VideoDecodeThread() {}
  16. void VideoDecodeThread::stop()
  17. {
  18. *m_exit = true;
  19. m_cv.notify_all();
  20. }
  21. void VideoDecodeThread::run()
  22. {
  23. qCDebug(playerControllerVideoDecodeThread)
  24. << "[VideoDecodeThread] run start, m_pState:" << (void*) m_pState;
  25. assert(m_pState);
  26. VideoState* is = m_pState;
  27. qCDebug(playerControllerVideoDecodeThread)
  28. << "[VideoDecodeThread] VideoState* is:" << (void*) is
  29. << ", abort_request:" << is->abort_request;
  30. AVFrame* frame = av_frame_alloc();
  31. AVFrame* sw_frame = av_frame_alloc();
  32. AVFrame* tmp_frame = nullptr;
  33. double pts;
  34. double duration;
  35. int ret;
  36. AVRational tb = is->video_st->time_base;
  37. AVRational frame_rate = av_guess_frame_rate(is->ic, is->video_st, nullptr);
  38. if (!frame) {
  39. qCWarning(playerControllerVideoDecodeThread)
  40. << "[VideoDecodeThread] av_frame_alloc failed!";
  41. return;
  42. }
  43. int loop_count = 0;
  44. for (;;) {
  45. qCDebug(playerControllerVideoDecodeThread)
  46. << "[VideoDecodeThread] loop start, loop_count:" << loop_count
  47. << ", m_exit:" << (m_exit ? m_exit->load() : -1)
  48. << ", abort_request:" << is->abort_request;
  49. if (isExit()) {
  50. qCDebug(playerControllerVideoDecodeThread) << "[VideoDecodeThread] m_exit set, exit.";
  51. break;
  52. }
  53. if (is->abort_request) {
  54. qCDebug(playerControllerVideoDecodeThread)
  55. << "[VideoDecodeThread] abort_request set, exit.";
  56. break;
  57. }
  58. qCDebug(playerControllerVideoDecodeThread)
  59. << "[VideoDecodeThread] call get_video_frame, loop:" << loop_count;
  60. ret = get_video_frame(is, frame);
  61. qCDebug(playerControllerVideoDecodeThread)
  62. << "[VideoDecodeThread] get_video_frame ret:" << ret << ", loop:" << loop_count++
  63. << ", m_exit:" << (m_exit ? m_exit->load() : -1)
  64. << ", abort_request:" << is->abort_request;
  65. if (ret < 0) {
  66. qCWarning(playerControllerVideoDecodeThread)
  67. << "[VideoDecodeThread] get_video_frame failed, exit.";
  68. goto the_end;
  69. }
  70. if (!ret) {
  71. qCDebug(playerControllerVideoDecodeThread) << "[VideoDecodeThread] no frame, continue. m_exit:" << (m_exit ? m_exit->load() : -1) << ", abort_request:" << is->abort_request;
  72. continue;
  73. }
  74. qCDebug(playerControllerVideoDecodeThread)
  75. << "[VideoDecodeThread] got video frame, pts:" << frame->pts
  76. << ", width:" << frame->width << ", height:" << frame->height;
  77. #if USE_AVFILTER_VIDEO
  78. if (last_w != frame->width || last_h != frame->height || last_format != frame->format
  79. || last_serial != is->viddec.pkt_serial || last_vfilter_idx != is->vfilter_idx
  80. || is->req_vfilter_reconfigure) {
  81. av_log(nullptr,
  82. AV_LOG_DEBUG,
  83. "Video frame changed from size:%dx%d format:%s serial:%d to "
  84. "size:%dx%d format:%s serial:%d\n",
  85. last_w,
  86. last_h,
  87. (const char*) av_x_if_null(av_get_pix_fmt_name(last_format), "none"),
  88. last_serial,
  89. frame->width,
  90. frame->height,
  91. (const char*) av_x_if_null(av_get_pix_fmt_name((AVPixelFormat) frame->format),
  92. "none"),
  93. is->viddec.pkt_serial);
  94. avfilter_graph_free(&is->vgraph);
  95. is->vgraph = avfilter_graph_alloc();
  96. if (!is->vgraph) {
  97. ret = AVERROR(ENOMEM);
  98. goto the_end;
  99. }
  100. is->vgraph->nb_threads = 0;
  101. if ((ret = configure_video_filters(is->vgraph, is, is->vfilters, frame)) < 0) {
  102. goto the_end;
  103. }
  104. filt_in = is->in_video_filter;
  105. filt_out = is->out_video_filter;
  106. last_w = frame->width;
  107. last_h = frame->height;
  108. last_format = (AVPixelFormat) frame->format;
  109. last_serial = is->viddec.pkt_serial;
  110. last_vfilter_idx = is->vfilter_idx;
  111. frame_rate = av_buffersink_get_frame_rate(filt_out);
  112. is->req_vfilter_reconfigure = 0;
  113. }
  114. ret = av_buffersrc_add_frame(filt_in, frame);
  115. if (ret < 0)
  116. goto the_end;
  117. while (ret >= 0) {
  118. is->frame_last_returned_time = av_gettime_relative() / 1000000.0;
  119. ret = av_buffersink_get_frame_flags(filt_out, frame, 0);
  120. if (ret < 0) {
  121. if (ret == AVERROR_EOF)
  122. is->viddec.finished = is->viddec.pkt_serial;
  123. ret = 0;
  124. break;
  125. }
  126. is->frame_last_filter_delay = av_gettime_relative() / 1000000.0
  127. - is->frame_last_returned_time;
  128. if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
  129. is->frame_last_filter_delay = 0;
  130. tb = av_buffersink_get_time_base(filt_out);
  131. #endif
  132. #if 0
  133. duration = (frame_rate.num && frame_rate.den ? av_q2d(AVRational{frame_rate.den, frame_rate.num}) : 0);
  134. pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(tb);
  135. ret = queue_picture(is, frame, pts, duration, frame->pkt_pos, is->viddec.pkt_serial);
  136. av_frame_unref(frame);
  137. #else
  138. tmp_frame = frame;
  139. if (frame->format == AV_PIX_FMT_DXVA2_VLD) // DXVA2 hardware decode frame
  140. {
  141. ret = av_hwframe_transfer_data(sw_frame, frame, 0);
  142. if (ret < 0) {
  143. av_log(nullptr,
  144. AV_LOG_WARNING,
  145. "Error transferring the data to system memory\n");
  146. goto the_end;
  147. }
  148. sw_frame->pts = frame->pts;
  149. sw_frame->pkt_dts = frame->pkt_dts;
  150. tmp_frame = sw_frame;
  151. }
  152. duration = (frame_rate.num && frame_rate.den ? av_q2d({frame_rate.den, frame_rate.num})
  153. : 0);
  154. pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(tb);
  155. ret = queue_picture(is, tmp_frame, pts, duration, frame->pkt_pos, is->viddec.pkt_serial);
  156. av_frame_unref(tmp_frame);
  157. #endif
  158. #if USE_AVFILTER_VIDEO
  159. if (is->videoq.serial != is->viddec.pkt_serial)
  160. break;
  161. }
  162. #endif
  163. if (ret < 0)
  164. goto the_end;
  165. }
  166. the_end:
  167. #if USE_AVFILTER_VIDEO
  168. avfilter_graph_free(&is->vgraph);
  169. if (is->vfilters) {
  170. av_free(is->vfilters);
  171. is->vfilters = nullptr;
  172. }
  173. #endif
  174. av_frame_free(&frame);
  175. av_frame_free(&sw_frame);
  176. // 可加日志输出
  177. qCDebug(playerControllerVideoDecodeThread)
  178. << "[VideoDecodeThread] run end, abort_request:" << is->abort_request
  179. << ", m_exit:" << (m_exit ? m_exit->load() : -1);
  180. return;
  181. }