2 * This file is part of FFmpeg.
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.
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.
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
19 #include "libavutil/imgutils.h"
20 #include "lavfutils.h"
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)
26 AVInputFormat *iformat = NULL;
27 AVFormatContext *format_ctx;
29 AVCodecContext *codec_ctx;
31 int frame_decoded, ret = 0;
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);
43 codec_ctx = format_ctx->streams[0]->codec;
44 codec = avcodec_find_decoder(codec_ctx->codec_id);
46 av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
47 ret = AVERROR(EINVAL);
51 if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
52 av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
56 if (!(frame = avcodec_alloc_frame()) ) {
57 av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
58 ret = AVERROR(ENOMEM);
62 ret = av_read_frame(format_ctx, &pkt);
64 av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
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");
77 *pix_fmt = frame->format;
79 if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
83 av_image_copy(data, linesize, frame->data, frame->linesize, *pix_fmt, *w, *h);
87 avcodec_close(codec_ctx);
89 avformat_close_input(&format_ctx);
93 av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);