From 14f0b1414e1aa2795cb95966cd2daa8ff4b9f937 Mon Sep 17 00:00:00 2001
From: Pavel Skakov <pavelsx@gmail.com>
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 <pavelsx@gmail.com>
---
libavcodec/tiff.c | 91 ++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 70 insertions(+), 21 deletions(-)
@@ -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