]> git.sesse.net Git - ffmpeg/blob - libavfilter/lavfutils.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / lavfutils.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "libavutil/imgutils.h"
20 #include "lavfutils.h"
21
22 int ff_load_image(uint8_t *data[4], int linesize[4],
23                   int *w, int *h, enum PixelFormat *pix_fmt,
24                   const char *filename, void *log_ctx)
25 {
26     AVInputFormat *iformat = NULL;
27     AVFormatContext *format_ctx;
28     AVCodec *codec;
29     AVCodecContext *codec_ctx;
30     AVFrame *frame;
31     int frame_decoded, ret = 0;
32     AVPacket pkt;
33
34     av_register_all();
35
36     iformat = av_find_input_format("image2");
37     if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) {
38         av_log(log_ctx, AV_LOG_ERROR,
39                "Failed to open input file '%s'\n", filename);
40         return ret;
41     }
42
43     codec_ctx = format_ctx->streams[0]->codec;
44     codec = avcodec_find_decoder(codec_ctx->codec_id);
45     if (!codec) {
46         av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
47         ret = AVERROR(EINVAL);
48         goto end;
49     }
50
51     if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
52         av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
53         goto end;
54     }
55
56     if (!(frame = avcodec_alloc_frame()) ) {
57         av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
58         ret = AVERROR(ENOMEM);
59         goto end;
60     }
61
62     ret = av_read_frame(format_ctx, &pkt);
63     if (ret < 0) {
64         av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
65         goto end;
66     }
67
68     ret = avcodec_decode_video2(codec_ctx, frame, &frame_decoded, &pkt);
69     if (ret < 0 || !frame_decoded) {
70         av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n");
71         goto end;
72     }
73     ret = 0;
74
75     *w       = frame->width;
76     *h       = frame->height;
77     *pix_fmt = frame->format;
78
79     if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
80         goto end;
81     ret = 0;
82
83     av_image_copy(data, linesize, frame->data, frame->linesize, *pix_fmt, *w, *h);
84
85 end:
86     if (codec_ctx)
87         avcodec_close(codec_ctx);
88     if (format_ctx)
89         avformat_close_input(&format_ctx);
90     av_freep(&frame);
91
92     if (ret < 0)
93         av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);
94     return ret;
95 }