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

velocityra at gmail.com velocityra at gmail.com
Wed Aug 7 18:27:20 EEST 2019


From: Nick Renieris <velocityra at gmail.com>

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 <velocityra at gmail.com>
---
 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 'frame' (final buffer) */
 
+    /* See dng_blit for explanation */
+    is_single_comp = (s->avctx_mjpeg->width == w * 2 && s->avctx_mjpeg->height == h / 2);
+
     is_u16 = (s->bpp > 8);
     pixel_size = (is_u16 ? sizeof(uint16_t) : sizeof(uint8_t));
 
+    if (is_single_comp && !is_u16) {
+        av_log(s->avctx, AV_LOG_ERROR, "DNGs with bpp <= 8 and 1 component are unsupported\n");
+        av_frame_unref(s->jpgframe);
+        return AVERROR_PATCHWELCOME;
+    }
+
     dst_offset = x + frame->linesize[0] * y / pixel_size;
     dst_data = frame->data[0] + dst_offset * pixel_size;
     src_data = s->jpgframe->data[0];
@@ -869,6 +909,7 @@ static int dng_decode_jpeg_tile(AVCodecContext *avctx, AVFrame *frame,
              s->jpgframe->linesize[0] / pixel_size,
              w,
              h,
+             is_single_comp,
              is_u16);
 
     av_frame_unref(s->jpgframe);
-- 
2.21.0.windows.1



More information about the ffmpeg-devel mailing list