OSDN Git Service

Added chapter markers in a chapter text track in mp4 files.
[handbrake-jp/handbrake-jp-git.git] / libhb / encfaac.c
1 /* $Id: encfaac.c,v 1.13 2005/03/03 17:21:57 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 #include "faac.h"
10
11 struct hb_work_private_s
12 {
13     hb_job_t   * job;
14
15     faacEncHandle * faac;
16     unsigned long   input_samples;
17     unsigned long   output_bytes;
18     uint8_t       * buf;
19
20     hb_list_t     * list;
21     int64_t         pts;
22         
23         int             channelsused;
24
25 };
26
27 int  encfaacInit( hb_work_object_t *, hb_job_t * );
28 int  encfaacWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
29 void encfaacClose( hb_work_object_t * );
30
31 hb_work_object_t hb_encfaac =
32 {
33     WORK_ENCFAAC,
34     "AAC encoder (libfaac)",
35     encfaacInit,
36     encfaacWork,
37     encfaacClose
38 };
39
40 /***********************************************************************
41  * hb_work_encfaac_init
42  ***********************************************************************
43  *
44  **********************************************************************/
45 int encfaacInit( hb_work_object_t * w, hb_job_t * job )
46 {
47     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
48     faacEncConfigurationPtr cfg;
49     uint8_t * bytes;
50     unsigned long length;
51
52     w->private_data = pv;
53
54     pv->job   = job;
55
56         /* pass the number of channels used into the private work data */
57         pv->channelsused = w->config->aac.channelsused;
58
59     pv->faac = faacEncOpen( job->arate, w->config->aac.channelsused, &pv->input_samples,
60                            &pv->output_bytes );
61     pv->buf  = malloc( pv->input_samples * sizeof( float ) );
62     
63     cfg                = faacEncGetCurrentConfiguration( pv->faac );
64     cfg->mpegVersion   = MPEG4;
65     cfg->aacObjectType = LOW;
66     cfg->allowMidside  = 1;
67         
68         if (w->config->aac.channelsused == 6) {
69                 /* we are preserving 5.1 audio into 6-channel AAC,
70                 so indicate that we have an lfe channel */
71                 cfg->useLfe    = 1;
72         } else {
73                 cfg->useLfe    = 0;
74         }
75
76     cfg->useTns        = 0;
77     cfg->bitRate       = job->abitrate * 500; /* Per channel */
78     cfg->bandWidth     = 0;
79     cfg->outputFormat  = 0;
80     cfg->inputFormat   =  FAAC_INPUT_FLOAT;
81         
82         if (w->config->aac.channelsused == 6) {
83                 /* we are preserving 5.1 audio into 6-channel AAC, and need to
84                 re-map the output of deca52 into our own mapping - the mapping
85                 below is the default mapping expected by QuickTime */
86                 /* This doesn't seem to be correct for VLC on Linux */
87                 cfg->channel_map[0] = 2;
88                 cfg->channel_map[1] = 1;
89                 cfg->channel_map[2] = 3;
90                 cfg->channel_map[3] = 4;
91                 cfg->channel_map[4] = 5;
92                 cfg->channel_map[5] = 0;
93         }
94         
95     if( !faacEncSetConfiguration( pv->faac, cfg ) )
96     {
97         hb_log( "faacEncSetConfiguration failed" );
98     }
99
100     if( faacEncGetDecoderSpecificInfo( pv->faac, &bytes, &length ) < 0 )
101     {
102         hb_log( "faacEncGetDecoderSpecificInfo failed" );
103     }
104     memcpy( w->config->aac.bytes, bytes, length );
105     w->config->aac.length = length;
106     free( bytes );
107
108     pv->list = hb_list_init();
109     pv->pts  = -1;
110
111     return 0;
112 }
113
114 /***********************************************************************
115  * Close
116  ***********************************************************************
117  *
118  **********************************************************************/
119 void encfaacClose( hb_work_object_t * w )
120 {
121     hb_work_private_t * pv = w->private_data;
122     faacEncClose( pv->faac );
123     free( pv->buf );
124     hb_list_empty( &pv->list );
125 }
126
127 /***********************************************************************
128  * Encode
129  ***********************************************************************
130  *
131  **********************************************************************/
132 static hb_buffer_t * Encode( hb_work_object_t * w )
133 {
134     hb_work_private_t * pv = w->private_data;
135     hb_buffer_t * buf;
136     uint64_t      pts, pos;
137
138     if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
139     {
140         /* Need more data */
141         return NULL;
142     }
143
144     hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
145                       &pts, &pos );
146
147     buf        = hb_buffer_init( pv->output_bytes );
148     buf->start = pts + 90000 * pos / pv->channelsused / sizeof( float ) / pv->job->arate;
149     buf->stop  = buf->start + 90000 * pv->input_samples / pv->job->arate / pv->channelsused;
150     buf->size  = faacEncEncode( pv->faac, (int32_t *) pv->buf,
151             pv->input_samples, buf->data, pv->output_bytes );
152     buf->key   = 1;
153
154     if( !buf->size )
155     {
156         /* Encoding was successful but we got no data. Try to encode
157            more */
158         hb_buffer_close( &buf );
159         return Encode( w );
160     }
161     else if( buf->size < 0 )
162     {
163         hb_log( "faacEncEncode failed" );
164         hb_buffer_close( &buf );
165         return NULL;
166     }
167
168     return buf;
169 }
170
171 /***********************************************************************
172  * Work
173  ***********************************************************************
174  *
175  **********************************************************************/
176 int encfaacWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
177                  hb_buffer_t ** buf_out )
178 {
179     hb_work_private_t * pv = w->private_data;
180     hb_buffer_t * buf;
181
182     hb_list_add( pv->list, *buf_in );
183     *buf_in = NULL;
184
185     *buf_out = buf = Encode( w );
186
187     while( buf )
188     {
189         buf->next = Encode( w );
190         buf       = buf->next;
191     }
192     
193     return HB_WORK_OK;
194 }
195