From patchwork Wed Aug 7 15:27:20 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: 14295 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 B660B4486BD for ; Wed, 7 Aug 2019 18:45:06 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A39CC68AAF6; Wed, 7 Aug 2019 18:45:06 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A66D268AA75 for ; Wed, 7 Aug 2019 18:45:00 +0300 (EEST) Received: by mail-wr1-f68.google.com with SMTP id 31so91907245wrm.1 for ; Wed, 07 Aug 2019 08:45:00 -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=FxOb+ERzq9A0az9BkGpE8hUSPZFKpez/OjiCV1Fh4zo=; b=I1BiN+hS9ON/JYZDDUCpzzCNDBkODBNPSE6Infno/5/Dj34AXIoSN4bc72MT8yl0BU gZEIZ57RCxz/fflnELBAWYmfiplV2UE7IofEzEKpFYvMonz6jECFNVEC+7ZLpuayp5VN GxCAwQbir7xW+DlqE3vpKtR0LNymLXpRbabWucs/uXDpH1WPsk0jlXmkXEFPzCjFLLLt VdZHoXnaBVz1gZZbqVYAQpfoDaK7P00qA6p5GEhfUFXSQMAiIthTlIFhUtSlvt16X8py /DgUHee1ZyDjSSS7KBZ8mEaFXd/LejIEelYG9i0tNN7qfCv/N4Cb1MTRtW43cOqbkfQN DJTw== 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=FxOb+ERzq9A0az9BkGpE8hUSPZFKpez/OjiCV1Fh4zo=; b=agVw3Fxfa+jNQQ4VPqMieRrBnumFzKC9oXerBHHyDyD1sGpCVWOLUTAgKe6Kxa81lg F7zQYlXq/GMgujVrHon3RpHFi1VQl0CX15NpoYoSdkCDYja8fTTI29mx1LpbG+1qgm7Y keaKrGRGU7PQvKW/Kpxodxotp3LmaqjZK+I1Y7zPgXTxExpvT6f6k0KQIJGgFbhS1b66 VagyT5+Pl+cqFDET7zcVuN4qK1t/QcKvvPDl94mcQypH5ZWSA3n9qv+Wf85uGAmC1sSN jpmYqVS5LV3qiECXApoEGzHInJfMFnAR4ExobOFX0nIg5yF0hDBZCzVbJV3Of5YvhdZc z/OQ== X-Gm-Message-State: APjAAAVgI3irQ3efKtv9NHvuXnepNbfZueevBXfgb7Jhkl1kk2H6GvDV cb+DmRwyjkERgw2bmM4IDAnV5gYL X-Google-Smtp-Source: APXvYqzFqP5B27d3f4KTOwGGAokoIDb4Ek9x3CaRe+hc2oAK1EKeIDsHJmykI7UbHnGHdZqsyGcorQ== X-Received: by 2002:adf:a344:: with SMTP id d4mr11616980wrb.237.1565192316542; Wed, 07 Aug 2019 08:38:36 -0700 (PDT) Received: from localhost.localdomain ([2a02:587:2409:5200:b577:c48f:5aea:bab0]) by smtp.gmail.com with ESMTPSA id 4sm212050375wro.78.2019.08.07.08.38.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 07 Aug 2019 08:38:35 -0700 (PDT) From: velocityra@gmail.com X-Google-Original-From: velocityra@gmail To: ffmpeg-devel@ffmpeg.org Date: Wed, 7 Aug 2019 18:27:20 +0300 Message-Id: <20190807152723.18892-10-velocityra@gmail.com> X-Mailer: git-send-email 2.21.0.windows.1 In-Reply-To: <20190807152723.18892-1-velocityra@gmail.com> References: <20190807152723.18892-1-velocityra@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v10 10/13] 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 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 '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);