~ubuntu-branches/ubuntu/trusty/sflphone/trusty

« back to all changes in this revision

Viewing changes to daemon/src/video/video_decoder.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (4.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20140128182336-jrsv0k9u6cawc068
Tags: 1.3.0-1
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2013 Savoir-Faire Linux Inc.
 
3
 *  Author: Guillaume Roguez <Guillaume.Roguez@savoirfairelinux.com>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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,
 
18
 *  MA  02110-1301 USA.
 
19
 *
 
20
 *  Additional permission under GNU GPL version 3 section 7:
 
21
 *
 
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.
 
30
 */
 
31
 
 
32
#include "libav_deps.h"
 
33
#include "video_decoder.h"
 
34
#include "check.h"
 
35
 
 
36
#include <iostream>
 
37
 
 
38
 
 
39
namespace sfl_video {
 
40
 
 
41
using std::string;
 
42
 
 
43
VideoDecoder::VideoDecoder() :
 
44
    inputDecoder_(0)
 
45
    , decoderCtx_(0)
 
46
    , inputCtx_(avformat_alloc_context())
 
47
    , streamIndex_(-1)
 
48
{
 
49
}
 
50
 
 
51
VideoDecoder::~VideoDecoder()
 
52
{
 
53
    if (decoderCtx_)
 
54
        avcodec_close(decoderCtx_);
 
55
 
 
56
    if (inputCtx_ and inputCtx_->nb_streams > 0) {
 
57
#if LIBAVFORMAT_VERSION_MAJOR < 54
 
58
        av_close_input_file(inputCtx_);
 
59
#else
 
60
        avformat_close_input(&inputCtx_);
 
61
#endif
 
62
    }
 
63
}
 
64
 
 
65
int VideoDecoder::openInput(const std::string &source_str,
 
66
                            const std::string &format_str)
 
67
{
 
68
    AVInputFormat *iformat = av_find_input_format(format_str.c_str());
 
69
 
 
70
    if (!iformat) {
 
71
        ERROR("Cannot find format \"%s\"", format_str.c_str());
 
72
        return -1;
 
73
    }
 
74
 
 
75
    int ret = avformat_open_input(&inputCtx_, source_str.c_str(), iformat,
 
76
                                  options_ ? &options_ : NULL);
 
77
 
 
78
    if (ret) {
 
79
        char errbuf[64];
 
80
        av_strerror(ret, errbuf, sizeof(errbuf));
 
81
        ERROR("avformat_open_input failed: %s", errbuf);
 
82
    } else {
 
83
        DEBUG("Using format %s", format_str.c_str());
 
84
    }
 
85
 
 
86
    return ret;
 
87
}
 
88
 
 
89
void VideoDecoder::setInterruptCallback(int (*cb)(void*), void *opaque)
 
90
{
 
91
    if (cb) {
 
92
        inputCtx_->interrupt_callback.callback = cb;
 
93
        inputCtx_->interrupt_callback.opaque = opaque;
 
94
    } else {
 
95
        inputCtx_->interrupt_callback.callback = 0;
 
96
    }
 
97
}
 
98
 
 
99
void VideoDecoder::setIOContext(VideoIOHandle *ioctx)
 
100
{ inputCtx_->pb = ioctx->getContext(); }
 
101
 
 
102
int VideoDecoder::setupFromVideoData()
 
103
{
 
104
    int ret;
 
105
 
 
106
    if (decoderCtx_)
 
107
        avcodec_close(decoderCtx_);
 
108
 
 
109
    DEBUG("Finding stream info");
 
110
    if (!inputCtx_->streams[0]->info)  {
 
111
        ERROR("Stream info is NULL");
 
112
        return -1;
 
113
    }
 
114
 
 
115
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 8, 0)
 
116
    ret = av_find_stream_info(inputCtx_);
 
117
#else
 
118
    ret = avformat_find_stream_info(inputCtx_, options_ ? &options_ : NULL);
 
119
#endif
 
120
 
 
121
    if (ret < 0) {
 
122
        // workaround for this bug:
 
123
        // http://patches.libav.org/patch/22541/
 
124
        if (ret == -1)
 
125
            ret = AVERROR_INVALIDDATA;
 
126
        char errBuf[64] = {0};
 
127
        // print nothing for unknown errors
 
128
        if (av_strerror(ret, errBuf, sizeof errBuf) < 0)
 
129
            errBuf[0] = '\0';
 
130
 
 
131
        // always fail here
 
132
        ERROR("Could not find stream info: %s", errBuf);
 
133
        return -1;
 
134
    }
 
135
 
 
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)
 
139
            streamIndex_ = i;
 
140
 
 
141
    if (streamIndex_ == -1) {
 
142
        ERROR("Could not find video stream");
 
143
        return -1;
 
144
    }
 
145
 
 
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");
 
150
        return -1;
 
151
    }
 
152
 
 
153
    // find the decoder for the video stream
 
154
    inputDecoder_ = avcodec_find_decoder(decoderCtx_->codec_id);
 
155
    if (!inputDecoder_) {
 
156
        ERROR("Unsupported codec");
 
157
        return -1;
 
158
    }
 
159
 
 
160
    decoderCtx_->thread_count = 1;
 
161
 
 
162
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 6, 0)
 
163
    ret = avcodec_open(decoderCtx_, inputDecoder_);
 
164
#else
 
165
    ret = avcodec_open2(decoderCtx_, inputDecoder_, NULL);
 
166
#endif
 
167
    if (ret) {
 
168
        ERROR("Could not open codec");
 
169
        return -1;
 
170
    }
 
171
 
 
172
    return 0;
 
173
}
 
174
 
 
175
int VideoDecoder::decode(VideoFrame& result)
 
176
{
 
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)) {
 
182
        return 0;
 
183
    } else if (ret < 0) {
 
184
        ERROR("Couldn't read frame: %s\n", strerror(ret));
 
185
        return -1;
 
186
    }
 
187
 
 
188
    // is this a packet from the video stream?
 
189
    if (inpacket->stream_index != streamIndex_)
 
190
        return 0;
 
191
 
 
192
    int frameFinished = 0;
 
193
    int len = avcodec_decode_video2(decoderCtx_, result.get(),
 
194
                                    &frameFinished, inpacket);
 
195
    if (len <= 0)
 
196
        return -2;
 
197
 
 
198
    if (frameFinished)
 
199
        return 1;
 
200
 
 
201
    return 0;
 
202
}
 
203
 
 
204
int VideoDecoder::flush(VideoFrame& result)
 
205
{
 
206
    AVPacket inpacket;
 
207
    av_init_packet(&inpacket);
 
208
    inpacket.data = NULL;
 
209
    inpacket.size = 0;
 
210
 
 
211
    int frameFinished = 0;
 
212
    int len = avcodec_decode_video2(decoderCtx_, result.get(),
 
213
                                    &frameFinished, &inpacket);
 
214
    if (len <= 0)
 
215
        return -2;
 
216
 
 
217
    if (frameFinished)
 
218
        return 1;
 
219
 
 
220
    return 0;
 
221
}
 
222
 
 
223
int VideoDecoder::getWidth() const
 
224
{ return decoderCtx_->width; }
 
225
 
 
226
int VideoDecoder::getHeight() const
 
227
{ return decoderCtx_->height; }
 
228
 
 
229
int VideoDecoder::getPixelFormat() const
 
230
{ return libav_utils::sfl_pixel_format(decoderCtx_->pix_fmt); }
 
231
 
 
232
}