#define ERROR 0
#define TRUE 1
#define FALSE 0 #define MAXSIZE 100 #define LElemType int
#define Status int
#define BOOL int typedef struct
{
LElemType data
int cur
}Component,SLinkList[MAXSIZE] int Malloc(SLinkList space)
{
//若备用链表非空,则返回分配的结点下标(备用链表的第一个结点),否则返回0
int i=space[0].cur
if (i)
space[0].cur=space[i].cur
return i
} Status Free(SLinkList space, int k)
{
//将下标为空的空闲结点回收到备用链表(成为备用链表的第一个结点)
space[k].cur=space[0].cur
space[0].cur=k
return OK
} Status InitList(SLinkList L)
{
//构造一个空的链表L,表头为L的最后一个单元L[MAXSIZE-1],其余单元链成
//一个备用链表,表头为L的第一个单元L[0],“0”表示空指针
int i
L[MAXSIZE-1].cur=0
for (i=0i<MAXSIZE-2i++)
L[i].cur=i+1
L[MAXSIZE-2].cur=0
return OK
} Status ClearList(SLinkList L)
{
//初始条件:线性表L已存在。 *** 作结果:将L重置为空表
int i,j,k
i=L[MAXSIZE-1].cur
L[MAXSIZE-1].cur=0
k=L[0].cur
L[0].cur=i
while (i)
{
j=i
i=L[i].cur
}
L[j].cur=k
return OK
} BOOL ListEmpty(SLinkList L)
{
//若L是空表,返回TRUE;否则返回FALSE
if (!L[MAXSIZE-1].cur)
return TRUE
else
return FALSE
} int ListLength(SLinkList L)
{
//返回L中数据元素个数
int i,len
i=L[MAXSIZE-1].cur
len=0
while (i)
{
i=L[i].cur
len++
}
return len
} Status GetElem(SLinkList L,int i,LElemType *e)
{
//用e返回L中第i个元素的值
int j,k=MAXSIZE-1
if (i<1||i>ListLength(L))
return ERROR
for (j=1j<=ij++)
k=L[k].cur
*e=L[k].data
return OK
} int LocateElem(SLinkList L,LElemType e)
{
//在静态单链线性表L中查找第1个值为e的元素。若找到,则返回它在L中的
//位序,否则返回0。(与其它LocateElem()的定义不同)
int i=L[MAXSIZE-1].cur
while (i&&L[i].data!=e)
i=L[i].cur
return i
} Status PriorElem(SLinkList L,LElemType cur_e,LElemType *pre_e)
{
//初始条件:线性表L已存在
// *** 作结果:若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱,
// 否则 *** 作失败,pre_e无定义
int i,j
i=L[MAXSIZE-1].cur
do{
j=i
i=L[i].cur
}while (i&&L[i].data!=cur_e)
if (i)
{
*pre_e=L[j].data
return OK
}
return ERROR
} Status NextElem(SLinkList L,LElemType cur_e,LElemType *next_e)
{
//初始条件:线性表L已存在
// *** 作结果:若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,
// 否则 *** 作失败,next_e无定义
int i,j
i=LocateElem(L,cur_e)
if (i)
{
j=L[i].cur
if (j)
{
*next_e=L[j].data
return OK
}
}
return ERROR
} Status ListInsert(SLinkList L,int i,LElemType e)
{
//在L中第i个元素之前插入新的数据元素e
int j,k,l
k=MAXSIZE-1
if (i<1||i>ListLength(L)+1)
return ERROR
j=Malloc(L)
if (j)
{
L[j].data=e
for(l=1l<=i-1l++)
k=L[k].cur
L[j].cur=L[k].cur
L[k].cur=j
return OK
}
return ERROR
} Status ListDelete(SLinkList L,int i,LElemType *e)
{
//删除在L中第i个数据元素e,并返回其值
int j,k
if (i<1||i>ListLength(L))
return ERROR
k=MAXSIZE-1
for (j=1j<=i-1j++)
k=L[k].cur
j=L[k].cur
L[k].cur=L[j].cur
*e=L[j].data
Free(L,j)
return OK
} Status ListTraverse(SLinkList L,void (* visit)(LElemType e))
{
int i=L[MAXSIZE-1].cur
while (i)
{
visit(L[i].data)
i=L[i].cur
}
return OK
} void Visit(LElemType e)
{
printf("%d\n",e)
} int main()
{
int i,j,k
LElemType e,e0
SLinkList L
InitList(L)
for(j=1j<=5j++)
i=ListInsert(L,1,j)
ListTraverse(L,Visit)
//判断链表是否为空
i=ListEmpty(L)
printf("%d\n",i)
//打印链表长度
printf("%d\n",ListLength(L))
//清空静态链表
ClearList(L)
ListTraverse(L,Visit)
for(j=1j<=10j++)
ListInsert(L,j,j)
//插入新节点后
ListTraverse(L,Visit)
//获取链表中的第5个元素
GetElem(L,5,&e)
printf("%d\n",e)
for(j=0j<=1j++)
{
k=LocateElem(L,j)
if(k)
printf("%d %d\n",j,k)//j在链表中的序号k
else
printf("Can't find %d\n",j)//链表中不存在j
}
for(j=1j<=2j++) //测试头两个数据
{
GetElem(L,j,&e0) //把第j个数据赋给e0
i=PriorElem(L,e0,&e) //求e0的前驱
if(!i)
printf("No elem before %d\n",e0)
else
printf("Elem before %d is %d\n",e0,e)//数据e0的前驱
}
for(j=ListLength(L)-1j<=ListLength(L)j++) //最后两个数据
{
GetElem(L,j,&e0) //把第j个数据赋给e0
i=NextElem(L,e0,&e) //求e0的后继
if(!i)
printf("No elem after %d\n",e0)
else
printf("The elem after % is %d\n",e0,e)//数据e0的后继
}
k=ListLength(L) //k为表长
for(j=k+1j>=kj--)
{
i=ListDelete(L,j,&e) //删除第j个数据
if(i)
printf("Delete Succussfully\n")
else
printf("Can't find the elem\n",j)
}
ListTraverse(L,Visit)
return 0
}
给你找了个静态链表的代码,能编译运行
给你一个通讯录管理系统程序 四百多行 够你用了吧!!! #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #define N 100 void input()//添加新用户函数 void amend()//修改用户信息函数 void delete_client()//删除用户信息函数 void demand_client()//用户信息查询函数 void collect_telephone()//用户信息汇总函数 void save_client(struct telephone message)//保存函数 void demand_name()//按用户名查询 void demand_telephone()//按电话号码查询 struct telephone { char client_name[20] char client_address[30] char client_telephone[15] } //添加新用户函数 void input() { struct telephone message char reply='y' char save='y' while (reply=='y') { printf("用户姓名:") scanf("%s",message.client_name) printf("电话号码:") scanf("%s",message.client_telephone) save_client(message) printf("要继续吗?(y/n):") scanf(" %c",&reply) } printf("按任意键返回主菜单……\n") getchar() getchar() } //保存函数 void save_client(struct telephone message) { FILE *fp fp=fopen("message.dat","a+") if (fp!=NULL) { fwrite(&message,sizeof(struct telephone),1,fp) } else { printf("\n打开文件时出现错误,按任意键返回……\n") getchar() return } fclose(fp) } //修改用户信息函数 void amend() { struct telephone message FILE *fp char amend_name[20] char reply='y' char found='y' char save='y' int size=sizeof(struct telephone) while (reply=='y') { found='n' fp=fopen("message.dat","r+w") if (fp!=NULL) { system("cls") printf("\n请输入要修改的姓名:") scanf("%s",amend_name) while ((fread(&message,size,1,fp))==1) { if ((strcmp(amend_name,message.client_name))==0) { found='y' break } } if (found=='y') { printf("==========================================\n") printf("\n用户姓名:%s\n",message.client_name) printf("\n电话号码:%s\n",message.client_telephone) printf("==========================================\n") printf("修改用户信息:\n") printf("\n用户姓名:") scanf("%s",message.client_name) printf("\n电话号码:") scanf("%s",message.client_telephone) printf("\n要保存吗?(y/n):") scanf(" %c",&save) if (save=='y') { fseek(fp,-size,1) fwrite(&message,sizeof(struct telephone),1,fp) } } else { printf("无此人信息!\n") } } else { printf("打开文件时出现错误,按任意键返回……\n") getchar() return } fclose(fp) printf("要继续吗?(y/n):") scanf(" %c",&reply) } printf("按任意键返回主菜单……\n") getchar() getchar() } //删除用户信息函数 void delete_client() { struct telephone message[N] struct telephone temp_str struct telephone delete_str int i=0,j=0 char reply='y' char found='y' char confirm='y' char delete_name[20] FILE *fp while (reply=='y') { system("cls") fp=fopen("message.dat","r") if (fp!=NULL) { i=0 found='n' printf("\n请输入姓名:") scanf("%s",delete_name) while ((fread(&temp_str,sizeof(struct telephone),1,fp))==1) { if ((strcmp(delete_name,temp_str.client_name))==0) { found='y' delete_str=temp_str }//查找要删除的记录 else { message[i]=temp_str i++ }//将其它无关记录保存起来 } } else { printf("打开文件时出现错误,按任意键返回……\n") getchar() return } fclose(fp) if (found=='y') { printf("==========================================\n") printf("用户姓名:%s\n",delete_str.client_name) printf("电话号码:%s\n",delete_str.client_telephone) printf("==========================================\n") } else { printf("无此人信息,按任意键返回……\n") getchar() break } printf("确定要删除吗?(y/n):") scanf(" %c",&confirm) if (confirm=='y') { fp=fopen("message.dat","w") if (fp!=NULL) { for(j=0j<ij++) { fwrite(&message[j],sizeof(struct telephone),1,fp) } printf("记录已删除!!!\n") } else { printf("打开文件时出现错误,按任意键返回……\n") getchar() return } fclose(fp) } printf("要继续吗?(y/n):") scanf(" %c",&reply) } printf("按任意键返回主菜单……\n") getchar() } //用户信息查询函数 void demand_client() { int choice=1 while (choice!=3) { system("cls") printf("电话查询菜单\n") printf(" 1 按联系人姓名查询\n") printf(" 2 按联系人电话号码查询\n") printf(" 3 返回主菜单\n") printf("请选择(1-3):") scanf("%d%*c",&choice) if (choice>3) { printf("请输入1-6之间的整数\n") printf("按任意键返回菜单……\n") getchar() continue } if (choice==1) { demand_name() } else if (choice==2) { demand_telephone() } } } //按用户名查询 void demand_name() { struct telephone message FILE *fp char amend_name[20] char reply='y' char found='y' while (reply=='y') { found='n' fp=fopen("message.dat","r+w") if (fp!=NULL) { system("cls") printf("\n请输入姓名:") scanf("%s",amend_name) while ((fread(&message,sizeof(struct telephone),1,fp))==1) { if ((strcmp(amend_name,message.client_name))==0) { found='y' break } } if (found=='y') { printf("==========================================\n") printf("用户姓名:%s\n",message.client_name) printf("电话号码:%s\n",message.client_telephone) printf("==========================================\n") } else { printf("无此人信息!\n") } } else { printf("打开文件时出现错误,按任意键返回……\n") getchar() return } fclose(fp) printf("要继续吗?(y/n):") scanf(" %c",&reply) } printf("按任意键返回主菜单……\n") getchar() getchar() } //按电话号码查询 void demand_telephone() { struct telephone message FILE *fp char telephone[20] char reply='y' char found='y' while (reply=='y') { found='n' fp=fopen("message.dat","r+w") if (fp!=NULL) { system("cls") printf("\n请输入电话号码:") scanf("%s",telephone) while ((fread(&message,sizeof(struct telephone),1,fp))==1) { if ((strcmp(telephone,message.client_telephone))==0) { found='y' break } } if (found=='y') { printf("==========================================\n") printf("用户姓名:%s\n",message.client_name) printf("电话号码:%s\n",message.client_telephone) printf("==========================================\n") } else { printf("无此电话号码的有关信息!\n") } } else { printf("打开文件时出现错误,按任意键返回……\n") getchar() return } fclose(fp) printf("要继续吗?(y/n):") scanf(" %c",&reply) } printf("按任意键返回主菜单……\n") getchar() getchar() } //用户信息汇总函数 void collect_telephone() { struct telephone message FILE *fp fp=fopen("message.dat","r") if (fp!=NULL) { system("cls") printf("\n用户姓名\t\t电话号码\n") while ((fread(&message,sizeof(struct telephone),1,fp))==1) { printf("\n%-24s",message.client_name) printf("%-12s\n",message.client_telephone) } } else { printf("打开文件时出现错误,按任意键返回……\n") getchar() return } fclose(fp) printf("按任意键返回主菜单……\n") getch() } void main() { char choice[10]="" int len=0 while (choice[0]!='7') { printf("\t==========电话本号码查询系统=============\n") printf("\t\t 1、添加新联系人\n") printf("\t\t 2、修改联系人信息\n") printf("\t\t 3、删除联系人信息\n") printf("\t\t 4、联系人信息查询\n") printf("\t\t 5、联系人信息汇总\n") printf("\t\t 7、退出\n") printf("\t=========================================\n") printf("请选择(1-7):") scanf("%s",choice) len=strlen(choice) if (len>1) { printf("请输入1-6之间的整数\n") printf("按任意键返回主菜单……\n") getchar() getchar() continue } switch (choice[0]) { case '1': input() break case '2': amend() break case '3': delete_client() break case '4': demand_client() break case '5': collect_telephone() break default: break } } }无聊,粘着玩 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
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)