]> git.sesse.net Git - ffmpeg/blob - libavcodec/escape130.c
escape130: make sure cliping is done on signed values.
[ffmpeg] / libavcodec / escape130.c
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 ALT_BITSTREAM_READER_LE
25 #include "get_bits.h"
26
27 typedef struct Escape130Context {
28     AVFrame frame;
29 } Escape130Context;
30
31 static int can_safely_read(GetBitContext* gb, int bits) {
32     return get_bits_count(gb) + bits <= gb->size_in_bits;
33 }
34
35 /**
36  * Initialize the decoder
37  * @param avctx decoder context
38  * @return 0 success, negative on error
39  */
40 static av_cold int escape130_decode_init(AVCodecContext *avctx)
41 {
42     avctx->pix_fmt = PIX_FMT_YUV420P;
43
44     return 0;
45 }
46
47 static av_cold int escape130_decode_close(AVCodecContext *avctx)
48 {
49     Escape130Context *s = avctx->priv_data;
50
51     if (s->frame.data[0])
52         avctx->release_buffer(avctx, &s->frame);
53
54     return 0;
55 }
56
57 static unsigned decode_skip_count(GetBitContext* gb) {
58     unsigned value;
59     // This function reads a maximum of 27 bits,
60     // which is within the padding space
61     if (!can_safely_read(gb, 1))
62         return -1;
63
64     value = get_bits1(gb);
65     if (value)
66         return 0;
67
68     value = get_bits(gb, 3);
69     if (value)
70         return value;
71
72     value = get_bits(gb, 8);
73     if (value)
74         return value + 7;
75
76     value = get_bits(gb, 15);
77     if (value)
78         return value + 262;
79
80     return -1;
81 }
82
83 /**
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
91  */
92 static int escape130_decode_frame(AVCodecContext *avctx,
93                                   void *data, int *data_size,
94                                   AVPacket *avpkt)
95 {
96     const uint8_t *buf = avpkt->data;
97     int buf_size = avpkt->size;
98     Escape130Context *s = avctx->priv_data;
99
100     GetBitContext gb;
101     unsigned i;
102
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;
110     unsigned skip = -1;
111
112     AVFrame new_frame = { { 0 } };
113
114     init_get_bits(&gb, buf, buf_size * 8);
115
116     if (!can_safely_read(&gb, 128))
117         return -1;
118
119     // Header; no useful information in here
120     skip_bits_long(&gb, 128);
121
122     new_frame.reference = 3;
123     if (avctx->get_buffer(avctx, &new_frame)) {
124         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
125         return -1;
126     }
127
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];
140
141     av_log(NULL, AV_LOG_DEBUG,
142            "Strides: %i, %i\n",
143            new_y_stride, new_cb_stride);
144
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
148         if (skip == -1)
149             skip = decode_skip_count(&gb);
150
151         if (skip) {
152             if (old_y) {
153                 y[0] = old_y[0] / 4;
154                 y[1] = old_y[1] / 4;
155                 y[2] = old_y[old_y_stride] / 4;
156                 y[3] = old_y[old_y_stride+1] / 4;
157                 cb = old_cb[0] / 8;
158                 cr = old_cr[0] / 8;
159             } else {
160                 y[0] = y[1] = y[2] = y[3] = 0;
161                 cb = cr = 16;
162             }
163         } else {
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] =
170                     { {0, 0, 0, 0},
171                       {-1, 1, 0, 0},
172                       {1, -1, 0, 0},
173                       {-1, 0, 1, 0},
174                       {-1, 1, 1, 0},
175                       {0, -1, 1, 0},
176                       {1, -1, 1, 0},
177                       {-1, -1, 1, 0},
178                       {1, 0, -1, 0},
179                       {0, 1, -1, 0},
180                       {1, 1, -1, 0},
181                       {-1, 1, -1, 0},
182                       {1, -1, -1, 0},
183                       {-1, 0, 0, 1},
184                       {-1, 1, 0, 1},
185                       {0, -1, 0, 1},
186
187                       {0, 0, 0, 0},
188                       {1, -1, 0, 1},
189                       {-1, -1, 0, 1},
190                       {-1, 0, 1, 1},
191                       {-1, 1, 1, 1},
192                       {0, -1, 1, 1},
193                       {1, -1, 1, 1},
194                       {-1, -1, 1, 1},
195                       {0, 0, -1, 1},
196                       {1, 0, -1, 1},
197                       {-1, 0, -1, 1},
198                       {0, 1, -1, 1},
199                       {1, 1, -1, 1},
200                       {-1, 1, -1, 1},
201                       {0, -1, -1, 1},
202                       {1, -1, -1, 1},
203
204                       {0, 0, 0, 0},
205                       {-1, -1, -1, 1},
206                       {1, 0, 0, -1},
207                       {0, 1, 0, -1},
208                       {1, 1, 0, -1},
209                       {-1, 1, 0, -1},
210                       {1, -1, 0, -1},
211                       {0, 0, 1, -1},
212                       {1, 0, 1, -1},
213                       {-1, 0, 1, -1},
214                       {0, 1, 1, -1},
215                       {1, 1, 1, -1},
216                       {-1, 1, 1, -1},
217                       {0, -1, 1, -1},
218                       {1, -1, 1, -1},
219                       {-1, -1, 1, -1},
220
221                       {0, 0, 0, 0},
222                       {1, 0, -1, -1},
223                       {0, 1, -1, -1},
224                       {1, 1, -1, -1},
225                       {-1, 1, -1, -1},
226                       {1, -1, -1, -1} };
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);
230                 }
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++)
235                         y[i] = y_base;
236                 } else {
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);
241                 }
242             }
243
244             if (get_bits1(&gb)) {
245                 if (get_bits1(&gb)) {
246                     cb = get_bits(&gb, 5);
247                     cr = get_bits(&gb, 5);
248                 } else {
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;
255                 }
256             }
257         }
258
259         new_y[0] = y[0] * 4;
260         new_y[1] = y[1] * 4;
261         new_y[new_y_stride] = y[2] * 4;
262         new_y[new_y_stride + 1] = y[3] * 4;
263         *new_cb = cb * 8;
264         *new_cr = cr * 8;
265
266         if (old_y)
267             old_y += 2, old_cb++, old_cr++;
268         new_y += 2, new_cb++, new_cr++;
269         row_index++;
270         if (avctx->width / 2 == row_index) {
271             row_index = 0;
272             if (old_y) {
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;
276             }
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;
280         }
281
282         skip--;
283     }
284
285     av_log(NULL, AV_LOG_DEBUG,
286            "Escape sizes: %i, %i\n",
287            buf_size, get_bits_count(&gb) / 8);
288
289     if (s->frame.data[0])
290         avctx->release_buffer(avctx, &s->frame);
291
292     *(AVFrame*)data = s->frame = new_frame;
293     *data_size = sizeof(AVFrame);
294
295     return buf_size;
296 }
297
298
299 AVCodec ff_escape130_decoder = {
300     .name           = "escape130",
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"),
309 };