audio_decode_thread.cpp 7.1 KB

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