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 & AV_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 & AV_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 + AV_INPUT_BUFFER_PADDING_SIZE);
85  if (!avctx->extradata) {
86  free(buffer);
87  return AVERROR(ENOMEM);
88  }
89  avctx->extradata_size = decoder_specific_info_size;
90  memcpy(avctx->extradata, buffer, avctx->extradata_size);
91  }
92  free(buffer);
93  }
94  return 0;
95 }
96 
98  const AVFrame *frame, int *got_packet)
99 {
100  aacPlusAudioContext *s = avctx->priv_data;
101  int32_t *input_buffer = (int32_t *)frame->data[0];
102  int ret;
103 
104  if ((ret = ff_alloc_packet2(avctx, pkt, s->max_output_bytes, 0)) < 0)
105  return ret;
106 
107  pkt->size = aacplusEncEncode(s->aacplus_handle, input_buffer,
108  s->samples_input, pkt->data, pkt->size);
109  *got_packet = 1;
110  pkt->pts = frame->pts;
111  return 0;
112 }
113 
115 {
116  aacPlusAudioContext *s = avctx->priv_data;
117 
118  av_freep(&avctx->extradata);
119  aacplusEncClose(s->aacplus_handle);
120 
121  return 0;
122 }
123 
124 static const AVProfile profiles[] = {
125  { FF_PROFILE_AAC_LOW, "LC" },
126  { FF_PROFILE_UNKNOWN },
127 };
128 
130  .name = "libaacplus",
131  .long_name = NULL_IF_CONFIG_SMALL("libaacplus AAC+ (Advanced Audio Codec with SBR+PS)"),
132  .type = AVMEDIA_TYPE_AUDIO,
133  .id = AV_CODEC_ID_AAC,
134  .priv_data_size = sizeof(aacPlusAudioContext),
136  .encode2 = aacPlus_encode_frame,
137  .close = aacPlus_encode_close,
138  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
141  .profiles = profiles,
142  .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
144  0 },
145 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
static int aacPlus_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: libaacplus.c:97
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1722
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1589
static av_cold int aacPlus_encode_close(AVCodecContext *avctx)
Definition: libaacplus.c:114
static AVPacket pkt
#define AV_CH_LAYOUT_STEREO
int profile
profile
Definition: avcodec.h:3161
AVCodec.
Definition: avcodec.h:3559
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2426
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:268
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1772
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1588
unsigned long samples_input
Definition: libaacplus.c:35
unsigned long max_output_bytes
Definition: libaacplus.c:34
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
static av_cold int aacPlus_encode_init(AVCodecContext *avctx)
Definition: libaacplus.c:38
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1752
const char * name
Name of the codec implementation.
Definition: avcodec.h:3566
#define FF_PROFILE_AAC_LOW
Definition: avcodec.h:3166
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:3162
int32_t
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2438
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
int sample_rate
samples per second
Definition: avcodec.h:2418
main external API structure.
Definition: avcodec.h:1657
int extradata_size
Definition: avcodec.h:1773
aacplusEncHandle aacplus_handle
Definition: libaacplus.c:33
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1690
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:879
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:282
signed 16 bits
Definition: samplefmt.h:61
static const AVProfile profiles[]
Definition: libaacplus.c:124
AVProfile.
Definition: avcodec.h:3547
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:731
void * priv_data
Definition: avcodec.h:1699
int cutoff
Audio cutoff bandwidth (0 means "automatic")
Definition: avcodec.h:2462
int channels
number of audio channels
Definition: avcodec.h:2419
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
#define av_freep(p)
#define AV_CH_LAYOUT_MONO
AVCodec ff_libaacplus_encoder
Definition: libaacplus.c:129
This structure stores compressed data.
Definition: avcodec.h:1565
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1581
GLuint buffer
Definition: opengl_enc.c:102