void DGVideo::unload() { if (_isLoaded) { _isLoaded = false; _state = DGVideoInitial; _theoraInfo->videobuf_ready = 0; _theoraInfo->videobuf_granulepos -= 1; _theoraInfo->videobuf_time = 0; if (_theoraInfo->theora_p) { // Rewind and reset fseek(_handle, (long)_theoraInfo->bos * 8, SEEK_SET); ogg_stream_reset(&_theoraInfo->to); ogg_stream_clear(&_theoraInfo->to); theora_clear(&_theoraInfo->td); theora_comment_clear(&_theoraInfo->tc); theora_info_clear(&_theoraInfo->ti); } ogg_sync_clear(&_theoraInfo->oy); _theoraInfo->theora_p = 0; free(_currentFrame.data); fclose(_handle); } }
static int noop_test_encode () { theora_info ti; theora_state th; INFO ("+ Initializing theora_info struct"); theora_info_init (&ti); INFO ("+ Setting a 16x16 frame"); ti.width = 16; ti.height = 16; INFO ("+ Setting a 1:1 frame rate"); ti.fps_numerator = 1; ti.fps_denominator = 1; INFO ("+ Initializing theora_state for encoding"); if (theora_encode_init (&th, &ti) != OC_DISABLED) { INFO ("+ Clearing theora_state"); theora_clear (&th); } INFO ("+ Clearing theora_info struct"); theora_info_clear (&ti); return 0; }
void Cin_OGM_Shutdown() { #ifdef USE_CIN_XVID int status; status = shutdown_xvid(); if (status) Com_Printf("[Xvid]Decore RELEASE problem, return value %d\n", status); #endif #ifdef USE_CIN_THEORA theora_clear(&g_ogm.th_state); theora_comment_clear(&g_ogm.th_comment); theora_info_clear(&g_ogm.th_info); #endif if(g_ogm.outputBuffer) free(g_ogm.outputBuffer); g_ogm.outputBuffer = NULL; vorbis_dsp_clear(&g_ogm.vd); vorbis_comment_clear(&g_ogm.vc); vorbis_info_clear(&g_ogm.vi); /* must be called last (comment from vorbis example code) */ ogg_stream_clear(&g_ogm.os_audio); ogg_stream_clear(&g_ogm.os_video); ogg_sync_clear(&g_ogm.oy); FS_FCloseFile(g_ogm.ogmFile); g_ogm.ogmFile = 0; }
static av_cold int encode_close(AVCodecContext* avc_context) { ogg_packet o_packet; TheoraContext *h = avc_context->priv_data; int result; const char* message; result = theora_encode_packetout( &(h->t_state), 1, &o_packet ); theora_clear( &(h->t_state) ); av_freep(&avc_context->coded_frame); av_freep(&avc_context->extradata); avc_context->extradata_size = 0; switch (result) { case 0:/* No packet is ready */ case -1:/* Encoding finished */ return 0; case 1: /* We have a packet */ message = "gave us a packet"; break; default: message = "unknown reason"; break; } av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed (%s) [%d]\n", message, result); return -1; }
static void enc_postprocess(MSFilter *f){ EncState *s=(EncState*)f->data; theora_clear(&s->tstate); theora_info_clear(&s->tinfo); if (s->packed_conf) { freemsg(s->packed_conf); s->packed_conf=NULL; } }
void TheoraClose() { ogg_stream_clear ( &m_StreamState ); theora_clear ( &m_State ); theora_comment_clear ( &m_Comment ); theora_info_clear ( &m_Info ); ogg_sync_clear ( &m_SyncState ); fclose( m_File ); }
int mm_close(mm_file *mf) { assert(mf); if (mf->file) { fclose(mf->file); mf->file = NULL; } if (mf->audio) { ogg_stream_destroy(mf->audio); mf->audio = NULL; } if (mf->video) { ogg_stream_destroy(mf->video); mf->video = NULL; } if (mf->video_ctx) { theora_clear(mf->video_ctx); free(mf->video_ctx); mf->video_ctx = NULL; } if (mf->video_info) { theora_info_clear(mf->video_info); free(mf->video_info); mf->video_info = NULL; } if (mf->audio_blk) { vorbis_block_clear(mf->audio_blk); free(mf->audio_blk); mf->audio_blk = NULL; } if (mf->audio_ctx) { vorbis_dsp_clear(mf->audio_ctx); free(mf->audio_ctx); mf->audio_ctx = NULL; } if (mf->audio_info) { vorbis_info_clear(mf->audio_info); free(mf->audio_info); mf->audio_info = NULL; } ogg_sync_clear(&mf->sync); return 0; }
/* * uninit driver */ static void uninit(sh_video_t *sh) { theora_struct_t *context = (theora_struct_t *)sh->context; if (context) { theora_info_clear(&context->inf); theora_comment_clear(&context->cc); theora_clear (&context->st); free (context); } }
static GF_Err THEO_DetachStream(GF_BaseDecoder *ifcg, u16 ES_ID) { THEORACTX(); if (ctx->ES_ID != ES_ID) return GF_BAD_PARAM; theora_clear(&ctx->td); theora_info_clear(&ctx->ti); theora_comment_clear(&ctx->tc); ctx->ES_ID = 0; return GF_OK; }
static void theora_enc_finalize (GObject * object) { GstTheoraEnc *enc = GST_THEORA_ENC (object); GST_DEBUG_OBJECT (enc, "Finalizing"); theora_clear (&enc->state); theora_comment_clear (&enc->comment); theora_info_clear (&enc->info); G_OBJECT_CLASS (parent_class)->finalize (object); }
static void enc_postprocess(MSFilter *f){ EncState *s=(EncState*)f->data; theora_clear(&s->tstate); //If preprocess is called after postprocess, //then we loose all info... //theora_info_clear(&s->tinfo); if (s->packed_conf) { freemsg(s->packed_conf); s->packed_conf=NULL; } }
bool Oggeyman::shutdown() { // shutdown in the reverse order of intialization theora_clear(&theo_state); ogg_stream_clear(&ogg_theora_stream_state); theora_comment_clear(&theo_comment); theora_info_clear(&theo_info); ogg_sync_clear(&overall_sync_state); if (infptr) fclose(infptr); return true; }
static void altheora_close(APEG_LAYER *layer) { ALOGG_INFO *info = layer->ogg_info; if(info && (layer->stream.flags&APEG_THEORA_VIDEO)) { theora_clear(&info->tstate); theora_info_clear(&info->tinfo); theora_comment_clear(&info->tcomment); ogg_stream_clear(&info->ostream[0]); layer->stream.flags &= ~APEG_THEORA_VIDEO; } }
static GstStateChangeReturn theora_enc_change_state (GstElement * element, GstStateChange transition) { GstTheoraEnc *enc; GstStateChangeReturn ret; enc = GST_THEORA_ENC (element); switch (transition) { case GST_STATE_CHANGE_NULL_TO_READY: break; case GST_STATE_CHANGE_READY_TO_PAUSED: GST_DEBUG_OBJECT (enc, "READY->PAUSED Initing theora state"); theora_info_init (&enc->info); theora_comment_init (&enc->comment); enc->packetno = 0; break; case GST_STATE_CHANGE_PAUSED_TO_PLAYING: break; default: break; } ret = parent_class->change_state (element, transition); switch (transition) { case GST_STATE_CHANGE_PLAYING_TO_PAUSED: break; case GST_STATE_CHANGE_PAUSED_TO_READY: GST_DEBUG_OBJECT (enc, "PAUSED->READY Clearing theora state"); theora_clear (&enc->state); theora_comment_clear (&enc->comment); theora_info_clear (&enc->info); theora_enc_clear (enc); enc->initialised = FALSE; break; case GST_STATE_CHANGE_READY_TO_NULL: break; default: break; } return ret; }
void OggFree(OggData *data) { if (data->audio) { ogg_stream_clear(&data->astream); vorbis_block_clear(&data->vblock); vorbis_dsp_clear(&data->vdsp); vorbis_comment_clear(&data->vcomment); vorbis_info_clear(&data->vinfo); } #ifdef USE_THEORA if (data->video) { ogg_stream_clear(&data->vstream); theora_comment_clear(&data->tcomment); theora_info_clear(&data->tinfo); theora_clear(&data->tstate); } #endif ogg_sync_clear(&data->sync); }
static int noop_test_encode () { theora_info ti; theora_state th; INFO ("+ Initializing theora_info struct"); theora_info_init (&ti); INFO ("+ Initializing theora_state for encoding"); if (theora_encode_init (&th, &ti) != OC_DISABLED) { INFO ("+ Clearing theora_state"); theora_clear (&th); } INFO ("+ Clearing theora_info struct"); theora_info_clear (&ti); return 0; }
static int noop_test_decode () { theora_info ti; theora_state th; INFO ("+ Initializing theora_info struct"); theora_info_init (&ti); INFO ("+ Initializing theora_state for decoding"); theora_decode_init (&th, &ti); INFO ("+ Clearing theora_state"); theora_clear (&th); INFO ("+ Clearing theora_info struct"); theora_info_clear (&ti); return 0; }
static GstStateChangeReturn theora_dec_change_state (GstElement * element, GstStateChange transition) { GstTheoraDec *dec = GST_THEORA_DEC (element); GstStateChangeReturn ret; switch (transition) { case GST_STATE_CHANGE_NULL_TO_READY: break; case GST_STATE_CHANGE_READY_TO_PAUSED: theora_info_init (&dec->info); theora_comment_init (&dec->comment); GST_DEBUG_OBJECT (dec, "Setting have_header to FALSE in READY->PAUSED"); dec->have_header = FALSE; gst_theora_dec_reset (dec); break; case GST_STATE_CHANGE_PAUSED_TO_PLAYING: break; default: break; } ret = parent_class->change_state (element, transition); switch (transition) { case GST_STATE_CHANGE_PLAYING_TO_PAUSED: break; case GST_STATE_CHANGE_PAUSED_TO_READY: theora_clear (&dec->state); theora_comment_clear (&dec->comment); theora_info_clear (&dec->info); gst_theora_dec_reset (dec); break; case GST_STATE_CHANGE_READY_TO_NULL: break; default: break; } return ret; }
void OGV_StopVideo(cinematic_t *cin) { if (cin->data) { theora_clear(&g_ogm->th_state); theora_comment_clear(&g_ogm->th_comment); theora_info_clear(&g_ogm->th_info); vorbis_dsp_clear(&g_ogm->vd); vorbis_comment_clear(&g_ogm->vc); vorbis_info_clear(&g_ogm->vi); /* must be called last (comment from vorbis example code) */ ogg_stream_clear(&g_ogm->os_audio); ogg_stream_clear(&g_ogm->os_video); ogg_sync_clear(&g_ogm->oy); Com_Dealloc(cin->data); cin->data = NULL; } }
int main(int argc,char *argv[]){ int c,long_option_index,ret; ogg_stream_state to; /* take physical pages, weld into a logical stream of packets */ ogg_stream_state vo; /* take physical pages, weld into a logical stream of packets */ ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */ ogg_packet op; /* one raw packet of data for decode */ theora_state td; theora_info ti; theora_comment tc; vorbis_info vi; /* struct that stores all the static vorbis bitstream settings */ vorbis_comment vc; /* struct that stores all the user comments */ vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */ vorbis_block vb; /* local working space for packet->PCM decode */ int audioflag=0; int videoflag=0; int akbps=0; int vkbps=0; ogg_int64_t audio_bytesout=0; ogg_int64_t video_bytesout=0; double timebase; FILE* outfile = stdout; #ifdef _WIN32 # ifdef THEORA_PERF_DATA LARGE_INTEGER start_time; LARGE_INTEGER final_time; LONGLONG elapsed_ticks; LARGE_INTEGER ticks_per_second; LONGLONG elapsed_secs; LONGLONG elapsed_sec_mod; double elapsed_secs_dbl ; # endif /* We need to set stdin/stdout to binary mode. Damn windows. */ /* if we were reading/writing a file, it would also need to in binary mode, eg, fopen("file.wav","wb"); */ /* Beware the evil ifdef. We avoid these where we can, but this one we cannot. Don't add any more, you'll probably go to hell if you do. */ _setmode( _fileno( stdin ), _O_BINARY ); _setmode( _fileno( stdout ), _O_BINARY ); #endif while((c=getopt_long(argc,argv,optstring,options,&long_option_index))!=EOF){ switch(c){ case 'o': outfile=fopen(optarg,"wb"); if(outfile==NULL){ fprintf(stderr,"Unable to open output file '%s'\n", optarg); exit(1); } break;; case 'a': audio_q=atof(optarg)*.099; if(audio_q<-.1 || audio_q>1){ fprintf(stderr,"Illegal audio quality (choose -1 through 10)\n"); exit(1); } audio_r=-1; break; case 'v': video_q=rint(atof(optarg)*6.3); if(video_q<0 || video_q>63){ fprintf(stderr,"Illegal video quality (choose 0 through 10)\n"); exit(1); } video_r=0; break; case 'A': audio_r=atof(optarg)*1000; if(audio_q<0){ fprintf(stderr,"Illegal audio quality (choose > 0 please)\n"); exit(1); } audio_q=-99; break; case 'V': video_r=rint(atof(optarg)*1000); if(video_r<45000 || video_r>2000000){ fprintf(stderr,"Illegal video bitrate (choose 45kbps through 2000kbps)\n"); exit(1); } video_q=0; break; case 's': video_an=rint(atof(optarg)); break; case 'S': video_ad=rint(atof(optarg)); break; case 'f': video_hzn=rint(atof(optarg)); break; case 'F': video_hzd=rint(atof(optarg)); break; default: usage(); } } while(optind<argc){ /* assume that anything following the options must be a filename */ id_file(argv[optind]); optind++; } #ifdef THEORA_PERF_DATA # ifdef WIN32 QueryPerformanceCounter(&start_time); # endif #endif /* yayness. Set up Ogg output stream */ srand(time(NULL)); { /* need two inequal serial numbers */ int serial1, serial2; serial1 = rand(); serial2 = rand(); if (serial1 == serial2) serial2++; ogg_stream_init(&to,serial1); ogg_stream_init(&vo,serial2); } /* Set up Theora encoder */ if(!video){ fprintf(stderr,"No video files submitted for compression?\n"); exit(1); } /* Theora has a divisible-by-sixteen restriction for the encoded video size */ /* scale the frame size up to the nearest /16 and calculate offsets */ video_x=((frame_x + 15) >>4)<<4; video_y=((frame_y + 15) >>4)<<4; /* We force the offset to be even. This ensures that the chroma samples align properly with the luma samples. */ frame_x_offset=((video_x-frame_x)/2)&~1; frame_y_offset=((video_y-frame_y)/2)&~1; theora_info_init(&ti); ti.width=video_x; ti.height=video_y; ti.frame_width=frame_x; ti.frame_height=frame_y; ti.offset_x=frame_x_offset; ti.offset_y=frame_y_offset; ti.fps_numerator=video_hzn; ti.fps_denominator=video_hzd; ti.aspect_numerator=video_an; ti.aspect_denominator=video_ad; ti.colorspace=OC_CS_UNSPECIFIED; ti.pixelformat=OC_PF_420; ti.target_bitrate=video_r; ti.quality=video_q; ti.dropframes_p=0; ti.quick_p=1; ti.keyframe_auto_p=1; ti.keyframe_frequency=64; ti.keyframe_frequency_force=64; ti.keyframe_data_target_bitrate=video_r*1.5; ti.keyframe_auto_threshold=80; ti.keyframe_mindistance=8; ti.noise_sensitivity=1; theora_encode_init(&td,&ti); theora_info_clear(&ti); /* initialize Vorbis too, assuming we have audio to compress. */ if(audio){ vorbis_info_init(&vi); if(audio_q>-99) ret = vorbis_encode_init_vbr(&vi,audio_ch,audio_hz,audio_q); else ret = vorbis_encode_init(&vi,audio_ch,audio_hz,-1,audio_r,-1); if(ret){ fprintf(stderr,"The Vorbis encoder could not set up a mode according to\n" "the requested quality or bitrate.\n\n"); exit(1); } vorbis_comment_init(&vc); vorbis_analysis_init(&vd,&vi); vorbis_block_init(&vd,&vb); } /* write the bitstream header packets with proper page interleave */ /* first packet will get its own page automatically */ theora_encode_header(&td,&op); ogg_stream_packetin(&to,&op); if(ogg_stream_pageout(&to,&og)!=1){ fprintf(stderr,"Internal Ogg library error.\n"); exit(1); } fwrite(og.header,1,og.header_len,outfile); fwrite(og.body,1,og.body_len,outfile); /* create the remaining theora headers */ theora_comment_init(&tc); theora_encode_comment(&tc,&op); ogg_stream_packetin(&to,&op); /*theora_encode_comment() doesn't take a theora_state parameter, so it has to allocate its own buffer to pass back the packet data. If we don't free it here, we'll leak. libogg2 makes this much cleaner: the stream owns the buffer after you call packetin in libogg2, but this is not true in libogg1.*/ free(op.packet); theora_encode_tables(&td,&op); ogg_stream_packetin(&to,&op); if(audio){ ogg_packet header; ogg_packet header_comm; ogg_packet header_code; vorbis_analysis_headerout(&vd,&vc,&header,&header_comm,&header_code); ogg_stream_packetin(&vo,&header); /* automatically placed in its own page */ if(ogg_stream_pageout(&vo,&og)!=1){ fprintf(stderr,"Internal Ogg library error.\n"); exit(1); } fwrite(og.header,1,og.header_len,outfile); fwrite(og.body,1,og.body_len,outfile); /* remaining vorbis header packets */ ogg_stream_packetin(&vo,&header_comm); ogg_stream_packetin(&vo,&header_code); } /* Flush the rest of our headers. This ensures the actual data in each stream will start on a new page, as per spec. */ while(1){ int result = ogg_stream_flush(&to,&og); if(result<0){ /* can't get here */ fprintf(stderr,"Internal Ogg library error.\n"); exit(1); } if(result==0)break; fwrite(og.header,1,og.header_len,outfile); fwrite(og.body,1,og.body_len,outfile); } if(audio){ while(1){ int result=ogg_stream_flush(&vo,&og); if(result<0){ /* can't get here */ fprintf(stderr,"Internal Ogg library error.\n"); exit(1); } if(result==0)break; fwrite(og.header,1,og.header_len,outfile); fwrite(og.body,1,og.body_len,outfile); } } /* setup complete. Raw processing loop */ fprintf(stderr,"Compressing....\n"); while(1){ ogg_page audiopage; ogg_page videopage; /* is there an audio page flushed? If not, fetch one if possible */ audioflag=fetch_and_process_audio(audio,&audiopage,&vo,&vd,&vb,audioflag); /* is there a video page flushed? If not, fetch one if possible */ videoflag=fetch_and_process_video(video,&videopage,&to,&td,videoflag); /* no pages of either? Must be end of stream. */ if(!audioflag && !videoflag)break; /* which is earlier; the end of the audio page or the end of the video page? Flush the earlier to stream */ { int audio_or_video=-1; double audiotime= audioflag?vorbis_granule_time(&vd,ogg_page_granulepos(&audiopage)):-1; double videotime= videoflag?theora_granule_time(&td,ogg_page_granulepos(&videopage)):-1; if(!audioflag){ audio_or_video=1; } else if(!videoflag) { audio_or_video=0; } else { if(audiotime<videotime) audio_or_video=0; else audio_or_video=1; } if(audio_or_video==1){ /* flush a video page */ video_bytesout+=fwrite(videopage.header,1,videopage.header_len,outfile); video_bytesout+=fwrite(videopage.body,1,videopage.body_len,outfile); videoflag=0; timebase=videotime; }else{ /* flush an audio page */ audio_bytesout+=fwrite(audiopage.header,1,audiopage.header_len,outfile); audio_bytesout+=fwrite(audiopage.body,1,audiopage.body_len,outfile); audioflag=0; timebase=audiotime; } { int hundredths=timebase*100-(long)timebase*100; int seconds=(long)timebase%60; int minutes=((long)timebase/60)%60; int hours=(long)timebase/3600; if(audio_or_video) vkbps=rint(video_bytesout*8./timebase*.001); else akbps=rint(audio_bytesout*8./timebase*.001); fprintf(stderr, "\r %d:%02d:%02d.%02d audio: %dkbps video: %dkbps ", hours,minutes,seconds,hundredths,akbps,vkbps); } } } /* clear out state */ if(audio){ ogg_stream_clear(&vo); vorbis_block_clear(&vb); vorbis_dsp_clear(&vd); vorbis_comment_clear(&vc); vorbis_info_clear(&vi); } if(video){ ogg_stream_clear(&to); theora_clear(&td); } if(outfile && outfile!=stdout)fclose(outfile); fprintf(stderr,"\r \ndone.\n\n"); #ifdef THEORA_PERF_DATA # ifdef WIN32 QueryPerformanceCounter(&final_time); elapsed_ticks = final_time.QuadPart - start_time.QuadPart; ticks_per_second; QueryPerformanceFrequency(&ticks_per_second); elapsed_secs = elapsed_ticks / ticks_per_second.QuadPart; elapsed_sec_mod = elapsed_ticks % ticks_per_second.QuadPart; elapsed_secs_dbl = elapsed_secs; elapsed_secs_dbl += ((double)elapsed_sec_mod / (double)ticks_per_second.QuadPart); printf("Encode time = %lld ticks\n", elapsed_ticks); printf("~%lld and %lld / %lld seconds\n", elapsed_secs, elapsed_sec_mod, ticks_per_second.QuadPart); printf("~%Lf seconds\n", elapsed_secs_dbl); # endif #endif return(0); }
static void theora_enc_reset (GstTheoraEnc * enc) { theora_clear (&enc->state); theora_encode_init (&enc->state, &enc->info); }
decoderTheora::~decoderTheora() { printf("Theora destroyed\n"); theora_clear(&_tstate); }
int main(int argc,char *argv[]){ ogg_packet op; int long_option_index; int c; FILE *infile = stdin; outfile = stdout; #ifdef _WIN32 /* We need to set stdin/stdout to binary mode on windows. */ /* Beware the evil ifdef. We avoid these where we can, but this one we cannot. Don't add any more, you'll probably go to hell if you do. */ _setmode( _fileno( stdin ), _O_BINARY ); _setmode( _fileno( stdout ), _O_BINARY ); #endif /* Process option arguments. */ while((c=getopt_long(argc,argv,optstring,options,&long_option_index))!=EOF){ switch(c){ case 'o': outfile=fopen(optarg,"wb"); if(outfile==NULL){ fprintf(stderr,"Unable to open output file '%s'\n", optarg); exit(1); } break; default: usage(); } } if(optind<argc){ infile=fopen(argv[optind],"rb"); if(infile==NULL){ fprintf(stderr,"Unable to open '%s' for extraction.\n", argv[optind]); exit(1); } if(++optind<argc){ usage(); exit(1); } } /* Ok, Ogg parsing. The idea here is we have a bitstream that is made up of Ogg pages. The libogg sync layer will find them for us. There may be pages from several logical streams interleaved; we find the first theora stream and ignore any others. Then we pass the pages for our stream to the libogg stream layer which assembles our original set of packets out of them. It's the packets that libtheora actually knows how to handle. */ /* start up Ogg stream synchronization layer */ ogg_sync_init(&oy); /* init supporting Theora structures needed in header parsing */ theora_comment_init(&tc); theora_info_init(&ti); /* Ogg file open; parse the headers */ /* Vorbis and Theora both depend on some initial header packets for decoder setup and initialization. We retrieve these first before entering the main decode loop. */ /* Only interested in Theora streams */ while(!stateflag){ int ret=buffer_data(infile,&oy); if(ret==0)break; while(ogg_sync_pageout(&oy,&og)>0){ ogg_stream_state test; /* is this a mandated initial header? If not, stop parsing */ if(!ogg_page_bos(&og)){ /* don't leak the page; get it into the appropriate stream */ queue_page(&og); stateflag=1; break; } ogg_stream_init(&test,ogg_page_serialno(&og)); ogg_stream_pagein(&test,&og); ogg_stream_packetout(&test,&op); /* identify the codec: try theora */ if(!theora_p && theora_decode_header(&ti,&tc,&op)>=0){ /* it is theora -- save this stream state */ memcpy(&to,&test,sizeof(test)); theora_p=1; }else{ /* whatever it is, we don't care about it */ ogg_stream_clear(&test); } } /* fall through to non-initial page parsing */ } /* we're expecting more header packets. */ while(theora_p && theora_p<3){ int ret; /* look for further theora headers */ while(theora_p && (theora_p<3) && (ret=ogg_stream_packetout(&to,&op))){ if(ret<0){ fprintf(stderr,"Error parsing Theora stream headers; corrupt stream?\n"); exit(1); } if(theora_decode_header(&ti,&tc,&op)){ printf("Error parsing Theora stream headers; corrupt stream?\n"); exit(1); } theora_p++; if(theora_p==3)break; } /* The header pages/packets will arrive before anything else we care about, or the stream is not obeying spec */ if(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); /* demux into the stream state */ }else{ int ret=buffer_data(infile,&oy); /* need more data */ if(ret==0){ fprintf(stderr,"End of file while searching for codec headers.\n"); exit(1); } } } /* Now we have all the required headers. initialize the decoder. */ if(theora_p){ theora_decode_init(&td,&ti); fprintf(stderr,"Ogg logical stream %x is Theora %dx%d %.02f fps video\nEncoded frame content is %dx%d with %dx%d offset\n", to.serialno,ti.width,ti.height, (double)ti.fps_numerator/ti.fps_denominator, ti.frame_width, ti.frame_height, ti.offset_x, ti.offset_y); }else{ /* tear down the partial theora setup */ theora_info_clear(&ti); theora_comment_clear(&tc); } /* open video */ if(theora_p)open_video(); /* install signal handler */ signal (SIGINT, sigint_handler); /* Finally the main decode loop. It's one Theora packet per frame, so this is pretty straightforward if we're not trying to maintain sync with other multiplexed streams. the videobuf_ready flag is used to maintain the input buffer in the libogg stream state. If there's no output frame available at the end of the decode step, we must need more input data. We could simplify this by just using the return code on ogg_page_packetout(), but the flag system extends easily to the case were you care about more than one multiplexed stream (like with audio playback). In that case, just maintain a flag for each decoder you care about, and pull data when any one of them stalls. videobuf_time holds the presentation time of the currently buffered video frame. We ignore this value. */ stateflag=0; /* playback has not begun */ /* queue any remaining pages from data we buffered but that did not contain headers */ while(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); } while(!got_sigint){ while(theora_p && !videobuf_ready){ /* theora is one in, one out... */ if(ogg_stream_packetout(&to,&op)>0){ theora_decode_packetin(&td,&op); videobuf_granulepos=td.granulepos; videobuf_time=theora_granule_time(&td,videobuf_granulepos); videobuf_ready=1; }else break; } if(!videobuf_ready && feof(infile))break; if(!videobuf_ready){ /* no data yet for somebody. Grab another page */ int ret=buffer_data(infile,&oy); while(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); } } /* dumpvideo frame, and get new one */ else video_write(); videobuf_ready=0; } /* end of decoder loop -- close everything */ if(theora_p){ ogg_stream_clear(&to); theora_clear(&td); theora_comment_clear(&tc); theora_info_clear(&ti); } ogg_sync_clear(&oy); if(infile && infile!=stdin)fclose(infile); fprintf(stderr, "\r " "\nDone.\n"); return(0); }
static int granulepos_test_encode (int frequency, int auto_p) { theora_info ti; theora_state th; int result; int frame, tframe, keyframe, keydist; int shift; double rate, ttime; yuv_buffer yuv; unsigned char *framedata; ogg_packet op; long long int last_granule = -1; /* INFO ("+ Initializing theora_info struct"); */ theora_info_init (&ti); ti.width = 32; ti.height = 32; ti.frame_width = ti.width; ti.frame_height = ti.frame_height; ti.offset_x = 0; ti.offset_y = 0; ti.fps_numerator = 16; ti.fps_denominator = 1; ti.aspect_numerator = 1; ti.aspect_denominator = 1; ti.colorspace = OC_CS_UNSPECIFIED; ti.pixelformat = OC_PF_420; ti.target_bitrate = 0; ti.quality = 16; ti.dropframes_p = 0; ti.quick_p = 1; /* check variations of automatic or forced keyframe choice */ ti.keyframe_auto_p = auto_p; /* check with variations of the maximum gap */ ti.keyframe_frequency = frequency; ti.keyframe_frequency_force = frequency; ti.keyframe_data_target_bitrate = ti.target_bitrate * 1.5; ti.keyframe_auto_threshold = 80; ti.keyframe_mindistance = MIN(8, frequency); ti.noise_sensitivity = 1; /* INFO ("+ Initializing theora_state for encoding"); */ result = theora_encode_init (&th, &ti); if (result == OC_DISABLED) { INFO ("+ Clearing theora_state"); theora_clear (&th); } else if (result < 0) { FAIL ("negative return code initializing encoder"); } /* INFO ("+ Setting up dummy 4:2:0 frame data"); */ framedata = calloc(ti.height, ti.width); yuv.y_width = ti.width; yuv.y_height = ti.height; yuv.y_stride = ti.width; yuv.y = framedata; yuv.uv_width = ti.width / 2; yuv.uv_height = ti.width / 2; yuv.uv_stride = ti.width; yuv.u = framedata; yuv.v = framedata; INFO ("+ Checking granulepos generation"); shift = theora_granule_shift(&ti); rate = (double)ti.fps_denominator/ti.fps_numerator; for (frame = 0; frame < frequency * 2 + 1; frame++) { result = theora_encode_YUVin (&th, &yuv); if (result < 0) { printf("theora_encode_YUVin() returned %d\n", result); FAIL ("negative error code submitting frame for compression"); } theora_encode_packetout (&th, frame >= frequency * 2, &op); if ((long long int)op.granulepos < last_granule) FAIL ("encoder returned a decreasing granulepos value"); last_granule = op.granulepos; keyframe = op.granulepos >> shift; keydist = op.granulepos - (keyframe << shift); tframe = theora_granule_frame (&th, op.granulepos); ttime = theora_granule_time(&th, op.granulepos); #if DEBUG printf("++ frame %d granulepos %lld %d:%d %d %.3lfs\n", frame, (long long int)op.granulepos, keyframe, keydist, tframe, theora_granule_time (&th, op.granulepos)); #endif if ((keyframe + keydist) != frame + 1) FAIL ("encoder granulepos does not map to the correct frame number"); if (tframe != frame) FAIL ("theora_granule_frame returned incorrect results"); if (fabs(rate*(frame+1) - ttime) > 1.0e-6) FAIL ("theora_granule_time returned incorrect results"); } /* clean up */ /* INFO ("+ Freeing dummy frame data"); */ free (framedata); /* INFO ("+ Clearing theora_info struct"); */ theora_info_clear (&ti); /* INFO ("+ Clearing theora_state"); */ theora_clear (&th); return 0; }
int main(int argc,char *argv[]){ int i,j; ogg_packet op; FILE *infile = stdin; #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */ /* Beware the evil ifdef. We avoid these where we can, but this one we cannot. Don't add any more, you'll probably go to hell if you do. */ _setmode( _fileno( stdin ), _O_BINARY ); #endif /* open the input file if any */ if(argc==2){ infile=fopen(argv[1],"rb"); if(infile==NULL){ fprintf(stderr,"Unable to open '%s' for playback.\n", argv[1]); exit(1); } } if(argc>2){ usage(); exit(1); } /* start up Ogg stream synchronization layer */ ogg_sync_init(&oy); /* init supporting Vorbis structures needed in header parsing */ vorbis_info_init(&vi); vorbis_comment_init(&vc); /* init supporting Theora structures needed in header parsing */ theora_comment_init(&tc); theora_info_init(&ti); /* Ogg file open; parse the headers */ /* Only interested in Vorbis/Theora streams */ while(!stateflag){ int ret=buffer_data(infile,&oy); if(ret==0)break; while(ogg_sync_pageout(&oy,&og)>0){ ogg_stream_state test; /* is this a mandated initial header? If not, stop parsing */ if(!ogg_page_bos(&og)){ /* don't leak the page; get it into the appropriate stream */ queue_page(&og); stateflag=1; break; } ogg_stream_init(&test,ogg_page_serialno(&og)); ogg_stream_pagein(&test,&og); ogg_stream_packetout(&test,&op); /* identify the codec: try theora */ if(!theora_p && theora_decode_header(&ti,&tc,&op)>=0){ /* it is theora */ memcpy(&to,&test,sizeof(test)); theora_p=1; }else if(!vorbis_p && vorbis_synthesis_headerin(&vi,&vc,&op)>=0){ /* it is vorbis */ memcpy(&vo,&test,sizeof(test)); vorbis_p=1; }else{ /* whatever it is, we don't care about it */ ogg_stream_clear(&test); } } /* fall through to non-bos page parsing */ } /* we're expecting more header packets. */ while((theora_p && theora_p<3) || (vorbis_p && vorbis_p<3)){ int ret; /* look for further theora headers */ while(theora_p && (theora_p<3) && (ret=ogg_stream_packetout(&to,&op))){ if(ret<0){ fprintf(stderr,"Error parsing Theora stream headers; corrupt stream?\n"); exit(1); } if(theora_decode_header(&ti,&tc,&op)){ printf("Error parsing Theora stream headers; corrupt stream?\n"); exit(1); } theora_p++; if(theora_p==3)break; } /* look for more vorbis header packets */ while(vorbis_p && (vorbis_p<3) && (ret=ogg_stream_packetout(&vo,&op))){ if(ret<0){ fprintf(stderr,"Error parsing Vorbis stream headers; corrupt stream?\n"); exit(1); } if(vorbis_synthesis_headerin(&vi,&vc,&op)){ fprintf(stderr,"Error parsing Vorbis stream headers; corrupt stream?\n"); exit(1); } vorbis_p++; if(vorbis_p==3)break; } /* The header pages/packets will arrive before anything else we care about, or the stream is not obeying spec */ if(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); /* demux into the appropriate stream */ }else{ int ret=buffer_data(infile,&oy); /* someone needs more data */ if(ret==0){ fprintf(stderr,"End of file while searching for codec headers.\n"); exit(1); } } } /* and now we have it all. initialize decoders */ if(theora_p){ theora_decode_init(&td,&ti); printf("Ogg logical stream %x is Theora %dx%d %.02f fps video\n", (unsigned int)to.serialno,ti.width,ti.height, (double)ti.fps_numerator/ti.fps_denominator); if(ti.width!=ti.frame_width || ti.height!=ti.frame_height) printf(" Frame content is %dx%d with offset (%d,%d).\n", ti.frame_width, ti.frame_height, ti.offset_x, ti.offset_y); report_colorspace(&ti); dump_comments(&tc); }else{ /* tear down the partial theora setup */ theora_info_clear(&ti); theora_comment_clear(&tc); } if(vorbis_p){ vorbis_synthesis_init(&vd,&vi); vorbis_block_init(&vd,&vb); fprintf(stderr,"Ogg logical stream %x is Vorbis %d channel %d Hz audio.\n", (unsigned int)vo.serialno,vi.channels,(int)vi.rate); }else{ /* tear down the partial vorbis setup */ vorbis_info_clear(&vi); vorbis_comment_clear(&vc); } /* open audio */ if(vorbis_p)open_audio(); /* open video */ if(theora_p)open_video(); /* install signal handler as SDL clobbered the default */ signal (SIGINT, sigint_handler); /* on to the main decode loop. We assume in this example that audio and video start roughly together, and don't begin playback until we have a start frame for both. This is not necessarily a valid assumption in Ogg A/V streams! It will always be true of the example_encoder (and most streams) though. */ stateflag=0; /* playback has not begun */ while(!got_sigint){ /* we want a video and audio frame ready to go at all times. If we have to buffer incoming, buffer the compressed data (ie, let ogg do the buffering) */ while(vorbis_p && !audiobuf_ready){ int ret; float **pcm; /* if there's pending, decoded audio, grab it */ if((ret=vorbis_synthesis_pcmout(&vd,&pcm))>0){ int count=audiobuf_fill/2; int maxsamples=(audiofd_fragsize-audiobuf_fill)/2/vi.channels; for(i=0;i<ret && i<maxsamples;i++) for(j=0;j<vi.channels;j++){ int val=rint(pcm[j][i]*32767.f); if(val>32767)val=32767; if(val<-32768)val=-32768; audiobuf[count++]=val; } vorbis_synthesis_read(&vd,i); audiobuf_fill+=i*vi.channels*2; if(audiobuf_fill==audiofd_fragsize)audiobuf_ready=1; if(vd.granulepos>=0) audiobuf_granulepos=vd.granulepos-ret+i; else audiobuf_granulepos+=i; }else{ /* no pending audio; is there a pending packet to decode? */ if(ogg_stream_packetout(&vo,&op)>0){ if(vorbis_synthesis(&vb,&op)==0) /* test for success! */ vorbis_synthesis_blockin(&vd,&vb); }else /* we need more data; break out to suck in another page */ break; } } while(theora_p && !videobuf_ready){ /* theora is one in, one out... */ if(ogg_stream_packetout(&to,&op)>0){ theora_decode_packetin(&td,&op); videobuf_granulepos=td.granulepos; videobuf_time=theora_granule_time(&td,videobuf_granulepos); /* is it already too old to be useful? This is only actually useful cosmetically after a SIGSTOP. Note that we have to decode the frame even if we don't show it (for now) due to keyframing. Soon enough libtheora will be able to deal with non-keyframe seeks. */ if(videobuf_time>=get_time()) videobuf_ready=1; }else break; } if(!videobuf_ready && !audiobuf_ready && feof(infile))break; if(!videobuf_ready || !audiobuf_ready){ /* no data yet for somebody. Grab another page */ int bytes=buffer_data(infile,&oy); while(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); } } /* If playback has begun, top audio buffer off immediately. */ if(stateflag) audio_write_nonblocking(); /* are we at or past time for this video frame? */ if(stateflag && videobuf_ready && videobuf_time<=get_time()){ video_write(); videobuf_ready=0; } if(stateflag && (audiobuf_ready || !vorbis_p) && (videobuf_ready || !theora_p) && !got_sigint){ /* we have an audio frame ready (which means the audio buffer is full), it's not time to play video, so wait until one of the audio buffer is ready or it's near time to play video */ /* set up select wait on the audiobuffer and a timeout for video */ struct timeval timeout; fd_set writefs; fd_set empty; int n=0; FD_ZERO(&writefs); FD_ZERO(&empty); if(audiofd>=0){ FD_SET(audiofd,&writefs); n=audiofd+1; } if(theora_p){ long milliseconds=(videobuf_time-get_time())*1000-5; if(milliseconds>500)milliseconds=500; if(milliseconds>0){ timeout.tv_sec=milliseconds/1000; timeout.tv_usec=(milliseconds%1000)*1000; n=select(n,&empty,&writefs,&empty,&timeout); if(n)audio_calibrate_timer(0); } }else{ select(n,&empty,&writefs,&empty,NULL); } } /* if our buffers either don't exist or are ready to go, we can begin playback */ if((!theora_p || videobuf_ready) && (!vorbis_p || audiobuf_ready))stateflag=1; /* same if we've run out of input */ if(feof(infile))stateflag=1; } /* tear it all down */ audio_close(); SDL_Quit(); if(vorbis_p){ ogg_stream_clear(&vo); vorbis_block_clear(&vb); vorbis_dsp_clear(&vd); vorbis_comment_clear(&vc); vorbis_info_clear(&vi); } if(theora_p){ ogg_stream_clear(&to); theora_clear(&td); theora_comment_clear(&tc); theora_info_clear(&ti); } ogg_sync_clear(&oy); if(infile && infile!=stdin)fclose(infile); fprintf(stderr, "\r " "\nDone.\n"); return(0); }
bool CTheoraPlayer::playVideo( //Plays specified OGG Theora file to screen surface. //If screen == NULL, then this method will test that the file is playable //by decoding it as fast as possible but not displaying anything. // //Returns: whether playback was successful CStretchyBuffer& buffer, SDL_Surface *screen, const int x, const int y) //[default=(0,0)] { //init theora_p = vorbis_p = 0; startticks = 0; bool bSkippedLastFrame = false; // start up Ogg stream synchronization layer ogg_sync_init(&oy); // init supporting Vorbis structures needed in header parsing vorbis_info_init(&vi); vorbis_comment_init(&vc); // init supporting Theora structures needed in header parsing theora_comment_init(&tc); theora_info_init(&ti); if (!screen) ti.quick_p = 1; ti.quality = 63; if (!parseHeaders(buffer)) return false; // force audio off vorbis_p = 0; // initialize decoders if (theora_p) { theora_decode_init(&td,&ti); #if 0 printf("Ogg logical stream %x is Theora %dx%d %.02f fps video\n" " Frame content is %dx%d with offset (%d,%d).\n", to.serialno,ti.width,ti.height, (double)ti.fps_numerator/ti.fps_denominator, ti.frame_width, ti.frame_height, ti.offset_x, ti.offset_y); //report_colorspace(&ti); //we're not using this info for anything dump_comments(&tc); #endif } else { // tear down the partial theora setup theora_info_clear(&ti); theora_comment_clear(&tc); } if(vorbis_p) { vorbis_synthesis_init(&vd,&vi); vorbis_block_init(&vd,&vb); printf("Ogg logical stream %lx is Vorbis %d channel %ld Hz audio.\n", vo.serialno,vi.channels,vi.rate); } else { // tear down the partial vorbis setup vorbis_info_clear(&vi); vorbis_comment_clear(&vc); } // open audio if (vorbis_p) open_audio(); // open video SDL_Overlay *yuv_overlay = NULL; if (theora_p && screen) yuv_overlay = open_video(screen); // single frame video buffering ogg_packet op; ogg_int64_t videobuf_granulepos=-1; double videobuf_time=0; double last_frame_time = 0; bool hasdatatobuffer = true; // Main loop bool audiobuf_ready=false; bool videobuf_ready=false; bool playbackdone = (yuv_overlay == NULL); bool isPlaying = false; bool bBreakout = false; while (!playbackdone) { // break out on SDL quit event SDL_Event event; if (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: playbackdone = bBreakout = true; break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE) playbackdone = bBreakout = true; break; default: break; } } while (theora_p && !videobuf_ready) { // get one video packet... if (ogg_stream_packetout(&to,&op)>0) { theora_decode_packetin(&td,&op); videobuf_granulepos=td.granulepos; videobuf_time=theora_granule_time(&td,videobuf_granulepos); #if 0 //Without sound channels to synch to, don't need to worry about skipping frames when slow. // update the frame counter //++frameNum; // check if this frame time has not passed yet. // If the frame is late we need to decode additional // ones and keep looping, since theora at this stage // needs to decode all frames. const double now=get_time(); const double delay=videobuf_time-now; if(delay>=0.0){ /// got a good frame, not late, ready to break out videobuf_ready=true; } else if(now-last_frame_time>=1.0) { // display at least one frame per second, regardless videobuf_ready=true; } else { //Need to catch up -- no time to display frame. if (bSkippedLastFrame) //only allow skipping one frame in a row videobuf_ready = true; //show anyway else bSkippedLastFrame = true; //printf("dropping frame %d (%.3fs behind)\n", frameNum, -delay); } #else videobuf_ready = true; //show every frame #endif } else { // need more data break; } } if (!hasdatatobuffer && !videobuf_ready && !audiobuf_ready) { isPlaying = false; playbackdone = true; } //If we're set for the next frame, sleep. //In other words, don't show frames too rapidly. if((!theora_p || videobuf_ready) && (!vorbis_p || audiobuf_ready)) { const int ticks = (int)(1000*(videobuf_time-get_time())); if(ticks>0 && screen) //don't need to sleep if only testing file SDL_Delay(ticks); } if (videobuf_ready) { // time to write our cached frame if (screen) { const bool bRes = video_write(screen, yuv_overlay, x, y); if (!bRes) //couldn't display image playbackdone = bBreakout = true; } videobuf_ready=false; last_frame_time=get_time(); bSkippedLastFrame = false; // if audio has not started (first frame) then start it if ((!isPlaying)&&(vorbis_p)) { start_audio(); isPlaying = true; } } // HACK: always look for more audio data audiobuf_ready=false; // buffer compressed data every loop if (hasdatatobuffer) { hasdatatobuffer = buffer_data(&oy, buffer) > 0; if (!hasdatatobuffer) { //printf("Ogg buffering stopped, end of file reached.\n"); } } if (ogg_sync_pageout(&oy,&og)>0) queue_page(&og); } // playbackdone // show number of video frames decoded //printf("\nFrames decoded: %d\n", frameNum); // deinit if (vorbis_p) { audio_close(); ogg_stream_clear(&vo); vorbis_block_clear(&vb); vorbis_dsp_clear(&vd); vorbis_comment_clear(&vc); vorbis_info_clear(&vi); } if (theora_p) { if (yuv_overlay) SDL_FreeYUVOverlay(yuv_overlay); ogg_stream_clear(&to); theora_clear(&td); theora_comment_clear(&tc); theora_info_clear(&ti); } ogg_sync_clear(&oy); //If broken out of testing, return false since entire file was not verified. return !bBreakout || screen != NULL; }
static BGBBTJ_VidCodecCTX *theo_begin_compress(int fcc, BGBBTJ_BMPInfoHeader *in, BGBBTJ_BMPInfoHeader *out) { byte tbuf[16384]; ogg_packet op; BGBBTJ_VidCodecCTX *ctx; vfw_ctxinfo *info; byte *ct; int err, sz, xs, ys, xs1, ys1, px, py; if((fcc!=RIFF_TAG_THEO) && (fcc!=RIFF_TAG_theo) && (fcc!=RIFF_TAG_ther)) return(NULL); ctx=BGBBTJ_VidCodecCTX_New(); info=gcalloc(sizeof(vfw_ctxinfo)); ctx->data=info; info->ihead=gcalloc(sizeof(BGBBTJ_BMPInfoHeader)); memset(info->ihead, 0, sizeof(BGBBTJ_BMPInfoHeader)); info->ihead->biSize = sizeof(BGBBTJ_BMPInfoHeader); info->ihead->biWidth = in->biWidth; info->ihead->biHeight = in->biHeight; info->ihead->biPlanes = in->biPlanes; info->ihead->biBitCount = in->biBitCount; info->ihead->biCompression = in->biCompression; info->ihead->biSizeImage = in->biWidth*in->biHeight*in->biBitCount/8; // out->biCompression=RIFF_TAG_ther; out->biCompression=RIFF_TAG_theo; info->ohead=gcalloc(sizeof(BGBBTJ_BMPInfoHeader)); memset(info->ohead, 0, sizeof(BGBBTJ_BMPInfoHeader)); info->ohead->biSize = sizeof(BGBBTJ_BMPInfoHeader); info->ohead->biWidth = out->biWidth; // info->ohead->biHeight = -out->biHeight; info->ohead->biHeight = out->biHeight; info->ohead->biPlanes = out->biPlanes; info->ohead->biBitCount = out->biBitCount; info->ohead->biCompression = out->biCompression; info->ihead->biSizeImage = out->biWidth*out->biHeight*out->biBitCount/8; // info->buffer=malloc(out->width*out->height*out->bpp/8); theora_info_init(&(info->ti)); // if(err<0)printf("theo_begin_compress: A Err=%d\n"); xs=(out->biWidth+15)&(~15); ys=(out->biHeight+15)&(~15); xs1=xs>>1; ys1=ys>>1; px=((xs-out->biWidth)>>1)&(~1); py=((ys-out->biHeight)>>1)&(~1); info->ti.width = out->biWidth; info->ti.height = out->biHeight; info->ti.frame_width = xs; info->ti.frame_height = ys; info->ti.offset_x=px; info->ti.offset_y=py; // info->ti.pixel_fmt=TH_PF_420; info->ti.pixelformat=TH_PF_420; // info->ti.quality=56; info->ti.quality=63; info->ti.colorspace=TH_CS_UNSPECIFIED; info->ti.fps_numerator=24; info->ti.fps_denominator=1; info->ti.aspect_numerator=1; info->ti.aspect_denominator=1; th_info_init(&(info->thi)); // if(err<0)printf("theo_begin_compress: B Err=%d\n"); info->thi.pic_width = out->biWidth; info->thi.pic_height = out->biHeight; info->thi.frame_width = xs; info->thi.frame_height = ys; info->thi.pic_x=px; info->thi.pic_y=py; info->thi.fps_numerator=24; info->thi.fps_denominator=1; info->thi.aspect_numerator=1; info->thi.aspect_denominator=1; info->thi.colorspace=TH_CS_UNSPECIFIED; info->thi.pixel_fmt=TH_PF_420; // info->thi.pixelformat=TH_PF_420; // info->thi.quality=48; info->thi.quality=63; if(theora_encode_init(&(info->th), &(info->ti)) != OC_DISABLED) { theora_clear(&(info->th)); } info->td=th_encode_alloc(&(info->thi)); th_info_clear(&(info->thi)); // if(err<0)printf("theo_begin_compress: C Err=%d\n"); th_comment_init(&(info->tc)); // if(err<0)printf("theo_begin_compress: D Err=%d\n"); ct=tbuf; err=th_encode_flushheader(info->td, &(info->tc), &op); if(err<0)printf("theo_begin_compress: E Err=%d\n"); ct[0]=(op.bytes>>8)&255; ct[1]=op.bytes&255; ct+=2; memcpy(ct, op.packet, op.bytes); ct+=op.bytes; while(1) { err=th_encode_flushheader(info->td, &(info->tc), &op); if(err<=0)break; ct[0]=(op.bytes>>8)&255; ct[1]=op.bytes&255; ct+=2; memcpy(ct, op.packet, op.bytes); ct+=op.bytes; } if(err<0)printf("theo_begin_compress: F Err=%d\n"); sz=ct-tbuf; ctx->vidStrd=gcalloc(sz); ctx->sz_vidStrd=sz; memcpy(ctx->vidStrd, tbuf, sz); info->ycbbuf[0].width=xs; info->ycbbuf[0].height=ys; info->ycbbuf[0].stride=xs; info->ycbbuf[0].data=malloc(xs*ys); info->ycbbuf[1].width=xs1; info->ycbbuf[1].height=ys1; info->ycbbuf[1].stride=xs1; info->ycbbuf[1].data=malloc(xs1*ys1); info->ycbbuf[2].width=xs1; info->ycbbuf[2].height=ys1; info->ycbbuf[2].stride=xs1; info->ycbbuf[2].data=malloc(xs1*ys1); ctx->compress_frame=&theo_compress_frame; return(ctx); }
int main( int argc, char* argv[] ){ int i,j; ogg_packet op; SDL_Event event; int hasdatatobuffer = 1; int playbackdone = 0; double now, delay, last_frame_time = 0; int frameNum=0; int skipNum=0; /* takes first argument as file to play */ /* this works better on Windows and is more convenient for drag and drop ogg files over the .exe */ if( argc != 2 ) { usage(); exit(0); } infile = fopen( argv[1], "rb" ); /* start up Ogg stream synchronization layer */ ogg_sync_init(&oy); /* init supporting Vorbis structures needed in header parsing */ vorbis_info_init(&vi); vorbis_comment_init(&vc); /* init supporting Theora structures needed in header parsing */ theora_comment_init(&tc); theora_info_init(&ti); parseHeaders(); /* force audio off */ /* vorbis_p = 0; */ /* initialize decoders */ if(theora_p){ theora_decode_init(&td,&ti); printf("Ogg logical stream %x is Theora %dx%d %.02f fps video\n" " Frame content is %dx%d with offset (%d,%d).\n", to.serialno,ti.width,ti.height, (double)ti.fps_numerator/ti.fps_denominator, ti.frame_width, ti.frame_height, ti.offset_x, ti.offset_y); report_colorspace(&ti); dump_comments(&tc); }else{ /* tear down the partial theora setup */ theora_info_clear(&ti); theora_comment_clear(&tc); } if(vorbis_p){ vorbis_synthesis_init(&vd,&vi); vorbis_block_init(&vd,&vb); printf("Ogg logical stream %x is Vorbis %d channel %d Hz audio.\n", vo.serialno,vi.channels,vi.rate); }else{ /* tear down the partial vorbis setup */ vorbis_info_clear(&vi); vorbis_comment_clear(&vc); } /* open audio */ if(vorbis_p)open_audio(); /* open video */ if(theora_p)open_video(); /* our main loop */ while(!playbackdone){ /* break out on SDL quit event */ if ( SDL_PollEvent ( &event ) ) { if ( event.type == SDL_QUIT ) break ; } /* get some audio data */ while(vorbis_p && !audiobuf_ready){ int ret; float **pcm; int count = 0; int maxBytesToWrite; /* is there pending audio? does it fit our circular buffer without blocking? */ ret=vorbis_synthesis_pcmout(&vd,&pcm); maxBytesToWrite = GetAudioStreamWriteable(aOutStream); if (maxBytesToWrite<=FRAMES_PER_BUFFER){ /* break out until there is a significant amount of data to avoid a series of small write operations. */ break; } /* if there's pending, decoded audio, grab it */ if((ret>0)&&(maxBytesToWrite>0)){ for(i=0;i<ret && i<(maxBytesToWrite/vi.channels);i++) for(j=0;j<vi.channels;j++){ int val=(int)(pcm[j][i]*32767.f); if(val>32767)val=32767; if(val<-32768)val=-32768; samples[count]=val; count++; } if(WriteAudioStream( aOutStream, samples, i )) { if(count==maxBytesToWrite){ audiobuf_ready=1; } } vorbis_synthesis_read(&vd,i); if(vd.granulepos>=0) audiobuf_granulepos=vd.granulepos-ret+i; else audiobuf_granulepos+=i; }else{ /* no pending audio; is there a pending packet to decode? */ if(ogg_stream_packetout(&vo,&op)>0){ if(vorbis_synthesis(&vb,&op)==0) /* test for success! */ vorbis_synthesis_blockin(&vd,&vb); }else /* we need more data; break out to suck in another page */ break; } } /* end audio cycle */ while(theora_p && !videobuf_ready){ /* get one video packet... */ if(ogg_stream_packetout(&to,&op)>0){ theora_decode_packetin(&td,&op); videobuf_granulepos=td.granulepos; videobuf_time=theora_granule_time(&td,videobuf_granulepos); /* update the frame counter */ frameNum++; /* check if this frame time has not passed yet. If the frame is late we need to decode additonal ones and keep looping, since theora at this stage needs to decode all frames */ now=get_time(); delay=videobuf_time-now; if(delay>=0.0){ /* got a good frame, not late, ready to break out */ videobuf_ready=1; }else if(now-last_frame_time>=1.0){ /* display at least one frame per second, regardless */ videobuf_ready=1; }else{ fprintf(stderr, "dropping frame %d (%.3fs behind)\n", frameNum, -delay); } }else{ /* need more data */ break; } } if(!hasdatatobuffer && !videobuf_ready && !audiobuf_ready){ isPlaying = 0; playbackdone = 1; } /* if we're set for the next frame, sleep */ if((!theora_p || videobuf_ready) && (!vorbis_p || audiobuf_ready)){ int ticks = 1.0e3*(videobuf_time-get_time()); if(ticks>0) SDL_Delay(ticks); } if(videobuf_ready){ /* time to write our cached frame */ video_write(); videobuf_ready=0; last_frame_time=get_time(); /* if audio has not started (first frame) then start it */ if ((!isPlaying)&&(vorbis_p)){ start_audio(); isPlaying = 1; } } /* HACK: always look for more audio data */ audiobuf_ready=0; /* buffer compressed data every loop */ if(hasdatatobuffer){ hasdatatobuffer=buffer_data(&oy); if(hasdatatobuffer==0){ printf("Ogg buffering stopped, end of file reached.\n"); } } if (ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); } } /* playbackdone */ /* show number of video frames decoded */ printf( "\n"); printf( "Frames decoded: %d", frameNum ); if(skipNum) printf( " (only %d shown)", frameNum-skipNum); printf( "\n" ); /* tear it all down */ fclose( infile ); if(vorbis_p){ audio_close(); ogg_stream_clear(&vo); vorbis_block_clear(&vb); vorbis_dsp_clear(&vd); vorbis_comment_clear(&vc); vorbis_info_clear(&vi); } if(theora_p){ ogg_stream_clear(&to); theora_clear(&td); theora_comment_clear(&tc); theora_info_clear(&ti); } ogg_sync_clear(&oy); printf("\r " "\nDone.\n"); SDL_Quit(); return(0); }