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 cout << "scale to " << out_size.width << "x" << out_size.height << "\n";
138 struct SwsContext* scale_context = sws_getContext (
139 size().width, size().height, pixel_format(),
140 content_size.width, content_size.height, PIX_FMT_RGB24,
141 scaler->ffmpeg_id (), 0, 0, 0
144 /* Scale and convert to RGB from whatever its currently in (which may be RGB) */
149 rgb->data(), rgb->stride()
152 /* Put the image in the right place in a black frame if are padding; this is
153 a bit grubby and expensive, but probably inconsequential in the great
157 shared_ptr<Image> padded_rgb (new SimpleImage (PIX_FMT_RGB24, out_size, aligned));
158 padded_rgb->make_black ();
160 /* XXX: we are cheating a bit here; we know the frame is RGB so we can
161 make assumptions about its composition.
163 uint8_t* p = padded_rgb->data()[0] + padding * 3;
164 uint8_t* q = rgb->data()[0];
165 for (int j = 0; j < rgb->lines(0); ++j) {
166 memcpy (p, q, rgb->line_size()[0]);
167 p += padded_rgb->stride()[0];
168 q += rgb->stride()[0];
174 sws_freeContext (scale_context);
179 /** Run a FFmpeg post-process on this image and return the processed version.
180 * @param pp Flags for the required set of post processes.
181 * @return Post-processed image.
184 Image::post_process (string pp, bool aligned) const
186 shared_ptr<Image> out (new SimpleImage (pixel_format(), size (), aligned));
189 switch (pixel_format()) {
190 case PIX_FMT_YUV420P:
191 pp_format = PP_FORMAT_420;
193 case PIX_FMT_YUV422P10LE:
194 case PIX_FMT_YUV422P:
195 pp_format = PP_FORMAT_422;
201 pp_mode* mode = pp_get_mode_by_name_and_quality (pp.c_str (), PP_QUALITY_MAX);
202 pp_context* context = pp_get_context (size().width, size().height, pp_format | PP_CPU_CAPS_MMX2);
205 (const uint8_t **) data(), stride(),
206 out->data(), out->stride(),
207 size().width, size().height,
208 0, 0, mode, context, 0
212 pp_free_context (context);
218 Image::crop (Crop crop, bool aligned) const
220 Size cropped_size = size ();
221 cropped_size.width -= crop.left + crop.right;
222 cropped_size.height -= crop.top + crop.bottom;
224 shared_ptr<Image> out (new SimpleImage (pixel_format(), cropped_size, aligned));
226 for (int c = 0; c < components(); ++c) {
227 int const crop_left_in_bytes = bytes_per_pixel(c) * crop.left;
228 int const cropped_width_in_bytes = bytes_per_pixel(c) * cropped_size.width;
230 /* Start of the source line, cropped from the top but not the left */
231 uint8_t* in_p = data()[c] + crop.top * stride()[c];
232 uint8_t* out_p = out->data()[c];
234 for (int y = 0; y < cropped_size.height; ++y) {
235 memcpy (out_p, in_p + crop_left_in_bytes, cropped_width_in_bytes);
236 in_p += line_size()[c];
237 out_p += out->line_size()[c];
247 switch (_pixel_format) {
248 case PIX_FMT_YUV420P:
249 case PIX_FMT_YUV422P10LE:
250 case PIX_FMT_YUV422P:
251 memset (data()[0], 0, lines(0) * stride()[0]);
252 memset (data()[1], 0x80, lines(1) * stride()[1]);
253 memset (data()[2], 0x80, lines(2) * stride()[2]);
257 memset (data()[0], 0, lines(0) * stride()[0]);
266 Image::alpha_blend (shared_ptr<const Image> other, Position position)
268 /* Only implemented for RGBA onto RGB24 so far */
269 assert (_pixel_format == PIX_FMT_RGB24 && other->pixel_format() == PIX_FMT_RGBA);
271 int start_tx = position.x;
275 start_ox = -start_tx;
279 int start_ty = position.y;
283 start_oy = -start_ty;
287 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
288 uint8_t* tp = data()[0] + ty * stride()[0] + position.x * 3;
289 uint8_t* op = other->data()[0] + oy * other->stride()[0];
290 for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
291 float const alpha = float (op[3]) / 255;
292 tp[0] = (tp[0] * (1 - alpha)) + op[0] * alpha;
293 tp[1] = (tp[1] * (1 - alpha)) + op[1] * alpha;
294 tp[2] = (tp[2] * (1 - alpha)) + op[2] * alpha;
302 Image::read_from_socket (shared_ptr<Socket> socket)
304 for (int i = 0; i < components(); ++i) {
305 uint8_t* p = data()[i];
306 for (int y = 0; y < lines(i); ++y) {
307 socket->read_definite_and_consume (p, line_size()[i], 30);
314 Image::write_to_socket (shared_ptr<Socket> socket) const
316 for (int i = 0; i < components(); ++i) {
317 uint8_t* p = data()[i];
318 for (int y = 0; y < lines(i); ++y) {
319 socket->write (p, line_size()[i], 30);
327 Image::bytes_per_pixel (int c) const
333 switch (_pixel_format) {
346 case PIX_FMT_YUV420P:
347 case PIX_FMT_YUV422P:
353 case PIX_FMT_YUV422P10LE:
367 /** Construct a SimpleImage of a given size and format, allocating memory
370 * @param p Pixel format.
371 * @param s Size in pixels.
373 SimpleImage::SimpleImage (AVPixelFormat p, Size s, bool aligned)
382 SimpleImage::allocate ()
384 _data = (uint8_t **) av_malloc (4 * sizeof (uint8_t *));
385 _data[0] = _data[1] = _data[2] = _data[3] = 0;
387 _line_size = (int *) av_malloc (4 * sizeof (int));
388 _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
390 _stride = (int *) av_malloc (4 * sizeof (int));
391 _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
393 for (int i = 0; i < components(); ++i) {
394 _line_size[i] = _size.width * bytes_per_pixel(i);
395 _stride[i] = stride_round_up (i, _line_size, _aligned ? 32 : 1);
396 _data[i] = (uint8_t *) av_malloc (_stride[i] * lines (i));
400 SimpleImage::SimpleImage (SimpleImage const & other)
404 _aligned = other._aligned;
408 for (int i = 0; i < components(); ++i) {
409 memcpy (_data[i], other._data[i], _line_size[i] * lines(i));
414 SimpleImage::operator= (SimpleImage const & other)
416 if (this == &other) {
420 SimpleImage tmp (other);
426 SimpleImage::swap (SimpleImage & other)
430 assert (_size == other._size);
431 assert (_aligned == other._aligned);
433 std::swap (_size, other._size);
435 for (int i = 0; i < 4; ++i) {
436 std::swap (_data[i], other._data[i]);
437 std::swap (_line_size[i], other._line_size[i]);
438 std::swap (_stride[i], other._stride[i]);
441 std::swap (_aligned, other._aligned);
444 /** Destroy a SimpleImage */
445 SimpleImage::~SimpleImage ()
447 for (int i = 0; i < components(); ++i) {
452 av_free (_line_size);
456 SimpleImage::SimpleImage (shared_ptr<const Image> im, bool aligned)
457 : Image (im->pixel_format())
459 assert (components() == im->components());
461 for (int c = 0; c < components(); ++c) {
463 assert (line_size()[c] == im->line_size()[c]);
465 uint8_t* t = data()[c];
466 uint8_t* o = im->data()[c];
468 for (int y = 0; y < lines(c); ++y) {
469 memcpy (t, o, line_size()[c]);
471 o += im->stride()[c];
477 SimpleImage::data () const
483 SimpleImage::line_size () const
489 SimpleImage::stride () const
495 SimpleImage::size () const
500 FilterBufferImage::FilterBufferImage (AVPixelFormat p, AVFilterBufferRef* b)
507 FilterBufferImage::~FilterBufferImage ()
509 avfilter_unref_buffer (_buffer);
513 FilterBufferImage::data () const
515 return _buffer->data;
519 FilterBufferImage::line_size () const
521 return _buffer->linesize;
525 FilterBufferImage::stride () const
528 return _buffer->linesize;
532 FilterBufferImage::size () const
534 return Size (_buffer->video->w, _buffer->video->h);
537 RGBPlusAlphaImage::RGBPlusAlphaImage (shared_ptr<const Image> im)
538 : SimpleImage (im->pixel_format(), im->size(), false)
540 assert (im->pixel_format() == PIX_FMT_RGBA);
542 _alpha = (uint8_t *) av_malloc (im->size().width * im->size().height);
544 uint8_t* in = im->data()[0];
545 uint8_t* out = data()[0];
546 uint8_t* out_alpha = _alpha;
547 for (int y = 0; y < im->size().height; ++y) {
549 for (int x = 0; x < im->size().width; ++x) {
553 *out_alpha++ = *in_r++;
556 in += im->stride()[0];
560 RGBPlusAlphaImage::~RGBPlusAlphaImage ()