FFmpeg  4.4.9
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  len = FFMIN(len, dst_size);
72  dst += len;
73  dst_size -= len;
74  }
75  if (dst_size > 0)
76  return NULL;
77  return src;
78 }
79 
80 static const unsigned char *seq_decode_op1(SeqVideoContext *seq,
81  const unsigned char *src,
82  const unsigned char *src_end,
83  unsigned char *dst)
84 {
85  const unsigned char *color_table;
86  int b, i, len, bits;
87  GetBitContext gb;
88  unsigned char block[8 * 8];
89 
90  if (src_end - src < 1)
91  return NULL;
92  len = *src++;
93  if (len & 0x80) {
94  switch (len & 3) {
95  case 1:
96  src = seq_unpack_rle_block(src, src_end, block, sizeof(block));
97  if (!src)
98  return NULL;
99  for (b = 0; b < 8; b++) {
100  memcpy(dst, &block[b * 8], 8);
101  dst += seq->frame->linesize[0];
102  }
103  break;
104  case 2:
105  src = seq_unpack_rle_block(src, src_end, block, sizeof(block));
106  if (!src)
107  return NULL;
108  for (i = 0; i < 8; i++) {
109  for (b = 0; b < 8; b++)
110  dst[b * seq->frame->linesize[0]] = block[i * 8 + b];
111  ++dst;
112  }
113  break;
114  }
115  } else {
116  if (len <= 0)
117  return NULL;
118  bits = ff_log2_tab[len - 1] + 1;
119  if (src_end - src < len + 8 * bits)
120  return NULL;
121  color_table = src;
122  src += len;
123  init_get_bits(&gb, src, bits * 8 * 8); src += bits * 8;
124  for (b = 0; b < 8; b++) {
125  for (i = 0; i < 8; i++)
126  dst[i] = color_table[get_bits(&gb, bits)];
127  dst += seq->frame->linesize[0];
128  }
129  }
130 
131  return src;
132 }
133 
134 static const unsigned char *seq_decode_op2(SeqVideoContext *seq,
135  const unsigned char *src,
136  const unsigned char *src_end,
137  unsigned char *dst)
138 {
139  int i;
140 
141  if (src_end - src < 8 * 8)
142  return NULL;
143 
144  for (i = 0; i < 8; i++) {
145  memcpy(dst, src, 8);
146  src += 8;
147  dst += seq->frame->linesize[0];
148  }
149 
150  return src;
151 }
152 
153 static const unsigned char *seq_decode_op3(SeqVideoContext *seq,
154  const unsigned char *src,
155  const unsigned char *src_end,
156  unsigned char *dst)
157 {
158  int pos, offset;
159 
160  do {
161  if (src_end - src < 2)
162  return NULL;
163  pos = *src++;
164  offset = ((pos >> 3) & 7) * seq->frame->linesize[0] + (pos & 7);
165  dst[offset] = *src++;
166  } while (!(pos & 0x80));
167 
168  return src;
169 }
170 
171 static int seqvideo_decode(SeqVideoContext *seq, const unsigned char *data, int data_size)
172 {
173  const unsigned char *data_end = data + data_size;
174  GetBitContext gb;
175  int flags, i, j, x, y, op;
176  unsigned char c[3];
177  unsigned char *dst;
178  uint32_t *palette;
179 
180  flags = *data++;
181 
182  if (flags & 1) {
183  palette = (uint32_t *)seq->frame->data[1];
184  if (data_end - data < 256 * 3)
185  return AVERROR_INVALIDDATA;
186  for (i = 0; i < 256; i++) {
187  for (j = 0; j < 3; j++, data++)
188  c[j] = (*data << 2) | (*data >> 4);
189  palette[i] = 0xFFU << 24 | AV_RB24(c);
190  }
191  seq->frame->palette_has_changed = 1;
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  void *data, int *got_frame,
241  AVPacket *avpkt)
242 {
243  const uint8_t *buf = avpkt->data;
244  int buf_size = avpkt->size;
245  int ret;
246 
247  SeqVideoContext *seq = avctx->priv_data;
248 
249  if ((ret = ff_reget_buffer(avctx, seq->frame, 0)) < 0)
250  return ret;
251 
252  if (seqvideo_decode(seq, buf, buf_size))
253  return AVERROR_INVALIDDATA;
254 
255  if ((ret = av_frame_ref(data, seq->frame)) < 0)
256  return ret;
257  *got_frame = 1;
258 
259  return buf_size;
260 }
261 
263 {
264  SeqVideoContext *seq = avctx->priv_data;
265 
266  av_frame_free(&seq->frame);
267 
268  return 0;
269 }
270 
272  .name = "tiertexseqvideo",
273  .long_name = NULL_IF_CONFIG_SMALL("Tiertex Limited SEQ video"),
274  .type = AVMEDIA_TYPE_VIDEO,
276  .priv_data_size = sizeof(SeqVideoContext),
278  .close = seqvideo_decode_end,
280  .capabilities = AV_CODEC_CAP_DR1,
281 };
#define av_cold
Definition: attributes.h:88
uint8_t
Libavcodec external API header.
#define AV_RB24
Definition: intreadwrite.h:64
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define FFMIN(a, b)
Definition: common.h:105
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define NULL
Definition: coverity.c:32
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:2018
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
bitstream reader API header.
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:359
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
@ AV_CODEC_ID_TIERTEXSEQVIDEO
Definition: codec_id.h:144
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
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:443
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
int i
Definition: input.c:407
const uint8_t ff_log2_tab[256]
Definition: log2_tab.c:23
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:75
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:84
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
const char data[16]
Definition: mxf.c:142
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
unsigned int pos
Definition: spdifenc.c:412
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:475
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
AVFrame * frame
Definition: tiertexseqv.c:35
AVCodecContext * avctx
Definition: tiertexseqv.c:34
#define src
Definition: vp8dsp.c:255
static int16_t block[64]
Definition: dct.c:116
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
static const unsigned char * seq_decode_op2(SeqVideoContext *seq, const unsigned char *src, const unsigned char *src_end, unsigned char *dst)
Definition: tiertexseqv.c:134
static int seqvideo_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: tiertexseqv.c:239
AVCodec ff_tiertexseqvideo_decoder
Definition: tiertexseqv.c:271
static int seqvideo_decode(SeqVideoContext *seq, const unsigned char *data, int data_size)
Definition: tiertexseqv.c:171
static av_cold int seqvideo_decode_end(AVCodecContext *avctx)
Definition: tiertexseqv.c:262
static const unsigned char * seq_decode_op3(SeqVideoContext *seq, const unsigned char *src, const unsigned char *src_end, unsigned char *dst)
Definition: tiertexseqv.c:153
static const unsigned char * seq_decode_op1(SeqVideoContext *seq, const unsigned char *src, const unsigned char *src_end, unsigned char *dst)
Definition: tiertexseqv.c:80
static av_cold int seqvideo_decode_init(AVCodecContext *avctx)
Definition: tiertexseqv.c:220
const char * b
Definition: vf_curves.c:119
if(ret< 0)
Definition: vf_mcdeint.c:282
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
int len
uint8_t bits
Definition: vp3data.h:141
static double c[64]
static const ColorEntry color_table[]
Definition: xpmdec.c:50