2003-12-11 21:07:25 +00:00
|
|
|
/* GStreamer mpeg2enc (mjpegtools) wrapper
|
|
|
|
* (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
|
|
|
|
*
|
|
|
|
* gstmpeg2encpicturereader.cc: GStreamer/mpeg2enc input wrapper
|
|
|
|
*
|
|
|
|
* 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
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2003-12-11 21:07:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <encoderparams.hh>
|
2008-03-21 14:50:41 +00:00
|
|
|
#include <string.h>
|
2003-12-11 21:07:25 +00:00
|
|
|
|
2006-07-13 11:06:45 +00:00
|
|
|
#include "gstmpeg2enc.hh"
|
2003-12-11 21:07:25 +00:00
|
|
|
#include "gstmpeg2encpicturereader.hh"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class init stuff.
|
|
|
|
*/
|
|
|
|
|
2006-07-13 11:06:45 +00:00
|
|
|
GstMpeg2EncPictureReader::GstMpeg2EncPictureReader (GstElement * in_element, GstCaps * in_caps, EncoderParams * params):
|
2004-03-14 23:20:41 +00:00
|
|
|
PictureReader (*params)
|
2003-12-11 21:07:25 +00:00
|
|
|
{
|
2006-07-13 11:06:45 +00:00
|
|
|
element = in_element;
|
|
|
|
gst_object_ref (element);
|
|
|
|
caps = in_caps;
|
|
|
|
gst_caps_ref (caps);
|
2003-12-11 21:07:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstMpeg2EncPictureReader::~GstMpeg2EncPictureReader ()
|
|
|
|
{
|
2006-07-13 11:06:45 +00:00
|
|
|
gst_caps_unref (caps);
|
|
|
|
gst_object_unref (element);
|
2003-12-11 21:07:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get input picture parameters (width/height etc.).
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2004-03-14 23:20:41 +00:00
|
|
|
GstMpeg2EncPictureReader::StreamPictureParams (MPEG2EncInVidParams & strm)
|
2003-12-11 21:07:25 +00:00
|
|
|
{
|
Fix caps breakage after Dave's caps branch merge.
Original commit message from CVS:
2003-12-23 Ronald Bultje <rbultje@ronald.bitfreak.net>
* ext/divx/gstdivxdec.c: (gst_divxdec_base_init),
(gst_divxdec_init), (gst_divxdec_negotiate):
* ext/divx/gstdivxdec.h:
* ext/divx/gstdivxenc.c: (gst_divxenc_base_init),
(gst_divxenc_init):
* ext/faac/gstfaac.c: (gst_faac_base_init), (gst_faac_init),
(gst_faac_sinkconnect), (gst_faac_srcconnect):
* ext/mpeg2enc/gstmpeg2enc.cc:
* ext/mpeg2enc/gstmpeg2encoder.cc:
* ext/mpeg2enc/gstmpeg2encpicturereader.cc:
* sys/dxr3/dxr3audiosink.c: (dxr3audiosink_base_init),
(dxr3audiosink_init), (dxr3audiosink_pcm_sinklink):
* sys/dxr3/dxr3spusink.c: (dxr3spusink_base_init),
(dxr3spusink_init):
* sys/dxr3/dxr3videosink.c: (dxr3videosink_base_init),
(dxr3videosink_init):
Fix caps breakage after Dave's caps branch merge.
2003-12-23 22:50:06 +00:00
|
|
|
GstStructure *structure = gst_caps_get_structure (caps, 0);
|
2003-12-11 21:07:25 +00:00
|
|
|
gint width, height;
|
2006-07-13 11:06:45 +00:00
|
|
|
const GValue *fps_val;
|
2008-02-03 18:22:18 +00:00
|
|
|
const GValue *par_val;
|
2006-07-13 11:06:45 +00:00
|
|
|
y4m_ratio_t fps;
|
2008-02-03 18:22:18 +00:00
|
|
|
y4m_ratio_t par;
|
2021-08-05 21:11:26 +00:00
|
|
|
const gchar *interlace_mode;
|
2008-02-03 18:22:18 +00:00
|
|
|
|
|
|
|
if (!gst_structure_get_int (structure, "width", &width))
|
|
|
|
width = -1;
|
|
|
|
|
|
|
|
if (!gst_structure_get_int (structure, "height", &height))
|
|
|
|
height = -1;
|
2003-12-11 21:07:25 +00:00
|
|
|
|
2006-07-13 11:06:45 +00:00
|
|
|
fps_val = gst_structure_get_value (structure, "framerate");
|
2008-02-03 18:22:18 +00:00
|
|
|
if (fps_val != NULL) {
|
|
|
|
fps.n = gst_value_get_fraction_numerator (fps_val);
|
|
|
|
fps.d = gst_value_get_fraction_denominator (fps_val);
|
|
|
|
|
|
|
|
strm.frame_rate_code = mpeg_framerate_code (fps);
|
|
|
|
} else
|
|
|
|
strm.frame_rate_code = 0;
|
|
|
|
|
|
|
|
par_val = gst_structure_get_value (structure, "pixel-aspect-ratio");
|
|
|
|
if (par_val != NULL) {
|
|
|
|
par.n = gst_value_get_fraction_numerator (par_val);
|
|
|
|
par.d = gst_value_get_fraction_denominator (par_val);
|
|
|
|
} else {
|
|
|
|
/* By default, assume square pixels */
|
|
|
|
par.n = 1;
|
|
|
|
par.d = 1;
|
|
|
|
}
|
2003-12-11 21:07:25 +00:00
|
|
|
|
|
|
|
strm.horizontal_size = width;
|
|
|
|
strm.vertical_size = height;
|
2008-02-03 18:22:18 +00:00
|
|
|
|
2021-08-05 21:11:26 +00:00
|
|
|
interlace_mode = gst_structure_get_string (structure, "interlace-mode");
|
|
|
|
|
|
|
|
if (!g_strcmp0(interlace_mode, "interleaved")) {
|
|
|
|
const gchar *field_order = gst_structure_get_string(structure, "field-order");
|
|
|
|
|
|
|
|
if (!g_strcmp0(field_order, "bottom-field-first")) {
|
|
|
|
strm.interlacing_code = Y4M_ILACE_BOTTOM_FIRST;
|
|
|
|
} else if (!g_strcmp0(field_order, "top-field-first")) {
|
|
|
|
strm.interlacing_code = Y4M_ILACE_TOP_FIRST;
|
|
|
|
} else {
|
|
|
|
GST_WARNING ("No field-order in caps, assuming top field first");
|
|
|
|
strm.interlacing_code = Y4M_ILACE_TOP_FIRST;
|
|
|
|
}
|
|
|
|
} else if (!g_strcmp0(interlace_mode, "mixed")) {
|
|
|
|
strm.interlacing_code = Y4M_ILACE_MIXED;
|
|
|
|
} else {
|
|
|
|
strm.interlacing_code = Y4M_ILACE_NONE;
|
|
|
|
}
|
2008-02-03 18:22:18 +00:00
|
|
|
|
|
|
|
strm.aspect_ratio_code = mpeg_guess_mpeg_aspect_code (2, par,
|
2004-03-14 23:20:41 +00:00
|
|
|
strm.horizontal_size, strm.vertical_size);
|
2008-02-04 13:12:09 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (element, "Guessing aspect ratio code for PAR %d/%d "
|
|
|
|
"yielded: %d", par.n, par.d, strm.aspect_ratio_code);
|
2003-12-11 21:07:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-01-16 14:14:58 +00:00
|
|
|
* Read a frame. Return true means EOS or error.
|
2003-12-11 21:07:25 +00:00
|
|
|
*/
|
|
|
|
|
2006-07-13 11:06:45 +00:00
|
|
|
bool
|
2008-01-27 07:32:19 +00:00
|
|
|
GstMpeg2EncPictureReader::LoadFrame (ImagePlanes & image)
|
2003-12-11 21:07:25 +00:00
|
|
|
{
|
2012-01-25 17:25:01 +00:00
|
|
|
gint i, x, y, s;
|
2006-07-13 11:06:45 +00:00
|
|
|
guint8 *frame;
|
|
|
|
GstMpeg2enc *enc;
|
2012-01-25 17:25:01 +00:00
|
|
|
GstVideoFrame vframe;
|
2020-08-04 14:05:33 +00:00
|
|
|
GstVideoCodecFrame *inframe = NULL;
|
2006-07-13 11:06:45 +00:00
|
|
|
|
|
|
|
enc = GST_MPEG2ENC (element);
|
2004-01-16 14:14:58 +00:00
|
|
|
|
2006-07-13 11:06:45 +00:00
|
|
|
GST_MPEG2ENC_MUTEX_LOCK (enc);
|
|
|
|
|
|
|
|
/* hang around until the element provides us with a buffer */
|
2020-08-04 14:05:33 +00:00
|
|
|
while (!(inframe = (GstVideoCodecFrame *)g_queue_pop_head (enc->frames))) {
|
2006-07-13 11:06:45 +00:00
|
|
|
if (enc->eos) {
|
|
|
|
GST_MPEG2ENC_MUTEX_UNLOCK (enc);
|
|
|
|
/* inform the mpeg encoding loop that it can give up */
|
|
|
|
return TRUE;
|
2003-12-11 21:07:25 +00:00
|
|
|
}
|
2006-07-13 11:06:45 +00:00
|
|
|
GST_MPEG2ENC_WAIT (enc);
|
|
|
|
}
|
2003-12-11 21:07:25 +00:00
|
|
|
|
2012-10-01 11:11:29 +00:00
|
|
|
gst_video_frame_map (&vframe, &enc->input_state->info, inframe->input_buffer, GST_MAP_READ);
|
|
|
|
|
2012-01-25 17:25:01 +00:00
|
|
|
frame = GST_VIDEO_FRAME_COMP_DATA (&vframe, 0);
|
|
|
|
s = GST_VIDEO_FRAME_COMP_STRIDE (&vframe, 0);
|
2003-12-11 21:07:25 +00:00
|
|
|
x = encparams.horizontal_size;
|
|
|
|
y = encparams.vertical_size;
|
|
|
|
|
|
|
|
for (i = 0; i < y; i++) {
|
2008-01-27 07:32:19 +00:00
|
|
|
memcpy (image.Plane (0) + i * encparams.phy_width, frame, x);
|
2012-01-25 17:25:01 +00:00
|
|
|
frame += s;
|
2003-12-11 21:07:25 +00:00
|
|
|
}
|
2012-01-25 17:25:01 +00:00
|
|
|
frame = GST_VIDEO_FRAME_COMP_DATA (&vframe, 1);
|
|
|
|
s = GST_VIDEO_FRAME_COMP_STRIDE (&vframe, 1);
|
2003-12-11 21:07:25 +00:00
|
|
|
x >>= 1;
|
|
|
|
y >>= 1;
|
|
|
|
for (i = 0; i < y; i++) {
|
2008-01-27 07:32:19 +00:00
|
|
|
memcpy (image.Plane (1) + i * encparams.phy_chrom_width, frame, x);
|
2012-01-25 17:25:01 +00:00
|
|
|
frame += s;
|
2003-12-11 21:07:25 +00:00
|
|
|
}
|
2012-01-25 17:25:01 +00:00
|
|
|
frame = GST_VIDEO_FRAME_COMP_DATA (&vframe, 2);
|
|
|
|
s = GST_VIDEO_FRAME_COMP_STRIDE (&vframe, 2);
|
2003-12-11 21:07:25 +00:00
|
|
|
for (i = 0; i < y; i++) {
|
2008-01-27 07:32:19 +00:00
|
|
|
memcpy (image.Plane (2) + i * encparams.phy_chrom_width, frame, x);
|
2012-01-25 17:25:01 +00:00
|
|
|
frame += s;
|
2003-12-11 21:07:25 +00:00
|
|
|
}
|
2012-01-25 17:25:01 +00:00
|
|
|
gst_video_frame_unmap (&vframe);
|
2012-10-01 11:11:29 +00:00
|
|
|
|
2006-07-13 11:06:45 +00:00
|
|
|
/* inform the element the buffer has been processed */
|
|
|
|
GST_MPEG2ENC_SIGNAL (enc);
|
|
|
|
GST_MPEG2ENC_MUTEX_UNLOCK (enc);
|
2003-12-11 21:07:25 +00:00
|
|
|
|
2006-07-13 11:06:45 +00:00
|
|
|
return FALSE;
|
2003-12-11 21:07:25 +00:00
|
|
|
}
|