• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #endif
37 #include <seqencoder.hh>
38 #include <mpeg2coder.hh>
39 
40 #include "gstmpeg2enc.hh"
41 #include "gstmpeg2encoder.hh"
42 
43 class GstOnTheFlyPass2 : public OnTheFlyPass2 {
44   public:
GstOnTheFlyPass2(EncoderParams & encoder,gboolean disable_encode_retries)45     GstOnTheFlyPass2 (EncoderParams &encoder, gboolean disable_encode_retries): OnTheFlyPass2(encoder), disable_encode_retries(disable_encode_retries) {}
ReencodeRequired() const46     bool ReencodeRequired() const { return disable_encode_retries ? false : OnTheFlyPass2::ReencodeRequired(); }
47   private:
48     gboolean disable_encode_retries;
49 };
50 
51 /*
52  * Class init stuff.
53  */
54 
GstMpeg2Encoder(GstMpeg2EncOptions * options,GstElement * in_element,GstCaps * in_caps)55 GstMpeg2Encoder::GstMpeg2Encoder (GstMpeg2EncOptions * options, GstElement * in_element, GstCaps * in_caps):
56 MPEG2Encoder (*options)
57 {
58   element = in_element;
59   gst_object_ref (element);
60   caps = in_caps;
61   gst_caps_ref (in_caps);
62   init_done = FALSE;
63   disable_encode_retries = options->disable_encode_retries;
64 }
65 
~GstMpeg2Encoder()66 GstMpeg2Encoder::~GstMpeg2Encoder ()
67 {
68   gst_caps_unref (caps);
69   gst_object_unref (element);
70 }
71 
setup()72 gboolean GstMpeg2Encoder::setup ()
73 {
74   MPEG2EncInVidParams
75       strm;
76   GstVideoEncoder *
77       video_encoder;
78 
79   video_encoder = GST_VIDEO_ENCODER (element);
80 
81   /* I/O */
82   reader = new GstMpeg2EncPictureReader (element, caps, &parms);
83   reader->StreamPictureParams (strm);
84   if (options.SetFormatPresets (strm)) {
85     delete reader;
86     reader = NULL;
87     writer = NULL;
88     quantizer = NULL;
89     pass1ratectl = NULL;
90     pass2ratectl = NULL;
91     /* sequencer */
92     seqencoder = NULL;
93 
94     return FALSE;
95   }
96   writer = new GstMpeg2EncStreamWriter (video_encoder, &parms);
97 
98   /* encoding internals */
99   quantizer = new Quantizer (parms);
100   pass1ratectl = new OnTheFlyPass1 (parms);
101   pass2ratectl = new GstOnTheFlyPass2 (parms, disable_encode_retries);
102   /* sequencer */
103   seqencoder = new SeqEncoder (parms, *reader, *quantizer,
104       *writer, *pass1ratectl, *pass2ratectl);
105 
106   return TRUE;
107 }
108 
109 void
init()110 GstMpeg2Encoder::init ()
111 {
112   if (!init_done) {
113     parms.Init (options);
114     reader->Init ();
115     quantizer->Init ();
116     seqencoder->Init ();
117     init_done = TRUE;
118   }
119 }
120 
121 /*
122  * Process all input provided by the reader until it signals eos.
123  */
124 
125 void
encode()126 GstMpeg2Encoder::encode ()
127 {
128   /* hm, this is all... eek! */
129   seqencoder->EncodeStream ();
130 }
131 
132 /*
133  * Get current output format.
134  */
135 
136 GstCaps *
getFormat()137 GstMpeg2Encoder::getFormat ()
138 {
139   y4m_ratio_t fps = mpeg_framerate (options.frame_rate);
140 
141   return gst_caps_new_simple ("video/mpeg",
142       "systemstream", G_TYPE_BOOLEAN, FALSE,
143       "mpegversion", G_TYPE_INT, options.mpeg,
144       "width", G_TYPE_INT, options.in_img_width,
145       "height", G_TYPE_INT, options.in_img_height,
146       "framerate", GST_TYPE_FRACTION, fps.n, fps.d, NULL);
147 }
148