MythTV  master
decoderbase.h
Go to the documentation of this file.
1 #ifndef DECODERBASE_H_
2 #define DECODERBASE_H_
3 
4 #include <cstdint>
5 #include <vector>
6 using namespace std;
7 
8 #include "ringbuffer.h"
9 #include "remoteencoder.h"
10 #include "mythcontext.h"
11 #include "mythdbcon.h"
12 #include "programinfo.h"
13 #include "mythcodecid.h"
14 #include "mythavutil.h"
15 #include "videodisplayprofile.h"
16 
17 class RingBuffer;
18 class TeletextViewer;
19 class MythPlayer;
20 class AudioPlayer;
21 class MythCodecContext;
22 
23 const int kDecoderProbeBufferSize = 256 * 1024;
24 
27 {
39  // The following are intentionally excluded from kTrackTypeCount which
40  // is used when auto-selecting the correct tracks to decode according to
41  // language, bitrate etc
43 };
44 QString toString(TrackType type);
45 int to_track_type(const QString &str);
46 
48 {
49  kDecodeNothing = 0x00, // Demux and preprocess only.
50  kDecodeVideo = 0x01,
51  kDecodeAudio = 0x02,
52  kDecodeAV = 0x03,
53 };
54 
56 {
58  kAudioTypeAudioDescription, // Audio Description for the visually impaired
59  kAudioTypeCleanEffects, // No dialog, soundtrack or effects only e.g. Karaoke
60  kAudioTypeHearingImpaired, // Greater contrast between dialog and background audio
61  kAudioTypeSpokenSubs, // Spoken subtitles for the visually impaired
62  kAudioTypeCommentary // Director/actor/etc Commentary
63 };
65 
66 // Eof States
68 {
69  kEofStateNone, // no eof
70  kEofStateDelayed, // decoder eof, but let player drain buffered frames
71  kEofStateImmediate // true eof
72 };
73 
75 {
76  public:
77  StreamInfo() = default;
78  StreamInfo(int a, int b, uint c, int d, int e, bool f = false,
79  bool g = false, bool h = false,
81  m_av_stream_index(a),
82  m_language(b), m_language_index(c), m_stream_id(d),
83  m_easy_reader(f), m_wide_aspect_ratio(g), m_orig_num_channels(e), m_forced(h),
84  m_audio_type(i) {}
85  StreamInfo(int a, int b, uint c, int d, int e, int f,
86  bool g = false, bool h = false, bool i = false,
88  m_av_stream_index(a), m_av_substream_index(e),
89  m_language(b), m_language_index(c), m_stream_id(d),
90  m_easy_reader(g), m_wide_aspect_ratio(h), m_orig_num_channels(f), m_forced(i),
91  m_audio_type(j) {}
92 
93  public:
94  int m_av_stream_index {-1};
96  int m_av_substream_index {-1};
97  int m_language {-2};
98  uint m_language_index {0};
99  int m_stream_id {-1};
100  bool m_easy_reader {false};
101  bool m_wide_aspect_ratio {false};
102  int m_orig_num_channels {2};
103  bool m_forced {false};
105 
106  bool operator<(const StreamInfo& b) const
107  {
108  return (this->m_stream_id < b.m_stream_id);
109  }
110 };
111 using sinfo_vec_t = vector<StreamInfo>;
112 
113 inline AVRational AVRationalInit(int num, int den = 1) {
114  AVRational result;
115  result.num = num;
116  result.den = den;
117  return result;
118 }
119 
121 {
122  public:
123  DecoderBase(MythPlayer *parent, const ProgramInfo &pginfo);
124  DecoderBase(const DecoderBase& rhs);
125  virtual ~DecoderBase();
126 
127  virtual void Reset(bool reset_video_data, bool seek_reset, bool reset_file);
128 
129  virtual int OpenFile(RingBuffer *rbuffer, bool novideo,
130  char testbuf[kDecoderProbeBufferSize],
131  int testbufsize = kDecoderProbeBufferSize) = 0;
132 
133  virtual void SetEofState(EofState eof) { m_atEof = eof; }
134  virtual void SetEof(bool eof) {
135  m_atEof = eof ? kEofStateDelayed : kEofStateNone;
136  }
137  EofState GetEof(void) { return m_atEof; }
138 
139  void SetSeekSnap(uint64_t snap) { m_seekSnap = snap; }
140  uint64_t GetSeekSnap(void) const { return m_seekSnap; }
141  void SetLiveTVMode(bool live) { m_livetv = live; }
142 
143  // Must be done while player is paused.
144  void SetProgramInfo(const ProgramInfo &pginfo);
145 
147  virtual void SetDisablePassThrough(bool disable) { (void)disable; }
148  // Reconfigure audio as necessary, following configuration change
149  virtual void ForceSetupAudioStream(void) { }
150 
151  virtual void SetWatchingRecording(bool mode);
153  virtual bool GetFrame(DecodeType Type, bool &Retry) = 0;
154  MythPlayer *GetPlayer() { return m_parent; }
155 
156  virtual int GetNumChapters(void) { return 0; }
157  virtual int GetCurrentChapter(long long /*framesPlayed*/) { return 0; }
158  virtual void GetChapterTimes(QList<long long> &/*times*/) { }
159  virtual long long GetChapter(int /*chapter*/) { return m_framesPlayed; }
160  virtual bool DoRewind(long long desiredFrame, bool discardFrames = true);
161  virtual bool DoFastForward(long long desiredFrame, bool discardFrames = true);
162  virtual void SetIdrOnlyKeyframes(bool /*value*/) { }
163 
164  static uint64_t
165  TranslatePositionAbsToRel(const frm_dir_map_t &deleteMap,
166  uint64_t absPosition,
167  const frm_pos_map_t &map = frm_pos_map_t(),
168  float fallback_ratio = 1.0);
169  static uint64_t
170  TranslatePositionRelToAbs(const frm_dir_map_t &deleteMap,
171  uint64_t relPosition,
172  const frm_pos_map_t &map = frm_pos_map_t(),
173  float fallback_ratio = 1.0);
174  static uint64_t TranslatePosition(const frm_pos_map_t &map,
175  long long key,
176  float fallback_ratio);
177  uint64_t TranslatePositionFrameToMs(long long position,
178  float fallback_framerate,
179  const frm_dir_map_t &cutlist);
180  uint64_t TranslatePositionMsToFrame(uint64_t dur_ms,
181  float fallback_framerate,
182  const frm_dir_map_t &cutlist);
183 
184  float GetVideoAspect(void) const { return m_currentAspect; }
185 
186  virtual int64_t NormalizeVideoTimecode(int64_t timecode) { return timecode; }
187 
188  virtual bool IsLastFrameKey(void) const = 0;
189  virtual void WriteStoredData(RingBuffer *rb, bool storevid,
190  long timecodeOffset) = 0;
191  virtual void ClearStoredData(void) { }
192  virtual void SetRawAudioState(bool state) { m_getRawFrames = state; }
193  virtual bool GetRawAudioState(void) const { return m_getRawFrames; }
194  virtual void SetRawVideoState(bool state) { m_getRawVideo = state; }
195  virtual bool GetRawVideoState(void) const { return m_getRawVideo; }
196 
197  virtual long UpdateStoredFrameNum(long frame) = 0;
198 
199  virtual double GetFPS(void) const { return m_fps; }
201  uint GetRawBitrate(void) const { return m_bitrate; }
202 
203  virtual void UpdateFramesPlayed(void);
204  long long GetFramesRead(void) const { return m_framesRead; }
205  long long GetFramesPlayed(void) const { return m_framesPlayed; }
206  void SetFramesPlayed(long long newValue) {m_framesPlayed = newValue;}
207 
208  virtual QString GetCodecDecoderName(void) const = 0;
209  virtual QString GetRawEncodingType(void) { return QString(); }
210  virtual MythCodecID GetVideoCodecID(void) const = 0;
211 
212  virtual void ResetPosMap(void);
213  virtual bool SyncPositionMap(void);
214  virtual bool PosMapFromDb(void);
215  virtual bool PosMapFromEnc(void);
216 
217  virtual bool FindPosition(long long desired_value, bool search_adjusted,
218  int &lower_bound, int &upper_bound);
219 
220  uint64_t SavePositionMapDelta(long long first_frame, long long last_frame);
221  virtual void SeekReset(long long newkey, uint skipFrames,
222  bool doFlush, bool discardFrames);
223 
224  void SetTranscoding(bool value) { m_transcoding = value; }
225 
226  bool IsErrored() const { return m_errored; }
227 
228  bool HasPositionMap(void) const { return GetPositionMapSize(); }
229 
230  void SetWaitForChange(void);
231  bool GetWaitForChange(void) const;
232  void SetReadAdjust(long long adjust);
233 
234  // Audio/Subtitle/EIA-608/EIA-708 stream selection
235  void SetDecodeAllSubtitles(bool DecodeAll);
236  virtual QStringList GetTracks(uint Type);
237  virtual uint GetTrackCount(uint Type);
238 
239  virtual int GetTrackLanguageIndex(uint Type, uint TrackNo);
240  virtual QString GetTrackDesc(uint Type, uint TrackNo);
241  virtual int SetTrack(uint Type, int TrackNo);
242  int GetTrack(uint Type);
243  StreamInfo GetTrackInfo(uint Type, uint TrackNo);
244  int ChangeTrack(uint Type, int Dir);
245  virtual bool InsertTrack(uint Type, const StreamInfo &Info);
246  int NextTrack(uint Type);
247 
248  virtual int GetTeletextDecoderType(void) const { return -1; }
249 
250  virtual QString GetXDS(const QString &/*key*/) const { return QString(); }
251  virtual QByteArray GetSubHeader(uint /*trackNo*/) { return QByteArray(); }
252  virtual void GetAttachmentData(uint /*trackNo*/, QByteArray &/*filename*/,
253  QByteArray &/*data*/) {}
254 
255  // MHEG/MHI stuff
256  virtual bool SetAudioByComponentTag(int /*tag*/) { return false; }
257  virtual bool SetVideoByComponentTag(int /*tag*/) { return false; }
258 
259  void SaveTotalDuration(void);
260  void ResetTotalDuration(void) { m_totalDuration = AVRationalInit(0); }
261  void SaveTotalFrames(void);
262  void TrackTotalDuration(bool track) { m_trackTotalDuration = track; }
263  int GetfpsMultiplier(void) { return m_fpsMultiplier; }
264  MythCodecContext *GetMythCodecContext(void) { return m_mythCodecCtx; }
265  VideoDisplayProfile * GetVideoDisplayProfile(void) { return &m_videoDisplayProfile; }
266  AVPixelFormat GetBestVideoFormat(AVPixelFormat* Formats);
267 
268  protected:
269  virtual int AutoSelectTrack(uint Type);
270  void AutoSelectTracks(void);
271  void ResetTracks(void);
272  void FileChanged(void);
273  virtual bool DoRewindSeek(long long desiredFrame);
274  virtual void DoFastForwardSeek(long long desiredFrame, bool &needflush);
275 
276  long long ConditionallyUpdatePosMap(long long desiredFrame);
277  long long GetLastFrameInPosMap(void) const;
278  unsigned long GetPositionMapSize(void) const;
279 
280  struct PosMapEntry
281  {
282  long long index; // frame or keyframe number
283  long long adjFrame; // keyFrameAdjustTable adjusted frame number
284  long long pos; // position in stream
285  };
286  long long GetKey(const PosMapEntry &entry) const;
287 
288  MythPlayer *m_parent {nullptr};
289  ProgramInfo *m_playbackInfo {nullptr};
290  AudioPlayer *m_audio {nullptr};
291  RingBuffer *m_ringBuffer {nullptr};
292 
293  int m_currentWidth {640};
294  int m_currentHeight {480};
295  float m_currentAspect {1.33333F};
296  double m_fps {29.97};
297  int m_fpsMultiplier {1};
298  int m_fpsSkip {0};
299  uint m_bitrate {4000};
300 
301  long long m_framesPlayed {0};
302  long long m_framesRead {0};
303  unsigned long long m_frameCounter {0};
304  AVRational m_totalDuration;
305  long long m_lastKey {0};
306  bool m_nextDecodedFrameIsKeyFrame { false };
307  int m_keyframeDist {-1};
308  long long m_indexOffset {0};
310 
311  // The totalDuration field is only valid when the video is played
312  // from start to finish without any jumping. trackTotalDuration
313  // indicates whether this is the case.
314  bool m_trackTotalDuration {false};
315 
317  bool m_exitAfterDecoded {false};
318  bool m_transcoding {false};
319 
320  bool m_hasFullPositionMap {false};
321  bool m_recordingHasPositionMap {false};
322  bool m_posmapStarted {false};
323  MarkTypes m_positionMapType {MARK_UNSET};
324 
325  mutable QMutex m_positionMapLock {QMutex::Recursive};
326  vector<PosMapEntry> m_positionMap;
327  frm_pos_map_t m_frameToDurMap; // guarded by m_positionMapLock
328  frm_pos_map_t m_durToFrameMap; // guarded by m_positionMapLock
329  bool m_dontSyncPositionMap {false};
330  mutable QDateTime m_lastPositionMapUpdate; // guarded by m_positionMapLock
331 
332  uint64_t m_seekSnap {UINT64_MAX};
333  bool m_livetv {false};
334  bool m_watchingRecording {false};
335 
336  bool m_hasKeyFrameAdjustTable {false};
337 
338  bool m_getRawFrames {false};
339  bool m_getRawVideo {false};
340 
341  bool m_errored {false};
342 
343  bool m_waitingForChange {false};
344  long long m_readAdjust {0};
345  bool m_justAfterChange {false};
346  int m_videoRotation {0};
347 
348  // Audio/Subtitle/EIA-608/EIA-708 stream selection
349  QMutex m_trackLock { QMutex::Recursive };
350  bool m_decodeAllSubtitles { false };
351  int m_currentTrack[kTrackTypeCount] { -1 };
352  vector<StreamInfo> m_tracks[kTrackTypeCount];
353  StreamInfo m_wantedTrack[kTrackTypeCount];
354  StreamInfo m_selectedTrack[kTrackTypeCount];
355 
357  vector<int> m_languagePreference;
358  MythCodecContext *m_mythCodecCtx { nullptr };
360 };
361 #endif
bool HasPositionMap(void) const
Definition: decoderbase.h:228
virtual void SetIdrOnlyKeyframes(bool)
Definition: decoderbase.h:162
MythPlayer * GetPlayer()
Definition: decoderbase.h:154
QString toString(TrackType type)
EofState GetEof(void)
Definition: decoderbase.h:137
virtual void SetEofState(EofState eof)
Definition: decoderbase.h:133
void SetLiveTVMode(bool live)
Definition: decoderbase.h:141
MythCodecContext * GetMythCodecContext(void)
Definition: decoderbase.h:264
long long GetFramesPlayed(void) const
Definition: decoderbase.h:205
virtual void ClearStoredData(void)
Definition: decoderbase.h:191
MythCodecID
Definition: mythcodecid.h:10
uint64_t GetSeekSnap(void) const
Definition: decoderbase.h:140
bool operator<(const StreamInfo &b) const
Definition: decoderbase.h:106
AudioTrackType
Definition: decoderbase.h:55
MythAVCopy Copy AVFrame<->frame, performing the required conversion if any.
Definition: mythavutil.h:106
virtual bool SetVideoByComponentTag(int)
Definition: decoderbase.h:257
StreamInfo(int a, int b, uint c, int d, int e, bool f=false, bool g=false, bool h=false, AudioTrackType i=kAudioTypeNormal)
Definition: decoderbase.h:78
QDateTime m_lastPositionMapUpdate
Definition: decoderbase.h:330
VideoDisplayProfile * GetVideoDisplayProfile(void)
Definition: decoderbase.h:265
vector< int > m_languagePreference
language preferences for auto-selection of streams
Definition: decoderbase.h:357
bool IsErrored() const
Definition: decoderbase.h:226
virtual long long GetChapter(int)
Definition: decoderbase.h:159
virtual bool SetAudioByComponentTag(int)
Definition: decoderbase.h:256
virtual void ForceSetupAudioStream(void)
Definition: decoderbase.h:149
virtual void GetAttachmentData(uint, QByteArray &, QByteArray &)
Definition: decoderbase.h:252
void ResetTotalDuration(void)
Definition: decoderbase.h:260
virtual void SetEof(bool eof)
Definition: decoderbase.h:134
virtual void GetChapterTimes(QList< long long > &)
Definition: decoderbase.h:158
QMap< long long, long long > frm_pos_map_t
Frame # -> File offset map.
Definition: programtypes.h:46
EofState
Definition: decoderbase.h:67
MarkTypes
Definition: programtypes.h:48
virtual QString GetRawEncodingType(void)
Definition: decoderbase.h:209
Holds information on recordings and videos.
Definition: programinfo.h:67
virtual double GetFPS(void) const
Definition: decoderbase.h:199
uint GetRawBitrate(void) const
Returns the estimated bitrate if the video were played at normal speed.
Definition: decoderbase.h:201
const int kDecoderProbeBufferSize
Definition: decoderbase.h:23
frm_pos_map_t m_frameToDurMap
Definition: decoderbase.h:327
virtual bool GetRawAudioState(void) const
Definition: decoderbase.h:193
virtual bool GetRawVideoState(void) const
Definition: decoderbase.h:195
static const uint16_t * d
TrackType
Track types.
Definition: decoderbase.h:26
void SetFramesPlayed(long long newValue)
Definition: decoderbase.h:206
int m_stream_id
Definition: decoderbase.h:99
StreamInfo(int a, int b, uint c, int d, int e, int f, bool g=false, bool h=false, bool i=false, AudioTrackType j=kAudioTypeNormal)
Definition: decoderbase.h:85
unsigned int uint
Definition: compat.h:140
void SetSeekSnap(uint64_t snap)
Definition: decoderbase.h:139
static float snap(float value, float snapto, float diff)
virtual QString GetXDS(const QString &) const
Definition: decoderbase.h:250
AVRational m_totalDuration
Definition: decoderbase.h:304
int to_track_type(const QString &str)
float GetVideoAspect(void) const
Definition: decoderbase.h:184
virtual int GetNumChapters(void)
Definition: decoderbase.h:156
frm_pos_map_t m_durToFrameMap
Definition: decoderbase.h:328
vector< PosMapEntry > m_positionMap
Definition: decoderbase.h:326
MythAVCopy m_copyFrame
Definition: decoderbase.h:309
DecodeType
Definition: decoderbase.h:47
virtual void SetDisablePassThrough(bool disable)
Disables AC3/DTS pass through.
Definition: decoderbase.h:147
virtual void SetRawAudioState(bool state)
Definition: decoderbase.h:192
virtual QByteArray GetSubHeader(uint)
Definition: decoderbase.h:251
int GetfpsMultiplier(void)
Definition: decoderbase.h:263
void SetTranscoding(bool value)
Definition: decoderbase.h:224
virtual void SetRawVideoState(bool state)
Definition: decoderbase.h:194
QMap< uint64_t, MarkTypes > frm_dir_map_t
Frame # -> Mark map.
Definition: programtypes.h:81
Implements a file/stream reader/writer.
virtual int GetCurrentChapter(long long)
Definition: decoderbase.h:157
vector< StreamInfo > sinfo_vec_t
Definition: decoderbase.h:111
void TrackTotalDuration(bool track)
Definition: decoderbase.h:262
long long GetFramesRead(void) const
Definition: decoderbase.h:204
VideoDisplayProfile m_videoDisplayProfile
Definition: decoderbase.h:359
virtual int GetTeletextDecoderType(void) const
Definition: decoderbase.h:248
virtual int64_t NormalizeVideoTimecode(int64_t timecode)
Definition: decoderbase.h:186
AVRational AVRationalInit(int num, int den=1)
Definition: decoderbase.h:113