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, bool aligned) const
96 shared_ptr<Image> scaled (new SimpleImage (pixel_format(), out_size, aligned));
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, bool aligned) const
125 Size content_size = out_size;
126 content_size.width -= (padding * 2);
128 shared_ptr<Image> rgb (new SimpleImage (PIX_FMT_RGB24, content_size, aligned));
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 SimpleImage (PIX_FMT_RGB24, out_size, aligned));
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, bool aligned) const
178 shared_ptr<Image> out (new SimpleImage (pixel_format(), size (), aligned));
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<const 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, bool aligned)
301 _data = (uint8_t **) av_malloc (4 * sizeof (uint8_t *));
302 _data[0] = _data[1] = _data[2] = _data[3] = 0;
304 _line_size = (int *) av_malloc (4 * sizeof (int));
305 _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
307 _stride = (int *) av_malloc (4 * sizeof (int));
308 _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
312 _line_size[0] = s.width * 3;
315 _line_size[0] = s.width * 4;
317 case PIX_FMT_YUV420P:
318 case PIX_FMT_YUV422P:
319 _line_size[0] = s.width;
320 _line_size[1] = s.width / 2;
321 _line_size[2] = s.width / 2;
323 case PIX_FMT_YUV422P10LE:
324 _line_size[0] = s.width * 2;
325 _line_size[1] = s.width;
326 _line_size[2] = s.width;
332 for (int i = 0; i < components(); ++i) {
333 _stride[i] = stride_round_up (i, _line_size, _aligned ? 32 : 1);
334 _data[i] = (uint8_t *) av_malloc (_stride[i] * lines (i));
338 /** Destroy a SimpleImage */
339 SimpleImage::~SimpleImage ()
341 for (int i = 0; i < components(); ++i) {
346 av_free (_line_size);
350 SimpleImage::SimpleImage (shared_ptr<const Image> im, bool aligned)
351 : Image (im->pixel_format())
353 assert (components() == im->components());
355 for (int c = 0; c < components(); ++c) {
357 assert (line_size()[c] == im->line_size()[c]);
359 uint8_t* t = data()[c];
360 uint8_t* o = im->data()[c];
362 for (int y = 0; y < lines(c); ++y) {
363 memcpy (t, o, line_size()[c]);
365 o += im->stride()[c];
371 SimpleImage::data () const
377 SimpleImage::line_size () const
383 SimpleImage::stride () const
389 SimpleImage::size () const
394 FilterBufferImage::FilterBufferImage (AVPixelFormat p, AVFilterBufferRef* b)
401 FilterBufferImage::~FilterBufferImage ()
403 avfilter_unref_buffer (_buffer);
407 FilterBufferImage::data () const
409 return _buffer->data;
413 FilterBufferImage::line_size () const
415 return _buffer->linesize;
419 FilterBufferImage::stride () const
422 return _buffer->linesize;
426 FilterBufferImage::size () const
428 return Size (_buffer->video->w, _buffer->video->h);
431 RGBPlusAlphaImage::RGBPlusAlphaImage (shared_ptr<const Image> im)
432 : SimpleImage (im->pixel_format(), im->size(), false)
434 assert (im->pixel_format() == PIX_FMT_RGBA);
436 _alpha = (uint8_t *) av_malloc (im->size().width * im->size().height);
438 uint8_t* in = im->data()[0];
439 uint8_t* out = data()[0];
440 uint8_t* out_alpha = _alpha;
441 for (int y = 0; y < im->size().height; ++y) {
443 for (int x = 0; x < im->size().width; ++x) {
447 *out_alpha++ = *in_r++;
450 in += im->stride()[0];
454 RGBPlusAlphaImage::~RGBPlusAlphaImage ()