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