yading@10
|
1 /*
|
yading@10
|
2 * Interface to libaacplus for aac+ (sbr+ps) encoding
|
yading@10
|
3 * Copyright (c) 2010 tipok <piratfm@gmail.com>
|
yading@10
|
4 *
|
yading@10
|
5 * This file is part of FFmpeg.
|
yading@10
|
6 *
|
yading@10
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
9 * License as published by the Free Software Foundation; either
|
yading@10
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
11 *
|
yading@10
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
15 * Lesser General Public License for more details.
|
yading@10
|
16 *
|
yading@10
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
20 */
|
yading@10
|
21
|
yading@10
|
22 /**
|
yading@10
|
23 * @file
|
yading@10
|
24 * Interface to libaacplus for aac+ (sbr+ps) encoding.
|
yading@10
|
25 */
|
yading@10
|
26
|
yading@10
|
27 #include <aacplus.h>
|
yading@10
|
28
|
yading@10
|
29 #include "avcodec.h"
|
yading@10
|
30 #include "internal.h"
|
yading@10
|
31
|
yading@10
|
32 typedef struct aacPlusAudioContext {
|
yading@10
|
33 aacplusEncHandle aacplus_handle;
|
yading@10
|
34 unsigned long max_output_bytes;
|
yading@10
|
35 unsigned long samples_input;
|
yading@10
|
36 } aacPlusAudioContext;
|
yading@10
|
37
|
yading@10
|
38 static av_cold int aacPlus_encode_init(AVCodecContext *avctx)
|
yading@10
|
39 {
|
yading@10
|
40 aacPlusAudioContext *s = avctx->priv_data;
|
yading@10
|
41 aacplusEncConfiguration *aacplus_cfg;
|
yading@10
|
42
|
yading@10
|
43 /* number of channels */
|
yading@10
|
44 if (avctx->channels < 1 || avctx->channels > 2) {
|
yading@10
|
45 av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels);
|
yading@10
|
46 return -1;
|
yading@10
|
47 }
|
yading@10
|
48
|
yading@10
|
49 s->aacplus_handle = aacplusEncOpen(avctx->sample_rate, avctx->channels,
|
yading@10
|
50 &s->samples_input, &s->max_output_bytes);
|
yading@10
|
51 if(!s->aacplus_handle) {
|
yading@10
|
52 av_log(avctx, AV_LOG_ERROR, "can't open encoder\n");
|
yading@10
|
53 return -1;
|
yading@10
|
54 }
|
yading@10
|
55
|
yading@10
|
56 /* check aacplus version */
|
yading@10
|
57 aacplus_cfg = aacplusEncGetCurrentConfiguration(s->aacplus_handle);
|
yading@10
|
58
|
yading@10
|
59 /* put the options in the configuration struct */
|
yading@10
|
60 if(avctx->profile != FF_PROFILE_AAC_LOW && avctx->profile != FF_PROFILE_UNKNOWN) {
|
yading@10
|
61 av_log(avctx, AV_LOG_ERROR, "invalid AAC profile: %d, only LC supported\n", avctx->profile);
|
yading@10
|
62 aacplusEncClose(s->aacplus_handle);
|
yading@10
|
63 return -1;
|
yading@10
|
64 }
|
yading@10
|
65
|
yading@10
|
66 aacplus_cfg->bitRate = avctx->bit_rate;
|
yading@10
|
67 aacplus_cfg->bandWidth = avctx->cutoff;
|
yading@10
|
68 aacplus_cfg->outputFormat = !(avctx->flags & CODEC_FLAG_GLOBAL_HEADER);
|
yading@10
|
69 aacplus_cfg->inputFormat = avctx->sample_fmt == AV_SAMPLE_FMT_FLT ? AACPLUS_INPUT_FLOAT : AACPLUS_INPUT_16BIT;
|
yading@10
|
70 if (!aacplusEncSetConfiguration(s->aacplus_handle, aacplus_cfg)) {
|
yading@10
|
71 av_log(avctx, AV_LOG_ERROR, "libaacplus doesn't support this output format!\n");
|
yading@10
|
72 return -1;
|
yading@10
|
73 }
|
yading@10
|
74
|
yading@10
|
75 avctx->frame_size = s->samples_input / avctx->channels;
|
yading@10
|
76
|
yading@10
|
77 /* Set decoder specific info */
|
yading@10
|
78 avctx->extradata_size = 0;
|
yading@10
|
79 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
|
yading@10
|
80
|
yading@10
|
81 unsigned char *buffer = NULL;
|
yading@10
|
82 unsigned long decoder_specific_info_size;
|
yading@10
|
83
|
yading@10
|
84 if (aacplusEncGetDecoderSpecificInfo(s->aacplus_handle, &buffer,
|
yading@10
|
85 &decoder_specific_info_size) == 1) {
|
yading@10
|
86 avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE);
|
yading@10
|
87 avctx->extradata_size = decoder_specific_info_size;
|
yading@10
|
88 memcpy(avctx->extradata, buffer, avctx->extradata_size);
|
yading@10
|
89 }
|
yading@10
|
90 free(buffer);
|
yading@10
|
91 }
|
yading@10
|
92 return 0;
|
yading@10
|
93 }
|
yading@10
|
94
|
yading@10
|
95 static int aacPlus_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
|
yading@10
|
96 const AVFrame *frame, int *got_packet)
|
yading@10
|
97 {
|
yading@10
|
98 aacPlusAudioContext *s = avctx->priv_data;
|
yading@10
|
99 int32_t *input_buffer = (int32_t *)frame->data[0];
|
yading@10
|
100 int ret;
|
yading@10
|
101
|
yading@10
|
102 if ((ret = ff_alloc_packet2(avctx, pkt, s->max_output_bytes)) < 0)
|
yading@10
|
103 return ret;
|
yading@10
|
104
|
yading@10
|
105 pkt->size = aacplusEncEncode(s->aacplus_handle, input_buffer,
|
yading@10
|
106 s->samples_input, pkt->data, pkt->size);
|
yading@10
|
107 *got_packet = 1;
|
yading@10
|
108 pkt->pts = frame->pts;
|
yading@10
|
109 return 0;
|
yading@10
|
110 }
|
yading@10
|
111
|
yading@10
|
112 static av_cold int aacPlus_encode_close(AVCodecContext *avctx)
|
yading@10
|
113 {
|
yading@10
|
114 aacPlusAudioContext *s = avctx->priv_data;
|
yading@10
|
115
|
yading@10
|
116 av_freep(&avctx->extradata);
|
yading@10
|
117
|
yading@10
|
118 aacplusEncClose(s->aacplus_handle);
|
yading@10
|
119 return 0;
|
yading@10
|
120 }
|
yading@10
|
121
|
yading@10
|
122 static const AVProfile profiles[] = {
|
yading@10
|
123 { FF_PROFILE_AAC_LOW, "LC" },
|
yading@10
|
124 { FF_PROFILE_UNKNOWN },
|
yading@10
|
125 };
|
yading@10
|
126
|
yading@10
|
127 AVCodec ff_libaacplus_encoder = {
|
yading@10
|
128 .name = "libaacplus",
|
yading@10
|
129 .type = AVMEDIA_TYPE_AUDIO,
|
yading@10
|
130 .id = AV_CODEC_ID_AAC,
|
yading@10
|
131 .priv_data_size = sizeof(aacPlusAudioContext),
|
yading@10
|
132 .init = aacPlus_encode_init,
|
yading@10
|
133 .encode2 = aacPlus_encode_frame,
|
yading@10
|
134 .close = aacPlus_encode_close,
|
yading@10
|
135 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
|
yading@10
|
136 AV_SAMPLE_FMT_FLT,
|
yading@10
|
137 AV_SAMPLE_FMT_NONE },
|
yading@10
|
138 .long_name = NULL_IF_CONFIG_SMALL("libaacplus AAC+ (Advanced Audio Codec with SBR+PS)"),
|
yading@10
|
139 .profiles = profiles,
|
yading@10
|
140 .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
|
yading@10
|
141 AV_CH_LAYOUT_STEREO,
|
yading@10
|
142 0 },
|
yading@10
|
143 };
|