00001 #ifndef DECODERBASE_H_
00002 #define DECODERBASE_H_
00003
00004 #include <stdint.h>
00005
00006 #include <vector>
00007 using namespace std;
00008
00009 #include "ringbuffer.h"
00010 #include "remoteencoder.h"
00011 #include "mythcontext.h"
00012 #include "mythdbcon.h"
00013 #include "programinfo.h"
00014 #include "mythcodecid.h"
00015
00016 class RingBuffer;
00017 class TeletextViewer;
00018 class MythPlayer;
00019 class AudioPlayer;
00020
00021 const int kDecoderProbeBufferSize = 256 * 1024;
00022
00024 typedef enum TrackTypes
00025 {
00026 kTrackTypeUnknown = 0,
00027 kTrackTypeAudio,
00028 kTrackTypeVideo,
00029 kTrackTypeSubtitle,
00030 kTrackTypeCC608,
00031 kTrackTypeCC708,
00032 kTrackTypeTeletextCaptions,
00033 kTrackTypeTeletextMenu,
00034 kTrackTypeRawText,
00035 kTrackTypeAttachment,
00036 kTrackTypeCount,
00037
00038 kTrackTypeTextSubtitle,
00039 } TrackType;
00040 QString toString(TrackType type);
00041 int to_track_type(const QString &str);
00042
00043 typedef enum DecodeTypes
00044 {
00045 kDecodeNothing = 0x00,
00046 kDecodeVideo = 0x01,
00047 kDecodeAudio = 0x02,
00048 kDecodeAV = 0x03,
00049 } DecodeType;
00050
00051 typedef enum AudioTrackType
00052 {
00053 kAudioTypeNormal = 0,
00054 kAudioTypeAudioDescription,
00055 kAudioTypeCleanEffects,
00056 kAudioTypeHearingImpaired,
00057 kAudioTypeSpokenSubs,
00058 kAudioTypeCommentary
00059 } AudioTrackType;
00060 QString toString(AudioTrackType type);
00061
00062
00063 typedef enum
00064 {
00065 kEofStateNone,
00066 kEofStateDelayed,
00067 kEofStateImmediate
00068 } EofState;
00069
00070 class StreamInfo
00071 {
00072 public:
00073 StreamInfo() :
00074 av_stream_index(-1), av_substream_index(-1),
00075 language(-2), language_index(0),
00076 stream_id(-1), easy_reader(false),
00077 wide_aspect_ratio(false), orig_num_channels(2), forced(false),
00078 audio_type(kAudioTypeNormal) {}
00079 StreamInfo(int a, int b, uint c, int d, int e, bool f = false,
00080 bool g = false, bool h = false,
00081 AudioTrackType i = kAudioTypeNormal) :
00082 av_stream_index(a), av_substream_index(-1),
00083 language(b), language_index(c), stream_id(d),
00084 easy_reader(f), wide_aspect_ratio(g), orig_num_channels(e), forced(h),
00085 audio_type(i) {}
00086 StreamInfo(int a, int b, uint c, int d, int e, int f,
00087 bool g = false, bool h = false, bool i = false,
00088 AudioTrackType j = kAudioTypeNormal) :
00089 av_stream_index(a), av_substream_index(e),
00090 language(b), language_index(c), stream_id(d),
00091 easy_reader(g), wide_aspect_ratio(h), orig_num_channels(f), forced(i),
00092 audio_type(j) {}
00093
00094 public:
00095 int av_stream_index;
00097 int av_substream_index;
00098 int language;
00099 uint language_index;
00100 int stream_id;
00101 bool easy_reader;
00102 bool wide_aspect_ratio;
00103 int orig_num_channels;
00104 bool forced;
00105 AudioTrackType audio_type;
00106
00107 bool operator<(const StreamInfo& b) const
00108 {
00109 return (this->stream_id < b.stream_id);
00110 }
00111 };
00112 typedef vector<StreamInfo> sinfo_vec_t;
00113
00114 inline AVRational AVRationalInit(int num, int den = 1) {
00115 AVRational result;
00116 result.num = num;
00117 result.den = den;
00118 return result;
00119 }
00120
00121 class DecoderBase
00122 {
00123 public:
00124 DecoderBase(MythPlayer *parent, const ProgramInfo &pginfo);
00125 virtual ~DecoderBase();
00126
00127 virtual void Reset(bool reset_video_data, bool seek_reset, bool reset_file);
00128
00129 virtual int OpenFile(RingBuffer *rbuffer, bool novideo,
00130 char testbuf[kDecoderProbeBufferSize],
00131 int testbufsize = kDecoderProbeBufferSize) = 0;
00132
00133 virtual void SetEofState(EofState eof) { ateof = eof; }
00134 virtual void SetEof(bool eof) {
00135 ateof = eof ? kEofStateDelayed : kEofStateNone;
00136 }
00137 EofState GetEof(void) { return ateof; }
00138
00139 void SetSeekSnap(uint64_t snap) { seeksnap = snap; }
00140 uint64_t GetSeekSnap(void) const { return seeksnap; }
00141 void SetLiveTVMode(bool live) { livetv = live; }
00142
00143
00144 void SetProgramInfo(const ProgramInfo &pginfo);
00145
00146 void SetLowBuffers(bool low) { lowbuffers = low; }
00148 virtual void SetDisablePassThrough(bool disable) { (void)disable; }
00149
00150 virtual void SetWatchingRecording(bool mode);
00152 virtual bool GetFrame(DecodeType) = 0;
00153 MythPlayer *GetPlayer() { return m_parent; }
00154
00155 virtual int GetNumChapters(void) { return 0; }
00156 virtual int GetCurrentChapter(long long framesPlayed) { return 0; }
00157 virtual void GetChapterTimes(QList<long long> ×) { return; }
00158 virtual long long GetChapter(int chapter) { return framesPlayed; }
00159 virtual bool DoRewind(long long desiredFrame, bool doflush = true);
00160 virtual bool DoFastForward(long long desiredFrame, bool doflush = true);
00161 virtual void SetIdrOnlyKeyframes(bool value) { }
00162
00163 static uint64_t
00164 TranslatePositionAbsToRel(const frm_dir_map_t &deleteMap,
00165 uint64_t absPosition,
00166 const frm_pos_map_t &map = frm_pos_map_t(),
00167 float fallback_ratio = 1.0);
00168 static uint64_t
00169 TranslatePositionRelToAbs(const frm_dir_map_t &deleteMap,
00170 uint64_t relPosition,
00171 const frm_pos_map_t &map = frm_pos_map_t(),
00172 float fallback_ratio = 1.0);
00173 static uint64_t TranslatePosition(const frm_pos_map_t &map,
00174 uint64_t key,
00175 float fallback_ratio);
00176 uint64_t TranslatePositionFrameToMs(uint64_t position,
00177 float fallback_framerate,
00178 const frm_dir_map_t &cutlist);
00179 uint64_t TranslatePositionMsToFrame(uint64_t dur_ms,
00180 float fallback_framerate,
00181 const frm_dir_map_t &cutlist);
00182
00183 float GetVideoAspect(void) const { return current_aspect; }
00184
00185 virtual int64_t NormalizeVideoTimecode(int64_t timecode) { return timecode; }
00186
00187 virtual bool IsLastFrameKey(void) const = 0;
00188 virtual bool IsCodecMPEG(void) const { return false; }
00189 virtual void WriteStoredData(RingBuffer *rb, bool storevid,
00190 long timecodeOffset) = 0;
00191 virtual void ClearStoredData(void) { return; }
00192 virtual void SetRawAudioState(bool state) { getrawframes = state; }
00193 virtual bool GetRawAudioState(void) const { return getrawframes; }
00194 virtual void SetRawVideoState(bool state) { getrawvideo = state; }
00195 virtual bool GetRawVideoState(void) const { return getrawvideo; }
00196
00197 virtual long UpdateStoredFrameNum(long frame) = 0;
00198
00199 virtual double GetFPS(void) const { return fps; }
00201 uint GetRawBitrate(void) const { return bitrate; }
00202
00203 virtual void UpdateFramesPlayed(void);
00204 long long GetFramesRead(void) const { return framesRead; }
00205 long long GetFramesPlayed(void) const { return framesPlayed; }
00206
00207 virtual QString GetCodecDecoderName(void) const = 0;
00208 virtual QString GetRawEncodingType(void) { return QString(); }
00209 virtual MythCodecID GetVideoCodecID(void) const = 0;
00210 virtual void *GetVideoCodecPrivate(void) { return NULL; }
00211
00212 virtual void ResetPosMap(void);
00213 virtual bool SyncPositionMap(void);
00214 virtual bool PosMapFromDb(void);
00215 virtual bool PosMapFromEnc(void);
00216
00217 virtual bool FindPosition(long long desired_value, bool search_adjusted,
00218 int &lower_bound, int &upper_bound);
00219
00220 uint64_t SavePositionMapDelta(uint64_t first_frame, uint64_t last_frame);
00221 virtual void SeekReset(long long newkey, uint skipFrames,
00222 bool doFlush, bool discardFrames);
00223
00224 void SetTranscoding(bool value) { transcoding = value; }
00225
00226 bool IsErrored() const { return errored; }
00227
00228 bool HasPositionMap(void) const { return GetPositionMapSize(); }
00229
00230 void SetWaitForChange(void);
00231 bool GetWaitForChange(void) const;
00232 void SetReadAdjust(long long adjust);
00233
00234
00235 void SetDecodeAllSubtitles(bool val) { decodeAllSubtitles = val; }
00236 virtual QStringList GetTracks(uint type) const;
00237 virtual uint GetTrackCount(uint type) const
00238 { return tracks[type].size(); }
00239
00240 virtual int GetTrackLanguageIndex(uint type, uint trackNo) const;
00241 virtual QString GetTrackDesc(uint type, uint trackNo) const;
00242 virtual int SetTrack(uint type, int trackNo);
00243 int GetTrack(uint type) const { return currentTrack[type]; }
00244 StreamInfo GetTrackInfo(uint type, uint trackNo) const;
00245 inline int IncrementTrack(uint type);
00246 inline int DecrementTrack(uint type);
00247 inline int ChangeTrack(uint type, int dir);
00248 virtual bool InsertTrack(uint type, const StreamInfo&);
00249 inline int NextTrack(uint type);
00250
00251 virtual int GetTeletextDecoderType(void) const { return -1; }
00252
00253 virtual QString GetXDS(const QString&) const { return QString::null; }
00254 virtual QByteArray GetSubHeader(uint trackNo) const { return QByteArray(); }
00255 virtual void GetAttachmentData(uint trackNo, QByteArray &filename,
00256 QByteArray &data) {}
00257
00258
00259 virtual bool SetAudioByComponentTag(int) { return false; }
00260 virtual bool SetVideoByComponentTag(int) { return false; }
00261
00262 void SaveTotalDuration(void);
00263 void ResetTotalDuration(void) { totalDuration = AVRationalInit(0); }
00264 void SaveTotalFrames(void);
00265 bool GetVideoInverted(void) const { return video_inverted; }
00266 void TrackTotalDuration(bool track) { trackTotalDuration = track; }
00267
00268 protected:
00269 virtual int AutoSelectTrack(uint type);
00270 inline void AutoSelectTracks(void);
00271 inline void ResetTracks(void);
00272
00273 void FileChanged(void);
00274
00275 virtual bool DoRewindSeek(long long desiredFrame);
00276 virtual void DoFastForwardSeek(long long desiredFrame, bool &needflush);
00277
00278 long long ConditionallyUpdatePosMap(long long desiredFrame);
00279 long long GetLastFrameInPosMap(void) const;
00280 unsigned long GetPositionMapSize(void) const;
00281
00282 typedef struct posmapentry
00283 {
00284 long long index;
00285 long long adjFrame;
00286 long long pos;
00287 } PosMapEntry;
00288 long long GetKey(const PosMapEntry &entry) const;
00289
00290 MythPlayer *m_parent;
00291 ProgramInfo *m_playbackinfo;
00292 AudioPlayer *m_audio;
00293 RingBuffer *ringBuffer;
00294
00295 int current_width;
00296 int current_height;
00297 float current_aspect;
00298 double fps;
00299 uint bitrate;
00300
00301 long long framesPlayed;
00302 long long framesRead;
00303 AVRational totalDuration;
00304 long long lastKey;
00305 int keyframedist;
00306 long long indexOffset;
00307
00308
00309
00310
00311 bool trackTotalDuration;
00312
00313 EofState ateof;
00314 bool exitafterdecoded;
00315 bool transcoding;
00316
00317 bool hasFullPositionMap;
00318 bool recordingHasPositionMap;
00319 bool posmapStarted;
00320 MarkTypes positionMapType;
00321
00322 mutable QMutex m_positionMapLock;
00323 vector<PosMapEntry> m_positionMap;
00324 frm_pos_map_t m_frameToDurMap;
00325 frm_pos_map_t m_durToFrameMap;
00326 bool dontSyncPositionMap;
00327 mutable QDateTime m_lastPositionMapUpdate;
00328
00329 uint64_t seeksnap;
00330 bool livetv;
00331 bool watchingrecording;
00332
00333 bool hasKeyFrameAdjustTable;
00334
00335 bool lowbuffers;
00336
00337 bool getrawframes;
00338 bool getrawvideo;
00339
00340 bool errored;
00341
00342 bool waitingForChange;
00343 long long readAdjust;
00344 bool justAfterChange;
00345 bool video_inverted;
00346
00347
00348 bool decodeAllSubtitles;
00349 int currentTrack[kTrackTypeCount];
00350 sinfo_vec_t tracks[kTrackTypeCount];
00351 StreamInfo wantedTrack[kTrackTypeCount];
00352 StreamInfo selectedTrack[(uint)kTrackTypeCount];
00354 vector<int> languagePreference;
00355 };
00356
00357 inline int DecoderBase::IncrementTrack(uint type)
00358 {
00359 int next_track = -1;
00360 int size = tracks[type].size();
00361 if (size)
00362 next_track = (max(-1, currentTrack[type]) + 1) % size;
00363 return SetTrack(type, next_track);
00364 }
00365
00366 inline int DecoderBase::DecrementTrack(uint type)
00367 {
00368 int next_track = -1;
00369 int size = tracks[type].size();
00370 if (size)
00371 next_track = (max(+0, currentTrack[type]) + size - 1) % size;
00372 return SetTrack(type, next_track);
00373 }
00374
00375 inline int DecoderBase::ChangeTrack(uint type, int dir)
00376 {
00377 if (dir > 0)
00378 return IncrementTrack(type);
00379 else
00380 return DecrementTrack(type);
00381 }
00382
00383 inline void DecoderBase::AutoSelectTracks(void)
00384 {
00385 for (uint i = 0; i < kTrackTypeCount; i++)
00386 AutoSelectTrack(i);
00387 }
00388
00389 inline void DecoderBase::ResetTracks(void)
00390 {
00391 for (uint i = 0; i < kTrackTypeCount; i++)
00392 currentTrack[i] = -1;
00393 }
00394
00395 inline int DecoderBase::NextTrack(uint type)
00396 {
00397 int next_track = -1;
00398 int size = tracks[type].size();
00399 if (size)
00400 next_track = (max(0, currentTrack[type]) + 1) % size;
00401 return next_track;
00402 }
00403 #endif