HarmonyOS Next – OPUS Audio Encoding in Audio and Video in Practice
Background
In the requirement of sending brief voice messages in chat eventualities, it’s essential to carry out encoding and compression on the despatched audio content material. Initially, the MP3 encoder was used for compression. Later, because the voice messages had been for use for the coaching of the ASR mannequin, the OPUS encoder was required to course of voice indicators. Previously, Android didn’t assist MP3 and OPUS encoding. Currently, HarmonyOS gives assist for each MP3 and OPUS encoding. The varieties of encoders supported by HarmonyOS are as follows:
| Container Specification | Audio Encoding Type |
|---|---|
| mp4 | AAC, Flac |
| m4a | AAC |
| flac | Flac |
| aac | AAC |
| mp3 | MP3 |
| uncooked | G711mu |
| amr | AMR |
| ogg | opus |
After performing opus encoding via the system API, it was discovered that the encoded audio file couldn’t be performed. By checking the file, it was found that the encoder didn’t mechanically carry out Muxer. By checking the system API, it was discovered that the system Muxer doesn’t assist the ogg container. The at present supported encapsulation capabilities are as follows:
| Encapsulation Format | Video Codec Type | Audio Codec Type | Cover Type |
|---|---|---|---|
| mp4 | AVC (H.264), HEVC (H.265) | AAC, MPEG (MP3) | jpeg, png, bmp |
| m4a | – | AAC | jpeg, png, bmp |
| mp3 | – | MPEG (MP3) | – |
Since the HarmonyOS system doesn’t assist the ogg container, it’s essential to implement the container by oneself.
Introduction to opus ogg Encapsulation
Ogg organizes and hyperlinks logical streams in items of pages. Each web page has a web page header and web page knowledge. The web page header has the next definitions:
- capture_pattern Page Identifier: ASCII characters, 0x4f ‘O’ 0x67 ‘g’ 0x67 ‘g’ 0x53 ‘S’, with a measurement of 4 bytes. It marks the start of a web page.
- stream_structure_version Version ID: Generally, the present model is defaulted to 0, with a measurement of 1 byte.
-
header_type_flag Type Identifier: Identifies the kind of the present web page, with a measurement of 1 byte.
- 0x01: The media encoding knowledge on this web page and the earlier web page belong to the identical packet of the identical logical stream. If this bit just isn’t set, it implies that this web page begins with a brand new packet.
- 0x02: Indicates that this web page is the primary web page of the logical stream, the bos identifier. If this bit just isn’t set, it means it isn’t the primary web page.
- 0x04: Indicates that this web page is the final web page of the logical stream, the eos identifier. If this bit just isn’t set, it means this web page just isn’t the final web page.
- granule_position: Parameter info associated to media encoding, with a measurement of 8 bytes. For audio streams, it shops the variety of sampling codes within the PCM output of the logical stream as much as this web page, and the timestamp will be calculated from it. For video streams, it shops the variety of encoded video frames as much as this web page. If this worth is -1, it implies that as much as this web page, the packet of the logical stream has not ended. (Little-endian)
- serial_number: The ID of the stream within the present web page, with a measurement of 4 bytes. It is the sequence quantity that distinguishes the logical stream to which this web page belongs from different logical streams. We can use this worth to divide the streams. (Little-endian)
- page_seguence_number: The sequence variety of this web page within the logical stream, with a measurement of 4 bytes.
- CRC_cbecksum: Cyclic redundancy test code for verifying the validity of every web page, with a measurement of 4 bytes.
- number_page_segments: Given the variety of segments that seem within the segment_table area of this web page, with a measurement of 1 byte.
- segment_table: Literally, it’s a desk representing the size of every section, with a price vary of 0 – 255. The packet worth will be obtained from the section (1 section is 1 byte). The measurement of every packet ends with the final section that isn’t equal to 255. From the segment_table within the web page header, the size of every packet will be obtained. For instance, if a bunch of segments are within the order of FF 45 FF FF FF 40 FF 05 FF FF FF 66 (a complete of 4 packets, containing 12 segments, and the size of every packet is: FF 45【324】; FF FF FF 40【829】; FF 05【260】; FF FF FF 66【847】), then the size of the primary packet is 255 + 69 = 324, the scale of the second packet is 829, and so forth.
Basically, the web page header consists of the above parameters. From this, we will receive the size of the web page header and the size of your complete web page:
header_size = 27 + number_page_segments ; (byte)
page_size = header_size + the scale of every section within the segment_table;
The format of the web page header:
Implementing ogg Encapsulation
Xiph gives the open-source implementation libopusenc for opus ogg encapsulation. However, libopusenc is determined by the libopus library. So, to make use of libopusenc to implement ogg container encapsulation, a easy method is to instantly implement opus encoding and container encapsulation based mostly on libopusenc.
The processing movement of libopusenc is as follows:
Creating the Encoder
First, create feedback:
OggOpusComments *feedback = ope_comments_create();
ope_comments_add(feedback, "ARTIST", "qingkouwei");
ope_comments_add(feedback, "TITLE", "qingkouwei-im");
You can customise some descriptions for the audio file. Next, create the encoder:
OggOpusEnc *pEnc = ope_encoder_create_file(outputFilePath_, feedback, inSamplerate, inChannel,
high quality, &error);
if (pEnc) {
int ret = ope_encoder_ctl(pEnc, OPUS_SET_BITRATE(outBitrate));
}
The parameters embrace the output file path of the encoding multiplexer, remark info, in addition to the sampling charge, variety of channels, audio high quality, and different info.
Encoding PCM Data
static napi_value encodePCMToOpusOggNative(napi_env env, napi_callback_info data)
{
size_t argc = 1;
napi_value args[1] = {nullptr};
napi_get_cb_info(env, data, &argc, args, nullptr, nullptr);
void* inputBuffer;
size_t inputLength;
napi_get_arraybuffer_info(env, args[0], &inputBuffer, &inputLength);
ope_encoder_write(pEnc, (brief *)inputBuffer, inputLength/2);
return nullptr;
}
Pass the binary knowledge on the TS layer to the ope_encoder_write perform. The ope_encoder_write perform will write the encoded knowledge to the trail specified when the encoder was created.
Closing the Encoding Multiplexer
static napi_value closeOpusOggEncoderNative(napi_env env, napi_callback_info data)
{
ope_encoder_drain(pEnc);
ope_encoder_destroy(pEnc);
if(feedback!= NULL){
ope_comments_destroy(feedback);
feedback = NULL;
}
return nullptr;
}
Release objects akin to enc and feedback. The total course of is comparatively easy. Finally, the information within the ogg container file will be performed usually utilizing a typical participant:
Summary
This article launched the strategy of implementing OPUS encoding and performing OGG container encapsulation in HarmonyOS, fixing the particular necessities of the enterprise situation for audio encoding.
HI-FI News
through DEV Community https://dev.to
December 25, 2024 at 08:18AM
-
Product on sale
Audiophile Vinyl Records Cleaning BundleOriginal price was: €69.90.€59.90Current price is: €59.90. incl. VAT -
Product on sale
Easy Start Vinyl Records Cleaning KitOriginal price was: €69.90.€59.90Current price is: €59.90. incl. VAT -
Vinyl Records Cleaner Easy Groove Concentrate€34.95 incl. VAT -
Easy Groove Super Set€199.00 incl. VAT -
Easy Groove Enzycaster – vinyl records prewash cleaner€34.95 incl. VAT -
Easy Groove Extreme MK. II- vinyl records prewash cleaner€45.00 incl. VAT -
Vinyl Records Cleaner – Easy Groove Virgin Concentrate€35.00 incl. VAT -
Easy Groove Spray&Wipe vinyl records cleaner€34.95 incl. VAT










