FFmpeg
Loading...
Searching...
No Matches
tiertexseqv.c
Go to the documentation of this file.
1/*
2 * Tiertex Limited SEQ Video Decoder
3 * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.net)
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 "codec_internal.h"
30#include "decode.h"
31#include "get_bits.h"
32
33
38
39
40static const unsigned char *seq_unpack_rle_block(const unsigned char *src,
41 const unsigned char *src_end,
42 unsigned char *dst, int dst_size)
43{
44 int i, len, sz;
46 int code_table[64];
47
48 /* get the rle codes */
49 init_get_bits(&gb, src, (src_end - src) * 8);
50 for (i = 0, sz = 0; i < 64 && sz < dst_size; i++) {
51 if (get_bits_left(&gb) < 4)
52 return NULL;
53 code_table[i] = get_sbits(&gb, 4);
54 sz += FFABS(code_table[i]);
55 }
56 src += (get_bits_count(&gb) + 7) / 8;
57
58 /* do the rle unpacking */
59 for (i = 0; i < 64 && dst_size > 0; i++) {
60 len = code_table[i];
61 if (len < 0) {
62 len = -len;
63 if (src_end - src < 1)
64 return NULL;
65 memset(dst, *src++, FFMIN(len, dst_size));
66 } else {
67 if (src_end - src < len)
68 return NULL;
69 memcpy(dst, src, FFMIN(len, dst_size));
70 src += len;
71 }
72 len = FFMIN(len, dst_size);
73 dst += len;
74 dst_size -= len;
75 }
76 if (dst_size > 0)
77 return NULL;
78 return src;
79}
80
81static const unsigned char *seq_decode_op1(SeqVideoContext *seq,
82 const unsigned char *src,
83 const unsigned char *src_end,
84 unsigned char *dst)
85{
86 const unsigned char *color_table;
87 int b, i, len, bits;
89 unsigned char block[8 * 8];
90
91 if (src_end - src < 1)
92 return NULL;
93 len = *src++;
94 if (len & 0x80) {
95 switch (len & 3) {
96 case 1:
97 src = seq_unpack_rle_block(src, src_end, block, sizeof(block));
98 if (!src)
99 return NULL;
100 for (b = 0; b < 8; b++) {
101 memcpy(dst, &block[b * 8], 8);
102 dst += seq->frame->linesize[0];
103 }
104 break;
105 case 2:
106 src = seq_unpack_rle_block(src, src_end, block, sizeof(block));
107 if (!src)
108 return NULL;
109 for (i = 0; i < 8; i++) {
110 for (b = 0; b < 8; b++)
111 dst[b * seq->frame->linesize[0]] = block[i * 8 + b];
112 ++dst;
113 }
114 break;
115 }
116 } else {
117 if (len <= 0)
118 return NULL;
119 bits = ff_log2_tab[len - 1] + 1;
120 if (src_end - src < len + 8 * bits)
121 return NULL;
123 src += len;
124 init_get_bits(&gb, src, bits * 8 * 8); src += bits * 8;
125 for (b = 0; b < 8; b++) {
126 for (i = 0; i < 8; i++)
127 dst[i] = color_table[get_bits(&gb, bits)];
128 dst += seq->frame->linesize[0];
129 }
130 }
131
132 return src;
133}
134
135static const unsigned char *seq_decode_op2(SeqVideoContext *seq,
136 const unsigned char *src,
137 const unsigned char *src_end,
138 unsigned char *dst)
139{
140 int i;
141
142 if (src_end - src < 8 * 8)
143 return NULL;
144
145 for (i = 0; i < 8; i++) {
146 memcpy(dst, src, 8);
147 src += 8;
148 dst += seq->frame->linesize[0];
149 }
150
151 return src;
152}
153
154static const unsigned char *seq_decode_op3(SeqVideoContext *seq,
155 const unsigned char *src,
156 const unsigned char *src_end,
157 unsigned char *dst)
158{
159 int pos, offset;
160
161 do {
162 if (src_end - src < 2)
163 return NULL;
164 pos = *src++;
165 offset = ((pos >> 3) & 7) * seq->frame->linesize[0] + (pos & 7);
166 dst[offset] = *src++;
167 } while (!(pos & 0x80));
168
169 return src;
170}
171
172static int seqvideo_decode(SeqVideoContext *seq, const unsigned char *data, int data_size)
173{
174 const unsigned char *data_end = data + data_size;
175 GetBitContext gb;
176 int flags, i, j, x, y, op;
177 unsigned char c[3];
178 unsigned char *dst;
179 uint32_t *palette;
180
181 flags = *data++;
182
183 if (flags & 1) {
184 palette = (uint32_t *)seq->frame->data[1];
185 if (data_end - data < 256 * 3)
186 return AVERROR_INVALIDDATA;
187 for (i = 0; i < 256; i++) {
188 for (j = 0; j < 3; j++, data++)
189 c[j] = (*data << 2) | (*data >> 4);
190 palette[i] = 0xFFU << 24 | AV_RB24(c);
191 }
192 }
193
194 if (flags & 2) {
195 if (data_end - data < 128)
196 return AVERROR_INVALIDDATA;
197 init_get_bits(&gb, data, 128 * 8); data += 128;
198 for (y = 0; y < 128; y += 8)
199 for (x = 0; x < 256; x += 8) {
200 dst = &seq->frame->data[0][y * seq->frame->linesize[0] + x];
201 op = get_bits(&gb, 2);
202 switch (op) {
203 case 1:
204 data = seq_decode_op1(seq, data, data_end, dst);
205 break;
206 case 2:
207 data = seq_decode_op2(seq, data, data_end, dst);
208 break;
209 case 3:
210 data = seq_decode_op3(seq, data, data_end, dst);
211 break;
212 }
213 if (!data)
214 return AVERROR_INVALIDDATA;
215 }
216 }
217 return 0;
218}
219
221{
222 SeqVideoContext *seq = avctx->priv_data;
223 int ret;
224
225 seq->avctx = avctx;
226 avctx->pix_fmt = AV_PIX_FMT_PAL8;
227
228 ret = ff_set_dimensions(avctx, 256, 128);
229 if (ret < 0)
230 return ret;
231
232 seq->frame = av_frame_alloc();
233 if (!seq->frame)
234 return AVERROR(ENOMEM);
235
236 return 0;
237}
238
240 int *got_frame, AVPacket *avpkt)
241{
242 const uint8_t *buf = avpkt->data;
243 int buf_size = avpkt->size;
244 int ret;
245
246 SeqVideoContext *seq = avctx->priv_data;
247
248 if ((ret = ff_reget_buffer(avctx, seq->frame, 0)) < 0)
249 return ret;
250
251 if (seqvideo_decode(seq, buf, buf_size))
252 return AVERROR_INVALIDDATA;
253
254 if ((ret = av_frame_ref(rframe, seq->frame)) < 0)
255 return ret;
256 *got_frame = 1;
257
258 return buf_size;
259}
260
262{
263 SeqVideoContext *seq = avctx->priv_data;
264
265 av_frame_free(&seq->frame);
266
267 return 0;
268}
269
271 .p.name = "tiertexseqvideo",
272 CODEC_LONG_NAME("Tiertex Limited SEQ video"),
273 .p.type = AVMEDIA_TYPE_VIDEO,
275 .priv_data_size = sizeof(SeqVideoContext),
279 .p.capabilities = AV_CODEC_CAP_DR1,
280};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
const FFCodec ff_tiertexseqvideo_decoder
static const uint8_t palette[16][3]
Definition apnggen.c:50
Libavcodec external API header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
static int16_t block[64]
Definition dct.c:125
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition decode.c:1943
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
static const uint8_t bits[8]
Definition fastaudio.c:100
int ret
Definition ffplay.c:391
bitstream reader API header.
static int get_sbits(GetBitContext *s, int n)
Definition get_bits.h:322
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_TIERTEXSEQVIDEO
Definition codec_id.h:145
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition frame.c:282
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define b
Definition input.c:43
const uint8_t ff_log2_tab[256]
Definition log2_tab.c:23
#define AV_RB24(x)
unsigned offset
Definition libaomenc.c:812
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition anm.c:76
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
#define av_cold
Definition attributes.h:117
#define FFMIN(a, b)
Definition macros.h:49
const char data[16]
Definition mxf.c:155
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
unsigned int pos
Definition spdifenc.c:431
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:524
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
AVFrame * frame
Definition tiertexseqv.c:36
AVCodecContext * avctx
Definition tiertexseqv.c:35
#define src
Definition vp8dsp.c:248
static int seqvideo_decode(SeqVideoContext *seq, const unsigned char *data, int data_size)
static av_cold int seqvideo_decode_end(AVCodecContext *avctx)
static const unsigned char * seq_decode_op1(SeqVideoContext *seq, const unsigned char *src, const unsigned char *src_end, unsigned char *dst)
Definition tiertexseqv.c:81
static av_cold int seqvideo_decode_init(AVCodecContext *avctx)
static const unsigned char * seq_unpack_rle_block(const unsigned char *src, const unsigned char *src_end, unsigned char *dst, int dst_size)
Definition tiertexseqv.c:40
static const unsigned char * seq_decode_op3(SeqVideoContext *seq, const unsigned char *src, const unsigned char *src_end, unsigned char *dst)
static const unsigned char * seq_decode_op2(SeqVideoContext *seq, const unsigned char *src, const unsigned char *src_end, unsigned char *dst)
static int seqvideo_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
int len
static double c[64]
static const ColorEntry color_table[]
Definition xpmdec.c:51