scale_video.c 5.0 KB

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