| 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
|
|---|
| 25 | * libswscale API use example.
|
|---|
| 26 | * @example scaling_video.c
|
|---|
| 27 | */
|
|---|
| 28 | extern "C" {
|
|---|
| 29 | #ifdef __cplusplus
|
|---|
| 30 | #define __STDC_CONSTANT_MACROS
|
|---|
| 31 |
|
|---|
| 32 | #endif
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | #include <libavutil/imgutils.h>
|
|---|
| 36 | #include <libavutil/parseutils.h>
|
|---|
| 37 | #include <libswscale/swscale.h>
|
|---|
| 38 |
|
|---|
| 39 | }
|
|---|
| 40 | static void fill_yuv_image(uint8_t *data[4], int linesize[4],
|
|---|
| 41 | int width, int height, int frame_index)
|
|---|
| 42 | {
|
|---|
| 43 | int x, y;
|
|---|
| 44 |
|
|---|
| 45 | /* Y */
|
|---|
| 46 | for (y = 0; y < height; y++)
|
|---|
| 47 | for (x = 0; x < width; x++)
|
|---|
| 48 | data[0][y * linesize[0] + x] = x + y + frame_index * 3;
|
|---|
| 49 |
|
|---|
| 50 | /* Cb and Cr */
|
|---|
| 51 | for (y = 0; y < height / 2; y++) {
|
|---|
| 52 | for (x = 0; x < width / 2; x++) {
|
|---|
| 53 | data[1][y * linesize[1] + x] = 128 + y + frame_index * 2;
|
|---|
| 54 | //data[2][y * linesize[2] + x] = 64 + x + frame_index * 5;
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | int main(int argc, char **argv)
|
|---|
| 60 | {
|
|---|
| 61 | uint8_t *src_data[4], *dst_data[4];
|
|---|
| 62 | int src_linesize[4], dst_linesize[4];
|
|---|
| 63 | int src_w = 1920, src_h = 1080, dst_w, dst_h;
|
|---|
| 64 | enum AVPixelFormat src_pix_fmt = AV_PIX_FMT_NV12, dst_pix_fmt = AV_PIX_FMT_NV12;
|
|---|
| 65 | const char *dst_size = NULL;
|
|---|
| 66 | const char *dst_filename = NULL;
|
|---|
| 67 | const char *src_filename = NULL;
|
|---|
| 68 | FILE *dst_file,*src_file;
|
|---|
| 69 | int dst_bufsize;
|
|---|
| 70 | struct SwsContext *sws_ctx;
|
|---|
| 71 | int i, ret;
|
|---|
| 72 |
|
|---|
| 73 | //if (argc != 3) {
|
|---|
| 74 | // fprintf(stderr, "Usage: %s output_file output_size\n"
|
|---|
| 75 | // "API example program to show how to scale an image with libswscale.\n"
|
|---|
| 76 | // "This program generates a series of pictures, rescales them to the given "
|
|---|
| 77 | // "output_size and saves them to an output file named output_file\n."
|
|---|
| 78 | // "\n", argv[0]);
|
|---|
| 79 | // exit(1);
|
|---|
| 80 | //}
|
|---|
| 81 | dst_filename = "output.yuv";
|
|---|
| 82 | src_filename = "input.yuv";
|
|---|
| 83 | dst_size = "1920x1080";
|
|---|
| 84 |
|
|---|
| 85 | if (av_parse_video_size(&dst_w, &dst_h, dst_size) < 0) {
|
|---|
| 86 | fprintf(stderr,
|
|---|
| 87 | "Invalid size '%s', must be in the form WxH or a valid size abbreviation\n",
|
|---|
| 88 | dst_size);
|
|---|
| 89 | exit(1);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | dst_file = fopen(dst_filename, "wb");
|
|---|
| 93 | src_file = fopen(src_filename, "wb");
|
|---|
| 94 | if (!dst_file) {
|
|---|
| 95 | fprintf(stderr, "Could not open destination file %s\n", dst_filename);
|
|---|
| 96 | exit(1);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /* create scaling context */
|
|---|
| 100 | sws_ctx = sws_getContext(src_w, src_h, src_pix_fmt,
|
|---|
| 101 | dst_w, dst_h, dst_pix_fmt,
|
|---|
| 102 | SWS_BILINEAR, NULL, NULL, NULL);
|
|---|
| 103 | if (!sws_ctx) {
|
|---|
| 104 | fprintf(stderr,
|
|---|
| 105 | "Impossible to create scale context for the conversion "
|
|---|
| 106 | "fmt:%s s:%dx%d -> fmt:%s s:%dx%d\n",
|
|---|
| 107 | av_get_pix_fmt_name(src_pix_fmt), src_w, src_h,
|
|---|
| 108 | av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h);
|
|---|
| 109 | ret = AVERROR(EINVAL);
|
|---|
| 110 | goto end;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /* allocate source and destination image buffers */
|
|---|
| 114 | if ((ret = av_image_alloc(src_data, src_linesize,
|
|---|
| 115 | src_w, src_h, src_pix_fmt, 16)) < 0) {
|
|---|
| 116 | fprintf(stderr, "Could not allocate source image\n");
|
|---|
| 117 | goto end;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /* buffer is going to be written to rawvideo file, no alignment */
|
|---|
| 121 | if ((ret = av_image_alloc(dst_data, dst_linesize,
|
|---|
| 122 | dst_w, dst_h, dst_pix_fmt, 1)) < 0) {
|
|---|
| 123 | fprintf(stderr, "Could not allocate destination image\n");
|
|---|
| 124 | goto end;
|
|---|
| 125 | }
|
|---|
| 126 | dst_bufsize = ret;
|
|---|
| 127 |
|
|---|
| 128 | for (i = 0; i < 1; i++) {
|
|---|
| 129 | /* generate synthetic video */
|
|---|
| 130 | fill_yuv_image(src_data, src_linesize, src_w, src_h, i);
|
|---|
| 131 |
|
|---|
| 132 | /* convert to destination format */
|
|---|
| 133 | sws_scale(sws_ctx, (const uint8_t * const*)src_data,
|
|---|
| 134 | src_linesize, 0, src_h, dst_data, dst_linesize);
|
|---|
| 135 |
|
|---|
| 136 | /* write scaled image to file */
|
|---|
| 137 | fwrite(src_data[0], 1, dst_bufsize, src_file);
|
|---|
| 138 | fwrite(dst_data[0], 1, dst_bufsize, dst_file);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | fprintf(stderr, "Scaling succeeded. Play the output file with the command:\n"
|
|---|
| 142 | "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
|
|---|
| 143 | av_get_pix_fmt_name(dst_pix_fmt), dst_w, dst_h, dst_filename);
|
|---|
| 144 |
|
|---|
| 145 | end:
|
|---|
| 146 | fclose(dst_file);
|
|---|
| 147 | fclose(src_file);
|
|---|
| 148 | av_freep(&src_data[0]);
|
|---|
| 149 | av_freep(&dst_data[0]);
|
|---|
| 150 | sws_freeContext(sws_ctx);
|
|---|
| 151 | return ret < 0;
|
|---|
| 152 | }
|
|---|