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