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>
38 #include <libavutil/pixdesc.h>
41 #include "exceptions.h"
47 using namespace boost;
51 Image::swap (Image& other)
53 std::swap (_pixel_format, other._pixel_format);
56 /** @param n Component index.
57 * @return Number of lines in the image for the given component.
60 Image::lines (int n) const
66 AVPixFmtDescriptor const * d = av_pix_fmt_desc_get(_pixel_format);
68 throw PixelFormatError (N_("lines()"), _pixel_format);
71 return size().height / pow(2, d->log2_chroma_h);
74 /** @return Number of components */
76 Image::components () const
78 AVPixFmtDescriptor const * d = av_pix_fmt_desc_get(_pixel_format);
80 throw PixelFormatError (N_("components()"), _pixel_format);
83 if ((d->flags & PIX_FMT_PLANAR) == 0) {
87 return d->nb_components;
91 Image::scale (libdcp::Size out_size, Scaler const * scaler, bool result_aligned) const
94 /* Empirical testing suggests that sws_scale() will crash if
95 the input image is not aligned.
99 shared_ptr<Image> scaled (new SimpleImage (pixel_format(), out_size, result_aligned));
101 struct SwsContext* scale_context = sws_getContext (
102 size().width, size().height, pixel_format(),
103 out_size.width, out_size.height, pixel_format(),
104 scaler->ffmpeg_id (), 0, 0, 0
111 scaled->data(), scaled->stride()
114 sws_freeContext (scale_context);
119 /** Scale this image to a given size and convert it to RGB.
120 * @param out_size Output image size in pixels.
121 * @param scaler Scaler to use.
124 Image::scale_and_convert_to_rgb (libdcp::Size out_size, int padding, Scaler const * scaler, bool result_aligned) const
127 /* Empirical testing suggests that sws_scale() will crash if
128 the input image is not aligned.
132 libdcp::Size content_size = out_size;
133 content_size.width -= (padding * 2);
135 shared_ptr<Image> rgb (new SimpleImage (PIX_FMT_RGB24, content_size, result_aligned));
137 struct SwsContext* scale_context = sws_getContext (
138 size().width, size().height, pixel_format(),
139 content_size.width, content_size.height, PIX_FMT_RGB24,
140 scaler->ffmpeg_id (), 0, 0, 0
143 /* Scale and convert to RGB from whatever its currently in (which may be RGB) */
148 rgb->data(), rgb->stride()
151 /* Put the image in the right place in a black frame if are padding; this is
152 a bit grubby and expensive, but probably inconsequential in the great
156 shared_ptr<Image> padded_rgb (new SimpleImage (PIX_FMT_RGB24, out_size, result_aligned));
157 padded_rgb->make_black ();
159 /* XXX: we are cheating a bit here; we know the frame is RGB so we can
160 make assumptions about its composition.
162 uint8_t* p = padded_rgb->data()[0] + padding * 3;
163 uint8_t* q = rgb->data()[0];
164 for (int j = 0; j < rgb->lines(0); ++j) {
165 memcpy (p, q, rgb->line_size()[0]);
166 p += padded_rgb->stride()[0];
167 q += rgb->stride()[0];
173 sws_freeContext (scale_context);
178 /** Run a FFmpeg post-process on this image and return the processed version.
179 * @param pp Flags for the required set of post processes.
180 * @return Post-processed image.
183 Image::post_process (string pp, bool aligned) const
185 shared_ptr<Image> out (new SimpleImage (pixel_format(), size (), aligned));
188 switch (pixel_format()) {
189 case PIX_FMT_YUV420P:
190 pp_format = PP_FORMAT_420;
192 case PIX_FMT_YUV422P10LE:
193 case PIX_FMT_YUV422P:
194 case PIX_FMT_UYVY422:
195 pp_format = PP_FORMAT_422;
197 case PIX_FMT_YUV444P:
198 case PIX_FMT_YUV444P9BE:
199 case PIX_FMT_YUV444P9LE:
200 case PIX_FMT_YUV444P10BE:
201 case PIX_FMT_YUV444P10LE:
202 pp_format = PP_FORMAT_444;
204 throw PixelFormatError (N_("post_process"), pixel_format());
207 pp_mode* mode = pp_get_mode_by_name_and_quality (pp.c_str (), PP_QUALITY_MAX);
208 pp_context* context = pp_get_context (size().width, size().height, pp_format | PP_CPU_CAPS_MMX2);
211 (const uint8_t **) data(), stride(),
212 out->data(), out->stride(),
213 size().width, size().height,
214 0, 0, mode, context, 0
218 pp_free_context (context);
224 Image::crop (Crop crop, bool aligned) const
226 libdcp::Size cropped_size = size ();
227 cropped_size.width -= crop.left + crop.right;
228 cropped_size.height -= crop.top + crop.bottom;
230 shared_ptr<Image> out (new SimpleImage (pixel_format(), cropped_size, aligned));
232 for (int c = 0; c < components(); ++c) {
233 int const crop_left_in_bytes = bytes_per_pixel(c) * crop.left;
234 int const cropped_width_in_bytes = bytes_per_pixel(c) * cropped_size.width;
236 /* Start of the source line, cropped from the top but not the left */
237 uint8_t* in_p = data()[c] + crop.top * stride()[c];
238 uint8_t* out_p = out->data()[c];
240 for (int y = 0; y < cropped_size.height; ++y) {
241 memcpy (out_p, in_p + crop_left_in_bytes, cropped_width_in_bytes);
243 out_p += out->stride()[c];
250 /** Blacken a YUV image whose bits per pixel is rounded up to 16 */
252 Image::yuv_16_black (uint16_t v)
254 memset (data()[0], 0, lines(0) * stride()[0]);
255 for (int i = 1; i < 3; ++i) {
256 int16_t* p = reinterpret_cast<int16_t*> (data()[i]);
257 for (int y = 0; y < size().height; ++y) {
258 for (int x = 0; x < line_size()[i] / 2; ++x) {
261 p += stride()[i] / 2;
267 Image::swap_16 (uint16_t v)
269 return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
275 /* U/V black value for 8-bit colour */
276 static uint8_t const eight_bit_uv = (1 << 7) - 1;
278 /* U/V black value for 9-bit colour */
279 static uint16_t const nine_bit_uv = (1 << 8) - 1;
281 /* U/V black value for 10-bit colour */
282 static uint16_t const ten_bit_uv = (1 << 9) - 1;
284 switch (_pixel_format) {
285 case PIX_FMT_YUV420P:
286 case PIX_FMT_YUV422P:
287 case PIX_FMT_YUV444P:
288 memset (data()[0], 0, lines(0) * stride()[0]);
289 memset (data()[1], eight_bit_uv, lines(1) * stride()[1]);
290 memset (data()[2], eight_bit_uv, lines(2) * stride()[2]);
293 case PIX_FMT_YUV422P9LE:
294 case PIX_FMT_YUV444P9LE:
295 yuv_16_black (nine_bit_uv);
298 case PIX_FMT_YUV422P9BE:
299 case PIX_FMT_YUV444P9BE:
300 yuv_16_black (swap_16 (nine_bit_uv));
303 case PIX_FMT_YUV422P10LE:
304 case PIX_FMT_YUV444P10LE:
305 yuv_16_black (ten_bit_uv);
308 case PIX_FMT_YUV444P10BE:
309 case PIX_FMT_YUV422P10BE:
310 yuv_16_black (swap_16 (ten_bit_uv));
313 memset (data()[0], 0, lines(0) * stride()[0]);
316 case PIX_FMT_UYVY422:
318 int const Y = lines(0);
319 int const X = line_size()[0];
320 uint8_t* p = data()[0];
321 for (int y = 0; y < Y; ++y) {
322 for (int x = 0; x < X / 4; ++x) {
323 *p++ = eight_bit_uv; // Cb
325 *p++ = eight_bit_uv; // Cr
333 throw PixelFormatError (N_("make_black()"), _pixel_format);
338 Image::alpha_blend (shared_ptr<const Image> other, Position position)
340 /* Only implemented for RGBA onto RGB24 so far */
341 assert (_pixel_format == PIX_FMT_RGB24 && other->pixel_format() == PIX_FMT_RGBA);
343 int start_tx = position.x;
347 start_ox = -start_tx;
351 int start_ty = position.y;
355 start_oy = -start_ty;
359 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
360 uint8_t* tp = data()[0] + ty * stride()[0] + position.x * 3;
361 uint8_t* op = other->data()[0] + oy * other->stride()[0];
362 for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
363 float const alpha = float (op[3]) / 255;
364 tp[0] = (tp[0] * (1 - alpha)) + op[0] * alpha;
365 tp[1] = (tp[1] * (1 - alpha)) + op[1] * alpha;
366 tp[2] = (tp[2] * (1 - alpha)) + op[2] * alpha;
374 Image::read_from_socket (shared_ptr<Socket> socket)
376 for (int i = 0; i < components(); ++i) {
377 uint8_t* p = data()[i];
378 for (int y = 0; y < lines(i); ++y) {
379 socket->read (p, line_size()[i]);
386 Image::write_to_socket (shared_ptr<Socket> socket) const
388 for (int i = 0; i < components(); ++i) {
389 uint8_t* p = data()[i];
390 for (int y = 0; y < lines(i); ++y) {
391 socket->write (p, line_size()[i]);
399 Image::bytes_per_pixel (int c) const
401 AVPixFmtDescriptor const * d = av_pix_fmt_desc_get(_pixel_format);
403 throw PixelFormatError (N_("lines()"), _pixel_format);
406 if (c >= components()) {
410 float bpp[4] = { 0, 0, 0, 0 };
412 bpp[0] = floor ((d->comp[0].depth_minus1 + 1 + 7) / 8);
413 if (d->nb_components > 1) {
414 bpp[1] = floor ((d->comp[1].depth_minus1 + 1 + 7) / 8) / pow (2, d->log2_chroma_w);
416 if (d->nb_components > 2) {
417 bpp[2] = floor ((d->comp[2].depth_minus1 + 1 + 7) / 8) / pow (2, d->log2_chroma_w);
419 if (d->nb_components > 3) {
420 bpp[3] = floor ((d->comp[3].depth_minus1 + 1 + 7) / 8) / pow (2, d->log2_chroma_w);
423 if ((d->flags & PIX_FMT_PLANAR) == 0) {
424 /* Not planar; sum them up */
425 return bpp[0] + bpp[1] + bpp[2] + bpp[3];
431 /** Construct a SimpleImage of a given size and format, allocating memory
434 * @param p Pixel format.
435 * @param s Size in pixels.
437 SimpleImage::SimpleImage (AVPixelFormat p, libdcp::Size s, bool aligned)
446 SimpleImage::allocate ()
448 _data = (uint8_t **) av_malloc (4 * sizeof (uint8_t *));
449 _data[0] = _data[1] = _data[2] = _data[3] = 0;
451 _line_size = (int *) av_malloc (4 * sizeof (int));
452 _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
454 _stride = (int *) av_malloc (4 * sizeof (int));
455 _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
457 for (int i = 0; i < components(); ++i) {
458 _line_size[i] = _size.width * bytes_per_pixel(i);
459 _stride[i] = stride_round_up (i, _line_size, _aligned ? 32 : 1);
460 _data[i] = (uint8_t *) av_malloc (_stride[i] * lines (i));
464 SimpleImage::SimpleImage (SimpleImage const & other)
468 _aligned = other._aligned;
472 for (int i = 0; i < components(); ++i) {
473 uint8_t* p = _data[i];
474 uint8_t* q = other._data[i];
475 for (int j = 0; j < lines(i); ++j) {
476 memcpy (p, q, _line_size[i]);
478 q += other.stride()[i];
483 SimpleImage::SimpleImage (shared_ptr<const Image> other)
484 : Image (*other.get())
486 _size = other->size ();
491 for (int i = 0; i < components(); ++i) {
492 assert(line_size()[i] == other->line_size()[i]);
493 uint8_t* p = _data[i];
494 uint8_t* q = other->data()[i];
495 for (int j = 0; j < lines(i); ++j) {
496 memcpy (p, q, line_size()[i]);
498 q += other->stride()[i];
504 SimpleImage::operator= (SimpleImage const & other)
506 if (this == &other) {
510 SimpleImage tmp (other);
516 SimpleImage::swap (SimpleImage & other)
520 std::swap (_size, other._size);
522 for (int i = 0; i < 4; ++i) {
523 std::swap (_data[i], other._data[i]);
524 std::swap (_line_size[i], other._line_size[i]);
525 std::swap (_stride[i], other._stride[i]);
528 std::swap (_aligned, other._aligned);
531 /** Destroy a SimpleImage */
532 SimpleImage::~SimpleImage ()
534 for (int i = 0; i < components(); ++i) {
539 av_free (_line_size);
544 SimpleImage::data () const
550 SimpleImage::line_size () const
556 SimpleImage::stride () const
562 SimpleImage::size () const
568 SimpleImage::aligned () const
573 FrameImage::FrameImage (AVFrame* frame)
574 : Image (static_cast<AVPixelFormat> (frame->format))
577 _line_size = (int *) av_malloc (4 * sizeof (int));
578 _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
580 for (int i = 0; i < components(); ++i) {
581 _line_size[i] = size().width * bytes_per_pixel(i);
585 FrameImage::~FrameImage ()
587 av_frame_free (&_frame);
588 av_free (_line_size);
592 FrameImage::data () const
598 FrameImage::line_size () const
604 FrameImage::stride () const
606 /* AVFrame's `linesize' is what we call `stride' */
607 return _frame->linesize;
611 FrameImage::size () const
613 return libdcp::Size (_frame->width, _frame->height);
617 FrameImage::aligned () const
622 RGBPlusAlphaImage::RGBPlusAlphaImage (shared_ptr<const Image> im)
623 : SimpleImage (im->pixel_format(), im->size(), false)
625 assert (im->pixel_format() == PIX_FMT_RGBA);
627 _alpha = (uint8_t *) av_malloc (im->size().width * im->size().height);
629 uint8_t* in = im->data()[0];
630 uint8_t* out = data()[0];
631 uint8_t* out_alpha = _alpha;
632 for (int y = 0; y < im->size().height; ++y) {
634 for (int x = 0; x < im->size().width; ++x) {
638 *out_alpha++ = *in_r++;
641 in += im->stride()[0];
645 RGBPlusAlphaImage::~RGBPlusAlphaImage ()