A simple video scaler as a library and an Element. Also a little non working mpeg2 to mpeg1 converter (still struggli...

Original commit message from CVS:
A simple video scaler as a library and an Element. Also a little non
working mpeg2 to mpeg1 converter (still struggling with the quantisation).
This commit is contained in:
Wim Taymans 2000-05-12 17:39:40 +00:00
parent 17224d3c8e
commit 2a4bfc0fc8
7 changed files with 1648 additions and 0 deletions

View file

@ -0,0 +1,15 @@
filterdir = $(libdir)/gst
filter_LTLIBRARIES = libgstvideoscale.la
libgstvideoscale_la_SOURCES = gstvideoscale.c
libgstvideoscaleincludedir = $(includedir)/gst/libs/gstvideoscale.h
libgstvideoscaleinclude_HEADERS = gstvideoscale.h
noinst_HEADERS = yuv2rgb.h
CFLAGS += -Wall -O2 -fomit-frame-pointer -funroll-all-loops -finline-functions -ffast-math
INCLUDES = $(GLIB_CFLAGS) $(GTK_CFLAGS) -I$(top_srcdir) -I$(top_srcdir)/include
LDADD = $(GLIB_LIBS) $(GTK_LIBS) $(top_srcdir)/gst/libgst.la

View file

@ -0,0 +1,169 @@
/* Gnome-Streamer
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
*
* 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.
*/
//#define DEBUG_ENABLED
#include <gst/gst.h>
#include <gstvideoscale.h>
#include <gst/meta/videoraw.h>
static void gst_videoscale_scale_plane(unsigned char *src, unsigned char *dest, int sw, int sh, int dw, int dh);
GstBuffer *gst_videoscale_scale(GstBuffer *src, int sw, int sh, int dw, int dh, int format) {
GstBuffer *outbuf;
char *source;
char *dest;
GstMeta *meta;
DEBUG("videoscale: scaling %dx%d to %dx%d\n", sw, sh, dw, dh);
source = GST_BUFFER_DATA(src);
outbuf = gst_buffer_new();
dest = GST_BUFFER_DATA(outbuf) = g_malloc((dw*dh*12)/8);
GST_BUFFER_SIZE(outbuf) = (dw*dh*12)/8;
meta = gst_buffer_get_first_meta(src);
if (meta) {
((MetaVideoRaw *)meta)->width = dw;
((MetaVideoRaw *)meta)->height = dh;
gst_buffer_add_meta(outbuf, meta);
}
gst_videoscale_scale_plane(source, dest, sw, sh, dw, dh);
source += sw*sh;
dest += dw*dh;
dh = dh>>1;
dw = dw>>1;
sh = sh>>1;
sw = sw>>1;
gst_videoscale_scale_plane(source, dest, sw, sh, dw, dh);
source += sw*sh;
dest += dw*dh;
gst_videoscale_scale_plane(source, dest, sw, sh, dw, dh);
gst_buffer_unref(src);
return outbuf;
}
static char gst_videoscale_interp_simple(unsigned char *src, int x, int y, int dw, int dh, int sw, int sh, int ix, int iy) {
unsigned char *isourcep;
int interp;
int i,j;
if (x>0) src--;
if (x>dw-1) src--;
if (y>0) src-=sw;
if (y>dh-1) src-=sw;
isourcep = src;
interp =0;
for (i =0; i<iy; i++) {
for (j =0; j<ix; j++) {
interp += *isourcep++;
}
isourcep = isourcep-ix+sw;
}
return interp/(ix*iy);
}
static char gst_videoscale_interp_other(unsigned char *src, int x, int y, int dw, int dh, int sw, int sh, int ix, int iy) {
unsigned char *isourcep;
int interp;
int i,j;
static int matrix[3][3] = {{1,2,1}, {2,3,2},{1,2,1}};
if (x>0) src--;
if (x>dw-1) src--;
if (y>0) src-=sw;
if (y>dh-1) src-=sw;
isourcep = src;
interp =0;
for (i =0; i<iy; i++) {
for (j =0; j<ix; j++) {
//printf("%d %d %d %d\n", i, j, *isourcep, matrix[i][j]);
interp += matrix[i][j]*(*isourcep++);
}
isourcep = isourcep-ix+sw;
}
//printf("%d\n", interp/15);
return interp/15;
}
static void gst_videoscale_scale_plane(unsigned char *src, unsigned char *dest, int sw, int sh, int dw, int dh) {
int yinc = sh / dh;
int srcys = sh % dh;
int destys = dh;
int dy = 2 * (srcys - destys);
int incyE = 2 * srcys;
int incyNE = 2 * (srcys - destys);
int x, y;
int xinc = sw / dw;
int srcxs = sw % dw;
int destxs = dw;
int incxE = 2 * srcxs;
int incxNE = 2 * (srcxs - destxs);
int dx;
unsigned char *sourcep;
int srcinc = 0;
int xskip, yskip =0;
for (y = 0; y<dh; y++) {
dx = 2 * (srcxs - destxs);
sourcep = src + (srcinc*sw);
for (x = dw; x; x--) {
if (dx <= 0) {
dx += incxE;
xskip = 0;
}
else {
dx += incxNE;
xskip = 1;
sourcep++;
}
sourcep += xinc;
*dest++ = gst_videoscale_interp_other(sourcep, x, y, dw, dh, sw, sh, 3, 3);
}
if (dy <= 0) {
dy += incyE;
yskip = 0;
}
else {
dy += incyNE;
srcinc++;
yskip = 1;
}
srcinc += yinc;
}
}

View file

@ -0,0 +1,30 @@
/* Gnome-Streamer
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
*
* 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.
*/
#ifndef __GST_VIDEOSCALE_H__
#define __GST_VIDEOSCALE_H__
#include <gdk/gdk.h>
#include <gst/gstbuffer.h>
#include <gst/gstplugin.h>
GstBuffer *gst_videoscale_scale(GstBuffer *src, int sw, int sh, int dw, int dh, int format);
#endif /* __GST_VIDEOSCALE_H__ */

88
libs/videoscale/rgb2rgb.c Normal file
View file

@ -0,0 +1,88 @@
/* Gnome-Streamer
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
*
* 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.
*/
//#define DEBUG_ENABLED
#include <gst/gst.h>
#include <gstcolorspace.h>
static GstBuffer *gst_colorspace_rgb24_to_bgr24(GstBuffer *src, GstColorSpaceParameters *params);
static GstBuffer *gst_colorspace_rgb_to_rgb_identity(GstBuffer *src, GstColorSpaceParameters *params);
GstColorSpaceConverter gst_colorspace_rgb2rgb_get_converter(GstColorSpace src, GstColorSpace dest) {
switch(src) {
case GST_COLORSPACE_RGB24:
switch(dest) {
case GST_COLORSPACE_RGB24:
return gst_colorspace_rgb_to_rgb_identity;
case GST_COLORSPACE_BGR24:
return gst_colorspace_rgb24_to_bgr24;
default:
break;
}
break;
case GST_COLORSPACE_BGR24:
switch(dest) {
case GST_COLORSPACE_RGB24:
return gst_colorspace_rgb24_to_bgr24;
case GST_COLORSPACE_BGR24:
return gst_colorspace_rgb_to_rgb_identity;
default:
break;
}
break;
default:
break;
}
g_print("gst_colorspace: conversion not supported\n");
return NULL;
}
static GstBuffer *gst_colorspace_rgb_to_rgb_identity(GstBuffer *src, GstColorSpaceParameters *params) {
return src;
}
static GstBuffer *gst_colorspace_rgb24_to_bgr24(GstBuffer *src, GstColorSpaceParameters *params) {
gint size;
gchar temp;
gchar *data;
DEBUG("gst_colorspace_rgb24_to_bgr24 %d\n", GST_BUFFER_SIZE(src));
size = GST_BUFFER_SIZE(src)/3;
if (params != NULL && params->outbuf != NULL) {
data = params->outbuf;
DEBUG("gst_colorspace: to buffer %p\n", data);
}
else {
data = GST_BUFFER_DATA(src);
}
while (size--) {
temp = data[0];
data[0] = data[2];
data[2] = temp;
data+=3;
}
DEBUG("gst_colorspace_rgb24_to_bgr24 end %d\n", GST_BUFFER_SIZE(src));
return src;
}

1058
libs/videoscale/yuv2rgb.c Normal file

File diff suppressed because it is too large Load diff

64
libs/videoscale/yuv2rgb.h Normal file
View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 1995 The Regents of the University of California.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
typedef struct _GstColorSpaceYUVTables GstColorSpaceYUVTables;
struct _GstColorSpaceYUVTables {
int gammaCorrectFlag;
double gammaCorrect;
int chromaCorrectFlag;
double chromaCorrect;
int *L_tab, *Cr_r_tab, *Cr_g_tab, *Cb_g_tab, *Cb_b_tab;
/*
* We define tables that convert a color value between -256 and 512
* into the R, G and B parts of the pixel. The normal range is 0-255.
**/
long *r_2_pix;
long *g_2_pix;
long *b_2_pix;
};
#define CB_BASE 1
#define CR_BASE (CB_BASE*CB_RANGE)
#define LUM_BASE (CR_BASE*CR_RANGE)
#define Min(x,y) (((x) < (y)) ? (x) : (y))
#define Max(x,y) (((x) > (y)) ? (x) : (y))
#define GAMMA_CORRECTION(x) ((int)(pow((x) / 255.0, 1.0 / gammaCorrect) * 255.0))
#define CHROMA_CORRECTION256(x) ((x) >= 128 \
? 128 + Min(127, (int)(((x) - 128.0) * chromaCorrect)) \
: 128 - Min(128, (int)((128.0 - (x)) * chromaCorrect)))
#define CHROMA_CORRECTION128(x) ((x) >= 0 \
? Min(127, (int)(((x) * chromaCorrect))) \
: Max(-128, (int)(((x) * chromaCorrect))))
#define CHROMA_CORRECTION256D(x) ((x) >= 128 \
? 128.0 + Min(127.0, (((x) - 128.0) * chromaCorrect)) \
: 128.0 - Min(128.0, (((128.0 - (x)) * chromaCorrect))))
#define CHROMA_CORRECTION128D(x) ((x) >= 0 \
? Min(127.0, ((x) * chromaCorrect)) \
: Max(-128.0, ((x) * chromaCorrect)))

224
test/mp2tomp1.c Normal file
View file

@ -0,0 +1,224 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <glib.h>
#include <gst/gst.h>
int fd;
char *outfile;
void eof(GstSrc *src) {
g_print("have eos, quitting\n");
exit(0);
}
void mp2tomp1(GstElement *parser,GstPad *pad, GstElement *pipeline) {
GstElement *parse_audio, *parse_video, *decode, *decode_video, *play, *encode;
GstElement *audio_queue, *video_queue;
GstElement *audio_thread, *video_thread;
GstElement *videoscale;
GstElement *fdsink;
GstElementFactory *fdsinkfactory;
g_print("***** a new pad %s was created\n", gst_pad_get_name(pad));
// connect to audio pad
if (0) {
//if (strncmp(gst_pad_get_name(pad), "private_stream_1.0", 18) == 0) {
gst_plugin_load("ac3parse");
gst_plugin_load("ac3dec");
// construct internal pipeline elements
parse_audio = gst_elementfactory_make("ac3parse","parse_audio");
g_return_if_fail(parse_audio != NULL);
decode = gst_elementfactory_make("ac3dec","decode_audio");
g_return_if_fail(decode != NULL);
play = gst_elementfactory_make("audiosink","play_audio");
g_return_if_fail(play != NULL);
// create the thread and pack stuff into it
audio_thread = gst_thread_new("audio_thread");
g_return_if_fail(audio_thread != NULL);
gst_bin_add(GST_BIN(audio_thread),GST_ELEMENT(parse_audio));
gst_bin_add(GST_BIN(audio_thread),GST_ELEMENT(decode));
gst_bin_add(GST_BIN(audio_thread),GST_ELEMENT(play));
// set up pad connections
gst_element_add_ghost_pad(GST_ELEMENT(audio_thread),
gst_element_get_pad(parse_audio,"sink"));
gst_pad_connect(gst_element_get_pad(parse_audio,"src"),
gst_element_get_pad(decode,"sink"));
gst_pad_connect(gst_element_get_pad(decode,"src"),
gst_element_get_pad(play,"sink"));
// construct queue and connect everything in the main pipelie
audio_queue = gst_elementfactory_make("queue","audio_queue");
gtk_object_set(GTK_OBJECT(audio_queue),"max_level",30,NULL);
gst_bin_add(GST_BIN(pipeline),GST_ELEMENT(audio_queue));
gst_bin_add(GST_BIN(pipeline),GST_ELEMENT(audio_thread));
gst_pad_connect(pad,
gst_element_get_pad(audio_queue,"sink"));
gst_pad_connect(gst_element_get_pad(audio_queue,"src"),
gst_element_get_pad(audio_thread,"sink"));
// set up thread state and kick things off
gtk_object_set(GTK_OBJECT(audio_thread),"create_thread",TRUE,NULL);
g_print("setting to RUNNING state\n");
gst_element_set_state(GST_ELEMENT(audio_thread),GST_STATE_RUNNING);
g_print("setting to PLAYING state\n");
gst_element_set_state(GST_ELEMENT(audio_thread),GST_STATE_PLAYING);
} else if (strncmp(gst_pad_get_name(pad), "audio_", 6) == 0) {
gst_plugin_load("mp3parse");
gst_plugin_load("mpg123");
// construct internal pipeline elements
parse_audio = gst_elementfactory_make("mp3parse","parse_audio");
g_return_if_fail(parse_audio != NULL);
decode = gst_elementfactory_make("mpg123","decode_audio");
g_return_if_fail(decode != NULL);
play = gst_elementfactory_make("audiosink","play_audio");
g_return_if_fail(play != NULL);
// create the thread and pack stuff into it
audio_thread = gst_thread_new("audio_thread");
g_return_if_fail(audio_thread != NULL);
gst_bin_add(GST_BIN(audio_thread),GST_ELEMENT(parse_audio));
gst_bin_add(GST_BIN(audio_thread),GST_ELEMENT(decode));
gst_bin_add(GST_BIN(audio_thread),GST_ELEMENT(play));
// set up pad connections
gst_element_add_ghost_pad(GST_ELEMENT(audio_thread),
gst_element_get_pad(parse_audio,"sink"));
gst_pad_connect(gst_element_get_pad(parse_audio,"src"),
gst_element_get_pad(decode,"sink"));
gst_pad_connect(gst_element_get_pad(decode,"src"),
gst_element_get_pad(play,"sink"));
// construct queue and connect everything in the main pipelie
audio_queue = gst_elementfactory_make("queue","audio_queue");
gtk_object_set(GTK_OBJECT(audio_queue),"max_level",30,NULL);
gst_bin_add(GST_BIN(pipeline),GST_ELEMENT(audio_queue));
gst_bin_add(GST_BIN(pipeline),GST_ELEMENT(audio_thread));
gst_pad_connect(pad,
gst_element_get_pad(audio_queue,"sink"));
gst_pad_connect(gst_element_get_pad(audio_queue,"src"),
gst_element_get_pad(audio_thread,"sink"));
// set up thread state and kick things off
gtk_object_set(GTK_OBJECT(audio_thread),"create_thread",TRUE,NULL);
g_print("setting to RUNNING state\n");
gst_element_set_state(GST_ELEMENT(audio_thread),GST_STATE_RUNNING);
g_print("setting to PLAYING state\n");
gst_element_set_state(GST_ELEMENT(audio_thread),GST_STATE_PLAYING);
} else if (strncmp(gst_pad_get_name(pad), "video_", 6) == 0) {
//} else if (0) {
gst_plugin_load("mp1videoparse");
gst_plugin_load("mpeg2play");
gst_plugin_load("videoscale");
gst_plugin_load("mpeg2enc");
// construct internal pipeline elements
parse_video = gst_elementfactory_make("mp1videoparse","parse_video");
g_return_if_fail(parse_video != NULL);
decode_video = gst_elementfactory_make("mpeg2play","decode_video");
g_return_if_fail(decode_video != NULL);
videoscale = gst_elementfactory_make("videoscale","videoscale");
g_return_if_fail(videoscale != NULL);
encode = gst_elementfactory_make("mpeg2enc","encode");
g_return_if_fail(encode != NULL);
//gtk_object_set(GTK_OBJECT(show),"width",640, "height", 480,NULL);
fd = open(outfile,O_CREAT|O_RDWR|O_TRUNC);
fdsinkfactory = gst_elementfactory_find("fdsink");
g_return_if_fail(fdsinkfactory != NULL);
fdsink = gst_elementfactory_create(fdsinkfactory,"fdsink");
g_return_if_fail(fdsink != NULL);
gtk_object_set(GTK_OBJECT(fdsink),"fd",fd,NULL);
// create the thread and pack stuff into it
video_thread = gst_thread_new("video_thread");
g_return_if_fail(video_thread != NULL);
gst_bin_add(GST_BIN(video_thread),GST_ELEMENT(parse_video));
gst_bin_add(GST_BIN(video_thread),GST_ELEMENT(decode_video));
gst_bin_add(GST_BIN(video_thread),GST_ELEMENT(videoscale));
gst_bin_add(GST_BIN(video_thread),GST_ELEMENT(encode));
gst_bin_add(GST_BIN(video_thread),GST_ELEMENT(fdsink));
// set up pad connections
gst_element_add_ghost_pad(GST_ELEMENT(video_thread),
gst_element_get_pad(parse_video,"sink"));
gst_pad_connect(gst_element_get_pad(parse_video,"src"),
gst_element_get_pad(decode_video,"sink"));
gst_pad_connect(gst_element_get_pad(decode_video,"src"),
gst_element_get_pad(videoscale,"sink"));
gst_pad_connect(gst_element_get_pad(videoscale,"src"),
gst_element_get_pad(encode,"sink"));
gst_pad_connect(gst_element_get_pad(encode,"src"),
gst_element_get_pad(fdsink,"sink"));
// construct queue and connect everything in the main pipeline
video_queue = gst_elementfactory_make("queue","video_queue");
gtk_object_set(GTK_OBJECT(video_queue),"max_level",30,NULL);
gst_bin_add(GST_BIN(pipeline),GST_ELEMENT(video_queue));
gst_bin_add(GST_BIN(pipeline),GST_ELEMENT(video_thread));
gst_pad_connect(pad,
gst_element_get_pad(video_queue,"sink"));
gst_pad_connect(gst_element_get_pad(video_queue,"src"),
gst_element_get_pad(video_thread,"sink"));
// set up thread state and kick things off
gtk_object_set(GTK_OBJECT(video_thread),"create_thread",TRUE,NULL);
g_print("setting to RUNNING state\n");
gst_element_set_state(GST_ELEMENT(video_thread),GST_STATE_RUNNING);
g_print("setting to PLAYING state\n");
gst_element_set_state(GST_ELEMENT(video_thread),GST_STATE_PLAYING);
}
g_print("\n");
}
int main(int argc,char *argv[]) {
GstPipeline *pipeline;
GstElement *src, *parse, *out;
GstPad *infopad;
int i,c;
g_print("have %d args\n",argc);
gst_init(&argc,&argv);
gst_plugin_load("mpeg2parse");
pipeline = gst_pipeline_new("pipeline");
g_return_if_fail(pipeline != NULL);
if (strstr(argv[1],"video_ts")) {
src = gst_elementfactory_make("dvdsrc","src");
g_print("using DVD source\n");
} else
src = gst_elementfactory_make("disksrc","src");
g_return_if_fail(src != NULL);
gtk_object_set(GTK_OBJECT(src),"location",argv[1],NULL);
g_print("should be using file '%s'\n",argv[1]);
g_print("should be using output file '%s'\n",argv[2]);
outfile = argv[2];
parse = gst_elementfactory_make("mpeg2parse","parse");
g_return_if_fail(parse != NULL);
gst_bin_add(GST_BIN(pipeline),GST_ELEMENT(src));
gst_bin_add(GST_BIN(pipeline),GST_ELEMENT(parse));
gtk_signal_connect(GTK_OBJECT(parse),"new_pad",mp2tomp1, pipeline);
gtk_signal_connect(GTK_OBJECT(src),"eos",GTK_SIGNAL_FUNC(eof),NULL);
gst_pad_connect(gst_element_get_pad(src,"src"),
gst_element_get_pad(parse,"sink"));
g_print("setting to RUNNING state\n");
gst_element_set_state(GST_ELEMENT(pipeline),GST_STATE_RUNNING);
while (1) {
gst_src_push(GST_SRC(src));
}
}