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 SetEof(bool eof) override; // DecoderBase
87
89 void CloseCodecs();
90 void CloseContext();
91 void Reset(bool reset_video_data, bool seek_reset,
92 bool reset_file) override; // DecoderBase
93
96 static bool CanHandle(TestBufferVec & testbuf,
97 const QString &filename);
98
100 int OpenFile(MythMediaBuffer *Buffer, bool novideo,
101 TestBufferVec & testbuf) override; // DecoderBase
102
103 bool GetFrame(DecodeType Type, bool &Retry) override; // DecoderBase
104
105 bool IsLastFrameKey(void) const override { return false; } // DecoderBase
106
107 QString GetCodecDecoderName(void) const override; // DecoderBase
108 QString GetRawEncodingType(void) override; // DecoderBase
109 MythCodecID GetVideoCodecID(void) const override { return m_videoCodecId; } // DecoderBase
110
111 void SetDisablePassThrough(bool disable) override; // DecoderBase
112 void ForceSetupAudioStream(void) override; // DecoderBase
113 void AddTextData(unsigned char *buf, int len, int64_t timecode, char type);
114
115 QString GetTrackDesc(uint Type, uint TrackNo) override;
116 int SetTrack(uint Type, int TrackNo) override;
117
118 int ScanStreams(bool novideo);
119
120 int GetNumChapters() override; // DecoderBase
121 void GetChapterTimes(QList<std::chrono::seconds> &times) override; // DecoderBase
122 int GetCurrentChapter(long long framesPlayed) override; // DecoderBase
123 long long GetChapter(int chapter) override; // DecoderBase
124 bool DoRewind(long long desiredFrame, bool discardFrames = true) override; // DecoderBase
125 bool DoFastForward(long long desiredFrame, bool discardFrames = true) override; // DecoderBase
126 void SetIdrOnlyKeyframes(bool value) override // DecoderBase
127 { m_avcParser->use_I_forKeyframes(!value); }
128
129 std::chrono::milliseconds NormalizeVideoTimecode(std::chrono::milliseconds timecode) override; // DecoderBase
130 virtual std::chrono::milliseconds NormalizeVideoTimecode(AVStream *st, std::chrono::milliseconds timecode);
131
132 int GetTeletextDecoderType(void) const override; // DecoderBase
133
134 QString GetXDS(const QString &Key) const override; // DecoderBase
135 QByteArray GetSubHeader(uint TrackNo) override;
136 void GetAttachmentData(uint TrackNo, QByteArray &Filename, QByteArray &Data) override; // DecoderBase
137
138 // MHEG stuff
139 bool SetAudioByComponentTag(int Tag) override; // DecoderBase
140 bool SetVideoByComponentTag(int Tag) override; // DecoderBase
141
142 // Stream language info
143 virtual int GetTeletextLanguage(uint Index);
144 virtual int GetSubtitleLanguage(uint /*unused*/, uint StreamIndex);
145 virtual int GetCaptionLanguage(TrackType TrackType, int ServiceNum);
146 virtual int GetAudioLanguage(uint AudioIndex, uint StreamIndex);
147 virtual AudioTrackType GetAudioTrackType(uint StreamIndex);
148
149 static int GetMaxReferenceFrames(AVCodecContext *Context);
150
151 static void streams_changed(void *data, int avprogram_id);
152
153 protected:
154 int AutoSelectTrack(uint type) override; // DecoderBase
155 void ScanATSCCaptionStreams(int av_index);
156 void UpdateATSCCaptionTracks(void);
157 void UpdateCaptionTracksFromStreams(bool check_608, bool check_708);
158 void ScanTeletextCaptions(int av_index);
159 void ScanRawTextCaptions(int av_stream_index);
160 void ScanDSMCCStreams(AVBufferRef* pmt_section);
161 int AutoSelectAudioTrack(void);
162 int filter_max_ch(const AVFormatContext *ic,
163 const sinfo_vec_t &tracks,
164 const std::vector<int>&fs,
165 enum AVCodecID codecId = AV_CODEC_ID_NONE,
166 int profile = -1);
167 int selectBestAudioTrack(int lang_key, const std::vector<int> &ftype);
168
169 friend int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic,
170 int flags);
171
172 void DecodeCCx08(const uint8_t *buf, uint buf_size);
173 void InitVideoCodec(AVStream *stream, AVCodecContext *codecContext,
174 bool selectedStream = false);
175
177 void MpegPreProcessPkt(AVCodecContext* codecContext, AVStream *stream, AVPacket *pkt);
178 int H264PreProcessPkt(AVCodecContext* codecContext, AVStream *stream, AVPacket *pkt);
179 bool PreProcessVideoPacket(AVCodecContext* codecContext, AVStream *stream, AVPacket *pkt);
180 virtual bool ProcessVideoPacket(AVCodecContext* codecContext, AVStream *stream, AVPacket *pkt, bool &Retry);
181 virtual bool ProcessVideoFrame(AVCodecContext* codecContext, AVStream *Stream, AVFrame *AvFrame);
182 bool ProcessAudioPacket(AVCodecContext* codecContext, AVStream *stream, AVPacket *pkt,
183 DecodeType decodetype);
184 bool ProcessSubtitlePacket(AVCodecContext* codecContext, AVStream *stream, AVPacket *pkt);
185 bool ProcessRawTextPacket(AVPacket* Packet);
186 virtual bool ProcessDataPacket(AVStream *curstream, AVPacket *pkt,
187 DecodeType decodetype);
188
189 void ProcessVBIDataPacket(const AVStream *stream, const AVPacket *pkt);
190 void ProcessDVBDataPacket(const AVStream *stream, const AVPacket *pkt);
191 void ProcessDSMCCPacket(const AVStream *stream, const AVPacket *pkt);
192
193 void SeekReset(long long newkey, uint skipFrames, bool doFlush, bool discardFrames) override; // DecoderBase
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 void UpdateFramesPlayed(void) override; // DecoderBase
212 bool DoRewindSeek(long long desiredFrame) override; // DecoderBase
213 void DoFastForwardSeek(long long desiredFrame, bool &needflush) override; // DecoderBase
214 virtual void StreamChangeCheck(void);
215 virtual void PostProcessTracks(void) { }
216 virtual bool IsValidStream(int /*streamid*/) {return true;}
217
218 int DecodeAudio(AVCodecContext *ctx, uint8_t *buffer, int &data_size,
219 AVPacket *pkt);
220
221 virtual int ReadPacket(AVFormatContext *ctx, AVPacket *pkt, bool &storePacket);
222
223 bool FlagIsSet(PlayerFlags arg) { return m_playerFlags & arg; }
224
225 int autoSelectVideoTrack(int& scanerror);
226 void remove_tracks_not_in_same_AVProgram(int stream_index);
227
229 AVProgram* get_current_AVProgram();
230
231 bool do_av_seek(long long desiredFrame, bool discardFrames, int flags);
232
234
236
237 AVFormatContext *m_ic {nullptr};
239
240 // AVFormatParameters params;
241
245
246 struct SwsContext *m_swsCtx {nullptr};
247 bool m_directRendering {false};
248
249 bool m_gopSet {false};
251 bool m_seenGop {false};
253 int m_seqCount {0};
254
255 QList<AVPacket*> m_storedPackets;
256
258
259 // GetFrame
260 bool m_gotVideoFrame {false};
261 bool m_hasVideo {false};
263 bool m_skipAudio {false};
264 bool m_allowedQuit {false};
265
266 uint32_t m_startCodeState {0xffffffff};
267
268 std::chrono::milliseconds m_lastVPts {0ms};
269 std::chrono::milliseconds m_lastAPts {0ms};
270 std::chrono::microseconds m_lastCcPtsu {0ms};
271 std::chrono::milliseconds m_firstVPts {0ms};
272 bool m_firstVPtsInuse {false};
273
276
279
280 // Caption/Subtitle/Teletext decoders
287 std::array<bool,68> m_ccX08InPmt {};
291 std::array<bool,68> m_ccX08InTracks {};
293 QList<StreamInfo> m_pmtTracks;
295 QList<TrackType> m_pmtTrackTypes;
298 QList<StreamInfo> m_streamTracks;
301 QList<TrackType> m_streamTrackTypes;
302
305
306 // Audio
307 uint8_t *m_audioSamples {nullptr};
308 bool m_disablePassthru {false};
309
312
313 bool m_processFrames {true};
314
315 bool m_streamsChanged { false };
317
318 // Value in milliseconds, from setting AudioReadAhead
319 std::chrono::milliseconds m_audioReadAhead {100ms};
320
321 QRecursiveMutex m_avCodecLock;
322};
323
324#endif
325
326/* 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:68
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