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