1 
2 /*
3  *  inputstrm.c:  Base classes related to muxing out input streams into
4  *                the output stream.
5  *
6  *  Copyright (C) 2001 Andrew Stevens <andrew.stevens@philips.com>
7  *
8  *
9  *  This program is free software; you can redistribute it and/or
10  *  modify it under the terms of version 2 of the GNU General Public License
11  *  as published by the Free Software Foundation.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22 
23 
24 #include <config.h>
25 #include <assert.h>
26 #include <limits.h>
27 
28 #include "mjpeg_types.h"
29 #include "inputstrm.hpp"
30 #include "multiplexor.hpp"
31 
MuxStream()32 MuxStream::MuxStream() : init(false)
33 {
34 }
35 
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)36 void MuxStream::Init( const int strm_id,
37 			const unsigned int _buf_scale,
38 			const unsigned int buf_size,
39 			const unsigned int _zero_stuffing,
40 			bool bufs_in_first,
41 			bool always_bufs)
42 {
43 	stream_id = strm_id;
44 	nsec = 0;
45 	zero_stuffing = _zero_stuffing;
46 	buffer_scale = _buf_scale;
47 	buffer_size = buf_size;
48 	bufmodel.Init( buf_size );
49 	buffers_in_header = bufs_in_first;
50 	always_buffers_in_header = always_bufs;
51 	new_au_next_sec = true;
52 	init = true;
53     min_pes_header_len = 0;
54 }
55 
56 
57 unsigned int
BufferSizeCode()58 MuxStream::BufferSizeCode()
59 {
60 	if( buffer_scale == 1 )
61 		return buffer_size / 1024;
62 	else if( buffer_scale == 0 )
63 		return buffer_size / 128;
64 	else
65 		assert(false);
66     return 0;                   // Never reached...
67 }
68 
69 
ElementaryStream(IBitStream & ibs,Multiplexor & into,stream_kind _kind)70 ElementaryStream::ElementaryStream( IBitStream &ibs,
71                                     Multiplexor &into, stream_kind _kind) :
72 
73     stream_length(0),
74     bs( ibs ),
75     eoscan(false),
76     last_buffered_AU(0),
77     decoding_order(0),
78     old_frames(0),
79     au(0),
80 	muxinto( into ),
81 	kind(_kind),
82     buffer_min(INT_MAX),
83     buffer_max(1)
84 {
85 }
86 
~ElementaryStream()87 ElementaryStream::~ElementaryStream ()
88 {
89     if( au != 0 )
90         delete au;
91 }
92 
93 /***********************************
94  *
95  * Scan ahead to buffer enough info on the coming Access Units to
96  * permit look-ahead of look_ahead/processing AUs forward from the
97  * current AU *and* the muxing of at least one sector.
98  *
99  **********************************/
100 
101 void
AUBufferLookaheadFill(unsigned int look_ahead)102 ElementaryStream::AUBufferLookaheadFill( unsigned int look_ahead)
103 {
104     while( !eoscan &&
105            ( look_ahead+1 > aunits.MaxAULookahead()
106              || bs.BufferedBytes() < muxinto.sector_size ) )
107     {
108         FillAUbuffer(FRAME_CHUNK);
109     }
110     if( eoscan )
111         bs.ScanDone();
112 
113 }
114 
115 /******************************************
116  *
117  * Move on to the next Access unit in the Elementary stream
118  *
119  *****************************************/
120 
121 bool
NextAU()122 ElementaryStream::NextAU()
123 {
124     // Free up no longer needed AU record
125     if( au != 0 )
126         delete au;
127     // Ensure we have enough in the AU buffer!
128     AUBufferLookaheadFill(1);
129 
130     // Get the details of the next AU to be muxed....
131 	AUnit *p_au = aunits.Next();
132 	if( p_au != NULL )
133 	{
134 
135 		au = p_au;
136 		au_unsent = p_au->length;
137 		return true;
138 	}
139 	else
140 	{
141         // We signal no MORE AU left to Mux in this stream...
142 		au_unsent = 0;
143         au = 0;
144 		return false;
145 	}
146 }
147 
148 
149 AUnit *
Lookahead(unsigned int n)150 ElementaryStream::Lookahead( unsigned int n)
151 {
152     AUBufferLookaheadFill(n);
153     return aunits.Lookahead( n );
154 }
155 
156 unsigned int
BytesToMuxAUEnd(unsigned int sector_transport_size)157 ElementaryStream::BytesToMuxAUEnd(unsigned int sector_transport_size)
158 {
159 	return (au_unsent/min_packet_data)*sector_transport_size +
160 		(au_unsent%min_packet_data)+(sector_transport_size-min_packet_data);
161 }
162 
163 
164 /******************************************************************
165  *	ElementaryStream::ReadPacketPayload
166  *
167  *  Reads the stream data from actual input stream, updates decode
168  *  buffer model and current access unit information from the
169  *  look-ahead scanning buffer to account for bytes_muxed bytes being
170  *  muxed out.  Particular important is the maintenance of "au_unsent"
171  *  the count of how much data in the current AU remains umuxed.  It
172  *  not only allows us to keep track of AU's but is also used for
173  *  generating substream headers
174  *
175  *  Unless we need to over-ride it to handle sub-stream headers
176  * The packet payload for an elementary stream is simply the parsed and
177  * spliced buffered stream data..
178  *
179  ******************************************************************/
180 
181 
182 unsigned int
ReadPacketPayload(uint8_t * dst,unsigned int to_read)183 ElementaryStream::ReadPacketPayload(uint8_t *dst, unsigned int to_read)
184 {
185     //
186     // Allow for the possibility that stream sub-headers might be needed
187     // E.g. AC3, LPCM, DTS....
188     unsigned int header_size = StreamHeaderSize();
189     bitcount_t read_start = bs.GetBytePos();
190     unsigned int actually_read = bs.GetBytes( dst+header_size, to_read-header_size );
191     bs.Flush( read_start );
192     Muxed( actually_read );
193     ReadStreamHeader(dst, header_size);
194 	return actually_read;
195 }
196 
Muxed(unsigned int bytes_muxed)197 void ElementaryStream::Muxed (unsigned int bytes_muxed)
198 {
199 	clockticks   decode_time;
200 
201 	if (bytes_muxed == 0 || MuxCompleted() )
202 		return;
203 
204 
205 	/* Work through what's left of the current AU and the following AU's
206 	   updating the info until we reach a point where an AU had to be
207 	   split between packets.
208 	   NOTE: It *is* possible for this loop to iterate.
209 
210 	   The DTS/PTS field for the packet in this case would have been
211 	   given the that for the first AU to start in the packet.
212 	   Whether Joe-Blow's hardware VCD player handles this properly is
213 	   another matter of course!
214 	*/
215 
216 	decode_time = RequiredDTS();
217 	while (au_unsent < bytes_muxed)
218 	{
219         AUMuxed(true);          // Update stream specific tracking
220                                 // of AUs muxed...
221 		bufmodel.Queued(au_unsent, decode_time);
222 		bytes_muxed -= au_unsent;
223         new_au_next_sec = NextAU();
224         if( !new_au_next_sec )
225 			return;
226 		decode_time = RequiredDTS();
227 	};
228 
229 	// We've now reached a point where the current AU overran or
230 	// fitted exactly.  We need to distinguish the latter case
231 	// so we can record whether the next packet starts with an
232 	// existing AU or not - info we need to decide what PTS/DTS
233 	// info to write at the start of the next packet.
234 
235 	if (au_unsent > bytes_muxed)
236 	{
237         AUMuxed(false);
238 		bufmodel.Queued( bytes_muxed, decode_time);
239 		au_unsent -= bytes_muxed;
240 		new_au_next_sec = false;
241 	}
242 	else //  if (au_unsent == bytes_muxed)
243 	{
244         AUMuxed(false);
245 		bufmodel.Queued(bytes_muxed, decode_time);
246 		new_au_next_sec = NextAU();
247 	}
248 
249 }
250 
MuxPossible(clockticks currentSCR)251 bool ElementaryStream::MuxPossible(clockticks currentSCR)
252 {
253 	return (!RunOutComplete() &&
254 			bufmodel.Space() > max_packet_data);
255 }
256 
UpdateBufferMinMax()257 void ElementaryStream::UpdateBufferMinMax()
258 {
259     buffer_min =  buffer_min < bufmodel.Space() ?
260         buffer_min : bufmodel.Space();
261     buffer_max = buffer_max > bufmodel.Space() ?
262         buffer_max : bufmodel.Space();
263 }
264 
265 
AllDemuxed()266 void ElementaryStream::AllDemuxed()
267 {
268 	bufmodel.Flushed();
269 }
270 
DemuxedTo(clockticks SCR)271 void ElementaryStream::DemuxedTo( clockticks SCR )
272 {
273 	bufmodel.Cleaned( SCR );
274 }
275 
MuxCompleted()276 bool ElementaryStream::MuxCompleted()
277 {
278 	return au_unsent == 0;
279 }
280 
281 void
SetSyncOffset(clockticks sync_offset)282 ElementaryStream::SetSyncOffset( clockticks sync_offset )
283 {
284 	timestamp_delay = sync_offset;
285 }
286 
BufferAndOutputSector()287 void ElementaryStream::BufferAndOutputSector( )
288 {
289     AUBufferLookaheadFill(1);   // TODO is this really needed here?
290     OutputSector();
291 }
292 
293 
294 /*
295  * Local variables:
296  *  c-file-style: "stroustrup"
297  *  tab-width: 4
298  *  indent-tabs-mode: nil
299  * End:
300  */
301