video_decode_thread.cpp 7.8 KB

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