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
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): ______________________________________.
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
49 #pragma implementation "pvidfile.h"
56 #include <ptclib/pvidfile.h>
57 #include <ptlib/videoio.h>
60 ///////////////////////////////////////////////////////////////////////////////
62 PVideoFile::PVideoFile()
64 yuvSize
= yuvWidth
= yuvHeight
= 0;
67 PVideoFile::PVideoFile(PINDEX width
,
71 : PFile(mode
, opts
), yuvWidth(width
), yuvHeight(height
)
73 yuvSize
= yuvWidth
* yuvHeight
* 3 / 2;
76 PVideoFile::PVideoFile(PINDEX width
,
78 const PFilePath
& name
,
81 : PFile(name
, mode
, opts
), yuvWidth(width
), yuvHeight(height
)
83 yuvSize
= yuvWidth
* yuvHeight
* 3 / 2;
86 void PVideoFile::SetWidth(PINDEX v
)
89 yuvSize
= yuvWidth
* yuvHeight
* 3 / 2;
92 void PVideoFile::SetHeight(PINDEX 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
)
105 PString
newType(str
.Left(pos
));
107 str
= str
.Mid(pos
+1);
110 if (pos
!= P_MAX_INDEX
)
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
;
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;
149 ///////////////////////////////////////////////////////////////////////////////
151 static PFactory
<PVideoFile
>::Worker
<PYUVFile
> yuvFileFactory("yuv");
152 static PFactory
<PVideoFile
>::Worker
<PYUVFile
> y4mFileFactory("y4m");
160 PYUVFile::PYUVFile(PINDEX width
,
164 : PVideoFile(width
, height
, mode
, opts
)
169 PYUVFile::PYUVFile(PINDEX width
,
171 const PFilePath
& name
,
174 : PVideoFile(width
, height
, name
, mode
, opts
)
179 void PYUVFile::Construct()
185 BOOL
PYUVFile::Open(OpenMode mode
, int opts
)
187 ExtractSizeHint(path
);
189 if (!(PFile::Open(mode
, opts
)))
192 y4mMode
= GetFilePath().GetType() *= ".y4m";
195 PFile::SetPosition(offset
);
200 if ((ch
= PFile::ReadChar()) < 0)
210 BOOL
PYUVFile::Open(const PFilePath
& name
, OpenMode mode
, int opts
)
215 return Open(mode
, opts
);
218 BOOL
PYUVFile::WriteFrame(const void * frame
)
220 return PFile::Write(frame
, yuvSize
);
223 BOOL
PYUVFile::ReadFrame(void * frame
)
228 if ((ch
= PFile::ReadChar()) < 0)
234 if (!PFile::Read(frame
, yuvSize
)) {
235 PTRACE(4, "YUVFILE\tError reading file " << GetErrorText(GetErrorCode(LastReadError
)));
239 if (GetLastReadCount() != yuvSize
)
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
)
259 return PFile::SetPosition(pos
+ offset
, origin
);
262 return PFile::SetPosition(pos
, origin
);
265 return PFile::SetPosition(offset
, origin
);
271 off_t
PYUVFile::GetPosition() const
273 return PFile::GetPosition() - offset
;