2 * Copyright 2012 Stefano Sabatini <stefasab gmail com>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/imgutils.h"
22 #include "lavfutils.h"
24 int ff_load_image(uint8_t *data[4], int linesize[4],
25 int *w, int *h, enum PixelFormat *pix_fmt,
26 const char *filename, void *log_ctx)
28 AVInputFormat *iformat = NULL;
29 AVFormatContext *format_ctx = NULL;
31 AVCodecContext *codec_ctx;
33 int frame_decoded, ret = 0;
38 iformat = av_find_input_format("image2");
39 if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) {
40 av_log(log_ctx, AV_LOG_ERROR,
41 "Failed to open input file '%s'\n", filename);
45 codec_ctx = format_ctx->streams[0]->codec;
46 codec = avcodec_find_decoder(codec_ctx->codec_id);
48 av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
49 ret = AVERROR(EINVAL);
53 if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
54 av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
58 if (!(frame = avcodec_alloc_frame()) ) {
59 av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
60 ret = AVERROR(ENOMEM);
64 ret = av_read_frame(format_ctx, &pkt);
66 av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
70 ret = avcodec_decode_video2(codec_ctx, frame, &frame_decoded, &pkt);
71 if (ret < 0 || !frame_decoded) {
72 av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n");
79 *pix_fmt = frame->format;
81 if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
85 av_image_copy(data, linesize, (const uint8_t **)frame->data, frame->linesize, *pix_fmt, *w, *h);
89 avcodec_close(codec_ctx);
91 avformat_close_input(&format_ctx);
95 av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);