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;
48 Image::swap (Image& other)
50 std::swap (_pixel_format, other._pixel_format);
53 /** @param n Component index.
54 * @return Number of lines in the image for the given component.
57 Image::lines (int n) const
59 switch (_pixel_format) {
64 return size().height / 2;
69 case PIX_FMT_YUV422P10LE:
79 /** @return Number of components */
81 Image::components () const
83 switch (_pixel_format) {
85 case PIX_FMT_YUV422P10LE:
99 Image::scale (Size out_size, Scaler const * scaler, bool aligned) const
103 shared_ptr<Image> scaled (new SimpleImage (pixel_format(), out_size, aligned));
105 struct SwsContext* scale_context = sws_getContext (
106 size().width, size().height, pixel_format(),
107 out_size.width, out_size.height, pixel_format(),
108 scaler->ffmpeg_id (), 0, 0, 0
115 scaled->data(), scaled->stride()
118 sws_freeContext (scale_context);
123 /** Scale this image to a given size and convert it to RGB.
124 * @param out_size Output image size in pixels.
125 * @param scaler Scaler to use.
128 Image::scale_and_convert_to_rgb (Size out_size, int padding, Scaler const * scaler, bool aligned) const
132 Size content_size = out_size;
133 content_size.width -= (padding * 2);
135 shared_ptr<Image> rgb (new SimpleImage (PIX_FMT_RGB24, content_size, aligned));
137 struct SwsContext* scale_context = sws_getContext (
138 size().width, size().height, pixel_format(),
139 content_size.width, content_size.height, PIX_FMT_RGB24,
140 scaler->ffmpeg_id (), 0, 0, 0
143 /* Scale and convert to RGB from whatever its currently in (which may be RGB) */
148 rgb->data(), rgb->stride()
151 /* Put the image in the right place in a black frame if are padding; this is
152 a bit grubby and expensive, but probably inconsequential in the great
156 shared_ptr<Image> padded_rgb (new SimpleImage (PIX_FMT_RGB24, out_size, aligned));
157 padded_rgb->make_black ();
159 /* XXX: we are cheating a bit here; we know the frame is RGB so we can
160 make assumptions about its composition.
162 uint8_t* p = padded_rgb->data()[0] + padding * 3;
163 uint8_t* q = rgb->data()[0];
164 for (int j = 0; j < rgb->lines(0); ++j) {
165 memcpy (p, q, rgb->line_size()[0]);
166 p += padded_rgb->stride()[0];
167 q += rgb->stride()[0];
173 sws_freeContext (scale_context);
178 /** Run a FFmpeg post-process on this image and return the processed version.
179 * @param pp Flags for the required set of post processes.
180 * @return Post-processed image.
183 Image::post_process (string pp, bool aligned) const
185 shared_ptr<Image> out (new SimpleImage (pixel_format(), size (), aligned));
188 switch (pixel_format()) {
189 case PIX_FMT_YUV420P:
190 pp_format = PP_FORMAT_420;
192 case PIX_FMT_YUV422P10LE:
193 case PIX_FMT_YUV422P:
194 pp_format = PP_FORMAT_422;
200 pp_mode* mode = pp_get_mode_by_name_and_quality (pp.c_str (), PP_QUALITY_MAX);
201 pp_context* context = pp_get_context (size().width, size().height, pp_format | PP_CPU_CAPS_MMX2);
204 (const uint8_t **) data(), stride(),
205 out->data(), out->stride(),
206 size().width, size().height,
207 0, 0, mode, context, 0
211 pp_free_context (context);
217 Image::crop (Crop crop, bool aligned) const
219 Size cropped_size = size ();
220 cropped_size.width -= crop.left + crop.right;
221 cropped_size.height -= crop.top + crop.bottom;
223 shared_ptr<Image> out (new SimpleImage (pixel_format(), cropped_size, aligned));
225 for (int c = 0; c < components(); ++c) {
226 int const crop_left_in_bytes = bytes_per_pixel(c) * crop.left;
227 int const cropped_width_in_bytes = bytes_per_pixel(c) * cropped_size.width;
229 /* Start of the source line, cropped from the top but not the left */
230 uint8_t* in_p = data()[c] + crop.top * stride()[c];
231 uint8_t* out_p = out->data()[c];
233 for (int y = 0; y < cropped_size.height; ++y) {
234 memcpy (out_p, in_p + crop_left_in_bytes, cropped_width_in_bytes);
235 in_p += line_size()[c];
236 out_p += out->line_size()[c];
246 switch (_pixel_format) {
247 case PIX_FMT_YUV420P:
248 case PIX_FMT_YUV422P:
249 memset (data()[0], 0, lines(0) * stride()[0]);
250 memset (data()[1], 0x7f, lines(1) * stride()[1]);
251 memset (data()[2], 0x7f, lines(2) * stride()[2]);
254 case PIX_FMT_YUV422P10LE:
255 memset (data()[0], 0, lines(0) * stride()[0]);
256 for (int i = 1; i < 3; ++i) {
257 int16_t* p = reinterpret_cast<int16_t*> (data()[i]);
258 for (int y = 0; y < size().height; ++y) {
259 for (int x = 0; x < line_size()[i] / 2; ++x) {
262 p += stride()[i] / 2;
268 memset (data()[0], 0, lines(0) * stride()[0]);
277 Image::alpha_blend (shared_ptr<const Image> other, Position position)
279 /* Only implemented for RGBA onto RGB24 so far */
280 assert (_pixel_format == PIX_FMT_RGB24 && other->pixel_format() == PIX_FMT_RGBA);
282 int start_tx = position.x;
286 start_ox = -start_tx;
290 int start_ty = position.y;
294 start_oy = -start_ty;
298 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
299 uint8_t* tp = data()[0] + ty * stride()[0] + position.x * 3;
300 uint8_t* op = other->data()[0] + oy * other->stride()[0];
301 for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
302 float const alpha = float (op[3]) / 255;
303 tp[0] = (tp[0] * (1 - alpha)) + op[0] * alpha;
304 tp[1] = (tp[1] * (1 - alpha)) + op[1] * alpha;
305 tp[2] = (tp[2] * (1 - alpha)) + op[2] * alpha;
313 Image::read_from_socket (shared_ptr<Socket> socket)
315 for (int i = 0; i < components(); ++i) {
316 uint8_t* p = data()[i];
317 for (int y = 0; y < lines(i); ++y) {
318 socket->read_definite_and_consume (p, line_size()[i], 30);
325 Image::write_to_socket (shared_ptr<Socket> socket) const
327 for (int i = 0; i < components(); ++i) {
328 uint8_t* p = data()[i];
329 for (int y = 0; y < lines(i); ++y) {
330 socket->write (p, line_size()[i], 30);
338 Image::bytes_per_pixel (int c) const
344 switch (_pixel_format) {
357 case PIX_FMT_YUV420P:
358 case PIX_FMT_YUV422P:
364 case PIX_FMT_YUV422P10LE:
378 /** Construct a SimpleImage of a given size and format, allocating memory
381 * @param p Pixel format.
382 * @param s Size in pixels.
384 SimpleImage::SimpleImage (AVPixelFormat p, Size s, bool aligned)
393 SimpleImage::allocate ()
395 _data = (uint8_t **) av_malloc (4 * sizeof (uint8_t *));
396 _data[0] = _data[1] = _data[2] = _data[3] = 0;
398 _line_size = (int *) av_malloc (4 * sizeof (int));
399 _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
401 _stride = (int *) av_malloc (4 * sizeof (int));
402 _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
404 for (int i = 0; i < components(); ++i) {
405 _line_size[i] = _size.width * bytes_per_pixel(i);
406 _stride[i] = stride_round_up (i, _line_size, _aligned ? 32 : 1);
407 _data[i] = (uint8_t *) av_malloc (_stride[i] * lines (i));
411 SimpleImage::SimpleImage (SimpleImage const & other)
415 _aligned = other._aligned;
419 for (int i = 0; i < components(); ++i) {
420 memcpy (_data[i], other._data[i], _line_size[i] * lines(i));
425 SimpleImage::operator= (SimpleImage const & other)
427 if (this == &other) {
431 SimpleImage tmp (other);
437 SimpleImage::swap (SimpleImage & other)
441 std::swap (_size, other._size);
443 for (int i = 0; i < 4; ++i) {
444 std::swap (_data[i], other._data[i]);
445 std::swap (_line_size[i], other._line_size[i]);
446 std::swap (_stride[i], other._stride[i]);
449 std::swap (_aligned, other._aligned);
452 /** Destroy a SimpleImage */
453 SimpleImage::~SimpleImage ()
455 for (int i = 0; i < components(); ++i) {
460 av_free (_line_size);
465 SimpleImage::data () const
471 SimpleImage::line_size () const
477 SimpleImage::stride () const
483 SimpleImage::size () const
488 FilterBufferImage::FilterBufferImage (AVPixelFormat p, AVFilterBufferRef* b)
495 FilterBufferImage::~FilterBufferImage ()
497 avfilter_unref_buffer (_buffer);
501 FilterBufferImage::data () const
503 return _buffer->data;
507 FilterBufferImage::line_size () const
509 return _buffer->linesize;
513 FilterBufferImage::stride () const
516 return _buffer->linesize;
520 FilterBufferImage::size () const
522 return Size (_buffer->video->w, _buffer->video->h);
525 RGBPlusAlphaImage::RGBPlusAlphaImage (shared_ptr<const Image> im)
526 : SimpleImage (im->pixel_format(), im->size(), false)
528 assert (im->pixel_format() == PIX_FMT_RGBA);
530 _alpha = (uint8_t *) av_malloc (im->size().width * im->size().height);
532 uint8_t* in = im->data()[0];
533 uint8_t* out = data()[0];
534 uint8_t* out_alpha = _alpha;
535 for (int y = 0; y < im->size().height; ++y) {
537 for (int x = 0; x < im->size().width; ++x) {
541 *out_alpha++ = *in_r++;
544 in += im->stride()[0];
548 RGBPlusAlphaImage::~RGBPlusAlphaImage ()