diff mbox series

[FFmpeg-devel,3/3] avcodec/mpeg12dec: Optimize reading mpeg2 intra escape codes

Message ID 20201008195313.471755-3-andreas.rheinhardt@gmail.com
State Accepted
Commit fe9bc1cc45e2bebba1efa7b9a20b0d66679bf2d5
Headers show
Series [FFmpeg-devel,1/3] avcodec/mpeg12: Reduce size of motion-vector VLC | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Oct. 8, 2020, 7:53 p.m. UTC
Said escape code is only six bits long, so that one has at least 25 - 6
bits in the bitstream reader's cache after reading it; therefore the
whole following 18 bits (containing the actual code) are already in the
bitstream reader's cache, making it unnecessary to reload the cache.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/mpeg12dec.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

Comments

Michael Niedermayer Oct. 9, 2020, 9:18 p.m. UTC | #1
On Thu, Oct 08, 2020 at 09:53:13PM +0200, Andreas Rheinhardt wrote:
> Said escape code is only six bits long, so that one has at least 25 - 6
> bits in the bitstream reader's cache after reading it; therefore the
> whole following 18 bits (containing the actual code) are already in the
> bitstream reader's cache, making it unnecessary to reload the cache.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavcodec/mpeg12dec.c | 10 ++++------
>  1 file changed, 4 insertions(+), 6 deletions(-)

Interresting that this was not noticed before but
LGTM

thx

[...]
Andreas Rheinhardt Oct. 10, 2020, 3:05 a.m. UTC | #2
Michael Niedermayer:
> On Thu, Oct 08, 2020 at 09:53:13PM +0200, Andreas Rheinhardt wrote:
>> Said escape code is only six bits long, so that one has at least 25 - 6
>> bits in the bitstream reader's cache after reading it; therefore the
>> whole following 18 bits (containing the actual code) are already in the
>> bitstream reader's cache, making it unnecessary to reload the cache.
>>
>> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
>> ---
>>  libavcodec/mpeg12dec.c | 10 ++++------
>>  1 file changed, 4 insertions(+), 6 deletions(-)
> 
> Interresting that this was not noticed before but
> LGTM
> 
> thx
> 
There is actually more like this: The GET_VLC macro (which is used
internally by get_vlc2() unless one uses the cached bitstream reader)
currently always reloads the cache after every read, although one can
omit the first reload if one only reads at most 12 bits at a time as is
done in most usages of get_vlc2() in the codebase. For the cached
bitstream reader one could even ensure that >= 32 bits are initially in
the cache and then only read from the cache; currently the code for
refilling the code (quite big in terms of size) exists multiple times in
the binaries. Will take care of it.

- Andreas
diff mbox series

Patch

diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
index 7b448d3648..3be90d7f25 100644
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -524,10 +524,9 @@  static inline int mpeg2_decode_block_intra(MpegEncContext *s,
             } else {
                 /* escape */
                 run = SHOW_UBITS(re, &s->gb, 6) + 1;
-                LAST_SKIP_BITS(re, &s->gb, 6);
-                UPDATE_CACHE(re, &s->gb);
+                SKIP_BITS(re, &s->gb, 6);
                 level = SHOW_SBITS(re, &s->gb, 12);
-                SKIP_BITS(re, &s->gb, 12);
+                LAST_SKIP_BITS(re, &s->gb, 12);
                 i += run;
                 if (i > MAX_INDEX)
                     break;
@@ -606,10 +605,9 @@  static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
             } else {
                 /* escape */
                 run = SHOW_UBITS(re, &s->gb, 6) + 1;
-                LAST_SKIP_BITS(re, &s->gb, 6);
-                UPDATE_CACHE(re, &s->gb);
+                SKIP_BITS(re, &s->gb, 6);
                 level = SHOW_SBITS(re, &s->gb, 12);
-                SKIP_BITS(re, &s->gb, 12);
+                LAST_SKIP_BITS(re, &s->gb, 12);
                 i += run;
                 j = scantable[i];
                 if (level < 0) {