~oah-dev/oah/gst-plugins-bad

« back to all changes in this revision

Viewing changes to gst/mpeg1sys/systems.c

  • Committer: Haakon Sporsheim
  • Date: 2009-03-12 13:52:03 UTC
  • Revision ID: haakon.sporsheim@tandberg.com-20090312135203-i5k294hgkushb0mt
Initial import of git repository: git://anongit.freedesktop.org/gstreamer/gst-plugins-bad (tag: RELEASE-0_10_10)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifdef HAVE_CONFIG_H
 
2
#include "config.h"
 
3
#endif
 
4
 
 
5
#include <string.h>
 
6
 
 
7
#include "main.h"
 
8
 
 
9
static void
 
10
buffer_timecode (timecode, marker, buffer)
 
11
     guint64 timecode;
 
12
     unsigned char marker;
 
13
     unsigned char **buffer;
 
14
 
 
15
{
 
16
  unsigned char temp;
 
17
 
 
18
  temp = (marker << 4) | ((timecode >> 29) & 0x38) |
 
19
      ((timecode >> 29) & 0x6) | 1;
 
20
  *((*buffer)++) = temp;
 
21
  temp = (timecode & 0x3fc00000) >> 22;
 
22
  *((*buffer)++) = temp;
 
23
  temp = ((timecode & 0x003f8000) >> 14) | 1;
 
24
  *((*buffer)++) = temp;
 
25
  temp = (timecode & 0x7f80) >> 7;
 
26
  *((*buffer)++) = temp;
 
27
  temp = ((timecode & 0x007f) << 1) | 1;
 
28
  *((*buffer)++) = temp;
 
29
}
 
30
 
 
31
/*************************************************************************
 
32
        creates a complete sector.
 
33
        Also copies Pack and Sys_Header informations into the
 
34
        sector buffer, then reads a packet full of data from
 
35
        the input stream into the sector buffer.
 
36
*************************************************************************/
 
37
 
 
38
 
 
39
void
 
40
create_sector (sector, pack, sys_header,
 
41
    packet_size, inputbuffer, type,
 
42
    buffer_scale, buffer_size, buffers, PTS, DTS, timestamps, which_streams)
 
43
 
 
44
     Sector_struc *sector;
 
45
     Pack_struc *pack;
 
46
     Sys_header_struc *sys_header;
 
47
     unsigned int packet_size;
 
48
     unsigned char *inputbuffer;
 
49
 
 
50
     unsigned char type;
 
51
     unsigned char buffer_scale;
 
52
     unsigned int buffer_size;
 
53
     unsigned char buffers;
 
54
     guint64 PTS;
 
55
     guint64 DTS;
 
56
     unsigned char timestamps;
 
57
     unsigned int which_streams;
 
58
 
 
59
{
 
60
  int i, j, tmp;
 
61
  unsigned char *index;
 
62
  unsigned char *size_offset;
 
63
 
 
64
  /* printf("creating sector\n"); */
 
65
 
 
66
  index = sector->buf;
 
67
  sector->length_of_sector = 0;
 
68
 
 
69
/* Should we copy Pack Header information ? */
 
70
 
 
71
  if (pack != NULL) {
 
72
    i = sizeof (pack->buf);
 
73
    memcpy (index, pack->buf, i);
 
74
    index += i;
 
75
    sector->length_of_sector += i;
 
76
  }
 
77
 
 
78
/* Should we copy System Header information ? */
 
79
 
 
80
  if (sys_header != NULL) {
 
81
    i = sizeof (sys_header->buf);
 
82
 
 
83
    /* only one stream? 3 bytes less in sys header */
 
84
    if (which_streams != STREAMS_BOTH)
 
85
      i -= 3;
 
86
 
 
87
    memcpy (index, sys_header->buf, i);
 
88
    index += i;
 
89
    sector->length_of_sector += i;
 
90
  }
 
91
 
 
92
  /* write constant packet header data */
 
93
 
 
94
  *(index++) = (unsigned char) (PACKET_START) >> 16;
 
95
  *(index++) = (unsigned char) (PACKET_START & 0x00ffff) >> 8;
 
96
  *(index++) = (unsigned char) (PACKET_START & 0x0000ff);
 
97
  *(index++) = type;
 
98
 
 
99
  /* we remember this offset in case we will have to shrink this packet */
 
100
 
 
101
  size_offset = index;
 
102
  *(index++) = (unsigned char) ((packet_size - PACKET_HEADER_SIZE) >> 8);
 
103
  *(index++) = (unsigned char) ((packet_size - PACKET_HEADER_SIZE) & 0xff);
 
104
 
 
105
  *(index++) = STUFFING_BYTE;
 
106
  *(index++) = STUFFING_BYTE;
 
107
  *(index++) = STUFFING_BYTE;
 
108
 
 
109
  i = 0;
 
110
 
 
111
  if (!buffers)
 
112
    i += 2;
 
113
  if (timestamps == TIMESTAMPS_NO)
 
114
    i += 9;
 
115
  else if (timestamps == TIMESTAMPS_PTS)
 
116
    i += 5;
 
117
 
 
118
  /* printf("%i stuffing %d\n", i, timestamps); */
 
119
 
 
120
  for (j = 0; j < i; j++)
 
121
    *(index++) = STUFFING_BYTE;
 
122
 
 
123
  /* should we write buffer info ? */
 
124
 
 
125
  if (buffers) {
 
126
    *(index++) = (unsigned char) (0x40 |
 
127
        (buffer_scale << 5) | (buffer_size >> 8));
 
128
    *(index++) = (unsigned char) (buffer_size & 0xff);
 
129
  }
 
130
 
 
131
  /* should we write PTS, PTS & DTS or nothing at all ? */
 
132
 
 
133
  switch (timestamps) {
 
134
    case TIMESTAMPS_NO:
 
135
      *(index++) = MARKER_NO_TIMESTAMPS;
 
136
      break;
 
137
    case TIMESTAMPS_PTS:
 
138
      buffer_timecode (PTS, MARKER_JUST_PTS, &index);
 
139
      sector->TS = PTS;
 
140
      break;
 
141
    case TIMESTAMPS_PTS_DTS:
 
142
      buffer_timecode (PTS, MARKER_PTS, &index);
 
143
      buffer_timecode (DTS, MARKER_DTS, &index);
 
144
      sector->TS = DTS;
 
145
      break;
 
146
  }
 
147
 
 
148
/* read in packet data */
 
149
 
 
150
  i = (packet_size - PACKET_HEADER_SIZE - AFTER_PACKET_LENGTH);
 
151
 
 
152
  if (type == PADDING_STR) {
 
153
    for (j = 0; j < i; j++)
 
154
      *(index++) = (unsigned char) STUFFING_BYTE;
 
155
    tmp = i;
 
156
  } else {
 
157
    /*tmp = fread (index, sizeof (unsigned char), i, inputstream); */
 
158
    memcpy (index, inputbuffer, i);
 
159
    tmp = i;
 
160
    index += tmp;
 
161
 
 
162
    /* if we did not get enough data bytes, shorten the Packet length */
 
163
 
 
164
    if (tmp != i) {
 
165
      packet_size -= (i - tmp);
 
166
      *(size_offset++) =
 
167
          (unsigned char) ((packet_size - PACKET_HEADER_SIZE) >> 8);
 
168
      *(size_offset++) =
 
169
          (unsigned char) ((packet_size - PACKET_HEADER_SIZE) & 0xff);
 
170
 
 
171
/* zero byte stuffing in the last Packet of a stream */
 
172
/* we don't need this any more, since we shortenend the packet */
 
173
/*          for (j=tmp; j<i; j++) */
 
174
/*              *(index++)=(unsigned char) ZERO_STUFFING_BYTE; */
 
175
    }
 
176
  }
 
177
 
 
178
 
 
179
  /* write other struct data */
 
180
 
 
181
  sector->length_of_sector += packet_size;
 
182
  sector->length_of_packet_data = tmp;
 
183
 
 
184
}
 
185
 
 
186
/*************************************************************************
 
187
        writes specifical pack header information into a buffer
 
188
        later this will be copied from the sector routine into
 
189
        the sector buffer
 
190
*************************************************************************/
 
191
 
 
192
void
 
193
create_pack (pack, SCR, mux_rate)
 
194
 
 
195
     Pack_struc *pack;
 
196
     unsigned int mux_rate;
 
197
     guint64 SCR;
 
198
 
 
199
{
 
200
  unsigned char *index;
 
201
 
 
202
  index = pack->buf;
 
203
 
 
204
  *(index++) = (unsigned char) ((PACK_START) >> 24);
 
205
  *(index++) = (unsigned char) ((PACK_START & 0x00ff0000) >> 16);
 
206
  *(index++) = (unsigned char) ((PACK_START & 0x0000ff00) >> 8);
 
207
  *(index++) = (unsigned char) (PACK_START & 0x000000ff);
 
208
  buffer_timecode (SCR, MARKER_SCR, &index);
 
209
  *(index++) = (unsigned char) (0x80 | (mux_rate >> 15));
 
210
  *(index++) = (unsigned char) (0xff & (mux_rate >> 7));
 
211
  *(index++) = (unsigned char) (0x01 | ((mux_rate & 0x7f) << 1));
 
212
  pack->SCR = SCR;
 
213
}
 
214
 
 
215
 
 
216
/*************************************************************************
 
217
        writes specifical system header information into a buffer
 
218
        later this will be copied from the sector routine into
 
219
        the sector buffer
 
220
*************************************************************************/
 
221
 
 
222
void
 
223
create_sys_header (sys_header, rate_bound, audio_bound,
 
224
    fixed, CSPS, audio_lock, video_lock,
 
225
    video_bound,
 
226
    stream1, buffer1_scale, buffer1_size,
 
227
    stream2, buffer2_scale, buffer2_size, which_streams)
 
228
 
 
229
     Sys_header_struc *sys_header;
 
230
     unsigned int rate_bound;
 
231
     unsigned char audio_bound;
 
232
     unsigned char fixed;
 
233
     unsigned char CSPS;
 
234
     unsigned char audio_lock;
 
235
     unsigned char video_lock;
 
236
     unsigned char video_bound;
 
237
 
 
238
     unsigned char stream1;
 
239
     unsigned char buffer1_scale;
 
240
     unsigned int buffer1_size;
 
241
     unsigned char stream2;
 
242
     unsigned char buffer2_scale;
 
243
     unsigned int buffer2_size;
 
244
     unsigned int which_streams;
 
245
 
 
246
{
 
247
  unsigned char *index;
 
248
 
 
249
  index = sys_header->buf;
 
250
 
 
251
  /* if we are not using both streams, we should clear some
 
252
     options here */
 
253
 
 
254
  if (!(which_streams & STREAMS_AUDIO))
 
255
    audio_bound = 0;
 
256
  if (!(which_streams & STREAMS_VIDEO))
 
257
    video_bound = 0;
 
258
 
 
259
  *(index++) = (unsigned char) ((SYS_HEADER_START) >> 24);
 
260
  *(index++) = (unsigned char) ((SYS_HEADER_START & 0x00ff0000) >> 16);
 
261
  *(index++) = (unsigned char) ((SYS_HEADER_START & 0x0000ff00) >> 8);
 
262
  *(index++) = (unsigned char) (SYS_HEADER_START & 0x000000ff);
 
263
 
 
264
  if (which_streams == STREAMS_BOTH) {
 
265
    *(index++) = (unsigned char) (SYS_HEADER_LENGTH >> 8);
 
266
    *(index++) = (unsigned char) (SYS_HEADER_LENGTH & 0xff);
 
267
  } else {
 
268
    *(index++) = (unsigned char) ((SYS_HEADER_LENGTH - 3) >> 8);
 
269
    *(index++) = (unsigned char) ((SYS_HEADER_LENGTH - 3) & 0xff);
 
270
  }
 
271
 
 
272
  *(index++) = (unsigned char) (0x80 | (rate_bound >> 15));
 
273
  *(index++) = (unsigned char) (0xff & (rate_bound >> 7));
 
274
  *(index++) = (unsigned char) (0x01 | ((rate_bound & 0x7f) << 1));
 
275
  *(index++) = (unsigned char) ((audio_bound << 2) | (fixed << 1) | CSPS);
 
276
  *(index++) = (unsigned char) ((audio_lock << 7) |
 
277
      (video_lock << 6) | 0x20 | video_bound);
 
278
 
 
279
  *(index++) = (unsigned char) RESERVED_BYTE;
 
280
 
 
281
  if (which_streams & STREAMS_AUDIO) {
 
282
    *(index++) = stream1;
 
283
    *(index++) = (unsigned char) (0xc0 |
 
284
        (buffer1_scale << 5) | (buffer1_size >> 8));
 
285
    *(index++) = (unsigned char) (buffer1_size & 0xff);
 
286
  }
 
287
 
 
288
  if (which_streams & STREAMS_VIDEO) {
 
289
    *(index++) = stream2;
 
290
    *(index++) = (unsigned char) (0xc0 |
 
291
        (buffer2_scale << 5) | (buffer2_size >> 8));
 
292
    *(index++) = (unsigned char) (buffer2_size & 0xff);
 
293
  }
 
294
 
 
295
}