video_decode_thread.cpp 5.7 KB

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