OSDN Git Service

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