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
This commit is contained in:
Debarshi Ray 2011-05-23 18:06:44 +03:00 committed by Sebastian Dröge
parent 51c7e6d252
commit 85036682cb
4 changed files with 231 additions and 447 deletions

View file

@ -63,18 +63,8 @@
#include <gst/base/gsttypefindhelper.h>
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif
#ifdef HAVE_BZ2
#include <bzlib.h>
#endif
#include <gst/pbutils/pbutils.h>
#include "lzo.h"
#include "matroska-demux.h"
#include "matroska-ids.h"
@ -775,176 +765,6 @@ gst_matroska_demux_read_track_encoding (GstMatroskaDemux * demux,
return ret;
}
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)
@ -1041,49 +861,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)

View file

@ -63,18 +63,8 @@
#include <gst/base/gsttypefindhelper.h>
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif
#ifdef HAVE_BZ2
#include <bzlib.h>
#endif
#include <gst/pbutils/pbutils.h>
#include "lzo.h"
#include "matroska-parse.h"
#include "matroska-ids.h"
@ -698,176 +688,6 @@ gst_matroska_parse_read_track_encoding (GstMatroskaParse * parse,
return ret;
}
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)
@ -930,49 +750,6 @@ gst_matroska_decode_data (GArray * encodings, guint8 ** data_out,
return ret;
}
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)

View file

@ -23,8 +23,19 @@
#include "config.h"
#endif
#include <string.h>
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif
#ifdef HAVE_BZ2
#include <bzlib.h>
#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)
{

View file

@ -25,6 +25,8 @@
#include <glib.h>
#include <gst/gst.h>
#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 *