diff mbox

[FFmpeg-devel,v2,2/4] avcodec/tiff: add support for 16-bit YUV images

Message ID 9646f429-96af-2f2e-775c-234c363cc3d7@gmail.com
State New
Headers show

Commit Message

Skakov Pavel Oct. 10, 2019, 12:11 a.m. UTC
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.
Formats that don't currently have ffmpeg support but would work if such support appears are left commented out (YUV440P16/YUV411P16/YUV410P16).
diff mbox

Patch

From 529a3e68a74bf7bde67cf9c4defdf4504efbd2ce Mon Sep 17 00:00:00 2001
From: Pavel Skakov <pavelsx@gmail.com>
Date: Thu, 10 Oct 2019 02:31:20 +0300
Subject: [PATCH 2/4] avcodec/tiff: add support for 16-bit YUV images

Signed-off-by: Pavel Skakov <pavelsx@gmail.com>
---
 libavcodec/tiff.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)

diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 70c49f7d44..eac5113ad7 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -366,6 +366,7 @@  static void unpack_yuv(TiffContext *s, AVFrame *p,
     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]) {
+        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++)
@@ -377,7 +378,24 @@  static void unpack_yuv(TiffContext *s, AVFrame *p,
             *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) {
+                            p->data[0][(lnum + j) * p->linesize[0] + (i * s->subsampling[0] + k)*2    ] = *src++;
+                            p->data[0][(lnum + j) * p->linesize[0] + (i * s->subsampling[0] + k)*2 + 1] = *src++;
+                        } else {
+                            src += 2;
+                        }
+                *pu++ = *src++;
+                *pu++ = *src++;
+                *pv++ = *src++;
+                *pv++ = *src++;
+            }
+        }
     }else{
+        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++)
@@ -386,6 +404,19 @@  static void unpack_yuv(TiffContext *s, AVFrame *p,
             *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++) {
+                        p->data[0][(lnum + j) * p->linesize[0] + (i * s->subsampling[0] + k)*2    ] = *src++;
+                        p->data[0][(lnum + j) * p->linesize[0] + (i * s->subsampling[0] + k)*2 + 1] = *src++;
+                    }
+                *pu++ = *src++;
+                *pu++ = *src++;
+                *pv++ = *src++;
+                *pv++ = *src++;
+            }
+        }
     }
 }
 
@@ -588,8 +619,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;
@@ -1145,7 +1177,26 @@  static int init_image(TiffContext *s, ThreadFrame *frame)
         }
         break;
     case 483:
+        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] == 2 && s->subsampling[1] == 2) {
+                s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YUV420P16LE : AV_PIX_FMT_YUV420P16BE;
+            //} 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] == 4 && s->subsampling[1] == 1) {
+            //    s->avctx->pix_fmt = s->le ? AV_PIX_FMT_YUV411P16LE : AV_PIX_FMT_YUV411P16BE;
+            //} 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