00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "libavutil/imgutils.h"
00026 #include "avcodec.h"
00027 #include "internal.h"
00028 #include "vp8.h"
00029 #include "vp8data.h"
00030 #include "rectangle.h"
00031 #include "thread.h"
00032
00033 #if ARCH_ARM
00034 # include "arm/vp8.h"
00035 #endif
00036
00037 static void free_buffers(VP8Context *s)
00038 {
00039 av_freep(&s->macroblocks_base);
00040 av_freep(&s->filter_strength);
00041 av_freep(&s->intra4x4_pred_mode_top);
00042 av_freep(&s->top_nnz);
00043 av_freep(&s->edge_emu_buffer);
00044 av_freep(&s->top_border);
00045
00046 s->macroblocks = NULL;
00047 }
00048
00049 static int vp8_alloc_frame(VP8Context *s, AVFrame *f)
00050 {
00051 int ret;
00052 if ((ret = ff_thread_get_buffer(s->avctx, f)) < 0)
00053 return ret;
00054 if (s->num_maps_to_be_freed && !s->maps_are_invalid) {
00055 f->ref_index[0] = s->segmentation_maps[--s->num_maps_to_be_freed];
00056 } else if (!(f->ref_index[0] = av_mallocz(s->mb_width * s->mb_height))) {
00057 ff_thread_release_buffer(s->avctx, f);
00058 return AVERROR(ENOMEM);
00059 }
00060 return 0;
00061 }
00062
00063 static void vp8_release_frame(VP8Context *s, AVFrame *f, int prefer_delayed_free, int can_direct_free)
00064 {
00065 if (f->ref_index[0]) {
00066 if (prefer_delayed_free) {
00067
00068
00069
00070
00071 int max_queued_maps = can_direct_free ? 1 : FF_ARRAY_ELEMS(s->segmentation_maps);
00072 if (s->num_maps_to_be_freed < max_queued_maps) {
00073 s->segmentation_maps[s->num_maps_to_be_freed++] = f->ref_index[0];
00074 } else if (can_direct_free) {
00075 av_free(f->ref_index[0]);
00076 }
00077 f->ref_index[0] = NULL;
00078 } else {
00079 av_free(f->ref_index[0]);
00080 }
00081 }
00082 ff_thread_release_buffer(s->avctx, f);
00083 }
00084
00085 static void vp8_decode_flush_impl(AVCodecContext *avctx,
00086 int prefer_delayed_free, int can_direct_free, int free_mem)
00087 {
00088 VP8Context *s = avctx->priv_data;
00089 int i;
00090
00091 if (!avctx->internal->is_copy) {
00092 for (i = 0; i < 5; i++)
00093 if (s->frames[i].data[0])
00094 vp8_release_frame(s, &s->frames[i], prefer_delayed_free, can_direct_free);
00095 }
00096 memset(s->framep, 0, sizeof(s->framep));
00097
00098 if (free_mem) {
00099 free_buffers(s);
00100 s->maps_are_invalid = 1;
00101 }
00102 }
00103
00104 static void vp8_decode_flush(AVCodecContext *avctx)
00105 {
00106 vp8_decode_flush_impl(avctx, 1, 1, 0);
00107 }
00108
00109 static int update_dimensions(VP8Context *s, int width, int height)
00110 {
00111 if (width != s->avctx->width || ((width+15)/16 != s->mb_width || (height+15)/16 != s->mb_height) && s->macroblocks_base ||
00112 height != s->avctx->height) {
00113 if (av_image_check_size(width, height, 0, s->avctx))
00114 return AVERROR_INVALIDDATA;
00115
00116 vp8_decode_flush_impl(s->avctx, 1, 0, 1);
00117
00118 avcodec_set_dimensions(s->avctx, width, height);
00119 }
00120
00121 s->mb_width = (s->avctx->coded_width +15) / 16;
00122 s->mb_height = (s->avctx->coded_height+15) / 16;
00123
00124 s->macroblocks_base = av_mallocz((s->mb_width+s->mb_height*2+1)*sizeof(*s->macroblocks));
00125 s->filter_strength = av_mallocz(s->mb_width*sizeof(*s->filter_strength));
00126 s->intra4x4_pred_mode_top = av_mallocz(s->mb_width*4);
00127 s->top_nnz = av_mallocz(s->mb_width*sizeof(*s->top_nnz));
00128 s->top_border = av_mallocz((s->mb_width+1)*sizeof(*s->top_border));
00129
00130 if (!s->macroblocks_base || !s->filter_strength || !s->intra4x4_pred_mode_top ||
00131 !s->top_nnz || !s->top_border)
00132 return AVERROR(ENOMEM);
00133
00134 s->macroblocks = s->macroblocks_base + 1;
00135
00136 return 0;
00137 }
00138
00139 static void parse_segment_info(VP8Context *s)
00140 {
00141 VP56RangeCoder *c = &s->c;
00142 int i;
00143
00144 s->segmentation.update_map = vp8_rac_get(c);
00145
00146 if (vp8_rac_get(c)) {
00147 s->segmentation.absolute_vals = vp8_rac_get(c);
00148
00149 for (i = 0; i < 4; i++)
00150 s->segmentation.base_quant[i] = vp8_rac_get_sint(c, 7);
00151
00152 for (i = 0; i < 4; i++)
00153 s->segmentation.filter_level[i] = vp8_rac_get_sint(c, 6);
00154 }
00155 if (s->segmentation.update_map)
00156 for (i = 0; i < 3; i++)
00157 s->prob->segmentid[i] = vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
00158 }
00159
00160 static void update_lf_deltas(VP8Context *s)
00161 {
00162 VP56RangeCoder *c = &s->c;
00163 int i;
00164
00165 for (i = 0; i < 4; i++) {
00166 if (vp8_rac_get(c)) {
00167 s->lf_delta.ref[i] = vp8_rac_get_uint(c, 6);
00168
00169 if (vp8_rac_get(c))
00170 s->lf_delta.ref[i] = -s->lf_delta.ref[i];
00171 }
00172 }
00173
00174 for (i = MODE_I4x4; i <= VP8_MVMODE_SPLIT; i++) {
00175 if (vp8_rac_get(c)) {
00176 s->lf_delta.mode[i] = vp8_rac_get_uint(c, 6);
00177
00178 if (vp8_rac_get(c))
00179 s->lf_delta.mode[i] = -s->lf_delta.mode[i];
00180 }
00181 }
00182 }
00183
00184 static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
00185 {
00186 const uint8_t *sizes = buf;
00187 int i;
00188
00189 s->num_coeff_partitions = 1 << vp8_rac_get_uint(&s->c, 2);
00190
00191 buf += 3*(s->num_coeff_partitions-1);
00192 buf_size -= 3*(s->num_coeff_partitions-1);
00193 if (buf_size < 0)
00194 return -1;
00195
00196 for (i = 0; i < s->num_coeff_partitions-1; i++) {
00197 int size = AV_RL24(sizes + 3*i);
00198 if (buf_size - size < 0)
00199 return -1;
00200
00201 ff_vp56_init_range_decoder(&s->coeff_partition[i], buf, size);
00202 buf += size;
00203 buf_size -= size;
00204 }
00205 ff_vp56_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
00206
00207 return 0;
00208 }
00209
00210 static void get_quants(VP8Context *s)
00211 {
00212 VP56RangeCoder *c = &s->c;
00213 int i, base_qi;
00214
00215 int yac_qi = vp8_rac_get_uint(c, 7);
00216 int ydc_delta = vp8_rac_get_sint(c, 4);
00217 int y2dc_delta = vp8_rac_get_sint(c, 4);
00218 int y2ac_delta = vp8_rac_get_sint(c, 4);
00219 int uvdc_delta = vp8_rac_get_sint(c, 4);
00220 int uvac_delta = vp8_rac_get_sint(c, 4);
00221
00222 for (i = 0; i < 4; i++) {
00223 if (s->segmentation.enabled) {
00224 base_qi = s->segmentation.base_quant[i];
00225 if (!s->segmentation.absolute_vals)
00226 base_qi += yac_qi;
00227 } else
00228 base_qi = yac_qi;
00229
00230 s->qmat[i].luma_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + ydc_delta , 7)];
00231 s->qmat[i].luma_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi , 7)];
00232 s->qmat[i].luma_dc_qmul[0] = 2 * vp8_dc_qlookup[av_clip_uintp2(base_qi + y2dc_delta, 7)];
00233 s->qmat[i].luma_dc_qmul[1] = 155 * vp8_ac_qlookup[av_clip_uintp2(base_qi + y2ac_delta, 7)] / 100;
00234 s->qmat[i].chroma_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + uvdc_delta, 7)];
00235 s->qmat[i].chroma_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi + uvac_delta, 7)];
00236
00237 s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
00238 s->qmat[i].chroma_qmul[0] = FFMIN(s->qmat[i].chroma_qmul[0], 132);
00239 }
00240 }
00241
00255 static VP56Frame ref_to_update(VP8Context *s, int update, VP56Frame ref)
00256 {
00257 VP56RangeCoder *c = &s->c;
00258
00259 if (update)
00260 return VP56_FRAME_CURRENT;
00261
00262 switch (vp8_rac_get_uint(c, 2)) {
00263 case 1:
00264 return VP56_FRAME_PREVIOUS;
00265 case 2:
00266 return (ref == VP56_FRAME_GOLDEN) ? VP56_FRAME_GOLDEN2 : VP56_FRAME_GOLDEN;
00267 }
00268 return VP56_FRAME_NONE;
00269 }
00270
00271 static void update_refs(VP8Context *s)
00272 {
00273 VP56RangeCoder *c = &s->c;
00274
00275 int update_golden = vp8_rac_get(c);
00276 int update_altref = vp8_rac_get(c);
00277
00278 s->update_golden = ref_to_update(s, update_golden, VP56_FRAME_GOLDEN);
00279 s->update_altref = ref_to_update(s, update_altref, VP56_FRAME_GOLDEN2);
00280 }
00281
00282 static int decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
00283 {
00284 VP56RangeCoder *c = &s->c;
00285 int header_size, hscale, vscale, i, j, k, l, m, ret;
00286 int width = s->avctx->width;
00287 int height = s->avctx->height;
00288
00289 s->keyframe = !(buf[0] & 1);
00290 s->profile = (buf[0]>>1) & 7;
00291 s->invisible = !(buf[0] & 0x10);
00292 header_size = AV_RL24(buf) >> 5;
00293 buf += 3;
00294 buf_size -= 3;
00295
00296 if (s->profile > 3)
00297 av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
00298
00299 if (!s->profile)
00300 memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
00301 else
00302 memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab, sizeof(s->put_pixels_tab));
00303
00304 if (header_size > buf_size - 7*s->keyframe) {
00305 av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
00306 return AVERROR_INVALIDDATA;
00307 }
00308
00309 if (s->keyframe) {
00310 if (AV_RL24(buf) != 0x2a019d) {
00311 av_log(s->avctx, AV_LOG_ERROR, "Invalid start code 0x%x\n", AV_RL24(buf));
00312 return AVERROR_INVALIDDATA;
00313 }
00314 width = AV_RL16(buf+3) & 0x3fff;
00315 height = AV_RL16(buf+5) & 0x3fff;
00316 hscale = buf[4] >> 6;
00317 vscale = buf[6] >> 6;
00318 buf += 7;
00319 buf_size -= 7;
00320
00321 if (hscale || vscale)
00322 av_log_missing_feature(s->avctx, "Upscaling", 1);
00323
00324 s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
00325 for (i = 0; i < 4; i++)
00326 for (j = 0; j < 16; j++)
00327 memcpy(s->prob->token[i][j], vp8_token_default_probs[i][vp8_coeff_band[j]],
00328 sizeof(s->prob->token[i][j]));
00329 memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter, sizeof(s->prob->pred16x16));
00330 memcpy(s->prob->pred8x8c , vp8_pred8x8c_prob_inter , sizeof(s->prob->pred8x8c));
00331 memcpy(s->prob->mvc , vp8_mv_default_prob , sizeof(s->prob->mvc));
00332 memset(&s->segmentation, 0, sizeof(s->segmentation));
00333 }
00334
00335 if (!s->macroblocks_base ||
00336 width != s->avctx->width || height != s->avctx->height || (width+15)/16 != s->mb_width || (height+15)/16 != s->mb_height) {
00337 if ((ret = update_dimensions(s, width, height)) < 0)
00338 return ret;
00339 }
00340
00341 ff_vp56_init_range_decoder(c, buf, header_size);
00342 buf += header_size;
00343 buf_size -= header_size;
00344
00345 if (s->keyframe) {
00346 if (vp8_rac_get(c))
00347 av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
00348 vp8_rac_get(c);
00349 }
00350
00351 if ((s->segmentation.enabled = vp8_rac_get(c)))
00352 parse_segment_info(s);
00353 else
00354 s->segmentation.update_map = 0;
00355
00356 s->filter.simple = vp8_rac_get(c);
00357 s->filter.level = vp8_rac_get_uint(c, 6);
00358 s->filter.sharpness = vp8_rac_get_uint(c, 3);
00359
00360 if ((s->lf_delta.enabled = vp8_rac_get(c)))
00361 if (vp8_rac_get(c))
00362 update_lf_deltas(s);
00363
00364 if (setup_partitions(s, buf, buf_size)) {
00365 av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
00366 return AVERROR_INVALIDDATA;
00367 }
00368
00369 get_quants(s);
00370
00371 if (!s->keyframe) {
00372 update_refs(s);
00373 s->sign_bias[VP56_FRAME_GOLDEN] = vp8_rac_get(c);
00374 s->sign_bias[VP56_FRAME_GOLDEN2 ] = vp8_rac_get(c);
00375 }
00376
00377
00378
00379 if (!(s->update_probabilities = vp8_rac_get(c)))
00380 s->prob[1] = s->prob[0];
00381
00382 s->update_last = s->keyframe || vp8_rac_get(c);
00383
00384 for (i = 0; i < 4; i++)
00385 for (j = 0; j < 8; j++)
00386 for (k = 0; k < 3; k++)
00387 for (l = 0; l < NUM_DCT_TOKENS-1; l++)
00388 if (vp56_rac_get_prob_branchy(c, vp8_token_update_probs[i][j][k][l])) {
00389 int prob = vp8_rac_get_uint(c, 8);
00390 for (m = 0; vp8_coeff_band_indexes[j][m] >= 0; m++)
00391 s->prob->token[i][vp8_coeff_band_indexes[j][m]][k][l] = prob;
00392 }
00393
00394 if ((s->mbskip_enabled = vp8_rac_get(c)))
00395 s->prob->mbskip = vp8_rac_get_uint(c, 8);
00396
00397 if (!s->keyframe) {
00398 s->prob->intra = vp8_rac_get_uint(c, 8);
00399 s->prob->last = vp8_rac_get_uint(c, 8);
00400 s->prob->golden = vp8_rac_get_uint(c, 8);
00401
00402 if (vp8_rac_get(c))
00403 for (i = 0; i < 4; i++)
00404 s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
00405 if (vp8_rac_get(c))
00406 for (i = 0; i < 3; i++)
00407 s->prob->pred8x8c[i] = vp8_rac_get_uint(c, 8);
00408
00409
00410 for (i = 0; i < 2; i++)
00411 for (j = 0; j < 19; j++)
00412 if (vp56_rac_get_prob_branchy(c, vp8_mv_update_prob[i][j]))
00413 s->prob->mvc[i][j] = vp8_rac_get_nn(c);
00414 }
00415
00416 return 0;
00417 }
00418
00419 static av_always_inline void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src)
00420 {
00421 dst->x = av_clip(src->x, s->mv_min.x, s->mv_max.x);
00422 dst->y = av_clip(src->y, s->mv_min.y, s->mv_max.y);
00423 }
00424
00428 static int read_mv_component(VP56RangeCoder *c, const uint8_t *p)
00429 {
00430 int bit, x = 0;
00431
00432 if (vp56_rac_get_prob_branchy(c, p[0])) {
00433 int i;
00434
00435 for (i = 0; i < 3; i++)
00436 x += vp56_rac_get_prob(c, p[9 + i]) << i;
00437 for (i = 9; i > 3; i--)
00438 x += vp56_rac_get_prob(c, p[9 + i]) << i;
00439 if (!(x & 0xFFF0) || vp56_rac_get_prob(c, p[12]))
00440 x += 8;
00441 } else {
00442
00443 const uint8_t *ps = p+2;
00444 bit = vp56_rac_get_prob(c, *ps);
00445 ps += 1 + 3*bit;
00446 x += 4*bit;
00447 bit = vp56_rac_get_prob(c, *ps);
00448 ps += 1 + bit;
00449 x += 2*bit;
00450 x += vp56_rac_get_prob(c, *ps);
00451 }
00452
00453 return (x && vp56_rac_get_prob(c, p[1])) ? -x : x;
00454 }
00455
00456 static av_always_inline
00457 const uint8_t *get_submv_prob(uint32_t left, uint32_t top)
00458 {
00459 if (left == top)
00460 return vp8_submv_prob[4-!!left];
00461 if (!top)
00462 return vp8_submv_prob[2];
00463 return vp8_submv_prob[1-!!left];
00464 }
00465
00470 static av_always_inline
00471 int decode_splitmvs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb)
00472 {
00473 int part_idx;
00474 int n, num;
00475 VP8Macroblock *top_mb = &mb[2];
00476 VP8Macroblock *left_mb = &mb[-1];
00477 const uint8_t *mbsplits_left = vp8_mbsplits[left_mb->partitioning],
00478 *mbsplits_top = vp8_mbsplits[top_mb->partitioning],
00479 *mbsplits_cur, *firstidx;
00480 VP56mv *top_mv = top_mb->bmv;
00481 VP56mv *left_mv = left_mb->bmv;
00482 VP56mv *cur_mv = mb->bmv;
00483
00484 if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[0])) {
00485 if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[1])) {
00486 part_idx = VP8_SPLITMVMODE_16x8 + vp56_rac_get_prob(c, vp8_mbsplit_prob[2]);
00487 } else {
00488 part_idx = VP8_SPLITMVMODE_8x8;
00489 }
00490 } else {
00491 part_idx = VP8_SPLITMVMODE_4x4;
00492 }
00493
00494 num = vp8_mbsplit_count[part_idx];
00495 mbsplits_cur = vp8_mbsplits[part_idx],
00496 firstidx = vp8_mbfirstidx[part_idx];
00497 mb->partitioning = part_idx;
00498
00499 for (n = 0; n < num; n++) {
00500 int k = firstidx[n];
00501 uint32_t left, above;
00502 const uint8_t *submv_prob;
00503
00504 if (!(k & 3))
00505 left = AV_RN32A(&left_mv[mbsplits_left[k + 3]]);
00506 else
00507 left = AV_RN32A(&cur_mv[mbsplits_cur[k - 1]]);
00508 if (k <= 3)
00509 above = AV_RN32A(&top_mv[mbsplits_top[k + 12]]);
00510 else
00511 above = AV_RN32A(&cur_mv[mbsplits_cur[k - 4]]);
00512
00513 submv_prob = get_submv_prob(left, above);
00514
00515 if (vp56_rac_get_prob_branchy(c, submv_prob[0])) {
00516 if (vp56_rac_get_prob_branchy(c, submv_prob[1])) {
00517 if (vp56_rac_get_prob_branchy(c, submv_prob[2])) {
00518 mb->bmv[n].y = mb->mv.y + read_mv_component(c, s->prob->mvc[0]);
00519 mb->bmv[n].x = mb->mv.x + read_mv_component(c, s->prob->mvc[1]);
00520 } else {
00521 AV_ZERO32(&mb->bmv[n]);
00522 }
00523 } else {
00524 AV_WN32A(&mb->bmv[n], above);
00525 }
00526 } else {
00527 AV_WN32A(&mb->bmv[n], left);
00528 }
00529 }
00530
00531 return num;
00532 }
00533
00534 static av_always_inline
00535 void decode_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y)
00536 {
00537 VP8Macroblock *mb_edge[3] = { mb + 2 ,
00538 mb - 1 ,
00539 mb + 1 };
00540 enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
00541 enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT };
00542 int idx = CNT_ZERO;
00543 int cur_sign_bias = s->sign_bias[mb->ref_frame];
00544 int8_t *sign_bias = s->sign_bias;
00545 VP56mv near_mv[4];
00546 uint8_t cnt[4] = { 0 };
00547 VP56RangeCoder *c = &s->c;
00548
00549 AV_ZERO32(&near_mv[0]);
00550 AV_ZERO32(&near_mv[1]);
00551 AV_ZERO32(&near_mv[2]);
00552
00553
00554 #define MV_EDGE_CHECK(n)\
00555 {\
00556 VP8Macroblock *edge = mb_edge[n];\
00557 int edge_ref = edge->ref_frame;\
00558 if (edge_ref != VP56_FRAME_CURRENT) {\
00559 uint32_t mv = AV_RN32A(&edge->mv);\
00560 if (mv) {\
00561 if (cur_sign_bias != sign_bias[edge_ref]) {\
00562 \
00563 mv = ~mv;\
00564 mv = ((mv&0x7fff7fff) + 0x00010001) ^ (mv&0x80008000);\
00565 }\
00566 if (!n || mv != AV_RN32A(&near_mv[idx]))\
00567 AV_WN32A(&near_mv[++idx], mv);\
00568 cnt[idx] += 1 + (n != 2);\
00569 } else\
00570 cnt[CNT_ZERO] += 1 + (n != 2);\
00571 }\
00572 }
00573
00574 MV_EDGE_CHECK(0)
00575 MV_EDGE_CHECK(1)
00576 MV_EDGE_CHECK(2)
00577
00578 mb->partitioning = VP8_SPLITMVMODE_NONE;
00579 if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_ZERO]][0])) {
00580 mb->mode = VP8_MVMODE_MV;
00581
00582
00583 if (cnt[CNT_SPLITMV] && AV_RN32A(&near_mv[1 + VP8_EDGE_TOP]) == AV_RN32A(&near_mv[1 + VP8_EDGE_TOPLEFT]))
00584 cnt[CNT_NEAREST] += 1;
00585
00586
00587 if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
00588 FFSWAP(uint8_t, cnt[CNT_NEAREST], cnt[CNT_NEAR]);
00589 FFSWAP( VP56mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
00590 }
00591
00592 if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAREST]][1])) {
00593 if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAR]][2])) {
00594
00595
00596 clamp_mv(s, &mb->mv, &near_mv[CNT_ZERO + (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])]);
00597 cnt[CNT_SPLITMV] = ((mb_edge[VP8_EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) +
00598 (mb_edge[VP8_EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 +
00599 (mb_edge[VP8_EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
00600
00601 if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_SPLITMV]][3])) {
00602 mb->mode = VP8_MVMODE_SPLIT;
00603 mb->mv = mb->bmv[decode_splitmvs(s, c, mb) - 1];
00604 } else {
00605 mb->mv.y += read_mv_component(c, s->prob->mvc[0]);
00606 mb->mv.x += read_mv_component(c, s->prob->mvc[1]);
00607 mb->bmv[0] = mb->mv;
00608 }
00609 } else {
00610 clamp_mv(s, &mb->mv, &near_mv[CNT_NEAR]);
00611 mb->bmv[0] = mb->mv;
00612 }
00613 } else {
00614 clamp_mv(s, &mb->mv, &near_mv[CNT_NEAREST]);
00615 mb->bmv[0] = mb->mv;
00616 }
00617 } else {
00618 mb->mode = VP8_MVMODE_ZERO;
00619 AV_ZERO32(&mb->mv);
00620 mb->bmv[0] = mb->mv;
00621 }
00622 }
00623
00624 static av_always_inline
00625 void decode_intra4x4_modes(VP8Context *s, VP56RangeCoder *c,
00626 int mb_x, int keyframe)
00627 {
00628 uint8_t *intra4x4 = s->intra4x4_pred_mode_mb;
00629 if (keyframe) {
00630 int x, y;
00631 uint8_t* const top = s->intra4x4_pred_mode_top + 4 * mb_x;
00632 uint8_t* const left = s->intra4x4_pred_mode_left;
00633 for (y = 0; y < 4; y++) {
00634 for (x = 0; x < 4; x++) {
00635 const uint8_t *ctx;
00636 ctx = vp8_pred4x4_prob_intra[top[x]][left[y]];
00637 *intra4x4 = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
00638 left[y] = top[x] = *intra4x4;
00639 intra4x4++;
00640 }
00641 }
00642 } else {
00643 int i;
00644 for (i = 0; i < 16; i++)
00645 intra4x4[i] = vp8_rac_get_tree(c, vp8_pred4x4_tree, vp8_pred4x4_prob_inter);
00646 }
00647 }
00648
00649 static av_always_inline
00650 void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, uint8_t *segment, uint8_t *ref)
00651 {
00652 VP56RangeCoder *c = &s->c;
00653
00654 if (s->segmentation.update_map) {
00655 int bit = vp56_rac_get_prob(c, s->prob->segmentid[0]);
00656 *segment = vp56_rac_get_prob(c, s->prob->segmentid[1+bit]) + 2*bit;
00657 } else if (s->segmentation.enabled)
00658 *segment = ref ? *ref : *segment;
00659 s->segment = *segment;
00660
00661 mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
00662
00663 if (s->keyframe) {
00664 mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra, vp8_pred16x16_prob_intra);
00665
00666 if (mb->mode == MODE_I4x4) {
00667 decode_intra4x4_modes(s, c, mb_x, 1);
00668 } else {
00669 const uint32_t modes = vp8_pred4x4_mode[mb->mode] * 0x01010101u;
00670 AV_WN32A(s->intra4x4_pred_mode_top + 4 * mb_x, modes);
00671 AV_WN32A(s->intra4x4_pred_mode_left, modes);
00672 }
00673
00674 s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, vp8_pred8x8c_prob_intra);
00675 mb->ref_frame = VP56_FRAME_CURRENT;
00676 } else if (vp56_rac_get_prob_branchy(c, s->prob->intra)) {
00677
00678 if (vp56_rac_get_prob_branchy(c, s->prob->last))
00679 mb->ref_frame = vp56_rac_get_prob(c, s->prob->golden) ?
00680 VP56_FRAME_GOLDEN2 : VP56_FRAME_GOLDEN;
00681 else
00682 mb->ref_frame = VP56_FRAME_PREVIOUS;
00683 s->ref_count[mb->ref_frame-1]++;
00684
00685
00686 decode_mvs(s, mb, mb_x, mb_y);
00687 } else {
00688
00689 mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_inter, s->prob->pred16x16);
00690
00691 if (mb->mode == MODE_I4x4)
00692 decode_intra4x4_modes(s, c, mb_x, 0);
00693
00694 s->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, s->prob->pred8x8c);
00695 mb->ref_frame = VP56_FRAME_CURRENT;
00696 mb->partitioning = VP8_SPLITMVMODE_NONE;
00697 AV_ZERO32(&mb->bmv[0]);
00698 }
00699 }
00700
00701 #ifndef decode_block_coeffs_internal
00702
00711 static int decode_block_coeffs_internal(VP56RangeCoder *c, DCTELEM block[16],
00712 uint8_t probs[16][3][NUM_DCT_TOKENS-1],
00713 int i, uint8_t *token_prob, int16_t qmul[2])
00714 {
00715 goto skip_eob;
00716 do {
00717 int coeff;
00718 if (!vp56_rac_get_prob_branchy(c, token_prob[0]))
00719 return i;
00720
00721 skip_eob:
00722 if (!vp56_rac_get_prob_branchy(c, token_prob[1])) {
00723 if (++i == 16)
00724 return i;
00725 token_prob = probs[i][0];
00726 goto skip_eob;
00727 }
00728
00729 if (!vp56_rac_get_prob_branchy(c, token_prob[2])) {
00730 coeff = 1;
00731 token_prob = probs[i+1][1];
00732 } else {
00733 if (!vp56_rac_get_prob_branchy(c, token_prob[3])) {
00734 coeff = vp56_rac_get_prob_branchy(c, token_prob[4]);
00735 if (coeff)
00736 coeff += vp56_rac_get_prob(c, token_prob[5]);
00737 coeff += 2;
00738 } else {
00739
00740 if (!vp56_rac_get_prob_branchy(c, token_prob[6])) {
00741 if (!vp56_rac_get_prob_branchy(c, token_prob[7])) {
00742 coeff = 5 + vp56_rac_get_prob(c, vp8_dct_cat1_prob[0]);
00743 } else {
00744 coeff = 7;
00745 coeff += vp56_rac_get_prob(c, vp8_dct_cat2_prob[0]) << 1;
00746 coeff += vp56_rac_get_prob(c, vp8_dct_cat2_prob[1]);
00747 }
00748 } else {
00749 int a = vp56_rac_get_prob(c, token_prob[8]);
00750 int b = vp56_rac_get_prob(c, token_prob[9+a]);
00751 int cat = (a<<1) + b;
00752 coeff = 3 + (8<<cat);
00753 coeff += vp8_rac_get_coeff(c, ff_vp8_dct_cat_prob[cat]);
00754 }
00755 }
00756 token_prob = probs[i+1][2];
00757 }
00758 block[zigzag_scan[i]] = (vp8_rac_get(c) ? -coeff : coeff) * qmul[!!i];
00759 } while (++i < 16);
00760
00761 return i;
00762 }
00763 #endif
00764
00776 static av_always_inline
00777 int decode_block_coeffs(VP56RangeCoder *c, DCTELEM block[16],
00778 uint8_t probs[16][3][NUM_DCT_TOKENS-1],
00779 int i, int zero_nhood, int16_t qmul[2])
00780 {
00781 uint8_t *token_prob = probs[i][zero_nhood];
00782 if (!vp56_rac_get_prob_branchy(c, token_prob[0]))
00783 return 0;
00784 return decode_block_coeffs_internal(c, block, probs, i, token_prob, qmul);
00785 }
00786
00787 static av_always_inline
00788 void decode_mb_coeffs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
00789 uint8_t t_nnz[9], uint8_t l_nnz[9])
00790 {
00791 int i, x, y, luma_start = 0, luma_ctx = 3;
00792 int nnz_pred, nnz, nnz_total = 0;
00793 int segment = s->segment;
00794 int block_dc = 0;
00795
00796 if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
00797 nnz_pred = t_nnz[8] + l_nnz[8];
00798
00799
00800 nnz = decode_block_coeffs(c, s->block_dc, s->prob->token[1], 0, nnz_pred,
00801 s->qmat[segment].luma_dc_qmul);
00802 l_nnz[8] = t_nnz[8] = !!nnz;
00803 if (nnz) {
00804 nnz_total += nnz;
00805 block_dc = 1;
00806 if (nnz == 1)
00807 s->vp8dsp.vp8_luma_dc_wht_dc(s->block, s->block_dc);
00808 else
00809 s->vp8dsp.vp8_luma_dc_wht(s->block, s->block_dc);
00810 }
00811 luma_start = 1;
00812 luma_ctx = 0;
00813 }
00814
00815
00816 for (y = 0; y < 4; y++)
00817 for (x = 0; x < 4; x++) {
00818 nnz_pred = l_nnz[y] + t_nnz[x];
00819 nnz = decode_block_coeffs(c, s->block[y][x], s->prob->token[luma_ctx], luma_start,
00820 nnz_pred, s->qmat[segment].luma_qmul);
00821
00822 s->non_zero_count_cache[y][x] = nnz + block_dc;
00823 t_nnz[x] = l_nnz[y] = !!nnz;
00824 nnz_total += nnz;
00825 }
00826
00827
00828
00829
00830 for (i = 4; i < 6; i++)
00831 for (y = 0; y < 2; y++)
00832 for (x = 0; x < 2; x++) {
00833 nnz_pred = l_nnz[i+2*y] + t_nnz[i+2*x];
00834 nnz = decode_block_coeffs(c, s->block[i][(y<<1)+x], s->prob->token[2], 0,
00835 nnz_pred, s->qmat[segment].chroma_qmul);
00836 s->non_zero_count_cache[i][(y<<1)+x] = nnz;
00837 t_nnz[i+2*x] = l_nnz[i+2*y] = !!nnz;
00838 nnz_total += nnz;
00839 }
00840
00841
00842
00843
00844 if (!nnz_total)
00845 mb->skip = 1;
00846 }
00847
00848 static av_always_inline
00849 void backup_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
00850 int linesize, int uvlinesize, int simple)
00851 {
00852 AV_COPY128(top_border, src_y + 15*linesize);
00853 if (!simple) {
00854 AV_COPY64(top_border+16, src_cb + 7*uvlinesize);
00855 AV_COPY64(top_border+24, src_cr + 7*uvlinesize);
00856 }
00857 }
00858
00859 static av_always_inline
00860 void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
00861 int linesize, int uvlinesize, int mb_x, int mb_y, int mb_width,
00862 int simple, int xchg)
00863 {
00864 uint8_t *top_border_m1 = top_border-32;
00865 src_y -= linesize;
00866 src_cb -= uvlinesize;
00867 src_cr -= uvlinesize;
00868
00869 #define XCHG(a,b,xchg) do { \
00870 if (xchg) AV_SWAP64(b,a); \
00871 else AV_COPY64(b,a); \
00872 } while (0)
00873
00874 XCHG(top_border_m1+8, src_y-8, xchg);
00875 XCHG(top_border, src_y, xchg);
00876 XCHG(top_border+8, src_y+8, 1);
00877 if (mb_x < mb_width-1)
00878 XCHG(top_border+32, src_y+16, 1);
00879
00880
00881
00882 if (!simple || !mb_y) {
00883 XCHG(top_border_m1+16, src_cb-8, xchg);
00884 XCHG(top_border_m1+24, src_cr-8, xchg);
00885 XCHG(top_border+16, src_cb, 1);
00886 XCHG(top_border+24, src_cr, 1);
00887 }
00888 }
00889
00890 static av_always_inline
00891 int check_dc_pred8x8_mode(int mode, int mb_x, int mb_y)
00892 {
00893 if (!mb_x) {
00894 return mb_y ? TOP_DC_PRED8x8 : DC_128_PRED8x8;
00895 } else {
00896 return mb_y ? mode : LEFT_DC_PRED8x8;
00897 }
00898 }
00899
00900 static av_always_inline
00901 int check_tm_pred8x8_mode(int mode, int mb_x, int mb_y)
00902 {
00903 if (!mb_x) {
00904 return mb_y ? VERT_PRED8x8 : DC_129_PRED8x8;
00905 } else {
00906 return mb_y ? mode : HOR_PRED8x8;
00907 }
00908 }
00909
00910 static av_always_inline
00911 int check_intra_pred8x8_mode(int mode, int mb_x, int mb_y)
00912 {
00913 if (mode == DC_PRED8x8) {
00914 return check_dc_pred8x8_mode(mode, mb_x, mb_y);
00915 } else {
00916 return mode;
00917 }
00918 }
00919
00920 static av_always_inline
00921 int check_intra_pred8x8_mode_emuedge(int mode, int mb_x, int mb_y)
00922 {
00923 switch (mode) {
00924 case DC_PRED8x8:
00925 return check_dc_pred8x8_mode(mode, mb_x, mb_y);
00926 case VERT_PRED8x8:
00927 return !mb_y ? DC_127_PRED8x8 : mode;
00928 case HOR_PRED8x8:
00929 return !mb_x ? DC_129_PRED8x8 : mode;
00930 case PLANE_PRED8x8 :
00931 return check_tm_pred8x8_mode(mode, mb_x, mb_y);
00932 }
00933 return mode;
00934 }
00935
00936 static av_always_inline
00937 int check_tm_pred4x4_mode(int mode, int mb_x, int mb_y)
00938 {
00939 if (!mb_x) {
00940 return mb_y ? VERT_VP8_PRED : DC_129_PRED;
00941 } else {
00942 return mb_y ? mode : HOR_VP8_PRED;
00943 }
00944 }
00945
00946 static av_always_inline
00947 int check_intra_pred4x4_mode_emuedge(int mode, int mb_x, int mb_y, int *copy_buf)
00948 {
00949 switch (mode) {
00950 case VERT_PRED:
00951 if (!mb_x && mb_y) {
00952 *copy_buf = 1;
00953 return mode;
00954 }
00955
00956 case DIAG_DOWN_LEFT_PRED:
00957 case VERT_LEFT_PRED:
00958 return !mb_y ? DC_127_PRED : mode;
00959 case HOR_PRED:
00960 if (!mb_y) {
00961 *copy_buf = 1;
00962 return mode;
00963 }
00964
00965 case HOR_UP_PRED:
00966 return !mb_x ? DC_129_PRED : mode;
00967 case TM_VP8_PRED:
00968 return check_tm_pred4x4_mode(mode, mb_x, mb_y);
00969 case DC_PRED:
00970 case DIAG_DOWN_RIGHT_PRED:
00971 case VERT_RIGHT_PRED:
00972 case HOR_DOWN_PRED:
00973 if (!mb_y || !mb_x)
00974 *copy_buf = 1;
00975 return mode;
00976 }
00977 return mode;
00978 }
00979
00980 static av_always_inline
00981 void intra_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
00982 int mb_x, int mb_y)
00983 {
00984 AVCodecContext *avctx = s->avctx;
00985 int x, y, mode, nnz;
00986 uint32_t tr;
00987
00988
00989
00990 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE && !mb_y) && (s->deblock_filter || !mb_y))
00991 xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
00992 s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
00993 s->filter.simple, 1);
00994
00995 if (mb->mode < MODE_I4x4) {
00996 if (avctx->flags & CODEC_FLAG_EMU_EDGE) {
00997 mode = check_intra_pred8x8_mode_emuedge(mb->mode, mb_x, mb_y);
00998 } else {
00999 mode = check_intra_pred8x8_mode(mb->mode, mb_x, mb_y);
01000 }
01001 s->hpc.pred16x16[mode](dst[0], s->linesize);
01002 } else {
01003 uint8_t *ptr = dst[0];
01004 uint8_t *intra4x4 = s->intra4x4_pred_mode_mb;
01005 uint8_t tr_top[4] = { 127, 127, 127, 127 };
01006
01007
01008
01009 uint8_t *tr_right = ptr - s->linesize + 16;
01010
01011
01012
01013 if (!(!mb_y && avctx->flags & CODEC_FLAG_EMU_EDGE) &&
01014 mb_x == s->mb_width-1) {
01015 tr = tr_right[-1]*0x01010101u;
01016 tr_right = (uint8_t *)&tr;
01017 }
01018
01019 if (mb->skip)
01020 AV_ZERO128(s->non_zero_count_cache);
01021
01022 for (y = 0; y < 4; y++) {
01023 uint8_t *topright = ptr + 4 - s->linesize;
01024 for (x = 0; x < 4; x++) {
01025 int copy = 0, linesize = s->linesize;
01026 uint8_t *dst = ptr+4*x;
01027 DECLARE_ALIGNED(4, uint8_t, copy_dst)[5*8];
01028
01029 if ((y == 0 || x == 3) && mb_y == 0 && avctx->flags & CODEC_FLAG_EMU_EDGE) {
01030 topright = tr_top;
01031 } else if (x == 3)
01032 topright = tr_right;
01033
01034 if (avctx->flags & CODEC_FLAG_EMU_EDGE) {
01035 mode = check_intra_pred4x4_mode_emuedge(intra4x4[x], mb_x + x, mb_y + y, ©);
01036 if (copy) {
01037 dst = copy_dst + 12;
01038 linesize = 8;
01039 if (!(mb_y + y)) {
01040 copy_dst[3] = 127U;
01041 AV_WN32A(copy_dst+4, 127U * 0x01010101U);
01042 } else {
01043 AV_COPY32(copy_dst+4, ptr+4*x-s->linesize);
01044 if (!(mb_x + x)) {
01045 copy_dst[3] = 129U;
01046 } else {
01047 copy_dst[3] = ptr[4*x-s->linesize-1];
01048 }
01049 }
01050 if (!(mb_x + x)) {
01051 copy_dst[11] =
01052 copy_dst[19] =
01053 copy_dst[27] =
01054 copy_dst[35] = 129U;
01055 } else {
01056 copy_dst[11] = ptr[4*x -1];
01057 copy_dst[19] = ptr[4*x+s->linesize -1];
01058 copy_dst[27] = ptr[4*x+s->linesize*2-1];
01059 copy_dst[35] = ptr[4*x+s->linesize*3-1];
01060 }
01061 }
01062 } else {
01063 mode = intra4x4[x];
01064 }
01065 s->hpc.pred4x4[mode](dst, topright, linesize);
01066 if (copy) {
01067 AV_COPY32(ptr+4*x , copy_dst+12);
01068 AV_COPY32(ptr+4*x+s->linesize , copy_dst+20);
01069 AV_COPY32(ptr+4*x+s->linesize*2, copy_dst+28);
01070 AV_COPY32(ptr+4*x+s->linesize*3, copy_dst+36);
01071 }
01072
01073 nnz = s->non_zero_count_cache[y][x];
01074 if (nnz) {
01075 if (nnz == 1)
01076 s->vp8dsp.vp8_idct_dc_add(ptr+4*x, s->block[y][x], s->linesize);
01077 else
01078 s->vp8dsp.vp8_idct_add(ptr+4*x, s->block[y][x], s->linesize);
01079 }
01080 topright += 4;
01081 }
01082
01083 ptr += 4*s->linesize;
01084 intra4x4 += 4;
01085 }
01086 }
01087
01088 if (avctx->flags & CODEC_FLAG_EMU_EDGE) {
01089 mode = check_intra_pred8x8_mode_emuedge(s->chroma_pred_mode, mb_x, mb_y);
01090 } else {
01091 mode = check_intra_pred8x8_mode(s->chroma_pred_mode, mb_x, mb_y);
01092 }
01093 s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
01094 s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
01095
01096 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE && !mb_y) && (s->deblock_filter || !mb_y))
01097 xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
01098 s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
01099 s->filter.simple, 0);
01100 }
01101
01102 static const uint8_t subpel_idx[3][8] = {
01103 { 0, 1, 2, 1, 2, 1, 2, 1 },
01104
01105 { 0, 3, 5, 3, 5, 3, 5, 3 },
01106 { 0, 2, 3, 2, 3, 2, 3, 2 },
01107 };
01108
01125 static av_always_inline
01126 void vp8_mc_luma(VP8Context *s, uint8_t *dst, AVFrame *ref, const VP56mv *mv,
01127 int x_off, int y_off, int block_w, int block_h,
01128 int width, int height, int linesize,
01129 vp8_mc_func mc_func[3][3])
01130 {
01131 uint8_t *src = ref->data[0];
01132
01133 if (AV_RN32A(mv)) {
01134
01135 int mx = (mv->x << 1)&7, mx_idx = subpel_idx[0][mx];
01136 int my = (mv->y << 1)&7, my_idx = subpel_idx[0][my];
01137
01138 x_off += mv->x >> 2;
01139 y_off += mv->y >> 2;
01140
01141
01142 ff_thread_await_progress(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 4, 0);
01143 src += y_off * linesize + x_off;
01144 if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
01145 y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
01146 s->dsp.emulated_edge_mc(s->edge_emu_buffer, src - my_idx * linesize - mx_idx, linesize,
01147 block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
01148 x_off - mx_idx, y_off - my_idx, width, height);
01149 src = s->edge_emu_buffer + mx_idx + linesize * my_idx;
01150 }
01151 mc_func[my_idx][mx_idx](dst, linesize, src, linesize, block_h, mx, my);
01152 } else {
01153 ff_thread_await_progress(ref, (3 + y_off + block_h) >> 4, 0);
01154 mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0);
01155 }
01156 }
01157
01175 static av_always_inline
01176 void vp8_mc_chroma(VP8Context *s, uint8_t *dst1, uint8_t *dst2, AVFrame *ref,
01177 const VP56mv *mv, int x_off, int y_off,
01178 int block_w, int block_h, int width, int height, int linesize,
01179 vp8_mc_func mc_func[3][3])
01180 {
01181 uint8_t *src1 = ref->data[1], *src2 = ref->data[2];
01182
01183 if (AV_RN32A(mv)) {
01184 int mx = mv->x&7, mx_idx = subpel_idx[0][mx];
01185 int my = mv->y&7, my_idx = subpel_idx[0][my];
01186
01187 x_off += mv->x >> 3;
01188 y_off += mv->y >> 3;
01189
01190
01191 src1 += y_off * linesize + x_off;
01192 src2 += y_off * linesize + x_off;
01193 ff_thread_await_progress(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 3, 0);
01194 if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
01195 y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
01196 s->dsp.emulated_edge_mc(s->edge_emu_buffer, src1 - my_idx * linesize - mx_idx, linesize,
01197 block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
01198 x_off - mx_idx, y_off - my_idx, width, height);
01199 src1 = s->edge_emu_buffer + mx_idx + linesize * my_idx;
01200 mc_func[my_idx][mx_idx](dst1, linesize, src1, linesize, block_h, mx, my);
01201
01202 s->dsp.emulated_edge_mc(s->edge_emu_buffer, src2 - my_idx * linesize - mx_idx, linesize,
01203 block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
01204 x_off - mx_idx, y_off - my_idx, width, height);
01205 src2 = s->edge_emu_buffer + mx_idx + linesize * my_idx;
01206 mc_func[my_idx][mx_idx](dst2, linesize, src2, linesize, block_h, mx, my);
01207 } else {
01208 mc_func[my_idx][mx_idx](dst1, linesize, src1, linesize, block_h, mx, my);
01209 mc_func[my_idx][mx_idx](dst2, linesize, src2, linesize, block_h, mx, my);
01210 }
01211 } else {
01212 ff_thread_await_progress(ref, (3 + y_off + block_h) >> 3, 0);
01213 mc_func[0][0](dst1, linesize, src1 + y_off * linesize + x_off, linesize, block_h, 0, 0);
01214 mc_func[0][0](dst2, linesize, src2 + y_off * linesize + x_off, linesize, block_h, 0, 0);
01215 }
01216 }
01217
01218 static av_always_inline
01219 void vp8_mc_part(VP8Context *s, uint8_t *dst[3],
01220 AVFrame *ref_frame, int x_off, int y_off,
01221 int bx_off, int by_off,
01222 int block_w, int block_h,
01223 int width, int height, VP56mv *mv)
01224 {
01225 VP56mv uvmv = *mv;
01226
01227
01228 vp8_mc_luma(s, dst[0] + by_off * s->linesize + bx_off,
01229 ref_frame, mv, x_off + bx_off, y_off + by_off,
01230 block_w, block_h, width, height, s->linesize,
01231 s->put_pixels_tab[block_w == 8]);
01232
01233
01234 if (s->profile == 3) {
01235 uvmv.x &= ~7;
01236 uvmv.y &= ~7;
01237 }
01238 x_off >>= 1; y_off >>= 1;
01239 bx_off >>= 1; by_off >>= 1;
01240 width >>= 1; height >>= 1;
01241 block_w >>= 1; block_h >>= 1;
01242 vp8_mc_chroma(s, dst[1] + by_off * s->uvlinesize + bx_off,
01243 dst[2] + by_off * s->uvlinesize + bx_off, ref_frame,
01244 &uvmv, x_off + bx_off, y_off + by_off,
01245 block_w, block_h, width, height, s->uvlinesize,
01246 s->put_pixels_tab[1 + (block_w == 4)]);
01247 }
01248
01249
01250
01251 static av_always_inline void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int mb_xy, int ref)
01252 {
01253
01254 if (s->ref_count[ref-1] > (mb_xy >> 5)) {
01255 int x_off = mb_x << 4, y_off = mb_y << 4;
01256 int mx = (mb->mv.x>>2) + x_off + 8;
01257 int my = (mb->mv.y>>2) + y_off;
01258 uint8_t **src= s->framep[ref]->data;
01259 int off= mx + (my + (mb_x&3)*4)*s->linesize + 64;
01260
01261
01262
01263 s->dsp.prefetch(src[0]+off, s->linesize, 4);
01264 off= (mx>>1) + ((my>>1) + (mb_x&7))*s->uvlinesize + 64;
01265 s->dsp.prefetch(src[1]+off, src[2]-src[1], 2);
01266 }
01267 }
01268
01272 static av_always_inline
01273 void inter_predict(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb,
01274 int mb_x, int mb_y)
01275 {
01276 int x_off = mb_x << 4, y_off = mb_y << 4;
01277 int width = 16*s->mb_width, height = 16*s->mb_height;
01278 AVFrame *ref = s->framep[mb->ref_frame];
01279 VP56mv *bmv = mb->bmv;
01280
01281 switch (mb->partitioning) {
01282 case VP8_SPLITMVMODE_NONE:
01283 vp8_mc_part(s, dst, ref, x_off, y_off,
01284 0, 0, 16, 16, width, height, &mb->mv);
01285 break;
01286 case VP8_SPLITMVMODE_4x4: {
01287 int x, y;
01288 VP56mv uvmv;
01289
01290
01291 for (y = 0; y < 4; y++) {
01292 for (x = 0; x < 4; x++) {
01293 vp8_mc_luma(s, dst[0] + 4*y*s->linesize + x*4,
01294 ref, &bmv[4*y + x],
01295 4*x + x_off, 4*y + y_off, 4, 4,
01296 width, height, s->linesize,
01297 s->put_pixels_tab[2]);
01298 }
01299 }
01300
01301
01302 x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
01303 for (y = 0; y < 2; y++) {
01304 for (x = 0; x < 2; x++) {
01305 uvmv.x = mb->bmv[ 2*y * 4 + 2*x ].x +
01306 mb->bmv[ 2*y * 4 + 2*x+1].x +
01307 mb->bmv[(2*y+1) * 4 + 2*x ].x +
01308 mb->bmv[(2*y+1) * 4 + 2*x+1].x;
01309 uvmv.y = mb->bmv[ 2*y * 4 + 2*x ].y +
01310 mb->bmv[ 2*y * 4 + 2*x+1].y +
01311 mb->bmv[(2*y+1) * 4 + 2*x ].y +
01312 mb->bmv[(2*y+1) * 4 + 2*x+1].y;
01313 uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT-1))) >> 2;
01314 uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT-1))) >> 2;
01315 if (s->profile == 3) {
01316 uvmv.x &= ~7;
01317 uvmv.y &= ~7;
01318 }
01319 vp8_mc_chroma(s, dst[1] + 4*y*s->uvlinesize + x*4,
01320 dst[2] + 4*y*s->uvlinesize + x*4, ref, &uvmv,
01321 4*x + x_off, 4*y + y_off, 4, 4,
01322 width, height, s->uvlinesize,
01323 s->put_pixels_tab[2]);
01324 }
01325 }
01326 break;
01327 }
01328 case VP8_SPLITMVMODE_16x8:
01329 vp8_mc_part(s, dst, ref, x_off, y_off,
01330 0, 0, 16, 8, width, height, &bmv[0]);
01331 vp8_mc_part(s, dst, ref, x_off, y_off,
01332 0, 8, 16, 8, width, height, &bmv[1]);
01333 break;
01334 case VP8_SPLITMVMODE_8x16:
01335 vp8_mc_part(s, dst, ref, x_off, y_off,
01336 0, 0, 8, 16, width, height, &bmv[0]);
01337 vp8_mc_part(s, dst, ref, x_off, y_off,
01338 8, 0, 8, 16, width, height, &bmv[1]);
01339 break;
01340 case VP8_SPLITMVMODE_8x8:
01341 vp8_mc_part(s, dst, ref, x_off, y_off,
01342 0, 0, 8, 8, width, height, &bmv[0]);
01343 vp8_mc_part(s, dst, ref, x_off, y_off,
01344 8, 0, 8, 8, width, height, &bmv[1]);
01345 vp8_mc_part(s, dst, ref, x_off, y_off,
01346 0, 8, 8, 8, width, height, &bmv[2]);
01347 vp8_mc_part(s, dst, ref, x_off, y_off,
01348 8, 8, 8, 8, width, height, &bmv[3]);
01349 break;
01350 }
01351 }
01352
01353 static av_always_inline void idct_mb(VP8Context *s, uint8_t *dst[3], VP8Macroblock *mb)
01354 {
01355 int x, y, ch;
01356
01357 if (mb->mode != MODE_I4x4) {
01358 uint8_t *y_dst = dst[0];
01359 for (y = 0; y < 4; y++) {
01360 uint32_t nnz4 = AV_RL32(s->non_zero_count_cache[y]);
01361 if (nnz4) {
01362 if (nnz4&~0x01010101) {
01363 for (x = 0; x < 4; x++) {
01364 if ((uint8_t)nnz4 == 1)
01365 s->vp8dsp.vp8_idct_dc_add(y_dst+4*x, s->block[y][x], s->linesize);
01366 else if((uint8_t)nnz4 > 1)
01367 s->vp8dsp.vp8_idct_add(y_dst+4*x, s->block[y][x], s->linesize);
01368 nnz4 >>= 8;
01369 if (!nnz4)
01370 break;
01371 }
01372 } else {
01373 s->vp8dsp.vp8_idct_dc_add4y(y_dst, s->block[y], s->linesize);
01374 }
01375 }
01376 y_dst += 4*s->linesize;
01377 }
01378 }
01379
01380 for (ch = 0; ch < 2; ch++) {
01381 uint32_t nnz4 = AV_RL32(s->non_zero_count_cache[4+ch]);
01382 if (nnz4) {
01383 uint8_t *ch_dst = dst[1+ch];
01384 if (nnz4&~0x01010101) {
01385 for (y = 0; y < 2; y++) {
01386 for (x = 0; x < 2; x++) {
01387 if ((uint8_t)nnz4 == 1)
01388 s->vp8dsp.vp8_idct_dc_add(ch_dst+4*x, s->block[4+ch][(y<<1)+x], s->uvlinesize);
01389 else if((uint8_t)nnz4 > 1)
01390 s->vp8dsp.vp8_idct_add(ch_dst+4*x, s->block[4+ch][(y<<1)+x], s->uvlinesize);
01391 nnz4 >>= 8;
01392 if (!nnz4)
01393 goto chroma_idct_end;
01394 }
01395 ch_dst += 4*s->uvlinesize;
01396 }
01397 } else {
01398 s->vp8dsp.vp8_idct_dc_add4uv(ch_dst, s->block[4+ch], s->uvlinesize);
01399 }
01400 }
01401 chroma_idct_end: ;
01402 }
01403 }
01404
01405 static av_always_inline void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb, VP8FilterStrength *f )
01406 {
01407 int interior_limit, filter_level;
01408
01409 if (s->segmentation.enabled) {
01410 filter_level = s->segmentation.filter_level[s->segment];
01411 if (!s->segmentation.absolute_vals)
01412 filter_level += s->filter.level;
01413 } else
01414 filter_level = s->filter.level;
01415
01416 if (s->lf_delta.enabled) {
01417 filter_level += s->lf_delta.ref[mb->ref_frame];
01418 filter_level += s->lf_delta.mode[mb->mode];
01419 }
01420
01421 filter_level = av_clip_uintp2(filter_level, 6);
01422
01423 interior_limit = filter_level;
01424 if (s->filter.sharpness) {
01425 interior_limit >>= (s->filter.sharpness + 3) >> 2;
01426 interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
01427 }
01428 interior_limit = FFMAX(interior_limit, 1);
01429
01430 f->filter_level = filter_level;
01431 f->inner_limit = interior_limit;
01432 f->inner_filter = !mb->skip || mb->mode == MODE_I4x4 || mb->mode == VP8_MVMODE_SPLIT;
01433 }
01434
01435 static av_always_inline void filter_mb(VP8Context *s, uint8_t *dst[3], VP8FilterStrength *f, int mb_x, int mb_y)
01436 {
01437 int mbedge_lim, bedge_lim, hev_thresh;
01438 int filter_level = f->filter_level;
01439 int inner_limit = f->inner_limit;
01440 int inner_filter = f->inner_filter;
01441 int linesize = s->linesize;
01442 int uvlinesize = s->uvlinesize;
01443 static const uint8_t hev_thresh_lut[2][64] = {
01444 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
01445 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
01446 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
01447 3, 3, 3, 3 },
01448 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
01449 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
01450 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
01451 2, 2, 2, 2 }
01452 };
01453
01454 if (!filter_level)
01455 return;
01456
01457 bedge_lim = 2*filter_level + inner_limit;
01458 mbedge_lim = bedge_lim + 4;
01459
01460 hev_thresh = hev_thresh_lut[s->keyframe][filter_level];
01461
01462 if (mb_x) {
01463 s->vp8dsp.vp8_h_loop_filter16y(dst[0], linesize,
01464 mbedge_lim, inner_limit, hev_thresh);
01465 s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], uvlinesize,
01466 mbedge_lim, inner_limit, hev_thresh);
01467 }
01468
01469 if (inner_filter) {
01470 s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 4, linesize, bedge_lim,
01471 inner_limit, hev_thresh);
01472 s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 8, linesize, bedge_lim,
01473 inner_limit, hev_thresh);
01474 s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+12, linesize, bedge_lim,
01475 inner_limit, hev_thresh);
01476 s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4,
01477 uvlinesize, bedge_lim,
01478 inner_limit, hev_thresh);
01479 }
01480
01481 if (mb_y) {
01482 s->vp8dsp.vp8_v_loop_filter16y(dst[0], linesize,
01483 mbedge_lim, inner_limit, hev_thresh);
01484 s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], uvlinesize,
01485 mbedge_lim, inner_limit, hev_thresh);
01486 }
01487
01488 if (inner_filter) {
01489 s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 4*linesize,
01490 linesize, bedge_lim,
01491 inner_limit, hev_thresh);
01492 s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 8*linesize,
01493 linesize, bedge_lim,
01494 inner_limit, hev_thresh);
01495 s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+12*linesize,
01496 linesize, bedge_lim,
01497 inner_limit, hev_thresh);
01498 s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * uvlinesize,
01499 dst[2] + 4 * uvlinesize,
01500 uvlinesize, bedge_lim,
01501 inner_limit, hev_thresh);
01502 }
01503 }
01504
01505 static av_always_inline void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8FilterStrength *f, int mb_x, int mb_y)
01506 {
01507 int mbedge_lim, bedge_lim;
01508 int filter_level = f->filter_level;
01509 int inner_limit = f->inner_limit;
01510 int inner_filter = f->inner_filter;
01511 int linesize = s->linesize;
01512
01513 if (!filter_level)
01514 return;
01515
01516 bedge_lim = 2*filter_level + inner_limit;
01517 mbedge_lim = bedge_lim + 4;
01518
01519 if (mb_x)
01520 s->vp8dsp.vp8_h_loop_filter_simple(dst, linesize, mbedge_lim);
01521 if (inner_filter) {
01522 s->vp8dsp.vp8_h_loop_filter_simple(dst+ 4, linesize, bedge_lim);
01523 s->vp8dsp.vp8_h_loop_filter_simple(dst+ 8, linesize, bedge_lim);
01524 s->vp8dsp.vp8_h_loop_filter_simple(dst+12, linesize, bedge_lim);
01525 }
01526
01527 if (mb_y)
01528 s->vp8dsp.vp8_v_loop_filter_simple(dst, linesize, mbedge_lim);
01529 if (inner_filter) {
01530 s->vp8dsp.vp8_v_loop_filter_simple(dst+ 4*linesize, linesize, bedge_lim);
01531 s->vp8dsp.vp8_v_loop_filter_simple(dst+ 8*linesize, linesize, bedge_lim);
01532 s->vp8dsp.vp8_v_loop_filter_simple(dst+12*linesize, linesize, bedge_lim);
01533 }
01534 }
01535
01536 static void filter_mb_row(VP8Context *s, AVFrame *curframe, int mb_y)
01537 {
01538 VP8FilterStrength *f = s->filter_strength;
01539 uint8_t *dst[3] = {
01540 curframe->data[0] + 16*mb_y*s->linesize,
01541 curframe->data[1] + 8*mb_y*s->uvlinesize,
01542 curframe->data[2] + 8*mb_y*s->uvlinesize
01543 };
01544 int mb_x;
01545
01546 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
01547 backup_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2], s->linesize, s->uvlinesize, 0);
01548 filter_mb(s, dst, f++, mb_x, mb_y);
01549 dst[0] += 16;
01550 dst[1] += 8;
01551 dst[2] += 8;
01552 }
01553 }
01554
01555 static void filter_mb_row_simple(VP8Context *s, AVFrame *curframe, int mb_y)
01556 {
01557 VP8FilterStrength *f = s->filter_strength;
01558 uint8_t *dst = curframe->data[0] + 16*mb_y*s->linesize;
01559 int mb_x;
01560
01561 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
01562 backup_mb_border(s->top_border[mb_x+1], dst, NULL, NULL, s->linesize, 0, 1);
01563 filter_mb_simple(s, dst, f++, mb_x, mb_y);
01564 dst += 16;
01565 }
01566 }
01567
01568 static void release_queued_segmaps(VP8Context *s, int is_close)
01569 {
01570 int leave_behind = is_close ? 0 : !s->maps_are_invalid;
01571 while (s->num_maps_to_be_freed > leave_behind)
01572 av_freep(&s->segmentation_maps[--s->num_maps_to_be_freed]);
01573 s->maps_are_invalid = 0;
01574 }
01575
01576 static int vp8_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
01577 AVPacket *avpkt)
01578 {
01579 VP8Context *s = avctx->priv_data;
01580 int ret, mb_x, mb_y, i, y, referenced;
01581 enum AVDiscard skip_thresh;
01582 AVFrame *av_uninit(curframe), *prev_frame;
01583
01584 release_queued_segmaps(s, 0);
01585
01586 if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
01587 goto err;
01588
01589 prev_frame = s->framep[VP56_FRAME_CURRENT];
01590
01591 referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
01592 || s->update_altref == VP56_FRAME_CURRENT;
01593
01594 skip_thresh = !referenced ? AVDISCARD_NONREF :
01595 !s->keyframe ? AVDISCARD_NONKEY : AVDISCARD_ALL;
01596
01597 if (avctx->skip_frame >= skip_thresh) {
01598 s->invisible = 1;
01599 memcpy(&s->next_framep[0], &s->framep[0], sizeof(s->framep[0]) * 4);
01600 goto skip_decode;
01601 }
01602 s->deblock_filter = s->filter.level && avctx->skip_loop_filter < skip_thresh;
01603
01604
01605 for (i = 0; i < 5; i++)
01606 if (s->frames[i].data[0] &&
01607 &s->frames[i] != prev_frame &&
01608 &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
01609 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
01610 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
01611 vp8_release_frame(s, &s->frames[i], 1, 0);
01612
01613
01614 for (i = 0; i < 5; i++)
01615 if (&s->frames[i] != prev_frame &&
01616 &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
01617 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
01618 &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
01619 curframe = s->framep[VP56_FRAME_CURRENT] = &s->frames[i];
01620 break;
01621 }
01622 if (i == 5) {
01623 av_log(avctx, AV_LOG_FATAL, "Ran out of free frames!\n");
01624 abort();
01625 }
01626 if (curframe->data[0])
01627 vp8_release_frame(s, curframe, 1, 0);
01628
01629
01630
01631
01632 if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
01633 !s->framep[VP56_FRAME_GOLDEN] ||
01634 !s->framep[VP56_FRAME_GOLDEN2])) {
01635 av_log(avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
01636 ret = AVERROR_INVALIDDATA;
01637 goto err;
01638 }
01639
01640 curframe->key_frame = s->keyframe;
01641 curframe->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
01642 curframe->reference = referenced ? 3 : 0;
01643 if ((ret = vp8_alloc_frame(s, curframe))) {
01644 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
01645 goto err;
01646 }
01647
01648
01649 if (s->update_altref != VP56_FRAME_NONE) {
01650 s->next_framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
01651 } else {
01652 s->next_framep[VP56_FRAME_GOLDEN2] = s->framep[VP56_FRAME_GOLDEN2];
01653 }
01654 if (s->update_golden != VP56_FRAME_NONE) {
01655 s->next_framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
01656 } else {
01657 s->next_framep[VP56_FRAME_GOLDEN] = s->framep[VP56_FRAME_GOLDEN];
01658 }
01659 if (s->update_last) {
01660 s->next_framep[VP56_FRAME_PREVIOUS] = curframe;
01661 } else {
01662 s->next_framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_PREVIOUS];
01663 }
01664 s->next_framep[VP56_FRAME_CURRENT] = curframe;
01665
01666 ff_thread_finish_setup(avctx);
01667
01668 s->linesize = curframe->linesize[0];
01669 s->uvlinesize = curframe->linesize[1];
01670
01671 if (!s->edge_emu_buffer)
01672 s->edge_emu_buffer = av_malloc(21*s->linesize);
01673
01674 memset(s->top_nnz, 0, s->mb_width*sizeof(*s->top_nnz));
01675
01676
01677 memset(s->macroblocks + s->mb_height*2 - 1, 0, (s->mb_width+1)*sizeof(*s->macroblocks));
01678
01679
01680 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
01681 s->top_border[0][15] = s->top_border[0][23] = 127;
01682 memset(s->top_border[1]-1, 127, s->mb_width*sizeof(*s->top_border)+1);
01683 }
01684 memset(s->ref_count, 0, sizeof(s->ref_count));
01685 if (s->keyframe)
01686 memset(s->intra4x4_pred_mode_top, DC_PRED, s->mb_width*4);
01687
01688 #define MARGIN (16 << 2)
01689 s->mv_min.y = -MARGIN;
01690 s->mv_max.y = ((s->mb_height - 1) << 6) + MARGIN;
01691
01692 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
01693 VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
01694 VP8Macroblock *mb = s->macroblocks + (s->mb_height - mb_y - 1)*2;
01695 int mb_xy = mb_y*s->mb_width;
01696 uint8_t *dst[3] = {
01697 curframe->data[0] + 16*mb_y*s->linesize,
01698 curframe->data[1] + 8*mb_y*s->uvlinesize,
01699 curframe->data[2] + 8*mb_y*s->uvlinesize
01700 };
01701
01702 memset(mb - 1, 0, sizeof(*mb));
01703 memset(s->left_nnz, 0, sizeof(s->left_nnz));
01704 AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED*0x01010101);
01705
01706
01707 if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
01708 for (i = 0; i < 3; i++)
01709 for (y = 0; y < 16>>!!i; y++)
01710 dst[i][y*curframe->linesize[i]-1] = 129;
01711 if (mb_y == 1)
01712 s->top_border[0][15] = s->top_border[0][23] = s->top_border[0][31] = 129;
01713 }
01714
01715 s->mv_min.x = -MARGIN;
01716 s->mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
01717 if (prev_frame && s->segmentation.enabled && !s->segmentation.update_map)
01718 ff_thread_await_progress(prev_frame, mb_y, 0);
01719
01720 for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
01721
01722 s->dsp.prefetch(dst[0] + (mb_x&3)*4*s->linesize + 64, s->linesize, 4);
01723 s->dsp.prefetch(dst[1] + (mb_x&7)*s->uvlinesize + 64, dst[2] - dst[1], 2);
01724
01725 decode_mb_mode(s, mb, mb_x, mb_y, curframe->ref_index[0] + mb_xy,
01726 prev_frame && prev_frame->ref_index[0] ? prev_frame->ref_index[0] + mb_xy : NULL);
01727
01728 prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_PREVIOUS);
01729
01730 if (!mb->skip)
01731 decode_mb_coeffs(s, c, mb, s->top_nnz[mb_x], s->left_nnz);
01732
01733 if (mb->mode <= MODE_I4x4)
01734 intra_predict(s, dst, mb, mb_x, mb_y);
01735 else
01736 inter_predict(s, dst, mb, mb_x, mb_y);
01737
01738 prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN);
01739
01740 if (!mb->skip) {
01741 idct_mb(s, dst, mb);
01742 } else {
01743 AV_ZERO64(s->left_nnz);
01744 AV_WN64(s->top_nnz[mb_x], 0);
01745
01746
01747 if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
01748 s->left_nnz[8] = 0;
01749 s->top_nnz[mb_x][8] = 0;
01750 }
01751 }
01752
01753 if (s->deblock_filter)
01754 filter_level_for_mb(s, mb, &s->filter_strength[mb_x]);
01755
01756 prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN2);
01757
01758 dst[0] += 16;
01759 dst[1] += 8;
01760 dst[2] += 8;
01761 s->mv_min.x -= 64;
01762 s->mv_max.x -= 64;
01763 }
01764 if (s->deblock_filter) {
01765 if (s->filter.simple)
01766 filter_mb_row_simple(s, curframe, mb_y);
01767 else
01768 filter_mb_row(s, curframe, mb_y);
01769 }
01770 s->mv_min.y -= 64;
01771 s->mv_max.y -= 64;
01772
01773 ff_thread_report_progress(curframe, mb_y, 0);
01774 }
01775
01776 ff_thread_report_progress(curframe, INT_MAX, 0);
01777 memcpy(&s->framep[0], &s->next_framep[0], sizeof(s->framep[0]) * 4);
01778
01779 skip_decode:
01780
01781
01782 if (!s->update_probabilities)
01783 s->prob[0] = s->prob[1];
01784
01785 if (!s->invisible) {
01786 *(AVFrame*)data = *curframe;
01787 *data_size = sizeof(AVFrame);
01788 }
01789
01790 return avpkt->size;
01791 err:
01792 memcpy(&s->next_framep[0], &s->framep[0], sizeof(s->framep[0]) * 4);
01793 return ret;
01794 }
01795
01796 static av_cold int vp8_decode_init(AVCodecContext *avctx)
01797 {
01798 VP8Context *s = avctx->priv_data;
01799
01800 s->avctx = avctx;
01801 avctx->pix_fmt = PIX_FMT_YUV420P;
01802
01803 ff_dsputil_init(&s->dsp, avctx);
01804 ff_h264_pred_init(&s->hpc, CODEC_ID_VP8, 8, 1);
01805 ff_vp8dsp_init(&s->vp8dsp);
01806
01807 return 0;
01808 }
01809
01810 static av_cold int vp8_decode_free(AVCodecContext *avctx)
01811 {
01812 vp8_decode_flush_impl(avctx, 0, 1, 1);
01813 release_queued_segmaps(avctx->priv_data, 1);
01814 return 0;
01815 }
01816
01817 static av_cold int vp8_decode_init_thread_copy(AVCodecContext *avctx)
01818 {
01819 VP8Context *s = avctx->priv_data;
01820
01821 s->avctx = avctx;
01822
01823 return 0;
01824 }
01825
01826 #define REBASE(pic) \
01827 pic ? pic - &s_src->frames[0] + &s->frames[0] : NULL
01828
01829 static int vp8_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
01830 {
01831 VP8Context *s = dst->priv_data, *s_src = src->priv_data;
01832
01833 if (s->macroblocks_base &&
01834 (s_src->mb_width != s->mb_width || s_src->mb_height != s->mb_height)) {
01835 free_buffers(s);
01836 s->maps_are_invalid = 1;
01837 s->mb_width = s_src->mb_width;
01838 s->mb_height = s_src->mb_height;
01839 }
01840
01841 s->prob[0] = s_src->prob[!s_src->update_probabilities];
01842 s->segmentation = s_src->segmentation;
01843 s->lf_delta = s_src->lf_delta;
01844 memcpy(s->sign_bias, s_src->sign_bias, sizeof(s->sign_bias));
01845
01846 memcpy(&s->frames, &s_src->frames, sizeof(s->frames));
01847 s->framep[0] = REBASE(s_src->next_framep[0]);
01848 s->framep[1] = REBASE(s_src->next_framep[1]);
01849 s->framep[2] = REBASE(s_src->next_framep[2]);
01850 s->framep[3] = REBASE(s_src->next_framep[3]);
01851
01852 return 0;
01853 }
01854
01855 AVCodec ff_vp8_decoder = {
01856 .name = "vp8",
01857 .type = AVMEDIA_TYPE_VIDEO,
01858 .id = CODEC_ID_VP8,
01859 .priv_data_size = sizeof(VP8Context),
01860 .init = vp8_decode_init,
01861 .close = vp8_decode_free,
01862 .decode = vp8_decode_frame,
01863 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
01864 .flush = vp8_decode_flush,
01865 .long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),
01866 .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp8_decode_init_thread_copy),
01867 .update_thread_context = ONLY_IF_THREADS_ENABLED(vp8_decode_update_thread_context),
01868 };