predfunc.go 12.7 KB
Newer Older
zhangweiwei's avatar
init  
zhangweiwei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package vp8

// This file implements the predicition functions, as specified in chapter 12.
//
// For each macroblock (of 1x16x16 luma and 2x8x8 chroma coefficients), the
// luma values are either predicted as one large 16x16 region or 16 separate
// 4x4 regions. The chroma values are always predicted as one 8x8 region.
//
// For 4x4 regions, the target block's predicted values (Xs) are a function of
// its previously-decoded top and left border values, as well as a number of
// pixels from the top-right:
//
//	a b c d e f g h
//	p X X X X
//	q X X X X
//	r X X X X
//	s X X X X
//
// The predictor modes are:
//	- DC: all Xs = (b + c + d + e + p + q + r + s + 4) / 8.
//	- TM: the first X = (b + p - a), the second X = (c + p - a), and so on.
//	- VE: each X = the weighted average of its column's top value and that
//	      value's neighbors, i.e. averages of abc, bcd, cde or def.
//	- HE: similar to VE except rows instead of columns, and the final row is
//	      an average of r, s and s.
//	- RD, VR, LD, VL, HD, HU: these diagonal modes ("Right Down", "Vertical
//	      Right", etc) are more complicated and are described in section 12.3.
// All Xs are clipped to the range [0, 255].
//
// For 8x8 and 16x16 regions, the target block's predicted values are a
// function of the top and left border values without the top-right overhang,
// i.e. without the 8x8 or 16x16 equivalent of f, g and h. Furthermore:
//	- There are no diagonal predictor modes, only DC, TM, VE and HE.
//	- The DC mode has variants for macroblocks in the top row and/or left
//	  column, i.e. for macroblocks with mby == 0 || mbx == 0.
//	- The VE and HE modes take only the column top or row left values; they do
//	  not smooth that top/left value with its neighbors.

// nPred is the number of predictor modes, not including the Top/Left versions
// of the DC predictor mode.
const nPred = 10

const (
	predDC = iota
	predTM
	predVE
	predHE
	predRD
	predVR
	predLD
	predVL
	predHD
	predHU
	predDCTop
	predDCLeft
	predDCTopLeft
)

func checkTopLeftPred(mbx, mby int, p uint8) uint8 {
	if p != predDC {
		return p
	}
	if mbx == 0 {
		if mby == 0 {
			return predDCTopLeft
		}
		return predDCLeft
	}
	if mby == 0 {
		return predDCTop
	}
	return predDC
}

var predFunc4 = [...]func(*Decoder, int, int){
	predFunc4DC,
	predFunc4TM,
	predFunc4VE,
	predFunc4HE,
	predFunc4RD,
	predFunc4VR,
	predFunc4LD,
	predFunc4VL,
	predFunc4HD,
	predFunc4HU,
	nil,
	nil,
	nil,
}

var predFunc8 = [...]func(*Decoder, int, int){
	predFunc8DC,
	predFunc8TM,
	predFunc8VE,
	predFunc8HE,
	nil,
	nil,
	nil,
	nil,
	nil,
	nil,
	predFunc8DCTop,
	predFunc8DCLeft,
	predFunc8DCTopLeft,
}

var predFunc16 = [...]func(*Decoder, int, int){
	predFunc16DC,
	predFunc16TM,
	predFunc16VE,
	predFunc16HE,
	nil,
	nil,
	nil,
	nil,
	nil,
	nil,
	predFunc16DCTop,
	predFunc16DCLeft,
	predFunc16DCTopLeft,
}

func predFunc4DC(z *Decoder, y, x int) {
	sum := uint32(4)
	for i := 0; i < 4; i++ {
		sum += uint32(z.ybr[y-1][x+i])
	}
	for j := 0; j < 4; j++ {
		sum += uint32(z.ybr[y+j][x-1])
	}
	avg := uint8(sum / 8)
	for j := 0; j < 4; j++ {
		for i := 0; i < 4; i++ {
			z.ybr[y+j][x+i] = avg
		}
	}
}

func predFunc4TM(z *Decoder, y, x int) {
	delta0 := -int32(z.ybr[y-1][x-1])
	for j := 0; j < 4; j++ {
		delta1 := delta0 + int32(z.ybr[y+j][x-1])
		for i := 0; i < 4; i++ {
			delta2 := delta1 + int32(z.ybr[y-1][x+i])
			z.ybr[y+j][x+i] = uint8(clip(delta2, 0, 255))
		}
	}
}

func predFunc4VE(z *Decoder, y, x int) {
	a := int32(z.ybr[y-1][x-1])
	b := int32(z.ybr[y-1][x+0])
	c := int32(z.ybr[y-1][x+1])
	d := int32(z.ybr[y-1][x+2])
	e := int32(z.ybr[y-1][x+3])
	f := int32(z.ybr[y-1][x+4])
	abc := uint8((a + 2*b + c + 2) / 4)
	bcd := uint8((b + 2*c + d + 2) / 4)
	cde := uint8((c + 2*d + e + 2) / 4)
	def := uint8((d + 2*e + f + 2) / 4)
	for j := 0; j < 4; j++ {
		z.ybr[y+j][x+0] = abc
		z.ybr[y+j][x+1] = bcd
		z.ybr[y+j][x+2] = cde
		z.ybr[y+j][x+3] = def
	}
}

func predFunc4HE(z *Decoder, y, x int) {
	s := int32(z.ybr[y+3][x-1])
	r := int32(z.ybr[y+2][x-1])
	q := int32(z.ybr[y+1][x-1])
	p := int32(z.ybr[y+0][x-1])
	a := int32(z.ybr[y-1][x-1])
	ssr := uint8((s + 2*s + r + 2) / 4)
	srq := uint8((s + 2*r + q + 2) / 4)
	rqp := uint8((r + 2*q + p + 2) / 4)
	apq := uint8((a + 2*p + q + 2) / 4)
	for i := 0; i < 4; i++ {
		z.ybr[y+0][x+i] = apq
		z.ybr[y+1][x+i] = rqp
		z.ybr[y+2][x+i] = srq
		z.ybr[y+3][x+i] = ssr
	}
}

func predFunc4RD(z *Decoder, y, x int) {
	s := int32(z.ybr[y+3][x-1])
	r := int32(z.ybr[y+2][x-1])
	q := int32(z.ybr[y+1][x-1])
	p := int32(z.ybr[y+0][x-1])
	a := int32(z.ybr[y-1][x-1])
	b := int32(z.ybr[y-1][x+0])
	c := int32(z.ybr[y-1][x+1])
	d := int32(z.ybr[y-1][x+2])
	e := int32(z.ybr[y-1][x+3])
	srq := uint8((s + 2*r + q + 2) / 4)
	rqp := uint8((r + 2*q + p + 2) / 4)
	qpa := uint8((q + 2*p + a + 2) / 4)
	pab := uint8((p + 2*a + b + 2) / 4)
	abc := uint8((a + 2*b + c + 2) / 4)
	bcd := uint8((b + 2*c + d + 2) / 4)
	cde := uint8((c + 2*d + e + 2) / 4)
	z.ybr[y+0][x+0] = pab
	z.ybr[y+0][x+1] = abc
	z.ybr[y+0][x+2] = bcd
	z.ybr[y+0][x+3] = cde
	z.ybr[y+1][x+0] = qpa
	z.ybr[y+1][x+1] = pab
	z.ybr[y+1][x+2] = abc
	z.ybr[y+1][x+3] = bcd
	z.ybr[y+2][x+0] = rqp
	z.ybr[y+2][x+1] = qpa
	z.ybr[y+2][x+2] = pab
	z.ybr[y+2][x+3] = abc
	z.ybr[y+3][x+0] = srq
	z.ybr[y+3][x+1] = rqp
	z.ybr[y+3][x+2] = qpa
	z.ybr[y+3][x+3] = pab
}

func predFunc4VR(z *Decoder, y, x int) {
	r := int32(z.ybr[y+2][x-1])
	q := int32(z.ybr[y+1][x-1])
	p := int32(z.ybr[y+0][x-1])
	a := int32(z.ybr[y-1][x-1])
	b := int32(z.ybr[y-1][x+0])
	c := int32(z.ybr[y-1][x+1])
	d := int32(z.ybr[y-1][x+2])
	e := int32(z.ybr[y-1][x+3])
	ab := uint8((a + b + 1) / 2)
	bc := uint8((b + c + 1) / 2)
	cd := uint8((c + d + 1) / 2)
	de := uint8((d + e + 1) / 2)
	rqp := uint8((r + 2*q + p + 2) / 4)
	qpa := uint8((q + 2*p + a + 2) / 4)
	pab := uint8((p + 2*a + b + 2) / 4)
	abc := uint8((a + 2*b + c + 2) / 4)
	bcd := uint8((b + 2*c + d + 2) / 4)
	cde := uint8((c + 2*d + e + 2) / 4)
	z.ybr[y+0][x+0] = ab
	z.ybr[y+0][x+1] = bc
	z.ybr[y+0][x+2] = cd
	z.ybr[y+0][x+3] = de
	z.ybr[y+1][x+0] = pab
	z.ybr[y+1][x+1] = abc
	z.ybr[y+1][x+2] = bcd
	z.ybr[y+1][x+3] = cde
	z.ybr[y+2][x+0] = qpa
	z.ybr[y+2][x+1] = ab
	z.ybr[y+2][x+2] = bc
	z.ybr[y+2][x+3] = cd
	z.ybr[y+3][x+0] = rqp
	z.ybr[y+3][x+1] = pab
	z.ybr[y+3][x+2] = abc
	z.ybr[y+3][x+3] = bcd
}

func predFunc4LD(z *Decoder, y, x int) {
	a := int32(z.ybr[y-1][x+0])
	b := int32(z.ybr[y-1][x+1])
	c := int32(z.ybr[y-1][x+2])
	d := int32(z.ybr[y-1][x+3])
	e := int32(z.ybr[y-1][x+4])
	f := int32(z.ybr[y-1][x+5])
	g := int32(z.ybr[y-1][x+6])
	h := int32(z.ybr[y-1][x+7])
	abc := uint8((a + 2*b + c + 2) / 4)
	bcd := uint8((b + 2*c + d + 2) / 4)
	cde := uint8((c + 2*d + e + 2) / 4)
	def := uint8((d + 2*e + f + 2) / 4)
	efg := uint8((e + 2*f + g + 2) / 4)
	fgh := uint8((f + 2*g + h + 2) / 4)
	ghh := uint8((g + 2*h + h + 2) / 4)
	z.ybr[y+0][x+0] = abc
	z.ybr[y+0][x+1] = bcd
	z.ybr[y+0][x+2] = cde
	z.ybr[y+0][x+3] = def
	z.ybr[y+1][x+0] = bcd
	z.ybr[y+1][x+1] = cde
	z.ybr[y+1][x+2] = def
	z.ybr[y+1][x+3] = efg
	z.ybr[y+2][x+0] = cde
	z.ybr[y+2][x+1] = def
	z.ybr[y+2][x+2] = efg
	z.ybr[y+2][x+3] = fgh
	z.ybr[y+3][x+0] = def
	z.ybr[y+3][x+1] = efg
	z.ybr[y+3][x+2] = fgh
	z.ybr[y+3][x+3] = ghh
}

func predFunc4VL(z *Decoder, y, x int) {
	a := int32(z.ybr[y-1][x+0])
	b := int32(z.ybr[y-1][x+1])
	c := int32(z.ybr[y-1][x+2])
	d := int32(z.ybr[y-1][x+3])
	e := int32(z.ybr[y-1][x+4])
	f := int32(z.ybr[y-1][x+5])
	g := int32(z.ybr[y-1][x+6])
	h := int32(z.ybr[y-1][x+7])
	ab := uint8((a + b + 1) / 2)
	bc := uint8((b + c + 1) / 2)
	cd := uint8((c + d + 1) / 2)
	de := uint8((d + e + 1) / 2)
	abc := uint8((a + 2*b + c + 2) / 4)
	bcd := uint8((b + 2*c + d + 2) / 4)
	cde := uint8((c + 2*d + e + 2) / 4)
	def := uint8((d + 2*e + f + 2) / 4)
	efg := uint8((e + 2*f + g + 2) / 4)
	fgh := uint8((f + 2*g + h + 2) / 4)
	z.ybr[y+0][x+0] = ab
	z.ybr[y+0][x+1] = bc
	z.ybr[y+0][x+2] = cd
	z.ybr[y+0][x+3] = de
	z.ybr[y+1][x+0] = abc
	z.ybr[y+1][x+1] = bcd
	z.ybr[y+1][x+2] = cde
	z.ybr[y+1][x+3] = def
	z.ybr[y+2][x+0] = bc
	z.ybr[y+2][x+1] = cd
	z.ybr[y+2][x+2] = de
	z.ybr[y+2][x+3] = efg
	z.ybr[y+3][x+0] = bcd
	z.ybr[y+3][x+1] = cde
	z.ybr[y+3][x+2] = def
	z.ybr[y+3][x+3] = fgh
}

func predFunc4HD(z *Decoder, y, x int) {
	s := int32(z.ybr[y+3][x-1])
	r := int32(z.ybr[y+2][x-1])
	q := int32(z.ybr[y+1][x-1])
	p := int32(z.ybr[y+0][x-1])
	a := int32(z.ybr[y-1][x-1])
	b := int32(z.ybr[y-1][x+0])
	c := int32(z.ybr[y-1][x+1])
	d := int32(z.ybr[y-1][x+2])
	sr := uint8((s + r + 1) / 2)
	rq := uint8((r + q + 1) / 2)
	qp := uint8((q + p + 1) / 2)
	pa := uint8((p + a + 1) / 2)
	srq := uint8((s + 2*r + q + 2) / 4)
	rqp := uint8((r + 2*q + p + 2) / 4)
	qpa := uint8((q + 2*p + a + 2) / 4)
	pab := uint8((p + 2*a + b + 2) / 4)
	abc := uint8((a + 2*b + c + 2) / 4)
	bcd := uint8((b + 2*c + d + 2) / 4)
	z.ybr[y+0][x+0] = pa
	z.ybr[y+0][x+1] = pab
	z.ybr[y+0][x+2] = abc
	z.ybr[y+0][x+3] = bcd
	z.ybr[y+1][x+0] = qp
	z.ybr[y+1][x+1] = qpa
	z.ybr[y+1][x+2] = pa
	z.ybr[y+1][x+3] = pab
	z.ybr[y+2][x+0] = rq
	z.ybr[y+2][x+1] = rqp
	z.ybr[y+2][x+2] = qp
	z.ybr[y+2][x+3] = qpa
	z.ybr[y+3][x+0] = sr
	z.ybr[y+3][x+1] = srq
	z.ybr[y+3][x+2] = rq
	z.ybr[y+3][x+3] = rqp
}

func predFunc4HU(z *Decoder, y, x int) {
	s := int32(z.ybr[y+3][x-1])
	r := int32(z.ybr[y+2][x-1])
	q := int32(z.ybr[y+1][x-1])
	p := int32(z.ybr[y+0][x-1])
	pq := uint8((p + q + 1) / 2)
	qr := uint8((q + r + 1) / 2)
	rs := uint8((r + s + 1) / 2)
	pqr := uint8((p + 2*q + r + 2) / 4)
	qrs := uint8((q + 2*r + s + 2) / 4)
	rss := uint8((r + 2*s + s + 2) / 4)
	sss := uint8(s)
	z.ybr[y+0][x+0] = pq
	z.ybr[y+0][x+1] = pqr
	z.ybr[y+0][x+2] = qr
	z.ybr[y+0][x+3] = qrs
	z.ybr[y+1][x+0] = qr
	z.ybr[y+1][x+1] = qrs
	z.ybr[y+1][x+2] = rs
	z.ybr[y+1][x+3] = rss
	z.ybr[y+2][x+0] = rs
	z.ybr[y+2][x+1] = rss
	z.ybr[y+2][x+2] = sss
	z.ybr[y+2][x+3] = sss
	z.ybr[y+3][x+0] = sss
	z.ybr[y+3][x+1] = sss
	z.ybr[y+3][x+2] = sss
	z.ybr[y+3][x+3] = sss
}

func predFunc8DC(z *Decoder, y, x int) {
	sum := uint32(8)
	for i := 0; i < 8; i++ {
		sum += uint32(z.ybr[y-1][x+i])
	}
	for j := 0; j < 8; j++ {
		sum += uint32(z.ybr[y+j][x-1])
	}
	avg := uint8(sum / 16)
	for j := 0; j < 8; j++ {
		for i := 0; i < 8; i++ {
			z.ybr[y+j][x+i] = avg
		}
	}
}

func predFunc8TM(z *Decoder, y, x int) {
	delta0 := -int32(z.ybr[y-1][x-1])
	for j := 0; j < 8; j++ {
		delta1 := delta0 + int32(z.ybr[y+j][x-1])
		for i := 0; i < 8; i++ {
			delta2 := delta1 + int32(z.ybr[y-1][x+i])
			z.ybr[y+j][x+i] = uint8(clip(delta2, 0, 255))
		}
	}
}

func predFunc8VE(z *Decoder, y, x int) {
	for j := 0; j < 8; j++ {
		for i := 0; i < 8; i++ {
			z.ybr[y+j][x+i] = z.ybr[y-1][x+i]
		}
	}
}

func predFunc8HE(z *Decoder, y, x int) {
	for j := 0; j < 8; j++ {
		for i := 0; i < 8; i++ {
			z.ybr[y+j][x+i] = z.ybr[y+j][x-1]
		}
	}
}

func predFunc8DCTop(z *Decoder, y, x int) {
	sum := uint32(4)
	for j := 0; j < 8; j++ {
		sum += uint32(z.ybr[y+j][x-1])
	}
	avg := uint8(sum / 8)
	for j := 0; j < 8; j++ {
		for i := 0; i < 8; i++ {
			z.ybr[y+j][x+i] = avg
		}
	}
}

func predFunc8DCLeft(z *Decoder, y, x int) {
	sum := uint32(4)
	for i := 0; i < 8; i++ {
		sum += uint32(z.ybr[y-1][x+i])
	}
	avg := uint8(sum / 8)
	for j := 0; j < 8; j++ {
		for i := 0; i < 8; i++ {
			z.ybr[y+j][x+i] = avg
		}
	}
}

func predFunc8DCTopLeft(z *Decoder, y, x int) {
	for j := 0; j < 8; j++ {
		for i := 0; i < 8; i++ {
			z.ybr[y+j][x+i] = 0x80
		}
	}
}

func predFunc16DC(z *Decoder, y, x int) {
	sum := uint32(16)
	for i := 0; i < 16; i++ {
		sum += uint32(z.ybr[y-1][x+i])
	}
	for j := 0; j < 16; j++ {
		sum += uint32(z.ybr[y+j][x-1])
	}
	avg := uint8(sum / 32)
	for j := 0; j < 16; j++ {
		for i := 0; i < 16; i++ {
			z.ybr[y+j][x+i] = avg
		}
	}
}

func predFunc16TM(z *Decoder, y, x int) {
	delta0 := -int32(z.ybr[y-1][x-1])
	for j := 0; j < 16; j++ {
		delta1 := delta0 + int32(z.ybr[y+j][x-1])
		for i := 0; i < 16; i++ {
			delta2 := delta1 + int32(z.ybr[y-1][x+i])
			z.ybr[y+j][x+i] = uint8(clip(delta2, 0, 255))
		}
	}
}

func predFunc16VE(z *Decoder, y, x int) {
	for j := 0; j < 16; j++ {
		for i := 0; i < 16; i++ {
			z.ybr[y+j][x+i] = z.ybr[y-1][x+i]
		}
	}
}

func predFunc16HE(z *Decoder, y, x int) {
	for j := 0; j < 16; j++ {
		for i := 0; i < 16; i++ {
			z.ybr[y+j][x+i] = z.ybr[y+j][x-1]
		}
	}
}

func predFunc16DCTop(z *Decoder, y, x int) {
	sum := uint32(8)
	for j := 0; j < 16; j++ {
		sum += uint32(z.ybr[y+j][x-1])
	}
	avg := uint8(sum / 16)
	for j := 0; j < 16; j++ {
		for i := 0; i < 16; i++ {
			z.ybr[y+j][x+i] = avg
		}
	}
}

func predFunc16DCLeft(z *Decoder, y, x int) {
	sum := uint32(8)
	for i := 0; i < 16; i++ {
		sum += uint32(z.ybr[y-1][x+i])
	}
	avg := uint8(sum / 16)
	for j := 0; j < 16; j++ {
		for i := 0; i < 16; i++ {
			z.ybr[y+j][x+i] = avg
		}
	}
}

func predFunc16DCTopLeft(z *Decoder, y, x int) {
	for j := 0; j < 16; j++ {
		for i := 0; i < 16; i++ {
			z.ybr[y+j][x+i] = 0x80
		}
	}
}