]> git.vomp.tv Git - vompclient.git/blob - video.cc
Zero length recording segfault fixed
[vompclient.git] / video.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "video.h"
22
23 Video* Video::instance = NULL;
24
25 Video::Video()
26 {
27   if (instance) return;
28   instance = this;
29   initted = 0;
30
31   fdVideo = 0;
32
33   format = 0;
34   connection = 0;
35   aspectRatio = 0;
36   mode = 0;
37   tvsize = 0;
38
39   screenWidth = 0;
40   screenHeight = 0;
41 }
42
43 Video::~Video()
44 {
45   instance = NULL;
46 }
47
48 Video* Video::getInstance()
49 {
50   return instance;
51 }
52
53 int Video::init(UCHAR tformat)
54 {
55   if (initted) return 0;
56   initted = 1;
57
58   if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
59
60   if (!setFormat(tformat))           { shutdown(); return 0; }
61   if (!setConnection(COMPOSITERGB))  { shutdown(); return 0; }
62   if (!setAspectRatio(ASPECT4X3))    { shutdown(); return 0; }
63   if (!setMode(NORMAL))              { shutdown(); return 0; }
64   if (!setSource())                  { shutdown(); return 0; }
65   if (!attachFrameBuffer())          { shutdown(); return 0; }
66
67   setTVsize(ASPECT4X3);
68
69   if (format == PAL) setLetterboxBorder("38");
70   else setLetterboxBorder("31");
71
72   stop();
73
74
75   return 1;
76 }
77
78 void Video::setLetterboxBorder(char* border)
79 {
80   FILE* fdlbox = fopen("/proc/lbox_border", "w");
81   if (!fdlbox) return;
82   fputs(border, fdlbox);
83   fclose(fdlbox);
84 }
85
86 int Video::setTVsize(UCHAR ttvsize)
87 {
88   tvsize = ttvsize;
89
90   // Override the aspect ratio usage, temporarily use to set the video chip mode
91   if (!setAspectRatio(tvsize))       { shutdown(); return 0; }
92   close(fdVideo);
93   if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
94   if (!setSource())                  { shutdown(); return 0; }
95   if (!attachFrameBuffer())          { shutdown(); return 0; }
96
97   // Reopening the fd causes the scart aspect line to go back to 4:3
98   // Set this again to the same as the tv screen size
99   if (!setAspectRatio(tvsize))       { shutdown(); return 0; }
100
101   // mode == LETTERBOX is invalid if the TV is widescreen
102   if (tvsize == ASPECT16X9) setMode(NORMAL);
103
104   return 1;
105 }
106
107 int Video::setDefaultAspect()
108 {
109   return setAspectRatio(tvsize);
110 }
111
112 int Video::shutdown()
113 {
114   if (!initted) return 0;
115   initted = 0;
116   close(fdVideo);
117   return 1;
118 }
119
120 int Video::checkSCART()
121 {
122   // Returns 3 for SCART Composite out
123   // Returns 3 for SCART S-Video out
124   // Returns 2 for SCART RGB out
125   // Returns 3 for SCART not plugged in
126
127   // So, as you can have RGB and composite out simultaneously,
128   // and it can't detect S-Video, what is the point of this?
129
130   int scart;
131   if (ioctl(fdVideo, AV_CHK_SCART, &scart) != 0) return -10;
132
133   return scart;
134 }
135
136 int Video::setFormat(UCHAR tformat)
137 {
138   if (!initted) return 0;
139   if ((tformat != PAL) && (tformat != NTSC)) return 0;
140   format = tformat;
141
142   if (ioctl(fdVideo, AV_SET_VID_DISP_FMT, format) != 0) return 0;
143
144   if (format == NTSC)
145   {
146     screenWidth = 720;
147     screenHeight = 480;
148   }
149   if (format == PAL)
150   {
151     screenWidth = 720;
152     screenHeight = 576;
153   }
154
155   return 1;
156 }
157
158 int Video::setConnection(UCHAR tconnection)
159 {
160   if (!initted) return 0;
161   if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
162   connection = tconnection;
163
164   if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
165   return 1;
166 }
167
168 int Video::setAspectRatio(UCHAR taspectRatio)
169 {
170   if (!initted) return 0;
171   if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
172   aspectRatio = taspectRatio;
173
174   if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
175   return 1;
176 }
177
178 int Video::setMode(UCHAR tmode)
179 {
180   if (!initted) return 0;
181
182   if ((tmode == LETTERBOX) && (tvsize == ASPECT16X9)) return 0; // invalid mode
183
184   if ((tmode != NORMAL) && (tmode != LETTERBOX) && (tmode != UNKNOWN2) && (tmode != QUARTER) && (tmode != EIGHTH)
185       && (tmode != ZOOM) && (tmode != UNKNOWN6)) return 0;
186   mode = tmode;
187
188   if (ioctl(fdVideo, AV_SET_VID_MODE, mode) != 0) return 0;
189   return 1;
190 }
191
192 int Video::getMode()
193 {
194   return mode;
195 }
196
197 int Video::signalOff()
198 {
199   if (ioctl(fdVideo, AV_SET_VID_DENC, 0) != 0) return 0;
200   return 1;
201 }
202
203 int Video::signalOn()
204 {
205   if (ioctl(fdVideo, AV_SET_VID_DENC, 1) != 0) return 0;
206   return 1;
207 }
208
209 int Video::setSource()
210 {
211   if (!initted) return 0;
212
213   // What does this do...
214   if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
215   return 1;
216 }
217
218 int Video::setPosition(int x, int y)
219 {
220   if (!initted) return 0;
221
222 //  vid_pos_regs_t pos_d;
223 //  pos_d.x = x;
224 //  pos_d.y = y;
225
226   vid_pos_regs_t pos_d;
227
228   memset(&pos_d, 0, sizeof(pos_d));
229
230   pos_d.dest.y = y;
231   pos_d.dest.x = x;
232 /*
233 typedef struct {
234   int w;
235   int h;
236   int scale;
237   int x1;
238   int y;
239   int x;
240   int y2;
241   int x3;
242   int y3;
243   int x4;
244   int y4;
245 } vid_pos_regs_t;
246 */
247
248 /*
249   pos_d.w = 100;
250   pos_d.h = 30;
251   pos_d.scale = 2;
252   pos_d.x1 = 0;
253   pos_d.y = 100;            // Top left X
254   pos_d.x = 50;            // Top left Y
255   pos_d.y2 = 30;
256   pos_d.x3 = 60;
257   pos_d.y3 = 90;
258   pos_d.x4 = 120;
259   pos_d.y4 = 150;
260 */
261
262   if (ioctl(fdVideo, AV_SET_VID_POSITION, &pos_d) != 0) return 0;
263   return 1;
264 }
265
266 int Video::sync()
267 {
268   if (!initted) return 0;
269
270   if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
271   return 1;
272 }
273
274 int Video::play()
275 {
276   if (!initted) return 0;
277
278   if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
279   return 1;
280 }
281
282 int Video::stop()
283 {
284   if (!initted) return 0;
285
286   if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
287   return 1;
288 }
289
290 int Video::reset()
291 {
292   if (!initted) return 0;
293
294   if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
295   return 1;
296 }
297
298 int Video::pause()
299 {
300   if (!initted) return 0;
301
302   if (ioctl(fdVideo, AV_SET_VID_PAUSE, 0) != 0) return 0;
303   return 1;
304 }
305
306 int Video::unPause() // FIXME get rid - same as play!!
307 {
308   if (!initted) return 0;
309   if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
310   return 1;
311 }
312
313 int Video::fastForward()
314 {
315   if (!initted) return 0;
316
317   if (ioctl(fdVideo, AV_SET_VID_FFWD, 1) != 0) return 0;
318   return 1;
319 }
320
321 int Video::unFastForward()
322 {
323   if (!initted) return 0;
324
325 //  if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
326
327   if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
328   return 1;
329 }
330
331 int Video::attachFrameBuffer()
332 {
333   if (!initted) return 0;
334
335   if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
336   return 1;
337 }
338
339 int Video::blank(void)
340 {
341   if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
342   if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
343   return 1;
344 }
345
346 int Video::getFD()
347 {
348   if (!initted) return 0;
349
350   return fdVideo;
351 }
352
353 UCHAR Video::getFormat()
354 {
355   return format;
356 }
357
358 UINT Video::getScreenWidth()
359 {
360   return screenWidth;
361 }
362
363 UINT Video::getScreenHeight()
364 {
365   return screenHeight;
366 }
367
368 UCHAR Video::getTVsize()
369 {
370   return tvsize;
371 }
372
373 #ifdef DEV
374 int Video::test()
375 {
376   return 0;
377
378 //  ULLONG stc = 0;
379 //  return ioctl(fdVideo, AV_SET_VID_STC, &stc);
380 /*
381  // reset();
382   return 1;
383 */
384 }
385
386 int Video::test2()
387 {
388   return 0;
389 }
390 #endif