• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer mpeg2enc (mjpegtools) wrapper
2  * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  *
4  * gstmpeg2encpicturereader.cc: GStreamer/mpeg2enc input wrapper
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <encoderparams.hh>
27 #include <string.h>
28 
29 #include "gstmpeg2enc.hh"
30 #include "gstmpeg2encpicturereader.hh"
31 
32 /*
33  * Class init stuff.
34  */
35 
GstMpeg2EncPictureReader(GstElement * in_element,GstCaps * in_caps,EncoderParams * params)36 GstMpeg2EncPictureReader::GstMpeg2EncPictureReader (GstElement * in_element, GstCaps * in_caps, EncoderParams * params):
37 PictureReader (*params)
38 {
39   element = in_element;
40   gst_object_ref (element);
41   caps = in_caps;
42   gst_caps_ref (caps);
43 }
44 
~GstMpeg2EncPictureReader()45 GstMpeg2EncPictureReader::~GstMpeg2EncPictureReader ()
46 {
47   gst_caps_unref (caps);
48   gst_object_unref (element);
49 }
50 
51 /*
52  * Get input picture parameters (width/height etc.).
53  */
54 
55 void
StreamPictureParams(MPEG2EncInVidParams & strm)56 GstMpeg2EncPictureReader::StreamPictureParams (MPEG2EncInVidParams & strm)
57 {
58   GstStructure *structure = gst_caps_get_structure (caps, 0);
59   gint width, height;
60   const GValue *fps_val;
61   const GValue *par_val;
62   y4m_ratio_t fps;
63   y4m_ratio_t par;
64   const gchar *interlace_mode;
65 
66   if (!gst_structure_get_int (structure, "width", &width))
67     width = -1;
68 
69   if (!gst_structure_get_int (structure, "height", &height))
70     height = -1;
71 
72   fps_val = gst_structure_get_value (structure, "framerate");
73   if (fps_val != NULL) {
74     fps.n = gst_value_get_fraction_numerator (fps_val);
75     fps.d = gst_value_get_fraction_denominator (fps_val);
76 
77     strm.frame_rate_code = mpeg_framerate_code (fps);
78   } else
79     strm.frame_rate_code = 0;
80 
81   par_val = gst_structure_get_value (structure, "pixel-aspect-ratio");
82   if (par_val != NULL) {
83     par.n = gst_value_get_fraction_numerator (par_val);
84     par.d = gst_value_get_fraction_denominator (par_val);
85   } else {
86     /* By default, assume square pixels */
87     par.n = 1;
88     par.d = 1;
89   }
90 
91   strm.horizontal_size = width;
92   strm.vertical_size = height;
93 
94   interlace_mode = gst_structure_get_string (structure, "interlace-mode");
95 
96   if (!g_strcmp0(interlace_mode, "interleaved")) {
97     const gchar *field_order = gst_structure_get_string(structure, "field-order");
98 
99     if (!g_strcmp0(field_order, "bottom-field-first")) {
100       strm.interlacing_code = Y4M_ILACE_BOTTOM_FIRST;
101     } else if (!g_strcmp0(field_order, "top-field-first")) {
102       strm.interlacing_code = Y4M_ILACE_TOP_FIRST;
103     } else {
104       GST_WARNING ("No field-order in caps, assuming top field first");
105       strm.interlacing_code = Y4M_ILACE_TOP_FIRST;
106     }
107   } else if (!g_strcmp0(interlace_mode, "mixed")) {
108     strm.interlacing_code = Y4M_ILACE_MIXED;
109   } else {
110     strm.interlacing_code = Y4M_ILACE_NONE;
111   }
112 
113   strm.aspect_ratio_code = mpeg_guess_mpeg_aspect_code (2, par,
114       strm.horizontal_size, strm.vertical_size);
115 
116   GST_DEBUG_OBJECT (element, "Guessing aspect ratio code for PAR %d/%d "
117       "yielded: %d", par.n, par.d, strm.aspect_ratio_code);
118 }
119 
120 /*
121  * Read a frame. Return true means EOS or error.
122  */
123 
124 bool
LoadFrame(ImagePlanes & image)125     GstMpeg2EncPictureReader::LoadFrame (ImagePlanes & image)
126 {
127   gint i, x, y, s;
128   guint8 *frame;
129   GstMpeg2enc *enc;
130   GstVideoFrame vframe;
131   GstVideoCodecFrame *inframe = NULL;
132 
133   enc = GST_MPEG2ENC (element);
134 
135   GST_MPEG2ENC_MUTEX_LOCK (enc);
136 
137   /* hang around until the element provides us with a buffer */
138   while (enc->pending_frame == NULL) {
139     if (enc->eos) {
140       GST_MPEG2ENC_MUTEX_UNLOCK (enc);
141       /* inform the mpeg encoding loop that it can give up */
142       return TRUE;
143     }
144     GST_MPEG2ENC_WAIT (enc);
145   }
146 
147   inframe = enc->pending_frame;
148   gst_video_frame_map (&vframe, &enc->input_state->info, inframe->input_buffer, GST_MAP_READ);
149   enc->pending_frame = NULL;
150 
151   frame = GST_VIDEO_FRAME_COMP_DATA (&vframe, 0);
152   s = GST_VIDEO_FRAME_COMP_STRIDE (&vframe, 0);
153   x = encparams.horizontal_size;
154   y = encparams.vertical_size;
155 
156   for (i = 0; i < y; i++) {
157     memcpy (image.Plane (0) + i * encparams.phy_width, frame, x);
158     frame += s;
159   }
160   frame = GST_VIDEO_FRAME_COMP_DATA (&vframe, 1);
161   s = GST_VIDEO_FRAME_COMP_STRIDE (&vframe, 1);
162   x >>= 1;
163   y >>= 1;
164   for (i = 0; i < y; i++) {
165     memcpy (image.Plane (1) + i * encparams.phy_chrom_width, frame, x);
166     frame += s;
167   }
168   frame = GST_VIDEO_FRAME_COMP_DATA (&vframe, 2);
169   s = GST_VIDEO_FRAME_COMP_STRIDE (&vframe, 2);
170   for (i = 0; i < y; i++) {
171     memcpy (image.Plane (2) + i * encparams.phy_chrom_width, frame, x);
172     frame += s;
173   }
174   gst_video_frame_unmap (&vframe);
175 
176   /* inform the element the buffer has been processed */
177   GST_MPEG2ENC_SIGNAL (enc);
178   GST_MPEG2ENC_MUTEX_UNLOCK (enc);
179 
180   return FALSE;
181 }
182