]> git.sesse.net Git - ffmpeg/blob - libavcodec/tiertexseqv.c
dxva2: Keep code shared between dxva2 and d3d11va under the correct #if
[ffmpeg] / libavcodec / tiertexseqv.c
1 /*
2  * Tiertex Limited SEQ Video Decoder
3  * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * Tiertex Limited SEQ video decoder
25  */
26
27 #define BITSTREAM_READER_LE
28 #include "avcodec.h"
29 #include "bitstream.h"
30 #include "internal.h"
31
32
33 typedef struct SeqVideoContext {
34     AVCodecContext *avctx;
35     AVFrame *frame;
36 } SeqVideoContext;
37
38
39 static const unsigned char *seq_unpack_rle_block(const unsigned char *src,
40                                                  const unsigned char *src_end,
41                                                  unsigned char *dst, int dst_size)
42 {
43     int i, len, sz;
44     BitstreamContext bc;
45     int code_table[64];
46
47     /* get the rle codes */
48     bitstream_init(&bc, src, (src_end - src) * 8);
49     for (i = 0, sz = 0; i < 64 && sz < dst_size; i++) {
50         if (bitstream_bits_left(&bc) < 4)
51             return NULL;
52         code_table[i] = bitstream_read_signed(&bc, 4);
53         sz += FFABS(code_table[i]);
54     }
55     src += (bitstream_tell(&bc) + 7) / 8;
56
57     /* do the rle unpacking */
58     for (i = 0; i < 64 && dst_size > 0; i++) {
59         len = code_table[i];
60         if (len < 0) {
61             len = -len;
62             if (src_end - src < 1)
63                 return NULL;
64             memset(dst, *src++, FFMIN(len, dst_size));
65         } else {
66             if (src_end - src < len)
67                 return NULL;
68             memcpy(dst, src, FFMIN(len, dst_size));
69             src += len;
70         }
71         dst += len;
72         dst_size -= len;
73     }
74     return src;
75 }
76
77 static const unsigned char *seq_decode_op1(SeqVideoContext *seq,
78                                            const unsigned char *src,
79                                            const unsigned char *src_end,
80                                            unsigned char *dst)
81 {
82     const unsigned char *color_table;
83     int b, i, len, bits;
84     BitstreamContext bc;
85     unsigned char block[8 * 8];
86
87     if (src_end - src < 1)
88         return NULL;
89     len = *src++;
90     if (len & 0x80) {
91         switch (len & 3) {
92         case 1:
93             src = seq_unpack_rle_block(src, src_end, block, sizeof(block));
94             for (b = 0; b < 8; b++) {
95                 memcpy(dst, &block[b * 8], 8);
96                 dst += seq->frame->linesize[0];
97             }
98             break;
99         case 2:
100             src = seq_unpack_rle_block(src, src_end, block, sizeof(block));
101             for (i = 0; i < 8; i++) {
102                 for (b = 0; b < 8; b++)
103                     dst[b * seq->frame->linesize[0]] = block[i * 8 + b];
104                 ++dst;
105             }
106             break;
107         }
108     } else {
109         if (len <= 0)
110             return NULL;
111         bits = ff_log2_tab[len - 1] + 1;
112         if (src_end - src < len + 8 * bits)
113             return NULL;
114         color_table = src;
115         src += len;
116         bitstream_init(&bc, src, bits * 8 * 8);
117         src += bits * 8;
118         for (b = 0; b < 8; b++) {
119             for (i = 0; i < 8; i++)
120                 dst[i] = color_table[bitstream_read(&bc, bits)];
121             dst += seq->frame->linesize[0];
122         }
123     }
124
125     return src;
126 }
127
128 static const unsigned char *seq_decode_op2(SeqVideoContext *seq,
129                                            const unsigned char *src,
130                                            const unsigned char *src_end,
131                                            unsigned char *dst)
132 {
133     int i;
134
135     if (src_end - src < 8 * 8)
136         return NULL;
137
138     for (i = 0; i < 8; i++) {
139         memcpy(dst, src, 8);
140         src += 8;
141         dst += seq->frame->linesize[0];
142     }
143
144     return src;
145 }
146
147 static const unsigned char *seq_decode_op3(SeqVideoContext *seq,
148                                            const unsigned char *src,
149                                            const unsigned char *src_end,
150                                            unsigned char *dst)
151 {
152     int pos, offset;
153
154     do {
155         if (src_end - src < 2)
156             return NULL;
157         pos = *src++;
158         offset = ((pos >> 3) & 7) * seq->frame->linesize[0] + (pos & 7);
159         dst[offset] = *src++;
160     } while (!(pos & 0x80));
161
162     return src;
163 }
164
165 static int seqvideo_decode(SeqVideoContext *seq, const unsigned char *data, int data_size)
166 {
167     const unsigned char *data_end = data + data_size;
168     BitstreamContext bc;
169     int flags, i, j, x, y, op;
170     unsigned char c[3];
171     unsigned char *dst;
172     uint32_t *palette;
173
174     flags = *data++;
175
176     if (flags & 1) {
177         palette = (uint32_t *)seq->frame->data[1];
178         if (data_end - data < 256 * 3)
179             return AVERROR_INVALIDDATA;
180         for (i = 0; i < 256; i++) {
181             for (j = 0; j < 3; j++, data++)
182                 c[j] = (*data << 2) | (*data >> 4);
183             palette[i] = AV_RB24(c);
184         }
185         seq->frame->palette_has_changed = 1;
186     }
187
188     if (flags & 2) {
189         if (data_end - data < 128)
190             return AVERROR_INVALIDDATA;
191         bitstream_init(&bc, data, 128 * 8);
192         data += 128;
193         for (y = 0; y < 128; y += 8)
194             for (x = 0; x < 256; x += 8) {
195                 dst = &seq->frame->data[0][y * seq->frame->linesize[0] + x];
196                 op = bitstream_read(&bc, 2);
197                 switch (op) {
198                 case 1:
199                     data = seq_decode_op1(seq, data, data_end, dst);
200                     break;
201                 case 2:
202                     data = seq_decode_op2(seq, data, data_end, dst);
203                     break;
204                 case 3:
205                     data = seq_decode_op3(seq, data, data_end, dst);
206                     break;
207                 }
208                 if (!data)
209                     return AVERROR_INVALIDDATA;
210             }
211     }
212     return 0;
213 }
214
215 static av_cold int seqvideo_decode_init(AVCodecContext *avctx)
216 {
217     SeqVideoContext *seq = avctx->priv_data;
218
219     seq->avctx = avctx;
220     avctx->pix_fmt = AV_PIX_FMT_PAL8;
221
222     seq->frame = av_frame_alloc();
223     if (!seq->frame)
224         return AVERROR(ENOMEM);
225
226     return 0;
227 }
228
229 static int seqvideo_decode_frame(AVCodecContext *avctx,
230                                  void *data, int *got_frame,
231                                  AVPacket *avpkt)
232 {
233     const uint8_t *buf = avpkt->data;
234     int buf_size = avpkt->size;
235     int ret;
236
237     SeqVideoContext *seq = avctx->priv_data;
238
239     if ((ret = ff_reget_buffer(avctx, seq->frame)) < 0) {
240         av_log(seq->avctx, AV_LOG_ERROR, "tiertexseqvideo: reget_buffer() failed\n");
241         return ret;
242     }
243
244     if (seqvideo_decode(seq, buf, buf_size))
245         return AVERROR_INVALIDDATA;
246
247     if ((ret = av_frame_ref(data, seq->frame)) < 0)
248         return ret;
249     *got_frame       = 1;
250
251     return buf_size;
252 }
253
254 static av_cold int seqvideo_decode_end(AVCodecContext *avctx)
255 {
256     SeqVideoContext *seq = avctx->priv_data;
257
258     av_frame_free(&seq->frame);
259
260     return 0;
261 }
262
263 AVCodec ff_tiertexseqvideo_decoder = {
264     .name           = "tiertexseqvideo",
265     .long_name      = NULL_IF_CONFIG_SMALL("Tiertex Limited SEQ video"),
266     .type           = AVMEDIA_TYPE_VIDEO,
267     .id             = AV_CODEC_ID_TIERTEXSEQVIDEO,
268     .priv_data_size = sizeof(SeqVideoContext),
269     .init           = seqvideo_decode_init,
270     .close          = seqvideo_decode_end,
271     .decode         = seqvideo_decode_frame,
272     .capabilities   = AV_CODEC_CAP_DR1,
273 };