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