00001
00002 #ifndef AVFORMATDECODER_H_
00003 #define AVFORMATDECODER_H_
00004
00005 #include <stdint.h>
00006
00007 #include <QString>
00008 #include <QMap>
00009 #include <QList>
00010
00011 #include "programinfo.h"
00012 #include "format.h"
00013 #include "decoderbase.h"
00014 #include "privatedecoder.h"
00015 #include "audiooutputsettings.h"
00016 #include "spdifencoder.h"
00017 #include "vbilut.h"
00018 #include "H264Parser.h"
00019 #include "videodisplayprofile.h"
00020 #include "mythplayer.h"
00021
00022 extern "C" {
00023 #include "frame.h"
00024 #include "libavcodec/avcodec.h"
00025 #include "libavformat/avformat.h"
00026 #include "libavcodec/audioconvert.h"
00027 }
00028
00029 #include "avfringbuffer.h"
00030
00031 class TeletextDecoder;
00032 class CC608Decoder;
00033 class CC708Decoder;
00034 class SubtitleReader;
00035 class InteractiveTV;
00036 class ProgramInfo;
00037 class MythSqlDatabase;
00038
00039 struct SwsContext;
00040
00041 extern "C" void HandleStreamChange(void*);
00042 extern "C" void HandleDVDStreamChange(void*);
00043 extern "C" void HandleBDStreamChange(void*);
00044
00045 class AudioInfo
00046 {
00047 public:
00048 AudioInfo() :
00049 codec_id(CODEC_ID_NONE), format(FORMAT_NONE), sample_size(-2),
00050 sample_rate(-1), channels(-1), codec_profile(0),
00051 do_passthru(false), original_channels(-1)
00052 {;}
00053
00054 AudioInfo(CodecID id, AudioFormat fmt, int sr, int ch, bool passthru,
00055 int original_ch, int profile = 0) :
00056 codec_id(id), format(fmt),
00057 sample_size(ch * AudioOutputSettings::SampleSize(fmt)),
00058 sample_rate(sr), channels(ch), codec_profile(profile),
00059 do_passthru(passthru), original_channels(original_ch)
00060 {
00061 }
00062
00063 CodecID codec_id;
00064 AudioFormat format;
00065 int sample_size, sample_rate, channels, codec_profile;
00066 bool do_passthru;
00067 int original_channels;
00068 bool operator==(const AudioInfo &o) const
00069 {
00070 return (codec_id==o.codec_id && channels==o.channels &&
00071 sample_size==o.sample_size && sample_rate==o.sample_rate &&
00072 format==o.format && do_passthru==o.do_passthru &&
00073 original_channels==o.original_channels &&
00074 codec_profile == o.codec_profile);
00075 }
00076 QString toString() const
00077 {
00078 return QString("id(%1) %2Hz %3ch %4bps %5 (profile %6)")
00079 .arg(ff_codec_id_string(codec_id),4).arg(sample_rate,6)
00080 .arg(channels,2).arg(AudioOutputSettings::FormatToBits(format),2)
00081 .arg((do_passthru) ? "pt":"",3).arg(codec_profile);
00082 }
00083 };
00084
00086
00089 class AvFormatDecoder : public DecoderBase
00090 {
00091 friend void HandleStreamChange(void*);
00092 friend void HandleDVDStreamChange(void*);
00093 friend void HandleBDStreamChange(void*);
00094 public:
00095 static void GetDecoders(render_opts &opts);
00096 AvFormatDecoder(MythPlayer *parent, const ProgramInfo &pginfo,
00097 PlayerFlags flags);
00098 ~AvFormatDecoder();
00099
00100 virtual void SetEof(bool eof);
00101
00102 void CloseCodecs();
00103 void CloseContext();
00104 virtual void Reset(bool reset_video_data, bool seek_reset, bool reset_file);
00105
00108 static bool CanHandle(char testbuf[kDecoderProbeBufferSize],
00109 const QString &filename,
00110 int testbufsize = kDecoderProbeBufferSize);
00111
00113 int OpenFile(RingBuffer *rbuffer, bool novideo,
00114 char testbuf[kDecoderProbeBufferSize],
00115 int testbufsize = kDecoderProbeBufferSize);
00116
00117 virtual bool GetFrame(DecodeType);
00118
00119 virtual bool IsLastFrameKey(void) const { return false; }
00120
00121 virtual bool IsCodecMPEG(void) const
00122 { return codec_is_mpeg; }
00123
00125 void WriteStoredData(RingBuffer *rb, bool storevid, long timecodeOffset)
00126 { (void)rb; (void)storevid; (void)timecodeOffset;}
00127
00129 void SetRawAudioState(bool state) { (void)state; }
00130
00132 bool GetRawAudioState(void) const { return false; }
00133
00135 void SetRawVideoState(bool state) { (void)state; }
00136
00138 bool GetRawVideoState(void) const { return false; }
00139
00141 long UpdateStoredFrameNum(long frame) { (void)frame; return 0;}
00142
00143 QString GetCodecDecoderName(void) const;
00144 QString GetRawEncodingType(void);
00145 MythCodecID GetVideoCodecID(void) const { return video_codec_id; }
00146 void *GetVideoCodecPrivate(void);
00147
00148 virtual void SetDisablePassThrough(bool disable);
00149 void AddTextData(unsigned char *buf, int len, int64_t timecode, char type);
00150
00151 virtual QString GetTrackDesc(uint type, uint trackNo) const;
00152 virtual int SetTrack(uint type, int trackNo);
00153
00154 int ScanStreams(bool novideo);
00155 int FindStreamInfo(void);
00156
00157 virtual int GetNumChapters();
00158 virtual void GetChapterTimes(QList<long long> ×);
00159 virtual int GetCurrentChapter(long long framesPlayed);
00160 virtual long long GetChapter(int chapter);
00161 virtual bool DoRewind(long long desiredFrame, bool doflush = true);
00162 virtual bool DoFastForward(long long desiredFrame, bool doflush = true);
00163
00164 virtual int64_t NormalizeVideoTimecode(int64_t timecode);
00165 virtual int64_t NormalizeVideoTimecode(AVStream *st, int64_t timecode);
00166
00167 virtual int GetTeletextDecoderType(void) const;
00168
00169 virtual QString GetXDS(const QString&) const;
00170 virtual QByteArray GetSubHeader(uint trackNo) const;
00171 virtual void GetAttachmentData(uint trackNo, QByteArray &filename,
00172 QByteArray &data);
00173
00174
00175 virtual bool SetAudioByComponentTag(int tag);
00176 virtual bool SetVideoByComponentTag(int tag);
00177
00178
00179 virtual int GetSubtitleLanguage(uint subtitle_index, uint stream_index);
00180 virtual int GetCaptionLanguage(TrackTypes trackType, int service_num);
00181 virtual int GetAudioLanguage(uint audio_index, uint stream_index);
00182
00183 protected:
00184 RingBuffer *getRingBuf(void) { return ringBuffer; }
00185
00186 virtual int AutoSelectTrack(uint type);
00187
00188 void ScanATSCCaptionStreams(int av_stream_index);
00189 void UpdateATSCCaptionTracks(void);
00190 void UpdateCaptionTracksFromStreams(bool check_608, bool check_708);
00191 void ScanTeletextCaptions(int av_stream_index);
00192 void ScanRawTextCaptions(int av_stream_index);
00193 void ScanDSMCCStreams(void);
00194 int AutoSelectAudioTrack(void);
00195 int filter_max_ch(const AVFormatContext *ic,
00196 const sinfo_vec_t &tracks,
00197 const vector<int> &fs,
00198 enum CodecID codecId = CODEC_ID_NONE,
00199 int profile = -1);
00200
00201 friend int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic);
00202 friend void release_avf_buffer(struct AVCodecContext *c, AVFrame *pic);
00203
00204 friend int open_avf(URLContext *h, const char *filename, int flags);
00205 friend int read_avf(URLContext *h, uint8_t *buf, int buf_size);
00206 friend int write_avf(URLContext *h, uint8_t *buf, int buf_size);
00207 friend int64_t seek_avf(URLContext *h, int64_t offset, int whence);
00208 friend int close_avf(URLContext *h);
00209
00210 void DecodeDTVCC(const uint8_t *buf, uint buf_size, bool scte);
00211 void InitByteContext(void);
00212 void InitVideoCodec(AVStream *stream, AVCodecContext *enc,
00213 bool selectedStream = false);
00214
00216 void MpegPreProcessPkt(AVStream *stream, AVPacket *pkt);
00217 bool H264PreProcessPkt(AVStream *stream, AVPacket *pkt);
00218 bool PreProcessVideoPacket(AVStream *stream, AVPacket *pkt);
00219 bool ProcessVideoPacket(AVStream *stream, AVPacket *pkt);
00220 bool ProcessVideoFrame(AVStream *stream, AVFrame *mpa_pic);
00221 bool ProcessAudioPacket(AVStream *stream, AVPacket *pkt,
00222 DecodeType decodetype);
00223 bool ProcessSubtitlePacket(AVStream *stream, AVPacket *pkt);
00224 bool ProcessRawTextPacket(AVPacket *pkt);
00225 bool ProcessDataPacket(AVStream *curstream, AVPacket *pkt,
00226 DecodeType decodetype);
00227
00228 void ProcessVBIDataPacket(const AVStream *stream, const AVPacket *pkt);
00229 void ProcessDVBDataPacket(const AVStream *stream, const AVPacket *pkt);
00230 void ProcessDSMCCPacket(const AVStream *stream, const AVPacket *pkt);
00231
00232 float GetMpegAspect(AVCodecContext *context, int aspect_ratio_info,
00233 int width, int height);
00234
00235 void SeekReset(long long, uint skipFrames, bool doFlush, bool discardFrames);
00236
00237 inline bool DecoderWillDownmix(const AVCodecContext *ctx);
00238 bool DoPassThrough(const AVCodecContext *ctx, bool withProfile=true);
00239 bool SetupAudioStream(void);
00240 void SetupAudioStreamSubIndexes(int streamIndex);
00241 void RemoveAudioStreams();
00242
00245 void HandleGopStart(AVPacket *pkt, bool can_reliably_parse_keyframes);
00246
00247 bool GenerateDummyVideoFrames(void);
00248 bool HasVideo(const AVFormatContext *ic);
00249 float normalized_fps(AVStream *stream, AVCodecContext *enc);
00250 void av_update_stream_timings_video(AVFormatContext *ic);
00251
00252 virtual void UpdateFramesPlayed(void);
00253 virtual bool DoRewindSeek(long long desiredFrame);
00254 virtual void DoFastForwardSeek(long long desiredFrame, bool &needflush);
00255 virtual void StreamChangeCheck(void) { }
00256 virtual void PostProcessTracks(void) { }
00257
00258 int DecodeAudio(AVCodecContext *ctx, uint8_t *buffer, int &data_size,
00259 AVPacket *pkt);
00260 PrivateDecoder *private_dec;
00261
00262 bool is_db_ignored;
00263
00264 H264Parser *m_h264_parser;
00265
00266 AVFormatContext *ic;
00267
00268
00269 URLContext readcontext;
00270
00271 int frame_decoded;
00272 VideoFrame *decoded_video_frame;
00273 AVFRingBuffer *avfRingBuffer;
00274
00275 struct SwsContext *sws_ctx;
00276 bool directrendering;
00277
00278 bool no_dts_hack;
00279 bool dorewind;
00280
00281 bool gopset;
00283 bool seen_gop;
00284 int seq_count;
00285
00286 QList<AVPacket*> storedPackets;
00287
00288 int prevgoppos;
00289
00290
00291 bool gotVideoFrame;
00292 bool hasVideo;
00293 bool needDummyVideoFrames;
00294 bool skipaudio;
00295 bool allowedquit;
00296
00297 uint32_t start_code_state;
00298
00299 long long lastvpts;
00300 long long lastapts;
00301 long long lastccptsu;
00302 long long firstvpts;
00303 bool firstvptsinuse;
00304
00305 int64_t faulty_pts;
00306 int64_t faulty_dts;
00307 int64_t last_pts_for_fault_detection;
00308 int64_t last_dts_for_fault_detection;
00309 bool pts_detected;
00310 bool reordered_pts_detected;
00311 bool pts_selected;
00312
00313 bool force_dts_timestamps;
00314
00315 PlayerFlags playerFlags;
00316 MythCodecID video_codec_id;
00317
00318 int maxkeyframedist;
00319
00320
00321 bool ignore_scte;
00322 uint invert_scte_field;
00323 uint last_scte_field;
00324 CC608Decoder *ccd608;
00325 CC708Decoder *ccd708;
00326 TeletextDecoder *ttd;
00327 int cc608_parity_table[256];
00331 bool ccX08_in_pmt[64+4];
00335 bool ccX08_in_tracks[64+4];
00337 QList<StreamInfo> pmt_tracks;
00339 QList<TrackType> pmt_track_types;
00342 QList<StreamInfo> stream_tracks;
00345 QList<TrackType> stream_track_types;
00346
00347
00348 InteractiveTV *itv;
00349
00350
00351 uint8_t *audioSamples;
00352 bool disable_passthru;
00353
00354 AudioInfo audioIn;
00355 AudioInfo audioOut;
00356
00357 float m_fps;
00358 bool codec_is_mpeg;
00359 };
00360
00361 #endif
00362
00363