cannam@86: cannam@86: cannam@86:
cannam@86: cannam@86: cannam@86:The equations below are used in multiple places by the Vorbis codec cannam@86: specification. Rather than cluttering up the main specification cannam@86: documents, they are defined here and linked in the main documents cannam@86: where appropriate.
cannam@86: cannam@86:The "ilog(x)" function returns the position number (1 through n) of the cannam@86: highest set bit in the two's complement integer value cannam@86: [x]. Values of [x] less than zero are defined to return zero.
cannam@86: cannam@86:cannam@86: 1) [return_value] = 0; cannam@86: 2) if ( [x] is greater than zero ){ cannam@86: cannam@86: 3) increment [return_value]; cannam@86: 4) logical shift [x] one bit to the right, padding the MSb with zero cannam@86: 5) repeat at step 2) cannam@86: cannam@86: } cannam@86: cannam@86: 6) done cannam@86:cannam@86: cannam@86:
Examples:
cannam@86: cannam@86:"float32_unpack(x)" is intended to translate the packed binary cannam@86: representation of a Vorbis codebook float value into the cannam@86: representation used by the decoder for floating point numbers. For cannam@86: purposes of this example, we will unpack a Vorbis float32 into a cannam@86: host-native floating point number.
cannam@86: cannam@86:cannam@86: 1) [mantissa] = [x] bitwise AND 0x1fffff (unsigned result) cannam@86: 2) [sign] = [x] bitwise AND 0x80000000 (unsigned result) cannam@86: 3) [exponent] = ( [x] bitwise AND 0x7fe00000) shifted right 21 bits (unsigned result) cannam@86: 4) if ( [sign] is nonzero ) then negate [mantissa] cannam@86: 5) return [mantissa] * ( 2 ^ ( [exponent] - 788 ) ) cannam@86:cannam@86: cannam@86:
"lookup1_values(codebook_entries,codebook_dimensions)" is used to cannam@86: compute the correct length of the value index for a codebook VQ lookup cannam@86: table of lookup type 1. The values on this list are permuted to cannam@86: construct the VQ vector lookup table of size cannam@86: [codebook_entries].
cannam@86: cannam@86:The return value for this function is defined to be 'the greatest cannam@86: integer value for which [return_value] to the power of cannam@86: [codebook_dimensions] is less than or equal to cannam@86: [codebook_entries]'.
cannam@86: cannam@86:"low_neighbor(v,x)" finds the position n in vector [v] of cannam@86: the greatest value scalar element for which n is less than cannam@86: [x] and vector [v] element n is less cannam@86: than vector [v] element [x].
cannam@86: cannam@86:"high_neighbor(v,x)" finds the position n in vector [v] of cannam@86: the lowest value scalar element for which n is less than cannam@86: [x] and vector [v] element n is greater cannam@86: than vector [v] element [x].
cannam@86: cannam@86:"render_point(x0,y0,x1,y1,X)" is used to find the Y value at point X cannam@86: along the line specified by x0, x1, y0 and y1. This function uses an cannam@86: integer algorithm to solve for the point directly without calculating cannam@86: intervening values along the line.
cannam@86: cannam@86:cannam@86: 1) [dy] = [y1] - [y0] cannam@86: 2) [adx] = [x1] - [x0] cannam@86: 3) [ady] = absolute value of [dy] cannam@86: 4) [err] = [ady] * ([X] - [x0]) cannam@86: 5) [off] = [err] / [adx] using integer division cannam@86: 6) if ( [dy] is less than zero ) { cannam@86: cannam@86: 7) [Y] = [y0] - [off] cannam@86: cannam@86: } else { cannam@86: cannam@86: 8) [Y] = [y0] + [off] cannam@86: cannam@86: } cannam@86: cannam@86: 9) done cannam@86:cannam@86: cannam@86:
Floor decode type one uses the integer line drawing algorithm of cannam@86: "render_line(x0, y0, x1, y1, v)" to construct an integer floor cannam@86: curve for contiguous piecewise line segments. Note that it has not cannam@86: been relevant elsewhere, but here we must define integer division as cannam@86: rounding division of both positive and negative numbers toward zero.
cannam@86: cannam@86:cannam@86: 1) [dy] = [y1] - [y0] cannam@86: 2) [adx] = [x1] - [x0] cannam@86: 3) [ady] = absolute value of [dy] cannam@86: 4) [base] = [dy] / [adx] using integer division cannam@86: 5) [x] = [x0] cannam@86: 6) [y] = [y0] cannam@86: 7) [err] = 0 cannam@86: cannam@86: 8) if ( [dy] is less than 0 ) { cannam@86: cannam@86: 9) [sy] = [base] - 1 cannam@86: cannam@86: } else { cannam@86: cannam@86: 10) [sy] = [base] + 1 cannam@86: cannam@86: } cannam@86: cannam@86: 11) [ady] = [ady] - (absolute value of [base]) * [adx] cannam@86: 12) vector [v] element [x] = [y] cannam@86: cannam@86: 13) iterate [x] over the range [x0]+1 ... [x1]-1 { cannam@86: cannam@86: 14) [err] = [err] + [ady]; cannam@86: 15) if ( [err] >= [adx] ) { cannam@86: cannam@86: 15) [err] = [err] - [adx] cannam@86: 16) [y] = [y] + [sy] cannam@86: cannam@86: } else { cannam@86: cannam@86: 17) [y] = [y] + [base] cannam@86: cannam@86: } cannam@86: cannam@86: 18) vector [v] element [x] = [y] cannam@86: cannam@86: } cannam@86:cannam@86: cannam@86: