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
15 #include <epan/packet.h>
16 #include <epan/prefs.h>
17 #include <epan/expert.h>
18 #include <epan/unit_strings.h>
20 #include <wsutil/array.h>
22 #include <ftypes/ftypes.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 */
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"},
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"},
109 static value_string_ext opus_codec_toc_c_request_vals_ext
110 = VALUE_STRING_EXT_INIT(opus_codec_toc_c_request_vals
);
113 parse_size_field(const unsigned char *ch
, int32_t cn
, int16_t *size
)
119 else if (ch
[0] < 252) {
128 *size
= 4 * ch
[1] + ch
[0];
134 opus_packet_get_samples_per_frame(const unsigned char *data
, uint16_t Fs
)
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;
145 audiosize
= ((data
[0] >> 3) & 0x3);
147 audiosize
= Fs
* 60 / 1000;
149 audiosize
= (Fs
<< audiosize
) / 100;
155 dissect_opus(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void* data _U_
)
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};
167 int16_t framesize
= 0;
171 } frames
[MAX_FRAMES_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:
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
);
204 toc
= tvb_get_uint8(tvb
, offset
++);
207 case 0: /* One frame */
208 frames
[0].begin
= offset
;
209 frames
[0].size
= pkt_total
- offset
;
212 case 1: /* Two CBR frames */
213 if ((pkt_total
- offset
) & 0x1) {
214 expert_add_info(pinfo
, opus_tree
, &ei_opus_err_r3
);
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
;
222 case 2: /* Two VBR frames */
223 if (offset
>= pkt_total
) {
224 expert_add_info(pinfo
, opus_tree
, &ei_opus_err_r4
);
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
);
237 proto_tree_add_uint(opus_tree
, hf_opus_frame_size
, tvb
, 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
;
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
);
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);
260 || framesize
* frame_count
> 120 * MAX_FRAMES_COUNT
) {
261 expert_add_info(pinfo
, opus_tree
, &ei_opus_err_r5
);
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 */
270 int padding_begin
= offset
;
273 if (offset
>= pkt_total
) {
274 expert_add_info(pinfo
, opus_tree
, &ei_opus_err_r7
);
277 p
= tvb_get_uint8(tvb
, offset
++);
278 tmp
= p
== 255 ? 254 : p
;
281 proto_tree_add_uint(opus_tree
, hf_opus_padding_size
, tvb
,
282 padding_begin
, offset
- padding_begin
,
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
);
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
);
309 proto_tree_add_uint(opus_tree
, hf_opus_frame_size
, tvb
, offset
,
310 bytes
, frames
[idx
].size
);
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
);
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
);
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
);
334 for (idx
= 0; idx
< frame_count
; idx
++) {
335 frames
[idx
].begin
= offset
+ idx
* frame_size
;
336 frames
[idx
].size
= frame_size
;
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
);
349 proto_tree_add_item(opus_tree
, hf_opus_frame
, tvb
, f
->begin
, f
->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
);
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",
384 {"TOC.S bit", "opus.TOC.s", FT_BOOLEAN
, 8, TFS(&toc_s_bit_vals
),
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
}},
402 {"Frame Data", "opus.frame_data", FT_BYTES
, BASE_NONE
| BASE_ALLOW_ZERO
,
403 NULL
, 0x0, NULL
, HFILL
}},
405 {"Padding", "opus.padding", FT_BYTES
, BASE_NONE
, NULL
, 0x0, NULL
,
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",
413 static int *ett
[] = { &ett_opus
, };
415 static ei_register_info ei
[] = {
417 {"opus.violate_r1", PI_PROTOCOL
, PI_ERROR
,
418 "Error:[R1] Packets are at least one byte.", EXPFILL
}},
420 {"opus.violate_r2", PI_MALFORMED
, PI_ERROR
,
421 "Error:[R2] No implicit frame length is larger than 1275 bytes.",
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.",
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.",
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.",
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.",
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.",
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",
461 = proto_register_protocol("Opus Interactive Audio Codec", /* name */
462 "OPUS", /* short name */
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
);
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
493 * indent-tabs-mode: nil
496 * vi: set shiftwidth=4 tabstop=8 expandtab:
497 * :indentSize=4:tabSize=8:noTabs=true: