Fixed bug 2595 - Padded, non-contiguous YUV does not display correctly using OpenGL ES 2.0 renderer
authorSam Lantinga <slouken@libsdl.org>
Sat, 21 Jun 2014 12:38:46 -0700
changeset 8890d9a2fa86cd97
parent 8889 26a6243b27c2
child 8891 7b6472f1b7ba
Fixed bug 2595 - Padded, non-contiguous YUV does not display correctly using OpenGL ES 2.0 renderer

Alvin

The new OpenGL ES 2.0 YUV Texture support does not correctly display padded, non-contiguous YUV data.

I am using SDL2 60edb019f0fe (as provided by 'hg id --id') from Mercurial.

The YUV data I am using is provided by the FFMPEG family of libraries. According to FFMPEG's documentation, "The linesize [pitch] may be larger than the size of usable data -- there may be extra padding present for performance reasons."

The dimensions of the video file that I am using are 480x360. What I get from FFMPEG is a Ypitch of 512, and Upitch and Vpitch are both 256.

When I pack new Y, U and V buffers with only the "usable" data (Ypitch is 480 and Upitch and Vpitch are both 240), and use those new buffers, the image is display correctly.

It appears that the Ypitch, Upitch and Vpitch parameters are not being used by SDL_UpdateYUVTexture().

I use SDL_PIXELFORMAT_YV12 for my YUV texture, however, the same results are seen when I use SDL_PIXELFORMAT_IYUV.

Not sure if this is related or not, but when I render the YUV texture (padded and unpadded) to a RGB24 texture, the resulting image is greyscale (or could by just the Y channel).

The URL field for this bug entry is set to my email (SDL mailing list archive) which includes an example image of what I see when rendering padded, non-contiguous YUV data.
src/render/opengles2/SDL_render_gles2.c
     1.1 --- a/src/render/opengles2/SDL_render_gles2.c	Wed Jun 18 20:11:39 2014 +0100
     1.2 +++ b/src/render/opengles2/SDL_render_gles2.c	Sat Jun 21 12:38:46 2014 -0700
     1.3 @@ -581,15 +581,44 @@
     1.4  }
     1.5  
     1.6  static int
     1.7 +GLES2_TexSubImage2D(GLES2_DriverContext *data, GLenum target, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels, GLint pitch, GLint bpp)
     1.8 +{
     1.9 +    Uint8 *blob = NULL;
    1.10 +    Uint8 *src;
    1.11 +    int src_pitch;
    1.12 +    int y;
    1.13 +
    1.14 +    /* Reformat the texture data into a tightly packed array */
    1.15 +    src_pitch = width * bpp;
    1.16 +    src = (Uint8 *)data;
    1.17 +    if (pitch != src_pitch) {
    1.18 +        blob = (Uint8 *)SDL_malloc(src_pitch * height);
    1.19 +        if (!blob) {
    1.20 +            return SDL_OutOfMemory();
    1.21 +        }
    1.22 +        src = blob;
    1.23 +        for (y = 0; y < height; ++y)
    1.24 +        {
    1.25 +            SDL_memcpy(src, pixels, src_pitch);
    1.26 +            src += src_pitch;
    1.27 +            pixels = (Uint8 *)pixels + pitch;
    1.28 +        }
    1.29 +        src = blob;
    1.30 +    }
    1.31 +
    1.32 +    data->glTexSubImage2D(target, 0, xoffset, yoffset, width, height, format, type, src);
    1.33 +    if (blob) {
    1.34 +        SDL_free(blob);
    1.35 +    }
    1.36 +    return 0;
    1.37 +}
    1.38 +
    1.39 +static int
    1.40  GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *rect,
    1.41                      const void *pixels, int pitch)
    1.42  {
    1.43      GLES2_DriverContext *data = (GLES2_DriverContext *)renderer->driverdata;
    1.44      GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
    1.45 -    Uint8 *blob = NULL;
    1.46 -    Uint8 *src;
    1.47 -    int srcPitch;
    1.48 -    int y;
    1.49  
    1.50      GLES2_ActivateRenderer(renderer);
    1.51  
    1.52 @@ -597,36 +626,16 @@
    1.53      if (rect->w <= 0 || rect->h <= 0)
    1.54          return 0;
    1.55  
    1.56 -    /* Reformat the texture data into a tightly packed array */
    1.57 -    srcPitch = rect->w * SDL_BYTESPERPIXEL(texture->format);
    1.58 -    src = (Uint8 *)pixels;
    1.59 -    if (pitch != srcPitch) {
    1.60 -        blob = (Uint8 *)SDL_malloc(srcPitch * rect->h);
    1.61 -        if (!blob) {
    1.62 -            return SDL_OutOfMemory();
    1.63 -        }
    1.64 -        src = blob;
    1.65 -        for (y = 0; y < rect->h; ++y)
    1.66 -        {
    1.67 -            SDL_memcpy(src, pixels, srcPitch);
    1.68 -            src += srcPitch;
    1.69 -            pixels = (Uint8 *)pixels + pitch;
    1.70 -        }
    1.71 -        src = blob;
    1.72 -    }
    1.73 -
    1.74      /* Create a texture subimage with the supplied data */
    1.75      data->glBindTexture(tdata->texture_type, tdata->texture);
    1.76 -    data->glTexSubImage2D(tdata->texture_type,
    1.77 -                    0,
    1.78 +    GLES2_TexSubImage2D(data, tdata->texture_type,
    1.79                      rect->x,
    1.80                      rect->y,
    1.81                      rect->w,
    1.82                      rect->h,
    1.83                      tdata->pixel_format,
    1.84                      tdata->pixel_type,
    1.85 -                    src);
    1.86 -    SDL_free(blob);
    1.87 +                    pixels, pitch, SDL_BYTESPERPIXEL(texture->format));
    1.88  
    1.89      return GL_CheckError("glTexSubImage2D()", renderer);
    1.90  }
    1.91 @@ -641,39 +650,41 @@
    1.92      GLES2_DriverContext *data = (GLES2_DriverContext *)renderer->driverdata;
    1.93      GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
    1.94  
    1.95 +    GLES2_ActivateRenderer(renderer);
    1.96 +
    1.97 +    /* Bail out if we're supposed to update an empty rectangle */
    1.98 +    if (rect->w <= 0 || rect->h <= 0)
    1.99 +        return 0;
   1.100 +
   1.101      data->glBindTexture(tdata->texture_type, tdata->texture_v);
   1.102 -    data->glTexSubImage2D(tdata->texture_type,
   1.103 -                    0,
   1.104 +    GLES2_TexSubImage2D(data, tdata->texture_type,
   1.105                      rect->x,
   1.106                      rect->y,
   1.107                      rect->w / 2,
   1.108                      rect->h / 2,
   1.109                      tdata->pixel_format,
   1.110                      tdata->pixel_type,
   1.111 -                    Vplane);
   1.112 +                    Vplane, Vpitch, 1);
   1.113  
   1.114      data->glBindTexture(tdata->texture_type, tdata->texture_u);
   1.115 -    data->glTexSubImage2D(tdata->texture_type,
   1.116 -                    0,
   1.117 +    GLES2_TexSubImage2D(data, tdata->texture_type,
   1.118                      rect->x,
   1.119                      rect->y,
   1.120                      rect->w / 2,
   1.121                      rect->h / 2,
   1.122                      tdata->pixel_format,
   1.123                      tdata->pixel_type,
   1.124 -                    Uplane);
   1.125 +                    Uplane, Upitch, 1);
   1.126  
   1.127      data->glBindTexture(tdata->texture_type, tdata->texture);
   1.128 -    data->glTexSubImage2D(tdata->texture_type,
   1.129 -                    0,
   1.130 +    GLES2_TexSubImage2D(data, tdata->texture_type,
   1.131                      rect->x,
   1.132                      rect->y,
   1.133                      rect->w,
   1.134                      rect->h,
   1.135                      tdata->pixel_format,
   1.136                      tdata->pixel_type,
   1.137 -                    Yplane);
   1.138 -
   1.139 +                    Vplane, Vpitch, 1);
   1.140  
   1.141      return GL_CheckError("glTexSubImage2D()", renderer);
   1.142  }