From 85036682cbe341da237830aae6123e52e6aa6580 Mon Sep 17 00:00:00 2001 From: Debarshi Ray Date: Mon, 23 May 2011 18:06:44 +0300 Subject: [PATCH] matroska: refactor code common to matroskademux and matroskaparse Move the following functions to matroska-read-common.[ch] from matroska-demux.c and matroska-parse.c: - gst_matroska_decode_content_encodings - gst_matroska_decompress_data https://bugzilla.gnome.org/show_bug.cgi?id=650877 --- gst/matroska/matroska-demux.c | 223 ----------------------------------- gst/matroska/matroska-parse.c | 223 ----------------------------------- gst/matroska/matroska-read-common.c | 226 +++++++++++++++++++++++++++++++++++- gst/matroska/matroska-read-common.h | 6 + 4 files changed, 231 insertions(+), 447 deletions(-) diff --git a/gst/matroska/matroska-demux.c b/gst/matroska/matroska-demux.c index 8a358bc..3a4be60 100644 --- a/gst/matroska/matroska-demux.c +++ b/gst/matroska/matroska-demux.c @@ -63,18 +63,8 @@ #include -#ifdef HAVE_ZLIB -#include -#endif - -#ifdef HAVE_BZ2 -#include -#endif - #include -#include "lzo.h" - #include "matroska-demux.h" #include "matroska-ids.h" @@ -776,176 +766,6 @@ gst_matroska_demux_read_track_encoding (GstMatroskaDemux * demux, } static gboolean -gst_matroska_decompress_data (GstMatroskaTrackEncoding * enc, - guint8 ** data_out, guint * size_out, - GstMatroskaTrackCompressionAlgorithm algo) -{ - guint8 *new_data = NULL; - guint new_size = 0; - guint8 *data = *data_out; - guint size = *size_out; - gboolean ret = TRUE; - - if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_ZLIB) { -#ifdef HAVE_ZLIB - /* zlib encoded data */ - z_stream zstream; - guint orig_size; - int result; - - orig_size = size; - zstream.zalloc = (alloc_func) 0; - zstream.zfree = (free_func) 0; - zstream.opaque = (voidpf) 0; - if (inflateInit (&zstream) != Z_OK) { - GST_WARNING ("zlib initialization failed."); - ret = FALSE; - goto out; - } - zstream.next_in = (Bytef *) data; - zstream.avail_in = orig_size; - new_size = orig_size; - new_data = g_malloc (new_size); - zstream.avail_out = new_size; - zstream.next_out = (Bytef *) new_data; - - do { - result = inflate (&zstream, Z_NO_FLUSH); - if (result != Z_OK && result != Z_STREAM_END) { - GST_WARNING ("zlib decompression failed."); - g_free (new_data); - inflateEnd (&zstream); - break; - } - new_size += 4000; - new_data = g_realloc (new_data, new_size); - zstream.next_out = (Bytef *) (new_data + zstream.total_out); - zstream.avail_out += 4000; - } while (zstream.avail_in != 0 && result != Z_STREAM_END); - - if (result != Z_STREAM_END) { - ret = FALSE; - goto out; - } else { - new_size = zstream.total_out; - inflateEnd (&zstream); - } -#else - GST_WARNING ("zlib encoded tracks not supported."); - ret = FALSE; - goto out; -#endif - } else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_BZLIB) { -#ifdef HAVE_BZ2 - /* bzip2 encoded data */ - bz_stream bzstream; - guint orig_size; - int result; - - bzstream.bzalloc = NULL; - bzstream.bzfree = NULL; - bzstream.opaque = NULL; - orig_size = size; - - if (BZ2_bzDecompressInit (&bzstream, 0, 0) != BZ_OK) { - GST_WARNING ("bzip2 initialization failed."); - ret = FALSE; - goto out; - } - - bzstream.next_in = (char *) data; - bzstream.avail_in = orig_size; - new_size = orig_size; - new_data = g_malloc (new_size); - bzstream.avail_out = new_size; - bzstream.next_out = (char *) new_data; - - do { - result = BZ2_bzDecompress (&bzstream); - if (result != BZ_OK && result != BZ_STREAM_END) { - GST_WARNING ("bzip2 decompression failed."); - g_free (new_data); - BZ2_bzDecompressEnd (&bzstream); - break; - } - new_size += 4000; - new_data = g_realloc (new_data, new_size); - bzstream.next_out = (char *) (new_data + bzstream.total_out_lo32); - bzstream.avail_out += 4000; - } while (bzstream.avail_in != 0 && result != BZ_STREAM_END); - - if (result != BZ_STREAM_END) { - ret = FALSE; - goto out; - } else { - new_size = bzstream.total_out_lo32; - BZ2_bzDecompressEnd (&bzstream); - } -#else - GST_WARNING ("bzip2 encoded tracks not supported."); - ret = FALSE; - goto out; -#endif - } else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_LZO1X) { - /* lzo encoded data */ - int result; - int orig_size, out_size; - - orig_size = size; - out_size = size; - new_size = size; - new_data = g_malloc (new_size); - - do { - orig_size = size; - out_size = new_size; - - result = lzo1x_decode (new_data, &out_size, data, &orig_size); - - if (orig_size > 0) { - new_size += 4000; - new_data = g_realloc (new_data, new_size); - } - } while (orig_size > 0 && result == LZO_OUTPUT_FULL); - - new_size -= out_size; - - if (result != LZO_OUTPUT_FULL) { - GST_WARNING ("lzo decompression failed"); - g_free (new_data); - - ret = FALSE; - goto out; - } - - } else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_HEADERSTRIP) { - /* header stripped encoded data */ - if (enc->comp_settings_length > 0) { - new_data = g_malloc (size + enc->comp_settings_length); - new_size = size + enc->comp_settings_length; - - memcpy (new_data, enc->comp_settings, enc->comp_settings_length); - memcpy (new_data + enc->comp_settings_length, data, size); - } - } else { - GST_ERROR ("invalid compression algorithm %d", algo); - ret = FALSE; - } - -out: - - if (!ret) { - *data_out = NULL; - *size_out = 0; - } else { - *data_out = new_data; - *size_out = new_size; - } - - return ret; -} - -static gboolean gst_matroska_decode_data (GArray * encodings, guint8 ** data_out, guint * size_out, GstMatroskaTrackEncodingScope scope, gboolean free) { @@ -1042,49 +862,6 @@ gst_matroska_decode_buffer (GstMatroskaTrackContext * context, GstBuffer * buf) } static GstFlowReturn -gst_matroska_decode_content_encodings (GArray * encodings) -{ - gint i; - - if (encodings == NULL) - return GST_FLOW_OK; - - for (i = 0; i < encodings->len; i++) { - GstMatroskaTrackEncoding *enc = - &g_array_index (encodings, GstMatroskaTrackEncoding, i); - guint8 *data = NULL; - guint size; - - if ((enc->scope & GST_MATROSKA_TRACK_ENCODING_SCOPE_NEXT_CONTENT_ENCODING) - == 0) - continue; - - /* Encryption not supported yet */ - if (enc->type != 0) - return GST_FLOW_ERROR; - - if (i + 1 >= encodings->len) - return GST_FLOW_ERROR; - - if (enc->comp_settings_length == 0) - continue; - - data = enc->comp_settings; - size = enc->comp_settings_length; - - if (!gst_matroska_decompress_data (enc, &data, &size, enc->comp_algo)) - return GST_FLOW_ERROR; - - g_free (enc->comp_settings); - - enc->comp_settings = data; - enc->comp_settings_length = size; - } - - return GST_FLOW_OK; -} - -static GstFlowReturn gst_matroska_demux_read_track_encodings (GstMatroskaDemux * demux, GstEbmlRead * ebml, GstMatroskaTrackContext * context) { diff --git a/gst/matroska/matroska-parse.c b/gst/matroska/matroska-parse.c index 3dfb944..1b4f724 100644 --- a/gst/matroska/matroska-parse.c +++ b/gst/matroska/matroska-parse.c @@ -63,18 +63,8 @@ #include -#ifdef HAVE_ZLIB -#include -#endif - -#ifdef HAVE_BZ2 -#include -#endif - #include -#include "lzo.h" - #include "matroska-parse.h" #include "matroska-ids.h" @@ -699,176 +689,6 @@ gst_matroska_parse_read_track_encoding (GstMatroskaParse * parse, } static gboolean -gst_matroska_decompress_data (GstMatroskaTrackEncoding * enc, - guint8 ** data_out, guint * size_out, - GstMatroskaTrackCompressionAlgorithm algo) -{ - guint8 *new_data = NULL; - guint new_size = 0; - guint8 *data = *data_out; - guint size = *size_out; - gboolean ret = TRUE; - - if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_ZLIB) { -#ifdef HAVE_ZLIB - /* zlib encoded data */ - z_stream zstream; - guint orig_size; - int result; - - orig_size = size; - zstream.zalloc = (alloc_func) 0; - zstream.zfree = (free_func) 0; - zstream.opaque = (voidpf) 0; - if (inflateInit (&zstream) != Z_OK) { - GST_WARNING ("zlib initialization failed."); - ret = FALSE; - goto out; - } - zstream.next_in = (Bytef *) data; - zstream.avail_in = orig_size; - new_size = orig_size; - new_data = g_malloc (new_size); - zstream.avail_out = new_size; - zstream.next_out = (Bytef *) new_data; - - do { - result = inflate (&zstream, Z_NO_FLUSH); - if (result != Z_OK && result != Z_STREAM_END) { - GST_WARNING ("zlib decompression failed."); - g_free (new_data); - inflateEnd (&zstream); - break; - } - new_size += 4000; - new_data = g_realloc (new_data, new_size); - zstream.next_out = (Bytef *) (new_data + zstream.total_out); - zstream.avail_out += 4000; - } while (zstream.avail_in != 0 && result != Z_STREAM_END); - - if (result != Z_STREAM_END) { - ret = FALSE; - goto out; - } else { - new_size = zstream.total_out; - inflateEnd (&zstream); - } -#else - GST_WARNING ("zlib encoded tracks not supported."); - ret = FALSE; - goto out; -#endif - } else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_BZLIB) { -#ifdef HAVE_BZ2 - /* bzip2 encoded data */ - bz_stream bzstream; - guint orig_size; - int result; - - bzstream.bzalloc = NULL; - bzstream.bzfree = NULL; - bzstream.opaque = NULL; - orig_size = size; - - if (BZ2_bzDecompressInit (&bzstream, 0, 0) != BZ_OK) { - GST_WARNING ("bzip2 initialization failed."); - ret = FALSE; - goto out; - } - - bzstream.next_in = (char *) data; - bzstream.avail_in = orig_size; - new_size = orig_size; - new_data = g_malloc (new_size); - bzstream.avail_out = new_size; - bzstream.next_out = (char *) new_data; - - do { - result = BZ2_bzDecompress (&bzstream); - if (result != BZ_OK && result != BZ_STREAM_END) { - GST_WARNING ("bzip2 decompression failed."); - g_free (new_data); - BZ2_bzDecompressEnd (&bzstream); - break; - } - new_size += 4000; - new_data = g_realloc (new_data, new_size); - bzstream.next_out = (char *) (new_data + bzstream.total_out_lo32); - bzstream.avail_out += 4000; - } while (bzstream.avail_in != 0 && result != BZ_STREAM_END); - - if (result != BZ_STREAM_END) { - ret = FALSE; - goto out; - } else { - new_size = bzstream.total_out_lo32; - BZ2_bzDecompressEnd (&bzstream); - } -#else - GST_WARNING ("bzip2 encoded tracks not supported."); - ret = FALSE; - goto out; -#endif - } else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_LZO1X) { - /* lzo encoded data */ - int result; - int orig_size, out_size; - - orig_size = size; - out_size = size; - new_size = size; - new_data = g_malloc (new_size); - - do { - orig_size = size; - out_size = new_size; - - result = lzo1x_decode (new_data, &out_size, data, &orig_size); - - if (orig_size > 0) { - new_size += 4000; - new_data = g_realloc (new_data, new_size); - } - } while (orig_size > 0 && result == LZO_OUTPUT_FULL); - - new_size -= out_size; - - if (result != LZO_OUTPUT_FULL) { - GST_WARNING ("lzo decompression failed"); - g_free (new_data); - - ret = FALSE; - goto out; - } - - } else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_HEADERSTRIP) { - /* header stripped encoded data */ - if (enc->comp_settings_length > 0) { - new_data = g_malloc (size + enc->comp_settings_length); - new_size = size + enc->comp_settings_length; - - memcpy (new_data, enc->comp_settings, enc->comp_settings_length); - memcpy (new_data + enc->comp_settings_length, data, size); - } - } else { - GST_ERROR ("invalid compression algorithm %d", algo); - ret = FALSE; - } - -out: - - if (!ret) { - *data_out = NULL; - *size_out = 0; - } else { - *data_out = new_data; - *size_out = new_size; - } - - return ret; -} - -static gboolean gst_matroska_decode_data (GArray * encodings, guint8 ** data_out, guint * size_out, GstMatroskaTrackEncodingScope scope, gboolean free) { @@ -931,49 +751,6 @@ gst_matroska_decode_data (GArray * encodings, guint8 ** data_out, } static GstFlowReturn -gst_matroska_decode_content_encodings (GArray * encodings) -{ - gint i; - - if (encodings == NULL) - return GST_FLOW_OK; - - for (i = 0; i < encodings->len; i++) { - GstMatroskaTrackEncoding *enc = - &g_array_index (encodings, GstMatroskaTrackEncoding, i); - guint8 *data = NULL; - guint size; - - if ((enc->scope & GST_MATROSKA_TRACK_ENCODING_SCOPE_NEXT_CONTENT_ENCODING) - == 0) - continue; - - /* Encryption not supported yet */ - if (enc->type != 0) - return GST_FLOW_ERROR; - - if (i + 1 >= encodings->len) - return GST_FLOW_ERROR; - - if (enc->comp_settings_length == 0) - continue; - - data = enc->comp_settings; - size = enc->comp_settings_length; - - if (!gst_matroska_decompress_data (enc, &data, &size, enc->comp_algo)) - return GST_FLOW_ERROR; - - g_free (enc->comp_settings); - - enc->comp_settings = data; - enc->comp_settings_length = size; - } - - return GST_FLOW_OK; -} - -static GstFlowReturn gst_matroska_parse_read_track_encodings (GstMatroskaParse * parse, GstEbmlRead * ebml, GstMatroskaTrackContext * context) { diff --git a/gst/matroska/matroska-read-common.c b/gst/matroska/matroska-read-common.c index 9172c171..c1a2abc 100644 --- a/gst/matroska/matroska-read-common.c +++ b/gst/matroska/matroska-read-common.c @@ -23,8 +23,19 @@ #include "config.h" #endif +#include + +#ifdef HAVE_ZLIB +#include +#endif + +#ifdef HAVE_BZ2 +#include +#endif + +#include "lzo.h" + #include "ebml-read.h" -#include "matroska-ids.h" #include "matroska-read-common.h" GST_DEBUG_CATEGORY_STATIC (matroskareadcommon_debug); @@ -38,6 +49,219 @@ GST_DEBUG_CATEGORY_STATIC (matroskareadcommon_debug); GST_DEBUG_OBJECT (common, "Parsing " element " element " \ " finished with '%s'", gst_flow_get_name (ret)) +GstFlowReturn +gst_matroska_decode_content_encodings (GArray * encodings) +{ + gint i; + + if (encodings == NULL) + return GST_FLOW_OK; + + for (i = 0; i < encodings->len; i++) { + GstMatroskaTrackEncoding *enc = + &g_array_index (encodings, GstMatroskaTrackEncoding, i); + guint8 *data = NULL; + guint size; + + if ((enc->scope & GST_MATROSKA_TRACK_ENCODING_SCOPE_NEXT_CONTENT_ENCODING) + == 0) + continue; + + /* Encryption not supported yet */ + if (enc->type != 0) + return GST_FLOW_ERROR; + + if (i + 1 >= encodings->len) + return GST_FLOW_ERROR; + + if (enc->comp_settings_length == 0) + continue; + + data = enc->comp_settings; + size = enc->comp_settings_length; + + if (!gst_matroska_decompress_data (enc, &data, &size, enc->comp_algo)) + return GST_FLOW_ERROR; + + g_free (enc->comp_settings); + + enc->comp_settings = data; + enc->comp_settings_length = size; + } + + return GST_FLOW_OK; +} + +gboolean +gst_matroska_decompress_data (GstMatroskaTrackEncoding * enc, + guint8 ** data_out, guint * size_out, + GstMatroskaTrackCompressionAlgorithm algo) +{ + guint8 *new_data = NULL; + guint new_size = 0; + guint8 *data = *data_out; + guint size = *size_out; + gboolean ret = TRUE; + + if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_ZLIB) { +#ifdef HAVE_ZLIB + /* zlib encoded data */ + z_stream zstream; + guint orig_size; + int result; + + orig_size = size; + zstream.zalloc = (alloc_func) 0; + zstream.zfree = (free_func) 0; + zstream.opaque = (voidpf) 0; + if (inflateInit (&zstream) != Z_OK) { + GST_WARNING ("zlib initialization failed."); + ret = FALSE; + goto out; + } + zstream.next_in = (Bytef *) data; + zstream.avail_in = orig_size; + new_size = orig_size; + new_data = g_malloc (new_size); + zstream.avail_out = new_size; + zstream.next_out = (Bytef *) new_data; + + do { + result = inflate (&zstream, Z_NO_FLUSH); + if (result != Z_OK && result != Z_STREAM_END) { + GST_WARNING ("zlib decompression failed."); + g_free (new_data); + inflateEnd (&zstream); + break; + } + new_size += 4000; + new_data = g_realloc (new_data, new_size); + zstream.next_out = (Bytef *) (new_data + zstream.total_out); + zstream.avail_out += 4000; + } while (zstream.avail_in != 0 && result != Z_STREAM_END); + + if (result != Z_STREAM_END) { + ret = FALSE; + goto out; + } else { + new_size = zstream.total_out; + inflateEnd (&zstream); + } +#else + GST_WARNING ("zlib encoded tracks not supported."); + ret = FALSE; + goto out; +#endif + } else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_BZLIB) { +#ifdef HAVE_BZ2 + /* bzip2 encoded data */ + bz_stream bzstream; + guint orig_size; + int result; + + bzstream.bzalloc = NULL; + bzstream.bzfree = NULL; + bzstream.opaque = NULL; + orig_size = size; + + if (BZ2_bzDecompressInit (&bzstream, 0, 0) != BZ_OK) { + GST_WARNING ("bzip2 initialization failed."); + ret = FALSE; + goto out; + } + + bzstream.next_in = (char *) data; + bzstream.avail_in = orig_size; + new_size = orig_size; + new_data = g_malloc (new_size); + bzstream.avail_out = new_size; + bzstream.next_out = (char *) new_data; + + do { + result = BZ2_bzDecompress (&bzstream); + if (result != BZ_OK && result != BZ_STREAM_END) { + GST_WARNING ("bzip2 decompression failed."); + g_free (new_data); + BZ2_bzDecompressEnd (&bzstream); + break; + } + new_size += 4000; + new_data = g_realloc (new_data, new_size); + bzstream.next_out = (char *) (new_data + bzstream.total_out_lo32); + bzstream.avail_out += 4000; + } while (bzstream.avail_in != 0 && result != BZ_STREAM_END); + + if (result != BZ_STREAM_END) { + ret = FALSE; + goto out; + } else { + new_size = bzstream.total_out_lo32; + BZ2_bzDecompressEnd (&bzstream); + } +#else + GST_WARNING ("bzip2 encoded tracks not supported."); + ret = FALSE; + goto out; +#endif + } else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_LZO1X) { + /* lzo encoded data */ + int result; + int orig_size, out_size; + + orig_size = size; + out_size = size; + new_size = size; + new_data = g_malloc (new_size); + + do { + orig_size = size; + out_size = new_size; + + result = lzo1x_decode (new_data, &out_size, data, &orig_size); + + if (orig_size > 0) { + new_size += 4000; + new_data = g_realloc (new_data, new_size); + } + } while (orig_size > 0 && result == LZO_OUTPUT_FULL); + + new_size -= out_size; + + if (result != LZO_OUTPUT_FULL) { + GST_WARNING ("lzo decompression failed"); + g_free (new_data); + + ret = FALSE; + goto out; + } + + } else if (algo == GST_MATROSKA_TRACK_COMPRESSION_ALGORITHM_HEADERSTRIP) { + /* header stripped encoded data */ + if (enc->comp_settings_length > 0) { + new_data = g_malloc (size + enc->comp_settings_length); + new_size = size + enc->comp_settings_length; + + memcpy (new_data, enc->comp_settings, enc->comp_settings_length); + memcpy (new_data + enc->comp_settings_length, data, size); + } + } else { + GST_ERROR ("invalid compression algorithm %d", algo); + ret = FALSE; + } + +out: + + if (!ret) { + *data_out = NULL; + *size_out = 0; + } else { + *data_out = new_data; + *size_out = new_size; + } + + return ret; +} + static gint gst_matroska_index_compare (GstMatroskaIndex * i1, GstMatroskaIndex * i2) { diff --git a/gst/matroska/matroska-read-common.h b/gst/matroska/matroska-read-common.h index a413f0b..8093fcb 100644 --- a/gst/matroska/matroska-read-common.h +++ b/gst/matroska/matroska-read-common.h @@ -25,6 +25,8 @@ #include #include +#include "matroska-ids.h" + G_BEGIN_DECLS typedef enum { @@ -60,6 +62,10 @@ typedef struct _GstMatroskaReadCommon { guint64 time_scale; } GstMatroskaReadCommon; +GstFlowReturn gst_matroska_decode_content_encodings (GArray * encodings); +gboolean gst_matroska_decompress_data (GstMatroskaTrackEncoding * enc, + guint8 ** data_out, guint * size_out, + GstMatroskaTrackCompressionAlgorithm algo); GstFlowReturn gst_matroska_read_common_parse_index (GstMatroskaReadCommon * common, GstEbmlRead * ebml); GstFlowReturn gst_matroska_read_common_parse_skip (GstMatroskaReadCommon * -- 2.7.4