audio_decode_thread.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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(QObject* parent, VideoState* pState)
  10. : QThread(parent)
  11. , m_pState(pState)
  12. {}
  13. AudioDecodeThread::~AudioDecodeThread() {}
  14. void AudioDecodeThread::run()
  15. {
  16. assert(m_pState);
  17. VideoState* is = m_pState;
  18. AVFrame* frame = av_frame_alloc();
  19. Frame* af;
  20. #if USE_AVFILTER_AUDIO
  21. int last_serial = -1;
  22. AVChannelLayout dec_channel_layout; // int64_t
  23. int reconfigure;
  24. #endif
  25. int got_frame = 0;
  26. AVRational tb;
  27. int ret = 0;
  28. if (!frame)
  29. return;
  30. do {
  31. /*if (is->abort_request)
  32. break;*/
  33. if ((got_frame = decoder_decode_frame(&is->auddec, frame, nullptr)) < 0)
  34. goto the_end;
  35. if (got_frame) {
  36. tb = AVRational{1, frame->sample_rate};
  37. #if USE_AVFILTER_AUDIO
  38. // dec_channel_layout = get_valid_channel_layout(frame->channel_layout,
  39. // frame->ch_layout.nb_channels);
  40. dec_channel_layout = frame->ch_layout; // frame->channel_layout; //
  41. reconfigure = cmp_audio_fmts(is->audio_filter_src.fmt,
  42. is->audio_filter_src.ch_layout.nb_channels,
  43. AVSampleFormat(frame->format),
  44. frame->ch_layout.nb_channels)
  45. || is->audio_filter_src.ch_layout.nb_channels
  46. != dec_channel_layout.nb_channels
  47. || is->audio_filter_src.freq != frame->sample_rate
  48. || is->auddec.pkt_serial != last_serial;
  49. if (reconfigure || is->req_afilter_reconfigure) {
  50. char buf1[1024], buf2[1024];
  51. // av_get_channel_layout_string(buf1, sizeof(buf1), -1,
  52. // is->audio_filter_src.channel_layout);
  53. // av_get_channel_layout_string(buf2, sizeof(buf2), -1,
  54. // dec_channel_layout);
  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; //AV_CODEC_FLAG_COPY_OPAQUE; //
  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. #if PRINT_PACKETQUEUE_AUDIO_INFO
  104. // int64 lld, double lf
  105. qDebug("queue audio sample, pts:%lf, duration:%lf, pos:%lld, serial:%d",
  106. af->pts,
  107. af->duration,
  108. af->pos,
  109. af->serial);
  110. #endif
  111. }
  112. } while (ret >= 0 || ret == AVERROR(EAGAIN) || ret == AVERROR_EOF);
  113. the_end:
  114. #if USE_AVFILTER_AUDIO
  115. avfilter_graph_free(&is->agraph);
  116. if (is->afilters) {
  117. av_free(is->afilters);
  118. is->afilters = nullptr;
  119. }
  120. #endif
  121. av_frame_free(&frame);
  122. qDebug("-------- audio decode thread exit.");
  123. return;
  124. }