ws-apifix: one bitrate unit: every encoder's bitrate option is bit/s (MP3 now too), values below 1000 are kbit/s (the old MP3 values keep working; Opus accepts them too); the MP3 Decoder's bitrate field is bit/s as the Decoder contract says (was kbit/s)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 14:06:51 -05:00
1 parent c4168d751f
commit 51630729f6
10 files changed
+35 -14

No files matched your search

+1 -1
View File
@@ -68,7 +68,7 @@ function Stream.new(b: buffer, idx: any, tags: { [string]: string }, id3: any):
channels = nch,
frames = total,
duration = total / hz,
bitrate = if idx.n > 0 then floor(idx.bytes * 8 * hz / (idx.n * spf) / 1000 + 0.5) else 0,
bitrate = if idx.n > 0 then floor(idx.bytes * 8 * hz / (idx.n * spf) + 0.5) else 0, -- bit/s
tags = tags,
id3 = id3,
layer = idx.layer,
+7 -2
View File
@@ -44,7 +44,8 @@ export type OpenOptions = {
export type Decoder = Stream.Decoder
export type EncodeOptions = {
layer: number?, -- 3 (MP3, default) or 2 (MP2)
bitrate: number?, -- kbps, constant (default 128 for layer III, 192 for layer II; MPEG-2 / 2.5 rates: 64 / 96)
bitrate: number?, -- bits per second, constant: one of the layer's rates (default 128000 for layer III, 192000 for
-- layer II; MPEG-2 / 2.5 rates: 64000 / 96000); values below 1000 are kbit/s (128 = 128000, the old unit)
mode: ("stereo" | "joint" | "mono")?, -- default "joint" (mid / side) for stereo layer III, "stereo" for layer II,
-- "mono" for mono input; layer II has no joint stereo (intensity) and writes "joint" as "stereo"
id3: boolean?, -- write an ID3v2.4 tag from the tags (default true when there are tags)
@@ -208,9 +209,13 @@ function Mp3.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
if layer == 2 and mode == "joint" then
mode = "stereo"
end
-- bitrate: bit/s, or kbit/s below 1000; the encoder works in kbit/s
local bitrate = o.bitrate or (if mpeg1 then (if layer == 3 then 128 else 192) else (if layer == 3 then 64 else 96))
if type(bitrate) == "number" and bitrate >= 1000 then
bitrate /= 1000
end
if not table.find(Enc.BITRATES[if mpeg1 then 1 else 2], bitrate) then
return nil, Err.new(Err.ARG, string.format("%s kbps is not a layer %d bit rate at %d Hz", tostring(bitrate), layer, rate))
return nil, Err.new(Err.ARG, string.format("%s bit/s is not a layer %d bit rate at %d Hz", tostring(if type(o.bitrate) == "number" and o.bitrate < 1000 then o.bitrate * 1000 else o.bitrate), layer, rate))
end
if layer == 2 and mpeg1 then
-- the standard's allowed bit rate / mode combinations
+5 -1
View File
@@ -15,7 +15,8 @@ local CeltEncoder = require("@formats/codecs/audio/Opus/CeltEncoder")
local Encode = {}
export type Options = {
bitrate: number?, -- bits per second (default 64000 per channel; 6000 .. 510000, constant)
bitrate: number?, -- bits per second (default 64000 per channel; 6000 .. 510000, constant); values below 1000
-- are kbit/s (96 = 96000), as for every encoder's bitrate option
tags: { [string]: string }?, -- default: the audio's tags
vendor: string?, -- OpusTags vendor (default "formats-luau")
serial: number?, -- Ogg stream serial (default 0x4f707573)
@@ -39,6 +40,9 @@ function Encode.encode(value: any, opts: Options?): (buffer?, Err.Err?)
return nil, Err.new(Err.ARG, "bad sample rate " .. tostring(rate))
end
local bitrate = o.bitrate or 64000 * ch
if type(bitrate) == "number" and bitrate > 0 and bitrate < 1000 then
bitrate *= 1000 -- kbit/s
end
if type(bitrate) ~= "number" or bitrate < 6000 or bitrate > 510000 then
return nil, Err.new(Err.ARG, "bitrate 6000 .. 510000")
end