[FFmpeg-devel] [PATCH 1/2] avcodec: add IPU Video decoder and parser

Paul B Mahol onemda at gmail.com
Mon Sep 14 18:24:26 EEST 2020


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 libavcodec/Makefile     |   2 +
 libavcodec/allcodecs.c  |   1 +
 libavcodec/codec_desc.c |   7 ++
 libavcodec/codec_id.h   |   1 +
 libavcodec/ipu_parser.c |  78 ++++++++++++++++++++
 libavcodec/ipudec.c     |   0
 libavcodec/mpeg12dec.c  | 155 ++++++++++++++++++++++++++++++++++++++++
 libavcodec/parsers.c    |   1 +
 8 files changed, 245 insertions(+)
 create mode 100644 libavcodec/ipu_parser.c
 create mode 100644 libavcodec/ipudec.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3239a752ac..b6b743133f 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -419,6 +419,7 @@ OBJS-$(CONFIG_INDEO5_DECODER)          += indeo5.o ivi.o
 OBJS-$(CONFIG_INTERPLAY_ACM_DECODER)   += interplayacm.o
 OBJS-$(CONFIG_INTERPLAY_DPCM_DECODER)  += dpcm.o
 OBJS-$(CONFIG_INTERPLAY_VIDEO_DECODER) += interplayvideo.o
+OBJS-$(CONFIG_IPU_DECODER)             += mpeg12dec.o mpeg12.o mpeg12data.o
 OBJS-$(CONFIG_JACOSUB_DECODER)         += jacosubdec.o ass.o
 OBJS-$(CONFIG_JPEG2000_ENCODER)        += j2kenc.o mqcenc.o mqc.o jpeg2000.o \
                                           jpeg2000dwt.o
@@ -1086,6 +1087,7 @@ OBJS-$(CONFIG_H261_PARSER)             += h261_parser.o
 OBJS-$(CONFIG_H263_PARSER)             += h263_parser.o
 OBJS-$(CONFIG_H264_PARSER)             += h264_parser.o h264_sei.o h264data.o
 OBJS-$(CONFIG_HEVC_PARSER)             += hevc_parser.o hevc_data.o
+OBJS-$(CONFIG_IPU_PARSER)              += ipu_parser.o
 OBJS-$(CONFIG_JPEG2000_PARSER)         += jpeg2000_parser.o
 OBJS-$(CONFIG_MJPEG_PARSER)            += mjpeg_parser.o
 OBJS-$(CONFIG_MLP_PARSER)              += mlp_parse.o mlp_parser.o mlp.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 713c5686a4..17c88d6290 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -166,6 +166,7 @@ extern AVCodec ff_indeo3_decoder;
 extern AVCodec ff_indeo4_decoder;
 extern AVCodec ff_indeo5_decoder;
 extern AVCodec ff_interplay_video_decoder;
+extern AVCodec ff_ipu_decoder;
 extern AVCodec ff_jpeg2000_encoder;
 extern AVCodec ff_jpeg2000_decoder;
 extern AVCodec ff_jpegls_encoder;
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 9e73dcba27..002ec86fa6 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1798,6 +1798,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("Kodak Photo CD"),
         .props     = AV_CODEC_PROP_LOSSY,
     },
+    {
+        .id        = AV_CODEC_ID_IPU,
+        .type      = AVMEDIA_TYPE_VIDEO,
+        .name      = "ipu",
+        .long_name = NULL_IF_CONFIG_SMALL("IPU Video"),
+        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
+    },
 
     /* various PCM "codecs" */
     {
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index e4eca5d580..e49ba1b28a 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -298,6 +298,7 @@ enum AVCodecID {
     AV_CODEC_ID_PFM,
     AV_CODEC_ID_MOBICLIP,
     AV_CODEC_ID_PHOTOCD,
+    AV_CODEC_ID_IPU,
 
     /* various PCM "codecs" */
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
diff --git a/libavcodec/ipu_parser.c b/libavcodec/ipu_parser.c
new file mode 100644
index 0000000000..3cc6fdd1fe
--- /dev/null
+++ b/libavcodec/ipu_parser.c
@@ -0,0 +1,78 @@
+/*
+ * IPU parser
+ * Copyright (c) 2020 Paul B Mahol
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * IPU parser
+ */
+
+#include "parser.h"
+#include "png.h"
+
+typedef struct IPUParseContext {
+    ParseContext pc;
+} IPUParseContext;
+
+static int ipu_parse(AVCodecParserContext *s, AVCodecContext *avctx,
+                     const uint8_t **poutbuf, int *poutbuf_size,
+                     const uint8_t *buf, int buf_size)
+{
+    IPUParseContext *ipc = s->priv_data;
+    uint32_t state = ipc->pc.state;
+    int next = END_NOT_FOUND, i = 0;
+
+    s->pict_type = AV_PICTURE_TYPE_NONE;
+    s->duration  = 1;
+
+    *poutbuf_size = 0;
+    *poutbuf = NULL;
+
+    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
+        next = buf_size;
+    } else {
+        for (; i < buf_size; i++) {
+            state = (state << 8) | buf[i];
+            if (state == 0x1b0) {
+                next = i + 1;
+                break;
+            }
+        }
+
+        ipc->pc.state = state;
+        if (ff_combine_frame(&ipc->pc, next, &buf, &buf_size) < 0) {
+            *poutbuf = NULL;
+            *poutbuf_size = 0;
+            return buf_size;
+        }
+    }
+
+    *poutbuf      = buf;
+    *poutbuf_size = buf_size;
+
+    return next;
+}
+
+AVCodecParser ff_ipu_parser = {
+    .codec_ids      = { AV_CODEC_ID_IPU },
+    .priv_data_size = sizeof(IPUParseContext),
+    .parser_parse   = ipu_parse,
+    .parser_close   = ff_parse_close,
+};
diff --git a/libavcodec/ipudec.c b/libavcodec/ipudec.c
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index ab470187a1..1dd12196b9 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -2984,3 +2984,158 @@ AVCodec ff_mpegvideo_decoder = {
     .flush          = flush,
     .max_lowres     = 3,
 };
+
+typedef struct IPUContext {
+    MpegEncContext m;
+
+    int flags;
+    DECLARE_ALIGNED(32, int16_t, block)[6][64];
+} IPUContext;
+
+static int ipu_decode_frame(AVCodecContext *avctx, void *data,
+                            int *got_frame, AVPacket *avpkt)
+{
+    IPUContext *s = avctx->priv_data;
+    MpegEncContext *m = &s->m;
+    GetBitContext *gb = &m->gb;
+    AVFrame * const frame = data;
+    int ret;
+
+    ret = ff_get_buffer(avctx, frame, 0);
+    if (ret < 0)
+        return ret;
+
+    ret = init_get_bits8(gb, avpkt->data, avpkt->size);
+    if (ret < 0)
+        return ret;
+
+    s->flags = get_bits(gb, 8);
+    if (!(s->flags & 0x80)) {
+        m->intra_dc_precision = s->flags & 3;
+        m->q_scale_type = !!(s->flags & 0x40);
+        m->intra_vlc_format = !!(s->flags & 0x20);
+        m->alternate_scan = !!(s->flags & 0x10);
+
+        if (s->flags & 0x10) {
+            ff_init_scantable(m->idsp.idct_permutation, &m->inter_scantable, ff_alternate_vertical_scan);
+            ff_init_scantable(m->idsp.idct_permutation, &m->intra_scantable, ff_alternate_vertical_scan);
+        } else {
+            ff_init_scantable(m->idsp.idct_permutation, &m->inter_scantable, ff_zigzag_direct);
+            ff_init_scantable(m->idsp.idct_permutation, &m->intra_scantable, ff_zigzag_direct);
+        }
+
+        m->last_dc[0] = m->last_dc[1] = m->last_dc[2] = 1 << (7 + (s->flags & 3));
+    }
+    m->qscale = 1;
+
+    for (int y = 0; y < avctx->height; y += 16) {
+        int intraquant;
+
+        for (int x = 0; x < avctx->width; x += 16) {
+            if (x || y) {
+                if (!get_bits1(gb))
+                    return AVERROR_INVALIDDATA;
+            }
+            if (get_bits1(gb)) {
+                intraquant = 0;
+            } else {
+                if (!get_bits1(gb))
+                    return AVERROR_INVALIDDATA;
+                intraquant = 1;
+            }
+
+            if (s->flags & 4)
+                skip_bits1(gb);
+
+            if (intraquant)
+                m->qscale = mpeg_get_qscale(m);
+
+            memset(s->block, 0, sizeof(s->block));
+
+            for (int n = 0; n < 6; n++) {
+                if (s->flags & 0x20)
+                    ret = mpeg2_decode_block_intra(m, s->block[n], n);
+                else
+                    ret = mpeg2_decode_block_non_intra(m, s->block[n], n);
+                if (ret < 0)
+                    return ret;
+            }
+
+            m->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x,
+                             frame->linesize[0], s->block[0]);
+            m->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x + 8,
+                             frame->linesize[0], s->block[1]);
+            m->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x,
+                             frame->linesize[0], s->block[2]);
+            m->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8,
+                             frame->linesize[0], s->block[3]);
+            m->idsp.idct_put(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
+                             frame->linesize[1], s->block[4]);
+            m->idsp.idct_put(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
+                             frame->linesize[2], s->block[5]);
+        }
+    }
+
+    align_get_bits(gb);
+    if (get_bits_left(gb) != 32)
+        return AVERROR_INVALIDDATA;
+
+    frame->pict_type = AV_PICTURE_TYPE_I;
+    frame->key_frame = 1;
+    *got_frame = 1;
+
+    return avpkt->size;
+}
+
+static av_cold int ipu_decode_init(AVCodecContext *avctx)
+{
+    IPUContext *s = avctx->priv_data;
+    MpegEncContext *m = &s->m;
+
+    avctx->pix_fmt = AV_PIX_FMT_YUV420P;
+
+    ff_mpv_decode_defaults(m);
+    ff_mpv_decode_init(m, avctx);
+    s->m.avctx = avctx;
+    ff_mpv_idct_init(m);
+    ff_mpeg12_common_init(m);
+    ff_mpeg12_init_vlcs();
+
+    for (int i = 0; i < 64; i++) {
+        int j = m->idsp.idct_permutation[i];
+        int v = ff_mpeg1_default_intra_matrix[i];
+        m->intra_matrix[j]        = v;
+        m->chroma_intra_matrix[j] = v;
+    }
+
+    for (int i = 0; i < 64; i++) {
+        int j = m->idsp.idct_permutation[i];
+        int v = ff_mpeg1_default_non_intra_matrix[i];
+        m->inter_matrix[j]        = v;
+        m->chroma_inter_matrix[j] = v;
+    }
+
+    return 0;
+}
+
+static av_cold int ipu_decode_end(AVCodecContext *avctx)
+{
+    IPUContext *s = avctx->priv_data;
+
+    ff_mpv_common_end(&s->m);
+
+    return 0;
+}
+
+AVCodec ff_ipu_decoder = {
+    .name           = "ipu",
+    .long_name      = NULL_IF_CONFIG_SMALL("IPU Video"),
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_IPU,
+    .priv_data_size = sizeof(IPUContext),
+    .init           = ipu_decode_init,
+    .decode         = ipu_decode_frame,
+    .close          = ipu_decode_end,
+    .capabilities   = AV_CODEC_CAP_DR1,
+    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+};
diff --git a/libavcodec/parsers.c b/libavcodec/parsers.c
index 7d75cea830..d23c603d34 100644
--- a/libavcodec/parsers.c
+++ b/libavcodec/parsers.c
@@ -48,6 +48,7 @@ extern AVCodecParser ff_h261_parser;
 extern AVCodecParser ff_h263_parser;
 extern AVCodecParser ff_h264_parser;
 extern AVCodecParser ff_hevc_parser;
+extern AVCodecParser ff_ipu_parser;
 extern AVCodecParser ff_jpeg2000_parser;
 extern AVCodecParser ff_mjpeg_parser;
 extern AVCodecParser ff_mlp_parser;
-- 
2.17.1



More information about the ffmpeg-devel mailing list