00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "libavutil/imgutils.h"
00022 #include "lavfutils.h"
00023
00024 int ff_load_image(uint8_t *data[4], int linesize[4],
00025 int *w, int *h, enum PixelFormat *pix_fmt,
00026 const char *filename, void *log_ctx)
00027 {
00028 AVInputFormat *iformat = NULL;
00029 AVFormatContext *format_ctx = NULL;
00030 AVCodec *codec;
00031 AVCodecContext *codec_ctx;
00032 AVFrame *frame;
00033 int frame_decoded, ret = 0;
00034 AVPacket pkt;
00035
00036 av_register_all();
00037
00038 iformat = av_find_input_format("image2");
00039 if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) {
00040 av_log(log_ctx, AV_LOG_ERROR,
00041 "Failed to open input file '%s'\n", filename);
00042 return ret;
00043 }
00044
00045 codec_ctx = format_ctx->streams[0]->codec;
00046 codec = avcodec_find_decoder(codec_ctx->codec_id);
00047 if (!codec) {
00048 av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
00049 ret = AVERROR(EINVAL);
00050 goto end;
00051 }
00052
00053 if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
00054 av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
00055 goto end;
00056 }
00057
00058 if (!(frame = avcodec_alloc_frame()) ) {
00059 av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
00060 ret = AVERROR(ENOMEM);
00061 goto end;
00062 }
00063
00064 ret = av_read_frame(format_ctx, &pkt);
00065 if (ret < 0) {
00066 av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
00067 goto end;
00068 }
00069
00070 ret = avcodec_decode_video2(codec_ctx, frame, &frame_decoded, &pkt);
00071 if (ret < 0 || !frame_decoded) {
00072 av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n");
00073 goto end;
00074 }
00075 ret = 0;
00076
00077 *w = frame->width;
00078 *h = frame->height;
00079 *pix_fmt = frame->format;
00080
00081 if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
00082 goto end;
00083 ret = 0;
00084
00085 av_image_copy(data, linesize, frame->data, frame->linesize, *pix_fmt, *w, *h);
00086
00087 end:
00088 if (codec_ctx)
00089 avcodec_close(codec_ctx);
00090 if (format_ctx)
00091 avformat_close_input(&format_ctx);
00092 av_freep(&frame);
00093
00094 if (ret < 0)
00095 av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);
00096 return ret;
00097 }