c语言程序200行以上,200行以上C语言程序举例

这是一个关于C语言的程序示例,涉及从TS(Transport Stream)文件中提取PES(Packetized Elementary Stream),然后将PES转换为ES(Elementary Stream)并进一步提取I帧。程序实现了视频流处理的基本步骤,包括时间戳的设置和数据包的解析。
摘要由CSDN通过智能技术生成

200行以上C语言程序举例

关注:295  答案:2  mip版

解决时间 2021-02-01 09:31

e6cb1a03ad541b3098697807b7bf1798.png

提问者萌城姑凉

2021-01-31 16:46

200行以上C语言程序举例

最佳答案

e6cb1a03ad541b3098697807b7bf1798.png

二级知识专家嶶笑螚讓亼瘋鋽

2021-01-31 18:08

无聊,粘着玩 657行

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

int raw2iframe(char *rawfile, char *ifile );

int Ts_pes(const char *src, const char *des ,unsigned short pid);

int pes_raw( char *pesfile, char *rawfile );

int es_pes(char *src, char *des);

int pes_ts(char *tsfile, char *pesfile);

static unsigned char m_buf[188 * 1024];

static unsigned char buf[188*1024];

unsigned int SetDtsTimeStamp( unsigned char *buf, unsigned int time_stemp)

{

buf[0] = ((time_stemp >> 29) | 0x11 ) & 0x1f;

buf[1] = time_stemp >> 22;

buf[2] = (time_stemp >> 14) | 0x01;

buf[3] = time_stemp >> 7;

buf[4] = (time_stemp << 1) | 0x01;

return 0;

}

unsigned int SetPtsTimeStamp( unsigned char *buf, unsigned int time_stemp)

{

buf[0] = ((time_stemp >> 29) | 0x31 ) & 0x3f;

buf[1] = time_stemp >> 22;

buf[2] = (time_stemp >> 14) | 0x01;

buf[3] = time_stemp >> 7;

buf[4] = (time_stemp << 1) | 0x01;

return 0;

}

unsigned int GetTimeStamp( unsigned char *buf )

{

unsigned int ts;

unsigned char *p = buf;

ts = ((*p>>1) & 0x7) << 30;

p++;

ts += (*p) << 22;

p++;

ts += ((*p)>>1) << 15;

p++;

ts += (*p) << 7;

p++;

ts += (*p) >> 1;

p++;

return ts;

}

int main(void)

{

Ts_pes("shoes.ts", "641.pes", 641); //提取PES

pes_raw("641.pes","641.raw"); //提取ES

raw2iframe( "641.raw", "641.iframe" ); //提取I帧

es_pes("641.iframe", "es.pes"); //打包成PES

pes_ts("pes.ts","es.pes"); //打包成TS

return 0;

}

int Ts_pes(const char *src, const char *des ,unsigned short pid)

{

unsigned char *p;

FILE *fpr, *fpw;

int size;

int ret;

unsigned short vpid;

unsigned char adaptation_field_control;

unsigned char continuity_counter;

unsigned char adaptation_field_length;

unsigned char *payload;

unsigned char payload_unit_start_indicator;

unsigned char last_counter = 0xff;

int start = 0;

fpr = fopen(src, "rb"); //打开文件

fpw = fopen(des, "wb"); //写入文件

if(NULL == fpr || NULL == fpw)

{

return -1;

}

size = sizeof(m_buf);

while(!feof(fpr))

{

ret = fread(m_buf, 1, size, fpr ); //读取文件

p = m_buf;

while(p < m_buf + ret)

{

vpid = (p[1] & 0x1f) << 8 | p[2];

if(pid == vpid)

{

adaptation_field_control = (p[3]>>4)&0x3; //判断是否有可调整字段

continuity_counter = p[3] & 0xf;

payload_unit_start_indicator = (p[1]>>6) & 0x1;

payload = NULL;

adaptation_field_length = p[4];

switch(adaptation_field_control)

{

case 0:

case 2:

break;

case 1:

payload = p + 4;

break;

case 3:

payload = p + 5 + p[4];

break;

}

if(1 == payload_unit_start_indicator)

{

start = 1;

}

if(start && payload)

{

fwrite(payload, 1, p + 188 - payload, fpw); //写入文件

}

if( last_counter!= 0xff && ((last_counter +1 )&0xf) != continuity_counter )

{

printf( "data lost\n" );

}

last_counter = continuity_counter;

}

p = p + 188;

}

}

printf("ts_pes_END\n");

fclose(fpr); //关闭

fclose(fpw);

return 0;

}

int pes_raw( char *pesfile, char *rawfile )

{

FILE *fpd, *fp;

unsigned char *p, *payload, *tmp;

int size, num, rdsize;

unsigned int last = 0;

__int64 total = 0, wrsize = 0;

fp = fopen( pesfile, "rb" );

fpd = fopen( rawfile, "wb" );

if( fp == NULL || fpd == NULL )

return -1;

num = 0;

size = 0;

p = m_buf;

while( true )

{

REDO:

if( m_buf + size <= p )

{

p = m_buf;

size = 0;

}

else if( m_buf < p && p < m_buf + size )

{

size -= p - m_buf;

memmove( m_buf, p, size );

p = m_buf;

}

if( !feof(fp) && size < sizeof(m_buf) )

{

rdsize = fread( m_buf+size, 1, sizeof(m_buf)-size, fp );

size += rdsize;

total += rdsize;

}

if( size <= 0 )

break;

tmp = p;

while( p[0] != 0 || p[1] != 0 || p[2] != 0x01 ||

( ( p[3] & 0xe0 ) != 0xe0 && ( p[3] & 0xe0 ) != 0xc0 ) )

{

p++;

if( m_buf + size <= p )

goto REDO;

}

if( p != tmp )

{

printf( "pes skip size=%d\n", p - tmp );

}

unsigned short len = (p[4]<<8) | p[5];

if( len == 0 )

{

unsigned char *end = p + 6;

while( end[0] != 0 || end[1] != 0 || end[2] != 0x01 ||

( ( end[3] & 0xe0 ) != 0xe0 && ( end[3] & 0xc0 ) != 0xc0 ) )

{

if( m_buf + size <= end )

{

if( feof(fp) )

break;

goto REDO;

}

end++;

}

len = end - p - 6;

}

if( m_buf + size < p + 6 + len )

{

if( feof(fp) )

break;

continue;

}

p += 6;

{

unsigned char PES_scrambling_control = (*p>>4)&0x3;

unsigned char PES_priority = (*p>>3)&0x1;

unsigned char data_alignment_indicator = (*p>>2)&0x1;

unsigned char copyright = (*p>>1)&0x1;

unsigned char original_or_copy = (*p)&0x1;

p++;

unsigned char PTS_DTS_flags = (*p>>6)&0x3;

unsigned char ESCR_flag = (*p>>5)&0x1;

unsigned char ES_rate_flag = (*p>>4)&0x1;

unsigned char DSM_trick_mode_flag = (*p>>3)&0x1;

unsigned char additional_copy_info_flag = (*p>>2)&0x1;

unsigned char PES_CRC_flag = (*p>>1)&0x1;

unsigned char PES_extension_flag = (*p)&0x1;

p++;

unsigned char PES_header_data_length = *p;

p++;

payload = p + PES_header_data_length;

if (PTS_DTS_flags == 0x2 )

{

unsigned int pts;

pts = (*p>>1) & 0x7;

pts = pts << 30;

p++;

pts += (*p)<<22;

p++;

pts += ((*p)>>1)<<15;

p++;

pts += (*p)<<7;

p++;

pts += (*p)>>1;

p++;

p -= 5;

if( pts < last )

{

printf( "?\n" );

}

last = pts;

}

else if( PTS_DTS_flags == 0x3 )

{

unsigned int pts, dts;

pts = (*p>>1) & 0x7;

pts = pts << 30;

p++;

pts += (*p)<<22;

p++;

pts += ((*p)>>1)<<15;

p++;

pts += (*p)<<7;

p++;

pts += (*p)>>1;

p++;

dts = (*p>>1) & 0x7;

dts = dts << 30;

p++;

dts += (*p)<<22;

p++;

dts += ((*p)>>1)<<15;

p++;

dts += (*p)<<7;

p++;

dts += (*p)>>1;

p++;

p -= 10;

printf( "num=%d dura=%d size=%d pts-dts=%d\n", num, pts - last, len-3-PES_header_data_length, (int)(pts-dts) );

if( pts < last )

{

printf( "?\n" );

}

last = pts;

}

else if( PTS_DTS_flags != 0 )

{

printf( "error\n" );

}

if( fpd )

{

fwrite( p + PES_header_data_length, 1, len - 3 - PES_header_data_length, fpd );

wrsize += len - 3 - PES_header_data_length;

}

num++;

p += len - 3;

}

payload = p;

size -= p - m_buf;

memmove( m_buf, p, size );

p = m_buf;

}

fclose( fp );

fclose( fpd );

printf("pes_raw_END\n");

return 0;

}

int raw2iframe(char *rawfile, char *ifile )

{

unsigned char *temp_p;

unsigned char *p;

unsigned char picture_coding_type;

unsigned char buf[188*1024] = {0};

unsigned char pes_buf[32*1024] = {0};

unsigned short pid = 641;

unsigned short t_pid = 0;

int i = 0;

int j = 0;

int size = 0;

int iLen = 0;

int wiLen = 0;

int temp_queue = 0;

int temp_ifrem = 0;

void *fps = CreateFile(rawfile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); //打开读文件

void *fpd = CreateFile(ifile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0 , NULL); //打开写文件

temp_p = NULL;

while(1)

{

if (0 == ReadFile(fps, buf+size, sizeof(buf)-size, (unsigned long *)&iLen, NULL)) //读取文件内容

{

CloseHandle((HANDLE)fps);

return -1;

}

if (0 == iLen)

{

break;

}

p = buf;

while( p + 6 < buf + iLen +size)

{

if (p[0] == 0x0 && p[1] == 0x0 && p[2] == 0x1) //是头进入

{

if (p[3] == 0x0 || p[3] == 0xB3 )

{

if ( (NULL != temp_p) && ((1 == temp_queue) || (1 == temp_ifrem)))

{

WriteFile(fpd, temp_p, p-temp_p, (unsigned long *)&wiLen, NULL); //写文件,先写序列头,再写I帧

temp_queue = 0;

temp_ifrem = 0;

temp_p = NULL;

}

}

if (p[3] == 0xB3) //判断是视频序列,则初始化

{

temp_queue = 1;

temp_p = p;

}

picture_coding_type = (p[5]>>3) & 0x7;

if (p[3] == 0x0 && 1 == picture_coding_type) //判断是I帧,则初始化

{

temp_ifrem = 1;

temp_p = p;

}

}

p++;

}

if(NULL != temp_p)

{

size = buf + iLen + size - temp_p;

memmove(buf, temp_p, size);

}

else

{

size = buf + iLen + size - p;

memmove(buf, p, size);

}

temp_p = NULL;

}

CloseHandle((HANDLE)fps);

CloseHandle((HANDLE)fpd);

printf("raw_iframe_END\n");

return 0;

}

int es_pes(char *src, char *des)

{

FILE *iframe_fp, *pes_fp;

unsigned char *p;

unsigned char *temp_p = NULL;

unsigned char buf[188*1024] = {0};

unsigned char pes_header[19];

unsigned short int pes_packet_length = 0;

unsigned int framecnt = 0;

unsigned char flag = 0;

unsigned int Pts = 0;

unsigned int Dts = 0;

int i = 0;

int size = 0;

int iLen = 0;

int wiLen = 0;

int temp_que = 0;

iframe_fp = fopen( src, "rb" );

pes_fp = fopen( des, "wb" );

if( iframe_fp == NULL || pes_fp == NULL )

{

return -1;

}

while(!feof(iframe_fp))

{

iLen = fread(buf + size, 1, sizeof(buf) - size, iframe_fp);

p = buf;

while( p + 3 < buf + iLen +size)

{

memset(pes_header, 0, sizeof(pes_header));

if (p[0] == 0x0 && p[1] == 0x0 && p[2] == 0x1 && 0xB3 == p[3])

{

if ((NULL != temp_p) && (1 == temp_que))

{

LAST_I:

pes_packet_length = p - temp_p + 13;

pes_header[4] = (pes_packet_length&0xff00) >> 8;

pes_header[5] = pes_packet_length&0x00ff;

pes_header[0] = 0;

pes_header[1] = 0;

pes_header[2] = 0x01;

pes_header[3] = 0xE0;

pes_header[6] = 0x81;

pes_header[7] = 0xC0;

pes_header[8] = 0x0A;

Dts = (framecnt + 1) * 40 * 90;

Pts = framecnt * 40 * 90;

SetPtsTimeStamp(&pes_header[9], Pts); //设置时间戳 PTS

SetDtsTimeStamp(&pes_header[14], Dts); //设置时间戳 DTS

framecnt++;

if (0 == flag)

{

fwrite(pes_header, 1, sizeof(pes_header), pes_fp); //把PES包头写入文件

fwrite(temp_p, 1, p-temp_p, pes_fp); //把I帧写入文件

}

else

{

fwrite(pes_header, 1, sizeof(pes_header), pes_fp); //把PES包头写入文件

fwrite(temp_p, 1, p-temp_p+3, pes_fp); //把I帧写入文件

}

temp_p = NULL;

}

if (p[3] == 0xB3) //判断是否到了下一个序列头

{

temp_p = p;

temp_que = 1;

}

}

p++;

}

if(NULL != temp_p)

{

if (feof(iframe_fp)) //把最后一帧写入文件

{

// flag = 1;

goto LAST_I;

}

size = buf + iLen + size - temp_p;

memmove(buf, temp_p, size);

}

else

{

size = buf + iLen + size - p;

memmove(buf, p, size);

}

temp_p = NULL;

}

printf("es_pes_END\n");

fclose(iframe_fp);

fclose(pes_fp);

return 0;

}

int pes_ts(char *tsfile, char *pesfile)

{

FILE *ts_fp, *pes_fp;

int flag = 0;

int iLen = 0;

int size = 0;

int temp_pes = 0;

int pes_packet_len = 0;

unsigned char *p;

unsigned char counter = 0;

unsigned char *temp_p = NULL;

unsigned char ts_buf[188] = {0};

unsigned char start_indicator_flag = 0;

pes_fp = fopen(pesfile, "rb");

ts_fp = fopen(tsfile, "wb");

if( ts_fp == NULL || pes_fp == NULL )

{

return -1;

}

ts_buf[0] = 0x47;

ts_buf[1] = 0x62;

ts_buf[2] = 0x81;

while(!feof(pes_fp))

{

iLen = fread(buf+size, 1, sizeof(buf)-size, pes_fp); //读文件

p = buf;

while( p + 6 < buf + iLen +size)

{

if (0 == p[0] && 0 == p[1] && 0x01 == p[2] && 0xE0 == p[3]) //进入

{

if (flag == 0) //第一次找到PES包

{

temp_p = p;

flag = 1;

}

else

{

pes_packet_len = p - temp_p; //pes包长度

start_indicator_flag = 0;

while (1)

{

ts_buf[3] = counter;

if (1 != start_indicator_flag)

{

ts_buf[1] = ts_buf[1] | 0x40; //payload_unit_start_indicator置为1

start_indicator_flag = 1;

}

else

{

ts_buf[1] = ts_buf[1] & 0xBF; //payload_unit_start_indicator置为0

}

if (pes_packet_len > 184) //打包成TS包(188)

{

ts_buf[3] = ts_buf[3] & 0xDF;

ts_buf[3] = ts_buf[3] | 0x10;

memcpy(&ts_buf[4], temp_p, 184);

fwrite(ts_buf, 1, 188, ts_fp); //写文件

pes_packet_len -=184;

temp_p += 184;

}

else //不够184B的加入调整字段,为空

{

ts_buf[3] = ts_buf[3] | 0x30;

ts_buf[4] = 183 - pes_packet_len;

memcpy(&ts_buf[4] + 1 + ts_buf[4], temp_p, pes_packet_len);

fwrite(ts_buf, 1, 188, ts_fp); //写文件

break;

}

counter = (counter + 1) % 0x10; //ts包计数

}

}

temp_p = p;

}

p++;

}

if (1 == flag)

{

size = buf + iLen + size - temp_p;

memmove(buf, temp_p, size);

temp_p = NULL;

flag = 0;

}

else

{

size =buf + iLen + size - p;

memmove(m_buf , p , size);

}

}

printf("pes_ts_END\n");

fclose(pes_fp);

fclose(ts_fp);

return 0;

}

全部回答

e6cb1a03ad541b3098697807b7bf1798.png

1楼等待、埖开

2021-01-31 19:48

期末的作业吧?呵呵 给你两个:

一、题目:家庭财务管理小程序

2.程序源代码:

#include "stdio.h"

#include "dos.h"

main()

{

file *fp;

struct date d;

float sum,chm=0.0;

int len,i,j=0;

int c;

char ch[4]="",ch1[16]="",chtime[12]="",chshop[16],chmoney[8];

pp: clrscr();

sum=0.0;

gotoxy(1,1);printf("|----------------------------------------------------|");

gotoxy(1,2);printf("| money management system(c1.0) 2000.03 |");

gotoxy(1,3);printf("|----------------------------------------------------|");

gotoxy(1,4);printf("| -- money records -- | -- today cost list -- |");

gotoxy(1,5);printf("| ------------------------ |-----------------------------|");

gotoxy(1,6);printf("| date: -------------- | |");

gotoxy(1,7);printf("| | | | |");

gotoxy(1,8);printf("| -------------- | |");

gotoxy(1,9);printf("| thgs: ------------------ | |");

gotoxy(1,10);printf("| | | | |");

gotoxy(1,11);printf("| ------------------ | |");

gotoxy(1,12);printf("| cost: ---------- | |");

gotoxy(1,13);printf("| | | | |");

gotoxy(1,14);printf("| ---------- | |");

gotoxy(1,15);printf("| | |");

gotoxy(1,16);printf("| | |");

gotoxy(1,17);printf("| | |");

gotoxy(1,18);printf("| | |");

gotoxy(1,19);printf("| | |");

gotoxy(1,20);printf("| | |");

gotoxy(1,21);printf("| | |");

gotoxy(1,22);printf("| | |");

gotoxy(1,23);printf("|--------------------------------------------------|");

i=0;

getdate(&d);

sprintf(chtime,"%4d.%02d.%02d",d.da_year,d.da_mon,d.da_day);

for(;;)

{

gotoxy(3,24);printf(" tab __browse cost list esc __quit");

gotoxy(13,10);printf(" ");

gotoxy(13,13);printf(" ");

gotoxy(13,7);printf("%s",chtime);

j=18;

ch[0 ]=getch();

if(ch[0]==27)

break;

strcpy (chshop,"");

strcpy(chmoney,"");

if(ch[0]==9)

{

mm:i=0;

fp=fopen("home.dat","r+");

gotoxy(3,24);printf(" ");

gotoxy(6,4);printf(" list records ");

gotoxy(1,5);printf("|-------------------------------------|");

gotoxy(41,4);printf(" ");

gotoxy(41,5);printf(" |");

while(fscanf(fp,"%10s%14s%f\n",chtime,chshop,&chm)!=eof)

{

if(i==36)

{

getch();

i=0;

}

if ((i%36)<17)

{

gotoxy(4,6+i);

printf(" ");

gotoxy(4,6+i);

}

else

if((i%36)>16)

{

gotoxy(41,4+i-17);

printf(" ");

gotoxy(42,4+i-17);

}

i++;

sum=sum+chm;

printf("%10s %-14s %6.1f\n",chtime,chshop,chm);}

gotoxy(1,23);printf("|----------------------------------------------|");

gotoxy(1,24);printf("| |");

gotoxy(1,25);printf("|----------------------------------------------|");

gotoxy(10,24);printf("total is %8.1f$",sum);

fclose(fp);

gotoxy(49,24);printf("press any key to.....");getch();goto pp;

}

else

{

while(ch[0]!='\r')

{

if(j<10)

{

strncat(chtime,ch,1);

j++;

}

if(ch[0]==8)

{

len=strlen(chtime)-1;

if(j>15)

{

len=len+1;

j=11;

}

strcpy(ch1,"");

j=j-2;

strncat(ch1,chtime,len);

strcpy(chtime,"");

strncat(chtime,ch1,len-1);

gotoxy(13,7);printf(" ");

}

gotoxy(13,7);printf("%s",chtime);ch[0]=getch();

if(ch[0]==9)

goto mm;

if(ch[0]==27)

exit(1);

}

gotoxy(3,24);printf(" ");

gotoxy(13,10);

j=0;

ch[0]=getch();

while(ch[0]!='\r')

{

if (j<14)

{

strncat(chshop,ch,1);

j++;

}

if(ch[0]==8)

{

len=strlen(chshop)-1;

strcpy(ch1,"");

j=j-2;

strncat(ch1,chshop,len);

strcpy(chshop,"");

strncat(chshop,ch1,len-1);

gotoxy(13,10);printf(" ");

}

gotoxy(13,10);printf("%s",chshop);ch[0]=getch();}

gotoxy(13,13);

j=0;

ch[0]=getch();

while(ch[0]!='\r')

{

if (j<6)

{

strncat(chmoney,ch,1);

j++;

}

if(ch[0]==8)

{

len=strlen(chmoney)-1;

strcpy(ch1,"");

j=j-2;

strncat(ch1,chmoney,len);

strcpy(chmoney,"");

strncat(chmoney,ch1,len-1);

gotoxy(13,13);printf(" ");

}

gotoxy(13,13);printf("%s",chmoney);ch[0]=getch();

}

if((strlen(chshop)==0)||(strlen(chmoney)==0))

continue;

if((fp=fopen("home.dat","a+"))!=null);

fprintf(fp,"%10s%14s%6s",chtime,chshop,chmoney);

fputc('\n',fp);

fclose(fp);

i++;

gotoxy(41,5+i);

printf("%10s %-14s %-6s",chtime,chshop,chmoney);

}

}

}

二、万年历

#include"iostream.h"

#include"iomanip.h"

#include"stdlib.h"

leapyear(int a)

{

if((a%4==0&&a%100!=0)||(a%100==0&&a%400==0))

return (1);

else return (0);

}

int aa[12]={31,28,31,30,31,30,31,31,30,31,30,31};

int cc[12]={31,29,31,30,31,30,31,31,30,31,30,31};

char bb[8][8]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};

ww(int a,int b,int c)

{

int n=0,m=0,i,j,k=0;

for(i=1;i{

if(leapyear(i)==1)

m=m+366;

else m=m+365;

}

for(j=1;jn;

switch(n)

{

case 1:cout<>year;

cout< cin>>month;

cout< cin>>day;

cout<>m;

if(leapyear(m)==1) cout<>m;

yuefen(m);break;

case 4:cout<>m;

nianfen(m);break;

case 5:cout<>n;

cout< cin>>m;

cout< nianyue(n,m);break;

case 6:exit(0);

}

}

}

我要举报

如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

→点此我要举报以上信息!←

推荐资讯

大家都在看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值