3 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
4 * Copyright (C) 2003-2016 Cinelerra CV contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "bcsignals.h"
25 #include "byteorder.h"
31 #include "interlacemodes.h"
33 #include "mainerror.h"
35 #include "mwindow.inc"
36 #include "preferences.h"
39 #include "versioninfo.h"
40 #include "videodevice.inc"
42 #include <sys/types.h>
49 /* This code was aspired by ffmpeg2theora */
53 #define READ_SIZE 4*66000
54 #define SEEK_SIZE 2*66000
56 sync_window_t::sync_window_t(FILE *fp, Mutex *sync_lock, int64_t begin, int64_t end)
60 this->sync_lock = sync_lock;
61 this->file_begin = begin;
68 sync_window_t::~sync_window_t()
73 int sync_window_t::ogg_read_locked(int buflen)
75 char *buffer = ogg_sync_buffer(this, buflen);
76 int len = fread(buffer, 1, buflen, fp);
77 ogg_sync_wrote(this, len);
82 int sync_window_t::ogg_read_buffer(int buflen)
84 sync_lock->lock("sync_window_t::ogg_read_buffer_at");
85 fseeko(fp, filepos, SEEK_SET);
86 int len = ogg_read_locked(buflen);
91 int sync_window_t::ogg_read_buffer_at(off_t filepos, int buflen)
93 if( bufpos == filepos && buflen == this->filepos - bufpos )
95 sync_lock->lock("sync_window_t::ogg_read_buffer_at");
96 this->bufpos = filepos;
97 fseeko(fp, filepos, SEEK_SET);
98 this->filepos = filepos;
100 int ret = ogg_read_locked(buflen);
105 // we never need to autoadvance when syncing, since our read chunks are larger than
107 int sync_window_t::ogg_sync_and_take_page_out(ogg_page *og)
113 int ret = ogg_sync_pageseek(this, og);
114 bufpos += abs(ret); // can be zero
118 int sync_window_t::ogg_sync_and_get_next_page(long serialno, ogg_page *og)
120 int ret = 0, retries = 1000;
121 while( --retries >= 0 && (ret = ogg_sync_and_take_page_out(og)) < 0 );
122 if( ret >= mn_pagesz && ogg_page_serialno(og) != serialno )
123 ret = ogg_get_next_page(serialno, og);
125 pagpos = bufpos - (og->header_len + og->body_len);
131 int sync_window_t::ogg_get_next_page(long serialno, ogg_page *og)
133 int ret = 0, retries = 1000;
134 while( --retries >= 0 && (ret=ogg_take_page_out_autoadvance(og)) &&
135 ogg_page_serialno(og) != serialno );
137 pagpos = bufpos - (og->header_len + og->body_len);
140 printf("ogg_get_next_page missed\n");
144 int sync_window_t::ogg_prev_page_search(long serialno, ogg_page *og,
145 off_t begin, off_t end)
148 int retries = 100, ret = 0;
150 while( ppos < 0 && --retries >= 0 ) {
152 int read_len = SEEK_SIZE;
155 read_len += fpos - begin;
156 if( read_len <= 0 ) break;
159 read_len = ogg_read_buffer_at(fpos, read_len);
160 if( read_len <= 0 ) return 0;
161 while( (ret=ogg_sync_and_take_page_out(&page)) < 0 );
164 if( ogg_page_serialno(&page) == serialno ) {
165 memcpy(og, &page, sizeof(page));
166 ppos = bufpos - (page.header_len + page.body_len);
168 ret = ogg_sync_pageout(this, &page);
169 bufpos += page.header_len + page.body_len;
176 printf("ogg_prev_page_search missed\n");
180 int sync_window_t::ogg_get_prev_page(long serialno, ogg_page *og)
182 return ogg_prev_page_search(serialno, og, file_begin, pagpos);
185 int sync_window_t::ogg_get_first_page(long serialno, ogg_page *og)
187 ogg_read_buffer_at(file_begin, SEEK_SIZE);
188 return ogg_sync_and_get_next_page(serialno, og);
191 int sync_window_t::ogg_get_last_page(long serialno, ogg_page *og)
195 off_t filepos = file_end - READ_SIZE;
196 if( filepos < 0 ) filepos = 0;
197 int ret = 0, first_page_offset = 0;
198 while( !ret && filepos >= 0 ) {
199 int readlen = ogg_read_buffer_at(filepos, READ_SIZE);
200 int page_offset = 0, page_length = 0;
201 int first_page = 1; // read all pages in the buffer
202 while( first_page || page_length ) {
203 // if negative, skip bytes
204 while( (page_length = ogg_sync_and_take_page_out(&page)) < 0 )
205 page_offset -= page_length;
206 if( page_length < mn_pagesz ) continue;
209 first_page_offset = page_offset;
211 if( ogg_page_serialno(&page) == serialno ) {
212 // return last match page
213 pagpos = bufpos - (page.header_len + page.body_len);
214 memcpy(og, &page, sizeof(page));
218 filepos -= readlen - first_page_offset; // move backward
223 OGG_PageBfr::OGG_PageBfr()
231 OGG_PageBfr::~OGG_PageBfr()
236 void OGG_PageBfr::demand(int sz)
238 if( allocated >= sz ) return;
239 uint8_t *new_page = new uint8_t[sz];
240 memcpy(new_page, page, len);
241 delete [] page; page = new_page;
245 int OGG_PageBfr::write_page(FILE *fp)
247 int sz = fwrite(page, 1, len, fp);
248 if( sz != len ) return -1;
249 ogg_page op; // kludgy
250 op.header = page; op.header_len = len;
251 op.body = page+len; op.body_len = 0;
252 packets -= ogg_page_packets(&op);
257 int64_t OGG_PageBfr::load(ogg_page *og)
259 int sz = og->header_len + og->body_len;
261 memcpy(page, og->header, og->header_len);
262 memcpy(page+og->header_len, og->body, og->body_len);
264 position = ogg_page_granulepos(og);
270 FileOGG::FileOGG(Asset *asset, File *file)
271 : FileBase(asset, file)
273 if( asset->format == FILE_UNKNOWN )
274 asset->format = FILE_OGG;
275 asset->byte_order = 0;
277 file_lock = new Mutex("OGGFile::Flush lock");
314 memset(&to, 0, sizeof(to));
315 memset(&vo, 0, sizeof(vo));
316 ogg_sample_position = 0;
317 ogg_frame_position = 0;
318 next_sample_position = 0;
319 next_frame_position = 0;
326 audio_pos = 0; audio_eos = 0;
327 video_pos = 0; video_eos = 0;
329 keyframe_granule_shift = 0;
330 iframe_granule_offset = 0;
331 theora_cmodel = BC_YUV420P;
334 memset(&ti, 0, sizeof(ti));
336 memset(&tc, 0, sizeof(tc));
337 memset(&vi, 0, sizeof(vi));
338 memset(&vc, 0, sizeof(vc));
339 memset(&vd, 0, sizeof(vd));
340 memset(&vb, 0, sizeof(vb));
347 frame_w = frame_h = 0;
348 colorspace = OC_CS_UNSPECIFIED;
350 bitrate = 0; quality = 0;
353 fps_num = fps_den = 0;
354 aratio_num = aratio_den = 0;
358 static int ilog(unsigned v)
361 while( v ) { ++ret; v >>= 1; }
365 int FileOGG::encode_theora_init()
367 ogg_stream_init(&to, rand());
369 pic_w = asset->width, pic_h = asset->height;
370 frame_w = (pic_w+0x0f) & ~0x0f;
371 frame_h = (pic_h+0x0f) & ~0x0f;
372 pic_x = ((frame_w-pic_w) >> 1) & ~1;
373 pic_y = ((frame_h-pic_h) >> 1) & ~1;
374 fps_num = asset->frame_rate * 1000000;
376 if( asset->aspect_ratio > 0 ) {
377 // Cinelerra uses frame aspect ratio, theora uses pixel aspect ratio
378 float pixel_aspect = asset->aspect_ratio / asset->width * asset->height;
379 aratio_num = pixel_aspect * 1000000;
380 aratio_den = 1000000;
383 aratio_num = 1000000;
384 aratio_den = 1000000;
386 if( EQUIV(asset->frame_rate, 25) || EQUIV(asset->frame_rate, 50) )
387 colorspace = OC_CS_ITU_REC_470BG;
388 else if( (asset->frame_rate > 29 && asset->frame_rate < 31) ||
389 (asset->frame_rate > 59 && asset->frame_rate < 61) )
390 colorspace = OC_CS_ITU_REC_470M;
392 colorspace = OC_CS_UNSPECIFIED;
394 if( asset->theora_fix_bitrate ) {
395 bitrate = asset->theora_bitrate;
400 quality = asset->theora_quality; // 0-63
402 keyframe_period = asset->theora_keyframe_frequency;
403 keyframe_force = asset->theora_keyframe_force_frequency;
407 ti.frame_width = frame_w;
408 ti.frame_height = frame_h;
409 ti.pic_width = pic_w;
410 ti.pic_height = pic_h;
413 ti.colorspace = (th_colorspace)colorspace;
414 ti.pixel_fmt = (th_pixel_fmt)pixfmt;
415 ti.target_bitrate = bitrate;
416 ti.quality = quality;
417 ti.fps_numerator = fps_num;
418 ti.fps_denominator = fps_den;
419 ti.aspect_numerator = aratio_num;
420 ti.aspect_denominator = aratio_den;
421 ti.keyframe_granule_shift = ilog(keyframe_period-1);
423 enc = th_encode_alloc(&ti);
424 int ret = enc ? 0 : 1;
425 if( !ret && force_keyframes )
426 ret = th_encode_ctl(enc,TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
427 &keyframe_period, sizeof(keyframe_period));
428 if( !ret && vp3_compatible )
429 ret = th_encode_ctl(enc,TH_ENCCTL_SET_VP3_COMPATIBLE,
430 &vp3_compatible, sizeof(vp3_compatible));
431 if( !ret && soft_target ) {
432 int arg = TH_RATECTL_CAP_UNDERFLOW;
433 if( th_encode_ctl(enc, TH_ENCCTL_SET_RATE_FLAGS, &arg, sizeof(arg)) < 0 ) {
434 eprintf(_("Could not set rate flags"));
437 int kr = keyframe_period*7>>1, fr = 5*fps_num/fps_den;
438 arg = kr > fr ? kr : fr;
439 if( th_encode_ctl(enc, TH_ENCCTL_SET_RATE_BUFFER, &arg, sizeof(arg)) ) {
440 eprintf(_("Could not set rate buffer"));
445 eprintf(_("theora init context failed"));
449 th_comment_init(&tc);
450 th_comment_add_tag(&tc, (char*)"ENCODER",
451 (char*)PROGRAM_NAME " " CINELERRA_VERSION);
454 ret = th_encode_flushheader(enc, &tc, &op);
455 if( ret <= 0 ) return 1;
456 ogg_stream_packetin(&to, &op);
457 ret = ogg_stream_pageout(&to, &og) != 1 ? 1 : 0;
459 fwrite(og.header, 1, og.header_len, out);
460 fwrite(og.body, 1, og.body_len, out);
463 eprintf(_("write header out failed"));
466 while( (ret=th_encode_flushheader(enc, &tc, &op)) > 0 )
467 ogg_stream_packetin(&to, &op);
469 eprintf(_("ogg_encoder_init video failed"));
475 int FileOGG::encode_vorbis_init()
477 ach = asset->channels;
478 ahz = asset->sample_rate;
479 amx = asset->vorbis_max_bitrate;
480 amn = asset->vorbis_min_bitrate;
481 abr = asset->vorbis_bitrate;
482 avbr = asset->vorbis_vbr;
486 ogg_stream_init(&vo, rand());
487 vorbis_info_init(&vi);
490 ret = vorbis_encode_setup_managed(&vi, ach, ahz, -1, abr, -1);
492 ret = vorbis_encode_ctl(&vi, OV_ECTL_RATEMANAGE_AVG, 0);
494 ret = vorbis_encode_setup_init(&vi);
497 ret = vorbis_encode_init(&vi, ach, ahz, amx, abr, amn);
499 eprintf(_("ogg_encoder_init audio init failed"));
502 vorbis_comment_init(&vc);
503 vorbis_comment_add_tag(&vc, (char*)"ENCODER",
504 (char*)PROGRAM_NAME " " CINELERRA_VERSION);
505 vorbis_analysis_init(&vd, &vi);
506 vorbis_block_init(&vd, &vb);
508 ogg_packet header_comm;
509 ogg_packet header_code;
510 vorbis_analysis_headerout(&vd, &vc,
511 &header, &header_comm, &header_code);
512 ogg_stream_packetin(&vo, &header);
514 ret = ogg_stream_pageout(&vo, &og)==1 ? 0 : -1;
516 fwrite(og.header, 1, og.header_len, out);
517 fwrite(og.body, 1, og.body_len, out);
518 ogg_stream_packetin(&vo, &header_comm);
519 ogg_stream_packetin(&vo, &header_code);
522 eprintf(_("ogg_encoder_init audio failed"));
528 int FileOGG::ogg_init_encode(FILE *out)
532 video = asset->video_data;
533 if( video && encode_theora_init() )
535 audio = asset->audio_data;
536 if( audio && encode_vorbis_init() )
540 if( !ret && video ) {
541 while( (ret=ogg_stream_flush(&to, &og)) > 0 ) {
542 fwrite(og.header, 1, og.header_len, out);
543 fwrite(og.body, 1, og.body_len, out);
546 if( !ret && audio ) {
547 while( (ret=ogg_stream_flush(&vo, &og)) > 0 ) {
548 fwrite(og.header, 1, og.header_len, out);
549 fwrite(og.body, 1, og.body_len, out);
553 eprintf(_("render init failed"));
559 int FileOGG::decode_theora_init()
561 dec = th_decode_alloc(&ti, ts);
563 eprintf(_("Error in probe data"));
566 keyframe_granule_shift = ti.keyframe_granule_shift;
567 iframe_granule_offset = th_granule_frame(dec, 0);
568 double fps = (double)ti.fps_numerator/ti.fps_denominator;
570 videosync = new sync_window_t(inp, file_lock, file_begin, file_end);
572 int ret = videosync->ogg_get_first_page(to.serialno, &og);
574 eprintf(_("cannot read video page from file"));
577 videosync->file_begin = videosync->pagpos;
578 ret = videosync->ogg_get_first_page(to.serialno, &og);
579 // video data starts here
580 // get to the page of the finish of the first packet
581 while( ret > 0 && !ogg_page_packets(&og) ) {
582 if( ogg_page_granulepos(&og) != -1 ) {
583 printf(_("FileOGG: Broken ogg file - broken page:"
584 " ogg_page_packets == 0 and granulepos != -1\n"));
587 ret = videosync->ogg_get_next_page(to.serialno, &og);
589 // video frames start here
590 start_frame = ogg_frame_pos(&og);
591 ret = videosync->ogg_get_first_page(to.serialno, &og);
593 printf(_("FileOGG: Cannot read data past header\n"));
596 //printf("start frame = %jd, gpos %jd, begins %jd\n",
597 // start_frame, ogg_page_granulepos(&og), videosync->file_begin);
599 ret = videosync->ogg_get_last_page(to.serialno, &og);
600 while( ret > 0 && !ogg_page_packets(&og) )
601 ret = videosync->ogg_get_prev_page(to.serialno, &og);
603 last_frame = ogg_next_frame_pos(&og);
604 if( start_frame >= last_frame ) {
605 eprintf(_("no video frames in file"));
606 last_frame = start_frame = 0;
608 asset->video_length = last_frame - start_frame;
611 printf("FileOGG: Cannot find the video length\n");
615 asset->width = ti.pic_width;
616 asset->height = ti.pic_height;
617 // Don't want a user configured frame rate to get destroyed
618 if( !asset->frame_rate )
619 asset->frame_rate = fps;
620 // All theora material is noninterlaced by definition
621 if( !asset->interlace_mode )
622 asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
624 set_video_position(0); // make sure seeking is done to the first sample
625 ogg_frame_position = -10;
626 asset->video_data = 1;
627 strncpy(asset->vcodec, "theo", 4);
628 // report_colorspace(&ti);
629 // dump_comments(&tc);
633 int FileOGG::decode_vorbis_init()
635 ogg_stream_reset(&vo);
636 vorbis_synthesis_init(&vd, &vi);
637 vorbis_block_init(&vd, &vb);
638 audiosync = new sync_window_t(inp, file_lock, file_begin, file_end);
640 int ret = audiosync->ogg_get_first_page(vo.serialno, &og);
642 eprintf(_("cannot read audio page from file"));
645 // audio data starts here
646 audiosync->file_begin = audiosync->pagpos;
647 // audio samples starts here
648 start_sample = ogg_sample_pos(&og);
649 //printf("start sample = %jd, gpos %jd, begins %jd\n",
650 // start_sample, ogg_page_granulepos(&og), audiosync->file_begin);
651 ret = audiosync->ogg_get_last_page(vo.serialno, &og);
652 last_sample = ret > 0 ? ogg_next_sample_pos(&og) : 0;
653 asset->audio_length = last_sample - start_sample;
654 if( asset->audio_length <= 0 ) {
655 eprintf(_("no audio samples in file"));
656 asset->audio_length = 0;
657 last_sample = start_sample;
660 asset->channels = vi.channels;
661 if( !asset->sample_rate )
662 asset->sample_rate = vi.rate;
663 asset->audio_data = 1;
665 ogg_sample_position = -10;
666 set_audio_position(0); // make sure seeking is done to the first sample
667 strncpy(asset->acodec, "vorb", 4);
671 int FileOGG::ogg_init_decode(FILE *inp)
675 struct stat file_stat; /* get file length */
676 file_end = stat(asset->path, &file_stat)>=0 ? file_stat.st_size : 0;
677 if( file_end < mn_pagesz ) return 1;
678 fseek(inp, 0, SEEK_SET);
679 vorbis_info_init(&vi);
680 vorbis_comment_init(&vc);
681 th_comment_init(&tc);
685 sync_window_t sy(inp, file_lock, 0, file_end);
686 int ret = sy.ogg_read_buffer_at(0, READ_SIZE);
687 if( ret < mn_pagesz ) return 1;
688 if( !sy.ogg_sync_and_take_page_out(&og) ) return 1;
689 ogg_stream_state tst;
691 while( ogg_page_bos(&og) ) {
692 ogg_stream_init(&tst, ogg_page_serialno(&og));
693 ogg_stream_pagein(&tst, &og);
694 if( ogg_stream_packetout(&tst, &op) ) {
695 if( !video && th_decode_headerin(&ti, &tc, &ts, &op) >=0 ) {
696 ogg_stream_init(&to, ogg_page_serialno(&og));
699 else if( !audio && vorbis_synthesis_headerin(&vi, &vc, &op) >=0 ) {
700 ogg_stream_init(&vo, ogg_page_serialno(&og));
704 ogg_stream_clear(&tst);
705 ret = sy.ogg_take_page_out_autoadvance(&og);
708 if( !ret || !video && !audio )
711 // expecting more a/v header packets
712 int vpkts = video ? 2 : 0;
713 int apkts = audio ? 2 : 0;
716 while( --retries >= 0 && !ret && (vpkts || apkts) ) {
717 if( vpkts && ogg_page_serialno(&og) == to.serialno ) {
718 ogg_stream_init(&tst, to.serialno);
719 ogg_stream_pagein(&tst, &og);
720 while( !ret && vpkts > 0 ) {
721 while( (ret=ogg_stream_packetout(&tst, &op)) < 0 );
724 ret = !th_decode_headerin(&ti, &tc, &ts, &op) ? 1 : 0;
727 printf("theora header error\n");
728 ogg_stream_clear(&tst);
730 else if( apkts && ogg_page_serialno(&og) == vo.serialno ) {
731 ogg_stream_init(&tst, vo.serialno);
732 ogg_stream_pagein(&tst, &og);
733 while( !ret && apkts > 0 ) {
734 while( (ret=ogg_stream_packetout(&tst, &op)) < 0 );
737 ret = vorbis_synthesis_headerin(&vi, &vc, &op) ? 1 : 0;
740 printf("vorbis header error\n");
741 ogg_stream_clear(&tst);
743 if( !ret && !sy.ogg_take_page_out_autoadvance(&og) )
746 printf("incomplete headers\n");
749 // find first start packet (not continued) with data
750 int64_t start_pos = sy.bufpos - (og.header_len + og.body_len);
752 while( --retries >= 0 && !ret && !ogg_page_packets(&og) ) {
753 if( !ogg_page_continued(&og) )
754 start_pos = sy.bufpos - (og.header_len + og.body_len);
755 if( !sy.ogg_take_page_out_autoadvance(&og) ) ret = 1;
758 printf("no data past headers\n");
760 printf("missed %d audio headers\n",apkts);
762 printf("missed %d video headers\n",vpkts);
764 if( retries < 0 || ret || (audio && apkts) || (video && vpkts) ) {
765 eprintf(_("Error in headers"));
769 file_begin = start_pos;
771 if( video && decode_theora_init() )
773 if( audio && decode_vorbis_init() )
778 void FileOGG::close_encoder()
782 write_samples_vorbis(0, 0, 1);
784 write_frames_theora(0, 1, 1);
788 vorbis_block_clear(&vb);
789 vorbis_dsp_clear(&vd);
790 vorbis_comment_clear(&vc);
791 vorbis_info_clear(&vi);
792 ogg_stream_clear(&vo);
796 th_comment_clear(&tc);
797 ogg_stream_clear(&to);
810 void FileOGG::close_decoder()
813 for( int i=0; i<pcm_channels; ++i )
814 delete [] pcm_history[i];
816 delete [] pcm_history; pcm_history = 0;
818 vorbis_dsp_clear(&vd);
819 vorbis_info_clear(&vi);
820 vorbis_block_clear(&vb);
821 vorbis_comment_clear(&vc);
822 ogg_stream_clear(&vo);
823 delete audiosync; audiosync = 0;
828 th_setup_free(ts); ts = 0;
829 th_comment_clear(&tc);
830 ogg_stream_clear(&to);
831 delete videosync; videosync = 0;
846 void FileOGG::get_parameters(BC_WindowBase *parent_window, Asset *asset,
847 BC_WindowBase* &format_window, int audio_options,
848 int video_options, EDL *edl)
852 OGGConfigAudio *window = new OGGConfigAudio(parent_window, asset);
853 format_window = window;
854 window->create_objects();
855 window->run_window();
861 OGGConfigVideo *window = new OGGConfigVideo(parent_window, asset);
862 format_window = window;
863 window->create_objects();
864 window->run_window();
871 int sync_window_t::ogg_take_page_out_autoadvance(ogg_page *og)
874 int ret = ogg_sync_pageout(this, og);
876 printf("FileOGG: Lost sync reading input file\n");
880 bufpos += og->header_len + og->body_len;
883 // need more data for page
884 if( !ogg_read_buffer(READ_SIZE) ) {
885 printf("FileOGG: Read past end of input file\n");
886 return 0; // No more data
893 int FileOGG::check_sig(Asset *asset)
895 FILE *fp = fopen(asset->path, "rb");
898 fseek(fp, 0, SEEK_SET);
900 int ret = fread(data, 4, 1, fp) == 1 &&
901 data[0] == 'O' && data[1] == 'g' &&
902 data[2] == 'g' && data[3] == 'S' ? 1 : 0;
908 int FileOGG::open_file(int rd, int wr)
912 if( !(out = fopen(asset->path, "wb")) ) {
913 eprintf(_("Error while opening %s for writing. %m\n"), asset->path);
916 if( (ret = ogg_init_encode(out)) && out ) {
917 fclose(out); out = 0;
921 if( !(inp = fopen(asset->path, "rb")) ) {
922 eprintf(_("Error while opening %s for reading. %m\n"), asset->path);
925 if( (ret = ogg_init_decode(inp)) && inp ) {
926 fclose(inp); inp = 0;
932 int FileOGG::close_file()
942 int64_t FileOGG::ogg_sample_pos(ogg_page *og)
946 ogg_stream_init(&ss, vo.serialno);
947 ogg_stream_pagein(&ss, og);
951 while( (ret=ogg_stream_packetout(&ss, &op)) ) {
952 if( ret < 0 ) continue; // ignore holes
953 long sz = vorbis_packet_blocksize(&vi, &op);
954 if( prev != -1 ) bsz += (prev + sz) >> 2;
957 ogg_stream_clear(&ss);
958 return ogg_next_sample_pos(og) - bsz;
961 int64_t FileOGG::ogg_next_sample_pos(ogg_page *og)
963 return ogg_page_granulepos(og);
966 int64_t FileOGG::ogg_frame_pos(ogg_page *og)
968 int64_t pos = th_granule_frame(dec, ogg_page_granulepos(og)) - ogg_page_packets(og);
969 if( ogg_page_continued(og) ) --pos;
973 int64_t FileOGG::ogg_next_frame_pos(ogg_page *og)
975 return th_granule_frame(dec, ogg_page_granulepos(og)) + 1;
979 int FileOGG::ogg_get_page_of_sample(ogg_page *og, int64_t sample)
981 if( sample >= asset->audio_length + start_sample ) {
982 printf(_("FileOGG: Illegal seek beyond end of samples\n"));
985 // guess about position
986 int64_t file_length = audiosync->file_end - audiosync->file_begin;
987 off_t guess = file_length * (sample - start_sample) /
988 asset->audio_length - SEEK_SIZE;
989 if( guess < 0 ) guess = 0;
990 guess += audiosync->file_begin;
991 audiosync->ogg_read_buffer_at(guess, READ_SIZE);
992 if( !audiosync->ogg_sync_and_get_next_page(vo.serialno, og) ) {
993 printf(_("FileOGG: llegal seek no pages\n"));
997 while( ret && (ogg_page_granulepos(og) == -1 || !ogg_page_packets(og)) )
998 ret = videosync->ogg_get_next_page(to.serialno, og);
1000 // linear seek to the sample
1001 int missp = 0, missm = 0;
1002 int64_t next_pos = ogg_next_sample_pos(og);
1003 if( sample >= next_pos ) { // scan forward
1004 while( sample >= next_pos ) {
1005 while( !(ret=audiosync->ogg_get_next_page(vo.serialno, og)) &&
1006 (ogg_page_granulepos(og) == -1 || !ogg_page_packets(og)) );
1008 next_pos = ogg_next_sample_pos(og);
1010 //printf("audio %jd next %jd %jd\n", sample, ogg_sample_pos(og), next_pos);
1013 else { // scan backward
1014 int64_t pos = ogg_sample_pos(og);
1015 while( sample < pos ) {
1016 while( (ret=audiosync->ogg_get_prev_page(vo.serialno, og)) &&
1017 (ogg_page_continued(og) && ogg_page_packets(og) == 1) );
1020 pos = ogg_sample_pos(og);
1021 //printf("audio %jd prev %jd %jd\n", sample, pos, ogg_next_sample_pos(og));
1024 //printf("audio %d seek %jd, missp %d, missm %d from %jd to %jd\n", ret,
1025 // sample, missp, missm, ogg_sample_pos(og), ogg_next_sample_pos(og));
1029 int FileOGG::ogg_seek_to_sample(int64_t ogg_sample)
1033 if( !ogg_get_page_of_sample(&og, ogg_sample) ) {
1034 eprintf(_("Seeking to sample's page failed\n"));
1038 int64_t pos = ogg_sample_pos(&og);
1039 int64_t next_pos = pos;
1040 if( ogg_page_continued(&og) ) {
1041 while( (ret=audiosync->ogg_get_prev_page(to.serialno, &og)) &&
1042 (ogg_page_packets(&og) == 0 && ogg_page_continued(&og)) );
1046 ogg_stream_reset(&vo);
1047 ogg_stream_pagein(&vo, &og);
1048 vorbis_synthesis_restart(&vd);
1049 ret = ogg_get_audio_packet(&op);
1051 if( ret && !vorbis_synthesis(&vb, &op) ) {
1052 vorbis_synthesis_blockin(&vd, &vb);
1053 if( vorbis_synthesis_pcmout(&vd, 0) )
1057 eprintf(_("Something wrong while trying to seek\n"));
1061 while( ogg_sample > next_pos ) {
1062 if( !(ret=ogg_get_audio_packet(&op)) ) break;
1063 if( vorbis_synthesis(&vb, &op) ) continue;
1064 vorbis_synthesis_blockin(&vd, &vb);
1066 next_pos += vorbis_synthesis_pcmout(&vd, NULL);
1067 if( next_pos > ogg_sample ) break;
1068 // discard decoded data before current sample
1069 vorbis_synthesis_read(&vd, (next_pos - pos));
1072 audio_pos = next_pos;
1073 vorbis_synthesis_read(&vd, (ogg_sample - pos));
1079 int FileOGG::ogg_get_page_of_frame(ogg_page *og, int64_t frame)
1081 if( frame >= asset->video_length + start_frame ) {
1082 eprintf(_("Illegal seek beyond end of frames\n"));
1085 if( frame < start_frame ) {
1086 eprintf(_("Illegal seek before start of frames\n"));
1089 int64_t file_length = videosync->file_end - videosync->file_begin;
1090 off_t guess = file_length * (frame - start_frame) /
1091 asset->video_length - SEEK_SIZE;
1092 if( guess < 0 ) guess = 0;
1093 guess += videosync->file_begin;
1094 videosync->ogg_read_buffer_at(guess, SEEK_SIZE);
1095 videosync->ogg_sync_and_get_next_page(to.serialno, og);
1096 // find the page with "real" ending
1098 while( ret && (ogg_page_granulepos(og) == -1 || !ogg_page_packets(og)) )
1099 ret = videosync->ogg_get_next_page(to.serialno, og);
1100 int64_t pos = ogg_next_frame_pos(og);
1102 int missp = 0, missm = 0;
1103 // move back if continued
1104 if( frame >= pos ) {
1105 do { // scan forward
1106 while( (ret=videosync->ogg_get_next_page(to.serialno, og)) &&
1107 ogg_page_packets(og) == 0 );
1110 pos = ogg_next_frame_pos(og);
1111 //printf("video %jd next %jd %jd\n", frame, ogg_frame_pos(og), pos);
1112 } while( frame >= pos );
1114 else if( (pos=ogg_frame_pos(og)) > frame ) {
1115 while( pos > start_frame && frame < pos ) { // scan backward
1116 while( (ret=videosync->ogg_get_prev_page(to.serialno, og)) &&
1117 ogg_page_packets(og) == 0 && ogg_page_continued(og) );
1120 pos = ogg_frame_pos(og);
1121 //printf("video %jd next %jd %jd\n", frame, pos, ogg_next_frame_pos(og));
1124 //printf("video %d seek %jd, missp %d, missm %d first %jd, next %jd\n", ret,
1125 // frame, missp, missm, ogg_frame_pos(og), ogg_next_frame_pos(og));
1129 int FileOGG::ogg_seek_to_keyframe(int64_t frame, int64_t *keyframe_number)
1131 //printf("ogg_seek_to_keyframe of === %jd\n", frame);
1134 int64_t ipagpos = -1;
1135 int64_t istart = -1;
1136 int64_t iframe = -1;
1138 int retries = 1000, ret = 1;
1139 while( --retries>=0 && frame>=start_frame ) {
1140 if( !ogg_get_page_of_frame(&og, frame) ) break;
1141 int64_t pos = ogg_frame_pos(&og);
1143 if( ogg_page_continued(&og) ) {
1144 while( (ret=videosync->ogg_get_prev_page(to.serialno, &og)) &&
1145 (ogg_page_packets(&og) == 0 && ogg_page_continued(&og)) );
1147 int64_t pagpos = videosync->pagpos;
1149 ogg_stream_reset(&to);
1150 ogg_stream_pagein(&to, &og);
1152 while( frame >= pos && (ret=ogg_get_video_packet(&op)) ) {
1153 if( th_packet_iskeyframe(&op) == 1 ) {
1157 //printf("keyframe %jd pkts %d\n", pos, pkts);
1159 //printf("packet %jd pkts %d is a %d\n", pos, pkts, th_packet_iskeyframe(&op));
1162 if( ipagpos >= 0 ) break;
1166 printf(_("Seeking to keyframe %jd search failed\n"), frame);
1169 videosync->ogg_read_buffer_at(ipagpos, READ_SIZE);
1170 videosync->ogg_sync_and_get_next_page(to.serialno, &og);
1172 ogg_stream_reset(&to);
1173 ogg_stream_pagein(&to, &og);
1174 video_pos = ogg_next_frame_pos(&og);
1175 // skip prev packets
1176 // int ipkts = iframe - ogg_frame_pos(&og);
1177 //printf("iframe %jd, page %jd, ipkts %d\n", iframe, ogg_page_pageno(&og), ipkts);
1178 while( --ipkts >= 0 )
1179 ogg_get_video_packet(&op);
1180 *keyframe_number = iframe;
1185 int64_t FileOGG::get_video_position()
1188 return next_frame_position - start_frame;
1191 int64_t FileOGG::get_audio_position()
1193 return next_sample_position - start_sample;
1196 int FileOGG::set_video_position(int64_t x)
1199 // printf("SVP: %lli\n", x);
1201 next_frame_position = x + start_frame;
1206 int FileOGG::colormodel_supported(int colormodel)
1210 if (colormodel == BC_YUV420P)
1215 int FileOGG::get_best_colormodel(Asset *asset, int driver)
1221 int FileOGG::set_audio_position(int64_t x)
1223 next_sample_position = x + start_sample;
1228 int FileOGG::ogg_get_video_packet(ogg_packet *op)
1231 while( (ret=ogg_stream_packetout(&to, op)) <= 0 ) {
1232 if( video_eos ) return 0;
1234 if( !videosync->ogg_get_next_page(to.serialno, &og) ) break;
1235 if( ogg_page_granulepos(&og) >= 0 )
1236 video_pos = ogg_next_frame_pos(&og);
1237 ogg_stream_pagein(&to, &og);
1238 video_eos = ogg_page_eos(&og);
1241 printf("FileOGG: Cannot read video packet\n");
1247 int FileOGG::read_frame(VFrame *frame)
1249 if( !inp || !video ) return 1;
1250 // skip is cheaper than seek, do it...
1251 int decode_frames = 0;
1252 int expect_keyframe = 0;
1253 if( ogg_frame_position >= 0 &&
1254 next_frame_position >= ogg_frame_position &&
1255 next_frame_position - ogg_frame_position < 32) {
1256 decode_frames = next_frame_position - ogg_frame_position;
1258 else if( next_frame_position != ogg_frame_position ) {
1259 if( !ogg_seek_to_keyframe(next_frame_position, &ogg_frame_position) ) {
1260 eprintf(_("Error while seeking to frame's keyframe"
1261 " (frame: %jd, keyframe: %jd)\n"),
1262 next_frame_position, ogg_frame_position);
1265 decode_frames = next_frame_position - ogg_frame_position + 1;
1266 --ogg_frame_position;
1267 if( decode_frames <= 0 ) {
1268 eprintf(_("Error while seeking to keyframe,"
1269 " wrong keyframe number (frame: %jd, keyframe: %jd)\n"),
1270 next_frame_position, ogg_frame_position);
1274 expect_keyframe = 1;
1278 while( decode_frames > 0 ) {
1279 if( !ogg_get_video_packet(&op) ) break;
1280 if( expect_keyframe ) {
1281 expect_keyframe = 0;
1282 if( th_packet_iskeyframe(&op) <= 0 )
1283 eprintf(_("FileOGG: Expecting keyframe, but didn't get it\n"));
1285 ogg_int64_t granpos = 0;
1286 if( th_decode_packetin(dec, &op, &granpos) >= 0 )
1288 ++ogg_frame_position;
1291 //if(ret < 0 )printf("ret = %d\n", ret);
1293 th_ycbcr_buffer ycbcr;
1294 ret = th_decode_ycbcr_out(dec, ycbcr);
1296 eprintf(_("th_decode_ycbcr_out failed with code %i\n"), ret);
1297 ret = 0; // not always fatal
1299 uint8_t *yp = ycbcr[0].data;
1300 uint8_t *up = ycbcr[1].data;
1301 uint8_t *vp = ycbcr[2].data;
1302 int yst = ycbcr[0].stride;
1303 int yw = ycbcr[0].width;
1304 int yh = ycbcr[0].height;
1305 VFrame temp_frame(yp, -1, 0, up-yp, vp-yp, yw,yh, BC_YUV420P, yst);
1306 int px = ti.pic_x, py = ti.pic_y;
1307 int pw = ti.pic_width, ph = ti.pic_height;
1308 frame->transfer_from(&temp_frame, -1, px, py, pw, ph);
1311 next_frame_position++;
1316 int FileOGG::ogg_get_audio_packet(ogg_packet *op)
1319 while( (ret=ogg_stream_packetout(&vo, op)) <= 0 ) {
1320 if( audio_eos ) return 0;
1322 if( !audiosync->ogg_get_next_page(vo.serialno, &og) ) break;
1323 if( ogg_page_granulepos(&og) >= 0 )
1324 audio_pos = ogg_next_sample_pos(&og);
1325 ogg_stream_pagein(&vo, &og);
1326 audio_eos = ogg_page_eos(&og);
1329 printf("FileOGG: Cannot read audio packet\n");
1335 int FileOGG::ogg_decode_more_samples()
1338 while( ogg_get_audio_packet(&op) ) {
1339 if( !vorbis_synthesis(&vb, &op) ) {
1340 vorbis_synthesis_blockin(&vd, &vb);
1344 ogg_sample_position = -11;
1345 eprintf(_("Cannot find next page while trying to decode more samples\n"));
1349 int FileOGG::move_history(int from, int to, int len)
1352 for( int i=0; i<asset->channels; ++i )
1353 memmove(pcm_history[i] + to,
1354 pcm_history[i] + from,
1355 sizeof(float) * len);
1357 history_start = history_start + from - to;
1358 if( history_start < 0 ) history_start = 0;
1362 int FileOGG::read_samples(double *buffer, int64_t len)
1364 float **vorbis_buffer;
1367 if( len > HISTORY_MAX ) {
1368 eprintf(_("max samples=%d\n"), HISTORY_MAX);
1372 if( !pcm_history ) {
1373 pcm_history = new float*[asset->channels];
1374 for(int i = 0; i < asset->channels; i++)
1375 pcm_history[i] = new float[HISTORY_MAX];
1376 history_start = -100000000;
1380 int64_t hole_start = -1;
1381 int64_t hole_len = -1;
1382 int64_t hole_absstart = -1;
1383 int64_t hole_fill = 0;
1385 if( history_start < next_sample_position &&
1386 history_start + history_size > next_sample_position &&
1387 history_start + history_size < next_sample_position + len ) {
1389 hole_start = history_start + history_size - next_sample_position;
1390 hole_len = history_size - hole_start;
1391 hole_absstart = next_sample_position + hole_start;
1392 move_history(next_sample_position - history_start, 0, hole_start);
1394 else if( next_sample_position < history_start &&
1395 history_start < next_sample_position + len ) {
1398 hole_len = history_start - next_sample_position;
1399 hole_absstart = next_sample_position;
1401 history_start - next_sample_position,
1402 history_size - history_start + next_sample_position);
1405 else if( next_sample_position >= history_start + history_size ||
1406 next_sample_position + len <= history_start ) {
1409 hole_len = HISTORY_MAX;
1410 hole_absstart = next_sample_position;
1411 history_start = hole_absstart;
1412 history_size = hole_len;
1416 if( hole_start < 0 || hole_len <= 0 || hole_absstart < 0 ) {
1417 eprintf(_("Error in finding read file position\n"));
1421 if( hole_absstart + hole_len > asset->audio_length + start_sample ) {
1422 hole_len = asset->audio_length + start_sample - hole_absstart;
1423 history_size = asset->audio_length + start_sample - history_start;
1426 history_size = HISTORY_MAX;
1429 int64_t samples_read = 0;
1430 if( ogg_sample_position != hole_absstart ) {
1431 ogg_sample_position = hole_absstart;
1432 if( !ogg_seek_to_sample(ogg_sample_position) ) {
1433 eprintf(_("Error while seeking to sample\n"));
1437 // now we have ogg_sample_positon aligned
1438 int64_t samples_to_read = hole_len;
1439 while( samples_read < hole_len ) {
1440 int64_t samples_waiting = vorbis_synthesis_pcmout(&vd, &vorbis_buffer);
1441 int64_t samples_avail = !samples_waiting && audio_eos ?
1442 hole_len - samples_read : // silence after eos
1444 int64_t sample_demand = samples_to_read - samples_read;
1445 int64_t sample_count = MIN(samples_avail, sample_demand);
1446 if( sample_count > 0 ) {
1447 int sz = sample_count*sizeof(float);
1448 if( samples_waiting ) {
1449 for( int i=0; i<asset->channels; ++i ) {
1450 float *input = vorbis_buffer[i];
1451 float *output = pcm_history[i] + hole_start;
1452 memcpy(output, input, sz);
1454 vorbis_synthesis_read(&vd, sample_count);
1457 for( int i=0; i<asset->channels; ++i ) {
1458 float *output = pcm_history[i] + hole_start;
1459 memset(output, 0, sz);
1462 ogg_sample_position += sample_count;
1463 hole_start += sample_count;
1464 samples_read += sample_count;
1465 if( samples_read >= hole_len ) break;
1467 if( samples_read < hole_len && !ogg_decode_more_samples() )
1472 if( next_sample_position < history_start ||
1473 next_sample_position + len > history_start + history_size ) {
1474 printf(_("FileOGG:: History not aligned properly \n"));
1475 printf(_("\tnext_sample_position: %jd, length: %jd\n"), next_sample_position, len);
1476 printf(_("\thistory_start: %jd, length: %jd\n"), history_start, history_size);
1479 float *input = pcm_history[file->current_channel] + next_sample_position - history_start;
1480 for (int i = 0; i < len; i++)
1481 buffer[i] = input[i];
1483 next_sample_position += len;
1488 int FileOGG::write_audio_page()
1490 int ret = apage.write_page(out);
1492 eprintf(_("error writing audio page\n"));
1496 int FileOGG::write_video_page()
1498 int ret = vpage.write_page(out);
1500 eprintf(_("error writing video page\n"));
1504 // flush out the ogg pages
1505 void FileOGG::flush_ogg(int last)
1508 file_lock->lock("FileOGG::flush_ogg");
1510 // this way seeking is much better, (per original fileogg)
1511 // not sure if 32 packets is a good value.
1513 if( video && !vpage.valid ) {
1514 if( (vpage.packets > mx_pkts && ogg_stream_flush(&to, &og) > 0) ||
1515 ogg_stream_pageout(&to, &og) > 0 ) {
1516 videotime = th_granule_time(enc, vpage.load(&og));
1519 if( audio && !apage.valid ) {
1520 if( (apage.packets > mx_pkts && ogg_stream_flush(&vo, &og) > 0) ||
1521 ogg_stream_pageout(&vo, &og) > 0 ) {
1522 audiotime = vorbis_granule_time(&vd, apage.load(&og));
1525 if( !audio && vpage.valid )
1527 else if( !video && apage.valid )
1529 else if( !vpage.valid || !apage.valid )
1531 // output earliest page
1532 else if( videotime > audiotime ) // output earliest
1537 if( last ) { // at last
1543 file_lock->unlock();
1547 int FileOGG::write_samples_vorbis(double **buffer, int64_t len, int last)
1549 if( !audio || !out ) return 1;
1552 float **vorbis_buffer = vorbis_analysis_buffer(&vd, len);
1553 for( int i=0; i<asset->channels; ++i ) // double to float
1554 for( int j=0; j < len; ++j )
1555 vorbis_buffer[i][j] = buffer[i][j];
1559 vorbis_analysis_wrote(&vd, len);
1561 while( vorbis_analysis_blockout(&vd, &vb) == 1 ) {
1562 vorbis_analysis(&vb, 0);
1563 vorbis_bitrate_addblock(&vb);
1565 while( vorbis_bitrate_flushpacket(&vd, &op) ) {
1566 file_lock->lock("FileOGG::write_vorbis_audio");
1567 ogg_stream_packetin(&vo, &op);
1569 file_lock->unlock();
1575 int FileOGG::write_samples(double **buffer, int64_t len)
1578 return write_samples_vorbis(buffer, len, 0);
1583 int FileOGG::write_frames_theora(VFrame ***frames, int len, int last)
1585 if( !video || !out ) return 1;
1586 for( int j=0; j<len; ++j ) {
1589 th_ycbcr_buffer ycbcr;
1590 ycbcr[0].width = frame_w;
1591 ycbcr[0].height = frame_h;
1592 ycbcr[0].stride = temp_frame->get_bytes_per_line();
1593 ycbcr[0].data = temp_frame->get_y();
1594 ycbcr[1].width = frame_w/2;
1595 ycbcr[1].height = frame_h/2;
1596 ycbcr[1].stride = (temp_frame->get_bytes_per_line()+1)/2;
1597 ycbcr[1].data = temp_frame->get_u();
1598 ycbcr[2].width = frame_w/2;
1599 ycbcr[2].height = frame_h/2;
1600 ycbcr[2].stride = (temp_frame->get_bytes_per_line()+1)/2;
1601 ycbcr[2].data = temp_frame->get_v();
1602 if( th_encode_ycbcr_in(enc, ycbcr) ) {
1603 eprintf(_("th_encode_ycbcr_in failed"));
1607 while( th_encode_packetout(enc, last, &op) > 0 ) {
1609 ogg_stream_packetin (&to, &op);
1611 file_lock->unlock();
1614 if( last ) return 0;
1616 temp_frame = new VFrame (0, -1, frame_w, frame_h, theora_cmodel, -1);
1617 VFrame *frame = frames[0][j];
1618 temp_frame->transfer_from(frame);
1623 int FileOGG::write_frames(VFrame ***frames, int len)
1625 return write_frames_theora(frames, len, 0);
1630 OGGConfigAudio::OGGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
1631 : BC_Window(PROGRAM_NAME ": Audio Compression",
1632 parent_window->get_abs_cursor_x(1),
1633 parent_window->get_abs_cursor_y(1),
1636 this->parent_window = parent_window;
1637 this->asset = asset;
1638 // *** CONTEXT_HELP ***
1639 context_help_set_keyword("Single File Rendering");
1642 OGGConfigAudio::~OGGConfigAudio()
1647 void OGGConfigAudio::create_objects()
1649 // add_tool(new BC_Title(10, 10, _("There are no audio options for this format")));
1651 int x = xS(10), y = yS(10);
1653 char string[BCTEXTLEN];
1655 lock_window("OGGConfigAudio::create_objects");
1656 add_tool(fixed_bitrate = new OGGVorbisFixedBitrate(x, y, this));
1657 add_tool(variable_bitrate = new OGGVorbisVariableBitrate(x + fixed_bitrate->get_w() + xS(5),
1662 sprintf(string, "%d", asset->vorbis_min_bitrate);
1663 add_tool(new BC_Title(x, y, _("Min bitrate:")));
1664 add_tool(new OGGVorbisMinBitrate(x1, y, this, string));
1667 add_tool(new BC_Title(x, y, _("Avg bitrate:")));
1668 sprintf(string, "%d", asset->vorbis_bitrate);
1669 add_tool(new OGGVorbisAvgBitrate(x1, y, this, string));
1672 add_tool(new BC_Title(x, y, _("Max bitrate:")));
1673 sprintf(string, "%d", asset->vorbis_max_bitrate);
1674 add_tool(new OGGVorbisMaxBitrate(x1, y, this, string));
1677 add_subwindow(new BC_OKButton(this));
1682 int OGGConfigAudio::close_event()
1688 OGGVorbisFixedBitrate::OGGVorbisFixedBitrate(int x, int y, OGGConfigAudio *gui)
1689 : BC_Radial(x, y, !gui->asset->vorbis_vbr, _("Average bitrate"))
1693 int OGGVorbisFixedBitrate::handle_event()
1695 gui->asset->vorbis_vbr = 0;
1696 gui->variable_bitrate->update(0);
1700 OGGVorbisVariableBitrate::OGGVorbisVariableBitrate(int x, int y, OGGConfigAudio *gui)
1701 : BC_Radial(x, y, gui->asset->vorbis_vbr, _("Variable bitrate"))
1705 int OGGVorbisVariableBitrate::handle_event()
1707 gui->asset->vorbis_vbr = 1;
1708 gui->fixed_bitrate->update(0);
1713 OGGVorbisMinBitrate::OGGVorbisMinBitrate(int x,
1715 OGGConfigAudio *gui,
1717 : BC_TextBox(x, y, xS(180), 1, text)
1721 int OGGVorbisMinBitrate::handle_event()
1723 gui->asset->vorbis_min_bitrate = atol(get_text());
1729 OGGVorbisMaxBitrate::OGGVorbisMaxBitrate(int x,
1731 OGGConfigAudio *gui,
1733 : BC_TextBox(x, y, xS(180), 1, text)
1737 int OGGVorbisMaxBitrate::handle_event()
1739 gui->asset->vorbis_max_bitrate = atol(get_text());
1745 OGGVorbisAvgBitrate::OGGVorbisAvgBitrate(int x, int y, OGGConfigAudio *gui, char *text)
1746 : BC_TextBox(x, y, xS(180), 1, text)
1750 int OGGVorbisAvgBitrate::handle_event()
1752 gui->asset->vorbis_bitrate = atol(get_text());
1760 OGGConfigVideo::OGGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
1761 : BC_Window(PROGRAM_NAME ": Video Compression",
1762 parent_window->get_abs_cursor_x(1),
1763 parent_window->get_abs_cursor_y(1),
1766 this->parent_window = parent_window;
1767 this->asset = asset;
1768 // *** CONTEXT_HELP ***
1769 context_help_set_keyword("Single File Rendering");
1772 OGGConfigVideo::~OGGConfigVideo()
1777 void OGGConfigVideo::create_objects()
1779 // add_tool(new BC_Title(10, 10, _("There are no video options for this format")));
1780 int x = xS(10), y = yS(10);
1781 int x1 = x + xS(150);
1782 int x2 = x + xS(300);
1784 lock_window("OGGConfigVideo::create_objects");
1786 add_subwindow(title = new BC_Title(x, y, _("Bitrate:")));
1787 add_subwindow(new OGGTheoraBitrate(x + title->get_w() + xS(5), y, this));
1788 add_subwindow(fixed_bitrate = new OGGTheoraFixedBitrate(x2, y, this));
1791 add_subwindow(new BC_Title(x, y, _("Quality:")));
1792 add_subwindow(new BC_ISlider(x + xS(80), y, 0, xS(200), xS(200),
1793 0, 63, asset->theora_quality,
1794 0, 0, &asset->theora_quality));
1797 add_subwindow(fixed_quality = new OGGTheoraFixedQuality(x2, y, this));
1800 add_subwindow(new BC_Title(x, y, _("Keyframe frequency:")));
1801 OGGTheoraKeyframeFrequency *keyframe_frequency =
1802 new OGGTheoraKeyframeFrequency(x1 + xS(60), y, this);
1803 keyframe_frequency->create_objects();
1806 add_subwindow(new BC_Title(x, y, _("Keyframe force frequency:")));
1807 OGGTheoraKeyframeForceFrequency *keyframe_force_frequency =
1808 new OGGTheoraKeyframeForceFrequency(x1 + xS(60), y, this);
1809 keyframe_force_frequency->create_objects();
1812 add_subwindow(new BC_Title(x, y, _("Sharpness:")));
1813 OGGTheoraSharpness *sharpness =
1814 new OGGTheoraSharpness(x1 + xS(60), y, this);
1815 sharpness->create_objects();
1819 add_subwindow(new BC_OKButton(this));
1827 int OGGConfigVideo::close_event()
1833 OGGTheoraBitrate::OGGTheoraBitrate(int x, int y, OGGConfigVideo *gui)
1834 : BC_TextBox(x, y, xS(100), 1, gui->asset->theora_bitrate)
1840 int OGGTheoraBitrate::handle_event()
1842 // TODO: MIN / MAX check
1843 gui->asset->theora_bitrate = atol(get_text());
1850 OGGTheoraFixedBitrate::OGGTheoraFixedBitrate(int x, int y, OGGConfigVideo *gui)
1851 : BC_Radial(x, y, gui->asset->theora_fix_bitrate, _("Fixed bitrate"))
1856 int OGGTheoraFixedBitrate::handle_event()
1859 gui->asset->theora_fix_bitrate = 1;
1860 gui->fixed_quality->update(0);
1864 OGGTheoraFixedQuality::OGGTheoraFixedQuality(int x, int y, OGGConfigVideo *gui)
1865 : BC_Radial(x, y, !gui->asset->theora_fix_bitrate, _("Fixed quality"))
1870 int OGGTheoraFixedQuality::handle_event()
1873 gui->asset->theora_fix_bitrate = 0;
1874 gui->fixed_bitrate->update(0);
1878 OGGTheoraKeyframeFrequency::OGGTheoraKeyframeFrequency(int x, int y, OGGConfigVideo *gui)
1879 : BC_TumbleTextBox(gui, (int64_t)gui->asset->theora_keyframe_frequency,
1880 (int64_t)1, (int64_t)500, x, y, xS(40))
1885 int OGGTheoraKeyframeFrequency::handle_event()
1887 gui->asset->theora_keyframe_frequency = atol(get_text());
1891 OGGTheoraKeyframeForceFrequency::OGGTheoraKeyframeForceFrequency(int x, int y, OGGConfigVideo *gui)
1892 : BC_TumbleTextBox(gui, (int64_t)gui->asset->theora_keyframe_frequency,
1893 (int64_t)1, (int64_t)500, x, y, xS(40))
1898 int OGGTheoraKeyframeForceFrequency::handle_event()
1900 gui->asset->theora_keyframe_frequency = atol(get_text());
1905 OGGTheoraSharpness::OGGTheoraSharpness(int x, int y, OGGConfigVideo *gui)
1906 : BC_TumbleTextBox(gui, (int64_t)gui->asset->theora_sharpness,
1907 (int64_t)0, (int64_t)2, x, y, xS(40))
1912 int OGGTheoraSharpness::handle_event()
1914 gui->asset->theora_sharpness = atol(get_text());