You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.6KB

  1. /*
  2. * Copyright (c) 2012 Stefano Sabatini
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include <libavutil/imgutils.h>
  23. #include <libavutil/parseutils.h>
  24. #include <libswscale/swscale.h>
  25. static void fill_yuv_image(uint8_t *data[4], int linesize[4],
  26. int width, int height, int frame_index)
  27. {
  28. int x, y, i;
  29. i = frame_index;
  30. /* Y */
  31. for (y = 0; y < height; y++)
  32. for (x = 0; x < width; x++)
  33. data[0][y * linesize[0] + x] = x + y + i * 3;
  34. /* Cb and Cr */
  35. for (y = 0; y < height / 2; y++) {
  36. for (x = 0; x < width / 2; x++) {
  37. data[1][y * linesize[1] + x] = 128 + y + i * 2;
  38. data[2][y * linesize[2] + x] = 64 + x + i * 5;
  39. }
  40. }
  41. }
  42. static void save_pgm(const char *filename,
  43. const uint8_t *data, int linesize, int width, int height)
  44. {
  45. FILE *f;
  46. int i;
  47. f = fopen(filename, "w");
  48. fprintf(f, "P5\n%d %d\n%d\n", width, height, 255);
  49. for (i = 0; i < height; i++)
  50. fwrite(data + i * linesize, 1, width, f);
  51. fclose(f);
  52. }
  53. int main(int argc, char **argv)
  54. {
  55. uint8_t *src_data[4], *dst_data[4];
  56. int src_linesize[4], dst_linesize[4];
  57. int src_w = 320, src_h = 240, dst_w, dst_h;
  58. enum PixelFormat src_pix_fmt = PIX_FMT_YUV420P, dst_pix_fmt = PIX_FMT_GRAY8;
  59. const char *dst_size = argv[1];
  60. struct SwsContext *sws_ctx;
  61. int i, ret;
  62. if (argc != 2) {
  63. fprintf(stderr, "Usage: %s output_size\n"
  64. "API example program to show how to scale an image with libswscale.\n"
  65. "This program generates a series of pictures, rescales them to the given "
  66. "<output size> and finally saves the rescaled pictures as PGM files named "
  67. "like outscale<frame number>.pgm.\n"
  68. "\n", argv[0]);
  69. exit(1);
  70. }
  71. if (av_parse_video_size(&dst_w, &dst_h, dst_size) < 0) {
  72. av_log(NULL, AV_LOG_ERROR,
  73. "Invalid size '%s', must be in the form WxH or a valid size abbreviation\n",
  74. dst_size);
  75. exit(1);
  76. }
  77. /* create scaling context */
  78. sws_ctx = sws_getContext(src_w, src_h, src_pix_fmt,
  79. dst_w, dst_h, dst_pix_fmt,
  80. SWS_BILINEAR, NULL, NULL, NULL);
  81. if (!sws_ctx) {
  82. av_log(NULL, AV_LOG_ERROR,
  83. "Impossible to create scale context for the conversion "
  84. "fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
  85. av_get_pix_fmt_name(src_pix_fmt), src_w, src_h,
  86. av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h);
  87. ret = AVERROR(EINVAL);
  88. goto end;
  89. }
  90. /* allocate source and destination image buffers */
  91. if ((ret = av_image_alloc(src_data, src_linesize,
  92. src_w, src_h, src_pix_fmt, 16)) < 0)
  93. goto end;
  94. if ((ret = av_image_alloc(dst_data, dst_linesize,
  95. dst_w, dst_h, dst_pix_fmt, 16)) < 0)
  96. goto end;
  97. for (i = 0; i < 100; i++) {
  98. char filename[1024];
  99. /* generate synthetic video */
  100. fill_yuv_image(src_data, src_linesize, src_w, src_h, i);
  101. /* convert to destination format */
  102. sws_scale(sws_ctx, (const uint8_t * const*)src_data,
  103. src_linesize, 0, src_h, dst_data, dst_linesize);
  104. /* write Y plane to to output PGM file */
  105. snprintf(filename, sizeof(filename), "outscale%02d.pgm", i);
  106. save_pgm(filename, dst_data[0], dst_linesize[0], dst_w, dst_h);
  107. }
  108. end:
  109. av_freep(&src_data[0]);
  110. av_freep(&dst_data[0]);
  111. sws_freeContext(sws_ctx);
  112. return ret < 0;
  113. }