Source: lib/util/id3_utils.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2022 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.util.Id3Utils');
  7. goog.require('shaka.log');
  8. goog.require('shaka.util.BufferUtils');
  9. goog.require('shaka.util.StringUtils');
  10. /**
  11. * @summary A set of Id3Utils utility functions.
  12. * @export
  13. */
  14. shaka.util.Id3Utils = class {
  15. /**
  16. * @param {Uint8Array} data
  17. * @param {number} offset
  18. * @return {boolean}
  19. * @private
  20. */
  21. static isHeader_(data, offset) {
  22. /*
  23. * http://id3.org/id3v2.3.0
  24. * [0] = 'I'
  25. * [1] = 'D'
  26. * [2] = '3'
  27. * [3,4] = {Version}
  28. * [5] = {Flags}
  29. * [6-9] = {ID3 Size}
  30. *
  31. * An ID3v2 tag can be detected with the following pattern:
  32. * $49 44 33 yy yy xx zz zz zz zz
  33. * Where yy is less than $FF, xx is the 'flags' byte and zz is less than $80
  34. */
  35. if (offset + 10 <= data.length) {
  36. // look for 'ID3' identifier
  37. if (data[offset] === 0x49 &&
  38. data[offset + 1] === 0x44 &&
  39. data[offset + 2] === 0x33) {
  40. // check version is within range
  41. if (data[offset + 3] < 0xff && data[offset + 4] < 0xff) {
  42. // check size is within range
  43. if (data[offset + 6] < 0x80 &&
  44. data[offset + 7] < 0x80 &&
  45. data[offset + 8] < 0x80 &&
  46. data[offset + 9] < 0x80) {
  47. return true;
  48. }
  49. }
  50. }
  51. }
  52. return false;
  53. }
  54. /**
  55. * @param {Uint8Array} data
  56. * @param {number} offset
  57. * @return {boolean}
  58. * @private
  59. */
  60. static isFooter_(data, offset) {
  61. /*
  62. * The footer is a copy of the header, but with a different identifier
  63. */
  64. if (offset + 10 <= data.length) {
  65. // look for '3DI' identifier
  66. if (data[offset] === 0x33 &&
  67. data[offset + 1] === 0x44 &&
  68. data[offset + 2] === 0x49) {
  69. // check version is within range
  70. if (data[offset + 3] < 0xff && data[offset + 4] < 0xff) {
  71. // check size is within range
  72. if (data[offset + 6] < 0x80 &&
  73. data[offset + 7] < 0x80 &&
  74. data[offset + 8] < 0x80 &&
  75. data[offset + 9] < 0x80) {
  76. return true;
  77. }
  78. }
  79. }
  80. }
  81. return false;
  82. }
  83. /**
  84. * @param {Uint8Array} data
  85. * @param {number} offset
  86. * @return {number}
  87. * @private
  88. */
  89. static readSize_(data, offset) {
  90. let size = 0;
  91. size = (data[offset] & 0x7f) << 21;
  92. size |= (data[offset + 1] & 0x7f) << 14;
  93. size |= (data[offset + 2] & 0x7f) << 7;
  94. size |= data[offset + 3] & 0x7f;
  95. return size;
  96. }
  97. /**
  98. * @param {Uint8Array} data
  99. * @return {shaka.extern.MetadataRawFrame}
  100. * @private
  101. */
  102. static getFrameData_(data) {
  103. /*
  104. * Frame ID $xx xx xx xx (four characters)
  105. * Size $xx xx xx xx
  106. * Flags $xx xx
  107. */
  108. const type = String.fromCharCode(data[0], data[1], data[2], data[3]);
  109. const size = shaka.util.Id3Utils.readSize_(data, 4);
  110. // skip frame id, size, and flags
  111. const offset = 10;
  112. return {
  113. type,
  114. size,
  115. data: data.subarray(offset, offset + size),
  116. };
  117. }
  118. /**
  119. * @param {shaka.extern.MetadataRawFrame} frame
  120. * @return {?shaka.extern.MetadataFrame}
  121. * @private
  122. */
  123. static decodeFrame_(frame) {
  124. const BufferUtils = shaka.util.BufferUtils;
  125. const StringUtils = shaka.util.StringUtils;
  126. const metadataFrame = {
  127. key: frame.type,
  128. description: '',
  129. data: '',
  130. };
  131. if (frame.type === 'TXXX') {
  132. /*
  133. * Format:
  134. * [0] = {Text Encoding}
  135. * [1-?] = {Description}\0{Value}
  136. */
  137. if (frame.size < 2) {
  138. return null;
  139. }
  140. if (frame.data[0] !== shaka.util.Id3Utils.UTF8_encoding) {
  141. shaka.log.warning('Ignore frame with unrecognized character ' +
  142. 'encoding');
  143. return null;
  144. }
  145. const descriptionEndIndex = frame.data.subarray(1).indexOf(0);
  146. if (descriptionEndIndex === -1) {
  147. return null;
  148. }
  149. const description = StringUtils.fromUTF8(
  150. BufferUtils.toUint8(frame.data, 1, descriptionEndIndex));
  151. const data = StringUtils.fromUTF8(
  152. BufferUtils.toUint8(frame.data, 2 + descriptionEndIndex))
  153. .replace(/\0*$/, '');
  154. metadataFrame.description = description;
  155. metadataFrame.data = data;
  156. return metadataFrame;
  157. } else if (frame.type === 'WXXX') {
  158. /*
  159. * Format:
  160. * [0] = {Text Encoding}
  161. * [1-?] = {Description}\0{URL}
  162. */
  163. if (frame.size < 2) {
  164. return null;
  165. }
  166. if (frame.data[0] !== shaka.util.Id3Utils.UTF8_encoding) {
  167. shaka.log.warning('Ignore frame with unrecognized character ' +
  168. 'encoding');
  169. return null;
  170. }
  171. const descriptionEndIndex = frame.data.subarray(1).indexOf(0);
  172. if (descriptionEndIndex === -1) {
  173. return null;
  174. }
  175. const description = StringUtils.fromUTF8(
  176. BufferUtils.toUint8(frame.data, 1, descriptionEndIndex));
  177. const data = StringUtils.fromUTF8(
  178. BufferUtils.toUint8(frame.data, 2 + descriptionEndIndex))
  179. .replace(/\0*$/, '');
  180. metadataFrame.description = description;
  181. metadataFrame.data = data;
  182. return metadataFrame;
  183. } else if (frame.type === 'PRIV') {
  184. /*
  185. * Format: <text string>\0<binary data>
  186. */
  187. if (frame.size < 2) {
  188. return null;
  189. }
  190. const textEndIndex = frame.data.indexOf(0);
  191. if (textEndIndex === -1) {
  192. return null;
  193. }
  194. const text = StringUtils.fromUTF8(
  195. BufferUtils.toUint8(frame.data, 0, textEndIndex));
  196. metadataFrame.description = text;
  197. if (text == 'com.apple.streaming.transportStreamTimestamp') {
  198. const data = frame.data.subarray(text.length + 1);
  199. // timestamp is 33 bit expressed as a big-endian eight-octet number,
  200. // with the upper 31 bits set to zero.
  201. const pts33Bit = data[3] & 0x1;
  202. let timestamp =
  203. (data[4] << 23) + (data[5] << 15) + (data[6] << 7) + data[7];
  204. timestamp /= 45;
  205. if (pts33Bit) {
  206. timestamp += 47721858.84;
  207. } // 2^32 / 90
  208. metadataFrame.data = timestamp;
  209. } else {
  210. const data = BufferUtils.toArrayBuffer(
  211. frame.data.subarray(text.length + 1));
  212. metadataFrame.data = data;
  213. }
  214. return metadataFrame;
  215. } else if (frame.type[0] === 'T') {
  216. /*
  217. * Format:
  218. * [0] = {Text Encoding}
  219. * [1-?] = {Value}
  220. */
  221. if (frame.size < 2) {
  222. return null;
  223. }
  224. if (frame.data[0] !== shaka.util.Id3Utils.UTF8_encoding) {
  225. shaka.log.warning('Ignore frame with unrecognized character ' +
  226. 'encoding');
  227. return null;
  228. }
  229. const text = StringUtils.fromUTF8(frame.data.subarray(1))
  230. .replace(/\0*$/, '');
  231. metadataFrame.data = text;
  232. return metadataFrame;
  233. } else if (frame.type[0] === 'W') {
  234. /*
  235. * Format:
  236. * [0-?] = {URL}
  237. */
  238. const url = StringUtils.fromUTF8(frame.data)
  239. .replace(/\0*$/, '');
  240. metadataFrame.data = url;
  241. return metadataFrame;
  242. } else if (frame.data) {
  243. shaka.log.warning('Unrecognized ID3 frame type:', frame.type);
  244. metadataFrame.data = BufferUtils.toArrayBuffer(frame.data);
  245. return metadataFrame;
  246. }
  247. return null;
  248. }
  249. /**
  250. * Returns an array of ID3 frames found in all the ID3 tags in the id3Data
  251. * @param {Uint8Array} id3Data - The ID3 data containing one or more ID3 tags
  252. * @return {!Array.<shaka.extern.MetadataFrame>}
  253. * @export
  254. */
  255. static getID3Frames(id3Data) {
  256. const Id3Utils = shaka.util.Id3Utils;
  257. let offset = 0;
  258. const frames = [];
  259. while (Id3Utils.isHeader_(id3Data, offset)) {
  260. const size = Id3Utils.readSize_(id3Data, offset + 6);
  261. if ((id3Data[offset + 5] >> 6) & 1) {
  262. // skip extended header
  263. offset += 10;
  264. }
  265. // skip past ID3 header
  266. offset += 10;
  267. const end = offset + size;
  268. // loop through frames in the ID3 tag
  269. while (offset + 10 < end) {
  270. const frameData = Id3Utils.getFrameData_(id3Data.subarray(offset));
  271. const frame = Id3Utils.decodeFrame_(frameData);
  272. if (frame) {
  273. frames.push(frame);
  274. }
  275. // skip frame header and frame data
  276. offset += frameData.size + 10;
  277. }
  278. if (Id3Utils.isFooter_(id3Data, offset)) {
  279. offset += 10;
  280. }
  281. }
  282. return frames;
  283. }
  284. /**
  285. * Returns any adjacent ID3 tags found in data starting at offset, as one
  286. * block of data
  287. * @param {Uint8Array} id3Data - The ID3 data containing one or more ID3 tags
  288. * @param {number=} offset - The offset at which to start searching
  289. * @return {!Uint8Array}
  290. * @export
  291. */
  292. static getID3Data(id3Data, offset = 0) {
  293. const Id3Utils = shaka.util.Id3Utils;
  294. const front = offset;
  295. let length = 0;
  296. while (Id3Utils.isHeader_(id3Data, offset)) {
  297. if ((id3Data[offset + 5] >> 6) & 1) {
  298. // skip extended header
  299. length += 10;
  300. }
  301. // skip past ID3 header
  302. length += 10;
  303. const size = Id3Utils.readSize_(id3Data, offset + 6);
  304. length += size;
  305. if (Id3Utils.isFooter_(id3Data, offset + 10)) {
  306. // ID3 footer is 10 bytes
  307. length += 10;
  308. }
  309. offset += length;
  310. }
  311. if (length > 0) {
  312. return id3Data.subarray(front, front + length);
  313. }
  314. return new Uint8Array([]);
  315. }
  316. };
  317. /**
  318. * UTF8 encoding byte
  319. * @const {number}
  320. */
  321. shaka.util.Id3Utils.UTF8_encoding = 0x03;