FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libaacplus.c
Go to the documentation of this file.
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;
37 
39 {
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 AVERROR(EINVAL);
47  }
48 
49  if (avctx->profile != FF_PROFILE_AAC_LOW && avctx->profile != FF_PROFILE_UNKNOWN) {
50  av_log(avctx, AV_LOG_ERROR, "invalid AAC profile: %d, only LC supported\n", avctx->profile);
51  return AVERROR(EINVAL);
52  }
53 
54  s->aacplus_handle = aacplusEncOpen(avctx->sample_rate, avctx->channels,
56  if (!s->aacplus_handle) {
57  av_log(avctx, AV_LOG_ERROR, "can't open encoder\n");
58  return AVERROR(EINVAL);
59  }
60 
61  /* check aacplus version */
62  aacplus_cfg = aacplusEncGetCurrentConfiguration(s->aacplus_handle);
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 = avctx->sample_fmt == AV_SAMPLE_FMT_FLT ? AACPLUS_INPUT_FLOAT : 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 AVERROR(EINVAL);
71  }
72 
73  avctx->frame_size = s->samples_input / avctx->channels;
74 
75  /* Set decoder specific info */
76  avctx->extradata_size = 0;
77  if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
78 
79  unsigned char *buffer = NULL;
80  unsigned long decoder_specific_info_size;
81 
82  if (aacplusEncGetDecoderSpecificInfo(s->aacplus_handle, &buffer,
83  &decoder_specific_info_size) == 1) {
84  avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE);
85  avctx->extradata_size = decoder_specific_info_size;
86  memcpy(avctx->extradata, buffer, avctx->extradata_size);
87  }
88  free(buffer);
89  }
90  return 0;
91 }
92 
94  const AVFrame *frame, int *got_packet)
95 {
97  int32_t *input_buffer = (int32_t *)frame->data[0];
98  int ret;
99 
100  if ((ret = ff_alloc_packet2(avctx, pkt, s->max_output_bytes)) < 0)
101  return ret;
102 
103  pkt->size = aacplusEncEncode(s->aacplus_handle, input_buffer,
104  s->samples_input, pkt->data, pkt->size);
105  *got_packet = 1;
106  pkt->pts = frame->pts;
107  return 0;
108 }
109 
111 {
112  aacPlusAudioContext *s = avctx->priv_data;
113 
114  av_freep(&avctx->extradata);
115  aacplusEncClose(s->aacplus_handle);
116 
117  return 0;
118 }
119 
120 static const AVProfile profiles[] = {
121  { FF_PROFILE_AAC_LOW, "LC" },
122  { FF_PROFILE_UNKNOWN },
123 };
124 
126  .name = "libaacplus",
127  .long_name = NULL_IF_CONFIG_SMALL("libaacplus AAC+ (Advanced Audio Codec with SBR+PS)"),
128  .type = AVMEDIA_TYPE_AUDIO,
129  .id = AV_CODEC_ID_AAC,
130  .priv_data_size = sizeof(aacPlusAudioContext),
132  .encode2 = aacPlus_encode_frame,
134  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
137  .profiles = profiles,
138  .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
140  0 },
141 };