1 /* GStreamer PNM encoder
2 * Copyright (C) 2009 Lutz Mueller <lutz@users.sourceforge.net>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * SECTION:element-pnmenc
22 * @title: pnmenc
23 *
24 * Encodes pnm images. This plugin supports both raw and ASCII encoding.
25 * To enable ASCII encoding, set the parameter ascii to TRUE. If you omit
26 * the parameter or set it to FALSE, the output will be raw encoded.
27 *
28 * ## Example launch line
29 * |[
30 * gst-launch-1.0 videotestsrc num_buffers=1 ! videoconvert ! "video/x-raw,format=GRAY8" ! pnmenc ascii=true ! filesink location=test.pnm
31 * ]| The above pipeline writes a test pnm file (ASCII encoding).
32 *
33 */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include "gstpnmenc.h"
40 #include "gstpnmutils.h"
41
42 #include <gst/gstutils.h>
43 #include <gst/video/video.h>
44 #include <gst/video/gstvideometa.h>
45 #include <stdio.h>
46
47 #include <string.h>
48
49 enum
50 {
51 GST_PNMENC_PROP_0,
52 GST_PNMENC_PROP_ASCII
53 /* Add here. */
54 };
55
56
57
58 static GstStaticPadTemplate sink_pad_template =
59 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
60 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
61 ("{ RGB, GRAY8, GRAY16_BE, GRAY16_LE }")));
62
63
64 static GstStaticPadTemplate src_pad_template =
65 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
66 GST_STATIC_CAPS (MIME_ALL));
67
68 #define parent_class gst_pnmenc_parent_class
69 G_DEFINE_TYPE (GstPnmenc, gst_pnmenc, GST_TYPE_VIDEO_ENCODER);
70 GST_ELEMENT_REGISTER_DEFINE (pnmenc, "pnmenc", GST_RANK_PRIMARY,
71 GST_TYPE_PNMENC);
72
73
74 static GstFlowReturn
75 gst_pnmenc_handle_frame (GstVideoEncoder * encoder, GstVideoCodecFrame * frame);
76
77 static void gst_pnmenc_finalize (GObject * object);
78
79 static void
gst_pnmenc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)80 gst_pnmenc_set_property (GObject * object, guint prop_id, const GValue * value,
81 GParamSpec * pspec)
82 {
83 GstPnmenc *s = GST_PNMENC (object);
84
85 switch (prop_id) {
86 case GST_PNMENC_PROP_ASCII:
87 if (g_value_get_boolean (value)) {
88 s->info.encoding = GST_PNM_ENCODING_ASCII;
89 } else {
90 s->info.encoding = GST_PNM_ENCODING_RAW;
91 }
92 s->info.fields |= GST_PNM_INFO_FIELDS_ENCODING;
93 break;
94 default:
95 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
96 break;
97 }
98 }
99
100 static void
gst_pnmenc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)101 gst_pnmenc_get_property (GObject * object, guint prop_id, GValue * value,
102 GParamSpec * pspec)
103 {
104 GstPnmenc *s = GST_PNMENC (object);
105
106 switch (prop_id) {
107 case GST_PNMENC_PROP_ASCII:
108 g_value_set_boolean (value, s->info.encoding == GST_PNM_ENCODING_ASCII);
109 break;
110 default:
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
112 break;
113 }
114 }
115
116 static void
gst_pnmenc_init(GstPnmenc * s)117 gst_pnmenc_init (GstPnmenc * s)
118 {
119 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_ENCODER_SINK_PAD (s));
120
121 /* Set default encoding as RAW as ASCII takes up 4 time more bytes */
122 s->info.encoding = GST_PNM_ENCODING_RAW;
123 }
124
125 static void
gst_pnmenc_finalize(GObject * object)126 gst_pnmenc_finalize (GObject * object)
127 {
128 GstPnmenc *pnmenc = GST_PNMENC (object);
129 if (pnmenc->input_state)
130 gst_video_codec_state_unref (pnmenc->input_state);
131
132 G_OBJECT_CLASS (parent_class)->finalize (object);
133 }
134
135 static gboolean
gst_pnmenc_set_format(GstVideoEncoder * encoder,GstVideoCodecState * state)136 gst_pnmenc_set_format (GstVideoEncoder * encoder, GstVideoCodecState * state)
137 {
138 GstPnmenc *pnmenc;
139 gboolean ret = TRUE;
140 GstVideoInfo *info;
141 GstVideoCodecState *output_state;
142 const gchar *mime_type = NULL;
143
144 pnmenc = GST_PNMENC (encoder);
145 info = &state->info;
146
147 switch (GST_VIDEO_INFO_FORMAT (info)) {
148 case GST_VIDEO_FORMAT_RGB:
149 pnmenc->info.max = 255;
150 pnmenc->info.type = GST_PNM_TYPE_PIXMAP;
151 mime_type = MIME_PM;
152 break;
153 case GST_VIDEO_FORMAT_GRAY8:
154 pnmenc->info.max = 255;
155 pnmenc->info.type = GST_PNM_TYPE_GRAYMAP;
156 mime_type = MIME_GM;
157 break;
158 case GST_VIDEO_FORMAT_GRAY16_BE:
159 case GST_VIDEO_FORMAT_GRAY16_LE:
160 pnmenc->info.max = 65535;
161 pnmenc->info.type = GST_PNM_TYPE_GRAYMAP;
162 mime_type = MIME_GM;
163 break;
164 default:
165 ret = FALSE;
166 goto done;
167 }
168
169 pnmenc->info.width = GST_VIDEO_INFO_WIDTH (info);
170 pnmenc->info.height = GST_VIDEO_INFO_HEIGHT (info);
171
172 if (pnmenc->input_state)
173 gst_video_codec_state_unref (pnmenc->input_state);
174 pnmenc->input_state = gst_video_codec_state_ref (state);
175
176 output_state =
177 gst_video_encoder_set_output_state (encoder,
178 gst_caps_new_empty_simple (mime_type), state);
179 gst_video_codec_state_unref (output_state);
180
181 done:
182 return ret;
183 }
184
185 static GstFlowReturn
gst_pnmenc_handle_frame(GstVideoEncoder * encoder,GstVideoCodecFrame * frame)186 gst_pnmenc_handle_frame (GstVideoEncoder * encoder, GstVideoCodecFrame * frame)
187 {
188 GstPnmenc *pnmenc;
189 guint size, pixels, bytesize;
190 GstMapInfo omap;
191 gchar *header = NULL;
192 GstVideoInfo *info;
193 GstFlowReturn ret = GST_FLOW_OK;
194 guint i_rowstride, o_rowstride;
195 guint bytes = 0, index, head_size;
196 guint i, j;
197 guint maxbytes_per_pixel, str_len;
198 gchar format_str[4];
199 GstVideoFrame in_frame;
200 pnmenc = GST_PNMENC (encoder);
201 info = &pnmenc->input_state->info;
202
203 switch (GST_VIDEO_INFO_FORMAT (info)) {
204 case GST_VIDEO_FORMAT_RGB:
205 pixels = size = pnmenc->info.width * pnmenc->info.height * 3;
206 bytesize = 1;
207 maxbytes_per_pixel = 4;
208 str_len = 3;
209 g_strlcpy (format_str, "%3i", 4);
210 break;
211 case GST_VIDEO_FORMAT_GRAY8:
212 pixels = size = pnmenc->info.width * pnmenc->info.height * 1;
213 bytesize = 1;
214 maxbytes_per_pixel = 4;
215 str_len = 3;
216 g_strlcpy (format_str, "%3i", 4);
217 break;
218 case GST_VIDEO_FORMAT_GRAY16_LE:
219 case GST_VIDEO_FORMAT_GRAY16_BE:
220 pixels = pnmenc->info.width * pnmenc->info.height * 1;
221 bytesize = 2;
222 size = pixels * bytesize;
223 maxbytes_per_pixel = 6;
224 str_len = 5;
225 g_strlcpy (format_str, "%5i", 4);
226 break;
227 default:
228 ret = FALSE;
229 goto done;
230 }
231
232 header = g_strdup_printf ("P%i\n%i %i\n%i\n",
233 pnmenc->info.type + 3 * (1 - pnmenc->info.encoding), pnmenc->info.width,
234 pnmenc->info.height, pnmenc->info.max);
235
236 if (pnmenc->info.encoding == GST_PNM_ENCODING_ASCII) {
237 /* Per component 4 bytes are used in case of ASCII encoding */
238 size = size * 4 + size / 20;
239 size += strlen (header);
240 frame->output_buffer =
241 gst_video_encoder_allocate_output_buffer (encoder, (size));
242 } else {
243 size += strlen (header);
244 frame->output_buffer =
245 gst_video_encoder_allocate_output_buffer (encoder, size);
246 }
247
248 if (gst_buffer_map (frame->output_buffer, &omap, GST_MAP_WRITE) == FALSE) {
249 ret = GST_FLOW_ERROR;
250 goto done;
251 }
252 if (!gst_video_frame_map (&in_frame, &(pnmenc->input_state->info),
253 frame->input_buffer, GST_MAP_READ)) {
254 /* Unmap already mapped buffer */
255 gst_buffer_unmap (frame->output_buffer, &omap);
256 ret = GST_FLOW_ERROR;
257 goto done;
258 }
259 /* Copy out the header first */
260 head_size = strlen (header);
261 memcpy (omap.data, header, head_size);
262
263 if (pnmenc->info.encoding == GST_PNM_ENCODING_ASCII) {
264 /* We need to convert to ASCII */
265 /* Convert from gstreamer rowstride to PNM rowstride as we go */
266 if (pnmenc->info.type == GST_PNM_TYPE_PIXMAP) {
267 o_rowstride = 3 * pnmenc->info.width;
268 } else {
269 o_rowstride = pnmenc->info.width;
270 }
271 i_rowstride = GST_VIDEO_FRAME_PLANE_STRIDE (&in_frame, 0);
272
273 switch (GST_VIDEO_INFO_FORMAT (info)) {
274 case GST_VIDEO_FORMAT_RGB:
275 case GST_VIDEO_FORMAT_GRAY8:
276 for (i = 0; i < pnmenc->info.height; i++) {
277 index = i * i_rowstride;
278 for (j = 0; j < o_rowstride; j++, bytes++, index++) {
279 g_snprintf ((char *) omap.data + head_size, maxbytes_per_pixel,
280 format_str, in_frame.map[0].data[index]);
281 head_size += str_len;
282 omap.data[head_size++] = ' ';
283 /* Add new line so that file will not end up with single big line */
284 if (!((bytes + 1) % 20))
285 omap.data[head_size++] = '\n';
286 }
287 }
288 break;
289 case GST_VIDEO_FORMAT_GRAY16_BE:
290 for (i = 0; i < pnmenc->info.height; i++) {
291 index = i * i_rowstride;
292 for (j = 0; j < o_rowstride; j++, bytes++, index += 2) {
293 g_snprintf ((char *) omap.data + head_size, maxbytes_per_pixel,
294 format_str, GST_READ_UINT16_BE (in_frame.map[0].data + index));
295 head_size += str_len;
296 omap.data[head_size++] = ' ';
297 /* Add new line so that file will not end up with single big line */
298 if (!((bytes + 1) % 20))
299 omap.data[head_size++] = '\n';
300 }
301 }
302 break;
303 case GST_VIDEO_FORMAT_GRAY16_LE:
304 for (i = 0; i < pnmenc->info.height; i++) {
305 index = i * i_rowstride;
306 for (j = 0; j < o_rowstride; j++, bytes++, index += 2) {
307 g_snprintf ((char *) omap.data + head_size, maxbytes_per_pixel,
308 format_str, GST_READ_UINT16_LE (in_frame.map[0].data + index));
309 head_size += str_len;
310 omap.data[head_size++] = ' ';
311 /* Add new line so that file will not end up with single big line */
312 if (!((bytes + 1) % 20))
313 omap.data[head_size++] = '\n';
314 }
315 }
316 break;
317 default:
318 GST_ERROR_OBJECT (encoder, "Unhandled format %s",
319 gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (info)));
320 gst_buffer_unmap (frame->output_buffer, &omap);
321 gst_video_frame_unmap (&in_frame);
322 g_free (header);
323 return GST_FLOW_ERROR;
324 }
325
326 gst_buffer_set_size (frame->output_buffer, head_size);
327 } else {
328 guint out_index = head_size;
329
330 /* Binary output. 8-bit, or 16-bit BE */
331 if (pnmenc->info.type == GST_PNM_TYPE_PIXMAP) {
332 o_rowstride = 3 * pnmenc->info.width * bytesize;
333 } else {
334 o_rowstride = pnmenc->info.width * bytesize;
335 }
336 i_rowstride = GST_VIDEO_FRAME_PLANE_STRIDE (&in_frame, 0);
337
338 switch (GST_VIDEO_INFO_FORMAT (info)) {
339 case GST_VIDEO_FORMAT_GRAY16_BE:
340 for (i = 0; i < pnmenc->info.height; i++) {
341 index = i * i_rowstride;
342 for (j = 0; j < o_rowstride; j += 2, index += 2) {
343 guint16 val = GST_READ_UINT16_LE (in_frame.map[0].data + index);
344 GST_WRITE_UINT16_BE (omap.data + out_index, val);
345 out_index += 2;
346 }
347 }
348 break;
349 case GST_VIDEO_FORMAT_GRAY16_LE:
350 for (i = 0; i < pnmenc->info.height; i++) {
351 index = i * i_rowstride;
352 for (j = 0; j < o_rowstride; j += 2, index += 2) {
353 guint16 val = GST_READ_UINT16_LE (in_frame.map[0].data + index);
354 GST_WRITE_UINT16_BE (omap.data + out_index, val);
355 out_index += 2;
356 }
357 }
358 break;
359 default:
360 for (i = 0; i < pnmenc->info.height; i++) {
361 memcpy (omap.data + head_size + o_rowstride * i,
362 in_frame.map[0].data + i_rowstride * i, o_rowstride);
363 }
364 }
365 }
366
367 gst_buffer_unmap (frame->output_buffer, &omap);
368 gst_video_frame_unmap (&in_frame);
369
370 if ((ret = gst_video_encoder_finish_frame (encoder, frame)) != GST_FLOW_OK)
371 goto done;
372
373 done:
374 g_free (header);
375 return ret;
376 }
377
378 static void
gst_pnmenc_class_init(GstPnmencClass * klass)379 gst_pnmenc_class_init (GstPnmencClass * klass)
380 {
381 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
382 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
383 GstVideoEncoderClass *venc_class = (GstVideoEncoderClass *) klass;
384
385 parent_class = g_type_class_peek_parent (klass);
386 gobject_class->set_property = gst_pnmenc_set_property;
387 gobject_class->get_property = gst_pnmenc_get_property;
388
389 g_object_class_install_property (gobject_class, GST_PNMENC_PROP_ASCII,
390 g_param_spec_boolean ("ascii", "ASCII Encoding", "The output will be "
391 "ASCII encoded", FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392
393 gst_element_class_add_static_pad_template (element_class, &sink_pad_template);
394 gst_element_class_add_static_pad_template (element_class, &src_pad_template);
395
396 gst_element_class_set_static_metadata (element_class, "PNM image encoder",
397 "Codec/Encoder/Image",
398 "Encodes images into portable pixmap or graymap (PNM) format",
399 "Lutz Mueller <lutz@users.sourceforge.net>");
400
401 venc_class->set_format = gst_pnmenc_set_format;
402 venc_class->handle_frame = gst_pnmenc_handle_frame;
403 gobject_class->finalize = gst_pnmenc_finalize;
404 }
405