libvo-aacenc.c
Go to the documentation of this file.
1 /*
2  * AAC encoder wrapper
3  * Copyright (c) 2010 Martin Storsjo
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 
22 #include <vo-aacenc/voAAC.h>
23 #include <vo-aacenc/cmnMemory.h>
24 
25 #include "avcodec.h"
26 #include "mpeg4audio.h"
27 
28 typedef struct AACContext {
29  VO_AUDIO_CODECAPI codec_api;
30  VO_HANDLE handle;
31  VO_MEM_OPERATOR mem_operator;
32  VO_CODEC_INIT_USERDATA user_data;
33 } AACContext;
34 
36 {
37  AACContext *s = avctx->priv_data;
38  AACENC_PARAM params = { 0 };
39  int index;
40 
42  avctx->frame_size = 1024;
43 
44  voGetAACEncAPI(&s->codec_api);
45 
46  s->mem_operator.Alloc = cmnMemAlloc;
47  s->mem_operator.Copy = cmnMemCopy;
48  s->mem_operator.Free = cmnMemFree;
49  s->mem_operator.Set = cmnMemSet;
50  s->mem_operator.Check = cmnMemCheck;
51  s->user_data.memflag = VO_IMF_USERMEMOPERATOR;
52  s->user_data.memData = &s->mem_operator;
53  s->codec_api.Init(&s->handle, VO_AUDIO_CodingAAC, &s->user_data);
54 
55  params.sampleRate = avctx->sample_rate;
56  params.bitRate = avctx->bit_rate;
57  params.nChannels = avctx->channels;
58  params.adtsUsed = !(avctx->flags & CODEC_FLAG_GLOBAL_HEADER);
59  if (s->codec_api.SetParam(s->handle, VO_PID_AAC_ENCPARAM, &params)
60  != VO_ERR_NONE) {
61  av_log(avctx, AV_LOG_ERROR, "Unable to set encoding parameters\n");
62  return AVERROR(EINVAL);
63  }
64 
65  for (index = 0; index < 16; index++)
66  if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[index])
67  break;
68  if (index == 16) {
69  av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n",
70  avctx->sample_rate);
71  return AVERROR(ENOSYS);
72  }
73  if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
74  avctx->extradata_size = 2;
75  avctx->extradata = av_mallocz(avctx->extradata_size +
77  if (!avctx->extradata)
78  return AVERROR(ENOMEM);
79 
80  avctx->extradata[0] = 0x02 << 3 | index >> 1;
81  avctx->extradata[1] = (index & 0x01) << 7 | avctx->channels << 3;
82  }
83  return 0;
84 }
85 
86 static int aac_encode_close(AVCodecContext *avctx)
87 {
88  AACContext *s = avctx->priv_data;
89 
90  s->codec_api.Uninit(s->handle);
91  av_freep(&avctx->coded_frame);
92 
93  return 0;
94 }
95 
96 static int aac_encode_frame(AVCodecContext *avctx,
97  unsigned char *frame/*out*/,
98  int buf_size, void *data/*in*/)
99 {
100  AACContext *s = avctx->priv_data;
101  VO_CODECBUFFER input = { 0 }, output = { 0 };
102  VO_AUDIO_OUTPUTINFO output_info = { { 0 } };
103 
104  input.Buffer = data;
105  input.Length = 2 * avctx->channels * avctx->frame_size;
106  output.Buffer = frame;
107  output.Length = buf_size;
108 
109  s->codec_api.SetInputData(s->handle, &input);
110  if (s->codec_api.GetOutputData(s->handle, &output, &output_info)
111  != VO_ERR_NONE) {
112  av_log(avctx, AV_LOG_ERROR, "Unable to encode frame\n");
113  return AVERROR(EINVAL);
114  }
115  return output.Length;
116 }
117 
119  .name = "libvo_aacenc",
120  .type = AVMEDIA_TYPE_AUDIO,
121  .id = CODEC_ID_AAC,
122  .priv_data_size = sizeof(AACContext),
124  .encode = aac_encode_frame,
126  .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
127  .long_name = NULL_IF_CONFIG_SMALL("Android VisualOn AAC"),
128 };
129 
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:154
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2000
signed 16 bits
Definition: samplefmt.h:30
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
AVCodec ff_libvo_aacenc_encoder
Definition: libvo-aacenc.c:118
static int aac_encode_frame(AVCodecContext *avctx, unsigned char *frame, int buf_size, void *data)
Definition: libvo-aacenc.c:96
#define av_cold
Definition: attributes.h:71
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
const char data[16]
Definition: mxf.c:60
static av_cold int aac_encode_init(AVCodecContext *avctx)
Definition: libvo-aacenc.c:35
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
VO_MEM_OPERATOR mem_operator
Definition: libvo-aacenc.c:31
#define AVERROR(e)
Definition: error.h:43
#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
struct AACContext AACContext
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
VO_CODEC_INIT_USERDATA user_data
Definition: libvo-aacenc.c:32
int frame_size
Samples per packet, initialized when calling 'init'.
Definition: avcodec.h:1470
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 AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
int extradata_size
Definition: avcodec.h:1388
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:648
int index
Definition: gxfenc.c:73
#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
main AAC context
Definition: aac.h:253
const int avpriv_mpeg4audio_sample_rates[16]
Definition: mpeg4audio.c:55
static int aac_encode_close(AVCodecContext *avctx)
Definition: libvo-aacenc.c:86
AVSampleFormat
all in native-endian format
Definition: samplefmt.h:27
void * priv_data
Definition: avcodec.h:1531
int channels
number of audio channels
Definition: avcodec.h:1457
VO_AUDIO_CODECAPI codec_api
Definition: libvo-aacenc.c:29
VO_HANDLE handle
Definition: libvo-aacenc.c:30