CM OPUS Decode

CM OPUS Decode

The cmOpusDecode is used to decode the audio/x-opus data to audio/x-raw(PCM) data by the OPUS library.


API Instructions

create/destroy

gpointer cm_opus_decode_create(); void cm_opus_decode_destroy(gpointer hd);

start/stop

void cm_opus_decode_start(gpointer hd); void cm_opus_decode_stop(gpointer hd);

parameters

Set the opus decode option. The default rate of the output data is 48000hz, if need other formats, set the caps str1 that decode supports.

/* @caps_str0 The input fomat, "audio/x-opus,channel-mapping-family=0,..." */ void cm_opus_decode_set_caps_str0(gpointer hd, const gchar* caps_str0); /* @caps_str1 The output fomat, "audio/x-raw,rate=x,..." */ void cm_opus_decode_set_caps_str1(gpointer hd, const gchar* caps_str1); /* @return The output format, "audio/x-raw,..." */ const char* cm_opus_decode_get_caps_str(gpointer had); /*The decode parse the audio/x-raw media information done*/ gboolean cm_opus_decode_is_ready(gpointer hd);

data

Use the way 1 or way 2 to input opus data.

// data input way 1, push buffer directly / /* @buffer Opus buffer */ gboolean cm_opus_decode_push_buffer(gpointer hd, GstBuffer *buffer); // data input way 2, link to source to get data // /* @src An audio/x-raw source(cmAlsaSrc, ...) */ void cm_opus_encode_link_to_source(gpointer hd, gpointer src); void cm_opus_encode_unlink(gpointer hd); /* @ cb data callback to get data */ void cm_opus_encode_set_data_callback(gpointerhd, cm_data_cb_ptrcb, gpointeruser_data); void cm_opus_encode_remove_data_callback(gpointerhd, cm_data_cb_ptrcb, gpointeruser_data);

others

GstElement* cm_opus_decode_get_bin(gpointer hd);

Demo

Opus PV to PB Demo

Structure

Get opus data and decode it to PCM data.

image-20240520-072042.png

Main codes

static GstFlowReturn _out_data_cb (GstBuffer *buffer, gpointer user_data) { ...... return GST_FLOW_OK; } static int idx = 0; static gboolean stop = FALSE; static gboolean _opus_feed_loop(gpointer arg){ if(opus_dec && !stop){ GstBuffer *buffer = NULL; if(input){ char name[16] = {0}; sprintf(name, input, idx++); FILE *fd = fopen(name, "rb"); gst_print("open %s\n", name); if(fd){ char data[256] = {0}; int len = fread(data, 1, 256, fd); fclose(fd); //data from file buffer = gst_buffer_new_allocate (NULL, len, NULL); gst_buffer_fill(buffer, 0, data, len); } else{ gst_print("read end\n"); idx = 0; stop = TRUE; } } else{ //data from cache buffer = gst_buffer_new_allocate (NULL, opus_frame_size, NULL); gst_buffer_fill(buffer, 0, opus_frame_data, opus_frame_size); } if(buffer){ //Set PTS and DTS GST_BUFFER_PTS(buffer) = gst_util_get_timestamp(); GST_BUFFER_DTS(buffer) = GST_BUFFER_PTS(buffer); //gst_buffer_info_dump(buffer, "OPUS_DEC", FALSE); //Push data to decode cm_opus_decode_push_buffer(opus_dec, buffer); gst_buffer_unref(buffer); } } return TRUE; } static gboolean _main_loop(gpointer arg) { if(!opus_dec){ //Create decode opus_dec = cm_opus_decode_create(); //Set output call back cm_opus_decode_set_data_callback(opus_dec , _out_data_cb, opus_dec); //Start cm_opus_decode_start(opus_dec); stop = FALSE; } ...... return TRUE; }

For more details please refer to the demo file.
Test result

opusdec-20240520-093316.png

The PCM data can be played on the PC.