xiph / opus-tools

A set of tools to encode, inspect, and decode audio in the Opus format.
https://opus-codec.org/
Other
212 stars 78 forks source link

Encoding multiple frames at once as oppose to per-frame encoding to save user<>kernel switches resulting from many `fread(...)` calls #88

Open vadimkantorov opened 7 months ago

vadimkantorov commented 7 months ago

https://github.com/xiph/opus-tools/blob/master/src/opusenc.c#L1116-L1119 :

/*Main encoding loop (one frame per iteration)*/
  while (1) {
    nb_samples = inopt.read_samples(inopt.readdata,input,frame_size);
    ret = ope_encoder_write_float(enc, input, nb_samples);

Currently read_samples would do an fread(...) call for every frame, and IIUC each such call results in a read-syscall and user<>kernel switch. I wonder if it would speed up if a large buffer is first read and then encoded at once - the underlying libopus API seems to support encoding multiple frames at once.

Would it be faster to do this?

If so, maybe a bufsize option can be introduced or even a command line switch for a regime where whole input is read in-memory / or input is open as mmap.

Also every read_samples/wav_read call alloca-tes a buffer dynamically for every frame.