PonyPlayer
decoders.hpp
浏览该文件的文档.
1//
2// Created by ColorsWind on 2022/5/18.
3//
4#pragma once
5
6#include "helper.hpp"
7INCLUDE_FFMPEG_BEGIN
8#include <libavformat/avformat.h>
9#include <libavcodec/avcodec.h>
10#include <libavutil/error.h>
11#include <libswresample/swresample.h>
12#include <libavutil/imgutils.h>
14#include <QDebug>
15#include "frame.hpp"
16#include "twins_queue.hpp"
17#include "concurrentqueue.h"
18#include "audioformat.hpp"
19#include <atomic>
20#include <utility>
21
23
24public:
28 enum class DecoderType {
29 Audio,
30 Video,
31 Common,
32 };
38 virtual bool accept(AVPacket *pkt, std::atomic<bool> &interrupt) = 0;
39
43 virtual void flushFFmpegBuffers() = 0;
44
45
52
58 virtual AudioFrame getSample() = 0;
59
64 virtual qreal viewFront() = 0;
65
71 virtual int skip(const std::function<bool(qreal)> &predicate) = 0;
72
77 virtual double duration() = 0;
78
79
80 virtual void setEnable(bool b) = 0;
81
82 virtual ~IDemuxDecoder() = default;
83
85
86 virtual void pushFrameStack() {}
87
88 virtual qreal getLastPts() {
90 }
91
92 virtual void clearFrameStack() {}
93
94 virtual void setStart(qreal secs) {}
95
96 virtual qreal nextSegment() {
98 }
99
101
102 virtual void setOutputFormat(const PonyAudioFormat& format) = 0;
103};
104
106public:
107 AVCodec *codec = nullptr;
108 AVStream *stream = nullptr;
109 AVCodecContext *codecCtx = nullptr;
110 AVFrame *frameBuf = nullptr;
111 DecoderContext(AVStream *vs): stream(vs) {
112 auto *videoCodecPara = stream->codecpar;
113 if (!(codec = const_cast<AVCodec *>(avcodec_find_decoder(videoCodecPara->codec_id)))) {
114 throw std::runtime_error("Cannot find valid video decode codec.");
115 }
116 if (!(codecCtx = avcodec_alloc_context3(codec))) {
117 throw std::runtime_error("Cannot find valid video decode codec context.");
118 }
119 if (avcodec_parameters_to_context(codecCtx, videoCodecPara) < 0) {
120 throw std::runtime_error("Cannot initialize videoCodecCtx.");
121 }
122 if (avcodec_open2(codecCtx, codec, nullptr) < 0) {
123 throw std::runtime_error("Cannot open codec.");
124 }
125 if (!(frameBuf = av_frame_alloc())) {
126 throw std::runtime_error("Cannot alloc frame buf.");
127 }
128 }
129
131 if (frameBuf) { av_frame_free(&frameBuf); }
132 if (codecCtx) { avcodec_close(codecCtx); }
133 if (codecCtx) { avcodec_free_context(&codecCtx); }
134 }
135
136};
137
138
Definition: frame.hpp:145
Definition: decoders.hpp:105
AVCodec * codec
Definition: decoders.hpp:107
AVFrame * frameBuf
Definition: decoders.hpp:110
AVCodecContext * codecCtx
Definition: decoders.hpp:109
~DecoderContext()
Definition: decoders.hpp:130
AVStream * stream
Definition: decoders.hpp:108
DecoderContext(AVStream *vs)
Definition: decoders.hpp:111
Definition: decoders.hpp:22
virtual bool accept(AVPacket *pkt, std::atomic< bool > &interrupt)=0
virtual void setFollower(IDemuxDecoder *follower)
Definition: decoders.hpp:84
virtual VideoFrameRef getPicture()=0
DecoderType
Definition: decoders.hpp:28
@ Video
视频解码器
@ Audio
音频解码器
virtual void setEnable(bool b)=0
virtual qreal viewFront()=0
virtual void setStart(qreal secs)
Definition: decoders.hpp:94
virtual qreal getLastPts()
Definition: decoders.hpp:88
virtual void pushFrameStack()
Definition: decoders.hpp:86
virtual PonyAudioFormat getInputFormat()=0
virtual void flushFFmpegBuffers()=0
virtual ~IDemuxDecoder()=default
virtual int skip(const std::function< bool(qreal)> &predicate)=0
virtual void clearFrameStack()
Definition: decoders.hpp:92
virtual double duration()=0
virtual AudioFrame getSample()=0
virtual qreal nextSegment()
Definition: decoders.hpp:96
virtual void setOutputFormat(const PonyAudioFormat &format)=0
Definition: audioformat.hpp:97
Definition: frame.hpp:47
constexpr auto Common
Definition: decoders.hpp:141
constexpr auto Video
Definition: decoders.hpp:140
constexpr auto Audio
Definition: decoders.hpp:139
#define INCLUDE_FFMPEG_END
Definition: ponyplayer.h:26
#define NOT_IMPLEMENT_YET
Definition: ponyplayer.h:39