blob: 706badf63d363d5e473c279bb8617eca11fffcd6
1 | /* |
2 | * Copyright 2012 Stefano Sabatini <stefasab gmail com> |
3 | * |
4 | * This file is part of FFmpeg. |
5 | * |
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. |
10 | * |
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. |
15 | * |
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 |
19 | */ |
20 | |
21 | #include "libavutil/imgutils.h" |
22 | #include "lavfutils.h" |
23 | |
24 | int ff_load_image(uint8_t *data[4], int linesize[4], |
25 | int *w, int *h, enum AVPixelFormat *pix_fmt, |
26 | const char *filename, void *log_ctx) |
27 | { |
28 | AVInputFormat *iformat = NULL; |
29 | AVFormatContext *format_ctx = NULL; |
30 | AVCodec *codec; |
31 | AVCodecContext *codec_ctx; |
32 | AVFrame *frame; |
33 | int frame_decoded, ret = 0; |
34 | AVPacket pkt; |
35 | AVDictionary *opt=NULL; |
36 | |
37 | av_init_packet(&pkt); |
38 | |
39 | av_register_all(); |
40 | |
41 | iformat = av_find_input_format("image2"); |
42 | if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) { |
43 | av_log(log_ctx, AV_LOG_ERROR, |
44 | "Failed to open input file '%s'\n", filename); |
45 | return ret; |
46 | } |
47 | |
48 | if ((ret = avformat_find_stream_info(format_ctx, NULL)) < 0) { |
49 | av_log(log_ctx, AV_LOG_ERROR, "Find stream info failed\n"); |
50 | return ret; |
51 | } |
52 | |
53 | codec_ctx = format_ctx->streams[0]->codec; |
54 | codec = avcodec_find_decoder(codec_ctx->codec_id); |
55 | if (!codec) { |
56 | av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n"); |
57 | ret = AVERROR(EINVAL); |
58 | goto end; |
59 | } |
60 | |
61 | av_dict_set(&opt, "thread_type", "slice", 0); |
62 | if ((ret = avcodec_open2(codec_ctx, codec, &opt)) < 0) { |
63 | av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n"); |
64 | goto end; |
65 | } |
66 | |
67 | if (!(frame = av_frame_alloc()) ) { |
68 | av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n"); |
69 | ret = AVERROR(ENOMEM); |
70 | goto end; |
71 | } |
72 | |
73 | ret = av_read_frame(format_ctx, &pkt); |
74 | if (ret < 0) { |
75 | av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n"); |
76 | goto end; |
77 | } |
78 | |
79 | ret = avcodec_decode_video2(codec_ctx, frame, &frame_decoded, &pkt); |
80 | if (ret < 0 || !frame_decoded) { |
81 | av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n"); |
82 | if (ret >= 0) |
83 | ret = -1; |
84 | goto end; |
85 | } |
86 | |
87 | *w = frame->width; |
88 | *h = frame->height; |
89 | *pix_fmt = frame->format; |
90 | |
91 | if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0) |
92 | goto end; |
93 | ret = 0; |
94 | |
95 | av_image_copy(data, linesize, (const uint8_t **)frame->data, frame->linesize, *pix_fmt, *w, *h); |
96 | |
97 | end: |
98 | av_packet_unref(&pkt); |
99 | avcodec_close(codec_ctx); |
100 | avformat_close_input(&format_ctx); |
101 | av_frame_free(&frame); |
102 | av_dict_free(&opt); |
103 | |
104 | if (ret < 0) |
105 | av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename); |
106 | return ret; |
107 | } |
108 |