[mmap] partial revert of 8cef8db4 to disable using mmap file reader
[videoplayer.git] / yuvReader.cpp
blob3476c898b81b0e27399e6e397c324b18f96e0fd0
1 /* ***** BEGIN LICENSE BLOCK *****
3 * The MIT License
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
23 * THE SOFTWARE.
25 * ***** END LICENSE BLOCK ***** */
27 #include <QtGui>
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <sstream>
35 #ifdef Q_OS_UNIX
36 #include <unistd.h>
37 #endif
39 #ifdef Q_OS_WIN32
40 #include <io.h>
41 #define lseek64 _lseeki64
42 #define read _read
43 #define open _open
44 #define off64_t qint64
45 #endif
47 #ifdef Q_OS_MAC
48 #define lseek64 lseek
49 #define off64_t off_t
50 #endif
52 #include "yuvReader.h"
53 #include "stats.h"
55 #define DEBUG 0
57 YUVReader::YUVReader(FrameQueue& frameQ) :
58 ReaderInterface(frameQ)
60 randomAccess = true;
63 void YUVReader::setForceFileType(bool f)
65 forceFileType = f;
68 void YUVReader::setFileType(const QString &t)
70 fileType = t;
73 void YUVReader::setVideoWidth(int w)
75 videoWidth = w;
78 void YUVReader::setVideoHeight(int h)
80 videoHeight = h;
83 void YUVReader::setFileName(const QString &fn)
85 fileName = fn;
86 QFileInfo info(fileName);
88 if (DEBUG)
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());
94 perror("OPEN");
97 QString type = forceFileType ? fileType.toLower() : info.suffix().toLower();
98 printf("Playing file with type %s\n", type.toLatin1().data());
100 firstFrameNum = 0;
102 if (type == "16p0") {
103 videoFormat = VideoData::V16P0;
104 lastFrameNum = info.size() / ((videoWidth * videoHeight * 3 * 2) / 2);
105 lastFrameNum--;
108 if (type == "16p2") {
109 videoFormat = VideoData::V16P2;
110 lastFrameNum = info.size() / (videoWidth * videoHeight * 2 * 2);
111 lastFrameNum--;
114 if (type == "16p4") {
115 videoFormat = VideoData::V16P4;
116 lastFrameNum = info.size() / (videoWidth * videoHeight * 3 * 2);
117 lastFrameNum--;
120 if (type == "420p") {
121 videoFormat = VideoData::V8P0;
122 lastFrameNum = info.size() / ((videoWidth * videoHeight * 3) / 2);
123 lastFrameNum--;
126 if (type == "422p") {
127 videoFormat = VideoData::V8P2;
128 lastFrameNum = info.size() / (videoWidth * videoHeight * 2);
129 lastFrameNum--;
132 if (type == "444p") {
133 videoFormat = VideoData::V8P4;
134 lastFrameNum = info.size() / (videoWidth * videoHeight * 3);
135 lastFrameNum--;
138 if (type == "i420") {
139 videoFormat = VideoData::V8P0;
140 lastFrameNum = info.size() / ((videoWidth * videoHeight * 3) / 2);
141 lastFrameNum--;
144 if (type == "yv12") {
145 videoFormat = VideoData::YV12;
146 lastFrameNum = info.size() / ((videoWidth * videoHeight * 3) / 2);
147 lastFrameNum--;
150 if (type == "uyvy") {
151 videoFormat = VideoData::UYVY;
152 lastFrameNum = info.size() / (videoWidth * videoHeight * 2);
153 lastFrameNum--;
156 if (type == "v216") {
157 videoFormat = VideoData::V216;
158 lastFrameNum = info.size() / (videoWidth * videoHeight * 4);
159 lastFrameNum--;
162 if (type == "v210") {
163 videoFormat = VideoData::V210;
164 lastFrameNum = info.size() / ((videoWidth * videoHeight * 2 * 4) / 3);
165 lastFrameNum--;
169 Stats &stat = Stats::getInstance();
170 std::stringstream ss;
172 ss.str("");
173 ss << videoWidth;
174 stat.addStat("YUVReader", "VideoWidth", ss.str());
176 ss.str("");
177 ss << videoHeight;
178 stat.addStat("YUVReader", "VideoHeight", ss.str());
180 ss.str("");
181 ss << firstFrameNum;
182 stat.addStat("YUVReader", "FirstFrame", ss.str());
184 ss.str("");
185 ss << lastFrameNum;
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);
198 if (DEBUG)
199 printf("Getting frame number %d\n", frameNumber);
201 //deal with frame number wrapping
202 if (frameNumber < 0) {
203 frameNumber *= -1;
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);
215 dst = frame;
217 //seek
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)
221 perror("LSEEK");
223 QTime timer;
224 timer.restart();
226 int rret = read(fd, dst->data, dst->dataSize);
227 if (!rret)
228 perror("READ");
230 int readtime = timer.elapsed();
233 Stats &stat = Stats::getInstance();
234 std::stringstream ss;
236 ss.str("");
237 ss << readtime << "ms";
238 stat.addStat("YUVReader", "Read", ss.str());