FFmpeg
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 
34 typedef struct SeqVideoContext {
38 
39 
40 static 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;
45  GetBitContext gb;
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  dst += len;
73  dst_size -= len;
74  }
75  return src;
76 }
77 
78 static const unsigned char *seq_decode_op1(SeqVideoContext *seq,
79  const unsigned char *src,
80  const unsigned char *src_end,
81  unsigned char *dst)
82 {
83  const unsigned char *color_table;
84  int b, i, len, bits;
85  GetBitContext gb;
86  unsigned char block[8 * 8];
87 
88  if (src_end - src < 1)
89  return NULL;
90  len = *src++;
91  if (len & 0x80) {
92  switch (len & 3) {
93  case 1:
94  src = seq_unpack_rle_block(src, src_end, block, sizeof(block));
95  for (b = 0; b < 8; b++) {
96  memcpy(dst, &block[b * 8], 8);
97  dst += seq->frame->linesize[0];
98  }
99  break;
100  case 2:
101  src = seq_unpack_rle_block(src, src_end, block, sizeof(block));
102  for (i = 0; i < 8; i++) {
103  for (b = 0; b < 8; b++)
104  dst[b * seq->frame->linesize[0]] = block[i * 8 + b];
105  ++dst;
106  }
107  break;
108  }
109  } else {
110  if (len <= 0)
111  return NULL;
112  bits = ff_log2_tab[len - 1] + 1;
113  if (src_end - src < len + 8 * bits)
114  return NULL;
115  color_table = src;
116  src += len;
117  init_get_bits(&gb, src, bits * 8 * 8); src += bits * 8;
118  for (b = 0; b < 8; b++) {
119  for (i = 0; i < 8; i++)
120  dst[i] = color_table[get_bits(&gb, 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  GetBitContext gb;
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] = 0xFFU << 24 | AV_RB24(c);
184  }
185 #if FF_API_PALETTE_HAS_CHANGED
187  seq->frame->palette_has_changed = 1;
189 #endif
190  }
191 
192  if (flags & 2) {
193  if (data_end - data < 128)
194  return AVERROR_INVALIDDATA;
195  init_get_bits(&gb, data, 128 * 8); data += 128;
196  for (y = 0; y < 128; y += 8)
197  for (x = 0; x < 256; x += 8) {
198  dst = &seq->frame->data[0][y * seq->frame->linesize[0] + x];
199  op = get_bits(&gb, 2);
200  switch (op) {
201  case 1:
202  data = seq_decode_op1(seq, data, data_end, dst);
203  break;
204  case 2:
205  data = seq_decode_op2(seq, data, data_end, dst);
206  break;
207  case 3:
208  data = seq_decode_op3(seq, data, data_end, dst);
209  break;
210  }
211  if (!data)
212  return AVERROR_INVALIDDATA;
213  }
214  }
215  return 0;
216 }
217 
219 {
220  SeqVideoContext *seq = avctx->priv_data;
221  int ret;
222 
223  seq->avctx = avctx;
224  avctx->pix_fmt = AV_PIX_FMT_PAL8;
225 
226  ret = ff_set_dimensions(avctx, 256, 128);
227  if (ret < 0)
228  return ret;
229 
230  seq->frame = av_frame_alloc();
231  if (!seq->frame)
232  return AVERROR(ENOMEM);
233 
234  return 0;
235 }
236 
237 static int seqvideo_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
238  int *got_frame, AVPacket *avpkt)
239 {
240  const uint8_t *buf = avpkt->data;
241  int buf_size = avpkt->size;
242  int ret;
243 
244  SeqVideoContext *seq = avctx->priv_data;
245 
246  if ((ret = ff_reget_buffer(avctx, seq->frame, 0)) < 0)
247  return ret;
248 
249  if (seqvideo_decode(seq, buf, buf_size))
250  return AVERROR_INVALIDDATA;
251 
252  if ((ret = av_frame_ref(rframe, seq->frame)) < 0)
253  return ret;
254  *got_frame = 1;
255 
256  return buf_size;
257 }
258 
260 {
261  SeqVideoContext *seq = avctx->priv_data;
262 
263  av_frame_free(&seq->frame);
264 
265  return 0;
266 }
267 
269  .p.name = "tiertexseqvideo",
270  CODEC_LONG_NAME("Tiertex Limited SEQ video"),
271  .p.type = AVMEDIA_TYPE_VIDEO,
273  .priv_data_size = sizeof(SeqVideoContext),
275  .close = seqvideo_decode_end,
277  .p.capabilities = AV_CODEC_CAP_DR1,
278 };
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
AVFrame::palette_has_changed
attribute_deprecated int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:582
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
SeqVideoContext
Definition: tiertexseqv.c:34
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVPacket::data
uint8_t * data
Definition: packet.h:539
b
#define b
Definition: input.c:41
data
const char data[16]
Definition: mxf.c:149
FFCodec
Definition: codec_internal.h:127
seqvideo_decode_end
static av_cold int seqvideo_decode_end(AVCodecContext *avctx)
Definition: tiertexseqv.c:259
SeqVideoContext::frame
AVFrame * frame
Definition: tiertexseqv.c:36
ff_set_dimensions
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:94
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:514
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
GetBitContext
Definition: get_bits.h:108
SeqVideoContext::avctx
AVCodecContext * avctx
Definition: tiertexseqv.c:35
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
av_cold
#define av_cold
Definition: attributes.h:90
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
op
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
bits
uint8_t bits
Definition: vp3data.h:128
get_sbits
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:320
AV_CODEC_ID_TIERTEXSEQVIDEO
@ AV_CODEC_ID_TIERTEXSEQVIDEO
Definition: codec_id.h:147
decode.h
get_bits.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
if
if(ret)
Definition: filter_design.txt:179
NULL
#define NULL
Definition: coverity.c:32
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ff_log2_tab
const uint8_t ff_log2_tab[256]
Definition: log2_tab.c:23
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
av_frame_ref
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:388
codec_internal.h
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
seqvideo_decode_init
static av_cold int seqvideo_decode_init(AVCodecContext *avctx)
Definition: tiertexseqv.c:218
seq_unpack_rle_block
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
seq_decode_op1
static const unsigned char * seq_decode_op1(SeqVideoContext *seq, const unsigned char *src, const unsigned char *src_end, unsigned char *dst)
Definition: tiertexseqv.c:78
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:663
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:84
ff_reget_buffer
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:1815
ret
ret
Definition: filter_design.txt:187
seq_decode_op3
static const unsigned char * seq_decode_op3(SeqVideoContext *seq, const unsigned char *src, const unsigned char *src_end, unsigned char *dst)
Definition: tiertexseqv.c:147
seq_decode_op2
static const unsigned char * seq_decode_op2(SeqVideoContext *seq, const unsigned char *src, const unsigned char *src_end, unsigned char *dst)
Definition: tiertexseqv.c:128
pos
unsigned int pos
Definition: spdifenc.c:414
U
#define U(x)
Definition: vpx_arith.h:37
seqvideo_decode
static int seqvideo_decode(SeqVideoContext *seq, const unsigned char *data, int data_size)
Definition: tiertexseqv.c:165
AVCodecContext
main external API structure.
Definition: avcodec.h:451
ff_tiertexseqvideo_decoder
const FFCodec ff_tiertexseqvideo_decoder
Definition: tiertexseqv.c:268
seqvideo_decode_frame
static int seqvideo_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: tiertexseqv.c:237
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
AVFrame::linesize
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:434
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_RB24
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:97
color_table
static const ColorEntry color_table[]
Definition: xpmdec.c:51
src
#define src
Definition: vp8dsp.c:248