2
0

resize_util.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <assert.h>
  11. #include <limits.h>
  12. #include <math.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "../tools_common.h"
  17. #include "../vp9/encoder/vp9_resize.h"
  18. static const char *exec_name = NULL;
  19. static void usage() {
  20. printf("Usage:\n");
  21. printf("%s <input_yuv> <width>x<height> <target_width>x<target_height> ",
  22. exec_name);
  23. printf("<output_yuv> [<frames>]\n");
  24. }
  25. void usage_exit(void) {
  26. usage();
  27. exit(EXIT_FAILURE);
  28. }
  29. static int parse_dim(char *v, int *width, int *height) {
  30. char *x = strchr(v, 'x');
  31. if (x == NULL) x = strchr(v, 'X');
  32. if (x == NULL) 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. int main(int argc, char *argv[]) {
  41. char *fin, *fout;
  42. FILE *fpin, *fpout;
  43. uint8_t *inbuf, *outbuf;
  44. uint8_t *inbuf_u, *outbuf_u;
  45. uint8_t *inbuf_v, *outbuf_v;
  46. int f, frames;
  47. int width, height, target_width, target_height;
  48. exec_name = argv[0];
  49. if (argc < 5) {
  50. printf("Incorrect parameters:\n");
  51. usage();
  52. return 1;
  53. }
  54. fin = argv[1];
  55. fout = argv[4];
  56. if (!parse_dim(argv[2], &width, &height)) {
  57. printf("Incorrect parameters: %s\n", argv[2]);
  58. usage();
  59. return 1;
  60. }
  61. if (!parse_dim(argv[3], &target_width, &target_height)) {
  62. printf("Incorrect parameters: %s\n", argv[3]);
  63. usage();
  64. return 1;
  65. }
  66. fpin = fopen(fin, "rb");
  67. if (fpin == NULL) {
  68. printf("Can't open file %s to read\n", fin);
  69. usage();
  70. return 1;
  71. }
  72. fpout = fopen(fout, "wb");
  73. if (fpout == NULL) {
  74. printf("Can't open file %s to write\n", fout);
  75. usage();
  76. return 1;
  77. }
  78. if (argc >= 6)
  79. frames = atoi(argv[5]);
  80. else
  81. frames = INT_MAX;
  82. printf("Input size: %dx%d\n", width, height);
  83. printf("Target size: %dx%d, Frames: ", target_width, target_height);
  84. if (frames == INT_MAX)
  85. printf("All\n");
  86. else
  87. printf("%d\n", frames);
  88. inbuf = (uint8_t *)malloc(width * height * 3 / 2);
  89. outbuf = (uint8_t *)malloc(target_width * target_height * 3 / 2);
  90. inbuf_u = inbuf + width * height;
  91. inbuf_v = inbuf_u + width * height / 4;
  92. outbuf_u = outbuf + target_width * target_height;
  93. outbuf_v = outbuf_u + target_width * target_height / 4;
  94. f = 0;
  95. while (f < frames) {
  96. if (fread(inbuf, width * height * 3 / 2, 1, fpin) != 1) break;
  97. vp9_resize_frame420(inbuf, width, inbuf_u, inbuf_v, width / 2, height,
  98. width, outbuf, target_width, outbuf_u, outbuf_v,
  99. target_width / 2, target_height, target_width);
  100. fwrite(outbuf, target_width * target_height * 3 / 2, 1, fpout);
  101. f++;
  102. }
  103. printf("%d frames processed\n", f);
  104. fclose(fpin);
  105. fclose(fpout);
  106. free(inbuf);
  107. free(outbuf);
  108. return 0;
  109. }