WebM Codec SDK
resize_util
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 "../vp9/encoder/vp9_resize.h"
19 
20 static const char *exec_name = NULL;
21 
22 static void usage() {
23  printf("Usage:\n");
24  printf("%s <input_yuv> <width>x<height> <target_width>x<target_height> ",
25  exec_name);
26  printf("<output_yuv> [<frames>]\n");
27 }
28 
29 void usage_exit() {
30  usage();
31  exit(EXIT_FAILURE);
32 }
33 
34 static int parse_dim(char *v, int *width, int *height) {
35  char *x = strchr(v, 'x');
36  if (x == NULL)
37  x = strchr(v, 'X');
38  if (x == NULL)
39  return 0;
40  *width = atoi(v);
41  *height = atoi(&x[1]);
42  if (*width <= 0 || *height <= 0)
43  return 0;
44  else
45  return 1;
46 }
47 
48 int main(int argc, char *argv[]) {
49  char *fin, *fout;
50  FILE *fpin, *fpout;
51  uint8_t *inbuf, *outbuf;
52  uint8_t *inbuf_u, *outbuf_u;
53  uint8_t *inbuf_v, *outbuf_v;
54  int f, frames;
55  int width, height, target_width, target_height;
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  printf("Can't open file %s to write\n", fout);
87  usage();
88  return 1;
89  }
90  if (argc >= 6)
91  frames = atoi(argv[5]);
92  else
93  frames = INT_MAX;
94 
95  printf("Input size: %dx%d\n",
96  width, height);
97  printf("Target size: %dx%d, Frames: ",
98  target_width, target_height);
99  if (frames == INT_MAX)
100  printf("All\n");
101  else
102  printf("%d\n", frames);
103 
104  inbuf = (uint8_t*)malloc(width * height * 3 / 2);
105  outbuf = (uint8_t*)malloc(target_width * target_height * 3 / 2);
106  inbuf_u = inbuf + width * height;
107  inbuf_v = inbuf_u + width * height / 4;
108  outbuf_u = outbuf + target_width * target_height;
109  outbuf_v = outbuf_u + target_width * target_height / 4;
110  f = 0;
111  while (f < frames) {
112  if (fread(inbuf, width * height * 3 / 2, 1, fpin) != 1)
113  break;
114  vp9_resize_frame420(inbuf, width, inbuf_u, inbuf_v, width / 2,
115  height, width,
116  outbuf, target_width, outbuf_u, outbuf_v,
117  target_width / 2,
118  target_height, target_width);
119  fwrite(outbuf, target_width * target_height * 3 / 2, 1, fpout);
120  f++;
121  }
122  printf("%d frames processed\n", f);
123  fclose(fpin);
124  fclose(fpout);
125 
126  free(inbuf);
127  free(outbuf);
128  return 0;
129 }