2016-09-18 31 views
0

我正在嘗試將幀發送到另一個進程以顯示它。我正在使用dranger tutorial02發送SDL幀到另一個進程並顯示它

我正在考慮在調用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

我已經成功地序列化和對其它過程顯示..

方式的系列化是錯誤的,這裏是正確的代碼:

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); 
} 
相關問題