MythTV  master
NuppelVideoRecorder.h
Go to the documentation of this file.
1 #ifndef NUPPELVIDEORECORDER
2 #define NUPPELVIDEORECORDER
3 
4 // C headers
5 #include <sys/time.h>
6 #ifdef MMX
7 #undef MMX
8 #define MMXBLAH
9 #endif
10 #include <lame/lame.h>
11 #ifdef MMXBLAH
12 #define MMX
13 #endif
14 
15 #include "mythconfig.h"
16 
17 #undef HAVE_AV_CONFIG_H
18 extern "C" {
19 #include "libavcodec/avcodec.h"
20 }
21 
22 // C++ std headers
23 #include <cstdint>
24 #include <ctime>
25 #include <vector>
26 using namespace std;
27 
28 // Qt headers
29 #include <QString>
30 
31 // MythTV headers
32 #include "v4lrecorder.h"
33 #include "format.h"
34 #include "cc608decoder.h"
35 #include "filter.h"
36 #include "lzo/lzo1x.h"
37 #include "mthread.h"
38 
39 #include "mythtvexp.h"
40 
41 #define KEYFRAMEDIST 30
42 
43 struct video_audio;
44 class RTjpeg;
45 class RingBuffer;
46 class ChannelBase;
47 class FilterManager;
48 class FilterChain;
49 class AudioInput;
51 
52 class NVRWriteThread : public MThread
53 {
54  public:
55  explicit NVRWriteThread(NuppelVideoRecorder *parent) :
56  MThread("NVRWrite"), m_parent(parent) {}
57  virtual ~NVRWriteThread() { wait(); m_parent = nullptr; }
58  void run(void) override; // MThread
59  private:
61 };
62 
63 class NVRAudioThread : public MThread
64 {
65  public:
66  explicit NVRAudioThread(NuppelVideoRecorder *parent) :
67  MThread("NVRAudio"), m_parent(parent) {}
68  virtual ~NVRAudioThread() { wait(); m_parent = nullptr; }
69  void run(void) override; // MThread
70  private:
72 };
73 
75 {
76  friend class NVRWriteThread;
77  friend class NVRAudioThread;
78  public:
79  NuppelVideoRecorder(TVRec *rec, ChannelBase *channel);
81 
82  void SetOption(const QString &opt, int value) override; // DTVRecorder
83  void SetOption(const QString &name, const QString &value) override; // DTVRecorder
84 
85  void SetOptionsFromProfile(RecordingProfile *profile,
86  const QString &videodev,
87  const QString &audiodev,
88  const QString &vbidev) override; // DTVRecorder
89 
90  void Initialize(void) override; // DTVRecorder
91  void run(void) override; // RecorderBase
92 
93  void Pause(bool clear = true) override; // RecorderBase
94  bool IsPaused(bool holding_lock = false) const override; // RecorderBase
95 
96  bool IsRecording(void) override; // RecorderBase
97 
98  long long GetFramesWritten(void) override; // DTVRecorder
99 
100  bool Open(void);
101  int GetVideoFd(void) override; // DTVRecorder
102  void Reset(void) override; // DTVRecorder
103 
104  void SetVideoFilters(QString &filters) override; // DTVRecorder
105  void SetTranscoding(bool value) { m_transcoding = value; };
106 
107  void ResetForNewFile(void) override; // DTVRecorder
108  void FinishRecording(void) override; // DTVRecorder
109  void StartNewFile(void) override; // RecorderBase
110 
111  // reencode stuff
112  void StreamAllocate(void);
113  void WriteHeader(void);
114  void WriteSeekTable(void);
115  void WriteKeyFrameAdjustTable(
116  const vector<struct kfatable_entry> &kfa_table);
117  void UpdateSeekTable(int frame_num, long offset = 0);
118 
119  bool SetupAVCodecVideo(void);
120  void SetupRTjpeg(void);
121  int AudioInit(bool skipdevice = false);
122  void SetVideoAspect(float newAspect) {m_video_aspect = newAspect; };
123  void WriteVideo(VideoFrame *frame, bool skipsync = false,
124  bool forcekey = false);
125  void WriteAudio(unsigned char *buf, int fnum, int timecode);
126  void WriteText(unsigned char *buf, int len, int timecode, int pagenr);
127 
128  void SetNewVideoParams(double newaspect);
129 
130  protected:
131  void doWriteThread(void);
132  void doAudioThread(void);
133 
134  private:
135  inline void WriteFrameheader(rtframeheader *fh);
136 
137  void WriteFileHeader(void);
138 
139  void InitBuffers(void);
140  void InitFilters(void);
141  void ResizeVideoBuffers(void);
142 
143  bool MJPEGInit(void);
144 
145  void KillChildren(void);
146 
147  void BufferIt(unsigned char *buf, int len = -1, bool forcekey = false);
148 
149  int CreateNuppelFile(void);
150 
151  void ProbeV4L2(void);
152  bool SetFormatV4L2(void);
153  void DoV4L1(void);
154  void DoV4L2(void);
155  void DoMJPEG(void);
156 
157  void FormatTT(struct VBIData*) override; // V4LRecorder
158  void FormatCC(uint code1, uint code2) override; // V4LRecorder
159  void AddTextData(unsigned char*,int,int64_t,char) override; // CC608Input
160 
161  void UpdateResolutions(void);
162 
163  int m_fd {-1}; // v4l input file handle
164  signed char *m_strm {nullptr};
165  unsigned int m_lf {0};
166  int m_tf {0};
167  int m_M1 {0};
168  int m_M2 {0};
169  int m_Q {255};
170  int m_width {352};
171  int m_height {240};
172  int m_pip_mode {0};
173  int m_compression {1};
174  bool m_compressaudio {true};
175  AudioInput *m_audio_device {nullptr};
176  unsigned long long m_audiobytes {0};
177  int m_audio_channels {2};
178  int m_audio_bits {16};
179  int m_audio_bytes_per_sample {m_audio_channels * m_audio_bits / 8};
180  int m_audio_samplerate {44100}; // rate we request from sounddevice
181  int m_effectivedsp {0}; // actual measured rate
182 
183  int m_usebttv {1};
184  float m_video_aspect {1.33333F};
185 
186  bool m_transcoding {false};
187 
188  int m_mp3quality {3};
189  char *m_mp3buf {nullptr};
190  int m_mp3buf_size {0};
191  lame_global_flags *m_gf {nullptr};
192 
193  RTjpeg *m_rtjc {nullptr};
194 
195 #define OUT_LEN (1024*1024 + 1024*1024 / 64 + 16 + 3)
196  lzo_byte m_out[OUT_LEN];
197 #define HEAP_ALLOC(var,size) \
198  long __LZO_MMODEL var [ ((size) + (sizeof(long) - 1)) / sizeof(long) ]
199  HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);
200 
201  vector<struct vidbuffertype *> videobuffer;
202  vector<struct audbuffertype *> audiobuffer;
203  vector<struct txtbuffertype *> textbuffer;
204 
205  int m_act_video_encode {0};
206  int m_act_video_buffer {0};
207 
208  int m_act_audio_encode {0};
209  int m_act_audio_buffer {0};
210  long long m_act_audio_sample {0};
211 
212  int m_act_text_encode {0};
213  int m_act_text_buffer {0};
214 
215  int m_video_buffer_count {0};
216  int m_audio_buffer_count {0};
217  int m_text_buffer_count {0};
218 
219  long m_video_buffer_size {0};
220  long m_audio_buffer_size {0};
221  long m_text_buffer_size {0};
222 
223  struct timeval m_stm;
224  struct timezone m_tzone;
225 
226  NVRWriteThread *m_write_thread {nullptr};
227  NVRAudioThread *m_audio_thread {nullptr};
228 
229  bool m_recording {false};
230 
231  int m_keyframedist {KEYFRAMEDIST};
232  vector<struct seektable_entry> *m_seektable {nullptr};
233  long long m_lastPositionMapPos {0};
234 
235  long long m_extendeddataOffset {0};
236 
237  long long m_framesWritten {0};
238 
239  bool m_livetv {false};
240  bool m_writepaused {false};
241  bool m_audiopaused {false};
242  bool m_mainpaused {false};
243 
244  double m_framerate_multiplier {1.0};
245  double m_height_multiplier {1.0};
246 
247  int m_last_block {0};
248  int m_firsttc {0};
249  long int m_oldtc {0};
250  int m_startnum {0};
251  int m_frameofgop {0};
252  int m_lasttimecode {0};
253  int m_audio_behind {0};
254 
255  bool m_useavcodec {false};
256 
257  AVCodec *m_mpa_vidcodec {nullptr};
258  AVCodecContext *m_mpa_vidctx {nullptr};
259 
260  int m_targetbitrate {2200};
261  int m_scalebitrate {1};
262  int m_maxquality {2};
263  int m_minquality {31};
264  int m_qualdiff {3};
265  int m_mp4opts {0};
266  int m_mb_decision {FF_MB_DECISION_SIMPLE};
268  int m_encoding_thread_count {1};
269 
271  FilterChain *m_videoFilters {nullptr};
272  FilterManager *m_filtMan {nullptr};
273 
274  VideoFrameType m_inpixfmt {FMT_YV12};
275  AVPixelFormat m_picture_format {AV_PIX_FMT_YUV420P};
276  uint32_t m_v4l2_pixelformat {0};
277  int m_w_out {0};
278  int m_h_out {0};
279 
280  bool m_hardware_encode {false};
281  int m_hmjpg_quality {80};
282  int m_hmjpg_hdecimation {2};
283  int m_hmjpg_vdecimation {2};
284  int m_hmjpg_maxw {640};
285 
286  bool m_cleartimeonpause {false};
287 
288  bool m_usingv4l2 {false};
289  int m_channelfd {-1};
290 
291  ChannelBase *m_channelObj {nullptr};
292 
293  bool m_skip_btaudio {false};
294 
295  bool m_correct_bttv {false};
296 
297  int m_volume {100};
298 
299  CC608Decoder *m_ccd {nullptr};
300 
301  bool m_go7007 {false};
302  bool m_resetcapture {false};
303 };
304 
305 #endif
NuppelVideoRecorder * m_parent
void SetTranscoding(bool value)
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
vector< struct audbuffertype * > audiobuffer
vector< struct vidbuffertype * > videobuffer
#define OUT_LEN
unsigned int uint
Definition: compat.h:140
enum FrameType_ VideoFrameType
NVRAudioThread(NuppelVideoRecorder *parent)
void SetVideoAspect(float newAspect)
#define HEAP_ALLOC(var, size)
static int run(MythMediaDevice *dev=nullptr, bool startRandomShow=false)
vector< struct txtbuffertype * > textbuffer
Abstract base class for Video4Linux based recorders.
Definition: v4lrecorder.h:24
virtual ~NVRWriteThread()
#define MTV_PUBLIC
Definition: mythtvexp.h:15
This is the coordinating class of the Recorder Subsystem.
Definition: tv_rec.h:150
static void clear(SettingsMap &cache, SettingsMap &overrides, const QString &myKey)
Definition: mythdb.cpp:830
const char * name
Definition: ParseText.cpp:328
Definition: RTjpegN.h:64
#define KEYFRAMEDIST
virtual ~NVRAudioThread()
Abstract class providing a generic interface to tuning hardware.
Definition: channelbase.h:31
Implements a file/stream reader/writer.
NVRWriteThread(NuppelVideoRecorder *parent)
NuppelVideoRecorder * m_parent