]> git.sesse.net Git - ffmpeg/blob - libavcodec/libvo-aacenc.c
roqaudioenc: return AVERROR codes instead of -1
[ffmpeg] / libavcodec / libvo-aacenc.c
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
35 static av_cold int aac_encode_init(AVCodecContext *avctx)
36 {
37     AACContext *s = avctx->priv_data;
38     AACENC_PARAM params = { 0 };
39     int index;
40
41     avctx->coded_frame = avcodec_alloc_frame();
42     if (!avctx->coded_frame)
43         return AVERROR(ENOMEM);
44     avctx->frame_size = 1024;
45
46     voGetAACEncAPI(&s->codec_api);
47
48     s->mem_operator.Alloc = cmnMemAlloc;
49     s->mem_operator.Copy = cmnMemCopy;
50     s->mem_operator.Free = cmnMemFree;
51     s->mem_operator.Set = cmnMemSet;
52     s->mem_operator.Check = cmnMemCheck;
53     s->user_data.memflag = VO_IMF_USERMEMOPERATOR;
54     s->user_data.memData = &s->mem_operator;
55     s->codec_api.Init(&s->handle, VO_AUDIO_CodingAAC, &s->user_data);
56
57     params.sampleRate = avctx->sample_rate;
58     params.bitRate    = avctx->bit_rate;
59     params.nChannels  = avctx->channels;
60     params.adtsUsed   = !(avctx->flags & CODEC_FLAG_GLOBAL_HEADER);
61     if (s->codec_api.SetParam(s->handle, VO_PID_AAC_ENCPARAM, &params)
62         != VO_ERR_NONE) {
63         av_log(avctx, AV_LOG_ERROR, "Unable to set encoding parameters\n");
64         return AVERROR(EINVAL);
65     }
66
67     for (index = 0; index < 16; index++)
68         if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[index])
69             break;
70     if (index == 16) {
71         av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n",
72                                     avctx->sample_rate);
73         return AVERROR(ENOSYS);
74     }
75     if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
76         avctx->extradata_size = 2;
77         avctx->extradata      = av_mallocz(avctx->extradata_size +
78                                            FF_INPUT_BUFFER_PADDING_SIZE);
79         if (!avctx->extradata)
80             return AVERROR(ENOMEM);
81
82         avctx->extradata[0] = 0x02 << 3 | index >> 1;
83         avctx->extradata[1] = (index & 0x01) << 7 | avctx->channels << 3;
84     }
85     return 0;
86 }
87
88 static int aac_encode_close(AVCodecContext *avctx)
89 {
90     AACContext *s = avctx->priv_data;
91
92     s->codec_api.Uninit(s->handle);
93     av_freep(&avctx->coded_frame);
94
95     return 0;
96 }
97
98 static int aac_encode_frame(AVCodecContext *avctx,
99                             unsigned char *frame/*out*/,
100                             int buf_size, void *data/*in*/)
101 {
102     AACContext *s = avctx->priv_data;
103     VO_CODECBUFFER input = { 0 }, output = { 0 };
104     VO_AUDIO_OUTPUTINFO output_info = { { 0 } };
105
106     input.Buffer = data;
107     input.Length = 2 * avctx->channels * avctx->frame_size;
108     output.Buffer = frame;
109     output.Length = buf_size;
110
111     s->codec_api.SetInputData(s->handle, &input);
112     if (s->codec_api.GetOutputData(s->handle, &output, &output_info)
113         != VO_ERR_NONE) {
114         av_log(avctx, AV_LOG_ERROR, "Unable to encode frame\n");
115         return AVERROR(EINVAL);
116     }
117     return output.Length;
118 }
119
120 AVCodec ff_libvo_aacenc_encoder = {
121     .name           = "libvo_aacenc",
122     .type           = AVMEDIA_TYPE_AUDIO,
123     .id             = CODEC_ID_AAC,
124     .priv_data_size = sizeof(AACContext),
125     .init           = aac_encode_init,
126     .encode         = aac_encode_frame,
127     .close          = aac_encode_close,
128     .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
129     .long_name = NULL_IF_CONFIG_SMALL("Android VisualOn AAC"),
130 };