video_decode_thread.cpp 6.7 KB

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