Merge branch 'master' of ssh://houllier/home/carl/git/dvdomatic
[dcpomatic.git] / src / lib / image.cc
index 0b887ea62c6979fcd1ba8b100435a28267b3bf5b..f28652d4e2cdac846773df4e202e69902d0c67cf 100644 (file)
@@ -68,7 +68,7 @@ Image::lines (int n) const
                throw PixelFormatError (N_("lines()"), _pixel_format);
        }
        
-       return size().height / pow(2, d->log2_chroma_h);
+       return size().height / pow(2.0f, d->log2_chroma_h);
 }
 
 /** @return Number of components */
@@ -273,13 +273,13 @@ void
 Image::make_black ()
 {
        /* U/V black value for 8-bit colour */
-       static uint8_t const eight_bit_uv = (1 << 7) - 1;
-       
+       static uint8_t const eight_bit_uv =     (1 << 7) - 1;
        /* U/V black value for 9-bit colour */
-       static uint16_t const nine_bit_uv = (1 << 8) - 1;
-
+       static uint16_t const nine_bit_uv =     (1 << 8) - 1;
        /* U/V black value for 10-bit colour */
-       static uint16_t const ten_bit_uv =  (1 << 9) - 1;
+       static uint16_t const ten_bit_uv =      (1 << 9) - 1;
+       /* U/V black value for 16-bit colour */
+       static uint16_t const sixteen_bit_uv =  (1 << 15) - 1;
        
        switch (_pixel_format) {
        case PIX_FMT_YUV420P:
@@ -290,6 +290,14 @@ Image::make_black ()
                memset (data()[2], eight_bit_uv, lines(2) * stride()[2]);
                break;
 
+       case PIX_FMT_YUVJ420P:
+       case PIX_FMT_YUVJ422P:
+       case PIX_FMT_YUVJ444P:
+               memset (data()[0], 0, lines(0) * stride()[0]);
+               memset (data()[1], eight_bit_uv + 1, lines(1) * stride()[1]);
+               memset (data()[2], eight_bit_uv + 1, lines(2) * stride()[2]);
+               break;
+
        case PIX_FMT_YUV422P9LE:
        case PIX_FMT_YUV444P9LE:
                yuv_16_black (nine_bit_uv);
@@ -304,11 +312,17 @@ Image::make_black ()
        case PIX_FMT_YUV444P10LE:
                yuv_16_black (ten_bit_uv);
                break;
+
+       case PIX_FMT_YUV422P16LE:
+       case PIX_FMT_YUV444P16LE:
+               yuv_16_black (sixteen_bit_uv);
+               break;
                
        case PIX_FMT_YUV444P10BE:
        case PIX_FMT_YUV422P10BE:
                yuv_16_black (swap_16 (ten_bit_uv));
-               
+               break;
+
        case PIX_FMT_RGB24:             
                memset (data()[0], 0, lines(0) * stride()[0]);
                break;
@@ -411,13 +425,13 @@ Image::bytes_per_pixel (int c) const
 
        bpp[0] = floor ((d->comp[0].depth_minus1 + 1 + 7) / 8);
        if (d->nb_components > 1) {
-               bpp[1] = floor ((d->comp[1].depth_minus1 + 1 + 7) / 8) / pow (2, d->log2_chroma_w);
+               bpp[1] = floor ((d->comp[1].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
        }
        if (d->nb_components > 2) {
-               bpp[2] = floor ((d->comp[2].depth_minus1 + 1 + 7) / 8) / pow (2, d->log2_chroma_w);
+               bpp[2] = floor ((d->comp[2].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
        }
        if (d->nb_components > 3) {
-               bpp[3] = floor ((d->comp[3].depth_minus1 + 1 + 7) / 8) / pow (2, d->log2_chroma_w);
+               bpp[3] = floor ((d->comp[3].depth_minus1 + 1 + 7) / 8) / pow (2.0f, d->log2_chroma_w);
        }
        
        if ((d->flags & PIX_FMT_PLANAR) == 0) {
@@ -463,10 +477,9 @@ SimpleImage::allocate ()
 
 SimpleImage::SimpleImage (SimpleImage const & other)
        : Image (other)
+       , _size (other._size)
+       , _aligned (other._aligned)
 {
-       _size = other._size;
-       _aligned = other._aligned;
-       
        allocate ();
 
        for (int i = 0; i < components(); ++i) {
@@ -480,6 +493,25 @@ SimpleImage::SimpleImage (SimpleImage const & other)
        }
 }
 
+SimpleImage::SimpleImage (AVFrame* frame)
+       : Image (static_cast<AVPixelFormat> (frame->format))
+       , _size (frame->width, frame->height)
+       , _aligned (true)
+{
+       allocate ();
+
+       for (int i = 0; i < components(); ++i) {
+               uint8_t* p = _data[i];
+               uint8_t* q = frame->data[i];
+               for (int j = 0; j < lines(i); ++j) {
+                       memcpy (p, q, _line_size[i]);
+                       p += stride()[i];
+                       /* AVFrame's linesize is what we call `stride' */
+                       q += frame->linesize[i];
+               }
+       }
+}
+
 SimpleImage::SimpleImage (shared_ptr<const Image> other)
        : Image (*other.get())
 {
@@ -570,55 +602,6 @@ SimpleImage::aligned () const
        return _aligned;
 }
 
-FrameImage::FrameImage (AVFrame* frame)
-       : Image (static_cast<AVPixelFormat> (frame->format))
-       , _frame (frame)
-{
-       _line_size = (int *) av_malloc (4 * sizeof (int));
-       _line_size[0] = _line_size[1] = _line_size[2] = _line_size[3] = 0;
-       
-       for (int i = 0; i < components(); ++i) {
-               _line_size[i] = size().width * bytes_per_pixel(i);
-       }
-}
-
-FrameImage::~FrameImage ()
-{
-       av_frame_free (&_frame);
-       av_free (_line_size);
-}
-
-uint8_t **
-FrameImage::data () const
-{
-       return _frame->data;
-}
-
-int *
-FrameImage::line_size () const
-{
-       return _line_size;
-}
-
-int *
-FrameImage::stride () const
-{
-       /* AVFrame's `linesize' is what we call `stride' */
-       return _frame->linesize;
-}
-
-libdcp::Size
-FrameImage::size () const
-{
-       return libdcp::Size (_frame->width, _frame->height);
-}
-
-bool
-FrameImage::aligned () const
-{
-       return true;
-}
-
 RGBPlusAlphaImage::RGBPlusAlphaImage (shared_ptr<const Image> im)
        : SimpleImage (im->pixel_format(), im->size(), false)
 {