Source: lib/cea/sei_processor.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.cea.SeiProcessor');
  7. /**
  8. * H.264 SEI NALU Parser used for extracting 708 closed caption packets.
  9. */
  10. shaka.cea.SeiProcessor = class {
  11. /**
  12. * Processes supplemental enhancement information data.
  13. * @param {!Uint8Array} naluData NALU from which SEI data is to be processed.
  14. * @return {!Iterable.<!Uint8Array>}
  15. */
  16. * process(naluData) {
  17. const emuCount = this.removeEmu_(naluData);
  18. // The following is an implementation of section 7.3.2.3.1
  19. // in Rec. ITU-T H.264 (06/2019), the H.264 spec.
  20. let offset = 0;
  21. while (offset + emuCount < naluData.length) {
  22. let payloadType = 0; // SEI payload type as defined by H.264 spec
  23. while (naluData[offset] == 0xFF) {
  24. payloadType += 255;
  25. offset++;
  26. }
  27. payloadType += naluData[offset++];
  28. let payloadSize = 0; // SEI payload size as defined by H.264 spec
  29. while (naluData[offset] == 0xFF) {
  30. payloadSize += 255;
  31. offset++;
  32. }
  33. payloadSize += naluData[offset++];
  34. // Payload type 4 is user_data_registered_itu_t_t35, as per the H.264
  35. // spec. This payload type contains caption data.
  36. if (payloadType == 0x04) {
  37. yield naluData.subarray(offset, offset + payloadSize);
  38. }
  39. offset += payloadSize;
  40. }
  41. }
  42. /**
  43. * Removes H.264 emulation prevention bytes from the byte array.
  44. * @param {!Uint8Array} naluData NALU from which EMUs should be removed.
  45. * @return {number} The number of removed emulation prevention bytes.
  46. * @private
  47. */
  48. removeEmu_(naluData) {
  49. let zeroCount = 0;
  50. let src = 0;
  51. let dst = 0;
  52. while (src < naluData.length) {
  53. if (zeroCount == 2 && naluData[src] == 0x03) {
  54. zeroCount = 0;
  55. } else {
  56. if (naluData[src] == 0x00) {
  57. zeroCount++;
  58. } else {
  59. zeroCount = 0;
  60. }
  61. naluData[dst] = naluData[src];
  62. dst++;
  63. }
  64. src++;
  65. }
  66. return (src - dst);
  67. }
  68. };