1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/*!
*************************************************************************************
* \file img_io.c
*
* \brief
* image I/O related functions
*
* \author
* Main contributors (see contributors.h for copyright, address and affiliation details)
* - Alexis Michael Tourapis <alexismt@ieee.org>
*************************************************************************************
*/
#include "contributors.h"
#include "global.h"
#include "img_io.h"
#include "report.h"
static const VIDEO_SIZE VideoRes[] = {
{ "qcif" , 176, 144},
{ "qqvga" , 160, 128},
{ "qvga" , 320, 240},
{ "sif" , 352, 240},
{ "cif" , 352, 288},
{ "vga" , 640, 480},
{ "sd1" , 720, 480},
{ "sd2" , 704, 576},
{ "sd3" , 720, 576},
{ "720p" , 1280, 720},
{ "1080p" , 1920, 1080},
{ NULL, 0, 0}
};
/*!
************************************************************************
* \brief
* Parse Size from from file name
*
************************************************************************
*/
int ParseSizeFromString (VideoDataFile *input_file, int *x_size, int *y_size, double *fps)
{
char *p1, *p2, *tail;
char *fn = input_file->fname;
char c;
int i = 0;
*x_size = *y_size = -1;
p1 = p2 = fn;
while (p1 != NULL && p2 != NULL)
{
// Search for first '_'
p1 = strstr( p1, "_");
if (p1 == NULL)
break;
// Search for end character of x_size (first 'x' after last '_')
p2 = strstr( p1, "x");
// If no 'x' is found, exit
if (p2 == NULL)
break;
// Try conversion of number
*p2 = 0;
*x_size = strtol( p1 + 1, &tail, 10);
// If there are characters left in the string, or the string is null, discard conversion
if (*tail != '\0' || *(p1 + 1) == '\0')
{
*p2 = 'x';
p1 = tail;
continue;
}
// Conversion was correct. Restore string
*p2 = 'x';
// Search for end character of y_size (first '_' or '.' after last 'x')
p1 = strpbrk( p2 + 1, "_.");
// If no '_' or '.' is found, try again from current position
if (p1 == NULL)
{
p1 = p2 + 1;
continue;
}
// Try conversion of number
c = *p1;
*p1 = 0;
*y_size = strtol( p2 + 1, &tail, 10);
// If there are characters left in the string, or the string is null, discard conversion
if (*tail != '\0' || *(p2 + 1) == '\0')
{
*p1 = c;
p1 = tail;
continue;
}
// Conversion was correct. Restore string
*p1 = c;
// Search for end character of y_size (first 'i' or 'p' after last '_')
p2 = strstr( p1 + 1, "ip");
// If no 'i' or 'p' is found, exit
if (p2 == NULL)
break;
// Try conversion of number
c = *p2;
*p2 = 0;
*fps = strtod( p1 + 1, &tail);
// If there are characters left in the string, or the string is null, discard conversion
if (*tail != '\0' || *(p1 + 1) == '\0')
{
*p2 = c;
p1 = tail;
continue;
}
// Conversion was correct. Restore string
*p2 = c;
break;
}
// Now lets test some common video file formats
if (p1 == NULL || p2 == NULL)
{
for (i = 0; VideoRes[i].name != NULL; i++)
{
if (strcasecmp (fn, VideoRes[i].name))
{
*x_size = VideoRes[i].x_size;
*y_size = VideoRes[i].y_size;
// Should add frame rate support as well
break;
}
}
}
return (*x_size == -1 || *y_size == -1) ? 0 : 1;
}
/*!
************************************************************************
* \brief
* Parse Size from from file name
*
************************************************************************
*/
void ParseFrameNoFormatFromString (VideoDataFile *input_file)
{
char *p1, *p2, *tail;
char *fn = input_file->fname;
char *fhead = input_file->fhead;
char *ftail = input_file->ftail;
int *zero_pad = &input_file->zero_pad;
int *num_digits = &input_file->num_digits;
*zero_pad = 0;
*num_digits = -1;
p1 = p2 = fn;
while (p1 != NULL && p2 != NULL)
{
// Search for first '_'
p1 = strstr( p1, "%");
if (p1 == NULL)
break;
strncpy(fhead, fn, p1 - fn);
// Search for end character of x_size (first 'x' after last '_')
p2 = strstr( p1, "d");
// If no 'x' is found, exit
if (p2 == NULL)
break;
// Try conversion of number
*p2 = 0;
if (*(p1 + 1) == '0')
*zero_pad = 1;
*num_digits = strtol( p1 + 1, &tail, 10);
// If there are characters left in the string, or the string is null, discard conversion
if (*tail != '\0' || *(p1 + 1) == '\0')
{
*p2 = 'd';
p1 = tail;
continue;
}
// Conversion was correct. Restore string
*p2 = 'd';
tail++;
strncpy(ftail, tail, strlen(tail));
break;
}
if (input_file->vdtype == VIDEO_TIFF)
{
input_file->is_concatenated = 0;
}
else
input_file->is_concatenated = (*num_digits == -1) ? 1 : 0;
}
/*!
************************************************************************
* \brief
* Open file containing a single frame
************************************************************************
*/
void OpenFrameFile( VideoDataFile *input_file, int FrameNumberInFile)
{
char infile [FILE_NAME_SIZE], in_number[16];
int length = 0;
in_number[length]='\0';
length = strlen(input_file->fhead);
strncpy(infile, input_file->fhead, length);
infile[length]='\0';
if (input_file->zero_pad)
snprintf(in_number, 16, "%0*d", input_file->num_digits, FrameNumberInFile);
else
snprintf(in_number, 16, "%*d", input_file->num_digits, FrameNumberInFile);
strncat(infile, in_number, sizeof(in_number));
length += sizeof(in_number);
infile[length]='\0';
strncat(infile, input_file->ftail, strlen(input_file->ftail));
length += strlen(input_file->ftail);
infile[length]='\0';
if ((input_file->f_num = open(infile, OPENFLAGS_READ)) == -1)
{
printf ("OpenFrameFile: cannot open file %s\n", infile);
report_stats_on_error();
}
}
/*!
************************************************************************
* \brief
* Open file(s) containing the entire frame sequence
************************************************************************
*/
void OpenFiles( VideoDataFile *input_file)
{
if (input_file->is_concatenated == 1)
{
if (strlen(input_file->fname) == 0)
{
snprintf(errortext, ET_SIZE, "No input sequence name was provided. Please check settings.");
error (errortext, 500);
}
if ((input_file->f_num = open(input_file->fname, OPENFLAGS_READ)) == -1)
{
snprintf(errortext, ET_SIZE, "Input file %s does not exist",input_file->fname);
error (errortext, 500);
}
}
}
/*!
************************************************************************
* \brief
* Close input file
************************************************************************
*/
void CloseFiles(VideoDataFile *input_file)
{
if (input_file->f_num != -1)
close(input_file->f_num);
input_file->f_num = -1;
}
/* ==========================================================================
*
* ParseVideoType
*
* ==========================================================================
*/
VideoFileType ParseVideoType (VideoDataFile *input_file)
{
char *format;
format = input_file->fname + strlen(input_file->fname) - 3;
if (strcasecmp (format, "yuv") == 0)
{
input_file->vdtype = VIDEO_YUV;
input_file->format.yuv_format = YUV420;
input_file->avi = NULL;
}
else if (strcasecmp (format, "rgb") == 0)
{
input_file->vdtype = VIDEO_RGB;
input_file->format.yuv_format = YUV444;
input_file->avi = NULL;
}
else if (strcasecmp (format, "tif") == 0)
{
input_file->vdtype = VIDEO_TIFF;
input_file->avi = NULL;
}
else if (strcasecmp (format, "avi") == 0)
{
input_file->vdtype = VIDEO_AVI;
}
else
{
//snprintf(errortext, ET_SIZE, "ERROR: video file format not supported");
//error (errortext, 500);
input_file->vdtype = VIDEO_YUV;
input_file->format.yuv_format = YUV420;
input_file->avi = NULL;
}
return input_file->vdtype;
}
|