| 1 | use nihav_core::codecs::*; |
| 2 | use nihav_core::compr::deflate::{Deflate, DeflateMode, DeflateWriter}; |
| 3 | use nihav_core::io::byteio::*; |
| 4 | |
| 5 | const RGB555_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3, |
| 6 | comp_info: [ |
| 7 | Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: 10, comp_offs: 0, next_elem: 2 }), |
| 8 | Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: 5, comp_offs: 0, next_elem: 2 }), |
| 9 | Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 5, shift: 0, comp_offs: 0, next_elem: 2 }), |
| 10 | None, None], |
| 11 | elem_size: 2, be: false, alpha: false, palette: false }; |
| 12 | const RGB24_0_FORMAT: NAPixelFormaton = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3, |
| 13 | comp_info: [ |
| 14 | Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 2, next_elem: 4 }), |
| 15 | Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 1, next_elem: 4 }), |
| 16 | Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 0, next_elem: 4 }), |
| 17 | None, None], |
| 18 | elem_size: 4, be: false, alpha: false, palette: false }; |
| 19 | |
| 20 | struct ZMBVEncoder { |
| 21 | stream: Option<NAStreamRef>, |
| 22 | frm1: Vec<u8>, |
| 23 | frm2: Vec<u8>, |
| 24 | tmp_buf: Vec<u8>, |
| 25 | zbuf: Vec<u8>, |
| 26 | pal: [u8; 768], |
| 27 | pkt: Option<NAPacket>, |
| 28 | frmcount: u8, |
| 29 | key_int: u8, |
| 30 | tile_w: usize, |
| 31 | tile_h: usize, |
| 32 | cmode: DeflateMode, |
| 33 | compr: Deflate, |
| 34 | bpp: u8, |
| 35 | width: usize, |
| 36 | height: usize, |
| 37 | range: usize, |
| 38 | sent_pal: bool, |
| 39 | } |
| 40 | |
| 41 | fn buf_type_to_bpp(buf: &NABufferType) -> u8 { |
| 42 | match buf { |
| 43 | NABufferType::Video(ref vbuf) => { |
| 44 | let vinfo = vbuf.get_info(); |
| 45 | if vinfo.get_format().is_paletted() { |
| 46 | 8 |
| 47 | } else { |
| 48 | vinfo.get_format().get_total_depth() |
| 49 | } |
| 50 | }, |
| 51 | NABufferType::VideoPacked(ref vbuf) => { |
| 52 | let vinfo = vbuf.get_info(); |
| 53 | vinfo.get_format().elem_size * 8 |
| 54 | }, |
| 55 | NABufferType::Video16(ref vbuf) => { |
| 56 | let vinfo = vbuf.get_info(); |
| 57 | vinfo.get_format().get_total_depth() |
| 58 | }, |
| 59 | _ => 0, |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | fn copy_frame(buf: NABufferType, dst: &mut [u8], bpp: u8) -> EncoderResult<()> { |
| 64 | match buf { |
| 65 | NABufferType::Video(ref vbuf) => { |
| 66 | if bpp != 8 { |
| 67 | return Err(EncoderError::FormatError); |
| 68 | } |
| 69 | let off = vbuf.get_offset(0); |
| 70 | let stride = vbuf.get_stride(0); |
| 71 | let data = vbuf.get_data(); |
| 72 | let w = vbuf.get_info().get_width(); |
| 73 | let h = vbuf.get_info().get_height(); |
| 74 | |
| 75 | for (dline, sline) in dst.chunks_mut(w).zip(data[off..].chunks(stride)).take(h) { |
| 76 | dline[..w].copy_from_slice(&sline[..w]); |
| 77 | } |
| 78 | }, |
| 79 | NABufferType::Video16(ref vbuf) => { |
| 80 | let off = vbuf.get_offset(0); |
| 81 | let stride = vbuf.get_stride(0); |
| 82 | let data = vbuf.get_data(); |
| 83 | let w = vbuf.get_info().get_width(); |
| 84 | let h = vbuf.get_info().get_height(); |
| 85 | |
| 86 | for (dline, sline) in dst.chunks_mut(w * 2).zip(data[off..].chunks(stride)).take(h) { |
| 87 | for (dst, &src) in dline[..w * 2].chunks_exact_mut(2).zip(sline.iter()) { |
| 88 | dst[0] = src as u8; |
| 89 | dst[1] = (src >> 8) as u8; |
| 90 | } |
| 91 | } |
| 92 | }, |
| 93 | NABufferType::VideoPacked(ref vbuf) => { |
| 94 | let off = vbuf.get_offset(0); |
| 95 | let stride = vbuf.get_stride(0); |
| 96 | let data = vbuf.get_data(); |
| 97 | let w = vbuf.get_info().get_width(); |
| 98 | let h = vbuf.get_info().get_height(); |
| 99 | let w = w * (((bpp as usize) + 7) / 8); |
| 100 | |
| 101 | for (dline, sline) in dst.chunks_mut(w).zip(data[off..].chunks(stride)).take(h) { |
| 102 | dline[..w].copy_from_slice(&sline[..w]); |
| 103 | } |
| 104 | }, |
| 105 | _ => return Err(EncoderError::FormatError), |
| 106 | }; |
| 107 | Ok(()) |
| 108 | } |
| 109 | |
| 110 | fn to_signed(val: usize) -> isize { |
| 111 | if (val & 1) == 0 { |
| 112 | (val >> 1) as isize |
| 113 | } else { |
| 114 | -((val >> 1) as isize) - 1 |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | impl ZMBVEncoder { |
| 119 | fn new() -> Self { |
| 120 | Self { |
| 121 | stream: None, |
| 122 | pkt: None, |
| 123 | frm1: Vec::new(), |
| 124 | frm2: Vec::new(), |
| 125 | pal: [0; 768], |
| 126 | tmp_buf: Vec::new(), |
| 127 | zbuf: Vec::new(), |
| 128 | frmcount: 0, |
| 129 | key_int: 25, |
| 130 | tile_w: 16, |
| 131 | tile_h: 16, |
| 132 | cmode: DeflateMode::default(), |
| 133 | compr: Deflate::new(DeflateMode::default()), |
| 134 | bpp: 0, |
| 135 | width: 0, |
| 136 | height: 0, |
| 137 | range: 128, |
| 138 | sent_pal: false, |
| 139 | } |
| 140 | } |
| 141 | fn encode_intra(&mut self, bw: &mut ByteWriter, buf: NABufferType) -> EncoderResult<()> { |
| 142 | let bpp = buf_type_to_bpp(&buf); |
| 143 | |
| 144 | if let NABufferType::None = buf { |
| 145 | if self.bpp == 0 { |
| 146 | return Err(EncoderError::FormatError); |
| 147 | } |
| 148 | self.frm1.copy_from_slice(&self.frm2); |
| 149 | } else { |
| 150 | if bpp == 0 { |
| 151 | return Err(EncoderError::FormatError); |
| 152 | } |
| 153 | self.bpp = bpp; |
| 154 | |
| 155 | if let (NABufferType::Video(ref vbuf), true) = (&buf, bpp == 8) { |
| 156 | let off = vbuf.get_offset(1); |
| 157 | let data = vbuf.get_data(); |
| 158 | self.pal.copy_from_slice(&data[off..][..768]); |
| 159 | } |
| 160 | |
| 161 | copy_frame(buf, &mut self.frm1, self.bpp)?; |
| 162 | } |
| 163 | |
| 164 | bw.write_byte(1)?; // intra flag |
| 165 | bw.write_byte(0)?; // high version |
| 166 | bw.write_byte(1)?; // low version |
| 167 | bw.write_byte(if self.cmode == DeflateMode::NoCompr { 0 } else { 1 })?; |
| 168 | let fmt = match self.bpp { |
| 169 | 8 => 4, |
| 170 | 15 => 5, |
| 171 | 16 => 6, |
| 172 | 24 => 7, |
| 173 | 32 => 8, |
| 174 | _ => unreachable!(), |
| 175 | }; |
| 176 | bw.write_byte(fmt)?; |
| 177 | bw.write_byte(self.tile_w as u8)?; |
| 178 | bw.write_byte(self.tile_h as u8)?; |
| 179 | |
| 180 | let bm = ((bpp as usize) + 7) / 8; |
| 181 | if self.cmode == DeflateMode::NoCompr { |
| 182 | if bpp == 8 { |
| 183 | bw.write_buf(&self.pal)?; |
| 184 | } |
| 185 | bw.write_buf(&self.frm1[..self.width * self.height * bm])?; |
| 186 | } else { |
| 187 | self.tmp_buf.truncate(0); |
| 188 | if bpp == 8 { |
| 189 | self.tmp_buf.extend_from_slice(&self.pal); |
| 190 | } |
| 191 | self.tmp_buf.extend_from_slice(&self.frm1[..self.width * self.height * bm]); |
| 192 | self.compr = Deflate::new(self.cmode); |
| 193 | |
| 194 | let mut db = Vec::new(); |
| 195 | std::mem::swap(&mut db, &mut self.zbuf); |
| 196 | db.truncate(0); |
| 197 | let mut wr = DeflateWriter::new(db); |
| 198 | self.compr.write_zlib_header(&mut wr); |
| 199 | self.compr.compress(&self.tmp_buf, &mut wr); |
| 200 | self.compr.compress_flush(&mut wr); |
| 201 | let mut db = wr.end(); |
| 202 | std::mem::swap(&mut db, &mut self.zbuf); |
| 203 | |
| 204 | bw.write_buf(&self.zbuf)?; |
| 205 | } |
| 206 | |
| 207 | Ok(()) |
| 208 | } |
| 209 | fn encode_inter(&mut self, bw: &mut ByteWriter, buf: NABufferType) -> EncoderResult<()> { |
| 210 | if let NABufferType::None = buf { |
| 211 | self.frm1.copy_from_slice(&self.frm2); |
| 212 | |
| 213 | bw.write_byte(0)?; |
| 214 | self.tmp_buf.truncate(0); |
| 215 | let tile_w = (self.width + self.tile_w - 1) / self.tile_w; |
| 216 | let tile_h = (self.height + self.tile_h - 1) / self.tile_h; |
| 217 | let mv_size = (tile_w * tile_h * 2 + 3) & !3; |
| 218 | for _ in 0..mv_size { |
| 219 | self.tmp_buf.push(0); |
| 220 | } |
| 221 | if self.cmode == DeflateMode::NoCompr { |
| 222 | bw.write_buf(&self.tmp_buf)?; |
| 223 | } else { |
| 224 | let mut db = Vec::new(); |
| 225 | |
| 226 | std::mem::swap(&mut db, &mut self.zbuf); |
| 227 | db.truncate(0); |
| 228 | let mut wr = DeflateWriter::new(db); |
| 229 | self.compr.compress(&self.tmp_buf, &mut wr); |
| 230 | self.compr.compress_flush(&mut wr); |
| 231 | let mut db = wr.end(); |
| 232 | std::mem::swap(&mut db, &mut self.zbuf); |
| 233 | |
| 234 | bw.write_buf(&self.zbuf)?; |
| 235 | } |
| 236 | return Ok(()); |
| 237 | } |
| 238 | let bpp = buf_type_to_bpp(&buf); |
| 239 | if bpp == 0 || bpp != self.bpp { |
| 240 | return Err(EncoderError::FormatError); |
| 241 | } |
| 242 | |
| 243 | self.tmp_buf.truncate(0); |
| 244 | if let (NABufferType::Video(ref vbuf), true) = (&buf, bpp == 8) { |
| 245 | let mut npal = [0; 768]; |
| 246 | let off = vbuf.get_offset(1); |
| 247 | let data = vbuf.get_data(); |
| 248 | npal.copy_from_slice(&data[off..][..768]); |
| 249 | let mut cmp = true; |
| 250 | for (&a, &b) in self.pal.iter().zip(npal.iter()) { |
| 251 | if a != b { |
| 252 | cmp = false; |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | if !cmp { |
| 257 | for (&a, &b) in self.pal.iter().zip(npal.iter()) { |
| 258 | self.tmp_buf.push(a ^ b); |
| 259 | } |
| 260 | self.pal = npal; |
| 261 | |
| 262 | bw.write_byte(2)?; |
| 263 | |
| 264 | self.sent_pal = false; |
| 265 | } else { |
| 266 | bw.write_byte(0)?; |
| 267 | } |
| 268 | } else { |
| 269 | bw.write_byte(0)?; |
| 270 | } |
| 271 | copy_frame(buf, &mut self.frm1, self.bpp)?; |
| 272 | |
| 273 | let tile_w = (self.width + self.tile_w - 1) / self.tile_w; |
| 274 | let tile_h = (self.height + self.tile_h - 1) / self.tile_h; |
| 275 | let mut mv_start = self.tmp_buf.len(); |
| 276 | let mv_size = (tile_w * tile_h * 2 + 3) & !3; |
| 277 | for _ in 0..mv_size { |
| 278 | self.tmp_buf.push(0); |
| 279 | } |
| 280 | |
| 281 | let bpp = ((self.bpp as usize) + 7) / 8; |
| 282 | let stride = self.width * bpp; |
| 283 | let mut off = 0; |
| 284 | for y in (0..self.height).step_by(self.tile_h) { |
| 285 | let cur_h = (self.height - y).min(self.tile_h); |
| 286 | for x in (0..self.width).step_by(self.tile_w) { |
| 287 | let cur_w = (self.width - x).min(self.tile_w); |
| 288 | |
| 289 | let mut best_dist = std::u32::MAX; |
| 290 | let mut best_x = x; |
| 291 | let mut best_y = y; |
| 292 | |
| 293 | 'search: for yoff in 0..self.range { |
| 294 | let ypos = (y as isize) + to_signed(yoff); |
| 295 | if ypos < 0 { |
| 296 | continue; |
| 297 | } |
| 298 | let ypos = ypos as usize; |
| 299 | if ypos + cur_h > self.height { |
| 300 | break; |
| 301 | } |
| 302 | for xoff in 0..self.range { |
| 303 | let xpos = (x as isize) + to_signed(xoff); |
| 304 | if xpos < 0 { |
| 305 | continue; |
| 306 | } |
| 307 | let xpos = xpos as usize; |
| 308 | if xpos + cur_w > self.width { |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | let mut diff = 0; |
| 313 | let roff = xpos * bpp + ypos * stride; |
| 314 | for (line0, line1) in self.frm1[off..].chunks(stride).take(cur_h).zip(self.frm2[roff..].chunks(stride)) { |
| 315 | for (&a, &b) in line0[..cur_w * bpp].iter().zip(line1[..cur_w * bpp].iter()) { |
| 316 | diff += u32::from(a ^ b); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | if best_dist > diff { |
| 321 | best_dist = diff; |
| 322 | best_x = xpos; |
| 323 | best_y = ypos; |
| 324 | if diff == 0 { |
| 325 | break 'search; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | let has_delta = best_dist != 0; |
| 332 | self.tmp_buf[mv_start] = (best_x.wrapping_sub(x) << 1) as u8; |
| 333 | if has_delta { |
| 334 | self.tmp_buf[mv_start] |= 1; |
| 335 | } |
| 336 | self.tmp_buf[mv_start + 1] = (best_y.wrapping_sub(y) << 1) as u8; |
| 337 | mv_start += 2; |
| 338 | if has_delta { |
| 339 | let rpos = best_x * bpp + best_y * stride; |
| 340 | for (line0, line1) in self.frm1[off..].chunks(stride).take(cur_h).zip(self.frm2[rpos..].chunks(stride)) { |
| 341 | for (&a, &b) in line0[..cur_w * bpp].iter().zip(line1[..cur_w * bpp].iter()) { |
| 342 | self.tmp_buf.push(a ^ b); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | off += self.tile_w * bpp; |
| 348 | } |
| 349 | off -= tile_w * self.tile_w * bpp; |
| 350 | off += stride * self.tile_h; |
| 351 | } |
| 352 | |
| 353 | if self.cmode == DeflateMode::NoCompr { |
| 354 | bw.write_buf(&self.tmp_buf)?; |
| 355 | } else { |
| 356 | let mut db = Vec::new(); |
| 357 | |
| 358 | std::mem::swap(&mut db, &mut self.zbuf); |
| 359 | db.truncate(0); |
| 360 | let mut wr = DeflateWriter::new(db); |
| 361 | self.compr.compress(&self.tmp_buf, &mut wr); |
| 362 | self.compr.compress_flush(&mut wr); |
| 363 | let mut db = wr.end(); |
| 364 | std::mem::swap(&mut db, &mut self.zbuf); |
| 365 | |
| 366 | bw.write_buf(&self.zbuf)?; |
| 367 | } |
| 368 | |
| 369 | Ok(()) |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | impl NAEncoder for ZMBVEncoder { |
| 374 | fn negotiate_format(&self, encinfo: &EncodeParameters) -> EncoderResult<EncodeParameters> { |
| 375 | match encinfo.format { |
| 376 | NACodecTypeInfo::None => { |
| 377 | let mut ofmt = EncodeParameters::default(); |
| 378 | ofmt.format = NACodecTypeInfo::Video(NAVideoInfo::new(0, 0, true, YUV420_FORMAT)); |
| 379 | Ok(ofmt) |
| 380 | }, |
| 381 | NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError), |
| 382 | NACodecTypeInfo::Video(vinfo) => { |
| 383 | let depth = vinfo.format.get_total_depth(); |
| 384 | let pix_fmt = if vinfo.format.is_paletted() { |
| 385 | PAL8_FORMAT |
| 386 | } else if !vinfo.format.model.is_rgb() || depth > 16 { |
| 387 | RGB24_0_FORMAT |
| 388 | } else if depth < 16 { |
| 389 | RGB555_FORMAT |
| 390 | } else { |
| 391 | RGB565_FORMAT |
| 392 | }; |
| 393 | let outinfo = NAVideoInfo::new(vinfo.width, vinfo.height, false, pix_fmt); |
| 394 | let mut ofmt = *encinfo; |
| 395 | ofmt.format = NACodecTypeInfo::Video(outinfo); |
| 396 | Ok(ofmt) |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | fn init(&mut self, stream_id: u32, encinfo: EncodeParameters) -> EncoderResult<NAStreamRef> { |
| 401 | match encinfo.format { |
| 402 | NACodecTypeInfo::None => Err(EncoderError::FormatError), |
| 403 | NACodecTypeInfo::Audio(_) => Err(EncoderError::FormatError), |
| 404 | NACodecTypeInfo::Video(vinfo) => { |
| 405 | self.width = vinfo.width; |
| 406 | self.height = vinfo.height; |
| 407 | |
| 408 | let out_info = NAVideoInfo::new(vinfo.width, vinfo.height, false, vinfo.format); |
| 409 | let info = NACodecInfo::new("zmbv", NACodecTypeInfo::Video(out_info), None); |
| 410 | let mut stream = NAStream::new(StreamType::Video, stream_id, info, encinfo.tb_num, encinfo.tb_den, 0); |
| 411 | stream.set_num(stream_id as usize); |
| 412 | let stream = stream.into_ref(); |
| 413 | |
| 414 | self.stream = Some(stream.clone()); |
| 415 | |
| 416 | self.frm1 = vec![0; vinfo.width * vinfo.height * 4]; |
| 417 | self.frm2 = vec![0; vinfo.width * vinfo.height * 4]; |
| 418 | |
| 419 | Ok(stream) |
| 420 | }, |
| 421 | } |
| 422 | } |
| 423 | fn encode(&mut self, frm: &NAFrame) -> EncoderResult<()> { |
| 424 | let buf = frm.get_buffer(); |
| 425 | let mut dbuf = Vec::with_capacity(4); |
| 426 | let mut gw = GrowableMemoryWriter::new_write(&mut dbuf); |
| 427 | let mut bw = ByteWriter::new(&mut gw); |
| 428 | let is_intra = if self.frmcount == 0 { |
| 429 | self.encode_intra(&mut bw, buf)?; |
| 430 | true |
| 431 | } else { |
| 432 | self.encode_inter(&mut bw, buf)?; |
| 433 | false |
| 434 | }; |
| 435 | self.pkt = Some(NAPacket::new(self.stream.clone().unwrap(), frm.ts, is_intra, dbuf)); |
| 436 | if self.bpp == 8 && !self.sent_pal { |
| 437 | if let NABufferType::Video(ref buf) = frm.get_buffer() { |
| 438 | let paloff = buf.get_offset(1); |
| 439 | let data = buf.get_data(); |
| 440 | let mut pal = [0; 1024]; |
| 441 | let srcpal = &data[paloff..][..768]; |
| 442 | for (dclr, sclr) in pal.chunks_exact_mut(4).zip(srcpal.chunks_exact(3)) { |
| 443 | dclr[..3].copy_from_slice(sclr); |
| 444 | } |
| 445 | if let Some(ref mut pkt) = &mut self.pkt { |
| 446 | pkt.side_data.push(NASideData::Palette(true, Arc::new(pal))); |
| 447 | } |
| 448 | } |
| 449 | self.sent_pal = true; |
| 450 | } |
| 451 | self.frmcount += 1; |
| 452 | if self.frmcount == self.key_int { |
| 453 | self.frmcount = 0; |
| 454 | } |
| 455 | std::mem::swap(&mut self.frm1, &mut self.frm2); |
| 456 | Ok(()) |
| 457 | } |
| 458 | fn get_packet(&mut self) -> EncoderResult<Option<NAPacket>> { |
| 459 | let mut npkt = None; |
| 460 | std::mem::swap(&mut self.pkt, &mut npkt); |
| 461 | Ok(npkt) |
| 462 | } |
| 463 | fn flush(&mut self) -> EncoderResult<()> { |
| 464 | self.frmcount = 0; |
| 465 | Ok(()) |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | const ENCODER_OPTS: &[NAOptionDefinition] = &[ |
| 470 | NAOptionDefinition { |
| 471 | name: KEYFRAME_OPTION, description: KEYFRAME_OPTION_DESC, |
| 472 | opt_type: NAOptionDefinitionType::Int(Some(0), Some(128)) }, |
| 473 | NAOptionDefinition { |
| 474 | name: "range", description: "Block search range (0-128)", |
| 475 | opt_type: NAOptionDefinitionType::Int(Some(0), Some(128)) }, |
| 476 | NAOptionDefinition { |
| 477 | name: "tile_width", description: "Block width (1-255)", |
| 478 | opt_type: NAOptionDefinitionType::Int(Some(1), Some(255)) }, |
| 479 | NAOptionDefinition { |
| 480 | name: "tile_height", description: "Block width (1-255)", |
| 481 | opt_type: NAOptionDefinitionType::Int(Some(1), Some(255)) }, |
| 482 | NAOptionDefinition { |
| 483 | name: "compr_level", description: "Compression level", |
| 484 | opt_type: DEFLATE_OPTION_VALUES }, |
| 485 | ]; |
| 486 | |
| 487 | impl NAOptionHandler for ZMBVEncoder { |
| 488 | fn get_supported_options(&self) -> &[NAOptionDefinition] { ENCODER_OPTS } |
| 489 | fn set_options(&mut self, options: &[NAOption]) { |
| 490 | for option in options.iter() { |
| 491 | for opt_def in ENCODER_OPTS.iter() { |
| 492 | if opt_def.check(option).is_ok() { |
| 493 | match option.name { |
| 494 | "compr_level" => { |
| 495 | if let NAValue::String(ref s) = option.value { |
| 496 | if let Ok(val) = s.parse::<DeflateMode>() { |
| 497 | self.cmode = val; |
| 498 | } |
| 499 | } |
| 500 | }, |
| 501 | KEYFRAME_OPTION => { |
| 502 | if let NAValue::Int(intval) = option.value { |
| 503 | self.key_int = intval as u8; |
| 504 | } |
| 505 | }, |
| 506 | "range" => { |
| 507 | if let NAValue::Int(intval) = option.value { |
| 508 | self.range = intval as usize; |
| 509 | } |
| 510 | }, |
| 511 | "tile_width" => { |
| 512 | if let NAValue::Int(intval) = option.value { |
| 513 | self.tile_w = intval as usize; |
| 514 | } |
| 515 | }, |
| 516 | "tile_height" => { |
| 517 | if let NAValue::Int(intval) = option.value { |
| 518 | self.tile_h = intval as usize; |
| 519 | } |
| 520 | }, |
| 521 | _ => {}, |
| 522 | }; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | fn query_option_value(&self, name: &str) -> Option<NAValue> { |
| 528 | match name { |
| 529 | "compr_level" => Some(NAValue::String(self.cmode.to_string())), |
| 530 | KEYFRAME_OPTION => Some(NAValue::Int(i64::from(self.key_int))), |
| 531 | "range" => Some(NAValue::Int(self.range as i64)), |
| 532 | "tile_width" => Some(NAValue::Int(self.tile_w as i64)), |
| 533 | "tile_height" => Some(NAValue::Int(self.tile_h as i64)), |
| 534 | _ => None, |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | pub fn get_encoder() -> Box<dyn NAEncoder + Send> { |
| 540 | Box::new(ZMBVEncoder::new()) |
| 541 | } |
| 542 | |
| 543 | #[cfg(test)] |
| 544 | mod test { |
| 545 | use nihav_core::codecs::*; |
| 546 | use nihav_core::demuxers::*; |
| 547 | use nihav_core::muxers::*; |
| 548 | use crate::*; |
| 549 | use nihav_codec_support::test::enc_video::*; |
| 550 | use super::{RGB555_FORMAT, RGB24_0_FORMAT}; |
| 551 | |
| 552 | #[test] |
| 553 | fn test_zmbv_encoder_8bit() { |
| 554 | let mut dmx_reg = RegisteredDemuxers::new(); |
| 555 | generic_register_all_demuxers(&mut dmx_reg); |
| 556 | let mut dec_reg = RegisteredDecoders::new(); |
| 557 | generic_register_all_decoders(&mut dec_reg); |
| 558 | let mut mux_reg = RegisteredMuxers::new(); |
| 559 | generic_register_all_muxers(&mut mux_reg); |
| 560 | let mut enc_reg = RegisteredEncoders::new(); |
| 561 | generic_register_all_encoders(&mut enc_reg); |
| 562 | |
| 563 | let dec_config = DecoderTestParams { |
| 564 | demuxer: "avi", |
| 565 | in_name: "assets/Misc/td3_000.avi", |
| 566 | stream_type: StreamType::Video, |
| 567 | limit: Some(20), |
| 568 | dmx_reg, dec_reg, |
| 569 | }; |
| 570 | let enc_config = EncoderTestParams { |
| 571 | muxer: "avi", |
| 572 | enc_name: "zmbv", |
| 573 | out_name: "zmbv8.avi", |
| 574 | mux_reg, enc_reg, |
| 575 | }; |
| 576 | let dst_vinfo = NAVideoInfo { |
| 577 | width: 0, |
| 578 | height: 0, |
| 579 | format: PAL8_FORMAT, |
| 580 | flipped: false, |
| 581 | bits: 8, |
| 582 | }; |
| 583 | let enc_params = EncodeParameters { |
| 584 | format: NACodecTypeInfo::Video(dst_vinfo), |
| 585 | quality: 0, |
| 586 | bitrate: 0, |
| 587 | tb_num: 0, |
| 588 | tb_den: 0, |
| 589 | flags: 0, |
| 590 | }; |
| 591 | let enc_options = &[ |
| 592 | NAOption { name: "range", value: NAValue::Int(16) }, |
| 593 | ]; |
| 594 | //test_encoding_to_file(&dec_config, &enc_config, enc_params, enc_options); |
| 595 | test_encoding_md5(&dec_config, &enc_config, enc_params, enc_options, |
| 596 | &[0x4bcdb816, 0x57d5d1b6, 0xc9412438, 0x9416c407]); |
| 597 | } |
| 598 | |
| 599 | #[test] |
| 600 | fn test_zmbv_encoder_15bit() { |
| 601 | let mut dmx_reg = RegisteredDemuxers::new(); |
| 602 | generic_register_all_demuxers(&mut dmx_reg); |
| 603 | let mut dec_reg = RegisteredDecoders::new(); |
| 604 | generic_register_all_decoders(&mut dec_reg); |
| 605 | let mut mux_reg = RegisteredMuxers::new(); |
| 606 | generic_register_all_muxers(&mut mux_reg); |
| 607 | let mut enc_reg = RegisteredEncoders::new(); |
| 608 | generic_register_all_encoders(&mut enc_reg); |
| 609 | |
| 610 | let dec_config = DecoderTestParams { |
| 611 | demuxer: "avi", |
| 612 | in_name: "assets/Misc/zmbv_15bit.avi", |
| 613 | stream_type: StreamType::Video, |
| 614 | limit: Some(4), |
| 615 | dmx_reg, dec_reg, |
| 616 | }; |
| 617 | let enc_config = EncoderTestParams { |
| 618 | muxer: "avi", |
| 619 | enc_name: "zmbv", |
| 620 | out_name: "zmbv15.avi", |
| 621 | mux_reg, enc_reg, |
| 622 | }; |
| 623 | let dst_vinfo = NAVideoInfo { |
| 624 | width: 0, |
| 625 | height: 0, |
| 626 | format: RGB555_FORMAT, |
| 627 | flipped: false, |
| 628 | bits: 8, |
| 629 | }; |
| 630 | let enc_params = EncodeParameters { |
| 631 | format: NACodecTypeInfo::Video(dst_vinfo), |
| 632 | quality: 0, |
| 633 | bitrate: 0, |
| 634 | tb_num: 0, |
| 635 | tb_den: 0, |
| 636 | flags: 0, |
| 637 | }; |
| 638 | let enc_options = &[ |
| 639 | NAOption { name: "range", value: NAValue::Int(16) }, |
| 640 | ]; |
| 641 | //test_encoding_to_file(&dec_config, &enc_config, enc_params, enc_options); |
| 642 | test_encoding_md5(&dec_config, &enc_config, enc_params, enc_options, |
| 643 | &[0x1cff4116, 0x5926d91b, 0x60aac53f, 0x8a22bc7b]); |
| 644 | } |
| 645 | |
| 646 | #[test] |
| 647 | fn test_zmbv_encoder_16bit() { |
| 648 | let mut dmx_reg = RegisteredDemuxers::new(); |
| 649 | generic_register_all_demuxers(&mut dmx_reg); |
| 650 | let mut dec_reg = RegisteredDecoders::new(); |
| 651 | generic_register_all_decoders(&mut dec_reg); |
| 652 | let mut mux_reg = RegisteredMuxers::new(); |
| 653 | generic_register_all_muxers(&mut mux_reg); |
| 654 | let mut enc_reg = RegisteredEncoders::new(); |
| 655 | generic_register_all_encoders(&mut enc_reg); |
| 656 | |
| 657 | let dec_config = DecoderTestParams { |
| 658 | demuxer: "avi", |
| 659 | in_name: "assets/Misc/zmbv_16bit.avi", |
| 660 | stream_type: StreamType::Video, |
| 661 | limit: Some(4), |
| 662 | dmx_reg, dec_reg, |
| 663 | }; |
| 664 | let enc_config = EncoderTestParams { |
| 665 | muxer: "avi", |
| 666 | enc_name: "zmbv", |
| 667 | out_name: "zmbv16.avi", |
| 668 | mux_reg, enc_reg, |
| 669 | }; |
| 670 | let dst_vinfo = NAVideoInfo { |
| 671 | width: 0, |
| 672 | height: 0, |
| 673 | format: RGB565_FORMAT, |
| 674 | flipped: false, |
| 675 | bits: 8, |
| 676 | }; |
| 677 | let enc_params = EncodeParameters { |
| 678 | format: NACodecTypeInfo::Video(dst_vinfo), |
| 679 | quality: 0, |
| 680 | bitrate: 0, |
| 681 | tb_num: 0, |
| 682 | tb_den: 0, |
| 683 | flags: 0, |
| 684 | }; |
| 685 | let enc_options = &[ |
| 686 | NAOption { name: "range", value: NAValue::Int(16) }, |
| 687 | ]; |
| 688 | //test_encoding_to_file(&dec_config, &enc_config, enc_params, enc_options); |
| 689 | test_encoding_md5(&dec_config, &enc_config, enc_params, enc_options, |
| 690 | &[0xb48aa633, 0x673539fe, 0xa312d45a, 0x76eee134]); |
| 691 | } |
| 692 | |
| 693 | #[test] |
| 694 | fn test_zmbv_encoder_32bit() { |
| 695 | let mut dmx_reg = RegisteredDemuxers::new(); |
| 696 | generic_register_all_demuxers(&mut dmx_reg); |
| 697 | let mut dec_reg = RegisteredDecoders::new(); |
| 698 | generic_register_all_decoders(&mut dec_reg); |
| 699 | let mut mux_reg = RegisteredMuxers::new(); |
| 700 | generic_register_all_muxers(&mut mux_reg); |
| 701 | let mut enc_reg = RegisteredEncoders::new(); |
| 702 | generic_register_all_encoders(&mut enc_reg); |
| 703 | |
| 704 | let dec_config = DecoderTestParams { |
| 705 | demuxer: "avi", |
| 706 | in_name: "assets/Misc/zmbv_32bit.avi", |
| 707 | stream_type: StreamType::Video, |
| 708 | limit: Some(4), |
| 709 | dmx_reg, dec_reg, |
| 710 | }; |
| 711 | let enc_config = EncoderTestParams { |
| 712 | muxer: "avi", |
| 713 | enc_name: "zmbv", |
| 714 | out_name: "zmbv32.avi", |
| 715 | mux_reg, enc_reg, |
| 716 | }; |
| 717 | let dst_vinfo = NAVideoInfo { |
| 718 | width: 0, |
| 719 | height: 0, |
| 720 | format: RGB24_0_FORMAT, |
| 721 | flipped: false, |
| 722 | bits: 8, |
| 723 | }; |
| 724 | let enc_params = EncodeParameters { |
| 725 | format: NACodecTypeInfo::Video(dst_vinfo), |
| 726 | quality: 0, |
| 727 | bitrate: 0, |
| 728 | tb_num: 0, |
| 729 | tb_den: 0, |
| 730 | flags: 0, |
| 731 | }; |
| 732 | let enc_options = &[ |
| 733 | NAOption { name: "range", value: NAValue::Int(16) }, |
| 734 | ]; |
| 735 | //test_encoding_to_file(&dec_config, &enc_config, enc_params, enc_options); |
| 736 | test_encoding_md5(&dec_config, &enc_config, enc_params, enc_options, |
| 737 | &[0x0836152c, 0xfcd7e1fc, 0xf1e2f619, 0x874d3dbc]); |
| 738 | } |
| 739 | } |