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 "bcprogressbox.h"
24 #include "bcsignals.h"
25 #include "bitspopup.h"
26 #include "byteorder.h"
28 #include "commercials.h"
29 #include "condition.h"
34 #include "filesystem.h"
36 #include "indexfile.h"
37 #include "interlacemodes.h"
38 #include "indexstate.h"
40 #include "mainerror.h"
43 #include "preferences.h"
44 #include "removefile.h"
46 #include "videodevice.inc"
53 #define HVPEG_EXE "/hveg2enc"
54 #define MJPEG_EXE "/mpeg2enc"
57 // M JPEG dependancies
58 static double frame_rate_codes[] =
71 static double aspect_ratio_codes[] =
87 FileMPEG::FileMPEG(Asset *asset, File *file)
88 : FileBase(asset, file)
91 // May also be VMPEG or AMPEG if write status.
92 if(asset->format == FILE_UNKNOWN) asset->format = FILE_MPEG;
93 asset->byte_order = 0;
94 next_frame_lock = new Condition(0, "FileMPEG::next_frame_lock");
95 next_frame_done = new Condition(0, "FileMPEG::next_frame_done");
96 vcommand_line.set_array_delete();
102 delete next_frame_lock;
103 delete next_frame_done;
104 vcommand_line.remove_all_objects();
107 void FileMPEG::get_parameters(BC_WindowBase *parent_window,
109 BC_WindowBase* &format_window,
113 if(audio_options && asset->format == FILE_AMPEG)
115 MPEGConfigAudio *window = new MPEGConfigAudio(parent_window, asset);
116 format_window = window;
117 window->create_objects();
118 window->run_window();
122 if(video_options && asset->format == FILE_VMPEG)
124 MPEGConfigVideo *window = new MPEGConfigVideo(parent_window, asset);
125 format_window = window;
126 window->create_objects();
127 window->run_window();
132 int FileMPEG::check_sig(Asset *asset)
134 return mpeg3_check_sig(asset->path);
137 void FileMPEG::get_info(char *title_path, char *path, char *text, int len)
143 int zio_access = ZIO_UNBUFFERED+ZIO_SINGLE_ACCESS;
144 if( !(fd=mpeg3_zopen(title_path, path, &result,zio_access)) ) result = 1;
145 if( !result ) result = mpeg3_create_title(fd, 0);
148 char *cp = text, *ep = text + len-1;
149 if( mpeg3_has_toc(fd) ) {
150 cp += snprintf(cp,ep-cp, _("toc path:%s\n"), path);
151 cp += snprintf(cp,ep-cp, _("title path:\n"));
152 for( int i=0; i<100; ++i ) {
153 char *title_path = mpeg3_title_path(fd,i);
154 if( !title_path ) break;
155 cp += snprintf(cp,ep-cp, " %2d. %s\n", i+1, title_path);
159 cp += snprintf(cp,ep-cp, _("file path:%s\n"), path);
160 int64_t bytes = mpeg3_get_bytes(fd);
161 char string[BCTEXTLEN];
162 sprintf(string,"%jd",bytes);
163 Units::punctuate(string);
164 cp += snprintf(cp,ep-cp, _("size: %s"), string);
166 if( mpeg3_is_program_stream(fd) )
167 cp += snprintf(cp,ep-cp, _(" program stream\n"));
168 else if( mpeg3_is_transport_stream(fd) )
169 cp += snprintf(cp,ep-cp, _(" transport stream\n"));
170 else if( mpeg3_is_video_stream(fd) )
171 cp += snprintf(cp,ep-cp, _(" video stream\n"));
172 else if( mpeg3_is_audio_stream(fd) )
173 cp += snprintf(cp,ep-cp, _(" audio stream\n"));
175 int64_t sdate = mpeg3_get_source_date(fd);
178 memset(&ostat,0,sizeof(struct stat64));
179 sdate = stat64(path, &ostat) < 0 ? 0 : ostat.st_mtime;
181 time_t tm = (time_t)sdate;
182 cp += snprintf(cp,ep-cp, _("date: %s\n"), ctime(&tm));
184 int vtrks = mpeg3_total_vstreams(fd);
185 cp += snprintf(cp,ep-cp, _("%d video tracks\n"), vtrks);
186 for( int vtrk=0; vtrk<vtrks; ++vtrk ) {
187 int cmdl = mpeg3_colormodel(fd, vtrk);
188 int color_model = bc_colormodel(cmdl);
189 char *cmodel = MPEGColorModel::cmodel_to_string(color_model);
190 int width = mpeg3_video_width(fd, vtrk);
191 int height = mpeg3_video_height(fd, vtrk);
192 cp += snprintf(cp,ep-cp, _(" v%d %s %dx%d"), vtrk, cmodel, width, height);
193 double frame_rate = mpeg3_frame_rate(fd, vtrk);
194 int64_t frames = mpeg3_video_frames(fd, vtrk);
195 cp += snprintf(cp,ep-cp, _(" (%5.2f), %jd frames"), frame_rate, frames);
196 if( frame_rate > 0 ) {
197 double secs = (double)frames / frame_rate;
198 cp += snprintf(cp,ep-cp, _(" (%0.3f secs)"),secs);
202 int atrks = mpeg3_total_astreams(fd);
203 cp += snprintf(cp,ep-cp, _("%d audio tracks\n"), atrks);
204 for( int atrk=0; atrk<atrks; ++atrk) {
205 const char *format = mpeg3_audio_format(fd, atrk);
206 cp += snprintf(cp,ep-cp, _(" a%d %s"), atrk, format);
207 int channels = mpeg3_audio_channels(fd, atrk);
208 int sample_rate = mpeg3_sample_rate(fd, atrk);
209 cp += snprintf(cp,ep-cp, _(" ch%d (%d)"), channels, sample_rate);
210 int64_t samples = mpeg3_audio_samples(fd, atrk);
211 cp += snprintf(cp,ep-cp, " %jd",samples);
212 int64_t nudge = mpeg3_get_audio_nudge(fd, atrk);
213 *cp++ = nudge >= 0 ? '+' : (nudge=-nudge, '-');
214 cp += snprintf(cp,ep-cp, _("%jd samples"),nudge);
215 if( sample_rate > 0 ) {
216 double secs = (double)(samples+nudge) / sample_rate;
217 cp += snprintf(cp,ep-cp, _(" (%0.3f secs)"),secs);
221 int stracks = mpeg3_subtitle_tracks(fd);
223 cp += snprintf(cp,ep-cp, _("%d subtitles\n"), stracks);
225 int vts_titles = mpeg3_get_total_vts_titles(fd);
227 cp += snprintf(cp,ep-cp, _("%d title sets, "), vts_titles);
228 int interleaves = mpeg3_get_total_interleaves(fd);
229 if( interleaves > 0 )
230 cp += snprintf(cp,ep-cp, _("%d interleaves\n"), interleaves);
231 int vts_title = mpeg3_set_vts_title(fd, -1);
232 int angle = mpeg3_set_angle(fd, -1);
233 int interleave = mpeg3_set_interleave(fd, -1);
234 int program = mpeg3_set_program(fd, -1);
235 cp += snprintf(cp,ep-cp, _("current program %d = title %d, angle %d, interleave %d\n\n"),
236 program, vts_title, angle, interleave);
238 ArrayList<double> cell_times;
239 int cell_no = 0; double cell_time;
240 while( !mpeg3_get_cell_time(fd, cell_no++, &cell_time) ) {
241 cell_times.append(cell_time);
243 if( cell_times.size() > 1 ) {
244 cp += snprintf(cp,ep-cp, _("cell times:"));
245 for( int i=0; i<cell_times.size(); ++i ) {
246 if( (i%4) == 0 ) *cp++ = '\n';
247 cp += snprintf(cp,ep-cp," %3d. %8.3f",i,cell_times.get(i));
249 cp += snprintf(cp,ep-cp, "\n");
252 int elements = mpeg3_dvb_channel_count(fd);
253 if( elements <= 0 ) return;
254 if( !mpeg3_dvb_get_system_time(fd, &sdate) ) {
256 cp += snprintf(cp,ep-cp, _("\nsystem time: %s"), ctime_r(&tm,string));
258 cp += snprintf(cp,ep-cp, _("elements %d\n"), elements);
260 for( int n=0; n<elements; ++n ) {
261 char name[16], enc[8]; int vstream, astream;
262 int major, minor, total_astreams, total_vstreams;
263 if( mpeg3_dvb_get_channel(fd,n, &major, &minor) ||
264 mpeg3_dvb_get_station_id(fd,n,&name[0]) ||
265 mpeg3_dvb_total_vstreams(fd,n,&total_vstreams) ||
266 mpeg3_dvb_total_astreams(fd,n,&total_astreams) ) continue;
267 cp += snprintf(cp,ep-cp, " %3d.%-3d %s", major, minor, &name[0]);
268 for( int vidx=0; vidx<total_vstreams; ++vidx ) {
269 if( mpeg3_dvb_vstream_number(fd,n,vidx,&vstream) ) continue;
270 if( vstream < 0 ) continue;
271 cp += snprintf(cp,ep-cp, " v%d", vstream);
273 for( int aidx=0; aidx<total_astreams; ++aidx ) {
274 if( mpeg3_dvb_astream_number(fd,n,aidx,&astream,&enc[0]) ) continue;
275 if( astream < 0 ) continue;
276 cp += snprintf(cp,ep-cp, " a%d %s", astream, &enc[0]);
278 for(int i=0; i<astream; ++i )
279 atrack += mpeg3_audio_channels(fd, i);
280 int channels = mpeg3_audio_channels(fd, astream);
281 cp += snprintf(cp,ep-cp, " trk %d-%d", atrack+1, atrack+channels);
282 if( enc[0] ) cp += snprintf(cp,ep-cp," (%s)",enc);
284 cp += snprintf(cp,ep-cp, "\n");
287 for( int n=0; n<elements; ++n ) {
289 if( mpeg3_dvb_get_channel(fd,n, &major, &minor) ) continue;
290 cp += snprintf(cp,ep-cp, "\n**chan %3d.%-3d\n", major, minor);
291 int len = mpeg3_dvb_get_chan_info(fd, n, -1, 0, cp, 1023);
292 if( len < 0 ) len = snprintf(cp,ep-cp,_("no info"));
293 cp += len; *cp++ = '*'; *cp++ = '*'; *cp++ = '\n';
294 for( int ord=0; ord<0x80; ++ord ) {
295 for( int i=0; (len=mpeg3_dvb_get_chan_info(fd,n,ord,i,cp,1023)) >= 0; ++i ) {
296 char *bp = cp; cp += len;
297 for( int k=2; --k>=0; ) { // skip 2 lines
298 while( bp<cp && *bp++!='\n' );
300 for( char *lp=bp; bp<cp; ++bp ) { // add new lines
301 if( *bp == '\n' || ((bp-lp)>=60 && *bp==' ') )
304 *cp++ = '\n'; *cp = 0; // trailing new line
314 int FileMPEG::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
318 int elements = mpeg3_dvb_channel_count(fd);
319 if( elements <= 0 ) return -1;
322 int total_astreams = 0, total_vstreams = 0;
323 for( int n=0; pidx<0 && n<elements; ++n ) {
324 total_astreams = total_vstreams = 0;
325 if( mpeg3_dvb_total_vstreams(fd,n,&total_vstreams) ||
326 mpeg3_dvb_total_astreams(fd,n,&total_astreams) ) continue;
327 if( !total_vstreams || !total_astreams ) continue;
328 for( int i=0; pidx<0 && i<total_vstreams; ++i ) {
330 if( mpeg3_dvb_vstream_number(fd,n,i,&vstrm) ) continue;
331 if( vstrm == vstream ) pidx = n;
334 if( pidx < 0 ) return -1;
336 int64_t channels = 0;
337 for( int i=0; i<total_astreams; ++i ) {
339 if( mpeg3_dvb_astream_number(fd,pidx,i,&astrm,0) ) continue;
340 if( astrm < 0 ) continue;
341 if( ret < 0 ) ret = astrm;
342 if( astream > 0 ) { --astream; continue; }
344 for(int i=0; i<astrm; ++i )
345 atrack += mpeg3_audio_channels(fd, i);
346 int64_t mask = (1 << mpeg3_audio_channels(fd, astrm)) - 1;
347 channels |= mask << atrack;
348 if( !astream ) break;
350 channel_mask = channels;
354 int FileMPEG::reset_parameters_derived()
367 twolame_allocation = 0;
376 lame_output_allocation = 0;
383 int FileMPEG::open_file(int rd, int wr)
389 char toc_name[BCTEXTLEN];
390 result = file->preferences->get_asset_file_path(asset, toc_name);
393 // if toc exists, use it otherwise just probe file
394 char *path = !result ? toc_name : asset->path;
395 fd = mpeg3_open_title(asset->path, path, &error);
398 case zmpeg3_t::ERR_INVALID_TOC_VERSION:
399 eprintf(_("Couldn't open %s: invalid table of contents version.\n"),asset->path);
402 case zmpeg3_t::ERR_TOC_DATE_MISMATCH:
403 eprintf(_("Couldn't open %s: table of contents out of date.\n"),asset->path);
407 eprintf(_("Couldn't open %s: table of contents corrupt.\n"),asset->path);
412 eprintf(_("Rebuilding the table of contents\n"));
417 if( mpeg3_has_toc(fd) )
419 else if( mpeg3_total_vstreams(fd) || mpeg3_total_astreams(fd) )
422 eprintf(_("Couldn't open %s: no audio or video.\n"),asset->path);
429 if( fd ) { mpeg3_close(fd); fd = 0; }
430 result = create_toc(toc_name);
434 mpeg3_set_cpus(fd, file->cpus < 4 ? file->cpus : 4);
435 file->current_program = mpeg3_set_program(fd, -1);
436 if( asset->program < 0 )
437 asset->program = file->current_program;
439 asset->audio_data = mpeg3_has_audio(fd);
440 if(asset->audio_data) {
442 for(int i = 0; i < mpeg3_total_astreams(fd); i++) {
443 asset->channels += mpeg3_audio_channels(fd, i);
445 if(!asset->sample_rate)
446 asset->sample_rate = mpeg3_sample_rate(fd, 0);
447 asset->audio_length = mpeg3_audio_samples(fd, 0);
448 if( !asset->channels || !asset->sample_rate )
452 asset->video_data = mpeg3_has_video(fd);
453 if( !result && asset->video_data ) {
454 //TODO: this is not as easy as just looking at headers.
455 //most interlaced media is rendered as FRM, not TOP/BOT in coding ext hdrs.
456 //currently, just using the assetedit menu to set the required result as needed.
457 // if( asset->interlace_mode == ILACE_MODE_UNDETECTED )
458 // asset->interlace_mode = mpeg3_detect_interlace(fd, 0);
459 if( !asset->layers ) {
460 asset->layers = mpeg3_total_vstreams(fd);
462 asset->actual_width = mpeg3_video_width(fd, 0);
464 asset->width = asset->actual_width;
465 asset->actual_height = mpeg3_video_height(fd, 0);
467 asset->height = asset->actual_height;
468 if( !asset->video_length )
469 asset->video_length = mpeg3_video_frames(fd, 0);
470 if( !asset->vmpeg_cmodel )
471 asset->vmpeg_cmodel = bc_colormodel(mpeg3_colormodel(fd, 0));
472 if( !asset->frame_rate )
473 asset->frame_rate = mpeg3_frame_rate(fd, 0);
477 eprintf(_("Couldn't open %s: failed.\n"), asset->path);
481 if( !result && wr && asset->format == FILE_VMPEG ) {
482 // Heroine Virtual encoder
483 // this one is cinelerra-x.x.x/mpeg2enc
484 if(asset->vmpeg_cmodel == BC_YUV422P)
486 char bitrate_string[BCTEXTLEN];
487 char quant_string[BCTEXTLEN];
488 char iframe_string[BCTEXTLEN];
490 sprintf(bitrate_string, "%d", asset->vmpeg_bitrate);
491 sprintf(quant_string, "%d", asset->vmpeg_quantization);
492 sprintf(iframe_string, "%d", asset->vmpeg_iframe_distance);
494 // Construct command line
497 const char *exec_path = File::get_cinlib_path();
498 sprintf(mjpeg_command, "%s/%s", exec_path, HVPEG_EXE);
499 append_vcommand_line(mjpeg_command);
501 if(asset->aspect_ratio > 0)
503 append_vcommand_line("-a");
505 if(EQUIV((double)asset->width / asset->height,
506 asset->aspect_ratio))
507 append_vcommand_line("1");
509 if(EQUIV(asset->aspect_ratio, 1.333))
510 append_vcommand_line("2");
512 if(EQUIV(asset->aspect_ratio, 1.777))
513 append_vcommand_line("3");
515 if(EQUIV(asset->aspect_ratio, 2.11))
516 append_vcommand_line("4");
519 append_vcommand_line(asset->vmpeg_derivative == 1 ? "-1" : "");
520 append_vcommand_line(asset->vmpeg_cmodel == BC_YUV422P ? "-422" : "");
521 if(asset->vmpeg_fix_bitrate)
523 append_vcommand_line("-b");
524 append_vcommand_line(bitrate_string);
528 append_vcommand_line("-q");
529 append_vcommand_line(quant_string);
531 append_vcommand_line("-n");
532 append_vcommand_line(iframe_string);
533 append_vcommand_line(asset->vmpeg_progressive ? "-p" : "");
534 append_vcommand_line(asset->vmpeg_denoise ? "-d" : "");
535 append_vcommand_line(file->cpus <= 1 ? "-u" : "");
536 append_vcommand_line(asset->vmpeg_seq_codes ? "-g" : "");
537 append_vcommand_line(asset->path);
539 video_out = new FileMPEGVideo(this);
544 // mjpegtools encoder
545 // this one is cinelerra-x.x.x/thirdparty/mjpegtools/mpeg2enc
547 const char *exec_path = File::get_cinlib_path();
548 sprintf(mjpeg_command, "%s/%s -v 0 ", exec_path, MJPEG_EXE);
550 // Must disable interlacing if MPEG-1
551 switch (asset->vmpeg_preset)
553 case 0: asset->vmpeg_progressive = 1; break;
554 case 1: asset->vmpeg_progressive = 1; break;
555 case 2: asset->vmpeg_progressive = 1; break;
558 char string[BCTEXTLEN];
559 // The current usage of mpeg2enc requires bitrate of 0 when quantization is fixed and
560 // quantization of 1 when bitrate is fixed. Perfectly intuitive.
561 if(asset->vmpeg_fix_bitrate)
563 sprintf(string, " -b %d -q 1", asset->vmpeg_bitrate / 1000);
567 sprintf(string, " -b 0 -q %d", asset->vmpeg_quantization);
569 strcat(mjpeg_command, string);
577 int aspect_ratio_code = -1;
578 if(asset->aspect_ratio > 0)
580 int ncodes = sizeof(aspect_ratio_codes) / sizeof(double);
581 for(int i = 1; i < ncodes; i++)
583 if(EQUIV(aspect_ratio_codes[i], asset->aspect_ratio))
585 aspect_ratio_code = i;
593 if(EQUIV((double)asset->width / asset->height, asset->aspect_ratio))
594 aspect_ratio_code = 1;
596 if(aspect_ratio_code < 0)
598 eprintf(_("Unsupported aspect ratio %f\n"), asset->aspect_ratio);
599 aspect_ratio_code = 2;
601 sprintf(string, " -a %d", aspect_ratio_code);
602 strcat(mjpeg_command, string);
610 int frame_rate_code = -1;
611 int ncodes = sizeof(frame_rate_codes) / sizeof(double);
612 for(int i = 1; i < ncodes; ++i)
614 if(EQUIV(asset->frame_rate, frame_rate_codes[i]))
620 if(frame_rate_code < 0)
623 eprintf(_("Unsupported frame rate %f\n"), asset->frame_rate);
625 sprintf(string, " -F %d", frame_rate_code);
626 strcat(mjpeg_command, string);
632 strcat(mjpeg_command,
633 asset->vmpeg_progressive ? " -I 0" : " -I 1");
637 sprintf(string, " -M %d", file->cpus);
638 strcat(mjpeg_command, string);
641 if(!asset->vmpeg_progressive)
643 strcat(mjpeg_command, asset->vmpeg_field_order ? " -z b" : " -z t");
647 sprintf(string, " -f %d", asset->vmpeg_preset);
648 strcat(mjpeg_command, string);
651 sprintf(string, " -g %d -G %d", asset->vmpeg_iframe_distance, asset->vmpeg_iframe_distance);
652 strcat(mjpeg_command, string);
655 if(asset->vmpeg_seq_codes) strcat(mjpeg_command, " -s");
658 sprintf(string, " -R %d", CLAMP(asset->vmpeg_pframe_distance, 0, 2));
659 strcat(mjpeg_command, string);
661 sprintf(string, " -o '%s'", asset->path);
662 strcat(mjpeg_command, string);
666 printf("FileMPEG::open_file: Running %s\n", mjpeg_command);
667 if(!(mjpeg_out = popen(mjpeg_command, "w")))
669 perror("FileMPEG::open_file");
670 eprintf(_("Error while opening \"%s\" for writing\n%m\n"), mjpeg_command);
674 video_out = new FileMPEGVideo(this);
678 else if( !result && wr && asset->format == FILE_AMPEG) {
679 //char encoder_string[BCTEXTLEN]; encoder_string[0] = 0;
680 //printf("FileMPEG::open_file 1 %d\n", asset->ampeg_derivative);
682 if(asset->ampeg_derivative == 2)
684 twofp = fopen(asset->path, "w" );
685 if( !twofp ) return 1;
686 twopts = twolame_init();
687 int channels = asset->channels >= 2 ? 2 : 1;
688 twolame_set_num_channels(twopts, channels);
689 twolame_set_in_samplerate(twopts, asset->sample_rate);
690 twolame_set_mode(twopts, channels >= 2 ?
691 TWOLAME_JOINT_STEREO : TWOLAME_MONO);
692 twolame_set_bitrate(twopts, asset->ampeg_bitrate);
693 twolame_init_params(twopts);
696 if(asset->ampeg_derivative == 3)
698 lame_global = lame_init();
699 // lame_set_brate(lame_global, asset->ampeg_bitrate / 1000);
700 lame_set_brate(lame_global, asset->ampeg_bitrate);
701 lame_set_quality(lame_global, 0);
702 lame_set_in_samplerate(lame_global,
704 lame_set_num_channels(lame_global,
706 if((result = lame_init_params(lame_global)) < 0)
708 eprintf(_("encode: lame_init_params returned %d\n"), result);
709 lame_close(lame_global);
713 if(!(lame_fd = fopen(asset->path, "w")))
715 perror("FileMPEG::open_file");
716 eprintf(_("Error while opening \"%s\" for writing\n%m\n"), asset->path);
717 lame_close(lame_global);
724 eprintf(_("ampeg_derivative=%d\n"), asset->ampeg_derivative);
729 // Transport stream for DVB capture
730 if( !result && !rd && !wr && asset->format == FILE_MPEG ) {
731 if( (recd_fd = open(asset->path, O_CREAT+O_TRUNC+O_WRONLY,
732 S_IRUSR+S_IWUSR + S_IRGRP+S_IWGRP)) < 0 ) {
733 perror("FileMPEG::open_file");
734 eprintf(_("Error while opening \"%s\" for writing\n%m\n"), asset->path);
748 int FileMPEG::set_skimming(int track, int skim, skim_fn fn, void *vp)
750 return !fn ? mpeg3_set_thumbnail_callback(fd, track, 0, 0, 0, 0) :
751 mpeg3_set_thumbnail_callback(fd, track, skim, 1, fn, vp);
754 int FileMPEG::skimming(void *vp, int track)
756 File *file = (File *)vp;
757 FileMPEG *mpeg = (FileMPEG *)file->file;
758 return mpeg->skim_result = mpeg->skim_callback(mpeg->skim_data, track);
761 int FileMPEG::skim_video(int track, void *vp, skim_fn fn)
763 skim_callback = fn; skim_data = vp;
764 mpeg3_set_thumbnail_callback(fd, track, 1, 1, skimming, (void*)file);
766 while( skim_result < 0 && !mpeg3_end_of_video(fd, track) )
767 mpeg3_drop_frames(fd, 1, track);
768 mpeg3_set_thumbnail_callback(fd, track, 0, 0, 0, 0);
774 int FileMPEG::toc_nail(void *vp, int track)
776 File *file = (File *)vp;
777 FileMPEG *mpeg = (FileMPEG *)file->file;
778 int64_t framenum; uint8_t *tdat; int mw, mh;
779 if( mpeg->get_thumbnail(track, framenum, tdat, mw, mh) ) return 1;
780 int pid, width, height; double framerate;
781 if( mpeg->get_video_info(track, pid, framerate, width, height) ) return 1;
782 if( pid < 0 || framerate <= 0 ) return 1;
783 double position = framenum / framerate;
784 //printf("t%d/%03x f"_LD", %dx%d %dx%d\n",track,pid,framenum,mw,mh,width,height);
785 MWindow::commercials->get_frame(file, pid, position, tdat, mw, mh, width, height);
790 int FileMPEG::create_toc(char *toc_path)
792 // delete any existing toc files
793 char toc_file[BCTEXTLEN];
794 strcpy(toc_file, toc_path);
795 if( strcmp(toc_file, asset->path) )
797 char *bp = strrchr(toc_file, '/');
798 if( !bp ) bp = toc_file;
799 char *sfx = strrchr(bp,'.');
805 int64_t total_bytes = 0, last_bytes = -1;
806 fd = mpeg3_start_toc(asset->path, toc_file,
807 file->current_program, &total_bytes);
809 eprintf(_("cant start toc/idx for file: %s\n"), asset->path);
813 // File needs a table of contents.
814 struct timeval new_time, prev_time, start_time, current_time;
815 gettimeofday(&prev_time, 0); gettimeofday(&start_time, 0);
816 if( file->preferences->scan_commercials ) {
817 set_skimming(-1, 1, toc_nail, file);
818 if( MWindow::commercials->resetDb() != 0 )
819 eprintf(_("cant access commercials database"));
821 // This gets around the fact that MWindowGUI may be locked.
822 char progress_title[BCTEXTLEN];
823 sprintf(progress_title, _("Creating %s\n"), toc_file);
824 BC_ProgressBox progress(-1, -1, progress_title, total_bytes);
829 int64_t bytes_processed = 0;
830 if( mpeg3_do_toc(fd, &bytes_processed) ) break;
832 if( bytes_processed >= total_bytes ) break;
833 if( bytes_processed == last_bytes ) {
834 eprintf(_("toc scan stopped before eof"));
837 last_bytes = bytes_processed;
839 gettimeofday(&new_time, 0);
840 if( new_time.tv_sec - prev_time.tv_sec >= 1 ) {
841 gettimeofday(¤t_time, 0);
842 int64_t elapsed_seconds = current_time.tv_sec - start_time.tv_sec;
843 int64_t total_seconds = !bytes_processed ? 0 :
844 elapsed_seconds * total_bytes / bytes_processed;
845 int64_t eta = total_seconds - elapsed_seconds;
846 progress.update(bytes_processed, 1);
847 char string[BCTEXTLEN];
848 sprintf(string, "%sETA: %jdm%jds",
849 progress_title, eta / 60, eta % 60);
850 progress.update_title(string, 1);
851 // fprintf(stderr, "ETA: %dm%ds \r",
852 // bytes_processed * 100 / total_bytes,
853 // eta / 60, eta % 60);
855 prev_time = new_time;
858 if( progress.is_cancelled() ) {
864 if( file->preferences->scan_commercials ) {
865 if( !result ) MWindow::commercials->write_ads(asset->path);
866 MWindow::commercials->closeDb();
872 progress.stop_progress();
875 remove_file(toc_file);
879 // Reopen file from toc path instead of asset path.
881 fd = mpeg3_open(toc_file, &error);
883 eprintf(_("mpeg3_open failed: %s"), toc_file);
889 int FileMPEG::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
892 IndexState *index_state = index_file->get_state();
893 index_state->reset_index();
894 index_state->reset_markers();
896 // Convert the index tables from tracks to channels.
897 int ntracks = mpeg3_index_tracks(fd);
898 if( !ntracks ) return 1;
900 int index_zoom = mpeg3_index_zoom(fd);
902 for( int i = 0; i < ntracks; ++i ) {
903 int nch = mpeg3_index_channels(fd, i);
904 for( int j = 0; j < nch; ++j ) {
905 float *bfr = (float *)mpeg3_index_data(fd, i, j);
906 int64_t size = 2*mpeg3_index_size(fd, i);
907 index_state->add_index_entry(bfr, offset, size);
913 int64_t file_bytes = fs.get_size(asset->path);
914 char *index_path = index_file->index_filename;
915 return index_state->write_index(index_path, asset, index_zoom, file_bytes);
923 void FileMPEG::append_vcommand_line(const char *string)
927 char *argv = cstrdup(string);
928 vcommand_line.append(argv);
932 int FileMPEG::close_file()
935 next_frame_lock->unlock();
944 // End of sequence signal
945 if(file->asset->vmpeg_cmodel == BC_YUV422P)
947 mpeg2enc_set_input_buffers(1, 0, 0, 0);
953 vcommand_line.remove_all_objects();
956 unsigned char opkt[1152*2];
957 int ret = twolame_encode_flush(twopts, opkt, sizeof(opkt));
959 fwrite(opkt, 1, ret, twofp);
961 fprintf(stderr, _("twolame error encoding audio: %d\n"), ret);
962 fclose(twofp); twofp = 0;
964 if( twopts ) { twolame_close(&twopts); twopts = 0; }
967 lame_close(lame_global);
969 if(temp_frame) delete temp_frame;
970 if(twolame_temp) delete [] twolame_temp;
972 if(lame_temp[0]) delete [] lame_temp[0];
973 if(lame_temp[1]) delete [] lame_temp[1];
974 if(lame_output) delete [] lame_output;
975 if(lame_fd) fclose(lame_fd);
977 if(mjpeg_out) pclose(mjpeg_out);
986 FileBase::close_file();
990 int FileMPEG::get_best_colormodel(Asset *asset, int driver)
992 //printf("FileMPEG::get_best_colormodel 1\n");
997 case PLAYBACK_X11_XV:
998 case PLAYBACK_ASYNCHRONOUS:
999 return zmpeg3_cmdl(asset->vmpeg_cmodel) > 0 ?
1000 asset->vmpeg_cmodel : BC_RGB888;
1001 case PLAYBACK_X11_GL:
1003 case PLAYBACK_DV1394:
1004 case PLAYBACK_FIREWIRE:
1007 return zmpeg3_cmdl(asset->vmpeg_cmodel) > 0 ?
1008 asset->vmpeg_cmodel : BC_RGB888;
1009 case VIDEO4LINUX2JPEG:
1010 return BC_COMPRESSED;
1012 case VIDEO4LINUX2MPEG:
1014 case CAPTURE_JPEG_WEBCAM:
1015 return BC_COMPRESSED;
1016 case CAPTURE_YUYV_WEBCAM:
1018 case CAPTURE_FIREWIRE:
1019 case CAPTURE_IEC61883:
1022 eprintf(_("unknown driver %d\n"),driver);
1026 int FileMPEG::colormodel_supported(int colormodel)
1031 int FileMPEG::can_copy_from(Asset *asset, int64_t position)
1036 int FileMPEG::set_audio_position(int64_t sample)
1041 int channel, stream;
1042 to_streamchannel(file->current_channel, stream, channel);
1044 //printf("FileMPEG::set_audio_position %d %d %d\n", sample, mpeg3_get_sample(fd, stream), last_sample);
1045 if(sample != mpeg3_get_sample(fd, stream) &&
1046 sample != last_sample)
1048 if(sample >= 0 && sample < asset->audio_length)
1050 //printf("FileMPEG::set_audio_position seeking stream %d\n", sample);
1051 return mpeg3_set_sample(fd, sample, stream);
1060 int FileMPEG::set_video_position(int64_t pos)
1062 if( !fd || pos < 0 || pos >= asset->video_length )
1064 //printf("FileMPEG::set_video_position 1 %jd\n", x);
1065 mpeg3_set_frame(fd, pos, file->current_layer);
1069 int64_t FileMPEG::get_memory_usage()
1071 int64_t result = file->rd && fd ? mpeg3_memory_usage(fd) : 0;
1072 //printf("FileMPEG::get_memory_usage %d %jd\n", __LINE__, result);
1076 int FileMPEG::set_program(int no)
1078 return fd ? mpeg3_set_program(fd, no) : -1;
1081 int FileMPEG::get_cell_time(int no, double &time)
1083 return fd ? mpeg3_get_cell_time(fd, no, &time) : -1;
1086 int FileMPEG::get_system_time(int64_t &tm)
1088 return fd ? mpeg3_dvb_get_system_time(fd, &tm) : -1;
1091 int FileMPEG::get_video_pid(int track)
1093 return fd ? mpeg3_video_pid(fd, track) : -1;
1096 int FileMPEG::get_video_info(int track, int &pid,
1097 double &framerate, int &width, int &height, char *title)
1099 if( !fd ) return -1;
1100 pid = mpeg3_video_pid(fd, track);
1101 framerate = mpeg3_frame_rate(fd, track);
1102 width = mpeg3_video_width(fd, track);
1103 height = mpeg3_video_height(fd, track);
1104 if( !title ) return 0;
1107 int elements = mpeg3_dvb_channel_count(fd);
1108 for( int n=0; n<elements; ++n ) {
1109 int major, minor, total_vstreams, vstream, vidx;
1110 if( mpeg3_dvb_get_channel(fd,n, &major, &minor) ||
1111 mpeg3_dvb_total_vstreams(fd,n,&total_vstreams) ) continue;
1112 for( vidx=0; vidx<total_vstreams; ++vidx ) {
1113 if( mpeg3_dvb_vstream_number(fd,n,vidx,&vstream) ) continue;
1114 if( vstream < 0 ) continue;
1115 if( vstream == track ) {
1116 sprintf(title, "%3d.%-3d", major, minor);
1124 int FileMPEG::select_video_stream(Asset *asset, int vstream)
1126 if( !fd ) return -1;
1127 asset->width = mpeg3_video_width(fd, vstream);
1128 asset->height = mpeg3_video_height(fd, vstream);
1129 asset->video_length = mpeg3_video_frames(fd, vstream);
1130 asset->frame_rate = mpeg3_frame_rate(fd, vstream);
1134 int FileMPEG::select_audio_stream(Asset *asset, int astream)
1136 if( !fd ) return -1;
1137 asset->sample_rate = mpeg3_sample_rate(fd, astream);
1138 asset->audio_length = mpeg3_audio_samples(fd, astream);
1142 int FileMPEG::get_thumbnail(int stream,
1143 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
1146 mpeg3_get_thumbnail(fd, stream, &position, &thumbnail, &ww, &hh);
1149 int FileMPEG::write_samples(double **buffer, int64_t len)
1153 //printf("FileMPEG::write_samples 1\n");
1154 if(asset->ampeg_derivative == 2) {
1156 int channels = MIN(asset->channels, 2);
1157 int64_t audio_size = len * channels * 2;
1158 if(twolame_allocation < audio_size) {
1159 if(twolame_temp) delete [] twolame_temp;
1160 twolame_temp = new unsigned char[audio_size];
1161 twolame_allocation = audio_size;
1162 if(twolame_out) delete [] twolame_out;
1163 twolame_out = new unsigned char[audio_size + 1152];
1166 for(int i = 0; i < channels; i++) {
1167 int16_t *output = ((int16_t*)twolame_temp) + i;
1168 double *input = buffer[i];
1169 for(int j = 0; j < len; j++) {
1170 int sample = (int)(*input * 0x7fff);
1171 *output = (int16_t)(CLIP(sample, -0x8000, 0x7fff));
1176 int ret = twolame_encode_buffer_interleaved(twopts,
1177 (int16_t*)twolame_temp, len,
1178 twolame_out, twolame_allocation+1152);
1180 fwrite(twolame_out, 1, ret, twofp);
1182 fprintf(stderr, _("twolame error encoding audio: %d\n"), ret);
1185 if(asset->ampeg_derivative == 3)
1187 int channels = MIN(asset->channels, 2);
1188 int64_t audio_size = len * channels;
1189 if(!lame_global) return 1;
1190 if(!lame_fd) return 1;
1191 if(lame_allocation < audio_size)
1193 if(lame_temp[0]) delete [] lame_temp[0];
1194 if(lame_temp[1]) delete [] lame_temp[1];
1195 lame_temp[0] = new float[audio_size];
1196 lame_temp[1] = new float[audio_size];
1197 lame_allocation = audio_size;
1200 if(lame_output_allocation < audio_size * 4)
1202 if(lame_output) delete [] lame_output;
1203 lame_output_allocation = audio_size * 4;
1204 lame_output = new char[lame_output_allocation];
1207 for(int i = 0; i < channels; i++)
1209 float *output = lame_temp[i];
1210 double *input = buffer[i];
1211 for(int j = 0; j < len; j++)
1213 *output++ = *input++ * (float)32768;
1217 result = lame_encode_buffer_float(lame_global,
1219 (channels > 1) ? lame_temp[1] : lame_temp[0],
1221 (unsigned char*)lame_output,
1222 lame_output_allocation);
1225 char *real_output = lame_output;
1229 for(int i = 0; i < bytes; i++)
1232 real_output = &lame_output[i];
1238 if(bytes > 0 && lame_started)
1240 result = !fwrite(real_output, 1, bytes, lame_fd);
1242 perror("FileMPEG::write_samples");
1243 eprintf(_("write failed: %m"));
1256 int FileMPEG::write_frames(VFrame ***frames, int len)
1262 int temp_w = (int)((asset->width + 15) / 16) * 16;
1265 int output_cmodel = asset->vmpeg_cmodel;
1266 // verify colormodel supported in MPEG output
1267 switch( output_cmodel ) {
1269 if( file->preferences->dvd_yuv420p_interlace &&
1270 ( asset->interlace_mode == ILACE_MODE_TOP_FIRST ||
1271 asset->interlace_mode == ILACE_MODE_BOTTOM_FIRST ) )
1272 output_cmodel = BC_YUV420PI;
1279 // Height depends on progressiveness
1280 if(asset->vmpeg_progressive || asset->vmpeg_derivative == 1)
1281 temp_h = (int)((asset->height + 15) / 16) * 16;
1283 temp_h = (int)((asset->height + 31) / 32) * 32;
1285 //printf("FileMPEG::write_frames 1\n");
1287 // Only 1 layer is supported in MPEG output
1288 for(int i = 0; i < 1; i++)
1290 for(int j = 0; j < len && !result; j++)
1292 VFrame *frame = frames[i][j];
1296 if(asset->vmpeg_cmodel == BC_YUV422P)
1298 if(frame->get_w() == temp_w &&
1299 frame->get_h() == temp_h &&
1300 frame->get_color_model() == output_cmodel)
1302 mpeg2enc_set_input_buffers(0,
1303 (char*)frame->get_y(),
1304 (char*)frame->get_u(),
1305 (char*)frame->get_v());
1310 (temp_frame->get_w() != temp_w ||
1311 temp_frame->get_h() != temp_h ||
1312 temp_frame->get_color_model() || output_cmodel))
1321 temp_frame = new VFrame(0,
1329 BC_CModels::transfer(temp_frame->get_rows(),
1331 temp_frame->get_y(),
1332 temp_frame->get_u(),
1333 temp_frame->get_v(),
1343 temp_frame->get_w(),
1344 temp_frame->get_h(),
1345 frame->get_color_model(),
1346 temp_frame->get_color_model(),
1349 temp_frame->get_w());
1351 mpeg2enc_set_input_buffers(0,
1352 (char*)temp_frame->get_y(),
1353 (char*)temp_frame->get_u(),
1354 (char*)temp_frame->get_v());
1359 // MJPEG uses the same dimensions as the input
1360 //printf("FileMPEG::write_frames %d\n", __LINE__);sleep(1);
1361 if(frame->get_color_model() == output_cmodel)
1363 mjpeg_y = frame->get_y();
1364 mjpeg_u = frame->get_u();
1365 mjpeg_v = frame->get_v();
1369 //printf("FileMPEG::write_frames %d\n", __LINE__);sleep(1);
1372 temp_frame = new VFrame(0,
1380 // printf("FileMPEG::write_frames %d temp_frame=%p %p %p %p frame=%p %p %p %p color_model=%p %p\n",
1383 // temp_frame->get_w(),
1384 // temp_frame->get_h(),
1388 // temp_frame->get_color_model(),
1389 // frame->get_color_model()); sleep(1);
1390 temp_frame->transfer_from(frame);
1391 //printf("FileMPEG::write_frames %d\n", __LINE__);sleep(1);
1393 mjpeg_y = temp_frame->get_y();
1394 mjpeg_u = temp_frame->get_u();
1395 mjpeg_v = temp_frame->get_v();
1401 next_frame_lock->unlock();
1402 next_frame_done->lock("FileMPEG::write_frames");
1403 if(mjpeg_error) result = 1;
1419 int FileMPEG::zmpeg3_cmdl(int colormodel)
1421 switch( colormodel ) {
1422 case BC_BGR888: return zmpeg3_t::cmdl_BGR888;
1423 case BC_BGR8888: return zmpeg3_t::cmdl_BGRA8888;
1424 case BC_RGB565: return zmpeg3_t::cmdl_RGB565;
1425 case BC_RGB888: return zmpeg3_t::cmdl_RGB888;
1426 case BC_RGBA8888: return zmpeg3_t::cmdl_RGBA8888;
1427 case BC_RGBA16161616: return zmpeg3_t::cmdl_RGBA16161616;
1428 case BC_YUV420P: return zmpeg3_t::cmdl_YUV420P;
1429 case BC_YUV422P: return zmpeg3_t::cmdl_YUV422P;
1430 case BC_YUV422: return zmpeg3_t::cmdl_YUYV;
1431 case BC_YUV888: return zmpeg3_t::cmdl_YUV888;
1432 case BC_YUVA8888: return zmpeg3_t::cmdl_YUVA8888;
1437 int FileMPEG::bc_colormodel(int cmdl)
1440 case zmpeg3_t::cmdl_BGR888: return BC_BGR888;
1441 case zmpeg3_t::cmdl_BGRA8888: return BC_BGR8888;
1442 case zmpeg3_t::cmdl_RGB565: return BC_RGB565;
1443 case zmpeg3_t::cmdl_RGB888: return BC_RGB888;
1444 case zmpeg3_t::cmdl_RGBA8888: return BC_RGBA8888;
1445 case zmpeg3_t::cmdl_RGBA16161616: return BC_RGBA16161616;
1446 case zmpeg3_t::cmdl_YUV420P: return BC_YUV420P;
1447 case zmpeg3_t::cmdl_YUV422P: return BC_YUV422P;
1448 case zmpeg3_t::cmdl_YUYV: return BC_YUV422;
1449 case zmpeg3_t::cmdl_YUV888: return BC_YUV888;
1450 case zmpeg3_t::cmdl_YUVA8888: return BC_YUVA8888;
1455 const char *FileMPEG::zmpeg3_cmdl_name(int cmdl)
1457 # define CMDL(nm) #nm
1458 static const char *cmdl_name[] = {
1484 return cmdl>=0 && cmdl<lengthof(cmdl_name) ? cmdl_name[cmdl] : cmdl_name[6];
1488 int FileMPEG::read_frame(VFrame *frame)
1492 int width = mpeg3_video_width(fd,file->current_layer);
1493 int height = mpeg3_video_height(fd,file->current_layer);
1494 int stream_cmdl = mpeg3_colormodel(fd,file->current_layer);
1495 int stream_color_model = bc_colormodel(stream_cmdl);
1496 int frame_color_model = frame->get_color_model();
1497 int frame_cmdl = asset->interlace_mode == ILACE_MODE_NOTINTERLACED ?
1498 zmpeg3_cmdl(frame_color_model) : -1;
1499 mpeg3_show_subtitle(fd, file->current_layer, file->playback_subtitle);
1501 switch( frame_color_model ) { // check for direct copy
1503 if( frame_cmdl < 0 ) break;
1505 if( stream_color_model == frame_color_model &&
1506 width == frame->get_w() && height == frame->get_h() ) {
1507 mpeg3_read_yuvframe(fd,
1508 (char*)frame->get_y(),
1509 (char*)frame->get_u(),
1510 (char*)frame->get_v(),
1511 0, 0, width, height,
1512 file->current_layer);
1517 if( frame_cmdl >= 0 ) { // supported by read_frame
1518 // cant rely on frame->get_rows(), format may not support it
1520 switch( frame_color_model ) {
1521 case BC_YUV420P: uvs = 2; break;
1522 case BC_YUV422P: uvs = 1; break;
1524 //int w = frame->get_w();
1525 int h = frame->get_h();
1526 int bpl = frame->get_bytes_per_line();
1527 int uvw = !uvs ? 0 : bpl / 2;
1528 int uvh = !uvs ? 0 : h / uvs;
1529 uint8_t *rows[h + 2*uvh], *rp;
1531 if( (rp=frame->get_y()) ) {
1532 for( int i=0; i<h; ++i, rp+=bpl ) rows[n++] = rp;
1533 rp = frame->get_u();
1534 for( int i=0; i<uvh; ++i, rp+=uvw ) rows[n++] = rp;
1535 rp = frame->get_v();
1536 for( int i=0; i<uvh; ++i, rp+=uvw ) rows[n++] = rp;
1539 rp = frame->get_data(); uvh *= 2;
1540 for( int i=0; i<h; ++i, rp+=bpl ) rows[n++] = rp;
1541 for( int i=0; i<uvh; ++i, rp+=uvw ) rows[n++] = rp;
1544 mpeg3_read_frame(fd,
1545 rows, /* start of each output row */
1546 0, 0, width, height, /* input box */
1547 frame->get_w(), /* Dimensions of output_rows */
1550 file->current_layer);
1555 mpeg3_read_yuvframe_ptr(fd, &y, &u, &v, file->current_layer);
1557 if( stream_color_model == BC_YUV420P &&
1558 file->preferences->dvd_yuv420p_interlace && (
1559 asset->interlace_mode == ILACE_MODE_TOP_FIRST ||
1560 asset->interlace_mode == ILACE_MODE_BOTTOM_FIRST ) )
1561 stream_color_model = BC_YUV420PI;
1562 BC_CModels::transfer(frame->get_rows(), 0,
1563 frame->get_y(), frame->get_u(), frame->get_v(),
1564 (unsigned char*)y, (unsigned char*)u, (unsigned char*)v,
1565 0,0, width,height, 0,0, frame->get_w(),frame->get_h(),
1566 stream_color_model, frame_color_model, 0, width, frame->get_w());
1573 void FileMPEG::to_streamchannel(int channel, int &stream_out, int &channel_out)
1575 int total_astreams = mpeg3_total_astreams(fd);
1576 for(stream_out = 0; stream_out < total_astreams; ++stream_out )
1578 int stream_channels = mpeg3_audio_channels(fd, stream_out);
1579 if( channel < stream_channels ) break;
1580 channel -= stream_channels;
1582 channel_out = channel;
1585 int FileMPEG::read_samples(double *buffer, int64_t len)
1588 if(len < 0) return 0;
1590 // Translate pure channel to a stream and a channel in the mpeg stream
1591 int stream, channel;
1592 to_streamchannel(file->current_channel, stream, channel);
1594 //printf("FileMPEG::read_samples 1 current_sample=%jd len=%jd channel=%d\n", file->current_sample, len, channel);
1596 mpeg3_set_sample(fd,
1597 file->current_sample,
1599 mpeg3_read_audio_d(fd,
1600 buffer, /* Pointer to pre-allocated buffer of doubles */
1601 channel, /* Channel to decode */
1602 len, /* Number of samples to decode */
1603 stream); /* Stream containing the channel */
1605 // last_sample = file->current_sample;
1614 FileMPEGVideo::FileMPEGVideo(FileMPEG *file)
1620 if(file->asset->vmpeg_cmodel == BC_YUV422P)
1622 mpeg2enc_init_buffers();
1623 mpeg2enc_set_w(file->asset->width);
1624 mpeg2enc_set_h(file->asset->height);
1625 mpeg2enc_set_rate(file->asset->frame_rate);
1629 FileMPEGVideo::~FileMPEGVideo()
1634 void FileMPEGVideo::run()
1636 if(file->asset->vmpeg_cmodel == BC_YUV422P)
1638 printf("FileMPEGVideo::run ");
1639 for(int i = 0; i < file->vcommand_line.total; i++)
1640 printf("%s ", file->vcommand_line.values[i]);
1642 mpeg2enc(file->vcommand_line.total, file->vcommand_line.values);
1648 //printf("FileMPEGVideo::run %d\n", __LINE__);
1649 file->next_frame_lock->lock("FileMPEGVideo::run");
1650 //printf("FileMPEGVideo::run %d\n", __LINE__);
1653 file->next_frame_done->unlock();
1659 //printf("FileMPEGVideo::run %d\n", __LINE__);
1660 // YUV4 sequence header
1661 if(!file->wrote_header)
1663 file->wrote_header = 1;
1665 char interlace_string[BCTEXTLEN];
1666 if(!file->asset->vmpeg_progressive)
1668 sprintf(interlace_string, file->asset->vmpeg_field_order ? "b" : "t");
1672 sprintf(interlace_string, "p");
1674 double aspect_ratio = file->asset->aspect_ratio >= 0 ?
1675 file->asset->aspect_ratio : 1.0;
1676 //printf("FileMPEGVideo::run %d\n", __LINE__);
1677 fprintf(file->mjpeg_out, "YUV4MPEG2 W%d H%d F%d:%d I%s A%d:%d C%s\n",
1678 file->asset->width, file->asset->height,
1679 (int)(file->asset->frame_rate * 1001),
1680 1001, interlace_string,
1681 (int)(aspect_ratio * 1000), 1000,
1683 //printf("FileMPEGVideo::run %d\n", __LINE__);
1686 // YUV4 frame header
1687 //printf("FileMPEGVideo::run %d\n", __LINE__);
1688 fprintf(file->mjpeg_out, "FRAME\n");
1691 //printf("FileMPEGVideo::run %d\n", __LINE__);
1692 if(!fwrite(file->mjpeg_y, file->asset->width * file->asset->height, 1, file->mjpeg_out))
1693 file->mjpeg_error = 1;
1694 //printf("FileMPEGVideo::run %d\n", __LINE__);
1695 if(!fwrite(file->mjpeg_u, file->asset->width * file->asset->height / 4, 1, file->mjpeg_out))
1696 file->mjpeg_error = 1;
1697 //printf("FileMPEGVideo::run %d\n", __LINE__);
1698 if(!fwrite(file->mjpeg_v, file->asset->width * file->asset->height / 4, 1, file->mjpeg_out))
1699 file->mjpeg_error = 1;
1700 //printf("FileMPEGVideo::run %d\n", __LINE__);
1701 fflush(file->mjpeg_out);
1703 //printf("FileMPEGVideo::run %d\n", __LINE__);
1704 file->next_frame_done->unlock();
1705 //printf("FileMPEGVideo::run %d\n", __LINE__);
1707 pclose(file->mjpeg_out);
1708 file->mjpeg_out = 0;
1723 MPEGConfigAudio::MPEGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
1724 : BC_Window(_(PROGRAM_NAME ": Audio Compression"),
1725 parent_window->get_abs_cursor_x(1),
1726 parent_window->get_abs_cursor_y(1),
1735 this->parent_window = parent_window;
1736 this->asset = asset;
1739 MPEGConfigAudio::~MPEGConfigAudio()
1743 void MPEGConfigAudio::create_objects()
1749 lock_window("MPEGConfigAudio::create_objects");
1750 if(asset->format == FILE_MPEG)
1752 add_subwindow(new BC_Title(x, y, _("No options for MPEG transport stream.")));
1758 add_tool(new BC_Title(x, y, _("Layer:")));
1759 add_tool(layer = new MPEGLayer(x1, y, this));
1760 layer->create_objects();
1763 add_tool(new BC_Title(x, y, _("Kbits per second:")));
1764 add_tool(bitrate = new MPEGABitrate(x1, y, this));
1765 bitrate->create_objects();
1768 add_subwindow(new BC_OKButton(this));
1773 int MPEGConfigAudio::close_event()
1785 MPEGLayer::MPEGLayer(int x, int y, MPEGConfigAudio *gui)
1786 : BC_PopupMenu(x, y, 100, layer_to_string(gui->asset->ampeg_derivative))
1791 void MPEGLayer::create_objects()
1793 add_item(new BC_MenuItem(layer_to_string(2)));
1794 add_item(new BC_MenuItem(layer_to_string(3)));
1797 int MPEGLayer::handle_event()
1799 gui->asset->ampeg_derivative = string_to_layer(get_text());
1800 gui->bitrate->set_layer(gui->asset->ampeg_derivative);
1804 int MPEGLayer::string_to_layer(char *string)
1806 if(!strcasecmp(layer_to_string(2), string))
1808 if(!strcasecmp(layer_to_string(3), string))
1814 char* MPEGLayer::layer_to_string(int layer)
1838 MPEGABitrate::MPEGABitrate(int x, int y, MPEGConfigAudio *gui)
1842 bitrate_to_string(gui->string, gui->asset->ampeg_bitrate))
1847 void MPEGABitrate::create_objects()
1849 set_layer(gui->asset->ampeg_derivative);
1852 void MPEGABitrate::set_layer(int layer)
1854 while(total_items()) del_item(0);
1858 add_item(new BC_MenuItem("160"));
1859 add_item(new BC_MenuItem("192"));
1860 add_item(new BC_MenuItem("224"));
1861 add_item(new BC_MenuItem("256"));
1862 add_item(new BC_MenuItem("320"));
1863 add_item(new BC_MenuItem("384"));
1867 add_item(new BC_MenuItem("8"));
1868 add_item(new BC_MenuItem("16"));
1869 add_item(new BC_MenuItem("24"));
1870 add_item(new BC_MenuItem("32"));
1871 add_item(new BC_MenuItem("40"));
1872 add_item(new BC_MenuItem("48"));
1873 add_item(new BC_MenuItem("56"));
1874 add_item(new BC_MenuItem("64"));
1875 add_item(new BC_MenuItem("80"));
1876 add_item(new BC_MenuItem("96"));
1877 add_item(new BC_MenuItem("112"));
1878 add_item(new BC_MenuItem("128"));
1879 add_item(new BC_MenuItem("144"));
1880 add_item(new BC_MenuItem("160"));
1881 add_item(new BC_MenuItem("192"));
1882 add_item(new BC_MenuItem("224"));
1883 add_item(new BC_MenuItem("256"));
1884 add_item(new BC_MenuItem("320"));
1888 int MPEGABitrate::handle_event()
1890 gui->asset->ampeg_bitrate = string_to_bitrate(get_text());
1894 int MPEGABitrate::string_to_bitrate(char *string)
1896 return atol(string);
1900 char* MPEGABitrate::bitrate_to_string(char *string, int bitrate)
1902 sprintf(string, "%d", bitrate);
1914 MPEGConfigVideo::MPEGConfigVideo(BC_WindowBase *parent_window,
1916 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
1917 parent_window->get_abs_cursor_x(1),
1918 parent_window->get_abs_cursor_y(1),
1927 this->parent_window = parent_window;
1928 this->asset = asset;
1932 MPEGConfigVideo::~MPEGConfigVideo()
1936 void MPEGConfigVideo::create_objects()
1942 lock_window("MPEGConfigVideo::create_objects");
1943 if(asset->format == FILE_MPEG)
1945 add_subwindow(new BC_Title(x, y, _("No options for MPEG transport stream.")));
1950 add_subwindow(new BC_Title(x, y, _("Color model:")));
1951 add_subwindow(cmodel = new MPEGColorModel(x1, y, this));
1952 cmodel->create_objects();
1955 update_cmodel_objs();
1957 add_subwindow(new BC_OKButton(this));
1962 int MPEGConfigVideo::close_event()
1969 void MPEGConfigVideo::delete_cmodel_objs()
1974 delete fixed_bitrate;
1977 delete iframe_distance;
1978 delete pframe_distance;
1979 delete top_field_first;
1983 titles.remove_all_objects();
1987 void MPEGConfigVideo::reset_cmodel()
1995 iframe_distance = 0;
1996 pframe_distance = 0;
1997 top_field_first = 0;
2003 void MPEGConfigVideo::update_cmodel_objs()
2011 delete_cmodel_objs();
2013 if(asset->vmpeg_cmodel == BC_YUV420P)
2015 add_subwindow(title = new BC_Title(x, y + 5, _("Format Preset:")));
2016 titles.append(title);
2017 add_subwindow(preset = new MPEGPreset(x1, y, this));
2018 preset->create_objects();
2022 add_subwindow(title = new BC_Title(x, y + 5, _("Derivative:")));
2023 titles.append(title);
2024 add_subwindow(derivative = new MPEGDerivative(x1, y, this));
2025 derivative->create_objects();
2028 add_subwindow(title = new BC_Title(x, y + 5, _("Bitrate:")));
2029 titles.append(title);
2030 add_subwindow(bitrate = new MPEGBitrate(x1, y, this));
2031 add_subwindow(fixed_bitrate = new MPEGFixedBitrate(x2, y, this));
2034 add_subwindow(title = new BC_Title(x, y, _("Quantization:")));
2035 titles.append(title);
2036 quant = new MPEGQuant(x1, y, this);
2037 quant->create_objects();
2038 add_subwindow(fixed_quant = new MPEGFixedQuant(x2, y, this));
2041 add_subwindow(title = new BC_Title(x, y, _("I frame distance:")));
2042 titles.append(title);
2043 iframe_distance = new MPEGIFrameDistance(x1, y, this);
2044 iframe_distance->create_objects();
2047 if(asset->vmpeg_cmodel == BC_YUV420P)
2049 add_subwindow(title = new BC_Title(x, y, _("P frame distance:")));
2050 titles.append(title);
2051 pframe_distance = new MPEGPFrameDistance(x1, y, this);
2052 pframe_distance->create_objects();
2055 add_subwindow(top_field_first = new BC_CheckBox(x, y, &asset->vmpeg_field_order, _("Bottom field first")));
2059 add_subwindow(progressive = new BC_CheckBox(x, y, &asset->vmpeg_progressive, _("Progressive frames")));
2061 add_subwindow(denoise = new BC_CheckBox(x, y, &asset->vmpeg_denoise, _("Denoise")));
2063 add_subwindow(seq_codes = new BC_CheckBox(x, y, &asset->vmpeg_seq_codes, _("Sequence start codes in every GOP")));
2068 MPEGDerivative::MPEGDerivative(int x, int y, MPEGConfigVideo *gui)
2069 : BC_PopupMenu(x, y, 150, derivative_to_string(gui->asset->vmpeg_derivative))
2074 void MPEGDerivative::create_objects()
2076 add_item(new BC_MenuItem(derivative_to_string(1)));
2077 add_item(new BC_MenuItem(derivative_to_string(2)));
2080 int MPEGDerivative::handle_event()
2082 gui->asset->vmpeg_derivative = string_to_derivative(get_text());
2086 int MPEGDerivative::string_to_derivative(char *string)
2088 if( !strcasecmp(derivative_to_string(1), string) ) return 1;
2089 if( !strcasecmp(derivative_to_string(2), string) ) return 2;
2093 char* MPEGDerivative::derivative_to_string(int derivative)
2095 switch(derivative) {
2096 case 1: return _("MPEG-1");
2097 case 2: return _("MPEG-2");
2103 MPEGPreset::MPEGPreset(int x, int y, MPEGConfigVideo *gui)
2104 : BC_PopupMenu(x, y, 200, value_to_string(gui->asset->vmpeg_preset))
2109 void MPEGPreset::create_objects()
2111 for(int i = 0; i < 14; i++) {
2112 add_item(new BC_MenuItem(value_to_string(i)));
2116 int MPEGPreset::handle_event()
2118 gui->asset->vmpeg_preset = string_to_value(get_text());
2122 int MPEGPreset::string_to_value(char *string)
2124 for(int i = 0; i < 14; i++) {
2125 if(!strcasecmp(value_to_string(i), string))
2131 char* MPEGPreset::value_to_string(int derivative)
2133 switch( derivative ) {
2134 case 0: return _("Generic MPEG-1"); break;
2135 case 1: return _("standard VCD"); break;
2136 case 2: return _("user VCD"); break;
2137 case 3: return _("Generic MPEG-2"); break;
2138 case 4: return _("standard SVCD"); break;
2139 case 5: return _("user SVCD"); break;
2140 case 6: return _("VCD Still sequence"); break;
2141 case 7: return _("SVCD Still sequence"); break;
2142 case 8: return _("DVD NAV"); break;
2143 case 9: return _("DVD"); break;
2144 case 10: return _("ATSC 480i"); break;
2145 case 11: return _("ATSC 480p"); break;
2146 case 12: return _("ATSC 720p"); break;
2147 case 13: return _("ATSC 1080i"); break;
2149 return _("Generic MPEG-1");
2162 MPEGBitrate::MPEGBitrate(int x, int y, MPEGConfigVideo *gui)
2163 : BC_TextBox(x, y, 100, 1, gui->asset->vmpeg_bitrate)
2169 int MPEGBitrate::handle_event()
2171 gui->asset->vmpeg_bitrate = atol(get_text());
2179 MPEGQuant::MPEGQuant(int x, int y, MPEGConfigVideo *gui)
2180 : BC_TumbleTextBox(gui,
2181 (int64_t)gui->asset->vmpeg_quantization,
2191 int MPEGQuant::handle_event()
2193 gui->asset->vmpeg_quantization = atol(get_text());
2197 MPEGFixedBitrate::MPEGFixedBitrate(int x, int y, MPEGConfigVideo *gui)
2198 : BC_Radial(x, y, gui->asset->vmpeg_fix_bitrate, _("Fixed bitrate"))
2203 int MPEGFixedBitrate::handle_event()
2206 gui->asset->vmpeg_fix_bitrate = 1;
2207 gui->fixed_quant->update(0);
2211 MPEGFixedQuant::MPEGFixedQuant(int x, int y, MPEGConfigVideo *gui)
2212 : BC_Radial(x, y, !gui->asset->vmpeg_fix_bitrate, _("Fixed quantization"))
2217 int MPEGFixedQuant::handle_event()
2220 gui->asset->vmpeg_fix_bitrate = 0;
2221 gui->fixed_bitrate->update(0);
2233 MPEGIFrameDistance::MPEGIFrameDistance(int x, int y, MPEGConfigVideo *gui)
2234 : BC_TumbleTextBox(gui,
2235 (int64_t)gui->asset->vmpeg_iframe_distance,
2245 int MPEGIFrameDistance::handle_event()
2247 gui->asset->vmpeg_iframe_distance = atoi(get_text());
2257 MPEGPFrameDistance::MPEGPFrameDistance(int x, int y, MPEGConfigVideo *gui)
2258 : BC_TumbleTextBox(gui,
2259 (int64_t)gui->asset->vmpeg_pframe_distance,
2269 int MPEGPFrameDistance::handle_event()
2271 gui->asset->vmpeg_pframe_distance = atoi(get_text());
2282 MPEGColorModel::MPEGColorModel(int x, int y, MPEGConfigVideo *gui)
2283 : BC_PopupMenu(x, y, 150, cmodel_to_string(gui->asset->vmpeg_cmodel))
2288 void MPEGColorModel::create_objects()
2290 add_item(new BC_MenuItem(cmodel_to_string(BC_YUV420P)));
2291 add_item(new BC_MenuItem(cmodel_to_string(BC_YUV422P)));
2294 int MPEGColorModel::handle_event()
2296 gui->asset->vmpeg_cmodel = string_to_cmodel(get_text());
2297 gui->update_cmodel_objs();
2298 gui->show_window(1);
2302 int MPEGColorModel::string_to_cmodel(char *string)
2304 if(!strcasecmp(cmodel_to_string(BC_YUV420P), string))
2306 if(!strcasecmp(cmodel_to_string(BC_YUV422P), string))
2311 char* MPEGColorModel::cmodel_to_string(int cmodel)
2315 case BC_YUV420P: return _("YUV 4:2:0");
2316 case BC_YUV422P: return _("YUV 4:2:2");
2317 default: return _("YUV 4:2:0");