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:
74 throw PixelFormatError ("lines()", _pixel_format);
80 /** @return Number of components */
82 Image::components () const
84 switch (_pixel_format) {
86 case PIX_FMT_YUV422P10LE:
94 throw PixelFormatError ("components()", _pixel_format);
101 Image::scale (libdcp::Size out_size, Scaler const * scaler, bool result_aligned) const
104 /* Empirical testing suggests that sws_scale() will crash if
105 the input image is not aligned.
109 shared_ptr<Image> scaled (new SimpleImage (pixel_format(), out_size, result_aligned));
111 struct SwsContext* scale_context = sws_getContext (
112 size().width, size().height, pixel_format(),
113 out_size.width, out_size.height, pixel_format(),
114 scaler->ffmpeg_id (), 0, 0, 0
121 scaled->data(), scaled->stride()
124 sws_freeContext (scale_context);
129 /** Scale this image to a given size and convert it to RGB.
130 * @param out_size Output image size in pixels.
131 * @param scaler Scaler to use.
134 Image::scale_and_convert_to_rgb (libdcp::Size out_size, int padding, Scaler const * scaler, bool result_aligned) const
137 /* Empirical testing suggests that sws_scale() will crash if
138 the input image is not aligned.
142 libdcp::Size content_size = out_size;
143 content_size.width -= (padding * 2);
145 shared_ptr<Image> rgb (new SimpleImage (PIX_FMT_RGB24, content_size, result_aligned));
147 struct SwsContext* scale_context = sws_getContext (
148 size().width, size().height, pixel_format(),
149 content_size.width, content_size.height, PIX_FMT_RGB24,
150 scaler->ffmpeg_id (), 0, 0, 0
153 /* Scale and convert to RGB from whatever its currently in (which may be RGB) */
158 rgb->data(), rgb->stride()
161 /* Put the image in the right place in a black frame if are padding; this is
162 a bit grubby and expensive, but probably inconsequential in the great
166 shared_ptr<Image> padded_rgb (new SimpleImage (PIX_FMT_RGB24, out_size, result_aligned));
167 padded_rgb->make_black ();
169 /* XXX: we are cheating a bit here; we know the frame is RGB so we can
170 make assumptions about its composition.
172 uint8_t* p = padded_rgb->data()[0] + padding * 3;
173 uint8_t* q = rgb->data()[0];
174 for (int j = 0; j < rgb->lines(0); ++j) {
175 memcpy (p, q, rgb->line_size()[0]);
176 p += padded_rgb->stride()[0];
177 q += rgb->stride()[0];
183 sws_freeContext (scale_context);
188 /** Run a FFmpeg post-process on this image and return the processed version.
189 * @param pp Flags for the required set of post processes.
190 * @return Post-processed image.
193 Image::post_process (string pp, bool aligned) const
195 shared_ptr<Image> out (new SimpleImage (pixel_format(), size (), aligned));
198 switch (pixel_format()) {
199 case PIX_FMT_YUV420P:
200 pp_format = PP_FORMAT_420;
202 case PIX_FMT_YUV422P10LE:
203 case PIX_FMT_YUV422P:
204 pp_format = PP_FORMAT_422;
206 case PIX_FMT_YUV444P:
207 pp_format = PP_FORMAT_444;
209 throw PixelFormatError ("post_process", pixel_format());
212 pp_mode* mode = pp_get_mode_by_name_and_quality (pp.c_str (), PP_QUALITY_MAX);
213 pp_context* context = pp_get_context (size().width, size().height, pp_format | PP_CPU_CAPS_MMX2);
216 (const uint8_t **) data(), stride(),
217 out->data(), out->stride(),
218 size().width, size().height,
219 0, 0, mode, context, 0
223 pp_free_context (context);
229 Image::crop (Crop crop, bool aligned) const
231 libdcp::Size cropped_size = size ();
232 cropped_size.width -= crop.left + crop.right;
233 cropped_size.height -= crop.top + crop.bottom;
235 shared_ptr<Image> out (new SimpleImage (pixel_format(), cropped_size, aligned));
237 for (int c = 0; c < components(); ++c) {
238 int const crop_left_in_bytes = bytes_per_pixel(c) * crop.left;
239 int const cropped_width_in_bytes = bytes_per_pixel(c) * cropped_size.width;
241 /* Start of the source line, cropped from the top but not the left */
242 uint8_t* in_p = data()[c] + crop.top * stride()[c];
243 uint8_t* out_p = out->data()[c];
245 for (int y = 0; y < cropped_size.height; ++y) {
246 memcpy (out_p, in_p + crop_left_in_bytes, cropped_width_in_bytes);
248 out_p += out->stride()[c];
258 switch (_pixel_format) {
259 case PIX_FMT_YUV420P:
260 case PIX_FMT_YUV422P:
261 case PIX_FMT_YUV444P:
262 memset (data()[0], 0, lines(0) * stride()[0]);
263 memset (data()[1], 0x7f, lines(1) * stride()[1]);
264 memset (data()[2], 0x7f, lines(2) * stride()[2]);
267 case PIX_FMT_YUV422P10LE:
268 memset (data()[0], 0, lines(0) * stride()[0]);
269 for (int i = 1; i < 3; ++i) {
270 int16_t* p = reinterpret_cast<int16_t*> (data()[i]);
271 for (int y = 0; y < size().height; ++y) {
272 for (int x = 0; x < line_size()[i] / 2; ++x) {
275 p += stride()[i] / 2;
281 memset (data()[0], 0, lines(0) * stride()[0]);
290 Image::alpha_blend (shared_ptr<const Image> other, Position position)
292 /* Only implemented for RGBA onto RGB24 so far */
293 assert (_pixel_format == PIX_FMT_RGB24 && other->pixel_format() == PIX_FMT_RGBA);
295 int start_tx = position.x;
299 start_ox = -start_tx;
303 int start_ty = position.y;
307 start_oy = -start_ty;
311 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
312 uint8_t* tp = data()[0] + ty * stride()[0] + position.x * 3;
313 uint8_t* op = other->data()[0] + oy * other->stride()[0];
314 for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
315 float const alpha = float (op[3]) / 255;
316 tp[0] = (tp[0] * (1 - alpha)) + op[0] * alpha;
317 tp[1] = (tp[1] * (1 - alpha)) + op[1] * alpha;
318 tp[2] = (tp[2] * (1 - alpha)) + op[2] * alpha;
326 Image::read_from_socket (shared_ptr<Socket> socket)
328 for (int i = 0; i < components(); ++i) {
329 uint8_t* p = data()[i];
330 for (int y = 0; y < lines(i); ++y) {
331 socket->read (p, line_size()[i]);
338 Image::write_to_socket (shared_ptr<Socket> socket) const
340 for (int i = 0; i < components(); ++i) {
341 uint8_t* p = data()[i];
342 for (int y = 0; y < lines(i); ++y) {
343 socket->write (p, line_size()[i]);
351 Image::bytes_per_pixel (int c) const
357 switch (_pixel_format) {
370 case PIX_FMT_YUV420P:
371 case PIX_FMT_YUV422P:
377 case PIX_FMT_YUV422P10LE:
383 case PIX_FMT_YUV444P:
393 /** Construct a SimpleImage of a given size and format, allocating memory
396 * @param p Pixel format.
397 * @param s Size in pixels.
399 SimpleImage::SimpleImage (AVPixelFormat p, libdcp::Size s, bool aligned)
408 SimpleImage::allocate ()
410 _data = (uint8_t **) av_malloc (4 * sizeof (uint8_t *));
411 _data[0] = _data[1] = _data[2] = _data[3] = 0;
413 _line_size = (int *) av_malloc (4 * sizeof (int));
414 _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
416 _stride = (int *) av_malloc (4 * sizeof (int));
417 _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
419 for (int i = 0; i < components(); ++i) {
420 _line_size[i] = _size.width * bytes_per_pixel(i);
421 _stride[i] = stride_round_up (i, _line_size, _aligned ? 32 : 1);
422 _data[i] = (uint8_t *) av_malloc (_stride[i] * lines (i));
426 SimpleImage::SimpleImage (SimpleImage const & other)
430 _aligned = other._aligned;
434 for (int i = 0; i < components(); ++i) {
435 memcpy (_data[i], other._data[i], _line_size[i] * lines(i));
440 SimpleImage::operator= (SimpleImage const & other)
442 if (this == &other) {
446 SimpleImage tmp (other);
452 SimpleImage::swap (SimpleImage & other)
456 std::swap (_size, other._size);
458 for (int i = 0; i < 4; ++i) {
459 std::swap (_data[i], other._data[i]);
460 std::swap (_line_size[i], other._line_size[i]);
461 std::swap (_stride[i], other._stride[i]);
464 std::swap (_aligned, other._aligned);
467 /** Destroy a SimpleImage */
468 SimpleImage::~SimpleImage ()
470 for (int i = 0; i < components(); ++i) {
475 av_free (_line_size);
480 SimpleImage::data () const
486 SimpleImage::line_size () const
492 SimpleImage::stride () const
498 SimpleImage::size () const
504 SimpleImage::aligned () const
509 FilterBufferImage::FilterBufferImage (AVPixelFormat p, AVFilterBufferRef* b)
513 _line_size = (int *) av_malloc (4 * sizeof (int));
514 _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
516 for (int i = 0; i < components(); ++i) {
517 _line_size[i] = size().width * bytes_per_pixel(i);
521 FilterBufferImage::~FilterBufferImage ()
523 avfilter_unref_buffer (_buffer);
524 av_free (_line_size);
528 FilterBufferImage::data () const
530 return _buffer->data;
534 FilterBufferImage::line_size () const
540 FilterBufferImage::stride () const
542 /* I've seen images where the _buffer->linesize is larger than the width
543 (by a small amount), suggesting that _buffer->linesize is what we call
544 stride. But I'm not sure.
546 return _buffer->linesize;
550 FilterBufferImage::size () const
552 return libdcp::Size (_buffer->video->w, _buffer->video->h);
556 FilterBufferImage::aligned () const
562 RGBPlusAlphaImage::RGBPlusAlphaImage (shared_ptr<const Image> im)
563 : SimpleImage (im->pixel_format(), im->size(), false)
565 assert (im->pixel_format() == PIX_FMT_RGBA);
567 _alpha = (uint8_t *) av_malloc (im->size().width * im->size().height);
569 uint8_t* in = im->data()[0];
570 uint8_t* out = data()[0];
571 uint8_t* out_alpha = _alpha;
572 for (int y = 0; y < im->size().height; ++y) {
574 for (int x = 0; x < im->size().width; ++x) {
578 *out_alpha++ = *in_r++;
581 in += im->stride()[0];
585 RGBPlusAlphaImage::~RGBPlusAlphaImage ()