repair default keyframe load, tweak init default histogram threshold
[goodguy/history.git] / cinelerra-5.1 / cinelerra / filempeg.C
index 59c4db2836841bbe74adf012faa04c43ba39d527..6c889b92c6cb7a169d3fcde23571454939682c87 100644 (file)
@@ -2,21 +2,21 @@
 /*
  * CINELERRA
  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
- * 
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * 
+ *
  */
 
 #include "asset.h"
@@ -55,7 +55,7 @@
 
 
 // M JPEG dependancies
-static double frame_rate_codes[] = 
+static double frame_rate_codes[] =
 {
        0,
        24000.0/1001.0,
@@ -104,11 +104,9 @@ FileMPEG::~FileMPEG()
        vcommand_line.remove_all_objects();
 }
 
-void FileMPEG::get_parameters(BC_WindowBase *parent_window, 
-       Asset *asset, 
-       BC_WindowBase* &format_window,
-       int audio_options,
-       int video_options)
+void FileMPEG::get_parameters(BC_WindowBase *parent_window,
+       Asset *asset, BC_WindowBase* &format_window,
+       int audio_options, int video_options, EDL *edl)
 {
        if(audio_options && asset->format == FILE_AMPEG)
        {
@@ -477,7 +475,7 @@ int FileMPEG::open_file(int rd, int wr)
                        eprintf(_("Couldn't open %s: failed.\n"), asset->path);
                }
        }
-       
+
        if( !result && wr && asset->format == FILE_VMPEG ) {
 // Heroine Virtual encoder
 //  this one is cinelerra-x.x.x/mpeg2enc
@@ -495,7 +493,8 @@ int FileMPEG::open_file(int rd, int wr)
                        if(!result)
                        {
                                const char *exec_path = File::get_cinlib_path();
-                               sprintf(mjpeg_command, "%s/%s", exec_path, HVPEG_EXE);
+                               snprintf(mjpeg_command, sizeof(mjpeg_command),
+                                       "%s/%s", exec_path, HVPEG_EXE);
                                append_vcommand_line(mjpeg_command);
 
                                if(asset->aspect_ratio > 0)
@@ -545,7 +544,8 @@ int FileMPEG::open_file(int rd, int wr)
 //  this one is cinelerra-x.x.x/thirdparty/mjpegtools/mpeg2enc
                {
                        const char *exec_path = File::get_cinlib_path();
-                       sprintf(mjpeg_command, "%s/%s -v 0 ", exec_path, MJPEG_EXE);
+                       snprintf(mjpeg_command, sizeof(mjpeg_command),
+                               "%s/%s -v 0 ", exec_path, MJPEG_EXE);
 
 // Must disable interlacing if MPEG-1
                        switch (asset->vmpeg_preset)
@@ -560,18 +560,15 @@ int FileMPEG::open_file(int rd, int wr)
 // quantization of 1 when bitrate is fixed.  Perfectly intuitive.
                        if(asset->vmpeg_fix_bitrate)
                        {
-                               sprintf(string, " -b %d -q 1", asset->vmpeg_bitrate / 1000);
+                               snprintf(string, sizeof(string),
+                                       " -b %d -q 1", asset->vmpeg_bitrate / 1000);
                        }
                        else
                        {
-                               sprintf(string, " -b 0 -q %d", asset->vmpeg_quantization);
+                               snprintf(string, sizeof(string),
+                                       " -b 0 -q %d", asset->vmpeg_quantization);
                        }
-                       strcat(mjpeg_command, string);
-
-
-
-
-
+                       strncat(mjpeg_command, string, sizeof(mjpeg_command));
 
 // Aspect ratio
                        int aspect_ratio_code = -1;
@@ -592,14 +589,14 @@ int FileMPEG::open_file(int rd, int wr)
 // Square pixels
                        if(EQUIV((double)asset->width / asset->height, asset->aspect_ratio))
                                aspect_ratio_code = 1;
-                       
+
                        if(aspect_ratio_code < 0)
                        {
                                eprintf(_("Unsupported aspect ratio %f\n"), asset->aspect_ratio);
                                aspect_ratio_code = 2;
                        }
                        sprintf(string, " -a %d", aspect_ratio_code);
-                       strcat(mjpeg_command, string);
+                       strncat(mjpeg_command, string, sizeof(mjpeg_command));
 
 
 
@@ -623,43 +620,49 @@ int FileMPEG::open_file(int rd, int wr)
                                eprintf(_("Unsupported frame rate %f\n"), asset->frame_rate);
                        }
                        sprintf(string, " -F %d", frame_rate_code);
-                       strcat(mjpeg_command, string);
+                       strncat(mjpeg_command, string, sizeof(mjpeg_command));
+
 
 
 
 
+                       strncat(mjpeg_command,
+                               asset->vmpeg_progressive ? " -I 0" : " -I 1",
+                               sizeof(mjpeg_command));
 
-                       strcat(mjpeg_command, 
-                               asset->vmpeg_progressive ? " -I 0" : " -I 1");
-                       
 
 
                        sprintf(string, " -M %d", file->cpus);
-                       strcat(mjpeg_command, string);
+                       strncat(mjpeg_command, string, sizeof(mjpeg_command));
 
 
                        if(!asset->vmpeg_progressive)
                        {
-                               strcat(mjpeg_command, asset->vmpeg_field_order ? " -z b" : " -z t");
+                               strncat(mjpeg_command,
+                                       asset->vmpeg_field_order ? " -z b" : " -z t",
+                                       sizeof(mjpeg_command));
                        }
 
 
-                       sprintf(string, " -f %d", asset->vmpeg_preset);
-                       strcat(mjpeg_command, string);
+                       snprintf(string, sizeof(string), " -f %d", asset->vmpeg_preset);
+                       strncat(mjpeg_command, string, sizeof(mjpeg_command));
 
 
-                       sprintf(string, " -g %d -G %d", asset->vmpeg_iframe_distance, asset->vmpeg_iframe_distance);
-                       strcat(mjpeg_command, string);
+                       snprintf(string, sizeof(string),
+                               " -g %d -G %d", asset->vmpeg_iframe_distance, asset->vmpeg_iframe_distance);
+                       strncat(mjpeg_command, string, sizeof(mjpeg_command));
 
 
-                       if(asset->vmpeg_seq_codes) strcat(mjpeg_command, " -s");
+                       if(asset->vmpeg_seq_codes)
+                               strncat(mjpeg_command, " -s", sizeof(mjpeg_command));
 
 
-                       sprintf(string, " -R %d", CLAMP(asset->vmpeg_pframe_distance, 0, 2));
-                       strcat(mjpeg_command, string);
+                       snprintf(string, sizeof(string),
+                               " -R %d", CLAMP(asset->vmpeg_pframe_distance, 0, 2));
+                       strncat(mjpeg_command, string, sizeof(mjpeg_command));
 
-                       sprintf(string, " -o '%s'", asset->path);
-                       strcat(mjpeg_command, string);
+                       snprintf(string, sizeof(string), " -o '%s'", asset->path);
+                       strncat(mjpeg_command, string, sizeof(mjpeg_command));
 
 
 
@@ -699,7 +702,7 @@ int FileMPEG::open_file(int rd, int wr)
 //                     lame_set_brate(lame_global, asset->ampeg_bitrate / 1000);
                        lame_set_brate(lame_global, asset->ampeg_bitrate);
                        lame_set_quality(lame_global, 0);
-                       lame_set_in_samplerate(lame_global, 
+                       lame_set_in_samplerate(lame_global,
                                asset->sample_rate);
                        lame_set_num_channels(lame_global,
                                asset->channels);
@@ -771,6 +774,7 @@ int FileMPEG::skim_video(int track, void *vp, skim_fn fn)
 
 
 
+#ifdef HAVE_COMMERCIAL
 int FileMPEG::toc_nail(void *vp, int track)
 {
        File *file = (File *)vp;
@@ -781,18 +785,19 @@ int FileMPEG::toc_nail(void *vp, int track)
        if( mpeg->get_video_info(track, pid, framerate, width, height) ) return 1;
        if( pid < 0 || framerate <= 0 ) return 1;
        double position = framenum / framerate;
-//printf("t%d/%03x f"_LD", %dx%d %dx%d\n",track,pid,framenum,mw,mh,width,height);
+//printf("t%d/%03x f%jd, %dx%d %dx%d\n",track,pid,framenum,mw,mh,width,height);
        MWindow::commercials->get_frame(file, pid, position, tdat, mw, mh, width, height);
        return 0;
 }
-
+#endif
 
 int FileMPEG::create_toc(char *toc_path)
 {
 // delete any existing toc files
        char toc_file[BCTEXTLEN];
        strcpy(toc_file, toc_path);
-       remove(toc_file);
+       if( strcmp(toc_file, asset->path) )
+               remove(toc_file);
        char *bp = strrchr(toc_file, '/');
        if( !bp ) bp = toc_file;
        char *sfx = strrchr(bp,'.');
@@ -812,11 +817,13 @@ int FileMPEG::create_toc(char *toc_path)
 // File needs a table of contents.
        struct timeval new_time, prev_time, start_time, current_time;
        gettimeofday(&prev_time, 0);  gettimeofday(&start_time, 0);
+#ifdef HAVE_COMMERCIAL
        if( file->preferences->scan_commercials ) {
                set_skimming(-1, 1, toc_nail, file);
                if( MWindow::commercials->resetDb() != 0 )
                        eprintf(_("cant access commercials database"));
        }
+#endif
 // This gets around the fact that MWindowGUI may be locked.
        char progress_title[BCTEXTLEN];
        sprintf(progress_title, _("Creating %s\n"), toc_file);
@@ -847,7 +854,7 @@ int FileMPEG::create_toc(char *toc_path)
                        sprintf(string, "%sETA: %jdm%jds",
                                progress_title, eta / 60, eta % 60);
                        progress.update_title(string, 1);
-//                     fprintf(stderr, "ETA: %dm%ds        \r", 
+//                     fprintf(stderr, "ETA: %dm%ds        \r",
 //                             bytes_processed * 100 / total_bytes,
 //                             eta / 60, eta % 60);
 //                     fflush(stdout);
@@ -860,10 +867,12 @@ int FileMPEG::create_toc(char *toc_path)
                }
        }
 
+#ifdef HAVE_COMMERCIAL
        if( file->preferences->scan_commercials ) {
                if( !result ) MWindow::commercials->write_ads(asset->path);
                MWindow::commercials->closeDb();
        }
+#endif
 
        mpeg3_stop_toc(fd);
        fd = 0;
@@ -952,7 +961,7 @@ int FileMPEG::close_file()
        vcommand_line.remove_all_objects();
 
        if(twofp) {
-               unsigned char opkt[1152*2]; 
+               unsigned char opkt[1152*2];
                int ret = twolame_encode_flush(twopts, opkt, sizeof(opkt));
                if( ret > 0 )
                        fwrite(opkt, 1, ret, twofp);
@@ -992,7 +1001,9 @@ int FileMPEG::get_best_colormodel(Asset *asset, int driver)
        switch(driver)
        {
                case PLAYBACK_X11:
-                       return BC_RGB888;
+//                     return BC_RGB888;
+// the direct X11 color model requires scaling in the codec
+                       return BC_BGR8888;
                case PLAYBACK_X11_XV:
                case PLAYBACK_ASYNCHRONOUS:
                        return zmpeg3_cmdl(asset->vmpeg_cmodel) > 0 ?
@@ -1036,7 +1047,7 @@ int FileMPEG::set_audio_position(int64_t sample)
 {
 #if 0
        if(!fd) return 1;
-       
+
        int channel, stream;
        to_streamchannel(file->current_channel, stream, channel);
 
@@ -1274,7 +1285,7 @@ int FileMPEG::write_frames(VFrame ***frames, int len)
                default:
                        return 1;
                }
-               
+
 // Height depends on progressiveness
                if(asset->vmpeg_progressive || asset->vmpeg_derivative == 1)
                        temp_h = (int)((asset->height + 15) / 16) * 16;
@@ -1282,23 +1293,23 @@ int FileMPEG::write_frames(VFrame ***frames, int len)
                        temp_h = (int)((asset->height + 31) / 32) * 32;
 
 //printf("FileMPEG::write_frames 1\n");
-               
+
 // Only 1 layer is supported in MPEG output
                for(int i = 0; i < 1; i++)
                {
                        for(int j = 0; j < len && !result; j++)
                        {
                                VFrame *frame = frames[i][j];
-                               
-                               
-                               
+
+
+
                                if(asset->vmpeg_cmodel == BC_YUV422P)
                                {
                                        if(frame->get_w() == temp_w &&
                                                frame->get_h() == temp_h &&
                                                frame->get_color_model() == output_cmodel)
                                        {
-                                               mpeg2enc_set_input_buffers(0, 
+                                               mpeg2enc_set_input_buffers(0,
                                                        (char*)frame->get_y(),
                                                        (char*)frame->get_u(),
                                                        (char*)frame->get_v());
@@ -1317,15 +1328,11 @@ int FileMPEG::write_frames(VFrame ***frames, int len)
 
                                                if(!temp_frame)
                                                {
-                                                       temp_frame = new VFrame(0, 
-                                                               -1,
-                                                               temp_w, 
-                                                               temp_h, 
-                                                               output_cmodel,
-                                                               -1);
+                                                       temp_frame = new VFrame(temp_w, temp_h,
+                                                                       output_cmodel, 0);
                                                }
 
-                                               BC_CModels::transfer(temp_frame->get_rows(), 
+                                               BC_CModels::transfer(temp_frame->get_rows(),
                                                        frame->get_rows(),
                                                        temp_frame->get_y(),
                                                        temp_frame->get_u(),
@@ -1341,13 +1348,13 @@ int FileMPEG::write_frames(VFrame ***frames, int len)
                                                        0,
                                                        temp_frame->get_w(),
                                                        temp_frame->get_h(),
-                                                       frame->get_color_model(), 
+                                                       frame->get_color_model(),
                                                        temp_frame->get_color_model(),
-                                                       0, 
+                                                       0,
                                                        frame->get_w(),
                                                        temp_frame->get_w());
 
-                                               mpeg2enc_set_input_buffers(0, 
+                                               mpeg2enc_set_input_buffers(0,
                                                        (char*)temp_frame->get_y(),
                                                        (char*)temp_frame->get_u(),
                                                        (char*)temp_frame->get_v());
@@ -1368,15 +1375,11 @@ int FileMPEG::write_frames(VFrame ***frames, int len)
 //printf("FileMPEG::write_frames %d\n", __LINE__);sleep(1);
                                                if(!temp_frame)
                                                {
-                                                       temp_frame = new VFrame(0, 
-                                                               -1,
-                                                               asset->width, 
-                                                               asset->height, 
-                                                               output_cmodel,
-                                                               -1);
+                                                       temp_frame = new VFrame(asset->width, asset->height,
+                                                                               output_cmodel, 0);
                                                }
 
-// printf("FileMPEG::write_frames %d temp_frame=%p %p %p %p frame=%p %p %p %p color_model=%p %p\n", 
+// printf("FileMPEG::write_frames %d temp_frame=%p %p %p %p frame=%p %p %p %p color_model=%p %p\n",
 // __LINE__,
 // temp_frame,
 // temp_frame->get_w(),
@@ -1416,7 +1419,7 @@ int FileMPEG::write_frames(VFrame ***frames, int len)
 }
 
 int FileMPEG::zmpeg3_cmdl(int colormodel)
-{   
+{
        switch( colormodel ) {
        case BC_BGR888:       return zmpeg3_t::cmdl_BGR888;
        case BC_BGR8888:      return zmpeg3_t::cmdl_BGRA8888;
@@ -1431,7 +1434,7 @@ int FileMPEG::zmpeg3_cmdl(int colormodel)
        case BC_YUVA8888:     return zmpeg3_t::cmdl_YUVA8888;
        }
        return -1;
-}   
+}
 
 int FileMPEG::bc_colormodel(int cmdl)
 {
@@ -1540,11 +1543,11 @@ int FileMPEG::read_frame(VFrame *frame)
                        for( int i=0; i<uvh; ++i, rp+=uvw ) rows[n++] = rp;
                }
 
-               mpeg3_read_frame(fd, 
+               mpeg3_read_frame(fd,
                                rows,                /* start of each output row */
                                0, 0, width, height, /* input box */
                                frame->get_w(),      /* Dimensions of output_rows */
-                               frame->get_h(), 
+                               frame->get_h(),
                                frame_cmdl,
                                file->current_layer);
                return result;
@@ -1592,10 +1595,10 @@ int FileMPEG::read_samples(double *buffer, int64_t len)
 
 //printf("FileMPEG::read_samples 1 current_sample=%jd len=%jd channel=%d\n", file->current_sample, len, channel);
 
-       mpeg3_set_sample(fd, 
+       mpeg3_set_sample(fd,
                file->current_sample,
                stream);
-       mpeg3_read_audio_d(fd, 
+       mpeg3_read_audio_d(fd,
                buffer, /* Pointer to pre-allocated buffer of doubles */
                channel,          /* Channel to decode */
                len,         /* Number of samples to decode */
@@ -1723,13 +1726,7 @@ MPEGConfigAudio::MPEGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
  : BC_Window(_(PROGRAM_NAME ": Audio Compression"),
        parent_window->get_abs_cursor_x(1),
        parent_window->get_abs_cursor_y(1),
-       310,
-       120,
-       -1,
-       -1,
-       0,
-       0,
-       1)
+       310, 120, -1, -1, 0, 0, 1)
 {
        this->parent_window = parent_window;
        this->asset = asset;
@@ -1776,11 +1773,6 @@ int MPEGConfigAudio::close_event()
 }
 
 
-
-
-
-
-
 MPEGLayer::MPEGLayer(int x, int y, MPEGConfigAudio *gui)
  : BC_PopupMenu(x, y, 100, layer_to_string(gui->asset->ampeg_derivative))
 {
@@ -1835,9 +1827,7 @@ char* MPEGLayer::layer_to_string(int layer)
 
 
 MPEGABitrate::MPEGABitrate(int x, int y, MPEGConfigAudio *gui)
- : BC_PopupMenu(x,
-       y,
-       100,
+ : BC_PopupMenu(x, y, 100,
        bitrate_to_string(gui->string, gui->asset->ampeg_bitrate))
 {
        this->gui = gui;
@@ -1915,13 +1905,7 @@ MPEGConfigVideo::MPEGConfigVideo(BC_WindowBase *parent_window,
  : BC_Window(_(PROGRAM_NAME ": Video Compression"),
        parent_window->get_abs_cursor_x(1),
        parent_window->get_abs_cursor_y(1),
-       500,
-       400,
-       -1,
-       -1,
-       0,
-       0,
-       1)
+       500, 400, -1, -1, 0, 0, 1)
 {
        this->parent_window = parent_window;
        this->asset = asset;