关于rtp传输jpeg图片到vlc播放

byd yes 2015-01-06 11:08:19
遇到的问题
1.调用libjpeg库压缩原始数据yuyv,保存为图片jpg正常。
2.去掉jpeg的头,打包为rtp格式发送带vlc播放,完全没法正常显示。
3.传输没问题。之前我做的h.264用rtp很正常,所以感觉肯定是在jpeg头加入rtp上时有问题。但是研究了几天没有结果。希望哪个大婶帮解决哈。
压缩代码如下:
/*24bit转化为网络字节顺序*/
#define BIGENDIAN (htons(1) == 1)
unsigned int convert24bit(unsigned int bit24)
{
if(BIGENDIAN == 1)/*大*/
return bit24 & 0xffffff;
else{//小
return ( htonl(bit24) >> 8 );
}
}
unsigned short SendFrame(unsigned short start_seq, unsigned long ts,unsigned long ssrc,unsigned char *jpeg_data, int len,
unsigned short type,unsigned short typespec, int width, int height, unsigned short dri,
unsigned short q,unsigned char *lqt,unsigned char*cqt)
{
rtp_hdr_t rtphdr;
struct jpeghdr jpghdr;
struct jpeghdr_rst rsthdr;
struct jpeghdr_qtable qtblhdr;
unsigned char packet_buf[PACKET_SIZE];
unsigned char *ptr;
unsigned int off = 0;
int bytes_left = len;
int seq = start_seq;
int pkt_len, data_len;
/* Initialize RTP header
*/
rtphdr.version = 2;
rtphdr.p = 0;
rtphdr.x = 0;
rtphdr.cc = 0;
rtphdr.m = 0;
rtphdr.pt = RTP_PT_JPEG;
rtphdr.seq = htons(seq_num);
rtphdr.ts = htonl(convertToRTPTimestamp());
// rtphdr.ts =htonl(ts);
rtphdr.ssrc = htonl(ssrc);

/* Initialize JPEG header
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type-specific | Fragment Offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type | Q | Width | Height |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
jpghdr.tspec = typespec;/*解码没有用*/
jpghdr.off = off;/*解码要用*/
jpghdr.type = type | ((dri != 0) ? RTP_JPEG_RESTART : 0);//0
jpghdr.q = q;
jpghdr.width = width>>3;
jpghdr.height = height>>3;

/* Initialize DRI header
// 0 1 2 3
//0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//| Restart Interval |F|L| Restart Count |
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
if (dri != 0) {
rsthdr.dri = dri;
rsthdr.f = 1; /* This code does not align RIs */
rsthdr.l = 1;
rsthdr.count = 0x3fff;
}

/* Initialize quantization table header
*/
if (q >= 128) {
qtblhdr.mbz = 0;
qtblhdr.precision = 0; /* This code uses 8 bit tables only */
qtblhdr.length = (128); /* 2 64-byte tables */
}

while (bytes_left > 0) {
ptr = packet_buf + RTP_HDR_SZ;
memcpy(ptr, &jpghdr, sizeof(jpghdr));
ptr += sizeof(jpghdr);

rtphdr.m = 0;

if (dri != 0) {
memcpy(ptr, &rsthdr, sizeof(rsthdr));
ptr += sizeof(rsthdr);
}
if (q >= 128 && jpghdr.off == 0) {/*发送的第一帧数据带上量化表*/
memcpy(ptr, &qtblhdr, sizeof(qtblhdr));
ptr += sizeof(qtblhdr);
memcpy(ptr, lqt, 64);
ptr += 64;
memcpy(ptr, cqt, 64);
ptr += 64;
}

data_len = PACKET_SIZE - (ptr - packet_buf);
if (data_len >= bytes_left) {
data_len = bytes_left;
rtphdr.m = 1;
}
memcpy(packet_buf, &rtphdr, RTP_HDR_SZ);
memcpy(ptr, jpeg_data + off, data_len);

send(sockfd,packet_buf, (ptr - packet_buf) + data_len,0);
off+=data_len;
//printf("off = %d\n",off & 0xffffff);
//jpghdr.off = htonl(off);
jpghdr.off = convert24bit(off);/*协议说要转化为网络字节顺序,但是这个off是24bit ,不知道如何转化为网络字节顺序,如果是用htonl直接报断错误,rfc2435*/
bytes_left -= data_len;
rtphdr.seq = htons(seq_num++);
}
return rtphdr.seq;
}

其中seq_num在前面都定义了。Q因子不知道该怎么设置,量化表直接参考RFC2435文档写的。文档:www.5dmail.net/rfc/RFC2435.txt
现在的问题是:原始数据格式是yuyv422........设置type为64,vlc端用yuyv422解压缩,完全花瓶,人影都看不到。type设置为65,vlc端为yuv420解压,可以隐约看到人影,也是花屏。。。
...全文
552 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgl7903 2015-01-06
  • 打赏
  • 举报
回复

19,469

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧