1# ParseQTAtoms.tcl --
2#
3#       Some random code for parsing (QT) atoms.
4
5namespace eval ::quicktimetcl { }
6
7proc ::quicktimetcl::ParseQTAtomFile {fileName} {
8
9    set fd [open $fileName RDONLY]
10    fconfigure $fd -translation binary
11    set version [GetAtomVersion $fd]
12    if {$version == 0} {
13	ParseContainerAtom $fd
14    } else {
15	set offset 12
16	foreach {size type id count} [ParseQTAtomHeader $fd $offset] {break}
17	set offset 32
18	for {set i 0} {$i < $count} {incr i} {
19	    foreach {size type} [ParseAtom $fd $offset] {break}
20	    incr offset $size
21	}
22    }
23    close $fd
24}
25
26proc ::quicktimetcl::GetAtomVersion {fd} {
27
28    # Classic atoms have 4 byte size + 4 byte type.
29    # QT atom container header starts with a 10 byte null element.
30    set data [read $fd 8]
31    binary scan $data II size type
32    if {($size == 0) && ($type == 0)} {
33	return 1
34    } else {
35	return 0
36    }
37}
38
39proc ::quicktimetcl::ParseContainerAtom {fd {offset 0}} {
40
41    # Classic atoms have 4 byte size + 4 byte type.
42    seek $fd $offset
43    set data [read $fd 8]
44    binary scan $data Ia4 size type
45    puts "Classic atom type: type='$type', size=$size"
46    return [list $size $type]
47}
48
49proc ::quicktimetcl::ParseQTAtomHeader {fd {offset 0}} {
50
51    seek $fd $offset
52    set data [read $fd 20]
53    binary scan $data Ia4ISSI size type id zero count zero
54    puts "QT atom header: type='$type', size=$size, id=$id, count=$count"
55    return [list $size $type $id $count]
56}
57
58proc ::quicktimetcl::ParseAtom {fd {offset 0}} {
59
60    # Classic atoms have 4 byte size + 4 byte type.
61    seek $fd $offset
62    set data [read $fd 8]
63    binary scan $data Ia4 size type
64    puts "Atom: type='$type', size=$size"
65    return [list $size $type]
66}
67
68
69