add system build to autoconf/automake
[goodguy/history.git] / cinelerra-5.1 / cinelerra / filempeg.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
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.
10  * 
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.
15  * 
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
19  * 
20  */
21
22 #include "asset.h"
23 #include "bcprogressbox.h"
24 #include "bcsignals.h"
25 #include "bitspopup.h"
26 #include "byteorder.h"
27 #include "clip.h"
28 #include "commercials.h"
29 #include "condition.h"
30 #include "cstrdup.h"
31 #include "edit.h"
32 #include "file.h"
33 #include "filempeg.h"
34 #include "filesystem.h"
35 #include "guicast.h"
36 #include "indexfile.h"
37 #include "interlacemodes.h"
38 #include "indexstate.h"
39 #include "language.h"
40 #include "mainerror.h"
41 #include "mwindow.h"
42 #include "pipe.h"
43 #include "preferences.h"
44 #include "removefile.h"
45 #include "vframe.h"
46 #include "videodevice.inc"
47
48 #include <stdio.h>
49 #include <string.h>
50 #include <unistd.h>
51
52
53 #define HVPEG_EXE "/hveg2enc"
54 #define MJPEG_EXE "/mpeg2enc"
55
56
57 // M JPEG dependancies
58 static double frame_rate_codes[] = 
59 {
60         0,
61         24000.0/1001.0,
62         24.0,
63         25.0,
64         30000.0/1001.0,
65         30.0,
66         50.0,
67         60000.0/1001.0,
68         60.0
69 };
70
71 static double aspect_ratio_codes[] =
72 {
73         0,
74         1.0,
75         1.333,
76         1.777,
77         2.21
78 };
79
80
81
82
83
84
85
86
87 FileMPEG::FileMPEG(Asset *asset, File *file)
88  : FileBase(asset, file)
89 {
90         reset_parameters();
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();
97 }
98
99 FileMPEG::~FileMPEG()
100 {
101         close_file();
102         delete next_frame_lock;
103         delete next_frame_done;
104         vcommand_line.remove_all_objects();
105 }
106
107 void FileMPEG::get_parameters(BC_WindowBase *parent_window, 
108         Asset *asset, 
109         BC_WindowBase* &format_window,
110         int audio_options,
111         int video_options)
112 {
113         if(audio_options && asset->format == FILE_AMPEG)
114         {
115                 MPEGConfigAudio *window = new MPEGConfigAudio(parent_window, asset);
116                 format_window = window;
117                 window->create_objects();
118                 window->run_window();
119                 delete window;
120         }
121         else
122         if(video_options && asset->format == FILE_VMPEG)
123         {
124                 MPEGConfigVideo *window = new MPEGConfigVideo(parent_window, asset);
125                 format_window = window;
126                 window->create_objects();
127                 window->run_window();
128                 delete window;
129         }
130 }
131
132 int FileMPEG::check_sig(Asset *asset)
133 {
134         return mpeg3_check_sig(asset->path);
135 }
136
137 void FileMPEG::get_info(char *title_path, char *path, char *text, int len)
138 {
139         mpeg3_t *fd;
140         *text = 0;
141
142         int result = 0;
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);
146         if( result ) return;
147
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);
156                 }
157         }
158         else
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,"%ld",bytes);
163         Units::punctuate(string);
164         cp += snprintf(cp,ep-cp, _("size: %s"), string);
165
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"));
174
175         int64_t sdate = mpeg3_get_source_date(fd);
176         if( !sdate ) {
177                 struct stat64 ostat;
178                 memset(&ostat,0,sizeof(struct stat64));
179                 sdate = stat64(path, &ostat) < 0 ? 0 : ostat.st_mtime;
180         }
181         time_t tm = (time_t)sdate;
182         cp += snprintf(cp,ep-cp, _("date: %s\n"), ctime(&tm));
183
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), %ld 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);
199                 }
200                 *cp++ = '\n';
201         }
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, " %ld",samples);
212                 int64_t nudge = mpeg3_get_audio_nudge(fd, atrk);
213                 *cp++ = nudge >= 0 ? '+' : (nudge=-nudge, '-');
214                 cp += snprintf(cp,ep-cp, _("%ld 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);
218                 }
219                 *cp++ = '\n';
220         }
221         int stracks = mpeg3_subtitle_tracks(fd);
222         if( stracks > 0 ) {
223                 cp += snprintf(cp,ep-cp, _("%d subtitles\n"), stracks);
224         }
225         int vts_titles = mpeg3_get_total_vts_titles(fd);
226         if( vts_titles > 0 )
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);
237
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);
242         }
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));
248                 }
249                 cp += snprintf(cp,ep-cp, "\n");
250         }
251
252         int elements = mpeg3_dvb_channel_count(fd);
253         if( elements <= 0 ) return;
254         if( !mpeg3_dvb_get_system_time(fd, &sdate) ) {
255                 tm = (time_t)sdate;
256                 cp += snprintf(cp,ep-cp, _("\nsystem time: %s"), ctime_r(&tm,string));
257         }
258         cp += snprintf(cp,ep-cp, _("elements %d\n"), elements);
259
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);
272                 }
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]);
277                         int atrack = 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);
283                 }
284                 cp += snprintf(cp,ep-cp, "\n");
285         }
286
287         for( int n=0; n<elements; ++n ) {
288                 int major, minor;
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' );
299                                 }
300                                 for( char *lp=bp; bp<cp; ++bp ) {  // add new lines
301                                         if( *bp == '\n' || ((bp-lp)>=60 && *bp==' ') )
302                                                 *(lp=bp) = '\n';
303                                 }
304                                 *cp++ = '\n';  *cp = 0;  // trailing new line
305                         }
306                 }
307         }
308
309         *cp = 0;
310         mpeg3_close(fd);
311         return;
312 }
313
314 int FileMPEG::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
315 {
316         channel_mask = 0;
317         if( !fd ) return -1;
318         int elements = mpeg3_dvb_channel_count(fd);
319         if( elements <= 0 ) return -1;
320
321         int pidx = -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 ) {
329                         int vstrm = -1;
330                         if( mpeg3_dvb_vstream_number(fd,n,i,&vstrm) ) continue;
331                         if( vstrm == vstream ) pidx = n;
332                 }
333         }
334         if( pidx < 0 ) return -1;
335         int ret = -1;
336         int64_t channels = 0;
337         for( int i=0; i<total_astreams; ++i ) {
338                 int astrm = -1;
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; }
343                 int atrack = 0;
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;
349         }
350         channel_mask = channels;
351         return ret;
352 }
353
354 int FileMPEG::reset_parameters_derived()
355 {
356         wrote_header = 0;
357         mjpeg_out = 0;
358         mjpeg_eof = 0;
359         mjpeg_error = 0;
360         recd_fd = -1;
361         fd = 0;
362         video_out = 0;
363         prev_track = 0;
364         temp_frame = 0;
365         twolame_temp = 0;
366         twolame_out = 0;
367         twolame_allocation = 0;
368         twolame_result = 0;
369         twofp = 0;
370         twopts = 0;
371         lame_temp[0] = 0;
372         lame_temp[1] = 0;
373         lame_allocation = 0;
374         lame_global = 0;
375         lame_output = 0;
376         lame_output_allocation = 0;
377         lame_fd = 0;
378         lame_started = 0;
379         return 0;
380 }
381
382
383 int FileMPEG::open_file(int rd, int wr)
384 {
385         int result = 0;
386
387         if(rd) {
388                 fd = 0;
389                 char toc_name[BCTEXTLEN];
390                 result = file->preferences->get_asset_file_path(asset, toc_name);
391                 if( result >= 0 ) {
392                         int error = 0;
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);
396                         if( !fd ) {
397                                 switch( error ) {
398                                 case zmpeg3_t::ERR_INVALID_TOC_VERSION:
399                                         eprintf(_("Couldn't open %s: invalid table of contents version.\n"),asset->path);
400                                         result = 1;
401                                         break;
402                                 case zmpeg3_t::ERR_TOC_DATE_MISMATCH:
403                                         eprintf(_("Couldn't open %s: table of contents out of date.\n"),asset->path);
404                                         result = 1;
405                                         break;
406                                 default:
407                                         eprintf(_("Couldn't open %s: table of contents corrupt.\n"),asset->path);
408                                         result = -1;
409                                         break;
410                                 }
411                                 if( result > 0 ) {
412                                         eprintf(_("Rebuilding the table of contents\n"));
413                                 }
414
415                         }
416                         else {
417                                 if( mpeg3_has_toc(fd) )
418                                         result = 0;
419                                 else if( mpeg3_total_vstreams(fd) || mpeg3_total_astreams(fd) )
420                                         result = 1;
421                                 else {
422                                         eprintf(_("Couldn't open %s: no audio or video.\n"),asset->path);
423                                         result = -1;
424                                 }
425                         }
426                 }
427
428                 if( result > 0 ) {
429                         if( fd ) { mpeg3_close(fd);  fd = 0; }
430                         result = create_toc(toc_name);
431                 }
432
433                 if( !result ) {
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;
438
439                         asset->audio_data = mpeg3_has_audio(fd);
440                         if(asset->audio_data) {
441                                 asset->channels = 0;
442                                 for(int i = 0; i < mpeg3_total_astreams(fd); i++) {
443                                         asset->channels += mpeg3_audio_channels(fd, i);
444                                 }
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 )
449                                         result = 1;
450                         }
451
452                         asset->video_data = mpeg3_has_video(fd);
453                         if( !result && asset->video_data ) {
454                                 asset->interlace_mode = BC_ILACE_MODE_UNDETECTED;
455                                 if( !asset->layers ) {
456                                         asset->layers = mpeg3_total_vstreams(fd);
457                                 }
458                                 asset->actual_width = mpeg3_video_width(fd, 0);
459                                 if( !asset->width )
460                                         asset->width = asset->actual_width;
461                                 asset->actual_height = mpeg3_video_height(fd, 0);
462                                 if( !asset->height )
463                                         asset->height = asset->actual_height;
464                                 if( !asset->video_length )
465                                         asset->video_length = mpeg3_video_frames(fd, 0);
466                                 if( !asset->vmpeg_cmodel )
467                                         asset->vmpeg_cmodel = bc_colormodel(mpeg3_colormodel(fd, 0));
468                                 if( !asset->frame_rate )
469                                         asset->frame_rate = mpeg3_frame_rate(fd, 0);
470                         }
471                 }
472                 if( result ) {
473                         eprintf(_("Couldn't open %s: failed.\n"), asset->path);
474                 }
475         }
476         
477         if( !result && wr && asset->format == FILE_VMPEG ) {
478 // Heroine Virtual encoder
479 //  this one is cinelerra-x.x.x/mpeg2enc
480                 if(asset->vmpeg_cmodel == BC_YUV422P)
481                 {
482                         char bitrate_string[BCTEXTLEN];
483                         char quant_string[BCTEXTLEN];
484                         char iframe_string[BCTEXTLEN];
485
486                         sprintf(bitrate_string, "%d", asset->vmpeg_bitrate);
487                         sprintf(quant_string, "%d", asset->vmpeg_quantization);
488                         sprintf(iframe_string, "%d", asset->vmpeg_iframe_distance);
489
490 // Construct command line
491                         if(!result)
492                         {
493                                 const char *exec_path = File::get_cinlib_path();
494                                 sprintf(mjpeg_command, "%s/%s", exec_path, HVPEG_EXE);
495
496                                 if(asset->aspect_ratio > 0)
497                                 {
498                                         append_vcommand_line("-a");
499 // Square pixels
500                                         if(EQUIV((double)asset->width / asset->height,
501                                                 asset->aspect_ratio))
502                                                 append_vcommand_line("1");
503                                         else
504                                         if(EQUIV(asset->aspect_ratio, 1.333))
505                                                 append_vcommand_line("2");
506                                         else
507                                         if(EQUIV(asset->aspect_ratio, 1.777))
508                                                 append_vcommand_line("3");
509                                         else
510                                         if(EQUIV(asset->aspect_ratio, 2.11))
511                                                 append_vcommand_line("4");
512                                 }
513
514                                 append_vcommand_line(asset->vmpeg_derivative == 1 ? "-1" : "");
515                                 append_vcommand_line(asset->vmpeg_cmodel == BC_YUV422P ? "-422" : "");
516                                 if(asset->vmpeg_fix_bitrate)
517                                 {
518                                         append_vcommand_line("--cbr -b");
519                                         append_vcommand_line(bitrate_string);
520                                 }
521                                 else
522                                 {
523                                         append_vcommand_line("-q");
524                                         append_vcommand_line(quant_string);
525                                 }
526                                 append_vcommand_line("-n");
527                                 append_vcommand_line(iframe_string);
528                                 append_vcommand_line(asset->vmpeg_progressive ? "-p" : "");
529                                 append_vcommand_line(asset->vmpeg_denoise ? "-d" : "");
530                                 append_vcommand_line(file->cpus <= 1 ? "-u" : "");
531                                 append_vcommand_line(asset->vmpeg_seq_codes ? "-g" : "");
532                                 append_vcommand_line(asset->path);
533
534                                 video_out = new FileMPEGVideo(this);
535                                 video_out->start();
536                         }
537                 }
538                 else
539 // mjpegtools encoder
540 //  this one is cinelerra-x.x.x/thirdparty/mjpegtools/mpeg2enc
541                 {
542                         const char *exec_path = File::get_cinlib_path();
543                         sprintf(mjpeg_command, "%s/%s -v 0 ", exec_path, MJPEG_EXE);
544
545 // Must disable interlacing if MPEG-1
546                         switch (asset->vmpeg_preset)
547                         {
548                                 case 0: asset->vmpeg_progressive = 1; break;
549                                 case 1: asset->vmpeg_progressive = 1; break;
550                                 case 2: asset->vmpeg_progressive = 1; break;
551                         }
552
553 // Be quiet
554                         strcat(mjpeg_command, " -v0");
555
556                         char string[BCTEXTLEN];
557 // The current usage of mpeg2enc requires bitrate of 0 when quantization is fixed and
558 // quantization of 1 when bitrate is fixed.  Perfectly intuitive.
559                         if(asset->vmpeg_fix_bitrate)
560                         {
561                                 sprintf(string, " -b %d -q 1", asset->vmpeg_bitrate / 1000);
562                         }
563                         else
564                         {
565                                 sprintf(string, " -b 0 -q %d", asset->vmpeg_quantization);
566                         }
567                         strcat(mjpeg_command, string);
568
569
570
571
572
573
574 // Aspect ratio
575                         int aspect_ratio_code = -1;
576                         if(asset->aspect_ratio > 0)
577                         {
578                                 int ncodes = sizeof(aspect_ratio_codes) / sizeof(double);
579                                 for(int i = 1; i < ncodes; i++)
580                                 {
581                                         if(EQUIV(aspect_ratio_codes[i], asset->aspect_ratio))
582                                         {
583                                                 aspect_ratio_code = i;
584                                                 break;
585                                         }
586                                 }
587                         }
588
589
590 // Square pixels
591                         if(EQUIV((double)asset->width / asset->height, asset->aspect_ratio))
592                                 aspect_ratio_code = 1;
593                         
594                         if(aspect_ratio_code < 0)
595                         {
596                                 eprintf(_("Unsupported aspect ratio %f\n"), asset->aspect_ratio);
597                                 aspect_ratio_code = 2;
598                         }
599                         sprintf(string, " -a %d", aspect_ratio_code);
600                         strcat(mjpeg_command, string);
601
602
603
604
605
606
607 // Frame rate
608                         int frame_rate_code = -1;
609                         int ncodes = sizeof(frame_rate_codes) / sizeof(double);
610                         for(int i = 1; i < ncodes; ++i)
611                         {
612                                 if(EQUIV(asset->frame_rate, frame_rate_codes[i]))
613                                 {
614                                         frame_rate_code = i;
615                                         break;
616                                 }
617                         }
618                         if(frame_rate_code < 0)
619                         {
620                                 frame_rate_code = 4;
621                                 eprintf(_("Unsupported frame rate %f\n"), asset->frame_rate);
622                         }
623                         sprintf(string, " -F %d", frame_rate_code);
624                         strcat(mjpeg_command, string);
625
626
627
628
629
630                         strcat(mjpeg_command, 
631                                 asset->vmpeg_progressive ? " -I 0" : " -I 1");
632                         
633
634
635                         sprintf(string, " -M %d", file->cpus);
636                         strcat(mjpeg_command, string);
637
638
639                         if(!asset->vmpeg_progressive)
640                         {
641                                 strcat(mjpeg_command, asset->vmpeg_field_order ? " -z b" : " -z t");
642                         }
643
644
645                         sprintf(string, " -f %d", asset->vmpeg_preset);
646                         strcat(mjpeg_command, string);
647
648
649                         sprintf(string, " -g %d -G %d", asset->vmpeg_iframe_distance, asset->vmpeg_iframe_distance);
650                         strcat(mjpeg_command, string);
651
652
653                         if(asset->vmpeg_seq_codes) strcat(mjpeg_command, " -s");
654
655
656                         sprintf(string, " -R %d", CLAMP(asset->vmpeg_pframe_distance, 0, 2));
657                         strcat(mjpeg_command, string);
658
659                         sprintf(string, " -o '%s'", asset->path);
660                         strcat(mjpeg_command, string);
661
662
663
664                         printf("FileMPEG::open_file: Running %s\n", mjpeg_command);
665                         if(!(mjpeg_out = popen(mjpeg_command, "w")))
666                         {
667                                 perror("FileMPEG::open_file");
668                                 eprintf(_("Error while opening \"%s\" for writing\n%m\n"), mjpeg_command);
669                                 return 1;
670                         }
671
672                         video_out = new FileMPEGVideo(this);
673                         video_out->start();
674                 }
675         }
676         else if( !result && wr && asset->format == FILE_AMPEG) {
677                 //char encoder_string[BCTEXTLEN]; encoder_string[0] = 0;
678 //printf("FileMPEG::open_file 1 %d\n", asset->ampeg_derivative);
679
680                 if(asset->ampeg_derivative == 2)
681                 {
682                         twofp = fopen(asset->path, "w" );
683                         if( !twofp ) return 1;
684                         twopts = twolame_init();
685                         int channels = asset->channels >= 2 ? 2 : 1;
686                         twolame_set_num_channels(twopts, channels);
687                         twolame_set_in_samplerate(twopts, asset->sample_rate);
688                         twolame_set_mode(twopts, channels >= 2 ?
689                                 TWOLAME_JOINT_STEREO : TWOLAME_MONO);
690                         twolame_set_bitrate(twopts, asset->ampeg_bitrate);
691                         twolame_init_params(twopts);
692                 }
693                 else
694                 if(asset->ampeg_derivative == 3)
695                 {
696                         lame_global = lame_init();
697 //                      lame_set_brate(lame_global, asset->ampeg_bitrate / 1000);
698                         lame_set_brate(lame_global, asset->ampeg_bitrate);
699                         lame_set_quality(lame_global, 0);
700                         lame_set_in_samplerate(lame_global, 
701                                 asset->sample_rate);
702                         lame_set_num_channels(lame_global,
703                                 asset->channels);
704                         if((result = lame_init_params(lame_global)) < 0)
705                         {
706                                 eprintf(_("encode: lame_init_params returned %d\n"), result);
707                                 lame_close(lame_global);
708                                 lame_global = 0;
709                         }
710                         else
711                         if(!(lame_fd = fopen(asset->path, "w")))
712                         {
713                                 perror("FileMPEG::open_file");
714                                 eprintf(_("Error while opening \"%s\" for writing\n%m\n"), asset->path);
715                                 lame_close(lame_global);
716                                 lame_global = 0;
717                                 result = 1;
718                         }
719                 }
720                 else
721                 {
722                         eprintf(_("ampeg_derivative=%d\n"), asset->ampeg_derivative);
723                         result = 1;
724                 }
725         }
726
727 // Transport stream for DVB capture
728         if( !result && !rd && !wr && asset->format == FILE_MPEG ) {
729                 if( (recd_fd = open(asset->path, O_CREAT+O_TRUNC+O_WRONLY,
730                         S_IRUSR+S_IWUSR + S_IRGRP+S_IWGRP)) < 0 ) {
731                         perror("FileMPEG::open_file");
732                         eprintf(_("Error while opening \"%s\" for writing\n%m\n"), asset->path);
733                         result = 1;
734                 }
735         }
736
737
738 //asset->dump();
739         return result;
740 }
741
742
743
744
745
746 int FileMPEG::set_skimming(int track, int skim, skim_fn fn, void *vp)
747 {
748         return !fn ? mpeg3_set_thumbnail_callback(fd, track, 0, 0, 0, 0) :
749                 mpeg3_set_thumbnail_callback(fd, track, skim, 1, fn, vp);
750 }
751
752 int FileMPEG::skimming(void *vp, int track)
753 {
754         File *file = (File *)vp;
755         FileMPEG *mpeg = (FileMPEG *)file->file;
756         return mpeg->skim_result = mpeg->skim_callback(mpeg->skim_data, track);
757 }
758
759 int FileMPEG::skim_video(int track, void *vp, skim_fn fn)
760 {
761         skim_callback = fn;  skim_data = vp;
762         mpeg3_set_thumbnail_callback(fd, track, 1, 1, skimming, (void*)file);
763         skim_result = -1;
764         while( skim_result < 0 && !mpeg3_end_of_video(fd, track) )
765                 mpeg3_drop_frames(fd, 1, track);
766         mpeg3_set_thumbnail_callback(fd, track, 0, 0, 0, 0);
767         return skim_result;
768 }
769
770
771
772 int FileMPEG::toc_nail(void *vp, int track)
773 {
774         File *file = (File *)vp;
775         FileMPEG *mpeg = (FileMPEG *)file->file;
776         int64_t framenum; uint8_t *tdat; int mw, mh;
777         if( mpeg->get_thumbnail(track, framenum, tdat, mw, mh) ) return 1;
778         int pid, width, height;  double framerate;
779         if( mpeg->get_video_info(track, pid, framerate, width, height) ) return 1;
780         if( pid < 0 || framerate <= 0 ) return 1;
781         double position = framenum / framerate;
782 //printf("t%d/%03x f"_LD", %dx%d %dx%d\n",track,pid,framenum,mw,mh,width,height);
783         MWindow::commercials->get_frame(file, pid, position, tdat, mw, mh, width, height);
784         return 0;
785 }
786
787
788 int FileMPEG::create_toc(char *toc_path)
789 {
790 // delete any existing toc files
791         char toc_file[BCTEXTLEN];
792         strcpy(toc_file, toc_path);
793         remove(toc_file);
794         char *bp = strrchr(toc_file, '/');
795         if( !bp ) bp = toc_file;
796         char *sfx = strrchr(bp,'.');
797         if( sfx ) {
798                 strcpy(sfx+1,"toc");
799                 remove(toc_file);
800         }
801
802         int64_t total_bytes = 0, last_bytes = -1;
803         fd = mpeg3_start_toc(asset->path, toc_file,
804                                 file->current_program, &total_bytes);
805         if( !fd ) {
806                 eprintf(_("cant start toc/idx for file: %s\n"), asset->path);
807                 return 1;
808         }
809
810 // File needs a table of contents.
811         struct timeval new_time, prev_time, start_time, current_time;
812         gettimeofday(&prev_time, 0);  gettimeofday(&start_time, 0);
813         if( file->preferences->scan_commercials ) {
814                 set_skimming(-1, 1, toc_nail, file);
815                 if( MWindow::commercials->resetDb() != 0 )
816                         eprintf(_("cant access commercials database"));
817         }
818 // This gets around the fact that MWindowGUI may be locked.
819         char progress_title[BCTEXTLEN];
820         sprintf(progress_title, _("Creating %s\n"), toc_file);
821         BC_ProgressBox progress(-1, -1, progress_title, total_bytes);
822         progress.start();
823
824         int result = 0;
825         while( !result ) {
826                 int64_t bytes_processed = 0;
827                 if( mpeg3_do_toc(fd, &bytes_processed) ) break;
828
829                 if( bytes_processed >= total_bytes ) break;
830                 if( bytes_processed == last_bytes ) {
831                         eprintf(_("toc scan stopped before eof"));
832                         break;
833                 }
834                 last_bytes = bytes_processed;
835
836                 gettimeofday(&new_time, 0);
837                 if( new_time.tv_sec - prev_time.tv_sec >= 1 ) {
838                         gettimeofday(&current_time, 0);
839                         int64_t elapsed_seconds = current_time.tv_sec - start_time.tv_sec;
840                         int64_t total_seconds = !bytes_processed ? 0 :
841                                  elapsed_seconds * total_bytes / bytes_processed;
842                         int64_t eta = total_seconds - elapsed_seconds;
843                         progress.update(bytes_processed, 1);
844                         char string[BCTEXTLEN];
845                         sprintf(string, "%sETA: %jdm%jds",
846                                 progress_title, eta / 60, eta % 60);
847                         progress.update_title(string, 1);
848 //                      fprintf(stderr, "ETA: %dm%ds        \r", 
849 //                              bytes_processed * 100 / total_bytes,
850 //                              eta / 60, eta % 60);
851 //                      fflush(stdout);
852                         prev_time = new_time;
853                 }
854
855                 if( progress.is_cancelled() ) {
856                         result = 1;
857                         break;
858                 }
859         }
860
861         if( file->preferences->scan_commercials ) {
862                 if( !result ) MWindow::commercials->write_ads(asset->path);
863                 MWindow::commercials->closeDb();
864         }
865
866         mpeg3_stop_toc(fd);
867         fd = 0;
868
869         progress.stop_progress();
870
871         if( result ) {
872                 remove_file(toc_file);
873                 return 1;
874         }
875
876 // Reopen file from toc path instead of asset path.
877         int error = 0;
878         fd = mpeg3_open(toc_file, &error);
879         if( !fd ) {
880                 eprintf(_("mpeg3_open failed: %s"), toc_file);
881                 return 1;
882         }
883         return 0;
884 }
885
886 int FileMPEG::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
887 {
888         if( !fd ) return 1;
889         IndexState *index_state = index_file->get_state();
890         index_state->reset_index();
891         index_state->reset_markers();
892
893 // Convert the index tables from tracks to channels.
894         int ntracks = mpeg3_index_tracks(fd);
895         if( !ntracks ) return 1;
896
897         int index_zoom = mpeg3_index_zoom(fd);
898         int64_t offset = 0;
899         for( int i = 0; i < ntracks; ++i ) {
900                 int nch = mpeg3_index_channels(fd, i);
901                 for( int j = 0; j < nch; ++j ) {
902                         float *bfr = (float *)mpeg3_index_data(fd, i, j);
903                         int64_t size = 2*mpeg3_index_size(fd, i);
904                         index_state->add_index_entry(bfr, offset, size);
905                         offset += size;
906                 }
907         }
908
909         FileSystem fs;
910         int64_t file_bytes = fs.get_size(asset->path);
911         char *index_path = index_file->index_filename;
912         return index_state->write_index(index_path, asset, index_zoom, file_bytes);
913 }
914
915
916
917
918
919
920 void FileMPEG::append_vcommand_line(const char *string)
921 {
922         if(string[0])
923         {
924                 char *argv = cstrdup(string);
925                 vcommand_line.append(argv);
926         }
927 }
928
929 int FileMPEG::close_file()
930 {
931         mjpeg_eof = 1;
932         next_frame_lock->unlock();
933
934         if(fd)
935         {
936                 mpeg3_close(fd);
937         }
938
939         if(video_out)
940         {
941 // End of sequence signal
942                 if(file->asset->vmpeg_cmodel == BC_YUV422P)
943                 {
944                         mpeg2enc_set_input_buffers(1, 0, 0, 0);
945                 }
946                 delete video_out;
947                 video_out = 0;
948         }
949
950         vcommand_line.remove_all_objects();
951
952         if(twofp) {
953                 unsigned char opkt[1152*2]; 
954                 int ret = twolame_encode_flush(twopts, opkt, sizeof(opkt));
955                 if( ret > 0 )
956                         fwrite(opkt, 1, ret, twofp);
957                 else if( ret < 0 )
958                         fprintf(stderr, _("twolame error encoding audio: %d\n"), ret);
959                 fclose(twofp);  twofp = 0;
960         }
961         if( twopts ) { twolame_close(&twopts); twopts = 0; }
962
963         if(lame_global)
964                 lame_close(lame_global);
965
966         if(temp_frame) delete temp_frame;
967         if(twolame_temp) delete [] twolame_temp;
968
969         if(lame_temp[0]) delete [] lame_temp[0];
970         if(lame_temp[1]) delete [] lame_temp[1];
971         if(lame_output) delete [] lame_output;
972         if(lame_fd) fclose(lame_fd);
973
974         if(mjpeg_out) pclose(mjpeg_out);
975
976         if( recd_fd >= 0 ) {
977                 close(recd_fd);
978                 recd_fd = -1;
979         }
980
981         reset_parameters();
982
983         FileBase::close_file();
984         return 0;
985 }
986
987 int FileMPEG::get_best_colormodel(Asset *asset, int driver)
988 {
989 //printf("FileMPEG::get_best_colormodel 1\n");
990         switch(driver)
991         {
992                 case PLAYBACK_X11:
993                         return BC_RGB888;
994                 case PLAYBACK_X11_XV:
995                 case PLAYBACK_ASYNCHRONOUS:
996                         return zmpeg3_cmdl(asset->vmpeg_cmodel) > 0 ?
997                                 asset->vmpeg_cmodel : BC_RGB888;
998                 case PLAYBACK_X11_GL:
999                         return BC_YUV888;
1000                 case PLAYBACK_LML:
1001                 case PLAYBACK_BUZ:
1002                         return BC_YUV422P;
1003                 case PLAYBACK_DV1394:
1004                 case PLAYBACK_FIREWIRE:
1005                         return BC_YUV422P;
1006                 case VIDEO4LINUX:
1007                 case VIDEO4LINUX2:
1008                         return zmpeg3_cmdl(asset->vmpeg_cmodel) > 0 ?
1009                                 asset->vmpeg_cmodel : BC_RGB888;
1010                 case VIDEO4LINUX2JPEG:
1011                         return BC_COMPRESSED;
1012                 case CAPTURE_DVB:
1013                 case VIDEO4LINUX2MPEG:
1014                         return BC_YUV422P;
1015                 case CAPTURE_JPEG_WEBCAM:
1016                         return BC_COMPRESSED;
1017                 case CAPTURE_YUYV_WEBCAM:
1018                         return BC_YUV422;
1019                 case CAPTURE_BUZ:
1020                 case CAPTURE_LML:
1021                         return BC_YUV422;
1022                 case CAPTURE_FIREWIRE:
1023                 case CAPTURE_IEC61883:
1024                         return BC_YUV422P;
1025         }
1026         eprintf(_("unknown driver %d\n"),driver);
1027         return BC_RGB888;
1028 }
1029
1030 int FileMPEG::colormodel_supported(int colormodel)
1031 {
1032         return colormodel;
1033 }
1034
1035 int FileMPEG::can_copy_from(Asset *asset, int64_t position)
1036 {
1037         return 0;
1038 }
1039
1040 int FileMPEG::set_audio_position(int64_t sample)
1041 {
1042 #if 0
1043         if(!fd) return 1;
1044         
1045         int channel, stream;
1046         to_streamchannel(file->current_channel, stream, channel);
1047
1048 //printf("FileMPEG::set_audio_position %d %d %d\n", sample, mpeg3_get_sample(fd, stream), last_sample);
1049         if(sample != mpeg3_get_sample(fd, stream) &&
1050                 sample != last_sample)
1051         {
1052                 if(sample >= 0 && sample < asset->audio_length)
1053                 {
1054 //printf("FileMPEG::set_audio_position seeking stream %d\n", sample);
1055                         return mpeg3_set_sample(fd, sample, stream);
1056                 }
1057                 else
1058                         return 1;
1059         }
1060 #endif
1061         return 0;
1062 }
1063
1064 int FileMPEG::set_video_position(int64_t pos)
1065 {
1066         if( !fd || pos < 0 || pos >= asset->video_length )
1067                 return 1;
1068 //printf("FileMPEG::set_video_position 1 %jd\n", x);
1069         mpeg3_set_frame(fd, pos, file->current_layer);
1070         return 0;
1071 }
1072
1073 int64_t FileMPEG::get_memory_usage()
1074 {
1075         int64_t result = file->rd && fd ? mpeg3_memory_usage(fd) : 0;
1076 //printf("FileMPEG::get_memory_usage %d  %jd\n", __LINE__, result);
1077         return result;
1078 }
1079
1080 int FileMPEG::set_program(int no)
1081 {
1082         return fd ? mpeg3_set_program(fd, no) : -1;
1083 }
1084
1085 int FileMPEG::get_cell_time(int no, double &time)
1086 {
1087         return fd ? mpeg3_get_cell_time(fd, no, &time) : -1;
1088 }
1089
1090 int FileMPEG::get_system_time(int64_t &tm)
1091 {
1092         return fd ? mpeg3_dvb_get_system_time(fd, &tm) : -1;
1093 }
1094
1095 int FileMPEG::get_video_pid(int track)
1096 {
1097         return fd ? mpeg3_video_pid(fd, track) : -1;
1098 }
1099
1100 int FileMPEG::get_video_info(int track, int &pid,
1101                 double &framerate, int &width, int &height, char *title)
1102 {
1103         if( !fd ) return -1;
1104         pid = mpeg3_video_pid(fd, track);
1105         framerate = mpeg3_frame_rate(fd, track);
1106         width = mpeg3_video_width(fd, track);
1107         height = mpeg3_video_height(fd, track);
1108         if( !title ) return 0;
1109         *title = 0;
1110
1111         int elements = mpeg3_dvb_channel_count(fd);
1112         for( int n=0; n<elements; ++n ) {
1113                 int major, minor, total_vstreams, vstream, vidx;
1114                 if( mpeg3_dvb_get_channel(fd,n, &major, &minor) ||
1115                     mpeg3_dvb_total_vstreams(fd,n,&total_vstreams) ) continue;
1116                 for( vidx=0; vidx<total_vstreams; ++vidx ) {
1117                         if( mpeg3_dvb_vstream_number(fd,n,vidx,&vstream) ) continue;
1118                         if( vstream < 0 ) continue;
1119                         if( vstream == track ) {
1120                                 sprintf(title, "%3d.%-3d", major, minor);
1121                                 return 0;
1122                         }
1123                 }
1124         }
1125         return 0;
1126 }
1127
1128 int FileMPEG::select_video_stream(Asset *asset, int vstream)
1129 {
1130         if( !fd ) return -1;
1131         asset->width = mpeg3_video_width(fd, vstream);
1132         asset->height = mpeg3_video_height(fd, vstream);
1133         asset->video_length = mpeg3_video_frames(fd, vstream);
1134         asset->frame_rate = mpeg3_frame_rate(fd, vstream);
1135         return 0;
1136 }
1137
1138 int FileMPEG::select_audio_stream(Asset *asset, int astream)
1139 {
1140         if( !fd ) return -1;
1141         asset->sample_rate = mpeg3_sample_rate(fd, astream);
1142         asset->audio_length = mpeg3_audio_samples(fd, astream);
1143         return 0;
1144 }
1145
1146 int FileMPEG::get_thumbnail(int stream,
1147         int64_t &position, unsigned char *&thumbnail, int &ww, int &hh)
1148 {
1149         return !fd ? -1 :
1150                 mpeg3_get_thumbnail(fd, stream, &position, &thumbnail, &ww, &hh);
1151 }
1152
1153 int FileMPEG::write_samples(double **buffer, int64_t len)
1154 {
1155         int result = 0;
1156
1157 //printf("FileMPEG::write_samples 1\n");
1158         if(asset->ampeg_derivative == 2) {
1159 // Convert to int16
1160                 int channels = MIN(asset->channels, 2);
1161                 int64_t audio_size = len * channels * 2;
1162                 if(twolame_allocation < audio_size) {
1163                         if(twolame_temp) delete [] twolame_temp;
1164                         twolame_temp = new unsigned char[audio_size];
1165                         twolame_allocation = audio_size;
1166                         if(twolame_out) delete [] twolame_out;
1167                         twolame_out = new unsigned char[audio_size + 1152];
1168                 }
1169
1170                 for(int i = 0; i < channels; i++) {
1171                         int16_t *output = ((int16_t*)twolame_temp) + i;
1172                         double *input = buffer[i];
1173                         for(int j = 0; j < len; j++) {
1174                                 int sample = (int)(*input * 0x7fff);
1175                                 *output = (int16_t)(CLIP(sample, -0x8000, 0x7fff));
1176                                 output += channels;
1177                                 input++;
1178                         }
1179                 }
1180                 int ret = twolame_encode_buffer_interleaved(twopts,
1181                                 (int16_t*)twolame_temp, len,
1182                                 twolame_out, twolame_allocation+1152);
1183                 if( ret > 0 )
1184                         fwrite(twolame_out, 1, ret, twofp);
1185                 else if( ret < 0 )
1186                         fprintf(stderr, _("twolame error encoding audio: %d\n"), ret);
1187         }
1188         else
1189         if(asset->ampeg_derivative == 3)
1190         {
1191                 int channels = MIN(asset->channels, 2);
1192                 int64_t audio_size = len * channels;
1193                 if(!lame_global) return 1;
1194                 if(!lame_fd) return 1;
1195                 if(lame_allocation < audio_size)
1196                 {
1197                         if(lame_temp[0]) delete [] lame_temp[0];
1198                         if(lame_temp[1]) delete [] lame_temp[1];
1199                         lame_temp[0] = new float[audio_size];
1200                         lame_temp[1] = new float[audio_size];
1201                         lame_allocation = audio_size;
1202                 }
1203
1204                 if(lame_output_allocation < audio_size * 4)
1205                 {
1206                         if(lame_output) delete [] lame_output;
1207                         lame_output_allocation = audio_size * 4;
1208                         lame_output = new char[lame_output_allocation];
1209                 }
1210
1211                 for(int i = 0; i < channels; i++)
1212                 {
1213                         float *output = lame_temp[i];
1214                         double *input = buffer[i];
1215                         for(int j = 0; j < len; j++)
1216                         {
1217                                 *output++ = *input++ * (float)32768;
1218                         }
1219                 }
1220
1221                 result = lame_encode_buffer_float(lame_global,
1222                         lame_temp[0],
1223                         (channels > 1) ? lame_temp[1] : lame_temp[0],
1224                         len,
1225                         (unsigned char*)lame_output,
1226                         lame_output_allocation);
1227                 if(result > 0)
1228                 {
1229                         char *real_output = lame_output;
1230                         int bytes = result;
1231                         if(!lame_started)
1232                         {
1233                                 for(int i = 0; i < bytes; i++)
1234                                         if(lame_output[i])
1235                                         {
1236                                                 real_output = &lame_output[i];
1237                                                 lame_started = 1;
1238                                                 bytes -= i;
1239                                                 break;
1240                                         }
1241                         }
1242                         if(bytes > 0 && lame_started)
1243                         {
1244                                 result = !fwrite(real_output, 1, bytes, lame_fd);
1245                                 if(result) {
1246                                         perror("FileMPEG::write_samples");
1247                                         eprintf(_("write failed: %m"));
1248                                 }
1249                         }
1250                         else
1251                                 result = 0;
1252                 }
1253                 else
1254                         result = 1;
1255         }
1256
1257         return result;
1258 }
1259
1260 int FileMPEG::write_frames(VFrame ***frames, int len)
1261 {
1262         int result = 0;
1263
1264         if(video_out)
1265         {
1266                 int temp_w = (int)((asset->width + 15) / 16) * 16;
1267                 int temp_h;
1268
1269                 int output_cmodel = asset->vmpeg_cmodel;
1270 // verify colormodel supported in MPEG output
1271                 switch( output_cmodel ) {
1272                 case BC_YUV420P:
1273                 case BC_YUV422P:
1274                         break;
1275                 default:
1276                         return 1;
1277                 }
1278                 
1279 // Height depends on progressiveness
1280                 if(asset->vmpeg_progressive || asset->vmpeg_derivative == 1)
1281                         temp_h = (int)((asset->height + 15) / 16) * 16;
1282                 else
1283                         temp_h = (int)((asset->height + 31) / 32) * 32;
1284
1285 //printf("FileMPEG::write_frames 1\n");
1286                 
1287 // Only 1 layer is supported in MPEG output
1288                 for(int i = 0; i < 1; i++)
1289                 {
1290                         for(int j = 0; j < len && !result; j++)
1291                         {
1292                                 VFrame *frame = frames[i][j];
1293                                 
1294                                 
1295                                 
1296                                 if(asset->vmpeg_cmodel == BC_YUV422P)
1297                                 {
1298                                         if(frame->get_w() == temp_w &&
1299                                                 frame->get_h() == temp_h &&
1300                                                 frame->get_color_model() == output_cmodel)
1301                                         {
1302                                                 mpeg2enc_set_input_buffers(0, 
1303                                                         (char*)frame->get_y(),
1304                                                         (char*)frame->get_u(),
1305                                                         (char*)frame->get_v());
1306                                         }
1307                                         else
1308                                         {
1309                                                 if(temp_frame &&
1310                                                         (temp_frame->get_w() != temp_w ||
1311                                                         temp_frame->get_h() != temp_h ||
1312                                                         temp_frame->get_color_model() || output_cmodel))
1313                                                 {
1314                                                         delete temp_frame;
1315                                                         temp_frame = 0;
1316                                                 }
1317
1318
1319                                                 if(!temp_frame)
1320                                                 {
1321                                                         temp_frame = new VFrame(0, 
1322                                                                 -1,
1323                                                                 temp_w, 
1324                                                                 temp_h, 
1325                                                                 output_cmodel,
1326                                                                 -1);
1327                                                 }
1328
1329                                                 BC_CModels::transfer(temp_frame->get_rows(), 
1330                                                         frame->get_rows(),
1331                                                         temp_frame->get_y(),
1332                                                         temp_frame->get_u(),
1333                                                         temp_frame->get_v(),
1334                                                         frame->get_y(),
1335                                                         frame->get_u(),
1336                                                         frame->get_v(),
1337                                                         0,
1338                                                         0,
1339                                                         frame->get_w(),
1340                                                         frame->get_h(),
1341                                                         0,
1342                                                         0,
1343                                                         temp_frame->get_w(),
1344                                                         temp_frame->get_h(),
1345                                                         frame->get_color_model(), 
1346                                                         temp_frame->get_color_model(),
1347                                                         0, 
1348                                                         frame->get_w(),
1349                                                         temp_frame->get_w());
1350
1351                                                 mpeg2enc_set_input_buffers(0, 
1352                                                         (char*)temp_frame->get_y(),
1353                                                         (char*)temp_frame->get_u(),
1354                                                         (char*)temp_frame->get_v());
1355                                         }
1356                                 }
1357                                 else
1358                                 {
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)
1362                                         {
1363                                                 mjpeg_y = frame->get_y();
1364                                                 mjpeg_u = frame->get_u();
1365                                                 mjpeg_v = frame->get_v();
1366                                         }
1367                                         else
1368                                         {
1369 //printf("FileMPEG::write_frames %d\n", __LINE__);sleep(1);
1370                                                 if(!temp_frame)
1371                                                 {
1372                                                         temp_frame = new VFrame(0, 
1373                                                                 -1,
1374                                                                 asset->width, 
1375                                                                 asset->height, 
1376                                                                 output_cmodel,
1377                                                                 -1);
1378                                                 }
1379
1380 // printf("FileMPEG::write_frames %d temp_frame=%p %p %p %p frame=%p %p %p %p color_model=%p %p\n", 
1381 // __LINE__,
1382 // temp_frame,
1383 // temp_frame->get_w(),
1384 // temp_frame->get_h(),
1385 // frame,
1386 // frame->get_w(),
1387 // frame->get_h(),
1388 // temp_frame->get_color_model(),
1389 // frame->get_color_model()); sleep(1);
1390                                                 BC_CModels::transfer(temp_frame->get_rows(), 
1391                                                         frame->get_rows(),
1392                                                         temp_frame->get_y(),
1393                                                         temp_frame->get_u(),
1394                                                         temp_frame->get_v(),
1395                                                         frame->get_y(),
1396                                                         frame->get_u(),
1397                                                         frame->get_v(),
1398                                                         0,
1399                                                         0,
1400                                                         frame->get_w(),
1401                                                         frame->get_h(),
1402                                                         0,
1403                                                         0,
1404                                                         temp_frame->get_w(),
1405                                                         temp_frame->get_h(),
1406                                                         frame->get_color_model(), 
1407                                                         temp_frame->get_color_model(),
1408                                                         0, 
1409                                                         frame->get_w(),
1410                                                         temp_frame->get_w());
1411 //printf("FileMPEG::write_frames %d\n", __LINE__);sleep(1);
1412
1413                                                 mjpeg_y = temp_frame->get_y();
1414                                                 mjpeg_u = temp_frame->get_u();
1415                                                 mjpeg_v = temp_frame->get_v();
1416                                         }
1417
1418
1419
1420
1421                                         next_frame_lock->unlock();
1422                                         next_frame_done->lock("FileMPEG::write_frames");
1423                                         if(mjpeg_error) result = 1;
1424                                 }
1425
1426
1427
1428
1429
1430                         }
1431                 }
1432         }
1433
1434
1435
1436         return result;
1437 }
1438
1439 int FileMPEG::zmpeg3_cmdl(int colormodel)
1440 {   
1441         switch( colormodel ) {
1442         case BC_BGR888:       return zmpeg3_t::cmdl_BGR888;
1443         case BC_BGR8888:      return zmpeg3_t::cmdl_BGRA8888;
1444         case BC_RGB565:       return zmpeg3_t::cmdl_RGB565;
1445         case BC_RGB888:       return zmpeg3_t::cmdl_RGB888;
1446         case BC_RGBA8888:     return zmpeg3_t::cmdl_RGBA8888;
1447         case BC_RGBA16161616: return zmpeg3_t::cmdl_RGBA16161616;
1448         case BC_YUV420P:      return zmpeg3_t::cmdl_YUV420P;
1449         case BC_YUV422P:      return zmpeg3_t::cmdl_YUV422P;
1450         case BC_YUV422:       return zmpeg3_t::cmdl_YUYV;
1451         case BC_YUV888:       return zmpeg3_t::cmdl_YUV888;
1452         case BC_YUVA8888:     return zmpeg3_t::cmdl_YUVA8888;
1453         }
1454         return -1;
1455 }   
1456
1457 int FileMPEG::bc_colormodel(int cmdl)
1458 {
1459         switch( cmdl ) {
1460         case zmpeg3_t::cmdl_BGR888:       return BC_BGR888;
1461         case zmpeg3_t::cmdl_BGRA8888:     return BC_BGR8888;
1462         case zmpeg3_t::cmdl_RGB565:       return BC_RGB565;
1463         case zmpeg3_t::cmdl_RGB888:       return BC_RGB888;
1464         case zmpeg3_t::cmdl_RGBA8888:     return BC_RGBA8888;
1465         case zmpeg3_t::cmdl_RGBA16161616: return BC_RGBA16161616;
1466         case zmpeg3_t::cmdl_YUV420P:      return BC_YUV420P;
1467         case zmpeg3_t::cmdl_YUV422P:      return BC_YUV422P;
1468         case zmpeg3_t::cmdl_YUYV:         return BC_YUV422;
1469         case zmpeg3_t::cmdl_YUV888:       return BC_YUV888;
1470         case zmpeg3_t::cmdl_YUVA8888:     return BC_YUVA8888;
1471         }
1472         return -1;
1473 }
1474
1475 const char *FileMPEG::zmpeg3_cmdl_name(int cmdl)
1476 {
1477 # define CMDL(nm) #nm
1478   static const char *cmdl_name[] = {
1479     CMDL(BGR888),
1480     CMDL(BGRA8888),
1481     CMDL(RGB565),
1482     CMDL(RGB888),
1483     CMDL(RGBA8888),
1484     CMDL(RGBA16161616),
1485     CMDL(UNKNOWN),
1486     CMDL(601_BGR888),
1487     CMDL(601_BGRA8888),
1488     CMDL(601_RGB888),
1489     CMDL(601_RGBA8888),
1490     CMDL(601_RGB565),
1491     CMDL(YUV420P),
1492     CMDL(YUV422P),
1493     CMDL(601_YUV420P),
1494     CMDL(601_YUV422P),
1495     CMDL(UYVY),
1496     CMDL(YUYV),
1497     CMDL(601_UYVY),
1498     CMDL(601_YUYV),
1499     CMDL(YUV888),
1500     CMDL(YUVA8888),
1501     CMDL(601_YUV888),
1502     CMDL(601_YUVA8888),
1503   };
1504   return cmdl>=0 && cmdl<lengthof(cmdl_name) ? cmdl_name[cmdl] : cmdl_name[6];
1505 }
1506
1507
1508 int FileMPEG::read_frame(VFrame *frame)
1509 {
1510         if(!fd) return 1;
1511         int result = 0;
1512         int width = mpeg3_video_width(fd,file->current_layer);
1513         int height = mpeg3_video_height(fd,file->current_layer);
1514         int stream_cmdl = mpeg3_colormodel(fd,file->current_layer);
1515         int stream_color_model = bc_colormodel(stream_cmdl);
1516         int frame_color_model = frame->get_color_model();
1517         int frame_cmdl = zmpeg3_cmdl(frame_color_model);
1518         mpeg3_show_subtitle(fd, file->current_layer, file->playback_subtitle);
1519
1520
1521         switch( frame_color_model ) { // check for direct copy
1522         case BC_YUV420P:
1523         case BC_YUV422P:
1524                 if( stream_color_model == frame_color_model &&
1525                         width == frame->get_w() && height == frame->get_h() ) {
1526                         mpeg3_read_yuvframe(fd,
1527                                 (char*)frame->get_y(),
1528                                 (char*)frame->get_u(),
1529                                 (char*)frame->get_v(),
1530                                 0, 0, width, height,
1531                                 file->current_layer);
1532                         return result;
1533                 }
1534         }
1535
1536         if( frame_cmdl >= 0 ) {        // supported by read_frame
1537                 // cant rely on frame->get_rows(), format may not support it
1538                 int uvs = 0;
1539                 switch( frame_color_model ) {
1540                 case BC_YUV420P: uvs = 2;  break;
1541                 case BC_YUV422P: uvs = 1;  break;
1542                 }
1543                 //int w = frame->get_w();
1544                 int h = frame->get_h();
1545                 int bpl = frame->get_bytes_per_line();
1546                 int uvw = !uvs ? 0 : bpl / 2;
1547                 int uvh = !uvs ? 0 : h / uvs;
1548                 uint8_t *rows[h + 2*uvh], *rp;
1549                 int n = 0;
1550                 if( (rp=frame->get_y()) ) {
1551                         for( int i=0; i<h; ++i, rp+=bpl ) rows[n++] = rp;
1552                         rp = frame->get_u();
1553                         for( int i=0; i<uvh; ++i, rp+=uvw ) rows[n++] = rp;
1554                         rp = frame->get_v();
1555                         for( int i=0; i<uvh; ++i, rp+=uvw ) rows[n++] = rp;
1556                 }
1557                 else {
1558                         rp = frame->get_data();  uvh *= 2;
1559                         for( int i=0; i<h; ++i, rp+=bpl ) rows[n++] = rp;
1560                         for( int i=0; i<uvh; ++i, rp+=uvw ) rows[n++] = rp;
1561                 }
1562
1563                 mpeg3_read_frame(fd, 
1564                                 rows,                /* start of each output row */
1565                                 0, 0, width, height, /* input box */
1566                                 frame->get_w(),      /* Dimensions of output_rows */
1567                                 frame->get_h(), 
1568                                 frame_cmdl,
1569                                 file->current_layer);
1570                 return result;
1571         }
1572
1573         char *y, *u, *v;
1574         mpeg3_read_yuvframe_ptr(fd, &y, &u, &v, file->current_layer);
1575         if( y && u && v ) {
1576                 BC_CModels::transfer(frame->get_rows(), 0,
1577                         frame->get_y(),
1578                         frame->get_u(),
1579                         frame->get_v(),
1580                         (unsigned char*)y,
1581                         (unsigned char*)u,
1582                         (unsigned char*)v,
1583                         0, 0, width, height,
1584                         0, 0, frame->get_w(), frame->get_h(),
1585                         stream_color_model, 
1586                         frame_color_model,
1587                         0, 
1588                         width,
1589                         frame->get_w());
1590         }
1591
1592         return result;
1593 }
1594
1595
1596 void FileMPEG::to_streamchannel(int channel, int &stream_out, int &channel_out)
1597 {
1598         int total_astreams = mpeg3_total_astreams(fd);
1599         for(stream_out = 0; stream_out < total_astreams; ++stream_out )
1600         {
1601                 int stream_channels = mpeg3_audio_channels(fd, stream_out);
1602                 if( channel < stream_channels ) break;
1603                 channel -= stream_channels;
1604         }
1605         channel_out = channel;
1606 }
1607
1608 int FileMPEG::read_samples(double *buffer, int64_t len)
1609 {
1610         if(!fd) return 0;
1611         if(len < 0) return 0;
1612
1613 // Translate pure channel to a stream and a channel in the mpeg stream
1614         int stream, channel;
1615         to_streamchannel(file->current_channel, stream, channel);
1616
1617 //printf("FileMPEG::read_samples 1 current_sample=%jd len=%jd channel=%d\n", file->current_sample, len, channel);
1618
1619         mpeg3_set_sample(fd, 
1620                 file->current_sample,
1621                 stream);
1622         mpeg3_read_audio_d(fd, 
1623                 buffer, /* Pointer to pre-allocated buffer of doubles */
1624                 channel,          /* Channel to decode */
1625                 len,         /* Number of samples to decode */
1626                 stream);          /* Stream containing the channel */
1627
1628 //      last_sample = file->current_sample;
1629         return 0;
1630 }
1631
1632
1633
1634
1635
1636
1637 FileMPEGVideo::FileMPEGVideo(FileMPEG *file)
1638  : Thread(1, 0, 0)
1639 {
1640         this->file = file;
1641
1642
1643         if(file->asset->vmpeg_cmodel == BC_YUV422P)
1644         {
1645                 mpeg2enc_init_buffers();
1646                 mpeg2enc_set_w(file->asset->width);
1647                 mpeg2enc_set_h(file->asset->height);
1648                 mpeg2enc_set_rate(file->asset->frame_rate);
1649         }
1650 }
1651
1652 FileMPEGVideo::~FileMPEGVideo()
1653 {
1654         Thread::join();
1655 }
1656
1657 void FileMPEGVideo::run()
1658 {
1659         if(file->asset->vmpeg_cmodel == BC_YUV422P)
1660         {
1661                 printf("FileMPEGVideo::run ");
1662                 for(int i = 0; i < file->vcommand_line.total; i++)
1663                 printf("%s ", file->vcommand_line.values[i]);
1664                 printf("\n");
1665                 mpeg2enc(file->vcommand_line.total, file->vcommand_line.values);
1666         }
1667         else
1668         {
1669                 while(1)
1670                 {
1671 //printf("FileMPEGVideo::run %d\n", __LINE__);
1672                         file->next_frame_lock->lock("FileMPEGVideo::run");
1673 //printf("FileMPEGVideo::run %d\n", __LINE__);
1674                         if(file->mjpeg_eof)
1675                         {
1676                                 file->next_frame_done->unlock();
1677                                 break;
1678                         }
1679
1680
1681
1682 //printf("FileMPEGVideo::run %d\n", __LINE__);
1683 // YUV4 sequence header
1684                         if(!file->wrote_header)
1685                         {
1686                                 file->wrote_header = 1;
1687
1688                                 char interlace_string[BCTEXTLEN];
1689                                 if(!file->asset->vmpeg_progressive)
1690                                 {
1691                                         sprintf(interlace_string, file->asset->vmpeg_field_order ? "b" : "t");
1692                                 }
1693                                 else
1694                                 {
1695                                         sprintf(interlace_string, "p");
1696                                 }
1697                                 double aspect_ratio = file->asset->aspect_ratio >= 0 ?
1698                                         file->asset->aspect_ratio : 1.0;
1699 //printf("FileMPEGVideo::run %d\n", __LINE__);
1700                                 fprintf(file->mjpeg_out, "YUV4MPEG2 W%d H%d F%d:%d I%s A%d:%d C%s\n",
1701                                         file->asset->width, file->asset->height,
1702                                         (int)(file->asset->frame_rate * 1001),
1703                                         1001, interlace_string,
1704                                         (int)(aspect_ratio * 1000), 1000,
1705                                         "420mpeg2");
1706 //printf("FileMPEGVideo::run %d\n", __LINE__);
1707                         }
1708
1709 // YUV4 frame header
1710 //printf("FileMPEGVideo::run %d\n", __LINE__);
1711                         fprintf(file->mjpeg_out, "FRAME\n");
1712
1713 // YUV data
1714 //printf("FileMPEGVideo::run %d\n", __LINE__);
1715                         if(!fwrite(file->mjpeg_y, file->asset->width * file->asset->height, 1, file->mjpeg_out))
1716                                 file->mjpeg_error = 1;
1717 //printf("FileMPEGVideo::run %d\n", __LINE__);
1718                         if(!fwrite(file->mjpeg_u, file->asset->width * file->asset->height / 4, 1, file->mjpeg_out))
1719                                 file->mjpeg_error = 1;
1720 //printf("FileMPEGVideo::run %d\n", __LINE__);
1721                         if(!fwrite(file->mjpeg_v, file->asset->width * file->asset->height / 4, 1, file->mjpeg_out))
1722                                 file->mjpeg_error = 1;
1723 //printf("FileMPEGVideo::run %d\n", __LINE__);
1724                         fflush(file->mjpeg_out);
1725
1726 //printf("FileMPEGVideo::run %d\n", __LINE__);
1727                         file->next_frame_done->unlock();
1728 //printf("FileMPEGVideo::run %d\n", __LINE__);
1729                 }
1730                 pclose(file->mjpeg_out);
1731                 file->mjpeg_out = 0;
1732         }
1733 }
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746 MPEGConfigAudio::MPEGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
1747  : BC_Window(_(PROGRAM_NAME ": Audio Compression"),
1748         parent_window->get_abs_cursor_x(1),
1749         parent_window->get_abs_cursor_y(1),
1750         310,
1751         120,
1752         -1,
1753         -1,
1754         0,
1755         0,
1756         1)
1757 {
1758         this->parent_window = parent_window;
1759         this->asset = asset;
1760 }
1761
1762 MPEGConfigAudio::~MPEGConfigAudio()
1763 {
1764 }
1765
1766 void MPEGConfigAudio::create_objects()
1767 {
1768         int x = 10, y = 10;
1769         int x1 = 150;
1770         MPEGLayer *layer;
1771
1772         lock_window("MPEGConfigAudio::create_objects");
1773         if(asset->format == FILE_MPEG)
1774         {
1775                 add_subwindow(new BC_Title(x, y, _("No options for MPEG transport stream.")));
1776                 unlock_window();
1777                 return;
1778         }
1779
1780
1781         add_tool(new BC_Title(x, y, _("Layer:")));
1782         add_tool(layer = new MPEGLayer(x1, y, this));
1783         layer->create_objects();
1784
1785         y += 30;
1786         add_tool(new BC_Title(x, y, _("Kbits per second:")));
1787         add_tool(bitrate = new MPEGABitrate(x1, y, this));
1788         bitrate->create_objects();
1789
1790
1791         add_subwindow(new BC_OKButton(this));
1792         show_window(1);
1793         unlock_window();
1794 }
1795
1796 int MPEGConfigAudio::close_event()
1797 {
1798         set_done(0);
1799         return 1;
1800 }
1801
1802
1803
1804
1805
1806
1807
1808 MPEGLayer::MPEGLayer(int x, int y, MPEGConfigAudio *gui)
1809  : BC_PopupMenu(x, y, 100, layer_to_string(gui->asset->ampeg_derivative))
1810 {
1811         this->gui = gui;
1812 }
1813
1814 void MPEGLayer::create_objects()
1815 {
1816         add_item(new BC_MenuItem(layer_to_string(2)));
1817         add_item(new BC_MenuItem(layer_to_string(3)));
1818 }
1819
1820 int MPEGLayer::handle_event()
1821 {
1822         gui->asset->ampeg_derivative = string_to_layer(get_text());
1823         gui->bitrate->set_layer(gui->asset->ampeg_derivative);
1824         return 1;
1825 };
1826
1827 int MPEGLayer::string_to_layer(char *string)
1828 {
1829         if(!strcasecmp(layer_to_string(2), string))
1830                 return 2;
1831         if(!strcasecmp(layer_to_string(3), string))
1832                 return 3;
1833
1834         return 2;
1835 }
1836
1837 char* MPEGLayer::layer_to_string(int layer)
1838 {
1839         switch(layer)
1840         {
1841                 case 2:
1842                         return _("II");
1843                         break;
1844
1845                 case 3:
1846                         return _("III");
1847                         break;
1848
1849                 default:
1850                         return _("II");
1851                         break;
1852         }
1853 }
1854
1855
1856
1857
1858
1859
1860
1861 MPEGABitrate::MPEGABitrate(int x, int y, MPEGConfigAudio *gui)
1862  : BC_PopupMenu(x,
1863         y,
1864         100,
1865         bitrate_to_string(gui->string, gui->asset->ampeg_bitrate))
1866 {
1867         this->gui = gui;
1868 }
1869
1870 void MPEGABitrate::create_objects()
1871 {
1872         set_layer(gui->asset->ampeg_derivative);
1873 }
1874
1875 void MPEGABitrate::set_layer(int layer)
1876 {
1877         while(total_items()) del_item(0);
1878
1879         if(layer == 2)
1880         {
1881                 add_item(new BC_MenuItem("160"));
1882                 add_item(new BC_MenuItem("192"));
1883                 add_item(new BC_MenuItem("224"));
1884                 add_item(new BC_MenuItem("256"));
1885                 add_item(new BC_MenuItem("320"));
1886                 add_item(new BC_MenuItem("384"));
1887         }
1888         else
1889         {
1890                 add_item(new BC_MenuItem("8"));
1891                 add_item(new BC_MenuItem("16"));
1892                 add_item(new BC_MenuItem("24"));
1893                 add_item(new BC_MenuItem("32"));
1894                 add_item(new BC_MenuItem("40"));
1895                 add_item(new BC_MenuItem("48"));
1896                 add_item(new BC_MenuItem("56"));
1897                 add_item(new BC_MenuItem("64"));
1898                 add_item(new BC_MenuItem("80"));
1899                 add_item(new BC_MenuItem("96"));
1900                 add_item(new BC_MenuItem("112"));
1901                 add_item(new BC_MenuItem("128"));
1902                 add_item(new BC_MenuItem("144"));
1903                 add_item(new BC_MenuItem("160"));
1904                 add_item(new BC_MenuItem("192"));
1905                 add_item(new BC_MenuItem("224"));
1906                 add_item(new BC_MenuItem("256"));
1907                 add_item(new BC_MenuItem("320"));
1908         }
1909 }
1910
1911 int MPEGABitrate::handle_event()
1912 {
1913         gui->asset->ampeg_bitrate = string_to_bitrate(get_text());
1914         return 1;
1915 };
1916
1917 int MPEGABitrate::string_to_bitrate(char *string)
1918 {
1919         return atol(string);
1920 }
1921
1922
1923 char* MPEGABitrate::bitrate_to_string(char *string, int bitrate)
1924 {
1925         sprintf(string, "%d", bitrate);
1926         return string;
1927 }
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937 MPEGConfigVideo::MPEGConfigVideo(BC_WindowBase *parent_window,
1938         Asset *asset)
1939  : BC_Window(_(PROGRAM_NAME ": Video Compression"),
1940         parent_window->get_abs_cursor_x(1),
1941         parent_window->get_abs_cursor_y(1),
1942         500,
1943         400,
1944         -1,
1945         -1,
1946         0,
1947         0,
1948         1)
1949 {
1950         this->parent_window = parent_window;
1951         this->asset = asset;
1952         reset_cmodel();
1953 }
1954
1955 MPEGConfigVideo::~MPEGConfigVideo()
1956 {
1957 }
1958
1959 void MPEGConfigVideo::create_objects()
1960 {
1961         int x = 10, y = 10;
1962         int x1 = x + 150;
1963         //int x2 = x + 300;
1964
1965         lock_window("MPEGConfigVideo::create_objects");
1966         if(asset->format == FILE_MPEG)
1967         {
1968                 add_subwindow(new BC_Title(x, y, _("No options for MPEG transport stream.")));
1969                 unlock_window();
1970                 return;
1971         }
1972
1973         add_subwindow(new BC_Title(x, y, _("Color model:")));
1974         add_subwindow(cmodel = new MPEGColorModel(x1, y, this));
1975         cmodel->create_objects();
1976         y += 30;
1977
1978         update_cmodel_objs();
1979
1980         add_subwindow(new BC_OKButton(this));
1981         show_window(1);
1982         unlock_window();
1983 }
1984
1985 int MPEGConfigVideo::close_event()
1986 {
1987         set_done(0);
1988         return 1;
1989 }
1990
1991
1992 void MPEGConfigVideo::delete_cmodel_objs()
1993 {
1994         delete preset;
1995         delete derivative;
1996         delete bitrate;
1997         delete fixed_bitrate;
1998         delete quant;
1999         delete fixed_quant;
2000         delete iframe_distance;
2001         delete pframe_distance;
2002         delete top_field_first;
2003         delete progressive;
2004         delete denoise;
2005         delete seq_codes;
2006         titles.remove_all_objects();
2007         reset_cmodel();
2008 }
2009
2010 void MPEGConfigVideo::reset_cmodel()
2011 {
2012         preset = 0;
2013         derivative = 0;
2014         bitrate = 0;
2015         fixed_bitrate = 0;
2016         quant = 0;
2017         fixed_quant = 0;
2018         iframe_distance = 0;
2019         pframe_distance = 0;
2020         top_field_first = 0;
2021         progressive = 0;
2022         denoise = 0;
2023         seq_codes = 0;
2024 }
2025
2026 void MPEGConfigVideo::update_cmodel_objs()
2027 {
2028         BC_Title *title;
2029         int x = 10;
2030         int y = 40;
2031         int x1 = x + 150;
2032         int x2 = x + 280;
2033
2034         delete_cmodel_objs();
2035
2036         if(asset->vmpeg_cmodel == BC_YUV420P)
2037         {
2038                 add_subwindow(title = new BC_Title(x, y + 5, _("Format Preset:")));
2039                 titles.append(title);
2040                 add_subwindow(preset = new MPEGPreset(x1, y, this));
2041                 preset->create_objects();
2042                 y += 30;
2043         }
2044
2045         add_subwindow(title = new BC_Title(x, y + 5, _("Derivative:")));
2046         titles.append(title);
2047         add_subwindow(derivative = new MPEGDerivative(x1, y, this));
2048         derivative->create_objects();
2049         y += 30;
2050
2051         add_subwindow(title = new BC_Title(x, y + 5, _("Bitrate:")));
2052         titles.append(title);
2053         add_subwindow(bitrate = new MPEGBitrate(x1, y, this));
2054         add_subwindow(fixed_bitrate = new MPEGFixedBitrate(x2, y, this));
2055         y += 30;
2056
2057         add_subwindow(title = new BC_Title(x, y, _("Quantization:")));
2058         titles.append(title);
2059         quant = new MPEGQuant(x1, y, this);
2060         quant->create_objects();
2061         add_subwindow(fixed_quant = new MPEGFixedQuant(x2, y, this));
2062         y += 30;
2063
2064         add_subwindow(title = new BC_Title(x, y, _("I frame distance:")));
2065         titles.append(title);
2066         iframe_distance = new MPEGIFrameDistance(x1, y, this);
2067         iframe_distance->create_objects();
2068         y += 30;
2069
2070         if(asset->vmpeg_cmodel == BC_YUV420P)
2071         {
2072                 add_subwindow(title = new BC_Title(x, y, _("P frame distance:")));
2073                 titles.append(title);
2074                 pframe_distance = new MPEGPFrameDistance(x1, y, this);
2075                 pframe_distance->create_objects();
2076                 y += 30;
2077
2078                 add_subwindow(top_field_first = new BC_CheckBox(x, y, &asset->vmpeg_field_order, _("Bottom field first")));
2079                 y += 30;
2080         }
2081
2082         add_subwindow(progressive = new BC_CheckBox(x, y, &asset->vmpeg_progressive, _("Progressive frames")));
2083         y += 30;
2084         add_subwindow(denoise = new BC_CheckBox(x, y, &asset->vmpeg_denoise, _("Denoise")));
2085         y += 30;
2086         add_subwindow(seq_codes = new BC_CheckBox(x, y, &asset->vmpeg_seq_codes, _("Sequence start codes in every GOP")));
2087
2088 }
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102 MPEGDerivative::MPEGDerivative(int x, int y, MPEGConfigVideo *gui)
2103  : BC_PopupMenu(x, y, 150, derivative_to_string(gui->asset->vmpeg_derivative))
2104 {
2105         this->gui = gui;
2106 }
2107
2108 void MPEGDerivative::create_objects()
2109 {
2110         add_item(new BC_MenuItem(derivative_to_string(1)));
2111         add_item(new BC_MenuItem(derivative_to_string(2)));
2112 }
2113
2114 int MPEGDerivative::handle_event()
2115 {
2116         gui->asset->vmpeg_derivative = string_to_derivative(get_text());
2117         return 1;
2118 };
2119
2120 int MPEGDerivative::string_to_derivative(char *string)
2121 {
2122         if(!strcasecmp(derivative_to_string(1), string))
2123                 return 1;
2124         if(!strcasecmp(derivative_to_string(2), string))
2125                 return 2;
2126
2127         return 1;
2128 }
2129
2130 char* MPEGDerivative::derivative_to_string(int derivative)
2131 {
2132         switch(derivative)
2133         {
2134                 case 1:
2135                         return _("MPEG-1");
2136                         break;
2137
2138                 case 2:
2139                         return _("MPEG-2");
2140                         break;
2141
2142                 default:
2143                         return _("MPEG-1");
2144                         break;
2145         }
2146 }
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158 MPEGPreset::MPEGPreset(int x, int y, MPEGConfigVideo *gui)
2159  : BC_PopupMenu(x, y, 200, value_to_string(gui->asset->vmpeg_preset))
2160 {
2161         this->gui = gui;
2162 }
2163
2164 void MPEGPreset::create_objects()
2165 {
2166         for(int i = 0; i < 10; i++)
2167         {
2168                 add_item(new BC_MenuItem(value_to_string(i)));
2169         }
2170 }
2171
2172 int MPEGPreset::handle_event()
2173 {
2174         gui->asset->vmpeg_preset = string_to_value(get_text());
2175         return 1;
2176 }
2177
2178 int MPEGPreset::string_to_value(char *string)
2179 {
2180         for(int i = 0; i < 10; i++)
2181         {
2182                 if(!strcasecmp(value_to_string(i), string))
2183                         return i;
2184         }
2185         return 0;
2186 }
2187
2188 char* MPEGPreset::value_to_string(int derivative)
2189 {
2190         switch(derivative)
2191         {
2192                 case 0: return _("Generic MPEG-1"); break;
2193                 case 1: return _("standard VCD"); break;
2194                 case 2: return _("user VCD"); break;
2195                 case 3: return _("Generic MPEG-2"); break;
2196                 case 4: return _("standard SVCD"); break;
2197                 case 5: return _("user SVCD"); break;
2198                 case 6: return _("VCD Still sequence"); break;
2199                 case 7: return _("SVCD Still sequence"); break;
2200                 case 8: return _("DVD NAV"); break;
2201                 case 9: return _("DVD"); break;
2202                 default: return _("Generic MPEG-1"); break;
2203         }
2204 }
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216 MPEGBitrate::MPEGBitrate(int x, int y, MPEGConfigVideo *gui)
2217  : BC_TextBox(x, y, 100, 1, gui->asset->vmpeg_bitrate)
2218 {
2219         this->gui = gui;
2220 }
2221
2222
2223 int MPEGBitrate::handle_event()
2224 {
2225         gui->asset->vmpeg_bitrate = atol(get_text());
2226         return 1;
2227 };
2228
2229
2230
2231
2232
2233 MPEGQuant::MPEGQuant(int x, int y, MPEGConfigVideo *gui)
2234  : BC_TumbleTextBox(gui,
2235         (int64_t)gui->asset->vmpeg_quantization,
2236         (int64_t)1,
2237         (int64_t)100,
2238         x,
2239         y,
2240         100)
2241 {
2242         this->gui = gui;
2243 }
2244
2245 int MPEGQuant::handle_event()
2246 {
2247         gui->asset->vmpeg_quantization = atol(get_text());
2248         return 1;
2249 };
2250
2251 MPEGFixedBitrate::MPEGFixedBitrate(int x, int y, MPEGConfigVideo *gui)
2252  : BC_Radial(x, y, gui->asset->vmpeg_fix_bitrate, _("Fixed bitrate"))
2253 {
2254         this->gui = gui;
2255 }
2256
2257 int MPEGFixedBitrate::handle_event()
2258 {
2259         update(1);
2260         gui->asset->vmpeg_fix_bitrate = 1;
2261         gui->fixed_quant->update(0);
2262         return 1;
2263 };
2264
2265 MPEGFixedQuant::MPEGFixedQuant(int x, int y, MPEGConfigVideo *gui)
2266  : BC_Radial(x, y, !gui->asset->vmpeg_fix_bitrate, _("Fixed quantization"))
2267 {
2268         this->gui = gui;
2269 }
2270
2271 int MPEGFixedQuant::handle_event()
2272 {
2273         update(1);
2274         gui->asset->vmpeg_fix_bitrate = 0;
2275         gui->fixed_bitrate->update(0);
2276         return 1;
2277 };
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287 MPEGIFrameDistance::MPEGIFrameDistance(int x, int y, MPEGConfigVideo *gui)
2288  : BC_TumbleTextBox(gui,
2289         (int64_t)gui->asset->vmpeg_iframe_distance,
2290         (int64_t)1,
2291         (int64_t)100,
2292         x,
2293         y,
2294         50)
2295 {
2296         this->gui = gui;
2297 }
2298
2299 int MPEGIFrameDistance::handle_event()
2300 {
2301         gui->asset->vmpeg_iframe_distance = atoi(get_text());
2302         return 1;
2303 }
2304
2305
2306
2307
2308
2309
2310
2311 MPEGPFrameDistance::MPEGPFrameDistance(int x, int y, MPEGConfigVideo *gui)
2312  : BC_TumbleTextBox(gui,
2313         (int64_t)gui->asset->vmpeg_pframe_distance,
2314         (int64_t)0,
2315         (int64_t)2,
2316         x,
2317         y,
2318         50)
2319 {
2320         this->gui = gui;
2321 }
2322
2323 int MPEGPFrameDistance::handle_event()
2324 {
2325         gui->asset->vmpeg_pframe_distance = atoi(get_text());
2326         return 1;
2327 }
2328
2329
2330
2331
2332
2333
2334
2335
2336 MPEGColorModel::MPEGColorModel(int x, int y, MPEGConfigVideo *gui)
2337  : BC_PopupMenu(x, y, 150, cmodel_to_string(gui->asset->vmpeg_cmodel))
2338 {
2339         this->gui = gui;
2340 }
2341
2342 void MPEGColorModel::create_objects()
2343 {
2344         add_item(new BC_MenuItem(cmodel_to_string(BC_YUV420P)));
2345         add_item(new BC_MenuItem(cmodel_to_string(BC_YUV422P)));
2346 }
2347
2348 int MPEGColorModel::handle_event()
2349 {
2350         gui->asset->vmpeg_cmodel = string_to_cmodel(get_text());
2351         gui->update_cmodel_objs();
2352         gui->show_window(1);
2353         return 1;
2354 };
2355
2356 int MPEGColorModel::string_to_cmodel(char *string)
2357 {
2358         if(!strcasecmp(cmodel_to_string(BC_YUV420P), string))
2359                 return BC_YUV420P;
2360         if(!strcasecmp(cmodel_to_string(BC_YUV422P), string))
2361                 return BC_YUV422P;
2362         return BC_YUV420P;
2363 }
2364
2365 char* MPEGColorModel::cmodel_to_string(int cmodel)
2366 {
2367         switch(cmodel)
2368         {
2369                 case BC_YUV420P: return _("YUV 4:2:0");
2370                 case BC_YUV422P: return _("YUV 4:2:2");
2371                 default: return _("YUV 4:2:0");
2372         }
2373 }
2374
2375
2376
2377
2378
2379