]> git.sesse.net Git - ffmpeg/blob - libavcodec/libfaac.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / libfaac.c
1 /*
2  * Interface to libfaac for aac encoding
3  * Copyright (c) 2002 Gildas Bazin <gbazin@netcourrier.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Interface to libfaac for aac encoding.
25  */
26
27 #include "avcodec.h"
28 #include <faac.h>
29
30 typedef struct FaacAudioContext {
31     faacEncHandle faac_handle;
32 } FaacAudioContext;
33
34 static const int channel_maps[][6] = {
35     { 2, 0, 1 },          //< C L R
36     { 2, 0, 1, 3 },       //< C L R Cs
37     { 2, 0, 1, 3, 4 },    //< C L R Ls Rs
38     { 2, 0, 1, 4, 5, 3 }, //< C L R Ls Rs LFE
39 };
40
41 static av_cold int Faac_encode_close(AVCodecContext *avctx)
42 {
43     FaacAudioContext *s = avctx->priv_data;
44
45     av_freep(&avctx->coded_frame);
46     av_freep(&avctx->extradata);
47
48     if (s->faac_handle)
49         faacEncClose(s->faac_handle);
50     return 0;
51 }
52
53 static av_cold int Faac_encode_init(AVCodecContext *avctx)
54 {
55     FaacAudioContext *s = avctx->priv_data;
56     faacEncConfigurationPtr faac_cfg;
57     unsigned long samples_input, max_bytes_output;
58     int ret;
59
60     /* number of channels */
61     if (avctx->channels < 1 || avctx->channels > 6) {
62         av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels);
63         ret = AVERROR(EINVAL);
64         goto error;
65     }
66
67     s->faac_handle = faacEncOpen(avctx->sample_rate,
68                                  avctx->channels,
69                                  &samples_input, &max_bytes_output);
70     if (!s->faac_handle) {
71         av_log(avctx, AV_LOG_ERROR, "error in faacEncOpen()\n");
72         ret = AVERROR_UNKNOWN;
73         goto error;
74     }
75
76     /* check faac version */
77     faac_cfg = faacEncGetCurrentConfiguration(s->faac_handle);
78     if (faac_cfg->version != FAAC_CFG_VERSION) {
79         av_log(avctx, AV_LOG_ERROR, "wrong libfaac version (compiled for: %d, using %d)\n", FAAC_CFG_VERSION, faac_cfg->version);
80         ret = AVERROR(EINVAL);
81         goto error;
82     }
83
84     /* put the options in the configuration struct */
85     switch(avctx->profile) {
86         case FF_PROFILE_AAC_MAIN:
87             faac_cfg->aacObjectType = MAIN;
88             break;
89         case FF_PROFILE_UNKNOWN:
90         case FF_PROFILE_AAC_LOW:
91             faac_cfg->aacObjectType = LOW;
92             break;
93         case FF_PROFILE_AAC_SSR:
94             faac_cfg->aacObjectType = SSR;
95             break;
96         case FF_PROFILE_AAC_LTP:
97             faac_cfg->aacObjectType = LTP;
98             break;
99         default:
100             av_log(avctx, AV_LOG_ERROR, "invalid AAC profile\n");
101             ret = AVERROR(EINVAL);
102             goto error;
103     }
104     faac_cfg->mpegVersion = MPEG4;
105     faac_cfg->useTns = 0;
106     faac_cfg->allowMidside = 1;
107     faac_cfg->bitRate = avctx->bit_rate / avctx->channels;
108     faac_cfg->bandWidth = avctx->cutoff;
109     if(avctx->flags & CODEC_FLAG_QSCALE) {
110         faac_cfg->bitRate = 0;
111         faac_cfg->quantqual = avctx->global_quality / FF_QP2LAMBDA;
112     }
113     faac_cfg->outputFormat = 1;
114     faac_cfg->inputFormat = FAAC_INPUT_16BIT;
115     if (avctx->channels > 2)
116         memcpy(faac_cfg->channel_map, channel_maps[avctx->channels-3],
117                avctx->channels * sizeof(int));
118
119     avctx->frame_size = samples_input / avctx->channels;
120
121     avctx->coded_frame= avcodec_alloc_frame();
122     if (!avctx->coded_frame) {
123         ret = AVERROR(ENOMEM);
124         goto error;
125     }
126
127     /* Set decoder specific info */
128     avctx->extradata_size = 0;
129     if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
130
131         unsigned char *buffer = NULL;
132         unsigned long decoder_specific_info_size;
133
134         if (!faacEncGetDecoderSpecificInfo(s->faac_handle, &buffer,
135                                            &decoder_specific_info_size)) {
136             avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE);
137             if (!avctx->extradata) {
138                 ret = AVERROR(ENOMEM);
139                 goto error;
140             }
141             avctx->extradata_size = decoder_specific_info_size;
142             memcpy(avctx->extradata, buffer, avctx->extradata_size);
143             faac_cfg->outputFormat = 0;
144         }
145 #undef free
146         free(buffer);
147 #define free please_use_av_free
148     }
149
150     if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) {
151         av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n");
152         ret = AVERROR(EINVAL);
153         goto error;
154     }
155
156     return 0;
157 error:
158     Faac_encode_close(avctx);
159     return ret;
160 }
161
162 static int Faac_encode_frame(AVCodecContext *avctx,
163                              unsigned char *frame, int buf_size, void *data)
164 {
165     FaacAudioContext *s = avctx->priv_data;
166     int bytes_written;
167     int num_samples = data ? avctx->frame_size : 0;
168
169     bytes_written = faacEncEncode(s->faac_handle,
170                                   data,
171                                   num_samples * avctx->channels,
172                                   frame,
173                                   buf_size);
174
175     return bytes_written;
176 }
177
178 static const AVProfile profiles[] = {
179     { FF_PROFILE_AAC_MAIN, "Main" },
180     { FF_PROFILE_AAC_LOW,  "LC"   },
181     { FF_PROFILE_AAC_SSR,  "SSR"  },
182     { FF_PROFILE_AAC_LTP,  "LTP"  },
183     { FF_PROFILE_UNKNOWN },
184 };
185
186 AVCodec ff_libfaac_encoder = {
187     .name           = "libfaac",
188     .type           = AVMEDIA_TYPE_AUDIO,
189     .id             = CODEC_ID_AAC,
190     .priv_data_size = sizeof(FaacAudioContext),
191     .init           = Faac_encode_init,
192     .encode         = Faac_encode_frame,
193     .close          = Faac_encode_close,
194     .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
195     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
196     .long_name = NULL_IF_CONFIG_SMALL("libfaac AAC (Advanced Audio Codec)"),
197     .profiles = NULL_IF_CONFIG_SMALL(profiles),
198 };