1212#include <sof/audio/format.h>
1313#include <sof/audio/pipeline.h>
1414#include <sof/audio/rtnr/rtnr.h>
15+ #include <sof/audio/sink_source_utils.h>
1516#include <sof/common.h>
1617#include <rtos/panic.h>
1718#include <sof/ipc/msg.h>
@@ -305,8 +306,8 @@ static int rtnr_free(struct processing_module *mod)
305306}
306307
307308/* Check component audio stream parameters */
308- static int rtnr_check_params (struct processing_module * mod , struct audio_stream * source ,
309- struct audio_stream * sink )
309+ static int rtnr_check_params (struct processing_module * mod , struct sof_source * source ,
310+ struct sof_sink * sink )
310311{
311312 struct comp_dev * dev = mod -> dev ;
312313 struct comp_data * cd = module_get_private_data (mod );
@@ -315,11 +316,11 @@ static int rtnr_check_params(struct processing_module *mod, struct audio_stream
315316 comp_info (dev , "entry" );
316317
317318 /* set source/sink_frames/rate */
318- cd -> source_rate = audio_stream_get_rate (source );
319- cd -> sink_rate = audio_stream_get_rate (sink );
320- cd -> sources_stream [0 ].rate = audio_stream_get_rate ( source ) ;
321- cd -> sink_stream .rate = audio_stream_get_rate ( sink ) ;
322- channels_valid = audio_stream_get_channels (source ) == audio_stream_get_channels (sink );
319+ cd -> source_rate = source_get_rate (source );
320+ cd -> sink_rate = sink_get_rate (sink );
321+ cd -> sources_stream [0 ].rate = cd -> source_rate ;
322+ cd -> sink_stream .rate = cd -> sink_rate ;
323+ channels_valid = source_get_channels (source ) == sink_get_channels (sink );
323324
324325 if (!cd -> sink_rate ) {
325326 comp_err (dev , "rtnr_nr_params(), zero sink rate" );
@@ -346,14 +347,18 @@ static int rtnr_check_params(struct processing_module *mod, struct audio_stream
346347 }
347348
348349 /* set source/sink stream channels */
349- cd -> sources_stream [0 ].channels = audio_stream_get_channels (source );
350- cd -> sink_stream .channels = audio_stream_get_channels (sink );
350+ cd -> sources_stream [0 ].channels = source_get_channels (source );
351+ cd -> sink_stream .channels = sink_get_channels (sink );
351352
352- /* set source/sink stream overrun/underrun permitted */
353- cd -> sources_stream [0 ].overrun_permitted = audio_stream_get_overrun (source );
354- cd -> sink_stream .overrun_permitted = audio_stream_get_overrun (sink );
355- cd -> sources_stream [0 ].underrun_permitted = audio_stream_get_underrun (source );
356- cd -> sink_stream .underrun_permitted = audio_stream_get_underrun (sink );
353+ /*
354+ * Set source/sink stream overrun/underrun permitted. The sink/source API exposes only
355+ * the direction-relevant flag (a reader can underrun, a writer can overrun); the
356+ * cross-direction flags have no getter and are hardcoded to false.
357+ */
358+ cd -> sources_stream [0 ].overrun_permitted = false;
359+ cd -> sink_stream .overrun_permitted = sink_get_overrun (sink );
360+ cd -> sources_stream [0 ].underrun_permitted = source_get_underrun (source );
361+ cd -> sink_stream .underrun_permitted = false;
357362
358363 return 0 ;
359364}
@@ -691,41 +696,76 @@ static int rtnr_set_config(struct processing_module *mod, uint32_t param_id,
691696#endif
692697}
693698
694- void rtnr_copy_from_sof_stream (struct audio_stream_rtnr * dst , struct audio_stream * src )
699+ /*
700+ * Acquire the source's circular buffer and describe it for the RTNR library, which does its
701+ * own circular-buffer arithmetic. It needs the buffer base/end address, the read pointer and
702+ * the number of bytes to process this cycle. Only the acquired bytes are reported as available
703+ * (with the write pointer placed at the end of that window), so the library processes exactly
704+ * the amount released afterwards and never more than the sink can accept.
705+ */
706+ static int rtnr_source_get_stream (struct audio_stream_rtnr * dst , struct sof_source * source ,
707+ size_t bytes )
695708{
709+ const void * data_ptr , * buf_start ;
710+ size_t buf_size ;
711+ int ret ;
712+
713+ ret = source_get_data (source , bytes , & data_ptr , & buf_start , & buf_size );
714+ if (ret )
715+ return ret ;
696716
697- dst -> size = audio_stream_get_size (src );
698- dst -> avail = audio_stream_get_avail (src );
699- dst -> free = audio_stream_get_free (src );
700- dst -> w_ptr = audio_stream_get_wptr (src );
701- dst -> r_ptr = audio_stream_get_rptr (src );
702- dst -> addr = audio_stream_get_addr (src );
703- dst -> end_addr = audio_stream_get_end_addr (src );
717+ dst -> addr = (void * )buf_start ;
718+ dst -> end_addr = (char * )buf_start + buf_size ;
719+ dst -> size = buf_size ;
720+ dst -> r_ptr = (void * )data_ptr ;
721+ dst -> avail = bytes ;
722+ dst -> free = buf_size - bytes ;
723+ dst -> w_ptr = cir_buf_wrap ((char * )data_ptr + bytes , dst -> addr , dst -> end_addr );
724+
725+ return 0 ;
704726}
705727
706- void rtnr_copy_to_sof_stream (struct audio_stream * dst , struct audio_stream_rtnr * src )
728+ /*
729+ * Acquire the sink's circular buffer and describe it for the RTNR library. See
730+ * rtnr_source_get_stream(); here data_ptr is the write pointer, only the acquired bytes are
731+ * reported as free and the read pointer is placed at the end of that window.
732+ */
733+ static int rtnr_sink_get_stream (struct audio_stream_rtnr * dst , struct sof_sink * sink ,
734+ size_t bytes )
707735{
708- audio_stream_set_size (dst , src -> size );
709- audio_stream_set_avail (dst , src -> avail );
710- audio_stream_set_free (dst , src -> free );
711- audio_stream_set_wptr (dst , src -> w_ptr );
712- audio_stream_set_rptr (dst , src -> r_ptr );
713- audio_stream_set_addr (dst , src -> addr );
714- audio_stream_set_end_addr (dst , src -> end_addr );
736+ void * data_ptr , * buf_start ;
737+ size_t buf_size ;
738+ int ret ;
739+
740+ ret = sink_get_buffer (sink , bytes , & data_ptr , & buf_start , & buf_size );
741+ if (ret )
742+ return ret ;
743+
744+ dst -> addr = buf_start ;
745+ dst -> end_addr = (char * )buf_start + buf_size ;
746+ dst -> size = buf_size ;
747+ dst -> w_ptr = data_ptr ;
748+ dst -> free = bytes ;
749+ dst -> avail = buf_size - bytes ;
750+ dst -> r_ptr = cir_buf_wrap ((char * )data_ptr + bytes , dst -> addr , dst -> end_addr );
751+
752+ return 0 ;
715753}
716754
717755/* copy and process stream data from source to sink buffers */
718756static int rtnr_process (struct processing_module * mod ,
719- struct input_stream_buffer * input_buffers , int num_input_buffers ,
720- struct output_stream_buffer * output_buffers , int num_output_buffers )
757+ struct sof_source * * sources , int num_of_sources ,
758+ struct sof_sink * * sinks , int num_of_sinks )
721759{
722- struct comp_dev * dev = mod -> dev ;
723- struct audio_stream * source = input_buffers [0 ].data ;
724- struct audio_stream * sink = output_buffers [0 ].data ;
725- int frames = input_buffers [0 ].size ;
726760 struct comp_data * cd = module_get_private_data (mod );
761+ struct sof_source * source = sources [0 ];
762+ struct sof_sink * sink = sinks [0 ];
763+ size_t src_frame_bytes = source_get_frame_bytes (source );
764+ size_t snk_frame_bytes = sink_get_frame_bytes (sink );
727765 struct audio_stream_rtnr * sources_stream [RTNR_MAX_SOURCES ];
728766 struct audio_stream_rtnr * sink_stream = & cd -> sink_stream ;
767+ struct comp_dev * dev = mod -> dev ;
768+ uint32_t frames ;
729769 int32_t i ;
730770 int ret ;
731771
@@ -741,18 +781,25 @@ static int rtnr_process(struct processing_module *mod,
741781 comp_dbg (dev , "rtnr_copy()" );
742782
743783 /* put empty data into output queue*/
744- RTKMA_API_First_Copy (cd -> rtk_agl , cd -> source_rate , audio_stream_get_channels (source ));
784+ RTKMA_API_First_Copy (cd -> rtk_agl , cd -> source_rate , source_get_channels (source ));
745785
786+ frames = MIN (source_get_data_frames_available (source ), sink_get_free_frames (sink ));
746787 if (!frames )
747788 return 0 ;
748789
749- comp_dbg (dev , "rtnr_copy() frames = %d " , frames );
790+ comp_dbg (dev , "rtnr_copy() frames = %u " , frames );
750791 if (cd -> process_enable ) {
751- /* Run processing function */
752792
753- /* copy required data from sof audio stream to RTNR audio stream */
754- rtnr_copy_from_sof_stream (sources_stream [0 ], source );
755- rtnr_copy_from_sof_stream (sink_stream , sink );
793+ /* acquire the source/sink circular buffers and describe them for the library */
794+ ret = rtnr_source_get_stream (sources_stream [0 ], source , frames * src_frame_bytes );
795+ if (ret )
796+ return ret ;
797+
798+ ret = rtnr_sink_get_stream (sink_stream , sink , frames * snk_frame_bytes );
799+ if (ret ) {
800+ source_release_data (source , 0 );
801+ return ret ;
802+ }
756803
757804 /*
758805 * Processing function uses an array of pointers to source streams
@@ -766,38 +813,30 @@ static int rtnr_process(struct processing_module *mod,
766813 */
767814 RTKMA_API_Process (cd -> rtk_agl , 0 , cd -> source_rate , MicNum );
768815
769- /* copy required data from RTNR audio stream to sof audio stream */
770- rtnr_copy_to_sof_stream (source , sources_stream [0 ]);
771- rtnr_copy_to_sof_stream (sink , sink_stream );
772-
816+ source_release_data (source , frames * src_frame_bytes );
817+ sink_commit_buffer (sink , frames * snk_frame_bytes );
773818 } else {
774819 comp_dbg (dev , "rtnr_copy() passthrough" );
775820
776- audio_stream_copy (source , 0 , sink , 0 , frames * audio_stream_get_channels ( source ) );
821+ return source_to_sink_copy (source , sink , true , frames * src_frame_bytes );
777822 }
778823
779- /* Track consume and produce */
780- module_update_buffer_position (& input_buffers [0 ], & output_buffers [0 ], frames );
781824 return 0 ;
782825}
783826
784827#if CONFIG_IPC_MAJOR_4
785- static void rtnr_params (struct processing_module * mod )
828+ static void rtnr_params (struct processing_module * mod , struct sof_source * source ,
829+ struct sof_sink * sink )
786830{
787831 struct sof_ipc_stream_params * params = mod -> stream_params ;
788- struct comp_buffer * sinkb , * sourceb ;
789832 struct comp_dev * dev = mod -> dev ;
790833
791834 ipc4_base_module_cfg_to_stream_params (& mod -> priv .cfg .base_cfg , params );
792835 component_set_nearest_period_frames (dev , params -> rate );
793836
794837 /* The caller has checked validity of source and sink buffers */
795-
796- sourceb = comp_dev_get_first_data_producer (dev );
797- ipc4_update_buffer_format (sourceb , & mod -> priv .cfg .base_cfg .audio_fmt );
798-
799- sinkb = comp_dev_get_first_data_consumer (dev );
800- ipc4_update_buffer_format (sinkb , & mod -> priv .cfg .base_cfg .audio_fmt );
838+ ipc4_update_source_format (source , & mod -> priv .cfg .base_cfg .audio_fmt );
839+ ipc4_update_sink_format (sink , & mod -> priv .cfg .base_cfg .audio_fmt );
801840}
802841#endif
803842
@@ -807,20 +846,17 @@ static int rtnr_prepare(struct processing_module *mod,
807846{
808847 struct comp_data * cd = module_get_private_data (mod );
809848 struct comp_dev * dev = mod -> dev ;
810- struct comp_buffer * sourceb , * sinkb ;
811849 int ret ;
812850
813851 comp_dbg (dev , "entry" );
814852
815- sinkb = comp_dev_get_first_data_consumer (dev );
816- sourceb = comp_dev_get_first_data_producer (dev );
817- if (!sourceb || !sinkb ) {
853+ if (!num_of_sources || !num_of_sinks ) {
818854 comp_err (dev , "no source or sink buffer" );
819855 return - ENOTCONN ;
820856 }
821857
822858#if CONFIG_IPC_MAJOR_4
823- rtnr_params (mod );
859+ rtnr_params (mod , sources [ 0 ], sinks [ 0 ] );
824860#endif
825861
826862 /* Check config */
@@ -833,9 +869,9 @@ static int rtnr_prepare(struct processing_module *mod,
833869 /* Initialize RTNR */
834870
835871 /* Get sink data format */
836- cd -> sink_format = audio_stream_get_frm_fmt ( & sinkb -> stream );
837- cd -> sink_stream .frame_fmt = audio_stream_get_frm_fmt ( & sinkb -> stream ) ;
838- ret = rtnr_check_params (mod , & sourceb -> stream , & sinkb -> stream );
872+ cd -> sink_format = sink_get_frm_fmt ( sinks [ 0 ] );
873+ cd -> sink_stream .frame_fmt = cd -> sink_format ;
874+ ret = rtnr_check_params (mod , sources [ 0 ], sinks [ 0 ] );
839875 if (ret )
840876 goto err ;
841877
@@ -877,7 +913,7 @@ static int rtnr_reset(struct processing_module *mod)
877913static const struct module_interface rtnr_interface = {
878914 .init = rtnr_init ,
879915 .prepare = rtnr_prepare ,
880- .process_audio_stream = rtnr_process ,
916+ .process = rtnr_process ,
881917 .set_configuration = rtnr_set_config ,
882918 .get_configuration = rtnr_get_config ,
883919 .reset = rtnr_reset ,
0 commit comments