edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching0 forksRust18.3 MiBf7fa0feTahinli25 days ago1027 commits
| 1 | //! Writes interleaved PCM as a standalone `.opus` file: Opus in Ogg, RFC 7845. |
| 2 | //! `ec-opus` encodes, `ec-ogg` pages. |
| 3 | //! |
| 4 | //! What the file states, and why each number is what it is: |
| 5 | //! |
| 6 | //! - **`OpusHead`**: the channel count; the pre-skip (the encoder's look-ahead |
| 7 | //! plus one warm-up frame, see [`write`]); the input rate; mapping family 0 |
| 8 | //! for mono and stereo, family 1 (Vorbis channel order, with the stream and |
| 9 | //! coupled counts and the table) for 3 to 8 channels. |
| 10 | //! - **`OpusTags`**: the vendor, an `ENCODER` tag, the language where the mix |
| 11 | //! has one, and the chapters as the `CHAPTERxxx` / `CHAPTERxxxNAME` comments |
| 12 | //! ffmpeg writes and reads. |
| 13 | //! - **Granule positions** at 48 kHz with the pre-skip counted, the last one |
| 14 | //! trimming the final frame's padding, so the duration is exactly the |
| 15 | //! samples handed in. |
| 16 | |
| 17 | use File; |
| 18 | use BufWriter; |
| 19 | use Path; |
| 20 | |
| 21 | use ; |
| 22 | use ; |
| 23 | |
| 24 | /// The only clock an Opus stream has (RFC 7845 §4). |
| 25 | pub const RATE: u32 = 48_000; |
| 26 | |
| 27 | /// 20 ms at [`RATE`]: the frame every packet codes. |
| 28 | const FRAME: usize = 960; |
| 29 | |
| 30 | /// The rates RFC 6716 gives an Opus stream, in kbps, that a file is clamped into. |
| 31 | pub const MIN_KBPS: u32 = 6; |
| 32 | pub const MAX_KBPS: u32 = 510; |
| 33 | |
| 34 | /// What a file with no rate asked for is coded at: transparent for stereo |
| 35 | /// music, and half the AAC default, because Opus is that much better at it. |
| 36 | pub const DEFAULT_KBPS: u32 = 128; |
| 37 | |
| 38 | /// The vendor string in `OpusTags`. |
| 39 | const VENDOR: &str = "ec-opus"; |
| 40 | |
| 41 | /// What travels in `OpusTags` beside the vendor. |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | /// Where WAV/film channel `c` sits in Vorbis order, per channel count: the |
| 51 | /// order family 1 codes in (RFC 7845 §5.1.1.2). `vorbis[i] = wav[ORDER[i]]`. |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | /// `OpusTags` with the vendor, `ENCODER`, `LANGUAGE` and the chapters. |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | /// Encode `pcm` (interleaved, `channels` wide in WAV/film order, at [`RATE`]) |
| 89 | /// at `kbps` total and write the file to `out`. `input_rate` is what the |
| 90 | /// header reports as the rate the sound arrived at. `tick` runs once per |
| 91 | /// packet with the fraction done, and an `Err` from it stops the write (the |
| 92 | /// export's cancel). |
| 93 | /// |
| 94 | /// Every stream is pinned to CELT: its look-ahead is one fixed number, so the |
| 95 | /// pre-skip holds for the whole file and every stream of a surround one lines |
| 96 | /// up with the others. The first frame is coded twice -- once to warm the |
| 97 | /// encoder, which has no history behind it and rings, and once for real -- |
| 98 | /// and the warm-up is inside the pre-skip, exactly what pre-skip is for. |
| 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 |