1 /*
2  *  Copyright (c) 2014 The WebM 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 <assert.h>
12 #include <limits.h>
13 #include <math.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include "../tools_common.h"
19 #include "../vp9/encoder/vp9_resize.h"
20 
21 static const char *exec_name = NULL;
22 
usage()23 static void usage() {
24   printf("Usage:\n");
25   printf("%s <input_yuv> <width>x<height> <target_width>x<target_height> ",
26          exec_name);
27   printf("<output_yuv> [<frames>]\n");
28 }
29 
usage_exit(void)30 void usage_exit(void) {
31   usage();
32   exit(EXIT_FAILURE);
33 }
34 
parse_dim(char * v,int * width,int * height)35 static int parse_dim(char *v, int *width, int *height) {
36   char *x = strchr(v, 'x');
37   if (x == NULL) x = strchr(v, 'X');
38   if (x == NULL) return 0;
39   *width = atoi(v);
40   *height = atoi(&x[1]);
41   if (*width <= 0 || *height <= 0)
42     return 0;
43   else
44     return 1;
45 }
46 
main(int argc,char * argv[])47 int main(int argc, char *argv[]) {
48   char *fin, *fout;
49   FILE *fpin, *fpout;
50   uint8_t *inbuf, *outbuf;
51   uint8_t *inbuf_u, *outbuf_u;
52   uint8_t *inbuf_v, *outbuf_v;
53   int f, frames;
54   int width, height, target_width, target_height;
55 
56   exec_name = argv[0];
57 
58   if (argc < 5) {
59     printf("Incorrect parameters:\n");
60     usage();
61     return 1;
62   }
63 
64   fin = argv[1];
65   fout = argv[4];
66   if (!parse_dim(argv[2], &width, &height)) {
67     printf("Incorrect parameters: %s\n", argv[2]);
68     usage();
69     return 1;
70   }
71   if (!parse_dim(argv[3], &target_width, &target_height)) {
72     printf("Incorrect parameters: %s\n", argv[3]);
73     usage();
74     return 1;
75   }
76 
77   fpin = fopen(fin, "rb");
78   if (fpin == NULL) {
79     printf("Can't open file %s to read\n", fin);
80     usage();
81     return 1;
82   }
83   fpout = fopen(fout, "wb");
84   if (fpout == NULL) {
85     printf("Can't open file %s to write\n", fout);
86     usage();
87     return 1;
88   }
89   if (argc >= 6)
90     frames = atoi(argv[5]);
91   else
92     frames = INT_MAX;
93 
94   printf("Input size:  %dx%d\n", width, height);
95   printf("Target size: %dx%d, Frames: ", target_width, target_height);
96   if (frames == INT_MAX)
97     printf("All\n");
98   else
99     printf("%d\n", frames);
100 
101   inbuf = (uint8_t *)malloc(width * height * 3 / 2);
102   outbuf = (uint8_t *)malloc(target_width * target_height * 3 / 2);
103   inbuf_u = inbuf + width * height;
104   inbuf_v = inbuf_u + width * height / 4;
105   outbuf_u = outbuf + target_width * target_height;
106   outbuf_v = outbuf_u + target_width * target_height / 4;
107   f = 0;
108   while (f < frames) {
109     if (fread(inbuf, width * height * 3 / 2, 1, fpin) != 1) break;
110     vp9_resize_frame420(inbuf, width, inbuf_u, inbuf_v, width / 2, height,
111                         width, outbuf, target_width, outbuf_u, outbuf_v,
112                         target_width / 2, target_height, target_width);
113     fwrite(outbuf, target_width * target_height * 3 / 2, 1, fpout);
114     f++;
115   }
116   printf("%d frames processed\n", f);
117   fclose(fpin);
118   fclose(fpout);
119 
120   free(inbuf);
121   free(outbuf);
122   return 0;
123 }
124