annotate faac.c @ 2463:9baa47d8297b libavcodec

check norm6 vlc validity as there are some bit sequences which dont corespond to any codeword, the other vlc tables all seem to be huffman tables though
author michael
date Tue, 25 Jan 2005 01:29:10 +0000
parents 5fb6572ca817
children bceeca1bb30f
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
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
18 */
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
19
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) {
1826
5fb6572ca817 av_log patch by ("Steven M. Schultz" <sms at 2BSD dot COM>)
michael
parents: 1500
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;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
59 faac_cfg->bitRate = avctx->bit_rate;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
60 faac_cfg->outputFormat = 0;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
61 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
62
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
63 if (!faacEncSetConfiguration(s->faac_handle, faac_cfg)) {
1826
5fb6572ca817 av_log patch by ("Steven M. Schultz" <sms at 2BSD dot COM>)
michael
parents: 1500
diff changeset
64 av_log(avctx, AV_LOG_ERROR, "libfaac doesn't support this output format!\n");
1500
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
65 return -1;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
66 }
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;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
84 }
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 return 0;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
88 }
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
89
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
90 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
91 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
92 {
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
93 FaacAudioContext *s = avctx->priv_data;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
94 int bytes_written;
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 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
97 data,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
98 avctx->frame_size * avctx->channels,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
99 frame,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
100 buf_size);
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 return bytes_written;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
103 }
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
104
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
105 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
106 {
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
107 FaacAudioContext *s = avctx->priv_data;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
108
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
109 av_freep(&avctx->coded_frame);
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 //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
112
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
113 faacEncClose(s->faac_handle);
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
114 return 0;
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
115 }
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 AVCodec faac_encoder = {
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
118 "aac",
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
119 CODEC_TYPE_AUDIO,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
120 CODEC_ID_AAC,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
121 sizeof(FaacAudioContext),
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
122 Faac_encode_init,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
123 Faac_encode_frame,
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
124 Faac_encode_close
726a81d83ddd Patch for AAC encoding with libfaac by (Gildas Bazin <gbazin at altern dot org>)
michaelni
parents:
diff changeset
125 };