diff options
author | Paul B Mahol <onemda@gmail.com> | 2020-09-14 11:35:26 +0200 |
---|---|---|
committer | Paul B Mahol <onemda@gmail.com> | 2020-09-24 13:33:29 +0200 |
commit | 5c2d7acb4f99d688b174103972934d10013c085a (patch) | |
tree | ba3be4b9ecf219a91cd240f35834b7b0d34ff548 /libavcodec/mpeg12dec.c | |
parent | aed8f40d45683c8febfbf98a7863352ff0530309 (diff) | |
download | ffmpeg-5c2d7acb4f99d688b174103972934d10013c085a.tar.gz |
avcodec: add IPU Video decoder and parser
Diffstat (limited to 'libavcodec/mpeg12dec.c')
-rw-r--r-- | libavcodec/mpeg12dec.c | 155 |
1 files changed, 155 insertions, 0 deletions
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, +}; |