Merge "Hook up remaining CookieManager methods"
diff --git a/api/current.xml b/api/current.xml
index 1ca51c2..79a56b4 100644
--- a/api/current.xml
+++ b/api/current.xml
@@ -113419,7 +113419,7 @@
  extends="java.lang.Object"
  abstract="false"
  static="false"
- final="false"
+ final="true"
  deprecated="not deprecated"
  visibility="public"
 >
@@ -113510,7 +113510,7 @@
  extends="java.lang.Object"
  abstract="false"
  static="false"
- final="false"
+ final="true"
  deprecated="not deprecated"
  visibility="public"
 >
@@ -113790,273 +113790,6 @@
 >
 </field>
 </class>
-<class name="NdefTag"
- extends="android.nfc.Tag"
- abstract="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<implements name="android.os.Parcelable">
-</implements>
-<method name="createMockNdefTag"
- return="android.nfc.NdefTag"
- abstract="false"
- native="false"
- synchronized="false"
- static="true"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="id" type="byte[]">
-</parameter>
-<parameter name="rawTargets" type="java.lang.String[]">
-</parameter>
-<parameter name="pollBytes" type="byte[]">
-</parameter>
-<parameter name="activationBytes" type="byte[]">
-</parameter>
-<parameter name="ndefTargets" type="java.lang.String[]">
-</parameter>
-<parameter name="messages" type="android.nfc.NdefMessage[][]">
-</parameter>
-</method>
-<method name="getNdefMessages"
- return="android.nfc.NdefMessage[]"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="getNdefMessages"
- return="android.nfc.NdefMessage[]"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="target" type="java.lang.String">
-</parameter>
-</method>
-<method name="getNdefTargets"
- return="java.lang.String[]"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<field name="CREATOR"
- type="android.os.Parcelable.Creator"
- transient="false"
- volatile="false"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="TARGET_MIFARE_CLASSIC"
- type="java.lang.String"
- transient="false"
- volatile="false"
- value="&quot;type_mifare_classic&quot;"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="TARGET_OTHER"
- type="java.lang.String"
- transient="false"
- volatile="false"
- value="&quot;other&quot;"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="TARGET_TYPE_1"
- type="java.lang.String"
- transient="false"
- volatile="false"
- value="&quot;type_1&quot;"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="TARGET_TYPE_2"
- type="java.lang.String"
- transient="false"
- volatile="false"
- value="&quot;type_2&quot;"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="TARGET_TYPE_3"
- type="java.lang.String"
- transient="false"
- volatile="false"
- value="&quot;type_3&quot;"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="TARGET_TYPE_4"
- type="java.lang.String"
- transient="false"
- volatile="false"
- value="&quot;type_4&quot;"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-</class>
-<class name="NdefTagConnection"
- extends="android.nfc.RawTagConnection"
- abstract="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<method name="getModeHint"
- return="int"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<exception name="IOException" type="java.io.IOException">
-</exception>
-</method>
-<method name="makeReadOnly"
- return="boolean"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<exception name="IOException" type="java.io.IOException">
-</exception>
-</method>
-<method name="readNdefMessages"
- return="android.nfc.NdefMessage[]"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<exception name="FormatException" type="android.nfc.FormatException">
-</exception>
-<exception name="IOException" type="java.io.IOException">
-</exception>
-</method>
-<method name="writeNdefMessage"
- return="void"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="message" type="android.nfc.NdefMessage">
-</parameter>
-<exception name="FormatException" type="android.nfc.FormatException">
-</exception>
-<exception name="IOException" type="java.io.IOException">
-</exception>
-</method>
-<field name="NDEF_MODE_READ_ONCE"
- type="int"
- transient="false"
- volatile="false"
- value="1"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="NDEF_MODE_READ_ONLY"
- type="int"
- transient="false"
- volatile="false"
- value="2"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="NDEF_MODE_UNKNOWN"
- type="int"
- transient="false"
- volatile="false"
- value="5"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="NDEF_MODE_WRITE_MANY"
- type="int"
- transient="false"
- volatile="false"
- value="4"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="NDEF_MODE_WRITE_ONCE"
- type="int"
- transient="false"
- volatile="false"
- value="3"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-</class>
 <class name="NfcAdapter"
  extends="java.lang.Object"
  abstract="false"
@@ -114065,62 +113798,6 @@
  deprecated="not deprecated"
  visibility="public"
 >
-<method name="createNdefTagConnection"
- return="android.nfc.NdefTagConnection"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="tag" type="android.nfc.NdefTag">
-</parameter>
-</method>
-<method name="createNdefTagConnection"
- return="android.nfc.NdefTagConnection"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="tag" type="android.nfc.NdefTag">
-</parameter>
-<parameter name="target" type="java.lang.String">
-</parameter>
-</method>
-<method name="createRawTagConnection"
- return="android.nfc.RawTagConnection"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="tag" type="android.nfc.Tag">
-</parameter>
-</method>
-<method name="createRawTagConnection"
- return="android.nfc.RawTagConnection"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="tag" type="android.nfc.Tag">
-</parameter>
-<parameter name="target" type="java.lang.String">
-</parameter>
-</method>
 <method name="getDefaultAdapter"
  return="android.nfc.NfcAdapter"
  abstract="false"
@@ -114132,17 +113809,6 @@
  visibility="public"
 >
 </method>
-<method name="getLocalNdefMessage"
- return="android.nfc.NdefMessage"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
 <method name="isEnabled"
  return="boolean"
  abstract="false"
@@ -114154,30 +113820,6 @@
  visibility="public"
 >
 </method>
-<method name="setLocalNdefMessage"
- return="void"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="message" type="android.nfc.NdefMessage">
-</parameter>
-</method>
-<field name="ACTION_NDEF_TAG_DISCOVERED"
- type="java.lang.String"
- transient="false"
- volatile="false"
- value="&quot;android.nfc.action.NDEF_TAG_DISCOVERED&quot;"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
 <field name="ACTION_TAG_DISCOVERED"
  type="java.lang.String"
  transient="false"
@@ -114189,268 +113831,22 @@
  visibility="public"
 >
 </field>
-<field name="EXTRA_TAG"
+<field name="EXTRA_ID"
  type="java.lang.String"
  transient="false"
  volatile="false"
- value="&quot;android.nfc.extra.TAG&quot;"
+ value="&quot;android.nfc.extra.ID&quot;"
  static="true"
  final="true"
  deprecated="not deprecated"
  visibility="public"
 >
 </field>
-</class>
-<class name="RawTagConnection"
- extends="java.lang.Object"
- abstract="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<method name="close"
- return="void"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="connect"
- return="void"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<exception name="IOException" type="java.io.IOException">
-</exception>
-</method>
-<method name="getTag"
- return="android.nfc.Tag"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="getTagTarget"
- return="java.lang.String"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="isConnected"
- return="boolean"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="transceive"
- return="byte[]"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="data" type="byte[]">
-</parameter>
-<exception name="IOException" type="java.io.IOException">
-</exception>
-</method>
-</class>
-<class name="Tag"
- extends="java.lang.Object"
- abstract="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<implements name="android.os.Parcelable">
-</implements>
-<method name="createMockTag"
- return="android.nfc.Tag"
- abstract="false"
- native="false"
- synchronized="false"
- static="true"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="id" type="byte[]">
-</parameter>
-<parameter name="rawTargets" type="java.lang.String[]">
-</parameter>
-<parameter name="pollBytes" type="byte[]">
-</parameter>
-<parameter name="activationBytes" type="byte[]">
-</parameter>
-</method>
-<method name="describeContents"
- return="int"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="getActivationBytes"
- return="byte[]"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="getId"
- return="byte[]"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="getPollBytes"
- return="byte[]"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="getRawTargets"
- return="java.lang.String[]"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="writeToParcel"
- return="void"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="dest" type="android.os.Parcel">
-</parameter>
-<parameter name="flags" type="int">
-</parameter>
-</method>
-<field name="CREATOR"
- type="android.os.Parcelable.Creator"
- transient="false"
- volatile="false"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="TARGET_ISO_14443_3A"
+<field name="EXTRA_NDEF_MESSAGES"
  type="java.lang.String"
  transient="false"
  volatile="false"
- value="&quot;iso14443_3a&quot;"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="TARGET_ISO_14443_3B"
- type="java.lang.String"
- transient="false"
- volatile="false"
- value="&quot;iso14443_3b&quot;"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="TARGET_ISO_14443_4"
- type="java.lang.String"
- transient="false"
- volatile="false"
- value="&quot;iso14443_4&quot;"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="TARGET_ISO_15693"
- type="java.lang.String"
- transient="false"
- volatile="false"
- value="&quot;iso15693&quot;"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="TARGET_JIS_X_6319_4"
- type="java.lang.String"
- transient="false"
- volatile="false"
- value="&quot;jis_x_6319_4&quot;"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="TARGET_OTHER"
- type="java.lang.String"
- transient="false"
- volatile="false"
- value="&quot;other&quot;"
+ value="&quot;android.nfc.extra.NDEF_MESSAGES&quot;"
  static="true"
  final="true"
  deprecated="not deprecated"
@@ -134781,6 +134177,17 @@
 <parameter name="key" type="java.lang.String">
 </parameter>
 </method>
+<method name="getClassLoader"
+ return="java.lang.ClassLoader"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</method>
 <method name="getDouble"
  return="double"
  abstract="false"
@@ -151830,6 +151237,36 @@
 </implements>
 <implements name="android.provider.ContactsContract.DataColumnsWithJoins">
 </implements>
+<method name="getTypeLabel"
+ return="java.lang.CharSequence"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="res" type="android.content.res.Resources">
+</parameter>
+<parameter name="type" type="int">
+</parameter>
+<parameter name="label" type="java.lang.CharSequence">
+</parameter>
+</method>
+<method name="getTypeLabelResource"
+ return="int"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="true"
+ final="true"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="type" type="int">
+</parameter>
+</method>
 <field name="CONTENT_ITEM_TYPE"
  type="java.lang.String"
  transient="false"
@@ -160954,7 +160391,7 @@
  value="&quot;show_web_suggestions&quot;"
  static="true"
  final="true"
- deprecated="not deprecated"
+ deprecated="deprecated"
  visibility="public"
 >
 </field>
diff --git a/core/java/android/app/Fragment.java b/core/java/android/app/Fragment.java
index 3ec0912..f27a15e 100644
--- a/core/java/android/app/Fragment.java
+++ b/core/java/android/app/Fragment.java
@@ -84,6 +84,10 @@
             return mInstance;
         }
         
+        if (mArguments != null) {
+            mArguments.setClassLoader(activity.getClassLoader());
+        }
+        
         mInstance = Fragment.instantiate(activity, mClassName, mArguments);
         
         if (mSavedFragmentState != null) {
diff --git a/core/java/android/app/FragmentManager.java b/core/java/android/app/FragmentManager.java
index d9a6171..45f9325 100644
--- a/core/java/android/app/FragmentManager.java
+++ b/core/java/android/app/FragmentManager.java
@@ -1200,6 +1200,7 @@
                 f.mInLayout = false;
                 f.mAdded = false;
                 if (fs.mSavedFragmentState != null) {
+                    fs.mSavedFragmentState.setClassLoader(mActivity.getClassLoader());
                     f.mSavedViewState = fs.mSavedFragmentState.getSparseParcelableArray(
                             FragmentManagerImpl.VIEW_STATE_TAG);
                 }
diff --git a/core/java/android/bluetooth/BluetoothDeviceProfileState.java b/core/java/android/bluetooth/BluetoothDeviceProfileState.java
index fd8f930..0c8e4d9 100644
--- a/core/java/android/bluetooth/BluetoothDeviceProfileState.java
+++ b/core/java/android/bluetooth/BluetoothDeviceProfileState.java
@@ -80,6 +80,7 @@
     public static final int UNPAIR = 100;
     public static final int AUTO_CONNECT_PROFILES = 101;
     public static final int TRANSITION_TO_STABLE = 102;
+    public static final int CONNECT_OTHER_PROFILES = 103;
 
     private static final int AUTO_CONNECT_DELAY = 6000; // 6 secs
 
@@ -149,10 +150,6 @@
                     sendMessage(TRANSITION_TO_STABLE);
                 }
             } else if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
-                if (!getCurrentState().equals(mBondedDevice)) {
-                    Log.e(TAG, "State is: " + getCurrentState());
-                    return;
-                }
                 Message msg = new Message();
                 msg.what = AUTO_CONNECT_PROFILES;
                 sendMessageDelayed(msg, AUTO_CONNECT_DELAY);
@@ -330,6 +327,27 @@
                         }
                     }
                     break;
+                case CONNECT_OTHER_PROFILES:
+                    if (isPhoneDocked(mDevice)) {
+                       break;
+                    }
+                    if (message.arg1 == CONNECT_A2DP_OUTGOING) {
+                        if (mA2dpService != null &&
+                            mA2dpService.getConnectedDevices().size() == 0) {
+                            Log.i(TAG, "A2dp:Connect Other Profiles");
+                            mA2dpService.connect(mDevice);
+                        }
+                    } else if (message.arg1 == CONNECT_HFP_OUTGOING) {
+                        if (mHeadsetService == null) {
+                            deferMessage(message);
+                        } else {
+                            if (mHeadsetService.getConnectedDevices().size() == 0) {
+                                Log.i(TAG, "Headset:Connect Other Profiles");
+                                mHeadsetService.connect(mDevice);
+                            }
+                        }
+                    }
+                    break;
                 case TRANSITION_TO_STABLE:
                     // ignore.
                     break;
@@ -440,6 +458,7 @@
                 case DISCONNECT_PBAP_OUTGOING:
                 case UNPAIR:
                 case AUTO_CONNECT_PROFILES:
+                case CONNECT_OTHER_PROFILES:
                     deferMessage(message);
                     break;
                 case TRANSITION_TO_STABLE:
@@ -519,6 +538,7 @@
                 case DISCONNECT_PBAP_OUTGOING:
                 case UNPAIR:
                 case AUTO_CONNECT_PROFILES:
+                case CONNECT_OTHER_PROFILES:
                     deferMessage(message);
                     break;
                 case TRANSITION_TO_STABLE:
@@ -628,6 +648,7 @@
                 case DISCONNECT_PBAP_OUTGOING:
                 case UNPAIR:
                 case AUTO_CONNECT_PROFILES:
+                case CONNECT_OTHER_PROFILES:
                     deferMessage(message);
                     break;
                 case TRANSITION_TO_STABLE:
@@ -705,6 +726,7 @@
                 case DISCONNECT_PBAP_OUTGOING:
                 case UNPAIR:
                 case AUTO_CONNECT_PROFILES:
+                case CONNECT_OTHER_PROFILES:
                     deferMessage(message);
                     break;
                 case TRANSITION_TO_STABLE:
@@ -890,6 +912,7 @@
                 } else if (mHeadsetState == BluetoothHeadset.STATE_CONNECTING) {
                     return mHeadsetService.acceptIncomingConnect(mDevice);
                 } else if (mHeadsetState == BluetoothHeadset.STATE_DISCONNECTED) {
+                    handleConnectionOfOtherProfiles(command);
                     return mHeadsetService.createIncomingConnect(mDevice);
                 }
                 break;
@@ -899,6 +922,7 @@
                 }
                 break;
             case CONNECT_A2DP_INCOMING:
+                handleConnectionOfOtherProfiles(command);
                 // ignore, Bluez takes care
                 return true;
             case CONNECT_HID_OUTGOING:
@@ -960,6 +984,60 @@
         return false;
     }
 
+    private void handleConnectionOfOtherProfiles(int command) {
+        // The white paper recommendations mentions that when there is a
+        // link loss, it is the responsibility of the remote device to connect.
+        // Many connect only 1 profile - and they connect the second profile on
+        // some user action (like play being pressed) and so we need this code.
+        // Auto Connect code only connects to the last connected device - which
+        // is useful in cases like when the phone reboots. But consider the
+        // following case:
+        // User is connected to the car's phone and  A2DP profile.
+        // User comes to the desk  and places the phone in the dock
+        // (or any speaker or music system or even another headset) and thus
+        // gets connected to the A2DP profile.  User goes back to the car.
+        // Ideally the car's system is supposed to send incoming connections
+        // from both Handsfree and A2DP profile. But they don't. The Auto
+        // connect code, will not work here because we only auto connect to the
+        // last connected device for that profile which in this case is the dock.
+        // Now suppose a user is using 2 headsets simultaneously, one for the
+        // phone profile one for the A2DP profile. If this is the use case, we
+        // expect the user to use the preference to turn off the A2DP profile in
+        // the Settings screen for the first headset. Else, after link loss,
+        // there can be an incoming connection from the first headset which
+        // might result in the connection of the A2DP profile (if the second
+        // headset is slower) and thus the A2DP profile on the second headset
+        // will never get connected.
+        //
+        // TODO(): Handle other profiles here.
+        switch (command) {
+            case CONNECT_HFP_INCOMING:
+                // Connect A2DP if there is no incoming connection
+                // If the priority is OFF - don't auto connect.
+                // If the priority is AUTO_CONNECT, auto connect code takes care.
+                if (mA2dpService.getPriority(mDevice) == BluetoothProfile.PRIORITY_ON) {
+                    Message msg = new Message();
+                    msg.what = CONNECT_OTHER_PROFILES;
+                    msg.arg1 = CONNECT_A2DP_OUTGOING;
+                    sendMessageDelayed(msg, AUTO_CONNECT_DELAY);
+                }
+                break;
+            case CONNECT_A2DP_INCOMING:
+                // This is again against spec. HFP incoming connections should be made
+                // before A2DP, so we should not hit this case. But many devices
+                // don't follow this.
+                if (mHeadsetService.getPriority(mDevice) == BluetoothProfile.PRIORITY_ON) {
+                    Message msg = new Message();
+                    msg.what = CONNECT_OTHER_PROFILES;
+                    msg.arg1 = CONNECT_HFP_OUTGOING;
+                    sendMessageDelayed(msg, AUTO_CONNECT_DELAY);
+                }
+                break;
+            default:
+                break;
+        }
+
+    }
 
     /*package*/ BluetoothDevice getDevice() {
         return mDevice;
diff --git a/core/java/android/content/SyncManager.java b/core/java/android/content/SyncManager.java
index c9115c5..34bd386 100644
--- a/core/java/android/content/SyncManager.java
+++ b/core/java/android/content/SyncManager.java
@@ -728,10 +728,16 @@
             newDelayInMs = maxSyncRetryTimeInSeconds * 1000;
         }
 
+        final long backoff = now + newDelayInMs;
+
         mSyncStorageEngine.setBackoff(op.account, op.authority,
-                now + newDelayInMs, newDelayInMs);
+                backoff, newDelayInMs);
+
+        op.backoff = backoff;
+        op.updateEffectiveRunTime();
+
         synchronized (mSyncQueue) {
-            mSyncQueue.onBackoffChanged(op.account, op.authority, now + newDelayInMs);
+            mSyncQueue.onBackoffChanged(op.account, op.authority, backoff);
         }
     }
 
diff --git a/core/java/android/database/sqlite/SQLiteClosable.java b/core/java/android/database/sqlite/SQLiteClosable.java
index 96e6f22..93f9a71e 100644
--- a/core/java/android/database/sqlite/SQLiteClosable.java
+++ b/core/java/android/database/sqlite/SQLiteClosable.java
@@ -23,14 +23,12 @@
  */
 public abstract class SQLiteClosable {
     private int mReferenceCount = 1;
-    private Object mLock = new Object(); // STOPSHIP remove this line
 
     protected abstract void onAllReferencesReleased();
     protected void onAllReferencesReleasedFromContainer() {}
 
     public void acquireReference() {
-        synchronized(mLock) { // STOPSHIP change 'mLock' to 'this'
-            checkRefCount();
+        synchronized(this) {
             if (mReferenceCount <= 0) {
                 throw new IllegalStateException(
                         "attempt to re-open an already-closed object: " + getObjInfo());
@@ -40,8 +38,7 @@
     }
 
     public void releaseReference() {
-        synchronized(mLock) { // STOPSHIP change 'mLock' to 'this'
-            checkRefCount();
+        synchronized(this) {
             mReferenceCount--;
             if (mReferenceCount == 0) {
                 onAllReferencesReleased();
@@ -50,8 +47,7 @@
     }
 
     public void releaseReferenceFromContainer() {
-        synchronized(mLock) { // STOPSHIP change 'mLock' to 'this'
-            checkRefCount();
+        synchronized(this) {
             mReferenceCount--;
             if (mReferenceCount == 0) {
                 onAllReferencesReleasedFromContainer();
@@ -76,12 +72,4 @@
         buff.append(") ");
         return buff.toString();
     }
-
-    // STOPSHIP remove this method before shipping
-    private void checkRefCount() {
-        if (mReferenceCount > 1000) {
-            throw new IllegalStateException("bad refcount: " + mReferenceCount +
-                    ". file bug against frameworks->database" + getObjInfo());
-        }
-    }
 }
diff --git a/core/java/android/nfc/FormatException.java b/core/java/android/nfc/FormatException.java
index 21a7c3b..7045a03 100644
--- a/core/java/android/nfc/FormatException.java
+++ b/core/java/android/nfc/FormatException.java
@@ -16,7 +16,6 @@
 
 package android.nfc;
 
-//TODO(npelly) javadoc or consider alternatives
 public class FormatException extends Exception {
     public FormatException() {
         super();
diff --git a/core/java/android/nfc/NdefMessage.java b/core/java/android/nfc/NdefMessage.java
index feca94e..d107b54 100644
--- a/core/java/android/nfc/NdefMessage.java
+++ b/core/java/android/nfc/NdefMessage.java
@@ -28,7 +28,7 @@
  * message always contains zero or more NDEF records.</p>
  * <p>This is an immutable data class.
  */
-public class NdefMessage implements Parcelable {
+public final class NdefMessage implements Parcelable {
     private static final byte FLAG_MB = (byte) 0x80;
     private static final byte FLAG_ME = (byte) 0x40;
 
@@ -69,9 +69,9 @@
      * Returns a byte array representation of this entire NDEF message.
      */
     public byte[] toByteArray() {
-        //TODO(nxp): do not return null
-        //TODO(nxp): allocate the byte array once, copy each record once
-        //TODO(nxp): process MB and ME flags outside loop
+        //TODO: do not return null
+        //TODO: allocate the byte array once, copy each record once
+        //TODO: process MB and ME flags outside loop
         if ((mRecords == null) || (mRecords.length == 0))
             return null;
 
diff --git a/core/java/android/nfc/NdefRecord.java b/core/java/android/nfc/NdefRecord.java
index 557e44d..746d3df 100644
--- a/core/java/android/nfc/NdefRecord.java
+++ b/core/java/android/nfc/NdefRecord.java
@@ -35,7 +35,7 @@
  * large.
  * <p>This is an immutable data class.
  */
-public class NdefRecord implements Parcelable {
+public final class NdefRecord implements Parcelable {
     /**
      * Indicates no type, id, or payload is associated with this NDEF Record.
      * <p>
diff --git a/core/java/android/nfc/NdefTag.java b/core/java/android/nfc/NdefTag.java
index 0626edb..eb9d0dc 100644
--- a/core/java/android/nfc/NdefTag.java
+++ b/core/java/android/nfc/NdefTag.java
@@ -35,6 +35,7 @@
  * time and calls on this class will retrieve those read-only properties, and
  * not cause any further RF activity or block. Note however that arrays passed to and
  * returned by this class are *not* cloned, so be careful not to modify them.
+ * @hide
  */
 public class NdefTag extends Tag implements Parcelable {
     /**
diff --git a/core/java/android/nfc/NdefTagConnection.java b/core/java/android/nfc/NdefTagConnection.java
index 27fa25c..aafdbfd 100644
--- a/core/java/android/nfc/NdefTagConnection.java
+++ b/core/java/android/nfc/NdefTagConnection.java
@@ -28,6 +28,7 @@
  * <p class="note"><strong>Note:</strong>
  * Use of this class requires the {@link android.Manifest.permission#NFC}
  * permission.
+ * @hide
  */
 public class NdefTagConnection extends RawTagConnection {
     public static final int NDEF_MODE_READ_ONCE = 1;
diff --git a/core/java/android/nfc/NfcAdapter.java b/core/java/android/nfc/NfcAdapter.java
index a093d12..cf80faf 100644
--- a/core/java/android/nfc/NfcAdapter.java
+++ b/core/java/android/nfc/NfcAdapter.java
@@ -31,39 +31,31 @@
  * Use the static {@link #getDefaultAdapter} method to get the default NFC
  * Adapter for this Android device. Most Android devices will have only one NFC
  * Adapter, and {@link #getDefaultAdapter} returns the singleton object.
- * <p>
- * {@link NfcAdapter} can be used to create {@link RawTagConnection} or
- * {@link NdefTagConnection} connections to modify or perform low level access
- * to NFC Tags.
- * <p class="note">
- * <strong>Note:</strong> Some methods require the
- * {@link android.Manifest.permission#NFC} permission.
  */
 public final class NfcAdapter {
     /**
-     * Intent to start an activity when a non-NDEF tag is discovered.
-     * TODO(npelly) finalize decision on using CATEGORY or DATA URI to provide a
-     * hint for applications to filter the tag type.
-     * TODO(npelly) probably combine these two intents since tags aren't that simple
+     * Intent to start an activity when a tag is discovered.
      */
     @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
     public static final String ACTION_TAG_DISCOVERED = "android.nfc.action.TAG_DISCOVERED";
 
     /**
-     * Intent to start an activity when a NDEF tag is discovered. TODO(npelly)
-     * finalize decision on using CATEGORY or DATA URI to provide a hint for
-     * applications to filter the tag type.
-     */
-    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
-    public static final String ACTION_NDEF_TAG_DISCOVERED =
-            "android.nfc.action.NDEF_TAG_DISCOVERED";
-
-    /**
-     * Mandatory Tag extra for the ACTION_TAG and ACTION_NDEF_TAG intents.
+     * Mandatory Tag extra for the ACTION_TAG intents.
+     * @hide
      */
     public static final String EXTRA_TAG = "android.nfc.extra.TAG";
 
     /**
+     * Optional NdefMessage[] extra for the ACTION_TAG intents.
+     */
+    public static final String EXTRA_NDEF_MESSAGES = "android.nfc.extra.NDEF_MESSAGES";
+
+    /**
+     * Optional byte[] extra for the tag identifier.
+     */
+    public static final String EXTRA_ID = "android.nfc.extra.ID";
+
+    /**
      * Broadcast Action: a transaction with a secure element has been detected.
      * <p>
      * Always contains the extra field
@@ -324,6 +316,7 @@
      * <p>Requires {@link android.Manifest.permission#NFC} permission.
      *
      * @param message NDEF message to make public
+     * @hide
      */
     public void setLocalNdefMessage(NdefMessage message) {
         try {
@@ -338,6 +331,7 @@
      * <p>Requires {@link android.Manifest.permission#NFC} permission.
      *
      * @return NDEF Message that is publicly readable
+     * @hide
      */
     public NdefMessage getLocalNdefMessage() {
         try {
@@ -351,6 +345,7 @@
     /**
      * Create a raw tag connection to the default Target
      * <p>Requires {@link android.Manifest.permission#NFC} permission.
+     * @hide
      */
     public RawTagConnection createRawTagConnection(Tag tag) {
         if (tag.mServiceHandle == 0) {
@@ -367,6 +362,7 @@
     /**
      * Create a raw tag connection to the specified Target
      * <p>Requires {@link android.Manifest.permission#NFC} permission.
+     * @hide
      */
     public RawTagConnection createRawTagConnection(Tag tag, String target) {
         if (tag.mServiceHandle == 0) {
@@ -383,6 +379,7 @@
     /**
      * Create an NDEF tag connection to the default Target
      * <p>Requires {@link android.Manifest.permission#NFC} permission.
+     * @hide
      */
     public NdefTagConnection createNdefTagConnection(NdefTag tag) {
         if (tag.mServiceHandle == 0) {
@@ -399,6 +396,7 @@
     /**
      * Create an NDEF tag connection to the specified Target
      * <p>Requires {@link android.Manifest.permission#NFC} permission.
+     * @hide
      */
     public NdefTagConnection createNdefTagConnection(NdefTag tag, String target) {
         if (tag.mServiceHandle == 0) {
diff --git a/core/java/android/nfc/RawTagConnection.java b/core/java/android/nfc/RawTagConnection.java
index 24072e5..bfdaa77 100644
--- a/core/java/android/nfc/RawTagConnection.java
+++ b/core/java/android/nfc/RawTagConnection.java
@@ -32,6 +32,7 @@
  * <p class="note"><strong>Note:</strong>
  * Use of this class requires the {@link android.Manifest.permission#NFC}
  * permission.
+ * @hide
  */
 public class RawTagConnection {
 
diff --git a/core/java/android/nfc/Tag.java b/core/java/android/nfc/Tag.java
index dc6e798..5d0b04c 100644
--- a/core/java/android/nfc/Tag.java
+++ b/core/java/android/nfc/Tag.java
@@ -41,6 +41,7 @@
  * time and calls on this class will retrieve those read-only properties, and
  * not cause any further RF activity or block. Note however that arrays passed to and
  * returned by this class are *not* cloned, so be careful not to modify them.
+ * @hide
  */
 public class Tag implements Parcelable {
     /**
@@ -156,6 +157,7 @@
      * from SENSF_RES
      *
      * @return poll bytes, or null if they do not exist for this Tag technology
+     * @hide
      */
     public byte[] getPollBytes() {
         return mPollBytes;
@@ -172,6 +174,7 @@
      * <p>JIS_X_6319_4: null
      * <p>ISO15693: response flags (1 byte), DSFID (1 byte): null
      * @return activation bytes, or null if they do not exist for this Tag technology
+     * @hide
      */
     public byte[] getActivationBytes() {
         return mActivationBytes;
diff --git a/core/java/android/nfc/package.html b/core/java/android/nfc/package.html
index 8754cb9..b054d1c 100644
--- a/core/java/android/nfc/package.html
+++ b/core/java/android/nfc/package.html
@@ -1,30 +1,18 @@
 <HTML>
 <BODY>
-Provides access to Near Field Communication (NFC) functionality, allowing applications to connect
-to NFC tags, then transmit and recieving data. A "tag" may actually be another device that appears
-as a tag.
+Provides access to Near Field Communication (NFC) functionality, allowing applications to read
+NDEF message in NFC tags. A "tag" may actually be another device that appears as a tag.
 
 <p>Here's a summary of the classes:</p>
 
 <dl>
   <dt>{@link android.nfc.NfcAdapter}</dt>
   <dd>This represents the device's NFC adapter, which is your entry-point to performing NFC
-operations. Once you acquire an instance with {@link android.nfc.NfcAdapter#getDefaultAdapter}, you
-can create connections to tags.</dd>
-  <dt>{@link android.nfc.NdefTag} and {@link android.nfc.Tag}</dt>
-  <dd>These objects represent a tag. A tag is a thing that the NFC-enabled device can
-communicate with when within range. Usually, you'll work with {@link android.nfc.NdefTag}, which
-represents a tag that's compliant with the NFC Data Exchange Format (NDEF); a {@link
-android.nfc.Tag} represents a generalized tag. Note that {@link android.nfc.NdefTag} extends
-{@link android.nfc.Tag}.</dd>
-  <dt>{@link android.nfc.NdefTagConnection} and {@link android.nfc.RawTagConnection}</dt>
-  <dd>These objects represent a connection to a tag, respective to the type of tag connected
-(either {@link android.nfc.NdefTag} or {@link android.nfc.Tag}). Note that {@link
-android.nfc.NdefTagConnection} extends {@link android.nfc.RawTagConnection}.</dd>
+operations. You can acquire an instance with {@link android.nfc.NfcAdapter#getDefaultAdapter}.</dd>
   <dt>{@link android.nfc.NdefMessage}</dt>
   <dd>Represents an NDEF data message, which is the standard format in which "records"
 carrying data are transmitted between devices and tags. Your application can receive these
-messages from an {@link android.nfc.NdefTagConnection}.</dd>
+messages from an {@link android.nfc.NfcAdapter#ACTION_TAG_DISCOVERED} intent.</dd>
   <dt>{@link android.nfc.NdefRecord}</dt>
   <dd>Represents a record, which is delivered in a {@link android.nfc.NdefMessage} and describes the
 type of data being shared and carries the data itself.</dd>
diff --git a/core/java/android/os/Bundle.java b/core/java/android/os/Bundle.java
index 0ec1c74..8eac7aa 100644
--- a/core/java/android/os/Bundle.java
+++ b/core/java/android/os/Bundle.java
@@ -181,6 +181,13 @@
     }
 
     /**
+     * Return the ClassLoader currently associated with this Bundle.
+     */
+    public ClassLoader getClassLoader() {
+        return mClassLoader;
+    }
+    
+    /**
      * Clones the current Bundle. The internal map is cloned, but the keys and
      * values to which it refers are copied by reference.
      */
diff --git a/core/java/android/os/StrictMode.java b/core/java/android/os/StrictMode.java
index 9786959..40aceb3 100644
--- a/core/java/android/os/StrictMode.java
+++ b/core/java/android/os/StrictMode.java
@@ -173,6 +173,12 @@
     public static final int PENALTY_GATHER = 0x100;
 
     /**
+     * Mask of all the penalty bits.
+     */
+    private static final int PENALTY_MASK =
+            PENALTY_LOG | PENALTY_DIALOG | PENALTY_DEATH | PENALTY_DROPBOX | PENALTY_GATHER;
+
+    /**
      * The current VmPolicy in effect.
      */
     private static volatile int sVmPolicyMask = 0;
@@ -882,7 +888,7 @@
                 }
             }
 
-            // The violationMask, passed to ActivityManager, is a
+            // The violationMaskSubset, passed to ActivityManager, is a
             // subset of the original StrictMode policy bitmask, with
             // only the bit violated and penalty bits to be executed
             // by the ActivityManagerService remaining set.
@@ -900,7 +906,35 @@
             if (violationMaskSubset != 0) {
                 int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage);
                 violationMaskSubset |= violationBit;
+                final int violationMaskSubsetFinal = violationMaskSubset;
                 final int savedPolicyMask = getThreadPolicyMask();
+
+                final boolean justDropBox = (info.policy & PENALTY_MASK) == PENALTY_DROPBOX;
+                if (justDropBox) {
+                    // If all we're going to ask the activity manager
+                    // to do is dropbox it (the common case during
+                    // platform development), we can avoid doing this
+                    // call synchronously which Binder data suggests
+                    // isn't always super fast, despite the implementation
+                    // in the ActivityManager trying to be mostly async.
+                    new Thread("callActivityManagerForStrictModeDropbox") {
+                        public void run() {
+                            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
+                            try {
+                                ActivityManagerNative.getDefault().
+                                        handleApplicationStrictModeViolation(
+                                            RuntimeInit.getApplicationObject(),
+                                            violationMaskSubsetFinal,
+                                            info);
+                            } catch (RemoteException e) {
+                                Log.e(TAG, "RemoteException handling StrictMode violation", e);
+                            }
+                        }
+                    }.start();
+                    return;
+                }
+
+                // Normal synchronous call to the ActivityManager.
                 try {
                     // First, remove any policy before we call into the Activity Manager,
                     // otherwise we'll infinite recurse as we try to log policy violations
diff --git a/core/java/android/provider/ContactsContract.java b/core/java/android/provider/ContactsContract.java
index 0448ec0..9e6434a 100644
--- a/core/java/android/provider/ContactsContract.java
+++ b/core/java/android/provider/ContactsContract.java
@@ -5127,6 +5127,47 @@
              * <P>Type: TEXT</P>
              */
             public static final String NAME = DATA;
+
+            /**
+             * Return the string resource that best describes the given
+             * {@link #TYPE}. Will always return a valid resource.
+             */
+            public static final int getTypeLabelResource(int type) {
+                switch (type) {
+                    case TYPE_ASSISTANT: return com.android.internal.R.string.relationTypeAssistant;
+                    case TYPE_BROTHER: return com.android.internal.R.string.relationTypeBrother;
+                    case TYPE_CHILD: return com.android.internal.R.string.relationTypeChild;
+                    case TYPE_DOMESTIC_PARTNER:
+                            return com.android.internal.R.string.relationTypeDomesticPartner;
+                    case TYPE_FATHER: return com.android.internal.R.string.relationTypeFather;
+                    case TYPE_FRIEND: return com.android.internal.R.string.relationTypeFriend;
+                    case TYPE_MANAGER: return com.android.internal.R.string.relationTypeManager;
+                    case TYPE_MOTHER: return com.android.internal.R.string.relationTypeMother;
+                    case TYPE_PARENT: return com.android.internal.R.string.relationTypeParent;
+                    case TYPE_PARTNER: return com.android.internal.R.string.relationTypePartner;
+                    case TYPE_REFERRED_BY:
+                            return com.android.internal.R.string.relationTypeReferredBy;
+                    case TYPE_RELATIVE: return com.android.internal.R.string.relationTypeRelative;
+                    case TYPE_SISTER: return com.android.internal.R.string.relationTypeSister;
+                    case TYPE_SPOUSE: return com.android.internal.R.string.relationTypeSpouse;
+                    default: return com.android.internal.R.string.orgTypeCustom;
+                }
+            }
+
+            /**
+             * Return a {@link CharSequence} that best describes the given type,
+             * possibly substituting the given {@link #LABEL} value
+             * for {@link #TYPE_CUSTOM}.
+             */
+            public static final CharSequence getTypeLabel(Resources res, int type,
+                    CharSequence label) {
+                if (type == TYPE_CUSTOM && !TextUtils.isEmpty(label)) {
+                    return label;
+                } else {
+                    final int labelRes = getTypeLabelResource(type);
+                    return res.getText(labelRes);
+                }
+            }
         }
 
         /**
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index 2b79037..ee53828 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -1641,11 +1641,10 @@
         public static final String HAPTIC_FEEDBACK_ENABLED = "haptic_feedback_enabled";
 
         /**
-         * Whether live web suggestions while the user types into search dialogs are
-         * enabled. Browsers and other search UIs should respect this, as it allows
-         * a user to avoid sending partial queries to a search engine, if it poses
-         * any privacy concern. The value is boolean (1 or 0).
+         * @deprecated Each application that shows web suggestions should have its own
+         * setting for this.
          */
+        @Deprecated
         public static final String SHOW_WEB_SUGGESTIONS = "show_web_suggestions";
 
         /**
diff --git a/core/java/android/service/wallpaper/WallpaperService.java b/core/java/android/service/wallpaper/WallpaperService.java
index 9645a17..26346d2 100644
--- a/core/java/android/service/wallpaper/WallpaperService.java
+++ b/core/java/android/service/wallpaper/WallpaperService.java
@@ -655,7 +655,10 @@
             if (!mDestroyed) {
                 if (DEBUG) Log.v(TAG, "onDesiredSizeChanged("
                         + desiredWidth + "," + desiredHeight + "): " + this);
+                mIWallpaperEngine.mReqWidth = desiredWidth;
+                mIWallpaperEngine.mReqHeight = desiredHeight;
                 onDesiredSizeChanged(desiredWidth, desiredHeight);
+                doOffsetsChanged();
             }
         }
         
diff --git a/core/java/android/webkit/L10nUtils.java b/core/java/android/webkit/L10nUtils.java
new file mode 100644
index 0000000..962492e
--- /dev/null
+++ b/core/java/android/webkit/L10nUtils.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.webkit;
+
+import android.content.Context;
+
+import java.util.List;
+import java.util.Vector;
+
+/**
+ * @hide
+ */
+public class L10nUtils {
+
+    // These array elements must be kept in sync with those defined in
+    // external/chromium/android/app/l10n_utils.h
+    private static int[] mIdsArray = {
+        com.android.internal.R.string.autofill_address_name_separator,      // IDS_AUTOFILL_DIALOG_ADDRESS_NAME_SEPARATOR
+        com.android.internal.R.string.autofill_address_summary_name_format, // IDS_AUTOFILL_DIALOG_ADDRESS_SUMMARY_NAME_FORMAT
+        com.android.internal.R.string.autofill_address_summary_separator,   // IDS_AUTOFILL_DIALOG_ADDRESS_SUMMARY_SEPARATOR
+        com.android.internal.R.string.autofill_address_summary_format       // IDS_AUTOFILL_DIALOG_ADDRESS_SUMMARY_FORMAT
+    };
+
+    private static List<String> mStrings;
+
+    public static void loadStrings(Context context) {
+        if (mStrings != null) {
+            return;
+        }
+
+        mStrings = new Vector<String>(mIdsArray.length);
+        for (int i = 0; i < mIdsArray.length; i++) {
+            mStrings.add(context.getResources().getString(mIdsArray[i]));
+        }
+    }
+
+    public static String getLocalisedString(int id) {
+        return mStrings.get(id);
+    }
+}
diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java
index 68fa5c385..72b4e36 100644
--- a/core/java/android/webkit/WebView.java
+++ b/core/java/android/webkit/WebView.java
@@ -917,6 +917,8 @@
         if (privateBrowsing) {
             startPrivateBrowsing();
         }
+
+        L10nUtils.loadStrings(context);
     }
 
     /*
@@ -6447,7 +6449,7 @@
                     mUserScroll = false;
                     final WebViewCore.DrawData draw = (WebViewCore.DrawData) msg.obj;
                     setBaseLayer(draw.mBaseLayer, draw.mInvalRegion.getBounds());
-                    final Point viewSize = draw.mViewPoint;
+                    final Point viewSize = draw.mViewSize;
                     WebViewCore.ViewState viewState = draw.mViewState;
                     boolean isPictureAfterFirstLayout = viewState != null;
                     if (isPictureAfterFirstLayout) {
@@ -6470,8 +6472,8 @@
                     // received in the fixed dimension.
                     final boolean updateLayout = viewSize.x == mLastWidthSent
                             && viewSize.y == mLastHeightSent;
-                    recordNewContentSize(draw.mWidthHeight.x,
-                            draw.mWidthHeight.y, updateLayout);
+                    recordNewContentSize(draw.mContentSize.x,
+                            draw.mContentSize.y, updateLayout);
                     if (DebugFlags.WEB_VIEW) {
                         Rect b = draw.mInvalRegion.getBounds();
                         Log.v(LOGTAG, "NEW_PICTURE_MSG_ID {" +
diff --git a/core/java/android/webkit/WebViewCore.java b/core/java/android/webkit/WebViewCore.java
index cd6c81e..9a873b6 100644
--- a/core/java/android/webkit/WebViewCore.java
+++ b/core/java/android/webkit/WebViewCore.java
@@ -1826,12 +1826,13 @@
         DrawData() {
             mBaseLayer = 0;
             mInvalRegion = new Region();
-            mWidthHeight = new Point();
+            mContentSize = new Point();
         }
         int mBaseLayer;
         Region mInvalRegion;
-        Point mViewPoint;
-        Point mWidthHeight;
+        // view size that was used by webkit during the most recent layout
+        Point mViewSize;
+        Point mContentSize;
         int mMinPrefWidth;
         // only non-null if it is for the first picture set after the first layout
         ViewState mViewState;
@@ -1842,16 +1843,14 @@
         mDrawIsScheduled = false;
         DrawData draw = new DrawData();
         if (DebugFlags.WEB_VIEW_CORE) Log.v(LOGTAG, "webkitDraw start");
-        draw.mBaseLayer = nativeRecordContent(draw.mInvalRegion, draw.mWidthHeight);
+        draw.mBaseLayer = nativeRecordContent(draw.mInvalRegion, draw.mContentSize);
         if (draw.mBaseLayer == 0) {
             if (DebugFlags.WEB_VIEW_CORE) Log.v(LOGTAG, "webkitDraw abort");
             return;
         }
         if (mWebView != null) {
-            // Send the native view size that was used during the most recent
-            // layout.
             draw.mFocusSizeChanged = nativeFocusBoundsChanged();
-            draw.mViewPoint = new Point(mCurrentViewWidth, mCurrentViewHeight);
+            draw.mViewSize = new Point(mCurrentViewWidth, mCurrentViewHeight);
             if (mSettings.getUseWideViewPort()) {
                 draw.mMinPrefWidth = Math.max(
                         mViewportWidth == -1 ? WebView.DEFAULT_VIEWPORT_WIDTH
diff --git a/core/java/android/webkit/ZoomManager.java b/core/java/android/webkit/ZoomManager.java
index 91c0fb2..86dfd1f 100644
--- a/core/java/android/webkit/ZoomManager.java
+++ b/core/java/android/webkit/ZoomManager.java
@@ -793,9 +793,9 @@
                 // bound to match the default scale for mobile sites.
                 setZoomOverviewWidth(Math.min(WebView.sMaxViewportWidth,
                     Math.max((int) (viewWidth * mInvDefaultScale),
-                            Math.max(drawData.mMinPrefWidth, drawData.mViewPoint.x))));
+                            Math.max(drawData.mMinPrefWidth, drawData.mViewSize.x))));
             } else {
-                final int contentWidth = drawData.mWidthHeight.x;
+                final int contentWidth = drawData.mContentSize.x;
                 setZoomOverviewWidth(Math.min(WebView.sMaxViewportWidth, contentWidth));
             }
         }
@@ -826,16 +826,16 @@
         assert mWebView.getSettings() != null;
 
         WebViewCore.ViewState viewState = drawData.mViewState;
-        final Point viewSize = drawData.mViewPoint;
+        final Point viewSize = drawData.mViewSize;
         updateZoomRange(viewState, viewSize.x, drawData.mMinPrefWidth);
         if (mWebView.getSettings().getUseWideViewPort() &&
             mWebView.getSettings().getUseFixedViewport()) {
-            final int contentWidth = drawData.mWidthHeight.x;
+            final int contentWidth = drawData.mContentSize.x;
             setZoomOverviewWidth(Math.min(WebView.sMaxViewportWidth, contentWidth));
         }
 
         if (!mWebView.drawHistory()) {
-            final float scale;
+            float scale;
             final boolean reflowText;
             WebSettings settings = mWebView.getSettings();
 
@@ -847,12 +847,13 @@
                 scale = viewState.mViewScale;
                 reflowText = false;
             } else {
+                scale = getZoomOverviewScale();
                 if (settings.getUseWideViewPort()
-                    && (settings.getLoadWithOverviewMode() || settings.getUseFixedViewport())) {
+                    && settings.getLoadWithOverviewMode()) {
                     mInitialZoomOverview = true;
-                    scale = (float) mWebView.getViewWidth() / mZoomOverviewWidth;
                 } else {
-                    scale = viewState.mTextWrapScale;
+                    scale = Math.max(viewState.mTextWrapScale, scale);
+                    mInitialZoomOverview = !exceedsMinScaleIncrement(scale, getZoomOverviewScale());
                 }
                 reflowText = exceedsMinScaleIncrement(mTextWrapScale, scale);
             }
diff --git a/core/java/com/android/internal/util/AsyncChannel.java b/core/java/com/android/internal/util/AsyncChannel.java
index 15b9290..b0778dd 100644
--- a/core/java/com/android/internal/util/AsyncChannel.java
+++ b/core/java/com/android/internal/util/AsyncChannel.java
@@ -246,7 +246,6 @@
                 mToken = token;
             }
 
-            @Override
             public void run() {
                 connectSrcHandlerToPackage(mSrcCtx, mSrcHdlr, mDstPackageName, mDstClassName,
                         mToken);
diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml
index 22a80f5..2444fce 100644
--- a/core/res/AndroidManifest.xml
+++ b/core/res/AndroidManifest.xml
@@ -88,7 +88,6 @@
     <protected-broadcast android:name="android.hardware.action.USB_CAMERA_ATTACHED" />
     <protected-broadcast android:name="android.hardware.action.USB_CAMERA_DETACHED" />
 
-    <protected-broadcast android:name="android.nfc.action.NDEF_TAG_DISCOVERED" />
     <protected-broadcast android:name="android.nfc.action.TAG_DISCOVERED" />
     <protected-broadcast android:name="android.nfc.action.LLCP_LINK_STATE_CHANGED" />
     <protected-broadcast android:name="android.nfc.action.TRANSACTION_DETECTED" />
diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml
index 061fa4f..8b9bc43 100755
--- a/core/res/res/values/strings.xml
+++ b/core/res/res/values/strings.xml
@@ -1440,6 +1440,37 @@
     <!-- Custom organization type -->
     <string name="orgTypeCustom">Custom</string>
 
+    <!-- Custom relationship custom [CHAR LIMIT=20] -->
+    <string name="relationTypeCustom">Custom</string>
+    <!-- Assistant relationship type [CHAR LIMIT=20] -->
+    <string name="relationTypeAssistant">Assistant</string>
+    <!-- Brother relationship type [CHAR LIMIT=20] -->
+    <string name="relationTypeBrother">Brother</string>
+    <!-- Child relationship type [CHAR LIMIT=20] -->
+    <string name="relationTypeChild">Child</string>
+    <!-- Domestic Partner relationship type [CHAR LIMIT=20] -->
+    <string name="relationTypeDomesticPartner">Domestic Partner</string>
+    <!-- Father relationship type [CHAR LIMIT=20] -->
+    <string name="relationTypeFather">Father</string>
+    <!-- Friend relationship type [CHAR LIMIT=20] -->
+    <string name="relationTypeFriend">Friend</string>
+    <!-- Manager relationship type [CHAR LIMIT=20] -->
+    <string name="relationTypeManager">Manager</string>
+    <!-- Mother relationship type [CHAR LIMIT=20] -->
+    <string name="relationTypeMother">Mother</string>
+    <!-- Parent relationship type [CHAR LIMIT=20] -->
+    <string name="relationTypeParent">Parent</string>
+    <!-- Partner relationship type [CHAR LIMIT=20] -->
+    <string name="relationTypePartner">Partner</string>
+    <!-- Referred by relationship type [CHAR LIMIT=20] -->
+    <string name="relationTypeReferredBy">Referred by</string>
+    <!-- Relative relationship type [CHAR LIMIT=20] -->
+    <string name="relationTypeRelative">Relative</string>
+    <!-- Sister relationship type [CHAR LIMIT=20] -->
+    <string name="relationTypeSister">Sister</string>
+    <!-- Spouse relationship type [CHAR LIMIT=20] -->
+    <string name="relationTypeSpouse">Spouse</string>
+    
     <!-- Custom SIP address type -->
     <string name="sipAddressTypeCustom">Custom</string>
     <!-- Home SIP address type -->
@@ -1652,7 +1683,21 @@
     <string name="double_tap_toast">Tip: double-tap to zoom in and out.</string>
 
     <!-- Text to show in the auto complete drop down list on a text view when the browser can auto fill the entire form -->
-    <string name="autofill_this_form">AutoFill this form</string>
+    <string name="autofill_this_form">AutoFill</string>
+
+    <!-- String used to separate FirstName and LastName when writing out a local name
+         e.g. John<separator>Smith [CHAR-LIMIT=NONE]-->
+    <string name="autofill_address_name_separator">\u0020</string>
+    <!-- Format string for displaying a name. $1 is First Name, $2 is autofill_address_name_separator, $3 is Last Name.
+         e.g. (John)( )(Smith) -->
+    <string name="autofill_address_summary_name_format">$1$2$3</string>
+
+    <!-- String used to separate Name and Address Line 1
+         e.g. John Smith<separator>123 Main Street [CHAR-LIMIT=NONE]-->
+    <string name="autofill_address_summary_separator">,\u0020</string>
+    <!-- Format string for displaying a name and address summary. $1 is the Full Name, $2 is autofill_address_summary_separator, $3 is the Address
+         e.g. (John Smith)(, )(123 Main Street) -->
+    <string name="autofill_address_summary_format">$1$2$3</string>
 
     <!-- Title of an application permission, listed so the user can choose whether
         they want to allow the application to do this. -->
diff --git a/graphics/java/android/renderscript/FileA3D.java b/graphics/java/android/renderscript/FileA3D.java
index 7548878..fc74fc4 100644
--- a/graphics/java/android/renderscript/FileA3D.java
+++ b/graphics/java/android/renderscript/FileA3D.java
@@ -141,9 +141,11 @@
     }
 
     IndexEntry[] mFileEntries;
+    InputStream mInputStream;
 
-    FileA3D(int id, RenderScript rs) {
+    FileA3D(int id, RenderScript rs, InputStream stream) {
         super(id, rs);
+        mInputStream = stream;
     }
 
     private void initEntries() {
@@ -193,20 +195,12 @@
             if(fileId == 0) {
                 throw new IllegalStateException("Load failed.");
             }
-            FileA3D fa3d = new FileA3D(fileId, rs);
+            FileA3D fa3d = new FileA3D(fileId, rs, is);
             fa3d.initEntries();
             return fa3d;
 
         } catch (Exception e) {
             // Ignore
-        } finally {
-            if (is != null) {
-                try {
-                    is.close();
-                } catch (IOException e) {
-                    // Ignore
-                }
-            }
         }
 
         return null;
diff --git a/graphics/jni/android_renderscript_RenderScript.cpp b/graphics/jni/android_renderscript_RenderScript.cpp
index dd108c0..b0faacc 100644
--- a/graphics/jni/android_renderscript_RenderScript.cpp
+++ b/graphics/jni/android_renderscript_RenderScript.cpp
@@ -104,7 +104,10 @@
 {
     LOG_API("nGetName, con(%p), obj(%p)", con, (void *)obj);
     const char *name = NULL;
-    rsGetName(con, (void *)obj, &name);
+    rsaGetName(con, (void *)obj, &name);
+    if(name == NULL || strlen(name) == 0) {
+        return NULL;
+    }
     return _env->NewStringUTF(name);
 }
 
@@ -306,7 +309,7 @@
     assert(dataSize == 5);
 
     uint32_t elementData[5];
-    rsElementGetNativeData(con, (RsElement)id, elementData, dataSize);
+    rsaElementGetNativeData(con, (RsElement)id, elementData, dataSize);
 
     for(jint i = 0; i < dataSize; i ++) {
         _env->SetIntArrayRegion(_elementData, i, 1, (const jint*)&elementData[i]);
@@ -323,7 +326,7 @@
     uint32_t *ids = (uint32_t *)malloc((uint32_t)dataSize * sizeof(uint32_t));
     const char **names = (const char **)malloc((uint32_t)dataSize * sizeof(const char *));
 
-    rsElementGetSubElements(con, (RsElement)id, ids, names, (uint32_t)dataSize);
+    rsaElementGetSubElements(con, (RsElement)id, ids, names, (uint32_t)dataSize);
 
     for(jint i = 0; i < dataSize; i++) {
         _env->SetObjectArrayElement(_names, i, _env->NewStringUTF(names[i]));
@@ -364,7 +367,7 @@
     LOG_API("nTypeCreate, con(%p)", con);
 
     uint32_t typeData[6];
-    rsTypeGetNativeData(con, (RsType)id, typeData, 6);
+    rsaTypeGetNativeData(con, (RsType)id, typeData, 6);
 
     for(jint i = 0; i < elementCount; i ++) {
         _env->SetIntArrayRegion(_typeData, i, 1, (const jint*)&typeData[i]);
@@ -590,7 +593,7 @@
 nAllocationGetType(JNIEnv *_env, jobject _this, RsContext con, jint a)
 {
     LOG_API("nAllocationGetType, con(%p), a(%p)", con, (RsAllocation)a);
-    return (jint) rsAllocationGetType(con, (RsAllocation)a);
+    return (jint) rsaAllocationGetType(con, (RsAllocation)a);
 }
 
 static void
@@ -616,7 +619,7 @@
 
     Asset* asset = reinterpret_cast<Asset*>(native_asset);
 
-    jint id = (jint)rsFileA3DCreateFromAssetStream(con, asset->getBuffer(false), asset->getLength());
+    jint id = (jint)rsaFileA3DCreateFromAssetStream(con, asset->getBuffer(false), asset->getLength());
     return id;
 }
 
@@ -624,7 +627,7 @@
 nFileA3DGetNumIndexEntries(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D)
 {
     int32_t numEntries = 0;
-    rsFileA3DGetNumIndexEntries(con, &numEntries, (RsFile)fileA3D);
+    rsaFileA3DGetNumIndexEntries(con, &numEntries, (RsFile)fileA3D);
     return numEntries;
 }
 
@@ -634,7 +637,7 @@
     LOGV("______nFileA3D %u", (uint32_t) fileA3D);
     RsFileIndexEntry *fileEntries = (RsFileIndexEntry*)malloc((uint32_t)numEntries * sizeof(RsFileIndexEntry));
 
-    rsFileA3DGetIndexEntries(con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
+    rsaFileA3DGetIndexEntries(con, fileEntries, (uint32_t)numEntries, (RsFile)fileA3D);
 
     for(jint i = 0; i < numEntries; i ++) {
         _env->SetObjectArrayElement(_entries, i, _env->NewStringUTF(fileEntries[i].objectName));
@@ -648,7 +651,7 @@
 nFileA3DGetEntryByIndex(JNIEnv *_env, jobject _this, RsContext con, jint fileA3D, jint index)
 {
     LOGV("______nFileA3D %u", (uint32_t) fileA3D);
-    jint id = (jint)rsFileA3DGetEntryByIndex(con, (uint32_t)index, (RsFile)fileA3D);
+    jint id = (jint)rsaFileA3DGetEntryByIndex(con, (uint32_t)index, (RsFile)fileA3D);
     return id;
 }
 
@@ -1166,7 +1169,7 @@
 {
     LOG_API("nMeshGetVertexBufferCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
     jint vtxCount = 0;
-    rsMeshGetVertexBufferCount(con, (RsMesh)mesh, &vtxCount);
+    rsaMeshGetVertexBufferCount(con, (RsMesh)mesh, &vtxCount);
     return vtxCount;
 }
 
@@ -1175,7 +1178,7 @@
 {
     LOG_API("nMeshGetIndexCount, con(%p), Mesh(%p)", con, (RsMesh)mesh);
     jint idxCount = 0;
-    rsMeshGetIndexCount(con, (RsMesh)mesh, &idxCount);
+    rsaMeshGetIndexCount(con, (RsMesh)mesh, &idxCount);
     return idxCount;
 }
 
@@ -1185,7 +1188,7 @@
     LOG_API("nMeshGetVertices, con(%p), Mesh(%p)", con, (RsMesh)mesh);
 
     RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numVtxIDs * sizeof(RsAllocation));
-    rsMeshGetVertices(con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
+    rsaMeshGetVertices(con, (RsMesh)mesh, allocs, (uint32_t)numVtxIDs);
 
     for(jint i = 0; i < numVtxIDs; i ++) {
         _env->SetIntArrayRegion(_ids, i, 1, (const jint*)&allocs[i]);
@@ -1202,7 +1205,7 @@
     RsAllocation *allocs = (RsAllocation*)malloc((uint32_t)numIndices * sizeof(RsAllocation));
     uint32_t *prims= (uint32_t*)malloc((uint32_t)numIndices * sizeof(uint32_t));
 
-    rsMeshGetIndices(con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
+    rsaMeshGetIndices(con, (RsMesh)mesh, allocs, prims, (uint32_t)numIndices);
 
     for(jint i = 0; i < numIndices; i ++) {
         _env->SetIntArrayRegion(_idxIds, i, 1, (const jint*)&allocs[i]);
diff --git a/libs/rs/RenderScript.h b/libs/rs/RenderScript.h
index 1d67329..8c2081d 100644
--- a/libs/rs/RenderScript.h
+++ b/libs/rs/RenderScript.h
@@ -286,6 +286,25 @@
 
 } RsScriptCall;
 
+// A3D loading and object update code.
+// Should only be called at object creation, not thread safe
+RsObjectBase rsaFileA3DGetEntryByIndex(RsContext, uint32_t idx, RsFile);
+RsFile rsaFileA3DCreateFromAssetStream(RsContext, const void *data, uint32_t len);
+void rsaFileA3DGetNumIndexEntries(RsContext, int32_t *numEntries, RsFile);
+void rsaFileA3DGetIndexEntries(RsContext, RsFileIndexEntry *fileEntries,uint32_t numEntries, RsFile);
+void rsaGetName(RsContext, void * obj, const char **name);
+// Mesh update functions
+void rsaMeshGetVertexBufferCount(RsContext, RsMesh, int32_t *vtxCount);
+void rsaMeshGetIndexCount(RsContext, RsMesh, int32_t *idxCount);
+void rsaMeshGetVertices(RsContext, RsMesh, RsAllocation *vtxData, uint32_t vtxDataCount);
+void rsaMeshGetIndices(RsContext, RsMesh, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount);
+// Allocation update
+const void* rsaAllocationGetType(RsContext con, RsAllocation va);
+// Type update
+void rsaTypeGetNativeData(RsContext, RsType, uint32_t *typeData, uint32_t typeDataSize);
+// Element update
+void rsaElementGetNativeData(RsContext, RsElement, uint32_t *elemData, uint32_t elemDataSize);
+void rsaElementGetSubElements(RsContext, RsElement, uint32_t *ids, const char **names, uint32_t dataSize);
 
 // Async commands for returning new IDS
 RsType rsaTypeCreate(RsContext, RsElement, uint32_t dimCount,
@@ -293,7 +312,6 @@
 RsAllocation rsaAllocationCreateTyped(RsContext rsc, RsType vtype);
 RsAllocation rsaAllocationCreateFromBitmap(RsContext con, uint32_t w, uint32_t h, RsElement _dst, RsElement _src,  bool genMips, const void *data);
 
-
 #ifndef NO_RS_FUNCS
 #include "rsgApiFuncDecl.h"
 #endif
diff --git a/libs/rs/rs.spec b/libs/rs/rs.spec
index 3e131b2..14809e9 100644
--- a/libs/rs/rs.spec
+++ b/libs/rs/rs.spec
@@ -58,11 +58,6 @@
 	param size_t len
 	}
 
-GetName {
-	param void *obj
-	param const char **name
-	}
-
 ObjDestroy {
 	param RsAsyncVoidPtr objPtr
 	}
@@ -84,26 +79,6 @@
 	ret RsElement
 	}
 
-ElementGetNativeData {
-	param RsElement elem
-	param uint32_t *elemData
-	param uint32_t elemDataSize
-	}
-
-ElementGetSubElements {
-	param RsElement elem
-	param uint32_t *ids
-	param const char **names
-	param uint32_t dataSize
-	}
-
-
-TypeGetNativeData {
-	param RsType type
-	param uint32_t * typeData
-	param uint32_t typeDataSize
-	}
-
 AllocationUpdateFromBitmap {
 	param RsAllocation alloc
 	param RsElement srcFmt
@@ -237,11 +212,6 @@
 	param const void *data
 	}
 
-AllocationGetType {
-	param RsAllocation va
-	ret const void*
-	}
-
 AllocationResize1D {
 	param RsAllocation va
 	param uint32_t dimX
@@ -433,35 +403,12 @@
 	ret RsProgramVertex
 	}
 
-FileA3DCreateFromAssetStream {
-	param const void * data
-	param size_t len
-	ret RsFile
-	}
-
 FileOpen {
 	ret RsFile
 	param const char *name
 	param size_t len
 	}
 
-FileA3DGetNumIndexEntries {
-	param int32_t * numEntries
-	param RsFile file
-	}
-
-FileA3DGetIndexEntries {
-	param RsFileIndexEntry * fileEntries
-	param uint32_t numEntries
-	param RsFile fileA3D
-	}
-
-FileA3DGetEntryByIndex {
-	param uint32_t index
-	param RsFile file
-	ret RsObjectBase
-	}
-
 FontCreateFromFile {
 	param const char *name
 	param uint32_t fontSize
@@ -488,29 +435,6 @@
 	param uint32_t slot
 	}
 
-MeshGetVertexBufferCount {
-	param RsMesh mesh
-	param int32_t *numVtx
-	}
-
-MeshGetIndexCount {
-	param RsMesh mesh
-	param int32_t *numIdx
-	}
-
-MeshGetVertices {
-	param RsMesh mv
-	param RsAllocation *vtxData
-	param uint32_t vtxDataCount
-	}
-
-MeshGetIndices {
-	param RsMesh mv
-	param RsAllocation *va
-	param uint32_t *primType
-	param uint32_t idxDataCount
-	}
-
 AnimationCreate {
 	param const float *inValues
 	param const float *outValues
diff --git a/libs/rs/rsAllocation.cpp b/libs/rs/rsAllocation.cpp
index b4872e3..b74fa8e 100644
--- a/libs/rs/rsAllocation.cpp
+++ b/libs/rs/rsAllocation.cpp
@@ -816,7 +816,12 @@
     a->resize2D(rsc, dimX, dimY);
 }
 
-const void* rsi_AllocationGetType(Context *rsc, RsAllocation va)
+#endif //ANDROID_RS_BUILD_FOR_HOST
+
+}
+}
+
+const void * rsaAllocationGetType(RsContext con, RsAllocation va)
 {
     Allocation *a = static_cast<Allocation *>(va);
     a->getType()->incUserRef();
@@ -824,11 +829,6 @@
     return a->getType();
 }
 
-#endif //ANDROID_RS_BUILD_FOR_HOST
-
-}
-}
-
 RsAllocation rsaAllocationCreateTyped(RsContext con, RsType vtype)
 {
     Context *rsc = static_cast<Context *>(con);
@@ -873,4 +873,3 @@
 
     return texAlloc;
 }
-
diff --git a/libs/rs/rsContext.cpp b/libs/rs/rsContext.cpp
index 944cd86..3f04585 100644
--- a/libs/rs/rsContext.cpp
+++ b/libs/rs/rsContext.cpp
@@ -222,6 +222,7 @@
     glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
 
     mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
+    mGL.GL_NV_texture_npot_2D_mipmap = NULL != strstr((const char *)mGL.mExtensions, "GL_NV_texture_npot_2D_mipmap");
     mGL.EXT_texture_max_aniso = 1.0f;
     bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic");
     if(hasAniso) {
@@ -970,12 +971,6 @@
     rsc->assignName(ob, name, len);
 }
 
-void rsi_GetName(Context *rsc, void * obj, const char **name)
-{
-    ObjectBase *ob = static_cast<ObjectBase *>(obj);
-    (*name) = ob->getName();
-}
-
 void rsi_ObjDestroy(Context *rsc, void *optr)
 {
     ObjectBase *ob = static_cast<ObjectBase *>(optr);
@@ -1062,3 +1057,10 @@
     rsc->deinitToClient();
 }
 
+// Only to be called at a3d load time, before object is visible to user
+// not thread safe
+void rsaGetName(RsContext con, void * obj, const char **name)
+{
+    ObjectBase *ob = static_cast<ObjectBase *>(obj);
+    (*name) = ob->getName();
+}
diff --git a/libs/rs/rsContext.h b/libs/rs/rsContext.h
index dbe2c79..e269d4e 100644
--- a/libs/rs/rsContext.h
+++ b/libs/rs/rsContext.h
@@ -190,6 +190,7 @@
     mutable const ObjectBase * mObjHead;
 
     bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
+    bool ext_GL_NV_texture_npot_2D_mipmap() const {return mGL.GL_NV_texture_npot_2D_mipmap;}
     float ext_texture_max_aniso() const {return mGL.EXT_texture_max_aniso; }
     uint32_t getMaxFragmentTextures() const {return mGL.mMaxFragmentTextureImageUnits;}
     uint32_t getMaxFragmentUniformVectors() const {return mGL.mMaxFragmentUniformVectors;}
@@ -232,6 +233,7 @@
         int32_t mMaxVertexTextureUnits;
 
         bool OES_texture_npot;
+        bool GL_NV_texture_npot_2D_mipmap;
         float EXT_texture_max_aniso;
     } mGL;
 
diff --git a/libs/rs/rsElement.cpp b/libs/rs/rsElement.cpp
index 096115c..d207dcf 100644
--- a/libs/rs/rsElement.cpp
+++ b/libs/rs/rsElement.cpp
@@ -379,7 +379,10 @@
     return (RsElement)e;
 }
 
-void rsi_ElementGetNativeData(Context *rsc, RsElement elem, uint32_t *elemData, uint32_t elemDataSize)
+}
+}
+
+void rsaElementGetNativeData(RsContext con, RsElement elem, uint32_t *elemData, uint32_t elemDataSize)
 {
     rsAssert(elemDataSize == 5);
     // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
@@ -393,7 +396,7 @@
 
 }
 
-void rsi_ElementGetSubElements(Context *rsc, RsElement elem, uint32_t *ids, const char **names, uint32_t dataSize)
+void rsaElementGetSubElements(RsContext con, RsElement elem, uint32_t *ids, const char **names, uint32_t dataSize)
 {
     Element *e = static_cast<Element *>(elem);
     rsAssert(e->getFieldCount() == dataSize);
@@ -405,7 +408,3 @@
     }
 
 }
-
-
-}
-}
diff --git a/libs/rs/rsFileA3D.cpp b/libs/rs/rsFileA3D.cpp
index c90edc2..e4c6dbd 100644
--- a/libs/rs/rsFileA3D.cpp
+++ b/libs/rs/rsFileA3D.cpp
@@ -115,19 +115,10 @@
         return false;
     }
 
-    uint8_t *headerData = (uint8_t *)malloc(headerSize);
-    if(!headerData) {
-        return false;
-    }
-
-    memcpy(headerData, localData, headerSize);
-
     // Now open the stream to parse the header
-    IStream headerStream(headerData, false);
+    IStream headerStream(localData, false);
     parseHeader(&headerStream);
 
-    free(headerData);
-
     localData += headerSize;
     lengthRemaining -= headerSize;
 
@@ -145,13 +136,7 @@
     }
 
     // We should know enough to read the file in at this point.
-    mAlloc = malloc(mDataSize);
-    if (!mAlloc) {
-        return false;
-    }
-    mData = (uint8_t *)mAlloc;
-    memcpy(mAlloc, localData, mDataSize);
-
+    mData = (uint8_t *)localData;
     mReadStream = new IStream(mData, mUse64BitOffsets);
 
     return true;
@@ -383,7 +368,41 @@
 namespace android {
 namespace renderscript {
 
-void rsi_FileA3DGetNumIndexEntries(Context *rsc, int32_t *numEntries, RsFile file)
+RsFile rsi_FileOpen(Context *rsc, char const *path, unsigned int len)
+{
+    FileA3D *fa3d = new FileA3D(rsc);
+
+    FILE *f = fopen("/sdcard/test.a3d", "rb");
+    if (f) {
+        fa3d->load(f);
+        fclose(f);
+        fa3d->incUserRef();
+        return fa3d;
+    }
+    delete fa3d;
+    return NULL;
+}
+
+
+}
+}
+
+RsObjectBase rsaFileA3DGetEntryByIndex(RsContext con, uint32_t index, RsFile file)
+{
+    FileA3D *fa3d = static_cast<FileA3D *>(file);
+    if(!fa3d) {
+        LOGE("Can't load entry. No valid file");
+        return NULL;
+    }
+
+    ObjectBase *obj = fa3d->initializeFromEntry(index);
+    LOGV("Returning object with name %s", obj->getName());
+
+    return obj;
+}
+
+
+void rsaFileA3DGetNumIndexEntries(RsContext con, int32_t *numEntries, RsFile file)
 {
     FileA3D *fa3d = static_cast<FileA3D *>(file);
 
@@ -395,7 +414,7 @@
     }
 }
 
-void rsi_FileA3DGetIndexEntries(Context *rsc, RsFileIndexEntry *fileEntries, uint32_t numEntries, RsFile file)
+void rsaFileA3DGetIndexEntries(RsContext con, RsFileIndexEntry *fileEntries, uint32_t numEntries, RsFile file)
 {
     FileA3D *fa3d = static_cast<FileA3D *>(file);
 
@@ -418,51 +437,17 @@
 
 }
 
-RsObjectBase rsi_FileA3DGetEntryByIndex(Context *rsc, uint32_t index, RsFile file)
-{
-    FileA3D *fa3d = static_cast<FileA3D *>(file);
-    if(!fa3d) {
-        LOGE("Can't load entry. No valid file");
-        return NULL;
-    }
-
-    ObjectBase *obj = fa3d->initializeFromEntry(index);
-    LOGV("Returning object with name %s", obj->getName());
-
-    return obj;
-}
-
-RsFile rsi_FileA3DCreateFromAssetStream(Context *rsc, const void *data, uint32_t len)
+RsFile rsaFileA3DCreateFromAssetStream(RsContext con, const void *data, uint32_t len)
 {
     if (data == NULL) {
         LOGE("File load failed. Asset stream is NULL");
         return NULL;
     }
 
+    Context *rsc = static_cast<Context *>(con);
     FileA3D *fa3d = new FileA3D(rsc);
-
-    fa3d->load(data, len);
     fa3d->incUserRef();
 
+    fa3d->load(data, len);
     return fa3d;
 }
-
-
-RsFile rsi_FileOpen(Context *rsc, char const *path, unsigned int len)
-{
-    FileA3D *fa3d = new FileA3D(rsc);
-
-    FILE *f = fopen("/sdcard/test.a3d", "rb");
-    if (f) {
-        fa3d->load(f);
-        fclose(f);
-        fa3d->incUserRef();
-        return fa3d;
-    }
-    delete fa3d;
-    return NULL;
-}
-
-
-}
-}
diff --git a/libs/rs/rsMesh.cpp b/libs/rs/rsMesh.cpp
index 761be93..fd604e5 100644
--- a/libs/rs/rsMesh.cpp
+++ b/libs/rs/rsMesh.cpp
@@ -310,19 +310,21 @@
     sm->updateGLPrimitives();
 }
 
-void rsi_MeshGetVertexBufferCount(Context *rsc, RsMesh mv, int32_t *numVtx)
+}}
+
+void rsaMeshGetVertexBufferCount(RsContext con, RsMesh mv, int32_t *numVtx)
 {
     Mesh *sm = static_cast<Mesh *>(mv);
     *numVtx = sm->mVertexBufferCount;
 }
 
-void rsi_MeshGetIndexCount(Context *rsc, RsMesh mv, int32_t *numIdx)
+void rsaMeshGetIndexCount(RsContext con, RsMesh mv, int32_t *numIdx)
 {
     Mesh *sm = static_cast<Mesh *>(mv);
     *numIdx = sm->mPrimitivesCount;
 }
 
-void rsi_MeshGetVertices(Context *rsc, RsMesh mv, RsAllocation *vtxData, uint32_t vtxDataCount)
+void rsaMeshGetVertices(RsContext con, RsMesh mv, RsAllocation *vtxData, uint32_t vtxDataCount)
 {
     Mesh *sm = static_cast<Mesh *>(mv);
     rsAssert(vtxDataCount == sm->mVertexBufferCount);
@@ -333,7 +335,7 @@
     }
 }
 
-void rsi_MeshGetIndices(Context *rsc, RsMesh mv, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount)
+void rsaMeshGetIndices(RsContext con, RsMesh mv, RsAllocation *va, uint32_t *primType, uint32_t idxDataCount)
 {
     Mesh *sm = static_cast<Mesh *>(mv);
     rsAssert(idxDataCount == sm->mPrimitivesCount);
@@ -347,8 +349,3 @@
     }
 
 }
-
-
-
-
-}}
diff --git a/libs/rs/rsSampler.cpp b/libs/rs/rsSampler.cpp
index cfae7b2..cbdc407 100644
--- a/libs/rs/rsSampler.cpp
+++ b/libs/rs/rsSampler.cpp
@@ -76,7 +76,11 @@
     };
 
     if (!rsc->ext_OES_texture_npot() && tex->getType()->getIsNp2()) {
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
+        if (tex->getHasGraphicsMipmaps() && rsc->ext_GL_NV_texture_npot_2D_mipmap()) {
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
+        } else {
+            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
+        }
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, transNP[mMagFilter]);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, transNP[mWrapS]);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, transNP[mWrapT]);
diff --git a/libs/rs/rsScriptC_LibCL.cpp b/libs/rs/rsScriptC_LibCL.cpp
index ce8e7b2..1b1a752 100644
--- a/libs/rs/rsScriptC_LibCL.cpp
+++ b/libs/rs/rsScriptC_LibCL.cpp
@@ -205,9 +205,9 @@
     { "_Z5asinhf", (void *)&asinhf },
     { "_Z6asinpif", (void *)&SC_asinpi },
     { "_Z4atanf", (void *)&atanf },
-    { "_Z5atan2f", (void *)&atan2f },
+    { "_Z5atan2ff", (void *)&atan2f },
     { "_Z6atanpif", (void *)&SC_atanpi },
-    { "_Z7atan2pif", (void *)&SC_atan2pi },
+    { "_Z7atan2piff", (void *)&SC_atan2pi },
     { "_Z4cbrtf", (void *)&cbrtf },
     { "_Z4ceilf", (void *)&ceilf },
     { "_Z8copysignff", (void *)&copysignf },
diff --git a/libs/rs/rsType.cpp b/libs/rs/rsType.cpp
index 7ef2464..caaa9f5 100644
--- a/libs/rs/rsType.cpp
+++ b/libs/rs/rsType.cpp
@@ -374,23 +374,6 @@
 namespace android {
 namespace renderscript {
 
-void rsi_TypeGetNativeData(Context *rsc, RsType type, uint32_t *typeData, uint32_t typeDataSize)
-{
-    rsAssert(typeDataSize == 6);
-    // Pack the data in the follofing way mDimX; mDimY; mDimZ;
-    // mDimLOD; mDimFaces; mElement; into typeData
-    Type *t = static_cast<Type *>(type);
-
-    (*typeData++) = t->getDimX();
-    (*typeData++) = t->getDimY();
-    (*typeData++) = t->getDimZ();
-    (*typeData++) = t->getDimLOD();
-    (*typeData++) = t->getDimFaces() ? 1 : 0;
-    (*typeData++) = (uint32_t)t->getElement();
-    t->getElement()->incUserRef();
-}
-
-
 }
 }
 
@@ -424,3 +407,18 @@
     return Type::getType(rsc, e, dimX, dimY, dimZ, dimLOD, dimFaces);
 }
 
+void rsaTypeGetNativeData(RsContext con, RsType type, uint32_t *typeData, uint32_t typeDataSize)
+{
+    rsAssert(typeDataSize == 6);
+    // Pack the data in the follofing way mDimX; mDimY; mDimZ;
+    // mDimLOD; mDimFaces; mElement; into typeData
+    Type *t = static_cast<Type *>(type);
+
+    (*typeData++) = t->getDimX();
+    (*typeData++) = t->getDimY();
+    (*typeData++) = t->getDimZ();
+    (*typeData++) = t->getDimLOD();
+    (*typeData++) = t->getDimFaces() ? 1 : 0;
+    (*typeData++) = (uint32_t)t->getElement();
+    t->getElement()->incUserRef();
+}
diff --git a/media/java/android/media/videoeditor/Effect.java b/media/java/android/media/videoeditor/Effect.java
index f5e6a67..8547e13 100755
--- a/media/java/android/media/videoeditor/Effect.java
+++ b/media/java/android/media/videoeditor/Effect.java
@@ -57,7 +57,7 @@
             throw new IllegalArgumentException("Media item cannot be null");

         }

 

-        if (startTimeMs + durationMs > mediaItem.getTimelineDuration()) {

+        if (startTimeMs + durationMs > mediaItem.getDuration()) {

             throw new IllegalArgumentException("Invalid start time and duration");

         }

 

@@ -81,7 +81,7 @@
      * @param durationMs of the effect in milliseconds

      */

     public void setDuration(long durationMs) {

-        if (mStartTimeMs + durationMs > mMediaItem.getTimelineDuration()) {

+        if (mStartTimeMs + durationMs > mMediaItem.getDuration()) {

             throw new IllegalArgumentException("Duration is too large");

         }

 

@@ -107,7 +107,7 @@
      *            of the media item in milliseconds

      */

     public void setStartTime(long startTimeMs) {

-        if (startTimeMs + mDurationMs > mMediaItem.getTimelineDuration()) {

+        if (startTimeMs + mDurationMs > mMediaItem.getDuration()) {

             throw new IllegalArgumentException("Start time is too large");

         }

 

@@ -130,7 +130,7 @@
      * @param durationMs The duration in milliseconds

      */

     public void setStartTimeAndDuration(long startTimeMs, long durationMs) {

-        if (startTimeMs + durationMs > mMediaItem.getTimelineDuration()) {

+        if (startTimeMs + durationMs > mMediaItem.getDuration()) {

             throw new IllegalArgumentException("Invalid start time or duration");

         }

 

diff --git a/media/java/android/media/videoeditor/MediaImageItem.java b/media/java/android/media/videoeditor/MediaImageItem.java
index d2f3694..6dc36c2 100755
--- a/media/java/android/media/videoeditor/MediaImageItem.java
+++ b/media/java/android/media/videoeditor/MediaImageItem.java
@@ -172,6 +172,14 @@
      * {@inheritDoc}

      */

     @Override

+    public long getDuration() {

+        return mDurationMs;

+    }

+

+    /*

+     * {@inheritDoc}

+     */

+    @Override

     public long getTimelineDuration() {

         return mDurationMs;

     }

diff --git a/media/java/android/media/videoeditor/MediaItem.java b/media/java/android/media/videoeditor/MediaItem.java
index 40d3619..04878f7 100755
--- a/media/java/android/media/videoeditor/MediaItem.java
+++ b/media/java/android/media/videoeditor/MediaItem.java
@@ -174,6 +174,11 @@
     public abstract long getTimelineDuration();

 

     /**

+     * @return The is the full duration of the media item (not trimmed)

+     */

+    public abstract long getDuration();

+

+    /**

      * @return The source file type

      */

     public abstract int getFileType();

@@ -223,7 +228,7 @@
             throw new IllegalArgumentException("Effect already exists: " + effect.getId());

         }

 

-        if (effect.getStartTime() + effect.getDuration() > getTimelineDuration()) {

+        if (effect.getStartTime() + effect.getDuration() > getDuration()) {

             throw new IllegalArgumentException(

                     "Effect start time + effect duration > media clip duration");

         }

@@ -300,7 +305,7 @@
             throw new IllegalArgumentException("Overlay already exists: " + overlay.getId());

         }

 

-        if (overlay.getStartTime() + overlay.getDuration() > getTimelineDuration()) {

+        if (overlay.getStartTime() + overlay.getDuration() > getDuration()) {

             throw new IllegalArgumentException(

                     "Overlay start time + overlay duration > media clip duration");

         }

@@ -455,7 +460,7 @@
         }

 

         if (mEndTransition != null) {

-            if (effect.getStartTime() + effect.getDuration() > getTimelineDuration()

+            if (effect.getStartTime() + effect.getDuration() > getDuration()

                     - mEndTransition.getDuration()) {

                 mEndTransition.invalidate();

             }

@@ -476,7 +481,7 @@
         }

 

         if (mEndTransition != null) {

-            if (overlay.getStartTime() + overlay.getDuration() > getTimelineDuration()

+            if (overlay.getStartTime() + overlay.getDuration() > getDuration()

                     - mEndTransition.getDuration()) {

                 mEndTransition.invalidate();

             }

@@ -511,7 +516,7 @@
         for (Effect effect : effects) {

             // Adjust the start time if necessary

             final long effectStartTimeMs;

-            if (effect.getStartTime() > getTimelineDuration()) {

+            if (effect.getStartTime() > getDuration()) {

                 effectStartTimeMs = 0;

             } else {

                 effectStartTimeMs = effect.getStartTime();

@@ -519,8 +524,8 @@
 

             // Adjust the duration if necessary

             final long effectDurationMs;

-            if (effectStartTimeMs + effect.getDuration() > getTimelineDuration()) {

-                effectDurationMs = getTimelineDuration() - effectStartTimeMs;

+            if (effectStartTimeMs + effect.getDuration() > getDuration()) {

+                effectDurationMs = getDuration() - effectStartTimeMs;

             } else {

                 effectDurationMs = effect.getDuration();

             }

@@ -540,7 +545,7 @@
         for (Overlay overlay : overlays) {

             // Adjust the start time if necessary

             final long overlayStartTimeMs;

-            if (overlay.getStartTime() > getTimelineDuration()) {

+            if (overlay.getStartTime() > getDuration()) {

                 overlayStartTimeMs = 0;

             } else {

                 overlayStartTimeMs = overlay.getStartTime();

@@ -548,8 +553,8 @@
 

             // Adjust the duration if necessary

             final long overlayDurationMs;

-            if (overlayStartTimeMs + overlay.getDuration() > getTimelineDuration()) {

-                overlayDurationMs = getTimelineDuration() - overlayStartTimeMs;

+            if (overlayStartTimeMs + overlay.getDuration() > getDuration()) {

+                overlayDurationMs = getDuration() - overlayStartTimeMs;

             } else {

                 overlayDurationMs = overlay.getDuration();

             }

diff --git a/media/java/android/media/videoeditor/MediaVideoItem.java b/media/java/android/media/videoeditor/MediaVideoItem.java
index f09219d..1fa98e7 100755
--- a/media/java/android/media/videoeditor/MediaVideoItem.java
+++ b/media/java/android/media/videoeditor/MediaVideoItem.java
@@ -243,16 +243,16 @@
         return mHeight;

     }

 

-    /**

-     * @return The duration of the video clip

+    /*

+     * {@inheritDoc}

      */

+    @Override

     public long getDuration() {

         return mDurationMs;

     }

 

-    /**

-     * @return The timeline duration. This is the actual duration in the

-     *      timeline (trimmed duration)

+    /*

+     * {@inheritDoc}

      */

     @Override

     public long getTimelineDuration() {

diff --git a/media/java/android/media/videoeditor/Overlay.java b/media/java/android/media/videoeditor/Overlay.java
index c58b5cb..fff9ca2 100755
--- a/media/java/android/media/videoeditor/Overlay.java
+++ b/media/java/android/media/videoeditor/Overlay.java
@@ -60,7 +60,7 @@
             throw new IllegalArgumentException("Media item cannot be null");

         }

 

-        if (startTimeMs + durationMs > mediaItem.getTimelineDuration()) {

+        if (startTimeMs + durationMs > mediaItem.getDuration()) {

             throw new IllegalArgumentException("Invalid start time and duration");

         }

 

@@ -92,7 +92,7 @@
      * @param durationMs The duration in milliseconds

      */

     public void setDuration(long durationMs) {

-        if (mStartTimeMs + durationMs > mMediaItem.getTimelineDuration()) {

+        if (mStartTimeMs + durationMs > mMediaItem.getDuration()) {

             throw new IllegalArgumentException("Duration is too large");

         }

 

@@ -116,7 +116,7 @@
      * @param startTimeMs start time in milliseconds

      */

     public void setStartTime(long startTimeMs) {

-        if (startTimeMs + mDurationMs > mMediaItem.getTimelineDuration()) {

+        if (startTimeMs + mDurationMs > mMediaItem.getDuration()) {

             throw new IllegalArgumentException("Start time is too large");

         }

 

@@ -132,7 +132,7 @@
      * @param durationMs The duration in milliseconds

      */

     public void setStartTimeAndDuration(long startTimeMs, long durationMs) {

-        if (startTimeMs + durationMs > mMediaItem.getTimelineDuration()) {

+        if (startTimeMs + durationMs > mMediaItem.getDuration()) {

             throw new IllegalArgumentException("Invalid start time or duration");

         }

 

diff --git a/media/libstagefright/matroska/MatroskaExtractor.cpp b/media/libstagefright/matroska/MatroskaExtractor.cpp
index 7c7d69e..d16476d 100644
--- a/media/libstagefright/matroska/MatroskaExtractor.cpp
+++ b/media/libstagefright/matroska/MatroskaExtractor.cpp
@@ -252,7 +252,7 @@
 }
 
 void BlockIterator::seek(int64_t seekTimeUs) {
-    mCluster = mSegment->GetCluster(seekTimeUs * 1000ll);
+    mCluster = mSegment->FindCluster(seekTimeUs * 1000ll);
     mBlockEntry = mCluster != NULL ? mCluster->GetFirst() : NULL;
 
     while (!eos() && block()->GetTrackNumber() != mTrackNum) {
@@ -476,7 +476,7 @@
 
         size_t codecPrivateSize;
         const unsigned char *codecPrivate =
-            track->GetCodecPrivate(&codecPrivateSize);
+            track->GetCodecPrivate(codecPrivateSize);
 
         enum { VIDEO_TRACK = 1, AUDIO_TRACK = 2 };
 
diff --git a/media/libstagefright/matroska/mkvparser.cpp b/media/libstagefright/matroska/mkvparser.cpp
index 4e51004..455b1d6 100644
--- a/media/libstagefright/matroska/mkvparser.cpp
+++ b/media/libstagefright/matroska/mkvparser.cpp
@@ -1,3103 +1,4511 @@
-#include "mkvparser.hpp"
-#include <cassert>
-#include <cstring>
-
-mkvparser::IMkvReader::~IMkvReader()
-{
-}
-
-long long mkvparser::ReadUInt(IMkvReader* pReader, long long pos, long& len)
-{
-    assert(pReader);
-    assert(pos >= 0);
-    
-    long long total, available;
-
-    long hr = pReader->Length(&total, &available);
-    assert(hr >= 0);
-    assert(pos < available);
-    assert((available - pos) >= 1);  //assume here max u-int len is 8
-    
-    unsigned char b;
-
-    hr = pReader->Read(pos, 1, &b);
-    if (hr < 0)
-        return hr;
-        
-    assert(hr == 0L);
-    
-    if (b & 0x80)       //1000 0000
-    {
-        len = 1;
-        b &= 0x7F;      //0111 1111
-    }        
-    else if (b & 0x40)  //0100 0000
-    {
-        len = 2;
-        b &= 0x3F;      //0011 1111
-    }
-    else if (b & 0x20)  //0010 0000
-    {
-        len = 3;
-        b &= 0x1F;      //0001 1111
-    }
-    else if (b & 0x10)  //0001 0000
-    {
-        len = 4;
-        b &= 0x0F;      //0000 1111
-    }
-    else if (b & 0x08)  //0000 1000
-    {
-        len = 5;
-        b &= 0x07;      //0000 0111
-    }
-    else if (b & 0x04)  //0000 0100
-    {
-        len = 6;
-        b &= 0x03;      //0000 0011
-    }
-    else if (b & 0x02)  //0000 0010
-    {
-        len = 7;
-        b &= 0x01;      //0000 0001
-    }
-    else 
-    {
-        assert(b & 0x01);  //0000 0001
-        len = 8;
-        b = 0;             //0000 0000
-    }
-    
-    assert((available - pos) >= len);
-    
-    long long result = b;
-    ++pos;
-    for (long i = 1; i < len; ++i)
-    {
-        hr = pReader->Read(pos, 1, &b);
-        
-        if (hr < 0)
-            return hr;
-            
-        assert(hr == 0L);
-        
-        result <<= 8;
-        result |= b;
-        
-        ++pos;
-    }
-    
-    return result;
-}
-    
-    
-long long mkvparser::GetUIntLength(
-    IMkvReader* pReader,
-    long long pos, 
-    long& len)
-{
-    assert(pReader);
-    assert(pos >= 0);
-    
-    long long total, available;
-
-    long hr = pReader->Length(&total, &available);
-    assert(hr >= 0);
-    assert(available <= total);
-    
-    if (pos >= available)
-        return pos;  //too few bytes available
-    
-    unsigned char b;
-    
-    hr = pReader->Read(pos, 1, &b);
-    
-    if (hr < 0)
-        return hr;
-
-    assert(hr == 0L);
-    
-    if (b == 0)  //we can't handle u-int values larger than 8 bytes
-        return E_FILE_FORMAT_INVALID;
-    
-    unsigned char m = 0x80;
-    len = 1;
-    
-    while (!(b & m))
-    {
-        m >>= 1;
-        ++len;
-    }
-    
-    return 0;  //success
-}
-
-
-long long mkvparser::SyncReadUInt(
-    IMkvReader* pReader,
-    long long pos, 
-    long long stop,
-    long& len)
-{
-    assert(pReader);
-
-    if (pos >= stop)
-        return E_FILE_FORMAT_INVALID;
-    
-    unsigned char b;
-    
-    long hr = pReader->Read(pos, 1, &b);
-    
-    if (hr < 0)
-        return hr;
-        
-    if (hr != 0L)
-        return E_BUFFER_NOT_FULL;
-
-    if (b == 0)  //we can't handle u-int values larger than 8 bytes
-        return E_FILE_FORMAT_INVALID;
-    
-    unsigned char m = 0x80;
-    len = 1;
-        
-    while (!(b & m))
-    {
-        m >>= 1;
-        ++len;
-    }
-    
-    if ((pos + len) > stop)
-        return E_FILE_FORMAT_INVALID;
-        
-    long long result = b & (~m);
-    ++pos;
-    
-    for (int i = 1; i < len; ++i)
-    {
-        hr = pReader->Read(pos, 1, &b);
-        
-        if (hr < 0)
-            return hr;
-           
-        if (hr != 0L)
-            return E_BUFFER_NOT_FULL;
-            
-        result <<= 8;
-        result |= b;
-        
-        ++pos;
-    }
-    
-    return result;
-}
-
-
-long long mkvparser::UnserializeUInt(
-    IMkvReader* pReader, 
-    long long pos,
-    long long size)
-{
-    assert(pReader);
-    assert(pos >= 0);
-    assert(size > 0);
-    assert(size <= 8);
-    
-    long long result = 0;
-    
-    for (long long i = 0; i < size; ++i)
-    {
-        unsigned char b;
-        
-        const long hr = pReader->Read(pos, 1, &b);
-        
-        if (hr < 0)      
-            return hr;
-        result <<= 8;
-        result |= b;
-        
-        ++pos;
-    }
-    
-    return result;
-}
-
-
-float mkvparser::Unserialize4Float(
-    IMkvReader* pReader, 
-    long long pos)
-{
-    assert(pReader);
-    assert(pos >= 0);
-    
-    long long total, available;
-    
-    long hr = pReader->Length(&total, &available);
-    assert(hr >= 0);
-    assert(available <= total);
-    assert((pos + 4) <= available);
-    
-    float result;
-    
-    unsigned char* const p = (unsigned char*)&result;
-    unsigned char* q = p + 4;
-    
-    for (;;)
-    {
-        hr = pReader->Read(pos, 1, --q);
-        assert(hr == 0L);
-        
-        if (q == p)
-            break;
-            
-        ++pos;
-    }
-    
-    return result;
-}
-
-
-double mkvparser::Unserialize8Double(
-    IMkvReader* pReader, 
-    long long pos)
-{
-    assert(pReader);
-    assert(pos >= 0);
-    
-    double result;
-    
-    unsigned char* const p = (unsigned char*)&result;
-    unsigned char* q = p + 8;
-    
-    for (;;)
-    {
-        const long hr = pReader->Read(pos, 1, --q);
-        assert(hr == 0L);
-        
-        if (q == p)
-            break;
-            
-        ++pos;
-    }
-    
-    return result;
-}
-
-signed char mkvparser::Unserialize1SInt(
-    IMkvReader* pReader,
-    long long pos)
-{
-    assert(pReader);
-    assert(pos >= 0);
- 
-    long long total, available;
-
-    long hr = pReader->Length(&total, &available);
-    assert(hr == 0);   
-    assert(available <= total);
-    assert(pos < available);
-
-    signed char result;
-
-    hr = pReader->Read(pos, 1, (unsigned char*)&result);
-    assert(hr == 0);
-
-    return result;
-}
-
-short mkvparser::Unserialize2SInt(
-    IMkvReader* pReader, 
-    long long pos)
-{
-    assert(pReader);
-    assert(pos >= 0);
-    
-    long long total, available;
-    
-    long hr = pReader->Length(&total, &available);
-    assert(hr >= 0);
-    assert(available <= total);
-    assert((pos + 2) <= available);
-    
-    short result;
-    
-    unsigned char* const p = (unsigned char*)&result;
-    unsigned char* q = p + 2;
-    
-    for (;;)
-    {
-        hr = pReader->Read(pos, 1, --q);
-        assert(hr == 0L);
-        
-        if (q == p)
-            break;
-            
-        ++pos;
-    }
-    
-    return result;
-}
-
-
-bool mkvparser::Match(
-    IMkvReader* pReader,
-    long long& pos,
-    unsigned long id_,
-    long long& val)
-
-{
-    assert(pReader);
-    assert(pos >= 0);
-    
-    long long total, available;
-
-    long hr = pReader->Length(&total, &available);
-    assert(hr >= 0);
-    assert(available <= total);
-    
-    long len;
-
-    const long long id = ReadUInt(pReader, pos, len);
-    assert(id >= 0);
-    assert(len > 0);
-    assert(len <= 8);
-    assert((pos + len) <= available);
-    
-    if ((unsigned long)id != id_)
-        return false;
-        
-    pos += len;  //consume id
-    
-    const long long size = ReadUInt(pReader, pos, len);
-    assert(size >= 0);
-    assert(size <= 8);
-    assert(len > 0);
-    assert(len <= 8);
-    assert((pos + len) <= available);
-    
-    pos += len;  //consume length of size of payload
-    
-    val = UnserializeUInt(pReader, pos, size);
-    assert(val >= 0);
-    
-    pos += size;  //consume size of payload
-    
-    return true;
-}
-
-bool mkvparser::Match(
-    IMkvReader* pReader,
-    long long& pos,
-    unsigned long id_,
-    char*& val)
-{
-    assert(pReader);
-    assert(pos >= 0);
-    
-    long long total, available;
-
-    long hr = pReader->Length(&total, &available);
-    assert(hr >= 0);
-    assert(available <= total);
-    
-    long len;
-
-    const long long id = ReadUInt(pReader, pos, len);
-    assert(id >= 0);
-    assert(len > 0);
-    assert(len <= 8);
-    assert((pos + len) <= available);
-    
-    if ((unsigned long)id != id_)
-        return false;
-    
-    pos += len;  //consume id
-    
-    const long long size_ = ReadUInt(pReader, pos, len);
-    assert(size_ >= 0);
-    assert(len > 0);
-    assert(len <= 8);
-    assert((pos + len) <= available);
-    
-    pos += len;  //consume length of size of payload
-    assert((pos + size_) <= available);
-
-    const size_t size = static_cast<size_t>(size_);    
-    val = new char[size+1];
-
-    for (size_t i = 0; i < size; ++i)
-    {
-        char c;
-
-        hr = pReader->Read(pos + i, 1, (unsigned char*)&c);
-        assert(hr == 0L);
-            
-        val[i] = c;
-   
-        if (c == '\0')
-            break;     
-   
-    }
-
-    val[size] = '\0';
-    pos += size_;  //consume size of payload
-    
-    return true;
-}
-
-#if 0
-bool mkvparser::Match(
-    IMkvReader* pReader,
-    long long& pos,
-    unsigned long id,
-    wchar_t*& val)
-{
-    char* str;
-    
-    if (!Match(pReader, pos, id, str))
-        return false;
-
-    const size_t size = mbstowcs(NULL, str, 0);
-       
-    if (size == 0) 
-        val = NULL;
-    else 
-    { 
-        val = new wchar_t[size+1];
-        mbstowcs(val, str, size);
-        val[size] = L'\0';
-    }
-
-    delete[] str;
-    return true;    
-}
-#endif
-
-
-bool mkvparser::Match(
-    IMkvReader* pReader,
-    long long& pos,
-    unsigned long id_,
-    unsigned char*& val,
-    size_t *optionalSize)
-{
-    assert(pReader);
-    assert(pos >= 0);
-    
-    long long total, available;
-
-    long hr = pReader->Length(&total, &available);
-    assert(hr >= 0);
-    assert(available <= total);
-    
-    long len;
-    const long long id = ReadUInt(pReader, pos, len);
-    assert(id >= 0);
-    assert(len > 0);
-    assert(len <= 8);
-    assert((pos + len) <= available);
-    
-    if ((unsigned long)id != id_)
-        return false;
-        
-    pos += len;  //consume id
-    
-    const long long size_ = ReadUInt(pReader, pos, len);
-    assert(size_ >= 0);
-    assert(len > 0);
-    assert(len <= 8);
-    assert((pos + len) <= available);
-    
-    pos += len;  //consume length of size of payload
-    assert((pos + size_) <= available);
-
-    const size_t size = static_cast<size_t>(size_);    
-    val = new unsigned char[size];
- 
-    if (optionalSize) {
-        *optionalSize = size;
-    }
-
-    for (size_t i = 0; i < size; ++i)
-    {
-        unsigned char b;
-
-        hr = pReader->Read(pos + i, 1, &b);
-        assert(hr == 0L);
-
-        val[i] = b; 
-    }
-    
-    pos += size_;  //consume size of payload    
-    return true;
-}
-
-
-bool mkvparser::Match(
-    IMkvReader* pReader,
-    long long& pos,
-    unsigned long id_,
-    double& val)
-{
-    assert(pReader);
-    assert(pos >= 0);
-    
-    long long total, available;
-
-    long hr = pReader->Length(&total, &available);
-    assert(hr >= 0);
-    assert(available <= total);
-    long idlen;
-    const long long id = ReadUInt(pReader, pos, idlen);
-    assert(id >= 0);  //TODO
-    
-    if ((unsigned long)id != id_)
-        return false;
-
-    long sizelen;
-    const long long size = ReadUInt(pReader, pos + idlen, sizelen);
-
-    switch (size)
-    {	
-        case 4:
-        case 8:
-            break;
-        default:
-            return false;
-    }
-
-    pos += idlen + sizelen;  //consume id and size fields
-    assert((pos + size) <= available);
-
-    if (size == 4)
-        val = Unserialize4Float(pReader, pos);
-    else
-    {
-        assert(size == 8);
-        val = Unserialize8Double(pReader, pos);
-    }
-    
-    pos += size;  //consume size of payload
-    
-    return true;
-}
-
-
-bool mkvparser::Match(
-    IMkvReader* pReader,
-    long long& pos,
-    unsigned long id_,
-    short& val)
-{
-    assert(pReader);
-    assert(pos >= 0);
-    
-    long long total, available;
-
-    long hr = pReader->Length(&total, &available);
-    assert(hr >= 0);
-    assert(available <= total);
-    
-    long len;
-    const long long id = ReadUInt(pReader, pos, len);
-    assert(id >= 0);
-    assert((pos + len) <= available);
-    
-    if ((unsigned long)id != id_)
-        return false;
-        
-    pos += len;  //consume id
-    
-    const long long size = ReadUInt(pReader, pos, len);
-    assert(size <= 2);
-    assert((pos + len) <= available);
-   
-    pos += len;  //consume length of size of payload
-    assert((pos + size) <= available);
-
-    //TODO:
-    // Generalize this to work for any size signed int
-    if (size == 1)
-        val = Unserialize1SInt(pReader, pos);
-    else 
-        val = Unserialize2SInt(pReader, pos);
-        
-    pos += size;  //consume size of payload
-    
-    return true;
-}
-
-
-namespace mkvparser
-{
-
-EBMLHeader::EBMLHeader():
-    m_docType(NULL)
-{
-}
-
-EBMLHeader::~EBMLHeader()
-{
-    delete[] m_docType;
-}
-
-long long EBMLHeader::Parse(
-    IMkvReader* pReader,
-    long long& pos)
-{
-    assert(pReader);
-    
-    long long total, available;
-    
-    long hr = pReader->Length(&total, &available);
-    
-    if (hr < 0) 
-        return hr;
-    
-    pos = 0;    
-    long long end = (1024 < available)? 1024: available;    
-
-    for (;;)
-    {    
-        unsigned char b = 0;
-    
-        while (pos < end)
-        {
-            hr = pReader->Read(pos, 1, &b);
-           
-            if (hr < 0)
-                return hr;
-            
-            if (b == 0x1A)
-                break;
-                
-            ++pos;
-        }
-    
-        if (b != 0x1A)
-        {
-            if ((pos >= 1024) ||
-                (available >= total) || 
-                ((total - available) < 5))
-                  return -1;
-                
-            return available + 5;  //5 = 4-byte ID + 1st byte of size
-        }
-    
-        if ((total - pos) < 5)
-            return E_FILE_FORMAT_INVALID;
-            
-        if ((available - pos) < 5)
-            return pos + 5;  //try again later
-
-        long len;            
-
-        const long long result = ReadUInt(pReader, pos, len);
-        
-        if (result < 0)  //error
-            return result;
-            
-        if (result == 0x0A45DFA3)  //ReadId masks-off length indicator bits
-        {
-            assert(len == 4);
-            pos += len;
-            break;
-        }
-
-        ++pos;  //throw away just the 0x1A byte, and try again
-    }
-        
-    long len;
-    long long result = GetUIntLength(pReader, pos, len);
-    
-    if (result < 0)  //error
-        return result;
-        
-    if (result > 0)  //need more data
-        return result;
-        
-    assert(len > 0);
-    assert(len <= 8);
-    
-    if ((total -  pos) < len)
-        return E_FILE_FORMAT_INVALID;
-    if ((available - pos) < len)
-        return pos + len;  //try again later
-        
-    result = ReadUInt(pReader, pos, len);
-    
-    if (result < 0)  //error
-        return result;
-        
-    pos += len;  //consume u-int
-    
-    if ((total - pos) < result)
-        return E_FILE_FORMAT_INVALID;
-
-    if ((available - pos) < result)
-        return pos + result;
-        
-    end = pos + result;
-    
-    m_version = 1;
-    m_readVersion = 1;
-    m_maxIdLength = 4;
-    m_maxSizeLength = 8;
-    m_docTypeVersion = 1;
-    m_docTypeReadVersion = 1;
-
-    while (pos < end)
-    {
-        if (Match(pReader, pos, 0x0286, m_version))   
-            ;
-        else if (Match(pReader, pos, 0x02F7, m_readVersion))        
-            ;
-        else if (Match(pReader, pos, 0x02F2, m_maxIdLength))        
-            ;
-        else if (Match(pReader, pos, 0x02F3, m_maxSizeLength))      
-            ;
-        else if (Match(pReader, pos, 0x0282, m_docType))            
-            ; 
-        else if (Match(pReader, pos, 0x0287, m_docTypeVersion))     
-            ;
-        else if (Match(pReader, pos, 0x0285, m_docTypeReadVersion)) 
-            ;
-        else
-        {
-            result = ReadUInt(pReader, pos, len);
-            assert(result > 0);
-            assert(len > 0);
-            assert(len <= 8);
-        
-            pos += len;
-            assert(pos < end);
-            
-            result = ReadUInt(pReader, pos, len);
-            assert(result >= 0);
-            assert(len > 0);
-            assert(len <= 8);
-            
-            pos += len + result;
-            assert(pos <= end);
-        }
-    }
-    
-    assert(pos == end);
-        
-    return 0;    
-}
-
-
-Segment::Segment(
-    IMkvReader* pReader,
-    long long start,
-    long long size) :
-    m_pReader(pReader),
-    m_start(start),
-    m_size(size),
-    m_pos(start),
-    m_pInfo(NULL),
-    m_pTracks(NULL),
-    m_clusterCount(0)
-    //m_clusterNumber(0)
-{
-}
-
-
-Segment::~Segment()
-{
-    Cluster** i = m_clusters;
-    Cluster** j = m_clusters + m_clusterCount;
-
-    while (i != j)
-    {
-        Cluster* p = *i++;
-        assert(p);		
-        delete p;
-    } 
-    
-    delete[] m_clusters;
-       
-    delete m_pTracks;
-    delete m_pInfo;
-}
-
-
-long long Segment::CreateInstance(
-    IMkvReader* pReader,
-    long long pos,
-    Segment*& pSegment)
-{
-    assert(pReader);
-    assert(pos >= 0);
-    
-    pSegment = NULL;
-    
-    long long total, available;
-    
-    long hr = pReader->Length(&total, &available);
-    assert(hr >= 0);
-    assert(available <= total);
-    
-    //I would assume that in practice this loop would execute
-    //exactly once, but we allow for other elements (e.g. Void)
-    //to immediately follow the EBML header.  This is fine for
-    //the source filter case (since the entire file is available),
-    //but in the splitter case over a network we should probably
-    //just give up early.  We could for example decide only to
-    //execute this loop a maximum of, say, 10 times.
-    
-    while (pos < total)
-    {    
-        //Read ID
-        
-        long len;
-        long long result = GetUIntLength(pReader, pos, len);
-        
-        if (result)  //error, or too few available bytes
-            return result;
-            
-        if ((pos + len) > total)
-            return E_FILE_FORMAT_INVALID;
-            
-        if ((pos + len) > available)
-            return pos + len;
-
-        //TODO: if we liberalize the behavior of ReadUInt, we can
-        //probably eliminate having to use GetUIntLength here.
-        const long long id = ReadUInt(pReader, pos, len);
-        
-        if (id < 0)  //error
-            return id;
-            
-        pos += len;  //consume ID
-        
-        //Read Size
-        
-        result = GetUIntLength(pReader, pos, len);
-        
-        if (result)  //error, or too few available bytes
-            return result;
-            
-        if ((pos + len) > total)
-            return E_FILE_FORMAT_INVALID;
-            
-        if ((pos + len) > available)
-            return pos + len;
-
-        //TODO: if we liberalize the behavior of ReadUInt, we can
-        //probably eliminate having to use GetUIntLength here.
-        const long long size = ReadUInt(pReader, pos, len);
-        
-        if (size < 0)
-            return size;
-            
-        pos += len;  //consume length of size of element
-        
-        //Pos now points to start of payload
-        
-        if ((pos + size) > total)
-            return E_FILE_FORMAT_INVALID;
-        
-        if (id == 0x08538067)  //Segment ID
-        {
-            pSegment = new  Segment(pReader, pos, size); 
-            assert(pSegment);  //TODO   
-
-            return 0;    //success
-        }
-        
-        pos += size;  //consume payload
-    }
-    
-    assert(pos == total);
-    
-    pSegment = new Segment(pReader, pos, 0); 
-    assert(pSegment);  //TODO   
-
-    return 0;  //success (sort of)
-}
-
-
-long long Segment::ParseHeaders()
-{
-    //Outermost (level 0) segment object has been constructed, 
-    //and pos designates start of payload.  We need to find the
-    //inner (level 1) elements.
-    long long total, available;
-    
-    long hr = m_pReader->Length(&total, &available);
-    assert(hr >= 0);
-    assert(available <= total);
-    
-    const long long stop = m_start + m_size;
-    assert(stop <= total);
-    assert(m_pos <= stop);
-    
-    bool bQuit = false;
-    while ((m_pos < stop) && !bQuit)
-    {
-        long long pos = m_pos;
-        
-        long len;
-        long long result = GetUIntLength(m_pReader, pos, len);
-        
-        if (result)  //error, or too few available bytes
-            return result;
-            
-        if ((pos + len) > stop)
-            return E_FILE_FORMAT_INVALID;
-            
-        if ((pos + len) > available)
-            return pos + len;
-            
-        const long long idpos = pos;
-        const long long id = ReadUInt(m_pReader, idpos, len);
-        
-        if (id < 0)  //error
-            return id;
-            
-        pos += len;  //consume ID
-        
-        //Read Size
-        result = GetUIntLength(m_pReader, pos, len);
-        
-        if (result)  //error, or too few available bytes
-            return result;
-            
-        if ((pos + len) > stop)
-            return E_FILE_FORMAT_INVALID;
-            
-        if ((pos + len) > available)
-            return pos + len;
-
-        const long long size = ReadUInt(m_pReader, pos, len);
-        
-        if (size < 0)
-            return size;
-            
-        pos += len;  //consume length of size of element
-        
-        //Pos now points to start of payload
-        
-        if ((pos + size) > stop)
-            return E_FILE_FORMAT_INVALID;
-            
-        //We read EBML elements either in total or nothing at all.
-            
-        if ((pos + size) > available)
-            return pos + size;
-        
-        if (id == 0x0549A966)  //Segment Info ID
-        {
-            assert(m_pInfo == NULL);
-            m_pInfo = new  SegmentInfo(this, pos, size);
-            assert(m_pInfo);  //TODO
-            
-            if (m_pTracks)
-                bQuit = true;
-        }
-        else if (id == 0x0654AE6B)  //Tracks ID
-        {
-            assert(m_pTracks == NULL);
-            m_pTracks = new  Tracks(this, pos, size);
-            assert(m_pTracks);  //TODO
-            
-            if (m_pInfo)
-                bQuit = true;
-        }
-        else if (id == 0x0F43B675)  //Cluster ID
-        {
-#if 0
-            if (m_pInfo == NULL)  //TODO: liberalize
-                ;  
-            else if (m_pTracks == NULL)
-                ;
-            else
-                //ParseCluster(idpos, pos, size);            
-                Cluster::Parse(this, m_clusters, pos, size);
-#endif
-            bQuit = true;
-        }
-        
-        m_pos = pos + size;  //consume payload
-    }
-    
-    assert(m_pos <= stop);
-    
-    return 0;  //success
-}
-
-
-long Segment::ParseCluster(Cluster*& pCluster, long long& pos_) const
-{
-    pCluster = NULL;
-    pos_ = -1;
-    
-    const long long stop = m_start + m_size;
-    assert(m_pos <= stop);
-    
-    long long pos = m_pos;
-    long long off = -1;
-   
- 
-    while (pos < stop)
-    {
-        long len;
-        const long long idpos = pos;
-        
-        const long long id = SyncReadUInt(m_pReader, pos, stop, len);
-        
-        if (id < 0)  //error
-            return static_cast<long>(id);
-            
-        if (id == 0)
-            return E_FILE_FORMAT_INVALID;
-            
-        pos += len;  //consume id        
-        assert(pos < stop);
-
-        const long long size = SyncReadUInt(m_pReader, pos, stop, len);
-        
-        if (size < 0)  //error
-            return static_cast<long>(size);
-            
-        pos += len;  //consume size
-        assert(pos <= stop);
-            
-        if (size == 0)  //weird
-            continue;
-            
-        //pos now points to start of payload
-            
-        pos += size;  //consume payload
-        assert(pos <= stop);
-
-        if (off >= 0)
-        {
-            pos_ = idpos;
-            break;
-        }
-
-        if (id == 0x0F43B675)  //Cluster ID
-            off = idpos - m_start;
-    }
-    
-    Segment* const this_ = const_cast<Segment*>(this);
-    const size_t idx = m_clusterCount;
-    
-    if (pos >= stop)
-    {
-        pos_ = stop;
-        
-#if 0        
-        if (off < 0)
-        {
-            pCluster = Cluster::CreateEndOfStream(this_, idx);
-            return 1L;
-        }
-#else
-        if (off < 0)
-            return 1L;
-#endif
-                
-        //Reading 0 bytes at pos might work too -- it would depend 
-        //on how the reader is implemented.
-        
-        unsigned char b;
-
-        const long hr = m_pReader->Read(pos - 1, 1, &b);
-        
-        if (hr < 0)
-            return hr;
-            
-        if (hr != 0L)
-            return E_BUFFER_NOT_FULL;
-    }
-    
-    assert(off >= 0);
-    assert(pos_ >= m_start);
-    assert(pos_ <= stop);
-
-    pCluster = Cluster::Parse(this_, idx, off);
-    return 0L;
-}
-
-
-bool Segment::AddCluster(Cluster* pCluster, long long pos)
-{
-    assert(pos >= m_start);
-    
-    const long long stop = m_start + m_size;
-    assert(pos <= stop);
-
-    if (pCluster)    
-        m_clusters[pos] = pCluster;
-        
-    m_pos = pos;  //m_pos >= stop is now we know we have all clusters
-    
-    return (pos >= stop);
-}
-
-
-long Segment::Load()
-{
-    //Outermost (level 0) segment object has been constructed, 
-    //and pos designates start of payload.  We need to find the
-    //inner (level 1) elements.
-    const long long stop = m_start + m_size;
-#ifdef _DEBUG
-    {
-        long long total, available;
-        
-        long hr = m_pReader->Length(&total, &available);
-        assert(hr >= 0);
-        assert(available >= total);
-        assert(stop <= total);
-    }
-#endif
-    long long index = m_pos;
-    
-    m_clusterCount = 0;
-
-    while (index < stop)
-    {
-        long len = 0;
-
-        long long result = GetUIntLength(m_pReader, index, len);
-       
-        if (result < 0)  //error
-            return static_cast<long>(result);
-            
-        if ((index + len) > stop)
-            return E_FILE_FORMAT_INVALID;
-            
-        const long long idpos = index;
-        const long long id = ReadUInt(m_pReader, idpos, len);
-        
-        if (id < 0)  //error
-            return static_cast<long>(id);
-            
-        index += len;  //consume ID
-        
-        //Read Size
-        result = GetUIntLength(m_pReader, index, len);
-        
-        if (result < 0)  //error
-            return static_cast<long>(result);
-            
-        if ((index + len) > stop)
-            return E_FILE_FORMAT_INVALID;
-            
-        const long long size = ReadUInt(m_pReader, index, len);
-        
-        if (size < 0)  //error
-            return static_cast<long>(size);
-            
-        index += len;  //consume length of size of element
- 
-        if (id == 0x0F43B675) // Cluster ID 
-            break;
-	
-        if (id == 0x014D9B74) // SeekHead ID 
-        {
-            ParseSeekHead(index, size, NULL); 
-            break;
-        }
-        index += size;
-    }
-        
-    if (m_clusterCount == 0)
-        return -1L;
-
-    while (m_pos < stop)
-    {
-        long long pos = m_pos;
-        
-        long len;
-
-        long long result = GetUIntLength(m_pReader, pos, len);
-        
-        if (result < 0)  //error
-            return static_cast<long>(result);
-            
-        if ((pos + len) > stop)
-            return E_FILE_FORMAT_INVALID;
-            
-        const long long idpos = pos;
-        const long long id = ReadUInt(m_pReader, idpos, len);
-        
-        if (id < 0)  //error
-            return static_cast<long>(id);
-            
-        pos += len;  //consume ID
-        
-        //Read Size
-        result = GetUIntLength(m_pReader, pos, len);
-        
-        if (result < 0)  //error
-            return static_cast<long>(result);
-            
-        if ((pos + len) > stop)
-	        return E_FILE_FORMAT_INVALID;
-            
-        const long long size = ReadUInt(m_pReader, pos, len);
-       
-        if (size < 0)  //error
-            return static_cast<long>(size);
-            
-        pos += len;  //consume length of size of element
-        
-        //Pos now points to start of payload
-        
-        if ((pos + size) > stop)
-            return E_FILE_FORMAT_INVALID;
-            
-        if (id == 0x0F43B675)  //Cluster ID
-            break;
-
-        if (id == 0x014D9B74)  //SeekHead ID
-        {
-            m_clusters = new Cluster*[m_clusterCount];   
-            size_t index = 0;
-            
-            ParseSeekHead(pos, size, &index);            
-            assert(index == m_clusterCount);
-        }            
-        else if (id == 0x0549A966)  //Segment Info ID
-        {
-            assert(m_pInfo == NULL);
-            m_pInfo = new  SegmentInfo(this, pos, size);
-            assert(m_pInfo);  //TODO
-        }
-        else if (id == 0x0654AE6B)  //Tracks ID
-        {
-            assert(m_pTracks == NULL);
-            m_pTracks = new Tracks(this, pos, size);
-            assert(m_pTracks);  //TODO
-        }
-
-        m_pos = pos + size;  //consume payload
-    }
-    
-    assert(m_clusters);
-    
-    //TODO: see notes above.  This check is here (temporarily) to ensure
-    //that the first seekhead has entries for the clusters (because that's
-    //when they're loaded).  In case we are given a file that lists the
-    //clusters in a second seekhead, the worst thing that happens is that
-    //we treat this as an invalid file (which is better then simply
-    //asserting somewhere).  But that's only a work-around.  What we need
-    //to do is be able to handle having multiple seekheads, and having
-    //clusters listed somewhere besides the first seekhead.
-    //    
-    //if (m_clusters == NULL)
-    //    return E_FILE_FORMAT_INVALID;
-        
-    //NOTE: we stop parsing when we reach the first cluster, under the
-    //assumption all clusters are named in some SeekHead.  Clusters
-    //will have been (pre)loaded, so we indicate that we have all clusters
-    //by adjusting the parse position:
-    m_pos = stop;  //means "we have all clusters"
-
-    return 0L;
-}
-
-
-void Segment::ParseSeekHead(long long start, long long size_, size_t* pIndex)
-{
-    long long pos = start;
-    const long long stop = start + size_;
-    while (pos < stop)
-    {
-        long len;
-        
-        const long long id = ReadUInt(m_pReader, pos, len);
-        assert(id >= 0);  //TODO
-        assert((pos + len) <= stop);
-        
-        pos += len;  //consume ID
-        
-        const long long size = ReadUInt(m_pReader, pos, len);
-        assert(size >= 0);
-        assert((pos + len) <= stop);
-        
-        pos += len;  //consume Size field
-        assert((pos + size) <= stop);
-
-        if (id == 0x0DBB)  //SeekEntry ID
-            ParseSeekEntry(pos, size, pIndex);
-        
-        pos += size;  //consume payload
-        assert(pos <= stop);
-    }
-    
-    assert(pos == stop);
-}
-
-
-void Segment::ParseSecondarySeekHead(long long off, size_t* pIndex)
-{
-    assert(off >= 0);
-    assert(off < m_size);
-
-    long long pos = m_start + off;
-    const long long stop = m_start + m_size;
-    
-    long len;
-
-    long long result = GetUIntLength(m_pReader, pos, len);
-    assert(result == 0);
-    assert((pos + len) <= stop);
-    
-    const long long idpos = pos;
-
-    const long long id = ReadUInt(m_pReader, idpos, len);
-    assert(id == 0x014D9B74);  //SeekHead ID
-    
-    pos += len;  //consume ID
-    assert(pos < stop);
-    
-    //Read Size
-    
-    result = GetUIntLength(m_pReader, pos, len);
-    assert(result == 0);
-    assert((pos + len) <= stop);
-    
-    const long long size = ReadUInt(m_pReader, pos, len);
-    assert(size >= 0);
-    
-    pos += len;  //consume length of size of element
-    assert((pos + size) <= stop);
-    
-    //Pos now points to start of payload
-    
-    ParseSeekHead(pos, size, pIndex);
-}
-
-
-void Segment::ParseSeekEntry(long long start, long long size_, size_t* pIndex)
-{
-    long long pos = start;
-
-    const long long stop = start + size_;
-    
-    long len;
-    
-    const long long seekIdId = ReadUInt(m_pReader, pos, len);
-    //seekIdId;
-    assert(seekIdId == 0x13AB);  //SeekID ID
-    assert((pos + len) <= stop);
-    
-    pos += len;  //consume id
-
-    const long long seekIdSize = ReadUInt(m_pReader, pos, len);
-    assert(seekIdSize >= 0);
-    assert((pos + len) <= stop);
-    
-    pos += len;  //consume size
-    
-    const long long seekId = ReadUInt(m_pReader, pos, len);  //payload
-    assert(seekId >= 0);
-    assert(len == seekIdSize);
-    assert((pos + len) <= stop);
-    
-    pos += seekIdSize;  //consume payload
-    
-    const long long seekPosId = ReadUInt(m_pReader, pos, len);
-    //seekPosId;
-    assert(seekPosId == 0x13AC);  //SeekPos ID
-    assert((pos + len) <= stop);
-    
-    pos += len;  //consume id
-    
-    const long long seekPosSize = ReadUInt(m_pReader, pos, len);
-    assert(seekPosSize >= 0);
-    assert((pos + len) <= stop);
-
-    pos += len;  //consume size
-    assert((pos + seekPosSize) <= stop);
-        
-    const long long seekOff = UnserializeUInt(m_pReader, pos, seekPosSize);
-    assert(seekOff >= 0);
-    assert(seekOff < m_size);
-    
-    pos += seekPosSize;  //consume payload
-    assert(pos == stop);
-    
-    const long long seekPos = m_start + seekOff;
-    assert(seekPos < (m_start + m_size));
-   
-    if (seekId == 0x0F43B675)  //cluster id
-    {       
-        if (pIndex == NULL)
-            ++m_clusterCount; 
-        else
-        {
-            assert(m_clusters);
-            assert(m_clusterCount > 0);
-            
-            size_t& index = *pIndex;
-            assert(index < m_clusterCount);
-            
-            Cluster*& pCluster = m_clusters[index];
-            
-            pCluster = Cluster::Parse(this, index, seekOff);
-            assert(pCluster);  //TODO
-            
-            ++index;
-        }
-    }
-    else if (seekId == 0x014D9B74)  //SeekHead ID
-    {
-        ParseSecondarySeekHead(seekOff, pIndex);
-    }
-}
-
-
-long long Segment::Unparsed() const
-{
-    const long long stop = m_start + m_size;
-
-    const long long result = stop - m_pos;
-    assert(result >= 0);
-    
-    return result;
-}
-
-
-#if 0  //NOTE: too inefficient
-long long Segment::Load(long long time_ns)
-{
-    if (Unparsed() <= 0)
-        return 0;
-    
-    while (m_clusters.empty())
-    {
-        const long long result = Parse();
-        
-        if (result)  //error, or not enough bytes available
-            return result;
-            
-        if (Unparsed() <= 0)
-            return 0;
-    }
-    
-    while (m_clusters.back()->GetTime() < time_ns)
-    {
-        const long long result = Parse();
-        
-        if (result)  //error, or not enough bytes available
-            return result;
-            
-        if (Unparsed() <= 0)
-            return 0;
-    }        
-
-    return 0;        
-}
-#endif
-
-
-Cluster* Segment::GetFirst()
-{
-    if ((m_clusters == NULL) || (m_clusterCount <= 0))
-       return &m_eos;
-
-    Cluster* const pCluster = m_clusters[0];
-    assert(pCluster);
-        
-    return pCluster;
-}
-
-
-Cluster* Segment::GetLast()
-{
-    if ((m_clusters == NULL) || (m_clusterCount <= 0))
-        return &m_eos;
-
-    const size_t idx = m_clusterCount - 1;    
-    Cluster* const pCluster = m_clusters[idx];
-    assert(pCluster);
-        
-    return pCluster;
-}
-
-
-unsigned long Segment::GetCount() const
-{
-    //TODO: m_clusterCount should not be long long.
-    return static_cast<unsigned long>(m_clusterCount);
-}
-
-
-Cluster* Segment::GetNext(const Cluster* pCurr)
-{
-    assert(pCurr);
-    assert(pCurr != &m_eos);
-    assert(m_clusters);
-    assert(m_clusterCount > 0);
-
-    size_t idx =  pCurr->m_index;
-    assert(idx < m_clusterCount);
-    assert(pCurr == m_clusters[idx]);
-    
-    idx++;
-    
-    if (idx >= m_clusterCount) 
-        return &m_eos;
-        
-    Cluster* const pNext = m_clusters[idx];
-    assert(pNext);
-    
-    return pNext;
-}
-
-
-Cluster* Segment::GetCluster(long long time_ns)
-{
-    if ((m_clusters == NULL) || (m_clusterCount <= 0))
-        return &m_eos;
-        
-    {
-        Cluster* const pCluster = m_clusters[0];
-        assert(pCluster);
-        assert(pCluster->m_index == 0);
-        
-        if (time_ns <= pCluster->GetTime())
-            return pCluster;
-    }
-    
-    //Binary search of cluster array
-       
-    size_t i = 0;
-    size_t j = m_clusterCount;
-    
-    while (i < j)
-    {
-        //INVARIANT:
-        //[0, i) <= time_ns
-        //[i, j) ?
-        //[j, m_clusterCount)  > time_ns
-        
-        const size_t k = i + (j - i) / 2;
-        assert(k < m_clusterCount);
-
-        Cluster* const pCluster = m_clusters[k];
-        assert(pCluster);
-        assert(pCluster->m_index == k);
-        
-        const long long t = pCluster->GetTime();
-        
-        if (t <= time_ns)
-            i = k + 1;
-        else
-            j = k;
-            
-        assert(i <= j);
-    }
-    
-    assert(i == j);
-    assert(i > 0);
-    assert(i <= m_clusterCount);
-    
-    const size_t k = i - 1;
-    
-    Cluster* const pCluster = m_clusters[k];
-    assert(pCluster);
-    assert(pCluster->m_index == k);
-    assert(pCluster->GetTime() <= time_ns);
-    
-    return pCluster;
-}
-
-
-Tracks* Segment::GetTracks() const
-{
-    return m_pTracks;
-}
-
-
-const SegmentInfo* const Segment::GetInfo() const
-{
-    return m_pInfo;
-}
-
-
-long long Segment::GetDuration() const
-{
-    assert(m_pInfo);
-    return m_pInfo->GetDuration();
-}
-
-
-SegmentInfo::SegmentInfo(Segment* pSegment, long long start, long long size_) :
-    m_pSegment(pSegment),
-    m_start(start),
-    m_size(size_),
-    m_pMuxingAppAsUTF8(NULL),
-    m_pWritingAppAsUTF8(NULL),
-    m_pTitleAsUTF8(NULL)
-{
-    IMkvReader* const pReader = m_pSegment->m_pReader;
-   
-    long long pos = start;
-    const long long stop = start + size_;
-    
-    m_timecodeScale = 1000000;
-    m_duration = 0;
-    
-    
-    while (pos < stop)
-    {
-        if (Match(pReader, pos, 0x0AD7B1, m_timecodeScale))
-            assert(m_timecodeScale > 0);
-
-        else if (Match(pReader, pos, 0x0489, m_duration))
-            assert(m_duration >= 0);
-
-        else if (Match(pReader, pos, 0x0D80, m_pMuxingAppAsUTF8))   //[4D][80] 
-            assert(m_pMuxingAppAsUTF8);
-
-        else if (Match(pReader, pos, 0x1741, m_pWritingAppAsUTF8))  //[57][41]
-            assert(m_pWritingAppAsUTF8);
-            
-        else if (Match(pReader, pos, 0x3BA9, m_pTitleAsUTF8))        //[7B][A9]
-            assert(m_pTitleAsUTF8);
-
-        else
-        {
-            long len;
-            
-            const long long id = ReadUInt(pReader, pos, len);
-            //id;
-            assert(id >= 0);
-            assert((pos + len) <= stop);
-            
-            pos += len;  //consume id
-            assert((stop - pos) > 0);
-            
-            const long long size = ReadUInt(pReader, pos, len);
-            assert(size >= 0);
-            assert((pos + len) <= stop);
-            
-            pos += len + size;  //consume size and payload
-            assert(pos <= stop);
-        }
-    }
-    
-    assert(pos == stop);
-}
-
-SegmentInfo::~SegmentInfo()
-{
-    if (m_pMuxingAppAsUTF8)
-    {
-        delete[] m_pMuxingAppAsUTF8;
-        m_pMuxingAppAsUTF8 = NULL;
-    }
-
-    if (m_pWritingAppAsUTF8)
-    {
-        delete[] m_pWritingAppAsUTF8;
-        m_pWritingAppAsUTF8 = NULL;
-    }
-   
-    if (m_pTitleAsUTF8)
-    {
-        delete[] m_pTitleAsUTF8;
-        m_pTitleAsUTF8 = NULL;
-    }
-}
-
-long long SegmentInfo::GetTimeCodeScale() const
-{
-    return m_timecodeScale;
-}
-
-
-long long SegmentInfo::GetDuration() const
-{
-    assert(m_duration >= 0);    
-    assert(m_timecodeScale >= 1);
-    
-    const double dd = double(m_duration) * double(m_timecodeScale);
-    const long long d = static_cast<long long>(dd);
-    
-    return d;
-}
-
-const char* SegmentInfo::GetMuxingAppAsUTF8() const
-{
-    return m_pMuxingAppAsUTF8;
-}
-
-const char* SegmentInfo::GetWritingAppAsUTF8() const
-{
-    return m_pWritingAppAsUTF8;
-}
-
-const char* SegmentInfo::GetTitleAsUTF8() const
-{
-    return m_pTitleAsUTF8;
-}
-
-Track::Track(Segment* pSegment, const Info& i) :
-    m_pSegment(pSegment),
-    m_info(i)
-{
-}
-
-Track::~Track()
-{
-    Info& info = const_cast<Info&>(m_info);
-    info.Clear();
-}
-
-Track::Info::Info():
-    type(-1),
-    number(-1),
-    uid(-1),
-    nameAsUTF8(NULL),
-    codecId(NULL),
-    codecPrivate(NULL),
-    codecPrivateSize(0),
-    codecNameAsUTF8(NULL)
-{
-}
-
-void Track::Info::Clear() 
-{
-    delete[] nameAsUTF8;
-    nameAsUTF8 = NULL;
-
-    delete[] codecId;
-    codecId = NULL;
-
-    delete[] codecPrivate;
-    codecPrivate = NULL;
-
-    delete[] codecNameAsUTF8;
-    codecNameAsUTF8 = NULL;
-}
-
-const BlockEntry* Track::GetEOS() const
-{
-    return &m_eos;
-}
-
-long long Track::GetType() const
-{
-    const unsigned long result = static_cast<unsigned long>(m_info.type);
-    return result;
-}
-
-unsigned long Track::GetNumber() const
-{
-    assert(m_info.number >= 0);
-    const unsigned long result = static_cast<unsigned long>(m_info.number);
-    return result;
-}
-
-const char* Track::GetNameAsUTF8() const
-{
-    return m_info.nameAsUTF8;
-}
-
-const char* Track::GetCodecNameAsUTF8() const
-{  
-    return m_info.codecNameAsUTF8;
-}
-
-
-const char* Track::GetCodecId() const
-{
-    return m_info.codecId;
-}
-
-
-const unsigned char* Track::GetCodecPrivate(size_t *optionalSize) const
-{
-    if (optionalSize) {
-        *optionalSize = m_info.codecPrivateSize;
-    }
-    return m_info.codecPrivate;
-}
-
-
-long Track::GetFirst(const BlockEntry*& pBlockEntry) const
-{
-    Cluster* const pCluster = m_pSegment->GetFirst();
-    
-    //If Segment::GetFirst returns NULL, then this must be a network 
-    //download, and we haven't loaded any clusters yet.  In this case,
-    //returning NULL from Track::GetFirst means the same thing.
-
-    if ((pCluster == NULL) || pCluster->EOS())
-    {
-        pBlockEntry = NULL;
-        return E_BUFFER_NOT_FULL;  //return 1L instead?
-    }
-        
-    pBlockEntry = pCluster->GetFirst();
-    
-    while (pBlockEntry)
-    {
-        const Block* const pBlock = pBlockEntry->GetBlock();
-        assert(pBlock);
-        
-        if (pBlock->GetTrackNumber() == (unsigned long)m_info.number)
-            return 0L;
-            
-        pBlockEntry = pCluster->GetNext(pBlockEntry);
-    }
-    
-    //NOTE: if we get here, it means that we didn't find a block with
-    //a matching track number.  We interpret that as an error (which
-    //might be too conservative).
-
-    pBlockEntry = GetEOS();  //so we can return a non-NULL value
-    return 1L;
-}
-
-
-long Track::GetNext(const BlockEntry* pCurrEntry, const BlockEntry*& pNextEntry) const
-{
-    assert(pCurrEntry);
-    assert(!pCurrEntry->EOS());  //?
-    assert(pCurrEntry->GetBlock()->GetTrackNumber() == (unsigned long)m_info.number);    
-    
-    const Cluster* const pCurrCluster = pCurrEntry->GetCluster();
-    assert(pCurrCluster);
-    assert(!pCurrCluster->EOS());
-    
-    pNextEntry = pCurrCluster->GetNext(pCurrEntry);
-            
-    while (pNextEntry)
-    {    
-        const Block* const pNextBlock = pNextEntry->GetBlock();
-        assert(pNextBlock);
-    
-        if (pNextBlock->GetTrackNumber() == (unsigned long)m_info.number)
-            return 0L;
-            
-        pNextEntry = pCurrCluster->GetNext(pNextEntry);
-    }
-
-    Segment* pSegment = pCurrCluster->m_pSegment;    
-    Cluster* const pNextCluster = pSegment->GetNext(pCurrCluster);
-    
-    if ((pNextCluster == NULL) || pNextCluster->EOS())
-    {
-        if (pSegment->Unparsed() <= 0)   //all clusters have been loaded
-        {
-            pNextEntry = GetEOS();
-            return 1L;
-        }
-        
-        pNextEntry = NULL;
-        return E_BUFFER_NOT_FULL;
-    }
-        
-    pNextEntry = pNextCluster->GetFirst();
-    
-    while (pNextEntry)
-    {
-        const Block* const pNextBlock = pNextEntry->GetBlock();
-        assert(pNextBlock);
-        
-        if (pNextBlock->GetTrackNumber() == (unsigned long)m_info.number)
-            return 0L;
-            
-        pNextEntry = pNextCluster->GetNext(pNextEntry);
-    }
-    
-    //TODO: what has happened here is that we did not find a block
-    //with a matching track number on the next cluster.  It might
-    //be the case that some cluster beyond the next cluster 
-    //contains a block having a matching track number, but for
-    //now we terminate the search immediately.  We do this so that
-    //we don't end up searching the entire file looking for the
-    //next block.  Another possibility is to try searching for the next
-    //block in a small, fixed number of clusters (intead searching
-    //just the next one), or to terminate the search when when the
-    //there is a large gap in time, or large gap in file position.  It
-    //might very well be the case that the approach we use here is
-    //unnecessarily conservative.
-    
-    //TODO: again, here's a case where we need to return the special
-    //EOS block.  Or something.  It's OK if pNext is NULL, because
-    //we only need it to set the stop time of the media sample.
-    //(The start time is determined from pCurr, which is non-NULL
-    //and non-EOS.)  The problem is when we set pCurr=pNext; when
-    //pCurr has the value NULL we interpret that to mean that we
-    //haven't fully initialized pCurr and we attempt to set it to
-    //point to the first block for this track.  But that's not what
-    //we want at all; we want the next call to PopulateSample to
-    //return end-of-stream, not (re)start from the beginning.
-    //
-    //One work-around is to send EOS immediately.  We would send 
-    //the EOS the next pass anyway, so maybe it's no great loss.  The 
-    //only problem is that if this the stream really does end one
-    //cluster early (relative to other tracks), or the last frame
-    //happens to be a keyframe ("CanSeekToEnd").
-    //
-    //The problem is that we need a way to mark as stream as
-    //"at end of stream" without actually being at end of stream.
-    //We need to give pCurr some value that means "you've reached EOS".
-    //We can't synthesize the special EOS Cluster immediately
-    //(when we first open the file, say), because we use the existance
-    //of that special cluster value to mean that we've read all of 
-    //the clusters (this is a network download, so we can't know apriori
-    //how many we have).
-    //
-    //Or, we could return E_FAIL, and set another bit in the stream
-    //object itself, to indicate that it should send EOS earlier
-    //than when (pCurr=pStop).
-    //
-    //Or, probably the best solution, when we actually load the 
-    //blocks into a cluster: if we notice that there's no block
-    //for a track, we synthesize a nonce EOS block for that track.
-    //That way we always have something to return.  But that will
-    //only work for sequential scan???
-
-    //pNext = NULL;    
-    //return E_FAIL;
-    pNextEntry = GetEOS();
-    return 1L;
-}
-
-
-Track::EOSBlock::EOSBlock()
-{
-}
-
-
-bool Track::EOSBlock::EOS() const
-{
-    return true;
-}
-
-
-Cluster* Track::EOSBlock::GetCluster() const
-{
-    return NULL;
-}
-
-
-size_t Track::EOSBlock::GetIndex() const
-{
-    return 0;
-}
-
-
-const Block* Track::EOSBlock::GetBlock() const
-{
-    return NULL;
-}
-
-
-bool Track::EOSBlock::IsBFrame() const
-{
-    return false;
-}
-
-
-VideoTrack::VideoTrack(Segment* pSegment, const Info& i) :
-    Track(pSegment, i),
-    m_width(-1),
-    m_height(-1),
-    m_rate(-1)
-{
-    assert(i.type == 1);
-    assert(i.number > 0);
-    
-    IMkvReader* const pReader = pSegment->m_pReader;
-    
-    const Settings& s = i.settings;
-    assert(s.start >= 0);
-    assert(s.size >= 0);
-    
-    long long pos = s.start;
-    assert(pos >= 0);
-    
-    const long long stop = pos + s.size;
-    
-    while (pos < stop)
-    {
-#ifdef _DEBUG
-        long len;
-        const long long id = ReadUInt(pReader, pos, len);
-        assert(id >= 0);  //TODO: handle error case
-        assert((pos + len) <= stop);
-#endif
-        if (Match(pReader, pos, 0x30, m_width))         
-            ;
-        else if (Match(pReader, pos, 0x3A, m_height))   
-            ;
-        else if (Match(pReader, pos, 0x0383E3, m_rate)) 
-            ;
-        else
-        {
-            long len;
-            const long long id = ReadUInt(pReader, pos, len);
-            assert(id >= 0);  //TODO: handle error case
-            assert((pos + len) <= stop);
-        
-            pos += len;  //consume id
-            
-            const long long size = ReadUInt(pReader, pos, len);
-            assert(size >= 0);  //TODO: handle error case
-            assert((pos + len) <= stop);
-            
-            pos += len;  //consume length of size
-            assert((pos + size) <= stop);
-            
-            //pos now designates start of payload
-            
-            pos += size;  //consume payload
-            assert(pos <= stop);
-        }
-    }
-    
-    return;
-}
-
-
-bool VideoTrack::VetEntry(const BlockEntry* pBlockEntry) const
-{
-    assert(pBlockEntry);
-    
-    const Block* const pBlock = pBlockEntry->GetBlock();
-    assert(pBlock);    
-    assert(pBlock->GetTrackNumber() == (unsigned long)m_info.number);
-    
-    return pBlock->IsKey();
-}
-
-
-
-long long VideoTrack::GetWidth() const
-{
-    return m_width;
-}
-
-
-long long VideoTrack::GetHeight() const
-{
-    return m_height;
-}
-
-
-double VideoTrack::GetFrameRate() const
-{
-    return m_rate;
-}
-
-
-AudioTrack::AudioTrack(Segment* pSegment, const Info& i) :
-    Track(pSegment, i)
-{
-    assert(i.type == 2);
-    assert(i.number > 0);
-
-    IMkvReader* const pReader = pSegment->m_pReader;
-    
-    const Settings& s = i.settings;
-    assert(s.start >= 0);
-    assert(s.size >= 0);
-    
-    long long pos = s.start;
-    assert(pos >= 0);
-    
-    const long long stop = pos + s.size;
-    
-    while (pos < stop)
-    {
-#ifdef _DEBUG
-        long len;
-        const long long id = ReadUInt(pReader, pos, len);
-        assert(id >= 0);  //TODO: handle error case
-        assert((pos + len) <= stop);
-#endif
-        if (Match(pReader, pos, 0x35, m_rate))            
-            ;
-        else if (Match(pReader, pos, 0x1F, m_channels))   
-            ;
-        else if (Match(pReader, pos, 0x2264, m_bitDepth))  
-            ;            
-        else
-        {
-            long len;
-            const long long id = ReadUInt(pReader, pos, len);
-            assert(id >= 0);  //TODO: handle error case
-            assert((pos + len) <= stop);
-        
-            pos += len;  //consume id
-            
-            const long long size = ReadUInt(pReader, pos, len);
-            assert(size >= 0);  //TODO: handle error case
-            assert((pos + len) <= stop);
-            
-            pos += len;  //consume length of size
-            assert((pos + size) <= stop);
-            
-            //pos now designates start of payload
-            
-            pos += size;  //consume payload
-            assert(pos <= stop);
-        }
-    }
-
-    return;
-}
-
-bool AudioTrack::VetEntry(const BlockEntry* pBlockEntry) const
-{
-    assert(pBlockEntry);
-    
-    const Block* const pBlock = pBlockEntry->GetBlock();
-    assert(pBlock);
-    assert(pBlock->GetTrackNumber() == (unsigned long)m_info.number);
-
-    return true;
-}
-
-
-double AudioTrack::GetSamplingRate() const
-{
-    return m_rate;
-}
-
-
-long long AudioTrack::GetChannels() const
-{
-    return m_channels;
-}
-
-long long AudioTrack::GetBitDepth() const
-{
-    return m_bitDepth;
-}
-
-Tracks::Tracks(Segment* pSegment, long long start, long long size_) :
-    m_pSegment(pSegment),
-    m_start(start),
-    m_size(size_),
-    m_trackEntries(NULL),
-    m_trackEntriesEnd(NULL)
-{
-    long long stop = m_start + m_size;
-    IMkvReader* const pReader = m_pSegment->m_pReader;
-    
-    long long pos1 = m_start;
-    int count = 0;
-    
-    while (pos1 < stop)
-    {
-        long len;
-        const long long id = ReadUInt(pReader, pos1, len);
-        assert(id >= 0);
-        assert((pos1 + len) <= stop);
-        
-        pos1 += len;  //consume id
-        
-        const long long size = ReadUInt(pReader, pos1, len);
-        assert(size >= 0);
-        assert((pos1 + len) <= stop);
-        
-        pos1 += len;  //consume length of size
-        
-        //pos now desinates start of element
-        if (id == 0x2E)  //TrackEntry ID
-            ++count;
-            
-        pos1 += size;  //consume payload
-        assert(pos1 <= stop);
-    }    
-
-    if (count <= 0)
-        return;
-
-    m_trackEntries = new Track*[count];
-    m_trackEntriesEnd = m_trackEntries;
-
-    long long pos = m_start;
-
-    while (pos < stop)
-    {
-        long len;
-        const long long id = ReadUInt(pReader, pos, len);
-        assert(id >= 0);
-        assert((pos + len) <= stop);
-        
-        pos += len;  //consume id
-        
-        const long long size1 = ReadUInt(pReader, pos, len);
-        assert(size1 >= 0);
-        assert((pos + len) <= stop);
-        
-        pos += len;  //consume length of size
-        
-        //pos now desinates start of element
-        
-        if (id == 0x2E)  //TrackEntry ID
-            ParseTrackEntry(pos, size1, *m_trackEntriesEnd++);
-            
-        pos += size1;  //consume payload
-        assert(pos <= stop);
-    }    
-}
-
-unsigned long Tracks::GetTracksCount() const
-{
-    const ptrdiff_t result = m_trackEntriesEnd - m_trackEntries;
-    assert(result >= 0);
-    
-    return static_cast<unsigned long>(result);
-}
-
-
-void Tracks::ParseTrackEntry(
-    long long start,
-    long long size,
-    Track*& pTrack)
-{
-    IMkvReader* const pReader = m_pSegment->m_pReader;
-    
-    long long pos = start;
-    const long long stop = start + size;
-
-    Track::Info i;
-    
-    Track::Settings videoSettings;
-    videoSettings.start = -1;
-    
-    Track::Settings audioSettings;
-    audioSettings.start = -1;
-    
-    while (pos < stop)
-    {
-#ifdef _DEBUG
-        long len;
-        const long long id = ReadUInt(pReader, pos, len);
-        len;
-        id;
-#endif
-        if (Match(pReader, pos, 0x57, i.number))
-            assert(i.number > 0);
-
-        else if (Match(pReader, pos, 0x33C5, i.uid))           
-            ;  
-
-        else if (Match(pReader, pos, 0x03, i.type))            
-            ;  
-
-        else if (Match(pReader, pos, 0x136E, i.nameAsUTF8))          
-            assert(i.nameAsUTF8);  
-
-        else if (Match(pReader, pos, 0x06, i.codecId))         
-            ;  
-
-        else if (Match(pReader, pos, 0x23A2, i.codecPrivate, &i.codecPrivateSize))  
-            ;  
-
-        else if (Match(pReader, pos, 0x058688, i.codecNameAsUTF8))   
-            assert(i.codecNameAsUTF8);  
-
-        else
-        {
-            long len;
-            
-            const long long id = ReadUInt(pReader, pos, len);
-            assert(id >= 0);  //TODO: handle error case
-            assert((pos + len) <= stop);
-            
-            pos += len;  //consume id
-            
-            const long long size = ReadUInt(pReader, pos, len);
-            assert(size >= 0);  //TODO: handle error case
-            assert((pos + len) <= stop);
-            
-            pos += len;  //consume length of size
-            const long long start = pos;
-            
-            pos += size;  //consume payload
-            assert(pos <= stop);
-            
-            if (id == 0x60)
-            {
-                videoSettings.start = start;
-                videoSettings.size = size;
-            }
-            else if (id == 0x61)
-            {
-                audioSettings.start = start;
-                audioSettings.size = size;
-            }
-        }
-    }
-    
-    assert(pos == stop);
-    //TODO: propertly vet info.number, to ensure both its existence,
-    //and that it is unique among all tracks.
-    assert(i.number > 0);
-
-    //TODO: vet settings, to ensure that video settings (0x60)
-    //were specified when type = 1, and that audio settings (0x61)
-    //were specified when type = 2.    
-    if (i.type == 1)  //video
-    {
-        assert(audioSettings.start < 0);
-        assert(videoSettings.start >= 0);
-        
-        i.settings = videoSettings;
-        
-        VideoTrack* const t = new VideoTrack(m_pSegment, i);
-        assert(t);  //TODO
-        pTrack = t;    
-    }
-    else if (i.type == 2)  //audio
-    {
-        assert(videoSettings.start < 0);
-        assert(audioSettings.start >= 0);
-        
-        i.settings = audioSettings;
-        
-        AudioTrack* const t = new  AudioTrack(m_pSegment, i);
-        assert(t);  //TODO
-        pTrack = t;  
-    }
-    else
-    {
-        // for now we do not support other track types yet.
-        // TODO: support other track types
-        i.Clear();
-  
-        pTrack = NULL;
-    }
-    
-    return;
-}
-
-
-Tracks::~Tracks()
-{
-    Track** i = m_trackEntries;
-    Track** const j = m_trackEntriesEnd;
-    
-    while (i != j)
-    {
-        Track* pTrack = *i++;
-        delete pTrack;
-        pTrack = NULL;    
-    }
-
-    delete[] m_trackEntries;
-}
-
-
-Track* Tracks::GetTrackByNumber(unsigned long tn) const
-{
-    Track** i = m_trackEntries;
-    Track** const j = m_trackEntriesEnd;
-
-    while (i != j)
-    {
-        Track* const pTrack = *i++;
-       
-        if (pTrack == NULL)
-            continue;
-
-        if (tn == pTrack->GetNumber())
-            return pTrack;
-    }
-
-    return NULL;  //not found
-}
-
-
-Track* Tracks::GetTrackByIndex(unsigned long idx) const
-{
-    const ptrdiff_t count = m_trackEntriesEnd - m_trackEntries;
-       
-    if (idx >= static_cast<unsigned long>(count))
-         return NULL;
-
-    return m_trackEntries[idx];
-}
-
-
-void Cluster::Load()
-{
-    assert(m_pSegment);
-    
-    if (m_start > 0)
-    {
-        assert(m_size > 0);
-        assert(m_timecode >= 0);
-        return;
-    }
-    
-    assert(m_size == 0);
-    assert(m_timecode < 0);
-    
-    IMkvReader* const pReader = m_pSegment->m_pReader;
-
-    const long long off = -m_start;  //relative to segment
-    long long pos = m_pSegment->m_start + off;  //absolute
-    
-    long len;
-
-    const long long id_ = ReadUInt(pReader, pos, len);
-    assert(id_ >= 0);
-    assert(id_ == 0x0F43B675);  //Cluster ID
-    
-    pos += len;  //consume id
-    
-    const long long size_ = ReadUInt(pReader, pos, len);
-    assert(size_ >= 0);
-    
-    pos += len;  //consume size
-    
-    m_start = pos;
-    m_size = size_;
-    
-    const long long stop = m_start + size_;
-    
-    long long timecode = -1;
-    
-    while (pos < stop)
-    {
-        if (Match(pReader, pos, 0x67, timecode))
-            break;            
-        else
-        {
-            const long long id = ReadUInt(pReader, pos, len);
-            assert(id >= 0);  //TODO
-            assert((pos + len) <= stop);
-            
-            pos += len;  //consume id
-            
-            const long long size = ReadUInt(pReader, pos, len);
-            assert(size >= 0);  //TODO
-            assert((pos + len) <= stop);
-            
-            pos += len;  //consume size
-            
-            if (id == 0x20)  //BlockGroup ID
-                break;
-                
-            if (id == 0x23)  //SimpleBlock ID
-                break;
-
-            pos += size;  //consume payload
-            assert(pos <= stop);
-        }
-    }
-    
-    assert(pos <= stop);
-    assert(timecode >= 0);
-    
-    m_timecode = timecode;
-}
-
-
-Cluster* Cluster::Parse(
-    Segment* pSegment,
-    size_t idx,
-    long long off)
-{
-    assert(pSegment);
-    assert(off >= 0);
-    assert(off < pSegment->m_size);
-    Cluster* const pCluster = new Cluster(pSegment, idx, -off);
-    assert(pCluster);
-    
-    return pCluster;
-}
-
-
-Cluster::Cluster() :
-    m_pSegment(NULL),
-    m_index(0),
-    m_start(0),
-    m_size(0),
-    m_timecode(0),
-    m_pEntries(NULL),
-    m_entriesCount(0)
-{
-}
-
-Cluster::Cluster(
-    Segment* pSegment,
-    size_t idx,
-    long long off) :
-    m_pSegment(pSegment),
-    m_index(idx),
-    m_start(off),
-    m_size(0),
-    m_timecode(-1),
-    m_pEntries(NULL),
-    m_entriesCount(0)
-{
-}
-
-
-Cluster::~Cluster()
-{
-#if 0
-    while (!m_pEntries.empty())
-    {
-        BlockEntry* pBlockEntry = m_pEntries.front();
-        assert(pBlockEntry);
-        
-        m_pEntries.pop_front();
-        delete pBlockEntry;
-    }
-#else
-    BlockEntry** i = m_pEntries;
-    BlockEntry** const j = m_pEntries + m_entriesCount;
-    while (i != j)
-    {
-         BlockEntry* p = *i++;
-   
-         assert(p);
-         delete p;
-    }
- 
-    delete[] m_pEntries;
-#endif
-
-}
-
-bool Cluster::EOS() const
-{
-    return (m_pSegment == 0);
-}
-
-
-void Cluster::LoadBlockEntries()
-{
-    if (m_pEntries)
-        return;
-
-    Load();    
-    assert(m_timecode >= 0);
-    assert(m_start > 0);
-    assert(m_size > 0);
-    
-    IMkvReader* const pReader = m_pSegment->m_pReader;
-    
-    long long pos = m_start;
-    const long long stop = m_start + m_size;
-    long long timecode = -1;
-   
-    long long idx = pos;
-
-    m_entriesCount = 0;
-    
-    while (idx < stop)
-    {
-        if (Match(pReader, idx, 0x67, timecode))
-            assert(timecode == m_timecode);
-        else 
-        {
-            long len;
-            
-            const long long id = ReadUInt(pReader, idx, len);
-            assert(id >= 0);  //TODO
-            assert((idx + len) <= stop);
-            
-            idx += len;  //consume id
-            
-            const long long size = ReadUInt(pReader, idx, len);
-            assert(size >= 0);  //TODO
-            assert((idx + len) <= stop);
-            
-            idx += len;  //consume size
-            
-            if (id == 0x20)  //BlockGroup ID
-                ++m_entriesCount;
-            else if (id == 0x23)  //SimpleBlock ID
-                ++m_entriesCount;
-
-            idx += size;  //consume payload
-
-            assert(idx <= stop);
-        }  
-    }
-
-    if (m_entriesCount == 0)
-        return;
-     
-    m_pEntries = new BlockEntry*[m_entriesCount];
-    size_t index = 0;
-    
-    while (pos < stop)
-    {
-        if (Match(pReader, pos, 0x67, timecode))
-            assert(timecode == m_timecode);
-        else
-        {
-            long len;
-            const long long id = ReadUInt(pReader, pos, len);
-            assert(id >= 0);  //TODO
-            assert((pos + len) <= stop);
-            
-            pos += len;  //consume id
-            
-            const long long size = ReadUInt(pReader, pos, len);
-            assert(size >= 0);  //TODO
-            assert((pos + len) <= stop);
-            
-            pos += len;  //consume size
-            
-            if (id == 0x20)  //BlockGroup ID
-                ParseBlockGroup(pos, size, index++);
-            else if (id == 0x23)  //SimpleBlock ID
-                ParseSimpleBlock(pos, size, index++);
-
-            pos += size;  //consume payload
-            assert(pos <= stop);
-        }
-    }
-    
-    assert(pos == stop);
-    assert(timecode >= 0);
-    assert(index == m_entriesCount);
-}
-
-
-
-long long Cluster::GetTimeCode()
-{
-    Load();
-    return m_timecode;
-}
-
-
-long long Cluster::GetTime()
-{
-    const long long tc = GetTimeCode();
-    assert(tc >= 0);
-    
-    const SegmentInfo* const pInfo = m_pSegment->GetInfo();
-    assert(pInfo);
-    
-    const long long scale = pInfo->GetTimeCodeScale();
-    assert(scale >= 1);
-    
-    const long long t = m_timecode * scale;
-
-    return t;
-}
-
-
-void Cluster::ParseBlockGroup(long long start, long long size, size_t index)
-{
-    assert(m_pEntries);
-    assert(m_entriesCount);
-    assert(index < m_entriesCount);
-    
-    BlockGroup* const pGroup = new BlockGroup(this, index, start, size);
-    assert(pGroup);  //TODO
-        
-    m_pEntries[index] = pGroup;
-}
-
-
-
-void Cluster::ParseSimpleBlock(long long start, long long size, size_t index)
-{
-    assert(m_pEntries);
-    assert(m_entriesCount);
-    assert(index < m_entriesCount);
-
-    SimpleBlock* const pSimpleBlock = new SimpleBlock(this, index, start, size);
-    assert(pSimpleBlock);  //TODO
-        
-    m_pEntries[index] = pSimpleBlock;
-}
-
-
-const BlockEntry* Cluster::GetFirst()
-{
-    LoadBlockEntries();
-    
-    return m_pEntries[0];
-}
-
-        
-const BlockEntry* Cluster::GetLast()
-{ 
-    if (m_entriesCount == 0)
-        return m_pEntries[0];
-    
-    return m_pEntries[m_entriesCount-1];
-}
-
-        
-const BlockEntry* Cluster::GetNext(const BlockEntry* pEntry) const
-{
-    assert(pEntry);
-    
-    size_t idx = pEntry->GetIndex();
-    
-    ++idx;
-
-    if (idx == m_entriesCount) 
-      return NULL;
-
-    return m_pEntries[idx];
-
-}
-
-
-const BlockEntry* Cluster::GetEntry(const Track* pTrack)
-{
-
-    assert(pTrack);
-    
-    if (m_pSegment == NULL)  //EOS
-        return pTrack->GetEOS();
-    
-    LoadBlockEntries();
-    
-    BlockEntry* i = *m_pEntries;
-    BlockEntry* j = *m_pEntries + m_entriesCount;
-    while (i != j)
-    {
-        BlockEntry* pEntry = i;
-        i++;
-        assert(pEntry);
-        assert(!pEntry->EOS());
-        
-        const Block* const pBlock = pEntry->GetBlock();
-        assert(pBlock);
-        
-        if (pBlock->GetTrackNumber() != pTrack->GetNumber())
-            continue;
-
-        if (pTrack->VetEntry(pEntry))
-            return pEntry;
-    }
-    
-    return pTrack->GetEOS();  //no satisfactory block found
-}
-
-
-BlockEntry::BlockEntry()
-{
-}
-
-
-BlockEntry::~BlockEntry()
-{
-}
-
-
-
-SimpleBlock::SimpleBlock(
-    Cluster* pCluster, 
-    size_t idx, 
-    long long start, 
-    long long size) :
-    m_pCluster(pCluster),
-    m_index(idx),
-    m_block(start, size, pCluster->m_pSegment->m_pReader)
-{
-}
-
-
-bool SimpleBlock::EOS() const
-{
-    return false;
-}
-
-
-Cluster* SimpleBlock::GetCluster() const
-{
-    return m_pCluster;
-}
-
-
-size_t SimpleBlock::GetIndex() const
-{
-    return m_index;
-}
-
-
-const Block* SimpleBlock::GetBlock() const
-{
-    return &m_block;
-}
-
-
-bool SimpleBlock::IsBFrame() const
-{
-    return false;
-}
-
-
-BlockGroup::BlockGroup(
-    Cluster* pCluster, 
-    size_t idx, 
-    long long start, 
-    long long size_) :
-    m_pCluster(pCluster),
-    m_index(idx),
-    m_prevTimeCode(0),
-    m_nextTimeCode(0),
-    m_pBlock(NULL)  //TODO: accept multiple blocks within a block group
-{
-    IMkvReader* const pReader = m_pCluster->m_pSegment->m_pReader;
-    
-    long long pos = start;
-    const long long stop = start + size_;
- 
-    bool bSimpleBlock = false;
-    
-    while (pos < stop)
-    {
-        short t;
-    
-        if (Match(pReader, pos, 0x7B, t))
-        {    
-            if (t < 0)
-                m_prevTimeCode = t;
-            else if (t > 0)
-                m_nextTimeCode = t;
-            else
-                assert(false);
-        }
-        else
-        {
-            long len;
-            const long long id = ReadUInt(pReader, pos, len);
-            assert(id >= 0);  //TODO
-            assert((pos + len) <= stop);
-            
-            pos += len;  //consume ID
-            
-            const long long size = ReadUInt(pReader, pos, len);
-            assert(size >= 0);  //TODO
-            assert((pos + len) <= stop);
-            
-            pos += len;  //consume size
-            
-            switch (id)
-            {
-                case 0x23:  //SimpleBlock ID
-                    bSimpleBlock = true;
-                    //YES, FALL THROUGH TO NEXT CASE
-
-                case 0x21:  //Block ID
-                    ParseBlock(pos, size);                    
-                    break;
-                    
-                default:
-                    break;
-            }
-                
-            pos += size;  //consume payload
-            assert(pos <= stop);
-        }
-    }
-    
-    assert(pos == stop);
-    assert(m_pBlock);
-    
-    if (!bSimpleBlock)
-        m_pBlock->SetKey(m_prevTimeCode >= 0);
-}
-
-
-BlockGroup::~BlockGroup()
-{
-    delete m_pBlock;
-}
-
-
-void BlockGroup::ParseBlock(long long start, long long size)
-{   
-    IMkvReader* const pReader = m_pCluster->m_pSegment->m_pReader;
-    
-    Block* const pBlock = new Block(start, size, pReader);
-    assert(pBlock);  //TODO
-
-    //TODO: the Matroska spec says you have multiple blocks within the 
-    //same block group, with blocks ranked by priority (the flag bits).
-    //I haven't ever seen such a file (mkvmux certainly doesn't make
-    //one), so until then I'll just assume block groups contain a single
-    //block.
-#if 0    
-    m_blocks.push_back(pBlock);
-#else
-    assert(m_pBlock == NULL);
-    m_pBlock = pBlock;
-#endif
-
-#if 0
-    Track* const pTrack = pBlock->GetTrack();
-    assert(pTrack);
-    
-    pTrack->Insert(pBlock);
-#endif
-}
-
-
-bool BlockGroup::EOS() const
-{
-    return false;
-}
-
-
-Cluster* BlockGroup::GetCluster() const
-{
-    return m_pCluster;
-}
-
-
-size_t BlockGroup::GetIndex() const
-{
-    return m_index;
-}
-
-
-const Block* BlockGroup::GetBlock() const
-{
-    return m_pBlock;
-}
-
-
-short BlockGroup::GetPrevTimeCode() const
-{
-    return m_prevTimeCode;
-}
-
-
-short BlockGroup::GetNextTimeCode() const
-{
-    return m_nextTimeCode;
-}    
-
-
-bool BlockGroup::IsBFrame() const
-{
-    return (m_nextTimeCode > 0);
-}
-
-
-
-Block::Block(long long start, long long size_, IMkvReader* pReader) :
-    m_start(start),
-    m_size(size_)
-{
-    long long pos = start;
-    const long long stop = start + size_;
-
-    long len;
-    
-    m_track = ReadUInt(pReader, pos, len);
-    assert(m_track > 0);
-    assert((pos + len) <= stop);
-    
-    pos += len;  //consume track number
-    assert((stop - pos) >= 2);
-    
-    m_timecode = Unserialize2SInt(pReader, pos);
-
-    pos += 2;
-    assert((stop - pos) >= 1);
-    
-    const long hr = pReader->Read(pos, 1, &m_flags);
-    assert(hr == 0L);
-
-    ++pos;
-    assert(pos <= stop);
-    
-    m_frameOff = pos;
-    
-    const long long frame_size = stop - pos;
-
-    assert(frame_size <= 2147483647L);
-    
-    m_frameSize = static_cast<long>(frame_size);
-}
-
-
-long long Block::GetTimeCode(Cluster* pCluster) const
-{
-    assert(pCluster);
-    
-    const long long tc0 = pCluster->GetTimeCode();
-    assert(tc0 >= 0);
-    
-    const long long tc = tc0 + static_cast<long long>(m_timecode);
-    assert(tc >= 0);
-    
-    return tc;  //unscaled timecode units
-}
-
-
-long long Block::GetTime(Cluster* pCluster) const
-{
-    assert(pCluster);
-    
-    const long long tc = GetTimeCode(pCluster);
-    
-    const Segment* const pSegment = pCluster->m_pSegment;
-    const SegmentInfo* const pInfo = pSegment->GetInfo();
-    assert(pInfo);
-    
-    const long long scale = pInfo->GetTimeCodeScale();
-    assert(scale >= 1);
-    
-    const long long ns = tc * scale;
-
-    return ns;
-}
-
-
-unsigned long Block::GetTrackNumber() const
-{
-    assert(m_track > 0);
-    
-    return static_cast<unsigned long>(m_track);
-}
-
-
-bool Block::IsKey() const
-{
-    return ((m_flags & static_cast<unsigned char>(1 << 7)) != 0);
-}
-
-
-void Block::SetKey(bool bKey)
-{
-    if (bKey)
-        m_flags |= static_cast<unsigned char>(1 << 7);
-    else
-        m_flags &= 0x7F;
-}
-
-
-long Block::GetSize() const
-{
-    return m_frameSize;
-}
-
-
-long Block::Read(IMkvReader* pReader, unsigned char* buf) const
-{
-
-    assert(pReader);
-    assert(buf);
-    
-    const long hr = pReader->Read(m_frameOff, m_frameSize, buf);
-    
-    return hr;
-}
-
-
-}  //end namespace mkvparser
+// Copyright (c) 2010 The WebM project authors. All Rights Reserved.

+//

+// Use of this source code is governed by a BSD-style license

+// that can be found in the LICENSE file in the root of the source

+// tree. An additional intellectual property rights grant can be found

+// in the file PATENTS.  All contributing project authors may

+// be found in the AUTHORS file in the root of the source tree.

+

+#include "mkvparser.hpp"

+#include <cassert>

+#include <cstring>

+#include <new>

+//#include <windows.h>

+//#include "odbgstream.hpp"

+//using std::endl;

+

+mkvparser::IMkvReader::~IMkvReader()

+{

+}

+

+

+void mkvparser::GetVersion(int& major, int& minor, int& build, int& revision)

+{

+    major = 1;

+    minor = 0;

+    build = 0;

+    revision = 4;

+}

+

+

+long long mkvparser::ReadUInt(IMkvReader* pReader, long long pos, long& len)

+{

+    assert(pReader);

+    assert(pos >= 0);

+

+    long long total, available;

+

+    long hr = pReader->Length(&total, &available);

+    assert(hr >= 0);

+    assert(pos < available);

+    assert((available - pos) >= 1);  //assume here max u-int len is 8

+

+    unsigned char b;

+

+    hr = pReader->Read(pos, 1, &b);

+    if (hr < 0)

+        return hr;

+

+    assert(hr == 0L);

+

+    if (b & 0x80)       //1000 0000

+    {

+        len = 1;

+        b &= 0x7F;      //0111 1111

+    }

+    else if (b & 0x40)  //0100 0000

+    {

+        len = 2;

+        b &= 0x3F;      //0011 1111

+    }

+    else if (b & 0x20)  //0010 0000

+    {

+        len = 3;

+        b &= 0x1F;      //0001 1111

+    }

+    else if (b & 0x10)  //0001 0000

+    {

+        len = 4;

+        b &= 0x0F;      //0000 1111

+    }

+    else if (b & 0x08)  //0000 1000

+    {

+        len = 5;

+        b &= 0x07;      //0000 0111

+    }

+    else if (b & 0x04)  //0000 0100

+    {

+        len = 6;

+        b &= 0x03;      //0000 0011

+    }

+    else if (b & 0x02)  //0000 0010

+    {

+        len = 7;

+        b &= 0x01;      //0000 0001

+    }

+    else

+    {

+        assert(b & 0x01);  //0000 0001

+        len = 8;

+        b = 0;             //0000 0000

+    }

+

+    assert((available - pos) >= len);

+

+    long long result = b;

+    ++pos;

+    for (long i = 1; i < len; ++i)

+    {

+        hr = pReader->Read(pos, 1, &b);

+

+        if (hr < 0)

+            return hr;

+

+        assert(hr == 0L);

+

+        result <<= 8;

+        result |= b;

+

+        ++pos;

+    }

+

+    return result;

+}

+

+

+long long mkvparser::GetUIntLength(

+    IMkvReader* pReader,

+    long long pos,

+    long& len)

+{

+    assert(pReader);

+    assert(pos >= 0);

+

+    long long total, available;

+

+    long hr = pReader->Length(&total, &available);

+    assert(hr >= 0);

+    assert(available <= total);

+

+    if (pos >= available)

+        return pos;  //too few bytes available

+

+    unsigned char b;

+

+    hr = pReader->Read(pos, 1, &b);

+

+    if (hr < 0)

+        return hr;

+

+    assert(hr == 0L);

+

+    if (b == 0)  //we can't handle u-int values larger than 8 bytes

+        return E_FILE_FORMAT_INVALID;

+

+    unsigned char m = 0x80;

+    len = 1;

+

+    while (!(b & m))

+    {

+        m >>= 1;

+        ++len;

+    }

+

+    return 0;  //success

+}

+

+

+long long mkvparser::SyncReadUInt(

+    IMkvReader* pReader,

+    long long pos,

+    long long stop,

+    long& len)

+{

+    assert(pReader);

+

+    if (pos >= stop)

+        return E_FILE_FORMAT_INVALID;

+

+    unsigned char b;

+

+    long hr = pReader->Read(pos, 1, &b);

+

+    if (hr < 0)

+        return hr;

+

+    if (hr != 0L)

+        return E_BUFFER_NOT_FULL;

+

+    if (b == 0)  //we can't handle u-int values larger than 8 bytes

+        return E_FILE_FORMAT_INVALID;

+

+    unsigned char m = 0x80;

+    len = 1;

+

+    while (!(b & m))

+    {

+        m >>= 1;

+        ++len;

+    }

+

+    if ((pos + len) > stop)

+        return E_FILE_FORMAT_INVALID;

+

+    long long result = b & (~m);

+    ++pos;

+

+    for (int i = 1; i < len; ++i)

+    {

+        hr = pReader->Read(pos, 1, &b);

+

+        if (hr < 0)

+            return hr;

+

+        if (hr != 0L)

+            return E_BUFFER_NOT_FULL;

+

+        result <<= 8;

+        result |= b;

+

+        ++pos;

+    }

+

+    return result;

+}

+

+

+long long mkvparser::UnserializeUInt(

+    IMkvReader* pReader,

+    long long pos,

+    long long size)

+{

+    assert(pReader);

+    assert(pos >= 0);

+    assert(size > 0);

+    assert(size <= 8);

+

+    long long result = 0;

+

+    for (long long i = 0; i < size; ++i)

+    {

+        unsigned char b;

+

+        const long hr = pReader->Read(pos, 1, &b);

+

+        if (hr < 0)

+            return hr;

+        result <<= 8;

+        result |= b;

+

+        ++pos;

+    }

+

+    return result;

+}

+

+

+float mkvparser::Unserialize4Float(

+    IMkvReader* pReader,

+    long long pos)

+{

+    assert(pReader);

+    assert(pos >= 0);

+

+    long long total, available;

+

+    long hr = pReader->Length(&total, &available);

+    assert(hr >= 0);

+    assert(available <= total);

+    assert((pos + 4) <= available);

+

+    float result;

+

+    unsigned char* const p = (unsigned char*)&result;

+    unsigned char* q = p + 4;

+

+    for (;;)

+    {

+        hr = pReader->Read(pos, 1, --q);

+        assert(hr == 0L);

+

+        if (q == p)

+            break;

+

+        ++pos;

+    }

+

+    return result;

+}

+

+

+double mkvparser::Unserialize8Double(

+    IMkvReader* pReader,

+    long long pos)

+{

+    assert(pReader);

+    assert(pos >= 0);

+

+    double result;

+

+    unsigned char* const p = (unsigned char*)&result;

+    unsigned char* q = p + 8;

+

+    for (;;)

+    {

+        const long hr = pReader->Read(pos, 1, --q);

+        assert(hr == 0L);

+

+        if (q == p)

+            break;

+

+        ++pos;

+    }

+

+    return result;

+}

+

+signed char mkvparser::Unserialize1SInt(

+    IMkvReader* pReader,

+    long long pos)

+{

+    assert(pReader);

+    assert(pos >= 0);

+

+    long long total, available;

+

+    long hr = pReader->Length(&total, &available);

+    assert(hr == 0);

+    assert(available <= total);

+    assert(pos < available);

+

+    signed char result;

+

+    hr = pReader->Read(pos, 1, (unsigned char*)&result);

+    assert(hr == 0);

+

+    return result;

+}

+

+short mkvparser::Unserialize2SInt(

+    IMkvReader* pReader,

+    long long pos)

+{

+    assert(pReader);

+    assert(pos >= 0);

+

+    long long total, available;

+

+    long hr = pReader->Length(&total, &available);

+    assert(hr >= 0);

+    assert(available <= total);

+    assert((pos + 2) <= available);

+

+    short result;

+

+    unsigned char* const p = (unsigned char*)&result;

+    unsigned char* q = p + 2;

+

+    for (;;)

+    {

+        hr = pReader->Read(pos, 1, --q);

+        assert(hr == 0L);

+

+        if (q == p)

+            break;

+

+        ++pos;

+    }

+

+    return result;

+}

+

+

+bool mkvparser::Match(

+    IMkvReader* pReader,

+    long long& pos,

+    unsigned long id_,

+    long long& val)

+

+{

+    assert(pReader);

+    assert(pos >= 0);

+

+    long long total, available;

+

+    long hr = pReader->Length(&total, &available);

+    assert(hr >= 0);

+    assert(available <= total);

+

+    long len;

+

+    const long long id = ReadUInt(pReader, pos, len);

+    assert(id >= 0);

+    assert(len > 0);

+    assert(len <= 8);

+    assert((pos + len) <= available);

+

+    if ((unsigned long)id != id_)

+        return false;

+

+    pos += len;  //consume id

+

+    const long long size = ReadUInt(pReader, pos, len);

+    assert(size >= 0);

+    assert(size <= 8);

+    assert(len > 0);

+    assert(len <= 8);

+    assert((pos + len) <= available);

+

+    pos += len;  //consume length of size of payload

+

+    val = UnserializeUInt(pReader, pos, size);

+    assert(val >= 0);

+

+    pos += size;  //consume size of payload

+

+    return true;

+}

+

+bool mkvparser::Match(

+    IMkvReader* pReader,

+    long long& pos,

+    unsigned long id_,

+    char*& val)

+{

+    assert(pReader);

+    assert(pos >= 0);

+

+    long long total, available;

+

+    long hr = pReader->Length(&total, &available);

+    assert(hr >= 0);

+    assert(available <= total);

+

+    long len;

+

+    const long long id = ReadUInt(pReader, pos, len);

+    assert(id >= 0);

+    assert(len > 0);

+    assert(len <= 8);

+    assert((pos + len) <= available);

+

+    if ((unsigned long)id != id_)

+        return false;

+

+    pos += len;  //consume id

+

+    const long long size_ = ReadUInt(pReader, pos, len);

+    assert(size_ >= 0);

+    assert(len > 0);

+    assert(len <= 8);

+    assert((pos + len) <= available);

+

+    pos += len;  //consume length of size of payload

+    assert((pos + size_) <= available);

+

+    const size_t size = static_cast<size_t>(size_);

+    val = new char[size+1];

+

+    for (size_t i = 0; i < size; ++i)

+    {

+        char c;

+

+        hr = pReader->Read(pos + i, 1, (unsigned char*)&c);

+        assert(hr == 0L);

+

+        val[i] = c;

+

+        if (c == '\0')

+            break;

+

+    }

+

+    val[size] = '\0';

+    pos += size_;  //consume size of payload

+

+    return true;

+}

+

+bool mkvparser::Match(

+    IMkvReader* pReader,

+    long long& pos,

+    unsigned long id_,

+    unsigned char*& buf,

+    size_t& buflen)

+{

+    assert(pReader);

+    assert(pos >= 0);

+

+    long long total, available;

+

+    long hr = pReader->Length(&total, &available);

+    assert(hr >= 0);

+    assert(available <= total);

+

+    long len;

+    const long long id = ReadUInt(pReader, pos, len);

+    assert(id >= 0);

+    assert(len > 0);

+    assert(len <= 8);

+    assert((pos + len) <= available);

+

+    if ((unsigned long)id != id_)

+        return false;

+

+    pos += len;  //consume id

+

+    const long long size_ = ReadUInt(pReader, pos, len);

+    assert(size_ >= 0);

+    assert(len > 0);

+    assert(len <= 8);

+    assert((pos + len) <= available);

+

+    pos += len;  //consume length of size of payload

+    assert((pos + size_) <= available);

+

+    const long buflen_ = static_cast<long>(size_);

+

+    buf = new (std::nothrow) unsigned char[buflen_];

+    assert(buf);  //TODO

+

+    hr = pReader->Read(pos, buflen_, buf);

+    assert(hr == 0L);

+

+    buflen = buflen_;

+

+    pos += size_;  //consume size of payload

+    return true;

+}

+

+

+bool mkvparser::Match(

+    IMkvReader* pReader,

+    long long& pos,

+    unsigned long id_,

+    double& val)

+{

+    assert(pReader);

+    assert(pos >= 0);

+

+    long long total, available;

+

+    long hr = pReader->Length(&total, &available);

+    assert(hr >= 0);

+    assert(available <= total);

+    long idlen;

+    const long long id = ReadUInt(pReader, pos, idlen);

+    assert(id >= 0);  //TODO

+

+    if ((unsigned long)id != id_)

+        return false;

+

+    long sizelen;

+    const long long size = ReadUInt(pReader, pos + idlen, sizelen);

+

+    switch (size)

+    {

+        case 4:

+        case 8:

+            break;

+        default:

+            return false;

+    }

+

+    pos += idlen + sizelen;  //consume id and size fields

+    assert((pos + size) <= available);

+

+    if (size == 4)

+        val = Unserialize4Float(pReader, pos);

+    else

+    {

+        assert(size == 8);

+        val = Unserialize8Double(pReader, pos);

+    }

+

+    pos += size;  //consume size of payload

+

+    return true;

+}

+

+

+bool mkvparser::Match(

+    IMkvReader* pReader,

+    long long& pos,

+    unsigned long id_,

+    short& val)

+{

+    assert(pReader);

+    assert(pos >= 0);

+

+    long long total, available;

+

+    long hr = pReader->Length(&total, &available);

+    assert(hr >= 0);

+    assert(available <= total);

+

+    long len;

+    const long long id = ReadUInt(pReader, pos, len);

+    assert(id >= 0);

+    assert((pos + len) <= available);

+

+    if ((unsigned long)id != id_)

+        return false;

+

+    pos += len;  //consume id

+

+    const long long size = ReadUInt(pReader, pos, len);

+    assert(size <= 2);

+    assert((pos + len) <= available);

+

+    pos += len;  //consume length of size of payload

+    assert((pos + size) <= available);

+

+    //TODO:

+    // Generalize this to work for any size signed int

+    if (size == 1)

+        val = Unserialize1SInt(pReader, pos);

+    else

+        val = Unserialize2SInt(pReader, pos);

+

+    pos += size;  //consume size of payload

+

+    return true;

+}

+

+

+namespace mkvparser

+{

+

+EBMLHeader::EBMLHeader():

+    m_docType(NULL)

+{

+}

+

+EBMLHeader::~EBMLHeader()

+{

+    delete[] m_docType;

+}

+

+long long EBMLHeader::Parse(

+    IMkvReader* pReader,

+    long long& pos)

+{

+    assert(pReader);

+

+    long long total, available;

+

+    long hr = pReader->Length(&total, &available);

+

+    if (hr < 0)

+        return hr;

+

+    pos = 0;

+    long long end = (1024 < available)? 1024: available;

+

+    for (;;)

+    {

+        unsigned char b = 0;

+

+        while (pos < end)

+        {

+            hr = pReader->Read(pos, 1, &b);

+

+            if (hr < 0)

+                return hr;

+

+            if (b == 0x1A)

+                break;

+

+            ++pos;

+        }

+

+        if (b != 0x1A)

+        {

+            if ((pos >= 1024) ||

+                (available >= total) ||

+                ((total - available) < 5))

+                  return -1;

+

+            return available + 5;  //5 = 4-byte ID + 1st byte of size

+        }

+

+        if ((total - pos) < 5)

+            return E_FILE_FORMAT_INVALID;

+

+        if ((available - pos) < 5)

+            return pos + 5;  //try again later

+

+        long len;

+

+        const long long result = ReadUInt(pReader, pos, len);

+

+        if (result < 0)  //error

+            return result;

+

+        if (result == 0x0A45DFA3)  //ReadId masks-off length indicator bits

+        {

+            assert(len == 4);

+            pos += len;

+            break;

+        }

+

+        ++pos;  //throw away just the 0x1A byte, and try again

+    }

+

+    long len;

+    long long result = GetUIntLength(pReader, pos, len);

+

+    if (result < 0)  //error

+        return result;

+

+    if (result > 0)  //need more data

+        return result;

+

+    assert(len > 0);

+    assert(len <= 8);

+

+    if ((total -  pos) < len)

+        return E_FILE_FORMAT_INVALID;

+    if ((available - pos) < len)

+        return pos + len;  //try again later

+

+    result = ReadUInt(pReader, pos, len);

+

+    if (result < 0)  //error

+        return result;

+

+    pos += len;  //consume u-int

+

+    if ((total - pos) < result)

+        return E_FILE_FORMAT_INVALID;

+

+    if ((available - pos) < result)

+        return pos + result;

+

+    end = pos + result;

+

+    m_version = 1;

+    m_readVersion = 1;

+    m_maxIdLength = 4;

+    m_maxSizeLength = 8;

+    m_docTypeVersion = 1;

+    m_docTypeReadVersion = 1;

+

+    while (pos < end)

+    {

+        if (Match(pReader, pos, 0x0286, m_version))

+            ;

+        else if (Match(pReader, pos, 0x02F7, m_readVersion))

+            ;

+        else if (Match(pReader, pos, 0x02F2, m_maxIdLength))

+            ;

+        else if (Match(pReader, pos, 0x02F3, m_maxSizeLength))

+            ;

+        else if (Match(pReader, pos, 0x0282, m_docType))

+            ;

+        else if (Match(pReader, pos, 0x0287, m_docTypeVersion))

+            ;

+        else if (Match(pReader, pos, 0x0285, m_docTypeReadVersion))

+            ;

+        else

+        {

+            result = ReadUInt(pReader, pos, len);

+            assert(result > 0);

+            assert(len > 0);

+            assert(len <= 8);

+

+            pos += len;

+            assert(pos < end);

+

+            result = ReadUInt(pReader, pos, len);

+            assert(result >= 0);

+            assert(len > 0);

+            assert(len <= 8);

+

+            pos += len + result;

+            assert(pos <= end);

+        }

+    }

+

+    assert(pos == end);

+

+    return 0;

+}

+

+

+Segment::Segment(

+    IMkvReader* pReader,

+    long long start,

+    long long size) :

+    m_pReader(pReader),

+    m_start(start),

+    m_size(size),

+    m_pos(start),

+    m_pInfo(NULL),

+    m_pTracks(NULL),

+    m_pCues(NULL),

+    m_clusters(NULL),

+    m_clusterCount(0),

+    m_clusterPreloadCount(0),

+    m_clusterSize(0)

+{

+}

+

+

+Segment::~Segment()

+{

+    const long count = m_clusterCount + m_clusterPreloadCount;

+

+    Cluster** i = m_clusters;

+    Cluster** j = m_clusters + count;

+

+    while (i != j)

+    {

+        Cluster* const p = *i++;

+        assert(p);

+

+        delete p;

+    }

+

+    delete[] m_clusters;

+

+    delete m_pTracks;

+    delete m_pInfo;

+    delete m_pCues;

+}

+

+

+long long Segment::CreateInstance(

+    IMkvReader* pReader,

+    long long pos,

+    Segment*& pSegment)

+{

+    assert(pReader);

+    assert(pos >= 0);

+

+    pSegment = NULL;

+

+    long long total, available;

+

+    long hr = pReader->Length(&total, &available);

+    assert(hr >= 0);

+    assert(available <= total);

+

+    //I would assume that in practice this loop would execute

+    //exactly once, but we allow for other elements (e.g. Void)

+    //to immediately follow the EBML header.  This is fine for

+    //the source filter case (since the entire file is available),

+    //but in the splitter case over a network we should probably

+    //just give up early.  We could for example decide only to

+    //execute this loop a maximum of, say, 10 times.

+

+    while (pos < total)

+    {

+        //Read ID

+

+        long len;

+        long long result = GetUIntLength(pReader, pos, len);

+

+        if (result)  //error, or too few available bytes

+            return result;

+

+        if ((pos + len) > total)

+            return E_FILE_FORMAT_INVALID;

+

+        if ((pos + len) > available)

+            return pos + len;

+

+        //TODO: if we liberalize the behavior of ReadUInt, we can

+        //probably eliminate having to use GetUIntLength here.

+        const long long id = ReadUInt(pReader, pos, len);

+

+        if (id < 0)  //error

+            return id;

+

+        pos += len;  //consume ID

+

+        //Read Size

+

+        result = GetUIntLength(pReader, pos, len);

+

+        if (result)  //error, or too few available bytes

+            return result;

+

+        if ((pos + len) > total)

+            return E_FILE_FORMAT_INVALID;

+

+        if ((pos + len) > available)

+            return pos + len;

+

+        //TODO: if we liberalize the behavior of ReadUInt, we can

+        //probably eliminate having to use GetUIntLength here.

+        const long long size = ReadUInt(pReader, pos, len);

+

+        if (size < 0)

+            return size;

+

+        pos += len;  //consume length of size of element

+

+        //Pos now points to start of payload

+

+        if ((pos + size) > total)

+            return E_FILE_FORMAT_INVALID;

+

+        if (id == 0x08538067)  //Segment ID

+        {

+            pSegment = new  Segment(pReader, pos, size);

+            assert(pSegment);  //TODO

+

+            return 0;    //success

+        }

+

+        pos += size;  //consume payload

+    }

+

+    assert(pos == total);

+

+    pSegment = new Segment(pReader, pos, 0);

+    assert(pSegment);  //TODO

+

+    return 0;  //success (sort of)

+}

+

+

+long long Segment::ParseHeaders()

+{

+    //Outermost (level 0) segment object has been constructed,

+    //and pos designates start of payload.  We need to find the

+    //inner (level 1) elements.

+    long long total, available;

+

+    long hr = m_pReader->Length(&total, &available);

+    assert(hr >= 0);

+    assert(available <= total);

+

+    const long long stop = m_start + m_size;

+    assert(stop <= total);

+    assert(m_pos <= stop);

+

+    bool bQuit = false;

+

+    while ((m_pos < stop) && !bQuit)

+    {

+        long long pos = m_pos;

+

+        long len;

+        long long result = GetUIntLength(m_pReader, pos, len);

+

+        if (result)  //error, or too few available bytes

+            return result;

+

+        if ((pos + len) > stop)

+            return E_FILE_FORMAT_INVALID;

+

+        if ((pos + len) > available)

+            return pos + len;

+

+        const long long idpos = pos;

+        const long long id = ReadUInt(m_pReader, idpos, len);

+

+        if (id < 0)  //error

+            return id;

+

+        pos += len;  //consume ID

+

+        //Read Size

+        result = GetUIntLength(m_pReader, pos, len);

+

+        if (result)  //error, or too few available bytes

+            return result;

+

+        if ((pos + len) > stop)

+            return E_FILE_FORMAT_INVALID;

+

+        if ((pos + len) > available)

+            return pos + len;

+

+        const long long size = ReadUInt(m_pReader, pos, len);

+

+        if (size < 0)

+            return size;

+

+        pos += len;  //consume length of size of element

+

+        //Pos now points to start of payload

+

+        if ((pos + size) > stop)

+            return E_FILE_FORMAT_INVALID;

+

+        //We read EBML elements either in total or nothing at all.

+

+        if ((pos + size) > available)

+            return pos + size;

+

+        if (id == 0x0549A966)  //Segment Info ID

+        {

+            assert(m_pInfo == NULL);

+

+            m_pInfo = new SegmentInfo(this, pos, size);

+            assert(m_pInfo);  //TODO

+        }

+        else if (id == 0x0654AE6B)  //Tracks ID

+        {

+            assert(m_pTracks == NULL);

+

+            m_pTracks = new Tracks(this, pos, size);

+            assert(m_pTracks);  //TODO

+        }

+        else if (id == 0x0C53BB6B)  //Cues ID

+        {

+            if (m_pCues == NULL)

+            {

+                m_pCues = new Cues(this, pos, size);

+                assert(m_pCues);  //TODO

+            }

+        }

+        else if (id == 0x014D9B74)  //SeekHead ID

+        {

+            ParseSeekHead(pos, size);

+        }

+        else if (id == 0x0F43B675)  //Cluster ID

+        {

+            bQuit = true;

+        }

+

+        if (!bQuit)

+            m_pos = pos + size;  //consume payload

+    }

+

+    assert(m_pos <= stop);

+

+    if (m_pInfo == NULL)  //TODO: liberalize this behavior

+        return E_FILE_FORMAT_INVALID;

+

+    if (m_pTracks == NULL)

+        return E_FILE_FORMAT_INVALID;

+

+    return 0;  //success

+}

+

+

+#if 0

+long Segment::ParseCluster(Cluster*& pCluster, long long& pos_) const

+{

+    pCluster = NULL;

+    pos_ = -1;

+

+    const long long stop = m_start + m_size;

+    assert(m_pos <= stop);

+

+    long long pos = m_pos;

+    long long off = -1;

+

+    while (pos < stop)

+    {

+        long len;

+        const long long idpos = pos;

+

+        const long long id = SyncReadUInt(m_pReader, pos, stop, len);

+

+        if (id < 0)  //error

+            return static_cast<long>(id);

+

+        if (id == 0)

+            return E_FILE_FORMAT_INVALID;

+

+        pos += len;  //consume id

+        assert(pos < stop);

+

+        const long long size = SyncReadUInt(m_pReader, pos, stop, len);

+

+        if (size < 0)  //error

+            return static_cast<long>(size);

+

+        pos += len;  //consume size

+        assert(pos <= stop);

+

+        if (size == 0)  //weird

+            continue;

+

+        //pos now points to start of payload

+

+        pos += size;  //consume payload

+        assert(pos <= stop);

+

+        if (id == 0x0F43B675)  //Cluster ID

+        {

+            off = idpos - m_start;  // >= 0 means we found a cluster

+            break;

+        }

+    }

+

+    assert(pos <= stop);

+

+    //Indicate to caller how much of file has been consumed. This is

+    //used later in AddCluster to adjust the current parse position

+    //(the value cached in the segment object itself) to the

+    //file position value just past the cluster we parsed.

+

+    if (off < 0)  //we did not found any more clusters

+    {

+        pos_ = stop;  //pos_ >= 0 here means EOF (cluster is NULL)

+        return 0;     //TODO: confirm this return value

+    }

+

+    //We found a cluster.  Now read something, to ensure that it is

+    //fully loaded in the network cache.

+

+    if (pos >= stop)  //we parsed the entire segment

+    {

+        //We did find a cluster, but it was very last element in the segment.

+        //Our preference is that the loop above runs 1 1/2 times:

+        //the first pass finds the cluster, and the second pass

+        //finds the element the follows the cluster.  In this case, however,

+        //we reached the end of the file without finding another element,

+        //so we didn't actually read anything yet associated with "end of the

+        //cluster".  And we must perform an actual read, in order

+        //to guarantee that all of the data that belongs to this

+        //cluster has been loaded into the network cache.  So instead

+        //of reading the next element that follows the cluster, we

+        //read the last byte of the cluster (which is also the last

+        //byte in the file).

+

+        //Read the last byte of the file. (Reading 0 bytes at pos

+        //might work too -- it would depend on how the reader is

+        //implemented.  Here we take the more conservative approach,

+        //since this makes fewer assumptions about the network

+        //reader abstraction.)

+

+        unsigned char b;

+

+        const int result = m_pReader->Read(pos - 1, 1, &b);

+        assert(result == 0);

+

+        pos_ = stop;

+    }

+    else

+    {

+        long len;

+        const long long idpos = pos;

+

+        const long long id = SyncReadUInt(m_pReader, pos, stop, len);

+

+        if (id < 0)  //error

+            return static_cast<long>(id);

+

+        if (id == 0)

+            return E_BUFFER_NOT_FULL;

+

+        pos += len;  //consume id

+        assert(pos < stop);

+

+        const long long size = SyncReadUInt(m_pReader, pos, stop, len);

+

+        if (size < 0)  //error

+            return static_cast<long>(size);

+

+        pos_ = idpos;

+    }

+

+    //We found a cluster, and it has been completely loaded into the

+    //network cache.  (We can guarantee this because we actually read

+    //the EBML tag that follows the cluster, or, if we reached EOF,

+    //because we actually read the last byte of the cluster).

+

+    Segment* const this_ = const_cast<Segment*>(this);

+

+    pCluster = Cluster::Parse(this_, m_clusterCount, off);

+    assert(pCluster);

+    assert(pCluster->m_index == m_clusterCount);

+

+    return 0;

+}

+

+

+bool Segment::AddCluster(Cluster* pCluster, long long pos)

+{

+    assert(pos >= m_start);

+

+    const long long stop = m_start + m_size;

+    assert(pos <= stop);

+

+    if (pCluster)

+    {

+        AppendCluster(pCluster);

+        assert(m_clusters);

+        assert(m_clusterSize > pCluster->m_index);

+        assert(m_clusters[pCluster->m_index] == pCluster);

+    }

+

+    m_pos = pos;  //m_pos >= stop is now we know we have all clusters

+

+    return (pos >= stop);

+}

+#endif

+

+

+long Segment::LoadCluster()

+{

+    const long long stop = m_start + m_size;

+

+    while (m_pos < stop)

+    {

+        long long pos = m_pos;

+

+        long len;

+

+        long long result = GetUIntLength(m_pReader, pos, len);

+

+        if (result < 0)  //error

+            return static_cast<long>(result);

+

+        if ((pos + len) > stop)

+            return E_FILE_FORMAT_INVALID;

+

+        const long long idpos = pos;

+        const long long id = ReadUInt(m_pReader, idpos, len);

+

+        if (id < 0)  //error

+            return static_cast<long>(id);

+

+        pos += len;  //consume ID

+

+        //Read Size

+        result = GetUIntLength(m_pReader, pos, len);

+

+        if (result < 0)  //error

+            return static_cast<long>(result);

+

+        if ((pos + len) > stop)

+            return E_FILE_FORMAT_INVALID;

+

+        const long long size = ReadUInt(m_pReader, pos, len);

+

+        if (size < 0)  //error

+            return static_cast<long>(size);

+

+        pos += len;  //consume length of size of element

+

+        if (size == 0)  //weird

+        {

+            m_pos = pos;

+            continue;

+        }

+

+        //Pos now points to start of payload

+

+        if ((pos + size) > stop)

+            return E_FILE_FORMAT_INVALID;

+

+        if (id == 0x0C53BB6B)  //Cues ID

+        {

+            if (m_pCues == NULL)

+            {

+                m_pCues = new Cues(this, pos, size);

+                assert(m_pCues);  //TODO

+            }

+

+            m_pos = pos + size;  //consume payload

+            continue;

+        }

+

+        if (id != 0x0F43B675)  //Cluster ID

+        {

+            m_pos = pos + size;  //consume payload

+            continue;

+        }

+

+        const long idx = m_clusterCount;

+        const long long idoff = idpos - m_start;

+

+        if (m_clusterPreloadCount > 0)

+        {

+            assert(idx < m_clusterSize);

+

+            Cluster* const pCluster = m_clusters[idx];

+            assert(pCluster);

+            assert(pCluster->m_index < 0);

+

+            const long long off_ = pCluster->m_pos;

+            assert(off_);

+

+            const long long off = off_ * ((off_ >= 0) ? 1 : -1);

+            assert(idoff <= off);

+

+            if (idoff == off)  //cluster has been preloaded already

+            {

+                pCluster->m_index = idx;

+                ++m_clusterCount;

+                --m_clusterPreloadCount;

+

+                m_pos = pos + size;  //consume payload

+                break;

+            }

+        }

+

+        Cluster* const pCluster = Cluster::Parse(this, idx, idoff);

+        assert(pCluster);

+        assert(pCluster->m_index == idx);

+

+        AppendCluster(pCluster);

+        assert(m_clusters);

+        assert(idx < m_clusterSize);

+        assert(m_clusters[idx] == pCluster);

+

+        m_pos = pos + size;  //consume payload

+        break;

+    }

+

+    assert(m_pos <= stop);

+    return 0;

+}

+

+

+void Segment::AppendCluster(Cluster* pCluster)

+{

+    assert(pCluster);

+    assert(pCluster->m_index >= 0);

+

+    const long count = m_clusterCount + m_clusterPreloadCount;

+

+    long& size = m_clusterSize;

+    assert(size >= count);

+

+    const long idx = pCluster->m_index;

+    assert(idx == m_clusterCount);

+

+    if (count >= size)

+    {

+        long n;

+

+        if (size > 0)

+            n = 2 * size;

+        else if (m_pInfo == 0)

+            n = 2048;

+        else

+        {

+            const long long ns = m_pInfo->GetDuration();

+

+            if (ns <= 0)

+                n = 2048;

+            else

+            {

+                const long long sec = (ns + 999999999LL) / 1000000000LL;

+                n = static_cast<long>(sec);

+            }

+        }

+

+        Cluster** const qq = new Cluster*[n];

+        Cluster** q = qq;

+

+        Cluster** p = m_clusters;

+        Cluster** const pp = p + count;

+

+        while (p != pp)

+            *q++ = *p++;

+

+        delete[] m_clusters;

+

+        m_clusters = qq;

+        size = n;

+    }

+

+    if (m_clusterPreloadCount > 0)

+    {

+        assert(m_clusters);

+

+        Cluster** const p = m_clusters + m_clusterCount;

+        assert(*p);

+        assert((*p)->m_index < 0);

+

+        Cluster** q = p + m_clusterPreloadCount;

+        assert(q < (m_clusters + size));

+

+        for (;;)

+        {

+            Cluster** const qq = q - 1;

+            assert((*qq)->m_index < 0);

+

+            *q = *qq;

+            q = qq;

+

+            if (q == p)

+                break;

+        }

+    }

+

+    m_clusters[idx] = pCluster;

+    ++m_clusterCount;

+}

+

+

+void Segment::PreloadCluster(Cluster* pCluster, ptrdiff_t idx)

+{

+    assert(pCluster);

+    assert(pCluster->m_index < 0);

+    assert(idx >= m_clusterCount);

+

+    const long count = m_clusterCount + m_clusterPreloadCount;

+

+    long& size = m_clusterSize;

+    assert(size >= count);

+

+    if (count >= size)

+    {

+        long n;

+

+        if (size > 0)

+            n = 2 * size;

+        else if (m_pInfo == 0)

+            n = 2048;

+        else

+        {

+            const long long ns = m_pInfo->GetDuration();

+

+            if (ns <= 0)

+                n = 2048;

+            else

+            {

+                const long long sec = (ns + 999999999LL) / 1000000000LL;

+                n = static_cast<long>(sec);

+            }

+        }

+

+        Cluster** const qq = new Cluster*[n];

+        Cluster** q = qq;

+

+        Cluster** p = m_clusters;

+        Cluster** const pp = p + count;

+

+        while (p != pp)

+            *q++ = *p++;

+

+        delete[] m_clusters;

+

+        m_clusters = qq;

+        size = n;

+    }

+

+    assert(m_clusters);

+

+    Cluster** const p = m_clusters + idx;

+

+    Cluster** q = m_clusters + count;

+    assert(q >= p);

+    assert(q < (m_clusters + size));

+

+    while (q > p)

+    {

+        Cluster** const qq = q - 1;

+        assert((*qq)->m_index < 0);

+

+        *q = *qq;

+        q = qq;

+    }

+

+    m_clusters[idx] = pCluster;

+    ++m_clusterPreloadCount;

+}

+

+

+long Segment::Load()

+{

+    assert(m_clusters == NULL);

+    assert(m_clusterSize == 0);

+    assert(m_clusterCount == 0);

+

+    //Outermost (level 0) segment object has been constructed,

+    //and pos designates start of payload.  We need to find the

+    //inner (level 1) elements.

+    const long long stop = m_start + m_size;

+

+#ifdef _DEBUG  //TODO: this is really Microsoft-specific

+    {

+        long long total, available;

+

+        long hr = m_pReader->Length(&total, &available);

+        assert(hr >= 0);

+        assert(available >= total);

+        assert(stop <= total);

+    }

+#endif

+

+    while (m_pos < stop)

+    {

+        long long pos = m_pos;

+

+        long len;

+

+        long long result = GetUIntLength(m_pReader, pos, len);

+

+        if (result < 0)  //error

+            return static_cast<long>(result);

+

+        if ((pos + len) > stop)

+            return E_FILE_FORMAT_INVALID;

+

+        const long long idpos = pos;

+        const long long id = ReadUInt(m_pReader, idpos, len);

+

+        if (id < 0)  //error

+            return static_cast<long>(id);

+

+        pos += len;  //consume ID

+

+        //Read Size

+        result = GetUIntLength(m_pReader, pos, len);

+

+        if (result < 0)  //error

+            return static_cast<long>(result);

+

+        if ((pos + len) > stop)

+            return E_FILE_FORMAT_INVALID;

+

+        const long long size = ReadUInt(m_pReader, pos, len);

+

+        if (size < 0)  //error

+            return static_cast<long>(size);

+

+        pos += len;  //consume length of size of element

+

+        //Pos now points to start of payload

+

+        if ((pos + size) > stop)

+            return E_FILE_FORMAT_INVALID;

+

+        if (id == 0x0F43B675)  //Cluster ID

+        {

+            const long idx = m_clusterCount;

+            const long long off = idpos - m_start;

+

+            Cluster* const pCluster = Cluster::Parse(this, idx, off);

+            assert(pCluster);

+            assert(pCluster->m_index == idx);

+

+            AppendCluster(pCluster);

+            assert(m_clusters);

+            assert(m_clusterSize > idx);

+            assert(m_clusters[idx] == pCluster);

+        }

+        else if (id == 0x0C53BB6B)  //Cues ID

+        {

+            assert(m_pCues == NULL);

+

+            m_pCues = new Cues(this, pos, size);

+            assert(m_pCues);  //TODO

+        }

+        else if (id == 0x0549A966)  //SegmentInfo ID

+        {

+            assert(m_pInfo == NULL);

+

+            m_pInfo = new  SegmentInfo(this, pos, size);

+            assert(m_pInfo);

+        }

+        else if (id == 0x0654AE6B)  //Tracks ID

+        {

+            assert(m_pTracks == NULL);

+

+            m_pTracks = new Tracks(this, pos, size);

+            assert(m_pTracks);  //TODO

+        }

+

+        m_pos = pos + size;  //consume payload

+    }

+

+    assert(m_pos >= stop);

+

+    if (m_pInfo == NULL)

+        return E_FILE_FORMAT_INVALID;  //TODO: ignore this case?

+

+    if (m_pTracks == NULL)

+        return E_FILE_FORMAT_INVALID;

+

+    if (m_clusters == NULL)  //TODO: ignore this case?

+        return E_FILE_FORMAT_INVALID;

+

+    //TODO: decide whether we require Cues element

+    //if (m_pCues == NULL)

+    //   return E_FILE_FORMAT_INVALID;

+

+    return 0;

+}

+

+

+void Segment::ParseSeekHead(long long start, long long size_)

+{

+    long long pos = start;

+    const long long stop = start + size_;

+

+    while (pos < stop)

+    {

+        long len;

+

+        const long long id = ReadUInt(m_pReader, pos, len);

+        assert(id >= 0);  //TODO

+        assert((pos + len) <= stop);

+

+        pos += len;  //consume ID

+

+        const long long size = ReadUInt(m_pReader, pos, len);

+        assert(size >= 0);

+        assert((pos + len) <= stop);

+

+        pos += len;  //consume Size field

+        assert((pos + size) <= stop);

+

+        if (id == 0x0DBB)  //SeekEntry ID

+            ParseSeekEntry(pos, size);

+

+        pos += size;  //consume payload

+        assert(pos <= stop);

+    }

+

+    assert(pos == stop);

+}

+

+

+void Segment::ParseCues(long long off)

+{

+    if (m_pCues)

+        return;

+

+    //odbgstream os;

+    //os << "Segment::ParseCues (begin)" << endl;

+

+    long long pos = m_start + off;

+    const long long stop = m_start + m_size;

+

+    long len;

+

+    long long result = GetUIntLength(m_pReader, pos, len);

+    assert(result == 0);

+    assert((pos + len) <= stop);

+

+    const long long idpos = pos;

+

+    const long long id = ReadUInt(m_pReader, idpos, len);

+    assert(id == 0x0C53BB6B);  //Cues ID

+

+    pos += len;  //consume ID

+    assert(pos < stop);

+

+    //Read Size

+

+    result = GetUIntLength(m_pReader, pos, len);

+    assert(result == 0);

+    assert((pos + len) <= stop);

+

+    const long long size = ReadUInt(m_pReader, pos, len);

+    assert(size >= 0);

+

+    pos += len;  //consume length of size of element

+    assert((pos + size) <= stop);

+

+    //Pos now points to start of payload

+

+    m_pCues = new Cues(this, pos, size);

+    assert(m_pCues);  //TODO

+

+    //os << "Segment::ParseCues (end)" << endl;

+}

+

+

+void Segment::ParseSeekEntry(

+   long long start,

+   long long size_)

+{

+    long long pos = start;

+

+    const long long stop = start + size_;

+

+    long len;

+

+    const long long seekIdId = ReadUInt(m_pReader, pos, len);

+    //seekIdId;

+    assert(seekIdId == 0x13AB);  //SeekID ID

+    assert((pos + len) <= stop);

+

+    pos += len;  //consume id

+

+    const long long seekIdSize = ReadUInt(m_pReader, pos, len);

+    assert(seekIdSize >= 0);

+    assert((pos + len) <= stop);

+

+    pos += len;  //consume size

+

+    const long long seekId = ReadUInt(m_pReader, pos, len);  //payload

+    assert(seekId >= 0);

+    assert(len == seekIdSize);

+    assert((pos + len) <= stop);

+

+    pos += seekIdSize;  //consume payload

+

+    const long long seekPosId = ReadUInt(m_pReader, pos, len);

+    //seekPosId;

+    assert(seekPosId == 0x13AC);  //SeekPos ID

+    assert((pos + len) <= stop);

+

+    pos += len;  //consume id

+

+    const long long seekPosSize = ReadUInt(m_pReader, pos, len);

+    assert(seekPosSize >= 0);

+    assert((pos + len) <= stop);

+

+    pos += len;  //consume size

+    assert((pos + seekPosSize) <= stop);

+

+    const long long seekOff = UnserializeUInt(m_pReader, pos, seekPosSize);

+    assert(seekOff >= 0);

+    assert(seekOff < m_size);

+

+    pos += seekPosSize;  //consume payload

+    assert(pos == stop);

+

+    const long long seekPos = m_start + seekOff;

+    assert(seekPos < (m_start + m_size));

+

+    if (seekId == 0x0C53BB6B)  //Cues ID

+        ParseCues(seekOff);

+}

+

+

+Cues::Cues(Segment* pSegment, long long start_, long long size_) :

+    m_pSegment(pSegment),

+    m_start(start_),

+    m_size(size_),

+    m_cue_points(NULL),

+    m_count(0),

+    m_preload_count(0),

+    m_pos(start_)

+{

+}

+

+

+Cues::~Cues()

+{

+    const size_t n = m_count + m_preload_count;

+

+    CuePoint** p = m_cue_points;

+    CuePoint** const q = p + n;

+

+    while (p != q)

+    {

+        CuePoint* const pCP = *p++;

+        assert(pCP);

+

+        delete pCP;

+    }

+

+    delete[] m_cue_points;

+}

+

+

+void Cues::Init() const

+{

+    if (m_cue_points)

+        return;

+

+    assert(m_count == 0);

+    assert(m_preload_count == 0);

+

+    IMkvReader* const pReader = m_pSegment->m_pReader;

+

+    const long long stop = m_start + m_size;

+    long long pos = m_start;

+

+    size_t cue_points_size = 0;

+

+    while (pos < stop)

+    {

+        const long long idpos = pos;

+

+        long len;

+

+        const long long id = ReadUInt(pReader, pos, len);

+        assert(id >= 0);  //TODO

+        assert((pos + len) <= stop);

+

+        pos += len;  //consume ID

+

+        const long long size = ReadUInt(pReader, pos, len);

+        assert(size >= 0);

+        assert((pos + len) <= stop);

+

+        pos += len;  //consume Size field

+        assert((pos + size) <= stop);

+

+        if (id == 0x3B)  //CuePoint ID

+            PreloadCuePoint(cue_points_size, idpos);

+

+        pos += size;  //consume payload

+        assert(pos <= stop);

+    }

+}

+

+

+void Cues::PreloadCuePoint(

+    size_t& cue_points_size,

+    long long pos) const

+{

+    assert(m_count == 0);

+

+    if (m_preload_count >= cue_points_size)

+    {

+        size_t n;

+

+        if (cue_points_size > 0)

+            n = static_cast<size_t>(2 * cue_points_size);

+        else

+        {

+            const SegmentInfo* const pInfo = m_pSegment->GetInfo();

+

+            if (pInfo == NULL)

+                n = 2048;

+            else

+            {

+                const long long ns = pInfo->GetDuration();

+

+                if (ns <= 0)

+                    n = 2048;

+                else

+                {

+                    const long long sec = (ns + 999999999LL) / 1000000000LL;

+                    n = static_cast<size_t>(sec);

+                }

+            }

+        }

+

+        CuePoint** const qq = new CuePoint*[n];

+        CuePoint** q = qq;  //beginning of target

+

+        CuePoint** p = m_cue_points;                //beginning of source

+        CuePoint** const pp = p + m_preload_count;  //end of source

+

+        while (p != pp)

+            *q++ = *p++;

+

+        delete[] m_cue_points;

+

+        m_cue_points = qq;

+        cue_points_size = n;

+    }

+

+    CuePoint* const pCP = new CuePoint(m_preload_count, pos);

+    m_cue_points[m_preload_count++] = pCP;

+}

+

+

+bool Cues::LoadCuePoint() const

+{

+    //odbgstream os;

+    //os << "Cues::LoadCuePoint" << endl;

+

+    const long long stop = m_start + m_size;

+

+    if (m_pos >= stop)

+        return false;  //nothing else to do

+

+    Init();

+

+    IMkvReader* const pReader = m_pSegment->m_pReader;

+

+    while (m_pos < stop)

+    {

+        const long long idpos = m_pos;

+

+        long len;

+

+        const long long id = ReadUInt(pReader, m_pos, len);

+        assert(id >= 0);  //TODO

+        assert((m_pos + len) <= stop);

+

+        m_pos += len;  //consume ID

+

+        const long long size = ReadUInt(pReader, m_pos, len);

+        assert(size >= 0);

+        assert((m_pos + len) <= stop);

+

+        m_pos += len;  //consume Size field

+        assert((m_pos + size) <= stop);

+

+        if (id != 0x3B)  //CuePoint ID

+        {

+            m_pos += size;  //consume payload

+            assert(m_pos <= stop);

+

+            continue;

+        }

+

+        assert(m_preload_count > 0);

+

+        CuePoint* const pCP = m_cue_points[m_count];

+        assert(pCP);

+        assert((pCP->GetTimeCode() >= 0) || (-pCP->GetTimeCode() == idpos));

+

+        pCP->Load(pReader);

+        ++m_count;

+        --m_preload_count;

+

+        m_pos += size;  //consume payload

+        assert(m_pos <= stop);

+

+        break;

+    }

+

+    return (m_pos < stop);

+}

+

+

+bool Cues::Find(

+    long long time_ns,

+    const Track* pTrack,

+    const CuePoint*& pCP,

+    const CuePoint::TrackPosition*& pTP) const

+{

+    assert(time_ns >= 0);

+    assert(pTrack);

+

+    LoadCuePoint();

+

+    assert(m_cue_points);

+    assert(m_count > 0);

+

+    CuePoint** const ii = m_cue_points;

+    CuePoint** i = ii;

+

+    CuePoint** const jj = ii + m_count + m_preload_count;

+    CuePoint** j = jj;

+

+    pCP = *i;

+    assert(pCP);

+

+    if (time_ns <= pCP->GetTime(m_pSegment))

+    {

+        pTP = pCP->Find(pTrack);

+        return (pTP != NULL);

+    }

+

+    IMkvReader* const pReader = m_pSegment->m_pReader;

+

+    while (i < j)

+    {

+        //INVARIANT:

+        //[ii, i) <= time_ns

+        //[i, j)  ?

+        //[j, jj) > time_ns

+

+        CuePoint** const k = i + (j - i) / 2;

+        assert(k < jj);

+

+        CuePoint* const pCP = *k;

+        assert(pCP);

+

+        pCP->Load(pReader);

+

+        const long long t = pCP->GetTime(m_pSegment);

+

+        if (t <= time_ns)

+            i = k + 1;

+        else

+            j = k;

+

+        assert(i <= j);

+    }

+

+    assert(i == j);

+    assert(i <= jj);

+    assert(i > ii);

+

+    pCP = *--i;

+    assert(pCP);

+    assert(pCP->GetTime(m_pSegment) <= time_ns);

+

+    //TODO: here and elsewhere, it's probably not correct to search

+    //for the cue point with this time, and then search for a matching

+    //track.  In principle, the matching track could be on some earlier

+    //cue point, and with our current algorithm, we'd miss it.  To make

+    //this bullet-proof, we'd need to create a secondary structure,

+    //with a list of cue points that apply to a track, and then search

+    //that track-based structure for a matching cue point.

+

+    pTP = pCP->Find(pTrack);

+    return (pTP != NULL);

+}

+

+

+#if 0

+bool Cues::FindNext(

+    long long time_ns,

+    const Track* pTrack,

+    const CuePoint*& pCP,

+    const CuePoint::TrackPosition*& pTP) const

+{

+    pCP = 0;

+    pTP = 0;

+

+    if (m_count == 0)

+        return false;

+

+    assert(m_cue_points);

+

+    const CuePoint* const* const ii = m_cue_points;

+    const CuePoint* const* i = ii;

+

+    const CuePoint* const* const jj = ii + m_count;

+    const CuePoint* const* j = jj;

+

+    while (i < j)

+    {

+        //INVARIANT:

+        //[ii, i) <= time_ns

+        //[i, j)  ?

+        //[j, jj) > time_ns

+

+        const CuePoint* const* const k = i + (j - i) / 2;

+        assert(k < jj);

+

+        pCP = *k;

+        assert(pCP);

+

+        const long long t = pCP->GetTime(m_pSegment);

+

+        if (t <= time_ns)

+            i = k + 1;

+        else

+            j = k;

+

+        assert(i <= j);

+    }

+

+    assert(i == j);

+    assert(i <= jj);

+

+    if (i >= jj)  //time_ns is greater than max cue point

+        return false;

+

+    pCP = *i;

+    assert(pCP);

+    assert(pCP->GetTime(m_pSegment) > time_ns);

+

+    pTP = pCP->Find(pTrack);

+    return (pTP != NULL);

+}

+#endif

+

+

+const CuePoint* Cues::GetFirst() const

+{

+    LoadCuePoint();  //init cues

+

+    const size_t count = m_count + m_preload_count;

+

+    if (count == 0)  //weird

+        return NULL;

+

+    CuePoint* const* const pp = m_cue_points;

+    assert(pp);

+

+    CuePoint* const pCP = pp[0];

+    assert(pCP);

+    assert(pCP->GetTimeCode() >= 0);

+

+    return pCP;

+}

+

+

+const CuePoint* Cues::GetLast() const

+{

+    LoadCuePoint();  //init cues

+

+    const size_t count = m_count + m_preload_count;

+

+    if (count == 0)  //weird

+        return NULL;

+

+    const size_t index = count - 1;

+

+    CuePoint* const* const pp = m_cue_points;

+    assert(pp);

+

+    CuePoint* const pCP = pp[index];

+    assert(pCP);

+

+    pCP->Load(m_pSegment->m_pReader);

+    assert(pCP->GetTimeCode() >= 0);

+

+    return pCP;

+}

+

+

+const CuePoint* Cues::GetNext(const CuePoint* pCurr) const

+{

+    if (pCurr == NULL)

+        return NULL;

+

+    assert(pCurr->GetTimeCode() >= 0);

+    assert(m_cue_points);

+    assert(m_count >= 1);

+

+    const size_t count = m_count + m_preload_count;

+

+    size_t index = pCurr->m_index;

+    assert(index < count);

+

+    CuePoint* const* const pp = m_cue_points;

+    assert(pp);

+    assert(pp[index] == pCurr);

+

+    ++index;

+

+    if (index >= count)

+        return NULL;

+

+    CuePoint* const pNext = pp[index];

+    assert(pNext);

+

+    pNext->Load(m_pSegment->m_pReader);

+

+    return pNext;

+}

+

+

+const BlockEntry* Cues::GetBlock(

+    const CuePoint* pCP,

+    const CuePoint::TrackPosition* pTP) const

+{

+    if (pCP == NULL)

+        return NULL;

+

+    if (pTP == NULL)

+        return NULL;

+

+    return m_pSegment->GetBlock(*pCP, *pTP);

+}

+

+

+const BlockEntry* Segment::GetBlock(

+    const CuePoint& cp,

+    const CuePoint::TrackPosition& tp)

+{

+    Cluster** const ii = m_clusters;

+    Cluster** i = ii;

+

+    const long count = m_clusterCount + m_clusterPreloadCount;

+

+    Cluster** const jj = ii + count;

+    Cluster** j = jj;

+

+    while (i < j)

+    {

+        //INVARIANT:

+        //[ii, i) < pTP->m_pos

+        //[i, j) ?

+        //[j, jj)  > pTP->m_pos

+

+        Cluster** const k = i + (j - i) / 2;

+        assert(k < jj);

+

+        Cluster* const pCluster = *k;

+        assert(pCluster);

+

+        const long long pos_ = pCluster->m_pos;

+        assert(pos_);

+

+        const long long pos = pos_ * ((pos_ < 0) ? -1 : 1);

+

+        if (pos < tp.m_pos)

+            i = k + 1;

+        else if (pos > tp.m_pos)

+            j = k;

+        else

+            return pCluster->GetEntry(cp, tp);

+    }

+

+    assert(i == j);

+

+    Cluster* const pCluster = Cluster::Parse(this, -1, tp.m_pos);

+    const ptrdiff_t idx = i - m_clusters;

+

+    PreloadCluster(pCluster, idx);

+    assert(m_clusters);

+    assert(m_clusterPreloadCount > 0);

+    assert(m_clusters[idx] == pCluster);

+

+    return pCluster->GetEntry(cp, tp);

+}

+

+

+

+CuePoint::CuePoint(size_t idx, long long pos) :

+    m_index(idx),

+    m_timecode(-1 * pos),

+    m_track_positions(NULL),

+    m_track_positions_count(0)

+{

+    assert(pos > 0);

+}

+

+

+CuePoint::~CuePoint()

+{

+    delete[] m_track_positions;

+}

+

+

+void CuePoint::Load(IMkvReader* pReader)

+{

+    //odbgstream os;

+    //os << "CuePoint::Load(begin): timecode=" << m_timecode << endl;

+

+    if (m_timecode >= 0)  //already loaded

+        return;

+

+    assert(m_track_positions == NULL);

+    assert(m_track_positions_count == 0);

+

+    long long pos_ = -m_timecode;

+

+    long long stop;

+

+    {

+        long len;

+

+        const long long id = ReadUInt(pReader, pos_, len);

+        assert(id == 0x3B);  //CuePoint ID

+        //assert((pos + len) <= stop);

+

+        pos_ += len;  //consume ID

+

+        const long long size = ReadUInt(pReader, pos_, len);

+        assert(size >= 0);

+        //assert((pos + len) <= stop);

+

+        pos_ += len;  //consume Size field

+        //assert((pos + size) <= stop);

+

+        //pos_ now points to start of payload

+

+        stop = pos_ + size;

+    }

+

+    long long pos = pos_;

+

+    //First count number of track positions

+

+    while (pos < stop)

+    {

+        long len;

+

+        const long long id = ReadUInt(pReader, pos, len);

+        assert(id >= 0);  //TODO

+        assert((pos + len) <= stop);

+

+        pos += len;  //consume ID

+

+        const long long size = ReadUInt(pReader, pos, len);

+        assert(size >= 0);

+        assert((pos + len) <= stop);

+

+        pos += len;  //consume Size field

+        assert((pos + size) <= stop);

+

+        if (id == 0x33)  //CueTime ID

+            m_timecode = UnserializeUInt(pReader, pos, size);

+

+        else if (id == 0x37) //CueTrackPosition(s) ID

+            ++m_track_positions_count;

+

+        pos += size;  //consume payload

+        assert(pos <= stop);

+    }

+

+    assert(m_timecode >= 0);

+    assert(m_track_positions_count > 0);

+

+    //os << "CuePoint::Load(cont'd): idpos=" << idpos

+    //   << " timecode=" << m_timecode

+    //   << endl;

+

+    m_track_positions = new TrackPosition[m_track_positions_count];

+

+    //Now parse track positions

+

+    TrackPosition* p = m_track_positions;

+    pos = pos_;

+

+    while (pos < stop)

+    {

+        long len;

+

+        const long long id = ReadUInt(pReader, pos, len);

+        assert(id >= 0);  //TODO

+        assert((pos + len) <= stop);

+

+        pos += len;  //consume ID

+

+        const long long size = ReadUInt(pReader, pos, len);

+        assert(size >= 0);

+        assert((pos + len) <= stop);

+

+        pos += len;  //consume Size field

+        assert((pos + size) <= stop);

+

+        if (id == 0x37) //CueTrackPosition(s) ID

+        {

+            TrackPosition& tp = *p++;

+            tp.Parse(pReader, pos, size);

+        }

+

+        pos += size;  //consume payload

+        assert(pos <= stop);

+    }

+

+    assert(size_t(p - m_track_positions) == m_track_positions_count);

+}

+

+

+

+void CuePoint::TrackPosition::Parse(

+    IMkvReader* pReader,

+    long long start_,

+    long long size_)

+{

+    const long long stop = start_ + size_;

+    long long pos = start_;

+

+    m_track = -1;

+    m_pos = -1;

+    m_block = 1;  //default

+

+    while (pos < stop)

+    {

+        long len;

+

+        const long long id = ReadUInt(pReader, pos, len);

+        assert(id >= 0);  //TODO

+        assert((pos + len) <= stop);

+

+        pos += len;  //consume ID

+

+        const long long size = ReadUInt(pReader, pos, len);

+        assert(size >= 0);

+        assert((pos + len) <= stop);

+

+        pos += len;  //consume Size field

+        assert((pos + size) <= stop);

+

+        if (id == 0x77)  //CueTrack ID

+            m_track = UnserializeUInt(pReader, pos, size);

+

+        else if (id == 0x71)  //CueClusterPos ID

+            m_pos = UnserializeUInt(pReader, pos, size);

+

+        else if (id == 0x1378)  //CueBlockNumber

+            m_block = UnserializeUInt(pReader, pos, size);

+

+        pos += size;  //consume payload

+        assert(pos <= stop);

+    }

+

+    assert(m_pos >= 0);

+    //assert(m_track > 0);

+    //assert(m_block > 0);

+}

+

+

+const CuePoint::TrackPosition* CuePoint::Find(const Track* pTrack) const

+{

+    assert(pTrack);

+

+    const long long n = pTrack->GetNumber();

+

+    const TrackPosition* i = m_track_positions;

+    const TrackPosition* const j = i + m_track_positions_count;

+

+    while (i != j)

+    {

+        const TrackPosition& p = *i++;

+

+        if (p.m_track == n)

+            return &p;

+    }

+

+    return NULL;  //no matching track number found

+}

+

+

+long long CuePoint::GetTimeCode() const

+{

+    return m_timecode;

+}

+

+long long CuePoint::GetTime(Segment* pSegment) const

+{

+    assert(pSegment);

+    assert(m_timecode >= 0);

+

+    const SegmentInfo* const pInfo = pSegment->GetInfo();

+    assert(pInfo);

+

+    const long long scale = pInfo->GetTimeCodeScale();

+    assert(scale >= 1);

+

+    const long long time = scale * m_timecode;

+

+    return time;

+}

+

+

+long long Segment::Unparsed() const

+{

+    const long long stop = m_start + m_size;

+

+    const long long result = stop - m_pos;

+    assert(result >= 0);

+

+    return result;

+}

+

+

+Cluster* Segment::GetFirst()

+{

+    if ((m_clusters == NULL) || (m_clusterCount <= 0))

+       return &m_eos;

+

+    Cluster* const pCluster = m_clusters[0];

+    assert(pCluster);

+

+    return pCluster;

+}

+

+

+Cluster* Segment::GetLast()

+{

+    if ((m_clusters == NULL) || (m_clusterCount <= 0))

+        return &m_eos;

+

+    const long idx = m_clusterCount - 1;

+

+    Cluster* const pCluster = m_clusters[idx];

+    assert(pCluster);

+

+    return pCluster;

+}

+

+

+unsigned long Segment::GetCount() const

+{

+    return m_clusterCount;

+}

+

+

+Cluster* Segment::GetNext(const Cluster* pCurr)

+{

+    assert(pCurr);

+    assert(pCurr != &m_eos);

+    assert(m_clusters);

+

+    long idx =  pCurr->m_index;

+

+    if (idx >= 0)

+    {

+        assert(m_clusterCount > 0);

+        assert(idx < m_clusterCount);

+        assert(pCurr == m_clusters[idx]);

+

+        ++idx;

+

+        if (idx >= m_clusterCount)

+            return &m_eos;  //caller will LoadCluster as desired

+

+        Cluster* const pNext = m_clusters[idx];

+        assert(pNext);

+        assert(pNext->m_index >= 0);

+        assert(pNext->m_index == idx);

+

+        return pNext;

+    }

+

+    assert(m_clusterPreloadCount > 0);

+

+    const long long off_ = pCurr->m_pos;

+    const long long off = off_ * ((off_ < 0) ? -1 : 1);

+

+    long long pos = m_start + off;

+    const long long stop = m_start + m_size;  //end of segment

+

+    {

+        long len;

+

+        long long result = GetUIntLength(m_pReader, pos, len);

+        assert(result == 0);  //TODO

+        assert((pos + len) <= stop);  //TODO

+

+        const long long id = ReadUInt(m_pReader, pos, len);

+        assert(id == 0x0F43B675);  //Cluster ID   //TODO

+

+        pos += len;  //consume ID

+

+        //Read Size

+        result = GetUIntLength(m_pReader, pos, len);

+        assert(result == 0);  //TODO

+        assert((pos + len) <= stop);  //TODO

+

+        const long long size = ReadUInt(m_pReader, pos, len);

+        assert(size > 0);  //TODO

+        assert((pCurr->m_size <= 0) || (pCurr->m_size == size));

+

+        pos += len;  //consume length of size of element

+        assert((pos + size) <= stop);  //TODO

+

+        //Pos now points to start of payload

+

+        pos += size;  //consume payload

+    }

+

+    long long off_next = 0;

+

+    while (pos < stop)

+    {

+        long len;

+

+        long long result = GetUIntLength(m_pReader, pos, len);

+        assert(result == 0);  //TODO

+        assert((pos + len) <= stop);  //TODO

+

+        const long long idpos = pos;  //pos of next (potential) cluster

+

+        const long long id = ReadUInt(m_pReader, idpos, len);

+        assert(id > 0);  //TODO

+

+        pos += len;  //consume ID

+

+        //Read Size

+        result = GetUIntLength(m_pReader, pos, len);

+        assert(result == 0);  //TODO

+        assert((pos + len) <= stop);  //TODO

+

+        const long long size = ReadUInt(m_pReader, pos, len);

+        assert(size >= 0);  //TODO

+

+        pos += len;  //consume length of size of element

+        assert((pos + size) <= stop);  //TODO

+

+        //Pos now points to start of payload

+

+        if (size == 0)  //weird

+            continue;

+

+        if (id == 0x0F43B675)  //Cluster ID

+        {

+            off_next = idpos - m_start;

+            break;

+        }

+

+        pos += size;  //consume payload

+    }

+

+    if (off_next <= 0)

+        return 0;

+

+    Cluster** const ii = m_clusters + m_clusterCount;

+    Cluster** i = ii;

+

+    Cluster** const jj = ii + m_clusterPreloadCount;

+    Cluster** j = jj;

+

+    while (i < j)

+    {

+        //INVARIANT:

+        //[0, i) < pos_next

+        //[i, j) ?

+        //[j, jj)  > pos_next

+

+        Cluster** const k = i + (j - i) / 2;

+        assert(k < jj);

+

+        Cluster* const pNext = *k;

+        assert(pNext);

+        assert(pNext->m_index < 0);

+

+        const long long pos_ = pNext->m_pos;

+        assert(pos_);

+

+        pos = pos_ * ((pos_ < 0) ? -1 : 1);

+

+        if (pos < off_next)

+            i = k + 1;

+        else if (pos > off_next)

+            j = k;

+        else

+            return pNext;

+    }

+

+    assert(i == j);

+

+    Cluster* const pNext = Cluster::Parse(this, -1, off_next);

+    const ptrdiff_t idx_next = i - m_clusters;  //insertion position

+

+    PreloadCluster(pNext, idx_next);

+    assert(m_clusters);

+    assert(idx_next < m_clusterSize);

+    assert(m_clusters[idx_next] == pNext);

+

+    return pNext;

+}

+

+

+Cluster* Segment::FindCluster(long long time_ns)

+{

+    if ((m_clusters == NULL) || (m_clusterCount <= 0))

+        return &m_eos;

+

+    {

+        Cluster* const pCluster = m_clusters[0];

+        assert(pCluster);

+        assert(pCluster->m_index == 0);

+

+        if (time_ns <= pCluster->GetTime())

+            return pCluster;

+    }

+

+    //Binary search of cluster array

+

+    long i = 0;

+    long j = m_clusterCount;

+

+    while (i < j)

+    {

+        //INVARIANT:

+        //[0, i) <= time_ns

+        //[i, j) ?

+        //[j, m_clusterCount)  > time_ns

+

+        const long k = i + (j - i) / 2;

+        assert(k < m_clusterCount);

+

+        Cluster* const pCluster = m_clusters[k];

+        assert(pCluster);

+        assert(pCluster->m_index == k);

+

+        const long long t = pCluster->GetTime();

+

+        if (t <= time_ns)

+            i = k + 1;

+        else

+            j = k;

+

+        assert(i <= j);

+    }

+

+    assert(i == j);

+    assert(i > 0);

+    assert(i <= m_clusterCount);

+

+    const long k = i - 1;

+

+    Cluster* const pCluster = m_clusters[k];

+    assert(pCluster);

+    assert(pCluster->m_index == k);

+    assert(pCluster->GetTime() <= time_ns);

+

+    return pCluster;

+}

+

+

+const BlockEntry* Segment::Seek(

+    long long time_ns,

+    const Track* pTrack)

+{

+    assert(pTrack);

+

+    if ((m_clusters == NULL) || (m_clusterCount <= 0))

+        return pTrack->GetEOS();

+

+    Cluster** const i = m_clusters;

+    assert(i);

+

+    {

+        Cluster* const pCluster = *i;

+        assert(pCluster);

+        assert(pCluster->m_index == 0);  //m_clusterCount > 0

+        assert(pCluster->m_pSegment == this);

+

+        if (time_ns <= pCluster->GetTime())

+            return pCluster->GetEntry(pTrack);

+    }

+

+    Cluster** const j = i + m_clusterCount;

+

+    if (pTrack->GetType() == 2)  //audio

+    {

+        //TODO: we could decide to use cues for this, as we do for video.

+        //But we only use it for video because looking around for a keyframe

+        //can get expensive.  Audio doesn't require anything special so a

+        //straight cluster search is good enough (we assume).

+

+        Cluster** lo = i;

+        Cluster** hi = j;

+

+        while (lo < hi)

+        {

+            //INVARIANT:

+            //[i, lo) <= time_ns

+            //[lo, hi) ?

+            //[hi, j)  > time_ns

+

+            Cluster** const mid = lo + (hi - lo) / 2;

+            assert(mid < hi);

+

+            Cluster* const pCluster = *mid;

+            assert(pCluster);

+            assert(pCluster->m_index == long(mid - m_clusters));

+            assert(pCluster->m_pSegment == this);

+

+            const long long t = pCluster->GetTime();

+

+            if (t <= time_ns)

+                lo = mid + 1;

+            else

+                hi = mid;

+

+            assert(lo <= hi);

+        }

+

+        assert(lo == hi);

+        assert(lo > i);

+        assert(lo <= j);

+

+        Cluster* const pCluster = *--lo;

+        assert(pCluster);

+        assert(pCluster->GetTime() <= time_ns);

+

+        return pCluster->GetEntry(pTrack);

+    }

+

+    assert(pTrack->GetType() == 1);  //video

+

+    Cluster** lo = i;

+    Cluster** hi = j;

+

+    while (lo < hi)

+    {

+        //INVARIANT:

+        //[i, lo) <= time_ns

+        //[lo, hi) ?

+        //[hi, j)  > time_ns

+

+        Cluster** const mid = lo + (hi - lo) / 2;

+        assert(mid < hi);

+

+        Cluster* const pCluster = *mid;

+        assert(pCluster);

+

+        const long long t = pCluster->GetTime();

+

+        if (t <= time_ns)

+            lo = mid + 1;

+        else

+            hi = mid;

+

+        assert(lo <= hi);

+    }

+

+    assert(lo == hi);

+    assert(lo > i);

+    assert(lo <= j);

+

+    Cluster* pCluster = *--lo;

+    assert(pCluster);

+    assert(pCluster->GetTime() <= time_ns);

+

+    {

+        const BlockEntry* const pBlockEntry = pCluster->GetEntry(pTrack);

+        assert(pBlockEntry);

+

+        if (!pBlockEntry->EOS())  //found a keyframe

+        {

+            const Block* const pBlock = pBlockEntry->GetBlock();

+            assert(pBlock);

+

+            //TODO: this isn't necessarily the keyframe we want,

+            //since there might another keyframe on this same

+            //cluster with a greater timecode that but that is

+            //still less than the requested time.  For now we

+            //simply return the first keyframe we find.

+

+            if (pBlock->GetTime(pCluster) <= time_ns)

+                return pBlockEntry;

+        }

+    }

+

+    const VideoTrack* const pVideo = static_cast<const VideoTrack*>(pTrack);

+

+    while (lo != i)

+    {

+        pCluster = *--lo;

+        assert(pCluster);

+        assert(pCluster->GetTime() <= time_ns);

+

+        const BlockEntry* const pBlockEntry = pCluster->GetMaxKey(pVideo);

+        assert(pBlockEntry);

+

+        if (!pBlockEntry->EOS())

+            return pBlockEntry;

+    }

+

+    //weird: we're on the first cluster, but no keyframe found

+    //should never happen but we must return something anyway

+

+    return pTrack->GetEOS();

+}

+

+

+#if 0

+bool Segment::SearchCues(

+    long long time_ns,

+    Track* pTrack,

+    Cluster*& pCluster,

+    const BlockEntry*& pBlockEntry,

+    const CuePoint*& pCP,

+    const CuePoint::TrackPosition*& pTP)

+{

+    if (pTrack->GetType() != 1)  //not video

+        return false;  //TODO: for now, just handle video stream

+

+    if (m_pCues == NULL)

+        return false;

+

+    if (!m_pCues->Find(time_ns, pTrack, pCP, pTP))

+        return false;  //weird

+

+    assert(pCP);

+    assert(pTP);

+    assert(pTP->m_track == pTrack->GetNumber());

+

+    //We have the cue point and track position we want,

+    //so we now need to search for the cluster having

+    //the indicated position.

+

+    return GetCluster(pCP, pTP, pCluster, pBlockEntry);

+}

+#endif

+

+

+Tracks* Segment::GetTracks() const

+{

+    return m_pTracks;

+}

+

+

+const SegmentInfo* Segment::GetInfo() const

+{

+    return m_pInfo;

+}

+

+

+const Cues* Segment::GetCues() const

+{

+    return m_pCues;

+}

+

+

+long long Segment::GetDuration() const

+{

+    assert(m_pInfo);

+    return m_pInfo->GetDuration();

+}

+

+

+SegmentInfo::SegmentInfo(Segment* pSegment, long long start, long long size_) :

+    m_pSegment(pSegment),

+    m_start(start),

+    m_size(size_),

+    m_pMuxingAppAsUTF8(NULL),

+    m_pWritingAppAsUTF8(NULL),

+    m_pTitleAsUTF8(NULL)

+{

+    IMkvReader* const pReader = m_pSegment->m_pReader;

+

+    long long pos = start;

+    const long long stop = start + size_;

+

+    m_timecodeScale = 1000000;

+    m_duration = -1;

+

+    while (pos < stop)

+    {

+        if (Match(pReader, pos, 0x0AD7B1, m_timecodeScale))

+            assert(m_timecodeScale > 0);

+

+        else if (Match(pReader, pos, 0x0489, m_duration))

+            assert(m_duration >= 0);

+

+        else if (Match(pReader, pos, 0x0D80, m_pMuxingAppAsUTF8))   //[4D][80]

+            assert(m_pMuxingAppAsUTF8);

+

+        else if (Match(pReader, pos, 0x1741, m_pWritingAppAsUTF8))  //[57][41]

+            assert(m_pWritingAppAsUTF8);

+

+        else if (Match(pReader, pos, 0x3BA9, m_pTitleAsUTF8))       //[7B][A9]

+            assert(m_pTitleAsUTF8);

+

+        else

+        {

+            long len;

+

+            const long long id = ReadUInt(pReader, pos, len);

+            //id;

+            assert(id >= 0);

+            assert((pos + len) <= stop);

+

+            pos += len;  //consume id

+            assert((stop - pos) > 0);

+

+            const long long size = ReadUInt(pReader, pos, len);

+            assert(size >= 0);

+            assert((pos + len) <= stop);

+

+            pos += len + size;  //consume size and payload

+            assert(pos <= stop);

+        }

+    }

+

+    assert(pos == stop);

+}

+

+SegmentInfo::~SegmentInfo()

+{

+    if (m_pMuxingAppAsUTF8)

+    {

+        delete[] m_pMuxingAppAsUTF8;

+        m_pMuxingAppAsUTF8 = NULL;

+    }

+

+    if (m_pWritingAppAsUTF8)

+    {

+        delete[] m_pWritingAppAsUTF8;

+        m_pWritingAppAsUTF8 = NULL;

+    }

+

+    if (m_pTitleAsUTF8)

+    {

+        delete[] m_pTitleAsUTF8;

+        m_pTitleAsUTF8 = NULL;

+    }

+}

+

+long long SegmentInfo::GetTimeCodeScale() const

+{

+    return m_timecodeScale;

+}

+

+

+long long SegmentInfo::GetDuration() const

+{

+    if (m_duration < 0)

+        return -1;

+

+    assert(m_timecodeScale >= 1);

+

+    const double dd = double(m_duration) * double(m_timecodeScale);

+    const long long d = static_cast<long long>(dd);

+

+    return d;

+}

+

+const char* SegmentInfo::GetMuxingAppAsUTF8() const

+{

+    return m_pMuxingAppAsUTF8;

+}

+

+

+const char* SegmentInfo::GetWritingAppAsUTF8() const

+{

+    return m_pWritingAppAsUTF8;

+}

+

+const char* SegmentInfo::GetTitleAsUTF8() const

+{

+    return m_pTitleAsUTF8;

+}

+

+Track::Track(Segment* pSegment, const Info& i) :

+    m_pSegment(pSegment),

+    m_info(i)

+{

+}

+

+Track::~Track()

+{

+    Info& info = const_cast<Info&>(m_info);

+    info.Clear();

+}

+

+Track::Info::Info():

+    type(-1),

+    number(-1),

+    uid(-1),

+    nameAsUTF8(NULL),

+    codecId(NULL),

+    codecPrivate(NULL),

+    codecPrivateSize(0),

+    codecNameAsUTF8(NULL)

+{

+}

+

+

+void Track::Info::Clear()

+{

+    delete[] nameAsUTF8;

+    nameAsUTF8 = NULL;

+

+    delete[] codecId;

+    codecId = NULL;

+

+    delete[] codecPrivate;

+    codecPrivate = NULL;

+

+    codecPrivateSize = 0;

+

+    delete[] codecNameAsUTF8;

+    codecNameAsUTF8 = NULL;

+}

+

+const BlockEntry* Track::GetEOS() const

+{

+    return &m_eos;

+}

+

+long long Track::GetType() const

+{

+    return m_info.type;

+}

+

+long long Track::GetNumber() const

+{

+    return m_info.number;

+}

+

+const char* Track::GetNameAsUTF8() const

+{

+    return m_info.nameAsUTF8;

+}

+

+const char* Track::GetCodecNameAsUTF8() const

+{

+    return m_info.codecNameAsUTF8;

+}

+

+

+const char* Track::GetCodecId() const

+{

+    return m_info.codecId;

+}

+

+const unsigned char* Track::GetCodecPrivate(size_t& size) const

+{

+    size = m_info.codecPrivateSize;

+    return m_info.codecPrivate;

+}

+

+

+long Track::GetFirst(const BlockEntry*& pBlockEntry) const

+{

+    Cluster* pCluster = m_pSegment->GetFirst();

+

+    //If Segment::GetFirst returns NULL, then this must be a network

+    //download, and we haven't loaded any clusters yet.  In this case,

+    //returning NULL from Track::GetFirst means the same thing.

+

+    for (int i = 0; i < 100; ++i)  //arbitrary upper bound

+    {

+        if (pCluster == NULL)

+        {

+            pBlockEntry = GetEOS();

+            return 1;

+        }

+

+        if (pCluster->EOS())

+        {

+            if (m_pSegment->Unparsed() <= 0)  //all clusters have been loaded

+            {

+                pBlockEntry = GetEOS();

+                return 1;

+            }

+

+            pBlockEntry = 0;

+            return E_BUFFER_NOT_FULL;

+        }

+

+        pBlockEntry = pCluster->GetFirst();

+

+        while (pBlockEntry)

+        {

+            const Block* const pBlock = pBlockEntry->GetBlock();

+            assert(pBlock);

+

+            if (pBlock->GetTrackNumber() == m_info.number)

+                return 0;

+

+            pBlockEntry = pCluster->GetNext(pBlockEntry);

+        }

+

+        pCluster = m_pSegment->GetNext(pCluster);

+    }

+

+    //NOTE: if we get here, it means that we didn't find a block with

+    //a matching track number.  We interpret that as an error (which

+    //might be too conservative).

+

+    pBlockEntry = GetEOS();  //so we can return a non-NULL value

+    return 1;

+}

+

+

+long Track::GetNext(

+    const BlockEntry* pCurrEntry,

+    const BlockEntry*& pNextEntry) const

+{

+    assert(pCurrEntry);

+    assert(!pCurrEntry->EOS());  //?

+

+    const Block* const pCurrBlock = pCurrEntry->GetBlock();

+    assert(pCurrBlock->GetTrackNumber() == m_info.number);

+

+    Cluster* pCluster = pCurrEntry->GetCluster();

+    assert(pCluster);

+    assert(!pCluster->EOS());

+

+    pNextEntry = pCluster->GetNext(pCurrEntry);

+

+    for (int i = 0; i < 100; ++i)  //arbitrary upper bound to search

+    {

+        while (pNextEntry)

+        {

+            const Block* const pNextBlock = pNextEntry->GetBlock();

+            assert(pNextBlock);

+

+            if (pNextBlock->GetTrackNumber() == m_info.number)

+                return 0;

+

+            pNextEntry = pCluster->GetNext(pNextEntry);

+        }

+

+        pCluster = m_pSegment->GetNext(pCluster);

+

+        if (pCluster == NULL)

+        {

+            pNextEntry = GetEOS();

+            return 1;

+        }

+

+        if (pCluster->EOS())

+        {

+            if (m_pSegment->Unparsed() <= 0)   //all clusters have been loaded

+            {

+                pNextEntry = GetEOS();

+                return 1;

+            }

+

+            //TODO: there is a potential O(n^2) problem here: we tell the

+            //caller to (pre)load another cluster, which he does, but then he

+            //calls GetNext again, which repeats the same search.  This is

+            //a pathological case, since the only way it can happen is if

+            //there exists a long sequence of clusters none of which contain a

+            // block from this track.  One way around this problem is for the

+            //caller to be smarter when he loads another cluster: don't call

+            //us back until you have a cluster that contains a block from this

+            //track. (Of course, that's not cheap either, since our caller

+            //would have to scan the each cluster as it's loaded, so that

+            //would just push back the problem.)

+

+            pNextEntry = NULL;

+            return E_BUFFER_NOT_FULL;

+        }

+

+        pNextEntry = pCluster->GetFirst();

+    }

+

+    //NOTE: if we get here, it means that we didn't find a block with

+    //a matching track number after lots of searching, so we give

+    //up trying.

+

+    pNextEntry = GetEOS();  //so we can return a non-NULL value

+    return 1;

+}

+

+

+Track::EOSBlock::EOSBlock()

+{

+}

+

+

+bool Track::EOSBlock::EOS() const

+{

+    return true;

+}

+

+

+Cluster* Track::EOSBlock::GetCluster() const

+{

+    return NULL;

+}

+

+

+size_t Track::EOSBlock::GetIndex() const

+{

+    return 0;

+}

+

+

+const Block* Track::EOSBlock::GetBlock() const

+{

+    return NULL;

+}

+

+

+bool Track::EOSBlock::IsBFrame() const

+{

+    return false;

+}

+

+

+VideoTrack::VideoTrack(Segment* pSegment, const Info& i) :

+    Track(pSegment, i),

+    m_width(-1),

+    m_height(-1),

+    m_rate(-1)

+{

+    assert(i.type == 1);

+    assert(i.number > 0);

+

+    IMkvReader* const pReader = pSegment->m_pReader;

+

+    const Settings& s = i.settings;

+    assert(s.start >= 0);

+    assert(s.size >= 0);

+

+    long long pos = s.start;

+    assert(pos >= 0);

+

+    const long long stop = pos + s.size;

+

+    while (pos < stop)

+    {

+#ifdef _DEBUG

+        long len;

+        const long long id = ReadUInt(pReader, pos, len);

+        assert(id >= 0);  //TODO: handle error case

+        assert((pos + len) <= stop);

+#endif

+        if (Match(pReader, pos, 0x30, m_width))

+            ;

+        else if (Match(pReader, pos, 0x3A, m_height))

+            ;

+        else if (Match(pReader, pos, 0x0383E3, m_rate))

+            ;

+        else

+        {

+            long len;

+            const long long id = ReadUInt(pReader, pos, len);

+            assert(id >= 0);  //TODO: handle error case

+            assert((pos + len) <= stop);

+

+            pos += len;  //consume id

+

+            const long long size = ReadUInt(pReader, pos, len);

+            assert(size >= 0);  //TODO: handle error case

+            assert((pos + len) <= stop);

+

+            pos += len;  //consume length of size

+            assert((pos + size) <= stop);

+

+            //pos now designates start of payload

+

+            pos += size;  //consume payload

+            assert(pos <= stop);

+        }

+    }

+

+    return;

+}

+

+

+bool VideoTrack::VetEntry(const BlockEntry* pBlockEntry) const

+{

+    assert(pBlockEntry);

+

+    const Block* const pBlock = pBlockEntry->GetBlock();

+    assert(pBlock);

+    assert(pBlock->GetTrackNumber() == m_info.number);

+

+    return pBlock->IsKey();

+}

+

+

+long long VideoTrack::GetWidth() const

+{

+    return m_width;

+}

+

+

+long long VideoTrack::GetHeight() const

+{

+    return m_height;

+}

+

+

+double VideoTrack::GetFrameRate() const

+{

+    return m_rate;

+}

+

+

+AudioTrack::AudioTrack(Segment* pSegment, const Info& i) :

+    Track(pSegment, i),

+    m_rate(0.0),

+    m_channels(0),

+    m_bitDepth(-1)

+{

+    assert(i.type == 2);

+    assert(i.number > 0);

+

+    IMkvReader* const pReader = pSegment->m_pReader;

+

+    const Settings& s = i.settings;

+    assert(s.start >= 0);

+    assert(s.size >= 0);

+

+    long long pos = s.start;

+    assert(pos >= 0);

+

+    const long long stop = pos + s.size;

+

+    while (pos < stop)

+    {

+#ifdef _DEBUG

+        long len;

+        const long long id = ReadUInt(pReader, pos, len);

+        assert(id >= 0);  //TODO: handle error case

+        assert((pos + len) <= stop);

+#endif

+        if (Match(pReader, pos, 0x35, m_rate))

+            ;

+        else if (Match(pReader, pos, 0x1F, m_channels))

+            ;

+        else if (Match(pReader, pos, 0x2264, m_bitDepth))

+            ;

+        else

+        {

+            long len;

+            const long long id = ReadUInt(pReader, pos, len);

+            assert(id >= 0);  //TODO: handle error case

+            assert((pos + len) <= stop);

+

+            pos += len;  //consume id

+

+            const long long size = ReadUInt(pReader, pos, len);

+            assert(size >= 0);  //TODO: handle error case

+            assert((pos + len) <= stop);

+

+            pos += len;  //consume length of size

+            assert((pos + size) <= stop);

+

+            //pos now designates start of payload

+

+            pos += size;  //consume payload

+            assert(pos <= stop);

+        }

+    }

+

+    return;

+}

+

+

+bool AudioTrack::VetEntry(const BlockEntry* pBlockEntry) const

+{

+    assert(pBlockEntry);

+

+    const Block* const pBlock = pBlockEntry->GetBlock();

+    assert(pBlock);

+    assert(pBlock->GetTrackNumber() == m_info.number);

+

+    return true;

+}

+

+

+double AudioTrack::GetSamplingRate() const

+{

+    return m_rate;

+}

+

+

+long long AudioTrack::GetChannels() const

+{

+    return m_channels;

+}

+

+long long AudioTrack::GetBitDepth() const

+{

+    return m_bitDepth;

+}

+

+Tracks::Tracks(Segment* pSegment, long long start, long long size_) :

+    m_pSegment(pSegment),

+    m_start(start),

+    m_size(size_),

+    m_trackEntries(NULL),

+    m_trackEntriesEnd(NULL)

+{

+    long long stop = m_start + m_size;

+    IMkvReader* const pReader = m_pSegment->m_pReader;

+

+    long long pos1 = m_start;

+    int count = 0;

+

+    while (pos1 < stop)

+    {

+        long len;

+        const long long id = ReadUInt(pReader, pos1, len);

+        assert(id >= 0);

+        assert((pos1 + len) <= stop);

+

+        pos1 += len;  //consume id

+

+        const long long size = ReadUInt(pReader, pos1, len);

+        assert(size >= 0);

+        assert((pos1 + len) <= stop);

+

+        pos1 += len;  //consume length of size

+

+        //pos now desinates start of element

+        if (id == 0x2E)  //TrackEntry ID

+            ++count;

+

+        pos1 += size;  //consume payload

+        assert(pos1 <= stop);

+    }

+

+    if (count <= 0)

+        return;

+

+    m_trackEntries = new Track*[count];

+    m_trackEntriesEnd = m_trackEntries;

+

+    long long pos = m_start;

+

+    while (pos < stop)

+    {

+        long len;

+        const long long id = ReadUInt(pReader, pos, len);

+        assert(id >= 0);

+        assert((pos + len) <= stop);

+

+        pos += len;  //consume id

+

+        const long long size1 = ReadUInt(pReader, pos, len);

+        assert(size1 >= 0);

+        assert((pos + len) <= stop);

+

+        pos += len;  //consume length of size

+

+        //pos now desinates start of element

+

+        if (id == 0x2E)  //TrackEntry ID

+            ParseTrackEntry(pos, size1, *m_trackEntriesEnd++);

+

+        pos += size1;  //consume payload

+        assert(pos <= stop);

+    }

+}

+

+

+unsigned long Tracks::GetTracksCount() const

+{

+    const ptrdiff_t result = m_trackEntriesEnd - m_trackEntries;

+    assert(result >= 0);

+

+    return static_cast<unsigned long>(result);

+}

+

+

+void Tracks::ParseTrackEntry(

+    long long start,

+    long long size,

+    Track*& pTrack)

+{

+    IMkvReader* const pReader = m_pSegment->m_pReader;

+

+    long long pos = start;

+    const long long stop = start + size;

+

+    Track::Info i;

+

+    Track::Settings videoSettings;

+    videoSettings.start = -1;

+

+    Track::Settings audioSettings;

+    audioSettings.start = -1;

+

+    while (pos < stop)

+    {

+#ifdef _DEBUG

+        long len;

+        const long long id = ReadUInt(pReader, pos, len);

+        len;

+        id;

+#endif

+        if (Match(pReader, pos, 0x57, i.number))

+            assert(i.number > 0);

+        else if (Match(pReader, pos, 0x33C5, i.uid))

+            ;

+        else if (Match(pReader, pos, 0x03, i.type))

+            ;

+        else if (Match(pReader, pos, 0x136E, i.nameAsUTF8))

+            assert(i.nameAsUTF8);

+        else if (Match(pReader, pos, 0x06, i.codecId))

+            ;

+        else if (Match(pReader,

+                       pos,

+                       0x23A2,

+                       i.codecPrivate,

+                       i.codecPrivateSize))

+            ;

+        else if (Match(pReader, pos, 0x058688, i.codecNameAsUTF8))

+            assert(i.codecNameAsUTF8);

+        else

+        {

+            long len;

+

+            const long long id = ReadUInt(pReader, pos, len);

+            assert(id >= 0);  //TODO: handle error case

+            assert((pos + len) <= stop);

+

+            pos += len;  //consume id

+

+            const long long size = ReadUInt(pReader, pos, len);

+            assert(size >= 0);  //TODO: handle error case

+            assert((pos + len) <= stop);

+

+            pos += len;  //consume length of size

+            const long long start = pos;

+

+            pos += size;  //consume payload

+            assert(pos <= stop);

+

+            if (id == 0x60)

+            {

+                videoSettings.start = start;

+                videoSettings.size = size;

+            }

+            else if (id == 0x61)

+            {

+                audioSettings.start = start;

+                audioSettings.size = size;

+            }

+        }

+    }

+

+    assert(pos == stop);

+    //TODO: propertly vet info.number, to ensure both its existence,

+    //and that it is unique among all tracks.

+    assert(i.number > 0);

+

+    //TODO: vet settings, to ensure that video settings (0x60)

+    //were specified when type = 1, and that audio settings (0x61)

+    //were specified when type = 2.

+    if (i.type == 1)  //video

+    {

+        assert(audioSettings.start < 0);

+        assert(videoSettings.start >= 0);

+

+        i.settings = videoSettings;

+

+        VideoTrack* const t = new VideoTrack(m_pSegment, i);

+        assert(t);  //TODO

+        pTrack = t;

+    }

+    else if (i.type == 2)  //audio

+    {

+        assert(videoSettings.start < 0);

+        assert(audioSettings.start >= 0);

+

+        i.settings = audioSettings;

+

+        AudioTrack* const t = new  AudioTrack(m_pSegment, i);

+        assert(t);  //TODO

+        pTrack = t;

+    }

+    else

+    {

+        // for now we do not support other track types yet.

+        // TODO: support other track types

+        i.Clear();

+

+        pTrack = NULL;

+    }

+

+    return;

+}

+

+

+Tracks::~Tracks()

+{

+    Track** i = m_trackEntries;

+    Track** const j = m_trackEntriesEnd;

+

+    while (i != j)

+    {

+        Track* const pTrack = *i++;

+        delete pTrack;

+    }

+

+    delete[] m_trackEntries;

+}

+

+

+Track* Tracks::GetTrackByNumber(unsigned long tn_) const

+{

+    const long long tn = tn_;

+

+    Track** i = m_trackEntries;

+    Track** const j = m_trackEntriesEnd;

+

+    while (i != j)

+    {

+        Track* const pTrack = *i++;

+

+        if (pTrack == NULL)

+            continue;

+

+        if (tn == pTrack->GetNumber())

+            return pTrack;

+    }

+

+    return NULL;  //not found

+}

+

+

+Track* Tracks::GetTrackByIndex(unsigned long idx) const

+{

+    const ptrdiff_t count = m_trackEntriesEnd - m_trackEntries;

+

+    if (idx >= static_cast<unsigned long>(count))

+         return NULL;

+

+    return m_trackEntries[idx];

+}

+

+

+void Cluster::Load()

+{

+    assert(m_pSegment);

+    assert(m_pos);

+    assert(m_size);

+

+    if (m_pos > 0)  //loaded

+    {

+        assert(m_size > 0);

+        assert(m_timecode >= 0);

+        return;

+    }

+

+    assert(m_pos < 0);  //not loaded yet

+    assert(m_size < 0);

+    assert(m_timecode < 0);

+

+    IMkvReader* const pReader = m_pSegment->m_pReader;

+

+    m_pos *= -1;                                  //relative to segment

+    long long pos = m_pSegment->m_start + m_pos;  //absolute

+

+    long len;

+

+    const long long id_ = ReadUInt(pReader, pos, len);

+    assert(id_ >= 0);

+    assert(id_ == 0x0F43B675);  //Cluster ID

+

+    pos += len;  //consume id

+

+    const long long size_ = ReadUInt(pReader, pos, len);

+    assert(size_ >= 0);

+

+    pos += len;  //consume size

+

+    m_size = size_;

+    const long long stop = pos + size_;

+

+    long long timecode = -1;

+

+    while (pos < stop)

+    {

+        if (Match(pReader, pos, 0x67, timecode))

+            break;

+        else

+        {

+            const long long id = ReadUInt(pReader, pos, len);

+            assert(id >= 0);  //TODO

+            assert((pos + len) <= stop);

+

+            pos += len;  //consume id

+

+            const long long size = ReadUInt(pReader, pos, len);

+            assert(size >= 0);  //TODO

+            assert((pos + len) <= stop);

+

+            pos += len;  //consume size

+

+            if (id == 0x20)  //BlockGroup ID

+                break;

+

+            if (id == 0x23)  //SimpleBlock ID

+                break;

+

+            pos += size;  //consume payload

+            assert(pos <= stop);

+        }

+    }

+

+    assert(pos <= stop);

+    assert(timecode >= 0);

+

+    m_timecode = timecode;

+}

+

+

+Cluster* Cluster::Parse(

+    Segment* pSegment,

+    long idx,

+    long long off)

+{

+    assert(pSegment);

+    assert(off >= 0);

+    assert(off < pSegment->m_size);

+

+    Cluster* const pCluster = new Cluster(pSegment, idx, -off);

+    assert(pCluster);

+

+    return pCluster;

+}

+

+

+Cluster::Cluster() :

+    m_pSegment(NULL),

+    m_index(0),

+    m_pos(0),

+    m_size(0),

+    m_timecode(0),

+    m_entries(NULL),

+    m_entriesCount(0)

+{

+}

+

+

+Cluster::Cluster(

+    Segment* pSegment,

+    long idx,

+    long long off) :

+    m_pSegment(pSegment),

+    m_index(idx),

+    m_pos(off),

+    m_size(-1),

+    m_timecode(-1),

+    m_entries(NULL),

+    m_entriesCount(0)

+{

+}

+

+

+Cluster::~Cluster()

+{

+    BlockEntry** i = m_entries;

+    BlockEntry** const j = m_entries + m_entriesCount;

+

+    while (i != j)

+    {

+         BlockEntry* p = *i++;

+         assert(p);

+

+         delete p;

+    }

+

+    delete[] m_entries;

+}

+

+

+bool Cluster::EOS() const

+{

+    return (m_pSegment == NULL);

+}

+

+

+void Cluster::LoadBlockEntries()

+{

+    if (m_entries)

+        return;

+

+    assert(m_pSegment);

+    assert(m_pos);

+    assert(m_size);

+    assert(m_entriesCount == 0);

+

+    IMkvReader* const pReader = m_pSegment->m_pReader;

+

+    if (m_pos < 0)

+        m_pos *= -1;  //relative to segment

+

+    long long pos = m_pSegment->m_start + m_pos;  //absolute

+

+    {

+        long len;

+

+        const long long id = ReadUInt(pReader, pos, len);

+        id;

+        assert(id >= 0);

+        assert(id == 0x0F43B675);  //Cluster ID

+

+        pos += len;  //consume id

+

+        const long long size = ReadUInt(pReader, pos, len);

+        assert(size > 0);

+

+        pos += len;  //consume size

+

+        //pos now points to start of payload

+

+        if (m_size >= 0)

+            assert(size == m_size);

+        else

+            m_size = size;

+    }

+

+    const long long stop = pos + m_size;

+    long long timecode = -1;  //of cluster itself

+

+    //First count the number of entries

+

+    long long idx = pos;  //points to start of payload

+    m_entriesCount = 0;

+

+    while (idx < stop)

+    {

+        if (Match(pReader, idx, 0x67, timecode))

+        {

+            if (m_timecode >= 0)

+                assert(timecode == m_timecode);

+            else

+                m_timecode = timecode;

+        }

+        else

+        {

+            long len;

+

+            const long long id = ReadUInt(pReader, idx, len);

+            assert(id >= 0);  //TODO

+            assert((idx + len) <= stop);

+

+            idx += len;  //consume id

+

+            const long long size = ReadUInt(pReader, idx, len);

+            assert(size >= 0);  //TODO

+            assert((idx + len) <= stop);

+

+            idx += len;  //consume size

+

+            if (id == 0x20)  //BlockGroup ID

+                ++m_entriesCount;

+            else if (id == 0x23)  //SimpleBlock ID

+                ++m_entriesCount;

+

+            idx += size;  //consume payload

+            assert(idx <= stop);

+        }

+    }

+

+    assert(idx == stop);

+    assert(m_timecode >= 0);

+

+    if (m_entriesCount == 0)  //TODO: handle empty clusters

+        return;

+

+    m_entries = new BlockEntry*[m_entriesCount];

+    size_t index = 0;

+

+    while (pos < stop)

+    {

+        if (Match(pReader, pos, 0x67, timecode))

+            assert(timecode == m_timecode);

+        else

+        {

+            long len;

+            const long long id = ReadUInt(pReader, pos, len);

+            assert(id >= 0);  //TODO

+            assert((pos + len) <= stop);

+

+            pos += len;  //consume id

+

+            const long long size = ReadUInt(pReader, pos, len);

+            assert(size >= 0);  //TODO

+            assert((pos + len) <= stop);

+

+            pos += len;  //consume size

+

+            if (id == 0x20)  //BlockGroup ID

+                ParseBlockGroup(pos, size, index++);

+            else if (id == 0x23)  //SimpleBlock ID

+                ParseSimpleBlock(pos, size, index++);

+

+            pos += size;  //consume payload

+            assert(pos <= stop);

+        }

+    }

+

+    assert(pos == stop);

+    assert(timecode >= 0);

+    assert(index == m_entriesCount);

+}

+

+

+

+long long Cluster::GetTimeCode()

+{

+    Load();

+    return m_timecode;

+}

+

+

+long long Cluster::GetTime()

+{

+    const long long tc = GetTimeCode();

+    assert(tc >= 0);

+

+    const SegmentInfo* const pInfo = m_pSegment->GetInfo();

+    assert(pInfo);

+

+    const long long scale = pInfo->GetTimeCodeScale();

+    assert(scale >= 1);

+

+    const long long t = m_timecode * scale;

+

+    return t;

+}

+

+

+long long Cluster::GetFirstTime()

+{

+    const BlockEntry* const pEntry = GetFirst();

+

+    if (pEntry == NULL)  //empty cluster

+        return GetTime();

+

+    const Block* const pBlock = pEntry->GetBlock();

+    assert(pBlock);

+

+    return pBlock->GetTime(this);

+}

+

+

+long long Cluster::GetLastTime()

+{

+    const BlockEntry* const pEntry = GetLast();

+

+    if (pEntry == NULL)  //empty cluster

+        return GetTime();

+

+    const Block* const pBlock = pEntry->GetBlock();

+    assert(pBlock);

+

+    return pBlock->GetTime(this);

+}

+

+

+void Cluster::ParseBlockGroup(long long start, long long size, size_t index)

+{

+    assert(m_entries);

+    assert(m_entriesCount);

+    assert(index < m_entriesCount);

+

+    BlockGroup* const pGroup =

+        new (std::nothrow) BlockGroup(this, index, start, size);

+    assert(pGroup);  //TODO

+

+    m_entries[index] = pGroup;

+}

+

+

+

+void Cluster::ParseSimpleBlock(long long start, long long size, size_t index)

+{

+    assert(m_entries);

+    assert(m_entriesCount);

+    assert(index < m_entriesCount);

+

+    SimpleBlock* const pSimpleBlock =

+        new (std::nothrow) SimpleBlock(this, index, start, size);

+    assert(pSimpleBlock);  //TODO

+

+    m_entries[index] = pSimpleBlock;

+}

+

+

+const BlockEntry* Cluster::GetFirst()

+{

+    LoadBlockEntries();

+    //assert(m_entries);

+    //assert(m_entriesCount >= 1);

+

+    if ((m_entries == NULL) || (m_entriesCount == 0))

+        return NULL;

+

+    const BlockEntry* const pFirst = m_entries[0];

+    assert(pFirst);

+

+    return pFirst;

+}

+

+

+const BlockEntry* Cluster::GetLast()

+{

+    LoadBlockEntries();

+    //assert(m_entries);

+    //assert(m_entriesCount >= 1);

+

+    if ((m_entries == NULL) || (m_entriesCount == 0))

+        return NULL;

+

+    const size_t idx = m_entriesCount - 1;

+

+    const BlockEntry* const pLast = m_entries[idx];

+    assert(pLast);

+

+    return pLast;

+}

+

+

+const BlockEntry* Cluster::GetNext(const BlockEntry* pEntry) const

+{

+    assert(pEntry);

+    assert(m_entries);

+    assert(m_entriesCount);

+

+    size_t idx = pEntry->GetIndex();

+    assert(idx < m_entriesCount);

+    assert(m_entries[idx] == pEntry);

+

+    ++idx;

+

+    if (idx >= m_entriesCount)

+      return NULL;

+

+    return m_entries[idx];

+}

+

+

+const BlockEntry* Cluster::GetEntry(const Track* pTrack)

+{

+    assert(pTrack);

+

+    if (m_pSegment == NULL)  //EOS

+        return pTrack->GetEOS();

+

+    LoadBlockEntries();

+

+    if ((m_entries == NULL) || (m_entriesCount == 0))

+        return NULL;

+

+    BlockEntry** i = m_entries;

+    assert(i);

+

+    BlockEntry** const j = i + m_entriesCount;

+

+    while (i != j)

+    {

+        const BlockEntry* const pEntry = *i++;

+        assert(pEntry);

+        assert(!pEntry->EOS());

+

+        const Block* const pBlock = pEntry->GetBlock();

+        assert(pBlock);

+

+        if (pBlock->GetTrackNumber() != pTrack->GetNumber())

+            continue;

+

+        if (pTrack->VetEntry(pEntry))

+            return pEntry;

+    }

+

+    return pTrack->GetEOS();  //no satisfactory block found

+}

+

+

+const BlockEntry*

+Cluster::GetEntry(

+    const CuePoint& cp,

+    const CuePoint::TrackPosition& tp)

+{

+    assert(m_pSegment);

+

+    LoadBlockEntries();

+

+    if (m_entries == NULL)

+        return NULL;

+

+    const long long count = m_entriesCount;

+

+    if (count <= 0)

+        return NULL;

+

+    const long long tc = cp.GetTimeCode();

+

+    if ((tp.m_block > 0) && (tp.m_block <= count))

+    {

+        const size_t block = static_cast<size_t>(tp.m_block);

+        const size_t index = block - 1;

+

+        const BlockEntry* const pEntry = m_entries[index];

+        assert(pEntry);

+        assert(!pEntry->EOS());

+

+        const Block* const pBlock = pEntry->GetBlock();

+        assert(pBlock);

+

+        if ((pBlock->GetTrackNumber() == tp.m_track) &&

+            (pBlock->GetTimeCode(this) == tc))

+        {

+            return pEntry;

+        }

+    }

+

+    const BlockEntry* const* i = m_entries;

+    const BlockEntry* const* const j = i + count;

+

+    while (i != j)

+    {

+        const BlockEntry* const pEntry = *i++;

+        assert(pEntry);

+        assert(!pEntry->EOS());

+

+        const Block* const pBlock = pEntry->GetBlock();

+        assert(pBlock);

+

+        if (pBlock->GetTrackNumber() != tp.m_track)

+            continue;

+

+        const long long tc_ = pBlock->GetTimeCode(this);

+

+        if (tc_ < tc)

+            continue;

+

+        if (tc_ > tc)

+            return NULL;

+

+        const Tracks* const pTracks = m_pSegment->GetTracks();

+        assert(pTracks);

+

+        const long tn = static_cast<long>(tp.m_track);

+        const Track* const pTrack = pTracks->GetTrackByNumber(tn);

+

+        if (pTrack == NULL)

+            return NULL;

+

+        const long long type = pTrack->GetType();

+

+        if (type == 2)  //audio

+            return pEntry;

+

+        if (type != 1)  //not video

+            return NULL;

+

+        if (!pBlock->IsKey())

+            return NULL;

+

+        return pEntry;

+    }

+

+    return NULL;

+}

+

+

+const BlockEntry* Cluster::GetMaxKey(const VideoTrack* pTrack)

+{

+    assert(pTrack);

+

+    if (m_pSegment == NULL)  //EOS

+        return pTrack->GetEOS();

+

+    LoadBlockEntries();

+    //assert(m_entries);

+

+    BlockEntry** i = m_entries + m_entriesCount;

+    BlockEntry** const j = m_entries;

+

+    while (i != j)

+    {

+        const BlockEntry* const pEntry = *--i;

+        assert(pEntry);

+        assert(!pEntry->EOS());

+

+        const Block* const pBlock = pEntry->GetBlock();

+        assert(pBlock);

+

+        if (pBlock->GetTrackNumber() != pTrack->GetNumber())

+            continue;

+

+        if (pBlock->IsKey())

+            return pEntry;

+    }

+

+    return pTrack->GetEOS();  //no satisfactory block found

+}

+

+

+

+BlockEntry::BlockEntry()

+{

+}

+

+

+BlockEntry::~BlockEntry()

+{

+}

+

+

+SimpleBlock::SimpleBlock(

+    Cluster* pCluster,

+    size_t idx,

+    long long start,

+    long long size) :

+    m_pCluster(pCluster),

+    m_index(idx),

+    m_block(start, size, pCluster->m_pSegment->m_pReader)

+{

+}

+

+

+bool SimpleBlock::EOS() const

+{

+    return false;

+}

+

+

+Cluster* SimpleBlock::GetCluster() const

+{

+    return m_pCluster;

+}

+

+

+size_t SimpleBlock::GetIndex() const

+{

+    return m_index;

+}

+

+

+const Block* SimpleBlock::GetBlock() const

+{

+    return &m_block;

+}

+

+

+bool SimpleBlock::IsBFrame() const

+{

+    return false;

+}

+

+

+BlockGroup::BlockGroup(

+    Cluster* pCluster,

+    size_t idx,

+    long long start,

+    long long size_) :

+    m_pCluster(pCluster),

+    m_index(idx),

+    m_prevTimeCode(0),

+    m_nextTimeCode(0),

+    m_pBlock(NULL)  //TODO: accept multiple blocks within a block group

+{

+    IMkvReader* const pReader = m_pCluster->m_pSegment->m_pReader;

+

+    long long pos = start;

+    const long long stop = start + size_;

+

+    bool bSimpleBlock = false;

+    bool bReferenceBlock = false;

+

+    while (pos < stop)

+    {

+        short t;

+

+        if (Match(pReader, pos, 0x7B, t))

+        {

+            if (t < 0)

+                m_prevTimeCode = t;

+            else if (t > 0)

+                m_nextTimeCode = t;

+            else

+                assert(false);

+

+            bReferenceBlock = true;

+        }

+        else

+        {

+            long len;

+            const long long id = ReadUInt(pReader, pos, len);

+            assert(id >= 0);  //TODO

+            assert((pos + len) <= stop);

+

+            pos += len;  //consume ID

+

+            const long long size = ReadUInt(pReader, pos, len);

+            assert(size >= 0);  //TODO

+            assert((pos + len) <= stop);

+

+            pos += len;  //consume size

+

+            switch (id)

+            {

+                case 0x23:  //SimpleBlock ID

+                    bSimpleBlock = true;

+                    //YES, FALL THROUGH TO NEXT CASE

+

+                case 0x21:  //Block ID

+                    ParseBlock(pos, size);

+                    break;

+

+                default:

+                    break;

+            }

+

+            pos += size;  //consume payload

+            assert(pos <= stop);

+        }

+    }

+

+    assert(pos == stop);

+    assert(m_pBlock);

+

+    if (!bSimpleBlock)

+        m_pBlock->SetKey(!bReferenceBlock);

+}

+

+

+BlockGroup::~BlockGroup()

+{

+    delete m_pBlock;

+}

+

+

+void BlockGroup::ParseBlock(long long start, long long size)

+{

+    IMkvReader* const pReader = m_pCluster->m_pSegment->m_pReader;

+

+    Block* const pBlock = new Block(start, size, pReader);

+    assert(pBlock);  //TODO

+

+    //TODO: the Matroska spec says you have multiple blocks within the

+    //same block group, with blocks ranked by priority (the flag bits).

+

+    assert(m_pBlock == NULL);

+    m_pBlock = pBlock;

+}

+

+

+bool BlockGroup::EOS() const

+{

+    return false;

+}

+

+

+Cluster* BlockGroup::GetCluster() const

+{

+    return m_pCluster;

+}

+

+

+size_t BlockGroup::GetIndex() const

+{

+    return m_index;

+}

+

+

+const Block* BlockGroup::GetBlock() const

+{

+    return m_pBlock;

+}

+

+

+short BlockGroup::GetPrevTimeCode() const

+{

+    return m_prevTimeCode;

+}

+

+

+short BlockGroup::GetNextTimeCode() const

+{

+    return m_nextTimeCode;

+}

+

+

+bool BlockGroup::IsBFrame() const

+{

+    return (m_nextTimeCode > 0);

+}

+

+

+

+Block::Block(long long start, long long size_, IMkvReader* pReader) :

+    m_start(start),

+    m_size(size_)

+{

+    long long pos = start;

+    const long long stop = start + size_;

+

+    long len;

+

+    m_track = ReadUInt(pReader, pos, len);

+    assert(m_track > 0);

+    assert((pos + len) <= stop);

+

+    pos += len;  //consume track number

+    assert((stop - pos) >= 2);

+

+    m_timecode = Unserialize2SInt(pReader, pos);

+

+    pos += 2;

+    assert((stop - pos) >= 1);

+

+    const long hr = pReader->Read(pos, 1, &m_flags);

+    assert(hr == 0L);

+

+    ++pos;

+    assert(pos <= stop);

+

+    m_frameOff = pos;

+

+    const long long frame_size = stop - pos;

+

+    assert(frame_size <= 2147483647L);

+

+    m_frameSize = static_cast<long>(frame_size);

+}

+

+

+long long Block::GetTimeCode(Cluster* pCluster) const

+{

+    assert(pCluster);

+

+    const long long tc0 = pCluster->GetTimeCode();

+    assert(tc0 >= 0);

+

+    const long long tc = tc0 + static_cast<long long>(m_timecode);

+    assert(tc >= 0);

+

+    return tc;  //unscaled timecode units

+}

+

+

+long long Block::GetTime(Cluster* pCluster) const

+{

+    assert(pCluster);

+

+    const long long tc = GetTimeCode(pCluster);

+

+    const Segment* const pSegment = pCluster->m_pSegment;

+    const SegmentInfo* const pInfo = pSegment->GetInfo();

+    assert(pInfo);

+

+    const long long scale = pInfo->GetTimeCodeScale();

+    assert(scale >= 1);

+

+    const long long ns = tc * scale;

+

+    return ns;

+}

+

+

+long long Block::GetTrackNumber() const

+{

+    return m_track;

+}

+

+

+bool Block::IsKey() const

+{

+    return ((m_flags & static_cast<unsigned char>(1 << 7)) != 0);

+}

+

+

+void Block::SetKey(bool bKey)

+{

+    if (bKey)

+        m_flags |= static_cast<unsigned char>(1 << 7);

+    else

+        m_flags &= 0x7F;

+}

+

+

+long long Block::GetOffset() const

+{

+  return m_frameOff;

+}

+

+

+long Block::GetSize() const

+{

+    return m_frameSize;

+}

+

+

+long Block::Read(IMkvReader* pReader, unsigned char* buf) const

+{

+

+    assert(pReader);

+    assert(buf);

+

+    const long hr = pReader->Read(m_frameOff, m_frameSize, buf);

+

+    return hr;

+}

+

+

+}  //end namespace mkvparser

diff --git a/media/libstagefright/matroska/mkvparser.hpp b/media/libstagefright/matroska/mkvparser.hpp
index 4d311b4..c46d349 100644
--- a/media/libstagefright/matroska/mkvparser.hpp
+++ b/media/libstagefright/matroska/mkvparser.hpp
@@ -1,428 +1,554 @@
-#ifndef MKVPARSER_HPP
-#define MKVPARSER_HPP
-
-#include <cstdlib>
-#include <cstdio>
-
-namespace mkvparser
-{
-
-const int E_FILE_FORMAT_INVALID = -2;
-const int E_BUFFER_NOT_FULL = -3;
-
-class IMkvReader
-{
-public:
-    virtual int Read(long long position, long length, unsigned char* buffer) = 0;
-    virtual int Length(long long* total, long long* available) = 0; 	
-protected:
-    virtual ~IMkvReader();
-};
-
-long long GetUIntLength(IMkvReader*, long long, long&);
-long long ReadUInt(IMkvReader*, long long, long&);
-long long SyncReadUInt(IMkvReader*, long long pos, long long stop, long&);
-long long UnserializeUInt(IMkvReader*, long long pos, long long size);
-float Unserialize4Float(IMkvReader*, long long);
-double Unserialize8Double(IMkvReader*, long long);
-short Unserialize2SInt(IMkvReader*, long long);
-signed char Unserialize1SInt(IMkvReader*, long long);
-bool Match(IMkvReader*, long long&, unsigned long, long long&);
-bool Match(IMkvReader*, long long&, unsigned long, char*&);
-bool Match(IMkvReader*, long long&, unsigned long,unsigned char*&,
-           size_t *optionalSize = NULL);
-bool Match(IMkvReader*, long long&, unsigned long, double&);
-bool Match(IMkvReader*, long long&, unsigned long, short&);
-
-
-struct EBMLHeader
-{
-    EBMLHeader();
-    ~EBMLHeader();  
-    long long m_version;
-    long long m_readVersion;
-    long long m_maxIdLength;
-    long long m_maxSizeLength;
-    char* m_docType;
-    long long m_docTypeVersion;
-    long long m_docTypeReadVersion;
-    
-    long long Parse(IMkvReader*, long long&);
-};
-
-
-class Segment;
-class Track;
-class Cluster;
-
-class Block
-{
-    Block(const Block&);
-    Block& operator=(const Block&);
-
-public:
-    const long long m_start;
-    const long long m_size;
-    
-    Block(long long start, long long size, IMkvReader*);
-    
-    unsigned long GetTrackNumber() const;
-    
-    long long GetTimeCode(Cluster*) const;  //absolute, but not scaled
-    long long GetTime(Cluster*) const;      //absolute, and scaled (nanosecond units)
-    bool IsKey() const;
-    void SetKey(bool);
-
-    long GetSize() const;
-    long Read(IMkvReader*, unsigned char*) const;
-    
-private:
-    long long m_track;   //Track::Number()
-    short m_timecode;  //relative to cluster
-    unsigned char m_flags;
-    long long m_frameOff;
-    long m_frameSize;    
-
-};
-
-
-class BlockEntry
-{
-    BlockEntry(const BlockEntry&);
-    BlockEntry& operator=(const BlockEntry&);
-    
-public:
-    virtual ~BlockEntry();
-    virtual bool EOS() const = 0;
-    virtual Cluster* GetCluster() const = 0;
-    virtual size_t GetIndex() const = 0;
-    virtual const Block* GetBlock() const = 0;
-    virtual bool IsBFrame() const = 0;
-    
-protected:
-    BlockEntry();
-
-};
-
-
-class SimpleBlock : public BlockEntry
-{
-    SimpleBlock(const SimpleBlock&);
-    SimpleBlock& operator=(const SimpleBlock&);
-
-public:
-    SimpleBlock(Cluster*, size_t, long long start, long long size);
-
-    bool EOS() const;
-    Cluster* GetCluster() const; 
-    size_t GetIndex() const;
-    const Block* GetBlock() const;
-    bool IsBFrame() const;
-
-protected:
-    Cluster* const m_pCluster;
-    const size_t m_index;
-    Block m_block;
-    
-};
-
-
-class BlockGroup : public BlockEntry
-{
-    BlockGroup(const BlockGroup&);
-    BlockGroup& operator=(const BlockGroup&);
-
-public:
-    BlockGroup(Cluster*, size_t, long long, long long);
-    ~BlockGroup();
-    
-    bool EOS() const;
-    Cluster* GetCluster() const; 
-    size_t GetIndex() const;
-    const Block* GetBlock() const;
-    bool IsBFrame() const;
-    
-    short GetPrevTimeCode() const;  //relative to block's time
-    short GetNextTimeCode() const;  //as above
-    
-protected:
-    Cluster* const m_pCluster;
-    const size_t m_index;
-    
-private:
-    BlockGroup(Cluster*, size_t, unsigned long);
-    void ParseBlock(long long start, long long size);
-
-    short m_prevTimeCode;
-    short m_nextTimeCode;
-    
-    //TODO: the Matroska spec says you can have multiple blocks within the 
-    //same block group, with blocks ranked by priority (the flag bits).
-    //For now we just cache a single block.
-#if 0
-    typedef std::deque<Block*> blocks_t;
-    blocks_t m_blocks;  //In practice should contain only a single element.
-#else
-    Block* m_pBlock;
-#endif
-    
-};
-
-
-class Track
-{
-    Track(const Track&);
-    Track& operator=(const Track&);
-
-public:    
-    Segment* const m_pSegment;
-    virtual ~Track();
-    
-    long long GetType() const; 
-    unsigned long GetNumber() const;
-    const char* GetNameAsUTF8() const;
-    const char* GetCodecNameAsUTF8() const;
-    const char* GetCodecId() const;
-    const unsigned char* GetCodecPrivate(
-            size_t *optionalSize = NULL) const;
-    
-    const BlockEntry* GetEOS() const;
-    
-    struct Settings 
-    {
-        long long start;
-        long long size;
-    };
-    
-    struct Info
-    {
-        long long type;
-        long long number;
-        long long uid;
-        char* nameAsUTF8;
-        char* codecId;
-        unsigned char* codecPrivate;
-        size_t codecPrivateSize;
-        char* codecNameAsUTF8;
-        Settings settings;
-        Info();
-        void Clear();
-    };
-    
-    long GetFirst(const BlockEntry*&) const;
-    long GetNext(const BlockEntry* pCurr, const BlockEntry*& pNext) const;
-    virtual bool VetEntry(const BlockEntry*) const = 0;
-        
-protected:
-    Track(Segment*, const Info&);        
-    const Info m_info;
-
-    class EOSBlock : public BlockEntry
-    {
-    public:
-        EOSBlock();
-
-        bool EOS() const;    
-        Cluster* GetCluster() const;
-        size_t GetIndex() const;
-        const Block* GetBlock() const;
-        bool IsBFrame() const;
-    };
-    
-    EOSBlock m_eos;
-    
-};
-
-
-class VideoTrack : public Track
-{
-    VideoTrack(const VideoTrack&);
-    VideoTrack& operator=(const VideoTrack&);
-    
-public:
-    VideoTrack(Segment*, const Info&);    
-    long long GetWidth() const;
-    long long GetHeight() const;
-    double GetFrameRate() const;
-    
-    bool VetEntry(const BlockEntry*) const;
-
-private:
-    long long m_width;
-    long long m_height;
-    double m_rate;
-    
-};
-
-
-class AudioTrack : public Track
-{
-    AudioTrack(const AudioTrack&);
-    AudioTrack& operator=(const AudioTrack&);
-
-public:
-    AudioTrack(Segment*, const Info&);    
-    double GetSamplingRate() const;
-    long long GetChannels() const;
-    long long GetBitDepth() const;    
-    bool VetEntry(const BlockEntry*) const;
-
-private:
-    double m_rate;
-    long long m_channels;
-    long long m_bitDepth;
-};
-
-
-class Tracks
-{
-    Tracks(const Tracks&);
-    Tracks& operator=(const Tracks&);
-
-public:
-    Segment* const m_pSegment;
-    const long long m_start;
-    const long long m_size;
-    
-    Tracks(Segment*, long long start, long long size);
-    virtual ~Tracks();
-
-    Track* GetTrackByNumber(unsigned long tn) const;
-    Track* GetTrackByIndex(unsigned long idx) const;
-    
-private:
-    Track** m_trackEntries; 
-    Track** m_trackEntriesEnd;
-
-    void ParseTrackEntry(long long, long long, Track*&);
-    
-public:
-    unsigned long GetTracksCount() const;
-};
-
-
-class SegmentInfo
-{
-    SegmentInfo(const SegmentInfo&);
-    SegmentInfo& operator=(const SegmentInfo&);
-    
-public:
-    Segment* const m_pSegment;
-    const long long m_start;
-    const long long m_size;
-    
-    SegmentInfo(Segment*, long long start, long long size);
-    ~SegmentInfo();
-    long long GetTimeCodeScale() const;
-    long long GetDuration() const;  //scaled
-    const char* GetMuxingAppAsUTF8() const;
-    const char* GetWritingAppAsUTF8() const;
-    const char* GetTitleAsUTF8() const;
-    
-private:
-    long long m_timecodeScale;
-    double m_duration;
-    char* m_pMuxingAppAsUTF8;
-    char* m_pWritingAppAsUTF8;
-    char* m_pTitleAsUTF8;
-};
-
-
-class Cluster
-{
-    Cluster(const Cluster&);
-    Cluster& operator=(const Cluster&);
-
-public:
-    Segment* const m_pSegment;
-    const size_t m_index;
-    
-public:    
-    static Cluster* Parse(Segment*, size_t, long long off);
-
-    Cluster();  //EndOfStream
-    ~Cluster();
-    
-    bool EOS() const;
-    
-    long long GetTimeCode();  //absolute, but not scaled
-    long long GetTime();      //absolute, and scaled (nanosecond units)
-
-    const BlockEntry* GetFirst();
-    const BlockEntry* GetLast();
-    const BlockEntry* GetNext(const BlockEntry*) const;    
-    const BlockEntry* GetEntry(const Track*);
-protected:    
-    Cluster(Segment*, size_t, long long off);
-    
-private:
-    long long m_start;
-    long long m_size;    
-    long long m_timecode;
-    BlockEntry** m_pEntries;
-    size_t m_entriesCount;
-   
-    void Load();
-    void LoadBlockEntries();
-    void ParseBlockGroup(long long, long long, size_t);
-    void ParseSimpleBlock(long long, long long, size_t);
-    
-};
-
-
-class Segment
-{
-    Segment(const Segment&);
-    Segment& operator=(const Segment&);
-
-private:
-    Segment(IMkvReader*, long long pos, long long size);
-
-public:
-    IMkvReader* const m_pReader;
-    const long long m_start;  //posn of segment payload
-    const long long m_size;   //size of segment payload
-    Cluster m_eos;  //TODO: make private?
-    
-    static long long CreateInstance(IMkvReader*, long long, Segment*&);
-    ~Segment();
-
-    //for big-bang loading (source filter)
-    long Load();
-
-    //for incremental loading (splitter)    
-    long long Unparsed() const;
-    long long ParseHeaders();
-    long ParseCluster(Cluster*&, long long& newpos) const;
-    bool AddCluster(Cluster*, long long);
-
-    Tracks* GetTracks() const;    
-    const SegmentInfo* const GetInfo() const;
-    long long GetDuration() const;
-    
-    //NOTE: this turned out to be too inefficient.
-    //long long Load(long long time_nanoseconds);
-
-    Cluster* GetFirst();
-    Cluster* GetLast();
-    unsigned long GetCount() const;
-    
-    Cluster* GetNext(const Cluster*);
-    Cluster* GetCluster(long long time_nanoseconds);
-    
-private:
-    long long m_pos;  //absolute file posn; what has been consumed so far    
-    SegmentInfo* m_pInfo;
-    Tracks* m_pTracks;    
-    Cluster** m_clusters;
-    size_t m_clusterCount;
-
-    void ParseSeekHead(long long pos, long long size, size_t*);
-    void ParseSeekEntry(long long pos, long long size, size_t*);
-    void ParseSecondarySeekHead(long long off, size_t*);
-};
-
-
-}  //end namespace mkvparser
-
-#endif  //MKVPARSER_HPP
+// Copyright (c) 2010 The WebM project authors. All Rights Reserved.

+//

+// Use of this source code is governed by a BSD-style license

+// that can be found in the LICENSE file in the root of the source

+// tree. An additional intellectual property rights grant can be found

+// in the file PATENTS.  All contributing project authors may

+// be found in the AUTHORS file in the root of the source tree.

+

+#ifndef MKVPARSER_HPP

+#define MKVPARSER_HPP

+

+#include <cstdlib>

+#include <cstdio>

+

+namespace mkvparser

+{

+

+const int E_FILE_FORMAT_INVALID = -2;

+const int E_BUFFER_NOT_FULL = -3;

+

+class IMkvReader

+{

+public:

+    virtual int Read(long long pos, long len, unsigned char* buf) = 0;

+    virtual int Length(long long* total, long long* available) = 0;

+protected:

+    virtual ~IMkvReader();

+};

+

+long long GetUIntLength(IMkvReader*, long long, long&);

+long long ReadUInt(IMkvReader*, long long, long&);

+long long SyncReadUInt(IMkvReader*, long long pos, long long stop, long&);

+long long UnserializeUInt(IMkvReader*, long long pos, long long size);

+float Unserialize4Float(IMkvReader*, long long);

+double Unserialize8Double(IMkvReader*, long long);

+short Unserialize2SInt(IMkvReader*, long long);

+signed char Unserialize1SInt(IMkvReader*, long long);

+bool Match(IMkvReader*, long long&, unsigned long, long long&);

+bool Match(IMkvReader*, long long&, unsigned long, char*&);

+bool Match(IMkvReader*, long long&, unsigned long,unsigned char*&, size_t&);

+bool Match(IMkvReader*, long long&, unsigned long, double&);

+bool Match(IMkvReader*, long long&, unsigned long, short&);

+

+void GetVersion(int& major, int& minor, int& build, int& revision);

+

+struct EBMLHeader

+{

+    EBMLHeader();

+    ~EBMLHeader();

+    long long m_version;

+    long long m_readVersion;

+    long long m_maxIdLength;

+    long long m_maxSizeLength;

+    char* m_docType;

+    long long m_docTypeVersion;

+    long long m_docTypeReadVersion;

+

+    long long Parse(IMkvReader*, long long&);

+};

+

+

+class Segment;

+class Track;

+class Cluster;

+

+class Block

+{

+    Block(const Block&);

+    Block& operator=(const Block&);

+

+public:

+    const long long m_start;

+    const long long m_size;

+

+    Block(long long start, long long size, IMkvReader*);

+

+    long long GetTrackNumber() const;

+    long long GetTimeCode(Cluster*) const;  //absolute, but not scaled

+    long long GetTime(Cluster*) const;      //absolute, and scaled (ns units)

+    bool IsKey() const;

+    void SetKey(bool);

+

+    long long GetOffset() const;

+    long GetSize() const;

+    long Read(IMkvReader*, unsigned char*) const;

+

+private:

+    long long m_track;   //Track::Number()

+    short m_timecode;  //relative to cluster

+    unsigned char m_flags;

+    long long m_frameOff;

+    long m_frameSize;

+

+};

+

+

+class BlockEntry

+{

+    BlockEntry(const BlockEntry&);

+    BlockEntry& operator=(const BlockEntry&);

+

+public:

+    virtual ~BlockEntry();

+    virtual bool EOS() const = 0;

+    virtual Cluster* GetCluster() const = 0;

+    virtual size_t GetIndex() const = 0;

+    virtual const Block* GetBlock() const = 0;

+    virtual bool IsBFrame() const = 0;

+

+protected:

+    BlockEntry();

+

+};

+

+

+class SimpleBlock : public BlockEntry

+{

+    SimpleBlock(const SimpleBlock&);

+    SimpleBlock& operator=(const SimpleBlock&);

+

+public:

+    SimpleBlock(Cluster*, size_t, long long start, long long size);

+

+    bool EOS() const;

+    Cluster* GetCluster() const;

+    size_t GetIndex() const;

+    const Block* GetBlock() const;

+    bool IsBFrame() const;

+

+protected:

+    Cluster* const m_pCluster;

+    const size_t m_index;

+    Block m_block;

+

+};

+

+

+class BlockGroup : public BlockEntry

+{

+    BlockGroup(const BlockGroup&);

+    BlockGroup& operator=(const BlockGroup&);

+

+public:

+    BlockGroup(Cluster*, size_t, long long, long long);

+    ~BlockGroup();

+

+    bool EOS() const;

+    Cluster* GetCluster() const;

+    size_t GetIndex() const;

+    const Block* GetBlock() const;

+    bool IsBFrame() const;

+

+    short GetPrevTimeCode() const;  //relative to block's time

+    short GetNextTimeCode() const;  //as above

+

+protected:

+    Cluster* const m_pCluster;

+    const size_t m_index;

+

+private:

+    BlockGroup(Cluster*, size_t, unsigned long);

+    void ParseBlock(long long start, long long size);

+

+    short m_prevTimeCode;

+    short m_nextTimeCode;

+

+    //TODO: the Matroska spec says you can have multiple blocks within the

+    //same block group, with blocks ranked by priority (the flag bits).

+    //For now we just cache a single block.

+#if 0

+    typedef std::deque<Block*> blocks_t;

+    blocks_t m_blocks;  //In practice should contain only a single element.

+#else

+    Block* m_pBlock;

+#endif

+

+};

+

+

+class Track

+{

+    Track(const Track&);

+    Track& operator=(const Track&);

+

+public:

+    Segment* const m_pSegment;

+    virtual ~Track();

+

+    long long GetType() const;

+    long long GetNumber() const;

+    const char* GetNameAsUTF8() const;

+    const char* GetCodecNameAsUTF8() const;

+    const char* GetCodecId() const;

+    const unsigned char* GetCodecPrivate(size_t&) const;

+

+    const BlockEntry* GetEOS() const;

+

+    struct Settings

+    {

+        long long start;

+        long long size;

+    };

+

+    struct Info

+    {

+        long long type;

+        long long number;

+        long long uid;

+        char* nameAsUTF8;

+        char* codecId;

+        unsigned char* codecPrivate;

+        size_t codecPrivateSize;

+        char* codecNameAsUTF8;

+        Settings settings;

+        Info();

+        void Clear();

+    };

+

+    long GetFirst(const BlockEntry*&) const;

+    long GetNext(const BlockEntry* pCurr, const BlockEntry*& pNext) const;

+    virtual bool VetEntry(const BlockEntry*) const = 0;

+

+protected:

+    Track(Segment*, const Info&);

+    const Info m_info;

+

+    class EOSBlock : public BlockEntry

+    {

+    public:

+        EOSBlock();

+

+        bool EOS() const;

+        Cluster* GetCluster() const;

+        size_t GetIndex() const;

+        const Block* GetBlock() const;

+        bool IsBFrame() const;

+    };

+

+    EOSBlock m_eos;

+

+};

+

+

+class VideoTrack : public Track

+{

+    VideoTrack(const VideoTrack&);

+    VideoTrack& operator=(const VideoTrack&);

+

+public:

+    VideoTrack(Segment*, const Info&);

+    long long GetWidth() const;

+    long long GetHeight() const;

+    double GetFrameRate() const;

+

+    bool VetEntry(const BlockEntry*) const;

+

+private:

+    long long m_width;

+    long long m_height;

+    double m_rate;

+

+};

+

+

+class AudioTrack : public Track

+{

+    AudioTrack(const AudioTrack&);

+    AudioTrack& operator=(const AudioTrack&);

+

+public:

+    AudioTrack(Segment*, const Info&);

+    double GetSamplingRate() const;

+    long long GetChannels() const;

+    long long GetBitDepth() const;

+    bool VetEntry(const BlockEntry*) const;

+

+private:

+    double m_rate;

+    long long m_channels;

+    long long m_bitDepth;

+};

+

+

+class Tracks

+{

+    Tracks(const Tracks&);

+    Tracks& operator=(const Tracks&);

+

+public:

+    Segment* const m_pSegment;

+    const long long m_start;

+    const long long m_size;

+

+    Tracks(Segment*, long long start, long long size);

+    virtual ~Tracks();

+

+    Track* GetTrackByNumber(unsigned long tn) const;

+    Track* GetTrackByIndex(unsigned long idx) const;

+

+private:

+    Track** m_trackEntries;

+    Track** m_trackEntriesEnd;

+

+    void ParseTrackEntry(long long, long long, Track*&);

+

+public:

+    unsigned long GetTracksCount() const;

+};

+

+

+class SegmentInfo

+{

+    SegmentInfo(const SegmentInfo&);

+    SegmentInfo& operator=(const SegmentInfo&);

+

+public:

+    Segment* const m_pSegment;

+    const long long m_start;

+    const long long m_size;

+

+    SegmentInfo(Segment*, long long start, long long size);

+    ~SegmentInfo();

+    long long GetTimeCodeScale() const;

+    long long GetDuration() const;  //scaled

+    const char* GetMuxingAppAsUTF8() const;

+    const char* GetWritingAppAsUTF8() const;

+    const char* GetTitleAsUTF8() const;

+

+private:

+    long long m_timecodeScale;

+    double m_duration;

+    char* m_pMuxingAppAsUTF8;

+    char* m_pWritingAppAsUTF8;

+    char* m_pTitleAsUTF8;

+};

+

+class Cues;

+class CuePoint

+{

+    friend class Cues;

+

+    CuePoint(size_t, long long);

+    ~CuePoint();

+

+    CuePoint(const CuePoint&);

+    CuePoint& operator=(const CuePoint&);

+

+public:

+    void Load(IMkvReader*);

+

+    long long GetTimeCode() const;      //absolute but unscaled

+    long long GetTime(Segment*) const;  //absolute and scaled (ns units)

+

+    struct TrackPosition

+    {

+        long long m_track;

+        long long m_pos;  //of cluster

+        long long m_block;

+        //codec_state  //defaults to 0

+        //reference = clusters containing req'd referenced blocks

+        //  reftime = timecode of the referenced block

+

+        void Parse(IMkvReader*, long long, long long);

+    };

+

+    const TrackPosition* Find(const Track*) const;

+

+private:

+    const size_t m_index;

+    long long m_timecode;

+    TrackPosition* m_track_positions;

+    size_t m_track_positions_count;

+

+};

+

+

+class Cues

+{

+    friend class Segment;

+

+    Cues(Segment*, long long start, long long size);

+    ~Cues();

+

+    Cues(const Cues&);

+    Cues& operator=(const Cues&);

+

+public:

+    Segment* const m_pSegment;

+    const long long m_start;

+    const long long m_size;

+

+    bool Find(  //lower bound of time_ns

+        long long time_ns,

+        const Track*,

+        const CuePoint*&,

+        const CuePoint::TrackPosition*&) const;

+

+#if 0

+    bool FindNext(  //upper_bound of time_ns

+        long long time_ns,

+        const Track*,

+        const CuePoint*&,

+        const CuePoint::TrackPosition*&) const;

+#endif

+

+    const CuePoint* GetFirst() const;

+    const CuePoint* GetLast() const;

+

+    const CuePoint* GetNext(const CuePoint*) const;

+

+    const BlockEntry* GetBlock(

+                        const CuePoint*,

+                        const CuePoint::TrackPosition*) const;

+

+private:

+    void Init() const;

+    bool LoadCuePoint() const;

+    void PreloadCuePoint(size_t&, long long) const;

+

+    mutable CuePoint** m_cue_points;

+    mutable size_t m_count;

+    mutable size_t m_preload_count;

+    mutable long long m_pos;

+

+};

+

+

+class Cluster

+{

+    Cluster(const Cluster&);

+    Cluster& operator=(const Cluster&);

+

+public:

+    Segment* const m_pSegment;

+

+public:

+    static Cluster* Parse(Segment*, long, long long off);

+

+    Cluster();  //EndOfStream

+    ~Cluster();

+

+    bool EOS() const;

+

+    long long GetTimeCode();   //absolute, but not scaled

+    long long GetTime();       //absolute, and scaled (nanosecond units)

+    long long GetFirstTime();  //time (ns) of first (earliest) block

+    long long GetLastTime();   //time (ns) of last (latest) block

+

+    const BlockEntry* GetFirst();

+    const BlockEntry* GetLast();

+    const BlockEntry* GetNext(const BlockEntry*) const;

+    const BlockEntry* GetEntry(const Track*);

+    const BlockEntry* GetEntry(

+        const CuePoint&,

+        const CuePoint::TrackPosition&);

+    const BlockEntry* GetMaxKey(const VideoTrack*);

+

+protected:

+    Cluster(Segment*, long, long long off);

+

+public:

+    //TODO: these should all be private, with public selector functions

+    long m_index;

+    long long m_pos;

+    long long m_size;

+

+private:

+    long long m_timecode;

+    BlockEntry** m_entries;

+    size_t m_entriesCount;

+

+    void Load();

+    void LoadBlockEntries();

+    void ParseBlockGroup(long long, long long, size_t);

+    void ParseSimpleBlock(long long, long long, size_t);

+

+};

+

+

+class Segment

+{

+    friend class Cues;

+

+    Segment(const Segment&);

+    Segment& operator=(const Segment&);

+

+private:

+    Segment(IMkvReader*, long long pos, long long size);

+

+public:

+    IMkvReader* const m_pReader;

+    const long long m_start;  //posn of segment payload

+    const long long m_size;   //size of segment payload

+    Cluster m_eos;  //TODO: make private?

+

+    static long long CreateInstance(IMkvReader*, long long, Segment*&);

+    ~Segment();

+

+    long Load();  //loads headers and all clusters

+

+    //for incremental loading (splitter)

+    long long Unparsed() const;

+    long long ParseHeaders();  //stops when first cluster is found

+    long LoadCluster();        //loads one cluster

+

+#if 0

+    //This pair parses one cluster, but only changes the state of the

+    //segment object when the cluster is actually added to the index.

+    long ParseCluster(Cluster*&, long long& newpos) const;

+    bool AddCluster(Cluster*, long long);

+#endif

+

+    Tracks* GetTracks() const;

+    const SegmentInfo* GetInfo() const;

+    const Cues* GetCues() const;

+

+    long long GetDuration() const;

+

+    unsigned long GetCount() const;

+    Cluster* GetFirst();

+    Cluster* GetLast();

+    Cluster* GetNext(const Cluster*);

+

+    Cluster* FindCluster(long long time_nanoseconds);

+    const BlockEntry* Seek(long long time_nanoseconds, const Track*);

+

+private:

+

+    long long m_pos;  //absolute file posn; what has been consumed so far

+    SegmentInfo* m_pInfo;

+    Tracks* m_pTracks;

+    Cues* m_pCues;

+    Cluster** m_clusters;

+    long m_clusterCount;         //number of entries for which m_index >= 0

+    long m_clusterPreloadCount;  //number of entries for which m_index < 0

+    long m_clusterSize;          //array size

+

+    void AppendCluster(Cluster*);

+    void PreloadCluster(Cluster*, ptrdiff_t);

+

+    void ParseSeekHead(long long pos, long long size);

+    void ParseSeekEntry(long long pos, long long size);

+    void ParseCues(long long);

+

+    const BlockEntry* GetBlock(

+        const CuePoint&,

+        const CuePoint::TrackPosition&);

+

+};

+

+

+}  //end namespace mkvparser

+

+#endif  //MKVPARSER_HPP

diff --git a/opengl/tests/gl2_yuvtex/Android.mk b/opengl/tests/gl2_yuvtex/Android.mk
new file mode 100644
index 0000000..6304700
--- /dev/null
+++ b/opengl/tests/gl2_yuvtex/Android.mk
@@ -0,0 +1,19 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+	gl2_yuvtex.cpp
+
+LOCAL_SHARED_LIBRARIES := \
+	libcutils \
+    libEGL \
+    libGLESv2 \
+    libui
+
+LOCAL_MODULE:= test-opengl-gl2_yuvtex
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_CFLAGS := -DGL_GLEXT_PROTOTYPES -DEGL_EGLEXT_PROTOTYPES
+
+include $(BUILD_EXECUTABLE)
diff --git a/opengl/tests/gl2_yuvtex/gl2_yuvtex.cpp b/opengl/tests/gl2_yuvtex/gl2_yuvtex.cpp
new file mode 100644
index 0000000..602ea1a
--- /dev/null
+++ b/opengl/tests/gl2_yuvtex/gl2_yuvtex.cpp
@@ -0,0 +1,427 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <sched.h>
+#include <sys/resource.h>
+
+#include <EGL/egl.h>
+#include <EGL/eglext.h>
+#include <GLES2/gl2.h>
+#include <GLES2/gl2ext.h>
+
+#include <utils/Timers.h>
+
+#include <ui/FramebufferNativeWindow.h>
+#include <ui/GraphicBuffer.h>
+#include <ui/EGLUtils.h>
+
+using namespace android;
+
+static void printGLString(const char *name, GLenum s) {
+    // fprintf(stderr, "printGLString %s, %d\n", name, s);
+    const char *v = (const char *) glGetString(s);
+    // int error = glGetError();
+    // fprintf(stderr, "glGetError() = %d, result of glGetString = %x\n", error,
+    //        (unsigned int) v);
+    // if ((v < (const char*) 0) || (v > (const char*) 0x10000))
+    //    fprintf(stderr, "GL %s = %s\n", name, v);
+    // else
+    //    fprintf(stderr, "GL %s = (null) 0x%08x\n", name, (unsigned int) v);
+    fprintf(stderr, "GL %s = %s\n", name, v);
+}
+
+static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
+    if (returnVal != EGL_TRUE) {
+        fprintf(stderr, "%s() returned %d\n", op, returnVal);
+    }
+
+    for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
+            = eglGetError()) {
+        fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
+                error);
+    }
+}
+
+static void checkGlError(const char* op) {
+    for (GLint error = glGetError(); error; error
+            = glGetError()) {
+        fprintf(stderr, "after %s() glError (0x%x)\n", op, error);
+    }
+}
+
+static const char gVertexShader[] = "attribute vec4 vPosition;\n"
+    "varying vec2 yuvTexCoords;\n"
+    "void main() {\n"
+    "  yuvTexCoords = vPosition.xy + vec2(0.5, 0.5);\n"
+    "  gl_Position = vPosition;\n"
+    "}\n";
+
+static const char gFragmentShader[] = "#extension GL_OES_EGL_image_external : require\n"
+    "precision mediump float;\n"
+    "uniform samplerExternalOES yuvTexSampler;\n"
+    "varying vec2 yuvTexCoords;\n"
+    "void main() {\n"
+    "  gl_FragColor = texture2D(yuvTexSampler, yuvTexCoords);\n"
+    "}\n";
+
+GLuint loadShader(GLenum shaderType, const char* pSource) {
+    GLuint shader = glCreateShader(shaderType);
+    if (shader) {
+        glShaderSource(shader, 1, &pSource, NULL);
+        glCompileShader(shader);
+        GLint compiled = 0;
+        glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
+        if (!compiled) {
+            GLint infoLen = 0;
+            glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
+            if (infoLen) {
+                char* buf = (char*) malloc(infoLen);
+                if (buf) {
+                    glGetShaderInfoLog(shader, infoLen, NULL, buf);
+                    fprintf(stderr, "Could not compile shader %d:\n%s\n",
+                            shaderType, buf);
+                    free(buf);
+                }
+            } else {
+                fprintf(stderr, "Guessing at GL_INFO_LOG_LENGTH size\n");
+                char* buf = (char*) malloc(0x1000);
+                if (buf) {
+                    glGetShaderInfoLog(shader, 0x1000, NULL, buf);
+                    fprintf(stderr, "Could not compile shader %d:\n%s\n",
+                            shaderType, buf);
+                    free(buf);
+                }
+            }
+            glDeleteShader(shader);
+            shader = 0;
+        }
+    }
+    return shader;
+}
+
+GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) {
+    GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
+    if (!vertexShader) {
+        return 0;
+    }
+
+    GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
+    if (!pixelShader) {
+        return 0;
+    }
+
+    GLuint program = glCreateProgram();
+    if (program) {
+        glAttachShader(program, vertexShader);
+        checkGlError("glAttachShader");
+        glAttachShader(program, pixelShader);
+        checkGlError("glAttachShader");
+        glLinkProgram(program);
+        GLint linkStatus = GL_FALSE;
+        glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
+        if (linkStatus != GL_TRUE) {
+            GLint bufLength = 0;
+            glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
+            if (bufLength) {
+                char* buf = (char*) malloc(bufLength);
+                if (buf) {
+                    glGetProgramInfoLog(program, bufLength, NULL, buf);
+                    fprintf(stderr, "Could not link program:\n%s\n", buf);
+                    free(buf);
+                }
+            }
+            glDeleteProgram(program);
+            program = 0;
+        }
+    }
+    return program;
+}
+
+GLuint gProgram;
+GLint gvPositionHandle;
+GLint gYuvTexSamplerHandle;
+
+bool setupGraphics(int w, int h) {
+    gProgram = createProgram(gVertexShader, gFragmentShader);
+    if (!gProgram) {
+        return false;
+    }
+    gvPositionHandle = glGetAttribLocation(gProgram, "vPosition");
+    checkGlError("glGetAttribLocation");
+    fprintf(stderr, "glGetAttribLocation(\"vPosition\") = %d\n",
+            gvPositionHandle);
+    gYuvTexSamplerHandle = glGetUniformLocation(gProgram, "yuvTexSampler");
+    checkGlError("glGetUniformLocation");
+    fprintf(stderr, "glGetUniformLocation(\"yuvTexSampler\") = %d\n",
+            gYuvTexSamplerHandle);
+
+    glViewport(0, 0, w, h);
+    checkGlError("glViewport");
+    return true;
+}
+
+int align(int x, int a) {
+    return (x + (a-1)) & (~(a-1));
+}
+
+const int yuvTexWidth = 608;
+const int yuvTexHeight = 480;
+const int yuvTexUsage = GraphicBuffer::USAGE_HW_TEXTURE |
+        GraphicBuffer::USAGE_SW_WRITE_RARELY;
+const int yuvTexFormat = HAL_PIXEL_FORMAT_YV12;
+const int yuvTexOffsetY = 0;
+const int yuvTexStrideY = (yuvTexWidth + 0xf) & ~0xf;
+const int yuvTexOffsetV = yuvTexStrideY * yuvTexHeight;
+const int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf; 
+const int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * yuvTexHeight/2;
+const int yuvTexStrideU = yuvTexStrideV;
+const bool yuvTexSameUV = false;
+static sp<GraphicBuffer> yuvTexBuffer;
+static GLuint yuvTex;
+
+bool setupYuvTexSurface(EGLDisplay dpy, EGLContext context) {
+    int blockWidth = yuvTexWidth > 16 ? yuvTexWidth / 16 : 1;
+    int blockHeight = yuvTexHeight > 16 ? yuvTexHeight / 16 : 1;
+    yuvTexBuffer = new GraphicBuffer(yuvTexWidth, yuvTexHeight, yuvTexFormat,
+            yuvTexUsage);
+    char* buf = NULL;
+    status_t err = yuvTexBuffer->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&buf));
+    if (err != 0) {
+        fprintf(stderr, "yuvTexBuffer->lock(...) failed: %d\n", err);
+        return false;
+    }
+    for (int x = 0; x < yuvTexWidth; x++) {
+        for (int y = 0; y < yuvTexHeight; y++) {
+            int parityX = (x / blockWidth) & 1;
+            int parityY = (y / blockHeight) & 1;
+            unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
+            buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
+            if (x < yuvTexWidth / 2 && y < yuvTexHeight / 2) {
+                buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
+                if (yuvTexSameUV) {
+                    buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] = intensity;
+                } else if (x < yuvTexWidth / 4 && y < yuvTexHeight / 4) {
+                    buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
+                    buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
+                    buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
+                    buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] = intensity;
+                }
+            }
+        }
+    }
+
+    err = yuvTexBuffer->unlock();
+    if (err != 0) {
+        fprintf(stderr, "yuvTexBuffer->unlock() failed: %d\n", err);
+        return false;
+    }
+
+    EGLClientBuffer clientBuffer = (EGLClientBuffer)yuvTexBuffer->getNativeBuffer();
+    EGLImageKHR img = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
+            clientBuffer, 0);
+    checkEglError("eglCreateImageKHR");
+    if (img == EGL_NO_IMAGE_KHR) {
+        return false;
+    }
+
+    glGenTextures(1, &yuvTex);
+    checkGlError("glGenTextures");
+    glBindTexture(GL_TEXTURE_EXTERNAL_OES, yuvTex);
+    checkGlError("glBindTexture");
+    glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)img);
+    checkGlError("glEGLImageTargetTexture2DOES");
+
+    return true;
+}
+
+const GLfloat gTriangleVertices[] = {
+    -0.5f, 0.5f,
+    -0.5f, -0.5f,
+    0.5f, -0.5f,
+    0.5f, 0.5f,
+};
+
+void renderFrame() {
+    glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
+    checkGlError("glClearColor");
+    glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
+    checkGlError("glClear");
+
+    glUseProgram(gProgram);
+    checkGlError("glUseProgram");
+
+    glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices);
+    checkGlError("glVertexAttribPointer");
+    glEnableVertexAttribArray(gvPositionHandle);
+    checkGlError("glEnableVertexAttribArray");
+
+    glUniform1i(gYuvTexSamplerHandle, 0);
+    checkGlError("glUniform1i");
+    glBindTexture(GL_TEXTURE_EXTERNAL_OES, yuvTex);
+    checkGlError("glBindTexture");
+
+    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+    checkGlError("glDrawArrays");
+}
+
+void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) {
+
+#define X(VAL) {VAL, #VAL}
+    struct {EGLint attribute; const char* name;} names[] = {
+    X(EGL_BUFFER_SIZE),
+    X(EGL_ALPHA_SIZE),
+    X(EGL_BLUE_SIZE),
+    X(EGL_GREEN_SIZE),
+    X(EGL_RED_SIZE),
+    X(EGL_DEPTH_SIZE),
+    X(EGL_STENCIL_SIZE),
+    X(EGL_CONFIG_CAVEAT),
+    X(EGL_CONFIG_ID),
+    X(EGL_LEVEL),
+    X(EGL_MAX_PBUFFER_HEIGHT),
+    X(EGL_MAX_PBUFFER_PIXELS),
+    X(EGL_MAX_PBUFFER_WIDTH),
+    X(EGL_NATIVE_RENDERABLE),
+    X(EGL_NATIVE_VISUAL_ID),
+    X(EGL_NATIVE_VISUAL_TYPE),
+    X(EGL_SAMPLES),
+    X(EGL_SAMPLE_BUFFERS),
+    X(EGL_SURFACE_TYPE),
+    X(EGL_TRANSPARENT_TYPE),
+    X(EGL_TRANSPARENT_RED_VALUE),
+    X(EGL_TRANSPARENT_GREEN_VALUE),
+    X(EGL_TRANSPARENT_BLUE_VALUE),
+    X(EGL_BIND_TO_TEXTURE_RGB),
+    X(EGL_BIND_TO_TEXTURE_RGBA),
+    X(EGL_MIN_SWAP_INTERVAL),
+    X(EGL_MAX_SWAP_INTERVAL),
+    X(EGL_LUMINANCE_SIZE),
+    X(EGL_ALPHA_MASK_SIZE),
+    X(EGL_COLOR_BUFFER_TYPE),
+    X(EGL_RENDERABLE_TYPE),
+    X(EGL_CONFORMANT),
+   };
+#undef X
+
+    for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
+        EGLint value = -1;
+        EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
+        EGLint error = eglGetError();
+        if (returnVal && error == EGL_SUCCESS) {
+            printf(" %s: ", names[j].name);
+            printf("%d (0x%x)", value, value);
+        }
+    }
+    printf("\n");
+}
+
+int main(int argc, char** argv) {
+    EGLBoolean returnValue;
+    EGLConfig myConfig = {0};
+
+    EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
+    EGLint s_configAttribs[] = {
+            EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
+            EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
+            EGL_NONE };
+    EGLint majorVersion;
+    EGLint minorVersion;
+    EGLContext context;
+    EGLSurface surface;
+    EGLint w, h;
+
+    EGLDisplay dpy;
+
+    checkEglError("<init>");
+    dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+    checkEglError("eglGetDisplay");
+    if (dpy == EGL_NO_DISPLAY) {
+        printf("eglGetDisplay returned EGL_NO_DISPLAY.\n");
+        return 0;
+    }
+
+    returnValue = eglInitialize(dpy, &majorVersion, &minorVersion);
+    checkEglError("eglInitialize", returnValue);
+    fprintf(stderr, "EGL version %d.%d\n", majorVersion, minorVersion);
+    if (returnValue != EGL_TRUE) {
+        printf("eglInitialize failed\n");
+        return 0;
+    }
+
+    EGLNativeWindowType window = android_createDisplaySurface();
+    returnValue = EGLUtils::selectConfigForNativeWindow(dpy, s_configAttribs, window, &myConfig);
+    if (returnValue) {
+        printf("EGLUtils::selectConfigForNativeWindow() returned %d", returnValue);
+        return 1;
+    }
+
+    checkEglError("EGLUtils::selectConfigForNativeWindow");
+
+    printf("Chose this configuration:\n");
+    printEGLConfiguration(dpy, myConfig);
+
+    surface = eglCreateWindowSurface(dpy, myConfig, window, NULL);
+    checkEglError("eglCreateWindowSurface");
+    if (surface == EGL_NO_SURFACE) {
+        printf("gelCreateWindowSurface failed.\n");
+        return 1;
+    }
+
+    context = eglCreateContext(dpy, myConfig, EGL_NO_CONTEXT, context_attribs);
+    checkEglError("eglCreateContext");
+    if (context == EGL_NO_CONTEXT) {
+        printf("eglCreateContext failed\n");
+        return 1;
+    }
+    returnValue = eglMakeCurrent(dpy, surface, surface, context);
+    checkEglError("eglMakeCurrent", returnValue);
+    if (returnValue != EGL_TRUE) {
+        return 1;
+    }
+    eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
+    checkEglError("eglQuerySurface");
+    eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
+    checkEglError("eglQuerySurface");
+    GLint dim = w < h ? w : h;
+
+    fprintf(stderr, "Window dimensions: %d x %d\n", w, h);
+
+    printGLString("Version", GL_VERSION);
+    printGLString("Vendor", GL_VENDOR);
+    printGLString("Renderer", GL_RENDERER);
+    printGLString("Extensions", GL_EXTENSIONS);
+
+    if(!setupYuvTexSurface(dpy, context)) {
+        fprintf(stderr, "Could not set up texture surface.\n");
+        return 1;
+    }
+
+    if(!setupGraphics(w, h)) {
+        fprintf(stderr, "Could not set up graphics.\n");
+        return 1;
+    }
+
+    for (;;) {
+        renderFrame();
+        eglSwapBuffers(dpy, surface);
+        checkEglError("eglSwapBuffers");
+    }
+
+    return 0;
+}
diff --git a/opengl/tests/gl_yuvtex/Android.mk b/opengl/tests/gl_yuvtex/Android.mk
new file mode 100644
index 0000000..a78db25
--- /dev/null
+++ b/opengl/tests/gl_yuvtex/Android.mk
@@ -0,0 +1,19 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+	gl_yuvtex.cpp
+
+LOCAL_SHARED_LIBRARIES := \
+	libcutils \
+    libEGL \
+    libGLESv1_CM \
+    libui
+
+LOCAL_MODULE:= test-opengl-gl_yuvtex
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_CFLAGS := -DGL_GLEXT_PROTOTYPES -DEGL_EGLEXT_PROTOTYPES
+
+include $(BUILD_EXECUTABLE)
diff --git a/opengl/tests/gl_yuvtex/gl_yuvtex.cpp b/opengl/tests/gl_yuvtex/gl_yuvtex.cpp
new file mode 100644
index 0000000..fbe65f1
--- /dev/null
+++ b/opengl/tests/gl_yuvtex/gl_yuvtex.cpp
@@ -0,0 +1,333 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <sched.h>
+#include <sys/resource.h>
+
+#include <EGL/egl.h>
+#include <EGL/eglext.h>
+#include <GLES/gl.h>
+#include <GLES/glext.h>
+
+#include <utils/Timers.h>
+
+#include <ui/FramebufferNativeWindow.h>
+#include <ui/GraphicBuffer.h>
+#include <ui/EGLUtils.h>
+
+using namespace android;
+
+static void printGLString(const char *name, GLenum s) {
+    // fprintf(stderr, "printGLString %s, %d\n", name, s);
+    const char *v = (const char *) glGetString(s);
+    // int error = glGetError();
+    // fprintf(stderr, "glGetError() = %d, result of glGetString = %x\n", error,
+    //        (unsigned int) v);
+    // if ((v < (const char*) 0) || (v > (const char*) 0x10000))
+    //    fprintf(stderr, "GL %s = %s\n", name, v);
+    // else
+    //    fprintf(stderr, "GL %s = (null) 0x%08x\n", name, (unsigned int) v);
+    fprintf(stderr, "GL %s = %s\n", name, v);
+}
+
+static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
+    if (returnVal != EGL_TRUE) {
+        fprintf(stderr, "%s() returned %d\n", op, returnVal);
+    }
+
+    for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
+            = eglGetError()) {
+        fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
+                error);
+    }
+}
+
+static void checkGlError(const char* op) {
+    for (GLint error = glGetError(); error; error
+            = glGetError()) {
+        fprintf(stderr, "after %s() glError (0x%x)\n", op, error);
+    }
+}
+
+bool setupGraphics(int w, int h) {
+    glViewport(0, 0, w, h);
+    checkGlError("glViewport");
+    return true;
+}
+
+int align(int x, int a) {
+    return (x + (a-1)) & (~(a-1));
+}
+
+const int yuvTexWidth = 600;
+const int yuvTexHeight = 480;
+const int yuvTexUsage = GraphicBuffer::USAGE_HW_TEXTURE |
+        GraphicBuffer::USAGE_SW_WRITE_RARELY;
+const int yuvTexFormat = HAL_PIXEL_FORMAT_YV12;
+const int yuvTexOffsetY = 0;
+const int yuvTexStrideY = (yuvTexWidth + 0xf) & ~0xf;
+const int yuvTexOffsetV = yuvTexStrideY * yuvTexHeight;
+const int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf; 
+const int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * yuvTexHeight/2;
+const int yuvTexStrideU = yuvTexStrideV;
+const bool yuvTexSameUV = false;
+static sp<GraphicBuffer> yuvTexBuffer;
+static GLuint yuvTex;
+
+bool setupYuvTexSurface(EGLDisplay dpy, EGLContext context) {
+    int blockWidth = yuvTexWidth > 16 ? yuvTexWidth / 16 : 1;
+    int blockHeight = yuvTexHeight > 16 ? yuvTexHeight / 16 : 1;
+    yuvTexBuffer = new GraphicBuffer(yuvTexWidth, yuvTexHeight, yuvTexFormat,
+            yuvTexUsage);
+    char* buf = NULL;
+    status_t err = yuvTexBuffer->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&buf));
+    if (err != 0) {
+        fprintf(stderr, "yuvTexBuffer->lock(...) failed: %d\n", err);
+        return false;
+    }
+    for (int x = 0; x < yuvTexWidth; x++) {
+        for (int y = 0; y < yuvTexHeight; y++) {
+            int parityX = (x / blockWidth) & 1;
+            int parityY = (y / blockHeight) & 1;
+            unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
+            buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
+            if (x < yuvTexWidth / 2 && y < yuvTexHeight / 2) {
+                buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
+                if (yuvTexSameUV) {
+                    buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] = intensity;
+                } else if (x < yuvTexWidth / 4 && y < yuvTexHeight / 4) {
+                    buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
+                    buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
+                    buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
+                    buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] = intensity;
+                }
+            }
+        }
+    }
+
+    err = yuvTexBuffer->unlock();
+    if (err != 0) {
+        fprintf(stderr, "yuvTexBuffer->unlock() failed: %d\n", err);
+        return false;
+    }
+
+    EGLClientBuffer clientBuffer = (EGLClientBuffer)yuvTexBuffer->getNativeBuffer();
+    EGLImageKHR img = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
+            clientBuffer, 0);
+    checkEglError("eglCreateImageKHR");
+    if (img == EGL_NO_IMAGE_KHR) {
+        return false;
+    }
+
+    glGenTextures(1, &yuvTex);
+    checkGlError("glGenTextures");
+    glBindTexture(GL_TEXTURE_EXTERNAL_OES, yuvTex);
+    checkGlError("glBindTexture");
+    glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)img);
+    checkGlError("glEGLImageTargetTexture2DOES");
+    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+    checkGlError("glTexParameteri");
+    glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+    checkGlError("glTexParameteri");
+    glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
+    checkGlError("glTexEnvx");
+
+    GLint crop[4] = { 0, 0, yuvTexWidth, yuvTexHeight };
+    glTexParameteriv(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_CROP_RECT_OES, crop);
+    checkGlError("glTexParameteriv");
+
+    return true;
+}
+
+void renderFrame(int w, int h) {
+    glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
+    checkGlError("glClearColor");
+    glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
+    checkGlError("glClear");
+    
+    glBindTexture(GL_TEXTURE_EXTERNAL_OES, yuvTex);
+    checkGlError("glBindTexture");
+    glEnable(GL_TEXTURE_EXTERNAL_OES);
+    checkGlError("glEnable");
+
+    glDrawTexiOES(0, 0, 0, w, h);
+    checkGlError("glDrawTexiOES");
+}
+
+void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) {
+
+#define X(VAL) {VAL, #VAL}
+    struct {EGLint attribute; const char* name;} names[] = {
+    X(EGL_BUFFER_SIZE),
+    X(EGL_ALPHA_SIZE),
+    X(EGL_BLUE_SIZE),
+    X(EGL_GREEN_SIZE),
+    X(EGL_RED_SIZE),
+    X(EGL_DEPTH_SIZE),
+    X(EGL_STENCIL_SIZE),
+    X(EGL_CONFIG_CAVEAT),
+    X(EGL_CONFIG_ID),
+    X(EGL_LEVEL),
+    X(EGL_MAX_PBUFFER_HEIGHT),
+    X(EGL_MAX_PBUFFER_PIXELS),
+    X(EGL_MAX_PBUFFER_WIDTH),
+    X(EGL_NATIVE_RENDERABLE),
+    X(EGL_NATIVE_VISUAL_ID),
+    X(EGL_NATIVE_VISUAL_TYPE),
+    X(EGL_SAMPLES),
+    X(EGL_SAMPLE_BUFFERS),
+    X(EGL_SURFACE_TYPE),
+    X(EGL_TRANSPARENT_TYPE),
+    X(EGL_TRANSPARENT_RED_VALUE),
+    X(EGL_TRANSPARENT_GREEN_VALUE),
+    X(EGL_TRANSPARENT_BLUE_VALUE),
+    X(EGL_BIND_TO_TEXTURE_RGB),
+    X(EGL_BIND_TO_TEXTURE_RGBA),
+    X(EGL_MIN_SWAP_INTERVAL),
+    X(EGL_MAX_SWAP_INTERVAL),
+    X(EGL_LUMINANCE_SIZE),
+    X(EGL_ALPHA_MASK_SIZE),
+    X(EGL_COLOR_BUFFER_TYPE),
+    X(EGL_RENDERABLE_TYPE),
+    X(EGL_CONFORMANT),
+   };
+#undef X
+
+    for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
+        EGLint value = -1;
+        EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
+        EGLint error = eglGetError();
+        if (returnVal && error == EGL_SUCCESS) {
+            printf(" %s: ", names[j].name);
+            printf("%d (0x%x)", value, value);
+        }
+    }
+    printf("\n");
+}
+
+int main(int argc, char** argv) {
+    EGLBoolean returnValue;
+    EGLConfig myConfig = {0};
+
+    EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE };
+    EGLint s_configAttribs[] = {
+            EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
+            EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
+            EGL_NONE };
+    EGLint majorVersion;
+    EGLint minorVersion;
+    EGLContext context;
+    EGLSurface surface;
+    EGLint w, h;
+
+    EGLDisplay dpy;
+
+    checkEglError("<init>");
+    dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+    checkEglError("eglGetDisplay");
+    if (dpy == EGL_NO_DISPLAY) {
+        printf("eglGetDisplay returned EGL_NO_DISPLAY.\n");
+        return 0;
+    }
+
+    returnValue = eglInitialize(dpy, &majorVersion, &minorVersion);
+    checkEglError("eglInitialize", returnValue);
+    fprintf(stderr, "EGL version %d.%d\n", majorVersion, minorVersion);
+    if (returnValue != EGL_TRUE) {
+        printf("eglInitialize failed\n");
+        return 0;
+    }
+
+    EGLNativeWindowType window = android_createDisplaySurface();
+    returnValue = EGLUtils::selectConfigForNativeWindow(dpy, s_configAttribs, window, &myConfig);
+    if (returnValue) {
+        printf("EGLUtils::selectConfigForNativeWindow() returned %d", returnValue);
+        return 1;
+    }
+
+    checkEglError("EGLUtils::selectConfigForNativeWindow");
+
+    printf("Chose this configuration:\n");
+    printEGLConfiguration(dpy, myConfig);
+
+    surface = eglCreateWindowSurface(dpy, myConfig, window, NULL);
+    checkEglError("eglCreateWindowSurface");
+    if (surface == EGL_NO_SURFACE) {
+        printf("gelCreateWindowSurface failed.\n");
+        return 1;
+    }
+
+    context = eglCreateContext(dpy, myConfig, EGL_NO_CONTEXT, context_attribs);
+    checkEglError("eglCreateContext");
+    if (context == EGL_NO_CONTEXT) {
+        printf("eglCreateContext failed\n");
+        return 1;
+    }
+    returnValue = eglMakeCurrent(dpy, surface, surface, context);
+    checkEglError("eglMakeCurrent", returnValue);
+    if (returnValue != EGL_TRUE) {
+        return 1;
+    }
+    eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
+    checkEglError("eglQuerySurface");
+    eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
+    checkEglError("eglQuerySurface");
+    GLint dim = w < h ? w : h;
+
+    fprintf(stderr, "Window dimensions: %d x %d\n", w, h);
+
+    printGLString("Version", GL_VERSION);
+    printGLString("Vendor", GL_VENDOR);
+    printGLString("Renderer", GL_RENDERER);
+    printGLString("Extensions", GL_EXTENSIONS);
+
+    if(!setupYuvTexSurface(dpy, context)) {
+        fprintf(stderr, "Could not set up texture surface.\n");
+        return 1;
+    }
+
+    if(!setupGraphics(w, h)) {
+        fprintf(stderr, "Could not set up graphics.\n");
+        return 1;
+    }
+
+    for (;;) {
+        static int dir = -1;
+
+        renderFrame(w, h);
+        eglSwapBuffers(dpy, surface);
+        checkEglError("eglSwapBuffers");
+
+        if (w <= 10 || h <= 10)
+        {
+            dir = -dir;
+        }
+
+        if (w >= 1300 || h >= 900)
+        {
+            dir = -dir;
+        }
+
+
+        w += dir;
+        h += dir;
+    }
+
+    return 0;
+}
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
index b4c696b..cd1ad4c 100755
--- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -256,8 +256,10 @@
         }
     };
     
-    // The current size of the screen.
-    int mW, mH;
+    // The current size of the screen; these may be different than (0,0)-(dw,dh)
+    // if the status bar can't be hidden; in that case it effectively carves out
+    // that area of the display from all other windows.
+    int mScreenLeft, mScreenTop, mScreenWidth, mScreenHeight;
     // During layout, the current screen borders with all outer decoration
     // (status bar, input method dock) accounted for.
     int mCurLeft, mCurTop, mCurRight, mCurBottom;
@@ -1316,10 +1318,11 @@
     public void getContentInsetHintLw(WindowManager.LayoutParams attrs, Rect contentInset) {
         final int fl = attrs.flags;
         
-        if (mStatusBarCanHide && (fl &
-                (FLAG_LAYOUT_IN_SCREEN | FLAG_FULLSCREEN | FLAG_LAYOUT_INSET_DECOR))
+        if ((fl & (FLAG_LAYOUT_IN_SCREEN | FLAG_FULLSCREEN | FLAG_LAYOUT_INSET_DECOR))
                 == (FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR)) {
-            contentInset.set(mCurLeft, mCurTop, mW - mCurRight, mH - mCurBottom);
+            contentInset.set(mCurLeft, mCurTop,
+                    (mScreenLeft+mScreenWidth) - mCurRight,
+                    (mScreenTop+mScreenHeight) - mCurBottom);
         } else {
             contentInset.setEmpty();
         }
@@ -1327,8 +1330,9 @@
     
     /** {@inheritDoc} */
     public void beginLayoutLw(int displayWidth, int displayHeight) {
-        mW = displayWidth;
-        mH = displayHeight;
+        mScreenLeft = mScreenTop = 0;
+        mScreenWidth = displayWidth;
+        mScreenHeight = displayHeight;
         mDockLeft = mContentLeft = mCurLeft = 0;
         mDockTop = mContentTop = mCurTop = 0;
         mDockRight = mContentRight = mCurRight = displayWidth;
@@ -1350,16 +1354,34 @@
                 // If the status bar is hidden, we don't want to cause
                 // windows behind it to scroll.
                 final Rect r = mStatusBar.getFrameLw();
-                if (mDockTop == r.top) mDockTop = r.bottom;
-                else if (mDockBottom == r.bottom) mDockBottom = r.top;
-                mContentTop = mCurTop = mDockTop;
-                mContentBottom = mCurBottom = mDockBottom;
-                if (DEBUG_LAYOUT) Log.v(TAG, "Status bar: mDockTop=" + mDockTop
-                        + " mContentTop=" + mContentTop
-                        + " mCurTop=" + mCurTop
-                        + " mDockBottom=" + mDockBottom
-                        + " mContentBottom=" + mContentBottom
-                        + " mCurBottom=" + mCurBottom);
+                if (mStatusBarCanHide) {
+                    // Status bar may go away, so the screen area it occupies
+                    // is available to apps but just covering them when the
+                    // status bar is visible.
+                    if (mDockTop == r.top) mDockTop = r.bottom;
+                    else if (mDockBottom == r.bottom) mDockBottom = r.top;
+                    mContentTop = mCurTop = mDockTop;
+                    mContentBottom = mCurBottom = mDockBottom;
+                    if (DEBUG_LAYOUT) Log.v(TAG, "Status bar: mDockTop=" + mDockTop
+                            + " mContentTop=" + mContentTop
+                            + " mCurTop=" + mCurTop
+                            + " mDockBottom=" + mDockBottom
+                            + " mContentBottom=" + mContentBottom
+                            + " mCurBottom=" + mCurBottom);
+                } else {
+                    // Status bar can't go away; the part of the screen it
+                    // covers does not exist for anything behind it.
+                    if (mScreenTop == r.top) {
+                        mScreenTop = r.bottom;
+                        mScreenHeight -= (r.bottom-r.top);
+                    } else if ((mScreenHeight-mScreenTop) == r.bottom) {
+                        mScreenHeight -= (r.bottom-r.top);
+                    }
+                    mContentTop = mCurTop = mDockTop = mScreenTop;
+                    mContentBottom = mCurBottom = mDockBottom = mScreenTop+mScreenHeight;
+                    if (DEBUG_LAYOUT) Log.v(TAG, "Status bar: mScreenTop=" + mScreenTop
+                            + " mScreenHeight=" + mScreenHeight);
+                }
             }
         }
     }
@@ -1444,8 +1466,7 @@
             attrs.gravity = Gravity.BOTTOM;
             mDockLayer = win.getSurfaceLayer();
         } else {
-            if (mStatusBarCanHide && (fl &
-                    (FLAG_LAYOUT_IN_SCREEN | FLAG_FULLSCREEN | FLAG_LAYOUT_INSET_DECOR))
+            if ((fl & (FLAG_LAYOUT_IN_SCREEN | FLAG_FULLSCREEN | FLAG_LAYOUT_INSET_DECOR))
                     == (FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR)) {
                 // This is the case for a normal activity window: we want it
                 // to cover all of the screen space, and it can take care of
@@ -1456,10 +1477,10 @@
                     // frame is the same as the one we are attached to.
                     setAttachedWindowFrames(win, fl, sim, attached, true, pf, df, cf, vf);
                 } else {
-                    pf.left = df.left = 0;
-                    pf.top = df.top = 0;
-                    pf.right = df.right = mW;
-                    pf.bottom = df.bottom = mH;
+                    pf.left = df.left = mScreenLeft;
+                    pf.top = df.top = mScreenTop;
+                    pf.right = df.right = mScreenLeft+mScreenWidth;
+                    pf.bottom = df.bottom = mScreenTop+mScreenHeight;
                     if ((sim & SOFT_INPUT_MASK_ADJUST) != SOFT_INPUT_ADJUST_RESIZE) {
                         cf.left = mDockLeft;
                         cf.top = mDockTop;
@@ -1476,13 +1497,13 @@
                     vf.right = mCurRight;
                     vf.bottom = mCurBottom;
                 }
-            } else if (mStatusBarCanHide && (fl & FLAG_LAYOUT_IN_SCREEN) != 0) {
+            } else if ((fl & FLAG_LAYOUT_IN_SCREEN) != 0) {
                 // A window that has requested to fill the entire screen just
                 // gets everything, period.
-                pf.left = df.left = cf.left = 0;
-                pf.top = df.top = cf.top = 0;
-                pf.right = df.right = cf.right = mW;
-                pf.bottom = df.bottom = cf.bottom = mH;
+                pf.left = df.left = cf.left = mScreenLeft;
+                pf.top = df.top = cf.top = mScreenTop;
+                pf.right = df.right = cf.right = mScreenLeft+mScreenWidth;
+                pf.bottom = df.bottom = cf.bottom = mScreenTop+mScreenHeight;
                 vf.left = mCurLeft;
                 vf.top = mCurTop;
                 vf.right = mCurRight;
@@ -1743,9 +1764,9 @@
                     rect.union(w.getShownFrameLw());
                 }
             }
-            final int insetw = mW/10;
-            final int inseth = mH/10;
-            if (rect.contains(insetw, inseth, mW-insetw, mH-inseth)) {
+            final int insetw = mScreenWidth/10;
+            final int inseth = mScreenHeight/10;
+            if (rect.contains(insetw, inseth, mScreenWidth-insetw, mScreenHeight-inseth)) {
                 // All of the status bar windows put together cover the
                 // screen, so the app can't be seen.  (Note this test doesn't
                 // work if the rects of these windows are at off offsets or
diff --git a/services/java/com/android/server/PowerManagerService.java b/services/java/com/android/server/PowerManagerService.java
index 30e90a8..5cf013f 100644
--- a/services/java/com/android/server/PowerManagerService.java
+++ b/services/java/com/android/server/PowerManagerService.java
@@ -219,6 +219,8 @@
     private float mLightSensorValue = -1;
     private boolean mProxIgnoredBecauseScreenTurnedOff = false;
     private int mHighestLightSensorValue = -1;
+    private boolean mLightSensorPendingDecrease = false;
+    private boolean mLightSensorPendingIncrease = false;
     private float mLightSensorPendingValue = -1;
     private int mLightSensorScreenBrightness = -1;
     private int mLightSensorButtonBrightness = -1;
@@ -1141,6 +1143,8 @@
             pw.println("  mLightSensorEnabled=" + mLightSensorEnabled);
             pw.println("  mLightSensorValue=" + mLightSensorValue
                     + " mLightSensorPendingValue=" + mLightSensorPendingValue);
+            pw.println("  mLightSensorPendingDecrease=" + mLightSensorPendingDecrease
+                    + " mLightSensorPendingIncrease=" + mLightSensorPendingIncrease);
             pw.println("  mLightSensorScreenBrightness=" + mLightSensorScreenBrightness
                     + " mLightSensorButtonBrightness=" + mLightSensorButtonBrightness
                     + " mLightSensorKeyboardBrightness=" + mLightSensorKeyboardBrightness);
@@ -1742,6 +1746,8 @@
                 } else {
                     // cancel light sensor task
                     mHandler.removeCallbacks(mAutoBrightnessTask);
+                    mLightSensorPendingDecrease = false;
+                    mLightSensorPendingIncrease = false;
                     mScreenOffTime = SystemClock.elapsedRealtime();
                     long identity = Binder.clearCallingIdentity();
                     try {
@@ -2325,9 +2331,10 @@
     private Runnable mAutoBrightnessTask = new Runnable() {
         public void run() {
             synchronized (mLocks) {
-                int value = (int)mLightSensorPendingValue;
-                if (value >= 0) {
-                    mLightSensorPendingValue = -1;
+                if (mLightSensorPendingDecrease || mLightSensorPendingIncrease) {
+                    int value = (int)mLightSensorPendingValue;
+                    mLightSensorPendingDecrease = false;
+                    mLightSensorPendingIncrease = false;
                     lightSensorChangedLocked(value);
                 }
             }
@@ -2981,19 +2988,29 @@
                 if (mDebugLightSensor) {
                     Slog.d(TAG, "onSensorChanged: light value: " + value);
                 }
-                mHandler.removeCallbacks(mAutoBrightnessTask);
-                if (mLightSensorValue != value) {
-                    if (mLightSensorValue == -1 ||
-                            milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) {
-                        // process the value immediately if screen has just turned on
-                        lightSensorChangedLocked(value);
-                    } else {
-                        // delay processing to debounce the sensor
-                        mLightSensorPendingValue = value;
-                        mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY);
-                    }
+                if (mLightSensorValue == -1 ||
+                        milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) {
+                    // process the value immediately if screen has just turned on
+                    mHandler.removeCallbacks(mAutoBrightnessTask);
+                    mLightSensorPendingDecrease = false;
+                    mLightSensorPendingIncrease = false;
+                    lightSensorChangedLocked(value);
                 } else {
-                    mLightSensorPendingValue = -1;
+                    if ((value > mLightSensorValue && mLightSensorPendingDecrease) ||
+                            (value < mLightSensorValue && mLightSensorPendingIncrease) ||
+                            (value == mLightSensorValue) ||
+                            (!mLightSensorPendingDecrease && !mLightSensorPendingIncrease)) {
+                        // delay processing to debounce the sensor
+                        mHandler.removeCallbacks(mAutoBrightnessTask);
+                        mLightSensorPendingDecrease = (value < mLightSensorValue);
+                        mLightSensorPendingIncrease = (value > mLightSensorValue);
+                        if (mLightSensorPendingDecrease || mLightSensorPendingIncrease) {
+                            mLightSensorPendingValue = value;
+                            mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY);
+                        }
+                    } else {
+                        mLightSensorPendingValue = value;
+                    }
                 }
             }
         }
diff --git a/services/jni/com_android_server_location_GpsLocationProvider.cpp b/services/jni/com_android_server_location_GpsLocationProvider.cpp
index f5e17f5..a75e41d 100755
--- a/services/jni/com_android_server_location_GpsLocationProvider.cpp
+++ b/services/jni/com_android_server_location_GpsLocationProvider.cpp
@@ -199,8 +199,6 @@
 
 static void agps_request_set_id(uint32_t flags)
 {
-    LOGD("agps_request_set_id: flags (%d)", flags);
-
     JNIEnv* env = AndroidRuntime::getJNIEnv();
     env->CallVoidMethod(mCallbacksObj, method_requestSetID, flags);
     checkAndClearExceptionFromCallback(env, __FUNCTION__);
@@ -208,8 +206,6 @@
 
 static void agps_request_ref_location(uint32_t flags)
 {
-    LOGD("agps_ref_location: flags (%d)", flags);
-
     JNIEnv* env = AndroidRuntime::getJNIEnv();
     env->CallVoidMethod(mCallbacksObj, method_requestRefLocation, flags);
     checkAndClearExceptionFromCallback(env, __FUNCTION__);
diff --git a/tools/layoutlib/bridge/.classpath b/tools/layoutlib/bridge/.classpath
index d3bcb6c..aeeffa6 100644
--- a/tools/layoutlib/bridge/.classpath
+++ b/tools/layoutlib/bridge/.classpath
@@ -6,7 +6,7 @@
 	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/>
 	<classpathentry kind="var" path="ANDROID_SRC/prebuilt/common/layoutlib_api/layoutlib_api-prebuilt.jar"/>
 	<classpathentry kind="var" path="ANDROID_SRC/prebuilt/common/kxml2/kxml2-2.3.0.jar" sourcepath="/ANDROID_SRC/dalvik/libcore/xml/src/main/java"/>
-	<classpathentry kind="var" path="ANDROID_PLAT_OUT_FRAMEWORK/layoutlib.jar" sourcepath="/ANDROID_PLAT_SRC/frameworks/base"/>
 	<classpathentry kind="var" path="ANDROID_OUT_FRAMEWORK/ninepatch.jar" sourcepath="/ANDROID_SRC/development/tools/ninepatch/src"/>
+	<classpathentry kind="var" path="ANDROID_PLAT_SRC/out/host/common/obj/JAVA_LIBRARIES/temp_layoutlib_intermediates/javalib.jar" sourcepath="/ANDROID_PLAT_SRC/frameworks/base"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>
diff --git a/tools/layoutlib/bridge/src/android/graphics/Paint.java b/tools/layoutlib/bridge/src/android/graphics/Paint.java
index d13b5fe..2de21c1 100644
--- a/tools/layoutlib/bridge/src/android/graphics/Paint.java
+++ b/tools/layoutlib/bridge/src/android/graphics/Paint.java
@@ -249,7 +249,7 @@
     private void updateFontObject() {
         if (mTypeface != null) {
             // Get the fonts from the TypeFace object.
-            List<Font> fonts = mTypeface.getFonts();
+            List<Font> fonts = Typeface_Delegate.getFonts(mTypeface);
 
             // create new font objects as well as FontMetrics, based on the current text size
             // and skew info.
diff --git a/tools/layoutlib/bridge/src/android/graphics/Typeface.java b/tools/layoutlib/bridge/src/android/graphics/Typeface.java
deleted file mode 100644
index af3adb5..0000000
--- a/tools/layoutlib/bridge/src/android/graphics/Typeface.java
+++ /dev/null
@@ -1,190 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package android.graphics;
-
-import com.android.layoutlib.bridge.FontLoader;
-
-import android.content.res.AssetManager;
-
-import java.awt.Font;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-/**
- * Re-implementation of Typeface over java.awt
- */
-public class Typeface {
-    private static final String DEFAULT_FAMILY = "sans-serif";
-    private static final int[] styleBuffer = new int[1];
-
-    /** The default NORMAL typeface object */
-    public static Typeface DEFAULT;
-    /**
-     * The default BOLD typeface object. Note: this may be not actually be
-     * bold, depending on what fonts are installed. Call getStyle() to know
-     * for sure.
-     */
-    public static Typeface DEFAULT_BOLD;
-    /** The NORMAL style of the default sans serif typeface. */
-    public static Typeface SANS_SERIF;
-    /** The NORMAL style of the default serif typeface. */
-    public static Typeface SERIF;
-    /** The NORMAL style of the default monospace typeface. */
-    public static Typeface MONOSPACE;
-
-    private static Typeface[] sDefaults;
-    private static FontLoader mFontLoader;
-
-    private final int mStyle;
-    private final List<Font> mFonts;
-    private final String mFamily;
-
-    // Style
-    public static final int NORMAL = _Original_Typeface.NORMAL;
-    public static final int BOLD = _Original_Typeface.BOLD;
-    public static final int ITALIC = _Original_Typeface.ITALIC;
-    public static final int BOLD_ITALIC = _Original_Typeface.BOLD_ITALIC;
-
-    /**
-     * Returns the underlying {@link Font} objects. The first item in the list is the real
-     * font. Any other items are fallback fonts for characters not found in the first one.
-     */
-    public List<Font> getFonts() {
-        return mFonts;
-    }
-
-    /** Returns the typeface's intrinsic style attributes */
-    public int getStyle() {
-        return mStyle;
-    }
-
-    /** Returns true if getStyle() has the BOLD bit set. */
-    public final boolean isBold() {
-        return (getStyle() & BOLD) != 0;
-    }
-
-    /** Returns true if getStyle() has the ITALIC bit set. */
-    public final boolean isItalic() {
-        return (getStyle() & ITALIC) != 0;
-    }
-
-    /**
-     * Create a typeface object given a family name, and option style information.
-     * If null is passed for the name, then the "default" font will be chosen.
-     * The resulting typeface object can be queried (getStyle()) to discover what
-     * its "real" style characteristics are.
-     *
-     * @param familyName May be null. The name of the font family.
-     * @param style  The style (normal, bold, italic) of the typeface.
-     *               e.g. NORMAL, BOLD, ITALIC, BOLD_ITALIC
-     * @return The best matching typeface.
-     */
-    public static Typeface create(String familyName, int style) {
-        styleBuffer[0] = style;
-        Font font = mFontLoader.getFont(familyName, styleBuffer);
-        if (font != null) {
-            ArrayList<Font> list = new ArrayList<Font>();
-            list.add(font);
-            list.addAll(mFontLoader.getFallBackFonts());
-            return new Typeface(familyName, styleBuffer[0], list);
-        }
-
-        return null;
-    }
-
-    /**
-     * Create a typeface object that best matches the specified existing
-     * typeface and the specified Style. Use this call if you want to pick a new
-     * style from the same family of an existing typeface object. If family is
-     * null, this selects from the default font's family.
-     *
-     * @param family May be null. The name of the existing type face.
-     * @param style  The style (normal, bold, italic) of the typeface.
-     *               e.g. NORMAL, BOLD, ITALIC, BOLD_ITALIC
-     * @return The best matching typeface.
-     */
-    public static Typeface create(Typeface family, int style) {
-        styleBuffer[0] = style;
-        Font font = mFontLoader.getFont(family.mFamily, styleBuffer);
-        if (font != null) {
-            ArrayList<Font> list = new ArrayList<Font>();
-            list.add(font);
-            list.addAll(mFontLoader.getFallBackFonts());
-            return new Typeface(family.mFamily, styleBuffer[0], list);
-        }
-
-        return null;
-    }
-
-    /**
-     * Returns one of the default typeface objects, based on the specified style
-     *
-     * @return the default typeface that corresponds to the style
-     */
-    public static Typeface defaultFromStyle(int style) {
-        return sDefaults[style];
-    }
-
-    /**
-     * Create a new typeface from the specified font data.
-     * @param mgr The application's asset manager
-     * @param path  The file name of the font data in the assets directory
-     * @return The new typeface.
-     */
-    public static Typeface createFromAsset(AssetManager mgr, String path) {
-        return null;
-        //return new Typeface(nativeCreateFromAsset(mgr, path));
-    }
-
-    // don't allow clients to call this directly
-    private Typeface(String family, int style, List<Font> fonts) {
-        mFamily = family;
-        mFonts = Collections.unmodifiableList(fonts);
-        mStyle = style;
-    }
-
-    public static void init(FontLoader fontLoader) {
-        mFontLoader = fontLoader;
-
-        DEFAULT = create(DEFAULT_FAMILY, NORMAL);
-        DEFAULT_BOLD = create(DEFAULT_FAMILY, BOLD);
-        SANS_SERIF = create("sans-serif", NORMAL);
-        SERIF = create("serif", NORMAL);
-        MONOSPACE = create("monospace", NORMAL);
-        sDefaults = new Typeface[] {
-                DEFAULT,
-                DEFAULT_BOLD,
-                create(DEFAULT_FAMILY, ITALIC),
-                create(DEFAULT_FAMILY, BOLD_ITALIC),
-        };
-
-        /*
-        DEFAULT         = create((String)null, 0);
-        DEFAULT_BOLD    = create((String)null, Typeface.BOLD);
-        SANS_SERIF      = create("sans-serif", 0);
-        SERIF           = create("serif", 0);
-        MONOSPACE       = create("monospace", 0);
-
-        sDefaults = new Typeface[] {
-            DEFAULT,
-            DEFAULT_BOLD,
-            create((String)null, Typeface.ITALIC),
-            create((String)null, Typeface.BOLD_ITALIC),
-        };*/
-    }
-}
diff --git a/tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java b/tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java
new file mode 100644
index 0000000..309d934
--- /dev/null
+++ b/tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java
@@ -0,0 +1,171 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.graphics;
+
+import com.android.layoutlib.bridge.DelegateManager;
+import com.android.layoutlib.bridge.FontLoader;
+
+import android.content.res.AssetManager;
+
+import java.awt.Font;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Delegate implementing the native methods of android.graphics.Typeface
+ *
+ * Through the layoutlib_create tool, the original native methods of Typeface have been replaced
+ * by calls to methods of the same name in this delegate class.
+ *
+ * This class behaves like the original native implementation, but in Java, keeping previously
+ * native data into its own objects and mapping them to int that are sent back and forth between
+ * it and the original Matrix class.
+ *
+ * @see DelegateManager
+ *
+ */
+public final class Typeface_Delegate {
+
+    // ---- delegate manager ----
+    private static final DelegateManager<Typeface_Delegate> sManager =
+            new DelegateManager<Typeface_Delegate>();
+
+    // ---- delegate helper data ----
+    private static final String DEFAULT_FAMILY = "sans-serif";
+    private static final int[] STYLE_BUFFER = new int[1];
+
+    private static FontLoader sFontLoader;
+    private static final List<Typeface_Delegate> sPostInitDelegate =
+            new ArrayList<Typeface_Delegate>();
+
+    // ---- delegate data ----
+
+    private final String mFamily;
+    private int mStyle;
+    private List<Font> mFonts;
+
+
+    // ---- Public Helper methods ----
+
+    public static synchronized void init(FontLoader fontLoader) {
+        sFontLoader = fontLoader;
+
+        for (Typeface_Delegate delegate : sPostInitDelegate) {
+            delegate.init();
+        }
+        sPostInitDelegate.clear();
+    }
+
+    public static List<Font> getFonts(Typeface typeface) {
+        Typeface_Delegate delegate = sManager.getDelegate(typeface.native_instance);
+        if (delegate == null) {
+            assert false;
+            return null;
+        }
+
+        return delegate.mFonts;
+    }
+
+
+    // ---- native methods ----
+
+    public static synchronized int nativeCreate(String familyName, int style) {
+        if (familyName == null) {
+            familyName = DEFAULT_FAMILY;
+        }
+
+        Typeface_Delegate newDelegate = new Typeface_Delegate(familyName, style);
+        if (sFontLoader != null) {
+            newDelegate.init();
+        } else {
+            // font loader has not been initialized yet, add the delegate to a list of delegates
+            // to init when the font loader is initialized.
+            // There won't be any rendering before this happens anyway.
+            sPostInitDelegate.add(newDelegate);
+        }
+
+        return sManager.addDelegate(newDelegate);
+    }
+
+    public static synchronized int nativeCreateFromTypeface(int native_instance, int style) {
+        Typeface_Delegate delegate = sManager.getDelegate(native_instance);
+        if (delegate == null) {
+            assert false;
+            return 0;
+        }
+
+        Typeface_Delegate newDelegate = new Typeface_Delegate(delegate.mFamily, style);
+        if (sFontLoader != null) {
+            newDelegate.init();
+        } else {
+            // font loader has not been initialized yet, add the delegate to a list of delegates
+            // to init when the font loader is initialized.
+            // There won't be any rendering before this happens anyway.
+            sPostInitDelegate.add(newDelegate);
+        }
+
+        return sManager.addDelegate(newDelegate);
+    }
+
+    public static synchronized int nativeCreateFromAsset(AssetManager mgr, String path) {
+        // FIXME
+        throw new UnsupportedOperationException();
+    }
+
+    public static synchronized int nativeCreateFromFile(String path) {
+        // FIXME
+        throw new UnsupportedOperationException();
+    }
+
+    public static void nativeUnref(int native_instance) {
+        sManager.removeDelegate(native_instance);
+    }
+
+    public static int nativeGetStyle(int native_instance) {
+        Typeface_Delegate delegate = sManager.getDelegate(native_instance);
+        if (delegate == null) {
+            assert false;
+            return 0;
+        }
+
+        return delegate.mStyle;
+    }
+
+    public static void setGammaForText(float blackGamma, float whiteGamma) {
+        // This is for device testing only: pass
+    }
+
+    // ---- Private delegate/helper methods ----
+
+    private Typeface_Delegate(String family, int style) {
+        mFamily = family;
+        mStyle = style;
+    }
+
+    private void init() {
+        STYLE_BUFFER[0] = mStyle;
+        Font font = sFontLoader.getFont(mFamily, STYLE_BUFFER);
+        if (font != null) {
+            List<Font> list = new ArrayList<Font>();
+            list.add(font);
+            list.addAll(sFontLoader.getFallBackFonts());
+            mFonts = Collections.unmodifiableList(list);
+            mStyle = STYLE_BUFFER[0];
+        }
+    }
+}
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java
index eb0eba2..9eb83c8 100644
--- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java
@@ -31,13 +31,12 @@
 import com.android.tools.layoutlib.create.OverrideMethod;
 
 import android.content.ClipData;
-import android.content.ClipDescription;
 import android.content.res.Configuration;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.Rect;
 import android.graphics.Region;
-import android.graphics.Typeface;
+import android.graphics.Typeface_Delegate;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.Handler;
@@ -49,9 +48,9 @@
 import android.util.TypedValue;
 import android.view.BridgeInflater;
 import android.view.DragEvent;
-import android.view.InputChannel;
 import android.view.IWindow;
 import android.view.IWindowSession;
+import android.view.InputChannel;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
 import android.view.Surface;
@@ -220,7 +219,7 @@
         // load the fonts.
         FontLoader fontLoader = FontLoader.create(fontOsLocation);
         if (fontLoader != null) {
-            Typeface.init(fontLoader);
+            Typeface_Delegate.init(fontLoader);
         } else {
             return false;
         }
@@ -1007,7 +1006,7 @@
             // pass for now.
             return 0;
         }
-        
+
         @SuppressWarnings("unused")
         public void finishDrawing(IWindow arg0) throws RemoteException {
             // pass for now.
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/BridgeContentProvider.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/BridgeContentProvider.java
new file mode 100644
index 0000000..9d6dd27
--- /dev/null
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/BridgeContentProvider.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.layoutlib.bridge;
+
+import android.content.ContentProviderOperation;
+import android.content.ContentProviderResult;
+import android.content.ContentValues;
+import android.content.IContentProvider;
+import android.content.OperationApplicationException;
+import android.content.res.AssetFileDescriptor;
+import android.database.Cursor;
+import android.database.CursorWindow;
+import android.database.IBulkCursor;
+import android.database.IContentObserver;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.IBinder;
+import android.os.ParcelFileDescriptor;
+import android.os.RemoteException;
+
+import java.io.FileNotFoundException;
+import java.util.ArrayList;
+
+/**
+ * Mock implementation of {@link IContentProvider}.
+ *
+ * TODO: never return null when the method is not supposed to. Return fake data instead.
+ */
+public final class BridgeContentProvider implements IContentProvider {
+
+    public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> arg0)
+            throws RemoteException, OperationApplicationException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public int bulkInsert(Uri arg0, ContentValues[] arg1) throws RemoteException {
+        // TODO Auto-generated method stub
+        return 0;
+    }
+
+    public IBulkCursor bulkQuery(Uri arg0, String[] arg1, String arg2, String[] arg3,
+            String arg4, IContentObserver arg5, CursorWindow arg6) throws RemoteException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public Bundle call(String arg0, String arg1, Bundle arg2) throws RemoteException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public int delete(Uri arg0, String arg1, String[] arg2) throws RemoteException {
+        // TODO Auto-generated method stub
+        return 0;
+    }
+
+    public String getType(Uri arg0) throws RemoteException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public Uri insert(Uri arg0, ContentValues arg1) throws RemoteException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public AssetFileDescriptor openAssetFile(Uri arg0, String arg1) throws RemoteException,
+            FileNotFoundException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public ParcelFileDescriptor openFile(Uri arg0, String arg1) throws RemoteException,
+            FileNotFoundException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3, String arg4)
+            throws RemoteException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3)
+            throws RemoteException {
+        // TODO Auto-generated method stub
+        return 0;
+    }
+
+    public IBinder asBinder() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public String[] getStreamTypes(Uri arg0, String arg1) throws RemoteException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public AssetFileDescriptor openTypedAssetFile(Uri arg0, String arg1, Bundle arg2)
+            throws RemoteException, FileNotFoundException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+}
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/BridgeContentResolver.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/BridgeContentResolver.java
index a063455..e15cb69 100644
--- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/BridgeContentResolver.java
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/BridgeContentResolver.java
@@ -16,27 +16,12 @@
 
 package com.android.layoutlib.bridge;
 
-import android.content.ContentProviderOperation;
-import android.content.ContentProviderResult;
 import android.content.ContentResolver;
-import android.content.ContentValues;
 import android.content.Context;
 import android.content.IContentProvider;
-import android.content.OperationApplicationException;
-import android.content.res.AssetFileDescriptor;
 import android.database.ContentObserver;
-import android.database.Cursor;
-import android.database.CursorWindow;
-import android.database.IBulkCursor;
-import android.database.IContentObserver;
 import android.net.Uri;
 import android.os.Bundle;
-import android.os.IBinder;
-import android.os.ParcelFileDescriptor;
-import android.os.RemoteException;
-
-import java.io.FileNotFoundException;
-import java.util.ArrayList;
 
 /**
  * A mock content resolver for the LayoutLib Bridge.
@@ -49,87 +34,6 @@
 
     private BridgeContentProvider mProvider = null;
 
-    public static final class BridgeContentProvider implements IContentProvider {
-
-        public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> arg0)
-                throws RemoteException, OperationApplicationException {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        public int bulkInsert(Uri arg0, ContentValues[] arg1) throws RemoteException {
-            // TODO Auto-generated method stub
-            return 0;
-        }
-
-        public IBulkCursor bulkQuery(Uri arg0, String[] arg1, String arg2, String[] arg3,
-                String arg4, IContentObserver arg5, CursorWindow arg6) throws RemoteException {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        public Bundle call(String arg0, String arg1, Bundle arg2) throws RemoteException {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        public int delete(Uri arg0, String arg1, String[] arg2) throws RemoteException {
-            // TODO Auto-generated method stub
-            return 0;
-        }
-
-        public String getType(Uri arg0) throws RemoteException {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        public Uri insert(Uri arg0, ContentValues arg1) throws RemoteException {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        public AssetFileDescriptor openAssetFile(Uri arg0, String arg1) throws RemoteException,
-                FileNotFoundException {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        public ParcelFileDescriptor openFile(Uri arg0, String arg1) throws RemoteException,
-                FileNotFoundException {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3, String arg4)
-                throws RemoteException {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3)
-                throws RemoteException {
-            // TODO Auto-generated method stub
-            return 0;
-        }
-
-        public IBinder asBinder() {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        public String[] getStreamTypes(Uri arg0, String arg1) throws RemoteException {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-        public AssetFileDescriptor openTypedAssetFile(Uri arg0, String arg1, Bundle arg2)
-                throws RemoteException, FileNotFoundException {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-    }
-
     public BridgeContentResolver(Context context) {
         super(context);
     }
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/FontLoader.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/FontLoader.java
index 801503b..de89a81 100644
--- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/FontLoader.java
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/FontLoader.java
@@ -134,6 +134,15 @@
         return mFallBackFonts;
     }
 
+    /**
+     * Returns a {@link Font} object given a family name and a style value (constant in
+     * {@link Typeface}).
+     * @param family the family name
+     * @param style a 1-item array containing the requested style. Based on the font being read
+     *              the actual style may be different. The array contains the actual style after
+     *              the method returns.
+     * @return the font object or null if no match could be found.
+     */
     public synchronized Font getFont(String family, int[] style) {
         if (family == null) {
             return null;
diff --git a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java
index 2d0ee6d..f6d11fe 100644
--- a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java
+++ b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java
@@ -104,6 +104,7 @@
      */
     private final static String[] DELEGATE_CLASS_NATIVES = new String[] {
         "android.graphics.Matrix",
+        "android.graphics.Typeface",
     };
 
     /**
@@ -135,7 +136,6 @@
             "android.graphics.RadialGradient",      "android.graphics._Original_RadialGradient",
             "android.graphics.Shader",              "android.graphics._Original_Shader",
             "android.graphics.SweepGradient",       "android.graphics._Original_SweepGradient",
-            "android.graphics.Typeface",            "android.graphics._Original_Typeface",
             "android.os.ServiceManager",            "android.os._Original_ServiceManager",
             "android.util.FloatMath",               "android.util._Original_FloatMath",
             "android.view.SurfaceView",             "android.view._Original_SurfaceView",