Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-opus.c
blobd4a0cdf86892a22a2d8e32e398ef449008a04055
1 /* packet-opus.c
2 * Routines for OPUS dissection
3 * Copyright 2014, Owen Williams williams.owen@gmail.com
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include "config.h"
13 #include "tvbuff.h"
15 #include <epan/packet.h>
16 #include <epan/prefs.h>
17 #include <epan/expert.h>
18 #include <epan/unit_strings.h>
19 #include <epan/tfs.h>
20 #include <wsutil/array.h>
22 #include <ftypes/ftypes.h>
23 #include <proto.h>
24 #include <stdint.h>
26 #define MAX_FRAMES_COUNT 48
28 void proto_reg_handoff_opus(void);
29 void proto_register_opus(void);
31 static dissector_handle_t opus_handle;
33 /* Initialize the protocol and registered fields */
34 static int proto_opus;
35 static int hf_opus_toc_config;
36 static int hf_opus_toc_s;
37 static int hf_opus_toc_c;
38 static int hf_opus_frame;
39 static int hf_opus_frame_size;
40 static int hf_opus_frame_count_v;
41 static int hf_opus_frame_count_p;
42 static int hf_opus_frame_count_m;
43 static int hf_opus_padding;
44 static int hf_opus_padding_size;
46 /* Initialize the subtree pointers */
47 static int ett_opus;
49 static expert_field ei_opus_err_r1;
50 static expert_field ei_opus_err_r2;
51 static expert_field ei_opus_err_r3;
52 static expert_field ei_opus_err_r4;
53 static expert_field ei_opus_err_r5;
54 static expert_field ei_opus_err_r6;
55 static expert_field ei_opus_err_r7;
56 static expert_field ei_opus_padding_nonzero;
58 /* From RFC6716 chapter 3.1
59 * The top five bits of the TOC byte, labeled "config", encode one of 32
60 * possible configurations of operating mode, audio bandwidth, and frame size.
62 static const value_string opus_codec_toc_config_request_vals[] = {
63 {0, "NB, SILK-only ptime=10"},
64 {1, "NB, SILK-only ptime=20"},
65 {2, "NB, SILK-only ptime=40"},
66 {3, "NB, SILK-only ptime=60"},
67 {4, "MB, SILK-only ptime=10"},
68 {5, "MB, SILK-only ptime=20"},
69 {6, "MB, SILK-only ptime=40"},
70 {7, "MB, SILK-only ptime=60"},
71 {8, "WB, SILK-only ptime=10"},
72 {9, "WB, SILK-only ptime=20"},
73 {10, "WB, SILK-only ptime=40"},
74 {11, "WB, SILK-only ptime=60"},
75 {12, "SWB, Hybrid ptime=10"},
76 {13, "SWB, Hybrid ptime=20"},
77 {14, "FB, Hybrid ptime=10"},
78 {15, "FB, Hybrid ptime=20"},
79 {16, "NB, CELT-only ptime=2.5"},
80 {17, "NB, CELT-only ptime=5"},
81 {18, "NB, CELT-only ptime=10"},
82 {19, "NB, CELT-only ptime=20"},
83 {20, "WB, CELT-only ptime=2.5"},
84 {21, "WB, CELT-only ptime=5"},
85 {22, "WB, CELT-only ptime=10"},
86 {23, "WB, CELT-only ptime=20"},
87 {24, "SWB, CELT-only ptime=2.5"},
88 {25, "SWB, CELT-only ptime=5"},
89 {26, "SWB, CELT-only ptime=10"},
90 {27, "SWB, CELT-only ptime=20"},
91 {28, "FB, CELT-only ptime=2.5"},
92 {29, "FB, CELT-only ptime=5"},
93 {30, "FB, CELT-only ptime=10"},
94 {31, "FB, CELT-only ptime=20"},
95 {0, NULL}};
96 static value_string_ext opus_codec_toc_config_request_vals_ext
97 = VALUE_STRING_EXT_INIT(opus_codec_toc_config_request_vals);
99 static const true_false_string toc_s_bit_vals = {"stereo", "mono"};
100 static const true_false_string fc_v_bit_vals = {"VBR", "CBR"};
101 static const true_false_string fc_p_bit_vals = {"Padding", "No Padding"};
103 static const value_string opus_codec_toc_c_request_vals[]
104 = {{0, "1 frame in the packet"},
105 {1, "2 frames in the packet, each with equal compressed size"},
106 {2, "2 frames in the packet, with different compressed sizes"},
107 {3, "an arbitrary number of frames in the packet"},
108 {0, NULL}};
109 static value_string_ext opus_codec_toc_c_request_vals_ext
110 = VALUE_STRING_EXT_INIT(opus_codec_toc_c_request_vals);
112 static int
113 parse_size_field(const unsigned char *ch, int32_t cn, int16_t *size)
115 if (cn < 1) {
116 *size = -1;
117 return -1;
119 else if (ch[0] < 252) {
120 *size = ch[0];
121 return 1;
123 else if (cn < 2) {
124 *size = -1;
125 return -1;
127 else {
128 *size = 4 * ch[1] + ch[0];
129 return 2;
133 static int16_t
134 opus_packet_get_samples_per_frame(const unsigned char *data, uint16_t Fs)
136 int audiosize;
137 if (data[0] & 0x80) {
138 audiosize = ((data[0] >> 3) & 0x3);
139 audiosize = (Fs << audiosize) / 400;
141 else if ((data[0] & 0x60) == 0x60) {
142 audiosize = (data[0] & 0x08) ? Fs / 50 : Fs / 100;
144 else {
145 audiosize = ((data[0] >> 3) & 0x3);
146 if (audiosize == 3)
147 audiosize = Fs * 60 / 1000;
148 else
149 audiosize = (Fs << audiosize) / 100;
151 return audiosize;
154 static int
155 dissect_opus(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
157 int idx;
159 proto_item *item;
160 proto_tree *opus_tree;
162 int pkt_total = 0, offset = 0;
163 unsigned cap_len = 0;
164 uint8_t ch = 0, toc = 0, octet[2] = {0, 0};
165 int octet_cnt = 0;
166 int bytes = 0;
167 int16_t framesize = 0;
168 struct FRAME_T {
169 int16_t begin;
170 int16_t size;
171 } frames[MAX_FRAMES_COUNT] = {{0}};
172 int frame_count = 0;
173 static int *toc_fields[]
174 = {&hf_opus_toc_config, &hf_opus_toc_s, &hf_opus_toc_c, NULL};
175 static int *frame_count_fields[]
176 = {&hf_opus_frame_count_v, &hf_opus_frame_count_p,
177 &hf_opus_frame_count_m, NULL};
179 int padding_size = 0;
181 col_set_str(pinfo->cinfo, COL_PROTOCOL, "OPUS");
183 item = proto_tree_add_item(tree, proto_opus, tvb, 0, -1, ENC_NA);
184 opus_tree = proto_item_add_subtree(item, ett_opus);
187 * A ToC entry takes the following format, details defined in section-3.1:
189 * 0 1 2 3 4 5 6 7
190 * +-+-+-+-+-+-+-+-+
191 * | config |s| c |
192 * +-+-+-+-+-+-+-+-+
194 proto_tree_add_bitmask_list(opus_tree, tvb, offset, 1, toc_fields, ENC_NA);
196 cap_len = tvb_captured_length(tvb);
197 pkt_total = tvb_reported_length_remaining(tvb, offset);
199 if (pkt_total <= 0) {
200 expert_add_info(pinfo, opus_tree, &ei_opus_err_r1);
201 return cap_len;
204 toc = tvb_get_uint8(tvb, offset++);
206 switch (toc & 0x3) {
207 case 0: /* One frame */
208 frames[0].begin = offset;
209 frames[0].size = pkt_total - offset;
210 frame_count = 1;
211 break;
212 case 1: /* Two CBR frames */
213 if ((pkt_total - offset) & 0x1) {
214 expert_add_info(pinfo, opus_tree, &ei_opus_err_r3);
215 return cap_len;
217 frames[0].begin = offset;
218 frames[0].size = frames[1].size = (pkt_total - offset) / 2;
219 frames[1].begin = frames[0].begin + frames[0].size;
220 frame_count = 2;
221 break;
222 case 2: /* Two VBR frames */
223 if (offset >= pkt_total) {
224 expert_add_info(pinfo, opus_tree, &ei_opus_err_r4);
225 return cap_len;
227 /* offset < pkt_total */
228 octet[octet_cnt++] = tvb_get_uint8(tvb, offset);
229 if (offset + 1 < pkt_total) {
230 octet[octet_cnt++] = tvb_get_uint8(tvb, offset + 1);
232 bytes = parse_size_field(octet, octet_cnt, &framesize);
233 if (framesize < 0 || framesize > pkt_total) {
234 expert_add_info(pinfo, opus_tree, &ei_opus_err_r1);
235 return cap_len;
237 proto_tree_add_uint(opus_tree, hf_opus_frame_size, tvb, offset, bytes,
238 framesize);
239 offset += bytes;
240 /* frame[0] has size header, frame[1] is remaining */
241 frames[0].begin = offset;
242 frames[0].size = framesize;
243 frames[1].begin = frames[0].begin + framesize;
244 frames[1].size = -1;
245 frame_count = 2;
246 break;
247 /* Multiple CBR/VBR frames (from 0 to 120 ms) */
248 default: /* case 3:*/
249 if ((pkt_total - offset) < 1) {
250 expert_add_info(pinfo, opus_tree, &ei_opus_err_r6);
251 return cap_len;
253 proto_tree_add_bitmask_list(opus_tree, tvb, offset, 1,
254 frame_count_fields, ENC_NA);
255 /* Number of frames encoded in bits 0 to 5 */
256 ch = tvb_get_uint8(tvb, offset++);
257 frame_count = ch & 0x3F;
258 framesize = opus_packet_get_samples_per_frame(&toc, 48000U);
259 if (frame_count <= 0
260 || framesize * frame_count > 120 * MAX_FRAMES_COUNT) {
261 expert_add_info(pinfo, opus_tree, &ei_opus_err_r5);
262 return cap_len;
264 /* The smallest value the function above can return is 120, so
265 * the below holds (but make it obvious). */
266 ws_assert(frame_count <= MAX_FRAMES_COUNT);
267 /* Padding flag (bit 6) used */
268 if (ch & 0x40) {
269 int p;
270 int padding_begin = offset;
271 do {
272 int tmp;
273 if (offset >= pkt_total) {
274 expert_add_info(pinfo, opus_tree, &ei_opus_err_r7);
275 return cap_len;
277 p = tvb_get_uint8(tvb, offset++);
278 tmp = p == 255 ? 254 : p;
279 padding_size += tmp;
280 } while (p == 255);
281 proto_tree_add_uint(opus_tree, hf_opus_padding_size, tvb,
282 padding_begin, offset - padding_begin,
283 padding_size);
284 /* This padding size is the number of padding bytes at the
285 * end of the packets "in addition to the byte(s) used to
286 * indicate the size of the padding."
288 pkt_total -= padding_size;
290 /* VBR flag is bit 7 */
291 if (ch & 0x80) { /* VBR case */
292 for (idx = 0; idx < frame_count - 1; idx++) {
293 if (offset >= pkt_total) {
294 expert_add_info(pinfo, opus_tree, &ei_opus_err_r7);
295 return cap_len;
297 octet_cnt = 0;
298 octet[octet_cnt++] = tvb_get_uint8(tvb, offset);
299 if (offset + 1 < pkt_total) {
300 octet[octet_cnt++] = tvb_get_uint8(tvb, offset);
302 bytes = parse_size_field(octet, octet_cnt, &frames[idx].size);
303 if (frames[idx].size < 0
304 || frames[idx].size > (pkt_total - offset)) {
305 expert_add_info(pinfo, opus_tree, &ei_opus_err_r1);
306 return cap_len;
309 proto_tree_add_uint(opus_tree, hf_opus_frame_size, tvb, offset,
310 bytes, frames[idx].size);
311 offset += bytes;
313 for (idx = 0; idx < frame_count - 1; idx++) {
314 frames[idx].begin = offset;
315 offset += frames[idx].size;
317 if (offset > pkt_total) {
318 expert_add_info(pinfo, opus_tree, &ei_opus_err_r7);
319 return cap_len;
321 frames[idx].begin = offset;
322 frames[idx].size = pkt_total - offset;
324 else { /* CBR case */
325 if (offset > pkt_total) {
326 expert_add_info(pinfo, opus_tree, &ei_opus_err_r6);
327 return cap_len;
329 unsigned frame_size = (pkt_total - offset) / frame_count;
330 if (frame_size * frame_count != (unsigned)(pkt_total - offset)) {
331 expert_add_info(pinfo, opus_tree, &ei_opus_err_r6);
332 return cap_len;
334 for (idx = 0; idx < frame_count; idx++) {
335 frames[idx].begin = offset + idx * frame_size;
336 frames[idx].size = frame_size;
339 break;
342 for (idx = 0; idx < frame_count; idx++) {
343 struct FRAME_T *f = &frames[idx];
344 /* reject the frame which is larger than 1275. */
345 if (f->size > 1275) {
346 expert_add_info(pinfo, opus_tree, &ei_opus_err_r2);
347 return cap_len;
349 proto_tree_add_item(opus_tree, hf_opus_frame, tvb, f->begin, f->size,
350 ENC_NA);
353 if (padding_size) {
354 int padding_begin = tvb_reported_length(tvb) - padding_size;
355 item = proto_tree_add_item(opus_tree, hf_opus_padding, tvb,
356 padding_begin, padding_size, ENC_NA);
357 /* RFC 6716: "The additional padding bytes appear at the end
358 * of the packet and MUST be set to zero by the encoder to avoid
359 * creating a covert channel."
361 for (int i = 0; i < padding_size; ++i) {
362 if (tvb_get_uint8(tvb, padding_begin + i) != 0) {
363 expert_add_info(pinfo, item, &ei_opus_padding_nonzero);
364 break;
369 return cap_len;
372 void
373 proto_register_opus(void)
375 module_t *opus_module;
376 expert_module_t* expert_opus;
378 static hf_register_info hf[] = {
379 {&hf_opus_toc_config,
380 {"TOC.config", "opus.TOC.config", FT_UINT8, BASE_DEC | BASE_EXT_STRING,
381 &opus_codec_toc_config_request_vals_ext, 0xF8, "Opus TOC config",
382 HFILL}},
383 {&hf_opus_toc_s,
384 {"TOC.S bit", "opus.TOC.s", FT_BOOLEAN, 8, TFS(&toc_s_bit_vals),
385 0x04, NULL, HFILL}},
386 {&hf_opus_toc_c,
387 {"TOC.C bits", "opus.TOC.c", FT_UINT8, BASE_DEC | BASE_EXT_STRING,
388 &opus_codec_toc_c_request_vals_ext, 0x03, "Opus TOC code", HFILL}},
389 {&hf_opus_frame_count_m,
390 {"Frame Count.m", "opus.FC.m", FT_UINT8, BASE_DEC, NULL, 0x3F,
391 "Frame Count", HFILL}},
392 {&hf_opus_frame_count_p,
393 {"Frame Count.p bit", "opus.FC.p", FT_BOOLEAN, 8,
394 TFS(&fc_p_bit_vals), 0x40, NULL, HFILL}},
395 {&hf_opus_frame_count_v,
396 {"Frame Count.v bit", "opus.FC.v", FT_BOOLEAN, 8,
397 TFS(&fc_v_bit_vals), 0x80, NULL, HFILL}},
398 {&hf_opus_frame_size,
399 {"Frame Size", "opus.frame_size", FT_UINT16, BASE_DEC | BASE_UNIT_STRING,
400 UNS(&units_byte_bytes), 0x0, NULL, HFILL}},
401 {&hf_opus_frame,
402 {"Frame Data", "opus.frame_data", FT_BYTES, BASE_NONE | BASE_ALLOW_ZERO,
403 NULL, 0x0, NULL, HFILL}},
404 {&hf_opus_padding,
405 {"Padding", "opus.padding", FT_BYTES, BASE_NONE, NULL, 0x0, NULL,
406 HFILL}},
407 {&hf_opus_padding_size,
408 {"Padding Size", "opus.padding_size", FT_UINT32, BASE_DEC, NULL, 0x0,
409 "Additional padding bytes, not including the bytes indicating the padding size",
410 HFILL}},
413 static int *ett[] = { &ett_opus, };
415 static ei_register_info ei[] = {
416 {&ei_opus_err_r1,
417 {"opus.violate_r1", PI_PROTOCOL, PI_ERROR,
418 "Error:[R1] Packets are at least one byte.", EXPFILL}},
419 {&ei_opus_err_r2,
420 {"opus.violate_r2", PI_MALFORMED, PI_ERROR,
421 "Error:[R2] No implicit frame length is larger than 1275 bytes.",
422 EXPFILL}},
423 {&ei_opus_err_r3,
424 {"opus.violate_r3", PI_MALFORMED, PI_ERROR,
425 "Error:[R3] Code 1 packets have an odd total length, N, so that "
426 "(N-1)/2 is an integer.",
427 EXPFILL}},
428 {&ei_opus_err_r4,
429 {"opus.violate_r4", PI_MALFORMED, PI_ERROR,
430 "Error:[R4] Code 2 packets have enough bytes after the TOC for a "
431 "valid frame length, and that length is no larger than the number of"
432 "bytes remaining in the packet.",
433 EXPFILL}},
434 {&ei_opus_err_r5,
435 {"opus.violate_r5", PI_PROTOCOL, PI_ERROR,
436 "Error:[R5] Code 3 packets contain at least one frame, but no more "
437 "than 120 ms of audio total.",
438 EXPFILL}},
439 {&ei_opus_err_r6,
440 {"opus.violate_r6", PI_PROTOCOL, PI_ERROR,
441 "Error:[R6] The length of a CBR code 3 packet, N, is at least two "
442 "bytes, the number of bytes added to indicate the padding size plus "
443 "the trailing padding bytes themselves, P, is no more than N-2, and "
444 "the frame count, M, satisfies the constraint that (N-2-P) is a "
445 "non-negative integer multiple of M.",
446 EXPFILL}},
447 {&ei_opus_err_r7,
448 {"opus.violate_r7", PI_PROTOCOL, PI_ERROR,
449 "Error:[R7] VBR code 3 packets are large enough to contain all the "
450 "header bytes (TOC byte, frame count byte, any padding length bytes, "
451 "and any frame length bytes), plus the length of the first M-1 "
452 "frames, plus any trailing padding bytes.",
453 EXPFILL}},
454 {&ei_opus_padding_nonzero,
455 {"opus.padding.nonzero", PI_PROTOCOL, PI_WARN,
456 "Additional padding bytes MUST be set to zero by the encoder",
457 EXPFILL}},
460 proto_opus
461 = proto_register_protocol("Opus Interactive Audio Codec", /* name */
462 "OPUS", /* short name */
463 "opus" /* abbrev */
466 proto_register_field_array(proto_opus, hf, array_length(hf));
467 proto_register_subtree_array(ett, array_length(ett));
469 opus_module = prefs_register_protocol(proto_opus, NULL);
471 expert_opus = expert_register_protocol(proto_opus);
472 expert_register_field_array(expert_opus, ei, array_length(ei));
474 prefs_register_obsolete_preference(opus_module, "dynamic.payload.type");
476 opus_handle = register_dissector("opus", dissect_opus, proto_opus);
479 void
480 proto_reg_handoff_opus(void)
482 dissector_add_string("rtp_dyn_payload_type" , "OPUS", opus_handle);
484 dissector_add_uint_range_with_preference("rtp.pt", "", opus_handle);
488 * Editor modelines - https://www.wireshark.org/tools/modelines.html
490 * Local variables:
491 * c-basic-offset: 4
492 * tab-width: 8
493 * indent-tabs-mode: nil
494 * End:
496 * vi: set shiftwidth=4 tabstop=8 expandtab:
497 * :indentSize=4:tabSize=8:noTabs=true: