File: inputstrm.cpp

package info (click to toggle)
mjpegtools 1%3A2.1.0%2Bdebian-8.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,940 kB
  • sloc: ansic: 60,401; cpp: 32,321; sh: 13,910; makefile: 763; python: 291; asm: 103
file content (300 lines) | stat: -rw-r--r-- 7,552 bytes parent folder | download | duplicates (5)
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

/*
 *  inputstrm.c:  Base classes related to muxing out input streams into
 *                the output stream.
 *
 *  Copyright (C) 2001 Andrew Stevens <andrew.stevens@philips.com>
 *
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of version 2 of the GNU General Public License
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */


#include <config.h>
#include <assert.h>
#include <limits.h>

#include "mjpeg_types.h"
#include "inputstrm.hpp"
#include "multiplexor.hpp"

MuxStream::MuxStream() : init(false) 
{
}

void MuxStream::Init( const int strm_id, 
			const unsigned int _buf_scale,
			const unsigned int buf_size,
			const unsigned int _zero_stuffing,
			bool bufs_in_first, 
			bool always_bufs) 
{
	stream_id = strm_id;
	nsec = 0;
	zero_stuffing = _zero_stuffing;
	buffer_scale = _buf_scale;
	buffer_size = buf_size;
	bufmodel.Init( buf_size );
	buffers_in_header = bufs_in_first;
	always_buffers_in_header = always_bufs;
	new_au_next_sec = true;
	init = true;
    min_pes_header_len = 0;
}


unsigned int 
MuxStream::BufferSizeCode()
{
	if( buffer_scale == 1 )
		return buffer_size / 1024;
	else if( buffer_scale == 0 )
		return buffer_size / 128;
	else
		assert(false);
    return 0;                   // Never reached...
}


ElementaryStream::ElementaryStream( IBitStream &ibs,
                                    Multiplexor &into, stream_kind _kind) : 

    stream_length(0),
    bs( ibs ),
    eoscan(false),
    last_buffered_AU(0),
    decoding_order(0),
    old_frames(0),
    au(0),
	muxinto( into ),
	kind(_kind),
    buffer_min(INT_MAX),
    buffer_max(1)
{
}

ElementaryStream::~ElementaryStream ()
{
    if( au != 0 )
        delete au;
}

/***********************************
 *
 * Scan ahead to buffer enough info on the coming Access Units to
 * permit look-ahead of look_ahead/processing AUs forward from the
 * current AU *and* the muxing of at least one sector.
 *
 **********************************/

void 
ElementaryStream::AUBufferLookaheadFill( unsigned int look_ahead)
{
    while( !eoscan &&
           ( look_ahead+1 > aunits.MaxAULookahead() 
             || bs.BufferedBytes() < muxinto.sector_size ) )
    {
        FillAUbuffer(FRAME_CHUNK);
    }
    if( eoscan )
        bs.ScanDone();
        
}

/******************************************
 *
 * Move on to the next Access unit in the Elementary stream
 *
 *****************************************/

bool 
ElementaryStream::NextAU()
{
    // Free up no longer needed AU record
    if( au != 0 )
        delete au;
    // Ensure we have enough in the AU buffer!
    AUBufferLookaheadFill(1);

    // Get the details of the next AU to be muxed....
	AUnit *p_au = aunits.Next();
	if( p_au != NULL )
	{

		au = p_au;
		au_unsent = p_au->length;
		return true;
	}
	else
	{
        // We signal no MORE AU left to Mux in this stream...
		au_unsent = 0;
        au = 0;
		return false;
	}
}


AUnit *
ElementaryStream::Lookahead( unsigned int n)
{
    AUBufferLookaheadFill(n);
    return aunits.Lookahead( n );
}

unsigned int 
ElementaryStream::BytesToMuxAUEnd(unsigned int sector_transport_size)
{
	return (au_unsent/min_packet_data)*sector_transport_size +
		(au_unsent%min_packet_data)+(sector_transport_size-min_packet_data);
}


/******************************************************************
 *	ElementaryStream::ReadPacketPayload
 *
 *  Reads the stream data from actual input stream, updates decode
 *  buffer model and current access unit information from the
 *  look-ahead scanning buffer to account for bytes_muxed bytes being
 *  muxed out.  Particular important is the maintenance of "au_unsent"
 *  the count of how much data in the current AU remains umuxed.  It
 *  not only allows us to keep track of AU's but is also used for
 *  generating substream headers
 *
 *  Unless we need to over-ride it to handle sub-stream headers
 * The packet payload for an elementary stream is simply the parsed and
 * spliced buffered stream data..
 *
 ******************************************************************/


unsigned int 
ElementaryStream::ReadPacketPayload(uint8_t *dst, unsigned int to_read)
{
    //
    // Allow for the possibility that stream sub-headers might be needed
    // E.g. AC3, LPCM, DTS....
    unsigned int header_size = StreamHeaderSize();
    bitcount_t read_start = bs.GetBytePos();
    unsigned int actually_read = bs.GetBytes( dst+header_size, to_read-header_size );
    bs.Flush( read_start );
    Muxed( actually_read );
    ReadStreamHeader(dst, header_size);
	return actually_read;
}

void ElementaryStream::Muxed (unsigned int bytes_muxed)
{
	clockticks   decode_time;
  
	if (bytes_muxed == 0 || MuxCompleted() )
		return;


	/* Work through what's left of the current AU and the following AU's
	   updating the info until we reach a point where an AU had to be
	   split between packets.
	   NOTE: It *is* possible for this loop to iterate. 

	   The DTS/PTS field for the packet in this case would have been
	   given the that for the first AU to start in the packet.
	   Whether Joe-Blow's hardware VCD player handles this properly is
	   another matter of course!
	*/

	decode_time = RequiredDTS();
	while (au_unsent < bytes_muxed)
	{	  
        AUMuxed(true);          // Update stream specific tracking 
                                // of AUs muxed...
		bufmodel.Queued(au_unsent, decode_time);
		bytes_muxed -= au_unsent;
        new_au_next_sec = NextAU();
        if( !new_au_next_sec )
			return;
		decode_time = RequiredDTS();
	};

	// We've now reached a point where the current AU overran or
	// fitted exactly.  We need to distinguish the latter case
	// so we can record whether the next packet starts with an
	// existing AU or not - info we need to decide what PTS/DTS
	// info to write at the start of the next packet.
	
	if (au_unsent > bytes_muxed)
	{
        AUMuxed(false);
		bufmodel.Queued( bytes_muxed, decode_time);
		au_unsent -= bytes_muxed;
		new_au_next_sec = false;
	} 
	else //  if (au_unsent == bytes_muxed)
	{
        AUMuxed(false);
		bufmodel.Queued(bytes_muxed, decode_time);
		new_au_next_sec = NextAU();
	}	   

}

bool ElementaryStream::MuxPossible(clockticks currentSCR)
{
	return (!RunOutComplete() &&
			bufmodel.Space() > max_packet_data);
}

void ElementaryStream::UpdateBufferMinMax()
{
    buffer_min =  buffer_min < bufmodel.Space() ? 
        buffer_min : bufmodel.Space();
    buffer_max = buffer_max > bufmodel.Space() ? 
        buffer_max : bufmodel.Space();
}


void ElementaryStream::AllDemuxed()
{
	bufmodel.Flushed();
}

void ElementaryStream::DemuxedTo( clockticks SCR )
{
	bufmodel.Cleaned( SCR );
}

bool ElementaryStream::MuxCompleted()
{
	return au_unsent == 0;
}

void 
ElementaryStream::SetSyncOffset( clockticks sync_offset )
{
	timestamp_delay = sync_offset;
}

void ElementaryStream::BufferAndOutputSector( )
{
    AUBufferLookaheadFill(1);   // TODO is this really needed here?
    OutputSector();
}


/* 
 * Local variables:
 *  c-file-style: "stroustrup"
 *  tab-width: 4
 *  indent-tabs-mode: nil
 * End:
 */