|
6 | 6 |
|
7 | 7 | goog.provide('shaka.text.Mp4TtmlParser'); |
8 | 8 |
|
| 9 | +goog.require('goog.asserts'); |
9 | 10 | goog.require('shaka.text.TextEngine'); |
10 | 11 | goog.require('shaka.text.TtmlTextParser'); |
11 | 12 | goog.require('shaka.util.BufferUtils'); |
12 | 13 | goog.require('shaka.util.Error'); |
| 14 | +goog.require('shaka.util.Mp4BoxParsers'); |
13 | 15 | goog.require('shaka.util.Mp4Parser'); |
14 | 16 | goog.require('shaka.util.Uint8ArrayUtils'); |
15 | 17 |
|
@@ -80,67 +82,117 @@ shaka.text.Mp4TtmlParser = class { |
80 | 82 | parseMedia(data, time, uri) { |
81 | 83 | const Mp4Parser = shaka.util.Mp4Parser; |
82 | 84 |
|
83 | | - let sawMDAT = false; |
84 | 85 | let payload = []; |
| 86 | + let defaultSampleSize = null; |
| 87 | + |
| 88 | + /** @type {!Array<Uint8Array>} */ |
| 89 | + const mdats = []; |
| 90 | + |
| 91 | + /* @type {!Map<number,!Array<number>>} */ |
| 92 | + const subSampleSizesPerSample = new Map(); |
85 | 93 |
|
86 | 94 | /** @type {!Array<number>} */ |
87 | | - let subSizes = []; |
| 95 | + const sampleSizes = []; |
88 | 96 |
|
89 | 97 | const parser = new Mp4Parser() |
90 | 98 | .box('moof', Mp4Parser.children) |
91 | 99 | .box('traf', Mp4Parser.children) |
| 100 | + .fullBox('tfhd', (box) => { |
| 101 | + goog.asserts.assert( |
| 102 | + box.flags != null, |
| 103 | + 'A TFHD box should have a valid flags value'); |
| 104 | + const parsedTFHDBox = shaka.util.Mp4BoxParsers.parseTFHD( |
| 105 | + box.reader, box.flags); |
| 106 | + defaultSampleSize = parsedTFHDBox.defaultSampleSize; |
| 107 | + }) |
| 108 | + .fullBox('trun', (box) => { |
| 109 | + goog.asserts.assert( |
| 110 | + box.version != null, |
| 111 | + 'A TRUN box should have a valid version value'); |
| 112 | + goog.asserts.assert( |
| 113 | + box.flags != null, |
| 114 | + 'A TRUN box should have a valid flags value'); |
| 115 | + |
| 116 | + const parsedTRUNBox = shaka.util.Mp4BoxParsers.parseTRUN( |
| 117 | + box.reader, box.version, box.flags); |
| 118 | + |
| 119 | + for (const sample of parsedTRUNBox.sampleData) { |
| 120 | + const sampleSize = |
| 121 | + sample.sampleSize || defaultSampleSize || 0; |
| 122 | + sampleSizes.push(sampleSize); |
| 123 | + } |
| 124 | + }) |
92 | 125 | .fullBox('subs', (box) => { |
93 | | - subSizes = []; |
94 | 126 | const reader = box.reader; |
95 | 127 | const entryCount = reader.readUint32(); |
| 128 | + let currentSampleNum = -1; |
96 | 129 | for (let i = 0; i < entryCount; i++) { |
97 | | - reader.readUint32(); // sample_delta |
| 130 | + const sampleDelta = reader.readUint32(); |
| 131 | + currentSampleNum += sampleDelta; |
98 | 132 | const subsampleCount = reader.readUint16(); |
| 133 | + const subsampleSizes = []; |
99 | 134 | for (let j = 0; j < subsampleCount; j++) { |
100 | 135 | if (box.version == 1) { |
101 | | - subSizes.push(reader.readUint32()); |
| 136 | + subsampleSizes.push(reader.readUint32()); |
102 | 137 | } else { |
103 | | - subSizes.push(reader.readUint16()); |
| 138 | + subsampleSizes.push(reader.readUint16()); |
104 | 139 | } |
105 | 140 | reader.readUint8(); // priority |
106 | 141 | reader.readUint8(); // discardable |
107 | 142 | reader.readUint32(); // codec_specific_parameters |
108 | 143 | } |
| 144 | + subSampleSizesPerSample.set(currentSampleNum, subsampleSizes); |
109 | 145 | } |
110 | 146 | }) |
111 | 147 | .box('mdat', Mp4Parser.allData((data) => { |
112 | | - sawMDAT = true; |
113 | | - // Join this to any previous payload, in case the mp4 has multiple |
114 | | - // mdats. |
115 | | - if (subSizes.length) { |
116 | | - const contentData = |
117 | | - shaka.util.BufferUtils.toUint8(data, 0, subSizes[0]); |
118 | | - const images = []; |
119 | | - let offset = subSizes[0]; |
120 | | - for (let i = 1; i < subSizes.length; i++) { |
121 | | - const imageData = |
122 | | - shaka.util.BufferUtils.toUint8(data, offset, subSizes[i]); |
123 | | - const raw = |
124 | | - shaka.util.Uint8ArrayUtils.toStandardBase64(imageData); |
125 | | - images.push('data:image/png;base64,' + raw); |
126 | | - offset += subSizes[i]; |
127 | | - } |
128 | | - payload = payload.concat( |
129 | | - this.parser_.parseMedia(contentData, time, uri, images)); |
130 | | - } else { |
131 | | - payload = payload.concat( |
132 | | - this.parser_.parseMedia(data, time, uri, /* images= */ [])); |
133 | | - } |
| 148 | + // We collect all of the mdats first, before parsing any of them. |
| 149 | + // This is necessary in case the mp4 has multiple mdats. |
| 150 | + mdats.push(data); |
134 | 151 | })); |
135 | 152 | parser.parse(data, /* partialOkay= */ false); |
136 | 153 |
|
137 | | - if (!sawMDAT) { |
| 154 | + if (mdats.length == 0) { |
138 | 155 | throw new shaka.util.Error( |
139 | 156 | shaka.util.Error.Severity.CRITICAL, |
140 | 157 | shaka.util.Error.Category.TEXT, |
141 | 158 | shaka.util.Error.Code.INVALID_MP4_TTML); |
142 | 159 | } |
143 | 160 |
|
| 161 | + const fullData = |
| 162 | + shaka.util.Uint8ArrayUtils.concat(...mdats); |
| 163 | + |
| 164 | + let sampleOffset = 0; |
| 165 | + for (let sampleNum = 0; sampleNum < sampleSizes.length; sampleNum++) { |
| 166 | + const sampleData = |
| 167 | + shaka.util.BufferUtils.toUint8(fullData, sampleOffset, |
| 168 | + sampleSizes[sampleNum]); |
| 169 | + sampleOffset += sampleSizes[sampleNum]; |
| 170 | + |
| 171 | + const subSampleSizes = subSampleSizesPerSample.get(sampleNum); |
| 172 | + |
| 173 | + if (subSampleSizes && subSampleSizes.length) { |
| 174 | + const contentData = |
| 175 | + shaka.util.BufferUtils.toUint8(sampleData, 0, subSampleSizes[0]); |
| 176 | + const images = []; |
| 177 | + let subOffset = subSampleSizes[0]; |
| 178 | + for (let i = 1; i < subSampleSizes.length; i++) { |
| 179 | + const imageData = |
| 180 | + shaka.util.BufferUtils.toUint8(data, subOffset, |
| 181 | + subSampleSizes[i]); |
| 182 | + const raw = |
| 183 | + shaka.util.Uint8ArrayUtils.toStandardBase64(imageData); |
| 184 | + images.push('data:image/png;base64,' + raw); |
| 185 | + subOffset += subSampleSizes[i]; |
| 186 | + } |
| 187 | + payload = payload.concat( |
| 188 | + this.parser_.parseMedia(contentData, time, uri, images)); |
| 189 | + } else { |
| 190 | + payload = payload.concat( |
| 191 | + this.parser_.parseMedia(sampleData, time, uri, |
| 192 | + /* images= */ [])); |
| 193 | + } |
| 194 | + } |
| 195 | + |
144 | 196 | return payload; |
145 | 197 | } |
146 | 198 | }; |
|
0 commit comments