1 use nihav_core::codecs::*;
2 use nihav_core::io::byteio::*;
3 use nihav_core::io::bitwriter::*;
5 const TMRT_DELTA_TAB: [&[i16]; 3] = [
7 &[ 2, -3, 8, -8, 18, -18, 36, -36 ],
8 &[ 1, -1, 2, -3, 8, -8, 18, -18, 36, -36, 54, -54, 96, -96, 144, -144 ]
11 const FIRST_NODE: u8 = 255;
12 const ERR_MAX: u32 = std::u32::MAX;
14 #[derive(Clone, Copy, Default)]
22 stream: Option<NAStreamRef>,
23 pkt: Option<NAPacket>,
28 trellis: Vec<TrellisNode>,
32 fn find_delta(cur_delta: i16, delta_tab: &[i16]) -> (i16, usize) {
34 let mut best_diff = 512;
35 let mut ndelta = delta_tab[0];
36 for (i, &delta) in delta_tab.iter().enumerate() {
37 let ddiff = (delta - cur_delta).abs();
38 if ddiff < best_diff {
60 fn encode_plane(&mut self, bw: &mut BitWriter, in_frm: &NAVideoBuffer<u8>, plane_no: usize) -> EncoderResult<()> {
61 let (width, height) = in_frm.get_dimensions(plane_no);
63 let stride = in_frm.get_stride(plane_no);
64 let offset = in_frm.get_offset(plane_no);
65 let src = in_frm.get_data();
67 let delta_tab = TMRT_DELTA_TAB[(self.dbits - 2) as usize];
68 self.top_line.clear();
69 self.top_line.resize(width, if plane_no == 0 { 0 } else { 0x80 });
71 let step = if self.hscale { 2 } else { 1 };
72 for line in src[offset..].chunks(stride).take(height) {
74 for (&cur, pred) in line[..width].iter().zip(self.top_line.iter_mut()).step_by(step) {
75 let cur = i16::from(cur);
76 let cur_delta = cur - i16::from(*pred) - hor_pred;
78 let (ndelta, idx) = find_delta(cur_delta, delta_tab);
80 bw.write(idx as u32, self.dbits);
82 *pred = (i16::from(*pred) + hor_pred).max(0).min(255) as u8;
89 fn encode_plane_trellis(&mut self, bw: &mut BitWriter, in_frm: &NAVideoBuffer<u8>, plane_no: usize) -> EncoderResult<()> {
90 let (width, height) = in_frm.get_dimensions(plane_no);
92 let stride = in_frm.get_stride(plane_no);
93 let offset = in_frm.get_offset(plane_no);
94 let src = in_frm.get_data();
96 let delta_tab = TMRT_DELTA_TAB[(self.dbits - 2) as usize];
97 self.top_line.clear();
98 self.top_line.resize(width, if plane_no == 0 { 0 } else { 0x80 });
100 let trellis_size = delta_tab.len();
101 self.trellis.resize(trellis_size * (width + 1), TrellisNode::default());
102 self.indices.resize(width, 0);
103 for node in self.trellis[..trellis_size].iter_mut() {
104 node.idx = FIRST_NODE;
107 let step = if self.hscale { 2 } else { 1 };
108 for line in src[offset..].chunks(stride).take(height) {
109 let mut tsplit = trellis_size;
110 for (&cur, &pred) in line[..width].iter().zip(self.top_line.iter()).step_by(step) {
111 let (tprev, tcur) = self.trellis.split_at_mut(tsplit);
112 let hist = &tprev[tprev.len() - trellis_size..];
114 let pix_val = i32::from(cur);
115 let top_val = i16::from(pred);
116 for (dst, &delta) in tcur.iter_mut().zip(delta_tab.iter()) {
118 for (idx, src) in hist.iter().enumerate() {
119 if src.err == ERR_MAX {
122 let nval = i32::from((top_val + src.hpred + delta).max(0).min(255));
123 let new_err = src.err + (((nval - pix_val) * (nval - pix_val)) as u32);
124 if new_err < dst.err {
127 dst.hpred = src.hpred + delta;
132 tsplit += trellis_size;
135 tsplit -= trellis_size;
136 let mut best_idx = 0;
137 let mut best_err = self.trellis[tsplit].err;
139 for (idx, node) in self.trellis[tsplit..].iter().take(trellis_size).enumerate() {
140 if node.err < best_err {
141 best_idx = idx as u8;
146 let mut cur_idx = best_idx;
147 for dst in self.indices[..width / step].iter_mut().rev() {
149 cur_idx = self.trellis[tsplit + (cur_idx as usize)].idx;
150 tsplit -= trellis_size;
153 let mut hor_pred = 0;
154 for (pred, &idx) in self.top_line.iter_mut().step_by(step).zip(self.indices.iter()) {
155 bw.write(u32::from(idx), self.dbits);
156 hor_pred += delta_tab[usize::from(idx)];
157 *pred = (i16::from(*pred) + hor_pred).max(0).min(255) as u8;
165 impl NAEncoder for TMRTEncoder {
166 fn negotiate_format(&self, encinfo: &EncodeParameters) -> EncoderResult<EncodeParameters> {
167 match encinfo.format {
168 NACodecTypeInfo::None => {
169 Ok(EncodeParameters {
170 format: NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, true, YUV410_FORMAT)),
174 NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError),
175 NACodecTypeInfo::Video(vinfo) => {
176 let pix_fmt = YUV410_FORMAT;
177 let outinfo = NAVideoInfo::new((vinfo.width + 3) & !3, (vinfo.height + 3) & !3, false, pix_fmt);
178 let mut ofmt = *encinfo;
179 ofmt.format = NACodecTypeInfo::Video(outinfo);
184 fn get_capabilities(&self) -> u64 { ENC_CAPS_PARAMCHANGE }
185 fn init(&mut self, stream_id: u32, encinfo: EncodeParameters) -> EncoderResult<NAStreamRef> {
186 match encinfo.format {
187 NACodecTypeInfo::None => Err(EncoderError::FormatError),
188 NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError),
189 NACodecTypeInfo::Video(vinfo) => {
190 if vinfo.format != YUV410_FORMAT {
191 return Err(EncoderError::FormatError);
193 if ((vinfo.width | vinfo.height) & 3) != 0 {
194 return Err(EncoderError::FormatError);
196 if (vinfo.width | vinfo.height) >= (1 << 10) {
197 return Err(EncoderError::FormatError);
200 let out_info = NAVideoInfo::new(vinfo.width, vinfo.height, false, vinfo.format);
201 let info = NACodecInfo::new("truemotionrt", NACodecTypeInfo::Video(out_info), None);
202 let mut stream = NAStream::new(StreamType::Video, stream_id, info, encinfo.tb_num, encinfo.tb_den, 0);
203 stream.set_num(stream_id as usize);
204 let stream = stream.into_ref();
206 self.stream = Some(stream.clone());
212 fn encode(&mut self, frm: &NAFrame) -> EncoderResult<()> {
213 let buf = frm.get_buffer();
214 if let Some(ref vbuf) = buf.get_vbuf() {
215 let mut dbuf = Vec::with_capacity(4);
216 let mut gw = GrowableMemoryWriter::new_write(&mut dbuf);
217 let mut bw = ByteWriter::new(&mut gw);
219 bw.write_byte(0)?; // header size
221 bw.write_byte(self.dbits)?;
223 bw.write_byte(self.hscale as u8)?;
224 bw.write_byte(0)?; // pad
225 let (width, height) = vbuf.get_dimensions(0);
226 bw.write_u16le(height as u16)?;
227 bw.write_u16le(width as u16)?;
228 bw.write_u16le(0)?; // pad
229 bw.write_u32le(0)?; // full frame_size
230 bw.write_u16le(0)?; // pad
232 let hdr_size = bw.tell() as usize;
234 bw.write_u32le(0)?; // data size
236 let mut bw = BitWriter::new(dbuf, BitWriterMode::LE);
237 if !self.do_trellis {
239 self.encode_plane(&mut bw, vbuf, plane)?;
243 self.encode_plane_trellis(&mut bw, vbuf, plane)?;
248 let frame_size = dbuf.len() as u32;
249 write_u32le(&mut dbuf[12..], frame_size)?;
250 write_u32le(&mut dbuf[hdr_size..], frame_size - (hdr_size as u32) - 4)?;
252 dbuf[0] = ((hdr_size | 0x80) as u8).rotate_right(3);
253 for i in (1..hdr_size).rev() {
254 dbuf[i] ^= dbuf[i + 1];
257 self.pkt = Some(NAPacket::new(self.stream.clone().unwrap(), frm.ts, true, dbuf));
260 Err(EncoderError::InvalidParameters)
263 fn get_packet(&mut self) -> EncoderResult<Option<NAPacket>> {
265 std::mem::swap(&mut self.pkt, &mut npkt);
268 fn flush(&mut self) -> EncoderResult<()> {
273 const ENCODER_OPTS: &[NAOptionDefinition] = &[
275 name: "bits", description: "Bits per delta",
276 opt_type: NAOptionDefinitionType::Int(Some(2), Some(4)) },
278 name: "hscale", description: "Horizontal scaling mode",
279 opt_type: NAOptionDefinitionType::Bool },
281 name: "trellis", description: "Trellis search for optimal deltas",
282 opt_type: NAOptionDefinitionType::Bool },
285 impl NAOptionHandler for TMRTEncoder {
286 fn get_supported_options(&self) -> &[NAOptionDefinition] { ENCODER_OPTS }
287 fn set_options(&mut self, options: &[NAOption]) {
288 for option in options.iter() {
289 for opt_def in ENCODER_OPTS.iter() {
290 if opt_def.check(option).is_ok() {
293 if let NAValue::Int(val) = option.value {
294 self.dbits = val as u8;
298 if let NAValue::Bool(val) = option.value {
303 if let NAValue::Bool(val) = option.value {
304 self.do_trellis = val;
313 fn query_option_value(&self, name: &str) -> Option<NAValue> {
315 "bits" => Some(NAValue::Int(i64::from(self.dbits))),
316 "hscale" => Some(NAValue::Bool(self.hscale)),
317 "trellis" => Some(NAValue::Bool(self.do_trellis)),
323 pub fn get_encoder() -> Box<dyn NAEncoder + Send> {
324 Box::new(TMRTEncoder::new())
329 use nihav_core::codecs::*;
330 use nihav_core::demuxers::*;
331 use nihav_core::muxers::*;
333 use nihav_commonfmt::*;
334 use nihav_codec_support::test::enc_video::*;
336 fn encode_test(name: &'static str, enc_options: &[NAOption], hash: &[u32; 4]) {
337 let mut dmx_reg = RegisteredDemuxers::new();
338 generic_register_all_demuxers(&mut dmx_reg);
339 let mut dec_reg = RegisteredDecoders::new();
340 duck_register_all_decoders(&mut dec_reg);
341 let mut mux_reg = RegisteredMuxers::new();
342 generic_register_all_muxers(&mut mux_reg);
343 let mut enc_reg = RegisteredEncoders::new();
344 duck_register_all_encoders(&mut enc_reg);
346 // sample from private collection
347 let dec_config = DecoderTestParams {
349 in_name: "assets/Duck/tr20_low.avi",
350 stream_type: StreamType::Video,
354 let enc_config = EncoderTestParams {
356 enc_name: "truemotionrt",
360 let dst_vinfo = NAVideoInfo {
363 format: YUV410_FORMAT,
367 let enc_params = EncodeParameters {
368 format: NACodecTypeInfo::Video(dst_vinfo),
375 //test_encoding_to_file(&dec_config, &enc_config, enc_params, enc_options);
376 test_encoding_md5(&dec_config, &enc_config, enc_params, enc_options, hash);
379 fn test_truemotionrt_encoder_2bit() {
381 NAOption { name: "bits", value: NAValue::Int(2) },
383 encode_test("tmrt-2bit.avi", enc_options, &[0x2c2a5ae3, 0xde1646e4, 0xf76bb219, 0xd09602fa]);
386 fn test_truemotionrt_encoder_3bit() {
388 NAOption { name: "bits", value: NAValue::Int(3) },
390 encode_test("tmrt-3bit.avi", enc_options, &[0x36cf8f48, 0x3e8ff2ce, 0x6f3822cf, 0xf7fbf19d]);
393 fn test_truemotionrt_encoder_4bit() {
395 NAOption { name: "bits", value: NAValue::Int(4) },
397 encode_test("tmrt-4bit.avi", enc_options, &[0xa5a7fbe3, 0x7bac0b2b, 0x2af6f97f, 0xa65cd1fc]);
400 fn test_truemotionrt_encoder_hscale() {
402 NAOption { name: "bits", value: NAValue::Int(3) },
403 NAOption { name: "hscale", value: NAValue::Bool(true) },
405 encode_test("tmrt-hscale.avi", enc_options, &[0xc17afa21, 0x5bdf49c9, 0x57997840, 0xfc2f17b6]);
408 fn test_truemotionrt_encoder_trellis() {
410 NAOption { name: "bits", value: NAValue::Int(3) },
411 NAOption { name: "hscale", value: NAValue::Bool(true) },
412 NAOption { name: "trellis", value: NAValue::Bool(true) },
414 encode_test("tmrt-trellis.avi", enc_options, &[0x3586b450, 0x6ea1ed31, 0xe14c0c7d, 0x0886bc4f]);