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:
72 case PIX_FMT_YUV444P9BE:
73 case PIX_FMT_YUV444P9LE:
74 case PIX_FMT_YUV444P10BE:
75 case PIX_FMT_YUV444P10LE:
78 throw PixelFormatError ("lines()", _pixel_format);
84 /** @return Number of components */
86 Image::components () const
88 switch (_pixel_format) {
90 case PIX_FMT_YUV422P10LE:
93 case PIX_FMT_YUV444P9BE:
94 case PIX_FMT_YUV444P9LE:
95 case PIX_FMT_YUV444P10BE:
96 case PIX_FMT_YUV444P10LE:
102 throw PixelFormatError ("components()", _pixel_format);
109 Image::scale (libdcp::Size out_size, Scaler const * scaler, bool result_aligned) const
112 /* Empirical testing suggests that sws_scale() will crash if
113 the input image is not aligned.
117 shared_ptr<Image> scaled (new SimpleImage (pixel_format(), out_size, result_aligned));
119 struct SwsContext* scale_context = sws_getContext (
120 size().width, size().height, pixel_format(),
121 out_size.width, out_size.height, pixel_format(),
122 scaler->ffmpeg_id (), 0, 0, 0
129 scaled->data(), scaled->stride()
132 sws_freeContext (scale_context);
137 /** Scale this image to a given size and convert it to RGB.
138 * @param out_size Output image size in pixels.
139 * @param scaler Scaler to use.
142 Image::scale_and_convert_to_rgb (libdcp::Size out_size, int padding, Scaler const * scaler, bool result_aligned) const
145 /* Empirical testing suggests that sws_scale() will crash if
146 the input image is not aligned.
150 libdcp::Size content_size = out_size;
151 content_size.width -= (padding * 2);
153 shared_ptr<Image> rgb (new SimpleImage (PIX_FMT_RGB24, content_size, result_aligned));
155 struct SwsContext* scale_context = sws_getContext (
156 size().width, size().height, pixel_format(),
157 content_size.width, content_size.height, PIX_FMT_RGB24,
158 scaler->ffmpeg_id (), 0, 0, 0
161 /* Scale and convert to RGB from whatever its currently in (which may be RGB) */
166 rgb->data(), rgb->stride()
169 /* Put the image in the right place in a black frame if are padding; this is
170 a bit grubby and expensive, but probably inconsequential in the great
174 shared_ptr<Image> padded_rgb (new SimpleImage (PIX_FMT_RGB24, out_size, result_aligned));
175 padded_rgb->make_black ();
177 /* XXX: we are cheating a bit here; we know the frame is RGB so we can
178 make assumptions about its composition.
180 uint8_t* p = padded_rgb->data()[0] + padding * 3;
181 uint8_t* q = rgb->data()[0];
182 for (int j = 0; j < rgb->lines(0); ++j) {
183 memcpy (p, q, rgb->line_size()[0]);
184 p += padded_rgb->stride()[0];
185 q += rgb->stride()[0];
191 sws_freeContext (scale_context);
196 /** Run a FFmpeg post-process on this image and return the processed version.
197 * @param pp Flags for the required set of post processes.
198 * @return Post-processed image.
201 Image::post_process (string pp, bool aligned) const
203 shared_ptr<Image> out (new SimpleImage (pixel_format(), size (), aligned));
206 switch (pixel_format()) {
207 case PIX_FMT_YUV420P:
208 pp_format = PP_FORMAT_420;
210 case PIX_FMT_YUV422P10LE:
211 case PIX_FMT_YUV422P:
212 pp_format = PP_FORMAT_422;
214 case PIX_FMT_YUV444P:
215 case PIX_FMT_YUV444P9BE:
216 case PIX_FMT_YUV444P9LE:
217 case PIX_FMT_YUV444P10BE:
218 case PIX_FMT_YUV444P10LE:
219 pp_format = PP_FORMAT_444;
221 throw PixelFormatError ("post_process", pixel_format());
224 pp_mode* mode = pp_get_mode_by_name_and_quality (pp.c_str (), PP_QUALITY_MAX);
225 pp_context* context = pp_get_context (size().width, size().height, pp_format | PP_CPU_CAPS_MMX2);
228 (const uint8_t **) data(), stride(),
229 out->data(), out->stride(),
230 size().width, size().height,
231 0, 0, mode, context, 0
235 pp_free_context (context);
241 Image::crop (Crop crop, bool aligned) const
243 libdcp::Size cropped_size = size ();
244 cropped_size.width -= crop.left + crop.right;
245 cropped_size.height -= crop.top + crop.bottom;
247 shared_ptr<Image> out (new SimpleImage (pixel_format(), cropped_size, aligned));
249 for (int c = 0; c < components(); ++c) {
250 int const crop_left_in_bytes = bytes_per_pixel(c) * crop.left;
251 int const cropped_width_in_bytes = bytes_per_pixel(c) * cropped_size.width;
253 /* Start of the source line, cropped from the top but not the left */
254 uint8_t* in_p = data()[c] + crop.top * stride()[c];
255 uint8_t* out_p = out->data()[c];
257 for (int y = 0; y < cropped_size.height; ++y) {
258 memcpy (out_p, in_p + crop_left_in_bytes, cropped_width_in_bytes);
260 out_p += out->stride()[c];
267 /** Blacken a YUV image whose bits per pixel is rounded up to 16 */
269 Image::yuv_16_black (uint16_t v)
271 memset (data()[0], 0, lines(0) * stride()[0]);
272 for (int i = 1; i < 3; ++i) {
273 int16_t* p = reinterpret_cast<int16_t*> (data()[i]);
274 for (int y = 0; y < size().height; ++y) {
275 for (int x = 0; x < line_size()[i] / 2; ++x) {
278 p += stride()[i] / 2;
284 Image::swap_16 (uint16_t v)
286 return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
292 /* U/V black value for 9-bit colour */
293 static uint16_t const nine_bit_uv = (1 << 8) - 1;
295 /* U/V black value for 10-bit colour */
296 static uint16_t const ten_bit_uv = (1 << 9) - 1;
298 switch (_pixel_format) {
299 case PIX_FMT_YUV420P:
300 case PIX_FMT_YUV422P:
301 case PIX_FMT_YUV444P:
302 memset (data()[0], 0, lines(0) * stride()[0]);
303 memset (data()[1], 0x7f, lines(1) * stride()[1]);
304 memset (data()[2], 0x7f, lines(2) * stride()[2]);
307 case PIX_FMT_YUV422P9LE:
308 case PIX_FMT_YUV444P9LE:
309 yuv_16_black (nine_bit_uv);
312 case PIX_FMT_YUV422P9BE:
313 case PIX_FMT_YUV444P9BE:
314 yuv_16_black (swap_16 (nine_bit_uv));
317 case PIX_FMT_YUV422P10LE:
318 case PIX_FMT_YUV444P10LE:
319 yuv_16_black (ten_bit_uv);
322 case PIX_FMT_YUV444P10BE:
323 case PIX_FMT_YUV422P10BE:
324 yuv_16_black (swap_16 (ten_bit_uv));
327 memset (data()[0], 0, lines(0) * stride()[0]);
336 Image::alpha_blend (shared_ptr<const Image> other, Position position)
338 /* Only implemented for RGBA onto RGB24 so far */
339 assert (_pixel_format == PIX_FMT_RGB24 && other->pixel_format() == PIX_FMT_RGBA);
341 int start_tx = position.x;
345 start_ox = -start_tx;
349 int start_ty = position.y;
353 start_oy = -start_ty;
357 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
358 uint8_t* tp = data()[0] + ty * stride()[0] + position.x * 3;
359 uint8_t* op = other->data()[0] + oy * other->stride()[0];
360 for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
361 float const alpha = float (op[3]) / 255;
362 tp[0] = (tp[0] * (1 - alpha)) + op[0] * alpha;
363 tp[1] = (tp[1] * (1 - alpha)) + op[1] * alpha;
364 tp[2] = (tp[2] * (1 - alpha)) + op[2] * alpha;
372 Image::read_from_socket (shared_ptr<Socket> socket)
374 for (int i = 0; i < components(); ++i) {
375 uint8_t* p = data()[i];
376 for (int y = 0; y < lines(i); ++y) {
377 socket->read (p, line_size()[i]);
384 Image::write_to_socket (shared_ptr<Socket> socket) const
386 for (int i = 0; i < components(); ++i) {
387 uint8_t* p = data()[i];
388 for (int y = 0; y < lines(i); ++y) {
389 socket->write (p, line_size()[i]);
397 Image::bytes_per_pixel (int c) const
403 switch (_pixel_format) {
416 case PIX_FMT_YUV420P:
417 case PIX_FMT_YUV422P:
423 case PIX_FMT_YUV422P10LE:
429 case PIX_FMT_YUV444P:
431 case PIX_FMT_YUV444P9BE:
432 case PIX_FMT_YUV444P9LE:
433 case PIX_FMT_YUV444P10LE:
434 case PIX_FMT_YUV444P10BE:
444 /** Construct a SimpleImage of a given size and format, allocating memory
447 * @param p Pixel format.
448 * @param s Size in pixels.
450 SimpleImage::SimpleImage (AVPixelFormat p, libdcp::Size s, bool aligned)
459 SimpleImage::allocate ()
461 _data = (uint8_t **) av_malloc (4 * sizeof (uint8_t *));
462 _data[0] = _data[1] = _data[2] = _data[3] = 0;
464 _line_size = (int *) av_malloc (4 * sizeof (int));
465 _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
467 _stride = (int *) av_malloc (4 * sizeof (int));
468 _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
470 for (int i = 0; i < components(); ++i) {
471 _line_size[i] = _size.width * bytes_per_pixel(i);
472 _stride[i] = stride_round_up (i, _line_size, _aligned ? 32 : 1);
473 _data[i] = (uint8_t *) av_malloc (_stride[i] * lines (i));
477 SimpleImage::SimpleImage (SimpleImage const & other)
481 _aligned = other._aligned;
485 for (int i = 0; i < components(); ++i) {
486 memcpy (_data[i], other._data[i], _line_size[i] * lines(i));
491 SimpleImage::operator= (SimpleImage const & other)
493 if (this == &other) {
497 SimpleImage tmp (other);
503 SimpleImage::swap (SimpleImage & other)
507 std::swap (_size, other._size);
509 for (int i = 0; i < 4; ++i) {
510 std::swap (_data[i], other._data[i]);
511 std::swap (_line_size[i], other._line_size[i]);
512 std::swap (_stride[i], other._stride[i]);
515 std::swap (_aligned, other._aligned);
518 /** Destroy a SimpleImage */
519 SimpleImage::~SimpleImage ()
521 for (int i = 0; i < components(); ++i) {
526 av_free (_line_size);
531 SimpleImage::data () const
537 SimpleImage::line_size () const
543 SimpleImage::stride () const
549 SimpleImage::size () const
555 SimpleImage::aligned () const
560 FilterBufferImage::FilterBufferImage (AVPixelFormat p, AVFilterBufferRef* b)
564 _line_size = (int *) av_malloc (4 * sizeof (int));
565 _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
567 for (int i = 0; i < components(); ++i) {
568 _line_size[i] = size().width * bytes_per_pixel(i);
572 FilterBufferImage::~FilterBufferImage ()
574 avfilter_unref_buffer (_buffer);
575 av_free (_line_size);
579 FilterBufferImage::data () const
581 return _buffer->data;
585 FilterBufferImage::line_size () const
591 FilterBufferImage::stride () const
593 /* I've seen images where the _buffer->linesize is larger than the width
594 (by a small amount), suggesting that _buffer->linesize is what we call
595 stride. But I'm not sure.
597 return _buffer->linesize;
601 FilterBufferImage::size () const
603 return libdcp::Size (_buffer->video->w, _buffer->video->h);
607 FilterBufferImage::aligned () const
613 RGBPlusAlphaImage::RGBPlusAlphaImage (shared_ptr<const Image> im)
614 : SimpleImage (im->pixel_format(), im->size(), false)
616 assert (im->pixel_format() == PIX_FMT_RGBA);
618 _alpha = (uint8_t *) av_malloc (im->size().width * im->size().height);
620 uint8_t* in = im->data()[0];
621 uint8_t* out = data()[0];
622 uint8_t* out_alpha = _alpha;
623 for (int y = 0; y < im->size().height; ++y) {
625 for (int x = 0; x < im->size().width; ++x) {
629 *out_alpha++ = *in_r++;
632 in += im->stride()[0];
636 RGBPlusAlphaImage::~RGBPlusAlphaImage ()