Debian Codegraph @

Xpjproject/tests/fuzz/fuzz-stream.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2026 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <stdio.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21
22 #include <pjlib.h>
23 #include <pjmedia.h>
24 #include <pjmedia-codec.h>
25
26 #define CLOCK_RATE 8000
27
28 static pj_caching_pool caching_pool;
29 static pjmedia_endpt *endpt = NULL;
30 static int initialized = 0;
31
32 static int init_media(void)
33 {
34 pj_status_t status;
35
36 if (initialized) {
37 return 0;
38 }
39
40 pj_log_set_level(0);
41
42 status = pj_init();
43 if (status != PJ_SUCCESS) {
44 return -1;
45 }
46
47 pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, 0);
48
49 status = pjmedia_endpt_create(&caching_pool.factory, NULL, 1, &endpt);
50 if (status != PJ_SUCCESS) {
51 pj_caching_pool_destroy(&caching_pool);
52 return -1;
53 }
54
55 pjmedia_codec_g711_init(endpt);
56 pjmedia_codec_gsm_init(endpt);
57 pjmedia_codec_ilbc_init(endpt, 30);
58
59 initialized = 1;
60 return 0;
61 }
62
63 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
64 {
65 if (Size < 10) {
66 return 0;
67 }
68
69 if (init_media() != 0) {
70 return 0;
71 }
72
73 pj_pool_t *pool = pj_pool_create(&caching_pool.factory, "fuzz", 4000, 4000, NULL);
74 if (!pool) {
75 return 0;
76 }
77
78 /* Select codec based on fuzzer input */
79 unsigned codec_id = Data[0] % 4;
80 pjmedia_codec_info codec_info;
81 unsigned samples_needed;
82
83 pj_bzero(&codec_info, sizeof(codec_info));
84 codec_info.type = PJMEDIA_TYPE_AUDIO;
85 codec_info.clock_rate = CLOCK_RATE;
86 codec_info.channel_cnt = 1;
87
88 switch (codec_id) {
89 case 0:
90 codec_info.pt = PJMEDIA_RTP_PT_PCMU;
91 codec_info.encoding_name = pj_str("PCMU");
92 samples_needed = 80;
93 break;
94 case 1:
95 codec_info.pt = PJMEDIA_RTP_PT_PCMA;
96 codec_info.encoding_name = pj_str("PCMA");
97 samples_needed = 80;
98 break;
99 case 2:
100 codec_info.pt = PJMEDIA_RTP_PT_GSM;
101 codec_info.encoding_name = pj_str("GSM");
102 samples_needed = 160;
103 break;
104 case 3:
105 codec_info.pt = 97;
106 codec_info.encoding_name = pj_str("iLBC");
107 samples_needed = 240;
108 break;
109 default:
110 pj_pool_release(pool);
111 return 0;
112 }
113
114 /* Get codec manager */
115 pjmedia_codec_mgr *mgr = pjmedia_endpt_get_codec_mgr(endpt);
116 if (!mgr) {
117 pj_pool_release(pool);
118 return 0;
119 }
120
121 /* Verify sufficient input size for codec */
122 size_t offset = 1;
123 if (Size < offset + samples_needed * 2) {
124 pj_pool_release(pool);
125 return 0;
126 }
127
128 /* Get default codec parameters */
129 pjmedia_codec *codec = NULL;
130 pjmedia_codec_param codec_param;
131
132 pj_status_t status = pjmedia_codec_mgr_get_default_param(mgr, &codec_info, &codec_param);
133 if (status != PJ_SUCCESS) {
134 pj_pool_release(pool);
135 return 0;
136 }
137
138 /* Allocate codec instance */
139 status = pjmedia_codec_mgr_alloc_codec(mgr, &codec_info, &codec);
140 if (status != PJ_SUCCESS || !codec) {
141 pj_pool_release(pool);
142 return 0;
143 }
144
145 /* Initialize codec */
146 status = pjmedia_codec_init(codec, pool);
147 if (status != PJ_SUCCESS) {
148 pjmedia_codec_mgr_dealloc_codec(mgr, codec);
149 pj_pool_release(pool);
150 return 0;
151 }
152
153 /* Open codec with parameters */
154 status = pjmedia_codec_open(codec, &codec_param);
155 if (status != PJ_SUCCESS) {
156 pjmedia_codec_mgr_dealloc_codec(mgr, codec);
157 pj_pool_release(pool);
158 return 0;
159 }
160
161 /* Prepare input and output frames */
162 pjmedia_frame input;
163 pjmedia_frame output;
164 pj_int16_t pcm_in[320];
165 pj_uint8_t encoded[300];
166
167 pj_memcpy(pcm_in, Data + offset, PJ_MIN(samples_needed * 2, Size - offset));
168 offset += samples_needed * 2;
169
170 input.type = PJMEDIA_FRAME_TYPE_AUDIO;
171 input.buf = pcm_in;
172 input.size = samples_needed * 2;
173 input.timestamp.u64 = 0;
174
175 output.buf = encoded;
176 output.size = sizeof(encoded);
177
178 /* Test encode operation */
179 status = pjmedia_codec_encode(codec, &input, sizeof(encoded), &output);
180
181 /* Test decode operation if encode succeeded */
182 if (status == PJ_SUCCESS && output.size > 0 && output.size <= sizeof(encoded)) {
183 /* G.711 requires exact frame size of 80 bytes */
184 if (codec_id >= 2 || output.size == 80) {
185 pjmedia_frame dec_input;
186 pjmedia_frame dec_output;
187 pj_int16_t pcm_out[320];
188
189 dec_input.type = PJMEDIA_FRAME_TYPE_AUDIO;
190 dec_input.buf = output.buf;
191 dec_input.size = output.size;
192 dec_input.timestamp.u64 = 0;
193
194 dec_output.buf = pcm_out;
195 dec_output.size = sizeof(pcm_out);
196
197 pjmedia_codec_decode(codec, &dec_input, sizeof(pcm_out), &dec_output);
198 }
199 }
200
201 /* Cleanup */
202 pjmedia_codec_close(codec);
203 pjmedia_codec_mgr_dealloc_codec(mgr, codec);
204 pj_pool_release(pool);
205
206 return 0;
207 }