summaryrefslogtreecommitdiff
path: root/sys/dxr3/ac3_padder.c
blob: bee59895a61c6b25d9750e1ae3db1ae32fc79fc1 (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
251
252
253
254
255
256
257
258
259
/* GStreamer
 * Copyright (C) 2003 Martin Soto <martinsoto@users.sourceforge.net>
 *
 * ac3_padder.c: Pad AC3 frames for use with an SPDIF interface.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <string.h>

#include "ac3_padder.h"

#define IEC61937_DATA_TYPE_AC3 1

struct frmsize_s
{
  unsigned short bit_rate;
  unsigned short frm_size[3];
};
                

static const struct frmsize_s frmsizecod_tbl[64] =
  {
    { 32  ,{64   ,69   ,96   } },
    { 32  ,{64   ,70   ,96   } },
    { 40  ,{80   ,87   ,120  } },
    { 40  ,{80   ,88   ,120  } },
    { 48  ,{96   ,104  ,144  } },
    { 48  ,{96   ,105  ,144  } },
    { 56  ,{112  ,121  ,168  } },
    { 56  ,{112  ,122  ,168  } },
    { 64  ,{128  ,139  ,192  } },
    { 64  ,{128  ,140  ,192  } },
    { 80  ,{160  ,174  ,240  } },
    { 80  ,{160  ,175  ,240  } },
    { 96  ,{192  ,208  ,288  } },
    { 96  ,{192  ,209  ,288  } },
    { 112 ,{224  ,243  ,336  } },
    { 112 ,{224  ,244  ,336  } },
    { 128 ,{256  ,278  ,384  } },
    { 128 ,{256  ,279  ,384  } },
    { 160 ,{320  ,348  ,480  } },
    { 160 ,{320  ,349  ,480  } },
    { 192 ,{384  ,417  ,576  } },
    { 192 ,{384  ,418  ,576  } },
    { 224 ,{448  ,487  ,672  } },
    { 224 ,{448  ,488  ,672  } },
    { 256 ,{512  ,557  ,768  } },
    { 256 ,{512  ,558  ,768  } },
    { 320 ,{640  ,696  ,960  } },
    { 320 ,{640  ,697  ,960  } },
    { 384 ,{768  ,835  ,1152 } },
    { 384 ,{768  ,836  ,1152 } },
    { 448 ,{896  ,975  ,1344 } },
    { 448 ,{896  ,976  ,1344 } },
    { 512 ,{1024 ,1114 ,1536 } },
    { 512 ,{1024 ,1115 ,1536 } },
    { 576 ,{1152 ,1253 ,1728 } },
    { 576 ,{1152 ,1254 ,1728 } },
    { 640 ,{1280 ,1393 ,1920 } },
    { 640 ,{1280 ,1394 ,1920 } }
  };



/* Go one byte forward in the input buffer. */
#define ac3p_in_fw(padder) ((padder)->in_ptr++, (padder)->remaining--)

/* Go one byte forward in the output buffer. */
#define ac3p_out_fw(padder) ((padder)->out_ptr++, (padder)->bytes_to_copy--)


/**
 * ac3p_init
 * @padder: The padder structure to initialize.
 *
 * Initializes an AC3 stream padder.  This structure can be
 * subsequently used to parse an AC3 stream and convert it to IEC958
 * (S/PDIF) padded packets.
 */
extern void
ac3p_init(ac3_padder *padder)
{
  const char sync[4] = { 0x72, 0xF8, 0x1F, 0x4E };

  padder->state = AC3P_STATE_SYNC1;

  /* No material to read yet. */
  padder->remaining = 0;

  /* Initialize the sync bytes in the frame. */
  memcpy(padder->frame.header, sync, 4);
}


/**
 * ac3_push_data
 * @padder The padder structure.
 * @data A pointer to a buffer with new data to parse.  This should 
 * correspond to a new piece of a stream containing raw AC3 data.
 * @size The number of available bytes in the buffer.
 *
 * Pushes a new buffer of data to be parsed by the ac3 padder.  The
 * ac3_parse() function will actually parse the data and report when
 * new frames are found.  This funcion should only be called once at
 * the beginning of the parsing process, or when the ac3_parse()
 * function returns the %AC3P_EVENT_PUSH event.
 */
extern void
ac3p_push_data(ac3_padder *padder, guchar *data, guint size)
{
  padder->in_ptr = data;
  padder->remaining = size;
}


/**
 * ac3p_parse
 * @padder The padder structure.
 * 
 * Parses the bytes already pushed into the padder structure (see
 * ac3p_push_data()) and returns an event value depending on the
 * results of the parsing.
 *
 * Returns: %AC3P_EVENT_FRAME to indicate that a new AC3 was found and
 * padded for IEC958 transmission.  This frame can be read inmediatly
 * with ac3p_frame(). %AC3P_EVENT_PUSH to indicate that new data from
 * the input stream must be pushed into the padder using
 * ac3p_push_data().  This function should be called again after
 * pushing the data.
 */
extern int
ac3p_parse(ac3_padder *padder)
{
  while (padder->remaining > 0) {
    switch (padder->state) {
    case AC3P_STATE_SYNC1:
      if (*(padder->in_ptr) == 0x0b) {
        /* The first sync byte was found.  Go to the next state. */
        padder->frame.sync_byte1 = 0x0b;
        padder->state = AC3P_STATE_SYNC2;
      }
      ac3p_in_fw(padder);
      break;

    case AC3P_STATE_SYNC2:
      if (*(padder->in_ptr) == 0x77) {
        /* The second sync byte was seen right after the first.  Go to
           the next state. */
        padder->frame.sync_byte2 = 0x77;
        padder->state = AC3P_STATE_HEADER;

        /* Skip one byte. */
        ac3p_in_fw(padder);

        /* Prepare for reading the header. */
        padder->out_ptr = (guchar *) &(padder->frame.crc1);
        /* Discount the 2 sync bytes from the header size. */
        padder->bytes_to_copy = AC3P_AC3_HEADER_SIZE - 2;
      }
      else {
        /* The second sync byte was not seen.  Go back to the
           first state. */
        padder->state = AC3P_STATE_SYNC1;
      }
      break;

    case AC3P_STATE_HEADER:
      if (padder->bytes_to_copy > 0) {
        /* Copy one byte. */
        *(padder->out_ptr) = *(padder->in_ptr);
        ac3p_in_fw(padder);
        ac3p_out_fw(padder);
      }
      else {
        int fscod;

        /* The header is ready: */

        fscod = (padder->frame.code >> 6) & 0x03;

        /* Calculate the frame size. */
        padder->ac3_frame_size =
          2 * frmsizecod_tbl[padder->frame.code & 0x3f].frm_size[fscod];

        /* Set up the IEC header. */
        if (padder->ac3_frame_size > 0) {
          padder->frame.header[4] = IEC61937_DATA_TYPE_AC3;
        }
        else {
          /* Don't know what it is, better be careful. */
          padder->state = AC3P_STATE_SYNC1;
          break;
        }
        padder->frame.header[5] = 0x00;
        padder->frame.header[6] = (padder->ac3_frame_size*8) & 0xFF;
        padder->frame.header[7] = ((padder->ac3_frame_size*8) >> 8) & 0xFF;
          
        /* Prepare for reading the body. */
        padder->bytes_to_copy = padder->ac3_frame_size - AC3P_AC3_HEADER_SIZE;
        padder->state = AC3P_STATE_CONTENT;
      }
      break;

    case AC3P_STATE_CONTENT:
      if (padder->bytes_to_copy > 0) {
        /* Copy one byte. */
        *(padder->out_ptr) = *(padder->in_ptr);
        ac3p_in_fw(padder);
        ac3p_out_fw(padder);
      }
      else {
        guint16 *ptr, i;

        /* Frame ready.  Prepare for output: */

        /* Zero the non AC3 portion of the padded frame. */
        memset(&(padder->frame.sync_byte1) + padder->ac3_frame_size, 0,
          AC3P_IEC_FRAME_SIZE - AC3P_IEC_HEADER_SIZE - padder->ac3_frame_size);

        /* Fix the byte order in the AC3 portion: */
        ptr = (guint16 *) &(padder->frame.sync_byte1);
        i = padder->ac3_frame_size / 2;
        while (i > 0) {
          *ptr = GUINT16_TO_BE(*ptr);
          ptr++;
          i--;
        }

        /* Start over again. */
        padder->state = AC3P_STATE_SYNC1;

        return AC3P_EVENT_FRAME;
      }
      break;
    }
  }

  return AC3P_EVENT_PUSH;
}