From patchwork Wed May 29 12:57:27 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: 13326 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 F338B448BF9 for ; Wed, 29 May 2019 16:06:20 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id D749868A742; Wed, 29 May 2019 16:06:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 166F668A741 for ; Wed, 29 May 2019 16:06:19 +0300 (EEST) Received: by mail-wm1-f52.google.com with SMTP id v22so1578678wml.1 for ; Wed, 29 May 2019 06:06:19 -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=Odj62t93syKwfWqbqYhUfv4OQ4kznp/0Yg8QYpNfZEQ=; b=Oi2gjdX17lan/NXcMlXJcWChTQeoWtkCuPVWjyGGEfHwxDCArJjn/++hWPkMbhfuto xcVuOPlC4Rbe+IsGCEFVzTQuLxO4c3ALfDaROIPWNmSJ0pLtK01AFHaxOz+q0e70EslB us+KHwBLvf7aFCYhrFRyIQerJzRMrWc8t7CO8Tzt4rDt5vaJqYT5LisNo/qiJubBph/m Dp+Eu5T4kcus0AktruFS9dJRsOheRbcJuRzFZ3sNwM28D3ZoLk50zzZ9BLrv/fZUoEej b+7mEUZ6HDnHGuXNN0mZCvVL8C000riIduNqHRxjWlp4CybS9s47No54ew3VPhy6QijN vuNQ== 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=Odj62t93syKwfWqbqYhUfv4OQ4kznp/0Yg8QYpNfZEQ=; b=rxX3GcrFgwY9T596ngjfSRH9Sd2ee/1Xz5mDTFd/NigfVL6CbWIr6Nn9Gan2tVsegz ME6qNoCX4Vnf0RU/ymr+W/hEzEZzfg8CQhcyaATTsFjJm/DOi48e/ujHnyUBCxydsgi/ fN2sx2Z5ft0CV5C8LsZiy0u6gRPkH4fvgf7IFTEXV1GG64Qdq6E4pRRFKR9OHSKNYnp8 sYfyWcLylKMTTScbRXMTrgyvGpykscoYv23lnGNGKzZ4eBRpDEAEBHdViZMq325+sFAd lduJyzKoMzz3o2fB1W2zc3kNhflf/QnQ4VB4CZuqOGfIfTKF5r8zTuzaLd3H9wgS37zf +KnA== X-Gm-Message-State: APjAAAXsEMGF9p7JpQK5kxcOqIZ1sTkUBiGzjG9t6ero1ERG40YLZd+Z 5/Slb+gbva6/+sdD1itIPr9SUd4u7Tk= X-Google-Smtp-Source: APXvYqz4LkEtjQOhomvLE545FFKn/wD1JOjOtq5vN2ATTaevJGbIu6WVrBnIkHODNxFiy6CO/LP5UQ== X-Received: by 2002:a05:600c:1051:: with SMTP id 17mr6940776wmx.10.1559134709110; Wed, 29 May 2019 05:58:29 -0700 (PDT) Received: from localhost.localdomain ([2a02:587:240e:1e00:9def:283b:1001:2c97]) by smtp.gmail.com with ESMTPSA id u205sm6805969wmu.47.2019.05.29.05.58.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 May 2019 05:58:28 -0700 (PDT) From: velocityra@gmail.com X-Google-Original-From: velocityra@gmail To: ffmpeg-devel@ffmpeg.org Date: Wed, 29 May 2019 15:57:27 +0300 Message-Id: <20190529125727.9452-1-velocityra@gmail.com> X-Mailer: git-send-email 2.21.0.windows.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avcodec/tiff: Add support for recognizing DNG/CinemaDNG files 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 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 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..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_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 = 32803, /* Color Filter Array (TIFF/AP and DNG) */ TIFF_PHOTOMETRIC_LOG_L = 32844, /* CIE Log2(L) */ TIFF_PHOTOMETRIC_LOG_LUV, /* CIE Log L*u*v* */ TIFF_PHOTOMETRIC_LINEAR_RAW = 34892, /* Linear Raw (DNG) */ diff --git a/libavformat/img2.c b/libavformat/img2.c index 8432cc0955..16bc9d2abd 100644 --- a/libavformat/img2.c +++ b/libavformat/img2.c @@ -51,6 +51,7 @@ const IdStrMap ff_img_tags[] = { { AV_CODEC_ID_TARGA, "tga" }, { AV_CODEC_ID_TIFF, "tiff" }, { AV_CODEC_ID_TIFF, "tif" }, + { AV_CODEC_ID_TIFF, "dng" }, { AV_CODEC_ID_SGI, "sgi" }, { AV_CODEC_ID_PTX, "ptx" }, { AV_CODEC_ID_PCX, "pcx" },