summaryrefslogtreecommitdiff
path: root/gst/pnm/gstpnmenc.c
blob: c637b7407902f1e2e80f4f1abdee594fa47a7178 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* GStreamer PNM encoder
 * Copyright (C) 2009 Lutz Mueller <lutz@users.sourceforge.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
 * SECTION:element-pnmenc
 * @title: pnmenc
 *
 * Encodes pnm images. This plugin supports both raw and ASCII encoding.
 * To enable ASCII encoding, set the parameter ascii to TRUE. If you omit
 * the parameter or set it to FALSE, the output will be raw encoded.
 *
 * ## Example launch line
 * |[
 * gst-launch-1.0 videotestsrc num_buffers=1 ! videoconvert ! "video/x-raw,format=GRAY8" ! pnmenc ascii=true ! filesink location=test.pnm
 * ]| The above pipeline writes a test pnm file (ASCII encoding).
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gstpnmenc.h"
#include "gstpnmutils.h"

#include <gst/gstutils.h>
#include <gst/video/video.h>
#include <gst/video/gstvideometa.h>
#include <stdio.h>

#include <string.h>

enum
{
  GST_PNMENC_PROP_0,
  GST_PNMENC_PROP_ASCII
      /* Add here. */
};



static GstStaticPadTemplate sink_pad_template =
GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
    GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
        ("{ RGB, GRAY8, GRAY16_BE, GRAY16_LE }")));


static GstStaticPadTemplate src_pad_template =
GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
    GST_STATIC_CAPS (MIME_ALL));

#define parent_class gst_pnmenc_parent_class
G_DEFINE_TYPE (GstPnmenc, gst_pnmenc, GST_TYPE_VIDEO_ENCODER);
GST_ELEMENT_REGISTER_DEFINE (pnmenc, "pnmenc", GST_RANK_PRIMARY,
    GST_TYPE_PNMENC);


static GstFlowReturn
gst_pnmenc_handle_frame (GstVideoEncoder * encoder, GstVideoCodecFrame * frame);

static void gst_pnmenc_finalize (GObject * object);

static void
gst_pnmenc_set_property (GObject * object, guint prop_id, const GValue * value,
    GParamSpec * pspec)
{
  GstPnmenc *s = GST_PNMENC (object);

  switch (prop_id) {
    case GST_PNMENC_PROP_ASCII:
      if (g_value_get_boolean (value)) {
        s->info.encoding = GST_PNM_ENCODING_ASCII;
      } else {
        s->info.encoding = GST_PNM_ENCODING_RAW;
      }
      s->info.fields |= GST_PNM_INFO_FIELDS_ENCODING;
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_pnmenc_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
  GstPnmenc *s = GST_PNMENC (object);

  switch (prop_id) {
    case GST_PNMENC_PROP_ASCII:
      g_value_set_boolean (value, s->info.encoding == GST_PNM_ENCODING_ASCII);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_pnmenc_init (GstPnmenc * s)
{
  GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_ENCODER_SINK_PAD (s));

  /* Set default encoding as RAW as ASCII takes up 4 time more bytes */
  s->info.encoding = GST_PNM_ENCODING_RAW;
}

static void
gst_pnmenc_finalize (GObject * object)
{
  GstPnmenc *pnmenc = GST_PNMENC (object);
  if (pnmenc->input_state)
    gst_video_codec_state_unref (pnmenc->input_state);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static gboolean
gst_pnmenc_set_format (GstVideoEncoder * encoder, GstVideoCodecState * state)
{
  GstPnmenc *pnmenc;
  gboolean ret = TRUE;
  GstVideoInfo *info;
  GstVideoCodecState *output_state;
  const gchar *mime_type = NULL;

  pnmenc = GST_PNMENC (encoder);
  info = &state->info;

  switch (GST_VIDEO_INFO_FORMAT (info)) {
    case GST_VIDEO_FORMAT_RGB:
      pnmenc->info.max = 255;
      pnmenc->info.type = GST_PNM_TYPE_PIXMAP;
      mime_type = MIME_PM;
      break;
    case GST_VIDEO_FORMAT_GRAY8:
      pnmenc->info.max = 255;
      pnmenc->info.type = GST_PNM_TYPE_GRAYMAP;
      mime_type = MIME_GM;
      break;
    case GST_VIDEO_FORMAT_GRAY16_BE:
    case GST_VIDEO_FORMAT_GRAY16_LE:
      pnmenc->info.max = 65535;
      pnmenc->info.type = GST_PNM_TYPE_GRAYMAP;
      mime_type = MIME_GM;
      break;
    default:
      ret = FALSE;
      goto done;
  }

  pnmenc->info.width = GST_VIDEO_INFO_WIDTH (info);
  pnmenc->info.height = GST_VIDEO_INFO_HEIGHT (info);

  if (pnmenc->input_state)
    gst_video_codec_state_unref (pnmenc->input_state);
  pnmenc->input_state = gst_video_codec_state_ref (state);

  output_state =
      gst_video_encoder_set_output_state (encoder,
      gst_caps_new_empty_simple (mime_type), state);
  gst_video_codec_state_unref (output_state);

done:
  return ret;
}

static GstFlowReturn
gst_pnmenc_handle_frame (GstVideoEncoder * encoder, GstVideoCodecFrame * frame)
{
  GstPnmenc *pnmenc;
  guint size, pixels, bytesize;
  GstMapInfo omap;
  gchar *header = NULL;
  GstVideoInfo *info;
  GstFlowReturn ret = GST_FLOW_OK;
  guint i_rowstride, o_rowstride;
  guint bytes = 0, index, head_size;
  guint i, j;
  guint maxbytes_per_pixel, str_len;
  gchar format_str[4];
  GstVideoFrame in_frame;
  pnmenc = GST_PNMENC (encoder);
  info = &pnmenc->input_state->info;

  switch (GST_VIDEO_INFO_FORMAT (info)) {
    case GST_VIDEO_FORMAT_RGB:
      pixels = size = pnmenc->info.width * pnmenc->info.height * 3;
      bytesize = 1;
      maxbytes_per_pixel = 4;
      str_len = 3;
      g_strlcpy (format_str, "%3i", 4);
      break;
    case GST_VIDEO_FORMAT_GRAY8:
      pixels = size = pnmenc->info.width * pnmenc->info.height * 1;
      bytesize = 1;
      maxbytes_per_pixel = 4;
      str_len = 3;
      g_strlcpy (format_str, "%3i", 4);
      break;
    case GST_VIDEO_FORMAT_GRAY16_LE:
    case GST_VIDEO_FORMAT_GRAY16_BE:
      pixels = pnmenc->info.width * pnmenc->info.height * 1;
      bytesize = 2;
      size = pixels * bytesize;
      maxbytes_per_pixel = 6;
      str_len = 5;
      g_strlcpy (format_str, "%5i", 4);
      break;
    default:
      ret = FALSE;
      goto done;
  }

  header = g_strdup_printf ("P%i\n%i %i\n%i\n",
      pnmenc->info.type + 3 * (1 - pnmenc->info.encoding), pnmenc->info.width,
      pnmenc->info.height, pnmenc->info.max);

  if (pnmenc->info.encoding == GST_PNM_ENCODING_ASCII) {
    /* Per component 4 bytes are used in case of ASCII encoding */
    size = size * 4 + size / 20;
    size += strlen (header);
    frame->output_buffer =
        gst_video_encoder_allocate_output_buffer (encoder, (size));
  } else {
    size += strlen (header);
    frame->output_buffer =
        gst_video_encoder_allocate_output_buffer (encoder, size);
  }

  if (gst_buffer_map (frame->output_buffer, &omap, GST_MAP_WRITE) == FALSE) {
    ret = GST_FLOW_ERROR;
    goto done;
  }
  if (!gst_video_frame_map (&in_frame, &(pnmenc->input_state->info),
          frame->input_buffer, GST_MAP_READ)) {
    /* Unmap already mapped buffer */
    gst_buffer_unmap (frame->output_buffer, &omap);
    ret = GST_FLOW_ERROR;
    goto done;
  }
  /* Copy out the header first */
  head_size = strlen (header);
  memcpy (omap.data, header, head_size);

  if (pnmenc->info.encoding == GST_PNM_ENCODING_ASCII) {
    /* We need to convert to ASCII */
    /* Convert from gstreamer rowstride to PNM rowstride as we go */
    if (pnmenc->info.type == GST_PNM_TYPE_PIXMAP) {
      o_rowstride = 3 * pnmenc->info.width;
    } else {
      o_rowstride = pnmenc->info.width;
    }
    i_rowstride = GST_VIDEO_FRAME_PLANE_STRIDE (&in_frame, 0);

    switch (GST_VIDEO_INFO_FORMAT (info)) {
      case GST_VIDEO_FORMAT_RGB:
      case GST_VIDEO_FORMAT_GRAY8:
        for (i = 0; i < pnmenc->info.height; i++) {
          index = i * i_rowstride;
          for (j = 0; j < o_rowstride; j++, bytes++, index++) {
            g_snprintf ((char *) omap.data + head_size, maxbytes_per_pixel,
                format_str, in_frame.map[0].data[index]);
            head_size += str_len;
            omap.data[head_size++] = ' ';
            /* Add new line so that file will not end up with single big line */
            if (!((bytes + 1) % 20))
              omap.data[head_size++] = '\n';
          }
        }
        break;
      case GST_VIDEO_FORMAT_GRAY16_BE:
        for (i = 0; i < pnmenc->info.height; i++) {
          index = i * i_rowstride;
          for (j = 0; j < o_rowstride; j++, bytes++, index += 2) {
            g_snprintf ((char *) omap.data + head_size, maxbytes_per_pixel,
                format_str, GST_READ_UINT16_BE (in_frame.map[0].data + index));
            head_size += str_len;
            omap.data[head_size++] = ' ';
            /* Add new line so that file will not end up with single big line */
            if (!((bytes + 1) % 20))
              omap.data[head_size++] = '\n';
          }
        }
        break;
      case GST_VIDEO_FORMAT_GRAY16_LE:
        for (i = 0; i < pnmenc->info.height; i++) {
          index = i * i_rowstride;
          for (j = 0; j < o_rowstride; j++, bytes++, index += 2) {
            g_snprintf ((char *) omap.data + head_size, maxbytes_per_pixel,
                format_str, GST_READ_UINT16_LE (in_frame.map[0].data + index));
            head_size += str_len;
            omap.data[head_size++] = ' ';
            /* Add new line so that file will not end up with single big line */
            if (!((bytes + 1) % 20))
              omap.data[head_size++] = '\n';
          }
        }
        break;
      default:
        GST_ERROR_OBJECT (encoder, "Unhandled format %s",
            gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (info)));
        gst_buffer_unmap (frame->output_buffer, &omap);
        gst_video_frame_unmap (&in_frame);
        g_free (header);
        return GST_FLOW_ERROR;
    }

    gst_buffer_set_size (frame->output_buffer, head_size);
  } else {
    guint out_index = head_size;

    /* Binary output. 8-bit, or 16-bit BE */
    if (pnmenc->info.type == GST_PNM_TYPE_PIXMAP) {
      o_rowstride = 3 * pnmenc->info.width * bytesize;
    } else {
      o_rowstride = pnmenc->info.width * bytesize;
    }
    i_rowstride = GST_VIDEO_FRAME_PLANE_STRIDE (&in_frame, 0);

    switch (GST_VIDEO_INFO_FORMAT (info)) {
      case GST_VIDEO_FORMAT_GRAY16_BE:
        for (i = 0; i < pnmenc->info.height; i++) {
          index = i * i_rowstride;
          for (j = 0; j < o_rowstride; j += 2, index += 2) {
            guint16 val = GST_READ_UINT16_LE (in_frame.map[0].data + index);
            GST_WRITE_UINT16_BE (omap.data + out_index, val);
            out_index += 2;
          }
        }
        break;
      case GST_VIDEO_FORMAT_GRAY16_LE:
        for (i = 0; i < pnmenc->info.height; i++) {
          index = i * i_rowstride;
          for (j = 0; j < o_rowstride; j += 2, index += 2) {
            guint16 val = GST_READ_UINT16_LE (in_frame.map[0].data + index);
            GST_WRITE_UINT16_BE (omap.data + out_index, val);
            out_index += 2;
          }
        }
        break;
      default:
        for (i = 0; i < pnmenc->info.height; i++) {
          memcpy (omap.data + head_size + o_rowstride * i,
              in_frame.map[0].data + i_rowstride * i, o_rowstride);
        }
    }
  }

  gst_buffer_unmap (frame->output_buffer, &omap);
  gst_video_frame_unmap (&in_frame);

  if ((ret = gst_video_encoder_finish_frame (encoder, frame)) != GST_FLOW_OK)
    goto done;

done:
  g_free (header);
  return ret;
}

static void
gst_pnmenc_class_init (GstPnmencClass * klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
  GstVideoEncoderClass *venc_class = (GstVideoEncoderClass *) klass;

  parent_class = g_type_class_peek_parent (klass);
  gobject_class->set_property = gst_pnmenc_set_property;
  gobject_class->get_property = gst_pnmenc_get_property;

  g_object_class_install_property (gobject_class, GST_PNMENC_PROP_ASCII,
      g_param_spec_boolean ("ascii", "ASCII Encoding", "The output will be "
          "ASCII encoded", FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  gst_element_class_add_static_pad_template (element_class, &sink_pad_template);
  gst_element_class_add_static_pad_template (element_class, &src_pad_template);

  gst_element_class_set_static_metadata (element_class, "PNM image encoder",
      "Codec/Encoder/Image",
      "Encodes images into portable pixmap or graymap (PNM) format",
      "Lutz Mueller <lutz@users.sourceforge.net>");

  venc_class->set_format = gst_pnmenc_set_format;
  venc_class->handle_frame = gst_pnmenc_handle_frame;
  gobject_class->finalize = gst_pnmenc_finalize;
}