[FFmpeg-devel,RFC,WIP] avcodec: add IMM5 decoder

Submitted by Paul B Mahol on Sept. 14, 2018, 3:55 p.m.

Details

Message ID 20180914155533.4413-1-onemda@gmail.com
State New
Headers show

Commit Message

Paul B Mahol Sept. 14, 2018, 3:55 p.m.
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavcodec/Makefile     |  5 ++++
 libavcodec/allcodecs.c  |  1 +
 libavcodec/avcodec.h    |  1 +
 libavcodec/codec_desc.c |  7 +++++
 libavcodec/h264dec.c    | 58 ++++++++++++++++++++++++++++++++++++++++-
 libavcodec/h264dec.h    |  3 +++
 libavformat/riff.c      |  1 +
 7 files changed, 75 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3e16a13004..22f456bda3 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -386,6 +386,11 @@  OBJS-$(CONFIG_IDF_DECODER)             += bintext.o cga_data.o
 OBJS-$(CONFIG_IFF_ILBM_DECODER)        += iff.o
 OBJS-$(CONFIG_IMC_DECODER)             += imc.o
 OBJS-$(CONFIG_IMM4_DECODER)            += imm4.o
+OBJS-$(CONFIG_IMM5_DECODER)            += h264dec.o h264_cabac.o h264_cavlc.o \
+                                          h264_direct.o h264_loopfilter.o  \
+                                          h264_mb.o h264_picture.o \
+                                          h264_refs.o h264_sei.o \
+                                          h264_slice.o h264data.o
 OBJS-$(CONFIG_INDEO2_DECODER)          += indeo2.o
 OBJS-$(CONFIG_INDEO3_DECODER)          += indeo3.o
 OBJS-$(CONFIG_INDEO4_DECODER)          += indeo4.o ivi.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 7dbfcb3dda..29bf718fd4 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -155,6 +155,7 @@  extern AVCodec ff_huffyuv_decoder;
 extern AVCodec ff_idcin_decoder;
 extern AVCodec ff_iff_ilbm_decoder;
 extern AVCodec ff_imm4_decoder;
+extern AVCodec ff_imm5_decoder;
 extern AVCodec ff_indeo2_decoder;
 extern AVCodec ff_indeo3_decoder;
 extern AVCodec ff_indeo4_decoder;
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 705a3ce4f3..356cf28325 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -452,6 +452,7 @@  enum AVCodecID {
     AV_CODEC_ID_MWSC,
     AV_CODEC_ID_WCMV,
     AV_CODEC_ID_RASC,
+    AV_CODEC_ID_IMM5,
 
     /* various PCM "codecs" */
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 67a30542d1..e630109db4 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1689,6 +1689,13 @@  static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("RemotelyAnywhere Screen Capture"),
         .props     = AV_CODEC_PROP_LOSSY,
     },
+    {
+        .id        = AV_CODEC_ID_IMM5,
+        .type      = AVMEDIA_TYPE_VIDEO,
+        .name      = "imm5",
+        .long_name = NULL_IF_CONFIG_SMALL("Infinity IMM5"),
+        .props     = AV_CODEC_PROP_LOSSY,
+    },
 
     /* various PCM "codecs" */
     {
diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
index 7b4c5c76ea..19e1e743cf 100644
--- a/libavcodec/h264dec.c
+++ b/libavcodec/h264dec.c
@@ -412,7 +412,7 @@  static av_cold int h264_decode_init(AVCodecContext *avctx)
     }
     avctx->ticks_per_frame = 2;
 
-    if (avctx->extradata_size > 0 && avctx->extradata) {
+    if (avctx->codec_id != AV_CODEC_ID_IMM5 && avctx->extradata_size > 0 && avctx->extradata) {
         ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
                                        &h->ps, &h->is_avc, &h->nal_length_size,
                                        avctx->err_recognition, avctx);
@@ -958,6 +958,26 @@  static int send_next_delayed_frame(H264Context *h, AVFrame *dst_frame,
     return buf_index;
 }
 
+static const struct IMM5_unit {
+    uint8_t bits[14];
+    uint8_t len;
+} IMM5_units[14] = {
+    { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x0B, 0x0F, 0x88 }, 12 },
+    { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x83, 0xE2 }, 12 },
+    { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x81, 0xE8, 0x80 }, 13 },
+    { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x0B, 0x04, 0xA2 }, 12 },
+    { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x81, 0x28, 0x80 }, 13 },
+    { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x1E, 0xF4, 0x05, 0x80, 0x92, 0x20 }, 13 },
+    { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x0B, 0x0F, 0xC8 }, 13 },
+    { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x83, 0xF2 }, 13 },
+    { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x81, 0xEC, 0x80 }, 14 },
+    { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x0B, 0x04, 0xB2 }, 13 },
+    { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x81, 0x2C, 0x80 }, 14 },
+    { { 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1E, 0x9A, 0x74, 0x05, 0x80, 0x93, 0x20 }, 14 },
+    { { 0x00, 0x00, 0x00, 0x01, 0x68, 0xDE, 0x3C, 0x80 }, 8 },
+    { { 0x00, 0x00, 0x00, 0x01, 0x68, 0xCE, 0x32, 0x28 }, 8 },
+};
+
 static int h264_decode_frame(AVCodecContext *avctx, void *data,
                              int *got_frame, AVPacket *avpkt)
 {
@@ -968,6 +988,26 @@  static int h264_decode_frame(AVCodecContext *avctx, void *data,
     int buf_index;
     int ret;
 
+    if (avctx->codec_id == AV_CODEC_ID_IMM5) {
+        if (avpkt->size > 24) {
+            int index = avpkt->data[10];
+            int offset;
+
+            if (index < 1 || index > 12)
+                return AVERROR_INVALIDDATA;
+            index -= 1;
+
+            av_fast_padded_malloc(&h->imm5_buffer, &h->imm5_buffer_size, buf_size);
+            memcpy(h->imm5_buffer, IMM5_units[index].bits, IMM5_units[index].len);
+            offset = IMM5_units[index].len;
+            memcpy(h->imm5_buffer + offset, IMM5_units[12].bits, IMM5_units[12].len);
+            offset += IMM5_units[12].len;
+            memcpy(h->imm5_buffer + offset, avpkt->data + 24, buf_size - 24);
+            buf = h->imm5_buffer;
+            buf_size = buf_size - 24 + offset;
+        }
+    }
+
     h->flags = avctx->flags;
     h->setup_finished = 0;
     h->nb_slice_ctx_queued = 0;
@@ -1090,3 +1130,19 @@  AVCodec ff_h264_decoder = {
     .profiles              = NULL_IF_CONFIG_SMALL(ff_h264_profiles),
     .priv_class            = &h264_class,
 };
+
+AVCodec ff_imm5_decoder = {
+    .name                  = "imm5",
+    .long_name             = NULL_IF_CONFIG_SMALL("Infinity IMM5"),
+    .type                  = AVMEDIA_TYPE_VIDEO,
+    .id                    = AV_CODEC_ID_IMM5,
+    .priv_data_size        = sizeof(H264Context),
+    .init                  = h264_decode_init,
+    .close                 = h264_decode_end,
+    .decode                = h264_decode_frame,
+    .capabilities          = /*AV_CODEC_CAP_DRAW_HORIZ_BAND |*/ AV_CODEC_CAP_DR1 |
+                             AV_CODEC_CAP_DELAY,
+    .caps_internal         = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_EXPORTS_CROPPING,
+    .flush                 = flush_dpb,
+    .profiles              = NULL_IF_CONFIG_SMALL(ff_h264_profiles),
+};
diff --git a/libavcodec/h264dec.h b/libavcodec/h264dec.h
index 1d9723260d..1e72781b3f 100644
--- a/libavcodec/h264dec.h
+++ b/libavcodec/h264dec.h
@@ -353,6 +353,9 @@  typedef struct H264Context {
 
     H2645Packet pkt;
 
+    uint8_t *imm5_buffer;
+    int imm5_buffer_size;
+
     int pixel_shift;    ///< 0 for 8-bit H.264, 1 for high-bit-depth H.264
 
     /* coded dimensions -- 16 * mb w/h */
diff --git a/libavformat/riff.c b/libavformat/riff.c
index 3907e1a9f3..583c0158d8 100644
--- a/libavformat/riff.c
+++ b/libavformat/riff.c
@@ -475,6 +475,7 @@  const AVCodecTag ff_codec_bmp_tags[] = {
     { AV_CODEC_ID_MWSC,         MKTAG('M', 'W', 'S', 'C') },
     { AV_CODEC_ID_WCMV,         MKTAG('W', 'C', 'M', 'V') },
     { AV_CODEC_ID_RASC,         MKTAG('R', 'A', 'S', 'C') },
+    { AV_CODEC_ID_IMM5,         MKTAG('I', 'M', 'M', '5') },
     { AV_CODEC_ID_NONE,         0 }
 };