1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #include <assert.h>
13 #include <limits.h>
14 #include <math.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "av1/common/resize.h"
20 #include "common/tools_common.h"
21
22 static const char *exec_name = NULL;
23
usage()24 static void usage() {
25 printf("Usage:\n");
26 printf("%s <input_yuv> <width>x<height> <target_width>x<target_height> ",
27 exec_name);
28 printf("<output_yuv> [<frames>]\n");
29 }
30
usage_exit(void)31 void usage_exit(void) {
32 usage();
33 exit(EXIT_FAILURE);
34 }
35
parse_dim(char * v,int * width,int * height)36 static int parse_dim(char *v, int *width, int *height) {
37 char *x = strchr(v, 'x');
38 if (x == NULL) x = strchr(v, 'X');
39 if (x == NULL) 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
main(int argc,char * argv[])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 int failed = 0;
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 fclose(fpin);
88 printf("Can't open file %s to write\n", fout);
89 usage();
90 return 1;
91 }
92 if (argc >= 6)
93 frames = atoi(argv[5]);
94 else
95 frames = INT_MAX;
96
97 printf("Input size: %dx%d\n", width, height);
98 printf("Target size: %dx%d, Frames: ", 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 if (!(inbuf && outbuf)) {
107 printf("Failed to allocate buffers.\n");
108 failed = 1;
109 goto Error;
110 }
111 inbuf_u = inbuf + width * height;
112 inbuf_v = inbuf_u + width * height / 4;
113 outbuf_u = outbuf + target_width * target_height;
114 outbuf_v = outbuf_u + target_width * target_height / 4;
115 f = 0;
116 while (f < frames) {
117 if (fread(inbuf, width * height * 3 / 2, 1, fpin) != 1) break;
118 av1_resize_frame420(inbuf, width, inbuf_u, inbuf_v, width / 2, height,
119 width, outbuf, target_width, outbuf_u, outbuf_v,
120 target_width / 2, target_height, target_width);
121 fwrite(outbuf, target_width * target_height * 3 / 2, 1, fpout);
122 f++;
123 }
124 printf("%d frames processed\n", f);
125 Error:
126 fclose(fpin);
127 fclose(fpout);
128
129 free(inbuf);
130 free(outbuf);
131 return failed;
132 }
133