|
808
|
1 /*
|
|
|
2 * Interface to libfaac for aac encoding
|
|
|
3 * Copyright (c) 2002 Gildas Bazin <gbazin@netcourrier.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 faacaudio.c
|
|
|
24 * Interface to libfaac for aac encoding.
|
|
|
25 */
|
|
|
26
|
|
|
27 #include "avcodec.h"
|
|
|
28 #include <faac.h>
|
|
|
29
|
|
|
30 typedef struct FaacAudioContext {
|
|
|
31 faacEncHandle faac_handle;
|
|
|
32 } FaacAudioContext;
|
|
|
33
|
|
|
34 static int Faac_encode_init(AVCodecContext *avctx)
|
|
|
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 return -1;
|
|
|
43
|
|
|
44 s->faac_handle = faacEncOpen(avctx->sample_rate,
|
|
|
45 avctx->channels,
|
|
|
46 &samples_input, &max_bytes_output);
|
|
|
47
|
|
|
48 /* check faac version */
|
|
|
49 faac_cfg = faacEncGetCurrentConfiguration(s->faac_handle);
|
|
|
50 if (faac_cfg->version != FAAC_CFG_VERSION) {
|
|
|
51 av_log(avctx, AV_LOG_ERROR, "wrong libfaac version (compiled for: %d, using %d)\n", FAAC_CFG_VERSION, faac_cfg->version);
|
|
|
52 faacEncClose(s->faac_handle);
|
|
|
53 return -1;
|
|
|
54 }
|
|
|
55
|
|
|
56 /* put the options in the configuration struct */
|
|
|
57 faac_cfg->aacObjectType = LOW;
|
|
|
58 faac_cfg->mpegVersion = MPEG4;
|
|
|
59 faac_cfg->useTns = 0;
|
|
|
60 faac_cfg->allowMidside = 1;
|
|
|
61 faac_cfg->bitRate = avctx->bit_rate / avctx->channels;
|
|
|
62 faac_cfg->bandWidth = avctx->cutoff;
|
|
|
63 if(avctx->flags & CODEC_FLAG_QSCALE) {
|
|
|
64 faac_cfg->bitRate = 0;
|
|
|
65 faac_cfg->quantqual = avctx->global_quality / FF_QP2LAMBDA;
|
|
|
66 }
|
|
|
67 faac_cfg->outputFormat = 1;
|
|
|
68 faac_cfg->inputFormat = FAAC_INPUT_16BIT;
|
|
|
69
|
|
|
70 avctx->frame_size = samples_input / avctx->channels;
|
|
|
71
|
|
|
72 avctx->coded_frame= avcodec_alloc_frame();
|
|
|
73 avctx->coded_frame->key_frame= 1;
|
|
|
74
|
|
|
75 /* Set decoder specific info */
|
|
|
76 avctx->extradata_size = 0;
|
|
|
77 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
|
|
|
78
|
|
|
79 unsigned char *buffer;
|
|
|
80 unsigned long decoder_specific_info_size;
|
|
|
81
|
|
|
82 if (!faacEncGetDecoderSpecificInfo(s->faac_handle, &buffer,
|
|
|
83 &decoder_specific_info_size)) {
|
|
|
84 avctx->extradata = buffer;
|
|
|
85 avctx->extradata_size = decoder_specific_info_size;
|
|
|
86 faac_cfg->outputFormat = 0;
|
|
|
87 }
|
|
|
88 }
|
|
|
89
|
|
|
90 if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) {
|
|
|
91 av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n");
|
|
|
92 return -1;
|
|
|
93 }
|
|
|
94
|
|
|
95 return 0;
|
|
|
96 }
|
|
|
97
|
|
|
98 int Faac_encode_frame(AVCodecContext *avctx,
|
|
|
99 unsigned char *frame, int buf_size, void *data)
|
|
|
100 {
|
|
|
101 FaacAudioContext *s = avctx->priv_data;
|
|
|
102 int bytes_written;
|
|
|
103
|
|
|
104 bytes_written = faacEncEncode(s->faac_handle,
|
|
|
105 data,
|
|
|
106 avctx->frame_size * avctx->channels,
|
|
|
107 frame,
|
|
|
108 buf_size);
|
|
|
109
|
|
|
110 return bytes_written;
|
|
|
111 }
|
|
|
112
|
|
|
113 int Faac_encode_close(AVCodecContext *avctx)
|
|
|
114 {
|
|
|
115 FaacAudioContext *s = avctx->priv_data;
|
|
|
116
|
|
|
117 av_freep(&avctx->coded_frame);
|
|
|
118
|
|
|
119 //if (avctx->extradata_size) free(avctx->extradata);
|
|
|
120
|
|
|
121 faacEncClose(s->faac_handle);
|
|
|
122 return 0;
|
|
|
123 }
|
|
|
124
|
|
|
125 AVCodec faac_encoder = {
|
|
|
126 "aac",
|
|
|
127 CODEC_TYPE_AUDIO,
|
|
|
128 CODEC_ID_AAC,
|
|
|
129 sizeof(FaacAudioContext),
|
|
|
130 Faac_encode_init,
|
|
|
131 Faac_encode_frame,
|
|
|
132 Faac_encode_close
|
|
|
133 };
|