Author: rolf
Date: 2008-02-04 09:35:53 -0500 (Mon, 04 Feb 2008)
New Revision: 94745

Modified:
   trunk/moon/src/asf/ChangeLog
   trunk/moon/src/asf/asf.cpp
   trunk/moon/src/asf/asf.h
Log:
* asf.cpp, asf.h: Make packet index a uint64. When adding an index
  entry, cap the packet index to the size of the index (fixes a
  crash).

Modified: trunk/moon/src/asf/ChangeLog
===================================================================
--- trunk/moon/src/asf/ChangeLog        2008-02-04 14:02:14 UTC (rev 94744)
+++ trunk/moon/src/asf/ChangeLog        2008-02-04 14:35:53 UTC (rev 94745)
@@ -1,3 +1,8 @@
+2008-02-04  Rolf Bjarne Kvinge <[EMAIL PROTECTED]> 
+
+       * asf.cpp, asf.h: Make packet index a uint64. When adding an index 
entry,
+         cap the packet index to the size of the index (fixes a crash).
+
 2008-02-01  Rolf Bjarne Kvinge <[EMAIL PROTECTED]> 
 
        * asf.cpp: Initialize key_frames_only.

Modified: trunk/moon/src/asf/asf.cpp
===================================================================
--- trunk/moon/src/asf/asf.cpp  2008-02-04 14:02:14 UTC (rev 94744)
+++ trunk/moon/src/asf/asf.cpp  2008-02-04 14:35:53 UTC (rev 94745)
@@ -7,7 +7,12 @@
  *
  * See the LICENSE file included with the distribution for details.
  */
+
+#define __STDC_LIMIT_MACROS
+
 #include <config.h>
+#include <stdint.h>
+
 #include "asf.h"
 #include "../debug.h"
 #include "../clock.h"
@@ -485,9 +490,9 @@
 }
 
 int64_t
-ASFParser::GetPacketOffset (int packet_index)
+ASFParser::GetPacketOffset (uint64_t packet_index)
 {
-       if (packet_index < 0 || packet_index >= (int) 
file_properties->data_packet_count) {
+       if (packet_index < 0 || packet_index >= 
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;
        }
@@ -496,19 +501,19 @@
        return packet_offset + packet_index * file_properties->min_packet_size;
 }
 
-int
+uint64_t
 ASFParser::GetPacketIndex (int64_t offset)
 {
        if (offset < packet_offset)
-               return -1;
+               return 0;
        
        if (offset > packet_offset_end)
-               return file_properties->data_packet_count;
+               return file_properties->data_packet_count - 1;
        
        return (offset - packet_offset) / file_properties->min_packet_size;
 }
 
-int
+uint64_t
 ASFParser::GetPacketIndexOfPts (int stream_id, uint64_t pts)
 {
        ASFPacket *packet = NULL;
@@ -751,7 +756,7 @@
 bool
 ASFFrameReader::Eof ()
 {
-       return (uint32_t) current_packet_index >= parser->GetPacketCount ();
+       return current_packet_index >= parser->GetPacketCount ();
 }
 
 bool
@@ -768,7 +773,7 @@
 }
 
 void
-ASFFrameReader::AddFrameIndex ()
+ASFFrameReader::AddFrameIndex (uint64_t packet_index)
 {
        // No need to create an index if we can't seek.
        if (!CanSeek ())
@@ -801,7 +806,7 @@
                }
        }
        
-       int k = current_packet_index;
+       int k = MAX (packet_index, index_size - 1);
        uint64_t current_start = index [k].start_pts;
        index [k].start_pts = MIN (index [k].start_pts, Pts ());
        index [k].end_pts = MAX (index [k].end_pts, Pts ());
@@ -903,8 +908,8 @@
        bool estimate;
        MediaResult result;
        
-       int32_t start_pi = GetPacketIndexOfPts (pts, &estimate);
-       int32_t kf_packet_index = -1; // The last packet index which had a key 
frame and pts below the requested one
+       uint64_t start_pi = GetPacketIndexOfPts (pts, &estimate);
+       uint64_t kf_packet_index = UINT64_MAX; // The last packet index which 
had a key frame and pts below the requested one
        uint64_t kf_pts = 0; // The last key frame with pts below the requested 
one
        
        first_pts = 0;
@@ -923,7 +928,7 @@
                ASF_LOG ("ASFFrameReader::Seek (%d, %llu): Checking pts: %llu, 
last_packet_index = %i, key_frame_pts = %llu\n", 
                        stream_number, pts, Pts (), kf_packet_index, kf_pts);
                if (Pts () > pts) {
-                       if (kf_packet_index == -1 && start_pi > 0) {
+                       if (kf_packet_index == UINT64_MAX && start_pi > 0) {
                                // We haven't found a key frame yet, go back to 
one packet before the first one we tried and try again
                                current_packet_index = --start_pi;
                                RemoveAll ();
@@ -947,7 +952,7 @@
        // Don't return any frames before the pts we seeked to.
        key_frames_only = true;
        first_pts = kf_pts; 
-       current_packet_index = MAX (0, kf_packet_index);
+       current_packet_index = (kf_packet_index == UINT64_MAX) ? 0 : 
kf_packet_index;
        RemoveAll ();
 
        ASF_LOG ("ASFFrameReader::Seek (%d, %llu): Seeked to packet index %i 
with first pts %llu\n", stream_number, pts, current_packet_index, first_pts);
@@ -964,6 +969,7 @@
        int payload_count = 0;
        uint32_t media_object_number = 0;
        uint64_t current_pts = 0;
+       uint64_t first_packet_index = 0; // The packet index where the frame 
starts.
        ASFFrameReaderData* current = NULL;
        
        ASF_LOG ("ASFFrameReader::Advance ().\n");
@@ -1073,6 +1079,7 @@
                        
                        key_frames_only = false;                        
                        media_object_number = payload->media_object_number;
+                       first_packet_index = current_packet_index - 1;
                        
                        // add the payload to the current frame's payloads
                        payload_count++;
@@ -1116,7 +1123,7 @@
                goto start;
        }
        
-       AddFrameIndex ();
+       AddFrameIndex (first_packet_index);
 
        return result;
 }
@@ -1127,19 +1134,19 @@
        return parser->GetPacketOffset (MIN (parser->GetPacketCount () - 1, 
GetPacketIndexOfPts (pts, estimate) + 1));
 }
 
-int32_t
+uint64_t
 ASFFrameReader::GetPacketIndexOfPts (uint64_t pts, bool *estimate)
 {
        //printf ("ASFFrameReader::GetPacketIndexOfPts (%llu, %p)\n", pts, 
estimate);
        
-       int64_t average = 0; // average duration per packet
        int32_t counter = 0;
-       int32_t last_good_pi = 0;
+       uint64_t average = 0; // average duration per packet
+       uint64_t last_good_pi = 0;
        uint64_t last_good_pts = 0;
        uint64_t duration = 0;
        uint64_t total_duration = 0;
-       int32_t result = 0;
-       int32_t packet_index = 0;
+       uint64_t result = 0;
+       uint64_t packet_index = 0;
        
        *estimate = false;
        
@@ -1192,7 +1199,7 @@
        }
        
        result = MAX (0, result);
-       result = MIN (result, (int64_t) parser->GetPacketCount () - 1);
+       result = MIN (result, MIN (0, parser->GetPacketCount () - 1));
        
        //printf ("ASFFrameReader::GetPacketIndexOfPts (%llu, %p): Final 
position: %lld of pi: %i. Total packets: %llu, total duration: %llu\n", pts, 
estimate, parser->GetPacketOffset (pi), pi, parser->GetFileProperties 
()->data_packet_count, parser->GetFileProperties ()->play_duration);
        return result;

Modified: trunk/moon/src/asf/asf.h
===================================================================
--- trunk/moon/src/asf/asf.h    2008-02-04 14:02:14 UTC (rev 94744)
+++ trunk/moon/src/asf/asf.h    2008-02-04 14:35:53 UTC (rev 94745)
@@ -173,7 +173,7 @@
        bool key_frames_only;
        int stream_number; // The stream this reader is reading for 
        
-       int32_t current_packet_index; // The index of the next packet that will 
be read when ReadMore is called.
+       uint64_t current_packet_index; // The index of the next packet that 
will be read when ReadMore is called.
        int32_t script_command_stream_index;
        
        // The queue of payloads we've built.
@@ -228,10 +228,10 @@
        int32_t FrameSearch (uint64_t pts);
 
        int64_t GetPositionOfPts (uint64_t pts, bool *estimate);
-       int32_t GetPacketIndexOfPts (uint64_t pts, bool *estimate);
+       uint64_t GetPacketIndexOfPts (uint64_t pts, bool *estimate);
 
        // Adds the current frame to the index.
-       void AddFrameIndex ();
+       void AddFrameIndex (uint64_t packet_index);
        bool IsAudio ();
        bool IsAudio (int stream);
 };
@@ -293,18 +293,17 @@
        int GetSequentialStreamNumber (int stream_index);
        
        // Returns 0 on failure, otherwise the offset of the packet index.
-       int64_t GetPacketOffset (int packet_index);
+       int64_t GetPacketOffset (uint64_t packet_index);
        
        // Returns the index of the packet at the specified offset (from the 
beginning of the file)
-       int GetPacketIndex (int64_t offset);
+       uint64_t GetPacketIndex (int64_t 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.
-       int GetPacketIndexOfPts (int stream_id, uint64_t pts);
+       uint64_t GetPacketIndexOfPts (int stream_id, uint64_t pts);
        
        // The number of packets in the stream (0 if unknown).
        uint64_t GetPacketCount ();

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

Reply via email to