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
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  */
21 #include "libavutil/imgutils.h"
22 #include "libavformat/avformat.h"
23 #include "lavfutils.h"
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 {
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;
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  }
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  }
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  }
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  }
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  }
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  }
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  }
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  }
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  }
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  }
103  *w = frame->width;
104  *h = frame->height;
105  *pix_fmt = frame->format;
107  if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
108  goto end;
109  ret = 0;
111  av_image_copy(data, linesize, (const uint8_t **)frame->data, frame->linesize, *pix_fmt, *w, *h);
113 end:
114  avcodec_free_context(&codec_ctx);
115  avformat_close_input(&format_ctx);
116  av_frame_free(&frame);
117  av_dict_free(&opt);
119  if (ret < 0)
120  av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);
121  return ret;
122 }
#define NULL
Definition: coverity.c:32
static enum AVPixelFormat pix_fmt
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
misc image utilities
int av_image_alloc(uint8_t *pointers[4], int linesizes[4], int w, int h, enum AVPixelFormat pix_fmt, int align)
Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...
Definition: imgutils.c:216
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:505
static AVPacket pkt
Definition: codec.h:190
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
Format I/O context.
Definition: avformat.h:1243
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1311
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
Definition: utils.c:2184
#define av_log(a,...)
int width
Definition: frame.h:372
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder.
Definition: decode.c:624
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
ff_const59 AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition: format.c:118
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)
Copy image in src_data to dst_data.
Definition: imgutils.c:422
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:172
uint8_t w
Definition: llviddspenc.c:39
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
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)
Load image from filename and put the resulting image in data.
Definition: lavfutils.c:25
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:387
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:561
static AVInputFormat * iformat
Definition: ffprobe.c:260
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
Definition: options.c:187
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:345
main external API structure.
Definition: avcodec.h:531
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:606
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:545
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1719
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:945
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:328
Main libavformat public API header.
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:3578
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4453
int height
Definition: frame.h:372
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1049
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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.Frame references ownership and permissions
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: packet.h:340
Miscellaneous utilities which make use of the libavformat library.