3939int VideoStreamPlaybackTheora::buffer_data () {
4040 char *buffer = ogg_sync_buffer (&oy, 4096 );
4141
42- #ifdef THEORA_USE_THREAD_STREAMING
43-
44- int read;
45-
46- do {
47- thread_sem->post ();
48- read = MIN (ring_buffer.data_left (), 4096 );
49- if (read) {
50- ring_buffer.read ((uint8_t *)buffer, read);
51- ogg_sync_wrote (&oy, read);
52- } else {
53- OS::get_singleton ()->delay_usec (100 );
54- }
55-
56- } while (read == 0 );
57-
58- return read;
59-
60- #else
61-
6242 uint64_t bytes = file->get_buffer ((uint8_t *)buffer, 4096 );
6343 ogg_sync_wrote (&oy, bytes);
6444 return (bytes);
65-
66- #endif
6745}
6846
6947int VideoStreamPlaybackTheora::queue_page (ogg_page *page) {
@@ -82,10 +60,7 @@ int VideoStreamPlaybackTheora::queue_page(ogg_page *page) {
8260 return 0 ;
8361}
8462
85- void VideoStreamPlaybackTheora::video_write () {
86- th_ycbcr_buffer yuv;
87- th_decode_ycbcr_out (td, yuv);
88-
63+ void VideoStreamPlaybackTheora::video_write (th_ycbcr_buffer yuv) {
8964 int pitch = 4 ;
9065 frame_data.resize (size.x * size.y * pitch);
9166 {
@@ -106,10 +81,11 @@ void VideoStreamPlaybackTheora::video_write() {
10681 }
10782
10883 Ref<Image> img = memnew (Image (size.x , size.y , false , Image::FORMAT_RGBA8, frame_data)); // zero copy image creation
84+ if (region.size .x != size.x || region.size .y != size.y ) {
85+ img = img->get_region (region);
86+ }
10987
11088 texture->update (img); // zero copy send to rendering server
111-
112- frames_pending = 1 ;
11389}
11490
11591void VideoStreamPlaybackTheora::clear () {
@@ -136,20 +112,15 @@ void VideoStreamPlaybackTheora::clear() {
136112 }
137113 ogg_sync_clear (&oy);
138114
139- #ifdef THEORA_USE_THREAD_STREAMING
140- thread_exit = true ;
141- thread_sem->post (); // just in case
142- thread.wait_to_finish ();
143- ring_buffer.clear ();
144- #endif
145-
146115 theora_p = 0 ;
147116 vorbis_p = 0 ;
148- videobuf_ready = 0 ;
149- frames_pending = 0 ;
150- videobuf_time = 0 ;
117+ next_frame_time = 0 ;
118+ current_frame_time = 0 ;
151119 theora_eos = false ;
152120 vorbis_eos = false ;
121+ video_ready = false ;
122+ video_done = false ;
123+ audio_done = false ;
153124
154125 file.unref ();
155126 playing = false ;
@@ -164,17 +135,6 @@ void VideoStreamPlaybackTheora::set_file(const String &p_file) {
164135 file = FileAccess::open (p_file, FileAccess::READ);
165136 ERR_FAIL_COND_MSG (file.is_null (), " Cannot open file '" + p_file + " '." );
166137
167- #ifdef THEORA_USE_THREAD_STREAMING
168- thread_exit = false ;
169- thread_eof = false ;
170- // pre-fill buffer
171- int to_read = ring_buffer.space_left ();
172- uint64_t read = file->get_buffer (read_buffer.ptr (), to_read);
173- ring_buffer.write (read_buffer.ptr (), read);
174-
175- thread.start (_streaming_thread, this );
176- #endif
177-
178138 ogg_sync_init (&oy);
179139
180140 /* init supporting Vorbis structures needed in header parsing */
@@ -327,16 +287,17 @@ void VideoStreamPlaybackTheora::set_file(const String &p_file) {
327287 th_decode_ctl (td, TH_DECCTL_SET_PPLEVEL, &pp_level, sizeof (pp_level));
328288 pp_inc = 0 ;
329289
330- int w ;
331- int h ;
332- w = ((ti. pic_x + ti. frame_width + 1 ) & ~ 1 ) - ( ti.pic_x & ~ 1 ) ;
333- h = ((ti. pic_y + ti. frame_height + 1 ) & ~ 1 ) - ( ti.pic_y & ~ 1 ) ;
334- size.x = w ;
335- size.y = h ;
290+ size. x = ti. frame_width ;
291+ size. y = ti. frame_height ;
292+ region. position . x = ti.pic_x ;
293+ region. position . y = ti.pic_y ;
294+ region. size .x = ti. pic_width ;
295+ region. size .y = ti. pic_height ;
336296
337- Ref<Image> img = Image::create_empty (w, h , false , Image::FORMAT_RGBA8);
297+ Ref<Image> img = Image::create_empty (region. size . x , region. size . y , false , Image::FORMAT_RGBA8);
338298 texture->set_image (img);
339299
300+ frame_duration = (double )ti.fps_denominator / ti.fps_numerator ;
340301 } else {
341302 /* tear down the partial theora setup */
342303 th_info_clear (&ti);
@@ -358,7 +319,8 @@ void VideoStreamPlaybackTheora::set_file(const String &p_file) {
358319 playing = false ;
359320 buffering = true ;
360321 time = 0 ;
361- audio_frames_wrote = 0 ;
322+ video_done = !theora_p;
323+ audio_done = !vorbis_p;
362324}
363325
364326double VideoStreamPlaybackTheora::get_time () const {
@@ -378,88 +340,55 @@ void VideoStreamPlaybackTheora::update(double p_delta) {
378340 }
379341
380342 if (!playing || paused) {
381- // printf("not playing\n");
382343 return ;
383344 }
384345
385- #ifdef THEORA_USE_THREAD_STREAMING
386- thread_sem->post ();
387- #endif
388-
389346 time += p_delta;
390347
391- if (videobuf_time > get_time ()) {
392- return ; // no new frames need to be produced
393- }
394-
395- bool frame_done = false ;
396- bool audio_done = !vorbis_p;
397-
398- while (!frame_done || (!audio_done && !vorbis_eos)) {
399- // a frame needs to be produced
348+ double comp_time = get_time ();
349+ bool audio_ready = false ;
400350
351+ // Read data until we fill the audio buffer and get a new video frame.
352+ while ((!audio_ready && !audio_done) || (!video_ready && !video_done)) {
401353 ogg_packet op;
402- bool no_theora = false ;
403- bool buffer_full = false ;
404354
405- while (vorbis_p && !audio_done && !buffer_full) {
406- int ret;
355+ while (!audio_ready && !audio_done) {
407356 float **pcm;
408-
409- /* if there's pending, decoded audio, grab it */
410- ret = vorbis_synthesis_pcmout (&vd, &pcm);
357+ int ret = vorbis_synthesis_pcmout (&vd, &pcm);
411358 if (ret > 0 ) {
412359 const int AUXBUF_LEN = 4096 ;
413360 int to_read = ret;
414361 float aux_buffer[AUXBUF_LEN];
415-
416362 while (to_read) {
417363 int m = MIN (AUXBUF_LEN / vi.channels , to_read);
418-
419364 int count = 0 ;
420-
421365 for (int j = 0 ; j < m; j++) {
422366 for (int i = 0 ; i < vi.channels ; i++) {
423367 aux_buffer[count++] = pcm[i][j];
424368 }
425369 }
426-
427- if (mix_callback) {
428- int mixed = mix_callback (mix_udata, aux_buffer, m);
429- to_read -= mixed;
430- if (mixed != m) { // could mix no more
431- buffer_full = true ;
432- break ;
433- }
434- } else {
435- to_read -= m; // just pretend we sent the audio
370+ int mixed = mix_callback (mix_udata, aux_buffer, m);
371+ to_read -= mixed;
372+ if (mixed != m) { // could mix no more
373+ audio_ready = true ;
374+ break ;
436375 }
437376 }
438-
439377 vorbis_synthesis_read (&vd, ret - to_read);
440-
441- audio_frames_wrote += ret - to_read;
442-
443378 } else {
444379 /* no pending audio; is there a pending packet to decode? */
445380 if (ogg_stream_packetout (&vo, &op) > 0 ) {
446381 if (vorbis_synthesis (&vb, &op) == 0 ) { /* test for success! */
447382 vorbis_synthesis_blockin (&vd, &vb);
448383 }
449384 } else { /* we need more data; break out to suck in another page */
385+ audio_done = vorbis_eos;
450386 break ;
451387 }
452388 }
453-
454- audio_done = videobuf_time < (audio_frames_wrote / float (vi.rate ));
455-
456- if (buffer_full) {
457- break ;
458- }
459389 }
460390
461- while (theora_p && !frame_done) {
462- /* theora is one in, one out... */
391+ while (!video_ready && !video_done) {
463392 if (ogg_stream_packetout (&to, &op) > 0 ) {
464393 /* HACK: This should be set after a seek or a gap, but we might not have
465394 a granulepos for the first packet (we only have them for the last
@@ -472,62 +401,37 @@ void VideoStreamPlaybackTheora::update(double p_delta) {
472401 sizeof (op.granulepos ));
473402 }
474403 ogg_int64_t videobuf_granulepos;
475- if (th_decode_packetin (td, &op, &videobuf_granulepos) == 0 ) {
476- videobuf_time = th_granule_time (td, videobuf_granulepos);
477-
478- // printf("frame time %f, play time %f, ready %i\n", (float)videobuf_time, get_time(), videobuf_ready);
479-
480- /* is it already too old to be useful? This is only actually
481- useful cosmetically after a SIGSTOP. Note that we have to
482- decode the frame even if we don't show it (for now) due to
483- keyframing. Soon enough libtheora will be able to deal
484- with non-keyframe seeks. */
485-
486- if (videobuf_time >= get_time ()) {
487- frame_done = true ;
404+ int ret = th_decode_packetin (td, &op, &videobuf_granulepos);
405+ if (ret == 0 || ret == TH_DUPFRAME) {
406+ next_frame_time = th_granule_time (td, videobuf_granulepos);
407+ if (next_frame_time > comp_time) {
408+ dup_frame = (ret == TH_DUPFRAME);
409+ video_ready = true ;
488410 } else {
489411 /* If we are too slow, reduce the pp level.*/
490412 pp_inc = pp_level > 0 ? -1 : 0 ;
491413 }
492414 }
493-
494- } else {
495- no_theora = true ;
415+ } else { /* we need more data; break out to suck in another page */
416+ video_done = theora_eos;
496417 break ;
497418 }
498419 }
499420
500- #ifdef THEORA_USE_THREAD_STREAMING
501- if (file.is_valid () && thread_eof && no_theora && theora_eos && ring_buffer.data_left () == 0 ) {
502- #else
503- if (file.is_valid () && /* !videobuf_ready && */ no_theora && theora_eos) {
504- #endif
505- // printf("video done, stopping\n");
506- stop ();
507- return ;
508- }
509-
510- if (!frame_done || !audio_done) {
511- // what's the point of waiting for audio to grab a page?
512-
513- buffer_data ();
514- while (ogg_sync_pageout (&oy, &og) > 0 ) {
515- queue_page (&og);
421+ if (!video_ready || !audio_ready) {
422+ int ret = buffer_data ();
423+ if (ret > 0 ) {
424+ while (ogg_sync_pageout (&oy, &og) > 0 ) {
425+ queue_page (&og);
426+ }
427+ } else {
428+ vorbis_eos = true ;
429+ theora_eos = true ;
430+ break ;
516431 }
517432 }
518433
519- /* If playback has begun, top audio buffer off immediately. */
520- // if(stateflag) audio_write_nonblocking();
521-
522- /* are we at or past time for this video frame? */
523- if (videobuf_ready && videobuf_time <= get_time ()) {
524- // video_write();
525- // videobuf_ready=0;
526- } else {
527- // printf("frame at %f not ready (time %f), ready %i\n", (float)videobuf_time, get_time(), videobuf_ready);
528- }
529-
530- double tdiff = videobuf_time - get_time ();
434+ double tdiff = next_frame_time - comp_time;
531435 /* If we have lots of extra time, increase the post-processing level.*/
532436 if (tdiff > ti.fps_denominator * 0.25 / ti.fps_numerator ) {
533437 pp_inc = pp_level < pp_level_max ? 1 : 0 ;
@@ -536,7 +440,22 @@ void VideoStreamPlaybackTheora::update(double p_delta) {
536440 }
537441 }
538442
539- video_write ();
443+ if (!video_ready && video_done && audio_done) {
444+ stop ();
445+ return ;
446+ }
447+
448+ // Wait for the last frame to end before rendering the next one.
449+ if (video_ready && comp_time >= current_frame_time) {
450+ if (!dup_frame) {
451+ th_ycbcr_buffer yuv;
452+ th_decode_ycbcr_out (td, yuv);
453+ video_write (yuv);
454+ }
455+ dup_frame = false ;
456+ video_ready = false ;
457+ current_frame_time = next_frame_time;
458+ }
540459}
541460
542461void VideoStreamPlaybackTheora::play () {
@@ -596,44 +515,11 @@ int VideoStreamPlaybackTheora::get_mix_rate() const {
596515 return vi.rate ;
597516}
598517
599- #ifdef THEORA_USE_THREAD_STREAMING
600-
601- void VideoStreamPlaybackTheora::_streaming_thread (void *ud) {
602- VideoStreamPlaybackTheora *vs = static_cast <VideoStreamPlaybackTheora *>(ud);
603-
604- while (!vs->thread_exit ) {
605- // just fill back the buffer
606- if (!vs->thread_eof ) {
607- int to_read = vs->ring_buffer .space_left ();
608- if (to_read > 0 ) {
609- uint64_t read = vs->file ->get_buffer (vs->read_buffer .ptr (), to_read);
610- vs->ring_buffer .write (vs->read_buffer .ptr (), read);
611- vs->thread_eof = vs->file ->eof_reached ();
612- }
613- }
614-
615- vs->thread_sem ->wait ();
616- }
617- }
618-
619- #endif
620-
621518VideoStreamPlaybackTheora::VideoStreamPlaybackTheora () {
622519 texture.instantiate ();
623-
624- #ifdef THEORA_USE_THREAD_STREAMING
625- int rb_power = nearest_shift (RB_SIZE_KB * 1024 );
626- ring_buffer.resize (rb_power);
627- read_buffer.resize (RB_SIZE_KB * 1024 );
628- thread_sem = Semaphore::create ();
629-
630- #endif
631520}
632521
633522VideoStreamPlaybackTheora::~VideoStreamPlaybackTheora () {
634- #ifdef THEORA_USE_THREAD_STREAMING
635- memdelete (thread_sem);
636- #endif
637523 clear ();
638524}
639525
0 commit comments