gstxvid.c 8.06 KB
Newer Older
1
/* GStreamer xvid encoder/decoder plugin
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

24
#include <string.h>
25
#include <xvid.h>
26

27
#include <gst/video/video.h>
28 29 30
#include "gstxviddec.h"
#include "gstxvidenc.h"

31 32 33
gboolean
gst_xvid_init (void)
{
34
  static xvid_gbl_init_t xinit;
35 36 37 38 39 40 41 42 43
  gint ret;
  static gboolean is_init = FALSE;

  /* only init once */
  if (is_init == TRUE) {
    return TRUE;
  }

  /* set up xvid initially (function pointers, CPU flags) */
44
  gst_xvid_init_struct (xinit);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
45 46
  if ((ret = xvid_global (NULL, XVID_GBL_INIT, &xinit, NULL)) < 0) {
    g_warning ("Failed to initialize XviD: %s (%d)", gst_xvid_error (ret), ret);
47 48
    return FALSE;
  }
49 50

  GST_LOG ("Initted XviD version %d.%d.%d (API %d.%d)",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
51 52 53 54
      XVID_VERSION_MAJOR (XVID_VERSION),
      XVID_VERSION_MINOR (XVID_VERSION),
      XVID_VERSION_PATCH (XVID_VERSION),
      XVID_API_MAJOR (XVID_API), XVID_API_MINOR (XVID_API));
55 56 57 58 59

  is_init = TRUE;
  return TRUE;
}

60 61 62 63 64 65 66 67 68
gchar *
gst_xvid_error (int errorcode)
{
  gchar *error;

  switch (errorcode) {
    case XVID_ERR_FAIL:
      error = "Operation failed";
      break;
69
    case 0:
70 71 72
      error = "No error";
      break;
    case XVID_ERR_MEMORY:
73
      error = "Memory allocation error";
74 75
      break;
    case XVID_ERR_FORMAT:
76 77 78 79
      error = "File format not supported";
      break;
    case XVID_ERR_VERSION:
      error = "Structure version not supported";
80 81 82 83 84 85 86 87 88
      break;
    default:
      error = "Unknown error";
      break;
  }

  return error;
}

89
gint
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
90 91
gst_xvid_structure_to_csp (GstStructure * structure,
    gint w, gint * _stride, gint * _bpp)
92 93 94 95 96 97 98 99 100
{
  const gchar *mime = gst_structure_get_name (structure);
  gint xvid_cs = -1, stride = -1, bpp = -1;

  if (!strcmp (mime, "video/x-raw-yuv")) {
    guint32 fourcc;

    gst_structure_get_fourcc (structure, "format", &fourcc);
    switch (fourcc) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
101
      case GST_MAKE_FOURCC ('I', '4', '2', '0'):
102 103 104 105
        xvid_cs = XVID_CSP_I420;
        stride = w;
        bpp = 12;
        break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
106
      case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
107 108 109 110
        xvid_cs = XVID_CSP_YUY2;
        stride = w * 2;
        bpp = 16;
        break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
111
      case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
112 113 114 115
        xvid_cs = XVID_CSP_YV12;
        stride = w;
        bpp = 12;
        break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
116
      case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
117 118 119 120
        xvid_cs = XVID_CSP_UYVY;
        stride = w * 2;
        bpp = 16;
        break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
121
      case GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U'):
122 123 124 125
        xvid_cs = XVID_CSP_YVYU;
        stride = w * 2;
        bpp = 16;
        break;
126 127 128 129
    }
  } else {
    gint depth, r_mask;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
130 131 132
    gst_structure_get_int (structure, "depth", &depth);
    gst_structure_get_int (structure, "bpp", &bpp);
    gst_structure_get_int (structure, "red_mask", &r_mask);
133 134 135

    switch (depth) {
      case 15:
136 137
        xvid_cs = XVID_CSP_RGB555;
        break;
138
      case 16:
139 140
        xvid_cs = XVID_CSP_RGB565;
        break;
141
      case 24:
142 143 144 145 146 147 148
        if (bpp == 24) {
          xvid_cs = XVID_CSP_BGR;
        } else {
          switch (r_mask) {
            case 0xff000000:
              xvid_cs = XVID_CSP_RGBA;
              break;
149
#ifdef XVID_CSP_ARGB
150 151 152
            case 0x00ff0000:
              xvid_cs = XVID_CSP_ARGB;
              break;
153
#endif
154 155 156 157 158 159 160 161 162
            case 0x0000ff00:
              xvid_cs = XVID_CSP_BGRA;
              break;
            case 0x000000ff:
              xvid_cs = XVID_CSP_ABGR;
              break;
          }
        }
        break;
163
      default:
164
        break;
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    }

    stride = w * bpp / 8;
  }

  if (_stride)
    *_stride = stride;
  if (_bpp)
    *_bpp = bpp;

  return xvid_cs;
}

GstCaps *
gst_xvid_csp_to_caps (gint csp, gint w, gint h, gdouble fps)
{
  GstCaps *caps = NULL;

  switch (csp) {
    case XVID_CSP_RGB555:
    case XVID_CSP_RGB565:
    case XVID_CSP_BGR:
    case XVID_CSP_ABGR:
    case XVID_CSP_BGRA:
189
#ifdef XVID_CSP_ARGB
190
    case XVID_CSP_ARGB:
191
#endif
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
192
    case XVID_CSP_RGBA:{
193
      gint r_mask = 0, b_mask = 0, g_mask = 0,
194
          endianness = 0, bpp = 0, depth = 0;
195 196

      switch (csp) {
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        case XVID_CSP_RGB555:
          r_mask = GST_VIDEO_RED_MASK_15_INT;
          g_mask = GST_VIDEO_GREEN_MASK_15_INT;
          b_mask = GST_VIDEO_BLUE_MASK_15_INT;
          endianness = G_BYTE_ORDER;
          depth = 15;
          bpp = 16;
          break;
        case XVID_CSP_RGB565:
          r_mask = GST_VIDEO_RED_MASK_16_INT;
          g_mask = GST_VIDEO_GREEN_MASK_16_INT;
          b_mask = GST_VIDEO_BLUE_MASK_16_INT;
          endianness = G_BYTE_ORDER;
          depth = 16;
          bpp = 16;
          break;
        case XVID_CSP_BGR:
          r_mask = 0x0000ff;
          g_mask = 0x00ff00;
          b_mask = 0xff0000;
          endianness = G_BIG_ENDIAN;
          depth = 24;
          bpp = 24;
          break;
        case XVID_CSP_ABGR:
          r_mask = 0x000000ff;
          g_mask = 0x0000ff00;
          b_mask = 0x00ff0000;
          endianness = G_BIG_ENDIAN;
          depth = 24;
          bpp = 32;
          break;
        case XVID_CSP_BGRA:
          r_mask = 0x0000ff00;
          g_mask = 0x00ff0000;
          b_mask = 0xff000000;
          endianness = G_BIG_ENDIAN;
          depth = 24;
          bpp = 32;
          break;
237
#ifdef XVID_CSP_ARGB
238 239 240 241 242 243 244 245
        case XVID_CSP_ARGB:
          r_mask = 0x00ff0000;
          g_mask = 0x0000ff00;
          b_mask = 0x000000ff;
          endianness = G_BIG_ENDIAN;
          depth = 24;
          bpp = 32;
          break;
246
#endif
247 248 249 250 251 252 253 254
        case XVID_CSP_RGBA:
          r_mask = 0xff000000;
          g_mask = 0x00ff0000;
          b_mask = 0x0000ff00;
          endianness = G_BIG_ENDIAN;
          depth = 24;
          bpp = 32;
          break;
255 256 257
      }

      caps = gst_caps_new_simple ("video/x-raw-rgb",
258 259 260 261 262 263 264 265 266
          "width", G_TYPE_INT, w,
          "height", G_TYPE_INT, h,
          "depth", G_TYPE_INT, depth,
          "bpp", G_TYPE_INT, bpp,
          "endianness", G_TYPE_INT, endianness,
          "red_mask", G_TYPE_INT, r_mask,
          "green_mask", G_TYPE_INT, g_mask,
          "blue_mask", G_TYPE_INT, b_mask,
          "framerate", G_TYPE_DOUBLE, fps, NULL);
267 268 269 270 271 272 273
      break;
    }

    case XVID_CSP_YUY2:
    case XVID_CSP_YVYU:
    case XVID_CSP_UYVY:
    case XVID_CSP_I420:
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
274
    case XVID_CSP_YV12:{
275 276 277
      guint32 fourcc = 0;

      switch (csp) {
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
        case XVID_CSP_YUY2:
          fourcc = GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
          break;
        case XVID_CSP_YVYU:
          fourcc = GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U');
          break;
        case XVID_CSP_UYVY:
          fourcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
          break;
        case XVID_CSP_I420:
          fourcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
          break;
        case XVID_CSP_YV12:
          fourcc = GST_MAKE_FOURCC ('Y', 'V', '1', '2');
          break;
293 294 295
      }

      caps = gst_caps_new_simple ("video/x-raw-yuv",
296 297 298 299
          "width", G_TYPE_INT, w,
          "height", G_TYPE_INT, h,
          "format", GST_TYPE_FOURCC, fourcc,
          "framerate", G_TYPE_DOUBLE, fps, NULL);
300 301 302 303 304 305 306 307
      break;
    }
  }

  return caps;
}


308
static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
309
plugin_init (GstPlugin * plugin)
310
{
311
  return (gst_element_register (plugin, "xvidenc",
312
          GST_RANK_NONE, GST_TYPE_XVIDENC) &&
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
313
      gst_element_register (plugin, "xviddec",
314
          GST_RANK_SECONDARY, GST_TYPE_XVIDDEC));
315 316
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
317 318 319 320
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "xvid",
    "XviD plugin library", plugin_init, VERSION, "GPL", GST_PACKAGE, GST_ORIGIN)