FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  AVCodecParameters *par;
33  AVFrame *frame;
34  int frame_decoded, ret = 0;
35  AVPacket pkt;
36  AVDictionary *opt=NULL;
37 
38  av_init_packet(&pkt);
39 
41 
42  iformat = av_find_input_format("image2pipe");
43  if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) {
44  av_log(log_ctx, AV_LOG_ERROR,
45  "Failed to open input file '%s'\n", filename);
46  return ret;
47  }
48 
49  if ((ret = avformat_find_stream_info(format_ctx, NULL)) < 0) {
50  av_log(log_ctx, AV_LOG_ERROR, "Find stream info failed\n");
51  return ret;
52  }
53 
54  par = format_ctx->streams[0]->codecpar;
55  codec = avcodec_find_decoder(par->codec_id);
56  if (!codec) {
57  av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
58  ret = AVERROR(EINVAL);
59  goto end;
60  }
61 
62  codec_ctx = avcodec_alloc_context3(codec);
63  if (!codec_ctx) {
64  av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc video decoder context\n");
65  ret = AVERROR(ENOMEM);
66  goto end;
67  }
68 
69  ret = avcodec_parameters_to_context(codec_ctx, par);
70  if (ret < 0) {
71  av_log(log_ctx, AV_LOG_ERROR, "Failed to copy codec parameters to decoder context\n");
72  goto end;
73  }
74 
75  av_dict_set(&opt, "thread_type", "slice", 0);
76  if ((ret = avcodec_open2(codec_ctx, codec, &opt)) < 0) {
77  av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
78  goto end;
79  }
80 
81  if (!(frame = av_frame_alloc()) ) {
82  av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
83  ret = AVERROR(ENOMEM);
84  goto end;
85  }
86 
87  ret = av_read_frame(format_ctx, &pkt);
88  if (ret < 0) {
89  av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
90  goto end;
91  }
92 
93  ret = avcodec_decode_video2(codec_ctx, frame, &frame_decoded, &pkt);
94  if (ret < 0 || !frame_decoded) {
95  av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n");
96  if (ret >= 0)
97  ret = -1;
98  goto end;
99  }
100 
101  *w = frame->width;
102  *h = frame->height;
103  *pix_fmt = frame->format;
104 
105  if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
106  goto end;
107  ret = 0;
108 
109  av_image_copy(data, linesize, (const uint8_t **)frame->data, frame->linesize, *pix_fmt, *w, *h);
110 
111 end:
112  av_packet_unref(&pkt);
113  avcodec_free_context(&codec_ctx);
114  avformat_close_input(&format_ctx);
115  av_frame_free(&frame);
116  av_dict_free(&opt);
117 
118  if (ret < 0)
119  av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);
120  return ret;
121 }
#define NULL
Definition: coverity.c:32
static enum AVPixelFormat pix_fmt
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
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:192
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3736
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3289
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3728
Format I/O context.
Definition: avformat.h:1325
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1393
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:2299
static AVFrame * frame
#define av_log(a,...)
int width
Definition: frame.h:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
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:386
AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition: format.c:164
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:157
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
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:274
static AVInputFormat * iformat
Definition: ffprobe.c:253
attribute_deprecated int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, const AVPacket *avpkt)
Decode the video frame of size avpkt->size from avpkt->data into picture.
Definition: decode.c:820
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:172
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
main external API structure.
Definition: avcodec.h:1481
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:1249
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:590
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:612
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1712
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:3496
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4349
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:515
int height
Definition: frame.h:259
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1014
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1376
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:394
Miscellaneous utilities which make use of the libavformat library.