]>
Commit | Line | Data |
---|---|---|
1 | use nihav_core::codecs::*; | |
2 | use nihav_core::io::byteio::*; | |
3 | ||
4 | mod cell; | |
5 | use cell::*; | |
6 | mod mv; | |
7 | use mv::*; | |
8 | mod ratectl; | |
9 | use ratectl::*; | |
10 | mod tree; | |
11 | pub use tree::{Indeo3Cell, Plane}; | |
12 | use tree::Indeo3PrimaryTree; | |
13 | ||
14 | const OS_HEADER_SIZE: usize = 16; | |
15 | const BITSTREAM_HEADER_SIZE: usize = 48; | |
16 | const HDR_FIELD_2: u32 = 0; | |
17 | const FRMH_TAG: u32 = ((b'F' as u32) << 24) | ((b'R' as u32) << 16) | |
18 | | ((b'M' as u32) << 8) | (b'H' as u32); | |
19 | const PLANE_OFFSETS: usize = 32; | |
20 | ||
21 | const CB_SELECTORS: [u8; 16] = [ | |
22 | 0x02, 0x14, 0x26, 0x38, 0x4A, 0x5C, 0x6E, 0x7F, | |
23 | 0x82, 0x94, 0xA6, 0xB8, 0xCA, 0xDC, 0xEE, 0xFF | |
24 | ]; | |
25 | ||
26 | const PLANE_ORDER: [usize; 3] = [1, 2, 0]; | |
27 | ||
28 | pub struct Indeo3Writer<'a> { | |
29 | dst: &'a mut Vec<u8>, | |
30 | bitbuf: u8, | |
31 | bits: u8, | |
32 | bitpos: Option<usize>, | |
33 | } | |
34 | ||
35 | impl<'a> Indeo3Writer<'a> { | |
36 | fn new(dst: &'a mut Vec<u8>) -> Self { | |
37 | Self { | |
38 | dst, | |
39 | bitbuf: 0, | |
40 | bits: 0, | |
41 | bitpos: None, | |
42 | } | |
43 | } | |
44 | pub fn put_byte(&mut self, b: u8) { | |
45 | self.dst.push(b); | |
46 | } | |
47 | pub fn put_2bits(&mut self, val: u8) { | |
48 | if self.bits == 0 { | |
49 | self.bitpos = Some(self.dst.len()); | |
50 | self.dst.push(0); | |
51 | } | |
52 | self.bitbuf |= val << (6 - self.bits); | |
53 | self.bits += 2; | |
54 | if self.bits == 8 { | |
55 | let bpos = self.bitpos.unwrap_or(0); | |
56 | self.dst[bpos] = self.bitbuf; | |
57 | self.bitbuf = 0; | |
58 | self.bits = 0; | |
59 | self.bitpos = None; | |
60 | } | |
61 | } | |
62 | } | |
63 | ||
64 | impl<'a> Drop for Indeo3Writer<'a> { | |
65 | fn drop(&mut self) { | |
66 | if self.bits != 0 { | |
67 | let bpos = self.bitpos.unwrap_or(0); | |
68 | self.dst[bpos] = self.bitbuf; | |
69 | } | |
70 | } | |
71 | } | |
72 | ||
73 | #[derive(Default)] | |
74 | struct Indeo3Frame { | |
75 | plane: [Plane; 3], | |
76 | } | |
77 | ||
78 | impl Indeo3Frame { | |
79 | fn new() -> Self { Self::default() } | |
80 | fn alloc(&mut self, width: usize, height: usize) { | |
81 | self.plane[0].alloc(width, height, 40); | |
82 | self.plane[1].alloc(width / 4, height / 4, 10); | |
83 | self.plane[2].alloc(width / 4, height / 4, 10); | |
84 | } | |
85 | fn fill(&mut self, vbuf: &NAVideoBufferRef<u8>) { | |
86 | let data = vbuf.get_data(); | |
87 | for (plane_no, plane) in self.plane.iter_mut().enumerate() { | |
88 | plane.fill(&data[vbuf.get_offset(plane_no)..], vbuf.get_stride(plane_no)); | |
89 | } | |
90 | } | |
91 | fn clear_mvs(&mut self) { | |
92 | for plane in self.plane.iter_mut() { | |
93 | plane.clear_mvs(); | |
94 | } | |
95 | } | |
96 | } | |
97 | ||
98 | struct Indeo3Encoder { | |
99 | stream: Option<NAStreamRef>, | |
100 | pkt: Option<NAPacket>, | |
101 | cframe: Indeo3Frame, | |
102 | pframe: Indeo3Frame, | |
103 | cenc: CellEncoder, | |
104 | mv_est: MotionEstimator, | |
105 | rc: RateControl, | |
106 | frameno: u32, | |
107 | buf_sel: bool, | |
108 | width: usize, | |
109 | height: usize, | |
110 | ||
111 | debug_tree: bool, | |
112 | debug_frm: bool, | |
113 | try_again: bool, | |
114 | } | |
115 | ||
116 | impl Indeo3Encoder { | |
117 | fn new() -> Self { | |
118 | Self { | |
119 | stream: None, | |
120 | pkt: None, | |
121 | cframe: Indeo3Frame::new(), | |
122 | pframe: Indeo3Frame::new(), | |
123 | cenc: CellEncoder::new(), | |
124 | mv_est: MotionEstimator::new(), | |
125 | rc: RateControl::new(), | |
126 | frameno: 0, | |
127 | buf_sel: false, | |
128 | width: 0, | |
129 | height: 0, | |
130 | ||
131 | debug_tree: false, | |
132 | debug_frm: false, | |
133 | try_again: false, | |
134 | } | |
135 | } | |
136 | fn encode_planes(&mut self, dbuf: &mut Vec<u8>, trees: &[Box<Indeo3PrimaryTree>], is_intra: bool) -> EncoderResult<()> { | |
137 | for (&planeno, tree) in PLANE_ORDER.iter().zip(trees.iter()) { | |
138 | let offset = dbuf.len(); | |
139 | let ref_plane = &self.pframe.plane[planeno]; | |
140 | ||
141 | let mut mc_count = [0; 4]; | |
142 | let mvs = &self.cframe.plane[planeno].mvs; | |
143 | write_u32le(&mut mc_count, mvs.len() as u32)?; | |
144 | dbuf.extend_from_slice(&mc_count); | |
145 | for &(mv, _) in mvs.iter() { | |
146 | dbuf.push(mv.y as u8); | |
147 | dbuf.push(mv.x as u8); | |
148 | } | |
149 | ||
150 | let mut iw = Indeo3Writer::new(dbuf); | |
151 | self.cframe.plane[planeno].encode_tree(&mut iw, tree, &mut self.cenc, ref_plane); | |
152 | drop(iw); | |
153 | while (dbuf.len() & 3) != 0 { | |
154 | dbuf.push(0); | |
155 | } | |
156 | ||
157 | let plane_off = PLANE_OFFSETS + 4 * if planeno > 0 { planeno ^ 3 } else { 0 }; | |
158 | write_u32le(&mut dbuf[plane_off..], (offset - OS_HEADER_SIZE) as u32)?; | |
159 | } | |
160 | ||
161 | let mut checksum = 0; | |
162 | for plane in self.cframe.plane.iter() { | |
163 | checksum ^= plane.checksum(); | |
164 | } | |
165 | write_u16le(&mut dbuf[26..], checksum * 2)?; | |
166 | ||
167 | let size = (dbuf.len() - OS_HEADER_SIZE) as u32; | |
168 | write_u32le(&mut dbuf[8..], self.frameno ^ HDR_FIELD_2 ^ FRMH_TAG ^ size)?; | |
169 | write_u32le(&mut dbuf[12..], size)?; | |
170 | write_u32le(&mut dbuf[20..], size * 8)?; | |
171 | ||
172 | if is_intra { | |
173 | dbuf.extend_from_slice(b"\x0d\x0aVer 3.99.00.00\x0d\x0a\x00"); | |
174 | while (dbuf.len() & 3) != 0 { | |
175 | dbuf.push(0); | |
176 | } | |
177 | } | |
178 | ||
179 | Ok(()) | |
180 | } | |
181 | } | |
182 | ||
183 | impl NAEncoder for Indeo3Encoder { | |
184 | fn negotiate_format(&self, encinfo: &EncodeParameters) -> EncoderResult<EncodeParameters> { | |
185 | match encinfo.format { | |
186 | NACodecTypeInfo::None => { | |
187 | Ok(EncodeParameters { | |
188 | format: NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, true, YUV410_FORMAT)), | |
189 | ..Default::default() | |
190 | }) | |
191 | }, | |
192 | NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError), | |
193 | NACodecTypeInfo::Video(vinfo) => { | |
194 | let pix_fmt = YUV410_FORMAT; | |
195 | let outinfo = NAVideoInfo::new((vinfo.width + 15) & !15, (vinfo.height + 15) & !15, false, pix_fmt); | |
196 | let mut ofmt = *encinfo; | |
197 | ofmt.format = NACodecTypeInfo::Video(outinfo); | |
198 | Ok(ofmt) | |
199 | } | |
200 | } | |
201 | } | |
202 | fn get_capabilities(&self) -> u64 { ENC_CAPS_SKIPFRAME } | |
203 | fn init(&mut self, stream_id: u32, encinfo: EncodeParameters) -> EncoderResult<NAStreamRef> { | |
204 | match encinfo.format { | |
205 | NACodecTypeInfo::None => Err(EncoderError::FormatError), | |
206 | NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError), | |
207 | NACodecTypeInfo::Video(vinfo) => { | |
208 | if vinfo.format != YUV410_FORMAT { | |
209 | return Err(EncoderError::FormatError); | |
210 | } | |
211 | if ((vinfo.width | vinfo.height) & 15) != 0 { | |
212 | return Err(EncoderError::FormatError); | |
213 | } | |
214 | if (vinfo.width > 640) || (vinfo.height > 480) { | |
215 | return Err(EncoderError::FormatError); | |
216 | } | |
217 | ||
218 | self.width = vinfo.width; | |
219 | self.height = vinfo.height; | |
220 | ||
221 | let out_info = NAVideoInfo::new(vinfo.width, vinfo.height, false, vinfo.format); | |
222 | let info = NACodecInfo::new("indeo3", NACodecTypeInfo::Video(out_info), None); | |
223 | let mut stream = NAStream::new(StreamType::Video, stream_id, info, encinfo.tb_num, encinfo.tb_den, 0); | |
224 | stream.set_num(stream_id as usize); | |
225 | let stream = stream.into_ref(); | |
226 | ||
227 | self.stream = Some(stream.clone()); | |
228 | ||
229 | self.cframe.alloc(vinfo.width, vinfo.height); | |
230 | self.pframe.alloc(vinfo.width, vinfo.height); | |
231 | ||
232 | self.rc.set_bitrate(encinfo.bitrate, encinfo.tb_num, encinfo.tb_den); | |
233 | self.rc.set_quality(encinfo.quality); | |
234 | ||
235 | Ok(stream) | |
236 | }, | |
237 | } | |
238 | } | |
239 | fn encode(&mut self, frm: &NAFrame) -> EncoderResult<()> { | |
240 | let buf = frm.get_buffer(); | |
241 | if self.debug_tree || self.debug_frm { | |
242 | println!("frame {}:", self.frameno); | |
243 | } | |
244 | ||
245 | let mut skip_frame = frm.get_frame_type() == FrameType::Skip; | |
246 | if let NABufferType::None = buf { | |
247 | skip_frame = true; | |
248 | } | |
249 | if skip_frame { | |
250 | let mut dbuf = Vec::with_capacity(16); | |
251 | let mut gw = GrowableMemoryWriter::new_write(&mut dbuf); | |
252 | let mut bw = ByteWriter::new(&mut gw); | |
253 | ||
254 | // OS header | |
255 | bw.write_u32le(self.frameno)?; | |
256 | bw.write_u32le(HDR_FIELD_2)?; | |
257 | bw.write_u32le(0)?; // check | |
258 | bw.write_u32le(0)?; // size | |
259 | ||
260 | // bitstream header | |
261 | bw.write_u16le(32)?; // version | |
262 | bw.write_u16le(0)?; | |
263 | bw.write_u32le(0)?; // data size in bits | |
264 | bw.write_byte(0)?; // cb offset | |
265 | bw.write_byte(14)?; // reserved | |
266 | bw.write_u16le(0)?; // checksum | |
267 | bw.write_u16le(self.height as u16)?; | |
268 | bw.write_u16le(self.width as u16)?; | |
269 | ||
270 | let size = (dbuf.len() - OS_HEADER_SIZE) as u32; | |
271 | write_u32le(&mut dbuf[8..], self.frameno ^ HDR_FIELD_2 ^ FRMH_TAG ^ size)?; | |
272 | write_u32le(&mut dbuf[12..], size)?; | |
273 | write_u32le(&mut dbuf[20..], size * 8)?; | |
274 | ||
275 | let fsize = dbuf.len() as u32; | |
276 | self.rc.advance(fsize); | |
277 | self.frameno += 1; | |
278 | ||
279 | self.pkt = Some(NAPacket::new(self.stream.clone().unwrap(), frm.ts, false, dbuf)); | |
280 | return Ok(()); | |
281 | } | |
282 | ||
283 | if let Some(ref vbuf) = buf.get_vbuf() { | |
284 | let mut dbuf = Vec::with_capacity(16); | |
285 | let mut gw = GrowableMemoryWriter::new_write(&mut dbuf); | |
286 | let mut bw = ByteWriter::new(&mut gw); | |
287 | ||
288 | let (width, height) = vbuf.get_dimensions(0); | |
289 | if width != self.width || height != self.height { | |
290 | self.width = width; | |
291 | self.height = height; | |
292 | self.cframe.alloc(width, height); | |
293 | self.pframe.alloc(width, height); | |
294 | self.rc.reset(); | |
295 | } | |
296 | ||
297 | let (is_intra, quant) = self.rc.get_quant(self.frameno); | |
298 | self.cenc.quant = quant; | |
299 | ||
300 | if is_intra { | |
301 | self.buf_sel = false; | |
302 | } else { | |
303 | self.buf_sel = !self.buf_sel; | |
304 | } | |
305 | ||
306 | self.cframe.fill(vbuf); | |
307 | self.cframe.clear_mvs(); | |
308 | ||
309 | // OS header | |
310 | bw.write_u32le(self.frameno)?; | |
311 | bw.write_u32le(HDR_FIELD_2)?; | |
312 | bw.write_u32le(0)?; // check | |
313 | bw.write_u32le(0)?; // size | |
314 | ||
315 | // bitstream header | |
316 | bw.write_u16le(32)?; // version | |
317 | let mut flags = 0; | |
318 | if is_intra { | |
319 | flags |= 0x5; | |
320 | } else { | |
321 | flags |= 1; | |
322 | if self.buf_sel { | |
323 | flags |= 1 << 9; | |
324 | } | |
325 | } | |
326 | bw.write_u16le(flags)?; | |
327 | bw.write_u32le(0)?; // data size in bits | |
328 | bw.write_byte(0)?; // cb offset | |
329 | bw.write_byte(14)?; // reserved | |
330 | bw.write_u16le(0)?; // checksum | |
331 | bw.write_u16le(height as u16)?; | |
332 | bw.write_u16le(width as u16)?; | |
333 | for _ in 0..3 { | |
334 | bw.write_u32le(0)?; // plane data offset | |
335 | } | |
336 | bw.write_u32le(0)?; // reserved | |
337 | bw.write_buf(&CB_SELECTORS)?; | |
338 | ||
339 | let mut trees = Vec::with_capacity(PLANE_ORDER.len()); | |
340 | ||
341 | // prepare plane data structure | |
342 | for &planeno in PLANE_ORDER.iter() { | |
343 | let ref_plane = &self.pframe.plane[planeno]; | |
344 | let mut tree = self.cframe.plane[planeno].find_cells(is_intra, ref_plane, &self.mv_est); | |
345 | if self.debug_tree { | |
346 | println!(" tree for plane {}:", planeno); | |
347 | tree.print(); | |
348 | } | |
349 | let mvs = &mut self.cframe.plane[planeno].mvs; | |
350 | if mvs.len() > 256 { | |
351 | compact_mvs(mvs); | |
352 | self.cframe.plane[planeno].prune_extra_mvs(&mut tree); | |
353 | } | |
354 | trees.push(tree); | |
355 | } | |
356 | ||
357 | self.encode_planes(&mut dbuf, &trees, is_intra)?; | |
358 | ||
359 | let cur_quant = self.cenc.quant.unwrap_or(42); | |
360 | if self.try_again && !is_intra && cur_quant < 8 { | |
361 | let expected_size = self.rc.get_expected_size(); | |
362 | if expected_size > 0 { | |
363 | let cur_size = dbuf.len() as u32; | |
364 | // try re-encoding frame if possible | |
365 | if cur_size > expected_size * 3 / 2 { | |
366 | self.cframe.fill(vbuf); | |
367 | let new_quant = if cur_quant < 7 { | |
368 | cur_quant + 1 | |
369 | } else { | |
370 | cur_quant - 1 | |
371 | }; | |
372 | self.cenc.quant = Some(new_quant); | |
373 | dbuf.truncate(OS_HEADER_SIZE + BITSTREAM_HEADER_SIZE); | |
374 | self.encode_planes(&mut dbuf, &trees, is_intra)?; | |
375 | } | |
376 | } | |
377 | } | |
378 | ||
379 | if self.debug_frm { | |
380 | for plane in self.cframe.plane.iter() { | |
381 | for (y, line) in plane.data.chunks(plane.width).enumerate() { | |
382 | print!(" {:3}:", y); | |
383 | for &el in line.iter() { print!(" {:02X}", el); } | |
384 | println!(); | |
385 | } | |
386 | println!(); | |
387 | } | |
388 | } | |
389 | ||
390 | std::mem::swap(&mut self.cframe, &mut self.pframe); | |
391 | self.frameno += 1; | |
392 | ||
393 | let fsize = dbuf.len() as u32; | |
394 | self.rc.advance(fsize); | |
395 | ||
396 | self.pkt = Some(NAPacket::new(self.stream.clone().unwrap(), frm.ts, is_intra, dbuf)); | |
397 | Ok(()) | |
398 | } else { | |
399 | Err(EncoderError::InvalidParameters) | |
400 | } | |
401 | } | |
402 | fn get_packet(&mut self) -> EncoderResult<Option<NAPacket>> { | |
403 | let mut npkt = None; | |
404 | std::mem::swap(&mut self.pkt, &mut npkt); | |
405 | Ok(npkt) | |
406 | } | |
407 | fn flush(&mut self) -> EncoderResult<()> { | |
408 | Ok(()) | |
409 | } | |
410 | } | |
411 | ||
412 | const DEBUG_TREE_OPTION: &str = "debug_tree"; | |
413 | const DEBUG_FRAME_OPTION: &str = "debug_frame"; | |
414 | const MV_RANGE_OPTION: &str = "mv_range"; | |
415 | const MV_FLAT_OPTION: &str = "mv_flat_threshold"; | |
416 | const MV_THRESHOLD_OPTION: &str = "mv_threshold"; | |
417 | const CELL_I_THRESHOLD_OPTION: &str = "cell_i_threshold"; | |
418 | const CELL_P_THRESHOLD_OPTION: &str = "cell_p_threshold"; | |
419 | const DO_RLE_OPTION: &str = "rle"; | |
420 | const TRY_AGAIN_OPTION: &str = "try_recompress"; | |
421 | ||
422 | const ENCODER_OPTS: &[NAOptionDefinition] = &[ | |
423 | NAOptionDefinition { | |
424 | name: KEYFRAME_OPTION, description: KEYFRAME_OPTION_DESC, | |
425 | opt_type: NAOptionDefinitionType::Int(Some(0), Some(128)) }, | |
426 | NAOptionDefinition { | |
427 | name: DEBUG_TREE_OPTION, description: "Print frame trees", | |
428 | opt_type: NAOptionDefinitionType::Bool }, | |
429 | NAOptionDefinition { | |
430 | name: DEBUG_FRAME_OPTION, description: "Print encoder-reconstructed frames", | |
431 | opt_type: NAOptionDefinitionType::Bool }, | |
432 | NAOptionDefinition { | |
433 | name: MV_RANGE_OPTION, description: "Motion search range", | |
434 | opt_type: NAOptionDefinitionType::Int(Some(0), Some(120)) }, | |
435 | NAOptionDefinition { | |
436 | name: MV_FLAT_OPTION, description: "Threshold for coding cell as skipped one", | |
437 | opt_type: NAOptionDefinitionType::Int(Some(0), Some(1000)) }, | |
438 | NAOptionDefinition { | |
439 | name: MV_THRESHOLD_OPTION, description: "Threshold for coding cell as inter", | |
440 | opt_type: NAOptionDefinitionType::Int(Some(0), Some(1000)) }, | |
441 | NAOptionDefinition { | |
442 | name: CELL_I_THRESHOLD_OPTION, description: "Threshold for coding intra block as flat", | |
443 | opt_type: NAOptionDefinitionType::Int(Some(0), Some(128)) }, | |
444 | NAOptionDefinition { | |
445 | name: CELL_P_THRESHOLD_OPTION, description: "Threshold for coding inter cell in coarser mode", | |
446 | opt_type: NAOptionDefinitionType::Int(Some(0), Some(128)) }, | |
447 | NAOptionDefinition { | |
448 | name: DO_RLE_OPTION, description: "Perform zero run length compation", | |
449 | opt_type: NAOptionDefinitionType::Bool }, | |
450 | NAOptionDefinition { | |
451 | name: TRY_AGAIN_OPTION, description: "Try compressing the frame again for the better bitrate fit", | |
452 | opt_type: NAOptionDefinitionType::Bool }, | |
453 | ]; | |
454 | ||
455 | impl NAOptionHandler for Indeo3Encoder { | |
456 | fn get_supported_options(&self) -> &[NAOptionDefinition] { ENCODER_OPTS } | |
457 | fn set_options(&mut self, options: &[NAOption]) { | |
458 | for option in options.iter() { | |
459 | for opt_def in ENCODER_OPTS.iter() { | |
460 | if opt_def.check(option).is_ok() { | |
461 | match option.name { | |
462 | KEYFRAME_OPTION => { | |
463 | if let NAValue::Int(val) = option.value { | |
464 | self.rc.set_key_int(val as u32); | |
465 | } | |
466 | }, | |
467 | DEBUG_TREE_OPTION => { | |
468 | if let NAValue::Bool(val) = option.value { | |
469 | self.debug_tree = val; | |
470 | } | |
471 | }, | |
472 | DEBUG_FRAME_OPTION => { | |
473 | if let NAValue::Bool(val) = option.value { | |
474 | self.debug_frm = val; | |
475 | } | |
476 | }, | |
477 | MV_RANGE_OPTION => { | |
478 | if let NAValue::Int(val) = option.value { | |
479 | self.mv_est.mv_range = val as i8; | |
480 | } | |
481 | }, | |
482 | MV_FLAT_OPTION => { | |
483 | if let NAValue::Int(val) = option.value { | |
484 | self.mv_est.flat_thr = val as u16; | |
485 | } | |
486 | }, | |
487 | MV_THRESHOLD_OPTION => { | |
488 | if let NAValue::Int(val) = option.value { | |
489 | self.mv_est.mv_thr = val as u16; | |
490 | } | |
491 | }, | |
492 | CELL_I_THRESHOLD_OPTION => { | |
493 | if let NAValue::Int(val) = option.value { | |
494 | self.cenc.flat_thr_i = val as u32; | |
495 | } | |
496 | }, | |
497 | CELL_P_THRESHOLD_OPTION => { | |
498 | if let NAValue::Int(val) = option.value { | |
499 | self.cenc.flat_thr_p = val as u32; | |
500 | } | |
501 | }, | |
502 | DO_RLE_OPTION => { | |
503 | if let NAValue::Bool(val) = option.value { | |
504 | self.cenc.do_rle = val; | |
505 | } | |
506 | }, | |
507 | TRY_AGAIN_OPTION => { | |
508 | if let NAValue::Bool(val) = option.value { | |
509 | self.try_again = val; | |
510 | } | |
511 | }, | |
512 | _ => {}, | |
513 | }; | |
514 | } | |
515 | } | |
516 | } | |
517 | } | |
518 | fn query_option_value(&self, name: &str) -> Option<NAValue> { | |
519 | match name { | |
520 | KEYFRAME_OPTION => Some(NAValue::Int(i64::from(self.rc.get_key_int()))), | |
521 | DEBUG_TREE_OPTION => Some(NAValue::Bool(self.debug_tree)), | |
522 | DEBUG_FRAME_OPTION => Some(NAValue::Bool(self.debug_frm)), | |
523 | MV_RANGE_OPTION => Some(NAValue::Int(i64::from(self.mv_est.mv_range))), | |
524 | MV_FLAT_OPTION => Some(NAValue::Int(i64::from(self.mv_est.flat_thr))), | |
525 | MV_THRESHOLD_OPTION => Some(NAValue::Int(i64::from(self.mv_est.mv_thr))), | |
526 | CELL_I_THRESHOLD_OPTION => Some(NAValue::Int(i64::from(self.cenc.flat_thr_i))), | |
527 | CELL_P_THRESHOLD_OPTION => Some(NAValue::Int(i64::from(self.cenc.flat_thr_p))), | |
528 | DO_RLE_OPTION => Some(NAValue::Bool(self.cenc.do_rle)), | |
529 | TRY_AGAIN_OPTION => Some(NAValue::Bool(self.try_again)), | |
530 | _ => None, | |
531 | } | |
532 | } | |
533 | } | |
534 | ||
535 | pub fn get_encoder() -> Box<dyn NAEncoder + Send> { | |
536 | Box::new(Indeo3Encoder::new()) | |
537 | } | |
538 | ||
539 | #[cfg(test)] | |
540 | mod test { | |
541 | use crate::*; | |
542 | use nihav_core::codecs::*; | |
543 | use nihav_core::demuxers::*; | |
544 | use nihav_core::muxers::*; | |
545 | use nihav_commonfmt::*; | |
546 | use nihav_codec_support::test::enc_video::*; | |
547 | ||
548 | #[allow(unused_variables)] | |
549 | fn encode_test(name: &'static str, enc_options: &[NAOption], limit: Option<u64>, hash: &[u32; 4]) { | |
550 | let mut dmx_reg = RegisteredDemuxers::new(); | |
551 | generic_register_all_demuxers(&mut dmx_reg); | |
552 | let mut dec_reg = RegisteredDecoders::new(); | |
553 | indeo_register_all_decoders(&mut dec_reg); | |
554 | let mut mux_reg = RegisteredMuxers::new(); | |
555 | generic_register_all_muxers(&mut mux_reg); | |
556 | let mut enc_reg = RegisteredEncoders::new(); | |
557 | indeo_register_all_encoders(&mut enc_reg); | |
558 | ||
559 | let dec_config = DecoderTestParams { | |
560 | demuxer: "avi", | |
561 | in_name: "assets/Indeo/laser05.avi", | |
562 | stream_type: StreamType::Video, | |
563 | limit, | |
564 | dmx_reg, dec_reg, | |
565 | }; | |
566 | let enc_config = EncoderTestParams { | |
567 | muxer: "avi", | |
568 | enc_name: "indeo3", | |
569 | out_name: name, | |
570 | mux_reg, enc_reg, | |
571 | }; | |
572 | let dst_vinfo = NAVideoInfo { | |
573 | width: 0, | |
574 | height: 0, | |
575 | format: YUV410_FORMAT, | |
576 | flipped: false, | |
577 | bits: 9, | |
578 | }; | |
579 | let enc_params = EncodeParameters { | |
580 | format: NACodecTypeInfo::Video(dst_vinfo), | |
581 | quality: 0, | |
582 | bitrate: 25000 * 8, | |
583 | tb_num: 0, | |
584 | tb_den: 0, | |
585 | flags: 0, | |
586 | }; | |
587 | //test_encoding_to_file(&dec_config, &enc_config, enc_params, enc_options); | |
588 | test_encoding_md5(&dec_config, &enc_config, enc_params, enc_options, hash); | |
589 | } | |
590 | #[test] | |
591 | fn test_indeo3_encoder1() { | |
592 | let enc_options = &[ | |
593 | NAOption { name: super::TRY_AGAIN_OPTION, value: NAValue::Bool(true) }, | |
594 | ]; | |
595 | encode_test("indeo3.avi", enc_options, Some(4), &[0x17d742bc, 0x6f4c1200, 0x79422bac, 0xc46b5dd0]); | |
596 | } | |
597 | /*#[test] | |
598 | fn test_indeo3_roundtrip() { | |
599 | const YPATTERN: [u8; 16] = [32, 72, 40, 106, 80, 20, 33, 58, 77, 140, 121, 100, 83, 57, 30, 11]; | |
600 | const CPATTERN: [u8; 4] = [0x80; 4]; | |
601 | ||
602 | let dst_vinfo = NAVideoInfo { | |
603 | width: 16, | |
604 | height: 16, | |
605 | format: YUV410_FORMAT, | |
606 | flipped: false, | |
607 | bits: 9, | |
608 | }; | |
609 | let enc_params = EncodeParameters { | |
610 | format: NACodecTypeInfo::Video(dst_vinfo), | |
611 | quality: 0, | |
612 | bitrate: 0, | |
613 | tb_num: 0, | |
614 | tb_den: 0, | |
615 | flags: 0, | |
616 | }; | |
617 | ||
618 | let mut ienc = super::get_encoder(); | |
619 | ienc.init(0, enc_params).unwrap(); | |
620 | let mut buffer = alloc_video_buffer(dst_vinfo, 2).unwrap(); | |
621 | if let NABufferType::Video(ref mut buf) = buffer { | |
622 | let vbuf = NASimpleVideoFrame::from_video_buf(buf).unwrap(); | |
623 | for i in 0..16 { | |
624 | vbuf.data[vbuf.offset[0] + i * vbuf.stride[0]..][..16].copy_from_slice(&YPATTERN); | |
625 | } | |
626 | for plane in 1..3 { | |
627 | for i in 0..4 { | |
628 | vbuf.data[vbuf.offset[plane] + i * vbuf.stride[plane]..][..4].copy_from_slice(&CPATTERN); | |
629 | } | |
630 | } | |
631 | } | |
632 | let info = NACodecInfo::new("indeo3", NACodecTypeInfo::Video(dst_vinfo), None).into_ref(); | |
633 | let frm = NAFrame::new(NATimeInfo::new(Some(0), None, None, 1, 12), FrameType::I, true, info.clone(), buffer); | |
634 | //ienc.set_options(&[NAOption{ name: super::DEBUG_FRAME_OPTION, value: NAValue::Bool(true) }]); | |
635 | ienc.encode(&frm).unwrap(); | |
636 | let pkt = ienc.get_packet().unwrap().unwrap(); | |
637 | println!(" pkt size {}", pkt.get_buffer().len()); | |
638 | ||
639 | let mut dec_reg = RegisteredDecoders::new(); | |
640 | indeo_register_all_decoders(&mut dec_reg); | |
641 | let decfunc = dec_reg.find_decoder("indeo3").unwrap(); | |
642 | let mut dec = (decfunc)(); | |
643 | let mut dsupp = Box::new(NADecoderSupport::new()); | |
644 | dec.init(&mut dsupp, info).unwrap(); | |
645 | dec.set_options(&[NAOption{ name: "checksum", value: NAValue::Bool(true) }]); | |
646 | let dst = dec.decode(&mut dsupp, &pkt).unwrap(); | |
647 | if let NABufferType::Video(ref vbuf) = dst.get_buffer() { | |
648 | for plane in 0..3 { | |
649 | let size = if plane == 0 { 16 } else { 4 }; | |
650 | let start = vbuf.get_offset(plane); | |
651 | for line in vbuf.get_data()[start..].chunks(vbuf.get_stride(plane)).take(size) { | |
652 | print!(" "); | |
653 | for &el in line[..size].iter() { | |
654 | print!(" {:02X}", el >> 1); | |
655 | } | |
656 | println!(); | |
657 | } | |
658 | if plane == 0 { | |
659 | print!("ref"); | |
660 | for &el in YPATTERN.iter() { print!(" {:02X}", el >> 1); } println!(); | |
661 | } | |
662 | println!(); | |
663 | } | |
664 | } | |
665 | panic!("end"); | |
666 | }*/ | |
667 | } |