2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file src/image.cc
21 * @brief A set of classes to describe video images.
28 #include <boost/algorithm/string.hpp>
29 #include <boost/bind.hpp>
32 #include <libavcodec/avcodec.h>
33 #include <libavformat/avformat.h>
34 #include <libswscale/swscale.h>
35 #include <libavfilter/avfiltergraph.h>
36 #include <libpostproc/postprocess.h>
37 #include <libavutil/pixfmt.h>
40 #include "exceptions.h"
44 using namespace boost;
46 /** @param n Component index.
47 * @return Number of lines in the image for the given component.
50 Image::lines (int n) const
52 switch (_pixel_format) {
57 return size().height / 2;
62 case PIX_FMT_YUV422P10LE:
72 /** @return Number of components */
74 Image::components () const
76 switch (_pixel_format) {
78 case PIX_FMT_YUV422P10LE:
92 Image::scale (Size out_size, Scaler const * scaler) const
96 shared_ptr<Image> scaled (new AlignedImage (pixel_format(), out_size));
98 struct SwsContext* scale_context = sws_getContext (
99 size().width, size().height, pixel_format(),
100 out_size.width, out_size.height, pixel_format(),
101 scaler->ffmpeg_id (), 0, 0, 0
108 scaled->data(), scaled->stride()
111 sws_freeContext (scale_context);
116 /** Scale this image to a given size and convert it to RGB.
117 * @param out_size Output image size in pixels.
118 * @param scaler Scaler to use.
121 Image::scale_and_convert_to_rgb (Size out_size, int padding, Scaler const * scaler) const
125 Size content_size = out_size;
126 content_size.width -= (padding * 2);
128 shared_ptr<Image> rgb (new AlignedImage (PIX_FMT_RGB24, content_size));
130 struct SwsContext* scale_context = sws_getContext (
131 size().width, size().height, pixel_format(),
132 content_size.width, content_size.height, PIX_FMT_RGB24,
133 scaler->ffmpeg_id (), 0, 0, 0
136 /* Scale and convert to RGB from whatever its currently in (which may be RGB) */
141 rgb->data(), rgb->stride()
144 /* Put the image in the right place in a black frame if are padding; this is
145 a bit grubby and expensive, but probably inconsequential in the great
149 shared_ptr<Image> padded_rgb (new AlignedImage (PIX_FMT_RGB24, out_size));
150 padded_rgb->make_black ();
152 /* XXX: we are cheating a bit here; we know the frame is RGB so we can
153 make assumptions about its composition.
155 uint8_t* p = padded_rgb->data()[0] + padding * 3;
156 uint8_t* q = rgb->data()[0];
157 for (int j = 0; j < rgb->lines(0); ++j) {
158 memcpy (p, q, rgb->line_size()[0]);
159 p += padded_rgb->stride()[0];
160 q += rgb->stride()[0];
166 sws_freeContext (scale_context);
171 /** Run a FFmpeg post-process on this image and return the processed version.
172 * @param pp Flags for the required set of post processes.
173 * @return Post-processed image.
176 Image::post_process (string pp) const
178 shared_ptr<Image> out (new AlignedImage (pixel_format(), size ()));
181 switch (pixel_format()) {
182 case PIX_FMT_YUV420P:
183 pp_format = PP_FORMAT_420;
185 case PIX_FMT_YUV422P10LE:
186 case PIX_FMT_YUV422P:
187 pp_format = PP_FORMAT_422;
193 pp_mode* mode = pp_get_mode_by_name_and_quality (pp.c_str (), PP_QUALITY_MAX);
194 pp_context* context = pp_get_context (size().width, size().height, pp_format | PP_CPU_CAPS_MMX2);
197 (const uint8_t **) data(), stride(),
198 out->data(), out->stride(),
199 size().width, size().height,
200 0, 0, mode, context, 0
204 pp_free_context (context);
212 switch (_pixel_format) {
213 case PIX_FMT_YUV420P:
214 case PIX_FMT_YUV422P10LE:
215 case PIX_FMT_YUV422P:
216 memset (data()[0], 0, lines(0) * stride()[0]);
217 memset (data()[1], 0x80, lines(1) * stride()[1]);
218 memset (data()[2], 0x80, lines(2) * stride()[2]);
222 memset (data()[0], 0, lines(0) * stride()[0]);
231 Image::alpha_blend (shared_ptr<Image> other, Position position)
233 /* Only implemented for RGBA onto RGB24 so far */
234 assert (_pixel_format == PIX_FMT_RGB24 && other->pixel_format() == PIX_FMT_RGBA);
236 int start_tx = position.x;
240 start_ox = -start_tx;
244 int start_ty = position.y;
248 start_oy = -start_ty;
252 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
253 uint8_t* tp = data()[0] + ty * stride()[0] + position.x * 3;
254 uint8_t* op = other->data()[0] + oy * other->stride()[0];
255 for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
256 float const alpha = float (op[3]) / 255;
257 tp[0] = (tp[0] * (1 - alpha)) + op[0] * alpha;
258 tp[1] = (tp[1] * (1 - alpha)) + op[1] * alpha;
259 tp[2] = (tp[2] * (1 - alpha)) + op[2] * alpha;
267 Image::read_from_socket (shared_ptr<Socket> socket)
269 for (int i = 0; i < components(); ++i) {
270 uint8_t* p = data()[i];
271 for (int y = 0; y < lines(i); ++y) {
272 socket->read_definite_and_consume (p, line_size()[i], 30);
279 Image::write_to_socket (shared_ptr<Socket> socket) const
281 for (int i = 0; i < components(); ++i) {
282 uint8_t* p = data()[i];
283 for (int y = 0; y < lines(i); ++y) {
284 socket->write (p, line_size()[i], 30);
290 /** Construct a SimpleImage of a given size and format, allocating memory
293 * @param p Pixel format.
294 * @param s Size in pixels.
296 SimpleImage::SimpleImage (AVPixelFormat p, Size s, function<int (int, int const *)> stride_computer)
300 _data = (uint8_t **) av_malloc (4 * sizeof (uint8_t *));
301 _data[0] = _data[1] = _data[2] = _data[3] = 0;
303 _line_size = (int *) av_malloc (4 * sizeof (int));
304 _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
306 _stride = (int *) av_malloc (4 * sizeof (int));
307 _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
311 _line_size[0] = s.width * 3;
314 _line_size[0] = s.width * 4;
316 case PIX_FMT_YUV420P:
317 case PIX_FMT_YUV422P:
318 _line_size[0] = s.width;
319 _line_size[1] = s.width / 2;
320 _line_size[2] = s.width / 2;
322 case PIX_FMT_YUV422P10LE:
323 _line_size[0] = s.width * 2;
324 _line_size[1] = s.width;
325 _line_size[2] = s.width;
331 for (int i = 0; i < components(); ++i) {
332 _stride[i] = stride_computer (i, _line_size);
333 _data[i] = (uint8_t *) av_malloc (_stride[i] * lines (i));
337 /** Destroy a SimpleImage */
338 SimpleImage::~SimpleImage ()
340 for (int i = 0; i < components(); ++i) {
345 av_free (_line_size);
350 SimpleImage::data () const
356 SimpleImage::line_size () const
362 SimpleImage::stride () const
368 SimpleImage::size () const
373 AlignedImage::AlignedImage (AVPixelFormat f, Size s)
374 : SimpleImage (f, s, boost::bind (stride_round_up, _1, _2, 32))
379 AlignedImage::AlignedImage (shared_ptr<Image> im)
380 : SimpleImage (im->pixel_format(), im->size(), boost::bind (stride_round_up, _1, _2, 32))
382 assert (components() == im->components());
384 for (int c = 0; c < components(); ++c) {
386 assert (line_size()[c] == im->line_size()[c]);
388 uint8_t* t = data()[c];
389 uint8_t* o = im->data()[c];
391 for (int y = 0; y < lines(c); ++y) {
392 memcpy (t, o, line_size()[c]);
394 o += im->stride()[c];
399 CompactImage::CompactImage (AVPixelFormat f, Size s)
400 : SimpleImage (f, s, boost::bind (stride_round_up, _1, _2, 1))
405 CompactImage::CompactImage (shared_ptr<Image> im)
406 : SimpleImage (im->pixel_format(), im->size(), boost::bind (stride_round_up, _1, _2, 1))
408 assert (components() == im->components());
410 for (int c = 0; c < components(); ++c) {
412 assert (line_size()[c] == im->line_size()[c]);
414 uint8_t* t = data()[c];
415 uint8_t* o = im->data()[c];
417 for (int y = 0; y < lines(c); ++y) {
418 memcpy (t, o, line_size()[c]);
420 o += im->stride()[c];
425 FilterBufferImage::FilterBufferImage (AVPixelFormat p, AVFilterBufferRef* b)
432 FilterBufferImage::~FilterBufferImage ()
434 avfilter_unref_buffer (_buffer);
438 FilterBufferImage::data () const
440 return _buffer->data;
444 FilterBufferImage::line_size () const
446 return _buffer->linesize;
450 FilterBufferImage::stride () const
453 return _buffer->linesize;
457 FilterBufferImage::size () const
459 return Size (_buffer->video->w, _buffer->video->h);