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 ALT_BITSTREAM_READER_LE
27 typedef struct Escape130Context {
31 static int can_safely_read(GetBitContext* gb, int bits) {
32 return get_bits_count(gb) + bits <= gb->size_in_bits;
36 * Initialize the decoder
37 * @param avctx decoder context
38 * @return 0 success, negative on error
40 static av_cold int escape130_decode_init(AVCodecContext *avctx)
42 avctx->pix_fmt = PIX_FMT_YUV420P;
47 static av_cold int escape130_decode_close(AVCodecContext *avctx)
49 Escape130Context *s = avctx->priv_data;
52 avctx->release_buffer(avctx, &s->frame);
57 static unsigned decode_skip_count(GetBitContext* gb) {
59 // This function reads a maximum of 27 bits,
60 // which is within the padding space
61 if (!can_safely_read(gb, 1))
64 value = get_bits1(gb);
68 value = get_bits(gb, 3);
72 value = get_bits(gb, 8);
76 value = get_bits(gb, 15);
84 * Decode a single frame
85 * @param avctx decoder context
86 * @param data decoded frame
87 * @param data_size size of the decoded frame
88 * @param buf input buffer
89 * @param buf_size input buffer size
90 * @return 0 success, -1 on error
92 static int escape130_decode_frame(AVCodecContext *avctx,
93 void *data, int *data_size,
96 const uint8_t *buf = avpkt->data;
97 int buf_size = avpkt->size;
98 Escape130Context *s = avctx->priv_data;
103 uint8_t *old_y, *old_cb, *old_cr,
104 *new_y, *new_cb, *new_cr;
105 unsigned old_y_stride, old_cb_stride, old_cr_stride,
106 new_y_stride, new_cb_stride, new_cr_stride;
107 unsigned total_blocks = avctx->width * avctx->height / 4,
108 block_index, row_index = 0;
109 unsigned y[4] = {0}, cb = 16, cr = 16;
112 AVFrame new_frame = { { 0 } };
114 init_get_bits(&gb, buf, buf_size * 8);
116 if (!can_safely_read(&gb, 128))
119 // Header; no useful information in here
120 skip_bits_long(&gb, 128);
122 new_frame.reference = 3;
123 if (avctx->get_buffer(avctx, &new_frame)) {
124 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
128 new_y = new_frame.data[0];
129 new_cb = new_frame.data[1];
130 new_cr = new_frame.data[2];
131 new_y_stride = new_frame.linesize[0];
132 new_cb_stride = new_frame.linesize[1];
133 new_cr_stride = new_frame.linesize[2];
134 old_y = s->frame.data[0];
135 old_cb = s->frame.data[1];
136 old_cr = s->frame.data[2];
137 old_y_stride = s->frame.linesize[0];
138 old_cb_stride = s->frame.linesize[1];
139 old_cr_stride = s->frame.linesize[2];
141 av_log(NULL, AV_LOG_DEBUG,
143 new_y_stride, new_cb_stride);
145 for (block_index = 0; block_index < total_blocks; block_index++) {
146 // Note that this call will make us skip the rest of the blocks
147 // if the frame prematurely ends
149 skip = decode_skip_count(&gb);
155 y[2] = old_y[old_y_stride] / 4;
156 y[3] = old_y[old_y_stride+1] / 4;
160 y[0] = y[1] = y[2] = y[3] = 0;
164 if (get_bits1(&gb)) {
165 unsigned sign_selector = get_bits(&gb, 6);
166 unsigned difference_selector = get_bits(&gb, 2);
167 unsigned y_base = 2 * get_bits(&gb, 5);
168 static const uint8_t offset_table[] = {2, 4, 10, 20};
169 static const int8_t sign_table[64][4] =
227 for (i = 0; i < 4; i++) {
228 y[i] = av_clip((int)y_base + offset_table[difference_selector] *
229 sign_table[sign_selector][i], 0, 63);
231 } else if (get_bits1(&gb)) {
232 if (get_bits1(&gb)) {
233 unsigned y_base = get_bits(&gb, 6);
234 for (i = 0; i < 4; i++)
237 unsigned adjust_index = get_bits(&gb, 3);
238 static const int8_t adjust[] = {-4, -3, -2, -1, 1, 2, 3, 4};
239 for (i = 0; i < 4; i++)
240 y[i] = av_clip(y[i] + adjust[adjust_index], 0, 63);
244 if (get_bits1(&gb)) {
245 if (get_bits1(&gb)) {
246 cb = get_bits(&gb, 5);
247 cr = get_bits(&gb, 5);
249 unsigned adjust_index = get_bits(&gb, 3);
250 static const int8_t adjust[2][8] =
251 { { 1, 1, 0, -1, -1, -1, 0, 1 },
252 { 0, 1, 1, 1, 0, -1, -1, -1 } };
253 cb = (cb + adjust[0][adjust_index]) & 31;
254 cr = (cr + adjust[1][adjust_index]) & 31;
261 new_y[new_y_stride] = y[2] * 4;
262 new_y[new_y_stride + 1] = y[3] * 4;
267 old_y += 2, old_cb++, old_cr++;
268 new_y += 2, new_cb++, new_cr++;
270 if (avctx->width / 2 == row_index) {
273 old_y += old_y_stride * 2 - avctx->width;
274 old_cb += old_cb_stride - avctx->width / 2;
275 old_cr += old_cr_stride - avctx->width / 2;
277 new_y += new_y_stride * 2 - avctx->width;
278 new_cb += new_cb_stride - avctx->width / 2;
279 new_cr += new_cr_stride - avctx->width / 2;
285 av_log(NULL, AV_LOG_DEBUG,
286 "Escape sizes: %i, %i\n",
287 buf_size, get_bits_count(&gb) / 8);
289 if (s->frame.data[0])
290 avctx->release_buffer(avctx, &s->frame);
292 *(AVFrame*)data = s->frame = new_frame;
293 *data_size = sizeof(AVFrame);
299 AVCodec ff_escape130_decoder = {
301 .type = AVMEDIA_TYPE_VIDEO,
302 .id = CODEC_ID_ESCAPE130,
303 .priv_data_size = sizeof(Escape130Context),
304 .init = escape130_decode_init,
305 .close = escape130_decode_close,
306 .decode = escape130_decode_frame,
307 .capabilities = CODEC_CAP_DR1,
308 .long_name = NULL_IF_CONFIG_SMALL("Escape 130"),