OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / libhb / decdca.c
1 /* $Id: decdca.c,v 1.14 2005/03/03 17:21:57 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.fr/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "hb.h"
8 #include "dca.h"
9
10 struct hb_work_private_s
11 {
12     hb_job_t    * job;
13
14     /* libdca handle */
15     dca_state_t * state;
16
17     double        next_pts;
18     int           flags_in;
19     int           flags_out;
20     int           rate;
21     int           bitrate;
22     int           frame_length;
23     float         level;
24
25     int           error;
26     int           sync;
27     int           size;
28
29     /* max frame size of the 16 bits version is 16384 */
30     /* max frame size of the 14 bits version is 18726 */
31     uint8_t       frame[18726];
32
33     hb_list_t   * list;
34
35         int           out_discrete_channels;
36
37 };
38
39 static int  decdcaInit( hb_work_object_t *, hb_job_t * );
40 static int  decdcaWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
41 static void decdcaClose( hb_work_object_t * );
42 static int  decdcaBSInfo( hb_work_object_t *, const hb_buffer_t *,
43                           hb_work_info_t * );
44
45 hb_work_object_t hb_decdca =
46 {
47     WORK_DECDCA,
48     "DCA decoder",
49     decdcaInit,
50     decdcaWork,
51     decdcaClose,
52     0,
53     decdcaBSInfo
54 };
55
56 /***********************************************************************
57  * Local prototypes
58  **********************************************************************/
59 static hb_buffer_t * Decode( hb_work_object_t * w );
60
61 /***********************************************************************
62  * hb_work_decdca_init
63  ***********************************************************************
64  * Allocate the work object, initialize libdca
65  **********************************************************************/
66 static int decdcaInit( hb_work_object_t * w, hb_job_t * job )
67 {
68     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
69     hb_audio_t * audio = w->audio;
70     w->private_data = pv;
71
72     pv->job   = job;
73
74     pv->list      = hb_list_init();
75     pv->state     = dca_init( 0 );
76
77         /* Decide what format we want out of libdca
78         work.c has already done some of this deduction for us in do_job() */
79
80         pv->flags_out = HB_AMIXDOWN_GET_DCA_FORMAT(audio->config.out.mixdown);
81
82         /* pass the number of channels used into the private work data */
83         /* will only be actually used if we're not doing AC3 passthru */
84     pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
85
86     pv->level     = 32768.0;
87
88     return 0;
89 }
90
91 /***********************************************************************
92  * Close
93  ***********************************************************************
94  * Free memory
95  **********************************************************************/
96 static void decdcaClose( hb_work_object_t * w )
97 {
98     hb_work_private_t * pv = w->private_data;
99     dca_free( pv->state );
100     hb_list_empty( &pv->list );
101     free( pv );
102     w->private_data = NULL;
103 }
104
105 /***********************************************************************
106  * Work
107  ***********************************************************************
108  * Add the given buffer to the data we already have, and decode as much
109  * as we can
110  **********************************************************************/
111 static int decdcaWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
112                 hb_buffer_t ** buf_out )
113 {
114     hb_work_private_t * pv = w->private_data;
115     hb_buffer_t * buf;
116
117     if ( (*buf_in)->size <= 0 )
118     {
119         /* EOF on input stream - send it downstream & say that we're done */
120         *buf_out = *buf_in;
121         *buf_in = NULL;
122         return HB_WORK_DONE;
123     }
124
125     hb_list_add( pv->list, *buf_in );
126     *buf_in = NULL;
127
128     /* If we got more than a frame, chain raw buffers */
129     *buf_out = buf = Decode( w );
130     while( buf )
131     {
132         buf->next = Decode( w );
133         buf       = buf->next;
134     }
135
136     return HB_WORK_OK;
137 }
138
139 /***********************************************************************
140  * Decode
141  ***********************************************************************
142  *
143  **********************************************************************/
144 static hb_buffer_t * Decode( hb_work_object_t * w )
145 {
146     hb_work_private_t * pv = w->private_data;
147     hb_buffer_t * buf;
148     int           i, j, k;
149     int64_t       pts, pos;
150     int           num_blocks;
151
152     /* Get a frame header if don't have one yet */
153     if( !pv->sync )
154     {
155         while( hb_list_bytes( pv->list ) >= 14 )
156         {
157             /* We have 14 bytes, check if this is a correct header */
158             hb_list_seebytes( pv->list, pv->frame, 14 );
159             pv->size = dca_syncinfo( pv->state, pv->frame, &pv->flags_in, &pv->rate,
160                                     &pv->bitrate, &pv->frame_length );
161             if( pv->size )
162             {
163                 /* It is. W00t. */
164                 if( pv->error )
165                 {
166                     hb_log( "dca_syncinfo ok" );
167                 }
168                 pv->error = 0;
169                 pv->sync  = 1;
170                 break;
171             }
172
173             /* It is not */
174             if( !pv->error )
175             {
176                 hb_log( "dca_syncinfo failed" );
177                 pv->error = 1;
178             }
179
180             /* Try one byte later */
181             hb_list_getbytes( pv->list, pv->frame, 1, NULL, NULL );
182         }
183     }
184
185     if( !pv->sync || hb_list_bytes( pv->list ) < pv->size )
186     {
187         /* Need more data */
188         return NULL;
189     }
190
191     /* Get the whole frame */
192     hb_list_getbytes( pv->list, pv->frame, pv->size, &pts, &pos );
193
194     /* Feed libdca */
195     dca_frame( pv->state, pv->frame, &pv->flags_out, &pv->level, 0 );
196
197     /* find out how many blocks are in this frame */
198     num_blocks = dca_blocks_num( pv->state );
199
200     /* num_blocks blocks per frame, 256 samples per block, channelsused channels */
201     int nsamp = num_blocks * 256;
202     buf = hb_buffer_init( nsamp * pv->out_discrete_channels * sizeof( float ) );
203
204     // mkv files typically use a 1ms timebase which results in a lot of
205     // truncation error in their timestamps. Also, TSMuxer or something
206     // in the m2ts-to-mkv toolchain seems to take a very casual attitude
207     // about time - timestamps seem to randomly offset by ~40ms for a few
208     // seconds then recover. So, if the pts we got is within 50ms of the
209     // pts computed from the data stream, use the data stream pts.
210     if ( pts < 0 || fabs( (double)pts - pv->next_pts ) < 50.*90. )
211     {
212         pts = pv->next_pts;
213     }
214     buf->start = pts;
215     pv->next_pts += (double)nsamp / (double)pv->rate * 90000.;
216     buf->stop  = pv->next_pts;
217
218     for( i = 0; i < num_blocks; i++ )
219     {
220         dca_sample_t * samples_in;
221         float    * samples_out;
222
223         dca_block( pv->state );
224         samples_in  = dca_samples( pv->state );
225         samples_out = ((float *) buf->data) + 256 * pv->out_discrete_channels * i;
226
227         /* Interleave */
228         for( j = 0; j < 256; j++ )
229         {
230                         for ( k = 0; k < pv->out_discrete_channels; k++ )
231                         {
232                                 samples_out[(pv->out_discrete_channels*j)+k]   = samples_in[(256*k)+j] * 16384;
233                         }
234         }
235
236     }
237
238     pv->sync = 0;
239     return buf;
240 }
241
242
243 static int decdcaBSInfo( hb_work_object_t *w, const hb_buffer_t *b,
244                          hb_work_info_t *info )
245 {
246     int i, flags, rate, bitrate, frame_length;
247     dca_state_t * state = dca_init( 0 );
248
249     memset( info, 0, sizeof(*info) );
250
251     /* since DCA frames don't line up with MPEG ES frames scan the
252      * entire frame for an DCA sync pattern.  */
253     for ( i = 0; i < b->size - 7; ++i )
254     {
255         if( dca_syncinfo( state, &b->data[i], &flags, &rate, &bitrate,
256                           &frame_length ) )
257         {
258             break;
259         }
260     }
261     if ( i >= b->size - 7 )
262     {
263         /* didn't find DCA sync */
264         return 0;
265     }
266
267     info->name = "DCA";
268     info->rate = rate;
269     info->rate_base = 1;
270     info->bitrate = bitrate;
271     info->flags = flags;
272
273     if ( ( flags & DCA_CHANNEL_MASK) == DCA_DOLBY )
274     {
275         info->flags |= AUDIO_F_DOLBY;
276     }
277
278     switch( flags & DCA_CHANNEL_MASK )
279     {
280         /* mono sources */
281         case DCA_MONO:
282             info->channel_layout = HB_INPUT_CH_LAYOUT_MONO;
283             break;
284         /* stereo input */
285         case DCA_CHANNEL:
286         case DCA_STEREO:
287         case DCA_STEREO_SUMDIFF:
288         case DCA_STEREO_TOTAL:
289             info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
290             break;
291         /* 3F/2R input */
292         case DCA_3F2R:
293             info->channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
294             break;
295         /* 3F/1R input */
296         case DCA_3F1R:
297             info->channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
298             break;
299         /* other inputs */
300         case DCA_3F:
301             info->channel_layout = HB_INPUT_CH_LAYOUT_3F;
302             break;
303         case DCA_2F1R:
304             info->channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
305             break;
306         case DCA_2F2R:
307             info->channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
308             break;
309         case DCA_4F2R:
310             info->channel_layout = HB_INPUT_CH_LAYOUT_4F2R;
311             break;
312         /* unknown */
313         default:
314             info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
315     }
316
317     if (flags & DCA_LFE)
318     {
319         info->channel_layout |= HB_INPUT_CH_LAYOUT_HAS_LFE;
320     }
321
322     return 1;
323 }