]> git.sesse.net Git - ffmpeg/blob - libavcodec/libaacplus.c
Merge commit 'b146d74730ab9ec5abede9066f770ad851e45fbc'
[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 av_cold int aacPlus_encode_init(AVCodecContext *avctx)
39 {
40     aacPlusAudioContext *s = avctx->priv_data;
41     aacplusEncConfiguration *aacplus_cfg;
42
43     /* number of channels */
44     if (avctx->channels < 1 || avctx->channels > 2) {
45         av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels);
46         return -1;
47     }
48
49     s->aacplus_handle = aacplusEncOpen(avctx->sample_rate, avctx->channels,
50                                        &s->samples_input, &s->max_output_bytes);
51     if(!s->aacplus_handle) {
52             av_log(avctx, AV_LOG_ERROR, "can't open encoder\n");
53             return -1;
54     }
55
56     /* check aacplus version */
57     aacplus_cfg = aacplusEncGetCurrentConfiguration(s->aacplus_handle);
58
59     /* put the options in the configuration struct */
60     if(avctx->profile != FF_PROFILE_AAC_LOW && avctx->profile != FF_PROFILE_UNKNOWN) {
61             av_log(avctx, AV_LOG_ERROR, "invalid AAC profile: %d, only LC supported\n", avctx->profile);
62             aacplusEncClose(s->aacplus_handle);
63             return -1;
64     }
65
66     aacplus_cfg->bitRate = avctx->bit_rate;
67     aacplus_cfg->bandWidth = avctx->cutoff;
68     aacplus_cfg->outputFormat = !(avctx->flags & CODEC_FLAG_GLOBAL_HEADER);
69     aacplus_cfg->inputFormat = 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         return -1;
73     }
74
75     avctx->frame_size = s->samples_input / avctx->channels;
76
77 #if FF_API_OLD_ENCODE_AUDIO
78     avctx->coded_frame= avcodec_alloc_frame();
79     avctx->coded_frame->key_frame= 1;
80 #endif
81
82     /* Set decoder specific info */
83     avctx->extradata_size = 0;
84     if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
85
86         unsigned char *buffer = NULL;
87         unsigned long decoder_specific_info_size;
88
89         if (aacplusEncGetDecoderSpecificInfo(s->aacplus_handle, &buffer,
90                                            &decoder_specific_info_size) == 1) {
91             avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE);
92             avctx->extradata_size = decoder_specific_info_size;
93             memcpy(avctx->extradata, buffer, avctx->extradata_size);
94         }
95 #undef free
96         free(buffer);
97 #define free please_use_av_free
98     }
99     return 0;
100 }
101
102 static int aacPlus_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
103                                 const AVFrame *frame, int *got_packet)
104 {
105     aacPlusAudioContext *s = avctx->priv_data;
106     int32_t *input_buffer = (int32_t *)frame->data[0];
107     int ret;
108
109     if ((ret = ff_alloc_packet2(avctx, pkt, s->max_output_bytes)))
110         return ret;
111
112     pkt->size = aacplusEncEncode(s->aacplus_handle, input_buffer,
113                                  s->samples_input, pkt->data, pkt->size);
114     *got_packet   = 1;
115     pkt->pts      = frame->pts;
116     return 0;
117 }
118
119 static av_cold int aacPlus_encode_close(AVCodecContext *avctx)
120 {
121     aacPlusAudioContext *s = avctx->priv_data;
122
123 #if FF_API_OLD_ENCODE_AUDIO
124     av_freep(&avctx->coded_frame);
125 #endif
126     av_freep(&avctx->extradata);
127
128     aacplusEncClose(s->aacplus_handle);
129     return 0;
130 }
131
132 AVCodec ff_libaacplus_encoder = {
133     .name           = "libaacplus",
134     .type           = AVMEDIA_TYPE_AUDIO,
135     .id             = AV_CODEC_ID_AAC,
136     .priv_data_size = sizeof(aacPlusAudioContext),
137     .init           = aacPlus_encode_init,
138     .encode2        = aacPlus_encode_frame,
139     .close          = aacPlus_encode_close,
140     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
141                                                      AV_SAMPLE_FMT_NONE },
142     .long_name      = NULL_IF_CONFIG_SMALL("libaacplus AAC+ (Advanced Audio Codec with SBR+PS)"),
143 };