feat(codec): allow codec context creation with specified codec

For some codecs (x264) it's necessary to set the codec while
the context is being created, otherwise the codec-specific defaults
aren't applied.
This commit is contained in:
tilpner
2023-05-24 17:22:59 +02:00
committed by Till Höppner
parent 5894120d39
commit bb8dc49245
3 changed files with 28 additions and 1 deletions
+9
View File
@@ -38,6 +38,15 @@ impl Context {
}
}
pub fn for_codec(codec: &Codec) -> Self {
unsafe {
Context {
ptr: avcodec_alloc_context3(codec.as_ptr()),
owner: None,
}
}
}
pub fn decoder(self) -> Decoder {
Decoder(self)
}
+7
View File
@@ -13,6 +13,13 @@ use crate::{
pub struct Decoder(pub Context);
impl Decoder {
pub fn new<D: traits::Decoder>(codec: D) -> Result<Self, Error> {
let codec = codec.decoder().ok_or(Error::DecoderNotFound)?;
let context = Context::for_codec(&codec);
Ok(Decoder(context))
}
pub fn open(mut self) -> Result<Opened, Error> {
unsafe {
match avcodec_open2(self.as_mut_ptr(), ptr::null(), ptr::null_mut()) {
+12 -1
View File
@@ -5,11 +5,22 @@ use std::{
};
use super::{audio, subtitle, video};
use crate::{codec::Context, ffi::*, media, packet, Error, Frame, Rational};
use crate::{
codec::{traits, Context},
ffi::*,
media, packet, Error, Frame, Rational,
};
pub struct Encoder(pub Context);
impl Encoder {
pub fn new<E: traits::Encoder>(codec: E) -> Result<Self, Error> {
let codec = codec.encoder().ok_or(Error::EncoderNotFound)?;
let context = Context::for_codec(&codec);
Ok(Encoder(context))
}
pub fn video(mut self) -> Result<video::Video, Error> {
match self.medium() {
media::Type::Unknown => {