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
use std::ffi::CStr;
use std::str::from_utf8_unchecked;

use ffi::*;
use super::{Id, Video, Audio, Capabilities, Profile};
use ::{Error, media};

#[derive(PartialEq, Eq, Copy, Clone)]
pub struct Codec {
	ptr: *mut AVCodec,
}

unsafe impl Send for Codec { }
unsafe impl Sync for Codec { }

impl Codec {
	pub unsafe fn wrap(ptr: *mut AVCodec) -> Self {
		Codec { ptr: ptr }
	}

	pub unsafe fn as_ptr(&self) -> *const AVCodec {
		self.ptr as *const _
	}

	pub unsafe fn as_mut_ptr(&mut self) -> *mut AVCodec {
		self.ptr
	}
}

impl Codec {
	pub fn is_encoder(&self) -> bool {
		unsafe {
			av_codec_is_encoder(self.as_ptr()) != 0
		}
	}

	pub fn is_decoder(&self) -> bool {
		unsafe {
			av_codec_is_decoder(self.as_ptr()) != 0
		}
	}

	pub fn name(&self) -> &str {
		unsafe {
			from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).name).to_bytes())
		}
	}

	pub fn description(&self) -> &str {
		unsafe {
			from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).long_name).to_bytes())
		}
	}

	pub fn medium(&self) -> media::Type {
		unsafe {
			media::Type::from((*self.as_ptr()).kind)
		}
	}

	pub fn id(&self) -> Id {
		unsafe {
			Id::from((*self.as_ptr()).id)
		}
	}

	pub fn is_video(&self) -> bool {
		self.medium() == media::Type::Video
	}

	pub fn video(self) -> Result<Video, Error> {
		unsafe {
			if self.medium() == media::Type::Video {
				Ok(Video::new(self))
			}
			else {
				Err(Error::InvalidData)
			}
		}
	}

	pub fn is_audio(&self) -> bool {
		self.medium() == media::Type::Audio
	}

	pub fn audio(self) -> Result<Audio, Error> {
		unsafe {
			if self.medium() == media::Type::Audio {
				Ok(Audio::new(self))
			}
			else {
				Err(Error::InvalidData)
			}
		}
	}

	pub fn max_lowres(&self) -> i32 {
		unsafe {
			av_codec_get_max_lowres(self.as_ptr())
		}
	}

	pub fn capabilities(&self) -> Capabilities {
		unsafe {
			Capabilities::from_bits_truncate((*self.as_ptr()).capabilities as u32)
		}
	}

	pub fn profiles(&self) -> Option<ProfileIter> {
		unsafe {
			if (*self.as_ptr()).profiles.is_null() {
				None
			}
			else {
				Some(ProfileIter::new(self.id(), (*self.as_ptr()).profiles))
			}
		}
	}
}

pub struct ProfileIter {
	id:  Id,
	ptr: *const AVProfile,
}

impl ProfileIter {
	pub fn new(id: Id, ptr: *const AVProfile) -> Self {
		ProfileIter { id: id, ptr: ptr }
	}
}

impl Iterator for ProfileIter {
	type Item = Profile;

	fn next(&mut self) -> Option<<Self as Iterator>::Item> {
		unsafe {
			if (*self.ptr).profile == FF_PROFILE_UNKNOWN {
				return None;
			}

			let profile = Profile::from((self.id, (*self.ptr).profile));
			self.ptr    = self.ptr.offset(1);

			Some(profile)
		}
	}
}