2 * Tiertex Limited SEQ Video Decoder
3 * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Tiertex Limited SEQ video decoder
28 #define BITSTREAM_READER_LE
32 typedef struct SeqVideoContext {
33 AVCodecContext *avctx;
38 static const unsigned char *seq_unpack_rle_block(const unsigned char *src,
39 const unsigned char *src_end,
40 unsigned char *dst, int dst_size)
46 /* get the rle codes */
47 init_get_bits(&gb, src, (src_end - src) * 8);
48 for (i = 0, sz = 0; i < 64 && sz < dst_size; i++) {
49 if (get_bits_left(&gb) < 4)
51 code_table[i] = get_sbits(&gb, 4);
52 sz += FFABS(code_table[i]);
54 src += (get_bits_count(&gb) + 7) / 8;
56 /* do the rle unpacking */
57 for (i = 0; i < 64 && dst_size > 0; i++) {
61 if (src_end - src < 1)
63 memset(dst, *src++, FFMIN(len, dst_size));
65 if (src_end - src < len)
67 memcpy(dst, src, FFMIN(len, dst_size));
76 static const unsigned char *seq_decode_op1(SeqVideoContext *seq,
77 const unsigned char *src,
78 const unsigned char *src_end,
81 const unsigned char *color_table;
84 unsigned char block[8 * 8];
86 if (src_end - src < 1)
92 src = seq_unpack_rle_block(src, src_end, block, sizeof(block));
93 for (b = 0; b < 8; b++) {
94 memcpy(dst, &block[b * 8], 8);
95 dst += seq->frame.linesize[0];
99 src = seq_unpack_rle_block(src, src_end, block, sizeof(block));
100 for (i = 0; i < 8; i++) {
101 for (b = 0; b < 8; b++)
102 dst[b * seq->frame.linesize[0]] = block[i * 8 + b];
110 bits = ff_log2_tab[len - 1] + 1;
111 if (src_end - src < len + 8 * bits)
115 init_get_bits(&gb, src, bits * 8 * 8); src += bits * 8;
116 for (b = 0; b < 8; b++) {
117 for (i = 0; i < 8; i++)
118 dst[i] = color_table[get_bits(&gb, bits)];
119 dst += seq->frame.linesize[0];
126 static const unsigned char *seq_decode_op2(SeqVideoContext *seq,
127 const unsigned char *src,
128 const unsigned char *src_end,
133 if (src_end - src < 8 * 8)
136 for (i = 0; i < 8; i++) {
139 dst += seq->frame.linesize[0];
145 static const unsigned char *seq_decode_op3(SeqVideoContext *seq,
146 const unsigned char *src,
147 const unsigned char *src_end,
153 if (src_end - src < 2)
156 offset = ((pos >> 3) & 7) * seq->frame.linesize[0] + (pos & 7);
157 dst[offset] = *src++;
158 } while (!(pos & 0x80));
163 static int seqvideo_decode(SeqVideoContext *seq, const unsigned char *data, int data_size)
165 const unsigned char *data_end = data + data_size;
167 int flags, i, j, x, y, op;
175 palette = (uint32_t *)seq->frame.data[1];
176 if (data_end - data < 256 * 3)
177 return AVERROR_INVALIDDATA;
178 for (i = 0; i < 256; i++) {
179 for (j = 0; j < 3; j++, data++)
180 c[j] = (*data << 2) | (*data >> 4);
181 palette[i] = AV_RB24(c);
183 seq->frame.palette_has_changed = 1;
187 if (data_end - data < 128)
188 return AVERROR_INVALIDDATA;
189 init_get_bits(&gb, data, 128 * 8); data += 128;
190 for (y = 0; y < 128; y += 8)
191 for (x = 0; x < 256; x += 8) {
192 dst = &seq->frame.data[0][y * seq->frame.linesize[0] + x];
193 op = get_bits(&gb, 2);
196 data = seq_decode_op1(seq, data, data_end, dst);
199 data = seq_decode_op2(seq, data, data_end, dst);
202 data = seq_decode_op3(seq, data, data_end, dst);
206 return AVERROR_INVALIDDATA;
212 static av_cold int seqvideo_decode_init(AVCodecContext *avctx)
214 SeqVideoContext *seq = avctx->priv_data;
217 avctx->pix_fmt = AV_PIX_FMT_PAL8;
219 seq->frame.data[0] = NULL;
224 static int seqvideo_decode_frame(AVCodecContext *avctx,
225 void *data, int *data_size,
228 const uint8_t *buf = avpkt->data;
229 int buf_size = avpkt->size;
231 SeqVideoContext *seq = avctx->priv_data;
233 seq->frame.reference = 1;
234 seq->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
235 if (avctx->reget_buffer(avctx, &seq->frame)) {
236 av_log(seq->avctx, AV_LOG_ERROR, "tiertexseqvideo: reget_buffer() failed\n");
240 if (seqvideo_decode(seq, buf, buf_size))
241 return AVERROR_INVALIDDATA;
243 *data_size = sizeof(AVFrame);
244 *(AVFrame *)data = seq->frame;
249 static av_cold int seqvideo_decode_end(AVCodecContext *avctx)
251 SeqVideoContext *seq = avctx->priv_data;
253 if (seq->frame.data[0])
254 avctx->release_buffer(avctx, &seq->frame);
259 AVCodec ff_tiertexseqvideo_decoder = {
260 .name = "tiertexseqvideo",
261 .type = AVMEDIA_TYPE_VIDEO,
262 .id = AV_CODEC_ID_TIERTEXSEQVIDEO,
263 .priv_data_size = sizeof(SeqVideoContext),
264 .init = seqvideo_decode_init,
265 .close = seqvideo_decode_end,
266 .decode = seqvideo_decode_frame,
267 .capabilities = CODEC_CAP_DR1,
268 .long_name = NULL_IF_CONFIG_SMALL("Tiertex Limited SEQ video"),