Support storage encoding AV1

Anything you want added or changed in future versions of ZoneMinder? Post here and there's a chance it will get in! Search to make sure it hasn't already been requested.
Post Reply
exuvo
Posts: 8
Joined: Thu Feb 17, 2022 12:57 pm

Support storage encoding AV1

Post by exuvo »

As almost no browsers support native H265 (https://caniuse.com/hevc) i am unable to view H265 stored camera footage via zm web interface (from events). I am currently re-encoding my footage to H264 as that is supported in my browsers (firefox, chromium) but it takes more disk space.
All browsers support AV1 (https://caniuse.com/av1), and therefore i would like to encode my stored camera footage as AV1 instead so that i can view that via zm web interface while keeping disk usage low.

Seeing as zm already has support for multiple encoders i assume this would not be too difficult to add and i am willing to do the work myself but i would need some pointers for where to look in the zoneminder code. Ffmpeg supports AV1 via libsvtav1.

It looks like i need to add a line for AV1 to VideoStore::codec_data in https://github.com/ZoneMinder/zoneminde ... ostore.cpp. with:
"{ AV_CODEC_ID_AV1, "av1", "libsvtav1", AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P, AV_HWDEVICE_TYPE_NONE },"

I think i also need to add it to web/skins/classic/views/monitor.php videowriter_codecs array with value 226 for AV_CODEC_ID_AV1 if i am counting right, and libsvtav1 to videowriter_encoders. And the graying out code in web/skins/classic/views/js/monitor.js for encoder_dropdown line 180.

Is there anything else that would need more changes too? It looked to me like event playback just gave a raw copy of the file to the browser but maybe i missed something.
User avatar
iconnor
Posts: 2879
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: Support storage encoding AV1

Post by iconnor »

That about covers it.

Please note that my ubuntu impish system ffmpeg 4.4 has libaom-av1 as the encoder.
exuvo
Posts: 8
Joined: Thu Feb 17, 2022 12:57 pm

Re: Support storage encoding AV1

Post by exuvo »

Good to know i did not miss anything.

libsvtav1 is faster than libaom-av1 but it seems like libsvtav1 is broken in ffmpeg 4.4.1 so i might use libaom-av1 too. After compiling ffmpeg 5.0 from source they seem to have fixed it but they did some minor changes to the api http://git.videolan.org/?p=ffmpeg.git;a ... 37863e2b46 so now zoneminder does not compile until i fix that too.

-edit-
Yeah no they changed more than just const-ness of structs. I'll just use libaom-av1 for now with ffmpeg 4.4.1.
exuvo
Posts: 8
Joined: Thu Feb 17, 2022 12:57 pm

Re: Support storage encoding AV1

Post by exuvo »

libaom-av1 is horribly slow. Converting a 2MB camera file took 17 seconds with libaom-av1 vs 3 seconds with libsvtav1. I will continue my effort on adapting the code to ffmpeg 5.

Commands used:
crf 40
17s 350kb: time ffmpeg -i 157-video.mp4 -c:v libaom-av1 -g 150 -crf 40 -b:v 0 -cpu-used 8 -y test.mkv
3s 382kb: time ffmpeg -i 157-video.mp4 -c:v libsvtav1 -g 150 -rc cqp -qp 40 -preset 8 -y test.mkv
crf 30
31s 631kb: time ffmpeg -i 157-video.mp4 -c:v libaom-av1 -g 150 -crf 30 -b:v 0 -cpu-used 8 -y test.mkv
3.5s 762kb: time ffmpeg -i 157-video.mp4 -c:v libsvtav1 -g 150 -rc cqp -qp 30 -preset 8 -pix_fmt yuv420p10le -y test.mkv

libaom-av1 compresses slightly better but at a massive time cost compared to libsvtav1.

-edit-
I should have based this work on master instead of 1.36.12 as i see some of ffmpeg 5 stuff is already fixed there >.<
User avatar
iconnor
Posts: 2879
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: Support storage encoding AV1

Post by iconnor »

You should add them both in videostore.cpp as either one may be present.

1.36.13 will contain the ffmpeg fixes, they are trivial.

Still your work should be based on master as it is unlikely to get into 1.36.

Yeah even h265 is prohibitively expensive in terms of cpu. av1 is going to be worse. gpu should help eventually.
exuvo
Posts: 8
Joined: Thu Feb 17, 2022 12:57 pm

Re: Support storage encoding AV1

Post by exuvo »

I disagree the ffmpeg fixes are trivial it is not just the const changes, master does not build with ffmpeg 5.0 either.

I am not adding libaom, it is 5-10x as slow for 10% gain.
I am however adding vp9 too as that is also supported on all browsers and decently fast too.
exuvo
Posts: 8
Joined: Thu Feb 17, 2022 12:57 pm

Re: Support storage encoding AV1

Post by exuvo »

I got the master branch building with ffmpeg 5 now (crudely) https://github.com/exuvo/zoneminder/commits/master.
vp9 works well but av1 is giving me some error. For both however i have the problem that the web part is still sending metadata to the webbrowser as if the file was mp4 which it no longer is. Need to figure out where to change that.
exuvo
Posts: 8
Joined: Thu Feb 17, 2022 12:57 pm

Re: Support storage encoding AV1

Post by exuvo »

VP9 is working now for me in the browser. I just left OutputContainer column in Monitors empty which defaults to mp4 and works. The video file is being named 123-video.vp9.mp4 but that does not really matter.

Video controls are not working but that i don't need that, i can just refresh to restart the current event from the beginning or access the file directly via the network if i need any frame by frame things.

Encoder set to libvpx-vp9 and encoding parameters crf=30.

I have yet to update to master but this is the only commit needed https://github.com/exuvo/zoneminder/com ... 32abf95ebd .
If i get more free time and energy i will test more with av1.
Last edited by exuvo on Mon May 30, 2022 11:52 pm, edited 1 time in total.
exuvo
Posts: 8
Joined: Thu Feb 17, 2022 12:57 pm

Re: Support storage encoding AV1

Post by exuvo »

Oh i see my change is already in the master now. I must have missed when that happened.
exuvo
Posts: 8
Joined: Thu Feb 17, 2022 12:57 pm

Re: Support storage encoding AV1

Post by exuvo »

I just built from master and now the video player controls are working but zmc crashes when it tries to record :(
User avatar
iconnor
Posts: 2879
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: Support storage encoding AV1

Post by iconnor »

How about posting some logs?

We can get this working, but I can't do it all on my own, and neither can you..
Post Reply