[FFmpeg-devel,1/5] startcode: Use common macro

Submitted by Andreas Rheinhardt on June 1, 2019, 10:47 p.m.

Details

Message ID 20190601224719.32872-2-andreas.rheinhardt@gmail.com
State New
Headers show

Commit Message

Andreas Rheinhardt June 1, 2019, 10:47 p.m.
The reasons are cosmetics and preparation for future patches that will
have even more cases.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/startcode.c | 37 +++++++++++++++++++------------------
 1 file changed, 19 insertions(+), 18 deletions(-)

Comments

Michael Niedermayer June 3, 2019, 9:30 p.m.
On Sun, Jun 02, 2019 at 12:47:15AM +0200, Andreas Rheinhardt wrote:
> The reasons are cosmetics and preparation for future patches that will
> have even more cases.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavcodec/startcode.c | 37 +++++++++++++++++++------------------
>  1 file changed, 19 insertions(+), 18 deletions(-)

This is not the same code, the way memory is read changes, 
the index is changed to a pointer. 
Why are these changes done ? And if they are done, they should be
benchmarked

Thanks
Andreas Rheinhardt June 3, 2019, 10:32 p.m.
Michael Niedermayer:
> On Sun, Jun 02, 2019 at 12:47:15AM +0200, Andreas Rheinhardt wrote:
>> The reasons are cosmetics and preparation for future patches that will
>> have even more cases.
>>
>> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
>> ---
>>  libavcodec/startcode.c | 37 +++++++++++++++++++------------------
>>  1 file changed, 19 insertions(+), 18 deletions(-)
> 
> This is not the same code, the way memory is read changes, 
> the index is changed to a pointer. 
> Why are these changes done ? And if they are done, they should be
> benchmarked
> 
> Thanks
> 
I'm sorry. I remember actually doing benchmarks of an earlier version
[1] which suddenly was quite faster after switching from index to
pointer, so I simply changed it. I must have forgotten to document it
in the commit message.

- Andreas

[1]: https://github.com/mkver/FFmpeg/commits/start_2
Warning: Very raw. IIRC the whole unaligned stuff does not yet work
for it and it definitely doesn't work on BE.

Patch hide | download patch | download mbox

diff --git a/libavcodec/startcode.c b/libavcodec/startcode.c
index 9efdffe8c6..a55a8fafa6 100644
--- a/libavcodec/startcode.c
+++ b/libavcodec/startcode.c
@@ -27,31 +27,32 @@ 
 
 #include "startcode.h"
 #include "config.h"
+#include "libavutil/intreadwrite.h"
 
 int ff_startcode_find_candidate_c(const uint8_t *buf, int size)
 {
-    int i = 0;
+    const uint8_t *start = buf, *end = buf + size;
+
 #if HAVE_FAST_UNALIGNED
-    /* we check i < size instead of i + 3 / 7 because it is
-     * simpler and there must be AV_INPUT_BUFFER_PADDING_SIZE
-     * bytes at the end.
-     */
+#define READ(bitness) AV_RN ## bitness
+#define MAIN_LOOP(bitness, mask1, mask2) do {                              \
+        /* we check p < end instead of p + 3 / 7 because it is
+         * simpler and there must be AV_INPUT_BUFFER_PADDING_SIZE
+         * bytes at the end. */                                            \
+        for (; buf < end; buf += bitness / 8)                              \
+            if ((~READ(bitness)(buf) & (READ(bitness)(buf) - mask1))       \
+                                     & mask2)                              \
+                break;                                                     \
+    } while (0)
+
 #if HAVE_FAST_64BIT
-    while (i < size &&
-            !((~*(const uint64_t *)(buf + i) &
-                    (*(const uint64_t *)(buf + i) - 0x0101010101010101ULL)) &
-                    0x8080808080808080ULL))
-        i += 8;
+    MAIN_LOOP(64, 0x0101010101010101ULL, 0x8080808080808080ULL);
 #else
-    while (i < size &&
-            !((~*(const uint32_t *)(buf + i) &
-                    (*(const uint32_t *)(buf + i) - 0x01010101U)) &
-                    0x80808080U))
-        i += 4;
+    MAIN_LOOP(32, 0x01010101U, 0x80808080U);
 #endif
 #endif
-    for (; i < size; i++)
-        if (!buf[i])
+    for (; buf < end; buf++)
+        if (!*buf)
             break;
-    return i;
+    return buf - start;
 }