]> git.sesse.net Git - ffmpeg/blob - libavcodec/libaacplus.c
Merge commit '892f037c55d86ce36f8705fbeab052189312a13e'
[ffmpeg] / libavcodec / libaacplus.c
1 /*
2  * Interface to libaacplus for aac+ (sbr+ps) encoding
3  * Copyright (c) 2010 tipok <piratfm@gmail.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 libaacplus for aac+ (sbr+ps) encoding.
25  */
26
27 #include <aacplus.h>
28
29 #include "avcodec.h"
30 #include "internal.h"
31
32 typedef struct aacPlusAudioContext {
33     aacplusEncHandle aacplus_handle;
34     unsigned long max_output_bytes;
35     unsigned long samples_input;
36 } aacPlusAudioContext;
37
38 static int aacPlus_encode_close(AVCodecContext *avctx);
39
40 static av_cold int aacPlus_encode_init(AVCodecContext *avctx)
41 {
42     aacPlusAudioContext *s = avctx->priv_data;
43     aacplusEncConfiguration *aacplus_cfg;
44
45     /* number of channels */
46     if (avctx->channels < 1 || avctx->channels > 2) {
47         av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels);
48         return AVERROR(EINVAL);
49     }
50
51     if (avctx->profile != FF_PROFILE_AAC_LOW && avctx->profile != FF_PROFILE_UNKNOWN) {
52         av_log(avctx, AV_LOG_ERROR, "invalid AAC profile: %d, only LC supported\n", avctx->profile);
53         return AVERROR(EINVAL);
54     }
55
56     s->aacplus_handle = aacplusEncOpen(avctx->sample_rate, avctx->channels,
57                                        &s->samples_input, &s->max_output_bytes);
58     if (!s->aacplus_handle) {
59         av_log(avctx, AV_LOG_ERROR, "can't open encoder\n");
60         return AVERROR(EINVAL);
61     }
62
63     /* check aacplus version */
64     aacplus_cfg = aacplusEncGetCurrentConfiguration(s->aacplus_handle);
65
66     aacplus_cfg->bitRate = avctx->bit_rate;
67     aacplus_cfg->bandWidth = avctx->cutoff;
68     aacplus_cfg->outputFormat = !(avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER);
69     aacplus_cfg->inputFormat = avctx->sample_fmt == AV_SAMPLE_FMT_FLT ? AACPLUS_INPUT_FLOAT : AACPLUS_INPUT_16BIT;
70     if (!aacplusEncSetConfiguration(s->aacplus_handle, aacplus_cfg)) {
71         av_log(avctx, AV_LOG_ERROR, "libaacplus doesn't support this output format!\n");
72         aacPlus_encode_close(avctx);
73         return AVERROR(EINVAL);
74     }
75
76     avctx->frame_size = s->samples_input / avctx->channels;
77
78     /* Set decoder specific info */
79     avctx->extradata_size = 0;
80     if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
81
82         unsigned char *buffer = NULL;
83         unsigned long decoder_specific_info_size;
84
85         if (aacplusEncGetDecoderSpecificInfo(s->aacplus_handle, &buffer,
86                                            &decoder_specific_info_size) == 1) {
87             avctx->extradata = av_malloc(decoder_specific_info_size + AV_INPUT_BUFFER_PADDING_SIZE);
88             if (!avctx->extradata) {
89                 free(buffer);
90                 aacPlus_encode_close(avctx);
91                 return AVERROR(ENOMEM);
92             }
93             avctx->extradata_size = decoder_specific_info_size;
94             memcpy(avctx->extradata, buffer, avctx->extradata_size);
95         }
96         free(buffer);
97     }
98     return 0;
99 }
100
101 static int aacPlus_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
102                                 const AVFrame *frame, int *got_packet)
103 {
104     aacPlusAudioContext *s = avctx->priv_data;
105     int32_t *input_buffer = (int32_t *)frame->data[0];
106     int ret;
107
108     if ((ret = ff_alloc_packet2(avctx, pkt, s->max_output_bytes, 0)) < 0)
109         return ret;
110
111     pkt->size = aacplusEncEncode(s->aacplus_handle, input_buffer,
112                                  s->samples_input, pkt->data, pkt->size);
113     *got_packet   = 1;
114     pkt->pts      = frame->pts;
115     return 0;
116 }
117
118 static av_cold int aacPlus_encode_close(AVCodecContext *avctx)
119 {
120     aacPlusAudioContext *s = avctx->priv_data;
121
122     av_freep(&avctx->extradata);
123     aacplusEncClose(s->aacplus_handle);
124     s->aacplus_handle = NULL;
125
126     return 0;
127 }
128
129 static const AVProfile profiles[] = {
130     { FF_PROFILE_AAC_LOW, "LC" },
131     { FF_PROFILE_UNKNOWN },
132 };
133
134 AVCodec ff_libaacplus_encoder = {
135     .name           = "libaacplus",
136     .long_name      = NULL_IF_CONFIG_SMALL("libaacplus AAC+ (Advanced Audio Codec with SBR+PS)"),
137     .type           = AVMEDIA_TYPE_AUDIO,
138     .id             = AV_CODEC_ID_AAC,
139     .priv_data_size = sizeof(aacPlusAudioContext),
140     .init           = aacPlus_encode_init,
141     .encode2        = aacPlus_encode_frame,
142     .close          = aacPlus_encode_close,
143     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
144                                                      AV_SAMPLE_FMT_FLT,
145                                                      AV_SAMPLE_FMT_NONE },
146     .profiles       = profiles,
147     .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
148                                             AV_CH_LAYOUT_STEREO,
149                                             0 },
150 };