FFmpeg
Loading...
Searching...
No Matches
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/frame.h"
24#include "libavutil/log.h"
25
27
28#include "libavcodec/avcodec.h"
29
30#include "lavfutils.h"
31
32int ff_load_image(struct AVFrame **outframe,
33 const char *filename, void *log_ctx)
34{
35 const AVInputFormat *iformat = NULL;
36 AVFormatContext *format_ctx = NULL;
37 const AVCodec *codec;
38 AVCodecContext *codec_ctx = NULL;
41 int ret = 0;
43
44 *outframe = NULL;
45
46 iformat = av_find_input_format("image2pipe");
47 if ((ret = avformat_open_input(&format_ctx, filename, iformat, NULL)) < 0) {
48 av_log(log_ctx, AV_LOG_ERROR,
49 "Failed to open input file '%s'\n", filename);
50 return ret;
51 }
52
53 if ((ret = avformat_find_stream_info(format_ctx, NULL)) < 0) {
54 av_log(log_ctx, AV_LOG_ERROR, "Find stream info failed\n");
55 goto end;
56 }
57
58 par = format_ctx->streams[0]->codecpar;
59 codec = avcodec_find_decoder(par->codec_id);
60 if (!codec) {
61 av_log(log_ctx, AV_LOG_ERROR, "Failed to find codec\n");
62 ret = AVERROR(EINVAL);
63 goto end;
64 }
65
66 codec_ctx = avcodec_alloc_context3(codec);
67 if (!codec_ctx) {
68 av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc video decoder context\n");
69 ret = AVERROR(ENOMEM);
70 goto end;
71 }
72
73 ret = avcodec_parameters_to_context(codec_ctx, par);
74 if (ret < 0) {
75 av_log(log_ctx, AV_LOG_ERROR, "Failed to copy codec parameters to decoder context\n");
76 goto end;
77 }
78
79 codec_ctx->thread_type = FF_THREAD_SLICE;
80 if ((ret = avcodec_open2(codec_ctx, codec, NULL)) < 0) {
81 av_log(log_ctx, AV_LOG_ERROR, "Failed to open codec\n");
82 goto end;
83 }
84
85 ret = av_read_frame(format_ctx, &pkt);
86 if (ret < 0) {
87 av_log(log_ctx, AV_LOG_ERROR, "Failed to read frame from file\n");
88 goto end;
89 }
90
91 ret = avcodec_send_packet(codec_ctx, &pkt);
93 if (ret < 0) {
94 av_log(log_ctx, AV_LOG_ERROR, "Error submitting a packet to decoder\n");
95 goto end;
96 }
97
98 if (!(frame = av_frame_alloc()) ) {
99 av_log(log_ctx, AV_LOG_ERROR, "Failed to alloc frame\n");
100 ret = AVERROR(ENOMEM);
101 goto end;
102 }
103
104 ret = avcodec_receive_frame(codec_ctx, frame);
105 if (ret < 0) {
107 av_log(log_ctx, AV_LOG_ERROR, "Failed to decode image from file\n");
108 goto end;
109 }
110 *outframe = frame;
111
112end:
113 avcodec_free_context(&codec_ctx);
114 avformat_close_input(&format_ctx);
115
116 if (ret < 0)
117 av_log(log_ctx, AV_LOG_ERROR, "Error loading image file '%s'\n", filename);
118 return ret;
119}
Libavcodec external API header.
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition avcodec.h:1591
Main libavformat public API header.
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Definition codec_par.c:206
#define NULL
Definition coverity.c:32
static AVPacket * pkt
static AVFrame * frame
static const AVInputFormat * iformat
Definition ffprobe.c:345
reference-counted frame API
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition avcodec.c:144
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition options.c:149
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition allcodecs.c:990
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
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Alias for avcodec_receive_frame_flags(avctx, frame, 0).
Definition avcodec.c:720
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition decode.c:730
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
const AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition format.c:146
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition demux.c:1588
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:231
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition demux.c:2606
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition demux.c:377
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int ff_load_image(struct AVFrame **outframe, const char *filename, void *log_ctx)
Load image from filename and put the resulting image in an AVFrame.
Definition lavfutils.c:32
Miscellaneous utilities which make use of the libavformat library.
main external API structure.
Definition avcodec.h:443
int thread_type
Which multithreading methods to use.
Definition avcodec.h:1589
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
AVCodec.
Definition codec.h:175
Format I/O context.
Definition avformat.h:1333
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1401
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
#define av_log(a,...)