3 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "bcsignals.h"
23 #include "byteorder.h"
29 #include "interlacemodes.h"
31 #include "mainerror.h"
33 #include "mwindow.inc"
34 #include "preferences.h"
37 #include "versioninfo.h"
38 #include "videodevice.inc"
40 #include <sys/types.h>
47 /* This code was aspired by ffmpeg2theora */
51 #define READ_SIZE 4*66000
52 #define SEEK_SIZE 2*66000
54 sync_window_t::sync_window_t(FILE *fp, Mutex *sync_lock, int64_t begin, int64_t end)
58 this->sync_lock = sync_lock;
59 this->file_begin = begin;
66 sync_window_t::~sync_window_t()
71 int sync_window_t::ogg_read_locked(int buflen)
73 char *buffer = ogg_sync_buffer(this, buflen);
74 int len = fread(buffer, 1, buflen, fp);
75 ogg_sync_wrote(this, len);
80 int sync_window_t::ogg_read_buffer(int buflen)
82 sync_lock->lock("sync_window_t::ogg_read_buffer_at");
83 fseeko(fp, filepos, SEEK_SET);
84 int len = ogg_read_locked(buflen);
89 int sync_window_t::ogg_read_buffer_at(off_t filepos, int buflen)
91 if( bufpos == filepos && buflen == this->filepos - bufpos )
93 sync_lock->lock("sync_window_t::ogg_read_buffer_at");
94 this->bufpos = filepos;
95 fseeko(fp, filepos, SEEK_SET);
96 this->filepos = filepos;
98 int ret = ogg_read_locked(buflen);
103 // we never need to autoadvance when syncing, since our read chunks are larger than
105 int sync_window_t::ogg_sync_and_take_page_out(ogg_page *og)
111 int ret = ogg_sync_pageseek(this, og);
112 bufpos += abs(ret); // can be zero
116 int sync_window_t::ogg_sync_and_get_next_page(long serialno, ogg_page *og)
118 int ret = 0, retries = 1000;
119 while( --retries >= 0 && (ret = ogg_sync_and_take_page_out(og)) < 0 );
120 if( ret >= mn_pagesz && ogg_page_serialno(og) != serialno )
121 ret = ogg_get_next_page(serialno, og);
123 pagpos = bufpos - (og->header_len + og->body_len);
129 int sync_window_t::ogg_get_next_page(long serialno, ogg_page *og)
131 int ret = 0, retries = 1000;
132 while( --retries >= 0 && (ret=ogg_take_page_out_autoadvance(og)) &&
133 ogg_page_serialno(og) != serialno );
135 pagpos = bufpos - (og->header_len + og->body_len);
138 printf("ogg_get_next_page missed\n");
142 int sync_window_t::ogg_prev_page_search(long serialno, ogg_page *og,
143 off_t begin, off_t end)
146 int retries = 100, ret = 0;
148 while( ppos < 0 && --retries >= 0 ) {
150 int read_len = SEEK_SIZE;
153 read_len += fpos - begin;
154 if( read_len <= 0 ) break;
157 read_len = ogg_read_buffer_at(fpos, read_len);
158 if( read_len <= 0 ) return 0;
159 while( (ret=ogg_sync_and_take_page_out(&page)) < 0 );
162 if( ogg_page_serialno(&page) == serialno ) {
163 memcpy(og, &page, sizeof(page));
164 ppos = bufpos - (page.header_len + page.body_len);
166 ret = ogg_sync_pageout(this, &page);
167 bufpos += page.header_len + page.body_len;
174 printf("ogg_prev_page_search missed\n");
178 int sync_window_t::ogg_get_prev_page(long serialno, ogg_page *og)
180 return ogg_prev_page_search(serialno, og, file_begin, pagpos);
183 int sync_window_t::ogg_get_first_page(long serialno, ogg_page *og)
185 ogg_read_buffer_at(file_begin, SEEK_SIZE);
186 return ogg_sync_and_get_next_page(serialno, og);
189 int sync_window_t::ogg_get_last_page(long serialno, ogg_page *og)
193 off_t filepos = file_end - READ_SIZE;
194 if( filepos < 0 ) filepos = 0;
195 int ret = 0, first_page_offset = 0;
196 while( !ret && filepos >= 0 ) {
197 int readlen = ogg_read_buffer_at(filepos, READ_SIZE);
198 int page_offset = 0, page_length = 0;
199 int first_page = 1; // read all pages in the buffer
200 while( first_page || page_length ) {
201 // if negative, skip bytes
202 while( (page_length = ogg_sync_and_take_page_out(&page)) < 0 )
203 page_offset -= page_length;
204 if( page_length < mn_pagesz ) continue;
207 first_page_offset = page_offset;
209 if( ogg_page_serialno(&page) == serialno ) {
210 // return last match page
211 pagpos = bufpos - (page.header_len + page.body_len);
212 memcpy(og, &page, sizeof(page));
216 filepos -= readlen - first_page_offset; // move backward
221 OGG_PageBfr::OGG_PageBfr()
229 OGG_PageBfr::~OGG_PageBfr()
234 void OGG_PageBfr::demand(int sz)
236 if( allocated >= sz ) return;
237 uint8_t *new_page = new uint8_t[sz];
238 memcpy(new_page, page, len);
239 delete [] page; page = new_page;
243 int OGG_PageBfr::write_page(FILE *fp)
245 int sz = fwrite(page, 1, len, fp);
246 if( sz != len ) return -1;
247 ogg_page op; // kludgy
248 op.header = page; op.header_len = len;
249 op.body = page+len; op.body_len = 0;
250 packets -= ogg_page_packets(&op);
255 int64_t OGG_PageBfr::load(ogg_page *og)
257 int sz = og->header_len + og->body_len;
259 memcpy(page, og->header, og->header_len);
260 memcpy(page+og->header_len, og->body, og->body_len);
262 position = ogg_page_granulepos(og);
268 FileOGG::FileOGG(Asset *asset, File *file)
269 : FileBase(asset, file)
271 if( asset->format == FILE_UNKNOWN )
272 asset->format = FILE_OGG;
273 asset->byte_order = 0;
275 file_lock = new Mutex("OGGFile::Flush lock");
312 memset(&to, 0, sizeof(to));
313 memset(&vo, 0, sizeof(vo));
314 ogg_sample_position = 0;
315 ogg_frame_position = 0;
316 next_sample_position = 0;
317 next_frame_position = 0;
324 audio_pos = 0; audio_eos = 0;
325 video_pos = 0; video_eos = 0;
327 keyframe_granule_shift = 0;
328 iframe_granule_offset = 0;
329 theora_cmodel = BC_YUV420P;
332 memset(&ti, 0, sizeof(ti));
334 memset(&tc, 0, sizeof(tc));
335 memset(&vi, 0, sizeof(vi));
336 memset(&vc, 0, sizeof(vc));
337 memset(&vd, 0, sizeof(vd));
338 memset(&vb, 0, sizeof(vb));
345 frame_w = frame_h = 0;
346 colorspace = OC_CS_UNSPECIFIED;
348 bitrate = 0; quality = 0;
351 fps_num = fps_den = 0;
352 aratio_num = aratio_den = 0;
356 static int ilog(unsigned v)
359 while( v ) { ++ret; v >>= 1; }
363 int FileOGG::encode_theora_init()
365 ogg_stream_init(&to, rand());
367 pic_w = asset->width, pic_h = asset->height;
368 frame_w = (pic_w+0x0f) & ~0x0f;
369 frame_h = (pic_h+0x0f) & ~0x0f;
370 pic_x = ((frame_w-pic_w) >> 1) & ~1;
371 pic_y = ((frame_h-pic_h) >> 1) & ~1;
372 fps_num = asset->frame_rate * 1000000;
374 if( asset->aspect_ratio > 0 ) {
375 // Cinelerra uses frame aspect ratio, theora uses pixel aspect ratio
376 float pixel_aspect = asset->aspect_ratio / asset->width * asset->height;
377 aratio_num = pixel_aspect * 1000000;
378 aratio_den = 1000000;
381 aratio_num = 1000000;
382 aratio_den = 1000000;
384 if( EQUIV(asset->frame_rate, 25) || EQUIV(asset->frame_rate, 50) )
385 colorspace = OC_CS_ITU_REC_470BG;
386 else if( (asset->frame_rate > 29 && asset->frame_rate < 31) ||
387 (asset->frame_rate > 59 && asset->frame_rate < 61) )
388 colorspace = OC_CS_ITU_REC_470M;
390 colorspace = OC_CS_UNSPECIFIED;
392 if( asset->theora_fix_bitrate ) {
393 bitrate = asset->theora_bitrate;
398 quality = asset->theora_quality; // 0-63
400 keyframe_period = asset->theora_keyframe_frequency;
401 keyframe_force = asset->theora_keyframe_force_frequency;
405 ti.frame_width = frame_w;
406 ti.frame_height = frame_h;
407 ti.pic_width = pic_w;
408 ti.pic_height = pic_h;
411 ti.colorspace = (th_colorspace)colorspace;
412 ti.pixel_fmt = (th_pixel_fmt)pixfmt;
413 ti.target_bitrate = bitrate;
414 ti.quality = quality;
415 ti.fps_numerator = fps_num;
416 ti.fps_denominator = fps_den;
417 ti.aspect_numerator = aratio_num;
418 ti.aspect_denominator = aratio_den;
419 ti.keyframe_granule_shift = ilog(keyframe_period-1);
421 enc = th_encode_alloc(&ti);
422 int ret = enc ? 0 : 1;
423 if( !ret && force_keyframes )
424 ret = th_encode_ctl(enc,TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
425 &keyframe_period, sizeof(keyframe_period));
426 if( !ret && vp3_compatible )
427 ret = th_encode_ctl(enc,TH_ENCCTL_SET_VP3_COMPATIBLE,
428 &vp3_compatible, sizeof(vp3_compatible));
429 if( !ret && soft_target ) {
430 int arg = TH_RATECTL_CAP_UNDERFLOW;
431 if( th_encode_ctl(enc, TH_ENCCTL_SET_RATE_FLAGS, &arg, sizeof(arg)) < 0 ) {
432 eprintf(_("Could not set rate flags"));
435 int kr = keyframe_period*7>>1, fr = 5*fps_num/fps_den;
436 arg = kr > fr ? kr : fr;
437 if( th_encode_ctl(enc, TH_ENCCTL_SET_RATE_BUFFER, &arg, sizeof(arg)) ) {
438 eprintf(_("Could not set rate buffer"));
443 eprintf(_("theora init context failed"));
447 th_comment_init(&tc);
448 th_comment_add_tag(&tc, (char*)"ENCODER",
449 (char*)PROGRAM_NAME " " CINELERRA_VERSION);
452 ret = th_encode_flushheader(enc, &tc, &op);
453 if( ret <= 0 ) return 1;
454 ogg_stream_packetin(&to, &op);
455 ret = ogg_stream_pageout(&to, &og) != 1 ? 1 : 0;
457 fwrite(og.header, 1, og.header_len, out);
458 fwrite(og.body, 1, og.body_len, out);
461 eprintf(_("write header out failed"));
464 while( (ret=th_encode_flushheader(enc, &tc, &op)) > 0 )
465 ogg_stream_packetin(&to, &op);
467 eprintf(_("ogg_encoder_init video failed"));
473 int FileOGG::encode_vorbis_init()
475 ach = asset->channels;
476 ahz = asset->sample_rate;
477 amx = asset->vorbis_max_bitrate;
478 amn = asset->vorbis_min_bitrate;
479 abr = asset->vorbis_bitrate;
480 avbr = asset->vorbis_vbr;
484 ogg_stream_init(&vo, rand());
485 vorbis_info_init(&vi);
488 ret = vorbis_encode_setup_managed(&vi, ach, ahz, -1, abr, -1);
490 ret = vorbis_encode_ctl(&vi, OV_ECTL_RATEMANAGE_AVG, 0);
492 ret = vorbis_encode_setup_init(&vi);
495 ret = vorbis_encode_init(&vi, ach, ahz, amx, abr, amn);
497 eprintf(_("ogg_encoder_init audio init failed"));
500 vorbis_comment_init(&vc);
501 vorbis_comment_add_tag(&vc, (char*)"ENCODER",
502 (char*)PROGRAM_NAME " " CINELERRA_VERSION);
503 vorbis_analysis_init(&vd, &vi);
504 vorbis_block_init(&vd, &vb);
506 ogg_packet header_comm;
507 ogg_packet header_code;
508 vorbis_analysis_headerout(&vd, &vc,
509 &header, &header_comm, &header_code);
510 ogg_stream_packetin(&vo, &header);
512 ret = ogg_stream_pageout(&vo, &og)==1 ? 0 : -1;
514 fwrite(og.header, 1, og.header_len, out);
515 fwrite(og.body, 1, og.body_len, out);
516 ogg_stream_packetin(&vo, &header_comm);
517 ogg_stream_packetin(&vo, &header_code);
520 eprintf(_("ogg_encoder_init audio failed"));
526 int FileOGG::ogg_init_encode(FILE *out)
530 video = asset->video_data;
531 if( video && encode_theora_init() )
533 audio = asset->audio_data;
534 if( audio && encode_vorbis_init() )
538 if( !ret && video ) {
539 while( (ret=ogg_stream_flush(&to, &og)) > 0 ) {
540 fwrite(og.header, 1, og.header_len, out);
541 fwrite(og.body, 1, og.body_len, out);
544 if( !ret && audio ) {
545 while( (ret=ogg_stream_flush(&vo, &og)) > 0 ) {
546 fwrite(og.header, 1, og.header_len, out);
547 fwrite(og.body, 1, og.body_len, out);
551 eprintf(_("render init failed"));
557 int FileOGG::decode_theora_init()
559 dec = th_decode_alloc(&ti, ts);
561 eprintf(_("Error in probe data"));
564 keyframe_granule_shift = ti.keyframe_granule_shift;
565 iframe_granule_offset = th_granule_frame(dec, 0);
566 double fps = (double)ti.fps_numerator/ti.fps_denominator;
568 videosync = new sync_window_t(inp, file_lock, file_begin, file_end);
570 int ret = videosync->ogg_get_first_page(to.serialno, &og);
572 eprintf(_("cannot read video page from file"));
575 videosync->file_begin = videosync->pagpos;
576 ret = videosync->ogg_get_first_page(to.serialno, &og);
577 // video data starts here
578 // get to the page of the finish of the first packet
579 while( ret > 0 && !ogg_page_packets(&og) ) {
580 if( ogg_page_granulepos(&og) != -1 ) {
581 printf(_("FileOGG: Broken ogg file - broken page:"
582 " ogg_page_packets == 0 and granulepos != -1\n"));
585 ret = videosync->ogg_get_next_page(to.serialno, &og);
587 // video frames start here
588 start_frame = ogg_frame_pos(&og);
589 ret = videosync->ogg_get_first_page(to.serialno, &og);
591 printf(_("FileOGG: Cannot read data past header\n"));
594 //printf("start frame = %jd, gpos %jd, begins %jd\n",
595 // start_frame, ogg_page_granulepos(&og), videosync->file_begin);
597 ret = videosync->ogg_get_last_page(to.serialno, &og);
598 while( ret > 0 && !ogg_page_packets(&og) )
599 ret = videosync->ogg_get_prev_page(to.serialno, &og);
601 last_frame = ogg_next_frame_pos(&og);
602 if( start_frame >= last_frame ) {
603 eprintf(_("no video frames in file"));
604 last_frame = start_frame = 0;
606 asset->video_length = last_frame - start_frame;
609 printf("FileOGG: Cannot find the video length\n");
613 asset->width = ti.pic_width;
614 asset->height = ti.pic_height;
615 // Don't want a user configured frame rate to get destroyed
616 if( !asset->frame_rate )
617 asset->frame_rate = fps;
618 // All theora material is noninterlaced by definition
619 if( !asset->interlace_mode )
620 asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
622 set_video_position(0); // make sure seeking is done to the first sample
623 ogg_frame_position = -10;
624 asset->video_data = 1;
625 strncpy(asset->vcodec, "theo", 4);
626 // report_colorspace(&ti);
627 // dump_comments(&tc);
631 int FileOGG::decode_vorbis_init()
633 ogg_stream_reset(&vo);
634 vorbis_synthesis_init(&vd, &vi);
635 vorbis_block_init(&vd, &vb);
636 audiosync = new sync_window_t(inp, file_lock, file_begin, file_end);
638 int ret = audiosync->ogg_get_first_page(vo.serialno, &og);
640 eprintf(_("cannot read audio page from file"));
643 // audio data starts here
644 audiosync->file_begin = audiosync->pagpos;
645 // audio samples starts here
646 start_sample = ogg_sample_pos(&og);
647 //printf("start sample = %jd, gpos %jd, begins %jd\n",
648 // start_sample, ogg_page_granulepos(&og), audiosync->file_begin);
649 ret = audiosync->ogg_get_last_page(vo.serialno, &og);
650 last_sample = ret > 0 ? ogg_next_sample_pos(&og) : 0;
651 asset->audio_length = last_sample - start_sample;
652 if( asset->audio_length <= 0 ) {
653 eprintf(_("no audio samples in file"));
654 asset->audio_length = 0;
655 last_sample = start_sample;
658 asset->channels = vi.channels;
659 if( !asset->sample_rate )
660 asset->sample_rate = vi.rate;
661 asset->audio_data = 1;
663 ogg_sample_position = -10;
664 set_audio_position(0); // make sure seeking is done to the first sample
665 strncpy(asset->acodec, "vorb", 4);
669 int FileOGG::ogg_init_decode(FILE *inp)
673 struct stat file_stat; /* get file length */
674 file_end = stat(asset->path, &file_stat)>=0 ? file_stat.st_size : 0;
675 if( file_end < mn_pagesz ) return 1;
676 fseek(inp, 0, SEEK_SET);
677 vorbis_info_init(&vi);
678 vorbis_comment_init(&vc);
679 th_comment_init(&tc);
683 sync_window_t sy(inp, file_lock, 0, file_end);
684 int ret = sy.ogg_read_buffer_at(0, READ_SIZE);
685 if( ret < mn_pagesz ) return 1;
686 if( !sy.ogg_sync_and_take_page_out(&og) ) return 1;
687 ogg_stream_state tst;
689 while( ogg_page_bos(&og) ) {
690 ogg_stream_init(&tst, ogg_page_serialno(&og));
691 ogg_stream_pagein(&tst, &og);
692 if( ogg_stream_packetout(&tst, &op) ) {
693 if( !video && th_decode_headerin(&ti, &tc, &ts, &op) >=0 ) {
694 ogg_stream_init(&to, ogg_page_serialno(&og));
697 else if( !audio && vorbis_synthesis_headerin(&vi, &vc, &op) >=0 ) {
698 ogg_stream_init(&vo, ogg_page_serialno(&og));
702 ogg_stream_clear(&tst);
703 ret = sy.ogg_take_page_out_autoadvance(&og);
706 if( !ret || !video && !audio )
709 // expecting more a/v header packets
710 int vpkts = video ? 2 : 0;
711 int apkts = audio ? 2 : 0;
714 while( --retries >= 0 && !ret && (vpkts || apkts) ) {
715 if( vpkts && ogg_page_serialno(&og) == to.serialno ) {
716 ogg_stream_init(&tst, to.serialno);
717 ogg_stream_pagein(&tst, &og);
718 while( !ret && vpkts > 0 ) {
719 while( (ret=ogg_stream_packetout(&tst, &op)) < 0 );
722 ret = !th_decode_headerin(&ti, &tc, &ts, &op) ? 1 : 0;
725 printf("theora header error\n");
726 ogg_stream_clear(&tst);
728 else if( apkts && ogg_page_serialno(&og) == vo.serialno ) {
729 ogg_stream_init(&tst, vo.serialno);
730 ogg_stream_pagein(&tst, &og);
731 while( !ret && apkts > 0 ) {
732 while( (ret=ogg_stream_packetout(&tst, &op)) < 0 );
735 ret = vorbis_synthesis_headerin(&vi, &vc, &op) ? 1 : 0;
738 printf("vorbis header error\n");
739 ogg_stream_clear(&tst);
741 if( !ret && !sy.ogg_take_page_out_autoadvance(&og) )
744 printf("incomplete headers\n");
747 // find first start packet (not continued) with data
748 int64_t start_pos = sy.bufpos - (og.header_len + og.body_len);
750 while( --retries >= 0 && !ret && !ogg_page_packets(&og) ) {
751 if( !ogg_page_continued(&og) )
752 start_pos = sy.bufpos - (og.header_len + og.body_len);
753 if( !sy.ogg_take_page_out_autoadvance(&og) ) ret = 1;
756 printf("no data past headers\n");
758 printf("missed %d audio headers\n",apkts);
760 printf("missed %d video headers\n",vpkts);
762 if( retries < 0 || ret || (audio && apkts) || (video && vpkts) ) {
763 eprintf(_("Error in headers"));
767 file_begin = start_pos;
769 if( video && decode_theora_init() )
771 if( audio && decode_vorbis_init() )
776 void FileOGG::close_encoder()
780 write_samples_vorbis(0, 0, 1);
782 write_frames_theora(0, 1, 1);
786 vorbis_block_clear(&vb);
787 vorbis_dsp_clear(&vd);
788 vorbis_comment_clear(&vc);
789 vorbis_info_clear(&vi);
790 ogg_stream_clear(&vo);
794 th_comment_clear(&tc);
795 ogg_stream_clear(&to);
808 void FileOGG::close_decoder()
811 for( int i=0; i<pcm_channels; ++i )
812 delete [] pcm_history[i];
814 delete [] pcm_history; pcm_history = 0;
816 vorbis_dsp_clear(&vd);
817 vorbis_info_clear(&vi);
818 vorbis_block_clear(&vb);
819 vorbis_comment_clear(&vc);
820 ogg_stream_clear(&vo);
821 delete audiosync; audiosync = 0;
826 th_setup_free(ts); ts = 0;
827 th_comment_clear(&tc);
828 ogg_stream_clear(&to);
829 delete videosync; videosync = 0;
844 void FileOGG::get_parameters(BC_WindowBase *parent_window, Asset *asset,
845 BC_WindowBase* &format_window, int audio_options,
846 int video_options, EDL *edl)
850 OGGConfigAudio *window = new OGGConfigAudio(parent_window, asset);
851 format_window = window;
852 window->create_objects();
853 window->run_window();
859 OGGConfigVideo *window = new OGGConfigVideo(parent_window, asset);
860 format_window = window;
861 window->create_objects();
862 window->run_window();
869 int sync_window_t::ogg_take_page_out_autoadvance(ogg_page *og)
872 int ret = ogg_sync_pageout(this, og);
874 printf("FileOGG: Lost sync reading input file\n");
878 bufpos += og->header_len + og->body_len;
881 // need more data for page
882 if( !ogg_read_buffer(READ_SIZE) ) {
883 printf("FileOGG: Read past end of input file\n");
884 return 0; // No more data
891 int FileOGG::check_sig(Asset *asset)
893 FILE *fp = fopen(asset->path, "rb");
896 fseek(fp, 0, SEEK_SET);
898 int ret = fread(data, 4, 1, fp) == 1 &&
899 data[0] == 'O' && data[1] == 'g' &&
900 data[2] == 'g' && data[3] == 'S' ? 1 : 0;
906 int FileOGG::open_file(int rd, int wr)
910 if( !(out = fopen(asset->path, "wb")) ) {
911 eprintf(_("Error while opening %s for writing. %m\n"), asset->path);
914 if( (ret = ogg_init_encode(out)) && out ) {
915 fclose(out); out = 0;
919 if( !(inp = fopen(asset->path, "rb")) ) {
920 eprintf(_("Error while opening %s for reading. %m\n"), asset->path);
923 if( (ret = ogg_init_decode(inp)) && inp ) {
924 fclose(inp); inp = 0;
930 int FileOGG::close_file()
940 int64_t FileOGG::ogg_sample_pos(ogg_page *og)
944 ogg_stream_init(&ss, vo.serialno);
945 ogg_stream_pagein(&ss, og);
949 while( (ret=ogg_stream_packetout(&ss, &op)) ) {
950 if( ret < 0 ) continue; // ignore holes
951 long sz = vorbis_packet_blocksize(&vi, &op);
952 if( prev != -1 ) bsz += (prev + sz) >> 2;
955 ogg_stream_clear(&ss);
956 return ogg_next_sample_pos(og) - bsz;
959 int64_t FileOGG::ogg_next_sample_pos(ogg_page *og)
961 return ogg_page_granulepos(og);
964 int64_t FileOGG::ogg_frame_pos(ogg_page *og)
966 int64_t pos = th_granule_frame(dec, ogg_page_granulepos(og)) - ogg_page_packets(og);
967 if( ogg_page_continued(og) ) --pos;
971 int64_t FileOGG::ogg_next_frame_pos(ogg_page *og)
973 return th_granule_frame(dec, ogg_page_granulepos(og)) + 1;
977 int FileOGG::ogg_get_page_of_sample(ogg_page *og, int64_t sample)
979 if( sample >= asset->audio_length + start_sample ) {
980 printf(_("FileOGG: Illegal seek beyond end of samples\n"));
983 // guess about position
984 int64_t file_length = audiosync->file_end - audiosync->file_begin;
985 off_t guess = file_length * (sample - start_sample) /
986 asset->audio_length - SEEK_SIZE;
987 if( guess < 0 ) guess = 0;
988 guess += audiosync->file_begin;
989 audiosync->ogg_read_buffer_at(guess, READ_SIZE);
990 if( !audiosync->ogg_sync_and_get_next_page(vo.serialno, og) ) {
991 printf(_("FileOGG: llegal seek no pages\n"));
995 while( ret && (ogg_page_granulepos(og) == -1 || !ogg_page_packets(og)) )
996 ret = videosync->ogg_get_next_page(to.serialno, og);
998 // linear seek to the sample
999 int missp = 0, missm = 0;
1000 int64_t next_pos = ogg_next_sample_pos(og);
1001 if( sample >= next_pos ) { // scan forward
1002 while( sample >= next_pos ) {
1003 while( !(ret=audiosync->ogg_get_next_page(vo.serialno, og)) &&
1004 (ogg_page_granulepos(og) == -1 || !ogg_page_packets(og)) );
1006 next_pos = ogg_next_sample_pos(og);
1008 //printf("audio %jd next %jd %jd\n", sample, ogg_sample_pos(og), next_pos);
1011 else { // scan backward
1012 int64_t pos = ogg_sample_pos(og);
1013 while( sample < pos ) {
1014 while( (ret=audiosync->ogg_get_prev_page(vo.serialno, og)) &&
1015 (ogg_page_continued(og) && ogg_page_packets(og) == 1) );
1018 pos = ogg_sample_pos(og);
1019 //printf("audio %jd prev %jd %jd\n", sample, pos, ogg_next_sample_pos(og));
1022 //printf("audio %d seek %jd, missp %d, missm %d from %jd to %jd\n", ret,
1023 // sample, missp, missm, ogg_sample_pos(og), ogg_next_sample_pos(og));
1027 int FileOGG::ogg_seek_to_sample(int64_t ogg_sample)
1031 if( !ogg_get_page_of_sample(&og, ogg_sample) ) {
1032 eprintf(_("Seeking to sample's page failed\n"));
1036 int64_t pos = ogg_sample_pos(&og);
1037 int64_t next_pos = pos;
1038 if( ogg_page_continued(&og) ) {
1039 while( (ret=audiosync->ogg_get_prev_page(to.serialno, &og)) &&
1040 (ogg_page_packets(&og) == 0 && ogg_page_continued(&og)) );
1044 ogg_stream_reset(&vo);
1045 ogg_stream_pagein(&vo, &og);
1046 vorbis_synthesis_restart(&vd);
1047 ret = ogg_get_audio_packet(&op);
1049 if( ret && !vorbis_synthesis(&vb, &op) ) {
1050 vorbis_synthesis_blockin(&vd, &vb);
1051 if( vorbis_synthesis_pcmout(&vd, 0) )
1055 eprintf(_("Something wrong while trying to seek\n"));
1059 while( ogg_sample > next_pos ) {
1060 if( !(ret=ogg_get_audio_packet(&op)) ) break;
1061 if( vorbis_synthesis(&vb, &op) ) continue;
1062 vorbis_synthesis_blockin(&vd, &vb);
1064 next_pos += vorbis_synthesis_pcmout(&vd, NULL);
1065 if( next_pos > ogg_sample ) break;
1066 // discard decoded data before current sample
1067 vorbis_synthesis_read(&vd, (next_pos - pos));
1070 audio_pos = next_pos;
1071 vorbis_synthesis_read(&vd, (ogg_sample - pos));
1077 int FileOGG::ogg_get_page_of_frame(ogg_page *og, int64_t frame)
1079 if( frame >= asset->video_length + start_frame ) {
1080 eprintf(_("Illegal seek beyond end of frames\n"));
1083 if( frame < start_frame ) {
1084 eprintf(_("Illegal seek before start of frames\n"));
1087 int64_t file_length = videosync->file_end - videosync->file_begin;
1088 off_t guess = file_length * (frame - start_frame) /
1089 asset->video_length - SEEK_SIZE;
1090 if( guess < 0 ) guess = 0;
1091 guess += videosync->file_begin;
1092 videosync->ogg_read_buffer_at(guess, SEEK_SIZE);
1093 videosync->ogg_sync_and_get_next_page(to.serialno, og);
1094 // find the page with "real" ending
1096 while( ret && (ogg_page_granulepos(og) == -1 || !ogg_page_packets(og)) )
1097 ret = videosync->ogg_get_next_page(to.serialno, og);
1098 int64_t pos = ogg_next_frame_pos(og);
1100 int missp = 0, missm = 0;
1101 // move back if continued
1102 if( frame >= pos ) {
1103 do { // scan forward
1104 while( (ret=videosync->ogg_get_next_page(to.serialno, og)) &&
1105 ogg_page_packets(og) == 0 );
1108 pos = ogg_next_frame_pos(og);
1109 //printf("video %jd next %jd %jd\n", frame, ogg_frame_pos(og), pos);
1110 } while( frame >= pos );
1112 else if( (pos=ogg_frame_pos(og)) > frame ) {
1113 while( pos > start_frame && frame < pos ) { // scan backward
1114 while( (ret=videosync->ogg_get_prev_page(to.serialno, og)) &&
1115 ogg_page_packets(og) == 0 && ogg_page_continued(og) );
1118 pos = ogg_frame_pos(og);
1119 //printf("video %jd next %jd %jd\n", frame, pos, ogg_next_frame_pos(og));
1122 //printf("video %d seek %jd, missp %d, missm %d first %jd, next %jd\n", ret,
1123 // frame, missp, missm, ogg_frame_pos(og), ogg_next_frame_pos(og));
1127 int FileOGG::ogg_seek_to_keyframe(int64_t frame, int64_t *keyframe_number)
1129 //printf("ogg_seek_to_keyframe of === %jd\n", frame);
1132 int64_t ipagpos = -1;
1133 int64_t istart = -1;
1134 int64_t iframe = -1;
1136 int retries = 1000, ret = 1;
1137 while( --retries>=0 && frame>=start_frame ) {
1138 if( !ogg_get_page_of_frame(&og, frame) ) break;
1139 int64_t pos = ogg_frame_pos(&og);
1141 if( ogg_page_continued(&og) ) {
1142 while( (ret=videosync->ogg_get_prev_page(to.serialno, &og)) &&
1143 (ogg_page_packets(&og) == 0 && ogg_page_continued(&og)) );
1145 int64_t pagpos = videosync->pagpos;
1147 ogg_stream_reset(&to);
1148 ogg_stream_pagein(&to, &og);
1150 while( frame >= pos && (ret=ogg_get_video_packet(&op)) ) {
1151 if( th_packet_iskeyframe(&op) == 1 ) {
1155 //printf("keyframe %jd pkts %d\n", pos, pkts);
1157 //printf("packet %jd pkts %d is a %d\n", pos, pkts, th_packet_iskeyframe(&op));
1160 if( ipagpos >= 0 ) break;
1164 printf(_("Seeking to keyframe %jd search failed\n"), frame);
1167 videosync->ogg_read_buffer_at(ipagpos, READ_SIZE);
1168 videosync->ogg_sync_and_get_next_page(to.serialno, &og);
1170 ogg_stream_reset(&to);
1171 ogg_stream_pagein(&to, &og);
1172 video_pos = ogg_next_frame_pos(&og);
1173 // skip prev packets
1174 // int ipkts = iframe - ogg_frame_pos(&og);
1175 //printf("iframe %jd, page %jd, ipkts %d\n", iframe, ogg_page_pageno(&og), ipkts);
1176 while( --ipkts >= 0 )
1177 ogg_get_video_packet(&op);
1178 *keyframe_number = iframe;
1183 int64_t FileOGG::get_video_position()
1186 return next_frame_position - start_frame;
1189 int64_t FileOGG::get_audio_position()
1191 return next_sample_position - start_sample;
1194 int FileOGG::set_video_position(int64_t x)
1197 // printf("SVP: %lli\n", x);
1199 next_frame_position = x + start_frame;
1204 int FileOGG::colormodel_supported(int colormodel)
1208 if (colormodel == BC_YUV420P)
1213 int FileOGG::get_best_colormodel(Asset *asset, int driver)
1219 int FileOGG::set_audio_position(int64_t x)
1221 next_sample_position = x + start_sample;
1226 int FileOGG::ogg_get_video_packet(ogg_packet *op)
1229 while( (ret=ogg_stream_packetout(&to, op)) <= 0 ) {
1230 if( video_eos ) return 0;
1232 if( !videosync->ogg_get_next_page(to.serialno, &og) ) break;
1233 if( ogg_page_granulepos(&og) >= 0 )
1234 video_pos = ogg_next_frame_pos(&og);
1235 ogg_stream_pagein(&to, &og);
1236 video_eos = ogg_page_eos(&og);
1239 printf("FileOGG: Cannot read video packet\n");
1245 int FileOGG::read_frame(VFrame *frame)
1247 if( !inp || !video ) return 1;
1248 // skip is cheaper than seek, do it...
1249 int decode_frames = 0;
1250 int expect_keyframe = 0;
1251 if( ogg_frame_position >= 0 &&
1252 next_frame_position >= ogg_frame_position &&
1253 next_frame_position - ogg_frame_position < 32) {
1254 decode_frames = next_frame_position - ogg_frame_position;
1256 else if( next_frame_position != ogg_frame_position ) {
1257 if( !ogg_seek_to_keyframe(next_frame_position, &ogg_frame_position) ) {
1258 eprintf(_("Error while seeking to frame's keyframe"
1259 " (frame: %jd, keyframe: %jd)\n"),
1260 next_frame_position, ogg_frame_position);
1263 decode_frames = next_frame_position - ogg_frame_position + 1;
1264 --ogg_frame_position;
1265 if( decode_frames <= 0 ) {
1266 eprintf(_("Error while seeking to keyframe,"
1267 " wrong keyframe number (frame: %jd, keyframe: %jd)\n"),
1268 next_frame_position, ogg_frame_position);
1272 expect_keyframe = 1;
1276 while( decode_frames > 0 ) {
1277 if( !ogg_get_video_packet(&op) ) break;
1278 if( expect_keyframe ) {
1279 expect_keyframe = 0;
1280 if( th_packet_iskeyframe(&op) <= 0 )
1281 eprintf(_("FileOGG: Expecting keyframe, but didn't get it\n"));
1283 ogg_int64_t granpos = 0;
1284 if( th_decode_packetin(dec, &op, &granpos) >= 0 )
1286 ++ogg_frame_position;
1289 //if(ret < 0 )printf("ret = %d\n", ret);
1291 th_ycbcr_buffer ycbcr;
1292 ret = th_decode_ycbcr_out(dec, ycbcr);
1294 eprintf(_("th_decode_ycbcr_out failed with code %i\n"), ret);
1295 ret = 0; // not always fatal
1297 uint8_t *yp = ycbcr[0].data;
1298 uint8_t *up = ycbcr[1].data;
1299 uint8_t *vp = ycbcr[2].data;
1300 int yst = ycbcr[0].stride;
1301 int yw = ycbcr[0].width;
1302 int yh = ycbcr[0].height;
1303 VFrame temp_frame(yp, -1, 0, up-yp, vp-yp, yw,yh, BC_YUV420P, yst);
1304 int px = ti.pic_x, py = ti.pic_y;
1305 int pw = ti.pic_width, ph = ti.pic_height;
1306 frame->transfer_from(&temp_frame, -1, px, py, pw, ph);
1309 next_frame_position++;
1314 int FileOGG::ogg_get_audio_packet(ogg_packet *op)
1317 while( (ret=ogg_stream_packetout(&vo, op)) <= 0 ) {
1318 if( audio_eos ) return 0;
1320 if( !audiosync->ogg_get_next_page(vo.serialno, &og) ) break;
1321 if( ogg_page_granulepos(&og) >= 0 )
1322 audio_pos = ogg_next_sample_pos(&og);
1323 ogg_stream_pagein(&vo, &og);
1324 audio_eos = ogg_page_eos(&og);
1327 printf("FileOGG: Cannot read audio packet\n");
1333 int FileOGG::ogg_decode_more_samples()
1336 while( ogg_get_audio_packet(&op) ) {
1337 if( !vorbis_synthesis(&vb, &op) ) {
1338 vorbis_synthesis_blockin(&vd, &vb);
1342 ogg_sample_position = -11;
1343 eprintf(_("Cannot find next page while trying to decode more samples\n"));
1347 int FileOGG::move_history(int from, int to, int len)
1350 for( int i=0; i<asset->channels; ++i )
1351 memmove(pcm_history[i] + to,
1352 pcm_history[i] + from,
1353 sizeof(float) * len);
1355 history_start = history_start + from - to;
1356 if( history_start < 0 ) history_start = 0;
1360 int FileOGG::read_samples(double *buffer, int64_t len)
1362 float **vorbis_buffer;
1365 if( len > HISTORY_MAX ) {
1366 eprintf(_("max samples=%d\n"), HISTORY_MAX);
1370 if( !pcm_history ) {
1371 pcm_history = new float*[asset->channels];
1372 for(int i = 0; i < asset->channels; i++)
1373 pcm_history[i] = new float[HISTORY_MAX];
1374 history_start = -100000000;
1378 int64_t hole_start = -1;
1379 int64_t hole_len = -1;
1380 int64_t hole_absstart = -1;
1381 int64_t hole_fill = 0;
1383 if( history_start < next_sample_position &&
1384 history_start + history_size > next_sample_position &&
1385 history_start + history_size < next_sample_position + len ) {
1387 hole_start = history_start + history_size - next_sample_position;
1388 hole_len = history_size - hole_start;
1389 hole_absstart = next_sample_position + hole_start;
1390 move_history(next_sample_position - history_start, 0, hole_start);
1392 else if( next_sample_position < history_start &&
1393 history_start < next_sample_position + len ) {
1396 hole_len = history_start - next_sample_position;
1397 hole_absstart = next_sample_position;
1399 history_start - next_sample_position,
1400 history_size - history_start + next_sample_position);
1403 else if( next_sample_position >= history_start + history_size ||
1404 next_sample_position + len <= history_start ) {
1407 hole_len = HISTORY_MAX;
1408 hole_absstart = next_sample_position;
1409 history_start = hole_absstart;
1410 history_size = hole_len;
1414 if( hole_start < 0 || hole_len <= 0 || hole_absstart < 0 ) {
1415 eprintf(_("Error in finding read file position\n"));
1419 if( hole_absstart + hole_len > asset->audio_length + start_sample ) {
1420 hole_len = asset->audio_length + start_sample - hole_absstart;
1421 history_size = asset->audio_length + start_sample - history_start;
1424 history_size = HISTORY_MAX;
1427 int64_t samples_read = 0;
1428 if( ogg_sample_position != hole_absstart ) {
1429 ogg_sample_position = hole_absstart;
1430 if( !ogg_seek_to_sample(ogg_sample_position) ) {
1431 eprintf(_("Error while seeking to sample\n"));
1435 // now we have ogg_sample_positon aligned
1436 int64_t samples_to_read = hole_len;
1437 while( samples_read < hole_len ) {
1438 int64_t samples_waiting = vorbis_synthesis_pcmout(&vd, &vorbis_buffer);
1439 int64_t samples_avail = !samples_waiting && audio_eos ?
1440 hole_len - samples_read : // silence after eos
1442 int64_t sample_demand = samples_to_read - samples_read;
1443 int64_t sample_count = MIN(samples_avail, sample_demand);
1444 if( sample_count > 0 ) {
1445 int sz = sample_count*sizeof(float);
1446 if( samples_waiting ) {
1447 for( int i=0; i<asset->channels; ++i ) {
1448 float *input = vorbis_buffer[i];
1449 float *output = pcm_history[i] + hole_start;
1450 memcpy(output, input, sz);
1452 vorbis_synthesis_read(&vd, sample_count);
1455 for( int i=0; i<asset->channels; ++i ) {
1456 float *output = pcm_history[i] + hole_start;
1457 memset(output, 0, sz);
1460 ogg_sample_position += sample_count;
1461 hole_start += sample_count;
1462 samples_read += sample_count;
1463 if( samples_read >= hole_len ) break;
1465 if( samples_read < hole_len && !ogg_decode_more_samples() )
1470 if( next_sample_position < history_start ||
1471 next_sample_position + len > history_start + history_size ) {
1472 printf(_("FileOGG:: History not aligned properly \n"));
1473 printf(_("\tnext_sample_position: %jd, length: %jd\n"), next_sample_position, len);
1474 printf(_("\thistory_start: %jd, length: %jd\n"), history_start, history_size);
1477 float *input = pcm_history[file->current_channel] + next_sample_position - history_start;
1478 for (int i = 0; i < len; i++)
1479 buffer[i] = input[i];
1481 next_sample_position += len;
1486 int FileOGG::write_audio_page()
1488 int ret = apage.write_page(out);
1490 eprintf(_("error writing audio page\n"));
1494 int FileOGG::write_video_page()
1496 int ret = vpage.write_page(out);
1498 eprintf(_("error writing video page\n"));
1502 // flush out the ogg pages
1503 void FileOGG::flush_ogg(int last)
1506 file_lock->lock("FileOGG::flush_ogg");
1508 // this way seeking is much better, (per original fileogg)
1509 // not sure if 32 packets is a good value.
1511 if( video && !vpage.valid ) {
1512 if( (vpage.packets > mx_pkts && ogg_stream_flush(&to, &og) > 0) ||
1513 ogg_stream_pageout(&to, &og) > 0 ) {
1514 videotime = th_granule_time(enc, vpage.load(&og));
1517 if( audio && !apage.valid ) {
1518 if( (apage.packets > mx_pkts && ogg_stream_flush(&vo, &og) > 0) ||
1519 ogg_stream_pageout(&vo, &og) > 0 ) {
1520 audiotime = vorbis_granule_time(&vd, apage.load(&og));
1523 if( !audio && vpage.valid )
1525 else if( !video && apage.valid )
1527 else if( !vpage.valid || !apage.valid )
1529 // output earliest page
1530 else if( videotime > audiotime ) // output earliest
1535 if( last ) { // at last
1541 file_lock->unlock();
1545 int FileOGG::write_samples_vorbis(double **buffer, int64_t len, int last)
1547 if( !audio || !out ) return 1;
1550 float **vorbis_buffer = vorbis_analysis_buffer(&vd, len);
1551 for( int i=0; i<asset->channels; ++i ) // double to float
1552 for( int j=0; j < len; ++j )
1553 vorbis_buffer[i][j] = buffer[i][j];
1557 vorbis_analysis_wrote(&vd, len);
1559 while( vorbis_analysis_blockout(&vd, &vb) == 1 ) {
1560 vorbis_analysis(&vb, 0);
1561 vorbis_bitrate_addblock(&vb);
1563 while( vorbis_bitrate_flushpacket(&vd, &op) ) {
1564 file_lock->lock("FileOGG::write_vorbis_audio");
1565 ogg_stream_packetin(&vo, &op);
1567 file_lock->unlock();
1573 int FileOGG::write_samples(double **buffer, int64_t len)
1576 return write_samples_vorbis(buffer, len, 0);
1581 int FileOGG::write_frames_theora(VFrame ***frames, int len, int last)
1583 if( !video || !out ) return 1;
1584 for( int j=0; j<len; ++j ) {
1587 th_ycbcr_buffer ycbcr;
1588 ycbcr[0].width = frame_w;
1589 ycbcr[0].height = frame_h;
1590 ycbcr[0].stride = temp_frame->get_bytes_per_line();
1591 ycbcr[0].data = temp_frame->get_y();
1592 ycbcr[1].width = frame_w/2;
1593 ycbcr[1].height = frame_h/2;
1594 ycbcr[1].stride = (temp_frame->get_bytes_per_line()+1)/2;
1595 ycbcr[1].data = temp_frame->get_u();
1596 ycbcr[2].width = frame_w/2;
1597 ycbcr[2].height = frame_h/2;
1598 ycbcr[2].stride = (temp_frame->get_bytes_per_line()+1)/2;
1599 ycbcr[2].data = temp_frame->get_v();
1600 if( th_encode_ycbcr_in(enc, ycbcr) ) {
1601 eprintf(_("th_encode_ycbcr_in failed"));
1605 while( th_encode_packetout(enc, last, &op) > 0 ) {
1607 ogg_stream_packetin (&to, &op);
1609 file_lock->unlock();
1612 if( last ) return 0;
1614 temp_frame = new VFrame (0, -1, frame_w, frame_h, theora_cmodel, -1);
1615 VFrame *frame = frames[0][j];
1616 temp_frame->transfer_from(frame);
1621 int FileOGG::write_frames(VFrame ***frames, int len)
1623 return write_frames_theora(frames, len, 0);
1628 OGGConfigAudio::OGGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
1629 : BC_Window(PROGRAM_NAME ": Audio Compression",
1630 parent_window->get_abs_cursor_x(1),
1631 parent_window->get_abs_cursor_y(1),
1634 this->parent_window = parent_window;
1635 this->asset = asset;
1636 // *** CONTEXT_HELP ***
1637 context_help_set_keyword("Single File Rendering");
1640 OGGConfigAudio::~OGGConfigAudio()
1645 void OGGConfigAudio::create_objects()
1647 // add_tool(new BC_Title(10, 10, _("There are no audio options for this format")));
1649 int x = xS(10), y = yS(10);
1651 char string[BCTEXTLEN];
1653 lock_window("OGGConfigAudio::create_objects");
1654 add_tool(fixed_bitrate = new OGGVorbisFixedBitrate(x, y, this));
1655 add_tool(variable_bitrate = new OGGVorbisVariableBitrate(x + fixed_bitrate->get_w() + xS(5),
1660 sprintf(string, "%d", asset->vorbis_min_bitrate);
1661 add_tool(new BC_Title(x, y, _("Min bitrate:")));
1662 add_tool(new OGGVorbisMinBitrate(x1, y, this, string));
1665 add_tool(new BC_Title(x, y, _("Avg bitrate:")));
1666 sprintf(string, "%d", asset->vorbis_bitrate);
1667 add_tool(new OGGVorbisAvgBitrate(x1, y, this, string));
1670 add_tool(new BC_Title(x, y, _("Max bitrate:")));
1671 sprintf(string, "%d", asset->vorbis_max_bitrate);
1672 add_tool(new OGGVorbisMaxBitrate(x1, y, this, string));
1675 add_subwindow(new BC_OKButton(this));
1680 int OGGConfigAudio::close_event()
1686 OGGVorbisFixedBitrate::OGGVorbisFixedBitrate(int x, int y, OGGConfigAudio *gui)
1687 : BC_Radial(x, y, !gui->asset->vorbis_vbr, _("Average bitrate"))
1691 int OGGVorbisFixedBitrate::handle_event()
1693 gui->asset->vorbis_vbr = 0;
1694 gui->variable_bitrate->update(0);
1698 OGGVorbisVariableBitrate::OGGVorbisVariableBitrate(int x, int y, OGGConfigAudio *gui)
1699 : BC_Radial(x, y, gui->asset->vorbis_vbr, _("Variable bitrate"))
1703 int OGGVorbisVariableBitrate::handle_event()
1705 gui->asset->vorbis_vbr = 1;
1706 gui->fixed_bitrate->update(0);
1711 OGGVorbisMinBitrate::OGGVorbisMinBitrate(int x,
1713 OGGConfigAudio *gui,
1715 : BC_TextBox(x, y, xS(180), 1, text)
1719 int OGGVorbisMinBitrate::handle_event()
1721 gui->asset->vorbis_min_bitrate = atol(get_text());
1727 OGGVorbisMaxBitrate::OGGVorbisMaxBitrate(int x,
1729 OGGConfigAudio *gui,
1731 : BC_TextBox(x, y, xS(180), 1, text)
1735 int OGGVorbisMaxBitrate::handle_event()
1737 gui->asset->vorbis_max_bitrate = atol(get_text());
1743 OGGVorbisAvgBitrate::OGGVorbisAvgBitrate(int x, int y, OGGConfigAudio *gui, char *text)
1744 : BC_TextBox(x, y, xS(180), 1, text)
1748 int OGGVorbisAvgBitrate::handle_event()
1750 gui->asset->vorbis_bitrate = atol(get_text());
1758 OGGConfigVideo::OGGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
1759 : BC_Window(PROGRAM_NAME ": Video Compression",
1760 parent_window->get_abs_cursor_x(1),
1761 parent_window->get_abs_cursor_y(1),
1764 this->parent_window = parent_window;
1765 this->asset = asset;
1766 // *** CONTEXT_HELP ***
1767 context_help_set_keyword("Single File Rendering");
1770 OGGConfigVideo::~OGGConfigVideo()
1775 void OGGConfigVideo::create_objects()
1777 // add_tool(new BC_Title(10, 10, _("There are no video options for this format")));
1778 int x = xS(10), y = yS(10);
1779 int x1 = x + xS(150);
1780 int x2 = x + xS(300);
1782 lock_window("OGGConfigVideo::create_objects");
1784 add_subwindow(title = new BC_Title(x, y, _("Bitrate:")));
1785 add_subwindow(new OGGTheoraBitrate(x + title->get_w() + xS(5), y, this));
1786 add_subwindow(fixed_bitrate = new OGGTheoraFixedBitrate(x2, y, this));
1789 add_subwindow(new BC_Title(x, y, _("Quality:")));
1790 add_subwindow(new BC_ISlider(x + xS(80), y, 0, xS(200), xS(200),
1791 0, 63, asset->theora_quality,
1792 0, 0, &asset->theora_quality));
1795 add_subwindow(fixed_quality = new OGGTheoraFixedQuality(x2, y, this));
1798 add_subwindow(new BC_Title(x, y, _("Keyframe frequency:")));
1799 OGGTheoraKeyframeFrequency *keyframe_frequency =
1800 new OGGTheoraKeyframeFrequency(x1 + xS(60), y, this);
1801 keyframe_frequency->create_objects();
1804 add_subwindow(new BC_Title(x, y, _("Keyframe force frequency:")));
1805 OGGTheoraKeyframeForceFrequency *keyframe_force_frequency =
1806 new OGGTheoraKeyframeForceFrequency(x1 + xS(60), y, this);
1807 keyframe_force_frequency->create_objects();
1810 add_subwindow(new BC_Title(x, y, _("Sharpness:")));
1811 OGGTheoraSharpness *sharpness =
1812 new OGGTheoraSharpness(x1 + xS(60), y, this);
1813 sharpness->create_objects();
1817 add_subwindow(new BC_OKButton(this));
1825 int OGGConfigVideo::close_event()
1831 OGGTheoraBitrate::OGGTheoraBitrate(int x, int y, OGGConfigVideo *gui)
1832 : BC_TextBox(x, y, xS(100), 1, gui->asset->theora_bitrate)
1838 int OGGTheoraBitrate::handle_event()
1840 // TODO: MIN / MAX check
1841 gui->asset->theora_bitrate = atol(get_text());
1848 OGGTheoraFixedBitrate::OGGTheoraFixedBitrate(int x, int y, OGGConfigVideo *gui)
1849 : BC_Radial(x, y, gui->asset->theora_fix_bitrate, _("Fixed bitrate"))
1854 int OGGTheoraFixedBitrate::handle_event()
1857 gui->asset->theora_fix_bitrate = 1;
1858 gui->fixed_quality->update(0);
1862 OGGTheoraFixedQuality::OGGTheoraFixedQuality(int x, int y, OGGConfigVideo *gui)
1863 : BC_Radial(x, y, !gui->asset->theora_fix_bitrate, _("Fixed quality"))
1868 int OGGTheoraFixedQuality::handle_event()
1871 gui->asset->theora_fix_bitrate = 0;
1872 gui->fixed_bitrate->update(0);
1876 OGGTheoraKeyframeFrequency::OGGTheoraKeyframeFrequency(int x, int y, OGGConfigVideo *gui)
1877 : BC_TumbleTextBox(gui, (int64_t)gui->asset->theora_keyframe_frequency,
1878 (int64_t)1, (int64_t)500, x, y, xS(40))
1883 int OGGTheoraKeyframeFrequency::handle_event()
1885 gui->asset->theora_keyframe_frequency = atol(get_text());
1889 OGGTheoraKeyframeForceFrequency::OGGTheoraKeyframeForceFrequency(int x, int y, OGGConfigVideo *gui)
1890 : BC_TumbleTextBox(gui, (int64_t)gui->asset->theora_keyframe_frequency,
1891 (int64_t)1, (int64_t)500, x, y, xS(40))
1896 int OGGTheoraKeyframeForceFrequency::handle_event()
1898 gui->asset->theora_keyframe_frequency = atol(get_text());
1903 OGGTheoraSharpness::OGGTheoraSharpness(int x, int y, OGGConfigVideo *gui)
1904 : BC_TumbleTextBox(gui, (int64_t)gui->asset->theora_sharpness,
1905 (int64_t)0, (int64_t)2, x, y, xS(40))
1910 int OGGTheoraSharpness::handle_event()
1912 gui->asset->theora_sharpness = atol(get_text());