audio_decode_thread.cpp 6.4 KB

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