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;
70 /** @return Number of components */
72 Image::components () const
74 switch (_pixel_format) {
88 Image::scale (Size out_size, Scaler const * scaler) const
92 shared_ptr<Image> scaled (new AlignedImage (pixel_format(), out_size));
94 struct SwsContext* scale_context = sws_getContext (
95 size().width, size().height, pixel_format(),
96 out_size.width, out_size.height, pixel_format(),
97 scaler->ffmpeg_id (), 0, 0, 0
104 scaled->data(), scaled->stride()
107 sws_freeContext (scale_context);
112 /** Scale this image to a given size and convert it to RGB.
113 * @param out_size Output image size in pixels.
114 * @param scaler Scaler to use.
117 Image::scale_and_convert_to_rgb (Size out_size, int padding, Scaler const * scaler) const
121 Size content_size = out_size;
122 content_size.width -= (padding * 2);
124 shared_ptr<Image> rgb (new AlignedImage (PIX_FMT_RGB24, content_size));
126 struct SwsContext* scale_context = sws_getContext (
127 size().width, size().height, pixel_format(),
128 content_size.width, content_size.height, PIX_FMT_RGB24,
129 scaler->ffmpeg_id (), 0, 0, 0
132 /* Scale and convert to RGB from whatever its currently in (which may be RGB) */
137 rgb->data(), rgb->stride()
140 /* Put the image in the right place in a black frame if are padding; this is
141 a bit grubby and expensive, but probably inconsequential in the great
145 shared_ptr<Image> padded_rgb (new AlignedImage (PIX_FMT_RGB24, out_size));
146 padded_rgb->make_black ();
148 /* XXX: we are cheating a bit here; we know the frame is RGB so we can
149 make assumptions about its composition.
151 uint8_t* p = padded_rgb->data()[0] + padding * 3;
152 uint8_t* q = rgb->data()[0];
153 for (int j = 0; j < rgb->lines(0); ++j) {
154 memcpy (p, q, rgb->line_size()[0]);
155 p += padded_rgb->stride()[0];
156 q += rgb->stride()[0];
162 sws_freeContext (scale_context);
167 /** Run a FFmpeg post-process on this image and return the processed version.
168 * @param pp Flags for the required set of post processes.
169 * @return Post-processed image.
172 Image::post_process (string pp) const
174 shared_ptr<Image> out (new AlignedImage (PIX_FMT_YUV420P, size ()));
176 pp_mode* mode = pp_get_mode_by_name_and_quality (pp.c_str (), PP_QUALITY_MAX);
177 pp_context* context = pp_get_context (size().width, size().height, PP_FORMAT_420 | PP_CPU_CAPS_MMX2);
180 (const uint8_t **) data(), stride(),
181 out->data(), out->stride(),
182 size().width, size().height,
183 0, 0, mode, context, 0
187 pp_free_context (context);
195 switch (_pixel_format) {
196 case PIX_FMT_YUV420P:
197 memset (data()[0], 0, lines(0) * stride()[0]);
198 memset (data()[1], 0x80, lines(1) * stride()[1]);
199 memset (data()[2], 0x80, lines(2) * stride()[2]);
203 memset (data()[0], 0, lines(0) * stride()[0]);
212 Image::alpha_blend (shared_ptr<Image> other, Position position)
214 /* Only implemented for RGBA onto RGB24 so far */
215 assert (_pixel_format == PIX_FMT_RGB24 && other->pixel_format() == PIX_FMT_RGBA);
217 int start_tx = position.x;
221 start_ox = -start_tx;
225 int start_ty = position.y;
229 start_oy = -start_ty;
233 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
234 uint8_t* tp = data()[0] + ty * stride()[0] + position.x * 3;
235 uint8_t* op = other->data()[0] + oy * other->stride()[0];
236 for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
237 float const alpha = float (op[3]) / 255;
238 tp[0] = (tp[0] * (1 - alpha)) + op[0] * alpha;
239 tp[1] = (tp[1] * (1 - alpha)) + op[1] * alpha;
240 tp[2] = (tp[2] * (1 - alpha)) + op[2] * alpha;
248 Image::read_from_socket (shared_ptr<Socket> socket)
250 for (int i = 0; i < components(); ++i) {
251 uint8_t* p = data()[i];
252 for (int y = 0; y < lines(i); ++y) {
253 socket->read_definite_and_consume (p, line_size()[i], 30);
260 Image::write_to_socket (shared_ptr<Socket> socket) const
262 for (int i = 0; i < components(); ++i) {
263 uint8_t* p = data()[i];
264 for (int y = 0; y < lines(i); ++y) {
265 socket->write (p, line_size()[i], 30);
271 /** Construct a SimpleImage of a given size and format, allocating memory
274 * @param p Pixel format.
275 * @param s Size in pixels.
277 SimpleImage::SimpleImage (PixelFormat p, Size s, function<int (int)> rounder)
281 _data = (uint8_t **) av_malloc (4 * sizeof (uint8_t *));
282 _data[0] = _data[1] = _data[2] = _data[3] = 0;
284 _line_size = (int *) av_malloc (4 * sizeof (int));
285 _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
287 _stride = (int *) av_malloc (4 * sizeof (int));
288 _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
292 _line_size[0] = s.width * 3;
295 _line_size[0] = s.width * 4;
297 case PIX_FMT_YUV420P:
298 _line_size[0] = s.width;
299 _line_size[1] = s.width / 2;
300 _line_size[2] = s.width / 2;
306 for (int i = 0; i < components(); ++i) {
307 _stride[i] = rounder (_line_size[i]);
308 _data[i] = (uint8_t *) av_malloc (_stride[i] * lines (i));
312 /** Destroy a SimpleImage */
313 SimpleImage::~SimpleImage ()
315 for (int i = 0; i < components(); ++i) {
320 av_free (_line_size);
325 SimpleImage::data () const
331 SimpleImage::line_size () const
337 SimpleImage::stride () const
343 SimpleImage::size () const
348 AlignedImage::AlignedImage (PixelFormat f, Size s)
349 : SimpleImage (f, s, boost::bind (round_up, _1, 32))
354 CompactImage::CompactImage (PixelFormat f, Size s)
355 : SimpleImage (f, s, boost::bind (round_up, _1, 1))
360 CompactImage::CompactImage (shared_ptr<Image> im)
361 : SimpleImage (im->pixel_format(), im->size(), boost::bind (round_up, _1, 1))
363 assert (components() == im->components());
365 for (int c = 0; c < components(); ++c) {
367 assert (line_size()[c] == im->line_size()[c]);
369 uint8_t* t = data()[c];
370 uint8_t* o = im->data()[c];
372 for (int y = 0; y < lines(c); ++y) {
373 memcpy (t, o, line_size()[c]);
375 o += im->stride()[c];
380 FilterBufferImage::FilterBufferImage (PixelFormat p, AVFilterBufferRef* b)
387 FilterBufferImage::~FilterBufferImage ()
389 avfilter_unref_buffer (_buffer);
393 FilterBufferImage::data () const
395 return _buffer->data;
399 FilterBufferImage::line_size () const
401 return _buffer->linesize;
405 FilterBufferImage::stride () const
408 return _buffer->linesize;
412 FilterBufferImage::size () const
414 return Size (_buffer->video->w, _buffer->video->h);
417 /** XXX: this could be generalised to use any format, but I don't
418 * understand how avpicture_fill is supposed to be called with
419 * multi-planar images.
421 RGBFrameImage::RGBFrameImage (Size s)
422 : Image (PIX_FMT_RGB24)
425 _frame = avcodec_alloc_frame ();
427 throw EncodeError ("could not allocate frame");
430 _data = (uint8_t *) av_malloc (size().width * size().height * 3);
431 avpicture_fill ((AVPicture *) _frame, _data, PIX_FMT_RGB24, size().width, size().height);
432 _frame->width = size().width;
433 _frame->height = size().height;
434 _frame->format = PIX_FMT_RGB24;
437 RGBFrameImage::~RGBFrameImage ()
444 RGBFrameImage::data () const
450 RGBFrameImage::line_size () const
452 return _frame->linesize;
456 RGBFrameImage::stride () const
463 RGBFrameImage::size () const