FFmpeg 7.1.1
Loading...
Searching...
No Matches
scale_video.c
Go to the documentation of this file.
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/**
24 * @file libswscale API usage example
25 * @example scale_video.c
26 *
27 * Generate a synthetic video signal and use libswscale to perform rescaling.
28 */
29
30#include <libavutil/imgutils.h>
32#include <libswscale/swscale.h>
33
34static void fill_yuv_image(uint8_t *data[4], int linesize[4],
35 int width, int height, int frame_index)
36{
37 int x, y;
38
39 /* Y */
40 for (y = 0; y < height; y++)
41 for (x = 0; x < width; x++)
42 data[0][y * linesize[0] + x] = x + y + frame_index * 3;
43
44 /* Cb and Cr */
45 for (y = 0; y < height / 2; y++) {
46 for (x = 0; x < width / 2; x++) {
47 data[1][y * linesize[1] + x] = 128 + y + frame_index * 2;
48 data[2][y * linesize[2] + x] = 64 + x + frame_index * 5;
49 }
50 }
51}
52
53int main(int argc, char **argv)
54{
55 uint8_t *src_data[4], *dst_data[4];
56 int src_linesize[4], dst_linesize[4];
57 int src_w = 320, src_h = 240, dst_w, dst_h;
58 enum AVPixelFormat src_pix_fmt = AV_PIX_FMT_YUV420P, dst_pix_fmt = AV_PIX_FMT_RGB24;
59 const char *dst_size = NULL;
60 const char *dst_filename = NULL;
61 FILE *dst_file;
62 int dst_bufsize;
63 struct SwsContext *sws_ctx;
64 int i, ret;
65
66 if (argc != 3) {
67 fprintf(stderr, "Usage: %s output_file output_size\n"
68 "API example program to show how to scale an image with libswscale.\n"
69 "This program generates a series of pictures, rescales them to the given "
70 "output_size and saves them to an output file named output_file\n."
71 "\n", argv[0]);
72 exit(1);
73 }
74 dst_filename = argv[1];
75 dst_size = argv[2];
76
77 if (av_parse_video_size(&dst_w, &dst_h, dst_size) < 0) {
78 fprintf(stderr,
79 "Invalid size '%s', must be in the form WxH or a valid size abbreviation\n",
80 dst_size);
81 exit(1);
82 }
83
84 dst_file = fopen(dst_filename, "wb");
85 if (!dst_file) {
86 fprintf(stderr, "Could not open destination file %s\n", dst_filename);
87 exit(1);
88 }
89
90 /* create scaling context */
91 sws_ctx = sws_getContext(src_w, src_h, src_pix_fmt,
92 dst_w, dst_h, dst_pix_fmt,
93 SWS_BILINEAR, NULL, NULL, NULL);
94 if (!sws_ctx) {
95 fprintf(stderr,
96 "Impossible to create scale context for the conversion "
97 "fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
98 av_get_pix_fmt_name(src_pix_fmt), src_w, src_h,
99 av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h);
100 ret = AVERROR(EINVAL);
101 goto end;
102 }
103
104 /* allocate source and destination image buffers */
105 if ((ret = av_image_alloc(src_data, src_linesize,
106 src_w, src_h, src_pix_fmt, 16)) < 0) {
107 fprintf(stderr, "Could not allocate source image\n");
108 goto end;
109 }
110
111 /* buffer is going to be written to rawvideo file, no alignment */
112 if ((ret = av_image_alloc(dst_data, dst_linesize,
113 dst_w, dst_h, dst_pix_fmt, 1)) < 0) {
114 fprintf(stderr, "Could not allocate destination image\n");
115 goto end;
116 }
117 dst_bufsize = ret;
118
119 for (i = 0; i < 100; i++) {
120 /* generate synthetic video */
121 fill_yuv_image(src_data, src_linesize, src_w, src_h, i);
122
123 /* convert to destination format */
124 sws_scale(sws_ctx, (const uint8_t * const*)src_data,
125 src_linesize, 0, src_h, dst_data, dst_linesize);
126
127 /* write scaled image to file */
128 fwrite(dst_data[0], 1, dst_bufsize, dst_file);
129 }
130
131 fprintf(stderr, "Scaling succeeded. Play the output file with the command:\n"
132 "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
133 av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h, dst_filename);
134
135end:
136 fclose(dst_file);
137 av_freep(&src_data[0]);
138 av_freep(&dst_data[0]);
139 sws_freeContext(sws_ctx);
140 return ret < 0;
141}
int main(int argc, char *argv[])
static int width
static int height
#define AVERROR(e)
Definition error.h:45
void av_freep(void *ptr)
Free a memory block which has been allocated with a function of av_malloc() or av_realloc() family,...
int av_image_alloc(uint8_t *pointers[4], int linesizes[4], int w, int h, enum AVPixelFormat pix_fmt, int align)
Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...
#define SWS_BILINEAR
Definition swscale.h:66
struct SwsContext * sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, enum AVPixelFormat dstFormat, int flags, SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
Allocate and return an SwsContext.
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
Scale the image slice in srcSlice and put the resulting scaled slice in the image in dst.
misc image utilities
static void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
Definition mux.c:449
misc parsing utilities
int av_parse_video_size(int *width_ptr, int *height_ptr, const char *str)
Parse str and put in width_ptr and height_ptr the detected values.
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
external API header