1 use nihav_core::codecs::*;
2 use nihav_core::io::byteio::*;
3 use nihav_codec_support::vq::*;
5 #[derive(Default,Clone,Copy,PartialEq)]
9 fn unpack(self) -> (u16, u16, u16) {
10 ((self.0 >> 10) & 0x1F, (self.0 >> 5) & 0x1F, self.0 & 0x1F)
12 fn pack(r: u16, g: u16, b: u16) -> Self {
13 Pixel16((r << 10) | (g << 5) | b)
15 fn invalid() -> Self { Self(0x8000) }
16 fn is_invalid(self) -> bool { self == Self::invalid() }
18 impl VQElement for Pixel16 {
19 fn dist(&self, rval: Self) -> u32 {
20 let (r0, g0, b0) = self.unpack();
21 let (r1, g1, b1) = rval.unpack();
22 let rd = i32::from(r0) - i32::from(r1);
23 let gd = i32::from(g0) - i32::from(g1);
24 let bd = i32::from(b0) - i32::from(b1);
25 (rd * rd + gd * gd + bd * bd) as u32
27 fn min_cw() -> Self { Pixel16(0x0000) }
28 fn max_cw() -> Self { Pixel16(0x7FFF) }
29 fn min(&self, rval: Self) -> Self {
30 let (r0, g0, b0) = self.unpack();
31 let (r1, g1, b1) = rval.unpack();
32 Self::pack(r0.min(r1), g0.min(g1), b0.min(b1))
34 fn max(&self, rval: Self) -> Self {
35 let (r0, g0, b0) = self.unpack();
36 let (r1, g1, b1) = rval.unpack();
37 Self::pack(r0.max(r1), g0.max(g1), b0.max(b1))
39 fn num_components() -> usize { 3 }
40 fn sort_by_component(arr: &mut [Self], component: usize) {
41 let mut counts = [0; 32];
42 for pix in arr.iter() {
43 let (r, g, b) = pix.unpack();
44 let idx = match component {
51 let mut offs = [0; 32];
53 offs[i + 1] = offs[i] + counts[i];
55 let mut dst = [Pixel16(0); 16];
56 assert!(dst.len() >= arr.len());
57 for pix in arr.iter() {
58 let (r, g, b) = pix.unpack();
59 let idx = match component {
64 dst[offs[idx]] = *pix;
68 arr.copy_from_slice(&dst[..len]);
70 fn max_dist_component(min: &Self, max: &Self) -> usize {
71 let (r0, g0, b0) = max.unpack();
72 let (r1, g1, b1) = min.unpack();
73 let rd = u32::from(r0) - u32::from(r1);
74 let gd = u32::from(g0) - u32::from(g1);
75 let bd = u32::from(b0) - u32::from(b1);
76 if rd > gd && rd >= bd {
78 } else if bd > rd && bd > gd {
93 impl VQElementSum<Pixel16> for Pixel16Sum {
94 fn zero() -> Self { Pixel16Sum { rsum: 0, gsum: 0, bsum: 0, count: 0 } }
95 fn add(&mut self, rval: Pixel16, count: u64) {
96 let (r, g, b) = rval.unpack();
97 let count = count as u16;
98 self.rsum += r * count;
99 self.gsum += g * count;
100 self.bsum += b * count;
103 fn get_centroid(&self) -> Pixel16 {
105 let r = (self.rsum + self.count / 2) / self.count;
106 let g = (self.gsum + self.count / 2) / self.count;
107 let b = (self.bsum + self.count / 2) / self.count;
108 Pixel16::pack(r, g, b)
124 clr8: [[Pixel16; 2]; 4],
128 fn calc_clrs(buf: &[Pixel16; 16]) -> Option<(Pixel16, Pixel16)> {
130 let mut clr1 = Pixel16::invalid();
131 for &pix in buf[1..].iter() {
132 if pix != clr0 && pix != clr1 {
133 if clr1.is_invalid() {
142 fn calc_stats(&mut self, buf: &[Pixel16; 16]) {
143 let mut filled = false;
144 let mut two_clr = false;
145 if let Some((clr0, clr1)) = Self::calc_clrs(buf) {
147 self.clr2[1] = if !clr1.is_invalid() { clr1 } else { clr0 };
149 self.fill_val = Pixel16(buf[0].0 & !0x400);
156 let mut avg = Pixel16Sum::zero();
157 for pix in buf.iter() {
160 self.fill_val = Pixel16(avg.get_centroid().0 & !0x400);
161 for pix in buf.iter() {
162 self.fill_dist += pix.dist(self.fill_val);
165 if self.fill_dist == 0 {
166 self.clr2_dist = std::u32::MAX;
167 self.clr8_dist = std::u32::MAX;
171 self.clr2_flags = 0u16;
175 for &pix in buf.iter() {
176 if pix == self.clr2[0] {
177 self.clr2_flags |= mask;
182 if (self.clr2_flags & 0x8000) != 0 {
183 self.clr2_flags = !self.clr2_flags;
184 self.clr2.swap(0, 1);
186 } else if quantise_median_cut::<Pixel16, Pixel16Sum>(buf, &mut self.clr2) == 2 {
189 for pix in buf.iter() {
190 let dist0 = pix.dist(self.clr2[0]);
191 let dist1 = pix.dist(self.clr2[1]);
193 self.clr2_flags |= mask;
194 self.clr2_dist += dist0;
196 self.clr2_dist += dist1;
200 if (self.clr2_flags & 0x8000) != 0 {
201 self.clr2_flags = !self.clr2_flags;
202 self.clr2.swap(0, 1);
205 self.clr2_dist = self.fill_dist;
206 self.clr2 = [self.fill_val; 2];
208 if self.clr2_dist == 0 {
209 self.clr8_dist = std::u32::MAX;
213 self.clr8 = [[Pixel16(0); 2]; 4];
218 let off = (i & 1) * 2 + (i & 2) * 4;
219 let src2 = [buf[off], buf[off + 1], buf[off + 4], buf[off + 5]];
220 let nc = quantise_median_cut::<Pixel16, Pixel16Sum>(&src2, &mut self.clr8[i]);
222 self.clr8[i][1] = self.clr8[i][0];
225 let dist0 = src2[j].dist(self.clr8[i][0]);
226 let dist1 = src2[j].dist(self.clr8[i][1]);
228 self.clr8_flags |= mask;
229 self.clr8_dist += dist0;
231 self.clr8_dist += dist1;
236 if (self.clr8_flags & 0x8000) != 0 {
237 self.clr8_flags ^= 0xF000;
238 self.clr8[3].swap(0, 1);
241 fn put_fill(&self, dst: &mut [u16], dstride: usize) {
242 for line in dst.chunks_mut(dstride) {
244 line[i] = self.fill_val.0;
248 fn put_clr2(&self, dst: &mut [u16], dstride: usize) {
251 if (self.clr2_flags & (1 << (i + j * 4))) == 0 {
252 dst[i + j * dstride] = self.clr2[0].0;
254 dst[i + j * dstride] = self.clr2[1].0;
259 fn put_clr8(&self, dst: &mut [u16], dstride: usize) {
261 let off = (i & 1) * 2 + (i & 2) * dstride;
262 let cur_flg = (self.clr8_flags >> (i * 4)) & 0xF;
263 dst[off] = self.clr8[i][( !cur_flg & 1) as usize].0;
264 dst[off + 1] = self.clr8[i][((!cur_flg >> 1) & 1) as usize].0;
265 dst[off + dstride] = self.clr8[i][((!cur_flg >> 2) & 1) as usize].0;
266 dst[off + 1 + dstride] = self.clr8[i][((!cur_flg >> 3) & 1) as usize].0;
269 fn write_fill(&self, bw: &mut ByteWriter) -> EncoderResult<()> {
270 bw.write_u16le(self.fill_val.0 | 0x8000)?;
273 fn write_clr2(&self, bw: &mut ByteWriter) -> EncoderResult<()> {
274 bw.write_u16le(self.clr2_flags)?;
275 bw.write_u16le(self.clr2[0].0)?;
276 bw.write_u16le(self.clr2[1].0)?;
279 fn write_clr8(&self, bw: &mut ByteWriter) -> EncoderResult<()> {
280 bw.write_u16le(self.clr8_flags)?;
281 bw.write_u16le(self.clr8[0][0].0 | 0x8000)?;
282 bw.write_u16le(self.clr8[0][1].0)?;
283 bw.write_u16le(self.clr8[1][0].0)?;
284 bw.write_u16le(self.clr8[1][1].0)?;
285 bw.write_u16le(self.clr8[2][0].0)?;
286 bw.write_u16le(self.clr8[2][1].0)?;
287 bw.write_u16le(self.clr8[3][0].0)?;
288 bw.write_u16le(self.clr8[3][1].0)?;
293 struct MSVideo1Encoder {
294 stream: Option<NAStreamRef>,
295 pkt: Option<NAPacket>,
296 pool: NAVideoBufferPool<u16>,
297 lastfrm: Option<NAVideoBufferRef<u16>>,
303 impl MSVideo1Encoder {
308 pool: NAVideoBufferPool::new(2),
315 fn get_block(src: &[u16], sstride: usize, buf: &mut [Pixel16; 16]) {
316 for (line, dst) in src.chunks(sstride).zip(buf.chunks_mut(4)) {
318 dst[i] = Pixel16(line[i]);
322 fn write_skips(bw: &mut ByteWriter, skips: usize) -> EncoderResult<()> {
323 bw.write_u16le((skips as u16) | 0x8400)?;
326 fn encode_inter(bw: &mut ByteWriter, cur_frm: &mut NAVideoBuffer<u16>, in_frm: &NAVideoBuffer<u16>, prev_frm: &NAVideoBuffer<u16>, _quality: u8) -> EncoderResult<bool> {
327 let mut is_intra = true;
328 let src = in_frm.get_data();
329 let sstride = in_frm.get_stride(0);
330 let soff = in_frm.get_offset(0);
331 let (w, h) = in_frm.get_dimensions(0);
332 let rsrc = prev_frm.get_data();
333 let rstride = prev_frm.get_stride(0);
334 let roff = prev_frm.get_offset(0);
335 let dstride = cur_frm.get_stride(0);
336 let doff = cur_frm.get_offset(0);
337 let dst = cur_frm.get_data_mut().unwrap();
338 let mut skip_run = 0;
339 for ((sstrip, rstrip), dstrip) in (&src[soff..]).chunks(sstride * 4).take(h / 4).zip((&rsrc[roff..]).chunks(rstride * 4)).zip((&mut dst[doff..]).chunks_mut(dstride * 4)) {
340 for x in (0..w).step_by(4) {
341 let mut buf = [Pixel16::min_cw(); 16];
342 let mut refbuf = [Pixel16::min_cw(); 16];
343 Self::get_block(&sstrip[x..], sstride, &mut buf);
344 Self::get_block(&rstrip[x..], rstride, &mut refbuf);
346 let mut skip_dist = 0;
347 for (pix, rpix) in buf.iter().zip(refbuf.iter()) {
348 skip_dist += pix.dist(*rpix);
353 if skip_run == 1023 {
354 Self::write_skips(bw, skip_run)?;
360 let mut bstate = BlockState::default();
361 bstate.calc_stats(&buf);
363 let dst = &mut dstrip[x..];
364 if skip_dist <= bstate.fill_dist {
367 if skip_run == 1023 {
368 Self::write_skips(bw, skip_run)?;
371 } else if bstate.fill_dist <= bstate.clr2_dist {
372 bstate.put_fill(dst, dstride);
374 Self::write_skips(bw, skip_run)?;
377 bstate.write_fill(bw)?;
378 } else if bstate.clr8_dist < bstate.clr2_dist {
379 bstate.put_clr8(dst, dstride);
381 Self::write_skips(bw, skip_run)?;
384 bstate.write_clr8(bw)?;
386 bstate.put_clr2(dst, dstride);
388 Self::write_skips(bw, skip_run)?;
391 bstate.write_clr2(bw)?;
396 Self::write_skips(bw, skip_run)?;
400 } //xxx: something for inter?
403 fn encode_intra(bw: &mut ByteWriter, cur_frm: &mut NAVideoBuffer<u16>, in_frm: &NAVideoBuffer<u16>, _quality: u8) -> EncoderResult<bool> {
404 let src = in_frm.get_data();
405 let sstride = in_frm.get_stride(0);
406 let soff = in_frm.get_offset(0);
407 let (w, h) = in_frm.get_dimensions(0);
408 let dstride = cur_frm.get_stride(0);
409 let doff = cur_frm.get_offset(0);
410 let dst = cur_frm.get_data_mut().unwrap();
411 for (sstrip, dstrip) in (&src[soff..]).chunks(sstride * 4).take(h / 4).zip((&mut dst[doff..]).chunks_mut(dstride * 4)) {
412 for x in (0..w).step_by(4) {
413 let mut buf = [Pixel16::min_cw(); 16];
414 Self::get_block(&sstrip[x..], sstride, &mut buf);
415 let mut bstate = BlockState::default();
416 bstate.calc_stats(&buf);
418 let dst = &mut dstrip[x..];
419 if bstate.fill_dist <= bstate.clr2_dist {
420 bstate.put_fill(dst, dstride);
421 bstate.write_fill(bw)?;
422 } else if bstate.clr8_dist < bstate.clr2_dist {
423 bstate.put_clr8(dst, dstride);
424 bstate.write_clr8(bw)?;
426 bstate.put_clr2(dst, dstride);
427 bstate.write_clr2(bw)?;
436 const RGB555_FORMAT: NAPixelFormaton = NAPixelFormaton {
437 model: ColorModel::RGB(RGBSubmodel::RGB), components: 3,
439 Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: 10, comp_offs: 0, next_elem: 2 }),
440 Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: 5, comp_offs: 0, next_elem: 2 }),
441 Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: 0, comp_offs: 0, next_elem: 2 }),
443 elem_size: 2, be: false, alpha: false, palette: false };
445 impl NAEncoder for MSVideo1Encoder {
446 fn negotiate_format(&self, encinfo: &EncodeParameters) -> EncoderResult<EncodeParameters> {
447 match encinfo.format {
448 NACodecTypeInfo::None => {
449 Ok(EncodeParameters {
450 format: NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, true, RGB555_FORMAT)),
451 ..Default::default() })
453 NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError),
454 NACodecTypeInfo::Video(vinfo) => {
455 let outinfo = NAVideoInfo::new((vinfo.width + 3) & !3, (vinfo.height + 3) & !3, true, RGB555_FORMAT);
456 let mut ofmt = *encinfo;
457 ofmt.format = NACodecTypeInfo::Video(outinfo);
462 fn init(&mut self, stream_id: u32, encinfo: EncodeParameters) -> EncoderResult<NAStreamRef> {
463 match encinfo.format {
464 NACodecTypeInfo::None => Err(EncoderError::FormatError),
465 NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError),
466 NACodecTypeInfo::Video(vinfo) => {
467 if vinfo.format != RGB555_FORMAT {
468 return Err(EncoderError::FormatError);
470 if ((vinfo.width | vinfo.height) & 3) != 0 {
471 return Err(EncoderError::FormatError);
474 let out_info = NAVideoInfo::new(vinfo.width, vinfo.height, true, RGB555_FORMAT);
475 let info = NACodecInfo::new("msvideo1", NACodecTypeInfo::Video(out_info), None);
476 let mut stream = NAStream::new(StreamType::Video, stream_id, info, encinfo.tb_num, encinfo.tb_den, 0);
477 stream.set_num(stream_id as usize);
478 let stream = stream.into_ref();
479 if self.pool.prealloc_video(out_info, 2).is_err() {
480 return Err(EncoderError::AllocError);
483 self.stream = Some(stream.clone());
484 self.quality = encinfo.quality;
490 fn encode(&mut self, frm: &NAFrame) -> EncoderResult<()> {
491 let buf = frm.get_buffer();
492 if let Some(ref vbuf) = buf.get_vbuf16() {
493 let mut cur_frm = self.pool.get_free().unwrap();
494 let mut dbuf = Vec::with_capacity(4);
495 let mut gw = GrowableMemoryWriter::new_write(&mut dbuf);
496 let mut bw = ByteWriter::new(&mut gw);
497 if self.frmcount == 0 {
500 let is_intra = if let Some(ref prev_buf) = self.lastfrm {
501 Self::encode_inter(&mut bw, &mut cur_frm, vbuf, prev_buf, self.quality)?
503 Self::encode_intra(&mut bw, &mut cur_frm, vbuf, self.quality)?
505 self.lastfrm = Some(cur_frm);
506 self.pkt = Some(NAPacket::new(self.stream.clone().unwrap(), frm.ts, is_intra, dbuf));
508 if self.frmcount == self.key_int {
513 Err(EncoderError::InvalidParameters)
516 fn get_packet(&mut self) -> EncoderResult<Option<NAPacket>> {
518 std::mem::swap(&mut self.pkt, &mut npkt);
521 fn flush(&mut self) -> EncoderResult<()> {
527 const ENCODER_OPTS: &[NAOptionDefinition] = &[
529 name: KEYFRAME_OPTION, description: KEYFRAME_OPTION_DESC,
530 opt_type: NAOptionDefinitionType::Int(Some(0), Some(128)) },
533 impl NAOptionHandler for MSVideo1Encoder {
534 fn get_supported_options(&self) -> &[NAOptionDefinition] { ENCODER_OPTS }
535 fn set_options(&mut self, options: &[NAOption]) {
536 for option in options.iter() {
537 for opt_def in ENCODER_OPTS.iter() {
538 if opt_def.check(option).is_ok() {
541 if let NAValue::Int(intval) = option.value {
542 self.key_int = intval as u8;
551 fn query_option_value(&self, name: &str) -> Option<NAValue> {
553 KEYFRAME_OPTION => Some(NAValue::Int(i64::from(self.key_int))),
559 pub fn get_encoder() -> Box<dyn NAEncoder + Send> {
560 Box::new(MSVideo1Encoder::new())
565 use nihav_core::codecs::*;
566 use nihav_core::demuxers::*;
567 use nihav_core::muxers::*;
569 use nihav_commonfmt::*;
570 use nihav_codec_support::test::enc_video::*;
571 use super::RGB555_FORMAT;
574 fn test_ms_video1_encoder() {
575 let mut dmx_reg = RegisteredDemuxers::new();
576 generic_register_all_demuxers(&mut dmx_reg);
577 let mut dec_reg = RegisteredDecoders::new();
578 generic_register_all_decoders(&mut dec_reg);
579 ms_register_all_decoders(&mut dec_reg);
580 let mut mux_reg = RegisteredMuxers::new();
581 generic_register_all_muxers(&mut mux_reg);
582 let mut enc_reg = RegisteredEncoders::new();
583 ms_register_all_encoders(&mut enc_reg);
585 // sample: https://samples.mplayerhq.hu/V-codecs/UCOD/TalkingHead_352x288.avi
586 let dec_config = DecoderTestParams {
588 in_name: "assets/Misc/TalkingHead_352x288.avi",
589 stream_type: StreamType::Video,
593 let enc_config = EncoderTestParams {
595 enc_name: "msvideo1",
596 out_name: "msvideo1.avi",
599 let dst_vinfo = NAVideoInfo {
602 format: RGB555_FORMAT,
606 let enc_params = EncodeParameters {
607 format: NACodecTypeInfo::Video(dst_vinfo),
614 //test_encoding_to_file(&dec_config, &enc_config, enc_params, &[]);
615 test_encoding_md5(&dec_config, &enc_config, enc_params, &[],
616 &[0x6921e67e, 0x4f2ada95, 0x009ffc62, 0xd4bfab6a]);