1 /* GStreamer mpeg2enc (mjpegtools) wrapper
2  * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  * (c) 2006 Mark Nauwelaerts <manauw@skynet.be>
4  *
5  * gstmpeg2encoder.cc: gstreamer/mpeg2enc encoder class
6  *
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.
11  *
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.
16  *
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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include <mpegconsts.h>
28 #include <quantize.hh>
29 #if GST_MJPEGTOOLS_API >= 20000
30 #include <ontheflyratectlpass1.hh>
31 #include <ontheflyratectlpass2.hh>
32 #elif GST_MJPEGTOOLS_API >= 10900
33 #include <ontheflyratectl.hh>
34 #include <pass1ratectl.hh>
35 #include <pass2ratectl.hh>
36 #else
37 #include <ratectl.hh>
38 #endif
39 #include <seqencoder.hh>
40 #include <mpeg2coder.hh>
41 
42 #include "gstmpeg2enc.hh"
43 #include "gstmpeg2encoder.hh"
44 
45 /*
46  * Class init stuff.
47  */
48 
GstMpeg2Encoder(GstMpeg2EncOptions * options,GstElement * in_element,GstCaps * in_caps)49 GstMpeg2Encoder::GstMpeg2Encoder (GstMpeg2EncOptions * options, GstElement * in_element, GstCaps * in_caps):
50 MPEG2Encoder (*options)
51 {
52   element = in_element;
53   gst_object_ref (element);
54   caps = in_caps;
55   gst_caps_ref (in_caps);
56   init_done = FALSE;
57 }
58 
~GstMpeg2Encoder()59 GstMpeg2Encoder::~GstMpeg2Encoder ()
60 {
61   gst_caps_unref (caps);
62   gst_object_unref (element);
63 }
64 
setup()65 gboolean GstMpeg2Encoder::setup ()
66 {
67   MPEG2EncInVidParams
68       strm;
69   GstMpeg2enc *
70       enc;
71 
72   enc = GST_MPEG2ENC (element);
73 
74   /* I/O */
75   reader = new GstMpeg2EncPictureReader (element, caps, &parms);
76   reader->StreamPictureParams (strm);
77 #if GST_MJPEGTOOLS_API == 10800
78   /* chain thread caters for reading, do not need another thread for this */
79   options.allow_parallel_read = FALSE;
80 #endif
81   if (options.SetFormatPresets (strm)) {
82     return FALSE;
83   }
84   writer = new GstMpeg2EncStreamWriter (enc->srcpad, &parms);
85 
86   /* encoding internals */
87   quantizer = new Quantizer (parms);
88 #if GST_MJPEGTOOLS_API < 10900
89   bitrate_controller = new OnTheFlyRateCtl (parms);
90 #else
91   pass1ratectl = new OnTheFlyPass1 (parms);
92   pass2ratectl = new OnTheFlyPass2 (parms);
93 #endif
94 #if GST_MJPEGTOOLS_API >= 10900
95   /* sequencer */
96   seqencoder = new SeqEncoder (parms, *reader, *quantizer,
97       *writer, *pass1ratectl, *pass2ratectl);
98 #elif GST_MJPEGTOOLS_API >= 10800
99   /* sequencer */
100   seqencoder = new SeqEncoder (parms, *reader, *quantizer,
101       *writer, *bitrate_controller);
102 #else
103   coder = new MPEG2Coder (parms, *writer);
104   /* sequencer */
105   seqencoder = new SeqEncoder (parms, *reader, *quantizer,
106       *writer, *coder, *bitrate_controller);
107 #endif
108 
109   return TRUE;
110 }
111 
112 void
init()113 GstMpeg2Encoder::init ()
114 {
115   if (!init_done) {
116     parms.Init (options);
117     reader->Init ();
118     quantizer->Init ();
119 #if GST_MJPEGTOOLS_API >= 10800
120     seqencoder->Init ();
121 #endif
122     init_done = TRUE;
123   }
124 }
125 
126 /*
127  * Process all input provided by the reader until it signals eos.
128  */
129 
130 void
encode()131 GstMpeg2Encoder::encode ()
132 {
133   /* hm, this is all... eek! */
134 #if GST_MJPEGTOOLS_API >= 10800
135   seqencoder->EncodeStream ();
136 #else
137   seqencoder->Encode ();
138 #endif
139 }
140 
141 /*
142  * Get current output format.
143  */
144 
145 GstCaps *
getFormat()146 GstMpeg2Encoder::getFormat ()
147 {
148   y4m_ratio_t fps = mpeg_framerate (options.frame_rate);
149 
150   return gst_caps_new_simple ("video/mpeg",
151       "systemstream", G_TYPE_BOOLEAN, FALSE,
152       "mpegversion", G_TYPE_INT, options.mpeg,
153       "width", G_TYPE_INT, options.in_img_width,
154       "height", G_TYPE_INT, options.in_img_height,
155       "framerate", GST_TYPE_FRACTION, fps.n, fps.d, NULL);
156 }
157