From patchwork Tue Aug 20 13:37:42 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: 14613 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 367E24481F9 for ; Tue, 20 Aug 2019 16:38:35 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2571D68AAD0; Tue, 20 Aug 2019 16:38:35 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9B51568AAA2 for ; Tue, 20 Aug 2019 16:38:30 +0300 (EEST) Received: by mail-wm1-f49.google.com with SMTP id o4so2665360wmh.2 for ; Tue, 20 Aug 2019 06:38:30 -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; bh=sn9i7UGpjZ2nzurYDUbFwfAEMfS6PCD8LlErRD59r48=; b=Vw5Enyal+aC0xfZi3zbMEcqQJPrxiLJYYV3gTAoG92StLpvWvkmpdGMhhPZGiis2bA 0szpI2SooL67akggkJ5xRm/rcM2kSKwrukRzQbR7O9OTK8svoQ3RVHOZe4wvYM2/7V7l enasoJYhyr1Egn9oao8emv1GEI4UiMpiZFXNFUEiouYkfKFs4ThBgZz8jLPUbOrOe0F5 jc4LIxrPNjX5CqUnchi5i7pQVnajES0euBgPaNb9QHFgnonFBoUr7Adqko0+kySjbAXS SGUlwIDUXEsV34iIQQfQS/626QVJJNpxxRtvdxrES8y3z8Oel3+DX3BVIWqasLJNVj9q 8zwQ== 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; bh=sn9i7UGpjZ2nzurYDUbFwfAEMfS6PCD8LlErRD59r48=; b=WgladMUP4GwExymhh+TxaSCHaXb94vvsq2GjqIbRsUrF8XngtGBzhfZKyL+W+0Th5Z CLSmSZcXn+pMraTo8bpPcLwHTHjKaQUBW5DHFZkFuARBVZlyr/85iW7H3aKbjYAhKTD/ A+AaY6mW+c8knvbvHM5+PF9vqha4Qj7BPCa3SaMv4BJ2/kTXFvC94KBfimOgbZ3/GC1f EE1sgh6QII43tEgsMdqCbUxd3aljIPLWGnh6hEl2x6LP2nT42CuzShrKYn68+djEwldM ifBl0/LveVR5XWHhXd5YlkvJ6XyMPyLO3GsQIfE5OgEa8ZH2UzeKz73DspQJdJoWvMoq Z3uQ== X-Gm-Message-State: APjAAAUzASlrRMHIy+mw6/ukfmJ/dkQrG0bJAYvEtLTgzHzF4QUyFEe5 W5RF9XUgKCtW8ESldW5XY6CryGpB X-Google-Smtp-Source: APXvYqyW4/TaLEs1ydsdmFWm1NHOG6aTFJlMi91a2IZZu8mwxpsnNcCfihsTfel+mvtIIWm/hcg/JA== X-Received: by 2002:a05:600c:21c1:: with SMTP id x1mr37275wmj.37.1566308309672; Tue, 20 Aug 2019 06:38:29 -0700 (PDT) Received: from localhost.localdomain (ppp-94-66-223-133.home.otenet.gr. [94.66.223.133]) by smtp.gmail.com with ESMTPSA id t14sm11545882wrs.58.2019.08.20.06.38.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Aug 2019 06:38:29 -0700 (PDT) From: Nick Renieris To: ffmpeg-devel@ffmpeg.org Date: Tue, 20 Aug 2019 16:37:42 +0300 Message-Id: <20190820133747.30983-10-velocityra@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190820133747.30983-1-velocityra@gmail.com> References: <20190811144542.11704-13-velocityra@gmail.com> <20190820133747.30983-1-velocityra@gmail.com> Subject: [FFmpeg-devel] [PATCH v15 10/15] 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" 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 77acf9f838..35234dc05e 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 * s->bppcount, // 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);