]> git.sesse.net Git - ffmpeg/blob - libavcodec/libaacplus.c
Merge remote-tracking branch 'qatar/master'
[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 "avcodec.h"
28 #include <aacplus.h>
29
30 typedef struct aacPlusAudioContext {
31     aacplusEncHandle aacplus_handle;
32 } aacPlusAudioContext;
33
34 static av_cold int aacPlus_encode_init(AVCodecContext *avctx)
35 {
36     aacPlusAudioContext *s = avctx->priv_data;
37     aacplusEncConfiguration *aacplus_cfg;
38     unsigned long samples_input, max_bytes_output;
39
40     /* number of channels */
41     if (avctx->channels < 1 || avctx->channels > 2) {
42         av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels);
43         return -1;
44     }
45
46     s->aacplus_handle = aacplusEncOpen(avctx->sample_rate,
47                                  avctx->channels,
48                                  &samples_input, &max_bytes_output);
49     if(!s->aacplus_handle) {
50             av_log(avctx, AV_LOG_ERROR, "can't open encoder\n");
51             return -1;
52     }
53
54     /* check aacplus version */
55     aacplus_cfg = aacplusEncGetCurrentConfiguration(s->aacplus_handle);
56
57     /* put the options in the configuration struct */
58     if(avctx->profile != FF_PROFILE_AAC_LOW && avctx->profile != FF_PROFILE_UNKNOWN) {
59             av_log(avctx, AV_LOG_ERROR, "invalid AAC profile: %d, only LC supported\n", avctx->profile);
60             aacplusEncClose(s->aacplus_handle);
61             return -1;
62     }
63
64     aacplus_cfg->bitRate = avctx->bit_rate;
65     aacplus_cfg->bandWidth = avctx->cutoff;
66     aacplus_cfg->outputFormat = !(avctx->flags & CODEC_FLAG_GLOBAL_HEADER);
67     aacplus_cfg->inputFormat = AACPLUS_INPUT_16BIT;
68     if (!aacplusEncSetConfiguration(s->aacplus_handle, aacplus_cfg)) {
69         av_log(avctx, AV_LOG_ERROR, "libaacplus doesn't support this output format!\n");
70         return -1;
71     }
72
73     avctx->frame_size = samples_input / avctx->channels;
74
75     avctx->coded_frame= avcodec_alloc_frame();
76     avctx->coded_frame->key_frame= 1;
77
78     /* Set decoder specific info */
79     avctx->extradata_size = 0;
80     if (avctx->flags & 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 + FF_INPUT_BUFFER_PADDING_SIZE);
88             avctx->extradata_size = decoder_specific_info_size;
89             memcpy(avctx->extradata, buffer, avctx->extradata_size);
90         }
91 #undef free
92         free(buffer);
93 #define free please_use_av_free
94     }
95     return 0;
96 }
97
98 static int aacPlus_encode_frame(AVCodecContext *avctx,
99                              unsigned char *frame, int buf_size, void *data)
100 {
101     aacPlusAudioContext *s = avctx->priv_data;
102     int bytes_written;
103
104     bytes_written = aacplusEncEncode(s->aacplus_handle,
105                                   data,
106                                   avctx->frame_size * avctx->channels,
107                                   frame,
108                                   buf_size);
109
110     return bytes_written;
111 }
112
113 static av_cold int aacPlus_encode_close(AVCodecContext *avctx)
114 {
115     aacPlusAudioContext *s = avctx->priv_data;
116
117     av_freep(&avctx->coded_frame);
118     av_freep(&avctx->extradata);
119
120     aacplusEncClose(s->aacplus_handle);
121     return 0;
122 }
123
124 AVCodec ff_libaacplus_encoder = {
125     .name           = "libaacplus",
126     .type           = AVMEDIA_TYPE_AUDIO,
127     .id             = CODEC_ID_AAC,
128     .priv_data_size = sizeof(aacPlusAudioContext),
129     .init           = aacPlus_encode_init,
130     .encode         = aacPlus_encode_frame,
131     .close          = aacPlus_encode_close,
132     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
133     .long_name = NULL_IF_CONFIG_SMALL("libaacplus AAC+ (Advanced Audio Codec with SBR+PS)"),
134 };