From patchwork Thu Aug 24 09:52:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 43296 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:24a6:b0:149:dfde:5c0a with SMTP id m38csp1190900pzd; Thu, 24 Aug 2023 02:45:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFYg3wRL86v7QBXysrexrurdENx2UKfmREgmPBsGlCzZrNexMRRMbvabZ4voO7UmZQT7nDB X-Received: by 2002:a17:907:a063:b0:9a1:eb4f:56f with SMTP id ia3-20020a170907a06300b009a1eb4f056fmr3654452ejc.13.1692870339087; Thu, 24 Aug 2023 02:45:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692870339; cv=none; d=google.com; s=arc-20160816; b=UN6mm2tl5K4ulvMiSH7mqmHnBT1K9l9BuaKmmx3Mjzy4z30l8Ni0Z653P8OWpD0ijg Yep/hbHHH1HAXoF46oEb7csR2JTeL6ECb4s5rUgk+xBlhOFTdVKSuLR4uKFWAcOFLtTh ZDC/07hOe1N4/fYnQAfpYZ0C9VXUGRVqM0Og0UcfBsgyPYJoo/Xglx5WP/xzs++x7kRb aqHCjDk2vmT4PL2S9gqisXioNm5q53QAkleRKzb8YkVBJUxlm8F+403xRgHtMDv1T3l7 sQ6EcfkJl8WO6AxincEuScq6B5oRjpuKGKDNW/z2ELgi95Fah28NbWxI6PrspyWqY8uL 7nRw== 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:to :message-id:date:from:mime-version:dkim-signature:delivered-to; bh=pVur7UpbUTe4qZC6DW1LISIaQkTYKBQCARgLZT539dA=; fh=ghmNPjFTSM6oKC/7k5yo9hnu8V2TFPrY7GkS/gBuc+4=; b=pxK9FdXTzZK3wvjBJLP+O6lfqCtBd+ywXe5mI4+Vs4kech927rhhbvsp0n/L74xtjT DMaKg9xOzzSmjHUtmq+X1tVNY2BxkeF2tWOjYGuHDd/Vbqfhmf8TxFigELNtMuitU5w9 mDEVIXBt+4y3sHP4No0eniuX8mhsmQ0MWwkLiitaCmjsEFZZhsl24zzuUJi6V8gGQJpP yaF+WWxioKQCY5P5IuKNSai9Rp7W9v5rXDkfsApYyz2MQzudQRPT0VWkuZi29Uuh1lKB YUEDZ+nHdAE58YNG2qH2OKX9PYfO6pLNBJplmimwFdegenxux0VmrRkpnZnRAUjRsKfR gFJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20221208 header.b=gwTBCRnh; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id g20-20020a170906199400b00992ae4db3c9si9888056ejd.15.2023.08.24.02.45.37; Thu, 24 Aug 2023 02:45:39 -0700 (PDT) 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=@gmail.com header.s=20221208 header.b=gwTBCRnh; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 34DA968C626; Thu, 24 Aug 2023 12:45:33 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-vk1-f174.google.com (mail-vk1-f174.google.com [209.85.221.174]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9E22868C5E1 for ; Thu, 24 Aug 2023 12:45:26 +0300 (EEST) Received: by mail-vk1-f174.google.com with SMTP id 71dfb90a1353d-48d0dbf4ec5so1508638e0c.2 for ; Thu, 24 Aug 2023 02:45:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692870325; x=1693475125; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=YlRisQeE3IQ9hjc2TdMbipFUQx12rOpqUjzE78shBSM=; b=gwTBCRnhatf3vQ+TPuxo20rr47jcQmDcTRKZwYkn93o6WCnLQhxUvaiOAOIYhYhdEr KDd4QjV5WsLBJFvxALPu0SwFELdwdXvStPJY9mceYJgU2fT5NkpD7okbToSgJgPy/GxM cEmY56e+5hwJrdW/6TvsSoUhH+LtgDGFI54z/2MDxgOw+52VjAGbRWPZUqm8V8SUfoDP yPNec0YqiWz5xvVuzEFEnIeLD1hRuGFEtn0izWLFvOO+/auLd3UCuW53pEYf0jTyPkF5 j4TofUfnD+euhPmNQ316hCLTtRGe8tjw9zpYVt0lPVHRsPzksDyU4x/340JGaS8PFnKx UYrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692870325; x=1693475125; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=YlRisQeE3IQ9hjc2TdMbipFUQx12rOpqUjzE78shBSM=; b=PT0Ft5dAZdsont3Cz7uu43oeQ87DgJe00SoqA7uz8Hf2FBe0NLujFReJo3+/l+EBKZ wXiRUCxH6h87pQg+6MFhSeuayznYxnKVRuA6O5BsoV8eu+VO7RT5z5h+40tJTY98AmiF 3hYPQqFYIutVakBbxDMTTirgs0FFRXeHD7DYphLKJ90DpyEfH03lVU8WKRxbzp/lhgBw YfUDTI24VLWl/6VOsVAlRAEcl1dalfa1/Ur/jtsx8SWB7Ey82Z2RrM1h5bE1m6npbJ55 oqcq64Cic9xVnrTksrysoeUF2qihGoC6wyfh6aGvkOt/RV6YQXCRDgRuH6pUeU6+5YBT 49Hw== X-Gm-Message-State: AOJu0Yx2a4AB+8wKjnR62F890i1d/t4NYz/YAkDcLxhWXjvJ2LshcOif 4aufoCgqDkpiBV2GcnugR1JXu9tIy197FGoyWSyd4Pbp X-Received: by 2002:a1f:c886:0:b0:48f:c2d6:6311 with SMTP id y128-20020a1fc886000000b0048fc2d66311mr2960673vkf.0.1692870324994; Thu, 24 Aug 2023 02:45:24 -0700 (PDT) MIME-Version: 1.0 From: Paul B Mahol Date: Thu, 24 Aug 2023 11:52:45 +0200 Message-ID: To: FFmpeg development discussions and patches X-Content-Filtered-By: Mailman/MimeDel 2.1.29 Subject: [FFmpeg-devel] [PATCH] OSQ lossless audio format support X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: WLUXPGyDBLI8 Patches attached. Stereo decoding have some issues with some predictors so not yet bitexact. Please comment. From 988ae6c88f0e5e9f26568825c8daa82a176bb2e1 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Tue, 27 Jun 2023 19:54:25 +0200 Subject: [PATCH 1/2] avcodec: add OSQ audio decoder Signed-off-by: Paul B Mahol --- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/codec_desc.c | 7 + libavcodec/codec_id.h | 1 + libavcodec/osq.c | 435 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 445 insertions(+) create mode 100644 libavcodec/osq.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 3c16b51462..f3afda61d4 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -579,6 +579,7 @@ OBJS-$(CONFIG_OPUS_DECODER) += opusdec.o opusdec_celt.o opus_celt.o \ opusdsp.o opus_parse.o opus_rc.o OBJS-$(CONFIG_OPUS_ENCODER) += opusenc.o opusenc_psy.o opus_celt.o \ opus_pvq.o opus_rc.o opustab.o +OBJS-$(CONFIG_OSQ_DECODER) += osq.o OBJS-$(CONFIG_PAF_AUDIO_DECODER) += pafaudio.o OBJS-$(CONFIG_PAF_VIDEO_DECODER) += pafvideo.o OBJS-$(CONFIG_PAM_DECODER) += pnmdec.o pnm.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 8775d15a4f..6e95ca5636 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -516,6 +516,7 @@ extern const FFCodec ff_nellymoser_decoder; extern const FFCodec ff_on2avc_decoder; extern const FFCodec ff_opus_encoder; extern const FFCodec ff_opus_decoder; +extern const FFCodec ff_osq_decoder; extern const FFCodec ff_paf_audio_decoder; extern const FFCodec ff_qcelp_decoder; extern const FFCodec ff_qdm2_decoder; diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 4406dd8318..f556bb94d5 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -3413,6 +3413,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("AC-4"), .props = AV_CODEC_PROP_LOSSY, }, + { + .id = AV_CODEC_ID_OSQ, + .type = AVMEDIA_TYPE_AUDIO, + .name = "osq", + .long_name = NULL_IF_CONFIG_SMALL("OSQ (Original Sound Quality)"), + .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS, + }, /* subtitle codecs */ { diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h index a5a0cb8525..29b410b8d3 100644 --- a/libavcodec/codec_id.h +++ b/libavcodec/codec_id.h @@ -543,6 +543,7 @@ enum AVCodecID { AV_CODEC_ID_WAVARC, AV_CODEC_ID_RKA, AV_CODEC_ID_AC4, + AV_CODEC_ID_OSQ, /* subtitle codecs */ AV_CODEC_ID_FIRST_SUBTITLE = 0x17000, ///< A dummy ID pointing at the start of subtitle codecs. diff --git a/libavcodec/osq.c b/libavcodec/osq.c new file mode 100644 index 0000000000..b6dc5c1bb4 --- /dev/null +++ b/libavcodec/osq.c @@ -0,0 +1,435 @@ +/* + * OSQ audio decoder + * Copyright (c) 2023 Paul B Mahol + * + * 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 + */ + +#define ASSERT_LEVEL 5 +#include "libavutil/avassert.h" +#include "libavutil/internal.h" +#include "libavutil/intreadwrite.h" +#include "avcodec.h" +#include "codec_internal.h" +#include "decode.h" +#include "internal.h" +#define BITSTREAM_READER_LE +#include "get_bits.h" +#include "unary.h" + +typedef struct OSQChannel { + int prediction; + int coding_mode; + int residue_parameter; + int residue_bits; +} OSQChannel; + +typedef struct OSQContext { + GetBitContext gb; + OSQChannel ch[2]; + + uint8_t *bitstream; + int64_t max_framesize; + int bitstream_size; + + int frame_samples; + int64_t nb_samples; + + int32_t *decode_buffer[2]; + + AVPacket *pkt; + int pkt_offset; +} OSQContext; + +static av_cold int osq_close(AVCodecContext *avctx) +{ + OSQContext *s = avctx->priv_data; + + av_freep(&s->bitstream); + s->bitstream_size = 0; + + for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) + av_freep(&s->decode_buffer[ch]); + + return 0; +} + +static av_cold int osq_init(AVCodecContext *avctx) +{ + OSQContext *s = avctx->priv_data; + + if (avctx->extradata_size < 48) + return AVERROR(EINVAL); + + if (avctx->extradata[0] != 1) { + av_log(avctx, AV_LOG_ERROR, "Unsupported version.\n"); + return AVERROR_INVALIDDATA; + } + + avctx->sample_rate = AV_RL32(avctx->extradata + 4); + if (avctx->sample_rate < 1) + return AVERROR_INVALIDDATA; + + avctx->ch_layout.nb_channels = avctx->extradata[3]; + if (avctx->ch_layout.nb_channels < 1) + return AVERROR_INVALIDDATA; + + switch (avctx->extradata[2]) { + case 8: avctx->sample_fmt = AV_SAMPLE_FMT_U8P; break; + case 16: avctx->sample_fmt = AV_SAMPLE_FMT_S16P; break; + case 20: + case 24: + case 28: + case 32: avctx->sample_fmt = AV_SAMPLE_FMT_S32P; break; + default: return AVERROR_INVALIDDATA; + } + + s->nb_samples = AV_RL64(avctx->extradata + 16); + s->frame_samples = AV_RL16(avctx->extradata + 8); + s->max_framesize = (s->frame_samples * 16 + 1024) * avctx->ch_layout.nb_channels; + + s->bitstream = av_calloc(s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE, sizeof(*s->bitstream)); + if (!s->bitstream) + return AVERROR(ENOMEM); + + for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) { + s->decode_buffer[ch] = av_calloc(s->frame_samples + 4, + sizeof(*s->decode_buffer[ch])); + if (!s->decode_buffer[ch]) + return AVERROR(ENOMEM); + } + + s->pkt = avctx->internal->in_pkt; + + return 0; +} + +static uint32_t get_urice(GetBitContext *gb, int k) +{ + uint32_t z, x, b; + + x = get_unary(gb, 1, 512); + b = get_bits_long(gb, k & 31); + z = b | x << (k & 31); + + return z; +} + +static int32_t get_srice(GetBitContext *gb, int x) +{ + int32_t y = get_urice(gb, x); + return get_bits1(gb) ? -y : y; +} + +static int osq_channel_parameters(AVCodecContext *avctx, int ch) +{ + OSQContext *s = avctx->priv_data; + OSQChannel *cb = &s->ch[ch]; + GetBitContext *gb = &s->gb; + + cb->prediction = get_urice(gb, 5); + cb->coding_mode = get_urice(gb, 3); + if (cb->prediction >= 15) + return AVERROR_INVALIDDATA; + if (cb->coding_mode > 0 && cb->coding_mode < 3) { + cb->residue_parameter = get_urice(gb, 4); + if (!cb->residue_parameter || cb->residue_parameter >= 31) + return AVERROR_INVALIDDATA; + } else if (cb->coding_mode == 3) { + cb->residue_bits = get_urice(gb, 4); + if (!cb->residue_bits || cb->residue_bits >= 31) + return AVERROR_INVALIDDATA; + } else if (cb->coding_mode) { + return AVERROR_INVALIDDATA; + } + + return 0; +} + +static int osq_decode_block(AVCodecContext *avctx, AVFrame *frame) +{ + OSQContext *s = avctx->priv_data; + int ret, decorrelate, downsample; + GetBitContext *gb = &s->gb; + + skip_bits1(gb); + decorrelate = get_bits1(gb); + downsample = get_bits1(gb); + + for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) { + if ((ret = osq_channel_parameters(avctx, ch)) < 0) { + av_log(avctx, AV_LOG_ERROR, "invalid channel parameters\n"); + return ret; + } + } + + for (int n = 0; n < frame->nb_samples; n++) { + for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) { + OSQChannel *cb = &s->ch[ch]; + int32_t *dst = s->decode_buffer[ch] + 4; + + if (!cb->coding_mode) { + dst[n] = 0; + } else if (cb->coding_mode == 3) { + dst[n] = get_sbits_long(gb, cb->residue_bits); + } else { + dst[n] = get_srice(gb, cb->residue_parameter); + } + + if (get_bits_left(gb) < 0) { + av_log(avctx, AV_LOG_ERROR, "overread!\n"); + return AVERROR_INVALIDDATA; + } + } + } + + align_get_bits(gb); + +#define A (n-1) +#define B (n-2) +#define C (n-3) +#define D (n-4) +#define P2 (dst[A] * 2LL - dst[B]) +#define P3 ((dst[A] * 1LL - dst[B]) * 3LL + dst[C]) + + for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) { + int32_t pred = 0, *dst = s->decode_buffer[ch] + 4; + const int nb_samples = frame->nb_samples; + OSQChannel *cb = &s->ch[ch]; + + av_log(avctx, AV_LOG_DEBUG, "prediction: %d\n", cb->prediction); + switch (cb->prediction) { + case 0: + break; + case 1: + for (int n = 0; n < nb_samples; n++) + dst[n] += dst[A]; + break; + case 2: + for (int n = 0; n < nb_samples; n++) { + int32_t p = pred; + pred = dst[n] / 2; + dst[n] += dst[A] + p; + } + break; + case 3: + for (int n = 0; n < nb_samples; n++) + dst[n] += P2; + break; + case 4: + for (int n = 0; n < nb_samples; n++) { + int32_t p = pred; + pred = dst[n] / 2; + dst[n] += P2 + p; + } + break; + case 5: + for (int n = 0; n < nb_samples; n++) + dst[n] += P3; + break; + case 6: + for (int n = 0; n < nb_samples; n++) { + int32_t p = pred; + pred = dst[n] / 2; + dst[n] += P3 + p; + } + break; + case 7: + for (int n = 0; n < nb_samples; n++) { + int32_t p = pred; + pred = dst[n] / 2; + dst[n] += (P2 + P3) / 2 + p; + } + break; + case 8: + for (int n = 0; n < nb_samples; n++) + dst[n] += (P2 * 1LL + P3) / 2; + break; + case 9: + for (int n = 0; n < nb_samples; n++) { + int32_t p = pred; + pred = dst[n] / 2; + dst[n] += (P2 * 2 + P3) / 3 + p; + } + break; + case 10: + for (int n = 0; n < nb_samples; n++) { + int32_t p = pred; + pred = dst[n] / 2; + dst[n] += (P2 + P3 * 2) / 3 + p; + } + break; + case 11: + for (int n = 0; n < nb_samples; n++) + dst[n] += (dst[A] * 1LL + dst[B]) / 2; + break; + case 12: + for (int n = 0; n < nb_samples; n++) + dst[n] += dst[B]; + break; + case 13: + for (int n = 0; n < nb_samples; n++) + dst[n] += (dst[D] * 1LL + dst[B]) / 2; + break; + case 14: + for (int n = 0; n < nb_samples; n++) { + int32_t p = pred; + pred = dst[n] / 2; + dst[n] += (P2 + dst[A]) / 2 + p; + } + break; + default: + return AVERROR_INVALIDDATA; + } + + memcpy(s->decode_buffer[ch], s->decode_buffer[ch] + frame->nb_samples, 4 * sizeof(*s->decode_buffer[0])); + + if (downsample) { + int32_t *dst = s->decode_buffer[ch]; + + for (int n = 0; n < frame->nb_samples + 4; n++) + dst[n] = (dst[n] / 256) * 256; + } + } + + if (decorrelate && avctx->ch_layout.nb_channels == 2) { + int32_t *l = s->decode_buffer[0] + 4; + int32_t *r = s->decode_buffer[1] + 4; + + for (int n = 0; n < frame->nb_samples; n++) { + int64_t L = l[n]; + int64_t R = r[n]; + + l[n] = L - R; + r[n] = L + R; + } + } + + switch (avctx->sample_fmt) { + case AV_SAMPLE_FMT_U8P: + for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) { + uint8_t *dst = (uint8_t *)frame->extended_data[ch]; + int32_t *src = s->decode_buffer[ch] + 4; + + for (int n = 0; n < frame->nb_samples; n++) + dst[n] = av_clip_uint8(src[n] + 0x80); + } + break; + case AV_SAMPLE_FMT_S16P: + for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) { + int16_t *dst = (int16_t *)frame->extended_data[ch]; + int32_t *src = s->decode_buffer[ch] + 4; + + for (int n = 0; n < frame->nb_samples; n++) + dst[n] = av_clip_int16(src[n]); + } + break; + case AV_SAMPLE_FMT_S32P: + for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) { + int32_t *dst = (int32_t *)frame->extended_data[ch]; + int32_t *src = s->decode_buffer[ch] + 4; + + for (int n = 0; n < frame->nb_samples; n++) + dst[n] = src[n]; + } + break; + default: + return AVERROR_BUG; + } + + return 0; +} + +static int osq_receive_frame(AVCodecContext *avctx, AVFrame *frame) +{ + OSQContext *s = avctx->priv_data; + GetBitContext *gb = &s->gb; + int ret, n; + + while (s->bitstream_size < s->max_framesize) { + int size; + + if (!s->pkt->data) { + ret = ff_decode_get_packet(avctx, s->pkt); + if (ret == AVERROR_EOF && s->bitstream_size > 0) + break; + if (ret < 0) + return ret; + } + + size = FFMIN(s->pkt->size - s->pkt_offset, s->max_framesize - s->bitstream_size); + memcpy(s->bitstream + s->bitstream_size, s->pkt->data + s->pkt_offset, size); + s->bitstream_size += size; + s->pkt_offset += size; + + if (s->pkt_offset == s->pkt->size) { + av_packet_unref(s->pkt); + s->pkt_offset = 0; + } + } + + frame->nb_samples = FFMIN(s->frame_samples, s->nb_samples); + if (frame->nb_samples <= 0) + return AVERROR_EOF; + + if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) + goto fail; + + if ((ret = init_get_bits8(gb, s->bitstream, s->bitstream_size)) < 0) + goto fail; + + if ((ret = osq_decode_block(avctx, frame)) < 0) + goto fail; + + s->nb_samples -= frame->nb_samples; + + n = get_bits_count(gb) / 8; + if (n > s->bitstream_size) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + + memmove(s->bitstream, &s->bitstream[n], s->bitstream_size - n); + s->bitstream_size -= n; + + return 0; + +fail: + s->bitstream_size = 0; + s->pkt_offset = 0; + av_packet_unref(s->pkt); + + return ret; +} + +const FFCodec ff_osq_decoder = { + .p.name = "osq", + CODEC_LONG_NAME("OSQ (Original Sound Quality)"), + .p.type = AVMEDIA_TYPE_AUDIO, + .p.id = AV_CODEC_ID_OSQ, + .priv_data_size = sizeof(OSQContext), + .init = osq_init, + FF_CODEC_RECEIVE_FRAME_CB(osq_receive_frame), + .close = osq_close, + .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | + AV_CODEC_CAP_DR1, + .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, + .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P, + AV_SAMPLE_FMT_S16P, + AV_SAMPLE_FMT_S32P, + AV_SAMPLE_FMT_NONE }, +}; -- 2.39.1