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. const data = BufferUtils.toArrayBuffer(
  197. frame.data.subarray(text.length + 1));
  198. metadataFrame.description = text;
  199. metadataFrame.data = data;
  200. return metadataFrame;
  201. } else if (frame.type[0] === 'T') {
  202. /*
  203. * Format:
  204. * [0] = {Text Encoding}
  205. * [1-?] = {Value}
  206. */
  207. if (frame.size < 2) {
  208. return null;
  209. }
  210. if (frame.data[0] !== shaka.util.Id3Utils.UTF8_encoding) {
  211. shaka.log.warning('Ignore frame with unrecognized character ' +
  212. 'encoding');
  213. return null;
  214. }
  215. const text = StringUtils.fromUTF8(frame.data.subarray(1))
  216. .replace(/\0*$/, '');
  217. metadataFrame.data = text;
  218. return metadataFrame;
  219. } else if (frame.type[0] === 'W') {
  220. /*
  221. * Format:
  222. * [0-?] = {URL}
  223. */
  224. const url = StringUtils.fromUTF8(frame.data)
  225. .replace(/\0*$/, '');
  226. metadataFrame.data = url;
  227. return metadataFrame;
  228. } else if (frame.data) {
  229. shaka.log.warning('Unrecognized ID3 frame type:', frame.type);
  230. metadataFrame.data = BufferUtils.toArrayBuffer(frame.data);
  231. return metadataFrame;
  232. }
  233. return null;
  234. }
  235. /**
  236. * Returns an array of ID3 frames found in all the ID3 tags in the id3Data
  237. * @param {Uint8Array} id3Data - The ID3 data containing one or more ID3 tags
  238. * @return {!Array.<shaka.extern.MetadataFrame>}
  239. * @export
  240. */
  241. static getID3Frames(id3Data) {
  242. const Id3Utils = shaka.util.Id3Utils;
  243. let offset = 0;
  244. const frames = [];
  245. while (Id3Utils.isHeader_(id3Data, offset)) {
  246. const size = Id3Utils.readSize_(id3Data, offset + 6);
  247. if ((id3Data[offset + 5] >> 6) & 1) {
  248. // skip extended header
  249. offset += 10;
  250. }
  251. // skip past ID3 header
  252. offset += 10;
  253. const end = offset + size;
  254. // loop through frames in the ID3 tag
  255. while (offset + 10 < end) {
  256. const frameData = Id3Utils.getFrameData_(id3Data.subarray(offset));
  257. const frame = Id3Utils.decodeFrame_(frameData);
  258. if (frame) {
  259. frames.push(frame);
  260. }
  261. // skip frame header and frame data
  262. offset += frameData.size + 10;
  263. }
  264. if (Id3Utils.isFooter_(id3Data, offset)) {
  265. offset += 10;
  266. }
  267. }
  268. return frames;
  269. }
  270. };
  271. /**
  272. * UTF8 encoding byte
  273. * @const {number}
  274. */
  275. shaka.util.Id3Utils.UTF8_encoding = 0x03;