summaryrefslogtreecommitdiff
path: root/omx/gstomxh264enc.c
blob: 4af175a9d287ddb07c64fff93bb1d588a852a7a0 (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
 * Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
 *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
 * Copyright (c) 2013-2015, NVIDIA CORPORATION.  All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation
 * version 2.1 of the License.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 *
 */

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

#include <gst/gst.h>

#include "gstomxh264enc.h"

GST_DEBUG_CATEGORY_STATIC (gst_omx_h264_enc_debug_category);
#define GST_CAT_DEFAULT gst_omx_h264_enc_debug_category

/* prototypes */
static gboolean gst_omx_h264_enc_set_format (GstOMXVideoEnc * enc,
    GstOMXPort * port, GstVideoCodecState * state);
static GstCaps *gst_omx_h264_enc_get_caps (GstOMXVideoEnc * enc,
    GstOMXPort * port, GstVideoCodecState * state);
static GstFlowReturn gst_omx_h264_enc_handle_output_frame (GstOMXVideoEnc *
    self, GstOMXPort * port, GstOMXBuffer * buf, GstVideoCodecFrame * frame);
static void gst_omx_h264_enc_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_omx_h264_enc_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec);

enum
{
  PROP_0,
  PROP_INSERT_SPS_PPS
};

/* class initialization */

#define DEBUG_INIT \
  GST_DEBUG_CATEGORY_INIT (gst_omx_h264_enc_debug_category, "omxh264enc", 0, \
      "debug category for gst-omx video encoder base class");

G_DEFINE_TYPE_WITH_CODE (GstOMXH264Enc, gst_omx_h264_enc,
    GST_TYPE_OMX_VIDEO_ENC, DEBUG_INIT);

static void
gst_omx_h264_enc_class_init (GstOMXH264EncClass * klass)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
  GstOMXVideoEncClass *videoenc_class = GST_OMX_VIDEO_ENC_CLASS (klass);
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  videoenc_class->set_format = GST_DEBUG_FUNCPTR (gst_omx_h264_enc_set_format);
  videoenc_class->get_caps = GST_DEBUG_FUNCPTR (gst_omx_h264_enc_get_caps);
  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_omx_h264_enc_set_property);
  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_omx_h264_enc_get_property);

  videoenc_class->cdata.default_src_template_caps = "video/x-h264, "
      "width=(int) [ 16, 4096 ], " "height=(int) [ 16, 4096 ], "
      "stream-format=(string) { byte-stream, avc }, " "alignment=(string) au ";
  videoenc_class->handle_output_frame =
      GST_DEBUG_FUNCPTR (gst_omx_h264_enc_handle_output_frame);

  gst_element_class_set_static_metadata (element_class,
      "OpenMAX H.264 Video Encoder",
      "Codec/Encoder/Video",
      "Encode H.264 video streams",
      "Sebastian Dröge <sebastian.droege@collabora.co.uk>");

  gst_omx_set_default_role (&videoenc_class->cdata, "video_encoder.avc");

  g_object_class_install_property (gobject_class, PROP_INSERT_SPS_PPS,
      g_param_spec_boolean ("insert-sps-pps",
          "Insert H.264 SPS, PPS",
          "Insert H.264 SPS, PPS at every IDR frame",
          FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}

static void
gst_omx_h264_enc_init (GstOMXH264Enc * self)
{
  self->insert_sps_pps = FALSE;
}

static OMX_ERRORTYPE
ifi_setup (GstOMXVideoEnc * enc)
{
  GstOMXH264Enc *self = GST_OMX_H264_ENC (enc);
  OMX_ERRORTYPE err = OMX_ErrorNone;

  if (enc->iframeinterval != 0xffffffff) {
    OMX_VIDEO_CONFIG_AVCINTRAPERIOD oIntraPeriod;
    GST_OMX_INIT_STRUCT (&oIntraPeriod);
    oIntraPeriod.nPortIndex = enc->enc_out_port->index;

    err =
      gst_omx_component_get_config (GST_OMX_VIDEO_ENC (self)->enc,
          OMX_IndexConfigVideoAVCIntraPeriod,
          &oIntraPeriod);

    if (err == OMX_ErrorNone) {
      if (enc->iframeinterval != 0) {
        oIntraPeriod.nIDRPeriod = enc->iframeinterval;
        oIntraPeriod.nPFrames = enc->iframeinterval - 1;
      }

      err =
        gst_omx_component_set_config (GST_OMX_VIDEO_ENC (self)->enc,
            OMX_IndexConfigVideoAVCIntraPeriod,
            &oIntraPeriod);
    }
  }
  return err;
}

static OMX_ERRORTYPE
gst_omx_h264_enc_set_insert_sps_pps (GstOMXVideoEnc * enc)
{
   OMX_INDEXTYPE eIndex;
   OMX_ERRORTYPE eError = OMX_ErrorNone;
   NVX_PARAM_VIDENCPROPERTY oEncodeProp;
   GstOMXH264Enc *self = GST_OMX_H264_ENC (enc);

   if (self->insert_sps_pps) {
     GST_OMX_INIT_STRUCT (&oEncodeProp);
     oEncodeProp.nPortIndex = enc->enc_out_port->index;

     eError = gst_omx_component_get_index (GST_OMX_VIDEO_ENC (self)->enc,
       (gpointer) NVX_INDEX_PARAM_VIDEO_ENCODE_PROPERTY, &eIndex);

     if (eError == OMX_ErrorNone) {
       eError =
         gst_omx_component_get_parameter (GST_OMX_VIDEO_ENC (self)->enc,
                                                 eIndex, &oEncodeProp);
       if (eError == OMX_ErrorNone) {
         oEncodeProp.bInsertSPSPPSAtIDR = self->insert_sps_pps;

         eError =
           gst_omx_component_set_parameter (GST_OMX_VIDEO_ENC (self)->enc, eIndex, &oEncodeProp);
       }
     }
   }
   return eError;
 }

static gboolean
gst_omx_h264_enc_set_format (GstOMXVideoEnc * enc, GstOMXPort * port,
    GstVideoCodecState * state)
{
  GstOMXH264Enc *self = GST_OMX_H264_ENC (enc);
  GstCaps *peercaps;
  OMX_PARAM_PORTDEFINITIONTYPE port_def;
  OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
  OMX_VIDEO_CONFIG_NALSIZE OMXNalSize;
  OMX_ERRORTYPE err;
  const gchar *profile_string, *level_string;
  const gchar *out_format = NULL;

  gst_omx_port_get_port_definition (GST_OMX_VIDEO_ENC (self)->enc_out_port,
      &port_def);
  port_def.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
  err =
      gst_omx_port_update_port_definition (GST_OMX_VIDEO_ENC
      (self)->enc_out_port, &port_def);
  if (err != OMX_ErrorNone)
    return FALSE;

  GST_OMX_INIT_STRUCT (&param);
  param.nPortIndex = GST_OMX_VIDEO_ENC (self)->enc_out_port->index;

  err =
      gst_omx_component_get_parameter (GST_OMX_VIDEO_ENC (self)->enc,
      OMX_IndexParamVideoProfileLevelCurrent, &param);
  if (err != OMX_ErrorNone) {
    GST_WARNING_OBJECT (self,
        "Setting profile/level not supported by component");
  }

  peercaps = gst_pad_peer_query_caps (GST_VIDEO_ENCODER_SRC_PAD (enc),
      gst_pad_get_pad_template_caps (GST_VIDEO_ENCODER_SRC_PAD (enc)));
  if (peercaps) {
    GstStructure *s;

    if (gst_caps_is_empty (peercaps)) {
      gst_caps_unref (peercaps);
      GST_ERROR_OBJECT (self, "Empty caps");
      return FALSE;
    }

    s = gst_caps_get_structure (peercaps, 0);

    if (err == OMX_ErrorNone) {
      profile_string = gst_structure_get_string (s, "profile");
      if (profile_string) {
        if (g_str_equal (profile_string, "baseline")) {
          param.eProfile = OMX_VIDEO_AVCProfileBaseline;
        } else if (g_str_equal (profile_string, "main")) {
          param.eProfile = OMX_VIDEO_AVCProfileMain;
        } else if (g_str_equal (profile_string, "extended")) {
          param.eProfile = OMX_VIDEO_AVCProfileExtended;
        } else if (g_str_equal (profile_string, "high")) {
          param.eProfile = OMX_VIDEO_AVCProfileHigh;
        } else if (g_str_equal (profile_string, "high-10")) {
          param.eProfile = OMX_VIDEO_AVCProfileHigh10;
        } else if (g_str_equal (profile_string, "high-4:2:2")) {
          param.eProfile = OMX_VIDEO_AVCProfileHigh422;
        } else if (g_str_equal (profile_string, "high-4:4:4")) {
          param.eProfile = OMX_VIDEO_AVCProfileHigh444;
        } else {
          goto unsupported_profile;
        }
      }
      level_string = gst_structure_get_string (s, "level");
      if (level_string) {
        if (g_str_equal (level_string, "1")) {
          param.eLevel = OMX_VIDEO_AVCLevel1;
        } else if (g_str_equal (level_string, "1b")) {
          param.eLevel = OMX_VIDEO_AVCLevel1b;
        } else if (g_str_equal (level_string, "1.1")) {
          param.eLevel = OMX_VIDEO_AVCLevel11;
        } else if (g_str_equal (level_string, "1.2")) {
          param.eLevel = OMX_VIDEO_AVCLevel12;
        } else if (g_str_equal (level_string, "1.3")) {
          param.eLevel = OMX_VIDEO_AVCLevel13;
        } else if (g_str_equal (level_string, "2")) {
          param.eLevel = OMX_VIDEO_AVCLevel2;
        } else if (g_str_equal (level_string, "2.1")) {
          param.eLevel = OMX_VIDEO_AVCLevel21;
        } else if (g_str_equal (level_string, "2.2")) {
          param.eLevel = OMX_VIDEO_AVCLevel22;
        } else if (g_str_equal (level_string, "3")) {
          param.eLevel = OMX_VIDEO_AVCLevel3;
        } else if (g_str_equal (level_string, "3.1")) {
          param.eLevel = OMX_VIDEO_AVCLevel31;
        } else if (g_str_equal (level_string, "3.2")) {
          param.eLevel = OMX_VIDEO_AVCLevel32;
        } else if (g_str_equal (level_string, "4")) {
          param.eLevel = OMX_VIDEO_AVCLevel4;
        } else if (g_str_equal (level_string, "4.1")) {
          param.eLevel = OMX_VIDEO_AVCLevel41;
        } else if (g_str_equal (level_string, "4.2")) {
          param.eLevel = OMX_VIDEO_AVCLevel42;
        } else if (g_str_equal (level_string, "5")) {
          param.eLevel = OMX_VIDEO_AVCLevel5;
        } else if (g_str_equal (level_string, "5.1")) {
          param.eLevel = OMX_VIDEO_AVCLevel51;
        } else {
          goto unsupported_level;
        }
      }

      err =
          gst_omx_component_set_parameter (GST_OMX_VIDEO_ENC (self)->enc,
          OMX_IndexParamVideoProfileLevelCurrent, &param);
      if (err == OMX_ErrorUnsupportedIndex) {
        GST_WARNING_OBJECT (self,
            "Setting profile/level not supported by component");
      } else if (err != OMX_ErrorNone) {
        GST_ERROR_OBJECT (self,
            "Error setting profile %u and level %u: %s (0x%08x)",
            (guint) param.eProfile, (guint) param.eLevel,
            gst_omx_error_to_string (err), err);
        return FALSE;
      }
    }

    out_format = gst_structure_get_string (s, "stream-format");
    if (out_format) {
      if (g_str_equal (out_format, "avc")) {
        self->stream_format = H264_AVC;
      } else if (g_str_equal (out_format, "byte-stream")) {
        self->stream_format = H264_BTS;
      } else {
        goto unsupported_out_format;
      }
    }
    gst_caps_unref (peercaps);
  }

  if (self->stream_format == H264_AVC) {
    GST_OMX_INIT_STRUCT (&OMXNalSize);
    err =
        gst_omx_component_get_config (GST_OMX_VIDEO_ENC (self)->enc,
        OMX_IndexConfigVideoNalSize, &OMXNalSize);
    if (err == OMX_ErrorNone) {
      OMXNalSize.nNaluBytes = 4;
      OMXNalSize.nPortIndex = GST_OMX_VIDEO_ENC (self)->enc_out_port->index;
      err =
          gst_omx_component_set_config (GST_OMX_VIDEO_ENC (self)->enc,
          OMX_IndexConfigVideoNalSize, &OMXNalSize);
      if (err != OMX_ErrorNone) {
        return FALSE;
      }
    }
  }

  if (enc->iframeinterval != 0xffffffff) {
    err = ifi_setup (enc);
    if (err != OMX_ErrorNone) {
      GST_WARNING_OBJECT (self,
          "Error setting iframeinterval %u : %s (0x%08x)",
          (guint) enc->iframeinterval,
          gst_omx_error_to_string (err), err);
      return FALSE;
    }
  }

  if (self->insert_sps_pps) {
    err = gst_omx_h264_enc_set_insert_sps_pps (enc);
    if (err != OMX_ErrorNone) {
      GST_WARNING_OBJECT (self,
          "Error setting insert sps pps: %s (0x%08x)",
          gst_omx_error_to_string (err), err);
      return FALSE;
    }
  }

  return TRUE;

unsupported_profile:
  GST_ERROR_OBJECT (self, "Unsupported profile %s", profile_string);
  gst_caps_unref (peercaps);
  return FALSE;

unsupported_level:
  GST_ERROR_OBJECT (self, "Unsupported level %s", level_string);
  gst_caps_unref (peercaps);
  return FALSE;

unsupported_out_format:
  GST_ERROR_OBJECT (self, "Unsupported stream-format %s", out_format);
  gst_caps_unref (peercaps);
  return FALSE;
}

static GstCaps *
gst_omx_h264_enc_get_caps (GstOMXVideoEnc * enc, GstOMXPort * port,
    GstVideoCodecState * state)
{
  GstOMXH264Enc *self = GST_OMX_H264_ENC (enc);
  GstCaps *caps;
  OMX_ERRORTYPE err;
  OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
  const gchar *profile, *level, *out_format;

  caps = gst_caps_new_simple ("video/x-h264",
      "alignment", G_TYPE_STRING, "au", NULL);

  GST_OMX_INIT_STRUCT (&param);
  param.nPortIndex = GST_OMX_VIDEO_ENC (self)->enc_out_port->index;

  err =
      gst_omx_component_get_parameter (GST_OMX_VIDEO_ENC (self)->enc,
      OMX_IndexParamVideoProfileLevelCurrent, &param);
  if (err != OMX_ErrorNone && err != OMX_ErrorUnsupportedIndex
      && err != OMX_ErrorNotImplemented)
    return NULL;

  if (err == OMX_ErrorNone) {
    switch (param.eProfile) {
      case OMX_VIDEO_AVCProfileBaseline:
        profile = "baseline";
        break;
      case OMX_VIDEO_AVCProfileMain:
        profile = "main";
        break;
      case OMX_VIDEO_AVCProfileExtended:
        profile = "extended";
        break;
      case OMX_VIDEO_AVCProfileHigh:
        profile = "high";
        break;
      case OMX_VIDEO_AVCProfileHigh10:
        profile = "high-10";
        break;
      case OMX_VIDEO_AVCProfileHigh422:
        profile = "high-4:2:2";
        break;
      case OMX_VIDEO_AVCProfileHigh444:
        profile = "high-4:4:4";
        break;
      default:
        g_assert_not_reached ();
        return NULL;
    }

    switch (param.eLevel) {
      case OMX_VIDEO_AVCLevel1:
        level = "1";
        break;
      case OMX_VIDEO_AVCLevel1b:
        level = "1b";
        break;
      case OMX_VIDEO_AVCLevel11:
        level = "1.1";
        break;
      case OMX_VIDEO_AVCLevel12:
        level = "1.2";
        break;
      case OMX_VIDEO_AVCLevel13:
        level = "1.3";
        break;
      case OMX_VIDEO_AVCLevel2:
        level = "2";
        break;
      case OMX_VIDEO_AVCLevel21:
        level = "2.1";
        break;
      case OMX_VIDEO_AVCLevel22:
        level = "2.2";
        break;
      case OMX_VIDEO_AVCLevel3:
        level = "3";
        break;
      case OMX_VIDEO_AVCLevel31:
        level = "3.1";
        break;
      case OMX_VIDEO_AVCLevel32:
        level = "3.2";
        break;
      case OMX_VIDEO_AVCLevel4:
        level = "4";
        break;
      case OMX_VIDEO_AVCLevel41:
        level = "4.1";
        break;
      case OMX_VIDEO_AVCLevel42:
        level = "4.2";
        break;
      case OMX_VIDEO_AVCLevel5:
        level = "5";
        break;
      case OMX_VIDEO_AVCLevel51:
        level = "5.1";
        break;
      default:
        g_assert_not_reached ();
        return NULL;
    }

    gst_caps_set_simple (caps,
        "profile", G_TYPE_STRING, profile, "level", G_TYPE_STRING, level, NULL);
  }

  switch (self->stream_format) {
    case H264_AVC:
      out_format = "avc";
      break;
    case H264_BTS:
      out_format = "byte-stream";
      break;
    default:
      g_assert_not_reached ();
      return NULL;
  }

  gst_caps_set_simple (caps, "stream-format", G_TYPE_STRING, out_format, NULL);

  return caps;
}

static GstFlowReturn
gst_omx_h264_enc_handle_output_frame (GstOMXVideoEnc * self, GstOMXPort * port,
    GstOMXBuffer * buf, GstVideoCodecFrame * frame)
{
  GstOMXH264Enc *h264enc = GST_OMX_H264_ENC (self);

  if (buf->omx_buf->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
    /* The codec data is SPS/PPS with a startcode => bytestream stream format
     * For bytestream stream format the SPS/PPS is only in-stream and not
     * in the caps!
     */
    if (buf->omx_buf->nFilledLen >= 4 &&
        GST_READ_UINT32_BE (buf->omx_buf->pBuffer +
            buf->omx_buf->nOffset) == 0x00000001) {
      GList *l = NULL;
      GstBuffer *hdrs;
      GstMapInfo map = GST_MAP_INFO_INIT;

      GST_DEBUG_OBJECT (self, "got codecconfig in byte-stream format");
      buf->omx_buf->nFlags &= ~OMX_BUFFERFLAG_CODECCONFIG;

      hdrs = gst_buffer_new_and_alloc (buf->omx_buf->nFilledLen);

      gst_buffer_map (hdrs, &map, GST_MAP_WRITE);
      memcpy (map.data,
          buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
          buf->omx_buf->nFilledLen);
      gst_buffer_unmap (hdrs, &map);
      l = g_list_append (l, hdrs);
      gst_video_encoder_set_headers (GST_VIDEO_ENCODER (self), l);
    }
#ifdef USE_OMX_TARGET_TEGRA
    else {
      if (h264enc->stream_format == H264_AVC) {
        gsize inbuf_size;
        GstBuffer *codec_data_buf;
        GstMapInfo cdmap = GST_MAP_INFO_INIT;
        OMX_U32 length_to_allocate, offset;
        OMX_U8 *str_ptr;
        OMX_U8 *pts_ptr;
        OMX_U8 data[6];
        OMX_U32 *codec_data;
        OMX_U32 sps_length;

        GST_DEBUG_OBJECT (self, "create codec_data caps buffer");

        str_ptr = (buf->omx_buf->pBuffer + buf->omx_buf->nOffset);
        inbuf_size = buf->omx_buf->nFilledLen;

        codec_data =
            (OMX_U32 *) (buf->omx_buf->pBuffer + buf->omx_buf->nOffset);
        sps_length = (codec_data[0] >> 24);

        // Need additional 7 bytes of data to indicate no of sps,pps
        //and how many bytes indicate nal_length,profile etc also we
        //are going to strip down 2 bytes each for the sps and pps
        //length hence reducing the size by 4

        length_to_allocate = (inbuf_size + ADDITIONAL_LENGTH);

        codec_data_buf = gst_buffer_new_and_alloc (length_to_allocate);
        gst_buffer_map (codec_data_buf, &cdmap, GST_MAP_WRITE);

        data[0] = 0x01;
        data[1] = 0x42;         // 66 Baseline profile
        data[2] = 0x40;         // constrained Baseline
        data[3] = 0x15;         // level2.1
        data[4] = 0x03;         // nal length = 4 bytes
        data[5] = 0x01;         // 1SPS

        // append data of 6 bytes and then the sps length and sps data
        memcpy (cdmap.data, data, HEADER_DATA_LENGTH);

        // Strip down the length indicating the NAL data size from 4 bytes to 2 bytes.
        // This is because the down stream qtmux are expecting this to be only 2 bytes

        memcpy ((cdmap.data + HEADER_DATA_LENGTH), (str_ptr + 2),
            DOWNSTREAM_NAL_LENGTH_INDICATOR);

        // Now copy the SPS data
        memcpy ((cdmap.data + HEADER_DATA_LENGTH + 2),
            (str_ptr + ENCODER_NAL_LENGTH_INDICATOR), sps_length);

        offset =
            HEADER_DATA_LENGTH + DOWNSTREAM_NAL_LENGTH_INDICATOR + sps_length;

        pts_ptr = cdmap.data + offset;
        pts_ptr[0] = 1;         // 1 pps

        offset += 1;

        // Strip down the length indicating the NAL data size from 4 bytes to 2 bytes.
        // This is because the down stream qtmux are expecting this to be only 2 bytes
        memcpy ((cdmap.data + offset),
            (str_ptr + (ENCODER_NAL_LENGTH_INDICATOR + sps_length) + 2),
            DOWNSTREAM_NAL_LENGTH_INDICATOR);

        offset += 2;

        // Now copy pps data
        memcpy ((cdmap.data + offset),
            (str_ptr + ((ENCODER_NAL_LENGTH_INDICATOR << 1) + sps_length)),
            (inbuf_size - ((ENCODER_NAL_LENGTH_INDICATOR << 1) + sps_length)));

        gst_buffer_unmap (codec_data_buf, &cdmap);

        self->codec_data = codec_data_buf;
      }
    }
#endif
  }

  return
      GST_OMX_VIDEO_ENC_CLASS
      (gst_omx_h264_enc_parent_class)->handle_output_frame (self, port, buf,
      frame);
}

static void
gst_omx_h264_enc_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstOMXH264Enc *self = GST_OMX_H264_ENC (object);

  switch (prop_id) {
    case PROP_INSERT_SPS_PPS:
      self->insert_sps_pps = g_value_get_boolean (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_omx_h264_enc_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
  GstOMXH264Enc *self = GST_OMX_H264_ENC (object);

  switch (prop_id) {
    case PROP_INSERT_SPS_PPS:
      g_value_set_boolean (value, self->insert_sps_pps);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}