m4a.lisp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. ;;; -*- Mode: Lisp; show-trailing-whitespace: t; Base: 10; indent-tabs: nil; Syntax: ANSI-Common-Lisp; Package: M4A; -*-
  2. ;;; Copyright (c) 2013, Mark VandenBrink. All rights reserved.
  3. (in-package #:m4a)
  4. ;;;; ATOMS
  5. ;;;
  6. ;;; A word about atoms (aka "boxes"). There are three kinds of atoms: ones that
  7. ;;; are containers, ones that are data, and ones that are both. A lot of the
  8. ;;; taggers out there mostly ignore third class and treat "container atoms that
  9. ;;; also have data" as a big blob of data that they rummage around in via
  10. ;;; indices. Seems sort of broken, IMHO, so we'll try to handle all three if
  11. ;;; at all possible.
  12. ;;;
  13. (defun mk-atom-class-name (name)
  14. "Create an atom class name by concatenating ATOM- with NAME, preserving case"
  15. (declare #.utils:*standard-optimize-settings*)
  16. (mkstr "atom-" name))
  17. (memoize 'mk-atom-class-name)
  18. (eval-when (:compile-toplevel :load-toplevel :execute)
  19. (defun as-string (atom-type)
  20. "Given an integer, return the string representation"
  21. (declare #.utils:*standard-optimize-settings*)
  22. (with-output-to-string (s nil)
  23. (write-char (code-char (ldb (byte 8 24) atom-type)) s)
  24. (write-char (code-char (ldb (byte 8 16) atom-type)) s)
  25. (write-char (code-char (ldb (byte 8 8) atom-type)) s)
  26. (write-char (code-char (ldb (byte 8 0) atom-type)) s)))
  27. (defun as-octet (c)
  28. "Used below so that we can create atom 'types' from char/ints"
  29. (declare #.utils:*standard-optimize-settings*)
  30. (cond ((typep c 'standard-char) (coerce (char-code c) '(unsigned-byte 8)))
  31. ((typep c 'integer) (coerce c '(unsigned-byte 8)))
  32. (t (error "can only handle characters and integers"))))
  33. (defmacro mk-mp4-atom-type (l1 l2 l3 l4)
  34. "Given 4 chars/ints, create a string for the name"
  35. `(with-output-to-string (s nil)
  36. (write-char (code-char ,(as-octet l1)) s)
  37. (write-char (code-char ,(as-octet l2)) s)
  38. (write-char (code-char ,(as-octet l3)) s)
  39. (write-char (code-char ,(as-octet l4)) s))))
  40. ;;;; Atom names/ids. Each one of these has an associated "value"(nee data) atom under it
  41. (defconstant* +root+ (mk-mp4-atom-type #\R #\O #\O #\T) "fake root for atom tree")
  42. (defconstant* +itunes-ilst-data+ (mk-mp4-atom-type #\d #\a #\t #\a) "carries the actual data under an ilst atom")
  43. (defconstant* +m4-ftyp+ (mk-mp4-atom-type #\f #\t #\y #\p) "This should be the first atom type found in file")
  44. (defconstant* +mp4-atom-hdlr+ (mk-mp4-atom-type #\h #\d #\l #\r) "Found under trak.mdia and tells what kind of handler it is")
  45. (defconstant* +audioprop-mdhd+ (mk-mp4-atom-type #\m #\d #\h #\d) "Found under trak.mdia and holds data to calculate length of audio")
  46. (defconstant* +audioprop-stsd+ (mk-mp4-atom-type #\s #\t #\s #\d) "Container atom: found under trak.mdia.minf.stbl and holds bit-rate, etc")
  47. (defconstant* +audioprop-mp4a+ (mk-mp4-atom-type #\m #\p #\4 #\a) "Found under trak.mdia.minf.stbl")
  48. (defconstant* +audioprop-esds+ (mk-mp4-atom-type #\e #\s #\d #\s) "Found under trak.mdia.minf.stbl.mp4a")
  49. (defconstant* +mp4-atom-ilst+ (mk-mp4-atom-type #\i #\l #\s #\t))
  50. (defconstant* +mp4-atom-mdia+ (mk-mp4-atom-type #\m #\d #\i #\a))
  51. (defconstant* +mp4-atom-meta+ (mk-mp4-atom-type #\m #\e #\t #\a))
  52. (defconstant* +mp4-atom-minf+ (mk-mp4-atom-type #\m #\i #\n #\f))
  53. (defconstant* +mp4-atom-moov+ (mk-mp4-atom-type #\m #\o #\o #\v))
  54. (defconstant* +mp4-atom-stbl+ (mk-mp4-atom-type #\s #\t #\b #\l))
  55. (defconstant* +mp4-atom-trak+ (mk-mp4-atom-type #\t #\r #\a #\k))
  56. (defconstant* +mp4-atom-udta+ (mk-mp4-atom-type #\u #\d #\t #\a))
  57. (defparameter *in-progress* nil "the node currently being worked upon")
  58. (defparameter *tree* nil "the root of the atom tree being constructed")
  59. ;;;; Atoms
  60. (defclass mp4-atom ()
  61. ((atom-file-pos :accessor atom-file-pos :initarg :atom-file-pos :initform nil)
  62. (atom-size :accessor atom-size :initarg :atom-size :initform nil)
  63. (atom-type :accessor atom-type :initarg :atom-type :initform nil))
  64. (:documentation "The minimal mp4-atom."))
  65. (defmethod initialize-instance :around ((me mp4-atom) &key &allow-other-keys)
  66. (declare #.utils:*standard-optimize-settings*)
  67. (let* ((old *in-progress*)
  68. (*in-progress* (tree:make-node me)))
  69. (if old
  70. (progn
  71. (tree:add-child old *in-progress*)
  72. (call-next-method))
  73. (let ((*tree* *in-progress*)) ; must use dynamic binding for multi-threading
  74. (call-next-method)
  75. ;; HACK ALERT: this is the top of the tree, so stuff the root
  76. ;; in the TREE slot of a container atom
  77. (setf (tree me) *tree*)))))
  78. (defmacro with-mp4-atom-slots ((instance) &body body)
  79. `(with-slots (atom-file-pos atom-size atom-type) ,instance
  80. ,@body))
  81. ;;;; Concrete atoms
  82. (defclass atom-skip (mp4-atom) ())
  83. (defmethod initialize-instance :after ((me atom-skip) &key mp4-file &allow-other-keys)
  84. "The 'skip' atom. Used when we want to capture the header of atom, but don't want/need
  85. to read the payload of an atom."
  86. (declare #.utils:*standard-optimize-settings*)
  87. (with-mp4-atom-slots (me)
  88. (stream-seek mp4-file (- atom-size 8) :current)))
  89. ;;; For atoms we don't implement yet, subclass atom-skip
  90. (defclass |atom-----| (atom-skip) ())
  91. (defclass |atom-cmID| (atom-skip) ()) ; ???
  92. (defclass |atom-dinf| (atom-skip) ())
  93. (defclass |atom-drms| (atom-skip) ())
  94. (defclass |atom-edts| (atom-skip) ())
  95. (defclass |atom-flvr| (atom-skip) ())
  96. (defclass |atom-free| (atom-skip) ())
  97. (defclass |atom-ftyp| (atom-skip) ())
  98. (defclass |atom-iods| (atom-skip) ())
  99. (defclass |atom-mdat| (atom-skip) ())
  100. (defclass |atom-mvhd| (atom-skip) ())
  101. (defclass |atom-name| (atom-skip) ())
  102. (defclass |atom-pinf| (atom-skip) ())
  103. (defclass |atom-plID| (atom-skip) ())
  104. (defclass |atom-sbtd| (atom-skip) ())
  105. (defclass |atom-smhd| (atom-skip) ())
  106. (defclass |atom-stco| (atom-skip) ())
  107. (defclass |atom-stsc| (atom-skip) ())
  108. (defclass |atom-stsz| (atom-skip) ())
  109. (defclass |atom-stts| (atom-skip) ())
  110. (defclass |atom-tkhd| (atom-skip) ())
  111. (defclass |atom-xid | (atom-skip) ()) ; NOTE: it's actually "xid#\Space"
  112. (defclass mp4-container-atom (mp4-atom)
  113. ((tree :accessor tree :documentation "Note: this is ONLY set for the ROOT atom"))
  114. (:documentation "An atom that 'contains' other atoms"))
  115. (defmethod initialize-instance :after ((me mp4-container-atom) &key mp4-file &allow-other-keys)
  116. (declare #.utils:*standard-optimize-settings*)
  117. (with-mp4-atom-slots (me)
  118. (loop for end = (+ atom-file-pos atom-size)
  119. for current = (stream-seek mp4-file) then (stream-seek mp4-file)
  120. while (< current end) do
  121. (make-mp4-atom mp4-file me))))
  122. ;;;; ILST ATOMS (ie atoms related to tagging)
  123. (defclass |atom-ilst| (mp4-container-atom) ())
  124. (eval-when (:compile-toplevel :load-toplevel :execute)
  125. (defparameter *ilst-atoms*
  126. '(("account-type" "akID")
  127. ("album" "©alb")
  128. ("album-artist" "aART")
  129. ("artist" "©ART")
  130. ("at-id" "atID")
  131. ("cn-id" "cnID")
  132. ("comment" "©cmt")
  133. ("compilation" "cpil")
  134. ("composer" "©com")
  135. ("content-rating" "rtng")
  136. ("copyright" "cprt")
  137. ("cover-art" "covr")
  138. ("disk" "disk")
  139. ("encoder" "©enc")
  140. ("gapless-playback" "pgap")
  141. ("ge-id" "geID")
  142. ("genre" "gnre")
  143. ("genre-x" "©gen")
  144. ("groups" "©grp")
  145. ("lyrics" "©lyr")
  146. ("media-type" "stik")
  147. ("purchase-account" "apID")
  148. ("purchased-date" "purd")
  149. ("sort-album" "soal")
  150. ("sort-album-artist" "soaa")
  151. ("sort-artist" "soar")
  152. ("sort-composer" "soco")
  153. ("sort-name" "sonm")
  154. ("store" "sfID")
  155. ("tempo" "tmpo")
  156. ("title" "©nam")
  157. ("tool" "©too")
  158. ("track" "©trk")
  159. ("track-n" "trkn")
  160. ("writer" "©wrt")
  161. ("year" "©day"))
  162. "This is the list of ILST atoms we handle. Each entry is constant-name/class-name")
  163. (defmacro mk-ilst-atoms-constants ()
  164. `(progn
  165. ,@(loop for e in *ilst-atoms*
  166. collect
  167. `(progn
  168. (defclass ,(mksym nil "atom-" (second e)) (|atom-ilst|) ())
  169. (defconstant* ,(mksym t "+itunes-" (first e) "+") ,(second e))
  170. (export ',(mksym t "+itunes-" (first e) "+")))))))
  171. ;;; generate the ilst atoms/constants/exports
  172. (mk-ilst-atoms-constants)
  173. (defclass |atom-data| (mp4-atom)
  174. ((atom-version :accessor atom-version :initarg :atom-version :initform nil)
  175. (atom-flags :accessor atom-flags :initarg :atom-flags :initform nil)
  176. (atom-locale :accessor atom-locale :initarg :atom-locale :initform nil)
  177. (atom-value :accessor atom-value :initarg :atom-value :initform nil))
  178. (:documentation "Represents the 'data' portion of ilst data atom"))
  179. (defmethod initialize-instance :after ((me |atom-data|) &key mp4-file parent &allow-other-keys)
  180. (declare #.utils:*standard-optimize-settings*)
  181. (with-slots (atom-size atom-type atom-version atom-flags atom-value atom-locale) me
  182. (setf atom-version (stream-read-u8 mp4-file)
  183. atom-flags (stream-read-u24 mp4-file)
  184. atom-locale (stream-read-u32 mp4-file))
  185. ;; Ideally, we would be able to read the atom's value by looking
  186. ;; solely at the atom-flags; however, when atom-flags == 0, then
  187. ;; things get crazy---I can NOT for the life of me figure out
  188. ;; the trck/trkn/disk integer format, hence the mess below.
  189. (setf atom-value
  190. (ecase atom-flags
  191. (1
  192. (stream-read-utf-8-string mp4-file (- atom-size 16)))
  193. ((13 14)
  194. (stream-read-sequence mp4-file (- atom-size 16)))
  195. ((0 21)
  196. (cond ; messy!
  197. ((member (atom-type parent)
  198. (list +itunes-track+ +itunes-track-n+ +itunes-disk+)
  199. :test #'string=)
  200. (stream-read-u16 mp4-file) ; throw away
  201. (let* ((a (stream-read-u16 mp4-file))
  202. (b (stream-read-u16 mp4-file)))
  203. (stream-seek mp4-file (- atom-size 16 6) :current)
  204. (list a b)))
  205. (t (ecase (- atom-size 16)
  206. (4 (stream-read-u32 mp4-file))
  207. (2 (stream-read-u16 mp4-file))
  208. (1 (stream-read-u8 mp4-file))))))))))
  209. ;;;; Audio Property Atoms
  210. (defclass |atom-trak| (mp4-container-atom) ())
  211. (defclass |atom-minf| (mp4-container-atom) ())
  212. (defclass |atom-moov| (mp4-container-atom) ())
  213. (defclass |atom-udta| (mp4-container-atom) ())
  214. (defclass |atom-mdia| (mp4-container-atom) ())
  215. (defclass |atom-stbl| (mp4-container-atom) ())
  216. (defclass |atom-hdlr| (mp4-atom)
  217. ((version :accessor version) ; 1 byte
  218. (flags :accessor flags) ; 3 bytes
  219. (qtype :accessor qtype) ; 4 bytes
  220. (mtype :accessor mtype) ; 4 bytes
  221. (resv :accessor resv) ; 4 bytes
  222. (rflag :accessor rflag) ; 4 bytes
  223. (rmask :accessor rmask) ; 4 bytes
  224. (mhdlr :accessor mhdlr))) ; null-terminated string (but we're reading it as octets)
  225. (defmethod initialize-instance :after ((me |atom-hdlr|) &key mp4-file &allow-other-keys)
  226. (declare #.utils:*standard-optimize-settings*)
  227. (with-slots (version flags qtype mtype resv rflag rmask mhdlr atom-size) me
  228. (setf version (stream-read-u8 mp4-file)
  229. flags (stream-read-u24 mp4-file)
  230. qtype (stream-read-u32 mp4-file)
  231. mtype (stream-read-u32 mp4-file)
  232. resv (stream-read-u32 mp4-file)
  233. rflag (stream-read-u32 mp4-file)
  234. rmask (stream-read-u32 mp4-file)
  235. mhdlr (stream-read-sequence mp4-file (- atom-size 32))) ; 32 is 8-bytes of header plus fields above
  236. ))
  237. (defclass |atom-mdhd| (mp4-atom)
  238. ((version :accessor version)
  239. (flags :accessor flags)
  240. (c-time :accessor c-time)
  241. (m-time :accessor m-time)
  242. (scale :accessor scale)
  243. (duration :accessor duration)
  244. (lang :accessor lang)
  245. (quality :accessor quality)))
  246. (defmethod initialize-instance :after ((me |atom-mdhd|) &key mp4-file &allow-other-keys)
  247. (declare #.utils:*standard-optimize-settings*)
  248. (with-slots (version flags c-time m-time scale duration lang quality) me
  249. (setf version (stream-read-u8 mp4-file)
  250. flags (stream-read-u24 mp4-file)
  251. c-time (stream-read-u32 mp4-file)
  252. m-time (stream-read-u32 mp4-file)
  253. scale (stream-read-u32 mp4-file)
  254. duration (if (= 0 version) (stream-read-u32 mp4-file) (stream-read-u64 mp4-file))
  255. lang (stream-read-u16 mp4-file)
  256. quality (stream-read-u16 mp4-file))))
  257. (defclass |atom-esds| (mp4-atom)
  258. ((version :accessor version) ; 1 byte
  259. (flags :accessor flags) ; 3 bytes
  260. (esid :accessor esid) ; 2 bytes
  261. (s-priority :accessor s-priority) ; 1 byte
  262. (obj-id :accessor obj-id) ; 1 byte
  263. (s-type :accessor s-type) ; 1 byte (1 bit up-stream, 1-but reservered, 6-bits stream type
  264. (buf-size :accessor buf-size) ; 3 bytes
  265. (max-bit-rate :accessor max-bit-rate) ; 4 bytes
  266. (avg-bit-rate :accessor avg-bit-rate)) ; 4 bytes
  267. (:documentation "XXX-partial definition for Elementary Stream Descriptors (ESDs)"))
  268. ;;; 3 bytes extended descriptor type tag string = 3 * 8-bit hex value
  269. ;;; types are Start = 0x80 ; End = 0xFE
  270. ;;; then, one byte of length
  271. ;;; Note: start types are optional
  272. (defun read-descriptor-len (instream)
  273. "Get the ES descriptor's length."
  274. (declare #.utils:*standard-optimize-settings*)
  275. (let* ((tmp (stream-read-u8 instream))
  276. (len (logand tmp #x7f)))
  277. (declare (type (unsigned-byte 8) tmp))
  278. (while (not (zerop (logand #x80 tmp)))
  279. (setf tmp (stream-read-u8 instream)
  280. len (logior (ash len 7) (logand tmp #x7f))))
  281. len))
  282. ;;; one-byte descriptor tags
  283. (defconstant* +mp4-odescrtag+ #x01)
  284. (defconstant* +mp4-iodescrtag+ #x02)
  285. (defconstant* +mp4-esdescrtag+ #x03)
  286. (defconstant* +mp4-decconfigdescrtag+ #x04)
  287. (defconstant* +mp4-decspecificdescrtag+ #x05)
  288. (defconstant* +mp4-slconfigdescrtag+ #x06)
  289. (defconstant* +mp4-contentiddescrtag+ #x07)
  290. (defconstant* +mp4-supplcontentiddescrtag+ #x08)
  291. (defconstant* +mp4-ipiptrdescrtag+ #x09)
  292. (defconstant* +mp4-ipmpptrdescrtag+ #x0a)
  293. (defconstant* +mp4-ipmpdescrtag+ #x0b)
  294. (defconstant* +mp4-registrationdescrtag+ #x0d)
  295. (defconstant* +mp4-esidincdescrtag+ #x0e)
  296. (defconstant* +mp4-esidrefdescrtag+ #x0f)
  297. (defconstant* +mp4-fileiodescrtag+ #x10)
  298. (defconstant* +mp4-fileodescrtag+ #x11)
  299. (defconstant* +mp4-extprofileleveldescrtag+ #x13)
  300. (defconstant* +mp4-extdescrtagsstart+ #x80)
  301. (defconstant* +mp4-extdescrtagsend+ #xfe)
  302. (defmethod initialize-instance :after ((me |atom-esds|) &key mp4-file &allow-other-keys)
  303. (declare #.utils:*standard-optimize-settings*)
  304. (with-slots (version flags esid s-priority obj-id s-type buf-size max-bit-rate avg-bit-rate) me
  305. (setf version (stream-read-u8 mp4-file)
  306. flags (stream-read-u24 mp4-file))
  307. (assert (= +MP4-ESDescrTag+ (stream-read-u8 mp4-file)) () "Expected description tag of ESDescrTag")
  308. (let* ((len (read-descriptor-len mp4-file))
  309. (end-of-atom (+ (stream-seek mp4-file) len)))
  310. (setf esid (stream-read-u16 mp4-file)
  311. s-priority (stream-read-u8 mp4-file))
  312. (assert (= +MP4-DecConfigDescrTag+ (stream-read-u8 mp4-file)) () "Expected tag type of DecConfigDescrTag")
  313. (setf len (read-descriptor-len mp4-file)
  314. obj-id (stream-read-u8 mp4-file)
  315. s-type (stream-read-u8 mp4-file)
  316. buf-size (stream-read-u24 mp4-file)
  317. max-bit-rate (stream-read-u32 mp4-file)
  318. avg-bit-rate (stream-read-u32 mp4-file))
  319. ;; Should do checking here and/or read rest of atom,
  320. ;; but for now, we have what we want, so just seek to end of atom
  321. (stream-seek mp4-file end-of-atom :start))))
  322. (defclass |atom-stsd| (mp4-atom)
  323. ((flags :accessor flags)
  324. (version :accessor version)
  325. (num-entries :accessor num-entries)))
  326. (defmethod initialize-instance :after ((me |atom-stsd|) &key mp4-file &allow-other-keys)
  327. (declare #.utils:*standard-optimize-settings*)
  328. (with-slots (flags version num-entries) me
  329. (setf version (stream-read-u8 mp4-file)
  330. flags (stream-read-u24 mp4-file)
  331. num-entries (stream-read-u32 mp4-file))))
  332. (defclass |atom-mp4a| (mp4-container-atom)
  333. ((reserved :accessor reserved) ; 6 bytes
  334. (d-ref-idx :accessor d-ref-idx) ; 2 bytes
  335. (version :accessor version) ; 2 bytes
  336. (revision :accessor revision) ; 2 bytes
  337. (vendor :accessor vendor) ; 4 bytes
  338. (num-chans :accessor num-chans) ; 2 bytes
  339. (samp-size :accessor samp-size) ; 2 bytes
  340. (comp-id :accessor comp-id) ; 2 bytes
  341. (packet-size :accessor packet-size) ; 2 bytes
  342. (samp-rate :accessor samp-rate))) ; 4 bytes
  343. (defmethod initialize-instance :around ((me |atom-mp4a|) &key mp4-file &allow-other-keys)
  344. "Note: this MUST be an AROUND method so that the atom's data can be read in before
  345. reading the container atoms"
  346. (declare #.utils:*standard-optimize-settings*)
  347. (with-slots (reserved d-ref-idx version revision vendor num-chans samp-size comp-id packet-size samp-rate) me
  348. (setf reserved (stream-read-sequence mp4-file 6)
  349. d-ref-idx (stream-read-u16 mp4-file)
  350. version (stream-read-u16 mp4-file)
  351. revision (stream-read-u16 mp4-file)
  352. vendor (stream-read-u32 mp4-file)
  353. num-chans (stream-read-u16 mp4-file)
  354. samp-size (stream-read-u16 mp4-file)
  355. comp-id (stream-read-u16 mp4-file)
  356. packet-size (stream-read-u16 mp4-file)
  357. samp-rate (stream-read-u32 mp4-file))) ; fixed 16.16 floating point number
  358. (call-next-method))
  359. (defclass |atom-meta| (mp4-container-atom)
  360. ((version :accessor version)
  361. (flags :accessor flags)))
  362. (defmethod initialize-instance :around ((me |atom-meta|) &key mp4-file &allow-other-keys)
  363. (declare #.utils:*standard-optimize-settings*)
  364. (with-slots (version flags) me
  365. (setf version (stream-read-u8 mp4-file)
  366. flags (stream-read-u24 mp4-file)))
  367. (call-next-method))
  368. (defun find-atom-class (id)
  369. "Search by concatenating 'atom-' with ID and look for that symbol in this package"
  370. (declare #.utils:*standard-optimize-settings*)
  371. (let ((found-class-symbol (find-symbol (mk-atom-class-name id) :M4A)))
  372. ;; if we found the class name, return the class (to be used for MAKE-INSTANCE)
  373. (when found-class-symbol
  374. (return-from find-atom-class (find-class found-class-symbol)))
  375. ;; didn't find a class, so return ATOM-SKIP class
  376. (warn-user "file ~a~%Unknown atom type <~a> encountered~%"
  377. audio-streams:*current-file* id)
  378. 'atom-skip))
  379. (memoize 'find-atom-class)
  380. (defun make-mp4-atom (mp4-file parent)
  381. "Get current file position, read in size/type, then construct the correct atom."
  382. (declare #.utils:*standard-optimize-settings*)
  383. (let* ((pos (stream-seek mp4-file))
  384. (siz (stream-read-u32 mp4-file))
  385. (typ (as-string (stream-read-u32 mp4-file)))
  386. (atom))
  387. (declare (fixnum pos siz))
  388. (when (= 0 siz)
  389. (error "trying to make an atom ~a with size of 0 at offset ~:d in file ~a"
  390. typ pos (stream-filename mp4-file)))
  391. (setf atom (make-instance (find-atom-class typ)
  392. :atom-size siz
  393. :atom-type typ
  394. :atom-file-pos pos
  395. :parent parent
  396. :mp4-file mp4-file))
  397. atom))
  398. (defmethod vpprint ((me mp4-atom) stream)
  399. (declare #.utils:*standard-optimize-settings*)
  400. (format stream "~a"
  401. (with-output-to-string (s)
  402. (with-mp4-atom-slots (me)
  403. (format s "atom:: type: <~a> @ ~:d of size ~:d"
  404. atom-type atom-file-pos atom-size))
  405. (if (typep me '|atom-data|)
  406. (with-slots (atom-version atom-flags atom-value atom-type) me
  407. (format s ", ilst fields: verison = ~d, flags = ~x, data = ~a"
  408. atom-version atom-flags
  409. (if (typep atom-value 'octets)
  410. (printable-array atom-value)
  411. atom-value)))))))
  412. (defun is-valid-m4-file (mp4-file)
  413. "Make sure this is an MP4 file. Quick check: is first atom (at file-offset 4) == FSTYP?
  414. Written in this fashion so as to be 'crash-proof' when passed an arbitrary file."
  415. (declare #.utils:*standard-optimize-settings*)
  416. (let ((valid)
  417. (size)
  418. (header))
  419. (when (> (stream-size mp4-file) 8)
  420. (stream-seek mp4-file 0 :start)
  421. (setf size (stream-read-u32 mp4-file)
  422. header (as-string (stream-read-u32 mp4-file))
  423. valid (and (<= size (stream-size mp4-file))
  424. (string= header +m4-ftyp+))))
  425. (stream-seek mp4-file 0 :start)
  426. valid))
  427. (defclass mp4-file ()
  428. ((filename :accessor filename :initform nil :initarg :filename
  429. :documentation "filename that was parsed")
  430. (mp4-atoms :accessor mp4-atoms :initform nil
  431. :documentation "holds tree of parsed MP4 atoms/boxes")
  432. (audio-info :accessor audio-info :initform nil
  433. :documentation "holds the bit-rate, etc info"))
  434. (:documentation "Stream for parsing MP4 audio files"))
  435. (defun parse-audio-file (instream &optional (get-audio-info nil))
  436. "Given a valid MP4 file, look for the 'right' atoms and return them."
  437. (declare #.utils:*standard-optimize-settings*)
  438. (stream-seek instream 0 :start)
  439. (let* ((*in-progress* nil) ; dynamic binding for multi-threading
  440. (parsed-info (make-instance 'mp4-file
  441. :filename (stream-filename instream))))
  442. (setf (mp4-atoms parsed-info)
  443. (tree (make-instance 'mp4-container-atom
  444. :atom-type +root+
  445. :atom-file-pos 0
  446. :atom-size (stream-size instream)
  447. :mp4-file instream)))
  448. (when get-audio-info
  449. (setf (audio-info parsed-info) (get-mp4-audio-info parsed-info)))
  450. parsed-info))
  451. (defparameter *ilst-data* (list +root+ +mp4-atom-moov+ +mp4-atom-udta+
  452. +mp4-atom-meta+ +mp4-atom-ilst+ nil)
  453. "iTunes artist/album/etc path. The 5th element should be set to
  454. one of the +iTunes- constants")
  455. (defun tag-get-value (mp4-file atom-type)
  456. "Helper function to extract text from ILST atom's data atom"
  457. (declare #.utils:*standard-optimize-settings*)
  458. (setf (nth 5 *ilst-data*) atom-type)
  459. (aif (tree:at-path (mp4-atoms mp4-file) *ilst-data*
  460. (lambda (x y)
  461. (string= (atom-type (tree:data x)) y)))
  462. (let ((ret))
  463. ;; NB: only the COVR atom can have more than one data atom,
  464. ;; and it can be "data", "name", or "itif"(???).
  465. (loop for e = (tree:first-child it)
  466. then (tree:next-sibling e)
  467. until (null e) do
  468. (when (typep (tree:data e) '|atom-data|)
  469. (push (atom-value (tree:data e)) ret)))
  470. (nreverse ret))
  471. nil))
  472. (defun mp4-show-raw-tag-atoms (mp4-file-stream out-stream)
  473. "Show all the iTunes data atoms"
  474. (declare #.utils:*standard-optimize-settings*)
  475. (let ((top-node
  476. (tree:at-path (mp4-atoms mp4-file-stream)
  477. (list +root+ +mp4-atom-moov+ +mp4-atom-udta+
  478. +mp4-atom-meta+ +mp4-atom-ilst+)
  479. (lambda (x y)
  480. (string= (atom-type (tree:data x)) y)))))
  481. (loop for node = (tree:first-child top-node)
  482. then (tree:next-sibling node) until (null node) do
  483. (format out-stream "~2t~a" (vpprint (tree:data node) nil))
  484. (aif (tree:first-child node)
  485. (format out-stream ", Data ~a~%" (vpprint (tree:data it) nil))
  486. (format out-stream "~%")))))
  487. (defun get-audio-properties-atoms (mp4-file)
  488. "Get the audio property atoms from MP4-FILE.
  489. MP4A audio info is held in under root.moov.trak.mdia.mdhd,
  490. root.moov.trak.mdia.minf.stbl.mp4a, and root.moov.trak.mdia.minf.stbl.mp4a.esds"
  491. (declare #.utils:*standard-optimize-settings*)
  492. (let ((mdhd
  493. (tree:find-tree
  494. (mp4-atoms mp4-file)
  495. (lambda (x)
  496. (string= (atom-type (tree:data x)) +audioprop-mdhd+))))
  497. (mp4a (tree:find-tree
  498. (mp4-atoms mp4-file)
  499. (lambda (x)
  500. (string= (atom-type (tree:data x)) +audioprop-mp4a+))))
  501. (esds (tree:find-tree
  502. (mp4-atoms mp4-file)
  503. (lambda (x)
  504. (string= (atom-type (tree:data x)) +audioprop-esds+)))))
  505. (if (and mdhd mp4a esds)
  506. (values (tree:data (first mdhd))
  507. (tree:data (first mp4a))
  508. (tree:data (first esds)))
  509. nil)))
  510. (defclass audio-info ()
  511. ((seconds :accessor seconds :initform nil)
  512. (channels :accessor channels :initform nil)
  513. (bits-per-sample :accessor bits-per-sample :initform nil)
  514. (sample-rate :accessor sample-rate :initform nil)
  515. (max-bit-rate :accessor max-bit-rate :initform nil)
  516. (avg-bit-rate :accessor avg-bit-rate :initform nil))
  517. (:documentation "Holds extracted audio information about an MP4 file."))
  518. (defmethod vpprint ((me audio-info) stream)
  519. (declare #.utils:*standard-optimize-settings*)
  520. (with-slots (seconds channels bits-per-sample sample-rate max-bit-rate avg-bit-rate) me
  521. (format stream "sample rate: ~:d Hz, # channels: ~d, bits-per-sample: ~:d, max bit-rate: ~:d Kbps, avg bit-rate: ~:d Kbps, duration: ~:d:~2,'0d"
  522. (if sample-rate sample-rate 0)
  523. (if channels channels 0)
  524. (if bits-per-sample bits-per-sample 0)
  525. (if max-bit-rate (round (/ max-bit-rate 1000)) 0)
  526. (if avg-bit-rate (round (/ avg-bit-rate 1000)) 0)
  527. (if seconds (floor (/ seconds 60)) 0)
  528. (if seconds (round (mod seconds 60)) 0))))
  529. (defun get-mp4-audio-info (mp4-file)
  530. "Find and parse the audio information in MP4-FILE"
  531. (declare #.utils:*standard-optimize-settings*)
  532. (let ((info (make-instance 'audio-info)))
  533. (multiple-value-bind (mdhd mp4a esds) (get-audio-properties-atoms mp4-file)
  534. (with-slots (seconds channels bits-per-sample sample-rate max-bit-rate avg-bit-rate) info
  535. (when mdhd
  536. (setf seconds (/ (float (duration mdhd)) (float (scale mdhd)))))
  537. (when mp4a
  538. (setf channels (num-chans mp4a)
  539. bits-per-sample (samp-size mp4a))
  540. (let* ((upper (ash (samp-rate mp4a) -16))
  541. (lower (logand (samp-rate mp4a) #xffff)))
  542. (setf sample-rate (+ (float upper) (/ (float lower) 1000))))
  543. (when esds
  544. (setf avg-bit-rate (avg-bit-rate esds)
  545. max-bit-rate (max-bit-rate esds))))))
  546. info))
  547. (defun map-mp4-atoms (m4a &key (func (constantly t)))
  548. "Visit each atom we found in M4A"
  549. (declare #.utils:*standard-optimize-settings*)
  550. (tree:traverse
  551. (m4a:mp4-atoms m4a)
  552. (lambda (node depth)
  553. (declare (ignore depth))
  554. (funcall func (tree:data node)))))