| 1 | package Peta::Vorbis::Encode::MDCT; | ||||||||||||||||||||||
| 2 | # ABSTRACT: Vorbis' MDCT through an FFT | ||||||||||||||||||||||
| 3 | |||||||||||||||||||||||
| 4 | # The MDCT of a Vorbis block of n samples: n/2 coefficients, | ||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||
| 6 | # X[k] = sum_t w[t] x[t] cos(2 pi / n (t + 1/2 + n/4)(k + 1/2)) * SCALE | ||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||
| 8 | # with Vorbis' window w (the sine of pi/2 times a squared sine), computed | ||||||||||||||||||||||
| 9 | # through a DCT-IV of n/2 points (the windowed block folded in half) and | ||||||||||||||||||||||
| 10 | # that through a complex FFT of n/4 points. | ||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||
| 12 | # SCALE makes the decoder's inverse give back the samples: 4 / n, as the | ||||||||||||||||||||||
| 13 | # inverse the decoders use is unscaled (measured with FFmpeg's decoder). | ||||||||||||||||||||||
| 14 | |||||||||||||||||||||||
| 15 | 1 | ▶ | use v5.36; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | 1 | our $VERSION = '0.2610180'; | |||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | 1 | ▶ | use constant PI => 3.141592653589793; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | # The window of a long block lapping long blocks on both sides, or of a | ||||||||||||||||||||||
| 22 | # short one: the slope sin(pi/2 sin^2((t + 1/2) / n pi)) over the whole | ||||||||||||||||||||||
| 23 | # block. (Long blocks next to short ones have narrower slopes: window().) | ||||||||||||||||||||||
| 24 | 15 | ▶ | sub window ($n, $left = $n, $right = $n) { | ||||||||||||||||||||
|
Subroutine
window: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||
| 25 | 15 | my $l0 = $n / 4 - $left / 4; # where the rising slope starts, | |||||||||||||||||||||
| 26 | 15 | my $ln = $left / 2; # and how long it is | |||||||||||||||||||||
| 27 | 15 | my $r0 = 3 * $n / 4 - $right / 4; # the falling slope | |||||||||||||||||||||
| 28 | 15 | my $rn = $right / 2; | |||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | 15 | my @w; | |||||||||||||||||||||
| 31 | 15 | for my $t (0 .. $n - 1) { | |||||||||||||||||||||
| 32 | 21568 | ▶ | push @w, | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 33 | $t < $l0 ? 0.0 | ||||||||||||||||||||||
| 34 | : $t < $l0 + $ln ? _slope(($t - $l0 + 0.5) / $ln * PI / 2) | ||||||||||||||||||||||
| 35 | : $t < $r0 ? 1.0 | ||||||||||||||||||||||
| 36 | : $t < $r0 + $rn ? _slope(($t - $r0 + 0.5) / $rn * PI / 2 + PI / 2) | ||||||||||||||||||||||
| 37 | : 0.0; | ||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||
| 39 | 15 | return \@w; | |||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||
| 41 | |||||||||||||||||||||||
| 42 | # Vorbis' slope, the sine of pi/2 times a squared sine. | ||||||||||||||||||||||
| 43 | 14400 | ▶ | sub _slope ($angle) { | ||||||||||||||||||||
|
Subroutine
_slope: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 44 | 14400 | my $s = sin($angle); | |||||||||||||||||||||
| 45 | 14400 | return sin(PI / 2 * $s * $s); | |||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | # The angles of the DCT-IV's pre- and post-rotations, for a block of $n: | ||||||||||||||||||||||
| 49 | # n/4 of each. | ||||||||||||||||||||||
| 50 | 6 | ▶ | sub pre_angles ($n) { return map { -PI * (4 * $_ + 1) / (2 * $n) } 0 .. $n / 4 - 1 } | ||||||||||||||||||||
|
Subroutine
pre_angles: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 51 | 6 | ▶ | sub post_angles ($n) { return map { -PI * $_ / ($n / 2) } 0 .. $n / 4 - 1 } | ||||||||||||||||||||
|
Subroutine
post_angles: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 52 | |||||||||||||||||||||||
| 53 | 1 | ▶ | sub new ($class, $n, $scale = 1.0) { | ||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 54 | 1 | my $q = $n / 4; | |||||||||||||||||||||
| 55 | 1 | my $bits = 0; | |||||||||||||||||||||
| 56 | 1 | ▶ | $bits++ while (1 << $bits) < $q; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 57 | |||||||||||||||||||||||
| 58 | # The rotations and twiddles as cosine and sine side by side. | ||||||||||||||||||||||
| 59 | return bless { | ||||||||||||||||||||||
| 60 | n => $n, | ||||||||||||||||||||||
| 61 | scale => $scale, | ||||||||||||||||||||||
| 62 | 16 | pre => [ map { (cos($_), sin($_)) } pre_angles($n) ], | |||||||||||||||||||||
| 63 | 16 | post => [ map { (cos($_), sin($_)) } post_angles($n) ], | |||||||||||||||||||||
| 64 | 16 | reverse => [ map { _reversed($_, $bits) } 0 .. $q - 1 ], # the FFT's input order | |||||||||||||||||||||
| 65 | 1 | twiddle => [ map { (cos($_), sin($_)) } map { -2 * PI * $_ / $q } 0 .. $q / 2 - 1 ], | |||||||||||||||||||||
| 66 | }, $class; | ||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | # $v with its low $bits bits in reverse order. | ||||||||||||||||||||||
| 70 | 16 | ▶ | sub _reversed ($v, $bits) { | ||||||||||||||||||||
|
Subroutine
_reversed: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 71 | 16 | my $r = 0; | |||||||||||||||||||||
| 72 | 16 | for (1 .. $bits) { | |||||||||||||||||||||
| 73 | 64 | $r = ($r << 1) | ($v & 1); | |||||||||||||||||||||
| 74 | 64 | $v >>= 1; | |||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||
| 76 | 16 | return $r; | |||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||
| 78 | |||||||||||||||||||||||
| 79 | # The coefficients of each channel's block of n samples, all windowed with | ||||||||||||||||||||||
| 80 | # $w: one array reference per channel. | ||||||||||||||||||||||
| 81 | 0 | ▶ | sub transform_all ($self, $channels, $w) { return map { $self->transform($_, $w) } @$channels } | ||||||||||||||||||||
|
Subroutine
transform_all: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 82 | |||||||||||||||||||||||
| 83 | # The coefficients of a block of n samples, windowed with $w. | ||||||||||||||||||||||
| 84 | 2 | ▶ | sub transform ($self, $x, $w) { | ||||||||||||||||||||
|
Subroutine
transform: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||
| 85 | 2 | my $n = $self->{n}; | |||||||||||||||||||||
| 86 | 2 | my $m = $n / 2; | |||||||||||||||||||||
| 87 | 2 | my $h = $m / 2; # = n/4 | |||||||||||||||||||||
| 88 | 2 | my $scale = $self->{scale}; | |||||||||||||||||||||
| 89 | |||||||||||||||||||||||
| 90 | # Windowed, and folded into the DCT-IV's m inputs. | ||||||||||||||||||||||
| 91 | 2 | my @v = (0.0) x $m; | |||||||||||||||||||||
| 92 | 2 | for my $t (0 .. $h - 1) { | |||||||||||||||||||||
| 93 | 32 | my $a = 3 * $h - 1 - $t; | |||||||||||||||||||||
| 94 | 32 | my $b = 3 * $h + $t; | |||||||||||||||||||||
| 95 | 32 | $v[$t] = -$x->[$a] * $w->[$a] - $x->[$b] * $w->[$b]; | |||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||
| 97 | 2 | for my $t ($h .. $m - 1) { | |||||||||||||||||||||
| 98 | 32 | my $a = $t - $h; | |||||||||||||||||||||
| 99 | 32 | my $b = 3 * $h - 1 - $t; | |||||||||||||||||||||
| 100 | 32 | $v[$t] = $x->[$a] * $w->[$a] - $x->[$b] * $w->[$b]; | |||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | # Pre-rotation into n/4 complex values, in bit-reversed order. | ||||||||||||||||||||||
| 104 | 2 | my @pre = @{ $self->{pre} }; | |||||||||||||||||||||
| 105 | 2 | my @rev = @{ $self->{reverse} }; | |||||||||||||||||||||
| 106 | 2 | my @re = (0.0) x $h; | |||||||||||||||||||||
| 107 | 2 | my @im = (0.0) x $h; | |||||||||||||||||||||
| 108 | 2 | for my $k (0 .. $h - 1) { | |||||||||||||||||||||
| 109 | 32 | my $a = $v[ 2 * $k ]; | |||||||||||||||||||||
| 110 | 32 | my $b = $v[ $m - 1 - 2 * $k ]; | |||||||||||||||||||||
| 111 | 32 | my $c = $pre[ 2 * $k ]; | |||||||||||||||||||||
| 112 | 32 | my $s = $pre[ 2 * $k + 1 ]; | |||||||||||||||||||||
| 113 | 32 | my $at = $rev[$k]; | |||||||||||||||||||||
| 114 | 32 | $re[$at] = $a * $c - $b * $s; | |||||||||||||||||||||
| 115 | 32 | $im[$at] = $a * $s + $b * $c; | |||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||
| 117 | |||||||||||||||||||||||
| 118 | # The FFT: each stage one flat loop over its butterflies. | ||||||||||||||||||||||
| 119 | 2 | my @tw = @{ $self->{twiddle} }; | |||||||||||||||||||||
| 120 | 2 | ▶ | for (my $len = 2; $len <= $h; $len *= 2) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 121 | 8 | my $half = $len / 2; | |||||||||||||||||||||
| 122 | 8 | my $stride = $h / $len; | |||||||||||||||||||||
| 123 | 8 | my ($s, $k) = (0, 0); | |||||||||||||||||||||
| 124 | 8 | for my $i (0 .. $h / 2 - 1) { | |||||||||||||||||||||
| 125 | 64 | my $a = $s + $k; | |||||||||||||||||||||
| 126 | 64 | my $b = $a + $half; | |||||||||||||||||||||
| 127 | 64 | my $wr = $tw[ 2 * $k * $stride ]; | |||||||||||||||||||||
| 128 | 64 | my $wi = $tw[ 2 * $k * $stride + 1 ]; | |||||||||||||||||||||
| 129 | 64 | my $tr = $re[$b] * $wr - $im[$b] * $wi; | |||||||||||||||||||||
| 130 | 64 | my $ti = $re[$b] * $wi + $im[$b] * $wr; | |||||||||||||||||||||
| 131 | 64 | $re[$b] = $re[$a] - $tr; | |||||||||||||||||||||
| 132 | 64 | $im[$b] = $im[$a] - $ti; | |||||||||||||||||||||
| 133 | 64 | $re[$a] += $tr; | |||||||||||||||||||||
| 134 | 64 | $im[$a] += $ti; | |||||||||||||||||||||
| 135 | 64 | $k++; | |||||||||||||||||||||
| 136 | 64 | ▶ | if ($k == $half) { $k = 0; $s += $len } | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||
| 139 | |||||||||||||||||||||||
| 140 | # Post-rotation: the DCT-IV's outputs, even ones forward, odd ones back. | ||||||||||||||||||||||
| 141 | 2 | my @post = @{ $self->{post} }; | |||||||||||||||||||||
| 142 | 2 | my @out = (0.0) x $m; | |||||||||||||||||||||
| 143 | 2 | for my $k (0 .. $h - 1) { | |||||||||||||||||||||
| 144 | 32 | my $c = $post[ 2 * $k ]; | |||||||||||||||||||||
| 145 | 32 | my $s = $post[ 2 * $k + 1 ]; | |||||||||||||||||||||
| 146 | 32 | $out[ 2 * $k ] = ($re[$k] * $c - $im[$k] * $s) * $scale; | |||||||||||||||||||||
| 147 | 32 | $out[ $m - 1 - 2 * $k ] = -($re[$k] * $s + $im[$k] * $c) * $scale; | |||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||
| 149 | 2 | return \@out; | |||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||
| 151 | |||||||||||||||||||||||
| 152 | 1 | 1; | |||||||||||||||||||||
| 153 | |||||||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||||