internal static EbmlBinary NormalizeHeader(Container c)
        {
            //Remove DATEUTC
            EbmlMaster info = (EbmlMaster)c.FindFirst(Ids.MATROSKA_ID_INFO);
            EbmlBinary date = (EbmlBinary)info.FindFirst(Ids.MATROSKA_ID_DATEUTC);

            if (date != null)
            {
                info.Value.Remove(date);
            }
            //Changing Muxing and Writting App to a Constant.
            EbmlUtf8 muxapp = (EbmlUtf8)info.FindFirst(Ids.MATROSKA_ID_MUXINGAPP);

            if (muxapp == null)
            {
                muxapp = new EbmlUtf8 {
                    Id = Ids.MATROSKA_ID_MUXINGAPP
                };
                info.Value.Add(muxapp);
            }
            EbmlUtf8 wrtapp = (EbmlUtf8)info.FindFirst(Ids.MATROSKA_ID_WRITINGAPP);

            if (wrtapp == null)
            {
                wrtapp = new EbmlUtf8 {
                    Id = Ids.MATROSKA_ID_WRITINGAPP
                };
                info.Value.Add(wrtapp);
            }
            muxapp.Value = wrtapp.Value = "AOD";
            EbmlBinary seguid = (EbmlBinary)info.FindFirst(Ids.MATROSKA_ID_SEGMENTUID);

            if (seguid == null)
            {
                seguid = new EbmlBinary {
                    Id = Ids.MATROSKA_ID_SEGMENTUID, Value = new byte[16]
                };
                info.Value.Add(seguid);
            }

            info.Value.Sort();
            return(seguid);
        }
        public static void FixPositions(Container c)
        {
            EbmlMaster segment    = (EbmlMaster)c.FindFirst(Ids.MATROSKA_ID_SEGMENT);
            bool       repeatpass = false;

            List <EbmlGeneric> cuepositions = c.FindAll(Ids.MATROSKA_ID_CUECLUSTERPOSITION);
            List <EbmlGeneric> clusters     = c.FindAll(Ids.MATROSKA_ID_CLUSTER);
            int cnt = 1;

            foreach (EbmlUint j in cuepositions)
            {
                ulong      val  = j.Value + segment.InputValueOffset;
                EbmlMaster repo = (EbmlMaster)clusters.First(a => a.InputOffset == val);
                if (repo.LinkedId != 0)
                {
                    j.LinkedId = repo.LinkedId;
                }
                else
                {
                    repo.LinkedId = j.LinkedId = cnt;
                    cnt++;
                }
            }

            do
            {
                repeatpass = false;
                //Propagate Expected Offsets
                c.PropagateExpectedOffset();
                //Reposition Cues
                foreach (EbmlUint j in cuepositions)
                {
                    EbmlMaster repo   = (EbmlMaster)clusters.First(a => a.LinkedId == j.LinkedId);
                    ulong      nvalue = repo.ExpectedOffset - segment.ExpectedValueOffset;
                    if (nvalue != j.Value)
                    {
                        repeatpass = true;
                        j.Value    = nvalue;
                    }
                }
                if (!repeatpass)
                {
                    //Propagate Expected Offsets
                    c.PropagateExpectedOffset();
                    //Fix Seek Header
                    EbmlMaster         smas  = (EbmlMaster)c.FindFirst(Ids.MATROSKA_ID_SEEKHEAD);
                    List <EbmlGeneric> seeks = c.FindAll(Ids.MATROSKA_ID_SEEKENTRY);
                    foreach (EbmlMaster sk in seeks)
                    {
                        EbmlUint id = (EbmlUint)sk.FindFirst(Ids.MATROSKA_ID_SEEKID);
                        if (id != null)
                        {
                            EbmlUint    pos = (EbmlUint)sk.FindFirst(Ids.MATROSKA_ID_SEEKPOSITION);
                            EbmlGeneric gen = segment.FindFirst(id.Value);
                            if (gen == null)
                            {
                                repeatpass = true;
                                smas.Value.Remove(sk);
                                break;
                            }
                            ulong nval = gen.ExpectedOffset - segment.ExpectedValueOffset;
                            if (pos.Value != nval)
                            {
                                pos.Value  = nval;
                                repeatpass = true;
                            }
                        }
                    }
                }
            } while (repeatpass);
        }
        internal static void MoveTagInfoToTracks(Container c)
        {
            //Moving Title and Language tags to tracks if they exists
            List <EbmlGeneric> tracks = c.FindAll(Ids.MATROSKA_ID_TRACKENTRY);
            List <EbmlGeneric> tags   = c.FindAll(Ids.MATROSKA_ID_TAG);

            foreach (EbmlMaster t in tags)
            {
                EbmlMaster tt = (EbmlMaster)t.FindFirst(Ids.MATROSKA_ID_TAGTARGETS);
                if (tt != null)
                {
                    EbmlMaster sp = (EbmlMaster)t.FindFirst(Ids.MATROSKA_ID_SIMPLETAG);
                    EbmlUint   jk = (EbmlUint)tt.FindFirst(Ids.MATROSKA_ID_TAGTARGETS_TRACKUID);
                    if (jk != null)
                    {
                        EbmlUtf8 name  = (EbmlUtf8)sp.FindFirst(Ids.MATROSKA_ID_TAGNAME);
                        EbmlUtf8 value = (EbmlUtf8)sp.FindFirst(Ids.MATROSKA_ID_TAGSTRING);
                        if (name != null && value != null)
                        {
                            string nm = name.Value.ToUpper(CultureInfo.InvariantCulture);
                            string vl = value.Value;
                            if (nm == "LANGUAGE" || nm == "TITLE" || nm == "NAME")
                            {
                                foreach (EbmlMaster j in tracks)
                                {
                                    EbmlUint u = (EbmlUint)j.FindFirst(Ids.MATROSKA_ID_TRACKUID);
                                    if (u.Value == jk.Value)
                                    {
                                        EbmlUtf8 change;
                                        if (nm == "TITLE" || nm == "NAME")
                                        {
                                            change = (EbmlUtf8)j.FindFirst(Ids.MATROSKA_ID_TRACKNAME);
                                            if (change == null)
                                            {
                                                change = new EbmlUtf8 {
                                                    Id = Ids.MATROSKA_ID_TRACKNAME
                                                };
                                                j.Value.Add(change);
                                            }
                                        }
                                        else
                                        {
                                            change = (EbmlUtf8)j.FindFirst(Ids.MATROSKA_ID_TRACKLANGUAGE);
                                            if (change == null)
                                            {
                                                change = new EbmlUtf8 {
                                                    Id = Ids.MATROSKA_ID_TRACKLANGUAGE
                                                };
                                                j.Value.Add(change);
                                            }
                                        }
                                        change.Value = vl;
                                        j.Value.Sort();
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }