From patchwork Tue Nov 27 11:29:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 11181 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 5539B44DC33 for ; Tue, 27 Nov 2018 13:37:15 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C4F8268A216; Tue, 27 Nov 2018 13:37:15 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AC33168A1D0 for ; Tue, 27 Nov 2018 13:37:09 +0200 (EET) Received: by mail-wm1-f68.google.com with SMTP id 125so21536565wmh.0 for ; Tue, 27 Nov 2018 03:37:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=KOxaWx6auAaYiCUDaShDLUQMikKoq/o2TlvPpfIEhG0=; b=hjBtVCPRI0DVMmvMXH7RocKNbj3EB5MBKIzl52+iWBARpd3g2rP/21LR4F7hebn/pL NwCUoxUsyZfcRlfb44lWLI2c6J9CTPIFQkiG2ZuwyARK/D81Fu4ljryzoX/vQSrlTsY0 XumIL+Ote7+bIIcJuIY6N8HucuC1EpEVTh221iUIBZy2yUbAQZR/TZi/RZGm/Hgcl94M G+X6YZ1/6dZ30ATpp2uFnerrcRuXY8LzUQ2s7jEt+4i3JlV6yYP3KA+xU4gsXqwobZ9d NziCRL2/3/RvEFU3AIrsT8smRLeFwCHx1UZhyOrYDJp65+QBOnCNTIj0k00BF85m/bhk F0tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=KOxaWx6auAaYiCUDaShDLUQMikKoq/o2TlvPpfIEhG0=; b=rjjhRHW2WYb5657GD2w/ErinHv6E9Ao0Aw+6mKX9BJfdJK29Kl5qYLsxM6PhlAoW4w w8pGSf5fsmykW6SzViGCRKMJnbro5cbNrkHWw0IjxIBulwXSqmb5JpNitmMu3t3zCoDc +p9l9jwy4H7LFnR2c/vL9CeqaVOJBACbIG99IqidYnX8D7vvZlFhVn+8LB2O+P+X18l7 YL4Neas1m/CDk0UiXVdRIv8NMIsfvOz9iFkceXobtlKlJn1QmTAbR0c/7mZMsx+vxONL sauvrctwfzntmZARH8bZJS2WJHmW9LNRQsx91yEJQowg4PKMRgvxE8+RbJySRjJB7QDu qA9A== X-Gm-Message-State: AA+aEWYPISDn/epDr3/nO03Xx0gdhT0vdPu+GC5eRWB18jedoIjsJ0mx i3/p/j2NOmjANyHxUBenuJP1xT5A X-Google-Smtp-Source: AFSGD/UXwMyWTpfI9u4L+pKBCa3geZRn5UvDXLmSE7BFdcJNip00XeW9UsynQO17Xg+5EVVLeLHsGw== X-Received: by 2002:a1c:ee14:: with SMTP id m20mr18400023wmh.47.1543318175979; Tue, 27 Nov 2018 03:29:35 -0800 (PST) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id l37sm3041871wre.69.2018.11.27.03.29.34 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Nov 2018 03:29:35 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Tue, 27 Nov 2018 12:29:25 +0100 Message-Id: <20181127112926.7058-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/2] avcodec: add PCM-DVD encoder 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Fixes #6784. Signed-off-by: Paul B Mahol --- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/pcm-dvdenc.c | 197 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 199 insertions(+) create mode 100644 libavcodec/pcm-dvdenc.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 2840a8069f..5feadac729 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -737,6 +737,7 @@ OBJS-$(CONFIG_PCM_ALAW_DECODER) += pcm.o OBJS-$(CONFIG_PCM_ALAW_ENCODER) += pcm.o OBJS-$(CONFIG_PCM_BLURAY_DECODER) += pcm-bluray.o OBJS-$(CONFIG_PCM_DVD_DECODER) += pcm-dvd.o +OBJS-$(CONFIG_PCM_DVD_ENCODER) += pcm-dvdenc.o OBJS-$(CONFIG_PCM_F16LE_DECODER) += pcm.o OBJS-$(CONFIG_PCM_F24LE_DECODER) += pcm.o OBJS-$(CONFIG_PCM_F32BE_DECODER) += pcm.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 2c17db5a70..d70646e91a 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -494,6 +494,7 @@ extern AVCodec ff_xma2_decoder; extern AVCodec ff_pcm_alaw_encoder; extern AVCodec ff_pcm_alaw_decoder; extern AVCodec ff_pcm_bluray_decoder; +extern AVCodec ff_pcm_dvd_encoder; extern AVCodec ff_pcm_dvd_decoder; extern AVCodec ff_pcm_f16le_decoder; extern AVCodec ff_pcm_f24le_decoder; diff --git a/libavcodec/pcm-dvdenc.c b/libavcodec/pcm-dvdenc.c new file mode 100644 index 0000000000..d26eaf071c --- /dev/null +++ b/libavcodec/pcm-dvdenc.c @@ -0,0 +1,197 @@ +/* + * LPCM codecs for PCM formats found in Video DVD streams + * Copyright (c) 2018 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 + */ + +#include "avcodec.h" +#include "bytestream.h" +#include "internal.h" + +typedef struct PCMDVDContext { + uint8_t header[3]; // Header added to every frame + int block_size; // Size of a block of samples in bytes + int samples_per_block; // Number of samples per channel per block + int groups_per_block; // Number of 20/24-bit sample groups per block + uint8_t *extra_samples; // Pointer to leftover samples from a frame + int extra_sample_count; // Number of leftover samples in the buffer +} PCMDVDContext; + +static av_cold int pcm_dvd_encode_init(AVCodecContext *avctx) +{ + PCMDVDContext *s = avctx->priv_data; + int quant, freq, frame_size; + + switch (avctx->sample_rate) { + case 48000: + freq = 0; + break; + case 96000: + freq = 1; + break; + } + + switch (avctx->sample_fmt) { + case AV_SAMPLE_FMT_S16: + avctx->bits_per_coded_sample = 16; + quant = 0; + break; + case AV_SAMPLE_FMT_S32: + avctx->bits_per_coded_sample = 24; + quant = 2; + break; + } + + avctx->bits_per_coded_sample = 16 + quant * 4; + avctx->block_align = avctx->channels * avctx->bits_per_coded_sample / 8; + avctx->bit_rate = avctx->block_align * 8LL * avctx->sample_rate; + if (avctx->bit_rate > 9800000) { + av_log(avctx, AV_LOG_ERROR, "Too big bitrate: reduce sample rate, bitdepth or channels.\n"); + return AVERROR(EINVAL); + } + + if (avctx->sample_fmt == AV_SAMPLE_FMT_S16) { + s->samples_per_block = 1; + s->block_size = avctx->channels * 2; + frame_size = 2008 / s->block_size; + } else { + switch (avctx->channels) { + case 1: + case 2: + case 4: + /* one group has all the samples needed */ + s->block_size = 4 * avctx->bits_per_coded_sample / 8; + s->samples_per_block = 4 / avctx->channels; + s->groups_per_block = 1; + break; + case 8: + /* two groups have all the samples needed */ + s->block_size = 8 * avctx->bits_per_coded_sample / 8; + s->samples_per_block = 1; + s->groups_per_block = 2; + break; + default: + /* need avctx->channels groups */ + s->block_size = 4 * avctx->channels * + avctx->bits_per_coded_sample / 8; + s->samples_per_block = 4; + s->groups_per_block = avctx->channels; + break; + } + + frame_size = FFALIGN(2008 / s->block_size, s->samples_per_block); + } + + s->header[0] = 0x0c; + s->header[1] = (quant << 6) | (freq << 4) | (avctx->channels - 1); + s->header[2] = 0x80; + + if (!avctx->frame_size) + avctx->frame_size = frame_size; + + return 0; +} + +static int pcm_dvd_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, + const AVFrame *frame, int *got_packet_ptr) +{ + PCMDVDContext *s = avctx->priv_data; + int samples = frame->nb_samples * avctx->channels; + int64_t pkt_size = (frame->nb_samples / s->samples_per_block) * s->block_size + 3; + int blocks = (pkt_size - 3) / s->block_size; + const int16_t *src16; + const int32_t *src32; + PutByteContext pb; + int ret; + + if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0) + return ret; + + memcpy(avpkt->data, s->header, 3); + + src16 = (const int16_t *)frame->data[0]; + src32 = (const int32_t *)frame->data[0]; + + bytestream2_init_writer(&pb, avpkt->data + 3, avpkt->size - 3); + + switch (avctx->sample_fmt) { + case AV_SAMPLE_FMT_S16: + do { + bytestream2_put_be16(&pb, *src16++); + } while (--samples); + break; + case AV_SAMPLE_FMT_S32: + if (avctx->channels == 1) { + do { + for (int i = 2; i; i--) { + bytestream2_put_be16(&pb, src32[0] >> 16); + bytestream2_put_be16(&pb, src32[1] >> 16); + bytestream2_put_byte(&pb, (*src32++) >> 24); + bytestream2_put_byte(&pb, (*src32++) >> 24); + } + } while (--blocks); + } else { + do { + for (int i = s->groups_per_block; i; i--) { + bytestream2_put_be16(&pb, src32[0] >> 16); + bytestream2_put_be16(&pb, src32[1] >> 16); + bytestream2_put_be16(&pb, src32[2] >> 16); + bytestream2_put_be16(&pb, src32[3] >> 16); + bytestream2_put_byte(&pb, (*src32++) >> 24); + bytestream2_put_byte(&pb, (*src32++) >> 24); + bytestream2_put_byte(&pb, (*src32++) >> 24); + bytestream2_put_byte(&pb, (*src32++) >> 24); + } + } while (--blocks); + } + break; + } + + avpkt->pts = frame->pts; + avpkt->size = pkt_size; + avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples); + *got_packet_ptr = 1; + + return 0; +} + +static av_cold int pcm_dvd_encode_close(AVCodecContext *avctx) +{ + return 0; +} + +AVCodec ff_pcm_dvd_encoder = { + .name = "pcm_dvd", + .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for DVD media"), + .type = AVMEDIA_TYPE_AUDIO, + .id = AV_CODEC_ID_PCM_DVD, + .priv_data_size = sizeof(PCMDVDContext), + .init = pcm_dvd_encode_init, + .close = pcm_dvd_encode_close, + .encode2 = pcm_dvd_encode_frame, + .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME, + .supported_samplerates = (const int[]) { 48000, 96000, 0}, + .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO, + AV_CH_LAYOUT_STEREO, + AV_CH_LAYOUT_5POINT1, + AV_CH_LAYOUT_7POINT1, + 0 }, + .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16, + AV_SAMPLE_FMT_S32, + AV_SAMPLE_FMT_NONE }, +};