• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   int failed = 0;
56 
57   exec_name = argv[0];
58 
59   if (argc < 5) {
60     printf("Incorrect parameters:\n");
61     usage();
62     return 1;
63   }
64 
65   fin = argv[1];
66   fout = argv[4];
67   if (!parse_dim(argv[2], &width, &height)) {
68     printf("Incorrect parameters: %s\n", argv[2]);
69     usage();
70     return 1;
71   }
72   if (!parse_dim(argv[3], &target_width, &target_height)) {
73     printf("Incorrect parameters: %s\n", argv[3]);
74     usage();
75     return 1;
76   }
77 
78   fpin = fopen(fin, "rb");
79   if (fpin == NULL) {
80     printf("Can't open file %s to read\n", fin);
81     usage();
82     return 1;
83   }
84   fpout = fopen(fout, "wb");
85   if (fpout == NULL) {
86     fclose(fpin);
87     printf("Can't open file %s to write\n", fout);
88     usage();
89     return 1;
90   }
91   if (argc >= 6)
92     frames = atoi(argv[5]);
93   else
94     frames = INT_MAX;
95 
96   printf("Input size:  %dx%d\n", width, height);
97   printf("Target size: %dx%d, Frames: ", target_width, target_height);
98   if (frames == INT_MAX)
99     printf("All\n");
100   else
101     printf("%d\n", frames);
102 
103   inbuf = (uint8_t *)malloc(width * height * 3 / 2);
104   outbuf = (uint8_t *)malloc(target_width * target_height * 3 / 2);
105   if (!(inbuf && outbuf)) {
106     printf("Failed to allocate buffers.\n");
107     failed = 1;
108     goto Error;
109   }
110   inbuf_u = inbuf + width * height;
111   inbuf_v = inbuf_u + width * height / 4;
112   outbuf_u = outbuf + target_width * target_height;
113   outbuf_v = outbuf_u + target_width * target_height / 4;
114   f = 0;
115   while (f < frames) {
116     if (fread(inbuf, width * height * 3 / 2, 1, fpin) != 1) break;
117     vp9_resize_frame420(inbuf, width, inbuf_u, inbuf_v, width / 2, height,
118                         width, outbuf, target_width, outbuf_u, outbuf_v,
119                         target_width / 2, target_height, target_width);
120     fwrite(outbuf, target_width * target_height * 3 / 2, 1, fpout);
121     f++;
122   }
123   printf("%d frames processed\n", f);
124 Error:
125   fclose(fpin);
126   fclose(fpout);
127 
128   free(inbuf);
129   free(outbuf);
130   return failed;
131 }
132