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,
108 Asset *asset, BC_WindowBase* &format_window,
109 int audio_options, int video_options, EDL *edl)
111 if(audio_options && asset->format == FILE_AMPEG)
113 MPEGConfigAudio *window = new MPEGConfigAudio(parent_window, asset);
114 format_window = window;
115 window->create_objects();
116 window->run_window();
120 if(video_options && asset->format == FILE_VMPEG)
122 MPEGConfigVideo *window = new MPEGConfigVideo(parent_window, asset);
123 format_window = window;
124 window->create_objects();
125 window->run_window();
130 int FileMPEG::check_sig(Asset *asset)
132 return mpeg3_check_sig(asset->path);
135 void FileMPEG::get_info(char *title_path, char *path, char *text, int len)
141 int zio_access = ZIO_UNBUFFERED+ZIO_SINGLE_ACCESS;
142 if( !(fd=mpeg3_zopen(title_path, path, &result,zio_access)) ) result = 1;
143 if( !result ) result = mpeg3_create_title(fd, 0);
146 char *cp = text, *ep = text + len-1;
147 if( mpeg3_has_toc(fd) ) {
148 cp += snprintf(cp,ep-cp, _("toc path:%s\n"), path);
149 cp += snprintf(cp,ep-cp, _("title path:\n"));
150 for( int i=0; i<100; ++i ) {
151 char *title_path = mpeg3_title_path(fd,i);
152 if( !title_path ) break;
153 cp += snprintf(cp,ep-cp, " %2d. %s\n", i+1, title_path);
157 cp += snprintf(cp,ep-cp, _("file path:%s\n"), path);
158 int64_t bytes = mpeg3_get_bytes(fd);
159 char string[BCTEXTLEN];
160 sprintf(string,"%jd",bytes);
161 Units::punctuate(string);
162 cp += snprintf(cp,ep-cp, _("size: %s"), string);
164 if( mpeg3_is_program_stream(fd) )
165 cp += snprintf(cp,ep-cp, _(" program stream\n"));
166 else if( mpeg3_is_transport_stream(fd) )
167 cp += snprintf(cp,ep-cp, _(" transport stream\n"));
168 else if( mpeg3_is_video_stream(fd) )
169 cp += snprintf(cp,ep-cp, _(" video stream\n"));
170 else if( mpeg3_is_audio_stream(fd) )
171 cp += snprintf(cp,ep-cp, _(" audio stream\n"));
173 int64_t sdate = mpeg3_get_source_date(fd);
176 memset(&ostat,0,sizeof(struct stat64));
177 sdate = stat64(path, &ostat) < 0 ? 0 : ostat.st_mtime;
179 time_t tm = (time_t)sdate;
180 cp += snprintf(cp,ep-cp, _("date: %s\n"), ctime(&tm));
182 int vtrks = mpeg3_total_vstreams(fd);
183 cp += snprintf(cp,ep-cp, _("%d video tracks\n"), vtrks);
184 for( int vtrk=0; vtrk<vtrks; ++vtrk ) {
185 int cmdl = mpeg3_colormodel(fd, vtrk);
186 int color_model = bc_colormodel(cmdl);
187 char *cmodel = MPEGColorModel::cmodel_to_string(color_model);
188 int width = mpeg3_video_width(fd, vtrk);
189 int height = mpeg3_video_height(fd, vtrk);
190 cp += snprintf(cp,ep-cp, _(" v%d %s %dx%d"), vtrk, cmodel, width, height);
191 double frame_rate = mpeg3_frame_rate(fd, vtrk);
192 int64_t frames = mpeg3_video_frames(fd, vtrk);
193 cp += snprintf(cp,ep-cp, _(" (%5.2f), %jd frames"), frame_rate, frames);
194 if( frame_rate > 0 ) {
195 double secs = (double)frames / frame_rate;
196 cp += snprintf(cp,ep-cp, _(" (%0.3f secs)"),secs);
200 int atrks = mpeg3_total_astreams(fd);
201 cp += snprintf(cp,ep-cp, _("%d audio tracks\n"), atrks);
202 for( int atrk=0; atrk<atrks; ++atrk) {
203 const char *format = mpeg3_audio_format(fd, atrk);
204 cp += snprintf(cp,ep-cp, _(" a%d %s"), atrk, format);
205 int channels = mpeg3_audio_channels(fd, atrk);
206 int sample_rate = mpeg3_sample_rate(fd, atrk);
207 cp += snprintf(cp,ep-cp, _(" ch%d (%d)"), channels, sample_rate);
208 int64_t samples = mpeg3_audio_samples(fd, atrk);
209 cp += snprintf(cp,ep-cp, " %jd",samples);
210 int64_t nudge = mpeg3_get_audio_nudge(fd, atrk);
211 *cp++ = nudge >= 0 ? '+' : (nudge=-nudge, '-');
212 cp += snprintf(cp,ep-cp, _("%jd samples"),nudge);
213 if( sample_rate > 0 ) {
214 double secs = (double)(samples+nudge) / sample_rate;
215 cp += snprintf(cp,ep-cp, _(" (%0.3f secs)"),secs);
219 int stracks = mpeg3_subtitle_tracks(fd);
221 cp += snprintf(cp,ep-cp, _("%d subtitles\n"), stracks);
223 int vts_titles = mpeg3_get_total_vts_titles(fd);
225 cp += snprintf(cp,ep-cp, _("%d title sets, "), vts_titles);
226 int interleaves = mpeg3_get_total_interleaves(fd);
227 if( interleaves > 0 )
228 cp += snprintf(cp,ep-cp, _("%d interleaves\n"), interleaves);
229 int vts_title = mpeg3_set_vts_title(fd, -1);
230 int angle = mpeg3_set_angle(fd, -1);
231 int interleave = mpeg3_set_interleave(fd, -1);
232 int program = mpeg3_set_program(fd, -1);
233 cp += snprintf(cp,ep-cp, _("current program %d = title %d, angle %d, interleave %d\n\n"),
234 program, vts_title, angle, interleave);
236 ArrayList<double> cell_times;
237 int cell_no = 0; double cell_time;
238 while( !mpeg3_get_cell_time(fd, cell_no++, &cell_time) ) {
239 cell_times.append(cell_time);
241 if( cell_times.size() > 1 ) {
242 cp += snprintf(cp,ep-cp, _("cell times:"));
243 for( int i=0; i<cell_times.size(); ++i ) {
244 if( (i%4) == 0 ) *cp++ = '\n';
245 cp += snprintf(cp,ep-cp," %3d. %8.3f",i,cell_times.get(i));
247 cp += snprintf(cp,ep-cp, "\n");
250 int elements = mpeg3_dvb_channel_count(fd);
251 if( elements <= 0 ) return;
252 if( !mpeg3_dvb_get_system_time(fd, &sdate) ) {
254 cp += snprintf(cp,ep-cp, _("\nsystem time: %s"), ctime_r(&tm,string));
256 cp += snprintf(cp,ep-cp, _("elements %d\n"), elements);
258 for( int n=0; n<elements; ++n ) {
259 char name[16], enc[8]; int vstream, astream;
260 int major, minor, total_astreams, total_vstreams;
261 if( mpeg3_dvb_get_channel(fd,n, &major, &minor) ||
262 mpeg3_dvb_get_station_id(fd,n,&name[0]) ||
263 mpeg3_dvb_total_vstreams(fd,n,&total_vstreams) ||
264 mpeg3_dvb_total_astreams(fd,n,&total_astreams) ) continue;
265 cp += snprintf(cp,ep-cp, " %3d.%-3d %s", major, minor, &name[0]);
266 for( int vidx=0; vidx<total_vstreams; ++vidx ) {
267 if( mpeg3_dvb_vstream_number(fd,n,vidx,&vstream) ) continue;
268 if( vstream < 0 ) continue;
269 cp += snprintf(cp,ep-cp, " v%d", vstream);
271 for( int aidx=0; aidx<total_astreams; ++aidx ) {
272 if( mpeg3_dvb_astream_number(fd,n,aidx,&astream,&enc[0]) ) continue;
273 if( astream < 0 ) continue;
274 cp += snprintf(cp,ep-cp, " a%d %s", astream, &enc[0]);
276 for(int i=0; i<astream; ++i )
277 atrack += mpeg3_audio_channels(fd, i);
278 int channels = mpeg3_audio_channels(fd, astream);
279 cp += snprintf(cp,ep-cp, " trk %d-%d", atrack+1, atrack+channels);
280 if( enc[0] ) cp += snprintf(cp,ep-cp," (%s)",enc);
282 cp += snprintf(cp,ep-cp, "\n");
285 for( int n=0; n<elements; ++n ) {
287 if( mpeg3_dvb_get_channel(fd,n, &major, &minor) ) continue;
288 cp += snprintf(cp,ep-cp, "\n**chan %3d.%-3d\n", major, minor);
289 int len = mpeg3_dvb_get_chan_info(fd, n, -1, 0, cp, 1023);
290 if( len < 0 ) len = snprintf(cp,ep-cp,_("no info"));
291 cp += len; *cp++ = '*'; *cp++ = '*'; *cp++ = '\n';
292 for( int ord=0; ord<0x80; ++ord ) {
293 for( int i=0; (len=mpeg3_dvb_get_chan_info(fd,n,ord,i,cp,1023)) >= 0; ++i ) {
294 char *bp = cp; cp += len;
295 for( int k=2; --k>=0; ) { // skip 2 lines
296 while( bp<cp && *bp++!='\n' );
298 for( char *lp=bp; bp<cp; ++bp ) { // add new lines
299 if( *bp == '\n' || ((bp-lp)>=60 && *bp==' ') )
302 *cp++ = '\n'; *cp = 0; // trailing new line
312 int FileMPEG::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
316 int elements = mpeg3_dvb_channel_count(fd);
317 if( elements <= 0 ) return -1;
320 int total_astreams = 0, total_vstreams = 0;
321 for( int n=0; pidx<0 && n<elements; ++n ) {
322 total_astreams = total_vstreams = 0;
323 if( mpeg3_dvb_total_vstreams(fd,n,&total_vstreams) ||
324 mpeg3_dvb_total_astreams(fd,n,&total_astreams) ) continue;
325 if( !total_vstreams || !total_astreams ) continue;
326 for( int i=0; pidx<0 && i<total_vstreams; ++i ) {
328 if( mpeg3_dvb_vstream_number(fd,n,i,&vstrm) ) continue;
329 if( vstrm == vstream ) pidx = n;
332 if( pidx < 0 ) return -1;
334 int64_t channels = 0;
335 for( int i=0; i<total_astreams; ++i ) {
337 if( mpeg3_dvb_astream_number(fd,pidx,i,&astrm,0) ) continue;
338 if( astrm < 0 ) continue;
339 if( ret < 0 ) ret = astrm;
340 if( astream > 0 ) { --astream; continue; }
342 for(int i=0; i<astrm; ++i )
343 atrack += mpeg3_audio_channels(fd, i);
344 int64_t mask = (1 << mpeg3_audio_channels(fd, astrm)) - 1;
345 channels |= mask << atrack;
346 if( !astream ) break;
348 channel_mask = channels;
352 int FileMPEG::reset_parameters_derived()
365 twolame_allocation = 0;
374 lame_output_allocation = 0;
381 int FileMPEG::open_file(int rd, int wr)
387 char toc_name[BCTEXTLEN];
388 result = file->preferences->get_asset_file_path(asset, toc_name);
391 // if toc exists, use it otherwise just probe file
392 char *path = !result ? toc_name : asset->path;
393 fd = mpeg3_open_title(asset->path, path, &error);
396 case zmpeg3_t::ERR_INVALID_TOC_VERSION:
397 eprintf(_("Couldn't open %s: invalid table of contents version.\n"),asset->path);
400 case zmpeg3_t::ERR_TOC_DATE_MISMATCH:
401 eprintf(_("Couldn't open %s: table of contents out of date.\n"),asset->path);
405 eprintf(_("Couldn't open %s: table of contents corrupt.\n"),asset->path);
410 eprintf(_("Rebuilding the table of contents\n"));
415 if( mpeg3_has_toc(fd) )
417 else if( mpeg3_total_vstreams(fd) || mpeg3_total_astreams(fd) )
420 eprintf(_("Couldn't open %s: no audio or video.\n"),asset->path);
427 if( fd ) { mpeg3_close(fd); fd = 0; }
428 result = create_toc(toc_name);
432 mpeg3_set_cpus(fd, file->cpus < 4 ? file->cpus : 4);
433 file->current_program = mpeg3_set_program(fd, -1);
434 if( asset->program < 0 )
435 asset->program = file->current_program;
437 asset->audio_data = mpeg3_has_audio(fd);
438 if(asset->audio_data) {
440 for(int i = 0; i < mpeg3_total_astreams(fd); i++) {
441 asset->channels += mpeg3_audio_channels(fd, i);
443 if(!asset->sample_rate)
444 asset->sample_rate = mpeg3_sample_rate(fd, 0);
445 asset->audio_length = mpeg3_audio_samples(fd, 0);
446 if( !asset->channels || !asset->sample_rate )
450 asset->video_data = mpeg3_has_video(fd);
451 if( !result && asset->video_data ) {
452 //TODO: this is not as easy as just looking at headers.
453 //most interlaced media is rendered as FRM, not TOP/BOT in coding ext hdrs.
454 //currently, just using the assetedit menu to set the required result as needed.
455 // if( asset->interlace_mode == ILACE_MODE_UNDETECTED )
456 // asset->interlace_mode = mpeg3_detect_interlace(fd, 0);
457 if( !asset->layers ) {
458 asset->layers = mpeg3_total_vstreams(fd);
460 asset->actual_width = mpeg3_video_width(fd, 0);
462 asset->width = asset->actual_width;
463 asset->actual_height = mpeg3_video_height(fd, 0);
465 asset->height = asset->actual_height;
466 if( !asset->video_length )
467 asset->video_length = mpeg3_video_frames(fd, 0);
468 if( !asset->vmpeg_cmodel )
469 asset->vmpeg_cmodel = bc_colormodel(mpeg3_colormodel(fd, 0));
470 if( !asset->frame_rate )
471 asset->frame_rate = mpeg3_frame_rate(fd, 0);
475 eprintf(_("Couldn't open %s: failed.\n"), asset->path);
479 if( !result && wr && asset->format == FILE_VMPEG ) {
480 // Heroine Virtual encoder
481 // this one is cinelerra-x.x.x/mpeg2enc
482 if(asset->vmpeg_cmodel == BC_YUV422P)
484 char bitrate_string[BCTEXTLEN];
485 char quant_string[BCTEXTLEN];
486 char iframe_string[BCTEXTLEN];
488 sprintf(bitrate_string, "%d", asset->vmpeg_bitrate);
489 sprintf(quant_string, "%d", asset->vmpeg_quantization);
490 sprintf(iframe_string, "%d", asset->vmpeg_iframe_distance);
492 // Construct command line
495 const char *exec_path = File::get_cinlib_path();
496 snprintf(mjpeg_command, sizeof(mjpeg_command),
497 "%s/%s", exec_path, HVPEG_EXE);
498 append_vcommand_line(mjpeg_command);
500 if(asset->aspect_ratio > 0)
502 append_vcommand_line("-a");
504 if(EQUIV((double)asset->width / asset->height,
505 asset->aspect_ratio))
506 append_vcommand_line("1");
508 if(EQUIV(asset->aspect_ratio, 1.333))
509 append_vcommand_line("2");
511 if(EQUIV(asset->aspect_ratio, 1.777))
512 append_vcommand_line("3");
514 if(EQUIV(asset->aspect_ratio, 2.11))
515 append_vcommand_line("4");
518 append_vcommand_line(asset->vmpeg_derivative == 1 ? "-1" : "");
519 append_vcommand_line(asset->vmpeg_cmodel == BC_YUV422P ? "-422" : "");
520 if(asset->vmpeg_fix_bitrate)
522 append_vcommand_line("-b");
523 append_vcommand_line(bitrate_string);
527 append_vcommand_line("-q");
528 append_vcommand_line(quant_string);
530 append_vcommand_line("-n");
531 append_vcommand_line(iframe_string);
532 append_vcommand_line(asset->vmpeg_progressive ? "-p" : "");
533 append_vcommand_line(asset->vmpeg_denoise ? "-d" : "");
534 append_vcommand_line(file->cpus <= 1 ? "-u" : "");
535 append_vcommand_line(asset->vmpeg_seq_codes ? "-g" : "");
536 append_vcommand_line(asset->path);
538 video_out = new FileMPEGVideo(this);
543 // mjpegtools encoder
544 // this one is cinelerra-x.x.x/thirdparty/mjpegtools/mpeg2enc
546 const char *exec_path = File::get_cinlib_path();
547 snprintf(mjpeg_command, sizeof(mjpeg_command),
548 "%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 snprintf(string, sizeof(string),
564 " -b %d -q 1", asset->vmpeg_bitrate / 1000);
568 snprintf(string, sizeof(string),
569 " -b 0 -q %d", asset->vmpeg_quantization);
571 strncat(mjpeg_command, string, sizeof(mjpeg_command));
574 int aspect_ratio_code = -1;
575 if(asset->aspect_ratio > 0)
577 int ncodes = sizeof(aspect_ratio_codes) / sizeof(double);
578 for(int i = 1; i < ncodes; i++)
580 if(EQUIV(aspect_ratio_codes[i], asset->aspect_ratio))
582 aspect_ratio_code = i;
590 if(EQUIV((double)asset->width / asset->height, asset->aspect_ratio))
591 aspect_ratio_code = 1;
593 if(aspect_ratio_code < 0)
595 eprintf(_("Unsupported aspect ratio %f\n"), asset->aspect_ratio);
596 aspect_ratio_code = 2;
598 sprintf(string, " -a %d", aspect_ratio_code);
599 strncat(mjpeg_command, string, sizeof(mjpeg_command));
607 int frame_rate_code = -1;
608 int ncodes = sizeof(frame_rate_codes) / sizeof(double);
609 for(int i = 1; i < ncodes; ++i)
611 if(EQUIV(asset->frame_rate, frame_rate_codes[i]))
617 if(frame_rate_code < 0)
620 eprintf(_("Unsupported frame rate %f\n"), asset->frame_rate);
622 sprintf(string, " -F %d", frame_rate_code);
623 strncat(mjpeg_command, string, sizeof(mjpeg_command));
629 strncat(mjpeg_command,
630 asset->vmpeg_progressive ? " -I 0" : " -I 1",
631 sizeof(mjpeg_command));
635 sprintf(string, " -M %d", file->cpus);
636 strncat(mjpeg_command, string, sizeof(mjpeg_command));
639 if(!asset->vmpeg_progressive)
641 strncat(mjpeg_command,
642 asset->vmpeg_field_order ? " -z b" : " -z t",
643 sizeof(mjpeg_command));
647 snprintf(string, sizeof(string), " -f %d", asset->vmpeg_preset);
648 strncat(mjpeg_command, string, sizeof(mjpeg_command));
651 snprintf(string, sizeof(string),
652 " -g %d -G %d", asset->vmpeg_iframe_distance, asset->vmpeg_iframe_distance);
653 strncat(mjpeg_command, string, sizeof(mjpeg_command));
656 if(asset->vmpeg_seq_codes)
657 strncat(mjpeg_command, " -s", sizeof(mjpeg_command));
660 snprintf(string, sizeof(string),
661 " -R %d", CLAMP(asset->vmpeg_pframe_distance, 0, 2));
662 strncat(mjpeg_command, string, sizeof(mjpeg_command));
664 snprintf(string, sizeof(string), " -o '%s'", asset->path);
665 strncat(mjpeg_command, string, sizeof(mjpeg_command));
669 printf("FileMPEG::open_file: Running %s\n", mjpeg_command);
670 if(!(mjpeg_out = popen(mjpeg_command, "w")))
672 perror("FileMPEG::open_file");
673 eprintf(_("Error while opening \"%s\" for writing\n%m\n"), mjpeg_command);
677 video_out = new FileMPEGVideo(this);
681 else if( !result && wr && asset->format == FILE_AMPEG) {
682 //char encoder_string[BCTEXTLEN]; encoder_string[0] = 0;
683 //printf("FileMPEG::open_file 1 %d\n", asset->ampeg_derivative);
685 if(asset->ampeg_derivative == 2)
687 twofp = fopen(asset->path, "w" );
688 if( !twofp ) return 1;
689 twopts = twolame_init();
690 int channels = asset->channels >= 2 ? 2 : 1;
691 twolame_set_num_channels(twopts, channels);
692 twolame_set_in_samplerate(twopts, asset->sample_rate);
693 twolame_set_mode(twopts, channels >= 2 ?
694 TWOLAME_JOINT_STEREO : TWOLAME_MONO);
695 twolame_set_bitrate(twopts, asset->ampeg_bitrate);
696 twolame_init_params(twopts);
699 if(asset->ampeg_derivative == 3)
701 lame_global = lame_init();
702 // lame_set_brate(lame_global, asset->ampeg_bitrate / 1000);
703 lame_set_brate(lame_global, asset->ampeg_bitrate);
704 lame_set_quality(lame_global, 0);
705 lame_set_in_samplerate(lame_global,
707 lame_set_num_channels(lame_global,
709 if((result = lame_init_params(lame_global)) < 0)
711 eprintf(_("encode: lame_init_params returned %d\n"), result);
712 lame_close(lame_global);
716 if(!(lame_fd = fopen(asset->path, "w")))
718 perror("FileMPEG::open_file");
719 eprintf(_("Error while opening \"%s\" for writing\n%m\n"), asset->path);
720 lame_close(lame_global);
727 eprintf(_("ampeg_derivative=%d\n"), asset->ampeg_derivative);
732 // Transport stream for DVB capture
733 if( !result && !rd && !wr && asset->format == FILE_MPEG ) {
734 if( (recd_fd = open(asset->path, O_CREAT+O_TRUNC+O_WRONLY,
735 S_IRUSR+S_IWUSR + S_IRGRP+S_IWGRP)) < 0 ) {
736 perror("FileMPEG::open_file");
737 eprintf(_("Error while opening \"%s\" for writing\n%m\n"), asset->path);
751 int FileMPEG::set_skimming(int track, int skim, skim_fn fn, void *vp)
753 return !fn ? mpeg3_set_thumbnail_callback(fd, track, 0, 0, 0, 0) :
754 mpeg3_set_thumbnail_callback(fd, track, skim, 1, fn, vp);
757 int FileMPEG::skimming(void *vp, int track)
759 File *file = (File *)vp;
760 FileMPEG *mpeg = (FileMPEG *)file->file;
761 return mpeg->skim_result = mpeg->skim_callback(mpeg->skim_data, track);
764 int FileMPEG::skim_video(int track, void *vp, skim_fn fn)
766 skim_callback = fn; skim_data = vp;
767 mpeg3_set_thumbnail_callback(fd, track, 1, 1, skimming, (void*)file);
769 while( skim_result < 0 && !mpeg3_end_of_video(fd, track) )
770 mpeg3_drop_frames(fd, 1, track);
771 mpeg3_set_thumbnail_callback(fd, track, 0, 0, 0, 0);
777 #ifdef HAVE_COMMERCIAL
778 int FileMPEG::toc_nail(void *vp, int track)
780 File *file = (File *)vp;
781 FileMPEG *mpeg = (FileMPEG *)file->file;
782 int64_t framenum; uint8_t *tdat; int mw, mh;
783 if( mpeg->get_thumbnail(track, framenum, tdat, mw, mh) ) return 1;
784 int pid, width, height; double framerate;
785 if( mpeg->get_video_info(track, pid, framerate, width, height) ) return 1;
786 if( pid < 0 || framerate <= 0 ) return 1;
787 double position = framenum / framerate;
788 //printf("t%d/%03x f%jd, %dx%d %dx%d\n",track,pid,framenum,mw,mh,width,height);
789 MWindow::commercials->get_frame(file, pid, position, tdat, mw, mh, width, height);
794 int FileMPEG::create_toc(char *toc_path)
796 // delete any existing toc files
797 char toc_file[BCTEXTLEN];
798 strcpy(toc_file, toc_path);
799 if( strcmp(toc_file, asset->path) )
801 char *bp = strrchr(toc_file, '/');
802 if( !bp ) bp = toc_file;
803 char *sfx = strrchr(bp,'.');
809 int64_t total_bytes = 0, last_bytes = -1;
810 fd = mpeg3_start_toc(asset->path, toc_file,
811 file->current_program, &total_bytes);
813 eprintf(_("cant start toc/idx for file: %s\n"), asset->path);
817 // File needs a table of contents.
818 struct timeval new_time, prev_time, start_time, current_time;
819 gettimeofday(&prev_time, 0); gettimeofday(&start_time, 0);
820 #ifdef HAVE_COMMERCIAL
821 if( file->preferences->scan_commercials ) {
822 set_skimming(-1, 1, toc_nail, file);
823 if( MWindow::commercials->resetDb() != 0 )
824 eprintf(_("cant access commercials database"));
827 // This gets around the fact that MWindowGUI may be locked.
828 char progress_title[BCTEXTLEN];
829 sprintf(progress_title, _("Creating %s\n"), toc_file);
830 BC_ProgressBox progress(-1, -1, progress_title, total_bytes);
835 int64_t bytes_processed = 0;
836 if( mpeg3_do_toc(fd, &bytes_processed) ) break;
838 if( bytes_processed >= total_bytes ) break;
839 if( bytes_processed == last_bytes ) {
840 eprintf(_("toc scan stopped before eof"));
843 last_bytes = bytes_processed;
845 gettimeofday(&new_time, 0);
846 if( new_time.tv_sec - prev_time.tv_sec >= 1 ) {
847 gettimeofday(¤t_time, 0);
848 int64_t elapsed_seconds = current_time.tv_sec - start_time.tv_sec;
849 int64_t total_seconds = !bytes_processed ? 0 :
850 elapsed_seconds * total_bytes / bytes_processed;
851 int64_t eta = total_seconds - elapsed_seconds;
852 progress.update(bytes_processed, 1);
853 char string[BCTEXTLEN];
854 sprintf(string, "%sETA: %jdm%jds",
855 progress_title, eta / 60, eta % 60);
856 progress.update_title(string, 1);
857 // fprintf(stderr, "ETA: %dm%ds \r",
858 // bytes_processed * 100 / total_bytes,
859 // eta / 60, eta % 60);
861 prev_time = new_time;
864 if( progress.is_cancelled() ) {
870 #ifdef HAVE_COMMERCIAL
871 if( file->preferences->scan_commercials ) {
872 if( !result ) MWindow::commercials->write_ads(asset->path);
873 MWindow::commercials->closeDb();
880 progress.stop_progress();
883 remove_file(toc_file);
887 // Reopen file from toc path instead of asset path.
889 fd = mpeg3_open(toc_file, &error);
891 eprintf(_("mpeg3_open failed: %s"), toc_file);
897 int FileMPEG::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
900 IndexState *index_state = index_file->get_state();
901 index_state->reset_index();
902 index_state->reset_markers();
904 // Convert the index tables from tracks to channels.
905 int ntracks = mpeg3_index_tracks(fd);
906 if( !ntracks ) return 1;
908 int index_zoom = mpeg3_index_zoom(fd);
910 for( int i = 0; i < ntracks; ++i ) {
911 int nch = mpeg3_index_channels(fd, i);
912 for( int j = 0; j < nch; ++j ) {
913 float *bfr = (float *)mpeg3_index_data(fd, i, j);
914 int64_t size = 2*mpeg3_index_size(fd, i);
915 index_state->add_index_entry(bfr, offset, size);
921 int64_t file_bytes = fs.get_size(asset->path);
922 char *index_path = index_file->index_filename;
923 return index_state->write_index(index_path, asset, index_zoom, file_bytes);
931 void FileMPEG::append_vcommand_line(const char *string)
935 char *argv = cstrdup(string);
936 vcommand_line.append(argv);
940 int FileMPEG::close_file()
943 next_frame_lock->unlock();
952 // End of sequence signal
953 if(file->asset->vmpeg_cmodel == BC_YUV422P)
955 mpeg2enc_set_input_buffers(1, 0, 0, 0);
961 vcommand_line.remove_all_objects();
964 unsigned char opkt[1152*2];
965 int ret = twolame_encode_flush(twopts, opkt, sizeof(opkt));
967 fwrite(opkt, 1, ret, twofp);
969 fprintf(stderr, _("twolame error encoding audio: %d\n"), ret);
970 fclose(twofp); twofp = 0;
972 if( twopts ) { twolame_close(&twopts); twopts = 0; }
975 lame_close(lame_global);
977 if(temp_frame) delete temp_frame;
978 if(twolame_temp) delete [] twolame_temp;
980 if(lame_temp[0]) delete [] lame_temp[0];
981 if(lame_temp[1]) delete [] lame_temp[1];
982 if(lame_output) delete [] lame_output;
983 if(lame_fd) fclose(lame_fd);
985 if(mjpeg_out) pclose(mjpeg_out);
994 FileBase::close_file();
998 int FileMPEG::get_best_colormodel(Asset *asset, int driver)
1000 //printf("FileMPEG::get_best_colormodel 1\n");
1004 // return BC_RGB888;
1005 // the direct X11 color model requires scaling in the codec
1007 case PLAYBACK_X11_XV:
1008 case PLAYBACK_ASYNCHRONOUS:
1009 return zmpeg3_cmdl(asset->vmpeg_cmodel) > 0 ?
1010 asset->vmpeg_cmodel : BC_RGB888;
1011 case PLAYBACK_X11_GL:
1013 case PLAYBACK_DV1394:
1014 case PLAYBACK_FIREWIRE:
1017 return zmpeg3_cmdl(asset->vmpeg_cmodel) > 0 ?
1018 asset->vmpeg_cmodel : BC_RGB888;
1019 case VIDEO4LINUX2JPEG:
1020 return BC_COMPRESSED;
1022 case VIDEO4LINUX2MPEG:
1024 case CAPTURE_JPEG_WEBCAM:
1025 return BC_COMPRESSED;
1026 case CAPTURE_YUYV_WEBCAM:
1028 case CAPTURE_FIREWIRE:
1029 case CAPTURE_IEC61883:
1032 eprintf(_("unknown driver %d\n"),driver);
1036 int FileMPEG::colormodel_supported(int colormodel)
1041 int FileMPEG::can_copy_from(Asset *asset, int64_t position)
1046 int FileMPEG::set_audio_position(int64_t sample)
1051 int channel, stream;
1052 to_streamchannel(file->current_channel, stream, channel);
1054 //printf("FileMPEG::set_audio_position %d %d %d\n", sample, mpeg3_get_sample(fd, stream), last_sample);
1055 if(sample != mpeg3_get_sample(fd, stream) &&
1056 sample != last_sample)
1058 if(sample >= 0 && sample < asset->audio_length)
1060 //printf("FileMPEG::set_audio_position seeking stream %d\n", sample);
1061 return mpeg3_set_sample(fd, sample, stream);
1070 int FileMPEG::set_video_position(int64_t pos)
1072 if( !fd || pos < 0 || pos >= asset->video_length )
1074 //printf("FileMPEG::set_video_position 1 %jd\n", x);
1075 mpeg3_set_frame(fd, pos, file->current_layer);
1079 int64_t FileMPEG::get_memory_usage()
1081 int64_t result = file->rd && fd ? mpeg3_memory_usage(fd) : 0;
1082 //printf("FileMPEG::get_memory_usage %d %jd\n", __LINE__, result);
1086 int FileMPEG::set_program(int no)
1088 return fd ? mpeg3_set_program(fd, no) : -1;
1091 int FileMPEG::get_cell_time(int no, double &time)
1093 return fd ? mpeg3_get_cell_time(fd, no, &time) : -1;
1096 int FileMPEG::get_system_time(int64_t &tm)
1098 return fd ? mpeg3_dvb_get_system_time(fd, &tm) : -1;
1101 int FileMPEG::get_video_pid(int track)
1103 return fd ? mpeg3_video_pid(fd, track) : -1;
1106 int FileMPEG::get_video_info(int track, int &pid,
1107 double &framerate, int &width, int &height, char *title)
1109 if( !fd ) return -1;
1110 pid = mpeg3_video_pid(fd, track);
1111 framerate = mpeg3_frame_rate(fd, track);
1112 width = mpeg3_video_width(fd, track);
1113 height = mpeg3_video_height(fd, track);
1114 if( !title ) return 0;
1117 int elements = mpeg3_dvb_channel_count(fd);
1118 for( int n=0; n<elements; ++n ) {
1119 int major, minor, total_vstreams, vstream, vidx;
1120 if( mpeg3_dvb_get_channel(fd,n, &major, &minor) ||
1121 mpeg3_dvb_total_vstreams(fd,n,&total_vstreams) ) continue;
1122 for( vidx=0; vidx<total_vstreams; ++vidx ) {
1123 if( mpeg3_dvb_vstream_number(fd,n,vidx,&vstream) ) continue;
1124 if( vstream < 0 ) continue;
1125 if( vstream == track ) {
1126 sprintf(title, "%3d.%-3d", major, minor);
1134 int FileMPEG::select_video_stream(Asset *asset, int vstream)
1136 if( !fd ) return -1;
1137 asset->width = mpeg3_video_width(fd, vstream);
1138 asset->height = mpeg3_video_height(fd, vstream);
1139 asset->video_length = mpeg3_video_frames(fd, vstream);
1140 asset->frame_rate = mpeg3_frame_rate(fd, vstream);
1144 int FileMPEG::select_audio_stream(Asset *asset, int astream)
1146 if( !fd ) return -1;
1147 asset->sample_rate = mpeg3_sample_rate(fd, astream);
1148 asset->audio_length = mpeg3_audio_samples(fd, astream);
1152 int FileMPEG::get_thumbnail(int stream,
1153 int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
1156 mpeg3_get_thumbnail(fd, stream, &position, &thumbnail, &ww, &hh);
1159 int FileMPEG::write_samples(double **buffer, int64_t len)
1163 //printf("FileMPEG::write_samples 1\n");
1164 if(asset->ampeg_derivative == 2) {
1166 int channels = MIN(asset->channels, 2);
1167 int64_t audio_size = len * channels * 2;
1168 if(twolame_allocation < audio_size) {
1169 if(twolame_temp) delete [] twolame_temp;
1170 twolame_temp = new unsigned char[audio_size];
1171 twolame_allocation = audio_size;
1172 if(twolame_out) delete [] twolame_out;
1173 twolame_out = new unsigned char[audio_size + 1152];
1176 for(int i = 0; i < channels; i++) {
1177 int16_t *output = ((int16_t*)twolame_temp) + i;
1178 double *input = buffer[i];
1179 for(int j = 0; j < len; j++) {
1180 int sample = (int)(*input * 0x7fff);
1181 *output = (int16_t)(CLIP(sample, -0x8000, 0x7fff));
1186 int ret = twolame_encode_buffer_interleaved(twopts,
1187 (int16_t*)twolame_temp, len,
1188 twolame_out, twolame_allocation+1152);
1190 fwrite(twolame_out, 1, ret, twofp);
1192 fprintf(stderr, _("twolame error encoding audio: %d\n"), ret);
1195 if(asset->ampeg_derivative == 3)
1197 int channels = MIN(asset->channels, 2);
1198 int64_t audio_size = len * channels;
1199 if(!lame_global) return 1;
1200 if(!lame_fd) return 1;
1201 if(lame_allocation < audio_size)
1203 if(lame_temp[0]) delete [] lame_temp[0];
1204 if(lame_temp[1]) delete [] lame_temp[1];
1205 lame_temp[0] = new float[audio_size];
1206 lame_temp[1] = new float[audio_size];
1207 lame_allocation = audio_size;
1210 if(lame_output_allocation < audio_size * 4)
1212 if(lame_output) delete [] lame_output;
1213 lame_output_allocation = audio_size * 4;
1214 lame_output = new char[lame_output_allocation];
1217 for(int i = 0; i < channels; i++)
1219 float *output = lame_temp[i];
1220 double *input = buffer[i];
1221 for(int j = 0; j < len; j++)
1223 *output++ = *input++ * (float)32768;
1227 result = lame_encode_buffer_float(lame_global,
1229 (channels > 1) ? lame_temp[1] : lame_temp[0],
1231 (unsigned char*)lame_output,
1232 lame_output_allocation);
1235 char *real_output = lame_output;
1239 for(int i = 0; i < bytes; i++)
1242 real_output = &lame_output[i];
1248 if(bytes > 0 && lame_started)
1250 result = !fwrite(real_output, 1, bytes, lame_fd);
1252 perror("FileMPEG::write_samples");
1253 eprintf(_("write failed: %m"));
1266 int FileMPEG::write_frames(VFrame ***frames, int len)
1272 int temp_w = (int)((asset->width + 15) / 16) * 16;
1275 int output_cmodel = asset->vmpeg_cmodel;
1276 // verify colormodel supported in MPEG output
1277 switch( output_cmodel ) {
1279 if( file->preferences->dvd_yuv420p_interlace &&
1280 ( asset->interlace_mode == ILACE_MODE_TOP_FIRST ||
1281 asset->interlace_mode == ILACE_MODE_BOTTOM_FIRST ) )
1282 output_cmodel = BC_YUV420PI;
1289 // Height depends on progressiveness
1290 if(asset->vmpeg_progressive || asset->vmpeg_derivative == 1)
1291 temp_h = (int)((asset->height + 15) / 16) * 16;
1293 temp_h = (int)((asset->height + 31) / 32) * 32;
1295 //printf("FileMPEG::write_frames 1\n");
1297 // Only 1 layer is supported in MPEG output
1298 for(int i = 0; i < 1; i++)
1300 for(int j = 0; j < len && !result; j++)
1302 VFrame *frame = frames[i][j];
1306 if(asset->vmpeg_cmodel == BC_YUV422P)
1308 if(frame->get_w() == temp_w &&
1309 frame->get_h() == temp_h &&
1310 frame->get_color_model() == output_cmodel)
1312 mpeg2enc_set_input_buffers(0,
1313 (char*)frame->get_y(),
1314 (char*)frame->get_u(),
1315 (char*)frame->get_v());
1320 (temp_frame->get_w() != temp_w ||
1321 temp_frame->get_h() != temp_h ||
1322 temp_frame->get_color_model() || output_cmodel))
1331 temp_frame = new VFrame(temp_w, temp_h,
1335 BC_CModels::transfer(temp_frame->get_rows(),
1337 temp_frame->get_y(),
1338 temp_frame->get_u(),
1339 temp_frame->get_v(),
1349 temp_frame->get_w(),
1350 temp_frame->get_h(),
1351 frame->get_color_model(),
1352 temp_frame->get_color_model(),
1355 temp_frame->get_w());
1357 mpeg2enc_set_input_buffers(0,
1358 (char*)temp_frame->get_y(),
1359 (char*)temp_frame->get_u(),
1360 (char*)temp_frame->get_v());
1365 // MJPEG uses the same dimensions as the input
1366 //printf("FileMPEG::write_frames %d\n", __LINE__);sleep(1);
1367 if(frame->get_color_model() == output_cmodel)
1369 mjpeg_y = frame->get_y();
1370 mjpeg_u = frame->get_u();
1371 mjpeg_v = frame->get_v();
1375 //printf("FileMPEG::write_frames %d\n", __LINE__);sleep(1);
1378 temp_frame = new VFrame(asset->width, asset->height,
1382 // printf("FileMPEG::write_frames %d temp_frame=%p %p %p %p frame=%p %p %p %p color_model=%p %p\n",
1385 // temp_frame->get_w(),
1386 // temp_frame->get_h(),
1390 // temp_frame->get_color_model(),
1391 // frame->get_color_model()); sleep(1);
1392 temp_frame->transfer_from(frame);
1393 //printf("FileMPEG::write_frames %d\n", __LINE__);sleep(1);
1395 mjpeg_y = temp_frame->get_y();
1396 mjpeg_u = temp_frame->get_u();
1397 mjpeg_v = temp_frame->get_v();
1403 next_frame_lock->unlock();
1404 next_frame_done->lock("FileMPEG::write_frames");
1405 if(mjpeg_error) result = 1;
1421 int FileMPEG::zmpeg3_cmdl(int colormodel)
1423 switch( colormodel ) {
1424 case BC_BGR888: return zmpeg3_t::cmdl_BGR888;
1425 case BC_BGR8888: return zmpeg3_t::cmdl_BGRA8888;
1426 case BC_RGB565: return zmpeg3_t::cmdl_RGB565;
1427 case BC_RGB888: return zmpeg3_t::cmdl_RGB888;
1428 case BC_RGBA8888: return zmpeg3_t::cmdl_RGBA8888;
1429 case BC_RGBA16161616: return zmpeg3_t::cmdl_RGBA16161616;
1430 case BC_YUV420P: return zmpeg3_t::cmdl_YUV420P;
1431 case BC_YUV422P: return zmpeg3_t::cmdl_YUV422P;
1432 case BC_YUV422: return zmpeg3_t::cmdl_YUYV;
1433 case BC_YUV888: return zmpeg3_t::cmdl_YUV888;
1434 case BC_YUVA8888: return zmpeg3_t::cmdl_YUVA8888;
1439 int FileMPEG::bc_colormodel(int cmdl)
1442 case zmpeg3_t::cmdl_BGR888: return BC_BGR888;
1443 case zmpeg3_t::cmdl_BGRA8888: return BC_BGR8888;
1444 case zmpeg3_t::cmdl_RGB565: return BC_RGB565;
1445 case zmpeg3_t::cmdl_RGB888: return BC_RGB888;
1446 case zmpeg3_t::cmdl_RGBA8888: return BC_RGBA8888;
1447 case zmpeg3_t::cmdl_RGBA16161616: return BC_RGBA16161616;
1448 case zmpeg3_t::cmdl_YUV420P: return BC_YUV420P;
1449 case zmpeg3_t::cmdl_YUV422P: return BC_YUV422P;
1450 case zmpeg3_t::cmdl_YUYV: return BC_YUV422;
1451 case zmpeg3_t::cmdl_YUV888: return BC_YUV888;
1452 case zmpeg3_t::cmdl_YUVA8888: return BC_YUVA8888;
1457 const char *FileMPEG::zmpeg3_cmdl_name(int cmdl)
1459 # define CMDL(nm) #nm
1460 static const char *cmdl_name[] = {
1486 return cmdl>=0 && cmdl<lengthof(cmdl_name) ? cmdl_name[cmdl] : cmdl_name[6];
1490 int FileMPEG::read_frame(VFrame *frame)
1494 int width = mpeg3_video_width(fd,file->current_layer);
1495 int height = mpeg3_video_height(fd,file->current_layer);
1496 int stream_cmdl = mpeg3_colormodel(fd,file->current_layer);
1497 int stream_color_model = bc_colormodel(stream_cmdl);
1498 int frame_color_model = frame->get_color_model();
1499 int frame_cmdl = asset->interlace_mode == ILACE_MODE_NOTINTERLACED ?
1500 zmpeg3_cmdl(frame_color_model) : -1;
1501 mpeg3_show_subtitle(fd, file->current_layer, file->playback_subtitle);
1503 switch( frame_color_model ) { // check for direct copy
1505 if( frame_cmdl < 0 ) break;
1507 if( stream_color_model == frame_color_model &&
1508 width == frame->get_w() && height == frame->get_h() ) {
1509 mpeg3_read_yuvframe(fd,
1510 (char*)frame->get_y(),
1511 (char*)frame->get_u(),
1512 (char*)frame->get_v(),
1513 0, 0, width, height,
1514 file->current_layer);
1519 if( frame_cmdl >= 0 ) { // supported by read_frame
1520 // cant rely on frame->get_rows(), format may not support it
1522 switch( frame_color_model ) {
1523 case BC_YUV420P: uvs = 2; break;
1524 case BC_YUV422P: uvs = 1; break;
1526 //int w = frame->get_w();
1527 int h = frame->get_h();
1528 int bpl = frame->get_bytes_per_line();
1529 int uvw = !uvs ? 0 : bpl / 2;
1530 int uvh = !uvs ? 0 : h / uvs;
1531 uint8_t *rows[h + 2*uvh], *rp;
1533 if( (rp=frame->get_y()) ) {
1534 for( int i=0; i<h; ++i, rp+=bpl ) rows[n++] = rp;
1535 rp = frame->get_u();
1536 for( int i=0; i<uvh; ++i, rp+=uvw ) rows[n++] = rp;
1537 rp = frame->get_v();
1538 for( int i=0; i<uvh; ++i, rp+=uvw ) rows[n++] = rp;
1541 rp = frame->get_data(); uvh *= 2;
1542 for( int i=0; i<h; ++i, rp+=bpl ) rows[n++] = rp;
1543 for( int i=0; i<uvh; ++i, rp+=uvw ) rows[n++] = rp;
1546 mpeg3_read_frame(fd,
1547 rows, /* start of each output row */
1548 0, 0, width, height, /* input box */
1549 frame->get_w(), /* Dimensions of output_rows */
1552 file->current_layer);
1557 mpeg3_read_yuvframe_ptr(fd, &y, &u, &v, file->current_layer);
1559 if( stream_color_model == BC_YUV420P &&
1560 file->preferences->dvd_yuv420p_interlace && (
1561 asset->interlace_mode == ILACE_MODE_TOP_FIRST ||
1562 asset->interlace_mode == ILACE_MODE_BOTTOM_FIRST ) )
1563 stream_color_model = BC_YUV420PI;
1564 BC_CModels::transfer(frame->get_rows(), 0,
1565 frame->get_y(), frame->get_u(), frame->get_v(),
1566 (unsigned char*)y, (unsigned char*)u, (unsigned char*)v,
1567 0,0, width,height, 0,0, frame->get_w(),frame->get_h(),
1568 stream_color_model, frame_color_model, 0, width, frame->get_w());
1575 void FileMPEG::to_streamchannel(int channel, int &stream_out, int &channel_out)
1577 int total_astreams = mpeg3_total_astreams(fd);
1578 for(stream_out = 0; stream_out < total_astreams; ++stream_out )
1580 int stream_channels = mpeg3_audio_channels(fd, stream_out);
1581 if( channel < stream_channels ) break;
1582 channel -= stream_channels;
1584 channel_out = channel;
1587 int FileMPEG::read_samples(double *buffer, int64_t len)
1590 if(len < 0) return 0;
1592 // Translate pure channel to a stream and a channel in the mpeg stream
1593 int stream, channel;
1594 to_streamchannel(file->current_channel, stream, channel);
1596 //printf("FileMPEG::read_samples 1 current_sample=%jd len=%jd channel=%d\n", file->current_sample, len, channel);
1598 mpeg3_set_sample(fd,
1599 file->current_sample,
1601 mpeg3_read_audio_d(fd,
1602 buffer, /* Pointer to pre-allocated buffer of doubles */
1603 channel, /* Channel to decode */
1604 len, /* Number of samples to decode */
1605 stream); /* Stream containing the channel */
1607 // last_sample = file->current_sample;
1616 FileMPEGVideo::FileMPEGVideo(FileMPEG *file)
1622 if(file->asset->vmpeg_cmodel == BC_YUV422P)
1624 mpeg2enc_init_buffers();
1625 mpeg2enc_set_w(file->asset->width);
1626 mpeg2enc_set_h(file->asset->height);
1627 mpeg2enc_set_rate(file->asset->frame_rate);
1631 FileMPEGVideo::~FileMPEGVideo()
1636 void FileMPEGVideo::run()
1638 if(file->asset->vmpeg_cmodel == BC_YUV422P)
1640 printf("FileMPEGVideo::run ");
1641 for(int i = 0; i < file->vcommand_line.total; i++)
1642 printf("%s ", file->vcommand_line.values[i]);
1644 mpeg2enc(file->vcommand_line.total, file->vcommand_line.values);
1650 //printf("FileMPEGVideo::run %d\n", __LINE__);
1651 file->next_frame_lock->lock("FileMPEGVideo::run");
1652 //printf("FileMPEGVideo::run %d\n", __LINE__);
1655 file->next_frame_done->unlock();
1661 //printf("FileMPEGVideo::run %d\n", __LINE__);
1662 // YUV4 sequence header
1663 if(!file->wrote_header)
1665 file->wrote_header = 1;
1667 char interlace_string[BCTEXTLEN];
1668 if(!file->asset->vmpeg_progressive)
1670 sprintf(interlace_string, file->asset->vmpeg_field_order ? "b" : "t");
1674 sprintf(interlace_string, "p");
1676 double aspect_ratio = file->asset->aspect_ratio >= 0 ?
1677 file->asset->aspect_ratio : 1.0;
1678 //printf("FileMPEGVideo::run %d\n", __LINE__);
1679 fprintf(file->mjpeg_out, "YUV4MPEG2 W%d H%d F%d:%d I%s A%d:%d C%s\n",
1680 file->asset->width, file->asset->height,
1681 (int)(file->asset->frame_rate * 1001),
1682 1001, interlace_string,
1683 (int)(aspect_ratio * 1000), 1000,
1685 //printf("FileMPEGVideo::run %d\n", __LINE__);
1688 // YUV4 frame header
1689 //printf("FileMPEGVideo::run %d\n", __LINE__);
1690 fprintf(file->mjpeg_out, "FRAME\n");
1693 //printf("FileMPEGVideo::run %d\n", __LINE__);
1694 if(!fwrite(file->mjpeg_y, file->asset->width * file->asset->height, 1, file->mjpeg_out))
1695 file->mjpeg_error = 1;
1696 //printf("FileMPEGVideo::run %d\n", __LINE__);
1697 if(!fwrite(file->mjpeg_u, file->asset->width * file->asset->height / 4, 1, file->mjpeg_out))
1698 file->mjpeg_error = 1;
1699 //printf("FileMPEGVideo::run %d\n", __LINE__);
1700 if(!fwrite(file->mjpeg_v, file->asset->width * file->asset->height / 4, 1, file->mjpeg_out))
1701 file->mjpeg_error = 1;
1702 //printf("FileMPEGVideo::run %d\n", __LINE__);
1703 fflush(file->mjpeg_out);
1705 //printf("FileMPEGVideo::run %d\n", __LINE__);
1706 file->next_frame_done->unlock();
1707 //printf("FileMPEGVideo::run %d\n", __LINE__);
1709 pclose(file->mjpeg_out);
1710 file->mjpeg_out = 0;
1725 MPEGConfigAudio::MPEGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
1726 : BC_Window(_(PROGRAM_NAME ": Audio Compression"),
1727 parent_window->get_abs_cursor_x(1),
1728 parent_window->get_abs_cursor_y(1),
1737 this->parent_window = parent_window;
1738 this->asset = asset;
1741 MPEGConfigAudio::~MPEGConfigAudio()
1745 void MPEGConfigAudio::create_objects()
1751 lock_window("MPEGConfigAudio::create_objects");
1752 if(asset->format == FILE_MPEG)
1754 add_subwindow(new BC_Title(x, y, _("No options for MPEG transport stream.")));
1760 add_tool(new BC_Title(x, y, _("Layer:")));
1761 add_tool(layer = new MPEGLayer(x1, y, this));
1762 layer->create_objects();
1765 add_tool(new BC_Title(x, y, _("Kbits per second:")));
1766 add_tool(bitrate = new MPEGABitrate(x1, y, this));
1767 bitrate->create_objects();
1770 add_subwindow(new BC_OKButton(this));
1775 int MPEGConfigAudio::close_event()
1787 MPEGLayer::MPEGLayer(int x, int y, MPEGConfigAudio *gui)
1788 : BC_PopupMenu(x, y, 100, layer_to_string(gui->asset->ampeg_derivative))
1793 void MPEGLayer::create_objects()
1795 add_item(new BC_MenuItem(layer_to_string(2)));
1796 add_item(new BC_MenuItem(layer_to_string(3)));
1799 int MPEGLayer::handle_event()
1801 gui->asset->ampeg_derivative = string_to_layer(get_text());
1802 gui->bitrate->set_layer(gui->asset->ampeg_derivative);
1806 int MPEGLayer::string_to_layer(char *string)
1808 if(!strcasecmp(layer_to_string(2), string))
1810 if(!strcasecmp(layer_to_string(3), string))
1816 char* MPEGLayer::layer_to_string(int layer)
1840 MPEGABitrate::MPEGABitrate(int x, int y, MPEGConfigAudio *gui)
1844 bitrate_to_string(gui->string, gui->asset->ampeg_bitrate))
1849 void MPEGABitrate::create_objects()
1851 set_layer(gui->asset->ampeg_derivative);
1854 void MPEGABitrate::set_layer(int layer)
1856 while(total_items()) del_item(0);
1860 add_item(new BC_MenuItem("160"));
1861 add_item(new BC_MenuItem("192"));
1862 add_item(new BC_MenuItem("224"));
1863 add_item(new BC_MenuItem("256"));
1864 add_item(new BC_MenuItem("320"));
1865 add_item(new BC_MenuItem("384"));
1869 add_item(new BC_MenuItem("8"));
1870 add_item(new BC_MenuItem("16"));
1871 add_item(new BC_MenuItem("24"));
1872 add_item(new BC_MenuItem("32"));
1873 add_item(new BC_MenuItem("40"));
1874 add_item(new BC_MenuItem("48"));
1875 add_item(new BC_MenuItem("56"));
1876 add_item(new BC_MenuItem("64"));
1877 add_item(new BC_MenuItem("80"));
1878 add_item(new BC_MenuItem("96"));
1879 add_item(new BC_MenuItem("112"));
1880 add_item(new BC_MenuItem("128"));
1881 add_item(new BC_MenuItem("144"));
1882 add_item(new BC_MenuItem("160"));
1883 add_item(new BC_MenuItem("192"));
1884 add_item(new BC_MenuItem("224"));
1885 add_item(new BC_MenuItem("256"));
1886 add_item(new BC_MenuItem("320"));
1890 int MPEGABitrate::handle_event()
1892 gui->asset->ampeg_bitrate = string_to_bitrate(get_text());
1896 int MPEGABitrate::string_to_bitrate(char *string)
1898 return atol(string);
1902 char* MPEGABitrate::bitrate_to_string(char *string, int bitrate)
1904 sprintf(string, "%d", bitrate);
1916 MPEGConfigVideo::MPEGConfigVideo(BC_WindowBase *parent_window,
1918 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
1919 parent_window->get_abs_cursor_x(1),
1920 parent_window->get_abs_cursor_y(1),
1929 this->parent_window = parent_window;
1930 this->asset = asset;
1934 MPEGConfigVideo::~MPEGConfigVideo()
1938 void MPEGConfigVideo::create_objects()
1944 lock_window("MPEGConfigVideo::create_objects");
1945 if(asset->format == FILE_MPEG)
1947 add_subwindow(new BC_Title(x, y, _("No options for MPEG transport stream.")));
1952 add_subwindow(new BC_Title(x, y, _("Color model:")));
1953 add_subwindow(cmodel = new MPEGColorModel(x1, y, this));
1954 cmodel->create_objects();
1957 update_cmodel_objs();
1959 add_subwindow(new BC_OKButton(this));
1964 int MPEGConfigVideo::close_event()
1971 void MPEGConfigVideo::delete_cmodel_objs()
1976 delete fixed_bitrate;
1979 delete iframe_distance;
1980 delete pframe_distance;
1981 delete top_field_first;
1985 titles.remove_all_objects();
1989 void MPEGConfigVideo::reset_cmodel()
1997 iframe_distance = 0;
1998 pframe_distance = 0;
1999 top_field_first = 0;
2005 void MPEGConfigVideo::update_cmodel_objs()
2013 delete_cmodel_objs();
2015 if(asset->vmpeg_cmodel == BC_YUV420P)
2017 add_subwindow(title = new BC_Title(x, y + 5, _("Format Preset:")));
2018 titles.append(title);
2019 add_subwindow(preset = new MPEGPreset(x1, y, this));
2020 preset->create_objects();
2024 add_subwindow(title = new BC_Title(x, y + 5, _("Derivative:")));
2025 titles.append(title);
2026 add_subwindow(derivative = new MPEGDerivative(x1, y, this));
2027 derivative->create_objects();
2030 add_subwindow(title = new BC_Title(x, y + 5, _("Bitrate:")));
2031 titles.append(title);
2032 add_subwindow(bitrate = new MPEGBitrate(x1, y, this));
2033 add_subwindow(fixed_bitrate = new MPEGFixedBitrate(x2, y, this));
2036 add_subwindow(title = new BC_Title(x, y, _("Quantization:")));
2037 titles.append(title);
2038 quant = new MPEGQuant(x1, y, this);
2039 quant->create_objects();
2040 add_subwindow(fixed_quant = new MPEGFixedQuant(x2, y, this));
2043 add_subwindow(title = new BC_Title(x, y, _("I frame distance:")));
2044 titles.append(title);
2045 iframe_distance = new MPEGIFrameDistance(x1, y, this);
2046 iframe_distance->create_objects();
2049 if(asset->vmpeg_cmodel == BC_YUV420P)
2051 add_subwindow(title = new BC_Title(x, y, _("P frame distance:")));
2052 titles.append(title);
2053 pframe_distance = new MPEGPFrameDistance(x1, y, this);
2054 pframe_distance->create_objects();
2057 add_subwindow(top_field_first = new BC_CheckBox(x, y, &asset->vmpeg_field_order, _("Bottom field first")));
2061 add_subwindow(progressive = new BC_CheckBox(x, y, &asset->vmpeg_progressive, _("Progressive frames")));
2063 add_subwindow(denoise = new BC_CheckBox(x, y, &asset->vmpeg_denoise, _("Denoise")));
2065 add_subwindow(seq_codes = new BC_CheckBox(x, y, &asset->vmpeg_seq_codes, _("Sequence start codes in every GOP")));
2070 MPEGDerivative::MPEGDerivative(int x, int y, MPEGConfigVideo *gui)
2071 : BC_PopupMenu(x, y, 150, derivative_to_string(gui->asset->vmpeg_derivative))
2076 void MPEGDerivative::create_objects()
2078 add_item(new BC_MenuItem(derivative_to_string(1)));
2079 add_item(new BC_MenuItem(derivative_to_string(2)));
2082 int MPEGDerivative::handle_event()
2084 gui->asset->vmpeg_derivative = string_to_derivative(get_text());
2088 int MPEGDerivative::string_to_derivative(char *string)
2090 if( !strcasecmp(derivative_to_string(1), string) ) return 1;
2091 if( !strcasecmp(derivative_to_string(2), string) ) return 2;
2095 char* MPEGDerivative::derivative_to_string(int derivative)
2097 switch(derivative) {
2098 case 1: return _("MPEG-1");
2099 case 2: return _("MPEG-2");
2105 MPEGPreset::MPEGPreset(int x, int y, MPEGConfigVideo *gui)
2106 : BC_PopupMenu(x, y, 200, value_to_string(gui->asset->vmpeg_preset))
2111 void MPEGPreset::create_objects()
2113 for(int i = 0; i < 14; i++) {
2114 add_item(new BC_MenuItem(value_to_string(i)));
2118 int MPEGPreset::handle_event()
2120 gui->asset->vmpeg_preset = string_to_value(get_text());
2124 int MPEGPreset::string_to_value(char *string)
2126 for(int i = 0; i < 14; i++) {
2127 if(!strcasecmp(value_to_string(i), string))
2133 char* MPEGPreset::value_to_string(int derivative)
2135 switch( derivative ) {
2136 case 0: return _("Generic MPEG-1"); break;
2137 case 1: return _("standard VCD"); break;
2138 case 2: return _("user VCD"); break;
2139 case 3: return _("Generic MPEG-2"); break;
2140 case 4: return _("standard SVCD"); break;
2141 case 5: return _("user SVCD"); break;
2142 case 6: return _("VCD Still sequence"); break;
2143 case 7: return _("SVCD Still sequence"); break;
2144 case 8: return _("DVD NAV"); break;
2145 case 9: return _("DVD"); break;
2146 case 10: return _("ATSC 480i"); break;
2147 case 11: return _("ATSC 480p"); break;
2148 case 12: return _("ATSC 720p"); break;
2149 case 13: return _("ATSC 1080i"); break;
2151 return _("Generic MPEG-1");
2164 MPEGBitrate::MPEGBitrate(int x, int y, MPEGConfigVideo *gui)
2165 : BC_TextBox(x, y, 100, 1, gui->asset->vmpeg_bitrate)
2171 int MPEGBitrate::handle_event()
2173 gui->asset->vmpeg_bitrate = atol(get_text());
2181 MPEGQuant::MPEGQuant(int x, int y, MPEGConfigVideo *gui)
2182 : BC_TumbleTextBox(gui,
2183 (int64_t)gui->asset->vmpeg_quantization,
2193 int MPEGQuant::handle_event()
2195 gui->asset->vmpeg_quantization = atol(get_text());
2199 MPEGFixedBitrate::MPEGFixedBitrate(int x, int y, MPEGConfigVideo *gui)
2200 : BC_Radial(x, y, gui->asset->vmpeg_fix_bitrate, _("Fixed bitrate"))
2205 int MPEGFixedBitrate::handle_event()
2208 gui->asset->vmpeg_fix_bitrate = 1;
2209 gui->fixed_quant->update(0);
2213 MPEGFixedQuant::MPEGFixedQuant(int x, int y, MPEGConfigVideo *gui)
2214 : BC_Radial(x, y, !gui->asset->vmpeg_fix_bitrate, _("Fixed quantization"))
2219 int MPEGFixedQuant::handle_event()
2222 gui->asset->vmpeg_fix_bitrate = 0;
2223 gui->fixed_bitrate->update(0);
2235 MPEGIFrameDistance::MPEGIFrameDistance(int x, int y, MPEGConfigVideo *gui)
2236 : BC_TumbleTextBox(gui,
2237 (int64_t)gui->asset->vmpeg_iframe_distance,
2247 int MPEGIFrameDistance::handle_event()
2249 gui->asset->vmpeg_iframe_distance = atoi(get_text());
2259 MPEGPFrameDistance::MPEGPFrameDistance(int x, int y, MPEGConfigVideo *gui)
2260 : BC_TumbleTextBox(gui,
2261 (int64_t)gui->asset->vmpeg_pframe_distance,
2271 int MPEGPFrameDistance::handle_event()
2273 gui->asset->vmpeg_pframe_distance = atoi(get_text());
2284 MPEGColorModel::MPEGColorModel(int x, int y, MPEGConfigVideo *gui)
2285 : BC_PopupMenu(x, y, 150, cmodel_to_string(gui->asset->vmpeg_cmodel))
2290 void MPEGColorModel::create_objects()
2292 add_item(new BC_MenuItem(cmodel_to_string(BC_YUV420P)));
2293 add_item(new BC_MenuItem(cmodel_to_string(BC_YUV422P)));
2296 int MPEGColorModel::handle_event()
2298 gui->asset->vmpeg_cmodel = string_to_cmodel(get_text());
2299 gui->update_cmodel_objs();
2300 gui->show_window(1);
2304 int MPEGColorModel::string_to_cmodel(char *string)
2306 if(!strcasecmp(cmodel_to_string(BC_YUV420P), string))
2308 if(!strcasecmp(cmodel_to_string(BC_YUV422P), string))
2313 char* MPEGColorModel::cmodel_to_string(int cmodel)
2317 case BC_YUV420P: return _("YUV 4:2:0");
2318 case BC_YUV422P: return _("YUV 4:2:2");
2319 default: return _("YUV 4:2:0");