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