200行以上C语言程序举例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了200行以上C语言程序举例相关的知识,希望对你有一定的参考价值。

无聊,粘着玩 657行

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <ctype.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <Winsock2.h>
#include <ws2tcpip.h>
#include <io.h>

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];

/* 设置Dts时间戳(90khz) */
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;


/* 设置Pts时间戳(90khz) */
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;


/* 读取时戳(45khz) */
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;


/*从视频中提取PES*/
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; /*0为保留,2为只有调整无负载*/
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;


/*从PES中提取ES*/
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;
/* 寻找PES-HEADER: 0X000001E0 */
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 );

/* PES_packet_length */
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;


/*提取I帧*/
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++;

/*把多出来的内容写入下个BUF*/
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;


/*打包成PES*/
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包头的相关数据*/
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++;


/*把多出来的内容写入下个BUF*/
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;


/*打包成TS包*/
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参数*/
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;
参考技术A 你下载一下Linux的源代码看看不就得了,那玩意好几十M呢,甚至你感兴趣还可以修改成具备你自己风格的操作系统

以上是关于200行以上C语言程序举例的主要内容,如果未能解决你的问题,请参考以下文章

C语言项目实战:《气球射击》游戏项目,200行代码轻松实现

C语言项目实战:《国际象棋》零基础项目,200 行源代码示例

C语言中goto语句的用法??举例来说。

C语言初学者项目 200行代码用一个二维数组实现贪吃蛇游戏

C语言中switch语句用法(做题)!谢谢!

c语言case