summaryrefslogtreecommitdiffstats
path: root/k9decmpeg/k9decodethread.cpp
blob: a7e54989ac3bcb105123163c264c38aaa82a5893 (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
//
// C++ Implementation: k9decodethread
//
// Description: 
//
//
// Author: Jean-Michel PETIT <k9copy@free.fr>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "k9decodethread.h"

k9DecodeThread::k9DecodeThread()
{
   m_decoder=new kDecMPEG2(this);
   noData=FALSE;
}


k9DecodeThread::~k9DecodeThread()
{
  delete m_decoder;
}

void k9DecodeThread::clear() {
    m_fifo.clear();
    wDataRead.wakeAll();
    wDataReady.wakeAll();

}

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


int k9DecodeThread::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)
	// oterwise, 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 k9DecodeThread::setNoData() {
    noData=true;
    wDataRead.wakeAll();
    wDataReady.wakeAll();
}

void k9DecodeThread::sleepms(int _ms) {
   msleep(_ms);
} 

void k9DecodeThread::run() {
   noData=FALSE;
   m_decoder->start();
   while (1) {
	int count=2048;
	uchar buffer[count];
	uint32_t size=readData(buffer,count);
	if (size==0)
	break;
	m_decoder->decode(buffer ,buffer+size,0);
   }
   m_decoder->stop();

}