Author: rolf
Date: 2007-11-01 07:27:03 -0500 (Thu, 01 Nov 2007)
New Revision: 88639

Modified:
   trunk/moon/src/asf/ChangeLog
   trunk/moon/src/asf/asf-ffmpeg.h
   trunk/moon/src/asf/asf.cpp
   trunk/moon/src/asf/asf.h
Log:
* asf.cpp, asf.h, asf-ffmpeg.h: Code cleanup, mostly moved
  implementation to the cpp file, some refactoring.

Modified: trunk/moon/src/asf/ChangeLog
===================================================================
--- trunk/moon/src/asf/ChangeLog        2007-11-01 11:41:03 UTC (rev 88638)
+++ trunk/moon/src/asf/ChangeLog        2007-11-01 12:27:03 UTC (rev 88639)
@@ -1,3 +1,8 @@
+2007-11-01  Rolf Bjarne Kvinge <[EMAIL PROTECTED]> 
+
+       * asf.cpp, asf.h, asf-ffmpeg.h: Code cleanup, mostly moved 
implementation to
+         the cpp file, some refactoring.
+
 2007-10-31  Rolf Bjarne Kvinge <[EMAIL PROTECTED]> 
 
        * asf.cpp, asf.cs, asf.h, asf-generated.cpp, asf-generated.h,

Modified: trunk/moon/src/asf/asf-ffmpeg.h
===================================================================
--- trunk/moon/src/asf/asf-ffmpeg.h     2007-11-01 11:41:03 UTC (rev 88638)
+++ trunk/moon/src/asf/asf-ffmpeg.h     2007-11-01 12:27:03 UTC (rev 88639)
@@ -144,7 +144,7 @@
                return result != 0; // Is this correct
        }
        
-       virtual bool Seek (size_t offset, int mode)
+       virtual bool SeekInternal (size_t offset, int mode)
        {
                //printf ("FFMPEGSource::Seek (%i, %i).\n", offset, mode);
                
@@ -153,6 +153,9 @@
                return result; // Is this correct?
        }
        
+       virtual bool Eof () { return url_feof (buffer); }
+       virtual bool CanSeek () { return true; }
+       
        virtual gint64 Position () 
        {
                return buffer ? (buffer->pos - (buffer->buf_end - 
buffer->buf_ptr)) : 0;

Modified: trunk/moon/src/asf/asf.cpp
===================================================================
--- trunk/moon/src/asf/asf.cpp  2007-11-01 11:41:03 UTC (rev 88638)
+++ trunk/moon/src/asf/asf.cpp  2007-11-01 12:27:03 UTC (rev 88639)
@@ -18,6 +18,19 @@
 {
        ASF_LOG ("ASFParser::ASFParser ('%s'), this: %p.\n", filename, this);
        source = new ASFFileSource (this, filename);
+       Initialize ();
+}
+
+ASFParser::ASFParser (ASFSource* src)
+{
+       ASF_LOG ("ASFParser::ASFParser ('%p'), this: %p.\n", src, this);
+       source = src;
+       Initialize ();
+}
+
+void
+ASFParser::Initialize ()
+{
        header = NULL;
        data = NULL;
        data_offset = 0;
@@ -35,6 +48,7 @@
 ASFParser::~ASFParser ()
 {
        ASF_LOG ("ASFParser::~ASFParser ().\n");
+       
        delete source;
        source = NULL;
                
@@ -154,8 +168,6 @@
        asf_error_correction_data ecd;
        asf_payload_parsing_information ppi;
        
-       //int64_t start_position = source->Position ();
-       
        if (!ecd.FillInAll (this))
                return false;
        
@@ -269,7 +281,7 @@
        }
        
        bool any_streams = false;
-       for (asf_dword i = 0; i < header->object_count; i++) {
+       for (guint32 i = 0; i < header->object_count; i++) {
                asf_object tmp;
                if (!source->Read (&tmp, sizeof (asf_object))) {
                        return false;
@@ -379,6 +391,122 @@
        g_free (err);
 }
 
+asf_stream_properties* 
+ASFParser::GetStream (gint32 stream_index)
+{
+       if (stream_index < 1 || stream_index > 127)
+               return NULL;
+               
+       return stream_properties [stream_index - 1];
+}
+
+void
+ASFParser::SetStream (gint32 stream_index, asf_stream_properties* stream)
+{
+       if (stream_index < 1 || stream_index > 127) {
+               printf ("ASFParser::SetStream (%i, %p): Invalid stream 
index.\n", stream_index, stream);
+               return;
+       }
+       stream_properties [stream_index - 1] = stream;
+}
+
+bool
+ASFParser::IsValidStream (gint32 stream_index)
+{
+       return GetStream (stream_index) != NULL;
+}
+
+guint64
+ASFParser::GetPacketOffset (gint32 packet_index)
+{
+       if (packet_index < 0 || (gint32) packet_index >= (gint32) 
file_properties->data_packet_count) {
+               AddError (g_strdup_printf ("ASFParser::GetPacketOffset (%i): 
Invalid packet index (there are %llu packets).", packet_index, 
file_properties->data_packet_count)); 
+               return 0;
+       }
+
+       return packet_offset + packet_index * file_properties->min_packet_size;
+}
+
+gint32
+ASFParser::GetPacketIndex (guint64 offset)
+{
+       if (offset < packet_offset)
+               return -1;
+       if (offset > (guint64) packet_offset_end)
+               return file_properties->data_packet_count;
+       return (offset - packet_offset) / file_properties->min_packet_size;
+}
+
+gint32
+ASFParser::GetPacketIndexOfPts (gint32 stream_number, gint64 pts)
+{
+       int result = 0;
+       ASFPacket* packet = NULL;
+       
+       // Read packets until we find the packet which has a pts
+       // greater than the one we're looking for.
+       
+       while (ReadPacket (packet, result)) {
+               gint64 current_pts = packet->GetPts (stream_number);
+               if (current_pts < 0) // Can't read pts for some reason.
+                       return -1;
+               if (current_pts > pts) // We've found the packet after the one 
we're looking for
+                       return result - 1; // return the previous one.
+               result++;
+       }
+       
+       return -1;
+}
+
+asf_header* 
+ASFParser::GetHeader ()
+{
+       return header;
+}
+
+asf_file_properties* 
+ASFParser::GetFileProperties ()
+{ 
+       return file_properties;
+}
+
+asf_object* 
+ASFParser::GetHeaderObject (const asf_guid* guid)
+{
+       int index = GetHeaderObjectIndex (guid);
+       if (index >= 0) {
+               return header_objects [index];
+       } else {
+               return NULL;
+       }
+}
+
+int 
+ASFParser::GetHeaderObjectIndex (const asf_guid* guid, int start)
+{
+       int i = start;
+       if (i < 0)
+               return -1;
+               
+       while (header_objects [i] != NULL) {
+               if (asf_guid_compare (guid, &header_objects [i]->id))
+                       return i;
+       
+               i++;
+       }
+       
+       return -1;
+}
+
+asf_object* 
+ASFParser::GetHeader (int index) 
+{
+       if (index < 0)
+               return NULL;
+       return header_objects [index];
+}
+
+
 /*
        ASFFileSource
 */
@@ -400,7 +528,7 @@
 }
 
 bool
-ASFFileSource::Seek (size_t offset, int mode)
+ASFFileSource::SeekInternal (size_t offset, int mode)
 {
        if (fseek (fd, offset, mode) != 0) {
                parser->AddError (g_strdup_printf ("Can't seek to offset %i 
with mode %i in '%s': %s.\n", offset, mode,  filename, strerror (errno)));
@@ -444,7 +572,34 @@
        return true;
 }
 
+/*
+       ASFSource
+*/
+
+ASFSource::ASFSource (ASFParser* asf_parser)
+{
+       parser = asf_parser;
+}
+
+ASFSource::~ASFSource ()
+{
+       parser = NULL;
+}
+
 bool
+ASFSource::Seek (size_t offset)
+{
+       return SeekInternal (offset, SEEK_CUR);
+}
+
+bool
+ASFSource::Seek (size_t offset, int mode)
+{
+       return SeekInternal (offset, mode);
+}
+
+
+bool
 ASFSource::Read (void* destination, size_t bytes)
 {
        bool result;
@@ -458,10 +613,10 @@
 }
 
 bool 
-ASFSource::ReadEncoded (int length, asf_dword* destination)
+ASFSource::ReadEncoded (gint32 length, guint32* destination)
 {
-       asf_word result2 = 0;
-       asf_byte result1 = 0;
+       guint16 result2 = 0;
+       guint8 result1 = 0;
        
        switch (length) {
        case 0x00: return true;
@@ -484,6 +639,68 @@
 }
 
 /*
+       ASFPacket
+*/
+
+ASFPacket::ASFPacket ()
+{
+       payloads = NULL;
+       index = -1;
+       position = -1;
+}
+
+ASFPacket::~ASFPacket ()
+{
+       delete payloads;
+       payloads = NULL;
+}
+
+gint32
+ASFPacket::GetPayloadCount ()
+{
+       if (!payloads)
+               return 0;
+       return payloads->get_number_of_payloads ();
+}
+
+asf_single_payload* 
+ASFPacket::GetPayload (gint32 index /* 0 based */)
+{
+       if (index >= 0 && index < GetPayloadCount ())
+               return payloads->payloads [index];
+               
+       return NULL;
+}
+
+gint64
+ASFPacket::GetPts (gint32 stream_number)
+{
+       if (!payloads)
+               return -1;
+       
+       asf_single_payload* first = GetFirstPayload (stream_number);
+       if (!first)
+               return -1;
+               
+       return first->get_presentation_time ();
+}
+
+asf_single_payload* 
+ASFPacket::GetFirstPayload (gint32 stream_number /* 1 - 127 */)
+{
+       if (!payloads)
+               return NULL;
+               
+       int index = 0;
+       while (payloads->payloads [index] != NULL) {
+               if (payloads->payloads [index]->stream_number == stream_number)
+                       return payloads->payloads [index];
+               index++;
+       }
+       return NULL;
+}
+
+/*
        ASFFrameReader
 */
 
@@ -493,14 +710,12 @@
        first = NULL;
        last = NULL;
        size = 0;
-       is_key_frame = true;
-       pts = 0;
-       stream_number = 0;
        payloads = NULL;
        current_packet_index = CanSeek () ? 0 : -1;
        
        payloads_size = 0;
        payloads = NULL;
+       eof = false;
 }
 
 ASFFrameReader::~ASFFrameReader ()
@@ -516,6 +731,32 @@
 }
 
 bool
+ASFFrameReader::ResizeList (gint32 size)
+{
+       asf_single_payload** new_list = NULL;
+       
+       if (payloads_size >= size && size > 0)
+               return true;
+       
+       // Allocate a new list
+       new_list = (asf_single_payload**) parser->Malloc (sizeof 
(asf_single_payload*) * (size + 1));
+       
+       if (new_list == NULL) {
+               return false;
+       }
+       
+       if (payloads != NULL) {
+               // Copy the old list to the new one
+               memcpy (new_list, payloads, payloads_size * sizeof 
(asf_single_payload*));
+               g_free (payloads);
+       }
+       payloads = new_list;
+       payloads_size = size;
+       
+       return true;
+}
+
+bool
 ASFFrameReader::Seek (gint32 stream_number, gint64 pts)
 {
        ASF_LOG ("ASFFrameReader::Seek (%i, %llu).\n", stream_number, pts);
@@ -565,13 +806,12 @@
        // Most streams has at least once a media object spanning two payloads.
        // so we allocate space for two (+ NULL at the end).
        if (payloads == NULL) {
-               payloads_size = 2;
-               payloads = (asf_single_payload**) parser->Malloc (sizeof 
(asf_single_payload*) * (payloads_size + 1));
-               if (payloads == NULL) {
+               if (!ResizeList (2)) {
                        parser->AddError ("Data corruption in packets.");
                        return false;
                }
        } else {
+               // Free all old payloads, they belong to the previous frame.
                int i = -1;
                while (payloads [++i] != NULL) {
                        delete payloads [i];
@@ -587,25 +827,24 @@
        }
        
        gint32 payload_count = 1;
+       guint32 media_object_number = 0;
+       ASFFrameReaderData* current = NULL;
+       gint32 stream_number = 0;
+       
        payloads [0] = first->payload;
-       guint32 media_object_number = payloads [0]->media_object_number;
+       first->payload = NULL;
+       media_object_number = payloads [0]->media_object_number;
        size = payloads [0]->payload_data_length;
-       is_key_frame = payloads [0]->is_key_frame;
-       pts = payloads [0]->get_presentation_time ();
-       stream_number = payloads [0]->stream_number;
+       stream_number = StreamNumber ();
        
        ASF_LOG ("ASFFrameReader::Advance (): frame data: size = %lld, key = 
%s, pts = %llu, stream# = %i.\n", 
-               size, is_key_frame ? "true" : "false", pts, stream_number);
+               size, IsKeyFrame () ? "true" : "false", Pts (), StreamNumber 
());
        
-       bool end = false;
-       ASFFrameReaderData *current = first->next;
+       current = first->next;
        
-       ASFFrameReaderData* tmp2 = first;
-       Remove (tmp2);
-       tmp2->payload = NULL;
-       delete tmp2;
+       Remove (first);
        
-       while (!end) {
+       while (true) {
                // Loop through payloads until we find a payload with the same 
stream number.
                // if the media number is different, no more payloads in the 
current frame,
                // otherwise add the payload to the current frame's payloads 
and continue looping.
@@ -613,8 +852,7 @@
                        // We went past the end, read another packet to get 
more data.
                        current = last; // go back to the last element.
                        if (!ReadMore ()) {
-                               end = true; // No more data, we've reached the 
end
-                               break;
+                               return true; // No more data, we've reached the 
end
                        } else {
                                if (current == NULL) {
                                        // There was no elements before reading 
more, our next element is the first one
@@ -625,39 +863,28 @@
                        }
                }
                
-               if (end)
-                       break;
-               
                if (current->payload->stream_number == stream_number) {
-                       if (current->payload->media_object_number == 
media_object_number) {
-                               // Add the payload to the current frame's 
payloads
-                               payload_count++;
-                               size += current->payload->payload_data_length;
-                               if (payload_count > payloads_size) {
-                                       gint32 new_size = payload_count + 3;
-                                       asf_single_payload** new_payloads = 
NULL;
-                                       new_payloads = (asf_single_payload**) 
g_try_realloc (payloads, sizeof (asf_single_payload*) * (new_size + 1));
-                                       if (new_payloads == NULL) {
-                                               parser->AddError ("Out of 
memory.");
-                                               return false;
-                                       }
-                                       payloads = new_payloads;
-                                       memset (payloads + payloads_size, 0, 
sizeof (asf_single_payload*) * (new_size - payloads_size + 1));
-                                       payloads_size = new_size;
-                               }
-                               payloads [payload_count - 1] = current->payload;
-                               
-                               // Remove it from the queue
-                               ASFFrameReaderData* tmp = current;
-                               current = current->prev;
-                               
-                               tmp->payload = NULL;
-                               Remove (tmp);
-                               delete tmp;
-                       } else {
+                       if (current->payload->media_object_number != 
media_object_number) {
                                // We've found the end of the current frame's 
payloads
-                               end = true;
+                               return true;
                        }
+                       
+                       // Still in the current frame, so
+                       // add the payload to the current frame's payloads
+                       payload_count++;
+                       size += current->payload->payload_data_length;
+                       if (payload_count > payloads_size) {
+                               if (!ResizeList (payload_count + 3)) {
+                                       return false;
+                               }
+                       }
+                       payloads [payload_count - 1] = current->payload;
+                       current->payload = NULL;
+                       
+                       // Remove it from the queue
+                       ASFFrameReaderData* tmp = current;
+                       current = current->prev;
+                       Remove (tmp);
                }
                
                if (current != NULL)
@@ -670,10 +897,10 @@
 bool
 ASFFrameReader::ReadMore ()
 {
+       ASF_LOG ("ASFFrameReader::ReadMore ().\n");
+       
        ASFPacket* packet = new ASFPacket ();
        
-       ASF_LOG ("ASFFrameReader::ReadMore ().\n");
-       
        if (!parser->ReadPacket (packet, current_packet_index)) {
                ASF_LOG ("ASFFrameReader::ReadMore (): could not read more 
packets.\n");
                delete packet;
@@ -713,7 +940,6 @@
        if (payloads == NULL)
                return false;
 
-       //void* start = destination;
        gint32 i = -1;
        while (payloads [++i] != NULL) {
                memcpy (destination, payloads [i]->payload_data, payloads 
[i]->payload_data_length);
@@ -723,3 +949,38 @@
        return true;
 }
 
+void 
+ASFFrameReader::RemoveAll ()
+{
+       ASFFrameReaderData* current = first, *next = NULL;
+       while (current != NULL) {
+               next = current->next;
+               delete current;
+               current = next;
+       }
+       first = NULL;
+       last = NULL;
+}
+
+void
+ASFFrameReader::Remove (ASFFrameReaderData* data)
+{
+       if (data->prev != NULL)
+               data->prev->next = data->next;
+       
+       if (data->next != NULL)
+               data->next->prev = data->prev;
+               
+       if (data == first)
+               first = data->next;
+               
+       if (data == last)
+               last = data->prev;
+
+       delete data;
+}
+
+       
+       
+       
+       

Modified: trunk/moon/src/asf/asf.h
===================================================================
--- trunk/moon/src/asf/asf.h    2007-11-01 11:41:03 UTC (rev 88638)
+++ trunk/moon/src/asf/asf.h    2007-11-01 12:27:03 UTC (rev 88639)
@@ -53,44 +53,28 @@
 #include "asf-debug.h"
 
 class ASFSource {
+protected:
+       virtual bool ReadInternal (void* destination, size_t bytes) = 0;
+       virtual bool SeekInternal (size_t offset, int mode) = 0;
+       
 public: 
-       virtual ~ASFSource ()
-       {
-               this->parser = NULL;
-       }
+       ASFSource (ASFParser* parser);
+       virtual ~ASFSource ();
        
        // Reads the number of the specified encoded length (0-3)
        // encoded length 3 = read 4 bytes, rest equals encoded length and 
#bytes
        // into the destionation.
-       bool ReadEncoded (int encoded_length, asf_dword* destionation); 
-       virtual bool Read (void* destination, size_t bytes);
-protected:
-       virtual bool ReadInternal (void* destination, size_t bytes) = 0;
-public:
-       virtual bool Seek (size_t offset, int mode) = 0;
-       virtual bool Seek (size_t offset)
-       {
-               return Seek (offset, SEEK_CUR);
-       }
-       virtual bool Skip (int bytes)
-       {
-               //asf_byte dummy;
-               //printf ("ASFSource::Skip (%i).\n", bytes);
-               return Seek (bytes);
-               /*while (bytes > 0) {
-                       if (!Read (&dummy, 1))
-                               return false;
-                       bytes--;
-               }
-               return true;*/
-       }
-       virtual gint64 Position () = 0;
-protected:
-       ASFSource (ASFParser* parser)
-       {
-               this->parser = parser;
-       }
-public:
+       bool ReadEncoded (gint32 encoded_length, guint32* destionation);        
+       
+       bool Read (void* destination, size_t bytes); // Reads the requested 
number of bytes into the destination
+
+       bool Seek (size_t offset); // Seeks to the offset from the current 
position
+       bool Seek (size_t offset, int mode); // Seeks to the offset, with the 
specified mode (SEEK_CUR, SEEK_END, SEEK_SET)
+       
+       virtual gint64 Position () = 0; // Returns the position within the 
source (may not apply if the source is not seekable)
+       virtual bool CanSeek () = 0;
+       virtual bool Eof () = 0;
+       
        ASFParser* parser;
 };
 
@@ -99,11 +83,15 @@
        ASFFileSource (ASFParser* parser, const char* filename);
        virtual ~ASFFileSource ();
        
+       virtual gint64 Position () { return fd == NULL ? 0 : ftell (fd); }
+       virtual bool CanSeek () { return true; }
+       virtual bool Eof () { return fd == NULL ? false : feof (fd) != 0; }
+       
        const char* GetFileName () { return filename; }
        
+protected:
        virtual bool ReadInternal (void* destination, size_t bytes);
-       virtual bool Seek (size_t offset, int mode);
-       virtual gint64 Position () { return fd == NULL ? 0 : ftell (fd); }
+       virtual bool SeekInternal (size_t offset, int mode);
        
 private:
        char* filename;
@@ -112,62 +100,17 @@
 
 class ASFPacket {
 public:
-       ASFPacket ()
-       {
-               payloads = NULL;
-               index = 0;
-       }
+       ASFPacket ();   
+       virtual ~ASFPacket ();
        
-       virtual ~ASFPacket ()
-       {
-               delete payloads;
-               payloads = NULL;
-       }
+       gint32 GetPayloadCount (); // Returns the number of payloads in this 
packet.
+       asf_single_payload* GetPayload (gint32 index /* 0 based */);
+       gint64 GetPts (gint32 stream_number /* 1 - 127 */); // Gets the pts of 
the first payload.
+       asf_single_payload* GetFirstPayload (gint32 stream_number /* 1 - 127 
*/); // Gets the index first payload of the specified stream.
        
-       gint32 GetPayloadCount ()
-       {
-               if (!payloads)
-                       return 0;
-               return payloads->get_number_of_payloads ();
-       }
-       
-       asf_single_payload* GetPayload (gint32 index /* 0 based */)
-       {
-               if (index >= 0 && index < GetPayloadCount ())
-                       return payloads->payloads [index];
-                       
-               return NULL;
-       }
-       
-       // Gets the pts of the first payload.
-       int64_t GetPts (gint32 stream_number)
-       {
-               if (!payloads)
-                       return -1;
-               
-               asf_single_payload* first = GetFirstPayload (stream_number);
-               if (!first)
-                       return -1;
-                       
-               return first->get_presentation_time ();
-       }
-       
-       asf_single_payload* GetFirstPayload (gint32 stream_number)
-       {
-               if (!payloads)
-                       return NULL;
-                       
-               int index = 0;
-               while (payloads->payloads [index] != NULL) {
-                       if (payloads->payloads [index]->stream_number == 
stream_number)
-                               return payloads->payloads [index];
-                       index++;
-               }
-               return NULL;
-       }
-       
-       asf_multiple_payloads* payloads;
-       
+       asf_multiple_payloads* payloads; // The payloads in this packet
+
+private:
        gint32 index; // The index of this packet. -1 if not known.
        gint64 position; // The position of this packet. -1 if not known.       
        
 };
@@ -190,6 +133,32 @@
        }
 };
 
+/*
+       The data in an ASF file has the following structure:
+               Data
+                       Packets
+                               Payload(s)
+                                       Chunks of Media objects
+       
+       The problem is that one chunk of "Media object data" can span several 
payloads (and packets),
+       and the pieces may come unordered, like this:
+       
+       - first 25% of media object #1 for stream #1
+       - first 25% of media object #1 for stream #2
+       - first 25% of media object #1 for stream #3
+       - middle 50% of media object #1 for stream #2
+       - last 75% of media object #1 for stream #1
+       => we have now all the data for the first media object of stream #1
+       
+       This class implements a reader that allows the consumer to just call 
Advance() and then get the all data
+       for each "Media object" (here called "Frame", since it's shorter, and 
in general it corresponds
+       to a frame of video/audio, etc, even though the ASF spec states that it 
can be just about anything)
+       in one convenient Write () call.
+       
+       We keep reading payloads until we have all the data for a frame, the 
payloads currently not wanted are 
+       kept in a queue until the next Advance ().
+*/
+
 class ASFFrameReader {
 public:
        ASFFrameReader (ASFParser* parser);
@@ -202,6 +171,7 @@
        bool Seek (gint32 stream_number, gint64 pts);
        
        // Advance to the next frame
+       // Returns false if unsuccessful (if due to no more data, eof is set, 
otherwise some error occurred)
        bool Advance ();
        
        // Write the frame's data to a the destination
@@ -209,9 +179,9 @@
        
        // Information about the current frame
        guint64 Size () { return size; }
-       bool IsKeyFrame () { return is_key_frame; }
-       gint64 Pts () { return pts; }
-       gint32 StreamNumber () { return stream_number; }
+       bool IsKeyFrame () { return (payloads_size > 0 && payloads [0] != NULL) 
? payloads [0]->is_key_frame : false; }
+       gint64 Pts () { return (payloads_size > 0 && payloads [0] != NULL) ? 
payloads [0]->get_presentation_time () : 0; }
+       gint32 StreamNumber () { return (payloads_size > 0 && payloads [0] != 
NULL) ? payloads [0]->stream_number : 0; }
        
 private:
        ASFParser* parser;
@@ -228,189 +198,87 @@
        
        // Information about the current frame.
        guint64 size;
-       bool is_key_frame;
-       gint64 pts;
-       gint32 stream_number;
        
-       // Reads another packet and fills data at the end 
-       bool ReadMore ();
+       bool eof;
        
-       void RemoveAll ()
-       {
-               ASFFrameReaderData* current = first, *next = NULL;
-               while (current != NULL) {
-                       next = current->next;
-                       delete current;
-                       current = next;
-               }
-               first = NULL;
-               last = NULL;
-       }
-       
-       void Remove (ASFFrameReaderData* data)
-       {
-               if (data->prev != NULL)
-                       data->prev->next = data->next;
-               
-               if (data->next != NULL)
-                       data->next->prev = data->prev;
-                       
-               if (data == first)
-                       first = data->next;
-                       
-               if (data == last)
-                       last = data->prev;
-                       
-               //delete data;
-       }
+       bool ResizeList (gint32 size); // Resizes the list of payloads to the 
requested size. 
+       bool ReadMore (); // Reads another packet and stuffs the payloads into 
our queue 
+       void RemoveAll (); // Deletes the entire queue of payloads (and deletes 
every element)
+       void Remove (ASFFrameReaderData* data); // Unlinks the payload from the 
queue and deletes it.
 };
 
 class ASFParser {
+private:
+       void Initialize ();
+       bool ReadData ();
+       asf_object* ReadObject (asf_object* guid);
+       void SetStream (gint32 stream_index, asf_stream_properties* stream);
+
 public:
        ASFParser (const char* filename);
+       ASFParser (ASFSource* source);
        virtual ~ASFParser ();
        
-       bool ReadHeader ();
-       bool ReadData ();
-       // Reads the packet at the current position.
-       bool ReadPacket (ASFPacket* packet);
+       bool ReadHeader (); // Reads the header of the asf file.
+       bool ReadPacket (ASFPacket* packet); // Reads the packet at the current 
position.
        // Seeks to the packet index (as long as the packet index >= 0), then 
reads it.
        // If the packet index is < 0, then just read at the current position
        bool ReadPacket (ASFPacket* packet, gint32 packet_index); 
        
-       asf_object* ReadObject (asf_object* guid);
-       const char* GetLastError ();
        bool VerifyHeaderDataSize (guint64 size); // Verifies that the 
requested size is a size that can be inside the header.
        void* Malloc (gint32 size); // Allocates the requested memory and 
verifies that the size can actually be contained within the header. Reports an 
Out of Memory error if the memory can't be allocated, and returns NULL
        void* MallocVerified (gint32 size); // Allocates the requested memory 
(no size checking), reports an Out of Memory error if the memory can't be 
allocated, and returns NULL
+
+       // Error handling
+       const char* GetLastError (); // Returns the last error (NULL if no 
errors have been reported)
        void AddError (const char* err); // Makes a copy of the provided error 
string.
-       void AddError (char* err); // Frees the provided error string.
+       void AddError (char* err); // Frees the provided error string (allows 
you to do things like: AddError (g_strdup_printf ("error #%i", 2)))
 
-       // Stream index from 1 to 127
-       asf_stream_properties* GetStream (gint32 stream_index)
-       {
-               if (stream_index < 1 || stream_index > 127)
-                       return NULL;
-                       
-               return stream_properties [stream_index - 1];
-       }
+       // Stream index: valid values range from 1 to 127
+       // If the stream_index doesn't specify a valid stream (for whatever 
reason), NULL is returned.
+       asf_stream_properties* GetStream (gint32 stream_index);
        
-       void SetStream (gint32 stream_index, asf_stream_properties* stream)
-       {
-               if (stream_index < 1 || stream_index > 127) {
-                       printf ("ASFParser::SetStream (%i, %p): Invalid stream 
index.\n", stream_index, stream);
-                       return;
-               }
-               stream_properties [stream_index - 1] = stream;
-       }
+       // Checks if the stream_index (range 1 - 127) is a valid stream index 
in the asf file.
+       bool IsValidStream (gint32 stream_index);
        
-       bool IsValidStream (gint32 stream_index)
-       {
-               return GetStream (stream_index) != NULL;
-       }
-       
        // Returns 0 on failure, otherwise the offset of the packet index.
-       guint64 GetPacketOffset (gint32 packet_index)
-       {
-               if (packet_index < 0 || (gint32) packet_index >= (gint32) 
file_properties->data_packet_count) {
-                       AddError (g_strdup_printf ("ASFParser::GetPacketOffset 
(%i): Invalid packet index (there are %llu packets).", packet_index, 
file_properties->data_packet_count)); 
-                       return 0;
-               }
-
-               return packet_offset + packet_index * 
file_properties->min_packet_size;
-       }
+       guint64 GetPacketOffset (gint32 packet_index);
        
-       gint32 GetPacketIndex (guint64 offset)
-       {
-               if (offset < packet_offset)
-                       return -1;
-               if (offset > (guint64) packet_offset_end)
-                       return file_properties->data_packet_count;
-               return (offset - packet_offset) / 
file_properties->min_packet_size;
-       }
+       gint32 GetPacketIndex (guint64 offset);
+       // Searches the header objects for the specified guid
+       // returns -1 if nothing is found.
+       int GetHeaderObjectIndex (const asf_guid* guid, int start = 0);
        
        // Returns the packet index where the desired pts is found.
        // Returns -1 on failure.
-       gint32 GetPacketIndexOfPts (gint32 stream_number, gint64 pts)
-       {
-               int result = 0;
-               ASFPacket* packet = NULL;
-               
-               // Read packets until we find the packet which has a pts
-               // greater than the one we're looking for.
-               
-               while (ReadPacket (packet, result)) {
-                       gint64 current_pts = packet->GetPts (stream_number);
-                       if (current_pts < 0) // Can't read pts for some reason.
-                               return -1;
-                       if (current_pts > pts) // We've found the packet after 
the one we're looking for
-                               return result - 1; // return the previous one.
-                       result++;
-               }
-               
-               return -1;
-       }
-       
-       asf_header* GetHeader ()
-       {
-               return header;
-       }
-       
-       asf_file_properties* GetFileProperties ()
-       { 
-               return file_properties;
-       }
-       
-       asf_object* GetHeaderObject (const asf_guid* guid)
-       {
-               int index = GetHeaderObjectIndex (guid);
-               if (index >= 0) {
-                       return header_objects [index];
-               } else {
-                       return NULL;
-               }
-       }
+       gint32 GetPacketIndexOfPts (gint32 stream_number, gint64 pts);
 
-       // Searches the header objects for the specified guid
-       // returns -1 if nothing is found.
-       int GetHeaderObjectIndex (const asf_guid* guid, int start = 0)
-       {
-               int i = start;
-               if (i < 0)
-                       return -1;
-                       
-               while (header_objects [i] != NULL) {
-                       if (asf_guid_compare (guid, &header_objects [i]->id))
-                               return i;
-               
-                       i++;
-               }
-               
-               return -1;
-       }
+       // Field accessors
        
-       asf_object* GetHeader (int index) 
-       {
-               if (index < 0)
-                       return NULL;
-               return header_objects [index];
-       }
+       asf_header* GetHeader ();
+       asf_object* GetHeader (int index);
+       asf_file_properties* GetFileProperties ();
+       asf_object* GetHeaderObject (const asf_guid* guid);
 
-       ASFSource* source;
        
-       asf_object** header_objects;
+       // The following fields are available only after ReadHeader is called.
+       
        asf_header* header;
-       asf_data* data;
+       asf_object** header_objects;
        asf_file_properties* file_properties;
        asf_header_extension* header_extension;
        asf_stream_properties* stream_properties [127];
        asf_marker* marker;
        asf_script_command* script_command;
        
+       asf_data* data;
        guint64 data_offset; // location of data object
        guint64 packet_offset; // location of the beginning of the first packet
        guint64 packet_offset_end; // location of the end of the last packet
+
+       ASFSource* source; // The source used to read data.
        
+private:
        GSList* errors;
 };
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to