escape130.c
Go to the documentation of this file.
1 /*
2  * Escape 130 Video Decoder
3  * Copyright (C) 2008 Eli Friedman (eli.friedman <at> gmail.com)
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 #include "avcodec.h"
23 
24 #define BITSTREAM_READER_LE
25 #include "get_bits.h"
26 #include "internal.h"
27 
28 
29 typedef struct Escape130Context {
33 
34 /**
35  * Initialize the decoder
36  * @param avctx decoder context
37  * @return 0 success, negative on error
38  */
40 {
41  Escape130Context *s = avctx->priv_data;
42  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
44 
45  if((avctx->width&1) || (avctx->height&1)){
46  av_log(avctx, AV_LOG_ERROR, "Dimensions are not a multiple of the block size\n");
47  return AVERROR(EINVAL);
48  }
49 
50  s->bases= av_malloc(avctx->width * avctx->height /4);
51 
52  return 0;
53 }
54 
56 {
57  Escape130Context *s = avctx->priv_data;
58 
59  av_frame_unref(&s->frame);
60 
61  av_freep(&s->bases);
62 
63  return 0;
64 }
65 
66 static unsigned decode_skip_count(GetBitContext* gb) {
67  unsigned value;
68  // This function reads a maximum of 27 bits,
69  // which is within the padding space
70  if (get_bits_left(gb) < 1+3)
71  return -1;
72 
73  value = get_bits1(gb);
74  if (value)
75  return 0;
76 
77  value = get_bits(gb, 3);
78  if (value)
79  return value;
80 
81  value = get_bits(gb, 8);
82  if (value)
83  return value + 7;
84 
85  value = get_bits(gb, 15);
86  if (value)
87  return value + 262;
88 
89  return -1;
90 }
91 
92 /**
93  * Decode a single frame
94  * @param avctx decoder context
95  * @param data decoded frame
96  * @param got_frame have decoded frame
97  * @param buf input buffer
98  * @param buf_size input buffer size
99  * @return 0 success, -1 on error
100  */
102  void *data, int *got_frame,
103  AVPacket *avpkt)
104 {
105  const uint8_t *buf = avpkt->data;
106  int buf_size = avpkt->size;
107  Escape130Context *s = avctx->priv_data;
108 
109  GetBitContext gb;
110  unsigned i;
111  int ret;
112 
113  uint8_t *old_y, *old_cb, *old_cr,
114  *new_y, *new_cb, *new_cr;
115  unsigned old_y_stride, old_cb_stride, old_cr_stride,
116  new_y_stride, new_cb_stride, new_cr_stride;
117  unsigned total_blocks = avctx->width * avctx->height / 4,
118  block_index, row_index = 0;
119  unsigned y[4] = {0}, cb = 16, cr = 16;
120  unsigned skip = -1;
121  unsigned y_base = 0;
122  uint8_t *yb= s->bases;
123 
124  AVFrame *frame = data;
125 
126  init_get_bits(&gb, buf, buf_size * 8);
127 
128  if (get_bits_left(&gb) < 128)
129  return -1;
130 
131  // Header; no useful information in here
132  skip_bits_long(&gb, 128);
133 
134  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
135  return ret;
136 
137  new_y = frame->data[0];
138  new_cb = frame->data[1];
139  new_cr = frame->data[2];
140  new_y_stride = frame->linesize[0];
141  new_cb_stride = frame->linesize[1];
142  new_cr_stride = frame->linesize[2];
143  old_y = s->frame.data[0];
144  old_cb = s->frame.data[1];
145  old_cr = s->frame.data[2];
146  old_y_stride = s->frame.linesize[0];
147  old_cb_stride = s->frame.linesize[1];
148  old_cr_stride = s->frame.linesize[2];
149 
150  av_log(avctx, AV_LOG_DEBUG,
151  "Strides: %i, %i\n",
152  new_y_stride, new_cb_stride);
153 
154  for (block_index = 0; block_index < total_blocks; block_index++) {
155  // Note that this call will make us skip the rest of the blocks
156  // if the frame prematurely ends
157  if (skip == -1)
158  skip = decode_skip_count(&gb);
159 
160  if (skip) {
161  if (old_y) {
162  y[0] = old_y[0] / 4;
163  y[1] = old_y[1] / 4;
164  y[2] = old_y[old_y_stride] / 4;
165  y[3] = old_y[old_y_stride+1] / 4;
166  y_base= yb[0];
167  cb = old_cb[0] / 8;
168  cr = old_cr[0] / 8;
169  } else {
170  y_base=y[0] = y[1] = y[2] = y[3] = 0;
171  cb = cr = 16;
172  }
173  } else {
174  if (get_bits1(&gb)) {
175  static const uint8_t offset_table[] = {2, 4, 10, 20};
176  static const int8_t sign_table[64][4] =
177  { {0, 0, 0, 0},
178  {-1, 1, 0, 0},
179  {1, -1, 0, 0},
180  {-1, 0, 1, 0},
181  {-1, 1, 1, 0},
182  {0, -1, 1, 0},
183  {1, -1, 1, 0},
184  {-1, -1, 1, 0},
185  {1, 0, -1, 0},
186  {0, 1, -1, 0},
187  {1, 1, -1, 0},
188  {-1, 1, -1, 0},
189  {1, -1, -1, 0},
190  {-1, 0, 0, 1},
191  {-1, 1, 0, 1},
192  {0, -1, 0, 1},
193 
194  {0, 0, 0, 0},
195  {1, -1, 0, 1},
196  {-1, -1, 0, 1},
197  {-1, 0, 1, 1},
198  {-1, 1, 1, 1},
199  {0, -1, 1, 1},
200  {1, -1, 1, 1},
201  {-1, -1, 1, 1},
202  {0, 0, -1, 1},
203  {1, 0, -1, 1},
204  {-1, 0, -1, 1},
205  {0, 1, -1, 1},
206  {1, 1, -1, 1},
207  {-1, 1, -1, 1},
208  {0, -1, -1, 1},
209  {1, -1, -1, 1},
210 
211  {0, 0, 0, 0},
212  {-1, -1, -1, 1},
213  {1, 0, 0, -1},
214  {0, 1, 0, -1},
215  {1, 1, 0, -1},
216  {-1, 1, 0, -1},
217  {1, -1, 0, -1},
218  {0, 0, 1, -1},
219  {1, 0, 1, -1},
220  {-1, 0, 1, -1},
221  {0, 1, 1, -1},
222  {1, 1, 1, -1},
223  {-1, 1, 1, -1},
224  {0, -1, 1, -1},
225  {1, -1, 1, -1},
226  {-1, -1, 1, -1},
227 
228  {0, 0, 0, 0},
229  {1, 0, -1, -1},
230  {0, 1, -1, -1},
231  {1, 1, -1, -1},
232  {-1, 1, -1, -1},
233  {1, -1, -1, -1} };
234  unsigned sign_selector = get_bits(&gb, 6);
235  unsigned difference_selector = get_bits(&gb, 2);
236  y_base = 2 * get_bits(&gb, 5);
237  for (i = 0; i < 4; i++) {
238  y[i] = av_clip((int)y_base + offset_table[difference_selector] *
239  sign_table[sign_selector][i], 0, 63);
240  }
241  } else if (get_bits1(&gb)) {
242  if (get_bits1(&gb)) {
243  y_base = get_bits(&gb, 6);
244  } else {
245  unsigned adjust_index = get_bits(&gb, 3);
246  static const int8_t adjust[] = {-4, -3, -2, -1, 1, 2, 3, 4};
247  y_base = (y_base + adjust[adjust_index]) & 63;
248  }
249  for (i = 0; i < 4; i++)
250  y[i] = y_base;
251  }
252 
253  if (get_bits1(&gb)) {
254  if (get_bits1(&gb)) {
255  cb = get_bits(&gb, 5);
256  cr = get_bits(&gb, 5);
257  } else {
258  unsigned adjust_index = get_bits(&gb, 3);
259  static const int8_t adjust[2][8] =
260  { { 1, 1, 0, -1, -1, -1, 0, 1 },
261  { 0, 1, 1, 1, 0, -1, -1, -1 } };
262  cb = (cb + adjust[0][adjust_index]) & 31;
263  cr = (cr + adjust[1][adjust_index]) & 31;
264  }
265  }
266  }
267  *yb++= y_base;
268 
269  new_y[0] = y[0] * 4;
270  new_y[1] = y[1] * 4;
271  new_y[new_y_stride] = y[2] * 4;
272  new_y[new_y_stride + 1] = y[3] * 4;
273  *new_cb = cb * 8;
274  *new_cr = cr * 8;
275 
276  if (old_y)
277  old_y += 2, old_cb++, old_cr++;
278  new_y += 2, new_cb++, new_cr++;
279  row_index++;
280  if (avctx->width / 2 == row_index) {
281  row_index = 0;
282  if (old_y) {
283  old_y += old_y_stride * 2 - avctx->width;
284  old_cb += old_cb_stride - avctx->width / 2;
285  old_cr += old_cr_stride - avctx->width / 2;
286  }
287  new_y += new_y_stride * 2 - avctx->width;
288  new_cb += new_cb_stride - avctx->width / 2;
289  new_cr += new_cr_stride - avctx->width / 2;
290  }
291 
292  skip--;
293  }
294 
295  av_log(avctx, AV_LOG_DEBUG,
296  "Escape sizes: %i, %i\n",
297  buf_size, get_bits_count(&gb) / 8);
298 
299  av_frame_unref(&s->frame);
300  if ((ret = av_frame_ref(&s->frame, frame)) < 0)
301  return ret;
302 
303  *got_frame = 1;
304 
305  return buf_size;
306 }
307 
308 
310  .name = "escape130",
311  .type = AVMEDIA_TYPE_VIDEO,
312  .id = AV_CODEC_ID_ESCAPE130,
313  .priv_data_size = sizeof(Escape130Context),
317  .capabilities = CODEC_CAP_DR1,
318  .long_name = NULL_IF_CONFIG_SMALL("Escape 130"),
319 };
uint8_t * bases
Definition: escape130.c:31
const char * s
Definition: avisynth_c.h:668
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:198
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static av_cold int escape130_decode_close(AVCodecContext *avctx)
Definition: escape130.c:55
uint8_t
#define av_cold
Definition: attributes.h:78
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:193
bitstream reader API header.
AVFrame frame
Definition: escape130.c:30
static int escape130_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Decode a single frame.
Definition: escape130.c:101
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:557
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
static unsigned decode_skip_count(GetBitContext *gb)
Definition: escape130.c:66
external API header
ret
Definition: avfilter.c:821
int width
picture width / height.
static const int offset_table[6]
Definition: vc1dec.c:3315
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
FIXME Range Coding of cb
Definition: snow.txt:367
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
struct Escape130Context Escape130Context
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
AVCodec ff_escape130_decoder
Definition: escape130.c:309
void * buf
Definition: avisynth_c.h:594
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:273
double value
Definition: eval.c:82
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
synthesis window for stochastic i
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:330
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by an given frame.
Definition: frame.c:228
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
common internal api header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
function y
Definition: D.m:1
static av_cold int escape130_decode_init(AVCodecContext *avctx)
Initialize the decoder.
Definition: escape130.c:39
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:85
This structure stores compressed data.
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.