我正在尝试将帧发送到另一个进程以显示它。我使用dranger tutorial02。
我想在调用sws_scale之后将SDL_Overlay结构序列化为字节,将其发送到另一个进程,对其进行反序列化并调用SDL_DisplayYUVOverlay来显示它。
你认为这是我最好的选择吗?
如果是这样,我很难序列化这个结构..这里的代码是:
size_t size_of_Overlay(SDL_Overlay *bmp) {
/*
* typedef struct {
*
* Uint32 format;
* int w, h;
* int planes;
* Uint16 *pitches;
* Uint8 **pixels;
* Uint32 hw_overlay:1; <- can I ignore it? cant point to a bit-field..
*
* } SDL_Overlay;
*/
// w,h,planes format pitches pixels
return sizeof(int)*3 + sizeof(Uint32) + sizeof(Uint16)*bmp->w + sizeof(Uint8)*bmp->h*3;
}
void overlay_to_buf(SDL_Overlay* bmp, char* buf) {
if(!bmp || !buf) {
perror("overlay_to_buf");
exit(1);
}
memcpy(buf, &bmp->format, sizeof(Uint32));
buf += sizeof(Uint32);
memcpy(buf, &bmp->w, sizeof(int));
buf += sizeof(int);
memcpy(buf, &bmp->h, sizeof(int));
buf += sizeof(int);
memcpy(buf, &bmp->planes, sizeof(int));
buf += sizeof(int);
memcpy(buf, bmp->pitches, sizeof(Uint16)*bmp->w);
buf += sizeof(Uint16)*bmp->w;
memcpy(buf, bmp->pixels[0], sizeof(Uint8)*bmp->h);
buf += sizeof(Uint8)*bmp->h;
memcpy(buf, bmp->pixels[1], sizeof(Uint8)*bmp->h);
buf += sizeof(Uint8)*bmp->h;
memcpy(buf, bmp->pixels[2], sizeof(Uint8)*bmp->h);
buf += sizeof(Uint8)*bmp->h;
}
void buf_to_overlay(SDL_Overlay *bmp, char* buf) {
if(!bmp || !buf) {
perror("buf_to_overlay");
exit(1);
}
memcpy(&bmp->format, buf, sizeof(Uint32));
buf += sizeof(Uint32);
memcpy(&bmp->w, buf, sizeof(int));
buf += sizeof(int);
memcpy(&bmp->h, buf, sizeof(int));
buf += sizeof(int);
memcpy(&bmp->planes, buf, sizeof(int));
buf += sizeof(int);
bmp->pitches = (Uint16*)malloc(sizeof(Uint16)*bmp->w);
memcpy(bmp->pitches, buf, sizeof(Uint16)*bmp->w);
buf += sizeof(Uint16)*bmp->w;
bmp->pixels[0] = (Uint8*)malloc(sizeof(Uint8)*bmp->h);
memcpy(bmp->pixels[0], buf, sizeof(Uint8)*bmp->h);
buf += sizeof(Uint8)*bmp->h;
bmp->pixels[1] = (Uint8*)malloc(sizeof(Uint8)*bmp->h);
memcpy(bmp->pixels[1], buf, sizeof(Uint8)*bmp->h);
buf += sizeof(Uint8)*bmp->h;
bmp->pixels[2] = (Uint8*)malloc(sizeof(Uint8)*bmp->h);
memcpy(bmp->pixels[2], buf, sizeof(Uint8)*bmp->h);
buf += sizeof(Uint8)*bmp->h;
}
答案 0 :(得分:0)
我已成功将其序列化并显示在其他进程上..
序列化的方式是错误的,这里是正确的代码:
size_t size_of_Overlay(SDL_Overlay *bmp) {
/*
* typedef struct {
*
* Uint32 format;
* int w, h;
* int planes;
* Uint16 *pitches;
* Uint8 **pixels;
* Uint32 hw_overlay:1; <- doesn't being accounted
*
* } SDL_Overlay;
*/
return sizeof(int)*3 + sizeof(Uint32) + sizeof(Uint16)*3 + sizeof(Uint8)*bmp->h*bmp->w*3;
}
void overlay_to_buf(SDL_Overlay* bmp, char* buf) {
if(!bmp || !buf) {
perror("overlay_to_buf");
exit(-1);
}
memcpy(buf, &bmp->format, sizeof(Uint32));
buf += sizeof(Uint32);
memcpy(buf, &bmp->w, sizeof(int));
buf += sizeof(int);
memcpy(buf, &bmp->h, sizeof(int));
buf += sizeof(int);
memcpy(buf, &bmp->planes, sizeof(int));
buf += sizeof(int);
memcpy(buf, &bmp->pitches[0], sizeof(Uint16));
buf += sizeof(Uint16);
memcpy(buf, &bmp->pitches[1], sizeof(Uint16));
buf += sizeof(Uint16);
memcpy(buf, &bmp->pitches[2], sizeof(Uint16));
buf += sizeof(Uint16);
memcpy(buf, bmp->pixels[0], sizeof(Uint8)*bmp->h*bmp->w);
buf += sizeof(Uint8)*bmp->h*bmp->w;
memcpy(buf, bmp->pixels[1], sizeof(Uint8)*bmp->h*bmp->w);
buf += sizeof(Uint8)*bmp->h*bmp->w;
memcpy(buf, bmp->pixels[2], sizeof(Uint8)*bmp->h*bmp->w);
}
void buf_to_overlay(SDL_Overlay *bmp, char* buf) {
if(!bmp || !buf) {
perror("to_message");
exit(-1);
}
memcpy(&bmp->format, buf, sizeof(Uint32));
buf += sizeof(Uint32);
memcpy(&bmp->w, buf, sizeof(int));
buf += sizeof(int);
memcpy(&bmp->h, buf, sizeof(int));
buf += sizeof(int);
memcpy(&bmp->planes, buf, sizeof(int));
buf += sizeof(int);
memcpy(&bmp->pitches[0], buf, sizeof(Uint16));
buf += sizeof(Uint16);
memcpy(&bmp->pitches[1], buf, sizeof(Uint16));
buf += sizeof(Uint16);
memcpy(&bmp->pitches[2], buf, sizeof(Uint16));
buf += sizeof(Uint16);
/* pixels are allocated outside the function,
* just one allocation during the whole video since
* the screen size doesn't change.
*/
memcpy(bmp->pixels[0], buf, sizeof(Uint8)*bmp->h*bmp->w);
buf += sizeof(Uint8)*bmp->h*bmp->w;
memcpy(bmp->pixels[1], buf, sizeof(Uint8)*bmp->h*bmp->w);
buf += sizeof(Uint8)*bmp->h*bmp->w;
memcpy(bmp->pixels[2], buf, sizeof(Uint8)*bmp->h*bmp->w);
}