annotate libamr.c @ 9657:8a65ae8929fb libavcodec

Fix off-by-one error in MS RLE decoder which may result into writing past picture buffer.
author kostya
date Sat, 16 May 2009 06:39:08 +0000
parents 03361db4df10
children 09b3564c17e5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
1 /*
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
2 * AMR Audio decoder stub
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
3 * Copyright (c) 2003 the ffmpeg project
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
4 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3463
diff changeset
5 * This file is part of FFmpeg.
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3463
diff changeset
6 *
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3463
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3463
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
11 *
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3463
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
15 * Lesser General Public License for more details.
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
16 *
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
3947
c8c591fe26f8 Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 3463
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
3036
0b546eab515d Update licensing information: The FSF changed postal address.
diego
parents: 2979
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1258
802614404398 AMR-NB audio support patch by (<joca at rixmail dot se>)
michaelni
parents:
diff changeset
20 */
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2895
diff changeset
21
4575
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
22 /** @file
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
23 * Adaptive Multi-Rate (AMR) Audio decoder stub.
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
24 *
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
25 * This code implements both an AMR-NarrowBand (AMR-NB) and an AMR-WideBand
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
26 * (AMR-WB) audio encoder/decoder through external reference code from
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
27 * http://www.3gpp.org/. The license of the code from 3gpp is unclear so you
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
28 * have to download the code separately. Two versions exists: One fixed-point
4857
e01ceb104eec Doxygen comment wording/spelling
diego
parents: 4844
diff changeset
29 * and one floating-point. For some reason the float encoder is significantly
e01ceb104eec Doxygen comment wording/spelling
diego
parents: 4844
diff changeset
30 * faster at least on a P4 1.5GHz (0.9s instead of 9.9s on a 30s audio clip
e01ceb104eec Doxygen comment wording/spelling
diego
parents: 4844
diff changeset
31 * at MR102). Both float and fixed point are supported for AMR-NB, but only
e01ceb104eec Doxygen comment wording/spelling
diego
parents: 4844
diff changeset
32 * float for AMR-WB.
4575
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
33 *
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
34 * \section AMR-NB
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
35 *
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
36 * \subsection Float
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
37 * The float version (default) can be downloaded from:
4746
4aedb3b6fa4e Update specification and reference implementation links.
diego
parents: 4744
diff changeset
38 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-610.zip
4575
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
39 *
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
40 * \subsection Specification
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
41 * The specification for AMR-NB can be found in TS 26.071
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
42 * (http://www.3gpp.org/ftp/Specs/html-info/26071.htm) and some other
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
43 * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm.
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
44 *
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
45 * \section AMR-WB
4858
3e38885a28bc Remove the build instructions from the doxygen comments, they are duplicated
diego
parents: 4857
diff changeset
46 *
4575
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
47 * \subsection Float
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
48 * The reference code can be downloaded from:
4746
4aedb3b6fa4e Update specification and reference implementation links.
diego
parents: 4744
diff changeset
49 * http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-600.zip
4575
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
50 *
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
51 * \subsection Specification
4746
4aedb3b6fa4e Update specification and reference implementation links.
diego
parents: 4744
diff changeset
52 * The specification for AMR-WB can be found in TS 26.171
4aedb3b6fa4e Update specification and reference implementation links.
diego
parents: 4744
diff changeset
53 * (http://www.3gpp.org/ftp/Specs/html-info/26171.htm) and some other
4aedb3b6fa4e Update specification and reference implementation links.
diego
parents: 4744
diff changeset
54 * info at http://www.3gpp.org/ftp/Specs/html-info/26-series.htm.
4575
715e6e3ac428 Doxygenize the existing amr.c documentation.
takis
parents: 4565
diff changeset
55 *
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
56 */
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
57
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
58 #include "avcodec.h"
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
59
4843
0e11f292482f Replace hackish support for amr-nb and amr-wb. Instead of including the source
diego
parents: 4842
diff changeset
60 #include <amrnb/interf_dec.h>
0e11f292482f Replace hackish support for amr-nb and amr-wb. Instead of including the source
diego
parents: 4842
diff changeset
61 #include <amrnb/interf_enc.h>
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
62
8347
81238f0bea66 Fix warning "format not a string literal and no format arguments".
benoit
parents: 7451
diff changeset
63 static const char nb_bitrate_unsupported[] =
4793
e69d9ecaaf84 Print a list of valid AMR bitrates if a wrong one is used.
diego
parents: 4789
diff changeset
64 "bitrate not supported: use one of 4.75k, 5.15k, 5.9k, 6.7k, 7.4k, 7.95k, 10.2k or 12.2k\n";
8347
81238f0bea66 Fix warning "format not a string literal and no format arguments".
benoit
parents: 7451
diff changeset
65 static const char wb_bitrate_unsupported[] =
4793
e69d9ecaaf84 Print a list of valid AMR bitrates if a wrong one is used.
diego
parents: 4789
diff changeset
66 "bitrate not supported: use one of 6.6k, 8.85k, 12.65k, 14.25k, 15.85k, 18.25k, 19.85k, 23.05k, or 23.85k\n";
e69d9ecaaf84 Print a list of valid AMR bitrates if a wrong one is used.
diego
parents: 4789
diff changeset
67
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
68 /* Common code for fixed and float version*/
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
69 typedef struct AMR_bitrates {
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
70 int rate;
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
71 enum Mode mode;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
72 } AMR_bitrates;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
73
4744
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
74 /* Match desired bitrate */
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
75 static int getBitrateMode(int bitrate)
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
76 {
4744
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
77 /* make the correspondance between bitrate and mode */
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
78 AMR_bitrates rates[] = { { 4750, MR475},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
79 { 5150, MR515},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
80 { 5900, MR59},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
81 { 6700, MR67},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
82 { 7400, MR74},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
83 { 7950, MR795},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
84 {10200, MR102},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
85 {12200, MR122}, };
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
86 int i;
4633
cd67b7ab311c Cosmetics in amr.c.
takis
parents: 4632
diff changeset
87
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
88 for (i = 0; i < 8; i++)
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
89 if (rates[i].rate == bitrate)
6750
c93570aeb3eb Remove unnecessary parentheses from return calls.
diego
parents: 6712
diff changeset
90 return rates[i].mode;
4744
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
91 /* no bitrate matching, return an error */
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
92 return -1;
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
93 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
94
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
95 static void amr_decode_fix_avctx(AVCodecContext *avctx)
4094
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
96 {
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
97 const int is_amr_wb = 1 + (avctx->codec_id == CODEC_ID_AMR_WB);
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
98
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
99 if (!avctx->sample_rate)
4094
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
100 avctx->sample_rate = 8000 * is_amr_wb;
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
101
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
102 if (!avctx->channels)
4094
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
103 avctx->channels = 1;
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
104
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
105 avctx->frame_size = 160 * is_amr_wb;
7451
85ab7655ad4d Modify all codecs to report their supported input and output sample format(s).
pross
parents: 7040
diff changeset
106 avctx->sample_fmt = SAMPLE_FMT_S16;
4094
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
107 }
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
108
9582
57a2076a2c40 Drop unused libamr-nb fixed-point glue code.
diego
parents: 9358
diff changeset
109 #if CONFIG_LIBAMR_NB
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
110
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
111 typedef struct AMRContext {
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
112 int frameCount;
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
113 void *decState;
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
114 int *enstate;
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
115 int enc_bitrate;
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
116 } AMRContext;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
117
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
118 static av_cold int amr_nb_decode_init(AVCodecContext *avctx)
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
119 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
120 AMRContext *s = avctx->priv_data;
4633
cd67b7ab311c Cosmetics in amr.c.
takis
parents: 4632
diff changeset
121
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
122 s->frameCount = 0;
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
123 s->decState = Decoder_Interface_init();
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
124 if (!s->decState) {
1825
231a6c19e28f av_log()
michael
parents: 1736
diff changeset
125 av_log(avctx, AV_LOG_ERROR, "Decoder_Interface_init error\r\n");
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
126 return -1;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
127 }
4094
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
128
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
129 amr_decode_fix_avctx(avctx);
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
130
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
131 if (avctx->channels > 1) {
4094
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
132 av_log(avctx, AV_LOG_ERROR, "amr_nb: multichannel decoding not supported\n");
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
133 return -1;
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
134 }
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
135
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
136 return 0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
137 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
138
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
139 static av_cold int amr_nb_decode_close(AVCodecContext *avctx)
9651
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
140 {
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
141 AMRContext *s = avctx->priv_data;
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
142
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
143 Decoder_Interface_exit(s->decState);
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
144 return 0;
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
145 }
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
146
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
147 static int amr_nb_decode_frame(AVCodecContext *avctx, void *data,
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
148 int *data_size, AVPacket *avpkt)
9651
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
149 {
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
150 const uint8_t *buf = avpkt->data;
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
151 int buf_size = avpkt->size;
9651
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
152 AMRContext *s = avctx->priv_data;
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
153 const uint8_t *amrData = buf;
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
154 static const uint8_t block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
9651
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
155 enum Mode dec_mode;
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
156 int packet_size;
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
157
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
158 /* av_log(NULL, AV_LOG_DEBUG, "amr_decode_frame buf=%p buf_size=%d frameCount=%d!!\n",
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
159 buf, buf_size, s->frameCount); */
9651
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
160
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
161 dec_mode = (buf[0] >> 3) & 0x000F;
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
162 packet_size = block_size[dec_mode] + 1;
9651
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
163
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
164 if (packet_size > buf_size) {
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
165 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
166 buf_size, packet_size);
9651
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
167 return -1;
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
168 }
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
169
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
170 s->frameCount++;
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
171 /* av_log(NULL, AV_LOG_DEBUG, "packet_size=%d amrData= 0x%X %X %X %X\n",
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
172 packet_size, amrData[0], amrData[1], amrData[2], amrData[3]); */
9651
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
173 /* call decoder */
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
174 Decoder_Interface_Decode(s->decState, amrData, data, 0);
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
175 *data_size = 160 * 2;
9651
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
176
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
177 return packet_size;
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
178 }
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
179
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
180 AVCodec libamr_nb_decoder = {
9651
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
181 "libamr_nb",
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
182 CODEC_TYPE_AUDIO,
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
183 CODEC_ID_AMR_NB,
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
184 sizeof(AMRContext),
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
185 amr_nb_decode_init,
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
186 NULL,
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
187 amr_nb_decode_close,
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
188 amr_nb_decode_frame,
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
189 .long_name = NULL_IF_CONFIG_SMALL("libamr-nb Adaptive Multi-Rate (AMR) Narrow-Band"),
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
190 };
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
191
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
192 static av_cold int amr_nb_encode_init(AVCodecContext *avctx)
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
193 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
194 AMRContext *s = avctx->priv_data;
4633
cd67b7ab311c Cosmetics in amr.c.
takis
parents: 4632
diff changeset
195
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
196 s->frameCount = 0;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2895
diff changeset
197
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
198 if (avctx->sample_rate != 8000) {
4309
1f95aa362562 always print message when error, AV_LOG_DEBUG -> AV_LOG_ERROR
bcoudurier
parents: 4221
diff changeset
199 av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
200 return -1;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
201 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
202
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
203 if (avctx->channels != 1) {
4309
1f95aa362562 always print message when error, AV_LOG_DEBUG -> AV_LOG_ERROR
bcoudurier
parents: 4221
diff changeset
204 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
205 return -1;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
206 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
207
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
208 avctx->frame_size = 160;
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
209 avctx->coded_frame = avcodec_alloc_frame();
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
210
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
211 s->enstate=Encoder_Interface_init(0);
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
212 if (!s->enstate) {
4309
1f95aa362562 always print message when error, AV_LOG_DEBUG -> AV_LOG_ERROR
bcoudurier
parents: 4221
diff changeset
213 av_log(avctx, AV_LOG_ERROR, "Encoder_Interface_init error\n");
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
214 return -1;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
215 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
216
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
217 if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
4793
e69d9ecaaf84 Print a list of valid AMR bitrates if a wrong one is used.
diego
parents: 4789
diff changeset
218 av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
4744
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
219 return -1;
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
220 }
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
221
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
222 return 0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
223 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
224
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
225 static av_cold int amr_nb_encode_close(AVCodecContext *avctx)
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
226 {
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
227 AMRContext *s = avctx->priv_data;
4633
cd67b7ab311c Cosmetics in amr.c.
takis
parents: 4632
diff changeset
228
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
229 Encoder_Interface_exit(s->enstate);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
230 av_freep(&avctx->coded_frame);
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
231 return 0;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
232 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
233
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
234 static int amr_nb_encode_frame(AVCodecContext *avctx,
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
235 unsigned char *frame/*out*/,
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
236 int buf_size, void *data/*in*/)
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
237 {
4634
8b96c50c6723 Remove useless casts as recommended by Mns Rullgrd.
takis
parents: 4633
diff changeset
238 AMRContext *s = avctx->priv_data;
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
239 int written;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
240
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
241 if ((s->enc_bitrate = getBitrateMode(avctx->bit_rate)) < 0) {
4793
e69d9ecaaf84 Print a list of valid AMR bitrates if a wrong one is used.
diego
parents: 4789
diff changeset
242 av_log(avctx, AV_LOG_ERROR, nb_bitrate_unsupported);
4744
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
243 return -1;
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
244 }
4220
4115a044e31d allow amr bitrate to be changed "on the fly" through avctx->bitrate.
gpoirier
parents: 4094
diff changeset
245
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
246 written = Encoder_Interface_Encode(s->enstate, s->enc_bitrate, data,
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
247 frame, 0);
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
248 /* av_log(NULL, AV_LOG_DEBUG, "amr_nb_encode_frame encoded %u bytes, bitrate %u, first byte was %#02x\n",
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
249 written, s->enc_bitrate, frame[0] ); */
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
250
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
251 return written;
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
252 }
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
253
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
254 AVCodec libamr_nb_encoder = {
4898
3df69e140c33 Give libamr decoders/encoders a lib prefix in the name.
diego
parents: 4858
diff changeset
255 "libamr_nb",
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
256 CODEC_TYPE_AUDIO,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
257 CODEC_ID_AMR_NB,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
258 sizeof(AMRContext),
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
259 amr_nb_encode_init,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
260 amr_nb_encode_frame,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
261 amr_nb_encode_close,
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
262 NULL,
7451
85ab7655ad4d Modify all codecs to report their supported input and output sample format(s).
pross
parents: 7040
diff changeset
263 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6750
diff changeset
264 .long_name = NULL_IF_CONFIG_SMALL("libamr-nb Adaptive Multi-Rate (AMR) Narrow-Band"),
1297
1db67cd7808a * 3gp patch by Joca
kabi
parents: 1258
diff changeset
265 };
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
266
2895
8fe1dc88294a Fix some #ifdef to allow building amr_wb alone.
diego
parents: 2571
diff changeset
267 #endif
8fe1dc88294a Fix some #ifdef to allow building amr_wb alone.
diego
parents: 2571
diff changeset
268
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
269 /* -----------AMR wideband ------------*/
8590
7a463923ecd1 Change semantic of CONFIG_*, HAVE_* and ARCH_*.
aurel
parents: 8348
diff changeset
270 #if CONFIG_LIBAMR_WB
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
271
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
272 #ifdef _TYPEDEF_H
6186
diego
parents: 5101
diff changeset
273 //To avoid duplicate typedefs from typedef in amr-nb
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
274 #define typedef_h
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
275 #endif
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
276
4843
0e11f292482f Replace hackish support for amr-nb and amr-wb. Instead of including the source
diego
parents: 4842
diff changeset
277 #include <amrwb/dec_if.h>
0e11f292482f Replace hackish support for amr-nb and amr-wb. Instead of including the source
diego
parents: 4842
diff changeset
278 #include <amrwb/if_rom.h>
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
279
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
280 /* Common code for fixed and float version*/
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
281 typedef struct AMRWB_bitrates {
4744
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
282 int rate;
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
283 int mode;
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
284 } AMRWB_bitrates;
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
285
9652
356079de3642 cosmetics: Move some more code around to avoid #ifdeffery.
diego
parents: 9651
diff changeset
286 typedef struct AMRWBContext {
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
287 int frameCount;
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
288 void *state;
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
289 int mode;
9652
356079de3642 cosmetics: Move some more code around to avoid #ifdeffery.
diego
parents: 9651
diff changeset
290 Word16 allow_dtx;
356079de3642 cosmetics: Move some more code around to avoid #ifdeffery.
diego
parents: 9651
diff changeset
291 } AMRWBContext;
356079de3642 cosmetics: Move some more code around to avoid #ifdeffery.
diego
parents: 9651
diff changeset
292
9653
a572a4628a5a Surround AMR_WB encoding code by appropriate #ifdefs.
diego
parents: 9652
diff changeset
293 #if CONFIG_LIBAMR_WB_ENCODER
a572a4628a5a Surround AMR_WB encoding code by appropriate #ifdefs.
diego
parents: 9652
diff changeset
294
9652
356079de3642 cosmetics: Move some more code around to avoid #ifdeffery.
diego
parents: 9651
diff changeset
295 #include <amrwb/enc_if.h>
356079de3642 cosmetics: Move some more code around to avoid #ifdeffery.
diego
parents: 9651
diff changeset
296
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
297 static int getWBBitrateMode(int bitrate)
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
298 {
4744
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
299 /* make the correspondance between bitrate and mode */
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
300 AMRWB_bitrates rates[] = { { 6600, 0},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
301 { 8850, 1},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
302 {12650, 2},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
303 {14250, 3},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
304 {15850, 4},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
305 {18250, 5},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
306 {19850, 6},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
307 {23050, 7},
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
308 {23850, 8}, };
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
309 int i;
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
310
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
311 for (i = 0; i < 9; i++)
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
312 if (rates[i].rate == bitrate)
6750
c93570aeb3eb Remove unnecessary parentheses from return calls.
diego
parents: 6712
diff changeset
313 return rates[i].mode;
4744
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
314 /* no bitrate matching, return an error */
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
315 return -1;
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
316 }
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
317
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
318 static av_cold int amr_wb_encode_init(AVCodecContext *avctx)
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
319 {
4634
8b96c50c6723 Remove useless casts as recommended by Mns Rullgrd.
takis
parents: 4633
diff changeset
320 AMRWBContext *s = avctx->priv_data;
4633
cd67b7ab311c Cosmetics in amr.c.
takis
parents: 4632
diff changeset
321
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
322 s->frameCount = 0;
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2895
diff changeset
323
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
324 if (avctx->sample_rate != 16000) {
4309
1f95aa362562 always print message when error, AV_LOG_DEBUG -> AV_LOG_ERROR
bcoudurier
parents: 4221
diff changeset
325 av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
326 return -1;
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
327 }
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
328
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
329 if (avctx->channels != 1) {
4309
1f95aa362562 always print message when error, AV_LOG_DEBUG -> AV_LOG_ERROR
bcoudurier
parents: 4221
diff changeset
330 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
331 return -1;
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
332 }
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
333
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
334 if ((s->mode = getWBBitrateMode(avctx->bit_rate)) < 0) {
4793
e69d9ecaaf84 Print a list of valid AMR bitrates if a wrong one is used.
diego
parents: 4789
diff changeset
335 av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
4744
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
336 return -1;
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
337 }
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
338
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
339 avctx->frame_size = 320;
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
340 avctx->coded_frame = avcodec_alloc_frame();
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
341
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
342 s->state = E_IF_init();
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
343 s->allow_dtx = 0;
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
344
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
345 return 0;
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
346 }
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
347
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
348 static int amr_wb_encode_close(AVCodecContext *avctx)
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
349 {
4634
8b96c50c6723 Remove useless casts as recommended by Mns Rullgrd.
takis
parents: 4633
diff changeset
350 AMRWBContext *s = avctx->priv_data;
4633
cd67b7ab311c Cosmetics in amr.c.
takis
parents: 4632
diff changeset
351
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
352 E_IF_exit(s->state);
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
353 av_freep(&avctx->coded_frame);
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
354 s->frameCount++;
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
355 return 0;
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
356 }
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
357
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
358 static int amr_wb_encode_frame(AVCodecContext *avctx,
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
359 unsigned char *frame/*out*/,
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
360 int buf_size, void *data/*in*/)
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
361 {
4634
8b96c50c6723 Remove useless casts as recommended by Mns Rullgrd.
takis
parents: 4633
diff changeset
362 AMRWBContext *s = avctx->priv_data;
4220
4115a044e31d allow amr bitrate to be changed "on the fly" through avctx->bitrate.
gpoirier
parents: 4094
diff changeset
363 int size;
4632
260b5f667458 Move the initialization on context definition. In amr.c, only the
takis
parents: 4575
diff changeset
364
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
365 if ((s->mode = getWBBitrateMode(avctx->bit_rate)) < 0) {
4793
e69d9ecaaf84 Print a list of valid AMR bitrates if a wrong one is used.
diego
parents: 4789
diff changeset
366 av_log(avctx, AV_LOG_ERROR, wb_bitrate_unsupported);
4744
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
367 return -1;
00f7cd30636f Accept only existing bitrates.
diego
parents: 4634
diff changeset
368 }
4220
4115a044e31d allow amr bitrate to be changed "on the fly" through avctx->bitrate.
gpoirier
parents: 4094
diff changeset
369 size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
370 return size;
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
371 }
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
372
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
373 AVCodec libamr_wb_encoder = {
9651
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
374 "libamr_wb",
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
375 CODEC_TYPE_AUDIO,
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
376 CODEC_ID_AMR_WB,
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
377 sizeof(AMRWBContext),
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
378 amr_wb_encode_init,
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
379 amr_wb_encode_frame,
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
380 amr_wb_encode_close,
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
381 NULL,
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
382 .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
383 .long_name = NULL_IF_CONFIG_SMALL("libamr-wb Adaptive Multi-Rate (AMR) Wide-Band"),
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
384 };
e5ea408f6e2a cosmetics: Move functions around so that encoding and decoding functions are
diego
parents: 9650
diff changeset
385
9653
a572a4628a5a Surround AMR_WB encoding code by appropriate #ifdefs.
diego
parents: 9652
diff changeset
386 #endif
a572a4628a5a Surround AMR_WB encoding code by appropriate #ifdefs.
diego
parents: 9652
diff changeset
387
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
388 static av_cold int amr_wb_decode_init(AVCodecContext *avctx)
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
389 {
4634
8b96c50c6723 Remove useless casts as recommended by Mns Rullgrd.
takis
parents: 4633
diff changeset
390 AMRWBContext *s = avctx->priv_data;
4633
cd67b7ab311c Cosmetics in amr.c.
takis
parents: 4632
diff changeset
391
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
392 s->frameCount = 0;
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
393 s->state = D_IF_init();
4094
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
394
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
395 amr_decode_fix_avctx(avctx);
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
396
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
397 if (avctx->channels > 1) {
4094
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
398 av_log(avctx, AV_LOG_ERROR, "amr_wb: multichannel decoding not supported\n");
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
399 return -1;
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
400 }
f812a11be0b1 sample rate fix
diego
parents: 3947
diff changeset
401
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
402 return 0;
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
403 }
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
404
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
405 static int amr_wb_decode_frame(AVCodecContext *avctx, void *data,
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
406 int *data_size, AVPacket *avpkt)
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
407 {
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 9007
diff changeset
408 const uint8_t *buf = avpkt->data;
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
409 int buf_size = avpkt->size;
4634
8b96c50c6723 Remove useless casts as recommended by Mns Rullgrd.
takis
parents: 4633
diff changeset
410 AMRWBContext *s = avctx->priv_data;
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
411 const uint8_t *amrData = buf;
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
412 int mode;
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
413 int packet_size;
6427
4c29380d2511 Define block size for AMR-WB (use IF2 table).
benoit
parents: 6186
diff changeset
414 static const uint8_t block_size[16] = {18, 23, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
415
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
416 if (!buf_size)
2287
679642dc54cf segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents: 2028
diff changeset
417 /* nothing to do */
679642dc54cf segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents: 2028
diff changeset
418 return 0;
679642dc54cf segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents: 2028
diff changeset
419
679642dc54cf segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents: 2028
diff changeset
420 mode = (amrData[0] >> 3) & 0x000F;
679642dc54cf segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents: 2028
diff changeset
421 packet_size = block_size[mode];
679642dc54cf segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents: 2028
diff changeset
422
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
423 if (packet_size > buf_size) {
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
424 av_log(avctx, AV_LOG_ERROR, "amr frame too short (%u, should be %u)\n",
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
425 buf_size, packet_size + 1);
2287
679642dc54cf segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents: 2028
diff changeset
426 return -1;
679642dc54cf segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents: 2028
diff changeset
427 }
2967
ef2149182f1c COSMETICS: Remove all trailing whitespace.
diego
parents: 2895
diff changeset
428
2287
679642dc54cf segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents: 2028
diff changeset
429 s->frameCount++;
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
430 D_IF_decode(s->state, amrData, data, _good_frame);
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
431 *data_size = 320 * 2;
2287
679642dc54cf segfault in amr decoder fix patch by (Richard van der Hoff - richvdh0)
michael
parents: 2028
diff changeset
432 return packet_size;
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
433 }
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
434
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
435 static int amr_wb_decode_close(AVCodecContext *avctx)
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
436 {
4634
8b96c50c6723 Remove useless casts as recommended by Mns Rullgrd.
takis
parents: 4633
diff changeset
437 AMRWBContext *s = avctx->priv_data;
4633
cd67b7ab311c Cosmetics in amr.c.
takis
parents: 4632
diff changeset
438
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
439 D_IF_exit(s->state);
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
440 return 0;
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
441 }
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
442
9656
03361db4df10 cosmetics: K&R coding style, indent correctly, break overly long lines
diego
parents: 9655
diff changeset
443 AVCodec libamr_wb_decoder = {
4898
3df69e140c33 Give libamr decoders/encoders a lib prefix in the name.
diego
parents: 4858
diff changeset
444 "libamr_wb",
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
445 CODEC_TYPE_AUDIO,
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
446 CODEC_ID_AMR_WB,
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
447 sizeof(AMRWBContext),
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
448 amr_wb_decode_init,
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
449 NULL,
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
450 amr_wb_decode_close,
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
451 amr_wb_decode_frame,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6750
diff changeset
452 .long_name = NULL_IF_CONFIG_SMALL("libamr-wb Adaptive Multi-Rate (AMR) Wide-Band"),
1486
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
453 };
f22010affdce AMR-WB support by (Johannes Carlsson <joca at rixmail dot se>)
michaelni
parents: 1297
diff changeset
454
4898
3df69e140c33 Give libamr decoders/encoders a lib prefix in the name.
diego
parents: 4858
diff changeset
455 #endif //CONFIG_LIBAMR_WB