ticoraw_entropy decodes the slices of a precinct into wavelet coefficients: the
precinct and packet headers, the GCLI sub-packets, magnitudes, signs and the
dequantization. Every read is bounds checked.

ticoraw_dsp holds the inverse 5/3 wavelet steps and the inverse Star-Tetrix
colour transform, behind a function pointer table.

ticoraw_core runs the stages in order, up to the linear Bayer mosaic. The
slices are entropy decoded in parallel through avctx->execute2, followed by
the horizontal wavelet steps per slice, the vertical step in column strips and
the Star-Tetrix passes in row jobs.

The files are not built yet. The decoder that uses them follows.

Signed-off-by: Alper Kagan AVCI <[email protected]>
---
 libavcodec/ticoraw_core.c    | 353 ++++++++++++++++++++++++++++
 libavcodec/ticoraw_core.h    | 103 ++++++++
 libavcodec/ticoraw_dsp.c     | 212 +++++++++++++++++
 libavcodec/ticoraw_dsp.h     |  70 ++++++
 libavcodec/ticoraw_entropy.c | 440 +++++++++++++++++++++++++++++++++++
 libavcodec/ticoraw_entropy.h | 114 +++++++++
 6 files changed, 1292 insertions(+)
 create mode 100644 libavcodec/ticoraw_core.c
 create mode 100644 libavcodec/ticoraw_core.h
 create mode 100644 libavcodec/ticoraw_dsp.c
 create mode 100644 libavcodec/ticoraw_dsp.h
 create mode 100644 libavcodec/ticoraw_entropy.c
 create mode 100644 libavcodec/ticoraw_entropy.h

diff --git a/libavcodec/ticoraw_core.c b/libavcodec/ticoraw_core.c
new file mode 100644
index 0000000000..404362e7e3
--- /dev/null
+++ b/libavcodec/ticoraw_core.c
@@ -0,0 +1,353 @@
+/*
+ * TICO RAW (Nikon N-RAW, R3D NE) core decoder: codestream to companded CFA 
planes
+ *
+ * Copyright (c) 2026 Alper Kagan AVCI
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <string.h>
+
+#include "libavutil/attributes.h"
+#include "libavutil/avassert.h"
+#include "libavutil/common.h"
+#include "libavutil/error.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/log.h"
+#include "libavutil/mem.h"
+
+#include "avcodec.h"
+#include "ticoraw_core.h"
+#include "ticoraw_dsp.h"
+#include "ticoraw_entropy.h"
+
+/* The header fields that the buffers and the band layout depend on. */
+typedef struct CoreGeometry {
+    int cw, ch, ng, ss, nb_bands, nb_precincts, slice_height, nb_slices;
+    uint8_t comp_nlx[TICORAW_NB_COMPONENTS];
+    uint8_t comp_nly[TICORAW_NB_COMPONENTS];
+} CoreGeometry;
+
+/* Scratch of one slice thread. Jobs that run at the same time never share 
one. */
+typedef struct CoreThread {
+    TicoRawSliceContext slice_ctx;
+    int32_t *row_tmp;               /* idwt53_h scratch, cw elements */
+} CoreThread;
+
+struct TicoRawCore {
+    AVCodecContext *avctx;          /* runs the jobs (execute2), log context */
+    TicoRawDSPContext dsp;
+    int nb_threads;                 /* slice threads of avctx, 1 without slice 
threads */
+    CoreThread *thread;             /* nb_threads entries, indexed by threadnr 
*/
+
+    /* Made for geom; rebuilt when the geometry of a header differs. */
+    int have_geom;
+    CoreGeometry geom;
+    TicoRawLayout layout;
+    TicoRawSliceSpan *spans;        /* nb_slices entries */
+    int *slice_ret;                 /* nb_slices entries: return value of each 
slice job */
+    int32_t *plane_buf;
+    int32_t *plane[4];
+    ptrdiff_t stride;               /* in int32_t elements */
+    /* L_j: low-pass width after j levels */
+    int len[TICORAW_NB_COMPONENTS][TICORAW_MAX_PACKET_BANDS];
+    int nb_row_jobs;                /* Star-Tetrix row jobs */
+    int nb_strips, strip_width;     /* vertical wavelet column jobs per 
component */
+
+    /* The call in progress, read by the jobs. */
+    const TicoRawHeader *h;
+    const uint8_t *cs;
+    int st_pass;
+};
+
+static void get_geometry(CoreGeometry *g, const TicoRawHeader *h)
+{
+    memset(g, 0, sizeof(*g));
+    g->cw           = h->cw;
+    g->ch           = h->ch;
+    g->ng           = h->ng;
+    g->ss           = h->ss;
+    g->nb_bands     = h->nb_bands;
+    g->nb_precincts = h->nb_precincts;
+    g->slice_height = h->slice_height;
+    g->nb_slices    = h->nb_slices;
+    memcpy(g->comp_nlx, h->comp_nlx, sizeof(g->comp_nlx));
+    memcpy(g->comp_nly, h->comp_nly, sizeof(g->comp_nly));
+}
+
+static void core_free_geometry(TicoRawCore *c)
+{
+    int t;
+
+    for (t = 0; c->thread && t < c->nb_threads; t++) {
+        ff_ticoraw_slice_context_free(&c->thread[t].slice_ctx);
+        av_freep(&c->thread[t].row_tmp);
+    }
+    av_freep(&c->spans);
+    av_freep(&c->slice_ret);
+    av_freep(&c->plane_buf);
+    memset(c->plane, 0, sizeof(c->plane));
+    c->have_geom = 0;
+}
+
+static int core_setup(TicoRawCore *c, const TicoRawHeader *h)
+{
+    CoreGeometry g;
+    int i, j, t, ret;
+
+    get_geometry(&g, h);
+    if (c->have_geom && !memcmp(&g, &c->geom, sizeof(g)))
+        return 0;
+
+    core_free_geometry(c);
+    if ((ret = ff_ticoraw_layout_init(&c->layout, h, c->avctx)) < 0)
+        return ret;
+
+    c->stride    = FFALIGN(h->cw, 16);
+    c->plane_buf = av_malloc_array(4 * c->stride, h->ch * sizeof(int32_t));
+    c->spans     = av_malloc_array(h->nb_slices, sizeof(*c->spans));
+    c->slice_ret = av_malloc_array(h->nb_slices, sizeof(*c->slice_ret));
+    if (!c->plane_buf || !c->spans || !c->slice_ret) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+    for (t = 0; t < c->nb_threads; t++) {
+        if ((ret = ff_ticoraw_slice_context_init(&c->thread[t].slice_ctx, 
&c->layout)) < 0)
+            goto fail;
+        c->thread[t].row_tmp = av_malloc_array(h->cw, sizeof(int32_t));
+        if (!c->thread[t].row_tmp) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
+    }
+    for (i = 0; i < 4; i++)
+        c->plane[i] = c->plane_buf + i * c->stride * h->ch;
+
+    /* len[c][j] = L_j, the low-pass width after j levels */
+    for (i = 0; i < TICORAW_NB_COMPONENTS; i++) {
+        c->len[i][0] = h->cw;
+        for (j = 1; j <= h->comp_nlx[i]; j++)
+            c->len[i][j] = c->len[i][j - 1] - c->len[i][j - 1] / 2;
+    }
+
+    c->nb_row_jobs = ff_ticoraw_nb_row_jobs(c->avctx, h->ch);
+    c->strip_width = FFALIGN((h->cw + c->nb_threads - 1) / c->nb_threads, 16);
+    c->nb_strips   = (h->cw + c->strip_width - 1) / c->strip_width;
+
+    c->geom      = g;
+    c->have_geom = 1;
+    return 0;
+
+fail:
+    core_free_geometry(c);
+    return ret;
+}
+
+int ff_ticoraw_nb_row_jobs(const AVCodecContext *avctx, int rows)
+{
+    if (!(avctx->active_thread_type & FF_THREAD_SLICE) || avctx->thread_count 
<= 1)
+        return 1;
+    return av_clip(4 * avctx->thread_count, 1, FFMAX(rows, 1));
+}
+
+av_cold int ff_ticoraw_core_init(TicoRawCore **core, const TicoRawHeader *h,
+                                 AVCodecContext *avctx)
+{
+    TicoRawCore *c = av_mallocz(sizeof(*c));
+    int ret;
+
+    if (!c)
+        return AVERROR(ENOMEM);
+    c->avctx      = avctx;
+    c->nb_threads = avctx->active_thread_type & FF_THREAD_SLICE ?
+                    FFMAX(avctx->thread_count, 1) : 1;
+    c->thread     = av_calloc(c->nb_threads, sizeof(*c->thread));
+    if (!c->thread) {
+        av_free(c);
+        return AVERROR(ENOMEM);
+    }
+    ff_ticoraw_dsp_init(&c->dsp);
+    if ((ret = core_setup(c, h)) < 0) {
+        ff_ticoraw_core_free(&c);
+        return ret;
+    }
+    *core = c;
+    return 0;
+}
+
+av_cold void ff_ticoraw_core_free(TicoRawCore **core)
+{
+    if (!*core)
+        return;
+    core_free_geometry(*core);
+    av_freep(&(*core)->thread);
+    av_freep(core);
+}
+
+/*
+ * Horizontal inverse wavelet steps on rows y0 to y1 - 1 of every component.
+ * Each row is transformed on its own.
+ */
+static void horizontal_rows(TicoRawCore *c, int32_t *tmp, int y0, int y1)
+{
+    int comp, y, j;
+
+    for (comp = 0; comp < TICORAW_NB_COMPONENTS; comp++) {
+        const int n = c->geom.comp_nlx[comp];
+        const int *len = c->len[comp];
+
+        if (!n)
+            continue;   /* no wavelet: planes_st equals coeffs */
+        for (y = y0; y < y1; y++) {
+            int32_t *row = c->plane[comp] + y * c->stride;
+            if (y & 1) {
+                c->dsp.idwt53_h(row, tmp, len[0]);
+            } else {
+                for (j = n - 1; j >= 0; j--)
+                    c->dsp.idwt53_h(row, tmp, len[j]);
+            }
+        }
+    }
+}
+
+/*
+ * Job s: entropy decode slice s (slices are independent), then run the 
horizontal
+ * wavelet steps on its rows: precinct k holds rows 2k and 2k + 1.
+ */
+static int slice_job(AVCodecContext *avctx, void *arg, int s, int threadnr)
+{
+    TicoRawCore *c = arg;
+    CoreThread *t;
+    int ret, y0, y1;
+
+    av_assert1(threadnr < c->nb_threads);
+    t   = &c->thread[threadnr];
+    ret = ff_ticoraw_decode_slice(&t->slice_ctx, &c->layout, c->h, c->cs, 
&c->spans[s], s,
+                                  c->plane, c->stride, c->avctx);
+    if (ret < 0)
+        return ret;
+
+    y0 = 2 * s * c->geom.slice_height;
+    y1 = FFMIN(y0 + 2 * c->geom.slice_height, c->geom.ch);
+    horizontal_rows(c, t->row_tmp, y0, y1);
+    return 0;
+}
+
+/* Job comp * nb_strips + i: the vertical wavelet step on column strip i of 
comp. */
+static int vertical_job(AVCodecContext *avctx, void *arg, int job, int 
threadnr)
+{
+    TicoRawCore *c = arg;
+    const int comp = job / c->nb_strips;
+    const int x0   = job % c->nb_strips * c->strip_width;
+
+    if (!c->geom.comp_nlx[comp] || x0 >= c->geom.cw)
+        return 0;
+    c->dsp.idwt53_v(c->plane[comp] + x0, c->stride,
+                    FFMIN(c->strip_width, c->geom.cw - x0), c->geom.ch);
+    return 0;
+}
+
+/* Job i: Star-Tetrix pass st_pass on row band i. */
+static int star_tetrix_job(AVCodecContext *avctx, void *arg, int job, int 
threadnr)
+{
+    TicoRawCore *c = arg;
+    const int ch = c->geom.ch;
+    const int y0 = (int64_t)ch * job       / c->nb_row_jobs;
+    const int y1 = (int64_t)ch * (job + 1) / c->nb_row_jobs;
+
+    c->dsp.inv_star_tetrix[c->st_pass](c->plane, c->stride, c->geom.cw, ch, 
y0, y1);
+    return 0;
+}
+
+int ff_ticoraw_decode_planes(TicoRawCore *c, const uint8_t *cs, int size,
+                             const TicoRawHeader *h, TicoRawPlanes *out)
+{
+    AVCodecContext *avctx = c->avctx;
+    int s, i, ret;
+
+    if ((ret = core_setup(c, h)) < 0)
+        return ret;
+    if ((ret = ff_ticoraw_find_slices(cs, size, h, &c->layout, c->spans, 
avctx)) < 0)
+        return ret;
+
+    c->h    = h;
+    c->cs   = cs;
+    avctx->execute2(avctx, slice_job, c, c->slice_ret, h->nb_slices);
+    for (s = 0; s < h->nb_slices; s++)
+        if (c->slice_ret[s] < 0)
+            return c->slice_ret[s];
+
+    avctx->execute2(avctx, vertical_job, c, NULL, TICORAW_NB_COMPONENTS * 
c->nb_strips);
+    for (c->st_pass = 0; c->st_pass < TICORAW_ST_PASSES; c->st_pass++)
+        avctx->execute2(avctx, star_tetrix_job, c, NULL, c->nb_row_jobs);
+
+    for (i = 0; i < 4; i++)
+        out->p[i] = c->plane[i];
+    out->cw     = c->geom.cw;
+    out->ch     = c->geom.ch;
+    out->stride = c->stride;
+    return 0;
+}
+
+#define CURVE_BIAS (5 << 15)   /* 5/8 of 2^18 */
+
+void ff_ticoraw_build_curve(uint16_t *lut, const TicoRawHeader *h)
+{
+    int i;
+
+    for (i = 0; i < TICORAW_CURVE_SIZE; i++) {
+        int64_t e = i - h->curve_pivot, phi, tau;
+
+        if (e < 0) {
+            phi = -e * e;
+        } else {
+            tau = (e * h->curve_slope + 32768) >> 16;
+            phi = tau * tau;
+        }
+        /*
+         * The curve is black_level + (phi + CURVE_BIAS) / 2^18, rounded down: 
the
+         * arithmetic shift floors also for a negative phi. The bias is 5/8 of 
the
+         * divisor, a constant of the curve and not a rounding to the nearest 
value.
+         */
+        lut[i] = av_clip64(h->black_level + ((phi + CURVE_BIAS) >> 18),
+                           0, TICORAW_WHITE_LEVEL);
+    }
+}
+
+void ff_ticoraw_planes_to_bayer(uint16_t *dst, ptrdiff_t dst_stride,
+                                const TicoRawPlanes *planes, const uint16_t 
*lut,
+                                int y0, int y1)
+{
+    int x, y;
+
+    for (y = y0; y < y1; y++) {
+        const int32_t *gr = planes->p[0] + y * planes->stride;
+        const int32_t *r  = planes->p[1] + y * planes->stride;
+        const int32_t *gb = planes->p[2] + y * planes->stride;
+        const int32_t *b  = planes->p[3] + y * planes->stride;
+        uint16_t *row0 = dst + 2 * (y - y0) * dst_stride;
+        uint16_t *row1 = row0 + dst_stride;
+
+        /* code index i = clip(v + 32768, 0, 65535) */
+        for (x = 0; x < planes->cw; x++) {
+            AV_WL16(&row0[2 * x],     lut[av_clip_uint16(r[x]  + 32768)]);
+            AV_WL16(&row0[2 * x + 1], lut[av_clip_uint16(gr[x] + 32768)]);
+            AV_WL16(&row1[2 * x],     lut[av_clip_uint16(gb[x] + 32768)]);
+            AV_WL16(&row1[2 * x + 1], lut[av_clip_uint16(b[x]  + 32768)]);
+        }
+    }
+}
diff --git a/libavcodec/ticoraw_core.h b/libavcodec/ticoraw_core.h
new file mode 100644
index 0000000000..de1bde3a12
--- /dev/null
+++ b/libavcodec/ticoraw_core.h
@@ -0,0 +1,103 @@
+/*
+ * TICO RAW (Nikon N-RAW, R3D NE) core decoder: codestream to companded CFA 
planes
+ *
+ * Copyright (c) 2026 Alper Kagan AVCI
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_TICORAW_CORE_H
+#define AVCODEC_TICORAW_CORE_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "ticoraw_header.h"
+
+/** Four int32 planes of cw x ch samples, in component order. stride is in 
elements. */
+typedef struct TicoRawPlanes {
+    int32_t *p[4];
+    int cw, ch;
+    ptrdiff_t stride;
+} TicoRawPlanes;
+
+struct AVCodecContext;
+
+typedef struct TicoRawCore TicoRawCore;   /* opaque: buffers, per-thread 
scratch, DSP */
+
+/**
+ * Make a core for the codec context avctx. The core runs its jobs through
+ * avctx->execute2(); with slice threads it keeps one scratch set per slice 
thread.
+ * avctx is also the log context. One core per codec context.
+ */
+int  ff_ticoraw_core_init(TicoRawCore **core, const TicoRawHeader *h,
+                          struct AVCodecContext *avctx);
+void ff_ticoraw_core_free(TicoRawCore **core);
+
+/**
+ * Decode one codestream to the four companded CFA planes: entropy decoding, 
the
+ * inverse wavelet and the inverse Star-Tetrix transform.
+ *
+ * The slices are entropy decoded in parallel, and the wavelet and Star-Tetrix 
passes are
+ * split into row or column jobs, all through avctx->execute2(). The output 
does not
+ * depend on the number of threads.
+ *
+ * The planes belong to core and stay valid until the next call. Calls on one 
core must
+ * not overlap. cs must be followed by AV_INPUT_BUFFER_PADDING_SIZE readable 
bytes (as
+ * in an AVPacket). h must be the result of ff_ticoraw_parse_header() on the 
same
+ * codestream; the core keeps its buffers and band layout while the geometry 
of h
+ * (sizes, levels, slices) stays the same, and rebuilds them when it changes.
+ *
+ * @return 0, AVERROR_INVALIDDATA for damaged data, AVERROR_PATCHWELCOME for an
+ *         unsupported structure, or AVERROR(ENOMEM)
+ */
+int ff_ticoraw_decode_planes(TicoRawCore *core, const uint8_t *cs, int size,
+                             const TicoRawHeader *h, TicoRawPlanes *out);
+
+/**
+ * Number of jobs to split rows rows over in avctx->execute2(): 1 without 
slice threads,
+ * else a few jobs per thread, so that faster cores take more of them.
+ */
+int ff_ticoraw_nb_row_jobs(const struct AVCodecContext *avctx, int rows);
+
+#define TICORAW_CURVE_SIZE  65536  /* entries of the transfer curve table: one 
per 16-bit code */
+#define TICORAW_WHITE_LEVEL 16383  /* ceiling of the transfer curve, 2^14 - 1 
*/
+
+/**
+ * Build the table that maps a 16-bit plane code to a linear sensor value, 
from the
+ * curve parameters of h (black_level, curve_slope, curve_pivot).
+ *
+ * @param lut  TICORAW_CURVE_SIZE entries, filled with linear values 0 to
+ *             TICORAW_WHITE_LEVEL
+ */
+void ff_ticoraw_build_curve(uint16_t *lut, const TicoRawHeader *h);
+
+/**
+ * Write quad rows y0 to y1 - 1 of the companded CFA planes as
+ * linear RGGB mosaic rows 2 * y0 to 2 * y1 - 1.
+ *
+ * The samples are stored as 16-bit little-endian values.
+ *
+ * @param dst     mosaic row 2 * y0; dst_stride is in uint16_t elements, not 
bytes
+ * @param planes  planes Gr, R, Gb, B as returned by ff_ticoraw_decode_planes()
+ * @param lut     table from ff_ticoraw_build_curve()
+ */
+void ff_ticoraw_planes_to_bayer(uint16_t *dst, ptrdiff_t dst_stride,
+                                const TicoRawPlanes *planes, const uint16_t 
*lut,
+                                int y0, int y1);
+
+#endif /* AVCODEC_TICORAW_CORE_H */
diff --git a/libavcodec/ticoraw_dsp.c b/libavcodec/ticoraw_dsp.c
new file mode 100644
index 0000000000..f1c4fc59dd
--- /dev/null
+++ b/libavcodec/ticoraw_dsp.c
@@ -0,0 +1,212 @@
+/*
+ * TICO RAW (Nikon N-RAW, R3D NE) inverse wavelet and inverse Star-Tetrix
+ *
+ * Copyright (c) 2026 Alper Kagan AVCI
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <string.h>
+
+#include "libavutil/attributes.h"
+#include "libavutil/common.h"
+
+#include "ticoraw_dsp.h"
+
+/* Index replacements give the whole-sample symmetric extension. */
+static void idwt53_h_c(int32_t *x, int32_t *tmp, int n)
+{
+    const int ns = (n + 1) >> 1, nd = n >> 1;
+    const int32_t *s = tmp, *d = tmp + ns;
+    int i;
+
+    if (n < 2)
+        return;
+    memcpy(tmp, x, n * sizeof(*x));
+
+    /* even samples: d[-1] -> d[0], d[nd] -> d[nd - 1] */
+    x[0] = s[0] - ((d[0] + d[0] + 2) >> 2);
+    for (i = 1; i < nd; i++)
+        x[2 * i] = s[i] - ((d[i - 1] + d[i] + 2) >> 2);
+    if (ns > nd)
+        x[2 * nd] = s[nd] - ((d[nd - 1] + d[nd - 1] + 2) >> 2);
+
+    /* odd samples, after all even ones: x[n] -> x[n - 2] when n is even */
+    for (i = 0; i < nd - 1; i++)
+        x[2 * i + 1] = d[i] + ((x[2 * i] + x[2 * i + 2]) >> 1);
+    i = nd - 1;
+    x[2 * i + 1] = d[i] + ((x[2 * i] + (2 * i + 2 < n ? x[2 * i + 2] : x[2 * 
i])) >> 1);
+}
+
+/*
+ * The same step down every column, s[i] = row 2i, d[i] = row 2i + 1.
+ * Done row by row in place: the even rows first (they read only odd rows), 
then the
+ * odd rows (they read only the finished even rows).
+ */
+static void idwt53_v_c(int32_t *p, ptrdiff_t stride, int w, int h)
+{
+    const int ns = (h + 1) >> 1, nd = h >> 1;
+    int i, x;
+
+    if (h < 2)
+        return;
+
+    for (i = 0; i < ns; i++) {
+        int32_t *e = p + 2 * i * stride;
+        const int32_t *dm = p + (2 * (i > 0 ? i - 1 : 0) + 1) * stride;
+        const int32_t *dp = p + (2 * (i < nd ? i : nd - 1) + 1) * stride;
+
+        for (x = 0; x < w; x++)
+            e[x] -= (dm[x] + dp[x] + 2) >> 2;
+    }
+    for (i = 0; i < nd; i++) {
+        int32_t *o = p + (2 * i + 1) * stride;
+        const int32_t *a = p + 2 * i * stride;
+        const int32_t *b = 2 * i + 2 < h ? a + 2 * stride : a;
+
+        for (x = 0; x < w; x++)
+            o[x] += (a[x] + b[x]) >> 1;
+    }
+}
+
+/*
+ * Inverse Star-Tetrix: five lifting steps, each over the whole picture before 
the next. A step
+ * reads only planes other than the one it updates (apart from the sample 
itself), so the
+ * order of the samples inside a step does not matter, and the rows of a step 
may be done
+ * in any split. Neighbours repeat the edge sample.
+ *
+ * Valid streams keep every value near 2^16. The input is clamped to +-2^24 so
+ * that crafted data cannot overflow the four-term sums: five steps grow the 
magnitude to
+ * at most 3.5 * 2^24, and the largest sum stays below 2^28.
+ */
+#define ST_LIMIT (1 << 24)
+
+#define ST_ARGS int32_t *const p[4], ptrdiff_t stride, int w, int h, int y0, 
int y1
+#define ROW(P, Y) ((P) + (ptrdiff_t)(Y) * stride)
+#define XM (x > 0 ? x - 1 : 0)
+#define XP (x < w - 1 ? x + 1 : w - 1)
+
+static void st_clamp_c(ST_ARGS)
+{
+    int x, y, i;
+
+    for (i = 0; i < 4; i++)
+        for (y = y0; y < y1; y++) {
+            int32_t *r = ROW(p[i], y);
+            for (x = 0; x < w; x++)
+                r[x] = av_clip(r[x], -ST_LIMIT, ST_LIMIT);
+        }
+}
+
+/* 1. A -= floor((E(x, y-) + E(x+, y-) + E(x, y) + E(x+, y)) / 8) */
+static void st_step1_c(ST_ARGS)
+{
+    int32_t *const A = p[0];
+    const int32_t *const E = p[2];
+    int x, y;
+
+    for (y = y0; y < y1; y++) {
+        int32_t *a = ROW(A, y);
+        const int32_t *e0 = ROW(E, y > 0 ? y - 1 : 0), *e1 = ROW(E, y);
+        for (x = 0; x < w; x++)
+            a[x] -= (e0[x] + e0[XP] + e1[x] + e1[XP]) >> 3;
+    }
+}
+
+/* 2. E += -floor((C(x, y) + C(x, y+) + F(x-, y) + F(x, y)) / 8)
+ *         + floor((A(x-, y) + A(x, y) + A(x-, y+) + A(x, y+)) / 4) */
+static void st_step2_c(ST_ARGS)
+{
+    const int32_t *const A = p[0], *const C = p[1], *const F = p[3];
+    int32_t *const E = p[2];
+    int x, y;
+
+    for (y = y0; y < y1; y++) {
+        int32_t *e = ROW(E, y);
+        const int yp = y < h - 1 ? y + 1 : h - 1;
+        const int32_t *c0 = ROW(C, y), *c1 = ROW(C, yp), *f0 = ROW(F, y);
+        const int32_t *a0 = ROW(A, y), *a1 = ROW(A, yp);
+        for (x = 0; x < w; x++) {
+            const int xm = XM;
+            e[x] += -((c0[x] + c1[x] + f0[xm] + f0[x]) >> 3) +
+                     ((a0[xm] + a0[x] + a1[xm] + a1[x]) >> 2);
+        }
+    }
+}
+
+/* 3. A -= floor((C(x, y) + C(x+, y) + F(x, y-) + F(x, y)) / 8) */
+static void st_step3_c(ST_ARGS)
+{
+    int32_t *const A = p[0];
+    const int32_t *const C = p[1], *const F = p[3];
+    int x, y;
+
+    for (y = y0; y < y1; y++) {
+        int32_t *a = ROW(A, y);
+        const int32_t *c0 = ROW(C, y), *f0 = ROW(F, y > 0 ? y - 1 : 0), *f1 = 
ROW(F, y);
+        for (x = 0; x < w; x++)
+            a[x] -= (c0[x] + c0[XP] + f0[x] + f1[x]) >> 3;
+    }
+}
+
+/* 4. C += floor((A(x-, y) + A(x, y) + E(x, y-) + E(x, y)) / 4) */
+static void st_step4_c(ST_ARGS)
+{
+    const int32_t *const A = p[0], *const E = p[2];
+    int32_t *const C = p[1];
+    int x, y;
+
+    for (y = y0; y < y1; y++) {
+        int32_t *c = ROW(C, y);
+        const int32_t *a0 = ROW(A, y), *e0 = ROW(E, y > 0 ? y - 1 : 0), *e1 = 
ROW(E, y);
+        for (x = 0; x < w; x++)
+            c[x] += (a0[XM] + a0[x] + e0[x] + e1[x]) >> 2;
+    }
+}
+
+/* 5. F += floor((A(x, y) + A(x, y+) + E(x, y) + E(x+, y)) / 4) */
+static void st_step5_c(ST_ARGS)
+{
+    const int32_t *const A = p[0], *const E = p[2];
+    int32_t *const F = p[3];
+    int x, y;
+
+    for (y = y0; y < y1; y++) {
+        int32_t *f = ROW(F, y);
+        const int32_t *a0 = ROW(A, y), *a1 = ROW(A, y < h - 1 ? y + 1 : h - 1);
+        const int32_t *e1 = ROW(E, y);
+        for (x = 0; x < w; x++)
+            f[x] += (a0[x] + a1[x] + e1[x] + e1[XP]) >> 2;
+    }
+}
+
+#undef ST_ARGS
+#undef ROW
+#undef XM
+#undef XP
+
+av_cold void ff_ticoraw_dsp_init(TicoRawDSPContext *c)
+{
+    c->idwt53_h           = idwt53_h_c;
+    c->idwt53_v           = idwt53_v_c;
+    c->inv_star_tetrix[0] = st_clamp_c;
+    c->inv_star_tetrix[1] = st_step1_c;
+    c->inv_star_tetrix[2] = st_step2_c;
+    c->inv_star_tetrix[3] = st_step3_c;
+    c->inv_star_tetrix[4] = st_step4_c;
+    c->inv_star_tetrix[5] = st_step5_c;
+}
diff --git a/libavcodec/ticoraw_dsp.h b/libavcodec/ticoraw_dsp.h
new file mode 100644
index 0000000000..e1fed8da08
--- /dev/null
+++ b/libavcodec/ticoraw_dsp.h
@@ -0,0 +1,70 @@
+/*
+ * TICO RAW (Nikon N-RAW, R3D NE) inverse wavelet and inverse Star-Tetrix
+ *
+ * Copyright (c) 2026 Alper Kagan AVCI
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_TICORAW_DSP_H
+#define AVCODEC_TICORAW_DSP_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+#define TICORAW_ST_PASSES 6  /* input clamp, then the five lifting steps */
+
+/*
+ * All strides are in int32_t elements, not bytes. All arithmetic is integer, 
with
+ * floor division done by arithmetic right shifts.
+ */
+typedef struct TicoRawDSPContext {
+    /**
+     * One-dimensional inverse 5/3 step, in place on one row.
+     * Input: ceil(n / 2) low-pass values, then floor(n / 2) high-pass values.
+     * Output: n interleaved samples.
+     *
+     * @param x    the row, n elements
+     * @param tmp  scratch, at least n elements, must not overlap x
+     */
+    void (*idwt53_h)(int32_t *x, int32_t *tmp, int n);
+
+    /**
+     * Vertical inverse 5/3 step, in place on a plane in the layout
+     * that the horizontal steps leave: row 2i holds low-pass value i and row 
2i + 1 holds high-pass
+     * value i of every column. Columns are independent, so a caller may pass 
a strip of
+     * columns (p + x0, w = strip width).
+     */
+    void (*idwt53_v)(int32_t *p, ptrdiff_t stride, int w, int h);
+
+    /**
+     * Inverse Star-Tetrix colour transform, in place on the four planes A, C, 
E, F
+     * (components 0 to 3), each w x h. After all passes they hold the CFA 
planes
+     * Gr, R, Gb, B.
+     *
+     * Pass 0 clamps the input, passes 1 to 5 are the five lifting steps. Each 
pass works
+     * on rows y0 to y1 - 1 only and reads only planes it does not write, so 
the rows of
+     * one pass may be split across threads; every row of a pass must be done 
before the
+     * next pass starts.
+     */
+    void (*inv_star_tetrix[TICORAW_ST_PASSES])(int32_t *const p[4], ptrdiff_t 
stride,
+                                               int w, int h, int y0, int y1);
+} TicoRawDSPContext;
+
+void ff_ticoraw_dsp_init(TicoRawDSPContext *c);
+
+#endif /* AVCODEC_TICORAW_DSP_H */
diff --git a/libavcodec/ticoraw_entropy.c b/libavcodec/ticoraw_entropy.c
new file mode 100644
index 0000000000..dad4fd9b5b
--- /dev/null
+++ b/libavcodec/ticoraw_entropy.c
@@ -0,0 +1,440 @@
+/*
+ * TICO RAW (Nikon N-RAW, R3D NE) precinct entropy decoding and dequantization
+ *
+ * Copyright (c) 2026 Alper Kagan AVCI
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/*
+ * Every read is bounds checked: the bit readers are the checked variant (they 
never
+ * read past the end of their buffer plus the input padding), and a sub-packet 
whose
+ * content runs past its length, or a length that runs past the precinct, is
+ * AVERROR_INVALIDDATA. Every loop is bounded by the band and packet geometry.
+ */
+
+#define UNCHECKED_BITSTREAM_READER 0
+
+#include <string.h>
+
+#include "libavutil/attributes.h"
+#include "libavutil/common.h"
+#include "libavutil/error.h"
+#include "libavutil/intmath.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/log.h"
+#include "libavutil/mem.h"
+
+#include "get_bits.h"
+#include "jpegxs.h"
+#include "ticoraw_entropy.h"
+
+#define SLH_SIZE            6   /* marker, L = 4, slice index */
+#define PACKET_HEADER_SIZE  7   /* Dr 1, Ldat 20, Lcnt 20, Lsgn 15 bits */
+#define GROUP_SIZE          4   /* Ng of the supported set; the magnitude 
planes are 4 bits */
+#define MAX_GCLI            15
+#define MAX_UNARY           31
+
+static int add_band(TicoRawLayout *l, const TicoRawHeader *h, int comp, int 
start,
+                    int width, int packet, int line, void *logctx)
+{
+    TicoRawBand *b;
+    int nb = l->packet_nb_bands[packet];
+
+    if (l->nb_bands >= TICORAW_NB_BANDS || nb >= TICORAW_MAX_PACKET_BANDS) {
+        av_log(logctx, AV_LOG_ERROR,
+               "Unsupported band structure: more than %d bands\n", 
TICORAW_NB_BANDS);
+        return AVERROR_PATCHWELCOME;
+    }
+    b = &l->band[l->nb_bands];
+    b->comp          = comp;
+    b->start         = start;
+    b->width         = width;
+    b->nb_groups     = (width + GROUP_SIZE - 1) / GROUP_SIZE;
+    b->nb_sig_groups = (b->nb_groups + h->ss - 1) / h->ss;
+    l->total_groups += b->nb_groups;
+
+    l->packet_band[packet][nb].band = l->nb_bands;
+    l->packet_band[packet][nb].line = line;
+    l->packet_nb_bands[packet]++;
+    l->nb_bands++;
+    return 0;
+}
+
+av_cold int ff_ticoraw_layout_init(TicoRawLayout *l, const TicoRawHeader *h, 
void *logctx)
+{
+    int line0_band[TICORAW_NB_COMPONENTS];
+    int c, j, ret;
+
+    memset(l, 0, sizeof(*l));
+    l->nb_packets = TICORAW_NB_PACKETS;
+
+    if (h->ng != GROUP_SIZE || h->ss < 1) {
+        av_log(logctx, AV_LOG_ERROR, "Unsupported Ng (ng) %d / Ss (ss) %d\n", 
h->ng, h->ss);
+        return AVERROR_PATCHWELCOME;
+    }
+    for (c = 0; c < TICORAW_NB_COMPONENTS; c++) {
+        int nlx = h->comp_nlx[c], nly = h->comp_nly[c];
+        if (!(nlx == 0 && nly == 0) &&
+            !(nlx >= 1 && nlx < TICORAW_MAX_PACKET_BANDS && nly == 1)) {
+            av_log(logctx, AV_LOG_ERROR,
+                   "Unsupported decomposition levels %d x %d of component 
%d\n", nlx, nly, c);
+            return AVERROR_PATCHWELCOME;
+        }
+    }
+
+    /* Line 0 bands of components 0 to 3, packets 0 to 3. */
+    for (c = 0; c < TICORAW_NB_COMPONENTS; c++) {
+        int n = h->comp_nlx[c], lo = h->cw, hi[TICORAW_MAX_PACKET_BANDS], 
start;
+
+        line0_band[c] = l->nb_bands;
+        for (j = 1; j <= n; j++) {
+            hi[j] = lo / 2;
+            lo   -= hi[j];
+        }
+        if ((ret = add_band(l, h, c, 0, lo, c, 0, logctx)) < 0)
+            return ret;
+        start = lo;
+        for (j = n; j >= 1; j--) {
+            if ((ret = add_band(l, h, c, start, hi[j], c, 0, logctx)) < 0)
+                return ret;
+            start += hi[j];
+        }
+    }
+
+    /*
+     * Line 1 bands, packets 4 to 7. A component without a wavelet codes line 
1 with its
+     * line 0 band (same band index).
+     */
+    for (c = 0; c < TICORAW_NB_COMPONENTS; c++) {
+        int packet = TICORAW_NB_COMPONENTS + c, lo = (h->cw + 1) / 2;
+
+        if (!h->comp_nlx[c]) {
+            int nb = l->packet_nb_bands[packet];
+            l->packet_band[packet][nb].band = line0_band[c];
+            l->packet_band[packet][nb].line = 1;
+            l->packet_nb_bands[packet]++;
+            continue;
+        }
+        if ((ret = add_band(l, h, c, 0,  lo,         packet, 1, logctx)) < 0 ||
+            (ret = add_band(l, h, c, lo, h->cw - lo, packet, 1, logctx)) < 0)
+            return ret;
+    }
+
+    if (l->nb_bands != h->nb_bands) {
+        av_log(logctx, AV_LOG_ERROR,
+               "Unsupported band count %d, the WGT marker has %d entries\n",
+               l->nb_bands, h->nb_bands);
+        return AVERROR_PATCHWELCOME;
+    }
+    l->precinct_header_size = 5 + (2 * l->nb_bands + 7) / 8;
+    return 0;
+}
+
+av_cold int ff_ticoraw_slice_context_init(TicoRawSliceContext *sc, const 
TicoRawLayout *l)
+{
+    int b, pos = 0;
+
+    memset(sc, 0, sizeof(*sc));
+    sc->gcli_buf = av_mallocz(FFMAX(l->total_groups, 1));
+    if (!sc->gcli_buf)
+        return AVERROR(ENOMEM);
+    for (b = 0; b < l->nb_bands; b++) {
+        sc->gcli[b] = sc->gcli_buf + pos;
+        pos += l->band[b].nb_groups;
+    }
+    return 0;
+}
+
+av_cold void ff_ticoraw_slice_context_free(TicoRawSliceContext *sc)
+{
+    av_freep(&sc->gcli_buf);
+    memset(sc->gcli, 0, sizeof(sc->gcli));
+}
+
+int ff_ticoraw_find_slices(const uint8_t *cs, int size, const TicoRawHeader *h,
+                           const TicoRawLayout *l, TicoRawSliceSpan *spans, 
void *logctx)
+{
+    const int end = size - 2; /* EOC */
+    int pos = h->first_slice_offset, s, k;
+
+    for (s = 0; s < h->nb_slices; s++) {
+        int nb_prec = FFMIN(h->slice_height, h->nb_precincts - s * 
h->slice_height);
+
+        if (pos < 0 || end - pos < SLH_SIZE || AV_RB16(cs + pos) != 
JPEGXS_MARKER_SLH ||
+            AV_RB16(cs + pos + 2) != 4 || AV_RB16(cs + pos + 4) != s) {
+            av_log(logctx, AV_LOG_ERROR, "Missing slice header %d at offset 
%d\n", s, pos);
+            return AVERROR_INVALIDDATA;
+        }
+        pos += SLH_SIZE;
+        spans[s].start = pos;
+        for (k = 0; k < nb_prec; k++) {
+            int lprc;
+
+            if (end - pos < l->precinct_header_size) {
+                av_log(logctx, AV_LOG_ERROR,
+                       "Precinct header of slice %d runs past the 
codestream\n", s);
+                return AVERROR_INVALIDDATA;
+            }
+            lprc = AV_RB24(cs + pos);
+            if (lprc > end - pos - l->precinct_header_size) {
+                av_log(logctx, AV_LOG_ERROR,
+                       "Precinct of slice %d (Lprc %d) runs past the 
codestream\n", s, lprc);
+                return AVERROR_INVALIDDATA;
+            }
+            pos += l->precinct_header_size + lprc;
+        }
+        spans[s].end = pos;
+    }
+    /* Any bytes may come between the last precinct and EOC. */
+    return 0;
+}
+
+/* Count the 1 bits before the next 0 bit, the 0 bit is consumed. */
+static av_always_inline int read_unary(GetBitContext *gb)
+{
+    unsigned v = show_bits_long(gb, 32);
+    int u;
+
+    if (v == 0xFFFFFFFFU)
+        return -1;
+    u = ff_clz(~v);
+    if (u > MAX_UNARY)
+        return -1;
+    skip_bits_long(gb, u + 1);
+    return u;
+}
+
+/* Uniform reconstruction, m + floor(m / 2^z) + floor(m / 2^2z) + ... */
+static av_always_inline int dequant(int m, int t, int gcli)
+{
+    const int zeta = gcli - t + 1;
+    int mh = 0;
+
+    if (!t)
+        return m;
+    for (; m; m >>= zeta)
+        mh += m;
+    return mh;
+}
+
+typedef struct PrecinctParams {
+    int k;                              /* precinct index */
+    int q, r;                           /* quantization and refinement values 
*/
+    uint8_t mode[TICORAW_NB_BANDS];     /* D[b] */
+} PrecinctParams;
+
+static int decode_packet(TicoRawSliceContext *sc, const TicoRawLayout *l,
+                         const TicoRawHeader *h, const uint8_t *cs, int *ppos, 
int pend,
+                         int packet, const PrecinctParams *pp,
+                         int32_t *const planes[4], ptrdiff_t stride, void 
*logctx)
+{
+    const int nb_bands = l->packet_nb_bands[packet];
+    GetBitContext gsig, gcnt, gdat, gsgn;
+    int pos = *ppos, dr, ldat, lcnt, lsgn, nb_flags = 0, sig_len, i;
+    uint64_t hdr;
+
+    if (pend - pos < PACKET_HEADER_SIZE)
+        goto overrun;
+    hdr  = (uint64_t)AV_RB32(cs + pos) << 24 | AV_RB24(cs + pos + 4);
+    dr   = hdr >> 55;
+    ldat = (hdr >> 35) & 0xFFFFF;
+    lcnt = (hdr >> 15) & 0xFFFFF;
+    lsgn = hdr & 0x7FFF;
+    pos += PACKET_HEADER_SIZE;
+
+    /* Significance sub-packet: no length field, one flag per significance 
group. */
+    if (!dr)
+        for (i = 0; i < nb_bands; i++) {
+            int b = l->packet_band[packet][i].band;
+            if (pp->mode[b] & 2)
+                nb_flags += l->band[b].nb_sig_groups;
+        }
+    sig_len = (nb_flags + 7) >> 3;
+
+    if (sig_len > pend - pos)
+        goto overrun;
+    init_get_bits8(&gsig, cs + pos, sig_len);
+    pos += sig_len;
+    if (lcnt > pend - pos)
+        goto overrun;
+    init_get_bits8(&gcnt, cs + pos, lcnt);
+    pos += lcnt;
+    if (ldat > pend - pos)
+        goto overrun;
+    init_get_bits8(&gdat, cs + pos, ldat);
+    pos += ldat;
+    if (lsgn > pend - pos)
+        goto overrun;
+    init_get_bits8(&gsgn, cs + pos, lsgn);
+    pos += lsgn;
+
+    /*
+     * The four sub-packets are read with their own readers, band by band and 
group by
+     * group. Each sub-packet is still read in its own order (all bands of the 
packet, in
+     * band order), so this gives the same result as decoding every GCLI first.
+     */
+    for (i = 0; i < nb_bands; i++) {
+        const int b = l->packet_band[packet][i].band;
+        const int line = l->packet_band[packet][i].line;
+        const TicoRawBand *band = &l->band[b];
+        const int row = 2 * pp->k + line;
+        const int use_sig = !dr && (pp->mode[b] & 2);
+        const int vertical = !dr && (pp->mode[b] & 1) && sc->have_src[b];
+        const int ts = sc->gtli[b];
+        uint8_t *gcli = sc->gcli[b];
+        int32_t *dst = row < h->ch ? planes[band->comp] + row * stride + 
band->start : NULL;
+        int t, s, g;
+
+        /* GTLI: priorities are only compared, gains only subtracted. */
+        t = pp->q - h->band_gain[b] - (h->band_priority[b] < pp->r);
+        t = av_clip(t, 0, MAX_GCLI);
+
+        for (s = 0; s < band->nb_sig_groups; s++) {
+            const int flag = use_sig ? get_bits1(&gsig) : 0;
+            const int gend = FFMIN((s + 1) * h->ss, band->nb_groups);
+
+            for (g = s * h->ss; g < gend; g++) {
+                const int pi = vertical ? FFMAX3(gcli[g], ts, t) : t;
+                int a[GROUP_SIZE] = { 0 }, n, j, v;
+
+                if (dr) {
+                    v = get_bits(&gcnt, h->br);
+                } else if (flag) {
+                    v = pi;
+                } else {
+                    int u = read_unary(&gcnt);
+                    if (u < 0) {
+                        av_log(logctx, AV_LOG_ERROR,
+                               "Unary GCLI value with more than %d ones in 
precinct %d\n",
+                               MAX_UNARY, pp->k);
+                        return AVERROR_INVALIDDATA;
+                    }
+                    if (!vertical) {
+                        v = t + u;
+                    } else {
+                        const int rho = pi - t;
+                        int delta;
+                        if (u > 2 * rho)
+                            delta = u - rho;
+                        else if (u & 1)
+                            delta = -((u + 1) >> 1);
+                        else
+                            delta = u >> 1;
+                        v = pi + delta;
+                    }
+                }
+                if (v < 0 || v > MAX_GCLI) {
+                    av_log(logctx, AV_LOG_ERROR, "GCLI %d out of range in 
precinct %d\n",
+                           v, pp->k);
+                    return AVERROR_INVALIDDATA;
+                }
+                gcli[g] = v;
+
+                /* Magnitudes: n bit planes of 4 bits, most significant first. 
*/
+                n = v - t;
+                for (; n > 0; n--) {
+                    const int bits = get_bits(&gdat, GROUP_SIZE);
+                    a[0] = a[0] << 1 | (bits >> 3);
+                    a[1] = a[1] << 1 | (bits >> 2 & 1);
+                    a[2] = a[2] << 1 | (bits >> 1 & 1);
+                    a[3] = a[3] << 1 | (bits      & 1);
+                }
+
+                /* Signs and dequantization. */
+                for (j = 0; j < GROUP_SIZE; j++) {
+                    const int idx = g * GROUP_SIZE + j;
+                    int m = a[j] << t;
+
+                    if (m) {
+                        m = dequant(m, t, v) << h->fq;
+                        if (get_bits1(&gsgn))
+                            m = -m;
+                    }
+                    if (dst && idx < band->width)
+                        dst[idx] = m;
+                }
+            }
+        }
+        if (get_bits_left(&gcnt) < 0 || get_bits_left(&gdat) < 0 ||
+            get_bits_left(&gsgn) < 0) {
+            av_log(logctx, AV_LOG_ERROR,
+                   "%s sub-packet content runs past its length in precinct 
%d\n",
+                   get_bits_left(&gcnt) < 0 ? "GCLI" :
+                   get_bits_left(&gdat) < 0 ? "Data" : "Sign", pp->k);
+            return AVERROR_INVALIDDATA;
+        }
+        /* This band line is now the prediction source of band index b. */
+        sc->gtli[b]     = t;
+        sc->have_src[b] = 1;
+    }
+
+    *ppos = pos;
+    return 0;
+
+overrun:
+    av_log(logctx, AV_LOG_ERROR, "Packet %d runs past its precinct %d\n", 
packet, pp->k);
+    return AVERROR_INVALIDDATA;
+}
+
+int ff_ticoraw_decode_slice(TicoRawSliceContext *sc, const TicoRawLayout *l,
+                            const TicoRawHeader *h, const uint8_t *cs,
+                            const TicoRawSliceSpan *span, int slice,
+                            int32_t *const planes[4], ptrdiff_t stride, void 
*logctx)
+{
+    const int first = slice * h->slice_height;
+    const int last  = FFMIN(first + h->slice_height, h->nb_precincts);
+    int pos = span->start, k, b, p, ret;
+
+    /* Prediction starts fresh in every slice. */
+    memset(sc->have_src, 0, sizeof(sc->have_src));
+    memset(sc->gtli, 0, sizeof(sc->gtli));
+
+    for (k = first; k < last; k++) {
+        PrecinctParams pp;
+        int lprc, pend;
+
+        if (span->end - pos < l->precinct_header_size)
+            goto overrun;
+        lprc = AV_RB24(cs + pos);
+        if (lprc > span->end - pos - l->precinct_header_size)
+            goto overrun;
+        pend = pos + l->precinct_header_size + lprc;
+
+        pp.k = k;
+        pp.q = cs[pos + 3];
+        pp.r = cs[pos + 4];
+        for (b = 0; b < l->nb_bands; b++)
+            pp.mode[b] = cs[pos + 5 + (b >> 2)] >> (6 - 2 * (b & 3)) & 3;
+
+        pos += l->precinct_header_size;
+        for (p = 0; p < l->nb_packets; p++) {
+            ret = decode_packet(sc, l, h, cs, &pos, pend, p, &pp, planes, 
stride, logctx);
+            if (ret < 0)
+                return ret;
+        }
+        /* Zero padding up to Lprc. */
+        pos = pend;
+    }
+    if (pos != span->end)
+        goto overrun;
+    return 0;
+
+overrun:
+    av_log(logctx, AV_LOG_ERROR, "Precinct runs past slice %d\n", slice);
+    return AVERROR_INVALIDDATA;
+}
diff --git a/libavcodec/ticoraw_entropy.h b/libavcodec/ticoraw_entropy.h
new file mode 100644
index 0000000000..fb69aaf3a2
--- /dev/null
+++ b/libavcodec/ticoraw_entropy.h
@@ -0,0 +1,114 @@
+/*
+ * TICO RAW (Nikon N-RAW, R3D NE) precinct entropy decoding and dequantization
+ *
+ * Copyright (c) 2026 Alper Kagan AVCI
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_TICORAW_ENTROPY_H
+#define AVCODEC_TICORAW_ENTROPY_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "ticoraw_header.h"
+
+#define TICORAW_NB_LINES          2  /* lines per precinct and component (NLy 
= 1) */
+#define TICORAW_NB_PACKETS        (TICORAW_NB_LINES * TICORAW_NB_COMPONENTS)
+#define TICORAW_MAX_PACKET_BANDS  16 /* bands of one component on one line: n 
+ 1 <= 16 */
+
+/** One band of the precinct layout. */
+typedef struct TicoRawBand {
+    int comp;           /* component */
+    int start;          /* first column in the wavelet layout */
+    int width;          /* coefficients */
+    int nb_groups;      /* code groups: ceil(width / Ng) */
+    int nb_sig_groups;  /* significance groups: ceil(nb_groups / Ss) */
+} TicoRawBand;
+
+/** One band of one packet, with the precinct line it is coded for. */
+typedef struct TicoRawPacketBand {
+    uint8_t band;
+    uint8_t line;
+} TicoRawPacketBand;
+
+/** Band and packet structure of every precinct, derived from the header once. 
*/
+typedef struct TicoRawLayout {
+    int nb_bands;
+    TicoRawBand band[TICORAW_NB_BANDS];
+    int nb_packets;
+    int packet_nb_bands[TICORAW_NB_PACKETS];
+    TicoRawPacketBand 
packet_band[TICORAW_NB_PACKETS][TICORAW_MAX_PACKET_BANDS];
+    int precinct_header_size;   /* 5 + ceil(2 * nb_bands / 8) bytes */
+    int total_groups;           /* code groups of all bands */
+} TicoRawLayout;
+
+/**
+ * Per-slice decoding state: the prediction source of every band index.
+ * Slices are independent, so each thread that decodes slices needs its own 
context.
+ */
+typedef struct TicoRawSliceContext {
+    uint8_t *gcli[TICORAW_NB_BANDS];    /* GCLIs of the last decoded line of 
band b */
+    uint8_t gtli[TICORAW_NB_BANDS];     /* GTLI of that line */
+    uint8_t have_src[TICORAW_NB_BANDS]; /* 0 until band b was decoded in this 
slice */
+    uint8_t *gcli_buf;
+} TicoRawSliceContext;
+
+/** Start and end offsets of the precinct data of one slice (SLH excluded). */
+typedef struct TicoRawSliceSpan {
+    int start, end;
+} TicoRawSliceSpan;
+
+/**
+ * Derive the bands and packets of a precinct from the header.
+ * @return 0, or AVERROR_PATCHWELCOME when the structure is outside the 
supported set
+ */
+int ff_ticoraw_layout_init(TicoRawLayout *l, const TicoRawHeader *h, void 
*logctx);
+
+int  ff_ticoraw_slice_context_init(TicoRawSliceContext *sc, const 
TicoRawLayout *l);
+void ff_ticoraw_slice_context_free(TicoRawSliceContext *sc);
+
+/**
+ * Walk the slice headers and precinct lengths of the codestream and record
+ * where the precinct data of each slice starts and ends.
+ *
+ * @param spans  h->nb_slices entries
+ * @return 0 or AVERROR_INVALIDDATA
+ */
+int ff_ticoraw_find_slices(const uint8_t *cs, int size, const TicoRawHeader *h,
+                           const TicoRawLayout *l, TicoRawSliceSpan *spans, 
void *logctx);
+
+/**
+ * Entropy decode and dequantize the precincts of one slice into the four 
coefficient
+ * planes. Each plane holds the bands of its component side by side in every 
row, the
+ * low-pass band first (the layout that the inverse wavelet starts from).
+ *
+ * cs must be followed by AV_INPUT_BUFFER_PADDING_SIZE readable bytes. Every 
read stays
+ * inside the slice span plus that padding.
+ *
+ * @param slice   slice index
+ * @param planes  four planes of h->cw x h->ch int32 samples
+ * @param stride  plane stride in int32_t elements
+ * @return 0 or AVERROR_INVALIDDATA
+ */
+int ff_ticoraw_decode_slice(TicoRawSliceContext *sc, const TicoRawLayout *l,
+                            const TicoRawHeader *h, const uint8_t *cs,
+                            const TicoRawSliceSpan *span, int slice,
+                            int32_t *const planes[4], ptrdiff_t stride, void 
*logctx);
+
+#endif /* AVCODEC_TICORAW_ENTROPY_H */
-- 
2.56.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to