diff mbox series

[FFmpeg-devel,v2] add prores bitstream demuxer and muxer

Message ID PH7PR11MB7430A00ABF66C520673D2F0DC403A@PH7PR11MB7430.namprd11.prod.outlook.com
State New
Headers show
Series [FFmpeg-devel,v2] add prores bitstream demuxer and muxer | expand

Checks

Context Check Description
andriy/commit_msg_x86 warning The first line of the commit message must start with a context terminated by a colon and a space, for example "lavu/opt: " or "doc: ".
yinshiyou/commit_msg_loongarch64 warning The first line of the commit message must start with a context terminated by a colon and a space, for example "lavu/opt: " or "doc: ".
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

hung kuishing July 25, 2023, 11:21 a.m. UTC
Signed-off-by: clarkh <hungkuishing@outlook.com>
---
 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

Comments

Tomas Härdin July 25, 2023, 11:47 a.m. UTC | #1
> +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)

As others pointed out, is there a maximum width or height also, or
perhaps a maximum width*height?

/Tomas
hung kuishing July 25, 2023, 12:40 p.m. UTC | #2
Hi, width and height use u(16) descriptor in prores specification, and spec does not limit scope, 
I guess 1~65535 are allowable. So I think I'll change it to check if they are equal to 0.
What do you propose?

-----邮件原件-----
发件人: ffmpeg-devel <ffmpeg-devel-bounces@ffmpeg.org> 代表 Tomas H?rdin
发送时间: 2023年7月25日 19:48
收件人: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
主题: Re: [FFmpeg-devel] [PATCH v2] add prores bitstream demuxer and muxer

> +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)

As others pointed out, is there a maximum width or height also, or perhaps a maximum width*height?

/Tomas
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
Tomas Härdin July 25, 2023, 12:42 p.m. UTC | #3
tis 2023-07-25 klockan 12:40 +0000 skrev hung kuishing:
> Hi, width and height use u(16) descriptor in prores specification,
> and spec does not limit scope, 
> I guess 1~65535 are allowable. So I think I'll change it to check if
> they are equal to 0.
> What do you propose?

If the spec says any non-zero value is fine then any non-zero value is
fine. Just wondering if it had any other limits explicitly stated.

/Tomas
Derek Buitenhuis July 25, 2023, 5:05 p.m. UTC | #4
Hello,

On 7/25/2023 12:21 PM, hung kuishing wrote:
> Signed-off-by: clarkh <hungkuishing@outlook.com>
> ---
>  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

My previous email on this patch was ignored: http://ffmpeg.org/pipermail/ffmpeg-devel/2023-July/312552.html

- Derek
diff mbox series

Patch

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 <hungkuishing@outlook.com>
+ * 
+ * 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 <hungkuishing@outlook.com>
+ *
+ * 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",