y4mreader.c 5.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * y4mreader.c - Y4M parser
 *
 * Copyright (C) 2015 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 */

#include "y4mreader.h"

#include <stdio.h>

/* format documentation:
 * http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 */

static inline gboolean
30
parse_int (const gchar * str, guint * out_value_ptr)
31 32 33 34 35 36 37 38
{
  gint saved_errno;
  glong value;
  gboolean ret;

  if (!str)
    return FALSE;
  str += 1;
39
  if (*str == '\0')
40 41 42 43 44 45 46
    return FALSE;

  saved_errno = errno;
  errno = 0;
  value = strtol (str, NULL, 0);
  ret = (errno == 0);
  errno = saved_errno;
47 48 49 50
  if (value > 0 && value <= G_MAXUINT)
    *out_value_ptr = value;
  else
    ret = FALSE;
51 52 53 54 55 56 57

  return ret;
}

static gboolean
parse_header (Y4MReader * file)
{
58
  gint i, j, b;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  guint8 header[BUFSIZ];
  size_t s;
  gchar *str;

  memset (header, 0, BUFSIZ);
  s = fread (header, 1, 9, file->fp);
  if (s < 9)
    return FALSE;

  if (memcmp (header, "YUV4MPEG2", 9) != 0)
    return FALSE;

  for (i = 9; i < BUFSIZ - 1; i++) {
    b = fgetc (file->fp);
    if (b == EOF)
      return FALSE;
    if (b == 0xa)
      break;
    header[i] = b;
  }

  if (i == BUFSIZ - 1)
    return FALSE;

  j = 9;
  while (j < i) {
    if ((header[j] != 0x20) && (header[j - 1] == 0x20)) {
      switch (header[j]) {
        case 'W':
88
          if (!parse_int ((gchar *) & header[j], &file->width))
89 90 91
            return FALSE;
          break;
        case 'H':
92
          if (!parse_int ((gchar *) & header[j], &file->height))
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
            return FALSE;
          break;
        case 'C':
          str = (char *) &header[j + 1];
          if (strncmp (str, "420", 3) != 0) {
            g_warning ("Unsupported chroma subsampling.");
            return FALSE;       /* unsupported chroma subsampling */
          }
          break;
        case 'I':
          str = (char *) &header[j + 1];
          if (*str != 'p' && *str != '?') {
            g_warning ("Interlaced content are not supported.");
            return FALSE;       /* interlaced is unsupported */
          }
          break;
        case 'F':              /* frame rate ratio */
        {
          guint num, den;

113
          if (!parse_int ((gchar *) & header[j], &num))
114 115 116
            return FALSE;
          while ((header[j] != ':') && (j < i))
            j++;
117
          if (!parse_int ((gchar *) & header[j], &den))
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
            return FALSE;

          if (num <= 0 || den <= 0) {
            file->fps_n = 30;   /* default to 30 fps */
            file->fps_d = 1;
          } else {
            file->fps_n = num;
            file->fps_d = den;
          }
          break;
        }
        case 'A':              /* sample aspect ration */
          break;
        case 'X':              /* metadata */
          break;
        default:
          break;
      }
    }
    j++;
  }

  return TRUE;
}

Y4MReader *
y4m_reader_open (const gchar * filename)
{
  Y4MReader *imagefile;

  imagefile = g_slice_new0 (Y4MReader);

  if (filename) {
    imagefile->fp = fopen (filename, "r");
    if (!imagefile->fp) {
      g_warning ("open file %s error", filename);
      goto bail;
    }
  } else {
    imagefile->fp = stdin;
  }

  if (!parse_header (imagefile))
    goto bail;

  return imagefile;

bail:
  if (imagefile->fp && imagefile->fp != stdin)
    fclose (imagefile->fp);

  g_slice_free (Y4MReader, imagefile);
  return NULL;
}

void
y4m_reader_close (Y4MReader * file)
{
  g_return_if_fail (file);

  if (file->fp && file->fp != stdin)
    fclose (file->fp);

  g_slice_free (Y4MReader, file);
}

static gboolean
skip_frame_header (Y4MReader * file)
{
187
  gint i, b;
188 189 190 191 192 193 194 195 196 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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  guint8 header[BUFSIZ];
  size_t s;

  memset (header, 0, BUFSIZ);
  s = fread (header, 1, 5, file->fp);
  if (s < 5)
    return FALSE;

  if (memcmp (header, "FRAME", 5) != 0)
    return FALSE;

  for (i = 5; i < BUFSIZ - 1; i++) {
    b = fgetc (file->fp);
    if (b == EOF)
      return FALSE;
    if (b == 0xa)
      break;
    header[i] = b;
  }

  return (i < BUFSIZ - 1);
}

gboolean
y4m_reader_load_image (Y4MReader * file, GstVaapiImage * image)
{
  guint8 *plane;
  size_t s;
  guint frame_size, stride, i;

  g_return_val_if_fail (gst_vaapi_image_is_mapped (image), FALSE);
  g_return_val_if_fail (file && file->fp, FALSE);

  /* only valid for I420 */
  frame_size = file->height * file->width * 3 / 2;
  if (gst_vaapi_image_get_data_size (image) < frame_size)
    return FALSE;
  if (gst_vaapi_image_get_plane_count (image) != 3)
    return FALSE;

  if (!skip_frame_header (file))
    return FALSE;

  /* Y plane */
  plane = gst_vaapi_image_get_plane (image, 0);
  stride = gst_vaapi_image_get_pitch (image, 0);
  for (i = 0; i < file->height; i++) {
    s = fread (plane, 1, file->width, file->fp);
    if (s != file->width)
      return FALSE;
    plane += stride;
  }

  /* U plane */
  plane = gst_vaapi_image_get_plane (image, 1);
  stride = gst_vaapi_image_get_pitch (image, 1);
  for (i = 0; i < file->height / 2; i++) {
    s = fread (plane, 1, file->width / 2, file->fp);
    if (s != file->width / 2)
      return FALSE;
    plane += stride;
  }

  /* V plane */
  plane = gst_vaapi_image_get_plane (image, 2);
  stride = gst_vaapi_image_get_pitch (image, 2);
  for (i = 0; i < file->height / 2; i++) {
    s = fread (plane, 1, file->width / 2, file->fp);
    if (s != file->width / 2)
      return FALSE;
    plane += stride;
  }

  return TRUE;
}