00001 #ifndef NUPPELVIDEORECORDER
00002 #define NUPPELVIDEORECORDER
00003
00004
00005 #include <stdint.h>
00006
00007 #include <sys/time.h>
00008 #include <time.h>
00009 #ifdef MMX
00010 #undef MMX
00011 #define MMXBLAH
00012 #endif
00013 #include <lame/lame.h>
00014 #ifdef MMXBLAH
00015 #define MMX
00016 #endif
00017
00018 #undef HAVE_AV_CONFIG_H
00019 extern "C" {
00020 #include "libavcodec/avcodec.h"
00021 }
00022
00023
00024 #include <vector>
00025 using namespace std;
00026
00027
00028 #include <QString>
00029
00030
00031 #include "v4lrecorder.h"
00032 #include "format.h"
00033 #include "cc608decoder.h"
00034 #include "filter.h"
00035 #include "minilzo.h"
00036 #include "mthread.h"
00037
00038 #include "mythtvexp.h"
00039
00040 struct video_audio;
00041 class RTjpeg;
00042 class RingBuffer;
00043 class ChannelBase;
00044 class FilterManager;
00045 class FilterChain;
00046 class AudioInput;
00047 class NuppelVideoRecorder;
00048
00049 class NVRWriteThread : public MThread
00050 {
00051 public:
00052 NVRWriteThread(NuppelVideoRecorder *parent) :
00053 MThread("NVRWrite"), m_parent(parent) {}
00054 virtual ~NVRWriteThread() { wait(); m_parent = NULL; }
00055 virtual void run(void);
00056 private:
00057 NuppelVideoRecorder *m_parent;
00058 };
00059
00060 class NVRAudioThread : public MThread
00061 {
00062 public:
00063 NVRAudioThread(NuppelVideoRecorder *parent) :
00064 MThread("NVRAudio"), m_parent(parent) {}
00065 virtual ~NVRAudioThread() { wait(); m_parent = NULL; }
00066 virtual void run(void);
00067 private:
00068 NuppelVideoRecorder *m_parent;
00069 };
00070
00071 class MTV_PUBLIC NuppelVideoRecorder : public V4LRecorder, public CC608Input
00072 {
00073 friend class NVRWriteThread;
00074 friend class NVRAudioThread;
00075 public:
00076 NuppelVideoRecorder(TVRec *rec, ChannelBase *channel);
00077 ~NuppelVideoRecorder();
00078
00079 void SetOption(const QString &name, int value);
00080 void SetOption(const QString &name, const QString &value);
00081
00082 void SetOptionsFromProfile(RecordingProfile *profile,
00083 const QString &videodev,
00084 const QString &audiodev,
00085 const QString &vbidev);
00086
00087 void Initialize(void);
00088 void run(void);
00089
00090 virtual void Pause(bool clear = true);
00091 virtual bool IsPaused(bool holding_lock = false) const;
00092
00093 bool IsRecording(void);
00094
00095 long long GetFramesWritten(void);
00096
00097 bool Open(void);
00098 int GetVideoFd(void);
00099 void Reset(void);
00100
00101 void SetVideoFilters(QString &filters);
00102 void SetTranscoding(bool value) { transcoding = value; };
00103
00104 void SetNextRecording(const ProgramInfo*, RingBuffer*);
00105 void ResetForNewFile(void);
00106 void FinishRecording(void);
00107 void StartNewFile(void);
00108
00109
00110 void StreamAllocate(void);
00111 void WriteHeader(void);
00112 void WriteSeekTable(void);
00113 void WriteKeyFrameAdjustTable(
00114 const vector<struct kfatable_entry> &kfa_table);
00115 void UpdateSeekTable(int frame_num, long offset = 0);
00116
00117 bool SetupAVCodecVideo(void);
00118 void SetupRTjpeg(void);
00119 int AudioInit(bool skipdevice = false);
00120 void SetVideoAspect(float newAspect) {video_aspect = newAspect; };
00121 void WriteVideo(VideoFrame *frame, bool skipsync = false,
00122 bool forcekey = false);
00123 void WriteAudio(unsigned char *buf, int fnum, int timecode);
00124 void WriteText(unsigned char *buf, int len, int timecode, int pagenr);
00125
00126 void SetNewVideoParams(double newaspect);
00127
00128 protected:
00129 void doWriteThread(void);
00130 void doAudioThread(void);
00131
00132 private:
00133 inline void WriteFrameheader(rtframeheader *fh);
00134
00135 void WriteFileHeader(void);
00136
00137 void InitBuffers(void);
00138 void InitFilters(void);
00139 void ResizeVideoBuffers(void);
00140
00141 bool MJPEGInit(void);
00142
00143 void KillChildren(void);
00144
00145 void BufferIt(unsigned char *buf, int len = -1, bool forcekey = false);
00146
00147 int CreateNuppelFile(void);
00148
00149 void ProbeV4L2(void);
00150 bool SetFormatV4L2(void);
00151 void DoV4L1(void);
00152 void DoV4L2(void);
00153 void DoMJPEG(void);
00154
00155 virtual void FormatTT(struct VBIData*);
00156 virtual void FormatCC(uint code1, uint code2);
00157 virtual void AddTextData(unsigned char*,int,int64_t,char);
00158
00159 void UpdateResolutions(void);
00160
00161 int fd;
00162 signed char *strm;
00163 unsigned int lf, tf;
00164 int M1, M2, Q;
00165 int width, height;
00166 int pip_mode;
00167 int pid, pid2;
00168 int inputchannel;
00169 int compression;
00170 int compressaudio;
00171 AudioInput *audio_device;
00172 unsigned long long audiobytes;
00173 int audio_channels;
00174 int audio_bits;
00175 int audio_bytes_per_sample;
00176 int audio_samplerate;
00177 int effectivedsp;
00178
00179 int usebttv;
00180 float video_aspect;
00181
00182 bool transcoding;
00183
00184 int mp3quality;
00185 char *mp3buf;
00186 int mp3buf_size;
00187 lame_global_flags *gf;
00188
00189 RTjpeg *rtjc;
00190
00191 #define OUT_LEN (1024*1024 + 1024*1024 / 64 + 16 + 3)
00192 lzo_byte out[OUT_LEN];
00193 #define HEAP_ALLOC(var,size) \
00194 long __LZO_MMODEL var [ ((size) + (sizeof(long) - 1)) / sizeof(long) ]
00195 HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);
00196
00197 vector<struct vidbuffertype *> videobuffer;
00198 vector<struct audbuffertype *> audiobuffer;
00199 vector<struct txtbuffertype *> textbuffer;
00200
00201 int act_video_encode;
00202 int act_video_buffer;
00203
00204 int act_audio_encode;
00205 int act_audio_buffer;
00206 long long act_audio_sample;
00207
00208 int act_text_encode;
00209 int act_text_buffer;
00210
00211 int video_buffer_count;
00212 int audio_buffer_count;
00213 int text_buffer_count;
00214
00215 long video_buffer_size;
00216 long audio_buffer_size;
00217 long text_buffer_size;
00218
00219 struct timeval stm;
00220 struct timezone tzone;
00221
00222 NVRWriteThread *write_thread;
00223 NVRAudioThread *audio_thread;
00224
00225 bool recording;
00226
00227 int keyframedist;
00228 vector<struct seektable_entry> *seektable;
00229 long long lastPositionMapPos;
00230
00231 long long extendeddataOffset;
00232
00233 long long framesWritten;
00234
00235 bool livetv;
00236 bool writepaused;
00237 bool audiopaused;
00238 bool mainpaused;
00239
00240 double framerate_multiplier;
00241 double height_multiplier;
00242
00243 int last_block;
00244 int firsttc;
00245 long int oldtc;
00246 int startnum;
00247 int frameofgop;
00248 int lasttimecode;
00249 int audio_behind;
00250
00251 bool useavcodec;
00252
00253 AVCodec *mpa_vidcodec;
00254 AVCodecContext *mpa_vidctx;
00255 AVFrame mpa_picture;
00256
00257 int targetbitrate;
00258 int scalebitrate;
00259 int maxquality;
00260 int minquality;
00261 int qualdiff;
00262 int mp4opts;
00263 int mb_decision;
00265 int encoding_thread_count;
00266
00267 QString videoFilterList;
00268 FilterChain *videoFilters;
00269 FilterManager *FiltMan;
00270
00271 VideoFrameType inpixfmt;
00272 PixelFormat picture_format;
00273 uint32_t v4l2_pixelformat;
00274 int w_out;
00275 int h_out;
00276
00277 bool hardware_encode;
00278 int hmjpg_quality;
00279 int hmjpg_hdecimation;
00280 int hmjpg_vdecimation;
00281 int hmjpg_maxw;
00282
00283 bool cleartimeonpause;
00284
00285 bool usingv4l2;
00286 int channelfd;
00287
00288 long long prev_bframe_save_pos;
00289
00290 ChannelBase *channelObj;
00291
00292 bool skip_btaudio;
00293
00294 bool correct_bttv;
00295
00296 int volume;
00297
00298 CC608Decoder *ccd;
00299
00300 bool go7007;
00301 bool resetcapture;
00302 };
00303
00304 #endif