2 * Escape 130 Video Decoder
3 * Copyright (C) 2008 Eli Friedman (eli.friedman <at> gmail.com)
5 * This file is part of FFmpeg.
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.
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.
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
24 #define BITSTREAM_READER_LE
27 typedef struct Escape130Context {
33 * Initialize the decoder
34 * @param avctx decoder context
35 * @return 0 success, negative on error
37 static av_cold int escape130_decode_init(AVCodecContext *avctx)
39 Escape130Context *s = avctx->priv_data;
40 avctx->pix_fmt = PIX_FMT_YUV420P;
42 if((avctx->width&1) || (avctx->height&1)){
43 av_log(avctx, AV_LOG_ERROR, "Dimensions are not a multiple of the block size\n");
44 return AVERROR(EINVAL);
47 s->bases= av_malloc(avctx->width * avctx->height /4);
52 static av_cold int escape130_decode_close(AVCodecContext *avctx)
54 Escape130Context *s = avctx->priv_data;
57 avctx->release_buffer(avctx, &s->frame);
64 static unsigned decode_skip_count(GetBitContext* gb) {
66 // This function reads a maximum of 27 bits,
67 // which is within the padding space
68 if (get_bits_left(gb) < 1+3)
71 value = get_bits1(gb);
75 value = get_bits(gb, 3);
79 value = get_bits(gb, 8);
83 value = get_bits(gb, 15);
91 * Decode a single frame
92 * @param avctx decoder context
93 * @param data decoded frame
94 * @param data_size size of the decoded frame
95 * @param buf input buffer
96 * @param buf_size input buffer size
97 * @return 0 success, -1 on error
99 static int escape130_decode_frame(AVCodecContext *avctx,
100 void *data, int *data_size,
103 const uint8_t *buf = avpkt->data;
104 int buf_size = avpkt->size;
105 Escape130Context *s = avctx->priv_data;
110 uint8_t *old_y, *old_cb, *old_cr,
111 *new_y, *new_cb, *new_cr;
112 unsigned old_y_stride, old_cb_stride, old_cr_stride,
113 new_y_stride, new_cb_stride, new_cr_stride;
114 unsigned total_blocks = avctx->width * avctx->height / 4,
115 block_index, row_index = 0;
116 unsigned y[4] = {0}, cb = 16, cr = 16;
119 uint8_t *yb= s->bases;
121 AVFrame new_frame = { { 0 } };
123 init_get_bits(&gb, buf, buf_size * 8);
125 if (get_bits_left(&gb) < 128)
128 // Header; no useful information in here
129 skip_bits_long(&gb, 128);
131 new_frame.reference = 3;
132 if (avctx->get_buffer(avctx, &new_frame)) {
133 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
137 new_y = new_frame.data[0];
138 new_cb = new_frame.data[1];
139 new_cr = new_frame.data[2];
140 new_y_stride = new_frame.linesize[0];
141 new_cb_stride = new_frame.linesize[1];
142 new_cr_stride = new_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];
150 av_log(avctx, AV_LOG_DEBUG,
152 new_y_stride, new_cb_stride);
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
158 skip = decode_skip_count(&gb);
164 y[2] = old_y[old_y_stride] / 4;
165 y[3] = old_y[old_y_stride+1] / 4;
170 y_base=y[0] = y[1] = y[2] = y[3] = 0;
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] =
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);
241 } else if (get_bits1(&gb)) {
242 if (get_bits1(&gb)) {
243 y_base = get_bits(&gb, 6);
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;
249 for (i = 0; i < 4; i++)
253 if (get_bits1(&gb)) {
254 if (get_bits1(&gb)) {
255 cb = get_bits(&gb, 5);
256 cr = get_bits(&gb, 5);
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;
271 new_y[new_y_stride] = y[2] * 4;
272 new_y[new_y_stride + 1] = y[3] * 4;
277 old_y += 2, old_cb++, old_cr++;
278 new_y += 2, new_cb++, new_cr++;
280 if (avctx->width / 2 == row_index) {
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;
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;
295 av_log(avctx, AV_LOG_DEBUG,
296 "Escape sizes: %i, %i\n",
297 buf_size, get_bits_count(&gb) / 8);
299 if (s->frame.data[0])
300 avctx->release_buffer(avctx, &s->frame);
302 *(AVFrame*)data = s->frame = new_frame;
303 *data_size = sizeof(AVFrame);
309 AVCodec ff_escape130_decoder = {
311 .type = AVMEDIA_TYPE_VIDEO,
312 .id = CODEC_ID_ESCAPE130,
313 .priv_data_size = sizeof(Escape130Context),
314 .init = escape130_decode_init,
315 .close = escape130_decode_close,
316 .decode = escape130_decode_frame,
317 .capabilities = CODEC_CAP_DR1,
318 .long_name = NULL_IF_CONFIG_SMALL("Escape 130"),