Bug 468281 - Update liboggplay to fix audit issues - rs=roc
☠☠ backed out by e24fe5aafa6f ☠ ☠
authorChris Double <chris.double@double.co.nz>
Fri, 06 Mar 2009 12:29:24 +1300
changeset 25789 d132b09831a138af2757217dc27b26f8c9615c6c
parent 25788 88074ea4c2880e8cf79d0708d17bbddde8489f45
child 25790 2a03f4a992fa348480489c49f5d26f78f7286aca
child 25791 e24fe5aafa6faac9f8b666cac601011623d1d365
push id5745
push usercdouble@mozilla.com
push dateThu, 05 Mar 2009 23:34:30 +0000
treeherdermozilla-central@d132b09831a1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc
bugs468281
milestone1.9.2a1pre
Bug 468281 - Update liboggplay to fix audit issues - rs=roc
media/liboggplay/README_MOZILLA
media/liboggplay/bug464007.patch
media/liboggplay/bug468281_r3863.patch
media/liboggplay/bug468281_r3864.patch
media/liboggplay/include/oggplay/oggplay.h
media/liboggplay/include/oggplay/oggplay_enums.h
media/liboggplay/include/oggplay/oggplay_reader.h
media/liboggplay/src/liboggplay/oggplay.c
media/liboggplay/src/liboggplay/oggplay_buffer.c
media/liboggplay/src/liboggplay/oggplay_buffer.h
media/liboggplay/src/liboggplay/oggplay_callback.c
media/liboggplay/src/liboggplay/oggplay_callback.h
media/liboggplay/src/liboggplay/oggplay_callback_info.c
media/liboggplay/src/liboggplay/oggplay_data.c
media/liboggplay/src/liboggplay/oggplay_file_reader.c
media/liboggplay/src/liboggplay/oggplay_private.h
media/liboggplay/src/liboggplay/oggplay_seek.c
media/liboggplay/src/liboggplay/oggplay_tcp_reader.c
media/liboggplay/update.sh
--- a/media/liboggplay/README_MOZILLA
+++ b/media/liboggplay/README_MOZILLA
@@ -8,10 +8,13 @@ http://svn.annodex.net/liboggplay/trunk/
 The svn revision number used was r3848.
 
 The patch from Bug 468327 (yuv_disable_optimized.patch) is applied
 to disable optimized yuv to rgb routines.
 
 The patch from Bug 452698 (yuv2argb.patch) is applied to fix
 yuv to rgb conversion on PPC Mac OS X.
 
-The patch from Bug 464007 (bug464007.patch) is applied
-to fix oggplay issues.
+Apply patch from bug 468281 to get liboggplay svn r3863 changeset.
+The patch for this is in bug468281_r3863.patch.
+
+Apply patch from bug 468281 to get liboggplay svn r3864 changeset.
+The patch for this is in bug468281_r3864.patch.
deleted file mode 100644
--- a/media/liboggplay/bug464007.patch
+++ /dev/null
@@ -1,18 +0,0 @@
-diff --git a/media/liboggplay/src/liboggplay/oggplay.c b/media/liboggplay/src/liboggplay/oggplay.c
-index 9a1c8b8..250064d 100644
---- a/media/liboggplay/src/liboggplay/oggplay.c
-+++ b/media/liboggplay/src/liboggplay/oggplay.c
-@@ -522,9 +522,11 @@ read_more_data:
-       }
- 
-       /*
--       * ensure all tracks have their final data packet set to end_of_stream
-+       * ensure all tracks have their final data packet set to end_of_stream.
-+       * But skip doing this if we're shutting down --- me->buffer may not
-+       * be in a safe state.
-        */
--      if (me->buffer != NULL) {
-+      if (me->buffer != NULL && !me->shutdown) {
-         oggplay_buffer_set_last_data(me, me->buffer);
-       }
- 
new file mode 100644
--- /dev/null
+++ b/media/liboggplay/bug468281_r3863.patch
@@ -0,0 +1,975 @@
+diff --git a/include/oggplay/oggplay.h b/include/oggplay/oggplay.h
+index 019958a..cdcb168 100644
+--- a/include/oggplay/oggplay.h
++++ b/include/oggplay/oggplay.h
+@@ -56,20 +56,27 @@ typedef int (OggPlayDataCallback)(OggPlay *player, int num_records,
+ #include <oggplay/oggplay_callback_info.h>
+ #include <oggplay/oggplay_tools.h>
+ #include <oggplay/oggplay_seek.h>
+-/*
+-#include <oggplay/oggplay_retrieve.h>
+-#include <oggplay/oggplay_cmml.h>
+-*/
+-
+-OggPlay *
+-oggplay_init(void);
+-
+-OggPlayErrorCode
+-oggplay_set_reader(OggPlay *OS, OggPlayReader *OSR);
+ 
++/**
++ * Create an OggPlay handle associated with the given reader.
++ * The functions creates a new OggPlay handle and associates with
++ * the given OggPlayReader and initialises the buffer.
++ * 
++ *
++ * @param reader an OggPlayReader handle associated with the Ogg content
++ * @return A new OggPlay handle
++ * @retval NULL in case of error.
++ */
+ OggPlay *
+ oggplay_open_with_reader(OggPlayReader *reader);
+ 
++/**
++ * Create a new OggPlay handle associated with the given reader.
++ *
++ * \param reader OggPlayReader handle associated with the Ogg content
++ * \return A new OggPlay handle
++ * \retval NULL in case of error.
++ */
+ OggPlay *
+ oggplay_new_with_reader(OggPlayReader *reader);
+ 
+@@ -128,6 +135,13 @@ oggplay_buffer_release(OggPlay *player, OggPlayCallbackInfo **track_info);
+ void
+ oggplay_prepare_for_close(OggPlay *me);
+ 
++/**
++ * @brief Destroys the OggPlay handle along with the associated OggPlayReader
++ * and clears out the buffer and shuts down the callback function. 
++ *
++ * @param player an OggPlay handle
++ * @retval E_OGGPLAY_OK on success 
++ */
+ OggPlayErrorCode
+ oggplay_close(OggPlay *player);
+ 
+diff --git a/include/oggplay/oggplay_enums.h b/include/oggplay/oggplay_enums.h
+index cda068f..6f4790f 100644
+--- a/include/oggplay/oggplay_enums.h
++++ b/include/oggplay/oggplay_enums.h
+@@ -62,6 +62,7 @@ typedef enum OggPlayErrorCode {
+   E_OGGPLAY_TIMEOUT           = -17,
+   E_OGGPLAY_CANT_SEEK         = -18,
+   E_OGGPLAY_NO_KATE_SUPPORT   = -19,
++  E_OGGPLAY_OUT_OF_MEMORY     = -20,
+   E_OGGPLAY_NOTCHICKENPAYBACK = -777
+ } OggPlayErrorCode;
+ 
+diff --git a/include/oggplay/oggplay_reader.h b/include/oggplay/oggplay_reader.h
+index 992f416..21b5206 100644
+--- a/include/oggplay/oggplay_reader.h
++++ b/include/oggplay/oggplay_reader.h
+@@ -63,9 +63,25 @@ typedef struct _OggPlayReader {
+   long              (*io_tell)(void *user_handle);
+ } OggPlayReader;
+ 
++/**
++ * Create and initialise an OggPlayReader for a given Ogg file.
++ * 
++ * @param filename The file to open
++ * @return A new OggPlayReader handle
++ * @retval NULL on error.
++ */
+ OggPlayReader *
+ oggplay_file_reader_new(char *filename);
+ 
++/**
++ * Create and initialise an OggPlayReader for an Ogg content at a given URI. 
++ *
++ * @param uri The URI to the Ogg file.
++ * @param proxy Proxy 
++ * @param proxy_port Proxy port.
++ * @return A new OggPlayReader handle
++ * @retval NULL on error.
++ */
+ OggPlayReader *
+ oggplay_tcp_reader_new(char *uri, char *proxy, int proxy_port);
+ 
+diff --git a/src/liboggplay/oggplay.c b/src/liboggplay/oggplay.c
+index c71d639..8fa3b9e 100644
+--- a/src/liboggplay/oggplay.c
++++ b/src/liboggplay/oggplay.c
+@@ -48,7 +48,15 @@
+ OggPlay *
+ oggplay_new_with_reader(OggPlayReader *reader) {
+ 
+-  OggPlay * me = (OggPlay *)malloc(sizeof(OggPlay));
++  OggPlay * me = NULL;
++
++  /* check whether the reader is valid. */
++  if (reader == NULL)
++    return NULL;
++
++  me = (OggPlay *)oggplay_malloc (sizeof(OggPlay));
++  if (me == NULL)
++	  return NULL;
+ 
+   me->reader = reader;
+   me->decode_data = NULL;
+@@ -94,10 +102,20 @@ oggplay_initialise(OggPlay *me, int block) {
+    * the main loop
+    */
+   me->oggz = oggz_new(OGGZ_READ | OGGZ_AUTO);
+-  oggz_io_set_read(me->oggz, me->reader->io_read, me->reader);
+-  oggz_io_set_seek(me->oggz, me->reader->io_seek, me->reader);
+-  oggz_io_set_tell(me->oggz, me->reader->io_tell, me->reader);
+-  oggz_set_read_callback(me->oggz, -1, oggplay_callback_predetected, me);
++  if (me->oggz == NULL)
++    return E_OGGPLAY_OGGZ_UNHAPPY;
++
++  if (oggz_io_set_read(me->oggz, me->reader->io_read, me->reader) != 0)
++    return E_OGGPLAY_OGGZ_UNHAPPY;
++
++  if (oggz_io_set_seek(me->oggz, me->reader->io_seek, me->reader) != 0)
++    return E_OGGPLAY_OGGZ_UNHAPPY;
++
++  if (oggz_io_set_tell(me->oggz, me->reader->io_tell, me->reader) != 0)
++    return E_OGGPLAY_OGGZ_UNHAPPY;
++
++  if (oggz_set_read_callback(me->oggz, -1, oggplay_callback_predetected, me))
++    return E_OGGPLAY_OGGZ_UNHAPPY;
+ 
+   while (1) {
+ 
+@@ -131,15 +149,20 @@ oggplay_initialise(OggPlay *me, int block) {
+ OggPlay *
+ oggplay_open_with_reader(OggPlayReader *reader) {
+ 
+-  OggPlay *me = oggplay_new_with_reader(reader);
+-
++  OggPlay *me = NULL;
+   int r = E_OGGPLAY_TIMEOUT;
++
++  if ( (me = oggplay_new_with_reader(reader)) == NULL)
++    return NULL;
++
+   while (r == E_OGGPLAY_TIMEOUT) {
+     r = oggplay_initialise(me, 0);
+   }
+ 
+   if (r != E_OGGPLAY_OK) {
+-    free(me);
++    /* in case of error close the OggPlay handle */
++    oggplay_close(me);
++
+     return NULL;
+   }
+ 
+@@ -195,6 +218,7 @@ oggplay_set_callback_num_frames(OggPlay *me, int track, int frames) {
+   me->callback_period = me->decode_data[track]->granuleperiod * frames;
+   me->target = me->presentation_time + me->callback_period - 1;
+ 
++//  printf("targ: %lld, callback_per: %lld, prestime: %lld\n", me->target, me->callback_period,me->presentation_time );  
+ 
+   return E_OGGPLAY_OK;
+ 
+@@ -524,8 +548,10 @@ read_more_data:
+ 
+       /*
+        * ensure all tracks have their final data packet set to end_of_stream
++       * But skip doing this if we're shutting down --- me->buffer may not
++       * be in a safe state.
+        */
+-      if (me->buffer != NULL) {
++      if (me->buffer != NULL && !me->shutdown) {
+         oggplay_buffer_set_last_data(me, me->buffer);
+       }
+ 
+@@ -594,17 +620,21 @@ oggplay_close(OggPlay *me) {
+     me->reader->destroy(me->reader);
+   }
+ 
+-  for (i = 0; i < me->num_tracks; i++) {
+-    oggplay_callback_shutdown(me->decode_data[i]);
++
++  if (me->decode_data) {
++    for (i = 0; i < me->num_tracks; i++) {
++      oggplay_callback_shutdown(me->decode_data[i]);
++    }
+   }
+ 
+-  oggz_close(me->oggz);
++  if (me->oggz)
++    oggz_close(me->oggz);
+ 
+   if (me->buffer != NULL) {
+     oggplay_buffer_shutdown(me, me->buffer);
+   }
+ 
+-  free(me);
++  oggplay_free(me);
+ 
+   return E_OGGPLAY_OK;
+ }
+@@ -685,3 +715,4 @@ oggplay_media_finished_retrieving(OggPlay *me) {
+   return me->reader->finished_retrieving(me->reader);
+ 
+ }
++
+diff --git a/src/liboggplay/oggplay_buffer.c b/src/liboggplay/oggplay_buffer.c
+index e752f33..b116c7f 100644
+--- a/src/liboggplay/oggplay_buffer.c
++++ b/src/liboggplay/oggplay_buffer.c
+@@ -53,26 +53,43 @@
+ OggPlayBuffer *
+ oggplay_buffer_new_buffer(int size) {
+ 
+-  OggPlayBuffer *buffer = 0;
++  OggPlayBuffer *buffer = NULL;
+   if (size < 0) {
+     size = OGGPLAY_DEFAULT_BUFFER_SIZE;
+   }
+ 
+-  buffer = (OggPlayBuffer*)malloc(sizeof (OggPlayBuffer));
++  buffer = (OggPlayBuffer*)oggplay_malloc(sizeof (OggPlayBuffer));
+ 
+-  buffer->buffer_list = malloc(sizeof (void *) * size);
+-  memset(buffer->buffer_list, 0, sizeof (void *) * size);
+-  buffer->buffer_mirror = malloc(sizeof (void *) * size);
+-  memset(buffer->buffer_mirror, 0, sizeof (void *) * size);
++  if (buffer == NULL)
++    return NULL;
++
++  buffer->buffer_list = oggplay_calloc(size, sizeof (void *));
++  if (buffer->buffer_list == NULL)
++    goto error;
++
++  buffer->buffer_mirror = oggplay_calloc(size, sizeof (void *));
++  if (buffer->buffer_mirror == NULL)
++    goto error;
+ 
+   buffer->buffer_size = size;
+   buffer->last_filled = -1;
+   buffer->last_emptied = -1;
+ 
+-  SEM_CREATE(buffer->frame_sem, size);
++  if (SEM_CREATE(buffer->frame_sem, size) != 0)
++    goto error;
+ 
+   return buffer;
+ 
++error:
++  if (buffer->buffer_list != NULL)
++    oggplay_free (buffer->buffer_list);
++
++  if (buffer->buffer_mirror != NULL)
++    oggplay_free (buffer->buffer_mirror);
++
++  oggplay_free (buffer);
++
++  return NULL;
+ }
+ 
+ void
+@@ -87,16 +104,16 @@ oggplay_buffer_shutdown(OggPlay *me, volatile OggPlayBuffer *vbuffer) {
+     if (buffer->buffer_mirror[i] != NULL) {
+       OggPlayCallbackInfo *ti = (OggPlayCallbackInfo *)buffer->buffer_mirror[i];
+       for (j = 0; j < me->num_tracks; j++) {
+-        free((ti + j)->records);
++        oggplay_free((ti + j)->records);
+       }
+-      free(ti);
++      oggplay_free(ti);
+     }
+   }
+ 
+-  free(buffer->buffer_list);
+-  free(buffer->buffer_mirror);
++  oggplay_free(buffer->buffer_list);
++  oggplay_free(buffer->buffer_mirror);
+   SEM_CLOSE(buffer->frame_sem);
+-  free(buffer);
++  oggplay_free(buffer);
+ }
+ 
+ int
+@@ -147,6 +164,9 @@ oggplay_buffer_callback(OggPlay *me, int tracks,
+   OggPlayCallbackInfo * ptr = track_info[0];
+   int                   required;
+ 
++  if (me == NULL)
++    return -1;
++
+   buffer = (OggPlayBuffer *)me->buffer;
+ 
+   if (buffer == NULL) {
+@@ -190,9 +210,9 @@ oggplay_buffer_callback(OggPlay *me, int tracks,
+         /* free these here, because we couldn't free them in
+          * oggplay_callback_info_destroy for buffer mode
+          */
+-        free((ti + i)->records);
++        oggplay_free((ti + i)->records);
+       }
+-      free(ti);
++      oggplay_free(ti);
+       buffer->buffer_mirror[k] = NULL;
+     }
+   }
+@@ -200,7 +220,10 @@ oggplay_buffer_callback(OggPlay *me, int tracks,
+   /*
+    * replace the decode_data buffer for the next callback
+    */
+-  me->callback_info = (OggPlayCallbackInfo *)calloc(me->num_tracks, sizeof (OggPlayCallbackInfo));
++  me->callback_info = 
++    (OggPlayCallbackInfo *)oggplay_calloc(me->num_tracks, sizeof (OggPlayCallbackInfo));
++  if (me->callback_info == NULL)
++    return -1;
+ 
+   /*
+    * fill both mirror and list, mirror first to avoid getting inconsistencies
+@@ -256,7 +279,9 @@ oggplay_buffer_retrieve_next(OggPlay *me) {
+   next_item = (OggPlayCallbackInfo*)buffer->buffer_list[next_loc];
+   buffer->last_emptied = next_loc;
+ 
+-  return_val = malloc(sizeof (OggPlayCallbackInfo *) * me->num_tracks);
++  return_val = oggplay_calloc(me->num_tracks, sizeof (OggPlayCallbackInfo *));
++  if (return_val == NULL)
++    return NULL;
+ 
+   for (i = 0; i < me->num_tracks; i++) {
+     return_val[i] = next_item + i;
+@@ -289,7 +314,7 @@ oggplay_buffer_release(OggPlay *me, OggPlayCallbackInfo **track_info) {
+     return E_OGGPLAY_UNINITIALISED;
+   }
+ 
+-  free(track_info);
++  oggplay_free(track_info);
+ 
+   buffer->buffer_list[buffer->last_emptied] = NULL;
+ 
+@@ -317,7 +342,8 @@ oggplay_use_buffer(OggPlay *me, int size) {
+     return E_OGGPLAY_OK;
+   }
+ 
+-  me->buffer = oggplay_buffer_new_buffer(size);
++  if( (me->buffer = oggplay_buffer_new_buffer(size)) == NULL)
++    return E_OGGPLAY_OUT_OF_MEMORY;
+ 
+   /*
+    * if oggplay is already initialised, then prepare the buffer now
+@@ -334,6 +360,9 @@ oggplay_buffer_prepare(OggPlay *me) {
+ 
+   int i;
+ 
++  if (me == NULL)
++    return;
++
+   oggplay_set_data_callback_force(me, &oggplay_buffer_callback, NULL);
+ 
+   for (i = 0; i < me->num_tracks; i++) {
+diff --git a/src/liboggplay/oggplay_buffer.h b/src/liboggplay/oggplay_buffer.h
+index a356514..7a38976 100644
+--- a/src/liboggplay/oggplay_buffer.h
++++ b/src/liboggplay/oggplay_buffer.h
+@@ -39,6 +39,13 @@
+ #ifndef __OGGPLAY_BUFFER_H__
+ #define __OGGPLAY_BUFFER_H__
+ 
++/**
++ * Creates a new buffer with the given size. 
++ *
++ * @param size The number of frames the buffer can store. 
++ * @return A new OggPlayBuffer.
++ * @retval NULL in case of error. 
++ */
+ OggPlayBuffer *
+ oggplay_buffer_new_buffer(int size);
+ 
+diff --git a/src/liboggplay/oggplay_callback.c b/src/liboggplay/oggplay_callback.c
+index 6916838..5a4d1ba 100644
+--- a/src/liboggplay/oggplay_callback.c
++++ b/src/liboggplay/oggplay_callback.c
+@@ -315,6 +315,8 @@ oggplay_fish_sound_callback_floats(FishSound * fsound, float ** pcm,
+      */
+     oggplay_data_handle_audio_data(&(decoder->decoder), (short *)pcm, frames,
+               sizeof(float));
++
++      return FISH_SOUND_STOP_ERR;
+   }
+ 
+   return FISH_SOUND_CONTINUE;
+@@ -473,10 +475,17 @@ OggPlayCallbackFunctions callbacks[] = {
+ OggPlayDecode *
+ oggplay_initialise_decoder(OggPlay *me, int content_type, int serialno) {
+ 
+-  ogg_int64_t   num;
+-  ogg_int64_t   denom;
++  ogg_int64_t    num;
++  ogg_int64_t    denom;
++  OggPlayDecode *decoder = NULL;
++
++  if (me == NULL)
++    return NULL;
++
++  decoder = oggplay_malloc (callbacks[content_type].size);
+ 
+-  OggPlayDecode * decoder = malloc (callbacks[content_type].size);
++  if (decoder == NULL)
++    return NULL;
+ 
+   decoder->serialno = serialno;
+   decoder->content_type = content_type;
+@@ -538,8 +547,7 @@ oggplay_callback_shutdown(OggPlayDecode *decoder) {
+ 
+   oggplay_data_shutdown_list(decoder);
+ 
+-  free(decoder);
+-
++  oggplay_free(decoder);
+ }
+ 
+ 
+@@ -595,11 +603,20 @@ oggplay_callback_predetected (OGGZ *oggz, ogg_packet *op, long serialno,
+     }
+   }
+ 
+-  me->callback_info = realloc (me->callback_info,
++  me->callback_info = oggplay_realloc (me->callback_info,
+                   sizeof (OggPlayCallbackInfo) * ++me->num_tracks);
+-  me->decode_data = realloc (me->decode_data, sizeof (long) * me->num_tracks);
++  if (me->callback_info == NULL)
++    return -1;
++
++  me->decode_data = oggplay_realloc (me->decode_data, sizeof (long) * me->num_tracks);
++  if (me->decode_data == NULL)
++    return -1;
++
+   me->decode_data[me->num_tracks - 1] = oggplay_initialise_decoder(me,
+                                                       content_type, serialno);
++  if (me->decode_data[me->num_tracks - 1] == NULL)
++    return -1; 
++
+   /*me->decode_data->callback_info = me->callback_info + (me->num_tracks - 1);*/
+ 
+   /*
+diff --git a/src/liboggplay/oggplay_callback.h b/src/liboggplay/oggplay_callback.h
+index b6e1204..c6ddac1 100644
+--- a/src/liboggplay/oggplay_callback.h
++++ b/src/liboggplay/oggplay_callback.h
+@@ -45,6 +45,17 @@ oggplay_callback_predetected (OGGZ *oggz, ogg_packet *op, long serialno,
+ void
+ oggplay_process_leftover_packet(OggPlay *me);
+ 
++/**
++ * Create and initialise an OggPlayDecode handle.
++ *
++ *  
++ *
++ * @param me OggPlay 
++ * @param content_type 
++ * @param serialno
++ * @return A new OggPlayDecode handle
++ * @retval NULL in case of error.
++ */
+ OggPlayDecode *
+ oggplay_initialise_decoder(OggPlay *me, int content_type, int serialno);
+ 
+diff --git a/src/liboggplay/oggplay_callback_info.c b/src/liboggplay/oggplay_callback_info.c
+index 2363744..cbfb6ab 100644
+--- a/src/liboggplay/oggplay_callback_info.c
++++ b/src/liboggplay/oggplay_callback_info.c
+@@ -40,7 +40,7 @@
+ 
+ #define M(x)  ((x) >> 32) 
+ 
+-void _print_list(char *name, OggPlayDataHeader *p);
++extern void _print_list(char *name, OggPlayDataHeader *p);
+ 
+ int
+ oggplay_callback_info_prepare(OggPlay *me, OggPlayCallbackInfo ***info) {
+@@ -56,7 +56,9 @@ oggplay_callback_info_prepare(OggPlay *me, OggPlayCallbackInfo ***info) {
+   /*
+    * allocate the structure for return to the user
+    */
+-  (*info) = malloc (me->num_tracks * sizeof (OggPlayCallbackInfo *));
++  (*info) = oggplay_calloc (me->num_tracks, sizeof (OggPlayCallbackInfo *));
++  if ((*info) == NULL)
++    return -1;
+ 
+   /*
+    * fill in each active track.  Leave gaps for inactive tracks.
+@@ -128,7 +130,18 @@ oggplay_callback_info_prepare(OggPlay *me, OggPlayCallbackInfo ***info) {
+     }
+    
+     /* null-terminate the record list for the python interface */
+-    track_info->records = malloc ((count + 1) * sizeof (OggPlayDataHeader *));
++    track_info->records = oggplay_calloc ((count + 1), sizeof (OggPlayDataHeader *));
++    if (track_info->records == NULL)
++    {
++      for (i = 0; i < me->num_tracks; i++) {
++        if ((*info)[i]->records != NULL) 
++          oggplay_free ((*info)[i]->records);
++      }
++      oggplay_free (*info);
++      *info = NULL;
++      return -1;
++    }
++
+     track_info->records[count] = NULL;
+ 
+     track_info->available_records = count;
+@@ -270,18 +283,20 @@ oggplay_callback_info_prepare(OggPlay *me, OggPlayCallbackInfo ***info) {
+      * and callback creation
+      */
+     for (i = 0; i < me->num_tracks; i++) {
+-      if ((*info)[i]->records != NULL) free((*info)[i]->records);
++      if ((*info)[i]->records != NULL) 
++        oggplay_free((*info)[i]->records);
+     }
+-    free(*info);
++    oggplay_free(*info);
+     (*info) = NULL;
+ 
+   }
+ 
+   if (tcount == 0) {
+     for (i = 0; i < me->num_tracks; i++) {
+-      if ((*info)[i]->records != NULL) free((*info)[i]->records);
++      if ((*info)[i]->records != NULL) 
++        oggplay_free((*info)[i]->records);
+     }
+-    free(*info);
++    oggplay_free(*info);
+     (*info) = NULL;
+   }
+ 
+@@ -299,10 +314,10 @@ oggplay_callback_info_destroy(OggPlay *me, OggPlayCallbackInfo **info) {
+   for (i = 0; i < me->num_tracks; i++) {
+     p = info[i];
+     if (me->buffer == NULL && p->records != NULL)
+-      free(p->records);
++      oggplay_free(p->records);
+   }
+ 
+-  free(info);
++  oggplay_free(info);
+ 
+ }
+ 
+diff --git a/src/liboggplay/oggplay_data.c b/src/liboggplay/oggplay_data.c
+index 3055f0e..0bf7651 100644
+--- a/src/liboggplay/oggplay_data.c
++++ b/src/liboggplay/oggplay_data.c
+@@ -157,7 +157,7 @@ oggplay_data_add_to_list (OggPlayDecode *decode, OggPlayDataHeader *data) {
+       if (untimed->presentation_time >= decode->player->presentation_time) {
+         oggplay_data_add_to_list_front(decode, untimed);
+       } else {
+-        free(untimed);
++        oggplay_free(untimed);
+       }
+ 
+     }
+@@ -188,7 +188,7 @@ oggplay_data_free_list(OggPlayDataHeader *list) {
+   while (list != NULL) {
+     p = list;
+     list = list->next;
+-    free(p);
++    oggplay_free(p);
+   }
+ }
+ 
+@@ -236,13 +236,13 @@ oggplay_data_clean_list (OggPlayDecode *decode) {
+         decode->data_list = decode->data_list->next;
+         if (decode->data_list == NULL)
+           decode->end_of_data_list = NULL;
+-        free (header);
++        oggplay_free (header);
+         header = decode->data_list;
+       } else {
+         if (header->next == NULL)
+           decode->end_of_data_list = p;
+         p->next = header->next;
+-        free (header);
++        oggplay_free (header);
+         header = p->next;
+       }
+     } else {
+@@ -271,12 +271,15 @@ oggplay_data_handle_audio_data (OggPlayDecode *decode, void *data,
+       int samples, int samplesize) {
+ 
+   int                   num_channels;
+-  OggPlayAudioRecord  * record;
++  OggPlayAudioRecord  * record = NULL;
+ 
+   num_channels = ((OggPlayAudioDecode *)decode)->sound_info.channels;
+-  record = (OggPlayAudioRecord*)calloc(sizeof(OggPlayAudioRecord) +
++  record = (OggPlayAudioRecord*)oggplay_calloc(sizeof(OggPlayAudioRecord) +
+                   samples * samplesize * num_channels, 1);
+ 
++  if (record == NULL)
++    return;
++
+   oggplay_data_initialise_header(decode, &(record->header));
+ 
+   record->header.samples_in_record = samples;
+@@ -295,10 +298,14 @@ void
+ oggplay_data_handle_cmml_data(OggPlayDecode *decode, unsigned char *data,
+                 int size) {
+ 
+-  OggPlayTextRecord * record;
++  OggPlayTextRecord * record = NULL;
+ 
+   record =
+-      (OggPlayTextRecord*)calloc (sizeof(OggPlayTextRecord) + size + 1, 1);
++      (OggPlayTextRecord*)oggplay_calloc (sizeof(OggPlayTextRecord) + size + 1, 1);
++
++  if (record == NULL)
++    return;
++
+   oggplay_data_initialise_header(decode, &(record->header));
+ 
+   record->header.samples_in_record = 1;
+@@ -336,7 +343,11 @@ oggplay_data_handle_theora_frame (OggPlayTheoraDecode *decode,
+    * we need to set the output strides to the input widths because we are
+    * trying not to pass negative output stride issues on to the poor user.
+    */
+-  record = (OggPlayVideoRecord*)malloc (size);
++  record = (OggPlayVideoRecord*)oggplay_malloc (size);
++
++  if (record == NULL)
++    return;
++
+   record->header.samples_in_record = 1;
+   data = &(record->data);
+   oggplay_data_initialise_header((OggPlayDecode *)decode, &(record->header));
+@@ -379,9 +390,13 @@ oggplay_data_handle_kate_data(OggPlayKateDecode *decode, const kate_event *ev) {
+ 
+   // TODO: should be able to send the data rendered as YUV data, but just text for now
+ 
+-  OggPlayTextRecord * record;
++  OggPlayTextRecord * record = NULL;
++
++  record = (OggPlayTextRecord*)oggplay_calloc (sizeof(OggPlayTextRecord) + ev->len0, 1);
++  
++  if (record = NULL)
++    return;
+ 
+-  record = (OggPlayTextRecord*)calloc (sizeof(OggPlayTextRecord) + ev->len0, 1);
+   oggplay_data_initialise_header(&decode->decoder, &(record->header));
+ 
+   //record->header.presentation_time = (ogg_int64_t)(ev->start_time*1000);
+diff --git a/src/liboggplay/oggplay_file_reader.c b/src/liboggplay/oggplay_file_reader.c
+index 789049f..1ec0f42 100644
+--- a/src/liboggplay/oggplay_file_reader.c
++++ b/src/liboggplay/oggplay_file_reader.c
+@@ -76,7 +76,7 @@ oggplay_file_reader_destroy(OggPlayReader * opr) {
+   me = (OggPlayFileReader *)opr;
+ 
+   fclose(me->file);
+-  free(me);
++  oggplay_free(me);
+ 
+   return E_OGGPLAY_OK;
+ }
+@@ -135,7 +135,10 @@ oggplay_file_reader_io_tell(void * user_handle) {
+ OggPlayReader *
+ oggplay_file_reader_new(char *file_name) {
+ 
+-  OggPlayFileReader * me = malloc (sizeof (OggPlayFileReader));
++  OggPlayFileReader * me = oggplay_malloc (sizeof (OggPlayFileReader));
++
++  if (me == NULL)
++    return NULL;
+ 
+   me->current_position = 0;
+   me->file_name = file_name;
+diff --git a/src/liboggplay/oggplay_private.h b/src/liboggplay/oggplay_private.h
+index c4743e6..7e5b9de 100644
+--- a/src/liboggplay/oggplay_private.h
++++ b/src/liboggplay/oggplay_private.h
+@@ -240,47 +240,15 @@ typedef struct {
+   int size;
+ } OggPlayCallbackFunctions;
+ 
++/* Allocate and free dynamic memory used by ogg.
++ * By default they are the ones from stdlib */
++#define oggplay_malloc _ogg_malloc
++#define oggplay_calloc _ogg_calloc
++#define oggplay_realloc _ogg_realloc
++#define oggplay_free _ogg_free
++
+ #include "oggplay_callback.h"
+ #include "oggplay_data.h"
+ #include "oggplay_buffer.h"
+ 
+-#if 0
+-static inline void _free(void *x) {
+-  printf("%p\n", x);
+-  free(x);
+-}
+-
+-static inline void *_malloc(int s) {
+-  void *x;
+-  printf("%d ", s);
+-  x = malloc(s);
+-  printf("%p\n", x);
+-  return x;
+-}
+-
+-static inline void *_realloc(void *x, int s) {
+-  void *y;
+-  printf("%p %d ", x, s);
+-  y = realloc(x, s);
+-  printf("%p\n", y);
+-  return y;
+-}
+-
+-static inline void *_calloc(int n, int s) {
+-  void *x;
+-  printf("%d %d ", n, s);
+-  x = calloc(n, s);
+-  printf("%p\n", x);
+-  return x;
+-}
+-
+-#define free(x) {printf("FREE %s %d ", __FILE__, __LINE__); _free(x);}
+-#define malloc(s) (printf("MALLOC %s %d ", __FILE__, __LINE__), \
+-    _malloc(s))
+-#define realloc(x, s) (printf("REALLOC %s %d ", __FILE__, __LINE__), \
+-    _realloc(x, s))
+-#define calloc(n, s) (printf("CALLOC %s %d ", __FILE__, __LINE__),  \
+-    _calloc(n, s))
+-#endif
+-
+ #endif
+diff --git a/src/liboggplay/oggplay_seek.c b/src/liboggplay/oggplay_seek.c
+index 415ce0f..f93552f 100644
+--- a/src/liboggplay/oggplay_seek.c
++++ b/src/liboggplay/oggplay_seek.c
+@@ -87,13 +87,19 @@ oggplay_seek_cleanup(OggPlay* me, ogg_int64_t milliseconds)
+   OggPlayDataHeader  ** end_of_list_p;
+   int                   i;
+ 
++  if (me  == NULL)
++    return;
++
+   /*
+    * first, create a trash object to store the context that we want to
+    * delete but can't until the presentation thread is no longer using it -
+    * this will occur as soon as the thread calls oggplay_buffer_release_next
+    */
+ 
+-  trash = calloc(sizeof(OggPlaySeekTrash), 1);
++  trash = oggplay_calloc(1, sizeof(OggPlaySeekTrash));
++
++  if (trash == NULL)
++    return;
+ 
+   /*
+    * store the old buffer in it next.
+@@ -106,6 +112,11 @@ oggplay_seek_cleanup(OggPlay* me, ogg_int64_t milliseconds)
+    */
+   me->buffer = oggplay_buffer_new_buffer(me->buffer->buffer_size);
+ 
++  if (me->buffer == NULL)
++  {
++    return;
++  }
++
+   /*
+    * strip all of the data packets out of the streams and put them into the
+    * trash.  We can free the untimed packets immediately - they are USELESS
+@@ -152,12 +163,12 @@ oggplay_take_out_trash(OggPlay *me, OggPlaySeekTrash *trash) {
+     oggplay_buffer_shutdown(me, trash->old_buffer);
+     oggplay_data_free_list(trash->old_data);
+     if (p != NULL) {
+-      free(p);
++      oggplay_free(p);
+     }
+     p = trash;
+   }
+ 
+   if (p != NULL) {
+-    free(p);
++    oggplay_free(p);
+   }
+ }
+diff --git a/src/liboggplay/oggplay_tcp_reader.c b/src/liboggplay/oggplay_tcp_reader.c
+index c2d55fb..e40d2da 100644
+--- a/src/liboggplay/oggplay_tcp_reader.c
++++ b/src/liboggplay/oggplay_tcp_reader.c
+@@ -148,13 +148,21 @@ oggplay_create_socket() {
+   return sock;
+ }
+ 
+-/*
+- * this function guarantees it will return malloced versions of host and
++/**
++ * This function guarantees it will return malloced versions of host and
+  * path
++ *
++ * @param location Location of the Ogg content
++ * @param proxy The proxy if there's any.
++ * @param proxy_port The port of the proxy if there's any.
++ * @param host The host to connect to; using proxy if set. 
++ * @param port The port to connect to;
++ * @param path The path where the content resides on the server.  
++ * @retval -1 in case of error, 0 otherwise. 
+  */
+-void
++int
+ oggplay_hostname_and_path(char *location, char *proxy, int proxy_port,
+-                                  char **host, int *port, char **path) {
++                          char **host, int *port, char **path) {
+ 
+ 
+   char  * colon;
+@@ -163,10 +171,15 @@ oggplay_hostname_and_path(char *location, char *proxy, int proxy_port,
+ 
+   /* if we have a proxy installed this is all dead simple */
+   if (proxy != NULL) {
+-    *host = strdup(proxy);
++    if ((*host = strdup(proxy)) == NULL)
++      goto error;
++
+     *port = proxy_port;
+-    *path = strdup(location);
+-    return;
++
++    if ((*path = strdup(location)) == NULL)
++      goto error;
++
++    return 0;
+   }
+ 
+   /* find start_pos */
+@@ -181,10 +194,15 @@ oggplay_hostname_and_path(char *location, char *proxy, int proxy_port,
+    * if both are null, then just set the simple defaults and return
+    */
+   if (colon == NULL && slash == NULL) {
+-    *host = strdup(location);
++    if ((*host = strdup(location)) == NULL)
++      goto error;
++
+     *port = 80;
+-    *path = strdup("/");
+-    return;
++
++    if ((*path = strdup("/") == NULL))
++      goto error;
++
++    return 0;
+   }
+ 
+   /*
+@@ -208,16 +226,29 @@ oggplay_hostname_and_path(char *location, char *proxy, int proxy_port,
+     end_of_host = slash;
+   }
+ 
+-  *host = strdup(location);
++  if ((*host = strdup(location)) == NULL)
++    goto error;
++  
+   (*host)[end_of_host - location] = '\0';
+ 
+   if (slash == NULL) {
+-    *path = strdup("/");
+-    return;
++    if ((*path = strdup("/")) == NULL)
++      goto error;
++
++    return 0;
+   }
+ 
+-  *path = strdup(slash);
++  if ((*path = strdup(slash)) == NULL)
++    goto error;
++
++  return 0;
++
++error:
++  /* there has been an error while copying strings... */
++  if (*host != NULL)
++    oggplay_free(*host);
+ 
++  return -1;
+ }
+ 
+ OggPlayErrorCode
+@@ -303,8 +334,9 @@ oggplay_tcp_reader_initialise(OggPlayReader * opr, int block) {
+   /*
+    * Extract the host name and the path from the location.
+    */
+-  oggplay_hostname_and_path(me->location, me->proxy, me->proxy_port,
+-                              &host, &port, &path);
++  if (oggplay_hostname_and_path(me->location, me->proxy, me->proxy_port,
++                              &host, &port, &path) != 0)
++    return E_OGGPLAY_OUT_OF_MEMORY;
+ 
+ 
+   /*
+@@ -320,8 +352,8 @@ oggplay_tcp_reader_initialise(OggPlayReader * opr, int block) {
+ 
+   he = gethostbyname(host);
+ 
+-  free(host);
+-  free(path);
++  oggplay_free(host);
++  oggplay_free(path);
+ 
+   if (he == NULL) {
+     printf("Host not found\n");
+@@ -374,7 +406,10 @@ oggplay_tcp_reader_initialise(OggPlayReader * opr, int block) {
+     int found_http_response = 0;
+ 
+     if (me->buffer == NULL) {
+-      me->buffer = (unsigned char*)malloc(TCP_READER_MAX_IN_MEMORY);
++      me->buffer = (unsigned char*)oggplay_malloc(TCP_READER_MAX_IN_MEMORY);
++      if (me->buffer == NULL)
++        return E_OGGPLAY_OUT_OF_MEMORY;
++
+       me->buffer_size = TCP_READER_MAX_IN_MEMORY;
+       me->amount_in_memory = 0;
+     }
+@@ -514,12 +549,12 @@ oggplay_tcp_reader_destroy(OggPlayReader * opr) {
+ #endif
+   }
+ 
+-  free(me->buffer);
+-  free(me->location);
++  if (me->buffer != NULL) oggplay_free(me->buffer);
++  if (me->location != NULL) oggplay_free(me->location);
+   if (me->backing_store != NULL) {
+     fclose(me->backing_store);
+   }
+-  free(me);
++  oggplay_free(me);
+   return E_OGGPLAY_OK;
+ }
+ 
+@@ -661,14 +696,22 @@ oggplay_tcp_reader_io_tell(void * user_handle) {
+ OggPlayReader *
+ oggplay_tcp_reader_new(char *location, char *proxy, int proxy_port) {
+ 
+-  OggPlayTCPReader * me = (OggPlayTCPReader *)malloc (sizeof (OggPlayTCPReader));
++  OggPlayTCPReader * me = (OggPlayTCPReader *)oggplay_malloc (sizeof (OggPlayTCPReader));
++
++  if (me == NULL)
++    return NULL;
+ 
+   me->state = OTRS_UNINITIALISED;
+   me->socket = INVALID_SOCKET;
+   me->buffer = NULL;
+   me->buffer_size = 0;
+   me->current_position = 0;
+-  me->location = strdup(location);
++  /* if there's not enough memory to copy the URI cancel the initialisation */
++  if ( (me->location = strdup(location)) == NULL)
++  {
++    oggplay_tcp_reader_destroy ((OggPlayReader*)me);
++    return NULL;
++  }
+   me->amount_in_memory = 0;
+   me->backing_store = NULL;
+   me->stored_offset = 0;
new file mode 100644
--- /dev/null
+++ b/media/liboggplay/bug468281_r3864.patch
@@ -0,0 +1,23 @@
+commit b16896c4b4c77dcff5d77851e17ab2846372e935
+Author: oggk <oggk@8158c8cd-e7e1-0310-9fa4-c5954c97daef>
+Date:   Tue Feb 24 23:19:59 2009 +0000
+
+    fix crucially misplaced parenthesis
+    
+    
+    
+    git-svn-id: http://svn.annodex.net/liboggplay/trunk@3864 8158c8cd-e7e1-0310-9fa4-c5954c97daef
+
+diff --git a/src/liboggplay/oggplay_tcp_reader.c b/src/liboggplay/oggplay_tcp_reader.c
+index e40d2da..3becb6d 100644
+--- a/src/liboggplay/oggplay_tcp_reader.c
++++ b/src/liboggplay/oggplay_tcp_reader.c
+@@ -199,7 +199,7 @@ oggplay_hostname_and_path(char *location, char *proxy, int proxy_port,
+ 
+     *port = 80;
+ 
+-    if ((*path = strdup("/") == NULL))
++    if ((*path = strdup("/")) == NULL)
+       goto error;
+ 
+     return 0;
--- a/media/liboggplay/include/oggplay/oggplay.h
+++ b/media/liboggplay/include/oggplay/oggplay.h
@@ -51,30 +51,37 @@ typedef struct _OggPlay OggPlay;
 typedef struct _OggPlayCallbackInfo OggPlayCallbackInfo;
 typedef int (OggPlayDataCallback)(OggPlay *player, int num_records,
                 OggPlayCallbackInfo **records, void *user);
 
 #include <oggplay/oggplay_query.h>
 #include <oggplay/oggplay_callback_info.h>
 #include <oggplay/oggplay_tools.h>
 #include <oggplay/oggplay_seek.h>
-/*
-#include <oggplay/oggplay_retrieve.h>
-#include <oggplay/oggplay_cmml.h>
-*/
 
-OggPlay *
-oggplay_init(void);
-
-OggPlayErrorCode
-oggplay_set_reader(OggPlay *OS, OggPlayReader *OSR);
-
+/**
+ * Create an OggPlay handle associated with the given reader.
+ * The functions creates a new OggPlay handle and associates with
+ * the given OggPlayReader and initialises the buffer.
+ * 
+ *
+ * @param reader an OggPlayReader handle associated with the Ogg content
+ * @return A new OggPlay handle
+ * @retval NULL in case of error.
+ */
 OggPlay *
 oggplay_open_with_reader(OggPlayReader *reader);
 
+/**
+ * Create a new OggPlay handle associated with the given reader.
+ *
+ * \param reader OggPlayReader handle associated with the Ogg content
+ * \return A new OggPlay handle
+ * \retval NULL in case of error.
+ */
 OggPlay *
 oggplay_new_with_reader(OggPlayReader *reader);
 
 OggPlayErrorCode
 oggplay_initialise(OggPlay *me, int block);
 
 OggPlayErrorCode
 oggplay_set_source(OggPlay *OS, char *source);
@@ -123,16 +130,23 @@ OggPlayCallbackInfo **
 oggplay_buffer_retrieve_next(OggPlay *player);
 
 OggPlayErrorCode
 oggplay_buffer_release(OggPlay *player, OggPlayCallbackInfo **track_info);
 
 void
 oggplay_prepare_for_close(OggPlay *me);
 
+/**
+ * @brief Destroys the OggPlay handle along with the associated OggPlayReader
+ * and clears out the buffer and shuts down the callback function. 
+ *
+ * @param player an OggPlay handle
+ * @retval E_OGGPLAY_OK on success 
+ */
 OggPlayErrorCode
 oggplay_close(OggPlay *player);
 
 int
 oggplay_get_available(OggPlay *player);
 
 ogg_int64_t
 oggplay_get_duration(OggPlay * player);
--- a/media/liboggplay/include/oggplay/oggplay_enums.h
+++ b/media/liboggplay/include/oggplay/oggplay_enums.h
@@ -57,16 +57,17 @@ typedef enum OggPlayErrorCode {
   E_OGGPLAY_UNINITIALISED     = -12,
   E_OGGPLAY_CALLBACK_MODE     = -13,
   E_OGGPLAY_BUFFER_MODE       = -14,
   E_OGGPLAY_USER_INTERRUPT    = -15,
   E_OGGPLAY_SOCKET_ERROR      = -16,
   E_OGGPLAY_TIMEOUT           = -17,
   E_OGGPLAY_CANT_SEEK         = -18,
   E_OGGPLAY_NO_KATE_SUPPORT   = -19,
+  E_OGGPLAY_OUT_OF_MEMORY     = -20,
   E_OGGPLAY_NOTCHICKENPAYBACK = -777
 } OggPlayErrorCode;
 
 typedef enum OggPlayDataType {
   OGGPLAY_INACTIVE      = -1,
   OGGPLAY_YUV_VIDEO     = 0,
   OGGPLAY_SHORTS_AUDIO  = 1000,
   OGGPLAY_FLOATS_AUDIO  = 1001,
--- a/media/liboggplay/include/oggplay/oggplay_reader.h
+++ b/media/liboggplay/include/oggplay/oggplay_reader.h
@@ -58,15 +58,31 @@ typedef struct _OggPlayReader {
   int               (*finished_retrieving)(struct _OggPlayReader *me);
 
   /* low-level io functions for oggz */
   size_t            (*io_read)(void *user_handle, void *buf, size_t n);
   int               (*io_seek)(void *user_handle, long offset, int whence);
   long              (*io_tell)(void *user_handle);
 } OggPlayReader;
 
+/**
+ * Create and initialise an OggPlayReader for a given Ogg file.
+ * 
+ * @param filename The file to open
+ * @return A new OggPlayReader handle
+ * @retval NULL on error.
+ */
 OggPlayReader *
 oggplay_file_reader_new(char *filename);
 
+/**
+ * Create and initialise an OggPlayReader for an Ogg content at a given URI. 
+ *
+ * @param uri The URI to the Ogg file.
+ * @param proxy Proxy 
+ * @param proxy_port Proxy port.
+ * @return A new OggPlayReader handle
+ * @retval NULL on error.
+ */
 OggPlayReader *
 oggplay_tcp_reader_new(char *uri, char *proxy, int proxy_port);
 
 #endif
--- a/media/liboggplay/src/liboggplay/oggplay.c
+++ b/media/liboggplay/src/liboggplay/oggplay.c
@@ -43,17 +43,25 @@
 #include <string.h>
 #include <stdlib.h>
 
 #define OGGZ_READ_CHUNK_SIZE 8192
 
 OggPlay *
 oggplay_new_with_reader(OggPlayReader *reader) {
 
-  OggPlay * me = (OggPlay *)malloc(sizeof(OggPlay));
+  OggPlay * me = NULL;
+
+  /* check whether the reader is valid. */
+  if (reader == NULL)
+    return NULL;
+
+  me = (OggPlay *)oggplay_malloc (sizeof(OggPlay));
+  if (me == NULL)
+	  return NULL;
 
   me->reader = reader;
   me->decode_data = NULL;
   me->callback_info = NULL;
   me->num_tracks = 0;
   me->all_tracks_initialised = 0;
   me->callback_period = 0;
   me->callback = NULL;
@@ -89,20 +97,30 @@ oggplay_initialise(OggPlay *me, int bloc
   me->presentation_time = 0;
 
   /*
    * start to retrieve data, until we get all of the track info.  We need
    * to do this now so that the user can query us for this info before entering
    * the main loop
    */
   me->oggz = oggz_new(OGGZ_READ | OGGZ_AUTO);
-  oggz_io_set_read(me->oggz, me->reader->io_read, me->reader);
-  oggz_io_set_seek(me->oggz, me->reader->io_seek, me->reader);
-  oggz_io_set_tell(me->oggz, me->reader->io_tell, me->reader);
-  oggz_set_read_callback(me->oggz, -1, oggplay_callback_predetected, me);
+  if (me->oggz == NULL)
+    return E_OGGPLAY_OGGZ_UNHAPPY;
+
+  if (oggz_io_set_read(me->oggz, me->reader->io_read, me->reader) != 0)
+    return E_OGGPLAY_OGGZ_UNHAPPY;
+
+  if (oggz_io_set_seek(me->oggz, me->reader->io_seek, me->reader) != 0)
+    return E_OGGPLAY_OGGZ_UNHAPPY;
+
+  if (oggz_io_set_tell(me->oggz, me->reader->io_tell, me->reader) != 0)
+    return E_OGGPLAY_OGGZ_UNHAPPY;
+
+  if (oggz_set_read_callback(me->oggz, -1, oggplay_callback_predetected, me))
+    return E_OGGPLAY_OGGZ_UNHAPPY;
 
   while (1) {
 
     if (oggz_read(me->oggz, OGGZ_READ_CHUNK_SIZE) <= 0) {
       return E_OGGPLAY_BAD_INPUT;
     }
 
     if (me->all_tracks_initialised) {
@@ -126,25 +144,30 @@ oggplay_initialise(OggPlay *me, int bloc
 
   return E_OGGPLAY_OK;
 
 }
 
 OggPlay *
 oggplay_open_with_reader(OggPlayReader *reader) {
 
-  OggPlay *me = oggplay_new_with_reader(reader);
+  OggPlay *me = NULL;
+  int r = E_OGGPLAY_TIMEOUT;
 
-  int r = E_OGGPLAY_TIMEOUT;
+  if ( (me = oggplay_new_with_reader(reader)) == NULL)
+    return NULL;
+
   while (r == E_OGGPLAY_TIMEOUT) {
     r = oggplay_initialise(me, 0);
   }
 
   if (r != E_OGGPLAY_OK) {
-    free(me);
+    /* in case of error close the OggPlay handle */
+    oggplay_close(me);
+
     return NULL;
   }
 
   return me;
 }
 
 /*
  * API function to prevent bad input, and to prevent data callbacks being registered
@@ -190,16 +213,17 @@ oggplay_set_callback_num_frames(OggPlay 
 
   if (track < 0 || track >= me->num_tracks) {
     return E_OGGPLAY_BAD_TRACK;
   }
 
   me->callback_period = me->decode_data[track]->granuleperiod * frames;
   me->target = me->presentation_time + me->callback_period - 1;
 
+//  printf("targ: %lld, callback_per: %lld, prestime: %lld\n", me->target, me->callback_period,me->presentation_time );  
 
   return E_OGGPLAY_OK;
 
 }
 
 OggPlayErrorCode
 oggplay_set_offset(OggPlay *me, int track, ogg_int64_t offset) {
 
@@ -518,17 +542,17 @@ read_more_data:
       }
 
       if (info != NULL) {
         me->callback (me, num_records, info, me->callback_user_ptr);
         oggplay_callback_info_destroy(me, info);
       }
 
       /*
-       * ensure all tracks have their final data packet set to end_of_stream.
+       * ensure all tracks have their final data packet set to end_of_stream
        * But skip doing this if we're shutting down --- me->buffer may not
        * be in a safe state.
        */
       if (me->buffer != NULL && !me->shutdown) {
         oggplay_buffer_set_last_data(me, me->buffer);
       }
 
       return E_OGGPLAY_OK;
@@ -591,27 +615,31 @@ oggplay_close(OggPlay *me) {
   if (me == NULL) {
     return E_OGGPLAY_BAD_OGGPLAY;
   }
 
   if (me->reader != NULL) {
     me->reader->destroy(me->reader);
   }
 
-  for (i = 0; i < me->num_tracks; i++) {
-    oggplay_callback_shutdown(me->decode_data[i]);
+
+  if (me->decode_data) {
+    for (i = 0; i < me->num_tracks; i++) {
+      oggplay_callback_shutdown(me->decode_data[i]);
+    }
   }
 
-  oggz_close(me->oggz);
+  if (me->oggz)
+    oggz_close(me->oggz);
 
   if (me->buffer != NULL) {
     oggplay_buffer_shutdown(me, me->buffer);
   }
 
-  free(me);
+  oggplay_free(me);
 
   return E_OGGPLAY_OK;
 }
 
 /*
  * this function is required to release the frame_sem in the buffer, if
  * the buffer is being used.
  */
@@ -682,8 +710,9 @@ oggplay_media_finished_retrieving(OggPla
 
   if (me->reader == NULL) {
     return E_OGGPLAY_BAD_READER;
   }
 
   return me->reader->finished_retrieving(me->reader);
 
 }
+
--- a/media/liboggplay/src/liboggplay/oggplay_buffer.c
+++ b/media/liboggplay/src/liboggplay/oggplay_buffer.c
@@ -48,60 +48,77 @@
 
 /*
  * Call this function to initialise the oggplay lock-free buffer.  Do not use
  * the buffer and the callback together!
  */
 OggPlayBuffer *
 oggplay_buffer_new_buffer(int size) {
 
-  OggPlayBuffer *buffer = 0;
+  OggPlayBuffer *buffer = NULL;
   if (size < 0) {
     size = OGGPLAY_DEFAULT_BUFFER_SIZE;
   }
 
-  buffer = (OggPlayBuffer*)malloc(sizeof (OggPlayBuffer));
+  buffer = (OggPlayBuffer*)oggplay_malloc(sizeof (OggPlayBuffer));
+
+  if (buffer == NULL)
+    return NULL;
 
-  buffer->buffer_list = malloc(sizeof (void *) * size);
-  memset(buffer->buffer_list, 0, sizeof (void *) * size);
-  buffer->buffer_mirror = malloc(sizeof (void *) * size);
-  memset(buffer->buffer_mirror, 0, sizeof (void *) * size);
+  buffer->buffer_list = oggplay_calloc(size, sizeof (void *));
+  if (buffer->buffer_list == NULL)
+    goto error;
+
+  buffer->buffer_mirror = oggplay_calloc(size, sizeof (void *));
+  if (buffer->buffer_mirror == NULL)
+    goto error;
 
   buffer->buffer_size = size;
   buffer->last_filled = -1;
   buffer->last_emptied = -1;
 
-  SEM_CREATE(buffer->frame_sem, size);
+  if (SEM_CREATE(buffer->frame_sem, size) != 0)
+    goto error;
 
   return buffer;
 
+error:
+  if (buffer->buffer_list != NULL)
+    oggplay_free (buffer->buffer_list);
+
+  if (buffer->buffer_mirror != NULL)
+    oggplay_free (buffer->buffer_mirror);
+
+  oggplay_free (buffer);
+
+  return NULL;
 }
 
 void
 oggplay_buffer_shutdown(OggPlay *me, volatile OggPlayBuffer *vbuffer) {
 
   int i;
   int j;
 
   OggPlayBuffer *buffer = (OggPlayBuffer *)vbuffer;
 
   for (i = 0; i < buffer->buffer_size; i++) {
     if (buffer->buffer_mirror[i] != NULL) {
       OggPlayCallbackInfo *ti = (OggPlayCallbackInfo *)buffer->buffer_mirror[i];
       for (j = 0; j < me->num_tracks; j++) {
-        free((ti + j)->records);
+        oggplay_free((ti + j)->records);
       }
-      free(ti);
+      oggplay_free(ti);
     }
   }
 
-  free(buffer->buffer_list);
-  free(buffer->buffer_mirror);
+  oggplay_free(buffer->buffer_list);
+  oggplay_free(buffer->buffer_mirror);
   SEM_CLOSE(buffer->frame_sem);
-  free(buffer);
+  oggplay_free(buffer);
 }
 
 int
 oggplay_buffer_is_full(volatile OggPlayBuffer *buffer) {
 
   return
   (
     (buffer == NULL) || (
@@ -142,16 +159,19 @@ oggplay_buffer_callback(OggPlay *me, int
   int                   i;
   int                   j;
   int                   k;
   OggPlayDataHeader  ** headers;
   OggPlayBuffer       * buffer;
   OggPlayCallbackInfo * ptr = track_info[0];
   int                   required;
 
+  if (me == NULL)
+    return -1;
+
   buffer = (OggPlayBuffer *)me->buffer;
 
   if (buffer == NULL) {
     return -1;
   }
 
   SEM_WAIT(buffer->frame_sem);
 
@@ -185,27 +205,30 @@ oggplay_buffer_callback(OggPlay *me, int
         headers = oggplay_callback_info_get_headers(ti + i);
         required = oggplay_callback_info_get_required(ti + i);
         for (j = 0; j < required; j++) {
           oggplay_callback_info_unlock_item(headers[j]);
         }
         /* free these here, because we couldn't free them in
          * oggplay_callback_info_destroy for buffer mode
          */
-        free((ti + i)->records);
+        oggplay_free((ti + i)->records);
       }
-      free(ti);
+      oggplay_free(ti);
       buffer->buffer_mirror[k] = NULL;
     }
   }
 
   /*
    * replace the decode_data buffer for the next callback
    */
-  me->callback_info = (OggPlayCallbackInfo *)calloc(me->num_tracks, sizeof (OggPlayCallbackInfo));
+  me->callback_info = 
+    (OggPlayCallbackInfo *)oggplay_calloc(me->num_tracks, sizeof (OggPlayCallbackInfo));
+  if (me->callback_info == NULL)
+    return -1;
 
   /*
    * fill both mirror and list, mirror first to avoid getting inconsistencies
    */
 
   buffer->last_filled = WRAP_INC(buffer->last_filled, buffer->buffer_size);
 
   /*
@@ -251,17 +274,19 @@ oggplay_buffer_retrieve_next(OggPlay *me
 
   if (buffer->buffer_list[next_loc] == NULL) {
     return NULL;
   }
 
   next_item = (OggPlayCallbackInfo*)buffer->buffer_list[next_loc];
   buffer->last_emptied = next_loc;
 
-  return_val = malloc(sizeof (OggPlayCallbackInfo *) * me->num_tracks);
+  return_val = oggplay_calloc(me->num_tracks, sizeof (OggPlayCallbackInfo *));
+  if (return_val == NULL)
+    return NULL;
 
   for (i = 0; i < me->num_tracks; i++) {
     return_val[i] = next_item + i;
   }
 
   return return_val;
 
 }
@@ -284,17 +309,17 @@ oggplay_buffer_release(OggPlay *me, OggP
   if (buffer == NULL) {
     return E_OGGPLAY_CALLBACK_MODE;
   }
 
   if (buffer->buffer_list[buffer->last_emptied] == NULL) {
     return E_OGGPLAY_UNINITIALISED;
   }
 
-  free(track_info);
+  oggplay_free(track_info);
 
   buffer->buffer_list[buffer->last_emptied] = NULL;
 
   SEM_SIGNAL(buffer->frame_sem);
 
   return E_OGGPLAY_OK;
 
 }
@@ -312,33 +337,37 @@ oggplay_use_buffer(OggPlay *me, int size
 
   if (me->buffer != NULL) {
     /*
      * we should check sizes, and maybe clear and reallocate the buffer?
      */
     return E_OGGPLAY_OK;
   }
 
-  me->buffer = oggplay_buffer_new_buffer(size);
+  if( (me->buffer = oggplay_buffer_new_buffer(size)) == NULL)
+    return E_OGGPLAY_OUT_OF_MEMORY;
 
   /*
    * if oggplay is already initialised, then prepare the buffer now
    */
   if (me->all_tracks_initialised) {
     oggplay_buffer_prepare(me);
   }
 
   return E_OGGPLAY_OK;
 }
 
 void
 oggplay_buffer_prepare(OggPlay *me) {
 
   int i;
 
+  if (me == NULL)
+    return;
+
   oggplay_set_data_callback_force(me, &oggplay_buffer_callback, NULL);
 
   for (i = 0; i < me->num_tracks; i++) {
     if (oggplay_get_track_type(me, i) == OGGZ_CONTENT_THEORA) {
       oggplay_set_callback_num_frames(me, i, 1);
       break;
     }
   }
--- a/media/liboggplay/src/liboggplay/oggplay_buffer.h
+++ b/media/liboggplay/src/liboggplay/oggplay_buffer.h
@@ -34,16 +34,23 @@
  * oggplay_buffer.h
  *
  * Shane Stephens <shane.stephens@annodex.net>
  */
 
 #ifndef __OGGPLAY_BUFFER_H__
 #define __OGGPLAY_BUFFER_H__
 
+/**
+ * Creates a new buffer with the given size. 
+ *
+ * @param size The number of frames the buffer can store. 
+ * @return A new OggPlayBuffer.
+ * @retval NULL in case of error. 
+ */
 OggPlayBuffer *
 oggplay_buffer_new_buffer(int size);
 
 int
 oggplay_buffer_is_full(volatile OggPlayBuffer *buffer);
 
 void
 oggplay_buffer_shutdown(OggPlay *me, volatile OggPlayBuffer *buffer);
--- a/media/liboggplay/src/liboggplay/oggplay_callback.c
+++ b/media/liboggplay/src/liboggplay/oggplay_callback.c
@@ -310,16 +310,18 @@ oggplay_fish_sound_callback_floats(FishS
   {
 
 
     /*
      * store the frame
      */
     oggplay_data_handle_audio_data(&(decoder->decoder), (short *)pcm, frames,
               sizeof(float));
+
+      return FISH_SOUND_STOP_ERR;
   }
 
   return FISH_SOUND_CONTINUE;
 }
 
 void
 oggplay_init_audio (void * user_data) {
 
@@ -468,20 +470,27 @@ OggPlayCallbackFunctions callbacks[] = {
   {oggplay_init_kate, oggplay_callback_kate, oggplay_shutdown_kate,
         sizeof(OggPlayKateDecode)},          /* KATE */
   {NULL, NULL, NULL, sizeof(OggPlayDecode)}  /* UNKNOWN */
 };
 
 OggPlayDecode *
 oggplay_initialise_decoder(OggPlay *me, int content_type, int serialno) {
 
-  ogg_int64_t   num;
-  ogg_int64_t   denom;
+  ogg_int64_t    num;
+  ogg_int64_t    denom;
+  OggPlayDecode *decoder = NULL;
 
-  OggPlayDecode * decoder = malloc (callbacks[content_type].size);
+  if (me == NULL)
+    return NULL;
+
+  decoder = oggplay_malloc (callbacks[content_type].size);
+
+  if (decoder == NULL)
+    return NULL;
 
   decoder->serialno = serialno;
   decoder->content_type = content_type;
   decoder->content_type_name =
           oggz_stream_get_content_type (me->oggz, serialno);
   decoder->active = 1;
   decoder->final_granulepos = -1;
   decoder->player = me;
@@ -533,18 +542,17 @@ void
 oggplay_callback_shutdown(OggPlayDecode *decoder) {
 
   if (callbacks[decoder->content_type].shutdown != NULL) {
     callbacks[decoder->content_type].shutdown(decoder);
   }
 
   oggplay_data_shutdown_list(decoder);
 
-  free(decoder);
-
+  oggplay_free(decoder);
 }
 
 
 /*
  * this is the callback that is used before all track types have been
  * determined - i.e. at the beginning of an ogg bitstream or at the start
  * of a new chain
  */
@@ -590,21 +598,30 @@ oggplay_callback_predetected (OGGZ *oggz
        * destroy this callback
        */
       oggz_set_read_callback (me->oggz, -1, NULL, NULL);
 
       return 0;
     }
   }
 
-  me->callback_info = realloc (me->callback_info,
+  me->callback_info = oggplay_realloc (me->callback_info,
                   sizeof (OggPlayCallbackInfo) * ++me->num_tracks);
-  me->decode_data = realloc (me->decode_data, sizeof (long) * me->num_tracks);
+  if (me->callback_info == NULL)
+    return -1;
+
+  me->decode_data = oggplay_realloc (me->decode_data, sizeof (long) * me->num_tracks);
+  if (me->decode_data == NULL)
+    return -1;
+
   me->decode_data[me->num_tracks - 1] = oggplay_initialise_decoder(me,
                                                       content_type, serialno);
+  if (me->decode_data[me->num_tracks - 1] == NULL)
+    return -1; 
+
   /*me->decode_data->callback_info = me->callback_info + (me->num_tracks - 1);*/
 
   /*
    * call appropriate callback
    */
   if (callbacks[content_type].callback != NULL) {
     callbacks[content_type].callback(oggz, op, serialno,
                                           me->decode_data[me->num_tracks - 1]);
--- a/media/liboggplay/src/liboggplay/oggplay_callback.h
+++ b/media/liboggplay/src/liboggplay/oggplay_callback.h
@@ -40,16 +40,27 @@
 
 int
 oggplay_callback_predetected (OGGZ *oggz, ogg_packet *op, long serialno,
                 void *user_data);
 
 void
 oggplay_process_leftover_packet(OggPlay *me);
 
+/**
+ * Create and initialise an OggPlayDecode handle.
+ *
+ *  
+ *
+ * @param me OggPlay 
+ * @param content_type 
+ * @param serialno
+ * @return A new OggPlayDecode handle
+ * @retval NULL in case of error.
+ */
 OggPlayDecode *
 oggplay_initialise_decoder(OggPlay *me, int content_type, int serialno);
 
 int
 oggplay_callback_info_prepare(OggPlay *me, OggPlayCallbackInfo ***info);
 
 void
 oggplay_callback_info_destroy(OggPlay *me, OggPlayCallbackInfo **info);
--- a/media/liboggplay/src/liboggplay/oggplay_callback_info.c
+++ b/media/liboggplay/src/liboggplay/oggplay_callback_info.c
@@ -35,33 +35,35 @@
  * 
  * Shane Stephens <shane.stephens@annodex.net>
  */
 #include "oggplay_private.h"
 #include <stdlib.h>
 
 #define M(x)  ((x) >> 32) 
 
-void _print_list(char *name, OggPlayDataHeader *p);
+extern void _print_list(char *name, OggPlayDataHeader *p);
 
 int
 oggplay_callback_info_prepare(OggPlay *me, OggPlayCallbackInfo ***info) {
 
   int i;
   int tcount = 0;
   
   int         added_required_record   = me->num_tracks;
   ogg_int64_t diff;
   ogg_int64_t latest_first_record     = 0x0LL;
   //ogg_int64_t lpt = 0;
  
   /*
    * allocate the structure for return to the user
    */
-  (*info) = malloc (me->num_tracks * sizeof (OggPlayCallbackInfo *));
+  (*info) = oggplay_calloc (me->num_tracks, sizeof (OggPlayCallbackInfo *));
+  if ((*info) == NULL)
+    return -1;
 
   /*
    * fill in each active track.  Leave gaps for inactive tracks.
    */
   for (i = 0; i < me->num_tracks; i++) {
     OggPlayDecode       * track = me->decode_data[i];
     OggPlayCallbackInfo * track_info = me->callback_info + i;
     int                   count = 0;
@@ -123,17 +125,28 @@ oggplay_callback_info_prepare(OggPlay *m
         track_info->stream_info = track->stream_info;
       }
 
     } else {
       track_info->stream_info = OGGPLAY_STREAM_UNINITIALISED;
     }
    
     /* null-terminate the record list for the python interface */
-    track_info->records = malloc ((count + 1) * sizeof (OggPlayDataHeader *));
+    track_info->records = oggplay_calloc ((count + 1), sizeof (OggPlayDataHeader *));
+    if (track_info->records == NULL)
+    {
+      for (i = 0; i < me->num_tracks; i++) {
+        if ((*info)[i]->records != NULL) 
+          oggplay_free ((*info)[i]->records);
+      }
+      oggplay_free (*info);
+      *info = NULL;
+      return -1;
+    }
+
     track_info->records[count] = NULL;
 
     track_info->available_records = count;
     track_info->required_records = 0;
 
     track_info->data_type = track->decoded_type;
  
     count = 0;
@@ -265,28 +278,30 @@ oggplay_callback_info_prepare(OggPlay *m
      */
     me->presentation_time = me->target - me->callback_period;
 
     /*
      * indicate that we need to go through another round of fragment collection
      * and callback creation
      */
     for (i = 0; i < me->num_tracks; i++) {
-      if ((*info)[i]->records != NULL) free((*info)[i]->records);
+      if ((*info)[i]->records != NULL) 
+        oggplay_free((*info)[i]->records);
     }
-    free(*info);
+    oggplay_free(*info);
     (*info) = NULL;
 
   }
 
   if (tcount == 0) {
     for (i = 0; i < me->num_tracks; i++) {
-      if ((*info)[i]->records != NULL) free((*info)[i]->records);
+      if ((*info)[i]->records != NULL) 
+        oggplay_free((*info)[i]->records);
     }
-    free(*info);
+    oggplay_free(*info);
     (*info) = NULL;
   }
 
   return me->num_tracks;
   
 }
 
 
@@ -294,20 +309,20 @@ void
 oggplay_callback_info_destroy(OggPlay *me, OggPlayCallbackInfo **info) {
 
   int                   i;
   OggPlayCallbackInfo * p;
 
   for (i = 0; i < me->num_tracks; i++) {
     p = info[i];
     if (me->buffer == NULL && p->records != NULL)
-      free(p->records);
+      oggplay_free(p->records);
   }
 
-  free(info);
+  oggplay_free(info);
 
 }
 
 OggPlayDataType
 oggplay_callback_info_get_type(OggPlayCallbackInfo *info) {
 
   if (info == NULL) {
     return (OggPlayDataType)E_OGGPLAY_BAD_CALLBACK_INFO;
--- a/media/liboggplay/src/liboggplay/oggplay_data.c
+++ b/media/liboggplay/src/liboggplay/oggplay_data.c
@@ -152,17 +152,17 @@ oggplay_data_add_to_list (OggPlayDecode 
                 samples_in_next_in_list * decode->granuleperiod;
       untimed->presentation_time = presentation_time;
       decode->untimed_data_list = untimed->next;
       samples_in_next_in_list = untimed->samples_in_record;
 
       if (untimed->presentation_time >= decode->player->presentation_time) {
         oggplay_data_add_to_list_front(decode, untimed);
       } else {
-        free(untimed);
+        oggplay_free(untimed);
       }
 
     }
 
     oggplay_data_add_to_list_end(decode, data);
 
     /*
      * if the StreamInfo is still at uninitialised, then this is the first
@@ -183,17 +183,17 @@ oggplay_data_add_to_list (OggPlayDecode 
 
 void
 oggplay_data_free_list(OggPlayDataHeader *list) {
   OggPlayDataHeader *p;
 
   while (list != NULL) {
     p = list;
     list = list->next;
-    free(p);
+    oggplay_free(p);
   }
 }
 
 void
 oggplay_data_shutdown_list (OggPlayDecode *decode) {
 
   oggplay_data_free_list(decode->data_list);
   oggplay_data_free_list(decode->untimed_data_list);
@@ -231,23 +231,23 @@ oggplay_data_clean_list (OggPlayDecode *
       )
 
     )
     {
       if (p == NULL) {
         decode->data_list = decode->data_list->next;
         if (decode->data_list == NULL)
           decode->end_of_data_list = NULL;
-        free (header);
+        oggplay_free (header);
         header = decode->data_list;
       } else {
         if (header->next == NULL)
           decode->end_of_data_list = p;
         p->next = header->next;
-        free (header);
+        oggplay_free (header);
         header = p->next;
       }
     } else {
       p = header;
       header = header->next;
     }
   }
 }
@@ -266,22 +266,25 @@ oggplay_data_initialise_header (OggPlayD
 
 }
 
 void
 oggplay_data_handle_audio_data (OggPlayDecode *decode, void *data,
       int samples, int samplesize) {
 
   int                   num_channels;
-  OggPlayAudioRecord  * record;
+  OggPlayAudioRecord  * record = NULL;
 
   num_channels = ((OggPlayAudioDecode *)decode)->sound_info.channels;
-  record = (OggPlayAudioRecord*)calloc(sizeof(OggPlayAudioRecord) +
+  record = (OggPlayAudioRecord*)oggplay_calloc(sizeof(OggPlayAudioRecord) +
                   samples * samplesize * num_channels, 1);
 
+  if (record == NULL)
+    return;
+
   oggplay_data_initialise_header(decode, &(record->header));
 
   record->header.samples_in_record = samples;
 
   record->data = (void *)(record + 1);
 
   memcpy(record->data, data, samples * samplesize * num_channels);
   /*
@@ -290,20 +293,24 @@ oggplay_data_handle_audio_data (OggPlayD
   */
   oggplay_data_add_to_list(decode, &(record->header));
 }
 
 void
 oggplay_data_handle_cmml_data(OggPlayDecode *decode, unsigned char *data,
                 int size) {
 
-  OggPlayTextRecord * record;
+  OggPlayTextRecord * record = NULL;
 
   record =
-      (OggPlayTextRecord*)calloc (sizeof(OggPlayTextRecord) + size + 1, 1);
+      (OggPlayTextRecord*)oggplay_calloc (sizeof(OggPlayTextRecord) + size + 1, 1);
+
+  if (record == NULL)
+    return;
+
   oggplay_data_initialise_header(decode, &(record->header));
 
   record->header.samples_in_record = 1;
   record->data = (char *)(record + 1);
 
   memcpy(record->data, data, size);
   record->data[size] = '\0';
 
@@ -331,17 +338,21 @@ oggplay_data_handle_theora_frame (OggPla
     size += buffer->y_stride * buffer->y_height;
     size += buffer->uv_stride * buffer->uv_height * 2;
   }
 
   /*
    * we need to set the output strides to the input widths because we are
    * trying not to pass negative output stride issues on to the poor user.
    */
-  record = (OggPlayVideoRecord*)malloc (size);
+  record = (OggPlayVideoRecord*)oggplay_malloc (size);
+
+  if (record == NULL)
+    return;
+
   record->header.samples_in_record = 1;
   data = &(record->data);
   oggplay_data_initialise_header((OggPlayDecode *)decode, &(record->header));
 
   data->y = (unsigned char *)(record + 1);
   data->u = data->y + (decode->y_stride * decode->y_height);
   data->v = data->u + (decode->uv_stride * decode->uv_height);
 
@@ -374,19 +385,23 @@ oggplay_data_handle_theora_frame (OggPla
 }
 
 #ifdef HAVE_KATE
 void
 oggplay_data_handle_kate_data(OggPlayKateDecode *decode, const kate_event *ev) {
 
   // TODO: should be able to send the data rendered as YUV data, but just text for now
 
-  OggPlayTextRecord * record;
+  OggPlayTextRecord * record = NULL;
 
-  record = (OggPlayTextRecord*)calloc (sizeof(OggPlayTextRecord) + ev->len0, 1);
+  record = (OggPlayTextRecord*)oggplay_calloc (sizeof(OggPlayTextRecord) + ev->len0, 1);
+  
+  if (record = NULL)
+    return;
+
   oggplay_data_initialise_header(&decode->decoder, &(record->header));
 
   //record->header.presentation_time = (ogg_int64_t)(ev->start_time*1000);
   record->header.samples_in_record = (ev->end_time-ev->start_time)*1000;
   record->data = (char *)(record + 1);
 
   memcpy(record->data, ev->text, ev->len0);
 
--- a/media/liboggplay/src/liboggplay/oggplay_file_reader.c
+++ b/media/liboggplay/src/liboggplay/oggplay_file_reader.c
@@ -71,17 +71,17 @@ oggplay_file_reader_initialise(OggPlayRe
 OggPlayErrorCode
 oggplay_file_reader_destroy(OggPlayReader * opr) {
 
   OggPlayFileReader * me;
 
   me = (OggPlayFileReader *)opr;
 
   fclose(me->file);
-  free(me);
+  oggplay_free(me);
 
   return E_OGGPLAY_OK;
 }
 
 int
 oggplay_file_reader_available(OggPlayReader * opr, ogg_int64_t current_bytes,
     ogg_int64_t current_time) {
 
@@ -130,17 +130,20 @@ oggplay_file_reader_io_tell(void * user_
 
   return ftell(me->file);
 
 }
 
 OggPlayReader *
 oggplay_file_reader_new(char *file_name) {
 
-  OggPlayFileReader * me = malloc (sizeof (OggPlayFileReader));
+  OggPlayFileReader * me = oggplay_malloc (sizeof (OggPlayFileReader));
+
+  if (me == NULL)
+    return NULL;
 
   me->current_position = 0;
   me->file_name = file_name;
   me->file = NULL;
 
   me->functions.initialise = &oggplay_file_reader_initialise;
   me->functions.destroy = &oggplay_file_reader_destroy;
   me->functions.available = &oggplay_file_reader_available;
--- a/media/liboggplay/src/liboggplay/oggplay_private.h
+++ b/media/liboggplay/src/liboggplay/oggplay_private.h
@@ -239,52 +239,20 @@ oggplay_seek_cleanup(OggPlay *me, ogg_in
 typedef struct {
   void (*init)(void *user_data);
   int (*callback)(OGGZ * oggz, ogg_packet * op, long serialno,
                                                           void * user_data);
   void (*shutdown)(void *user_data);
   int size;
 } OggPlayCallbackFunctions;
 
+/* Allocate and free dynamic memory used by ogg.
+ * By default they are the ones from stdlib */
+#define oggplay_malloc _ogg_malloc
+#define oggplay_calloc _ogg_calloc
+#define oggplay_realloc _ogg_realloc
+#define oggplay_free _ogg_free
+
 #include "oggplay_callback.h"
 #include "oggplay_data.h"
 #include "oggplay_buffer.h"
 
-#if 0
-static inline void _free(void *x) {
-  printf("%p\n", x);
-  free(x);
-}
-
-static inline void *_malloc(int s) {
-  void *x;
-  printf("%d ", s);
-  x = malloc(s);
-  printf("%p\n", x);
-  return x;
-}
-
-static inline void *_realloc(void *x, int s) {
-  void *y;
-  printf("%p %d ", x, s);
-  y = realloc(x, s);
-  printf("%p\n", y);
-  return y;
-}
-
-static inline void *_calloc(int n, int s) {
-  void *x;
-  printf("%d %d ", n, s);
-  x = calloc(n, s);
-  printf("%p\n", x);
-  return x;
-}
-
-#define free(x) {printf("FREE %s %d ", __FILE__, __LINE__); _free(x);}
-#define malloc(s) (printf("MALLOC %s %d ", __FILE__, __LINE__), \
-    _malloc(s))
-#define realloc(x, s) (printf("REALLOC %s %d ", __FILE__, __LINE__), \
-    _realloc(x, s))
-#define calloc(n, s) (printf("CALLOC %s %d ", __FILE__, __LINE__),  \
-    _calloc(n, s))
 #endif
-
-#endif
--- a/media/liboggplay/src/liboggplay/oggplay_seek.c
+++ b/media/liboggplay/src/liboggplay/oggplay_seek.c
@@ -82,35 +82,46 @@ void
 oggplay_seek_cleanup(OggPlay* me, ogg_int64_t milliseconds)
 {
 
   OggPlaySeekTrash    * trash;
   OggPlaySeekTrash   ** p;
   OggPlayDataHeader  ** end_of_list_p;
   int                   i;
 
+  if (me  == NULL)
+    return;
+
   /*
    * first, create a trash object to store the context that we want to
    * delete but can't until the presentation thread is no longer using it -
    * this will occur as soon as the thread calls oggplay_buffer_release_next
    */
 
-  trash = calloc(sizeof(OggPlaySeekTrash), 1);
+  trash = oggplay_calloc(1, sizeof(OggPlaySeekTrash));
+
+  if (trash == NULL)
+    return;
 
   /*
    * store the old buffer in it next.
    */
   trash->old_buffer = (OggPlayBuffer *)me->buffer;
 
   /*
    * replace the buffer with a new one.  From here on, the presentation thread
    * will start using this buffer instead.
    */
   me->buffer = oggplay_buffer_new_buffer(me->buffer->buffer_size);
 
+  if (me->buffer == NULL)
+  {
+    return;
+  }
+
   /*
    * strip all of the data packets out of the streams and put them into the
    * trash.  We can free the untimed packets immediately - they are USELESS
    * SCUM OF THE EARTH (and also unreferenced by the buffer).
    */
   end_of_list_p = &trash->old_data;
   for (i = 0; i < me->num_tracks; i++) {
     OggPlayDecode *track = me->decode_data[i];
@@ -147,17 +158,17 @@ oggplay_take_out_trash(OggPlay *me, OggP
 
   OggPlaySeekTrash *p = NULL;
 
   for (; trash != NULL; trash = trash->next) {
 
     oggplay_buffer_shutdown(me, trash->old_buffer);
     oggplay_data_free_list(trash->old_data);
     if (p != NULL) {
-      free(p);
+      oggplay_free(p);
     }
     p = trash;
   }
 
   if (p != NULL) {
-    free(p);
+    oggplay_free(p);
   }
 }
--- a/media/liboggplay/src/liboggplay/oggplay_tcp_reader.c
+++ b/media/liboggplay/src/liboggplay/oggplay_tcp_reader.c
@@ -143,53 +143,71 @@ oggplay_create_socket() {
     WSACleanup();
 #endif
     return INVALID_SOCKET;
   }
 
   return sock;
 }
 
-/*
- * this function guarantees it will return malloced versions of host and
+/**
+ * This function guarantees it will return malloced versions of host and
  * path
+ *
+ * @param location Location of the Ogg content
+ * @param proxy The proxy if there's any.
+ * @param proxy_port The port of the proxy if there's any.
+ * @param host The host to connect to; using proxy if set. 
+ * @param port The port to connect to;
+ * @param path The path where the content resides on the server.  
+ * @retval -1 in case of error, 0 otherwise. 
  */
-void
+int
 oggplay_hostname_and_path(char *location, char *proxy, int proxy_port,
-                                  char **host, int *port, char **path) {
+                          char **host, int *port, char **path) {
 
 
   char  * colon;
   char  * slash;
   char  * end_of_host;
 
   /* if we have a proxy installed this is all dead simple */
   if (proxy != NULL) {
-    *host = strdup(proxy);
+    if ((*host = strdup(proxy)) == NULL)
+      goto error;
+
     *port = proxy_port;
-    *path = strdup(location);
-    return;
+
+    if ((*path = strdup(location)) == NULL)
+      goto error;
+
+    return 0;
   }
 
   /* find start_pos */
   if (strncmp(location, "http://", 7) == 0) {
     location += 7;
   }
 
   colon = strchr(location, ':');
   slash = strchr(location, '/');
 
   /*
    * if both are null, then just set the simple defaults and return
    */
   if (colon == NULL && slash == NULL) {
-    *host = strdup(location);
+    if ((*host = strdup(location)) == NULL)
+      goto error;
+
     *port = 80;
-    *path = strdup("/");
-    return;
+
+    if ((*path = strdup("/")) == NULL)
+      goto error;
+
+    return 0;
   }
 
   /*
    * if there's a slash and it's before colon, there's no port.  Hence, after
    * this code, the only time that there's a port is when colon is non-NULL
    */
   if (slash != NULL && colon > slash) {
     colon = NULL;
@@ -203,26 +221,39 @@ oggplay_hostname_and_path(char *location
   if (colon != NULL) {
     *port = (int)strtol(colon+1, NULL, 10);
     end_of_host = colon;
   } else {
     *port = 80;
     end_of_host = slash;
   }
 
-  *host = strdup(location);
+  if ((*host = strdup(location)) == NULL)
+    goto error;
+  
   (*host)[end_of_host - location] = '\0';
 
   if (slash == NULL) {
-    *path = strdup("/");
-    return;
+    if ((*path = strdup("/")) == NULL)
+      goto error;
+
+    return 0;
   }
 
-  *path = strdup(slash);
+  if ((*path = strdup(slash)) == NULL)
+    goto error;
+
+  return 0;
 
+error:
+  /* there has been an error while copying strings... */
+  if (*host != NULL)
+    oggplay_free(*host);
+
+  return -1;
 }
 
 OggPlayErrorCode
 oggplay_connect_to_host(SOCKET socket, struct sockaddr *addr) {
 
   ogg_int64_t           time_ref;
 
   START_TIMEOUT(time_ref);
@@ -298,35 +329,36 @@ oggplay_tcp_reader_initialise(OggPlayRea
     if (!oggplay_set_socket_blocking_state(me->socket, 0)) {
       return E_OGGPLAY_SOCKET_ERROR;
     }
   }
 
   /*
    * Extract the host name and the path from the location.
    */
-  oggplay_hostname_and_path(me->location, me->proxy, me->proxy_port,
-                              &host, &port, &path);
+  if (oggplay_hostname_and_path(me->location, me->proxy, me->proxy_port,
+                              &host, &port, &path) != 0)
+    return E_OGGPLAY_OUT_OF_MEMORY;
 
 
   /*
    * Prepare the HTTP request header now, so we can free all our
    * allocated memory before returning on any errors.
    */
   snprintf(http_request_header, 1024,
     "GET %s HTTP/1.0\n"
     "Host: %s\n"
     "User-Agent: AnnodexFirefoxPlugin/0.1\n"
     "Accept: */*\n"
     "Connection: Keep-Alive\n\n", path, host);
 
   he = gethostbyname(host);
 
-  free(host);
-  free(path);
+  oggplay_free(host);
+  oggplay_free(path);
 
   if (he == NULL) {
     printf("Host not found\n");
     return E_OGGPLAY_BAD_INPUT;
   }
   memcpy(&addr.sin_addr.s_addr, he->h_addr, he->h_length);
   addr.sin_family = AF_INET;
   addr.sin_port = htons(port);
@@ -369,17 +401,20 @@ oggplay_tcp_reader_initialise(OggPlayRea
   /*
    * Strip out the HTTP response by finding the first Ogg packet.
    */
   if (me->state == OTRS_SENT_HEADER) {
     int offset;
     int found_http_response = 0;
 
     if (me->buffer == NULL) {
-      me->buffer = (unsigned char*)malloc(TCP_READER_MAX_IN_MEMORY);
+      me->buffer = (unsigned char*)oggplay_malloc(TCP_READER_MAX_IN_MEMORY);
+      if (me->buffer == NULL)
+        return E_OGGPLAY_OUT_OF_MEMORY;
+
       me->buffer_size = TCP_READER_MAX_IN_MEMORY;
       me->amount_in_memory = 0;
     }
 
     while (1) {
       char *dpos;
 
       remaining = TCP_READER_MAX_IN_MEMORY - me->amount_in_memory - 1;
@@ -509,22 +544,22 @@ oggplay_tcp_reader_destroy(OggPlayReader
 #endif
     closesocket(me->socket);
     WSACleanup();
 #else
     close(me->socket);
 #endif
   }
 
-  free(me->buffer);
-  free(me->location);
+  if (me->buffer != NULL) oggplay_free(me->buffer);
+  if (me->location != NULL) oggplay_free(me->location);
   if (me->backing_store != NULL) {
     fclose(me->backing_store);
   }
-  free(me);
+  oggplay_free(me);
   return E_OGGPLAY_OK;
 }
 
 OggPlayErrorCode
 grab_some_data(OggPlayTCPReader *me, int block) {
 
   int remaining;
   int r;
@@ -656,24 +691,32 @@ oggplay_tcp_reader_io_tell(void * user_h
 
   return me->current_position;
 
 }
 
 OggPlayReader *
 oggplay_tcp_reader_new(char *location, char *proxy, int proxy_port) {
 
-  OggPlayTCPReader * me = (OggPlayTCPReader *)malloc (sizeof (OggPlayTCPReader));
+  OggPlayTCPReader * me = (OggPlayTCPReader *)oggplay_malloc (sizeof (OggPlayTCPReader));
+
+  if (me == NULL)
+    return NULL;
 
   me->state = OTRS_UNINITIALISED;
   me->socket = INVALID_SOCKET;
   me->buffer = NULL;
   me->buffer_size = 0;
   me->current_position = 0;
-  me->location = strdup(location);
+  /* if there's not enough memory to copy the URI cancel the initialisation */
+  if ( (me->location = strdup(location)) == NULL)
+  {
+    oggplay_tcp_reader_destroy ((OggPlayReader*)me);
+    return NULL;
+  }
   me->amount_in_memory = 0;
   me->backing_store = NULL;
   me->stored_offset = 0;
 
   me->proxy = proxy;
   me->proxy_port = proxy_port;
 
   me->functions.initialise = &oggplay_tcp_reader_initialise;
--- a/media/liboggplay/update.sh
+++ b/media/liboggplay/update.sh
@@ -33,9 +33,10 @@ cp $1/src/liboggplay/oggplay_query.c ./s
 sed s/\#include\ \"config_win32.h\"//g $1/src/liboggplay/oggplay_private.h >./src/liboggplay/oggplay_private.h1
 sed s/\#include\ \<config.h\>/\#ifdef\ WIN32\\n\#include\ \"config_win32.h\"\\n\#else\\n\#include\ \<config.h\>\\n\#endif/g ./src/liboggplay/oggplay_private.h1 >./src/liboggplay/oggplay_private.h
 rm ./src/liboggplay/oggplay_private.h1
 sed s/\#ifdef\ HAVE_INTTYPES_H/\#if\ HAVE_INTTYPES_H/g $1/src/liboggplay/oggplay_data.c >./src/liboggplay/oggplay_data.c
 cd ./src/liboggplay
 patch <../../yuv_disable_optimized.patch
 cd ../..
 patch -p3 <yuv2argb.patch
-patch -p3 <bug464007.patch
+patch -p1 <bug468281_r3863.patch 
+patch -p1 <bug468281_r3864.patch