audio_decode_thread.cpp 6.3 KB

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