BlueGit

ict rusty-teams

Files

rusty-teams / vendor / webrtc-sys / test / video_source.cc

video_source.cc

video_source.cc 9.4 KiB Download Raw
1/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "video_source.h"
12
13#include <stdio.h>
14
15#include "fileutils.h"
16
17#define ASSERT_TRUE(condition) \
18 do { \
19 if (!(condition)) { \
20 fprintf(stderr, "Assertion failed: %s\n", #condition); \
21 abort(); \
22 } \
23 } while (0)
24
25VideoSource::VideoSource()
26:
27_fileName(webrtc::test::ProjectRootPath() + "resources/foreman_cif.yuv"),
28_width(352),
29_height(288),
30_type(webrtc::VideoType::kI420),
31_frameRate(30)
32{
33}
34
35VideoSource::VideoSource(std::string fileName, VideoSize size,
36 int frameRate /*= 30*/, webrtc::VideoType type /*= webrtc::kI420*/)
37:
38_fileName(fileName),
39_type(type),
40_frameRate(frameRate)
41{
42 assert(size != kUndefined && size != kNumberOfVideoSizes);
43 assert(type != webrtc::VideoType::kUnknown);
44 assert(frameRate > 0);
45 if (GetWidthHeight(size, _width, _height) != 0) {
46 assert(false);
47 }
48}
49
50VideoSource::VideoSource(std::string fileName, int width, int height,
51 int frameRate /*= 30*/, webrtc::VideoType type /*= webrtc::kI420*/)
52:
53_fileName(fileName),
54_width(width),
55_height(height),
56_type(type),
57_frameRate(frameRate)
58{
59 assert(width > 0);
60 assert(height > 0);
61 assert(type != webrtc::VideoType::kUnknown);
62 assert(frameRate > 0);
63}
64
65VideoSize
66VideoSource::GetSize() const
67{
68 return GetSize(_width, _height);
69}
70
71VideoSize
72VideoSource::GetSize(uint16_t width, uint16_t height)
73{
74 if(width == 128 && height == 96)
75 {
76 return kSQCIF;
77 }else if(width == 160 && height == 120)
78 {
79 return kQQVGA;
80 }else if(width == 176 && height == 144)
81 {
82 return kQCIF;
83 }else if(width == 320 && height == 240)
84 {
85 return kQVGA;
86 }else if(width == 352 && height == 288)
87 {
88 return kCIF;
89 }else if(width == 640 && height == 480)
90 {
91 return kVGA;
92 }else if(width == 720 && height == 480)
93 {
94 return kNTSC;
95 }else if(width == 704 && height == 576)
96 {
97 return k4CIF;
98 }else if(width == 800 && height == 600)
99 {
100 return kSVGA;
101 }else if(width == 960 && height == 720)
102 {
103 return kHD;
104 }else if(width == 1024 && height == 768)
105 {
106 return kXGA;
107 }else if(width == 1440 && height == 1080)
108 {
109 return kFullHD;
110 }else if(width == 400 && height == 240)
111 {
112 return kWQVGA;
113 }else if(width == 800 && height == 480)
114 {
115 return kWVGA;
116 }else if(width == 1280 && height == 720)
117 {
118 return kWHD;
119 }else if(width == 1920 && height == 1080)
120 {
121 return kWFullHD;
122 }
123 return kUndefined;
124}
125
126unsigned int
127VideoSource::GetFrameLength() const
128{
129 return webrtc::CalcBufferSize(_type, _width, _height);
130}
131
132const char*
133VideoSource::GetMySizeString() const
134{
135 return VideoSource::GetSizeString(GetSize());
136}
137
138const char*
139VideoSource::GetSizeString(VideoSize size)
140{
141 switch (size)
142 {
143 case kSQCIF:
144 return "SQCIF";
145 case kQQVGA:
146 return "QQVGA";
147 case kQCIF:
148 return "QCIF";
149 case kQVGA:
150 return "QVGA";
151 case kCIF:
152 return "CIF";
153 case kVGA:
154 return "VGA";
155 case kNTSC:
156 return "NTSC";
157 case k4CIF:
158 return "4CIF";
159 case kSVGA:
160 return "SVGA";
161 case kHD:
162 return "HD";
163 case kXGA:
164 return "XGA";
165 case kFullHD:
166 return "Full_HD";
167 case kWQVGA:
168 return "WQVGA";
169 case kWHD:
170 return "WHD";
171 case kWFullHD:
172 return "WFull_HD";
173 default:
174 return "Undefined";
175 }
176}
177
178std::string
179VideoSource::GetFilePath() const
180{
181 size_t slashPos = _fileName.find_last_of("/\\");
182 if (slashPos == std::string::npos)
183 {
184 return ".";
185 }
186
187 return _fileName.substr(0, slashPos);
188}
189
190std::string
191VideoSource::GetName() const
192{
193 // Remove path.
194 size_t slashPos = _fileName.find_last_of("/\\");
195 if (slashPos == std::string::npos)
196 {
197 slashPos = 0;
198 }
199 else
200 {
201 slashPos++;
202 }
203
204 // Remove extension and underscored suffix if it exists.
205 return _fileName.substr(slashPos, std::min(_fileName.find_last_of("_"),
206 _fileName.find_last_of(".")) - slashPos);
207}
208
209void
210VideoSource::Convert(const VideoSource &target, bool force /* = false */) const
211{
212 // Ensure target rate is less than or equal to source
213 // (i.e. we are only temporally downsampling).
214 ASSERT_TRUE(target.GetFrameRate() <= _frameRate);
215 // Only supports YUV420 currently.
216 ASSERT_TRUE(_type == webrtc::VideoType::kI420 && target.GetType() == webrtc::VideoType::kI420);
217 if (!force && (FileExists(target.GetFileName().c_str()) ||
218 (target.GetWidth() == _width && target.GetHeight() == _height && target.GetFrameRate() == _frameRate)))
219 {
220 // Assume that the filename uniquely defines the content.
221 // If the file already exists, it is the correct file.
222 return;
223 }
224 FILE *inFile = NULL;
225 FILE *outFile = NULL;
226
227 inFile = fopen(_fileName.c_str(), "rb");
228 ASSERT_TRUE(inFile != NULL);
229
230 outFile = fopen(target.GetFileName().c_str(), "wb");
231 ASSERT_TRUE(outFile != NULL);
232
233 FrameDropper fd;
234 fd.SetFrameRate(target.GetFrameRate(), _frameRate);
235
236 const size_t lengthOutFrame = webrtc::CalcBufferSize(target.GetType(),
237 target.GetWidth(), target.GetHeight());
238 ASSERT_TRUE(lengthOutFrame > 0);
239 unsigned char *outFrame = new unsigned char[lengthOutFrame];
240
241 const size_t lengthInFrame = webrtc::CalcBufferSize(_type, _width, _height);
242 ASSERT_TRUE(lengthInFrame > 0);
243 unsigned char *inFrame = new unsigned char[lengthInFrame];
244
245 while (fread(inFrame, 1, lengthInFrame, inFile) == lengthInFrame)
246 {
247 if (!fd.DropFrame())
248 {
249 ASSERT_TRUE(target.GetWidth() == _width &&
250 target.GetHeight() == _height);
251 // Add video interpolator here!
252 if (fwrite(outFrame, 1, lengthOutFrame,
253 outFile) != lengthOutFrame) {
254 return;
255 }
256 }
257 }
258
259 delete inFrame;
260 delete outFrame;
261 fclose(inFile);
262 fclose(outFile);
263}
264
265bool VideoSource::FileExists(const char* fileName)
266{
267 FILE* fp = NULL;
268 fp = fopen(fileName, "rb");
269 if(fp != NULL)
270 {
271 fclose(fp);
272 return true;
273 }
274 return false;
275}
276
277
278int
279VideoSource::GetWidthHeight( VideoSize size, int & width, int& height)
280{
281 switch(size)
282 {
283 case kSQCIF:
284 width = 128;
285 height = 96;
286 return 0;
287 case kQQVGA:
288 width = 160;
289 height = 120;
290 return 0;
291 case kQCIF:
292 width = 176;
293 height = 144;
294 return 0;
295 case kCGA:
296 width = 320;
297 height = 200;
298 return 0;
299 case kQVGA:
300 width = 320;
301 height = 240;
302 return 0;
303 case kSIF:
304 width = 352;
305 height = 240;
306 return 0;
307 case kWQVGA:
308 width = 400;
309 height = 240;
310 return 0;
311 case kCIF:
312 width = 352;
313 height = 288;
314 return 0;
315 case kW288p:
316 width = 512;
317 height = 288;
318 return 0;
319 case k448p:
320 width = 576;
321 height = 448;
322 return 0;
323 case kVGA:
324 width = 640;
325 height = 480;
326 return 0;
327 case k432p:
328 width = 720;
329 height = 432;
330 return 0;
331 case kW432p:
332 width = 768;
333 height = 432;
334 return 0;
335 case k4SIF:
336 width = 704;
337 height = 480;
338 return 0;
339 case kW448p:
340 width = 768;
341 height = 448;
342 return 0;
343 case kNTSC:
344 width = 720;
345 height = 480;
346 return 0;
347 case kFW448p:
348 width = 800;
349 height = 448;
350 return 0;
351 case kWVGA:
352 width = 800;
353 height = 480;
354 return 0;
355 case k4CIF:
356 width = 704;
357 height = 576;
358 return 0;
359 case kSVGA:
360 width = 800;
361 height = 600;
362 return 0;
363 case kW544p:
364 width = 960;
365 height = 544;
366 return 0;
367 case kW576p:
368 width = 1024;
369 height = 576;
370 return 0;
371 case kHD:
372 width = 960;
373 height = 720;
374 return 0;
375 case kXGA:
376 width = 1024;
377 height = 768;
378 return 0;
379 case kFullHD:
380 width = 1440;
381 height = 1080;
382 return 0;
383 case kWHD:
384 width = 1280;
385 height = 720;
386 return 0;
387 case kWFullHD:
388 width = 1920;
389 height = 1080;
390 return 0;
391 default:
392 return -1;
393 }
394}
395
396FrameDropper::FrameDropper()
397:
398_dropsBetweenRenders(0),
399_frameCounter(0)
400{
401}
402
403bool
404FrameDropper::DropFrame()
405{
406 _frameCounter++;
407 if (_frameCounter > _dropsBetweenRenders)
408 {
409 _frameCounter = 0;
410 return false;
411 }
412 return true;
413}
414
415unsigned int
416FrameDropper::DropsBetweenRenders()
417{
418 return _dropsBetweenRenders;
419}
420
421void
422FrameDropper::SetFrameRate(double frameRate, double maxFrameRate)
423{
424 if (frameRate >= 1.0)
425 {
426 _dropsBetweenRenders = static_cast<unsigned int>(maxFrameRate / frameRate + 0.5) - 1;
427 }
428 else
429 {
430 _dropsBetweenRenders = 0;
431 }
432}