~ubuntu-branches/ubuntu/vivid/gstreamer-vaapi/vivid

« back to all changes in this revision

Viewing changes to ext/codecparsers/gst-libs/gst/codecparsers/gsth264parser.c

  • Committer: Package Import Robot
  • Author(s): Vincent Cheng
  • Date: 2014-08-06 23:56:00 UTC
  • mfrom: (0.1.4 sid) (1.1.3)
  • Revision ID: package-import@ubuntu.com-20140806235600-fg1kcmiu67k315q5
Tags: 0.5.9-2
* Remove spurious build-deps: libva-drm1, libavcodec-dev. (Closes: #757283)
* Drop Build-Depends-Indep and build docs unconditionally on all archs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
84
84
#  include "config.h"
85
85
#endif
86
86
 
 
87
#include "nalutils.h"
87
88
#include "gsth264parser.h"
88
89
 
89
90
#include <gst/base/gstbytereader.h>
170
171
  {2, 1}
171
172
};
172
173
 
173
 
/* Compute Ceil(Log2(v)) */
174
 
/* Derived from branchless code for integer log2(v) from:
175
 
   <http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog> */
176
 
static guint
177
 
ceil_log2 (guint32 v)
178
 
{
179
 
  guint r, shift;
180
 
 
181
 
  v--;
182
 
  r = (v > 0xFFFF) << 4;
183
 
  v >>= r;
184
 
  shift = (v > 0xFF) << 3;
185
 
  v >>= shift;
186
 
  r |= shift;
187
 
  shift = (v > 0xF) << 2;
188
 
  v >>= shift;
189
 
  r |= shift;
190
 
  shift = (v > 0x3) << 1;
191
 
  v >>= shift;
192
 
  r |= shift;
193
 
  r |= (v >> 1);
194
 
  return r + 1;
195
 
}
196
 
 
197
 
/****** Nal parser ******/
198
 
 
199
 
typedef struct
200
 
{
201
 
  const guint8 *data;
202
 
  guint size;
203
 
 
204
 
  guint n_epb;                  /* Number of emulation prevention bytes */
205
 
  guint byte;                   /* Byte position */
206
 
  guint bits_in_cache;          /* bitpos in the cache of next bit */
207
 
  guint8 first_byte;
208
 
  guint64 cache;                /* cached bytes */
209
 
} NalReader;
210
 
 
211
 
static void
212
 
nal_reader_init (NalReader * nr, const guint8 * data, guint size)
213
 
{
214
 
  nr->data = data;
215
 
  nr->size = size;
216
 
  nr->n_epb = 0;
217
 
 
218
 
  nr->byte = 0;
219
 
  nr->bits_in_cache = 0;
220
 
  /* fill with something other than 0 to detect emulation prevention bytes */
221
 
  nr->first_byte = 0xff;
222
 
  nr->cache = 0xff;
223
 
}
224
 
 
225
 
static inline gboolean
226
 
nal_reader_read (NalReader * nr, guint nbits)
227
 
{
228
 
  if (G_UNLIKELY (nr->byte * 8 + (nbits - nr->bits_in_cache) > nr->size * 8)) {
229
 
    GST_DEBUG ("Can not read %u bits, bits in cache %u, Byte * 8 %u, size in "
230
 
        "bits %u", nbits, nr->bits_in_cache, nr->byte * 8, nr->size * 8);
231
 
    return FALSE;
232
 
  }
233
 
 
234
 
  while (nr->bits_in_cache < nbits) {
235
 
    guint8 byte;
236
 
    gboolean check_three_byte;
237
 
 
238
 
    check_three_byte = TRUE;
239
 
  next_byte:
240
 
    if (G_UNLIKELY (nr->byte >= nr->size))
241
 
      return FALSE;
242
 
 
243
 
    byte = nr->data[nr->byte++];
244
 
 
245
 
    /* check if the byte is a emulation_prevention_three_byte */
246
 
    if (check_three_byte && byte == 0x03 && nr->first_byte == 0x00 &&
247
 
        ((nr->cache & 0xff) == 0)) {
248
 
      /* next byte goes unconditionally to the cache, even if it's 0x03 */
249
 
      check_three_byte = FALSE;
250
 
      nr->n_epb++;
251
 
      goto next_byte;
252
 
    }
253
 
    nr->cache = (nr->cache << 8) | nr->first_byte;
254
 
    nr->first_byte = byte;
255
 
    nr->bits_in_cache += 8;
256
 
  }
257
 
 
258
 
  return TRUE;
259
 
}
260
 
 
261
 
static inline gboolean
262
 
nal_reader_skip (NalReader * nr, guint nbits)
263
 
{
264
 
  if (G_UNLIKELY (!nal_reader_read (nr, nbits)))
265
 
    return FALSE;
266
 
 
267
 
  nr->bits_in_cache -= nbits;
268
 
 
269
 
  return TRUE;
270
 
}
271
 
 
272
 
static inline gboolean
273
 
nal_reader_skip_to_byte (NalReader * nr)
274
 
{
275
 
  if (nr->bits_in_cache == 0) {
276
 
    if (G_LIKELY ((nr->size - nr->byte) > 0))
277
 
      nr->byte++;
278
 
    else
279
 
      return FALSE;
280
 
  }
281
 
 
282
 
  nr->bits_in_cache = 0;
283
 
 
284
 
  return TRUE;
285
 
}
286
 
 
287
 
static inline guint
288
 
nal_reader_get_pos (const NalReader * nr)
289
 
{
290
 
  return nr->byte * 8 - nr->bits_in_cache;
291
 
}
292
 
 
293
 
static inline guint
294
 
nal_reader_get_remaining (const NalReader * nr)
295
 
{
296
 
  return (nr->size - nr->byte) * 8 + nr->bits_in_cache;
297
 
}
298
 
 
299
 
static inline guint
300
 
nal_reader_get_epb_count (const NalReader * nr)
301
 
{
302
 
  return nr->n_epb;
303
 
}
304
 
 
305
 
#define GST_NAL_READER_READ_BITS(bits) \
306
 
static gboolean \
307
 
nal_reader_get_bits_uint##bits (NalReader *nr, guint##bits *val, guint nbits) \
308
 
{ \
309
 
  guint shift; \
310
 
  \
311
 
  if (!nal_reader_read (nr, nbits)) \
312
 
    return FALSE; \
313
 
  \
314
 
  /* bring the required bits down and truncate */ \
315
 
  shift = nr->bits_in_cache - nbits; \
316
 
  *val = nr->first_byte >> shift; \
317
 
  \
318
 
  *val |= nr->cache << (8 - shift); \
319
 
  /* mask out required bits */ \
320
 
  if (nbits < bits) \
321
 
    *val &= ((guint##bits)1 << nbits) - 1; \
322
 
  \
323
 
  nr->bits_in_cache = shift; \
324
 
  \
325
 
  return TRUE; \
326
 
} \
327
 
 
328
 
GST_NAL_READER_READ_BITS (8);
329
 
GST_NAL_READER_READ_BITS (16);
330
 
GST_NAL_READER_READ_BITS (32);
331
 
 
332
 
static gboolean
333
 
nal_reader_get_ue (NalReader * nr, guint32 * val)
334
 
{
335
 
  guint i = 0;
336
 
  guint8 bit;
337
 
  guint32 value;
338
 
 
339
 
  if (G_UNLIKELY (!nal_reader_get_bits_uint8 (nr, &bit, 1))) {
340
 
 
341
 
    return FALSE;
342
 
  }
343
 
 
344
 
  while (bit == 0) {
345
 
    i++;
346
 
    if G_UNLIKELY
347
 
      ((!nal_reader_get_bits_uint8 (nr, &bit, 1)))
348
 
          return FALSE;
349
 
  }
350
 
 
351
 
  if (G_UNLIKELY (i > 32))
352
 
    return FALSE;
353
 
 
354
 
  if (G_UNLIKELY (!nal_reader_get_bits_uint32 (nr, &value, i)))
355
 
    return FALSE;
356
 
 
357
 
  *val = (1 << i) - 1 + value;
358
 
 
359
 
  return TRUE;
360
 
}
361
 
 
362
 
static inline gboolean
363
 
nal_reader_get_se (NalReader * nr, gint32 * val)
364
 
{
365
 
  guint32 value;
366
 
 
367
 
  if (G_UNLIKELY (!nal_reader_get_ue (nr, &value)))
368
 
    return FALSE;
369
 
 
370
 
  if (value % 2)
371
 
    *val = (value / 2) + 1;
372
 
  else
373
 
    *val = -(value / 2);
374
 
 
375
 
  return TRUE;
376
 
}
377
 
 
378
 
#define CHECK_ALLOWED(val, min, max) { \
379
 
  if (val < min || val > max) { \
380
 
    GST_WARNING ("value not in allowed range. value: %d, range %d-%d", \
381
 
                     val, min, max); \
382
 
    goto error; \
383
 
  } \
384
 
}
385
 
 
386
 
#define READ_UINT8(nr, val, nbits) { \
387
 
  if (!nal_reader_get_bits_uint8 (nr, &val, nbits)) { \
388
 
    GST_WARNING ("failed to read uint8, nbits: %d", nbits); \
389
 
    goto error; \
390
 
  } \
391
 
}
392
 
 
393
 
#define READ_UINT16(nr, val, nbits) { \
394
 
  if (!nal_reader_get_bits_uint16 (nr, &val, nbits)) { \
395
 
  GST_WARNING ("failed to read uint16, nbits: %d", nbits); \
396
 
    goto error; \
397
 
  } \
398
 
}
399
 
 
400
 
#define READ_UINT32(nr, val, nbits) { \
401
 
  if (!nal_reader_get_bits_uint32 (nr, &val, nbits)) { \
402
 
  GST_WARNING ("failed to read uint32, nbits: %d", nbits); \
403
 
    goto error; \
404
 
  } \
405
 
}
406
 
 
407
 
#define READ_UINT64(nr, val, nbits) { \
408
 
  if (!nal_reader_get_bits_uint64 (nr, &val, nbits)) { \
409
 
    GST_WARNING ("failed to read uint32, nbits: %d", nbits); \
410
 
    goto error; \
411
 
  } \
412
 
}
413
 
 
414
 
#define READ_UE(nr, val) { \
415
 
  if (!nal_reader_get_ue (nr, &val)) { \
416
 
    GST_WARNING ("failed to read UE"); \
417
 
    goto error; \
418
 
  } \
419
 
}
420
 
 
421
 
#define READ_UE_ALLOWED(nr, val, min, max) { \
422
 
  guint32 tmp; \
423
 
  READ_UE (nr, tmp); \
424
 
  CHECK_ALLOWED (tmp, min, max); \
425
 
  val = tmp; \
426
 
}
427
 
 
428
 
#define READ_SE(nr, val) { \
429
 
  if (!nal_reader_get_se (nr, &val)) { \
430
 
    GST_WARNING ("failed to read SE"); \
431
 
    goto error; \
432
 
  } \
433
 
}
434
 
 
435
 
#define READ_SE_ALLOWED(nr, val, min, max) { \
436
 
  gint32 tmp; \
437
 
  READ_SE (nr, tmp); \
438
 
  CHECK_ALLOWED (tmp, min, max); \
439
 
  val = tmp; \
440
 
}
441
 
 
442
 
/***********  end of nal parser ***************/
443
 
 
444
174
/*****  Utils ****/
445
175
#define EXTENDED_SAR 255
446
176
 
470
200
  return NULL;
471
201
}
472
202
 
473
 
static inline void
474
 
set_nalu_datas (GstH264NalUnit * nalu)
 
203
static gboolean
 
204
gst_h264_parse_nalu_header (GstH264NalUnit * nalu)
475
205
{
476
206
  guint8 *data = nalu->data + nalu->offset;
 
207
  guint8 svc_extension_flag;
 
208
  GstBitReader br;
 
209
 
 
210
  if (nalu->size < 1)
 
211
    return FALSE;
477
212
 
478
213
  nalu->type = (data[0] & 0x1f);
479
214
  nalu->ref_idc = (data[0] & 0x60) >> 5;
480
215
  nalu->idr_pic_flag = (nalu->type == 5 ? 1 : 0);
 
216
  nalu->header_bytes = 1;
 
217
 
 
218
  nalu->extension_type = GST_H264_NAL_EXTENSION_NONE;
 
219
  switch (nalu->type) {
 
220
    case GST_H264_NAL_PREFIX_UNIT:
 
221
    case GST_H264_NAL_SLICE_EXT:
 
222
      if (nalu->size < 4)
 
223
        return FALSE;
 
224
      gst_bit_reader_init (&br, nalu->data + nalu->offset + nalu->header_bytes,
 
225
          nalu->size - nalu->header_bytes);
 
226
 
 
227
      svc_extension_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
 
228
      if (svc_extension_flag) { /* SVC */
 
229
 
 
230
        nalu->extension_type = GST_H264_NAL_EXTENSION_SVC;
 
231
 
 
232
      } else {                  /* MVC */
 
233
        GstH264NalUnitExtensionMVC *const mvc = &nalu->extension.mvc;
 
234
 
 
235
        nalu->extension_type = GST_H264_NAL_EXTENSION_MVC;
 
236
        mvc->non_idr_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
 
237
        mvc->priority_id = gst_bit_reader_get_bits_uint8_unchecked (&br, 6);
 
238
        mvc->view_id = gst_bit_reader_get_bits_uint16_unchecked (&br, 10);
 
239
        mvc->temporal_id = gst_bit_reader_get_bits_uint8_unchecked (&br, 3);
 
240
        mvc->anchor_pic_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
 
241
        mvc->inter_view_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
 
242
 
 
243
        /* Update IdrPicFlag (H.7.4.1.1) */
 
244
        nalu->idr_pic_flag = !mvc->non_idr_flag;
 
245
      }
 
246
      nalu->header_bytes += 3;
 
247
      break;
 
248
  }
481
249
 
482
250
  GST_DEBUG ("Nal type %u, ref_idc %u", nalu->type, nalu->ref_idc);
483
 
}
484
 
 
485
 
static inline gint
486
 
scan_for_start_codes (const guint8 * data, guint size)
487
 
{
488
 
  GstByteReader br;
489
 
  gst_byte_reader_init (&br, data, size);
490
 
 
491
 
  /* NALU not empty, so we can at least expect 1 (even 2) bytes following sc */
492
 
  return gst_byte_reader_masked_scan_uint32 (&br, 0xffffff00, 0x00000100,
493
 
      0, size);
494
 
}
495
 
 
 
251
  return TRUE;
 
252
}
 
253
 
 
254
/* Copy MVC-specific data for subset SPS header */
496
255
static gboolean
497
 
gst_h264_parser_more_data (NalReader * nr)
 
256
gst_h264_sps_mvc_copy (GstH264SPS * dst_sps, const GstH264SPS * src_sps)
498
257
{
499
 
  NalReader nr_tmp;
500
 
  guint remaining, nbits;
501
 
  guint8 rbsp_stop_one_bit, zero_bits;
502
 
 
503
 
  remaining = nal_reader_get_remaining (nr);
504
 
  if (remaining == 0)
505
 
    return FALSE;
506
 
 
507
 
  nr_tmp = *nr;
508
 
  nr = &nr_tmp;
509
 
 
510
 
  if (!nal_reader_get_bits_uint8 (nr, &rbsp_stop_one_bit, 1))
511
 
    return FALSE;
512
 
  if (!rbsp_stop_one_bit)
513
 
    return TRUE;
514
 
 
515
 
  nbits = --remaining % 8;
516
 
  while (remaining > 0) {
517
 
    if (!nal_reader_get_bits_uint8 (nr, &zero_bits, nbits))
 
258
  GstH264SPSExtMVC *const dst_mvc = &dst_sps->extension.mvc;
 
259
  const GstH264SPSExtMVC *const src_mvc = &src_sps->extension.mvc;
 
260
  guint i, j, k;
 
261
 
 
262
  g_assert (dst_sps->extension_type == GST_H264_NAL_EXTENSION_MVC);
 
263
 
 
264
  dst_mvc->num_views_minus1 = src_mvc->num_views_minus1;
 
265
  dst_mvc->view = g_new0 (GstH264SPSExtMVCView, dst_mvc->num_views_minus1 + 1);
 
266
  if (!dst_mvc->view)
 
267
    return FALSE;
 
268
 
 
269
  dst_mvc->view[0].view_id = src_mvc->view[0].view_id;
 
270
 
 
271
  for (i = 1; i <= dst_mvc->num_views_minus1; i++) {
 
272
    GstH264SPSExtMVCView *const dst_view = &dst_mvc->view[i];
 
273
    const GstH264SPSExtMVCView *const src_view = &src_mvc->view[i];
 
274
 
 
275
    dst_view->view_id = src_view->view_id;
 
276
 
 
277
    dst_view->num_anchor_refs_l0 = src_view->num_anchor_refs_l0;
 
278
    for (j = 0; j < dst_view->num_anchor_refs_l0; j++)
 
279
      dst_view->anchor_ref_l0[j] = src_view->anchor_ref_l0[j];
 
280
 
 
281
    dst_view->num_anchor_refs_l1 = src_view->num_anchor_refs_l1;
 
282
    for (j = 0; j < dst_view->num_anchor_refs_l1; j++)
 
283
      dst_view->anchor_ref_l1[j] = src_view->anchor_ref_l1[j];
 
284
 
 
285
    dst_view->num_non_anchor_refs_l0 = src_view->num_non_anchor_refs_l0;
 
286
    for (j = 0; j < dst_view->num_non_anchor_refs_l0; j++)
 
287
      dst_view->non_anchor_ref_l0[j] = src_view->non_anchor_ref_l0[j];
 
288
 
 
289
    dst_view->num_non_anchor_refs_l1 = src_view->num_non_anchor_refs_l1;
 
290
    for (j = 0; j < dst_view->num_non_anchor_refs_l1; j++)
 
291
      dst_view->non_anchor_ref_l1[j] = src_view->non_anchor_ref_l1[j];
 
292
  }
 
293
 
 
294
  dst_mvc->num_level_values_signalled_minus1 =
 
295
      src_mvc->num_level_values_signalled_minus1;
 
296
  dst_mvc->level_value = g_new0 (GstH264SPSExtMVCLevelValue,
 
297
      dst_mvc->num_level_values_signalled_minus1 + 1);
 
298
  if (!dst_mvc->level_value)
 
299
    return FALSE;
 
300
 
 
301
  for (i = 0; i <= dst_mvc->num_level_values_signalled_minus1; i++) {
 
302
    GstH264SPSExtMVCLevelValue *const dst_value = &dst_mvc->level_value[i];
 
303
    const GstH264SPSExtMVCLevelValue *const src_value =
 
304
        &src_mvc->level_value[i];
 
305
 
 
306
    dst_value->level_idc = src_value->level_idc;
 
307
 
 
308
    dst_value->num_applicable_ops_minus1 = src_value->num_applicable_ops_minus1;
 
309
    dst_value->applicable_op = g_new0 (GstH264SPSExtMVCLevelValueOp,
 
310
        dst_value->num_applicable_ops_minus1 + 1);
 
311
    if (!dst_value->applicable_op)
518
312
      return FALSE;
519
 
    if (zero_bits != 0)
520
 
      return TRUE;
521
 
    remaining -= nbits;
522
 
    nbits = 8;
523
 
  }
524
 
  return FALSE;
 
313
 
 
314
    for (j = 0; j <= dst_value->num_applicable_ops_minus1; j++) {
 
315
      GstH264SPSExtMVCLevelValueOp *const dst_op = &dst_value->applicable_op[j];
 
316
      const GstH264SPSExtMVCLevelValueOp *const src_op =
 
317
          &src_value->applicable_op[j];
 
318
 
 
319
      dst_op->temporal_id = src_op->temporal_id;
 
320
      dst_op->num_target_views_minus1 = src_op->num_target_views_minus1;
 
321
      dst_op->target_view_id =
 
322
          g_new (guint16, dst_op->num_target_views_minus1 + 1);
 
323
      if (!dst_op->target_view_id)
 
324
        return FALSE;
 
325
 
 
326
      for (k = 0; k <= dst_op->num_target_views_minus1; k++)
 
327
        dst_op->target_view_id[k] = src_op->target_view_id[k];
 
328
      dst_op->num_views_minus1 = src_op->num_views_minus1;
 
329
    }
 
330
  }
 
331
  return TRUE;
 
332
}
 
333
 
 
334
/*
 
335
 * gst_h264_sps_copy:
 
336
 * @dst_sps: The destination #GstH264SPS to copy into
 
337
 * @src_sps: The source #GstH264SPS to copy from
 
338
 *
 
339
 * Copies @src_sps into @dst_sps.
 
340
 *
 
341
 * Returns: %TRUE if everything went fine, %FALSE otherwise
 
342
 */
 
343
static gboolean
 
344
gst_h264_sps_copy (GstH264SPS * dst_sps, const GstH264SPS * src_sps)
 
345
{
 
346
  g_return_val_if_fail (dst_sps != NULL, FALSE);
 
347
  g_return_val_if_fail (src_sps != NULL, FALSE);
 
348
 
 
349
  gst_h264_sps_clear (dst_sps);
 
350
 
 
351
  *dst_sps = *src_sps;
 
352
 
 
353
  switch (dst_sps->extension_type) {
 
354
    case GST_H264_NAL_EXTENSION_MVC:
 
355
      if (!gst_h264_sps_mvc_copy (dst_sps, src_sps))
 
356
        return FALSE;
 
357
      break;
 
358
  }
 
359
  return TRUE;
 
360
}
 
361
 
 
362
/*
 
363
 * gst_h264_pps_copy:
 
364
 * @dst_pps: The destination #GstH264PPS to copy into
 
365
 * @src_pps: The source #GstH264PPS to copy from
 
366
 *
 
367
 * Copies @src_pps into @dst_pps.
 
368
 *
 
369
 * Returns: %TRUE if everything went fine, %FALSE otherwise
 
370
 */
 
371
static gboolean
 
372
gst_h264_pps_copy (GstH264PPS * dst_pps, const GstH264PPS * src_pps)
 
373
{
 
374
  g_return_val_if_fail (dst_pps != NULL, FALSE);
 
375
  g_return_val_if_fail (src_pps != NULL, FALSE);
 
376
 
 
377
  gst_h264_pps_clear (dst_pps);
 
378
 
 
379
  *dst_pps = *src_pps;
 
380
 
 
381
  if (src_pps->slice_group_id)
 
382
    dst_pps->slice_group_id = g_memdup (src_pps->slice_group_id,
 
383
        src_pps->pic_size_in_map_units_minus1 + 1);
 
384
 
 
385
  return TRUE;
525
386
}
526
387
 
527
388
/****** Parsing functions *****/
672
533
{
673
534
  guint i;
674
535
 
 
536
  static const guint8 *default_lists[12] = {
 
537
    default_4x4_intra, default_4x4_intra, default_4x4_intra,
 
538
    default_4x4_inter, default_4x4_inter, default_4x4_inter,
 
539
    default_8x8_intra, default_8x8_inter,
 
540
    default_8x8_intra, default_8x8_inter,
 
541
    default_8x8_intra, default_8x8_inter
 
542
  };
 
543
 
675
544
  GST_DEBUG ("parsing scaling lists");
676
545
 
677
546
  for (i = 0; i < 12; i++) {
683
552
      READ_UINT8 (nr, scaling_list_present_flag, 1);
684
553
      if (scaling_list_present_flag) {
685
554
        guint8 *scaling_list;
686
 
        const guint8 *scan;
687
555
        guint size;
688
556
        guint j;
689
557
        guint8 last_scale, next_scale;
690
558
 
691
559
        if (i < 6) {
692
560
          scaling_list = scaling_lists_4x4[i];
693
 
          scan = zigzag_4x4;
694
561
          size = 16;
695
562
        } else {
696
563
          scaling_list = scaling_lists_8x8[i - 6];
697
 
          scan = zigzag_8x8;
698
564
          size = 64;
699
565
        }
700
566
 
708
574
            next_scale = (last_scale + delta_scale) & 0xff;
709
575
          }
710
576
          if (j == 0 && next_scale == 0) {
711
 
            use_default = TRUE;
 
577
            /* Use default scaling lists (7.4.2.1.1.1) */
 
578
            memcpy (scaling_list, default_lists[i], size);
712
579
            break;
713
580
          }
714
 
          last_scale = scaling_list[scan[j]] =
 
581
          last_scale = scaling_list[j] =
715
582
              (next_scale == 0) ? last_scale : next_scale;
716
583
        }
717
584
      } else
773
640
 
774
641
static gboolean
775
642
slice_parse_ref_pic_list_modification_1 (GstH264SliceHdr * slice,
776
 
    NalReader * nr, guint list)
 
643
    NalReader * nr, guint list, gboolean is_mvc)
777
644
{
778
645
  GstH264RefPicListModification *entries;
779
646
  guint8 *ref_pic_list_modification_flag, *n_ref_pic_list_modification;
802
669
            slice->max_pic_num - 1);
803
670
      } else if (modification_of_pic_nums_idc == 2) {
804
671
        READ_UE (nr, entries[i].value.long_term_pic_num);
805
 
      }
 
672
      } else if (is_mvc && (modification_of_pic_nums_idc == 4 ||
 
673
              modification_of_pic_nums_idc == 5)) {
 
674
        READ_UE (nr, entries[i].value.abs_diff_view_idx_minus1);
 
675
      } else
 
676
        continue;
806
677
      entries[i++].modification_of_pic_nums_idc = modification_of_pic_nums_idc;
807
678
    }
808
679
  }
816
687
}
817
688
 
818
689
static gboolean
819
 
slice_parse_ref_pic_list_modification (GstH264SliceHdr * slice, NalReader * nr)
 
690
slice_parse_ref_pic_list_modification (GstH264SliceHdr * slice, NalReader * nr,
 
691
    gboolean is_mvc)
820
692
{
821
693
  if (!GST_H264_IS_I_SLICE (slice) && !GST_H264_IS_SI_SLICE (slice)) {
822
 
    if (!slice_parse_ref_pic_list_modification_1 (slice, nr, 0))
 
694
    if (!slice_parse_ref_pic_list_modification_1 (slice, nr, 0, is_mvc))
823
695
      return FALSE;
824
696
  }
825
697
 
826
698
  if (GST_H264_IS_B_SLICE (slice)) {
827
 
    if (!slice_parse_ref_pic_list_modification_1 (slice, nr, 1))
 
699
    if (!slice_parse_ref_pic_list_modification_1 (slice, nr, 1, is_mvc))
828
700
      return FALSE;
829
701
  }
830
702
  return TRUE;
1002
874
 
1003
875
    if (vui->nal_hrd_parameters_present_flag) {
1004
876
      GstH264HRDParams *hrd = &vui->nal_hrd_parameters;
 
877
      const guint8 nbits = hrd->initial_cpb_removal_delay_length_minus1 + 1;
1005
878
      guint8 sched_sel_idx;
1006
879
 
1007
880
      for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1;
1008
881
          sched_sel_idx++) {
1009
 
        READ_UINT8 (nr, per->nal_initial_cpb_removal_delay[sched_sel_idx], 5);
 
882
        READ_UINT8 (nr, per->nal_initial_cpb_removal_delay[sched_sel_idx],
 
883
            nbits);
1010
884
        READ_UINT8 (nr,
1011
 
            per->nal_initial_cpb_removal_delay_offset[sched_sel_idx], 5);
 
885
            per->nal_initial_cpb_removal_delay_offset[sched_sel_idx], nbits);
1012
886
      }
1013
887
    }
1014
888
 
1015
889
    if (vui->vcl_hrd_parameters_present_flag) {
1016
890
      GstH264HRDParams *hrd = &vui->vcl_hrd_parameters;
 
891
      const guint8 nbits = hrd->initial_cpb_removal_delay_length_minus1 + 1;
1017
892
      guint8 sched_sel_idx;
1018
893
 
1019
894
      for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1;
1020
895
          sched_sel_idx++) {
1021
 
        READ_UINT8 (nr, per->vcl_initial_cpb_removal_delay[sched_sel_idx], 5);
 
896
        READ_UINT8 (nr, per->vcl_initial_cpb_removal_delay[sched_sel_idx],
 
897
            nbits);
1022
898
        READ_UINT8 (nr,
1023
 
            per->vcl_initial_cpb_removal_delay_offset[sched_sel_idx], 5);
 
899
            per->vcl_initial_cpb_removal_delay_offset[sched_sel_idx], nbits);
1024
900
      }
1025
901
    }
1026
902
  }
1113
989
          vui->nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
1114
990
      READ_UINT32 (nr, tim->dpb_output_delay,
1115
991
          vui->nal_hrd_parameters.dpb_output_delay_length_minus1 + 1);
1116
 
    } else if (vui->nal_hrd_parameters_present_flag) {
 
992
    } else if (vui->vcl_hrd_parameters_present_flag) {
1117
993
      READ_UINT32 (nr, tim->cpb_removal_delay,
1118
994
          vui->vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
1119
995
      READ_UINT32 (nr, tim->dpb_output_delay,
1150
1026
  return GST_H264_PARSER_ERROR;
1151
1027
}
1152
1028
 
 
1029
/* Parse SEI stereo_video_info() message */
 
1030
static GstH264ParserResult
 
1031
gst_h264_parser_parse_stereo_video_info (GstH264NalParser * nalparser,
 
1032
    GstH264StereoVideoInfo * info, NalReader * nr)
 
1033
{
 
1034
  GST_DEBUG ("parsing \"Stereo Video info\"");
 
1035
 
 
1036
  READ_UINT8 (nr, info->field_views_flag, 1);
 
1037
  if (info->field_views_flag) {
 
1038
    READ_UINT8 (nr, info->top_field_is_left_view_flag, 1);
 
1039
  } else {
 
1040
    READ_UINT8 (nr, info->current_frame_is_left_view_flag, 1);
 
1041
    READ_UINT8 (nr, info->next_frame_is_second_view_flag, 1);
 
1042
  }
 
1043
  READ_UINT8 (nr, info->left_view_self_contained_flag, 1);
 
1044
  READ_UINT8 (nr, info->right_view_self_contained_flag, 1);
 
1045
 
 
1046
  return GST_H264_PARSER_OK;
 
1047
 
 
1048
error:
 
1049
  GST_WARNING ("error parsing \"Stereo Video info\"");
 
1050
  return GST_H264_PARSER_ERROR;
 
1051
}
 
1052
 
 
1053
/* Parse SEI frame_packing_arrangement() message */
 
1054
static GstH264ParserResult
 
1055
gst_h264_parser_parse_frame_packing (GstH264NalParser * nalparser,
 
1056
    GstH264FramePacking * frame_packing, NalReader * nr, guint payload_size)
 
1057
{
 
1058
  guint8 frame_packing_extension_flag;
 
1059
  guint start_pos;
 
1060
 
 
1061
  GST_DEBUG ("parsing \"Frame Packing Arrangement\"");
 
1062
 
 
1063
  start_pos = nal_reader_get_pos (nr);
 
1064
  READ_UE (nr, frame_packing->frame_packing_id);
 
1065
  READ_UINT8 (nr, frame_packing->frame_packing_cancel_flag, 1);
 
1066
 
 
1067
  if (!frame_packing->frame_packing_cancel_flag) {
 
1068
    READ_UINT8 (nr, frame_packing->frame_packing_type, 7);
 
1069
    READ_UINT8 (nr, frame_packing->quincunx_sampling_flag, 1);
 
1070
    READ_UINT8 (nr, frame_packing->content_interpretation_type, 6);
 
1071
    READ_UINT8 (nr, frame_packing->spatial_flipping_flag, 1);
 
1072
    READ_UINT8 (nr, frame_packing->frame0_flipped_flag, 1);
 
1073
    READ_UINT8 (nr, frame_packing->field_views_flag, 1);
 
1074
    READ_UINT8 (nr, frame_packing->current_frame_is_frame0_flag, 1);
 
1075
    READ_UINT8 (nr, frame_packing->frame0_self_contained_flag, 1);
 
1076
    READ_UINT8 (nr, frame_packing->frame1_self_contained_flag, 1);
 
1077
 
 
1078
    if (!frame_packing->quincunx_sampling_flag &&
 
1079
        frame_packing->frame_packing_type !=
 
1080
        GST_H264_FRAME_PACKING_TEMPORAL_INTERLEAVING) {
 
1081
      READ_UINT8 (nr, frame_packing->frame0_grid_position_x, 4);
 
1082
      READ_UINT8 (nr, frame_packing->frame0_grid_position_y, 4);
 
1083
      READ_UINT8 (nr, frame_packing->frame1_grid_position_x, 4);
 
1084
      READ_UINT8 (nr, frame_packing->frame1_grid_position_y, 4);
 
1085
    }
 
1086
 
 
1087
    /* Skip frame_packing_arrangement_reserved_byte */
 
1088
    if (!nal_reader_skip (nr, 8))
 
1089
      goto error;
 
1090
 
 
1091
    READ_UE_ALLOWED (nr, frame_packing->frame_packing_repetition_period, 0,
 
1092
        16384);
 
1093
  }
 
1094
 
 
1095
  /* All data that follows within a frame packing arrangement SEI message
 
1096
     after the value 1 for frame_packing_arrangement_extension_flag shall
 
1097
     be ignored (D.2.25) */
 
1098
  READ_UINT8 (nr, frame_packing_extension_flag, 1);
 
1099
  if (!frame_packing_extension_flag)
 
1100
    goto error;
 
1101
  nal_reader_skip_long (nr,
 
1102
      payload_size - (nal_reader_get_pos (nr) - start_pos));
 
1103
 
 
1104
  return GST_H264_PARSER_OK;
 
1105
 
 
1106
error:
 
1107
  GST_WARNING ("error parsing \"Frame Packing Arrangement\"");
 
1108
  return GST_H264_PARSER_ERROR;
 
1109
}
 
1110
 
 
1111
static GstH264ParserResult
 
1112
gst_h264_parser_parse_recovery_point (GstH264NalParser * nalparser,
 
1113
    GstH264RecoveryPoint * rp, NalReader * nr)
 
1114
{
 
1115
  GstH264SPS *const sps = nalparser->last_sps;
 
1116
 
 
1117
  GST_DEBUG ("parsing \"Recovery point\"");
 
1118
  if (!sps || !sps->valid) {
 
1119
    GST_WARNING ("didn't get the associated sequence paramater set for the "
 
1120
        "current access unit");
 
1121
    goto error;
 
1122
  }
 
1123
 
 
1124
  READ_UE_ALLOWED (nr, rp->recovery_frame_cnt, 0, sps->max_frame_num - 1);
 
1125
  READ_UINT8 (nr, rp->exact_match_flag, 1);
 
1126
  READ_UINT8 (nr, rp->broken_link_flag, 1);
 
1127
  READ_UINT8 (nr, rp->changing_slice_group_idc, 2);
 
1128
 
 
1129
  return GST_H264_PARSER_OK;
 
1130
 
 
1131
error:
 
1132
  GST_WARNING ("error parsing \"Recovery point\"");
 
1133
  return GST_H264_PARSER_ERROR;
 
1134
}
 
1135
 
 
1136
static GstH264ParserResult
 
1137
gst_h264_parser_parse_sei_message (GstH264NalParser * nalparser,
 
1138
    NalReader * nr, GstH264SEIMessage * sei)
 
1139
{
 
1140
  guint32 payloadSize;
 
1141
  guint8 payload_type_byte, payload_size_byte;
 
1142
  guint remaining, payload_size;
 
1143
  GstH264ParserResult res;
 
1144
 
 
1145
  GST_DEBUG ("parsing \"Sei message\"");
 
1146
 
 
1147
  sei->payloadType = 0;
 
1148
  do {
 
1149
    READ_UINT8 (nr, payload_type_byte, 8);
 
1150
    sei->payloadType += payload_type_byte;
 
1151
  } while (payload_type_byte == 0xff);
 
1152
 
 
1153
  payloadSize = 0;
 
1154
  do {
 
1155
    READ_UINT8 (nr, payload_size_byte, 8);
 
1156
    payloadSize += payload_size_byte;
 
1157
  }
 
1158
  while (payload_size_byte == 0xff);
 
1159
 
 
1160
  remaining = nal_reader_get_remaining (nr);
 
1161
  payload_size = payloadSize * 8 < remaining ? payloadSize * 8 : remaining;
 
1162
 
 
1163
  GST_DEBUG ("SEI message received: payloadType  %u, payloadSize = %u bits",
 
1164
      sei->payloadType, payload_size);
 
1165
 
 
1166
  switch (sei->payloadType) {
 
1167
    case GST_H264_SEI_BUF_PERIOD:
 
1168
      /* size not set; might depend on emulation_prevention_three_byte */
 
1169
      res = gst_h264_parser_parse_buffering_period (nalparser,
 
1170
          &sei->payload.buffering_period, nr);
 
1171
      break;
 
1172
    case GST_H264_SEI_PIC_TIMING:
 
1173
      /* size not set; might depend on emulation_prevention_three_byte */
 
1174
      res = gst_h264_parser_parse_pic_timing (nalparser,
 
1175
          &sei->payload.pic_timing, nr);
 
1176
      break;
 
1177
    case GST_H264_SEI_RECOVERY_POINT:
 
1178
      res = gst_h264_parser_parse_recovery_point (nalparser,
 
1179
          &sei->payload.recovery_point, nr);
 
1180
      break;
 
1181
    case GST_H264_SEI_STEREO_VIDEO_INFO:
 
1182
      res = gst_h264_parser_parse_stereo_video_info (nalparser,
 
1183
          &sei->payload.stereo_video_info, nr);
 
1184
      break;
 
1185
    case GST_H264_SEI_FRAME_PACKING:
 
1186
      res = gst_h264_parser_parse_frame_packing (nalparser,
 
1187
          &sei->payload.frame_packing, nr, payload_size);
 
1188
      break;
 
1189
    default:
 
1190
      /* Just consume payloadSize bytes, which does not account for
 
1191
         emulation prevention bytes */
 
1192
      if (!nal_reader_skip_long (nr, payload_size))
 
1193
        goto error;
 
1194
      res = GST_H264_PARSER_OK;
 
1195
      break;
 
1196
  }
 
1197
 
 
1198
  /* When SEI message doesn't end at byte boundary,
 
1199
   * check remaining bits fit the specification.
 
1200
   */
 
1201
  if (!nal_reader_is_byte_aligned (nr)) {
 
1202
    guint8 bit_equal_to_one;
 
1203
    READ_UINT8 (nr, bit_equal_to_one, 1);
 
1204
    if (!bit_equal_to_one)
 
1205
      GST_WARNING ("Bit non equal to one.");
 
1206
 
 
1207
    while (!nal_reader_is_byte_aligned (nr)) {
 
1208
      guint8 bit_equal_to_zero;
 
1209
      READ_UINT8 (nr, bit_equal_to_zero, 1);
 
1210
      if (bit_equal_to_zero)
 
1211
        GST_WARNING ("Bit non equal to zero.");
 
1212
    }
 
1213
  }
 
1214
 
 
1215
  return res;
 
1216
 
 
1217
error:
 
1218
  GST_WARNING ("error parsing \"Sei message\"");
 
1219
  return GST_H264_PARSER_ERROR;
 
1220
}
 
1221
 
1153
1222
/******** API *************/
1154
1223
 
1155
1224
/**
1180
1249
void
1181
1250
gst_h264_nal_parser_free (GstH264NalParser * nalparser)
1182
1251
{
 
1252
  guint i;
 
1253
 
 
1254
  for (i = 0; i < GST_H264_MAX_PPS_COUNT; i++)
 
1255
    gst_h264_pps_clear (&nalparser->pps[i]);
 
1256
  for (i = 0; i < GST_H264_MAX_SPS_COUNT; i++)
 
1257
    gst_h264_sps_clear (&nalparser->sps[i]);
1183
1258
  g_slice_free (GstH264NalParser, nalparser);
1184
1259
 
1185
1260
  nalparser = NULL;
1228
1303
    return GST_H264_PARSER_ERROR;
1229
1304
  }
1230
1305
 
1231
 
  nalu->valid = TRUE;
1232
1306
  nalu->sc_offset = offset + off1;
1233
1307
 
 
1308
 
 
1309
  nalu->offset = offset + off1 + 3;
 
1310
  nalu->data = (guint8 *) data;
 
1311
  nalu->size = size - nalu->offset;
 
1312
 
 
1313
  if (!gst_h264_parse_nalu_header (nalu)) {
 
1314
    GST_WARNING ("error parsing \"NAL unit header\"");
 
1315
    nalu->size = 0;
 
1316
    return GST_H264_PARSER_BROKEN_DATA;
 
1317
  }
 
1318
 
 
1319
  nalu->valid = TRUE;
 
1320
 
1234
1321
  /* sc might have 2 or 3 0-bytes */
1235
 
  if (nalu->sc_offset > 0 && data[nalu->sc_offset - 1] == 00)
 
1322
  if (nalu->sc_offset > 0 && data[nalu->sc_offset - 1] == 00
 
1323
      && (nalu->type == GST_H264_NAL_SPS || nalu->type == GST_H264_NAL_PPS
 
1324
          || nalu->type == GST_H264_NAL_AU_DELIMITER))
1236
1325
    nalu->sc_offset--;
1237
1326
 
1238
 
  nalu->offset = offset + off1 + 3;
1239
 
  nalu->data = (guint8 *) data;
1240
 
 
1241
 
  set_nalu_datas (nalu);
1242
 
 
1243
1327
  if (nalu->type == GST_H264_NAL_SEQ_END ||
1244
1328
      nalu->type == GST_H264_NAL_STREAM_END) {
1245
1329
    GST_DEBUG ("end-of-seq or end-of-stream nal found");
1246
 
    nalu->size = 0;
 
1330
    nalu->size = 1;
1247
1331
    return GST_H264_PARSER_OK;
1248
1332
  }
1249
1333
 
1250
 
  nalu->size = size - nalu->offset;
1251
 
 
1252
1334
  return GST_H264_PARSER_OK;
1253
1335
}
1254
1336
 
1275
1357
      gst_h264_parser_identify_nalu_unchecked (nalparser, data, offset, size,
1276
1358
      nalu);
1277
1359
 
1278
 
  if (res != GST_H264_PARSER_OK || nalu->size == 0)
 
1360
  if (res != GST_H264_PARSER_OK || nalu->size == 1)
1279
1361
    goto beach;
1280
1362
 
1281
1363
  off2 = scan_for_start_codes (data + nalu->offset, size - nalu->offset);
1285
1367
    return GST_H264_PARSER_NO_NAL_END;
1286
1368
  }
1287
1369
 
1288
 
  if (off2 > 0 && data[nalu->offset + off2 - 1] == 00)
 
1370
  /* Mini performance improvement:
 
1371
   * We could have a way to store how many 0s were skipped to avoid
 
1372
   * parsing them again on the next NAL */
 
1373
  while (off2 > 0 && data[nalu->offset + off2 - 1] == 00)
1289
1374
    off2--;
1290
1375
 
1291
1376
  nalu->size = off2;
1341
1426
 
1342
1427
  nalu->data = (guint8 *) data;
1343
1428
 
1344
 
  set_nalu_datas (nalu);
1345
 
 
1346
 
  if (nalu->size < 2)
 
1429
  if (!gst_h264_parse_nalu_header (nalu)) {
 
1430
    GST_WARNING ("error parsing \"NAL unit header\"");
 
1431
    nalu->size = 0;
1347
1432
    return GST_H264_PARSER_BROKEN_DATA;
 
1433
  }
1348
1434
 
1349
1435
  nalu->valid = TRUE;
1350
1436
 
1399
1485
  if (res == GST_H264_PARSER_OK) {
1400
1486
    GST_DEBUG ("adding sequence parameter set with id: %d to array", sps->id);
1401
1487
 
1402
 
    nalparser->sps[sps->id] = *sps;
 
1488
    if (!gst_h264_sps_copy (&nalparser->sps[sps->id], sps))
 
1489
      return GST_H264_PARSER_ERROR;
1403
1490
    nalparser->last_sps = &nalparser->sps[sps->id];
1404
1491
  }
1405
 
 
1406
 
 
1407
 
 
1408
1492
  return res;
1409
1493
}
1410
1494
 
1411
 
/**
1412
 
 * gst_h264_parse_sps:
1413
 
 * @nalu: The #GST_H264_NAL_SPS #GstH264NalUnit to parse
1414
 
 * @sps: The #GstH264SPS to fill.
1415
 
 * @parse_vui_params: Whether to parse the vui_params or not
1416
 
 *
1417
 
 * Parses @data, and fills the @sps structure.
1418
 
 *
1419
 
 * Returns: a #GstH264ParserResult
1420
 
 */
1421
 
GstH264ParserResult
1422
 
gst_h264_parse_sps (GstH264NalUnit * nalu, GstH264SPS * sps,
 
1495
/* Parse seq_parameter_set_data() */
 
1496
static gboolean
 
1497
gst_h264_parse_sps_data (NalReader * nr, GstH264SPS * sps,
1423
1498
    gboolean parse_vui_params)
1424
1499
{
1425
 
  NalReader nr;
1426
1500
  gint width, height;
1427
1501
  guint subwc[] = { 1, 2, 2, 1 };
1428
1502
  guint subhc[] = { 1, 2, 1, 1 };
1429
1503
  GstH264VUIParams *vui = NULL;
1430
1504
 
1431
 
  INITIALIZE_DEBUG_CATEGORY;
1432
 
  GST_DEBUG ("parsing SPS");
1433
 
  nal_reader_init (&nr, nalu->data + nalu->offset + 1, nalu->size - 1);
1434
 
 
1435
1505
  /* set default values for fields that might not be present in the bitstream
1436
1506
     and have valid defaults */
 
1507
  sps->extension_type = GST_H264_NAL_EXTENSION_NONE;
1437
1508
  sps->chroma_format_idc = 1;
1438
1509
  sps->separate_colour_plane_flag = 0;
1439
1510
  sps->bit_depth_luma_minus8 = 0;
1448
1519
  sps->frame_crop_bottom_offset = 0;
1449
1520
  sps->delta_pic_order_always_zero_flag = 0;
1450
1521
 
1451
 
  READ_UINT8 (&nr, sps->profile_idc, 8);
1452
 
  READ_UINT8 (&nr, sps->constraint_set0_flag, 1);
1453
 
  READ_UINT8 (&nr, sps->constraint_set1_flag, 1);
1454
 
  READ_UINT8 (&nr, sps->constraint_set2_flag, 1);
1455
 
  READ_UINT8 (&nr, sps->constraint_set3_flag, 1);
 
1522
  READ_UINT8 (nr, sps->profile_idc, 8);
 
1523
  READ_UINT8 (nr, sps->constraint_set0_flag, 1);
 
1524
  READ_UINT8 (nr, sps->constraint_set1_flag, 1);
 
1525
  READ_UINT8 (nr, sps->constraint_set2_flag, 1);
 
1526
  READ_UINT8 (nr, sps->constraint_set3_flag, 1);
 
1527
  READ_UINT8 (nr, sps->constraint_set4_flag, 1);
 
1528
  READ_UINT8 (nr, sps->constraint_set5_flag, 1);
1456
1529
 
1457
 
  /* skip reserved_zero_4bits */
1458
 
  if (!nal_reader_skip (&nr, 4))
 
1530
  /* skip reserved_zero_2bits */
 
1531
  if (!nal_reader_skip (nr, 2))
1459
1532
    goto error;
1460
1533
 
1461
 
  READ_UINT8 (&nr, sps->level_idc, 8);
 
1534
  READ_UINT8 (nr, sps->level_idc, 8);
1462
1535
 
1463
 
  READ_UE_ALLOWED (&nr, sps->id, 0, GST_H264_MAX_SPS_COUNT - 1);
 
1536
  READ_UE_ALLOWED (nr, sps->id, 0, GST_H264_MAX_SPS_COUNT - 1);
1464
1537
 
1465
1538
  if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
1466
1539
      sps->profile_idc == 122 || sps->profile_idc == 244 ||
1467
1540
      sps->profile_idc == 44 || sps->profile_idc == 83 ||
1468
 
      sps->profile_idc == 86) {
1469
 
    READ_UE_ALLOWED (&nr, sps->chroma_format_idc, 0, 3);
 
1541
      sps->profile_idc == 86 || sps->profile_idc == 118 ||
 
1542
      sps->profile_idc == 128) {
 
1543
    READ_UE_ALLOWED (nr, sps->chroma_format_idc, 0, 3);
1470
1544
    if (sps->chroma_format_idc == 3)
1471
 
      READ_UINT8 (&nr, sps->separate_colour_plane_flag, 1);
1472
 
 
1473
 
    READ_UE_ALLOWED (&nr, sps->bit_depth_luma_minus8, 0, 6);
1474
 
    READ_UE_ALLOWED (&nr, sps->bit_depth_chroma_minus8, 0, 6);
1475
 
    READ_UINT8 (&nr, sps->qpprime_y_zero_transform_bypass_flag, 1);
1476
 
 
1477
 
    READ_UINT8 (&nr, sps->scaling_matrix_present_flag, 1);
 
1545
      READ_UINT8 (nr, sps->separate_colour_plane_flag, 1);
 
1546
 
 
1547
    READ_UE_ALLOWED (nr, sps->bit_depth_luma_minus8, 0, 6);
 
1548
    READ_UE_ALLOWED (nr, sps->bit_depth_chroma_minus8, 0, 6);
 
1549
    READ_UINT8 (nr, sps->qpprime_y_zero_transform_bypass_flag, 1);
 
1550
 
 
1551
    READ_UINT8 (nr, sps->scaling_matrix_present_flag, 1);
1478
1552
    if (sps->scaling_matrix_present_flag) {
1479
1553
      guint8 n_lists;
1480
1554
 
1481
1555
      n_lists = (sps->chroma_format_idc != 3) ? 8 : 12;
1482
 
      if (!gst_h264_parser_parse_scaling_list (&nr,
 
1556
      if (!gst_h264_parser_parse_scaling_list (nr,
1483
1557
              sps->scaling_lists_4x4, sps->scaling_lists_8x8,
1484
1558
              default_4x4_inter, default_4x4_intra,
1485
1559
              default_8x8_inter, default_8x8_intra, n_lists))
1487
1561
    }
1488
1562
  }
1489
1563
 
1490
 
  READ_UE_ALLOWED (&nr, sps->log2_max_frame_num_minus4, 0, 12);
 
1564
  READ_UE_ALLOWED (nr, sps->log2_max_frame_num_minus4, 0, 12);
1491
1565
 
1492
1566
  sps->max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4);
1493
1567
 
1494
 
  READ_UE_ALLOWED (&nr, sps->pic_order_cnt_type, 0, 2);
 
1568
  READ_UE_ALLOWED (nr, sps->pic_order_cnt_type, 0, 2);
1495
1569
  if (sps->pic_order_cnt_type == 0) {
1496
 
    READ_UE_ALLOWED (&nr, sps->log2_max_pic_order_cnt_lsb_minus4, 0, 12);
 
1570
    READ_UE_ALLOWED (nr, sps->log2_max_pic_order_cnt_lsb_minus4, 0, 12);
1497
1571
  } else if (sps->pic_order_cnt_type == 1) {
1498
1572
    guint i;
1499
1573
 
1500
 
    READ_UINT8 (&nr, sps->delta_pic_order_always_zero_flag, 1);
1501
 
    READ_SE (&nr, sps->offset_for_non_ref_pic);
1502
 
    READ_SE (&nr, sps->offset_for_top_to_bottom_field);
1503
 
    READ_UE_ALLOWED (&nr, sps->num_ref_frames_in_pic_order_cnt_cycle, 0, 255);
 
1574
    READ_UINT8 (nr, sps->delta_pic_order_always_zero_flag, 1);
 
1575
    READ_SE (nr, sps->offset_for_non_ref_pic);
 
1576
    READ_SE (nr, sps->offset_for_top_to_bottom_field);
 
1577
    READ_UE_ALLOWED (nr, sps->num_ref_frames_in_pic_order_cnt_cycle, 0, 255);
1504
1578
 
1505
1579
    for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
1506
 
      READ_SE (&nr, sps->offset_for_ref_frame[i]);
 
1580
      READ_SE (nr, sps->offset_for_ref_frame[i]);
1507
1581
  }
1508
1582
 
1509
 
  READ_UE (&nr, sps->num_ref_frames);
1510
 
  READ_UINT8 (&nr, sps->gaps_in_frame_num_value_allowed_flag, 1);
1511
 
  READ_UE (&nr, sps->pic_width_in_mbs_minus1);
1512
 
  READ_UE (&nr, sps->pic_height_in_map_units_minus1);
1513
 
  READ_UINT8 (&nr, sps->frame_mbs_only_flag, 1);
 
1583
  READ_UE (nr, sps->num_ref_frames);
 
1584
  READ_UINT8 (nr, sps->gaps_in_frame_num_value_allowed_flag, 1);
 
1585
  READ_UE (nr, sps->pic_width_in_mbs_minus1);
 
1586
  READ_UE (nr, sps->pic_height_in_map_units_minus1);
 
1587
  READ_UINT8 (nr, sps->frame_mbs_only_flag, 1);
1514
1588
 
1515
1589
  if (!sps->frame_mbs_only_flag)
1516
 
    READ_UINT8 (&nr, sps->mb_adaptive_frame_field_flag, 1);
 
1590
    READ_UINT8 (nr, sps->mb_adaptive_frame_field_flag, 1);
1517
1591
 
1518
 
  READ_UINT8 (&nr, sps->direct_8x8_inference_flag, 1);
1519
 
  READ_UINT8 (&nr, sps->frame_cropping_flag, 1);
 
1592
  READ_UINT8 (nr, sps->direct_8x8_inference_flag, 1);
 
1593
  READ_UINT8 (nr, sps->frame_cropping_flag, 1);
1520
1594
  if (sps->frame_cropping_flag) {
1521
 
    READ_UE (&nr, sps->frame_crop_left_offset);
1522
 
    READ_UE (&nr, sps->frame_crop_right_offset);
1523
 
    READ_UE (&nr, sps->frame_crop_top_offset);
1524
 
    READ_UE (&nr, sps->frame_crop_bottom_offset);
 
1595
    READ_UE (nr, sps->frame_crop_left_offset);
 
1596
    READ_UE (nr, sps->frame_crop_right_offset);
 
1597
    READ_UE (nr, sps->frame_crop_top_offset);
 
1598
    READ_UE (nr, sps->frame_crop_bottom_offset);
1525
1599
  }
1526
1600
 
1527
 
  READ_UINT8 (&nr, sps->vui_parameters_present_flag, 1);
 
1601
  READ_UINT8 (nr, sps->vui_parameters_present_flag, 1);
1528
1602
  if (sps->vui_parameters_present_flag && parse_vui_params) {
1529
 
    if (!gst_h264_parse_vui_parameters (sps, &nr))
 
1603
    if (!gst_h264_parse_vui_parameters (sps, nr))
1530
1604
      goto error;
1531
1605
    vui = &sps->vui_parameters;
1532
1606
  }
1579
1653
        vui->fixed_frame_rate_flag, sps->frame_mbs_only_flag,
1580
1654
        vui->pic_struct_present_flag);
1581
1655
 
1582
 
    if (parse_vui_params && vui->fixed_frame_rate_flag &&
1583
 
        sps->frame_mbs_only_flag && !vui->pic_struct_present_flag) {
 
1656
    if (parse_vui_params && vui->fixed_frame_rate_flag) {
1584
1657
      sps->fps_num = vui->time_scale;
1585
1658
      sps->fps_den = vui->num_units_in_tick;
1586
1659
      /* picture is a frame = 2 fields */
1590
1663
  } else {
1591
1664
    GST_LOG ("No VUI, unknown framerate");
1592
1665
  }
 
1666
  return TRUE;
 
1667
 
 
1668
error:
 
1669
  return FALSE;
 
1670
}
 
1671
 
 
1672
/* Parse subset_seq_parameter_set() data for MVC */
 
1673
static gboolean
 
1674
gst_h264_parse_sps_mvc_data (NalReader * nr, GstH264SPS * sps,
 
1675
    gboolean parse_vui_params)
 
1676
{
 
1677
  GstH264SPSExtMVC *const mvc = &sps->extension.mvc;
 
1678
  guint8 bit_equal_to_one;
 
1679
  guint i, j, k;
 
1680
 
 
1681
  READ_UINT8 (nr, bit_equal_to_one, 1);
 
1682
  if (!bit_equal_to_one)
 
1683
    return FALSE;
 
1684
 
 
1685
  sps->extension_type = GST_H264_NAL_EXTENSION_MVC;
 
1686
 
 
1687
  READ_UE_ALLOWED (nr, mvc->num_views_minus1, 0, GST_H264_MAX_VIEW_COUNT - 1);
 
1688
 
 
1689
  mvc->view = g_new0 (GstH264SPSExtMVCView, mvc->num_views_minus1 + 1);
 
1690
  if (!mvc->view)
 
1691
    goto error_allocation_failed;
 
1692
 
 
1693
  for (i = 0; i <= mvc->num_views_minus1; i++)
 
1694
    READ_UE_ALLOWED (nr, mvc->view[i].view_id, 0, GST_H264_MAX_VIEW_ID);
 
1695
 
 
1696
  for (i = 1; i <= mvc->num_views_minus1; i++) {
 
1697
    /* for RefPicList0 */
 
1698
    READ_UE_ALLOWED (nr, mvc->view[i].num_anchor_refs_l0, 0, 15);
 
1699
    for (j = 0; j < mvc->view[i].num_anchor_refs_l0; j++) {
 
1700
      READ_UE_ALLOWED (nr, mvc->view[i].anchor_ref_l0[j], 0,
 
1701
          GST_H264_MAX_VIEW_ID);
 
1702
    }
 
1703
 
 
1704
    /* for RefPicList1 */
 
1705
    READ_UE_ALLOWED (nr, mvc->view[i].num_anchor_refs_l1, 0, 15);
 
1706
    for (j = 0; j < mvc->view[i].num_anchor_refs_l1; j++) {
 
1707
      READ_UE_ALLOWED (nr, mvc->view[i].anchor_ref_l1[j], 0,
 
1708
          GST_H264_MAX_VIEW_ID);
 
1709
    }
 
1710
  }
 
1711
 
 
1712
  for (i = 1; i <= mvc->num_views_minus1; i++) {
 
1713
    /* for RefPicList0 */
 
1714
    READ_UE_ALLOWED (nr, mvc->view[i].num_non_anchor_refs_l0, 0, 15);
 
1715
    for (j = 0; j < mvc->view[i].num_non_anchor_refs_l0; j++) {
 
1716
      READ_UE_ALLOWED (nr, mvc->view[i].non_anchor_ref_l0[j], 0,
 
1717
          GST_H264_MAX_VIEW_ID);
 
1718
    }
 
1719
 
 
1720
    /* for RefPicList1 */
 
1721
    READ_UE_ALLOWED (nr, mvc->view[i].num_non_anchor_refs_l1, 0, 15);
 
1722
    for (j = 0; j < mvc->view[i].num_non_anchor_refs_l1; j++) {
 
1723
      READ_UE_ALLOWED (nr, mvc->view[i].non_anchor_ref_l1[j], 0,
 
1724
          GST_H264_MAX_VIEW_ID);
 
1725
    }
 
1726
  }
 
1727
 
 
1728
  READ_UE_ALLOWED (nr, mvc->num_level_values_signalled_minus1, 0, 63);
 
1729
 
 
1730
  mvc->level_value =
 
1731
      g_new0 (GstH264SPSExtMVCLevelValue,
 
1732
      mvc->num_level_values_signalled_minus1 + 1);
 
1733
  if (!mvc->level_value)
 
1734
    goto error_allocation_failed;
 
1735
 
 
1736
  for (i = 0; i <= mvc->num_level_values_signalled_minus1; i++) {
 
1737
    GstH264SPSExtMVCLevelValue *const level_value = &mvc->level_value[i];
 
1738
 
 
1739
    READ_UINT8 (nr, level_value->level_idc, 8);
 
1740
 
 
1741
    READ_UE_ALLOWED (nr, level_value->num_applicable_ops_minus1, 0, 1023);
 
1742
    level_value->applicable_op =
 
1743
        g_new0 (GstH264SPSExtMVCLevelValueOp,
 
1744
        level_value->num_applicable_ops_minus1 + 1);
 
1745
    if (!level_value->applicable_op)
 
1746
      goto error_allocation_failed;
 
1747
 
 
1748
    for (j = 0; j <= level_value->num_applicable_ops_minus1; j++) {
 
1749
      GstH264SPSExtMVCLevelValueOp *const op = &level_value->applicable_op[j];
 
1750
 
 
1751
      READ_UINT8 (nr, op->temporal_id, 3);
 
1752
 
 
1753
      READ_UE_ALLOWED (nr, op->num_target_views_minus1, 0, 1023);
 
1754
      op->target_view_id = g_new (guint16, op->num_target_views_minus1 + 1);
 
1755
      if (!op->target_view_id)
 
1756
        goto error_allocation_failed;
 
1757
 
 
1758
      for (k = 0; k <= op->num_target_views_minus1; k++)
 
1759
        READ_UE_ALLOWED (nr, op->target_view_id[k], 0, GST_H264_MAX_VIEW_ID);
 
1760
      READ_UE_ALLOWED (nr, op->num_views_minus1, 0, 1023);
 
1761
    }
 
1762
  }
 
1763
  return TRUE;
 
1764
 
 
1765
error_allocation_failed:
 
1766
  GST_WARNING ("failed to allocate memory");
 
1767
  gst_h264_sps_clear (sps);
 
1768
  return FALSE;
 
1769
 
 
1770
error:
 
1771
  gst_h264_sps_clear (sps);
 
1772
  return FALSE;
 
1773
}
 
1774
 
 
1775
/**
 
1776
 * gst_h264_parse_sps:
 
1777
 * @nalu: The #GST_H264_NAL_SPS #GstH264NalUnit to parse
 
1778
 * @sps: The #GstH264SPS to fill.
 
1779
 * @parse_vui_params: Whether to parse the vui_params or not
 
1780
 *
 
1781
 * Parses @data, and fills the @sps structure.
 
1782
 *
 
1783
 * Returns: a #GstH264ParserResult
 
1784
 */
 
1785
GstH264ParserResult
 
1786
gst_h264_parse_sps (GstH264NalUnit * nalu, GstH264SPS * sps,
 
1787
    gboolean parse_vui_params)
 
1788
{
 
1789
  NalReader nr;
 
1790
 
 
1791
  INITIALIZE_DEBUG_CATEGORY;
 
1792
  GST_DEBUG ("parsing SPS");
 
1793
 
 
1794
  nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
 
1795
      nalu->size - nalu->header_bytes);
 
1796
 
 
1797
  if (!gst_h264_parse_sps_data (&nr, sps, parse_vui_params))
 
1798
    goto error;
1593
1799
 
1594
1800
  sps->valid = TRUE;
1595
1801
 
1602
1808
}
1603
1809
 
1604
1810
/**
 
1811
 * gst_h264_parser_parse_subset_sps:
 
1812
 * @nalparser: a #GstH264NalParser
 
1813
 * @nalu: The #GST_H264_NAL_SUBSET_SPS #GstH264NalUnit to parse
 
1814
 * @sps: The #GstH264SPS to fill.
 
1815
 * @parse_vui_params: Whether to parse the vui_params or not
 
1816
 *
 
1817
 * Parses @data, and fills in the @sps structure.
 
1818
 *
 
1819
 * This function fully parses @data and allocates all the necessary
 
1820
 * data structures needed for MVC extensions. The resulting @sps
 
1821
 * structure shall be deallocated with gst_h264_sps_clear() when it is
 
1822
 * no longer needed.
 
1823
 *
 
1824
 * Note: if the caller doesn't need any of the MVC-specific data, then
 
1825
 * gst_h264_parser_parse_sps() is more efficient because those extra
 
1826
 * syntax elements are not parsed and no extra memory is allocated.
 
1827
 *
 
1828
 * Returns: a #GstH264ParserResult
 
1829
 */
 
1830
GstH264ParserResult
 
1831
gst_h264_parser_parse_subset_sps (GstH264NalParser * nalparser,
 
1832
    GstH264NalUnit * nalu, GstH264SPS * sps, gboolean parse_vui_params)
 
1833
{
 
1834
  GstH264ParserResult res;
 
1835
 
 
1836
  res = gst_h264_parse_subset_sps (nalu, sps, parse_vui_params);
 
1837
  if (res == GST_H264_PARSER_OK) {
 
1838
    GST_DEBUG ("adding sequence parameter set with id: %d to array", sps->id);
 
1839
 
 
1840
    if (!gst_h264_sps_copy (&nalparser->sps[sps->id], sps))
 
1841
      return GST_H264_PARSER_ERROR;
 
1842
    nalparser->last_sps = &nalparser->sps[sps->id];
 
1843
  }
 
1844
  return res;
 
1845
}
 
1846
 
 
1847
/**
 
1848
 * gst_h264_parse_subset_sps:
 
1849
 * @nalu: The #GST_H264_NAL_SUBSET_SPS #GstH264NalUnit to parse
 
1850
 * @sps: The #GstH264SPS to fill.
 
1851
 * @parse_vui_params: Whether to parse the vui_params or not
 
1852
 *
 
1853
 * Parses @data, and fills in the @sps structure.
 
1854
 *
 
1855
 * This function fully parses @data and allocates all the necessary
 
1856
 * data structures needed for MVC extensions. The resulting @sps
 
1857
 * structure shall be deallocated with gst_h264_sps_clear() when it is
 
1858
 * no longer needed.
 
1859
 *
 
1860
 * Note: if the caller doesn't need any of the MVC-specific data, then
 
1861
 * gst_h264_parser_parse_sps() is more efficient because those extra
 
1862
 * syntax elements are not parsed and no extra memory is allocated.
 
1863
 *
 
1864
 * Returns: a #GstH264ParserResult
 
1865
 */
 
1866
GstH264ParserResult
 
1867
gst_h264_parse_subset_sps (GstH264NalUnit * nalu, GstH264SPS * sps,
 
1868
    gboolean parse_vui_params)
 
1869
{
 
1870
  NalReader nr;
 
1871
 
 
1872
  INITIALIZE_DEBUG_CATEGORY;
 
1873
  GST_DEBUG ("parsing Subset SPS");
 
1874
 
 
1875
  nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
 
1876
      nalu->size - nalu->header_bytes);
 
1877
 
 
1878
  if (!gst_h264_parse_sps_data (&nr, sps, TRUE))
 
1879
    goto error;
 
1880
 
 
1881
  if (sps->profile_idc == GST_H264_PROFILE_MULTIVIEW_HIGH ||
 
1882
      sps->profile_idc == GST_H264_PROFILE_STEREO_HIGH) {
 
1883
    if (!gst_h264_parse_sps_mvc_data (&nr, sps, parse_vui_params))
 
1884
      goto error;
 
1885
  }
 
1886
 
 
1887
  sps->valid = TRUE;
 
1888
  return GST_H264_PARSER_OK;
 
1889
 
 
1890
error:
 
1891
  GST_WARNING ("error parsing \"Subset sequence parameter set\"");
 
1892
  sps->valid = FALSE;
 
1893
  return GST_H264_PARSER_ERROR;
 
1894
}
 
1895
 
 
1896
/**
1605
1897
 * gst_h264_parse_pps:
1606
1898
 * @nalparser: a #GstH264NalParser
1607
1899
 * @nalu: The #GST_H264_NAL_PPS #GstH264NalUnit to parse
1609
1901
 *
1610
1902
 * Parses @data, and fills the @pps structure.
1611
1903
 *
 
1904
 * The resulting @pps data structure shall be deallocated with the
 
1905
 * gst_h264_pps_clear() function when it is no longer needed, or prior
 
1906
 * to parsing a new PPS NAL unit.
 
1907
 *
1612
1908
 * Returns: a #GstH264ParserResult
1613
1909
 */
1614
1910
GstH264ParserResult
1624
1920
  INITIALIZE_DEBUG_CATEGORY;
1625
1921
  GST_DEBUG ("parsing PPS");
1626
1922
 
1627
 
  nal_reader_init (&nr, nalu->data + nalu->offset + 1, nalu->size - 1);
 
1923
  nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
 
1924
      nalu->size - nalu->header_bytes);
1628
1925
 
1629
1926
  READ_UE_ALLOWED (&nr, pps->id, 0, GST_H264_MAX_PPS_COUNT - 1);
1630
1927
  READ_UE_ALLOWED (&nr, sps_id, 0, GST_H264_MAX_SPS_COUNT - 1);
1660
1957
    } else if (pps->slice_group_map_type == 2) {
1661
1958
      gint i;
1662
1959
 
1663
 
      for (i = 0; i <= pps->num_slice_groups_minus1; i++) {
 
1960
      for (i = 0; i < pps->num_slice_groups_minus1; i++) {
1664
1961
        READ_UE (&nr, pps->top_left[i]);
1665
1962
        READ_UE (&nr, pps->bottom_right[i]);
1666
1963
      }
1693
1990
  READ_UINT8 (&nr, pps->constrained_intra_pred_flag, 1);
1694
1991
  READ_UINT8 (&nr, pps->redundant_pic_cnt_present_flag, 1);
1695
1992
 
1696
 
  if (!gst_h264_parser_more_data (&nr))
 
1993
  if (!nal_reader_has_more_data (&nr))
1697
1994
    goto done;
1698
1995
 
1699
1996
  READ_UINT8 (&nr, pps->transform_8x8_mode_flag, 1);
1708
2005
    if (sps->scaling_matrix_present_flag) {
1709
2006
      if (!gst_h264_parser_parse_scaling_list (&nr,
1710
2007
              pps->scaling_lists_4x4, pps->scaling_lists_8x8,
1711
 
              sps->scaling_lists_4x4[0], sps->scaling_lists_4x4[3],
1712
 
              sps->scaling_lists_8x8[0], sps->scaling_lists_8x8[3], n_lists))
 
2008
              sps->scaling_lists_4x4[3], sps->scaling_lists_4x4[0],
 
2009
              sps->scaling_lists_8x8[3], sps->scaling_lists_8x8[0], n_lists))
1713
2010
        goto error;
1714
2011
    } else {
1715
2012
      if (!gst_h264_parser_parse_scaling_list (&nr,
1729
2026
error:
1730
2027
  GST_WARNING ("error parsing \"Picture parameter set\"");
1731
2028
  pps->valid = FALSE;
 
2029
  gst_h264_pps_clear (pps);
1732
2030
  return GST_H264_PARSER_ERROR;
1733
2031
}
1734
2032
 
1740
2038
 *
1741
2039
 * Parses @data, and fills the @pps structure.
1742
2040
 *
 
2041
 * The resulting @pps data structure shall be deallocated with the
 
2042
 * gst_h264_pps_clear() function when it is no longer needed, or prior
 
2043
 * to parsing a new PPS NAL unit.
 
2044
 *
1743
2045
 * Returns: a #GstH264ParserResult
1744
2046
 */
1745
2047
GstH264ParserResult
1751
2053
  if (res == GST_H264_PARSER_OK) {
1752
2054
    GST_DEBUG ("adding picture parameter set with id: %d to array", pps->id);
1753
2055
 
1754
 
    nalparser->pps[pps->id] = *pps;
 
2056
    if (!gst_h264_pps_copy (&nalparser->pps[pps->id], pps))
 
2057
      return GST_H264_PARSER_ERROR;
1755
2058
    nalparser->last_pps = &nalparser->pps[pps->id];
1756
2059
  }
1757
2060
 
1759
2062
}
1760
2063
 
1761
2064
/**
 
2065
 * gst_h264_pps_clear:
 
2066
 * @pps: The #GstH264PPS to free
 
2067
 *
 
2068
 * Clears all @pps internal resources.
 
2069
 *
 
2070
 * Since: 1.4
 
2071
 */
 
2072
void
 
2073
gst_h264_pps_clear (GstH264PPS * pps)
 
2074
{
 
2075
  g_return_if_fail (pps != NULL);
 
2076
 
 
2077
  g_free (pps->slice_group_id);
 
2078
  pps->slice_group_id = NULL;
 
2079
}
 
2080
 
 
2081
/**
1762
2082
 * gst_h264_parser_parse_slice_hdr:
1763
2083
 * @nalparser: a #GstH264NalParser
1764
2084
 * @nalu: The #GST_H264_NAL_SLICE #GstH264NalUnit to parse
1786
2106
  }
1787
2107
 
1788
2108
 
1789
 
  nal_reader_init (&nr, nalu->data + nalu->offset + 1, nalu->size - 1);
 
2109
  nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
 
2110
      nalu->size - nalu->header_bytes);
1790
2111
 
1791
2112
  READ_UE (&nr, slice->first_mb_in_slice);
1792
2113
  READ_UE (&nr, slice->type);
1811
2132
    return GST_H264_PARSER_BROKEN_LINK;
1812
2133
  }
1813
2134
 
 
2135
  /* Check we can actually parse this slice (AVC, MVC headers only) */
 
2136
  if (sps->extension_type && sps->extension_type != GST_H264_NAL_EXTENSION_MVC) {
 
2137
    GST_WARNING ("failed to parse unsupported slice header");
 
2138
    return GST_H264_PARSER_BROKEN_DATA;
 
2139
  }
 
2140
 
1814
2141
  /* set default values for fields that might not be present in the bitstream
1815
2142
     and have valid defaults */
1816
2143
  slice->field_pic_flag = 0;
1842
2169
  else
1843
2170
    slice->max_pic_num = 2 * sps->max_frame_num;
1844
2171
 
1845
 
  if (nalu->type == 5)
 
2172
  if (nalu->idr_pic_flag)
1846
2173
    READ_UE_ALLOWED (&nr, slice->idr_pic_id, 0, G_MAXUINT16);
1847
2174
 
1848
2175
  if (sps->pic_order_cnt_type == 0) {
1878
2205
    }
1879
2206
  }
1880
2207
 
1881
 
  if (!slice_parse_ref_pic_list_modification (slice, &nr))
 
2208
  if (!slice_parse_ref_pic_list_modification (slice, &nr,
 
2209
          GST_H264_IS_MVC_NALU (nalu)))
1882
2210
    goto error;
1883
2211
 
1884
2212
  if ((pps->weighted_pred_flag && (GST_H264_IS_P_SLICE (slice)
1941
2269
 * gst_h264_parser_parse_sei:
1942
2270
 * @nalparser: a #GstH264NalParser
1943
2271
 * @nalu: The #GST_H264_NAL_SEI #GstH264NalUnit to parse
1944
 
 * @sei: The #GstH264SEIMessage to fill.
 
2272
 * @messages: The GArray of #GstH264SEIMessage to fill. The caller must free it when done.
1945
2273
 *
1946
 
 * Parses @data, and fills the @sei structures.
 
2274
 * Parses @data, create and fills the @messages array.
1947
2275
 *
1948
2276
 * Returns: a #GstH264ParserResult
1949
2277
 */
1950
2278
GstH264ParserResult
1951
2279
gst_h264_parser_parse_sei (GstH264NalParser * nalparser, GstH264NalUnit * nalu,
1952
 
    GstH264SEIMessage * sei)
 
2280
    GArray ** messages)
1953
2281
{
1954
2282
  NalReader nr;
1955
 
 
1956
 
  guint32 payloadSize;
1957
 
  guint8 payload_type_byte, payload_size_byte;
1958
 
  guint remaining, payload_size;
 
2283
  GstH264SEIMessage sei;
1959
2284
  GstH264ParserResult res;
1960
2285
 
1961
 
  GST_DEBUG ("parsing \"Sei message\"");
1962
 
 
1963
 
  nal_reader_init (&nr, nalu->data + nalu->offset + 1, nalu->size - 1);
1964
 
 
1965
 
  /* init */
1966
 
  memset (sei, 0, sizeof (*sei));
1967
 
 
1968
 
  sei->payloadType = 0;
1969
 
  do {
1970
 
    READ_UINT8 (&nr, payload_type_byte, 8);
1971
 
    sei->payloadType += payload_type_byte;
1972
 
  } while (payload_type_byte == 0xff);
1973
 
 
1974
 
  payloadSize = 0;
1975
 
  do {
1976
 
    READ_UINT8 (&nr, payload_size_byte, 8);
1977
 
    payloadSize += payload_size_byte;
1978
 
  }
1979
 
  while (payload_size_byte == 0xff);
1980
 
 
1981
 
  remaining = nal_reader_get_remaining (&nr) * 8;
1982
 
  payload_size = payloadSize < remaining ? payloadSize : remaining;
1983
 
 
1984
 
  GST_DEBUG ("SEI message received: payloadType  %u, payloadSize = %u bytes",
1985
 
      sei->payloadType, payload_size);
1986
 
 
1987
 
  if (sei->payloadType == GST_H264_SEI_BUF_PERIOD) {
1988
 
    /* size not set; might depend on emulation_prevention_three_byte */
1989
 
    res = gst_h264_parser_parse_buffering_period (nalparser,
1990
 
        &sei->payload.buffering_period, &nr);
1991
 
  } else if (sei->payloadType == GST_H264_SEI_PIC_TIMING) {
1992
 
    /* size not set; might depend on emulation_prevention_three_byte */
1993
 
    res = gst_h264_parser_parse_pic_timing (nalparser,
1994
 
        &sei->payload.pic_timing, &nr);
1995
 
  } else
1996
 
    res = GST_H264_PARSER_OK;
 
2286
  GST_DEBUG ("parsing SEI nal");
 
2287
  nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
 
2288
      nalu->size - nalu->header_bytes);
 
2289
  *messages = g_array_new (FALSE, FALSE, sizeof (GstH264SEIMessage));
 
2290
 
 
2291
  do {
 
2292
    res = gst_h264_parser_parse_sei_message (nalparser, &nr, &sei);
 
2293
    if (res == GST_H264_PARSER_OK)
 
2294
      g_array_append_val (*messages, sei);
 
2295
    else
 
2296
      break;
 
2297
  } while (nal_reader_has_more_data (&nr));
1997
2298
 
1998
2299
  return res;
1999
 
 
2000
 
error:
2001
 
  GST_WARNING ("error parsing \"Sei message\"");
2002
 
  return GST_H264_PARSER_ERROR;
 
2300
}
 
2301
 
 
2302
/* Free MVC-specific data from subset SPS header */
 
2303
static void
 
2304
gst_h264_sps_mvc_clear (GstH264SPS * sps)
 
2305
{
 
2306
  GstH264SPSExtMVC *const mvc = &sps->extension.mvc;
 
2307
  guint i, j;
 
2308
 
 
2309
  g_assert (sps->extension_type == GST_H264_NAL_EXTENSION_MVC);
 
2310
 
 
2311
  g_free (mvc->view);
 
2312
  mvc->view = NULL;
 
2313
 
 
2314
  for (i = 0; i <= mvc->num_level_values_signalled_minus1; i++) {
 
2315
    GstH264SPSExtMVCLevelValue *const level_value = &mvc->level_value[i];
 
2316
 
 
2317
    for (j = 0; j <= level_value->num_applicable_ops_minus1; j++) {
 
2318
      g_free (level_value->applicable_op[j].target_view_id);
 
2319
      level_value->applicable_op[j].target_view_id = NULL;
 
2320
    }
 
2321
    g_free (level_value->applicable_op);
 
2322
    level_value->applicable_op = NULL;
 
2323
  }
 
2324
  g_free (mvc->level_value);
 
2325
  mvc->level_value = NULL;
 
2326
 
 
2327
  /* All meaningful MVC info are now gone, just pretend to be a
 
2328
   * standard AVC struct now */
 
2329
  sps->extension_type = GST_H264_NAL_EXTENSION_NONE;
 
2330
}
 
2331
 
 
2332
/**
 
2333
 * gst_h264_sps_clear:
 
2334
 * @sps: The #GstH264SPS to free
 
2335
 *
 
2336
 * Clears all @sps internal resources.
 
2337
 */
 
2338
void
 
2339
gst_h264_sps_clear (GstH264SPS * sps)
 
2340
{
 
2341
  g_return_if_fail (sps != NULL);
 
2342
 
 
2343
  switch (sps->extension_type) {
 
2344
    case GST_H264_NAL_EXTENSION_MVC:
 
2345
      gst_h264_sps_mvc_clear (sps);
 
2346
      break;
 
2347
  }
 
2348
}
 
2349
 
 
2350
/**
 
2351
 * gst_h264_quant_matrix_8x8_get_zigzag_from_raster:
 
2352
 * @out_quant: (out): The resulting quantization matrix
 
2353
 * @quant: The source quantization matrix
 
2354
 *
 
2355
 * Converts quantization matrix @quant from raster scan order to
 
2356
 * zigzag scan order and store the resulting factors into @out_quant.
 
2357
 *
 
2358
 * Note: it is an error to pass the same table in both @quant and
 
2359
 * @out_quant arguments.
 
2360
 *
 
2361
 * Since: 1.4
 
2362
 */
 
2363
void
 
2364
gst_h264_quant_matrix_8x8_get_zigzag_from_raster (guint8 out_quant[64],
 
2365
    const guint8 quant[64])
 
2366
{
 
2367
  guint i;
 
2368
 
 
2369
  g_return_if_fail (out_quant != quant);
 
2370
 
 
2371
  for (i = 0; i < 64; i++)
 
2372
    out_quant[i] = quant[zigzag_8x8[i]];
 
2373
}
 
2374
 
 
2375
/**
 
2376
 * gst_h264_quant_matrix_8x8_get_raster_from_zigzag:
 
2377
 * @out_quant: (out): The resulting quantization matrix
 
2378
 * @quant: The source quantization matrix
 
2379
 *
 
2380
 * Converts quantization matrix @quant from zigzag scan order to
 
2381
 * raster scan order and store the resulting factors into @out_quant.
 
2382
 *
 
2383
 * Note: it is an error to pass the same table in both @quant and
 
2384
 * @out_quant arguments.
 
2385
 *
 
2386
 * Since: 1.4
 
2387
 */
 
2388
void
 
2389
gst_h264_quant_matrix_8x8_get_raster_from_zigzag (guint8 out_quant[64],
 
2390
    const guint8 quant[64])
 
2391
{
 
2392
  guint i;
 
2393
 
 
2394
  g_return_if_fail (out_quant != quant);
 
2395
 
 
2396
  for (i = 0; i < 64; i++)
 
2397
    out_quant[zigzag_8x8[i]] = quant[i];
 
2398
}
 
2399
 
 
2400
/**
 
2401
 * gst_h264_quant_matrix_4x4_get_zigzag_from_raster:
 
2402
 * @out_quant: (out): The resulting quantization matrix
 
2403
 * @quant: The source quantization matrix
 
2404
 *
 
2405
 * Converts quantization matrix @quant from raster scan order to
 
2406
 * zigzag scan order and store the resulting factors into @out_quant.
 
2407
 *
 
2408
 * Note: it is an error to pass the same table in both @quant and
 
2409
 * @out_quant arguments.
 
2410
 *
 
2411
 * Since: 1.4
 
2412
 */
 
2413
void
 
2414
gst_h264_quant_matrix_4x4_get_zigzag_from_raster (guint8 out_quant[16],
 
2415
    const guint8 quant[16])
 
2416
{
 
2417
  guint i;
 
2418
 
 
2419
  g_return_if_fail (out_quant != quant);
 
2420
 
 
2421
  for (i = 0; i < 16; i++)
 
2422
    out_quant[i] = quant[zigzag_4x4[i]];
 
2423
}
 
2424
 
 
2425
/**
 
2426
 * gst_h264_quant_matrix_4x4_get_raster_from_zigzag:
 
2427
 * @out_quant: (out): The resulting quantization matrix
 
2428
 * @quant: The source quantization matrix
 
2429
 *
 
2430
 * Converts quantization matrix @quant from zigzag scan order to
 
2431
 * raster scan order and store the resulting factors into @out_quant.
 
2432
 *
 
2433
 * Note: it is an error to pass the same table in both @quant and
 
2434
 * @out_quant arguments.
 
2435
 *
 
2436
 * Since: 1.4
 
2437
 */
 
2438
void
 
2439
gst_h264_quant_matrix_4x4_get_raster_from_zigzag (guint8 out_quant[16],
 
2440
    const guint8 quant[16])
 
2441
{
 
2442
  guint i;
 
2443
 
 
2444
  g_return_if_fail (out_quant != quant);
 
2445
 
 
2446
  for (i = 0; i < 16; i++)
 
2447
    out_quant[zigzag_4x4[i]] = quant[i];
 
2448
}
 
2449
 
 
2450
/**
 
2451
 * gst_h264_video_calculate_framerate:
 
2452
 * @sps: Current Sequence Parameter Set
 
2453
 * @field_pic_flag: Current @field_pic_flag, obtained from latest slice header
 
2454
 * @pic_struct: @pic_struct value if available, 0 otherwise
 
2455
 * @fps_num: (out): The resulting fps numerator
 
2456
 * @fps_den: (out): The resulting fps denominator
 
2457
 *
 
2458
 * Calculate framerate of a video sequence using @sps VUI information,
 
2459
 * @field_pic_flag from a slice header and @pic_struct from #GstH264PicTiming SEI
 
2460
 * message.
 
2461
 *
 
2462
 * If framerate is variable or can't be determined, @fps_num will be set to 0
 
2463
 * and @fps_den to 1.
 
2464
 */
 
2465
void
 
2466
gst_h264_video_calculate_framerate (const GstH264SPS * sps,
 
2467
    guint field_pic_flag, guint pic_struct, gint * fps_num, gint * fps_den)
 
2468
{
 
2469
  gint num = 0;
 
2470
  gint den = 1;
 
2471
 
 
2472
  /* To calculate framerate, we use this formula:
 
2473
   *          time_scale                1                         1
 
2474
   * fps = -----------------  x  ---------------  x  ------------------------
 
2475
   *       num_units_in_tick     DeltaTfiDivisor     (field_pic_flag ? 2 : 1)
 
2476
   *
 
2477
   * See H264 specification E2.1 for more details.
 
2478
   */
 
2479
 
 
2480
  if (sps) {
 
2481
    if (sps->vui_parameters_present_flag) {
 
2482
      const GstH264VUIParams *vui = &sps->vui_parameters;
 
2483
      if (vui->timing_info_present_flag && vui->fixed_frame_rate_flag) {
 
2484
        int delta_tfi_divisor = 1;
 
2485
        num = vui->time_scale;
 
2486
        den = vui->num_units_in_tick;
 
2487
 
 
2488
        if (vui->pic_struct_present_flag) {
 
2489
          switch (pic_struct) {
 
2490
            case 1:
 
2491
            case 2:
 
2492
              delta_tfi_divisor = 1;
 
2493
              break;
 
2494
            case 0:
 
2495
            case 3:
 
2496
            case 4:
 
2497
              delta_tfi_divisor = 2;
 
2498
              break;
 
2499
            case 5:
 
2500
            case 6:
 
2501
              delta_tfi_divisor = 3;
 
2502
              break;
 
2503
            case 7:
 
2504
              delta_tfi_divisor = 4;
 
2505
              break;
 
2506
            case 8:
 
2507
              delta_tfi_divisor = 6;
 
2508
              break;
 
2509
          }
 
2510
        } else {
 
2511
          delta_tfi_divisor = field_pic_flag ? 1 : 2;
 
2512
        }
 
2513
        den *= delta_tfi_divisor;
 
2514
 
 
2515
        /* Picture is two fields ? */
 
2516
        den *= (field_pic_flag ? 2 : 1);
 
2517
      }
 
2518
    }
 
2519
  }
 
2520
 
 
2521
  *fps_num = num;
 
2522
  *fps_den = den;
2003
2523
}