Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Video Encoding > MPEG-4 AVC / H.264

Reply
 
Thread Tools Search this Thread Display Modes
Old 1st November 2024, 18:17   #1  |  Link
hellgauss
Registered User
 
hellgauss's Avatar
 
Join Date: Sep 2002
Location: Italy
Posts: 108
h264qp - A script to analyze h264 qp at macroblocks level via ffmpeg and ffprobe

I wrote a small cpp script to analyze qp values at macroblocks level via ffmpeg and ffprobe, organized per frame type ( K/I , P , B ). Results are in agreement with the x264 log at the end of encoding (sometimes average framesize is a little bit different). The motivation is to provide the same info on QP as the well known avinaptic, but with more accuracy.

This is an example of output

Code:
TOT:
N               : 109201
N/TOT           : 100.000000 %
Total Size      : 1137524780
Total Size/TOT  : 100.000000 %
Average Size    : 10416.798198
AVERAGE QP      : 28.333866
StdDev (frames) : 4.429616
StdDev (MB)     : 6.219143

B:
N               : 79928
N/TOT           : 73.193469 %
Total Size      : 310997300
Total Size/TOT  : 27.339826 %
Average Size    : 3890.968121
AVERAGE QP      : 28.844389
StdDev (frames) : 4.685202
StdDev (MB)     : 6.422568

P:
N               : 28799
N/TOT           : 26.372469 %
Total Size      : 776058630
Total Size/TOT  : 68.223448 %
Average Size    : 26947.415882
AVERAGE QP      : 27.026055
StdDev (frames) : 3.186714
StdDev (MB)     : 5.347749

K+I:
N               : 474
N/TOT           : 0.434062 %
Total Size      : 50468850
Total Size/TOT  : 4.436725 %
Average Size    : 106474.367089
AVERAGE QP      : 21.706530
StdDev (frames) : 3.186385
StdDev (MB)     : 5.063920

K:
N               : 450
N/TOT           : 0.412084 %
Total Size      : 48814460
Total Size/TOT  : 4.291288 %
Average Size    : 108476.577778
AVERAGE QP      : 21.804706
StdDev (frames) : 3.079572
StdDev (MB)     : 5.008669

I:
N               : 24
N/TOT           : 0.021978 %
Total Size      : 1654390
Total Size/TOT  : 0.145438 %
Average Size    : 68932.916667
AVERAGE QP      : 19.865722
StdDev (frames) : 4.374115
StdDev (MB)     : 5.701641

P+B:
N               : 108727
N/TOT           : 99.565938 %
Total Size      : 1087055930
Total Size/TOT  : 95.563275 %
Average Size    : 9998.031124
AVERAGE QP      : 28.362758
StdDev (frames) : 4.412535
StdDev (MB)     : 6.208241

Consecutive B-Frames (number of B-Frames in each group) :
1 : 2151 ( 2.691172 % )
2 : 3404 ( 4.258833 % )
3 : 74373 ( 93.049995 % )
Optionally it also outputs detailed per-frame statistisc as follows.
First row = total number of frames
Then, for each frame, the following are reported
[Frame Type] [Frame Size] [Frame position] [Average QP of the frame] [Macroblock QP StdDev of the frame]
Code:
109201
K	99565	0	17.639706	5.942646
B	81	100109	17.125000	1.452369
B	89	100020	17.477451	2.297053
B	72	100190	20.386029	2.292464
P	455	99565	20.032475	4.160982
B	73	103057	19.386029	2.292464
B	76	103130	17.248162	1.913333
P	2795	100262	23.354657	5.087658
B	92	103528	17.984069	2.674038
B	109	103419	18.191912	3.457931
B	66	103620	33.000000	0.000000
P	213	103206	18.170956	3.413506
B	73	104318	31.788603	1.768125
P	632	103686	20.997917	4.652969
I	108528	104391	19.261397	4.447370
B	67	213822	25.000000	0.000000
B	88	213734	21.337010	1.027634
B	67	213889	25.000000	0.000000
P	815	212919	19.608701	2.853781
B	69	215874	23.000000	0.000000
B	70	215943	18.012745	0.225428
P	1918	213956	25.711029	4.187233
B	83	216932	20.799877	0.821151
P	919	216013	24.768873	5.223346
B	87	217555	22.476348	1.807915
P	540	217015	25.545711	4.772148
I	111822	217642	22.179657	4.503248
B	67	373128	29.000000	0.000000
B	13819	359309	30.065196	5.149497
...etc...
The script basically launches the command

Quote:
ffmpeg -hide_banner -loglevel error -threads 1 -i inputFile -map 0:V:0 -c:v copy -f h264 pipe: | ffprobe -threads 1 -v quiet -show_frames -show_streams -show_entries frame=key_frame,pkt_pos,pkt_size,pict_type -debug qp -i pipe:
and parses stdout and stderr. More info in the readme.txt

It consists in only one .cpp file so it can be easily be compiled. E.g. in Embarcadero Dev-ccp it only requires the flag -std=c++17 to be added to compiler options. The script is provided "as is" without any warranty, fell free to copy, modify and distribute it.

EDIT: just created my first page on github
https://github.com/HG3112/h264qp

Last edited by tebasuna51; 3rd November 2024 at 11:05. Reason: I created a github page
hellgauss is offline   Reply With Quote
Old 9th November 2024, 13:47   #2  |  Link
tormento
Acid fr0g
 
tormento's Avatar
 
Join Date: May 2002
Location: Italy
Posts: 2,823
Please evaluate to create a similar one for hevc.
__________________
@turment on Telegram
tormento is offline   Reply With Quote
Old 9th November 2024, 19:39   #3  |  Link
hellgauss
Registered User
 
hellgauss's Avatar
 
Join Date: Sep 2002
Location: Italy
Posts: 108
That's not so easy. Unfortunately the debug qp flag does not work for hevc decoder, and it will probably not be implemented.

This is a 7 years old "wish" ffmpeg ticket

https://trac.ffmpeg.org/ticket/6637

I managed to parse per-slice (per-frame) qp informations on hevc stream using "-bsf:v trace_headers" trying to grab approximated info. It works well for videos coded at constant quantizer with "--qp" x265 options. However, with --crf encodings it gives completely wrong results. I assume that hevc can store a lot of informations at macroblocks level to optimize encoding.

Another approach I tried to follow is the usage of "videoparse.dll" from this repository

https://github.com/Telecommunication...e3_videoparser

However I've not been able to compile it under windows, and it seems that it is a challenge even for skilled programmers. I only have some little experience using Visual Studio Community Edition, I'm not a Software Developer.
hellgauss is offline   Reply With Quote
Reply

Tags
analysis, drf, macroblocks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:58.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.