Learning Libopus (1) Encoder encoder

Source: Internet
Author: User
Tags int size shallow copy

From: Http://www.opus-codec.org/docs/html_api-1.1.0/group__opus__encoder.html#gaefeb7dc1d6e3b59dea5ea674c86e9c18


Detailed description

This page describes the use procedures and features for encoding.


Since Opus is a state codec, it is necessary to create an encoder before encoding. You can do this:


int error;
Opusencoder *enc;
ENC = opus_encoder_create (Fs, channels, application, &error);


From there, the enc can be used to encode the audio stream. This encoder cannot encode multiple streams at the same time (thread insecure). At the same time, when encoding multiple frames, the encoder does not need to repeat initialization.


When you call Opus_encoder_create (), the memory space is created, and it can initialize the pre-allocated memory:


int size;
int error;
Opusencoder *enc;
Size = opus_encoder_get_size (channels);
ENC = malloc (size);
Error = Opus_encoder_init (enc, Fs, channels, application);


Opus_encoder_get_size () returns the memory size required by the encoder. Please note that this size may be modified in future versions, so try not to use constants instead.


The encoder is assigned to a contiguous block of memory that can be obtained by a shallow copy (e.g. memcpy)


You can modify the internal settings of the encoder by Opus_encoder_ctl (). These settings use the recommended values by default. The following are the most common modifications:


Opus_encoder_ctl (Enc, opus_set_bitrate (bitrate));//baud rate
Opus_encoder_ctl (ENC, opus_set_complexity (complexity));//complexity
Opus_encoder_ctl (Enc, opus_set_signal (Signal_type));//Signal
Over here
Bitrate units are bits per second (b/s)


Complexity is a value from 1 to 10, where 1 is the lowest and 10 is the highest


Signal_type either Opus_auto (default), Opus_signal_voice or Opus_signal_music


See encoder-related CTL and common CTL for a complete list of settings. Most parameters can be set or modified when the stream is encoded.


Must be a complete audio data (2.5,5,10,20,40 or 60 milliseconds) when calling Opus_encode () or opus_encode_float ():


Len = Opus_encode (enc, audio_frame, frame_size, Packet, max_packet);
Over here


Audio_frame is opus_int16 audio data (or opus_encode_float ())


Frame_size is the duration of the sample in the frame (per channel)


Packet is an array of bytes that the audio data needs to write


Max_packet is the maximum number of bytes that can be written in the package (4000 bytes recommended). Do not control the bitrate of VBR via Max_packet, use opus_set_bitrate CTL if necessary.


Opus_encode () and Opus_encode_float () return the actual number of bytes written to the packet. The return value can be negative, which indicates an error has occurred. If the return value is 1 bytes, the grouping does not need to be sent (DTX).


When you need to release the encoder, you can pass:
Opus_encoder_destroy (ENC);


If the encoder is created with Opus_encoder_init () instead of opus_encoder_create (), nothing is done, only the pre-allocated memory is freed, and the call to free (ENC)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.