Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 1/3] avcodec/evc_frame_merge: properly support packets with more than one NALU
@ 2023-06-21 16:59 James Almer
  2023-06-21 16:59 ` [FFmpeg-devel] [PATCH 2/3] avcodec/evc_frame_merge: reindent after previous commit James Almer
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: James Almer @ 2023-06-21 16:59 UTC (permalink / raw)
  To: ffmpeg-devel

Parse through all NALUs in a packet, pull new ones when a complete AU could not
be assembled, or keep them around if an AU was assembled while data remained in
them.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/evc_frame_merge_bsf.c | 110 +++++++++++++++++++------------
 1 file changed, 69 insertions(+), 41 deletions(-)

diff --git a/libavcodec/evc_frame_merge_bsf.c b/libavcodec/evc_frame_merge_bsf.c
index a9b44f4d10..8af6095bf1 100644
--- a/libavcodec/evc_frame_merge_bsf.c
+++ b/libavcodec/evc_frame_merge_bsf.c
@@ -70,30 +70,14 @@ static void evc_frame_merge_flush(AVBSFContext *bsf)
     ctx->au_buffer.data_size = 0;
 }
 
-static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
+static int parse_nal_unit(AVBSFContext *bsf, const uint8_t *buf, int buf_size)
 {
     EVCFMergeContext *ctx = bsf->priv_data;
-    AVPacket *in = ctx->in;
-    uint8_t *buffer;
     GetBitContext gb;
     enum EVCNALUnitType nalu_type;
-    uint32_t nalu_size;
-    int tid, au_end_found = 0;
-    int err;
-
-    err = ff_bsf_get_packet_ref(bsf, in);
-    if (err < 0)
-        return err;
-
-    // NAL unit parsing needed to determine if end of AU was found
-    nalu_size = evc_read_nal_unit_length(in->data, EVC_NALU_LENGTH_PREFIX_SIZE, bsf);
-    if (!nalu_size || nalu_size > INT_MAX) {
-        av_log(bsf, AV_LOG_ERROR, "Invalid NAL unit size: (%d)\n", nalu_size);
-        err = AVERROR_INVALIDDATA;
-        goto end;
-    }
+    int tid, err;
 
-    err = init_get_bits8(&gb, in->data + EVC_NALU_LENGTH_PREFIX_SIZE, nalu_size);
+    err = init_get_bits8(&gb, buf, buf_size);
     if (err < 0)
         return err;
 
@@ -101,15 +85,13 @@ static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
     // @see enum EVCNALUnitType in evc.h
     if (get_bits1(&gb)) {// forbidden_zero_bit
         av_log(bsf, AV_LOG_ERROR, "Invalid NAL unit header\n");
-        err = AVERROR_INVALIDDATA;
-        goto end;
+        return AVERROR_INVALIDDATA;
     }
 
     nalu_type = get_bits(&gb, 6) - 1;
     if (nalu_type < EVC_NOIDR_NUT || nalu_type > EVC_UNSPEC_NUT62) {
         av_log(bsf, AV_LOG_ERROR, "Invalid NAL unit type: (%d)\n", nalu_type);
-        err = AVERROR_INVALIDDATA;
-        goto end;
+        return AVERROR_INVALIDDATA;
     }
 
     tid = get_bits(&gb, 3);
@@ -121,14 +103,14 @@ static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
         err = ff_evc_parse_sps(&gb, &ctx->ps);
         if (err < 0) {
             av_log(bsf, AV_LOG_ERROR, "SPS parsing error\n");
-            goto end;
+            return err;
         }
         break;
     case EVC_PPS_NUT:
         err = ff_evc_parse_pps(&gb, &ctx->ps);
         if (err < 0) {
             av_log(bsf, AV_LOG_ERROR, "PPS parsing error\n");
-            goto end;
+            return err;
         }
         break;
     case EVC_IDR_NUT:   // Coded slice of a IDR or non-IDR picture
@@ -138,16 +120,16 @@ static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
         err = ff_evc_parse_slice_header(&gb, &sh, &ctx->ps, nalu_type);
         if (err < 0) {
             av_log(bsf, AV_LOG_ERROR, "Slice header parsing error\n");
-            goto end;
+            return err;
         }
 
         // POC (picture order count of the current picture) derivation
         // @see ISO/IEC 23094-1:2020(E) 8.3.1 Decoding process for picture order count
         err = ff_evc_derive_poc(&ctx->ps, &sh, &ctx->poc, nalu_type, tid);
         if (err < 0)
-            goto end;
+            return err;
 
-        au_end_found = end_of_access_unit_found(&ctx->ps, &sh, &ctx->poc, nalu_type);
+        return end_of_access_unit_found(&ctx->ps, &sh, &ctx->poc, nalu_type);
 
         break;
     }
@@ -158,8 +140,56 @@ static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
         break;
     }
 
+    return 0;
+}
+
+static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
+{
+    EVCFMergeContext *ctx = bsf->priv_data;
+    AVPacket *in = ctx->in;
+    size_t data_size;
+    int au_end_found = 0, err;
+
+    while (!au_end_found) {
+        uint8_t *buffer;
+        uint32_t nalu_size;
+
+        if (!in->size) {
+            av_packet_unref(in);
+            err = ff_bsf_get_packet_ref(bsf, in);
+            if (err < 0) {
+                if (err == AVERROR_EOF && ctx->au_buffer.data_size > 0)
+                    break;
+                return err;
+            }
+        }
+
+        // Buffer size is not enough for buffer to store NAL unit 4-bytes prefix (length)
+        if (in->size < EVC_NALU_LENGTH_PREFIX_SIZE)
+            return AVERROR_INVALIDDATA;
+
+        nalu_size = evc_read_nal_unit_length(in->data, EVC_NALU_LENGTH_PREFIX_SIZE, bsf);
+        if (!nalu_size || nalu_size > INT_MAX) {
+            av_log(bsf, AV_LOG_ERROR, "Invalid NAL unit size: (%u)\n", nalu_size);
+            err = AVERROR_INVALIDDATA;
+            goto end;
+        }
+
+        if (in->size < nalu_size + EVC_NALU_LENGTH_PREFIX_SIZE) {
+            err = AVERROR_INVALIDDATA;
+            goto end;
+        }
+
+        err = parse_nal_unit(bsf, in->data + EVC_NALU_LENGTH_PREFIX_SIZE, nalu_size);
+        if (err < 0) {
+            av_log(bsf, AV_LOG_ERROR, "Parsing of NAL unit failed\n");
+            goto end;
+        }
+        au_end_found = err;
+
+        nalu_size += EVC_NALU_LENGTH_PREFIX_SIZE;
     buffer = av_fast_realloc(ctx->au_buffer.data, &ctx->au_buffer.capacity,
-                             ctx->au_buffer.data_size + in->size);
+                             ctx->au_buffer.data_size + nalu_size);
     if (!buffer) {
         av_freep(&ctx->au_buffer.data);
         err = AVERROR_INVALIDDATA;
@@ -167,30 +197,28 @@ static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
     }
 
     ctx->au_buffer.data = buffer;
-    memcpy(ctx->au_buffer.data + ctx->au_buffer.data_size, in->data, in->size);
+    memcpy(ctx->au_buffer.data + ctx->au_buffer.data_size, in->data, nalu_size);
 
-    ctx->au_buffer.data_size += in->size;
+    ctx->au_buffer.data_size += nalu_size;
 
-    av_packet_unref(in);
+        in->data += nalu_size;
+        in->size -= nalu_size;
+    }
 
-    if (au_end_found) {
-        size_t data_size = ctx->au_buffer.data_size;
+    av_packet_unref(in);
+    data_size = ctx->au_buffer.data_size;
 
         ctx->au_buffer.data_size = 0;
         err = av_new_packet(out, data_size);
         if (err < 0)
-            return err;
+            goto end;
 
         memcpy(out->data, ctx->au_buffer.data, data_size);
-    } else
-        err = AVERROR(EAGAIN);
-
-    if (err < 0 && err != AVERROR(EAGAIN))
-        ctx->au_buffer.data_size = 0;
 
+    err = 0;
 end:
     if (err < 0)
-        av_packet_unref(in);
+        evc_frame_merge_flush(bsf);
     return err;
 }
 
-- 
2.41.0

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [FFmpeg-devel] [PATCH 2/3] avcodec/evc_frame_merge: reindent after previous commit
  2023-06-21 16:59 [FFmpeg-devel] [PATCH 1/3] avcodec/evc_frame_merge: properly support packets with more than one NALU James Almer
@ 2023-06-21 16:59 ` James Almer
  2023-06-21 16:59 ` [FFmpeg-devel] [PATCH 3/3] avcodec/evc_frame_merge: copy input packet props to output James Almer
  2023-06-22 17:25 ` [FFmpeg-devel] [PATCH 1/3] avcodec/evc_frame_merge: properly support packets with more than one NALU James Almer
  2 siblings, 0 replies; 4+ messages in thread
From: James Almer @ 2023-06-21 16:59 UTC (permalink / raw)
  To: ffmpeg-devel

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/evc_frame_merge_bsf.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/libavcodec/evc_frame_merge_bsf.c b/libavcodec/evc_frame_merge_bsf.c
index 8af6095bf1..36ffec5736 100644
--- a/libavcodec/evc_frame_merge_bsf.c
+++ b/libavcodec/evc_frame_merge_bsf.c
@@ -188,18 +188,18 @@ static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
         au_end_found = err;
 
         nalu_size += EVC_NALU_LENGTH_PREFIX_SIZE;
-    buffer = av_fast_realloc(ctx->au_buffer.data, &ctx->au_buffer.capacity,
-                             ctx->au_buffer.data_size + nalu_size);
-    if (!buffer) {
-        av_freep(&ctx->au_buffer.data);
-        err = AVERROR_INVALIDDATA;
-        goto end;
-    }
+        buffer = av_fast_realloc(ctx->au_buffer.data, &ctx->au_buffer.capacity,
+                                 ctx->au_buffer.data_size + nalu_size);
+        if (!buffer) {
+            av_freep(&ctx->au_buffer.data);
+            err = AVERROR_INVALIDDATA;
+            goto end;
+        }
 
-    ctx->au_buffer.data = buffer;
-    memcpy(ctx->au_buffer.data + ctx->au_buffer.data_size, in->data, nalu_size);
+        ctx->au_buffer.data = buffer;
+        memcpy(ctx->au_buffer.data + ctx->au_buffer.data_size, in->data, nalu_size);
 
-    ctx->au_buffer.data_size += nalu_size;
+        ctx->au_buffer.data_size += nalu_size;
 
         in->data += nalu_size;
         in->size -= nalu_size;
@@ -208,12 +208,12 @@ static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
     av_packet_unref(in);
     data_size = ctx->au_buffer.data_size;
 
-        ctx->au_buffer.data_size = 0;
-        err = av_new_packet(out, data_size);
-        if (err < 0)
-            goto end;
+    ctx->au_buffer.data_size = 0;
+    err = av_new_packet(out, data_size);
+    if (err < 0)
+        goto end;
 
-        memcpy(out->data, ctx->au_buffer.data, data_size);
+    memcpy(out->data, ctx->au_buffer.data, data_size);
 
     err = 0;
 end:
-- 
2.41.0

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [FFmpeg-devel] [PATCH 3/3] avcodec/evc_frame_merge: copy input packet props to output
  2023-06-21 16:59 [FFmpeg-devel] [PATCH 1/3] avcodec/evc_frame_merge: properly support packets with more than one NALU James Almer
  2023-06-21 16:59 ` [FFmpeg-devel] [PATCH 2/3] avcodec/evc_frame_merge: reindent after previous commit James Almer
@ 2023-06-21 16:59 ` James Almer
  2023-06-22 17:25 ` [FFmpeg-devel] [PATCH 1/3] avcodec/evc_frame_merge: properly support packets with more than one NALU James Almer
  2 siblings, 0 replies; 4+ messages in thread
From: James Almer @ 2023-06-21 16:59 UTC (permalink / raw)
  To: ffmpeg-devel

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/evc_frame_merge_bsf.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/libavcodec/evc_frame_merge_bsf.c b/libavcodec/evc_frame_merge_bsf.c
index 36ffec5736..8a09c12dbb 100644
--- a/libavcodec/evc_frame_merge_bsf.c
+++ b/libavcodec/evc_frame_merge_bsf.c
@@ -34,7 +34,7 @@ typedef struct AccessUnitBuffer {
 } AccessUnitBuffer;
 
 typedef struct EVCFMergeContext {
-    AVPacket *in;
+    AVPacket *in, *buffer_pkt;
     EVCParamSets ps;
     EVCParserPoc poc;
     AccessUnitBuffer au_buffer;
@@ -67,6 +67,7 @@ static void evc_frame_merge_flush(AVBSFContext *bsf)
 
     ff_evc_ps_free(&ctx->ps);
     av_packet_unref(ctx->in);
+    av_packet_unref(ctx->buffer_pkt);
     ctx->au_buffer.data_size = 0;
 }
 
@@ -146,7 +147,7 @@ static int parse_nal_unit(AVBSFContext *bsf, const uint8_t *buf, int buf_size)
 static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
 {
     EVCFMergeContext *ctx = bsf->priv_data;
-    AVPacket *in = ctx->in;
+    AVPacket *in = ctx->in, *buffer_pkt = ctx->buffer_pkt;
     size_t data_size;
     int au_end_found = 0, err;
 
@@ -162,6 +163,16 @@ static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
                     break;
                 return err;
             }
+            /* Buffer packets with timestamps (there should be at most one per AU)
+             * or any packet if buffer_pkt is empty. The latter is needed to
+             * passthrough positions in case there are no timestamps like with
+             * the raw EVC demuxer. */
+            if (!buffer_pkt->data ||
+                in->pts != AV_NOPTS_VALUE && buffer_pkt->pts == AV_NOPTS_VALUE) {
+                err = av_packet_ref(buffer_pkt, in);
+                if (err < 0)
+                    goto end;
+            }
         }
 
         // Buffer size is not enough for buffer to store NAL unit 4-bytes prefix (length)
@@ -210,9 +221,13 @@ static int evc_frame_merge_filter(AVBSFContext *bsf, AVPacket *out)
 
     ctx->au_buffer.data_size = 0;
     err = av_new_packet(out, data_size);
+    if (err < 0)
+        goto end;
+    err = av_packet_copy_props(out, buffer_pkt);
     if (err < 0)
         goto end;
 
+    av_packet_unref(buffer_pkt);
     memcpy(out->data, ctx->au_buffer.data, data_size);
 
     err = 0;
@@ -227,7 +242,8 @@ static int evc_frame_merge_init(AVBSFContext *bsf)
     EVCFMergeContext *ctx = bsf->priv_data;
 
     ctx->in  = av_packet_alloc();
-    if (!ctx->in)
+    ctx->buffer_pkt = av_packet_alloc();
+    if (!ctx->in || !ctx->buffer_pkt)
         return AVERROR(ENOMEM);
 
     return 0;
@@ -238,6 +254,7 @@ static void evc_frame_merge_close(AVBSFContext *bsf)
     EVCFMergeContext *ctx = bsf->priv_data;
 
     av_packet_free(&ctx->in);
+    av_packet_free(&ctx->buffer_pkt);
     ff_evc_ps_free(&ctx->ps);
 
     ctx->au_buffer.capacity = 0;
-- 
2.41.0

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/3] avcodec/evc_frame_merge: properly support packets with more than one NALU
  2023-06-21 16:59 [FFmpeg-devel] [PATCH 1/3] avcodec/evc_frame_merge: properly support packets with more than one NALU James Almer
  2023-06-21 16:59 ` [FFmpeg-devel] [PATCH 2/3] avcodec/evc_frame_merge: reindent after previous commit James Almer
  2023-06-21 16:59 ` [FFmpeg-devel] [PATCH 3/3] avcodec/evc_frame_merge: copy input packet props to output James Almer
@ 2023-06-22 17:25 ` James Almer
  2 siblings, 0 replies; 4+ messages in thread
From: James Almer @ 2023-06-22 17:25 UTC (permalink / raw)
  To: ffmpeg-devel

On 6/21/2023 1:59 PM, James Almer wrote:
> Parse through all NALUs in a packet, pull new ones when a complete AU could not
> be assembled, or keep them around if an AU was assembled while data remained in
> them.
> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
>   libavcodec/evc_frame_merge_bsf.c | 110 +++++++++++++++++++------------
>   1 file changed, 69 insertions(+), 41 deletions(-)

Will apply set.
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-06-22 17:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-21 16:59 [FFmpeg-devel] [PATCH 1/3] avcodec/evc_frame_merge: properly support packets with more than one NALU James Almer
2023-06-21 16:59 ` [FFmpeg-devel] [PATCH 2/3] avcodec/evc_frame_merge: reindent after previous commit James Almer
2023-06-21 16:59 ` [FFmpeg-devel] [PATCH 3/3] avcodec/evc_frame_merge: copy input packet props to output James Almer
2023-06-22 17:25 ` [FFmpeg-devel] [PATCH 1/3] avcodec/evc_frame_merge: properly support packets with more than one NALU James Almer

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git