123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- From f6a8b85db4d85d8e6101d1a73c825c7e6df52f3a Mon Sep 17 00:00:00 2001
- From: Marco Ballesio <marco.ballesio@nokia.com>
- Date: Fri, 5 Jun 2009 15:35:06 +0300
- Subject: [PATCH] parse: add MPEG-4
- Signed-off-by: Felipe Contreras <felipe.contreras@nokia.com>
- ---
- gstdspparse.c | 242 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
- gstdspparse.h | 1 +
- gstdspvdec.c | 4 +-
- 3 files changed, 245 insertions(+), 2 deletions(-)
- diff --git a/gstdspparse.c b/gstdspparse.c
- index 299e86b..15c55a1 100644
- --- a/gstdspparse.c
- +++ b/gstdspparse.c
- @@ -3,7 +3,8 @@
- * Copyright (C) 2007-2009 Nokia Corporation
- *
- * Authors:
- - * Marco Ballesio <marco.ballesio@gmail.com>
- + * Juha Alanen <juha.m.alanen@nokia.com>
- + * Marco Ballesio <marco.ballesio@nokia.com>
- * Felipe Contreras <felipe.contreras@nokia.com>
- *
- * This library is free software; you can redistribute it and/or
- @@ -384,3 +385,242 @@ not_enough_data:
- bail:
- return FALSE;
- }
- +
- +/* From mpeg4videoparse */
- +/* Used for parsing frame size from codec data */
- +typedef struct
- +{
- + const guint8 *data;
- + /* byte offset */
- + gsize offset;
- + /* bit offset */
- + gsize b_offset;
- + /* size in bytes */
- + gsize size;
- +} bitstream_t;
- +
- +static gboolean
- +get_bits (bitstream_t * b, int num, guint32 * bits)
- +{
- + *bits = 0;
- +
- + if (b->offset + ((b->b_offset + num) / 8) > b->size)
- + return FALSE;
- +
- + if (b->b_offset + num <= 8) {
- + *bits = b->data[b->offset];
- + *bits = (*bits >> (8 - num - b->b_offset)) & (((1 << num)) - 1);
- +
- + b->offset += (b->b_offset + num) / 8;
- + b->b_offset = (b->b_offset + num) % 8;
- + return TRUE;
- + } else {
- + /* going over the edge.. */
- + int next;
- +
- + next = (8 - b->b_offset);
- + do {
- + guint32 t;
- +
- + if (!get_bits (b, next, &t))
- + return FALSE;
- + *bits <<= next;
- + *bits |= t;
- + num -= next;
- + next = MIN (8, num);
- + } while (num > 0);
- +
- + return TRUE;
- + }
- +}
- +
- +#define GET_BITS(b, num, bits) G_STMT_START { \
- + if (!get_bits(b, num, bits)) \
- + goto failed; \
- +} G_STMT_END
- +
- +#define MARKER_BIT(b) G_STMT_START { \
- + guint32 i; \
- + GET_BITS(b, 1, &i); \
- + if (i != 0x1) \
- + goto failed; \
- +} G_STMT_END
- +
- +static inline gboolean
- +next_start_code (bitstream_t * b)
- +{
- + guint32 bits;
- +
- + GET_BITS (b, 1, &bits);
- + if (bits != 0)
- + goto failed;
- +
- + while (b->b_offset != 0) {
- + GET_BITS (b, 1, &bits);
- + if (bits != 0x1)
- + goto failed;
- + }
- +
- + return TRUE;
- +
- +failed:
- + return FALSE;
- +}
- +
- +gboolean
- +gst_dsp_mpeg4_parse(GstDspBase* base, GstBuffer *buf)
- +{
- + guint32 bits;
- + bitstream_t bs;
- + guint8 *codec_data;
- + guint codec_data_size;
- + guint16 time_increment_resolution;
- + guint16 width, height;
- +
- + codec_data = GST_BUFFER_DATA (buf);
- + codec_data_size = GST_BUFFER_SIZE (buf);
- +
- + bs.data = codec_data;
- + bs.size = codec_data_size;
- + bs.offset = 0;
- + bs.b_offset = 0;
- +
- + /* Expect Visual Object Sequence startcode (0x000001B0) */
- + GET_BITS (&bs, 32, &bits);
- + if (bits != 0x1B0)
- + goto failed;
- +
- + /* profile and level indication */
- + GET_BITS (&bs, 8, &bits);
- + if (bits == 0)
- + goto failed;
- +
- + /* Expect Visual Object startcode (0x000001B5) */
- + GET_BITS (&bs, 32, &bits);
- + if (bits != 0x1B5)
- + goto failed;
- +
- + GET_BITS (&bs, 1, &bits);
- + if (bits == 0x1) {
- + /* Skip visual_object_verid and priority */
- + GET_BITS (&bs, 7, &bits);
- + }
- +
- + GET_BITS (&bs, 4, &bits);
- + /* Only support video ID */
- + if (bits != 0x1)
- + goto failed;
- +
- + /* video signal type */
- + GET_BITS (&bs, 1, &bits);
- +
- + if (bits == 0x1) {
- + /* video signal type, ignore format and range */
- + GET_BITS (&bs, 4, &bits);
- +
- + GET_BITS (&bs, 1, &bits);
- + if (bits == 0x1) {
- + /* ignore color description */
- + GET_BITS (&bs, 24, &bits);
- + }
- + }
- +
- + if (!next_start_code (&bs))
- + goto failed;
- +
- + /* expecting a video object startcode */
- + GET_BITS (&bs, 32, &bits);
- + if (bits > 0x11F)
- + goto failed;
- +
- + /* expecting a video object layer startcode */
- + GET_BITS (&bs, 32, &bits);
- + if (bits < 0x120 || bits > 0x12F)
- + goto failed;
- +
- + /* ignore random accessible vol and video object type indication */
- + GET_BITS (&bs, 9, &bits);
- +
- + GET_BITS (&bs, 1, &bits);
- + if (bits) {
- + /* skip video object layer verid and priority */
- + GET_BITS (&bs, 7, &bits);
- + }
- +
- + /* aspect ratio info */
- + GET_BITS (&bs, 4, &bits);
- + if (bits == 0)
- + goto failed;
- +
- + /* check if aspect ratio info is extended par */
- + if (bits == 0xff) {
- + /* aspect_ratio_width */
- + GET_BITS (&bs, 4, &bits);
- + /* aspect_ratio_height */
- + GET_BITS (&bs, 4, &bits);
- + } else if (bits < 0x6) {
- + /* get aspect ratio width and height from aspect ratio table */
- + }
- +
- + GET_BITS (&bs, 1, &bits);
- + if (bits) {
- + /* vol control parameters, skip chroma and low delay */
- + GET_BITS (&bs, 3, &bits);
- + GET_BITS (&bs, 1, &bits);
- + if (bits) {
- + /* skip vbv_parameters */
- + GET_BITS (&bs, 79, &bits);
- + }
- + }
- +
- + /* layer shape */
- + GET_BITS (&bs, 2, &bits);
- + /* only support rectangular */
- + if (bits != 0)
- + goto failed;
- +
- + MARKER_BIT (&bs);
- + GET_BITS (&bs, 16, &bits);
- + time_increment_resolution = bits;
- + MARKER_BIT (&bs);
- +
- + GET_BITS (&bs, 1, &bits);
- + if (bits) {
- + /* fixed time increment */
- + int n;
- +
- + /* Lenght of the time increment is the minimal number of bits needed to
- + * represent time_increment_resolution */
- + for (n = 0; (time_increment_resolution >> n) != 0; n++);
- + GET_BITS (&bs, n, &bits);
- + /* fixed_time_increment = bits; */
- + } else {
- + /* fixed_time_increment = 1; */
- + }
- +
- + /* assuming rectangular shape */
- + MARKER_BIT (&bs);
- + GET_BITS (&bs, 13, &bits);
- + if (bits == 0)
- + goto failed;
- + width = bits;
- +
- + MARKER_BIT (&bs);
- + GET_BITS (&bs, 13, &bits);
- + if (bits == 0)
- + goto failed;
- + height = bits;
- + MARKER_BIT (&bs);
- +
- + pr_debug (base, "width=%u, height=%u", width, height);
- +
- + set_framesize(base, width, height);
- + return TRUE;
- +
- + /* ERRORS */
- +failed:
- + {
- + GST_DEBUG_OBJECT (base, "Could not parse MPEG4 codec data");
- + return FALSE;
- + }
- +}
- diff --git a/gstdspparse.h b/gstdspparse.h
- index cbaf711..c06abea 100644
- --- a/gstdspparse.h
- +++ b/gstdspparse.h
- @@ -26,5 +26,6 @@
-
- gboolean gst_dsp_h263_parse(GstDspBase *base, GstBuffer *buf);
- gboolean gst_dsp_h264_parse(GstDspBase *base, GstBuffer *buf);
- +gboolean gst_dsp_mpeg4_parse(GstDspBase *base, GstBuffer *buf);
-
- #endif
- diff --git a/gstdspvdec.c b/gstdspvdec.c
- index b842bbd..f4cd0a7 100644
- --- a/gstdspvdec.c
- +++ b/gstdspvdec.c
- @@ -1008,8 +1008,10 @@ sink_setcaps(GstPad *pad,
- self->wmv_is_vc1 = FALSE;
- }
- }
- - else
- + else {
- base->alg = GSTDSP_MPEG4VDEC;
- + base->parse_func = gst_dsp_mpeg4_parse;
- + }
-
- out_caps = gst_caps_new_empty();
-
|