1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
// Take a look at the license at the top of the repository in the LICENSE file.
use std::{mem, ptr};
pub use crate::auto::functions::*;
use crate::ffi;
use glib::translate::*;
pub unsafe trait CodecTag<'a>: gst::Tag<'a, TagType = &'a str> {}
unsafe impl<'a> CodecTag<'a> for gst::tags::ContainerFormat {}
unsafe impl<'a> CodecTag<'a> for gst::tags::AudioCodec {}
unsafe impl<'a> CodecTag<'a> for gst::tags::VideoCodec {}
unsafe impl<'a> CodecTag<'a> for gst::tags::SubtitleCodec {}
unsafe impl<'a> CodecTag<'a> for gst::tags::Codec {}
pub fn pb_utils_add_codec_description_to_tag_list_for_tag<'a, T: CodecTag<'a>>(
taglist: &mut gst::TagListRef,
caps: &gst::CapsRef,
) -> Result<(), glib::BoolError> {
assert_initialized_main_thread!();
let codec_tag = T::TAG_NAME;
unsafe {
glib::result_from_gboolean!(
ffi::gst_pb_utils_add_codec_description_to_tag_list(
taglist.as_mut_ptr(),
codec_tag.as_ptr(),
caps.as_ptr(),
),
"Failed to find codec description",
)
}
}
#[doc(alias = "gst_pb_utils_add_codec_description_to_tag_list")]
pub fn pb_utils_add_codec_description_to_tag_list(
taglist: &mut gst::TagListRef,
caps: &gst::CapsRef,
) -> Result<(), glib::BoolError> {
assert_initialized_main_thread!();
unsafe {
glib::result_from_gboolean!(
ffi::gst_pb_utils_add_codec_description_to_tag_list(
taglist.as_mut_ptr(),
ptr::null_mut(),
caps.as_ptr(),
),
"Failed to find codec description",
)
}
}
#[doc(alias = "gst_pb_utils_get_encoder_description")]
pub fn pb_utils_get_encoder_description(caps: &gst::CapsRef) -> glib::GString {
assert_initialized_main_thread!();
unsafe { from_glib_full(ffi::gst_pb_utils_get_encoder_description(caps.as_ptr())) }
}
#[doc(alias = "gst_pb_utils_get_decoder_description")]
pub fn pb_utils_get_decoder_description(caps: &gst::CapsRef) -> glib::GString {
assert_initialized_main_thread!();
unsafe { from_glib_full(ffi::gst_pb_utils_get_decoder_description(caps.as_ptr())) }
}
#[doc(alias = "gst_pb_utils_get_codec_description")]
pub fn pb_utils_get_codec_description(caps: &gst::CapsRef) -> glib::GString {
assert_initialized_main_thread!();
unsafe { from_glib_full(ffi::gst_pb_utils_get_codec_description(caps.as_ptr())) }
}
/// Sets the level and profile on `caps` if it can be determined from
/// `audio_config`. See [`codec_utils_aac_get_level()`][crate::codec_utils_aac_get_level()] and
/// [`codec_utils_aac_get_profile()`][crate::codec_utils_aac_get_profile()] for more details on the parameters.
/// `caps` must be audio/mpeg caps with an "mpegversion" field of either 2 or 4.
/// If mpegversion is 4, the "base-profile" field is also set in `caps`.
/// ## `caps`
/// the [`gst::Caps`][crate::gst::Caps] to which level and profile fields are to be added
/// ## `audio_config`
/// a pointer to the AudioSpecificConfig
/// as specified in the Elementary Stream Descriptor (esds)
/// in ISO/IEC 14496-1. (See below for more details)
///
/// # Returns
///
/// [`true`] if the level and profile could be set, [`false`] otherwise.
#[doc(alias = "gst_codec_utils_aac_caps_set_level_and_profile")]
pub fn codec_utils_aac_caps_set_level_and_profile(
caps: &mut gst::CapsRef,
audio_config: &[u8],
) -> Result<(), glib::BoolError> {
assert_initialized_main_thread!();
assert_eq!(caps.size(), 1);
let s = caps.structure(0).unwrap();
assert_eq!(s.name(), "audio/mpeg");
assert!(s.get::<i32>("mpegversion").is_ok_and(|v| v == 2 || v == 4));
let len = audio_config.len() as u32;
unsafe {
let res: bool = from_glib(ffi::gst_codec_utils_aac_caps_set_level_and_profile(
caps.as_mut_ptr(),
audio_config.to_glib_none().0,
len,
));
if res {
Ok(())
} else {
Err(glib::bool_error!("Failed to set AAC level/profile to caps"))
}
}
}
/// Sets the level and profile in `caps` if it can be determined from `sps`. See
/// [`codec_utils_h264_get_level()`][crate::codec_utils_h264_get_level()] and [`codec_utils_h264_get_profile()`][crate::codec_utils_h264_get_profile()]
/// for more details on the parameters.
/// ## `caps`
/// the [`gst::Caps`][crate::gst::Caps] to which the level and profile are to be added
/// ## `sps`
/// Pointer to the sequence parameter set for the stream.
///
/// # Returns
///
/// [`true`] if the level and profile could be set, [`false`] otherwise.
#[doc(alias = "gst_codec_utils_h264_caps_set_level_and_profile")]
pub fn codec_utils_h264_caps_set_level_and_profile(
caps: &mut gst::CapsRef,
sps: &[u8],
) -> Result<(), glib::BoolError> {
assert_initialized_main_thread!();
assert_eq!(caps.size(), 1);
let s = caps.structure(0).unwrap();
assert_eq!(s.name(), "video/x-h264");
let len = sps.len() as u32;
unsafe {
let res: bool = from_glib(ffi::gst_codec_utils_h264_caps_set_level_and_profile(
caps.as_mut_ptr(),
sps.to_glib_none().0,
len,
));
if res {
Ok(())
} else {
Err(glib::bool_error!(
"Failed to set H264 level/profile to caps"
))
}
}
}
/// Parses profile, flags, and level from a H264 AVCC extradata/sequence_header.
/// These are most commonly retrieved from a video/x-h264 caps with a codec_data
/// buffer.
///
/// The format of H264 AVCC extradata/sequence_header is documented in the
/// ITU-T H.264 specification section 7.3.2.1.1 as well as in ISO/IEC 14496-15
/// section 5.3.3.1.2.
/// ## `codec_data`
/// H264 AVCC extradata
///
/// # Returns
///
/// [`true`] on success, [`false`] on failure
///
/// ## `profile`
/// return location for h264 profile_idc or [`None`]
///
/// ## `flags`
/// return location for h264 constraint set flags or [`None`]
///
/// ## `level`
/// return location h264 level_idc or [`None`]
#[cfg(feature = "v1_20")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
#[doc(alias = "gst_codec_utils_h264_get_profile_flags_level")]
pub fn codec_utils_h264_get_profile_flags_level(
codec_data: &[u8],
) -> Result<(u8, u8, u8), glib::BoolError> {
assert_initialized_main_thread!();
let len = codec_data.len() as u32;
unsafe {
let mut profile = mem::MaybeUninit::uninit();
let mut flags = mem::MaybeUninit::uninit();
let mut level = mem::MaybeUninit::uninit();
glib::result_from_gboolean!(
ffi::gst_codec_utils_h264_get_profile_flags_level(
codec_data.to_glib_none().0,
len,
profile.as_mut_ptr(),
flags.as_mut_ptr(),
level.as_mut_ptr()
),
"Failed to get H264 profile, flags and level"
)?;
let profile = profile.assume_init();
let flags = flags.assume_init();
let level = level.assume_init();
Ok((profile, flags, level))
}
}
/// Sets the level, tier and profile in `caps` if it can be determined from
/// `profile_tier_level`. See [`codec_utils_h265_get_level()`][crate::codec_utils_h265_get_level()],
/// [`codec_utils_h265_get_tier()`][crate::codec_utils_h265_get_tier()] and [`codec_utils_h265_get_profile()`][crate::codec_utils_h265_get_profile()]
/// for more details on the parameters.
/// ## `caps`
/// the [`gst::Caps`][crate::gst::Caps] to which the level, tier and profile are to be added
/// ## `profile_tier_level`
/// Pointer to the profile_tier_level
/// struct
///
/// # Returns
///
/// [`true`] if the level, tier, profile could be set, [`false`] otherwise.
#[doc(alias = "gst_codec_utils_h265_caps_set_level_tier_and_profile")]
pub fn codec_utils_h265_caps_set_level_tier_and_profile(
caps: &mut gst::CapsRef,
profile_tier_level: &[u8],
) -> Result<(), glib::BoolError> {
assert_initialized_main_thread!();
assert_eq!(caps.size(), 1);
let s = caps.structure(0).unwrap();
assert_eq!(s.name(), "video/x-h265");
let len = profile_tier_level.len() as u32;
unsafe {
let res: bool = from_glib(ffi::gst_codec_utils_h265_caps_set_level_tier_and_profile(
caps.as_mut_ptr(),
profile_tier_level.to_glib_none().0,
len,
));
if res {
Ok(())
} else {
Err(glib::bool_error!(
"Failed to set H265 level/tier/profile to caps"
))
}
}
}
/// Sets the level and profile in `caps` if it can be determined from
/// `vis_obj_seq`. See [`codec_utils_mpeg4video_get_level()`][crate::codec_utils_mpeg4video_get_level()] and
/// [`codec_utils_mpeg4video_get_profile()`][crate::codec_utils_mpeg4video_get_profile()] for more details on the
/// parameters.
/// ## `caps`
/// the [`gst::Caps`][crate::gst::Caps] to which the level and profile are to be added
/// ## `vis_obj_seq`
/// Pointer to the visual object
/// sequence for the stream.
///
/// # Returns
///
/// [`true`] if the level and profile could be set, [`false`] otherwise.
#[doc(alias = "gst_codec_utils_mpeg4video_caps_set_level_and_profile")]
pub fn codec_utils_mpeg4video_caps_set_level_and_profile(
caps: &mut gst::CapsRef,
vis_obj_seq: &[u8],
) -> Result<(), glib::BoolError> {
assert_initialized_main_thread!();
assert_eq!(caps.size(), 1);
let s = caps.structure(0).unwrap();
assert_eq!(s.name(), "video/mpeg");
assert!(s.get::<i32>("mpegversion").is_ok_and(|v| v == 4));
let len = vis_obj_seq.len() as u32;
unsafe {
let res: bool = from_glib(ffi::gst_codec_utils_mpeg4video_caps_set_level_and_profile(
caps.as_mut_ptr(),
vis_obj_seq.to_glib_none().0,
len,
));
if res {
Ok(())
} else {
Err(glib::bool_error!(
"Failed to set MPEG4 video level/profile to caps"
))
}
}
}
/// Creates Opus caps from the given parameters.
/// ## `rate`
/// the sample rate
/// ## `channels`
/// the number of channels
/// ## `channel_mapping_family`
/// the channel mapping family
/// ## `stream_count`
/// the number of independent streams
/// ## `coupled_count`
/// the number of stereo streams
/// ## `channel_mapping`
/// the mapping between the streams
///
/// # Returns
///
/// The [`gst::Caps`][crate::gst::Caps], or [`None`] if the parameters would lead to
/// invalid Opus caps.
#[doc(alias = "gst_codec_utils_opus_create_caps")]
pub fn codec_utils_opus_create_caps(
rate: u32,
channels: u8,
channel_mapping_family: u8,
stream_count: u8,
coupled_count: u8,
channel_mapping: &[u8],
) -> Result<gst::Caps, glib::BoolError> {
assert_initialized_main_thread!();
assert!(channel_mapping.is_empty() || channel_mapping.len() == channels as usize);
unsafe {
let caps = ffi::gst_codec_utils_opus_create_caps(
rate,
channels,
channel_mapping_family,
stream_count,
coupled_count,
if channel_mapping.is_empty() {
ptr::null()
} else {
channel_mapping.to_glib_none().0
},
);
if caps.is_null() {
Err(glib::bool_error!(
"Failed to create caps from Opus configuration"
))
} else {
Ok(from_glib_full(caps))
}
}
}
/// Creates Opus caps from the given OpusHead `header` and comment header
/// `comments`.
/// ## `header`
/// OpusHead header
/// ## `comments`
/// Comment header or NULL
///
/// # Returns
///
/// The [`gst::Caps`][crate::gst::Caps].
#[doc(alias = "gst_codec_utils_opus_create_caps_from_header")]
pub fn codec_utils_opus_create_caps_from_header(
header: &gst::BufferRef,
comments: Option<&gst::BufferRef>,
) -> Result<gst::Caps, glib::BoolError> {
assert_initialized_main_thread!();
unsafe {
Option::<_>::from_glib_full(ffi::gst_codec_utils_opus_create_caps_from_header(
mut_override(header.as_ptr()),
comments
.map(|b| mut_override(b.as_ptr()))
.unwrap_or(ptr::null_mut()),
))
.ok_or_else(|| glib::bool_error!("Failed to create caps from Opus headers"))
}
}
/// Creates OpusHead header from the given parameters.
/// ## `rate`
/// the sample rate
/// ## `channels`
/// the number of channels
/// ## `channel_mapping_family`
/// the channel mapping family
/// ## `stream_count`
/// the number of independent streams
/// ## `coupled_count`
/// the number of stereo streams
/// ## `channel_mapping`
/// the mapping between the streams
/// ## `pre_skip`
/// Pre-skip in 48kHz samples or 0
/// ## `output_gain`
/// Output gain or 0
///
/// # Returns
///
/// The [`gst::Buffer`][crate::gst::Buffer] containing the OpusHead.
#[doc(alias = "gst_codec_utils_opus_create_header")]
#[allow(clippy::too_many_arguments)]
pub fn codec_utils_opus_create_header(
rate: u32,
channels: u8,
channel_mapping_family: u8,
stream_count: u8,
coupled_count: u8,
channel_mapping: &[u8],
pre_skip: u16,
output_gain: i16,
) -> Result<gst::Buffer, glib::BoolError> {
assert_initialized_main_thread!();
assert!(channel_mapping.is_empty() || channel_mapping.len() == channels as usize);
unsafe {
let header = ffi::gst_codec_utils_opus_create_header(
rate,
channels,
channel_mapping_family,
stream_count,
coupled_count,
if channel_mapping.is_empty() {
ptr::null()
} else {
channel_mapping.to_glib_none().0
},
pre_skip,
output_gain,
);
if header.is_null() {
Err(glib::bool_error!(
"Failed to create header from Opus configuration"
))
} else {
Ok(from_glib_full(header))
}
}
}
/// Parses Opus caps and fills the different fields with defaults if possible.
/// ## `caps`
/// the [`gst::Caps`][crate::gst::Caps] to parse the data from
///
/// # Returns
///
/// [`true`] if parsing was successful, [`false`] otherwise.
///
/// ## `rate`
/// the sample rate
///
/// ## `channels`
/// the number of channels
///
/// ## `channel_mapping_family`
/// the channel mapping family
///
/// ## `stream_count`
/// the number of independent streams
///
/// ## `coupled_count`
/// the number of stereo streams
///
/// ## `channel_mapping`
/// the mapping between the streams
#[doc(alias = "gst_codec_utils_opus_parse_caps")]
pub fn codec_utils_opus_parse_caps(
caps: &gst::CapsRef,
channel_mapping: Option<&mut [u8; 256]>,
) -> Result<(u32, u8, u8, u8, u8), glib::BoolError> {
assert_initialized_main_thread!();
unsafe {
let mut rate = mem::MaybeUninit::uninit();
let mut channels = mem::MaybeUninit::uninit();
let mut channel_mapping_family = mem::MaybeUninit::uninit();
let mut stream_count = mem::MaybeUninit::uninit();
let mut coupled_count = mem::MaybeUninit::uninit();
let res: bool = from_glib(ffi::gst_codec_utils_opus_parse_caps(
mut_override(caps.as_ptr()),
rate.as_mut_ptr(),
channels.as_mut_ptr(),
channel_mapping_family.as_mut_ptr(),
stream_count.as_mut_ptr(),
coupled_count.as_mut_ptr(),
if let Some(channel_mapping) = channel_mapping {
channel_mapping.as_mut_ptr() as *mut [u8; 256]
} else {
ptr::null_mut()
},
));
if res {
Ok((
rate.assume_init(),
channels.assume_init(),
channel_mapping_family.assume_init(),
stream_count.assume_init(),
coupled_count.assume_init(),
))
} else {
Err(glib::bool_error!("Failed to parse Opus caps"))
}
}
}
/// Parses the OpusHead header.
/// ## `header`
/// the OpusHead [`gst::Buffer`][crate::gst::Buffer]
///
/// # Returns
///
/// [`true`] if parsing was successful, [`false`] otherwise.
///
/// ## `rate`
/// the sample rate
///
/// ## `channels`
/// the number of channels
///
/// ## `channel_mapping_family`
/// the channel mapping family
///
/// ## `stream_count`
/// the number of independent streams
///
/// ## `coupled_count`
/// the number of stereo streams
///
/// ## `channel_mapping`
/// the mapping between the streams
///
/// ## `pre_skip`
/// Pre-skip in 48kHz samples or 0
///
/// ## `output_gain`
/// Output gain or 0
#[doc(alias = "gst_codec_utils_opus_parse_header")]
#[allow(clippy::type_complexity)]
pub fn codec_utils_opus_parse_header(
header: &gst::BufferRef,
channel_mapping: Option<&mut [u8; 256]>,
) -> Result<(u32, u8, u8, u8, u8, u16, i16), glib::BoolError> {
assert_initialized_main_thread!();
unsafe {
let mut rate = mem::MaybeUninit::uninit();
let mut channels = mem::MaybeUninit::uninit();
let mut channel_mapping_family = mem::MaybeUninit::uninit();
let mut stream_count = mem::MaybeUninit::uninit();
let mut coupled_count = mem::MaybeUninit::uninit();
let mut pre_skip = mem::MaybeUninit::uninit();
let mut output_gain = mem::MaybeUninit::uninit();
let res: bool = from_glib(ffi::gst_codec_utils_opus_parse_header(
mut_override(header.as_ptr()),
rate.as_mut_ptr(),
channels.as_mut_ptr(),
channel_mapping_family.as_mut_ptr(),
stream_count.as_mut_ptr(),
coupled_count.as_mut_ptr(),
if let Some(channel_mapping) = channel_mapping {
channel_mapping.as_mut_ptr() as *mut [u8; 256]
} else {
ptr::null_mut()
},
pre_skip.as_mut_ptr(),
output_gain.as_mut_ptr(),
));
if res {
Ok((
rate.assume_init(),
channels.assume_init(),
channel_mapping_family.assume_init(),
stream_count.assume_init(),
coupled_count.assume_init(),
pre_skip.assume_init(),
output_gain.assume_init(),
))
} else {
Err(glib::bool_error!("Failed to parse Opus header"))
}
}
}
/// Converts `caps` to a RFC 6381 compatible codec string if possible.
///
/// Useful for providing the 'codecs' field inside the 'Content-Type' HTTP
/// header for containerized formats, such as mp4 or matroska.
///
/// Registered codecs can be found at http://mp4ra.org/#/codecs
/// ## `caps`
/// A [`gst::Caps`][crate::gst::Caps] to convert to mime codec
///
/// # Returns
///
/// a RFC 6381 compatible codec string or [`None`]
#[cfg(feature = "v1_20")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
#[doc(alias = "gst_codec_utils_caps_get_mime_codec")]
pub fn codec_utils_caps_get_mime_codec(
caps: &gst::CapsRef,
) -> Result<glib::GString, glib::BoolError> {
assert_initialized_main_thread!();
unsafe {
Option::<_>::from_glib_full(ffi::gst_codec_utils_caps_get_mime_codec(mut_override(
caps.as_ptr(),
)))
.ok_or_else(|| glib::bool_error!("Unsupported caps"))
}
}
/// Returns flags that describe the format of the caps if known. No flags are
/// set for unknown caps.
/// ## `caps`
/// the (fixed) [`gst::Caps`][crate::gst::Caps] for which flags are requested
///
/// # Returns
///
/// [`PbUtilsCapsDescriptionFlags`][crate::PbUtilsCapsDescriptionFlags] that describe `caps`, or no flags
/// if the caps are unknown.
#[cfg(feature = "v1_20")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
#[doc(alias = "gst_pb_utils_get_caps_description_flags")]
pub fn pb_utils_get_caps_description_flags(
caps: &gst::CapsRef,
) -> crate::PbUtilsCapsDescriptionFlags {
assert_initialized_main_thread!();
unsafe { from_glib(ffi::gst_pb_utils_get_caps_description_flags(caps.as_ptr())) }
}
/// Returns a possible file extension for the given caps, if known.
/// ## `caps`
/// the (fixed) [`gst::Caps`][crate::gst::Caps] for which a file extension is needed
///
/// # Returns
///
/// a newly-allocated file extension string, or NULL on error. Free
/// string with `g_free()` when not needed any longer.
#[cfg(feature = "v1_20")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
#[doc(alias = "gst_pb_utils_get_file_extension_from_caps")]
pub fn pb_utils_get_file_extension_from_caps(caps: &gst::CapsRef) -> Option<glib::GString> {
assert_initialized_main_thread!();
unsafe {
from_glib_full(ffi::gst_pb_utils_get_file_extension_from_caps(
caps.as_ptr(),
))
}
}