[FFmpeg-devel] [PATCH v11 10/14] lavc/tiff: Support decoding of DNGs with single-component JPEGs

2019-08-08 Thread velocityra
From: Nick Renieris 

This enables decoding of DNG images generated by the 'DJI Zenmuse X7'
digital camera
Samples: https://www.dji.com/gr/zenmuse-x7/info#downloads

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 61 +++
 1 file changed, 51 insertions(+), 10 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 9d20763186..52c49e1bcf 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -274,7 +274,8 @@ static int add_metadata(int count, int type,
 }
 
 static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
-  const uint8_t *src, int src_stride, int 
width, int height, int is_u16);
+  const uint8_t *src, int src_stride, int 
width, int height,
+  int is_single_comp, int is_u16);
 
 static void av_always_inline horizontal_fill(TiffContext *s,
  unsigned int bpp, uint8_t* dst,
@@ -698,6 +699,7 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
  0, // no stride, only 1 line
  width / pixel_size_bytes * pixel_size_bits / s->bpp, 
// need to account for [1, 16] bpp
  1,
+ 0, // single-component variation is only preset in 
JPEG-encoded DNGs
  is_u16);
 }
 
@@ -795,18 +797,32 @@ static uint16_t av_always_inline 
dng_process_color8(uint16_t value,
 
 static void dng_blit(TiffContext *s, uint8_t *dst, int dst_stride,
  const uint8_t *src, int src_stride,
- int width, int height, int is_u16)
+ int width, int height, int is_single_comp, int is_u16)
 {
 int line, col;
 float scale_factor;
 
 scale_factor = 1.0f / (s->white_level - s->black_level);
 
-if (is_u16) {
-for (line = 0; line < height; line++) {
+if (is_single_comp) {
+if (!is_u16)
+return; /* <= 8bpp unsupported */
+
+/* Image is double the width and half the height we need, each row 
comprises 2 rows of the output
+   (split vertically in the middle). */
+for (line = 0; line < height / 2; line++) {
 uint16_t *dst_u16 = (uint16_t *)dst;
 uint16_t *src_u16 = (uint16_t *)src;
 
+/* Blit first half of input row row to initial row of output */
+for (col = 0; col < width; col++)
+*dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, 
s->black_level, scale_factor);
+
+/* Advance the destination pointer by a row (source pointer 
remains in the same place) */
+dst += dst_stride * sizeof(uint16_t);
+dst_u16 = (uint16_t *)dst;
+
+/* Blit second half of input row row to next row of output */
 for (col = 0; col < width; col++)
 *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, 
s->black_level, scale_factor);
 
@@ -814,12 +830,27 @@ static void dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
 src += src_stride * sizeof(uint16_t);
 }
 } else {
-for (line = 0; line < height; line++) {
-for (col = 0; col < width; col++)
-*dst++ = dng_process_color8(*src++, s->dng_lut, 
s->black_level, scale_factor);
+/* Input and output image are the same size and the MJpeg decoder has 
done per-component
+   deinterleaving, so blitting here is straightforward. */
+if (is_u16) {
+for (line = 0; line < height; line++) {
+uint16_t *dst_u16 = (uint16_t *)dst;
+uint16_t *src_u16 = (uint16_t *)src;
+
+for (col = 0; col < width; col++)
+*dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, 
s->black_level, scale_factor);
+
+dst += dst_stride * sizeof(uint16_t);
+src += src_stride * sizeof(uint16_t);
+}
+} else {
+for (line = 0; line < height; line++) {
+for (col = 0; col < width; col++)
+*dst++ = dng_process_color8(*src++, s->dng_lut, 
s->black_level, scale_factor);
 
-dst += dst_stride;
-src += src_stride;
+dst += dst_stride;
+src += src_stride;
+}
 }
 }
 }
@@ -831,7 +862,7 @@ static int dng_decode_jpeg_tile(AVCodecContext *avctx, 
AVFrame *frame,
 AVPacket jpkt;
 uint8_t *dst_data, *src_data;
 uint32_t dst_offset; /* offset from dst buffer in pixels */
-int is_u16, pixel_size;
+int is_single_comp, is_u16, pixel_size;
 int ret;
 
 /* Prepare a packet and send to the MJPEG decoder */
@@ -858,9 +889,18 @@ static int dng_decode_jpeg_tile(AVCodecContext *avctx, 
AVFrame *frame,
 
 /* Copy the outputted tile's pixels from 'jpgfram

[FFmpeg-devel] [PATCH v11 12/14] lavc/mjpegdec: Skip useless APPx marker on bayer images

2019-08-08 Thread velocityra
From: Nick Renieris 

Samples:
- Embedded JPEG images in the DNG images here:
  https://www.photographyblog.com/previews/pentax_k1_photos

Signed-off-by: Nick Renieris 
---
 libavcodec/mjpegdec.c | 11 +--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 94cf73549d..b3d6279853 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -1810,8 +1810,15 @@ static int mjpeg_decode_app(MJpegDecodeContext *s)
 int len, id, i;
 
 len = get_bits(&s->gb, 16);
-if (len < 6)
-return AVERROR_INVALIDDATA;
+if (len < 6) {
+if (s->bayer) {
+// Pentax K-1 (digital camera) JPEG images embedded in DNG images 
contain useless APP0 markers
+av_log(s->avctx, AV_LOG_WARNING, "skipping APPx (len=%"PRId32") 
for bayer-encoded image\n", len);
+skip_bits(&s->gb, len);
+return 0;
+} else
+return AVERROR_INVALIDDATA;
+}
 if (8 * len > get_bits_left(&s->gb))
 return AVERROR_INVALIDDATA;
 
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v11 14/14] lavc/tiff: Initialize WhiteLevel DNG tag value

2019-08-08 Thread velocityra
From: Nick Renieris 

Inited to (2^BitsPerSample)-1 as per the DNG Specification

This fixes decoding for "X7 CinemaDNG" samples here:
- https://www.dji.com/gr/zenmuse-x7/info#downloads

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 47140cddbc..47f2a5a441 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -1787,7 +1787,7 @@ again:
 s->photometric = TIFF_PHOTOMETRIC_NONE;
 s->compr   = TIFF_RAW;
 s->fill_order  = 0;
-s->white_level = 0;
+s->white_level = (1 << s->bpp) - 1; /* Default value as per the spec */
 s->is_bayer= 0;
 s->is_tiled= 0;
 s->is_jpeg = 0;
@@ -2044,7 +2044,7 @@ again:
 FFSWAP(int,  p->linesize[0], p->linesize[1]);
 }
 
-if (s->is_bayer && s->white_level && s->bpp == 16 &&
+if (s->is_bayer && (s->white_level != (1 << s->bpp) - 1) && s->bpp == 16 &&
 !(s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == 
TIFF_TYPE_CINEMADNG)) {
 uint16_t *dst = (uint16_t *)p->data[0];
 for (i = 0; i < s->height; i++) {
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v11 09/14] lavc/mjpegdec: Enable decoding of single-component bayer images

2019-08-08 Thread velocityra
From: Nick Renieris 

Signed-off-by: Nick Renieris 
---
 libavcodec/mjpegdec.c | 35 ---
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 6391107f78..94cf73549d 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -412,13 +412,21 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 return AVERROR_PATCHWELCOME;
 }
 
-/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. They contain 
2
-   interleaved components and the width stored in their SOF3 markers is the
-   width of each one.  We only output a single component, therefore we need
-   to adjust the output image width. */
-if (s->lossless == 1 && nb_components == 2) {
-s->bayer = 1;
-width *= 2;
+/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. */
+if (s->lossless) {
+if (nb_components == 1) {
+/* They can contain 1 component, which is double the width and 
half the height
+   of the final image (rows are interleaved).  We don't handle the 
decoding in
+   this file, but leave that to the DNG decoder. */
+s->bayer = 1;
+} else if (nb_components == 2) {
+/* Or they can contain 2 interleaved components and the width 
stored in their
+   SOF3 markers is the width of each one.  We only output a single 
component,
+   therefore we need to adjust the output image width.  We handle 
the
+   deinterleaving (but not the debayering) in this file. */
+s->bayer = 1;
+width *= 2;
+}
 }
 
 /* if different size, realloc/alloc picture */
@@ -1184,10 +1192,15 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
 }
-} else if (s->bayer && nb_components == 2) {
-for (mb_x = 0; mb_x < width; mb_x++) {
-((uint16_t*)ptr)[2*mb_x + 0] = buffer[mb_x][0];
-((uint16_t*)ptr)[2*mb_x + 1] = buffer[mb_x][1];
+} else if (s->bayer) {
+if (nb_components == 1) {
+for (mb_x = 0; mb_x < width; mb_x++)
+((uint16_t*)ptr)[mb_x] = buffer[mb_x][0];
+} else if (nb_components == 2) {
+for (mb_x = 0; mb_x < width; mb_x++) {
+((uint16_t*)ptr)[2*mb_x + 0] = buffer[mb_x][0];
+((uint16_t*)ptr)[2*mb_x + 1] = buffer[mb_x][1];
+}
 }
 } else {
 for(i=0; ihttps://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v11 13/14] lavc/tiff: Support DNGs with striped (non-tiled) JPEGs images

2019-08-08 Thread velocityra
From: Nick Renieris 

DNG samples here can now be decoded:
- https://www.photographyblog.com/previews/pentax_k1_photos

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 73 +++
 1 file changed, 42 insertions(+), 31 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 7855c36aa7..47140cddbc 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -550,6 +550,8 @@ static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, 
int stride,
 return ret;
 }
 
+static int dng_decode_strip(AVCodecContext *avctx, AVFrame *frame);
+
 static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int 
stride,
  const uint8_t *src, int size, int strip_start, 
int lines)
 {
@@ -665,6 +667,17 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 
 is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == 
TIFF_TYPE_CINEMADNG);
 
+/* Decode JPEG-encoded DNGs with strips */
+if (s->compr == TIFF_NEWJPEG && is_dng) {
+if (s->strips > 1) {
+av_log(s->avctx, AV_LOG_ERROR, "More than one DNG JPEG strips 
unsupported\n");
+return AVERROR_PATCHWELCOME;
+}
+if ((ret = dng_decode_strip(s->avctx, p)) < 0)
+return ret;
+return 0;
+}
+
 for (line = 0; line < lines; line++) {
 if (src - ssrc > size) {
 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
@@ -859,8 +872,8 @@ static void dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
 }
 }
 
-static int dng_decode_jpeg_tile(AVCodecContext *avctx, AVFrame *frame,
-int tile_byte_count, int x, int y, int w, int 
h)
+static int dng_decode_jpeg(AVCodecContext *avctx, AVFrame *frame,
+   int tile_byte_count, int dst_x, int dst_y, int w, 
int h)
 {
 TiffContext *s = avctx->priv_data;
 AVPacket jpkt;
@@ -905,7 +918,7 @@ static int dng_decode_jpeg_tile(AVCodecContext *avctx, 
AVFrame *frame,
 return AVERROR_PATCHWELCOME;
 }
 
-dst_offset = x + frame->linesize[0] * y / pixel_size;
+dst_offset = dst_x + frame->linesize[0] * dst_y / pixel_size;
 dst_data = frame->data[0] + dst_offset * pixel_size;
 src_data = s->jpgframe->data[0];
 
@@ -924,7 +937,7 @@ static int dng_decode_jpeg_tile(AVCodecContext *avctx, 
AVFrame *frame,
 return 0;
 }
 
-static int dng_decode_tiles(AVCodecContext *avctx, AVFrame *frame)
+static int dng_decode_tiles(AVCodecContext *avctx, AVFrame *frame, AVPacket 
*avpkt)
 {
 TiffContext *s = avctx->priv_data;
 int tile_idx;
@@ -937,6 +950,12 @@ static int dng_decode_tiles(AVCodecContext *avctx, AVFrame 
*frame)
 int pos_x = 0, pos_y = 0;
 int ret;
 
+s->jpgframe->width  = s->tile_width;
+s->jpgframe->height = s->tile_length;
+
+s->avctx_mjpeg->width = s->tile_width;
+s->avctx_mjpeg->height = s->tile_length;
+
 has_width_leftover = (s->width % s->tile_width != 0);
 has_height_leftover = (s->height % s->tile_length != 0);
 
@@ -973,7 +992,7 @@ static int dng_decode_tiles(AVCodecContext *avctx, AVFrame 
*frame)
 bytestream2_seek(&s->gb, tile_offset, SEEK_SET);
 
 /* Decode JPEG tile and copy it in the reference frame */
-ret = dng_decode_jpeg_tile(avctx, frame, tile_byte_count, pos_x, 
pos_y, tile_width, tile_length);
+ret = dng_decode_jpeg(avctx, frame, tile_byte_count, pos_x, pos_y, 
tile_width, tile_length);
 
 if (ret < 0)
 return ret;
@@ -986,30 +1005,24 @@ static int dng_decode_tiles(AVCodecContext *avctx, 
AVFrame *frame)
 }
 }
 
-return 0;
-}
+/* Frame is ready to be output */
+frame->pict_type = AV_PICTURE_TYPE_I;
+frame->key_frame = 1;
 
-static int dng_decode(AVCodecContext *avctx, AVFrame *frame, AVPacket *avpkt) {
-int ret;
+return avpkt->size;
+}
 
+static int dng_decode_strip(AVCodecContext *avctx, AVFrame *frame)
+{
 TiffContext *s = avctx->priv_data;
 
-s->jpgframe->width  = s->tile_width;
-s->jpgframe->height = s->tile_length;
-
-s->avctx_mjpeg->width = s->tile_width;
-s->avctx_mjpeg->height = s->tile_length;
-
-/* Decode all tiles in a frame */
-ret = dng_decode_tiles(avctx, frame);
-if (ret < 0)
-return ret;
+s->jpgframe->width  = s->width;
+s->jpgframe->height = s->height;
 
-/* Frame is ready to be output */
-frame->pict_type = AV_PICTURE_TYPE_I;
-frame->key_frame = 1;
+s->avctx_mjpeg->width = s->width;
+s->avctx_mjpeg->height = s->height;
 
-return avpkt->size;
+return dng_decode_jpeg(avctx, frame, s->stripsize, 0, 0, s->width, 
s->height);
 }
 
 static int init_image(TiffContext *s, ThreadFrame *frame)
@@ -1873,17 +1886,15 @@ again:
 
 /* Handle DNG images with JPEG-compressed tiles */
 
-if (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG) {
-if (!s->is_jpeg && s->is_ti

[FFmpeg-devel] [PATCH v11 07/14] lavc/tiff: Don't apply strips-related logic to tiled images

2019-08-08 Thread velocityra
From: Nick Renieris 

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 42 ++
 1 file changed, 22 insertions(+), 20 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 37fda15162..174ca168c6 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -1780,7 +1780,7 @@ again:
 }
 }
 
-if (!s->strippos && !s->stripoff) {
+if (!s->is_tiled && !s->strippos && !s->stripoff) {
 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
 return AVERROR_INVALIDDATA;
 }
@@ -1788,27 +1788,29 @@ again:
 if ((ret = init_image(s, &frame)) < 0)
 return ret;
 
-if (s->strips == 1 && !s->stripsize) {
-av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
-s->stripsize = avpkt->size - s->stripoff;
-}
+if (!s->is_tiled) {
+if (s->strips == 1 && !s->stripsize) {
+av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
+s->stripsize = avpkt->size - s->stripoff;
+}
 
-if (s->stripsizesoff) {
-if (s->stripsizesoff >= (unsigned)avpkt->size)
-return AVERROR_INVALIDDATA;
-bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
- avpkt->size - s->stripsizesoff);
-}
-if (s->strippos) {
-if (s->strippos >= (unsigned)avpkt->size)
-return AVERROR_INVALIDDATA;
-bytestream2_init(&stripdata, avpkt->data + s->strippos,
- avpkt->size - s->strippos);
-}
+if (s->stripsizesoff) {
+if (s->stripsizesoff >= (unsigned)avpkt->size)
+return AVERROR_INVALIDDATA;
+bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
+avpkt->size - s->stripsizesoff);
+}
+if (s->strippos) {
+if (s->strippos >= (unsigned)avpkt->size)
+return AVERROR_INVALIDDATA;
+bytestream2_init(&stripdata, avpkt->data + s->strippos,
+avpkt->size - s->strippos);
+}
 
-if (s->rps <= 0 || s->rps % s->subsampling[1]) {
-av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
-return AVERROR_INVALIDDATA;
+if (s->rps <= 0 || s->rps % s->subsampling[1]) {
+av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
+return AVERROR_INVALIDDATA;
+}
 }
 
 /* Handle DNG images with JPEG-compressed tiles */
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v11 08/14] lavc/tiff: Force DNG pixel data endianness on an edge case

2019-08-08 Thread velocityra
From: Nick Renieris 

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 12 
 1 file changed, 12 insertions(+)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 174ca168c6..9d20763186 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -1038,6 +1038,18 @@ static int init_image(TiffContext *s, ThreadFrame *frame)
AV_RL32(s->pattern));
 return AVERROR_PATCHWELCOME;
 }
+/* Force endianness as mentioned in 'DNG Specification: Chapter 3: 
BitsPerSample'
+NOTE: The spec actually specifies big-endian, not sure why we need 
little-endian,
+  but such images don't work otherwise. */
+if ((s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == 
TIFF_TYPE_CINEMADNG)
+&& (s->bpp != 8 && s->bpp != 16 && s->bpp != 32)) {
+switch (s->avctx->pix_fmt) {
+case AV_PIX_FMT_BAYER_RGGB16BE: s->avctx->pix_fmt = 
AV_PIX_FMT_BAYER_RGGB16LE; break;
+case AV_PIX_FMT_BAYER_BGGR16BE: s->avctx->pix_fmt = 
AV_PIX_FMT_BAYER_BGGR16LE; break;
+case AV_PIX_FMT_BAYER_GBRG16BE: s->avctx->pix_fmt = 
AV_PIX_FMT_BAYER_GBRG16LE; break;
+case AV_PIX_FMT_BAYER_GRBG16BE: s->avctx->pix_fmt = 
AV_PIX_FMT_BAYER_GRBG16LE; break;
+}
+}
 break;
 case 10161:
 switch (AV_RL32(s->pattern)) {
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v11 06/14] lavc/tiff: Fix edge case with full-length/width tiles

2019-08-08 Thread velocityra
From: Nick Renieris 

In an image [1], the height was equal to the tile length (full-height
tile) and after `height % tile_length` was applied to them with the
current code, it resulted in the operating tile_length to be 0.  This
commit makes this leftover logic only applies if it's necessary.

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 8 ++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 4620508d53..37fda15162 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -887,10 +887,14 @@ static int dng_decode_tiles(AVCodecContext *avctx, 
AVFrame *frame)
 int tile_byte_count_offset, tile_byte_count;
 int tile_count_x, tile_count_y;
 int tile_width, tile_length;
+int has_width_leftover, has_height_leftover;
 int tile_x = 0, tile_y = 0;
 int pos_x = 0, pos_y = 0;
 int ret;
 
+has_width_leftover = (s->width % s->tile_width != 0);
+has_height_leftover = (s->height % s->tile_length != 0);
+
 /* Calculate tile counts (round up) */
 tile_count_x = (s->width + s->tile_width - 1) / s->tile_width;
 tile_count_y = (s->height + s->tile_length - 1) / s->tile_length;
@@ -900,12 +904,12 @@ static int dng_decode_tiles(AVCodecContext *avctx, 
AVFrame *frame)
 tile_x = tile_idx % tile_count_x;
 tile_y = tile_idx / tile_count_x;
 
-if (tile_x == tile_count_x - 1) // If on the right edge
+if (has_width_leftover && tile_x == tile_count_x - 1) // If on the 
right-most tile
 tile_width = s->width % s->tile_width;
 else
 tile_width = s->tile_width;
 
-if (tile_y == tile_count_y - 1) // If on the bottom edge
+if (has_height_leftover && tile_y == tile_count_y - 1) // If on the 
bottom-most tile
 tile_length = s->height % s->tile_length;
 else
 tile_length = s->tile_length;
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v11 05/14] lavc/jpegtables: Handle multiple mappings to the same value

2019-08-08 Thread velocityra
From: Nick Renieris 

Some JPEGs [1] have incorrect DHT entries that map 2 codes to
the same value.

The second (last) mapping does not ever actually appear in the
code stream, therefore ignoring any mappings after the first one
fixes this.

Without this, an "mjpeg_decode_dc: bad vlc: 0:0" error is thrown.

---

[1]: Embedded JPEGs in "X7 RAW" and "X7 CinemaDNG" samples here:
 https://www.dji.com/gr/zenmuse-x7/info#downloads

Signed-off-by: Nick Renieris 
---
 libavcodec/jpegtables.c | 19 ---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/libavcodec/jpegtables.c b/libavcodec/jpegtables.c
index cbe5523cb4..6f596cfc92 100644
--- a/libavcodec/jpegtables.c
+++ b/libavcodec/jpegtables.c
@@ -130,14 +130,27 @@ void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, 
uint16_t *huff_code,
 {
 int i, j, k,nb, code, sym;
 
-code = 0;
+/* Zero-initialize huff_size (needed for multiple mappings check below) */
+k = 0;
+for(i=1;i<=16;i++) {
+nb = bits_table[i];
+for(j=0;jhttps://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v11 11/14] lavc/tiff: Decode 14-bit DNG images

2019-08-08 Thread velocityra
From: Nick Renieris 

Sample file: 
https://drive.google.com/open?id=0B4JyRT3Lth5HVndyOTVOdWktM3J4TFEydTk1MnY3RWlpSzVB

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 21 +
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 52c49e1bcf..7855c36aa7 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -309,14 +309,18 @@ static void av_always_inline horizontal_fill(TiffContext 
*s,
 dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
 }
 break;
-case 12: {
- uint16_t *dst16 = (uint16_t *)dst;
- GetBitContext gb;
- init_get_bits8(&gb, src, width);
- for (int i = 0; i < s->width; i++) {
- dst16[i] = get_bits(&gb, 12) << 4;
- }
- }
+case 12:
+case 14: {
+uint16_t *dst16 = (uint16_t *)dst;
+int is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == 
TIFF_TYPE_CINEMADNG);
+uint8_t shift = is_dng ? 0 : 16 - bpp;
+GetBitContext gb;
+
+init_get_bits8(&gb, src, width);
+for (int i = 0; i < s->width; i++) {
+dst16[i] = get_bits(&gb, bpp) << shift;
+}
+}
 break;
 default:
 if (usePtr) {
@@ -1061,6 +1065,7 @@ static int init_image(TiffContext *s, ThreadFrame *frame)
 }
 break;
 case 10121:
+case 10141:
 switch (AV_RL32(s->pattern)) {
 case 0x02010100:
 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_BAYER_RGGB16LE : 
AV_PIX_FMT_BAYER_RGGB16BE;
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v11 04/14] lavc/tiff: Apply color scaling to uncompressed DNGs

2019-08-08 Thread velocityra
From: Nick Renieris 

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 25 -
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index a118c37c41..4620508d53 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -556,6 +556,7 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
  (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
  desc->nb_components >= 3;
+int is_dng;
 
 if (s->planar)
 width /= s->bppcount;
@@ -657,6 +658,8 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 bytestream2_init(&s->gb, src, size);
 bytestream2_init_writer(&pb, dst, is_yuv ? s->yuv_line_size : (stride * 
lines));
 
+is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == 
TIFF_TYPE_CINEMADNG);
+
 for (line = 0; line < lines; line++) {
 if (src - ssrc > size) {
 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
@@ -679,6 +682,25 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 for (i = 0; i < width; i++)
 dst[i] = ff_reverse[src[i]];
 }
+
+/* Color processing for DNG images with uncompressed strips 
(non-tiled) */
+if (is_dng) {
+int is_u16, pixel_size_bytes, pixel_size_bits;
+
+is_u16 = (s->bpp > 8);
+pixel_size_bits = (is_u16 ? 16 : 8);
+pixel_size_bytes = (is_u16 ? sizeof(uint16_t) : 
sizeof(uint8_t));
+
+dng_blit(s,
+ dst,
+ 0, // no stride, only 1 line
+ dst,
+ 0, // no stride, only 1 line
+ width / pixel_size_bytes * pixel_size_bits / s->bpp, 
// need to account for [1, 16] bpp
+ 1,
+ is_u16);
+}
+
 src += width;
 break;
 case TIFF_PACKBITS:
@@ -1947,7 +1969,8 @@ again:
 FFSWAP(int,  p->linesize[0], p->linesize[1]);
 }
 
-if (s->is_bayer && s->white_level && s->bpp == 16) {
+if (s->is_bayer && s->white_level && s->bpp == 16 &&
+!(s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == 
TIFF_TYPE_CINEMADNG)) {
 uint16_t *dst = (uint16_t *)p->data[0];
 for (i = 0; i < s->height; i++) {
 for (j = 0; j < s->width; j++)
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v11 03/14] lavc/tiff: Convert DNGs to sRGB color space

2019-08-08 Thread velocityra
From: Nick Renieris 

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 34 +++---
 1 file changed, 23 insertions(+), 11 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index d5673abb19..a118c37c41 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -731,14 +731,23 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 return 0;
 }
 
+static float av_always_inline linear_to_srgb(float value) {
+if (value <= 0.0031308)
+return value * 12.92;
+else
+return pow(value * 1.055, 1.0 / 2.4) - 0.055;
+}
+
 /**
- * Map stored raw sensor values into linear reference values.
- * See: DNG Specification - Chapter 5
+ * Map stored raw sensor values into linear reference values (see: DNG 
Specification - Chapter 5)
+ * Then convert to sRGB color space.
  */
-static uint16_t av_always_inline dng_raw_to_linear16(uint16_t value,
-const uint16_t *lut,
-uint16_t black_level,
-float scale_factor) {
+static uint16_t av_always_inline dng_process_color16(uint16_t value,
+ const uint16_t *lut,
+ uint16_t black_level,
+ float scale_factor) {
+float value_norm;
+
 // Lookup table lookup
 if (lut)
 value = lut[value];
@@ -747,16 +756,19 @@ static uint16_t av_always_inline 
dng_raw_to_linear16(uint16_t value,
 value = av_clip_uint16_c((unsigned)value - black_level);
 
 // Color scaling
-value = av_clip_uint16_c((unsigned)(((float)value * scale_factor) * 
0x));
+value_norm = (float)value * scale_factor;
+
+// Color space conversion (sRGB)
+value = av_clip_uint16_c((uint16_t)(linear_to_srgb(value_norm) * 0x));
 
 return value;
 }
 
-static uint16_t av_always_inline dng_raw_to_linear8(uint16_t value,
+static uint16_t av_always_inline dng_process_color8(uint16_t value,
 const uint16_t *lut,
 uint16_t black_level,
 float scale_factor) {
-return dng_raw_to_linear16(value, lut, black_level, scale_factor) >> 8;
+return dng_process_color16(value, lut, black_level, scale_factor) >> 8;
 }
 
 static void dng_blit(TiffContext *s, uint8_t *dst, int dst_stride,
@@ -774,7 +786,7 @@ static void dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
 uint16_t *src_u16 = (uint16_t *)src;
 
 for (col = 0; col < width; col++)
-*dst_u16++ = dng_raw_to_linear16(*src_u16++, s->dng_lut, 
s->black_level, scale_factor);
+*dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, 
s->black_level, scale_factor);
 
 dst += dst_stride * sizeof(uint16_t);
 src += src_stride * sizeof(uint16_t);
@@ -782,7 +794,7 @@ static void dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
 } else {
 for (line = 0; line < height; line++) {
 for (col = 0; col < width; col++)
-*dst++ = dng_raw_to_linear8(*src++, s->dng_lut, 
s->black_level, scale_factor);
+*dst++ = dng_process_color8(*src++, s->dng_lut, 
s->black_level, scale_factor);
 
 dst += dst_stride;
 src += src_stride;
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v11 02/14] lavc/tiff: Decode embedded JPEGs in DNG images

2019-08-08 Thread velocityra
From: Nick Renieris 

Used a technique similar to lavc/tdsc.c for invoking the MJPEG decoder.

This commit adds support for:
- DNG tiles
- DNG tile huffman lossless JPEG decoding
- DNG 8-bpp ("packed" as dcraw calls it) decoding
- DNG color scaling [1]
  - LinearizationTable tag
  - BlackLevel tag

[1]: As specified in the DNG Specification - Chapter 5

Signed-off-by: Nick Renieris 
---
 configure   |   1 +
 libavcodec/Makefile |   2 +-
 libavcodec/tiff.c   | 315 +++-
 libavcodec/tiff.h   |   2 +
 4 files changed, 312 insertions(+), 8 deletions(-)

diff --git a/configure b/configure
index 34c2adb4a4..112b84f0ba 100755
--- a/configure
+++ b/configure
@@ -2817,6 +2817,7 @@ tdsc_decoder_deps="zlib"
 tdsc_decoder_select="mjpeg_decoder"
 theora_decoder_select="vp3_decoder"
 thp_decoder_select="mjpeg_decoder"
+tiff_decoder_select="mjpeg_decoder"
 tiff_decoder_suggest="zlib lzma"
 tiff_encoder_suggest="zlib"
 truehd_decoder_select="mlp_parser"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3cd73fbcc6..f814c69996 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -616,7 +616,7 @@ OBJS-$(CONFIG_TARGA_ENCODER)   += targaenc.o rle.o
 OBJS-$(CONFIG_TARGA_Y216_DECODER)  += targa_y216dec.o
 OBJS-$(CONFIG_TDSC_DECODER)+= tdsc.o
 OBJS-$(CONFIG_TIERTEXSEQVIDEO_DECODER) += tiertexseqv.o
-OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o
+OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o mjpegdec.o
 OBJS-$(CONFIG_TIFF_ENCODER)+= tiffenc.o rle.o lzwenc.o tiff_data.o
 OBJS-$(CONFIG_TMV_DECODER) += tmv.o cga_data.o
 OBJS-$(CONFIG_TRUEHD_DECODER)  += mlpdec.o mlpdsp.o
diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index c520d7df83..d5673abb19 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -35,6 +35,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/avstring.h"
+#include "libavutil/error.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/opt.h"
@@ -46,6 +47,7 @@
 #include "mathops.h"
 #include "tiff.h"
 #include "tiff_data.h"
+#include "mjpegdec.h"
 #include "thread.h"
 #include "get_bits.h"
 
@@ -54,6 +56,10 @@ typedef struct TiffContext {
 AVCodecContext *avctx;
 GetByteContext gb;
 
+/* JPEG decoding for DNG */
+AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG
+AVFrame *jpgframe;   // decoded JPEG tile
+
 int get_subimage;
 uint16_t get_page;
 int get_thumbnail;
@@ -76,7 +82,9 @@ typedef struct TiffContext {
 
 int is_bayer;
 uint8_t pattern[4];
+unsigned black_level;
 unsigned white_level;
+const uint16_t *dng_lut; // Pointer to DNG linearization table
 
 uint32_t sub_ifd;
 uint16_t cur_page;
@@ -86,6 +94,14 @@ typedef struct TiffContext {
 int stripsizesoff, stripsize, stripoff, strippos;
 LZWState *lzw;
 
+/* Tile support */
+int is_tiled;
+int tile_byte_counts_offset, tile_offsets_offset;
+int tile_width, tile_length;
+int tile_count;
+
+int is_jpeg;
+
 uint8_t *deinvert_buf;
 int deinvert_buf_size;
 uint8_t *yuv_line;
@@ -257,6 +273,9 @@ static int add_metadata(int count, int type,
 };
 }
 
+static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
+  const uint8_t *src, int src_stride, int 
width, int height, int is_u16);
+
 static void av_always_inline horizontal_fill(TiffContext *s,
  unsigned int bpp, uint8_t* dst,
  int usePtr, const uint8_t *src,
@@ -712,6 +731,204 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 return 0;
 }
 
+/**
+ * Map stored raw sensor values into linear reference values.
+ * See: DNG Specification - Chapter 5
+ */
+static uint16_t av_always_inline dng_raw_to_linear16(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+// Lookup table lookup
+if (lut)
+value = lut[value];
+
+// Black level subtraction
+value = av_clip_uint16_c((unsigned)value - black_level);
+
+// Color scaling
+value = av_clip_uint16_c((unsigned)(((float)value * scale_factor) * 
0x));
+
+return value;
+}
+
+static uint16_t av_always_inline dng_raw_to_linear8(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+return dng_raw_to_linear16(value, lut, black_level, scale_factor) >> 8;
+}
+
+static void dng_blit(TiffContext *s,

[FFmpeg-devel] [PATCH v11 01/14] lavc/mjpegdec: Decode Huffman-coded lossless JPEGs embedded in DNGs

2019-08-08 Thread velocityra
From: Nick Renieris 

Main image data in DNGs is usually comprised of tiles, each of which is a 
Huffman-encoded lossless JPEG.

Tested for ljpeg regressions with:
`ffmpeg -f lavfi -i testsrc=d=1 -vcodec ljpeg test.avi`
`ffmpeg test.avi out.avi`
The modified code in ljpeg_decode_rgb_scan runs without issues.

Signed-off-by: Nick Renieris 
---
 libavcodec/mjpegdec.c | 52 +--
 libavcodec/mjpegdec.h |  1 +
 2 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index a65bc8df15..6391107f78 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -412,6 +412,14 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 return AVERROR_PATCHWELCOME;
 }
 
+/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. They contain 
2
+   interleaved components and the width stored in their SOF3 markers is the
+   width of each one.  We only output a single component, therefore we need
+   to adjust the output image width. */
+if (s->lossless == 1 && nb_components == 2) {
+s->bayer = 1;
+width *= 2;
+}
 
 /* if different size, realloc/alloc picture */
 if (width != s->width || height != s->height || bits != s->bits ||
@@ -488,6 +496,9 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 }
 
 switch (pix_fmt_id) {
+case 0x: /* for bayer-encoded huffman lossless JPEGs embedded 
in DNGs */
+s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
+break;
 case 0x1100:
 if (s->rgb)
 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : 
AV_PIX_FMT_BGR48;
@@ -1041,17 +1052,20 @@ static int handle_rstn(MJpegDecodeContext *s, int 
nb_components)
 return reset;
 }
 
+/* Handles 1 to 4 components */
 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int 
predictor, int point_transform)
 {
 int i, mb_x, mb_y;
+unsigned width;
 uint16_t (*buffer)[4];
 int left[4], top[4], topleft[4];
 const int linesize = s->linesize[0];
 const int mask = ((1 << s->bits) - 1) << point_transform;
 int resync_mb_y = 0;
 int resync_mb_x = 0;
+int vpred[6];
 
-if (s->nb_components != 3 && s->nb_components != 4)
+if (s->nb_components <= 0 || s->nb_components > 4)
 return AVERROR_INVALIDDATA;
 if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
 return AVERROR_INVALIDDATA;
@@ -1059,8 +1073,15 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 
 s->restart_count = s->restart_interval;
 
-av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
-   (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
+if (s->restart_interval == 0)
+s->restart_interval = INT_MAX;
+
+if (s->bayer)
+width = s->mb_width / nb_components; /* Interleaved, width stored is 
the total so need to divide */
+else
+width = s->mb_width;
+
+av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, width * 4 * 
sizeof(s->ljpeg_buffer[0][0]));
 if (!s->ljpeg_buffer)
 return AVERROR(ENOMEM);
 
@@ -1078,7 +1099,12 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 for (i = 0; i < 4; i++)
 top[i] = left[i] = topleft[i] = buffer[0][i];
 
-for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
+if ((mb_y * s->width) % s->restart_interval == 0) {
+for (i = 0; i < 6; i++)
+vpred[i] = 1 << (s->bits-1);
+}
+
+for (mb_x = 0; mb_x < width; mb_x++) {
 int modified_predictor = predictor;
 
 if (get_bits_left(&s->gb) < 1) {
@@ -1102,12 +1128,19 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 topleft[i] = top[i];
 top[i] = buffer[mb_x][i];
 
-PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
-
 dc = mjpeg_decode_dc(s, s->dc_index[i]);
 if(dc == 0xF)
 return -1;
 
+if (!s->bayer || mb_x) {
+pred = left[i];
+} else { /* This path runs only for the first line in bayer 
images */
+vpred[i] += dc;
+pred = vpred[i] - dc;
+}
+
+PREDICT(pred, topleft[i], top[i], pred, modified_predictor);
+
 left[i] = buffer[mb_x][i] =
 mask & (pred + (unsigned)(dc * (1 << point_transform)));
 }
@@ -1151,6 +1184,11 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
 }
+} else if (s->bayer && nb_components == 2) {
+for (mb_x = 0; mb

[FFmpeg-devel] [PATCH v10 11/13] lavc/tiff: Decode 14-bit DNG images

2019-08-07 Thread velocityra
From: Nick Renieris 

Sample file: 
https://drive.google.com/open?id=0B4JyRT3Lth5HVndyOTVOdWktM3J4TFEydTk1MnY3RWlpSzVB

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 21 +
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 8a671538aa..a40bcfb3d7 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -309,14 +309,18 @@ static void av_always_inline horizontal_fill(TiffContext 
*s,
 dst[(width+offset)*2+0] = (usePtr ? src[width] : c) >> 4;
 }
 break;
-case 12: {
- uint16_t *dst16 = (uint16_t *)dst;
- GetBitContext gb;
- init_get_bits8(&gb, src, width);
- for (int i = 0; i < s->width; i++) {
- dst16[i] = get_bits(&gb, 12) << 4;
- }
- }
+case 12:
+case 14: {
+uint16_t *dst16 = (uint16_t *)dst;
+int is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == 
TIFF_TYPE_CINEMADNG);
+uint8_t shift = is_dng ? 0 : 16 - bpp;
+GetBitContext gb;
+
+init_get_bits8(&gb, src, width);
+for (int i = 0; i < s->width; i++) {
+dst16[i] = get_bits(&gb, bpp) << shift;
+}
+}
 break;
 default:
 if (usePtr) {
@@ -1058,6 +1062,7 @@ static int init_image(TiffContext *s, ThreadFrame *frame)
 }
 break;
 case 10121:
+case 10141:
 switch (AV_RL32(s->pattern)) {
 case 0x02010100:
 s->avctx->pix_fmt = s->le ? AV_PIX_FMT_BAYER_RGGB16LE : 
AV_PIX_FMT_BAYER_RGGB16BE;
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v10 06/13] lavc/tiff: Fix edge case with full-length/width tiles

2019-08-07 Thread velocityra
From: Nick Renieris 

In an image [1], the height was equal to the tile length (full-height
tile) and after `height % tile_length` was applied to them with the
current code, it resulted in the operating tile_length to be 0.  This
commit makes this leftover logic only applies if it's necessary.

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 8 ++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index b6f626daca..f577639dfa 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -884,10 +884,14 @@ static int dng_decode_tiles(AVCodecContext *avctx, 
AVFrame *frame)
 int tile_byte_count_offset, tile_byte_count;
 int tile_count_x, tile_count_y;
 int tile_width, tile_length;
+int has_width_leftover, has_height_leftover;
 int tile_x = 0, tile_y = 0;
 int pos_x = 0, pos_y = 0;
 int ret;
 
+has_width_leftover = (s->width % s->tile_width != 0);
+has_height_leftover = (s->height % s->tile_length != 0);
+
 /* Calculate tile counts (round up) */
 tile_count_x = (s->width + s->tile_width - 1) / s->tile_width;
 tile_count_y = (s->height + s->tile_length - 1) / s->tile_length;
@@ -897,12 +901,12 @@ static int dng_decode_tiles(AVCodecContext *avctx, 
AVFrame *frame)
 tile_x = tile_idx % tile_count_x;
 tile_y = tile_idx / tile_count_x;
 
-if (tile_x == tile_count_x - 1) // If on the right edge
+if (has_width_leftover && tile_x == tile_count_x - 1) // If on the 
right-most tile
 tile_width = s->width % s->tile_width;
 else
 tile_width = s->tile_width;
 
-if (tile_y == tile_count_y - 1) // If on the bottom edge
+if (has_height_leftover && tile_y == tile_count_y - 1) // If on the 
bottom-most tile
 tile_length = s->height % s->tile_length;
 else
 tile_length = s->tile_length;
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v10 02/13] lavc/tiff: Decode embedded JPEGs in DNG images

2019-08-07 Thread velocityra
From: Nick Renieris 

Used a technique similar to lavc/tdsc.c for invoking the MJPEG decoder.

This commit adds support for:
- DNG tiles
- DNG tile huffman lossless JPEG decoding
- DNG 8-bpp ("packed" as dcraw calls it) decoding
- DNG color scaling [1]
  - LinearizationTable tag
  - BlackLevel tag

[1]: As specified in the DNG Specification - Chapter 5

Signed-off-by: Nick Renieris 
---
 configure   |   1 +
 libavcodec/Makefile |   2 +-
 libavcodec/tiff.c   | 318 +++-
 libavcodec/tiff.h   |   2 +
 4 files changed, 315 insertions(+), 8 deletions(-)

diff --git a/configure b/configure
index 34c2adb4a4..112b84f0ba 100755
--- a/configure
+++ b/configure
@@ -2817,6 +2817,7 @@ tdsc_decoder_deps="zlib"
 tdsc_decoder_select="mjpeg_decoder"
 theora_decoder_select="vp3_decoder"
 thp_decoder_select="mjpeg_decoder"
+tiff_decoder_select="mjpeg_decoder"
 tiff_decoder_suggest="zlib lzma"
 tiff_encoder_suggest="zlib"
 truehd_decoder_select="mlp_parser"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3cd73fbcc6..f814c69996 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -616,7 +616,7 @@ OBJS-$(CONFIG_TARGA_ENCODER)   += targaenc.o rle.o
 OBJS-$(CONFIG_TARGA_Y216_DECODER)  += targa_y216dec.o
 OBJS-$(CONFIG_TDSC_DECODER)+= tdsc.o
 OBJS-$(CONFIG_TIERTEXSEQVIDEO_DECODER) += tiertexseqv.o
-OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o
+OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o mjpegdec.o
 OBJS-$(CONFIG_TIFF_ENCODER)+= tiffenc.o rle.o lzwenc.o tiff_data.o
 OBJS-$(CONFIG_TMV_DECODER) += tmv.o cga_data.o
 OBJS-$(CONFIG_TRUEHD_DECODER)  += mlpdec.o mlpdsp.o
diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index c520d7df83..d1d2e15f45 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -35,6 +35,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/avstring.h"
+#include "libavutil/error.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/opt.h"
@@ -46,6 +47,7 @@
 #include "mathops.h"
 #include "tiff.h"
 #include "tiff_data.h"
+#include "mjpegdec.h"
 #include "thread.h"
 #include "get_bits.h"
 
@@ -54,6 +56,10 @@ typedef struct TiffContext {
 AVCodecContext *avctx;
 GetByteContext gb;
 
+/* JPEG decoding for DNG */
+AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG
+AVFrame *jpgframe;   // decoded JPEG tile
+
 int get_subimage;
 uint16_t get_page;
 int get_thumbnail;
@@ -76,7 +82,9 @@ typedef struct TiffContext {
 
 int is_bayer;
 uint8_t pattern[4];
+unsigned black_level;
 unsigned white_level;
+const uint16_t *dng_lut; // Pointer to DNG linearization table
 
 uint32_t sub_ifd;
 uint16_t cur_page;
@@ -86,6 +94,14 @@ typedef struct TiffContext {
 int stripsizesoff, stripsize, stripoff, strippos;
 LZWState *lzw;
 
+/* Tile support */
+int is_tiled;
+int tile_byte_counts_offset, tile_offsets_offset;
+int tile_width, tile_length;
+int tile_count;
+
+int is_jpeg;
+
 uint8_t *deinvert_buf;
 int deinvert_buf_size;
 uint8_t *yuv_line;
@@ -257,6 +273,9 @@ static int add_metadata(int count, int type,
 };
 }
 
+static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
+  const uint8_t *src, int src_stride, int 
width, int height, int is_u16);
+
 static void av_always_inline horizontal_fill(TiffContext *s,
  unsigned int bpp, uint8_t* dst,
  int usePtr, const uint8_t *src,
@@ -712,6 +731,204 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 return 0;
 }
 
+/**
+ * Map stored raw sensor values into linear reference values.
+ * See: DNG Specification - Chapter 5
+ */
+static uint16_t av_always_inline dng_raw_to_linear16(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+// Lookup table lookup
+if (lut)
+value = lut[value];
+
+// Black level subtraction
+value = av_clip_uint16_c((unsigned)value - black_level);
+
+// Color scaling
+value = av_clip_uint16_c((unsigned)(((float)value * scale_factor) * 
0x));
+
+return value;
+}
+
+static uint16_t av_always_inline dng_raw_to_linear8(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+return dng_raw_to_linear16(value, lut, black_level, scale_factor) >> 8;
+}
+
+static void dng_blit(TiffContext *s,

[FFmpeg-devel] [PATCH v10 08/13] lavc/tiff: Force DNG pixel data endianness on an edge case

2019-08-07 Thread velocityra
From: Nick Renieris 

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 12 
 1 file changed, 12 insertions(+)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 8db42d2bc5..7b9d7574c8 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -1035,6 +1035,18 @@ static int init_image(TiffContext *s, ThreadFrame *frame)
AV_RL32(s->pattern));
 return AVERROR_PATCHWELCOME;
 }
+/* Force endianness as mentioned in 'DNG Specification: Chapter 3: 
BitsPerSample'
+NOTE: The spec actually specifies big-endian, not sure why we need 
little-endian,
+  but such images don't work otherwise. */
+if ((s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == 
TIFF_TYPE_CINEMADNG)
+&& (s->bpp != 8 && s->bpp != 16 && s->bpp != 32)) {
+switch (s->avctx->pix_fmt) {
+case AV_PIX_FMT_BAYER_RGGB16BE: s->avctx->pix_fmt = 
AV_PIX_FMT_BAYER_RGGB16LE; break;
+case AV_PIX_FMT_BAYER_BGGR16BE: s->avctx->pix_fmt = 
AV_PIX_FMT_BAYER_BGGR16LE; break;
+case AV_PIX_FMT_BAYER_GBRG16BE: s->avctx->pix_fmt = 
AV_PIX_FMT_BAYER_GBRG16LE; break;
+case AV_PIX_FMT_BAYER_GRBG16BE: s->avctx->pix_fmt = 
AV_PIX_FMT_BAYER_GRBG16LE; break;
+}
+}
 break;
 case 10161:
 switch (AV_RL32(s->pattern)) {
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v10 12/13] lavc/mjpegdec: Skip useless APPx marker on bayer images

2019-08-07 Thread velocityra
From: Nick Renieris 

Samples:
- Embedded JPEG images in the DNG images here:
  https://www.photographyblog.com/previews/pentax_k1_photos

Signed-off-by: Nick Renieris 
---
 libavcodec/mjpegdec.c | 11 +--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 94cf73549d..b3d6279853 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -1810,8 +1810,15 @@ static int mjpeg_decode_app(MJpegDecodeContext *s)
 int len, id, i;
 
 len = get_bits(&s->gb, 16);
-if (len < 6)
-return AVERROR_INVALIDDATA;
+if (len < 6) {
+if (s->bayer) {
+// Pentax K-1 (digital camera) JPEG images embedded in DNG images 
contain useless APP0 markers
+av_log(s->avctx, AV_LOG_WARNING, "skipping APPx (len=%"PRId32") 
for bayer-encoded image\n", len);
+skip_bits(&s->gb, len);
+return 0;
+} else
+return AVERROR_INVALIDDATA;
+}
 if (8 * len > get_bits_left(&s->gb))
 return AVERROR_INVALIDDATA;
 
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v10 10/13] lavc/tiff: Support decoding of DNGs with single-component JPEGs

2019-08-07 Thread velocityra
From: Nick Renieris 

This enables decoding of DNG and CinemaDNG images generated by 'DJI Zenmuse X7'
Samples: https://www.dji.com/gr/zenmuse-x7/info#downloads

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 61 +++
 1 file changed, 51 insertions(+), 10 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 7b9d7574c8..8a671538aa 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -274,7 +274,8 @@ static int add_metadata(int count, int type,
 }
 
 static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
-  const uint8_t *src, int src_stride, int 
width, int height, int is_u16);
+  const uint8_t *src, int src_stride, int 
width, int height,
+  int is_single_comp, int is_u16);
 
 static void av_always_inline horizontal_fill(TiffContext *s,
  unsigned int bpp, uint8_t* dst,
@@ -695,6 +696,7 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
  0, // no stride, only 1 line
  width / pixel_size_bytes * pixel_size_bits / s->bpp, 
// need to account for [1, 16] bpp
  1,
+ 0, // single-component variation is only preset in 
JPEG-encoded DNGs
  is_u16);
 }
 
@@ -792,18 +794,32 @@ static uint16_t av_always_inline 
dng_process_color8(uint16_t value,
 
 static void dng_blit(TiffContext *s, uint8_t *dst, int dst_stride,
  const uint8_t *src, int src_stride,
- int width, int height, int is_u16)
+ int width, int height, int is_single_comp, int is_u16)
 {
 int line, col;
 float scale_factor;
 
 scale_factor = 1.0f / (s->white_level - s->black_level);
 
-if (is_u16) {
-for (line = 0; line < height; line++) {
+if (is_single_comp) {
+if (!is_u16)
+return; /* <= 8bpp unsupported */
+
+/* Image is double the width and half the height we need, each row 
comprises 2 rows of the output
+   (split vertically in the middle). */
+for (line = 0; line < height / 2; line++) {
 uint16_t *dst_u16 = (uint16_t *)dst;
 uint16_t *src_u16 = (uint16_t *)src;
 
+/* Blit first half of input row row to initial row of output */
+for (col = 0; col < width; col++)
+*dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, 
s->black_level, scale_factor);
+
+/* Advance the destination pointer by a row (source pointer 
remains in the same place) */
+dst += dst_stride * sizeof(uint16_t);
+dst_u16 = (uint16_t *)dst;
+
+/* Blit second half of input row row to next row of output */
 for (col = 0; col < width; col++)
 *dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, 
s->black_level, scale_factor);
 
@@ -811,12 +827,27 @@ static void dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
 src += src_stride * sizeof(uint16_t);
 }
 } else {
-for (line = 0; line < height; line++) {
-for (col = 0; col < width; col++)
-*dst++ = dng_process_color8(*src++, s->dng_lut, 
s->black_level, scale_factor);
+/* Input and output image are the same size and the MJpeg decoder has 
done per-component
+   deinterleaving, so blitting here is straightforward. */
+if (is_u16) {
+for (line = 0; line < height; line++) {
+uint16_t *dst_u16 = (uint16_t *)dst;
+uint16_t *src_u16 = (uint16_t *)src;
+
+for (col = 0; col < width; col++)
+*dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, 
s->black_level, scale_factor);
+
+dst += dst_stride * sizeof(uint16_t);
+src += src_stride * sizeof(uint16_t);
+}
+} else {
+for (line = 0; line < height; line++) {
+for (col = 0; col < width; col++)
+*dst++ = dng_process_color8(*src++, s->dng_lut, 
s->black_level, scale_factor);
 
-dst += dst_stride;
-src += src_stride;
+dst += dst_stride;
+src += src_stride;
+}
 }
 }
 }
@@ -828,7 +859,7 @@ static int dng_decode_jpeg_tile(AVCodecContext *avctx, 
AVFrame *frame,
 AVPacket jpkt;
 uint8_t *dst_data, *src_data;
 uint32_t dst_offset; /* offset from dst buffer in pixels */
-int is_u16, pixel_size;
+int is_single_comp, is_u16, pixel_size;
 int ret;
 
 /* Prepare a packet and send to the MJPEG decoder */
@@ -855,9 +886,18 @@ static int dng_decode_jpeg_tile(AVCodecContext *avctx, 
AVFrame *frame,
 
 /* Copy the outputted tile's pixels from 'jpgframe' to

[FFmpeg-devel] [PATCH v10 09/13] lavc/mjpegdec: Enable decoding of single-component bayer images

2019-08-07 Thread velocityra
From: Nick Renieris 

Signed-off-by: Nick Renieris 
---
 libavcodec/mjpegdec.c | 35 ---
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 6391107f78..94cf73549d 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -412,13 +412,21 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 return AVERROR_PATCHWELCOME;
 }
 
-/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. They contain 
2
-   interleaved components and the width stored in their SOF3 markers is the
-   width of each one.  We only output a single component, therefore we need
-   to adjust the output image width. */
-if (s->lossless == 1 && nb_components == 2) {
-s->bayer = 1;
-width *= 2;
+/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. */
+if (s->lossless) {
+if (nb_components == 1) {
+/* They can contain 1 component, which is double the width and 
half the height
+   of the final image (rows are interleaved).  We don't handle the 
decoding in
+   this file, but leave that to the DNG decoder. */
+s->bayer = 1;
+} else if (nb_components == 2) {
+/* Or they can contain 2 interleaved components and the width 
stored in their
+   SOF3 markers is the width of each one.  We only output a single 
component,
+   therefore we need to adjust the output image width.  We handle 
the
+   deinterleaving (but not the debayering) in this file. */
+s->bayer = 1;
+width *= 2;
+}
 }
 
 /* if different size, realloc/alloc picture */
@@ -1184,10 +1192,15 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
 }
-} else if (s->bayer && nb_components == 2) {
-for (mb_x = 0; mb_x < width; mb_x++) {
-((uint16_t*)ptr)[2*mb_x + 0] = buffer[mb_x][0];
-((uint16_t*)ptr)[2*mb_x + 1] = buffer[mb_x][1];
+} else if (s->bayer) {
+if (nb_components == 1) {
+for (mb_x = 0; mb_x < width; mb_x++)
+((uint16_t*)ptr)[mb_x] = buffer[mb_x][0];
+} else if (nb_components == 2) {
+for (mb_x = 0; mb_x < width; mb_x++) {
+((uint16_t*)ptr)[2*mb_x + 0] = buffer[mb_x][0];
+((uint16_t*)ptr)[2*mb_x + 1] = buffer[mb_x][1];
+}
 }
 } else {
 for(i=0; ihttps://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v10 04/13] lavc/tiff: Apply color scaling to uncompressed DNGs

2019-08-07 Thread velocityra
From: Nick Renieris 

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 22 +-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index c7e2adb3ae..b6f626daca 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -679,6 +679,25 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 for (i = 0; i < width; i++)
 dst[i] = ff_reverse[src[i]];
 }
+
+/* Color processing for DNG images with uncompressed strips 
(non-tiled) */
+if (is_dng) {
+int is_u16, pixel_size_bytes, pixel_size_bits;
+
+is_u16 = (s->bpp > 8);
+pixel_size_bits = (is_u16 ? 16 : 8);
+pixel_size_bytes = (is_u16 ? sizeof(uint16_t) : 
sizeof(uint8_t));
+
+dng_blit(s,
+ dst,
+ 0, // no stride, only 1 line
+ dst,
+ 0, // no stride, only 1 line
+ width / pixel_size_bytes * pixel_size_bits / s->bpp, 
// need to account for [1, 16] bpp
+ 1,
+ is_u16);
+}
+
 src += width;
 break;
 case TIFF_PACKBITS:
@@ -1950,7 +1969,8 @@ again:
 FFSWAP(int,  p->linesize[0], p->linesize[1]);
 }
 
-if (s->is_bayer && s->white_level && s->bpp == 16) {
+if (s->is_bayer && s->white_level && s->bpp == 16 &&
+!(s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == 
TIFF_TYPE_CINEMADNG)) {
 uint16_t *dst = (uint16_t *)p->data[0];
 for (i = 0; i < s->height; i++) {
 for (j = 0; j < s->width; j++)
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v10 03/13] lavc/tiff: Convert DNGs to sRGB color space

2019-08-07 Thread velocityra
From: Nick Renieris 

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 34 +++---
 1 file changed, 23 insertions(+), 11 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index d1d2e15f45..c7e2adb3ae 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -731,14 +731,23 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 return 0;
 }
 
+static float av_always_inline linear_to_srgb(float value) {
+if (value <= 0.0031308)
+return value * 12.92;
+else
+return pow(value * 1.055, 1.0 / 2.4) - 0.055;
+}
+
 /**
- * Map stored raw sensor values into linear reference values.
- * See: DNG Specification - Chapter 5
+ * Map stored raw sensor values into linear reference values (see: DNG 
Specification - Chapter 5)
+ * Then convert to sRGB color space.
  */
-static uint16_t av_always_inline dng_raw_to_linear16(uint16_t value,
-const uint16_t *lut,
-uint16_t black_level,
-float scale_factor) {
+static uint16_t av_always_inline dng_process_color16(uint16_t value,
+ const uint16_t *lut,
+ uint16_t black_level,
+ float scale_factor) {
+float value_norm;
+
 // Lookup table lookup
 if (lut)
 value = lut[value];
@@ -747,16 +756,19 @@ static uint16_t av_always_inline 
dng_raw_to_linear16(uint16_t value,
 value = av_clip_uint16_c((unsigned)value - black_level);
 
 // Color scaling
-value = av_clip_uint16_c((unsigned)(((float)value * scale_factor) * 
0x));
+value_norm = (float)value * scale_factor;
+
+// Color space conversion (sRGB)
+value = av_clip_uint16_c((uint16_t)(linear_to_srgb(value_norm) * 0x));
 
 return value;
 }
 
-static uint16_t av_always_inline dng_raw_to_linear8(uint16_t value,
+static uint16_t av_always_inline dng_process_color8(uint16_t value,
 const uint16_t *lut,
 uint16_t black_level,
 float scale_factor) {
-return dng_raw_to_linear16(value, lut, black_level, scale_factor) >> 8;
+return dng_process_color16(value, lut, black_level, scale_factor) >> 8;
 }
 
 static void dng_blit(TiffContext *s, uint8_t *dst, int dst_stride,
@@ -774,7 +786,7 @@ static void dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
 uint16_t *src_u16 = (uint16_t *)src;
 
 for (col = 0; col < width; col++)
-*dst_u16++ = dng_raw_to_linear16(*src_u16++, s->dng_lut, 
s->black_level, scale_factor);
+*dst_u16++ = dng_process_color16(*src_u16++, s->dng_lut, 
s->black_level, scale_factor);
 
 dst += dst_stride * sizeof(uint16_t);
 src += src_stride * sizeof(uint16_t);
@@ -782,7 +794,7 @@ static void dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
 } else {
 for (line = 0; line < height; line++) {
 for (col = 0; col < width; col++)
-*dst++ = dng_raw_to_linear8(*src++, s->dng_lut, 
s->black_level, scale_factor);
+*dst++ = dng_process_color8(*src++, s->dng_lut, 
s->black_level, scale_factor);
 
 dst += dst_stride;
 src += src_stride;
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v10 07/13] lavc/tiff: Don't apply strips-related logic to tiled images

2019-08-07 Thread velocityra
From: Nick Renieris 

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 42 ++
 1 file changed, 22 insertions(+), 20 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index f577639dfa..8db42d2bc5 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -1780,7 +1780,7 @@ again:
 }
 }
 
-if (!s->strippos && !s->stripoff) {
+if (!s->is_tiled && !s->strippos && !s->stripoff) {
 av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");
 return AVERROR_INVALIDDATA;
 }
@@ -1788,27 +1788,29 @@ again:
 if ((ret = init_image(s, &frame)) < 0)
 return ret;
 
-if (s->strips == 1 && !s->stripsize) {
-av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
-s->stripsize = avpkt->size - s->stripoff;
-}
+if (!s->is_tiled) {
+if (s->strips == 1 && !s->stripsize) {
+av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");
+s->stripsize = avpkt->size - s->stripoff;
+}
 
-if (s->stripsizesoff) {
-if (s->stripsizesoff >= (unsigned)avpkt->size)
-return AVERROR_INVALIDDATA;
-bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
- avpkt->size - s->stripsizesoff);
-}
-if (s->strippos) {
-if (s->strippos >= (unsigned)avpkt->size)
-return AVERROR_INVALIDDATA;
-bytestream2_init(&stripdata, avpkt->data + s->strippos,
- avpkt->size - s->strippos);
-}
+if (s->stripsizesoff) {
+if (s->stripsizesoff >= (unsigned)avpkt->size)
+return AVERROR_INVALIDDATA;
+bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,
+avpkt->size - s->stripsizesoff);
+}
+if (s->strippos) {
+if (s->strippos >= (unsigned)avpkt->size)
+return AVERROR_INVALIDDATA;
+bytestream2_init(&stripdata, avpkt->data + s->strippos,
+avpkt->size - s->strippos);
+}
 
-if (s->rps <= 0 || s->rps % s->subsampling[1]) {
-av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
-return AVERROR_INVALIDDATA;
+if (s->rps <= 0 || s->rps % s->subsampling[1]) {
+av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);
+return AVERROR_INVALIDDATA;
+}
 }
 
 /* Handle DNG images with JPEG-compressed tiles */
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v10 05/13] lavc/jpegtables: Handle multiple mappings to the same value

2019-08-07 Thread velocityra
From: Nick Renieris 

Some JPEGs [1] have incorrect DHT entries that map 2 codes to
the same value.

The second (last) mapping does not ever actually appear in the
code stream, therefore ignoring any mappings after the first one
fixes this.

Without this, an "mjpeg_decode_dc: bad vlc: 0:0" error is thrown.

---

[1]: Embedded JPEGs in "X7 RAW" and "X7 CinemaDNG" samples here:
 https://www.dji.com/gr/zenmuse-x7/info#downloads

Signed-off-by: Nick Renieris 
---
 libavcodec/jpegtables.c | 19 ---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/libavcodec/jpegtables.c b/libavcodec/jpegtables.c
index cbe5523cb4..6f596cfc92 100644
--- a/libavcodec/jpegtables.c
+++ b/libavcodec/jpegtables.c
@@ -130,14 +130,27 @@ void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, 
uint16_t *huff_code,
 {
 int i, j, k,nb, code, sym;
 
-code = 0;
+/* Zero-initialize huff_size (needed for multiple mappings check below) */
+k = 0;
+for(i=1;i<=16;i++) {
+nb = bits_table[i];
+for(j=0;jhttps://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v10 01/13] lavc/mjpegdec: Decode Huffman-coded lossless JPEGs embedded in DNGs

2019-08-07 Thread velocityra
From: Nick Renieris 

Main image data in DNGs is usually comprised of tiles, each of which is a 
Huffman-encoded lossless JPEG.

Tested for ljpeg regressions with:
`ffmpeg -f lavfi -i testsrc=d=1 -vcodec ljpeg test.avi`
`ffmpeg test.avi out.avi`
The modified code in ljpeg_decode_rgb_scan runs without issues.

Signed-off-by: Nick Renieris 
---
 libavcodec/mjpegdec.c | 52 +--
 libavcodec/mjpegdec.h |  1 +
 2 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index a65bc8df15..6391107f78 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -412,6 +412,14 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 return AVERROR_PATCHWELCOME;
 }
 
+/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. They contain 
2
+   interleaved components and the width stored in their SOF3 markers is the
+   width of each one.  We only output a single component, therefore we need
+   to adjust the output image width. */
+if (s->lossless == 1 && nb_components == 2) {
+s->bayer = 1;
+width *= 2;
+}
 
 /* if different size, realloc/alloc picture */
 if (width != s->width || height != s->height || bits != s->bits ||
@@ -488,6 +496,9 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 }
 
 switch (pix_fmt_id) {
+case 0x: /* for bayer-encoded huffman lossless JPEGs embedded 
in DNGs */
+s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
+break;
 case 0x1100:
 if (s->rgb)
 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : 
AV_PIX_FMT_BGR48;
@@ -1041,17 +1052,20 @@ static int handle_rstn(MJpegDecodeContext *s, int 
nb_components)
 return reset;
 }
 
+/* Handles 1 to 4 components */
 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int 
predictor, int point_transform)
 {
 int i, mb_x, mb_y;
+unsigned width;
 uint16_t (*buffer)[4];
 int left[4], top[4], topleft[4];
 const int linesize = s->linesize[0];
 const int mask = ((1 << s->bits) - 1) << point_transform;
 int resync_mb_y = 0;
 int resync_mb_x = 0;
+int vpred[6];
 
-if (s->nb_components != 3 && s->nb_components != 4)
+if (s->nb_components <= 0 || s->nb_components > 4)
 return AVERROR_INVALIDDATA;
 if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
 return AVERROR_INVALIDDATA;
@@ -1059,8 +1073,15 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 
 s->restart_count = s->restart_interval;
 
-av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
-   (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
+if (s->restart_interval == 0)
+s->restart_interval = INT_MAX;
+
+if (s->bayer)
+width = s->mb_width / nb_components; /* Interleaved, width stored is 
the total so need to divide */
+else
+width = s->mb_width;
+
+av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, width * 4 * 
sizeof(s->ljpeg_buffer[0][0]));
 if (!s->ljpeg_buffer)
 return AVERROR(ENOMEM);
 
@@ -1078,7 +1099,12 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 for (i = 0; i < 4; i++)
 top[i] = left[i] = topleft[i] = buffer[0][i];
 
-for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
+if ((mb_y * s->width) % s->restart_interval == 0) {
+for (i = 0; i < 6; i++)
+vpred[i] = 1 << (s->bits-1);
+}
+
+for (mb_x = 0; mb_x < width; mb_x++) {
 int modified_predictor = predictor;
 
 if (get_bits_left(&s->gb) < 1) {
@@ -1102,12 +1128,19 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 topleft[i] = top[i];
 top[i] = buffer[mb_x][i];
 
-PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
-
 dc = mjpeg_decode_dc(s, s->dc_index[i]);
 if(dc == 0xF)
 return -1;
 
+if (!s->bayer || mb_x) {
+pred = left[i];
+} else { /* This path runs only for the first line in bayer 
images */
+vpred[i] += dc;
+pred = vpred[i] - dc;
+}
+
+PREDICT(pred, topleft[i], top[i], pred, modified_predictor);
+
 left[i] = buffer[mb_x][i] =
 mask & (pred + (unsigned)(dc * (1 << point_transform)));
 }
@@ -1151,6 +1184,11 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
 }
+} else if (s->bayer && nb_components == 2) {
+for (mb_x = 0; mb

[FFmpeg-devel] [PATCH v10 13/13] lavc/tiff: Support DNGs with striped (non-tiled) JPEGs images

2019-08-07 Thread velocityra
From: Nick Renieris 

DNG samples here can now be decoded:
- https://www.photographyblog.com/previews/pentax_k1_photos

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 79 +++
 1 file changed, 45 insertions(+), 34 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index a40bcfb3d7..47140cddbc 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -550,6 +550,8 @@ static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, 
int stride,
 return ret;
 }
 
+static int dng_decode_strip(AVCodecContext *avctx, AVFrame *frame);
+
 static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int 
stride,
  const uint8_t *src, int size, int strip_start, 
int lines)
 {
@@ -561,6 +563,7 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 int is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
  (desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
  desc->nb_components >= 3;
+int is_dng;
 
 if (s->planar)
 width /= s->bppcount;
@@ -662,6 +665,19 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 bytestream2_init(&s->gb, src, size);
 bytestream2_init_writer(&pb, dst, is_yuv ? s->yuv_line_size : (stride * 
lines));
 
+is_dng = (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == 
TIFF_TYPE_CINEMADNG);
+
+/* Decode JPEG-encoded DNGs with strips */
+if (s->compr == TIFF_NEWJPEG && is_dng) {
+if (s->strips > 1) {
+av_log(s->avctx, AV_LOG_ERROR, "More than one DNG JPEG strips 
unsupported\n");
+return AVERROR_PATCHWELCOME;
+}
+if ((ret = dng_decode_strip(s->avctx, p)) < 0)
+return ret;
+return 0;
+}
+
 for (line = 0; line < lines; line++) {
 if (src - ssrc > size) {
 av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");
@@ -856,8 +872,8 @@ static void dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
 }
 }
 
-static int dng_decode_jpeg_tile(AVCodecContext *avctx, AVFrame *frame,
-int tile_byte_count, int x, int y, int w, int 
h)
+static int dng_decode_jpeg(AVCodecContext *avctx, AVFrame *frame,
+   int tile_byte_count, int dst_x, int dst_y, int w, 
int h)
 {
 TiffContext *s = avctx->priv_data;
 AVPacket jpkt;
@@ -902,7 +918,7 @@ static int dng_decode_jpeg_tile(AVCodecContext *avctx, 
AVFrame *frame,
 return AVERROR_PATCHWELCOME;
 }
 
-dst_offset = x + frame->linesize[0] * y / pixel_size;
+dst_offset = dst_x + frame->linesize[0] * dst_y / pixel_size;
 dst_data = frame->data[0] + dst_offset * pixel_size;
 src_data = s->jpgframe->data[0];
 
@@ -921,7 +937,7 @@ static int dng_decode_jpeg_tile(AVCodecContext *avctx, 
AVFrame *frame,
 return 0;
 }
 
-static int dng_decode_tiles(AVCodecContext *avctx, AVFrame *frame)
+static int dng_decode_tiles(AVCodecContext *avctx, AVFrame *frame, AVPacket 
*avpkt)
 {
 TiffContext *s = avctx->priv_data;
 int tile_idx;
@@ -934,6 +950,12 @@ static int dng_decode_tiles(AVCodecContext *avctx, AVFrame 
*frame)
 int pos_x = 0, pos_y = 0;
 int ret;
 
+s->jpgframe->width  = s->tile_width;
+s->jpgframe->height = s->tile_length;
+
+s->avctx_mjpeg->width = s->tile_width;
+s->avctx_mjpeg->height = s->tile_length;
+
 has_width_leftover = (s->width % s->tile_width != 0);
 has_height_leftover = (s->height % s->tile_length != 0);
 
@@ -970,7 +992,7 @@ static int dng_decode_tiles(AVCodecContext *avctx, AVFrame 
*frame)
 bytestream2_seek(&s->gb, tile_offset, SEEK_SET);
 
 /* Decode JPEG tile and copy it in the reference frame */
-ret = dng_decode_jpeg_tile(avctx, frame, tile_byte_count, pos_x, 
pos_y, tile_width, tile_length);
+ret = dng_decode_jpeg(avctx, frame, tile_byte_count, pos_x, pos_y, 
tile_width, tile_length);
 
 if (ret < 0)
 return ret;
@@ -983,30 +1005,24 @@ static int dng_decode_tiles(AVCodecContext *avctx, 
AVFrame *frame)
 }
 }
 
-return 0;
-}
+/* Frame is ready to be output */
+frame->pict_type = AV_PICTURE_TYPE_I;
+frame->key_frame = 1;
 
-static int dng_decode(AVCodecContext *avctx, AVFrame *frame, AVPacket *avpkt) {
-int ret;
+return avpkt->size;
+}
 
+static int dng_decode_strip(AVCodecContext *avctx, AVFrame *frame)
+{
 TiffContext *s = avctx->priv_data;
 
-s->jpgframe->width  = s->tile_width;
-s->jpgframe->height = s->tile_length;
-
-s->avctx_mjpeg->width = s->tile_width;
-s->avctx_mjpeg->height = s->tile_length;
+s->jpgframe->width  = s->width;
+s->jpgframe->height = s->height;
 
-/* Decode all tiles in a frame */
-ret = dng_decode_tiles(avctx, frame);
-if (ret < 0)
-return ret;
+s->avctx_mjpeg->width = s->width;
+s->avctx_mjpeg->height = s->height;
 
-/* Frame is ready to be o

[FFmpeg-devel] [PATCH v9 2/2] lavc/tiff: Decode embedded JPEGs in DNG images

2019-07-26 Thread velocityra
From: Nick Renieris 

Used a technique similar to lavc/tdsc.c for invoking the MJPEG decoder.

This commit adds support for:
- DNG tiles
- DNG tile huffman lossless JPEG decoding
- DNG 8-bpp ("packed" as dcraw calls it) decoding
- DNG color scaling [1]
  - LinearizationTable tag
  - BlackLevel tag

[1]: As specified in the DNG Specification - Chapter 5

Signed-off-by: Nick Renieris 
---
 configure   |   1 +
 libavcodec/Makefile |   2 +-
 libavcodec/tiff.c   | 315 +++-
 libavcodec/tiff.h   |   2 +
 4 files changed, 312 insertions(+), 8 deletions(-)

diff --git a/configure b/configure
index 5a4f507246..6726883d5b 100755
--- a/configure
+++ b/configure
@@ -2811,6 +2811,7 @@ tdsc_decoder_deps="zlib"
 tdsc_decoder_select="mjpeg_decoder"
 theora_decoder_select="vp3_decoder"
 thp_decoder_select="mjpeg_decoder"
+tiff_decoder_select="mjpeg_decoder"
 tiff_decoder_suggest="zlib lzma"
 tiff_encoder_suggest="zlib"
 truehd_decoder_select="mlp_parser"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3cd73fbcc6..f814c69996 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -616,7 +616,7 @@ OBJS-$(CONFIG_TARGA_ENCODER)   += targaenc.o rle.o
 OBJS-$(CONFIG_TARGA_Y216_DECODER)  += targa_y216dec.o
 OBJS-$(CONFIG_TDSC_DECODER)+= tdsc.o
 OBJS-$(CONFIG_TIERTEXSEQVIDEO_DECODER) += tiertexseqv.o
-OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o
+OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o mjpegdec.o
 OBJS-$(CONFIG_TIFF_ENCODER)+= tiffenc.o rle.o lzwenc.o tiff_data.o
 OBJS-$(CONFIG_TMV_DECODER) += tmv.o cga_data.o
 OBJS-$(CONFIG_TRUEHD_DECODER)  += mlpdec.o mlpdsp.o
diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index c520d7df83..db68690736 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -35,6 +35,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/avstring.h"
+#include "libavutil/error.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/opt.h"
@@ -46,6 +47,7 @@
 #include "mathops.h"
 #include "tiff.h"
 #include "tiff_data.h"
+#include "mjpegdec.h"
 #include "thread.h"
 #include "get_bits.h"
 
@@ -54,6 +56,10 @@ typedef struct TiffContext {
 AVCodecContext *avctx;
 GetByteContext gb;
 
+/* JPEG decoding for DNG */
+AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG
+AVFrame *jpgframe;   // decoded JPEG tile
+
 int get_subimage;
 uint16_t get_page;
 int get_thumbnail;
@@ -76,7 +82,9 @@ typedef struct TiffContext {
 
 int is_bayer;
 uint8_t pattern[4];
+unsigned black_level;
 unsigned white_level;
+const uint16_t *dng_lut; // Pointer to DNG linearization table
 
 uint32_t sub_ifd;
 uint16_t cur_page;
@@ -86,6 +94,14 @@ typedef struct TiffContext {
 int stripsizesoff, stripsize, stripoff, strippos;
 LZWState *lzw;
 
+/* Tile support */
+int is_tiled;
+int tile_byte_counts_offset, tile_offsets_offset;
+int tile_width, tile_length;
+int tile_count;
+
+int is_jpeg;
+
 uint8_t *deinvert_buf;
 int deinvert_buf_size;
 uint8_t *yuv_line;
@@ -257,6 +273,9 @@ static int add_metadata(int count, int type,
 };
 }
 
+static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
+  const uint8_t *src, int src_stride, int 
width, int height, int is_u16);
+
 static void av_always_inline horizontal_fill(TiffContext *s,
  unsigned int bpp, uint8_t* dst,
  int usePtr, const uint8_t *src,
@@ -712,6 +731,204 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 return 0;
 }
 
+/**
+ * Map stored raw sensor values into linear reference values.
+ * See: DNG Specification - Chapter 5
+ */
+static uint16_t av_always_inline dng_raw_to_linear16(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+// Lookup table lookup
+if (lut)
+value = lut[value];
+
+// Black level subtraction
+value = av_clip_uint16_c((unsigned)value - black_level);
+
+// Color scaling
+value = av_clip_uint16_c((unsigned)(((float)value * scale_factor) * 
0x));
+
+return value;
+}
+
+static uint16_t av_always_inline dng_raw_to_linear8(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+return dng_raw_to_linear16(value, lut, black_level, scale_factor) >> 8;
+}
+
+static void dng_blit(TiffContext *s,

[FFmpeg-devel] [PATCH v9 1/2] lavc/mjpegdec: Decode Huffman-coded lossless JPEGs embedded in DNGs

2019-07-26 Thread velocityra
From: Nick Renieris 

Main image data in DNGs is usually comprised of tiles, each of which is a 
Huffman-encoded lossless JPEG.

Tested for ljpeg regressions with:
`ffmpeg -f lavfi -i testsrc=d=1 -vcodec ljpeg test.avi`
`ffmpeg test.avi out.avi`
The modified code in ljpeg_decode_rgb_scan runs without issues.

Signed-off-by: Nick Renieris 
---
 libavcodec/mjpegdec.c | 52 +--
 libavcodec/mjpegdec.h |  1 +
 2 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index a65bc8df15..6391107f78 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -412,6 +412,14 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 return AVERROR_PATCHWELCOME;
 }
 
+/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. They contain 
2
+   interleaved components and the width stored in their SOF3 markers is the
+   width of each one.  We only output a single component, therefore we need
+   to adjust the output image width. */
+if (s->lossless == 1 && nb_components == 2) {
+s->bayer = 1;
+width *= 2;
+}
 
 /* if different size, realloc/alloc picture */
 if (width != s->width || height != s->height || bits != s->bits ||
@@ -488,6 +496,9 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 }
 
 switch (pix_fmt_id) {
+case 0x: /* for bayer-encoded huffman lossless JPEGs embedded 
in DNGs */
+s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
+break;
 case 0x1100:
 if (s->rgb)
 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : 
AV_PIX_FMT_BGR48;
@@ -1041,17 +1052,20 @@ static int handle_rstn(MJpegDecodeContext *s, int 
nb_components)
 return reset;
 }
 
+/* Handles 1 to 4 components */
 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int 
predictor, int point_transform)
 {
 int i, mb_x, mb_y;
+unsigned width;
 uint16_t (*buffer)[4];
 int left[4], top[4], topleft[4];
 const int linesize = s->linesize[0];
 const int mask = ((1 << s->bits) - 1) << point_transform;
 int resync_mb_y = 0;
 int resync_mb_x = 0;
+int vpred[6];
 
-if (s->nb_components != 3 && s->nb_components != 4)
+if (s->nb_components <= 0 || s->nb_components > 4)
 return AVERROR_INVALIDDATA;
 if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
 return AVERROR_INVALIDDATA;
@@ -1059,8 +1073,15 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 
 s->restart_count = s->restart_interval;
 
-av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
-   (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
+if (s->restart_interval == 0)
+s->restart_interval = INT_MAX;
+
+if (s->bayer)
+width = s->mb_width / nb_components; /* Interleaved, width stored is 
the total so need to divide */
+else
+width = s->mb_width;
+
+av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, width * 4 * 
sizeof(s->ljpeg_buffer[0][0]));
 if (!s->ljpeg_buffer)
 return AVERROR(ENOMEM);
 
@@ -1078,7 +1099,12 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 for (i = 0; i < 4; i++)
 top[i] = left[i] = topleft[i] = buffer[0][i];
 
-for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
+if ((mb_y * s->width) % s->restart_interval == 0) {
+for (i = 0; i < 6; i++)
+vpred[i] = 1 << (s->bits-1);
+}
+
+for (mb_x = 0; mb_x < width; mb_x++) {
 int modified_predictor = predictor;
 
 if (get_bits_left(&s->gb) < 1) {
@@ -1102,12 +1128,19 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 topleft[i] = top[i];
 top[i] = buffer[mb_x][i];
 
-PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
-
 dc = mjpeg_decode_dc(s, s->dc_index[i]);
 if(dc == 0xF)
 return -1;
 
+if (!s->bayer || mb_x) {
+pred = left[i];
+} else { /* This path runs only for the first line in bayer 
images */
+vpred[i] += dc;
+pred = vpred[i] - dc;
+}
+
+PREDICT(pred, topleft[i], top[i], pred, modified_predictor);
+
 left[i] = buffer[mb_x][i] =
 mask & (pred + (unsigned)(dc * (1 << point_transform)));
 }
@@ -1151,6 +1184,11 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
 }
+} else if (s->bayer && nb_components == 2) {
+for (mb_x = 0; mb

[FFmpeg-devel] [PATCH v8 1/2] lavc/mjpegdec: Decode Huffman-coded lossless JPEGs embedded in DNGs

2019-07-26 Thread velocityra
From: Nick Renieris 

Main image data in DNGs is usually comprised of tiles, each of which is a 
Huffman-encoded lossless JPEG.

Tested for ljpeg regressions with:
`ffmpeg -f lavfi -i testsrc=d=1 -vcodec ljpeg test.avi`
`ffmpeg test.avi out.avi`
The modified code in ljpeg_decode_rgb_scan runs without issues.

Signed-off-by: Nick Renieris 
---
 libavcodec/mjpegdec.c | 52 +--
 libavcodec/mjpegdec.h |  1 +
 2 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index a65bc8df15..6391107f78 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -412,6 +412,14 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 return AVERROR_PATCHWELCOME;
 }
 
+/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. They contain 
2
+   interleaved components and the width stored in their SOF3 markers is the
+   width of each one.  We only output a single component, therefore we need
+   to adjust the output image width. */
+if (s->lossless == 1 && nb_components == 2) {
+s->bayer = 1;
+width *= 2;
+}
 
 /* if different size, realloc/alloc picture */
 if (width != s->width || height != s->height || bits != s->bits ||
@@ -488,6 +496,9 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 }
 
 switch (pix_fmt_id) {
+case 0x: /* for bayer-encoded huffman lossless JPEGs embedded 
in DNGs */
+s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
+break;
 case 0x1100:
 if (s->rgb)
 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : 
AV_PIX_FMT_BGR48;
@@ -1041,17 +1052,20 @@ static int handle_rstn(MJpegDecodeContext *s, int 
nb_components)
 return reset;
 }
 
+/* Handles 1 to 4 components */
 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int 
predictor, int point_transform)
 {
 int i, mb_x, mb_y;
+unsigned width;
 uint16_t (*buffer)[4];
 int left[4], top[4], topleft[4];
 const int linesize = s->linesize[0];
 const int mask = ((1 << s->bits) - 1) << point_transform;
 int resync_mb_y = 0;
 int resync_mb_x = 0;
+int vpred[6];
 
-if (s->nb_components != 3 && s->nb_components != 4)
+if (s->nb_components <= 0 || s->nb_components > 4)
 return AVERROR_INVALIDDATA;
 if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
 return AVERROR_INVALIDDATA;
@@ -1059,8 +1073,15 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 
 s->restart_count = s->restart_interval;
 
-av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
-   (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
+if (s->restart_interval == 0)
+s->restart_interval = INT_MAX;
+
+if (s->bayer)
+width = s->mb_width / nb_components; /* Interleaved, width stored is 
the total so need to divide */
+else
+width = s->mb_width;
+
+av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, width * 4 * 
sizeof(s->ljpeg_buffer[0][0]));
 if (!s->ljpeg_buffer)
 return AVERROR(ENOMEM);
 
@@ -1078,7 +1099,12 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 for (i = 0; i < 4; i++)
 top[i] = left[i] = topleft[i] = buffer[0][i];
 
-for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
+if ((mb_y * s->width) % s->restart_interval == 0) {
+for (i = 0; i < 6; i++)
+vpred[i] = 1 << (s->bits-1);
+}
+
+for (mb_x = 0; mb_x < width; mb_x++) {
 int modified_predictor = predictor;
 
 if (get_bits_left(&s->gb) < 1) {
@@ -1102,12 +1128,19 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 topleft[i] = top[i];
 top[i] = buffer[mb_x][i];
 
-PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
-
 dc = mjpeg_decode_dc(s, s->dc_index[i]);
 if(dc == 0xF)
 return -1;
 
+if (!s->bayer || mb_x) {
+pred = left[i];
+} else { /* This path runs only for the first line in bayer 
images */
+vpred[i] += dc;
+pred = vpred[i] - dc;
+}
+
+PREDICT(pred, topleft[i], top[i], pred, modified_predictor);
+
 left[i] = buffer[mb_x][i] =
 mask & (pred + (unsigned)(dc * (1 << point_transform)));
 }
@@ -1151,6 +1184,11 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
 }
+} else if (s->bayer && nb_components == 2) {
+for (mb_x = 0; mb

[FFmpeg-devel] [PATCH v8 2/2] lavc/tiff: Decode embedded JPEGs in DNG images

2019-07-26 Thread velocityra
From: Nick Renieris 

Used a technique similar to lavc/tdsc.c for invoking the MJPEG decoder.

This commit adds support for:
- DNG tiles
- DNG tile huffman lossless JPEG decoding
- DNG 8-bpp ("packed" as dcraw calls it) decoding
- DNG color scaling [1]
  - LinearizationTable tag
  - BlackLevel tag

[1]: As specified in the DNG Specification - Chapter 5

Signed-off-by: Nick Renieris 
---
 configure   |   1 +
 libavcodec/Makefile |   2 +-
 libavcodec/tiff.c   | 315 +++-
 libavcodec/tiff.h   |   2 +
 4 files changed, 312 insertions(+), 8 deletions(-)

diff --git a/configure b/configure
index 5a4f507246..6726883d5b 100755
--- a/configure
+++ b/configure
@@ -2811,6 +2811,7 @@ tdsc_decoder_deps="zlib"
 tdsc_decoder_select="mjpeg_decoder"
 theora_decoder_select="vp3_decoder"
 thp_decoder_select="mjpeg_decoder"
+tiff_decoder_select="mjpeg_decoder"
 tiff_decoder_suggest="zlib lzma"
 tiff_encoder_suggest="zlib"
 truehd_decoder_select="mlp_parser"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3cd73fbcc6..f814c69996 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -616,7 +616,7 @@ OBJS-$(CONFIG_TARGA_ENCODER)   += targaenc.o rle.o
 OBJS-$(CONFIG_TARGA_Y216_DECODER)  += targa_y216dec.o
 OBJS-$(CONFIG_TDSC_DECODER)+= tdsc.o
 OBJS-$(CONFIG_TIERTEXSEQVIDEO_DECODER) += tiertexseqv.o
-OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o
+OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o mjpegdec.o
 OBJS-$(CONFIG_TIFF_ENCODER)+= tiffenc.o rle.o lzwenc.o tiff_data.o
 OBJS-$(CONFIG_TMV_DECODER) += tmv.o cga_data.o
 OBJS-$(CONFIG_TRUEHD_DECODER)  += mlpdec.o mlpdsp.o
diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index c520d7df83..09cec2 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -35,6 +35,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/avstring.h"
+#include "libavutil/error.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/opt.h"
@@ -46,6 +47,7 @@
 #include "mathops.h"
 #include "tiff.h"
 #include "tiff_data.h"
+#include "mjpegdec.h"
 #include "thread.h"
 #include "get_bits.h"
 
@@ -54,6 +56,10 @@ typedef struct TiffContext {
 AVCodecContext *avctx;
 GetByteContext gb;
 
+/* JPEG decoding for DNG */
+AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG
+AVFrame *jpgframe;   // decoded JPEG tile
+
 int get_subimage;
 uint16_t get_page;
 int get_thumbnail;
@@ -76,7 +82,9 @@ typedef struct TiffContext {
 
 int is_bayer;
 uint8_t pattern[4];
+unsigned black_level;
 unsigned white_level;
+const uint16_t *dng_lut; // Pointer to DNG linearization table
 
 uint32_t sub_ifd;
 uint16_t cur_page;
@@ -86,6 +94,14 @@ typedef struct TiffContext {
 int stripsizesoff, stripsize, stripoff, strippos;
 LZWState *lzw;
 
+/* Tile support */
+int is_tiled;
+int tile_byte_counts_offset, tile_offsets_offset;
+int tile_width, tile_length;
+int tile_count;
+
+int is_jpeg;
+
 uint8_t *deinvert_buf;
 int deinvert_buf_size;
 uint8_t *yuv_line;
@@ -257,6 +273,9 @@ static int add_metadata(int count, int type,
 };
 }
 
+static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
+  const uint8_t *src, int src_stride, int 
width, int height, int is_u16);
+
 static void av_always_inline horizontal_fill(TiffContext *s,
  unsigned int bpp, uint8_t* dst,
  int usePtr, const uint8_t *src,
@@ -712,6 +731,204 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 return 0;
 }
 
+/**
+ * Map stored raw sensor values into linear reference values.
+ * See: DNG Specification - Chapter 5
+ */
+static uint16_t av_always_inline dng_raw_to_linear16(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+// Lookup table lookup
+if (lut)
+value = lut[value];
+
+// Black level subtraction
+value = av_clip_uint16_c((unsigned)value - black_level);
+
+// Color scaling
+value = av_clip_uint16_c((unsigned)(((float)value * scale_factor) * 
0x));
+
+return value;
+}
+
+static uint16_t av_always_inline dng_raw_to_linear8(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+return dng_raw_to_linear16(value, lut, black_level, scale_factor) >> 8;
+}
+
+static void dng_blit(TiffContext *s,

[FFmpeg-devel] [PATCH v7 2/2] lavc/tiff: Decode embedded JPEGs in DNG images

2019-07-25 Thread velocityra
From: Nick Renieris 

Used a technique similar to lavc/tdsc.c for invoking the MJPEG decoder.

This commit adds support for:
- DNG tiles
- DNG tile huffman lossless JPEG decoding
- DNG 8-bpp ("packed" as dcraw calls it) decoding
- DNG color scaling [1]
  - LinearizationTable tag
  - BlackLevel tag

[1]: As specified in the DNG Specification - Chapter 5

Signed-off-by: Nick Renieris 
---
 configure   |   1 +
 libavcodec/Makefile |   2 +-
 libavcodec/tiff.c   | 315 +++-
 libavcodec/tiff.h   |   2 +
 4 files changed, 312 insertions(+), 8 deletions(-)

diff --git a/configure b/configure
index 5a4f507246..6726883d5b 100755
--- a/configure
+++ b/configure
@@ -2811,6 +2811,7 @@ tdsc_decoder_deps="zlib"
 tdsc_decoder_select="mjpeg_decoder"
 theora_decoder_select="vp3_decoder"
 thp_decoder_select="mjpeg_decoder"
+tiff_decoder_select="mjpeg_decoder"
 tiff_decoder_suggest="zlib lzma"
 tiff_encoder_suggest="zlib"
 truehd_decoder_select="mlp_parser"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3cd73fbcc6..f814c69996 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -616,7 +616,7 @@ OBJS-$(CONFIG_TARGA_ENCODER)   += targaenc.o rle.o
 OBJS-$(CONFIG_TARGA_Y216_DECODER)  += targa_y216dec.o
 OBJS-$(CONFIG_TDSC_DECODER)+= tdsc.o
 OBJS-$(CONFIG_TIERTEXSEQVIDEO_DECODER) += tiertexseqv.o
-OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o
+OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o mjpegdec.o
 OBJS-$(CONFIG_TIFF_ENCODER)+= tiffenc.o rle.o lzwenc.o tiff_data.o
 OBJS-$(CONFIG_TMV_DECODER) += tmv.o cga_data.o
 OBJS-$(CONFIG_TRUEHD_DECODER)  += mlpdec.o mlpdsp.o
diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index c520d7df83..09cec2 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -35,6 +35,7 @@
 
 #include "libavutil/attributes.h"
 #include "libavutil/avstring.h"
+#include "libavutil/error.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/opt.h"
@@ -46,6 +47,7 @@
 #include "mathops.h"
 #include "tiff.h"
 #include "tiff_data.h"
+#include "mjpegdec.h"
 #include "thread.h"
 #include "get_bits.h"
 
@@ -54,6 +56,10 @@ typedef struct TiffContext {
 AVCodecContext *avctx;
 GetByteContext gb;
 
+/* JPEG decoding for DNG */
+AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG
+AVFrame *jpgframe;   // decoded JPEG tile
+
 int get_subimage;
 uint16_t get_page;
 int get_thumbnail;
@@ -76,7 +82,9 @@ typedef struct TiffContext {
 
 int is_bayer;
 uint8_t pattern[4];
+unsigned black_level;
 unsigned white_level;
+const uint16_t *dng_lut; // Pointer to DNG linearization table
 
 uint32_t sub_ifd;
 uint16_t cur_page;
@@ -86,6 +94,14 @@ typedef struct TiffContext {
 int stripsizesoff, stripsize, stripoff, strippos;
 LZWState *lzw;
 
+/* Tile support */
+int is_tiled;
+int tile_byte_counts_offset, tile_offsets_offset;
+int tile_width, tile_length;
+int tile_count;
+
+int is_jpeg;
+
 uint8_t *deinvert_buf;
 int deinvert_buf_size;
 uint8_t *yuv_line;
@@ -257,6 +273,9 @@ static int add_metadata(int count, int type,
 };
 }
 
+static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
+  const uint8_t *src, int src_stride, int 
width, int height, int is_u16);
+
 static void av_always_inline horizontal_fill(TiffContext *s,
  unsigned int bpp, uint8_t* dst,
  int usePtr, const uint8_t *src,
@@ -712,6 +731,204 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 return 0;
 }
 
+/**
+ * Map stored raw sensor values into linear reference values.
+ * See: DNG Specification - Chapter 5
+ */
+static uint16_t av_always_inline dng_raw_to_linear16(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+// Lookup table lookup
+if (lut)
+value = lut[value];
+
+// Black level subtraction
+value = av_clip_uint16_c((unsigned)value - black_level);
+
+// Color scaling
+value = av_clip_uint16_c((unsigned)(((float)value * scale_factor) * 
0x));
+
+return value;
+}
+
+static uint16_t av_always_inline dng_raw_to_linear8(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+return dng_raw_to_linear16(value, lut, black_level, scale_factor) >> 8;
+}
+
+static void dng_blit(TiffContext *s,

[FFmpeg-devel] [PATCH v7 1/2] lavc/mjpegdec: Decode Huffman-coded lossless JPEGs embedded in DNGs

2019-07-25 Thread velocityra
From: Nick Renieris 

Main image data in DNGs is usually comprised of tiles, each of which is a 
Huffman-encoded lossless JPEG.

Tested for ljpeg regressions with:
`ffmpeg -f lavfi -i testsrc=d=1 -vcodec ljpeg test.avi`
`ffmpeg test.avi out.avi`
The modified code in ljpeg_decode_rgb_scan runs without issues.

Signed-off-by: Nick Renieris 
---
 libavcodec/mjpegdec.c | 52 +--
 libavcodec/mjpegdec.h |  1 +
 2 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index a65bc8df15..6391107f78 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -412,6 +412,14 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 return AVERROR_PATCHWELCOME;
 }
 
+/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. They contain 
2
+   interleaved components and the width stored in their SOF3 markers is the
+   width of each one.  We only output a single component, therefore we need
+   to adjust the output image width. */
+if (s->lossless == 1 && nb_components == 2) {
+s->bayer = 1;
+width *= 2;
+}
 
 /* if different size, realloc/alloc picture */
 if (width != s->width || height != s->height || bits != s->bits ||
@@ -488,6 +496,9 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 }
 
 switch (pix_fmt_id) {
+case 0x: /* for bayer-encoded huffman lossless JPEGs embedded 
in DNGs */
+s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
+break;
 case 0x1100:
 if (s->rgb)
 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : 
AV_PIX_FMT_BGR48;
@@ -1041,17 +1052,20 @@ static int handle_rstn(MJpegDecodeContext *s, int 
nb_components)
 return reset;
 }
 
+/* Handles 1 to 4 components */
 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int 
predictor, int point_transform)
 {
 int i, mb_x, mb_y;
+unsigned width;
 uint16_t (*buffer)[4];
 int left[4], top[4], topleft[4];
 const int linesize = s->linesize[0];
 const int mask = ((1 << s->bits) - 1) << point_transform;
 int resync_mb_y = 0;
 int resync_mb_x = 0;
+int vpred[6];
 
-if (s->nb_components != 3 && s->nb_components != 4)
+if (s->nb_components <= 0 || s->nb_components > 4)
 return AVERROR_INVALIDDATA;
 if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
 return AVERROR_INVALIDDATA;
@@ -1059,8 +1073,15 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 
 s->restart_count = s->restart_interval;
 
-av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
-   (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
+if (s->restart_interval == 0)
+s->restart_interval = INT_MAX;
+
+if (s->bayer)
+width = s->mb_width / nb_components; /* Interleaved, width stored is 
the total so need to divide */
+else
+width = s->mb_width;
+
+av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, width * 4 * 
sizeof(s->ljpeg_buffer[0][0]));
 if (!s->ljpeg_buffer)
 return AVERROR(ENOMEM);
 
@@ -1078,7 +1099,12 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 for (i = 0; i < 4; i++)
 top[i] = left[i] = topleft[i] = buffer[0][i];
 
-for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
+if ((mb_y * s->width) % s->restart_interval == 0) {
+for (i = 0; i < 6; i++)
+vpred[i] = 1 << (s->bits-1);
+}
+
+for (mb_x = 0; mb_x < width; mb_x++) {
 int modified_predictor = predictor;
 
 if (get_bits_left(&s->gb) < 1) {
@@ -1102,12 +1128,19 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 topleft[i] = top[i];
 top[i] = buffer[mb_x][i];
 
-PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
-
 dc = mjpeg_decode_dc(s, s->dc_index[i]);
 if(dc == 0xF)
 return -1;
 
+if (!s->bayer || mb_x) {
+pred = left[i];
+} else { /* This path runs only for the first line in bayer 
images */
+vpred[i] += dc;
+pred = vpred[i] - dc;
+}
+
+PREDICT(pred, topleft[i], top[i], pred, modified_predictor);
+
 left[i] = buffer[mb_x][i] =
 mask & (pred + (unsigned)(dc * (1 << point_transform)));
 }
@@ -1151,6 +1184,11 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
 }
+} else if (s->bayer && nb_components == 2) {
+for (mb_x = 0; mb

[FFmpeg-devel] [PATCH v6 1/2] lavc/mjpegdec: Decode Huffman-coded lossless JPEGs embedded in DNGs

2019-07-25 Thread velocityra
From: Nick Renieris 

Main image data in DNGs is usually comprised of tiles, each of which is a 
Huffman-encoded lossless JPEG.

Tested for ljpeg regressions with:
`ffmpeg -f lavfi -i testsrc=d=1 -vcodec ljpeg test.avi`
`ffmpeg test.avi out.avi`
The modified code in ljpeg_decode_rgb_scan runs without issues.

Signed-off-by: Nick Renieris 
---
 libavcodec/mjpegdec.c | 54 ---
 libavcodec/mjpegdec.h |  1 +
 2 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index a65bc8df15..d622305298 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -412,6 +412,14 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 return AVERROR_PATCHWELCOME;
 }
 
+/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. They contain 
2
+   interleaved components and the width stored in their SOF3 markers is the
+   width of each one.  We only output a single component, therefore we need
+   to adjust the output image width. */
+if (s->lossless == 1 && nb_components == 2) {
+s->bayer = 1;
+width *= 2;
+}
 
 /* if different size, realloc/alloc picture */
 if (width != s->width || height != s->height || bits != s->bits ||
@@ -488,6 +496,9 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 }
 
 switch (pix_fmt_id) {
+case 0x: /* for bayer-encoded huffman lossless JPEGs embedded 
in DNGs */
+s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
+break;
 case 0x1100:
 if (s->rgb)
 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : 
AV_PIX_FMT_BGR48;
@@ -1041,26 +1052,36 @@ static int handle_rstn(MJpegDecodeContext *s, int 
nb_components)
 return reset;
 }
 
+/* Handles 1 to 4 components */
 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int 
predictor, int point_transform)
 {
 int i, mb_x, mb_y;
+unsigned width;
 uint16_t (*buffer)[4];
 int left[4], top[4], topleft[4];
 const int linesize = s->linesize[0];
 const int mask = ((1 << s->bits) - 1) << point_transform;
 int resync_mb_y = 0;
 int resync_mb_x = 0;
+int vpred[6];
 
-if (s->nb_components != 3 && s->nb_components != 4)
+if (s->nb_components <= 0 || s->nb_components > 4)
 return AVERROR_INVALIDDATA;
+
 if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
 return AVERROR_INVALIDDATA;
 
-
 s->restart_count = s->restart_interval;
 
-av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
-   (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
+if (s->restart_interval == 0)
+s->restart_interval = INT_MAX;
+
+if (s->bayer)
+width = s->mb_width / nb_components; /* Interleaved, width stored is 
the total so need to divide */
+else
+width = s->mb_width;
+
+av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, width * 4 * 
sizeof(s->ljpeg_buffer[0][0]));
 if (!s->ljpeg_buffer)
 return AVERROR(ENOMEM);
 
@@ -1078,7 +1099,12 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 for (i = 0; i < 4; i++)
 top[i] = left[i] = topleft[i] = buffer[0][i];
 
-for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
+if (mb_y * s->width % s->restart_interval == 0) {
+for (i = 0; i < 6; i++)
+vpred[i] = 1 << (s->bits-1);
+}
+
+for (mb_x = 0; mb_x < width; mb_x++) {
 int modified_predictor = predictor;
 
 if (get_bits_left(&s->gb) < 1) {
@@ -1102,12 +1128,19 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 topleft[i] = top[i];
 top[i] = buffer[mb_x][i];
 
-PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
-
 dc = mjpeg_decode_dc(s, s->dc_index[i]);
 if(dc == 0xF)
 return -1;
 
+if (!s->bayer || mb_x) {
+pred = left[i];
+} else { /* This path runs only for the first line in bayer 
images */
+vpred[i] += dc;
+pred = vpred[i] - dc;
+}
+
+PREDICT(pred, topleft[i], top[i], pred, modified_predictor);
+
 left[i] = buffer[mb_x][i] =
 mask & (pred + (unsigned)(dc * (1 << point_transform)));
 }
@@ -1151,6 +1184,11 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
 }
+} else if (s->bayer && nb_components == 2) {
+for (mb_x = 0; mb_x < width; mb_x++) {
+((uint16_t*)ptr)[2*mb_x + 0] = buffer[mb_x][0];
+   

[FFmpeg-devel] [PATCH v6 2/2] lavc/tiff: Decode embedded JPEGs in DNG images

2019-07-25 Thread velocityra
From: Nick Renieris 

Used a technique similar to lavc/tdsc.c for invoking the MJPEG decoder.

This commit adds support for:
- DNG tiles
- DNG tile huffman lossless JPEG decoding
- DNG 8-bpp ("packed" as dcraw calls it) decoding
- DNG color scaling [1]
  - LinearizationTable tag
  - BlackLevel tag

[1]: As specified in the DNG Specification - Chapter 5

Signed-off-by: Nick Renieris 
---
 configure   |   1 +
 libavcodec/Makefile |   2 +-
 libavcodec/tiff.c   | 310 +++-
 libavcodec/tiff.h   |   2 +
 4 files changed, 307 insertions(+), 8 deletions(-)

diff --git a/configure b/configure
index 5a4f507246..54163822a8 100755
--- a/configure
+++ b/configure
@@ -2813,6 +2813,7 @@ theora_decoder_select="vp3_decoder"
 thp_decoder_select="mjpeg_decoder"
 tiff_decoder_suggest="zlib lzma"
 tiff_encoder_suggest="zlib"
+tiff_decoder_select="mjpeg_decoder"
 truehd_decoder_select="mlp_parser"
 truehd_encoder_select="lpc audio_frame_queue"
 truemotion2_decoder_select="bswapdsp"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3cd73fbcc6..f814c69996 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -616,7 +616,7 @@ OBJS-$(CONFIG_TARGA_ENCODER)   += targaenc.o rle.o
 OBJS-$(CONFIG_TARGA_Y216_DECODER)  += targa_y216dec.o
 OBJS-$(CONFIG_TDSC_DECODER)+= tdsc.o
 OBJS-$(CONFIG_TIERTEXSEQVIDEO_DECODER) += tiertexseqv.o
-OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o
+OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o mjpegdec.o
 OBJS-$(CONFIG_TIFF_ENCODER)+= tiffenc.o rle.o lzwenc.o tiff_data.o
 OBJS-$(CONFIG_TMV_DECODER) += tmv.o cga_data.o
 OBJS-$(CONFIG_TRUEHD_DECODER)  += mlpdec.o mlpdsp.o
diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index c520d7df83..4f6ba256c6 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -46,6 +46,7 @@
 #include "mathops.h"
 #include "tiff.h"
 #include "tiff_data.h"
+#include "mjpegdec.h"
 #include "thread.h"
 #include "get_bits.h"
 
@@ -54,6 +55,10 @@ typedef struct TiffContext {
 AVCodecContext *avctx;
 GetByteContext gb;
 
+/* JPEG decoding for DNG */
+AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG
+AVFrame *jpgframe;   // decoded JPEG tile
+
 int get_subimage;
 uint16_t get_page;
 int get_thumbnail;
@@ -76,7 +81,9 @@ typedef struct TiffContext {
 
 int is_bayer;
 uint8_t pattern[4];
+unsigned black_level;
 unsigned white_level;
+const uint16_t *dng_lut; // Pointer to DNG linearization table
 
 uint32_t sub_ifd;
 uint16_t cur_page;
@@ -86,6 +93,14 @@ typedef struct TiffContext {
 int stripsizesoff, stripsize, stripoff, strippos;
 LZWState *lzw;
 
+/* Tile support */
+int is_tiled;
+int tile_byte_counts_offset, tile_offsets_offset;
+int tile_width, tile_length;
+int tile_count;
+
+int is_jpeg; // 0 - Not JPEG, 1 - JPEG, 2 - New JPEG
+
 uint8_t *deinvert_buf;
 int deinvert_buf_size;
 uint8_t *yuv_line;
@@ -257,6 +272,9 @@ static int add_metadata(int count, int type,
 };
 }
 
+static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
+  const uint8_t *src, int src_stride, int 
width, int height, int is_u16);
+
 static void av_always_inline horizontal_fill(TiffContext *s,
  unsigned int bpp, uint8_t* dst,
  int usePtr, const uint8_t *src,
@@ -712,6 +730,203 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 return 0;
 }
 
+/**
+ * Map stored raw sensor values into linear reference values.
+ * See: DNG Specification - Chapter 5
+ */
+static uint16_t av_always_inline dng_raw_to_linear16(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+// Lookup table lookup
+if (lut) value = lut[value];
+
+// Black level subtraction
+value = av_clip_uint16_c((unsigned)value - black_level);
+
+// Color scaling
+value = av_clip_uint16_c((unsigned)(((float)value * scale_factor) * 
0x));
+
+return value;
+}
+
+static uint16_t av_always_inline dng_raw_to_linear8(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+return dng_raw_to_linear16(value, lut, black_level, scale_factor) >> 8;
+}
+
+static void dng_blit(TiffContext *s, uint8_t *dst, int dst_stride,
+ const uint8_t *src, int src_stride,
+ int width, int height, int is_u16)
+{
+int line

[FFmpeg-devel] [PATCH v5 1/2] lavc/mjpegdec: Decode Huffman-coded lossless JPEGs embedded in DNGs

2019-07-24 Thread velocityra
From: Nick Renieris 

Main image data in DNGs is usually comprised of tiles, each of which is a 
Huffman-encoded lossless JPEG.

Tested for ljpeg regressions with:
`ffmpeg -f lavfi -i testsrc=d=1 -vcodec ljpeg test.avi`
`ffmpeg test.avi out.avi`
The modified code in ljpeg_decode_rgb_scan runs without issues.

Signed-off-by: Nick Renieris 
---
 libavcodec/mjpegdec.c | 54 ---
 libavcodec/mjpegdec.h |  1 +
 2 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index a65bc8df15..225833d1bd 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -412,6 +412,14 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 return AVERROR_PATCHWELCOME;
 }
 
+/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. They contain 
2
+   interleaved components and the width stored in their SOF3 markers is the
+   width of each one.  We only output a single component, therefore we need
+   to adjust the output image width. */
+if (s->lossless == 1 && nb_components == 2) {
+s->bayer = 1;
+width *= 2;
+}
 
 /* if different size, realloc/alloc picture */
 if (width != s->width || height != s->height || bits != s->bits ||
@@ -488,6 +496,9 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 }
 
 switch (pix_fmt_id) {
+case 0x: /* for bayer-encoded huffman lossless JPEGs embedded 
in DNGs */
+s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
+break;
 case 0x1100:
 if (s->rgb)
 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : 
AV_PIX_FMT_BGR48;
@@ -1041,26 +1052,36 @@ static int handle_rstn(MJpegDecodeContext *s, int 
nb_components)
 return reset;
 }
 
+/* Handles 1 to 4 components */
 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int 
predictor, int point_transform)
 {
 int i, mb_x, mb_y;
+unsigned width;
 uint16_t (*buffer)[4];
 int left[4], top[4], topleft[4];
 const int linesize = s->linesize[0];
 const int mask = ((1 << s->bits) - 1) << point_transform;
 int resync_mb_y = 0;
 int resync_mb_x = 0;
+int vpred[6];
 
-if (s->nb_components != 3 && s->nb_components != 4)
+if (s->nb_components <= 0 || s->nb_components > 4)
 return AVERROR_INVALIDDATA;
+
 if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
 return AVERROR_INVALIDDATA;
 
-
 s->restart_count = s->restart_interval;
 
-av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
-   (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
+if (s->restart_interval == 0)
+s->restart_interval = INT_MAX;
+
+if (s->bayer)
+width = s->mb_width / nb_components; /* Interleaved, width stored is 
the total so need to divide */
+else
+width = s->mb_width;
+
+av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, width * 4 * 
sizeof(s->ljpeg_buffer[0][0]));
 if (!s->ljpeg_buffer)
 return AVERROR(ENOMEM);
 
@@ -1078,7 +1099,12 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 for (i = 0; i < 4; i++)
 top[i] = left[i] = topleft[i] = buffer[0][i];
 
-for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
+if (mb_y * s->width % s->restart_interval == 0) {
+for (i = 0; i < 6; i++)
+vpred[i] = 1 << (s->bits-1);
+}
+
+for (mb_x = 0; mb_x < width; mb_x++) {
 int modified_predictor = predictor;
 
 if (get_bits_left(&s->gb) < 1) {
@@ -1102,12 +1128,19 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 topleft[i] = top[i];
 top[i] = buffer[mb_x][i];
 
-PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
-
 dc = mjpeg_decode_dc(s, s->dc_index[i]);
 if(dc == 0xF)
 return -1;
 
+if (mb_x) {
+pred = left[i];
+} else {
+vpred[i] += dc;
+pred = vpred[i] - dc;
+}
+
+PREDICT(pred, topleft[i], top[i], pred, modified_predictor);
+
 left[i] = buffer[mb_x][i] =
 mask & (pred + (unsigned)(dc * (1 << point_transform)));
 }
@@ -1151,6 +1184,11 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
 }
+} else if (s->bayer && nb_components == 2) {
+for (mb_x = 0; mb_x < width; mb_x++) {
+((uint16_t*)ptr)[2*mb_x + 0] = buffer[mb_x][0];
+((uint16_t*)ptr)[2*mb_x + 1] = buffer[mb_x][1];
+}
 } 

[FFmpeg-devel] [PATCH v5 2/2] lavc/tiff: Decode embedded JPEGs in DNG images

2019-07-24 Thread velocityra
From: Nick Renieris 

Used a technique similar to lavc/tdsc.c for invoking the MJPEG decoder.

This commit adds support for:
- DNG tiles
- DNG tile huffman lossless JPEG decoding
- DNG 8-bpp ("packed" as dcraw calls it) decoding
- DNG color scaling [1]
  - LinearizationTable tag
  - BlackLevel tag

[1]: As specified in the DNG Specification - Chapter 5

Signed-off-by: Nick Renieris 
---
 configure   |   1 +
 libavcodec/Makefile |   2 +-
 libavcodec/tiff.c   | 310 +++-
 libavcodec/tiff.h   |   2 +
 4 files changed, 307 insertions(+), 8 deletions(-)

diff --git a/configure b/configure
index 5a4f507246..54163822a8 100755
--- a/configure
+++ b/configure
@@ -2813,6 +2813,7 @@ theora_decoder_select="vp3_decoder"
 thp_decoder_select="mjpeg_decoder"
 tiff_decoder_suggest="zlib lzma"
 tiff_encoder_suggest="zlib"
+tiff_decoder_select="mjpeg_decoder"
 truehd_decoder_select="mlp_parser"
 truehd_encoder_select="lpc audio_frame_queue"
 truemotion2_decoder_select="bswapdsp"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3cd73fbcc6..f814c69996 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -616,7 +616,7 @@ OBJS-$(CONFIG_TARGA_ENCODER)   += targaenc.o rle.o
 OBJS-$(CONFIG_TARGA_Y216_DECODER)  += targa_y216dec.o
 OBJS-$(CONFIG_TDSC_DECODER)+= tdsc.o
 OBJS-$(CONFIG_TIERTEXSEQVIDEO_DECODER) += tiertexseqv.o
-OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o
+OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o mjpegdec.o
 OBJS-$(CONFIG_TIFF_ENCODER)+= tiffenc.o rle.o lzwenc.o tiff_data.o
 OBJS-$(CONFIG_TMV_DECODER) += tmv.o cga_data.o
 OBJS-$(CONFIG_TRUEHD_DECODER)  += mlpdec.o mlpdsp.o
diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index c520d7df83..4f6ba256c6 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -46,6 +46,7 @@
 #include "mathops.h"
 #include "tiff.h"
 #include "tiff_data.h"
+#include "mjpegdec.h"
 #include "thread.h"
 #include "get_bits.h"
 
@@ -54,6 +55,10 @@ typedef struct TiffContext {
 AVCodecContext *avctx;
 GetByteContext gb;
 
+/* JPEG decoding for DNG */
+AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG
+AVFrame *jpgframe;   // decoded JPEG tile
+
 int get_subimage;
 uint16_t get_page;
 int get_thumbnail;
@@ -76,7 +81,9 @@ typedef struct TiffContext {
 
 int is_bayer;
 uint8_t pattern[4];
+unsigned black_level;
 unsigned white_level;
+const uint16_t *dng_lut; // Pointer to DNG linearization table
 
 uint32_t sub_ifd;
 uint16_t cur_page;
@@ -86,6 +93,14 @@ typedef struct TiffContext {
 int stripsizesoff, stripsize, stripoff, strippos;
 LZWState *lzw;
 
+/* Tile support */
+int is_tiled;
+int tile_byte_counts_offset, tile_offsets_offset;
+int tile_width, tile_length;
+int tile_count;
+
+int is_jpeg; // 0 - Not JPEG, 1 - JPEG, 2 - New JPEG
+
 uint8_t *deinvert_buf;
 int deinvert_buf_size;
 uint8_t *yuv_line;
@@ -257,6 +272,9 @@ static int add_metadata(int count, int type,
 };
 }
 
+static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
+  const uint8_t *src, int src_stride, int 
width, int height, int is_u16);
+
 static void av_always_inline horizontal_fill(TiffContext *s,
  unsigned int bpp, uint8_t* dst,
  int usePtr, const uint8_t *src,
@@ -712,6 +730,203 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 return 0;
 }
 
+/**
+ * Map stored raw sensor values into linear reference values.
+ * See: DNG Specification - Chapter 5
+ */
+static uint16_t av_always_inline dng_raw_to_linear16(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+// Lookup table lookup
+if (lut) value = lut[value];
+
+// Black level subtraction
+value = av_clip_uint16_c((unsigned)value - black_level);
+
+// Color scaling
+value = av_clip_uint16_c((unsigned)(((float)value * scale_factor) * 
0x));
+
+return value;
+}
+
+static uint16_t av_always_inline dng_raw_to_linear8(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+return dng_raw_to_linear16(value, lut, black_level, scale_factor) >> 8;
+}
+
+static void dng_blit(TiffContext *s, uint8_t *dst, int dst_stride,
+ const uint8_t *src, int src_stride,
+ int width, int height, int is_u16)
+{
+int line

[FFmpeg-devel] [PATCH] lavc/tiff: Decode embedded JPEGs in DNG images

2019-07-24 Thread velocityra
From: Nick Renieris 

Used a technique similar to lavc/tdsc.c for invoking the MJPEG decoder.

This commit adds support for:
- DNG tiles
- DNG tile huffman lossless JPEG decoding
- DNG color scaling [1]
  - LinearizationTable tag
  - BlackLevel tag

[1]: As specified in the DNG Specification - Chapter 5

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 16 
 1 file changed, 16 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 423eaf0647..4f6ba256c6 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -280,19 +280,6 @@ static void av_always_inline horizontal_fill(TiffContext 
*s,
  int usePtr, const uint8_t *src,
  uint8_t c, int width, int offset)
 {
-/* Handle DNG images with uncompressed strips (non-tiled) */
-if (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG) {
-dng_blit(s,
- dst + offset,
- width,
- src,
- width,
- width,
- 1,
- 0);
-return;
-}
-
 switch (bpp) {
 case 1:
 while (--width >= 0) {
@@ -1799,9 +1786,6 @@ again:
 } else if (s->is_tiled) {
 av_log(avctx, AV_LOG_ERROR, "DNG uncompressed tiled images are not 
supported\n");
 return AVERROR_PATCHWELCOME;
-} else if (s->bpp != 8) {
-av_log(avctx, AV_LOG_ERROR, "DNG uncompressed non-tiled non-8bpp 
images are not supported\n");
-return AVERROR_PATCHWELCOME;
 }
 }
 
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v3 1/2] lavc/mjpegdec: Decode Huffman-coded lossless JPEGs embedded in DNGs

2019-07-23 Thread velocityra
From: Nick Renieris 

Main image data in DNGs is usually comprised of tiles, each of which is a 
Huffman-encoded lossless JPEG.

Tested for ljpeg regressions with:
`ffmpeg -f lavfi -i testsrc=d=1 -vcodec ljpeg test.avi`
`ffmpeg test.avi out.avi`
The modified code in ljpeg_decode_rgb_scan runs without issues.

Signed-off-by: Nick Renieris 
---
 libavcodec/mjpegdec.c | 54 ---
 libavcodec/mjpegdec.h |  1 +
 2 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index a65bc8df15..225833d1bd 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -412,6 +412,14 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 return AVERROR_PATCHWELCOME;
 }
 
+/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. They contain 
2
+   interleaved components and the width stored in their SOF3 markers is the
+   width of each one.  We only output a single component, therefore we need
+   to adjust the output image width. */
+if (s->lossless == 1 && nb_components == 2) {
+s->bayer = 1;
+width *= 2;
+}
 
 /* if different size, realloc/alloc picture */
 if (width != s->width || height != s->height || bits != s->bits ||
@@ -488,6 +496,9 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 }
 
 switch (pix_fmt_id) {
+case 0x: /* for bayer-encoded huffman lossless JPEGs embedded 
in DNGs */
+s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
+break;
 case 0x1100:
 if (s->rgb)
 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : 
AV_PIX_FMT_BGR48;
@@ -1041,26 +1052,36 @@ static int handle_rstn(MJpegDecodeContext *s, int 
nb_components)
 return reset;
 }
 
+/* Handles 1 to 4 components */
 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int 
predictor, int point_transform)
 {
 int i, mb_x, mb_y;
+unsigned width;
 uint16_t (*buffer)[4];
 int left[4], top[4], topleft[4];
 const int linesize = s->linesize[0];
 const int mask = ((1 << s->bits) - 1) << point_transform;
 int resync_mb_y = 0;
 int resync_mb_x = 0;
+int vpred[6];
 
-if (s->nb_components != 3 && s->nb_components != 4)
+if (s->nb_components <= 0 || s->nb_components > 4)
 return AVERROR_INVALIDDATA;
+
 if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
 return AVERROR_INVALIDDATA;
 
-
 s->restart_count = s->restart_interval;
 
-av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
-   (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
+if (s->restart_interval == 0)
+s->restart_interval = INT_MAX;
+
+if (s->bayer)
+width = s->mb_width / nb_components; /* Interleaved, width stored is 
the total so need to divide */
+else
+width = s->mb_width;
+
+av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, width * 4 * 
sizeof(s->ljpeg_buffer[0][0]));
 if (!s->ljpeg_buffer)
 return AVERROR(ENOMEM);
 
@@ -1078,7 +1099,12 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 for (i = 0; i < 4; i++)
 top[i] = left[i] = topleft[i] = buffer[0][i];
 
-for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
+if (mb_y * s->width % s->restart_interval == 0) {
+for (i = 0; i < 6; i++)
+vpred[i] = 1 << (s->bits-1);
+}
+
+for (mb_x = 0; mb_x < width; mb_x++) {
 int modified_predictor = predictor;
 
 if (get_bits_left(&s->gb) < 1) {
@@ -1102,12 +1128,19 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 topleft[i] = top[i];
 top[i] = buffer[mb_x][i];
 
-PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
-
 dc = mjpeg_decode_dc(s, s->dc_index[i]);
 if(dc == 0xF)
 return -1;
 
+if (mb_x) {
+pred = left[i];
+} else {
+vpred[i] += dc;
+pred = vpred[i] - dc;
+}
+
+PREDICT(pred, topleft[i], top[i], pred, modified_predictor);
+
 left[i] = buffer[mb_x][i] =
 mask & (pred + (unsigned)(dc * (1 << point_transform)));
 }
@@ -1151,6 +1184,11 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
 }
+} else if (s->bayer && nb_components == 2) {
+for (mb_x = 0; mb_x < width; mb_x++) {
+((uint16_t*)ptr)[2*mb_x + 0] = buffer[mb_x][0];
+((uint16_t*)ptr)[2*mb_x + 1] = buffer[mb_x][1];
+}
 } 

[FFmpeg-devel] [PATCH v3 2/2] lavc/tiff: Decode embedded JPEGs in DNG images

2019-07-23 Thread velocityra
From: Nick Renieris 

Used a technique similar to lavc/tdsc.c for invoking the MJPEG decoder.

This commit adds support for:
- DNG tiles
- DNG tile huffman lossless JPEG decoding
- DNG 8-bpp ("packed" as dcraw calls it) decoding
- DNG color scaling [1]
  - LinearizationTable tag
  - BlackLevel tag

[1]: As specified in the DNG Specification - Chapter 5

Signed-off-by: Nick Renieris 
---
 configure   |   1 +
 libavcodec/Makefile |   2 +-
 libavcodec/tiff.c   | 326 +++-
 libavcodec/tiff.h   |   2 +
 4 files changed, 323 insertions(+), 8 deletions(-)

diff --git a/configure b/configure
index 5a4f507246..54163822a8 100755
--- a/configure
+++ b/configure
@@ -2813,6 +2813,7 @@ theora_decoder_select="vp3_decoder"
 thp_decoder_select="mjpeg_decoder"
 tiff_decoder_suggest="zlib lzma"
 tiff_encoder_suggest="zlib"
+tiff_decoder_select="mjpeg_decoder"
 truehd_decoder_select="mlp_parser"
 truehd_encoder_select="lpc audio_frame_queue"
 truemotion2_decoder_select="bswapdsp"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3cd73fbcc6..f814c69996 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -616,7 +616,7 @@ OBJS-$(CONFIG_TARGA_ENCODER)   += targaenc.o rle.o
 OBJS-$(CONFIG_TARGA_Y216_DECODER)  += targa_y216dec.o
 OBJS-$(CONFIG_TDSC_DECODER)+= tdsc.o
 OBJS-$(CONFIG_TIERTEXSEQVIDEO_DECODER) += tiertexseqv.o
-OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o
+OBJS-$(CONFIG_TIFF_DECODER)+= tiff.o lzw.o faxcompr.o tiff_data.o 
tiff_common.o mjpegdec.o
 OBJS-$(CONFIG_TIFF_ENCODER)+= tiffenc.o rle.o lzwenc.o tiff_data.o
 OBJS-$(CONFIG_TMV_DECODER) += tmv.o cga_data.o
 OBJS-$(CONFIG_TRUEHD_DECODER)  += mlpdec.o mlpdsp.o
diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index c520d7df83..423eaf0647 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -46,6 +46,7 @@
 #include "mathops.h"
 #include "tiff.h"
 #include "tiff_data.h"
+#include "mjpegdec.h"
 #include "thread.h"
 #include "get_bits.h"
 
@@ -54,6 +55,10 @@ typedef struct TiffContext {
 AVCodecContext *avctx;
 GetByteContext gb;
 
+/* JPEG decoding for DNG */
+AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG
+AVFrame *jpgframe;   // decoded JPEG tile
+
 int get_subimage;
 uint16_t get_page;
 int get_thumbnail;
@@ -76,7 +81,9 @@ typedef struct TiffContext {
 
 int is_bayer;
 uint8_t pattern[4];
+unsigned black_level;
 unsigned white_level;
+const uint16_t *dng_lut; // Pointer to DNG linearization table
 
 uint32_t sub_ifd;
 uint16_t cur_page;
@@ -86,6 +93,14 @@ typedef struct TiffContext {
 int stripsizesoff, stripsize, stripoff, strippos;
 LZWState *lzw;
 
+/* Tile support */
+int is_tiled;
+int tile_byte_counts_offset, tile_offsets_offset;
+int tile_width, tile_length;
+int tile_count;
+
+int is_jpeg; // 0 - Not JPEG, 1 - JPEG, 2 - New JPEG
+
 uint8_t *deinvert_buf;
 int deinvert_buf_size;
 uint8_t *yuv_line;
@@ -257,11 +272,27 @@ static int add_metadata(int count, int type,
 };
 }
 
+static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
+  const uint8_t *src, int src_stride, int 
width, int height, int is_u16);
+
 static void av_always_inline horizontal_fill(TiffContext *s,
  unsigned int bpp, uint8_t* dst,
  int usePtr, const uint8_t *src,
  uint8_t c, int width, int offset)
 {
+/* Handle DNG images with uncompressed strips (non-tiled) */
+if (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG) {
+dng_blit(s,
+ dst + offset,
+ width,
+ src,
+ width,
+ width,
+ 1,
+ 0);
+return;
+}
+
 switch (bpp) {
 case 1:
 while (--width >= 0) {
@@ -712,6 +743,203 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 return 0;
 }
 
+/**
+ * Map stored raw sensor values into linear reference values.
+ * See: DNG Specification - Chapter 5
+ */
+static uint16_t av_always_inline dng_raw_to_linear16(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+// Lookup table lookup
+if (lut) value = lut[value];
+
+// Black level subtraction
+value = av_clip_uint16_c((unsigned)value - black_level);
+
+// Color scaling
+value = av_clip_uint16_c((unsigned)(((float)value * scale_factor) * 
0x));
+
+return value;
+}
+
+static uint16_t av_always_inline dng_raw_to_linear8(uint

[FFmpeg-devel] [PATCH v2 2/2] lavc/tiff: Decode embedded JPEGs in DNG images

2019-07-23 Thread velocityra
From: Nick Renieris 

Used a technique similar to lavc/tdsc.c for invoking the MJPEG decoder.

This commit adds support for:
- DNG tiles
- DNG tile huffman lossless JPEG decoding
- DNG 8-bpp ("packed" as dcraw calls it) decoding
- DNG color scaling [1]
  - LinearizationTable tag
  - BlackLevel tag

[1]: As specified in the DNG Specification - Chapter 5

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 326 +-
 libavcodec/tiff.h |   2 +
 2 files changed, 321 insertions(+), 7 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index c520d7df83..423eaf0647 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -46,6 +46,7 @@
 #include "mathops.h"
 #include "tiff.h"
 #include "tiff_data.h"
+#include "mjpegdec.h"
 #include "thread.h"
 #include "get_bits.h"
 
@@ -54,6 +55,10 @@ typedef struct TiffContext {
 AVCodecContext *avctx;
 GetByteContext gb;
 
+/* JPEG decoding for DNG */
+AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG
+AVFrame *jpgframe;   // decoded JPEG tile
+
 int get_subimage;
 uint16_t get_page;
 int get_thumbnail;
@@ -76,7 +81,9 @@ typedef struct TiffContext {
 
 int is_bayer;
 uint8_t pattern[4];
+unsigned black_level;
 unsigned white_level;
+const uint16_t *dng_lut; // Pointer to DNG linearization table
 
 uint32_t sub_ifd;
 uint16_t cur_page;
@@ -86,6 +93,14 @@ typedef struct TiffContext {
 int stripsizesoff, stripsize, stripoff, strippos;
 LZWState *lzw;
 
+/* Tile support */
+int is_tiled;
+int tile_byte_counts_offset, tile_offsets_offset;
+int tile_width, tile_length;
+int tile_count;
+
+int is_jpeg; // 0 - Not JPEG, 1 - JPEG, 2 - New JPEG
+
 uint8_t *deinvert_buf;
 int deinvert_buf_size;
 uint8_t *yuv_line;
@@ -257,11 +272,27 @@ static int add_metadata(int count, int type,
 };
 }
 
+static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
+  const uint8_t *src, int src_stride, int 
width, int height, int is_u16);
+
 static void av_always_inline horizontal_fill(TiffContext *s,
  unsigned int bpp, uint8_t* dst,
  int usePtr, const uint8_t *src,
  uint8_t c, int width, int offset)
 {
+/* Handle DNG images with uncompressed strips (non-tiled) */
+if (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG) {
+dng_blit(s,
+ dst + offset,
+ width,
+ src,
+ width,
+ width,
+ 1,
+ 0);
+return;
+}
+
 switch (bpp) {
 case 1:
 while (--width >= 0) {
@@ -712,6 +743,203 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 return 0;
 }
 
+/**
+ * Map stored raw sensor values into linear reference values.
+ * See: DNG Specification - Chapter 5
+ */
+static uint16_t av_always_inline dng_raw_to_linear16(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+// Lookup table lookup
+if (lut) value = lut[value];
+
+// Black level subtraction
+value = av_clip_uint16_c((unsigned)value - black_level);
+
+// Color scaling
+value = av_clip_uint16_c((unsigned)(((float)value * scale_factor) * 
0x));
+
+return value;
+}
+
+static uint16_t av_always_inline dng_raw_to_linear8(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+return dng_raw_to_linear16(value, lut, black_level, scale_factor) >> 8;
+}
+
+static void dng_blit(TiffContext *s, uint8_t *dst, int dst_stride,
+ const uint8_t *src, int src_stride,
+ int width, int height, int is_u16)
+{
+int line, col;
+float scale_factor;
+
+scale_factor = 1.0 / (s->white_level - s->black_level);
+
+if (is_u16) {
+for (line = 0; line < height; line++) {
+uint16_t *dst_u16 = (uint16_t *)dst;
+uint16_t *src_u16 = (uint16_t *)src;
+
+for (col = 0; col < width; col++)
+*dst_u16++ = dng_raw_to_linear16(*src_u16++, s->dng_lut, 
s->black_level, scale_factor);
+
+dst += dst_stride * sizeof(uint16_t);
+src += src_stride * sizeof(uint16_t);
+}
+} else {
+for (line = 0; line < height; line++) {
+for (col = 0; col < width; col++)
+*dst++ = dng_raw_to_linear8(*src++, s->dng_lut, 
s->black_level, scale_factor);
+
+  

[FFmpeg-devel] [PATCH v2 1/2] lavc/mjpegdec: Decode Huffman-coded lossless JPEGs embedded in DNGs

2019-07-23 Thread velocityra
From: Nick Renieris 

Main image data in DNGs is usually comprised of tiles, each of which is a 
Huffman-encoded lossless JPEG.

Tested for ljpeg regressions with:
`ffmpeg -f lavfi -i testsrc=d=1 -vcodec ljpeg test.avi`
`ffmpeg test.avi out.avi`
The modified code in ljpeg_decode_rgb_scan runs without issues.

Signed-off-by: Nick Renieris 
---
 libavcodec/mjpegdec.c | 54 ---
 libavcodec/mjpegdec.h |  1 +
 2 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index a65bc8df15..225833d1bd 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -412,6 +412,14 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 return AVERROR_PATCHWELCOME;
 }
 
+/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. They contain 
2
+   interleaved components and the width stored in their SOF3 markers is the
+   width of each one.  We only output a single component, therefore we need
+   to adjust the output image width. */
+if (s->lossless == 1 && nb_components == 2) {
+s->bayer = 1;
+width *= 2;
+}
 
 /* if different size, realloc/alloc picture */
 if (width != s->width || height != s->height || bits != s->bits ||
@@ -488,6 +496,9 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 }
 
 switch (pix_fmt_id) {
+case 0x: /* for bayer-encoded huffman lossless JPEGs embedded 
in DNGs */
+s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
+break;
 case 0x1100:
 if (s->rgb)
 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : 
AV_PIX_FMT_BGR48;
@@ -1041,26 +1052,36 @@ static int handle_rstn(MJpegDecodeContext *s, int 
nb_components)
 return reset;
 }
 
+/* Handles 1 to 4 components */
 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int 
predictor, int point_transform)
 {
 int i, mb_x, mb_y;
+unsigned width;
 uint16_t (*buffer)[4];
 int left[4], top[4], topleft[4];
 const int linesize = s->linesize[0];
 const int mask = ((1 << s->bits) - 1) << point_transform;
 int resync_mb_y = 0;
 int resync_mb_x = 0;
+int vpred[6];
 
-if (s->nb_components != 3 && s->nb_components != 4)
+if (s->nb_components <= 0 || s->nb_components > 4)
 return AVERROR_INVALIDDATA;
+
 if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
 return AVERROR_INVALIDDATA;
 
-
 s->restart_count = s->restart_interval;
 
-av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
-   (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
+if (s->restart_interval == 0)
+s->restart_interval = INT_MAX;
+
+if (s->bayer)
+width = s->mb_width / nb_components; /* Interleaved, width stored is 
the total so need to divide */
+else
+width = s->mb_width;
+
+av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, width * 4 * 
sizeof(s->ljpeg_buffer[0][0]));
 if (!s->ljpeg_buffer)
 return AVERROR(ENOMEM);
 
@@ -1078,7 +1099,12 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 for (i = 0; i < 4; i++)
 top[i] = left[i] = topleft[i] = buffer[0][i];
 
-for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
+if (mb_y * s->width % s->restart_interval == 0) {
+for (i = 0; i < 6; i++)
+vpred[i] = 1 << (s->bits-1);
+}
+
+for (mb_x = 0; mb_x < width; mb_x++) {
 int modified_predictor = predictor;
 
 if (get_bits_left(&s->gb) < 1) {
@@ -1102,12 +1128,19 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 topleft[i] = top[i];
 top[i] = buffer[mb_x][i];
 
-PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
-
 dc = mjpeg_decode_dc(s, s->dc_index[i]);
 if(dc == 0xF)
 return -1;
 
+if (mb_x) {
+pred = left[i];
+} else {
+vpred[i] += dc;
+pred = vpred[i] - dc;
+}
+
+PREDICT(pred, topleft[i], top[i], pred, modified_predictor);
+
 left[i] = buffer[mb_x][i] =
 mask & (pred + (unsigned)(dc * (1 << point_transform)));
 }
@@ -1151,6 +1184,11 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
 }
+} else if (s->bayer && nb_components == 2) {
+for (mb_x = 0; mb_x < width; mb_x++) {
+((uint16_t*)ptr)[2*mb_x + 0] = buffer[mb_x][0];
+((uint16_t*)ptr)[2*mb_x + 1] = buffer[mb_x][1];
+}
 } 

[FFmpeg-devel] [PATCH 1/2] lavc/mjpegdec: Decode Huffman-coded lossless JPEGs embedded in DNGs

2019-07-23 Thread velocityra
From: Nick Renieris 

Main image data in DNGs is usually comprised of tiles, each of which is a 
Huffman-encoded lossless JPEG.

Changes in ljpeg_decode_rgb_scan are based on dcraw.

Tested for ljpeg regressions with:
`ffmpeg -f lavfi -i testsrc=d=1 -vcodec ljpeg test.avi`
`ffmpeg test.avi out.avi`
The modified code in ljpeg_decode_rgb_scan runs without issues.

Signed-off-by: Nick Renieris 
---
 libavcodec/mjpegdec.c | 54 ---
 libavcodec/mjpegdec.h |  1 +
 2 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index a65bc8df15..225833d1bd 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -412,6 +412,14 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 return AVERROR_PATCHWELCOME;
 }
 
+/* Lossless JPEGs encoded in DNGs are commonly bayer-encoded. They contain 
2
+   interleaved components and the width stored in their SOF3 markers is the
+   width of each one.  We only output a single component, therefore we need
+   to adjust the output image width. */
+if (s->lossless == 1 && nb_components == 2) {
+s->bayer = 1;
+width *= 2;
+}
 
 /* if different size, realloc/alloc picture */
 if (width != s->width || height != s->height || bits != s->bits ||
@@ -488,6 +496,9 @@ int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
 }
 
 switch (pix_fmt_id) {
+case 0x: /* for bayer-encoded huffman lossless JPEGs embedded 
in DNGs */
+s->avctx->pix_fmt = AV_PIX_FMT_GRAY16LE;
+break;
 case 0x1100:
 if (s->rgb)
 s->avctx->pix_fmt = s->bits <= 9 ? AV_PIX_FMT_BGR24 : 
AV_PIX_FMT_BGR48;
@@ -1041,26 +1052,36 @@ static int handle_rstn(MJpegDecodeContext *s, int 
nb_components)
 return reset;
 }
 
+/* Handles 1 to 4 components */
 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int nb_components, int 
predictor, int point_transform)
 {
 int i, mb_x, mb_y;
+unsigned width;
 uint16_t (*buffer)[4];
 int left[4], top[4], topleft[4];
 const int linesize = s->linesize[0];
 const int mask = ((1 << s->bits) - 1) << point_transform;
 int resync_mb_y = 0;
 int resync_mb_x = 0;
+int vpred[6];
 
-if (s->nb_components != 3 && s->nb_components != 4)
+if (s->nb_components <= 0 || s->nb_components > 4)
 return AVERROR_INVALIDDATA;
+
 if (s->v_max != 1 || s->h_max != 1 || !s->lossless)
 return AVERROR_INVALIDDATA;
 
-
 s->restart_count = s->restart_interval;
 
-av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
-   (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
+if (s->restart_interval == 0)
+s->restart_interval = INT_MAX;
+
+if (s->bayer)
+width = s->mb_width / nb_components; /* Interleaved, width stored is 
the total so need to divide */
+else
+width = s->mb_width;
+
+av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, width * 4 * 
sizeof(s->ljpeg_buffer[0][0]));
 if (!s->ljpeg_buffer)
 return AVERROR(ENOMEM);
 
@@ -1078,7 +1099,12 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 for (i = 0; i < 4; i++)
 top[i] = left[i] = topleft[i] = buffer[0][i];
 
-for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
+if (mb_y * s->width % s->restart_interval == 0) {
+for (i = 0; i < 6; i++)
+vpred[i] = 1 << (s->bits-1);
+}
+
+for (mb_x = 0; mb_x < width; mb_x++) {
 int modified_predictor = predictor;
 
 if (get_bits_left(&s->gb) < 1) {
@@ -1102,12 +1128,19 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 topleft[i] = top[i];
 top[i] = buffer[mb_x][i];
 
-PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
-
 dc = mjpeg_decode_dc(s, s->dc_index[i]);
 if(dc == 0xF)
 return -1;
 
+if (mb_x) {
+pred = left[i];
+} else {
+vpred[i] += dc;
+pred = vpred[i] - dc;
+}
+
+PREDICT(pred, topleft[i], top[i], pred, modified_predictor);
+
 left[i] = buffer[mb_x][i] =
 mask & (pred + (unsigned)(dc * (1 << point_transform)));
 }
@@ -1151,6 +1184,11 @@ static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, 
int nb_components, int p
 ptr[3*mb_x + 0] = buffer[mb_x][1] + ptr[3*mb_x + 1];
 ptr[3*mb_x + 2] = buffer[mb_x][2] + ptr[3*mb_x + 1];
 }
+} else if (s->bayer && nb_components == 2) {
+for (mb_x = 0; mb_x < width; mb_x++) {
+((uint16_t*)ptr)[2*mb_x + 0] = buffer[mb_x][0];
+((uint16_t*)ptr)[2*m

[FFmpeg-devel] [PATCH 2/2] lavc/tiff: Decode embedded JPEGs in DNG images

2019-07-23 Thread velocityra
From: Nick Renieris 

Used a technique similar to lavc/tdsc.c for invoking the MJPEG decoder.

This commit adds support for:
- DNG tiles
- DNG tile huffman lossless JPEG decoding
- DNG 8-bpp ("packed" as dcraw calls it) decoding
- DNG color scaling [1]
  - LinearizationTable tag
  - BlackLevel tag

[1]: As specified in the DNG Specification - Chapter 5

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 328 +-
 libavcodec/tiff.h |   2 +
 2 files changed, 323 insertions(+), 7 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index c520d7df83..e62adb9fc1 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -46,6 +46,7 @@
 #include "mathops.h"
 #include "tiff.h"
 #include "tiff_data.h"
+#include "mjpegdec.h"
 #include "thread.h"
 #include "get_bits.h"
 
@@ -54,6 +55,10 @@ typedef struct TiffContext {
 AVCodecContext *avctx;
 GetByteContext gb;
 
+/* JPEG decoding for DNG */
+AVCodecContext *avctx_mjpeg; // wrapper context for MJPEG
+AVFrame *jpgframe;   // decoded JPEG tile
+
 int get_subimage;
 uint16_t get_page;
 int get_thumbnail;
@@ -76,7 +81,9 @@ typedef struct TiffContext {
 
 int is_bayer;
 uint8_t pattern[4];
+unsigned black_level;
 unsigned white_level;
+const uint16_t *dng_lut; // Pointer to DNG linearization table
 
 uint32_t sub_ifd;
 uint16_t cur_page;
@@ -86,6 +93,14 @@ typedef struct TiffContext {
 int stripsizesoff, stripsize, stripoff, strippos;
 LZWState *lzw;
 
+/* Tile support */
+int is_tiled;
+int tile_byte_counts_offset, tile_offsets_offset;
+int tile_width, tile_length;
+int tile_count;
+
+int is_jpeg; // 0 - Not JPEG, 1 - JPEG, 2 - New JPEG
+
 uint8_t *deinvert_buf;
 int deinvert_buf_size;
 uint8_t *yuv_line;
@@ -257,11 +272,32 @@ static int add_metadata(int count, int type,
 };
 }
 
+static void av_always_inline dng_blit(TiffContext *s, uint8_t *dst, int 
dst_stride,
+  const uint8_t *src, int src_stride, int 
width, int height, int is_u16);
+
 static void av_always_inline horizontal_fill(TiffContext *s,
  unsigned int bpp, uint8_t* dst,
  int usePtr, const uint8_t *src,
  uint8_t c, int width, int offset)
 {
+/* Handle DNG images with uncompressed strips (non-tiled) */
+if (s->tiff_type == TIFF_TYPE_DNG || s->tiff_type == TIFF_TYPE_CINEMADNG) {
+int is_u16, pixel_size;
+
+is_u16 = (bpp > 8);
+pixel_size = (is_u16 ? sizeof(uint16_t) : sizeof(uint8_t));
+
+dng_blit(s,
+ dst + offset,
+ width * pixel_size,
+ src,
+ width * pixel_size,
+ width * pixel_size,
+ 1,
+ is_u16);
+return;
+}
+
 switch (bpp) {
 case 1:
 while (--width >= 0) {
@@ -712,6 +748,203 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, 
uint8_t *dst, int strid
 return 0;
 }
 
+/**
+ * Map stored raw sensor values into linear reference values.
+ * See: DNG Specification - Chapter 5
+ */
+static uint16_t av_always_inline dng_raw_to_linear16(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+// Lookup table lookup
+if (lut) value = lut[value];
+
+// Black level subtraction
+value = av_clip_uint16_c((unsigned)value - black_level);
+
+// Color scaling
+value = av_clip_uint16_c((unsigned)(((float)value * scale_factor) * 
0x));
+
+return value;
+}
+
+static uint16_t av_always_inline dng_raw_to_linear8(uint16_t value,
+const uint16_t *lut,
+uint16_t black_level,
+float scale_factor) {
+return dng_raw_to_linear16(value, lut, black_level, scale_factor) >> 8;
+}
+
+static void dng_blit(TiffContext *s, uint8_t *dst, int dst_stride,
+ const uint8_t *src, int src_stride,
+ int width, int height, int is_u16)
+{
+int line, col;
+float scale_factor;
+
+scale_factor = 1.0 / (s->white_level - s->black_level);
+
+if (is_u16) {
+for (line = 0; line < height; line++) {
+uint16_t *dst_u16 = (uint16_t *)dst;
+uint16_t *src_u16 = (uint16_t *)src;
+
+for (col = 0; col < width; col++)
+*dst_u16++ = dng_raw_to_linear16(*src_u16++, s->dng_lut, 
s->black_level, scale_factor);
+
+dst += dst_stride * sizeof(uint16_t);
+src += src_stride * sizeof(uint16_t);
+}
+} else {
+for (line = 0;

[FFmpeg-devel] [PATCH v2 1/3] avcodec/tiff: Option to decode embedded thumbnail

2019-05-30 Thread velocityra
From: Nick Renieris 

Adds the "-thumbnail" option, that works like the current "-subifd"
option, but only for non-full-sized images.

This is particularly useful for DNG images (see next commit) that
have SubIFDs that are not necessarily thumbnails.

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 17 ++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 79e6242549..6e15d00310 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -56,6 +56,7 @@ typedef struct TiffContext {
 
 int get_subimage;
 uint16_t get_page;
+int get_thumbnail;
 
 int width, height;
 unsigned int bpp, bppcount;
@@ -70,6 +71,7 @@ typedef struct TiffContext {
 int predictor;
 int fill_order;
 uint32_t res[4];
+int is_thumbnail;
 
 int is_bayer;
 uint8_t pattern[4];
@@ -948,6 +950,8 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
 }
 
 switch (tag) {
+case TIFF_SUBFILE:
+s->is_thumbnail = (value != 0);
 case TIFF_WIDTH:
 s->width = value;
 break;
@@ -1387,6 +1391,7 @@ static int decode_frame(AVCodecContext *avctx,
 s->le  = le;
 // TIFF_BPP is not a required tag and defaults to 1
 again:
+s->is_thumbnail = 0;
 s->bppcount= s->bpp = 1;
 s->photometric = TIFF_PHOTOMETRIC_NONE;
 s->compr   = TIFF_RAW;
@@ -1408,9 +1413,14 @@ again:
 return ret;
 }
 
-/** whether we should look for this IFD's SubIFD */
-retry_for_subifd = s->sub_ifd && s->get_subimage;
-/** whether we should look for this multi-page IFD's next page */
+if (s->get_thumbnail && !s->is_thumbnail) {
+av_log(avctx, AV_LOG_INFO, "No embedded thumbnail present\n");
+return AVERROR_EOF;
+}
+
+/** whether we should process this IFD's SubIFD */
+retry_for_subifd = s->sub_ifd && (s->get_subimage || (!s->get_thumbnail && 
s->is_thumbnail));
+/** whether we should process this multi-page IFD's next page */
 retry_for_page = s->get_page && s->cur_page + 1 < s->get_page;  // 
get_page is 1-indexed
 
 if (retry_for_page) {
@@ -1670,6 +1680,7 @@ static av_cold int tiff_end(AVCodecContext *avctx)
 #define OFFSET(x) offsetof(TiffContext, x)
 static const AVOption tiff_options[] = {
 { "subimage", "decode subimage instead if available", 
OFFSET(get_subimage), AV_OPT_TYPE_BOOL, {.i64=0},  0, 1, 
AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
+{ "thumbnail", "decode embedded thumbnail subimage instead if available", 
OFFSET(get_thumbnail), AV_OPT_TYPE_BOOL, {.i64=0},  0, 1, 
AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
 { "page", "page number of multi-page image to decode (starting from 1)", 
OFFSET(get_page), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, 
AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
 { NULL },
 };
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v2 2/3] libavcodec/tiff: Process SubIFDs tag with multiple entries

2019-05-30 Thread velocityra
From: Nick Renieris 

SubIFDs that were part of more than single-sized "SubIFDs" tags were
being ignored due to existing code ignoring that case.

This patch makes is so the first entry is read, which is not ideal
but enough for some DNG images present in the wild to be decodeable
More specifically, the first SubIFD which we would process with this
patch is the main image and the second one is a second thumbnail,
which is not as important to decode.

In DNG images with the .tiff extension, it solves the issue where
the TIFF thumbnail in IFD 0 was incorrectly parsed (related
confusion: [1]).

Embedded thumbnails for DNG images can still be decoded with the
"-thumbnail" option.

Related to ticket: https://trac.ffmpeg.org/ticket/4364

[1]: 
https://superuser.com/questions/546879/creating-video-from-dng-images-with-ffmpeg

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 6e15d00310..3a3f68fa94 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -1090,7 +1090,10 @@ static int tiff_decode_tag(TiffContext *s, AVFrame 
*frame)
 s->predictor = value;
 break;
 case TIFF_SUB_IFDS:
-s->sub_ifd = value;
+if (count == 1)
+s->sub_ifd = value;
+else if (count > 1)
+s->sub_ifd = ff_tget(&s->gb, TIFF_LONG, s->le); /** Only get the 
first SubIFD */
 break;
 case TIFF_WHITE_LEVEL:
 s->white_level = value;
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v2 3/3] avcodec/tiff: Recognize DNG/CinemaDNG images

2019-05-30 Thread velocityra
From: Nick Renieris 

Additionally:
- Renamed TIFF_WHITE_LEVEL to DNG_WHITE_LEVEL since it is specified
  in the DNG spec.
- Added/changed some comments to be more precise in differentiating
  between TIFF, TIFF/EP and DNG values.

Related to ticket: https://trac.ffmpeg.org/ticket/4364

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c  | 30 +-
 libavcodec/tiff.h  | 36 +++-
 libavformat/img2.c |  1 +
 3 files changed, 61 insertions(+), 6 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 3a3f68fa94..c520d7df83 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -58,6 +58,7 @@ typedef struct TiffContext {
 uint16_t get_page;
 int get_thumbnail;
 
+enum TiffType tiff_type;
 int width, height;
 unsigned int bpp, bppcount;
 uint32_t palette[256];
@@ -96,6 +97,11 @@ typedef struct TiffContext {
 TiffGeoTag *geotags;
 } TiffContext;
 
+static void tiff_set_type(TiffContext *s, enum TiffType tiff_type) {
+if (s->tiff_type < tiff_type) // Prioritize higher-valued entries
+s->tiff_type = tiff_type;
+}
+
 static void free_geotags(TiffContext *const s)
 {
 int i;
@@ -1095,7 +1101,7 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
 else if (count > 1)
 s->sub_ifd = ff_tget(&s->gb, TIFF_LONG, s->le); /** Only get the 
first SubIFD */
 break;
-case TIFF_WHITE_LEVEL:
+case DNG_WHITE_LEVEL:
 s->white_level = value;
 break;
 case TIFF_CFA_PATTERN_DIM:
@@ -1346,6 +1352,27 @@ static int tiff_decode_tag(TiffContext *s, AVFrame 
*frame)
 case TIFF_SOFTWARE_NAME:
 ADD_METADATA(count, "software", NULL);
 break;
+case DNG_VERSION:
+if (count == 4) {
+unsigned int ver[4];
+ver[0] = ff_tget(&s->gb, type, s->le);
+ver[1] = ff_tget(&s->gb, type, s->le);
+ver[2] = ff_tget(&s->gb, type, s->le);
+ver[3] = ff_tget(&s->gb, type, s->le);
+
+av_log(s->avctx, AV_LOG_DEBUG, "DNG file, version %u.%u.%u.%u\n",
+ver[0], ver[1], ver[2], ver[3]);
+
+tiff_set_type(s, TIFF_TYPE_DNG);
+}
+break;
+case CINEMADNG_TIME_CODES:
+case CINEMADNG_FRAME_RATE:
+case CINEMADNG_T_STOP:
+case CINEMADNG_REEL_NAME:
+case CINEMADNG_CAMERA_LABEL:
+tiff_set_type(s, TIFF_TYPE_CINEMADNG);
+break;
 default:
 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
 av_log(s->avctx, AV_LOG_ERROR,
@@ -1402,6 +1429,7 @@ again:
 s->white_level = 0;
 s->is_bayer= 0;
 s->cur_page= 0;
+s->tiff_type   = TIFF_TYPE_TIFF;
 free_geotags(s);
 
 // Reset these offsets so we can tell if they were set this frame
diff --git a/libavcodec/tiff.h b/libavcodec/tiff.h
index 4b08650108..81913c6b1a 100644
--- a/libavcodec/tiff.h
+++ b/libavcodec/tiff.h
@@ -20,7 +20,7 @@
 
 /**
  * @file
- * TIFF tables
+ * TIFF constants & data structures
  *
  * For more information about the TIFF format, check the official docs at:
  * http://partners.adobe.com/public/developer/tiff/index.html
@@ -33,7 +33,17 @@
 #include 
 #include "tiff_common.h"
 
-/** abridged list of TIFF tags */
+/** TIFF types in ascenting priority (last in the list is highest) */
+enum TiffType {
+/** TIFF image based on the TIFF 6.0 or TIFF/EP (ISO 12234-2) 
specifications */
+TIFF_TYPE_TIFF,
+/** Digital Negative (DNG) image */
+TIFF_TYPE_DNG,
+/** Digital Negative (DNG) image part of an CinemaDNG image sequence */
+TIFF_TYPE_CINEMADNG,
+};
+
+/** abridged list of TIFF and TIFF/EP tags */
 enum TiffTags {
 TIFF_SUBFILE= 0xfe,
 TIFF_WIDTH  = 0x100,
@@ -85,10 +95,25 @@ enum TiffTags {
 TIFF_GEO_KEY_DIRECTORY  = 0x87AF,
 TIFF_GEO_DOUBLE_PARAMS  = 0x87B0,
 TIFF_GEO_ASCII_PARAMS   = 0x87B1,
-TIFF_WHITE_LEVEL= 0xC61D,
 };
 
-/** list of TIFF compression types */
+/** abridged list of DNG tags */
+enum DngTags {
+DNG_VERSION = 0xC612,
+DNG_BACKWARD_VERSION= 0xC613,
+DNG_WHITE_LEVEL = 0xC61D,
+};
+
+/** list of CinemaDNG tags */
+enum CinemaDngTags {
+CINEMADNG_TIME_CODES= 0xC763,
+CINEMADNG_FRAME_RATE= 0xC764,
+CINEMADNG_T_STOP= 0xC772,
+CINEMADNG_REEL_NAME = 0xC789,
+CINEMADNG_CAMERA_LABEL  = 0xC7A1,
+};
+
+/** list of TIFF, TIFF/EP and DNG compression types */
 enum TiffCompr {
 TIFF_RAW = 1,
 TIFF_CCITT_RLE,
@@ -151,6 +176,7 @@ enum TiffGeoTagKey {
 TIFF_VERTICAL_UNITS_GEOKEY   = 4099
 };
 
+/** list of TIFF, TIFF/AP and DNG PhotometricInterpretation (TIFF_PHOTOMETRIC) 
values */
 enum TiffPhotometric {
 TIFF_PHOTOMETRIC_NONE   = -1,
 TIFF_PHOTOMETRIC_WHITE_IS_ZERO,  /* mono or grayscale, 0 is white */
@@ -163,7 +189,7 @@ enum TiffPhotometric {
 TIFF_PHOTOMETRIC_CIE_LAB= 8, /* 1976 CIE L*a*b* */
 TIFF_P

[FFmpeg-devel] [PATCH] avcodec/tiff: Add support for recognizing DNG/CinemaDNG files

2019-05-29 Thread velocityra
From: Nick Renieris 

In DNG images with the .tiff extension, it solves the issue where the
TIFF thumbnail in IFD 0 was incorrectly parsed (related confusion: [1]).
Embedded thumbnails for DNG images can still be decoded with the added
"-dng_thumb" option.

Maintains current support for CinemaDNG (at least the ones found
in the wild) by detecting them and not erroring out in that case.

Additionally:
 - Renamed TIFF_WHITE_LEVEL to DNG_WHITE_LEVEL since it is specified
   in the DNG spec.
 - Added/changed some comments to be more precise in differentiating
   between TIFF, TIFF/EP and DNG values.

Related to ticket: https://trac.ffmpeg.org/ticket/4364


[1]: 
https://superuser.com/questions/546879/creating-video-from-dng-images-with-ffmpeg

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c  | 48 +-
 libavcodec/tiff.h  | 36 +-
 libavformat/img2.c |  1 +
 3 files changed, 79 insertions(+), 6 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 79e6242549..23d979eada 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -56,7 +56,9 @@ typedef struct TiffContext {
 
 int get_subimage;
 uint16_t get_page;
+int get_dng_thumb;
 
+enum TiffType tiff_type;
 int width, height;
 unsigned int bpp, bppcount;
 uint32_t palette[256];
@@ -70,6 +72,7 @@ typedef struct TiffContext {
 int predictor;
 int fill_order;
 uint32_t res[4];
+int is_thumbnail;
 
 int is_bayer;
 uint8_t pattern[4];
@@ -94,6 +97,11 @@ typedef struct TiffContext {
 TiffGeoTag *geotags;
 } TiffContext;
 
+static void tiff_set_type(TiffContext *s, enum TiffType tiff_type) {
+if (s->tiff_type < tiff_type) // Prioritize higher-valued entries
+s->tiff_type = tiff_type;
+}
+
 static void free_geotags(TiffContext *const s)
 {
 int i;
@@ -948,6 +956,8 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
 }
 
 switch (tag) {
+case TIFF_SUBFILE:
+s->is_thumbnail = (value != 0);
 case TIFF_WIDTH:
 s->width = value;
 break;
@@ -1088,7 +1098,7 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
 case TIFF_SUB_IFDS:
 s->sub_ifd = value;
 break;
-case TIFF_WHITE_LEVEL:
+case DNG_WHITE_LEVEL:
 s->white_level = value;
 break;
 case TIFF_CFA_PATTERN_DIM:
@@ -1339,6 +1349,27 @@ static int tiff_decode_tag(TiffContext *s, AVFrame 
*frame)
 case TIFF_SOFTWARE_NAME:
 ADD_METADATA(count, "software", NULL);
 break;
+case DNG_VERSION:
+if (count == 4) {
+unsigned int ver[4];
+ver[0] = ff_tget(&s->gb, type, s->le);
+ver[1] = ff_tget(&s->gb, type, s->le);
+ver[2] = ff_tget(&s->gb, type, s->le);
+ver[3] = ff_tget(&s->gb, type, s->le);
+
+av_log(s->avctx, AV_LOG_DEBUG, "DNG file, version %u.%u.%u.%u\n",
+ver[0], ver[1], ver[2], ver[3]);
+
+tiff_set_type(s, TIFF_TYPE_DNG);
+}
+break;
+case CINEMADNG_TIME_CODES:
+case CINEMADNG_FRAME_RATE:
+case CINEMADNG_T_STOP:
+case CINEMADNG_REEL_NAME:
+case CINEMADNG_CAMERA_LABEL:
+tiff_set_type(s, TIFF_TYPE_CINEMADNG);
+break;
 default:
 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
 av_log(s->avctx, AV_LOG_ERROR,
@@ -1387,6 +1418,7 @@ static int decode_frame(AVCodecContext *avctx,
 s->le  = le;
 // TIFF_BPP is not a required tag and defaults to 1
 again:
+s->is_thumbnail = 0;
 s->bppcount= s->bpp = 1;
 s->photometric = TIFF_PHOTOMETRIC_NONE;
 s->compr   = TIFF_RAW;
@@ -1394,6 +1426,7 @@ again:
 s->white_level = 0;
 s->is_bayer= 0;
 s->cur_page= 0;
+s->tiff_type   = TIFF_TYPE_TIFF;
 free_geotags(s);
 
 // Reset these offsets so we can tell if they were set this frame
@@ -1408,6 +1441,18 @@ again:
 return ret;
 }
 
+if (s->tiff_type == TIFF_TYPE_DNG) {
+if (!s->get_dng_thumb) {
+av_log(avctx, AV_LOG_ERROR, "DNG images are not supported\n");
+av_log(avctx, AV_LOG_INFO, "You can use -dng_thumb to decode an 
embedded TIFF thumbnail (if any) instead\n");
+return AVERROR_INVALIDDATA;
+}
+if (!s->is_thumbnail) {
+av_log(avctx, AV_LOG_INFO, "No embedded thumbnail present\n");
+return AVERROR_EOF;
+}
+}
+
 /** whether we should look for this IFD's SubIFD */
 retry_for_subifd = s->sub_ifd && s->get_subimage;
 /** whether we should look for this multi-page IFD's next page */
@@ -1671,6 +1716,7 @@ static av_cold int tiff_end(AVCodecContext *avctx)
 static const AVOption tiff_options[] = {
 { "subimage", "decode subimage instead if available", 
OFFSET(get_subimage), AV_OPT_TYPE_BOOL, {.i64=0},  0, 1, 
AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
 { "page", "page

[FFmpeg-devel] [PATCH] avcodec/tiff: Add support for recognizing DNG files

2019-05-28 Thread velocityra
From: Nick Renieris 

In DNG images with the .tiff extension, it solves the issue where the
TIFF thumbnail in IFD 0 was incorrectly parsed (related confusion: [1]).
Embedded thumbnails for DNG images can still be decoded with the added
"-dng_thumb" option.

Additionally:
 - Renamed TIFF_WHITE_LEVEL to DNG_WHITE_LEVEL since it is specified
   in the DNG spec.
 - Added/changed some comments to be more precise in differentiating
   between TIFF, TIFF/EP and DNG values.

Related to ticket: https://trac.ffmpeg.org/ticket/4364

---

[1]: 
https://superuser.com/questions/546879/creating-video-from-dng-images-with-ffmpeg

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c  | 36 +++-
 libavcodec/tiff.h  | 18 +-
 libavformat/img2.c |  1 +
 3 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 79e6242549..9575fe92ce 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -56,6 +56,7 @@ typedef struct TiffContext {
 
 int get_subimage;
 uint16_t get_page;
+int get_dng_thumb;
 
 int width, height;
 unsigned int bpp, bppcount;
@@ -70,7 +71,9 @@ typedef struct TiffContext {
 int predictor;
 int fill_order;
 uint32_t res[4];
+int is_thumbnail;
 
+int dng_mode; /** denotes that this is a DNG image */
 int is_bayer;
 uint8_t pattern[4];
 unsigned white_level;
@@ -948,6 +951,8 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
 }
 
 switch (tag) {
+case TIFF_SUBFILE:
+s->is_thumbnail = (value != 0);
 case TIFF_WIDTH:
 s->width = value;
 break;
@@ -1088,7 +1093,7 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
 case TIFF_SUB_IFDS:
 s->sub_ifd = value;
 break;
-case TIFF_WHITE_LEVEL:
+case DNG_WHITE_LEVEL:
 s->white_level = value;
 break;
 case TIFF_CFA_PATTERN_DIM:
@@ -1339,6 +1344,20 @@ static int tiff_decode_tag(TiffContext *s, AVFrame 
*frame)
 case TIFF_SOFTWARE_NAME:
 ADD_METADATA(count, "software", NULL);
 break;
+case DNG_VERSION:
+if (count == 4) {
+unsigned int ver[4];
+ver[0] = ff_tget(&s->gb, type, s->le);
+ver[1] = ff_tget(&s->gb, type, s->le);
+ver[2] = ff_tget(&s->gb, type, s->le);
+ver[3] = ff_tget(&s->gb, type, s->le);
+
+av_log(s->avctx, AV_LOG_DEBUG, "DNG file, version %u.%u.%u.%u\n",
+ver[0], ver[1], ver[2], ver[3]);
+
+s->dng_mode = 1;
+}
+break;
 default:
 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
 av_log(s->avctx, AV_LOG_ERROR,
@@ -1387,6 +1406,7 @@ static int decode_frame(AVCodecContext *avctx,
 s->le  = le;
 // TIFF_BPP is not a required tag and defaults to 1
 again:
+s->is_thumbnail = 0;
 s->bppcount= s->bpp = 1;
 s->photometric = TIFF_PHOTOMETRIC_NONE;
 s->compr   = TIFF_RAW;
@@ -1394,6 +1414,7 @@ again:
 s->white_level = 0;
 s->is_bayer= 0;
 s->cur_page= 0;
+s->dng_mode= 0;
 free_geotags(s);
 
 // Reset these offsets so we can tell if they were set this frame
@@ -1408,6 +1429,18 @@ again:
 return ret;
 }
 
+if (s->dng_mode) {
+if (!s->get_dng_thumb) {
+av_log(avctx, AV_LOG_ERROR, "DNG images are not supported\n");
+av_log(avctx, AV_LOG_INFO, "You can use -dng_thumb to decode an 
embedded TIFF thumbnail (if any) instead\n");
+return AVERROR_INVALIDDATA;
+}
+if (!s->is_thumbnail) {
+av_log(avctx, AV_LOG_INFO, "No embedded thumbnail present\n");
+return AVERROR_EOF;
+}
+}
+
 /** whether we should look for this IFD's SubIFD */
 retry_for_subifd = s->sub_ifd && s->get_subimage;
 /** whether we should look for this multi-page IFD's next page */
@@ -1671,6 +1704,7 @@ static av_cold int tiff_end(AVCodecContext *avctx)
 static const AVOption tiff_options[] = {
 { "subimage", "decode subimage instead if available", 
OFFSET(get_subimage), AV_OPT_TYPE_BOOL, {.i64=0},  0, 1, 
AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
 { "page", "page number of multi-page image to decode (starting from 1)", 
OFFSET(get_page), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, 
AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
+{ "dng_thumb", "decode TIFF thumbnail of DNG image", 
OFFSET(get_dng_thumb), AV_OPT_TYPE_BOOL, {.i64=0},  0, 1, 
AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
 { NULL },
 };
 
diff --git a/libavcodec/tiff.h b/libavcodec/tiff.h
index 4b08650108..3cede299f6 100644
--- a/libavcodec/tiff.h
+++ b/libavcodec/tiff.h
@@ -20,7 +20,7 @@
 
 /**
  * @file
- * TIFF tables
+ * TIFF constants & data structures
  *
  * For more information about the TIFF format, check the official docs at:
  * http://partners.adobe.com/public/developer/tiff/inde

[FFmpeg-devel] [PATCH] avcodec/tiff: Multi-page support

2019-03-22 Thread velocityra
From: Nick Renieris 

Option "-page N" (page index N starts from 1) can now be used to specify which 
TIFF page/subfile to decode.

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 25 -
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 29a80a6892..9595681839 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -55,6 +55,7 @@ typedef struct TiffContext {
 GetByteContext gb;
 
 int get_subimage;
+uint16_t get_page;
 
 int width, height;
 unsigned int bpp, bppcount;
@@ -75,6 +76,7 @@ typedef struct TiffContext {
 unsigned white_level;
 
 uint32_t sub_ifd;
+uint16_t cur_page;
 
 int strips, rps, sstype;
 int sot;
@@ -1322,6 +1324,12 @@ static int tiff_decode_tag(TiffContext *s, AVFrame 
*frame)
 break;
 case TIFF_PAGE_NUMBER:
 ADD_METADATA(count, "page_number", " / ");
+// need to seek back to re-read the page number
+bytestream2_seek(&s->gb, -count * sizeof(uint16_t), SEEK_CUR);
+// read the page number
+s->cur_page = ff_tget(&s->gb, TIFF_SHORT, s->le);
+// get back to where we were before the previous seek
+bytestream2_seek(&s->gb, count * sizeof(uint16_t) - sizeof(uint16_t), 
SEEK_CUR);
 break;
 case TIFF_SOFTWARE_NAME:
 ADD_METADATA(count, "software", NULL);
@@ -1359,6 +1367,7 @@ static int decode_frame(AVCodecContext *avctx,
 uint8_t *dst;
 GetByteContext stripsizes;
 GetByteContext stripdata;
+int retry_for_subifd, retry_for_page;
 
 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
 
@@ -1379,6 +1388,7 @@ again:
 s->fill_order  = 0;
 s->white_level = 0;
 s->is_bayer= 0;
+s->cur_page= 0;
 free_geotags(s);
 
 // Reset these offsets so we can tell if they were set this frame
@@ -1393,8 +1403,20 @@ again:
 return ret;
 }
 
-if (s->sub_ifd && s->get_subimage) {
+/** whether we should look for this IFD's SubIFD */
+retry_for_subifd = s->sub_ifd && s->get_subimage;
+/** whether we should look for this multi-page IFD's next page */
+retry_for_page = s->get_page && s->cur_page + 1 < s->get_page;  // 
get_page is 1-indexed
+
+if (retry_for_page) {
+// set offset to the next IFD
+off = ff_tget_long(&s->gb, le);
+} else if (retry_for_subifd) {
+// set offset to the SubIFD
 off = s->sub_ifd;
+}
+
+if (retry_for_subifd || retry_for_page) {
 if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image 
size\n");
 return AVERROR_INVALIDDATA;
@@ -1643,6 +1665,7 @@ static av_cold int tiff_end(AVCodecContext *avctx)
 #define OFFSET(x) offsetof(TiffContext, x)
 static const AVOption tiff_options[] = {
 { "subimage", "decode subimage instead if available", 
OFFSET(get_subimage), AV_OPT_TYPE_BOOL, {.i64=0},  0, 1, 
AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
+{ "page", "page number of multi-page image to decode (starting from 1)", 
OFFSET(get_page), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, 
AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
 { NULL },
 };
 
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 1/2] avutil/opt: Add AV_OPT_TYPE_UINT16

2019-03-22 Thread velocityra
From: Nick Renieris 

Signed-off-by: Nick Renieris 
---
 libavutil/opt.c | 29 +++--
 libavutil/opt.h |  1 +
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/libavutil/opt.c b/libavutil/opt.c
index 93d6c26c11..d409f3521a 100644
--- a/libavutil/opt.c
+++ b/libavutil/opt.c
@@ -71,6 +71,9 @@ static int read_number(const AVOption *o, const void *dst, 
double *num, int *den
 case AV_OPT_TYPE_INT:
 *intnum = *(int *)dst;
 return 0;
+case AV_OPT_TYPE_UINT16:
+*intnum = *(uint16_t*)dst;
+return 0;
 case AV_OPT_TYPE_CHANNEL_LAYOUT:
 case AV_OPT_TYPE_DURATION:
 case AV_OPT_TYPE_INT64:
@@ -125,6 +128,13 @@ static int write_number(void *obj, const AVOption *o, void 
*dst, double num, int
 case AV_OPT_TYPE_INT:
 *(int *)dst = llrint(num / den) * intnum;
 break;
+case AV_OPT_TYPE_UINT16:{
+float d = num / den;
+if (intnum == 1 && d == (float)UINT16_MAX) {
+*(int16_t *)dst = INT16_MAX;
+} else
+*(int16_t *)dst = llrint(d) * intnum;
+break;}
 case AV_OPT_TYPE_DURATION:
 case AV_OPT_TYPE_CHANNEL_LAYOUT:
 case AV_OPT_TYPE_INT64:{
@@ -218,8 +228,9 @@ static int set_string(void *obj, const AVOption *o, const 
char *val, uint8_t **d
 return *dst ? 0 : AVERROR(ENOMEM);
 }
 
-#define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_INT64 || \
-  opt->type == AV_OPT_TYPE_UINT64 || \
+#define DEFAULT_NUMVAL(opt) ((opt->type == AV_OPT_TYPE_UINT16 ||\
+  opt->type == AV_OPT_TYPE_UINT64 ||\
+  opt->type == AV_OPT_TYPE_INT64 || \
   opt->type == AV_OPT_TYPE_CONST || \
   opt->type == AV_OPT_TYPE_FLAGS || \
   opt->type == AV_OPT_TYPE_INT) \
@@ -476,6 +487,7 @@ int av_opt_set(void *obj, const char *name, const char 
*val, int search_flags)
 return set_string_binary(obj, o, val, dst);
 case AV_OPT_TYPE_FLAGS:
 case AV_OPT_TYPE_INT:
+case AV_OPT_TYPE_UINT16:
 case AV_OPT_TYPE_INT64:
 case AV_OPT_TYPE_UINT64:
 case AV_OPT_TYPE_FLOAT:
@@ -785,6 +797,9 @@ int av_opt_get(void *obj, const char *name, int 
search_flags, uint8_t **out_val)
 case AV_OPT_TYPE_INT:
 ret = snprintf(buf, sizeof(buf), "%d", *(int *)dst);
 break;
+case AV_OPT_TYPE_UINT16:
+ret = snprintf(buf, sizeof(buf), "%"PRIu16, *(uint16_t *)dst);
+break;
 case AV_OPT_TYPE_INT64:
 ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t *)dst);
 break;
@@ -1136,6 +1151,9 @@ static void opt_list(void *obj, void *av_log_obj, const 
char *unit,
 case AV_OPT_TYPE_INT:
 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
 break;
+case AV_OPT_TYPE_UINT16:
+av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
+break;
 case AV_OPT_TYPE_INT64:
 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
 break;
@@ -1202,6 +1220,7 @@ static void opt_list(void *obj, void *av_log_obj, const 
char *unit,
 if (av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) >= 
0) {
 switch (opt->type) {
 case AV_OPT_TYPE_INT:
+case AV_OPT_TYPE_UINT16:
 case AV_OPT_TYPE_INT64:
 case AV_OPT_TYPE_UINT64:
 case AV_OPT_TYPE_DOUBLE:
@@ -1248,6 +1267,7 @@ static void opt_list(void *obj, void *av_log_obj, const 
char *unit,
 break;
 }
 case AV_OPT_TYPE_INT:
+case AV_OPT_TYPE_UINT16:
 case AV_OPT_TYPE_UINT64:
 case AV_OPT_TYPE_INT64: {
 const char *def_const = get_opt_const_name(obj, opt->unit, 
opt->default_val.i64);
@@ -1326,6 +1346,7 @@ void av_opt_set_defaults2(void *s, int mask, int flags)
 case AV_OPT_TYPE_BOOL:
 case AV_OPT_TYPE_FLAGS:
 case AV_OPT_TYPE_INT:
+case AV_OPT_TYPE_UINT16:
 case AV_OPT_TYPE_INT64:
 case AV_OPT_TYPE_UINT64:
 case AV_OPT_TYPE_DURATION:
@@ -1685,6 +1706,8 @@ static int opt_size(enum AVOptionType type)
 case AV_OPT_TYPE_INT:
 case AV_OPT_TYPE_FLAGS:
 return sizeof(int);
+case AV_OPT_TYPE_UINT16:
+return sizeof(uint16_t);
 case AV_OPT_TYPE_DURATION:
 case AV_OPT_TYPE_CHANNEL_LAYOUT:
 case AV_OPT_TYPE_INT64:
@@ -1817,6 +1840,7 @@ int av_opt_query_ranges_default(AVOptionRanges 
**ranges_arg, void *obj, const ch
 switch (field->type) {
 case AV_OPT_TYPE_BOOL:
 case AV_OPT_TYPE_INT:
+case AV_OPT_TYPE_UINT16:
 case AV_OPT_TYPE_INT64:
 case AV_OPT_TYPE_UINT64:
 case AV_OPT_TYPE_PIXEL_FMT:
@@ -1905,6 +1929,7 @@ int av_opt_is_set_to_default(void *obj, const AVOption *o)
 case AV_OPT_TYPE_PIXEL_FMT:
 case AV_OPT_T

[FFmpeg-devel] [PATCH 2/2] avcodec/tiff: Multi-page support

2019-03-22 Thread velocityra
From: Nick Renieris 

Option "-page N" (page index N starts from 1) can now be used to specify which 
TIFF page/subfile to decode.

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c | 25 -
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 29a80a6892..94ff4bd394 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -55,6 +55,7 @@ typedef struct TiffContext {
 GetByteContext gb;
 
 int get_subimage;
+uint16_t get_page;
 
 int width, height;
 unsigned int bpp, bppcount;
@@ -75,6 +76,7 @@ typedef struct TiffContext {
 unsigned white_level;
 
 uint32_t sub_ifd;
+uint16_t cur_page;
 
 int strips, rps, sstype;
 int sot;
@@ -1322,6 +1324,12 @@ static int tiff_decode_tag(TiffContext *s, AVFrame 
*frame)
 break;
 case TIFF_PAGE_NUMBER:
 ADD_METADATA(count, "page_number", " / ");
+// need to seek back to re-read the page number
+bytestream2_seek(&s->gb, -count * sizeof(uint16_t), SEEK_CUR);
+// read the page number
+s->cur_page = ff_tget(&s->gb, TIFF_SHORT, s->le);
+// get back to where we were before the previous seek
+bytestream2_seek(&s->gb, count * sizeof(uint16_t) - sizeof(uint16_t), 
SEEK_CUR);
 break;
 case TIFF_SOFTWARE_NAME:
 ADD_METADATA(count, "software", NULL);
@@ -1359,6 +1367,7 @@ static int decode_frame(AVCodecContext *avctx,
 uint8_t *dst;
 GetByteContext stripsizes;
 GetByteContext stripdata;
+int retry_for_subifd, retry_for_page;
 
 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
 
@@ -1379,6 +1388,7 @@ again:
 s->fill_order  = 0;
 s->white_level = 0;
 s->is_bayer= 0;
+s->cur_page= 0;
 free_geotags(s);
 
 // Reset these offsets so we can tell if they were set this frame
@@ -1393,8 +1403,20 @@ again:
 return ret;
 }
 
-if (s->sub_ifd && s->get_subimage) {
+/** whether we should look for this IFD's SubIFD */
+retry_for_subifd = s->sub_ifd && s->get_subimage;
+/** whether we should look for this multi-page IFD's next page */
+retry_for_page = s->get_page && s->cur_page + 1 < s->get_page;  // 
get_page is 1-indexed
+
+if (retry_for_page) {
+// set offset to the next IFD
+off = ff_tget_long(&s->gb, le);
+} else if (retry_for_subifd) {
+// set offset to the SubIFD
 off = s->sub_ifd;
+}
+
+if (retry_for_subifd || retry_for_page) {
 if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
 av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image 
size\n");
 return AVERROR_INVALIDDATA;
@@ -1643,6 +1665,7 @@ static av_cold int tiff_end(AVCodecContext *avctx)
 #define OFFSET(x) offsetof(TiffContext, x)
 static const AVOption tiff_options[] = {
 { "subimage", "decode subimage instead if available", 
OFFSET(get_subimage), AV_OPT_TYPE_BOOL, {.i64=0},  0, 1, 
AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
+{ "page", "page number of multi-page image to decode (starting from 1)", 
OFFSET(get_page), AV_OPT_TYPE_UINT16, {.i64=0}, 0, UINT16_MAX, 
AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
 { NULL },
 };
 
-- 
2.21.0.windows.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH] avcodec/tiff: Add support for recognizing DNG files

2019-03-17 Thread velocityra
From: Nick Renieris 

Prints "DNG images are not supported" if it finds a TIFF image with
the 'DNGVersion' tag.  In DNG images with the .tiff extension, it
solves the issue where the TIFF thumbnail in IFD 0 was incorrectly
parsed (related confusion: [1]).  The user can still decode those
with the -subimage option.  Also prints the DNG version of the file
on the debug channel.

Additionally:
 - Renamed TIFF_WHITE_LEVEL to DNG_WHITE_LEVEL since it is specified
   in the DNG spec.
 - Added/changed some comments to be more precise in differentiating
   between TIFF, TIFF/EP and DNG values.

Related to ticket: https://trac.ffmpeg.org/ticket/4364

---

[1]: 
https://superuser.com/questions/546879/creating-video-from-dng-images-with-ffmpeg

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c  | 29 -
 libavcodec/tiff.h  | 18 +-
 libavformat/img2.c |  1 +
 3 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 112f5b52f4..41c63900fd 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -70,6 +70,7 @@ typedef struct TiffContext {
 int fill_order;
 uint32_t res[4];
 
+int dng_mode; /** denotes that this is a DNG image */
 int is_bayer;
 uint8_t pattern[4];
 unsigned white_level;
@@ -1077,7 +1078,7 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
 case TIFF_SUB_IFDS:
 s->sub_ifd = value;
 break;
-case TIFF_WHITE_LEVEL:
+case DNG_WHITE_LEVEL:
 s->white_level = value;
 break;
 case TIFF_CFA_PATTERN_DIM:
@@ -1322,6 +1323,20 @@ static int tiff_decode_tag(TiffContext *s, AVFrame 
*frame)
 case TIFF_SOFTWARE_NAME:
 ADD_METADATA(count, "software", NULL);
 break;
+case DNG_VERSION:
+if (count == 4) {
+unsigned int ver[4];
+ver[0] = ff_tget(&s->gb, type, s->le);
+ver[1] = ff_tget(&s->gb, type, s->le);
+ver[2] = ff_tget(&s->gb, type, s->le);
+ver[3] = ff_tget(&s->gb, type, s->le);
+
+av_log(s->avctx, AV_LOG_DEBUG, "DNG file, version %u.%u.%u.%u\n",
+ver[0], ver[1], ver[2], ver[3]);
+
+s->dng_mode = 1;
+}
+break;
 default:
 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
 av_log(s->avctx, AV_LOG_ERROR,
@@ -1375,6 +1390,7 @@ again:
 s->fill_order  = 0;
 s->white_level = 0;
 s->is_bayer= 0;
+s->dng_mode= 0;
 free_geotags(s);
 
 // Reset these offsets so we can tell if they were set this frame
@@ -1389,6 +1405,17 @@ again:
 return ret;
 }
 
+if (s->dng_mode) {
+av_log(avctx, AV_LOG_ERROR, "DNG images are not supported\n");
+
+if (s->get_subimage) {
+av_log(avctx, AV_LOG_INFO, "Decoding embedded TIFF thumbnail in 
DNG image\n");
+} else {
+av_log(avctx, AV_LOG_INFO, "Consider using -subimage for decoding 
the embedded thumbail instead\n");
+return AVERROR_INVALIDDATA;
+}
+}
+
 if (s->sub_ifd && s->get_subimage) {
 off = s->sub_ifd;
 if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
diff --git a/libavcodec/tiff.h b/libavcodec/tiff.h
index 4b08650108..3cede299f6 100644
--- a/libavcodec/tiff.h
+++ b/libavcodec/tiff.h
@@ -20,7 +20,7 @@
 
 /**
  * @file
- * TIFF tables
+ * TIFF constants & data structures
  *
  * For more information about the TIFF format, check the official docs at:
  * http://partners.adobe.com/public/developer/tiff/index.html
@@ -33,7 +33,7 @@
 #include 
 #include "tiff_common.h"
 
-/** abridged list of TIFF tags */
+/** abridged list of TIFF and TIFF/EP tags */
 enum TiffTags {
 TIFF_SUBFILE= 0xfe,
 TIFF_WIDTH  = 0x100,
@@ -85,10 +85,17 @@ enum TiffTags {
 TIFF_GEO_KEY_DIRECTORY  = 0x87AF,
 TIFF_GEO_DOUBLE_PARAMS  = 0x87B0,
 TIFF_GEO_ASCII_PARAMS   = 0x87B1,
-TIFF_WHITE_LEVEL= 0xC61D,
 };
 
-/** list of TIFF compression types */
+/** abridged list of DNG tags */
+enum DngTags
+{
+DNG_VERSION = 0xC612,
+DNG_BACKWARD_VERSION= 0xC613,
+DNG_WHITE_LEVEL = 0xC61D,
+};
+
+/** list of TIFF, TIFF/EP and DNG compression types */
 enum TiffCompr {
 TIFF_RAW = 1,
 TIFF_CCITT_RLE,
@@ -151,6 +158,7 @@ enum TiffGeoTagKey {
 TIFF_VERTICAL_UNITS_GEOKEY   = 4099
 };
 
+/** list of TIFF, TIFF/AP and DNG PhotometricInterpretation (TIFF_PHOTOMETRIC) 
values */
 enum TiffPhotometric {
 TIFF_PHOTOMETRIC_NONE   = -1,
 TIFF_PHOTOMETRIC_WHITE_IS_ZERO,  /* mono or grayscale, 0 is white */
@@ -163,7 +171,7 @@ enum TiffPhotometric {
 TIFF_PHOTOMETRIC_CIE_LAB= 8, /* 1976 CIE L*a*b* */
 TIFF_PHOTOMETRIC_ICC_LAB,/* ICC L*a*b* */
 TIFF_PHOTOMETRIC_ITU_LAB,/* ITU L*a*b* */
-TIFF_PHOTOMETRIC_CFA= 32803, /* Color Filter Array (DNG) */
+TIFF_PHOTOMETRIC_CFA= 

[FFmpeg-devel] [PATCH] avcodec/tiff: Add support for recognizing DNG files

2019-03-17 Thread velocityra
From: Nick Renieris 

Prints "DNG images are not supported" if it finds a TIFF image with
the 'DNGVersion' tag.  In DNG images with the .tiff extension it,
solves the issue where the TIFF thumbnail in IFD 0 was incorrectly
parsed (related confusion: [1]).  Adds an option (-dng_thumbnail)
for dumping TIFF thumbnails, should the user want to.  Also prints
the DNG version of the file on the debug channel.

Additionally:
 - Renamed TIFF_WHITE_LEVEL to DNG_WHITE_LEVEL since it is specified
   in the DNG spec.
 - Added/changed some comments to be more precise in differentiating
   between TIFF, TIFF/EP and DNG values.

Related to ticket: https://trac.ffmpeg.org/ticket/4364

---

[1]: 
https://superuser.com/questions/546879/creating-video-from-dng-images-with-ffmpeg

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c  | 29 -
 libavcodec/tiff.h  | 18 +-
 libavformat/img2.c |  1 +
 3 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 112f5b52f4..341c280736 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -54,7 +54,9 @@ typedef struct TiffContext {
 AVCodecContext *avctx;
 GetByteContext gb;
 
+/** options */
 int get_subimage;
+int get_dng_thumbnail;
 
 int width, height;
 unsigned int bpp, bppcount;
@@ -70,6 +72,7 @@ typedef struct TiffContext {
 int fill_order;
 uint32_t res[4];
 
+int dng_mode; /** denotes that this is a DNG image */
 int is_bayer;
 uint8_t pattern[4];
 unsigned white_level;
@@ -1077,7 +1080,7 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
 case TIFF_SUB_IFDS:
 s->sub_ifd = value;
 break;
-case TIFF_WHITE_LEVEL:
+case DNG_WHITE_LEVEL:
 s->white_level = value;
 break;
 case TIFF_CFA_PATTERN_DIM:
@@ -1322,6 +1325,20 @@ static int tiff_decode_tag(TiffContext *s, AVFrame 
*frame)
 case TIFF_SOFTWARE_NAME:
 ADD_METADATA(count, "software", NULL);
 break;
+case DNG_VERSION:
+if (count == 4) {
+unsigned int ver[4];
+ver[0] = ff_tget(&s->gb, type, s->le);
+ver[1] = ff_tget(&s->gb, type, s->le);
+ver[2] = ff_tget(&s->gb, type, s->le);
+ver[3] = ff_tget(&s->gb, type, s->le);
+
+av_log(s->avctx, AV_LOG_DEBUG, "DNG file, version %u.%u.%u.%u\n",
+ver[0], ver[1], ver[2], ver[3]);
+
+s->dng_mode = 1;
+}
+break;
 default:
 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
 av_log(s->avctx, AV_LOG_ERROR,
@@ -1375,6 +1392,7 @@ again:
 s->fill_order  = 0;
 s->white_level = 0;
 s->is_bayer= 0;
+s->dng_mode= 0;
 free_geotags(s);
 
 // Reset these offsets so we can tell if they were set this frame
@@ -1389,6 +1407,14 @@ again:
 return ret;
 }
 
+if (s->dng_mode) {
+av_log(avctx, AV_LOG_ERROR, "DNG images are not supported\n");
+av_log(avctx, AV_LOG_INFO, "Consider using -dng_thumbnail for decoding 
the embedded thumbail instead\n");
+
+if (!s->get_dng_thumbnail)
+return AVERROR_PATCHWELCOME;
+}
+
 if (s->sub_ifd && s->get_subimage) {
 off = s->sub_ifd;
 if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
@@ -1607,6 +1633,7 @@ static av_cold int tiff_end(AVCodecContext *avctx)
 #define OFFSET(x) offsetof(TiffContext, x)
 static const AVOption tiff_options[] = {
 { "subimage", "decode subimage instead if available", 
OFFSET(get_subimage), AV_OPT_TYPE_BOOL, {.i64=0},  0, 1, 
AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
+{ "dng_thumbnail", "decode embedded thumbnail for DNG images, if 
available", OFFSET(get_dng_thumbnail), AV_OPT_TYPE_BOOL, {.i64=0},  0, 1, 
AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM },
 { NULL },
 };
 
diff --git a/libavcodec/tiff.h b/libavcodec/tiff.h
index 4b08650108..3cede299f6 100644
--- a/libavcodec/tiff.h
+++ b/libavcodec/tiff.h
@@ -20,7 +20,7 @@
 
 /**
  * @file
- * TIFF tables
+ * TIFF constants & data structures
  *
  * For more information about the TIFF format, check the official docs at:
  * http://partners.adobe.com/public/developer/tiff/index.html
@@ -33,7 +33,7 @@
 #include 
 #include "tiff_common.h"
 
-/** abridged list of TIFF tags */
+/** abridged list of TIFF and TIFF/EP tags */
 enum TiffTags {
 TIFF_SUBFILE= 0xfe,
 TIFF_WIDTH  = 0x100,
@@ -85,10 +85,17 @@ enum TiffTags {
 TIFF_GEO_KEY_DIRECTORY  = 0x87AF,
 TIFF_GEO_DOUBLE_PARAMS  = 0x87B0,
 TIFF_GEO_ASCII_PARAMS   = 0x87B1,
-TIFF_WHITE_LEVEL= 0xC61D,
 };
 
-/** list of TIFF compression types */
+/** abridged list of DNG tags */
+enum DngTags
+{
+DNG_VERSION = 0xC612,
+DNG_BACKWARD_VERSION= 0xC613,
+DNG_WHITE_LEVEL = 0xC61D,
+};
+
+/** list of TIFF, TIFF/EP and DNG compression types */
 enum TiffCompr {
 TI

[FFmpeg-devel] [PATCH] avcodec/tiff: Add support for recognizing DNG files

2019-03-17 Thread velocityra
From: Nick Renieris 

Prints "DNG images are not supported" if it finds a TIFF image with
the 'DNGVersion' tag.  In DNG images with the .tiff extension it,
solves the issue where the TIFF thumbnail in IFD 0 was incorrectly
parsed (related confusion: [1]).  Also prints the DNG version of the
file on the debug channel.

Additionally:
 - Renamed TIFF_WHITE_LEVEL to DNG_WHITE_LEVEL since it is specified
   in the DNG spec.
 - Added/changed some comments to be more precise in differentiating
   between TIFF, TIFF/EP and DNG values.

Related to ticket: https://trac.ffmpeg.org/ticket/4364

---

[1]: 
https://superuser.com/questions/546879/creating-video-from-dng-images-with-ffmpeg

Signed-off-by: Nick Renieris 
---
 libavcodec/tiff.c  | 25 +++--
 libavcodec/tiff.h  | 18 +-
 libavformat/img2.c |  1 +
 3 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 112f5b52f4..95d00930ce 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -70,6 +70,7 @@ typedef struct TiffContext {
 int fill_order;
 uint32_t res[4];
 
+int dng_mode; /** denotes that this is a DNG image */
 int is_bayer;
 uint8_t pattern[4];
 unsigned white_level;
@@ -1077,7 +1078,7 @@ static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
 case TIFF_SUB_IFDS:
 s->sub_ifd = value;
 break;
-case TIFF_WHITE_LEVEL:
+case DNG_WHITE_LEVEL:
 s->white_level = value;
 break;
 case TIFF_CFA_PATTERN_DIM:
@@ -1322,6 +1323,20 @@ static int tiff_decode_tag(TiffContext *s, AVFrame 
*frame)
 case TIFF_SOFTWARE_NAME:
 ADD_METADATA(count, "software", NULL);
 break;
+case DNG_VERSION:
+if (count == 4) {
+unsigned int ver[4];
+ver[0] = ff_tget(&s->gb, type, s->le);
+ver[1] = ff_tget(&s->gb, type, s->le);
+ver[2] = ff_tget(&s->gb, type, s->le);
+ver[3] = ff_tget(&s->gb, type, s->le);
+
+av_log(s->avctx, AV_LOG_DEBUG, "DNG file, version %u.%u.%u.%u\n",
+ver[0], ver[1], ver[2], ver[3]);
+
+s->dng_mode = 1;
+}
+break;
 default:
 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
 av_log(s->avctx, AV_LOG_ERROR,
@@ -1375,6 +1390,7 @@ again:
 s->fill_order  = 0;
 s->white_level = 0;
 s->is_bayer= 0;
+s->dng_mode= 0;
 free_geotags(s);
 
 // Reset these offsets so we can tell if they were set this frame
@@ -1389,6 +1405,11 @@ again:
 return ret;
 }
 
+if (s->dng_mode) {
+av_log(avctx, AV_LOG_ERROR, "DNG images are not supported\n");
+return AVERROR_PATCHWELCOME;
+}
+
 if (s->sub_ifd && s->get_subimage) {
 off = s->sub_ifd;
 if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {
@@ -1619,7 +1640,7 @@ static const AVClass tiff_decoder_class = {
 
 AVCodec ff_tiff_decoder = {
 .name   = "tiff",
-.long_name  = NULL_IF_CONFIG_SMALL("TIFF image"),
+.long_name  = NULL_IF_CONFIG_SMALL("TIFF / DNG image"),
 .type   = AVMEDIA_TYPE_VIDEO,
 .id = AV_CODEC_ID_TIFF,
 .priv_data_size = sizeof(TiffContext),
diff --git a/libavcodec/tiff.h b/libavcodec/tiff.h
index 4b08650108..3cede299f6 100644
--- a/libavcodec/tiff.h
+++ b/libavcodec/tiff.h
@@ -20,7 +20,7 @@
 
 /**
  * @file
- * TIFF tables
+ * TIFF constants & data structures
  *
  * For more information about the TIFF format, check the official docs at:
  * http://partners.adobe.com/public/developer/tiff/index.html
@@ -33,7 +33,7 @@
 #include 
 #include "tiff_common.h"
 
-/** abridged list of TIFF tags */
+/** abridged list of TIFF and TIFF/EP tags */
 enum TiffTags {
 TIFF_SUBFILE= 0xfe,
 TIFF_WIDTH  = 0x100,
@@ -85,10 +85,17 @@ enum TiffTags {
 TIFF_GEO_KEY_DIRECTORY  = 0x87AF,
 TIFF_GEO_DOUBLE_PARAMS  = 0x87B0,
 TIFF_GEO_ASCII_PARAMS   = 0x87B1,
-TIFF_WHITE_LEVEL= 0xC61D,
 };
 
-/** list of TIFF compression types */
+/** abridged list of DNG tags */
+enum DngTags
+{
+DNG_VERSION = 0xC612,
+DNG_BACKWARD_VERSION= 0xC613,
+DNG_WHITE_LEVEL = 0xC61D,
+};
+
+/** list of TIFF, TIFF/EP and DNG compression types */
 enum TiffCompr {
 TIFF_RAW = 1,
 TIFF_CCITT_RLE,
@@ -151,6 +158,7 @@ enum TiffGeoTagKey {
 TIFF_VERTICAL_UNITS_GEOKEY   = 4099
 };
 
+/** list of TIFF, TIFF/AP and DNG PhotometricInterpretation (TIFF_PHOTOMETRIC) 
values */
 enum TiffPhotometric {
 TIFF_PHOTOMETRIC_NONE   = -1,
 TIFF_PHOTOMETRIC_WHITE_IS_ZERO,  /* mono or grayscale, 0 is white */
@@ -163,7 +171,7 @@ enum TiffPhotometric {
 TIFF_PHOTOMETRIC_CIE_LAB= 8, /* 1976 CIE L*a*b* */
 TIFF_PHOTOMETRIC_ICC_LAB,/* ICC L*a*b* */
 TIFF_PHOTOMETRIC_ITU_LAB,/* ITU L*a*b* */
-TIFF_PHOTOMETRIC_CFA= 32803, /* Color Filter Array (D