annotate ffmpeg/doc/examples/scaling_video.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Copyright (c) 2012 Stefano Sabatini
yading@10 3 *
yading@10 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
yading@10 5 * of this software and associated documentation files (the "Software"), to deal
yading@10 6 * in the Software without restriction, including without limitation the rights
yading@10 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yading@10 8 * copies of the Software, and to permit persons to whom the Software is
yading@10 9 * furnished to do so, subject to the following conditions:
yading@10 10 *
yading@10 11 * The above copyright notice and this permission notice shall be included in
yading@10 12 * all copies or substantial portions of the Software.
yading@10 13 *
yading@10 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yading@10 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yading@10 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
yading@10 17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yading@10 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yading@10 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
yading@10 20 * THE SOFTWARE.
yading@10 21 */
yading@10 22
yading@10 23 /**
yading@10 24 * @file
yading@10 25 * libswscale API use example.
yading@10 26 * @example doc/examples/scaling_video.c
yading@10 27 */
yading@10 28
yading@10 29 #include <libavutil/imgutils.h>
yading@10 30 #include <libavutil/parseutils.h>
yading@10 31 #include <libswscale/swscale.h>
yading@10 32
yading@10 33 static void fill_yuv_image(uint8_t *data[4], int linesize[4],
yading@10 34 int width, int height, int frame_index)
yading@10 35 {
yading@10 36 int x, y;
yading@10 37
yading@10 38 /* Y */
yading@10 39 for (y = 0; y < height; y++)
yading@10 40 for (x = 0; x < width; x++)
yading@10 41 data[0][y * linesize[0] + x] = x + y + frame_index * 3;
yading@10 42
yading@10 43 /* Cb and Cr */
yading@10 44 for (y = 0; y < height / 2; y++) {
yading@10 45 for (x = 0; x < width / 2; x++) {
yading@10 46 data[1][y * linesize[1] + x] = 128 + y + frame_index * 2;
yading@10 47 data[2][y * linesize[2] + x] = 64 + x + frame_index * 5;
yading@10 48 }
yading@10 49 }
yading@10 50 }
yading@10 51
yading@10 52 int main(int argc, char **argv)
yading@10 53 {
yading@10 54 uint8_t *src_data[4], *dst_data[4];
yading@10 55 int src_linesize[4], dst_linesize[4];
yading@10 56 int src_w = 320, src_h = 240, dst_w, dst_h;
yading@10 57 enum AVPixelFormat src_pix_fmt = AV_PIX_FMT_YUV420P, dst_pix_fmt = AV_PIX_FMT_RGB24;
yading@10 58 const char *dst_size = NULL;
yading@10 59 const char *dst_filename = NULL;
yading@10 60 FILE *dst_file;
yading@10 61 int dst_bufsize;
yading@10 62 struct SwsContext *sws_ctx;
yading@10 63 int i, ret;
yading@10 64
yading@10 65 if (argc != 3) {
yading@10 66 fprintf(stderr, "Usage: %s output_file output_size\n"
yading@10 67 "API example program to show how to scale an image with libswscale.\n"
yading@10 68 "This program generates a series of pictures, rescales them to the given "
yading@10 69 "output_size and saves them to an output file named output_file\n."
yading@10 70 "\n", argv[0]);
yading@10 71 exit(1);
yading@10 72 }
yading@10 73 dst_filename = argv[1];
yading@10 74 dst_size = argv[2];
yading@10 75
yading@10 76 if (av_parse_video_size(&dst_w, &dst_h, dst_size) < 0) {
yading@10 77 fprintf(stderr,
yading@10 78 "Invalid size '%s', must be in the form WxH or a valid size abbreviation\n",
yading@10 79 dst_size);
yading@10 80 exit(1);
yading@10 81 }
yading@10 82
yading@10 83 dst_file = fopen(dst_filename, "wb");
yading@10 84 if (!dst_file) {
yading@10 85 fprintf(stderr, "Could not open destination file %s\n", dst_filename);
yading@10 86 exit(1);
yading@10 87 }
yading@10 88
yading@10 89 /* create scaling context */
yading@10 90 sws_ctx = sws_getContext(src_w, src_h, src_pix_fmt,
yading@10 91 dst_w, dst_h, dst_pix_fmt,
yading@10 92 SWS_BILINEAR, NULL, NULL, NULL);
yading@10 93 if (!sws_ctx) {
yading@10 94 fprintf(stderr,
yading@10 95 "Impossible to create scale context for the conversion "
yading@10 96 "fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
yading@10 97 av_get_pix_fmt_name(src_pix_fmt), src_w, src_h,
yading@10 98 av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h);
yading@10 99 ret = AVERROR(EINVAL);
yading@10 100 goto end;
yading@10 101 }
yading@10 102
yading@10 103 /* allocate source and destination image buffers */
yading@10 104 if ((ret = av_image_alloc(src_data, src_linesize,
yading@10 105 src_w, src_h, src_pix_fmt, 16)) < 0) {
yading@10 106 fprintf(stderr, "Could not allocate source image\n");
yading@10 107 goto end;
yading@10 108 }
yading@10 109
yading@10 110 /* buffer is going to be written to rawvideo file, no alignment */
yading@10 111 if ((ret = av_image_alloc(dst_data, dst_linesize,
yading@10 112 dst_w, dst_h, dst_pix_fmt, 1)) < 0) {
yading@10 113 fprintf(stderr, "Could not allocate destination image\n");
yading@10 114 goto end;
yading@10 115 }
yading@10 116 dst_bufsize = ret;
yading@10 117
yading@10 118 for (i = 0; i < 100; i++) {
yading@10 119 /* generate synthetic video */
yading@10 120 fill_yuv_image(src_data, src_linesize, src_w, src_h, i);
yading@10 121
yading@10 122 /* convert to destination format */
yading@10 123 sws_scale(sws_ctx, (const uint8_t * const*)src_data,
yading@10 124 src_linesize, 0, src_h, dst_data, dst_linesize);
yading@10 125
yading@10 126 /* write scaled image to file */
yading@10 127 fwrite(dst_data[0], 1, dst_bufsize, dst_file);
yading@10 128 }
yading@10 129
yading@10 130 fprintf(stderr, "Scaling succeeded. Play the output file with the command:\n"
yading@10 131 "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
yading@10 132 av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h, dst_filename);
yading@10 133
yading@10 134 end:
yading@10 135 if (dst_file)
yading@10 136 fclose(dst_file);
yading@10 137 av_freep(&src_data[0]);
yading@10 138 av_freep(&dst_data[0]);
yading@10 139 sws_freeContext(sws_ctx);
yading@10 140 return ret < 0;
yading@10 141 }