video_decode_thread.cpp 7.5 KB

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