随机数 随机丢包

来源:互联网 发布:校园网用户mac地址错误 编辑:程序博客网 时间:2024/05/29 07:57

1.随机数
rand()函数, srand()函数

rand():会返回范围在0至RAND_MAX 间的随机数;
rand()%x:会返回范围在0至x-1间的随机数;

srand(seed)设好随机数种子(seed);
srand((unsigned) time(NULL));
srand(unsigned(time(0)));

x = rand()%11; //产生1~10之间的随机整数
y = rand()%51 - 25; //产生-25 ~ 25之间的随机整数
z = ((double)rand()/RAND_MAX)*(b-a) + a; //产生区间[a,b]上的随机数

#include <iostream>#include <fstream>#include <stdio.h>#include <stdlib.h>#include <time.h> using namespace std;void main(){    ofstream out("E:\\1.txt");    for (int i=0;i<100;i++)    {        out<<1;    }    srand(unsigned(time(NULL)));    int j1;    for (int i=0;i<5;i++)    {        j1=rand()%101;        cout<<j1<<" ";    }}

2.随机丢包
因为在做整帧丢失的恢复,在HTM11.0解码端的输入是stream.bit,需要丢包软件进行随机丢包,代码如下:

#include <stdio.h>#include <stdlib.h>#include <tchar.h>#include <math.h>#include <float.h>#include <ctype.h>#define OPTIONCHAR '-'#define MAXNALUNITSIZE 1000000static FILE *errfile, *infile, *outfile;static unsigned char nalu [MAXNALUNITSIZE];static int nalutypecountread[64], nalutypecountwrite[64];static int nalusread=0, nalusreadValid=0,naluswritten=0,nalusLost=0;bool var=0;void parsecmd (int ac, char *av[]);     int readnalu(unsigned char *buf);       void writenalu (unsigned char *buf, int size);      int nal_unit_type(unsigned char *buf);      int naluloss();                 int startcode();                                                                                    void cleanup();             int main(int argc, char* argv[]) //argv[1]是输入文件;argv[2]是输出文件;argv[3]是错误类型文件;{    int nalusize, nalutype, nalucount=0;    parsecmd (argc, argv);    while(1)     {        nalusize=readnalu(nalu);//每个NALU的大小        nalutype=nal_unit_type(nalu);//NALU的类型        nalucount++;//NALU的个数        if (nalusreadValid==0||(nalusreadValid+1)%6!=0||var==0)        {            writenalu(nalu, nalusize);        }        else//对应中间视点的纹理图        {        //  printf("%5d,%5d\n",nalusreadValid/5-1 ,nalusize);            var=0;            if (!naluloss())            {                writenalu(nalu, nalusize);            }            else            {                nalusLost++;                printf("POC=%d is discard\n",nalusreadValid/6);            }        }    }    fprintf(stderr, "You should never see this\n");    getchar();}int readnalu(unsigned char *buf){//将inflie中的数据读入到buf中    int c, pos=0;    // Check NAL unit starts with start code    if (!startcode())     {       // can be no startcode in file at this position or EOF        if (feof(infile)) //feof  用于返回值,来判断文件是否执行结束,若结束,则返回非零值        {            // EOF found at the position of the first octet of the start code.            // This is the correct position for an EOF in the bitstream, so clean up            // and exit successfully            cleanup();            exit(0);//exit(0)表示正常退出;exit(x)(x不为0)都表示异常退出;exit()会终止整个程序        }         else        {            fprintf (stderr, "Startcode expected at octet offset %d expected, exiting\n", ftell(infile));            exit(-1);        }    }    // read startcode   将起始码读入    if (fread(&buf[pos], 1, 4, infile)!=4) // fread()用来从infile中读取数据放入buf中,每次读取的数据大小为1byte,读取4次。Fread()会返回实际读取到的count数目,如果此值比参数count来得小,则代表可能读到了文件尾了或者有错误发生(前者几率大),这时必须用feof()或ferror()来决定发生什么情况。    {        fprintf (stderr, "cannot read startcode at octet offset %d or thereabouts, existing\n", ftell(infile));        exit(-1);    }    pos=pos+4;    // read NAL unit   将NALU数据读入    while (!startcode())     {        if ((c=getc(infile))==EOF) {break;}        buf[pos++]=(unsigned char)c;        if (pos>=MAXNALUNITSIZE)         {            fprintf (stderr, "Found NAL unit of more than %d bits, can't deal with that,\                             use slices :-) or recompile with larger NALUNITSIZE, exiting\n", MAXNALUNITSIZE);            exit(-1);        }       }    nalusread++;    nalutypecountread[nal_unit_type(buf)]++;    if (nal_unit_type(buf)!=32&&nal_unit_type(buf)!=33&&nal_unit_type(buf)!=34)    {        nalusreadValid++;        var=1;    }    return pos;}void writenalu(unsigned char *buf, int size) {    if (fwrite (buf, 1, size, outfile)!=size) //将buf中的数据写入outfile    {        fprintf(stderr, "Cannot write %d bytes to outfile, exiting\n", size);        exit(-1);    }    naluswritten++;    nalutypecountwrite[nal_unit_type(buf)]++;}int startcode() {//一般的NALU以0x000001开始,若NALU为VPS、SPS、PPS,则以若为开始0x00000001开始。。    //由于该函数丢失整帧,所以判断0x00000001,所以如果是起始码返回1;否则返回0;    int c0, c1, c2, c3;    if ((c0=fgetc(infile))==EOF)        return 0;    if (c0!=0)     {        if (fseek(infile, -1, SEEK_CUR)!=0)         {            fprintf (stderr, "fseek() fails in startcode() (0)\n");            exit(-1);        }        return 0;   // not a startcode    }    if ((c1=fgetc(infile))==EOF)        return 0;    if (c1!=0)     {        if (fseek(infile, -2, SEEK_CUR)!=0)         {            fprintf (stderr, "fseek() fails in startcode() (1)\n");            exit(-1);        }        return 0;   // not a startcode    }    if ((c2=fgetc(infile))==EOF)        return 0;    if ((c2!=0)&&(c2!=0x01))     {        if (fseek(infile, -3, SEEK_CUR)!=0)         {            fprintf (stderr, "fseek() fails in startcode() (1)\n");            exit(-1);        }        return 0;   // not a startcode    }    if (c2==0x01)//对应起始码为00 00 01    {        if (fseek(infile, -3, SEEK_CUR)!=0)         {            fprintf (stderr, "fseek() fails in startcode() (5)\n");            exit(-1);        }        return (1);     }    if (c2==0)//对应起始码为00 00 00 01    {        if ((c3=fgetc(infile))==EOF)            return 0;        if (c3!=0x01)         {            if (fseek(infile, -4, SEEK_CUR)!=0)             {                fprintf (stderr, "fseek() fails in startcode() (3)\n");                exit(-1);            }            return 0;        }        if (fseek(infile, -4, SEEK_CUR)!=0)         {            fprintf (stderr, "fseek() fails in startcode() (5)\n");            exit(-1);        }        return (1);     }}int naluloss() {    int c;    do        if ((c=getc(errfile))==EOF) //getc(fp):从文件指针fp中读取一个字符。。EOF表示读入的是文件结束符        {            fseek (errfile, 0, SEEK_SET);            c=getc(errfile);        }    while (isspace(c));     // Skip over whitespace    检查参数c是否为空格字符,也就是判断是否为空格(' ')、水平定位字符('\t')、归位键('\r')、换行('\n')、垂直定位字符('\v')或翻页('\f')的情况。若参数c为空格字符,则返回TRUE,否则返回NULL(0)。    return (c=='0');}void parsecmd (int ac, char *av[]) {    if ((infile=fopen(av[1], "rb"))==NULL) //argv[1]是输入文件;    {        fprintf (stderr, "Cannot open infile %s\n", av[1]);        exit(-1);    }    if ((outfile=fopen(av[2], "wb"))==NULL) //argv[2]是输出文件;    {        fprintf (stderr, "Cannot open outfile %s\n", av[2]);        exit(-1);    }    if ((errfile=fopen(av[3], "r"))==NULL) //argv[3]是错误文件:这是一个.txt文件,在该文件中空白部分忽略,0表示一个丢失的NALU,1表示正确传输的NALU    {        fprintf (stderr, "Cannot open error file %s\n", av[3]);        exit(-1);    }}void cleanup() {    fclose(infile);    fclose(outfile);    fclose(errfile);    int n=nalusreadValid/6;    printf ("Loss terminated.  Lost %d nalus\n   Total %d nalus\n    Percentage of loss: %f%\n",nalusLost, n, float(nalusLost)/float(n)*100);    for(int i=0; i<64; i++)        if (nalutypecountread[i]>0)            printf ("Nal unit type %2d, read %5d, wrote %5d\n", i, nalutypecountread[i], nalutypecountwrite[i]);    printf("press any key to exit...\n");    getchar();}int nal_unit_type(unsigned char buf[]) {    unsigned int firstbyte;    unsigned int nalutype;      firstbyte=buf[4];//buf[0]~buf[3]存储的是起始码(0x00000001);buf[4]存储了数据的第一个字节    nalutype=firstbyte&0x7E;    // 0x7E的二进制位01111110    // sanity check: forbidden_zero_bit is really zero    if (firstbyte&0x80) //0x80的二进制为10000000。。。forbidden_zero_bit 必须为0    {        fprintf(stderr, "forbidden_zero_bit is 1 at octet %d, exiting\n", ftell(infile));        exit(-1);    }    nalutype=nalutype>>1;    if (0<=nalutype<=63)        return nalutype;    else    {        fprintf (stderr, "wrong NAL unit type %d 0x%x detected in first byte 0x%x, octet %d, exiting\n", nalutype, firstbyte, ftell(infile));        getchar();        exit(-1);    }}

这里写图片描述
该函数实现的功能是中间视点纹理图的随机丢包(IBP情况下)。
以上代码参考JCTVC-H0072

0 0
原创粉丝点击