JeffyCN / libv4l-rkmpp

A rockchip-mpp V4L2 wrapper plugin for chromium V4L2 VDA/VEA
GNU Lesser General Public License v2.1
79 stars 26 forks source link

Can't play av1 video with big bit rate #12

Closed amazingfate closed 1 year ago

amazingfate commented 1 year ago

I can play this 20MB video. But when playing this 30MB video, chromium gets stuck and after a long time it falls back to dav1d decoder(software decoder). This video is played well with ffmpeg and gstreamer.

JeffyCN commented 1 year ago

looks like it crashed in mpp's put_packet:

[075.905] [RKMPP] [6218] rkmpp_put_packets(116): ctx(0x4c04302d80): ENTER
[6116:6116:0101/203116.082089:ERROR:gpu_process_host.cc(991)] GPU process exited unexpectedly: exit_code=5
JeffyCN commented 1 year ago

it seems like an mpp issue, i'll report to mpp team:

+++ b/mpp/codec/dec/av1/av1d_parser.c
@@ -1158,7 +1158,7 @@ MPP_RET av1d_get_frame_stream(Av1CodecContext *ctx, RK_U8 *buf, RK_S32 length)
     if ((length + offset) > size) {
         mpp_packet_deinit(&ctx->pkt);
         buff_size = length + offset + 10 * 1024;
-        data = mpp_realloc(data, RK_U8, buff_size);
+        data = mpp_malloc(RK_U8, buff_size);
JeffyCN commented 1 year ago

after a few tests, it turns out to be a chromium issue.

the chromium has a shim allocator to override the libc's memory apis(malloc/realloc/posix_memalign) 1/ the mpp's av1 dec would try to do posix_memalign in one thread and realloc in another thread, which would crash/hang in chromium's shim allocator's realloc api

mpp/codec/dec/av1/av1d_api.c
    buf = mpp_malloc(RK_U8, size); <--posix_memalign

mpp/codec/dec/av1/av1d_parser.c
        data = mpp_realloc(data, RK_U8, buff_size); <--realloc

2/ posix_memalign and realloc in the same thread works (add mpp_free and mpp_alloc before mpp_realloc) 3/ malloc and realloc works (change posix_memalign to malloc)

so: 1/ please report to google 2/ try this hack:

+++ b/osal/linux/os_mem.c
@@ -20,7 +20,9 @@

 int os_malloc(void **memptr, size_t alignment, size_t size)
 {
-    return posix_memalign(memptr, alignment, size);
+    *memptr = malloc(size);
+    return 0;
+    //return posix_memalign(memptr, alignment, size);
 }

3/ try to disable shim partition allocator in chromium: set use_partition_alloc to false

JeffyCN commented 1 year ago

so the chromium's shim partition allocator is broken in this kind of cases: 1/ aligned memory allocation with size=512K and alignment>=32 2/ realloc it in other thread

another workaround:

+++ b/mpp/codec/dec/av1/av1d_api.c
@@ -56,7 +56,7 @@ MPP_RET av1d_init(void *ctx, ParserCfg *init)
     if ((ret = av1d_split_init(av1_ctx)) != MPP_OK)
         goto _err_exit;

-    buf = mpp_malloc(RK_U8, size);
+    buf = mpp_malloc(RK_U8, size * 2);

another test case:

+++ b/src/libv4l-rkmpp-dec.c
@@ -15,6 +15,8 @@
 #include <signal.h>

 #include "libv4l-rkmpp-dec.h"
+#include <malloc.h>
+void *dummy_buf = NULL;

@@ -722,6 +726,8 @@ void *rkmpp_dec_init(struct rkmpp_context *ctx)
        struct rkmpp_dec_context *dec;
        MPP_RET ret;

+       dummy_buf = memalign(32, 512*1024);
+
        ENTER();

@@ -256,6 +258,8 @@ static void *decoder_thread_fn(void *data)
        MPP_RET ret;
        int index;

+       dummy_buf = realloc(dummy_buf, 1000*1024); <--- crash here
+
        ENTER();

anyway, please report to chromium issue system.

amazingfate commented 1 year ago

This issue has been reported upstream: https://bugs.chromium.org/p/chromium/issues/detail?id=1418880