summaryrefslogtreecommitdiff
path: root/ts2pes.cpp
blob: c75b6727d8c1db28b899adc6ceeda60556db5574 (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
/*
 * ts2pes.cpp: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include "ts2pes.h"

#include <stdio.h>

cMarkAdTS2PES::cMarkAdTS2PES()
{
    pesdata=NULL;
    pesdatalast=NULL;
    Reset();
}

cMarkAdTS2PES::~cMarkAdTS2PES()
{
    if (pesdata) free(pesdata);
    if (pesdatalast) free(pesdatalast);
}

void cMarkAdTS2PES::Reset()
{
    if (pesdata) free(pesdata);
    pesdata=NULL;
    pessize=0;
    data_left=false;
    streamsize=0;
    counter=-1;
    sync=false;
}

int cMarkAdTS2PES::FindPESHeader(uchar *TSData, int TSSize, int *StreamSize)
{
    unsigned long scanner=0xFFFFFFFF;
    int i;
    for (i=0; i<TSSize; i++)
    {
        scanner<<=8;
        if (scanner==0x00000100)
        {
            break;
        }
        scanner|=TSData[i];
    }
    if (i!=TSSize)
    {
        if (StreamSize)
        {
            if (TSData[i]>=0xBC)
            {
                *StreamSize=(TSData[i+1]<<8)+TSData[i+2];
                if (*StreamSize) (*StreamSize)+=6; // 6 Byte PES-Header
            }
        }
        return i-3;
    }
    return -1;
}

int cMarkAdTS2PES::Process(int Pid, uchar *TSData, int TSSize, uchar **PESData, int *PESSize)
{
    if ((!PESData) || (!PESSize)) return -1;
    *PESData=NULL;
    *PESSize=0;

    int buflen=TS_SIZE+1;
    uchar *buf=NULL;

    int bytes_processed;

    if (!data_left)
    {
        // search for TS packet sync
        int i;
        for (i=0; i<TSSize; i++)
        {
            if (TSData[i]==0x47) break;
        }
        if (i==TSSize)
        {
            Reset();
            return TSSize;
        }
        TSData+=i;
        TSSize-=i;

        struct TSHDR *tshdr = (struct TSHDR *) TSData;

        int pid = (tshdr->PidH << 8) | tshdr->PidL;
        if (Pid!=pid)
        {
            return TS_SIZE; // not for us
        }

        if ((tshdr->PayloadStart==0) && (!sync))
        {
            return TS_SIZE;
        }
        else
        {
            sync=true;
        }

        if ((counter!=-1) && (((counter+1) & 0xF)!=tshdr->Counter))
        {
            // sequence error
            Reset();
            return TS_SIZE;
        }
        counter=tshdr->Counter;

        if ((tshdr->AFC<=0) || (tshdr->AFC>3))
        {
            Reset();
            return TS_SIZE;
        }

        // we just ignore the infos in the adaption field (e.g. OPCR/PCR)
        if ((tshdr->AFC!=1) && (tshdr->AFC!=3))
        {
            return TS_SIZE;
        }

        if (tshdr->AFC==1)
        {
            // payload only
            buflen=TS_SIZE-sizeof(struct TSHDR);
            buf=&TSData[sizeof(struct TSHDR)];
        }

        if (tshdr->AFC==3)
        {
            // adaption field + payload
            struct TSADAPT *tsadapt = (struct TSADAPT *) &TSData[4];
            int alen=tsadapt->Len+1;
            buflen=TS_SIZE-(sizeof(struct TSHDR)+alen);
            buf=&TSData[sizeof(struct TSHDR)+alen];
        }

        if (buflen>TS_SIZE)
        {
            // size to large
            Reset();
            return TS_SIZE;
        }
        bytes_processed=TS_SIZE-buflen;

        pesdata=(uchar *) realloc(pesdata,pessize+buflen);
        if (!pesdata)
        {
            pessize=0;
            return -1;
        }
        memcpy(pesdata+pessize,buf,buflen);
        pessize+=buflen;
        bytes_processed+=buflen;
    }
    else
    {
        bytes_processed=pessize;
        data_left=false;
    }

    int peshdr=FindPESHeader(pesdata, pessize, &streamsize);
    if (peshdr==0)
    {
        if (!streamsize)
        {
            peshdr=FindPESHeader(pesdata+3,pessize-3,NULL);
            if (peshdr>0) peshdr+=3;
        }
        else
        {
            if (pessize>streamsize)
            {
                int size=pessize-streamsize;

                *PESData=pesdata;
                *PESSize=streamsize;
                if (pesdatalast) free(pesdatalast);
                pesdatalast=pesdata;
                pesdata=NULL;
                pessize=0;

                void *ptr=malloc(size);
                if (!ptr) return -1;
                memcpy(ptr,(*PESData)+streamsize,size);
                bytes_processed-=size;
                pessize=size;
                pesdata=(uchar *) ptr;
                data_left=true;
                streamsize=0;
            }
        }
    }
    if (peshdr>0)
    {
        // start of next PES paket found
        if (pesdata)
        {
            // return old data
            *PESData=pesdata;
            *PESSize=peshdr;
            if (pesdatalast) free(pesdatalast);
            pesdatalast=pesdata;
            int size=pessize-peshdr;
            pesdata=NULL;
            pessize=0;

            if (size>0)
            {
                void *ptr=malloc(size);
                if (!ptr) return -1;
                memcpy(ptr,(*PESData)+peshdr,size);
                bytes_processed-=size;
                pessize=size;
                pesdata=(uchar *) ptr;
                data_left=true;
            }
            else
            {
                // TODO: not sure if this is ok
                bytes_processed-=size;
            }
        }
    }
    return bytes_processed;
}