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 G_DEFINE_TYPE (GstPnmenc, gst_pnmenc, GST_TYPE_VIDEO_ENCODER);
69 #define parent_class gst_pnmenc_parent_class
70 
71 static GstFlowReturn
72 gst_pnmenc_handle_frame (GstVideoEncoder * encoder, GstVideoCodecFrame * frame);
73 
74 static void gst_pnmenc_finalize (GObject * object);
75 
76 static void
gst_pnmenc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)77 gst_pnmenc_set_property (GObject * object, guint prop_id, const GValue * value,
78     GParamSpec * pspec)
79 {
80   GstPnmenc *s = GST_PNMENC (object);
81 
82   switch (prop_id) {
83     case GST_PNMENC_PROP_ASCII:
84       if (g_value_get_boolean (value)) {
85         s->info.encoding = GST_PNM_ENCODING_ASCII;
86       } else {
87         s->info.encoding = GST_PNM_ENCODING_RAW;
88       }
89       s->info.fields |= GST_PNM_INFO_FIELDS_ENCODING;
90       break;
91     default:
92       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93       break;
94   }
95 }
96 
97 static void
gst_pnmenc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)98 gst_pnmenc_get_property (GObject * object, guint prop_id, GValue * value,
99     GParamSpec * pspec)
100 {
101   GstPnmenc *s = GST_PNMENC (object);
102 
103   switch (prop_id) {
104     case GST_PNMENC_PROP_ASCII:
105       g_value_set_boolean (value, s->info.encoding == GST_PNM_ENCODING_ASCII);
106       break;
107     default:
108       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109       break;
110   }
111 }
112 
113 static void
gst_pnmenc_init(GstPnmenc * s)114 gst_pnmenc_init (GstPnmenc * s)
115 {
116   GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_ENCODER_SINK_PAD (s));
117 
118   /* Set default encoding as RAW as ASCII takes up 4 time more bytes */
119   s->info.encoding = GST_PNM_ENCODING_RAW;
120 }
121 
122 static void
gst_pnmenc_finalize(GObject * object)123 gst_pnmenc_finalize (GObject * object)
124 {
125   GstPnmenc *pnmenc = GST_PNMENC (object);
126   if (pnmenc->input_state)
127     gst_video_codec_state_unref (pnmenc->input_state);
128 
129   G_OBJECT_CLASS (parent_class)->finalize (object);
130 }
131 
132 static gboolean
gst_pnmenc_set_format(GstVideoEncoder * encoder,GstVideoCodecState * state)133 gst_pnmenc_set_format (GstVideoEncoder * encoder, GstVideoCodecState * state)
134 {
135   GstPnmenc *pnmenc;
136   gboolean ret = TRUE;
137   GstVideoInfo *info;
138   GstVideoCodecState *output_state;
139   const gchar *mime_type = NULL;
140 
141   pnmenc = GST_PNMENC (encoder);
142   info = &state->info;
143 
144   switch (GST_VIDEO_INFO_FORMAT (info)) {
145     case GST_VIDEO_FORMAT_RGB:
146       pnmenc->info.max = 255;
147       pnmenc->info.type = GST_PNM_TYPE_PIXMAP;
148       mime_type = MIME_PM;
149       break;
150     case GST_VIDEO_FORMAT_GRAY8:
151       pnmenc->info.max = 255;
152       pnmenc->info.type = GST_PNM_TYPE_GRAYMAP;
153       mime_type = MIME_GM;
154       break;
155     case GST_VIDEO_FORMAT_GRAY16_BE:
156     case GST_VIDEO_FORMAT_GRAY16_LE:
157       pnmenc->info.max = 65535;
158       pnmenc->info.type = GST_PNM_TYPE_GRAYMAP;
159       mime_type = MIME_GM;
160       break;
161     default:
162       ret = FALSE;
163       goto done;
164   }
165 
166   pnmenc->info.width = GST_VIDEO_INFO_WIDTH (info);
167   pnmenc->info.height = GST_VIDEO_INFO_HEIGHT (info);
168 
169   if (pnmenc->input_state)
170     gst_video_codec_state_unref (pnmenc->input_state);
171   pnmenc->input_state = gst_video_codec_state_ref (state);
172 
173   output_state =
174       gst_video_encoder_set_output_state (encoder,
175       gst_caps_new_empty_simple (mime_type), state);
176   gst_video_codec_state_unref (output_state);
177 
178 done:
179   return ret;
180 }
181 
182 static GstFlowReturn
gst_pnmenc_handle_frame(GstVideoEncoder * encoder,GstVideoCodecFrame * frame)183 gst_pnmenc_handle_frame (GstVideoEncoder * encoder, GstVideoCodecFrame * frame)
184 {
185   GstPnmenc *pnmenc;
186   guint size, pixels, bytesize;
187   GstMapInfo omap;
188   gchar *header = NULL;
189   GstVideoInfo *info;
190   GstFlowReturn ret = GST_FLOW_OK;
191   guint i_rowstride, o_rowstride;
192   guint bytes = 0, index, head_size;
193   guint i, j;
194   guint maxbytes_per_pixel, str_len;
195   gchar format_str[4];
196   GstVideoFrame in_frame;
197   pnmenc = GST_PNMENC (encoder);
198   info = &pnmenc->input_state->info;
199 
200   switch (GST_VIDEO_INFO_FORMAT (info)) {
201     case GST_VIDEO_FORMAT_RGB:
202       pixels = size = pnmenc->info.width * pnmenc->info.height * 3;
203       bytesize = 1;
204       maxbytes_per_pixel = 4;
205       str_len = 3;
206       g_strlcpy (format_str, "%3i", 4);
207       break;
208     case GST_VIDEO_FORMAT_GRAY8:
209       pixels = size = pnmenc->info.width * pnmenc->info.height * 1;
210       bytesize = 1;
211       maxbytes_per_pixel = 4;
212       str_len = 3;
213       g_strlcpy (format_str, "%3i", 4);
214       break;
215     case GST_VIDEO_FORMAT_GRAY16_LE:
216     case GST_VIDEO_FORMAT_GRAY16_BE:
217       pixels = pnmenc->info.width * pnmenc->info.height * 1;
218       bytesize = 2;
219       size = pixels * bytesize;
220       maxbytes_per_pixel = 6;
221       str_len = 5;
222       g_strlcpy (format_str, "%5i", 4);
223       break;
224     default:
225       ret = FALSE;
226       goto done;
227   }
228 
229   header = g_strdup_printf ("P%i\n%i %i\n%i\n",
230       pnmenc->info.type + 3 * (1 - pnmenc->info.encoding), pnmenc->info.width,
231       pnmenc->info.height, pnmenc->info.max);
232 
233   if (pnmenc->info.encoding == GST_PNM_ENCODING_ASCII) {
234     /* Per component 4 bytes are used in case of ASCII encoding */
235     size = size * 4 + size / 20;
236     size += strlen (header);
237     frame->output_buffer =
238         gst_video_encoder_allocate_output_buffer (encoder, (size));
239   } else {
240     size += strlen (header);
241     frame->output_buffer =
242         gst_video_encoder_allocate_output_buffer (encoder, size);
243   }
244 
245   if (gst_buffer_map (frame->output_buffer, &omap, GST_MAP_WRITE) == FALSE) {
246     ret = GST_FLOW_ERROR;
247     goto done;
248   }
249   if (!gst_video_frame_map (&in_frame, &(pnmenc->input_state->info),
250           frame->input_buffer, GST_MAP_READ)) {
251     /* Unmap already mapped buffer */
252     gst_buffer_unmap (frame->output_buffer, &omap);
253     ret = GST_FLOW_ERROR;
254     goto done;
255   }
256   /* Copy out the header first */
257   head_size = strlen (header);
258   memcpy (omap.data, header, head_size);
259 
260   if (pnmenc->info.encoding == GST_PNM_ENCODING_ASCII) {
261     /* We need to convert to ASCII */
262     /* Convert from gstreamer rowstride to PNM rowstride as we go */
263     if (pnmenc->info.type == GST_PNM_TYPE_PIXMAP) {
264       o_rowstride = 3 * pnmenc->info.width;
265     } else {
266       o_rowstride = pnmenc->info.width;
267     }
268     i_rowstride = GST_VIDEO_FRAME_PLANE_STRIDE (&in_frame, 0);
269 
270     switch (GST_VIDEO_INFO_FORMAT (info)) {
271       case GST_VIDEO_FORMAT_RGB:
272       case GST_VIDEO_FORMAT_GRAY8:
273         for (i = 0; i < pnmenc->info.height; i++) {
274           index = i * i_rowstride;
275           for (j = 0; j < o_rowstride; j++, bytes++, index++) {
276             g_snprintf ((char *) omap.data + head_size, maxbytes_per_pixel,
277                 format_str, in_frame.map[0].data[index]);
278             head_size += str_len;
279             omap.data[head_size++] = ' ';
280             /* Add new line so that file will not end up with single big line */
281             if (!((bytes + 1) % 20))
282               omap.data[head_size++] = '\n';
283           }
284         }
285         break;
286       case GST_VIDEO_FORMAT_GRAY16_BE:
287         for (i = 0; i < pnmenc->info.height; i++) {
288           index = i * i_rowstride;
289           for (j = 0; j < o_rowstride; j++, bytes++, index += 2) {
290             g_snprintf ((char *) omap.data + head_size, maxbytes_per_pixel,
291                 format_str, GST_READ_UINT16_BE (in_frame.map[0].data + index));
292             head_size += str_len;
293             omap.data[head_size++] = ' ';
294             /* Add new line so that file will not end up with single big line */
295             if (!((bytes + 1) % 20))
296               omap.data[head_size++] = '\n';
297           }
298         }
299         break;
300       case GST_VIDEO_FORMAT_GRAY16_LE:
301         for (i = 0; i < pnmenc->info.height; i++) {
302           index = i * i_rowstride;
303           for (j = 0; j < o_rowstride; j++, bytes++, index += 2) {
304             g_snprintf ((char *) omap.data + head_size, maxbytes_per_pixel,
305                 format_str, GST_READ_UINT16_LE (in_frame.map[0].data + index));
306             head_size += str_len;
307             omap.data[head_size++] = ' ';
308             /* Add new line so that file will not end up with single big line */
309             if (!((bytes + 1) % 20))
310               omap.data[head_size++] = '\n';
311           }
312         }
313         break;
314       default:
315         GST_ERROR_OBJECT (encoder, "Unhandled format %s",
316             gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (info)));
317         gst_buffer_unmap (frame->output_buffer, &omap);
318         gst_video_frame_unmap (&in_frame);
319         g_free (header);
320         return GST_FLOW_ERROR;
321     }
322 
323     gst_buffer_set_size (frame->output_buffer, head_size);
324   } else {
325     guint out_index = head_size;
326 
327     /* Binary output. 8-bit, or 16-bit BE */
328     if (pnmenc->info.type == GST_PNM_TYPE_PIXMAP) {
329       o_rowstride = 3 * pnmenc->info.width * bytesize;
330     } else {
331       o_rowstride = pnmenc->info.width * bytesize;
332     }
333     i_rowstride = GST_VIDEO_FRAME_PLANE_STRIDE (&in_frame, 0);
334 
335     switch (GST_VIDEO_INFO_FORMAT (info)) {
336       case GST_VIDEO_FORMAT_GRAY16_BE:
337         for (i = 0; i < pnmenc->info.height; i++) {
338           index = i * i_rowstride;
339           for (j = 0; j < o_rowstride; j += 2, index += 2) {
340             guint16 val = GST_READ_UINT16_LE (in_frame.map[0].data + index);
341             GST_WRITE_UINT16_BE (omap.data + out_index, val);
342             out_index += 2;
343           }
344         }
345         break;
346       case GST_VIDEO_FORMAT_GRAY16_LE:
347         for (i = 0; i < pnmenc->info.height; i++) {
348           index = i * i_rowstride;
349           for (j = 0; j < o_rowstride; j += 2, index += 2) {
350             guint16 val = GST_READ_UINT16_LE (in_frame.map[0].data + index);
351             GST_WRITE_UINT16_BE (omap.data + out_index, val);
352             out_index += 2;
353           }
354         }
355         break;
356       default:
357         for (i = 0; i < pnmenc->info.height; i++) {
358           memcpy (omap.data + head_size + o_rowstride * i,
359               in_frame.map[0].data + i_rowstride * i, o_rowstride);
360         }
361     }
362   }
363 
364   gst_buffer_unmap (frame->output_buffer, &omap);
365   gst_video_frame_unmap (&in_frame);
366 
367   if ((ret = gst_video_encoder_finish_frame (encoder, frame)) != GST_FLOW_OK)
368     goto done;
369 
370 done:
371   g_free (header);
372   return ret;
373 }
374 
375 static void
gst_pnmenc_class_init(GstPnmencClass * klass)376 gst_pnmenc_class_init (GstPnmencClass * klass)
377 {
378   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
379   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
380   GstVideoEncoderClass *venc_class = (GstVideoEncoderClass *) klass;
381 
382   parent_class = g_type_class_peek_parent (klass);
383   gobject_class->set_property = gst_pnmenc_set_property;
384   gobject_class->get_property = gst_pnmenc_get_property;
385 
386   g_object_class_install_property (gobject_class, GST_PNMENC_PROP_ASCII,
387       g_param_spec_boolean ("ascii", "ASCII Encoding", "The output will be "
388           "ASCII encoded", FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
389 
390   gst_element_class_add_static_pad_template (element_class, &sink_pad_template);
391   gst_element_class_add_static_pad_template (element_class, &src_pad_template);
392 
393   gst_element_class_set_static_metadata (element_class, "PNM image encoder",
394       "Codec/Encoder/Image",
395       "Encodes images into portable pixmap or graymap (PNM) format",
396       "Lutz Mueller <lutz@users.sourceforge.net>");
397 
398   venc_class->set_format = gst_pnmenc_set_format;
399   venc_class->handle_frame = gst_pnmenc_handle_frame;
400   gobject_class->finalize = gst_pnmenc_finalize;
401 }
402