View Single Post
Old 18th November 2013, 19:13   #12  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,316
Is this calculation correct ?
Code:
// VBV_Check.cpp*: définit le point d'entrée pour l'application console.
//

#include "stdafx.h"

const long double THRESHOLD_LOW=1000.0;

int _tmain(int argc, _TCHAR* argv[])
{
    FILE    *fp;
    uint8_t buffer[16384];
    int i,length;
    bool first=true;
	bool VBV_Underlfow=false,VBV_Low=false;
    uint32_t parse=0,picture_size=0;
	long double VBV_Buffer,VBV_Picture,VBV_Min;
	long double fps,VBV_Init,VBV_Bitrate;
	uint32_t FrameNumber=0,FrameMin=0;
	char *FileName;

    if (argc != 2) {
        fprintf(stderr, "Syntax : VBV_Check <file_name>\n");
        exit(-1);
    }

	length=0;
	while ((argv[1][length]!='\0') && (length<10000)) length++;
	if ((length==10000) || (length==0)) {printf("Error with the file name !\n"); exit(-1);}
	FileName=(char *)malloc(length+1);
	if (FileName==NULL) {printf("Allocation error !\n"); exit(-1);}
	for (i=0; i<length; i++)
		FileName[i]=(char)argv[1][i];
	FileName[length]='\0';

	VBV_Init=0.9;
	fps=23.976;
	VBV_Bitrate=24000000.0;

	VBV_Buffer=VBV_Init*VBV_Bitrate;
	VBV_Picture=VBV_Bitrate/fps;
	VBV_Min=VBV_Bitrate;

    /*--- open binary file (for parsing) ---*/
    fp=fopen(FileName,"rb");
    if (fp==0)
	{
        fprintf(stderr,"Unable to open file <%s>\n",FileName);
		free(FileName);
        exit(-1);
    }
	free(FileName);

    while(!feof(fp))
	{
        length=fread(&buffer[0],1,16384,fp);
        for(i=0; i<length; i++)
		{
            parse=(parse<<8)+buffer[i];
            if ((parse & 0xffffff00)==0x00000100)
			{    /* look for a start code */
				uint32_t detect_start=parse & 0xffffff9f;

                if ( (detect_start==0x00000101) || (detect_start==0x00000105) )
				{    /* look for a coded slice */
					if (i<(length-1)) i++;
					else
					{
						if (!feof(fp))
						{
							length=fread(&buffer[0],1,16384,fp);
							i=0;
						}
						else
						{
							fclose(fp);
							printf("\nMin value of VBV Buffer : %f at frame : %lu\n",VBV_Min,FrameMin);
							return 0;
						}
					}
                    picture_size++;
                    parse=(parse << 8)+buffer[i];
                    if (parse & 0x00000080)
					{    /* make sure it's the first slice of a picture */
                        if (first==true)
						{
                            first=false;
                            picture_size=0;
                        }
                        else
						{
							picture_size<<=3;
                            printf("Frame Number : %lu    Size : %lu",FrameNumber,picture_size);
							printf("   VBV Buffer : %lu/",(uint32_t)floor(VBV_Buffer));
							VBV_Buffer-=picture_size;
							printf("%lu/",(uint32_t)floor(VBV_Buffer));
							if (VBV_Buffer<VBV_Min)
							{
								VBV_Min=VBV_Buffer;
								FrameMin=FrameNumber;
							}
							if (VBV_Buffer<=0.0)
							{
								VBV_Buffer=0.0;
								VBV_Underlfow=true;
							}
							else if (VBV_Buffer<=THRESHOLD_LOW) VBV_Low=true;
							VBV_Buffer+=VBV_Picture;
							if (VBV_Underlfow)
							{
								VBV_Underlfow=false;
								printf("%lu  UNDERFLOW !!!!!!!!!!!!!!!!!!\n",(uint32_t)floor(VBV_Buffer));
							}
							else if (VBV_Low)
							{
								VBV_Low=false;
								printf("%lu  LOW !!!\n",(uint32_t)floor(VBV_Buffer));
							}
							else printf("%lu\n",(uint32_t)floor(VBV_Buffer));
							if (VBV_Buffer>VBV_Bitrate) VBV_Buffer=VBV_Bitrate;
                            picture_size=0;
							FrameNumber++;
                        }
                    }
                }
            }
            picture_size++;
        }
    }
    fclose(fp);
	printf("\nMin value of VBV Buffer : %f at frame : %lu\n",VBV_Min,FrameMin);
    return 0;
}
When muxing for BR with Scenarist, should i remove only picture size or something else also ?
Because a stream i've muxed in Scenarist generated a buffer underflow, but according this calcul method, my min buffer value is 5228639 (Buffer and video bitrate at 24000kb).
But... I indeed find the position of this min value at the position the stream failed in Scenarist.
But this value is... far away from 0, so, either my program is doing something wrong, either... There is something i've misunderstood...
File is 23.976fps viodeo encoded with x264 with --vbv-maxrate 24000 --vbv-bufsize 24000.
According x264 help, default value of init buffer is 0.9 if you don't change this parameter.

Last edited by jpsdr; 18th November 2013 at 19:17.
jpsdr is offline   Reply With Quote