Don't overlap simultaneous video content in the timeline. Fix keep-aligned for separ...
[dcpomatic.git] / src / lib / image.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file src/image.cc
21  *  @brief A class to describe a video image.
22  */
23
24 #include <iostream>
25 extern "C" {
26 #include <libswscale/swscale.h>
27 #include <libavutil/pixfmt.h>
28 #include <libavutil/pixdesc.h>
29 }
30 #include "image.h"
31 #include "exceptions.h"
32 #include "scaler.h"
33
34 #include "i18n.h"
35
36 using std::string;
37 using std::min;
38 using std::cout;
39 using std::cerr;
40 using boost::shared_ptr;
41 using libdcp::Size;
42
43 int
44 Image::line_factor (int n) const
45 {
46         if (n == 0) {
47                 return 1;
48         }
49
50         AVPixFmtDescriptor const * d = av_pix_fmt_desc_get(_pixel_format);
51         if (!d) {
52                 throw PixelFormatError ("lines()", _pixel_format);
53         }
54         
55         return pow (2.0f, d->log2_chroma_h);
56 }
57
58 /** @param n Component index.
59  *  @return Number of lines in the image for the given component.
60  */
61 int
62 Image::lines (int n) const
63 {
64         return rint (ceil (static_cast<double>(size().height) / line_factor (n)));
65 }
66
67 /** @return Number of components */
68 int
69 Image::components () const
70 {
71         AVPixFmtDescriptor const * d = av_pix_fmt_desc_get(_pixel_format);
72         if (!d) {
73                 throw PixelFormatError ("components()", _pixel_format);
74         }
75
76         if ((d->flags & PIX_FMT_PLANAR) == 0) {
77                 return 1;
78         }
79         
80         return d->nb_components;
81 }
82
83 /** Crop this image, scale it to `inter_size' and then place it in a black frame of `out_size' */
84 shared_ptr<Image>
85 Image::crop_scale_window (Crop crop, libdcp::Size inter_size, libdcp::Size out_size, Scaler const * scaler, AVPixelFormat out_format, bool out_aligned) const
86 {
87         assert (scaler);
88         /* Empirical testing suggests that sws_scale() will crash if
89            the input image is not aligned.
90         */
91         assert (aligned ());
92
93         assert (out_size.width >= inter_size.width);
94         assert (out_size.height >= inter_size.height);
95
96         /* Here's an image of out_size */
97         shared_ptr<Image> out (new Image (out_format, out_size, out_aligned));
98         out->make_black ();
99
100         /* Size of the image after any crop */
101         libdcp::Size const cropped_size = crop.apply (size ());
102
103         /* Scale context for a scale from cropped_size to inter_size */
104         struct SwsContext* scale_context = sws_getContext (
105                 cropped_size.width, cropped_size.height, pixel_format(),
106                 inter_size.width, inter_size.height, out_format,
107                 scaler->ffmpeg_id (), 0, 0, 0
108                 );
109
110         if (!scale_context) {
111                 throw StringError (N_("Could not allocate SwsContext"));
112         }
113
114         /* Prepare input data pointers with crop */
115         uint8_t* scale_in_data[components()];
116         for (int c = 0; c < components(); ++c) {
117                 scale_in_data[c] = data()[c] + int (rint (bytes_per_pixel(c) * crop.left)) + stride()[c] * (crop.top / line_factor(c));
118         }
119
120         /* Corner of the image within out_size */
121         Position<int> const corner ((out_size.width - inter_size.width) / 2, (out_size.height - inter_size.height) / 2);
122
123         uint8_t* scale_out_data[out->components()];
124         for (int c = 0; c < out->components(); ++c) {
125                 scale_out_data[c] = out->data()[c] + int (rint (out->bytes_per_pixel(c) * corner.x)) + out->stride()[c] * corner.y;
126         }
127
128         sws_scale (
129                 scale_context,
130                 scale_in_data, stride(),
131                 0, cropped_size.height,
132                 scale_out_data, out->stride()
133                 );
134
135         sws_freeContext (scale_context);
136
137         return out;     
138 }
139
140 shared_ptr<Image>
141 Image::scale (libdcp::Size out_size, Scaler const * scaler, AVPixelFormat out_format, bool out_aligned) const
142 {
143         assert (scaler);
144         /* Empirical testing suggests that sws_scale() will crash if
145            the input image is not aligned.
146         */
147         assert (aligned ());
148
149         shared_ptr<Image> scaled (new Image (out_format, out_size, out_aligned));
150
151         struct SwsContext* scale_context = sws_getContext (
152                 size().width, size().height, pixel_format(),
153                 out_size.width, out_size.height, out_format,
154                 scaler->ffmpeg_id (), 0, 0, 0
155                 );
156
157         sws_scale (
158                 scale_context,
159                 data(), stride(),
160                 0, size().height,
161                 scaled->data(), scaled->stride()
162                 );
163
164         sws_freeContext (scale_context);
165
166         return scaled;
167 }
168
169 shared_ptr<Image>
170 Image::crop (Crop crop, bool aligned) const
171 {
172         libdcp::Size cropped_size = crop.apply (size ());
173         shared_ptr<Image> out (new Image (pixel_format(), cropped_size, aligned));
174
175         for (int c = 0; c < components(); ++c) {
176                 int const crop_left_in_bytes = bytes_per_pixel(c) * crop.left;
177                 /* bytes_per_pixel() could be a fraction; in this case the stride will be rounded
178                    up, and we need to make sure that we copy over the width (up to the stride)
179                    rather than short of the width; hence the ceil() here.
180                 */
181                 int const cropped_width_in_bytes = ceil (bytes_per_pixel(c) * cropped_size.width);
182
183                 /* Start of the source line, cropped from the top but not the left */
184                 uint8_t* in_p = data()[c] + (crop.top / out->line_factor(c)) * stride()[c];
185                 uint8_t* out_p = out->data()[c];
186
187                 for (int y = 0; y < out->lines(c); ++y) {
188                         memcpy (out_p, in_p + crop_left_in_bytes, cropped_width_in_bytes);
189                         in_p += stride()[c];
190                         out_p += out->stride()[c];
191                 }
192         }
193
194         return out;
195 }
196
197 /** Blacken a YUV image whose bits per pixel is rounded up to 16 */
198 void
199 Image::yuv_16_black (uint16_t v, bool alpha)
200 {
201         memset (data()[0], 0, lines(0) * stride()[0]);
202         for (int i = 1; i < 3; ++i) {
203                 int16_t* p = reinterpret_cast<int16_t*> (data()[i]);
204                 for (int y = 0; y < lines(i); ++y) {
205                         /* We divide by 2 here because we are writing 2 bytes at a time */
206                         for (int x = 0; x < line_size()[i] / 2; ++x) {
207                                 p[x] = v;
208                         }
209                         p += stride()[i] / 2;
210                 }
211         }
212
213         if (alpha) {
214                 memset (data()[3], 0, lines(3) * stride()[3]);
215         }
216 }
217
218 uint16_t
219 Image::swap_16 (uint16_t v)
220 {
221         return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
222 }
223
224 void
225 Image::make_black ()
226 {
227         /* U/V black value for 8-bit colour */
228         static uint8_t const eight_bit_uv =     (1 << 7) - 1;
229         /* U/V black value for 9-bit colour */
230         static uint16_t const nine_bit_uv =     (1 << 8) - 1;
231         /* U/V black value for 10-bit colour */
232         static uint16_t const ten_bit_uv =      (1 << 9) - 1;
233         /* U/V black value for 16-bit colour */
234         static uint16_t const sixteen_bit_uv =  (1 << 15) - 1;
235         
236         switch (_pixel_format) {
237         case PIX_FMT_YUV420P:
238         case PIX_FMT_YUV422P:
239         case PIX_FMT_YUV444P:
240         case PIX_FMT_YUV411P:
241                 memset (data()[0], 0, lines(0) * stride()[0]);
242                 memset (data()[1], eight_bit_uv, lines(1) * stride()[1]);
243                 memset (data()[2], eight_bit_uv, lines(2) * stride()[2]);
244                 break;
245
246         case PIX_FMT_YUVJ420P:
247         case PIX_FMT_YUVJ422P:
248         case PIX_FMT_YUVJ444P:
249                 memset (data()[0], 0, lines(0) * stride()[0]);
250                 memset (data()[1], eight_bit_uv + 1, lines(1) * stride()[1]);
251                 memset (data()[2], eight_bit_uv + 1, lines(2) * stride()[2]);
252                 break;
253
254         case PIX_FMT_YUV422P9LE:
255         case PIX_FMT_YUV444P9LE:
256                 yuv_16_black (nine_bit_uv, false);
257                 break;
258
259         case PIX_FMT_YUV422P9BE:
260         case PIX_FMT_YUV444P9BE:
261                 yuv_16_black (swap_16 (nine_bit_uv), false);
262                 break;
263                 
264         case PIX_FMT_YUV422P10LE:
265         case PIX_FMT_YUV444P10LE:
266                 yuv_16_black (ten_bit_uv, false);
267                 break;
268
269         case PIX_FMT_YUV422P16LE:
270         case PIX_FMT_YUV444P16LE:
271                 yuv_16_black (sixteen_bit_uv, false);
272                 break;
273                 
274         case PIX_FMT_YUV444P10BE:
275         case PIX_FMT_YUV422P10BE:
276                 yuv_16_black (swap_16 (ten_bit_uv), false);
277                 break;
278
279         case AV_PIX_FMT_YUVA420P9BE:
280         case AV_PIX_FMT_YUVA422P9BE:
281         case AV_PIX_FMT_YUVA444P9BE:
282                 yuv_16_black (swap_16 (nine_bit_uv), true);
283                 break;
284                 
285         case AV_PIX_FMT_YUVA420P9LE:
286         case AV_PIX_FMT_YUVA422P9LE:
287         case AV_PIX_FMT_YUVA444P9LE:
288                 yuv_16_black (nine_bit_uv, true);
289                 break;
290                 
291         case AV_PIX_FMT_YUVA420P10BE:
292         case AV_PIX_FMT_YUVA422P10BE:
293         case AV_PIX_FMT_YUVA444P10BE:
294                 yuv_16_black (swap_16 (ten_bit_uv), true);
295                 break;
296                 
297         case AV_PIX_FMT_YUVA420P10LE:
298         case AV_PIX_FMT_YUVA422P10LE:
299         case AV_PIX_FMT_YUVA444P10LE:
300                 yuv_16_black (ten_bit_uv, true);
301                 break;
302                 
303         case AV_PIX_FMT_YUVA420P16BE:
304         case AV_PIX_FMT_YUVA422P16BE:
305         case AV_PIX_FMT_YUVA444P16BE:
306                 yuv_16_black (swap_16 (sixteen_bit_uv), true);
307                 break;
308                 
309         case AV_PIX_FMT_YUVA420P16LE:
310         case AV_PIX_FMT_YUVA422P16LE:
311         case AV_PIX_FMT_YUVA444P16LE:
312                 yuv_16_black (sixteen_bit_uv, true);
313                 break;
314
315         case PIX_FMT_RGB24:
316         case PIX_FMT_ARGB:
317         case PIX_FMT_RGBA:
318         case PIX_FMT_ABGR:
319         case PIX_FMT_BGRA:
320                 memset (data()[0], 0, lines(0) * stride()[0]);
321                 break;
322
323         case PIX_FMT_UYVY422:
324         {
325                 int const Y = lines(0);
326                 int const X = line_size()[0];
327                 uint8_t* p = data()[0];
328                 for (int y = 0; y < Y; ++y) {
329                         for (int x = 0; x < X / 4; ++x) {
330                                 *p++ = eight_bit_uv; // Cb
331                                 *p++ = 0;            // Y0
332                                 *p++ = eight_bit_uv; // Cr
333                                 *p++ = 0;            // Y1
334                         }
335                 }
336                 break;
337         }
338
339         default:
340                 throw PixelFormatError ("make_black()", _pixel_format);
341         }
342 }
343
344 void
345 Image::alpha_blend (shared_ptr<const Image> other, Position<int> position)
346 {
347         /* Only implemented for RGBA onto RGB24 so far */
348         assert (_pixel_format == PIX_FMT_RGB24 && other->pixel_format() == PIX_FMT_RGBA);
349
350         int start_tx = position.x;
351         int start_ox = 0;
352
353         if (start_tx < 0) {
354                 start_ox = -start_tx;
355                 start_tx = 0;
356         }
357
358         int start_ty = position.y;
359         int start_oy = 0;
360
361         if (start_ty < 0) {
362                 start_oy = -start_ty;
363                 start_ty = 0;
364         }
365
366         for (int ty = start_ty, oy = start_oy; ty < size().height && oy < other->size().height; ++ty, ++oy) {
367                 uint8_t* tp = data()[0] + ty * stride()[0] + position.x * 3;
368                 uint8_t* op = other->data()[0] + oy * other->stride()[0];
369                 for (int tx = start_tx, ox = start_ox; tx < size().width && ox < other->size().width; ++tx, ++ox) {
370                         float const alpha = float (op[3]) / 255;
371                         tp[0] = (tp[0] * (1 - alpha)) + op[0] * alpha;
372                         tp[1] = (tp[1] * (1 - alpha)) + op[1] * alpha;
373                         tp[2] = (tp[2] * (1 - alpha)) + op[2] * alpha;
374                         tp += 3;
375                         op += 4;
376                 }
377         }
378 }
379
380 void
381 Image::copy (shared_ptr<const Image> other, Position<int> position)
382 {
383         /* Only implemented for RGB24 onto RGB24 so far */
384         assert (_pixel_format == PIX_FMT_RGB24 && other->pixel_format() == PIX_FMT_RGB24);
385         assert (position.x >= 0 && position.y >= 0);
386
387         int const N = min (position.x + other->size().width, size().width) - position.x;
388         for (int ty = position.y, oy = 0; ty < size().height && oy < other->size().height; ++ty, ++oy) {
389                 uint8_t * const tp = data()[0] + ty * stride()[0] + position.x * 3;
390                 uint8_t * const op = other->data()[0] + oy * other->stride()[0];
391                 memcpy (tp, op, N * 3);
392         }
393 }       
394
395 void
396 Image::read_from_socket (shared_ptr<Socket> socket)
397 {
398         for (int i = 0; i < components(); ++i) {
399                 uint8_t* p = data()[i];
400                 for (int y = 0; y < lines(i); ++y) {
401                         socket->read (p, line_size()[i]);
402                         p += stride()[i];
403                 }
404         }
405 }
406
407 void
408 Image::write_to_socket (shared_ptr<Socket> socket) const
409 {
410         for (int i = 0; i < components(); ++i) {
411                 uint8_t* p = data()[i];
412                 for (int y = 0; y < lines(i); ++y) {
413                         socket->write (p, line_size()[i]);
414                         p += stride()[i];
415                 }
416         }
417 }
418
419
420 float
421 Image::bytes_per_pixel (int c) const
422 {
423         AVPixFmtDescriptor const * d = av_pix_fmt_desc_get(_pixel_format);
424         if (!d) {
425                 throw PixelFormatError ("lines()", _pixel_format);
426         }
427
428         if (c >= components()) {
429                 return 0;
430         }
431
432         float bpp[4] = { 0, 0, 0, 0 };
433
434         bpp[0] = floor ((d->comp[0].depth_minus1 + 1 + 7) / 8);
435         if (d->nb_components > 1) {
436                 bpp[1] = floor ((d->comp[1].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
437         }
438         if (d->nb_components > 2) {
439                 bpp[2] = floor ((d->comp[2].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
440         }
441         if (d->nb_components > 3) {
442                 bpp[3] = floor ((d->comp[3].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
443         }
444         
445         if ((d->flags & PIX_FMT_PLANAR) == 0) {
446                 /* Not planar; sum them up */
447                 return bpp[0] + bpp[1] + bpp[2] + bpp[3];
448         }
449
450         return bpp[c];
451 }
452
453 /** Construct a Image of a given size and format, allocating memory
454  *  as required.
455  *
456  *  @param p Pixel format.
457  *  @param s Size in pixels.
458  */
459 Image::Image (AVPixelFormat p, libdcp::Size s, bool aligned)
460         : libdcp::Image (s)
461         , _pixel_format (p)
462         , _aligned (aligned)
463 {
464         allocate ();
465 }
466
467 void
468 Image::allocate ()
469 {
470         _data = (uint8_t **) wrapped_av_malloc (4 * sizeof (uint8_t *));
471         _data[0] = _data[1] = _data[2] = _data[3] = 0;
472         
473         _line_size = (int *) wrapped_av_malloc (4 * sizeof (int));
474         _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
475         
476         _stride = (int *) wrapped_av_malloc (4 * sizeof (int));
477         _stride[0] = _stride[1] = _stride[2] = _stride[3] = 0;
478
479         for (int i = 0; i < components(); ++i) {
480                 _line_size[i] = ceil (_size.width * bytes_per_pixel(i));
481                 _stride[i] = stride_round_up (i, _line_size, _aligned ? 32 : 1);
482
483                 /* The assembler function ff_rgb24ToY_avx (in libswscale/x86/input.asm)
484                    uses a 16-byte fetch to read three bytes (R/G/B) of image data.
485                    Hence on the last pixel of the last line it reads over the end of
486                    the actual data by 1 byte.  If the width of an image is a multiple
487                    of the stride alignment there will be no padding at the end of image lines.
488                    OS X crashes on this illegal read, though other operating systems don't
489                    seem to mind.  The nasty + 1 in this malloc makes sure there is always a byte
490                    for that instruction to read safely.
491                 */
492                 _data[i] = (uint8_t *) wrapped_av_malloc (_stride[i] * lines (i) + 1);
493         }
494 }
495
496 Image::Image (Image const & other)
497         : libdcp::Image (other)
498         ,  _pixel_format (other._pixel_format)
499         , _aligned (other._aligned)
500 {
501         allocate ();
502
503         for (int i = 0; i < components(); ++i) {
504                 uint8_t* p = _data[i];
505                 uint8_t* q = other._data[i];
506                 for (int j = 0; j < lines(i); ++j) {
507                         memcpy (p, q, _line_size[i]);
508                         p += stride()[i];
509                         q += other.stride()[i];
510                 }
511         }
512 }
513
514 Image::Image (AVFrame* frame)
515         : libdcp::Image (libdcp::Size (frame->width, frame->height))
516         , _pixel_format (static_cast<AVPixelFormat> (frame->format))
517         , _aligned (true)
518 {
519         allocate ();
520
521         for (int i = 0; i < components(); ++i) {
522                 uint8_t* p = _data[i];
523                 uint8_t* q = frame->data[i];
524                 for (int j = 0; j < lines(i); ++j) {
525                         memcpy (p, q, _line_size[i]);
526                         p += stride()[i];
527                         /* AVFrame's linesize is what we call `stride' */
528                         q += frame->linesize[i];
529                 }
530         }
531 }
532
533 Image::Image (shared_ptr<const Image> other, bool aligned)
534         : libdcp::Image (other)
535         , _pixel_format (other->_pixel_format)
536         , _aligned (aligned)
537 {
538         allocate ();
539
540         for (int i = 0; i < components(); ++i) {
541                 assert(line_size()[i] == other->line_size()[i]);
542                 uint8_t* p = _data[i];
543                 uint8_t* q = other->data()[i];
544                 for (int j = 0; j < lines(i); ++j) {
545                         memcpy (p, q, line_size()[i]);
546                         p += stride()[i];
547                         q += other->stride()[i];
548                 }
549         }
550 }
551
552 Image&
553 Image::operator= (Image const & other)
554 {
555         if (this == &other) {
556                 return *this;
557         }
558
559         Image tmp (other);
560         swap (tmp);
561         return *this;
562 }
563
564 void
565 Image::swap (Image & other)
566 {
567         libdcp::Image::swap (other);
568         
569         std::swap (_pixel_format, other._pixel_format);
570
571         for (int i = 0; i < 4; ++i) {
572                 std::swap (_data[i], other._data[i]);
573                 std::swap (_line_size[i], other._line_size[i]);
574                 std::swap (_stride[i], other._stride[i]);
575         }
576
577         std::swap (_aligned, other._aligned);
578 }
579
580 /** Destroy a Image */
581 Image::~Image ()
582 {
583         for (int i = 0; i < components(); ++i) {
584                 av_free (_data[i]);
585         }
586
587         av_free (_data);
588         av_free (_line_size);
589         av_free (_stride);
590 }
591
592 uint8_t **
593 Image::data () const
594 {
595         return _data;
596 }
597
598 int *
599 Image::line_size () const
600 {
601         return _line_size;
602 }
603
604 int *
605 Image::stride () const
606 {
607         return _stride;
608 }
609
610 libdcp::Size
611 Image::size () const
612 {
613         return _size;
614 }
615
616 bool
617 Image::aligned () const
618 {
619         return _aligned;
620 }
621