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*/) { 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 void WriteStoredData(RingBuffer *rb, bool storevid,
190  long timecodeOffset) = 0;
191  virtual void ClearStoredData(void) { return; }
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 val) { m_decodeAllSubtitles = val; }
236  virtual QStringList GetTracks(uint type) const;
237  virtual uint GetTrackCount(uint type) const
238  { return m_tracks[type].size(); }
239 
240  virtual int GetTrackLanguageIndex(uint type, uint trackNo) const;
241  virtual QString GetTrackDesc(uint type, uint trackNo) const;
242  virtual int SetTrack(uint type, int trackNo);
243  int GetTrack(uint type) const { return m_currentTrack[type]; }
244  StreamInfo GetTrackInfo(uint type, uint trackNo) const;
245  inline int IncrementTrack(uint type);
246  inline int DecrementTrack(uint type);
247  inline int ChangeTrack(uint type, int dir);
248  virtual bool InsertTrack(uint type, const StreamInfo&);
249  inline int NextTrack(uint type);
250 
251  virtual int GetTeletextDecoderType(void) const { return -1; }
252 
253  virtual QString GetXDS(const QString&) const { return QString(); }
254  virtual QByteArray GetSubHeader(uint /*trackNo*/) const { return QByteArray(); }
255  virtual void GetAttachmentData(uint /*trackNo*/, QByteArray &/*filename*/,
256  QByteArray &/*data*/) {}
257 
258  // MHEG/MHI stuff
259  virtual bool SetAudioByComponentTag(int) { return false; }
260  virtual bool SetVideoByComponentTag(int) { return false; }
261 
262  void SaveTotalDuration(void);
263  void ResetTotalDuration(void) { m_totalDuration = AVRationalInit(0); }
264  void SaveTotalFrames(void);
265  void TrackTotalDuration(bool track) { m_trackTotalDuration = track; }
266  int GetfpsMultiplier(void) { return m_fpsMultiplier; }
267  MythCodecContext *GetMythCodecContext(void) { return m_mythcodecctx; }
268  VideoDisplayProfile * GetVideoDisplayProfile(void) { return &m_videoDisplayProfile; }
269  AVPixelFormat GetBestVideoFormat(AVPixelFormat* Formats);
270 
271  protected:
272  virtual int AutoSelectTrack(uint type);
273  inline void AutoSelectTracks(void);
274  inline void ResetTracks(void);
275 
276  void FileChanged(void);
277 
278  virtual bool DoRewindSeek(long long desiredFrame);
279  virtual void DoFastForwardSeek(long long desiredFrame, bool &needflush);
280 
281  long long ConditionallyUpdatePosMap(long long desiredFrame);
282  long long GetLastFrameInPosMap(void) const;
283  unsigned long GetPositionMapSize(void) const;
284 
285  struct PosMapEntry
286  {
287  long long index; // frame or keyframe number
288  long long adjFrame; // keyFrameAdjustTable adjusted frame number
289  long long pos; // position in stream
290  };
291  long long GetKey(const PosMapEntry &entry) const;
292 
293  MythPlayer *m_parent {nullptr};
294  ProgramInfo *m_playbackinfo {nullptr};
295  AudioPlayer *m_audio {nullptr};
296  RingBuffer *ringBuffer {nullptr};
297 
298  int m_current_width {640};
299  int m_current_height {480};
300  float m_current_aspect {1.33333f};
301  double m_fps {29.97};
302  int m_fpsMultiplier {1};
303  int m_fpsSkip {0};
304  uint m_bitrate {4000};
305 
306  long long m_framesPlayed {0};
307  long long m_framesRead {0};
308  unsigned long long m_frameCounter {0};
309  AVRational m_totalDuration;
310  long long m_lastKey {0};
311  int m_keyframedist {-1};
312  long long m_indexOffset {0};
314 
315  // The totalDuration field is only valid when the video is played
316  // from start to finish without any jumping. trackTotalDuration
317  // indicates whether this is the case.
318  bool m_trackTotalDuration {false};
319 
321  bool m_exitafterdecoded {false};
322  bool m_transcoding {false};
323 
324  bool m_hasFullPositionMap {false};
325  bool m_recordingHasPositionMap {false};
326  bool m_posmapStarted {false};
327  MarkTypes m_positionMapType {MARK_UNSET};
328 
329  mutable QMutex m_positionMapLock {QMutex::Recursive};
330  vector<PosMapEntry> m_positionMap;
331  frm_pos_map_t m_frameToDurMap; // guarded by m_positionMapLock
332  frm_pos_map_t m_durToFrameMap; // guarded by m_positionMapLock
333  bool m_dontSyncPositionMap {false};
334  mutable QDateTime m_lastPositionMapUpdate; // guarded by m_positionMapLock
335 
336  uint64_t m_seeksnap {UINT64_MAX};
337  bool m_livetv {false};
338  bool m_watchingrecording {false};
339 
340  bool m_hasKeyFrameAdjustTable {false};
341 
342  bool m_getrawframes {false};
343  bool m_getrawvideo {false};
344 
345  bool m_errored {false};
346 
347  bool m_waitingForChange {false};
348  long long m_readAdjust {0};
349  bool m_justAfterChange {false};
350  int m_videoRotation {0};
351 
352  // Audio/Subtitle/EIA-608/EIA-708 stream selection
353  bool m_decodeAllSubtitles {false};
354  int m_currentTrack[kTrackTypeCount];
356  StreamInfo m_wantedTrack[kTrackTypeCount];
357  StreamInfo m_selectedTrack[(uint)kTrackTypeCount];
359  vector<int> m_languagePreference;
360  MythCodecContext *m_mythcodecctx {nullptr};
362 };
363 
365 {
366  int next_track = -1;
367  int size = m_tracks[type].size();
368  if (size)
369  next_track = (max(-1, m_currentTrack[type]) + 1) % size;
370  return SetTrack(type, next_track);
371 }
372 
374 {
375  int next_track = -1;
376  int size = m_tracks[type].size();
377  if (size)
378  next_track = (max(+0, m_currentTrack[type]) + size - 1) % size;
379  return SetTrack(type, next_track);
380 }
381 
382 inline int DecoderBase::ChangeTrack(uint type, int dir)
383 {
384  if (dir > 0)
385  return IncrementTrack(type);
386  else
387  return DecrementTrack(type);
388 }
389 
391 {
392  for (uint i = 0; i < kTrackTypeCount; i++)
393  AutoSelectTrack(i);
394 }
395 
396 inline void DecoderBase::ResetTracks(void)
397 {
398  for (uint i = 0; i < kTrackTypeCount; i++)
399  m_currentTrack[i] = -1;
400 }
401 
403 {
404  int next_track = -1;
405  int size = m_tracks[type].size();
406  if (size)
407  next_track = (max(0, m_currentTrack[type]) + 1) % size;
408  return next_track;
409 }
410 #endif
virtual QByteArray GetSubHeader(uint) const
Definition: decoderbase.h:254
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
int GetTrack(uint type) const
Definition: decoderbase.h:243
void SetLiveTVMode(bool live)
Definition: decoderbase.h:141
MythCodecContext * GetMythCodecContext(void)
Definition: decoderbase.h:267
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
void ResetTracks(void)
Definition: decoderbase.h:396
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:260
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:334
VideoDisplayProfile * GetVideoDisplayProfile(void)
Definition: decoderbase.h:268
vector< int > m_languagePreference
language preferences for auto-selection of streams
Definition: decoderbase.h:359
bool IsErrored() const
Definition: decoderbase.h:226
virtual long long GetChapter(int)
Definition: decoderbase.h:159
int NextTrack(uint type)
Definition: decoderbase.h:402
virtual bool SetAudioByComponentTag(int)
Definition: decoderbase.h:259
int DecrementTrack(uint type)
Definition: decoderbase.h:373
virtual void ForceSetupAudioStream(void)
Definition: decoderbase.h:149
virtual void GetAttachmentData(uint, QByteArray &, QByteArray &)
Definition: decoderbase.h:255
void ResetTotalDuration(void)
Definition: decoderbase.h:263
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:66
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:331
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
int ChangeTrack(uint type, int dir)
Definition: decoderbase.h:382
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:253
AVRational m_totalDuration
Definition: decoderbase.h:309
int to_track_type(const QString &str)
virtual uint GetTrackCount(uint type) const
Definition: decoderbase.h:237
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:332
vector< PosMapEntry > m_positionMap
Definition: decoderbase.h:330
void AutoSelectTracks(void)
Definition: decoderbase.h:390
MythAVCopy m_copyFrame
Definition: decoderbase.h:313
DecodeType
Definition: decoderbase.h:47
int IncrementTrack(uint type)
Definition: decoderbase.h:364
virtual void SetDisablePassThrough(bool disable)
Disables AC3/DTS pass through.
Definition: decoderbase.h:147
virtual void SetRawAudioState(bool state)
Definition: decoderbase.h:192
void SetDecodeAllSubtitles(bool val)
Definition: decoderbase.h:235
int GetfpsMultiplier(void)
Definition: decoderbase.h:266
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:265
long long GetFramesRead(void) const
Definition: decoderbase.h:204
VideoDisplayProfile m_videoDisplayProfile
Definition: decoderbase.h:361
virtual int GetTeletextDecoderType(void) const
Definition: decoderbase.h:251
virtual int64_t NormalizeVideoTimecode(int64_t timecode)
Definition: decoderbase.h:186
AVRational AVRationalInit(int num, int den=1)
Definition: decoderbase.h:113