From patchwork Wed Dec 20 19:58:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aurelien Jacobs X-Patchwork-Id: 6872 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.79.195 with SMTP id r64csp3371039jad; Wed, 20 Dec 2017 11:58:59 -0800 (PST) X-Google-Smtp-Source: ACJfBouM/Lo8ZtH+2p1SPB08ZLMiUjkYLQNBzh4D5maDv/tlwJwYWSWB8sfvOj9FBiXWfTy8MG7J X-Received: by 10.28.175.66 with SMTP id y63mr8086926wme.125.1513799939838; Wed, 20 Dec 2017 11:58:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1513799939; cv=none; d=google.com; s=arc-20160816; b=bvE6L8n1DC6utEZ2OA0/TGnV8m30xRSdwX6l5lZx9AbZbOcAJq11FHXcxXSGgDqcBN Lzx4tffhNiJZFhl7B5w4zou0tM+7LdkAiCxAnLeFQHb44iAtpkUYzG60fbsZix3UngOa OiJJRswTCPglocqL/V+j0BmmQpGDbjZCBfiA/C7xtZ3hlJIKTqlOG++PiaqoyxByhi3g DlUSY/gqyiZN6hV/dggoWCcy2A4VQG3m3l660YekDxgSYNQ+pV4u3J6o29uA9/1VNCfk GiziAepnx9JeJ/G45D3kkurSsjSW6TbwkJmFAW54H6fuIH+JxqXwmAOPJavTzg4ZHDnh gG9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:delivered-to:arc-authentication-results; bh=xuK76VH1q/UNXTn1PS4nv+C7h113LH1zrX8T5+gIgHI=; b=w47J2s4tpZrcmxrCYDnJdCs+PkiT8L2J3FpkVD7J++FJFmtsRaXBU96J2Ho8NIfXen t4PiY4TjEmh+ww63VtWceI/klE012mR1e7bfTLCCl/+Qh45AloIhGFnV+zar09ktqyzz LEfSa499zU+oty6cBgBVqe59jKm0h/x3YeLgsaIfC70uNH1QzpsVazJ6cHkQ61iPdNX8 BOrLtqKmW1AZ0/8tHqnvJxiW9EfRpnofjPwEbcGgE7xg7JQ0qMeRhhTu3EzxNWQsRQpy zPCqw2iM2JNmFzIeocdv30ew8TO5mbORprulW2WFBnRXAHRJB3Y77LWcFuTHdF8Gcqeg h52Q== ARC-Authentication-Results: i=1; mx.google.com; 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 x20si3444767wmc.1.2017.12.20.11.58.59; Wed, 20 Dec 2017 11:58:59 -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; 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 D515868092E; Wed, 20 Dec 2017 21:58:25 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smtp6-g21.free.fr (smtp6-g21.free.fr [212.27.42.6]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2F023680671 for ; Wed, 20 Dec 2017 21:58:25 +0200 (EET) Received: from marge.gnuage.org (unknown [IPv6:2a01:e35:2e26:9790::10]) by smtp6-g21.free.fr (Postfix) with ESMTPS id 2EBCC780326 for ; Wed, 20 Dec 2017 20:58:36 +0100 (CET) Received: from [2a01:e35:2e26:9790:bc8a:b994:5b68:84cc] (helo=xps) by marge.gnuage.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1eRkVf-00073H-64; Wed, 20 Dec 2017 20:58:35 +0100 Received: from aurel by xps with local (Exim 4.90_RC4) (envelope-from ) id 1eRkVf-0004lg-2l; Wed, 20 Dec 2017 20:58:35 +0100 From: Aurelien Jacobs To: FFmpeg development discussions and patches Date: Wed, 20 Dec 2017 20:58:08 +0100 Message-Id: <20171220195814.18269-4-aurel@gnuage.org> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171220195814.18269-1-aurel@gnuage.org> References: <20171220195814.18269-1-aurel@gnuage.org> X-SA-Exim-Connect-IP: 2a01:e35:2e26:9790:bc8a:b994:5b68:84cc X-SA-Exim-Mail-From: aurel@gnuage.org X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on marge.gnuage.org X-Spam-Level: X-Spam-Status: No, score=-0.2 required=3.0 tests=BAYES_00, FSL_HELO_NON_FQDN_1, RDNS_NONE, URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.1 X-SA-Exim-Version: 4.2.1 (built Tue, 02 Aug 2016 21:08:31 +0000) X-SA-Exim-Scanned: Yes (on marge.gnuage.org) Subject: [FFmpeg-devel] [PATCH 04/10] sbc: add parser for SBC 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 Cc: Aurelien Jacobs MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/sbc_parser.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 libavcodec/sbc_parser.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index c46d102227..58c8c6e499 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1012,6 +1012,7 @@ OBJS-$(CONFIG_PNG_PARSER) += png_parser.o OBJS-$(CONFIG_PNM_PARSER) += pnm_parser.o pnm.o OBJS-$(CONFIG_RV30_PARSER) += rv34_parser.o OBJS-$(CONFIG_RV40_PARSER) += rv34_parser.o +OBJS-$(CONFIG_SBC_PARSER) += sbc_parser.o OBJS-$(CONFIG_SIPR_PARSER) += sipr_parser.o OBJS-$(CONFIG_TAK_PARSER) += tak_parser.o tak.o OBJS-$(CONFIG_VC1_PARSER) += vc1_parser.o vc1.o vc1data.o \ diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 9e2480d2d4..3e3b37f638 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -653,6 +653,7 @@ static void register_all(void) REGISTER_PARSER(PNM, pnm); REGISTER_PARSER(RV30, rv30); REGISTER_PARSER(RV40, rv40); + REGISTER_PARSER(SBC, sbc); REGISTER_PARSER(SIPR, sipr); REGISTER_PARSER(TAK, tak); REGISTER_PARSER(VC1, vc1); diff --git a/libavcodec/sbc_parser.c b/libavcodec/sbc_parser.c new file mode 100644 index 0000000000..032f72cf2b --- /dev/null +++ b/libavcodec/sbc_parser.c @@ -0,0 +1,124 @@ +/* + * SBC parser + * + * Copyright (C) 2017 Aurelien Jacobs + * + * 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 "sbc.h" +#include "parser.h" + +typedef struct SBCParseContext { + ParseContext pc; + uint8_t header[3]; + int header_size; + int buffered_size; +} SBCParseContext; + +static int sbc_parse_header(AVCodecParserContext *s, AVCodecContext *avctx, + const uint8_t *data, size_t len) +{ + static const int sample_rates[4] = { 16000, 32000, 44100, 48000 }; + int sr, blocks, mode, subbands, bitpool, channels, joint; + int length; + + if (len < 3) + return -1; + + if (data[0] == MSBC_SYNCWORD && data[1] == 0 && data[2] == 0) { + avctx->channels = 1; + avctx->sample_fmt = AV_SAMPLE_FMT_S16; + avctx->sample_rate = 16000; + avctx->frame_size = 120; + s->duration = avctx->frame_size; + return 57; + } + + if (data[0] != SBC_SYNCWORD) + return -2; + + sr = (data[1] >> 6) & 0x03; + blocks = (((data[1] >> 4) & 0x03) + 1) << 2; + mode = (data[1] >> 2) & 0x03; + subbands = (((data[1] >> 0) & 0x01) + 1) << 2; + bitpool = data[2]; + + channels = mode == SBC_MODE_MONO ? 1 : 2; + joint = mode == SBC_MODE_JOINT_STEREO; + + length = 4 + (subbands * channels) / 2; + if (channels == 1 || mode == SBC_MODE_DUAL_CHANNEL) + length += ((channels * blocks * bitpool) + 7) / 8; + else + length += (((joint ? subbands : 0) + blocks * bitpool) + 7) / 8; + + avctx->channels = channels; + avctx->sample_fmt = AV_SAMPLE_FMT_S16; + avctx->sample_rate = sample_rates[sr]; + avctx->frame_size = subbands * blocks; + s->duration = avctx->frame_size; + return length; +} + +static int sbc_parse(AVCodecParserContext *s, AVCodecContext *avctx, + const uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size) +{ + SBCParseContext *pc = s->priv_data; + int next; + + if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { + next = buf_size; + } else { + if (pc->header_size) { + memcpy(pc->header + pc->header_size, buf, + sizeof(pc->header) - pc->header_size); + next = sbc_parse_header(s, avctx, pc->header, sizeof(pc->header)) + - pc->buffered_size; + pc->header_size = 0; + } else { + next = sbc_parse_header(s, avctx, buf, buf_size); + if (next >= buf_size) + next = -1; + } + + if (next < 0) { + pc->header_size = FFMIN(sizeof(pc->header), buf_size); + memcpy(pc->header, buf, pc->header_size); + pc->buffered_size = buf_size; + next = END_NOT_FOUND; + } + + if (ff_combine_frame(&pc->pc, next, &buf, &buf_size) < 0) { + *poutbuf = NULL; + *poutbuf_size = 0; + return buf_size; + } + } + + *poutbuf = buf; + *poutbuf_size = buf_size; + return next; +} + +AVCodecParser ff_sbc_parser = { + .codec_ids = { AV_CODEC_ID_SBC, AV_CODEC_ID_MSBC }, + .priv_data_size = sizeof(SBCParseContext), + .parser_parse = sbc_parse, + .parser_close = ff_parse_close, +};