yading@10: /* yading@10: * Intel Indeo 2 codec yading@10: * Copyright (c) 2005 Konstantin Shishkov yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * Intel Indeo 2 decoder. yading@10: */ yading@10: yading@10: #define BITSTREAM_READER_LE yading@10: #include "libavutil/attributes.h" yading@10: #include "avcodec.h" yading@10: #include "get_bits.h" yading@10: #include "indeo2data.h" yading@10: #include "internal.h" yading@10: #include "mathops.h" yading@10: yading@10: typedef struct Ir2Context{ yading@10: AVCodecContext *avctx; yading@10: AVFrame picture; yading@10: GetBitContext gb; yading@10: int decode_delta; yading@10: } Ir2Context; yading@10: yading@10: #define CODE_VLC_BITS 14 yading@10: static VLC ir2_vlc; yading@10: yading@10: /* Indeo 2 codes are in range 0x01..0x7F and 0x81..0x90 */ yading@10: static inline int ir2_get_code(GetBitContext *gb) yading@10: { yading@10: return get_vlc2(gb, ir2_vlc.table, CODE_VLC_BITS, 1) + 1; yading@10: } yading@10: yading@10: static int ir2_decode_plane(Ir2Context *ctx, int width, int height, uint8_t *dst, yading@10: int stride, const uint8_t *table) yading@10: { yading@10: int i; yading@10: int j; yading@10: int out = 0; yading@10: int c; yading@10: int t; yading@10: yading@10: if (width & 1) yading@10: return AVERROR_INVALIDDATA; yading@10: yading@10: /* first line contain absolute values, other lines contain deltas */ yading@10: while (out < width) { yading@10: c = ir2_get_code(&ctx->gb); yading@10: if (c >= 0x80) { /* we have a run */ yading@10: c -= 0x7F; yading@10: if (out + c*2 > width) yading@10: return AVERROR_INVALIDDATA; yading@10: for (i = 0; i < c * 2; i++) yading@10: dst[out++] = 0x80; yading@10: } else { /* copy two values from table */ yading@10: dst[out++] = table[c * 2]; yading@10: dst[out++] = table[(c * 2) + 1]; yading@10: } yading@10: } yading@10: dst += stride; yading@10: yading@10: for (j = 1; j < height; j++) { yading@10: out = 0; yading@10: while (out < width) { yading@10: c = ir2_get_code(&ctx->gb); yading@10: if (c >= 0x80) { /* we have a skip */ yading@10: c -= 0x7F; yading@10: if (out + c*2 > width) yading@10: return AVERROR_INVALIDDATA; yading@10: for (i = 0; i < c * 2; i++) { yading@10: dst[out] = dst[out - stride]; yading@10: out++; yading@10: } yading@10: } else { /* add two deltas from table */ yading@10: t = dst[out - stride] + (table[c * 2] - 128); yading@10: t = av_clip_uint8(t); yading@10: dst[out] = t; yading@10: out++; yading@10: t = dst[out - stride] + (table[(c * 2) + 1] - 128); yading@10: t = av_clip_uint8(t); yading@10: dst[out] = t; yading@10: out++; yading@10: } yading@10: } yading@10: dst += stride; yading@10: } yading@10: return 0; yading@10: } yading@10: yading@10: static int ir2_decode_plane_inter(Ir2Context *ctx, int width, int height, uint8_t *dst, yading@10: int stride, const uint8_t *table) yading@10: { yading@10: int j; yading@10: int out = 0; yading@10: int c; yading@10: int t; yading@10: yading@10: if (width & 1) yading@10: return AVERROR_INVALIDDATA; yading@10: yading@10: for (j = 0; j < height; j++) { yading@10: out = 0; yading@10: while (out < width) { yading@10: c = ir2_get_code(&ctx->gb); yading@10: if (c >= 0x80) { /* we have a skip */ yading@10: c -= 0x7F; yading@10: out += c * 2; yading@10: } else { /* add two deltas from table */ yading@10: t = dst[out] + (((table[c * 2] - 128)*3) >> 2); yading@10: t = av_clip_uint8(t); yading@10: dst[out] = t; yading@10: out++; yading@10: t = dst[out] + (((table[(c * 2) + 1] - 128)*3) >> 2); yading@10: t = av_clip_uint8(t); yading@10: dst[out] = t; yading@10: out++; yading@10: } yading@10: } yading@10: dst += stride; yading@10: } yading@10: return 0; yading@10: } yading@10: yading@10: static int ir2_decode_frame(AVCodecContext *avctx, yading@10: void *data, int *got_frame, yading@10: AVPacket *avpkt) yading@10: { yading@10: Ir2Context * const s = avctx->priv_data; yading@10: const uint8_t *buf = avpkt->data; yading@10: int buf_size = avpkt->size; yading@10: AVFrame *picture = data; yading@10: AVFrame * const p = &s->picture; yading@10: int start, ret; yading@10: yading@10: if ((ret = ff_reget_buffer(avctx, p)) < 0) yading@10: return ret; yading@10: yading@10: start = 48; /* hardcoded for now */ yading@10: yading@10: if (start >= buf_size) { yading@10: av_log(s->avctx, AV_LOG_ERROR, "input buffer size too small (%d)\n", buf_size); yading@10: return AVERROR_INVALIDDATA; yading@10: } yading@10: yading@10: s->decode_delta = buf[18]; yading@10: yading@10: /* decide whether frame uses deltas or not */ yading@10: #ifndef BITSTREAM_READER_LE yading@10: for (i = 0; i < buf_size; i++) yading@10: buf[i] = ff_reverse[buf[i]]; yading@10: #endif yading@10: yading@10: init_get_bits(&s->gb, buf + start, (buf_size - start) * 8); yading@10: yading@10: if (s->decode_delta) { /* intraframe */ yading@10: if ((ret = ir2_decode_plane(s, avctx->width, avctx->height, yading@10: s->picture.data[0], s->picture.linesize[0], yading@10: ir2_luma_table)) < 0) yading@10: return ret; yading@10: yading@10: /* swapped U and V */ yading@10: if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2, yading@10: s->picture.data[2], s->picture.linesize[2], yading@10: ir2_luma_table)) < 0) yading@10: return ret; yading@10: if ((ret = ir2_decode_plane(s, avctx->width >> 2, avctx->height >> 2, yading@10: s->picture.data[1], s->picture.linesize[1], yading@10: ir2_luma_table)) < 0) yading@10: return ret; yading@10: } else { /* interframe */ yading@10: if ((ret = ir2_decode_plane_inter(s, avctx->width, avctx->height, yading@10: s->picture.data[0], s->picture.linesize[0], yading@10: ir2_luma_table)) < 0) yading@10: return ret; yading@10: /* swapped U and V */ yading@10: if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2, yading@10: s->picture.data[2], s->picture.linesize[2], yading@10: ir2_luma_table)) < 0) yading@10: return ret; yading@10: if ((ret = ir2_decode_plane_inter(s, avctx->width >> 2, avctx->height >> 2, yading@10: s->picture.data[1], s->picture.linesize[1], yading@10: ir2_luma_table)) < 0) yading@10: return ret; yading@10: } yading@10: yading@10: if ((ret = av_frame_ref(picture, &s->picture)) < 0) yading@10: return ret; yading@10: yading@10: *got_frame = 1; yading@10: yading@10: return buf_size; yading@10: } yading@10: yading@10: static av_cold int ir2_decode_init(AVCodecContext *avctx) yading@10: { yading@10: Ir2Context * const ic = avctx->priv_data; yading@10: static VLC_TYPE vlc_tables[1 << CODE_VLC_BITS][2]; yading@10: yading@10: avcodec_get_frame_defaults(&ic->picture); yading@10: ic->avctx = avctx; yading@10: yading@10: avctx->pix_fmt= AV_PIX_FMT_YUV410P; yading@10: yading@10: avcodec_get_frame_defaults(&ic->picture); yading@10: yading@10: ir2_vlc.table = vlc_tables; yading@10: ir2_vlc.table_allocated = 1 << CODE_VLC_BITS; yading@10: #ifdef BITSTREAM_READER_LE yading@10: init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES, yading@10: &ir2_codes[0][1], 4, 2, yading@10: &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | INIT_VLC_LE); yading@10: #else yading@10: init_vlc(&ir2_vlc, CODE_VLC_BITS, IR2_CODES, yading@10: &ir2_codes[0][1], 4, 2, yading@10: &ir2_codes[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC); yading@10: #endif yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static av_cold int ir2_decode_end(AVCodecContext *avctx) yading@10: { yading@10: Ir2Context * const ic = avctx->priv_data; yading@10: AVFrame *pic = &ic->picture; yading@10: yading@10: av_frame_unref(pic); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: AVCodec ff_indeo2_decoder = { yading@10: .name = "indeo2", yading@10: .type = AVMEDIA_TYPE_VIDEO, yading@10: .id = AV_CODEC_ID_INDEO2, yading@10: .priv_data_size = sizeof(Ir2Context), yading@10: .init = ir2_decode_init, yading@10: .close = ir2_decode_end, yading@10: .decode = ir2_decode_frame, yading@10: .capabilities = CODEC_CAP_DR1, yading@10: .long_name = NULL_IF_CONFIG_SMALL("Intel Indeo 2"), yading@10: };