00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include <libavutil/imgutils.h>
00029 #include <libavutil/parseutils.h>
00030 #include <libswscale/swscale.h>
00031
00032 static void fill_yuv_image(uint8_t *data[4], int linesize[4],
00033 int width, int height, int frame_index)
00034 {
00035 int x, y, i;
00036
00037 i = frame_index;
00038
00039
00040 for (y = 0; y < height; y++)
00041 for (x = 0; x < width; x++)
00042 data[0][y * linesize[0] + x] = x + y + i * 3;
00043
00044
00045 for (y = 0; y < height / 2; y++) {
00046 for (x = 0; x < width / 2; x++) {
00047 data[1][y * linesize[1] + x] = 128 + y + i * 2;
00048 data[2][y * linesize[2] + x] = 64 + x + i * 5;
00049 }
00050 }
00051 }
00052
00053 int main(int argc, char **argv)
00054 {
00055 uint8_t *src_data[4], *dst_data[4];
00056 int src_linesize[4], dst_linesize[4];
00057 int src_w = 320, src_h = 240, dst_w, dst_h;
00058 enum PixelFormat src_pix_fmt = PIX_FMT_YUV420P, dst_pix_fmt = PIX_FMT_RGB24;
00059 const char *dst_size = NULL;
00060 const char *dst_filename = NULL;
00061 FILE *dst_file;
00062 int dst_bufsize;
00063 struct SwsContext *sws_ctx;
00064 int i, ret;
00065
00066 if (argc != 3) {
00067 fprintf(stderr, "Usage: %s output_file output_size\n"
00068 "API example program to show how to scale an image with libswscale.\n"
00069 "This program generates a series of pictures, rescales them to the given "
00070 "output_size and saves them to an output file named output_file\n."
00071 "\n", argv[0]);
00072 exit(1);
00073 }
00074 dst_filename = argv[1];
00075 dst_size = argv[2];
00076
00077 if (av_parse_video_size(&dst_w, &dst_h, dst_size) < 0) {
00078 fprintf(stderr,
00079 "Invalid size '%s', must be in the form WxH or a valid size abbreviation\n",
00080 dst_size);
00081 exit(1);
00082 }
00083
00084 dst_file = fopen(dst_filename, "wb");
00085 if (!dst_file) {
00086 fprintf(stderr, "Could not open destination file %s\n", dst_filename);
00087 exit(1);
00088 }
00089
00090
00091 sws_ctx = sws_getContext(src_w, src_h, src_pix_fmt,
00092 dst_w, dst_h, dst_pix_fmt,
00093 SWS_BILINEAR, NULL, NULL, NULL);
00094 if (!sws_ctx) {
00095 fprintf(stderr,
00096 "Impossible to create scale context for the conversion "
00097 "fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
00098 av_get_pix_fmt_name(src_pix_fmt), src_w, src_h,
00099 av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h);
00100 ret = AVERROR(EINVAL);
00101 goto end;
00102 }
00103
00104
00105 if ((ret = av_image_alloc(src_data, src_linesize,
00106 src_w, src_h, src_pix_fmt, 16)) < 0) {
00107 fprintf(stderr, "Could not allocate source image\n");
00108 goto end;
00109 }
00110
00111
00112 if ((ret = av_image_alloc(dst_data, dst_linesize,
00113 dst_w, dst_h, dst_pix_fmt, 1)) < 0) {
00114 fprintf(stderr, "Could not allocate destination image\n");
00115 goto end;
00116 }
00117 dst_bufsize = ret;
00118
00119 for (i = 0; i < 100; i++) {
00120
00121 fill_yuv_image(src_data, src_linesize, src_w, src_h, i);
00122
00123
00124 sws_scale(sws_ctx, (const uint8_t * const*)src_data,
00125 src_linesize, 0, src_h, dst_data, dst_linesize);
00126
00127
00128 fwrite(dst_data[0], 1, dst_bufsize, dst_file);
00129 }
00130
00131 fprintf(stderr, "Scaling succeeded. Play the output file with the command:\n"
00132 "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
00133 av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h, dst_filename);
00134
00135 end:
00136 if (dst_file)
00137 fclose(dst_file);
00138 av_freep(&src_data[0]);
00139 av_freep(&dst_data[0]);
00140 sws_freeContext(sws_ctx);
00141 return ret < 0;
00142 }