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
20static void usage(char *progname) {
21  printf("Usage:\n");
22  printf("%s <input_yuv> <width>x<height> <target_width>x<target_height> ",
23         progname);
24  printf("<output_yuv> [<frames>]\n");
25}
26
27static int parse_dim(char *v, int *width, int *height) {
28  char *x = strchr(v, 'x');
29  if (x == NULL)
30    x = strchr(v, 'X');
31  if (x == NULL)
32    return 0;
33  *width = atoi(v);
34  *height = atoi(&x[1]);
35  if (*width <= 0 || *height <= 0)
36    return 0;
37  else
38    return 1;
39}
40
41int main(int argc, char *argv[]) {
42  char *fin, *fout;
43  FILE *fpin, *fpout;
44  uint8_t *inbuf, *outbuf;
45  uint8_t *inbuf_u, *outbuf_u;
46  uint8_t *inbuf_v, *outbuf_v;
47  int f, frames;
48  int width, height, target_width, target_height;
49
50  if (argc < 5) {
51    printf("Incorrect parameters:\n");
52    usage(argv[0]);
53    return 1;
54  }
55
56  fin = argv[1];
57  fout = argv[4];
58  if (!parse_dim(argv[2], &width, &height)) {
59    printf("Incorrect parameters: %s\n", argv[2]);
60    usage(argv[0]);
61    return 1;
62  }
63  if (!parse_dim(argv[3], &target_width, &target_height)) {
64    printf("Incorrect parameters: %s\n", argv[3]);
65    usage(argv[0]);
66    return 1;
67  }
68
69  fpin = fopen(fin, "rb");
70  if (fpin == NULL) {
71    printf("Can't open file %s to read\n", fin);
72    usage(argv[0]);
73    return 1;
74  }
75  fpout = fopen(fout, "wb");
76  if (fpout == NULL) {
77    printf("Can't open file %s to write\n", fout);
78    usage(argv[0]);
79    return 1;
80  }
81  if (argc >= 6)
82    frames = atoi(argv[5]);
83  else
84    frames = INT_MAX;
85
86  printf("Input size:  %dx%d\n",
87         width, height);
88  printf("Target size: %dx%d, Frames: ",
89         target_width, target_height);
90  if (frames == INT_MAX)
91    printf("All\n");
92  else
93    printf("%d\n", frames);
94
95  inbuf = (uint8_t*)malloc(width * height * 3 / 2);
96  outbuf = (uint8_t*)malloc(target_width * target_height * 3 / 2);
97  inbuf_u = inbuf + width * height;
98  inbuf_v = inbuf_u + width * height / 4;
99  outbuf_u = outbuf + target_width * target_height;
100  outbuf_v = outbuf_u + target_width * target_height / 4;
101  f = 0;
102  while (f < frames) {
103    if (fread(inbuf, width * height * 3 / 2, 1, fpin) != 1)
104      break;
105    vp9_resize_frame420(inbuf, width, inbuf_u, inbuf_v, width / 2,
106                        height, width,
107                        outbuf, target_width, outbuf_u, outbuf_v,
108                        target_width / 2,
109                        target_height, target_width);
110    fwrite(outbuf, target_width * target_height * 3 / 2, 1, fpout);
111    f++;
112  }
113  printf("%d frames processed\n", f);
114  fclose(fpin);
115  fclose(fpout);
116
117  free(inbuf);
118  free(outbuf);
119  return 0;
120}
121