From patchwork Sat Aug 10 22:19:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: velocityra@gmail.com X-Patchwork-Id: 14396 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id ED44E448F16 for ; Sun, 11 Aug 2019 01:20:21 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D7FAD68AB80; Sun, 11 Aug 2019 01:20:21 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1957968ABCD for ; Sun, 11 Aug 2019 01:20:18 +0300 (EEST) Received: by mail-wm1-f44.google.com with SMTP id f72so8943355wmf.5 for ; Sat, 10 Aug 2019 15:20:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=z+qFyOkM2E1xekhQoWab2a94LCo0Q679WGIyuCjK3qM=; b=gvNnalRub8pM+aCyZumImx+7cYO4ER6a7j4EkVOT+bjwwhjO/UKWycQKvZ0axROp7Y z0/WXQmGQE2qJB3Gb5vq6LVkTZpJKHldpw8kIy6inqh+LgQ56Py0NFZsZ+glp/uzU+KI g014g3cym9po5GNSUV96o+18n6wjjzSC+QqoObKDBffZ0Z1QDNNcn0wVVuwoZooZlhRS GrgiRHQSZbqSv4loaHiHdQt+IlIwR5MZh5Im9Vf78hWyl5tdXogZcx7gWAYg1fbgqwKO RFdvBTRrYn/ddQBh+xFuUyShYMVMkfc1uwBJxRb9AtoVqQwZLJ15jZ45ANIxfnPlW32V /sjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=z+qFyOkM2E1xekhQoWab2a94LCo0Q679WGIyuCjK3qM=; b=msN6nVx2T/KXA9WFphTamuV/fzBdLSv/31SJK/Y58YJCNSGPXIjb3EhwXgo9Sr4GYH vzMxOv/CwJ7GaogVIcGfUVNmLj3HWTCYgTSfwJX4s6wPFDao93/pkCXJOL1UvzbdQ5Vv SmKTsQWH53pkAV8T+m++C4j5huB1zoVXi8swpVTlamXaHKnABF5bueruYFXKi9aKf+0t 7lFzajaXjPHt+DjEF+RgVYKGRjSfDZsoy/g3P8+YEaLsiBqJHpxGBaNBuEKqvHIsxoY6 8ptcOALvChqOJTJJYDFX8w7bz9JJ3RN7YA17OGZ/fE43t3Tp+aYjA8e8YK9QPy+A9K6M DFKA== X-Gm-Message-State: APjAAAVqzlJWUDJnjVzxm4XlvRnm4a0WgZ1GXbvgWu5JXjdrLo/SOV1u s0ItWO3Kn6Bux07jATkHETDiXeR2R9A= X-Google-Smtp-Source: APXvYqwazYxozQT3V75z3W2sVvoZlxT5ljLes7X1bTPpZwEelIzUXA7EufOncw9GZszxFEJUZROP2A== X-Received: by 2002:a1c:61d4:: with SMTP id v203mr17843526wmb.164.1565475618158; Sat, 10 Aug 2019 15:20:18 -0700 (PDT) Received: from localhost.localdomain ([2a02:587:2409:5200:5c0b:c2fa:5389:82dd]) by smtp.gmail.com with ESMTPSA id q20sm40403233wrc.79.2019.08.10.15.20.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 10 Aug 2019 15:20:17 -0700 (PDT) From: Nick Renieris X-Google-Original-From: Nick Renieris To: ffmpeg-devel@ffmpeg.org Date: Sun, 11 Aug 2019 01:19:30 +0300 Message-Id: <20190810221934.44996-10-velocityra@gmail.com> X-Mailer: git-send-email 2.21.0.windows.1 In-Reply-To: <20190810221934.44996-1-velocityra@gmail.com> References: <20190810221934.44996-1-velocityra@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v13 10/14] lavc/tiff: Support decoding of DNGs with single-component JPEGs X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Nick Renieris Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 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 cf8965453c..ecd87c0b2f 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 */ @@ -865,9 +896,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]; @@ -879,6 +919,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);