diff mbox series

[FFmpeg-devel,v2,12/13] cbs_mpeg2.c: improve readability of start code search (part 2)

Message ID 20220203184450.5491-13-scott.the.elm@gmail.com
State New
Headers show
Series rewrite avpriv_find_start_code() for clarity | expand

Commit Message

Scott Theisen Feb. 3, 2022, 6:44 p.m. UTC
If the last four bytes form a valid start code, the loop would run another time.
Since (start == buf_end), start_code is invalidated so the loop terminates.

However, calling avpriv_find_start_code() with p == end, it will immediately
return due to the zero size buffer.  Thus the history is not needed.
---
 libavcodec/cbs_mpeg2.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Andreas Rheinhardt Feb. 5, 2022, 2:24 a.m. UTC | #1
Scott Theisen:
> If the last four bytes form a valid start code, the loop would run another time.
> Since (start == buf_end), start_code is invalidated so the loop terminates.
> 
> However, calling avpriv_find_start_code() with p == end, it will immediately
> return due to the zero size buffer.  Thus the history is not needed.
> ---
>  libavcodec/cbs_mpeg2.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
> index f2efedde5d..bf95fb7546 100644
> --- a/libavcodec/cbs_mpeg2.c
> +++ b/libavcodec/cbs_mpeg2.c
> @@ -168,8 +168,9 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
>              // the next unit will be treated as the last unit.
>              start_code = 0;
>          }
> -
> -        end = avpriv_find_start_code(start, buf_end, &start_code, 0);
> +        else {
> +            end = avpriv_find_start_code(start, buf_end, &start_code, 1);
> +        }
>          start--;
>          // decrement so start points to the byte containing the start_code_identifier
>          // (may be the last byte of fragment->data); end points to the byte

end is now uninitialized in case of a unit consisting solely of a
Sequence End.

- Andreas
Scott Theisen Feb. 5, 2022, 3:59 a.m. UTC | #2
On 2/4/22 21:24, Andreas Rheinhardt wrote:
> end is now uninitialized in case of a unit consisting solely of a
> Sequence End.

I hadn't noticed that.  However, I immediately restored it in the next 
patch.
diff mbox series

Patch

diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index f2efedde5d..bf95fb7546 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -168,8 +168,9 @@  static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
             // the next unit will be treated as the last unit.
             start_code = 0;
         }
-
-        end = avpriv_find_start_code(start, buf_end, &start_code, 0);
+        else {
+            end = avpriv_find_start_code(start, buf_end, &start_code, 1);
+        }
         start--;
         // decrement so start points to the byte containing the start_code_identifier
         // (may be the last byte of fragment->data); end points to the byte