1 // SPDX-License-Identifier: BSD-3-Clause
2 //
3 // Copyright 2020 NXP
4 //
5 // Author: Daniel Baluta <daniel.baluta@nxp.com>
6 //
7 // Passthrough codec implementation to demonstrate Codec Adapter API
8
9 #include <sof/audio/codec_adapter/codec/generic.h>
10
11 /* 376b5e44-9c82-4ec2-bc83-10ea101afa8f */
12 DECLARE_SOF_RT_UUID("passthrough_codec", passthrough_uuid, 0x376b5e44, 0x9c82, 0x4ec2,
13 0xbc, 0x83, 0x10, 0xea, 0x10, 0x1a, 0xf8, 0x8f);
14 DECLARE_TR_CTX(passthrough_tr, SOF_UUID(passthrough_uuid), LOG_LEVEL_INFO);
15
passthrough_codec_init(struct comp_dev * dev)16 static int passthrough_codec_init(struct comp_dev *dev)
17 {
18 comp_info(dev, "passthrough_codec_init() start");
19 return 0;
20 }
21
passthrough_codec_prepare(struct comp_dev * dev)22 static int passthrough_codec_prepare(struct comp_dev *dev)
23 {
24 struct codec_data *codec = comp_get_codec(dev);
25 struct comp_data *cd = comp_get_drvdata(dev);
26
27 comp_info(dev, "passthrough_codec_prepare()");
28
29 codec->cpd.in_buff = rballoc(0, SOF_MEM_CAPS_RAM, cd->period_bytes);
30 if (!codec->cpd.in_buff) {
31 comp_err(dev, "passthrough_codec_prepare(): Failed to alloc in_buff");
32 return -ENOMEM;
33 }
34 codec->cpd.in_buff_size = cd->period_bytes;
35
36 codec->cpd.out_buff = rballoc(0, SOF_MEM_CAPS_RAM, cd->period_bytes);
37 if (!codec->cpd.out_buff) {
38 comp_err(dev, "passthrough_codec_prepare(): Failed to alloc out_buff");
39 rfree(codec->cpd.in_buff);
40 return -ENOMEM;
41 }
42 codec->cpd.out_buff_size = cd->period_bytes;
43
44 return 0;
45 }
46
passthrough_codec_init_process(struct comp_dev * dev)47 static int passthrough_codec_init_process(struct comp_dev *dev)
48 {
49 struct codec_data *codec = comp_get_codec(dev);
50
51 comp_dbg(dev, "passthrough_codec_init_process()");
52
53 codec->cpd.produced = 0;
54 codec->cpd.consumed = 0;
55 codec->cpd.init_done = 1;
56
57 return 0;
58 }
59
passthrough_codec_process(struct comp_dev * dev)60 static int passthrough_codec_process(struct comp_dev *dev)
61 {
62 struct codec_data *codec = comp_get_codec(dev);
63 struct comp_data *cd = comp_get_drvdata(dev);
64
65 comp_dbg(dev, "passthrough_codec_process()");
66
67 memcpy_s(codec->cpd.out_buff, codec->cpd.out_buff_size,
68 codec->cpd.in_buff, codec->cpd.in_buff_size);
69 codec->cpd.produced = cd->period_bytes;
70 codec->cpd.consumed = cd->period_bytes;
71
72 return 0;
73 }
74
passthrough_codec_apply_config(struct comp_dev * dev)75 static int passthrough_codec_apply_config(struct comp_dev *dev)
76 {
77 comp_info(dev, "passthrough_codec_apply_config()");
78
79 /* nothing to do */
80 return 0;
81 }
82
passthrough_codec_reset(struct comp_dev * dev)83 static int passthrough_codec_reset(struct comp_dev *dev)
84 {
85 comp_info(dev, "passthrough_codec_reset()");
86
87 /* nothing to do */
88 return 0;
89 }
90
passthrough_codec_free(struct comp_dev * dev)91 static int passthrough_codec_free(struct comp_dev *dev)
92 {
93 struct codec_data *codec = comp_get_codec(dev);
94
95 comp_info(dev, "passthrough_codec_free()");
96
97 rfree(codec->cpd.in_buff);
98 rfree(codec->cpd.out_buff);
99
100 return 0;
101 }
102
103 static struct codec_interface passthrough_interface = {
104 .init = passthrough_codec_init,
105 .prepare = passthrough_codec_prepare,
106 .init_process = passthrough_codec_init_process,
107 .process = passthrough_codec_process,
108 .apply_config = passthrough_codec_apply_config,
109 .reset = passthrough_codec_reset,
110 .free = passthrough_codec_free
111 };
112
113 DECLARE_CODEC_ADAPTER(passthrough_interface, passthrough_uuid, passthrough_tr);
114