libvo-amrwbenc.c
Go to the documentation of this file.
1 /*
2  * AMR Audio encoder stub
3  * Copyright (c) 2003 the ffmpeg project
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <vo-amrwbenc/enc_if.h>
23 
24 #include "avcodec.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/opt.h"
27 
28 typedef struct AMRWBContext {
30  void *state;
31  int mode;
33  int allow_dtx;
34 } AMRWBContext;
35 
36 static const AVOption options[] = {
37  { "dtx", "Allow DTX (generate comfort noise)", offsetof(AMRWBContext, allow_dtx), AV_OPT_TYPE_INT, { 0 }, 0, 1, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
38  { NULL }
39 };
40 
41 static const AVClass class = {
43 };
44 
45 static int get_wb_bitrate_mode(int bitrate, void *log_ctx)
46 {
47  /* make the correspondance between bitrate and mode */
48  static const int rates[] = { 6600, 8850, 12650, 14250, 15850, 18250,
49  19850, 23050, 23850 };
50  int i, best = -1, min_diff = 0;
51  char log_buf[200];
52 
53  for (i = 0; i < 9; i++) {
54  if (rates[i] == bitrate)
55  return i;
56  if (best < 0 || abs(rates[i] - bitrate) < min_diff) {
57  best = i;
58  min_diff = abs(rates[i] - bitrate);
59  }
60  }
61  /* no bitrate matching exactly, log a warning */
62  snprintf(log_buf, sizeof(log_buf), "bitrate not supported: use one of ");
63  for (i = 0; i < 9; i++)
64  av_strlcatf(log_buf, sizeof(log_buf), "%.2fk, ", rates[i] / 1000.f);
65  av_strlcatf(log_buf, sizeof(log_buf), "using %.2fk", rates[best] / 1000.f);
66  av_log(log_ctx, AV_LOG_WARNING, "%s\n", log_buf);
67 
68  return best;
69 }
70 
72 {
73  AMRWBContext *s = avctx->priv_data;
74 
75  if (avctx->sample_rate != 16000) {
76  av_log(avctx, AV_LOG_ERROR, "Only 16000Hz sample rate supported\n");
77  return AVERROR(ENOSYS);
78  }
79 
80  if (avctx->channels != 1) {
81  av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
82  return AVERROR(ENOSYS);
83  }
84 
85  s->mode = get_wb_bitrate_mode(avctx->bit_rate, avctx);
86  s->last_bitrate = avctx->bit_rate;
87 
88  avctx->frame_size = 320;
90 
91  s->state = E_IF_init();
92 
93  return 0;
94 }
95 
97 {
98  AMRWBContext *s = avctx->priv_data;
99 
100  E_IF_exit(s->state);
101  av_freep(&avctx->coded_frame);
102  return 0;
103 }
104 
106  unsigned char *frame/*out*/,
107  int buf_size, void *data/*in*/)
108 {
109  AMRWBContext *s = avctx->priv_data;
110  int size;
111 
112  if (s->last_bitrate != avctx->bit_rate) {
113  s->mode = get_wb_bitrate_mode(avctx->bit_rate, avctx);
114  s->last_bitrate = avctx->bit_rate;
115  }
116  size = E_IF_encode(s->state, s->mode, data, frame, s->allow_dtx);
117  return size;
118 }
119 
121  .name = "libvo_amrwbenc",
122  .type = AVMEDIA_TYPE_AUDIO,
123  .id = CODEC_ID_AMR_WB,
124  .priv_data_size = sizeof(AMRWBContext),
126  .encode = amr_wb_encode_frame,
128  .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
129  .long_name = NULL_IF_CONFIG_SMALL("Android VisualOn Adaptive Multi-Rate "
130  "(AMR) Wide-Band"),
131  .priv_class = &class,
132 };
133 
AVClass * av_class
int size
AVOption.
Definition: opt.h:244
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:277
av_default_item_name
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2000
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:117
AVOptions.
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:274
signed 16 bits
Definition: samplefmt.h:30
AVCodec.
Definition: avcodec.h:3189
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:147
struct AMRWBContext AMRWBContext
#define av_cold
Definition: attributes.h:71
static const AVOption options[]
const char data[16]
Definition: mxf.c:60
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:618
static int get_wb_bitrate_mode(int bitrate, void *log_ctx)
int bit_rate
the average bitrate
Definition: avcodec.h:1340
#define f(n)
Definition: regs.h:33
LIBAVUTIL_VERSION_INT
Definition: eval.c:50
int frame_size
Samples per packet, initialized when calling 'init'.
Definition: avcodec.h:1470
NULL
Definition: eval.c:50
external API header
int sample_rate
samples per second
Definition: avcodec.h:1456
main external API structure.
Definition: avcodec.h:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
Describe the class of an AVClass context structure.
Definition: log.h:33
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:82
static int amr_wb_encode_frame(AVCodecContext *avctx, unsigned char *frame, int buf_size, void *data)
AVSampleFormat
all in native-endian format
Definition: samplefmt.h:27
static int amr_wb_encode_close(AVCodecContext *avctx)
static av_cold int amr_wb_encode_init(AVCodecContext *avctx)
void * priv_data
Definition: avcodec.h:1531
int channels
number of audio channels
Definition: avcodec.h:1457
AVCodec ff_libvo_amrwbenc_encoder