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 "captions/cc608decoder.h"
35 #include "lzo/lzo1x.h"
36 #include "mthread.h"
37 #include "mythframe.h"
38 
39 #include "mythtvexp.h"
40 
41 #define KEYFRAMEDIST 30
42 
43 struct video_audio;
44 class RTjpeg;
45 class MythMediaBuffer;
46 class ChannelBase;
47 class AudioInput;
49 
50 class NVRWriteThread : public MThread
51 {
52  public:
53  explicit NVRWriteThread(NuppelVideoRecorder *parent) :
54  MThread("NVRWrite"), m_parent(parent) {}
55  ~NVRWriteThread() override { wait(); m_parent = nullptr; }
56  void run(void) override; // MThread
57  private:
59 };
60 
61 class NVRAudioThread : public MThread
62 {
63  public:
64  explicit NVRAudioThread(NuppelVideoRecorder *parent) :
65  MThread("NVRAudio"), m_parent(parent) {}
66  ~NVRAudioThread() override { wait(); m_parent = nullptr; }
67  void run(void) override; // MThread
68  private:
70 };
71 
73 {
74  friend class NVRWriteThread;
75  friend class NVRAudioThread;
76  public:
78  ~NuppelVideoRecorder() override;
79 
80  void SetOption(const QString &opt, int value) override; // DTVRecorder
81  void SetOption(const QString &name, const QString &value) override; // DTVRecorder
82 
83  void SetOptionsFromProfile(RecordingProfile *profile,
84  const QString &videodev,
85  const QString &audiodev,
86  const QString &vbidev) override; // DTVRecorder
87 
88  void Initialize(void) override; // DTVRecorder
89  void run(void) override; // RecorderBase
90 
91  void Pause(bool clear = true) override; // RecorderBase
92  bool IsPaused(bool holding_lock = false) const override; // RecorderBase
93 
94  bool IsRecording(void) override; // RecorderBase
95 
96  long long GetFramesWritten(void) override; // DTVRecorder
97 
98  bool Open(void);
99  int GetVideoFd(void) override; // DTVRecorder
100  void Reset(void) override; // DTVRecorder
101 
102  void SetVideoFilters(QString &filters) override; // DTVRecorder
103  void SetTranscoding(bool value) { m_transcoding = value; };
104 
105  void ResetForNewFile(void) override; // DTVRecorder
106  void FinishRecording(void) override; // DTVRecorder
107  void StartNewFile(void) override; // RecorderBase
108 
109  // reencode stuff
110  void StreamAllocate(void);
111  void WriteHeader(void);
112  void WriteSeekTable(void);
113  void WriteKeyFrameAdjustTable(
114  const vector<struct kfatable_entry> &kfa_table);
115  void UpdateSeekTable(int frame_num, long offset = 0);
116 
117  bool SetupAVCodecVideo(void);
118  void SetupRTjpeg(void);
119  int AudioInit(bool skipdevice = false);
120  void SetVideoAspect(float newAspect) {m_videoAspect = newAspect; };
121  void WriteVideo(VideoFrame *frame, bool skipsync = false,
122  bool forcekey = false);
123  void WriteAudio(unsigned char *buf, int fnum, int timecode);
124  void WriteText(unsigned char *buf, int len, int timecode, int pagenr);
125 
126  void SetNewVideoParams(double newaspect);
127 
128  protected:
129  void doWriteThread(void);
130  void doAudioThread(void);
131 
132  private:
133  inline void WriteFrameheader(rtframeheader *fh);
134 
135  void WriteFileHeader(void);
136 
137  void InitBuffers(void);
138  void ResizeVideoBuffers(void);
139 
140  bool MJPEGInit(void);
141 
142  void KillChildren(void);
143 
144  void BufferIt(unsigned char *buf, int len = -1, bool forcekey = false);
145 
146  int CreateNuppelFile(void);
147 
148  void ProbeV4L2(void);
149  bool SetFormatV4L2(void);
150  void DoV4L1(void);
151  void DoV4L2(void);
152  void DoMJPEG(void);
153 
154  void FormatTT(struct VBIData *vbidata) override; // V4LRecorder
155  void FormatCC(uint code1, uint code2) override; // V4LRecorder
156  void AddTextData(unsigned char*buf, int len, int64_t timecode, char type) override; // CC608Input
157 
158  void UpdateResolutions(void);
159 
160  int m_fd {-1}; // v4l input file handle
161  signed char *m_strm {nullptr};
162  unsigned int m_lf {0};
163  int m_tf {0};
164  int m_m1 {0};
165  int m_m2 {0};
166  int m_q {255};
167  int m_width {352};
168  int m_height {240};
169  int m_pipMode {0};
170  int m_compression {1};
171  bool m_compressAudio {true};
172  AudioInput *m_audioDevice {nullptr};
173  unsigned long long m_audioBytes {0};
174  int m_audioChannels {2};
175  int m_audioBits {16};
176  int m_audioBytesPerSample {m_audioChannels * m_audioBits / 8};
177  int m_audioSampleRate {44100}; // rate we request from sounddevice
178  int m_effectiveDsp {0}; // actual measured rate
179 
180  int m_useBttv {1};
181  float m_videoAspect {1.33333F};
182 
183  bool m_transcoding {false};
184 
185  int m_mp3Quality {3};
186  char *m_mp3Buf {nullptr};
187  int m_mp3BufSize {0};
188  lame_global_flags *m_gf {nullptr};
189 
190  RTjpeg *m_rtjc {nullptr};
191 
192 #define OUT_LEN (1024*1024 + 1024*1024 / 64 + 16 + 3)
193  lzo_byte m_out[OUT_LEN] {};
194 #define HEAP_ALLOC(var,size) \
195  long __LZO_MMODEL var [ ((size) + (sizeof(long) - 1)) / sizeof(long) ]
196  HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS) {};
197 
198  vector<struct vidbuffertype *> m_videoBuffer;
199  vector<struct audbuffertype *> m_audioBuffer;
200  vector<struct txtbuffertype *> m_textBuffer;
201 
202  int m_actVideoEncode {0};
203  int m_actVideoBuffer {0};
204 
205  int m_actAudioEncode {0};
206  int m_actAudioBuffer {0};
207  long long m_actAudioSample {0};
208 
209  int m_actTextEncode {0};
210  int m_actTextBuffer {0};
211 
212  int m_videoBufferCount {0};
213  int m_audioBufferCount {0};
214  int m_textBufferCount {0};
215 
216  long m_videoBufferSize {0};
217  long m_audioBufferSize {0};
218  long m_textBufferSize {0};
219 
220  struct timeval m_stm {0,0};
221  struct timezone m_tzone {0,0};
222 
223  NVRWriteThread *m_writeThread {nullptr};
224  NVRAudioThread *m_audioThread {nullptr};
225 
226  bool m_recording {false};
227 
228  int m_keyframeDist {KEYFRAMEDIST};
229  vector<struct seektable_entry> *m_seekTable {nullptr};
230  long long m_lastPositionMapPos {0};
231 
232  long long m_extendedDataOffset {0};
233 
234  long long m_framesWritten {0};
235 
236  bool m_livetv {false};
237  bool m_writePaused {false};
238  bool m_audioPaused {false};
239  bool m_mainPaused {false};
240 
241  double m_frameRateMultiplier {1.0};
242  double m_heightMultiplier {1.0};
243 
244  int m_lastBlock {0};
245  int m_firstTc {0};
246  long int m_oldTc {0};
247  int m_startNum {0};
248  int m_frameOfGop {0};
249  int m_lastTimecode {0};
250  int m_audioBehind {0};
251 
252  bool m_useAvCodec {false};
253 
254  AVCodec *m_mpaVidCodec {nullptr};
255  AVCodecContext *m_mpaVidCtx {nullptr};
256 
257  int m_targetBitRate {2200};
258  int m_scaleBitRate {1};
259  int m_maxQuality {2};
260  int m_minQuality {31};
261  int m_qualDiff {3};
262  int m_mp4Opts {0};
263  int m_mbDecision {FF_MB_DECISION_SIMPLE};
265  int m_encodingThreadCount {1};
266 
267  VideoFrameType m_inPixFmt {FMT_YV12};
268  AVPixelFormat m_pictureFormat {AV_PIX_FMT_YUV420P};
269 #ifdef USING_V4L2
270  uint32_t m_v4l2PixelFormat {0};
271 #endif
272  int m_wOut {0};
273  int m_hOut {0};
274 
275  bool m_hardwareEncode {false};
276  int m_hmjpgQuality {80};
277  int m_hmjpgHDecimation {2};
278  int m_hmjpgVDecimation {2};
279  int m_hmjpgMaxW {640};
280 
281  bool m_clearTimeOnPause {false};
282 
283  bool m_usingV4l2 {false};
284  int m_channelFd {-1};
285 
286  ChannelBase *m_channelObj {nullptr};
287 
288  bool m_skipBtAudio {false};
289 
290 #ifdef USING_V4L2
291  bool m_correctBttv {false};
292 #endif
293 
294  int m_volume {100};
295 
296  CC608Decoder *m_ccd {nullptr};
297 
298  bool m_go7007 {false};
299  bool m_resetCapture {false};
300 };
301 
302 #endif
rtframeheader
Definition: format.h:31
channel
QDomElement channel
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:498
NVRWriteThread::NVRWriteThread
NVRWriteThread(NuppelVideoRecorder *parent)
Definition: NuppelVideoRecorder.h:53
NVRWriteThread::~NVRWriteThread
~NVRWriteThread() override
Definition: NuppelVideoRecorder.h:55
V4LRecorder
Abstract base class for Video4Linux based recorders.
Definition: v4lrecorder.h:25
mythtvexp.h
CC608Decoder
Definition: cc608decoder.h:42
OUT_LEN
#define OUT_LEN
Definition: NuppelVideoRecorder.h:192
format.h
NuppelVideoRecorder::HEAP_ALLOC
HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS)
Definition: NuppelVideoRecorder.h:196
MythMediaBuffer
Definition: mythmediabuffer.h:50
mythframe.h
CC608Input
Definition: cc608decoder.h:19
VideoFrame
Definition: mythframe.h:138
NuppelVideoRecorder::m_textBuffer
vector< struct txtbuffertype * > m_textBuffer
Definition: NuppelVideoRecorder.h:200
NVRWriteThread
Definition: NuppelVideoRecorder.h:51
NuppelVideoRecorder::m_audioBuffer
vector< struct audbuffertype * > m_audioBuffer
Definition: NuppelVideoRecorder.h:199
NVRAudioThread::~NVRAudioThread
~NVRAudioThread() override
Definition: NuppelVideoRecorder.h:66
NuppelVideoRecorder::SetVideoAspect
void SetVideoAspect(float newAspect)
Definition: NuppelVideoRecorder.h:120
NVRAudioThread
Definition: NuppelVideoRecorder.h:62
NuppelVideoRecorder::m_videoBuffer
vector< struct vidbuffertype * > m_videoBuffer
Definition: NuppelVideoRecorder.h:196
AudioInput
Definition: audioinput.h:28
cc608decoder.h
NVRAudioThread::m_parent
NuppelVideoRecorder * m_parent
Definition: NuppelVideoRecorder.h:69
ChannelBase
Abstract class providing a generic interface to tuning hardware.
Definition: channelbase.h:32
hardwareprofile.scan.profile
profile
Definition: scan.py:99
FMT_YV12
@ FMT_YV12
Definition: mythframe.h:28
v4lrecorder.h
NVRWriteThread::m_parent
NuppelVideoRecorder * m_parent
Definition: NuppelVideoRecorder.h:58
clear
static void clear(SettingsMap &cache, SettingsMap &overrides, const QString &myKey)
Definition: mythdb.cpp:846
NVRAudioThread::run
void run(void) override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
Definition: NuppelVideoRecorder.cpp:78
uint
unsigned int uint
Definition: compat.h:140
MTV_PUBLIC
#define MTV_PUBLIC
Definition: mythtvexp.h:15
RTjpeg
Definition: RTjpegN.h:65
NVRAudioThread::NVRAudioThread
NVRAudioThread(NuppelVideoRecorder *parent)
Definition: NuppelVideoRecorder.h:64
KEYFRAMEDIST
#define KEYFRAMEDIST
Definition: NuppelVideoRecorder.h:41
VBIData
Definition: v4lrecorder.h:17
NuppelVideoRecorder::m_stm
Definition: NuppelVideoRecorder.h:220
TVRec
This is the coordinating class of the Recorder Subsystem.
Definition: tv_rec.h:143
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
mthread.h
VideoFrameType
VideoFrameType
Definition: mythframe.h:25
RecordingProfile
Definition: recordingprofile.h:40
NuppelVideoRecorder::SetTranscoding
void SetTranscoding(bool value)
Definition: NuppelVideoRecorder.h:103
NuppelVideoRecorder::m_tzone
Definition: NuppelVideoRecorder.h:221
NuppelVideoRecorder
Definition: NuppelVideoRecorder.h:73