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 January 2011, 16:15   #1  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
How compute m2ts result files size ?

If i have the following :
A raw h264 video file of size V1.
2 audio files of sizes A1 and A2.
Does someone know the formula wich will give me, with an accuracy of around 1MB for files of around 5GB, the size of the resulting m2ts muxed file ? (Blu-Ray authoring).
If others parameters like bitrate/frame are to be used, notify me.

Thanks.
jpsdr is offline   Reply With Quote
Old 1st January 2011, 17:05   #2  |  Link
Groucho2004
 
Join Date: Mar 2006
Location: Barcelona
Posts: 5,034
Check out the source code from Selur's sx264 GUI. His calculations for M2TS overhead are pretty accurate.
Groucho2004 is offline   Reply With Quote
Old 1st January 2011, 17:32   #3  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,259
here's what I use in Hybrid atm:
Code:
qint64 CalculationService::m2tsVideoOverhead(const qint64 completeVideo, const int frameCount, \
                                                                           const bool audioHandling)
{
    qint64 videoOverhead = 0;
    qint64 videoSize = completeVideo; //videoOverhead+generalOverhead+rawVideo
    if (!audioHandling) {
        videoOverhead = frameCount*263 + 9412;
    }
    videoSize -= videoOverhead;
    int packageSize = 192;
    int packageFill = 184; //of 192
    sendMessage(MYDEBUG, "packageFill", packageFill);
    qint64 packageCount = videoSize/packageSize+1;
    qint64 packageOverhead = packageCount*(packageSize-packageFill);
    videoOverhead += packageOverhead;
    return videoOverhead;
}
qint64 CalculationService::m2tsAudioOverhead(const bool videohandling, const int frameCount, const double framerate, const QString format, const qint64 rawsize)
{
    int blocksize = 80;
    if (format == "ac3" || format.contains("true") || format == "dts"
     || format == "dts-hd" || format == "pcm") {
        blocksize = 907;
    }
    qint64 audioOverhead = qint64((2*blocksize)/180.0 * 8.0  + 17.0 +0.5) * ((qint64)(rawsize*1.0/(blocksize*1.0)+0.5));
    if (videohandling) {
        double length = frameCount/framerate;
        double audiobitrate = rawsize/length;
        audioOverhead += frameCount*(262+(audiobitrate/184+1)) + 9412;
    }
    return audioOverhead;
}
If you find a better way to calculate the overhead please share.
__________________
Hybrid here in the forum, homepage

Last edited by Selur; 1st January 2011 at 17:35.
Selur is offline   Reply With Quote
Old 2nd January 2011, 11:42   #4  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
Thanks for answers. I have no idea of how calculate them, it's why i'm asking.
I need it for using in oposite way : Knowing the size (or bitrate) of my audio and knowing my final avaible size, i need to know how exactly (or as precise as possible) how much data will be avaible for my raw video (=> what bitrate i can use).

@Selar : What unit is the result size ?

Last edited by jpsdr; 2nd January 2011 at 11:54.
jpsdr is offline   Reply With Quote
Old 2nd January 2011, 12:50   #5  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,259
Quote:
What unit is the result size ?
byte

Quote:
Knowing the size (or bitrate) of my audio and knowing my final avaible size, i need to know how exactly (or as precise as possible) how much data will be avaible for my raw video (=> what bitrate i can use).
Sorry, but if you got a function that helps you how to calculate audio&video overhead, calculating the size of the bitrate available for a given size isn't hard,...
sx264 and Hybrid both do it -> sx264 source code is unorganized, but iirc the X264Model::setBitrateForSizemethodis the method that does exactly what you want with the methods i quoted,..

Cu Selur
__________________
Hybrid here in the forum, homepage
Selur is offline   Reply With Quote
Old 16th January 2011, 16:41   #6  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
I'm having trouble...
Note : 1k=1e3 1M=1e6 b=bit B=Byte.
I have a video of 145037 frames at 23.976fps.
Video bitrate is 5922kb, audio bitrate is 448kb (AC3).
According the formulas, result file should be around 5122MB, with audio header around 119MB and video header around 187MB.
Problem is that my result m2ts file with Scenarist is around 5111MB, so, 11MB smaller than expected !

Edit : Maybe i'm doing something wrong, but i don't see what...

Last edited by jpsdr; 16th January 2011 at 20:25.
jpsdr is offline   Reply With Quote
Old 16th January 2011, 17:11   #7  |  Link
poisondeathray
Registered User
 
Join Date: Sep 2007
Posts: 5,346
you can try this tool (from avchdcoder author)

http://tools.twanwintjes.nl/index.php?page=avchdcalc
poisondeathray is offline   Reply With Quote
Old 16th January 2011, 20:23   #8  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
Nice tool.
Unfortunately, not fitted to my needs, as i often have severals m2ts files in my result Blu-Ray. Would have been perfect if i have only one m2ts file in result, but it's not always the case.

@selur
About the code you've provided, if there is several audio stream, do i need to add all the overhead audio of each stream, or is there something different again ?

Last edited by jpsdr; 16th January 2011 at 20:30.
jpsdr is offline   Reply With Quote
Old 16th January 2011, 20:39   #9  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,259
@poisondeathray: afaik AVCHDCalculator is a. closed source and b. doesn't allow custom target size.
But it would be interesting to know which formulas were used.

Quote:
Problem is that my result m2ts file with Scenarist is around 5111MB, so, 11MB smaller than expected !
better 11MB to small, than 11MB to large
Sorry, should have posted that I know that the calculation isn't the best (most accurate) and everyone is welcome to post a tweak and correct it, it's the best I came up with the data I had. And at least I never encountered a problem with it predicting the file size to low or being really far of. (11MB on 5022MB doesn't seem that bad, to me since it's mainly educated guessing )

With multiaudio the formula will be more off. (assuming to large audio overhead)
Since I personally normally don't encounter multi audio I never spend much time collecting statistical data and thinking about what happens when multi audio is present.
(I was totally happy when I found a way to calculate single audio&video )


Cu Selur
__________________
Hybrid here in the forum, homepage

Last edited by Selur; 16th January 2011 at 20:43.
Selur is offline   Reply With Quote
Old 16th January 2011, 23:05   #10  |  Link
Groucho2004
 
Join Date: Mar 2006
Location: Barcelona
Posts: 5,034
Quote:
Originally Posted by Selur View Post
Sorry, should have posted that I know that the calculation isn't the best (most accurate) and everyone is welcome to post a tweak and correct it, it's the best I came up with the data I had.
We're talking about 0.2 % deviation here. I'd say it's more than adequate considering that one should always shoot for a target size 0.5% - 1% below the disk capacity.
Groucho2004 is offline   Reply With Quote
Old 21st January 2011, 00:33   #11  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
I've made some test, and have following results :
Video is 147084 frames at 23.976fps encoded at 5923.32kb.
Audio is AC3 at 448kb. Total overhead is 278286018 bytes.
Audio is AC3 at 192kb. Total overhead is 290556610 bytes.
Video is 119098 frames at 23.976fps encoded at 5921.61kb.
Audio is AC3 at 448kb. Total overhead is 225322715 bytes.
Audio is AC3 at 192kb. Total overhaed is 235254491 bytes.
When audio bitrate goes low, overhead grows.
Actualy, if bitrate audio b is in kilobit (1000 bits), for AC3 audio, one part of the calcul of audio overhead is :
(2000*(C-b)/256)*time, time is duration is seconds. C would be 640 will not surprise me !
Actualy, for my specific cases, with my video bitrate : (2000*(640-audiobitrate)/256)*time + 1830*frames gives a good overhead estimation. If some are interesting to search using this as a base search...
jpsdr is offline   Reply With Quote
Old 21st January 2011, 08:45   #12  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,259
Quote:
Video is 147084 frames at 23.976fps encoded at 5923.32kb.
Audio is AC3 at 448kb. Total overhead is 278286018 bytes.
Audio is AC3 at 192kb. Total overhead is 290556610 bytes.
Video is 119098 frames at 23.976fps encoded at 5921.61kb.
Audio is AC3 at 448kb. Total overhead is 225322715 bytes.
Audio is AC3 at 192kb. Total overhaed is 235254491 bytes.
In general when posting numbers it would be helpful to see the following infos:
1. raw video size (byte)
2. video frame count
3. raw audio size (byte)
4. average audio bitrate (bit/s)
5. length of video (assuming audio&video length is the same), video frame rate
6. size of video only muxed inside m2ts container (byte)
7. size of audio only muxed inside m2ts container (byte)
8. size of video&audio muxed together inside m2ts container (byte)
this would allow to use them as control values for later tests.

Quote:
Actualy, if bitrate audio b is in kilobit (1000 bits), for AC3 audio, one part of the calcul of audio overhead is :
(2000*(C-b)/256)*time, time is duration is seconds. C would be 640 will not surprise me !
Actualy, for my specific cases, with my video bitrate : (2000*(640-audiobitrate)/256)*time + 1830*frames gives a good overhead estimation. If some are interesting to search using this as a base search...
Here's why I'm reluctant to use this as a base for a search:
1. since the package size of m2ts is 192 it would kind of make sense to see it appear somewhere in the formula
2. some value that is assumed as average package fill would make sense
3. calculation video overhead by only looking at the frame count is definitly a dead end even if video bitrate is 1/5 of the current value, video overhead stays the same,..

video only overhead example:
video frame count: 109645 frames
video frame rate: 23.976 frames/second
video length: frame count/frame rate = 4573.115 seconds
raw video size: 8.472.672 Bytes
video inside m2ts: 37.502.976 Bytes
-> video overhead: 29.030.304 Bytes


(2000*(640-audiobitrate)/256)*time + 1830*frames would be way of:
(2000*(640-0)/256)*4573.115 + 1830*109645 = 5000*4573.115 + 1830*109645 = 22865575 + 1830*109645 = 22.865.575 + 200.650.350 = 223.515.925
I know you are probably aware that the video overhead formula wasn't good. I just wanted to show an extreme example,..

The main point is that a formula to calculate the overhead should not contain constants that fall from the heavens.

Sadly I lost nearly all my muxing data collection a year ago in a hdd crash, only thing I found was some data about a single video-only source I reencoded with different bitrates:
Quote:
m2ts - videoOnly

Framecount: 109645
Length: 109645/23,976 = 4573,1147814481147814481147814481

quant 51
37.502.976 Bytes inside container
8.472.672 Bytes raw
29.030.304 Bytes overhead

200
143.388.672 Bytes inside container
111.974.627 Bytes raw
31.414.045 Bytes overhead


300
200.534.016 Bytes inside container
166.799.049 Bytes raw
33.734.967 Bytes overhead


400
257.857.536 Bytes inside container
221.618.987 Bytes raw
36.238.549 Bytes overhead


500
315.033.600 Bytes inside container
276.435.819 Bytes raw
38.597.781 Bytes overhead

1000
601.055.232 Bytes inside container
550.501.276 Bytes raw
50.553.956 Bytes overhead

2000
1.173.006.336 Bytes inside container
1.098.623.746 Bytes raw
74.382.590 Bytes overhead
A side from the lag of suspected variables, a main problem with the audio overhead formula (2000*(C-b)/256)*time for me is that using C = 640 would result in no or negative overheads if the audio bitrate is 640+kbit/s.

Cu Selur

Ps.: aiming for a formula that only works if the target size is i.e. 10GB, DVD9, DVD5 or something like that is a thing that would probably be easier, but will probably not result in a all around working formula
__________________
Hybrid here in the forum, homepage

Last edited by Selur; 21st January 2011 at 08:48.
Selur is offline   Reply With Quote
Old 21st January 2011, 10:36   #13  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
As i said, it's for one AC3 audio track, so, in AC3 case, bitrate is not more than 640kb i think, and my actual formula is for now only with the video bitrate i've used.
I'll add later the others files size informations.
I'm currently doing a 10000kb video to try to have video track at different bitrate, to identify video parameter.
I'll also do some tests with 640kb AC3 audio track.
I'm not interested in audio or video only, because they may have different way of doing things, and Scenarist will probably not allow me a track without video stream.
jpsdr is offline   Reply With Quote
Old 21st January 2011, 18:16   #14  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
Video1 : 147084 frames @23.976fps raw h264 encoded (x264) at 5923.32kb. .264 file size : 4542170174 bytes.
Video2 : 119098 frames @23.976fps raw h264 encoded (x264) at 5921.61kb. .264 file size : 3676859941 bytes.
Audio 1.1 (for video 1) : AC3 encoded at 448kb. AC3 file size : 343538944 bytes.
Audio 1.2 (for video 1) : AC3 encoded at 192kb. AC3 file size : 147230976 bytes.
Audio 2.1 (for video 2) : AC3 encoded at 448kb. AC3 file size : 278170368 bytes.
Audio 2.2 (for video 2) : AC3 encoded at 192kb. AC3 file size : 119215872 bytes.

M2TS files :
(1) : Video1 + audio1.1 => 5163995136 bytes.
(1.1) : Video1 + 2xaudio1.1 => 5532076032 bytes.
(2) : Video2 + audio2.1 => 4180353024 bytes.
(2.1) : Video2 + 2xaudio2.1 => 4478392320 bytes.
(3) : Video1 + audio1.2 => 4979957760 bytes.
(3.1) : Video1 + 2xaudio1.2 => 5163995136 bytes.
(4) : Video2 + audio 2.2 => 4031330304 bytes.
(4.1) : Video2 + 2xaudio2.2 => 4180353024 bytes.

Last edited by jpsdr; 22nd January 2011 at 17:43.
jpsdr is offline   Reply With Quote
Old 22nd January 2011, 09:31   #15  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
Video3 : Video1 encoded at 9971,98kb. .264 file size : 7646802893 bytes.
Video4 : Video2 encoded at 9998,64kb. .264 file size : 6208381775 bytes.
Audio 1.3 (for video1) : AC3 encoded at 640kb. AC3 file size : 490769920 bytes.
Audio 2.3 (for video2) : AC3 encoded at 640kb. AC3 file size : 397386240 bytes.

M2TS files :
(5) : Video3 + audio 1.1 => 8403603456 bytes.
(6) : Video4 + audio 2.1 => 6821910528 bytes.
(7) : Video1 + audio 1.3 => 5348038656 bytes.
(8) : Video2 + audio 2.3 => 4329375744 bytes.

Now, i think there is globaly enough relevant data to create a good estimation of video/audio overhead in case of AC3 audio file.

Last edited by jpsdr; 22nd January 2011 at 17:44.
jpsdr is offline   Reply With Quote
Old 22nd January 2011, 17:42   #16  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
For now, i think, for video + audio :
Video overhead is rawvideosize/23 (or rawvideosize/23 + constant).
For at least 448kb and 192kb, audio overhead is : "something"-rawaudiosize/16.
Still not found what this "something" is for audio.
For 192kb and 448kb, a working formula is (1000*(2134-bitrate)/128)*time, but it's only considering if the "something" is directly linked to the time.
jpsdr is offline   Reply With Quote
Old 22nd January 2011, 20:38   #17  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
According the data, i have for now the following formulas :
Video overhead = rawvideosize/23
Audio :
AC3 192kb & 448kb : K=696*frames (or K=29*fps*frames, i don't know, only tested with 23.976fps video)
Audio overhead = K-rawaudiosize/16. I don't know for which bitrate value formula switch.
AC3 640kb : Audio overhead=800*frames.

Last edited by jpsdr; 22nd January 2011 at 20:40.
jpsdr is offline   Reply With Quote
Old 22nd January 2011, 20:58   #18  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,259
may be also worth a try:
Code:
qint64 CalculationService::m2tsVideoOverhead(const qint64 completeVideo, const int frameCount, \
                                                                           const bool audioHandling)
{
    qint64 videoOverhead = 0;
    qint64 videoSize = completeVideo; //videoOverhead+generalOverhead+rawVideo
    if (!audioHandling) {
        videoOverhead = frameCount*263 + 9412;
    }
    videoSize -= videoOverhead;
    int packageSize = 192;
    int packageFill = 185; //of 192
    sendMessage(MYDEBUG, "packageFill", packageFill);
    qint64 packageCount = videoSize/packageFill;
    qint64 packageOverhead = packageCount*(packageSize-packageFill);
    videoOverhead += packageOverhead;
    return videoOverhead;
}

qint64 CalculationService::m2tsAudioOverhead(const bool videohandling, const int frameCount, const double framerate, const QString format, const qint64 rawsize)
{
    int blocksize = 80;
    if (format == "ac3" || format.contains("true") || format == "dts"
     || format == "dts-hd" || format == "pcm") {
        blocksize = 907;
    }
	double packageSize = 192.0;
	double packageFill = 182.0;
    qint64 audioOverhead = qint64((2*blocksize)/packageFill * 8.0  + 17.0 +0.5) \
                                                      * ((qint64)(rawsize*1.0/(blocksize*1.0)+0.5));
    if (videohandling) {
        double length = frameCount/framerate;
        double audiobitrate = rawsize/length;
        audioOverhead += frameCount*(packagesize+(audiobitrate/packageFill+1)) + 9412;
    }
    return audioOverhead;
}
__________________
Hybrid here in the forum, homepage
Selur is offline   Reply With Quote
Old 23rd January 2011, 08:28   #19  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
I've allready try your formulas you give in the post #3, error is too big for me... Has i said, your formulas give around 11MB over a stream with subtitles ! Wich indeed result of around 30MB too much for a stream (subtitles was around 20MB), resulting in 360MB error for 12 streams (my need). Way too much out of the league for me, i'm looking for error <1MB (by stream) !
jpsdr is offline   Reply With Quote
Old 23rd January 2011, 08:40   #20  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,259
You did notice it's a modified version in the last post didn't you?
__________________
Hybrid here in the forum, homepage
Selur is offline   Reply With Quote
Reply

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 19:01.


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