OSDN Git Service

When encoding anamorphic with lavc, ensure PAR values are 8-bit. Thanks, j45!
[handbrake-jp/handbrake-jp-git.git] / libhb / deca52.c
1 /* $Id: deca52.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
9 #include "a52dec/a52.h"
10
11 struct hb_work_private_s
12 {
13     hb_job_t    * job;
14
15     /* liba52 handle */
16     a52_state_t * state;
17
18     int           flags_in;
19     int           flags_out;
20     int           rate;
21     int           bitrate;
22     float         level;
23     float         dynamic_range_compression;
24
25     int           error;
26     int           sync;
27     int           size;
28
29     int64_t       next_expected_pts;
30
31     int64_t       sequence;
32
33     uint8_t       frame[3840];
34
35     hb_list_t   * list;
36
37         int           out_discrete_channels;
38
39 };
40
41 int  deca52Init( hb_work_object_t *, hb_job_t * );
42 int  deca52Work( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
43 void deca52Close( hb_work_object_t * );
44
45 hb_work_object_t hb_deca52 =
46 {
47     WORK_DECA52,
48     "AC3 decoder",
49     deca52Init,
50     deca52Work,
51     deca52Close
52 };
53
54 /***********************************************************************
55  * Local prototypes
56  **********************************************************************/
57 static hb_buffer_t * Decode( hb_work_object_t * w );
58
59 /***********************************************************************
60  * dynrng_call
61  ***********************************************************************
62  * Boosts soft audio -- taken from gbooker's work in A52Decoder, comment and all..
63  * Two cases
64  * 1) The user requested a compression of 1 or less, return the typical power rule
65  * 2) The user requested a compression of more than 1 (decompression):
66  *    If the stream's requested compression is less than 1.0 (loud sound), return the normal compression
67  *    If the stream's requested compression is more than 1.0 (soft sound), use power rule (which will make
68  *   it louder in this case).
69  *
70  **********************************************************************/
71 static sample_t dynrng_call (sample_t c, void *data)
72 {
73         float *level = (float *)data;
74         float levelToUse = (float)*level;
75         if(c > 1.0 || levelToUse <= 1.0)
76         {
77             return powf(c, levelToUse);
78         }
79         else
80                 return c;
81 }
82
83 /***********************************************************************
84  * hb_work_deca52_init
85  ***********************************************************************
86  * Allocate the work object, initialize liba52
87  **********************************************************************/
88 int deca52Init( hb_work_object_t * w, hb_job_t * job )
89 {
90     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
91     hb_audio_t * audio = w->audio;
92     w->private_data = pv;
93
94     pv->job   = job;
95
96     pv->list      = hb_list_init();
97     pv->state     = a52_init( 0 );
98
99         /* Decide what format we want out of a52dec
100         work.c has already done some of this deduction for us in do_job() */
101
102         pv->flags_out = HB_AMIXDOWN_GET_A52_FORMAT(audio->config.out.mixdown);
103
104         /* pass the number of channels used into the private work data */
105         /* will only be actually used if we're not doing AC3 passthru */
106     pv->out_discrete_channels = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown);
107
108     pv->level     = 32768.0;
109     pv->dynamic_range_compression = audio->config.out.dynamic_range_compression;
110
111     pv->next_expected_pts = 0;
112     pv->sequence = 0;
113
114     return 0;
115 }
116
117 /***********************************************************************
118  * Close
119  ***********************************************************************
120  * Free memory
121  **********************************************************************/
122 void deca52Close( hb_work_object_t * w )
123 {
124     hb_work_private_t * pv = w->private_data;
125     a52_free( pv->state );
126     hb_list_empty( &pv->list );
127     free( pv );
128     w->private_data = NULL;
129 }
130
131 /***********************************************************************
132  * Work
133  ***********************************************************************
134  * Add the given buffer to the data we already have, and decode as much
135  * as we can
136  **********************************************************************/
137 int deca52Work( hb_work_object_t * w, hb_buffer_t ** buf_in,
138                 hb_buffer_t ** buf_out )
139 {
140     hb_work_private_t * pv = w->private_data;
141     hb_buffer_t * buf;
142
143     if( buf_in && *buf_in )
144     {
145         pv->sequence = (*buf_in)->sequence;
146     }
147
148     hb_list_add( pv->list, *buf_in );
149     *buf_in = NULL;
150
151     /* If we got more than a frame, chain raw buffers */
152     *buf_out = buf = Decode( w );
153     while( buf )
154     {
155         buf->sequence = pv->sequence;
156         buf->next = Decode( w );
157         buf       = buf->next;
158     }
159
160     return HB_WORK_OK;
161 }
162
163 /***********************************************************************
164  * Decode
165  ***********************************************************************
166  *
167  **********************************************************************/
168 static hb_buffer_t * Decode( hb_work_object_t * w )
169 {
170     hb_work_private_t * pv = w->private_data;
171     hb_buffer_t * buf;
172     hb_audio_t  * audio = w->audio;
173     int           i, j, k;
174     uint64_t      pts, pos;
175
176     /* Get a frame header if don't have one yet */
177     if( !pv->sync )
178     {
179         while( hb_list_bytes( pv->list ) >= 7 )
180         {
181             /* We have 7 bytes, check if this is a correct header */
182             hb_list_seebytes( pv->list, pv->frame, 7 );
183             pv->size = a52_syncinfo( pv->frame, &pv->flags_in, &pv->rate,
184                                     &pv->bitrate );
185             if( pv->size )
186             {
187                 /* It is. W00t. */
188                 if( pv->error )
189                 {
190                     hb_log( "a52_syncinfo ok" );
191                 }
192                 pv->error = 0;
193                 pv->sync  = 1;
194                 break;
195             }
196
197             /* It is not */
198             if( !pv->error )
199             {
200                 hb_log( "a52_syncinfo failed" );
201                 pv->error = 1;
202             }
203
204             /* Try one byte later */
205             hb_list_getbytes( pv->list, pv->frame, 1, NULL, NULL );
206         }
207     }
208
209     if( !pv->sync ||
210         hb_list_bytes( pv->list ) < pv->size )
211     {
212         /* Need more data */
213         return NULL;
214     }
215
216     /* Get the whole frame */
217     hb_list_getbytes( pv->list, pv->frame, pv->size, &pts, &pos );
218     if (pts == -1)
219     {
220         pts = pv->next_expected_pts;
221     }
222
223     /* AC3 passthrough: don't decode the AC3 frame */
224     if( audio->config.out.codec == HB_ACODEC_AC3 )
225     {
226         buf = hb_buffer_init( pv->size );
227         memcpy( buf->data, pv->frame, pv->size );
228         buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
229         buf->stop  = buf->start + 6 * 256 * 90000 / pv->rate;
230         pv->next_expected_pts = buf->stop;
231         pv->sync = 0;
232         return buf;
233     }
234
235     /* Feed liba52 */
236     a52_frame( pv->state, pv->frame, &pv->flags_out, &pv->level, 0 );
237
238     if ( pv->dynamic_range_compression > 1.0 )
239     {
240         a52_dynrng( pv->state, dynrng_call, &pv->dynamic_range_compression);
241     }
242
243     /* 6 blocks per frame, 256 samples per block, channelsused channels */
244     buf        = hb_buffer_init( 6 * 256 * pv->out_discrete_channels * sizeof( float ) );
245     buf->start = pts + ( pos / pv->size ) * 6 * 256 * 90000 / pv->rate;
246     buf->stop  = buf->start + 6 * 256 * 90000 / pv->rate;
247
248     /*
249        * To track AC3 PTS add this back in again.
250         *hb_log("AC3: pts is %lld, buf->start %lld buf->stop %lld", pts, buf->start, buf->stop);
251         */
252
253     pv->next_expected_pts = buf->stop;
254
255     for( i = 0; i < 6; i++ )
256     {
257         sample_t * samples_in;
258         float    * samples_out;
259
260         a52_block( pv->state );
261         samples_in  = a52_samples( pv->state );
262         samples_out = ((float *) buf->data) + 256 * pv->out_discrete_channels * i;
263
264         /* Interleave */
265         for( j = 0; j < 256; j++ )
266         {
267                         for ( k = 0; k < pv->out_discrete_channels; k++ )
268                         {
269                                 samples_out[(pv->out_discrete_channels*j)+k]   = samples_in[(256*k)+j];
270                         }
271         }
272
273     }
274
275     pv->sync = 0;
276     return buf;
277 }
278