1 /* ***** BEGIN LICENSE BLOCK *****
5 * Copyright (c) 2008 BBC Research
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * ***** END LICENSE BLOCK ***** */
30 #include <sys/types.h>
41 #define lseek64 _lseeki64
44 #define off64_t qint64
52 #include "yuvReader.h"
57 YUVReader::YUVReader(FrameQueue
& frameQ
) :
58 ReaderInterface(frameQ
)
63 void YUVReader::setForceFileType(bool f
)
68 void YUVReader::setFileType(const QString
&t
)
73 void YUVReader::setVideoWidth(int w
)
78 void YUVReader::setVideoHeight(int h
)
83 void YUVReader::setFileName(const QString
&fn
)
86 QFileInfo
info(fileName
);
89 printf("Opening file %s\n", fileName
.toLatin1().data());
90 fd
= open(fileName
.toLatin1().data(), O_RDONLY
);
92 if (fd
< 0 && DEBUG
== 1) {
93 printf("[%s]\n", fileName
.toLatin1().data());
97 QString type
= forceFileType
? fileType
.toLower() : info
.suffix().toLower();
98 printf("Playing file with type %s\n", type
.toLatin1().data());
102 if (type
== "16p0") {
103 videoFormat
= VideoData::V16P0
;
104 lastFrameNum
= info
.size() / ((videoWidth
* videoHeight
* 3 * 2) / 2);
108 if (type
== "16p2") {
109 videoFormat
= VideoData::V16P2
;
110 lastFrameNum
= info
.size() / (videoWidth
* videoHeight
* 2 * 2);
114 if (type
== "16p4") {
115 videoFormat
= VideoData::V16P4
;
116 lastFrameNum
= info
.size() / (videoWidth
* videoHeight
* 3 * 2);
120 if (type
== "420p") {
121 videoFormat
= VideoData::V8P0
;
122 lastFrameNum
= info
.size() / ((videoWidth
* videoHeight
* 3) / 2);
126 if (type
== "422p") {
127 videoFormat
= VideoData::V8P2
;
128 lastFrameNum
= info
.size() / (videoWidth
* videoHeight
* 2);
132 if (type
== "444p") {
133 videoFormat
= VideoData::V8P4
;
134 lastFrameNum
= info
.size() / (videoWidth
* videoHeight
* 3);
138 if (type
== "i420") {
139 videoFormat
= VideoData::V8P0
;
140 lastFrameNum
= info
.size() / ((videoWidth
* videoHeight
* 3) / 2);
144 if (type
== "yv12") {
145 videoFormat
= VideoData::YV12
;
146 lastFrameNum
= info
.size() / ((videoWidth
* videoHeight
* 3) / 2);
150 if (type
== "uyvy") {
151 videoFormat
= VideoData::UYVY
;
152 lastFrameNum
= info
.size() / (videoWidth
* videoHeight
* 2);
156 if (type
== "v216") {
157 videoFormat
= VideoData::V216
;
158 lastFrameNum
= info
.size() / (videoWidth
* videoHeight
* 4);
162 if (type
== "v210") {
163 videoFormat
= VideoData::V210
;
164 lastFrameNum
= info
.size() / ((videoWidth
* videoHeight
* 2 * 4) / 3);
169 Stats
&stat
= Stats::getInstance();
170 std::stringstream ss
;
174 stat
.addStat("YUVReader", "VideoWidth", ss
.str());
178 stat
.addStat("YUVReader", "VideoHeight", ss
.str());
182 stat
.addStat("YUVReader", "FirstFrame", ss
.str());
186 stat
.addStat("YUVReader", "LastFrame", ss
.str());
188 stat
.addStat("YUVReader", "VideoFormat", type
.toLatin1().data());
192 //called from the frame queue controller to get frame data for display
193 void YUVReader::pullFrame(int frameNumber
, VideoData
*& dst
)
195 VideoData
* frame
= frameQueue
.allocateFrame();
196 frame
->resize(videoWidth
, videoHeight
, videoFormat
);
199 printf("Getting frame number %d\n", frameNumber
);
201 //deal with frame number wrapping
202 if (frameNumber
< 0) {
204 frameNumber
%= (lastFrameNum
+ 1);
205 frameNumber
= lastFrameNum
- frameNumber
;
208 if (frameNumber
> lastFrameNum
)
209 frameNumber
%= (lastFrameNum
+ 1);
211 //set frame number and first/last flags
212 frame
->frameNum
= frameNumber
;
213 frame
->isFirstFrame
= (frameNumber
== firstFrameNum
);
214 frame
->isLastFrame
= (frameNumber
== lastFrameNum
);
218 off64_t offset
= (off_t
)dst
->dataSize
* (off_t
)frameNumber
; //seek to the wanted frame
219 off64_t sret
= lseek64(fd
, offset
, SEEK_SET
);
220 if (!sret
&& offset
!= 0)
226 int rret
= read(fd
, dst
->data
, dst
->dataSize
);
230 int readtime
= timer
.elapsed();
233 Stats
&stat
= Stats::getInstance();
234 std::stringstream ss
;
237 ss
<< readtime
<< "ms";
238 stat
.addStat("YUVReader", "Read", ss
.str());