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 
26 typedef enum TrackTypes
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 } TrackType;
44 QString toString(TrackType type);
45 int to_track_type(const QString &str);
46 
47 typedef enum DecodeTypes
48 {
49  kDecodeNothing = 0x00, // Demux and preprocess only.
50  kDecodeVideo = 0x01,
51  kDecodeAudio = 0x02,
52  kDecodeAV = 0x03,
53 } DecodeType;
54 
55 typedef enum AudioTrackType
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
65 
66 // Eof States
67 typedef enum
68 {
69  kEofStateNone, // no eof
70  kEofStateDelayed, // decoder eof, but let player drain buffered frames
71  kEofStateImmediate // true eof
72 } EofState;
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 typedef vector<StreamInfo> sinfo_vec_t;
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) = 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*/) { return; }
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_current_aspect; }
185 
186  virtual int64_t NormalizeVideoTimecode(int64_t timecode) { return timecode; }
187 
188  virtual bool IsLastFrameKey(void) const = 0;
189  virtual bool IsCodecMPEG(void) const { return false; }
190  virtual void WriteStoredData(RingBuffer *rb, bool storevid,
191  long timecodeOffset) = 0;
192  virtual void ClearStoredData(void) { return; }
193  virtual void SetRawAudioState(bool state) { m_getrawframes = state; }
194  virtual bool GetRawAudioState(void) const { return m_getrawframes; }
195  virtual void SetRawVideoState(bool state) { m_getrawvideo = state; }
196  virtual bool GetRawVideoState(void) const { return m_getrawvideo; }
197 
198  virtual long UpdateStoredFrameNum(long frame) = 0;
199 
200  virtual double GetFPS(void) const { return m_fps; }
202  uint GetRawBitrate(void) const { return m_bitrate; }
203 
204  virtual void UpdateFramesPlayed(void);
205  long long GetFramesRead(void) const { return m_framesRead; }
206  long long GetFramesPlayed(void) const { return m_framesPlayed; }
207  void SetFramesPlayed(long long newValue) {m_framesPlayed = newValue;}
208 
209  virtual QString GetCodecDecoderName(void) const = 0;
210  virtual QString GetRawEncodingType(void) { return QString(); }
211  virtual MythCodecID GetVideoCodecID(void) const = 0;
212  virtual void *GetVideoCodecPrivate(void) { return nullptr; }
213 
214  virtual void ResetPosMap(void);
215  virtual bool SyncPositionMap(void);
216  virtual bool PosMapFromDb(void);
217  virtual bool PosMapFromEnc(void);
218 
219  virtual bool FindPosition(long long desired_value, bool search_adjusted,
220  int &lower_bound, int &upper_bound);
221 
222  uint64_t SavePositionMapDelta(long long first_frame, long long last_frame);
223  virtual void SeekReset(long long newkey, uint skipFrames,
224  bool doFlush, bool discardFrames);
225 
226  void SetTranscoding(bool value) { m_transcoding = value; }
227 
228  bool IsErrored() const { return m_errored; }
229 
230  bool HasPositionMap(void) const { return GetPositionMapSize(); }
231 
232  void SetWaitForChange(void);
233  bool GetWaitForChange(void) const;
234  void SetReadAdjust(long long adjust);
235 
236  // Audio/Subtitle/EIA-608/EIA-708 stream selection
237  void SetDecodeAllSubtitles(bool val) { m_decodeAllSubtitles = val; }
238  virtual QStringList GetTracks(uint type) const;
239  virtual uint GetTrackCount(uint type) const
240  { return m_tracks[type].size(); }
241 
242  virtual int GetTrackLanguageIndex(uint type, uint trackNo) const;
243  virtual QString GetTrackDesc(uint type, uint trackNo) const;
244  virtual int SetTrack(uint type, int trackNo);
245  int GetTrack(uint type) const { return m_currentTrack[type]; }
246  StreamInfo GetTrackInfo(uint type, uint trackNo) const;
247  inline int IncrementTrack(uint type);
248  inline int DecrementTrack(uint type);
249  inline int ChangeTrack(uint type, int dir);
250  virtual bool InsertTrack(uint type, const StreamInfo&);
251  inline int NextTrack(uint type);
252 
253  virtual int GetTeletextDecoderType(void) const { return -1; }
254 
255  virtual QString GetXDS(const QString&) const { return QString(); }
256  virtual QByteArray GetSubHeader(uint /*trackNo*/) const { return QByteArray(); }
257  virtual void GetAttachmentData(uint /*trackNo*/, QByteArray &/*filename*/,
258  QByteArray &/*data*/) {}
259 
260  // MHEG/MHI stuff
261  virtual bool SetAudioByComponentTag(int) { return false; }
262  virtual bool SetVideoByComponentTag(int) { return false; }
263 
264  void SaveTotalDuration(void);
265  void ResetTotalDuration(void) { m_totalDuration = AVRationalInit(0); }
266  void SaveTotalFrames(void);
267  bool GetVideoInverted(void) const { return m_video_inverted; }
268  void TrackTotalDuration(bool track) { m_trackTotalDuration = track; }
269  int GetfpsMultiplier(void) { return m_fpsMultiplier; }
270  MythCodecContext *GetMythCodecContext(void) { return m_mythcodecctx; }
271  VideoDisplayProfile * GetVideoDisplayProfile(void) { return &m_videoDisplayProfile; }
272 
273  protected:
274  virtual int AutoSelectTrack(uint type);
275  inline void AutoSelectTracks(void);
276  inline void ResetTracks(void);
277 
278  void FileChanged(void);
279 
280  virtual bool DoRewindSeek(long long desiredFrame);
281  virtual void DoFastForwardSeek(long long desiredFrame, bool &needflush);
282 
283  long long ConditionallyUpdatePosMap(long long desiredFrame);
284  long long GetLastFrameInPosMap(void) const;
285  unsigned long GetPositionMapSize(void) const;
286 
287  typedef struct posmapentry
288  {
289  long long index; // frame or keyframe number
290  long long adjFrame; // keyFrameAdjustTable adjusted frame number
291  long long pos; // position in stream
292  } PosMapEntry;
293  long long GetKey(const PosMapEntry &entry) const;
294 
295  MythPlayer *m_parent {nullptr};
296  ProgramInfo *m_playbackinfo {nullptr};
297  AudioPlayer *m_audio {nullptr};
298  RingBuffer *ringBuffer {nullptr};
299 
300  int m_current_width {640};
301  int m_current_height {480};
302  float m_current_aspect {1.33333f};
303  double m_fps {29.97};
304  int m_fpsMultiplier {1};
305  int m_fpsSkip {0};
306  uint m_bitrate {4000};
307 
308  long long m_framesPlayed {0};
309  long long m_framesRead {0};
310  AVRational m_totalDuration;
311  long long m_lastKey {0};
312  int m_keyframedist {-1};
313  long long m_indexOffset {0};
315 
316  // The totalDuration field is only valid when the video is played
317  // from start to finish without any jumping. trackTotalDuration
318  // indicates whether this is the case.
319  bool m_trackTotalDuration {false};
320 
322  bool m_exitafterdecoded {false};
323  bool m_transcoding {false};
324 
325  bool m_hasFullPositionMap {false};
326  bool m_recordingHasPositionMap {false};
327  bool m_posmapStarted {false};
328  MarkTypes m_positionMapType {MARK_UNSET};
329 
330  mutable QMutex m_positionMapLock {QMutex::Recursive};
331  vector<PosMapEntry> m_positionMap;
332  frm_pos_map_t m_frameToDurMap; // guarded by m_positionMapLock
333  frm_pos_map_t m_durToFrameMap; // guarded by m_positionMapLock
334  bool m_dontSyncPositionMap {false};
335  mutable QDateTime m_lastPositionMapUpdate; // guarded by m_positionMapLock
336 
337  uint64_t m_seeksnap {UINT64_MAX};
338  bool m_livetv {false};
339  bool m_watchingrecording {false};
340 
341  bool m_hasKeyFrameAdjustTable {false};
342 
343  bool m_getrawframes {false};
344  bool m_getrawvideo {false};
345 
346  bool m_errored {false};
347 
348  bool m_waitingForChange {false};
349  long long m_readAdjust {0};
350  bool m_justAfterChange {false};
351  bool m_video_inverted {false};
352 
353  // Audio/Subtitle/EIA-608/EIA-708 stream selection
354  bool m_decodeAllSubtitles {false};
355  int m_currentTrack[kTrackTypeCount];
357  StreamInfo m_wantedTrack[kTrackTypeCount];
358  StreamInfo m_selectedTrack[(uint)kTrackTypeCount];
360  vector<int> m_languagePreference;
361  MythCodecContext *m_mythcodecctx {nullptr};
363 };
364 
366 {
367  int next_track = -1;
368  int size = m_tracks[type].size();
369  if (size)
370  next_track = (max(-1, m_currentTrack[type]) + 1) % size;
371  return SetTrack(type, next_track);
372 }
373 
375 {
376  int next_track = -1;
377  int size = m_tracks[type].size();
378  if (size)
379  next_track = (max(+0, m_currentTrack[type]) + size - 1) % size;
380  return SetTrack(type, next_track);
381 }
382 
383 inline int DecoderBase::ChangeTrack(uint type, int dir)
384 {
385  if (dir > 0)
386  return IncrementTrack(type);
387  else
388  return DecrementTrack(type);
389 }
390 
392 {
393  for (uint i = 0; i < kTrackTypeCount; i++)
394  AutoSelectTrack(i);
395 }
396 
397 inline void DecoderBase::ResetTracks(void)
398 {
399  for (uint i = 0; i < kTrackTypeCount; i++)
400  m_currentTrack[i] = -1;
401 }
402 
404 {
405  int next_track = -1;
406  int size = m_tracks[type].size();
407  if (size)
408  next_track = (max(0, m_currentTrack[type]) + 1) % size;
409  return next_track;
410 }
411 #endif
virtual QByteArray GetSubHeader(uint) const
Definition: decoderbase.h:256
bool HasPositionMap(void) const
Definition: decoderbase.h:230
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
DecodeTypes
Definition: decoderbase.h:47
virtual void SetEofState(EofState eof)
Definition: decoderbase.h:133
int GetTrack(uint type) const
Definition: decoderbase.h:245
void SetLiveTVMode(bool live)
Definition: decoderbase.h:141
MythCodecContext * GetMythCodecContext(void)
Definition: decoderbase.h:270
long long GetFramesPlayed(void) const
Definition: decoderbase.h:206
virtual void ClearStoredData(void)
Definition: decoderbase.h:192
MythCodecID
Definition: mythcodecid.h:10
uint64_t GetSeekSnap(void) const
Definition: decoderbase.h:140
bool operator<(const StreamInfo &b) const
Definition: decoderbase.h:106
void ResetTracks(void)
Definition: decoderbase.h:397
AudioTrackType
Definition: decoderbase.h:55
MythAVCopy Copy AVFrame<->frame, performing the required conversion if any.
Definition: mythavutil.h:114
virtual bool SetVideoByComponentTag(int)
Definition: decoderbase.h:262
virtual bool IsCodecMPEG(void) const
Definition: decoderbase.h:189
TrackTypes
Track types.
Definition: decoderbase.h:26
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
bool GetVideoInverted(void) const
Definition: decoderbase.h:267
QMap< uint64_t, MarkTypes > frm_dir_map_t
Frame # -> Mark map.
Definition: programtypes.h:81
unsigned int uint
Definition: compat.h:140
QDateTime m_lastPositionMapUpdate
Definition: decoderbase.h:335
VideoDisplayProfile * GetVideoDisplayProfile(void)
Definition: decoderbase.h:271
vector< int > m_languagePreference
language preferences for auto-selection of streams
Definition: decoderbase.h:360
bool IsErrored() const
Definition: decoderbase.h:228
virtual long long GetChapter(int)
Definition: decoderbase.h:159
int NextTrack(uint type)
Definition: decoderbase.h:403
virtual bool SetAudioByComponentTag(int)
Definition: decoderbase.h:261
int DecrementTrack(uint type)
Definition: decoderbase.h:374
virtual void ForceSetupAudioStream(void)
Definition: decoderbase.h:149
virtual void GetAttachmentData(uint, QByteArray &, QByteArray &)
Definition: decoderbase.h:257
void ResetTotalDuration(void)
Definition: decoderbase.h:265
virtual void SetEof(bool eof)
Definition: decoderbase.h:134
virtual void GetChapterTimes(QList< long long > &)
Definition: decoderbase.h:158
unsigned char b
Definition: ParseText.cpp:329
EofState
Definition: decoderbase.h:67
MarkTypes
Definition: programtypes.h:48
virtual QString GetRawEncodingType(void)
Definition: decoderbase.h:210
Holds information on recordings and videos.
Definition: programinfo.h:66
virtual double GetFPS(void) const
Definition: decoderbase.h:200
uint GetRawBitrate(void) const
Returns the estimated bitrate if the video were played at normal speed.
Definition: decoderbase.h:202
const int kDecoderProbeBufferSize
Definition: decoderbase.h:23
frm_pos_map_t m_frameToDurMap
Definition: decoderbase.h:332
virtual bool GetRawAudioState(void) const
Definition: decoderbase.h:194
virtual bool GetRawVideoState(void) const
Definition: decoderbase.h:196
static const uint16_t * d
enum TrackTypes TrackType
Track types.
void SetFramesPlayed(long long newValue)
Definition: decoderbase.h:207
int ChangeTrack(uint type, int dir)
Definition: decoderbase.h:383
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
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:255
AVRational m_totalDuration
Definition: decoderbase.h:310
enum DecodeTypes DecodeType
int to_track_type(const QString &str)
virtual uint GetTrackCount(uint type) const
Definition: decoderbase.h:239
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:333
vector< PosMapEntry > m_positionMap
Definition: decoderbase.h:331
vector< StreamInfo > sinfo_vec_t
Definition: decoderbase.h:111
void AutoSelectTracks(void)
Definition: decoderbase.h:391
MythAVCopy m_copyFrame
Definition: decoderbase.h:314
int IncrementTrack(uint type)
Definition: decoderbase.h:365
virtual void SetDisablePassThrough(bool disable)
Disables AC3/DTS pass through.
Definition: decoderbase.h:147
virtual void SetRawAudioState(bool state)
Definition: decoderbase.h:193
void SetDecodeAllSubtitles(bool val)
Definition: decoderbase.h:237
int GetfpsMultiplier(void)
Definition: decoderbase.h:269
void SetTranscoding(bool value)
Definition: decoderbase.h:226
virtual void SetRawVideoState(bool state)
Definition: decoderbase.h:195
Implements a file/stream reader/writer.
virtual int GetCurrentChapter(long long)
Definition: decoderbase.h:157
QMap< long long, long long > frm_pos_map_t
Frame # -> File offset map.
Definition: programtypes.h:46
virtual void * GetVideoCodecPrivate(void)
Definition: decoderbase.h:212
void TrackTotalDuration(bool track)
Definition: decoderbase.h:268
long long GetFramesRead(void) const
Definition: decoderbase.h:205
VideoDisplayProfile m_videoDisplayProfile
Definition: decoderbase.h:362
virtual int GetTeletextDecoderType(void) const
Definition: decoderbase.h:253
virtual int64_t NormalizeVideoTimecode(int64_t timecode)
Definition: decoderbase.h:186
AVRational AVRationalInit(int num, int den=1)
Definition: decoderbase.h:113
unsigned char g
Definition: ParseText.cpp:329