gcut.c 45.7 KB
Newer Older
1
#include "config.h"
2 3
#include <string.h>
#include <signal.h>
4
#include <math.h>
5 6 7 8
#include <unistd.h>
#include <ctype.h>
#include <stdio.h>
#include <gegl.h>
9
#include <libgen.h>
10 11
#include <gegl-audio-fragment.h>

12 13 14 15
#if HAVE_GEXIV2
#include <gexiv2/gexiv2.h>
#endif

Edward E.'s avatar
Edward E. committed
16 17 18 19 20
#ifdef G_OS_WIN32
#define realpath(a,b) _fullpath(b,a,_MAX_PATH)
#define SIGUSR2 (-1) /* quick and dirty, FIXME */
#endif

21 22 23 24 25 26 27 28
/* GEGL edit decision list - a digital video cutter and splicer */

/* take a string and expand {t=v i t=v t=v }  to numeric or string
   value. Having it that way.. makes it hard to keep parts of graph,.
   unless graph is kept when constructing... and values are filled in
   if topology of graphs match..
 */

29
#include "gcut.h"
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#define DEFAULT_output_path     "output.mp4"
#define DEFAULT_video_codec     "auto"
#define DEFAULT_audio_codec     "auto"
#define DEFAULT_video_width      0
#define DEFAULT_video_height     0
#define DEFAULT_proxy_width      0
#define DEFAULT_proxy_height     0
#define DEFAULT_video_bufsize    0
#define DEFAULT_video_bitrate    256
#define DEFAULT_video_tolerance  -1
#define DEFAULT_audio_bitrate    64
#define DEFAULT_audio_samplerate 64
#define DEFAULT_frame_start      0
#define DEFAULT_frame_end        0
#define DEFAULT_selection_start  0
#define DEFAULT_selection_end    0
#define DEFAULT_range_start      0
#define DEFAULT_range_end        0
#define DEFAULT_framedrop        0
50

51
char *gcut_binary_path = NULL;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

const char *default_edl =
#include "default.edl.inc"
;

static char *escaped_base_path (GeglEDL *edl, const char *clip_path)
{
  char *path0= g_strdup (clip_path);
  char *path = path0;
  int i;
  char *ret = 0;
  if (!strncmp (path, edl->parent_path, strlen(edl->parent_path)))
      path += strlen (edl->parent_path);
  for (i = 0; path[i];i ++)
  {
    switch (path[i]){
      case '/':
      case ' ':
      case '\'':
      case '#':
      case '%':
      case '?':
      case '*':
        path[i]='_';
        break;
    }
  }
  ret = g_strdup (path);
  g_free (path0);
  return ret;
}

84
char *gcut_make_thumb_path (GeglEDL *edl, const char *clip_path)
85 86 87
{
  gchar *ret;
  gchar *path = escaped_base_path (edl, clip_path);
88
  ret = g_strdup_printf ("%s.gcut/thumb/%s.png", edl->parent_path, path); // XXX: should escape relative/absolute path instead of basename - or add bit of its hash
89 90 91 92
  g_free (path);
  return ret;
}

93
char *gcut_make_proxy_path (GeglEDL *edl, const char *clip_path)
94 95 96
{
  gchar *ret;
  gchar *path = escaped_base_path (edl, clip_path);
97
  ret = g_strdup_printf ("%s.gcut/proxy/%s-%ix%i.mp4", edl->parent_path, path, edl->proxy_width, edl->proxy_height);
98 99 100 101 102 103 104
  g_free (path);
  return ret;
}


#include <stdlib.h>

105
GeglEDL *gcut_new           (void)
106 107 108 109 110 111 112 113 114 115 116 117 118
{
  GeglRectangle roi = {0,0,1024, 1024};
  GeglEDL *edl = g_malloc0(sizeof (GeglEDL));

  edl->gegl = gegl_node_new ();
  edl->cache_flags = 0;
  edl->cache_flags = CACHE_TRY_ALL;
  //edl->cache_flags = CACHE_TRY_ALL | CACHE_MAKE_ALL;
  edl->selection_start = 23;
  edl->selection_end = 42;

  edl->cache_loader     = gegl_node_new_child (edl->gegl, "operation", "gegl:" CACHE_FORMAT  "-load", NULL);

119 120 121 122 123 124 125
  edl->output_path        = DEFAULT_output_path;
  edl->video_codec        = DEFAULT_video_codec;
  edl->audio_codec        = DEFAULT_audio_codec;
  edl->video_width        = DEFAULT_video_width;
  edl->video_height       = DEFAULT_video_height;
  edl->proxy_width        = DEFAULT_proxy_width;
  edl->proxy_height       = DEFAULT_proxy_height;
126
  edl->video_size_default = 1;
127 128 129 130 131 132 133
  edl->video_bufsize      = DEFAULT_video_bufsize;
  edl->video_bitrate      = DEFAULT_video_bitrate;
  edl->video_tolerance    = DEFAULT_video_tolerance;;
  edl->audio_bitrate      = DEFAULT_audio_bitrate;
  edl->audio_samplerate   = DEFAULT_audio_samplerate;
  edl->framedrop          = DEFAULT_framedrop;
  edl->frame_pos_ui       = 0.0; /* frame-no in ui shell */
134 135
  edl->frame = -1;        /* frame-no in renderer thread */
  edl->pos  = -1.0;       /* frame-no in renderer thread */
136 137 138 139 140 141 142 143
  edl->scale = 1.0;

  edl->buffer = gegl_buffer_new (&roi, babl_format ("R'G'B'A u8"));
  edl->buffer_copy = gegl_buffer_new (&roi, babl_format ("R'G'B'A u8"));
  edl->buffer_copy_temp = gegl_buffer_new (&roi, babl_format ("R'G'B'A u8"));

  edl->clip_query = strdup ("");
  edl->use_proxies = 0;
144
  edl->ui_mode = GEDL_UI_MODE_PART;
145 146 147 148 149

  g_mutex_init (&edl->buffer_copy_mutex);
  return edl;
}

150 151
void gcut_set_size (GeglEDL *edl, int width, int height);
void gcut_set_size (GeglEDL *edl, int width, int height)
152 153 154 155 156
{
  edl->width = width;
  edl->height = height;
}

157
void     gcut_free          (GeglEDL *edl)
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
{
  while (edl->clips)
  {
    clip_free (edl->clips->data);
    edl->clips = g_list_remove (edl->clips, edl->clips->data);
  }
  if (edl->path)
    g_free (edl->path);
  if (edl->parent_path)
    g_free (edl->parent_path);

  g_object_unref (edl->gegl);
  if (edl->buffer)
    g_object_unref (edl->buffer);
  if (edl->buffer_copy)
    g_object_unref (edl->buffer_copy);
  if (edl->buffer_copy_temp)
    g_object_unref (edl->buffer_copy_temp);
  g_mutex_clear (&edl->buffer_copy_mutex);
  g_free (edl);
}

180
Clip *gcut_get_clip (GeglEDL *edl, double frame_pos, double *clip_frame_pos)
181 182
{
  GList *l;
183
  double clip_start = 0;
184 185 186 187

  for (l = edl->clips; l; l = l->next)
  {
    Clip *clip = l->data;
188
    double clip_duration = clip_get_duration (clip);
189 190 191
    if (clip->is_meta)
      continue;

192
    if (frame_pos - clip_start < clip_duration)
193 194
    {
      /* found right clip */
195 196
      if (clip_frame_pos)
       *clip_frame_pos = (frame_pos - clip_start) + clip_get_start (clip);
197 198
      return clip;
    }
199
    clip_start += clip_duration;
200 201 202 203
  }
  return NULL;
}

204
void gcut_set_use_proxies (GeglEDL *edl, int use_proxies)
205
{
206
  double frame_pos;
207 208 209
  edl->use_proxies = use_proxies;

  if (edl->use_proxies)
210
    gcut_set_size (edl, edl->proxy_width, edl->proxy_height);
211
  else
212
    gcut_set_size (edl, edl->video_width, edl->video_height);
213

214 215 216
  frame_pos = edl->pos;

  if (frame_pos > 0)
217 218
  {
    edl->frame--;
219
    gcut_set_pos (edl, frame_pos);
220 221 222 223 224 225 226
  }

}

/* computes the hash of a given rendered frame - without altering
 * any state
 */
227 228 229 230
gchar *gcut_get_pos_hash_full (GeglEDL *edl, double pos,
                               Clip **clip0, double *clip0_pos,
                               Clip **clip1, double *clip1_pos,
                               double *mix)
231
{
232
  GString *str = g_string_new ("");
233
  GList *l;
234 235
  double clip_start = 0;
  double prev_clip_start = 0;
236

237
  pos = gcut_snap_pos (edl->fps, pos);
238 239 240 241

  for (l = edl->clips; l; l = l->next)
  {
    Clip *clip = l->data;
242
    double clip_duration = clip_get_duration (clip);
243 244 245 246

    if (clip->is_meta)
      continue;

247
    if (pos - clip_start < clip_duration)
248
    {
249
      double clip_frame_pos = (pos - clip_start) + clip_get_start (clip);
250 251 252 253 254

      GList *lp = l->prev;
      GList *ln = l->next;
      Clip *prev = lp?lp->data:NULL;
      Clip *next = ln?ln->data:NULL;
255 256
      double prev_fade_len;
      double next_fade_len;
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

      while (prev && prev->is_meta)
      {
        lp = lp->prev;
        prev = lp?lp->data:NULL;
      }

      while (next && next->is_meta)
      {
        ln = ln->next;
        next = ln?ln->data:NULL;
      }

      /* XXX: fade in from black if there is no previous clip */

272 273
      prev_fade_len = prev ? clip_get_duration (prev) : clip_duration;
      next_fade_len = next ? clip_get_duration (next) : clip_duration;
274

275 276
      if (prev_fade_len > clip_duration) prev_fade_len = clip_duration;
      if (next_fade_len > clip_duration) next_fade_len = clip_duration;
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

      prev_fade_len /= 2;
      next_fade_len /= 2;  /* 1/4 the length of the smallest of this or other
                              clip is our maximum fade - should be adjusted to
                              1/2 later - when perhaps a scaling factor or a
                              per-case duration is set which gets maxed by this
                              heuristic   */
      if (clip->fade/2 < prev_fade_len)
        prev_fade_len = clip->fade/2;

      if (next)
      {
        if (next->fade/2 < next_fade_len)
          next_fade_len = next->fade/2;
      }

293
      if (prev && pos - clip_start < prev_fade_len)                   /* in */
294
      {
295 296 297
        char *clip0_hash = clip_get_pos_hash (clip, clip_frame_pos);
        char *clip1_hash = clip_get_pos_hash (prev, pos - prev_clip_start + clip_get_start (prev));
        double ratio = 0.5 + ((pos -clip_start) * 1.0 / prev_fade_len)/2;
298 299 300
        gchar fstr[G_ASCII_DTOSTR_BUF_SIZE];
        g_ascii_dtostr (fstr, sizeof(fstr), ratio);
        g_string_append_printf (str, "%s %s %s", clip1_hash, clip0_hash, fstr);
301

302 303 304
        g_free (clip0_hash);
        g_free (clip1_hash);
        if (clip0) *clip0 = prev;
305
        if (clip0_pos) *clip0_pos = pos - prev_clip_start + clip_get_start (prev);
306
        if (clip1) *clip1 = clip;
307
        if (clip1_pos) *clip1_pos = clip_frame_pos;
308
        if (mix) *mix = ratio;
309
        goto done;
310 311
      }

312
      if (next && pos - clip_start > clip_duration - next_fade_len)/* out*/
313
      {
314 315 316
        char *clip0_hash = clip_get_pos_hash (clip, clip_frame_pos);
        char *clip1_hash = clip_get_pos_hash (next, pos - (clip_start + clip_duration) + clip_get_start (next));
        double ratio = (1.0-(clip_duration -(pos -clip_start)) * 1.0 / next_fade_len)/2;
317 318 319
        gchar fstr[G_ASCII_DTOSTR_BUF_SIZE];
        g_ascii_dtostr (fstr, sizeof(fstr), ratio);
        g_string_append_printf (str, "%s %s %s", clip0_hash, clip1_hash, fstr);
320 321 322
        g_free (clip0_hash);
        g_free (clip1_hash);
        if (clip0) *clip0 = clip;
323 324
        if (clip0_pos) *clip0_pos = clip_frame_pos;
        if (clip1_pos) *clip1_pos = pos - (clip_start +clip_duration) + clip_get_start (next);
325 326
        if (clip1) *clip1 = next;
        if (mix)   *mix = ratio;
327
        goto done;
328 329 330
      }
      else
      {
331 332 333 334
        char *clip0_hash = clip_get_pos_hash (clip, clip_frame_pos);
        g_string_append_printf (str, "%s ", clip0_hash);
        g_free (clip0_hash);

335
        if (clip0) *clip0 = clip;
336
        if (clip0_pos) *clip0_pos = clip_frame_pos;
337 338
        if (clip1) *clip1 = NULL;
        if (mix)   *mix = 0.0;
339
        goto done;
340 341 342
      }
    }
    prev_clip_start = clip_start;
343
    clip_start += clip_duration;
344 345
  }

346 347 348 349 350
  if (clip0)     *clip0 = NULL;
  if (clip0_pos) *clip0_pos = 0;
  if (clip1_pos) *clip1_pos = 0;
  if (clip1)     *clip1 = NULL;
  if (mix)       *mix = 0.0;
351
  return NULL;
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
done:

  {
    GList *l;
    for (l = edl->clips; l; l = l->next)
    {
      Clip *c = l->data;
      if (c->is_meta)
      {
        if (pos >= c->start && pos < c->end)
          g_string_append_printf (str, "[%s]\n", c->filter_graph);
      }
    }
  }

  {
    GChecksum *hash = g_checksum_new (G_CHECKSUM_MD5);
    char *ret;
    g_checksum_update (hash, (void*)str->str, -1);
    ret = g_strdup (g_checksum_get_string(hash));
    g_checksum_free (hash);
    return ret;
  }
375 376
}

377
gchar *gcut_get_pos_hash (GeglEDL *edl, double pos)
378
{
379
  return gcut_get_pos_hash_full (edl, pos, NULL, NULL, NULL, NULL, NULL);
380 381
}

382
void gcut_update_buffer (GeglEDL *edl)
383 384 385 386 387 388 389 390 391 392
{
  g_mutex_lock (&edl->buffer_copy_mutex);
  {
    GeglBuffer *t = edl->buffer_copy;
    edl->buffer_copy = gegl_buffer_dup (edl->buffer);
    if (t)
      g_object_unref (t);
  }
  g_mutex_unlock (&edl->buffer_copy_mutex);
}
393
/*  calling this causes gcut to rig up its graphs for providing/rendering this frame
394
 */
395
void gcut_set_pos (GeglEDL *edl, double pos)
396
{
397
  int frame;
398 399
  Clip *clip0; double clip0_pos;
  Clip *clip1; double clip1_pos;
400

401 402 403
  pos = gcut_snap_pos (edl->fps, pos);
  frame  = pos * edl->fps;

404 405 406 407 408
  if ((edl->frame) == frame && (frame != 0))
  {
    return;
  }

409
  {
410 411 412

  double mix;

413
  char *frame_hash = gcut_get_pos_hash_full (edl, pos, &clip0, &clip0_pos, &clip1, &clip1_pos, &mix);
414
  char *cache_path = g_strdup_printf ("%s.gcut/cache/%s", edl->parent_path, frame_hash);
415
  edl->pos = pos;
416
  edl->frame = frame;
417 418 419 420 421 422
  g_free (frame_hash);
  if (g_file_test (cache_path, G_FILE_TEST_IS_REGULAR) &&
      (edl->cache_flags & CACHE_TRY_ALL))
  {
    Clip *clip = NULL;
    gegl_node_set (edl->cache_loader, "path", cache_path, NULL);
423
    gegl_node_link_many (edl->cache_loader, edl->final_result, NULL);
424
    clip = edl_get_clip_for_pos (edl, pos);
425 426 427 428 429 430 431 432 433 434
    if (clip)
    {
    if (clip->audio)
      {
        g_object_unref (clip->audio);
        clip->audio = NULL;
      }
    clip->audio = gegl_audio_fragment_new (44100, 2, 0, 44100);
    gegl_meta_get_audio (cache_path, clip->audio);
    }
435
    {
436
    GeglRectangle ext = gegl_node_get_bounding_box (edl->final_result);
437
    gegl_buffer_set_extent (edl->buffer, &ext);
438
    }
439 440
    gegl_node_process (edl->store_final_buf);

441
    gcut_update_buffer (edl);
442 443 444 445 446 447 448 449 450 451 452 453
    g_free (cache_path);
    return;
  }

  if (clip0 == NULL)
  {
    g_free (cache_path);
    return;
  }

  if (clip1 == NULL)
  {
454
    clip_render_pos (clip0, clip0_pos);
455
    gegl_node_link_many (clip0->nop_crop, edl->video_result, NULL);
456 457 458 459
  }
  else
  {
    gegl_node_set (edl->mix, "ratio", mix, NULL);
460 461
    clip_render_pos (clip0, clip0_pos);
    clip_render_pos (clip1, clip1_pos);
462
    gegl_node_link_many (clip0->nop_crop, edl->mix, edl->video_result, NULL);
463 464
    gegl_node_connect_to (clip1->nop_crop, "output", edl->mix, "aux");
  }
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
  gegl_node_link_many (edl->video_result, edl->final_result, NULL);

  {
    GList *l;
    for (l = edl->clips; l; l = l->next)
    {
      Clip *c = l->data;
      if (c->is_meta)
      {
        if (pos >= c->start && pos < c->end)
        {
          GeglNode *prev = gegl_node_get_producer (edl->final_result, "input", NULL);
          gegl_create_chain (c->filter_graph, prev, edl->final_result,
                             pos - c->start,
                             edl->height, NULL, NULL);
        }
      }
    }
  }

485
  gegl_node_process (edl->store_final_buf);
486
  gcut_update_buffer (edl);
487 488 489

  /* write cached render of this frame */
  if (cache_path &&
490
      !strstr (clip0->path, ".gcut/cache") && (!edl->use_proxies))
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    {
      const gchar *cache_path_final = cache_path;
      gchar *cache_path       = g_strdup_printf ("%s~", cache_path_final);

      if (!g_file_test (cache_path_final, G_FILE_TEST_IS_REGULAR) && !edl->playing)
        {
          GeglNode *save_graph = gegl_node_new ();
          GeglNode *save;
          save = gegl_node_new_child (save_graph,
                      "operation", "gegl:" CACHE_FORMAT "-save",
                      "path", cache_path,
                      NULL);
          if (!strcmp (CACHE_FORMAT, "png"))
          {
            gegl_node_set (save, "bitdepth", 8, NULL);
          }
507
          gegl_node_link_many (edl->final_result, save, NULL);
508 509 510 511 512 513 514 515 516 517 518 519
          gegl_node_process (save);
          if (clip1 && clip1->audio && mix > 0.5)
            gegl_meta_set_audio (cache_path, clip1->audio);
          else if (clip0->audio) // XXX: mix audio
            gegl_meta_set_audio (cache_path, clip0->audio);
          rename (cache_path, cache_path_final);
          g_object_unref (save_graph);
        }
      g_free (cache_path);
    }

  g_free (cache_path);
520
  }
521 522
}

523
void gcut_set_fps (GeglEDL *edl, double fps)
524 525 526
{
  edl->fps = fps;
}
527
double gcut_get_fps (GeglEDL *edl)
528 529 530
{
  return edl->fps;
}
531
double gcut_get_pos (GeglEDL *edl)
532
{
533
  return edl->pos;
534
}
535

536
GeglAudioFragment *gcut_get_audio (GeglEDL *edl)
537
{
538
  Clip * clip = edl_get_clip_for_pos (edl, edl->pos);
539 540 541
  return clip?clip->audio:NULL;
}

542
double gcut_get_duration (GeglEDL *edl)
543
{
544
  double count = 0;
545 546 547 548
  GList *l;
  for (l = edl->clips; l; l = l->next)
  {
    ((Clip*)(l->data))->abs_start = count;
549
    count += clip_get_duration (l->data);
550 551 552 553 554 555
  }
  return count;
}
#include <string.h>


556
void gcut_parse_line (GeglEDL *edl, const char *line)
557
{
558
  double start = 0; double end = 0;
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
  const char *rest = NULL;
  char path[1024];
  if (line[0] == '#' ||
      line[1] == '#' ||
      strlen (line) < 4)
    return;

  if (strchr (line, '=') && !strstr(line, "--"))
   {
     char *key = g_strdup (line);
     char *value = strchr (key, '=') + 1;
     value[-1]='\0';

     while (value[strlen(value)-1]==' ' ||
            value[strlen(value)-1]=='\n')
            value[strlen(value)-1]='\0';
575
     if (!strcmp (key, "fps"))               gcut_set_fps (edl, g_strtod (value, NULL));
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
     if (!strcmp (key, "framedrop"))         edl->framedrop     = g_strtod (value, NULL);
     if (!strcmp (key, "output-path"))       edl->output_path = g_strdup (value);
     if (!strcmp (key, "video-codec"))       edl->video_codec = g_strdup (value);
     if (!strcmp (key, "audio-codec"))       edl->audio_codec = g_strdup (value);
     if (!strcmp (key, "audio-sample-rate")) edl->audio_samplerate = g_strtod (value, NULL);
     if (!strcmp (key, "video-bufsize"))     edl->video_bufsize = g_strtod (value, NULL);
     if (!strcmp (key, "video-bitrate"))     edl->video_bitrate = g_strtod (value, NULL);
     if (!strcmp (key, "audio-bitrate"))     edl->audio_bitrate = g_strtod (value, NULL);
     if (!strcmp (key, "video-width"))       edl->video_width = g_strtod (value, NULL);
     if (!strcmp (key, "video-height"))      edl->video_height = g_strtod (value, NULL);
     if (!strcmp (key, "proxy-width"))       edl->proxy_width = g_strtod (value, NULL);
     if (!strcmp (key, "proxy-height"))      edl->proxy_height = g_strtod (value, NULL);
     if (!strcmp (key, "frame-start"))       edl->range_start = g_strtod (value, NULL);
     if (!strcmp (key, "frame-end"))         edl->range_end = g_strtod (value, NULL);
     if (!strcmp (key, "selection-start"))   edl->selection_start = g_strtod (value, NULL);
     if (!strcmp (key, "selection-end"))     edl->selection_end = g_strtod (value, NULL);
592
     if (!strcmp (key, "frame-pos"))         edl->frame_pos_ui = g_strtod (value, NULL);
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
     if (!strcmp (key, "frame-scale"))       edl->scale = g_strtod (value, NULL);
     if (!strcmp (key, "t0"))                edl->t0 = g_strtod (value, NULL);

     g_free (key);
     return;
   }
  if (strstr (line, "--"))
    rest = strstr (line, "--") + 2;


  {
    const char *p = strstr (line, "--");
    if (!p)
      p = line + strlen(line)-1;
    {
608
      int is_seconds = 0;
609 610 611
      if (p>line) p --;
      while (p>line && *p == ' ') p --;

612
      while (p>line && (isdigit (*p) || (*p=='s') || (*p=='.') || (*p==':' ))){
613 614 615 616 617 618
        if (*p == 's') is_seconds = 1;
        p --;
      }
      end = g_strtod (p+1, NULL);
      if (!is_seconds)
        end /= edl->fps;
619 620 621

      if (p>line) p --;
      while (p>line && *p == ' ') p --;
622
      is_seconds = 0;
623
      while (p>line && (isdigit (*p) || (*p=='s') || (*p=='.') || (*p==':'))){
624 625 626 627 628 629
        if (*p == 's') is_seconds = 1;
        p --;
      }
      start = g_strtod (p+1, NULL);
      if (!is_seconds)
        start /= edl->fps;
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649

      if (p>line) p --;
      while (p>line && *p == ' ') p --;

      memcpy (path, line, (p-line) + 1);
      path[(p-line)+1]=0;
    }
  }

  if (strlen (path) > 3)
    {
      Clip *clip = NULL;
      int ff_probe = 0;
     clip = clip_new_full (edl, path, start, end);


     if (!clip_is_static_source (clip) &&
         (start == 0 && end == 0))
       ff_probe = 1;
     edl->clips = g_list_append (edl->clips, clip);
650
     if (rest && strstr (rest, "[fade="))
651
       {
652
         int was_seconds = 0;
653
         ff_probe = 1;
654
         rest = strstr (rest, "[fade=") + strlen ("[fade=");
655 656 657 658 659 660
         clip->fade = g_strtod (rest, NULL);
         while (*rest && *rest != ']'){ if (*rest == 's') was_seconds =1; rest++;}
         if (!was_seconds)
         {
           clip->fade = clip->fade / edl->fps;
         }
661 662
         if (*rest == ']') rest++;
       }
663 664 665 666 667 668 669 670
     if (rest && strstr (rest, "[fps="))
       {
         ff_probe = 1;
         rest = strstr (rest, "[fps=") + strlen ("[fps=");
         clip->fps = g_strtod (rest, NULL);
         while (*rest && *rest != ']'){ rest++;}
         if (*rest == ']') rest++;
       }
671 672 673 674 675 676 677 678
     if (rest && strstr (rest, "[rate="))
       {
         ff_probe = 1;
         rest = strstr (rest, "[rate=") + strlen ("[rate=");
         clip->rate = g_strtod (rest, NULL);
         while (*rest && *rest != ']'){ rest++;}
         if (*rest == ']') rest++;
       }
679 680 681

     if (rest) while (*rest == ' ')rest++;

682
     if (clip == edl->clips->data || clip->fps < 0.001)
683 684 685 686 687 688
     {
       ff_probe = 1;
     }

     if (ff_probe && !clip_is_static_source (clip))
       {
689 690
         int clip_frames;
         gcut_get_video_info (clip->path, &clip_frames, &clip->duration, &clip->fps);
691 692 693

         if (edl->fps == 0.0)
         {
694
           gcut_set_fps (edl, clip->fps);
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
         }
       }

     if (rest)
       {
         clip->filter_graph = g_strdup (rest);
         while (clip->filter_graph[strlen(clip->filter_graph)-1]==' ' ||
                clip->filter_graph[strlen(clip->filter_graph)-1]=='\n')
                clip->filter_graph[strlen(clip->filter_graph)-1]='\0';
       }

     if (clip->end == 0)
     {
        clip->end = clip->duration;
     }
    }
  else if (start == 0 && end == 0 && rest)
  {
    Clip *clip = clip_new_full (edl, NULL, 0, 0);
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
    const char *first = NULL;
    const char *second = NULL;
    const char *p = rest;
    while (*p == ' ') p++;
    if (isdigit(*p))
    {
      first = p;
      while (isdigit(*p) || (*p == '.') || (*p == 's')) p++;
      while (*p == ' ') p++;
      if (isdigit(*p))
      {
        second = p;
        while (isdigit(*p) || (*p == '.') || (*p == 's')) p++;
      }
      while (*p == ' ') p++;
      rest = p;
    }
731
    clip->filter_graph = g_strdup (rest);
732 733 734 735 736 737 738
    if (first)
      clip->start = g_strtod (first, NULL);
    if (second)
      clip->end = g_strtod (second, NULL);
    else
      clip->end = clip->start;

739 740 741 742 743 744
    edl->clips = g_list_append (edl->clips, clip);
  }
}

#include <string.h>

745 746 747
void gcut_update_video_size (GeglEDL *edl);
GeglEDL *gcut_new_from_string (const char *string, const char *parent_path);
GeglEDL *gcut_new_from_string (const char *string, const char *parent_path)
748 749
{
  GString *line = g_string_new ("");
750
  GeglEDL *edl = gcut_new ();
751 752 753 754 755 756 757 758 759 760 761 762
  int clips_done = 0;
  int newlines = 0;
  edl->parent_path = g_strdup (parent_path);

  for (const char *p = string; p==string || *(p-1); p++)
  {
    switch (*p)
    {
      case 0:
      case '\n':
       if (clips_done)
       {
763 764
      //   if (line->len > 2)
      //     gcut_parse_clip (edl, line->str);
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
         g_string_assign (line, "");
       }
       else
       {
         if (line->str[0] == '-' &&
             line->str[1] == '-' &&
             line->str[2] == '-')
         {
           clips_done = 1;
           g_string_assign (line, "");
         }
         else
         {
           if (*p == 0)
           {
             newlines = 2;
           }
           else if (*p == '\n')
           {
             newlines ++;
           }
           else
           {
             newlines = 0;
           }
           if (strchr (line->str, '='))
             newlines = 3;

           if (newlines >= 2)
           {
795
             gcut_parse_line (edl, line->str);
796 797 798 799 800 801 802 803 804 805 806 807 808
             g_string_assign (line, "");
           }
           else
             g_string_append_c (line, *p);
         }
       }
       break;
      default: g_string_append_c (line, *p);
       break;
    }
  }
  g_string_free (line, TRUE);

809 810
  gcut_update_video_size (edl);
  gcut_set_use_proxies (edl, edl->use_proxies);
811 812 813 814

  return edl;
}

815
void gcut_save_path (GeglEDL *edl, const char *path)
816 817
{
  char *serialized;
818
  serialized = gcut_serialize (edl);
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840

  if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
  {
     char backup_path[4096];
     struct tm *tim;
     char *old_contents = NULL;

     g_file_get_contents (path, &old_contents, NULL, NULL);
     if (old_contents)
     {
       char *oc, *s;

       oc = strstr (old_contents, "\n\n");
       s  = strstr (serialized, "\n\n");
       if (oc && s && !strcmp (oc, s))
       {
         g_free (old_contents);
         return;
       }
       g_free (old_contents);
     }

841
     sprintf (backup_path, "%s.gcut/history/%s-", edl->parent_path, basename(edl->path));
842

843
     {
844 845
     time_t now = time(NULL);
     tim = gmtime(&now);
846
     }
847 848 849 850 851

     strftime(backup_path + strlen(backup_path), sizeof(backup_path)-strlen(backup_path), "%Y%m%d_%H%M%S", tim);
     rename (path, backup_path);
  }

852
  {
853 854 855 856 857 858 859 860 861 862 863 864 865
  FILE *file = fopen (path, "w");
  if (!file)
  {
    g_free (serialized);
    return;
  }

  if (serialized)
  {
    fprintf (file, "%s", serialized);
    g_free (serialized);
  }
  fclose (file);
866
  }
867 868
}

869
void gcut_update_video_size (GeglEDL *edl)
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
{
  if ((edl->video_width == 0 || edl->video_height == 0) && edl->clips)
    {
      Clip *clip = edl->clips->data;
      GeglNode *gegl = gegl_node_new ();
      GeglRectangle rect;
      // XXX: is ff-load good for pngs and jpgs as well?
      GeglNode *probe;
      probe = gegl_node_new_child (gegl, "operation", "gegl:ff-load", "path", clip->path, NULL);
      gegl_node_process (probe);
      rect = gegl_node_get_bounding_box (probe);
      edl->video_width = rect.width;
      edl->video_height = rect.height;
      g_object_unref (gegl);
    }
  if ((edl->proxy_width <= 0) && edl->video_width)
  {
    edl->proxy_width = 320;
  }
  if ((edl->proxy_height <= 0) && edl->video_width)
  {
    edl->proxy_height = edl->proxy_width * (1.0 * edl->video_height / edl->video_width);
  }
}

895
static void generate_gcut_dir (GeglEDL *edl)
896
{
897
  char *tmp = g_strdup_printf ("cd %s; mkdir .gcut 2>/dev/null ; mkdir .gcut/cache 2>/dev/null ; mkdir .gcut/proxy 2>/dev/null ; mkdir .gcut/thumb 2>/dev/null ; mkdir .gcut/video 2>/dev/null; mkdir .gcut/history 2>/dev/null", edl->parent_path);
898 899 900 901 902 903 904 905
  system (tmp);
  g_free (tmp);
}

static GTimer *  timer            = NULL;
static guint     timeout_id       = 0;
static gdouble   throttle         = 4.0;

906
static void gcut_reread (GeglEDL *edl)
907
{
908
  GeglEDL *new_edl = gcut_new_from_path (edl->path);
909 910 911 912 913 914 915
  GList *l;

  /* swap clips */
  l = edl->clips;
  edl->clips = new_edl->clips;
  new_edl->clips = l;
  edl->active_clip = NULL; // XXX: better to resolve?
916
  edl->active_overlay = NULL;
917 918 919 920 921 922 923 924 925 926 927 928 929

  for (l = edl->clips; l; l = l->next)
  {
    Clip *clip = l->data;
    clip->edl = edl;
  }

  for (l = new_edl->clips; l; l = l->next)
  {
    Clip *clip = l->data;
    clip->edl = new_edl;
  }

930
  gcut_free (new_edl);
931 932 933 934 935
}

static gboolean timeout (gpointer user_data)
{
  GeglEDL *edl = user_data;
936
  gcut_reread (edl);
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
  // system (user_data);
  g_timer_start (timer);
  timeout_id = 0;
  return FALSE;
}


static void file_changed (GFileMonitor     *monitor,
                          GFile            *file,
                          GFile            *other_file,
                          GFileMonitorEvent event_type,
                          GeglEDL          *edl)
{
  if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
    {
          if (!timeout_id)
            {
              gdouble elapsed = g_timer_elapsed (timer, NULL);
              gdouble wait    = throttle - elapsed;

              if (wait <= 0.0)
                wait = 0.0;

              timeout_id = g_timeout_add (wait * 1000, timeout, edl);
            }
    }
}

965
static void
966
gcut_monitor_start (GeglEDL *edl)
967 968 969 970
{
  if (!edl->path)
    return;
  /* save to make sure file exists */
971
  gcut_save_path (edl, edl->path);
972 973 974 975 976 977 978 979
  /* start monitor */
  timer = g_timer_new ();
  edl->monitor = g_file_monitor_file (g_file_new_for_path (edl->path),
                                      G_FILE_MONITOR_NONE,
                                      NULL, NULL);
  if(0)g_signal_connect (edl->monitor, "changed", G_CALLBACK (file_changed), edl);
}

980
GeglEDL *gcut_new_from_path (const char *path)
981 982 983 984 985 986 987 988 989 990 991
{
  GeglEDL *edl = NULL;
  gchar *string = NULL;

  g_file_get_contents (path, &string, NULL, NULL);
  if (string)
  {
    char *rpath = realpath (path, NULL);
    char *parent = g_strdup (rpath);
    strrchr(parent, '/')[1]='\0';

992
      edl = gcut_new_from_string (string, parent);
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008

    g_free (parent);
    g_free (string);
    if (!edl->path)
      edl->path = g_strdup (realpath (path, NULL)); // XXX: leak
  }
  else
  {
    char *parent = NULL;
    if (path[0] == '/')
    {
      parent = strdup (path);
      strrchr(parent, '/')[1]='\0';
    }
    else
    {
1009 1010
      parent = g_malloc0 (strlen(path));
      getcwd (parent, strlen(path));
1011
    }
1012
    edl = gcut_new_from_string ("", parent);
1013 1014 1015 1016 1017 1018 1019 1020
    if (!edl->path)
    {
      if (path[0] == '/')
      {
        edl->path = g_strdup (path);
      }
      else
      {
1021
        edl->path = g_strdup_printf ("%s/%s", parent, basename ((void*)path));
1022 1023 1024 1025
      }
    }
    g_free (parent);
  }
1026
  generate_gcut_dir (edl);
1027 1028 1029 1030 1031 1032

  return edl;
}

static void setup (GeglEDL *edl)
{
1033 1034
  edl->video_result = gegl_node_new_child (edl->gegl, "operation", "gegl:nop", NULL);
  edl->final_result = gegl_node_new_child (edl->gegl, "operation", "gegl:nop", NULL);
1035 1036 1037
  edl->mix = gegl_node_new_child (edl->gegl, "operation", "gegl:mix", NULL);
  edl->encode = gegl_node_new_child (edl->gegl, "operation", "gegl:ff-save",
                                     "path",           edl->output_path,
1038
                                     "frame-rate",     gcut_get_fps (edl),
1039 1040 1041 1042 1043 1044 1045 1046 1047
                                     "video-bit-rate", edl->video_bitrate,
                                     "video-bufsize",  edl->video_bufsize,
                                     "audio-bit-rate", edl->audio_bitrate,
                                     "audio-codec",    edl->audio_codec,
                                     "video-codec",    edl->video_codec,
                                     NULL);
  edl->cached_result = gegl_node_new_child (edl->gegl, "operation", "gegl:buffer-source", "buffer", edl->buffer, NULL);
  edl->store_final_buf = gegl_node_new_child (edl->gegl, "operation", "gegl:write-buffer", "buffer", edl->buffer, NULL);

1048 1049
  gegl_node_link_many (edl->video_result, edl->final_result, NULL);
  gegl_node_link_many (edl->final_result, edl->store_final_buf, NULL);
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
  gegl_node_link_many (edl->cached_result, edl->encode, NULL);
}

static void init (int argc, char **argv)
{
  gegl_init (&argc, &argv);
  g_object_set (gegl_config (),
                "application-license", "GPL3",
                NULL);
}

static void encode_frames (GeglEDL *edl)
{
  int frame_no;
1064
  for (frame_no = edl->range_start * edl->fps; frame_no <= edl->range_end * edl->fps; frame_no++)
1065
  {
1066 1067
    int frame_no_ui = frame_no;
    gcut_set_pos (edl, frame_no_ui / edl->fps);
1068

1069 1070 1071
    fprintf (stdout, "\r%1.2f%% %04d / %04d   ",
     100.0 * (frame_no/edl->fps-edl->range_start) * 1.0 / (edl->range_end - edl->range_start),
     frame_no, (int)(edl->range_end * edl->fps));
1072

1073
    gegl_node_set (edl->encode, "audio", gcut_get_audio (edl), NULL);
1074 1075 1076 1077 1078 1079
    gegl_node_process (edl->encode);
    fflush (0);
  }
  fprintf (stdout, "\n");
}

1080
static void nop_handler(int sig)
1081 1082 1083 1084 1085
{
}

static int stop_cacher = 0;

1086
static void handler1(int sig)
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
{
  stop_cacher = 1;
}

static int cacheno = 0;
static int cachecount = 2;

static inline int this_cacher (int frame_no)
{
  if (frame_no % cachecount == cacheno) return 1;
  return 0;
}

static void process_frames_cache (GeglEDL *edl)
{
1102 1103
  int frame_no = edl->frame_pos_ui * edl->fps;
  int frame_start = frame_no;
1104
  int frames;
1105 1106

  GList *l;
1107
  double clip_start = 0;
1108

1109
  signal(SIGUSR2, handler1);
1110
  frames = gcut_get_duration (edl) * edl->fps;
1111 1112
  // TODO: use bitmap from ui to speed up check

1113
  edl->frame_pos_ui = frame_start / edl->fps;
1114 1115
  if (this_cacher (floor (edl->frame_pos_ui * edl->fps)))
    gcut_set_pos (edl, edl->frame_pos_ui);
1116 1117 1118 1119 1120 1121
   if (stop_cacher)
    return;

  for (l = edl->clips; l; l = l->next)
  {
    Clip *clip = l->data;
1122 1123
    double clip_duration = clip_get_duration (clip);
    int frame_pos_ui = floor (clip_start * edl->fps);
1124
    if (this_cacher (floor (frame_pos_ui * edl->fps)))
1125
    {
1126
      gcut_set_pos (edl, frame_pos_ui / edl->fps);
1127 1128
    }

1129
    clip_start += clip_duration;
1130 1131 1132 1133
    if (stop_cacher)
      return;
  }

1134
  for (frame_no = frame_start - 3; frame_no < frames; frame_no++)
1135
  {
1136
    double frame_pos_ui = frame_no / edl->fps;
1137 1138 1139

    if (this_cacher (frame_no))
      gcut_set_pos (edl, frame_pos_ui);
1140 1141 1142 1143 1144
    if (stop_cacher)
      return;
  }
  for (frame_no = 0; frame_no < frame_start; frame_no++)
  {
1145
    double frame_pos_ui = frame_no / edl->fps;
1146 1147
    if (this_cacher (frame_no))
      gcut_set_pos (edl, frame_pos_ui);
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
    if (stop_cacher)
      return;
  }
}

static inline void set_bit (guchar *bitmap, int no)
{
  bitmap[no/8] |= (1 << (no % 8));
}

1158
guchar *gcut_get_cache_bitmap (GeglEDL *edl, int *length_ret)
1159
{
1160 1161
  double duration = gcut_get_duration (edl);
  int frames = duration * edl->fps;
1162
  int frame_no;
1163
  int length = (frames / 8) + 1;
1164 1165 1166 1167 1168
  guchar *ret = g_malloc0 (length);

  if (length_ret)
    *length_ret = length;

1169
  for (frame_no = 0; frame_no < frames; frame_no++)
1170
  {
1171
    const gchar *hash = gcut_get_pos_hash (edl, frame_no / edl->fps);
1172
    gchar *path = g_strdup_printf ("%s.gcut/cache/%s", edl->parent_path, hash);
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
    if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
      set_bit (ret, frame_no);
    g_free (path);
  }

  return ret;
}

static void process_frames_cache_stat (GeglEDL *edl)
{
1183 1184
  int frame_no = edl->frame_pos_ui / edl->fps;
  double duration;
1185
  signal(SIGUSR2, handler1);
1186
  duration = gcut_get_duration (edl);
1187 1188 1189 1190 1191 1192

  /* XXX: should probably do first frame of each clip - since
          these are used for quick keyboard navigation of the
          project
   */

1193
  for (frame_no = 0; frame_no < duration * edl->fps; frame_no++)
1194
  {
1195
    const gchar *hash = gcut_get_pos_hash (edl, frame_no / edl->fps);
1196
    gchar *path = g_strdup_printf ("%s.gcut/cache/%s", edl->parent_path, hash);
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
    if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
      fprintf (stdout, "%i ", frame_no);
    //  fprintf (stdout, ". %i %s\n", frame_no, hash);
   // else
    //  fprintf (stdout, "  %i %s\n", frame_no, hash);
    g_free (path);
  }
}

int gegl_make_thumb_image (GeglEDL *edl, const char *path, const char *icon_path)
{
  GString *str = g_string_new ("");

  g_string_assign (str, "");
  g_string_append_printf (str, "%s iconographer -p -h -f 'mid-col 96 audio' %s -a %s",
  //g_string_append_printf (str, "iconographer -p -h -f 'thumb 96' %s -a %s",
1213
                          gcut_binary_path, path, icon_path);
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
  system (str->str);

  g_string_free (str, TRUE);

  return 0;
}

int gegl_make_thumb_video (GeglEDL *edl, const char *path, const char *thumb_path)
{
  GString *str = g_string_new ("");

  g_string_assign (str, "");
  g_string_append_printf (str, "ffmpeg -y -i %s -vf scale=%ix%i %s", path, edl->proxy_width, edl->proxy_height, thumb_path);
  system (str->str);
  g_string_free (str, TRUE);

  return 0;
#if 0  // much slower and worse for fps/audio than ffmpeg method for creating thumbs
  int tot_frames; //
  g_string_append_printf (str, "video-bitrate=100\n\noutput-path=%s\nvideo-width=256\nvideo-height=144\n\n%s\n", thumb_path, path);
1234
  edl = gcut_new_from_string (str->str);
1235
  setup (edl);
1236
  tot_frames = gcut_get_duration (edl);
1237 1238 1239 1240

  if (edl->range_end == 0)
    edl->range_end = tot_frames-1;
  process_frames (edl);
1241
  gcut_free (edl);
1242 1243 1244 1245 1246
  g_string_free (str, TRUE);
  return 0;
#endif
}

1247
#if HAVE_MRG
1248
int gcut_ui_main (GeglEDL *edl);
1249 1250 1251 1252
#else
int gcut_ui_main (GeglEDL *edl);
int gcut_ui_main (GeglEDL *edl)
{
1253
  fprintf (stderr, "gcut built without mrg UI\n");
1254 1255 1256
  return -1;
}
#endif
1257 1258

int gegl_make_thumb_video (GeglEDL *edl, const char *path, const char *thumb_path);
1259
void gcut_make_proxies (GeglEDL *edl)
1260 1261 1262 1263 1264 1265 1266
{
  GList *l;
  for (l = edl->clips; l; l = l->next)
  {
    Clip *clip = l->data;
    if (clip->is_chain == 0 && clip->static_source == 0 && clip->is_meta == 0)
    {
1267 1268
      char *proxy_path = gcut_make_proxy_path (edl, clip->path);
      char *thumb_path = gcut_make_thumb_path (edl, clip->path);
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
      if (!g_file_test (proxy_path, G_FILE_TEST_IS_REGULAR))
        gegl_make_thumb_video (edl, clip->path, proxy_path);
      if (!g_file_test (thumb_path, G_FILE_TEST_IS_REGULAR))
        gegl_make_thumb_image(edl, proxy_path, thumb_path);
      g_free (proxy_path);
      g_free (thumb_path);
    }
  }
}

1279
static void gcut_start_sanity (void)
1280 1281 1282 1283
{
  int fails = 0;
  if (system("which ffmpeg > /dev/null") != 0)
  {
1284
    fprintf (stderr, "gcut missing runtime dependency: ffmpeg command in PATH\n");
1285 1286 1287 1288
    fails ++;
  }
  if (!gegl_has_operation ("gegl:ff-load"))
  {
1289
    fprintf (stderr, "gcut missing runtime dependency: gegl:ff-load operation\n");
1290 1291 1292 1293
    fails ++;
  }
  if (!gegl_has_operation ("gegl:ff-save"))
  {
1294
    fprintf (stderr, "gcut missing runtime dependency: gegl:ff-save operation\n");
1295 1296 1297 1298 1299 1300 1301 1302
    fails ++;
  }
  if (fails)
    exit (-1);
}

gint iconographer_main (gint    argc, gchar **argv);

1303 1304 1305 1306
gint str_has_video_suffix (const gchar *edl_path);
gint str_has_video_suffix (const gchar *edl_path)
{
  if (g_str_has_suffix (edl_path, ".mp4") ||
1307
      g_str_has_suffix (edl_path, ".avi") ||
1308 1309
      g_str_has_suffix (edl_path, ".ogv") ||
      g_str_has_suffix (edl_path, ".mkv") ||
1310
      g_str_has_suffix (edl_path, ".webm") ||
1311 1312
      g_str_has_suffix (edl_path, ".MP4") ||
      g_str_has_suffix (edl_path, ".OGV") ||
1313 1314
      g_str_has_suffix (edl_path, ".MKV") ||
      g_str_has_suffix (edl_path, ".WEBM") ||
1315 1316 1317 1318 1319
      g_str_has_suffix (edl_path, ".AVI"))
    return 1;
  return 0;
}

1320 1321 1322 1323 1324 1325
int main (int argc, char **argv)
{
  GeglEDL *edl = NULL;
  const char *edl_path = "input.edl";
  int tot_frames;

1326 1327 1328
  gcut_binary_path = realpath (argv[0], NULL);
  if (!gcut_binary_path)
    gcut_binary_path = "gcut";
1329 1330 1331 1332 1333 1334 1335

  if (argv[1] && !strcmp (argv[1], "iconographer"))
  {
    argv[1] = argv[0];
    return iconographer_main (argc-1, argv + 1);
  }

Edward E.'s avatar
Edward E. committed
1336 1337
  g_setenv ("GEGL_USE_OPENCL", "no", 1);
  g_setenv ("GEGL_MIPMAP_RENDERING", "1", 1);
1338 1339

  init (argc, argv);
1340
  gcut_start_sanity ();
1341 1342 1343

  if (!argv[1])
  {
1344
    static char *new_argv[3]={NULL, "gcut.edl", NULL};
1345 1346 1347 1348 1349 1350 1351 1352
    new_argv[0] = argv[0];
    argv = new_argv;
    argc++;
    g_file_set_contents (argv[1], default_edl, -1, NULL);
  }

  edl_path = argv[1]; //realpath (argv[1], NULL);

1353 1354 1355 1356 1357 1358 1359 1360 1361
  if (str_has_video_suffix (edl_path))
  {
    char * path = realpath (edl_path, NULL);
    char * rpath = g_strdup_printf ("%s.edl", path);
    if (g_file_test (rpath, G_FILE_TEST_IS_REGULAR))
      edl_path = rpath;
  }

  if (str_has_video_suffix (edl_path))
1362 1363
  {
    char str[1024];
1364 1365
    int frames;
    double duration;
1366
    double fps;
1367 1368
    gchar fstr[G_ASCII_DTOSTR_BUF_SIZE];

1369 1370 1371 1372