audio_decode_thread.cpp 6.2 KB

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