[FFmpeg-devel] avcodec/tiff: add support for 16-bit YCbCr images and correct processing of border pixels

Submitted by Skakov Pavel on Oct. 9, 2019, 7:10 p.m.

Details

Message ID f1c27bd9-05c1-e236-9330-f5275a81275e@gmail.com
State New
Headers show

Commit Message

Skakov Pavel Oct. 9, 2019, 7:10 p.m.
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.

Comments

Carl Eugen Hoyos Oct. 9, 2019, 9:59 p.m.
Am Mi., 9. Okt. 2019 um 21:16 Uhr schrieb Skakov Pavel <pavelsx@gmail.com>:
>
> 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.

This should be a separate patch and please mention ticket #1178 if it
is related.

Please always use "} else {" instead of "else" and please do not re-indent the
code to make reviewing easier (feel free to also send a patch with the
re-indentation).

Carl Eugen
Carl Eugen Hoyos Oct. 9, 2019, 10:05 p.m.
Am Mi., 9. Okt. 2019 um 21:16 Uhr schrieb Skakov Pavel <pavelsx@gmail.com>:

> 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.

This is the most unexpected change - please make it a separate patch.

Carl Eugen
Skakov Pavel Oct. 9, 2019, 11:58 p.m.
>> Corrected handling of border pixels when unpacking YUV.
> 
> This should be a separate patch and please mention ticket #1178 if it is related.

It's probably unrelated: the ticket is about TIFF writer while I'm working on TIFF reader for now.

Patch hide | download patch | download mbox

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(-)

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