2 extern crate nihav_core;
3 extern crate nihav_registry;
4 extern crate nihav_allstuff;
7 use sdl::audio::{DesiredAudioSpec, Channels, AudioFormat, AudioCallback};
8 use sdl::event::{Event, Key};
13 use std::time::{Duration, SystemTime};
16 use std::sync::{Arc, Mutex};
17 use std::sync::atomic::{AtomicBool, Ordering};
18 use std::str::FromStr;
20 use nihav_registry::detect;
21 use nihav_core::formats::*;
22 use nihav_core::frame::*;
23 use nihav_core::io::byteio::{FileReader, ByteReader};
24 use nihav_core::reorder::*;
25 use nihav_core::codecs::*;
26 use nihav_core::demuxers::*;
27 use nihav_core::scale::*;
28 use nihav_core::soundcvt::*;
29 use nihav_allstuff::*;
31 const AUDIO_BUF_SIZE: usize = 1024;
43 fn new(len: usize, max_len: usize) -> Self {
44 Self { data: vec![0; len], max_len, pos: 0, end: 0, done: false, full: false }
46 fn add(&mut self, src: &[u8]) -> bool {
47 if self.done || self.full { return true; }
49 for i in 0..(self.end - self.pos) {
50 self.data[i] = self.data[i + self.pos];
55 if self.end + src.len() > self.max_len {
59 if self.end + src.len() > self.data.len() { self.data.resize(self.end + src.len(), 0); }
60 (&mut self.data[self.end..][..src.len()]).copy_from_slice(src);
61 self.end += src.len();
64 fn consume(&mut self, dst: &mut [u8]) -> bool {
65 if self.done { for el in dst.iter_mut() { *el = 0; } return true; }
67 if self.end - self.pos < size { return false; }
68 dst.copy_from_slice(&self.data[self.pos..][..size]);
70 if self.pos >= self.max_len / 2 { self.full = false; }
73 fn finish(&mut self) {
78 struct AudioConsumer {
79 afifo: Arc<Mutex<AudioFIFO>>,
83 fn new(afifo: Arc<Mutex<AudioFIFO>>) -> Self { Self { afifo } }
86 impl AudioCallback for AudioConsumer {
87 fn callback(&mut self, out: &mut [u8]) {
90 let ret = self.afifo.lock();
91 if let Ok(mut afifo) = ret {
92 done = afifo.consume(out);
94 if !done { thread::sleep(Duration::from_millis(400)); }
99 impl Drop for AudioConsumer {
101 let ret = self.afifo.lock();
102 if let Ok(mut afifo) = ret {
108 struct DecoderStuff {
109 dsupp: Box<NADecoderSupport>,
110 dec: Box<dyn NADecoder + Send>,
111 reord: Box<dyn FrameReorderer + Send>,
120 RGB(u64, NABufferRef<Surface>),
121 YUV(u64, NABufferRef<Overlay>),
126 fn get_time(&self) -> u64 {
128 DisplayEvent::RGB(time, _) => time,
129 DisplayEvent::YUV(time, _) => time,
135 struct CommonMessages {
136 receiver: mpsc::Receiver<SendEvent>,
137 esend: mpsc::SyncSender<DisplayEvent>,
138 ev_mtx: Arc<Mutex<isize>>,
139 finished: Arc<AtomicBool>,
142 fn add_audio<T:Copy>(amtx: &mut Arc<Mutex<AudioFIFO>>, data: &[T]) {
143 let len = std::mem::size_of::<T>() * data.len();
144 let mut done = false;
146 let ret = amtx.try_lock();
147 if let Ok(mut afifo) = ret {
149 let u8_ptr = data.as_ptr();
150 let u8_data = std::mem::transmute((u8_ptr, len));
151 done = afifo.add(u8_data);
154 if !done { std::thread::sleep(Duration::from_millis(100)); }
158 fn open_audio(arate: u32, amtx: Arc<Mutex<AudioFIFO>>) -> NAAudioInfo {
159 let dspec = DesiredAudioSpec {
160 freq: arate as i32, format: AudioFormat::S16Lsb, channels: Channels::Stereo, samples: AUDIO_BUF_SIZE as u16,
161 callback: Box::new(AudioConsumer::new(amtx))
163 let tspec = sdl::audio::open(dspec).unwrap();
164 //println!("target spec: {} Hz fmt {:X} {} ch {} samp {} size", tspec.freq, tspec.format as u32, if tspec.channels == Channels::Stereo { 2 } else { 1 }, tspec.samples, tspec.size);
165 sdl::audio::pause(true);
166 let dst_ch = if tspec.channels == Channels::Stereo { 2 } else { 1 };
167 let snd_fmt = match tspec.format {
168 AudioFormat::U8 => SND_U8_FORMAT,
169 AudioFormat::S8 => unimplemented!(),
170 AudioFormat::U16Lsb => unimplemented!(),
171 AudioFormat::S16Lsb => SND_S16_FORMAT,
172 AudioFormat::U16Msb => unimplemented!(),
173 AudioFormat::S16Msb => NASoniton { bits: 16, be: true, packed: false, planar: false, float: false, signed: true },
175 NAAudioInfo { sample_rate: tspec.freq as u32, channels: dst_ch, format: snd_fmt, block_len: 1024 }
178 fn start_audio_dec(audio_dec: Option<DecoderStuff>, dst_info: NAAudioInfo, cmsg: CommonMessages) -> thread::JoinHandle<()> {
179 thread::spawn(move || {
180 let mut audio_dec = audio_dec.unwrap();
181 let dst_chmap = if dst_info.channels == 2 {
182 NAChannelMap::from_str("L,R").unwrap()
184 NAChannelMap::from_str("C").unwrap()
187 let ret = cmsg.receiver.recv();
188 if ret.is_err() { break; }
189 if let Ok(SendEvent::EOF) = ret { break; }
190 let pkt = if let Ok(SendEvent::Packet(pkt)) = ret { pkt } else { unreachable!(); };
191 let ret = audio_dec.dec.decode(&mut audio_dec.dsupp, &pkt);
192 if let Ok(frm) = ret {
193 let buf = frm.get_buffer();
194 let out_buf = convert_audio_frame(&buf, &dst_info, &dst_chmap).unwrap();
195 cmsg.esend.send(DisplayEvent::Audio(out_buf)).unwrap();
196 let mut count = cmsg.ev_mtx.lock().unwrap();
201 println!("error decoding audio");
204 cmsg.finished.store(true, Ordering::Relaxed);
208 fn start_video_dec(video_dec: Option<DecoderStuff>, ifmt: Option<NAVideoInfo>, width: usize, height: usize, tb_num: u32, tb_den: u32, cmsg: CommonMessages, scr_mtx: Arc<Mutex<NABufferRef<Surface>>>) -> thread::JoinHandle<()> {
209 thread::spawn(move || {
210 let mut ifmt = ifmt.unwrap();
211 let mut video_dec = video_dec.unwrap();
212 let rgb32_fmt = NAPixelFormaton { model: ColorModel::RGB(RGBSubmodel::RGB), components: 3,
214 Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 0, next_elem: 4 }),
215 Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 1, next_elem: 4 }),
216 Some(NAPixelChromaton { h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 2, next_elem: 4 }),
218 ], elem_size: 4, be: false, alpha: false, palette: false };
219 let ofmt_rgb = ScaleInfo { width, height, fmt: rgb32_fmt };
220 let ofmt_yuv = ScaleInfo { width, height, fmt: YUV420_FORMAT };
222 let sc_ifmt = ScaleInfo { width: ifmt.get_width(), height: ifmt.get_height(), fmt: ifmt.get_format() };
223 let mut do_yuv = if let ColorModel::YUV(_) = ifmt.get_format().get_model() { true } else { false };
224 let ofmt = if do_yuv { ofmt_yuv } else { ofmt_rgb };
225 let mut opic = alloc_video_buffer(NAVideoInfo::new(width, height, false, ofmt.fmt), 4).unwrap();
226 let mut scaler = NAScale::new(sc_ifmt, ofmt).unwrap();
228 let ret = cmsg.receiver.recv();
229 if ret.is_err() { break; }
230 if let Ok(SendEvent::EOF) = ret { break; }
231 let pkt = if let Ok(SendEvent::Packet(pkt)) = ret { pkt } else { unreachable!() };
232 let ret = video_dec.dec.decode(&mut video_dec.dsupp, &pkt);
233 if let Ok(frm) = ret {
234 video_dec.reord.add_frame(frm);
235 while let Some(frm) = video_dec.reord.get_frame() {
236 let bt = frm.get_buffer();
237 if let NABufferType::None = bt { continue; }
238 let vinfo = bt.get_video_info().unwrap();
239 if ifmt.get_width() != vinfo.get_width() ||
240 ifmt.get_height() != vinfo.get_height() ||
241 ifmt.get_format() != vinfo.get_format() {
242 println!("reinit scaler!");
243 ifmt = vinfo.clone();
244 let sc_ifmt = ScaleInfo { width: ifmt.get_width(), height: ifmt.get_height(), fmt: ifmt.get_format() };
245 do_yuv = if let ColorModel::YUV(_) = ifmt.get_format().get_model() { true } else { false };
246 let ofmt = if do_yuv { ofmt_yuv } else { ofmt_rgb };
247 opic = alloc_video_buffer(NAVideoInfo::new(width, height, false, ofmt.fmt), 4).unwrap();
248 scaler = NAScale::new(sc_ifmt, ofmt).unwrap();
250 let ret = scaler.convert(&bt, &mut opic);
251 if ret.is_err() { println!(" scaler error {:?}", ret.err()); continue; }
253 let ts = frm.get_dts().unwrap_or(frm.get_pts().unwrap_or(0));
254 let time = NATimeInfo::ts_to_time(ts, 1000, tb_num, tb_den);
256 let buf = opic.get_vbuf().unwrap();
258 let sstride = buf.get_stride(0);
259 let src = buf.get_data();
260 let surface = Surface::new(&[SurfaceFlag::SWSurface], width as isize, height as isize, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000).unwrap();
261 let pitch = unsafe { (*surface.raw).pitch } as usize;
262 surface.with_lock(|x: &mut [u8]| -> bool {
263 let csize = sstride.min(pitch);
264 for (dst, src) in x.chunks_mut(pitch).zip(src.chunks(sstride)) {
265 (&mut dst[..csize]).copy_from_slice(&src[..csize]);
269 let mut count = cmsg.ev_mtx.lock().unwrap();
270 cmsg.esend.send(DisplayEvent::RGB(time, NABufferRef::new(surface))).unwrap();
273 let screen = scr_mtx.lock().unwrap();
274 let overlay = screen.create_overlay(width as isize, height as isize, OverlayFormat::YV12).unwrap();
276 while !overlay.lock() {}
277 let src = buf.get_data();
278 let ysstride = buf.get_stride(0);
279 let ysrc = &src[buf.get_offset(0)..];
280 let usstride = buf.get_stride(2);
281 let usrc = &src[buf.get_offset(2)..];
282 let vsstride = buf.get_stride(1);
283 let vsrc = &src[buf.get_offset(1)..];
285 let ydst = overlay.get_pixel_ptr(0);
286 let ydstride = overlay.get_pitch(0);
287 let udst = overlay.get_pixel_ptr(1);
288 let udstride = overlay.get_pitch(1);
289 let vdst = overlay.get_pixel_ptr(2);
290 let vdstride = overlay.get_pitch(2);
291 for (ydst, ysrc) in ydst.chunks_mut(ydstride).take(height).zip(ysrc.chunks(ysstride)) {
292 (&mut ydst[..width]).copy_from_slice(&ysrc[..width]);
294 for (udst, usrc) in udst.chunks_mut(udstride).take(height).zip(usrc.chunks(usstride)) {
295 (&mut udst[..width / 2]).copy_from_slice(&usrc[..width / 2]);
297 for (vdst, vsrc) in vdst.chunks_mut(vdstride).take(height).zip(vsrc.chunks(vsstride)) {
298 (&mut vdst[..width / 2]).copy_from_slice(&vsrc[..width / 2]);
302 let mut count = cmsg.ev_mtx.lock().unwrap();
303 cmsg.esend.send(DisplayEvent::YUV(time, NABufferRef::new(overlay))).unwrap();
308 println!("error decoding video");
311 cmsg.finished.store(true, Ordering::Relaxed);
315 fn play_file(args: Vec<String>) {
317 let mut cur_arg: usize = 1;
318 let mut decode_audio = true;
319 while (cur_arg < args.len()) && args[cur_arg].starts_with('-') {
320 match args[cur_arg].as_str() {
322 "-an" => { decode_audio = false; },
323 _ => { println!("unknown option {}", args[cur_arg]); return; },
327 let name = args[cur_arg].as_str();
329 let path = Path::new(name);
330 let mut file = File::open(path).unwrap();
332 let mut fr = FileReader::new_read(&mut file);
333 let mut br = ByteReader::new(&mut fr);
334 let res = detect::detect_format(name, &mut br);
336 println!("cannot detect format for {}", name);
339 let (dmx_name, _) = res.unwrap();
340 println!("trying demuxer {} on {}", dmx_name, name);
342 let mut dmx_reg = RegisteredDemuxers::new();
343 nihav_register_all_demuxers(&mut dmx_reg);
344 let mut dec_reg = RegisteredDecoders::new();
345 nihav_register_all_decoders(&mut dec_reg);
347 dmx_fact = dmx_reg.find_demuxer(dmx_name).unwrap();
348 br.seek(SeekFrom::Start(0)).unwrap();
349 let mut dmx = create_demuxer(dmx_fact, &mut br).unwrap();
352 let mut height = 480;
357 let mut video_str = 0;
358 let mut audio_str = 0;
360 let mut video_dec: Option<DecoderStuff> = None;
361 let mut audio_dec: Option<DecoderStuff> = None;
363 for i in 0..dmx.get_num_streams() {
364 let s = dmx.get_stream(i).unwrap();
365 let info = s.get_info();
366 let decfunc = dec_reg.find_decoder(info.get_name());
367 println!("stream {} - {} {}", i, s, info.get_name());
368 let str_id = s.get_id();
370 if video_dec.is_none() {
371 if decfunc.is_none() {
372 println!("no video decoder for {} found!", info.get_name());
375 let mut dec = (decfunc.unwrap())();
376 let mut dsupp = Box::new(NADecoderSupport::new());
377 let props = info.get_properties().get_video_info().unwrap();
378 if props.get_width() != 0 {
379 width = props.get_width();
380 height = props.get_height();
381 ifmt = Some(props.clone());
383 let reorder_depth = 3;
384 dsupp.pool_u8 = NAVideoBufferPool::new(reorder_depth);
385 dsupp.pool_u16 = NAVideoBufferPool::new(reorder_depth);
386 dsupp.pool_u32 = NAVideoBufferPool::new(reorder_depth);
387 dec.init(&mut dsupp, info).unwrap();
388 let reord = Box::new(IPBReorderer::new());
389 video_dec = Some(DecoderStuff{ dsupp, dec, reord });
391 let (tbn, tbd) = s.get_timebase();
395 } else if info.is_audio() {
396 if audio_dec.is_none() && decode_audio {
397 if decfunc.is_none() {
398 println!("no audio decoder for {} found!", info.get_name());
400 let mut dec = (decfunc.unwrap())();
401 let mut dsupp = Box::new(NADecoderSupport::new());
402 let props = info.get_properties().get_audio_info().unwrap();
403 arate = props.get_sample_rate();
404 dec.init(&mut dsupp, info).unwrap();
405 let reord = Box::new(NoReorderer::new());
406 audio_dec = Some(DecoderStuff{ dsupp, dec, reord });
411 println!("decoder {} not found", info.get_name());
415 while (width <= 384) && (height <= 288) {
420 sdl::init(&[sdl::InitFlag::Video, sdl::InitFlag::Audio]);
421 sdl::wm::set_caption("NihAV Player", "nihav-player");
422 let screen = match sdl::video::set_video_mode(width as isize, height as isize, 32,
423 &[SurfaceFlag::HWSurface, SurfaceFlag::AsyncBlit, SurfaceFlag::HWAccel],
424 &[VideoFlag::DoubleBuf]) {
425 Ok(screen) => screen,
426 Err(err) => panic!("failed to set video mode: {}", err)
429 let (vsend, vrecv) = mpsc::sync_channel::<SendEvent>(0);
430 let (asend, arecv) = mpsc::sync_channel::<SendEvent>(0);
431 let (esend, erecv) = mpsc::sync_channel::<DisplayEvent>(50);
432 let events_mtx = Arc::new(Mutex::new(0isize));
434 let has_audio = audio_dec.is_some();
435 let mut amtx = Arc::new(Mutex::new(AudioFIFO::new((arate * 8) as usize, (arate * 80) as usize)));
436 let aud_finished = Arc::new(AtomicBool::new(!has_audio));
437 let audio_thread = if has_audio {
438 let ainfo = open_audio(arate, amtx.clone());
439 let cmsg = CommonMessages {
441 esend: esend.clone(),
442 ev_mtx: events_mtx.clone(),
443 finished: aud_finished.clone(),
445 Some(start_audio_dec(audio_dec, ainfo, cmsg))
448 let has_video = video_dec.is_some();
449 let video_thread: Option<thread::JoinHandle<()>>;
450 let scr_mtx = Arc::new(Mutex::new(NABufferRef::new(screen)));
451 let vid_finished = Arc::new(AtomicBool::new(!has_video));
453 let cmsg = CommonMessages {
456 ev_mtx: events_mtx.clone(),
457 finished: vid_finished.clone(),
459 video_thread = Some(start_video_dec(video_dec, ifmt, width, height, tb_num, tb_den, cmsg, scr_mtx.clone()));
464 let mut frame_queue: Vec<DisplayEvent> = Vec::new();
466 let systime = SystemTime::now();
467 let mut has_data = true;
471 match sdl::event::poll_event() {
472 Event::Quit => break 'main,
473 Event::None => break 'event,
474 Event::Key(k, _, _, _)
475 if k == Key::Escape || k == Key::Q
477 Event::Key(k, _, _, _)
484 let pktres = dmx.get_frame();
485 if let Err(DemuxerError::EOF) = pktres {
488 vsend.send(SendEvent::EOF).unwrap();
491 asend.send(SendEvent::EOF).unwrap();
493 } else if let Err(_) = pktres {
495 } else if let Ok(pkt) = pktres {
496 let streamno = pkt.get_stream().get_id();
498 if has_video && streamno == video_str {
499 vsend.send(SendEvent::Packet(pkt)).unwrap();
500 } else if has_audio && streamno == audio_str {
501 asend.send(SendEvent::Packet(pkt)).unwrap();
506 let mut nevents = events_mtx.lock().unwrap();
509 let ret = erecv.recv();
510 if ret.is_err() { break 'main; }
511 let mut disp_evt = ret.unwrap();
513 DisplayEvent::Audio(ref mut out_buf) => {
514 unsafe { sdl::audio::ll::SDL_LockAudio(); }
516 NABufferType::AudioPacked(buf) => add_audio(&mut amtx, buf.get_data()),
517 NABufferType::AudioU8(buf) => add_audio(&mut amtx, buf.get_data()),
518 NABufferType::AudioI16(buf) => add_audio(&mut amtx, buf.get_data()),
519 NABufferType::AudioI32(buf) => add_audio(&mut amtx, buf.get_data()),
520 NABufferType::AudioF32(buf) => add_audio(&mut amtx, buf.get_data()),
523 unsafe { sdl::audio::ll::SDL_UnlockAudio(); }
524 sdl::audio::pause(false);
526 _ => { frame_queue.push(disp_evt); },
530 if vid_finished.load(Ordering::Relaxed) &&
531 aud_finished.load(Ordering::Relaxed) && frame_queue.len() == 0 {
535 if frame_queue.len() > 0 {
536 let cur_time = systime.elapsed().unwrap();
537 let disp_time = Duration::from_millis(frame_queue[0].get_time());
539 //println!("cur time {:?} disp time {:?}", cur_time, disp_time);
540 if (disp_time <= cur_time + Duration::from_millis(10)) && disp_time + Duration::from_millis(10) >= cur_time {
541 let screen = scr_mtx.lock().unwrap();
542 let disp_evt = frame_queue.remove(0);
544 DisplayEvent::RGB(_, ref surf) => {
548 DisplayEvent::YUV(_, ref ovl) => {
549 ovl.display(Some(screen.get_rect()));
553 } else if disp_time > cur_time {
554 let diff = disp_time - cur_time;
555 if diff > Duration::from_millis(20) {
556 thread::sleep(Duration::from_millis(20));
561 frame_queue.remove(0);
567 unsafe { sdl::audio::ll::SDL_LockAudio(); }
568 let mut afifo = amtx.lock().unwrap();
571 unsafe { sdl::audio::ll::SDL_UnlockAudio(); }
572 sdl::audio::pause(true);
576 if let Some(vthread) = video_thread {
577 vthread.join().unwrap();
579 if let Some(athread) = audio_thread {
580 athread.join().unwrap();
589 let args: Vec<String> = env::args().collect();
592 println!("usage: nihav-player input");