• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "libavformat/avformat.h"
23 #include "lavfutils.h"
24 
ff_load_image(uint8_t * data[4],int linesize[4],int * w,int * h,enum AVPixelFormat * pix_fmt,const char * filename,void * log_ctx)25 int ff_load_image(uint8_t *data[4], int linesize[4],
26                   int *w, int *h, enum AVPixelFormat *pix_fmt,
27                   const char *filename, void *log_ctx)
28 {
29     AVInputFormat *iformat = NULL;
30     AVFormatContext *format_ctx = NULL;
31     const AVCodec *codec;
32     AVCodecContext *codec_ctx = NULL;
33     AVCodecParameters *par;
34     AVFrame *frame = NULL;
35     int ret = 0;
36     AVPacket pkt;
37     AVDictionary *opt=NULL;
38 
39     iformat = av_find_input_format("image2pipe");
40     if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) {
41         av_log(log_ctx, AV_LOG_ERROR,
42                "Failed to open input file '%s'\n", filename);
43         return ret;
44     }
45 
46     if ((ret = avformat_find_stream_info(format_ctx, NULL)) < 0) {
47         av_log(log_ctx, AV_LOG_ERROR, "Find stream info failed\n");
48         goto end;
49     }
50 
51     par = format_ctx->streams[0]->codecpar;
52     codec = avcodec_find_decoder(par->codec_id);
53     if (!codec) {
54         av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
55         ret = AVERROR(EINVAL);
56         goto end;
57     }
58 
59     codec_ctx = avcodec_alloc_context3(codec);
60     if (!codec_ctx) {
61         av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc video decoder context\n");
62         ret = AVERROR(ENOMEM);
63         goto end;
64     }
65 
66     ret = avcodec_parameters_to_context(codec_ctx, par);
67     if (ret < 0) {
68         av_log(log_ctx, AV_LOG_ERROR, "Failed to copy codec parameters to decoder context\n");
69         goto end;
70     }
71 
72     av_dict_set(&opt, "thread_type", "slice", 0);
73     if ((ret = avcodec_open2(codec_ctx, codec, &opt)) < 0) {
74         av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
75         goto end;
76     }
77 
78     if (!(frame = av_frame_alloc()) ) {
79         av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
80         ret = AVERROR(ENOMEM);
81         goto end;
82     }
83 
84     ret = av_read_frame(format_ctx, &pkt);
85     if (ret < 0) {
86         av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
87         goto end;
88     }
89 
90     ret = avcodec_send_packet(codec_ctx, &pkt);
91     av_packet_unref(&pkt);
92     if (ret < 0) {
93         av_log(log_ctx, AV_LOG_ERROR, "Error submitting a packet to decoder\n");
94         goto end;
95     }
96 
97     ret = avcodec_receive_frame(codec_ctx, frame);
98     if (ret < 0) {
99         av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n");
100         goto end;
101     }
102 
103     *w       = frame->width;
104     *h       = frame->height;
105     *pix_fmt = frame->format;
106 
107     if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
108         goto end;
109     ret = 0;
110 
111     av_image_copy(data, linesize, (const uint8_t **)frame->data, frame->linesize, *pix_fmt, *w, *h);
112 
113 end:
114     avcodec_free_context(&codec_ctx);
115     avformat_close_input(&format_ctx);
116     av_frame_free(&frame);
117     av_dict_free(&opt);
118 
119     if (ret < 0)
120         av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);
121     return ret;
122 }
123