mirror of
https://github.com/encounter/rust-ffmpeg-zmwangx.git
synced 2026-07-10 21:18:40 -07:00
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:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
@@ -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 => {
|
||||
|
||||
Reference in New Issue
Block a user