summaryrefslogtreecommitdiff
path: root/gst/rtpvp8/gstrtpvp8pay.c
blob: d3795da3ba1371780403288b90a1b58de6515ac5 (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
/*
 * gst-rtp-vp8-pay.c - Source for GstRtpVP8Pay
 * Copyright (C) 2011 Sjoerd Simons <sjoerd@luon.net>
 *
 * 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; either
 * version 2.1 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
 * 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 St, Fifth Floor, Boston, MA  02110-1301  USA
 */


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

#include <gst/base/gstbitreader.h>
#include <gst/rtp/gstrtppayloads.h>
#include <gst/rtp/gstrtpbuffer.h>
#include "dboolhuff.h"
#include "gstrtpvp8pay.h"

#define FI_FRAG_UNFRAGMENTED 0x0
#define FI_FRAG_START 0x1
#define FI_FRAG_MIDDLE 0x2
#define FI_FRAG_END 0x3

GST_DEBUG_CATEGORY_STATIC (gst_rtp_vp8_pay_debug);
#define GST_CAT_DEFAULT gst_rtp_vp8_pay_debug

GST_BOILERPLATE (GstRtpVP8Pay, gst_rtp_vp8_pay, GstBaseRTPPayload,
    GST_TYPE_BASE_RTP_PAYLOAD);

static GstStaticPadTemplate gst_rtp_vp8_pay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp, "
        "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ","
        "clock-rate = (int) 90000, encoding-name = (string) \"VP8-DRAFT-0-3-2\""));

static GstStaticPadTemplate gst_rtp_vp8_pay_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-vp8"));

static void
gst_rtp_vp8_pay_init (GstRtpVP8Pay * obj, GstRtpVP8PayClass * klass)
{
}

static GstFlowReturn gst_rtp_vp8_pay_handle_buffer (GstBaseRTPPayload * payload,
    GstBuffer * buffer);
static gboolean gst_rtp_vp8_pay_set_caps (GstBaseRTPPayload * payload,
    GstCaps * caps);

static void
gst_rtp_vp8_pay_base_init (gpointer klass)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_rtp_vp8_pay_sink_template));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_rtp_vp8_pay_src_template));

  gst_element_class_set_details_simple (element_class, "RTP VP8 payloader",
      "Codec/Payloader/Network/RTP",
      "Puts VP8 video in RTP packets)", "Sjoerd Simons <sjoerd@luon.net>");
}

static void
gst_rtp_vp8_pay_class_init (GstRtpVP8PayClass * gst_rtp_vp8_pay_class)
{
  GstBaseRTPPayloadClass *pay_class =
      GST_BASE_RTP_PAYLOAD_CLASS (gst_rtp_vp8_pay_class);

  pay_class->handle_buffer = gst_rtp_vp8_pay_handle_buffer;
  pay_class->set_caps = gst_rtp_vp8_pay_set_caps;

  GST_DEBUG_CATEGORY_INIT (gst_rtp_vp8_pay_debug, "rtpvp8pay", 0,
      "VP8 Video RTP Payloader");
}

static gsize
gst_rtp_vp8_calc_payload_len (GstBaseRTPPayload * payload)
{
  return gst_rtp_buffer_calc_payload_len (GST_BASE_RTP_PAYLOAD_MTU (payload) -
      1, 0, 0);
}

/* When growing the vp8 header keep gst_rtp_vp8_calc_payload_len in sync */
static GstBuffer *
gst_rtp_vp8_create_header_buffer (gboolean start, gboolean mark, guint fi,
    GstBuffer * in)
{
  GstBuffer *out;
  guint8 *p;

  out = gst_rtp_buffer_new_allocate (1, 0, 0);
  p = gst_rtp_buffer_get_payload (out);
  /* Hardcode I = 0 and N = 0, only set FI and B */
  p[0] = (fi & 0x3) << 1 | (start ? 1 : 0);

  gst_rtp_buffer_set_marker (out, mark);

  GST_BUFFER_DURATION (out) = GST_BUFFER_DURATION (in);
  GST_BUFFER_TIMESTAMP (out) = GST_BUFFER_TIMESTAMP (in);

  return out;
}


static gboolean
gst_rtp_vp8_pay_parse_frame (GstRtpVP8Pay * self, GstBuffer * buffer)
{
  GstBitReader *reader;
  int i;
  gboolean keyframe;
  guint32 header_size;
  guint8 version;
  guint8 tmp8 = 0;
  guint8 *data;
  guint8 partitions;
  guint offset;
  BOOL_DECODER bc;

  reader = gst_bit_reader_new_from_buffer (buffer);

  if (G_UNLIKELY (GST_BUFFER_SIZE (buffer) < 3))
    goto error;

  data = GST_BUFFER_DATA (buffer);

  self->is_keyframe = keyframe = ((data[0] & 0x1) == 0);
  version = (data[0] >> 1) & 0x7;

  if (G_UNLIKELY (version > 3)) {
    GST_ERROR_OBJECT (self, "Unknown VP8 version %u", version);
    goto error;
  }

  /* keyframe, version and show_frame use 5 bits */
  header_size = data[2] << 11 | data[1] << 3 | (data[0] >> 5);

  /* Include the uncompressed data blob in the header */
  offset = keyframe ? 10 : 3;
  header_size += offset;

  if (!gst_bit_reader_skip (reader, 24))
    goto error;

  if (keyframe) {
    /* check start tag: 0x9d 0x01 0x2a */
    if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 8) || tmp8 != 0x9d)
      goto error;

    if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 8) || tmp8 != 0x01)
      goto error;

    if (!gst_bit_reader_get_bits_uint8 (reader, &tmp8, 8) || tmp8 != 0x2a)
      goto error;

    /* Skip horizontal size code (16 bits) vertical size code (16 bits) */
    if (!gst_bit_reader_skip (reader, 32))
      goto error;
  }

  offset = keyframe ? 10 : 3;
  vp8dx_start_decode (&bc, GST_BUFFER_DATA (buffer) + offset,
      GST_BUFFER_SIZE (buffer) - offset);

  if (keyframe) {
    /* color space (1 bit) and clamping type (1 bit) */
    vp8dx_decode_bool (&bc, 0x80);
    vp8dx_decode_bool (&bc, 0x80);
  }

  /* segmentation_enabled */
  if (vp8dx_decode_bool (&bc, 0x80)) {
    guint8 update_mb_segmentation_map = vp8dx_decode_bool (&bc, 0x80);
    guint8 update_segment_feature_data = vp8dx_decode_bool (&bc, 0x80);

    if (update_segment_feature_data) {
      /* skip segment feature mode */
      vp8dx_decode_bool (&bc, 0x80);

      /* quantizer update */
      for (i = 0; i < 4; i++) {
        /* skip flagged quantizer value (7 bits) and sign (1 bit) */
        if (vp8dx_decode_bool (&bc, 0x80))
          vp8_decode_value (&bc, 8);
      }

      /* loop filter update */
      for (i = 0; i < 4; i++) {
        /* skip flagged lf update value (6 bits) and sign (1 bit) */
        if (vp8dx_decode_bool (&bc, 0x80))
          vp8_decode_value (&bc, 7);
      }
    }

    if (update_mb_segmentation_map) {
      /* segment prob update */
      for (i = 0; i < 3; i++) {
        /* skip flagged segment prob */
        if (vp8dx_decode_bool (&bc, 0x80))
          vp8_decode_value (&bc, 8);
      }
    }
  }

  /* skip filter type (1 bit), loop filter level (6 bits) and
   * sharpness level (3 bits) */
  vp8_decode_value (&bc, 1);
  vp8_decode_value (&bc, 6);
  vp8_decode_value (&bc, 3);

  /* loop_filter_adj_enabled */
  if (vp8dx_decode_bool (&bc, 0x80)) {

    /* delta update */
    if (vp8dx_decode_bool (&bc, 0x80)) {

      for (i = 0; i < 8; i++) {
        /* 8 updates, 1 bit indicate whether there is one and if follow by a
         * 7 bit update */
        if (vp8dx_decode_bool (&bc, 0x80))
          vp8_decode_value (&bc, 7);
      }
    }
  }

  if (vp8dx_bool_error (&bc))
    goto error;

  tmp8 = vp8_decode_value (&bc, 2);

  partitions = 1 << tmp8;

  /* Check if things are still sensible */
  if (header_size + (partitions - 1) * 3 >= GST_BUFFER_SIZE (buffer))
    goto error;

  /* partition data is right after the frame header */
  data = GST_BUFFER_DATA (buffer) + header_size;

  /* Set up mapping, count the initial header as a partition to make other
   * sections of the code easier */
  self->n_partitions = partitions + 1;
  self->partition_offset[0] = 0;
  self->partition_size[0] = header_size + (partitions - 1) * 3;

  self->partition_offset[1] = self->partition_size[0];
  for (i = 1; i < partitions; i++) {
    guint size = (data[2] << 16 | data[1] << 8 | data[0]);

    data += 3;
    self->partition_size[i] = size;
    self->partition_offset[i + 1] = self->partition_offset[i] + size;
  }

  /* Check that our partition offsets and sizes don't go outsize the buffer
   * size. */
  if (self->partition_offset[i] >= GST_BUFFER_SIZE (buffer))
    goto error;

  self->partition_size[i] = GST_BUFFER_SIZE (buffer)
      - self->partition_offset[i];

  self->partition_offset[i + 1] = GST_BUFFER_SIZE (buffer);

  gst_bit_reader_free (reader);
  return TRUE;

error:
  GST_DEBUG ("Failed to parse frame");
  gst_bit_reader_free (reader);
  return FALSE;
}

static guint
gst_rtp_vp8_fit_partitions (GstRtpVP8Pay * self, gint first, gsize available)
{
  guint num = 0;
  int i;

  g_assert (first < self->n_partitions);

  for (i = first;
      i < self->n_partitions && self->partition_size[i] < available; i++) {
    num++;
    available -= self->partition_size[i];
  }

  return num;
}

static GstBuffer *
gst_rtp_vp8_create_sub (GstRtpVP8Pay * self,
    GstBuffer * buffer, guint current, guint num)
{
  guint offset = self->partition_offset[current];
  guint size = self->partition_offset[current + num] - offset;

  return gst_buffer_create_sub (buffer, offset, size);
}


static guint
gst_rtp_vp8_payload_next (GstRtpVP8Pay * self,
    GstBufferListIterator * it, guint first, GstBuffer * buffer)
{
  guint num;
  GstBuffer *header;
  GstBuffer *sub;
  gboolean mark;
  gsize available = gst_rtp_vp8_calc_payload_len (GST_BASE_RTP_PAYLOAD (self));

  g_assert (first < 9);

  /* How many partitions can we fit */
  num = gst_rtp_vp8_fit_partitions (self, first, available);

  if (num > 0) {
    mark = (first + num == self->n_partitions);
    /* whole set of partitions, payload them and done */
    header = gst_rtp_vp8_create_header_buffer (first == 0, mark,
        FI_FRAG_UNFRAGMENTED, buffer);
    sub = gst_rtp_vp8_create_sub (self, buffer, first, num);

    gst_buffer_list_iterator_add_group (it);
    gst_buffer_list_iterator_add (it, header);
    gst_buffer_list_iterator_add (it, sub);
  } else {
    /* Fragmented packets */
    guint offset = self->partition_offset[first];
    guint left = self->partition_size[first];
    gboolean start = (first == 0);

    header = gst_rtp_vp8_create_header_buffer (start, FALSE,
        FI_FRAG_START, buffer);
    sub = gst_buffer_create_sub (buffer, offset, available);
    offset += available;

    gst_buffer_list_iterator_add_group (it);
    gst_buffer_list_iterator_add (it, header);
    gst_buffer_list_iterator_add (it, sub);

    left -= available;

    for (; left > available; left -= available) {
      header = gst_rtp_vp8_create_header_buffer (start, FALSE,
          FI_FRAG_MIDDLE, buffer);

      sub = gst_buffer_create_sub (buffer, offset, available);
      offset += available;

      gst_buffer_list_iterator_add_group (it);
      gst_buffer_list_iterator_add (it, header);
      gst_buffer_list_iterator_add (it, sub);
    }

    mark = (first + 1 == self->n_partitions);

    header = gst_rtp_vp8_create_header_buffer (start, mark,
        FI_FRAG_END, buffer);
    sub = gst_buffer_create_sub (buffer, offset, left);

    gst_buffer_list_iterator_add_group (it);
    gst_buffer_list_iterator_add (it, header);
    gst_buffer_list_iterator_add (it, sub);

    return 1;
  }

  return num;
}


static GstFlowReturn
gst_rtp_vp8_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
{
  GstRtpVP8Pay *self = GST_RTP_VP8_PAY (payload);
  GstFlowReturn ret;
  GstBufferList *list;
  GstBufferListIterator *it;
  guint current;

  if (G_UNLIKELY (!gst_rtp_vp8_pay_parse_frame (self, buffer))) {
    /* FIXME throw flow error */
    g_message ("Failed to parse frame");
    return GST_FLOW_ERROR;
  }

  list = gst_buffer_list_new ();
  it = gst_buffer_list_iterate (list);

  for (current = 0; current < self->n_partitions;) {
    guint n;

    n = gst_rtp_vp8_payload_next (self, it, current, buffer);
    current += n;
  }

  ret = gst_basertppayload_push_list (payload, list);
  gst_buffer_list_iterator_free (it);

  return ret;
}

static gboolean
gst_rtp_vp8_pay_set_caps (GstBaseRTPPayload * payload, GstCaps * caps)
{
  gst_basertppayload_set_options (payload, "video", TRUE,
      "VP8-DRAFT-0-3-2", 90000);
  return gst_basertppayload_set_outcaps (payload, NULL);
}

gboolean
gst_rtp_vp8_pay_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "rtpvp8pay",
      GST_RANK_MARGINAL, GST_TYPE_RTP_VP8_PAY);
}