summaryrefslogtreecommitdiffstats
path: root/libhb/work.c
blob: 39c5a0dab7a1e709f782aff1d30e52f49b6cab7b (plain)
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
/* $Id: work.c,v 1.43 2005/03/17 16:38:49 titer Exp $

   This file is part of the HandBrake source code.
   Homepage: <http://handbrake.fr/>.
   It may be used under the terms of the GNU General Public License. */

#include "hb.h"
#include "a52dec/a52.h"
#include "dca.h"
#include "libavformat/avformat.h"

typedef struct
{
    hb_list_t * jobs;
    hb_job_t  ** current_job;
    int       * error;
    volatile int * die;

} hb_work_t;

static void work_func();
static void do_job( hb_job_t *);
static void work_loop( void * );
static void filter_loop( void * );

#define FIFO_UNBOUNDED 65536
#define FIFO_UNBOUNDED_WAKE 65535
#define FIFO_LARGE 32
#define FIFO_LARGE_WAKE 16
#define FIFO_SMALL 16
#define FIFO_SMALL_WAKE 15
#define FIFO_MINI 4
#define FIFO_MINI_WAKE 3

/**
 * Allocates work object and launches work thread with work_func.
 * @param jobs Handle to hb_list_t.
 * @param die Handle to user inititated exit indicator.
 * @param error Handle to error indicator.
 */
hb_thread_t * hb_work_init( hb_list_t * jobs, volatile int * die, int * error, hb_job_t ** job )
{
    hb_work_t * work = calloc( sizeof( hb_work_t ), 1 );

    work->jobs      = jobs;
    work->current_job = job;
    work->die       = die;
    work->error     = error;

    return hb_thread_init( "work", work_func, work, HB_LOW_PRIORITY );
}

static void InitWorkState( hb_handle_t * h )
{
    hb_state_t state;

    state.state = HB_STATE_WORKING;
#define p state.param.working
    p.progress  = 0.0;
    p.rate_cur  = 0.0;
    p.rate_avg  = 0.0;
    p.hours     = -1;
    p.minutes   = -1;
    p.seconds   = -1; 
#undef p

    hb_set_state( h, &state );

}

/**
 * Iterates through job list and calls do_job for each job.
 * @param _work Handle work object.
 */
static void work_func( void * _work )
{
    hb_work_t  * work = _work;
    hb_job_t   * job;

    hb_log( "%d job(s) to process", hb_list_count( work->jobs ) );

    while( !*work->die && ( job = hb_list_item( work->jobs, 0 ) ) )
    {
        hb_list_rem( work->jobs, job );
        job->die = work->die;
        *(work->current_job) = job;
        InitWorkState( job->h );
        do_job( job );
        *(work->current_job) = NULL;
    }

    *(work->error) = HB_ERROR_NONE;

    free( work );
}

hb_work_object_t * hb_get_work( int id )
{
    hb_work_object_t * w;
    for( w = hb_objects; w; w = w->next )
    {
        if( w->id == id )
        {
            hb_work_object_t *wc = malloc( sizeof(*w) );
            *wc = *w;
            return wc;
        }
    }
    return NULL;
}

hb_work_object_t * hb_codec_decoder( int codec )
{
    switch( codec )
    {
        case HB_ACODEC_AC3:  return hb_get_work( WORK_DECA52 );
        case HB_ACODEC_DCA:  return hb_get_work( WORK_DECDCA );
        case HB_ACODEC_LPCM: return hb_get_work( WORK_DECLPCM );
        default:
            if ( codec & HB_ACODEC_FF_MASK )
            {
                return hb_get_work( WORK_DECAVCODEC );
            }
            break;
    }
    return NULL;
}

hb_work_object_t * hb_codec_encoder( int codec )
{
    hb_work_object_t * w;
    switch( codec )
    {
        case HB_ACODEC_FAAC:   return hb_get_work( WORK_ENCFAAC );
        case HB_ACODEC_LAME:   return hb_get_work( WORK_ENCLAME );
        case HB_ACODEC_VORBIS: return hb_get_work( WORK_ENCVORBIS );
        case HB_ACODEC_CA_AAC: return hb_get_work( WORK_ENC_CA_AAC );
        case HB_ACODEC_CA_HAAC:return hb_get_work( WORK_ENC_CA_HAAC );
        case HB_ACODEC_FFAAC:
        {
            w = hb_get_work( WORK_ENCAVCODEC_AUDIO );
            w->codec_param = CODEC_ID_AAC;
            return w;
        }
        case HB_ACODEC_FFFLAC:
        {
            w = hb_get_work( WORK_ENCAVCODEC_AUDIO );
            w->codec_param = CODEC_ID_FLAC;
            return w;
        }
        case HB_ACODEC_AC3:
        {
            w = hb_get_work( WORK_ENCAVCODEC_AUDIO );
            w->codec_param = CODEC_ID_AC3;
            return w;
        }
    }
    return NULL;
}

/**
 * Displays job parameters in the debug log.
 * @param job Handle work hb_job_t.
 */
void hb_display_job_info( hb_job_t * job )
{
    hb_title_t * title = job->title;
    hb_audio_t   * audio;
    hb_subtitle_t * subtitle;
    int             i, j;
    
    hb_log("job configuration:");
    hb_log( " * source");
    
    hb_log( "   + %s", title->path );

    if( job->pts_to_start || job->pts_to_stop )
    {
        int64_t stop;
        int hr_start, min_start, hr_stop, min_stop;
        float sec_start, sec_stop;

        stop = job->pts_to_start + job->pts_to_stop;

        hr_start = job->pts_to_start / (90000 * 60 * 60);
        min_start = job->pts_to_start / (90000 * 60);
        sec_start = (float)job->pts_to_start / 90000.0 - min_start * 60;
        min_start %= 60;

        hr_stop = stop / (90000 * 60 * 60);
        min_stop = stop / (90000 * 60);
        sec_stop = (float)stop / 90000.0 - min_stop * 60;
        min_stop %= 60;

        hb_log( "   + title %d, start %d:%d:%.2f stop %d:%d:%.2f", title->index,
                hr_start, min_start, sec_start,
                hr_stop, min_stop, sec_stop);
    }
    else if( job->frame_to_start || job->frame_to_stop )
    {
        hb_log( "   + title %d, frames %d to %d", title->index,
                job->frame_to_start, job->frame_to_start + job->frame_to_stop );
    }
    else
    {
        hb_log( "   + title %d, chapter(s) %d to %d", title->index,
                job->chapter_start, job->chapter_end );
    }

    if( title->container_name != NULL )
        hb_log( "   + container: %s", title->container_name);

    if( title->data_rate )
    {
        hb_log( "   + data rate: %d kbps", title->data_rate / 1000 );
    }
    
    hb_log( " * destination");

    hb_log( "   + %s", job->file );

    switch( job->mux )
    {
        case HB_MUX_MP4:
            hb_log("   + container: MPEG-4 (.mp4 and .m4v)");
            
            if( job->ipod_atom )
                hb_log( "     + compatibility atom for iPod 5G");

            if( job->largeFileSize )
                hb_log( "     + 64-bit formatting");

            if( job->mp4_optimize )
                hb_log( "     + optimized for progressive web downloads");
            
            if( job->color_matrix_code )
                hb_log( "     + custom color matrix: %s", job->color_matrix_code == 1 ? "ITU Bt.601 (SD)" : job->color_matrix_code == 2 ? "ITU Bt.709 (HD)" : "Custom" );
            break;

        case HB_MUX_MKV:
            hb_log("   + container: Matroska (.mkv)");
            break;
    }

    if( job->chapter_markers )
    {
        hb_log( "     + chapter markers" );
    }
    
    hb_log(" * video track");
    
    hb_log("   + decoder: %s", title->video_codec_name );
    
    if( title->video_bitrate )
    {
        hb_log( "     + bitrate %d kbps", title->video_bitrate / 1000 );
    }
    
    if( job->cfr == 0 )
    {
        hb_log( "   + frame rate: same as source (around %.3f fps)",
            (float) title->rate / (float) title->rate_base );
    }
    else if( job->cfr == 1 )
    {
        hb_log( "   + frame rate: %.3f fps -> constant %.3f fps",
            (float) title->rate / (float) title->rate_base,
            (float) job->vrate / (float) job->vrate_base );
    }
    else if( job->cfr == 2 )
    {
        hb_log( "   + frame rate: %.3f fps -> peak rate limited to %.3f fps",
            (float) title->rate / (float) title->rate_base,
            (float) job->pfr_vrate / (float) job->pfr_vrate_base );
    }

    // Filters can modify dimensions.  So show them first.
    if( hb_list_count( job->list_filter ) )
    {
        hb_log("   + %s", hb_list_count( job->list_filter) > 1 ? "filters" : "filter" );
        for( i = 0; i < hb_list_count( job->list_filter ); i++ )
        {
            hb_filter_object_t * filter = hb_list_item( job->list_filter, i );
            if( filter->settings )
                hb_log("     + %s (%s)", filter->name, filter->settings);
            else
                hb_log("     + %s (default settings)", filter->name);
            if( filter->info )
            {
                hb_filter_info_t info;
                filter->info( filter, &info );
                if( info.human_readable_desc[0] )
                {
                    hb_log("       + %s", info.human_readable_desc);
                }
            }
        }
    }
    
    if( job->anamorphic.mode )
    {
        hb_log( "   + %s anamorphic", job->anamorphic.mode == 1 ? "strict" : job->anamorphic.mode == 2? "loose" : "custom" );
        if( job->anamorphic.mode == 3 && job->anamorphic.keep_display_aspect )
        {
            hb_log( "     + keeping source display aspect ratio"); 
        }
        hb_log( "     + storage dimensions: %d * %d, mod %i",
                    job->width, job->height, job->modulus );
        if( job->anamorphic.itu_par )
        {
            hb_log( "     + using ITU pixel aspect ratio values"); 
        }
        hb_log( "     + pixel aspect ratio: %i / %i", job->anamorphic.par_width, job->anamorphic.par_height );
        hb_log( "     + display dimensions: %.0f * %i",
            (float)( job->width * job->anamorphic.par_width / job->anamorphic.par_height ), job->height );
    }
    else
    {
        hb_log( "   + dimensions: %d * %d, mod %i",
                job->width, job->height, job->modulus );
    }

    if ( job->grayscale )
        hb_log( "   + grayscale mode" );

    if( !job->indepth_scan )
    {
        /* Video encoder */
        for( i = 0; i < hb_video_encoders_count; i++ )
        {
            if( hb_video_encoders[i].encoder == job->vcodec )
            {
                hb_log( "   + encoder: %s", hb_video_encoders[i].human_readable_name );
                break;
            }
        }

        if( job->x264_preset && *job->x264_preset &&
            job->vcodec == HB_VCODEC_X264 )
        {
            hb_log( "     + x264 preset: %s", job->x264_preset );
        }
        if( job->x264_tune && *job->x264_tune &&
            job->vcodec == HB_VCODEC_X264 )
        {
            hb_log( "     + x264 tune: %s", job->x264_tune );
        }
        if( job->advanced_opts && *job->advanced_opts &&
            ( ( job->vcodec & HB_VCODEC_FFMPEG_MASK ) ||
              ( job->vcodec == HB_VCODEC_X264 ) ) )
        {
            hb_log( "     + options: %s", job->advanced_opts );
        }
        if( job->x264_profile && *job->x264_profile &&
            job->vcodec == HB_VCODEC_X264 )
        {
            hb_log( "     + x264 profile: %s", job->x264_profile );
        }
        if( job->h264_level && *job->h264_level &&
            job->vcodec == HB_VCODEC_X264 )
        {
            hb_log( "     + h264 level: %s", job->h264_level );
        }

        if( job->vquality >= 0 )
        {
            hb_log( "     + quality: %.2f %s", job->vquality, job->vcodec == HB_VCODEC_X264 ? "(RF)" : "(QP)" ); 
        }
        else
        {
            hb_log( "     + bitrate: %d kbps, pass: %d", job->vbitrate, job->pass );
            if( job->pass == 1 && job->fastfirstpass == 1 &&
                job->vcodec == HB_VCODEC_X264 )
            {
                hb_log( "     + fast first pass" );
                hb_log( "     + options: ref=1:8x8dct=0:me=dia:trellis=0" );
                hb_log( "                analyse=i4x4 (if originally enabled, else analyse=none)" );
                hb_log( "                subq=2 (if originally greater than 2, else subq unchanged)" );
            }
        }
    }

    if( job->indepth_scan )
    {
        hb_log( " * Foreign Audio Search: %s%s%s",
                job->select_subtitle_config.dest == RENDERSUB ? "Render/Burn-in" : "Passthrough",
                job->select_subtitle_config.force ? ", Forced Only" : "",
                job->select_subtitle_config.default_track ? ", Default" : "" );
    }

    for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
    {
        subtitle =  hb_list_item( title->list_subtitle, i );

        if( subtitle )
        {
            if( job->indepth_scan )
            {
                hb_log( "   + subtitle, %s (track %d, id 0x%x) %s [%s]",
                        subtitle->lang, subtitle->track, subtitle->id,
                        subtitle->format == PICTURESUB ? "Picture" : "Text",
                        hb_subsource_name( subtitle->source ) );
            }
            else if( subtitle->source == SRTSUB )
            {
                /* For SRT, print offset and charset too */
                hb_log( " * subtitle track %i, %s (track %d, id 0x%x) Text [SRT] -> %s%s, offset: %"PRId64", charset: %s",
                        subtitle->out_track, subtitle->lang, subtitle->track, subtitle->id, "Passthrough",
                        subtitle->config.default_track ? ", Default" : "",
                        subtitle->config.offset, subtitle->config.src_codeset );
            }
            else
            {
                hb_log( " * subtitle track %i, %s (track %d, id 0x%x) %s [%s] -> %s%s%s",
                        subtitle->out_track, subtitle->lang, subtitle->track, subtitle->id,
                        subtitle->format == PICTURESUB ? "Picture" : "Text",
                        hb_subsource_name( subtitle->source ),
                        subtitle->config.dest == RENDERSUB ? "Render/Burn-in" : "Passthrough",
                        subtitle->config.force ? ", Forced Only" : "",
                        subtitle->config.default_track ? ", Default" : "" );
            }
        }
    }

    if( !job->indepth_scan )
    {
        for( i = 0; i < hb_list_count( title->list_audio ); i++ )
        {
            audio = hb_list_item( title->list_audio, i );

            hb_log( " * audio track %d", audio->config.out.track );
            
            if( audio->config.out.name )
                hb_log( "   + name: %s", audio->config.out.name );

            hb_log( "   + decoder: %s (track %d, id 0x%x)", audio->config.lang.description, audio->config.in.track + 1, audio->id );

            if( ( audio->config.in.codec == HB_ACODEC_AC3 ) || ( audio->config.in.codec == HB_ACODEC_DCA) )
            {
                hb_log( "     + bitrate: %d kbps, samplerate: %d Hz", audio->config.in.bitrate / 1000, audio->config.in.samplerate );
            }

            if( audio->config.out.codec & HB_ACODEC_PASS_FLAG )
            {
                for( j = 0; j < hb_audio_encoders_count; j++ )
                {
                    if( hb_audio_encoders[j].encoder == audio->config.out.codec )
                    {
                        hb_log( "   + %s", hb_audio_encoders[j].human_readable_name );
                        break;
                    }
                }
            }
            else
            {
                for( j = 0; j < hb_audio_mixdowns_count; j++ )
                {
                    if( hb_audio_mixdowns[j].amixdown == audio->config.out.mixdown )
                    {
                        hb_log( "   + mixdown: %s", hb_audio_mixdowns[j].human_readable_name );
                        break;
                    }
                }
                if( audio->config.out.gain != 0.0 )
                {
                    hb_log( "   + gain: %.fdB", audio->config.out.gain );
                }
                if( ( audio->config.out.dynamic_range_compression != 0.0 ) && ( audio->config.in.codec == HB_ACODEC_AC3 ) )
                {
                    hb_log( "   + dynamic range compression: %f", audio->config.out.dynamic_range_compression );
                }
                for( j = 0; j < hb_audio_encoders_count; j++ )
                {
                    if( hb_audio_encoders[j].encoder == audio->config.out.codec )
                    {
                        hb_log( "   + encoder: %s", hb_audio_encoders[j].human_readable_name );
                        if( audio->config.out.bitrate > 0 )
                            hb_log( "     + bitrate: %d kbps, samplerate: %d Hz", audio->config.out.bitrate, audio->config.out.samplerate );
                        else if( audio->config.out.quality != HB_INVALID_AUDIO_QUALITY )
                            hb_log( "     + quality: %.2f, samplerate: %d Hz", audio->config.out.quality, audio->config.out.samplerate );
                        else if( audio->config.out.samplerate > 0 )
                            hb_log( "     + samplerate: %d Hz", audio->config.out.samplerate );
                        if( audio->config.out.compression_level >= 0 )
                            hb_log( "     + compression level: %.2f", 
                                    audio->config.out.compression_level );
                        break;
                    }
                }
            }
        }
    }
}

/* Corrects framerates when actual duration and frame count numbers are known. */
void correct_framerate( hb_job_t * job )
{
    hb_interjob_t * interjob = hb_interjob_get( job->h );

    if( ( job->sequence_id & 0xFFFFFF ) != ( interjob->last_job & 0xFFFFFF) )
        return; // Interjob information is for a different encode.

    // compute actual output vrate from first pass
    interjob->vrate = job->vrate_base * ( (double)interjob->out_frame_count * 90000 / interjob->total_time );
    interjob->vrate_base = job->vrate_base;
}

void hb_filter_init_next( hb_list_t * list, int *score, int *ret_pos )
{
    int count = hb_list_count( list );
    int pos = *ret_pos + 1;
    int ii = 0;
    if ( pos == 0 )
    {
        hb_filter_object_t * filter = hb_list_item( list, pos );
        if ( filter->init_index == *score )
        {
            *ret_pos = pos;
            return;
        }
        pos++;
    }
    while (1)
    {
        if ( pos == count )
        {
            pos = 0;
            (*score)++;
        }
        hb_filter_object_t * filter = hb_list_item( list, pos );
        if ( filter->init_index == *score )
        {
            *ret_pos = pos;
            return;
        }
        pos++;
        ii++;
        if (ii > count)
        {
            // This is an error that should never happen
            hb_error("internal error during filter initialization");
            *ret_pos = -1;
            return;
        }
    }
}

/**
 * Job initialization rountine.
 * Initializes fifos.
 * Creates work objects for synchronizer, video decoder, video renderer, video decoder, audio decoder, audio encoder, reader, muxer.
 * Launches thread for each work object with work_loop.
 * Loops while monitoring status of work threads and fifos.
 * Exits loop when conversion is done and fifos are empty.
 * Closes threads and frees fifos.
 * @param job Handle work hb_job_t.
 */
static void do_job( hb_job_t * job )
{
    hb_title_t    * title;
    int             i, j;
    hb_work_object_t * w;
    hb_work_object_t * sync;
    hb_work_object_t * muxer;
    hb_interjob_t * interjob;

    hb_audio_t   * audio;
    hb_subtitle_t * subtitle;
    unsigned int subtitle_highest = 0;
    unsigned int subtitle_highest_id = 0;
    unsigned int subtitle_lowest = 0;
    unsigned int subtitle_lowest_id = 0;
    unsigned int subtitle_forced_id = 0;
    unsigned int subtitle_forced_hits = 0;
    unsigned int subtitle_hit = 0;

    title = job->title;
    interjob = hb_interjob_get( job->h );

    if( job->pass == 2 )
    {
        correct_framerate( job );
    }

    job->list_work = hb_list_init();

    hb_log( "starting job" );

    /*
     * Look for the scanned subtitle in the existing subtitle list
     * select_subtitle implies that we did a scan.
     */
    if ( !job->indepth_scan && interjob->select_subtitle &&
         ( job->pass == 0 || job->pass == 2 ) )
    {
        /*
         * Disable forced subtitles if we didn't find any in the scan
         * so that we display normal subtitles instead.
         */
        if( interjob->select_subtitle->config.force &&
            interjob->select_subtitle->forced_hits == 0 )
        {
            interjob->select_subtitle->config.force = 0;
        }
        for( i = 0; i < hb_list_count(title->list_subtitle); )
        {
            if( ( subtitle = hb_list_item( title->list_subtitle, i ) ) )
            {
                /*
                * Remove the scanned subtitle from the subtitle list if
                * it would result in an identical duplicate subtitle track
                * or an emty track (forced and no forced hits).
                */
                if( ( interjob->select_subtitle->id == subtitle->id ) &&
                    ( ( subtitle->config.force && interjob->select_subtitle->forced_hits == 0 ) ||
                      ( subtitle->config.force == interjob->select_subtitle->config.force ) ) )
                {
                    hb_list_rem( title->list_subtitle, subtitle );
                    free( subtitle );
                    continue;
                }
                /* Adjust output track number, in case we removed one.
                 * Output tracks sadly still need to be in sequential order.
                 * Note: out.track starts at 1, i starts at 0, and track 1 is
                 *       interjob->select_subtitle */
                subtitle->out_track = ++i + 1;
            }
            else
            {
                // avoid infinite loop is subtitle == NULL
                i++;
            }
        }

        /*
         * Add the subtitle that we found on the first pass for use in this
         * pass.
         *
         * Make sure it's the first subtitle in the list so that is is the
         * first burned subtitle (explicitly or after sanitizing) - which
         * should ensures that it doesn't get dropped.
         */
        interjob->select_subtitle->out_track = 1;
        hb_list_insert( title->list_subtitle, 0, interjob->select_subtitle );
        interjob->select_subtitle = NULL;
    }

    if ( !job->indepth_scan )
    {
        // Sanitize subtitles
        uint8_t one_burned = 0;
        for( i=0; i < hb_list_count(title->list_subtitle); )
        {
            subtitle =  hb_list_item( title->list_subtitle, i );

            if ( subtitle->config.dest == RENDERSUB )
            {
                if ( one_burned )
                {
                    if ( !hb_subtitle_can_pass(subtitle->source, job->mux) )
                    {
                        hb_log( "More than one subtitle burn-in requested, dropping track %d.", i );
                        hb_list_rem( title->list_subtitle, subtitle );
                        free( subtitle );
                        continue;
                    }
                    else
                    {
                        hb_log( "More than one subtitle burn-in requested.  Changing track %d to soft subtitle.", i );
                        subtitle->config.dest = PASSTHRUSUB;
                    }
                }
                else if ( !hb_subtitle_can_burn(subtitle->source) )
                {
                    hb_log( "Subtitle burn-in requested and input track can not be rendered.  Changing track %d to soft subtitle.", i );
                    subtitle->config.dest = PASSTHRUSUB;
                }
                else
                {
                    one_burned = 1;
                }
            }

            if ( subtitle->config.dest == PASSTHRUSUB &&
                 !hb_subtitle_can_pass(subtitle->source, job->mux) )
            {
                if ( !one_burned )
                {
                    hb_log( "Subtitle pass-thru requested and input track is not compatible with container.  Changing track %d to burned-in subtitle.", i );
                    subtitle->config.dest = RENDERSUB;
                    subtitle->config.default_track = 0;
                    one_burned = 1;
                }
                else
                {
                    hb_log( "Subtitle pass-thru requested and input track is not compatible with container.  One track already burned, dropping track %d.", i );
                    hb_list_rem( title->list_subtitle, subtitle );
                    free( subtitle );
                    continue;
                }
            }
            /* Adjust output track number, in case we removed one.
             * Output tracks sadly still need to be in sequential order.
             * Note: out.track starts at 1, i starts at 0 */
            subtitle->out_track = ++i;
        }
        if ( one_burned )
        {
            hb_filter_object_t * filter;

            // Add subtitle rendering filter
            // Note that if the filter is already in the filter chain, this
            // has no effect. Note also that this means the front-end is
            // not required to add the subtitle rendering filter since
            // we will always try to do it here.
            filter = hb_filter_init(HB_FILTER_RENDER_SUB);
            hb_add_filter( job, filter, NULL );
        }
    }

    // Filters have an effect on settings.
    // So initialize the filters and update the job.
    if( job->list_filter && hb_list_count( job->list_filter ) )
    {
        hb_filter_init_t init;
        int pos = -1, score = 0;

        init.job = job;
        init.pix_fmt = PIX_FMT_YUV420P;
        init.width = title->width;
        init.height = title->height;
        init.par_width = job->anamorphic.par_width;
        init.par_height = job->anamorphic.par_height;
        memcpy(init.crop, title->crop, sizeof(int[4]));
        init.vrate_base = title->rate_base;
        init.vrate = title->rate;
        init.cfr = 0;
        for( i = 0; i < hb_list_count( job->list_filter ); i++ )
        {
            // Some filters need to be initialized in a different order
            // than they are executed in the pipeline.  e.g. rendersub
            // needs to be initialized after cropscale so that it knows
            // the crop settings, but it needs to be executed before
            // cropscale. so hb_filter_init_next() finds returns the
            // position of filters in initialization order.
            hb_filter_init_next( job->list_filter, &score, &pos );
            if( pos == -1)
            {
                // This is an error that should never happen!
                break;
            }
            hb_filter_object_t * filter = hb_list_item( job->list_filter, pos );
            if( filter->init( filter, &init ) )
            {
                hb_log( "Failure to initialise filter '%s', disabling",
                        filter->name );
                hb_list_rem( job->list_filter, filter );
                hb_filter_close( &filter );
            }
        }
        job->width = init.width;
        job->height = init.height;
        job->anamorphic.par_width = init.par_width;
        job->anamorphic.par_height = init.par_height;
        memcpy(title->crop, init.crop, sizeof(int[4]));
        job->vrate_base = init.vrate_base;
        job->vrate = init.vrate;
        job->cfr = init.cfr;
    }

    if( job->anamorphic.mode )
    {
        /* While x264 is smart enough to reduce fractions on its own, libavcodec and
         * the MacGUI need some help with the math, so lose superfluous factors. */
        hb_reduce( &job->anamorphic.par_width, &job->anamorphic.par_height,
                    job->anamorphic.par_width,  job->anamorphic.par_height );
        if( job->vcodec & HB_VCODEC_FFMPEG_MASK )
        {
            /* Just to make working with ffmpeg even more fun,
             * lavc's MPEG-4 encoder can't handle PAR values >= 255,
             * even though AVRational does. Adjusting downwards
             * distorts the display aspect slightly, but such is life. */
            while( ( job->anamorphic.par_width  & ~0xFF ) ||
                   ( job->anamorphic.par_height & ~0xFF ) )
            {
                job->anamorphic.par_width  >>= 1;
                job->anamorphic.par_height >>= 1;
                hb_reduce( &job->anamorphic.par_width, &job->anamorphic.par_height,
                            job->anamorphic.par_width,  job->anamorphic.par_height );
            }
        }
    }
    
    job->fifo_mpeg2  = hb_fifo_init( FIFO_LARGE, FIFO_LARGE_WAKE );
    job->fifo_raw    = hb_fifo_init( FIFO_SMALL, FIFO_SMALL_WAKE );
    job->fifo_sync   = hb_fifo_init( FIFO_SMALL, FIFO_SMALL_WAKE );
    job->fifo_render = hb_fifo_init( FIFO_SMALL, FIFO_SMALL_WAKE );
    job->fifo_mpeg4  = hb_fifo_init( FIFO_LARGE, FIFO_LARGE_WAKE );

    /*
     * Audio fifos must be initialized before sync
     */
    if( !job->indepth_scan )
    {
    // apply Auto Passthru settings
    hb_autopassthru_apply_settings( job, title );
    // sanitize audio settings
    for( i = 0; i < hb_list_count( title->list_audio ); )
    {
        audio = hb_list_item( title->list_audio, i );
        if( audio->config.out.codec == HB_ACODEC_AUTO_PASS )
        {
            // Auto Passthru should have been handled above
            // remove track to avoid a crash
            hb_log( "Auto Passthru error, dropping track %d",
                    audio->config.out.track  );
            hb_list_rem( title->list_audio, audio );
            free( audio );
            continue;
        }
        if( ( audio->config.out.codec & HB_ACODEC_PASS_FLAG ) &&
           !( audio->config.in.codec & audio->config.out.codec & HB_ACODEC_PASS_MASK ) )
        {
            hb_log( "Passthru requested and input codec is not the same as output codec for track %d, dropping track",
                    audio->config.out.track );
            hb_list_rem( title->list_audio, audio );
            free( audio );
            continue;
        }
        if( !(audio->config.out.codec & HB_ACODEC_PASS_FLAG) )
        {
            if( audio->config.out.samplerate > 48000 )
            {
                hb_log( "Sample rate %d not supported. Down-sampling to 48kHz.",
                        audio->config.out.samplerate );
                audio->config.out.samplerate = 48000;
            }
            // if not specified, set to same as input
            if( audio->config.out.samplerate < 0 )
            {
                audio->config.out.samplerate = audio->config.in.samplerate;
            }
        }
        if( audio->config.out.codec == HB_ACODEC_CA_HAAC )
        {
            if( !encca_haac_available() )
            {
                // user chose Core Audio HE-AAC but the encoder is unavailable
                hb_log( "Core Audio HE-AAC unavailable. Using Core Audio AAC for track %d",
                        audio->config.out.track );
                audio->config.out.codec = HB_ACODEC_CA_AAC;
            }
            else if( audio->config.out.samplerate < 32000 )
            {
                // Core Audio HE-AAC doesn't support samplerates < 32 kHz
                hb_log( "Sample rate %d not supported (ca_haac). Using 32kHz for track %d",
                        audio->config.out.samplerate, audio->config.out.track );
                audio->config.out.samplerate = 32000;
            }
        }
        /* Adjust output track number, in case we removed one.
         * Output tracks sadly still need to be in sequential order.
         * Note: out.track starts at 1, i starts at 0 */
        audio->config.out.track = ++i;
    }

    int requested_mixdown = 0;
    int requested_mixdown_index = 0;
    int best_mixdown = 0;
    int best_bitrate = 0;

    for( i = 0; i < hb_list_count( title->list_audio ); i++ )
    {
        audio = hb_list_item( title->list_audio, i );

        /* sense-check the requested mixdown */
        if( audio->config.out.mixdown <= 0 &&
            !( audio->config.out.codec & HB_ACODEC_PASS_FLAG ) )
        {
            /*
             * Mixdown wasn't specified and this is not pass-through,
             * set a default mixdown
             */
            audio->config.out.mixdown = hb_get_default_mixdown( audio->config.out.codec,
                                                                audio->config.in.channel_layout );
            for (j = 0; j < hb_audio_mixdowns_count; j++)
            {
                if (hb_audio_mixdowns[j].amixdown == audio->config.out.mixdown)
                {
                    hb_log("work: mixdown not specified, track %i setting mixdown %s", audio->config.out.track, hb_audio_mixdowns[j].human_readable_name);
                    break;
                }
            }
        }

        /* log the requested mixdown */
        for (j = 0; j < hb_audio_mixdowns_count; j++) {
            if (hb_audio_mixdowns[j].amixdown == audio->config.out.mixdown) {
                requested_mixdown = audio->config.out.mixdown;
                requested_mixdown_index = j;
                break;
            }
        }

        best_mixdown = hb_get_best_mixdown( audio->config.out.codec,
                                            audio->config.in.channel_layout, 0 );

        if ( audio->config.out.mixdown > best_mixdown )
        {
            audio->config.out.mixdown = best_mixdown;
        }

        if ( audio->config.out.mixdown != requested_mixdown )
        {
            /* log the output mixdown */
            for (j = 0; j < hb_audio_mixdowns_count; j++)
            {
                if (hb_audio_mixdowns[j].amixdown == audio->config.out.mixdown)
                {
                    hb_log("work: sanitizing track %i mixdown %s to %s", 
                        audio->config.out.track, 
                        hb_audio_mixdowns[requested_mixdown_index].human_readable_name, 
                        hb_audio_mixdowns[j].human_readable_name);
                    break;
                }
            }
        }
        
        /* sense-check the requested compression level */
        if( audio->config.out.compression_level < 0 )
        {
            audio->config.out.compression_level =
                hb_get_default_audio_compression( audio->config.out.codec );
            if( audio->config.out.compression_level >= 0 )
            {
                hb_log( "work: compression level not specified, track %d setting compression level %.2f",
                    audio->config.out.track, audio->config.out.compression_level );
            }
        }

        if( audio->config.out.compression_level >= 0 )
        {
            float best_compression = hb_get_best_audio_compression( 
                                        audio->config.out.codec, 
                                        audio->config.out.compression_level );
            if( best_compression != audio->config.out.compression_level )
            {
                if( best_compression == -1 )
                {
                    hb_log( "work: compression levels not supported by codec" );
                }
                else
                {
                    hb_log( "work: sanitizing track %d audio compression level %.2f to %.2f", 
                            audio->config.out.track,
                            audio->config.out.compression_level,
                            best_compression );
                }
                audio->config.out.compression_level = best_compression;
            }
        }
        
        /* sense-check the requested quality */
        if( audio->config.out.quality != HB_INVALID_AUDIO_QUALITY )
        {
            float best_quality = hb_get_best_audio_quality( 
                    audio->config.out.codec, audio->config.out.quality );
            if( best_quality != audio->config.out.quality )
            {
                if( best_quality == HB_INVALID_AUDIO_QUALITY )
                {
                    hb_log( "work: quality mode not supported by codec" );
                }
                else
                {
                    hb_log( "work: sanitizing track %d audio quality %.2f to %.2f", 
                            audio->config.out.track,
                            audio->config.out.quality,
                            best_quality );
                }
                audio->config.out.quality = best_quality;
            }
        }
        
        /* sense-check the requested bitrate */
        if( audio->config.out.quality == HB_INVALID_AUDIO_QUALITY )
        {
            if( audio->config.out.bitrate <= 0 )
            {
                /*
                 * Bitrate wasn't specified and this is not pass-through,
                 * set a default bitrate
                 */
                audio->config.out.bitrate = 
                    hb_get_default_audio_bitrate( audio->config.out.codec,
                                                  audio->config.out.samplerate,
                                                  audio->config.out.mixdown );
                
                if( audio->config.out.bitrate > 0 )
                {
                    hb_log( "work: bitrate not specified, track %d setting bitrate %d",
                        audio->config.out.track, audio->config.out.bitrate );
                }
            }
            
            /* log the requested bitrate */
            best_bitrate = hb_get_best_audio_bitrate( audio->config.out.codec, 
                                                      audio->config.out.bitrate,
                                                      audio->config.out.samplerate,
                                                      audio->config.out.mixdown );
            
            if ( audio->config.out.bitrate != best_bitrate && 
                 best_bitrate > 0 )
            {
                /* log the output bitrate */
                hb_log( "work: sanitizing track %d audio bitrate %d to %d", 
                        audio->config.out.track,
                        audio->config.out.bitrate,
                        best_bitrate );
            }
            audio->config.out.bitrate = best_bitrate;
        }

        if (audio->config.out.codec == HB_ACODEC_VORBIS)
            audio->priv.config.vorbis.language = audio->config.lang.simple;

        /* set up the audio work structures */
        audio->priv.fifo_raw  = hb_fifo_init( FIFO_SMALL, FIFO_SMALL_WAKE );
        audio->priv.fifo_sync = hb_fifo_init( FIFO_SMALL, FIFO_SMALL_WAKE );
        audio->priv.fifo_out  = hb_fifo_init( FIFO_LARGE, FIFO_LARGE_WAKE );
        audio->priv.fifo_in   = hb_fifo_init( FIFO_LARGE, FIFO_LARGE_WAKE );
    }

    }
    /* Synchronization */
    sync = hb_sync_init( job );

    /* Video decoder */
    int vcodec = title->video_codec? title->video_codec : WORK_DECMPEG2;
#if defined(USE_FF_MPEG2)
    if (vcodec == WORK_DECMPEG2)
    {
        vcodec = WORK_DECAVCODECV;
        title->video_codec_param = CODEC_ID_MPEG2VIDEO;
    }
#endif
    hb_list_add( job->list_work, ( w = hb_get_work( vcodec ) ) );
    w->codec_param = title->video_codec_param;
    w->fifo_in  = job->fifo_mpeg2;
    w->fifo_out = job->fifo_raw;

    for( i=0; i < hb_list_count(title->list_subtitle); i++ )
    {
        subtitle =  hb_list_item( title->list_subtitle, i );

        if( subtitle )
        {
            subtitle->fifo_in   = hb_fifo_init( FIFO_SMALL, FIFO_SMALL_WAKE );
            // Must set capacity of the raw-FIFO to be set >= the maximum number of subtitle
            // lines that could be decoded prior to a video frame in order to prevent the following
            // deadlock condition:
            //   1. Subtitle decoder blocks trying to generate more subtitle lines than will fit in the FIFO.
            //   2. Blocks the processing of further subtitle packets read from the input stream.
            //   3. And that blocks the processing of any further video packets read from the input stream.
            //   4. And that blocks the sync work-object from running, which is needed to consume the subtitle lines in the raw-FIFO.
            // Since that number is unbounded, the FIFO must be made (effectively) unbounded in capacity.
            subtitle->fifo_raw  = hb_fifo_init( FIFO_UNBOUNDED, FIFO_UNBOUNDED_WAKE );
            subtitle->fifo_sync = hb_fifo_init( FIFO_SMALL, FIFO_SMALL_WAKE );
            subtitle->fifo_out  = hb_fifo_init( FIFO_SMALL, FIFO_SMALL_WAKE );

            if( job->indepth_scan && 
                !( job->select_subtitle_config.force &&
                   hb_subtitle_can_force( subtitle->source ) ) )
            {
                continue;
            }
            w = hb_get_work( subtitle->codec );
            w->fifo_in = subtitle->fifo_in;
            w->fifo_out = subtitle->fifo_raw;
            w->subtitle = subtitle;
            hb_list_add( job->list_work, w );
        }
    }

    /* Set up the video filter fifo pipeline */
    if( !job->indepth_scan )
    {
        if( job->list_filter )
        {
            int filter_count = hb_list_count( job->list_filter );
            int i;
            hb_fifo_t * fifo_in = job->fifo_sync;

            for( i = 0; i < filter_count; i++ )
            {
                hb_filter_object_t * filter = hb_list_item( job->list_filter, i );

                filter->fifo_in = fifo_in;
                filter->fifo_out = hb_fifo_init( FIFO_MINI, FIFO_MINI_WAKE );
                fifo_in = filter->fifo_out;
            }
            job->fifo_render = fifo_in;
        }
        else if ( !job->list_filter )
        {
            hb_log("work: Internal Error: no filters");
            job->fifo_render = NULL;
        }

        /* Video encoder */
        switch( job->vcodec )
        {
        case HB_VCODEC_FFMPEG_MPEG4:
            w = hb_get_work( WORK_ENCAVCODEC );
            w->codec_param = CODEC_ID_MPEG4;
            break;
        case HB_VCODEC_FFMPEG_MPEG2:
            w = hb_get_work( WORK_ENCAVCODEC );
            w->codec_param = CODEC_ID_MPEG2VIDEO;
            break;
        case HB_VCODEC_X264:
            w = hb_get_work( WORK_ENCX264 );
            break;
        case HB_VCODEC_THEORA:
            w = hb_get_work( WORK_ENCTHEORA );
            break;
        }
        // Handle case where there are no filters.  
        // This really should never happen.
        if ( job->fifo_render )
            w->fifo_in  = job->fifo_render;
        else
            w->fifo_in  = job->fifo_sync;

        w->fifo_out = job->fifo_mpeg4;
        w->config   = &job->config;

        hb_list_add( job->list_work, w );

        for( i = 0; i < hb_list_count( title->list_audio ); i++ )
        {
            audio = hb_list_item( title->list_audio, i );

            /*
            * Audio Decoder Thread
            */
            if ( audio->priv.fifo_in )
            {
                if ( ( w = hb_codec_decoder( audio->config.in.codec ) ) == NULL )
                {
                    hb_error("Invalid input codec: %d", audio->config.in.codec);
                    *job->die = 1;
                    goto cleanup;
                }
                w->fifo_in  = audio->priv.fifo_in;
                w->fifo_out = audio->priv.fifo_raw;
                w->config   = &audio->priv.config;
                w->audio    = audio;
                w->codec_param = audio->config.in.codec_param;

                hb_list_add( job->list_work, w );
            }

            /*
            * Audio Encoder Thread
            */
            if ( !(audio->config.out.codec & HB_ACODEC_PASS_FLAG ) )
            {
                /*
                * Add the encoder thread if not doing AC-3 pass through
                */
                if ( ( w = hb_codec_encoder( audio->config.out.codec ) ) == NULL )
                {
                    hb_error("Invalid audio codec: %#x", audio->config.out.codec);
                    w = NULL;
                    *job->die = 1;
                    goto cleanup;
                }
                w->fifo_in  = audio->priv.fifo_sync;
                w->fifo_out = audio->priv.fifo_out;
                w->config   = &audio->priv.config;
                w->audio    = audio;

                hb_list_add( job->list_work, w );
            }
        }
    }
    
    if( job->chapter_markers && job->chapter_start == job->chapter_end )
    {
        job->chapter_markers = 0;
        hb_log("work: only 1 chapter, disabling chapter markers");
    }

    /* Display settings */
    hb_display_job_info( job );

    /* Init read & write threads */
    hb_work_object_t *reader = hb_get_work(WORK_READER);
    if ( reader->init( reader, job ) )
    {
        hb_error( "Failure to initialise thread '%s'", reader->name );
        *job->die = 1;
        goto cleanup;
    }
    reader->done = &job->done;
    reader->thread = hb_thread_init( reader->name, ReadLoop, reader, HB_NORMAL_PRIORITY );

    job->done = 0;

    if( job->list_filter && !job->indepth_scan )
    {
        int filter_count = hb_list_count( job->list_filter );
        int i;

        for( i = 0; i < filter_count; i++ )
        {
            hb_filter_object_t * filter = hb_list_item( job->list_filter, i );

            if( !filter ) continue;

            // Filters were initialized earlier, so we just need
            // to start the filter's thread
            filter->done = &job->done;
            filter->thread = hb_thread_init( filter->name, filter_loop, filter,
                                             HB_LOW_PRIORITY );
        }
    }

    /* Launch processing threads */
    for( i = 0; i < hb_list_count( job->list_work ); i++ )
    {
        w = hb_list_item( job->list_work, i );
        w->done = &job->done;
        w->thread_sleep_interval = 10;
        if( w->init( w, job ) )
        {
            hb_error( "Failure to initialise thread '%s'", w->name );
            *job->die = 1;
            goto cleanup;
        }
        w->thread = hb_thread_init( w->name, work_loop, w,
                                    HB_LOW_PRIORITY );
    }

    if ( job->indepth_scan )
    {
        muxer = NULL;
        w = sync;
        sync->done = &job->done;
    }
    else
    {
        sync->done = &job->done;
        sync->thread_sleep_interval = 10;
        if( sync->init( w, job ) )
        {
            hb_error( "Failure to initialise thread '%s'", w->name );
            *job->die = 1;
            goto cleanup;
        }
        sync->thread = hb_thread_init( sync->name, work_loop, sync,
                                    HB_LOW_PRIORITY );

        // The muxer requires track information that's set up by the encoder
        // init routines so we have to init the muxer last.
        muxer = hb_muxer_init( job );
        w = muxer;
    }

    hb_buffer_t      * buf_in, * buf_out = NULL;

    while ( !*job->die && !*w->done && w->status != HB_WORK_DONE )
    {
        buf_in = hb_fifo_get_wait( w->fifo_in );
        if ( buf_in == NULL )
            continue;
        if ( *job->die )
        {
            if( buf_in )
            {
                hb_buffer_close( &buf_in );
            }
            break;
        }

        buf_out = NULL;
        w->status = w->work( w, &buf_in, &buf_out );

        if( buf_in )
        {
            hb_buffer_close( &buf_in );
        }
        if ( buf_out && w->fifo_out == NULL )
        {
            hb_buffer_close( &buf_out );
        }
        if( buf_out )
        {
            while ( !*job->die )
            {
                if ( hb_fifo_full_wait( w->fifo_out ) )
                {
                    hb_fifo_push( w->fifo_out, buf_out );
                    buf_out = NULL;
                    break;
                }
            }
        }
    }

    if ( buf_out )
    {
        hb_buffer_close( &buf_out );
    }

    hb_handle_t * h = job->h;
    hb_state_t state;
    hb_get_state( h, &state );
    
    hb_log("work: average encoding speed for job is %f fps", state.param.working.rate_avg);

    job->done = 1;
    if( muxer != NULL )
    {
        muxer->close( muxer );
        free( muxer );

        if( sync->thread != NULL )
        {
            hb_thread_close( &sync->thread );
            sync->close( sync );
        }
        free( sync );
    }

cleanup:
    /* Stop the write thread (thread_close will block until the muxer finishes) */
    job->done = 1;

    // Close render filter pipeline
    if( job->list_filter )
    {
        int filter_count = hb_list_count( job->list_filter );
        int i;

        for( i = 0; i < filter_count; i++ )
        {
            hb_filter_object_t * filter = hb_list_item( job->list_filter, i );

            if( !filter ) continue;

            if( filter->thread != NULL )
            {
                hb_thread_close( &filter->thread );
            }
            filter->close( filter );
        }
    }

    /* Close work objects */
    while( ( w = hb_list_item( job->list_work, 0 ) ) )
    {
        hb_list_rem( job->list_work, w );
        if( w->thread != NULL )
        {
            hb_thread_close( &w->thread );
            w->close( w );
        }
        free( w );
    }

    hb_list_close( &job->list_work );

    /* Stop the read thread */
    if( reader->thread != NULL )
    {
        hb_thread_close( &reader->thread );
        reader->close( reader );
    }
    free( reader );

    /* Close fifos */
    hb_fifo_close( &job->fifo_mpeg2 );
    hb_fifo_close( &job->fifo_raw );
    hb_fifo_close( &job->fifo_sync );
    hb_fifo_close( &job->fifo_render );
    hb_fifo_close( &job->fifo_mpeg4 );

    for (i=0; i < hb_list_count(title->list_subtitle); i++) {
        subtitle =  hb_list_item( title->list_subtitle, i);
        if( subtitle )
        {
            hb_fifo_close( &subtitle->fifo_in );
            hb_fifo_close( &subtitle->fifo_raw );
            hb_fifo_close( &subtitle->fifo_sync );
            hb_fifo_close( &subtitle->fifo_out );
        }
    }
    for( i = 0; i < hb_list_count( title->list_audio ); i++ )
    {
        audio = hb_list_item( title->list_audio, i );
        if( audio->priv.fifo_in != NULL )
            hb_fifo_close( &audio->priv.fifo_in );
        if( audio->priv.fifo_raw != NULL )
            hb_fifo_close( &audio->priv.fifo_raw );
        if( audio->priv.fifo_sync != NULL )
            hb_fifo_close( &audio->priv.fifo_sync );
        if( audio->priv.fifo_out != NULL )
            hb_fifo_close( &audio->priv.fifo_out );
    }

    if( job->indepth_scan )
    {
        /*
         * Before closing the title print out our subtitle stats if we need to
         * Find the highest and lowest.
         */
        for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
        {
            subtitle =  hb_list_item( title->list_subtitle, i );

            hb_log( "Subtitle track %d (id 0x%x) '%s': %d hits (%d forced)",
                    subtitle->track, subtitle->id, subtitle->lang,
                    subtitle->hits, subtitle->forced_hits );

            if( subtitle->hits == 0 )
                continue;

            if( subtitle_highest < subtitle->hits )
            {
                subtitle_highest = subtitle->hits;
                subtitle_highest_id = subtitle->id;
            }

            if( subtitle_lowest == 0 ||
                subtitle_lowest > subtitle->hits )
            {
                subtitle_lowest = subtitle->hits;
                subtitle_lowest_id = subtitle->id;
            }

            // pick the track with fewest forced hits
            if( subtitle->forced_hits > 0 &&
                ( subtitle_forced_hits == 0 ||
                  subtitle_forced_hits > subtitle->forced_hits ) )
            {
                subtitle_forced_id = subtitle->id;
                subtitle_forced_hits = subtitle->forced_hits;
            }
        }

        
        if( subtitle_forced_id )
        {
            /*
             * If there are any subtitle streams with forced subtitles
             * then select it in preference to the lowest.
             */
            subtitle_hit = subtitle_forced_id;
            hb_log( "Found a subtitle candidate with id 0x%x (contains forced subs)",
                    subtitle_hit );
        }
        else if( subtitle_lowest > 0 &&
                 subtitle_lowest < ( subtitle_highest * 0.1 ) )
        {
            /*
             * OK we have more than one, and the lowest is lower,
             * but how much lower to qualify for turning it on by
             * default?
             *
             * Let's say 10% as a default.
             */
            subtitle_hit = subtitle_lowest_id;
            hb_log( "Found a subtitle candidate with id 0x%x", subtitle_hit );
        }
        else
        {
            hb_log( "No candidate detected during subtitle scan" );
        }
    }

    if( job->indepth_scan )
    {
        for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
        {
            subtitle =  hb_list_item( title->list_subtitle, i );
            if( subtitle->id == subtitle_hit )
            {
                subtitle->config = job->select_subtitle_config;
                hb_list_rem( title->list_subtitle, subtitle );
                interjob->select_subtitle = subtitle;
                break;
            }
        }
    }

    if( job->list_filter )
    {
        for( i = 0; i < hb_list_count( job->list_filter ); i++ )
        {
            hb_filter_object_t * filter = hb_list_item( job->list_filter, i );
            hb_filter_close( &filter );
        }
        hb_list_close( &job->list_filter );
    }

    hb_buffer_pool_free();

    hb_title_close( &job->title );
    free( job );
}

static inline void copy_chapter( hb_buffer_t * dst, hb_buffer_t * src )
{
    // Propagate any chapter breaks for the worker if and only if the
    // output frame has the same time stamp as the input frame (any
    // worker that delays frames has to propagate the chapter marks itself
    // and workers that move chapter marks to a different time should set
    // 'src' to NULL so that this code won't generate spurious duplicates.)
    if( src && dst && src->s.start == dst->s.start)
    {
        // restore log below to debug chapter mark propagation problems
        //hb_log("work %s: Copying Chapter Break @ %"PRId64, w->name, src->s.start);
        dst->s.new_chap = src->s.new_chap;
    }
}

/**
 * Performs the work object's specific work function.
 * Loops calling work function for associated work object. Sleeps when fifo is full.
 * Monitors work done indicator.
 * Exits loop when work indiactor is set.
 * @param _w Handle to work object.
 */
static void work_loop( void * _w )
{
    hb_work_object_t * w = _w;
    hb_buffer_t      * buf_in = NULL, * buf_out = NULL;

    while( !*w->done && w->status != HB_WORK_DONE )
    {
        buf_in = hb_fifo_get_wait( w->fifo_in );
        if ( buf_in == NULL )
            continue;
        if ( *w->done )
        {
            if( buf_in )
            {
                hb_buffer_close( &buf_in );
            }
            break;
        }
        // Invalidate buf_out so that if there is no output
        // we don't try to pass along junk.
        buf_out = NULL;
        w->status = w->work( w, &buf_in, &buf_out );

        copy_chapter( buf_out, buf_in );

        if( buf_in )
        {
            hb_buffer_close( &buf_in );
        }
        if ( buf_out && w->fifo_out == NULL )
        {
            hb_buffer_close( &buf_out );
        }
        if( buf_out )
        {
            while ( !*w->done )
            {
                if ( hb_fifo_full_wait( w->fifo_out ) )
                {
                    hb_fifo_push( w->fifo_out, buf_out );
                    buf_out = NULL;
                    break;
                }
            }
        }
    }
    if ( buf_out )
    {
        hb_buffer_close( &buf_out );
    }

    // Consume data in incoming fifo till job complete so that
    // residual data does not stall the pipeline
    while( !*w->done )
    {
        buf_in = hb_fifo_get_wait( w->fifo_in );
        if ( buf_in != NULL )
            hb_buffer_close( &buf_in );
    }
}

/**
 * Performs the filter object's specific work function.
 * Loops calling work function for associated filter object. 
 * Sleeps when fifo is full.
 * Monitors work done indicator.
 * Exits loop when work indiactor is set.
 * @param _w Handle to work object.
 */
static void filter_loop( void * _f )
{
    hb_filter_object_t * f = _f;
    hb_buffer_t      * buf_in, * buf_out;

    while( !*f->done && f->status != HB_FILTER_DONE )
    {
        buf_in = hb_fifo_get_wait( f->fifo_in );
        if ( buf_in == NULL )
            continue;

        // Filters can drop buffers.  Remember chapter information
        // so that it can be propagated to the next buffer
        if ( buf_in->s.new_chap )
        {
            f->chapter_time = buf_in->s.start;
            f->chapter_val = buf_in->s.new_chap;
        }
        if ( *f->done )
        {
            if( buf_in )
            {
                hb_buffer_close( &buf_in );
            }
            break;
        }

        buf_out = NULL;
        f->status = f->work( f, &buf_in, &buf_out );

        if ( buf_out && f->chapter_val && f->chapter_time <= buf_out->s.start )
        {
            buf_out->s.new_chap = f->chapter_val;
            f->chapter_val = 0;
        }

        if( buf_in )
        {
            hb_buffer_close( &buf_in );
        }
        if ( buf_out && f->fifo_out == NULL )
        {
            hb_buffer_close( &buf_out );
        }
        if( buf_out )
        {
            while ( !*f->done )
            {
                if ( hb_fifo_full_wait( f->fifo_out ) )
                {
                    hb_fifo_push( f->fifo_out, buf_out );
                    break;
                }
            }
        }
    }
    // Consume data in incoming fifo till job complete so that
    // residual data does not stall the pipeline
    while( !*f->done )
    {
        buf_in = hb_fifo_get_wait( f->fifo_in );
        if ( buf_in != NULL )
            hb_buffer_close( &buf_in );
    }
}