4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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
23 #include "bcsignals.h"
24 #include "byteorder.h"
30 #include "interlacemodes.h"
32 #include "mainerror.h"
34 #include "mwindow.inc"
35 #include "preferences.h"
38 #include "versioninfo.h"
39 #include "videodevice.inc"
41 #include <sys/types.h>
48 #define READ_SIZE 3*66000
50 /* This code was aspired by ffmpeg2theora */
51 /* Special thanks for help on this code goes out to j@v2v.cc */
53 int ogg_ret0, ogg_ret1, ogg_ret2;
55 FileOGG::FileOGG(Asset *asset, File *file)
56 : FileBase(asset, file)
58 if(asset->format == FILE_UNKNOWN)
59 asset->format = FILE_OGG;
60 asset->byte_order = 0;
72 ogg_sync_clear(&tf->videosync->sync);
74 theora_info_clear(&tf->ti);
75 theora_comment_clear(&tf->tc);
79 ogg_sync_clear(&tf->audiosync->sync);
81 vorbis_info_clear(&tf->vi);
82 vorbis_comment_clear(&tf->vc);
90 if (temp_frame) delete temp_frame;
91 if (stream) close_file();
94 for(int i = 0; i < asset->channels; i++)
95 delete [] pcm_history[i];
96 delete [] pcm_history;
99 if (flush_lock) delete flush_lock;
102 void FileOGG::get_parameters(BC_WindowBase *parent_window,
103 Asset *asset, BC_WindowBase* &format_window,
104 int audio_options, int video_options, EDL *edl)
108 OGGConfigAudio *window = new OGGConfigAudio(parent_window, asset);
109 format_window = window;
110 window->create_objects();
111 window->run_window();
117 OGGConfigVideo *window = new OGGConfigVideo(parent_window, asset);
118 format_window = window;
119 window->create_objects();
120 window->run_window();
125 int FileOGG::reset_parameters_derived()
136 static int read_buffer(FILE *in, sync_window_t *sw, int buflen)
138 char *buffer = ogg_sync_buffer(&sw->sync, buflen);
139 // printf("reading range: %lli - %lli\n", sw->file_bufpos, sw->file_bufpos + buflen);
140 sw->wlen = fread(buffer, 1, buflen, in);
141 ogg_sync_wrote(&sw->sync, sw->wlen);
142 // printf("XX data: %c %c %c %c\n", sw->sync.data[0], sw->sync.data[1], sw->sync.data[2], sw->sync.data[3]);
143 sw->file_bufpos += sw->wlen;
144 // printf("sb: %i\n",buffer);
148 static int read_buffer_at(FILE *in, sync_window_t *sw, int buflen, off_t filepos)
150 // printf("seeking to %lli %lli\n", filepos, sw->file_bufpos);
151 fseeko(in, filepos, SEEK_SET);
152 // if (sw->file_bufpos != filepos)
154 sw->file_bufpos = filepos;
155 sw->file_pagepos = filepos; // this one is not valid until sync_pageseek!
156 ogg_sync_reset(&sw->sync);
159 return read_buffer(in, sw, buflen);
162 static int take_page_out_autoadvance(FILE *in, sync_window_t *sw, ogg_page *og)
166 int ret = ogg_sync_pageout(&sw->sync, og);
169 // printf("fpa: %lli\n", sw->file_pagepos);
170 // advance 'virtual' position
171 sw->file_pagepos += og->header_len + og->body_len;
172 // printf("ret2: %i %i\n",ret, og->header_len + og->body_len);
177 eprintf(_("FileOGG: Taking page out on nonsynced stream!\n"));
182 // need more data for page
183 if ((ret = read_buffer(in, sw, READ_SIZE)) == 0)
185 printf(_("FileOGG: There is no more data in the file we are reading from\n"));
186 return 0; // No more data
194 // we never need to autoadvance when syncing, since our read chunks are larger than
196 static int sync_and_take_page_out(sync_window_t *sw, ogg_page *page)
198 page->header_len = 0;
202 int ret = ogg_sync_pageseek(&sw->sync, page);
205 sw->file_pagepos -= ret;
209 sw->file_pagepos += ret;
210 // printf("ret: %i %i\n",ret, page->header_len + page->body_len);
215 int FileOGG::open_file(int rd, int wr)
219 tf = new theoraframes_info_t;
220 memset(tf, 0, sizeof(*tf));
228 if((stream = fopen(asset->path, "w+b")) == 0)
230 eprintf(_("Error while opening \"%s\" for writing. %m\n"), asset->path);
234 tf->audio_bytesout = 0;
235 tf->video_bytesout = 0;
241 tf->apage_buffer_length = 0;
242 tf->vpage_buffer_length = 0;
249 /* yayness. Set up Ogg output stream */
252 if(asset->video_data)
254 ogg_stream_init (&tf->to, rand ()); /* oops, add one ot the above */
256 theora_info_init (&tf->ti);
258 tf->ti.frame_width = asset->width;
259 tf->ti.frame_height = asset->height;
261 tf->ti.width = ((asset->width + 15) >>4)<<4; // round up to the nearest multiple of 16
262 tf->ti.height = ((asset->height + 15) >>4)<<4; // round up to the nearest multiple of 16
263 if (tf->ti.width != tf->ti.frame_width || tf->ti.height != tf->ti.frame_height)
265 eprintf(_("WARNING: Encoding theora when width or height are not dividable by 16 is suboptimal\n"));
269 tf->ti.offset_y = tf->ti.height - tf->ti.frame_height;
270 tf->ti.fps_numerator = (unsigned int)(asset->frame_rate * 1000000);
271 tf->ti.fps_denominator = 1000000;
273 if (asset->aspect_ratio > 0)
275 // Cinelerra uses frame aspect ratio, theora uses pixel aspect ratio
276 float pixel_aspect = asset->aspect_ratio / asset->width * asset->height;
277 tf->ti.aspect_numerator = (unsigned int)(pixel_aspect * 1000000);
278 tf->ti.aspect_denominator = 1000000;
281 tf->ti.aspect_numerator = 1000000;
282 tf->ti.aspect_denominator = 1000000;
284 if(EQUIV(asset->frame_rate, 25) || EQUIV(asset->frame_rate, 50))
285 tf->ti.colorspace = OC_CS_ITU_REC_470BG;
286 else if((asset->frame_rate > 29 && asset->frame_rate < 31) || (asset->frame_rate > 59 && asset->frame_rate < 61) )
287 tf->ti.colorspace = OC_CS_ITU_REC_470M;
289 tf->ti.colorspace = OC_CS_UNSPECIFIED;
291 if (asset->theora_fix_bitrate)
293 tf->ti.target_bitrate = asset->theora_bitrate;
297 tf->ti.target_bitrate = 0;
298 tf->ti.quality = asset->theora_quality; // video quality 0-63
300 tf->ti.dropframes_p = 0;
302 tf->ti.keyframe_auto_p = 1;
303 tf->ti.keyframe_frequency = asset->theora_keyframe_frequency;
304 tf->ti.keyframe_frequency_force = asset->theora_keyframe_force_frequency;
305 tf->ti.keyframe_data_target_bitrate = (unsigned int) (tf->ti.target_bitrate * 1.5) ;
306 tf->ti.keyframe_auto_threshold = 80;
307 tf->ti.keyframe_mindistance = 8;
308 tf->ti.noise_sensitivity = 1;
309 tf->ti.sharpness = 2;
312 if (theora_encode_init (&tf->td, &tf->ti))
314 eprintf(_("(FileOGG:file_open) initialization of theora codec failed\n"));
317 /* init theora done */
319 /* initialize Vorbis too, if we have audio. */
320 if(asset->audio_data)
322 ogg_stream_init (&tf->vo, rand ());
323 vorbis_info_init (&tf->vi);
324 /* Encoding using a VBR quality mode. */
326 if(!asset->vorbis_vbr)
328 ret = vorbis_encode_init(&tf->vi,
331 asset->vorbis_max_bitrate,
332 asset->vorbis_bitrate,
333 asset->vorbis_min_bitrate);
336 // Set true VBR as demonstrated by http://svn.xiph.org/trunk/vorbis/doc/vorbisenc/examples.html
337 ret = vorbis_encode_setup_managed(&tf->vi,
341 asset->vorbis_bitrate,
343 ret |= vorbis_encode_ctl(&tf->vi, OV_ECTL_RATEMANAGE_AVG, NULL);
344 ret |= vorbis_encode_setup_init(&tf->vi);
349 eprintf(_("The Vorbis encoder could not set up a mode according to\n"
350 "the requested quality or bitrate.\n\n"));
356 vorbis_comment_init (&tf->vc); // comment is cleared lateron
357 vorbis_comment_add_tag (&tf->vc, (char*)"ENCODER", (char*)PROGRAM_NAME " " CINELERRA_VERSION);
358 /* set up the analysis state and auxiliary encoding storage */
359 vorbis_analysis_init (&tf->vd, &tf->vi);
360 vorbis_block_init (&tf->vd, &tf->vb);
363 /* audio init done */
365 /* write the bitstream header packets with proper page interleave */
367 /* first packet will get its own page automatically */
368 if(asset->video_data)
370 theora_encode_header (&tf->td, &tf->op);
371 ogg_stream_packetin (&tf->to, &tf->op);
372 if (ogg_stream_pageout (&tf->to, &tf->og) != 1)
374 eprintf(_("Internal Ogg library error.\n"));
377 fwrite (tf->og.header, 1, tf->og.header_len, stream);
378 fwrite (tf->og.body, 1, tf->og.body_len, stream);
380 /* create the remaining theora headers */
381 theora_comment_init (&tf->tc);
382 theora_comment_add_tag (&tf->tc, (char*)"ENCODER",
383 (char*)PROGRAM_NAME " " CINELERRA_VERSION);
384 theora_encode_comment (&tf->tc, &tf->op);
385 ogg_stream_packetin (&tf->to, &tf->op);
386 theora_comment_clear(&tf->tc);
387 theora_encode_tables (&tf->td, &tf->op);
388 ogg_stream_packetin (&tf->to, &tf->op);
390 if(asset->audio_data)
393 ogg_packet header_comm;
394 ogg_packet header_code;
396 vorbis_analysis_headerout (&tf->vd, &tf->vc, &header,
397 &header_comm, &header_code);
398 ogg_stream_packetin (&tf->vo, &header); /* automatically placed in its own page */
399 vorbis_comment_clear(&tf->vc);
400 if (ogg_stream_pageout (&tf->vo, &tf->og) != 1)
402 eprintf(_("Internal Ogg library error.\n"));
405 fwrite (tf->og.header, 1, tf->og.header_len, stream);
406 fwrite (tf->og.body, 1, tf->og.body_len, stream);
408 /* remaining vorbis header packets */
409 ogg_stream_packetin (&tf->vo, &header_comm);
410 ogg_stream_packetin (&tf->vo, &header_code);
413 /* Flush the rest of our headers. This ensures
414 * the actual data in each stream will start
415 * on a new page, as per spec. */
416 while (1 && asset->video_data)
418 int result = ogg_stream_flush (&tf->to, &tf->og);
422 eprintf(_("Internal Ogg library error.\n"));
427 fwrite (tf->og.header, 1, tf->og.header_len, stream);
428 fwrite (tf->og.body, 1, tf->og.body_len, stream);
430 while (1 && asset->audio_data)
432 int result = ogg_stream_flush (&tf->vo, &tf->og);
436 eprintf(_("Internal Ogg library error.\n"));
441 fwrite (tf->og.header, 1, tf->og.header_len, stream);
442 fwrite (tf->og.body, 1, tf->og.body_len, stream);
444 flush_lock = new Mutex("OGGFile::Flush lock");
445 // printf("End of headers at position: %lli\n", ftello(stream));
450 if((stream = fopen(asset->path, "rb")) == 0)
452 eprintf(_("Error while opening %s for reading. %m\n"), asset->path);
456 /* get file length */
457 struct stat file_stat;
458 stat(asset->path, &file_stat);
459 file_length = file_stat.st_size;
461 /* start up Ogg stream synchronization layer */
462 /* oy is used just here to parse header, we use separate syncs for video and audio*/
464 ogg_sync_init(&oy.sync);
465 // make sure we init the position structures to zero
466 read_buffer_at(stream, &oy, READ_SIZE, 0);
469 /* init supporting Vorbis structures needed in header parsing */
470 vorbis_info_init(&tf->vi);
471 vorbis_comment_init(&tf->vc);
474 /* init supporting Theora structures needed in header parsing */
475 theora_comment_init(&tf->tc);
476 theora_info_init(&tf->ti);
480 /* Ogg file open; parse the headers */
481 /* Only interested in Vorbis/Theora streams */
489 // int ret = read_buffer(stream, &oy, 4096);
490 TRACE("FileOGG::open_file 60")
494 while(take_page_out_autoadvance(stream, &oy, &tf->og) > 0)
496 ogg_stream_state test;
498 /* is this a mandated initial header? If not, stop parsing */
499 if(!ogg_page_bos(&tf->og))
501 /* don't leak the page; get it into the appropriate stream */
502 // queue_page(&tf->og);
503 if(theora_p)ogg_stream_pagein(&tf->to, &tf->og);
504 if(vorbis_p)ogg_stream_pagein(&tf->vo, &tf->og);
510 ogg_stream_init(&test, ogg_page_serialno(&tf->og));
511 ogg_stream_pagein(&test, &tf->og);
512 ogg_stream_packetout(&test, &tf->op);
514 /* identify the codec: try theora */
515 if(!theora_p && theora_decode_header(&tf->ti, &tf->tc, &tf->op)>=0)
518 memcpy(&tf->to, &test, sizeof(test));
520 // find out granule shift - from liboggz's oggz_auto.c
521 unsigned char * header = tf->op.packet;
522 theora_keyframe_granule_shift = (char) ((header[40] & 0x03) << 3);
523 theora_keyframe_granule_shift |= (header[41] & 0xe0) >> 5;
525 } else if(!vorbis_p && vorbis_synthesis_headerin(&tf->vi, &tf->vc, &tf->op)>=0)
528 memcpy(&tf->vo, &test, sizeof(test));
532 /* whatever it is, we don't care about it */
533 ogg_stream_clear(&test);
536 /* fall through to non-bos page parsing */
540 /* we're expecting more header packets. */
541 while((theora_p && theora_p < 3) || (vorbis_p && vorbis_p < 3))
545 /* look for further theora headers */
546 while(theora_p && (theora_p < 3) && (ret = ogg_stream_packetout(&tf->to, &tf->op)))
550 eprintf(_("FileOGG: Error parsing Theora stream headers; corrupt stream?\n"));
553 if(theora_decode_header(&tf->ti, &tf->tc, &tf->op))
555 eprintf(_("FileOGG: Error parsing Theora stream headers; corrupt stream?\n"));
563 /* look for more vorbis header packets */
564 while(vorbis_p && (vorbis_p < 3) && (ret = ogg_stream_packetout(&tf->vo, &tf->op)))
568 eprintf(_("FileOGG: Error parsing Vorbis stream headers; corrupt stream?\n"));
571 if (vorbis_synthesis_headerin(&tf->vi, &tf->vc, &tf->op))
573 eprintf(_("FileOGG: Error parsing Vorbis stream headers; corrupt stream?\n"));
581 if ((!vorbis_p || vorbis_p == 3) && (!theora_p || theora_p == 3))
583 /* The header pages/packets will arrive before anything else we
584 care about, or the stream is not obeying spec */
586 if(take_page_out_autoadvance(stream, &oy, &tf->og) > 0)
588 // queue_page(&tf->og); /* demux into the appropriate stream */
589 if(theora_p) ogg_stream_pagein(&tf->to, &tf->og);
590 if(vorbis_p) ogg_stream_pagein(&tf->vo, &tf->og);
594 eprintf(_("FileOGG: End of file while searching for codec headers.\n"));
598 // Remember where the real data begins for later seeking purposes
599 filedata_begin = oy.file_pagepos;
603 /* and now we have it all. initialize decoders */
608 // WORKAROUND for bug in alpha4 version of theora:
609 tf->td.internal_encode = 0;
611 ret = theora_decode_init(&tf->td, &tf->ti);
612 if( ret ) printf("theora_decode_init ret=%d\n", ret);
613 double fps = (double)tf->ti.fps_numerator/tf->ti.fps_denominator;
614 /* printf("FileOGG: Ogg logical stream %x is Theora %dx%d %.02f fps\n",
615 (unsigned int)tf->to.serialno, tf->ti.width, tf->ti.height,
619 Not yet available in alpha4, we assume 420 for now
621 switch(tf->ti.pixelformat)
623 case OC_PF_420: printf(" 4:2:0 video\n"); break;
624 case OC_PF_422: printf(" 4:2:2 video\n"); break;
625 case OC_PF_444: printf(" 4:4:4 video\n"); break;
628 printf(" video\n (UNKNOWN Chroma sampling!)\n");
633 theora_cmodel = BC_YUV420P;
635 if(tf->ti.width!=tf->ti.frame_width || tf->ti.height!=tf->ti.frame_height)
637 eprintf(_("Frame content is %dx%d with offset (%d,%d), We do not support this yet. You will get black border.\n"),
638 tf->ti.frame_width, tf->ti.frame_height, tf->ti.offset_x, tf->ti.offset_y);
640 tf->videosync = new sync_window_t;
641 ogg_sync_init(&tf->videosync->sync);
642 tf->videosync->wlen = 0;
644 ret = ogg_get_first_page(tf->videosync, tf->to.serialno, &tf->videopage);
645 if( !ret ) printf("ogg_get_first_page ret=%d\n", ret);
648 // we have headers already decoded, so just skip them
649 ogg_stream_reset(&tf->to);
650 ogg_stream_pagein(&tf->to, &tf->videopage);
653 while (ogg_stream_packetpeek(&tf->to, NULL) != 1)
655 if (!ogg_get_next_page(tf->videosync, tf->to.serialno, &tf->videopage))
657 printf(_("FileOGG: Cannot find next page while looking for first non-header packet\n"));
660 ogg_stream_pagein(&tf->to, &tf->videopage);
662 ogg_stream_packetout(&tf->to, &op);
663 if (!theora_packet_isheader(&op))
666 // now get to the page of the finish of the first packet
667 while (ogg_page_packets(&tf->videopage) == 0)
669 if (ogg_page_granulepos(&tf->videopage) != -1)
671 printf(_("FileOGG: Broken ogg file - broken page: ogg_page_packets == 0 and granulepos != -1\n"));
674 ogg_get_next_page(tf->videosync, tf->to.serialno, &tf->videopage);
676 // FIXME - LOW PRIORITY - start counting at first decodable keyframe!
677 // but then we have to fix a/v sync somehow
678 start_frame = (int64_t) (theora_granule_frame (&tf->td, ogg_page_granulepos(&tf->videopage)) - ogg_page_packets(&tf->videopage)) + 1;
680 ret = ogg_get_last_page(tf->videosync, tf->to.serialno, &tf->videopage);
681 last_frame = (int64_t) (theora_granule_frame (&tf->td, ogg_page_granulepos(&tf->videopage)));
682 asset->video_length = last_frame - start_frame + 1;
683 // printf("FileOGG:: first frame: %lli, last frame: %lli, video length: %lli\n", start_frame, last_frame, asset->video_length);
686 // FIXME - LOW PRIORITY Cinelerra does not honor the frame_width and frame_height
687 asset->width = tf->ti.width;
688 asset->height = tf->ti.height;
689 // Don't want a user configured frame rate to get destroyed
690 if(!asset->frame_rate)
691 asset->frame_rate = fps;
692 // All theora material is noninterlaced by definition
693 if(!asset->interlace_mode)
694 asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
696 /* ogg_get_page_of_frame(tf->videosync, tf->to.serialno, &og, 0 +start_frame);
697 ogg_get_page_of_frame(tf->videosync, tf->to.serialno, &og, 1 +start_frame);
698 ogg_get_page_of_frame(tf->videosync, tf->to.serialno, &og, 5 +start_frame);
699 ogg_get_page_of_frame(tf->videosync, tf->to.serialno, &og, 206 +start_frame);
700 ogg_get_page_of_frame(tf->videosync, tf->to.serialno, &og, 207 +start_frame);
701 ogg_get_page_of_frame(tf->videosync, tf->to.serialno, &og, 208 +start_frame);
704 ogg_seek_to_keyframe(tf->videosync, tf->to.serialno, 0 + start_frame, &kf);
705 ogg_seek_to_keyframe(tf->videosync, tf->to.serialno, 1 + start_frame, &kf);
706 ogg_seek_to_keyframe(tf->videosync, tf->to.serialno, 5 + start_frame, &kf);
707 ogg_seek_to_keyframe(tf->videosync, tf->to.serialno, 66 + start_frame, &kf);
708 //printf("Keyframe: %lli\n", kf);
709 ogg_seek_to_keyframe(tf->videosync, tf->to.serialno, 1274 + start_frame, &kf);
712 set_video_position(0); // make sure seeking is done to the first sample
713 ogg_frame_position = -10;
714 asset->video_data = 1;
715 strncpy(asset->vcodec, "theo", 4);
718 // report_colorspace(&ti);
719 // dump_comments(&tc);
722 /* tear down the partial theora setup */
723 theora_info_clear(&tf->ti);
724 theora_comment_clear(&tf->tc);
730 vorbis_synthesis_init(&tf->vd, &tf->vi);
731 vorbis_block_init(&tf->vd, &tf->vb);
732 /* eprintf(_("FileOGG: Ogg logical stream %x is Vorbis %d channel %d Hz audio.\n"),
733 (unsigned int)tf->vo.serialno, tf->vi.channels, (int)tf->vi.rate);
735 /* init audio_sync structure */
736 tf->audiosync = new sync_window_t;
737 ogg_sync_init(&tf->audiosync->sync);
738 tf->audiosync->wlen = 0;
740 ogg_get_first_page(tf->audiosync, tf->vo.serialno, &tf->audiopage);
742 ogg_stream_reset(&tf->vo);
743 // FIXME - LOW PRIORITY should be getting pages until one has granulepos,
744 // probably never happens in pracitce
745 ogg_ret2 = ogg_stream_pagein(&tf->vo, &tf->audiopage);
746 ogg_int64_t accumulated = 0;
749 while((result = ogg_stream_packetout(&tf->vo, &op)))
753 long thisblock = vorbis_packet_blocksize(&tf->vi, &op);
755 accumulated += (lastblock + thisblock) >> 2;
756 lastblock = thisblock;
759 start_sample = ogg_page_granulepos(&tf->audiopage) - accumulated;
761 printf("Begin: %lli, To byte: %lli, granule: %lli, serialno: %x\n",
762 tf->audiosync->file_pagepos_found,
763 tf->audiosync->file_pagepos_found + tf->audiopage.body_len + tf->audiopage.header_len,
764 ogg_page_granulepos(&tf->audiopage),
765 ogg_page_serialno(&tf->audiopage));
766 while (ogg_get_next_page(tf->audiosync, tf->vo.serialno, &tf->audiopage))
767 printf("At byte: %lli, To byte: %lli, granule: %lli, serialno: %x\n",
768 tf->audiosync->file_pagepos_found,
769 tf->audiosync->file_pagepos_found + tf->audiopage.body_len + tf->audiopage.header_len,
770 ogg_page_granulepos(&tf->audiopage),
771 ogg_page_serialno(&tf->audiopage));
774 ogg_ret1 = ogg_get_last_page(tf->audiosync, tf->vo.serialno, &tf->audiopage);
775 last_sample = ogg_page_granulepos(&tf->audiopage);
776 asset->audio_length = last_sample - start_sample;
778 /* printf("FileOGG:: First sample: %lli, last_sample: %lli\n", start_sample, last_sample);
779 printf("FileOGG:: audio length: samples: %lli, playing time: %f\n", asset->audio_length, 1.0 * asset->audio_length / tf->vi.rate);
781 /* printf("End: %lli, To byte: %lli, granule: %lli, serialno: %x\n",
782 tf->audiosync->file_pagepos_found,
783 tf->audiosync->file_pagepos_found + tf->audiopage.body_len + tf->audiopage.header_len,
784 ogg_page_granulepos(&tf->audiopage),
785 ogg_page_serialno(&tf->audiopage));
786 while (ogg_get_prev_page(tf->audiosync, tf->vo.serialno, &tf->audiopage))
787 printf("At byte: %lli, To byte: %lli, granule: %lli, serialno: %x\n",
788 tf->audiosync->file_pagepos_found,
789 tf->audiosync->file_pagepos_found + tf->audiopage.body_len + tf->audiopage.header_len,
790 ogg_page_granulepos(&tf->audiopage),
791 ogg_page_serialno(&tf->audiopage));
794 /* ogg_get_page_of_sample(tf->audiosync, tf->vo.serialno, &tf->audiopage, 0);
795 ogg_get_page_of_sample(tf->audiosync, tf->vo.serialno, &tf->audiopage, 1);
796 ogg_get_page_of_sample(tf->audiosync, tf->vo.serialno, &tf->audiopage, 50);
797 ogg_get_page_of_sample(tf->audiosync, tf->vo.serialno, &tf->audiopage, 5000);
798 ogg_get_page_of_sample(tf->audiosync, tf->vo.serialno, &tf->audiopage, 30000);
799 ogg_get_page_of_sample(tf->audiosync, tf->vo.serialno, &tf->audiopage, 50000);
800 ogg_get_page_of_sample(tf->audiosync, tf->vo.serialno, &tf->audiopage, 90000);
801 ogg_get_page_of_sample(tf->audiosync, tf->vo.serialno, &tf->audiopage, 95999);
802 ogg_get_page_of_sample(tf->audiosync, tf->vo.serialno, &tf->audiopage, 96000);
803 ogg_seek_to_sample(tf->audiosync, tf->vo.serialno, 0);
804 ogg_seek_to_sample(tf->audiosync, tf->vo.serialno, 1);
805 ogg_seek_to_sample(tf->audiosync, tf->vo.serialno, 5000);
806 ogg_seek_to_sample(tf->audiosync, tf->vo.serialno, 30000);
807 ogg_seek_to_sample(tf->audiosync, tf->vo.serialno, 50000);
808 ogg_seek_to_sample(tf->audiosync, tf->vo.serialno, 90000);
809 ogg_seek_to_sample(tf->audiosync, tf->vo.serialno, 95999);
810 ogg_seek_to_sample(tf->audiosync, tf->vo.serialno, 96000);
812 asset->channels = tf->vi.channels;
813 if(!asset->sample_rate)
814 asset->sample_rate = tf->vi.rate;
815 asset->audio_data = 1;
818 ogg_sample_position = -10;
819 set_audio_position(0); // make sure seeking is done to the first sample
820 strncpy(asset->acodec, "vorb", 4);
825 /* tear down the partial vorbis setup */
826 vorbis_info_clear(&tf->vi);
827 vorbis_comment_clear(&tf->vc);
830 ogg_sync_clear(&oy.sync);
836 int FileOGG::ogg_get_prev_page(sync_window_t *sw, long serialno, ogg_page *og)
839 off_t filepos = sw->file_pagepos_found - READ_SIZE;
840 int first_page_offset = 0;
842 int read_len = READ_SIZE;
844 // printf("fp: %lli pagepos found: %lli\n", filepos, sw->file_pagepos_found);
849 // read_len = read_len - (filedata_begin - filepos);
850 read_len = read_len + filepos;
855 int have_read = read_buffer_at(stream, sw, read_len, filepos);
857 // printf("reading at %lli, len: %i, read: %i, pagepos: %lli, pageposfound: %lli\n", filepos, read_len, have_read, sw->file_pagepos, sw->file_pagepos_found);
858 // printf("Buffer position: %lli\n", sw->file_bufpos);
859 // printf("SS: storage: %i, fill: %i, returned: %i\n", sw->sync.storage, sw->sync.fill, sw->sync.returned);
860 // printf("US: unsynced%i, headrebytes: %i, bodybyes: %i\n", sw->sync.unsynced, sw->sync.headerbytes, sw->sync.bodybytes);
861 // printf("data: %c %c %c %c\n", sw->sync.data[0], sw->sync.data[1], sw->sync.data[2], sw->sync.data[3]);
865 // read all pages in the buffer
869 while (first_page || page_length)
871 // if negative, skip bytes
872 while ((page_length = sync_and_take_page_out(sw, &page)) < 0)
874 page_offset -= page_length;
877 // printf("BBBb page_len: %i\n", page_length);
879 // if (filepos == 0 && page_length)
881 // printf("AAAAAAAAAAAAAAAAAAAAAAAAAaaa\n");
882 // printf("pp: %lli %x\n", sw->file_pagepos, ogg_page_serialno(&page));
885 first_page_offset = page_offset;
887 // printf("pl: %i, serial: %x iscem: %x\n", page_length, ogg_page_serialno(&page), serialno);
888 if (page_length && ogg_page_serialno(&page) == serialno)
890 // we will copy every page until last page in this buffer
893 sw->file_pagepos_found = sw->file_pagepos - page.header_len - page.body_len;
894 // printf("got it : %lli %i %i\n", sw->file_pagepos, page.header_len, page.body_len);
895 memcpy(og, &page, sizeof(page));
898 // printf("fpo: %i\n", first_page_offset);
899 filepos += first_page_offset - READ_SIZE;
902 // printf("finished\n");
909 int FileOGG::ogg_get_last_page(sync_window_t *sw, long serialno, ogg_page *og)
912 off_t filepos = file_length - READ_SIZE;
916 int first_page_offset = 0;
918 while (!done && filepos >= 0)
921 read_buffer_at(stream, sw, READ_SIZE, filepos);
923 // read all pages in the buffer
927 while (first_page || page_length)
929 // if negative, skip bytes
930 while ((page_length = sync_and_take_page_out(sw, &page)) < 0)
931 page_offset -= page_length;
933 first_page_offset = page_offset;
935 if (page_length && ogg_page_serialno(&page) == serialno)
937 // we will copy every page until last page in this buffer
939 sw->file_pagepos_found = sw->file_pagepos - page.header_len - page.body_len;
940 memcpy(og, &page, sizeof(page));
943 filepos = filepos + first_page_offset - READ_SIZE;
952 int FileOGG::ogg_get_first_page(sync_window_t *sw, long serialno, ogg_page *og)
954 // printf("FileOGG:: Seeking to first page at %lli\n", filedata_begin);
955 read_buffer_at(stream, sw, READ_SIZE, 0);
956 // we don't even need to sync since we _know_ it is right
957 return (ogg_get_next_page(sw, serialno, og));
960 int FileOGG::ogg_seek_to_databegin(sync_window_t *sw, long serialno)
963 // printf("FileOGG:: Seeking to first page at %lli\n", filedata_begin);
964 read_buffer_at(stream, sw, READ_SIZE, filedata_begin);
965 // we don't even need to sync since we _know_ it is right
969 int FileOGG::ogg_get_next_page(sync_window_t *sw, long serialno, ogg_page *og)
971 while (take_page_out_autoadvance(stream, sw, og) > 0)
973 if (ogg_page_serialno(og) == serialno)
975 sw->file_pagepos_found = sw->file_pagepos - og->header_len - og->body_len;
982 int FileOGG::ogg_sync_and_get_next_page(sync_window_t *sw, long serialno, ogg_page *og)
984 // TODO: Put better error reporting inhere
986 while ((ret = sync_and_take_page_out(sw, og)) < 0)
992 if (ogg_page_serialno(og) == serialno)
994 sw->file_pagepos_found = sw->file_pagepos - og->header_len - og->body_len;
997 while (ogg_get_next_page(sw, serialno, og))
998 if (ogg_page_serialno(og) == serialno)
1000 sw->file_pagepos_found = sw->file_pagepos - og->header_len - og->body_len;
1007 // >= 0, number of sample within a page
1009 int FileOGG::ogg_get_page_of_sample(sync_window_t *sw, long serialno, ogg_page *og, int64_t sample)
1011 // First make an educated guess about position
1012 if (sample >= asset->audio_length + start_sample)
1014 printf(_("FileOGG: Illegal seek beyond end of samples\n"));
1017 off_t educated_guess = filedata_begin + (file_length - filedata_begin) * (sample - start_sample) / asset->audio_length - READ_SIZE;
1018 if (educated_guess < 0)
1020 // printf("My educated guess: %lli\n", educated_guess);
1021 // now see if we won
1022 read_buffer_at(stream, sw, READ_SIZE, educated_guess);
1023 ogg_sync_and_get_next_page(sw, serialno, og);
1024 int64_t end_sample = ogg_page_granulepos(og);
1025 // linear seek to the sample
1026 int64_t start_sample = 0;
1027 // TODO: Use bisection also
1028 // printf("Next page granulepos: %lli, pagepos: %lli\n", end_sample, sw->file_pagepos_found);
1029 if (end_sample <= sample)
1032 while (end_sample <= sample)
1034 ogg_get_next_page(sw, serialno, og);
1035 start_sample = end_sample;
1036 end_sample = ogg_page_granulepos(og);
1038 ogg_get_prev_page(sw, serialno, og);
1039 while (ogg_page_continued(og) && ogg_page_packets(og) == 1)
1040 ogg_get_prev_page(sw, serialno, og);
1044 start_sample = end_sample;
1045 while (start_sample > sample || (ogg_page_continued(og) &&
1046 ogg_page_packets(og) == 1))
1048 // printf("get prev page: %lli pagepos:%lli\n", ogg_page_granulepos(og), sw->file_pagepos_found);
1049 ogg_get_prev_page(sw, serialno, og);
1050 end_sample = start_sample;
1051 start_sample = ogg_page_granulepos(og);
1053 // go forward one page at the end
1057 // printf("For sample %lli we need to start decoding on page with granulepos: %lli\n", sample, ogg_page_granulepos(og));
1061 // seeks, so next sample returned will be the correct one
1062 // sample here is still the vorbis sample number (= cinelerra sample number + start_sample)
1063 // reinits the decoding engine
1065 int FileOGG::ogg_seek_to_sample(sync_window_t *sw, long serialno, int64_t sample)
1067 // MAYBE FIXME - find out if we really are decoding previous two packets or not
1068 // get to the sample
1071 // printf("Calling get page of sample\n");
1072 if (!ogg_get_page_of_sample(sw, serialno, &og, sample))
1074 eprintf(_("FileOGG: Seeking to sample's page failed\n"));
1077 // printf("Pagepos: %lli\n", sw->file_pagepos);
1078 vorbis_synthesis_restart(&tf->vd);
1079 ogg_stream_reset(&tf->vo);
1080 ogg_stream_pagein(&tf->vo, &og);
1082 // printf("seeking to sample : %lli , starting at page with gpos: %lli\n", sample, ogg_page_granulepos(&og));
1084 int64_t current_comming_sample = -1;
1088 // make sure we have a packet ready
1089 while (ogg_stream_packetpeek(&tf->vo, NULL) != 1)
1091 if (!ogg_get_next_page(sw, serialno, &og))
1093 eprintf(_("FileOGG: Cannot find next page while seeking\n"));
1096 ogg_stream_pagein(&tf->vo, &og);
1098 ogg_stream_packetout(&tf->vo, &op);
1102 if(!vorbis_synthesis(&tf->vb, &op))
1104 ogg_ret0 = vorbis_synthesis_blockin(&tf->vd, &tf->vb);
1105 int64_t previous_comming_sample = current_comming_sample;
1106 current_comming_sample += vorbis_synthesis_pcmout(&tf->vd, NULL);
1107 if (current_comming_sample > sample)
1109 if (previous_comming_sample > sample)
1111 eprintf(_("Ogg decoding error while seeking sample\n"));
1113 vorbis_synthesis_read(&tf->vd, (sample - previous_comming_sample));
1114 // printf("WE GOT IT, samples already decoded: %jd\n", vorbis_synthesis_pcmout(&tf->vd,NULL));
1115 return 1; // YAY next sample read is going to be ours, sexy!
1118 // discard decoded data before current sample
1119 vorbis_synthesis_read(&tf->vd, (current_comming_sample - previous_comming_sample));
1124 if (!sync && op.granulepos >= 0)
1127 current_comming_sample = op.granulepos;
1128 if(!vorbis_synthesis(&tf->vb, &op))
1130 vorbis_synthesis_blockin(&tf->vd, &tf->vb);
1131 if (vorbis_synthesis_pcmout(&tf->vd, NULL) != 0)
1133 eprintf(_("FileOGG: Something wrong while trying to seek\n"));
1146 int FileOGG::ogg_get_page_of_frame(sync_window_t *sw, long serialno, ogg_page *og, int64_t frame)
1148 if (frame >= asset->video_length + start_frame)
1150 eprintf(_("FileOGG: Illegal seek beyond end of frames\n"));
1153 // printf("frame: %lli start frame: %lli\n", frame, start_frame);
1154 // printf("file_length: %lli filedata_begin: %lli\n", file_length, filedata_begin);
1155 off_t educated_guess = filedata_begin + (file_length - filedata_begin) * (frame - start_frame) / asset->video_length - READ_SIZE/2;
1156 // educated_guess += 100000;
1157 if (educated_guess > file_length - READ_SIZE)
1158 educated_guess = file_length - READ_SIZE;
1159 if (educated_guess < filedata_begin)
1160 educated_guess = filedata_begin;
1161 // printf("My educated guess: %lli\n", educated_guess);
1162 // now see if we won
1163 read_buffer_at(stream, sw, READ_SIZE, educated_guess);
1164 if( !ogg_sync_and_get_next_page(sw, serialno, og) ) {
1165 printf(_("FileOGG: ogg_sync_and_get_next_page failed\n"));
1168 int64_t pageend_frame;
1169 //int read_back = 0;
1170 // find the page with "real" ending
1171 while ((pageend_frame = ogg_page_granulepos(og)) == -1)
1173 if (ogg_get_next_page(sw, serialno, og) == 0)
1179 pageend_frame = theora_granule_frame(&tf->td, ogg_page_granulepos(og));
1181 // FIXME - MEDIUM PRIORITY: read back if we've gone too far and no page of our serialno at all can be found
1184 // linear seek to the sample
1185 // TODO: Use bisection also
1186 // printf("Next page granulepos: %lli, pagepos: %lli\n", end_sample, sw->file_pagepos_found);
1187 //int discard_packets = 0;
1190 if (pageend_frame <= frame)
1193 while (pageend_frame < frame)
1196 ogg_get_next_page(sw, serialno, og);
1197 } while (ogg_page_packets(og) == 0);
1198 pageend_frame = theora_granule_frame(&tf->td, ogg_page_granulepos(og));
1201 // go back if this frame starts on previous page
1202 if (ogg_page_continued(og) && pageend_frame - ogg_page_packets(og) == frame - 1)
1205 ogg_get_prev_page(sw, serialno, og);
1206 } while (ogg_page_packets(og) == 0 && ogg_page_continued(og));
1208 pageend_frame = theora_granule_frame(&tf->td, ogg_page_granulepos(og));
1212 int64_t first_frame_on_page = theora_granule_frame(&tf->td, ogg_page_granulepos(og)) - ogg_page_packets(og) + 2;
1213 if (!ogg_page_continued(og))
1214 first_frame_on_page--;
1215 while (first_frame_on_page > frame)
1217 // printf("get prev page: %lli pagepos:%lli\n", ogg_page_granulepos(og), sw->file_pagepos_found);
1219 ogg_get_prev_page(sw, serialno, og);
1220 } while (ogg_page_packets(og) == 0 && ogg_page_continued(og));
1222 // pageend_frame = theora_granule_frame(&tf->td, ogg_page_granulepos(og));
1223 first_frame_on_page = theora_granule_frame(&tf->td, ogg_page_granulepos(og)) - ogg_page_packets(og) + 2;
1224 if (!ogg_page_continued(og))
1225 first_frame_on_page--;
1228 // printf("Miss plus: %i, miss minus: %i\n", missp, missm);
1229 // printf("last frame of page with frame : %lli\n", pageend_frame);
1234 int FileOGG::ogg_seek_to_keyframe(sync_window_t *sw, long serialno, int64_t frame, int64_t *position)
1236 //printf("seek %jd\n", frame);
1240 if( !ogg_get_page_of_frame(sw, serialno, &og, frame) ) {
1241 eprintf(_("FileOGG: seek to frame failed\n"));
1244 // find a page with packets
1245 while( ogg_page_packets(&og) == 0 ) {
1246 ogg_get_prev_page(sw, serialno, &og);
1248 int64_t granulepos = ogg_page_granulepos(&og);
1249 granulepos &= ~((1<<theora_keyframe_granule_shift)-1);
1250 int64_t iframe = theora_granule_frame(&tf->td, granulepos);
1251 // iframe based on granulepos
1252 if( frame < iframe || !ogg_get_page_of_frame(sw, serialno, &og, iframe) ) {
1253 eprintf(_("FileOGG: seek to iframe failed\n"));
1256 while( ogg_page_packets(&og) == 0 ) {
1257 ogg_get_prev_page(sw, serialno, &og);
1259 int64_t pageend_frame = theora_granule_frame(&tf->td, ogg_page_granulepos(&og));
1260 int64_t frames_on_page = ogg_page_packets(&og);
1261 if( ogg_page_continued(&og) ) --frames_on_page;
1262 // get frame before page with with iframe
1263 int64_t page_frame = pageend_frame - frames_on_page;
1264 if( page_frame < 0 ) page_frame = 0;
1265 //printf("iframe %jd, page_frame %jd, frames_on_page %jd\n", iframe, page_frame, frames_on_page);
1266 ogg_stream_reset(&tf->to);
1267 ogg_stream_pagein(&tf->to, &og);
1269 while( ++page_frame < iframe ) {
1270 while( ogg_stream_packetpeek(&tf->to, NULL) != 1 ) {
1271 if( !ogg_get_next_page(sw, serialno, &og) ) {
1272 eprintf(_("FileOGG: Cannot find next page while seeking\n"));
1275 ogg_stream_pagein(&tf->to, &og);
1277 ogg_stream_packetout(&tf->to, &op);
1280 *position = iframe - 1;
1285 int FileOGG::check_sig(Asset *asset)
1288 FILE *fd = fopen(asset->path, "rb");
1291 fseek(fd, 0, SEEK_SET);
1294 (void)fread(data, 4, 1, fd);
1296 if(data[0] == 'O' &&
1303 // printf("Yay, we have an ogg file\n");
1313 int FileOGG::close_file()
1320 if (asset->audio_data)
1321 write_samples_vorbis(0, 0, 1); // set eos
1322 if (asset->video_data)
1323 write_frames_theora(0, 1, 1); // set eos
1325 flush_ogg(1); // flush all
1327 if (asset->audio_data)
1329 vorbis_block_clear (&tf->vb);
1330 vorbis_dsp_clear (&tf->vd);
1331 vorbis_info_clear (&tf->vi);
1332 ogg_stream_clear (&tf->vo);
1334 if (asset->video_data)
1336 theora_info_clear (&tf->ti);
1337 ogg_stream_clear (&tf->to);
1338 theora_clear (&tf->td);
1341 if (stream) fclose(stream);
1347 if (asset->audio_data)
1349 vorbis_block_clear (&tf->vb);
1350 vorbis_dsp_clear (&tf->vd);
1351 vorbis_comment_clear (&tf->vc);
1352 vorbis_info_clear (&tf->vi);
1353 ogg_stream_clear (&tf->vo);
1355 theora_comment_clear(&tf->tc);
1356 if (asset->video_data)
1358 theora_info_clear (&tf->ti);
1359 theora_comment_clear (&tf->tc);
1360 theora_clear (&tf->td);
1361 ogg_stream_clear (&tf->to);
1365 if (stream) fclose(stream);
1372 int FileOGG::close_file_derived()
1374 //printf("FileOGG::close_file_derived(): 1\n");
1375 if (stream) fclose(stream);
1380 int64_t FileOGG::get_video_position()
1383 return next_frame_position - start_frame;
1386 int64_t FileOGG::get_audio_position()
1388 return next_sample_position - start_sample;
1391 int FileOGG::set_video_position(int64_t x)
1394 // printf("SVP: %lli\n", x);
1396 next_frame_position = x + start_frame;
1401 int FileOGG::colormodel_supported(int colormodel)
1405 if (colormodel == BC_YUV420P)
1410 int FileOGG::get_best_colormodel(Asset *asset, int driver)
1417 int FileOGG::read_frame(VFrame *frame)
1420 if(!stream) return 1;
1423 // skip is cheaper than seek, do it...
1424 int decode_frames = 0;
1425 int expect_keyframe = 0;
1426 if (ogg_frame_position >= 0 &&
1427 next_frame_position >= ogg_frame_position &&
1428 next_frame_position - ogg_frame_position < 32)
1430 decode_frames = next_frame_position - ogg_frame_position;
1432 if (next_frame_position != ogg_frame_position)
1434 if (!ogg_seek_to_keyframe(tf->videosync, tf->to.serialno,
1435 next_frame_position, &ogg_frame_position)) {
1436 eprintf(_("FileOGG:: Error while seeking to frame's keyframe"
1437 " (frame: %jd, keyframe: %jd)\n"),
1438 next_frame_position, ogg_frame_position);
1441 // printf("For frame: %lli, keyframe is: %lli\n", next_frame_position,ogg_frame_position);
1442 // skip frames must be > 0 here
1443 decode_frames = next_frame_position - ogg_frame_position;
1444 if (decode_frames <= 0)
1446 eprintf(_("FileOGG:: Error while seeking to keyframe,"
1447 " wrong keyframe number (frame: %jd, keyframe: %jd)\n"),
1448 next_frame_position, ogg_frame_position);
1452 expect_keyframe = 1;
1455 // printf("Frames to decode: %i\n", decode_frames);
1457 // THIS IS WHAT CAUSES SLOWNESS OF SEEKING, but we can do nothing about it.
1459 while (decode_frames > 0)
1463 while (ogg_stream_packetpeek(&tf->to, NULL) != 1)
1465 if (!ogg_get_next_page(tf->videosync, tf->to.serialno, &og))
1467 eprintf(_("FileOGG: Cannot find next page while seeking\n"));
1470 ogg_stream_pagein(&tf->to, &og);
1472 ogg_stream_packetout(&tf->to, &op);
1473 if( theora_packet_isheader(&op) ) continue;
1474 //printf("frame %jd, key %d\n", ogg_frame_position, theora_packet_iskeyframe(&op));
1475 if (expect_keyframe && !theora_packet_iskeyframe(&op))
1477 eprintf(_("FileOGG: Expecting keyframe, but didn't get it\n"));
1478 // return 1; this is generally not a fatal error
1480 expect_keyframe = 0;
1483 ret = theora_decode_packetin(&tf->td, &op);
1484 //if(ret < 0 )printf("ret = %d\n", ret);
1486 ogg_frame_position ++;
1490 int ret = theora_decode_YUVout (&tf->td, &yuv);
1493 eprintf(_("FileOGG: theora_decode_YUVout failed with code %i\n"), ret);
1497 /* yuv.y += yuv.y_stride * (yuv.y_height - 1);
1498 yuv.u += yuv.uv_stride * (yuv.uv_height - 1);
1499 yuv.v += yuv.uv_stride * (yuv.uv_height - 1);
1500 yuv.y_stride = - yuv.y_stride;
1501 yuv.uv_stride = - yuv.uv_stride;*/
1502 VFrame *temp_frame = new VFrame(yuv.y, -1, 0,
1503 yuv.u - yuv.y, yuv.v - yuv.y, - yuv.y_stride,
1504 yuv.y_height, BC_YUV420P, - yuv.y_stride);
1505 // copy into temp frame...
1507 BC_CModels::transfer(frame->get_rows(),
1508 temp_frame->get_rows(),
1512 temp_frame->get_y(),
1513 temp_frame->get_u(),
1514 temp_frame->get_v(),
1521 yuv.y_width, // temp_frame can be larger than frame if width not dividable by 16
1524 frame->get_color_model(),
1526 -temp_frame->get_w(),
1531 next_frame_position ++;
1538 int FileOGG::ogg_decode_more_samples(sync_window_t *sw, long serialno)
1545 while (ogg_stream_packetpeek(&tf->vo, NULL) != 1)
1547 if (!ogg_get_next_page(sw, serialno, &og))
1549 eprintf(_("FileOGG: Cannot find next page while trying to decode more samples\n"));
1552 ogg_stream_pagein(&tf->vo, &og);
1554 ogg_stream_packetout(&tf->vo, &op);
1555 if(!vorbis_synthesis(&tf->vb, &op))
1558 vorbis_synthesis_blockin(&tf->vd, &tf->vb);
1564 int FileOGG::set_audio_position(int64_t x)
1566 next_sample_position = x + start_sample;
1570 int FileOGG::move_history(int from, int to, int len)
1573 for(int i = 0; i < asset->channels; i++)
1574 memmove(pcm_history[i] + to, pcm_history[i] + from, sizeof(float) * len);
1576 history_start = history_start + from - to;
1577 if( history_start < 0 ) history_start = 0;
1581 int FileOGG::read_samples(double *buffer, int64_t len)
1583 float **vorbis_buffer;
1586 // printf("Reading samples: Channel: %i, number of samples: %lli, reading at :%lli\n", file->current_channel, len, next_sample_position);
1587 // printf("\tnext_sample_position: %lli, length: %i\n", next_sample_position, len);
1588 // printf("\thistory_start: %lli, length: %i\n", history_start, history_size);
1590 if(len > HISTORY_MAX)
1592 eprintf(_("max samples=%d\n"), HISTORY_MAX);
1598 pcm_history = new float*[asset->channels];
1599 for(int i = 0; i < asset->channels; i++)
1600 pcm_history[i] = new float[HISTORY_MAX];
1601 history_start = -100000000; // insane value to trigger reload
1605 int64_t hole_start = -1;
1606 int64_t hole_len = -1;
1607 int64_t hole_absstart = -1;
1608 int64_t hole_fill = 0;
1610 if (history_start < next_sample_position && history_start + history_size > next_sample_position && history_start + history_size < next_sample_position + len)
1614 hole_start = history_start + history_size - next_sample_position;
1615 hole_len = history_size - hole_start;
1616 hole_absstart = next_sample_position + hole_start;
1617 move_history(next_sample_position - history_start,
1622 if (next_sample_position < history_start && history_start < next_sample_position + len)
1627 hole_len = history_start - next_sample_position;
1628 hole_absstart = next_sample_position;
1629 // printf("hs: %lli, histstart: %lli, next_sample_position: %lli\n", history_size, history_start, next_sample_position);
1630 // printf("to: 0, from: %lli, size: %lli\n",
1631 // history_start - next_sample_position,
1632 // history_size - history_start + next_sample_position);
1634 history_start - next_sample_position,
1635 history_size - history_start + next_sample_position);
1638 if (next_sample_position >= history_start + history_size || next_sample_position + len <= history_start)
1643 hole_len = HISTORY_MAX;
1644 hole_absstart = next_sample_position;
1645 history_start = hole_absstart;
1646 history_size = hole_len;
1651 if (hole_start < 0 || hole_len <= 0 || hole_absstart < 0)
1653 eprintf(_("FileOGG: Error at finding out what to read from file\n"));
1657 if (hole_absstart + hole_len > asset->audio_length + start_sample)
1659 hole_len = asset->audio_length + start_sample - hole_absstart;
1660 history_size = asset->audio_length + start_sample - history_start;
1663 history_size = HISTORY_MAX;
1667 // printf("Decode samples at position: %lli, samples to read: %lli\n", hole_absstart, hole_len);
1669 int64_t samples_read = 0;
1670 if (ogg_sample_position != hole_absstart)
1672 ogg_sample_position = hole_absstart;
1673 if (!ogg_seek_to_sample(tf->audiosync, tf->vo.serialno, ogg_sample_position))
1675 eprintf(_("Error while seeking to sample\n"));
1679 // now we have ogg_sample_positon aligned
1680 int64_t samples_to_read = hole_len;
1681 while (samples_read < hole_len)
1683 int64_t waiting_samples = vorbis_synthesis_pcmout(&tf->vd, &vorbis_buffer);
1684 int64_t takeout_samples;
1685 if (waiting_samples > samples_to_read - samples_read)
1686 takeout_samples = samples_to_read - samples_read;
1688 takeout_samples = waiting_samples;
1690 // printf("takeout samples: %lli, samples_read: %lli\n", takeout_samples, samples_read);
1694 for(int i = 0; i < asset->channels; i++)
1696 float *input = vorbis_buffer[i];
1697 float *output = pcm_history[i] + hole_start;
1699 for(int j = 0; j < takeout_samples ; j++)
1701 output[j] = input[j];
1706 vorbis_synthesis_read(&tf->vd, takeout_samples);
1707 samples_read += takeout_samples;
1708 ogg_sample_position += takeout_samples;
1709 hole_start += takeout_samples;
1711 if (samples_read < hole_len)
1712 if (!ogg_decode_more_samples(tf->audiosync, tf->vo.serialno))
1714 ogg_sample_position = -1;
1722 // now we can be sure our history is correct, just copy it out
1723 if (next_sample_position < history_start || next_sample_position + len > history_start + history_size)
1725 printf(_("FileOGG:: History not aligned properly \n"));
1726 printf(_("\tnext_sample_position: %jd, length: %jd\n"), next_sample_position, len);
1727 printf(_("\thistory_start: %jd, length: %jd\n"), history_start, history_size);
1730 float *input = pcm_history[file->current_channel] + next_sample_position - history_start;
1731 for (int i = 0; i < len; i++)
1732 buffer[i] = input[i];
1734 next_sample_position += len;
1739 int FileOGG::write_audio_page()
1743 ret = fwrite(tf->apage, 1, tf->apage_len, stream);
1744 if(ret < tf->apage_len)
1746 eprintf(_("error writing audio page\n"));
1748 tf->apage_valid = 0;
1749 tf->a_pkg -= ogg_page_packets((ogg_page *)&tf->apage);
1753 int FileOGG::write_video_page()
1757 ret = fwrite(tf->vpage, 1, tf->vpage_len, stream);
1758 if(ret < tf->vpage_len)
1760 eprintf(_("error writing video page\n"));
1762 tf->vpage_valid = 0;
1763 tf->v_pkg -= ogg_page_packets((ogg_page *)&tf->vpage);
1767 void FileOGG::flush_ogg (int e_o_s)
1773 /* flush out the ogg pages */
1775 /* Get pages for both streams, if not already present, and if available.*/
1776 if(asset->video_data && !tf->vpage_valid) {
1777 // this way seeking is much better,
1778 // not sure if 23 packets is a good value. it works though
1780 if(tf->v_pkg>22 && ogg_stream_flush(&tf->to, &og) > 0) {
1783 else if(ogg_stream_pageout(&tf->to, &og) > 0) {
1787 len = og.header_len + og.body_len;
1788 if(tf->vpage_buffer_length < len) {
1789 tf->vpage = (unsigned char *)realloc(tf->vpage, len);
1790 tf->vpage_buffer_length = len;
1792 tf->vpage_len = len;
1793 memcpy(tf->vpage, og.header, og.header_len);
1794 memcpy(tf->vpage+og.header_len , og.body, og.body_len);
1796 tf->vpage_valid = 1;
1797 tf->videotime = theora_granule_time (&tf->td,
1798 ogg_page_granulepos(&og));
1801 if(asset->audio_data && !tf->apage_valid) {
1802 // this way seeking is much better,
1803 // not sure if 23 packets is a good value. it works though
1805 if(tf->a_pkg>22 && ogg_stream_flush(&tf->vo, &og) > 0) {
1808 else if(ogg_stream_pageout(&tf->vo, &og) > 0) {
1812 len = og.header_len + og.body_len;
1813 if(tf->apage_buffer_length < len) {
1814 tf->apage = (unsigned char *)realloc(tf->apage, len);
1815 tf->apage_buffer_length = len;
1817 tf->apage_len = len;
1818 memcpy(tf->apage, og.header, og.header_len);
1819 memcpy(tf->apage+og.header_len , og.body, og.body_len);
1821 tf->apage_valid = 1;
1822 tf->audiotime= vorbis_granule_time (&tf->vd,
1823 ogg_page_granulepos(&og));
1827 if(!asset->audio_data && tf->vpage_valid) {
1830 else if(!asset->video_data && tf->apage_valid) {
1833 /* We're using both. We can output only:
1834 * a) If we have valid pages for both
1835 * b) At EOS, for the remaining stream.
1837 else if(tf->vpage_valid && tf->apage_valid) {
1838 /* Make sure they're in the right order. */
1839 if(tf->videotime <= tf->audiotime)
1844 else if(e_o_s && tf->vpage_valid) {
1847 else if(e_o_s && tf->apage_valid) {
1851 break; /* Nothing more writable at the moment */
1854 flush_lock->unlock();
1858 int FileOGG::write_samples_vorbis(double **buffer, int64_t len, int e_o_s)
1860 float **vorbis_buffer;
1861 static int samples = 0;
1865 vorbis_analysis_wrote (&tf->vd, 0);
1868 vorbis_buffer = vorbis_analysis_buffer (&tf->vd, len);
1869 /* double to float conversion */
1870 for(int i = 0; i<asset->channels; i++)
1872 for (int j = 0; j < len; j++)
1874 vorbis_buffer[i][j] = buffer[i][j];
1877 vorbis_analysis_wrote (&tf->vd, len);
1879 while(vorbis_analysis_blockout (&tf->vd, &tf->vb) == 1)
1881 /* analysis, assume we want to use bitrate management */
1882 vorbis_analysis (&tf->vb, NULL);
1883 vorbis_bitrate_addblock (&tf->vb);
1885 /* weld packets into the bitstream */
1886 while (vorbis_bitrate_flushpacket (&tf->vd, &tf->op))
1889 ogg_stream_packetin (&tf->vo, &tf->op);
1891 flush_lock->unlock();
1901 int FileOGG::write_samples(double **buffer, int64_t len)
1904 return write_samples_vorbis(buffer, len, 0);
1908 int FileOGG::write_frames_theora(VFrame ***frames, int len, int e_o_s)
1910 // due to clumsy theora's design we need to delay writing out by one frame
1911 // always stay one frame behind, so we can correctly encode e_o_s
1913 if(!stream) return 0;
1915 for(int j = 0; j < len && !result; j++)
1917 if (temp_frame) // encode previous frame if available
1920 yuv.y_width = tf->ti.width;
1921 yuv.y_height = tf->ti.height;
1922 yuv.y_stride = temp_frame->get_bytes_per_line();
1924 yuv.uv_width = tf->ti.width / 2;
1925 yuv.uv_height = tf->ti.height / 2;
1926 yuv.uv_stride = temp_frame->get_bytes_per_line() /2;
1928 yuv.y = temp_frame->get_y();
1929 yuv.u = temp_frame->get_u();
1930 yuv.v = temp_frame->get_v();
1931 int ret = theora_encode_YUVin (&tf->td, &yuv);
1934 printf(_("FileOGG: theora_encode_YUVin failed with code %i\n"), ret);
1935 printf("yuv_buffer: y_width: %i, y_height: %i, y_stride: %i,"
1936 " uv_width: %i, uv_height: %i, uv_stride: %i\n",
1937 yuv.y_width, yuv.y_height, yuv.y_stride,
1938 yuv.uv_width, yuv.uv_height, yuv.uv_stride);
1940 while(theora_encode_packetout (&tf->td, e_o_s, &tf->op)) {
1942 ogg_stream_packetin (&tf->to, &tf->op);
1944 flush_lock->unlock();
1946 flush_ogg(0); // eos flush is done later at close_file
1948 // If we have e_o_s, don't encode any new frames
1954 temp_frame = new VFrame ( tf->ti.width, tf->ti.height, BC_YUV420P, 0);
1956 VFrame *frame = frames[0][j];
1957 int in_color_model = frame->get_color_model();
1958 if (in_color_model == BC_YUV422P &&
1959 temp_frame->get_w() == frame->get_w() &&
1960 temp_frame->get_h() == frame->get_h() &&
1961 temp_frame->get_bytes_per_line() == frame->get_bytes_per_line())
1963 temp_frame->copy_from(frame);
1967 BC_CModels::transfer(temp_frame->get_rows(),
1969 temp_frame->get_y(),
1970 temp_frame->get_u(),
1971 temp_frame->get_v(),
1981 frame->get_w(), // temp_frame can be larger than frame if width not dividable by 16
1983 frame->get_color_model(),
1987 temp_frame->get_w());
1996 int FileOGG::write_frames(VFrame ***frames, int len)
1999 return write_frames_theora(frames, len, 0);
2002 OGGConfigAudio::OGGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
2003 : BC_Window(_(PROGRAM_NAME ": Audio Compression"),
2004 parent_window->get_abs_cursor_x(1),
2005 parent_window->get_abs_cursor_y(1),
2009 this->parent_window = parent_window;
2010 this->asset = asset;
2013 OGGConfigAudio::~OGGConfigAudio()
2018 void OGGConfigAudio::create_objects()
2020 // add_tool(new BC_Title(10, 10, _("There are no audio options for this format")));
2024 char string[BCTEXTLEN];
2026 lock_window("OGGConfigAudio::create_objects");
2027 add_tool(fixed_bitrate = new OGGVorbisFixedBitrate(x, y, this));
2028 add_tool(variable_bitrate = new OGGVorbisVariableBitrate(x1, y, this));
2031 sprintf(string, "%d", asset->vorbis_min_bitrate);
2032 add_tool(new BC_Title(x, y, _("Min bitrate:")));
2033 add_tool(new OGGVorbisMinBitrate(x1, y, this, string));
2036 add_tool(new BC_Title(x, y, _("Avg bitrate:")));
2037 sprintf(string, "%d", asset->vorbis_bitrate);
2038 add_tool(new OGGVorbisAvgBitrate(x1, y, this, string));
2041 add_tool(new BC_Title(x, y, _("Max bitrate:")));
2042 sprintf(string, "%d", asset->vorbis_max_bitrate);
2043 add_tool(new OGGVorbisMaxBitrate(x1, y, this, string));
2046 add_subwindow(new BC_OKButton(this));
2051 int OGGConfigAudio::close_event()
2057 OGGVorbisFixedBitrate::OGGVorbisFixedBitrate(int x, int y, OGGConfigAudio *gui)
2058 : BC_Radial(x, y, !gui->asset->vorbis_vbr, _("Average bitrate"))
2062 int OGGVorbisFixedBitrate::handle_event()
2064 gui->asset->vorbis_vbr = 0;
2065 gui->variable_bitrate->update(0);
2069 OGGVorbisVariableBitrate::OGGVorbisVariableBitrate(int x, int y, OGGConfigAudio *gui)
2070 : BC_Radial(x, y, gui->asset->vorbis_vbr, _("Variable bitrate"))
2074 int OGGVorbisVariableBitrate::handle_event()
2076 gui->asset->vorbis_vbr = 1;
2077 gui->fixed_bitrate->update(0);
2082 OGGVorbisMinBitrate::OGGVorbisMinBitrate(int x,
2084 OGGConfigAudio *gui,
2086 : BC_TextBox(x, y, 180, 1, text)
2090 int OGGVorbisMinBitrate::handle_event()
2092 gui->asset->vorbis_min_bitrate = atol(get_text());
2098 OGGVorbisMaxBitrate::OGGVorbisMaxBitrate(int x,
2100 OGGConfigAudio *gui,
2102 : BC_TextBox(x, y, 180, 1, text)
2106 int OGGVorbisMaxBitrate::handle_event()
2108 gui->asset->vorbis_max_bitrate = atol(get_text());
2114 OGGVorbisAvgBitrate::OGGVorbisAvgBitrate(int x, int y, OGGConfigAudio *gui, char *text)
2115 : BC_TextBox(x, y, 180, 1, text)
2119 int OGGVorbisAvgBitrate::handle_event()
2121 gui->asset->vorbis_bitrate = atol(get_text());
2129 OGGConfigVideo::OGGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
2130 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
2131 parent_window->get_abs_cursor_x(1),
2132 parent_window->get_abs_cursor_y(1),
2136 this->parent_window = parent_window;
2137 this->asset = asset;
2140 OGGConfigVideo::~OGGConfigVideo()
2145 void OGGConfigVideo::create_objects()
2147 // add_tool(new BC_Title(10, 10, _("There are no video options for this format")));
2152 lock_window("OGGConfigVideo::create_objects");
2153 add_subwindow(new BC_Title(x, y + 5, _("Bitrate:")));
2154 add_subwindow(new OGGTheoraBitrate(x1, y, this));
2155 add_subwindow(fixed_bitrate = new OGGTheoraFixedBitrate(x2, y, this));
2158 add_subwindow(new BC_Title(x, y, _("Quality:")));
2159 add_subwindow(new BC_ISlider(x + 80,
2166 asset->theora_quality,
2169 &asset->theora_quality));
2172 add_subwindow(fixed_quality = new OGGTheoraFixedQuality(x2, y, this));
2175 add_subwindow(new BC_Title(x, y, _("Keyframe frequency:")));
2176 OGGTheoraKeyframeFrequency *keyframe_frequency =
2177 new OGGTheoraKeyframeFrequency(x1 + 60, y, this);
2178 keyframe_frequency->create_objects();
2181 add_subwindow(new BC_Title(x, y, _("Keyframe force frequency:")));
2182 OGGTheoraKeyframeForceFrequency *keyframe_force_frequency =
2183 new OGGTheoraKeyframeForceFrequency(x1 + 60, y, this);
2184 keyframe_force_frequency->create_objects();
2187 add_subwindow(new BC_Title(x, y, _("Sharpness:")));
2188 OGGTheoraSharpness *sharpness =
2189 new OGGTheoraSharpness(x1 + 60, y, this);
2190 sharpness->create_objects();
2194 add_subwindow(new BC_OKButton(this));
2202 int OGGConfigVideo::close_event()
2208 OGGTheoraBitrate::OGGTheoraBitrate(int x, int y, OGGConfigVideo *gui)
2209 : BC_TextBox(x, y, 100, 1, gui->asset->theora_bitrate)
2215 int OGGTheoraBitrate::handle_event()
2217 // TODO: MIN / MAX check
2218 gui->asset->theora_bitrate = atol(get_text());
2225 OGGTheoraFixedBitrate::OGGTheoraFixedBitrate(int x, int y, OGGConfigVideo *gui)
2226 : BC_Radial(x, y, gui->asset->theora_fix_bitrate, _("Fixed bitrate"))
2231 int OGGTheoraFixedBitrate::handle_event()
2234 gui->asset->theora_fix_bitrate = 1;
2235 gui->fixed_quality->update(0);
2239 OGGTheoraFixedQuality::OGGTheoraFixedQuality(int x, int y, OGGConfigVideo *gui)
2240 : BC_Radial(x, y, !gui->asset->theora_fix_bitrate, _("Fixed quality"))
2245 int OGGTheoraFixedQuality::handle_event()
2248 gui->asset->theora_fix_bitrate = 0;
2249 gui->fixed_bitrate->update(0);
2253 OGGTheoraKeyframeFrequency::OGGTheoraKeyframeFrequency(int x, int y, OGGConfigVideo *gui)
2254 : BC_TumbleTextBox(gui,
2255 (int64_t)gui->asset->theora_keyframe_frequency,
2265 int OGGTheoraKeyframeFrequency::handle_event()
2267 gui->asset->theora_keyframe_frequency = atol(get_text());
2271 OGGTheoraKeyframeForceFrequency::OGGTheoraKeyframeForceFrequency(int x, int y, OGGConfigVideo *gui)
2272 : BC_TumbleTextBox(gui,
2273 (int64_t)gui->asset->theora_keyframe_frequency,
2283 int OGGTheoraKeyframeForceFrequency::handle_event()
2285 gui->asset->theora_keyframe_frequency = atol(get_text());
2290 OGGTheoraSharpness::OGGTheoraSharpness(int x, int y, OGGConfigVideo *gui)
2291 : BC_TumbleTextBox(gui,
2292 (int64_t)gui->asset->theora_sharpness,
2302 int OGGTheoraSharpness::handle_event()
2304 gui->asset->theora_sharpness = atol(get_text());
2309 PackagingEngineOGG::PackagingEngineOGG()
2315 PackagingEngineOGG::~PackagingEngineOGG()
2319 for(int i = 0; i < total_packages; i++)
2324 default_asset->remove_user();
2329 int PackagingEngineOGG::create_packages_single_farm(
2331 Preferences *preferences,
2332 Asset *default_asset,
2336 this->total_start = total_start;
2337 this->total_end = total_end;
2340 this->preferences = preferences;
2342 // We make A COPY of the asset, because we set audio_data = 0 on local asset which is the same copy as default_asset...
2343 // Should be taken care of somewhere else actually
2344 this->default_asset = new Asset(*default_asset);
2346 audio_start = Units::to_int64(total_start * default_asset->sample_rate);
2347 video_start = Units::to_int64(total_start * default_asset->frame_rate);
2348 audio_position = audio_start;
2349 video_position = video_start;
2350 audio_end = Units::to_int64(total_end * default_asset->sample_rate);
2351 video_end = Units::to_int64(total_end * default_asset->frame_rate);
2352 current_package = 0;
2354 double total_len = total_end - total_start;
2355 //printf("PackageDispatcher::create_packages: %f / %d = %f\n", total_len, total_packages, package_len);
2358 if (default_asset->audio_data)
2360 if (default_asset->video_data)
2361 total_packages += preferences->renderfarm_job_count;
2363 packages = new RenderPackage*[total_packages];
2365 int local_current_package = 0;
2366 if (default_asset->audio_data)
2368 packages[local_current_package] = new RenderPackage;
2369 snprintf(packages[current_package]->path,
2370 sizeof(packages[current_package]->path),
2371 "%s.audio", default_asset->path);
2372 local_current_package++;
2375 if (default_asset->video_data)
2377 video_package_len = (total_len) / preferences->renderfarm_job_count;
2378 int current_number; // The number being injected into the filename.
2379 int number_start; // Character in the filename path at which the number begins
2380 int total_digits; // Total number of digits including padding the user specified.
2382 Render::get_starting_number(default_asset->path,
2388 for(int i = 0; i < preferences->renderfarm_job_count; i++)
2390 RenderPackage *package = packages[local_current_package] = new RenderPackage;
2391 Render::create_filename(package->path,
2392 default_asset->path,
2397 local_current_package++;
2403 RenderPackage* PackagingEngineOGG::get_package_single_farm(double frames_per_second,
2408 //printf("PackageDispatcher::get_package %ld %ld %ld %ld\n", audio_position, video_position, audio_end, video_end);
2409 if (current_package == total_packages)
2412 RenderPackage *result = 0;
2413 if (current_package == 0 && default_asset->audio_data)
2415 result = packages[0];
2416 result->audio_start = audio_start;
2417 result->video_start = video_start;
2418 result->audio_end = audio_end;
2419 result->video_end = video_end;
2420 result->audio_do = 1;
2421 result->video_do = 0;
2422 } else if (default_asset->video_data)
2424 // Do not do any scaling according to node speed, so we know we can get evenly distributed 'forced' keyframes
2425 result = packages[current_package];
2426 result->audio_do = 0;
2427 result->video_do = 1;
2429 result->audio_start = audio_position;
2430 result->video_start = video_position;
2431 result->audio_end = audio_position +
2432 Units::round(video_package_len * default_asset->sample_rate);
2433 result->video_end = video_position +
2434 Units::round(video_package_len * default_asset->frame_rate);
2436 // Last package... take it all!
2437 if (current_package == total_packages -1 )
2439 result->audio_end = audio_end;
2440 result->video_end = video_end;
2443 audio_position = result->audio_end;
2444 video_position = result->video_end;
2453 void PackagingEngineOGG::get_package_paths(ArrayList<char*> *path_list)
2455 for(int i = 0; i < total_packages; i++)
2457 path_list->append(strdup(packages[i]->path));
2459 // We will mux to the the final file at the end!
2460 path_list->append(strdup(default_asset->path));
2461 path_list->set_free();
2464 int64_t PackagingEngineOGG::get_progress_max()
2466 return Units::to_int64(default_asset->sample_rate *
2467 (total_end - total_start)) * 2+
2468 Units::to_int64(preferences->render_preroll *
2470 default_asset->sample_rate);
2473 int PackagingEngineOGG::packages_are_done()
2477 // Mux audio and video into one file
2479 // First fix our asset... have to workaround the bug of corruption of local asset
2480 // Render::check_asset(edl, *default_asset);
2482 Asset *video_asset = 0, *audio_asset = 0;
2483 File *audio_file_gen = 0, *video_file_gen = 0;
2484 FileOGG *video_file = 0, *audio_file = 0;
2485 ogg_stream_state audio_in_stream, video_in_stream;
2487 int local_current_package = 0;
2488 if (default_asset->audio_data)
2490 audio_asset = new Asset(packages[local_current_package]->path);
2491 audio_asset->format = FILE_OGG;
2492 local_current_package++;
2494 audio_file_gen = new File();
2495 audio_file_gen->open_file(preferences, audio_asset, 1, 0);
2496 audio_file = (FileOGG*) audio_file_gen->file;
2497 ogg_stream_init(&audio_in_stream, audio_file->tf->vo.serialno);
2498 audio_file->ogg_seek_to_databegin(audio_file->tf->audiosync, audio_file->tf->vo.serialno);
2501 if (default_asset->video_data)
2503 video_asset = new Asset(packages[local_current_package]->path);
2504 video_asset->format = FILE_OGG;
2505 local_current_package++;
2507 video_file_gen = new File();
2508 video_file_gen->open_file(preferences, video_asset, 1, 0);
2509 video_file = (FileOGG*) video_file_gen->file;
2510 ogg_stream_init(&video_in_stream, video_file->tf->to.serialno);
2511 video_file->ogg_seek_to_databegin(video_file->tf->videosync, video_file->tf->to.serialno);
2515 File *output_file_gen = new File();
2516 output_file_gen->open_file(preferences, default_asset, 0, 1);
2517 FileOGG *output_file = (FileOGG*) output_file_gen->file;
2519 //ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */
2520 ogg_packet op; /* one raw packet of data for decode */
2523 int audio_ready = default_asset->audio_data;
2524 int video_ready = default_asset->video_data;
2525 int64_t video_packetno = 1;
2526 int64_t audio_packetno = 1;
2527 int64_t frame_offset = 0;
2528 int64_t current_frame = 0;
2529 while ((default_asset->audio_data && audio_ready) || (default_asset->video_data && video_ready))
2533 while (ogg_stream_packetpeek(&video_in_stream, NULL) != 1) // get as many pages as needed for one package
2535 if (!video_file->ogg_get_next_page(video_file->tf->videosync, video_file->tf->to.serialno, &video_file->tf->videopage))
2537 // We are at the end of our file, see if it is more and open more if there is
2538 if (local_current_package < total_packages)
2540 frame_offset = current_frame +1;
2541 ogg_stream_clear(&video_in_stream);
2542 video_file_gen->close_file();
2543 delete video_file_gen;
2544 if( video_asset ) video_asset->remove_user();
2545 video_asset = new Asset(packages[local_current_package]->path);
2546 video_asset->format = FILE_OGG;
2547 local_current_package++;
2549 video_file_gen = new File();
2550 video_file_gen->open_file(preferences, video_asset, 1, 0);
2551 video_file = (FileOGG*) video_file_gen->file;
2552 ogg_stream_init(&video_in_stream, video_file->tf->to.serialno);
2553 video_file->ogg_seek_to_databegin(video_file->tf->videosync, video_file->tf->to.serialno);
2559 ogg_stream_pagein(&video_in_stream, &video_file->tf->videopage);
2561 while (ogg_stream_packetpeek(&video_in_stream, NULL) == 1) // get all packets out of the page
2563 ogg_stream_packetout(&video_in_stream, &op);
2564 if (local_current_package != total_packages) // keep it from closing the stream
2566 if (video_packetno != 1) // if this is not the first video package do not start with b_o_s
2570 op.packetno = video_packetno;
2572 int64_t granulepos = op.granulepos;
2573 if (granulepos != -1)
2576 int64_t rel_iframe = granulepos >> video_file->theora_keyframe_granule_shift;
2577 int64_t rel_pframe = granulepos - (rel_iframe << video_file->theora_keyframe_granule_shift);
2578 int64_t rel_current_frame = rel_iframe + rel_pframe;
2579 current_frame = frame_offset + rel_current_frame;
2580 int64_t abs_iframe = current_frame - rel_pframe;
2582 op.granulepos = (abs_iframe << video_file->theora_keyframe_granule_shift) + rel_pframe;
2584 // printf("iframe: %i, pframe: %i, granulepos: %i, op.packetno %lli, abs_iframe: %i\n", rel_iframe, rel_pframe, granulepos, op.packetno, abs_iframe);
2587 ogg_stream_packetin (&output_file->tf->to, &op);
2588 output_file->tf->v_pkg++;
2593 while (ogg_stream_packetpeek(&audio_in_stream, NULL) != 1) // get as many pages as needed for one package
2595 if (!audio_file->ogg_get_next_page(audio_file->tf->audiosync, audio_file->tf->vo.serialno, &audio_file->tf->audiopage))
2600 ogg_stream_pagein(&audio_in_stream, &audio_file->tf->audiopage);
2602 while (ogg_stream_packetpeek(&audio_in_stream, NULL) == 1) // get all packets out of the page
2604 ogg_stream_packetout(&audio_in_stream, &op);
2605 ogg_stream_packetin (&output_file->tf->vo, &op);
2607 output_file->tf->a_pkg++;
2611 output_file->flush_ogg(0);
2616 // flush_ogg(1) is called on file closing time...
2617 // output_file->flush_ogg(1);
2619 // Just prevent thet write_samples and write_frames are called
2620 output_file->final_write = 0;
2622 if (default_asset->audio_data)
2624 ogg_stream_clear(&audio_in_stream);
2625 audio_file_gen->close_file();
2626 delete audio_file_gen;
2628 audio_asset->remove_user();
2630 if (default_asset->video_data)
2632 ogg_stream_clear(&video_in_stream);
2633 video_file_gen->close_file();
2634 delete video_file_gen;
2636 video_asset->remove_user();
2639 output_file_gen->close_file();
2640 delete output_file_gen;
2642 // don't delete the temp files, for now
2643 // for(int i = 0; i < total_packages; i++)
2644 // unlink(packages[i]->path);