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