mpeg.lisp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. ;;; -*- Mode: Lisp; show-trailing-whitespace: t; Base: 10; indent-tabs: nil; Syntax: ANSI-Common-Lisp; Package: MPEG; -*-
  2. ;;; Copyright (c) 2013, Mark VandenBrink. All rights reserved.
  3. ;;; Parsing MPEG audio frames. See http://www.datavoyage.com/mpgscript/mpeghdr.htm for format of a frame.
  4. (in-package #:mpeg)
  5. (log5:defcategory cat-log-mpeg-frame)
  6. (defmacro log-mpeg-frame (&rest log-stuff) `(log5:log-for (cat-log-mpeg-frame) ,@log-stuff))
  7. (defconstant +sync-word+ #x7ff "NB: this is 11 bits so as to be able to recognize V2.5")
  8. ;;; the versions
  9. (defconstant +mpeg-2.5+ 0)
  10. (defconstant +v-reserved+ 1)
  11. (defconstant +mpeg-2+ 2)
  12. (defconstant +mpeg-1+ 3)
  13. (defun valid-version (version)
  14. (declare #.utils:*standard-optimize-settings*)
  15. (or ;; can't deal with 2.5's yet (= (the fixnum +mpeg-2.5+) (the fixnum version))
  16. (= (the fixnum +mpeg-2+) (the fixnum version))
  17. (= (the fixnum +mpeg-1+) (the fixnum version))))
  18. (defun get-mpeg-version-string (version)
  19. (declare #.utils:*standard-optimize-settings*)
  20. (nth version '("MPEG 2.5" "Reserved" "MPEG 2" "MPEG 1")))
  21. ;;; the layers
  22. (defconstant +layer-reserved+ 0)
  23. (defconstant +layer-3+ 1)
  24. (defconstant +layer-2+ 2)
  25. (defconstant +layer-1+ 3)
  26. (defun valid-layer (layer)
  27. (declare #.utils:*standard-optimize-settings*)
  28. (or (= (the fixnum +layer-3+) (the fixnum layer))
  29. (= (the fixnum +layer-2+) (the fixnum layer))
  30. (= (the fixnum +layer-1+) (the fixnum layer))))
  31. (defun get-layer-string (layer)
  32. (declare #.utils:*standard-optimize-settings*)
  33. (nth layer '("Reserved" "Layer III" "Layer II" "Layer I")))
  34. ;;; the modes
  35. (defconstant +channel-mode-stereo+ 0)
  36. (defconstant +channel-mode-joint+ 1)
  37. (defconstant +channel-mode-dual+ 2)
  38. (defconstant +channel-mode-mono+ 3)
  39. (defun get-channel-mode-string (mode)
  40. (declare #.utils:*standard-optimize-settings*)
  41. (nth mode '("Stereo" "Joint" "Dual" "Mono")))
  42. ;;; the emphases
  43. (defconstant +emphasis-none+ 0)
  44. (defconstant +emphasis-50-15+ 1)
  45. (defconstant +emphasis-reserved+ 2)
  46. (defconstant +emphasis-ccit+ 3)
  47. (defun get-emphasis-string (e)
  48. (declare #.utils:*standard-optimize-settings*)
  49. (nth e '("None" "50/15 ms" "Reserved" "CCIT J.17")))
  50. (defun valid-emphasis (e)
  51. (declare #.utils:*standard-optimize-settings*)
  52. (or (= (the fixnum e) (the fixnum +emphasis-none+))
  53. (= (the fixnum e) (the fixnum +emphasis-50-15+))
  54. (= (the fixnum e) (the fixnum +emphasis-ccit+))))
  55. ;;; the modes
  56. (defconstant +mode-extension-0+ 0)
  57. (defconstant +mode-extension-1+ 1)
  58. (defconstant +mode-extension-2+ 2)
  59. (defconstant +mode-extension-3+ 3)
  60. (defun get-mode-extension-string (channel-mode layer mode-extension)
  61. (declare #.utils:*standard-optimize-settings*)
  62. (if (not (= channel-mode +channel-mode-joint+))
  63. ""
  64. (if (or (= layer +layer-1+)
  65. (= layer +layer-2+))
  66. (format nil "Bands ~[4~;8~;12~;16~] to 31" mode-extension)
  67. (format nil "Intensity Stereo: ~[off~;on~], MS Stereo: ~[off~;on~]" (ash mode-extension -1) (logand mode-extension 1)))))
  68. (defun get-samples-per-frame (version layer)
  69. (declare #.utils:*standard-optimize-settings*)
  70. (cond ((= (the fixnum layer) (the fixnum +layer-1+)) 384)
  71. ((= (the fixnum layer) (the fixnum +layer-2+)) 1152)
  72. ((= (the fixnum layer) (the fixnum +layer-3+))
  73. (cond ((= (the fixnum version) +mpeg-1+) 1152)
  74. ((or (= (the fixnum version) (the fixnum +mpeg-2+))
  75. (= (the fixnum version) (the fixnum +mpeg-2.5+))) 576)))))
  76. (defclass frame ()
  77. ((pos :accessor pos :initarg :pos)
  78. (hdr-u32 :accessor hdr-u32 :initarg :hdr-u32)
  79. (samples :accessor samples :initarg :samples)
  80. (sync :accessor sync :initarg :sync)
  81. (version :accessor version :initarg :version)
  82. (layer :accessor layer :initarg :layer)
  83. (protection :accessor protection :initarg :protection)
  84. (bit-rate :accessor bit-rate :initarg :bit-rate)
  85. (sample-rate :accessor sample-rate :initarg :sample-rate)
  86. (padded :accessor padded :initarg :padded)
  87. (private :accessor private :initarg :private)
  88. (channel-mode :accessor channel-mode :initarg :channel-mode)
  89. (mode-extension :accessor mode-extension :initarg :mode-extension)
  90. (copyright :accessor copyright :initarg :copyright)
  91. (original :accessor original :initarg :original)
  92. (emphasis :accessor emphasis :initarg :emphasis)
  93. (size :accessor size :initarg :size)
  94. (vbr :accessor vbr :initarg :vbr)
  95. (payload :accessor payload :initarg :payload))
  96. (:documentation "Data in and associated with an MPEG audio frame.")
  97. (:default-initargs :pos nil :hdr-u32 nil :samples 0 :sync 0 :version 0 :layer 0 :protection 0 :bit-rate 0
  98. :sample-rate 0 :padded 0 :private 0 :channel-mode 0 :mode-extension 0
  99. :copyright 0 :original 0 :emphasis 0 :size nil :vbr nil :payload nil))
  100. (defmacro with-frame-slots ((instance) &body body)
  101. `(with-slots (pos hdr-u32 samples sync version layer protection bit-rate sample-rate
  102. padded private channel-mode mode-extension copyright
  103. original emphasis size vbr payload) ,instance
  104. ,@body))
  105. (let ((bit-array-table
  106. (make-array '(14 5) :initial-contents
  107. '((32 32 32 32 8)
  108. (64 48 40 48 16)
  109. (96 56 48 56 24)
  110. (128 64 56 64 32)
  111. (160 80 64 80 40)
  112. (192 96 80 96 48)
  113. (224 112 96 112 56)
  114. (256 128 112 128 64)
  115. (288 160 128 144 80)
  116. (320 192 160 160 96)
  117. (352 224 192 176 112)
  118. (384 256 224 192 128)
  119. (416 320 256 224 144)
  120. (448 384 320 256 160)))))
  121. (defun valid-bit-rate-index (br-index)
  122. (declare #.utils:*standard-optimize-settings*)
  123. (and (> (the fixnum br-index) 0) (< (the fixnum br-index) 15)))
  124. (defun get-bit-rate (version layer bit-rate-index)
  125. (declare #.utils:*standard-optimize-settings*)
  126. (log5:with-context "get-bit-rate"
  127. (log-mpeg-frame "version = ~d, layer = ~d, bit-rate-index = ~d" version layer bit-rate-index)
  128. (let ((row (1- bit-rate-index))
  129. (col (cond ((= (the fixnum version) (the fixnum +mpeg-1+))
  130. (cond ((= (the fixnum layer) (the fixnum +layer-1+)) 0)
  131. ((= (the fixnum layer) (the fixnum +layer-2+)) 1)
  132. ((= (the fixnum layer) (the fixnum +layer-3+)) 2)
  133. (t nil)))
  134. ((= (the fixnum version) (the fixnum +mpeg-2+))
  135. (cond ((= (the fixnum layer) (the fixnum +layer-1+)) 3)
  136. ((= (the fixnum layer) (the fixnum +layer-2+)) 4)
  137. ((= (the fixnum layer) (the fixnum +layer-3+)) 4)
  138. (t nil)))
  139. (t (error "don't support MPEG 2.5 yet")))))
  140. (log-mpeg-frame "version = ~d, row = ~d, col = ~d" version row col)
  141. (if (or (null col) (< row 0) (> row 14))
  142. nil
  143. (let ((ret (* 1000 (aref bit-array-table row col))))
  144. (log-mpeg-frame "returning ~:d" ret)
  145. ret))))))
  146. (defun valid-sample-rate-index (sr-index)
  147. (declare #.utils:*standard-optimize-settings*)
  148. (and (>= (the fixnum sr-index) 0)
  149. (< (the fixnum sr-index) 3)))
  150. (defun get-sample-rate (version sr-index)
  151. (declare #.utils:*standard-optimize-settings*)
  152. (cond ((= (the fixnum version) (the fixnum +mpeg-1+))
  153. (case (the fixnum sr-index) (0 44100) (1 48000) (2 32000)))
  154. ((= (the fixnum version) (the fixnum +mpeg-2+))
  155. (case (the fixnum sr-index) (0 22050) (1 24000) (2 16000)))
  156. (t nil)))
  157. (defun get-frame-size (version layer bit-rate sample-rate padded)
  158. (declare #.utils:*standard-optimize-settings*)
  159. (truncate (float (cond ((= (the fixnum layer) (the fixnum +layer-1+))
  160. (* 4 (+ (/ (* 12 bit-rate) sample-rate) padded)))
  161. ((= (the fixnum layer) (the fixnum +layer-2+))
  162. (+ (* 144 (/ bit-rate sample-rate)) padded))
  163. ((= (the fixnum layer) (the fixnum +layer-3+))
  164. (if (= (the fixnum version) (the fixnum +mpeg-1+))
  165. (+ (* 144 (/ bit-rate sample-rate)) padded)
  166. (+ (* 72 (/ bit-rate sample-rate)) padded)))))))
  167. (defmethod load-frame ((me frame) &key instream (read-payload nil))
  168. "Load an MPEG frame from current file position. If READ-PAYLOAD is set, read in frame's content."
  169. (declare #.utils:*standard-optimize-settings*)
  170. (log5:with-context "load-frame"
  171. (handler-case
  172. (with-frame-slots (me)
  173. (log-mpeg-frame "loading frame from pos ~:d" (stream-here instream))
  174. (when (null hdr-u32) ; has header already been read in?
  175. (log-mpeg-frame "reading in header")
  176. (setf pos (stream-here instream)
  177. hdr-u32 (stream-read-u32 instream))
  178. (when (null hdr-u32)
  179. (log-mpeg-frame "hit EOF")
  180. (return-from load-frame nil)))
  181. (if (parse-header me)
  182. (progn
  183. (log-mpeg-frame "header parsed ok")
  184. (setf size (get-frame-size version layer bit-rate sample-rate padded))
  185. (when read-payload
  186. (setf payload (stream-read-sequence instream (- size 4))))
  187. t)
  188. (progn
  189. (log-mpeg-frame "header didn't parse!")
  190. nil)))
  191. (end-of-file (c)
  192. (declare (ignore c))
  193. (log-mpeg-frame "Hit EOF")
  194. nil))))
  195. (defmethod parse-header ((me frame))
  196. "Given a frame, verify that is a valid MPEG audio frame by examining the header.
  197. A header looks like this:
  198. Bits 31-21 (11 bits): the sync word. Must be #xffe (NB version 2.5 standard)
  199. Bits 20-19 (2 bits): the version
  200. Bits 18-17 (2 bits): the layer
  201. Bit 16 (1 bit ): the protection bit
  202. Bits 15-12 (4 bits): the bit-rate index
  203. Bits 11-10 (2 bits): the sample-rate index
  204. Bit 9 (1 bit ): the padding bit
  205. Bit 8 (1 bit ): the private bit
  206. Bits 7-6 (2 bits): the channel mode
  207. Bits 5-4 (2 bits): the mode extension
  208. Bit 3 (1 bit ): the copyright bit
  209. Bit 2 (1 bit ): the original bit
  210. Bits 1-0 (2 bits): the emphasis"
  211. (declare #.utils:*standard-optimize-settings*)
  212. (log5:with-context "parse-header"
  213. (with-frame-slots (me)
  214. ;; check sync word
  215. (setf sync (get-bitfield hdr-u32 31 11))
  216. (when (not (= sync +sync-word+))
  217. (log-mpeg-frame "bad sync ~x/~x" sync hdr-u32)
  218. (return-from parse-header nil))
  219. ;; check version
  220. (setf version (get-bitfield hdr-u32 20 2))
  221. (when (not (valid-version version))
  222. (log-mpeg-frame "bad version ~d" version)
  223. (return-from parse-header nil))
  224. ;; check layer
  225. (setf layer (get-bitfield hdr-u32 18 2))
  226. (when (not (valid-layer layer))
  227. (log-mpeg-frame "bad layer ~d" layer)
  228. (return-from parse-header nil))
  229. (setf protection (get-bitfield hdr-u32 16 1)
  230. samples (get-samples-per-frame version layer))
  231. ;; check bit-rate
  232. (let ((br-index (get-bitfield hdr-u32 15 4)))
  233. (when (not (valid-bit-rate-index br-index))
  234. (log-mpeg-frame "bad bit-rate index ~d" br-index)
  235. (return-from parse-header nil))
  236. (setf bit-rate (get-bit-rate version layer br-index)))
  237. ;; check sample rate
  238. (let ((sr-index (get-bitfield hdr-u32 11 2)))
  239. (when (not (valid-sample-rate-index sr-index))
  240. (log-mpeg-frame "bad sample-rate index ~d" sr-index)
  241. (return-from parse-header nil))
  242. (setf sample-rate (get-sample-rate version sr-index)))
  243. (setf padded (get-bitfield hdr-u32 9 1)
  244. private (get-bitfield hdr-u32 8 1)
  245. channel-mode (get-bitfield hdr-u32 7 2)
  246. mode-extension (get-bitfield hdr-u32 5 2)
  247. copyright (get-bitfield hdr-u32 3 1)
  248. original (get-bitfield hdr-u32 2 1)
  249. emphasis (get-bitfield hdr-u32 1 2))
  250. ;; check emphasis
  251. (when (not (valid-emphasis emphasis))
  252. (log-mpeg-frame "bad emphasis ~d" emphasis)
  253. (return-from parse-header nil))
  254. (log-mpeg-frame "good parse: ~a" me)
  255. t)))
  256. (defmethod vpprint ((me frame) stream)
  257. (format stream "~a"
  258. (with-output-to-string (s)
  259. (with-frame-slots (me)
  260. (format s "MPEG Frame: position in file = ~:d, header in (hex) bytes = ~x, size = ~d, sync word = ~x, " pos hdr-u32 size sync)
  261. (when vbr
  262. (format s "~&vbr-info: ~a~%" vbr))
  263. (format s "version = ~a, layer = ~a, crc protected? = ~[yes~;no~], bit-rate = ~:d bps, sampling rate = ~:d bps, padded? = ~[no~;yes~], private bit set? = ~[no~;yes~], channel mode = ~a, "
  264. (get-mpeg-version-string version) (get-layer-string layer)
  265. protection bit-rate sample-rate padded private (get-channel-mode-string channel-mode))
  266. (format s "mode extension = ~a, copyrighted? = ~[no~;yes~], original? = ~[no~;yes~], emphasis = ~a"
  267. (get-mode-extension-string channel-mode layer mode-extension) copyright original (get-emphasis-string emphasis))
  268. (when payload
  269. (format s "~%frame payload[~:d] = ~a~%" (length payload) (utils:printable-array payload)))))))
  270. (defclass vbr-info ()
  271. ((tag :accessor tag :initarg :tag)
  272. (flags :accessor flags :initarg :flags)
  273. (frames :accessor frames :initarg :frames)
  274. (bytes :accessor bytes :initarg :bytes)
  275. (tocs :accessor tocs :initarg :tocs)
  276. (scale :accessor scale :initarg :scale))
  277. (:default-initargs :tag nil :flags 0 :frames nil :bytes nil :tocs nil :scale nil))
  278. (defmacro with-vbr-info-slots ((instance) &body body)
  279. `(with-slots (tag flags frames bytes tocs scale) ,instance
  280. ,@body))
  281. (defconstant +vbr-frames+ 1)
  282. (defconstant +vbr-bytes+ 2)
  283. (defconstant +vbr-tocs+ 4)
  284. (defconstant +vbr-scale+ 8)
  285. (defun get-side-info-size (version channel-mode)
  286. (declare #.utils:*standard-optimize-settings*)
  287. (cond ((= (the fixnum version) (the fixnum +mpeg-1+))
  288. (cond ((= (the fixnum channel-mode) (the fixnum +channel-mode-mono+)) 17)
  289. (t 32)))
  290. (t (cond ((= (the fixnum channel-mode) (the fixnum +channel-mode-mono+)) 9)
  291. (t 17)))))
  292. (defmethod check-vbr ((me frame) fn)
  293. (declare #.utils:*standard-optimize-settings*)
  294. (log5::with-context "check-vbr"
  295. (with-frame-slots (me)
  296. (let ((i (get-side-info-size version channel-mode)))
  297. (log-mpeg-frame "array index = ~d, payload size = ~d" i (length payload))
  298. (when (>= i (length payload))
  299. (return-from check-vbr nil))
  300. (when (or (and (= (aref payload (+ i 0)) (char-code #\X))
  301. (= (aref payload (+ i 1)) (char-code #\i))
  302. (= (aref payload (+ i 2)) (char-code #\n))
  303. (= (aref payload (+ i 3)) (char-code #\g)))
  304. (and (= (aref payload (+ i 0)) (char-code #\I))
  305. (= (aref payload (+ i 1)) (char-code #\n))
  306. (= (aref payload (+ i 2)) (char-code #\f))
  307. (= (aref payload (+ i 3)) (char-code #\o))))
  308. (log-mpeg-frame "found xing/info: ~c ~c ~c ~c"
  309. (code-char (aref payload (+ i 0)))
  310. (code-char (aref payload (+ i 1)))
  311. (code-char (aref payload (+ i 2)))
  312. (code-char (aref payload (+ i 3))))
  313. (setf vbr (make-instance 'vbr-info))
  314. (let ((v (make-mem-stream (payload me))))
  315. (stream-seek v i :start) ; seek to Xing/Info offset
  316. (setf (tag vbr) (stream-read-iso-string-with-len v 4)
  317. (flags vbr) (stream-read-u32 v))
  318. (when (logand (flags vbr) +vbr-frames+)
  319. (setf (frames vbr) (stream-read-u32 v))
  320. (log-mpeg-frame "Xing frames set: read ~d" (frames vbr))
  321. ;; some VBR files have the Xing/Info header, but it is not correctly formulated.
  322. ;; just warn the user.
  323. (when (zerop (frames vbr))
  324. (warn-user "file ~a Xing/Info header flags has FRAMES set, but field is zero." fn)))
  325. (when (logand (flags vbr) +vbr-bytes+)
  326. (setf (bytes vbr) (stream-read-u32 v))
  327. (log-mpeg-frame "Xing bytes set: read ~d" (bytes vbr)))
  328. (when (logand (flags vbr) +vbr-tocs+)
  329. (setf (tocs vbr) (stream-read-sequence v 100))
  330. (log-mpeg-frame "Xing tocs set: read ~a" (tocs vbr)))
  331. (when (logand (flags vbr) +vbr-scale+)
  332. (setf (scale vbr) (stream-read-u32 v))
  333. (log-mpeg-frame "Xing scale set: read ~d" (scale vbr)))
  334. (log-mpeg-frame "vbr-info = ~a" (vpprint vbr nil))))))))
  335. (defmethod vpprint ((me vbr-info) stream)
  336. (with-vbr-info-slots (me)
  337. (format stream "tag = ~a, flags = 0x~x, frame~p = ~:d, bytes = ~:d, tocs = ~d, scale = ~d, "
  338. tag flags frames frames bytes tocs scale)))
  339. (defun find-first-sync (in)
  340. (declare #.utils:*standard-optimize-settings*)
  341. (log5:with-context "find-first-sync"
  342. (log-mpeg-frame "Looking for first sync, begining at file position ~:d" (stream-here in))
  343. (let ((hdr-u32)
  344. (count 0)
  345. (pos))
  346. (handler-case
  347. (loop
  348. (setf pos (stream-here in)
  349. hdr-u32 (stream-read-u32 in))
  350. (when (null hdr-u32)
  351. (return-from find-first-sync nil))
  352. (incf count)
  353. (when (= (logand hdr-u32 #xffe00000) #xffe00000) ; magic number is potential sync frame header
  354. (log-mpeg-frame "Potential sync bytes at ~:d: <~x>" pos hdr-u32)
  355. (let ((hdr (make-instance 'frame :hdr-u32 hdr-u32 :pos pos)))
  356. (if (load-frame hdr :instream in :read-payload t)
  357. (progn
  358. (check-vbr hdr (stream-filename in))
  359. (log-mpeg-frame "Valid header being returned: ~a, searched ~:d times" hdr count)
  360. (return-from find-first-sync hdr))
  361. (progn
  362. (log-mpeg-frame "hdr wasn't valid: ~a" hdr))))))
  363. (condition (c) (progn
  364. (warn-user "Condtion <~a> signaled while looking for first sync" c)
  365. (log-mpeg-frame "got a condition while looking for first sync: ~a" c)
  366. (error c))))
  367. nil)))
  368. (defmethod next-frame ((me frame) &key instream read-payload)
  369. "Get next frame. If READ-PAYLOAD is true, read in contents for frame, else, seek to next frame header."
  370. (declare #.utils:*standard-optimize-settings*)
  371. (log5:with-context "next-frame"
  372. (let ((nxt-frame (make-instance 'frame)))
  373. (when (not (payload me))
  374. (log-mpeg-frame "no payload load required in current frame, skipping from ~:d forward ~:d bytes"
  375. (stream-here instream)
  376. (- (size me) 4) :current)
  377. (stream-seek instream (- (size me) 4) :current))
  378. (log-mpeg-frame "at pos ~:d, read-payload is ~a" (stream-here instream) read-payload)
  379. (if (load-frame nxt-frame :instream instream :read-payload read-payload)
  380. nxt-frame
  381. nil))))
  382. (defparameter *max-frames-to-read* most-positive-fixnum "when trying to determine bit-rate, etc, read at most this many frames")
  383. (defun map-frames (in func &key (start-pos nil) (read-payload nil) (max nil))
  384. "Loop through the MPEG audio frames in a file. If *MAX-FRAMES-TO-READ* is set, return after reading that many frames."
  385. (declare #.utils:*standard-optimize-settings*)
  386. (log5:with-context "map-frames"
  387. (log-mpeg-frame "mapping frames, start pos ~:d" start-pos)
  388. (when start-pos
  389. (stream-seek in start-pos :start))
  390. (loop
  391. for max-frames = (if max max *max-frames-to-read*)
  392. for count = 0 then (incf count)
  393. for frame = (find-first-sync in) then (next-frame frame :instream in :read-payload read-payload)
  394. while (and frame (< count max-frames)) do
  395. (log-mpeg-frame "map-frames: at pos ~:d, dispatching function" (pos frame))
  396. (funcall func frame))))
  397. (defclass mpeg-audio-info ()
  398. ((is-vbr :accessor is-vbr :initarg :is-vbr :initform nil)
  399. (n-frames :accessor n-frames :initarg :n-frames :initform 0)
  400. (bit-rate :accessor bit-rate :initarg :bit-rate :initform nil)
  401. (sample-rate :accessor sample-rate :initarg :sample-rate :initform nil)
  402. (len :accessor len :initarg :len :initform nil)
  403. (version :accessor version :initarg :version :initform nil)
  404. (layer :accessor layer :initarg :layer :initform nil)))
  405. (defmethod vpprint ((me mpeg-audio-info) stream)
  406. (with-slots (is-vbr sample-rate bit-rate len version layer n-frames) me
  407. (format stream "~:d frame~p read, ~a, ~a, ~:[CBR,~;VBR,~] sample rate: ~:d Hz, bit rate: ~:d Kbps, duration: ~:d:~2,'0d"
  408. n-frames n-frames
  409. (get-mpeg-version-string version)
  410. (get-layer-string layer)
  411. is-vbr
  412. sample-rate
  413. (round (/ bit-rate 1000))
  414. (floor (/ len 60)) (round (mod len 60)))))
  415. (defun calc-bit-rate-exhaustive (in start info)
  416. "Map every MPEG frame in IN and calculate the bit-rate"
  417. (declare #.utils:*standard-optimize-settings*)
  418. (log5:with-context "calc-bit-rate-exhaustive"
  419. (let ((total-len 0)
  420. (last-bit-rate nil)
  421. (bit-rate-total 0)
  422. (vbr nil))
  423. (log-mpeg-frame "broken Xing/Info header found, reading all frames")
  424. (with-slots (is-vbr sample-rate bit-rate len version layer n-frames) info
  425. (map-frames in (lambda (f)
  426. (incf n-frames)
  427. (incf total-len (float (/ (samples f) (sample-rate f))))
  428. (incf bit-rate-total (bit-rate f))
  429. (if (null last-bit-rate)
  430. (setf last-bit-rate (bit-rate f))
  431. (progn
  432. (when (not (= last-bit-rate (bit-rate f)))
  433. (setf vbr t))
  434. (setf last-bit-rate (bit-rate f)))))
  435. :read-payload nil :start-pos start)
  436. (log-mpeg-frame "finished mapping. read ~:d frames" n-frames)
  437. (when (or (< n-frames 10) (zerop bit-rate-total))
  438. (log-mpeg-frame "couldn't get audio-info: only got ~d frames" n-frames)
  439. (return-from calc-bit-rate-exhaustive))
  440. (setf is-vbr t
  441. len total-len
  442. bit-rate (float (/ bit-rate-total n-frames)))
  443. (log-mpeg-frame "len = ~:d, bit-rate = ~f" len bit-rate)))))
  444. (defun get-mpeg-audio-info (in &key) ;; (max-frames *max-frames-to-read*))
  445. "Get MPEG Layer 3 audio information.
  446. If the first MPEG frame we find is a Xing/Info header, return that as info.
  447. Else, we assume CBR and calculate the duration, etc."
  448. (declare #.utils:*standard-optimize-settings*)
  449. (log5:with-context "get-mpeg-audio-info"
  450. (let ((first-frame (find-first-sync in))
  451. (info (make-instance 'mpeg-audio-info)))
  452. (log-mpeg-frame "search for first frame yielded ~a" (vpprint first-frame nil))
  453. (when (null first-frame)
  454. (return-from get-mpeg-audio-info nil))
  455. (with-slots (is-vbr sample-rate bit-rate len version layer n-frames) info
  456. (setf version (version first-frame)
  457. layer (layer first-frame)
  458. sample-rate (sample-rate first-frame))
  459. (if (vbr first-frame)
  460. ;; found a Xing header, now check to see if it is correct
  461. (if (zerop (frames (vbr first-frame)))
  462. (calc-bit-rate-exhaustive in (pos first-frame) info) ; Xing header broken, read all frames to calc
  463. ;; Good Xing header, use info in VBR to calc
  464. (setf n-frames 1
  465. is-vbr t
  466. len (float (* (frames (vbr first-frame)) (/ (samples first-frame) (sample-rate first-frame))))
  467. bit-rate (float (/ (* 8 (bytes (vbr first-frame))) len))))
  468. ;; No Xing header found. Assume CBR and calculate based on first frame
  469. (let* ((first (pos first-frame))
  470. (last (- (audio-streams:stream-size in) (if (id3-frame::v21-tag-header (id3-header in)) 128 0)))
  471. (n-fr (round (/ (float (- last first)) (float (size first-frame)))))
  472. (n-sec (round (/ (float (* (size first-frame) n-fr)) (float (* 125 (float (/ (bit-rate first-frame) 1000))))))))
  473. (setf is-vbr nil
  474. n-frames 1
  475. len n-sec
  476. bit-rate (float (bit-rate first-frame))))))
  477. info)))