FFmpeg
4.4.5
scaling_video.c
/*
* Copyright (c) 2012 Stefano Sabatini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* @file
* libswscale API use example.
* @example scaling_video.c
*/
#include <
libavutil/imgutils.h
>
#include <
libavutil/parseutils.h
>
#include <
libswscale/swscale.h
>
static
void
fill_yuv_image
(
uint8_t
*
data
[4],
int
linesize[4],
int
width
,
int
height
,
int
frame_index)
{
int
x, y;
/* Y */
for
(y = 0; y <
height
; y++)
for
(x = 0; x <
width
; x++)
data
[0][y * linesize[0] + x] = x + y + frame_index * 3;
/* Cb and Cr */
for
(y = 0; y <
height
/ 2; y++) {
for
(x = 0; x <
width
/ 2; x++) {
data
[1][y * linesize[1] + x] = 128 + y + frame_index * 2;
data
[2][y * linesize[2] + x] = 64 + x + frame_index * 5;
}
}
}
int
main
(
int
argc,
char
**argv)
{
uint8_t
*src_data[4], *dst_data[4];
int
src_linesize[4], dst_linesize[4];
int
src_w = 320, src_h = 240, dst_w, dst_h;
enum
AVPixelFormat
src_pix_fmt =
AV_PIX_FMT_YUV420P
, dst_pix_fmt =
AV_PIX_FMT_RGB24
;
const
char
*dst_size =
NULL
;
const
char
*dst_filename =
NULL
;
FILE *dst_file;
int
dst_bufsize;
struct
SwsContext
*sws_ctx;
int
i
, ret;
if
(argc != 3) {
fprintf(stderr,
"Usage: %s output_file output_size\n"
"API example program to show how to scale an image with libswscale.\n"
"This program generates a series of pictures, rescales them to the given "
"output_size and saves them to an output file named output_file\n."
"\n"
, argv[0]);
exit(1);
}
dst_filename = argv[1];
dst_size = argv[2];
if
(
av_parse_video_size
(&dst_w, &dst_h, dst_size) < 0) {
fprintf(stderr,
"Invalid size '%s', must be in the form WxH or a valid size abbreviation\n"
,
dst_size);
exit(1);
}
dst_file = fopen(dst_filename,
"wb"
);
if
(!dst_file) {
fprintf(stderr,
"Could not open destination file %s\n"
, dst_filename);
exit(1);
}
/* create scaling context */
sws_ctx =
sws_getContext
(src_w, src_h, src_pix_fmt,
dst_w, dst_h, dst_pix_fmt,
SWS_BILINEAR
,
NULL
,
NULL
,
NULL
);
if
(!sws_ctx) {
fprintf(stderr,
"Impossible to create scale context for the conversion "
"fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n"
,
av_get_pix_fmt_name
(src_pix_fmt), src_w, src_h,
av_get_pix_fmt_name
(dst_pix_fmt), dst_w, dst_h);
ret =
AVERROR
(EINVAL);
goto
end;
}
/* allocate source and destination image buffers */
if
((ret =
av_image_alloc
(src_data, src_linesize,
src_w, src_h, src_pix_fmt, 16)) < 0) {
fprintf(stderr,
"Could not allocate source image\n"
);
goto
end;
}
/* buffer is going to be written to rawvideo file, no alignment */
if
((ret =
av_image_alloc
(dst_data, dst_linesize,
dst_w, dst_h, dst_pix_fmt, 1)) < 0) {
fprintf(stderr,
"Could not allocate destination image\n"
);
goto
end;
}
dst_bufsize = ret;
for
(
i
= 0;
i
< 100;
i
++) {
/* generate synthetic video */
fill_yuv_image
(src_data, src_linesize, src_w, src_h,
i
);
/* convert to destination format */
sws_scale
(sws_ctx, (
const
uint8_t
*
const
*)src_data,
src_linesize, 0, src_h, dst_data, dst_linesize);
/* write scaled image to file */
fwrite(dst_data[0], 1, dst_bufsize, dst_file);
}
fprintf(stderr,
"Scaling succeeded. Play the output file with the command:\n"
"ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n"
,
av_get_pix_fmt_name
(dst_pix_fmt), dst_w, dst_h, dst_filename);
end:
fclose(dst_file);
av_freep
(&src_data[0]);
av_freep
(&dst_data[0]);
sws_freeContext
(sws_ctx);
return
ret < 0;
}
uint8_t
uint8_t
Definition:
audio_convert.c:194
NULL
#define NULL
Definition:
coverity.c:32
AVERROR
#define AVERROR(e)
Definition:
error.h:43
av_image_alloc
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...
Definition:
imgutils.c:216
SWS_BILINEAR
#define SWS_BILINEAR
Definition:
swscale.h:59
sws_freeContext
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
Definition:
utils.c:2337
sws_scale
int attribute_align_arg sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int srcStride[], int srcSliceY, int srcSliceH, uint8_t *const dst[], const int dstStride[])
swscale wrapper, so we don't need to export the SwsContext.
Definition:
swscale.c:745
sws_getContext
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.
Definition:
utils.c:1917
imgutils.h
misc image utilities
i
int i
Definition:
input.c:407
data
const char data[16]
Definition:
mxf.c:142
av_parse_video_size
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.
Definition:
parseutils.c:148
parseutils.h
misc parsing utilities
av_get_pix_fmt_name
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.
Definition:
pixdesc.c:2489
AVPixelFormat
AVPixelFormat
Pixel format.
Definition:
pixfmt.h:64
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition:
pixfmt.h:68
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition:
pixfmt.h:66
main
int main(int argc, char **argv)
Definition:
scaling_video.c:52
fill_yuv_image
static void fill_yuv_image(uint8_t *data[4], int linesize[4], int width, int height, int frame_index)
Definition:
scaling_video.c:33
SwsContext
Definition:
swscale_internal.h:283
swscale.h
external API header
av_freep
#define av_freep(p)
Definition:
tableprint_vlc.h:35
height
#define height
width
#define width
Generated on Sun Apr 20 2025 14:57:21 for FFmpeg by
1.9.1