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;
47 Image::swap (Image& other)
49 std::swap (_pixel_format, other._pixel_format);
52 /** @param n Component index.
53 * @return Number of lines in the image for the given component.
56 Image::lines (int n) const
58 switch (_pixel_format) {
63 return size().height / 2;
68 case PIX_FMT_YUV422P10LE:
78 /** @return Number of components */
80 Image::components () const
82 switch (_pixel_format) {
84 case PIX_FMT_YUV422P10LE:
98 Image::scale (Size out_size, Scaler const * scaler, bool aligned) const
102 shared_ptr<Image> scaled (new SimpleImage (pixel_format(), out_size, aligned));
104 struct SwsContext* scale_context = sws_getContext (
105 size().width, size().height, pixel_format(),
106 out_size.width, out_size.height, pixel_format(),
107 scaler->ffmpeg_id (), 0, 0, 0
114 scaled->data(), scaled->stride()
117 sws_freeContext (scale_context);
122 /** Scale this image to a given size and convert it to RGB.
123 * @param out_size Output image size in pixels.
124 * @param scaler Scaler to use.
127 Image::scale_and_convert_to_rgb (Size out_size, int padding, Scaler const * scaler, bool aligned) const
131 Size content_size = out_size;
132 content_size.width -= (padding * 2);
134 shared_ptr<Image> rgb (new SimpleImage (PIX_FMT_RGB24, content_size, aligned));
136 struct SwsContext* scale_context = sws_getContext (
137 size().width, size().height, pixel_format(),
138 content_size.width, content_size.height, PIX_FMT_RGB24,
139 scaler->ffmpeg_id (), 0, 0, 0
142 /* Scale and convert to RGB from whatever its currently in (which may be RGB) */
147 rgb->data(), rgb->stride()
150 /* Put the image in the right place in a black frame if are padding; this is
151 a bit grubby and expensive, but probably inconsequential in the great
155 shared_ptr<Image> padded_rgb (new SimpleImage (PIX_FMT_RGB24, out_size, aligned));
156 padded_rgb->make_black ();
158 /* XXX: we are cheating a bit here; we know the frame is RGB so we can
159 make assumptions about its composition.
161 uint8_t* p = padded_rgb->data()[0] + padding * 3;
162 uint8_t* q = rgb->data()[0];
163 for (int j = 0; j < rgb->lines(0); ++j) {
164 memcpy (p, q, rgb->line_size()[0]);
165 p += padded_rgb->stride()[0];
166 q += rgb->stride()[0];
172 sws_freeContext (scale_context);
177 /** Run a FFmpeg post-process on this image and return the processed version.
178 * @param pp Flags for the required set of post processes.
179 * @return Post-processed image.
182 Image::post_process (string pp, bool aligned) const
184 shared_ptr<Image> out (new SimpleImage (pixel_format(), size (), aligned));
187 switch (pixel_format()) {
188 case PIX_FMT_YUV420P:
189 pp_format = PP_FORMAT_420;
191 case PIX_FMT_YUV422P10LE:
192 case PIX_FMT_YUV422P:
193 pp_format = PP_FORMAT_422;
199 pp_mode* mode = pp_get_mode_by_name_and_quality (pp.c_str (), PP_QUALITY_MAX);
200 pp_context* context = pp_get_context (size().width, size().height, pp_format | PP_CPU_CAPS_MMX2);
203 (const uint8_t **) data(), stride(),
204 out->data(), out->stride(),
205 size().width, size().height,
206 0, 0, mode, context, 0
210 pp_free_context (context);
216 Image::crop (Crop crop, bool aligned) const
218 Size cropped_size = size ();
219 cropped_size.width -= crop.left + crop.right;
220 cropped_size.height -= crop.top + crop.bottom;
222 shared_ptr<Image> out (new SimpleImage (pixel_format(), cropped_size, aligned));
224 for (int c = 0; c < components(); ++c) {
225 int const crop_left_in_bytes = bytes_per_pixel(c) * crop.left;
226 int const cropped_width_in_bytes = bytes_per_pixel(c) * cropped_size.width;
228 /* Start of the source line, cropped from the top but not the left */
229 uint8_t* in_p = data()[c] + crop.top * stride()[c];
230 uint8_t* out_p = out->data()[c];
232 for (int y = 0; y < cropped_size.height; ++y) {
233 memcpy (out_p, in_p + crop_left_in_bytes, cropped_width_in_bytes);
234 in_p += line_size()[c];
235 out_p += out->line_size()[c];
245 switch (_pixel_format) {
246 case PIX_FMT_YUV420P:
247 case PIX_FMT_YUV422P10LE:
248 case PIX_FMT_YUV422P:
249 memset (data()[0], 0, lines(0) * stride()[0]);
250 memset (data()[1], 0x80, lines(1) * stride()[1]);
251 memset (data()[2], 0x80, lines(2) * stride()[2]);
255 memset (data()[0], 0, lines(0) * stride()[0]);
264 Image::alpha_blend (shared_ptr<const Image> other, Position position)
266 /* Only implemented for RGBA onto RGB24 so far */
267 assert (_pixel_format == PIX_FMT_RGB24 && other->pixel_format() == PIX_FMT_RGBA);
269 int start_tx = position.x;
273 start_ox = -start_tx;
277 int start_ty = position.y;
281 start_oy = -start_ty;
285 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
286 uint8_t* tp = data()[0] + ty * stride()[0] + position.x * 3;
287 uint8_t* op = other->data()[0] + oy * other->stride()[0];
288 for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
289 float const alpha = float (op[3]) / 255;
290 tp[0] = (tp[0] * (1 - alpha)) + op[0] * alpha;
291 tp[1] = (tp[1] * (1 - alpha)) + op[1] * alpha;
292 tp[2] = (tp[2] * (1 - alpha)) + op[2] * alpha;
300 Image::read_from_socket (shared_ptr<Socket> socket)
302 for (int i = 0; i < components(); ++i) {
303 uint8_t* p = data()[i];
304 for (int y = 0; y < lines(i); ++y) {
305 socket->read_definite_and_consume (p, line_size()[i], 30);
312 Image::write_to_socket (shared_ptr<Socket> socket) const
314 for (int i = 0; i < components(); ++i) {
315 uint8_t* p = data()[i];
316 for (int y = 0; y < lines(i); ++y) {
317 socket->write (p, line_size()[i], 30);
325 Image::bytes_per_pixel (int c) const
331 switch (_pixel_format) {
344 case PIX_FMT_YUV420P:
345 case PIX_FMT_YUV422P:
351 case PIX_FMT_YUV422P10LE:
365 /** Construct a SimpleImage of a given size and format, allocating memory
368 * @param p Pixel format.
369 * @param s Size in pixels.
371 SimpleImage::SimpleImage (AVPixelFormat p, Size s, bool aligned)
380 SimpleImage::allocate ()
382 _data = (uint8_t **) av_malloc (4 * sizeof (uint8_t *));
383 _data[0] = _data[1] = _data[2] = _data[3] = 0;
385 _line_size = (int *) av_malloc (4 * sizeof (int));
386 _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
388 _stride = (int *) av_malloc (4 * sizeof (int));
389 _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
391 for (int i = 0; i < components(); ++i) {
392 _line_size[i] = _size.width * bytes_per_pixel(i);
393 _stride[i] = stride_round_up (i, _line_size, _aligned ? 32 : 1);
394 _data[i] = (uint8_t *) av_malloc (_stride[i] * lines (i));
398 SimpleImage::SimpleImage (SimpleImage const & other)
402 _aligned = other._aligned;
406 for (int i = 0; i < components(); ++i) {
407 memcpy (_data[i], other._data[i], _line_size[i] * lines(i));
412 SimpleImage::operator= (SimpleImage const & other)
414 if (this == &other) {
418 SimpleImage tmp (other);
424 SimpleImage::swap (SimpleImage & other)
428 std::swap (_size, other._size);
430 for (int i = 0; i < 4; ++i) {
431 std::swap (_data[i], other._data[i]);
432 std::swap (_line_size[i], other._line_size[i]);
433 std::swap (_stride[i], other._stride[i]);
436 std::swap (_aligned, other._aligned);
439 /** Destroy a SimpleImage */
440 SimpleImage::~SimpleImage ()
442 for (int i = 0; i < components(); ++i) {
447 av_free (_line_size);
452 SimpleImage::data () const
458 SimpleImage::line_size () const
464 SimpleImage::stride () const
470 SimpleImage::size () const
475 FilterBufferImage::FilterBufferImage (AVPixelFormat p, AVFilterBufferRef* b)
482 FilterBufferImage::~FilterBufferImage ()
484 avfilter_unref_buffer (_buffer);
488 FilterBufferImage::data () const
490 return _buffer->data;
494 FilterBufferImage::line_size () const
496 return _buffer->linesize;
500 FilterBufferImage::stride () const
503 return _buffer->linesize;
507 FilterBufferImage::size () const
509 return Size (_buffer->video->w, _buffer->video->h);
512 RGBPlusAlphaImage::RGBPlusAlphaImage (shared_ptr<const Image> im)
513 : SimpleImage (im->pixel_format(), im->size(), false)
515 assert (im->pixel_format() == PIX_FMT_RGBA);
517 _alpha = (uint8_t *) av_malloc (im->size().width * im->size().height);
519 uint8_t* in = im->data()[0];
520 uint8_t* out = data()[0];
521 uint8_t* out_alpha = _alpha;
522 for (int y = 0; y < im->size().height; ++y) {
524 for (int x = 0; x < im->size().width; ++x) {
528 *out_alpha++ = *in_r++;
531 in += im->stride()[0];
535 RGBPlusAlphaImage::~RGBPlusAlphaImage ()