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.9KB

  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;
  33. /* Y */
  34. for (y = 0; y < height; y++)
  35. for (x = 0; x < width; x++)
  36. data[0][y * linesize[0] + x] = x + y + frame_index * 3;
  37. /* Cb and Cr */
  38. for (y = 0; y < height / 2; y++) {
  39. for (x = 0; x < width / 2; x++) {
  40. data[1][y * linesize[1] + x] = 128 + y + frame_index * 2;
  41. data[2][y * linesize[2] + x] = 64 + x + frame_index * 5;
  42. }
  43. }
  44. }
  45. int main(int argc, char **argv)
  46. {
  47. uint8_t *src_data[4], *dst_data[4];
  48. int src_linesize[4], dst_linesize[4];
  49. int src_w = 320, src_h = 240, dst_w, dst_h;
  50. enum AVPixelFormat src_pix_fmt = AV_PIX_FMT_YUV420P, dst_pix_fmt = AV_PIX_FMT_RGB24;
  51. const char *dst_size = NULL;
  52. const char *dst_filename = NULL;
  53. FILE *dst_file;
  54. int dst_bufsize;
  55. struct SwsContext *sws_ctx;
  56. int i, ret;
  57. if (argc != 3) {
  58. fprintf(stderr, "Usage: %s output_file output_size\n"
  59. "API example program to show how to scale an image with libswscale.\n"
  60. "This program generates a series of pictures, rescales them to the given "
  61. "output_size and saves them to an output file named output_file\n."
  62. "\n", argv[0]);
  63. exit(1);
  64. }
  65. dst_filename = argv[1];
  66. dst_size = argv[2];
  67. if (av_parse_video_size(&dst_w, &dst_h, dst_size) < 0) {
  68. fprintf(stderr,
  69. "Invalid size '%s', must be in the form WxH or a valid size abbreviation\n",
  70. dst_size);
  71. exit(1);
  72. }
  73. dst_file = fopen(dst_filename, "wb");
  74. if (!dst_file) {
  75. fprintf(stderr, "Could not open destination file %s\n", dst_filename);
  76. exit(1);
  77. }
  78. /* create scaling context */
  79. sws_ctx = sws_getContext(src_w, src_h, src_pix_fmt,
  80. dst_w, dst_h, dst_pix_fmt,
  81. SWS_BILINEAR, NULL, NULL, NULL);
  82. if (!sws_ctx) {
  83. fprintf(stderr,
  84. "Impossible to create scale context for the conversion "
  85. "fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
  86. av_get_pix_fmt_name(src_pix_fmt), src_w, src_h,
  87. av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h);
  88. ret = AVERROR(EINVAL);
  89. goto end;
  90. }
  91. /* allocate source and destination image buffers */
  92. if ((ret = av_image_alloc(src_data, src_linesize,
  93. src_w, src_h, src_pix_fmt, 16)) < 0) {
  94. fprintf(stderr, "Could not allocate source image\n");
  95. goto end;
  96. }
  97. /* buffer is going to be written to rawvideo file, no alignmnet */
  98. if ((ret = av_image_alloc(dst_data, dst_linesize,
  99. dst_w, dst_h, dst_pix_fmt, 1)) < 0) {
  100. fprintf(stderr, "Could not allocate destination image\n");
  101. goto end;
  102. }
  103. dst_bufsize = ret;
  104. for (i = 0; i < 100; i++) {
  105. /* generate synthetic video */
  106. fill_yuv_image(src_data, src_linesize, src_w, src_h, i);
  107. /* convert to destination format */
  108. sws_scale(sws_ctx, (const uint8_t * const*)src_data,
  109. src_linesize, 0, src_h, dst_data, dst_linesize);
  110. /* write scaled image to file */
  111. fwrite(dst_data[0], 1, dst_bufsize, dst_file);
  112. }
  113. fprintf(stderr, "Scaling succeeded. Play the output file with the command:\n"
  114. "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
  115. av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h, dst_filename);
  116. end:
  117. if (dst_file)
  118. fclose(dst_file);
  119. av_freep(&src_data[0]);
  120. av_freep(&dst_data[0]);
  121. sws_freeContext(sws_ctx);
  122. return ret < 0;
  123. }