summaryrefslogtreecommitdiffstats
path: root/k9vamps/k9vamps.cpp
blob: ebf4164200b265fda6e6dca6cac0047776df7a22 (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
//
// C++ Interface: k9vamps
//
// Description: A transcription from Vamps  in C++
//
//
// Author: Jean-Michel PETIT <k9copy@free.fr>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include "k9vamps.h"
#include <tqapplication.h>
#include "ac.h"


void k9vamps::setNoData() {
    noData=true;
    wDataRead.wakeAll();
    wDataReady.wakeAll();
}

void k9vamps::addData(uchar *data,uint size) {
    while (1) {
        if (m_fifo.freespace()>=size) {
            m_fifo.enqueue(data,size);
            wDataReady.wakeAll();
            break;
        } else
            wDataRead.wait();
    }
}


int k9vamps::readData(uchar * data,uint size) {
    uint size2=size;
    uint32_t readSize=0,s=0;
    
    while (1) {
	// is there data in the buffer?
	if (m_fifo.count() >0) {
		// s= size of data that we will read (maximum = size)
		s=(m_fifo.count()) <size2 ? (m_fifo.count()) : size2;
		// increments the number of readen bytes
		readSize+=s;
		// decrements the number of max bytes to read 
		size2-=s;
		//moves bytes from buffer to output
		m_fifo.dequeue(data,s);
		//moves the position of output buffer to receive next bytes
		data+=s;
		//there's now free space in input buffer, we can wake the injection thread
		wDataRead.wakeAll();
	}
	// break the loop if injection thread terminated or we got what we want (size bytes)
	// otherwise, we're waiting for datas
        if(noData || (m_fifo.count() >=size2)) {
            break;
        } else
            wDataReady.wait();
    }
    // if there's datas in input buffer and we did not get all what we wanted, we take them.
    s= (m_fifo.count()) <size2 ? (m_fifo.count()) : size2;
    readSize+=s;
    if (s>0 ) 
        m_fifo.dequeue(data,s);
   
    wDataRead.wakeAll();
    return readSize;
}

void k9vamps::addSubpicture(uint id) {
    int cpt=1;
    for (uint i=0;i<32;i++)
	if (spu_track_map[i]!=0) cpt++;
    spu_track_map[id-1]=cpt;
}

void k9vamps::addAudio(uint id) {
    int cpt=1;
    for (uint i=0;i <8;i++)
        if (audio_track_map[i] !=0) cpt++;

    audio_track_map[id-1]=cpt;
}

void k9vamps::addAudio(uint id,uint newId) {
    if (newId==0) 
        addAudio(id);
    else 
        audio_track_map[id-1]=newId;
}


void k9vamps::setInputSize(uint64_t size) {
    ps_size=size;
}

void k9vamps::setVapFactor(float factor) {
    vap_fact=factor;
}

void k9vamps::setSaveImage(k9SaveImage *m_save) {
    m_saveImage=m_save;
}

void k9vamps::reset() {
    m_preserve=true;
    bytes_read =0;
    bytes_written=0;
    padding_bytes=0;
    total_packs=0;
    video_packs=0;
    skipped_video_packs=0;
    aux_packs=0;
    skipped_aux_packs=0;
    sequence_headers=0;
    nav_packs=0;

    rptr = rbuf;
    rhwp = rbuf;
    wptr =  wbuf;
    vbuf_size = VBUF_SIZE;
    vap_fact= 1.0f;

    //	inbuffw=inbuff;
    for (uint i=0; i<8;i++) {
        audio_track_map[i]=0;
    }
    for (uint i=0; i<32;i++) {
        spu_track_map[i]=0;
    }

    calc_ps_vap = 1;
    vap_fact=1.0;
    ps_size=0;
    noData=false;

    avgdiff=1;
    m_totfact=m_nbfact=m_avgfact=0;

    vin_bytes=0;
    vout_bytes=0;

}

k9vamps::k9vamps(k9DVDBackup *dvdbackup) {
    m_saveImage=NULL;
    m_dvdbackup=dvdbackup;
    reset();
    m_requant=NULL;
    if (dvdbackup !=NULL)
    	m_bgUpdate = new k9bgUpdate(dvdbackup);
    else
	m_bgUpdate=NULL;
    rbuf_size= RBUF_SIZE;
    rbuf = (uchar*) malloc(rbuf_size);;
    m_output=NULL;
}


void k9vamps::setPreserve(bool _value) {
    m_preserve = _value;
}
void k9vamps::setOutput(TQFile *_output) {
    m_output=_output;
} 

k9vamps::~k9vamps() {
    if (m_bgUpdate !=NULL)
	delete m_bgUpdate;
    free (rbuf);
}


void k9vamps::run () {
    m_error=false;
   m_errMsg="";
    m_requant=new k9requant();
    eof=0;

    // allocate video buffers
    vibuf =(uchar*) malloc (vbuf_size);
    vobuf = (uchar*) malloc (vbuf_size);

    if (vibuf == NULL || vobuf == NULL)
        fatal (TQString("Allocation of video buffers failed: %1").arg(strerror (errno)));


    // actually do vaporization
    vaporize ();


    flush();

    if (m_requant !=NULL) {	
        m_requant->rqt_stop=true;
	while(m_requant->running()) {
	    m_requant->condr.wakeAll();
	    m_requant->condw.wakeAll();
	    m_requant->wait(10);
	}
//        m_requant->mutr.unlock();
//        m_requant->mutw.unlock();
    }
    delete m_requant;
    m_requant=NULL;
    free (vibuf);
    free(vobuf);
    if (m_bgUpdate!=NULL)
	m_bgUpdate->wait();
    //mutex.unlock();
}



// lock `size' bytes in read buffer
// i.e. ensure the next `size' input bytes are available in buffer
// returns nonzero on EOF
int k9vamps::lock (int size) {
    int avail, n;

    avail = rhwp - rptr;

    if (avail >= size)
        return 0;

    if (avail) {
        tc_memcpy (rbuf, rptr, avail);
        rptr = rbuf;
        rhwp = rptr + avail;
    }

    if (rbuf_size -avail <=0) {
	uchar *buffer =(uchar*) malloc (rbuf_size+20480);
	tc_memcpy (buffer,rbuf,rbuf_size);
	rptr = buffer +(rptr-rbuf);
	rhwp=buffer+(rhwp-rbuf);
	rbuf_size+=20480;
	free(rbuf);
	rbuf=buffer;
   }

    n = readData(rhwp,rbuf_size - avail);

    if (n % SECT_SIZE)
        fatal ("Premature EOF");

    rhwp       += n;
    bytes_read += n;

    return !n;
}


// copy `size' bytes from rbuf to wbuf
void k9vamps::copy (int size) {
    if (!size)
        return;

    if ((wptr - wbuf) + size > WBUF_SIZE)
        fatal ("Write buffer overflow");

    tc_memcpy (wptr, rptr, size);
    rptr += size;
    wptr += size;
}


// skip `size' bytes in rbuf
void k9vamps::skip (int size) {
    rptr += size;
}


// flush wbuf
void k9vamps::flush (void) {
    int size;
    mutex.lock();
    size = wptr - wbuf;

    if (!size) {
        mutex.unlock();
        return;
    }
    //m_dvdbackup->getOutput(wbuf,size);
    // wait for a preceding update to finish
    if (m_bgUpdate!=NULL) {
	m_bgUpdate->wait();
	m_bgUpdate->update( wbuf,size);
    } 
    if (m_output != NULL)
	m_output->writeBlock((const char*) wbuf,size);
    if (m_saveImage !=NULL) 
        m_saveImage->addData(wbuf,size);
    wptr           = wbuf;
    bytes_written += size;
    mutex.unlock();
}


// returns no. bytes read up to where `ptr' points
uint64_t k9vamps::rtell (uchar *ptr) {
    return bytes_read - (rhwp - ptr);
}


// returns no. bytes written up to where `ptr' points
// (including those in buffer which are not actually written yet)
uint64_t k9vamps::wtell (uchar *ptr) {
    return bytes_written + (ptr - wbuf);
}


// some pack header consistency checking
bool k9vamps::check_pack (uchar *ptr) {
    uint32_t pack_start_code;
    int    pack_stuffing_length;

    pack_start_code  = (uint32_t) (ptr [0]) << 24;
    pack_start_code |= (uint32_t) (ptr [1]) << 16;
    pack_start_code |= (uint32_t) (ptr [2]) <<  8;
    pack_start_code |= (uint32_t) (ptr [3]);

    if (pack_start_code != 0x000001ba) {
//        fatal ("Bad pack start code at %llu: %08lx", rtell (ptr), pack_start_code);
	return false;
    }

    if ((ptr [4] & 0xc0) != 0x40) {
     //   fatal ("Not an MPEG2 program stream pack at %llu", rtell (ptr));
	return false;
    }

    // we rely on a fixed pack header size of 14
    // so better to ensure this is true
    pack_stuffing_length = ptr [13] & 7;

    if (pack_stuffing_length) {
        //fatal ("Non-zero pack stuffing length at %llu: %d\n",   rtell (ptr), pack_stuffing_length);
       return false;
    }

    return true;
}


// video packet consistency checking
int k9vamps::check_video_packet (uchar *ptr) {
    int    vid_packet_length, pad_packet_length, rc = 0;
    uint32_t vid_packet_start_code, pad_packet_start_code, sequence_header_code;

    vid_packet_start_code  = (uint32_t) (ptr [0]) << 24;
    vid_packet_start_code |= (uint32_t) (ptr [1]) << 16;
    vid_packet_start_code |= (uint32_t) (ptr [2]) <<  8;
    vid_packet_start_code |= (uint32_t) (ptr [3]);

    if (vid_packet_start_code != 0x000001e0)
        fatal(TQString ("Bad video packet start code at %1: %2").arg(rtell(ptr)).arg(vid_packet_start_code,0,16));

    vid_packet_length  = ptr [4] << 8;
    vid_packet_length |= ptr [5];
    vid_packet_length += 6;

    if ((ptr [6] & 0xc0) != 0x80)
        fatal (TQString("Not an MPEG2 video packet at %1").arg(rtell (ptr)));

    if (ptr [7]) {
        if ((ptr [7] & 0xc0) != 0xc0)
           tqDebug ("%s", (TQString("First video packet in sequence starting at %1 misses PTS or DTS, flags=%2").arg(rtell (ptr)).arg(ptr [7])).ascii());
        else {
            sequence_header_code  = (uint32_t) (ptr [6 + 3 + ptr [8] + 0]) << 24;
            sequence_header_code |= (uint32_t) (ptr [6 + 3 + ptr [8] + 1]) << 16;
            sequence_header_code |= (uint32_t) (ptr [6 + 3 + ptr [8] + 2]) <<  8;
            sequence_header_code |= (uint32_t) (ptr [6 + 3 + ptr [8] + 3]);
    
            if (sequence_header_code == 0x000001b3) {
                rc = 1;
            } else {
                //fprintf (stderr, "Start of GOP at %llu not on sector boundary\n",
                //         rtell (ptr + 6 + 3 + ptr [8]));
                sequence_headers++;
            }
        }
        
    }

    pad_packet_length = 0;

    if (14 + vid_packet_length < SECT_SIZE - 6) {
        // video packet does not fill whole sector
        // check for padding packet
        ptr += vid_packet_length;

        pad_packet_start_code  = (uint32_t) (ptr [0]) << 24;
        pad_packet_start_code |= (uint32_t) (ptr [1]) << 16;
        pad_packet_start_code |= (uint32_t) (ptr [2]) <<  8;
        pad_packet_start_code |= (uint32_t) (ptr [3]);

        if (pad_packet_start_code != 0x000001be)
            tqDebug ("%s", (TQString("Bad padding packet start code at %1: %2").arg(rtell (ptr + vid_packet_length)).arg(pad_packet_start_code)).ascii());
        else {
            pad_packet_length  = ptr [4] << 8;
            pad_packet_length |= ptr [5];
            pad_packet_length += 6;
        }
    }

    // length of video packet plus padding packet must always match sector size
    if (14 + vid_packet_length + pad_packet_length != SECT_SIZE)
        tqDebug ("%s", (TQString("Bad video packet length at %1: %2").arg(rtell (ptr)).arg(vid_packet_length)).ascii());

    return rc;
}


// here we go
// this is where we switch to the requantization thread
// note that this and the requant thread never run concurrently (apart
// from a very short time) so a dual CPU box does not give an advantage
// returns size of evaporated GOP
int k9vamps::requant (uchar *dst, uchar *src, int n, float fact) {
    if (n==0) return 0;
    int rv;
    if (! m_requant->running()) {
        m_requant->initvar();
    }
    m_requant->rqt_stop=false;
    // this ensures for the requant thread to stop at this GOP's end
    tc_memcpy (src + n, "\0\0\1", 3);

    m_requant->mutr.lock();

    m_requant->rqt_rptr     = src;
    m_requant->rqt_wptr     = dst;
    m_requant->rqt_rcnt     = n;
    m_requant->rqt_wcnt     = 0;
    m_requant->rqt_fact     =  fact  ;
    m_requant->rqt_inbytes  = vin_bytes;
    m_requant->rqt_outbytes = vout_bytes;
    m_requant->rqt_visize   = (uint64_t) ((float) ps_size * (float) vin_bytes / ((float) total_packs * (float) SECT_SIZE));

    // create requantization thread
    if (! m_requant->running()) {
        m_requant->start();
        m_requant->rqt_run=true;
    }

    m_requant->condr.wakeAll();
    m_requant->mutr.unlock();

    // now the requant thread should be running

    m_requant->mutw.lock();

    // wait for requant thread to finish
    while (!m_requant->rqt_wcnt)
        m_requant->condw.wait( &m_requant->mutw);

    rv = m_requant->rqt_wcnt;

    m_requant->mutw.unlock();
/*    if ((m_requant->rbuf-m_requant->cbuf -3) >0 )  {
        tc_memcpy(dst+m_requant->rqt_wcnt,m_requant->cbuf,m_requant->rbuf-m_requant->cbuf -3);
        rv +=m_requant->rbuf-m_requant->cbuf -3;
    }
/*/
   if ((m_requant->rbuf-m_requant->cbuf -2) >0 )  {
        tc_memcpy(dst+m_requant->rqt_wcnt,m_requant->cbuf,m_requant->rbuf-m_requant->cbuf -2);
        rv +=m_requant->rbuf-m_requant->cbuf -2;
    }



//    if (rv>n)
//        tqDebug("requant error");

    double realrqtfact=(double)(vin_bytes) / (double)(vout_bytes+rv);
    avgdiff = ((m_avgfact) /realrqtfact);

    //tqDebug ("factor : " +TQString::number(m_avgfact) +"  --> " +TQString::number((float)n/(float)rv) +" avgdiff : " + TQString::number(avgdiff) +" rqt_visize :" +TQString::number(m_requant->rqt_visize) +" ps_size :" +TQString::number(ps_size) + " vin_bytes :" + TQString::number(vin_bytes)) ;

    return rv;

}


// translate type of private stream 1 packet
// according to the track translation maps
// returns new track type (e.g. 0x80 for first AC3 audio
// track in cmd line) or zero if track is not to be copied
int k9vamps::new_private_1_type (uchar *ptr) {
    int type, track, abase;

    type = ptr [6 + 3 + ptr [8]];
    //fprintf (stderr, "type=%02x\n", type);

    if (type >= 0x20 && type <= 0x3f) {
        // subpicture
        
        track = spu_track_map [type - 0x20];

        return track ? track - 1 + 0x20 : 0;
    }

    if (type >= 0x80 && type <= 0x87) {
        // AC3 audio
        abase = 0x80;
    } else if (type >= 0x88 && type <= 0x8f) {
        // DTS audio
        abase = 0x88;
    } else if (type >= 0xa0 && type <= 0xa7) {
        // LPCM audio
        abase = 0xa0;
    } else {
//        fatal ("Unknown private stream 1 type at %llu: %02x", rtell (ptr), type);
        abase = 0;
    }
	
    track = audio_track_map [type - abase];

    return track ? track - 1 + abase : 0;
}


// selectivly copy private stream 1 packs
// patches track type to reflect new track
// mapping unless user opted to preserve them
void k9vamps::copy_private_1 (uchar *ptr) {
    int type;

    type = new_private_1_type (ptr);

    if (type) {
        if (!m_preserve)
            ptr [6 + 3 + ptr [8]] = type;

        copy (SECT_SIZE);

        return;
    }

    skip (SECT_SIZE);
}


// translate ID of MPEG audio packet
// according to the audio track translation map
// returns new ID (e.g. 0xc0 for first MPEG audio
// track in cmd line) or zero if track is not to be copied
int k9vamps::new_mpeg_audio_id (int id) {
    int track;

    track = audio_track_map [id - 0xc0];

    return track ? track - 1 + 0xc0 : 0;
}


// selectivly copy MPEG audio packs
// patches ID to reflect new track mapping unless user opted to preserve them
void k9vamps::copy_mpeg_audio (uchar *ptr) {
    int id;

    id = new_mpeg_audio_id (ptr [3]);

    if (id) {
        if (!m_preserve)
            ptr [3] = id;

        copy (SECT_SIZE);

        return;
    }

    skip (SECT_SIZE);
}


// process beginning of program stream up to
// - but not including - first sequence header
// this PS leader is NOT shrunk since the PS may not
// necessarily begin at a GOP boundary (although it should?)
// nevertheless the unwanted private stream 1 and MPEG audio
// packs are skipped since some players could get confused otherwise
void k9vamps::vap_leader () {
    uchar *ptr;
    int    id, data_length;

    while (!lock (SECT_SIZE)) {
        ptr = rptr;
        if (check_pack (ptr)) {
	        ptr += 14;
        	id   = ptr [3];
	} else {
		ptr +=14;
	        id   = 0;
	}

        switch (id) {
        case 0xe0:
            // video
            if (check_video_packet (ptr))
                // sequence header
                return;

            copy (SECT_SIZE);
            break;

        case 0xbd:
            // private 1: audio/subpicture
            copy_private_1 (ptr);
            break;

        case 0xc0:
        case 0xc1:
        case 0xc2:
        case 0xc3:
        case 0xc4:
        case 0xc5:
        case 0xc6:
        case 0xc7:
            // MPEG audio
            copy_mpeg_audio (ptr);
            break;

        case 0xbb:
            // system header/private 2: PCI/DSI
            copy (SECT_SIZE);
            break;

        case 0xbe:
            // padding
            data_length  = ptr [4] << 8;
            data_length |= ptr [5];

            if (14 + data_length != SECT_SIZE - 6)
                fatal (TQString("Bad padding packet length at %1: %2").arg(rtell (ptr)).arg(data_length));
  	    //JMP:à vérifier 
  	    skip (SECT_SIZE);

            break;
	   
        default:
	 //   fatal("Encountered stream ID %02x at %llu, "
         //          "probably bad MPEG2 program stream", id, rtell (ptr));
		copy (SECT_SIZE);
        }

        if (wptr == wbuf + WBUF_SIZE)
            flush ();
    }

    eof = 1;
    flush ();

    return;
}


// process end of program stream
// the same counts here as for the PS' beginning
void k9vamps::vap_trailer (int length) {
    uchar *ptr;
    int    i, id, data_length;

    for (i = 0; i < length; i += SECT_SIZE) {
        ptr = rptr + 14;
        id  = ptr [3];

        if (id == 0xbd) {
            // private 1: audio/subpicture
            copy_private_1 (ptr);
        } else if (id >= 0xc0 && id <= 0xc7) {
            // MPEG audio
            copy_mpeg_audio (ptr);
        } else if (id == 0xbe) {
            // padding
            data_length  = ptr [4] << 8;
            data_length |= ptr [5];

            if (14 + data_length != SECT_SIZE - 6)
                fatal (TQString("Bad padding packet length at %1: %2").arg(rtell (ptr)).arg(data_length));
	    skip (SECT_SIZE);
        } else {
            copy (SECT_SIZE);
        }

        if (wptr == wbuf + WBUF_SIZE)
            flush ();
    }

    flush ();
}


// vaporization is split in two phases - this is phase 1
// PS packs are read into rbuf until a sequence header is found.
// All video packs are unpacketized and the contained video ES
// GOP copied to vibuf. In the same course the private stream 1
// and MPEG audio packs are inspected and the number of packs
// not to be copied are counted. This is to forecast the video
// vaporization factor in case the user specified a PS shrink factor.
// returns GOP length in bytes
int k9vamps::vap_phase1 (void) {
    uchar *ptr, *viptr = vibuf;
    int    seq_length, id, data_length, opt_length, seqhdr;

    for (seq_length = 0;
            !lock (seq_length + SECT_SIZE); seq_length += SECT_SIZE) {
        ptr = rptr + seq_length;
        if (check_pack (ptr)) {
	        ptr += 14;
        	id   = ptr [3];
	} else {
		ptr += 14;
		id   = 0;
	}
	

        // avoid duplicate counts for sequence headers
        if (seq_length)
            total_packs++;

        switch (id) {
        case 0xe0:
            // video
            seqhdr = check_video_packet (ptr);

            if (seq_length) {
                video_packs++;

                if (seqhdr) {
                    sequence_headers++;
                    vilen = viptr - vibuf;

                    return seq_length;
                }
            }

            // copy contained video ES fragment to vibuf
            data_length  = ptr [4] << 8;
            data_length |= ptr [5];
            opt_length   = 3 + ptr [8];
            data_length -= opt_length;

            if ((viptr - vibuf) + data_length > vbuf_size - 3) {
                // reallocate video buffers
                int i = viptr - vibuf;

                // grow by another VBUF_SIZE bytes
                vbuf_size += VBUF_SIZE;
                vibuf      = (uchar*)realloc (vibuf, vbuf_size);
                vobuf      = (uchar*)realloc (vobuf, vbuf_size);

                if (vibuf == NULL || vobuf == NULL)
                    fatal ("Reallocation of video buffers failed");

                viptr = vibuf + i;
            }

            //fprintf (stderr, "data_length=%d\n", data_length);
            tc_memcpy (viptr, ptr + 6 + opt_length, data_length);
            viptr += data_length;
            break;

        case 0xbd:
            // private 1: audio/subpicture
            aux_packs++;

            if (!new_private_1_type (ptr))
                skipped_aux_packs++;

            break;

        case 0xc0:
        case 0xc1:
        case 0xc2:
        case 0xc3:
        case 0xc4:
        case 0xc5:
        case 0xc6:
        case 0xc7:
            // MPEG audio
            aux_packs++;

            if (!new_mpeg_audio_id (id))
                skipped_aux_packs++;

            break;

        case 0xbb:
            // system header/private 2: PCI/DSI
            nav_packs++;
            break;

        case 0xbe:
            // padding
            skipped_aux_packs++;
            data_length  = ptr [4] << 8;
            data_length |= ptr [5];

            if (14 + data_length != SECT_SIZE - 6)
                fatal (TQString("Bad padding packet length at %1: %2").arg(rtell (ptr)).arg(data_length));

            break;

        default:
//	    fatal("Encountered stream ID %02x at %llu, "
//                   "probably bad MPEG2 program stream", id, rtell (ptr));	
		break;
        }

    }

    eof = 1;

    return seq_length;
}


// re-packetize the video ES
// `ptr' points to original PES packet where to put the video data
// `voptr' points to first unpacketized byte in vobuf
// `avail' specifies number of bytes remaining in vobuf
// returns number of ES bytes in generated PES packet
int k9vamps::gen_video_packet (uchar *ptr, uchar *voptr, int avail) {
    int i, header_data_length, data_length, padding_length;

    // if original PES holds optional data (e.g. DTS/PTS) we must keep it
    header_data_length = (ptr [7] & 0xc0) == 0xc0 ? ptr [8] : 0;
    data_length        = SECT_SIZE - (14 + 6 + 3 + header_data_length);

    if (avail >= data_length) {
        // write out a full video packet (usually 2025 byte)
        tc_memcpy (ptr + 6 + 3 + header_data_length, voptr, data_length);
        ptr [4] = (SECT_SIZE - (14 + 6)) >> 8;
        ptr [5] = (SECT_SIZE - (14 + 6)) & 0xff;
        ptr [8] = header_data_length;

        return data_length;
    }

    if (avail < data_length - 6) {
        // write a short video packet and a padding packet
        tc_memcpy (ptr + 6 + 3 + header_data_length, voptr, avail);
        ptr [4] = (3 + header_data_length + avail) >> 8;
        ptr [5] =  3 + header_data_length + avail;
        ptr [8] = header_data_length;

        // generate padding packet
        ptr           += 6 + 3 + header_data_length + avail;
        padding_length = data_length - (avail + 6);
        padding_bytes += padding_length + 6;
        ptr [0]        = 0;
        ptr [1]        = 0;
        ptr [2]        = 1;
        ptr [3]        = 0xbe;
        ptr [4]        = padding_length >> 8;
        ptr [5]        = padding_length;

        for (i = 0; i < padding_length; i++)
            ptr [6+i] = 0xff;

        return avail;
    }

    // write a padded video packet (1 to 6 padding bytes)
    padding_length = data_length - avail;
    padding_bytes += padding_length;
    memset (ptr + 6 + 3 + header_data_length, 0xff, padding_length);
    header_data_length += padding_length;
    tc_memcpy (ptr + 6 + 3 + header_data_length, voptr, avail);
    ptr [4] = (SECT_SIZE - (14 + 6)) >> 8;
    ptr [5] = (SECT_SIZE - (14 + 6)) & 0xff;
    ptr [8] = header_data_length;

    return avail;
}


// this is phase 2 of vaporization
// the shrunk video ES is re-packetized by using the source PES packets
// unused PS packs are skipped
// only wanted private stream 1 and MPEG audio packs are copied
// all nav packs are copied
void k9vamps::vap_phase2 (int seq_length) {
    int    i, id, avail, data_length;
    uchar *ptr, *voptr = vobuf, *vohwp = vobuf + volen;

    for (i = 0; i < seq_length; i += SECT_SIZE) {
        ptr = rptr + 14;
        id  = ptr [3];

        switch (id) {
        case 0xe0:
            // video
            avail = vohwp - voptr;

            if (avail) {
                // still some video output data left
                voptr += gen_video_packet (ptr, voptr, avail);
                copy (SECT_SIZE);
            } else {
                // no video output data left - skip input sector
                skip (SECT_SIZE);
                skipped_video_packs++;
            }

            break;

        case 0xbd:
            // private 1: audio/subpicture
            copy_private_1 (ptr);
            break;

        case 0xc0:
        case 0xc1:
        case 0xc2:
        case 0xc3:
        case 0xc4:
        case 0xc5:
        case 0xc6:
        case 0xc7:
            // MPEG audio
            copy_mpeg_audio (ptr);
            break;

        case 0xbb:
            // system header/private 2: PCI/DSI
            copy (SECT_SIZE);
            break;

        case 0xbe:
            // padding
            data_length  = ptr [4] << 8;
            data_length |= ptr [5];

            if (14 + data_length != SECT_SIZE - 6)
                fatal (TQString("Bad padding packet length at %1: %2").arg(rtell (ptr)).arg(data_length));
	    //JMP: à vérifier
  	    skip (SECT_SIZE);
            break;

        default:
            copy (SECT_SIZE);
//	    fatal("Encountered stream ID %02x at %llu, "
//                   "probably bad MPEG2 program stream", id, rtell (ptr));
        }

        if (wptr == wbuf + WBUF_SIZE)
            // end of write buffer reached --> flush it to disk
            flush ();
    }
}

TQString & k9vamps::geterrMsg() {
    return m_errMsg;
}

bool k9vamps::geterror() {
    return m_error;
}

// entry point from main()
// the requant thread already has been started
void k9vamps::vaporize (void) {
    int   seq_length;
    float fact = vap_fact;

    // process PS up to but not including first sequence header
    vap_leader ();

    // just in case - maybe should spit out a warning/error here
    if (eof)
        return;

    total_packs++;
    nav_packs++;
    total_packs++;
    video_packs++;

    // main loop
    while (1) {
        // do phase 1 of vaporization
        seq_length = vap_phase1 ();

        if (eof) {
            // EOF on source PS
            // process packs after and including last sequence header
            vap_trailer (seq_length);

            // only exit point from main loop
            return;
        }

        //fprintf (stderr, "seq_length=%d\n", seq_length);

        if (calc_ps_vap && vap_fact > 1.0f) {
            // forecast video ES vaporization factor
            // the basic formulars look like:
            // vap_fact  = total_packs/(restpacks+vop)
            // restpacks = total_packs-(video_packs+skipped_aux_packs)
            // fact      = (video_packs*net-(gops*net/2+10))/(vop*net-(gops*net/2+10))
            // net       = SECT_SIZE-(14+9)
            // 14: pack header size
            // 9:  PES header size
            // 10: PTS+DTS size in PES header of sequence header
            // You are welcome to double check everything here!
            float vop, net;
            net  = (float) (SECT_SIZE - (14+9));
            vop  = video_packs + skipped_aux_packs -
                   (float) total_packs * (1.0f-1.0f/vap_fact);
            fact = ((float) video_packs * net -
                    ((float) sequence_headers * net/2.0f + 10.0f)) /
                   (vop * net - ((float) sequence_headers * net/2.0f + 10.0f));

            //JMP
            m_totfact+=fact ;
            m_nbfact++;
            m_avgfact=m_totfact/m_nbfact;

            // requant seems to get stuck on factors < 1
            if (fact < 1.0f)
                fact = 1.0f;

            if (verbose >= 2)
                fprintf (stderr, "Info: Target video ES vaporization factor: %.3f\n",
                         fact);
        }

        vin_bytes += vilen;

        if (fact > 1.0f) {
            // do requantization
            volen = requant (vobuf, vibuf, vilen, fact);
        } else {
            // don't do requantization
            tc_memcpy (vobuf, vibuf, vilen);
            volen = vilen;
        }

        vout_bytes += volen;

        // do phase 2 of vaporization
        vap_phase2 (seq_length);

        //fprintf (stderr,
        //	       "tot=%d, vid=%d, ps1=%d, nav=%d, sv=%d, sp1=%d, fact=%.3f\n",
        //	       total_packs, video_packs, aux_packs, nav_packs,
        //	       skipped_video_packs, skipped_aux_packs, fact);
    }
}

uint64_t k9vamps::getOutputBytes() {
    return bytes_written;
}

void k9vamps::abort() {
    //fatal("vamps stopped");
    setNoData();
    if (m_requant !=NULL)
        m_requant->wait();
    if (m_bgUpdate!=NULL)
        m_bgUpdate->wait();
}

// this is a *very* sophisticated kind of error handling :-)
void
k9vamps::fatal (TQString msg) {
    m_errMsg=msg;
    m_error=true;
    if (m_requant !=NULL)
        m_requant->terminate();
    if (m_bgUpdate !=NULL)
        m_bgUpdate->terminate();
    terminate();
}

/****************************  BACKGROUND UPDATE **********************/

k9bgUpdate::k9bgUpdate(k9DVDBackup * _backup) {
    m_backup = _backup;

}

void k9bgUpdate::update(uchar *_buffer,uint32_t _size) {
    mutex.lock();
    m_buffer=(uchar*)malloc(_size);
    tc_memcpy(m_buffer,_buffer,_size);
    m_size=_size;
    start();
    mutex.unlock();
}

void k9bgUpdate::run() {
    m_backup->getOutput(m_buffer,m_size);
    free(m_buffer);
}