yading@10
|
1 /*
|
yading@10
|
2 * AMR Audio encoder stub
|
yading@10
|
3 * Copyright (c) 2003 the ffmpeg project
|
yading@10
|
4 *
|
yading@10
|
5 * This file is part of FFmpeg.
|
yading@10
|
6 *
|
yading@10
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
9 * License as published by the Free Software Foundation; either
|
yading@10
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
11 *
|
yading@10
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
15 * Lesser General Public License for more details.
|
yading@10
|
16 *
|
yading@10
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
20 */
|
yading@10
|
21
|
yading@10
|
22 #include <vo-amrwbenc/enc_if.h>
|
yading@10
|
23 #include <stdio.h>
|
yading@10
|
24 #include <stdlib.h>
|
yading@10
|
25
|
yading@10
|
26 #include "libavutil/avstring.h"
|
yading@10
|
27 #include "libavutil/internal.h"
|
yading@10
|
28 #include "libavutil/mem.h"
|
yading@10
|
29 #include "libavutil/opt.h"
|
yading@10
|
30 #include "avcodec.h"
|
yading@10
|
31 #include "internal.h"
|
yading@10
|
32
|
yading@10
|
33 #define MAX_PACKET_SIZE (1 + (477 + 7) / 8)
|
yading@10
|
34
|
yading@10
|
35 typedef struct AMRWBContext {
|
yading@10
|
36 AVClass *av_class;
|
yading@10
|
37 void *state;
|
yading@10
|
38 int mode;
|
yading@10
|
39 int last_bitrate;
|
yading@10
|
40 int allow_dtx;
|
yading@10
|
41 } AMRWBContext;
|
yading@10
|
42
|
yading@10
|
43 static const AVOption options[] = {
|
yading@10
|
44 { "dtx", "Allow DTX (generate comfort noise)", offsetof(AMRWBContext, allow_dtx), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
|
yading@10
|
45 { NULL }
|
yading@10
|
46 };
|
yading@10
|
47
|
yading@10
|
48 static const AVClass class = {
|
yading@10
|
49 "libvo_amrwbenc", av_default_item_name, options, LIBAVUTIL_VERSION_INT
|
yading@10
|
50 };
|
yading@10
|
51
|
yading@10
|
52 static int get_wb_bitrate_mode(int bitrate, void *log_ctx)
|
yading@10
|
53 {
|
yading@10
|
54 /* make the correspondance between bitrate and mode */
|
yading@10
|
55 static const int rates[] = { 6600, 8850, 12650, 14250, 15850, 18250,
|
yading@10
|
56 19850, 23050, 23850 };
|
yading@10
|
57 int i, best = -1, min_diff = 0;
|
yading@10
|
58 char log_buf[200];
|
yading@10
|
59
|
yading@10
|
60 for (i = 0; i < 9; i++) {
|
yading@10
|
61 if (rates[i] == bitrate)
|
yading@10
|
62 return i;
|
yading@10
|
63 if (best < 0 || abs(rates[i] - bitrate) < min_diff) {
|
yading@10
|
64 best = i;
|
yading@10
|
65 min_diff = abs(rates[i] - bitrate);
|
yading@10
|
66 }
|
yading@10
|
67 }
|
yading@10
|
68 /* no bitrate matching exactly, log a warning */
|
yading@10
|
69 snprintf(log_buf, sizeof(log_buf), "bitrate not supported: use one of ");
|
yading@10
|
70 for (i = 0; i < 9; i++)
|
yading@10
|
71 av_strlcatf(log_buf, sizeof(log_buf), "%.2fk, ", rates[i] / 1000.f);
|
yading@10
|
72 av_strlcatf(log_buf, sizeof(log_buf), "using %.2fk", rates[best] / 1000.f);
|
yading@10
|
73 av_log(log_ctx, AV_LOG_WARNING, "%s\n", log_buf);
|
yading@10
|
74
|
yading@10
|
75 return best;
|
yading@10
|
76 }
|
yading@10
|
77
|
yading@10
|
78 static av_cold int amr_wb_encode_init(AVCodecContext *avctx)
|
yading@10
|
79 {
|
yading@10
|
80 AMRWBContext *s = avctx->priv_data;
|
yading@10
|
81
|
yading@10
|
82 if (avctx->sample_rate != 16000 && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
|
yading@10
|
83 av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
|
yading@10
|
84 return AVERROR(ENOSYS);
|
yading@10
|
85 }
|
yading@10
|
86
|
yading@10
|
87 if (avctx->channels != 1) {
|
yading@10
|
88 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
|
yading@10
|
89 return AVERROR(ENOSYS);
|
yading@10
|
90 }
|
yading@10
|
91
|
yading@10
|
92 s->mode = get_wb_bitrate_mode(avctx->bit_rate, avctx);
|
yading@10
|
93 s->last_bitrate = avctx->bit_rate;
|
yading@10
|
94
|
yading@10
|
95 avctx->frame_size = 320;
|
yading@10
|
96 avctx->delay = 80;
|
yading@10
|
97
|
yading@10
|
98 s->state = E_IF_init();
|
yading@10
|
99
|
yading@10
|
100 return 0;
|
yading@10
|
101 }
|
yading@10
|
102
|
yading@10
|
103 static int amr_wb_encode_close(AVCodecContext *avctx)
|
yading@10
|
104 {
|
yading@10
|
105 AMRWBContext *s = avctx->priv_data;
|
yading@10
|
106
|
yading@10
|
107 E_IF_exit(s->state);
|
yading@10
|
108 return 0;
|
yading@10
|
109 }
|
yading@10
|
110
|
yading@10
|
111 static int amr_wb_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
|
yading@10
|
112 const AVFrame *frame, int *got_packet_ptr)
|
yading@10
|
113 {
|
yading@10
|
114 AMRWBContext *s = avctx->priv_data;
|
yading@10
|
115 const int16_t *samples = (const int16_t *)frame->data[0];
|
yading@10
|
116 int size, ret;
|
yading@10
|
117
|
yading@10
|
118 if ((ret = ff_alloc_packet2(avctx, avpkt, MAX_PACKET_SIZE)) < 0)
|
yading@10
|
119 return ret;
|
yading@10
|
120
|
yading@10
|
121 if (s->last_bitrate != avctx->bit_rate) {
|
yading@10
|
122 s->mode = get_wb_bitrate_mode(avctx->bit_rate, avctx);
|
yading@10
|
123 s->last_bitrate = avctx->bit_rate;
|
yading@10
|
124 }
|
yading@10
|
125 size = E_IF_encode(s->state, s->mode, samples, avpkt->data, s->allow_dtx);
|
yading@10
|
126 if (size <= 0 || size > MAX_PACKET_SIZE) {
|
yading@10
|
127 av_log(avctx, AV_LOG_ERROR, "Error encoding frame\n");
|
yading@10
|
128 return AVERROR(EINVAL);
|
yading@10
|
129 }
|
yading@10
|
130
|
yading@10
|
131 if (frame->pts != AV_NOPTS_VALUE)
|
yading@10
|
132 avpkt->pts = frame->pts - ff_samples_to_time_base(avctx, avctx->delay);
|
yading@10
|
133
|
yading@10
|
134 avpkt->size = size;
|
yading@10
|
135 *got_packet_ptr = 1;
|
yading@10
|
136 return 0;
|
yading@10
|
137 }
|
yading@10
|
138
|
yading@10
|
139 AVCodec ff_libvo_amrwbenc_encoder = {
|
yading@10
|
140 .name = "libvo_amrwbenc",
|
yading@10
|
141 .type = AVMEDIA_TYPE_AUDIO,
|
yading@10
|
142 .id = AV_CODEC_ID_AMR_WB,
|
yading@10
|
143 .priv_data_size = sizeof(AMRWBContext),
|
yading@10
|
144 .init = amr_wb_encode_init,
|
yading@10
|
145 .encode2 = amr_wb_encode_frame,
|
yading@10
|
146 .close = amr_wb_encode_close,
|
yading@10
|
147 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
|
yading@10
|
148 AV_SAMPLE_FMT_NONE },
|
yading@10
|
149 .long_name = NULL_IF_CONFIG_SMALL("Android VisualOn AMR-WB "
|
yading@10
|
150 "(Adaptive Multi-Rate Wide-Band)"),
|
yading@10
|
151 .priv_class = &class,
|
yading@10
|
152 };
|