|
1 /* |
|
2 * Copyright 2013 The LibYuv 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 // Convert an ARGB image to YUV. |
|
12 // Usage: convert src_argb.raw dst_yuv.raw |
|
13 |
|
14 #ifndef _CRT_SECURE_NO_WARNINGS |
|
15 #define _CRT_SECURE_NO_WARNINGS |
|
16 #endif |
|
17 |
|
18 #include <stddef.h> |
|
19 #include <stdio.h> |
|
20 #include <stdlib.h> |
|
21 #include <string.h> |
|
22 |
|
23 #include "libyuv/convert.h" |
|
24 #include "libyuv/planar_functions.h" |
|
25 #include "libyuv/scale_argb.h" |
|
26 |
|
27 // options |
|
28 bool verbose = false; |
|
29 bool attenuate = false; |
|
30 bool unattenuate = false; |
|
31 int image_width = 0, image_height = 0; // original width and height |
|
32 int dst_width = 0, dst_height = 0; // new width and height |
|
33 int fileindex_org = 0; // argv argument contains the original file name. |
|
34 int fileindex_rec = 0; // argv argument contains the reconstructed file name. |
|
35 int num_rec = 0; // Number of reconstructed images. |
|
36 int num_skip_org = 0; // Number of frames to skip in original. |
|
37 int num_frames = 0; // Number of frames to convert. |
|
38 int filter = 1; // Bilinear filter for scaling. |
|
39 |
|
40 static __inline uint32 Abs(int32 v) { |
|
41 return v >= 0 ? v : -v; |
|
42 } |
|
43 |
|
44 // Parse PYUV format. ie name.1920x800_24Hz_P420.yuv |
|
45 bool ExtractResolutionFromFilename(const char* name, |
|
46 int* width_ptr, |
|
47 int* height_ptr) { |
|
48 // Isolate the .width_height. section of the filename by searching for a |
|
49 // dot or underscore followed by a digit. |
|
50 for (int i = 0; name[i]; ++i) { |
|
51 if ((name[i] == '.' || name[i] == '_') && |
|
52 name[i + 1] >= '0' && name[i + 1] <= '9') { |
|
53 int n = sscanf(name + i + 1, "%dx%d", width_ptr, height_ptr); // NOLINT |
|
54 if (2 == n) { |
|
55 return true; |
|
56 } |
|
57 } |
|
58 } |
|
59 return false; |
|
60 } |
|
61 |
|
62 void PrintHelp(const char * program) { |
|
63 printf("%s [-options] src_argb.raw dst_yuv.raw\n", program); |
|
64 printf(" -s <width> <height> .... specify source resolution. " |
|
65 "Optional if name contains\n" |
|
66 " resolution (ie. " |
|
67 "name.1920x800_24Hz_P420.yuv)\n" |
|
68 " Negative value mirrors.\n"); |
|
69 printf(" -d <width> <height> .... specify destination resolution.\n"); |
|
70 printf(" -f <filter> ............ 0 = point, 1 = bilinear (default).\n"); |
|
71 printf(" -skip <src_argb> ....... Number of frame to skip of src_argb\n"); |
|
72 printf(" -frames <num> .......... Number of frames to convert\n"); |
|
73 printf(" -attenuate ............. Attenuate the ARGB image\n"); |
|
74 printf(" -unattenuate ........... Unattenuate the ARGB image\n"); |
|
75 printf(" -v ..................... verbose\n"); |
|
76 printf(" -h ..................... this help\n"); |
|
77 exit(0); |
|
78 } |
|
79 |
|
80 void ParseOptions(int argc, const char* argv[]) { |
|
81 if (argc <= 1) PrintHelp(argv[0]); |
|
82 for (int c = 1; c < argc; ++c) { |
|
83 if (!strcmp(argv[c], "-v")) { |
|
84 verbose = true; |
|
85 } else if (!strcmp(argv[c], "-attenuate")) { |
|
86 attenuate = true; |
|
87 } else if (!strcmp(argv[c], "-unattenuate")) { |
|
88 unattenuate = true; |
|
89 } else if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) { |
|
90 PrintHelp(argv[0]); |
|
91 } else if (!strcmp(argv[c], "-s") && c + 2 < argc) { |
|
92 image_width = atoi(argv[++c]); // NOLINT |
|
93 image_height = atoi(argv[++c]); // NOLINT |
|
94 } else if (!strcmp(argv[c], "-d") && c + 2 < argc) { |
|
95 dst_width = atoi(argv[++c]); // NOLINT |
|
96 dst_height = atoi(argv[++c]); // NOLINT |
|
97 } else if (!strcmp(argv[c], "-skip") && c + 1 < argc) { |
|
98 num_skip_org = atoi(argv[++c]); // NOLINT |
|
99 } else if (!strcmp(argv[c], "-frames") && c + 1 < argc) { |
|
100 num_frames = atoi(argv[++c]); // NOLINT |
|
101 } else if (!strcmp(argv[c], "-f") && c + 1 < argc) { |
|
102 filter = atoi(argv[++c]); // NOLINT |
|
103 } else if (argv[c][0] == '-') { |
|
104 fprintf(stderr, "Unknown option. %s\n", argv[c]); |
|
105 } else if (fileindex_org == 0) { |
|
106 fileindex_org = c; |
|
107 } else if (fileindex_rec == 0) { |
|
108 fileindex_rec = c; |
|
109 num_rec = 1; |
|
110 } else { |
|
111 ++num_rec; |
|
112 } |
|
113 } |
|
114 if (fileindex_org == 0 || fileindex_rec == 0) { |
|
115 fprintf(stderr, "Missing filenames\n"); |
|
116 PrintHelp(argv[0]); |
|
117 } |
|
118 if (num_skip_org < 0) { |
|
119 fprintf(stderr, "Skipped frames incorrect\n"); |
|
120 PrintHelp(argv[0]); |
|
121 } |
|
122 if (num_frames < 0) { |
|
123 fprintf(stderr, "Number of frames incorrect\n"); |
|
124 PrintHelp(argv[0]); |
|
125 } |
|
126 |
|
127 int org_width, org_height; |
|
128 int rec_width, rec_height; |
|
129 bool org_res_avail = ExtractResolutionFromFilename(argv[fileindex_org], |
|
130 &org_width, |
|
131 &org_height); |
|
132 bool rec_res_avail = ExtractResolutionFromFilename(argv[fileindex_rec], |
|
133 &rec_width, |
|
134 &rec_height); |
|
135 if (image_width == 0 || image_height == 0) { |
|
136 if (org_res_avail) { |
|
137 image_width = org_width; |
|
138 image_height = org_height; |
|
139 } else if (rec_res_avail) { |
|
140 image_width = rec_width; |
|
141 image_height = rec_height; |
|
142 } else { |
|
143 fprintf(stderr, "Missing dimensions.\n"); |
|
144 PrintHelp(argv[0]); |
|
145 } |
|
146 } |
|
147 if (dst_width == 0 || dst_height == 0) { |
|
148 if (rec_res_avail) { |
|
149 dst_width = rec_width; |
|
150 dst_height = rec_height; |
|
151 } else { |
|
152 dst_width = Abs(image_width); |
|
153 dst_height = Abs(image_height); |
|
154 } |
|
155 } |
|
156 } |
|
157 |
|
158 static const int kTileX = 32; |
|
159 static const int kTileY = 32; |
|
160 |
|
161 static int TileARGBScale(const uint8* src_argb, int src_stride_argb, |
|
162 int src_width, int src_height, |
|
163 uint8* dst_argb, int dst_stride_argb, |
|
164 int dst_width, int dst_height, |
|
165 libyuv::FilterMode filtering) { |
|
166 for (int y = 0; y < dst_height; y += kTileY) { |
|
167 for (int x = 0; x < dst_width; x += kTileX) { |
|
168 int clip_width = kTileX; |
|
169 if (x + clip_width > dst_width) { |
|
170 clip_width = dst_width - x; |
|
171 } |
|
172 int clip_height = kTileY; |
|
173 if (y + clip_height > dst_height) { |
|
174 clip_height = dst_height - y; |
|
175 } |
|
176 int r = libyuv::ARGBScaleClip(src_argb, src_stride_argb, |
|
177 src_width, src_height, |
|
178 dst_argb, dst_stride_argb, |
|
179 dst_width, dst_height, |
|
180 x, y, clip_width, clip_height, filtering); |
|
181 if (r) { |
|
182 return r; |
|
183 } |
|
184 } |
|
185 } |
|
186 return 0; |
|
187 } |
|
188 |
|
189 int main(int argc, const char* argv[]) { |
|
190 ParseOptions(argc, argv); |
|
191 |
|
192 // Open original file (first file argument) |
|
193 FILE* const file_org = fopen(argv[fileindex_org], "rb"); |
|
194 if (file_org == NULL) { |
|
195 fprintf(stderr, "Cannot open %s\n", argv[fileindex_org]); |
|
196 exit(1); |
|
197 } |
|
198 |
|
199 // Open all files to convert to |
|
200 FILE** file_rec = new FILE* [num_rec]; |
|
201 memset(file_rec, 0, num_rec * sizeof(FILE*)); // NOLINT |
|
202 for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) { |
|
203 file_rec[cur_rec] = fopen(argv[fileindex_rec + cur_rec], "wb"); |
|
204 if (file_rec[cur_rec] == NULL) { |
|
205 fprintf(stderr, "Cannot open %s\n", argv[fileindex_rec + cur_rec]); |
|
206 fclose(file_org); |
|
207 for (int i = 0; i < cur_rec; ++i) { |
|
208 fclose(file_rec[i]); |
|
209 } |
|
210 delete[] file_rec; |
|
211 exit(1); |
|
212 } |
|
213 } |
|
214 |
|
215 bool org_is_yuv = strstr(argv[fileindex_org], "_P420.") != NULL; |
|
216 bool org_is_argb = strstr(argv[fileindex_org], "_ARGB.") != NULL; |
|
217 if (!org_is_yuv && !org_is_argb) { |
|
218 fprintf(stderr, "Original format unknown %s\n", argv[fileindex_org]); |
|
219 exit(1); |
|
220 } |
|
221 int org_size = Abs(image_width) * Abs(image_height) * 4; // ARGB |
|
222 // Input is YUV |
|
223 if (org_is_yuv) { |
|
224 const int y_size = Abs(image_width) * Abs(image_height); |
|
225 const int uv_size = ((Abs(image_width) + 1) / 2) * |
|
226 ((Abs(image_height) + 1) / 2); |
|
227 org_size = y_size + 2 * uv_size; // YUV original. |
|
228 } |
|
229 |
|
230 const int dst_size = dst_width * dst_height * 4; // ARGB scaled |
|
231 const int y_size = dst_width * dst_height; |
|
232 const int uv_size = ((dst_width + 1) / 2) * ((dst_height + 1) / 2); |
|
233 const size_t total_size = y_size + 2 * uv_size; |
|
234 #if defined(_MSC_VER) |
|
235 _fseeki64(file_org, |
|
236 static_cast<__int64>(num_skip_org) * |
|
237 static_cast<__int64>(org_size), SEEK_SET); |
|
238 #else |
|
239 fseek(file_org, num_skip_org * total_size, SEEK_SET); |
|
240 #endif |
|
241 |
|
242 uint8* const ch_org = new uint8[org_size]; |
|
243 uint8* const ch_dst = new uint8[dst_size]; |
|
244 uint8* const ch_rec = new uint8[total_size]; |
|
245 if (ch_org == NULL || ch_rec == NULL) { |
|
246 fprintf(stderr, "No memory available\n"); |
|
247 fclose(file_org); |
|
248 for (int i = 0; i < num_rec; ++i) { |
|
249 fclose(file_rec[i]); |
|
250 } |
|
251 delete[] ch_org; |
|
252 delete[] ch_dst; |
|
253 delete[] ch_rec; |
|
254 delete[] file_rec; |
|
255 exit(1); |
|
256 } |
|
257 |
|
258 if (verbose) { |
|
259 printf("Size: %dx%d to %dx%d\n", image_width, image_height, |
|
260 dst_width, dst_height); |
|
261 } |
|
262 |
|
263 int number_of_frames; |
|
264 for (number_of_frames = 0; ; ++number_of_frames) { |
|
265 if (num_frames && number_of_frames >= num_frames) |
|
266 break; |
|
267 |
|
268 // Load original YUV or ARGB frame. |
|
269 size_t bytes_org = fread(ch_org, sizeof(uint8), |
|
270 static_cast<size_t>(org_size), file_org); |
|
271 if (bytes_org < static_cast<size_t>(org_size)) |
|
272 break; |
|
273 |
|
274 // TODO(fbarchard): Attenuate doesnt need to know dimensions. |
|
275 // ARGB attenuate frame |
|
276 if (org_is_argb && attenuate) { |
|
277 libyuv::ARGBAttenuate(ch_org, 0, ch_org, 0, org_size / 4, 1); |
|
278 } |
|
279 // ARGB unattenuate frame |
|
280 if (org_is_argb && unattenuate) { |
|
281 libyuv::ARGBUnattenuate(ch_org, 0, ch_org, 0, org_size / 4, 1); |
|
282 } |
|
283 |
|
284 for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) { |
|
285 // Scale YUV or ARGB frame. |
|
286 if (org_is_yuv) { |
|
287 int src_width = Abs(image_width); |
|
288 int src_height = Abs(image_height); |
|
289 int half_src_width = (src_width + 1) / 2; |
|
290 int half_src_height = (src_height + 1) / 2; |
|
291 int half_dst_width = (dst_width + 1) / 2; |
|
292 int half_dst_height = (dst_height + 1) / 2; |
|
293 I420Scale(ch_org, src_width, |
|
294 ch_org + src_width * src_height, half_src_width, |
|
295 ch_org + src_width * src_height + |
|
296 half_src_width * half_src_height, half_src_width, |
|
297 image_width, image_height, |
|
298 ch_rec, dst_width, |
|
299 ch_rec + dst_width * dst_height, half_dst_width, |
|
300 ch_rec + dst_width * dst_height + |
|
301 half_dst_width * half_dst_height, half_dst_width, |
|
302 dst_width, dst_height, |
|
303 static_cast<libyuv::FilterMode>(filter)); |
|
304 } else { |
|
305 TileARGBScale(ch_org, Abs(image_width) * 4, |
|
306 image_width, image_height, |
|
307 ch_dst, dst_width * 4, |
|
308 dst_width, dst_height, |
|
309 static_cast<libyuv::FilterMode>(filter)); |
|
310 } |
|
311 bool rec_is_yuv = strstr(argv[fileindex_rec + cur_rec], "_P420.") != NULL; |
|
312 bool rec_is_argb = |
|
313 strstr(argv[fileindex_rec + cur_rec], "_ARGB.") != NULL; |
|
314 if (!rec_is_yuv && !rec_is_argb) { |
|
315 fprintf(stderr, "Output format unknown %s\n", |
|
316 argv[fileindex_rec + cur_rec]); |
|
317 continue; // Advance to next file. |
|
318 } |
|
319 |
|
320 // Convert ARGB to YUV. |
|
321 if (!org_is_yuv && rec_is_yuv) { |
|
322 int half_width = (dst_width + 1) / 2; |
|
323 int half_height = (dst_height + 1) / 2; |
|
324 libyuv::ARGBToI420(ch_dst, dst_width * 4, |
|
325 ch_rec, dst_width, |
|
326 ch_rec + dst_width * dst_height, half_width, |
|
327 ch_rec + dst_width * dst_height + |
|
328 half_width * half_height, half_width, |
|
329 dst_width, dst_height); |
|
330 } |
|
331 |
|
332 // Output YUV or ARGB frame. |
|
333 if (rec_is_yuv) { |
|
334 size_t bytes_rec = fwrite(ch_rec, sizeof(uint8), |
|
335 static_cast<size_t>(total_size), |
|
336 file_rec[cur_rec]); |
|
337 if (bytes_rec < static_cast<size_t>(total_size)) |
|
338 break; |
|
339 } else { |
|
340 size_t bytes_rec = fwrite(ch_dst, sizeof(uint8), |
|
341 static_cast<size_t>(dst_size), |
|
342 file_rec[cur_rec]); |
|
343 if (bytes_rec < static_cast<size_t>(dst_size)) |
|
344 break; |
|
345 } |
|
346 if (verbose) { |
|
347 printf("%5d", number_of_frames); |
|
348 } |
|
349 if (verbose) { |
|
350 printf("\t%s", argv[fileindex_rec + cur_rec]); |
|
351 printf("\n"); |
|
352 } |
|
353 } |
|
354 } |
|
355 |
|
356 fclose(file_org); |
|
357 for (int cur_rec = 0; cur_rec < num_rec; ++cur_rec) { |
|
358 fclose(file_rec[cur_rec]); |
|
359 } |
|
360 delete[] ch_org; |
|
361 delete[] ch_dst; |
|
362 delete[] ch_rec; |
|
363 delete[] file_rec; |
|
364 return 0; |
|
365 } |