From patchwork Fri Jan 5 11:34:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: misty@brew.sh X-Patchwork-Id: 7142 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.78.2 with SMTP id r2csp661487jaa; Fri, 5 Jan 2018 03:35:43 -0800 (PST) X-Google-Smtp-Source: ACJfBosxuN3xfQUojkzUNWOXQ70bqEJDU4Qk1xnD/ChEJDkRtQSJwWDNAImT6h6HPIDBvwQUTXIE X-Received: by 10.223.155.202 with SMTP id e10mr2322997wrc.264.1515152143714; Fri, 05 Jan 2018 03:35:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515152143; cv=none; d=google.com; s=arc-20160816; b=pbNcrhoXMIIw07Tkk/ho554YV+Ql9Glf6uk/ig9tWxNKo8Nq5+lvBaprVBCe7WzyUL NovmavaxgWvsMI+HdDRSjw+8cCJYP1qPFhK+IewjqMIZXQVrVx6EG6bTAu+XrxFV5hG4 CXFlS1KZucewaodl7RMy0sQujtGBhcOIEDCgFslyOo2D4yfG9UWc6zW6cklZV1FbChHi KTk1K1/er1PYZ94ymMEirR4wx3gl9uSbmHTqyYDxZTqEqLkFAQCbsJImao9iQf2ahB/e 7QbG7suO3rFkOubOp89tQ2fxmj8hdKGdSvky7TBRfWTcXDYd6sGmn6jBGjH68xs4lbmr g3eg== 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:dkim-signature:delivered-to:arc-authentication-results; bh=rlqb0LZFHLYOue+jyBx4WBTZWiyWUiYFBU/a2tPwoY4=; b=cut+PfIV6pxlfAbE6naxRyE/OagYYzBcQ5lv7/IQ0YIXnkRDZyoiYeAI72EJgDVNMJ 9TdhQ5MK8Ufm5TEO8mk7ENL5NHkizqAPmmn1qSpPHdLrTkF8YIVWsanAd1PgkJLCM7rJ Gin4/RFjBHrMb28XdsCsZ6gG6XHJGSOyadLZFIWLi/9H6vkBRfI57BU0PN9PzNVtDoDg CidOYRdjmfMhZYE8IgM56Qjo4buEbwmdFjoJfmCKCvgvG7OcodBI8WZ5a7q3yNgHlC+/ qYwXzs9AQwHrVl9Yt6Ot0io3os3M4ISJ0oJIU90HrKjtkJcpS5qRR+Tde6P+jdg4ZF2O I5GA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=jRhL8d5V; 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 v20si4239688wrf.62.2018.01.05.03.35.43; Fri, 05 Jan 2018 03:35:43 -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=@gmail.com header.s=20161025 header.b=jRhL8d5V; 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 B5BC4689A40; Fri, 5 Jan 2018 13:35:01 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl0-f66.google.com (mail-pl0-f66.google.com [209.85.160.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4530F689A11 for ; Fri, 5 Jan 2018 13:34:54 +0200 (EET) Received: by mail-pl0-f66.google.com with SMTP id 1so2910927plv.0 for ; Fri, 05 Jan 2018 03:35:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=pZ6gLGhyyD3IOYee2MfisVDMoHBVctHoLPxE7elWI4o=; b=jRhL8d5VY1c9RMbQWL3p/MHqpcN0BwWQcUqgcmy9Pts7FZr6i5vGf965/7iSwkSXZ4 /g6LuP0tYjWb7EawE9DjLrCgKmN1Ba8UhILLkF0qrSdh1oWKNQmy7u8jMehmepLPPwI2 IXxfQKmEAUmK4qqZmvJJK/PFtrS5b4E0AzMsTZpj/FG+2s8Hjmdz4OLJPLWDLUd7o2dO luzcsdVoFPYMHkylXHIrF2i7JwAdYaN3to7LasbPqcb2N//ln9m8ESMRneryISfX0o5Q hHVY8AW/q9yZZkt0lifo+Y/Hc+XyyehJUs0M7ftVNLRNh9jsALoztVwU08kV5spXX0jZ W1DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=pZ6gLGhyyD3IOYee2MfisVDMoHBVctHoLPxE7elWI4o=; b=XP+OBdPycDcg+Nwfsa20vLjTTUnq/OdZmS5a0DTs+r0h9V+Xag/Gj2LI7tgmGyil5Y C1R6IMFDjOXbZq5rq7OyHEiF1F5twHwVW2Lq76jajteqh+giNUmpiOsXa5uoUqf8iSMT T3oRhsBhqIu3zH/0xJl156h9wYcq0mYjvn+mhc/bLCNm+abU2MrB0urYfjSmvQOXSKgR wxtDDot54hQPPf0xHlyoux+7ME33UZtK4I/rwxuO9j0TLsAn+iVIiTR/PRcnoRQzidBj 3DNkFK9vj67MQYbR+yKVFgqKo++P60YKnV79vGfIgaQ/2fytENLwi/WYsMYlVH5d2gfd 7RRg== X-Gm-Message-State: AKGB3mLag9RC7xtNYHb1Sod83LiSaNf9V59MQsBpNSx6RkgjzImIVkU2 KhUK1rI08OuhECeLw17ssgTbBkWK X-Received: by 10.84.244.130 with SMTP id h2mr2835685pll.408.1515152109715; Fri, 05 Jan 2018 03:35:09 -0800 (PST) Received: from localhost.localdomain (180-150-45-216.NBN.mel.aussiebb.net. [180.150.45.216]) by smtp.gmail.com with ESMTPSA id s184sm11549586pfb.9.2018.01.05.03.35.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 05 Jan 2018 03:35:08 -0800 (PST) From: misty@brew.sh To: ffmpeg-devel@ffmpeg.org Date: Fri, 5 Jan 2018 22:34:45 +1100 Message-Id: <20180105113448.13917-4-misty@brew.sh> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180105113448.13917-1-misty@brew.sh> References: <20180102213143.GE4926@michaelspb> <20180105113448.13917-1-misty@brew.sh> Subject: [FFmpeg-devel] [PATCH 3/6] mpeg: add experimental support for PSMF audio. 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: Misty De Meo , Maxim Poliakovski MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Maxim Poliakovski Changes by Misty De Meo : atrac3plus_parser: remove return statements for invalid data atrac3plus_parser: use libavcodec's oma atrac3plus_parser: pass along unexpected data unaltered atrac3plus_parser: adjust bytes_remain type Change by Michael "Bazz" Bazzinotti : atrac3plus_parser: don't always fail video for "2nd frame portion found" Signed-off-by: Misty De Meo --- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/atrac3plus_parser.c | 170 +++++++++++++++++++++++++++++++++++++++++ libavformat/mpeg.c | 27 ++++++- 4 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 libavcodec/atrac3plus_parser.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index cfacd6b70c..e3f7afbfe7 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -977,6 +977,7 @@ OBJS-$(CONFIG_AAC_PARSER) += aac_parser.o aac_ac3_parser.o \ mpeg4audio.o OBJS-$(CONFIG_AC3_PARSER) += ac3tab.o aac_ac3_parser.o OBJS-$(CONFIG_ADX_PARSER) += adx_parser.o adx.o +OBJS-$(CONFIG_ATRAC3P_PARSER) += atrac3plus_parser.o OBJS-$(CONFIG_BMP_PARSER) += bmp_parser.o OBJS-$(CONFIG_CAVSVIDEO_PARSER) += cavs_parser.o OBJS-$(CONFIG_COOK_PARSER) += cook_parser.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index ed1e7ab06e..81d5d2814a 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -623,6 +623,7 @@ static void register_all(void) REGISTER_PARSER(AAC_LATM, aac_latm); REGISTER_PARSER(AC3, ac3); REGISTER_PARSER(ADX, adx); + REGISTER_PARSER(ATRAC3P, atrac3p); REGISTER_PARSER(BMP, bmp); REGISTER_PARSER(CAVSVIDEO, cavsvideo); REGISTER_PARSER(COOK, cook); diff --git a/libavcodec/atrac3plus_parser.c b/libavcodec/atrac3plus_parser.c new file mode 100644 index 0000000000..5aebdd5fbc --- /dev/null +++ b/libavcodec/atrac3plus_parser.c @@ -0,0 +1,170 @@ +/* + * Copyright (C) 2016 Michael "Bazz" Bazzinotti + * Copyright (C) 2014 Maxim Poliakovski + * + * 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 "parser.h" +#include "get_bits.h" +#include "oma.h" + +typedef struct Atrac3PlusParseContext { + ParseContext pc; + uint8_t hdr[8]; + int hdr_bytes_needed; + int sample_rate, channel_id, frame_size; + uint8_t got_bytes; +} Atrac3PlusParseContext; + +static int parse_sound_frame_header(Atrac3PlusParseContext *c, + const uint8_t *buf) +{ + uint16_t atrac_config; + + if (AV_RB16(buf) != 0x0FD0) + return AVERROR_INVALIDDATA; + + atrac_config = AV_RB16(&buf[2]); + c->sample_rate = oma_srate_tab[(atrac_config >> 13) & 7] * 100; + c->channel_id = (atrac_config >> 10) & 7; + c->frame_size = ((atrac_config & 0x3FF) * 8) + 8; + + if (!c->channel_id || !c->sample_rate || !c->frame_size) + return AVERROR_INVALIDDATA; + + return 0; +} + +static int ff_atrac3p_parse(AVCodecParserContext *s, + AVCodecContext *avctx, + const uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size) +{ + Atrac3PlusParseContext *ctx = s->priv_data; + const uint8_t *hdr_buf = buf; + uint16_t bytes_remain; + int frame_size, hdr_bytes = 8; + int next = 0; + int second_portion_found = 0; + + if (s->flags & PARSER_FLAG_COMPLETE_FRAMES || !buf_size) { + next = buf_size; + } else { + if (buf_size >= 2) { + bytes_remain = AV_RB16(buf); + + if (bytes_remain != 0xFD0) { + second_portion_found = bytes_remain && !ctx->pc.index && !ctx->hdr_bytes_needed; + /* Got something unexpected; either this means we got + the second part of a frame and not the first part, + or more likely these aren't ATRAC3+ packets in the + format we're expecting. For example, it might mean + the source data is from ATRAC3+ in RIFF WAVE, which + doesn't use the same format as ATRAC3+ in MPEG. + Just pass this along unaltered. */ + if (second_portion_found && !ctx->got_bytes) { + *poutbuf = buf; + *poutbuf_size = buf_size; + return buf_size; + } + + next += 2; + buf += 2; + buf_size -= 2; + hdr_buf = buf; + + if (second_portion_found) { + ctx->got_bytes = 0; + goto process; + } + + if (ctx->hdr_bytes_needed) { + if (buf_size >= ctx->hdr_bytes_needed) { + memcpy(&ctx->hdr[8 - ctx->hdr_bytes_needed], + buf, ctx->hdr_bytes_needed); + hdr_bytes = ctx->hdr_bytes_needed; + ctx->hdr_bytes_needed = 0; + hdr_buf = ctx->hdr; + } + } else if (bytes_remain) { + if (buf_size < bytes_remain) { + av_log(avctx, AV_LOG_ERROR, + "Couldn't combine frame: bytes needed=%d, bytes supplied=%d\n", + bytes_remain, buf_size); + } + + next += bytes_remain; + ff_combine_frame(&ctx->pc, bytes_remain, &buf, &buf_size); + + *poutbuf = buf; + *poutbuf_size = buf_size; + return next; + } + } + } + + if (buf_size < hdr_bytes) { + /* looks like we got an incomplete header */ + memcpy(ctx->hdr, buf, buf_size); + ctx->hdr_bytes_needed = 8 - buf_size; + *poutbuf = NULL; + *poutbuf_size = 0; + return buf_size; + } + + if (parse_sound_frame_header(ctx, hdr_buf)) { + av_log(avctx, AV_LOG_ERROR, "Invalid sound frame header!\n"); + } + + avctx->sample_rate = ctx->sample_rate; + avctx->block_align = ctx->frame_size; + avctx->bit_rate = ctx->sample_rate * ctx->frame_size * 8 / 2048; + avctx->channels = oma_chid_to_num_channels[ctx->channel_id - 1]; + avctx->channel_layout = oma_chid_to_native_layout[ctx->channel_id - 1]; + + next += hdr_bytes; + buf += hdr_bytes; + buf_size -= hdr_bytes; + if (!buf_size) + ctx->got_bytes = 1; + process: + frame_size = ctx->frame_size; + + if (buf_size < frame_size) + frame_size = END_NOT_FOUND; + + if (ff_combine_frame(&ctx->pc, frame_size, &buf, &buf_size) < 0) { + *poutbuf = NULL; + *poutbuf_size = 0; + return buf_size + next; + } + + next += frame_size >= 0 ? frame_size : buf_size; + } + + *poutbuf = buf; + *poutbuf_size = buf_size; + return next; +} + +AVCodecParser ff_atrac3p_parser = { + .codec_ids = { AV_CODEC_ID_ATRAC3P }, + .priv_data_size = sizeof(Atrac3PlusParseContext), + .parser_parse = ff_atrac3p_parse, + .parser_close = ff_parse_close, +}; diff --git a/libavformat/mpeg.c b/libavformat/mpeg.c index 50fe7a1a76..895c6fb231 100644 --- a/libavformat/mpeg.c +++ b/libavformat/mpeg.c @@ -128,6 +128,7 @@ typedef struct MpegDemuxContext { int sofdec; int dvd; int imkh_cctv; + int sony_psmf; // true if Play Station Movie file signature is present #if CONFIG_VOBSUB_DEMUXER AVFormatContext *sub_ctx; FFDemuxSubtitlesQueue q[32]; @@ -147,6 +148,8 @@ static int mpegps_read_header(AVFormatContext *s) avio_get_str(s->pb, 6, buffer, sizeof(buffer)); if (!memcmp("IMKH", buffer, 4)) { m->imkh_cctv = 1; + } else if (!memcmp("PSMF00", buffer, 6)) { + m->sony_psmf = 1; } else if (!memcmp("Sofdec", buffer, 6)) { m->sofdec = 1; } else @@ -441,7 +444,7 @@ redo: goto redo; } - if (startcode == PRIVATE_STREAM_1) { + if (startcode == PRIVATE_STREAM_1 && !m->sony_psmf) { startcode = avio_r8(s->pb); len--; } @@ -541,6 +544,28 @@ redo: else request_probe= 1; type = AVMEDIA_TYPE_VIDEO; + } else if (startcode == PRIVATE_STREAM_1 && m->sony_psmf) { + uint8_t stream_id; + + if (len < 2) + goto skip; + stream_id = avio_r8(s->pb); + avio_r8(s->pb); // skip padding + len -= 2; + if (!(stream_id & 0xF0)) { // seems like we got an ATRAC stream + /* check if an appropriate stream already exists */ + for (i = 0; i < s->nb_streams; i++) { + st = s->streams[i]; + if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && + st->codec->codec_id == AV_CODEC_ID_ATRAC3P && + st->id - 0x1BD0 == (stream_id & 0xF)) + goto found; + } + + startcode = 0x1BD0 + (stream_id & 0xF); + type = AVMEDIA_TYPE_AUDIO; + codec_id = AV_CODEC_ID_ATRAC3P; + } } else if (startcode == PRIVATE_STREAM_2) { type = AVMEDIA_TYPE_DATA; codec_id = AV_CODEC_ID_DVD_NAV;