video_decode_thread.cpp 7.4 KB

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