mpeg.lisp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. (declare #.utils:*standard-optimize-settings*)
  171. (log5:with-context "load-frame"
  172. (handler-case
  173. (with-frame-slots (me)
  174. (log-mpeg-frame "loading frame from pos ~:d" (stream-seek instream))
  175. (when (null hdr-u32) ; has header already been read in?
  176. (log-mpeg-frame "reading in header")
  177. (setf pos (stream-seek instream))
  178. (setf hdr-u32 (stream-read-u32 instream))
  179. (when (null hdr-u32)
  180. (log-mpeg-frame "hit EOF")
  181. (return-from load-frame nil)))
  182. (if (parse-header me)
  183. (progn
  184. (log-mpeg-frame "header parsed ok")
  185. (setf size (get-frame-size version layer bit-rate sample-rate padded))
  186. (when read-payload
  187. (setf payload (stream-read-sequence instream (- size 4))))
  188. t)
  189. (progn
  190. (log-mpeg-frame "header didn't parse!")
  191. nil)))
  192. (end-of-file (c)
  193. (declare (ignore c))
  194. (log-mpeg-frame "Hit EOF")
  195. nil))))
  196. (defmethod parse-header ((me frame))
  197. "Given a frame, verify that is a valid MPEG audio frame by examining the header.
  198. A header looks like this:
  199. Bits 31-21 (11 bits): the sync word. Must be #xffe (NB version 2.5 standard)
  200. Bits 20-19 (2 bits): the version
  201. Bits 18-17 (2 bits): the layer
  202. Bit 16 (1 bit ): the protection bit
  203. Bits 15-12 (4 bits): the bit-rate index
  204. Bits 11-10 (2 bits): the sample-rate index
  205. Bit 9 (1 bit ): the padding bit
  206. Bit 8 (1 bit ): the private bit
  207. Bits 7-6 (2 bits): the channel mode
  208. Bits 5-4 (2 bits): the mode extension
  209. Bit 3 (1 bit ): the copyright bit
  210. Bit 2 (1 bit ): the original bit
  211. Bits 1-0 (2 bits): the emphasis"
  212. (declare #.utils:*standard-optimize-settings*)
  213. (log5:with-context "parse-header"
  214. (with-frame-slots (me)
  215. ;; check sync word
  216. (setf sync (get-bitfield hdr-u32 31 11))
  217. ;(setf (ldb (byte 8 8) sync) (ldb (byte 8 24) hdr-u32))
  218. ;(setf (ldb (byte 3 5) sync) (ldb (byte 3 5) (ldb (byte 8 16) hdr-u32)))
  219. (when (not (= sync +sync-word+))
  220. (log-mpeg-frame "bad sync ~x/~x" sync hdr-u32)
  221. (return-from parse-header nil))
  222. ;; check version
  223. ;(setf version (ldb (byte 2 3) (ldb (byte 8 16) hdr-u32)))
  224. (setf version (get-bitfield hdr-u32 20 2))
  225. (when (not (valid-version version))
  226. (log-mpeg-frame "bad version ~d" version)
  227. (return-from parse-header nil))
  228. ;; check layer
  229. ;(setf layer (ldb (byte 2 1) (ldb (byte 8 16) hdr-u32)))
  230. (setf layer (get-bitfield hdr-u32 18 2))
  231. (when (not (valid-layer layer))
  232. (log-mpeg-frame "bad layer ~d" layer)
  233. (return-from parse-header nil))
  234. ;(setf protection (ldb (byte 1 0) (ldb (byte 8 16) hdr-u32)))
  235. (setf protection (get-bitfield hdr-u32 16 1))
  236. (setf samples (get-samples-per-frame version layer))
  237. ;; check bit-rate
  238. ;(let ((br-index (the fixnum (ldb (byte 4 4) (ldb (byte 8 8) hdr-u32)))))
  239. (let ((br-index (get-bitfield hdr-u32 15 4)))
  240. (when (not (valid-bit-rate-index br-index))
  241. (log-mpeg-frame "bad bit-rate index ~d" br-index)
  242. (return-from parse-header nil))
  243. (setf bit-rate (get-bit-rate version layer br-index)))
  244. ;; check sample rate
  245. ;(let ((sr-index (the fixnum (ldb (byte 2 2) (ldb (byte 8 8) hdr-u32)))))
  246. (let ((sr-index (get-bitfield hdr-u32 11 2)))
  247. (when (not (valid-sample-rate-index sr-index))
  248. (log-mpeg-frame "bad sample-rate index ~d" sr-index)
  249. (return-from parse-header nil))
  250. (setf sample-rate (get-sample-rate version sr-index)))
  251. ;(setf padded (ldb (byte 1 1) (ldb (byte 8 8) hdr-u32)))
  252. (setf padded (get-bitfield hdr-u32 9 1))
  253. ;(setf private (ldb (byte 1 0) (ldb (byte 8 8) hdr-u32)))
  254. (setf private (get-bitfield hdr-u32 8 1))
  255. ;(setf channel-mode (ldb (byte 2 6) (ldb (byte 8 0) hdr-u32)))
  256. (setf channel-mode (get-bitfield hdr-u32 7 2))
  257. ;(setf mode-extension (ldb (byte 2 4) (ldb (byte 8 0) hdr-u32)))
  258. (setf mode-extension (get-bitfield hdr-u32 5 2))
  259. ;(setf copyright (ldb (byte 1 3) (ldb (byte 8 0) hdr-u32)))
  260. (setf copyright (get-bitfield hdr-u32 3 1))
  261. ;(setf original (ldb (byte 1 2) (ldb (byte 8 0) hdr-u32)))
  262. (setf original (get-bitfield hdr-u32 2 1))
  263. ;(setf emphasis (ldb (byte 2 0) (ldb (byte 8 0) hdr-u32)))
  264. (setf emphasis (get-bitfield hdr-u32 1 2))
  265. ;; check emphasis
  266. (when (not (valid-emphasis emphasis))
  267. (log-mpeg-frame "bad emphasis ~d" emphasis)
  268. (return-from parse-header nil))
  269. (log-mpeg-frame "good parse: ~a" me)
  270. t)))
  271. (defmethod vpprint ((me frame) stream)
  272. (format stream "~a"
  273. (with-output-to-string (s)
  274. (with-frame-slots (me)
  275. (format s "MPEG Frame: position in file = ~:d, header in (hex) bytes = ~x, size = ~d, sync word = ~x, " pos hdr-u32 size sync)
  276. (when vbr
  277. (format s "~&vbr-info: ~a~%" vbr))
  278. (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, "
  279. (get-mpeg-version-string version) (get-layer-string layer)
  280. protection bit-rate sample-rate padded private (get-channel-mode-string channel-mode))
  281. (format s "mode extension = ~a, copyrighted? = ~[no~;yes~], original? = ~[no~;yes~], emphasis = ~a"
  282. (get-mode-extension-string channel-mode layer mode-extension) copyright original (get-emphasis-string emphasis))
  283. (when payload
  284. (format s "~%frame payload[~:d] = ~a~%" (length payload) (utils:printable-array payload)))))))
  285. (defclass vbr-info ()
  286. ((tag :accessor tag :initarg :tag)
  287. (flags :accessor flags :initarg :flags)
  288. (frames :accessor frames :initarg :frames)
  289. (bytes :accessor bytes :initarg :bytes)
  290. (tocs :accessor tocs :initarg :tocs)
  291. (scale :accessor scale :initarg :scale))
  292. (:default-initargs :tag nil :flags 0 :frames nil :bytes nil :tocs nil :scale nil))
  293. (defmacro with-vbr-info-slots ((instance) &body body)
  294. `(with-slots (tag flags frames bytes tocs scale) ,instance
  295. ,@body))
  296. (defconstant +vbr-frames+ 1)
  297. (defconstant +vbr-bytes+ 2)
  298. (defconstant +vbr-tocs+ 4)
  299. (defconstant +vbr-scale+ 8)
  300. (defun get-side-info-size (version channel-mode)
  301. (declare #.utils:*standard-optimize-settings*)
  302. (cond ((= (the fixnum version) (the fixnum +mpeg-1+))
  303. (cond ((= (the fixnum channel-mode) (the fixnum +channel-mode-mono+)) 17)
  304. (t 32)))
  305. (t (cond ((= (the fixnum channel-mode) (the fixnum +channel-mode-mono+)) 9)
  306. (t 17)))))
  307. (defmethod check-vbr ((me frame) fn)
  308. (declare #.utils:*standard-optimize-settings*)
  309. (log5::with-context "check-vbr"
  310. (with-frame-slots (me)
  311. (let ((i (get-side-info-size version channel-mode)))
  312. (log-mpeg-frame "array index = ~d, payload size = ~d" i (length payload))
  313. (when (>= i (length payload))
  314. (return-from check-vbr nil))
  315. (when (or (and (= (aref payload (+ i 0)) (char-code #\X))
  316. (= (aref payload (+ i 1)) (char-code #\i))
  317. (= (aref payload (+ i 2)) (char-code #\n))
  318. (= (aref payload (+ i 3)) (char-code #\g)))
  319. (and (= (aref payload (+ i 0)) (char-code #\I))
  320. (= (aref payload (+ i 1)) (char-code #\n))
  321. (= (aref payload (+ i 2)) (char-code #\f))
  322. (= (aref payload (+ i 3)) (char-code #\o))))
  323. (log-mpeg-frame "found xing/info: ~c ~c ~c ~c"
  324. (code-char (aref payload (+ i 0)))
  325. (code-char (aref payload (+ i 1)))
  326. (code-char (aref payload (+ i 2)))
  327. (code-char (aref payload (+ i 3))))
  328. (setf vbr (make-instance 'vbr-info))
  329. (let ((v (make-mem-stream (payload me))))
  330. (stream-seek v i :start) ; seek to Xing/Info offset
  331. (setf (tag vbr) (stream-read-iso-string-with-len v 4))
  332. (setf (flags vbr) (stream-read-u32 v))
  333. (when (logand (flags vbr) +vbr-frames+)
  334. (setf (frames vbr) (stream-read-u32 v))
  335. (log-mpeg-frame "Xing frames set: read ~d" (frames vbr))
  336. ;; some VBR files have the Xing/Info header, but it is not correctly formulated.
  337. ;; just warn the user.
  338. (when (zerop (frames vbr))
  339. (warn-user "file ~a Xing/Info header flags has FRAMES set, but field is zero." fn)))
  340. (when (logand (flags vbr) +vbr-bytes+)
  341. (setf (bytes vbr) (stream-read-u32 v))
  342. (log-mpeg-frame "Xing bytes set: read ~d" (bytes vbr)))
  343. (when (logand (flags vbr) +vbr-tocs+)
  344. (setf (tocs vbr) (stream-read-sequence v 100))
  345. (log-mpeg-frame "Xing tocs set: read ~a" (tocs vbr)))
  346. (when (logand (flags vbr) +vbr-scale+)
  347. (setf (scale vbr) (stream-read-u32 v))
  348. (log-mpeg-frame "Xing scale set: read ~d" (scale vbr)))
  349. (log-mpeg-frame "vbr-info = ~a" (vpprint vbr nil))))))))
  350. (defmethod vpprint ((me vbr-info) stream)
  351. (with-vbr-info-slots (me)
  352. (format stream "tag = ~a, flags = 0x~x, frame~p = ~:d, bytes = ~:d, tocs = ~d, scale = ~d, "
  353. tag flags frames frames bytes tocs scale)))
  354. (defun find-first-sync (in)
  355. (declare #.utils:*standard-optimize-settings*)
  356. (log5:with-context "find-first-sync"
  357. (log-mpeg-frame "Looking for first sync, begining at file position ~:d" (stream-seek in))
  358. (let ((hdr-u32)
  359. (count 0)
  360. (pos))
  361. (handler-case
  362. (loop
  363. (setf pos (stream-seek in))
  364. (setf hdr-u32 (stream-read-u32 in))
  365. (when (null hdr-u32)
  366. (return-from find-first-sync nil))
  367. (incf count)
  368. (when (= (logand hdr-u32 #xffe00000) #xffe00000) ; magic number is potential sync frame header
  369. (log-mpeg-frame "Potential sync bytes at ~:d: <~x>" pos hdr-u32)
  370. (let ((hdr (make-instance 'frame :hdr-u32 hdr-u32 :pos pos)))
  371. (if (load-frame hdr :instream in :read-payload t)
  372. (progn
  373. (check-vbr hdr (stream-filename in))
  374. (log-mpeg-frame "Valid header being returned: ~a, searched ~:d times" hdr count)
  375. (return-from find-first-sync hdr))
  376. (progn
  377. (log-mpeg-frame "hdr wasn't valid: ~a" hdr))))))
  378. (condition (c) (progn
  379. (warn-user "Condtion <~a> signaled while looking for first sync" c)
  380. (log-mpeg-frame "got a condition while looking for first sync: ~a" c)
  381. (error c)))) ; XXX should I propogate this, or just return nil
  382. nil)))
  383. (defmethod next-frame ((me frame) &key instream read-payload)
  384. "Get next frame. If READ-PAYLOAD is true, read in contents for frame, else, seek to next frame header."
  385. (declare #.utils:*standard-optimize-settings*)
  386. (log5:with-context "next-frame"
  387. (let ((nxt-frame (make-instance 'frame)))
  388. (when (not (payload me))
  389. (log-mpeg-frame "no payload load required in current frame, skipping from ~:d forward ~:d bytes"
  390. (stream-seek instream)
  391. (- (size me) 4) :current)
  392. (stream-seek instream (- (size me) 4) :current))
  393. (log-mpeg-frame "at pos ~:d, read-payload is ~a" (stream-seek instream) read-payload)
  394. (if (load-frame nxt-frame :instream instream :read-payload read-payload)
  395. nxt-frame
  396. nil))))
  397. (defparameter *max-frames-to-read* most-positive-fixnum "when trying to determine bit-rate, etc, read at most this many frames")
  398. (defun map-frames (in func &key (start-pos nil) (read-payload nil) (max nil))
  399. "Loop through the MPEG audio frames in a file. If *MAX-FRAMES-TO-READ* is set, return after reading that many frames."
  400. (declare #.utils:*standard-optimize-settings*)
  401. (log5:with-context "next-frame"
  402. (log-mpeg-frame "mapping frames, start pos ~:d" start-pos)
  403. (when start-pos
  404. (stream-seek in start-pos :start))
  405. (loop
  406. for max-frames = (if max max *max-frames-to-read*)
  407. for count = 0 then (incf count)
  408. for frame = (find-first-sync in) then (next-frame frame :instream in :read-payload read-payload)
  409. while (and frame (< count max-frames)) do
  410. (log-mpeg-frame "map-frames: at pos ~:d, dispatching function" (pos frame))
  411. (funcall func frame))))
  412. (defclass mpeg-audio-info ()
  413. ((is-vbr :accessor is-vbr :initarg :is-vbr :initform nil)
  414. (n-frames :accessor n-frames :initarg :n-frames :initform 0)
  415. (bit-rate :accessor bit-rate :initarg :bit-rate :initform nil)
  416. (sample-rate :accessor sample-rate :initarg :sample-rate :initform nil)
  417. (len :accessor len :initarg :len :initform nil)
  418. (version :accessor version :initarg :version :initform nil)
  419. (layer :accessor layer :initarg :layer :initform nil)))
  420. (defmethod vpprint ((me mpeg-audio-info) stream)
  421. (with-slots (is-vbr sample-rate bit-rate len version layer n-frames) me
  422. (format stream "~:d frame~p read, ~a, ~a, ~:[CBR,~;VBR,~] sample rate: ~:d Hz, bit rate: ~:d Kbps, duration: ~:d:~2,'0d"
  423. n-frames n-frames
  424. (get-mpeg-version-string version)
  425. (get-layer-string layer)
  426. is-vbr
  427. sample-rate
  428. (round (/ bit-rate 1000))
  429. (floor (/ len 60)) (round (mod len 60)))))
  430. (defun calc-bit-rate-exhaustive (in start info)
  431. "Map every MPEG frame in IN and calculate the bit-rate"
  432. (declare #.utils:*standard-optimize-settings*)
  433. (log5:with-context "calc-bit-rate-exhaustive"
  434. (let ((total-len 0)
  435. (last-bit-rate nil)
  436. (bit-rate-total 0)
  437. (vbr nil))
  438. (log-mpeg-frame "broken Xing/Info header found, reading all frames")
  439. (with-slots (is-vbr sample-rate bit-rate len version layer n-frames) info
  440. (map-frames in (lambda (f)
  441. (incf n-frames)
  442. (incf total-len (float (/ (samples f) (sample-rate f))))
  443. (incf bit-rate-total (bit-rate f))
  444. (if (null last-bit-rate)
  445. (setf last-bit-rate (bit-rate f))
  446. (progn
  447. (when (not (= last-bit-rate (bit-rate f)))
  448. (setf vbr t))
  449. (setf last-bit-rate (bit-rate f)))))
  450. :read-payload nil :start-pos start)
  451. (log-mpeg-frame "finished mapping. read ~:d frames" n-frames)
  452. (when (or (< n-frames 10) (zerop bit-rate-total))
  453. (log-mpeg-frame "couldn't get audio-info: only got ~d frames" n-frames)
  454. (return-from calc-bit-rate-exhaustive))
  455. (setf is-vbr t)
  456. (setf len total-len)
  457. (setf bit-rate (float (/ bit-rate-total n-frames)))
  458. (log-mpeg-frame "len = ~:d, bit-rate = ~f" len bit-rate)))))
  459. (defun get-mpeg-audio-info (in &key) ;; (max-frames *max-frames-to-read*))
  460. "Get MPEG Layer 3 audio information.
  461. If the first MPEG frame we find is a Xing/Info header, return that as info.
  462. Else, we assume CBR and calculate the duration, etc."
  463. (declare #.utils:*standard-optimize-settings*)
  464. (log5:with-context "get-mpeg-audio-info"
  465. (let ((first-frame (find-first-sync in))
  466. (info (make-instance 'mpeg-audio-info)))
  467. (log-mpeg-frame "search for first frame yielded ~a" (vpprint first-frame nil))
  468. (when (null first-frame)
  469. (return-from get-mpeg-audio-info nil))
  470. (with-slots (is-vbr sample-rate bit-rate len version layer n-frames) info
  471. (setf version (version first-frame))
  472. (setf layer (layer first-frame))
  473. (setf sample-rate (sample-rate first-frame))
  474. (if (vbr first-frame)
  475. ;; found a Xing header, now check to see if it is correct
  476. (if (zerop (frames (vbr first-frame)))
  477. (calc-bit-rate-exhaustive in (pos first-frame) info) ; Xing header broken, read all frames to calc
  478. ;; Good Xing header, use info in VBR to calc
  479. (progn
  480. (setf n-frames 1)
  481. (setf is-vbr t)
  482. (setf len (float (* (frames (vbr first-frame)) (/ (samples first-frame) (sample-rate first-frame)))))
  483. (setf bit-rate (float (/ (* 8 (bytes (vbr first-frame))) len)))))
  484. ;; No Xing header found. Assume CBR and calculate based on first frame
  485. (let* ((first (pos first-frame))
  486. (last (- (audio-streams:stream-size in) (if (id3-frame::v21-tag-header (id3-header in)) 128 0)))
  487. (n-fr (round (/ (float (- last first)) (float (size first-frame)))))
  488. (n-sec (round (/ (float (* (size first-frame) n-fr)) (float (* 125 (float (/ (bit-rate first-frame) 1000))))))))
  489. (setf is-vbr nil)
  490. (setf n-frames 1)
  491. (setf len n-sec)
  492. (setf bit-rate (float (bit-rate first-frame))))))
  493. info)))