From: Scott Theisen <scott.the.elm@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Subject: Re: [FFmpeg-devel] [PATCH 4/7] avcodec/cbs_mpeg2: Simplify splitting fragment
Date: Fri, 4 Feb 2022 14:01:41 -0500
Message-ID: <7cefe7ce-b6a0-e58f-6145-73deb65e8731@gmail.com> (raw)
In-Reply-To: <AM7PR03MB666042964502BE402BAE58658F299@AM7PR03MB6660.eurprd03.prod.outlook.com>
On 2/4/22 10:16, Andreas Rheinhardt wrote:
> avpriv_find_start_code() supports non-contiguous buffers
> by maintaining a state that allows to find start codes
> that span across multiple buffers; a consequence thereof
> is that avpriv_find_start_code() is given a zero-sized
> buffer, it does not modify this state, so that it appears
> as if a start code was found if the state contained a start code.
>
> This can e.g. happen with Sequence End units in MPEG-2 and
> to counter this, cbs_mpeg2_split_fragment() reset the state
> when it has already encountered the end of the fragment
> in order to add the last unit (if it is only of the form 00 00 01 xy)
> only once; it also used a flag to set whether this is the final unit.
>
> Yet this can be improved by simply resetting state unconditionally
> (thereby avoiding a branch); the flag can be removed by just checking
> whether we have a valid start code (of the next unit to add)
> at the end.
>
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
> libavcodec/cbs_mpeg2.c | 17 ++++++-----------
> 1 file changed, 6 insertions(+), 11 deletions(-)
>
> diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
> index 2bf38c6001..90d667ddc8 100644
> --- a/libavcodec/cbs_mpeg2.c
> +++ b/libavcodec/cbs_mpeg2.c
> @@ -149,7 +149,6 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
> uint32_t start_code = -1;
> size_t unit_size;
> int err;
> - int final = 0;
>
> start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
> &start_code);
> @@ -161,14 +160,11 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
> do {
> unit_type = start_code & 0xff;
>
> - if (start == frag->data + frag->data_size) {
> - // The last four bytes form a start code which constitutes
> - // a unit of its own. In this situation avpriv_find_start_code
> - // won't modify start_code at all so modify start_code so that
> - // the next unit will be treated as the last unit.
> - start_code = 0;
> - }
> -
> + // Reset start_code to ensure that avpriv_find_start_code()
> + // really reads a new start code and does not reuse the old
> + // start code in any way (as e.g. happens when there is a
> + // Sequence End unit at the very end of a packet).
> + start_code = UINT32_MAX;
> end = avpriv_find_start_code(start--, frag->data + frag->data_size,
> &start_code);
>
> @@ -183,7 +179,6 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
> } else {
> // We didn't find a start code, so this is the final unit.
> unit_size = end - start;
> - final = 1;
> }
>
> err = ff_cbs_append_unit_data(frag, unit_type, (uint8_t*)start,
> @@ -192,7 +187,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
> return err;
>
> start = end;
> - } while (!final);
> + } while ((start_code >> 8) == 0x000001);
>
> return 0;
> }
I assume this is inspired by my patch series. Did you see v2 of my
patch series where I made similar and more extensive clarifications?
The check for sequence_end_codes should not be in the loop. It harms
readability, although you also eliminated the branch.
Regards,
Scott
_______________________________________________
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 prev parent reply other threads:[~2022-02-04 19:01 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-04 15:14 [FFmpeg-devel] [PATCH 1/7] avcodec/cbs_mpeg2: Remove redundant counter Andreas Rheinhardt
2022-02-04 15:16 ` [FFmpeg-devel] [PATCH 2/7] avcodec/cbs_jpeg: " Andreas Rheinhardt
2022-02-04 15:16 ` [FFmpeg-devel] [PATCH 3/7] avcodec/cbs: Make ff_cbs_insert_unit_data() always append the new unit Andreas Rheinhardt
2022-02-04 15:16 ` [FFmpeg-devel] [PATCH 4/7] avcodec/cbs_mpeg2: Simplify splitting fragment Andreas Rheinhardt
2022-02-04 19:01 ` Scott Theisen [this message]
2022-02-05 8:16 ` Scott Theisen
2022-02-04 15:16 ` [FFmpeg-devel] [PATCH 5/7] all: Remove unnecessary libavcodec/internal.h inclusions Andreas Rheinhardt
2022-02-04 15:16 ` [FFmpeg-devel] [PATCH 6/7] avcodec/internal.h: Move avpriv_find_start_code() to startcode.h Andreas Rheinhardt
2022-02-04 19:05 ` Scott Theisen
2022-02-05 8:04 ` Andreas Rheinhardt
2022-02-04 15:16 ` [FFmpeg-devel] [PATCH 7/7] avcodec/cbs_mpeg2: Use smaller scope for variables Andreas Rheinhardt
2022-02-05 6:36 ` [FFmpeg-devel] [PATCH] cbs_mpeg2_split_fragment(): cache the buffer end Scott Theisen
2022-02-07 2:18 ` [FFmpeg-devel] [PATCH 1/7] avcodec/cbs_mpeg2: Remove redundant counter Andreas Rheinhardt
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=7cefe7ce-b6a0-e58f-6145-73deb65e8731@gmail.com \
--to=scott.the.elm@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