FFmpeg  2.1.1
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 {
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 
36  av_init_packet(&pkt);
37 
39 
40  iformat = av_find_input_format("image2");
41  if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) {
42  av_log(log_ctx, AV_LOG_ERROR,
43  "Failed to open input file '%s'\n", filename);
44  return ret;
45  }
46 
47  codec_ctx = format_ctx->streams[0]->codec;
48  codec = avcodec_find_decoder(codec_ctx->codec_id);
49  if (!codec) {
50  av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
51  ret = AVERROR(EINVAL);
52  goto end;
53  }
54 
55  if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
56  av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
57  goto end;
58  }
59 
60  if (!(frame = avcodec_alloc_frame()) ) {
61  av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
62  ret = AVERROR(ENOMEM);
63  goto end;
64  }
65 
66  ret = av_read_frame(format_ctx, &pkt);
67  if (ret < 0) {
68  av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
69  goto end;
70  }
71 
72  ret = avcodec_decode_video2(codec_ctx, frame, &frame_decoded, &pkt);
73  if (ret < 0 || !frame_decoded) {
74  av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n");
75  goto end;
76  }
77  ret = 0;
78 
79  *w = frame->width;
80  *h = frame->height;
81  *pix_fmt = frame->format;
82 
83  if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
84  goto end;
85  ret = 0;
86 
87  av_image_copy(data, linesize, (const uint8_t **)frame->data, frame->linesize, *pix_fmt, *w, *h);
88 
89 end:
90  av_free_packet(&pkt);
91  avcodec_close(codec_ctx);
92  avformat_close_input(&format_ctx);
93  av_freep(&frame);
94 
95  if (ret < 0)
96  av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);
97  return ret;
98 }
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:279
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:487
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:190
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:1071
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
AVCodec.
Definition: avcodec.h:2922
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
Format I/O context.
Definition: avformat.h:968
uint8_t
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
const char data[16]
Definition: mxf.c:68
enum AVPixelFormat pix_fmt
Definition: v4l.c:62
static AVFrame * frame
Definition: demuxing.c:51
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
Definition: utils.c:2505
int width
width and height of the video frame
Definition: frame.h:145
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, const AVPacket *avpkt)
Decode the video frame of size avpkt-&gt;size from avpkt-&gt;data into picture.
Definition: utils.c:2029
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
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:257
ret
Definition: avfilter.c:961
AVStream ** streams
Definition: avformat.h:1016
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:24
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:2607
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:157
static AVInputFormat * iformat
Definition: ffprobe.c:176
enum AVCodecID codec_id
Definition: avcodec.h:1157
main external API structure.
Definition: avcodec.h:1146
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:1157
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1432
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:49
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:3309
#define AVERROR(e)
int height
Definition: frame.h:145
static AVPacket pkt
Definition: demuxing.c:52
This structure stores compressed data.
Definition: avcodec.h:1040
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:52
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition: format.c:177
Miscellaneous utilities which make use of the libavformat library.