libfaac.c
Go to the documentation of this file.
1 /*
2  * Interface to libfaac for aac encoding
3  * Copyright (c) 2002 Gildas Bazin <gbazin@netcourrier.com>
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "avcodec.h"
28 #include <faac.h>
29 
30 typedef struct FaacAudioContext {
31  faacEncHandle faac_handle;
33 
35 {
36  FaacAudioContext *s = avctx->priv_data;
37  faacEncConfigurationPtr faac_cfg;
38  unsigned long samples_input, max_bytes_output;
39 
40  /* number of channels */
41  if (avctx->channels < 1 || avctx->channels > 6) {
42  av_log(avctx, AV_LOG_ERROR, "encoding %d channel(s) is not allowed\n", avctx->channels);
43  return -1;
44  }
45 
46  s->faac_handle = faacEncOpen(avctx->sample_rate,
47  avctx->channels,
48  &samples_input, &max_bytes_output);
49 
50  /* check faac version */
51  faac_cfg = faacEncGetCurrentConfiguration(s->faac_handle);
52  if (faac_cfg->version != FAAC_CFG_VERSION) {
53  av_log(avctx, AV_LOG_ERROR, "wrong libfaac version (compiled for: %d, using %d)\n", FAAC_CFG_VERSION, faac_cfg->version);
54  faacEncClose(s->faac_handle);
55  return -1;
56  }
57 
58  /* put the options in the configuration struct */
59  switch(avctx->profile) {
61  faac_cfg->aacObjectType = MAIN;
62  break;
63  case FF_PROFILE_UNKNOWN:
64  case FF_PROFILE_AAC_LOW:
65  faac_cfg->aacObjectType = LOW;
66  break;
67  case FF_PROFILE_AAC_SSR:
68  faac_cfg->aacObjectType = SSR;
69  break;
70  case FF_PROFILE_AAC_LTP:
71  faac_cfg->aacObjectType = LTP;
72  break;
73  default:
74  av_log(avctx, AV_LOG_ERROR, "invalid AAC profile\n");
75  faacEncClose(s->faac_handle);
76  return -1;
77  }
78  faac_cfg->mpegVersion = MPEG4;
79  faac_cfg->useTns = 0;
80  faac_cfg->allowMidside = 1;
81  faac_cfg->bitRate = avctx->bit_rate / avctx->channels;
82  faac_cfg->bandWidth = avctx->cutoff;
83  if(avctx->flags & CODEC_FLAG_QSCALE) {
84  faac_cfg->bitRate = 0;
85  faac_cfg->quantqual = avctx->global_quality / FF_QP2LAMBDA;
86  }
87  faac_cfg->outputFormat = 1;
88  faac_cfg->inputFormat = FAAC_INPUT_16BIT;
89 
90  avctx->frame_size = samples_input / avctx->channels;
91 
93  avctx->coded_frame->key_frame= 1;
94 
95  /* Set decoder specific info */
96  avctx->extradata_size = 0;
97  if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
98 
99  unsigned char *buffer = NULL;
100  unsigned long decoder_specific_info_size;
101 
102  if (!faacEncGetDecoderSpecificInfo(s->faac_handle, &buffer,
103  &decoder_specific_info_size)) {
104  avctx->extradata = av_malloc(decoder_specific_info_size + FF_INPUT_BUFFER_PADDING_SIZE);
105  avctx->extradata_size = decoder_specific_info_size;
106  memcpy(avctx->extradata, buffer, avctx->extradata_size);
107  faac_cfg->outputFormat = 0;
108  }
109 #undef free
110  free(buffer);
111 #define free please_use_av_free
112  }
113 
114  if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) {
115  av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n");
116  return -1;
117  }
118 
119  return 0;
120 }
121 
123  unsigned char *frame, int buf_size, void *data)
124 {
125  FaacAudioContext *s = avctx->priv_data;
126  int bytes_written;
127  int num_samples = data ? avctx->frame_size : 0;
128 
129  bytes_written = faacEncEncode(s->faac_handle,
130  data,
131  num_samples * avctx->channels,
132  frame,
133  buf_size);
134 
135  return bytes_written;
136 }
137 
139 {
140  FaacAudioContext *s = avctx->priv_data;
141 
142  av_freep(&avctx->coded_frame);
143  av_freep(&avctx->extradata);
144 
145  faacEncClose(s->faac_handle);
146  return 0;
147 }
148 
149 static const AVProfile profiles[] = {
150  { FF_PROFILE_AAC_MAIN, "Main" },
151  { FF_PROFILE_AAC_LOW, "LC" },
152  { FF_PROFILE_AAC_SSR, "SSR" },
153  { FF_PROFILE_AAC_LTP, "LTP" },
154  { FF_PROFILE_UNKNOWN },
155 };
156 
158  .name = "libfaac",
159  .type = AVMEDIA_TYPE_AUDIO,
160  .id = CODEC_ID_AAC,
161  .priv_data_size = sizeof(FaacAudioContext),
163  .encode = Faac_encode_frame,
166  .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
167  .long_name = NULL_IF_CONFIG_SMALL("libfaac AAC (Advanced Audio Codec)"),
168  .profiles = NULL_IF_CONFIG_SMALL(profiles),
169 };
faacEncHandle faac_handle
Definition: libfaac.c:31
static int Faac_encode_frame(AVCodecContext *avctx, unsigned char *frame, int buf_size, void *data)
Definition: libfaac.c:122
static av_cold int Faac_encode_init(AVCodecContext *avctx)
Definition: libfaac.c:34
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2000
#define FF_PROFILE_AAC_MAIN
Definition: avcodec.h:2466
#define free
signed 16 bits
Definition: samplefmt.h:30
int profile
profile
Definition: avcodec.h:2462
AVCodec.
Definition: avcodec.h:3189
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:147
#define av_cold
Definition: attributes.h:71
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
static av_cold int Faac_encode_close(AVCodecContext *avctx)
Definition: libfaac.c:138
AVCodec ff_libfaac_encoder
Definition: libfaac.c:157
const char data[16]
Definition: mxf.c:60
#define FF_PROFILE_AAC_LTP
Definition: avcodec.h:2469
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
#define CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: avcodec.h:755
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1355
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:618
int bit_rate
the average bitrate
Definition: avcodec.h:1340
#define FF_PROFILE_AAC_LOW
Definition: avcodec.h:2467
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2463
static char buffer[20]
Definition: seek-test.c:34
int frame_size
Samples per packet, initialized when calling 'init'.
Definition: avcodec.h:1470
NULL
Definition: eval.c:50
external API header
int sample_rate
samples per second
Definition: avcodec.h:1456
main external API structure.
Definition: avcodec.h:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
#define CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:627
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
#define FF_PROFILE_AAC_SSR
Definition: avcodec.h:2468
int extradata_size
Definition: avcodec.h:1388
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:64
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:648
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:497
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:2208
static const AVProfile profiles[]
Definition: libfaac.c:149
AVSampleFormat
all in native-endian format
Definition: samplefmt.h:27
AVProfile.
Definition: avcodec.h:3179
void * priv_data
Definition: avcodec.h:1531
int cutoff
Audio cutoff bandwidth (0 means "automatic")
Definition: avcodec.h:2741
struct FaacAudioContext FaacAudioContext
int channels
number of audio channels
Definition: avcodec.h:1457
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1022
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:250
#define MAIN
Definition: vf_overlay.c:58
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:750