From patchwork Mon Jan 15 21:36:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Tomas_H=C3=A4rdin?= X-Patchwork-Id: 7293 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.78.2 with SMTP id r2csp3396457jaa; Mon, 15 Jan 2018 13:37:05 -0800 (PST) X-Google-Smtp-Source: ACJfBov4DEZmPks3EIxWiMnyOZ/Gso6lhkFibuBZULc1/jx/KAV1QnBA0pQbpEFvHVC/ZvpZ8Vwm X-Received: by 10.223.170.70 with SMTP id q6mr35116877wrd.265.1516052224939; Mon, 15 Jan 2018 13:37:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516052224; cv=none; d=google.com; s=arc-20160816; b=A7EZlqektcHm7wVmMI5ABFMv36EFUFZz26TCxjJYNAN7KrBY1miXqNl6KyPWTa5eeO gEjHHsxUHJI4eSeuf2QnGOJF760U6OFkzFZGSg6/K9GQ7XF9rYLfdalcTiKfTn+8foN9 CoXUsaiX8RUb5vYRbDfenlIthlcmAzEpWG8AbZres+4lh+nJ0522Hkmp+8dSdPGCGrDV esPKo5h9DT7EWRT6cf6pjQMeQd6pmWU5NQjUWt5SoQPHHZQu1LFy8el3y8feB7Zhre2U gQhAocxnrpLn/UtG3Hfz4WKAzYYuHxEdX+IhS7WXCAayLIhrAF1hDHaJMXslGB/Pp8Or 8mqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:date:references:in-reply-to:to:from:message-id :dkim-signature:dkim-signature:delivered-to :arc-authentication-results; bh=qrNjcSbdXEABX6aFWSXpQer/deK1c0iP5qH0OK89V1g=; b=J9/bgtXqHCE8ostqxRLjHrKNS5v3SPSHPffIFzXR9o/1KsWtaMVtjO8DqhCBHCGyIW 8zWDBQgcMvSTZzlSKxFGC1sI1hRAEIGQsjOnrAzIU2/1681G1OsWz+JcalVSKc6mfyoc 2MNf0SSRYWmuqwUT9lQgM1jcDJdyFNfIi1s01KfaH5L77Zj6LX619kCYvfbfW9EXGjOt p/nObBpPt1seE+0gLUo6ETqwWBUfM6WWbuU97xKzOCi1G5Nhy5+1sOxg+G7kY3mdNENi KUKAphX9oWwPF0xrf4csrI8SDv9DtuoOMx9m4gBZPcGp9llDODRB+bNTcIY0cLyw8UXC Jegg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@acc.umu.se header.s=mail1 header.b=sLpwHrCW; dkim=neutral (body hash did not verify) header.i=@acc.umu.se header.s=mail1 header.b=iLGkEGEL; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 189si649114wmr.127.2018.01.15.13.37.04; Mon, 15 Jan 2018 13:37:04 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@acc.umu.se header.s=mail1 header.b=sLpwHrCW; dkim=neutral (body hash did not verify) header.i=@acc.umu.se header.s=mail1 header.b=iLGkEGEL; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BE1DF68A05F; Mon, 15 Jan 2018 23:37:01 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.acc.umu.se (mail.acc.umu.se [130.239.18.156]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7614A689EB5 for ; Mon, 15 Jan 2018 23:36:55 +0200 (EET) Received: from localhost (localhost.localdomain [127.0.0.1]) by amavisd-new (Postfix) with ESMTP id 5634444B8E for ; Mon, 15 Jan 2018 22:36:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=acc.umu.se; s=mail1; t=1516052216; bh=qVG4E03+MuD8KR/GwFQM8i55RD94QvvrxF5TxKlWVQI=; h=Subject:From:To:In-Reply-To:References:Date:From; b=sLpwHrCWlYVnwmfbr9L9YEGBgL1IW3mxxQnNfRt94TqhTQEEbGxmwlzbxqt8h8c86 xqWBTE1PqXy4qvKWiy330Fw1fG9kYIAa7G2LEzrH7NcqPSL+20+WuFuqracf45tCdy JzNc4nFrAiL/FjQGp8zzbRm+4ClzkVUS1c9xI2hGMT9FmIME8lvVqaTvgEIYNWjZNn htJBSQr/AthDOmDOQs67uCE0Ev/RALxu6FIjQimkdlA1Q5dVOqhPkiDCfeGUP5lxJS eoGGvprpNfLyG1q5cXFOnY7rOK/jc4Yi7YCS1vuE/Zg7Sj2z2Okj+CKTKWQYUfy2ZV EzVdvA5tvaUZw== Received: from [IPv6:2a00:66c0:4:b477:ae81:12ff:fe25:528c] (unknown [IPv6:2a00:66c0:4:b477:ae81:12ff:fe25:528c]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: tjoppen) by mail.acc.umu.se (Postfix) with ESMTPSA id 2FB7A44B8B for ; Mon, 15 Jan 2018 22:36:53 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=acc.umu.se; s=mail1; t=1516052214; bh=qVG4E03+MuD8KR/GwFQM8i55RD94QvvrxF5TxKlWVQI=; h=Subject:From:To:In-Reply-To:References:Date:From; b=iLGkEGEL1kQINdK7aMEMG6igH/fPRl0rUPI1VZuncoNLz2qSbQKpncSyRhmNWGaLb Lk4FoJ/jWyjiySmGFy3Z3//84DrdHzqs5in8qu/FBioisTjqusVzGyjzQbtDKl9TsZ 7hIrl/pcxU6Ewsvx9OzclqSnx3NDj3NMdSzaYirLtumz0GTHNw42lTohUFGhv0NoiO 5ClGxMkYNk3tAZDTVpiTuo//pf8p9PxPeNpOGT4bYd1lhOcPpWyBOeEip0+dIItW6i KAEc3IgQZc1P7u1/00c8En92oeiSus/XHhMAMuvUiPxR0wfB64xKZEDZ0ahuU5SJ+L VcFX3x2lu944A== Message-ID: <1516052193.21348.6.camel@acc.umu.se> From: Tomas =?ISO-8859-1?Q?H=E4rdin?= To: ffmpeg-devel@ffmpeg.org In-Reply-To: References: <1514066939.15007.10.camel@acc.umu.se> <1514067335.4024.1.camel@acc.umu.se> Date: Mon, 15 Jan 2018 22:36:33 +0100 Mime-Version: 1.0 X-Mailer: Evolution 3.22.6-1+deb9u1 Subject: Re: [FFmpeg-devel] [PATCH 2/3] Add muxer/demuxer for raw codec2 and .c2 files X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" fre 2018-01-12 klockan 21:21 +0100 skrev Michael Niedermayer: > On Sat, Dec 23, 2017 at 11:15:35PM +0100, Tomas Härdin wrote: > > > > [...] > > +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(EINVAL); > > + } > > This should be AVERROR_INVALIDDATA i think OK, seem reasonable. It will typically be triggered by invalid mode > > + > > + avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); > > + > > + //replicating estimate_timings_from_bit_rate() in utils.c to avoid warnings > > + if (s->pb && st->codecpar->bit_rate > 0) { > > + int64_t filesize = avio_size(s->pb); > > + if (filesize > s->internal->data_offset) { > > + filesize -= s->internal->data_offset; > > + st->duration = av_rescale(8 * filesize, > > + st->time_base.den, > > + st->codecpar->bit_rate * (int64_t) st->time_base.num); > > + } > > + } > > Is this exact ? > or is a calculation from frame_size / block_align more accurate ? > the most accurate one should be used bit_rate is derived from block_align and frame_size in codec2utils.c so either would be fine. It strikes me that a better solution would be to suppress the "Estimating duration from bitrate, this may be inaccurate" warning for CBR formats. Removed this hunk for now. > > +static int codec2_read_header(AVFormatContext *s) > > +{ > > + [...] > > + avio_read(s->pb, st->codecpar->extradata, AVPRIV_CODEC2_EXTRADATA_SIZE); > > The return codes from avio_read and not checked Replaced with ffio_read_size() and checking for AVERROR mån 2018-01-15 klockan 00:32 +0100 skrev Carl Eugen Hoyos: > > 2017-12-23 23:15 GMT+01:00 Tomas Härdin : > > > +//check for 0xC0DEC2, return non-zero if it doesn't match > > +static int check_magic(uint8_t *ptr) { > > +    return memcmp(ptr, avpriv_codec2_magic, 3); > > AV_RB24(), or do I miss something? Good idea. Replaced some of the related read/write functions with avio_rb24/wb24 too > > > +static int codec2_probe(AVProbeData *p) > > +{ > > +    int score; > > + > > +    //must be at least 7 bytes and start wih 0xC0DEC2 > > +    if (p->buf_size < AVPRIV_CODEC2_HEADER_SIZE || > > This check is unneeded. Good, removed > > check_magic(p->buf)) { > > +        return 0; > +    } > + > +    //no .c2 files prior to 0.8 > > +    if (p->buf[3] == 0 && p->buf[4] < 8) { > > You chose to define the versions, please use the defines here. Fixed > +        return 0; > +    } > + > +    //give a poor score if major version doesn't match > +    //this allows such files to be detected at least, even if we > can't do much with them > +    if (p->buf[3] != EXPECTED_CODEC2_MAJOR_VERSION) { > +        return AVPROBE_SCORE_MAX/10; > +    } > > That may be ok. Actually, I feel like being more strict and coordinating future format changes with the FreeDV folks. Changed this to rejecting major != 0. > +    //if the minor version is known, no unknown mode is used and no > flags are set then -> max score, > +    //else penalize 20% for each byte outside of expectations > +    //this way if only the first four bytes are OK then we're > slightly less than AVPROBE_SCORE_MAX/2 > +    //cap score at max-1 unless file extension is .c2 > +    score = AVPROBE_SCORE_MAX; > > +    if (!av_match_ext(p->filename, "c2"))           score -= 1; > > We don't do this for any other demuxer, if this makes sense, it should > be done in general code, not for a specific demuxer imo. Removed it, see below. > +    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 > > Returning MAX for less than 64bit seems wrong to me. I think my reasoning was that if everything is within what the FreeDV tools are currently outputting then a higher score may be OK. On the other hand I can lobby the FreeDV list for more identifying bits together with a major version bump in the future, if needed. Changed to returning MAX/2 since we have C0 DE C2 00. Updated patch attached. /Tomas From a6573ff8e147aaf1acf45b0c3d000ae49da4798e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= Date: Tue, 8 Aug 2017 15:28:06 +0200 Subject: [PATCH 2/3] Add muxer/demuxer for raw codec2 and .c2 files --- Changelog | 1 + doc/general.texi | 4 + libavformat/Makefile | 4 + libavformat/allformats.c | 2 + libavformat/codec2.c | 285 +++++++++++++++++++++++++++++++++++++++++++++++ libavformat/rawenc.c | 13 +++ libavformat/utils.c | 1 + libavformat/version.h | 2 +- 8 files changed, 311 insertions(+), 1 deletion(-) create mode 100644 libavformat/codec2.c diff --git a/Changelog b/Changelog index b828190e52..0531460a6a 100644 --- a/Changelog +++ b/Changelog @@ -28,6 +28,7 @@ version : - nsp demuxer - support LibreSSL (via libtls) - 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 2b7efe2bef..09e2e815b7 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -308,6 +308,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 cb70eac920..ea804c92de 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -131,6 +131,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 6a9b9883c9..4c84d29650 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -97,6 +97,8 @@ static void register_all(void) REGISTER_DEMUXER (CDG, cdg); REGISTER_DEMUXER (CDXL, cdxl); REGISTER_DEMUXER (CINE, cine); + REGISTER_MUXDEMUX(CODEC2, codec2); + REGISTER_MUXDEMUX(CODEC2RAW, codec2raw); REGISTER_DEMUXER (CONCAT, concat); REGISTER_MUXER (CRC, crc); REGISTER_MUXDEMUX(DASH, dash); diff --git a/libavformat/codec2.c b/libavformat/codec2.c new file mode 100644 index 0000000000..772fc0d925 --- /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 +#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 -> half score + return AVPROBE_SCORE_MAX/2; +} + +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 aa3ef76fbf..2a80977e37 100644 --- a/libavformat/rawenc.c +++ b/libavformat/rawenc.c @@ -117,6 +117,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 84e49208b8..47c85b26c7 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -905,6 +905,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 5ced041f0a..49b9906a20 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 3 +#define LIBAVFORMAT_VERSION_MINOR 4 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ -- 2.11.0