8template<IDemuxDecoder::DecoderType type>
17 return static_cast<double>(
stream->duration) * av_q2d(
stream->time_base);
28 while(
ret >= 0 && !interrupt) {
32 frameQueue->clear([](AVFrame *frame) { av_frame_free(&frame); });
37 }
else if (
ret == AVERROR(EAGAIN)) {
61 return frameQueue->viewFront<qreal>([
this](AVFrame * frame) {
63 return static_cast<qreal
>(frame->pts) * av_q2d(
stream->time_base);
65 return std::numeric_limits<qreal>::quiet_NaN();
70 PONY_THREAD_SAFE
int skip(
const std::function<
bool(qreal)> &predicate)
override {
71 return frameQueue->skip([
this, predicate](AVFrame *frame){
72 return frame && predicate(
static_cast<qreal
>(frame->pts) * av_q2d(
stream->time_base));
73 }, [](AVFrame *frame) { av_frame_free(&frame); });
90 SwrContext *swrCtx =
nullptr;
91 uint8_t *audioOutBuf =
nullptr;
92 AVFrame * sampleFrameBuf =
nullptr;
98 throw std::runtime_error(
"Cannot alloc audioOutBuf");
100 sampleFrameBuf = av_frame_alloc();
104 if (sampleFrameBuf) { av_frame_free(&sampleFrameBuf); }
105 if (audioOutBuf) { av_freep(&audioOutBuf); }
106 if (swrCtx) { swr_free(&swrCtx); }
116 if (!frame) {
return {}; }
117 double pts =
static_cast<double>(frame->pts) * av_q2d(
stream->time_base);
119 const_cast<const uint8_t **
>(frame->data), frame->nb_samples);
121 int out_size = av_samples_get_buffer_size(
nullptr, targetFmt.
getChannelCount(),
125 av_frame_free(&frame);
126 return {
reinterpret_cast<std::byte *
>(audioOutBuf), out_size, pts};
135 if (swrCtx) { swr_free(&swrCtx); }
136 this->swrCtx = swr_alloc_set_opts(swrCtx, av_get_default_channel_layout(format.
getChannelCount()),
141 if (!swrCtx || swr_init(swrCtx) < 0) {
142 throw std::runtime_error(
"Cannot initialize swrCtx");
156 std::atomic<AVFrame *> stillVideoFrame =
nullptr;
162 if (stillVideoFrame !=
nullptr) {
return {stillVideoFrame,
true, -1}; }
164 if (!frame) {
return {}; }
165 if (frame->pts < 0) {
166 stillVideoFrame = frame;
167 return {frame,
true, -9};
168 }
else {
double pts =
static_cast<double>(frame->pts) * av_q2d(
stream->time_base);
169 return {frame,
true, pts};
175 auto *frame = stillVideoFrame.load();
176 if (frame) { av_frame_free(&frame); }
Definition: frame.hpp:145
Definition: decoders.hpp:105
AVFrame * frameBuf
Definition: decoders.hpp:110
AVCodecContext * codecCtx
Definition: decoders.hpp:109
AVStream * stream
Definition: decoders.hpp:108
PonyAudioFormat getInputFormat() override
Definition: forward.hpp:129
void setOutputFormat(const PonyAudioFormat &format) override
Definition: forward.hpp:133
DecoderImpl(AVStream *vs, TwinsBlockQueue< AVFrame * > *queue)
Definition: forward.hpp:96
virtual ~DecoderImpl() override
Definition: forward.hpp:103
PONY_THREAD_SAFE AudioFrame getSample() override
Definition: forward.hpp:110
~DecoderImpl() override
Definition: forward.hpp:174
VideoFrameRef getPicture() override
Definition: forward.hpp:161
DecoderImpl(AVStream *vs, TwinsBlockQueue< AVFrame * > *queue)
Definition: forward.hpp:158
Definition: forward.hpp:9
PONY_THREAD_SAFE AudioFrame getSample() override
Definition: forward.hpp:53
PONY_THREAD_SAFE double duration() override
Definition: forward.hpp:16
PonyAudioFormat getInputFormat() override
Definition: forward.hpp:55
PONY_THREAD_SAFE int skip(const std::function< bool(qreal)> &predicate) override
Definition: forward.hpp:70
PONY_GUARD_BY(DECODER) void flushFFmpegBuffers() override
Definition: forward.hpp:80
TwinsBlockQueue< AVFrame * > * frameQueue
Definition: forward.hpp:11
int ret
Definition: forward.hpp:23
PONY_GUARD_BY(DECODER) bool accept(AVPacket *pkt
PONY_THREAD_SAFE void setEnable(bool b) override
Definition: forward.hpp:76
PONY_THREAD_SAFE VideoFrameRef getPicture() override
Definition: forward.hpp:51
void setOutputFormat(const PonyAudioFormat &format) override
Definition: forward.hpp:57
PONY_THREAD_SAFE qreal viewFront() override
Definition: forward.hpp:60
DecoderImpl(AVStream *vs, TwinsBlockQueue< AVFrame * > *queue)
Definition: forward.hpp:13
Definition: decoders.hpp:22
virtual bool accept(AVPacket *pkt, std::atomic< bool > &interrupt)=0
virtual void flushFFmpegBuffers()=0
联动队列. 用于单生产者多队列, 单消费者通信.
Definition: twins_queue.hpp:17
constexpr auto Common
Definition: decoders.hpp:141
constexpr auto Video
Definition: decoders.hpp:140
constexpr auto Audio
Definition: decoders.hpp:139
const int MAX_AUDIO_FRAME_SIZE
Definition: helper.hpp:15
const int ERROR_EOF
Definition: helper.hpp:14
QString ffmpegErrToString(int err)
Definition: helper.hpp:21
constexpr PonyThread DECODER
Definition: ponyplayer.h:46
const PonySampleFormat Int16
Definition: audioformat.hpp:160
#define ILLEGAL_STATE(msg)
Definition: ponyplayer.h:40
#define NOT_IMPLEMENT_YET
Definition: ponyplayer.h:39