]> granicus.if.org Git - libvpx/blob - examples/resize_util.c
Merge "VP9_resizing: add limitation to the downsacling resolution."
[libvpx] / examples / resize_util.c
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
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
30 void usage_exit(void) {
31   usage();
32   exit(EXIT_FAILURE);
33 }
34
35 static int parse_dim(char *v, int *width, int *height) {
36   char *x = strchr(v, 'x');
37   if (x == NULL)
38     x = strchr(v, 'X');
39   if (x == NULL)
40     return 0;
41   *width = atoi(v);
42   *height = atoi(&x[1]);
43   if (*width <= 0 || *height <= 0)
44     return 0;
45   else
46     return 1;
47 }
48
49 int main(int argc, char *argv[]) {
50   char *fin, *fout;
51   FILE *fpin, *fpout;
52   uint8_t *inbuf, *outbuf;
53   uint8_t *inbuf_u, *outbuf_u;
54   uint8_t *inbuf_v, *outbuf_v;
55   int f, frames;
56   int width, height, target_width, target_height;
57
58   exec_name = argv[0];
59
60   if (argc < 5) {
61     printf("Incorrect parameters:\n");
62     usage();
63     return 1;
64   }
65
66   fin = argv[1];
67   fout = argv[4];
68   if (!parse_dim(argv[2], &width, &height)) {
69     printf("Incorrect parameters: %s\n", argv[2]);
70     usage();
71     return 1;
72   }
73   if (!parse_dim(argv[3], &target_width, &target_height)) {
74     printf("Incorrect parameters: %s\n", argv[3]);
75     usage();
76     return 1;
77   }
78
79   fpin = fopen(fin, "rb");
80   if (fpin == NULL) {
81     printf("Can't open file %s to read\n", fin);
82     usage();
83     return 1;
84   }
85   fpout = fopen(fout, "wb");
86   if (fpout == NULL) {
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",
97          width, height);
98   printf("Target size: %dx%d, Frames: ",
99          target_width, target_height);
100   if (frames == INT_MAX)
101     printf("All\n");
102   else
103     printf("%d\n", frames);
104
105   inbuf = (uint8_t*)malloc(width * height * 3 / 2);
106   outbuf = (uint8_t*)malloc(target_width * target_height * 3 / 2);
107   inbuf_u = inbuf + width * height;
108   inbuf_v = inbuf_u + width * height / 4;
109   outbuf_u = outbuf + target_width * target_height;
110   outbuf_v = outbuf_u + target_width * target_height / 4;
111   f = 0;
112   while (f < frames) {
113     if (fread(inbuf, width * height * 3 / 2, 1, fpin) != 1)
114       break;
115     vp9_resize_frame420(inbuf, width, inbuf_u, inbuf_v, width / 2,
116                         height, width,
117                         outbuf, target_width, outbuf_u, outbuf_v,
118                         target_width / 2,
119                         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   fclose(fpin);
125   fclose(fpout);
126
127   free(inbuf);
128   free(outbuf);
129   return 0;
130 }