2
* Copyright (C) 2013 Savoir-Faire Linux Inc.
3
* Author: Guillaume Roguez <Guillaume.Roguez@savoirfairelinux.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
* Additional permission under GNU GPL version 3 section 7:
22
* If you modify this program, or any covered work, by linking or
23
* combining it with the OpenSSL project's OpenSSL library (or a
24
* modified version of that library), containing parts covered by the
25
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
26
* grants you additional permission to convey the resulting work.
27
* Corresponding Source for a non-source form of such a combination
28
* shall include the source code for the parts of OpenSSL used as well
29
* as that of the covered work.
32
#include "libav_deps.h"
33
#include "video_decoder.h"
43
VideoDecoder::VideoDecoder() :
46
, inputCtx_(avformat_alloc_context())
51
VideoDecoder::~VideoDecoder()
54
avcodec_close(decoderCtx_);
56
if (inputCtx_ and inputCtx_->nb_streams > 0) {
57
#if LIBAVFORMAT_VERSION_MAJOR < 54
58
av_close_input_file(inputCtx_);
60
avformat_close_input(&inputCtx_);
65
int VideoDecoder::openInput(const std::string &source_str,
66
const std::string &format_str)
68
AVInputFormat *iformat = av_find_input_format(format_str.c_str());
71
ERROR("Cannot find format \"%s\"", format_str.c_str());
75
int ret = avformat_open_input(&inputCtx_, source_str.c_str(), iformat,
76
options_ ? &options_ : NULL);
80
av_strerror(ret, errbuf, sizeof(errbuf));
81
ERROR("avformat_open_input failed: %s", errbuf);
83
DEBUG("Using format %s", format_str.c_str());
89
void VideoDecoder::setInterruptCallback(int (*cb)(void*), void *opaque)
92
inputCtx_->interrupt_callback.callback = cb;
93
inputCtx_->interrupt_callback.opaque = opaque;
95
inputCtx_->interrupt_callback.callback = 0;
99
void VideoDecoder::setIOContext(VideoIOHandle *ioctx)
100
{ inputCtx_->pb = ioctx->getContext(); }
102
int VideoDecoder::setupFromVideoData()
107
avcodec_close(decoderCtx_);
109
DEBUG("Finding stream info");
110
if (!inputCtx_->streams[0]->info) {
111
ERROR("Stream info is NULL");
115
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 8, 0)
116
ret = av_find_stream_info(inputCtx_);
118
ret = avformat_find_stream_info(inputCtx_, options_ ? &options_ : NULL);
122
// workaround for this bug:
123
// http://patches.libav.org/patch/22541/
125
ret = AVERROR_INVALIDDATA;
126
char errBuf[64] = {0};
127
// print nothing for unknown errors
128
if (av_strerror(ret, errBuf, sizeof errBuf) < 0)
132
ERROR("Could not find stream info: %s", errBuf);
136
// find the first video stream from the input
137
for (size_t i = 0; streamIndex_ == -1 && i < inputCtx_->nb_streams; ++i)
138
if (inputCtx_->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
141
if (streamIndex_ == -1) {
142
ERROR("Could not find video stream");
146
// Get a pointer to the codec context for the video stream
147
decoderCtx_ = inputCtx_->streams[streamIndex_]->codec;
148
if (decoderCtx_ == 0) {
149
ERROR("Decoder context is NULL");
153
// find the decoder for the video stream
154
inputDecoder_ = avcodec_find_decoder(decoderCtx_->codec_id);
155
if (!inputDecoder_) {
156
ERROR("Unsupported codec");
160
decoderCtx_->thread_count = 1;
162
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 6, 0)
163
ret = avcodec_open(decoderCtx_, inputDecoder_);
165
ret = avcodec_open2(decoderCtx_, inputDecoder_, NULL);
168
ERROR("Could not open codec");
175
int VideoDecoder::decode(VideoFrame& result)
177
// Guarantee that we free the packet every iteration
178
VideoPacket video_packet;
179
AVPacket *inpacket = video_packet.get();
180
int ret = av_read_frame(inputCtx_, inpacket);
181
if (ret == AVERROR(EAGAIN)) {
183
} else if (ret < 0) {
184
ERROR("Couldn't read frame: %s\n", strerror(ret));
188
// is this a packet from the video stream?
189
if (inpacket->stream_index != streamIndex_)
192
int frameFinished = 0;
193
int len = avcodec_decode_video2(decoderCtx_, result.get(),
194
&frameFinished, inpacket);
204
int VideoDecoder::flush(VideoFrame& result)
207
av_init_packet(&inpacket);
208
inpacket.data = NULL;
211
int frameFinished = 0;
212
int len = avcodec_decode_video2(decoderCtx_, result.get(),
213
&frameFinished, &inpacket);
223
int VideoDecoder::getWidth() const
224
{ return decoderCtx_->width; }
226
int VideoDecoder::getHeight() const
227
{ return decoderCtx_->height; }
229
int VideoDecoder::getPixelFormat() const
230
{ return libav_utils::sfl_pixel_format(decoderCtx_->pix_fmt); }