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"
46 using namespace boost;
50 Image::swap (Image& other)
52 std::swap (_pixel_format, other._pixel_format);
55 /** @param n Component index.
56 * @return Number of lines in the image for the given component.
59 Image::lines (int n) const
61 switch (_pixel_format) {
66 return size().height / 2;
71 case PIX_FMT_YUV422P10LE:
74 case PIX_FMT_YUV444P9BE:
75 case PIX_FMT_YUV444P9LE:
76 case PIX_FMT_YUV444P10BE:
77 case PIX_FMT_YUV444P10LE:
81 throw PixelFormatError (N_("lines()"), _pixel_format);
87 /** @return Number of components */
89 Image::components () const
91 switch (_pixel_format) {
93 case PIX_FMT_YUV422P9BE:
94 case PIX_FMT_YUV422P9LE:
95 case PIX_FMT_YUV422P10BE:
96 case PIX_FMT_YUV422P10LE:
99 case PIX_FMT_YUV444P9BE:
100 case PIX_FMT_YUV444P9LE:
101 case PIX_FMT_YUV444P10BE:
102 case PIX_FMT_YUV444P10LE:
106 case PIX_FMT_UYVY422:
109 throw PixelFormatError (N_("components()"), _pixel_format);
116 Image::scale (libdcp::Size out_size, Scaler const * scaler, bool result_aligned) const
119 /* Empirical testing suggests that sws_scale() will crash if
120 the input image is not aligned.
124 shared_ptr<Image> scaled (new SimpleImage (pixel_format(), out_size, result_aligned));
126 struct SwsContext* scale_context = sws_getContext (
127 size().width, size().height, pixel_format(),
128 out_size.width, out_size.height, pixel_format(),
129 scaler->ffmpeg_id (), 0, 0, 0
136 scaled->data(), scaled->stride()
139 sws_freeContext (scale_context);
144 /** Scale this image to a given size and convert it to RGB.
145 * @param out_size Output image size in pixels.
146 * @param scaler Scaler to use.
149 Image::scale_and_convert_to_rgb (libdcp::Size out_size, int padding, Scaler const * scaler, bool result_aligned) const
152 /* Empirical testing suggests that sws_scale() will crash if
153 the input image is not aligned.
157 libdcp::Size content_size = out_size;
158 content_size.width -= (padding * 2);
160 shared_ptr<Image> rgb (new SimpleImage (PIX_FMT_RGB24, content_size, result_aligned));
162 struct SwsContext* scale_context = sws_getContext (
163 size().width, size().height, pixel_format(),
164 content_size.width, content_size.height, PIX_FMT_RGB24,
165 scaler->ffmpeg_id (), 0, 0, 0
168 /* Scale and convert to RGB from whatever its currently in (which may be RGB) */
173 rgb->data(), rgb->stride()
176 /* Put the image in the right place in a black frame if are padding; this is
177 a bit grubby and expensive, but probably inconsequential in the great
181 shared_ptr<Image> padded_rgb (new SimpleImage (PIX_FMT_RGB24, out_size, result_aligned));
182 padded_rgb->make_black ();
184 /* XXX: we are cheating a bit here; we know the frame is RGB so we can
185 make assumptions about its composition.
187 uint8_t* p = padded_rgb->data()[0] + padding * 3;
188 uint8_t* q = rgb->data()[0];
189 for (int j = 0; j < rgb->lines(0); ++j) {
190 memcpy (p, q, rgb->line_size()[0]);
191 p += padded_rgb->stride()[0];
192 q += rgb->stride()[0];
198 sws_freeContext (scale_context);
203 /** Run a FFmpeg post-process on this image and return the processed version.
204 * @param pp Flags for the required set of post processes.
205 * @return Post-processed image.
208 Image::post_process (string pp, bool aligned) const
210 shared_ptr<Image> out (new SimpleImage (pixel_format(), size (), aligned));
213 switch (pixel_format()) {
214 case PIX_FMT_YUV420P:
215 pp_format = PP_FORMAT_420;
217 case PIX_FMT_YUV422P10LE:
218 case PIX_FMT_YUV422P:
219 case PIX_FMT_UYVY422:
220 pp_format = PP_FORMAT_422;
222 case PIX_FMT_YUV444P:
223 case PIX_FMT_YUV444P9BE:
224 case PIX_FMT_YUV444P9LE:
225 case PIX_FMT_YUV444P10BE:
226 case PIX_FMT_YUV444P10LE:
227 pp_format = PP_FORMAT_444;
229 throw PixelFormatError (N_("post_process"), pixel_format());
232 pp_mode* mode = pp_get_mode_by_name_and_quality (pp.c_str (), PP_QUALITY_MAX);
233 pp_context* context = pp_get_context (size().width, size().height, pp_format | PP_CPU_CAPS_MMX2);
236 (const uint8_t **) data(), stride(),
237 out->data(), out->stride(),
238 size().width, size().height,
239 0, 0, mode, context, 0
243 pp_free_context (context);
249 Image::crop (Crop crop, bool aligned) const
251 libdcp::Size cropped_size = size ();
252 cropped_size.width -= crop.left + crop.right;
253 cropped_size.height -= crop.top + crop.bottom;
255 shared_ptr<Image> out (new SimpleImage (pixel_format(), cropped_size, aligned));
257 for (int c = 0; c < components(); ++c) {
258 int const crop_left_in_bytes = bytes_per_pixel(c) * crop.left;
259 int const cropped_width_in_bytes = bytes_per_pixel(c) * cropped_size.width;
261 /* Start of the source line, cropped from the top but not the left */
262 uint8_t* in_p = data()[c] + crop.top * stride()[c];
263 uint8_t* out_p = out->data()[c];
265 for (int y = 0; y < cropped_size.height; ++y) {
266 memcpy (out_p, in_p + crop_left_in_bytes, cropped_width_in_bytes);
268 out_p += out->stride()[c];
275 /** Blacken a YUV image whose bits per pixel is rounded up to 16 */
277 Image::yuv_16_black (uint16_t v)
279 memset (data()[0], 0, lines(0) * stride()[0]);
280 for (int i = 1; i < 3; ++i) {
281 int16_t* p = reinterpret_cast<int16_t*> (data()[i]);
282 for (int y = 0; y < size().height; ++y) {
283 for (int x = 0; x < line_size()[i] / 2; ++x) {
286 p += stride()[i] / 2;
292 Image::swap_16 (uint16_t v)
294 return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
300 /* U/V black value for 8-bit colour */
301 static uint8_t const eight_bit_uv = (1 << 7) - 1;
303 /* U/V black value for 9-bit colour */
304 static uint16_t const nine_bit_uv = (1 << 8) - 1;
306 /* U/V black value for 10-bit colour */
307 static uint16_t const ten_bit_uv = (1 << 9) - 1;
309 switch (_pixel_format) {
310 case PIX_FMT_YUV420P:
311 case PIX_FMT_YUV422P:
312 case PIX_FMT_YUV444P:
313 memset (data()[0], 0, lines(0) * stride()[0]);
314 memset (data()[1], eight_bit_uv, lines(1) * stride()[1]);
315 memset (data()[2], eight_bit_uv, lines(2) * stride()[2]);
318 case PIX_FMT_YUV422P9LE:
319 case PIX_FMT_YUV444P9LE:
320 yuv_16_black (nine_bit_uv);
323 case PIX_FMT_YUV422P9BE:
324 case PIX_FMT_YUV444P9BE:
325 yuv_16_black (swap_16 (nine_bit_uv));
328 case PIX_FMT_YUV422P10LE:
329 case PIX_FMT_YUV444P10LE:
330 yuv_16_black (ten_bit_uv);
333 case PIX_FMT_YUV444P10BE:
334 case PIX_FMT_YUV422P10BE:
335 yuv_16_black (swap_16 (ten_bit_uv));
338 memset (data()[0], 0, lines(0) * stride()[0]);
341 case PIX_FMT_UYVY422:
343 int const Y = lines(0);
344 int const X = line_size()[0];
345 uint8_t* p = data()[0];
346 for (int y = 0; y < Y; ++y) {
347 for (int x = 0; x < X / 4; ++x) {
348 *p++ = eight_bit_uv; // Cb
350 *p++ = eight_bit_uv; // Cr
358 throw PixelFormatError (N_("make_black()"), _pixel_format);
363 Image::alpha_blend (shared_ptr<const Image> other, Position position)
365 /* Only implemented for RGBA onto RGB24 so far */
366 assert (_pixel_format == PIX_FMT_RGB24 && other->pixel_format() == PIX_FMT_RGBA);
368 int start_tx = position.x;
372 start_ox = -start_tx;
376 int start_ty = position.y;
380 start_oy = -start_ty;
384 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
385 uint8_t* tp = data()[0] + ty * stride()[0] + position.x * 3;
386 uint8_t* op = other->data()[0] + oy * other->stride()[0];
387 for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
388 float const alpha = float (op[3]) / 255;
389 tp[0] = (tp[0] * (1 - alpha)) + op[0] * alpha;
390 tp[1] = (tp[1] * (1 - alpha)) + op[1] * alpha;
391 tp[2] = (tp[2] * (1 - alpha)) + op[2] * alpha;
399 Image::read_from_socket (shared_ptr<Socket> socket)
401 for (int i = 0; i < components(); ++i) {
402 uint8_t* p = data()[i];
403 for (int y = 0; y < lines(i); ++y) {
404 socket->read (p, line_size()[i]);
411 Image::write_to_socket (shared_ptr<Socket> socket) const
413 for (int i = 0; i < components(); ++i) {
414 uint8_t* p = data()[i];
415 for (int y = 0; y < lines(i); ++y) {
416 socket->write (p, line_size()[i]);
424 Image::bytes_per_pixel (int c) const
430 switch (_pixel_format) {
443 case PIX_FMT_YUV420P:
444 case PIX_FMT_YUV422P:
450 case PIX_FMT_YUV422P10LE:
456 case PIX_FMT_UYVY422:
458 case PIX_FMT_YUV444P:
460 case PIX_FMT_YUV444P9BE:
461 case PIX_FMT_YUV444P9LE:
462 case PIX_FMT_YUV444P10LE:
463 case PIX_FMT_YUV444P10BE:
466 throw PixelFormatError (N_("bytes_per_pixel()"), _pixel_format);
473 /** Construct a SimpleImage of a given size and format, allocating memory
476 * @param p Pixel format.
477 * @param s Size in pixels.
479 SimpleImage::SimpleImage (AVPixelFormat p, libdcp::Size s, bool aligned)
488 SimpleImage::allocate ()
490 _data = (uint8_t **) av_malloc (4 * sizeof (uint8_t *));
491 _data[0] = _data[1] = _data[2] = _data[3] = 0;
493 _line_size = (int *) av_malloc (4 * sizeof (int));
494 _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
496 _stride = (int *) av_malloc (4 * sizeof (int));
497 _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
499 for (int i = 0; i < components(); ++i) {
500 _line_size[i] = _size.width * bytes_per_pixel(i);
501 _stride[i] = stride_round_up (i, _line_size, _aligned ? 32 : 1);
502 _data[i] = (uint8_t *) av_malloc (_stride[i] * lines (i));
506 SimpleImage::SimpleImage (SimpleImage const & other)
510 _aligned = other._aligned;
514 for (int i = 0; i < components(); ++i) {
515 uint8_t* p = _data[i];
516 uint8_t* q = other._data[i];
517 for (int j = 0; j < lines(i); ++j) {
518 memcpy (p, q, _line_size[i]);
520 q += other.stride()[i];
525 SimpleImage::SimpleImage (shared_ptr<const Image> other)
526 : Image (*other.get())
528 _size = other->size ();
533 for (int i = 0; i < components(); ++i) {
534 assert(line_size()[i] == other->line_size()[i]);
535 uint8_t* p = _data[i];
536 uint8_t* q = other->data()[i];
537 for (int j = 0; j < lines(i); ++j) {
538 memcpy (p, q, line_size()[i]);
540 q += other->stride()[i];
546 SimpleImage::operator= (SimpleImage const & other)
548 if (this == &other) {
552 SimpleImage tmp (other);
558 SimpleImage::swap (SimpleImage & other)
562 std::swap (_size, other._size);
564 for (int i = 0; i < 4; ++i) {
565 std::swap (_data[i], other._data[i]);
566 std::swap (_line_size[i], other._line_size[i]);
567 std::swap (_stride[i], other._stride[i]);
570 std::swap (_aligned, other._aligned);
573 /** Destroy a SimpleImage */
574 SimpleImage::~SimpleImage ()
576 for (int i = 0; i < components(); ++i) {
581 av_free (_line_size);
586 SimpleImage::data () const
592 SimpleImage::line_size () const
598 SimpleImage::stride () const
604 SimpleImage::size () const
610 SimpleImage::aligned () const
615 FrameImage::FrameImage (AVFrame* frame)
616 : Image (static_cast<AVPixelFormat> (frame->format))
619 _line_size = (int *) av_malloc (4 * sizeof (int));
620 _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
622 for (int i = 0; i < components(); ++i) {
623 _line_size[i] = size().width * bytes_per_pixel(i);
627 FrameImage::~FrameImage ()
629 av_frame_free (&_frame);
630 av_free (_line_size);
634 FrameImage::data () const
640 FrameImage::line_size () const
646 FrameImage::stride () const
648 /* AVFrame's `linesize' is what we call `stride' */
649 return _frame->linesize;
653 FrameImage::size () const
655 return libdcp::Size (_frame->width, _frame->height);
659 FrameImage::aligned () const
664 RGBPlusAlphaImage::RGBPlusAlphaImage (shared_ptr<const Image> im)
665 : SimpleImage (im->pixel_format(), im->size(), false)
667 assert (im->pixel_format() == PIX_FMT_RGBA);
669 _alpha = (uint8_t *) av_malloc (im->size().width * im->size().height);
671 uint8_t* in = im->data()[0];
672 uint8_t* out = data()[0];
673 uint8_t* out_alpha = _alpha;
674 for (int y = 0; y < im->size().height; ++y) {
676 for (int x = 0; x < im->size().width; ++x) {
680 *out_alpha++ = *in_r++;
683 in += im->stride()[0];
687 RGBPlusAlphaImage::~RGBPlusAlphaImage ()