MythTV master
avformatdecoder.h
Go to the documentation of this file.
1
2#ifndef AVFORMATDECODER_H_
3#define AVFORMATDECODER_H_
4
5#include <array>
6#include <cstdint>
7
8extern "C" {
9#include "libavcodec/avcodec.h"
10#include "libavformat/avformat.h"
11}
12
13#include <QList>
14#include <QMap>
15#include <QString>
16
18#include "decoderbase.h"
20#include "mpeg/AVCParser.h"
21#include "mythcodeccontext.h"
22#include "mythplayer.h"
23#include "programinfo.h"
24
25class TeletextDecoder;
26class CC608Decoder;
27class CC708Decoder;
28class SubtitleReader;
29class InteractiveTV;
30class MythSqlDatabase;
31
32struct SwsContext;
33
35{
36 public:
37 AudioInfo() = default;
38
39 AudioInfo(AVCodecID id, AudioFormat fmt, int sr, int ch, bool passthru,
40 int original_ch, int profile = 0) :
41 m_codecId(id), format(fmt),
42 m_sampleSize(ch * AudioOutputSettings::SampleSize(fmt)),
44 m_doPassthru(passthru), m_originalChannels(original_ch)
45 {
46 }
47
48 AVCodecID m_codecId {AV_CODEC_ID_NONE};
50 int m_sampleSize {-2};
51 int m_sampleRate {-1};
52 int m_channels {-1};
54 bool m_doPassthru {false};
56
57 bool operator==(const AudioInfo &o) const
58 {
59 return (m_codecId==o.m_codecId && m_channels==o.m_channels &&
64 }
65 QString toString() const
66 {
67 return QString("id(%1) %2Hz %3ch %4bps %5 (profile %6)")
68 .arg(avcodec_get_name(m_codecId),4).arg(m_sampleRate,6)
70 .arg((m_doPassthru) ? "pt":"",3).arg(m_codecProfile);
71 }
72};
73
76{
77 public:
78 AvFormatDecoder(MythPlayer *parent, const ProgramInfo &pginfo,
79 PlayerFlags flags);
80 ~AvFormatDecoder() override;
81
82 // Deleted functions should be public.
83 AvFormatDecoder(const AvFormatDecoder &) = delete; // not copyable
84 AvFormatDecoder &operator=(const AvFormatDecoder &) = delete; // not copyable
85
86 void UpdateFramesPlayed(void) override; // DecoderBase
87 void SetEof(bool eof) override; // DecoderBase
88
90 void CloseCodecs();
91 void CloseContext();
92 void Reset(bool reset_video_data, bool seek_reset,
93 bool reset_file) override; // DecoderBase
94
97 static bool CanHandle(TestBufferVec & testbuf,
98 const QString &filename);
99
101 int OpenFile(MythMediaBuffer *Buffer, bool novideo,
102 TestBufferVec & testbuf) override; // DecoderBase
103
104 bool GetFrame(DecodeType Type, bool &Retry) override; // DecoderBase
105
106 bool IsLastFrameKey(void) const override { return false; } // DecoderBase
107
108 QString GetCodecDecoderName(void) const override; // DecoderBase
109 QString GetRawEncodingType(void) override; // DecoderBase
110 MythCodecID GetVideoCodecID(void) const override { return m_videoCodecId; } // DecoderBase
111
112 void SetDisablePassThrough(bool disable) override; // DecoderBase
113 void ForceSetupAudioStream(void) override; // DecoderBase
114 void AddTextData(unsigned char *buf, int len, int64_t timecode, char type);
115
116 QString GetTrackDesc(uint Type, uint TrackNo) override;
117 int SetTrack(uint Type, int TrackNo) override;
118
119 int ScanStreams(bool novideo);
120
121 int GetNumChapters() override; // DecoderBase
122 void GetChapterTimes(QList<std::chrono::seconds> &times) override; // DecoderBase
123 int GetCurrentChapter(long long framesPlayed) override; // DecoderBase
124 long long GetChapter(int chapter) override; // DecoderBase
125 bool DoRewind(long long desiredFrame, bool discardFrames = true) override; // DecoderBase
126 bool DoFastForward(long long desiredFrame, bool discardFrames = true) override; // DecoderBase
127 void SeekReset(long long newkey, uint skipFrames, bool doFlush, bool discardFrames) override; // DecoderBase
128 void SetIdrOnlyKeyframes(bool value) override // DecoderBase
129 { m_avcParser->use_I_forKeyframes(!value); }
130
131 std::chrono::milliseconds NormalizeVideoTimecode(std::chrono::milliseconds timecode) override; // DecoderBase
132 virtual std::chrono::milliseconds NormalizeVideoTimecode(AVStream *st, std::chrono::milliseconds timecode);
133
134 int GetTeletextDecoderType(void) const override; // DecoderBase
135
136 QString GetXDS(const QString &Key) const override; // DecoderBase
137 QByteArray GetSubHeader(uint TrackNo) override;
138 void GetAttachmentData(uint TrackNo, QByteArray &Filename, QByteArray &Data) override; // DecoderBase
139
140 // MHEG stuff
141 bool SetAudioByComponentTag(int Tag) override; // DecoderBase
142 bool SetVideoByComponentTag(int Tag) override; // DecoderBase
143
144 // Stream language info
145 virtual int GetTeletextLanguage(uint Index);
146 virtual int GetSubtitleLanguage(uint /*unused*/, uint StreamIndex);
147 virtual int GetCaptionLanguage(TrackType TrackType, int ServiceNum);
148 virtual int GetAudioLanguage(uint AudioIndex, uint StreamIndex);
149 virtual AudioTrackType GetAudioTrackType(uint StreamIndex);
150
151 static int GetMaxReferenceFrames(AVCodecContext *Context);
152
153 static void streams_changed(void *data, int avprogram_id);
154
155 protected:
156 int AutoSelectTrack(uint type) override; // DecoderBase
157 void ScanATSCCaptionStreams(int av_index);
158 void UpdateATSCCaptionTracks(void);
159 void UpdateCaptionTracksFromStreams(bool check_608, bool check_708);
160 void ScanTeletextCaptions(int av_index);
161 void ScanRawTextCaptions(int av_stream_index);
162 void ScanDSMCCStreams(AVBufferRef* pmt_section);
163 int AutoSelectAudioTrack(void);
164 int filter_max_ch(const AVFormatContext *ic,
165 const sinfo_vec_t &tracks,
166 const std::vector<int>&fs,
167 enum AVCodecID codecId = AV_CODEC_ID_NONE,
168 int profile = -1);
169 int selectBestAudioTrack(int lang_key, const std::vector<int> &ftype);
170
171 friend int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic,
172 int flags);
173
174 void DecodeCCx08(const uint8_t *buf, uint buf_size);
175 void InitVideoCodec(AVStream *stream, AVCodecContext *codecContext,
176 bool selectedStream = false);
177
179 void MpegPreProcessPkt(AVCodecContext* codecContext, AVStream *stream, AVPacket *pkt);
180 int H264PreProcessPkt(AVCodecContext* codecContext, AVStream *stream, AVPacket *pkt);
181 bool PreProcessVideoPacket(AVCodecContext* codecContext, AVStream *stream, AVPacket *pkt);
182 virtual bool ProcessVideoPacket(AVCodecContext* codecContext, AVStream *stream, AVPacket *pkt, bool &Retry);
183 virtual bool ProcessVideoFrame(AVCodecContext* codecContext, AVStream *Stream, AVFrame *AvFrame);
184 bool ProcessAudioPacket(AVCodecContext* codecContext, AVStream *stream, AVPacket *pkt,
185 DecodeType decodetype);
186 bool ProcessSubtitlePacket(AVCodecContext* codecContext, AVStream *stream, AVPacket *pkt);
187 bool ProcessRawTextPacket(AVPacket* Packet);
188 virtual bool ProcessDataPacket(AVStream *curstream, AVPacket *pkt,
189 DecodeType decodetype);
190
191 void ProcessVBIDataPacket(const AVStream *stream, const AVPacket *pkt);
192 void ProcessDVBDataPacket(const AVStream *stream, const AVPacket *pkt);
193 void ProcessDSMCCPacket(const AVStream *stream, const AVPacket *pkt);
194
195 inline bool DecoderWillDownmix(const AVCodecContext *ctx);
196 bool DoPassThrough(const AVCodecParameters *par, bool withProfile=true);
197 bool SetupAudioStream(void);
198 void SetupAudioStreamSubIndexes(int streamIndex);
199 void RemoveAudioStreams();
200
203 void HandleGopStart(AVPacket *pkt, bool can_reliably_parse_keyframes);
204
205 bool GenerateDummyVideoFrames(void);
206 bool HasVideo();
207 float GetVideoFrameRate(AVStream *Stream, AVCodecContext *Context, bool Sanitise = false);
208 static void av_update_stream_timings_video(AVFormatContext *ic);
209 bool OpenAVCodec(AVCodecContext *avctx, const AVCodec *codec);
210
211 bool DoRewindSeek(long long desiredFrame) override; // DecoderBase
212 void DoFastForwardSeek(long long desiredFrame, bool &needflush) override; // DecoderBase
213 virtual void StreamChangeCheck(void);
214 virtual void PostProcessTracks(void) { }
215 virtual bool IsValidStream(int /*streamid*/) {return true;}
216
217 int DecodeAudio(AVCodecContext *ctx, uint8_t *buffer, int &data_size,
218 AVPacket *pkt);
219
220 virtual int ReadPacket(AVFormatContext *ctx, AVPacket *pkt, bool &storePacket);
221
222 bool FlagIsSet(PlayerFlags arg) { return m_playerFlags & arg; }
223
224 int autoSelectVideoTrack(int& scanerror);
225 void remove_tracks_not_in_same_AVProgram(int stream_index);
226
228 AVProgram* get_current_AVProgram();
229
230 bool do_av_seek(long long desiredFrame, bool discardFrames, int flags);
231
233
235
236 AVFormatContext *m_ic {nullptr};
238
239 // AVFormatParameters params;
240
244
245 struct SwsContext *m_swsCtx {nullptr};
246 bool m_directRendering {false};
247
248 bool m_gopSet {false};
250 bool m_seenGop {false};
252 int m_seqCount {0};
253
254 QList<AVPacket*> m_storedPackets;
255
257
258 // GetFrame
259 bool m_gotVideoFrame {false};
260 bool m_hasVideo {false};
262 bool m_skipAudio {false};
263 bool m_allowedQuit {false};
264
265 uint32_t m_startCodeState {0xffffffff};
266
267 std::chrono::milliseconds m_lastVPts {0ms};
268 std::chrono::milliseconds m_lastAPts {0ms};
269 std::chrono::microseconds m_lastCcPtsu {0ms};
270 std::chrono::milliseconds m_firstVPts {0ms};
271 bool m_firstVPtsInuse {false};
272
275
278
279 // Caption/Subtitle/Teletext decoders
286 std::array<bool,68> m_ccX08InPmt {};
290 std::array<bool,68> m_ccX08InTracks {};
292 QList<StreamInfo> m_pmtTracks;
294 QList<TrackType> m_pmtTrackTypes;
297 QList<StreamInfo> m_streamTracks;
300 QList<TrackType> m_streamTrackTypes;
301
304
305 // Audio
306 uint8_t *m_audioSamples {nullptr};
307 bool m_disablePassthru {false};
308
311
312 bool m_processFrames {true};
313
314 bool m_streamsChanged { false };
316
317 // Value in milliseconds, from setting AudioReadAhead
318 std::chrono::milliseconds m_audioReadAhead {100ms};
319
320 QRecursiveMutex m_avCodecLock;
321};
322
323#endif
324
325/* vim: set expandtab tabstop=4 shiftwidth=4: */
AVFrame AVFrame
@ FORMAT_NONE
void use_I_forKeyframes(bool val)
Definition: AVCParser.h:99
int m_originalChannels
bool operator==(const AudioInfo &o) const
QString toString() const
AVCodecID m_codecId
int m_codecProfile
AudioInfo()=default
AudioInfo(AVCodecID id, AudioFormat fmt, int sr, int ch, bool passthru, int original_ch, int profile=0)
bool m_doPassthru
AudioFormat format
static int FormatToBits(AudioFormat format)
A decoder for media files.
long long GetChapter(int chapter) override
uint8_t * m_audioSamples
std::chrono::milliseconds m_lastAPts
bool ProcessAudioPacket(AVCodecContext *codecContext, AVStream *stream, AVPacket *pkt, DecodeType decodetype)
MythCodecMap * CodecMap(void)
void ProcessVBIDataPacket(const AVStream *stream, const AVPacket *pkt)
Process ivtv proprietary embedded vertical blanking interval captions.
bool DecoderWillDownmix(const AVCodecContext *ctx)
QList< StreamInfo > m_streamTracks
StreamInfo for 608 and 708 Captions seen in the caption stream itself but not seen in the PMT.
int selectBestAudioTrack(int lang_key, const std::vector< int > &ftype)
void UpdateCaptionTracksFromStreams(bool check_608, bool check_708)
bool GenerateDummyVideoFrames(void)
int GetCurrentChapter(long long framesPlayed) override
AVFormatContext * m_ic
std::chrono::milliseconds m_lastVPts
void Reset(bool reset_video_data, bool seek_reset, bool reset_file) override
InteractiveTV * m_itv
MHEG/MHP decoder.
bool GetFrame(DecodeType Type, bool &Retry) override
Demux, preprocess and possibly decode a frame of video/audio.
PlayerFlags m_playerFlags
void SetIdrOnlyKeyframes(bool value) override
int GetTeletextDecoderType(void) const override
static int GetMaxReferenceFrames(AVCodecContext *Context)
void ForceSetupAudioStream(void) override
bool ProcessSubtitlePacket(AVCodecContext *codecContext, AVStream *stream, AVPacket *pkt)
QString GetTrackDesc(uint Type, uint TrackNo) override
CC608Decoder * m_ccd608
bool DoPassThrough(const AVCodecParameters *par, bool withProfile=true)
QList< TrackType > m_streamTrackTypes
TrackType (608 or 708) for Captions seen in the caption stream itself but not seen in the PMT.
QString GetCodecDecoderName(void) const override
virtual int GetCaptionLanguage(TrackType TrackType, int ServiceNum)
Return ATSC Closed Caption Language.
std::array< bool, 68 > m_ccX08InPmt
Lookup table for whether a stream was seen in the PMT entries 0-3 correspond to CEA-608 CC1 through C...
virtual int GetAudioLanguage(uint AudioIndex, uint StreamIndex)
AvFormatDecoder(const AvFormatDecoder &)=delete
friend int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic, int flags)
CC708Decoder * m_ccd708
int AutoSelectTrack(uint type) override
Select best track.
QList< TrackType > m_pmtTrackTypes
TrackType (608 or 708) for Captions seen in the PMT descriptor.
QString GetXDS(const QString &Key) const override
AVCParser * m_avcParser
MythAVFormatBuffer * m_avfRingBuffer
void RemoveAudioStreams()
remove audio streams from the context used by dvd code during title transitions to remove stale audio...
std::array< bool, 68 > m_ccX08InTracks
Lookup table for whether a stream is represented in the UI entries 0-3 correspond to CEA-608 CC1 thro...
void DecodeCCx08(const uint8_t *buf, uint buf_size)
bool do_av_seek(long long desiredFrame, bool discardFrames, int flags)
void DoFastForwardSeek(long long desiredFrame, bool &needflush) override
Seeks to the keyframe just before the desiredFrame if exact seeks is enabled, or the frame just after...
int m_seqCount
A counter used to determine if we need to force a call to HandleGopStart.
void SetupAudioStreamSubIndexes(int streamIndex)
Reacts to DUAL/STEREO changes on the fly and fix streams.
void ScanTeletextCaptions(int av_index)
bool FlagIsSet(PlayerFlags arg)
void GetChapterTimes(QList< std::chrono::seconds > &times) override
AudioInfo m_audioOut
virtual void StreamChangeCheck(void)
void InitVideoCodec(AVStream *stream, AVCodecContext *codecContext, bool selectedStream=false)
int ScanStreams(bool novideo)
void ScanRawTextCaptions(int av_stream_index)
void AddTextData(unsigned char *buf, int len, int64_t timecode, char type)
void ScanDSMCCStreams(AVBufferRef *pmt_section)
Check to see whether there is a Network Boot Ifo sub-descriptor in the PMT which requires the MHEG ap...
AvFormatDecoder & operator=(const AvFormatDecoder &)=delete
int SetTrack(uint Type, int TrackNo) override
std::chrono::milliseconds m_firstVPts
bool SetAudioByComponentTag(int Tag) override
int AutoSelectAudioTrack(void)
Selects the best audio track.
void ScanATSCCaptionStreams(int av_index)
virtual void PostProcessTracks(void)
int filter_max_ch(const AVFormatContext *ic, const sinfo_vec_t &tracks, const std::vector< int > &fs, enum AVCodecID codecId=AV_CODEC_ID_NONE, int profile=-1)
bool IsLastFrameKey(void) const override
QList< AVPacket * > m_storedPackets
AVProgram * get_current_AVProgram()
void SetDisablePassThrough(bool disable) override
Disables AC3/DTS pass through.
void SeekReset(long long newkey, uint skipFrames, bool doFlush, bool discardFrames) override
void HandleGopStart(AVPacket *pkt, bool can_reliably_parse_keyframes)
Update our position map, keyframe distance, and the like.
int GetNumChapters() override
void UpdateATSCCaptionTracks(void)
bool PreProcessVideoPacket(AVCodecContext *codecContext, AVStream *stream, AVPacket *pkt)
virtual AudioTrackType GetAudioTrackType(uint StreamIndex)
void MpegPreProcessPkt(AVCodecContext *codecContext, AVStream *stream, AVPacket *pkt)
Preprocess a packet, setting the video parms if necessary.
QByteArray GetSubHeader(uint TrackNo) override
bool OpenAVCodec(AVCodecContext *avctx, const AVCodec *codec)
MythCodecID m_videoCodecId
virtual int GetSubtitleLanguage(uint, uint StreamIndex)
Returns DVD Subtitle language.
void UpdateFramesPlayed(void) override
virtual bool ProcessDataPacket(AVStream *curstream, AVPacket *pkt, DecodeType decodetype)
static void av_update_stream_timings_video(AVFormatContext *ic)
AvFormatDecoder(MythPlayer *parent, const ProgramInfo &pginfo, PlayerFlags flags)
MythCodecMap m_codecMap
TeletextDecoder * m_ttd
int OpenFile(MythMediaBuffer *Buffer, bool novideo, TestBufferVec &testbuf) override
Open our file and set up or audio and video parameters.
void ProcessDSMCCPacket(const AVStream *stream, const AVPacket *pkt)
Process DSMCC object carousel packet.
int autoSelectVideoTrack(int &scanerror)
QRecursiveMutex m_avCodecLock
virtual int GetTeletextLanguage(uint Index)
Returns TeleText language.
void GetAttachmentData(uint TrackNo, QByteArray &Filename, QByteArray &Data) override
void ProcessDVBDataPacket(const AVStream *stream, const AVPacket *pkt)
Process DVB Teletext.
uint32_t m_startCodeState
struct SwsContext * m_swsCtx
int get_current_AVStream_index(TrackType type)
bool DoRewindSeek(long long desiredFrame) override
bool DoRewind(long long desiredFrame, bool discardFrames=true) override
virtual int ReadPacket(AVFormatContext *ctx, AVPacket *pkt, bool &storePacket)
int H264PreProcessPkt(AVCodecContext *codecContext, AVStream *stream, AVPacket *pkt)
bool SetupAudioStream(void)
Reinitializes audio if it needs to be reinitialized.
int DecodeAudio(AVCodecContext *ctx, uint8_t *buffer, int &data_size, AVPacket *pkt)
virtual bool IsValidStream(int)
std::chrono::milliseconds m_audioReadAhead
QString GetRawEncodingType(void) override
bool ProcessRawTextPacket(AVPacket *Packet)
~AvFormatDecoder() override
MythCodecID GetVideoCodecID(void) const override
std::chrono::milliseconds NormalizeVideoTimecode(std::chrono::milliseconds timecode) override
virtual bool ProcessVideoFrame(AVCodecContext *codecContext, AVStream *Stream, AVFrame *AvFrame)
void SetEof(bool eof) override
bool SetVideoByComponentTag(int Tag) override
static bool CanHandle(TestBufferVec &testbuf, const QString &filename)
Perform an av_probe_input_format on the passed data to see if we can decode it with this class.
void remove_tracks_not_in_same_AVProgram(int stream_index)
virtual bool ProcessVideoPacket(AVCodecContext *codecContext, AVStream *stream, AVPacket *pkt, bool &Retry)
float GetVideoFrameRate(AVStream *Stream, AVCodecContext *Context, bool Sanitise=false)
bool DoFastForward(long long desiredFrame, bool discardFrames=true) override
Skips ahead or rewinds to desiredFrame.
std::chrono::microseconds m_lastCcPtsu
static void streams_changed(void *data, int avprogram_id)
bool m_seenGop
A flag to indicate that we've seen a GOP frame. Used in junction with seq_count.
QList< StreamInfo > m_pmtTracks
StreamInfo for 608 and 708 Captions seen in the PMT descriptor.
MythVideoFrame * m_decodedVideoFrame
This is the interface between an MHEG engine and a MythTV TV object.
Definition: interactivetv.h:18
Holds information on recordings and videos.
Definition: programinfo.h:74
unsigned int uint
Definition: compat.h:60
AudioTrackType
Definition: decoderbase.h:56
std::vector< char > TestBufferVec
Definition: decoderbase.h:23
std::vector< StreamInfo > sinfo_vec_t
Definition: decoderbase.h:118
TrackType
Track types.
Definition: decoderbase.h:27
DecodeType
Definition: decoderbase.h:48
MythCodecID
Definition: mythcodecid.h:14
@ kCodec_NONE
Definition: mythcodecid.h:17
PlayerFlags
Definition: mythplayer.h:64