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
65 if (!gst_structure_get_int (structure, "width", &width))
66 width = -1;
67
68 if (!gst_structure_get_int (structure, "height", &height))
69 height = -1;
70
71 fps_val = gst_structure_get_value (structure, "framerate");
72 if (fps_val != NULL) {
73 fps.n = gst_value_get_fraction_numerator (fps_val);
74 fps.d = gst_value_get_fraction_denominator (fps_val);
75
76 strm.frame_rate_code = mpeg_framerate_code (fps);
77 } else
78 strm.frame_rate_code = 0;
79
80 par_val = gst_structure_get_value (structure, "pixel-aspect-ratio");
81 if (par_val != NULL) {
82 par.n = gst_value_get_fraction_numerator (par_val);
83 par.d = gst_value_get_fraction_denominator (par_val);
84 } else {
85 /* By default, assume square pixels */
86 par.n = 1;
87 par.d = 1;
88 }
89
90 strm.horizontal_size = width;
91 strm.vertical_size = height;
92
93 strm.interlacing_code = Y4M_ILACE_NONE;
94
95 strm.aspect_ratio_code = mpeg_guess_mpeg_aspect_code (2, par,
96 strm.horizontal_size, strm.vertical_size);
97
98 GST_DEBUG_OBJECT (element, "Guessing aspect ratio code for PAR %d/%d "
99 "yielded: %d", par.n, par.d, strm.aspect_ratio_code);
100 }
101
102 /*
103 * Read a frame. Return true means EOS or error.
104 */
105
106 bool
107 #if GST_MJPEGTOOLS_API >= 10900
LoadFrame(ImagePlanes & image)108 GstMpeg2EncPictureReader::LoadFrame (ImagePlanes & image)
109 #else
110 GstMpeg2EncPictureReader::LoadFrame ()
111 #endif
112 {
113 #if GST_MJPEGTOOLS_API < 10900
114 gint n;
115 #endif
116 gint i, x, y, s;
117 guint8 *frame;
118 GstMpeg2enc *enc;
119 GstVideoFrame vframe;
120
121 enc = GST_MPEG2ENC (element);
122
123 GST_MPEG2ENC_MUTEX_LOCK (enc);
124
125 /* hang around until the element provides us with a buffer */
126 while (!enc->buffer) {
127 if (enc->eos) {
128 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
129 /* inform the mpeg encoding loop that it can give up */
130 return TRUE;
131 }
132 GST_MPEG2ENC_WAIT (enc);
133 }
134
135 gst_video_frame_map (&vframe, &enc->vinfo, enc->buffer, GST_MAP_READ);
136 // frame = GST_BUFFER_DATA (enc->buffer);
137 #if GST_MJPEGTOOLS_API < 10900
138 n = frames_read % input_imgs_buf_size;
139 #endif
140 frame = GST_VIDEO_FRAME_COMP_DATA (&vframe, 0);
141 s = GST_VIDEO_FRAME_COMP_STRIDE (&vframe, 0);
142 x = encparams.horizontal_size;
143 y = encparams.vertical_size;
144
145 for (i = 0; i < y; i++) {
146 #if GST_MJPEGTOOLS_API >= 10900
147 memcpy (image.Plane (0) + i * encparams.phy_width, frame, x);
148 #else
149 memcpy (input_imgs_buf[n][0] + i * encparams.phy_width, frame, x);
150 #endif
151 frame += s;
152 }
153 #if GST_MJPEGTOOLS_API < 10900
154 lum_mean[n] = LumMean (input_imgs_buf[n][0]);
155 #endif
156 frame = GST_VIDEO_FRAME_COMP_DATA (&vframe, 1);
157 s = GST_VIDEO_FRAME_COMP_STRIDE (&vframe, 1);
158 x >>= 1;
159 y >>= 1;
160 for (i = 0; i < y; i++) {
161 #if GST_MJPEGTOOLS_API >= 10900
162 memcpy (image.Plane (1) + i * encparams.phy_chrom_width, frame, x);
163 #else
164 memcpy (input_imgs_buf[n][1] + i * encparams.phy_chrom_width, frame, x);
165 #endif
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 #if GST_MJPEGTOOLS_API >= 10900
172 memcpy (image.Plane (2) + i * encparams.phy_chrom_width, frame, x);
173 #else
174 memcpy (input_imgs_buf[n][2] + i * encparams.phy_chrom_width, frame, x);
175 #endif
176 frame += s;
177 }
178 gst_video_frame_unmap (&vframe);
179 gst_buffer_unref (enc->buffer);
180 enc->buffer = NULL;
181
182 /* inform the element the buffer has been processed */
183 GST_MPEG2ENC_SIGNAL (enc);
184 GST_MPEG2ENC_MUTEX_UNLOCK (enc);
185
186 return FALSE;
187 }
188