From patchwork Tue Jul 25 11:21:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: hung kuishing X-Patchwork-Id: 42975 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:2987:b0:130:ccc6:6c4b with SMTP id f7csp148773pzh; Tue, 25 Jul 2023 04:21:52 -0700 (PDT) X-Google-Smtp-Source: APBJJlHIxD9ur4cEDGR1ZrQweEJzZ3vM227d+b7Q2hELFXb6+lFoH3NXUPWYOvzQ78dhkco9P0Od X-Received: by 2002:a05:6512:b97:b0:4fb:97e8:bc1c with SMTP id b23-20020a0565120b9700b004fb97e8bc1cmr9534078lfv.54.1690284111706; Tue, 25 Jul 2023 04:21:51 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z3-20020aa7d403000000b005222780b1c4si3785998edq.600.2023.07.25.04.21.51; Tue, 25 Jul 2023 04:21:51 -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=@outlook.com header.s=selector1 header.b=WwTYW2zq; arc=fail (body hash mismatch); 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=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 345BC68C8B8; Tue, 25 Jul 2023 14:21:48 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02olkn2098.outbound.protection.outlook.com [40.92.15.98]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2BD9C68C57E for ; Tue, 25 Jul 2023 14:21:42 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=F3NtE5QbqIbGp2gQbKfcZ9IkuHot3IMjcA3RmcCy1I1Jax9c7ZEgpRt96cIJqzuQNIAXuINO6ON5CUCXY6DD+35vekQH8SM2K/wf/rp9573DfvLAYwfQlXBB6ovKjxSVh5LPQXo/+EsRnn1EA4+QVwBzS08En6jPY1Rg4dCICVNNi45GOWKv3peQUJ9ZEZC47SYBG3Whi0X66odQO63dYLGEWRXxhPGHGadVNBXPHrFRp61BzebuAet5sodrTCEaoHHD4fKAhmgcCVOiZAAB/xvBEhEsnAQITlqg9ssAai02JUMQbSchExMacEyKpN7KAbhXwAXUJijaO14CwARZQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=1wWL2DyFID8Fr+Tof0UkYKrr/QoUEUNCdjVe8vko68Q=; b=VXs2KayoOG4wEC9UciX2+/LAsGUinyuTz21Kovl6RB6OLRPZjaYDNJeNCK8mu77ExvjsGASu4ChkGkJchmQErq4Em75+VVzF3fr32UcNnAo7VmlC+WUdR2uoAqNKtDG0JktkzGrBq+utFfQ7znoOQmUraO8D2M+RAEkDdbNTg3TN6OjLnHIN4602Z176Gd/4w8NnmzkL2OQof7hwvkwC7uBjNJ56MbsE8BKYe51LliAgxSZDfCcOe9pkZqhSilYSYDkrfOQ+CNmWmyQ8xhwnCqCgjkxZhS5z/ESmIiRVtIXX/Hcb2Qr0O5rg1pxNU2wUu5Xee/ckFxHv3JQecqQUWw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1wWL2DyFID8Fr+Tof0UkYKrr/QoUEUNCdjVe8vko68Q=; b=WwTYW2zq3M5NpDvwiFWY6+uhvRram3+JEnwg99Ikze9tNZBQR/6V/8ixawLPLtAcqfX9+GMxSEyPtn1ZjDzLvJ8ViBSPsqY+YPl49Xov8dBKIwmTZCGB9u4xilKeZQzF3o/U9sitU5/EhSJo/2zH0iwA74YmFH/42EBHs2RO1CHOxvLFo8VyIUzF4HuwcTukSripOwhxD9Vl9ARXV+UlnykAbKAqYqhRuMx0dphaiatBYfjX6vliefuROE3K82kRT6hzQIGGcxCrlJT2zLeaZyUo8hkY5JebrpagyYjHJ3xZS4Z5ESSS7+u+aPNphg/hu3jxpII+qFZXjFhG38X1sA== Received: from PH7PR11MB7430.namprd11.prod.outlook.com (2603:10b6:510:274::20) by MW4PR11MB6666.namprd11.prod.outlook.com (2603:10b6:303:1eb::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.32; Tue, 25 Jul 2023 11:21:39 +0000 Received: from PH7PR11MB7430.namprd11.prod.outlook.com ([fe80::6216:b3a7:a5b2:bd43]) by PH7PR11MB7430.namprd11.prod.outlook.com ([fe80::6216:b3a7:a5b2:bd43%4]) with mapi id 15.20.6609.032; Tue, 25 Jul 2023 11:21:39 +0000 From: hung kuishing To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH v2] add prores bitstream demuxer and muxer Thread-Index: Adm+5/fnEwcCvfVrTp2fUDJ7K7srUw== Date: Tue, 25 Jul 2023 11:21:39 +0000 Message-ID: Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [ICgST5nQtGpIb7E20cwtHE+s4tL3NOCCc6BFAh8Bsfg=] x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PH7PR11MB7430:EE_|MW4PR11MB6666:EE_ x-ms-office365-filtering-correlation-id: cd591b9a-22e4-4bb6-7015-08db8d0150a3 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: vphSBZ/1WdCSBNV+HVUnr0Ty+U7yfRqz9UdBIM8uCZqp2aWwc6gwyGZ7/s+0Av1KYOVCNBK040iVwaMMdDcKy8e2DU2tmc1X5DCBys7hPExy07nBNwqNhgE/3nyuZ9dvTv8yjh74uMcHAeZY0jdpXUYbFCTGXuOu94txkqj5iWIZ6Vb3CJBqWPuG/R+OPoUalLrLjE0O6LMzSX2aprKvzsE16kUs9QY1aRhCDQjgIHpPQB36YPZ9NXnQ4KBqjNGI68gKdsmop1/stBjj962bD+vVQ5LTpOe4XSp/1f3dNAWSvyJosHtjmfb3E4QwybVsdH2oNHJ/zf4AF2o8mOft3vuW0MXQLpTCA0YB1zfdB+pXHJXhxEbZJU5NgPiZi/1uayB15kASclCzp9DfL64lrv1ig42ieXMtkgq9zxkyn+trllyeytrK/YUS+1FeKIi24yzjX+sBujcNlomGXeyLv1uvPiN0w1I+aU9D5GpfPzqJKtLAVma/SXT76YEAE6O49sEQtaRQ+/nV+D1YYaT5VPq/BrK7JLl3LLkmC+etI1aR+2ab9NEZuuqcoM/rKw6U x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: zjCIiBjJkEaP/gX7Y4Wncl1wKi2oA/p716IrqpVEl1klxh2SUQloqaca1sXX7PyBuVA4X9Bp+jw510tXDh8vkFh7758ndyFWf2M4ASxDH+/1R6Z3SxgI5c93bS95/qsxofFbhjQgNk5R6VkDZgGDj5oNaYlkHd+9E7DJamvadLul0jS/1ESXC6EdRad/ZrWHKaRe8VxCq3cncvsZ6ydf048RgZOGVwboWJqXV2+I1mHUPBHz1Oah4PtLSYzT/eLxCiYu5U6cmmtesdmCwAO2oNMj3lbEHNJYv3UIhFTfaAeD09LKjgrCPpVDIpd+uzUC267w8TLlNMDmTLivRechoC07H39R6g5wv241624r9O3+omqMfGOb2MtvyskPg60jhLfpcQoaF8yBF40dKDug3K99cQ/Q1olRAtUADxZW8rK7cw5Bhpbx3etbsETgnvmThe4RZZbrCSONgd9z+UqN4wg5i2PMQXz+jdlN/sxXsLHfLdVtClckZE6qUyf69zbIHhQO078zB1bHbEDgEBe+UtE+ZWbUgGiaRpFHj5oqD6nJt8gryIZaZS0V/8jqolxa07WCQktRop2mz/1T2f3R3XUS48VSvuT+g/k4VpDEIO0FhN3V2lL3pBXuTXmR0+w+e2PEu5fhuJx6Bg+TEenb8P68bzlxHcnp5yJ8cU8p0EqAWtC/iiNhFo320Z9hzTmf3JIxkSgE5mbtMnQ1rrbrdhcOuKpdRggLV251XCI16WoGvNkYnXELIx93wKk1EsjURiKk4sBzWfBUZPxKxenIJrqNyAvBzFJ+w1hP/WyvwE4QvyHMKBtufXyeJuq7o3bjJwWWdyNY1unDeLD5dvnK7ocBqP4Nkjj2+WtHe9HssNx0Qi4jXL4lW1u/lTRdBsC4wa6A53HDdNOKHOvUrJne36dvGRsXTNZjFhgnklsCFZNTAsEBOYLbubhKyLjmrMjSUFM8lqd6vopCEflwGkmDxgeQ9j/iycd/syHBIGdqGQKuLTqBqw9OazVN2T2c61zP1Hmcp85NduFseo4HjpmzNrN/TzQaFCzGRCDjcr6n8KqGdmQsKr1xoMfAYQEoZzLU/SlZ2JiBZN8ep6oSyZyXiKiwNHftJxC4Wr8KEDtn4mSOlYZKZLvRluzkqf7FjkM7a+qp+FbEPXmXSeRfzCGGBoqUm4TVlBFtPEJlhtIJebhT5FB8nawzQsJ5BT3aZTjdxArgYdxJtdYnXnQaTmZGcQ== MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: PH7PR11MB7430.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: cd591b9a-22e4-4bb6-7015-08db8d0150a3 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-originalarrivaltime: 25 Jul 2023 11:21:39.0344 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR11MB6666 Subject: [FFmpeg-devel] [PATCH v2] add prores bitstream demuxer and muxer 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: KGjjdg52BGKY Signed-off-by: clarkh --- libavcodec/Makefile | 1 + libavcodec/parsers.c | 1 + libavcodec/prores_parser.c | 107 +++++++++++++++++++++++++++++++++++++ libavformat/Makefile | 2 + libavformat/allformats.c | 2 + libavformat/proresdec.c | 66 +++++++++++++++++++++++ libavformat/rawenc.c | 13 +++++ 7 files changed, 192 insertions(+) create mode 100644 libavcodec/prores_parser.c create mode 100644 libavformat/proresdec.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 1b0226c089..21cd28c9ac 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -1198,6 +1198,7 @@ OBJS-$(CONFIG_OPUS_PARSER) += opus_parser.o opus_parse.o \ vorbis_data.o OBJS-$(CONFIG_PNG_PARSER) += png_parser.o OBJS-$(CONFIG_PNM_PARSER) += pnm_parser.o pnm.o +OBJS-$(CONFIG_PRORES_PARSER) += prores_parser.o OBJS-$(CONFIG_QOI_PARSER) += qoi_parser.o OBJS-$(CONFIG_RV30_PARSER) += rv34_parser.o OBJS-$(CONFIG_RV40_PARSER) += rv34_parser.o diff --git a/libavcodec/parsers.c b/libavcodec/parsers.c index 285f81a901..131867686a 100644 --- a/libavcodec/parsers.c +++ b/libavcodec/parsers.c @@ -64,6 +64,7 @@ extern const AVCodecParser ff_mpegvideo_parser; extern const AVCodecParser ff_opus_parser; extern const AVCodecParser ff_png_parser; extern const AVCodecParser ff_pnm_parser; +extern const AVCodecParser ff_prores_parser; extern const AVCodecParser ff_qoi_parser; extern const AVCodecParser ff_rv30_parser; extern const AVCodecParser ff_rv40_parser; diff --git a/libavcodec/prores_parser.c b/libavcodec/prores_parser.c new file mode 100644 index 0000000000..a9e5b9e100 --- /dev/null +++ b/libavcodec/prores_parser.c @@ -0,0 +1,107 @@ +/* + * ProRes bitstream parser + * Copyright (c) 2023 clarkh + * + * 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 "libavutil/intreadwrite.h" +#include "libavcodec/proresdata.h" + +typedef struct { + ParseContext pc; + int remaining; + int overwrite; +} ProResParserContext; + +static int prores_find_frame_end(ProResParserContext *pctx, const uint8_t *buf, int buf_size) +{ + ParseContext *pc = &pctx->pc; + uint64_t state64 = pc->state64; + int pic_found = pc->frame_start_found; + int i = 0; + + if (!pic_found) { + for (i = 0; i < buf_size; i++) { + state64 = (state64 << 8) | buf[i]; + if ((state64 & 0xFFFFFFFF) == FRAME_ID) { + i++; + pic_found = 1; + pctx->remaining = state64 >> 32; + pctx->remaining -= pctx->overwrite; + break; + } + } + } + + if (pic_found) { + if (!buf_size) + return END_NOT_FOUND; + + if (pctx->remaining > buf_size) { + pctx->remaining -= buf_size; + } else { + int remaining = pctx->remaining; + + pc->frame_start_found = 0; + pc->state64 = -1; + pctx->remaining = 0; + pctx->overwrite = 0; + return remaining; + } + } else { + pctx->overwrite += buf_size; + } + + pc->frame_start_found = pic_found; + pc->state64 = state64; + + return END_NOT_FOUND; +} + +static int prores_parse(AVCodecParserContext *s, AVCodecContext *avctx, + const uint8_t **poutbuf, int *poutbuf_size, + const uint8_t *buf, int buf_size) +{ + ProResParserContext *pctx = s->priv_data; + ParseContext *pc = &pctx->pc; + int next; + + if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { + next = buf_size; + } else { + next = prores_find_frame_end(pctx, buf, buf_size); + if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { + *poutbuf = NULL; + *poutbuf_size = 0; + return buf_size; + } + } + + *poutbuf = buf; + *poutbuf_size = buf_size; + + return next; +} + +const AVCodecParser ff_prores_parser = { + .codec_ids = { AV_CODEC_ID_PRORES }, + .priv_data_size = sizeof(ProResParserContext), + .parser_parse = prores_parse, + .parser_close = ff_parse_close +}; diff --git a/libavformat/Makefile b/libavformat/Makefile index bd78c206b9..16def0765b 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -480,6 +480,8 @@ OBJS-$(CONFIG_PDV_DEMUXER) += pdvdec.o OBJS-$(CONFIG_PJS_DEMUXER) += pjsdec.o subtitles.o OBJS-$(CONFIG_PMP_DEMUXER) += pmpdec.o OBJS-$(CONFIG_PP_BNK_DEMUXER) += pp_bnk.o +OBJS-$(CONFIG_PRORES_DEMUXER) += proresdec.o rawdec.o +OBJS-$(CONFIG_PRORES_MUXER) += rawenc.o OBJS-$(CONFIG_PVA_DEMUXER) += pva.o OBJS-$(CONFIG_PVF_DEMUXER) += pvfdec.o pcm.o OBJS-$(CONFIG_QCP_DEMUXER) += qcp.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 6324952bd2..0b762034ca 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -378,6 +378,8 @@ extern const AVInputFormat ff_pdv_demuxer; extern const AVInputFormat ff_pjs_demuxer; extern const AVInputFormat ff_pmp_demuxer; extern const AVInputFormat ff_pp_bnk_demuxer; +extern const AVInputFormat ff_prores_demuxer; +extern const FFOutputFormat ff_prores_muxer; extern const FFOutputFormat ff_psp_muxer; extern const AVInputFormat ff_pva_demuxer; extern const AVInputFormat ff_pvf_demuxer; diff --git a/libavformat/proresdec.c b/libavformat/proresdec.c new file mode 100644 index 0000000000..f81863a973 --- /dev/null +++ b/libavformat/proresdec.c @@ -0,0 +1,66 @@ +/* + * ProRes bitstream probe + * Copyright (c) 2023 clarkh + * + * 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 "libavutil/intreadwrite.h" +#include "libavcodec/proresdata.h" +#include "avformat.h" +#include "rawdec.h" + +#define FRAME_FIXED_HEADER_SIZE 20 + +static int prores_check_frame_header(const uint8_t *buf, const int data_size) +{ + int hdr_size, width, height; + int version, alpha_info; + + hdr_size = AV_RB16(buf); + if (hdr_size < FRAME_FIXED_HEADER_SIZE) + return AVERROR_INVALIDDATA; + + version = buf[3]; + if (version > 1) + return AVERROR_INVALIDDATA; + + width = AV_RB16(buf + 8); + height = AV_RB16(buf + 10); + if (width < 16 || height < 16) + return AVERROR_INVALIDDATA; + + alpha_info = buf[17] & 0x0f; + if (alpha_info > 2) + return AVERROR_INVALIDDATA; + + return 0; +} + +static int prores_probe(const AVProbeData *p) +{ + // 8: frame_size(4B) + frame_identifier(4B) + if (p->buf_size < (8 + FRAME_FIXED_HEADER_SIZE) || AV_RB32(p->buf + 4) != FRAME_ID) + return 0; + + if (prores_check_frame_header(p->buf + 8, p->buf_size - 8) < 0) + return 0; + + return AVPROBE_SCORE_MAX; +} + +FF_DEF_RAWVIDEO_DEMUXER(prores, "raw ProRes", prores_probe, "prores", AV_CODEC_ID_PRORES) diff --git a/libavformat/rawenc.c b/libavformat/rawenc.c index f916db13a2..28ca47ae70 100644 --- a/libavformat/rawenc.c +++ b/libavformat/rawenc.c @@ -538,6 +538,19 @@ const FFOutputFormat ff_obu_muxer = { }; #endif +#if CONFIG_PRORES_MUXER +const FFOutputFormat ff_prores_muxer = { + .p.name = "prores", + .p.long_name = NULL_IF_CONFIG_SMALL("raw prores video"), + .p.extensions = "prores", + .p.audio_codec = AV_CODEC_ID_NONE, + .p.video_codec = AV_CODEC_ID_PRORES, + .init = force_one_stream, + .write_packet = ff_raw_write_packet, + .p.flags = AVFMT_NOTIMESTAMPS, +}; +#endif + #if CONFIG_RAWVIDEO_MUXER const FFOutputFormat ff_rawvideo_muxer = { .p.name = "rawvideo",