From: James Almer <jamrial@gmail.com> To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH 1/3] avcodec/evc_frame_merge: properly support packets with more than one NALU Date: Wed, 21 Jun 2023 13:59:47 -0300 Message-ID: <20230621165949.1112-1-jamrial@gmail.com> (raw) 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".
next reply other threads:[~2023-06-21 17:00 UTC|newest] Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-06-21 16:59 James Almer [this message] 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
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230621165949.1112-1-jamrial@gmail.com \ --to=jamrial@gmail.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
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