File: subpstream.cpp

package info (click to toggle)
mjpegtools 1%3A2.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,460 kB
  • sloc: ansic: 60,386; cpp: 32,062; sh: 5,978; makefile: 751; asm: 103
file content (392 lines) | stat: -rw-r--r-- 9,912 bytes parent folder | download
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
//
// C++ Implementation: subpstream
//
// Description:
//
//
// Author: grotti <grotti@verstehts.net>, (C) 2010
//
// Copyright: See COPYING file that comes with this distribution
// Most of the code is stolen from ac3stream.cpp
//
//
#include <config.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "audiostrm.hpp"
#include "interact.hpp"
#include "multiplexor.hpp"
// minimum version code for subtitle stream that
// can be handled by the program
#define MIN_VERSION_CODE 0x00030000
// common part of the subtitle header struct for major version 3
// taken from subtitle2vobsub

typedef struct {

    unsigned int header_length;
    unsigned int header_version;
    unsigned int payload_length;

    unsigned int lpts;
    double rpts;

    // version 0x00030001
    unsigned int discont_ctr;

} subtitle_header_v3_t;

typedef struct vobsub_header_s {

    char marker[8];
    subtitle_header_v3_t header;
} vobsub_header ;

static unsigned int minor_version(unsigned int version)
{
    // bit 0-15 contain the minor version number
    return version & 0xffff;
}

// get the major version number from the version code
static unsigned int major_version(unsigned int version)
{
    // bit 16-31 contain the major version number
    return version >> 16;
}


static const char* SUBPHEADER="SUBTITLE";

SUBPStream::SUBPStream(IBitStream &ibs, SubtitleStreamParams* subpparm, Multiplexor &into) :
    AudioStream( ibs, into ),parms(subpparm)

{
    kind =  ElementaryStream::subtitle;
    num_frames =0;
    initial_offset = -1;
    last_sub_stream_id =-1;

}




bool SUBPStream::Probe(IBitStream &bs )
{
    //char *last_dot = strrchr( bs, '.' );
    char buffer[20];
    bs.GetBytes((uint8_t*)buffer,strlen(SUBPHEADER));
    if (strncmp(buffer,SUBPHEADER,strlen(SUBPHEADER)) == 0)
        return true;
    return false;


}

void SUBPStream::Init(const int stream_num)
{
    MuxStream::Init( PRIVATE_STR_1,
                     1,  // Buffer scale
                     8192, // default buffer size
                     false,
                     muxinto.buffers_in_audio,
                     muxinto.always_buffers_in_audio
                   );
    mjpeg_info ("Scanning for header info: Subpicture stream %02x (%s)",
                stream_num,
                bs.StreamName()
               );
    sub_stream_id =parms->StreamId();

    ParseAUBitwise();

}

bool SUBPStream::CheckAndSkipHeader( vobsub_header& vobsub, bool bitwise)
{


    uint8_t* ptr = reinterpret_cast<uint8_t*> (&vobsub);
    subtitle_header_v3_t &header = vobsub.header;
    int i;

    if (bitwise)
    {

        for (i=0; i<sizeof(vobsub); i++)
            *(ptr+i) = bs.GetBits(8);
    }
    else
    {
        bs.GetBytes((uint8_t*) &vobsub,sizeof(vobsub));
    }

    if (strncmp(vobsub.marker,SUBPHEADER,strlen(SUBPHEADER)) != 0)
    {
        mjpeg_error( "Subtitle: expected header %s!",SUBPHEADER);
        return false;

    }

    if (major_version(header.header_version) != major_version(MIN_VERSION_CODE))
    {
        mjpeg_error( "Subtitle: expected version 0x%08X, got version 0x%08X while reading subtitle header!",MIN_VERSION_CODE,header.header_version);
        return false;
    }
    int16_t skip_len = header.header_length-sizeof(header);

    if (skip_len)
    {
        assert (skip_len>0);
        if (bitwise)
            bs.SeekFwdBits(skip_len);
        else
        {
            uint8_t* b = (uint8_t* ) malloc(skip_len);

            bs.GetBytes(b,skip_len);
            free (b);
        }
    }
    return true;

}


bool SUBPStream::ParseAUBitwise()
{
    vobsub_header vobsub;


    if (!CheckAndSkipHeader(vobsub, true))
        return false;
    subtitle_header_v3_t &header = vobsub.header;
    uint8_t subpid;
    prev_offset = AU_start;
    AU_start = bs.bitcount();
    // packet starts here...
    subpid = bs.GetBits(8);


    access_unit.start = AU_start;
    access_unit.length = header.payload_length;
    access_unit.PTS=header.rpts*CLOCKS;
    if(header.rpts > 0) {


        if (initial_offset == -1)
        {
            if (sub_stream_id == -1)
            {
                sub_stream_id = subpid;
                last_sub_stream_id =subpid;
            }
            mjpeg_info( "SUBTITLE id 0x%02X => 0x%02X", subpid,sub_stream_id);
            initial_offset = header.rpts*CLOCKS;
            mjpeg_info("Stream  offset is :       %lld (PTS)",access_unit.PTS);
            mjpeg_info("Initial offset is :       %lld (PTS)",initial_offset);
            mjpeg_info("Cmd line offset is:       %lld (PTS)",parms->Offset());
            initial_offset -= parms->Offset();
            mjpeg_info("Adjustment offset :       %lld (PTS)",initial_offset);
//			mjpeg_info("fist sector scr   :       %f   (LPTS)",((double)header.lpts)/CLOCKS);
        }
        access_unit.PTS-=initial_offset;
        //access_unit.PTS-=initial_offset;


    } else {
        // calculate the time from lpts
        mjpeg_info( "Subtitle %d: fallback to lpts", subpid);
        access_unit.PTS= ((double)header.lpts)/CLOCKS -initial_offset;
    }
    if (subpid != last_sub_stream_id)
        mjpeg_info("Subtitle id changes from 0x%02X to 0x%02X in during muxing, is that realy what you want?",last_sub_stream_id, subpid);

    access_unit.DTS = access_unit.PTS;
    access_unit.dorder = decoding_order;

    decoding_order++;
    aunits.Append( access_unit );


    bs.SeekFwdBits(header.payload_length-1);
    num_frames++;
    return true;
}

void SUBPStream::Close()
{
    stream_length = AU_start >> 3;
    mjpeg_info ("SUBTITLE STATISTICS:  0x%02x", sub_stream_id);
    mjpeg_info ("Subtitle stream length  %lld bytes.", stream_length);
    mjpeg_info ("Nr. of subtitle packets read: %d, wrote: %d",  num_frames, nsec);

}

void SUBPStream::FillAUbuffer(unsigned int frames_to_buffer)
{
    uint32_t discont_ctr;
    last_buffered_AU += frames_to_buffer;
    mjpeg_debug( "Scanning %d Subpicture frames to frame %d",
                 frames_to_buffer, last_buffered_AU );
    prev_offset = AU_start;
    AU_start = bs.bitcount();
    while (decoding_order < last_buffered_AU && !bs.eos()
            && !muxinto.AfterMaxPTS(access_unit.PTS))
    {
        if (!ParseAUBitwise())
            break;

    }

    last_buffered_AU = decoding_order;
    eoscan = bs.eos() || muxinto.AfterMaxPTS(access_unit.PTS);
}

unsigned int SUBPStream::ReadPacketPayload(uint8_t *dst, unsigned int to_read)
{
    bool finished_pack = true;
    bitcount_t read_start = bs.GetBytePos();
    mjpeg_debug( "SUBPStream called: ReadPacketPayload at 0x%08lld", read_start);
    unsigned int bytes_max, bytes_read;
    unsigned int bytes_muxed = bytes_read;
    if (new_au_next_sec == true)
    {   // check and skip header

        vobsub_header vobsub;
        if (!CheckAndSkipHeader(vobsub, false))
            return 0;


        bytes_max = (au_unsent<to_read)?au_unsent:to_read;
        bytes_read = bs.GetBytes( dst, bytes_max);
        if (au_unsent>bytes_read)
        {

            finished_pack=false;
        }
        else
        {

            finished_pack=true;
        }
        bs.Flush( read_start );
        dst[0] = sub_stream_id;
        bytes_muxed = bytes_read;
    }
    else
    {
        // continious block
        bytes_max = (au_unsent<to_read-1)?au_unsent:to_read-1;
        bytes_read = bs.GetBytes( dst+1, bytes_max);
        dst[0] = sub_stream_id;
        if (bytes_read == au_unsent)
            finished_pack=true;
        bytes_muxed = bytes_read+1;

        bs.Flush( read_start );

    }



    clockticks   decode_time;


    if (bytes_muxed == 0 || MuxCompleted() )
    {
        goto completion;
    }


    /* Check if we're finished with that specific pack

    */


    decode_time = RequiredDTS();
    mjpeg_debug("SUBPStream: Required DTS is %f",decode_time/300.0);

    if (!finished_pack)
    {
        bufmodel.Queued( bytes_muxed, decode_time);
        au_unsent -= bytes_muxed;
        if (!new_au_next_sec)
            au_unsent++;
        new_au_next_sec = false;
    }
    else //  if (au_unsent == bytes_muxed)
    {
        bufmodel.Queued(bytes_muxed, decode_time);
        new_au_next_sec = NextAU();
    }
completion:

    return bytes_muxed;
    //return bytes_read;
}
void SUBPStream::OutputSector ( )

{
    clockticks   PTS;
    unsigned int max_packet_data;
    unsigned int actual_payload;
    unsigned int old_au_then_new_payload;

    PTS = RequiredDTS();
    old_au_then_new_payload =
        muxinto.PacketPayload( *this, buffers_in_header, false, false );
    bool last_packet = Lookahead() == 0;
    // Ensure we have access units data buffered to allow a sector to be
    // written.
    max_packet_data = 0;
    if( (muxinto.running_out && NextRequiredPTS() > muxinto.runout_PTS)
            || last_packet)
    {
        /* We're now in the last AU of a segment.  So we don't want to
           go beyond it's end when writing sectors. Hence we limit
           packet payload size to (remaining) AU length.
        */
        max_packet_data = au_unsent+StreamHeaderSize();
    }

    /* CASE: packet starts with new access unit			*/


    if (new_au_next_sec)
    {

        actual_payload =
            muxinto.WritePacket ( max_packet_data,
                                  *this,
                                  buffers_in_header, PTS, 0,
                                  TIMESTAMPBITS_PTS);

    }


    /* CASE: packet starts with old access unit, no new one	*/
    /*       starts in this very same packet			*/
    /* for subtitles: never start a new subtitle in the same packet */
    else
    {



        actual_payload =
            muxinto.WritePacket ( max_packet_data,
                                  *this,
                                  buffers_in_header, 0, 0,
                                  TIMESTAMPBITS_NO );
    }



    ++nsec;

    buffers_in_header = always_buffers_in_header;

}