2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
36 int VideoMVP::init(UCHAR tformat)
38 if (initted) return 0;
41 if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
43 if (!setFormat(tformat)) { shutdown(); return 0; }
44 if (!setConnection(COMPOSITERGB)) { shutdown(); return 0; }
45 if (!setAspectRatio(ASPECT4X3,12,11)) { shutdown(); return 0; }
46 if (!setMode(NORMAL)) { shutdown(); return 0; }
47 if (!setSource()) { shutdown(); return 0; }
48 if (!attachFrameBuffer()) { shutdown(); return 0; }
52 if (format == PAL) setLetterboxBorder("38");
53 else setLetterboxBorder("31");
61 void VideoMVP::setLetterboxBorder(char* border)
63 FILE* fdlbox = fopen("/proc/lbox_border", "w");
65 fputs(border, fdlbox);
69 int VideoMVP::setTVsize(UCHAR ttvsize)
73 // Override the aspect ratio usage, temporarily use to set the video chip mode
74 if (!setAspectRatio(tvsize,parx,pary)) { shutdown(); return 0; }
76 if ((fdVideo = open("/dev/vdec_dev", O_WRONLY)) < 0) return 0;
77 if (!setSource()) { shutdown(); return 0; }
78 if (!attachFrameBuffer()) { shutdown(); return 0; }
80 // Reopening the fd causes the scart aspect line to go back to 4:3
81 // Set this again to the same as the tv screen size
82 if (!setAspectRatio(tvsize,parx,pary)) { shutdown(); return 0; }
84 // mode == LETTERBOX is invalid if the TV is widescreen
85 if (tvsize == ASPECT16X9) setMode(NORMAL);
90 int VideoMVP::setDefaultAspect()
92 return setAspectRatio(tvsize,parx,pary);
95 int VideoMVP::shutdown()
97 if (!initted) return 0;
103 int VideoMVP::checkSCART()
105 // Returns 3 for SCART Composite out
106 // Returns 3 for SCART S-Video out
107 // Returns 2 for SCART RGB out
108 // Returns 3 for SCART not plugged in
110 // So, as you can have RGB and composite out simultaneously,
111 // and it can't detect S-Video, what is the point of this?
114 if (ioctl(fdVideo, AV_CHK_SCART, &scart) != 0) return -10;
119 int VideoMVP::setFormat(UCHAR tformat)
121 if (!initted) return 0;
122 if ((tformat != PAL) && (tformat != NTSC)) return 0;
125 if (ioctl(fdVideo, AV_SET_VID_DISP_FMT, format) != 0) return 0;
141 int VideoMVP::setConnection(UCHAR tconnection)
143 if (!initted) return 0;
144 if ((tconnection != COMPOSITERGB) && (tconnection != SVIDEO)) return 0;
145 connection = tconnection;
147 if (ioctl(fdVideo, AV_SET_VID_OUTPUT, connection) != 0) return 0;
151 int VideoMVP::setAspectRatio(UCHAR taspectRatio, int tparx,int tpary)
153 if (!initted) return 0;
156 if ((taspectRatio != ASPECT4X3) && (taspectRatio != ASPECT16X9)) return 0;
157 aspectRatio = taspectRatio;
159 Log::getInstance()->log("Video", Log::DEBUG, "Setting aspect to %i", aspectRatio);
161 if (ioctl(fdVideo, AV_SET_VID_RATIO, aspectRatio) != 0) return 0;
165 int VideoMVP::setMode(UCHAR tmode)
167 if (!initted) return 0;
169 if ((tmode == LETTERBOX) && (tvsize == ASPECT16X9)) return 0; // invalid mode
171 if ((tmode != NORMAL) && (tmode != LETTERBOX) && (tmode != UNKNOWN2) && (tmode != QUARTER) && (tmode != EIGHTH)
172 && (tmode != ZOOM) && (tmode != UNKNOWN6)) return 0;
173 if (tmode==NORMAL || tmode == LETTERBOX || tmode == QUARTER) mode = tmode;
174 if (ioctl(fdVideo, AV_SET_VID_MODE, mode) != 0) return 0;
178 int VideoMVP::signalOff()
180 if (ioctl(fdVideo, AV_SET_VID_DENC, 0) != 0) return 0;
184 int VideoMVP::signalOn()
186 if (ioctl(fdVideo, AV_SET_VID_DENC, 1) != 0) return 0;
190 int VideoMVP::setSource()
192 if (!initted) return 0;
194 // What does this do...
195 if (ioctl(fdVideo, AV_SET_VID_SRC, 1) != 0) return 0;
199 int VideoMVP::setPosition(int x, int y)
201 if (!initted) return 0;
203 // vid_pos_regs_t pos_d;
207 vid_pos_regs_t pos_d;
209 memset(&pos_d, 0, sizeof(pos_d));
234 pos_d.y = 100; // Top left X
235 pos_d.x = 50; // Top left Y
243 if (ioctl(fdVideo, AV_SET_VID_POSITION, &pos_d) != 0) return 0;
249 if (!initted) return 0;
251 if (ioctl(fdVideo, AV_SET_VID_SYNC, 2) != 0) return 0;
257 if (!initted) return 0;
259 if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
265 if (!initted) return 0;
267 if (ioctl(fdVideo, AV_SET_VID_STOP, 0) != 0) return 0;
271 int VideoMVP::reset()
273 if (!initted) return 0;
275 if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0;
279 int VideoMVP::pause()
281 if (!initted) return 0;
283 if (ioctl(fdVideo, AV_SET_VID_PAUSE, 0) != 0) return 0;
287 int VideoMVP::unPause() // FIXME get rid - same as play!!
289 if (!initted) return 0;
290 if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
294 int VideoMVP::fastForward()
296 if (!initted) return 0;
298 if (ioctl(fdVideo, AV_SET_VID_FFWD, 1) != 0) return 0;
302 int VideoMVP::unFastForward()
304 if (!initted) return 0;
306 // if (ioctl(fdVideo, AV_SET_VID_RESET, 0x11) != 0) return 0; // don't need this.
308 if (ioctl(fdVideo, AV_SET_VID_PLAY, 0) != 0) return 0;
312 int VideoMVP::attachFrameBuffer()
314 if (!initted) return 0;
316 if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
320 int VideoMVP::blank(void)
322 if (ioctl(fdVideo, AV_SET_VID_FB, 1) != 0) return 0;
323 if (ioctl(fdVideo, AV_SET_VID_FB, 0) != 0) return 0;
327 ULLONG VideoMVP::getCurrentTimestamp()
329 sync_data_t timestamps;
330 if (ioctl(fdVideo, AV_GET_VID_TIMESTAMPS, ×tamps) == 0)
332 // FIXME are these the right way around?
334 timestamps.stc = (timestamps.stc >> 31 ) | (timestamps.stc & 1);
335 timestamps.pts = (timestamps.pts >> 31 ) | (timestamps.pts & 1);
337 return timestamps.stc;
345 ULONG VideoMVP::timecodeToFrameNumber(ULLONG timecode)
347 if (format == PAL) return (ULONG)(((double)timecode / (double)90000) * (double)25);
348 else return (ULONG)(((double)timecode / (double)90000) * (double)30);
357 // return ioctl(fdVideo, AV_SET_VID_STC, &stc);
364 int VideoMVP::test2()
370 void VideoMVP::PrepareMediaSample(const MediaPacketList& mplist,UINT samplepos)
372 MediaPacketList::const_iterator iter = mplist.begin();
373 deliver_start = iter->pos_buffer + samplepos;
374 mediapacket_len[0] = deliver_length = iter->length;
376 while (++iter != mplist.end() &&
377 iter->pos_buffer == deliver_start + deliver_length)
379 deliver_length += iter->length;
380 mediapacket_len[deliver_count] = iter->length;
382 if (deliver_length >= WRITE_LENGTH ||
383 deliver_count == WRITE_PACKETS) break;
387 UINT VideoMVP::DeliverMediaSample(UCHAR* buffer, UINT* samplepos)
389 int written = ::write(fdVideo, buffer + deliver_start, deliver_length);
390 if (written == (int)deliver_length) { *samplepos = 0; return deliver_count;}
391 if (written <= 0) return 0;
392 // Handle a partial write. Is this possible? Should we bother?
394 while ((written -= mediapacket_len[i]) >= 0) i++;
395 *samplepos = mediapacket_len[i] + written;
399 void VideoMVP::ResetTimeOffsets()
403 bool VideoMVP::displayIFrame(const UCHAR* buffer, UINT length)
405 write(fdVideo, buffer, length);