Blame gst-libs/gst/video/video.c

Packit 971217
/* GStreamer
Packit 971217
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
Packit 971217
 * Library       <2002> Ronald Bultje <rbultje@ronald.bitfreak.net>
Packit 971217
 * Copyright (C) 2007 David A. Schleef <ds@schleef.org>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#  include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include <string.h>
Packit 971217
#include <stdio.h>
Packit 971217
Packit 971217
#include "video.h"
Packit 971217
#include "gstvideometa.h"
Packit 971217
Packit 971217
/**
Packit 971217
 * SECTION:gstvideo
Packit 971217
 * @title: GstVideoAlignment
Packit 971217
 * @short_description: Support library for video operations
Packit 971217
 *
Packit 971217
 * This library contains some helper functions and includes the
Packit 971217
 * videosink and videofilter base classes.
Packit 971217
 *
Packit 971217
 */
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_calculate_display_ratio:
Packit 971217
 * @dar_n: (out): Numerator of the calculated display_ratio
Packit 971217
 * @dar_d: (out): Denominator of the calculated display_ratio
Packit 971217
 * @video_width: Width of the video frame in pixels
Packit 971217
 * @video_height: Height of the video frame in pixels
Packit 971217
 * @video_par_n: Numerator of the pixel aspect ratio of the input video.
Packit 971217
 * @video_par_d: Denominator of the pixel aspect ratio of the input video.
Packit 971217
 * @display_par_n: Numerator of the pixel aspect ratio of the display device
Packit 971217
 * @display_par_d: Denominator of the pixel aspect ratio of the display device
Packit 971217
 *
Packit 971217
 * Given the Pixel Aspect Ratio and size of an input video frame, and the
Packit 971217
 * pixel aspect ratio of the intended display device, calculates the actual
Packit 971217
 * display ratio the video will be rendered with.
Packit 971217
 *
Packit 971217
 * Returns: A boolean indicating success and a calculated Display Ratio in the
Packit 971217
 * dar_n and dar_d parameters.
Packit 971217
 * The return value is FALSE in the case of integer overflow or other error.
Packit 971217
 */
Packit 971217
gboolean
Packit 971217
gst_video_calculate_display_ratio (guint * dar_n, guint * dar_d,
Packit 971217
    guint video_width, guint video_height,
Packit 971217
    guint video_par_n, guint video_par_d,
Packit 971217
    guint display_par_n, guint display_par_d)
Packit 971217
{
Packit 971217
  gint num, den;
Packit 971217
  gint tmp_n, tmp_d;
Packit 971217
Packit 971217
  g_return_val_if_fail (dar_n != NULL, FALSE);
Packit 971217
  g_return_val_if_fail (dar_d != NULL, FALSE);
Packit 971217
Packit 971217
  /* Calculate (video_width * video_par_n * display_par_d) /
Packit 971217
   * (video_height * video_par_d * display_par_n) */
Packit 971217
  if (!gst_util_fraction_multiply (video_width, video_height, video_par_n,
Packit 971217
          video_par_d, &tmp_n, &tmp_d))
Packit 971217
    goto error_overflow;
Packit 971217
Packit 971217
  if (!gst_util_fraction_multiply (tmp_n, tmp_d, display_par_d, display_par_n,
Packit 971217
          &num, &den))
Packit 971217
    goto error_overflow;
Packit 971217
Packit 971217
  g_return_val_if_fail (num > 0, FALSE);
Packit 971217
  g_return_val_if_fail (den > 0, FALSE);
Packit 971217
Packit 971217
  *dar_n = num;
Packit 971217
  *dar_d = den;
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
Packit 971217
  /* ERRORS */
Packit 971217
error_overflow:
Packit 971217
  {
Packit 971217
    GST_WARNING ("overflow in multiply");
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_guess_framerate:
Packit 971217
 * @duration: Nominal duration of one frame
Packit 971217
 * @dest_n: (out) (allow-none): Numerator of the calculated framerate
Packit 971217
 * @dest_d: (out) (allow-none): Denominator of the calculated framerate
Packit 971217
 *
Packit 971217
 * Given the nominal duration of one video frame,
Packit 971217
 * this function will check some standard framerates for
Packit 971217
 * a close match (within 0.1%) and return one if possible,
Packit 971217
 *
Packit 971217
 * It will calculate an arbitrary framerate if no close
Packit 971217
 * match was found, and return %FALSE.
Packit 971217
 *
Packit 971217
 * It returns %FALSE if a duration of 0 is passed.
Packit 971217
 *
Packit 971217
 * Returns: %TRUE if a close "standard" framerate was
Packit 971217
 * recognised, and %FALSE otherwise.
Packit 971217
 *
Packit 971217
 * Since: 1.6
Packit 971217
 */
Packit 971217
gboolean
Packit 971217
gst_video_guess_framerate (GstClockTime duration, gint * dest_n, gint * dest_d)
Packit 971217
{
Packit 971217
  const int common_den[] = { 1, 2, 3, 4, 1001 };
Packit 971217
  int best_n, best_d, gcd;
Packit 971217
  guint64 best_error = G_MAXUINT64;
Packit 971217
  guint64 a;
Packit 971217
  int i;
Packit 971217
Packit 971217
  if (G_UNLIKELY (duration == 0))
Packit 971217
    return FALSE;
Packit 971217
Packit 971217
  /* Use a limited precision conversion by default for more sensible results,
Packit 971217
   * unless the frame duration is absurdly small (high speed cameras?) */
Packit 971217
  if (duration > 100000) {
Packit 971217
    best_n = GST_SECOND / 10000;
Packit 971217
    best_d = duration / 10000;
Packit 971217
  } else {
Packit 971217
    best_n = GST_SECOND;
Packit 971217
    best_d = duration;
Packit 971217
  }
Packit 971217
Packit 971217
  for (i = 0; i < G_N_ELEMENTS (common_den); i++) {
Packit 971217
    gint d = common_den[i];
Packit 971217
    gint n = gst_util_uint64_scale_round (d, GST_SECOND, duration);
Packit 971217
Packit 971217
    /* For NTSC framerates, round to the nearest 1000 fps */
Packit 971217
    if (d == 1001) {
Packit 971217
      n += 500;
Packit 971217
      n -= (n % 1000);
Packit 971217
    }
Packit 971217
Packit 971217
    if (n > 0) {
Packit 971217
      /* See what duration the given framerate should be */
Packit 971217
      a = gst_util_uint64_scale_int (GST_SECOND, d, n);
Packit 971217
      /* Compute absolute error */
Packit 971217
      a = (a < duration) ? (duration - a) : (a - duration);
Packit 971217
      if (a < 2) {
Packit 971217
        /* Really precise - take this option */
Packit 971217
        if (dest_n)
Packit 971217
          *dest_n = n;
Packit 971217
        if (dest_d)
Packit 971217
          *dest_d = d;
Packit 971217
        return TRUE;
Packit 971217
      }
Packit 971217
      /* If within 0.1%, remember this denominator */
Packit 971217
      if (a * 1000 < duration && a < best_error) {
Packit 971217
        best_error = a;
Packit 971217
        best_n = n;
Packit 971217
        best_d = d;
Packit 971217
      }
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  /* set results */
Packit 971217
  gcd = gst_util_greatest_common_divisor (best_n, best_d);
Packit 971217
  if (gcd) {
Packit 971217
    best_n /= gcd;
Packit 971217
    best_d /= gcd;
Packit 971217
  }
Packit 971217
  if (dest_n)
Packit 971217
    *dest_n = best_n;
Packit 971217
  if (dest_d)
Packit 971217
    *dest_d = best_d;
Packit 971217
Packit 971217
  return (best_error != G_MAXUINT64);
Packit 971217
}
Packit 971217
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_alignment_reset:
Packit 971217
 * @align: a #GstVideoAlignment
Packit 971217
 *
Packit 971217
 * Set @align to its default values with no padding and no alignment.
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_video_alignment_reset (GstVideoAlignment * align)
Packit 971217
{
Packit 971217
  gint i;
Packit 971217
Packit 971217
  g_return_if_fail (align != NULL);
Packit 971217
Packit 971217
  align->padding_top = 0;
Packit 971217
  align->padding_bottom = 0;
Packit 971217
  align->padding_left = 0;
Packit 971217
  align->padding_right = 0;
Packit 971217
  for (i = 0; i < GST_VIDEO_MAX_PLANES; i++)
Packit 971217
    align->stride_align[i] = 0;
Packit 971217
}