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.

141 lines
4.7KB

  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. /**
  23. * @file
  24. * libswscale API use example.
  25. */
  26. #include <libavutil/imgutils.h>
  27. #include <libavutil/parseutils.h>
  28. #include <libswscale/swscale.h>
  29. static void fill_yuv_image(uint8_t *data[4], int linesize[4],
  30. int width, int height, int frame_index)
  31. {
  32. int x, y, i;
  33. i = frame_index;
  34. /* Y */
  35. for (y = 0; y < height; y++)
  36. for (x = 0; x < width; x++)
  37. data[0][y * linesize[0] + x] = x + y + i * 3;
  38. /* Cb and Cr */
  39. for (y = 0; y < height / 2; y++) {
  40. for (x = 0; x < width / 2; x++) {
  41. data[1][y * linesize[1] + x] = 128 + y + i * 2;
  42. data[2][y * linesize[2] + x] = 64 + x + i * 5;
  43. }
  44. }
  45. }
  46. static void save_pgm(const char *filename,
  47. const uint8_t *data, int linesize, int width, int height)
  48. {
  49. FILE *f;
  50. int i;
  51. f = fopen(filename, "w");
  52. fprintf(f, "P5\n%d %d\n%d\n", width, height, 255);
  53. for (i = 0; i < height; i++)
  54. fwrite(data + i * linesize, 1, width, f);
  55. fclose(f);
  56. }
  57. int main(int argc, char **argv)
  58. {
  59. uint8_t *src_data[4], *dst_data[4];
  60. int src_linesize[4], dst_linesize[4];
  61. int src_w = 320, src_h = 240, dst_w, dst_h;
  62. enum PixelFormat src_pix_fmt = PIX_FMT_YUV420P, dst_pix_fmt = PIX_FMT_GRAY8;
  63. const char *dst_size = argv[1];
  64. struct SwsContext *sws_ctx;
  65. int i, ret;
  66. if (argc != 2) {
  67. fprintf(stderr, "Usage: %s output_size\n"
  68. "API example program to show how to scale an image with libswscale.\n"
  69. "This program generates a series of pictures, rescales them to the given "
  70. "<output size> and finally saves the rescaled pictures as PGM files named "
  71. "like outscale<frame number>.pgm.\n"
  72. "\n", argv[0]);
  73. exit(1);
  74. }
  75. if (av_parse_video_size(&dst_w, &dst_h, dst_size) < 0) {
  76. fprintf(stderr,
  77. "Invalid size '%s', must be in the form WxH or a valid size abbreviation\n",
  78. dst_size);
  79. exit(1);
  80. }
  81. /* create scaling context */
  82. sws_ctx = sws_getContext(src_w, src_h, src_pix_fmt,
  83. dst_w, dst_h, dst_pix_fmt,
  84. SWS_BILINEAR, NULL, NULL, NULL);
  85. if (!sws_ctx) {
  86. fprintf(stderr,
  87. "Impossible to create scale context for the conversion "
  88. "fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
  89. av_get_pix_fmt_name(src_pix_fmt), src_w, src_h,
  90. av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h);
  91. ret = AVERROR(EINVAL);
  92. goto end;
  93. }
  94. /* allocate source and destination image buffers */
  95. if ((ret = av_image_alloc(src_data, src_linesize,
  96. src_w, src_h, src_pix_fmt, 16)) < 0) {
  97. fprintf(stderr, "Could not allocate source image\n");
  98. goto end;
  99. }
  100. if ((ret = av_image_alloc(dst_data, dst_linesize,
  101. dst_w, dst_h, dst_pix_fmt, 16)) < 0) {
  102. fprintf(stderr, "Could not allocate destination image\n");
  103. goto end;
  104. }
  105. for (i = 0; i < 100; i++) {
  106. char filename[1024];
  107. /* generate synthetic video */
  108. fill_yuv_image(src_data, src_linesize, src_w, src_h, i);
  109. /* convert to destination format */
  110. sws_scale(sws_ctx, (const uint8_t * const*)src_data,
  111. src_linesize, 0, src_h, dst_data, dst_linesize);
  112. /* write Y plane to to output PGM file */
  113. snprintf(filename, sizeof(filename), "outscale%02d.pgm", i);
  114. save_pgm(filename, dst_data[0], dst_linesize[0], dst_w, dst_h);
  115. }
  116. end:
  117. av_freep(&src_data[0]);
  118. av_freep(&dst_data[0]);
  119. sws_freeContext(sws_ctx);
  120. return ret < 0;
  121. }