00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023
00024 #define BITSTREAM_READER_LE
00025 #include "get_bits.h"
00026
00027 typedef struct Escape130Context {
00028 AVFrame frame;
00029 uint8_t *bases;
00030 } Escape130Context;
00031
00037 static av_cold int escape130_decode_init(AVCodecContext *avctx)
00038 {
00039 Escape130Context *s = avctx->priv_data;
00040 avctx->pix_fmt = PIX_FMT_YUV420P;
00041
00042 if((avctx->width&1) || (avctx->height&1)){
00043 av_log(avctx, AV_LOG_ERROR, "Dimensions are not a multiple of the block size\n");
00044 return AVERROR(EINVAL);
00045 }
00046
00047 s->bases= av_malloc(avctx->width * avctx->height /4);
00048
00049 return 0;
00050 }
00051
00052 static av_cold int escape130_decode_close(AVCodecContext *avctx)
00053 {
00054 Escape130Context *s = avctx->priv_data;
00055
00056 if (s->frame.data[0])
00057 avctx->release_buffer(avctx, &s->frame);
00058
00059 av_freep(&s->bases);
00060
00061 return 0;
00062 }
00063
00064 static unsigned decode_skip_count(GetBitContext* gb) {
00065 unsigned value;
00066
00067
00068 if (get_bits_left(gb) < 1+3)
00069 return -1;
00070
00071 value = get_bits1(gb);
00072 if (value)
00073 return 0;
00074
00075 value = get_bits(gb, 3);
00076 if (value)
00077 return value;
00078
00079 value = get_bits(gb, 8);
00080 if (value)
00081 return value + 7;
00082
00083 value = get_bits(gb, 15);
00084 if (value)
00085 return value + 262;
00086
00087 return -1;
00088 }
00089
00099 static int escape130_decode_frame(AVCodecContext *avctx,
00100 void *data, int *data_size,
00101 AVPacket *avpkt)
00102 {
00103 const uint8_t *buf = avpkt->data;
00104 int buf_size = avpkt->size;
00105 Escape130Context *s = avctx->priv_data;
00106
00107 GetBitContext gb;
00108 unsigned i;
00109
00110 uint8_t *old_y, *old_cb, *old_cr,
00111 *new_y, *new_cb, *new_cr;
00112 unsigned old_y_stride, old_cb_stride, old_cr_stride,
00113 new_y_stride, new_cb_stride, new_cr_stride;
00114 unsigned total_blocks = avctx->width * avctx->height / 4,
00115 block_index, row_index = 0;
00116 unsigned y[4] = {0}, cb = 16, cr = 16;
00117 unsigned skip = -1;
00118 unsigned y_base = 0;
00119 uint8_t *yb= s->bases;
00120
00121 AVFrame new_frame = { { 0 } };
00122
00123 init_get_bits(&gb, buf, buf_size * 8);
00124
00125 if (get_bits_left(&gb) < 128)
00126 return -1;
00127
00128
00129 skip_bits_long(&gb, 128);
00130
00131 new_frame.reference = 3;
00132 if (avctx->get_buffer(avctx, &new_frame)) {
00133 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00134 return -1;
00135 }
00136
00137 new_y = new_frame.data[0];
00138 new_cb = new_frame.data[1];
00139 new_cr = new_frame.data[2];
00140 new_y_stride = new_frame.linesize[0];
00141 new_cb_stride = new_frame.linesize[1];
00142 new_cr_stride = new_frame.linesize[2];
00143 old_y = s->frame.data[0];
00144 old_cb = s->frame.data[1];
00145 old_cr = s->frame.data[2];
00146 old_y_stride = s->frame.linesize[0];
00147 old_cb_stride = s->frame.linesize[1];
00148 old_cr_stride = s->frame.linesize[2];
00149
00150 av_log(avctx, AV_LOG_DEBUG,
00151 "Strides: %i, %i\n",
00152 new_y_stride, new_cb_stride);
00153
00154 for (block_index = 0; block_index < total_blocks; block_index++) {
00155
00156
00157 if (skip == -1)
00158 skip = decode_skip_count(&gb);
00159
00160 if (skip) {
00161 if (old_y) {
00162 y[0] = old_y[0] / 4;
00163 y[1] = old_y[1] / 4;
00164 y[2] = old_y[old_y_stride] / 4;
00165 y[3] = old_y[old_y_stride+1] / 4;
00166 y_base= yb[0];
00167 cb = old_cb[0] / 8;
00168 cr = old_cr[0] / 8;
00169 } else {
00170 y_base=y[0] = y[1] = y[2] = y[3] = 0;
00171 cb = cr = 16;
00172 }
00173 } else {
00174 if (get_bits1(&gb)) {
00175 static const uint8_t offset_table[] = {2, 4, 10, 20};
00176 static const int8_t sign_table[64][4] =
00177 { {0, 0, 0, 0},
00178 {-1, 1, 0, 0},
00179 {1, -1, 0, 0},
00180 {-1, 0, 1, 0},
00181 {-1, 1, 1, 0},
00182 {0, -1, 1, 0},
00183 {1, -1, 1, 0},
00184 {-1, -1, 1, 0},
00185 {1, 0, -1, 0},
00186 {0, 1, -1, 0},
00187 {1, 1, -1, 0},
00188 {-1, 1, -1, 0},
00189 {1, -1, -1, 0},
00190 {-1, 0, 0, 1},
00191 {-1, 1, 0, 1},
00192 {0, -1, 0, 1},
00193
00194 {0, 0, 0, 0},
00195 {1, -1, 0, 1},
00196 {-1, -1, 0, 1},
00197 {-1, 0, 1, 1},
00198 {-1, 1, 1, 1},
00199 {0, -1, 1, 1},
00200 {1, -1, 1, 1},
00201 {-1, -1, 1, 1},
00202 {0, 0, -1, 1},
00203 {1, 0, -1, 1},
00204 {-1, 0, -1, 1},
00205 {0, 1, -1, 1},
00206 {1, 1, -1, 1},
00207 {-1, 1, -1, 1},
00208 {0, -1, -1, 1},
00209 {1, -1, -1, 1},
00210
00211 {0, 0, 0, 0},
00212 {-1, -1, -1, 1},
00213 {1, 0, 0, -1},
00214 {0, 1, 0, -1},
00215 {1, 1, 0, -1},
00216 {-1, 1, 0, -1},
00217 {1, -1, 0, -1},
00218 {0, 0, 1, -1},
00219 {1, 0, 1, -1},
00220 {-1, 0, 1, -1},
00221 {0, 1, 1, -1},
00222 {1, 1, 1, -1},
00223 {-1, 1, 1, -1},
00224 {0, -1, 1, -1},
00225 {1, -1, 1, -1},
00226 {-1, -1, 1, -1},
00227
00228 {0, 0, 0, 0},
00229 {1, 0, -1, -1},
00230 {0, 1, -1, -1},
00231 {1, 1, -1, -1},
00232 {-1, 1, -1, -1},
00233 {1, -1, -1, -1} };
00234 unsigned sign_selector = get_bits(&gb, 6);
00235 unsigned difference_selector = get_bits(&gb, 2);
00236 y_base = 2 * get_bits(&gb, 5);
00237 for (i = 0; i < 4; i++) {
00238 y[i] = av_clip((int)y_base + offset_table[difference_selector] *
00239 sign_table[sign_selector][i], 0, 63);
00240 }
00241 } else if (get_bits1(&gb)) {
00242 if (get_bits1(&gb)) {
00243 y_base = get_bits(&gb, 6);
00244 } else {
00245 unsigned adjust_index = get_bits(&gb, 3);
00246 static const int8_t adjust[] = {-4, -3, -2, -1, 1, 2, 3, 4};
00247 y_base = (y_base + adjust[adjust_index]) & 63;
00248 }
00249 for (i = 0; i < 4; i++)
00250 y[i] = y_base;
00251 }
00252
00253 if (get_bits1(&gb)) {
00254 if (get_bits1(&gb)) {
00255 cb = get_bits(&gb, 5);
00256 cr = get_bits(&gb, 5);
00257 } else {
00258 unsigned adjust_index = get_bits(&gb, 3);
00259 static const int8_t adjust[2][8] =
00260 { { 1, 1, 0, -1, -1, -1, 0, 1 },
00261 { 0, 1, 1, 1, 0, -1, -1, -1 } };
00262 cb = (cb + adjust[0][adjust_index]) & 31;
00263 cr = (cr + adjust[1][adjust_index]) & 31;
00264 }
00265 }
00266 }
00267 *yb++= y_base;
00268
00269 new_y[0] = y[0] * 4;
00270 new_y[1] = y[1] * 4;
00271 new_y[new_y_stride] = y[2] * 4;
00272 new_y[new_y_stride + 1] = y[3] * 4;
00273 *new_cb = cb * 8;
00274 *new_cr = cr * 8;
00275
00276 if (old_y)
00277 old_y += 2, old_cb++, old_cr++;
00278 new_y += 2, new_cb++, new_cr++;
00279 row_index++;
00280 if (avctx->width / 2 == row_index) {
00281 row_index = 0;
00282 if (old_y) {
00283 old_y += old_y_stride * 2 - avctx->width;
00284 old_cb += old_cb_stride - avctx->width / 2;
00285 old_cr += old_cr_stride - avctx->width / 2;
00286 }
00287 new_y += new_y_stride * 2 - avctx->width;
00288 new_cb += new_cb_stride - avctx->width / 2;
00289 new_cr += new_cr_stride - avctx->width / 2;
00290 }
00291
00292 skip--;
00293 }
00294
00295 av_log(avctx, AV_LOG_DEBUG,
00296 "Escape sizes: %i, %i\n",
00297 buf_size, get_bits_count(&gb) / 8);
00298
00299 if (s->frame.data[0])
00300 avctx->release_buffer(avctx, &s->frame);
00301
00302 *(AVFrame*)data = s->frame = new_frame;
00303 *data_size = sizeof(AVFrame);
00304
00305 return buf_size;
00306 }
00307
00308
00309 AVCodec ff_escape130_decoder = {
00310 .name = "escape130",
00311 .type = AVMEDIA_TYPE_VIDEO,
00312 .id = AV_CODEC_ID_ESCAPE130,
00313 .priv_data_size = sizeof(Escape130Context),
00314 .init = escape130_decode_init,
00315 .close = escape130_decode_close,
00316 .decode = escape130_decode_frame,
00317 .capabilities = CODEC_CAP_DR1,
00318 .long_name = NULL_IF_CONFIG_SMALL("Escape 130"),
00319 };