FFmpeg
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 <stdint.h>
22 
23 #include "libavutil/dict.h"
24 #include "libavutil/imgutils.h"
25 #include "libavutil/log.h"
26 #include "libavutil/pixfmt.h"
27 
28 #include "libavformat/avformat.h"
29 
30 #include "libavcodec/avcodec.h"
31 
32 #include "lavfutils.h"
33 
34 int ff_load_image(uint8_t *data[4], int linesize[4],
35  int *w, int *h, enum AVPixelFormat *pix_fmt,
36  const char *filename, void *log_ctx)
37 {
38  const AVInputFormat *iformat = NULL;
39  AVFormatContext *format_ctx = NULL;
40  const AVCodec *codec;
41  AVCodecContext *codec_ctx = NULL;
42  AVCodecParameters *par;
43  AVFrame *frame = NULL;
44  int ret = 0;
45  AVPacket pkt;
46  AVDictionary *opt=NULL;
47 
48  iformat = av_find_input_format("image2pipe");
49  if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) {
50  av_log(log_ctx, AV_LOG_ERROR,
51  "Failed to open input file '%s'\n", filename);
52  return ret;
53  }
54 
55  if ((ret = avformat_find_stream_info(format_ctx, NULL)) < 0) {
56  av_log(log_ctx, AV_LOG_ERROR, "Find stream info failed\n");
57  goto end;
58  }
59 
60  par = format_ctx->streams[0]->codecpar;
61  codec = avcodec_find_decoder(par->codec_id);
62  if (!codec) {
63  av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
64  ret = AVERROR(EINVAL);
65  goto end;
66  }
67 
68  codec_ctx = avcodec_alloc_context3(codec);
69  if (!codec_ctx) {
70  av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc video decoder context\n");
71  ret = AVERROR(ENOMEM);
72  goto end;
73  }
74 
75  ret = avcodec_parameters_to_context(codec_ctx, par);
76  if (ret < 0) {
77  av_log(log_ctx, AV_LOG_ERROR, "Failed to copy codec parameters to decoder context\n");
78  goto end;
79  }
80 
81  av_dict_set(&opt, "thread_type", "slice", 0);
82  if ((ret = avcodec_open2(codec_ctx, codec, &opt)) < 0) {
83  av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
84  goto end;
85  }
86 
87  if (!(frame = av_frame_alloc()) ) {
88  av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
89  ret = AVERROR(ENOMEM);
90  goto end;
91  }
92 
93  ret = av_read_frame(format_ctx, &pkt);
94  if (ret < 0) {
95  av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
96  goto end;
97  }
98 
99  ret = avcodec_send_packet(codec_ctx, &pkt);
101  if (ret < 0) {
102  av_log(log_ctx, AV_LOG_ERROR, "Error submitting a packet to decoder\n");
103  goto end;
104  }
105 
106  ret = avcodec_receive_frame(codec_ctx, frame);
107  if (ret < 0) {
108  av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n");
109  goto end;
110  }
111 
112  *w = frame->width;
113  *h = frame->height;
114  *pix_fmt = frame->format;
115 
116  if ((ret = av_image_alloc(data, linesize, *w, *h, *pix_fmt, 16)) < 0)
117  goto end;
118  ret = 0;
119 
120  av_image_copy2(data, linesize, frame->data, frame->linesize,
121  *pix_fmt, *w, *h);
122 
123 end:
124  avcodec_free_context(&codec_ctx);
125  avformat_close_input(&format_ctx);
127  av_dict_free(&opt);
128 
129  if (ret < 0)
130  av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);
131  return ret;
132 }
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:427
AVCodec
AVCodec.
Definition: codec.h:187
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVERROR
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
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:130
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1323
AVFrame::width
int width
Definition: frame.h:416
w
uint8_t w
Definition: llviddspenc.c:38
data
const char data[16]
Definition: mxf.c:148
AVDictionary
Definition: dict.c:34
av_read_frame
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: demux.c:1525
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
avformat_close_input
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: demux.c:362
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:118
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVInputFormat
Definition: avformat.h:548
avformat_open_input
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: demux.c:214
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:149
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demux_decode.c:41
avcodec_receive_frame
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder or encoder (when the AV_CODEC_FLAG_RECON_FRAME flag is used...
Definition: avcodec.c:681
frame
static AVFrame * frame
Definition: demux_decode.c:54
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
avcodec_parameters_to_context
int avcodec_parameters_to_context(AVCodecContext *codec, const struct AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
NULL
#define NULL
Definition: coverity.c:32
avcodec_free_context
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:164
avcodec_open2
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:128
avcodec_find_decoder
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:971
av_image_alloc
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:218
avformat_find_stream_info
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: demux.c:2499
ff_load_image
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:34
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:431
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:223
iformat
static const AVInputFormat * iformat
Definition: ffprobe.c:359
avcodec_send_packet
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:675
log.h
avcodec.h
ret
ret
Definition: filter_design.txt:187
pixfmt.h
avformat.h
dict.h
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVFrame::height
int height
Definition: frame.h:416
av_image_copy2
static void av_image_copy2(uint8_t *const dst_data[4], const int dst_linesizes[4], uint8_t *const src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Wrapper around av_image_copy() to workaround the limitation that the conversion from uint8_t * const ...
Definition: imgutils.h:184
av_find_input_format
const AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition: format.c:145
lavfutils.h
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
av_dict_set
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:88
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:389
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
h
h
Definition: vp9dsp_template.c:2038