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