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