video_base.cpp 7.12 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 *  Copyright (C) 2013 Savoir-Faire Linux Inc.
 *  Author: Guillaume Roguez <Guillaume.Roguez@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301 USA.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

#include "libav_deps.h"
#include "video_base.h"
34
#include "logger.h"
35
36
37
38


namespace sfl_video {

39
40
/*=== VideoPacket  ===========================================================*/

41
42
43
VideoPacket::VideoPacket() : packet_(0)
{
    packet_ = static_cast<AVPacket *>(av_malloc(sizeof(AVPacket)));
44
    av_init_packet(packet_);
45
46
}

47
VideoPacket::~VideoPacket() { av_free_packet(packet_); av_free(packet_); }
48

49
50
/*=== VideoIOHandle  =========================================================*/

51
52
53
54
55
56
57
58
59
60
61
VideoIOHandle::VideoIOHandle(ssize_t buffer_size,
                             bool writeable,
                             io_readcallback read_cb,
                             io_writecallback write_cb,
                             io_seekcallback seek_cb,
                             void *opaque) : ctx_(0), buf_(0)

{
    buf_ = static_cast<unsigned char *>(av_malloc(buffer_size));
    ctx_ = avio_alloc_context(buf_, buffer_size, writeable, opaque, read_cb,
                              write_cb, seek_cb);
62
    ctx_->max_packet_size = buffer_size;
63
64
65
66
67
68
69
70
71
72
73
}

VideoIOHandle::~VideoIOHandle() { av_free(ctx_); av_free(buf_); }

VideoCodec::VideoCodec() : options_(0) {}

void VideoCodec::setOption(const char *name, const char *value)
{
    av_dict_set(&options_, name, value, 0);
}

74
75
/*=== VideoFrame =============================================================*/

76
VideoFrame::VideoFrame() : frame_(avcodec_alloc_frame()), allocated_(false) {}
77

Guillaume Roguez's avatar
Guillaume Roguez committed
78
79
80
81
82
83
VideoFrame::~VideoFrame()
{
    if (allocated_)
        avpicture_free((AVPicture *) frame_);
    avcodec_free_frame(&frame_);
}
84

85
86
87
88
89
90
91
92
int VideoFrame::getPixelFormat() const
{ return libav_utils::sfl_pixel_format(frame_->format); }

int VideoFrame::getWidth() const
{ return frame_->width; }

int VideoFrame::getHeight() const
{ return frame_->height; }
93
94
95
96

bool VideoFrame::allocBuffer(int width, int height, int pix_fmt)
{
    AVPixelFormat libav_pix_fmt = (AVPixelFormat) libav_utils::libav_pixel_format(pix_fmt);
Guillaume Roguez's avatar
Guillaume Roguez committed
97
98
99
100
101
102
    if (allocated_) {
        // nothing to do if same properties
        if (width == frame_->width
            and height == frame_->height
            and libav_pix_fmt == frame_->format)
            return true;
103
        avpicture_free((AVPicture *) frame_);
Guillaume Roguez's avatar
Guillaume Roguez committed
104
    }
105
106
107
108
109
110
111
112
113

    allocated_ = not avpicture_alloc((AVPicture *) frame_,
                                     libav_pix_fmt, width, height);
    if (allocated_)
        setGeometry(width, height, pix_fmt);

    return allocated_;
}

114
115
116
117
118
void VideoFrame::setdefaults()
{
    avcodec_get_frame_defaults(frame_);
}

119
120
121
122
123
124
125
126
127
void VideoFrame::setGeometry(int width, int height, int pix_fmt)
{
    frame_->format = libav_utils::libav_pixel_format(pix_fmt);
    frame_->width = width;
    frame_->height = height;
}

void VideoFrame::setDestination(void *data)
{
Guillaume Roguez's avatar
Guillaume Roguez committed
128
    if (allocated_) {
129
        avpicture_free((AVPicture *) frame_);
Guillaume Roguez's avatar
Guillaume Roguez committed
130
131
        allocated_ = false;
    }
132

133
    avpicture_fill((AVPicture *) frame_, (uint8_t *) data,
134
                   (AVPixelFormat) frame_->format, frame_->width,
135
136
137
138
139
                   frame_->height);
}

size_t VideoFrame::getSize()
{
140
    return avpicture_get_size((AVPixelFormat) frame_->format,
141
142
143
144
                              frame_->width,
                              frame_->height);
}

145
146
147
size_t VideoFrame::getSize(int width, int height, int format)
{
    return avpicture_get_size(
148
        (AVPixelFormat) libav_utils::libav_pixel_format(format), width, height);
149
150
}

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
int VideoFrame::blit(VideoFrame &src, int xoff, int yoff)
{
    const AVFrame *src_frame = src.get();

    if (src_frame->format != PIX_FMT_YUV420P
        || frame_->format != PIX_FMT_YUV420P) {
        ERROR("Unsupported pixel format");
        return -1;
    }

    uint8_t *src_data, *dst_data;
	ssize_t dst_stride;

    // Y
    dst_stride = frame_->linesize[0];
	src_data = src_frame->data[0];
167
	dst_data = frame_->data[0] + yoff * dst_stride + xoff;
168
169
170
171
172
173
174
175
176
	for (int i = 0; i < src_frame->height; i++) {
		memcpy(dst_data, src_data, src_frame->linesize[0]);
		src_data += src_frame->linesize[0];
		dst_data += dst_stride;
	}

    // U
	dst_stride = frame_->linesize[1];
	src_data = src_frame->data[1];
177
	dst_data = frame_->data[1] + yoff / 2 * dst_stride + xoff / 2;
178
179
180
181
182
183
184
185
186
	for (int i = 0; i < src_frame->height / 2; i++) {
		memcpy(dst_data, src_data, src_frame->linesize[1]);
		src_data += src_frame->linesize[1];
		dst_data += dst_stride;
	}

    // V
	dst_stride = frame_->linesize[2];
	src_data = src_frame->data[2];
187
	dst_data = frame_->data[2] + yoff / 2 * dst_stride + xoff / 2;
188
189
190
191
192
193
194
195
196
	for (int i = 0; i < src_frame->height / 2; i++) {
		memcpy(dst_data, src_data, src_frame->linesize[2]);
		src_data += src_frame->linesize[2];
		dst_data += dst_stride;
	}

    return 0;
}

197
void VideoFrame::copy(VideoFrame &dst)
198
{
199
200
201
202
    const AVFrame *dst_frame = dst.get();
    av_picture_copy((AVPicture *)dst_frame, (AVPicture *)frame_,
                    (AVPixelFormat)frame_->format, frame_->width,
                    frame_->height);
203
204
}

205
206
207
208
209
210
211
212
void VideoFrame::clear()
{
    // FIXME: beurk!!!!
    memset(frame_->data[0], 0, frame_->linesize[0]*frame_->height);
    memset(frame_->data[1], 0, frame_->linesize[1]*frame_->height/2);
    memset(frame_->data[2], 0, frame_->linesize[2]*frame_->height/2);
}

213
214
215
216
217
218
void VideoFrame::test()
{
    memset(frame_->data[0], 0xaa, frame_->linesize[0]*frame_->height/2);
}

/*=== VideoGenerator =========================================================*/
219

220
VideoFrame& VideoGenerator::getNewFrame()
221
{
222
    std::unique_lock<std::mutex> lk(mutex_);
223
224
225
226
227
    if (writableFrame_)
        writableFrame_->setdefaults();
    else
        writableFrame_.reset(new VideoFrame());
    return *writableFrame_.get();
228
229
230
231
}

void VideoGenerator::publishFrame()
{
232
    std::unique_lock<std::mutex> lk(mutex_);
233
    lastFrame_ = std::move(writableFrame_);
234
    notify(lastFrame_);
235
236
}

237
VideoFrameSP VideoGenerator::obtainLastFrame()
238
{
239
    std::unique_lock<std::mutex> lk(mutex_);
240
    return lastFrame_;
241
242
}

243
}