]> git.sesse.net Git - ffmpeg/blob - libavcodec/faac.c
vbr audio encode patch by (Justin Ruggles: jruggle, earthlink net)
[ffmpeg] / libavcodec / faac.c
1 /*
2  * Interface to libfaac for aac encoding
3  * Copyright (c) 2002 Gildas Bazin <gbazin@netcourrier.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19  
20 /**
21  * @file faacaudio.c
22  * Interface to libfaac for aac encoding.
23  */
24
25 #include "avcodec.h"
26 #include <faac.h>
27
28 typedef struct FaacAudioContext {
29     faacEncHandle faac_handle;
30 } FaacAudioContext;
31
32 static int Faac_encode_init(AVCodecContext *avctx)
33 {
34     FaacAudioContext *s = avctx->priv_data;
35     faacEncConfigurationPtr faac_cfg;
36     unsigned long samples_input, max_bytes_output;
37
38     /* number of channels */
39     if (avctx->channels < 1 || avctx->channels > 6)
40         return -1;
41
42     s->faac_handle = faacEncOpen(avctx->sample_rate,
43                                  avctx->channels,
44                                  &samples_input, &max_bytes_output);
45
46     /* check faac version */
47     faac_cfg = faacEncGetCurrentConfiguration(s->faac_handle);
48     if (faac_cfg->version != FAAC_CFG_VERSION) {
49         av_log(avctx, AV_LOG_ERROR, "wrong libfaac version (compiled for: %d, using %d)\n", FAAC_CFG_VERSION, faac_cfg->version);
50         faacEncClose(s->faac_handle);
51         return -1;
52     }
53
54     /* put the options in the configuration struct */
55     faac_cfg->aacObjectType = LOW;
56     faac_cfg->mpegVersion = MPEG4;
57     faac_cfg->useTns = 0;
58     faac_cfg->allowMidside = 1;
59     faac_cfg->bitRate = avctx->bit_rate / avctx->channels;
60     if(avctx->flags & CODEC_FLAG_QSCALE) {
61         faac_cfg->bitRate = 0;
62         faac_cfg->quantqual = avctx->global_quality / FF_QP2LAMBDA;
63     }
64     faac_cfg->outputFormat = 0;
65     faac_cfg->inputFormat = FAAC_INPUT_16BIT;
66
67     if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) {
68         av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n");
69         return -1;
70     }
71
72     avctx->frame_size = samples_input / avctx->channels;
73
74     avctx->coded_frame= avcodec_alloc_frame();
75     avctx->coded_frame->key_frame= 1;
76
77     /* Set decoder specific info */
78     avctx->extradata_size = 0;
79     if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
80
81         unsigned char *buffer;
82         unsigned long decoder_specific_info_size;
83
84         if (!faacEncGetDecoderSpecificInfo(s->faac_handle, &buffer,
85                                            &decoder_specific_info_size)) {
86             avctx->extradata = buffer;
87             avctx->extradata_size = decoder_specific_info_size;
88         }
89     }
90
91     return 0;
92 }
93
94 int Faac_encode_frame(AVCodecContext *avctx,
95                       unsigned char *frame, int buf_size, void *data)
96 {
97     FaacAudioContext *s = avctx->priv_data;
98     int bytes_written;
99
100     bytes_written = faacEncEncode(s->faac_handle,
101                                   data,
102                                   avctx->frame_size * avctx->channels,
103                                   frame,
104                                   buf_size);
105
106     return bytes_written;
107 }
108
109 int Faac_encode_close(AVCodecContext *avctx)
110 {
111     FaacAudioContext *s = avctx->priv_data;
112
113     av_freep(&avctx->coded_frame);
114
115     //if (avctx->extradata_size) free(avctx->extradata);
116
117     faacEncClose(s->faac_handle);
118     return 0;
119 }
120
121 AVCodec faac_encoder = {
122     "aac",
123     CODEC_TYPE_AUDIO,
124     CODEC_ID_AAC,
125     sizeof(FaacAudioContext),
126     Faac_encode_init,
127     Faac_encode_frame,
128     Faac_encode_close
129 };