From patchwork Tue Mar 30 07:56:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26646 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 043A444BBC7 for ; Tue, 30 Mar 2021 11:11:35 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C8856688179; Tue, 30 Mar 2021 11:11:34 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-oln040092075055.outbound.protection.outlook.com [40.92.75.55]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 521D8680282 for ; Tue, 30 Mar 2021 11:11:28 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=J6wQaz2sSRwem0PPXaH29i/SjUR5sDeKYdyGHPhmCoLhG3/sTRyV4tIjrnNHWu2oR0Hrd3aiAUumwOiWl4IB9eeSys+1Oh0W83BU4Ud8hf11u+KRpTemyfC1Tvmp5vsutWFayyPTV6fV4f46C/oKkOWsHvt/TfZJeuAhwzDLPSAghz+f/EGdYuvOnR8zVkGK43iV0KsMgec1pfyWSZe9k4Tphb/Aq3nwYZ3wsEKz7Heit+xpXkQEXeaDWwtUiSnnGyTTfp5dVPcEz7diUq/slODb4kA15nDJZ3IqumP3H5FoUx51VLjQf8PLm6QuAVc6wAR6NiDIUVBlvSJ7lIfNRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=yES8Ah5+yWUlpdwm1vFCkXPTocbUrmJkniGgYIIJtEQ=; b=Gkv+nAPXT5OBYzNqdzgfb3yDAlICjukheW0LP0zZp63ue+SuIorPLvVNcNgf1JE/lI6URwa9n1K48WwmDJ2CeIHq0kt33z/12TA0rqw8Fy2NrbBTFKWv6KCtuLPrav1rmagjE6rTtZZPTDGp0GrpissAPXunxIcoJ475aX0spLBeTZUZ84AFYpYnkU8Oh64AG+Vulp+zlOaagV845qMO+qaaocUJRPZweOAPDCtFCIe5ntKeB5hDVyGMr8H1mqXqrs0hHKKVHc5nYNsSSfCVHo2T+b66vB7UwGOkROcjL1NVIVA7bT8CZn3zJtXGjMRNyF4p8uqdkSYkBqHuCJ30eQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=yES8Ah5+yWUlpdwm1vFCkXPTocbUrmJkniGgYIIJtEQ=; b=MGs/HY9hYhzt6M7vRa0EnkUI/FaxPWvu7AzFgccIIfFl/qi0whEd2dA+3CKx/n98KuopsH7ir1LkuA4mcbSYdM6W6XtL+I44HQWZ9uKeGDh0L3s1CB9hPQqYZ/Rk9WuRjRNOuiFKvoI/sXCd5bV9YOa91TIXq+B+HOEk6puDfRWE576/UzNztJUHLbFWwvSGpXjxudjaP/C6lixqo0VYAuV74SYrxDzrHLSWkoKYErmKu4PGpYb9TDMX/ptI0POPfINHm6INS0lItwrW1oBWXQY/j9eEiJiDtNDjrHc+K4qngu26Fa3uwDsVIapGNRbFRsJZvC8PKS+6Be7H4his3w== Received: from DB3EUR04FT054.eop-eur04.prod.protection.outlook.com (2a01:111:e400:7e0c::41) by DB3EUR04HT244.eop-eur04.prod.protection.outlook.com (2a01:111:e400:7e0c::400) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Tue, 30 Mar 2021 07:56:32 +0000 Received: from HE1PR0301MB2154.eurprd03.prod.outlook.com (2a01:111:e400:7e0c::4d) by DB3EUR04FT054.mail.protection.outlook.com (2a01:111:e400:7e0c::100) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Tue, 30 Mar 2021 07:56:32 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:C77A97E5F949AD198A2167050D2B72EC4E18D8FFBE4701FB914A8D7D1F2EDE31; UpperCasedChecksum:EE33C0406CAC4B7AE4D28483DD5391F939B6A4EB34B45FCB24E587115DB97CCF; SizeAsReceived:7327; Count:45 Received: from HE1PR0301MB2154.eurprd03.prod.outlook.com ([fe80::8128:5de5:4e94:9a21]) by HE1PR0301MB2154.eurprd03.prod.outlook.com ([fe80::8128:5de5:4e94:9a21%3]) with mapi id 15.20.3977.033; Tue, 30 Mar 2021 07:56:32 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 30 Mar 2021 09:56:22 +0200 Message-ID: X-Mailer: git-send-email 2.27.0 X-TMN: [foxLJ9ek7wUnlgBQWmzbrBsaGkzvBXQP] X-ClientProxiedBy: AM4PR05CA0003.eurprd05.prod.outlook.com (2603:10a6:205::16) To HE1PR0301MB2154.eurprd03.prod.outlook.com (2603:10a6:3:2a::22) X-Microsoft-Original-Message-ID: <20210330075623.936413-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sblaptop.fritz.box (188.192.137.96) by AM4PR05CA0003.eurprd05.prod.outlook.com (2603:10a6:205::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.24 via Frontend Transport; Tue, 30 Mar 2021 07:56:31 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 45 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: aba6a77a-fef4-44c0-1a7b-08d8f351552a X-MS-Exchange-SLBlob-MailProps: /2cIMDU2oIAk1gbyAs/pT/j4B0DmYXJ83sCTUSoMKo4jBKhxZ03h/vp64zRHzGCYK+oMVzxQOy3FZzGVVW/6GqaWhtYzKHaNfCryoi145tOXWs9yPMEQrQYQpkLUiI9GfH4OOBEkpuxs6zOjbSmPo2zge+f6Euhk+x/6TVQMq7bW4MGp10Fk3RsDpabWNn3f7Mv3mEyhXhUFEgc7dMFMLJOvNi+kG4n15bXy4GePHiBpMIJ5kciNnrkoAw1zv221b2SOEKY1IWqKwtEy+hwotZV/2gBUqD9WYJNDgtfS0DL9QI+SLYRhn/wKz4E8HUcv5w7LDqBYtHS1yV+OCunp85UaohnSWD6JkeDq+fwtp8A9kN0yYn77MVxHP36rWZpMSIq+2tZ+Y6ILQ5kKuo5JSs1enNfScoxgeGhBQdoI483Dssd9kk/VOtPu0OaqrEShJlpskaj8PWxK632G7ciF64x8QWIr5OSLGj57pJh34wzoqeWDgqkeIK79d9US7uJrfRVH5YiVa75QM+UEa9zzK2QnBkPoe7VelVCRNExn4a0zJjVjdNL7TRf50blHLtwmOikCyFTlcAaBqoxYmj+1Bcr0I9NRNl3qqrSa5O1GofjA5nVc6+3Gk+CAsDmraCXx8CtY3Ab2ugr6MJTHTue+LOTamWGFhbTKEQNLifdqw+mCADt8GA/70l6gzheNowiSFR9wREKjr3vvcpjN1alE8lTYqJL//Dnd+eh1srgbF64DyXWMULiPQULqWHyeCouN3S38kRsAM6S2+uUrVhgO86ve5AFQfjNA2okND6iU86kJXgCI398kufmEaYj3eqbCdS9HBBmmpw/O8add63slAyPCtf2Z9mkL/oPlLeUIc2KxsevFSoshObd0wY9cLC0ThlDiUv6Yfpbc1p4dIBe13kZ+yDFSfym8b2Mvq7uywDGS8lkzYQNR7uODrqvuoLvUXIp38h+wv38= X-MS-TrafficTypeDiagnostic: DB3EUR04HT244: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8w46j++4qqsZQXjLCjoYwjnGvse9uZXQWc35WDYqzscnNSkOU6pqFSaOCVL13Km9HvmzOoltmIaEyivRIMYBKQtX3kockRDNejWiIKraxEM9u9JOK24HIcAc659kVfZILBByLNrzhICVCnucEXCi3JF5Z0vbjmWNkzddYEIGMra90kVmCJC6tDYUYofiCRDEPMbS0eOX+eKOPwu7eBlgfeuWMIQc+ni971J4jihfloz6DO44RJsQzYDCWZohM5HYMphjNVsJejNNE4AOcCG2WY4OqTejzN/fvo7OPvWrk3F+WLNQfUQkJDcwaEJ1cPRGhTm2zt7uBAkYynEOiZPqCYBmF7U+CUajEqGqPYGWPSscn23WHLgYNeAmgXXGExBc0HddblJ+N1PilF2GwD8u3rTf6TE2OgcCx6LukX2v5JEjriVQCOJB6vIdgJ0W36wa X-MS-Exchange-AntiSpam-MessageData: ZoOK6tA90MHAXTJU+5dKYGZ7Yc/2cW/1p97M5ESDC65seHRaO2DSrXJ89p6AIkIvKwROAdYUtjDMTQdQtBITcS1qzlelzpRs7DnwQvt8VrI01IuqwYLemdA1+OZG5rr2XqzpNAARXaerVKnhi7pAKA== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: aba6a77a-fef4-44c0-1a7b-08d8f351552a X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2021 07:56:32.2397 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: DB3EUR04FT054.eop-eur04.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: Internet X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB3EUR04HT244 Subject: [FFmpeg-devel] [PATCH 1/2] avcodec/tiff: Avoid forward declarations 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" In this case it also fixes a potential for compilation failures: Not all compilers can handle the case in which a function with a forward declaration declared with an attribute to always inline it is called before the function body appears. Signed-off-by: Andreas Rheinhardt --- This is of course designed as an alternative to Pavel's patch https://ffmpeg.org/pipermail/ffmpeg-devel/2021-March/278383.html libavcodec/tiff.c | 392 +++++++++++++++++++++++----------------------- 1 file changed, 193 insertions(+), 199 deletions(-) diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c index 0878098b90..1d72fdc720 100644 --- a/libavcodec/tiff.c +++ b/libavcodec/tiff.c @@ -275,9 +275,99 @@ static int add_metadata(int count, int type, }; } +/** + * Map stored raw sensor values into linear reference values (see: DNG Specification - Chapter 5) + */ +static uint16_t av_always_inline dng_process_color16(uint16_t value, + const uint16_t *lut, + uint16_t black_level, + float scale_factor) { + float value_norm; + + // Lookup table lookup + if (lut) + value = lut[value]; + + // Black level subtraction + value = av_clip_uint16_c((unsigned)value - black_level); + + // Color scaling + value_norm = (float)value * scale_factor; + + value = av_clip_uint16_c(value_norm * 65535); + + return value; +} + +static uint16_t av_always_inline dng_process_color8(uint16_t value, + const uint16_t *lut, + uint16_t black_level, + float scale_factor) { + return dng_process_color16(value, lut, black_level, scale_factor) >> 8; +} + 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_single_comp, int is_u16); + 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_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); + + dst += dst_stride * sizeof(uint16_t); + src += src_stride * sizeof(uint16_t); + } + } else { + /* 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++) { + uint8_t *dst_u8 = dst; + const uint8_t *src_u8 = src; + + for (col = 0; col < width; col++) + *dst_u8++ = dng_process_color8(*src_u8++, s->dng_lut, s->black_level, scale_factor); + + dst += dst_stride; + src += src_stride; + } + } + } +} static void av_always_inline horizontal_fill(TiffContext *s, unsigned int bpp, uint8_t* dst, @@ -553,7 +643,108 @@ static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride, return ret; } -static int dng_decode_strip(AVCodecContext *avctx, AVFrame *frame); +static int dng_decode_jpeg(AVCodecContext *avctx, AVFrame *frame, + int tile_byte_count, int dst_x, int dst_y, int w, int h) +{ + TiffContext *s = avctx->priv_data; + uint8_t *dst_data, *src_data; + uint32_t dst_offset; /* offset from dst buffer in pixels */ + int is_single_comp, is_u16, pixel_size; + int ret; + + if (tile_byte_count < 0 || tile_byte_count > bytestream2_get_bytes_left(&s->gb)) + return AVERROR_INVALIDDATA; + + /* Prepare a packet and send to the MJPEG decoder */ + av_packet_unref(s->jpkt); + s->jpkt->data = (uint8_t*)s->gb.buffer; + s->jpkt->size = tile_byte_count; + + if (s->is_bayer) { + MJpegDecodeContext *mjpegdecctx = s->avctx_mjpeg->priv_data; + /* We have to set this information here, there is no way to know if a given JPEG is a DNG-embedded + image or not from its own data (and we need that information when decoding it). */ + mjpegdecctx->bayer = 1; + } + + ret = avcodec_send_packet(s->avctx_mjpeg, s->jpkt); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n"); + return ret; + } + + ret = avcodec_receive_frame(s->avctx_mjpeg, s->jpgframe); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "JPEG decoding error: %s.\n", av_err2str(ret)); + + /* Normally skip, error if explode */ + if (avctx->err_recognition & AV_EF_EXPLODE) + return AVERROR_INVALIDDATA; + else + return 0; + } + + is_u16 = (s->bpp > 8); + + /* Copy the outputted tile's pixels from 'jpgframe' to 'frame' (final buffer) */ + + if (s->jpgframe->width != s->avctx_mjpeg->width || + s->jpgframe->height != s->avctx_mjpeg->height || + s->jpgframe->format != s->avctx_mjpeg->pix_fmt) + return AVERROR_INVALIDDATA; + + /* See dng_blit for explanation */ + if (s->avctx_mjpeg->width == w * 2 && + s->avctx_mjpeg->height == h / 2 && + s->avctx_mjpeg->pix_fmt == AV_PIX_FMT_GRAY16LE) { + is_single_comp = 1; + } else if (s->avctx_mjpeg->width >= w && + s->avctx_mjpeg->height >= h && + s->avctx_mjpeg->pix_fmt == (is_u16 ? AV_PIX_FMT_GRAY16 : AV_PIX_FMT_GRAY8) + ) { + is_single_comp = 0; + } else + return AVERROR_INVALIDDATA; + + 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 = dst_x + frame->linesize[0] * dst_y / pixel_size; + dst_data = frame->data[0] + dst_offset * pixel_size; + src_data = s->jpgframe->data[0]; + + dng_blit(s, + dst_data, + frame->linesize[0] / pixel_size, + src_data, + s->jpgframe->linesize[0] / pixel_size, + w, + h, + is_single_comp, + is_u16); + + av_frame_unref(s->jpgframe); + + return 0; +} + +static int dng_decode_strip(AVCodecContext *avctx, AVFrame *frame) +{ + TiffContext *s = avctx->priv_data; + + s->jpgframe->width = s->width; + s->jpgframe->height = s->height; + + s->avctx_mjpeg->width = s->width; + s->avctx_mjpeg->height = s->height; + + return dng_decode_jpeg(avctx, frame, s->stripsize, 0, 0, s->width, s->height); +} static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int stride, const uint8_t *src, int size, int strip_start, int lines) @@ -780,190 +971,6 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int strid return 0; } -/** - * Map stored raw sensor values into linear reference values (see: DNG Specification - Chapter 5) - */ -static uint16_t av_always_inline dng_process_color16(uint16_t value, - const uint16_t *lut, - uint16_t black_level, - float scale_factor) { - float value_norm; - - // Lookup table lookup - if (lut) - value = lut[value]; - - // Black level subtraction - value = av_clip_uint16_c((unsigned)value - black_level); - - // Color scaling - value_norm = (float)value * scale_factor; - - value = av_clip_uint16_c(value_norm * 65535); - - return value; -} - -static uint16_t av_always_inline dng_process_color8(uint16_t value, - const uint16_t *lut, - uint16_t black_level, - float scale_factor) { - return dng_process_color16(value, lut, black_level, scale_factor) >> 8; -} - -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_single_comp, int is_u16) -{ - int line, col; - float scale_factor; - - scale_factor = 1.0f / (s->white_level - s->black_level); - - 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); - - dst += dst_stride * sizeof(uint16_t); - src += src_stride * sizeof(uint16_t); - } - } else { - /* 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++) { - uint8_t *dst_u8 = dst; - const uint8_t *src_u8 = src; - - for (col = 0; col < width; col++) - *dst_u8++ = dng_process_color8(*src_u8++, s->dng_lut, s->black_level, scale_factor); - - dst += dst_stride; - src += src_stride; - } - } - } -} - -static int dng_decode_jpeg(AVCodecContext *avctx, AVFrame *frame, - int tile_byte_count, int dst_x, int dst_y, int w, int h) -{ - TiffContext *s = avctx->priv_data; - uint8_t *dst_data, *src_data; - uint32_t dst_offset; /* offset from dst buffer in pixels */ - int is_single_comp, is_u16, pixel_size; - int ret; - - if (tile_byte_count < 0 || tile_byte_count > bytestream2_get_bytes_left(&s->gb)) - return AVERROR_INVALIDDATA; - - /* Prepare a packet and send to the MJPEG decoder */ - av_packet_unref(s->jpkt); - s->jpkt->data = (uint8_t*)s->gb.buffer; - s->jpkt->size = tile_byte_count; - - if (s->is_bayer) { - MJpegDecodeContext *mjpegdecctx = s->avctx_mjpeg->priv_data; - /* We have to set this information here, there is no way to know if a given JPEG is a DNG-embedded - image or not from its own data (and we need that information when decoding it). */ - mjpegdecctx->bayer = 1; - } - - ret = avcodec_send_packet(s->avctx_mjpeg, s->jpkt); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n"); - return ret; - } - - ret = avcodec_receive_frame(s->avctx_mjpeg, s->jpgframe); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "JPEG decoding error: %s.\n", av_err2str(ret)); - - /* Normally skip, error if explode */ - if (avctx->err_recognition & AV_EF_EXPLODE) - return AVERROR_INVALIDDATA; - else - return 0; - } - - is_u16 = (s->bpp > 8); - - /* Copy the outputted tile's pixels from 'jpgframe' to 'frame' (final buffer) */ - - if (s->jpgframe->width != s->avctx_mjpeg->width || - s->jpgframe->height != s->avctx_mjpeg->height || - s->jpgframe->format != s->avctx_mjpeg->pix_fmt) - return AVERROR_INVALIDDATA; - - /* See dng_blit for explanation */ - if (s->avctx_mjpeg->width == w * 2 && - s->avctx_mjpeg->height == h / 2 && - s->avctx_mjpeg->pix_fmt == AV_PIX_FMT_GRAY16LE) { - is_single_comp = 1; - } else if (s->avctx_mjpeg->width >= w && - s->avctx_mjpeg->height >= h && - s->avctx_mjpeg->pix_fmt == (is_u16 ? AV_PIX_FMT_GRAY16 : AV_PIX_FMT_GRAY8) - ) { - is_single_comp = 0; - } else - return AVERROR_INVALIDDATA; - - 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 = dst_x + frame->linesize[0] * dst_y / pixel_size; - dst_data = frame->data[0] + dst_offset * pixel_size; - src_data = s->jpgframe->data[0]; - - dng_blit(s, - dst_data, - frame->linesize[0] / pixel_size, - src_data, - s->jpgframe->linesize[0] / pixel_size, - w, - h, - is_single_comp, - is_u16); - - av_frame_unref(s->jpgframe); - - return 0; -} - static int dng_decode_tiles(AVCodecContext *avctx, AVFrame *frame, const AVPacket *avpkt) { @@ -1040,19 +1047,6 @@ static int dng_decode_tiles(AVCodecContext *avctx, AVFrame *frame, return avpkt->size; } -static int dng_decode_strip(AVCodecContext *avctx, AVFrame *frame) -{ - TiffContext *s = avctx->priv_data; - - s->jpgframe->width = s->width; - s->jpgframe->height = s->height; - - s->avctx_mjpeg->width = s->width; - s->avctx_mjpeg->height = s->height; - - return dng_decode_jpeg(avctx, frame, s->stripsize, 0, 0, s->width, s->height); -} - static int init_image(TiffContext *s, ThreadFrame *frame) { int ret; From patchwork Tue Mar 30 08:03:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 26645 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 0E1A144B8E6 for ; Tue, 30 Mar 2021 11:04:10 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E317C680BE8; Tue, 30 Mar 2021 11:04:09 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05olkn2012.outbound.protection.outlook.com [40.92.89.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 553A4680613 for ; Tue, 30 Mar 2021 11:04:03 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SDD8/oQFpxMCFViuUbV5ckgLWwIFjSn49GnbXmZHkgK+MDDcivOzvbqjZD6FJgDhKrF5my42VDY7QwkuHM4+b9yvGKwDT+h5+S2AT60YfIyHb+l3FLIEvxBpQfhWqyzDCSil+wovvWeHjXaQm8JwGrTpiIcWLYRd0WjpAxe0yZ/oMv8hV+EiikGrXwIqCUNaDYfWjiH/f5qaNl3IMHG93qSQgmzJj020b2iQd7AS7Hk2hMOeN2CBFkMm91JtR/REWhpLFLffp/VXwwbiacDPVinR59imleuMRUDnIDJFHLmL82CXjz+fMj3A3fcEbz807/Zt+UWv1izjN9W3eibtdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LLENC5i2XZdzbHZ1ZSScUSufAKWzwTUGfzo4dxx6YE8=; b=BfQKpcpMC3b+GrA5sHAdaJtqKJ5qbPYY9+4AKu3N4a786Qn4O3wLoRxfG0MESz/XNlqOASc/G176MA3a2ZIzwKm0Vzm47GPoN4M3D8GFb+OiPSqnun+O89Tmnf4r0Y63PnYzJI5mlq2WLyxHsh6WZ0A753SBMIn2BYdLQJr76p3rLT7/yIejQptxrWO8M6kivDNDd8h5E23j8hY+DkvWjjy6FJpVheoONeCzyeFtbPU/8adgiTqc5Be61hAbKHm72t97eIoQZp7QeOOrRTQJyd0behgEff/ds1aLhCFZfkQGpzjYmFygK+oOkIEcoR4aUyyUhM5O+WrfZ3IyMkZR3A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LLENC5i2XZdzbHZ1ZSScUSufAKWzwTUGfzo4dxx6YE8=; b=ssYKFL3pmm7PgO38WH2/tkGAUaaDbqD1b47uBqlRFVO/ciry2osh/dX1UA4cIpZCWWQntJbScO3x/YW/U5e1r0e2C8OQh/H8fHQ0gJDzL+KSo5IIuT92AXxRk+rqI3zd6Axa6zZZ7vrIze8JPIksD3CX7VPLbMFryN+mmZmlZvWLVXBUms8HUul2R+9qXHARcVHDQjjmAMXHumRcnuV++3aeLoUXWEr5OJXPbwJvgkV1ShOhqXAM+aqL6SXulgVQd8y8ewLMncA0bOrdkGXetL4PXcxIbPknhke7h8ZIBW2LgAND34TBFutaYFrxDVASkJ1sKNNKQg2xLHgE8+n07g== Received: from DB8EUR05FT005.eop-eur05.prod.protection.outlook.com (2a01:111:e400:fc0f::4b) by DB8EUR05HT143.eop-eur05.prod.protection.outlook.com (2a01:111:e400:fc0f::294) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.29; Tue, 30 Mar 2021 08:04:02 +0000 Received: from HE1PR0301MB2154.eurprd03.prod.outlook.com (2a01:111:e400:fc0f::43) by DB8EUR05FT005.mail.protection.outlook.com (2a01:111:e400:fc0f::335) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3955.18 via Frontend Transport; Tue, 30 Mar 2021 08:04:02 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:5D5EA5242B9F8EA6B8C65A225A7AA4A5D20750B2037D65ED1EB7974D5A95DCDF; UpperCasedChecksum:125758C5D3A4D0D29DAA58999FD61651FEC28261BA99C7A039F2F37AD0D19E94; SizeAsReceived:7566; Count:47 Received: from HE1PR0301MB2154.eurprd03.prod.outlook.com ([fe80::8128:5de5:4e94:9a21]) by HE1PR0301MB2154.eurprd03.prod.outlook.com ([fe80::8128:5de5:4e94:9a21%3]) with mapi id 15.20.3977.033; Tue, 30 Mar 2021 08:04:02 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 30 Mar 2021 10:03:52 +0200 Message-ID: X-Mailer: git-send-email 2.27.0 In-Reply-To: References: X-TMN: [HzxZ5jZxDdcmTPhIMZI4qCasUJ0u/Ov3] X-ClientProxiedBy: AM0PR02CA0104.eurprd02.prod.outlook.com (2603:10a6:208:154::45) To HE1PR0301MB2154.eurprd03.prod.outlook.com (2603:10a6:3:2a::22) X-Microsoft-Original-Message-ID: <20210330080352.936548-1-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sblaptop.fritz.box (188.192.137.96) by AM0PR02CA0104.eurprd02.prod.outlook.com (2603:10a6:208:154::45) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Tue, 30 Mar 2021 08:04:01 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 47 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: e0c3259c-43b6-40f5-5584-08d8f3526155 X-MS-Exchange-SLBlob-MailProps: 6rN1YWC+iUWSfY1XRqGPVxU3myXOheZQRnnfKFuSmmNSxWv9oCjruR6/BtTO/TKhoGrfi4PHoUTgVKhDfT3+DWWuDViVgZ2rvg++7uL7gjUrDV+3QuE3YTGkyxsV5qdf3J8dgZLe0BcsE06PvhVd6GvlT+7E0C7YRWPBLMrWbljNvW1xy9A4LPfnwuMUTH7IlG0jhpK7ozlEmlFtp5Nvs6LCOrrvv9WeQezsIepqJjfOoEEcX98X7+nud1mP0PKeR/eWev4dNd844DolETa9QM/XzUCbvz+3EVEjdXYoxv/iLqRu+fycCoaF+7sethaPbzWYCpu9cOvhZ4JmdPRsJS1ikO+DDq7ecELCutQ8cujcaSw6tmroskXBUb/DhMzCV9V04adMbeHuXkiZqoOomnpQ08iueBUOQaHS/Wlz3put1NXQMBxSKZjriqeviGnAvZepT+sAp7xGbKvlWFtIf5n/pIwdKCr3UQ47WlTMNsm9cbC9uvR+Lt0Exw/ISW0wWo+nsDl8xEP6ahnVt8l8pfRB/q7sCChcajx4gU0XlYRfGVmGUx7MTSiyvn2TYuvocG4Ymd6j+wBs4bVho3fzmCxAryFJoSwufpw9WF7heNG5HEIm4mrIlJc3qXknph3Mr/BucRgyy5z4yTjZ/yyrHzDifDfKa3opM49zQtUAW446xx+DpmemgJz8jW67sR6aI/muiNMEWcWoytVlBHUvTVaqUNAEqW3AK2RswtX6QfuiJrAcuSUPXYGnOOgv8LJXHTsDSSnKXlkwbQL/4aXU1uhK0MG4HDi3CqXEHuGultlknlehw/2yEmlhhaFIsJ2+ X-MS-TrafficTypeDiagnostic: DB8EUR05HT143: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1yIU1NUOuo3WdwVoUFFBczszhibNb6rxb/+GK1gIbChxuaAFJcIsZ+VxAlQ5X6/SVq4QouWDexWgEBbFrTFqvg388LgtyZxXmnATarJzc3wcdSJBpoaLtSmWoX3ahhC5w8M7dJNGxyhADI0g2ofZWxYHrM1y/9W75UmTaX/vzloKbaRBC54ntN47YPdBN2Orm9nsZ6CpE50sKwIxeW8fgkoKTkZov+6orA0RVTU+VxtzcmC6Oj4mmW14+En8J7yNYDAvmbMruwMbaXDrQhCs2f/vz/bc6o7ZUI9IS/oGGYJBdnO3Vtvi9SYshfNYgRXpOMSRqAIhAoE2BO5cSo/3xq8hbakRwPby8MC3qIMQ5eIWtkhYVfc7w/PtwbadG5rLfW5C8baKpXbJ0qcdWWwIgQ== X-MS-Exchange-AntiSpam-MessageData: wyRX+1QmC3ZKKJDX7OMSFvEn38n3wtTuJ8KZJdgOMA/UMsPX4c9JlxJ1UeO0qLAaKjDx0MU62WyUXJN8qzIu1Fa0H1H7GKLkSo8CZ4hLwWlDPo+FsssWyw3FdtbcvbJQ7HVEOBU7dj4lJ23wo4DR8w== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: e0c3259c-43b6-40f5-5584-08d8f3526155 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2021 08:04:02.0614 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: DB8EUR05FT005.eop-eur05.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: Internet X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB8EUR05HT143 Subject: [FFmpeg-devel] [PATCH 2/2] avcodec/tiff: Don't use separate temporary buffer for fax 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Also don't unnecessarily copy the input data around if it needn't be reversed; and remove a redundant memset -- av_fast_padded_malloc() already does this for us. Signed-off-by: Andreas Rheinhardt --- libavcodec/tiff.c | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c index 1d72fdc720..6129ae1c25 100644 --- a/libavcodec/tiff.c +++ b/libavcodec/tiff.c @@ -108,8 +108,6 @@ typedef struct TiffContext { int deinvert_buf_size; uint8_t *yuv_line; unsigned int yuv_line_size; - uint8_t *fax_buffer; - unsigned int fax_buffer_size; int geotag_count; TiffGeoTag *geotags; @@ -613,27 +611,15 @@ static int tiff_unpack_lzma(TiffContext *s, AVFrame *p, uint8_t *dst, int stride static int tiff_unpack_fax(TiffContext *s, uint8_t *dst, int stride, const uint8_t *src, int size, int width, int lines) { - int i, ret = 0; int line; - uint8_t *src2; - - av_fast_padded_malloc(&s->fax_buffer, &s->fax_buffer_size, size); - src2 = s->fax_buffer; - - if (!src2) { - av_log(s->avctx, AV_LOG_ERROR, - "Error allocating temporary buffer\n"); - return AVERROR(ENOMEM); - } + int ret; - if (!s->fill_order) { - memcpy(src2, src, size); - } else { - for (i = 0; i < size; i++) - src2[i] = ff_reverse[src[i]]; + if (s->fill_order) { + if ((ret = deinvert_buffer(s, src, size)) < 0) + return ret; + src = s->deinvert_buf; } - memset(src2 + size, 0, AV_INPUT_BUFFER_PADDING_SIZE); - ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, + ret = ff_ccitt_unpack(s->avctx, src, size, dst, lines, stride, s->compr, s->fax_opts); if (s->bpp < 8 && s->avctx->pix_fmt == AV_PIX_FMT_PAL8) for (line = 0; line < lines; line++) { @@ -2186,8 +2172,6 @@ static av_cold int tiff_end(AVCodecContext *avctx) s->deinvert_buf_size = 0; av_freep(&s->yuv_line); s->yuv_line_size = 0; - av_freep(&s->fax_buffer); - s->fax_buffer_size = 0; av_frame_free(&s->jpgframe); av_packet_free(&s->jpkt); avcodec_free_context(&s->avctx_mjpeg);