00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "libavutil/avassert.h"
00029 #include "libavutil/crc.h"
00030 #include "libavutil/opt.h"
00031 #include "libavutil/imgutils.h"
00032 #include "libavutil/pixdesc.h"
00033 #include "libavutil/timer.h"
00034 #include "avcodec.h"
00035 #include "internal.h"
00036 #include "get_bits.h"
00037 #include "put_bits.h"
00038 #include "dsputil.h"
00039 #include "rangecoder.h"
00040 #include "golomb.h"
00041 #include "mathops.h"
00042 #include "ffv1.h"
00043
00044 static inline av_flatten int get_symbol_inline(RangeCoder *c, uint8_t *state,
00045 int is_signed)
00046 {
00047 if (get_rac(c, state + 0))
00048 return 0;
00049 else {
00050 int i, e, a;
00051 e = 0;
00052 while (get_rac(c, state + 1 + FFMIN(e, 9)))
00053 e++;
00054
00055 a = 1;
00056 for (i = e - 1; i >= 0; i--)
00057 a += a + get_rac(c, state + 22 + FFMIN(i, 9));
00058
00059 e = -(is_signed && get_rac(c, state + 11 + FFMIN(e, 10)));
00060 return (a ^ e) - e;
00061 }
00062 }
00063
00064 static av_noinline int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
00065 {
00066 return get_symbol_inline(c, state, is_signed);
00067 }
00068
00069 static inline int get_vlc_symbol(GetBitContext *gb, VlcState *const state,
00070 int bits)
00071 {
00072 int k, i, v, ret;
00073
00074 i = state->count;
00075 k = 0;
00076 while (i < state->error_sum) {
00077 k++;
00078 i += i;
00079 }
00080
00081 v = get_sr_golomb(gb, k, 12, bits);
00082 av_dlog(NULL, "v:%d bias:%d error:%d drift:%d count:%d k:%d",
00083 v, state->bias, state->error_sum, state->drift, state->count, k);
00084
00085 #if 0 // JPEG LS
00086 if (k == 0 && 2 * state->drift <= -state->count)
00087 v ^= (-1);
00088 #else
00089 v ^= ((2 * state->drift + state->count) >> 31);
00090 #endif
00091
00092 ret = fold(v + state->bias, bits);
00093
00094 update_vlc_state(state, v);
00095
00096 return ret;
00097 }
00098
00099 static av_always_inline void decode_line(FFV1Context *s, int w,
00100 int16_t *sample[2],
00101 int plane_index, int bits)
00102 {
00103 PlaneContext *const p = &s->plane[plane_index];
00104 RangeCoder *const c = &s->c;
00105 int x;
00106 int run_count = 0;
00107 int run_mode = 0;
00108 int run_index = s->run_index;
00109
00110 for (x = 0; x < w; x++) {
00111 int diff, context, sign;
00112
00113 context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
00114 if (context < 0) {
00115 context = -context;
00116 sign = 1;
00117 } else
00118 sign = 0;
00119
00120 av_assert2(context < p->context_count);
00121
00122 if (s->ac) {
00123 diff = get_symbol_inline(c, p->state[context], 1);
00124 } else {
00125 if (context == 0 && run_mode == 0)
00126 run_mode = 1;
00127
00128 if (run_mode) {
00129 if (run_count == 0 && run_mode == 1) {
00130 if (get_bits1(&s->gb)) {
00131 run_count = 1 << ff_log2_run[run_index];
00132 if (x + run_count <= w)
00133 run_index++;
00134 } else {
00135 if (ff_log2_run[run_index])
00136 run_count = get_bits(&s->gb, ff_log2_run[run_index]);
00137 else
00138 run_count = 0;
00139 if (run_index)
00140 run_index--;
00141 run_mode = 2;
00142 }
00143 }
00144 run_count--;
00145 if (run_count < 0) {
00146 run_mode = 0;
00147 run_count = 0;
00148 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
00149 bits);
00150 if (diff >= 0)
00151 diff++;
00152 } else
00153 diff = 0;
00154 } else
00155 diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
00156
00157 av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
00158 run_count, run_index, run_mode, x, get_bits_count(&s->gb));
00159 }
00160
00161 if (sign)
00162 diff = -diff;
00163
00164 sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
00165 ((1 << bits) - 1);
00166 }
00167 s->run_index = run_index;
00168 }
00169
00170 static void decode_plane(FFV1Context *s, uint8_t *src,
00171 int w, int h, int stride, int plane_index)
00172 {
00173 int x, y;
00174 int16_t *sample[2];
00175 sample[0] = s->sample_buffer + 3;
00176 sample[1] = s->sample_buffer + w + 6 + 3;
00177
00178 s->run_index = 0;
00179
00180 memset(s->sample_buffer, 0, 2 * (w + 6) * sizeof(*s->sample_buffer));
00181
00182 for (y = 0; y < h; y++) {
00183 int16_t *temp = sample[0];
00184
00185 sample[0] = sample[1];
00186 sample[1] = temp;
00187
00188 sample[1][-1] = sample[0][0];
00189 sample[0][w] = sample[0][w - 1];
00190
00191
00192 if (s->avctx->bits_per_raw_sample <= 8) {
00193 decode_line(s, w, sample, plane_index, 8);
00194 for (x = 0; x < w; x++)
00195 src[x + stride * y] = sample[1][x];
00196 } else {
00197 decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);
00198 if (s->packed_at_lsb) {
00199 for (x = 0; x < w; x++) {
00200 ((uint16_t*)(src + stride*y))[x] = sample[1][x];
00201 }
00202 } else {
00203 for (x = 0; x < w; x++) {
00204 ((uint16_t*)(src + stride*y))[x] = sample[1][x] << (16 - s->avctx->bits_per_raw_sample);
00205 }
00206 }
00207 }
00208
00209 }
00210 }
00211
00212 static void decode_rgb_frame(FFV1Context *s, uint8_t *src[3], int w, int h, int stride[3])
00213 {
00214 int x, y, p;
00215 int16_t *sample[4][2];
00216 int lbd = s->avctx->bits_per_raw_sample <= 8;
00217 int bits = s->avctx->bits_per_raw_sample > 0 ? s->avctx->bits_per_raw_sample : 8;
00218 int offset = 1 << bits;
00219
00220 for (x = 0; x < 4; x++) {
00221 sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;
00222 sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;
00223 }
00224
00225 s->run_index = 0;
00226
00227 memset(s->sample_buffer, 0, 8 * (w + 6) * sizeof(*s->sample_buffer));
00228
00229 for (y = 0; y < h; y++) {
00230 for (p = 0; p < 3 + s->transparency; p++) {
00231 int16_t *temp = sample[p][0];
00232
00233 sample[p][0] = sample[p][1];
00234 sample[p][1] = temp;
00235
00236 sample[p][1][-1]= sample[p][0][0 ];
00237 sample[p][0][ w]= sample[p][0][w-1];
00238 if (lbd)
00239 decode_line(s, w, sample[p], (p + 1)/2, 9);
00240 else
00241 decode_line(s, w, sample[p], (p + 1)/2, bits + 1);
00242 }
00243 for (x = 0; x < w; x++) {
00244 int g = sample[0][1][x];
00245 int b = sample[1][1][x];
00246 int r = sample[2][1][x];
00247 int a = sample[3][1][x];
00248
00249 b -= offset;
00250 r -= offset;
00251 g -= (b + r) >> 2;
00252 b += g;
00253 r += g;
00254
00255 if (lbd)
00256 *((uint32_t*)(src[0] + x*4 + stride[0]*y)) = b + (g<<8) + (r<<16) + (a<<24);
00257 else {
00258 *((uint16_t*)(src[0] + x*2 + stride[0]*y)) = b;
00259 *((uint16_t*)(src[1] + x*2 + stride[1]*y)) = g;
00260 *((uint16_t*)(src[2] + x*2 + stride[2]*y)) = r;
00261 }
00262 }
00263 }
00264 }
00265
00266 static int decode_slice_header(FFV1Context *f, FFV1Context *fs)
00267 {
00268 RangeCoder *c = &fs->c;
00269 uint8_t state[CONTEXT_SIZE];
00270 unsigned ps, i, context_count;
00271 memset(state, 128, sizeof(state));
00272
00273 av_assert0(f->version > 2);
00274
00275 fs->slice_x = get_symbol(c, state, 0) * f->width ;
00276 fs->slice_y = get_symbol(c, state, 0) * f->height;
00277 fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
00278 fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
00279
00280 fs->slice_x /= f->num_h_slices;
00281 fs->slice_y /= f->num_v_slices;
00282 fs->slice_width = fs->slice_width /f->num_h_slices - fs->slice_x;
00283 fs->slice_height = fs->slice_height/f->num_v_slices - fs->slice_y;
00284 if ((unsigned)fs->slice_width > f->width || (unsigned)fs->slice_height > f->height)
00285 return -1;
00286 if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
00287 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
00288 return -1;
00289
00290 for (i = 0; i < f->plane_count; i++) {
00291 PlaneContext * const p = &fs->plane[i];
00292 int idx = get_symbol(c, state, 0);
00293 if (idx > (unsigned)f->quant_table_count) {
00294 av_log(f->avctx, AV_LOG_ERROR, "quant_table_index out of range\n");
00295 return -1;
00296 }
00297 p->quant_table_index = idx;
00298 memcpy(p->quant_table, f->quant_tables[idx], sizeof(p->quant_table));
00299 context_count = f->context_count[idx];
00300
00301 if (p->context_count < context_count) {
00302 av_freep(&p->state);
00303 av_freep(&p->vlc_state);
00304 }
00305 p->context_count = context_count;
00306 }
00307
00308 ps = get_symbol(c, state, 0);
00309 if (ps == 1) {
00310 f->picture.interlaced_frame = 1;
00311 f->picture.top_field_first = 1;
00312 } else if (ps == 2) {
00313 f->picture.interlaced_frame = 1;
00314 f->picture.top_field_first = 0;
00315 } else if (ps == 3) {
00316 f->picture.interlaced_frame = 0;
00317 }
00318 f->picture.sample_aspect_ratio.num = get_symbol(c, state, 0);
00319 f->picture.sample_aspect_ratio.den = get_symbol(c, state, 0);
00320
00321 return 0;
00322 }
00323
00324 static int decode_slice(AVCodecContext *c, void *arg)
00325 {
00326 FFV1Context *fs = *(void **)arg;
00327 FFV1Context *f = fs->avctx->priv_data;
00328 int width, height, x, y, ret;
00329 const int ps = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step_minus1 + 1;
00330 AVFrame * const p = &f->picture;
00331
00332 if (f->version > 2) {
00333 if (ffv1_init_slice_state(f, fs) < 0)
00334 return AVERROR(ENOMEM);
00335 if (decode_slice_header(f, fs) < 0) {
00336 fs->slice_damaged = 1;
00337 return AVERROR_INVALIDDATA;
00338 }
00339 }
00340 if ((ret = ffv1_init_slice_state(f, fs)) < 0)
00341 return ret;
00342 if (f->picture.key_frame)
00343 ffv1_clear_slice_state(f, fs);
00344
00345 width = fs->slice_width;
00346 height = fs->slice_height;
00347 x = fs->slice_x;
00348 y = fs->slice_y;
00349
00350 if (!fs->ac) {
00351 if (f->version == 3 && f->minor_version > 1 || f->version > 3)
00352 get_rac(&fs->c, (uint8_t[]) { 129 });
00353 fs->ac_byte_count = f->version > 2 || (!x && !y) ? fs->c.bytestream - fs->c.bytestream_start - 1 : 0;
00354 init_get_bits(&fs->gb,
00355 fs->c.bytestream_start + fs->ac_byte_count,
00356 (fs->c.bytestream_end - fs->c.bytestream_start - fs->ac_byte_count) * 8);
00357 }
00358
00359 av_assert1(width && height);
00360 if (f->colorspace == 0) {
00361 const int chroma_width = -((-width) >> f->chroma_h_shift);
00362 const int chroma_height = -((-height) >> f->chroma_v_shift);
00363 const int cx = x >> f->chroma_h_shift;
00364 const int cy = y >> f->chroma_v_shift;
00365 decode_plane(fs, p->data[0] + ps*x + y*p->linesize[0], width, height, p->linesize[0], 0);
00366
00367 if (f->chroma_planes) {
00368 decode_plane(fs, p->data[1] + ps*cx+cy*p->linesize[1], chroma_width, chroma_height, p->linesize[1], 1);
00369 decode_plane(fs, p->data[2] + ps*cx+cy*p->linesize[2], chroma_width, chroma_height, p->linesize[2], 1);
00370 }
00371 if (fs->transparency)
00372 decode_plane(fs, p->data[3] + ps*x + y*p->linesize[3], width, height, p->linesize[3], 2);
00373 } else {
00374 uint8_t *planes[3] = { p->data[0] + ps * x + y * p->linesize[0],
00375 p->data[1] + ps * x + y * p->linesize[1],
00376 p->data[2] + ps * x + y * p->linesize[2] };
00377 decode_rgb_frame(fs, planes, width, height, p->linesize);
00378 }
00379 if (fs->ac && f->version > 2) {
00380 int v;
00381 get_rac(&fs->c, (uint8_t[]) { 129 });
00382 v = fs->c.bytestream_end - fs->c.bytestream - 2 - 5*f->ec;
00383 if (v) {
00384 av_log(f->avctx, AV_LOG_ERROR, "bytestream end mismatching by %d\n", v);
00385 fs->slice_damaged = 1;
00386 }
00387 }
00388
00389 emms_c();
00390
00391 return 0;
00392 }
00393
00394 static int read_quant_table(RangeCoder *c, int16_t *quant_table, int scale)
00395 {
00396 int v;
00397 int i = 0;
00398 uint8_t state[CONTEXT_SIZE];
00399
00400 memset(state, 128, sizeof(state));
00401
00402 for (v = 0; i < 128; v++) {
00403 unsigned len = get_symbol(c, state, 0) + 1;
00404
00405 if (len > 128 - i)
00406 return AVERROR_INVALIDDATA;
00407
00408 while (len--) {
00409 quant_table[i] = scale * v;
00410 i++;
00411 }
00412 }
00413
00414 for (i = 1; i < 128; i++)
00415 quant_table[256 - i] = -quant_table[i];
00416 quant_table[128] = -quant_table[127];
00417
00418 return 2 * v - 1;
00419 }
00420
00421 static int read_quant_tables(RangeCoder *c,
00422 int16_t quant_table[MAX_CONTEXT_INPUTS][256])
00423 {
00424 int i;
00425 int context_count = 1;
00426
00427 for (i = 0; i < 5; i++) {
00428 context_count *= read_quant_table(c, quant_table[i], context_count);
00429 if (context_count > 32768U) {
00430 return AVERROR_INVALIDDATA;
00431 }
00432 }
00433 return (context_count + 1) / 2;
00434 }
00435
00436 static int read_extra_header(FFV1Context *f)
00437 {
00438 RangeCoder *const c = &f->c;
00439 uint8_t state[CONTEXT_SIZE];
00440 int i, j, k, ret;
00441 uint8_t state2[32][CONTEXT_SIZE];
00442
00443 memset(state2, 128, sizeof(state2));
00444 memset(state, 128, sizeof(state));
00445
00446 ff_init_range_decoder(c, f->avctx->extradata, f->avctx->extradata_size);
00447 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
00448
00449 f->version = get_symbol(c, state, 0);
00450 if (f->version > 2) {
00451 c->bytestream_end -= 4;
00452 f->minor_version = get_symbol(c, state, 0);
00453 }
00454 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
00455 if (f->ac > 1) {
00456 for (i = 1; i < 256; i++)
00457 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
00458 }
00459
00460 f->colorspace = get_symbol(c, state, 0);
00461 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
00462 f->chroma_planes = get_rac(c, state);
00463 f->chroma_h_shift = get_symbol(c, state, 0);
00464 f->chroma_v_shift = get_symbol(c, state, 0);
00465 f->transparency = get_rac(c, state);
00466 f->plane_count = 2 + f->transparency;
00467 f->num_h_slices = 1 + get_symbol(c, state, 0);
00468 f->num_v_slices = 1 + get_symbol(c, state, 0);
00469
00470 if (f->num_h_slices > (unsigned)f->width || !f->num_h_slices ||
00471 f->num_v_slices > (unsigned)f->height || !f->num_v_slices
00472 ) {
00473 av_log(f->avctx, AV_LOG_ERROR, "slice count invalid\n");
00474 return AVERROR_INVALIDDATA;
00475 }
00476
00477 f->quant_table_count = get_symbol(c, state, 0);
00478 if (f->quant_table_count > (unsigned)MAX_QUANT_TABLES)
00479 return AVERROR_INVALIDDATA;
00480
00481 for (i = 0; i < f->quant_table_count; i++) {
00482 f->context_count[i] = read_quant_tables(c, f->quant_tables[i]);
00483 if (f->context_count[i] < 0) {
00484 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
00485 return AVERROR_INVALIDDATA;
00486 }
00487 }
00488 if ((ret = ffv1_allocate_initial_states(f)) < 0)
00489 return ret;
00490
00491 for (i = 0; i < f->quant_table_count; i++)
00492 if (get_rac(c, state)) {
00493 for (j = 0; j < f->context_count[i]; j++)
00494 for (k = 0; k < CONTEXT_SIZE; k++) {
00495 int pred = j ? f->initial_states[i][j - 1][k] : 128;
00496 f->initial_states[i][j][k] =
00497 (pred + get_symbol(c, state2[k], 1)) & 0xFF;
00498 }
00499 }
00500
00501 if (f->version > 2) {
00502 f->ec = get_symbol(c, state, 0);
00503 }
00504
00505 if (f->version > 2) {
00506 unsigned v;
00507 v = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0,
00508 f->avctx->extradata, f->avctx->extradata_size);
00509 if (v) {
00510 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!\n", v);
00511 return AVERROR_INVALIDDATA;
00512 }
00513 }
00514
00515 return 0;
00516 }
00517
00518 static int read_header(FFV1Context *f)
00519 {
00520 uint8_t state[CONTEXT_SIZE];
00521 int i, j, context_count = -1;
00522 RangeCoder *const c = &f->slice_context[0]->c;
00523
00524 memset(state, 128, sizeof(state));
00525
00526 if (f->version < 2) {
00527 unsigned v= get_symbol(c, state, 0);
00528 if (v >= 2) {
00529 av_log(f->avctx, AV_LOG_ERROR, "invalid version %d in ver01 header\n", v);
00530 return AVERROR_INVALIDDATA;
00531 }
00532 f->version = v;
00533 f->ac = f->avctx->coder_type = get_symbol(c, state, 0);
00534 if (f->ac > 1) {
00535 for (i = 1; i < 256; i++)
00536 f->state_transition[i] = get_symbol(c, state, 1) + c->one_state[i];
00537 }
00538
00539 f->colorspace = get_symbol(c, state, 0);
00540
00541 if (f->version > 0)
00542 f->avctx->bits_per_raw_sample = get_symbol(c, state, 0);
00543
00544 f->chroma_planes = get_rac(c, state);
00545 f->chroma_h_shift = get_symbol(c, state, 0);
00546 f->chroma_v_shift = get_symbol(c, state, 0);
00547 f->transparency = get_rac(c, state);
00548 f->plane_count = 2 + f->transparency;
00549 }
00550
00551 if (f->colorspace == 0) {
00552 if (!f->transparency && !f->chroma_planes) {
00553 if (f->avctx->bits_per_raw_sample <= 8)
00554 f->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
00555 else
00556 f->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
00557 } else if (f->avctx->bits_per_raw_sample<=8 && !f->transparency) {
00558 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
00559 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P; break;
00560 case 0x01: f->avctx->pix_fmt = AV_PIX_FMT_YUV440P; break;
00561 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P; break;
00562 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P; break;
00563 case 0x20: f->avctx->pix_fmt = AV_PIX_FMT_YUV411P; break;
00564 case 0x22: f->avctx->pix_fmt = AV_PIX_FMT_YUV410P; break;
00565 default:
00566 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
00567 return AVERROR(ENOSYS);
00568 }
00569 } else if (f->avctx->bits_per_raw_sample <= 8 && f->transparency) {
00570 switch(16*f->chroma_h_shift + f->chroma_v_shift) {
00571 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUVA444P; break;
00572 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUVA422P; break;
00573 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUVA420P; break;
00574 default:
00575 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
00576 return AVERROR(ENOSYS);
00577 }
00578 } else if (f->avctx->bits_per_raw_sample == 9) {
00579 f->packed_at_lsb = 1;
00580 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
00581 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P9; break;
00582 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P9; break;
00583 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P9; break;
00584 default:
00585 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
00586 return AVERROR(ENOSYS);
00587 }
00588 } else if (f->avctx->bits_per_raw_sample == 10) {
00589 f->packed_at_lsb = 1;
00590 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
00591 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; break;
00592 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; break;
00593 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P10; break;
00594 default:
00595 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
00596 return AVERROR(ENOSYS);
00597 }
00598 } else {
00599 switch(16 * f->chroma_h_shift + f->chroma_v_shift) {
00600 case 0x00: f->avctx->pix_fmt = AV_PIX_FMT_YUV444P16; break;
00601 case 0x10: f->avctx->pix_fmt = AV_PIX_FMT_YUV422P16; break;
00602 case 0x11: f->avctx->pix_fmt = AV_PIX_FMT_YUV420P16; break;
00603 default:
00604 av_log(f->avctx, AV_LOG_ERROR, "format not supported\n");
00605 return AVERROR(ENOSYS);
00606 }
00607 }
00608 } else if (f->colorspace == 1) {
00609 if (f->chroma_h_shift || f->chroma_v_shift) {
00610 av_log(f->avctx, AV_LOG_ERROR,
00611 "chroma subsampling not supported in this colorspace\n");
00612 return AVERROR(ENOSYS);
00613 }
00614 if ( f->avctx->bits_per_raw_sample == 9)
00615 f->avctx->pix_fmt = AV_PIX_FMT_GBRP9;
00616 else if (f->avctx->bits_per_raw_sample == 10)
00617 f->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
00618 else if (f->avctx->bits_per_raw_sample == 12)
00619 f->avctx->pix_fmt = AV_PIX_FMT_GBRP12;
00620 else if (f->avctx->bits_per_raw_sample == 14)
00621 f->avctx->pix_fmt = AV_PIX_FMT_GBRP14;
00622 else
00623 if (f->transparency) f->avctx->pix_fmt = AV_PIX_FMT_RGB32;
00624 else f->avctx->pix_fmt = AV_PIX_FMT_0RGB32;
00625 } else {
00626 av_log(f->avctx, AV_LOG_ERROR, "colorspace not supported\n");
00627 return AVERROR(ENOSYS);
00628 }
00629
00630 av_dlog(f->avctx, "%d %d %d\n",
00631 f->chroma_h_shift, f->chroma_v_shift, f->avctx->pix_fmt);
00632 if (f->version < 2) {
00633 context_count = read_quant_tables(c, f->quant_table);
00634 if (context_count < 0) {
00635 av_log(f->avctx, AV_LOG_ERROR, "read_quant_table error\n");
00636 return AVERROR_INVALIDDATA;
00637 }
00638 } else if (f->version < 3) {
00639 f->slice_count = get_symbol(c, state, 0);
00640 } else {
00641 const uint8_t *p = c->bytestream_end;
00642 for (f->slice_count = 0;
00643 f->slice_count < MAX_SLICES && 3 < p - c->bytestream_start;
00644 f->slice_count++) {
00645 int trailer = 3 + 5*!!f->ec;
00646 int size = AV_RB24(p-trailer);
00647 if (size + trailer > p - c->bytestream_start)
00648 break;
00649 p -= size + trailer;
00650 }
00651 }
00652 if (f->slice_count > (unsigned)MAX_SLICES || f->slice_count <= 0) {
00653 av_log(f->avctx, AV_LOG_ERROR, "slice count %d is invalid\n", f->slice_count);
00654 return AVERROR_INVALIDDATA;
00655 }
00656
00657 for (j = 0; j < f->slice_count; j++) {
00658 FFV1Context *fs = f->slice_context[j];
00659 fs->ac = f->ac;
00660 fs->packed_at_lsb = f->packed_at_lsb;
00661
00662 fs->slice_damaged = 0;
00663
00664 if (f->version == 2) {
00665 fs->slice_x = get_symbol(c, state, 0) * f->width ;
00666 fs->slice_y = get_symbol(c, state, 0) * f->height;
00667 fs->slice_width = (get_symbol(c, state, 0) + 1) * f->width + fs->slice_x;
00668 fs->slice_height = (get_symbol(c, state, 0) + 1) * f->height + fs->slice_y;
00669
00670 fs->slice_x /= f->num_h_slices;
00671 fs->slice_y /= f->num_v_slices;
00672 fs->slice_width = fs->slice_width / f->num_h_slices - fs->slice_x;
00673 fs->slice_height = fs->slice_height / f->num_v_slices - fs->slice_y;
00674 if ((unsigned)fs->slice_width > f->width ||
00675 (unsigned)fs->slice_height > f->height)
00676 return AVERROR_INVALIDDATA;
00677 if ( (unsigned)fs->slice_x + (uint64_t)fs->slice_width > f->width
00678 || (unsigned)fs->slice_y + (uint64_t)fs->slice_height > f->height)
00679 return AVERROR_INVALIDDATA;
00680 }
00681
00682 for (i = 0; i < f->plane_count; i++) {
00683 PlaneContext *const p = &fs->plane[i];
00684
00685 if (f->version == 2) {
00686 int idx = get_symbol(c, state, 0);
00687 if (idx > (unsigned)f->quant_table_count) {
00688 av_log(f->avctx, AV_LOG_ERROR,
00689 "quant_table_index out of range\n");
00690 return AVERROR_INVALIDDATA;
00691 }
00692 p->quant_table_index = idx;
00693 memcpy(p->quant_table, f->quant_tables[idx],
00694 sizeof(p->quant_table));
00695 context_count = f->context_count[idx];
00696 } else {
00697 memcpy(p->quant_table, f->quant_table, sizeof(p->quant_table));
00698 }
00699
00700 if (f->version <= 2) {
00701 av_assert0(context_count >= 0);
00702 if (p->context_count < context_count) {
00703 av_freep(&p->state);
00704 av_freep(&p->vlc_state);
00705 }
00706 p->context_count = context_count;
00707 }
00708 }
00709 }
00710 return 0;
00711 }
00712
00713 static av_cold int decode_init(AVCodecContext *avctx)
00714 {
00715 FFV1Context *f = avctx->priv_data;
00716 int ret;
00717
00718 if ((ret = ffv1_common_init(avctx)) < 0)
00719 return ret;
00720
00721 if (avctx->extradata && (ret = read_extra_header(f)) < 0)
00722 return ret;
00723
00724 if ((ret = ffv1_init_slice_contexts(f)) < 0)
00725 return ret;
00726
00727 return 0;
00728 }
00729
00730 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
00731 {
00732 const uint8_t *buf = avpkt->data;
00733 int buf_size = avpkt->size;
00734 FFV1Context *f = avctx->priv_data;
00735 RangeCoder *const c = &f->slice_context[0]->c;
00736 AVFrame *const p = &f->picture;
00737 int i, ret;
00738 uint8_t keystate = 128;
00739 const uint8_t *buf_p;
00740
00741 AVFrame *picture = data;
00742
00743
00744 if (p->data[0])
00745 avctx->release_buffer(avctx, p);
00746
00747 ff_init_range_decoder(c, buf, buf_size);
00748 ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
00749
00750 p->pict_type = AV_PICTURE_TYPE_I;
00751 if (get_rac(c, &keystate)) {
00752 p->key_frame = 1;
00753 f->key_frame_ok = 0;
00754 if ((ret = read_header(f)) < 0)
00755 return ret;
00756 f->key_frame_ok = 1;
00757 } else {
00758 if (!f->key_frame_ok) {
00759 av_log(avctx, AV_LOG_ERROR,
00760 "Cant decode non keyframe without valid keyframe\n");
00761 return AVERROR_INVALIDDATA;
00762 }
00763 p->key_frame = 0;
00764 }
00765
00766 p->reference = 3;
00767 if ((ret = ff_get_buffer(avctx, p)) < 0) {
00768 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00769 return ret;
00770 }
00771
00772 if (avctx->debug & FF_DEBUG_PICT_INFO)
00773 av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
00774 f->version, p->key_frame, f->ac, f->ec, f->slice_count, f->avctx->bits_per_raw_sample);
00775
00776 buf_p = buf + buf_size;
00777 for (i = f->slice_count - 1; i >= 0; i--) {
00778 FFV1Context *fs = f->slice_context[i];
00779 int trailer = 3 + 5*!!f->ec;
00780 int v;
00781
00782 if (i || f->version > 2) v = AV_RB24(buf_p-trailer) + trailer;
00783 else v = buf_p - c->bytestream_start;
00784 if (buf_p - c->bytestream_start < v) {
00785 av_log(avctx, AV_LOG_ERROR, "Slice pointer chain broken\n");
00786 return AVERROR_INVALIDDATA;
00787 }
00788 buf_p -= v;
00789
00790 if (f->ec) {
00791 unsigned crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), 0, buf_p, v);
00792 if (crc) {
00793 int64_t ts = avpkt->pts != AV_NOPTS_VALUE ? avpkt->pts : avpkt->dts;
00794 av_log(f->avctx, AV_LOG_ERROR, "CRC mismatch %X!", crc);
00795 if (ts != AV_NOPTS_VALUE && avctx->pkt_timebase.num) {
00796 av_log(f->avctx, AV_LOG_ERROR, "at %f seconds\n", ts*av_q2d(avctx->pkt_timebase));
00797 } else if (ts != AV_NOPTS_VALUE) {
00798 av_log(f->avctx, AV_LOG_ERROR, "at %"PRId64"\n", ts);
00799 } else {
00800 av_log(f->avctx, AV_LOG_ERROR, "\n");
00801 }
00802 fs->slice_damaged = 1;
00803 }
00804 }
00805
00806 if (i) {
00807 ff_init_range_decoder(&fs->c, buf_p, v);
00808 } else
00809 fs->c.bytestream_end = (uint8_t *)(buf_p + v);
00810 }
00811
00812 avctx->execute(avctx,
00813 decode_slice,
00814 &f->slice_context[0],
00815 NULL,
00816 f->slice_count,
00817 sizeof(void*));
00818
00819 for (i = f->slice_count - 1; i >= 0; i--) {
00820 FFV1Context *fs = f->slice_context[i];
00821 int j;
00822 if (fs->slice_damaged && f->last_picture.data[0]) {
00823 const uint8_t *src[4];
00824 uint8_t *dst[4];
00825 for (j = 0; j < 4; j++) {
00826 int sh = (j==1 || j==2) ? f->chroma_h_shift : 0;
00827 int sv = (j==1 || j==2) ? f->chroma_v_shift : 0;
00828 dst[j] = f->picture .data[j] + f->picture .linesize[j]*
00829 (fs->slice_y>>sv) + (fs->slice_x>>sh);
00830 src[j] = f->last_picture.data[j] + f->last_picture.linesize[j]*
00831 (fs->slice_y>>sv) + (fs->slice_x>>sh);
00832 }
00833 av_image_copy(dst,
00834 f->picture.linesize,
00835 (const uint8_t **)src,
00836 f->last_picture.linesize,
00837 avctx->pix_fmt,
00838 fs->slice_width,
00839 fs->slice_height);
00840 }
00841 }
00842
00843 f->picture_number++;
00844
00845 *picture = *p;
00846 *got_frame = 1;
00847
00848 FFSWAP(AVFrame, f->picture, f->last_picture);
00849
00850 return buf_size;
00851 }
00852
00853 AVCodec ff_ffv1_decoder = {
00854 .name = "ffv1",
00855 .type = AVMEDIA_TYPE_VIDEO,
00856 .id = AV_CODEC_ID_FFV1,
00857 .priv_data_size = sizeof(FFV1Context),
00858 .init = decode_init,
00859 .close = ffv1_close,
00860 .decode = decode_frame,
00861 .capabilities = CODEC_CAP_DR1 |
00862 CODEC_CAP_SLICE_THREADS,
00863 .long_name = NULL_IF_CONFIG_SMALL("FFmpeg video codec #1"),
00864 };