OSDN Git Service

added call to av_log_set_level in hb_init_real based on value of verbose param.
[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
24 int  encfaacInit( hb_work_object_t *, hb_job_t * );
25 int  encfaacWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
26 void encfaacClose( hb_work_object_t * );
27
28 hb_work_object_t hb_encfaac =
29 {
30     WORK_ENCFAAC,
31     "AAC encoder (libfaac)",
32     encfaacInit,
33     encfaacWork,
34     encfaacClose
35 };
36
37 /***********************************************************************
38  * hb_work_encfaac_init
39  ***********************************************************************
40  *
41  **********************************************************************/
42 int encfaacInit( hb_work_object_t * w, hb_job_t * job )
43 {
44     hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
45     faacEncConfigurationPtr cfg;
46     uint8_t * bytes;
47     unsigned long length;
48
49     w->private_data = pv;
50
51     pv->job   = job;
52
53     pv->faac = faacEncOpen( job->arate, 2, &pv->input_samples,
54                            &pv->output_bytes );
55     pv->buf  = malloc( pv->input_samples * sizeof( float ) );
56     
57     cfg                = faacEncGetCurrentConfiguration( pv->faac );
58     cfg->mpegVersion   = MPEG4;
59     cfg->aacObjectType = LOW;
60     cfg->allowMidside  = 1;
61     cfg->useLfe        = 0;
62     cfg->useTns        = 0;
63     cfg->bitRate       = job->abitrate * 500; /* Per channel */
64     cfg->bandWidth     = 0;
65     cfg->outputFormat  = 0;
66     cfg->inputFormat   =  FAAC_INPUT_FLOAT;
67     if( !faacEncSetConfiguration( pv->faac, cfg ) )
68     {
69         hb_log( "faacEncSetConfiguration failed" );
70     }
71
72     if( faacEncGetDecoderSpecificInfo( pv->faac, &bytes, &length ) < 0 )
73     {
74         hb_log( "faacEncGetDecoderSpecificInfo failed" );
75     }
76     memcpy( w->config->aac.bytes, bytes, length );
77     w->config->aac.length = length;
78     free( bytes );
79
80     pv->list = hb_list_init();
81     pv->pts  = -1;
82
83     return 0;
84 }
85
86 /***********************************************************************
87  * Close
88  ***********************************************************************
89  *
90  **********************************************************************/
91 void encfaacClose( hb_work_object_t * w )
92 {
93     hb_work_private_t * pv = w->private_data;
94     faacEncClose( pv->faac );
95     free( pv->buf );
96     hb_list_empty( &pv->list );
97 }
98
99 /***********************************************************************
100  * Encode
101  ***********************************************************************
102  *
103  **********************************************************************/
104 static hb_buffer_t * Encode( hb_work_object_t * w )
105 {
106     hb_work_private_t * pv = w->private_data;
107     hb_buffer_t * buf;
108     uint64_t      pts;
109     int           pos;
110
111     if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
112     {
113         /* Need more data */
114         return NULL;
115     }
116
117     hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
118                       &pts, &pos );
119
120     buf        = hb_buffer_init( pv->output_bytes );
121     buf->start = pts + 90000 * pos / 2 / sizeof( float ) / pv->job->arate;
122     buf->stop  = buf->start + 90000 * pv->input_samples / pv->job->arate / 2;
123     buf->size  = faacEncEncode( pv->faac, (int32_t *) pv->buf,
124             pv->input_samples, buf->data, pv->output_bytes );
125     buf->key   = 1;
126
127     if( !buf->size )
128     {
129         /* Encoding was successful but we got no data. Try to encode
130            more */
131         hb_buffer_close( &buf );
132         return Encode( w );
133     }
134     else if( buf->size < 0 )
135     {
136         hb_log( "faacEncEncode failed" );
137         hb_buffer_close( &buf );
138         return NULL;
139     }
140
141     return buf;
142 }
143
144 /***********************************************************************
145  * Work
146  ***********************************************************************
147  *
148  **********************************************************************/
149 int encfaacWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
150                  hb_buffer_t ** buf_out )
151 {
152     hb_work_private_t * pv = w->private_data;
153     hb_buffer_t * buf;
154
155     hb_list_add( pv->list, *buf_in );
156     *buf_in = NULL;
157
158     *buf_out = buf = Encode( w );
159
160     while( buf )
161     {
162         buf->next = Encode( w );
163         buf       = buf->next;
164     }
165     
166     return HB_WORK_OK;
167 }
168