annotate faac.c @ 3845:59f260f0f5bb libavcodec

Original Commit: r42 | ods15 | 2006-09-23 10:53:16 +0300 (Sat, 23 Sep 2006) | 2 lines fix for residue end
author ods15
date Mon, 02 Oct 2006 05:56:32 +0000
parents a2a116fee880
children c8c591fe26f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1500
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
1 /*
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
2 * Interface to libfaac for aac encoding
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
3 * Copyright (c) 2002 Gildas Bazin <gbazin@netcourrier.com>
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
4 *
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
9 *
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
13 * Lesser General Public License for more details.
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
14 *
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 3031
diff changeset
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1500
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
18 */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2841
diff changeset
19
1500
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
20 /**
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
21 * @file faacaudio.c
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
22 * Interface to libfaac for aac encoding.
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
23 */
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
24
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
25 #include "avcodec.h"
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
26 #include <faac.h>
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
27
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
28 typedef struct FaacAudioContext {
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
29 faacEncHandle faac_handle;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
30 } FaacAudioContext;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
31
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
32 static int Faac_encode_init(AVCodecContext *avctx)
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
33 {
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
34 FaacAudioContext *s = avctx->priv_data;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
35 faacEncConfigurationPtr faac_cfg;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
36 unsigned long samples_input, max_bytes_output;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
37
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
38 /* number of channels */
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
39 if (avctx->channels < 1 || avctx->channels > 6)
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
40 return -1;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
41
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
42 s->faac_handle = faacEncOpen(avctx->sample_rate,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
43 avctx->channels,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
44 &samples_input, &max_bytes_output);
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
45
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
46 /* check faac version */
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
47 faac_cfg = faacEncGetCurrentConfiguration(s->faac_handle);
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
48 if (faac_cfg->version != FAAC_CFG_VERSION) {
2979
bfabfdf9ce55 COSMETICS: tabs --> spaces, some prettyprinting
diego
parents: 2967
diff changeset
49 av_log(avctx, AV_LOG_ERROR, "wrong libfaac version (compiled for: %d, using %d)\n", FAAC_CFG_VERSION, faac_cfg->version);
1500
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
50 faacEncClose(s->faac_handle);
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
51 return -1;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
52 }
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
53
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
54 /* put the options in the configuration struct */
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
55 faac_cfg->aacObjectType = LOW;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
56 faac_cfg->mpegVersion = MPEG4;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
57 faac_cfg->useTns = 0;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
58 faac_cfg->allowMidside = 1;
2841
bceeca1bb30f vbr audio encode patch by (Justin Ruggles: jruggle, earthlink net)
michael
parents: 1826
diff changeset
59 faac_cfg->bitRate = avctx->bit_rate / avctx->channels;
3031
0f5f311f2f94 Parameter added for user to set the cutoff frequency when encoding with faac.
banan
parents: 2979
diff changeset
60 faac_cfg->bandWidth = avctx->cutoff;
2841
bceeca1bb30f vbr audio encode patch by (Justin Ruggles: jruggle, earthlink net)
michael
parents: 1826
diff changeset
61 if(avctx->flags & CODEC_FLAG_QSCALE) {
bceeca1bb30f vbr audio encode patch by (Justin Ruggles: jruggle, earthlink net)
michael
parents: 1826
diff changeset
62 faac_cfg->bitRate = 0;
bceeca1bb30f vbr audio encode patch by (Justin Ruggles: jruggle, earthlink net)
michael
parents: 1826
diff changeset
63 faac_cfg->quantqual = avctx->global_quality / FF_QP2LAMBDA;
bceeca1bb30f vbr audio encode patch by (Justin Ruggles: jruggle, earthlink net)
michael
parents: 1826
diff changeset
64 }
3099
a2a116fee880 create adts stream if CODEC_FLAG_GLOBAL_HEADER not set
mru
parents: 3036
diff changeset
65 faac_cfg->outputFormat = 1;
1500
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
66 faac_cfg->inputFormat = FAAC_INPUT_16BIT;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
67
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
68 avctx->frame_size = samples_input / avctx->channels;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
69
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
70 avctx->coded_frame= avcodec_alloc_frame();
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
71 avctx->coded_frame->key_frame= 1;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
72
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
73 /* Set decoder specific info */
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
74 avctx->extradata_size = 0;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
75 if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
76
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
77 unsigned char *buffer;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
78 unsigned long decoder_specific_info_size;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
79
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
80 if (!faacEncGetDecoderSpecificInfo(s->faac_handle, &buffer,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
81 &decoder_specific_info_size)) {
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
82 avctx->extradata = buffer;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
83 avctx->extradata_size = decoder_specific_info_size;
3099
a2a116fee880 create adts stream if CODEC_FLAG_GLOBAL_HEADER not set
mru
parents: 3036
diff changeset
84 faac_cfg->outputFormat = 0;
1500
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
85 }
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
86 }
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
87
3099
a2a116fee880 create adts stream if CODEC_FLAG_GLOBAL_HEADER not set
mru
parents: 3036
diff changeset
88 if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) {
a2a116fee880 create adts stream if CODEC_FLAG_GLOBAL_HEADER not set
mru
parents: 3036
diff changeset
89 av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n");
a2a116fee880 create adts stream if CODEC_FLAG_GLOBAL_HEADER not set
mru
parents: 3036
diff changeset
90 return -1;
a2a116fee880 create adts stream if CODEC_FLAG_GLOBAL_HEADER not set
mru
parents: 3036
diff changeset
91 }
a2a116fee880 create adts stream if CODEC_FLAG_GLOBAL_HEADER not set
mru
parents: 3036
diff changeset
92
1500
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
93 return 0;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
94 }
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
95
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
96 int Faac_encode_frame(AVCodecContext *avctx,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
97 unsigned char *frame, int buf_size, void *data)
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
98 {
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
99 FaacAudioContext *s = avctx->priv_data;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
100 int bytes_written;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
101
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
102 bytes_written = faacEncEncode(s->faac_handle,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
103 data,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
104 avctx->frame_size * avctx->channels,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
105 frame,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
106 buf_size);
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
107
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
108 return bytes_written;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
109 }
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
110
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
111 int Faac_encode_close(AVCodecContext *avctx)
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
112 {
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
113 FaacAudioContext *s = avctx->priv_data;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
114
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
115 av_freep(&avctx->coded_frame);
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
116
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
117 //if (avctx->extradata_size) free(avctx->extradata);
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
118
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
119 faacEncClose(s->faac_handle);
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
120 return 0;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
121 }
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
122
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
123 AVCodec faac_encoder = {
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
124 "aac",
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
125 CODEC_TYPE_AUDIO,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
126 CODEC_ID_AAC,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
127 sizeof(FaacAudioContext),
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
128 Faac_encode_init,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
129 Faac_encode_frame,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
130 Faac_encode_close
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
131 };