audio_decode_thread.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // ***********************************************************/
  2. // audio_decode_thread.cpp
  3. //
  4. // Copy Right @ Steven Huang. All rights reserved.
  5. //
  6. // audio decode thread
  7. // ***********************************************************/
  8. #include "audio_decode_thread.h"
  9. #include "AVPlayer2/playercontroller.h"
  10. AudioDecodeThread::AudioDecodeThread(VideoState* pState)
  11. : m_pState(pState)
  12. {}
  13. AudioDecodeThread::~AudioDecodeThread() {}
  14. void AudioDecodeThread::stop()
  15. {
  16. *m_exit = true;
  17. m_cv.notify_all();
  18. }
  19. void AudioDecodeThread::run()
  20. {
  21. qCDebug(playerControllerLog) << "[AudioDecodeThread] run start, m_pState:" << (void*) m_pState;
  22. assert(m_pState);
  23. VideoState* is = m_pState;
  24. qCDebug(playerControllerLog) << "[AudioDecodeThread] VideoState* is:" << (void*) is
  25. << ", abort_request:" << is->abort_request;
  26. AVFrame* frame = av_frame_alloc();
  27. Frame* af;
  28. #if USE_AVFILTER_AUDIO
  29. int last_serial = -1;
  30. AVChannelLayout dec_channel_layout; // int64_t
  31. int reconfigure;
  32. #endif
  33. int got_frame = 0;
  34. AVRational tb;
  35. int ret = 0;
  36. if (!frame) {
  37. qCWarning(playerControllerLog) << "[AudioDecodeThread] av_frame_alloc failed!";
  38. return;
  39. }
  40. do {
  41. if (is->abort_request) {
  42. qCDebug(playerControllerLog) << "[AudioDecodeThread] abort_request set, exit.";
  43. break;
  44. }
  45. if (m_exit) {
  46. qCDebug(playerControllerLog) << "[AudioDecodeThread] m_exit set, exit.";
  47. break;
  48. }
  49. qCDebug(playerControllerLog)
  50. << "[AudioDecodeThread] call decoder_decode_frame, auddec.avctx:"
  51. << (void*) is->auddec.avctx;
  52. if ((got_frame = decoder_decode_frame(&is->auddec, frame, nullptr)) < 0) {
  53. qCWarning(playerControllerLog)
  54. << "[AudioDecodeThread] decoder_decode_frame failed, ret:" << got_frame;
  55. goto the_end;
  56. }
  57. if (got_frame) {
  58. qCDebug(playerControllerLog)
  59. << "[AudioDecodeThread] got audio frame, pts:" << frame->pts
  60. << ", sample_rate:" << frame->sample_rate << ", nb_samples:" << frame->nb_samples;
  61. tb = AVRational{1, frame->sample_rate};
  62. #if USE_AVFILTER_AUDIO
  63. dec_channel_layout = frame->ch_layout; // frame->channel_layout; //
  64. reconfigure = cmp_audio_fmts(is->audio_filter_src.fmt,
  65. is->audio_filter_src.ch_layout.nb_channels,
  66. AVSampleFormat(frame->format),
  67. frame->ch_layout.nb_channels)
  68. || is->audio_filter_src.ch_layout.nb_channels
  69. != dec_channel_layout.nb_channels
  70. || is->audio_filter_src.freq != frame->sample_rate
  71. || is->auddec.pkt_serial != last_serial;
  72. if (reconfigure || is->req_afilter_reconfigure) {
  73. char buf1[1024], buf2[1024];
  74. av_channel_layout_describe(&is->audio_filter_src.ch_layout, buf1, sizeof(buf1));
  75. av_channel_layout_describe(&dec_channel_layout, buf2, sizeof(buf2));
  76. av_log(nullptr,
  77. AV_LOG_DEBUG,
  78. "Audio frame changed from rate:%d ch:%d fmt:%s layout:%s "
  79. "serial:%d to rate:%d ch:%d fmt:%s layout:%s serial:%d\n",
  80. is->audio_filter_src.freq,
  81. is->audio_filter_src.ch_layout.nb_channels,
  82. av_get_sample_fmt_name(is->audio_filter_src.fmt),
  83. buf1,
  84. last_serial,
  85. frame->sample_rate,
  86. frame->ch_layout.nb_channels,
  87. av_get_sample_fmt_name(AVSampleFormat(frame->format)),
  88. buf2,
  89. is->auddec.pkt_serial);
  90. is->audio_filter_src.fmt = (AVSampleFormat) frame->format;
  91. ret = av_channel_layout_copy(&is->audio_filter_src.ch_layout, &frame->ch_layout);
  92. if (ret < 0)
  93. goto the_end;
  94. is->audio_filter_src.freq = frame->sample_rate;
  95. last_serial = is->auddec.pkt_serial;
  96. ret = configure_audio_filters(is, is->afilters, 1);
  97. if (ret < 0)
  98. goto the_end;
  99. is->req_afilter_reconfigure = 0;
  100. }
  101. ret = av_buffersrc_add_frame(is->in_audio_filter, frame);
  102. if (ret < 0)
  103. goto the_end;
  104. while ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, frame, 0)) >= 0) {
  105. tb = av_buffersink_get_time_base(is->out_audio_filter);
  106. #endif
  107. if (!(af = frame_queue_peek_writable(&is->sampq)))
  108. goto the_end;
  109. af->pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(tb);
  110. af->pos = frame->pkt_pos;
  111. af->serial = is->auddec.pkt_serial;
  112. af->duration = av_q2d(AVRational{frame->nb_samples, frame->sample_rate});
  113. av_frame_move_ref(af->frame, frame);
  114. frame_queue_push(&is->sampq);
  115. #if USE_AVFILTER_AUDIO
  116. if (is->audioq.serial != is->auddec.pkt_serial)
  117. break;
  118. }
  119. if (ret == AVERROR_EOF)
  120. is->auddec.finished = is->auddec.pkt_serial;
  121. #endif
  122. } else {
  123. qCDebug(playerControllerLog) << "[AudioDecodeThread] no frame decoded, continue.";
  124. }
  125. } while (got_frame);
  126. the_end:
  127. #if USE_AVFILTER_AUDIO
  128. avfilter_graph_free(&is->agraph);
  129. if (is->afilters) {
  130. av_free(is->afilters);
  131. is->afilters = nullptr;
  132. }
  133. #endif
  134. av_frame_free(&frame);
  135. // 可加日志输出
  136. qCDebug(playerControllerLog) << "[AudioDecodeThread] run end, abort_request:"
  137. << is->abort_request << ", m_exit:" << m_exit;
  138. return;
  139. }