summaryrefslogtreecommitdiff
path: root/gst-libs/gst/mpegts/gst-scte-section.c
blob: e91191d12de0d8afdeb1bebe879c0b309193ba23 (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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
 * gst-scte-section.c -
 * Copyright (C) 2019 Centricular ltd
 *  Author: Edward Hervey <edward@centricular.com>
 *
 * 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.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <stdlib.h>

#include "mpegts.h"
#include "gstmpegts-private.h"
#define MPEGTIME_TO_GSTTIME(t) ((t) * (guint64)100000 / 9)

/**
 * SECTION:gst-scte-section
 * @title: SCTE variants of MPEG-TS sections
 * @short_description: Sections for the various SCTE specifications
 * @include: gst/mpegts/mpegts.h
 *
 * This contains the %GstMpegtsSection relevent to SCTE specifications.
 */

/* Splice Information Table (SIT) */

static GstMpegtsSCTESpliceEvent *
_gst_mpegts_scte_splice_event_copy (GstMpegtsSCTESpliceEvent * event)
{
  return g_slice_dup (GstMpegtsSCTESpliceEvent, event);
}

static void
_gst_mpegts_scte_splice_event_free (GstMpegtsSCTESpliceEvent * event)
{
  g_slice_free (GstMpegtsSCTESpliceEvent, event);
}

G_DEFINE_BOXED_TYPE (GstMpegtsSCTESpliceEvent, gst_mpegts_scte_splice_event,
    (GBoxedCopyFunc) _gst_mpegts_scte_splice_event_copy,
    (GFreeFunc) _gst_mpegts_scte_splice_event_free);

static GstMpegtsSCTESpliceEvent *
_parse_slice_event (guint8 ** orig_data, guint8 * end, gboolean insert_event)
{
  GstMpegtsSCTESpliceEvent *event = g_slice_new0 (GstMpegtsSCTESpliceEvent);
  guint8 *data = *orig_data;

  /* Note : +6 is because of the final descriptor_loop_length and CRC */
  if (data + 5 + 6 > end)
    goto error;

  event->insert_event = insert_event;
  event->splice_event_id = GST_READ_UINT32_BE (data);
  GST_LOG ("splice_event_id: 0x%08x", event->splice_event_id);
  data += 4;
  event->splice_event_cancel_indicator = *data >> 7;
  GST_LOG ("splice_event_cancel_indicator: %d",
      event->splice_event_cancel_indicator);
  data += 1;

  GST_DEBUG ("data %p", data);

  if (event->splice_event_cancel_indicator == 0) {
    if (data + 5 + 6 > end)
      goto error;
    event->out_of_network_indicator = *data >> 7;
    event->program_splice_flag = (*data >> 6) & 0x01;
    event->duration_flag = (*data >> 5) & 0x01;
    event->splice_immediate_flag = (*data >> 4) & 0x01;
    GST_LOG ("out_of_network_indicator:%d", event->out_of_network_indicator);
    GST_LOG ("program_splice_flag:%d", event->program_splice_flag);
    GST_LOG ("duration_flag:%d", event->duration_flag);
    GST_LOG ("splice_immediate_flag:%d", event->splice_immediate_flag);
    data += 1;

    if (event->program_splice_flag == 0) {
      GST_ERROR ("Component splice flag not supported !");
      goto error;
    }

    if (event->splice_immediate_flag == 0) {
      event->program_splice_time_specified = *data >> 7;
      if (event->program_splice_time_specified) {
        event->program_splice_time = ((guint64) (*data & 0x01)) << 32;
        data += 1;
        event->program_splice_time += GST_READ_UINT32_BE (data);
        data += 4;
        GST_LOG ("program_splice_time %" G_GUINT64_FORMAT " (%" GST_TIME_FORMAT
            ")", event->program_splice_time,
            GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (event->program_splice_time)));
      } else
        data += 1;
    }

    if (event->duration_flag) {
      event->break_duration_auto_return = *data >> 7;
      event->break_duration = ((guint64) (*data & 0x01)) << 32;
      data += 1;
      event->break_duration += GST_READ_UINT32_BE (data);
      data += 4;
      GST_LOG ("break_duration_auto_return:%d",
          event->break_duration_auto_return);
      GST_LOG ("break_duration %" G_GUINT64_FORMAT " (%" GST_TIME_FORMAT ")",
          event->break_duration,
          GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (event->break_duration)));
    }

    event->unique_program_id = GST_READ_UINT16_BE (data);
    GST_LOG ("unique_program_id:%" G_GUINT16_FORMAT, event->unique_program_id);
    data += 2;
    event->avail_num = *data++;
    event->avails_expected = *data++;
    GST_LOG ("avail %d/%d", event->avail_num, event->avails_expected);
  }

  GST_DEBUG ("done");
  *orig_data = data;
  return event;

error:
  {
    if (event)
      _gst_mpegts_scte_splice_event_free (event);
    return NULL;
  }
}

static GstMpegtsSCTESIT *
_gst_mpegts_scte_sit_copy (GstMpegtsSCTESIT * sit)
{
  GstMpegtsSCTESIT *copy = g_slice_dup (GstMpegtsSCTESIT, sit);

  copy->splices = g_ptr_array_ref (sit->splices);
  copy->descriptors = g_ptr_array_ref (sit->descriptors);

  return copy;
}

static void
_gst_mpegts_scte_sit_free (GstMpegtsSCTESIT * sit)
{
  g_ptr_array_unref (sit->splices);
  g_ptr_array_unref (sit->descriptors);
  g_slice_free (GstMpegtsSCTESIT, sit);
}

G_DEFINE_BOXED_TYPE (GstMpegtsSCTESIT, gst_mpegts_scte_sit,
    (GBoxedCopyFunc) _gst_mpegts_scte_sit_copy,
    (GFreeFunc) _gst_mpegts_scte_sit_free);


static gpointer
_parse_sit (GstMpegtsSection * section)
{
  GstMpegtsSCTESIT *sit = NULL;
  guint8 *data, *end;
  guint32 tmp;

  GST_DEBUG ("SIT");

  /* Even if the section is not a short one, it still uses CRC */
  if (_calc_crc32 (section->data, section->section_length) != 0) {
    GST_WARNING ("PID:0x%04x table_id:0x%02x, Bad CRC on section", section->pid,
        section->table_id);
    return NULL;
  }

  sit = g_slice_new0 (GstMpegtsSCTESIT);

  data = section->data;
  end = data + section->section_length;

  GST_MEMDUMP ("section", data, section->section_length);

  /* Skip already-checked fields */
  data += 3;

  /* Ensure protocol_version is 0 */
  if (*data != 0) {
    GST_WARNING ("Unsupported SCTE SIT protocol version (%d)", *data);
    goto error;
  }
  data += 1;

  /* encryption */
  sit->encrypted_packet = (*data) >> 7;
  sit->encryption_algorithm = (*data) & 0x3f;
  sit->pts_adjustment = ((guint64) (*data & 0x01)) << 32;
  data += 1;

  sit->pts_adjustment += GST_READ_UINT32_BE (data);
  data += 4;

  sit->cw_index = *data;
  data += 1;

  tmp = GST_READ_UINT24_BE (data);
  data += 3;

  sit->tier = (tmp >> 12);
  sit->splice_command_length = tmp & 0xfff;
  /* 0xfff is for backwards compatibility when reading */
  if (sit->splice_command_length == 0xfff)
    sit->splice_command_length = 0;
  GST_LOG ("command length %d", sit->splice_command_length);
  sit->splice_command_type = *data;
  data += 1;

  if (sit->splice_command_length
      && (data + sit->splice_command_length > end - 4)) {
    GST_WARNING ("PID %d invalid SCTE SIT splice command length %d",
        section->pid, sit->splice_command_length);
    goto error;
  }

  sit->splices = g_ptr_array_new_with_free_func ((GDestroyNotify)
      _gst_mpegts_scte_splice_event_free);
  switch (sit->splice_command_type) {
    case GST_MTS_SCTE_SPLICE_COMMAND_NULL:
    case GST_MTS_SCTE_SPLICE_COMMAND_BANDWIDTH:
      /* These commands have no payload */
      break;
    case GST_MTS_SCTE_SPLICE_COMMAND_TIME:
    {
      sit->splice_time_specified = (*data >> 7);
      if (sit->splice_time_specified) {
        sit->splice_time = ((guint64) (*data & 0x01)) << 32;
        data += 1;
        sit->splice_time += GST_READ_UINT32_BE (data);
        data += 4;
      } else
        data += 1;
    }
      break;
    case GST_MTS_SCTE_SPLICE_COMMAND_INSERT:
    {
      GstMpegtsSCTESpliceEvent *event = _parse_slice_event (&data, end, TRUE);
      if (event == NULL)
        goto error;
      g_ptr_array_add (sit->splices, event);
    }
      break;
    case GST_MTS_SCTE_SPLICE_COMMAND_PRIVATE:
    {
      GST_FIXME ("Implement SCTE-35 private commands");
      data += sit->splice_command_length;
    }
      break;
    default:
      GST_ERROR ("Unknown SCTE splice command type (0x%02x) !",
          sit->splice_command_type);
      goto error;
  }

  /* descriptors */
  tmp = GST_READ_UINT16_BE (data);
  data += 2;
  GST_MEMDUMP ("desc ?", data, tmp);
  sit->descriptors = gst_mpegts_parse_descriptors (data, tmp);
  if (!sit->descriptors) {
    GST_DEBUG ("no descriptors %d", tmp);
    goto error;
  }
  data += tmp;


  GST_DEBUG ("%p - %p", data, end);
  if (data != end - 4) {
    GST_WARNING ("PID %d invalid SIT parsed %d length %d",
        section->pid, (gint) (data - section->data), section->section_length);
    goto error;
  }

  return sit;

error:
  if (sit)
    _gst_mpegts_scte_sit_free (sit);

  return NULL;
}

/**
 * gst_mpegts_section_get_scte_sit:
 * @section: a #GstMpegtsSection of type %GST_MPEGTS_SECTION_SCTE_SIT
 *
 * Returns the #GstMpegtsSCTESIT contained in the @section.
 *
 * Returns: The #GstMpegtsSCTESIT contained in the section, or %NULL if an error
 * happened.
 */
const GstMpegtsSCTESIT *
gst_mpegts_section_get_scte_sit (GstMpegtsSection * section)
{
  g_return_val_if_fail (section->section_type == GST_MPEGTS_SECTION_SCTE_SIT,
      NULL);
  g_return_val_if_fail (section->cached_parsed || section->data, NULL);

  if (!section->cached_parsed)
    section->cached_parsed =
        __common_section_checks (section, 18, _parse_sit,
        (GDestroyNotify) _gst_mpegts_scte_sit_free);

  return (const GstMpegtsSCTESIT *) section->cached_parsed;
}

/**
 * gst_mpegts_scte_sit_new:
 *
 * Allocates and initializes a #GstMpegtsSCTESIT.
 *
 * Returns: (transfer full): A newly allocated #GstMpegtsSCTESIT
 */
GstMpegtsSCTESIT *
gst_mpegts_scte_sit_new (void)
{
  GstMpegtsSCTESIT *sit;

  sit = g_slice_new0 (GstMpegtsSCTESIT);

  /* Set all default values (which aren't already 0/NULL) */
  sit->tier = 0xfff;

  sit->splices = g_ptr_array_new_with_free_func ((GDestroyNotify)
      _gst_mpegts_scte_splice_event_free);
  sit->descriptors = g_ptr_array_new_with_free_func ((GDestroyNotify)
      gst_mpegts_descriptor_free);

  return sit;
}

/**
 * gst_mpegts_scte_null_new:
 *
 * Allocates and initializes a NULL command #GstMpegtsSCTESIT.
 *
 * Returns: (transfer full): A newly allocated #GstMpegtsSCTESIT
 */
GstMpegtsSCTESIT *
gst_mpegts_scte_null_new (void)
{
  GstMpegtsSCTESIT *sit = gst_mpegts_scte_sit_new ();

  sit->splice_command_type = GST_MTS_SCTE_SPLICE_COMMAND_NULL;
  return sit;
}

/**
 * gst_mpegts_scte_cancel_new:
 * @event_id: The event ID to cancel.
 *
 * Allocates and initializes a new INSERT command #GstMpegtsSCTESIT
 * setup to cancel the specified @event_id.
 *
 * Returns: (transfer full): A newly allocated #GstMpegtsSCTESIT
 */
GstMpegtsSCTESIT *
gst_mpegts_scte_cancel_new (guint32 event_id)
{
  GstMpegtsSCTESIT *sit = gst_mpegts_scte_sit_new ();
  GstMpegtsSCTESpliceEvent *event = gst_mpegts_scte_splice_event_new ();

  sit->splice_command_type = GST_MTS_SCTE_SPLICE_COMMAND_INSERT;
  event->splice_event_id = event_id;
  event->splice_event_cancel_indicator = TRUE;
  g_ptr_array_add (sit->splices, event);

  return sit;
}

/**
 * gst_mpegts_scte_splice_in_new:
 * @event_id: The event ID.
 * @splice_time: The PCR time for the splice event
 *
 * Allocates and initializes a new "Splice In" INSERT command
 * #GstMpegtsSCTESIT for the given @event_id and @splice_time.
 *
 * If the @splice_time is #G_MAXUINT64 then the event will be
 * immediate as opposed to for the target @splice_time.
 *
 * Returns: (transfer full): A newly allocated #GstMpegtsSCTESIT
 */
GstMpegtsSCTESIT *
gst_mpegts_scte_splice_in_new (guint32 event_id, guint64 splice_time)
{
  GstMpegtsSCTESIT *sit = gst_mpegts_scte_sit_new ();
  GstMpegtsSCTESpliceEvent *event = gst_mpegts_scte_splice_event_new ();

  sit->splice_command_type = GST_MTS_SCTE_SPLICE_COMMAND_INSERT;
  event->splice_event_id = event_id;
  if (splice_time == G_MAXUINT64) {
    event->splice_immediate_flag = TRUE;
  } else {
    event->program_splice_time_specified = TRUE;
    event->program_splice_time = splice_time;
  }
  g_ptr_array_add (sit->splices, event);

  return sit;
}

/**
 * gst_mpegts_scte_splice_out_new:
 * @event_id: The event ID.
 * @splice_time: The PCR time for the splice event
 * @duration: The optional duration.
 *
 * Allocates and initializes a new "Splice Out" INSERT command
 * #GstMpegtsSCTESIT for the given @event_id, @splice_time and
 * duration.
 *
 * If the @splice_time is #G_MAXUINT64 then the event will be
 * immediate as opposed to for the target @splice_time.
 *
 * If the @duration is 0 it won't be specified in the event.
 *
 * Returns: (transfer full): A newly allocated #GstMpegtsSCTESIT
 */
GstMpegtsSCTESIT *
gst_mpegts_scte_splice_out_new (guint32 event_id, guint64 splice_time,
    guint64 duration)
{
  GstMpegtsSCTESIT *sit = gst_mpegts_scte_sit_new ();
  GstMpegtsSCTESpliceEvent *event = gst_mpegts_scte_splice_event_new ();

  sit->splice_command_type = GST_MTS_SCTE_SPLICE_COMMAND_INSERT;
  event->splice_event_id = event_id;
  event->out_of_network_indicator = TRUE;
  if (splice_time == G_MAXUINT64) {
    event->splice_immediate_flag = TRUE;
  } else {
    event->program_splice_time_specified = TRUE;
    event->program_splice_time = splice_time;
  }
  if (duration != 0) {
    event->duration_flag = TRUE;
    event->break_duration = duration;
  }
  g_ptr_array_add (sit->splices, event);

  return sit;
}

/**
 * gst_mpegts_scte_splice_event_new:
 *
 * Allocates and initializes a #GstMpegtsSCTESpliceEvent.
 *
 * Returns: (transfer full): A newly allocated #GstMpegtsSCTESpliceEvent
 */
GstMpegtsSCTESpliceEvent *
gst_mpegts_scte_splice_event_new (void)
{
  GstMpegtsSCTESpliceEvent *event = g_slice_new0 (GstMpegtsSCTESpliceEvent);

  /* Non-0 Default values */
  event->program_splice_flag = TRUE;

  return event;
}

static gboolean
_packetize_sit (GstMpegtsSection * section)
{
  gsize length, command_length, descriptor_length;
  const GstMpegtsSCTESIT *sit;
  GstMpegtsDescriptor *descriptor;
  guint32 tmp32;
  guint i;
  guint8 *data;

  sit = gst_mpegts_section_get_scte_sit (section);

  if (sit == NULL)
    return FALSE;

  /* Skip cases we don't handle for now */
  if (sit->encrypted_packet) {
    GST_WARNING ("SCTE encrypted packet is not supported");
    return FALSE;
  }

  switch (sit->splice_command_type) {
    case GST_MTS_SCTE_SPLICE_COMMAND_SCHEDULE:
    case GST_MTS_SCTE_SPLICE_COMMAND_TIME:
    case GST_MTS_SCTE_SPLICE_COMMAND_PRIVATE:
      GST_WARNING ("SCTE command not supported");
      return FALSE;
    default:
      break;
  }

  /* Smallest splice section are the NULL and bandwith command:
   * 14 bytes for the header
   * 0 bytes for the command
   * 2 bytes for the empty descriptor loop length
   * 4 bytes for the CRC */
  length = 20;

  command_length = 0;
  /* Add the size of splices */
  for (i = 0; i < sit->splices->len; i++) {
    GstMpegtsSCTESpliceEvent *event = g_ptr_array_index (sit->splices, i);
    /* There is at least 5 bytes */
    command_length += 5;
    if (!event->splice_event_cancel_indicator) {
      if (!event->program_splice_flag) {
        GST_WARNING ("Only SCTE program splices are supported");
        return FALSE;
      }
      /* Add at least 5 bytes for common fields */
      command_length += 5;
      if (!event->splice_immediate_flag) {
        if (event->program_splice_time_specified)
          command_length += 5;
        else
          command_length += 1;
      }
      if (event->duration_flag)
        command_length += 5;
    }
  }
  length += command_length;

  /* Calculate size of descriptors */

  descriptor_length = 0;
  for (i = 0; i < sit->descriptors->len; i++) {
    descriptor = g_ptr_array_index (sit->descriptors, i);
    descriptor_length += descriptor->length + 2;
  }
  length += descriptor_length;

  /* Max length of SIT section is 4093 bytes */
  g_return_val_if_fail (length <= 4093, FALSE);

  _packetize_common_section (section, length);

  data = section->data + 3;
  /* Protocol version (default 0) */
  *data++ = 0;
  /* 7bits for encryption (not supported : 0) */
  /* 33bits for pts_adjustment */
  *data++ = (sit->pts_adjustment) >> 32 & 0x01;
  GST_WRITE_UINT32_BE (data, sit->pts_adjustment & 0xffffffff);
  data += 4;
  /* cw_index : 8 bit */
  *data++ = sit->cw_index;
  /* tier                  : 12bit
   * splice_command_length : 12bit
   * splice_command_type   : 8 bit */
  tmp32 = (sit->tier & 0xfff) << 20;
  tmp32 |= (command_length & 0xfff) << 8;
  tmp32 |= sit->splice_command_type;
  GST_WRITE_UINT32_BE (data, tmp32);
  data += 4;

  /* Write the events */
  for (i = 0; i < sit->splices->len; i++) {
    GstMpegtsSCTESpliceEvent *event = g_ptr_array_index (sit->splices, i);

    /* splice_event_id : 32bit */
    GST_WRITE_UINT32_BE (data, event->splice_event_id);
    data += 4;
    /* splice_event_cancel_indicator : 1bit
     * reserved                      ; 7bit */
    *data++ = event->splice_event_cancel_indicator ? 0xff : 0x7f;

    if (!event->splice_event_cancel_indicator) {
      /* out_of_network_indicator : 1bit
       * program_splice_flag      : 1bit
       * duration_flag            : 1bit
       * splice_immediate_flag    : 1bit
       * reserved                 : 4bit */
      *data++ = (event->out_of_network_indicator << 7) |
          (event->program_splice_flag << 6) |
          (event->duration_flag << 5) |
          (event->splice_immediate_flag << 4) | 0x0f;
      if (!event->splice_immediate_flag) {
        /* program_splice_time_specified : 1bit
         * reserved : 6/7 bit */
        if (!event->program_splice_time_specified)
          *data++ = 0x7f;
        else {
          /* time : 33bit */
          *data++ = 0xf2 | ((event->program_splice_time >> 32) & 0x1);
          GST_WRITE_UINT32_BE (data, event->program_splice_time & 0xffffffff);
          data += 4;
        }
      }
      if (event->duration_flag) {
        *data = event->break_duration_auto_return ? 0xfe : 0x7e;
        *data++ |= (event->break_duration >> 32) & 0x1;
        GST_WRITE_UINT32_BE (data, event->break_duration & 0xffffffff);
        data += 4;
      }
      /* unique_program_id : 16bit */
      GST_WRITE_UINT16_BE (data, event->unique_program_id);
      data += 2;
      /* avail_num : 8bit */
      *data++ = event->avail_num;
      /* avails_expected : 8bit */
      *data++ = event->avails_expected;
    }
  }

  /* Descriptors */
  GST_WRITE_UINT16_BE (data, descriptor_length);
  data += 2;
  _packetize_descriptor_array (sit->descriptors, &data);

  /* CALCULATE AND WRITE THE CRC ! */
  GST_WRITE_UINT32_BE (data, _calc_crc32 (section->data, data - section->data));

  return TRUE;
}

/**
 * gst_mpegts_section_from_scte_sit:
 * @sit: (transfer full): a #GstMpegtsSCTESIT to create the #GstMpegtsSection from
 *
 * Ownership of @sit is taken. The data in @sit is managed by the #GstMpegtsSection
 *
 * Returns: (transfer full): the #GstMpegtsSection
 */
GstMpegtsSection *
gst_mpegts_section_from_scte_sit (GstMpegtsSCTESIT * sit, guint16 pid)
{
  GstMpegtsSection *section;

  g_return_val_if_fail (sit != NULL, NULL);

  section = _gst_mpegts_section_init (pid, GST_MTS_TABLE_ID_SCTE_SPLICE);

  section->short_section = TRUE;
  section->cached_parsed = (gpointer) sit;
  section->packetizer = _packetize_sit;
  section->destroy_parsed = (GDestroyNotify) _gst_mpegts_scte_sit_free;
  section->short_section = TRUE;

  return section;
}