blob: 5485691a8b8d3a84fa1c7d641abbda78908a54f9 [file] [log] [blame]
Deb Mukherjeecacea002014-01-23 14:53:53 -08001/*
Yaowu Xubde4ac82016-11-28 15:26:06 -08002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Deb Mukherjeecacea002014-01-23 14:53:53 -08003 *
Yaowu Xubde4ac82016-11-28 15:26:06 -08004 * 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.
Deb Mukherjeecacea002014-01-23 14:53:53 -080010 */
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
James Zern59e7a472015-05-09 10:33:26 -070019#include "../tools_common.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070020#include "../av1/encoder/av1_resize.h"
Deb Mukherjeecacea002014-01-23 14:53:53 -080021
Tom Finegan9e96bdc2015-02-04 16:11:57 -080022static const char *exec_name = NULL;
23
24static void usage() {
Deb Mukherjeecacea002014-01-23 14:53:53 -080025 printf("Usage:\n");
26 printf("%s <input_yuv> <width>x<height> <target_width>x<target_height> ",
Tom Finegan9e96bdc2015-02-04 16:11:57 -080027 exec_name);
Deb Mukherjeecacea002014-01-23 14:53:53 -080028 printf("<output_yuv> [<frames>]\n");
29}
30
James Zern59e7a472015-05-09 10:33:26 -070031void usage_exit(void) {
Tom Finegan9e96bdc2015-02-04 16:11:57 -080032 usage();
33 exit(EXIT_FAILURE);
34}
35
Deb Mukherjeecacea002014-01-23 14:53:53 -080036static int parse_dim(char *v, int *width, int *height) {
37 char *x = strchr(v, 'x');
clang-format397d9642016-08-08 18:51:16 -070038 if (x == NULL) x = strchr(v, 'X');
39 if (x == NULL) return 0;
Deb Mukherjeecacea002014-01-23 14:53:53 -080040 *width = atoi(v);
41 *height = atoi(&x[1]);
42 if (*width <= 0 || *height <= 0)
43 return 0;
44 else
45 return 1;
46}
47
48int 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
Tom Finegan9e96bdc2015-02-04 16:11:57 -080057 exec_name = argv[0];
58
Deb Mukherjeecacea002014-01-23 14:53:53 -080059 if (argc < 5) {
60 printf("Incorrect parameters:\n");
Tom Finegan9e96bdc2015-02-04 16:11:57 -080061 usage();
Deb Mukherjeecacea002014-01-23 14:53:53 -080062 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]);
Tom Finegan9e96bdc2015-02-04 16:11:57 -080069 usage();
Deb Mukherjeecacea002014-01-23 14:53:53 -080070 return 1;
71 }
72 if (!parse_dim(argv[3], &target_width, &target_height)) {
73 printf("Incorrect parameters: %s\n", argv[3]);
Tom Finegan9e96bdc2015-02-04 16:11:57 -080074 usage();
Deb Mukherjeecacea002014-01-23 14:53:53 -080075 return 1;
76 }
77
78 fpin = fopen(fin, "rb");
79 if (fpin == NULL) {
80 printf("Can't open file %s to read\n", fin);
Tom Finegan9e96bdc2015-02-04 16:11:57 -080081 usage();
Deb Mukherjeecacea002014-01-23 14:53:53 -080082 return 1;
83 }
84 fpout = fopen(fout, "wb");
85 if (fpout == NULL) {
86 printf("Can't open file %s to write\n", fout);
Tom Finegan9e96bdc2015-02-04 16:11:57 -080087 usage();
Deb Mukherjeecacea002014-01-23 14:53:53 -080088 return 1;
89 }
90 if (argc >= 6)
91 frames = atoi(argv[5]);
92 else
93 frames = INT_MAX;
94
clang-format397d9642016-08-08 18:51:16 -070095 printf("Input size: %dx%d\n", width, height);
96 printf("Target size: %dx%d, Frames: ", target_width, target_height);
Deb Mukherjeecacea002014-01-23 14:53:53 -080097 if (frames == INT_MAX)
98 printf("All\n");
99 else
100 printf("%d\n", frames);
101
clang-format397d9642016-08-08 18:51:16 -0700102 inbuf = (uint8_t *)malloc(width * height * 3 / 2);
103 outbuf = (uint8_t *)malloc(target_width * target_height * 3 / 2);
Deb Mukherjeecacea002014-01-23 14:53:53 -0800104 inbuf_u = inbuf + width * height;
105 inbuf_v = inbuf_u + width * height / 4;
106 outbuf_u = outbuf + target_width * target_height;
107 outbuf_v = outbuf_u + target_width * target_height / 4;
108 f = 0;
109 while (f < frames) {
clang-format397d9642016-08-08 18:51:16 -0700110 if (fread(inbuf, width * height * 3 / 2, 1, fpin) != 1) break;
Yaowu Xuf883b422016-08-30 14:01:10 -0700111 av1_resize_frame420(inbuf, width, inbuf_u, inbuf_v, width / 2, height,
clang-format397d9642016-08-08 18:51:16 -0700112 width, outbuf, target_width, outbuf_u, outbuf_v,
113 target_width / 2, target_height, target_width);
Deb Mukherjeecacea002014-01-23 14:53:53 -0800114 fwrite(outbuf, target_width * target_height * 3 / 2, 1, fpout);
115 f++;
116 }
117 printf("%d frames processed\n", f);
118 fclose(fpin);
119 fclose(fpout);
120
121 free(inbuf);
122 free(outbuf);
123 return 0;
124}