From patchwork Wed Oct 9 19:10:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Skakov Pavel X-Patchwork-Id: 15641 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 6D37E4465CC for ; Wed, 9 Oct 2019 22:15:58 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 42E7B6880E8; Wed, 9 Oct 2019 22:15:58 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 72F24680B67 for ; Wed, 9 Oct 2019 22:15:51 +0300 (EEST) Received: by mail-lf1-f48.google.com with SMTP id 72so2504319lfh.6 for ; Wed, 09 Oct 2019 12:15:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:subject:to:message-id:date:user-agent:mime-version :content-language; bh=QAudBWhvRuRp55U0dwquLCQ7MgXcQ9RnVE6eRwK7wws=; b=Plwosyt3k/vbUQ4OAVkGBWoqRqOyhKqwnoNaHI/mS2twBu6mOMRnYwtJCUxjrqefDF 2aio86Wh/AsM8aSCzWzE/lqCQUcST5n1+uujUs3zyLBKFhJL1oLRSIVl6Kk4kls/25RE MiYu0W0dBnMpHywCnYE9Q8lfZ8byWKQuImpb4P6jiuVEEtHM5oNoyr05fP4JOwgwdv1H ZdhxFIk27ADnS4DlWtc6UtJNvJ/Vs7ZiWLAcxu5QJ3lwoEFQMl7BkA2ijalXnLMsub6x qtccFACrmDb5DS/PrHlIX9ZbcFyu3s+xhX0sf6XQikFiStcIelKRrvdDFgpUyHaawT9k N6Kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:message-id:date:user-agent :mime-version:content-language; bh=QAudBWhvRuRp55U0dwquLCQ7MgXcQ9RnVE6eRwK7wws=; b=FmmoR4EBz7W5wFSaQ2pjkiVCPu/41K7+xVn9PrgLADAEHoUbAsZPM8BYHlEramUFkE bLN6l9dzMN/tCgzUaQqSuW/ZrutpDp72MEw3hMlfGYtqPCKJX5vPdCmOD+ZPxoZyeOlD 0l4nBayGfzMefvrr0XIiZo4LP0j/L2z1E3Wpdi5d1/E95vsZnDj8FXBmiNB79T8EAkQS PTdmowRxznkon5PoUoPnNORqIf0k8kz4gev4sxsIEQw62oCdl2f9zwen0WHq02FW3upx /4wv83R5NS2dertt9J0lV6PVLT+FS7hd8alBb5hel39VIt7qBpMDRAOs7LQ7VwZ2ACuc 4F0g== X-Gm-Message-State: APjAAAWftOwZ9R5EAkuGpAGACKi1OG6AD2QRwfS6124qF/oojErYCIn7 PmsTnQan6BULHMCPmM5yobQnwIJH X-Google-Smtp-Source: APXvYqxZULMpsmA0st7iLyqDG2dbWKdI5A5NTgFrF/Xp4dy0THmokV8hc5PYZY70p0ZaZre/bBuaeQ== X-Received: by 2002:ac2:4a81:: with SMTP id l1mr769375lfp.67.1570648227311; Wed, 09 Oct 2019 12:10:27 -0700 (PDT) Received: from [10.1.0.176] ([5.18.248.33]) by smtp.gmail.com with ESMTPSA id k13sm665351ljc.96.2019.10.09.12.10.26 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Oct 2019 12:10:26 -0700 (PDT) From: Skakov Pavel To: FFmpeg development discussions and patches Message-ID: Date: Wed, 9 Oct 2019 22:10:25 +0300 User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 Content-Language: en-US Subject: [FFmpeg-devel] [PATCH] avcodec/tiff: add support for 16-bit YCbCr images and correct processing of border pixels 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Added support for 16-bit per sample YUV formats. This would allow ffmpeg to losslessly save (later) and load subsampled 10/16-bit YUV frames in simple and somewhat standard format. Corrected handling of border pixels when unpacking YUV. Example for 8-bit (bottom line): https://github.com/BitMiracle/libtiff.net/blob/master/TestCase/ycbcr-cat.tif Also made code a bit more compiler-friendly. From 14f0b1414e1aa2795cb95966cd2daa8ff4b9f937 Mon Sep 17 00:00:00 2001 From: Pavel Skakov Date: Wed, 9 Oct 2019 21:47:06 +0300 Subject: [PATCH] avcodec/tiff: add support for 16-bit YCbCr images and correct processing of border pixels Signed-off-by: Pavel Skakov --- libavcodec/tiff.c | 91 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 21 deletions(-) diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c index 069792b4c3..8e4a050ae9 100644 --- a/libavcodec/tiff.c +++ b/libavcodec/tiff.c @@ -358,31 +358,61 @@ static void unpack_gray(TiffContext *s, AVFrame *p, } } -static void unpack_yuv(TiffContext *s, AVFrame *p, +static void unpack_yuv(TiffContext *av_restrict s, AVFrame *av_restrict p, const uint8_t *src, int lnum) { - int i, j, k; - int w = (s->width - 1) / s->subsampling[0] + 1; + size_t i, j, k; + size_t w = (s->width - 1) / s->subsampling[0] + 1; + uint8_t *py = &p->data[0][lnum * p->linesize[0]]; uint8_t *pu = &p->data[1][lnum / s->subsampling[1] * p->linesize[1]]; uint8_t *pv = &p->data[2][lnum / s->subsampling[1] * p->linesize[2]]; if (s->width % s->subsampling[0] || s->height % s->subsampling[1]) { - for (i = 0; i < w; i++) { - for (j = 0; j < s->subsampling[1]; j++) - for (k = 0; k < s->subsampling[0]; k++) - p->data[0][FFMIN(lnum + j, s->height-1) * p->linesize[0] + - FFMIN(i * s->subsampling[0] + k, s->width-1)] = *src++; - *pu++ = *src++; - *pv++ = *src++; - } + if (s->bpp == 24) + for (i = 0; i < w; i++) { + for (j = 0; j < s->subsampling[1]; j++) + for (k = 0; k < s->subsampling[0]; k++) + if (j < s->height - lnum && i * s->subsampling[0] + k < s->width) + py[j * p->linesize[0] + i * s->subsampling[0] + k] = *src++; + else + src++; + *pu++ = *src++; + *pv++ = *src++; + } + else + for (i = 0; i < w; i++) { + for (j = 0; j < s->subsampling[1]; j++) + for (k = 0; k < s->subsampling[0]; k++) + if (j < s->height - lnum && i * s->subsampling[0] + k < s->width) { + py[j * p->linesize[0] + (i * s->subsampling[0] + k)*2 ] = *src++; + py[j * p->linesize[0] + (i * s->subsampling[0] + k)*2 + 1] = *src++; + } else + src += 2; + *pu++ = *src++; + *pu++ = *src++; + *pv++ = *src++; + *pv++ = *src++; + } }else{ - for (i = 0; i < w; i++) { - for (j = 0; j < s->subsampling[1]; j++) - for (k = 0; k < s->subsampling[0]; k++) - p->data[0][(lnum + j) * p->linesize[0] + - i * s->subsampling[0] + k] = *src++; - *pu++ = *src++; - *pv++ = *src++; - } + if (s->bpp == 24) + for (i = 0; i < w; i++) { + for (j = 0; j < s->subsampling[1]; j++) + for (k = 0; k < s->subsampling[0]; k++) + py[j * p->linesize[0] + i * s->subsampling[0] + k] = *src++; + *pu++ = *src++; + *pv++ = *src++; + } + else + for (i = 0; i < w; i++) { + for (j = 0; j < s->subsampling[1]; j++) + for (k = 0; k < s->subsampling[0]; k++) { + py[j * p->linesize[0] + (i * s->subsampling[0] + k)*2 ] = *src++; + py[j * p->linesize[0] + (i * s->subsampling[0] + k)*2 + 1] = *src++; + } + *pu++ = *src++; + *pu++ = *src++; + *pv++ = *src++; + *pv++ = *src++; + } } } @@ -585,8 +615,9 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int strid width = (s->width - 1) / s->subsampling[0] + 1; width = width * s->subsampling[0] * s->subsampling[1] + 2*width; + width *= s->bpp/24; av_assert0(width <= bytes_per_row); - av_assert0(s->bpp == 24); + av_assert0(s->bpp == 24 || s->bpp == 48); } if (s->is_bayer) { width = (s->bpp * s->width + 7) >> 3; @@ -1142,7 +1173,25 @@ static int init_image(TiffContext *s, ThreadFrame *frame) } break; case 483: - s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE; + if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) { + if (s->subsampling[0] == 1 && s->subsampling[1] == 1) { + s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YUV444P16LE : AV_PIX_FMT_YUV444P16BE; + } else if (s->subsampling[0] == 2 && s->subsampling[1] == 1) { + s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YUV422P16LE : AV_PIX_FMT_YUV422P16BE; + //} else if (s->subsampling[0] == 4 && s->subsampling[1] == 1) { + // s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YUV411P16LE : AV_PIX_FMT_YUV411P16BE; + //} else if (s->subsampling[0] == 1 && s->subsampling[1] == 2) { + // s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YUV440P16LE : AV_PIX_FMT_YUV440P16BE; + } else if (s->subsampling[0] == 2 && s->subsampling[1] == 2) { + s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YUV420P16LE : AV_PIX_FMT_YUV420P16BE; + //} else if (s->subsampling[0] == 4 && s->subsampling[1] == 4) { + // s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YUV410P16LE : AV_PIX_FMT_YUV410P16BE; + } else { + av_log(s->avctx, AV_LOG_ERROR, "Unsupported YCbCr subsampling\n"); + return AVERROR_PATCHWELCOME; + } + } else + s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB48BE; break; case 644: s->avctx->pix_fmt = s->le ? AV_PIX_FMT_RGBA64LE : AV_PIX_FMT_RGBA64BE; -- 2.13.2.windows.1