Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 1 | /* |
Krishna Rapaka | 7319db5 | 2021-09-28 20:35:29 -0700 | [diff] [blame] | 2 | * Copyright (c) 2021, Alliance for Open Media. All rights reserved |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 3 | * |
Vibhoothi | 41c6dd7 | 2021-10-12 18:48:26 +0000 | [diff] [blame] | 4 | * This source code is subject to the terms of the BSD 3-Clause Clear License |
| 5 | * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear |
| 6 | * License was not distributed with this source code in the LICENSE file, you |
| 7 | * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the |
| 8 | * Alliance for Open Media Patent License 1.0 was not distributed with this |
| 9 | * source code in the PATENTS file, you can obtain it at |
| 10 | * aomedia.org/license/patent-license/. |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <assert.h> |
| 14 | #include <limits.h> |
| 15 | #include <math.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | |
Tom Finegan | 7790213 | 2018-05-21 10:19:15 -0700 | [diff] [blame] | 20 | #include "av1/common/resize.h" |
| 21 | #include "common/tools_common.h" |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 22 | |
Tom Finegan | 9e96bdc | 2015-02-04 16:11:57 -0800 | [diff] [blame] | 23 | static const char *exec_name = NULL; |
| 24 | |
| 25 | static void usage() { |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 26 | printf("Usage:\n"); |
| 27 | printf("%s <input_yuv> <width>x<height> <target_width>x<target_height> ", |
Tom Finegan | 9e96bdc | 2015-02-04 16:11:57 -0800 | [diff] [blame] | 28 | exec_name); |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 29 | printf("<output_yuv> [<frames>]\n"); |
| 30 | } |
| 31 | |
James Zern | 59e7a47 | 2015-05-09 10:33:26 -0700 | [diff] [blame] | 32 | void usage_exit(void) { |
Tom Finegan | 9e96bdc | 2015-02-04 16:11:57 -0800 | [diff] [blame] | 33 | usage(); |
| 34 | exit(EXIT_FAILURE); |
| 35 | } |
| 36 | |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 37 | static int parse_dim(char *v, int *width, int *height) { |
| 38 | char *x = strchr(v, 'x'); |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 39 | if (x == NULL) x = strchr(v, 'X'); |
| 40 | if (x == NULL) return 0; |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 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 | |
Tom Finegan | 9e96bdc | 2015-02-04 16:11:57 -0800 | [diff] [blame] | 58 | exec_name = argv[0]; |
| 59 | |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 60 | if (argc < 5) { |
| 61 | printf("Incorrect parameters:\n"); |
Tom Finegan | 9e96bdc | 2015-02-04 16:11:57 -0800 | [diff] [blame] | 62 | usage(); |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 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]); |
Tom Finegan | 9e96bdc | 2015-02-04 16:11:57 -0800 | [diff] [blame] | 70 | usage(); |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 71 | return 1; |
| 72 | } |
| 73 | if (!parse_dim(argv[3], &target_width, &target_height)) { |
| 74 | printf("Incorrect parameters: %s\n", argv[3]); |
Tom Finegan | 9e96bdc | 2015-02-04 16:11:57 -0800 | [diff] [blame] | 75 | usage(); |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 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); |
Tom Finegan | 9e96bdc | 2015-02-04 16:11:57 -0800 | [diff] [blame] | 82 | usage(); |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 83 | return 1; |
| 84 | } |
| 85 | fpout = fopen(fout, "wb"); |
| 86 | if (fpout == NULL) { |
Yaowu Xu | 2df76f2 | 2020-04-10 09:37:34 -0700 | [diff] [blame] | 87 | fclose(fpin); |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 88 | printf("Can't open file %s to write\n", fout); |
Tom Finegan | 9e96bdc | 2015-02-04 16:11:57 -0800 | [diff] [blame] | 89 | usage(); |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 90 | return 1; |
| 91 | } |
| 92 | if (argc >= 6) |
| 93 | frames = atoi(argv[5]); |
| 94 | else |
| 95 | frames = INT_MAX; |
| 96 | |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 97 | printf("Input size: %dx%d\n", width, height); |
| 98 | printf("Target size: %dx%d, Frames: ", target_width, target_height); |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 99 | if (frames == INT_MAX) |
| 100 | printf("All\n"); |
| 101 | else |
| 102 | printf("%d\n", frames); |
| 103 | |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 104 | inbuf = (uint8_t *)malloc(width * height * 3 / 2); |
| 105 | outbuf = (uint8_t *)malloc(target_width * target_height * 3 / 2); |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 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) { |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 112 | if (fread(inbuf, width * height * 3 / 2, 1, fpin) != 1) break; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 113 | av1_resize_frame420(inbuf, width, inbuf_u, inbuf_v, width / 2, height, |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 114 | width, outbuf, target_width, outbuf_u, outbuf_v, |
| 115 | target_width / 2, target_height, target_width); |
Deb Mukherjee | cacea00 | 2014-01-23 14:53:53 -0800 | [diff] [blame] | 116 | fwrite(outbuf, target_width * target_height * 3 / 2, 1, fpout); |
| 117 | f++; |
| 118 | } |
| 119 | printf("%d frames processed\n", f); |
| 120 | fclose(fpin); |
| 121 | fclose(fpout); |
| 122 | |
| 123 | free(inbuf); |
| 124 | free(outbuf); |
| 125 | return 0; |
| 126 | } |