127
s->mb_width = (s->avctx->coded_width +15) / 16;
128
s->mb_height = (s->avctx->coded_height+15) / 16;
155
s->mb_width = (s->avctx->coded_width + 15) / 16;
156
s->mb_height = (s->avctx->coded_height + 15) / 16;
130
s->mb_layout = (avctx->active_thread_type == FF_THREAD_SLICE) && (FFMIN(s->num_coeff_partitions, avctx->thread_count) > 1);
158
s->mb_layout = is_vp7 || avctx->active_thread_type == FF_THREAD_SLICE &&
159
FFMIN(s->num_coeff_partitions, avctx->thread_count) > 1;
131
160
if (!s->mb_layout) { // Frame threading and one thread
132
s->macroblocks_base = av_mallocz((s->mb_width+s->mb_height*2+1)*sizeof(*s->macroblocks));
133
s->intra4x4_pred_mode_top = av_mallocz(s->mb_width*4);
135
else // Sliced threading
136
s->macroblocks_base = av_mallocz((s->mb_width+2)*(s->mb_height+2)*sizeof(*s->macroblocks));
137
s->top_nnz = av_mallocz(s->mb_width*sizeof(*s->top_nnz));
138
s->top_border = av_mallocz((s->mb_width+1)*sizeof(*s->top_border));
139
s->thread_data = av_mallocz(MAX_THREADS*sizeof(VP8ThreadData));
161
s->macroblocks_base = av_mallocz((s->mb_width + s->mb_height * 2 + 1) *
162
sizeof(*s->macroblocks));
163
s->intra4x4_pred_mode_top = av_mallocz(s->mb_width * 4);
164
} else // Sliced threading
165
s->macroblocks_base = av_mallocz((s->mb_width + 2) * (s->mb_height + 2) *
166
sizeof(*s->macroblocks));
167
s->top_nnz = av_mallocz(s->mb_width * sizeof(*s->top_nnz));
168
s->top_border = av_mallocz((s->mb_width + 1) * sizeof(*s->top_border));
169
s->thread_data = av_mallocz(MAX_THREADS * sizeof(VP8ThreadData));
141
171
for (i = 0; i < MAX_THREADS; i++) {
142
s->thread_data[i].filter_strength = av_mallocz(s->mb_width*sizeof(*s->thread_data[0].filter_strength));
172
s->thread_data[i].filter_strength =
173
av_mallocz(s->mb_width * sizeof(*s->thread_data[0].filter_strength));
144
175
pthread_mutex_init(&s->thread_data[i].lock, NULL);
145
176
pthread_cond_init(&s->thread_data[i].cond, NULL);
299
407
s->update_altref = ref_to_update(s, update_altref, VP56_FRAME_GOLDEN2);
302
static int decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
304
VP56RangeCoder *c = &s->c;
305
int header_size, hscale, vscale, i, j, k, l, m, ret;
410
static void copy_luma(AVFrame *dst, AVFrame *src, int width, int height)
414
for (j = 1; j < 3; j++) {
415
for (i = 0; i < height / 2; i++)
416
memcpy(dst->data[j] + i * dst->linesize[j],
417
src->data[j] + i * src->linesize[j], width / 2);
421
static void fade(uint8_t *dst, uint8_t *src,
422
int width, int height, int linesize,
427
for (j = 0; j < height; j++) {
428
for (i = 0; i < width; i++) {
429
uint8_t y = src[j * linesize + i];
430
dst[j * linesize + i] = av_clip_uint8(y + ((y * beta) >> 8) + alpha);
435
static int vp7_fade_frame(VP8Context *s, VP56RangeCoder *c)
437
int alpha = (int8_t) vp8_rac_get_uint(c, 8);
438
int beta = (int8_t) vp8_rac_get_uint(c, 8);
441
if (!s->keyframe && (alpha || beta)) {
442
int width = s->mb_width * 16;
443
int height = s->mb_height * 16;
446
if (!s->framep[VP56_FRAME_PREVIOUS])
447
return AVERROR_INVALIDDATA;
450
src = s->framep[VP56_FRAME_PREVIOUS]->tf.f;
452
/* preserve the golden frame, write a new previous frame */
453
if (s->framep[VP56_FRAME_GOLDEN] == s->framep[VP56_FRAME_PREVIOUS]) {
454
s->framep[VP56_FRAME_PREVIOUS] = vp8_find_free_buffer(s);
455
if ((ret = vp8_alloc_frame(s, s->framep[VP56_FRAME_PREVIOUS], 1)) < 0)
458
dst = s->framep[VP56_FRAME_PREVIOUS]->tf.f;
460
copy_luma(dst, src, width, height);
463
fade(dst->data[0], src->data[0],
464
width, height, dst->linesize[0], alpha, beta);
470
static int vp7_decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
472
VP56RangeCoder *c = &s->c;
473
int part1_size, hscale, vscale, i, j, ret;
474
int width = s->avctx->width;
475
int height = s->avctx->height;
477
s->profile = (buf[0] >> 1) & 7;
478
if (s->profile > 1) {
479
avpriv_request_sample(s->avctx, "Unknown profile %d", s->profile);
480
return AVERROR_INVALIDDATA;
483
s->keyframe = !(buf[0] & 1);
485
part1_size = AV_RL24(buf) >> 4;
487
buf += 4 - s->profile;
488
buf_size -= 4 - s->profile;
490
memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
492
ff_vp56_init_range_decoder(c, buf, part1_size);
494
buf_size -= part1_size;
496
/* A. Dimension information (keyframes only) */
498
width = vp8_rac_get_uint(c, 12);
499
height = vp8_rac_get_uint(c, 12);
500
hscale = vp8_rac_get_uint(c, 2);
501
vscale = vp8_rac_get_uint(c, 2);
502
if (hscale || vscale)
503
avpriv_request_sample(s->avctx, "Upscaling");
505
s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
506
vp78_reset_probability_tables(s);
507
memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter,
508
sizeof(s->prob->pred16x16));
509
memcpy(s->prob->pred8x8c, vp8_pred8x8c_prob_inter,
510
sizeof(s->prob->pred8x8c));
511
for (i = 0; i < 2; i++)
512
memcpy(s->prob->mvc[i], vp7_mv_default_prob[i],
513
sizeof(vp7_mv_default_prob[i]));
514
memset(&s->segmentation, 0, sizeof(s->segmentation));
515
memset(&s->lf_delta, 0, sizeof(s->lf_delta));
516
memcpy(s->prob[0].scan, zigzag_scan, sizeof(s->prob[0].scan));
519
if (s->keyframe || s->profile > 0)
520
memset(s->inter_dc_pred, 0 , sizeof(s->inter_dc_pred));
522
/* B. Decoding information for all four macroblock-level features */
523
for (i = 0; i < 4; i++) {
524
s->feature_enabled[i] = vp8_rac_get(c);
525
if (s->feature_enabled[i]) {
526
s->feature_present_prob[i] = vp8_rac_get_uint(c, 8);
528
for (j = 0; j < 3; j++)
529
s->feature_index_prob[i][j] =
530
vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
532
if (vp7_feature_value_size[i])
533
for (j = 0; j < 4; j++)
534
s->feature_value[i][j] =
535
vp8_rac_get(c) ? vp8_rac_get_uint(c, vp7_feature_value_size[s->profile][i]) : 0;
539
s->segmentation.enabled = 0;
540
s->segmentation.update_map = 0;
541
s->lf_delta.enabled = 0;
543
s->num_coeff_partitions = 1;
544
ff_vp56_init_range_decoder(&s->coeff_partition[0], buf, buf_size);
546
if (!s->macroblocks_base || /* first frame */
547
width != s->avctx->width || height != s->avctx->height ||
548
(width + 15) / 16 != s->mb_width || (height + 15) / 16 != s->mb_height) {
549
if ((ret = vp7_update_dimensions(s, width, height)) < 0)
553
/* C. Dequantization indices */
556
/* D. Golden frame update flag (a Flag) for interframes only */
558
s->update_golden = vp8_rac_get(c) ? VP56_FRAME_CURRENT : VP56_FRAME_NONE;
559
s->sign_bias[VP56_FRAME_GOLDEN] = 0;
563
s->update_probabilities = 1;
566
if (s->profile > 0) {
567
s->update_probabilities = vp8_rac_get(c);
568
if (!s->update_probabilities)
569
s->prob[1] = s->prob[0];
572
s->fade_present = vp8_rac_get(c);
575
/* E. Fading information for previous frame */
576
if (s->fade_present && vp8_rac_get(c)) {
577
if ((ret = vp7_fade_frame(s ,c)) < 0)
581
/* F. Loop filter type */
583
s->filter.simple = vp8_rac_get(c);
585
/* G. DCT coefficient ordering specification */
587
for (i = 1; i < 16; i++)
588
s->prob[0].scan[i] = zigzag_scan[vp8_rac_get_uint(c, 4)];
590
/* H. Loop filter levels */
592
s->filter.simple = vp8_rac_get(c);
593
s->filter.level = vp8_rac_get_uint(c, 6);
594
s->filter.sharpness = vp8_rac_get_uint(c, 3);
596
/* I. DCT coefficient probability update; 13.3 Token Probability Updates */
597
vp78_update_probability_tables(s);
599
s->mbskip_enabled = 0;
601
/* J. The remaining frame header data occurs ONLY FOR INTERFRAMES */
603
s->prob->intra = vp8_rac_get_uint(c, 8);
604
s->prob->last = vp8_rac_get_uint(c, 8);
605
vp78_update_pred16x16_pred8x8_mvc_probabilities(s, VP7_MVC_SIZE);
611
static int vp8_decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
613
VP56RangeCoder *c = &s->c;
614
int header_size, hscale, vscale, ret;
306
615
int width = s->avctx->width;
307
616
int height = s->avctx->height;
317
626
av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
320
memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
629
memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab,
630
sizeof(s->put_pixels_tab));
321
631
else // profile 1-3 use bilinear, 4+ aren't defined so whatever
322
memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab, sizeof(s->put_pixels_tab));
632
memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab,
633
sizeof(s->put_pixels_tab));
324
if (header_size > buf_size - 7*s->keyframe) {
635
if (header_size > buf_size - 7 * s->keyframe) {
325
636
av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
326
637
return AVERROR_INVALIDDATA;
329
640
if (s->keyframe) {
330
641
if (AV_RL24(buf) != 0x2a019d) {
331
av_log(s->avctx, AV_LOG_ERROR, "Invalid start code 0x%x\n", AV_RL24(buf));
642
av_log(s->avctx, AV_LOG_ERROR,
643
"Invalid start code 0x%x\n", AV_RL24(buf));
332
644
return AVERROR_INVALIDDATA;
334
width = AV_RL16(buf+3) & 0x3fff;
335
height = AV_RL16(buf+5) & 0x3fff;
336
hscale = buf[4] >> 6;
337
vscale = buf[6] >> 6;
646
width = AV_RL16(buf + 3) & 0x3fff;
647
height = AV_RL16(buf + 5) & 0x3fff;
648
hscale = buf[4] >> 6;
649
vscale = buf[6] >> 6;
562
static av_always_inline
563
void decode_mvs(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int layout)
565
VP8Macroblock *mb_edge[3] = { 0 /* top */,
859
* The vp7 reference decoder uses a padding macroblock column (added to right
860
* edge of the frame) to guard against illegal macroblock offsets. The
861
* algorithm has bugs that permit offsets to straddle the padding column.
862
* This function replicates those bugs.
864
* @param[out] edge_x macroblock x address
865
* @param[out] edge_y macroblock y address
867
* @return macroblock offset legal (boolean)
869
static int vp7_calculate_mb_offset(int mb_x, int mb_y, int mb_width,
870
int xoffset, int yoffset, int boundary,
871
int *edge_x, int *edge_y)
873
int vwidth = mb_width + 1;
874
int new = (mb_y + yoffset) * vwidth + mb_x + xoffset;
875
if (new < boundary || new % vwidth == vwidth - 1)
877
*edge_y = new / vwidth;
878
*edge_x = new % vwidth;
882
static const VP56mv *get_bmv_ptr(const VP8Macroblock *mb, int subblock)
884
return &mb->bmv[mb->mode == VP8_MVMODE_SPLIT ? vp8_mbsplits[mb->partitioning][subblock] : 0];
887
static av_always_inline
888
void vp7_decode_mvs(VP8Context *s, VP8Macroblock *mb,
889
int mb_x, int mb_y, int layout)
891
VP8Macroblock *mb_edge[12];
892
enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR };
893
enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT };
896
uint8_t cnt[3] = { 0 };
897
VP56RangeCoder *c = &s->c;
900
AV_ZERO32(&near_mv[0]);
901
AV_ZERO32(&near_mv[1]);
902
AV_ZERO32(&near_mv[2]);
904
for (i = 0; i < VP7_MV_PRED_COUNT; i++) {
905
const VP7MVPred * pred = &vp7_mv_pred[i];
908
if (vp7_calculate_mb_offset(mb_x, mb_y, s->mb_width, pred->xoffset,
909
pred->yoffset, !s->profile, &edge_x, &edge_y)) {
910
VP8Macroblock *edge = mb_edge[i] = (s->mb_layout == 1)
911
? s->macroblocks_base + 1 + edge_x +
912
(s->mb_width + 1) * (edge_y + 1)
913
: s->macroblocks + edge_x +
914
(s->mb_height - edge_y - 1) * 2;
915
uint32_t mv = AV_RN32A(get_bmv_ptr(edge, vp7_mv_pred[i].subblock));
917
if (AV_RN32A(&near_mv[CNT_NEAREST])) {
918
if (mv == AV_RN32A(&near_mv[CNT_NEAREST])) {
920
} else if (AV_RN32A(&near_mv[CNT_NEAR])) {
921
if (mv != AV_RN32A(&near_mv[CNT_NEAR]))
925
AV_WN32A(&near_mv[CNT_NEAR], mv);
929
AV_WN32A(&near_mv[CNT_NEAREST], mv);
938
cnt[idx] += vp7_mv_pred[i].score;
941
mb->partitioning = VP8_SPLITMVMODE_NONE;
943
if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_ZERO]][0])) {
944
mb->mode = VP8_MVMODE_MV;
946
if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAREST]][1])) {
948
if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAR]][2])) {
950
if (cnt[CNT_NEAREST] > cnt[CNT_NEAR])
951
AV_WN32A(&mb->mv, cnt[CNT_ZERO] > cnt[CNT_NEAREST] ? 0 : AV_RN32A(&near_mv[CNT_NEAREST]));
953
AV_WN32A(&mb->mv, cnt[CNT_ZERO] > cnt[CNT_NEAR] ? 0 : AV_RN32A(&near_mv[CNT_NEAR]));
955
if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAR]][3])) {
956
mb->mode = VP8_MVMODE_SPLIT;
957
mb->mv = mb->bmv[decode_splitmvs(s, c, mb, layout, IS_VP7) - 1];
959
mb->mv.y += read_mv_component(c, s->prob->mvc[0], IS_VP7);
960
mb->mv.x += read_mv_component(c, s->prob->mvc[1], IS_VP7);
964
mb->mv = near_mv[CNT_NEAR];
968
mb->mv = near_mv[CNT_NEAREST];
972
mb->mode = VP8_MVMODE_ZERO;
978
static av_always_inline
979
void vp8_decode_mvs(VP8Context *s, VP8Macroblock *mb,
980
int mb_x, int mb_y, int layout)
982
VP8Macroblock *mb_edge[3] = { 0 /* top */,
566
983
mb - 1 /* left */,
568
985
enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
569
986
enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT };
570
987
int idx = CNT_ZERO;
679
1096
for (y = 0; y < 4; y++) {
680
1097
for (x = 0; x < 4; x++) {
681
1098
const uint8_t *ctx;
682
ctx = vp8_pred4x4_prob_intra[top[x]][left[y]];
1099
ctx = vp8_pred4x4_prob_intra[top[x]][left[y]];
683
1100
*intra4x4 = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
684
left[y] = top[x] = *intra4x4;
1101
left[y] = top[x] = *intra4x4;
690
1107
for (i = 0; i < 16; i++)
691
intra4x4[i] = vp8_rac_get_tree(c, vp8_pred4x4_tree, vp8_pred4x4_prob_inter);
1108
intra4x4[i] = vp8_rac_get_tree(c, vp8_pred4x4_tree,
1109
vp8_pred4x4_prob_inter);
695
1113
static av_always_inline
696
1114
void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
697
uint8_t *segment, uint8_t *ref, int layout)
1115
uint8_t *segment, uint8_t *ref, int layout, int is_vp7)
699
1117
VP56RangeCoder *c = &s->c;
701
if (s->segmentation.update_map)
1118
const char *vp7_feature_name[] = { "q-index",
1120
"partial-golden-update",
1125
for (i = 0; i < 4; i++) {
1126
if (s->feature_enabled[i]) {
1127
if (vp56_rac_get_prob(c, s->feature_present_prob[i])) {
1128
int index = vp8_rac_get_tree(c, vp7_feature_index_tree,
1129
s->feature_index_prob[i]);
1130
av_log(s->avctx, AV_LOG_WARNING,
1131
"Feature %s present in macroblock (value 0x%x)\n",
1132
vp7_feature_name[i], s->feature_value[i][index]);
1136
} else if (s->segmentation.update_map)
702
1137
*segment = vp8_rac_get_tree(c, vp8_segmentid_tree, s->prob->segmentid);
703
1138
else if (s->segmentation.enabled)
704
1139
*segment = ref ? *ref : *segment;
707
1142
mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
709
1144
if (s->keyframe) {
710
mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra, vp8_pred16x16_prob_intra);
1145
mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra,
1146
vp8_pred16x16_prob_intra);
712
1148
if (mb->mode == MODE_I4x4) {
713
1149
decode_intra4x4_modes(s, c, mb, mb_x, 1, layout);
715
const uint32_t modes = vp8_pred4x4_mode[mb->mode] * 0x01010101u;
1151
const uint32_t modes = (is_vp7 ? vp7_pred4x4_mode
1152
: vp8_pred4x4_mode)[mb->mode] * 0x01010101u;
716
1153
if (s->mb_layout == 1)
717
1154
AV_WN32A(mb->intra4x4_pred_mode_top, modes);
719
1156
AV_WN32A(s->intra4x4_pred_mode_top + 4 * mb_x, modes);
720
AV_WN32A( s->intra4x4_pred_mode_left, modes);
1157
AV_WN32A(s->intra4x4_pred_mode_left, modes);
723
mb->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, vp8_pred8x8c_prob_intra);
724
mb->ref_frame = VP56_FRAME_CURRENT;
1160
mb->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree,
1161
vp8_pred8x8c_prob_intra);
1162
mb->ref_frame = VP56_FRAME_CURRENT;
725
1163
} else if (vp56_rac_get_prob_branchy(c, s->prob->intra)) {
726
1164
// inter MB, 16.2
727
1165
if (vp56_rac_get_prob_branchy(c, s->prob->last))
728
mb->ref_frame = vp56_rac_get_prob(c, s->prob->golden) ?
729
VP56_FRAME_GOLDEN2 /* altref */ : VP56_FRAME_GOLDEN;
1167
(!is_vp7 && vp56_rac_get_prob(c, s->prob->golden)) ? VP56_FRAME_GOLDEN2 /* altref */
1168
: VP56_FRAME_GOLDEN;
731
1170
mb->ref_frame = VP56_FRAME_PREVIOUS;
732
s->ref_count[mb->ref_frame-1]++;
1171
s->ref_count[mb->ref_frame - 1]++;
734
1173
// motion vectors, 16.3
735
decode_mvs(s, mb, mb_x, mb_y, layout);
1175
vp7_decode_mvs(s, mb, mb_x, mb_y, layout);
1177
vp8_decode_mvs(s, mb, mb_x, mb_y, layout);
737
1179
// intra MB, 16.1
738
1180
mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_inter, s->prob->pred16x16);
740
1182
if (mb->mode == MODE_I4x4)
741
1183
decode_intra4x4_modes(s, c, mb, mb_x, 0, layout);
743
mb->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree, s->prob->pred8x8c);
744
mb->ref_frame = VP56_FRAME_CURRENT;
745
mb->partitioning = VP8_SPLITMVMODE_NONE;
1185
mb->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree,
1187
mb->ref_frame = VP56_FRAME_CURRENT;
1188
mb->partitioning = VP8_SPLITMVMODE_NONE;
746
1189
AV_ZERO32(&mb->bmv[0]);
750
#ifndef decode_block_coeffs_internal
752
* @param r arithmetic bitstream reader context
1194
* @param r arithmetic bitstream reader context
753
1195
* @param block destination for block coefficients
754
1196
* @param probs probabilities to use when reading trees from the bitstream
755
* @param i initial coeff index, 0 unless a separate DC block is coded
756
* @param qmul array holding the dc/ac dequant factor at position 0/1
1197
* @param i initial coeff index, 0 unless a separate DC block is coded
1198
* @param qmul array holding the dc/ac dequant factor at position 0/1
757
1200
* @return 0 if no coeffs were decoded
758
1201
* otherwise, the index of the last coeff decoded plus one
760
static int decode_block_coeffs_internal(VP56RangeCoder *r, int16_t block[16],
761
uint8_t probs[16][3][NUM_DCT_TOKENS-1],
762
int i, uint8_t *token_prob, int16_t qmul[2])
1203
static av_always_inline
1204
int decode_block_coeffs_internal(VP56RangeCoder *r, int16_t block[16],
1205
uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
1206
int i, uint8_t *token_prob, int16_t qmul[2],
1207
const uint8_t scan[16], int vp7)
764
1209
VP56RangeCoder c = *r;
768
1214
if (!vp56_rac_get_prob_branchy(&c, token_prob[0])) // DCT_EOB
790
1238
if (!vp56_rac_get_prob_branchy(&c, token_prob[6])) {
791
1239
if (!vp56_rac_get_prob_branchy(&c, token_prob[7])) { // DCT_CAT1
792
coeff = 5 + vp56_rac_get_prob(&c, vp8_dct_cat1_prob[0]);
1240
coeff = 5 + vp56_rac_get_prob(&c, vp8_dct_cat1_prob[0]);
793
1241
} else { // DCT_CAT2
795
1243
coeff += vp56_rac_get_prob(&c, vp8_dct_cat2_prob[0]) << 1;
796
1244
coeff += vp56_rac_get_prob(&c, vp8_dct_cat2_prob[1]);
798
1246
} else { // DCT_CAT3 and up
799
int a = vp56_rac_get_prob(&c, token_prob[8]);
800
int b = vp56_rac_get_prob(&c, token_prob[9+a]);
801
int cat = (a<<1) + b;
802
coeff = 3 + (8<<cat);
1247
int a = vp56_rac_get_prob(&c, token_prob[8]);
1248
int b = vp56_rac_get_prob(&c, token_prob[9 + a]);
1249
int cat = (a << 1) + b;
1250
coeff = 3 + (8 << cat);
803
1251
coeff += vp8_rac_get_coeff(&c, ff_vp8_dct_cat_prob[cat]);
806
token_prob = probs[i+1][2];
1254
token_prob = probs[i + 1][2];
808
block[zigzag_scan[i]] = (vp8_rac_get(&c) ? -coeff : coeff) * qmul[!!i];
1256
block[scan[i]] = (vp8_rac_get(&c) ? -coeff : coeff) * qmul[!!i];
809
1257
} while (++i < 16);
1263
static av_always_inline
1264
int inter_predict_dc(int16_t block[16], int16_t pred[2])
1266
int16_t dc = block[0];
1274
if (!pred[0] | !dc | ((int32_t)pred[0] ^ (int32_t)dc) >> 31) {
1275
block[0] = pred[0] = dc;
1280
block[0] = pred[0] = dc;
1286
static int vp7_decode_block_coeffs_internal(VP56RangeCoder *r,
1288
uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
1289
int i, uint8_t *token_prob,
1291
const uint8_t scan[16])
1293
return decode_block_coeffs_internal(r, block, probs, i,
1294
token_prob, qmul, scan, IS_VP7);
1297
#ifndef vp8_decode_block_coeffs_internal
1298
static int vp8_decode_block_coeffs_internal(VP56RangeCoder *r,
1300
uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
1301
int i, uint8_t *token_prob,
1304
return decode_block_coeffs_internal(r, block, probs, i,
1305
token_prob, qmul, zigzag_scan, IS_VP8);
817
* @param c arithmetic bitstream reader context
818
* @param block destination for block coefficients
819
* @param probs probabilities to use when reading trees from the bitstream
820
* @param i initial coeff index, 0 unless a separate DC block is coded
1310
* @param c arithmetic bitstream reader context
1311
* @param block destination for block coefficients
1312
* @param probs probabilities to use when reading trees from the bitstream
1313
* @param i initial coeff index, 0 unless a separate DC block is coded
821
1314
* @param zero_nhood the initial prediction context for number of surrounding
822
1315
* all-zero blocks (only left/top, so 0-2)
823
* @param qmul array holding the dc/ac dequant factor at position 0/1
1316
* @param qmul array holding the dc/ac dequant factor at position 0/1
824
1318
* @return 0 if no coeffs were decoded
825
1319
* otherwise, the index of the last coeff decoded plus one
827
1321
static av_always_inline
828
1322
int decode_block_coeffs(VP56RangeCoder *c, int16_t block[16],
829
uint8_t probs[16][3][NUM_DCT_TOKENS-1],
830
int i, int zero_nhood, int16_t qmul[2])
1323
uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
1324
int i, int zero_nhood, int16_t qmul[2],
1325
const uint8_t scan[16], int vp7)
832
1327
uint8_t *token_prob = probs[i][zero_nhood];
833
1328
if (!vp56_rac_get_prob_branchy(c, token_prob[0])) // DCT_EOB
835
return decode_block_coeffs_internal(c, block, probs, i, token_prob, qmul);
1330
return vp7 ? vp7_decode_block_coeffs_internal(c, block, probs, i,
1331
token_prob, qmul, scan)
1332
: vp8_decode_block_coeffs_internal(c, block, probs, i,
838
1336
static av_always_inline
839
void decode_mb_coeffs(VP8Context *s, VP8ThreadData *td, VP56RangeCoder *c, VP8Macroblock *mb,
840
uint8_t t_nnz[9], uint8_t l_nnz[9])
1337
void decode_mb_coeffs(VP8Context *s, VP8ThreadData *td, VP56RangeCoder *c,
1338
VP8Macroblock *mb, uint8_t t_nnz[9], uint8_t l_nnz[9],
842
1341
int i, x, y, luma_start = 0, luma_ctx = 3;
843
1342
int nnz_pred, nnz, nnz_total = 0;
844
1343
int segment = mb->segment;
845
1344
int block_dc = 0;
847
if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
1346
if (mb->mode != MODE_I4x4 && (is_vp7 || mb->mode != VP8_MVMODE_SPLIT)) {
848
1347
nnz_pred = t_nnz[8] + l_nnz[8];
850
1349
// decode DC values and do hadamard
851
nnz = decode_block_coeffs(c, td->block_dc, s->prob->token[1], 0, nnz_pred,
852
s->qmat[segment].luma_dc_qmul);
1350
nnz = decode_block_coeffs(c, td->block_dc, s->prob->token[1], 0,
1351
nnz_pred, s->qmat[segment].luma_dc_qmul,
1352
zigzag_scan, is_vp7);
853
1353
l_nnz[8] = t_nnz[8] = !!nnz;
1355
if (is_vp7 && mb->mode > MODE_I4x4) {
1356
nnz |= inter_predict_dc(td->block_dc,
1357
s->inter_dc_pred[mb->ref_frame - 1]);
855
1361
nnz_total += nnz;
858
1364
s->vp8dsp.vp8_luma_dc_wht_dc(td->block, td->block_dc);
860
1366
s->vp8dsp.vp8_luma_dc_wht(td->block, td->block_dc);
867
1373
for (y = 0; y < 4; y++)
868
1374
for (x = 0; x < 4; x++) {
869
1375
nnz_pred = l_nnz[y] + t_nnz[x];
870
nnz = decode_block_coeffs(c, td->block[y][x], s->prob->token[luma_ctx], luma_start,
871
nnz_pred, s->qmat[segment].luma_qmul);
872
// nnz+block_dc may be one more than the actual last index, but we don't care
1376
nnz = decode_block_coeffs(c, td->block[y][x],
1377
s->prob->token[luma_ctx],
1378
luma_start, nnz_pred,
1379
s->qmat[segment].luma_qmul,
1380
s->prob[0].scan, is_vp7);
1381
/* nnz+block_dc may be one more than the actual last index,
1382
* but we don't care */
873
1383
td->non_zero_count_cache[y][x] = nnz + block_dc;
874
1384
t_nnz[x] = l_nnz[y] = !!nnz;
875
1385
nnz_total += nnz;
899
1411
static av_always_inline
900
void backup_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
1412
void backup_mb_border(uint8_t *top_border, uint8_t *src_y,
1413
uint8_t *src_cb, uint8_t *src_cr,
901
1414
int linesize, int uvlinesize, int simple)
903
AV_COPY128(top_border, src_y + 15*linesize);
1416
AV_COPY128(top_border, src_y + 15 * linesize);
905
AV_COPY64(top_border+16, src_cb + 7*uvlinesize);
906
AV_COPY64(top_border+24, src_cr + 7*uvlinesize);
1418
AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
1419
AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
910
1423
static av_always_inline
911
void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr,
912
int linesize, int uvlinesize, int mb_x, int mb_y, int mb_width,
913
int simple, int xchg)
1424
void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb,
1425
uint8_t *src_cr, int linesize, int uvlinesize, int mb_x,
1426
int mb_y, int mb_width, int simple, int xchg)
915
uint8_t *top_border_m1 = top_border-32; // for TL prediction
1428
uint8_t *top_border_m1 = top_border - 32; // for TL prediction
917
1430
src_cb -= uvlinesize;
918
1431
src_cr -= uvlinesize;
920
#define XCHG(a,b,xchg) do { \
921
if (xchg) AV_SWAP64(b,a); \
922
else AV_COPY64(b,a); \
1433
#define XCHG(a, b, xchg) \
925
XCHG(top_border_m1+8, src_y-8, xchg);
926
XCHG(top_border, src_y, xchg);
927
XCHG(top_border+8, src_y+8, 1);
928
if (mb_x < mb_width-1)
929
XCHG(top_border+32, src_y+16, 1);
1441
XCHG(top_border_m1 + 8, src_y - 8, xchg);
1442
XCHG(top_border, src_y, xchg);
1443
XCHG(top_border + 8, src_y + 8, 1);
1444
if (mb_x < mb_width - 1)
1445
XCHG(top_border + 32, src_y + 16, 1);
931
1447
// only copy chroma for normal loop filter
932
1448
// or to initialize the top row to 127
933
1449
if (!simple || !mb_y) {
934
XCHG(top_border_m1+16, src_cb-8, xchg);
935
XCHG(top_border_m1+24, src_cr-8, xchg);
936
XCHG(top_border+16, src_cb, 1);
937
XCHG(top_border+24, src_cr, 1);
1450
XCHG(top_border_m1 + 16, src_cb - 8, xchg);
1451
XCHG(top_border_m1 + 24, src_cr - 8, xchg);
1452
XCHG(top_border + 16, src_cb, 1);
1453
XCHG(top_border + 24, src_cr, 1);
941
1457
static av_always_inline
942
1458
int check_dc_pred8x8_mode(int mode, int mb_x, int mb_y)
945
1461
return mb_y ? TOP_DC_PRED8x8 : DC_128_PRED8x8;
947
1463
return mb_y ? mode : LEFT_DC_PRED8x8;
951
1466
static av_always_inline
952
int check_tm_pred8x8_mode(int mode, int mb_x, int mb_y)
1467
int check_tm_pred8x8_mode(int mode, int mb_x, int mb_y, int vp7)
955
return mb_y ? VERT_PRED8x8 : DC_129_PRED8x8;
1470
return mb_y ? VERT_PRED8x8 : (vp7 ? DC_128_PRED8x8 : DC_129_PRED8x8);
957
1472
return mb_y ? mode : HOR_PRED8x8;
961
1475
static av_always_inline
962
int check_intra_pred8x8_mode_emuedge(int mode, int mb_x, int mb_y)
1476
int check_intra_pred8x8_mode_emuedge(int mode, int mb_x, int mb_y, int vp7)
965
1479
case DC_PRED8x8:
966
1480
return check_dc_pred8x8_mode(mode, mb_x, mb_y);
967
1481
case VERT_PRED8x8:
968
return !mb_y ? DC_127_PRED8x8 : mode;
1482
return !mb_y ? (vp7 ? DC_128_PRED8x8 : DC_127_PRED8x8) : mode;
969
1483
case HOR_PRED8x8:
970
return !mb_x ? DC_129_PRED8x8 : mode;
971
case PLANE_PRED8x8 /*TM*/:
972
return check_tm_pred8x8_mode(mode, mb_x, mb_y);
1484
return !mb_x ? (vp7 ? DC_128_PRED8x8 : DC_129_PRED8x8) : mode;
1485
case PLANE_PRED8x8: /* TM */
1486
return check_tm_pred8x8_mode(mode, mb_x, mb_y, vp7);
977
1491
static av_always_inline
978
int check_tm_pred4x4_mode(int mode, int mb_x, int mb_y)
1492
int check_tm_pred4x4_mode(int mode, int mb_x, int mb_y, int vp7)
981
return mb_y ? VERT_VP8_PRED : DC_129_PRED;
1495
return mb_y ? VERT_VP8_PRED : (vp7 ? DC_128_PRED : DC_129_PRED);
983
1497
return mb_y ? mode : HOR_VP8_PRED;
987
1501
static av_always_inline
988
int check_intra_pred4x4_mode_emuedge(int mode, int mb_x, int mb_y, int *copy_buf)
1502
int check_intra_pred4x4_mode_emuedge(int mode, int mb_x, int mb_y,
1503
int *copy_buf, int vp7)
1021
1537
static av_always_inline
1022
1538
void intra_predict(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3],
1023
VP8Macroblock *mb, int mb_x, int mb_y)
1539
VP8Macroblock *mb, int mb_x, int mb_y, int is_vp7)
1025
1541
int x, y, mode, nnz;
1028
// for the first row, we need to run xchg_mb_border to init the top edge to 127
1029
// otherwise, skip it if we aren't going to deblock
1544
/* for the first row, we need to run xchg_mb_border to init the top edge
1545
* to 127 otherwise, skip it if we aren't going to deblock */
1030
1546
if (mb_y && (s->deblock_filter || !mb_y) && td->thread_nr == 0)
1031
xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
1547
xchg_mb_border(s->top_border[mb_x + 1], dst[0], dst[1], dst[2],
1032
1548
s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
1033
1549
s->filter.simple, 1);
1035
1551
if (mb->mode < MODE_I4x4) {
1036
mode = check_intra_pred8x8_mode_emuedge(mb->mode, mb_x, mb_y);
1552
mode = check_intra_pred8x8_mode_emuedge(mb->mode, mb_x, mb_y, is_vp7);
1037
1553
s->hpc.pred16x16[mode](dst[0], s->linesize);
1039
1555
uint8_t *ptr = dst[0];
1040
1556
uint8_t *intra4x4 = mb->intra4x4_pred_mode_mb;
1041
uint8_t tr_top[4] = { 127, 127, 127, 127 };
1557
const uint8_t lo = is_vp7 ? 128 : 127;
1558
const uint8_t hi = is_vp7 ? 128 : 129;
1559
uint8_t tr_top[4] = { lo, lo, lo, lo };
1043
1561
// all blocks on the right edge of the macroblock use bottom edge
1044
1562
// the top macroblock for their topright edge
1059
1576
uint8_t *topright = ptr + 4 - s->linesize;
1060
1577
for (x = 0; x < 4; x++) {
1061
1578
int copy = 0, linesize = s->linesize;
1062
uint8_t *dst = ptr+4*x;
1063
DECLARE_ALIGNED(4, uint8_t, copy_dst)[5*8];
1579
uint8_t *dst = ptr + 4 * x;
1580
DECLARE_ALIGNED(4, uint8_t, copy_dst)[5 * 8];
1065
1582
if ((y == 0 || x == 3) && mb_y == 0) {
1066
1583
topright = tr_top;
1067
1584
} else if (x == 3)
1068
1585
topright = tr_right;
1070
mode = check_intra_pred4x4_mode_emuedge(intra4x4[x], mb_x + x, mb_y + y, ©);
1587
mode = check_intra_pred4x4_mode_emuedge(intra4x4[x], mb_x + x,
1588
mb_y + y, ©, is_vp7);
1072
dst = copy_dst + 12;
1590
dst = copy_dst + 12;
1074
1592
if (!(mb_y + y)) {
1076
AV_WN32A(copy_dst+4, 127U * 0x01010101U);
1594
AV_WN32A(copy_dst + 4, lo * 0x01010101U);
1078
AV_COPY32(copy_dst+4, ptr+4*x-s->linesize);
1596
AV_COPY32(copy_dst + 4, ptr + 4 * x - s->linesize);
1079
1597
if (!(mb_x + x)) {
1082
copy_dst[3] = ptr[4*x-s->linesize-1];
1600
copy_dst[3] = ptr[4 * x - s->linesize - 1];
1085
1603
if (!(mb_x + x)) {
1089
copy_dst[35] = 129U;
1091
copy_dst[11] = ptr[4*x -1];
1092
copy_dst[19] = ptr[4*x+s->linesize -1];
1093
copy_dst[27] = ptr[4*x+s->linesize*2-1];
1094
copy_dst[35] = ptr[4*x+s->linesize*3-1];
1609
copy_dst[11] = ptr[4 * x - 1];
1610
copy_dst[19] = ptr[4 * x + s->linesize - 1];
1611
copy_dst[27] = ptr[4 * x + s->linesize * 2 - 1];
1612
copy_dst[35] = ptr[4 * x + s->linesize * 3 - 1];
1097
1615
s->hpc.pred4x4[mode](dst, topright, linesize);
1099
AV_COPY32(ptr+4*x , copy_dst+12);
1100
AV_COPY32(ptr+4*x+s->linesize , copy_dst+20);
1101
AV_COPY32(ptr+4*x+s->linesize*2, copy_dst+28);
1102
AV_COPY32(ptr+4*x+s->linesize*3, copy_dst+36);
1617
AV_COPY32(ptr + 4 * x, copy_dst + 12);
1618
AV_COPY32(ptr + 4 * x + s->linesize, copy_dst + 20);
1619
AV_COPY32(ptr + 4 * x + s->linesize * 2, copy_dst + 28);
1620
AV_COPY32(ptr + 4 * x + s->linesize * 3, copy_dst + 36);
1105
1623
nnz = td->non_zero_count_cache[y][x];
1108
s->vp8dsp.vp8_idct_dc_add(ptr+4*x, td->block[y][x], s->linesize);
1626
s->vp8dsp.vp8_idct_dc_add(ptr + 4 * x,
1627
td->block[y][x], s->linesize);
1110
s->vp8dsp.vp8_idct_add(ptr+4*x, td->block[y][x], s->linesize);
1629
s->vp8dsp.vp8_idct_add(ptr + 4 * x,
1630
td->block[y][x], s->linesize);
1115
ptr += 4*s->linesize;
1635
ptr += 4 * s->linesize;
1120
mode = check_intra_pred8x8_mode_emuedge(mb->chroma_pred_mode, mb_x, mb_y);
1640
mode = check_intra_pred8x8_mode_emuedge(mb->chroma_pred_mode,
1641
mb_x, mb_y, is_vp7);
1121
1642
s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
1122
1643
s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
1124
1645
if (mb_y && (s->deblock_filter || !mb_y) && td->thread_nr == 0)
1125
xchg_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2],
1646
xchg_mb_border(s->top_border[mb_x + 1], dst[0], dst[1], dst[2],
1126
1647
s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
1127
1648
s->filter.simple, 0);
1138
1659
* luma MC function
1140
* @param s VP8 decoding context
1141
* @param dst target buffer for block data at block position
1142
* @param ref reference picture buffer at origin (0, 0)
1143
* @param mv motion vector (relative to block position) to get pixel data from
1144
* @param x_off horizontal position of block from origin (0, 0)
1145
* @param y_off vertical position of block from origin (0, 0)
1146
* @param block_w width of block (16, 8 or 4)
1147
* @param block_h height of block (always same as block_w)
1148
* @param width width of src/dst plane data
1149
* @param height height of src/dst plane data
1661
* @param s VP8 decoding context
1662
* @param dst target buffer for block data at block position
1663
* @param ref reference picture buffer at origin (0, 0)
1664
* @param mv motion vector (relative to block position) to get pixel data from
1665
* @param x_off horizontal position of block from origin (0, 0)
1666
* @param y_off vertical position of block from origin (0, 0)
1667
* @param block_w width of block (16, 8 or 4)
1668
* @param block_h height of block (always same as block_w)
1669
* @param width width of src/dst plane data
1670
* @param height height of src/dst plane data
1150
1671
* @param linesize size of a single line of plane data, including padding
1151
* @param mc_func motion compensation function pointers (bilinear or sixtap MC)
1672
* @param mc_func motion compensation function pointers (bilinear or sixtap MC)
1153
1674
static av_always_inline
1154
1675
void vp8_mc_luma(VP8Context *s, VP8ThreadData *td, uint8_t *dst,
1176
1697
s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
1177
1698
src - my_idx * linesize - mx_idx,
1178
1699
EDGE_EMU_LINESIZE, linesize,
1179
block_w + subpel_idx[1][mx], block_h + subpel_idx[1][my],
1180
x_off - mx_idx, y_off - my_idx, width, height);
1700
block_w + subpel_idx[1][mx],
1701
block_h + subpel_idx[1][my],
1702
x_off - mx_idx, y_off - my_idx,
1181
1704
src = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
1182
1705
src_linesize = EDGE_EMU_LINESIZE;
1184
1707
mc_func[my_idx][mx_idx](dst, linesize, src, src_linesize, block_h, mx, my);
1186
1709
ff_thread_await_progress(ref, (3 + y_off + block_h) >> 4, 0);
1187
mc_func[0][0](dst, linesize, src + y_off * linesize + x_off, linesize, block_h, 0, 0);
1710
mc_func[0][0](dst, linesize, src + y_off * linesize + x_off,
1711
linesize, block_h, 0, 0);
1192
1716
* chroma MC function
1194
* @param s VP8 decoding context
1195
* @param dst1 target buffer for block data at block position (U plane)
1196
* @param dst2 target buffer for block data at block position (V plane)
1197
* @param ref reference picture buffer at origin (0, 0)
1198
* @param mv motion vector (relative to block position) to get pixel data from
1199
* @param x_off horizontal position of block from origin (0, 0)
1200
* @param y_off vertical position of block from origin (0, 0)
1201
* @param block_w width of block (16, 8 or 4)
1202
* @param block_h height of block (always same as block_w)
1203
* @param width width of src/dst plane data
1204
* @param height height of src/dst plane data
1718
* @param s VP8 decoding context
1719
* @param dst1 target buffer for block data at block position (U plane)
1720
* @param dst2 target buffer for block data at block position (V plane)
1721
* @param ref reference picture buffer at origin (0, 0)
1722
* @param mv motion vector (relative to block position) to get pixel data from
1723
* @param x_off horizontal position of block from origin (0, 0)
1724
* @param y_off vertical position of block from origin (0, 0)
1725
* @param block_w width of block (16, 8 or 4)
1726
* @param block_h height of block (always same as block_w)
1727
* @param width width of src/dst plane data
1728
* @param height height of src/dst plane data
1205
1729
* @param linesize size of a single line of plane data, including padding
1206
* @param mc_func motion compensation function pointers (bilinear or sixtap MC)
1730
* @param mc_func motion compensation function pointers (bilinear or sixtap MC)
1208
1732
static av_always_inline
1209
void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1, uint8_t *dst2,
1210
ThreadFrame *ref, const VP56mv *mv, int x_off, int y_off,
1211
int block_w, int block_h, int width, int height, ptrdiff_t linesize,
1733
void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1,
1734
uint8_t *dst2, ThreadFrame *ref, const VP56mv *mv,
1735
int x_off, int y_off, int block_w, int block_h,
1736
int width, int height, ptrdiff_t linesize,
1212
1737
vp8_mc_func mc_func[3][3])
1214
1739
uint8_t *src1 = ref->f->data[1], *src2 = ref->f->data[2];
1216
1741
if (AV_RN32A(mv)) {
1217
int mx = mv->x&7, mx_idx = subpel_idx[0][mx];
1218
int my = mv->y&7, my_idx = subpel_idx[0][my];
1742
int mx = mv->x & 7, mx_idx = subpel_idx[0][mx];
1743
int my = mv->y & 7, my_idx = subpel_idx[0][my];
1220
1745
x_off += mv->x >> 3;
1221
1746
y_off += mv->y >> 3;
1286
1816
/* Fetch pixels for estimated mv 4 macroblocks ahead.
1287
* Optimized for 64-byte cache lines. Inspired by ffh264 prefetch_motion. */
1288
static av_always_inline void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y, int mb_xy, int ref)
1817
* Optimized for 64-byte cache lines. Inspired by ffh264 prefetch_motion. */
1818
static av_always_inline
1819
void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
1290
1822
/* Don't prefetch refs that haven't been used very often this frame. */
1291
if (s->ref_count[ref-1] > (mb_xy >> 5)) {
1823
if (s->ref_count[ref - 1] > (mb_xy >> 5)) {
1292
1824
int x_off = mb_x << 4, y_off = mb_y << 4;
1293
int mx = (mb->mv.x>>2) + x_off + 8;
1294
int my = (mb->mv.y>>2) + y_off;
1295
uint8_t **src= s->framep[ref]->tf.f->data;
1296
int off= mx + (my + (mb_x&3)*4)*s->linesize + 64;
1825
int mx = (mb->mv.x >> 2) + x_off + 8;
1826
int my = (mb->mv.y >> 2) + y_off;
1827
uint8_t **src = s->framep[ref]->tf.f->data;
1828
int off = mx + (my + (mb_x & 3) * 4) * s->linesize + 64;
1297
1829
/* For threading, a ff_thread_await_progress here might be useful, but
1298
1830
* it actually slows down the decoder. Since a bad prefetch doesn't
1299
1831
* generate bad decoder output, we don't run it here. */
1300
s->vdsp.prefetch(src[0]+off, s->linesize, 4);
1301
off= (mx>>1) + ((my>>1) + (mb_x&7))*s->uvlinesize + 64;
1302
s->vdsp.prefetch(src[1]+off, src[2]-src[1], 2);
1832
s->vdsp.prefetch(src[0] + off, s->linesize, 4);
1833
off = (mx >> 1) + ((my >> 1) + (mb_x & 7)) * s->uvlinesize + 64;
1834
s->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
1328
1860
for (y = 0; y < 4; y++) {
1329
1861
for (x = 0; x < 4; x++) {
1330
vp8_mc_luma(s, td, dst[0] + 4*y*s->linesize + x*4,
1332
4*x + x_off, 4*y + y_off, 4, 4,
1862
vp8_mc_luma(s, td, dst[0] + 4 * y * s->linesize + x * 4,
1863
ref, &bmv[4 * y + x],
1864
4 * x + x_off, 4 * y + y_off, 4, 4,
1333
1865
width, height, s->linesize,
1334
1866
s->put_pixels_tab[2]);
1339
x_off >>= 1; y_off >>= 1; width >>= 1; height >>= 1;
1340
1875
for (y = 0; y < 2; y++) {
1341
1876
for (x = 0; x < 2; x++) {
1342
uvmv.x = mb->bmv[ 2*y * 4 + 2*x ].x +
1343
mb->bmv[ 2*y * 4 + 2*x+1].x +
1344
mb->bmv[(2*y+1) * 4 + 2*x ].x +
1345
mb->bmv[(2*y+1) * 4 + 2*x+1].x;
1346
uvmv.y = mb->bmv[ 2*y * 4 + 2*x ].y +
1347
mb->bmv[ 2*y * 4 + 2*x+1].y +
1348
mb->bmv[(2*y+1) * 4 + 2*x ].y +
1349
mb->bmv[(2*y+1) * 4 + 2*x+1].y;
1350
uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT-1))) >> 2;
1351
uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT-1))) >> 2;
1877
uvmv.x = mb->bmv[2 * y * 4 + 2 * x ].x +
1878
mb->bmv[2 * y * 4 + 2 * x + 1].x +
1879
mb->bmv[(2 * y + 1) * 4 + 2 * x ].x +
1880
mb->bmv[(2 * y + 1) * 4 + 2 * x + 1].x;
1881
uvmv.y = mb->bmv[2 * y * 4 + 2 * x ].y +
1882
mb->bmv[2 * y * 4 + 2 * x + 1].y +
1883
mb->bmv[(2 * y + 1) * 4 + 2 * x ].y +
1884
mb->bmv[(2 * y + 1) * 4 + 2 * x + 1].y;
1885
uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT - 1))) >> 2;
1886
uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT - 1))) >> 2;
1352
1887
if (s->profile == 3) {
1356
vp8_mc_chroma(s, td, dst[1] + 4*y*s->uvlinesize + x*4,
1357
dst[2] + 4*y*s->uvlinesize + x*4, ref, &uvmv,
1358
4*x + x_off, 4*y + y_off, 4, 4,
1891
vp8_mc_chroma(s, td, dst[1] + 4 * y * s->uvlinesize + x * 4,
1892
dst[2] + 4 * y * s->uvlinesize + x * 4, ref,
1893
&uvmv, 4 * x + x_off, 4 * y + y_off, 4, 4,
1359
1894
width, height, s->uvlinesize,
1360
1895
s->put_pixels_tab[2]);
1411
1950
s->vp8dsp.vp8_idct_dc_add4y(y_dst, td->block[y], s->linesize);
1414
y_dst += 4*s->linesize;
1953
y_dst += 4 * s->linesize;
1418
1957
for (ch = 0; ch < 2; ch++) {
1419
uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[4+ch]);
1958
uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[4 + ch]);
1421
uint8_t *ch_dst = dst[1+ch];
1422
if (nnz4&~0x01010101) {
1960
uint8_t *ch_dst = dst[1 + ch];
1961
if (nnz4 & ~0x01010101) {
1423
1962
for (y = 0; y < 2; y++) {
1424
1963
for (x = 0; x < 2; x++) {
1425
if ((uint8_t)nnz4 == 1)
1426
s->vp8dsp.vp8_idct_dc_add(ch_dst+4*x, td->block[4+ch][(y<<1)+x], s->uvlinesize);
1427
else if((uint8_t)nnz4 > 1)
1428
s->vp8dsp.vp8_idct_add(ch_dst+4*x, td->block[4+ch][(y<<1)+x], s->uvlinesize);
1964
if ((uint8_t) nnz4 == 1)
1965
s->vp8dsp.vp8_idct_dc_add(ch_dst + 4 * x,
1966
td->block[4 + ch][(y << 1) + x],
1968
else if ((uint8_t) nnz4 > 1)
1969
s->vp8dsp.vp8_idct_add(ch_dst + 4 * x,
1970
td->block[4 + ch][(y << 1) + x],
1431
1974
goto chroma_idct_end;
1433
ch_dst += 4*s->uvlinesize;
1976
ch_dst += 4 * s->uvlinesize;
1436
s->vp8dsp.vp8_idct_dc_add4uv(ch_dst, td->block[4+ch], s->uvlinesize);
1979
s->vp8dsp.vp8_idct_dc_add4uv(ch_dst, td->block[4 + ch], s->uvlinesize);
1443
static av_always_inline void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb, VP8FilterStrength *f )
1987
static av_always_inline
1988
void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb,
1989
VP8FilterStrength *f, int is_vp7)
1445
1991
int interior_limit, filter_level;
1492
2041
if (!filter_level)
1495
bedge_lim = 2*filter_level + inner_limit;
1496
mbedge_lim = bedge_lim + 4;
2045
bedge_lim_y = filter_level;
2046
bedge_lim_uv = filter_level * 2;
2047
mbedge_lim = filter_level + 2;
2050
bedge_lim_uv = filter_level * 2 + inner_limit;
2051
mbedge_lim = bedge_lim_y + 4;
1498
2054
hev_thresh = hev_thresh_lut[s->keyframe][filter_level];
1501
s->vp8dsp.vp8_h_loop_filter16y(dst[0], linesize,
1502
mbedge_lim, inner_limit, hev_thresh);
1503
s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], uvlinesize,
1504
mbedge_lim, inner_limit, hev_thresh);
1508
s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 4, linesize, bedge_lim,
1509
inner_limit, hev_thresh);
1510
s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+ 8, linesize, bedge_lim,
1511
inner_limit, hev_thresh);
1512
s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0]+12, linesize, bedge_lim,
1513
inner_limit, hev_thresh);
1514
s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4,
1515
uvlinesize, bedge_lim,
1516
inner_limit, hev_thresh);
2057
s->vp8dsp.vp8_h_loop_filter16y(dst[0], linesize,
2058
mbedge_lim, inner_limit, hev_thresh);
2059
s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], uvlinesize,
2060
mbedge_lim, inner_limit, hev_thresh);
2063
#define H_LOOP_FILTER_16Y_INNER(cond) \
2064
if (cond && inner_filter) { \
2065
s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 4, linesize, \
2066
bedge_lim_y, inner_limit, \
2068
s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 8, linesize, \
2069
bedge_lim_y, inner_limit, \
2071
s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 12, linesize, \
2072
bedge_lim_y, inner_limit, \
2074
s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4, \
2075
uvlinesize, bedge_lim_uv, \
2076
inner_limit, hev_thresh); \
2079
H_LOOP_FILTER_16Y_INNER(!is_vp7)
1520
s->vp8dsp.vp8_v_loop_filter16y(dst[0], linesize,
2082
s->vp8dsp.vp8_v_loop_filter16y(dst[0], linesize,
1521
2083
mbedge_lim, inner_limit, hev_thresh);
1522
s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], uvlinesize,
2084
s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], uvlinesize,
1523
2085
mbedge_lim, inner_limit, hev_thresh);
1526
2088
if (inner_filter) {
1527
s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 4*linesize,
1528
linesize, bedge_lim,
1529
inner_limit, hev_thresh);
1530
s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+ 8*linesize,
1531
linesize, bedge_lim,
1532
inner_limit, hev_thresh);
1533
s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0]+12*linesize,
1534
linesize, bedge_lim,
1535
inner_limit, hev_thresh);
1536
s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * uvlinesize,
1537
dst[2] + 4 * uvlinesize,
1538
uvlinesize, bedge_lim,
2089
s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 4 * linesize,
2090
linesize, bedge_lim_y,
2091
inner_limit, hev_thresh);
2092
s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 8 * linesize,
2093
linesize, bedge_lim_y,
2094
inner_limit, hev_thresh);
2095
s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 12 * linesize,
2096
linesize, bedge_lim_y,
2097
inner_limit, hev_thresh);
2098
s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * uvlinesize,
2099
dst[2] + 4 * uvlinesize,
2100
uvlinesize, bedge_lim_uv,
1539
2101
inner_limit, hev_thresh);
2104
H_LOOP_FILTER_16Y_INNER(is_vp7)
1543
static av_always_inline void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8FilterStrength *f, int mb_x, int mb_y)
2107
static av_always_inline
2108
void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8FilterStrength *f,
1545
2111
int mbedge_lim, bedge_lim;
1546
2112
int filter_level = f->filter_level;
1547
int inner_limit = f->inner_limit;
2113
int inner_limit = f->inner_limit;
1548
2114
int inner_filter = f->inner_filter;
1549
int linesize = s->linesize;
2115
int linesize = s->linesize;
1551
2117
if (!filter_level)
1554
bedge_lim = 2*filter_level + inner_limit;
2120
bedge_lim = 2 * filter_level + inner_limit;
1555
2121
mbedge_lim = bedge_lim + 4;
1558
2124
s->vp8dsp.vp8_h_loop_filter_simple(dst, linesize, mbedge_lim);
1559
2125
if (inner_filter) {
1560
s->vp8dsp.vp8_h_loop_filter_simple(dst+ 4, linesize, bedge_lim);
1561
s->vp8dsp.vp8_h_loop_filter_simple(dst+ 8, linesize, bedge_lim);
1562
s->vp8dsp.vp8_h_loop_filter_simple(dst+12, linesize, bedge_lim);
2126
s->vp8dsp.vp8_h_loop_filter_simple(dst + 4, linesize, bedge_lim);
2127
s->vp8dsp.vp8_h_loop_filter_simple(dst + 8, linesize, bedge_lim);
2128
s->vp8dsp.vp8_h_loop_filter_simple(dst + 12, linesize, bedge_lim);
1566
2132
s->vp8dsp.vp8_v_loop_filter_simple(dst, linesize, mbedge_lim);
1567
2133
if (inner_filter) {
1568
s->vp8dsp.vp8_v_loop_filter_simple(dst+ 4*linesize, linesize, bedge_lim);
1569
s->vp8dsp.vp8_v_loop_filter_simple(dst+ 8*linesize, linesize, bedge_lim);
1570
s->vp8dsp.vp8_v_loop_filter_simple(dst+12*linesize, linesize, bedge_lim);
2134
s->vp8dsp.vp8_v_loop_filter_simple(dst + 4 * linesize, linesize, bedge_lim);
2135
s->vp8dsp.vp8_v_loop_filter_simple(dst + 8 * linesize, linesize, bedge_lim);
2136
s->vp8dsp.vp8_v_loop_filter_simple(dst + 12 * linesize, linesize, bedge_lim);
1574
2140
#define MARGIN (16 << 2)
1575
static void vp8_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *curframe,
1576
VP8Frame *prev_frame)
2141
static av_always_inline
2142
void vp78_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *curframe,
2143
VP8Frame *prev_frame, int is_vp7)
1578
2145
VP8Context *s = avctx->priv_data;
1579
2146
int mb_x, mb_y;
2174
static void vp7_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame,
2175
VP8Frame *prev_frame)
2177
vp78_decode_mv_mb_modes(avctx, cur_frame, prev_frame, IS_VP7);
2180
static void vp8_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame,
2181
VP8Frame *prev_frame)
2183
vp78_decode_mv_mb_modes(avctx, cur_frame, prev_frame, IS_VP8);
1605
2186
#if HAVE_THREADS
1606
#define check_thread_pos(td, otd, mb_x_check, mb_y_check)\
1608
int tmp = (mb_y_check << 16) | (mb_x_check & 0xFFFF);\
1609
if (otd->thread_mb_pos < tmp) {\
1610
pthread_mutex_lock(&otd->lock);\
1611
td->wait_mb_pos = tmp;\
1613
if (otd->thread_mb_pos >= tmp)\
1615
pthread_cond_wait(&otd->cond, &otd->lock);\
1617
td->wait_mb_pos = INT_MAX;\
1618
pthread_mutex_unlock(&otd->lock);\
2187
#define check_thread_pos(td, otd, mb_x_check, mb_y_check) \
2189
int tmp = (mb_y_check << 16) | (mb_x_check & 0xFFFF); \
2190
if (otd->thread_mb_pos < tmp) { \
2191
pthread_mutex_lock(&otd->lock); \
2192
td->wait_mb_pos = tmp; \
2194
if (otd->thread_mb_pos >= tmp) \
2196
pthread_cond_wait(&otd->cond, &otd->lock); \
2198
td->wait_mb_pos = INT_MAX; \
2199
pthread_mutex_unlock(&otd->lock); \
1622
#define update_pos(td, mb_y, mb_x)\
1624
int pos = (mb_y << 16) | (mb_x & 0xFFFF);\
1625
int sliced_threading = (avctx->active_thread_type == FF_THREAD_SLICE) && (num_jobs > 1);\
1626
int is_null = (next_td == NULL) || (prev_td == NULL);\
1627
int pos_check = (is_null) ? 1 :\
1628
(next_td != td && pos >= next_td->wait_mb_pos) ||\
1629
(prev_td != td && pos >= prev_td->wait_mb_pos);\
1630
td->thread_mb_pos = pos;\
1631
if (sliced_threading && pos_check) {\
1632
pthread_mutex_lock(&td->lock);\
1633
pthread_cond_broadcast(&td->cond);\
1634
pthread_mutex_unlock(&td->lock);\
2203
#define update_pos(td, mb_y, mb_x) \
2205
int pos = (mb_y << 16) | (mb_x & 0xFFFF); \
2206
int sliced_threading = (avctx->active_thread_type == FF_THREAD_SLICE) && \
2208
int is_null = !next_td || !prev_td; \
2209
int pos_check = (is_null) ? 1 \
2210
: (next_td != td && \
2211
pos >= next_td->wait_mb_pos) || \
2213
pos >= prev_td->wait_mb_pos); \
2214
td->thread_mb_pos = pos; \
2215
if (sliced_threading && pos_check) { \
2216
pthread_mutex_lock(&td->lock); \
2217
pthread_cond_broadcast(&td->cond); \
2218
pthread_mutex_unlock(&td->lock); \
1638
2222
#define check_thread_pos(td, otd, mb_x_check, mb_y_check)
1639
2223
#define update_pos(td, mb_y, mb_x)
1642
2226
static void vp8_decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata,
1643
int jobnr, int threadnr)
2227
int jobnr, int threadnr, int is_vp7)
1645
2229
VP8Context *s = avctx->priv_data;
1646
2230
VP8ThreadData *prev_td, *next_td, *td = &s->thread_data[threadnr];
1647
int mb_y = td->thread_mb_pos>>16;
1648
int mb_x, mb_xy = mb_y*s->mb_width;
2231
int mb_y = td->thread_mb_pos >> 16;
2232
int mb_x, mb_xy = mb_y * s->mb_width;
1649
2233
int num_jobs = s->num_jobs;
1650
2234
VP8Frame *curframe = s->curframe, *prev_frame = s->prev_frame;
1651
VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
2235
VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions - 1)];
1652
2236
VP8Macroblock *mb;
1653
2237
uint8_t *dst[3] = {
1654
curframe->tf.f->data[0] + 16*mb_y*s->linesize,
1655
curframe->tf.f->data[1] + 8*mb_y*s->uvlinesize,
1656
curframe->tf.f->data[2] + 8*mb_y*s->uvlinesize
2238
curframe->tf.f->data[0] + 16 * mb_y * s->linesize,
2239
curframe->tf.f->data[1] + 8 * mb_y * s->uvlinesize,
2240
curframe->tf.f->data[2] + 8 * mb_y * s->uvlinesize
1658
if (mb_y == 0) prev_td = td;
1659
else prev_td = &s->thread_data[(jobnr + num_jobs - 1)%num_jobs];
1660
if (mb_y == s->mb_height-1) next_td = td;
1661
else next_td = &s->thread_data[(jobnr + 1)%num_jobs];
2245
prev_td = &s->thread_data[(jobnr + num_jobs - 1) % num_jobs];
2246
if (mb_y == s->mb_height - 1)
2249
next_td = &s->thread_data[(jobnr + 1) % num_jobs];
1662
2250
if (s->mb_layout == 1)
1663
mb = s->macroblocks_base + ((s->mb_width+1)*(mb_y + 1) + 1);
2251
mb = s->macroblocks_base + ((s->mb_width + 1) * (mb_y + 1) + 1);
1665
2253
// Make sure the previous frame has read its segmentation map,
1666
2254
// if we re-use the same map.
1667
2255
if (prev_frame && s->segmentation.enabled &&
1668
2256
!s->segmentation.update_map)
1669
2257
ff_thread_await_progress(&prev_frame->tf, mb_y, 0);
1670
mb = s->macroblocks + (s->mb_height - mb_y - 1)*2;
2258
mb = s->macroblocks + (s->mb_height - mb_y - 1) * 2;
1671
2259
memset(mb - 1, 0, sizeof(*mb)); // zero left macroblock
1672
AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED*0x01010101);
2260
AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED * 0x01010101);
1675
memset(td->left_nnz, 0, sizeof(td->left_nnz));
2263
if (!is_vp7 || mb_y == 0)
2264
memset(td->left_nnz, 0, sizeof(td->left_nnz));
1677
2266
s->mv_min.x = -MARGIN;
1678
s->mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
2267
s->mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
1680
2269
for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
1681
2270
// Wait for previous thread to read mb_x+2, and reach mb_y-1.
1682
2271
if (prev_td != td) {
1683
2272
if (threadnr != 0) {
1684
check_thread_pos(td, prev_td, mb_x+1, mb_y-1);
2273
check_thread_pos(td, prev_td,
2274
mb_x + (is_vp7 ? 2 : 1),
2275
mb_y - (is_vp7 ? 2 : 1));
1686
check_thread_pos(td, prev_td, (s->mb_width+3) + (mb_x+1), mb_y-1);
2277
check_thread_pos(td, prev_td,
2278
mb_x + (is_vp7 ? 2 : 1) + s->mb_width + 3,
2279
mb_y - (is_vp7 ? 2 : 1));
1690
s->vdsp.prefetch(dst[0] + (mb_x&3)*4*s->linesize + 64, s->linesize, 4);
1691
s->vdsp.prefetch(dst[1] + (mb_x&7)*s->uvlinesize + 64, dst[2] - dst[1], 2);
2283
s->vdsp.prefetch(dst[0] + (mb_x & 3) * 4 * s->linesize + 64,
2285
s->vdsp.prefetch(dst[1] + (mb_x & 7) * s->uvlinesize + 64,
2286
dst[2] - dst[1], 2);
1693
2288
if (!s->mb_layout)
1694
2289
decode_mb_mode(s, mb, mb_x, mb_y, curframe->seg_map->data + mb_xy,
1695
2290
prev_frame && prev_frame->seg_map ?
1696
prev_frame->seg_map->data + mb_xy : NULL, 0);
2291
prev_frame->seg_map->data + mb_xy : NULL, 0, is_vp7);
1698
2293
prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_PREVIOUS);
1701
decode_mb_coeffs(s, td, c, mb, s->top_nnz[mb_x], td->left_nnz);
2296
decode_mb_coeffs(s, td, c, mb, s->top_nnz[mb_x], td->left_nnz, is_vp7);
1703
2298
if (mb->mode <= MODE_I4x4)
1704
intra_predict(s, td, dst, mb, mb_x, mb_y);
2299
intra_predict(s, td, dst, mb, mb_x, mb_y, is_vp7);
1706
2301
inter_predict(s, td, dst, mb, mb_x, mb_y);
1723
2319
if (s->deblock_filter)
1724
filter_level_for_mb(s, mb, &td->filter_strength[mb_x]);
2320
filter_level_for_mb(s, mb, &td->filter_strength[mb_x], is_vp7);
1726
if (s->deblock_filter && num_jobs != 1 && threadnr == num_jobs-1) {
2322
if (s->deblock_filter && num_jobs != 1 && threadnr == num_jobs - 1) {
1727
2323
if (s->filter.simple)
1728
backup_mb_border(s->top_border[mb_x+1], dst[0], NULL, NULL, s->linesize, 0, 1);
2324
backup_mb_border(s->top_border[mb_x + 1], dst[0],
2325
NULL, NULL, s->linesize, 0, 1);
1730
backup_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2], s->linesize, s->uvlinesize, 0);
2327
backup_mb_border(s->top_border[mb_x + 1], dst[0],
2328
dst[1], dst[2], s->linesize, s->uvlinesize, 0);
1733
2331
prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN2);
1738
2336
s->mv_min.x -= 64;
1739
2337
s->mv_max.x -= 64;
1741
if (mb_x == s->mb_width+1) {
1742
update_pos(td, mb_y, s->mb_width+3);
2339
if (mb_x == s->mb_width + 1) {
2340
update_pos(td, mb_y, s->mb_width + 3);
1744
2342
update_pos(td, mb_y, mb_x);
1749
2347
static void vp8_filter_mb_row(AVCodecContext *avctx, void *tdata,
1750
int jobnr, int threadnr)
2348
int jobnr, int threadnr, int is_vp7)
1752
2350
VP8Context *s = avctx->priv_data;
1753
2351
VP8ThreadData *td = &s->thread_data[threadnr];
1754
int mb_x, mb_y = td->thread_mb_pos>>16, num_jobs = s->num_jobs;
2352
int mb_x, mb_y = td->thread_mb_pos >> 16, num_jobs = s->num_jobs;
1755
2353
AVFrame *curframe = s->curframe->tf.f;
1756
2354
VP8Macroblock *mb;
1757
2355
VP8ThreadData *prev_td, *next_td;
1758
2356
uint8_t *dst[3] = {
1759
curframe->data[0] + 16*mb_y*s->linesize,
1760
curframe->data[1] + 8*mb_y*s->uvlinesize,
1761
curframe->data[2] + 8*mb_y*s->uvlinesize
2357
curframe->data[0] + 16 * mb_y * s->linesize,
2358
curframe->data[1] + 8 * mb_y * s->uvlinesize,
2359
curframe->data[2] + 8 * mb_y * s->uvlinesize
1764
2362
if (s->mb_layout == 1)
1765
mb = s->macroblocks_base + ((s->mb_width+1)*(mb_y + 1) + 1);
2363
mb = s->macroblocks_base + ((s->mb_width + 1) * (mb_y + 1) + 1);
1767
mb = s->macroblocks + (s->mb_height - mb_y - 1)*2;
2365
mb = s->macroblocks + (s->mb_height - mb_y - 1) * 2;
1769
if (mb_y == 0) prev_td = td;
1770
else prev_td = &s->thread_data[(jobnr + num_jobs - 1)%num_jobs];
1771
if (mb_y == s->mb_height-1) next_td = td;
1772
else next_td = &s->thread_data[(jobnr + 1)%num_jobs];
2370
prev_td = &s->thread_data[(jobnr + num_jobs - 1) % num_jobs];
2371
if (mb_y == s->mb_height - 1)
2374
next_td = &s->thread_data[(jobnr + 1) % num_jobs];
1774
2376
for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb++) {
1775
2377
VP8FilterStrength *f = &td->filter_strength[mb_x];
1776
if (prev_td != td) {
1777
check_thread_pos(td, prev_td, (mb_x+1) + (s->mb_width+3), mb_y-1);
2379
check_thread_pos(td, prev_td,
2380
(mb_x + 1) + (s->mb_width + 3), mb_y - 1);
1779
2381
if (next_td != td)
1780
if (next_td != &s->thread_data[0]) {
1781
check_thread_pos(td, next_td, mb_x+1, mb_y+1);
2382
if (next_td != &s->thread_data[0])
2383
check_thread_pos(td, next_td, mb_x + 1, mb_y + 1);
1784
2385
if (num_jobs == 1) {
1785
2386
if (s->filter.simple)
1786
backup_mb_border(s->top_border[mb_x+1], dst[0], NULL, NULL, s->linesize, 0, 1);
2387
backup_mb_border(s->top_border[mb_x + 1], dst[0],
2388
NULL, NULL, s->linesize, 0, 1);
1788
backup_mb_border(s->top_border[mb_x+1], dst[0], dst[1], dst[2], s->linesize, s->uvlinesize, 0);
2390
backup_mb_border(s->top_border[mb_x + 1], dst[0],
2391
dst[1], dst[2], s->linesize, s->uvlinesize, 0);
1791
2394
if (s->filter.simple)
1792
2395
filter_mb_simple(s, dst[0], f, mb_x, mb_y);
1794
filter_mb(s, dst, f, mb_x, mb_y);
2397
filter_mb(s, dst, f, mb_x, mb_y, is_vp7);
1799
update_pos(td, mb_y, (s->mb_width+3) + mb_x);
2402
update_pos(td, mb_y, (s->mb_width + 3) + mb_x);
1803
static int vp8_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
1804
int jobnr, int threadnr)
2406
static av_always_inline
2407
int vp78_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata, int jobnr,
2408
int threadnr, int is_vp7)
1806
2410
VP8Context *s = avctx->priv_data;
1807
2411
VP8ThreadData *td = &s->thread_data[jobnr];
1808
2412
VP8ThreadData *next_td = NULL, *prev_td = NULL;
1809
2413
VP8Frame *curframe = s->curframe;
1810
2414
int mb_y, num_jobs = s->num_jobs;
1811
2416
td->thread_nr = threadnr;
1812
2417
for (mb_y = jobnr; mb_y < s->mb_height; mb_y += num_jobs) {
1813
if (mb_y >= s->mb_height) break;
1814
td->thread_mb_pos = mb_y<<16;
1815
vp8_decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr);
2418
if (mb_y >= s->mb_height)
2420
td->thread_mb_pos = mb_y << 16;
2421
vp8_decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr, is_vp7);
1816
2422
if (s->deblock_filter)
1817
vp8_filter_mb_row(avctx, tdata, jobnr, threadnr);
2423
vp8_filter_mb_row(avctx, tdata, jobnr, threadnr, is_vp7);
1818
2424
update_pos(td, mb_y, INT_MAX & 0xFFFF);
1820
2426
s->mv_min.y -= 64;
1830
int ff_vp8_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
2436
static int vp7_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
2437
int jobnr, int threadnr)
2439
return vp78_decode_mb_row_sliced(avctx, tdata, jobnr, threadnr, IS_VP7);
2442
static int vp8_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
2443
int jobnr, int threadnr)
2445
return vp78_decode_mb_row_sliced(avctx, tdata, jobnr, threadnr, IS_VP8);
2449
static av_always_inline
2450
int vp78_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
2451
AVPacket *avpkt, int is_vp7)
1833
2453
VP8Context *s = avctx->priv_data;
1834
2454
int ret, i, referenced, num_jobs;
1835
2455
enum AVDiscard skip_thresh;
1836
2456
VP8Frame *av_uninit(curframe), *prev_frame;
1838
if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
2459
ret = vp7_decode_frame_header(s, avpkt->data, avpkt->size);
2461
ret = vp8_decode_frame_header(s, avpkt->data, avpkt->size);
1841
2466
prev_frame = s->framep[VP56_FRAME_CURRENT];
1843
referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT
1844
|| s->update_altref == VP56_FRAME_CURRENT;
2468
referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT ||
2469
s->update_altref == VP56_FRAME_CURRENT;
1846
skip_thresh = !referenced ? AVDISCARD_NONREF :
1847
!s->keyframe ? AVDISCARD_NONKEY : AVDISCARD_ALL;
2471
skip_thresh = !referenced ? AVDISCARD_NONREF
2472
: !s->keyframe ? AVDISCARD_NONKEY
1849
2475
if (avctx->skip_frame >= skip_thresh) {
1850
2476
s->invisible = 1;
1858
2484
if (s->frames[i].tf.f->data[0] &&
1859
2485
&s->frames[i] != prev_frame &&
1860
2486
&s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1861
&s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
2487
&s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1862
2488
&s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
1863
2489
vp8_release_frame(s, &s->frames[i]);
1865
// find a free buffer
1866
for (i = 0; i < 5; i++)
1867
if (&s->frames[i] != prev_frame &&
1868
&s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
1869
&s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
1870
&s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
1871
curframe = s->framep[VP56_FRAME_CURRENT] = &s->frames[i];
1875
av_log(avctx, AV_LOG_FATAL, "Ran out of free frames!\n");
1878
if (curframe->tf.f->data[0])
1879
vp8_release_frame(s, curframe);
2491
curframe = s->framep[VP56_FRAME_CURRENT] = vp8_find_free_buffer(s);
1881
// Given that arithmetic probabilities are updated every frame, it's quite likely
1882
// that the values we have on a random interframe are complete junk if we didn't
1883
// start decode on a keyframe. So just don't display anything rather than junk.
2493
/* Given that arithmetic probabilities are updated every frame, it's quite
2494
* likely that the values we have on a random interframe are complete
2495
* junk if we didn't start decode on a keyframe. So just don't display
2496
* anything rather than junk. */
1884
2497
if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
1885
!s->framep[VP56_FRAME_GOLDEN] ||
2498
!s->framep[VP56_FRAME_GOLDEN] ||
1886
2499
!s->framep[VP56_FRAME_GOLDEN2])) {
1887
av_log(avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
2500
av_log(avctx, AV_LOG_WARNING,
2501
"Discarding interframe without a prior keyframe!\n");
1888
2502
ret = AVERROR_INVALIDDATA;
1892
2506
curframe->tf.f->key_frame = s->keyframe;
1893
curframe->tf.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
2507
curframe->tf.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
2508
: AV_PICTURE_TYPE_P;
1894
2509
if ((ret = vp8_alloc_frame(s, curframe, referenced))) {
1895
2510
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
1899
2514
// check if golden and altref are swapped
1900
if (s->update_altref != VP56_FRAME_NONE) {
1901
s->next_framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
1903
s->next_framep[VP56_FRAME_GOLDEN2] = s->framep[VP56_FRAME_GOLDEN2];
1905
if (s->update_golden != VP56_FRAME_NONE) {
1906
s->next_framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
1908
s->next_framep[VP56_FRAME_GOLDEN] = s->framep[VP56_FRAME_GOLDEN];
1910
if (s->update_last) {
2515
if (s->update_altref != VP56_FRAME_NONE)
2516
s->next_framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
2518
s->next_framep[VP56_FRAME_GOLDEN2] = s->framep[VP56_FRAME_GOLDEN2];
2520
if (s->update_golden != VP56_FRAME_NONE)
2521
s->next_framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
2523
s->next_framep[VP56_FRAME_GOLDEN] = s->framep[VP56_FRAME_GOLDEN];
1911
2526
s->next_framep[VP56_FRAME_PREVIOUS] = curframe;
1913
2528
s->next_framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_PREVIOUS];
1915
s->next_framep[VP56_FRAME_CURRENT] = curframe;
2530
s->next_framep[VP56_FRAME_CURRENT] = curframe;
1917
2532
ff_thread_finish_setup(avctx);
1919
2534
s->linesize = curframe->tf.f->linesize[0];
1920
2535
s->uvlinesize = curframe->tf.f->linesize[1];
1922
memset(s->top_nnz, 0, s->mb_width*sizeof(*s->top_nnz));
1923
/* Zero macroblock structures for top/top-left prediction from outside the frame. */
2537
memset(s->top_nnz, 0, s->mb_width * sizeof(*s->top_nnz));
2538
/* Zero macroblock structures for top/top-left prediction
2539
* from outside the frame. */
1924
2540
if (!s->mb_layout)
1925
memset(s->macroblocks + s->mb_height*2 - 1, 0, (s->mb_width+1)*sizeof(*s->macroblocks));
2541
memset(s->macroblocks + s->mb_height * 2 - 1, 0,
2542
(s->mb_width + 1) * sizeof(*s->macroblocks));
1926
2543
if (!s->mb_layout && s->keyframe)
1927
memset(s->intra4x4_pred_mode_top, DC_PRED, s->mb_width*4);
2544
memset(s->intra4x4_pred_mode_top, DC_PRED, s->mb_width * 4);
1929
2546
memset(s->ref_count, 0, sizeof(s->ref_count));
1932
2548
if (s->mb_layout == 1) {
1933
2549
// Make sure the previous frame has read its segmentation map,
1934
2550
// if we re-use the same map.
1935
2551
if (prev_frame && s->segmentation.enabled &&
1936
2552
!s->segmentation.update_map)
1937
2553
ff_thread_await_progress(&prev_frame->tf, 1, 0);
1938
vp8_decode_mv_mb_modes(avctx, curframe, prev_frame);
2555
vp7_decode_mv_mb_modes(avctx, curframe, prev_frame);
2557
vp8_decode_mv_mb_modes(avctx, curframe, prev_frame);
1941
2560
if (avctx->active_thread_type == FF_THREAD_FRAME)