[FFmpeg-devel] [PATCH 2/5] avcodec/ffv1: Add YAF16 support

Michael Niedermayer michael at niedermayer.cc
Sat Mar 15 02:00:04 EET 2025


Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael at niedermayer.cc>
---
 libavcodec/ffv1_parse.c |  6 ++++--
 libavcodec/ffv1dec.c    |  4 ++--
 libavcodec/ffv1enc.c    | 33 +++++++++++++++++----------------
 3 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/libavcodec/ffv1_parse.c b/libavcodec/ffv1_parse.c
index a85b563e98b..a49f1eb6cef 100644
--- a/libavcodec/ffv1_parse.c
+++ b/libavcodec/ffv1_parse.c
@@ -294,9 +294,11 @@ int ff_ffv1_parse_header(FFV1Context *f, RangeCoder *c, uint8_t *state)
             } else
                 return AVERROR(ENOSYS);
         } else if (f->transparency && !f->chroma_planes) {
-            if (f->avctx->bits_per_raw_sample <= 8)
+            if (f->avctx->bits_per_raw_sample <= 8 && !f->flt) {
                 f->pix_fmt = AV_PIX_FMT_YA8;
-            else
+            } else if (f->avctx->bits_per_raw_sample == 16 && f->flt) {
+                f->pix_fmt = AV_PIX_FMT_YAF16;
+            } else
                 return AVERROR(ENOSYS);
         } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
             switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c
index d702e36625a..e24c6b8d3a2 100644
--- a/libavcodec/ffv1dec.c
+++ b/libavcodec/ffv1dec.c
@@ -362,8 +362,8 @@ static int decode_slice(AVCodecContext *c, void *arg)
         if (f->transparency)
             decode_plane(f, sc, &gb, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], (f->version >= 4 && !f->chroma_planes) ? 1 : 2, 2, 1, ac);
     } else if (f->colorspace == 0) {
-         decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0]    , width, height, p->linesize[0], 0, 0, 2, ac);
-         decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] + 1, width, height, p->linesize[0], 1, 1, 2, ac);
+         decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0]          , width, height, p->linesize[0], 0, 0, 2, ac);
+         decode_plane(f, sc, &gb, p->data[0] + ps*x + y*p->linesize[0] + (ps>>1), width, height, p->linesize[0], 1, 1, 2, ac);
     } else if (f->use32bit) {
         uint8_t *planes[4] = { p->data[0] + ps * x + y * p->linesize[0],
                                p->data[1] + ps * x + y * p->linesize[1],
diff --git a/libavcodec/ffv1enc.c b/libavcodec/ffv1enc.c
index b8ccda469ec..9fb98c37584 100644
--- a/libavcodec/ffv1enc.c
+++ b/libavcodec/ffv1enc.c
@@ -301,11 +301,11 @@ static int encode_plane(FFV1Context *f, FFV1SliceContext *sc,
         } else {
             if (f->packed_at_lsb) {
                 for (x = 0; x < w; x++) {
-                    sample[0][x] = ((uint16_t*)(src + stride*y))[x];
+                    sample[0][x] = ((uint16_t*)(src + stride*y))[x * pixel_stride];
                 }
             } else {
                 for (x = 0; x < w; x++) {
-                    sample[0][x] = ((uint16_t*)(src + stride*y))[x] >> (16 - f->bits_per_raw_sample);
+                    sample[0][x] = ((uint16_t*)(src + stride*y))[x * pixel_stride] >> (16 - f->bits_per_raw_sample);
                 }
             }
             if (sc->remap)
@@ -334,10 +334,10 @@ static void load_plane(FFV1Context *f, FFV1SliceContext *sc,
         } else {
             if (f->packed_at_lsb) {
                 for (x = 0; x < w; x++)
-                    sc->fltmap[remap_index][ ((uint16_t*)(src + stride*y))[x] ] = 1;
+                    sc->fltmap[remap_index][ ((uint16_t*)(src + stride*y))[x * pixel_stride] ] = 1;
             } else {
                 for (x = 0; x < w; x++)
-                    sc->fltmap[remap_index][ ((uint16_t*)(src + stride*y))[x] >> (16 - f->bits_per_raw_sample) ] = 1;
+                    sc->fltmap[remap_index][ ((uint16_t*)(src + stride*y))[x * pixel_stride] >> (16 - f->bits_per_raw_sample) ] = 1;
             }
         }
     }
@@ -842,6 +842,7 @@ av_cold int ff_ffv1_encode_setup_plane_info(AVCodecContext *avctx,
     case AV_PIX_FMT_YUVA444P16:
     case AV_PIX_FMT_YUVA422P16:
     case AV_PIX_FMT_YUVA420P16:
+    case AV_PIX_FMT_YAF16:
         if (!avctx->bits_per_raw_sample && !s->bits_per_raw_sample) {
             s->bits_per_raw_sample = 16;
         } else if (!s->bits_per_raw_sample) {
@@ -926,17 +927,16 @@ av_cold int ff_ffv1_encode_setup_plane_info(AVCodecContext *avctx,
         if (s->bits_per_raw_sample >= 16) {
             s->use32bit = 1;
         }
-        s->flt     = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT);
         s->version = FFMAX(s->version, 1);
-
-        if (s->flt)
-            s->version = FFMAX(s->version, 4);
         break;
     default:
         av_log(avctx, AV_LOG_ERROR, "format %s not supported\n",
                av_get_pix_fmt_name(pix_fmt));
         return AVERROR(ENOSYS);
     }
+    s->flt = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT);
+    if (s->flt)
+        s->version = FFMAX(s->version, 4);
     av_assert0(s->bits_per_raw_sample >= 8);
 
     if (s->remap_mode < 0)
@@ -1225,7 +1225,7 @@ retry:
     }
 
     if (sc->remap) {
-        if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8) {
+        if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8 && c->pix_fmt != AV_PIX_FMT_YAF16) {
             const int cx            = x >> f->chroma_h_shift;
             const int cy            = y >> f->chroma_v_shift;
 
@@ -1240,9 +1240,9 @@ retry:
             }
             if (f->transparency)
                 load_plane(f, sc, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 3, 1);
-        } else if (c->pix_fmt == AV_PIX_FMT_YA8) {
-            load_plane(f, sc, p->data[0] +     ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 2);
-            load_plane(f, sc, p->data[0] + 1 + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 2);
+        } else if (c->pix_fmt == AV_PIX_FMT_YA8 || c->pix_fmt == AV_PIX_FMT_YAF16) {
+            load_plane(f, sc, p->data[0] +           ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 2);
+            load_plane(f, sc, p->data[0] + (ps>>1) + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 2);
         } else if (f->use32bit) {
             load_rgb_frame32(f, sc, planes, width, height, p->linesize);
         } else
@@ -1265,7 +1265,7 @@ retry:
                       lsb_size_bytes);
     }
 
-    if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8) {
+    if (f->colorspace == 0 && c->pix_fmt != AV_PIX_FMT_YA8 && c->pix_fmt != AV_PIX_FMT_YAF16) {
         const int cx            = x >> f->chroma_h_shift;
         const int cy            = y >> f->chroma_v_shift;
 
@@ -1277,9 +1277,9 @@ retry:
         }
         if (f->transparency)
             ret |= encode_plane(f, sc, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2, 3, 1, ac);
-    } else if (c->pix_fmt == AV_PIX_FMT_YA8) {
-        ret  = encode_plane(f, sc, p->data[0] +     ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 0, 2, ac);
-        ret |= encode_plane(f, sc, p->data[0] + 1 + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 1, 2, ac);
+    } else if (c->pix_fmt == AV_PIX_FMT_YA8 || c->pix_fmt == AV_PIX_FMT_YAF16) {
+        ret  = encode_plane(f, sc, p->data[0] +           ps*x + y*p->linesize[0], width, height, p->linesize[0], 0, 0, 2, ac);
+        ret |= encode_plane(f, sc, p->data[0] + (ps>>1) + ps*x + y*p->linesize[0], width, height, p->linesize[0], 1, 1, 2, ac);
     } else if (f->use32bit) {
         ret = encode_rgb_frame32(f, sc, planes, width, height, p->linesize, ac);
     } else {
@@ -1564,6 +1564,7 @@ const FFCodec ff_ffv1_encoder = {
         AV_PIX_FMT_GRAY9,
         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
         AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV440P12,
+        AV_PIX_FMT_YAF16,
         AV_PIX_FMT_GBRPF16),
     .color_ranges   = AVCOL_RANGE_MPEG,
     .p.priv_class   = &ffv1_class,
-- 
2.48.1



More information about the ffmpeg-devel mailing list