--- /dev/null
+/*
+ * 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"
+
+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,
+};
.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,
+};