00001 #ifndef AVFORMATDECODER_H_
00002 #define AVFORMATDECODER_H_
00003
00004 #include <qstring.h>
00005 #include <qmap.h>
00006
00007 #include "programinfo.h"
00008 #include "format.h"
00009 #include "decoderbase.h"
00010 #include "vbilut.h"
00011 #include "h264utils.h"
00012
00013 extern "C" {
00014 #include "frame.h"
00015 #include "../libavcodec/avcodec.h"
00016 #include "../libavformat/avformat.h"
00017 }
00018
00019 #include "avfringbuffer.h"
00020
00021 #define CODEC_IS_MPEG(c) (c == CODEC_ID_MPEG1VIDEO || \
00022 c == CODEC_ID_MPEG2VIDEO || \
00023 c == CODEC_ID_MPEG2VIDEO_DVDV || \
00024 c == CODEC_ID_MPEG2VIDEO_XVMC || \
00025 c == CODEC_ID_MPEG2VIDEO_XVMC_VLD)
00026
00027 #define CODEC_IS_HW_ACCEL(c) (c == CODEC_ID_MPEG2VIDEO_DVDV || \
00028 c == CODEC_ID_MPEG2VIDEO_XVMC || \
00029 c == CODEC_ID_MPEG2VIDEO_XVMC_VLD)
00030
00031 class TeletextDecoder;
00032 class CC608Decoder;
00033 class CC708Decoder;
00034 class InteractiveTV;
00035 class ProgramInfo;
00036 class MythSqlDatabase;
00037
00038 extern "C" void HandleStreamChange(void*);
00039
00040 class AudioInfo
00041 {
00042 public:
00043 AudioInfo() :
00044 codec_id(CODEC_ID_NONE), sample_size(-2), sample_rate(-1),
00045 channels(-1), do_passthru(false)
00046 {;}
00047
00048 AudioInfo(CodecID id, int sr, int ch, bool passthru) :
00049 codec_id(id), sample_size(ch*2), sample_rate(sr),
00050 channels(ch), do_passthru(passthru)
00051 {;}
00052
00053 CodecID codec_id;
00054 int sample_size, sample_rate, channels;
00055 bool do_passthru;
00056
00058 int bps(void) const
00059 {
00060 uint chan = (channels) ? channels : 2;
00061 return (8 * sample_size) / chan;
00062 }
00063 bool operator==(const AudioInfo &o) const
00064 {
00065 return (codec_id==o.codec_id && channels==o.channels &&
00066 sample_size==o.sample_size && sample_rate==o.sample_rate &&
00067 do_passthru==o.do_passthru);
00068 }
00069 QString toString() const
00070 {
00071 return QString("id(%1) %2Hz %3ch %4bps%5")
00072 .arg(codec_id_string(codec_id),4).arg(sample_rate,5)
00073 .arg(channels,2).arg(bps(),3)
00074 .arg((do_passthru) ? "pt":"",3);
00075 }
00076 };
00077
00079
00083 class AvFormatDecoder : public DecoderBase
00084 {
00085 friend void HandleStreamChange(void*);
00086 public:
00087 AvFormatDecoder(NuppelVideoPlayer *parent, ProgramInfo *pginfo,
00088 bool use_null_video_out, bool allow_libmpeg2 = true);
00089 ~AvFormatDecoder();
00090
00091 void CloseCodecs();
00092 void CloseContext();
00093 void Reset(void);
00094 void Reset(bool reset_video_data = true, bool seek_reset = true);
00095
00098 static bool CanHandle(char testbuf[kDecoderProbeBufferSize],
00099 const QString &filename,
00100 int testbufsize = kDecoderProbeBufferSize);
00101
00103 int OpenFile(RingBuffer *rbuffer, bool novideo,
00104 char testbuf[kDecoderProbeBufferSize],
00105 int testbufsize = kDecoderProbeBufferSize);
00106
00107 bool GetFrame(int onlyvideo);
00108
00109 bool isLastFrameKey(void) { return false; }
00110
00112 void WriteStoredData(RingBuffer *rb, bool storevid, long timecodeOffset)
00113 { (void)rb; (void)storevid; (void)timecodeOffset;}
00114
00116 void SetRawAudioState(bool state) { (void)state; }
00117
00119 bool GetRawAudioState(void) const { return false; }
00120
00122 void SetRawVideoState(bool state) { (void)state; }
00123
00125 bool GetRawVideoState(void) const { return false; }
00126
00128 long UpdateStoredFrameNum(long frame) { (void)frame; return 0;}
00129
00130 QString GetCodecDecoderName(void) const;
00131 MythCodecID GetVideoCodecID(void) const { return video_codec_id; }
00132 void *GetVideoCodecPrivate(void);
00133
00134 virtual void SetDisablePassThrough(bool disable);
00135 void AddTextData(unsigned char *buf, int len, long long timecode, char type);
00136
00137 virtual QString GetTrackDesc(uint type, uint trackNo) const;
00138 virtual int SetTrack(uint type, int trackNo);
00139
00140 int ScanStreams(bool novideo);
00141
00142 virtual bool DoRewind(long long desiredFrame, bool doflush = true);
00143 virtual bool DoFastForward(long long desiredFrame, bool doflush = true);
00144
00145 virtual int GetTeletextDecoderType(void) const;
00146 virtual void SetTeletextDecoderViewer(TeletextViewer*);
00147
00148 virtual QString GetXDS(const QString&) const;
00149
00150
00151 virtual bool SetAudioByComponentTag(int tag);
00152 virtual bool SetVideoByComponentTag(int tag);
00153
00154 protected:
00155 RingBuffer *getRingBuf(void) { return ringBuffer; }
00156
00157 virtual int AutoSelectTrack(uint type);
00158
00159 void ScanATSCCaptionStreams(int av_stream_index);
00160 void ScanTeletextCaptions(int av_stream_index);
00161 void ScanDSMCCStreams(void);
00162 int AutoSelectAudioTrack(void);
00163
00164 private:
00165 friend int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic);
00166 friend void release_avf_buffer(struct AVCodecContext *c, AVFrame *pic);
00167
00168 friend int get_avf_buffer_xvmc(struct AVCodecContext *c, AVFrame *pic);
00169 friend void release_avf_buffer_xvmc(struct AVCodecContext *c, AVFrame *pic);
00170 friend void render_slice_xvmc(struct AVCodecContext *c, const AVFrame *src,
00171 int offset[4], int y, int type, int height);
00172
00173 friend void decode_cc_dvd(struct AVCodecContext *c, const uint8_t *buf, int buf_size);
00174
00175 friend int open_avf(URLContext *h, const char *filename, int flags);
00176 friend int read_avf(URLContext *h, uint8_t *buf, int buf_size);
00177 friend int write_avf(URLContext *h, uint8_t *buf, int buf_size);
00178 friend offset_t seek_avf(URLContext *h, offset_t offset, int whence);
00179 friend int close_avf(URLContext *h);
00180
00181 void DecodeDTVCC(const uint8_t *buf);
00182 void InitByteContext(void);
00183 void InitVideoCodec(AVStream *stream, AVCodecContext *enc,
00184 bool selectedStream = false);
00185
00187 void MpegPreProcessPkt(AVStream *stream, AVPacket *pkt);
00188 void H264PreProcessPkt(AVStream *stream, AVPacket *pkt);
00189
00190 void ProcessVBIDataPacket(const AVStream *stream, const AVPacket *pkt);
00191 void ProcessDVBDataPacket(const AVStream *stream, const AVPacket *pkt);
00192 void ProcessDSMCCPacket(const AVStream *stream, const AVPacket *pkt);
00193
00194 float GetMpegAspect(AVCodecContext *context, int aspect_ratio_info,
00195 int width, int height);
00196
00197 void SeekReset(long long, uint skipFrames, bool doFlush, bool discardFrames);
00198
00199 bool SetupAudioStream(void);
00200 void SetupAudioStreamSubIndexes(int streamIndex);
00201 void RemoveAudioStreams();
00202
00205 void HandleGopStart(AVPacket *pkt);
00206
00207 bool GenerateDummyVideoFrame(void);
00208 bool HasVideo(const AVFormatContext *ic);
00209
00210 private:
00211 class AvFormatDecoderPrivate *d;
00212 H264::KeyframeSequencer *h264_kf_seq;
00213
00214 AVFormatContext *ic;
00215 AVFormatParameters params;
00216
00217 URLContext readcontext;
00218
00219 int frame_decoded;
00220 VideoFrame *decoded_video_frame;
00221 AVFRingBuffer *avfRingBuffer;
00222
00223 bool directrendering;
00224 bool drawband;
00225
00226 bool no_dts_hack;
00227
00228 bool gopset;
00230 bool seen_gop;
00231 int seq_count;
00232
00233 QPtrList<AVPacket> storedPackets;
00234
00235 int firstgoppos;
00236 int prevgoppos;
00237
00238 bool gotvideo;
00239
00240 uint32_t start_code_state;
00241
00242 long long lastvpts;
00243 long long lastapts;
00244 long long lastccptsu;
00245
00246 bool using_null_videoout;
00247 MythCodecID video_codec_id;
00248
00249 int maxkeyframedist;
00250
00251
00252 CC608Decoder *ccd608;
00253 CC708Decoder *ccd708;
00254 TeletextDecoder *ttd;
00255 int cc608_parity_table[256];
00256
00257
00258 InteractiveTV *itv;
00259 int selectedVideoIndex;
00260
00261
00262 short int *audioSamples;
00263 bool allow_ac3_passthru;
00264 bool allow_dts_passthru;
00265 bool disable_passthru;
00266 uint max_channels;
00267
00268 VideoFrame *dummy_frame;
00269
00270 AudioInfo audioIn;
00271 AudioInfo audioOut;
00272
00273
00274 int lastdvdtitle;
00275 bool decodeStillFrame;
00276 bool dvd_xvmc_enabled;
00277 bool dvd_video_codec_changed;
00278 bool dvdTitleChanged;
00279 bool mpeg_seq_end_seen;
00280 };
00281
00282 #endif
00283
00284