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 "audio_frame_queue.h"
27 #include "internal.h"
28 #include "mpeg4audio.h"
29 
30 #define FRAME_SIZE 1024
31 #define ENC_DELAY 1600
32 
33 typedef struct AACContext {
34  VO_AUDIO_CODECAPI codec_api;
35  VO_HANDLE handle;
36  VO_MEM_OPERATOR mem_operator;
37  VO_CODEC_INIT_USERDATA user_data;
38  VO_PBYTE end_buffer;
42 } AACContext;
43 
44 
45 static int aac_encode_close(AVCodecContext *avctx)
46 {
47  AACContext *s = avctx->priv_data;
48 
49  s->codec_api.Uninit(s->handle);
50 #if FF_API_OLD_ENCODE_AUDIO
51  av_freep(&avctx->coded_frame);
52 #endif
53  av_freep(&avctx->extradata);
55  av_freep(&s->end_buffer);
56 
57  return 0;
58 }
59 
61 {
62  AACContext *s = avctx->priv_data;
63  AACENC_PARAM params = { 0 };
64  int index, ret;
65 
66 #if FF_API_OLD_ENCODE_AUDIO
68  if (!avctx->coded_frame)
69  return AVERROR(ENOMEM);
70 #endif
71  avctx->frame_size = FRAME_SIZE;
72  avctx->delay = ENC_DELAY;
73  s->last_frame = 2;
74  ff_af_queue_init(avctx, &s->afq);
75 
76  s->end_buffer = av_mallocz(avctx->frame_size * avctx->channels * 2);
77  if (!s->end_buffer) {
78  ret = AVERROR(ENOMEM);
79  goto error;
80  }
81 
82  voGetAACEncAPI(&s->codec_api);
83 
84  s->mem_operator.Alloc = cmnMemAlloc;
85  s->mem_operator.Copy = cmnMemCopy;
86  s->mem_operator.Free = cmnMemFree;
87  s->mem_operator.Set = cmnMemSet;
88  s->mem_operator.Check = cmnMemCheck;
89  s->user_data.memflag = VO_IMF_USERMEMOPERATOR;
90  s->user_data.memData = &s->mem_operator;
91  s->codec_api.Init(&s->handle, VO_AUDIO_CodingAAC, &s->user_data);
92 
93  params.sampleRate = avctx->sample_rate;
94  params.bitRate = avctx->bit_rate;
95  params.nChannels = avctx->channels;
96  params.adtsUsed = !(avctx->flags & CODEC_FLAG_GLOBAL_HEADER);
97  if (s->codec_api.SetParam(s->handle, VO_PID_AAC_ENCPARAM, &params)
98  != VO_ERR_NONE) {
99  av_log(avctx, AV_LOG_ERROR, "Unable to set encoding parameters\n");
100  ret = AVERROR(EINVAL);
101  goto error;
102  }
103 
104  for (index = 0; index < 16; index++)
105  if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[index])
106  break;
107  if (index == 16) {
108  av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n",
109  avctx->sample_rate);
110  ret = AVERROR(ENOSYS);
111  goto error;
112  }
113  if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
114  avctx->extradata_size = 2;
115  avctx->extradata = av_mallocz(avctx->extradata_size +
117  if (!avctx->extradata) {
118  ret = AVERROR(ENOMEM);
119  goto error;
120  }
121 
122  avctx->extradata[0] = 0x02 << 3 | index >> 1;
123  avctx->extradata[1] = (index & 0x01) << 7 | avctx->channels << 3;
124  }
125  return 0;
126 error:
127  aac_encode_close(avctx);
128  return ret;
129 }
130 
131 static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
132  const AVFrame *frame, int *got_packet_ptr)
133 {
134  AACContext *s = avctx->priv_data;
135  VO_CODECBUFFER input = { 0 }, output = { 0 };
136  VO_AUDIO_OUTPUTINFO output_info = { { 0 } };
137  VO_PBYTE samples;
138  int ret;
139 
140  /* handle end-of-stream small frame and flushing */
141  if (!frame) {
142  if (s->last_frame <= 0)
143  return 0;
144  if (s->last_samples > 0 && s->last_samples < ENC_DELAY - FRAME_SIZE) {
145  s->last_samples = 0;
146  s->last_frame--;
147  }
148  s->last_frame--;
149  memset(s->end_buffer, 0, 2 * avctx->channels * avctx->frame_size);
150  samples = s->end_buffer;
151  } else {
152  if (frame->nb_samples < avctx->frame_size) {
153  s->last_samples = frame->nb_samples;
154  memcpy(s->end_buffer, frame->data[0], 2 * avctx->channels * frame->nb_samples);
155  samples = s->end_buffer;
156  } else {
157  samples = (VO_PBYTE)frame->data[0];
158  }
159  /* add current frame to the queue */
160  if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
161  return ret;
162  }
163 
164  if ((ret = ff_alloc_packet(avpkt, FFMAX(8192, 768 * avctx->channels)))) {
165  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
166  return ret;
167  }
168 
169  input.Buffer = samples;
170  input.Length = 2 * avctx->channels * avctx->frame_size;
171  output.Buffer = avpkt->data;
172  output.Length = avpkt->size;
173 
174  s->codec_api.SetInputData(s->handle, &input);
175  if (s->codec_api.GetOutputData(s->handle, &output, &output_info)
176  != VO_ERR_NONE) {
177  av_log(avctx, AV_LOG_ERROR, "Unable to encode frame\n");
178  return AVERROR(EINVAL);
179  }
180 
181  /* Get the next frame pts/duration */
182  ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
183  &avpkt->duration);
184 
185  avpkt->size = output.Length;
186  *got_packet_ptr = 1;
187  return 0;
188 }
189 
191  .name = "libvo_aacenc",
192  .type = AVMEDIA_TYPE_AUDIO,
193  .id = AV_CODEC_ID_AAC,
194  .priv_data_size = sizeof(AACContext),
196  .encode2 = aac_encode_frame,
199  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
201  .long_name = NULL_IF_CONFIG_SMALL("Android VisualOn AAC (Advanced Audio Coding)"),
202 };
#define ENC_DELAY
Definition: libvo-aacenc.c:31
static int16_t * samples
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
HANDLE_AACENCODER handle
Definition: libfdk-aacenc.c:33
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
int size
Definition: avcodec.h:916
signed 16 bits
Definition: samplefmt.h:52
AVCodec.
Definition: avcodec.h:2960
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:151
AVCodec ff_libvo_aacenc_encoder
Definition: libvo-aacenc.c:190
AudioFrameQueue afq
Definition: libfdk-aacenc.c:41
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
uint8_t * data
Definition: avcodec.h:915
static av_cold int aac_encode_init(AVCodecContext *avctx)
Definition: libvo-aacenc.c:60
VO_PBYTE end_buffer
Definition: libvo-aacenc.c:38
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:937
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
VO_MEM_OPERATOR mem_operator
Definition: libvo-aacenc.c:36
sample_fmts
Definition: avconv_filter.c:63
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1434
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
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:1404
VO_CODEC_INIT_USERDATA user_data
Definition: libvo-aacenc.c:37
int last_frame
Definition: libvo-aacenc.c:40
#define FRAME_SIZE
Definition: libvo-aacenc.c:30
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:880
int last_samples
Definition: libvo-aacenc.c:41
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2124
external API header
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:63
int sample_rate
samples per second
Definition: avcodec.h:2104
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
int extradata_size
Definition: avcodec.h:1455
int index
Definition: gxfenc.c:72
main AAC context
Definition: aac.h:263
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
const int avpriv_mpeg4audio_sample_rates[16]
Definition: mpeg4audio.c:55
static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libvo-aacenc.c:131
static int aac_encode_close(AVCodecContext *avctx)
Definition: libvo-aacenc.c:45
common internal api header.
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
void * priv_data
Definition: avcodec.h:1382
void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int *duration)
Remove frame(s) from the queue.
int channels
number of audio channels
Definition: avcodec.h:2105
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
VO_AUDIO_CODECAPI codec_api
Definition: libvo-aacenc.c:34
This structure stores compressed data.
Definition: avcodec.h:898
int delay
Codec delay.
Definition: avcodec.h:1497
int nb_samples
number of audio samples (per channel) described by this frame
Definition: avcodec.h:1042
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:158
VO_HANDLE handle
Definition: libvo-aacenc.c:35
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:908