OSDN Git Service

884680b9410ec6994feb1033b2e71cf2fa28283d
[handbrake-jp/handbrake-jp-git.git] / libhb / declpcm.c
1 /* $Id: declpcm.c,v 1.8 2005/11/04 14:44:01 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "hb.h"
8
9 struct hb_work_private_s
10 {
11     hb_job_t    *job;
12     uint32_t    size;       /* frame size in bytes */
13     uint32_t    count;      /* frame size in samples */
14     uint32_t    pos;        /* buffer offset for next input data */
15
16     int64_t     next_pts;   /* pts for next output frame */
17     int64_t     sequence;
18
19     /* the following is frame info for the frame we're currently accumulating */
20     uint64_t    duration;   /* frame duratin (in 90KHz ticks) */
21     uint32_t    offset;     /* where in buf frame starts */
22     uint32_t    samplerate; /* sample rate in bits/sec */
23     uint8_t     nchannels;
24     uint8_t     sample_size; /* bits per sample */
25
26     uint8_t     frame[HB_DVD_READ_BUFFER_SIZE*2];
27 };
28
29 static hb_buffer_t * Decode( hb_work_object_t * w );
30 int  declpcmInit( hb_work_object_t *, hb_job_t * );
31 int  declpcmWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
32 void declpcmClose( hb_work_object_t * );
33
34 hb_work_object_t hb_declpcm =
35 {
36     WORK_DECLPCM,
37     "LPCM decoder",
38     declpcmInit,
39     declpcmWork,
40     declpcmClose
41 };
42
43 static const int hdr2samplerate[] = { 48000, 96000, 44100, 32000 };
44 static const int hdr2samplesize[] = { 16, 20, 24, 16 };
45
46 static void lpcmInfo( hb_work_object_t *w, hb_buffer_t *in )
47 {
48     hb_work_private_t * pv = w->private_data;
49
50     /*
51      * LPCM packets have a 7 byte header (the substream id is stripped off
52      * before we get here so it's numbered -1 below)::
53      * byte -1  Substream id
54      * byte 0   Number of frames that begin in this packet
55      *          (last frame may finish in next packet)
56      * byte 1,2 offset to first frame that begins in this packet (not including hdr)
57      * byte 3:
58      *   bits 0-4  continuity counter (increments modulo 20)
59      *   bit   5   reserved
60      *   bit   6   audio mute on/off
61      *   bit   7   audio emphasis on/off
62      * byte 4:
63      *   bits 0-2  #channels - 1 (e.g., stereo = 1)
64      *   bit   3   reserved
65      *   bits 4-5  sample rate (0=48K,1=96K,2=44.1K,3=32K)
66      *   bits 6-7  bits per sample (0=16 bit, 1=20 bit, 2=24 bit)
67      * byte 5   Dynamic range control (0x80 = off)
68      *
69      * The audio is viewed as "frames" of 150 90KHz ticks each (80 samples @ 48KHz).
70      * The frames are laid down continuously without regard to MPEG packet
71      * boundaries. E.g., for 48KHz stereo, the first packet will contain 6
72      * frames plus the start of the 7th, the second packet will contain the
73      * end of the 7th, 8-13 & the start of 14, etc. The frame structure is
74      * important because the PTS on the packet gives the time of the first
75      * frame that starts in the packet *NOT* the time of the first sample 
76      * in the packet. Also samples get split across packet boundaries
77      * so we can't assume that we can consume all the data in one packet
78      * on every call to the work routine.
79      */
80     pv->offset = ( ( in->data[1] << 8 ) | in->data[2] ) + 2;
81     if ( pv->offset >= HB_DVD_READ_BUFFER_SIZE )
82     {
83         hb_log( "declpcm: illegal frame offset %d", pv->offset );
84         pv->offset = 2; /*XXX*/
85     }
86     pv->samplerate = hdr2samplerate[ ( in->data[4] >> 4 ) & 0x3 ];
87     pv->nchannels  = ( in->data[4] & 7 ) + 1;
88     pv->sample_size = hdr2samplesize[in->data[4] >> 6];
89
90     /*
91      * PCM frames have a constant duration (150 90KHz ticks).
92      * We need to convert that to the amount of data expected.  It's the
93      * duration divided by the sample rate (to get #samples) times the number
94      * of channels times the bits per sample divided by 8 to get bytes.
95      * (we have to compute in bits because 20 bit samples are not an integral
96      * number of bytes). We do all the multiplies first then the divides to
97      * avoid truncation errors. 
98      */
99     pv->duration = in->data[0] * 150;
100     pv->count = ( pv->duration * pv->nchannels * pv->samplerate ) / 90000;
101     pv->size = ( pv->count * pv->sample_size ) / 8;
102
103     pv->next_pts = in->start;
104 }
105
106 int declpcmInit( hb_work_object_t * w, hb_job_t * job )
107 {
108     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
109     w->private_data = pv;
110     pv->job   = job;
111     return 0;
112 }
113
114 /* 
115  * Convert DVD encapsulated LPCM to floating point PCM audio buffers.
116  * The amount of audio in a PCM frame is always <= the amount that will fit
117  * in a DVD block (2048 bytes) but the standard doesn't require that the audio
118  * frames line up with the DVD frames. Since audio frame boundaries are unrelated
119  * to DVD PES boundaries, this routine has to reconstruct then extract the audio
120  * frames. Because of the arbitrary alignment, it can output zero, one or two buf's.
121  */
122 int declpcmWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
123                  hb_buffer_t ** buf_out )
124 {
125     hb_work_private_t * pv = w->private_data;
126     hb_buffer_t *in;
127     hb_buffer_t *buf = NULL;
128
129     /* need an input buffer to do anything */
130     if( ! buf_in || ! ( in = *buf_in ) )
131     {
132         *buf_out = buf;
133         return HB_WORK_OK;
134     }
135
136     pv->sequence = in->sequence;
137
138     /* if we have a frame to finish, add enough data from this buf to finish it */
139     if ( pv->size )
140     {
141         memcpy( pv->frame + pv->pos, in->data + 6, pv->size - pv->pos );
142         buf = Decode( w );
143     }
144     *buf_out = buf;
145
146     /* save the (rest of) data from this buf in our frame buffer */
147     lpcmInfo( w, in );
148     int off = pv->offset;
149     int amt = in->size - off;
150     pv->pos = amt;
151     memcpy( pv->frame, in->data + off, amt );
152     if ( amt >= pv->size )
153     {
154         if ( buf )
155         {
156             buf->next = Decode( w );
157         }
158         else
159         {
160             *buf_out = Decode( w );
161         }
162         pv->size = 0;
163     }
164     return HB_WORK_OK;
165 }
166
167 static hb_buffer_t *Decode( hb_work_object_t *w )
168 {
169     hb_work_private_t *pv = w->private_data;
170     hb_buffer_t *out = hb_buffer_init( pv->count * sizeof( float ) );
171  
172     out->start  = pv->next_pts;
173     pv->next_pts += pv->duration;
174     out->stop = pv->next_pts;
175
176     uint8_t *frm = pv->frame;
177     float *odat = (float *)out->data;
178     int count = pv->count;
179
180     switch( pv->sample_size )
181     {
182         case 16: // 2 byte, big endian, signed (the right shift sign extends)
183             while ( --count >= 0 )
184             {
185                 *odat++ = ( (int)( frm[0] << 24 ) >> 16 ) | frm[1];
186                 frm += 2;
187             }
188             break;
189         case 20:
190             // 20 bit big endian signed (5 bytes for 2 samples = 2.5 bytes/sample
191             // so we do two samples per iteration).
192             count /= 2;
193             while ( --count >= 0 )
194             {
195                 *odat++ = (float)( ( (int)( frm[0] << 24 ) >> 12 ) |
196                                    ( frm[1] << 4 ) | ( frm[2] >> 4 ) ) / 16.;
197                 *odat++ = (float)( ( (int)( frm[2] << 28 ) >> 16 ) |
198                                    ( frm[3] << 8 ) | frm[4] ) / 16.;
199                 frm += 5;
200             }
201             break;
202         case 24:
203             // This format is bizarre. It's 24 bit samples but some confused
204             // individual apparently thought they would be easier to interpret
205             // as 16 bits if they were scrambled in the following way:
206             // Things are stored in 4 sample (12 byte) chunks. Each chunk has
207             // 4 samples containing the two top bytes of the actual samples in
208             // 16 bit big-endian order followed by the four least significant bytes
209             // of each sample.
210             count /= 4; // the loop has to work in 4 sample chunks
211             while ( --count >= 0 )
212             {
213                 *odat++ = (float)( ( (int)( frm[0] << 24 ) >> 8 ) |
214                             ( frm[1] << 8 ) | frm[8] ) / 256.;
215                 *odat++ = (float)( ( (int)( frm[2] << 24 ) >> 8 ) |
216                             ( frm[3] << 8 ) | frm[9] ) / 256.;
217                 *odat++ = (float)( ( (int)( frm[4] << 24 ) >> 8 ) |
218                             ( frm[5] << 8 ) | frm[10] ) / 256.;
219                 *odat++ = (float)( ( (int)( frm[6] << 24 ) >> 8 ) |
220                             ( frm[7] << 8 ) | frm[11] ) / 256.;
221                 frm += 12;
222             }
223             break;
224     }
225     return out;
226 }
227
228 void declpcmClose( hb_work_object_t * w )
229 {
230     if ( w->private_data )
231     {
232         free( w->private_data );
233         w->private_data = 0;
234     }
235 }