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 -1;
47  }
48 
49  s->aacplus_handle = aacplusEncOpen(avctx->sample_rate, avctx->channels,
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 = avctx->sample_fmt == AV_SAMPLE_FMT_FLT ? AACPLUS_INPUT_FLOAT : 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  /* Set decoder specific info */
78  avctx->extradata_size = 0;
79  if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
80 
81  unsigned char *buffer = NULL;
82  unsigned long decoder_specific_info_size;
83 
84  if (aacplusEncGetDecoderSpecificInfo(s->aacplus_handle, &buffer,
85  &decoder_specific_info_size) == 1) {
86  avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE);
87  avctx->extradata_size = decoder_specific_info_size;
88  memcpy(avctx->extradata, buffer, avctx->extradata_size);
89  }
90  free(buffer);
91  }
92  return 0;
93 }
94 
96  const AVFrame *frame, int *got_packet)
97 {
99  int32_t *input_buffer = (int32_t *)frame->data[0];
100  int ret;
101 
102  if ((ret = ff_alloc_packet2(avctx, pkt, s->max_output_bytes)) < 0)
103  return ret;
104 
105  pkt->size = aacplusEncEncode(s->aacplus_handle, input_buffer,
106  s->samples_input, pkt->data, pkt->size);
107  *got_packet = 1;
108  pkt->pts = frame->pts;
109  return 0;
110 }
111 
113 {
114  aacPlusAudioContext *s = avctx->priv_data;
115 
116  av_freep(&avctx->extradata);
117 
118  aacplusEncClose(s->aacplus_handle);
119  return 0;
120 }
121 
122 static const AVProfile profiles[] = {
123  { FF_PROFILE_AAC_LOW, "LC" },
124  { FF_PROFILE_UNKNOWN },
125 };
126 
128  .name = "libaacplus",
129  .type = AVMEDIA_TYPE_AUDIO,
130  .id = AV_CODEC_ID_AAC,
131  .priv_data_size = sizeof(aacPlusAudioContext),
133  .encode2 = aacPlus_encode_frame,
135  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
138  .long_name = NULL_IF_CONFIG_SMALL("libaacplus AAC+ (Advanced Audio Codec with SBR+PS)"),
139  .profiles = profiles,
140  .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
142  0 },
143 };
const char * s
Definition: avisynth_c.h:668
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static int aacPlus_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: libaacplus.c:95
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static av_cold int aacPlus_encode_close(AVCodecContext *avctx)
Definition: libaacplus.c:112
#define AV_CH_LAYOUT_STEREO
signed 16 bits
Definition: samplefmt.h:52
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
enum AVSampleFormat sample_fmt
audio sample format
#define av_cold
Definition: attributes.h:78
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
static AVPacket pkt
Definition: demuxing.c:56
#define FF_PROFILE_UNKNOWN
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
uint8_t * data
unsigned long samples_input
Definition: libaacplus.c:35
unsigned long max_output_bytes
Definition: libaacplus.c:34
frame
Definition: stft.m:14
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. ...
int flags
CODEC_FLAG_*.
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
external API header
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int bit_rate
the average bitrate
ret
Definition: avfilter.c:821
struct aacPlusAudioContext aacPlusAudioContext
int32_t
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
int frame_size
Number of samples per channel in an audio frame.
NULL
Definition: eval.c:55
int sample_rate
samples per second
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
#define FF_PROFILE_AAC_LOW
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
aacplusEncHandle aacplus_handle
Definition: libaacplus.c:33
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
common internal api header.
static const AVProfile profiles[]
Definition: libaacplus.c:122
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
AVProfile.
the buffer and buffer reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFilterBuffer structures They must not be accessed but through references stored in AVFilterBufferRef structures Several references can point to the same buffer
int cutoff
Audio cutoff bandwidth (0 means "automatic")
int channels
number of audio channels
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
#define AV_CH_LAYOUT_MONO
AVCodec ff_libaacplus_encoder
Definition: libaacplus.c:127
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
MUSIC TECHNOLOGY GROUP UNIVERSITAT POMPEU FABRA Free Non Commercial Binary License Agreement UNIVERSITAT POMPEU OR INDICATING ACCEPTANCE BY SELECTING THE ACCEPT BUTTON ON ANY DOWNLOAD OR INSTALL YOU ACCEPT THE TERMS OF THE LICENSE SUMMARY TABLE Software MELODIA Melody Extraction vamp plug in Licensor Music Technology Group Universitat Pompeu Plaça de la Spain Permitted purposes Non commercial internal research and validation and educational purposes only All commercial uses in a production either internal or are prohibited by this license and require an additional commercial exploitation license TERMS AND CONDITIONS SOFTWARE Software means the software programs identified herein in binary any other machine readable any updates or error corrections provided by and any user programming guides and other documentation provided to you by UPF under this Agreement LICENSE Subject to the terms and conditions of this UPF grants you a royalty free