audio_decode_thread.cpp 4.6 KB

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