lavfutils.c
Go to the documentation of this file.
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 {
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 
37 
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);
42  return ret;
43  }
44 
45  codec_ctx = format_ctx->streams[0]->codec;
46  codec = avcodec_find_decoder(codec_ctx->codec_id);
47  if (!codec) {
48  av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
49  ret = AVERROR(EINVAL);
50  goto end;
51  }
52 
53  if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
54  av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
55  goto end;
56  }
57 
58  if (!(frame = avcodec_alloc_frame()) ) {
59  av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
60  ret = AVERROR(ENOMEM);
61  goto end;
62  }
63 
64  ret = av_read_frame(format_ctx, &pkt);
65  if (ret < 0) {
66  av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
67  goto end;
68  }
69 
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");
73  goto end;
74  }
75  ret = 0;
76 
77  *w = frame->width;
78  *h = frame->height;
79  *pix_fmt = frame->format;
80 
81  if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
82  goto end;
83  ret = 0;
84 
85  av_image_copy(data, linesize, (const uint8_t **)frame->data, frame->linesize, *pix_fmt, *w, *h);
86 
87 end:
88  avcodec_close(codec_ctx);
89  avformat_close_input(&format_ctx);
90  av_freep(&frame);
91 
92  if (ret < 0)
93  av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);
94  return ret;
95 }
Definition: frame.h:76
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
int av_image_alloc(uint8_t *pointers[4], int linesizes[4], int w, int h, enum AVPixelFormat pix_fmt, int align)
Definition: imgutils.c:190
output residual component w
void av_freep(void *arg)
Definition: mem.c:198
uint8_t
static AVPacket pkt
Definition: demuxing.c:56
end end
AVStream ** streams
Definition: avformat.h:992
enum AVPixelFormat pix_fmt
Definition: v4l.c:63
frame
Definition: stft.m:14
int avcodec_close(AVCodecContext *avctx)
int width
Definition: frame.h:122
int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, const AVPacket *avpkt)
Spectrum Plot time data
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Definition: imgutils.c:257
AVCodecContext * codec
Definition: avformat.h:662
AVFrame * avcodec_alloc_frame(void)
AVInputFormat * av_find_input_format(const char *short_name)
ret
Definition: avfilter.c:821
int ff_load_image(uint8_t *data[4], int linesize[4], int *w, int *h, enum AVPixelFormat *pix_fmt, const char *filename, void *log_ctx)
Definition: lavfutils.c:24
int format
Definition: frame.h:134
NULL
Definition: eval.c:55
static AVInputFormat * iformat
Definition: ffprobe.c:141
enum AVCodecID codec_id
int linesize[AV_NUM_DATA_POINTERS]
Definition: frame.h:101
AVCodec * avcodec_find_decoder(enum AVCodecID id)
#define AV_LOG_ERROR
Definition: log.h:148
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition: frame.h:87
void avformat_close_input(AVFormatContext **s)
int height
Definition: frame.h:122
AVPixelFormat
Definition: pixfmt.h:66
void av_register_all(void)
Definition: allformats.c:52