tis 2018-02-13 klockan 11:48 +0100 skrev Tomas Härdin: > fre 2018-02-09 klockan 11:29 +0100 skrev Carl Eugen Hoyos: > > 2018-01-15 22:36 GMT+01:00 Tomas Härdin <tjop...@acc.umu.se>: > > > > > > + if (p->buf[4] > EXPECTED_CODEC2_MINOR_VERSION) score -= > > > > AVPROBE_SCORE_MAX/5; > > > > + if (p->buf[5] > AVPRIV_CODEC2_MODE_MAX) score -= > > > > AVPROBE_SCORE_MAX/5; > > > > + if (p->buf[6] != 0) score -= > > > > AVPROBE_SCORE_MAX/5; > > > > + return score; > > > > > > > > Imo, this is too complicated: > > > > If the first 32bit are correct, return MAX/2, for 24bit, return > > > > less > > > > This should have been AVPROBE_SCORE_EXTENSION + 1, > > sorry about my mistake. > > Done. > > > Please threaten to push this and push after a few days. > > Alright, rebased. I'll push on Sunday if there's no objections > > /Tomas
Aaaand a set that actually passes FATE this time :) /Tomas
From 857d988892a028b7a273f70fc51770d4d0fbf08d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tjop...@acc.umu.se> Date: Tue, 8 Aug 2017 15:27:27 +0200 Subject: [PATCH 1/4] Add libcodec2 en/decoder --- Changelog | 1 + configure | 5 ++ doc/general.texi | 20 +++++ libavcodec/Makefile | 2 + libavcodec/allcodecs.c | 2 + libavcodec/avcodec.h | 1 + libavcodec/codec2utils.c | 80 ++++++++++++++++++ libavcodec/codec2utils.h | 82 ++++++++++++++++++ libavcodec/codec_desc.c | 7 ++ libavcodec/libcodec2.c | 213 +++++++++++++++++++++++++++++++++++++++++++++++ libavcodec/version.h | 4 +- 11 files changed, 415 insertions(+), 2 deletions(-) create mode 100644 libavcodec/codec2utils.c create mode 100644 libavcodec/codec2utils.h create mode 100644 libavcodec/libcodec2.c diff --git a/Changelog b/Changelog index 2acdbbea30..4ca137c0ce 100644 --- a/Changelog +++ b/Changelog @@ -39,6 +39,7 @@ version <next>: - Removed the ffmenc and ffmdec muxer and demuxer - VideoToolbox HEVC encoder and hwaccel - VAAPI-accelerated ProcAmp (color balance), denoise and sharpness filters +- codec2 en/decoding via libcodec2 version 3.4: diff --git a/configure b/configure index 99c53d482a..cad9d35457 100755 --- a/configure +++ b/configure @@ -224,6 +224,7 @@ External library support: --enable-libcaca enable textual display using libcaca [no] --enable-libcelt enable CELT decoding via libcelt [no] --enable-libcdio enable audio CD grabbing with libcdio [no] + --enable-libcodec2 enable codec2 en/decoding using libcodec2 [no] --enable-libdc1394 enable IIDC-1394 grabbing using libdc1394 and libraw1394 [no] --enable-libfdk-aac enable AAC de/encoding via libfdk-aac [no] @@ -1603,6 +1604,7 @@ EXTERNAL_LIBRARY_LIST=" libbs2b libcaca libcelt + libcodec2 libdc1394 libdrm libflite @@ -2940,6 +2942,8 @@ h264_videotoolbox_encoder_select="videotoolbox_encoder" hevc_videotoolbox_encoder_deps="pthreads" hevc_videotoolbox_encoder_select="videotoolbox_encoder" libcelt_decoder_deps="libcelt" +libcodec2_decoder_deps="libcodec2" +libcodec2_encoder_deps="libcodec2" libfdk_aac_decoder_deps="libfdk_aac" libfdk_aac_encoder_deps="libfdk_aac" libfdk_aac_encoder_select="audio_frame_queue" @@ -5865,6 +5869,7 @@ enabled libcelt && require libcelt celt/celt.h celt_decode -lcelt0 && { check_lib libcelt celt/celt.h celt_decoder_create_custom -lcelt0 || die "ERROR: libcelt must be installed and version must be >= 0.11.0."; } enabled libcaca && require_pkg_config libcaca caca caca.h caca_create_canvas +enabled libcodec2 && require libcodec2 codec2/codec2.h codec2_create -lcodec2 enabled libdc1394 && require_pkg_config libdc1394 libdc1394-2 dc1394/dc1394.h dc1394_new enabled libdrm && require_pkg_config libdrm libdrm xf86drm.h drmGetVersion enabled libfdk_aac && { check_pkg_config libfdk_aac fdk-aac "fdk-aac/aacenc_lib.h" aacEncOpen || diff --git a/doc/general.texi b/doc/general.texi index 9ddcccf041..63722f272d 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -85,6 +85,24 @@ Go to @url{http://www.twolame.org/} and follow the instructions for installing the library. Then pass @code{--enable-libtwolame} to configure to enable it. +@section libcodec2 / codec2 general + +FFmpeg can make use of libcodec2 for codec2 encoding and decoding. +There is currently no native decoder, so libcodec2 must be used for decoding. + +Go to @url{http://freedv.org/}, download "Codec 2 source archive". +Build and install using CMake. Debian users can install the libcodec2-dev package instead. +Once libcodec2 is installed you can pass @code{--enable-libcodec2} to configure to enable it. + +The easiest way to use codec2 is with .c2 files, since they contain the mode information required for decoding. +To encode such a file, use a .c2 file extension and give the libcodec2 encoder the -mode option: +@code{ffmpeg -i input.wav -mode 700C output.c2}. +Playback is as simple as @code{ffplay output.c2}. +For a list of supported modes, run @code{ffmpeg -h encoder=libcodec2}. +Raw codec2 files are also supported. +To make sense of them the mode in use needs to be specified as a format option: +@code{ffmpeg -f codec2raw -mode 1300 -i input.raw output.wav}. + @section libvpx FFmpeg can make use of the libvpx library for VP8/VP9 encoding. @@ -1019,6 +1037,8 @@ following image formats are supported: @tab Used in Bink and Smacker files in many games. @item CELT @tab @tab E @tab decoding supported through external library libcelt +@item codec2 @tab E @tab E + @tab en/decoding supported through external library libcodec2 @item Delphine Software International CIN audio @tab @tab X @tab Codec used in Delphine Software International games. @item Digital Speech Standard - Standard Play mode (DSS SP) @tab @tab X diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 3d4b738e0b..7639abbba0 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -929,6 +929,8 @@ OBJS-$(CONFIG_ILBC_AT_ENCODER) += audiotoolboxenc.o OBJS-$(CONFIG_PCM_ALAW_AT_ENCODER) += audiotoolboxenc.o OBJS-$(CONFIG_PCM_MULAW_AT_ENCODER) += audiotoolboxenc.o OBJS-$(CONFIG_LIBCELT_DECODER) += libcelt_dec.o +OBJS-$(CONFIG_LIBCODEC2_DECODER) += libcodec2.o codec2utils.o +OBJS-$(CONFIG_LIBCODEC2_ENCODER) += libcodec2.o codec2utils.o OBJS-$(CONFIG_LIBFDK_AAC_DECODER) += libfdk-aacdec.o OBJS-$(CONFIG_LIBFDK_AAC_ENCODER) += libfdk-aacenc.o OBJS-$(CONFIG_LIBGSM_DECODER) += libgsmdec.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 774b78ef09..70cbb7da0c 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -662,6 +662,8 @@ extern AVCodec ff_pcm_mulaw_at_decoder; extern AVCodec ff_qdmc_at_decoder; extern AVCodec ff_qdm2_at_decoder; extern AVCodec ff_libcelt_decoder; +extern AVCodec ff_libcodec2_encoder; +extern AVCodec ff_libcodec2_decoder; extern AVCodec ff_libfdk_aac_encoder; extern AVCodec ff_libfdk_aac_decoder; extern AVCodec ff_libgsm_encoder; diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index bc0eacd66b..ea774fb723 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -615,6 +615,7 @@ enum AVCodecID { AV_CODEC_ID_PAF_AUDIO, AV_CODEC_ID_ON2AVC, AV_CODEC_ID_DSS_SP, + AV_CODEC_ID_CODEC2, AV_CODEC_ID_FFWAVESYNTH = 0x15800, AV_CODEC_ID_SONIC, diff --git a/libavcodec/codec2utils.c b/libavcodec/codec2utils.c new file mode 100644 index 0000000000..931478f22a --- /dev/null +++ b/libavcodec/codec2utils.c @@ -0,0 +1,80 @@ +/* + * codec2 utility functions + * Copyright (c) 2017 Tomas Härdin + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * 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. + * + * FFmpeg is distributed in the hope that it will be useful, + * 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 + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <string.h> +#include "internal.h" +#include "libavcodec/codec2utils.h" + +int avpriv_codec2_mode_bit_rate(void *logctx, int mode) +{ + int frame_size = avpriv_codec2_mode_frame_size(logctx, mode); + int block_align = avpriv_codec2_mode_block_align(logctx, mode); + + if (frame_size <= 0 || block_align <= 0) { + return 0; + } + + return 8 * 8000 * block_align / frame_size; +} + +int avpriv_codec2_mode_frame_size(void *logctx, int mode) +{ + int frame_size_table[AVPRIV_CODEC2_MODE_MAX+1] = { + 160, // 3200 + 160, // 2400 + 320, // 1600 + 320, // 1400 + 320, // 1300 + 320, // 1200 + 320, // 700 + 320, // 700B + 320, // 700C + }; + + if (mode < 0 || mode > AVPRIV_CODEC2_MODE_MAX) { + av_log(logctx, AV_LOG_ERROR, "unknown codec2 mode %i, can't find frame_size\n", mode); + return 0; + } else { + return frame_size_table[mode]; + } +} + +int avpriv_codec2_mode_block_align(void *logctx, int mode) +{ + int block_align_table[AVPRIV_CODEC2_MODE_MAX+1] = { + 8, // 3200 + 6, // 2400 + 8, // 1600 + 7, // 1400 + 7, // 1300 + 6, // 1200 + 4, // 700 + 4, // 700B + 4, // 700C + }; + + if (mode < 0 || mode > AVPRIV_CODEC2_MODE_MAX) { + av_log(logctx, AV_LOG_ERROR, "unknown codec2 mode %i, can't find block_align\n", mode); + return 0; + } else { + return block_align_table[mode]; + } +} diff --git a/libavcodec/codec2utils.h b/libavcodec/codec2utils.h new file mode 100644 index 0000000000..6def4d4aa3 --- /dev/null +++ b/libavcodec/codec2utils.h @@ -0,0 +1,82 @@ +/* + * codec2 utility functions + * Copyright (c) 2017 Tomas Härdin + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * 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. + * + * FFmpeg is distributed in the hope that it will be useful, + * 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 + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_CODEC2UTILS_H +#define AVCODEC_CODEC2UTILS_H + +#include <stdint.h> + +//Highest mode we're willing to use. +//Don't want to let users accidentally produce files that can't be decoded in the future. +//CODEC2_MODE_WB (9) is experimental/unstable as of 2017-11-23. +#define AVPRIV_CODEC2_MODE_MAX 8 //CODEC2_MODE_700C + +//Used by both codec2raw demuxer and libcodec2 encoder. +//The integers match the values in codec2.h, so "3200" -> CODEC2_MODE_3000 = 0 and so on. +//It is possible that we're linked to a version of libcodec2 that lacks some of these modes. +//For example Debian stretch ships with libcodec2.so.0.4 which lacks CODEC2_MODE_700C. +#define AVPRIV_CODEC2_AVOPTIONS(desc, classname, min_val, default_val, option_flags) \ + { "mode", desc, offsetof(classname, mode), AV_OPT_TYPE_INT, {.i64 = default_val}, min_val, AVPRIV_CODEC2_MODE_MAX, .flags=option_flags, .unit="codec2_mode"},\ + { "3200", "3200", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, .flags=option_flags, .unit="codec2_mode"},\ + { "2400", "2400", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, .flags=option_flags, .unit="codec2_mode"},\ + { "1600", "1600", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, .flags=option_flags, .unit="codec2_mode"},\ + { "1400", "1400", 0, AV_OPT_TYPE_CONST, {.i64 = 3}, .flags=option_flags, .unit="codec2_mode"},\ + { "1300", "1300", 0, AV_OPT_TYPE_CONST, {.i64 = 4}, .flags=option_flags, .unit="codec2_mode"},\ + { "1200", "1200", 0, AV_OPT_TYPE_CONST, {.i64 = 5}, .flags=option_flags, .unit="codec2_mode"},\ + { "700", "700", 0, AV_OPT_TYPE_CONST, {.i64 = 6}, .flags=option_flags, .unit="codec2_mode"},\ + { "700B", "700B", 0, AV_OPT_TYPE_CONST, {.i64 = 7}, .flags=option_flags, .unit="codec2_mode"},\ + { "700C", "700C", 0, AV_OPT_TYPE_CONST, {.i64 = 8}, .flags=option_flags, .unit="codec2_mode"} + +//The three following functions are here to avoid needing libavformat/codec2.c to depend on libcodec2 + +//Computes bitrate from mode, with frames rounded up to the nearest octet. +//So 700 bit/s (28 bits/frame) becomes 800 bits/s (32 bits/frame). +//logctx is used for av_log() +//Returns <0 if mode is invalid +int avpriv_codec2_mode_bit_rate(void *logctx, int mode); + +//Mimics codec2_samples_per_frame() +int avpriv_codec2_mode_frame_size(void *logctx, int mode); + +//Mimics (codec2_bits_per_frame()+7)/8 +int avpriv_codec2_mode_block_align(void *logctx, int mode); + +#define AVPRIV_CODEC2_EXTRADATA_SIZE 4 + +//Used in codec2raw demuxer and libcodec2 encoder +static inline void avpriv_codec2_make_extradata(uint8_t *ptr, int mode) { + //version 0.8 as of 2017-12-23 (r3386) + ptr[0] = 0; //major + ptr[1] = 8; //minor + ptr[2] = mode; //mode + ptr[3] = 0; //flags +} + +//Returns version as a 16-bit value. 0.8 -> 0x0008 +static inline uint16_t avpriv_codec2_version_from_extradata(uint8_t *ptr) { + return (ptr[0] << 8) + ptr[1]; +} + +static inline uint8_t avpriv_codec2_mode_from_extradata(uint8_t *ptr) { + return ptr[2]; +} + +#endif /* AVCODEC_CODEC2UTILS_H */ diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 44a54a31c9..f1ba6b13ac 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -2734,6 +2734,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .props = AV_CODEC_PROP_LOSSY, }, { + .id = AV_CODEC_ID_CODEC2, + .type = AVMEDIA_TYPE_AUDIO, + .name = "codec2", + .long_name = NULL_IF_CONFIG_SMALL("codec2 (very low bitrate speech codec)"), + .props = AV_CODEC_PROP_LOSSY, + }, + { .id = AV_CODEC_ID_FFWAVESYNTH, .type = AVMEDIA_TYPE_AUDIO, .name = "wavesynth", diff --git a/libavcodec/libcodec2.c b/libavcodec/libcodec2.c new file mode 100644 index 0000000000..1d6bed0383 --- /dev/null +++ b/libavcodec/libcodec2.c @@ -0,0 +1,213 @@ +/* + * codec2 encoder/decoder using libcodec2 + * Copyright (c) 2017 Tomas Härdin + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * 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. + * + * FFmpeg is distributed in the hope that it will be useful, + * 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 + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <codec2/codec2.h> +#include "avcodec.h" +#include "libavutil/opt.h" +#include "internal.h" +#include "codec2utils.h" + +typedef struct { + const AVClass *class; + struct CODEC2 *codec; + int mode; +} LibCodec2Context; + +static const AVOption options[] = { + //not AV_OPT_FLAG_DECODING_PARAM since mode should come from the demuxer + //1300 (aka FreeDV 1600) is the most common mode on-the-air, default to it here as well + AVPRIV_CODEC2_AVOPTIONS("codec2 mode", LibCodec2Context, 0, 4 /*CODEC2_MODE_1300*/, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_ENCODING_PARAM), + { NULL }, +}; + +static const AVClass libcodec2_enc_class = { + .class_name = "libcodec2 encoder", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +static const AVClass libcodec2_dec_class = { + .class_name = "libcodec2 decoder", + .item_name = av_default_item_name, + .version = LIBAVUTIL_VERSION_INT, +}; + +static av_cold int libcodec2_init_common(AVCodecContext *avctx, int mode) +{ + LibCodec2Context *c2 = avctx->priv_data; + //Grab mode name from options, unless it's some weird number. + const char *modename = mode >= 0 && mode <= AVPRIV_CODEC2_MODE_MAX ? options[mode+1].name : "?"; + + c2->codec = codec2_create(mode); + if (!c2->codec) { + //Out of memory or unsupported mode. The latter seems most likely, + //but we can't tell for sure with the current API. + goto libcodec2_init_common_error; + } + + avctx->frame_size = codec2_samples_per_frame(c2->codec); + avctx->block_align = (codec2_bits_per_frame(c2->codec) + 7) / 8; + + if (avctx->frame_size <= 0 || avctx->block_align <= 0) { + //codec2_create() may succeed for some modes but still fail at codec2_samples_per_frame() + //example is -mode 700C on libcodec2 0.4 + codec2_destroy(c2->codec); + c2->codec = NULL; + goto libcodec2_init_common_error; + } + + codec2_set_natural_or_gray(c2->codec, 1); + + return 0; + +libcodec2_init_common_error: + av_log(avctx, AV_LOG_ERROR, + "Mode %i (%s) not supported with the linked version of libcodec2\n", + mode, modename); + return AVERROR(EINVAL); +} + +static av_cold int libcodec2_init_decoder(AVCodecContext *avctx) +{ + avctx->sample_rate = 8000; + avctx->channels = 1; + avctx->sample_fmt = AV_SAMPLE_FMT_S16; + avctx->channel_layout = AV_CH_LAYOUT_MONO; + + if (avctx->extradata_size != AVPRIV_CODEC2_EXTRADATA_SIZE) { + av_log(avctx, AV_LOG_ERROR, "must have exactly %i bytes of extradata (got %i)\n", + AVPRIV_CODEC2_EXTRADATA_SIZE, avctx->extradata_size); + return AVERROR_INVALIDDATA; + } + + return libcodec2_init_common(avctx, avpriv_codec2_mode_from_extradata(avctx->extradata)); +} + +static av_cold int libcodec2_init_encoder(AVCodecContext *avctx) +{ + LibCodec2Context *c2 = avctx->priv_data; + + //will need to be smarter once we get wideband support + if (avctx->sample_rate != 8000 || + avctx->channels != 1 || + avctx->sample_fmt != AV_SAMPLE_FMT_S16) { + av_log(avctx, AV_LOG_ERROR, "only 8 kHz 16-bit mono allowed\n"); + return AVERROR(EINVAL); + } + + avctx->extradata = av_mallocz(AVPRIV_CODEC2_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE); + if (!avctx->extradata) { + return AVERROR(ENOMEM); + } + + avctx->extradata_size = AVPRIV_CODEC2_EXTRADATA_SIZE; + avpriv_codec2_make_extradata(avctx->extradata, c2->mode); + + return libcodec2_init_common(avctx, c2->mode); +} + +static av_cold int libcodec2_close(AVCodecContext *avctx) +{ + LibCodec2Context *c2 = avctx->priv_data; + + codec2_destroy(c2->codec); + return 0; +} + +static int libcodec2_decode(AVCodecContext *avctx, void *data, + int *got_frame_ptr, AVPacket *pkt) +{ + LibCodec2Context *c2 = avctx->priv_data; + AVFrame *frame = data; + int ret, nframes, i; + uint8_t *input; + int16_t *output; + + nframes = pkt->size / avctx->block_align; + frame->nb_samples = avctx->frame_size * nframes; + + ret = ff_get_buffer(avctx, frame, 0); + if (ret < 0) { + return ret; + } + + input = pkt->data; + output = (int16_t *)frame->data[0]; + + for (i = 0; i < nframes; i++) { + codec2_decode(c2->codec, output, input); + input += avctx->block_align; + output += avctx->frame_size; + } + + *got_frame_ptr = nframes > 0; + return nframes * avctx->block_align; +} + +static int libcodec2_encode(AVCodecContext *avctx, AVPacket *avpkt, + const AVFrame *frame, int *got_packet_ptr) +{ + LibCodec2Context *c2 = avctx->priv_data; + int16_t *samples = (int16_t *)frame->data[0]; + + int ret = ff_alloc_packet2(avctx, avpkt, avctx->block_align, 0); + if (ret < 0) { + return ret; + } + + codec2_encode(c2->codec, avpkt->data, samples); + *got_packet_ptr = 1; + + return 0; +} + +AVCodec ff_libcodec2_decoder = { + .name = "libcodec2", + .long_name = NULL_IF_CONFIG_SMALL("codec2 decoder using libcodec2"), + .type = AVMEDIA_TYPE_AUDIO, + .id = AV_CODEC_ID_CODEC2, + .priv_data_size = sizeof(LibCodec2Context), + .init = libcodec2_init_decoder, + .close = libcodec2_close, + .decode = libcodec2_decode, + .capabilities = 0, + .supported_samplerates = (const int[]){ 8000, 0 }, + .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE }, + .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO, 0 }, + .priv_class = &libcodec2_dec_class, +}; + +AVCodec ff_libcodec2_encoder = { + .name = "libcodec2", + .long_name = NULL_IF_CONFIG_SMALL("codec2 encoder using libcodec2"), + .type = AVMEDIA_TYPE_AUDIO, + .id = AV_CODEC_ID_CODEC2, + .priv_data_size = sizeof(LibCodec2Context), + .init = libcodec2_init_encoder, + .close = libcodec2_close, + .encode2 = libcodec2_encode, + .capabilities = 0, + .supported_samplerates = (const int[]){ 8000, 0 }, + .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE }, + .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO, 0 }, + .priv_class = &libcodec2_enc_class, +}; diff --git a/libavcodec/version.h b/libavcodec/version.h index d13dd41fa3..d772f3e48e 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -28,8 +28,8 @@ #include "libavutil/version.h" #define LIBAVCODEC_VERSION_MAJOR 58 -#define LIBAVCODEC_VERSION_MINOR 11 -#define LIBAVCODEC_VERSION_MICRO 101 +#define LIBAVCODEC_VERSION_MINOR 12 +#define LIBAVCODEC_VERSION_MICRO 100 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \ -- 2.11.0
From dc38fd8e82149da2eafa2da6d978d32e629def1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tjop...@acc.umu.se> Date: Tue, 8 Aug 2017 15:28:06 +0200 Subject: [PATCH 2/4] Add muxer/demuxer for raw codec2 and .c2 files --- Changelog | 1 + doc/general.texi | 4 + libavformat/Makefile | 4 + libavformat/allformats.c | 4 + libavformat/codec2.c | 285 +++++++++++++++++++++++++++++++++++++++++++++++ libavformat/rawenc.c | 13 +++ libavformat/utils.c | 1 + libavformat/version.h | 2 +- 8 files changed, 313 insertions(+), 1 deletion(-) create mode 100644 libavformat/codec2.c diff --git a/Changelog b/Changelog index 4ca137c0ce..73d3212c65 100644 --- a/Changelog +++ b/Changelog @@ -40,6 +40,7 @@ version <next>: - VideoToolbox HEVC encoder and hwaccel - VAAPI-accelerated ProcAmp (color balance), denoise and sharpness filters - codec2 en/decoding via libcodec2 +- muxer/demuxer for raw codec2 files and .c2 files version 3.4: diff --git a/doc/general.texi b/doc/general.texi index 63722f272d..63aed34808 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -320,6 +320,10 @@ library: @item BRSTM @tab @tab X @tab Audio format used on the Nintendo Wii. @item BWF @tab X @tab X +@item codec2 (raw) @tab X @tab X + @tab Must be given -mode format option to decode correctly. +@item codec2 (.c2 files) @tab X @tab X + @tab Contains header with version and mode info, simplifying playback. @item CRI ADX @tab X @tab X @tab Audio-only format used in console video games. @item Discworld II BMV @tab @tab X diff --git a/libavformat/Makefile b/libavformat/Makefile index 7ac1ba95ad..d90766e201 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -133,6 +133,10 @@ OBJS-$(CONFIG_CAVSVIDEO_MUXER) += rawenc.o OBJS-$(CONFIG_CDG_DEMUXER) += cdg.o OBJS-$(CONFIG_CDXL_DEMUXER) += cdxl.o OBJS-$(CONFIG_CINE_DEMUXER) += cinedec.o +OBJS-$(CONFIG_CODEC2_DEMUXER) += ../libavcodec/codec2utils.o codec2.o rawdec.o pcm.o +OBJS-$(CONFIG_CODEC2_MUXER) += ../libavcodec/codec2utils.o codec2.o rawenc.o +OBJS-$(CONFIG_CODEC2RAW_DEMUXER) += ../libavcodec/codec2utils.o codec2.o rawdec.o pcm.o +OBJS-$(CONFIG_CODEC2RAW_MUXER) += rawenc.o OBJS-$(CONFIG_CONCAT_DEMUXER) += concatdec.o OBJS-$(CONFIG_CRC_MUXER) += crcenc.o OBJS-$(CONFIG_DATA_DEMUXER) += rawdec.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index e75cd917e4..662e505416 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -96,6 +96,10 @@ extern AVOutputFormat ff_cavsvideo_muxer; extern AVInputFormat ff_cdg_demuxer; extern AVInputFormat ff_cdxl_demuxer; extern AVInputFormat ff_cine_demuxer; +extern AVInputFormat ff_codec2_demuxer; +extern AVOutputFormat ff_codec2_muxer; +extern AVInputFormat ff_codec2raw_demuxer; +extern AVOutputFormat ff_codec2raw_muxer; extern AVInputFormat ff_concat_demuxer; extern AVOutputFormat ff_crc_muxer; extern AVInputFormat ff_dash_demuxer; diff --git a/libavformat/codec2.c b/libavformat/codec2.c new file mode 100644 index 0000000000..28dbbd8176 --- /dev/null +++ b/libavformat/codec2.c @@ -0,0 +1,285 @@ +/* + * codec2 muxer and demuxers + * Copyright (c) 2017 Tomas Härdin + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * 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. + * + * FFmpeg is distributed in the hope that it will be useful, + * 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 + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <memory.h> +#include "libavcodec/codec2utils.h" +#include "libavutil/intreadwrite.h" +#include "avio_internal.h" +#include "avformat.h" +#include "internal.h" +#include "rawdec.h" +#include "rawenc.h" +#include "pcm.h" + +#define AVPRIV_CODEC2_HEADER_SIZE 7 +#define AVPRIV_CODEC2_MAGIC 0xC0DEC2 + +//the lowest version we should ever run across is 0.8 +//we may run across later versions as the format evolves +#define EXPECTED_CODEC2_MAJOR_VERSION 0 +#define EXPECTED_CODEC2_MINOR_VERSION 8 + +typedef struct { + const AVClass *class; + int mode; + int frames_per_packet; +} Codec2Context; + +static int codec2_probe(AVProbeData *p) +{ + //must start wih C0 DE C2 + if (AV_RB24(p->buf) != AVPRIV_CODEC2_MAGIC) { + return 0; + } + + //no .c2 files prior to 0.8 + //be strict about major version while we're at it + if (p->buf[3] != EXPECTED_CODEC2_MAJOR_VERSION || + p->buf[4] < EXPECTED_CODEC2_MINOR_VERSION) { + return 0; + } + + //32 bits of identification -> low score + return AVPROBE_SCORE_EXTENSION + 1; +} + +static int codec2_read_header_common(AVFormatContext *s, AVStream *st) +{ + int mode = avpriv_codec2_mode_from_extradata(st->codecpar->extradata); + + st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; + st->codecpar->codec_id = AV_CODEC_ID_CODEC2; + st->codecpar->sample_rate = 8000; + st->codecpar->channels = 1; + st->codecpar->format = AV_SAMPLE_FMT_S16; + st->codecpar->channel_layout = AV_CH_LAYOUT_MONO; + st->codecpar->bit_rate = avpriv_codec2_mode_bit_rate(s, mode); + st->codecpar->frame_size = avpriv_codec2_mode_frame_size(s, mode); + st->codecpar->block_align = avpriv_codec2_mode_block_align(s, mode); + + if (st->codecpar->bit_rate <= 0 || + st->codecpar->frame_size <= 0 || + st->codecpar->block_align <= 0) { + return AVERROR_INVALIDDATA; + } + + avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); + + return 0; +} + +static int codec2_read_header(AVFormatContext *s) +{ + AVStream *st = avformat_new_stream(s, NULL); + int ret, version; + + if (!st) { + return AVERROR(ENOMEM); + } + + if (avio_rb24(s->pb) != AVPRIV_CODEC2_MAGIC) { + av_log(s, AV_LOG_ERROR, "not a .c2 file\n"); + return AVERROR_INVALIDDATA; + } + + ret = ff_alloc_extradata(st->codecpar, AVPRIV_CODEC2_EXTRADATA_SIZE); + if (ret) { + return ret; + } + + ret = ffio_read_size(s->pb, st->codecpar->extradata, AVPRIV_CODEC2_EXTRADATA_SIZE); + if (ret < 0) { + return ret; + } + + version = avpriv_codec2_version_from_extradata(st->codecpar->extradata); + if ((version >> 8) != EXPECTED_CODEC2_MAJOR_VERSION) { + avpriv_report_missing_feature(s, "Major version %i", version >> 8); + return AVERROR_PATCHWELCOME; + } + + s->internal->data_offset = AVPRIV_CODEC2_HEADER_SIZE; + + return codec2_read_header_common(s, st); +} + +static int codec2_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + Codec2Context *c2 = s->priv_data; + AVStream *st = s->streams[0]; + int ret, size, n, block_align, frame_size; + + block_align = st->codecpar->block_align; + frame_size = st->codecpar->frame_size; + + if (block_align <= 0 || frame_size <= 0 || c2->frames_per_packet <= 0) { + return AVERROR(EINVAL); + } + + //try to read desired number of frames, compute n from to actual number of bytes read + size = c2->frames_per_packet * block_align; + ret = av_get_packet(s->pb, pkt, size); + if (ret < 0) { + return ret; + } + + //only set duration - compute_pkt_fields() and ff_pcm_read_seek() takes care of everything else + //tested by spamming the seek functionality in ffplay + n = ret / block_align; + pkt->duration = n * frame_size; + + return ret; +} + +static int codec2_write_header(AVFormatContext *s) +{ + AVStream *st; + + if (s->nb_streams != 1 || s->streams[0]->codecpar->codec_id != AV_CODEC_ID_CODEC2) { + av_log(s, AV_LOG_ERROR, ".c2 files must have exactly one codec2 stream\n"); + return AVERROR(EINVAL); + } + + st = s->streams[0]; + + if (st->codecpar->extradata_size != AVPRIV_CODEC2_EXTRADATA_SIZE) { + av_log(s, AV_LOG_ERROR, ".c2 files require exactly %i bytes of extradata (got %i)\n", + AVPRIV_CODEC2_EXTRADATA_SIZE, st->codecpar->extradata_size); + return AVERROR(EINVAL); + } + + avio_wb24(s->pb, AVPRIV_CODEC2_MAGIC); + avio_write(s->pb, st->codecpar->extradata, AVPRIV_CODEC2_EXTRADATA_SIZE); + + return 0; +} + +static int codec2raw_read_header(AVFormatContext *s) +{ + Codec2Context *c2 = s->priv_data; + AVStream *st; + int ret; + + if (c2->mode < 0) { + //FIXME: using a default value of -1 for mandatory options is an incredibly ugly hack + av_log(s, AV_LOG_ERROR, "-mode must be set in order to make sense of raw codec2 files\n"); + return AVERROR(EINVAL); + } + + st = avformat_new_stream(s, NULL); + if (!st) { + return AVERROR(ENOMEM); + } + + ret = ff_alloc_extradata(st->codecpar, AVPRIV_CODEC2_EXTRADATA_SIZE); + if (ret) { + return ret; + } + + s->internal->data_offset = 0; + avpriv_codec2_make_extradata(st->codecpar->extradata, c2->mode); + + return codec2_read_header_common(s, st); +} + +//transcoding report2074.c2 to wav went from 7.391s to 5.322s with -frames_per_packet 1000 compared to default, same sha1sum +#define FRAMES_PER_PACKET \ + { "frames_per_packet", "Number of frames to read at a time. Higher = faster decoding, lower granularity", \ + offsetof(Codec2Context, frames_per_packet), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM} + +static const AVOption codec2_options[] = { + FRAMES_PER_PACKET, + { NULL }, +}; + +static const AVOption codec2raw_options[] = { + AVPRIV_CODEC2_AVOPTIONS("codec2 mode [mandatory]", Codec2Context, -1, -1, AV_OPT_FLAG_DECODING_PARAM), + FRAMES_PER_PACKET, + { NULL }, +}; + +static const AVClass codec2_mux_class = { + .class_name = "codec2 muxer", + .item_name = av_default_item_name, + .version = LIBAVUTIL_VERSION_INT, + .category = AV_CLASS_CATEGORY_DEMUXER, +}; + +static const AVClass codec2_demux_class = { + .class_name = "codec2 demuxer", + .item_name = av_default_item_name, + .option = codec2_options, + .version = LIBAVUTIL_VERSION_INT, + .category = AV_CLASS_CATEGORY_DEMUXER, +}; + +static const AVClass codec2raw_demux_class = { + .class_name = "codec2raw demuxer", + .item_name = av_default_item_name, + .option = codec2raw_options, + .version = LIBAVUTIL_VERSION_INT, + .category = AV_CLASS_CATEGORY_DEMUXER, +}; + +#if CONFIG_CODEC2_DEMUXER +AVInputFormat ff_codec2_demuxer = { + .name = "codec2", + .long_name = NULL_IF_CONFIG_SMALL("codec2 .c2 demuxer"), + .priv_data_size = sizeof(Codec2Context), + .extensions = "c2", + .read_probe = codec2_probe, + .read_header = codec2_read_header, + .read_packet = codec2_read_packet, + .read_seek = ff_pcm_read_seek, + .flags = AVFMT_GENERIC_INDEX, + .raw_codec_id = AV_CODEC_ID_CODEC2, + .priv_class = &codec2_demux_class, +}; +#endif + +#if CONFIG_CODEC2_MUXER +AVOutputFormat ff_codec2_muxer = { + .name = "codec2", + .long_name = NULL_IF_CONFIG_SMALL("codec2 .c2 muxer"), + .priv_data_size = sizeof(Codec2Context), + .extensions = "c2", + .audio_codec = AV_CODEC_ID_CODEC2, + .video_codec = AV_CODEC_ID_NONE, + .write_header = codec2_write_header, + .write_packet = ff_raw_write_packet, + .flags = AVFMT_NOTIMESTAMPS, + .priv_class = &codec2_mux_class, +}; +#endif + +#if CONFIG_CODEC2RAW_DEMUXER +AVInputFormat ff_codec2raw_demuxer = { + .name = "codec2raw", + .long_name = NULL_IF_CONFIG_SMALL("raw codec2 demuxer"), + .priv_data_size = sizeof(Codec2Context), + .read_header = codec2raw_read_header, + .read_packet = codec2_read_packet, + .read_seek = ff_pcm_read_seek, + .flags = AVFMT_GENERIC_INDEX, + .raw_codec_id = AV_CODEC_ID_CODEC2, + .priv_class = &codec2raw_demux_class, +}; +#endif diff --git a/libavformat/rawenc.c b/libavformat/rawenc.c index dcf880d17e..fcfbfbce8e 100644 --- a/libavformat/rawenc.c +++ b/libavformat/rawenc.c @@ -130,6 +130,19 @@ AVOutputFormat ff_cavsvideo_muxer = { }; #endif +#if CONFIG_CODEC2RAW_MUXER +AVOutputFormat ff_codec2raw_muxer = { + .name = "codec2raw", + .long_name = NULL_IF_CONFIG_SMALL("raw codec2 muxer"), + .audio_codec = AV_CODEC_ID_CODEC2, + .video_codec = AV_CODEC_ID_NONE, + .write_header = force_one_stream, + .write_packet = ff_raw_write_packet, + .flags = AVFMT_NOTIMESTAMPS, +}; +#endif + + #if CONFIG_DATA_MUXER AVOutputFormat ff_data_muxer = { .name = "data", diff --git a/libavformat/utils.c b/libavformat/utils.c index 28ea071409..2c2ea876b6 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -933,6 +933,7 @@ static int determinable_frame_size(AVCodecContext *avctx) case AV_CODEC_ID_MP1: case AV_CODEC_ID_MP2: case AV_CODEC_ID_MP3: + case AV_CODEC_ID_CODEC2: return 1; } diff --git a/libavformat/version.h b/libavformat/version.h index 009f981095..e28a9e7dfe 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -32,7 +32,7 @@ // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium) // Also please add any ticket numbers that you believe might be affected here #define LIBAVFORMAT_VERSION_MAJOR 58 -#define LIBAVFORMAT_VERSION_MINOR 9 +#define LIBAVFORMAT_VERSION_MINOR 10 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ -- 2.11.0
From 8c15363f8374e000e88696b452d832d84f2f647d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tjop...@acc.umu.se> Date: Thu, 3 Aug 2017 17:33:04 +0200 Subject: [PATCH 3/4] Don't complain about codec2's 700 bit/s modes in ffmpeg.c --- fftools/ffmpeg.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index a37de2ff98..4bbbfeba31 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3502,7 +3502,8 @@ static int init_output_stream(OutputStream *ost, char *error, int error_len) av_buffersink_set_frame_size(ost->filter->filter, ost->enc_ctx->frame_size); assert_avoptions(ost->encoder_opts); - if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000) + if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 && + ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */) av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low." " It takes bits/s as argument, not kbits/s\n"); -- 2.11.0
From 3058b050d4a716ab77c5feef7820579ea9949dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tjop...@acc.umu.se> Date: Tue, 13 Feb 2018 11:46:44 +0100 Subject: [PATCH 4/4] Add myself as codec2 maintainer --- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index b691bd56ec..ded464172a 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -188,6 +188,7 @@ Codecs: jvdec.c Peter Ross lcl*.c Roberto Togni, Reimar Doeffinger libcelt_dec.c Nicolas George + libcodec2.c Tomas Härdin libdirac* David Conrad libgsm.c Michel Bardiaux libkvazaar.c Arttu Ylä-Outinen @@ -394,6 +395,7 @@ Muxers/Demuxers: brstm.c Paul B Mahol caf* Peter Ross cdxl.c Paul B Mahol + codec2.c Tomas Härdin crc.c Michael Niedermayer dashdec.c Steven Liu dashenc.c Karthick Jeyapal -- 2.11.0
signature.asc
Description: This is a digitally signed message part
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel