FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "get_bits.h"
30 #include "internal.h"
31 
32 
33 typedef struct 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  GetBitContext gb;
45  int code_table[64];
46 
47  /* get the rle codes */
48  init_get_bits(&gb, src, (src_end - src) * 8);
49  for (i = 0, sz = 0; i < 64 && sz < dst_size; i++) {
50  if (get_bits_left(&gb) < 4)
51  return NULL;
52  code_table[i] = get_sbits(&gb, 4);
53  sz += FFABS(code_table[i]);
54  }
55  src += (get_bits_count(&gb) + 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  GetBitContext gb;
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  init_get_bits(&gb, src, bits * 8 * 8); src += bits * 8;
117  for (b = 0; b < 8; b++) {
118  for (i = 0; i < 8; i++)
119  dst[i] = color_table[get_bits(&gb, bits)];
120  dst += seq->frame->linesize[0];
121  }
122  }
123 
124  return src;
125 }
126 
127 static const unsigned char *seq_decode_op2(SeqVideoContext *seq,
128  const unsigned char *src,
129  const unsigned char *src_end,
130  unsigned char *dst)
131 {
132  int i;
133 
134  if (src_end - src < 8 * 8)
135  return NULL;
136 
137  for (i = 0; i < 8; i++) {
138  memcpy(dst, src, 8);
139  src += 8;
140  dst += seq->frame->linesize[0];
141  }
142 
143  return src;
144 }
145 
146 static const unsigned char *seq_decode_op3(SeqVideoContext *seq,
147  const unsigned char *src,
148  const unsigned char *src_end,
149  unsigned char *dst)
150 {
151  int pos, offset;
152 
153  do {
154  if (src_end - src < 2)
155  return NULL;
156  pos = *src++;
157  offset = ((pos >> 3) & 7) * seq->frame->linesize[0] + (pos & 7);
158  dst[offset] = *src++;
159  } while (!(pos & 0x80));
160 
161  return src;
162 }
163 
164 static int seqvideo_decode(SeqVideoContext *seq, const unsigned char *data, int data_size)
165 {
166  const unsigned char *data_end = data + data_size;
167  GetBitContext gb;
168  int flags, i, j, x, y, op;
169  unsigned char c[3];
170  unsigned char *dst;
171  uint32_t *palette;
172 
173  flags = *data++;
174 
175  if (flags & 1) {
176  palette = (uint32_t *)seq->frame->data[1];
177  if (data_end - data < 256 * 3)
178  return AVERROR_INVALIDDATA;
179  for (i = 0; i < 256; i++) {
180  for (j = 0; j < 3; j++, data++)
181  c[j] = (*data << 2) | (*data >> 4);
182  palette[i] = 0xFFU << 24 | AV_RB24(c);
183  }
184  seq->frame->palette_has_changed = 1;
185  }
186 
187  if (flags & 2) {
188  if (data_end - data < 128)
189  return AVERROR_INVALIDDATA;
190  init_get_bits(&gb, data, 128 * 8); data += 128;
191  for (y = 0; y < 128; y += 8)
192  for (x = 0; x < 256; x += 8) {
193  dst = &seq->frame->data[0][y * seq->frame->linesize[0] + x];
194  op = get_bits(&gb, 2);
195  switch (op) {
196  case 1:
197  data = seq_decode_op1(seq, data, data_end, dst);
198  break;
199  case 2:
200  data = seq_decode_op2(seq, data, data_end, dst);
201  break;
202  case 3:
203  data = seq_decode_op3(seq, data, data_end, dst);
204  break;
205  }
206  if (!data)
207  return AVERROR_INVALIDDATA;
208  }
209  }
210  return 0;
211 }
212 
214 {
215  SeqVideoContext *seq = avctx->priv_data;
216  int ret;
217 
218  seq->avctx = avctx;
219  avctx->pix_fmt = AV_PIX_FMT_PAL8;
220 
221  ret = ff_set_dimensions(avctx, 256, 128);
222  if (ret < 0)
223  return ret;
224 
225  seq->frame = av_frame_alloc();
226  if (!seq->frame)
227  return AVERROR(ENOMEM);
228 
229  return 0;
230 }
231 
233  void *data, int *got_frame,
234  AVPacket *avpkt)
235 {
236  const uint8_t *buf = avpkt->data;
237  int buf_size = avpkt->size;
238  int ret;
239 
240  SeqVideoContext *seq = avctx->priv_data;
241 
242  if ((ret = ff_reget_buffer(avctx, seq->frame)) < 0)
243  return ret;
244 
245  if (seqvideo_decode(seq, buf, buf_size))
246  return AVERROR_INVALIDDATA;
247 
248  if ((ret = av_frame_ref(data, seq->frame)) < 0)
249  return ret;
250  *got_frame = 1;
251 
252  return buf_size;
253 }
254 
256 {
257  SeqVideoContext *seq = avctx->priv_data;
258 
259  av_frame_free(&seq->frame);
260 
261  return 0;
262 }
263 
265  .name = "tiertexseqvideo",
266  .long_name = NULL_IF_CONFIG_SMALL("Tiertex Limited SEQ video"),
267  .type = AVMEDIA_TYPE_VIDEO,
269  .priv_data_size = sizeof(SeqVideoContext),
271  .close = seqvideo_decode_end,
273  .capabilities = AV_CODEC_CAP_DR1,
274 };
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:211
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static av_cold int seqvideo_decode_end(AVCodecContext *avctx)
Definition: tiertexseqv.c:255
static av_cold int seqvideo_decode_init(AVCodecContext *avctx)
Definition: tiertexseqv.c:213
AVFrame * frame
Definition: tiertexseqv.c:35
int size
Definition: avcodec.h:1680
const char * b
Definition: vf_curves.c:113
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1989
static int seqvideo_decode(SeqVideoContext *seq, const unsigned char *data, int data_size)
Definition: tiertexseqv.c:164
#define src
Definition: vp8dsp.c:254
AVCodec.
Definition: avcodec.h:3739
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:246
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
Definition: decode.c:1718
static int16_t block[64]
Definition: dct.c:115
AVCodecContext * avctx
Definition: tiertexseqv.c:34
uint8_t bits
Definition: crc.c:296
uint8_t
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:73
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:395
uint8_t * data
Definition: avcodec.h:1679
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:199
static int flags
Definition: log.c:57
bitstream reader API header.
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:587
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:39
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
static const ColorEntry color_table[]
Definition: xpmdec.c:46
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
const char * name
Name of the codec implementation.
Definition: avcodec.h:3746
static const unsigned char * seq_decode_op3(SeqVideoContext *seq, const unsigned char *src, const unsigned char *src_end, unsigned char *dst)
Definition: tiertexseqv.c:146
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMIN(a, b)
Definition: common.h:96
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
static const unsigned char * seq_decode_op1(SeqVideoContext *seq, const unsigned char *src, const unsigned char *src_end, unsigned char *dst)
Definition: tiertexseqv.c:77
const uint8_t ff_log2_tab[256]
Definition: log2_tab.c:23
Libavcodec external API header.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:87
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
main external API structure.
Definition: avcodec.h:1761
void * buf
Definition: avisynth_c.h:690
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:425
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:358
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
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:78
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:279
static double c[64]
static int seqvideo_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: tiertexseqv.c:232
static const unsigned char * seq_decode_op2(SeqVideoContext *seq, const unsigned char *src, const unsigned char *src_end, unsigned char *dst)
Definition: tiertexseqv.c:127
void * priv_data
Definition: avcodec.h:1803
int len
This structure stores compressed data.
Definition: avcodec.h:1656
AVCodec ff_tiertexseqvideo_decoder
Definition: tiertexseqv.c:264
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:1002