summaryrefslogtreecommitdiff
path: root/gst-libs/ext/mplex/inputstrm.cc
blob: 64edec54fd4766dc7c460e84f31a950f662074c3 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250

/*
 *  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 "fastintfns.h"
#include "inputstrm.hh"
#include "outputstream.hh"

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;
}



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;
}



ElementaryStream::ElementaryStream (IBitStream & ibs, OutputStream & into, stream_kind _kind):
InputStream (ibs), muxinto (into), kind (_kind), buffer_min (INT_MAX), buffer_max (1)
{
}


bool ElementaryStream::NextAU ()
{
  Aunit *
    p_au =
    next ();

  if (p_au != NULL) {
    au = p_au;
    au_unsent = p_au->length;
    return true;
  } else {
    au_unsent = 0;
    return false;
  }
}


Aunit *
ElementaryStream::Lookahead ()
{
  return aunits.lookahead ();
}

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)
{
  unsigned int actually_read = bs.read_buffered_bytes (dst, to_read);

  Muxed (actually_read);
  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) {

    bufmodel.Queued (au_unsent, decode_time);
    bytes_muxed -= au_unsent;
    if (!NextAU ())
      return;
    new_au_next_sec = true;
    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) {

    bufmodel.Queued (bytes_muxed, decode_time);
    au_unsent -= bytes_muxed;
    new_au_next_sec = false;
  } else			//  if (au_unsent == bytes_muxed)
  {
    bufmodel.Queued (bytes_muxed, decode_time);
    if (!NextAU ())
      return;
    new_au_next_sec = true;
  }

}

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


void
ElementaryStream::UpdateBufferMinMax ()
{
  buffer_min = buffer_min < (int) bufmodel.Space ()? buffer_min : bufmodel.Space ();
  buffer_max = buffer_max > (int) 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;
}

Aunit *
ElementaryStream::next ()
{
  Aunit *res;

  while (AUBufferNeedsRefill ()) {
    FillAUbuffer (FRAME_CHUNK);
  }
  res = aunits.next ();
  return res;
}




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