Changed output and intermediate directories, tuned up compile parameters for Windows...
[pwlib.git] / src / ptclib / pvidfile.cxx
blob8ae80d97fd8bc656c19affe401595c094d661e0b
1 /*
2 * pvidfile.cxx
4 * Video file implementation
6 * Portable Windows Library
8 * Copyright (C) 2004 Post Increment
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is
23 * Craig Southeren <craigs@postincrement.com>
25 * All Rights Reserved.
27 * Contributor(s): ______________________________________.
29 * $Log$
30 * Revision 1.5 2007/02/06 05:03:52 csoutheren
31 * Add factory for video file types
33 * Revision 1.4 2006/10/31 04:10:40 csoutheren
34 * Make sure PVidFileDev class is loaded, and make it work with OPAL
36 * Revision 1.3 2006/02/24 04:51:26 csoutheren
37 * Fixed problem with using CIF from video files
38 * Added support for video files in y4m format
40 * Revision 1.2 2006/02/20 06:49:45 csoutheren
41 * Added video file and video file input device code
43 * Revision 1.1 2006/02/20 06:17:28 csoutheren
44 * Added ability to read video from a file
48 #ifdef __GNUC__
49 #pragma implementation "pvidfile.h"
50 #endif
52 #include <ptlib.h>
54 #if P_VIDFILE
56 #include <ptclib/pvidfile.h>
57 #include <ptlib/videoio.h>
60 ///////////////////////////////////////////////////////////////////////////////
62 PVideoFile::PVideoFile()
64 yuvSize = yuvWidth = yuvHeight = 0;
67 PVideoFile::PVideoFile(PINDEX width,
68 PINDEX height,
69 OpenMode mode,
70 int opts)
71 : PFile(mode, opts), yuvWidth(width), yuvHeight(height)
73 yuvSize = yuvWidth * yuvHeight * 3 / 2;
76 PVideoFile::PVideoFile(PINDEX width,
77 PINDEX height,
78 const PFilePath & name,
79 OpenMode mode,
80 int opts)
81 : PFile(name, mode, opts), yuvWidth(width), yuvHeight(height)
83 yuvSize = yuvWidth * yuvHeight * 3 / 2;
86 void PVideoFile::SetWidth(PINDEX v)
88 yuvWidth = v;
89 yuvSize = yuvWidth * yuvHeight * 3 / 2;
92 void PVideoFile::SetHeight(PINDEX v)
94 yuvHeight = v;
95 yuvSize = yuvWidth * yuvHeight * 3 / 2;
98 BOOL PVideoFile::ExtractSizeHint(PFilePath & fn, PINDEX & width, PINDEX & height)
100 PString str(fn.GetType());
101 PINDEX pos = str.Find('{');
102 if (pos == P_MAX_INDEX)
103 return TRUE;
105 PString newType(str.Left(pos));
106 fn.SetType(newType);
107 str = str.Mid(pos+1);
109 pos = str.Find('}');
110 if (pos != P_MAX_INDEX)
111 str = str.Left(pos);
113 BOOL ret = TRUE;
115 if (str *= "cif16") {
116 width = PVideoDevice::CIF16Width;
117 height = PVideoDevice::CIF16Height;
119 else if (str *= "cif4") {
120 width = PVideoDevice::CIF4Width;
121 height = PVideoDevice::CIF4Height;
123 else if (str *= "cif") {
124 width = PVideoDevice::CIFWidth;
125 height = PVideoDevice::CIFHeight;
127 else if (str *= "qcif") {
128 width = PVideoDevice::QCIFWidth;
129 height = PVideoDevice::QCIFHeight;
131 else if (str *= "sqcif") {
132 width = PVideoDevice::SQCIFWidth;
133 height = PVideoDevice::SQCIFHeight;
135 else
136 ret = FALSE;
138 return ret;
141 BOOL PVideoFile::ExtractSizeHint(PFilePath & fn)
143 BOOL stat = ExtractSizeHint(fn, yuvWidth, yuvHeight);
144 if (stat && (yuvHeight != 0) && (yuvWidth != 0))
145 yuvSize = yuvWidth * yuvHeight * 3 / 2;
146 return stat;
149 ///////////////////////////////////////////////////////////////////////////////
151 static PFactory<PVideoFile>::Worker<PYUVFile> yuvFileFactory("yuv");
152 static PFactory<PVideoFile>::Worker<PYUVFile> y4mFileFactory("y4m");
154 PYUVFile::PYUVFile()
155 : PVideoFile()
157 Construct();
160 PYUVFile::PYUVFile(PINDEX width,
161 PINDEX height,
162 OpenMode mode,
163 int opts)
164 : PVideoFile(width, height, mode, opts)
166 Construct();
169 PYUVFile::PYUVFile(PINDEX width,
170 PINDEX height,
171 const PFilePath & name,
172 OpenMode mode,
173 int opts)
174 : PVideoFile(width, height, name, mode, opts)
176 Construct();
179 void PYUVFile::Construct()
181 offset = 0;
182 y4mMode = FALSE;
185 BOOL PYUVFile::Open(OpenMode mode, int opts)
187 ExtractSizeHint(path);
189 if (!(PFile::Open(mode, opts)))
190 return FALSE;
192 y4mMode = GetFilePath().GetType() *= ".y4m";
194 if (offset != 0)
195 PFile::SetPosition(offset);
197 if (y4mMode) {
198 int ch;
199 do {
200 if ((ch = PFile::ReadChar()) < 0)
201 return FALSE;
203 while (ch != 0x0a);
206 return TRUE;
210 BOOL PYUVFile::Open(const PFilePath & name, OpenMode mode, int opts)
212 if (IsOpen())
213 Close();
214 SetFilePath(name);
215 return Open(mode, opts);
218 BOOL PYUVFile::WriteFrame(const void * frame)
220 return PFile::Write(frame, yuvSize);
223 BOOL PYUVFile::ReadFrame(void * frame)
225 if (y4mMode) {
226 int ch;
227 do {
228 if ((ch = PFile::ReadChar()) < 0)
229 return FALSE;
231 while (ch != 0x0a);
234 if (!PFile::Read(frame, yuvSize)) {
235 PTRACE(4, "YUVFILE\tError reading file " << GetErrorText(GetErrorCode(LastReadError)));
236 return FALSE;
239 if (GetLastReadCount() != yuvSize)
240 return FALSE;
242 return TRUE;
245 off_t PYUVFile::GetLength() const
247 return PFile::GetLength() - offset;
250 BOOL PYUVFile::SetLength(off_t len)
252 return PFile::SetLength(len + offset);
255 BOOL PYUVFile::SetPosition(off_t pos, FilePositionOrigin origin)
257 switch (origin) {
258 case PFile::Start:
259 return PFile::SetPosition(pos + offset, origin);
261 case PFile::Current:
262 return PFile::SetPosition(pos, origin);
264 case PFile::End:
265 return PFile::SetPosition(offset, origin);
268 return FALSE;
271 off_t PYUVFile::GetPosition() const
273 return PFile::GetPosition() - offset;
276 #endif // P_VIDFILE