diff mbox series

[FFmpeg-devel,v5,3/4] libavformat/dnxucdec: DNxUncompressed decoder

Message ID 20240911105616.849239-4-ms+git@mur.at
State New
Headers show
Series [FFmpeg-devel,v5,1/4] libavcodec/dnxuc_parser: DNxUncompressed essence parser | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 fail Make failed
andriy/make_x86 fail Make failed

Commit Message

martin schitter Sept. 11, 2024, 10:56 a.m. UTC
---
 libavcodec/allcodecs.c  |   1 +
 libavcodec/codec_desc.c |   7 +
 libavcodec/codec_id.h   |   1 +
 libavcodec/dnxucdec.c   | 391 ++++++++++++++++++++++++++++++++++++++++
 libavcodec/version.c    |   2 +-
 5 files changed, 401 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/dnxucdec.c
diff mbox series

Patch

diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index d773ac3..1a3c084 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -93,6 +93,7 @@  extern const FFCodec ff_dfa_decoder;
 extern const FFCodec ff_dirac_decoder;
 extern const FFCodec ff_dnxhd_encoder;
 extern const FFCodec ff_dnxhd_decoder;
+extern const FFCodec ff_dnxuc_decoder;
 extern const FFCodec ff_dpx_encoder;
 extern const FFCodec ff_dpx_decoder;
 extern const FFCodec ff_dsicinvideo_decoder;
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index a28ef68..27bf105 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1959,6 +1959,13 @@  static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("LEAD MCMP"),
         .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
     },
+    {
+        .id        = AV_CODEC_ID_DNXUC,
+        .type      = AVMEDIA_TYPE_VIDEO,
+        .name      = "dnxuc",
+        .long_name = NULL_IF_CONFIG_SMALL("DNxUncompressed / SMPTE RDD 50"),
+        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
+    },
 
     /* various PCM "codecs" */
     {
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index 0ab1e34..c8f8072 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -322,6 +322,7 @@  enum AVCodecID {
     AV_CODEC_ID_RTV1,
     AV_CODEC_ID_VMIX,
     AV_CODEC_ID_LEAD,
+    AV_CODEC_ID_DNXUC,
 
     /* various PCM "codecs" */
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
diff --git a/libavcodec/dnxucdec.c b/libavcodec/dnxucdec.c
new file mode 100644
index 0000000..8fffd0a
--- /dev/null
+++ b/libavcodec/dnxucdec.c
@@ -0,0 +1,391 @@ 
+/*
+ * Avid DNxUncomressed / SMPTE RDD 50 decoder
+ * Copyright (c) 2024 Martin Schitter
+ *
+ * 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
+ */
+
+/*
+ This decoder for DNxUncompressed video data is mostly based on
+ reverse engineering of output generated by DaVinci Resolve 19
+ but was later also checked against the SMPTE RDD 50 specification.
+
+ Not all DNxUncompressed pixel format variants are supported,
+ but at least an elementary base set is already usable:
+
+  - YUV 4:2:2 8/10/12bit
+  - RGB 8/10/12bit/half/float
+
+*/
+
+#include "avcodec.h"
+#include "codec_internal.h"
+#include "decode.h"
+#include "libavutil/imgutils.h"
+#include "thread.h"
+
+static av_cold int dnxuc_decode_init(AVCodecContext *avctx)
+{
+    return 0;
+}
+
+
+static int pass_though(AVCodecContext *avctx, AVFrame *frame, const AVPacket *avpkt)
+{
+    /* there is no need to copy as the data already match
+     * a known pixel format */
+
+    frame->buf[0] = av_buffer_ref(avpkt->buf);
+
+    if (!frame->buf[0]) {
+        return AVERROR(ENOMEM);
+    }
+
+    return av_image_fill_arrays(frame->data, frame->linesize, avpkt->data,
+                               avctx->pix_fmt, avctx->width, avctx->height, 1);
+}
+
+static int float2planes(AVCodecContext *avctx, AVFrame *frame, const AVPacket *pkt)
+{
+    int x, y, lw;
+    const size_t sof = 4;
+
+    lw = frame->width;
+
+    for(y = 0; y < frame->height; y++){
+        for(x = 0; x < frame->width; x++){
+            memcpy(&frame->data[2][sof*(lw*y + x)], &pkt->data[sof* 3*(lw*y + x)], sof);
+            memcpy(&frame->data[0][sof*(lw*y + x)], &pkt->data[sof*(3*(lw*y + x) + 1)], sof);
+            memcpy(&frame->data[1][sof*(lw*y + x)], &pkt->data[sof*(3*(lw*y + x) + 2)], sof);
+        }
+    }
+    return pkt->size;
+}
+
+static int half_add_alpha(AVCodecContext *avctx, AVFrame *frame, const AVPacket *pkt)
+{
+    /* ffmpeg doesn't provide RGB half bit depth without alpha channel right now
+     * we simply add an opaque alpha layer as workaround */
+
+    int x, y, lw;
+    const size_t soh = 2;
+    const uint16_t opaque = 0x3c00;
+
+    lw = frame->width;
+
+    for(y = 0; y < frame->height; y++){
+        for(x = 0; x < frame->width; x++){
+            memcpy(&frame->data[0][soh*4*(lw*y + x)], &pkt->data[soh*3*(lw*y + x)], soh*3);
+            memcpy(&frame->data[0][soh*(4*(lw*y + x) + 3)], &opaque, soh);
+        }
+    }
+    return pkt->size;
+}
+
+/* DNxUncompressed utilizes a very dense bitpack representation of 10bit and 12bit pixel data.
+
+Lines of Image data, which look like in their ordinary 8bit counterpart, contain the most
+significant upper bits of the pixel data. These sections alternate with shorter segments in
+which the complementary least significant bits of information get packed in a gapless sequence.
+
++----------------------+ +----------------------+ +------------------------+ +----------~
+|  8 m.s.bits of R[1]  | |  8 m.s.bits of G[1]  | |  8 m.s.bits of B[1]    | | msb R[2]    ... one line
++----------------------+ +----------------------+ +------------------------+ +----------~
+
++---------------------------------------------------------------+  +-----------~
+| +------------+ +------------+ +------------+ +--------------+ |  | +--------~
+| | 2 lsb R[2] | | 2 lsb B[1] | | 2 lsb G[1] | | 2 lsb R[1]   | |  | | G[3]lsb    ... LSB bits for line
+| +------------+ +------------+ +------------+ +--------------+ |  | +--------~
++---------------------------- one byte ------------------------ +  +-----------~
+
+next line of MSB bytes...   */
+
+static int unpack_rg10(AVCodecContext *avctx, AVFrame *frame, const AVPacket *pkt)
+{
+    int x, y, lw, msp, pack, lsp, p_off;
+    uint16_t r,g,b;
+
+    lw = frame->width;
+
+    for(y = 0; y < frame->height; y++){
+        for(x = 0; x < frame->width; x++){
+            msp = pkt->data[y*3*(lw + lw/4) + 3*x];
+            p_off = y*(3*(lw + lw/4)) + 3*lw + 3*x/4;
+            pack = pkt->data[p_off];
+            lsp = (pack >> (3*x%4)*2) & 0x3;
+            r = (msp << 2) + lsp;
+            // av_log(0, AV_LOG_DEBUG, "r: %04x, %02x, %02x, %02x, %d\n",
+            //    r, msp, lsp, pack, p_off);
+
+            msp = pkt->data[y*3*(lw + lw/4) + 3*x + 1];
+            p_off = y*(3*(lw + lw/4)) + 3*lw + (3*x+1)/4;
+            pack = pkt->data[p_off];
+            lsp = (pack >> ((3*x+1)%4)*2) & 0x3;
+            g = (msp << 2) + lsp;
+            // av_log(0, AV_LOG_DEBUG, "g: %04x, %02x, %02x, %02x, %d\n",
+            //    g, msp, lsp, pack, p_off);
+
+            msp = pkt->data[y*3*(lw + lw/4) + 3*x + 2];
+            p_off = y*(3*(lw + lw/4)) + 3*lw + (3*x+2)/4;
+            pack = pkt->data[p_off];
+            lsp = (pack >> ((3*x+2)%4)*2) & 0x3;
+            b = (msp << 2) + lsp;
+            // av_log(0, AV_LOG_DEBUG, "b: %04x, %02x, %02x, %02x, %d\n\n",
+            //    b, msp, lsp, pack, p_off);
+
+            memcpy(&frame->data[2][2*(y*lw + x)], &r, 2);
+            memcpy(&frame->data[0][2*(y*lw + x)], &g, 2);
+            memcpy(&frame->data[1][2*(y*lw + x)], &b, 2);
+        }
+    }
+    return pkt->size;
+}
+
+static int unpack_rg12(AVCodecContext *avctx, AVFrame *frame, const AVPacket *pkt)
+{
+    int x, y, lw, msp, pack, lsp, p_off;
+    uint16_t r,g,b;
+
+    lw = frame->width;
+
+    for(y = 0; y < frame->height; y++){
+        for(x = 0; x < frame->width; x++){
+            msp = pkt->data[y*3*(lw + lw/2) + 3*x];
+            p_off = y*(3*(lw + lw/2)) + 3*lw + 3*x/2;
+            pack = pkt->data[p_off];
+            lsp = (pack >> (3*x%2)*4) & 0xf;
+            r = (msp << 4) + lsp;
+            // av_log(0, AV_LOG_DEBUG, "r: %04x, %02x, %02x, %02x, %d\n",
+            //    r, msp, lsp, pack, p_off);
+
+            msp = pkt->data[y*3*(lw + lw/2) + 3*x + 1];
+            p_off =y*(3*(lw + lw/2)) + 3*lw + (3*x+1)/2;
+            pack = pkt->data[p_off];
+            lsp = (pack >> ((3*x+1)%2)*4) & 0xf;
+            g = (msp << 4) + lsp;
+            // av_log(0, AV_LOG_DEBUG, "g: %04x, %02x, %02x, %02x, %d\n",
+            //    g, msp, lsp, pack, p_off);
+
+            msp = pkt->data[y*3*(lw + lw/2) + 3*x + 2];
+            p_off = y*(3*(lw + lw/2)) + 3*lw + (3*x+2)/2;
+            pack = pkt->data[p_off];
+            lsp = (pack >> ((3*x+2)%2)*4) & 0xf;
+            b = (msp << 4) + lsp;
+            // av_log(0, AV_LOG_DEBUG, "b: %04x, %02x, %02x, %02x, %d\n\n",
+            //    b, msp, lsp, pack, p_off);
+
+            memcpy(&frame->data[2][2*(y*lw + x)], &r, 2);
+            memcpy(&frame->data[0][2*(y*lw + x)], &g, 2);
+            memcpy(&frame->data[1][2*(y*lw + x)], &b, 2);
+        }
+    }
+    return pkt->size;
+}
+
+
+static int unpack_y210(AVCodecContext *avctx, AVFrame *frame, const AVPacket *pkt)
+{
+    int x, y, lw, msp, pack, lsp, p_off;
+    uint16_t y1, y2, u, v;
+
+    lw = frame->width;
+
+    for(y = 0; y < frame->height; y++){
+        for(x = 0; x < frame->width; x += 2){
+
+            p_off = y*(2*(lw + lw/4)) + 2*lw + x/2;
+            pack = pkt->data[p_off];
+
+            msp = pkt->data[y*2*(lw + lw/4) + 2*x];
+            lsp = pack & 0x3;
+            u = (msp << 2) + lsp;
+            // av_log(0, AV_LOG_DEBUG, " u: %04x, %02x, %02x, %02x, %d\n",
+            //    u, msp, lsp, pack, p_off);
+
+            msp = pkt->data[y*2*(lw + lw/4) + 2*x + 1];
+            lsp = (pack >> 2) & 0x3;
+            y1 = (msp << 2) + lsp;
+            // av_log(0, AV_LOG_DEBUG, "y1: %04x, %02x, %02x, %02x, %d\n",
+            //    y1, msp, lsp, pack, p_off);
+
+            msp = pkt->data[y*2*(lw + lw/4) + 2*x + 2];
+            lsp = (pack >> 4) & 0x3;
+            v = (msp << 2) + lsp;
+            // av_log(0, AV_LOG_DEBUG, " v: %04x, %02x, %02x, %02x, %d\n",
+            //    v, msp, lsp, pack, p_off);
+
+            msp = pkt->data[y*2*(lw + lw/4) + 2*x + 3];
+            lsp = (pack >> 6) & 0x3;
+            y2 = (msp << 2) + lsp;
+            // av_log(0, AV_LOG_DEBUG, "y2: %04x, %02x, %02x, %02x, %d\n\n",
+            //    y2, msp, lsp, pack, p_off);
+
+            memcpy(&frame->data[0][2*(y*lw + x)], &y1, 2);
+            memcpy(&frame->data[0][2*(y*lw + x+1)], &y2, 2);
+            memcpy(&frame->data[1][2*(y*lw/2 + x/2)], &u, 2);
+            memcpy(&frame->data[2][2*(y*lw/2 + x/2)], &v, 2);
+        }
+    }
+    return pkt->size;
+}
+
+
+static int unpack_y212(AVCodecContext *avctx, AVFrame *frame, const AVPacket *pkt)
+{
+    int x, y, lw, msp, pack, lsp, p_off;
+    uint16_t y1, y2, u, v;
+
+    lw = frame->width;
+
+    for(y = 0; y < frame->height; y++){
+        for(x = 0; x < frame->width; x += 2){
+
+            p_off = y*(2*(lw + lw/2)) + 2*lw + x;
+            pack = pkt->data[p_off];
+
+            msp = pkt->data[y*2*(lw + lw/2) + 2*x];
+            lsp = pack & 0xf;
+            u = (msp << 4) + lsp;
+            // av_log(0, AV_LOG_DEBUG, " u: %04x, %02x, %02x, %02x, %d\n",
+            //    u, msp, lsp, pack, p_off);
+
+            msp = pkt->data[y*2*(lw + lw/2) + 2*x + 1];
+            lsp = (pack >> 4) & 0xf;
+            y1 = (msp << 4) + lsp;
+            // av_log(0, AV_LOG_DEBUG, "y1: %04x, %02x, %02x, %02x, %d\n",
+            //    y1, msp, lsp, pack, p_off);
+
+            p_off = y*(2*(lw + lw/2)) + 2*lw + x+1;
+            pack = pkt->data[p_off];
+
+            msp = pkt->data[y*2*(lw + lw/2) + 2*x + 2];
+            lsp = pack & 0xf;
+            v = (msp << 4) + lsp;
+            // av_log(0, AV_LOG_DEBUG, " v: %04x, %02x, %02x, %02x, %d\n",
+            //    v, msp, lsp, pack, p_off);
+
+            msp = pkt->data[y*2*(lw + lw/2) + 2*x + 3];
+            lsp = (pack >> 4) & 0xf;
+            y2 = (msp << 4) + lsp;
+            // av_log(0, AV_LOG_DEBUG, "y2: %04x, %02x, %02x, %02x, %d\n\n",
+            //    y2, msp, lsp, pack, p_off);
+
+            memcpy(&frame->data[0][2*(y*lw + x)], &y1, 2);
+            memcpy(&frame->data[0][2*(y*lw + x+1)], &y2, 2);
+            memcpy(&frame->data[1][2*(y*lw/2 + x/2)], &u, 2);
+            memcpy(&frame->data[2][2*(y*lw/2 + x/2)], &v, 2);
+        }
+    }
+    return pkt->size;
+}
+
+static int check_pkt_size(AVCodecContext *avctx, AVPacket *avpkt, int bpp)
+{
+    int needed = ((avctx->width * bpp + 7) / 8) * avctx->height;
+    if (avpkt->size < needed){
+        av_log(avctx, AV_LOG_ERROR,
+        "Insufficient size of AVPacket data (pkg size: %d needed: %d)\n", avpkt->size, needed);
+        return AVERROR_EXIT;
+    }
+    return 0;
+}
+
+static int fmt_frame(AVCodecContext *avctx, AVFrame *frame, AVPacket *avpkt,
+                    enum AVPixelFormat pix_fmt, int src_bpp,
+                    int (*frame_handler)(AVCodecContext *avctx, AVFrame *frame, const AVPacket *avpkt))
+{
+    int ret;
+    avctx->pix_fmt = pix_fmt;
+
+    ret = check_pkt_size(avctx, avpkt, src_bpp);
+    if (ret)
+        return ret;
+
+    ret = ff_thread_get_buffer(avctx, frame, 0);
+    if (ret < 0)
+        return ret;
+
+    return frame_handler(avctx, frame, avpkt);
+}
+
+static int dnxuc_decode_frame(AVCodecContext *avctx, AVFrame *frame,
+                             int *got_frame, AVPacket *avpkt)
+{
+    char fourcc_buf[5];
+    int ret;
+
+    av_fourcc_make_string(fourcc_buf, avctx->codec_tag);
+    if ((avctx->width % 2) && ((fourcc_buf[0] == 'y' && fourcc_buf[1] == '2')
+                             ||(fourcc_buf[1] == 'y' && fourcc_buf[2] == '2'))){
+        av_log(avctx, AV_LOG_ERROR,
+        "Image width must be a multiple of 2 for YUV 4:2:2 DNxUncompressed!\n");
+        return AVERROR_EXIT;
+    }
+
+    switch (avctx->codec_tag) {
+        case MKTAG('r','g','0','8'):
+            ret = fmt_frame(avctx, frame, avpkt, AV_PIX_FMT_RGB24, 24, pass_though);
+            break;
+        case MKTAG('r','g','1','0'):
+            ret = fmt_frame(avctx, frame, avpkt, AV_PIX_FMT_GBRP10LE, 30, unpack_rg10);
+            break;
+        case MKTAG('r','g','1','2'):
+            ret = fmt_frame(avctx, frame, avpkt, AV_PIX_FMT_GBRP12LE, 36, unpack_rg12);
+            break;
+        case MKTAG(' ','r','g','h'):
+            ret = fmt_frame(avctx, frame, avpkt, AV_PIX_FMT_RGBAF16LE, 48, half_add_alpha);
+            break;
+        case MKTAG(' ','r','g','f'):
+            ret = fmt_frame(avctx, frame, avpkt, AV_PIX_FMT_GBRPF32LE, 96, float2planes);
+            break;
+
+        case MKTAG('y','2','0','8'):
+            ret = fmt_frame(avctx, frame, avpkt, AV_PIX_FMT_UYVY422, 16, pass_though);
+            break;
+        case MKTAG('y','2','1','0'):
+            ret = fmt_frame(avctx, frame, avpkt, AV_PIX_FMT_YUV422P10LE, 20, unpack_y210);
+            break;
+        case MKTAG('y','2','1','2'):
+            ret = fmt_frame(avctx, frame, avpkt, AV_PIX_FMT_YUV422P12LE, 24, unpack_y212);
+            break;
+
+        default:
+            av_log(avctx, AV_LOG_ERROR,
+            "Unsupported DNxUncompressed pixel format variant: '%s'\n",
+            fourcc_buf);
+            return AVERROR(ENOSYS);
+    }
+
+    if (ret < 0) {
+        av_buffer_unref(&frame->buf[0]);
+        return ret;
+    }
+
+    *got_frame = 1;
+
+    return avpkt->size;
+}
+
+const FFCodec ff_dnxuc_decoder = {
+    .p.name         = "dnxuc",
+    CODEC_LONG_NAME("DNxUncompressed (SMPTE RDD 50)"),
+    .p.type         = AVMEDIA_TYPE_VIDEO,
+    .p.id             = AV_CODEC_ID_DNXUC,
+    .init           = dnxuc_decode_init,
+    FF_CODEC_DECODE_CB(dnxuc_decode_frame),
+    .p.capabilities = AV_CODEC_CAP_FRAME_THREADS,
+};
diff --git a/libavcodec/version.c b/libavcodec/version.c
index 27f9432..03dd95e 100644
--- a/libavcodec/version.c
+++ b/libavcodec/version.c
@@ -31,7 +31,7 @@  const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
 
 unsigned avcodec_version(void)
 {
-    static_assert(AV_CODEC_ID_LEAD         ==   269 &&
+    static_assert(AV_CODEC_ID_DNXUC        ==   270 &&
                   AV_CODEC_ID_PCM_SGA      == 65572 &&
                   AV_CODEC_ID_ADPCM_XMD    == 69683 &&
                   AV_CODEC_ID_CBD2_DPCM    == 81928 &&