1 /* GStreamer mpeg2enc (mjpegtools) wrapper
2 * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3 * (c) 2006 Mark Nauwelaerts <manauw@skynet.be>
5 * gstmpeg2encstreamwriter.cc: GStreamer/mpeg2enc output wrapper
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include "gstmpeg2enc.hh"
28 #include "gstmpeg2encstreamwriter.hh"
31 #if GST_MJPEGTOOLS_API >= 10800
37 GstMpeg2EncStreamWriter::GstMpeg2EncStreamWriter (GstPad * in_pad,
38 EncoderParams * params)
45 GstMpeg2EncStreamWriter::~GstMpeg2EncStreamWriter ()
47 gst_object_unref (pad);
51 GstMpeg2EncStreamWriter::WriteOutBufferUpto (const guint8 * buffer,
52 const guint32 flush_upto)
54 GstBuffer *buf, *inbuf;
55 GstMpeg2enc *enc = GST_MPEG2ENC (GST_PAD_PARENT (pad));
57 buf = gst_buffer_new_and_alloc (flush_upto);
59 memcpy (GST_BUFFER_DATA (buf), buffer, flush_upto);
60 flushed += flush_upto;
62 /* this should not block anything else (e.g. chain), but if it does,
63 * it's ok as mpeg2enc is not really a loop-based element, but push-based */
64 GST_MPEG2ENC_MUTEX_LOCK (enc);
65 /* best effort at giving output some meaningful time metadata
66 * no mpeg2enc specs on this though, but it might help getting the output
67 * into container formats that really do like timestamps (unlike mplex) */
68 if ((inbuf = (GstBuffer *) g_queue_pop_head (enc->time))) {
69 GST_BUFFER_TIMESTAMP (buf) = GST_BUFFER_TIMESTAMP (inbuf);
70 GST_BUFFER_DURATION (buf) = GST_BUFFER_DURATION (inbuf);
71 gst_buffer_unref (inbuf);
73 gst_buffer_set_caps (buf, GST_PAD_CAPS (pad));
74 enc->srcresult = gst_pad_push (pad, buf);
75 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
79 GstMpeg2EncStreamWriter::BitCount ()
86 #define BUFSIZE (128*1024)
92 GstMpeg2EncStreamWriter::GstMpeg2EncStreamWriter (GstPad * in_pad, EncoderParams * params):
93 ElemStrmWriter (*params)
100 GstMpeg2EncStreamWriter::~GstMpeg2EncStreamWriter ()
102 gst_object_unref (pad);
110 GstMpeg2EncStreamWriter::PutBits (guint32 val, gint n)
112 /* only relevant bits. Note that (according to Andrew),
113 * some CPUs do bitshifts modulo wordsize (32), which
114 * means that we have to check for n != 32 before
115 * bitshifting to the relevant bits (i.e. 0xffffffff <<
116 * 32 == 0xffffffff). */
118 val &= ~(0xffffffffU << n);
121 while (n >= outcnt) {
123 buf = gst_buffer_new_and_alloc (BUFSIZE);
124 GST_BUFFER_SIZE (buf) = 0;
127 outbfr = (outbfr << outcnt) | (val >> (n - outcnt));
128 GST_BUFFER_DATA (buf)[GST_BUFFER_SIZE (buf)++] = outbfr;
133 if (GST_BUFFER_SIZE (buf) >= BUFSIZE)
137 /* cache remaining bits */
139 outbfr = (outbfr << n) | val;
145 GstMpeg2EncStreamWriter::FrameBegin ()
150 GstMpeg2EncStreamWriter::FrameFlush ()
152 GstMpeg2enc *enc = GST_MPEG2ENC (GST_PAD_PARENT (pad));
155 /* this should not block anything else (e.g. chain), but if it does,
156 * it's ok as mpeg2enc is not really a loop-based element, but push-based */
157 GST_MPEG2ENC_MUTEX_LOCK (enc);
158 gst_buffer_set_caps (buf, GST_PAD_CAPS (pad));
159 enc->srcresult = gst_pad_push (pad, buf);
160 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
166 GstMpeg2EncStreamWriter::FrameDiscard ()
169 #endif /* GST_MJPEGTOOLS_API >= 10800 */