examples/scaling_video: write to rawvideo file

This is more useful for testing purposes. Also allow to specify the name
of the output file.
This commit is contained in:
Stefano Sabatini 2012-08-30 22:37:44 +02:00
parent 838b1d60a1
commit 3b34cbce19
2 changed files with 28 additions and 26 deletions

View File

@ -30,7 +30,7 @@ muxing: LDLIBS += -lm
all: $(OBJS) $(EXAMPLES)
clean-test:
$(RM) test*.pgm test.h264 test.mp2 test.sw test.mpg outscale*.pgm
$(RM) test*.pgm test.h264 test.mp2 test.sw test.mpg
clean: clean-test
$(RM) $(EXAMPLES) $(OBJS)

View File

@ -50,38 +50,29 @@ static void fill_yuv_image(uint8_t *data[4], int linesize[4],
}
}
static void save_pgm(const char *filename,
const uint8_t *data, int linesize, int width, int height)
{
FILE *f;
int i;
f = fopen(filename, "w");
fprintf(f, "P5\n%d %d\n%d\n", width, height, 255);
for (i = 0; i < height; i++)
fwrite(data + i * linesize, 1, width, f);
fclose(f);
}
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 PixelFormat src_pix_fmt = PIX_FMT_YUV420P, dst_pix_fmt = PIX_FMT_GRAY8;
const char *dst_size = argv[1];
enum PixelFormat src_pix_fmt = PIX_FMT_YUV420P, dst_pix_fmt = 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 != 2) {
fprintf(stderr, "Usage: %s output_size\n"
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 finally saves the rescaled pictures as PGM files named "
"like outscale<frame number>.pgm.\n"
"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,
@ -90,6 +81,12 @@ int main(int argc, char **argv)
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,
@ -111,15 +108,15 @@ int main(int argc, char **argv)
goto end;
}
/* buffer is going to be written to rawvideo file, no alignmnet */
if ((ret = av_image_alloc(dst_data, dst_linesize,
dst_w, dst_h, dst_pix_fmt, 16)) < 0) {
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++) {
char filename[1024];
/* generate synthetic video */
fill_yuv_image(src_data, src_linesize, src_w, src_h, i);
@ -127,12 +124,17 @@ int main(int argc, char **argv)
sws_scale(sws_ctx, (const uint8_t * const*)src_data,
src_linesize, 0, src_h, dst_data, dst_linesize);
/* write Y plane to to output PGM file */
snprintf(filename, sizeof(filename), "outscale%02d.pgm", i);
save_pgm(filename, dst_data[0], dst_linesize[0], dst_w, dst_h);
/* 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:
if (dst_file)
fclose(dst_file);
av_freep(&src_data[0]);
av_freep(&dst_data[0]);
sws_freeContext(sws_ctx);