2012-06-16 00:42:13 +03:00
/*
* iLBC decoder / encoder stub
* Copyright ( c ) 2012 Martin Storsjo
*
2012-06-19 21:52:00 +03:00
* This file is part of FFmpeg .
2012-06-16 00:42:13 +03:00
*
2012-06-19 21:52:00 +03:00
* FFmpeg is free software ; you can redistribute it and / or
2012-06-16 00:42:13 +03:00
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
2012-06-19 21:52:00 +03:00
* FFmpeg is distributed in the hope that it will be useful ,
2012-06-16 00:42:13 +03:00
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
2012-06-19 21:52:00 +03:00
* License along with FFmpeg ; if not , write to the Free Software
2012-06-16 00:42:13 +03:00
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
# include <ilbc.h>
2012-11-10 18:00:00 +03:00
# include "libavutil/channel_layout.h"
2012-08-06 16:49:32 +03:00
# include "libavutil/common.h"
2012-06-16 00:42:13 +03:00
# include "libavutil/opt.h"
2012-11-10 18:00:00 +03:00
# include "avcodec.h"
2022-03-16 19:18:28 +02:00
# include "codec_internal.h"
2022-08-24 21:28:16 +02:00
# include "decode.h"
2021-05-11 15:17:13 +02:00
# include "encode.h"
2012-06-16 00:42:13 +03:00
2021-03-11 02:43:44 +02:00
# ifndef LIBILBC_VERSION_MAJOR
# define LIBILBC_VERSION_MAJOR 2
# endif
2012-06-16 00:42:13 +03:00
static int get_mode ( AVCodecContext * avctx )
{
if ( avctx - > block_align = = 38 )
return 20 ;
else if ( avctx - > block_align = = 50 )
return 30 ;
else if ( avctx - > bit_rate > 0 )
return avctx - > bit_rate < = 14000 ? 30 : 20 ;
else
return - 1 ;
}
typedef struct ILBCDecContext {
const AVClass * class ;
2021-03-11 02:43:44 +02:00
# if LIBILBC_VERSION_MAJOR < 3
2012-06-16 00:42:13 +03:00
iLBC_Dec_Inst_t decoder ;
2021-03-11 02:43:44 +02:00
# else
IlbcDecoder decoder ;
# endif
2012-06-16 00:42:13 +03:00
int enhance ;
} ILBCDecContext ;
static const AVOption ilbc_dec_options [ ] = {
2012-08-31 13:22:31 +03:00
{ " enhance " , " Enhance the decoded audio (adds delay) " , offsetof ( ILBCDecContext , enhance ) , AV_OPT_TYPE_INT , { . i64 = 0 } , 0 , 1 , AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM } ,
2012-06-16 00:42:13 +03:00
{ NULL }
} ;
static const AVClass ilbc_dec_class = {
2012-06-19 23:43:28 +03:00
. class_name = " libilbc " ,
. item_name = av_default_item_name ,
. option = ilbc_dec_options ,
2012-07-17 09:53:36 +03:00
. version = LIBAVUTIL_VERSION_INT ,
2012-06-16 00:42:13 +03:00
} ;
static av_cold int ilbc_decode_init ( AVCodecContext * avctx )
{
ILBCDecContext * s = avctx - > priv_data ;
int mode ;
if ( ( mode = get_mode ( avctx ) ) < 0 ) {
av_log ( avctx , AV_LOG_ERROR , " iLBC frame mode not indicated \n " ) ;
return AVERROR ( EINVAL ) ;
}
WebRtcIlbcfix_InitDecode ( & s - > decoder , mode , s - > enhance ) ;
2013-05-07 08:20:32 +03:00
av_channel_layout_uninit ( & avctx - > ch_layout ) ;
avctx - > ch_layout = ( AVChannelLayout ) AV_CHANNEL_LAYOUT_MONO ;
2012-10-23 00:41:47 +03:00
avctx - > sample_rate = 8000 ;
avctx - > sample_fmt = AV_SAMPLE_FMT_S16 ;
2012-06-16 00:42:13 +03:00
return 0 ;
}
2022-03-30 21:33:24 +02:00
static int ilbc_decode_frame ( AVCodecContext * avctx , AVFrame * frame ,
2012-06-16 00:42:13 +03:00
int * got_frame_ptr , AVPacket * avpkt )
{
const uint8_t * buf = avpkt - > data ;
int buf_size = avpkt - > size ;
ILBCDecContext * s = avctx - > priv_data ;
int ret ;
if ( s - > decoder . no_of_bytes > buf_size ) {
2021-03-11 02:43:44 +02:00
# if LIBILBC_VERSION_MAJOR < 3
2012-06-16 00:42:13 +03:00
av_log ( avctx , AV_LOG_ERROR , " iLBC frame too short (%u, should be %u) \n " ,
2021-03-11 02:43:44 +02:00
# else
av_log ( avctx , AV_LOG_ERROR , " iLBC frame too short (%u, should be "
" % " SIZE_SPECIFIER " ) \n " ,
# endif
2012-06-16 00:42:13 +03:00
buf_size , s - > decoder . no_of_bytes ) ;
return AVERROR_INVALIDDATA ;
}
2012-12-24 02:05:53 +03:00
frame - > nb_samples = s - > decoder . blockl ;
lavc: factorize ff_{thread_,re,}get_buffer error messages.
Coccinelle profile used:
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_get_buffer(ctx, f, flags)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str;
@@
-if ((r = ff_reget_buffer(ctx, f)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_reget_buffer(ctx, f)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0)
+ return r;
...along with some manual patches for the remaining ones.
2013-03-12 10:41:53 +03:00
if ( ( ret = ff_get_buffer ( avctx , frame , 0 ) ) < 0 )
2012-06-16 00:42:13 +03:00
return ret ;
2014-09-19 05:49:36 +03:00
WebRtcIlbcfix_DecodeImpl ( ( int16_t * ) frame - > data [ 0 ] , ( const uint16_t * ) buf , & s - > decoder , 1 ) ;
2012-06-16 00:42:13 +03:00
2012-12-24 02:05:53 +03:00
* got_frame_ptr = 1 ;
2012-06-16 00:42:13 +03:00
return s - > decoder . no_of_bytes ;
}
2022-03-16 22:09:54 +02:00
const FFCodec ff_libilbc_decoder = {
. p . name = " libilbc " ,
2022-08-29 13:38:02 +02:00
CODEC_LONG_NAME ( " iLBC (Internet Low Bitrate Codec) " ) ,
2022-03-16 22:09:54 +02:00
. p . type = AVMEDIA_TYPE_AUDIO ,
. p . id = AV_CODEC_ID_ILBC ,
2022-07-09 22:25:41 +02:00
. caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE ,
2012-06-16 00:42:13 +03:00
. priv_data_size = sizeof ( ILBCDecContext ) ,
. init = ilbc_decode_init ,
2022-03-30 23:28:24 +02:00
FF_CODEC_DECODE_CB ( ilbc_decode_frame ) ,
2022-03-16 22:09:54 +02:00
. p . capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF ,
. p . priv_class = & ilbc_dec_class ,
2012-06-16 00:42:13 +03:00
} ;
typedef struct ILBCEncContext {
const AVClass * class ;
2021-03-11 02:43:44 +02:00
# if LIBILBC_VERSION_MAJOR < 3
2012-06-16 00:42:13 +03:00
iLBC_Enc_Inst_t encoder ;
2021-03-11 02:43:44 +02:00
# else
IlbcEncoder encoder ;
# endif
2012-06-16 00:42:13 +03:00
int mode ;
} ILBCEncContext ;
static const AVOption ilbc_enc_options [ ] = {
2012-08-31 13:22:31 +03:00
{ " mode " , " iLBC mode (20 or 30 ms frames) " , offsetof ( ILBCEncContext , mode ) , AV_OPT_TYPE_INT , { . i64 = 20 } , 20 , 30 , AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM } ,
2012-06-16 00:42:13 +03:00
{ NULL }
} ;
static const AVClass ilbc_enc_class = {
2012-06-19 23:43:28 +03:00
. class_name = " libilbc " ,
. item_name = av_default_item_name ,
. option = ilbc_enc_options ,
. version = LIBAVUTIL_VERSION_INT ,
2012-06-16 00:42:13 +03:00
} ;
static av_cold int ilbc_encode_init ( AVCodecContext * avctx )
{
ILBCEncContext * s = avctx - > priv_data ;
int mode ;
if ( avctx - > sample_rate ! = 8000 ) {
av_log ( avctx , AV_LOG_ERROR , " Only 8000Hz sample rate supported \n " ) ;
return AVERROR ( EINVAL ) ;
}
2013-05-07 08:20:32 +03:00
if ( avctx - > ch_layout . nb_channels ! = 1 ) {
2012-06-16 00:42:13 +03:00
av_log ( avctx , AV_LOG_ERROR , " Only mono supported \n " ) ;
return AVERROR ( EINVAL ) ;
}
if ( ( mode = get_mode ( avctx ) ) > 0 )
s - > mode = mode ;
else
s - > mode = s - > mode ! = 30 ? 20 : 30 ;
WebRtcIlbcfix_InitEncode ( & s - > encoder , s - > mode ) ;
avctx - > block_align = s - > encoder . no_of_bytes ;
avctx - > frame_size = s - > encoder . blockl ;
return 0 ;
}
static int ilbc_encode_frame ( AVCodecContext * avctx , AVPacket * avpkt ,
const AVFrame * frame , int * got_packet_ptr )
{
ILBCEncContext * s = avctx - > priv_data ;
int ret ;
2021-05-11 15:17:13 +02:00
if ( ( ret = ff_alloc_packet ( avctx , avpkt , 50 ) ) < 0 )
2012-06-16 00:42:13 +03:00
return ret ;
2014-09-19 05:49:36 +03:00
WebRtcIlbcfix_EncodeImpl ( ( uint16_t * ) avpkt - > data , ( const int16_t * ) frame - > data [ 0 ] , & s - > encoder ) ;
2012-06-16 00:42:13 +03:00
avpkt - > size = s - > encoder . no_of_bytes ;
* got_packet_ptr = 1 ;
return 0 ;
}
2022-03-16 22:26:11 +02:00
static const FFCodecDefault ilbc_encode_defaults [ ] = {
2012-06-16 00:42:13 +03:00
{ " b " , " 0 " } ,
{ NULL }
} ;
2022-03-16 22:09:54 +02:00
const FFCodec ff_libilbc_encoder = {
. p . name = " libilbc " ,
2022-08-29 13:38:02 +02:00
CODEC_LONG_NAME ( " iLBC (Internet Low Bitrate Codec) " ) ,
2022-03-16 22:09:54 +02:00
. p . type = AVMEDIA_TYPE_AUDIO ,
. p . id = AV_CODEC_ID_ILBC ,
2022-11-27 14:37:10 +02:00
. p . capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE ,
2022-07-09 22:25:41 +02:00
. caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE ,
2012-06-16 00:42:13 +03:00
. priv_data_size = sizeof ( ILBCEncContext ) ,
. init = ilbc_encode_init ,
2022-03-30 23:28:24 +02:00
FF_CODEC_ENCODE_CB ( ilbc_encode_frame ) ,
2022-03-16 22:09:54 +02:00
. p . sample_fmts = ( const enum AVSampleFormat [ ] ) { AV_SAMPLE_FMT_S16 ,
2012-06-16 00:42:13 +03:00
AV_SAMPLE_FMT_NONE } ,
. defaults = ilbc_encode_defaults ,
2022-03-16 22:09:54 +02:00
. p . priv_class = & ilbc_enc_class ,
. p . wrapper_name = " libbilbc " ,
2012-06-16 00:42:13 +03:00
} ;