File: gap_decode_mplayer.c

package info (click to toggle)
gimp-gap 2.6.0%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 19,656 kB
  • ctags: 7,358
  • sloc: ansic: 119,801; sh: 3,890; makefile: 932; lisp: 97; pascal: 55
file content (1696 lines) | stat: -rw-r--r-- 49,942 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
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
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
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
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
/* gap_decode_mplayer.c
 * 2004.12.06 hof (Wolfgang Hofer)
 *
 * GAP ... Gimp Animation Plugins
 *
 * This Module contains:
 *
 *        GIMP/GAP-frontend interface for mplayer
 *         Calls mplayer to split any mplayer supported video into
 *         video frames (single images on disk)
 *         Audio can also be extracted.
 *
 *        mplayer  exporting edition is available at:
 *            Web:        http://www.mplayerhq.hu/homepage/design7/news.html
 *
 * Warning: This Module needs UNIX environment to run.
 *   It uses programs and commands that are NOT available
 *   on other Operating Systems (Win95, NT, XP ...)
 *
 *     - mplayer            MPlayer 1.0pre5 or MPlayer 1.0pre7
 *                          (the best mediaplayer for linux)
 *                          set environment GAP_MPLAYER_PROG to configure where to find mplayer
 *                          (default: search mplayer in your PATH)
 *     - cd                 (UNIX command)
 *     - rm                 (UNIX command is used to delete by wildcard (expanded by /bin/sh)
 *                                and to delete a directory with all files
 */
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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.
 */

/* revision history
 * gimp-gap    2.1;     2005/05/22  hof: support MPlayer1.0pre7 (has new calling options)
 * gimp-gap    2.1;     2004/11/29  hof: created
 */


/*
 *    Here is a list of the mplayer 1.0 extracting related options
 *    that were used in this frontend.
 *    Please Note: Options changed incompatible from 1.0pre5 to 1.0pre7 release,
 *                 the old MPlayer1.0pre5 options can be selected via dialog.
 *
 *    -ss <time> (see -sb option too)
 *                  Seek to given time position.
 *
 *                  EXAMPLE:
 *                        -ss 56
 *                                seeks to 56 seconds
 *                        -ss 01:10:00
 *                                seeks to 1 hour 10 min
 *
 *    -frames <number>
 *       Play/convert only first <number> frames, then quit.
 *
 *    -vo png
 *       set video output to png device (this device saves png frames to disc)
 *    -vo jpeg
 *       set video output to jpeg device (this device saves jpeg frames to disc)
 *
 *    -jpeg <option1:option2:...> (-vo jpeg only)  # old syntax for Mplayer1.0pre5
 *            Specify options for the JPEG output.
 *            Available options are:
 *
 *                  [no]progressive
 *                          Specify standard or progressive JPEG.
 *                  [no]baseline
 *                          Specify use of baseline or not.
 *                  optimize=<value>
 *                          Optimization factor [0-100]
 *                  smooth=<value>
 *                          Smooth factor [0-100]
 *                  quality=<value>
 *                          Quality factor [0-100]
 *                  outdir=<value>
 *                          Directory to save the JPEG files
 *    -z <0-9> (-vo png only)   # old syntax for Mplayer1.0pre5
 *                  Specifies compression level for PNG output.
 *                        0    no compression
 *                        9    max compression
 *
 *    -dvdangle <angle id> (DVD only)
 *               Some DVD discs contain scenes that  can  be  viewed
 *               from  multiple  angles.   Here you can tell MPlayer
 *               which angles to use (default: 1).  Examples can  be
 *               found below.
 *
 *
 *    -ao pcm -aofile <filename>   # syntax for MPlayer 1.0pre5
 *    -ao pcm:file=<filename>      # syntax for MPlayer 1.0pre7 or latest CVS
 *       set audiooutput to pcm device (writes PCM wavefiles to disc)
 *
 *
 *    -aid <id> (also see -alang option)
 *                  Select audio channel [MPEG: 0-31 AVI/OGM: 1-99 ASF/
 *                  RM:  0-127  VOB(AC3):  128-159  VOB(LPCM):  160-191
 *                  MPEG-TS 17-8190].  MPlayer prints the available IDs
 *                  when running in verbose (-v) mode.  When playing an
 *                  MPEG-TS stream, MPlayer/Mencoder will use the first
 *                  program (if present) with the chosen audio  stream.
 *    -nosound
 *                  Do not play/encode sound.  Useful for benchmarking.
 *
 *    -novideo
 *                  Do not play/encode video.
 *
 *
 *    examples (MPlayer 1.0pre5):
 *    -----------------------
 *      mplayer  -ss 00:00:14 -frames 200 -ao pcm -aofile extracted_audio.wav  videoinput.rm
 *
 *        ==> extracts wav file named audiodump.wav
 *
 *      mplayer -vo jpeg -ss 00:00:14 -frames 150 -jpeg quality=92:smooth=70 videoinput.rm
 *
 *        ==> extracts 150 jpeg frames starting at second 13
 *
 *         00000001.jpg
 *      ..
 *      mplayer -vo png -ss 00:00:14 -frames 25 -z 9 videoinput.rm
 *
 *        ==> extracts 25 png frames in best compression starting at second 13
 *
 *         00000001.jpg
 *      ..
 *    examples (MPlayer 1.0pre7  or latest CVS):
 *    -----------------------
 *      mplayer  -ss 00:00:14 -frames 200 -ao pcm:file=extracted_audio.wav  videoinput.rm
 *
 *        ==> extracts wav file named audiodump.wav
 *
 *      mplayer -vo jpeg:quality=92:smooth=70 -ss 00:00:14 -frames 150  videoinput.rm
 *
 *        ==> extracts 150 jpeg frames starting at second 13   WARNING does not work yet
 *
 *         00000001.jpg
 *      ..
 *
 *      mplayer -vo png:z=9 -ss 00:00:14 -frames 25  videoinput.mpg
 *        ==> extracts 25 png frames in best compression starting at second 13   WARNING does not work yet
 */



/* SYTEM (UNIX) includes */
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/stat.h>
#include <unistd.h>

#include <glib/gstdio.h>

/* GIMP includes */
#include "gtk/gtk.h"
#include "config.h"
#include "gap-intl.h"
#include "libgimp/gimp.h"

#ifdef G_OS_WIN32
#include <io.h>
#  ifndef S_ISDIR
#    define S_ISDIR(m) ((m) & _S_IFDIR)
#  endif
#  ifndef S_ISREG
#    define S_ISREG(m) ((m) & _S_IFREG)
#  endif
#endif


#define MPLAYER_PROG "mplayer"



/* GAP includes */
#include "gap_libgapbase.h"
#include "gap_lib.h"
#include "gap_arr_dialog.h"
#include "gap_decode_mplayer.h"

extern      int gap_debug; /* ==0  ... dont print debug infos */


static gchar *global_errlist = NULL;

gint32  global_delete_number;
/* fileformats supported by gap_decode_mplayer */

/* ============================================================================
 * p_mplayer_info
 * ============================================================================
 * this dialog is shown when errors occurred after attempt
 * to call mplayer.
 * Provides some informations what is required,
 * and the information about error.
 */
static int
p_mplayer_info(char *errlist)
{
  GapArrArg  argv[22];
  GapArrButtonArg  b_argv[2];

  int        l_idx;
  int        l_rc;


  l_idx = 0;
  gap_arr_arg_init(&argv[l_idx], GAP_ARR_WGT_LABEL_LEFT);
  argv[l_idx].label_txt = _("Requirements to run the mplayer based video split");

  l_idx++;
  gap_arr_arg_init(&argv[l_idx], GAP_ARR_WGT_LABEL_LEFT);
  argv[l_idx].label_txt = "";

  l_idx++;
  gap_arr_arg_init(&argv[l_idx], GAP_ARR_WGT_LABEL_LEFT);
  argv[l_idx].label_txt = "1.)";

  l_idx++;
  gap_arr_arg_init(&argv[l_idx], GAP_ARR_WGT_LABEL_LEFT);
  argv[l_idx].label_txt = _("mplayer 1.0 must be installed somewhere in your PATH\n"
                            "you can get mplayer exporting edition at:\n"
                           );

  l_idx++;
  gap_arr_arg_init(&argv[l_idx], GAP_ARR_WGT_LABEL_LEFT);
  argv[l_idx].label_txt = "http://www.mplayerhq.hu";


  l_idx++;
  gap_arr_arg_init(&argv[l_idx], GAP_ARR_WGT_LABEL_LEFT);
  argv[l_idx].label_txt = "";

  l_idx++;
  gap_arr_arg_init(&argv[l_idx], GAP_ARR_WGT_LABEL_LEFT);
  argv[l_idx].label_txt = "2.)";

  l_idx++;
  gap_arr_arg_init(&argv[l_idx], GAP_ARR_WGT_LABEL_LEFT);
  argv[l_idx].label_txt = _("if your mplayer is not in your PATH or is not named mplayer\n"
                            "you have to set environment variable GAP_MPLAYER_PROG\n"
                            "to your mplayer program and restart gimp"
                           );

  l_idx++;
  gap_arr_arg_init(&argv[l_idx], GAP_ARR_WGT_LABEL_LEFT);
  argv[l_idx].label_txt = "";

  l_idx++;
  gap_arr_arg_init(&argv[l_idx], GAP_ARR_WGT_LABEL_LEFT);
  argv[l_idx].label_txt = _("An error occurred while trying to call mplayer:");

  l_idx++;
  gap_arr_arg_init(&argv[l_idx], GAP_ARR_WGT_LABEL_LEFT);
  argv[l_idx].label_txt = "--------------------------------------------";


  l_idx++;
  gap_arr_arg_init(&argv[l_idx], GAP_ARR_WGT_LABEL_LEFT);
  argv[l_idx].label_txt = errlist;


  l_idx++;

  /* the  Action Button */
    b_argv[0].but_txt  = GTK_STOCK_CANCEL;
    b_argv[0].but_val  = -1;
    b_argv[1].but_txt  = GTK_STOCK_OK;
    b_argv[1].but_val  = 0;

  l_rc = gap_arr_std_dialog(_("mplayer Information"),
                             "",
                              l_idx,   argv,      /* widget array */
                              1,       b_argv,    /* button array */
                              -1);

  return (l_rc);
}       /* end p_mplayer_info */


/* -----------------------
 * p_scann_start_time
 * -----------------------
 * scann hour, minute, second from a string
 * that has the format "HH:MM:SS"
 * where any other non-digit characters are treated same as ":"
 * strings with format "HHMMSS" are also accepted.
 *
 * This procedure does not check if MM and SS values are in the range
 * 00 upto 59
 */
static void
p_scann_start_time(char *buf, GapMPlayerParams *gpp)
{
  gint ii;
  gint tab_idx;
  gint num;
  gint factor;
  gint tab[3];
  gboolean num_ready;


  tab[0] = 0;
  tab[1] = 0;
  tab[2] = 0;

  tab_idx = 0;
  num = 0;
  factor = 1;
  num_ready = FALSE;

  ii = strlen(buf);
  while(TRUE)
  {
    ii--;

    if((buf[ii] >= '0') && (buf[ii] <= '9'))
    {
      num += ((buf[ii] - (gint)'0') * factor);
      factor = factor * 10;
      if((factor > 10)  /* 2 digits done ? */
      || (ii==0))       /* last character handled ? */
      {
        num_ready = TRUE;
      }
    }
    else
    {
      /* current char is no digit */
      if(factor > 1)
      {
        /* at least one digit was collected in num */
        num_ready = TRUE;
      }
    }

    if(num_ready)
    {
      if(tab_idx < 3)
      {
        tab[tab_idx] = num;
        tab_idx++;
      }
      num = 0;
      factor = 1;
      num_ready = FALSE;
    }

    if(ii <= 0)
    {
      break;
    }
  }

  gpp->start_hour   = tab[2];
  gpp->start_minute = tab[1];
  gpp->start_second = tab[0];

}  /* end p_scann_start_time */


/* -----------------------
 * p_mplayer_dialog
 * -----------------------
 * the main dialog for the mplayer based video extract settings
 */
static int
p_mplayer_dialog   (GapMPlayerParams *gpp)
{
#define MPDIALOG_SMALL_ENTRY_WIDTH 80
#define MPDIALOG_LARGE_ENTRY_WIDTH 250
#define MPDIALOG_NUM_ARGS 22
  static GapArrArg  argv[MPDIALOG_NUM_ARGS];
  static char *radio_args[3]  = { "XCF", "PNG", "JPEG" };

  char buf_start_time[10];
  char err_msg_buffer[1000];

  gint ii;
  gint ii_num_frames;
  gint ii_vtrack;
  gint ii_atrack;
  gint ii_img_format;
  gint ii_png_compression;
  gint ii_jpg_quality;
  gint ii_jpg_optimize;
  gint ii_jpg_smooth;
  gint ii_jpg_progressive;
  gint ii_jpg_baseline;
  gint ii_autoload;
  gint ii_silent;
  gint ii_async;
  gint ii_old_syntax;

  err_msg_buffer[0] = '\0';
  g_snprintf(buf_start_time, sizeof(buf_start_time), "%02d:%02d:%02d"
     ,(int)gpp->start_hour
     ,(int)gpp->start_minute
     ,(int)gpp->start_second
     );


  /* the loop shows the dialog again until
   * - parameters are OK (valid starttime, videofile exists)
   * - CANCEL button is pressed
   */
  while(TRUE)
  {

  ii=0;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_FILESEL);
  argv[ii].label_txt = _("Input Video:");
  argv[ii].help_txt  = _("Name of a videofile to read by mplayer. "
                        "Frames are extracted from the videofile "
                        "and written to separate diskfiles. "
                        "mplayer 1.0 is required.");
  argv[ii].text_buf_len = sizeof(gpp->video_filename);
  argv[ii].text_buf_ret = gpp->video_filename;
  argv[ii].entry_width = MPDIALOG_LARGE_ENTRY_WIDTH;

  if(err_msg_buffer[0] != '\0')
  {
    ii++;
    gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_LABEL_LEFT);
    argv[ii].label_txt = err_msg_buffer;
  }

  ii++;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_TEXT);
  argv[ii].label_txt = _("Start Time:");
  argv[ii].help_txt  = _("Extracting starts at the specified time offset HH:MM:SS in the video");
  argv[ii].text_buf_len = sizeof(buf_start_time);
  argv[ii].text_buf_ret = buf_start_time;
  argv[ii].entry_width = MPDIALOG_SMALL_ENTRY_WIDTH;



  ii++; ii_num_frames = ii;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_INT);
  argv[ii].label_txt = _("Frames:");
  argv[ii].help_txt  = _("Number of frames to extract");
  argv[ii].constraint = TRUE;
  argv[ii].int_min    = 1;
  argv[ii].int_max    = 999999;
  argv[ii].int_ret    = gpp->number_of_frames;
  argv[ii].umin       = 1;
  argv[ii].umax       = 999999;
  argv[ii].entry_width = MPDIALOG_SMALL_ENTRY_WIDTH;

  ii++; ii_vtrack = ii;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_INT);
  argv[ii].label_txt = _("Videotrack:");
  argv[ii].help_txt  = _("Number of videotrack to extract. (0 == ignore video)");
  argv[ii].constraint = TRUE;
  argv[ii].int_min    = 0;
  argv[ii].int_max    = 20;
  argv[ii].int_ret    = gpp->vtrack;
  argv[ii].umin       = 0;
  argv[ii].umax       = 20;
  argv[ii].entry_width = MPDIALOG_SMALL_ENTRY_WIDTH;

  ii++; ii_atrack = ii;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_INT);
  argv[ii].label_txt = _("Audiotrack:");
  argv[ii].help_txt  = _("Number of audiotrack to extract. (0 == ignore audio)");
  argv[ii].constraint = TRUE;
  argv[ii].int_min    = 0;
  argv[ii].int_max    = 20;
  argv[ii].int_ret    = gpp->atrack;
  argv[ii].umin       = 0;
  argv[ii].umax       = 20;
  argv[ii].entry_width = MPDIALOG_SMALL_ENTRY_WIDTH;


  ii++;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_FILESEL);
  argv[ii].label_txt = _("Output Audio:");
  argv[ii].help_txt  = _("Filename for the extracted audiodata. "
                        "Audiodata is written in RIFF WAV fileformat "
                        "(but only if audiotrack >= 1)");
  argv[ii].text_buf_len = sizeof(gpp->audio_filename);
  argv[ii].text_buf_ret = gpp->audio_filename;
  argv[ii].entry_width = MPDIALOG_LARGE_ENTRY_WIDTH;

  ii++;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_FILESEL);
  argv[ii].label_txt = _("Framenames:");
  argv[ii].help_txt  = _("Basename for the video frames to write on disk. "
                        "Framenumber and extension is added.");
  argv[ii].text_buf_len = sizeof(gpp->basename);
  argv[ii].text_buf_ret = gpp->basename;
  argv[ii].entry_width = MPDIALOG_LARGE_ENTRY_WIDTH;


  ii++; ii_img_format = ii;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_OPTIONMENU);
  argv[ii].label_txt = _("Format:");
  argv[ii].help_txt  = _("Image fileformat for the extracted video frames. "
                       "(xcf is extracted as png and converted to xcf)");
  argv[ii].radio_argc  = 3;
  argv[ii].radio_argv = radio_args;
  argv[ii].radio_ret  = gpp->img_format;


  ii++; ii_png_compression = ii;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_INT);
  argv[ii].label_txt = _("Png Compression:");
  argv[ii].help_txt  = _("Compression for resulting png frames where 0 is uncompressed (fast), 9 is max. compression "
                        "(this option is ignored when JPEG format is used)");
  argv[ii].constraint = TRUE;
  argv[ii].int_min    = 0;
  argv[ii].int_max    = 9;
  argv[ii].int_ret    = gpp->png_compression;

  ii++; ii_jpg_quality = ii;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_INT);
  argv[ii].label_txt = _("Jpeg Quality:");
  argv[ii].help_txt  = _("Quality for resulting jpeg frames where 100 is best quality "
                        "(ignored when other formats are used)");
  argv[ii].constraint = TRUE;
  argv[ii].int_min    = 0;
  argv[ii].int_max    = 100;
  argv[ii].int_ret    = gpp->jpg_quality;


  ii++; ii_jpg_optimize = ii;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_INT);
  argv[ii].label_txt = _("Jpeg Optimize:");
  argv[ii].help_txt  = _("optimization factor"
                        "(is ignored when other formats are used)");
  argv[ii].constraint = TRUE;
  argv[ii].int_min    = 0;
  argv[ii].int_max    = 100;
  argv[ii].int_ret    = gpp->jpg_optimize;


  ii++; ii_jpg_smooth = ii;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_INT);
  argv[ii].label_txt = _("Jpeg Smooth:");
  argv[ii].help_txt  = _("Smooth factor"
                        "(is ignored when other formats are used)");
  argv[ii].constraint = TRUE;
  argv[ii].int_min    = 0;
  argv[ii].int_max    = 100;
  argv[ii].int_ret    = gpp->jpg_smooth;


  ii++; ii_jpg_progressive = ii;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_TOGGLE);
  argv[ii].label_txt = _("Jpeg Progressive:");
  argv[ii].help_txt  = _("Enable progressive jpeg encoding"
                        "(is ignored when other formats are used)");
  argv[ii].int_ret   = gpp->jpg_progressive;

  ii++; ii_jpg_baseline = ii;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_TOGGLE);
  argv[ii].label_txt = _("Jpeg Baseline:");
  argv[ii].help_txt  = _("Enable baseline jpeg encoding"
                        "(is ignored when other formats are used)");
  argv[ii].int_ret   = gpp->jpg_baseline;

  ii++;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_LABEL);
  argv[ii].label_txt = "";


  ii++; ii_silent = ii;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_TOGGLE);
  argv[ii].label_txt = _("Silent");
  argv[ii].help_txt  = _("use -nosound (-novideo) in case audiotrack (videotrack) is 0.\n"
                         "mplayer will operate silent and faster.");
  argv[ii].int_ret   = gpp->silent;

  ii++; ii_autoload = ii;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_TOGGLE);
  argv[ii].label_txt = _("Open");
  argv[ii].help_txt  = _("Open the 1st one of the extracted frames");
  argv[ii].int_ret   = gpp->autoload;

  ii++; ii_async = ii;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_TOGGLE);
  argv[ii].label_txt = _("Asynchronous");
  argv[ii].help_txt  = _("Run the mplayer as asynchronous process");
  argv[ii].int_ret   = gpp->run_mplayer_asynchron;


  ii++; ii_old_syntax = ii;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_TOGGLE);
  argv[ii].label_txt = _("MPlayer 1.0pre5:");
  argv[ii].help_txt  = _("ON: use deprecated options for mplayer 1.0pre5\n"
                         "OFF: use options for newer mplayer\n"
                        " Dont turn on the deprecated options if you have mplayer 1.0pre7 or newer mplayer versions");
  argv[ii].int_ret   = gpp->use_old_mplayer1_syntax;

  ii++;
  gap_arr_arg_init(&argv[ii], GAP_ARR_WGT_HELP_BUTTON);
  argv[ii].help_id = GAP_MPLAYER_HELP_ID;

  ii++;

  if(TRUE == gap_arr_ok_cancel_dialog(_("MPlayer based extraction"),
                            _("Select Frame Range"), ii, argv))
  {
     gboolean params_ok;
     p_scann_start_time(buf_start_time, gpp);

     if(gap_debug)
     {
       printf("# gpp->start_hour:   %d\n", (int)gpp->start_hour );
       printf("# gpp->start_minute: %d\n", (int)gpp->start_minute );
       printf("# gpp->start_second: %d\n", (int)gpp->start_second );
     }

     gpp->number_of_frames  = (gint32)(argv[ii_num_frames].int_ret);
     gpp->vtrack            = (gint32)(argv[ii_vtrack].int_ret);
     gpp->atrack            = (gint32)(argv[ii_atrack].int_ret);
     gpp->png_compression   = (gint32)(argv[ii_png_compression].int_ret);
     gpp->jpg_quality       = (gint32)(argv[ii_jpg_quality].int_ret);
     gpp->jpg_optimize      = (gint32)(argv[ii_jpg_optimize].int_ret);
     gpp->jpg_smooth        = (gint32)(argv[ii_jpg_smooth].int_ret);
     gpp->jpg_progressive   = (gint32)(argv[ii_jpg_progressive].int_ret);
     gpp->jpg_baseline      = (gint32)(argv[ii_jpg_baseline].int_ret);

     gpp->img_format        = (GapMPlayerFormats)(argv[ii_img_format].radio_ret);
     gpp->silent            = (gboolean)(argv[ii_silent].int_ret);
     gpp->autoload          = (gboolean)(argv[ii_autoload].int_ret);
     gpp->run_mplayer_asynchron  = (gboolean)(argv[ii_async].int_ret);
     gpp->use_old_mplayer1_syntax  = (gboolean)(argv[ii_old_syntax].int_ret);

     params_ok = TRUE;
     if(!g_file_test(gpp->video_filename, G_FILE_TEST_EXISTS))
     {
       params_ok = FALSE;
       g_snprintf(err_msg_buffer, sizeof(err_msg_buffer)
                 , _("videofile %s not existent\n")
                 , gpp->video_filename
                 );
     }
     if((gpp->start_minute > 59)
     || (gpp->start_second > 59))
     {
       params_ok = FALSE;
       g_snprintf(err_msg_buffer, sizeof(err_msg_buffer)
                 ,_("Illegal starttime %s")
                 , buf_start_time
                 );
     }

     if(params_ok)
     {
       return (0);    /* OK */
     }
  }
  else
  {
     return -1;     /* Cancel */
  }

  }
}       /* end p_mplayer_dialog */


/* ------------------------------
 * p_overwrite_dialog
 * ------------------------------
 */
static gint
p_overwrite_dialog(char *filename, gint overwrite_mode)
{
  static  GapArrButtonArg  l_argv[3];
  static  GapArrArg  argv[1];

  if(g_file_test(filename, G_FILE_TEST_EXISTS))
  {
    if (overwrite_mode < 1)
    {
       l_argv[0].but_txt  = _("Overwrite Frame");
       l_argv[0].but_val  = 0;
       l_argv[1].but_txt  = _("Overwrite All");
       l_argv[1].but_val  = 1;
       l_argv[2].but_txt  = GTK_STOCK_CANCEL;
       l_argv[2].but_val  = -1;

       gap_arr_arg_init(&argv[0], GAP_ARR_WGT_LABEL);
       argv[0].label_txt = filename;

       return(gap_arr_std_dialog ( _("GAP Question"),
                                   _("File already exists"),
                                   1, argv,
                                   3, l_argv, -1));
    }
  }
  return (overwrite_mode);
}  /* end p_overwrite_dialog */



/* ------------------------------
 * p_build_mplayer_framename
 * ------------------------------
 * MPlayer extracted frames are named with an 8digit number and extension
 * and created in the current directory.
 * (for the mplayer call the current directory is set to gpp->mplayer_working_dir)
 * example
 *   00000001.png
 *   00000002.png
 *   ...
 */
static void
p_build_mplayer_framename(GapMPlayerParams *gpp, char *framename, gint32 sizeof_framename, gint32 frame_nr, char *ext)
{
   g_snprintf(framename, sizeof_framename, "%s%s%08d.%s"
              , gpp->mplayer_working_dir
              , G_DIR_SEPARATOR_S
              , (int)frame_nr
              ,ext
              );
}

/* ------------------------------
 * p_build_gap_framename
 * ------------------------------
 */
static void
p_build_gap_framename(char *framename, gint32 sizeof_framename, gint32 frame_nr, char *basename, char *ext)
{
   g_snprintf(framename, sizeof_framename, "%s%06d.%s", basename, (int)frame_nr, ext);
}


/* ------------------------------
 * p_dirname
 * ------------------------------
 */
static void
p_dirname(char *fname)
{
  int l_idx;

  l_idx = strlen(fname) -1;
  while(l_idx > 0)
  {
     if(fname[l_idx] == G_DIR_SEPARATOR)
     {
        fname[l_idx] = '\0';
        return;
     }
     l_idx--;
  }
  *fname = '\0';
}  /* end p_dirname */


/* ------------------------------
 * p_init_mplayer_working_dir
 * ------------------------------
 * build the name for a temporary directory
 * where mplayer will run and writes the extracted frames.
 *
 * the first guess is making a subdirectory at same location
 * where the resulting frames will be stored.
 * (has the advantage that fast renaming is possible
 *  on the same device)
 *
 * the 2nd gues uses a subdirectory loacted at the users home directory.
 */
static void
p_init_mplayer_working_dir(GapMPlayerParams *gpp)
{
  gchar        tmp_dir[200];
  gchar       *absolute;
  pid_t        l_pid;


  /* include process id of the current process in the temp dir name */
  l_pid = gap_base_getpid();
  g_snprintf(tmp_dir, sizeof(tmp_dir), "tmp_mplayer_frames.%d", (int)l_pid);

  if((gpp->basename[0] != '\0')
  && (gpp->vtrack > 0))
  {
    if (! g_path_is_absolute (gpp->basename))
    {
        gchar *current;

        current = g_get_current_dir ();
        absolute = g_build_filename (current, gpp->basename, NULL);
        g_free (current);
    }
    else
    {
        absolute = g_strdup (gpp->basename);
    }

    p_dirname(absolute);

    /* if destination directorypart does not exist, try to create it */
    if (! g_file_test(absolute, G_FILE_TEST_IS_DIR))
    {
       gap_file_mkdir (absolute, GAP_FILE_MKDIR_MODE);
    }

    gpp->mplayer_working_dir = g_build_filename(absolute, tmp_dir, NULL);

    g_free (absolute);

    return;
  }

  /* use gimp_temp_name to find a writeable tempdir
   * (used as mplayer workingdir)
   */

  gpp->mplayer_working_dir = g_build_filename(g_get_home_dir(), tmp_dir, NULL);

}  /* end p_init_mplayer_working_dir */

/* -----------------------
 * p_convert_frames
 * -----------------------
 * this step converts the frames that were extracted by mplayer (from png)
 * to another fileformat (to xcf)
 * the destination frames also get the desired naming.
 */
static int
p_convert_frames(GapMPlayerParams *gpp, gint32 frame_from, gint32 frame_to, char *basename, char *ext, char *ext2)
{
   GimpParam          *return_vals;
   int              nreturn_vals;
   gint32           l_tmp_image_id;
   char             l_first_mp_frame[500];
   int              l_rc;

   l_rc = -1;

  /* load 1st one of those frames generated by mplayer  */
   p_build_mplayer_framename(gpp, l_first_mp_frame, sizeof(l_first_mp_frame), frame_from, ext);
   l_tmp_image_id = gap_lib_load_image(l_first_mp_frame);

   /* convert the mplayer frames (from png) to xcf fileformat
    * (the gap module for range convert is not linked to the frontends
    *  main program, therefore i call the convert procedure via PDB-interface)
    */
   return_vals = gimp_run_procedure ("plug_in_gap_range_convert2",
                                    &nreturn_vals,
                                    GIMP_PDB_INT32, GIMP_RUN_NONINTERACTIVE,     /* runmode  */
                                    GIMP_PDB_IMAGE, l_tmp_image_id,
                                    GIMP_PDB_DRAWABLE, 0,         /* (unused)  */
                                    GIMP_PDB_INT32, frame_from,
                                    GIMP_PDB_INT32, frame_to,
                                    GIMP_PDB_INT32, 0,            /* dont flatten */
                                    GIMP_PDB_INT32, 4444,         /* dest type (keep type) */
                                    GIMP_PDB_INT32, 256,          /* colors (unused)  */
                                    GIMP_PDB_INT32, 0,            /* no dither (unused)  */
                                    GIMP_PDB_STRING, ext2,        /* extension for dest. filetype  */
                                    GIMP_PDB_STRING, basename,    /* basename for dest. filetype  */
                                    GIMP_PDB_INT32, 0,            /* (unused)  */
                                    GIMP_PDB_INT32, 0,            /* (unused)  */
                                    GIMP_PDB_INT32, 0,            /* (unused)  */
                                    GIMP_PDB_STRING, "none",      /* (unused) palettename */
                                    GIMP_PDB_END);

   /* destroy the tmp image */
   gimp_image_delete(l_tmp_image_id);

   if (return_vals[0].data.d_status == GIMP_PDB_SUCCESS)
   {
      l_rc = 0;   /* OK */
   }
   gimp_destroy_params (return_vals, nreturn_vals);

   return(l_rc);
}

/* ---------------------------
 * p_find_max_mplayer_frame
 * ---------------------------
 * check the names of the extracted frames
 * and search for the max framenumber.
 */
static gint32
p_find_max_mplayer_frame(GapMPlayerParams *gpp, gint32 from_nr, char *ext)
{
  gint32 l_high;
  gint32 l_max_found;
  gint32 l_nr;
  gint32 l_delta;
  char   l_frame[500];

  l_nr = from_nr;
  l_max_found = 0;
  l_high = 99999999;

  while(1 == 1)
  {
     p_build_mplayer_framename(gpp, l_frame, sizeof(l_frame), l_nr, ext);

     if(gap_debug) printf("DEBUG find_MAX :%s\n", l_frame);

     if(g_file_test(l_frame, G_FILE_TEST_EXISTS))
     {
        l_max_found = l_nr;
        l_delta = (l_high - l_nr) / 2;
        if(l_delta == 0)
        {
           l_delta = 1;
        }
        l_nr = l_max_found + l_delta;
     }
     else
     {
        if(l_nr == from_nr) { return (-1); }  /* no frames found */

        if(l_nr < l_high)
        {
           l_high = l_nr;
           l_nr = l_max_found + 1;
        }
        else
        {
           return(l_max_found);
        }
     }
  }
}       /* end p_find_max_mplayer_frame */

/* ---------------------
 * p_rename_frames
 * ---------------------
 * rename the extracted frames (they are locatred in a temp directory)
 * to the required name.
 * If the destination is on the same device, this will be done
 * fast via renaming.
 * If renaming is not possible (ondifferent devices)
 * the frames will be moved via  copy / remove sequence.
 */
static int
p_rename_frames(GapMPlayerParams *gpp, gint32 frame_from, gint32 frame_to, char *basename, char *ext)
{
  gint32 l_use_mv;
  gint32 l_frame_nr;
  gint32 l_max_found;
  char   l_src_frame[500];
  char   l_dst_frame[500];
  gint    l_overwrite_mode;


  if(gap_debug) printf("p_rename_frames:\n");
  l_use_mv = TRUE;
  l_overwrite_mode = 0;


  l_max_found = p_find_max_mplayer_frame (gpp, frame_from, ext);
  if(l_max_found < 0)
  {
       global_errlist = g_strdup_printf(
               _("can't find any extracted frames,\n%s\nmaybe mplayer has failed or was cancelled"),
               l_src_frame);
       return(-1);
  }


  l_frame_nr = frame_from;

  while (l_frame_nr <= frame_to)
  {
     p_build_mplayer_framename(gpp, l_src_frame, sizeof(l_src_frame), l_frame_nr, ext);
     p_build_gap_framename(l_dst_frame, sizeof(l_dst_frame), l_frame_nr, basename, ext);

     if(!g_file_test(l_src_frame, G_FILE_TEST_EXISTS))
     {
        break;  /* srcfile not found, stop */
     }

     if (strcmp(l_src_frame, l_dst_frame) != 0)
     {
        /* check overwrite if Destination frame already exsts */
        l_overwrite_mode = p_overwrite_dialog(l_dst_frame, l_overwrite_mode);
        if (l_overwrite_mode < 0)
        {
             global_errlist = g_strdup_printf(
                     _("frames are not extracted, because overwrite of %s was cancelled"),
                     l_dst_frame);
             return(-1);
        }
        else
        {
           g_remove(l_dst_frame);
           if(g_file_test(l_dst_frame, G_FILE_TEST_EXISTS))
           {
             global_errlist = g_strdup_printf(
                     _("failed to overwrite %s (check permissions ?)"),
                     l_dst_frame);
             return(-1);
           }
        }

        if (l_use_mv)
        {
           g_rename(l_src_frame, l_dst_frame);
        }

        if(!g_file_test(l_dst_frame, G_FILE_TEST_EXISTS))
        {
           gap_lib_file_copy(l_src_frame, l_dst_frame);
           if(g_file_test(l_dst_frame, G_FILE_TEST_EXISTS))
           {
              l_use_mv = FALSE; /* if destination is on another device use copy-remove strategy */
              g_remove(l_src_frame);
           }
           else
           {
              global_errlist = g_strdup_printf(
                     _("failed to write %s (check permissions ?)"),
                     l_dst_frame);
              return(-1);
           }
        }
     }
     l_frame_nr++;
     if(l_max_found > 0) gimp_progress_update ((gdouble)l_frame_nr / (gdouble)l_max_found);
  }
  return(0);
}       /* end p_rename_frames */


/* -----------------------------
 * p_init_and_check_mplayer
 * -----------------------------
 */
static gint
p_init_and_check_mplayer(GapMPlayerParams *gpp)
{
  gint l_rc;
  const char *cp;
  gboolean mplayer_prog_found;

  if (gap_debug) printf("p_init_and_check_mplayer\n");

  mplayer_prog_found = FALSE;

  l_rc = 0;  /* SUCCESS */

  /*
   * Set environmental values for the mplayer program:
   */

  /* check gimprc for the mplayer executable name (including path) */
  if ( (cp = gimp_gimprc_query("mplayer_prog")) != NULL )
  {
    gpp->mplayer_prog = g_strdup(cp);           /* Environment overrides compiled in default for WAVPLAYPATH */
    if(g_file_test (gpp->mplayer_prog, G_FILE_TEST_IS_EXECUTABLE) )
    {
      mplayer_prog_found = TRUE;
    }
    else
    {
      g_message(_("WARNING: your gimprc file configuration for the mediaplayer\n"
             "does not point to an executable program;\n"
             "the configured value for %s is: %s\n")
             , "mplayer_prog"
             , gpp->mplayer_prog
             );
    }
  }

  /* check environment variable for the mplayer program */
  if(!mplayer_prog_found)
  {
    if ( (cp = g_getenv("GAP_MPLAYER_PROG")) != NULL )
    {
      gpp->mplayer_prog = g_strdup(cp);         /* Environment overrides compiled in default for WAVPLAYPATH */
      if(g_file_test (gpp->mplayer_prog, G_FILE_TEST_IS_EXECUTABLE) )
      {
        mplayer_prog_found = TRUE;
      }
      else
      {
        g_message(_("WARNING: the environment variable %s\n"
               "does not point to an executable program;\n"
               "the current value is: %s\n")
               , "GAP_MPLAYER_PROG"
               , gpp->mplayer_prog
               );
      }
    }
  }

  if(!mplayer_prog_found)
  {
    if ( (cp = g_getenv("PATH")) != NULL )
    {
      gpp->mplayer_prog = gap_lib_searchpath_for_exefile(MPLAYER_PROG, cp);
      if(gpp->mplayer_prog)
      {
        mplayer_prog_found = TRUE;
      }
    }
  }


  if(!mplayer_prog_found)
  {
    l_rc = 10;  /* ERROR */
    global_errlist = g_strdup_printf(_("The mediaplayer executable file '%s' was not found.")
                 , MPLAYER_PROG
                 );
  }

  return (l_rc);
}  /* end p_init_and_check_mplayer */


/* -----------------------
 * p_poll
 * -----------------------
 * poll until asynchron mplayer process exits
 * and do checks for already extracted frames to show corresponding progress
 * (this assumes that the video contains all the request frames
 * otherwise the progress will be not correct)
 */
static void
p_poll(GapMPlayerParams *gpp, pid_t mplayer_pid, char *ext)
{
  gint32 l_max_frame;
  /* loop as long as the mplayer Process is alive */

  if(gap_debug) printf("poll started on mplayer pid: %d\n", (int)mplayer_pid);


  while (gap_base_is_pid_alive(mplayer_pid))
  {
    usleep(100000);  /* sleep 1 second, and let mplayer write some frames */

    /* check for the max_frame number out of the already extracted frames */
    l_max_frame = p_find_max_mplayer_frame (gpp, 1, ext);

    if(gap_debug)
    {
      printf("POLL: frames found: %d\n", (int)l_max_frame);
    }

    if(l_max_frame > 0)
    {
      gimp_progress_update ((gdouble)l_max_frame / (gdouble)gpp->number_of_frames);
    }
  }

  if(gap_debug) printf("poll ended on mplayer pid: %d\n", (int)mplayer_pid);
}       /* end p_poll */


/* -----------------------
 * p_start_mplayer_process
 * -----------------------
 *
 * build a cmd string
 *   "cd <dir>; mplayer <options> video_filename"
 * and run this string SYNCHRON as system command.
 * or asynchron (using a generated shellscript)
 */
static pid_t
p_start_mplayer_process(GapMPlayerParams *gpp)
{
   gchar  l_cmd[2500];
   gchar  l_buf[500];
   int    l_rc;
   pid_t  l_mplayer_pid;

   l_mplayer_pid = -1;

   if((gpp->vtrack > 0)
   && (!gpp->run_mplayer_asynchron))
   {
     /* use the working dir in case where frames should be extracted
      * (for asynchron process the cd is done later in a generated shellscript)
      */
     g_snprintf(l_buf, sizeof(l_buf), "cd \"%s\"; "
             , gpp->mplayer_working_dir
             );
   }
   else
   {
     l_buf[0] = '\0';
   }

   /* prepare args for the mplayer call */
   g_snprintf(l_cmd, sizeof(l_cmd), "%s%s -ss %02d:%02d:%02d -frames %d "
             , l_buf                            /* optional cd   gpp->mplayer_working_dir */
             , gpp->mplayer_prog                /* programname */
             , (int)gpp->start_hour
             , (int)gpp->start_minute
             , (int)gpp->start_second
             , (int)gpp->number_of_frames
             );

   if (gpp->atrack > 0)
   {
     if(gpp->atrack > 1)
     {
       /* select a non-default audiotrack (such as multilingual dvd stuff) */
       g_snprintf(l_buf, sizeof(l_buf), "-aid %d "
                    ,(int)gpp->atrack
                    );
       strcat(l_cmd, l_buf);
     }

     /* the -ao option selects the audio output device.
      * device pcm writes pcm encoded audiodata
      * to RIFF wave formated audiofiles on disc.
      * the -aofile option defines the filename for the wav file.
      */
     if(gpp->use_old_mplayer1_syntax)
     {
       strcat(l_cmd, "-ao pcm -aofile '");  // deprecated syntax
     }
     else
     {
       strcat(l_cmd, "-ao pcm:file='");
     }

     if(gpp->audio_filename[0] == '\0')
     {
       strcat(l_cmd, gpp->video_filename);
       strcat(l_cmd, ".wav");
     }
     else
     {
       strcat(l_cmd, gpp->audio_filename);
     }
     strcat(l_cmd, "' ");
   }
   else
   {
     if(gpp->silent)
     {
       strcat(l_cmd, "-nosound ");
     }
   }

   if (gpp->vtrack)
   {
     if(gpp->vtrack > 1)
     {
       /* select a non-default videotrack (usually another DVD angle) */
       g_snprintf(l_buf, sizeof(l_buf), "-dvdangle %d "
                    ,(int)gpp->vtrack
                    );
       strcat(l_cmd, l_buf);
     }

     /* the -vo option selects the video output device
      * devices jpeg and png are used to write frames to disc
      * in the corresponding image fileformat.
      */
     strcat(l_cmd, "-vo ");

     switch(gpp->img_format)
     {
       case MPENC_JPEG:
          if(gpp->use_old_mplayer1_syntax)
          {
            g_snprintf(l_buf, sizeof(l_buf), "jpeg -jpeg quality=%d:optimize=%d:smooth=%d"
                    ,(int)gpp->jpg_quality
                    ,(int)gpp->jpg_optimize
                    ,(int)gpp->jpg_smooth
                    );
          }
          else
          {
            g_snprintf(l_buf, sizeof(l_buf), "jpeg:quality=%d:optimize=%d:smooth=%d"
                    ,(int)gpp->jpg_quality
                    ,(int)gpp->jpg_optimize
                    ,(int)gpp->jpg_smooth
                    );
          }

          strcat(l_cmd, l_buf);

          if(gpp->jpg_progressive)
          {
            strcat(l_cmd, ":progressive");
          }
          else
          {
            strcat(l_cmd, ":noprogressive");
          }
          if(gpp->jpg_baseline)
          {
            strcat(l_cmd, ":baseline");
          }
          else
          {
            strcat(l_cmd, ":nobaseline");
          }
          strcat(l_cmd, " ");
          break;
       case MPENC_PNG:
       default:
          if(gpp->use_old_mplayer1_syntax)
          {
            g_snprintf(l_buf, sizeof(l_buf), "png -z %d"
                    ,(int)gpp->png_compression
                    );
          }
          else
          {
            g_snprintf(l_buf, sizeof(l_buf), "png:z=%d"
                    ,(int)gpp->png_compression
                    );
          }
          strcat(l_cmd, l_buf);   /* other formats extract as png,
                                   * and may need further processing for convert
                                   */
          break;
      }

   }
   else
   {
     if(gpp->silent)
     {
       strcat(l_cmd, "-novideo ");
     }
   }

   /* add the videofilename (in quotes to protect blanks) as last parameter */
   strcat(l_cmd, " \"");
   strcat(l_cmd, gpp->video_filename);
   strcat(l_cmd, "\"");



   /* ============= START ================= */
   if (gpp->run_mplayer_asynchron)
   {
     gchar *l_mplayer_startscript;
     gchar *l_mplayer_pidfile;
     FILE  *l_fp;

     l_mplayer_startscript = gimp_temp_name(".mplayer_start.sh");
     l_mplayer_pidfile = gimp_temp_name(".mplayer_pidfile.txt");

     /* asynchron start */
     g_remove(l_mplayer_pidfile);

     /* generate a shellscript */
     l_fp = g_fopen(l_mplayer_startscript, "w+");
     if (l_fp != NULL)
     {
         fprintf(l_fp, "#!/bin/sh\n");
         fprintf(l_fp, "CURR_DIR=`pwd`\n");
         if(gpp->vtrack > 0)
         {
           fprintf(l_fp, "cd \"%s\"\n", gpp->mplayer_working_dir);
         }
         fprintf(l_fp, "%s &\n"
                       , l_cmd                 /* start mplayer as background process */
                 );
         fprintf(l_fp, "MPLAYER_PID=$!\n");
         fprintf(l_fp, "cd \"$CURR_DIR\"\n");
         fprintf(l_fp, "echo \"$MPLAYER_PID # MPLAYER_PID\"\n");
         fprintf(l_fp, "echo \"$MPLAYER_PID # MPLAYER_PID\" > \"%s\"\n", l_mplayer_pidfile);

         /* we pass the mplayer pid in a file,
          * exitcodes are truncated to 8 bit
          * by the system call
          */
         /* fprintf(l_fp, "exit $MPLAYER_PID\n"); */
         fclose(l_fp);

         /* set execute permissions for the generated shellscript */
         gap_file_chmod(l_mplayer_startscript, GAP_FILE_MKDIR_MODE);
     }

     /* START the generated shellscrit */
     l_rc = system(l_mplayer_startscript);

     l_fp = g_fopen(l_mplayer_pidfile, "r");
     if (l_fp != NULL)
     {
        fscanf(l_fp, "%d", &l_rc);
        fclose(l_fp);
        l_mplayer_pid = (pid_t)l_rc;
     }

     g_remove(l_mplayer_startscript);
     g_remove(l_mplayer_pidfile);
     g_free(l_mplayer_startscript);
     g_free(l_mplayer_pidfile);

     if(gap_debug)
     {
       printf("ASYNCHRON CALL: %s\nl_mplayer_pid:%d\n", l_cmd, (int)l_mplayer_pid);
     }
   }
   else
   {
     /* synchron start (blocks until mplayer process has finished */
     l_rc = system(l_cmd);
     if ((l_rc & 0xff) == 0) l_mplayer_pid = 0;    /* OK */
     else                    l_mplayer_pid = -1;   /* Synchron call failed */

     if(gap_debug)
     {
       printf("SYNCHRON CALL: %s\nretcode:%d (%d)\n", l_cmd, (int)l_rc, (int)l_mplayer_pid);
     }
   }

   return(l_mplayer_pid);

}       /* end p_start_mplayer_process */


/* ============================================================================
 * gap_mplayer_decode
 * ============================================================================
 */

gint32
gap_mplayer_decode(GapMPlayerParams *gpp)
{
  gint32 l_rc;
  char   extension[20];
  char   extension2[20];
  char   l_cmd[300];
  char   l_first_to_laod[200];
  char  *l_dst_dir;
  int    l_input_dir_created_by_myself;
  pid_t  l_mplayer_pid;

  l_rc = 0;
  l_input_dir_created_by_myself = FALSE;
  global_errlist = NULL;


  /* init mplayer_prog name
   * (including check if mplayer is installed)
   */
  l_rc = p_init_and_check_mplayer(gpp);
  if(l_rc != 0)
  {
    if(global_errlist)
    {
      p_mplayer_info(global_errlist);
    }
    else
    {
      p_mplayer_info("ERROR: could not execute mplayer");
    }

    return(l_rc);
  }


  l_rc = p_mplayer_dialog (gpp);


  if(l_rc != 0)
  {
    return(l_rc);
  }

  if((gpp->vtrack <= 0)
  && (gpp->atrack <= 0))
  {
    g_message(_("Exit, neither video nor audio track was selected"));
    return(0);
  }


  p_init_mplayer_working_dir(gpp);

  if(!g_file_test(gpp->video_filename, G_FILE_TEST_EXISTS))
  {
     global_errlist = g_strdup_printf(
            _("videofile %s not existent\n"),
            gpp->video_filename);
            l_rc = 10;
  }


  if (l_rc == 0)
  {
    switch(gpp->img_format)
    {
      case MPENC_PNG:
         strcpy(extension,  "png");
         strcpy(extension2, ".png");
         break;
      case MPENC_JPEG:
         strcpy(extension,  "jpg");
         strcpy(extension2, ".jpg");
         break;
      default:
         strcpy(extension,  "png");
         strcpy(extension2, ".xcf");
         break;

     }

     if (gpp->vtrack > 0)
     {
         /* for the frames we create a new directory */
         if (g_file_test(gpp->mplayer_working_dir, G_FILE_TEST_IS_DIR))
         {
           /* the input directory already exists,
            * remove frames.
            */
           g_snprintf(l_cmd, sizeof(l_cmd), "rm -f %s%s*.%s", gpp->mplayer_working_dir, G_DIR_SEPARATOR_S, extension);
           system(l_cmd);
         }
         else
         {
            /* create input directory (needed by mplayer to store the frames) */
            gap_file_mkdir(gpp->mplayer_working_dir, GAP_FILE_MKDIR_MODE);

            if (g_file_test(gpp->mplayer_working_dir, G_FILE_TEST_IS_DIR))
            {
              l_input_dir_created_by_myself = TRUE;
            }
            else
            {
               global_errlist = g_strdup_printf(
                      _("could not create %s directory\n"
                       "(that is required for mplayer frame export)"),
                       gpp->mplayer_working_dir);
               l_rc = 10;
            }
         }
     }
  }

  if(l_rc == 0)
  {
     if (gpp->vtrack > 0)
     {
       gimp_progress_init (_("Extracting frames..."));
     }
     else
     {
       gimp_progress_init (_("Extracting audio..."));
     }
     gimp_progress_update (0.02);  /* fake some progress */
     /* note:
      *  we can't show realistic progress for the extracting process
      *  because we know nothing about videofileformat and how much frames
      *  are realy stored in the videofile.
      *
      *  note: the mplayer process was already called synchron
      *  now rename and/or convert the extracted frames
      */

     l_mplayer_pid = p_start_mplayer_process(gpp);

     /* negative pid is used as error indicator */
     if (l_mplayer_pid < 0)
     {
        global_errlist = g_strdup_printf(
           _("could not start mplayer process\n(program=%s)")
           , gpp->mplayer_prog
           );
        l_rc = -1;
     }
  }

  if((l_rc == 0)
  && (gpp->vtrack > 0))
  {
     gint32  l_max_frame;


     if(gpp->run_mplayer_asynchron)
     {
       /* if mplayer was started as asynchron process
        * we use a polling procedure to show progress
        * the polling is done until the asynchron process is finished
        */
       p_poll(gpp, l_mplayer_pid, extension);
     }

     gimp_progress_update (1.0);

     l_max_frame = p_find_max_mplayer_frame (gpp, 1, extension);
     if (l_max_frame < 1)
     {
        global_errlist = g_strdup_printf(
               _("can't find any extracted frames,\n"
                 "mplayer has failed or was cancelled"));
        l_rc = -1;
     }
     else
     {
       /* if destination directorypart does not exist, try to create it */
       l_dst_dir = g_strdup(gpp->basename);
       p_dirname(l_dst_dir);
       if (*l_dst_dir != '\0')
       {
         if (! g_file_test(l_dst_dir, G_FILE_TEST_IS_DIR))
         {
            gap_file_mkdir (l_dst_dir, GAP_FILE_MKDIR_MODE);
         }
       }

       if(gap_debug)
       {
         printf("## img_format: %d ", gpp->img_format);
         printf("extension:%s:", extension);
         printf("extension2:%s:\n", extension2);
       }

       if(strcmp(extension, &extension2[1]) == 0)
       {
          gimp_progress_init (_("Renaming frames..."));
          l_rc = p_rename_frames(gpp
                                ,1                     /* first */
                                ,gpp->number_of_frames /* last_frame */
                                ,gpp->basename
                                ,extension
                                );
       }
       else
       {
          gimp_progress_init (_("Converting frames..."));
          l_rc = p_convert_frames(gpp
                                 ,1                      /* first_frame */
                                 ,gpp->number_of_frames  /* last_frame */
                                 ,gpp->basename
                                 ,extension
                                 ,extension2
                                 );
       }

       if (l_input_dir_created_by_myself)
       {
         if (strcmp(l_dst_dir, gpp->mplayer_working_dir) != 0)
         {
            /* remove input dir with all files */
            g_snprintf(l_cmd, sizeof(l_cmd), "rm -rf \"%s\"", gpp->mplayer_working_dir);
            system(l_cmd);
         }
       }
       g_free(l_dst_dir);
       gimp_progress_update (1.0);
     }
   }



   if(l_rc != 0)
   {
      if(global_errlist == NULL)
      {
         p_mplayer_info("ERROR: could not execute mplayer");
      }
      else
      {
         p_mplayer_info(global_errlist);
      }
      l_rc = -1;
   }
   else
   {
     if((gpp->autoload)
     && (gpp->vtrack > 0))
     {
        /* load first frame and add a display */
        p_build_gap_framename(l_first_to_laod
                             ,sizeof(l_first_to_laod)
                             ,1 /* mplayer starts extracted frames with nr 00000001 */
                             ,gpp->basename
                             ,&extension2[1]
                             );
        l_rc = gap_lib_load_image(l_first_to_laod);

        if(l_rc >= 0) gimp_display_new(l_rc);
     }
   }

   return(l_rc);
}       /* end  gap_mplayer_decode */