Fix typo in log message.
[dcpomatic.git] / src / lib / image.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 /** @file src/image.cc
23  *  @brief A class to describe a video image.
24  */
25
26
27 #include "compose.hpp"
28 #include "dcpomatic_assert.h"
29 #include "dcpomatic_socket.h"
30 #include "exceptions.h"
31 #include "image.h"
32 #include "maths_util.h"
33 #include "memory_util.h"
34 #include "rect.h"
35 #include "timer.h"
36 #include <dcp/rgb_xyz.h>
37 #include <dcp/transfer_function.h>
38 #include <dcp/warnings.h>
39 LIBDCP_DISABLE_WARNINGS
40 extern "C" {
41 #include <libavutil/frame.h>
42 #include <libavutil/pixdesc.h>
43 #include <libavutil/pixfmt.h>
44 #include <libswscale/swscale.h>
45 }
46 LIBDCP_ENABLE_WARNINGS
47 #if HAVE_VALGRIND_MEMCHECK_H
48 #include <valgrind/memcheck.h>
49 #endif
50 #include <iostream>
51
52
53 #include "i18n.h"
54
55
56 using std::cerr;
57 using std::cout;
58 using std::list;
59 using std::make_shared;
60 using std::max;
61 using std::min;
62 using std::runtime_error;
63 using std::shared_ptr;
64 using std::string;
65 using dcp::Size;
66
67
68 /** The memory alignment, in bytes, used for each row of an image if Alignment::PADDED is requested */
69 int constexpr ALIGNMENT = 64;
70
71 /* U/V black value for 8-bit colour */
72 static uint8_t const eight_bit_uv =     (1 << 7) - 1;
73 /* U/V black value for 9-bit colour */
74 static uint16_t const nine_bit_uv =     (1 << 8) - 1;
75 /* U/V black value for 10-bit colour */
76 static uint16_t const ten_bit_uv =      (1 << 9) - 1;
77 /* U/V black value for 16-bit colour */
78 static uint16_t const sixteen_bit_uv =  (1 << 15) - 1;
79
80
81 int
82 Image::vertical_factor (int n) const
83 {
84         if (n == 0) {
85                 return 1;
86         }
87
88         auto d = av_pix_fmt_desc_get(_pixel_format);
89         if (!d) {
90                 throw PixelFormatError ("line_factor()", _pixel_format);
91         }
92
93         return lrintf(powf(2.0f, d->log2_chroma_h));
94 }
95
96 int
97 Image::horizontal_factor (int n) const
98 {
99         if (n == 0) {
100                 return 1;
101         }
102
103         auto d = av_pix_fmt_desc_get(_pixel_format);
104         if (!d) {
105                 throw PixelFormatError ("sample_size()", _pixel_format);
106         }
107
108         return lrintf(powf(2.0f, d->log2_chroma_w));
109 }
110
111
112 /** @param n Component index.
113  *  @return Number of samples (i.e. pixels, unless sub-sampled) in each direction for this component.
114  */
115 dcp::Size
116 Image::sample_size (int n) const
117 {
118         return dcp::Size (
119                 lrint (ceil(static_cast<double>(size().width) / horizontal_factor(n))),
120                 lrint (ceil(static_cast<double>(size().height) / vertical_factor(n)))
121                 );
122 }
123
124
125 /** @return Number of planes */
126 int
127 Image::planes () const
128 {
129         if (_pixel_format == AV_PIX_FMT_PAL8) {
130                 return 2;
131         }
132
133         auto d = av_pix_fmt_desc_get(_pixel_format);
134         if (!d) {
135                 throw PixelFormatError ("planes()", _pixel_format);
136         }
137
138         if ((d->flags & AV_PIX_FMT_FLAG_PLANAR) == 0) {
139                 return 1;
140         }
141
142         return d->nb_components;
143 }
144
145
146 static
147 int
148 round_width_for_subsampling (int p, AVPixFmtDescriptor const * desc)
149 {
150         return p & ~ ((1 << desc->log2_chroma_w) - 1);
151 }
152
153
154 static
155 int
156 round_height_for_subsampling (int p, AVPixFmtDescriptor const * desc)
157 {
158         return p & ~ ((1 << desc->log2_chroma_h) - 1);
159 }
160
161
162 /** Crop this image, scale it to `inter_size' and then place it in a black frame of `out_size'.
163  *  @param crop Amount to crop by.
164  *  @param inter_size Size to scale the cropped image to.
165  *  @param out_size Size of output frame; if this is larger than inter_size there will be black padding.
166  *  @param yuv_to_rgb YUV to RGB transformation to use, if required.
167  *  @param video_range Video range of the image.
168  *  @param out_format Output pixel format.
169  *  @param out_aligned true to make the output image aligned.
170  *  @param out_video_range Video range to use for the output image.
171  *  @param fast Try to be fast at the possible expense of quality; at present this means using
172  *  fast bilinear rather than bicubic scaling.
173  */
174 shared_ptr<Image>
175 Image::crop_scale_window (
176         Crop crop,
177         dcp::Size inter_size,
178         dcp::Size out_size,
179         dcp::YUVToRGB yuv_to_rgb,
180         VideoRange video_range,
181         AVPixelFormat out_format,
182         VideoRange out_video_range,
183         Alignment out_alignment,
184         bool fast
185         ) const
186 {
187         /* Empirical testing suggests that sws_scale() will crash if
188            the input image is not padded.
189         */
190         DCPOMATIC_ASSERT (alignment() == Alignment::PADDED);
191
192         DCPOMATIC_ASSERT (out_size.width >= inter_size.width);
193         DCPOMATIC_ASSERT (out_size.height >= inter_size.height);
194
195         auto out = make_shared<Image>(out_format, out_size, out_alignment);
196         out->make_black ();
197
198         auto in_desc = av_pix_fmt_desc_get (_pixel_format);
199         if (!in_desc) {
200                 throw PixelFormatError ("crop_scale_window()", _pixel_format);
201         }
202
203         /* Round down so that we crop only the number of pixels that is straightforward
204          * considering any subsampling.
205          */
206         Crop corrected_crop(
207                 round_width_for_subsampling(crop.left, in_desc),
208                 round_width_for_subsampling(crop.right, in_desc),
209                 round_height_for_subsampling(crop.top, in_desc),
210                 round_height_for_subsampling(crop.bottom, in_desc)
211                 );
212
213         /* Also check that we aren't cropping more image than there actually is */
214         if ((corrected_crop.left + corrected_crop.right) >= (size().width - 4)) {
215                 corrected_crop.left = 0;
216                 corrected_crop.right = size().width - 4;
217         }
218
219         if ((corrected_crop.top + corrected_crop.bottom) >= (size().height - 4)) {
220                 corrected_crop.top = 0;
221                 corrected_crop.bottom = size().height - 4;
222         }
223
224         /* Size of the image after any crop */
225         auto const cropped_size = corrected_crop.apply (size());
226
227         /* Scale context for a scale from cropped_size to inter_size */
228         auto scale_context = sws_getContext (
229                         cropped_size.width, cropped_size.height, pixel_format(),
230                         inter_size.width, inter_size.height, out_format,
231                         fast ? SWS_FAST_BILINEAR : SWS_BICUBIC, 0, 0, 0
232                 );
233
234         if (!scale_context) {
235                 throw runtime_error (N_("Could not allocate SwsContext"));
236         }
237
238         DCPOMATIC_ASSERT (yuv_to_rgb < dcp::YUVToRGB::COUNT);
239         int const lut[static_cast<int>(dcp::YUVToRGB::COUNT)] = {
240                 SWS_CS_ITU601,
241                 SWS_CS_ITU709
242         };
243
244         /* The 3rd parameter here is:
245            0 -> source range MPEG (i.e. "video", 16-235)
246            1 -> source range JPEG (i.e. "full", 0-255)
247            And the 5th:
248            0 -> destination range MPEG (i.e. "video", 16-235)
249            1 -> destination range JPEG (i.e. "full", 0-255)
250
251            But remember: sws_setColorspaceDetails ignores these
252            parameters unless the both source and destination images
253            are isYUV or isGray.  (If either is not, it uses video range).
254         */
255         sws_setColorspaceDetails (
256                 scale_context,
257                 sws_getCoefficients (lut[static_cast<int>(yuv_to_rgb)]), video_range == VideoRange::VIDEO ? 0 : 1,
258                 sws_getCoefficients (lut[static_cast<int>(yuv_to_rgb)]), out_video_range == VideoRange::VIDEO ? 0 : 1,
259                 0, 1 << 16, 1 << 16
260                 );
261
262         /* Prepare input data pointers with crop */
263         uint8_t* scale_in_data[planes()];
264         for (int c = 0; c < planes(); ++c) {
265                 int const x = lrintf(bytes_per_pixel(c) * corrected_crop.left);
266                 scale_in_data[c] = data()[c] + x + stride()[c] * (corrected_crop.top / vertical_factor(c));
267         }
268
269         auto out_desc = av_pix_fmt_desc_get (out_format);
270         if (!out_desc) {
271                 throw PixelFormatError ("crop_scale_window()", out_format);
272         }
273
274         /* Corner of the image within out_size */
275         Position<int> const corner (
276                 round_width_for_subsampling((out_size.width - inter_size.width) / 2, out_desc),
277                 round_height_for_subsampling((out_size.height - inter_size.height) / 2, out_desc)
278                 );
279
280         uint8_t* scale_out_data[out->planes()];
281         for (int c = 0; c < out->planes(); ++c) {
282                 int const x = lrintf(out->bytes_per_pixel(c) * corner.x);
283                 scale_out_data[c] = out->data()[c] + x + out->stride()[c] * (corner.y / out->vertical_factor(c));
284         }
285
286         sws_scale (
287                 scale_context,
288                 scale_in_data, stride(),
289                 0, cropped_size.height,
290                 scale_out_data, out->stride()
291                 );
292
293         sws_freeContext (scale_context);
294
295         /* There are some cases where there will be unwanted image data left in the image at this point:
296          *
297          * 1. When we are cropping without any scaling or pixel format conversion.
298          * 2. When we are scaling to certain sizes and placing the result into a larger
299          *    black frame.
300          *
301          * Clear out the sides of the image to take care of those cases.
302          */
303         auto const pad = (out_size.width - inter_size.width) / 2;
304         out->make_part_black(0, pad);
305         out->make_part_black(corner.x + inter_size.width, pad);
306
307         if (
308                 video_range == VideoRange::VIDEO &&
309                 out_video_range == VideoRange::FULL &&
310                 av_pix_fmt_desc_get(_pixel_format)->flags & AV_PIX_FMT_FLAG_RGB
311            ) {
312                 /* libswscale will not convert video range for RGB sources, so we have to do it ourselves */
313                 out->video_range_to_full_range ();
314         }
315
316         return out;
317 }
318
319
320 shared_ptr<Image>
321 Image::convert_pixel_format (dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, Alignment out_alignment, bool fast) const
322 {
323         return scale(size(), yuv_to_rgb, out_format, out_alignment, fast);
324 }
325
326
327 /** @param out_size Size to scale to.
328  *  @param yuv_to_rgb YUVToRGB transform transform to use, if required.
329  *  @param out_format Output pixel format.
330  *  @param out_aligment Output alignment.
331  *  @param fast Try to be fast at the possible expense of quality; at present this means using
332  *  fast bilinear rather than bicubic scaling.
333  */
334 shared_ptr<Image>
335 Image::scale (dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, Alignment out_alignment, bool fast) const
336 {
337         /* Empirical testing suggests that sws_scale() will crash if
338            the input image alignment is not PADDED.
339         */
340         DCPOMATIC_ASSERT (alignment() == Alignment::PADDED);
341
342         auto scaled = make_shared<Image>(out_format, out_size, out_alignment);
343         auto scale_context = sws_getContext (
344                 size().width, size().height, pixel_format(),
345                 out_size.width, out_size.height, out_format,
346                 (fast ? SWS_FAST_BILINEAR : SWS_BICUBIC) | SWS_ACCURATE_RND, 0, 0, 0
347                 );
348
349         DCPOMATIC_ASSERT (yuv_to_rgb < dcp::YUVToRGB::COUNT);
350         int const lut[static_cast<int>(dcp::YUVToRGB::COUNT)] = {
351                 SWS_CS_ITU601,
352                 SWS_CS_ITU709
353         };
354
355         /* The 3rd parameter here is:
356            0 -> source range MPEG (i.e. "video", 16-235)
357            1 -> source range JPEG (i.e. "full", 0-255)
358            And the 5th:
359            0 -> destination range MPEG (i.e. "video", 16-235)
360            1 -> destination range JPEG (i.e. "full", 0-255)
361
362            But remember: sws_setColorspaceDetails ignores these
363            parameters unless the corresponding image isYUV or isGray.
364            (If it's neither, it uses video range).
365         */
366         sws_setColorspaceDetails (
367                 scale_context,
368                 sws_getCoefficients (lut[static_cast<int>(yuv_to_rgb)]), 0,
369                 sws_getCoefficients (lut[static_cast<int>(yuv_to_rgb)]), 0,
370                 0, 1 << 16, 1 << 16
371                 );
372
373         sws_scale (
374                 scale_context,
375                 data(), stride(),
376                 0, size().height,
377                 scaled->data(), scaled->stride()
378                 );
379
380         sws_freeContext (scale_context);
381
382         return scaled;
383 }
384
385
386 /** Blacken a YUV image whose bits per pixel is rounded up to 16 */
387 void
388 Image::yuv_16_black (uint16_t v, bool alpha)
389 {
390         memset (data()[0], 0, sample_size(0).height * stride()[0]);
391         for (int i = 1; i < 3; ++i) {
392                 auto p = reinterpret_cast<int16_t*> (data()[i]);
393                 int const lines = sample_size(i).height;
394                 for (int y = 0; y < lines; ++y) {
395                         /* We divide by 2 here because we are writing 2 bytes at a time */
396                         for (int x = 0; x < line_size()[i] / 2; ++x) {
397                                 p[x] = v;
398                         }
399                         p += stride()[i] / 2;
400                 }
401         }
402
403         if (alpha) {
404                 memset (data()[3], 0, sample_size(3).height * stride()[3]);
405         }
406 }
407
408
409 uint16_t
410 Image::swap_16 (uint16_t v)
411 {
412         return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
413 }
414
415
416 void
417 Image::make_part_black (int const start, int const width)
418 {
419         auto y_part = [&]() {
420                 int const bpp = bytes_per_pixel(0);
421                 int const h = sample_size(0).height;
422                 int const s = stride()[0];
423                 auto p = data()[0];
424                 for (int y = 0; y < h; ++y) {
425                         memset (p + start * bpp, 0, width * bpp);
426                         p += s;
427                 }
428         };
429
430         switch (_pixel_format) {
431         case AV_PIX_FMT_RGB24:
432         case AV_PIX_FMT_ARGB:
433         case AV_PIX_FMT_RGBA:
434         case AV_PIX_FMT_ABGR:
435         case AV_PIX_FMT_BGRA:
436         case AV_PIX_FMT_RGB555LE:
437         case AV_PIX_FMT_RGB48LE:
438         case AV_PIX_FMT_RGB48BE:
439         case AV_PIX_FMT_XYZ12LE:
440         {
441                 int const h = sample_size(0).height;
442                 int const bpp = bytes_per_pixel(0);
443                 int const s = stride()[0];
444                 uint8_t* p = data()[0];
445                 for (int y = 0; y < h; y++) {
446                         memset (p + start * bpp, 0, width * bpp);
447                         p += s;
448                 }
449                 break;
450         }
451         case AV_PIX_FMT_YUV420P:
452         {
453                 y_part ();
454                 for (int i = 1; i < 3; ++i) {
455                         auto p = data()[i];
456                         int const h = sample_size(i).height;
457                         for (int y = 0; y < h; ++y) {
458                                 for (int x = start / 2; x < (start + width) / 2; ++x) {
459                                         p[x] = eight_bit_uv;
460                                 }
461                                 p += stride()[i];
462                         }
463                 }
464                 break;
465         }
466         case AV_PIX_FMT_YUV422P10LE:
467         {
468                 y_part ();
469                 for (int i = 1; i < 3; ++i) {
470                         auto p = reinterpret_cast<int16_t*>(data()[i]);
471                         int const h = sample_size(i).height;
472                         for (int y = 0; y < h; ++y) {
473                                 for (int x = start / 2; x < (start + width) / 2; ++x) {
474                                         p[x] = ten_bit_uv;
475                                 }
476                                 p += stride()[i] / 2;
477                         }
478                 }
479                 break;
480         }
481         case AV_PIX_FMT_YUV444P10LE:
482         {
483                 y_part();
484                 for (int i = 1; i < 3; ++i) {
485                         auto p = reinterpret_cast<int16_t*>(data()[i]);
486                         int const h = sample_size(i).height;
487                         for (int y = 0; y < h; ++y) {
488                                 for (int x = start; x < (start + width); ++x) {
489                                         p[x] = ten_bit_uv;
490                                 }
491                                 p += stride()[i] / 2;
492                         }
493                 }
494                 break;
495         }
496         default:
497                 throw PixelFormatError ("make_part_black()", _pixel_format);
498         }
499 }
500
501
502 void
503 Image::make_black ()
504 {
505         switch (_pixel_format) {
506         case AV_PIX_FMT_YUV420P:
507         case AV_PIX_FMT_YUV422P:
508         case AV_PIX_FMT_YUV444P:
509         case AV_PIX_FMT_YUV411P:
510                 memset (data()[0], 0, sample_size(0).height * stride()[0]);
511                 memset (data()[1], eight_bit_uv, sample_size(1).height * stride()[1]);
512                 memset (data()[2], eight_bit_uv, sample_size(2).height * stride()[2]);
513                 break;
514
515         case AV_PIX_FMT_YUVJ420P:
516         case AV_PIX_FMT_YUVJ422P:
517         case AV_PIX_FMT_YUVJ444P:
518                 memset (data()[0], 0, sample_size(0).height * stride()[0]);
519                 memset (data()[1], eight_bit_uv + 1, sample_size(1).height * stride()[1]);
520                 memset (data()[2], eight_bit_uv + 1, sample_size(2).height * stride()[2]);
521                 break;
522
523         case AV_PIX_FMT_YUV422P9LE:
524         case AV_PIX_FMT_YUV444P9LE:
525                 yuv_16_black (nine_bit_uv, false);
526                 break;
527
528         case AV_PIX_FMT_YUV422P9BE:
529         case AV_PIX_FMT_YUV444P9BE:
530                 yuv_16_black (swap_16 (nine_bit_uv), false);
531                 break;
532
533         case AV_PIX_FMT_YUV422P10LE:
534         case AV_PIX_FMT_YUV444P10LE:
535                 yuv_16_black (ten_bit_uv, false);
536                 break;
537
538         case AV_PIX_FMT_YUV422P16LE:
539         case AV_PIX_FMT_YUV444P16LE:
540                 yuv_16_black (sixteen_bit_uv, false);
541                 break;
542
543         case AV_PIX_FMT_YUV444P10BE:
544         case AV_PIX_FMT_YUV422P10BE:
545                 yuv_16_black (swap_16 (ten_bit_uv), false);
546                 break;
547
548         case AV_PIX_FMT_YUVA420P9BE:
549         case AV_PIX_FMT_YUVA422P9BE:
550         case AV_PIX_FMT_YUVA444P9BE:
551                 yuv_16_black (swap_16 (nine_bit_uv), true);
552                 break;
553
554         case AV_PIX_FMT_YUVA420P9LE:
555         case AV_PIX_FMT_YUVA422P9LE:
556         case AV_PIX_FMT_YUVA444P9LE:
557                 yuv_16_black (nine_bit_uv, true);
558                 break;
559
560         case AV_PIX_FMT_YUVA420P10BE:
561         case AV_PIX_FMT_YUVA422P10BE:
562         case AV_PIX_FMT_YUVA444P10BE:
563                 yuv_16_black (swap_16 (ten_bit_uv), true);
564                 break;
565
566         case AV_PIX_FMT_YUVA420P10LE:
567         case AV_PIX_FMT_YUVA422P10LE:
568         case AV_PIX_FMT_YUVA444P10LE:
569                 yuv_16_black (ten_bit_uv, true);
570                 break;
571
572         case AV_PIX_FMT_YUVA420P16BE:
573         case AV_PIX_FMT_YUVA422P16BE:
574         case AV_PIX_FMT_YUVA444P16BE:
575                 yuv_16_black (swap_16 (sixteen_bit_uv), true);
576                 break;
577
578         case AV_PIX_FMT_YUVA420P16LE:
579         case AV_PIX_FMT_YUVA422P16LE:
580         case AV_PIX_FMT_YUVA444P16LE:
581                 yuv_16_black (sixteen_bit_uv, true);
582                 break;
583
584         case AV_PIX_FMT_RGB24:
585         case AV_PIX_FMT_ARGB:
586         case AV_PIX_FMT_RGBA:
587         case AV_PIX_FMT_ABGR:
588         case AV_PIX_FMT_BGRA:
589         case AV_PIX_FMT_RGB555LE:
590         case AV_PIX_FMT_RGB48LE:
591         case AV_PIX_FMT_RGB48BE:
592         case AV_PIX_FMT_XYZ12LE:
593                 memset (data()[0], 0, sample_size(0).height * stride()[0]);
594                 break;
595
596         case AV_PIX_FMT_UYVY422:
597         {
598                 int const Y = sample_size(0).height;
599                 int const X = line_size()[0];
600                 uint8_t* p = data()[0];
601                 for (int y = 0; y < Y; ++y) {
602                         for (int x = 0; x < X / 4; ++x) {
603                                 *p++ = eight_bit_uv; // Cb
604                                 *p++ = 0;            // Y0
605                                 *p++ = eight_bit_uv; // Cr
606                                 *p++ = 0;            // Y1
607                         }
608                 }
609                 break;
610         }
611
612         default:
613                 throw PixelFormatError ("make_black()", _pixel_format);
614         }
615 }
616
617
618 void
619 Image::make_transparent ()
620 {
621         if (_pixel_format != AV_PIX_FMT_BGRA && _pixel_format != AV_PIX_FMT_RGBA) {
622                 throw PixelFormatError ("make_transparent()", _pixel_format);
623         }
624
625         memset (data()[0], 0, sample_size(0).height * stride()[0]);
626 }
627
628
629 void
630 Image::alpha_blend (shared_ptr<const Image> other, Position<int> position)
631 {
632         /* We're blending RGBA or BGRA images */
633         DCPOMATIC_ASSERT (other->pixel_format() == AV_PIX_FMT_BGRA || other->pixel_format() == AV_PIX_FMT_RGBA);
634         int const blue = other->pixel_format() == AV_PIX_FMT_BGRA ? 0 : 2;
635         int const red = other->pixel_format() == AV_PIX_FMT_BGRA ? 2 : 0;
636
637         int const other_bpp = 4;
638
639         int start_tx = position.x;
640         int start_ox = 0;
641
642         if (start_tx < 0) {
643                 start_ox = -start_tx;
644                 start_tx = 0;
645         }
646
647         int start_ty = position.y;
648         int start_oy = 0;
649
650         if (start_ty < 0) {
651                 start_oy = -start_ty;
652                 start_ty = 0;
653         }
654
655         switch (_pixel_format) {
656         case AV_PIX_FMT_RGB24:
657         {
658                 /* Going onto RGB24.  First byte is red, second green, third blue */
659                 int const this_bpp = 3;
660                 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
661                         uint8_t* tp = data()[0] + ty * stride()[0] + start_tx * this_bpp;
662                         uint8_t* op = other->data()[0] + oy * other->stride()[0];
663                         for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
664                                 float const alpha = float (op[3]) / 255;
665                                 tp[0] = op[red] * alpha + tp[0] * (1 - alpha);
666                                 tp[1] = op[1] * alpha + tp[1] * (1 - alpha);
667                                 tp[2] = op[blue] * alpha + tp[2] * (1 - alpha);
668
669                                 tp += this_bpp;
670                                 op += other_bpp;
671                         }
672                 }
673                 break;
674         }
675         case AV_PIX_FMT_BGRA:
676         {
677                 int const this_bpp = 4;
678                 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
679                         uint8_t* tp = data()[0] + ty * stride()[0] + start_tx * this_bpp;
680                         uint8_t* op = other->data()[0] + oy * other->stride()[0];
681                         for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
682                                 float const alpha = float (op[3]) / 255;
683                                 tp[0] = op[blue] * alpha + tp[0] * (1 - alpha);
684                                 tp[1] = op[1] * alpha + tp[1] * (1 - alpha);
685                                 tp[2] = op[red] * alpha + tp[2] * (1 - alpha);
686                                 tp[3] = op[3] * alpha + tp[3] * (1 - alpha);
687
688                                 tp += this_bpp;
689                                 op += other_bpp;
690                         }
691                 }
692                 break;
693         }
694         case AV_PIX_FMT_RGBA:
695         {
696                 int const this_bpp = 4;
697                 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
698                         uint8_t* tp = data()[0] + ty * stride()[0] + start_tx * this_bpp;
699                         uint8_t* op = other->data()[0] + oy * other->stride()[0];
700                         for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
701                                 float const alpha = float (op[3]) / 255;
702                                 tp[0] = op[red] * alpha + tp[0] * (1 - alpha);
703                                 tp[1] = op[1] * alpha + tp[1] * (1 - alpha);
704                                 tp[2] = op[blue] * alpha + tp[2] * (1 - alpha);
705                                 tp[3] = op[3] * alpha + tp[3] * (1 - alpha);
706
707                                 tp += this_bpp;
708                                 op += other_bpp;
709                         }
710                 }
711                 break;
712         }
713         case AV_PIX_FMT_RGB48LE:
714         {
715                 int const this_bpp = 6;
716                 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
717                         uint8_t* tp = data()[0] + ty * stride()[0] + start_tx * this_bpp;
718                         uint8_t* op = other->data()[0] + oy * other->stride()[0];
719                         for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
720                                 float const alpha = float (op[3]) / 255;
721                                 /* Blend high bytes */
722                                 tp[1] = op[red] * alpha + tp[1] * (1 - alpha);
723                                 tp[3] = op[1] * alpha + tp[3] * (1 - alpha);
724                                 tp[5] = op[blue] * alpha + tp[5] * (1 - alpha);
725
726                                 tp += this_bpp;
727                                 op += other_bpp;
728                         }
729                 }
730                 break;
731         }
732         case AV_PIX_FMT_XYZ12LE:
733         {
734                 auto conv = dcp::ColourConversion::srgb_to_xyz();
735                 double fast_matrix[9];
736                 dcp::combined_rgb_to_xyz (conv, fast_matrix);
737                 auto lut_in = conv.in()->lut(0, 1, 8, false);
738                 auto lut_out = conv.out()->lut(0, 1, 16, true);
739                 int const this_bpp = 6;
740                 for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
741                         uint16_t* tp = reinterpret_cast<uint16_t*> (data()[0] + ty * stride()[0] + start_tx * this_bpp);
742                         uint8_t* op = other->data()[0] + oy * other->stride()[0];
743                         for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
744                                 float const alpha = float (op[3]) / 255;
745
746                                 /* Convert sRGB to XYZ; op is BGRA.  First, input gamma LUT */
747                                 double const r = lut_in[op[red]];
748                                 double const g = lut_in[op[1]];
749                                 double const b = lut_in[op[blue]];
750
751                                 /* RGB to XYZ, including Bradford transform and DCI companding */
752                                 double const x = max(0.0, min(1.0, r * fast_matrix[0] + g * fast_matrix[1] + b * fast_matrix[2]));
753                                 double const y = max(0.0, min(1.0, r * fast_matrix[3] + g * fast_matrix[4] + b * fast_matrix[5]));
754                                 double const z = max(0.0, min(1.0, r * fast_matrix[6] + g * fast_matrix[7] + b * fast_matrix[8]));
755
756                                 /* Out gamma LUT and blend */
757                                 tp[0] = lrint(lut_out[lrint(x * 65535)] * 65535) * alpha + tp[0] * (1 - alpha);
758                                 tp[1] = lrint(lut_out[lrint(y * 65535)] * 65535) * alpha + tp[1] * (1 - alpha);
759                                 tp[2] = lrint(lut_out[lrint(z * 65535)] * 65535) * alpha + tp[2] * (1 - alpha);
760
761                                 tp += this_bpp / 2;
762                                 op += other_bpp;
763                         }
764                 }
765                 break;
766         }
767         case AV_PIX_FMT_YUV420P:
768         {
769                 auto yuv = other->convert_pixel_format (dcp::YUVToRGB::REC709, _pixel_format, Alignment::COMPACT, false);
770                 dcp::Size const ts = size();
771                 dcp::Size const os = yuv->size();
772                 for (int ty = start_ty, oy = start_oy; ty < ts.height && oy < os.height; ++ty, ++oy) {
773                         int const hty = ty / 2;
774                         int const hoy = oy / 2;
775                         uint8_t* tY = data()[0] + (ty * stride()[0]) + start_tx;
776                         uint8_t* tU = data()[1] + (hty * stride()[1]) + start_tx / 2;
777                         uint8_t* tV = data()[2] + (hty * stride()[2]) + start_tx / 2;
778                         uint8_t* oY = yuv->data()[0] + (oy * yuv->stride()[0]) + start_ox;
779                         uint8_t* oU = yuv->data()[1] + (hoy * yuv->stride()[1]) + start_ox / 2;
780                         uint8_t* oV = yuv->data()[2] + (hoy * yuv->stride()[2]) + start_ox / 2;
781                         uint8_t* alpha = other->data()[0] + (oy * other->stride()[0]) + start_ox * 4;
782                         for (int tx = start_tx, ox = start_ox; tx < ts.width && ox < os.width; ++tx, ++ox) {
783                                 float const a = float(alpha[3]) / 255;
784                                 *tY = *oY * a + *tY * (1 - a);
785                                 *tU = *oU * a + *tU * (1 - a);
786                                 *tV = *oV * a + *tV * (1 - a);
787                                 ++tY;
788                                 ++oY;
789                                 if (tx % 2) {
790                                         ++tU;
791                                         ++tV;
792                                 }
793                                 if (ox % 2) {
794                                         ++oU;
795                                         ++oV;
796                                 }
797                                 alpha += 4;
798                         }
799                 }
800                 break;
801         }
802         case AV_PIX_FMT_YUV420P10:
803         {
804                 auto yuv = other->convert_pixel_format (dcp::YUVToRGB::REC709, _pixel_format, Alignment::COMPACT, false);
805                 dcp::Size const ts = size();
806                 dcp::Size const os = yuv->size();
807                 for (int ty = start_ty, oy = start_oy; ty < ts.height && oy < os.height; ++ty, ++oy) {
808                         int const hty = ty / 2;
809                         int const hoy = oy / 2;
810                         uint16_t* tY = ((uint16_t *) (data()[0] + (ty * stride()[0]))) + start_tx;
811                         uint16_t* tU = ((uint16_t *) (data()[1] + (hty * stride()[1]))) + start_tx / 2;
812                         uint16_t* tV = ((uint16_t *) (data()[2] + (hty * stride()[2]))) + start_tx / 2;
813                         uint16_t* oY = ((uint16_t *) (yuv->data()[0] + (oy * yuv->stride()[0]))) + start_ox;
814                         uint16_t* oU = ((uint16_t *) (yuv->data()[1] + (hoy * yuv->stride()[1]))) + start_ox / 2;
815                         uint16_t* oV = ((uint16_t *) (yuv->data()[2] + (hoy * yuv->stride()[2]))) + start_ox / 2;
816                         uint8_t* alpha = other->data()[0] + (oy * other->stride()[0]) + start_ox * 4;
817                         for (int tx = start_tx, ox = start_ox; tx < ts.width && ox < os.width; ++tx, ++ox) {
818                                 float const a = float(alpha[3]) / 255;
819                                 *tY = *oY * a + *tY * (1 - a);
820                                 *tU = *oU * a + *tU * (1 - a);
821                                 *tV = *oV * a + *tV * (1 - a);
822                                 ++tY;
823                                 ++oY;
824                                 if (tx % 2) {
825                                         ++tU;
826                                         ++tV;
827                                 }
828                                 if (ox % 2) {
829                                         ++oU;
830                                         ++oV;
831                                 }
832                                 alpha += 4;
833                         }
834                 }
835                 break;
836         }
837         case AV_PIX_FMT_YUV422P10LE:
838         {
839                 auto yuv = other->convert_pixel_format (dcp::YUVToRGB::REC709, _pixel_format, Alignment::COMPACT, false);
840                 dcp::Size const ts = size();
841                 dcp::Size const os = yuv->size();
842                 for (int ty = start_ty, oy = start_oy; ty < ts.height && oy < os.height; ++ty, ++oy) {
843                         uint16_t* tY = ((uint16_t *) (data()[0] + (ty * stride()[0]))) + start_tx;
844                         uint16_t* tU = ((uint16_t *) (data()[1] + (ty * stride()[1]))) + start_tx / 2;
845                         uint16_t* tV = ((uint16_t *) (data()[2] + (ty * stride()[2]))) + start_tx / 2;
846                         uint16_t* oY = ((uint16_t *) (yuv->data()[0] + (oy * yuv->stride()[0]))) + start_ox;
847                         uint16_t* oU = ((uint16_t *) (yuv->data()[1] + (oy * yuv->stride()[1]))) + start_ox / 2;
848                         uint16_t* oV = ((uint16_t *) (yuv->data()[2] + (oy * yuv->stride()[2]))) + start_ox / 2;
849                         uint8_t* alpha = other->data()[0] + (oy * other->stride()[0]) + start_ox * 4;
850                         for (int tx = start_tx, ox = start_ox; tx < ts.width && ox < os.width; ++tx, ++ox) {
851                                 float const a = float(alpha[3]) / 255;
852                                 *tY = *oY * a + *tY * (1 - a);
853                                 *tU = *oU * a + *tU * (1 - a);
854                                 *tV = *oV * a + *tV * (1 - a);
855                                 ++tY;
856                                 ++oY;
857                                 if (tx % 2) {
858                                         ++tU;
859                                         ++tV;
860                                 }
861                                 if (ox % 2) {
862                                         ++oU;
863                                         ++oV;
864                                 }
865                                 alpha += 4;
866                         }
867                 }
868                 break;
869         }
870         default:
871                 throw PixelFormatError ("alpha_blend()", _pixel_format);
872         }
873 }
874
875
876 void
877 Image::copy (shared_ptr<const Image> other, Position<int> position)
878 {
879         /* Only implemented for RGB24 onto RGB24 so far */
880         DCPOMATIC_ASSERT (_pixel_format == AV_PIX_FMT_RGB24 && other->pixel_format() == AV_PIX_FMT_RGB24);
881         DCPOMATIC_ASSERT (position.x >= 0 && position.y >= 0);
882
883         int const N = min (position.x + other->size().width, size().width) - position.x;
884         for (int ty = position.y, oy = 0; ty < size().height && oy < other->size().height; ++ty, ++oy) {
885                 uint8_t * const tp = data()[0] + ty * stride()[0] + position.x * 3;
886                 uint8_t * const op = other->data()[0] + oy * other->stride()[0];
887                 memcpy (tp, op, N * 3);
888         }
889 }
890
891
892 void
893 Image::read_from_socket (shared_ptr<Socket> socket)
894 {
895         for (int i = 0; i < planes(); ++i) {
896                 uint8_t* p = data()[i];
897                 int const lines = sample_size(i).height;
898                 for (int y = 0; y < lines; ++y) {
899                         socket->read (p, line_size()[i]);
900                         p += stride()[i];
901                 }
902         }
903 }
904
905
906 void
907 Image::write_to_socket (shared_ptr<Socket> socket) const
908 {
909         for (int i = 0; i < planes(); ++i) {
910                 uint8_t* p = data()[i];
911                 int const lines = sample_size(i).height;
912                 for (int y = 0; y < lines; ++y) {
913                         socket->write (p, line_size()[i]);
914                         p += stride()[i];
915                 }
916         }
917 }
918
919
920 float
921 Image::bytes_per_pixel (int c) const
922 {
923         auto d = av_pix_fmt_desc_get(_pixel_format);
924         if (!d) {
925                 throw PixelFormatError ("bytes_per_pixel()", _pixel_format);
926         }
927
928         if (c >= planes()) {
929                 return 0;
930         }
931
932         float bpp[4] = { 0, 0, 0, 0 };
933
934 #ifdef DCPOMATIC_HAVE_AVCOMPONENTDESCRIPTOR_DEPTH_MINUS1
935         bpp[0] = floor ((d->comp[0].depth_minus1 + 8) / 8);
936         if (d->nb_components > 1) {
937                 bpp[1] = floor ((d->comp[1].depth_minus1 + 8) / 8) / pow (2.0f, d->log2_chroma_w);
938         }
939         if (d->nb_components > 2) {
940                 bpp[2] = floor ((d->comp[2].depth_minus1 + 8) / 8) / pow (2.0f, d->log2_chroma_w);
941         }
942         if (d->nb_components > 3) {
943                 bpp[3] = floor ((d->comp[3].depth_minus1 + 8) / 8) / pow (2.0f, d->log2_chroma_w);
944         }
945 #else
946         bpp[0] = floor ((d->comp[0].depth + 7) / 8);
947         if (d->nb_components > 1) {
948                 bpp[1] = floor ((d->comp[1].depth + 7) / 8) / pow (2.0f, d->log2_chroma_w);
949         }
950         if (d->nb_components > 2) {
951                 bpp[2] = floor ((d->comp[2].depth + 7) / 8) / pow (2.0f, d->log2_chroma_w);
952         }
953         if (d->nb_components > 3) {
954                 bpp[3] = floor ((d->comp[3].depth + 7) / 8) / pow (2.0f, d->log2_chroma_w);
955         }
956 #endif
957
958         if ((d->flags & AV_PIX_FMT_FLAG_PLANAR) == 0) {
959                 /* Not planar; sum them up */
960                 return bpp[0] + bpp[1] + bpp[2] + bpp[3];
961         }
962
963         return bpp[c];
964 }
965
966
967 /** Construct a Image of a given size and format, allocating memory
968  *  as required.
969  *
970  *  @param p Pixel format.
971  *  @param s Size in pixels.
972  *  @param alignment PADDED to make each row of this image aligned to a ALIGNMENT-byte boundary, otherwise COMPACT.
973  */
974 Image::Image (AVPixelFormat p, dcp::Size s, Alignment alignment)
975         : _size (s)
976         , _pixel_format (p)
977         , _alignment (alignment)
978 {
979         allocate ();
980 }
981
982
983 void
984 Image::allocate ()
985 {
986         _data = (uint8_t **) wrapped_av_malloc (4 * sizeof (uint8_t *));
987         _data[0] = _data[1] = _data[2] = _data[3] = 0;
988
989         _line_size = (int *) wrapped_av_malloc (4 * sizeof (int));
990         _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
991
992         _stride = (int *) wrapped_av_malloc (4 * sizeof (int));
993         _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
994
995         auto stride_round_up = [](int stride, int t) {
996                 int const a = stride + (t - 1);
997                 return a - (a % t);
998         };
999
1000         for (int i = 0; i < planes(); ++i) {
1001                 _line_size[i] = ceil (_size.width * bytes_per_pixel(i));
1002                 _stride[i] = stride_round_up (_line_size[i], _alignment == Alignment::PADDED ? ALIGNMENT : 1);
1003
1004                 /* The assembler function ff_rgb24ToY_avx (in libswscale/x86/input.asm)
1005                    uses a 16-byte fetch to read three bytes (R/G/B) of image data.
1006                    Hence on the last pixel of the last line it reads over the end of
1007                    the actual data by 1 byte.  If the width of an image is a multiple
1008                    of the stride alignment there will be no padding at the end of image lines.
1009                    OS X crashes on this illegal read, though other operating systems don't
1010                    seem to mind.  The nasty + 1 in this malloc makes sure there is always a byte
1011                    for that instruction to read safely.
1012
1013                    Further to the above, valgrind is now telling me that ff_rgb24ToY_ssse3
1014                    over-reads by more then _avx.  I can't follow the code to work out how much,
1015                    so I'll just over-allocate by ALIGNMENT bytes and have done with it.  Empirical
1016                    testing suggests that it works.
1017
1018                    In addition to these concerns, we may read/write as much as a whole extra line
1019                    at the end of each plane in cases where we are messing with offsets in order to
1020                    do pad or crop.  To solve this we over-allocate by an extra _stride[i] bytes.
1021
1022                    As an example: we may write to images starting at an offset so we get some padding.
1023                    Hence we want to write in the following pattern:
1024
1025                    block start   write start                                  line end
1026                    |..(padding)..|<------line-size------------->|..(padding)..|
1027                    |..(padding)..|<------line-size------------->|..(padding)..|
1028                    |..(padding)..|<------line-size------------->|..(padding)..|
1029
1030                    where line-size is of the smaller (inter_size) image and the full padded line length is that of
1031                    out_size.  To get things to work we have to tell FFmpeg that the stride is that of out_size.
1032                    However some parts of FFmpeg (notably rgb48Toxyz12 in swscale.c) process data for the full
1033                    specified *stride*.  This does not matter until we get to the last line:
1034
1035                    block start   write start                                  line end
1036                    |..(padding)..|<------line-size------------->|XXXwrittenXXX|
1037                    |XXXwrittenXXX|<------line-size------------->|XXXwrittenXXX|
1038                    |XXXwrittenXXX|<------line-size------------->|XXXwrittenXXXXXXwrittenXXX
1039                                                                                ^^^^ out of bounds
1040                 */
1041                 _data[i] = (uint8_t *) wrapped_av_malloc (_stride[i] * (sample_size(i).height + 1) + ALIGNMENT);
1042 #if HAVE_VALGRIND_MEMCHECK_H
1043                 /* The data between the end of the line size and the stride is undefined but processed by
1044                    libswscale, causing lots of valgrind errors.  Mark it all defined to quell these errors.
1045                 */
1046                 VALGRIND_MAKE_MEM_DEFINED (_data[i], _stride[i] * (sample_size(i).height + 1) + ALIGNMENT);
1047 #endif
1048         }
1049 }
1050
1051
1052 Image::Image (Image const & other)
1053         : std::enable_shared_from_this<Image>(other)
1054         , _size (other._size)
1055         , _pixel_format (other._pixel_format)
1056         , _alignment (other._alignment)
1057 {
1058         allocate ();
1059
1060         for (int i = 0; i < planes(); ++i) {
1061                 uint8_t* p = _data[i];
1062                 uint8_t* q = other._data[i];
1063                 int const lines = sample_size(i).height;
1064                 for (int j = 0; j < lines; ++j) {
1065                         memcpy (p, q, _line_size[i]);
1066                         p += stride()[i];
1067                         q += other.stride()[i];
1068                 }
1069         }
1070 }
1071
1072
1073 Image::Image (AVFrame const * frame, Alignment alignment)
1074         : _size (frame->width, frame->height)
1075         , _pixel_format (static_cast<AVPixelFormat>(frame->format))
1076         , _alignment (alignment)
1077 {
1078         DCPOMATIC_ASSERT (_pixel_format != AV_PIX_FMT_NONE);
1079
1080         allocate ();
1081
1082         for (int i = 0; i < planes(); ++i) {
1083                 uint8_t* p = _data[i];
1084                 uint8_t* q = frame->data[i];
1085                 int const lines = sample_size(i).height;
1086                 for (int j = 0; j < lines; ++j) {
1087                         memcpy (p, q, _line_size[i]);
1088                         p += stride()[i];
1089                         /* AVFrame's linesize is what we call `stride' */
1090                         q += frame->linesize[i];
1091                 }
1092         }
1093 }
1094
1095
1096 Image::Image (shared_ptr<const Image> other, Alignment alignment)
1097         : _size (other->_size)
1098         , _pixel_format (other->_pixel_format)
1099         , _alignment (alignment)
1100 {
1101         allocate ();
1102
1103         for (int i = 0; i < planes(); ++i) {
1104                 DCPOMATIC_ASSERT (line_size()[i] == other->line_size()[i]);
1105                 uint8_t* p = _data[i];
1106                 uint8_t* q = other->data()[i];
1107                 int const lines = sample_size(i).height;
1108                 for (int j = 0; j < lines; ++j) {
1109                         memcpy (p, q, line_size()[i]);
1110                         p += stride()[i];
1111                         q += other->stride()[i];
1112                 }
1113         }
1114 }
1115
1116
1117 Image&
1118 Image::operator= (Image const & other)
1119 {
1120         if (this == &other) {
1121                 return *this;
1122         }
1123
1124         Image tmp (other);
1125         swap (tmp);
1126         return *this;
1127 }
1128
1129
1130 void
1131 Image::swap (Image & other)
1132 {
1133         std::swap (_size, other._size);
1134         std::swap (_pixel_format, other._pixel_format);
1135
1136         for (int i = 0; i < 4; ++i) {
1137                 std::swap (_data[i], other._data[i]);
1138                 std::swap (_line_size[i], other._line_size[i]);
1139                 std::swap (_stride[i], other._stride[i]);
1140         }
1141
1142         std::swap (_alignment, other._alignment);
1143 }
1144
1145
1146 Image::~Image ()
1147 {
1148         for (int i = 0; i < planes(); ++i) {
1149                 av_free (_data[i]);
1150         }
1151
1152         av_free (_data);
1153         av_free (_line_size);
1154         av_free (_stride);
1155 }
1156
1157
1158 uint8_t * const *
1159 Image::data () const
1160 {
1161         return _data;
1162 }
1163
1164
1165 int const *
1166 Image::line_size () const
1167 {
1168         return _line_size;
1169 }
1170
1171
1172 int const *
1173 Image::stride () const
1174 {
1175         return _stride;
1176 }
1177
1178
1179 dcp::Size
1180 Image::size () const
1181 {
1182         return _size;
1183 }
1184
1185
1186 Image::Alignment
1187 Image::alignment () const
1188 {
1189         return _alignment;
1190 }
1191
1192
1193 PositionImage
1194 merge (list<PositionImage> images, Image::Alignment alignment)
1195 {
1196         if (images.empty ()) {
1197                 return {};
1198         }
1199
1200         if (images.size() == 1) {
1201                 images.front().image = Image::ensure_alignment(images.front().image, alignment);
1202                 return images.front();
1203         }
1204
1205         dcpomatic::Rect<int> all (images.front().position, images.front().image->size().width, images.front().image->size().height);
1206         for (auto const& i: images) {
1207                 all.extend (dcpomatic::Rect<int>(i.position, i.image->size().width, i.image->size().height));
1208         }
1209
1210         auto merged = make_shared<Image>(images.front().image->pixel_format(), dcp::Size(all.width, all.height), alignment);
1211         merged->make_transparent ();
1212         for (auto const& i: images) {
1213                 merged->alpha_blend (i.image, i.position - all.position());
1214         }
1215
1216         return PositionImage (merged, all.position ());
1217 }
1218
1219
1220 bool
1221 operator== (Image const & a, Image const & b)
1222 {
1223         if (a.planes() != b.planes() || a.pixel_format() != b.pixel_format() || a.alignment() != b.alignment()) {
1224                 return false;
1225         }
1226
1227         for (int c = 0; c < a.planes(); ++c) {
1228                 if (a.sample_size(c).height != b.sample_size(c).height || a.line_size()[c] != b.line_size()[c] || a.stride()[c] != b.stride()[c]) {
1229                         return false;
1230                 }
1231
1232                 uint8_t* p = a.data()[c];
1233                 uint8_t* q = b.data()[c];
1234                 int const lines = a.sample_size(c).height;
1235                 for (int y = 0; y < lines; ++y) {
1236                         if (memcmp (p, q, a.line_size()[c]) != 0) {
1237                                 return false;
1238                         }
1239
1240                         p += a.stride()[c];
1241                         q += b.stride()[c];
1242                 }
1243         }
1244
1245         return true;
1246 }
1247
1248
1249 /** Fade the image.
1250  *  @param f Amount to fade by; 0 is black, 1 is no fade.
1251  */
1252 void
1253 Image::fade (float f)
1254 {
1255         /* U/V black value for 8-bit colour */
1256         static int const eight_bit_uv =    (1 << 7) - 1;
1257         /* U/V black value for 10-bit colour */
1258         static uint16_t const ten_bit_uv = (1 << 9) - 1;
1259
1260         switch (_pixel_format) {
1261         case AV_PIX_FMT_YUV420P:
1262         {
1263                 /* Y */
1264                 uint8_t* p = data()[0];
1265                 int const lines = sample_size(0).height;
1266                 for (int y = 0; y < lines; ++y) {
1267                         uint8_t* q = p;
1268                         for (int x = 0; x < line_size()[0]; ++x) {
1269                                 *q = int(float(*q) * f);
1270                                 ++q;
1271                         }
1272                         p += stride()[0];
1273                 }
1274
1275                 /* U, V */
1276                 for (int c = 1; c < 3; ++c) {
1277                         uint8_t* p = data()[c];
1278                         int const lines = sample_size(c).height;
1279                         for (int y = 0; y < lines; ++y) {
1280                                 uint8_t* q = p;
1281                                 for (int x = 0; x < line_size()[c]; ++x) {
1282                                         *q = eight_bit_uv + int((int(*q) - eight_bit_uv) * f);
1283                                         ++q;
1284                                 }
1285                                 p += stride()[c];
1286                         }
1287                 }
1288
1289                 break;
1290         }
1291
1292         case AV_PIX_FMT_RGB24:
1293         {
1294                 /* 8-bit */
1295                 uint8_t* p = data()[0];
1296                 int const lines = sample_size(0).height;
1297                 for (int y = 0; y < lines; ++y) {
1298                         uint8_t* q = p;
1299                         for (int x = 0; x < line_size()[0]; ++x) {
1300                                 *q = int (float (*q) * f);
1301                                 ++q;
1302                         }
1303                         p += stride()[0];
1304                 }
1305                 break;
1306         }
1307
1308         case AV_PIX_FMT_XYZ12LE:
1309         case AV_PIX_FMT_RGB48LE:
1310                 /* 16-bit little-endian */
1311                 for (int c = 0; c < 3; ++c) {
1312                         int const stride_pixels = stride()[c] / 2;
1313                         int const line_size_pixels = line_size()[c] / 2;
1314                         uint16_t* p = reinterpret_cast<uint16_t*> (data()[c]);
1315                         int const lines = sample_size(c).height;
1316                         for (int y = 0; y < lines; ++y) {
1317                                 uint16_t* q = p;
1318                                 for (int x = 0; x < line_size_pixels; ++x) {
1319                                         *q = int (float (*q) * f);
1320                                         ++q;
1321                                 }
1322                                 p += stride_pixels;
1323                         }
1324                 }
1325                 break;
1326
1327         case AV_PIX_FMT_YUV422P10LE:
1328         {
1329                 /* Y */
1330                 {
1331                         int const stride_pixels = stride()[0] / 2;
1332                         int const line_size_pixels = line_size()[0] / 2;
1333                         uint16_t* p = reinterpret_cast<uint16_t*> (data()[0]);
1334                         int const lines = sample_size(0).height;
1335                         for (int y = 0; y < lines; ++y) {
1336                                 uint16_t* q = p;
1337                                 for (int x = 0; x < line_size_pixels; ++x) {
1338                                         *q = int(float(*q) * f);
1339                                         ++q;
1340                                 }
1341                                 p += stride_pixels;
1342                         }
1343                 }
1344
1345                 /* U, V */
1346                 for (int c = 1; c < 3; ++c) {
1347                         int const stride_pixels = stride()[c] / 2;
1348                         int const line_size_pixels = line_size()[c] / 2;
1349                         uint16_t* p = reinterpret_cast<uint16_t*> (data()[c]);
1350                         int const lines = sample_size(c).height;
1351                         for (int y = 0; y < lines; ++y) {
1352                                 uint16_t* q = p;
1353                                 for (int x = 0; x < line_size_pixels; ++x) {
1354                                         *q = ten_bit_uv + int((int(*q) - ten_bit_uv) * f);
1355                                         ++q;
1356                                 }
1357                                 p += stride_pixels;
1358                         }
1359                 }
1360                 break;
1361
1362         }
1363
1364         default:
1365                 throw PixelFormatError ("fade()", _pixel_format);
1366         }
1367 }
1368
1369
1370 shared_ptr<const Image>
1371 Image::ensure_alignment (shared_ptr<const Image> image, Image::Alignment alignment)
1372 {
1373         if (image->alignment() == alignment) {
1374                 return image;
1375         }
1376
1377         return make_shared<Image>(image, alignment);
1378 }
1379
1380
1381 size_t
1382 Image::memory_used () const
1383 {
1384         size_t m = 0;
1385         for (int i = 0; i < planes(); ++i) {
1386                 m += _stride[i] * sample_size(i).height;
1387         }
1388         return m;
1389 }
1390
1391
1392 void
1393 Image::video_range_to_full_range ()
1394 {
1395         switch (_pixel_format) {
1396         case AV_PIX_FMT_RGB24:
1397         {
1398                 float const factor = 256.0 / 219.0;
1399                 uint8_t* p = data()[0];
1400                 int const lines = sample_size(0).height;
1401                 for (int y = 0; y < lines; ++y) {
1402                         uint8_t* q = p;
1403                         for (int x = 0; x < line_size()[0]; ++x) {
1404                                 *q = clamp(lrintf((*q - 16) * factor), 0L, 255L);
1405                                 ++q;
1406                         }
1407                         p += stride()[0];
1408                 }
1409                 break;
1410         }
1411         case AV_PIX_FMT_RGB48LE:
1412         {
1413                 float const factor = 65536.0 / 56064.0;
1414                 uint16_t* p = reinterpret_cast<uint16_t*>(data()[0]);
1415                 int const lines = sample_size(0).height;
1416                 for (int y = 0; y < lines; ++y) {
1417                         uint16_t* q = p;
1418                         int const line_size_pixels = line_size()[0] / 2;
1419                         for (int x = 0; x < line_size_pixels; ++x) {
1420                                 *q = clamp(lrintf((*q - 4096) * factor), 0L, 65535L);
1421                                 ++q;
1422                         }
1423                         p += stride()[0] / 2;
1424                 }
1425                 break;
1426         }
1427         case AV_PIX_FMT_GBRP12LE:
1428         {
1429                 float const factor = 4096.0 / 3504.0;
1430                 for (int c = 0; c < 3; ++c) {
1431                         uint16_t* p = reinterpret_cast<uint16_t*>(data()[c]);
1432                         int const lines = sample_size(c).height;
1433                         for (int y = 0; y < lines; ++y) {
1434                                 uint16_t* q = p;
1435                                 int const line_size_pixels = line_size()[c] / 2;
1436                                 for (int x = 0; x < line_size_pixels; ++x) {
1437                                         *q = clamp(lrintf((*q - 256) * factor), 0L, 4095L);
1438                                         ++q;
1439                                 }
1440                         }
1441                 }
1442                 break;
1443         }
1444         default:
1445                 throw PixelFormatError ("video_range_to_full_range()", _pixel_format);
1446         }
1447 }
1448