File: Box.cs

package info (click to toggle)
banshee 0.11.2%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 11,976 kB
  • ctags: 9,958
  • sloc: cs: 54,529; xml: 21,240; sh: 8,835; ansic: 2,040; makefile: 1,248
file content (457 lines) | stat: -rw-r--r-- 14,976 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
using System.Collections;

namespace TagLib.Mpeg4
{
   public class Box
   {
      //////////////////////////////////////////////////////////////////////////
      // private properties
      //////////////////////////////////////////////////////////////////////////
      private BoxHeader  header;
      private Box        parent;
      private ByteVector data;
      private bool       load_children_started;
      private ArrayList  children;
      
      
      //////////////////////////////////////////////////////////////////////////
      // public methods
      //////////////////////////////////////////////////////////////////////////
      public Box (BoxHeader header, Box parent)
      {
         // Initialize everything.
         this.header   = header;
         this.parent   = parent;
         this.data     = null;
         this.children = new ArrayList ();
         this.load_children_started = false;
      }
      
      // Do the same as above, but accept a box type.
      public Box (ByteVector type, Box parent) : this (new BoxHeader (type), parent)
      {
      }
      
      // Who needs a parent? I'm all grown up.
      public Box (ByteVector type) : this (type, null)
      {
      }
      
      // Render the complete box including children.
      public virtual ByteVector Render ()
      {
         return Render (new ByteVector ());
      }
      
      // Overwrite the box's header with a new header incorporating a size
      // change.
      public virtual void OverwriteHeader (long size_change)
      {
         // If we don't have a header we can't do anything.
         if (header == null || header.Position < 0)
         {
            Debugger.Debug ("Box.OverWriteHeader() - No header to overwrite.");
            return;
         }
         
         // Make sure this alteration won't screw up the reading of children.
         LoadChildren ();
         
         // Save the header's original position and size.
         long position = header.Position;
         long old_size = header.HeaderSize;
         
         // Update the data size.
         header.DataSize = (ulong) ((long) header.DataSize + size_change);
         
         // Render the header onto the file.
         File.Insert (header.Render (), position, old_size);
      }
      
      // Find the first child with a given box type.
      public Box FindChild (ByteVector type)
      {
         foreach (Box child in Children)
            if (child.BoxType == type)
               return child;
         
         return null;
      }
      
      // Find the first child with a given System.Type.
      public Box FindChild (System.Type type)
      {
         foreach (Box child in Children)
            if (child.GetType () == type)
               return child;
         
         return null;
      }
      
      // Recursively find the first child with a given box type giving 
      // preference to the current depth.
      public Box FindChildDeep (ByteVector type)
      {
         foreach (Box child in Children)
            if (child.BoxType == type)
               return child;
         
         foreach (Box child in Children)
         {
            Box success = child.FindChildDeep (type);
            if (success != null)
               return success;
         }
         
         return null;
      }
      
      // Recursively find the first child with a given System.Type giving
      // preference to the current depth.
      public Box FindChildDeep (System.Type type)
      {
         foreach (Box child in Children)
            if (child.GetType () == type)
               return child;
         
         foreach (Box child in Children)
         {
            Box success = child.FindChildDeep (type);
            if (success != null)
               return success;
         }
         
         return null;
      }
      
      // Add a child to this box.
      public void AddChild (Box child)
      {
         children.Add (child);
         child.Parent = this;
      }
      
      // Remove a child from this box.
      public void RemoveChild (Box child)
      {
         children.Remove (child);
         child.Parent = null;
      }
      
      // Remove all children with a given box type.
      public void RemoveChildren (ByteVector type)
      {
         Box box;
         while ((box = FindChild (type)) != null)
            RemoveChild (box);
      }
      
      // Remove all children with a given System.Type.
      public void RemoveChildren (System.Type type)
      {
         Box box;
         while ((box = FindChild (type)) != null)
            RemoveChild (box);
      }
      
      // Detach the current box from its parent.
      public void RemoveFromParent ()
      {
         if (Parent != null)
            Parent.RemoveChild (this);
      }
      
      // Remove all children from this box.
      public void ClearChildren ()
      {
         foreach (Box box in children)
            box.Parent = null;
         children.Clear ();
      }
      
      // Replace a child with a new one.
      public void ReplaceChild (Box old_child, Box new_child)
      {
         int index = children.IndexOf (old_child);
         
         if (index >= 0)
         {
            children [index] = new_child;
            old_child.Parent = null;
            new_child.Parent = this;
         }
         else
            AddChild (new_child);
      }
      
      // Replace this box with another one.
      public void ReplaceWith (Box box)
      {
         if (Parent != null)
            Parent.ReplaceChild (this, box);
      }
      
      // Load this box's children as well as their children. 
      public void LoadChildren ()
      {
         if (!HasChildren || children.Count != 0 || load_children_started)
            return;
         
         load_children_started = true;
         
         Box box = FirstChild;
         while (box != null)
         {
            box.LoadChildren ();
            children.Add (box);
            box = NextChild (box);
         }
      }
      
      // Load the data stored in this box.
      public void LoadData ()
      {
         if (data == null && this.File != null && this.File.Mode != File.AccessMode.Closed)
         {
            File.Seek (DataPosition);
            data = File.ReadBlock ((int) DataSize);
         }
      }
      
      //////////////////////////////////////////////////////////////////////////
      // public properties
      //////////////////////////////////////////////////////////////////////////
      
      // Is the box valid?
      public virtual bool  IsValid           {get {return header.IsValid;}}
      
      // The file associated with this box.
      public virtual File File               {get {return header.File;}}
      
      // The box's parent box.
      public Box Parent                      {get {return parent;} set {parent = value;}}
      
      // The box type.
      public virtual ByteVector BoxType      {get {return header.BoxType;}}
      
      // The total size of the box.
      public virtual ulong BoxSize           {get {return header.BoxSize;}}
      
      // The size of the non-header data.
      protected virtual ulong DataSize       {get {return header.DataSize;}}
      
      // The stream position of the box's data.
      protected virtual long DataPosition    {get {return header.DataPosition;}}
      
      // Whether or not the box can have children.
      public virtual bool  HasChildren       {get {return false;}}
      
      // The stream position of the next box.
      public virtual long NextBoxPosition    {get {return header.NextBoxPosition;}}
      
      // All child boxes of this box.
      public Box [] Children {get {LoadChildren (); return (Box []) children.ToArray (typeof (Box));}}
      
      // The handler used for this box.
      public IsoHandlerBox Handler
      {
         get
         {
            Box box = this;
            
            // Look in all parent boxes.
            while (box != null)
            {
               // Handlers will be contained in "meta" and "mdia" boxes.
               if (box.BoxType == "mdia" || box.BoxType == "meta")
               {
                  // See if you can find a handler, and return if you can.
                  IsoHandlerBox handler = (IsoHandlerBox) box.FindChild (typeof (IsoHandlerBox));
                  if (handler != null)
                     return handler;
               }
               
               // Check the parent next.
               box = box.Parent;
            }
            
            // Failure.
            return null;
         }
      }
      
      //////////////////////////////////////////////////////////////////////////
      // public static methods
      //////////////////////////////////////////////////////////////////////////
      
      // Create a box by reading the file and add it to "parent".
      public static Box Create (File file, long position, Box parent)
      {
         // Read the box header.
         BoxHeader h = new BoxHeader (file, position);
         
         // If we're not even valid, quit.
         if (!h.IsValid)
            return null;
         
         // IF we're in a SampleDescriptionBox and haven't loaded all the
         // entries, try loading an appropriate entry.
         if (parent.BoxType == "stsd" && parent.Children.Length < ((IsoSampleDescriptionBox) parent).EntryCount)
         {
            IsoHandlerBox handler = parent.Handler;
            if (handler != null && handler.HandlerType == "soun")
               return new IsoAudioSampleEntry (h, parent);
            else
               return new IsoSampleEntry (h, parent);
         }
         
         //
         // A bunch of standard items.
         //
         
         if (h.BoxType == "moov")
            return new IsoMovieBox (h, parent);
         
         if (h.BoxType == "mvhd")
            return new IsoMovieHeaderBox (h, parent);
         
         if (h.BoxType == "mdia")
            return new IsoMediaBox (h, parent);
         
         if (h.BoxType == "minf")
            return new IsoMediaInformationBox (h, parent);
         
         if (h.BoxType == "stbl")
            return new IsoSampleTableBox (h, parent);
         
         if (h.BoxType == "stsd")
            return new IsoSampleDescriptionBox (h, parent);
         
         if (h.BoxType == "stco")
            return new IsoChunkOffsetBox (h, parent);
         
         if (h.BoxType == "co64")
            return new IsoChunkLargeOffsetBox (h, parent);
         
         if (h.BoxType == "trak")
            return new IsoTrackBox (h, parent);
         
         if (h.BoxType == "hdlr")
            return new IsoHandlerBox (h, parent);
         
         if (h.BoxType == "udta")
            return new IsoUserDataBox (h, parent);
         
         if (h.BoxType == "meta")
            return new IsoMetaBox (h, parent);
         
         if (h.BoxType == "ilst")
            return new AppleItemListBox (h, parent);
         
         if (h.BoxType == "data")
            return new AppleDataBox (h, parent);
         
         if (h.BoxType == "esds")
            return new AppleElementaryStreamDescriptor (h, parent);
         
         if (h.BoxType == "free" || h.BoxType == "skip")
            return new IsoFreeSpaceBox (h, parent);
         
         if (h.BoxType == "mean" || h.BoxType == "name")
            return new AppleAdditionalInfoBox (h, parent);
         
         // If we still don't have a tag, and we're inside an ItemLisBox, load
         // lthe box as an AnnotationBox (Apple tag item).
         if (parent.GetType () == typeof (AppleItemListBox))
            return new AppleAnnotationBox (h, parent);
         
         // Nothing good. Go generic.
         return new UnknownBox (h, parent);
      }
      
      // The data contained within this box.
      public virtual ByteVector Data
      {
         get
         {
            LoadData ();
            return data;
         }
         set
         {
            data = value;
         }
      }
      
      
      //////////////////////////////////////////////////////////////////////////
      // protected methods
      //////////////////////////////////////////////////////////////////////////
      
      // Render a box with the "data" before its content.
      protected virtual ByteVector Render (ByteVector data)
      {
         bool free_found = false;
         
         ByteVector output = new ByteVector ();
         
         // If we have children, render them if they aren't "free", otherwise
         // render the box's data.
         if (HasChildren)
            foreach (Box box in Children)
               if (box.GetType () == typeof (IsoFreeSpaceBox))
                  free_found = true;
               else
                  output += box.Render ();
         else
            output += Data;
         
         // If there was a free, don't take it away, and let meta be a special case.
         if (free_found || BoxType == "meta")
         {
            long size_difference = (long) DataSize - (long) output.Count;
            
            // If we have room for free space, add it so we don't have to resize the file.
            if (header.DataSize != 0 && size_difference >= 8)
               output += (new IsoFreeSpaceBox ((ulong) size_difference, this)).Render ();
            // If we're getting bigger, get a lot bigger so we might not have to again.
            else
               output += (new IsoFreeSpaceBox (2048, this)).Render ();
         }
         
         // Adjust the header's data size to match the content.
         header.DataSize = (ulong) (data.Count + output.Count);
         
         // Render the full box.
         return header.Render () + data + output;
      }
      
      
      //////////////////////////////////////////////////////////////////////////
      // private members
      //////////////////////////////////////////////////////////////////////////
      
      // If the file is readable and the box can have children and there is 
      // enough space to read the data, get the first child box by reading the
      // file.
      private Box FirstChild
      {
         get
         {
            if (HasChildren && this.File != null && (GetType () == typeof (FileBox) || DataSize >= 8))
               return Box.Create (File, DataPosition, this);
            return null;
         }
      }
      
      // If the file is readable and the box can have children and there is 
      // enough space to read the data, get the next box by reading from the end
      // of the first box.
      private Box NextChild (Box child)
      {
         if (HasChildren && this.File != null && child.NextBoxPosition >= DataPosition && child.NextBoxPosition < NextBoxPosition)
            return Box.Create (File, child.NextBoxPosition, this);
         return null;
      }
   }
}