1 /*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "Utils"
19 #include <utils/Log.h>
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <sys/stat.h>
23
24 #include <utility>
25 #include <vector>
26
27 #include <media/esds/ESDS.h>
28 #include "include/HevcUtils.h"
29
30 #include <cutils/properties.h>
31 #include <media/stagefright/CodecBase.h>
32 #include <media/stagefright/foundation/ABuffer.h>
33 #include <media/stagefright/foundation/ADebug.h>
34 #include <media/stagefright/foundation/ALookup.h>
35 #include <media/stagefright/foundation/AMessage.h>
36 #include <media/stagefright/foundation/ByteUtils.h>
37 #include <media/stagefright/foundation/OpusHeader.h>
38 #include <media/stagefright/MetaData.h>
39 #include <media/stagefright/MediaCodecConstants.h>
40 #include <media/stagefright/MediaDefs.h>
41 #include <media/AudioSystem.h>
42 #include <media/MediaPlayerInterface.h>
43 #include <media/stagefright/Utils.h>
44 #include <media/AudioParameter.h>
45 #include <system/audio.h>
46
47 #include <com_android_media_extractor_flags.h>
48
49 // TODO : Remove the defines once mainline media is built against NDK >= 31.
50 // The mp4 extractor is part of mainline and builds against NDK 29 as of
51 // writing. These keys are available only from NDK 31:
52 #define AMEDIAFORMAT_KEY_MPEGH_PROFILE_LEVEL_INDICATION \
53 "mpegh-profile-level-indication"
54 #define AMEDIAFORMAT_KEY_MPEGH_REFERENCE_CHANNEL_LAYOUT \
55 "mpegh-reference-channel-layout"
56 #define AMEDIAFORMAT_KEY_MPEGH_COMPATIBLE_SETS \
57 "mpegh-compatible-sets"
58
59 namespace {
60 // TODO: this should possibly be handled in an else
61 constexpr static int32_t AACObjectNull = 0;
62
63 // TODO: decide if we should just not transmit the level in this case
64 constexpr static int32_t DolbyVisionLevelUnknown = 0;
65 }
66
67 namespace android {
68
copyNALUToABuffer(sp<ABuffer> * buffer,const uint8_t * ptr,size_t length)69 static status_t copyNALUToABuffer(sp<ABuffer> *buffer, const uint8_t *ptr, size_t length) {
70 if (((*buffer)->size() + 4 + length) > ((*buffer)->capacity() - (*buffer)->offset())) {
71 sp<ABuffer> tmpBuffer = new (std::nothrow) ABuffer((*buffer)->size() + 4 + length + 1024);
72 if (tmpBuffer.get() == NULL || tmpBuffer->base() == NULL) {
73 return NO_MEMORY;
74 }
75 memcpy(tmpBuffer->data(), (*buffer)->data(), (*buffer)->size());
76 tmpBuffer->setRange(0, (*buffer)->size());
77 (*buffer) = tmpBuffer;
78 }
79
80 memcpy((*buffer)->data() + (*buffer)->size(), "\x00\x00\x00\x01", 4);
81 memcpy((*buffer)->data() + (*buffer)->size() + 4, ptr, length);
82 (*buffer)->setRange((*buffer)->offset(), (*buffer)->size() + 4 + length);
83 return OK;
84 }
85
86 #if 0
87 static void convertMetaDataToMessageInt32(
88 const sp<MetaData> &meta, sp<AMessage> &msg, uint32_t key, const char *name) {
89 int32_t value;
90 if (meta->findInt32(key, &value)) {
91 msg->setInt32(name, value);
92 }
93 }
94 #endif
95
convertMetaDataToMessageColorAspects(const MetaDataBase * meta,sp<AMessage> & msg)96 static void convertMetaDataToMessageColorAspects(const MetaDataBase *meta, sp<AMessage> &msg) {
97 // 0 values are unspecified
98 int32_t range = 0;
99 int32_t primaries = 0;
100 int32_t transferFunction = 0;
101 int32_t colorMatrix = 0;
102 meta->findInt32(kKeyColorRange, &range);
103 meta->findInt32(kKeyColorPrimaries, &primaries);
104 meta->findInt32(kKeyTransferFunction, &transferFunction);
105 meta->findInt32(kKeyColorMatrix, &colorMatrix);
106 ColorAspects colorAspects;
107 memset(&colorAspects, 0, sizeof(colorAspects));
108 colorAspects.mRange = (ColorAspects::Range)range;
109 colorAspects.mPrimaries = (ColorAspects::Primaries)primaries;
110 colorAspects.mTransfer = (ColorAspects::Transfer)transferFunction;
111 colorAspects.mMatrixCoeffs = (ColorAspects::MatrixCoeffs)colorMatrix;
112
113 int32_t rangeMsg, standardMsg, transferMsg;
114 if (CodecBase::convertCodecColorAspectsToPlatformAspects(
115 colorAspects, &rangeMsg, &standardMsg, &transferMsg) != OK) {
116 return;
117 }
118
119 // save specified values to msg
120 if (rangeMsg != 0) {
121 msg->setInt32("color-range", rangeMsg);
122 }
123 if (standardMsg != 0) {
124 msg->setInt32("color-standard", standardMsg);
125 }
126 if (transferMsg != 0) {
127 msg->setInt32("color-transfer", transferMsg);
128 }
129 }
130
131 /**
132 * Returns true if, and only if, the given format corresponds to HDR10 or HDR10+.
133 */
isHdr10or10Plus(const sp<AMessage> & format)134 static bool isHdr10or10Plus(const sp<AMessage> &format) {
135
136 // if user/container supplied HDR static info without transfer set, assume true
137 if ((format->contains("hdr-static-info") || format->contains("hdr10-plus-info"))
138 && !format->contains("color-transfer")) {
139 return true;
140 }
141 // otherwise, verify that an HDR transfer function is set
142 int32_t transfer;
143 if (format->findInt32("color-transfer", &transfer)) {
144 return transfer == ColorUtils::kColorTransferST2084;
145 }
146 return false;
147 }
148
parseAacProfileFromCsd(const sp<ABuffer> & csd,sp<AMessage> & format)149 static void parseAacProfileFromCsd(const sp<ABuffer> &csd, sp<AMessage> &format) {
150 if (csd->size() < 2) {
151 return;
152 }
153
154 uint16_t audioObjectType = U16_AT((uint8_t*)csd->data());
155 if ((audioObjectType & 0xF800) == 0xF800) {
156 audioObjectType = 32 + ((audioObjectType >> 5) & 0x3F);
157 } else {
158 audioObjectType >>= 11;
159 }
160
161
162 const static ALookup<uint16_t, int32_t> profiles {
163 { 1, AACObjectMain },
164 { 2, AACObjectLC },
165 { 3, AACObjectSSR },
166 { 4, AACObjectLTP },
167 { 5, AACObjectHE },
168 { 6, AACObjectScalable },
169 { 17, AACObjectERLC },
170 { 23, AACObjectLD },
171 { 29, AACObjectHE_PS },
172 { 39, AACObjectELD },
173 { 42, AACObjectXHE },
174 };
175
176 int32_t profile;
177 if (profiles.map(audioObjectType, &profile)) {
178 format->setInt32("profile", profile);
179 }
180 }
181
parseAvcProfileLevelFromAvcc(const uint8_t * ptr,size_t size,sp<AMessage> & format)182 static void parseAvcProfileLevelFromAvcc(const uint8_t *ptr, size_t size, sp<AMessage> &format) {
183 if (size < 4 || ptr[0] != 1) { // configurationVersion == 1
184 return;
185 }
186 const uint8_t profile = ptr[1];
187 const uint8_t constraints = ptr[2];
188 const uint8_t level = ptr[3];
189
190 const static ALookup<uint8_t, int32_t> levels {
191 { 9, AVCLevel1b }, // technically, 9 is only used for High+ profiles
192 { 10, AVCLevel1 },
193 { 11, AVCLevel11 }, // prefer level 1.1 for the value 11
194 { 11, AVCLevel1b },
195 { 12, AVCLevel12 },
196 { 13, AVCLevel13 },
197 { 20, AVCLevel2 },
198 { 21, AVCLevel21 },
199 { 22, AVCLevel22 },
200 { 30, AVCLevel3 },
201 { 31, AVCLevel31 },
202 { 32, AVCLevel32 },
203 { 40, AVCLevel4 },
204 { 41, AVCLevel41 },
205 { 42, AVCLevel42 },
206 { 50, AVCLevel5 },
207 { 51, AVCLevel51 },
208 { 52, AVCLevel52 },
209 { 60, AVCLevel6 },
210 { 61, AVCLevel61 },
211 { 62, AVCLevel62 },
212 };
213 const static ALookup<uint8_t, int32_t> profiles {
214 { 66, AVCProfileBaseline },
215 { 77, AVCProfileMain },
216 { 88, AVCProfileExtended },
217 { 100, AVCProfileHigh },
218 { 110, AVCProfileHigh10 },
219 { 122, AVCProfileHigh422 },
220 { 244, AVCProfileHigh444 },
221 };
222
223 // set profile & level if they are recognized
224 int32_t codecProfile;
225 int32_t codecLevel;
226 if (profiles.map(profile, &codecProfile)) {
227 if (profile == 66 && (constraints & 0x40)) {
228 codecProfile = AVCProfileConstrainedBaseline;
229 } else if (profile == 100 && (constraints & 0x0C) == 0x0C) {
230 codecProfile = AVCProfileConstrainedHigh;
231 }
232 format->setInt32("profile", codecProfile);
233 if (levels.map(level, &codecLevel)) {
234 // for 9 && 11 decide level based on profile and constraint_set3 flag
235 if (level == 11 && (profile == 66 || profile == 77 || profile == 88)) {
236 codecLevel = (constraints & 0x10) ? AVCLevel1b : AVCLevel11;
237 }
238 format->setInt32("level", codecLevel);
239 }
240 }
241 }
242
getDolbyVisionProfileTable()243 static const ALookup<uint8_t, int32_t>& getDolbyVisionProfileTable() {
244 static const ALookup<uint8_t, int32_t> profileTable = {
245 {1, DolbyVisionProfileDvavPen},
246 {3, DolbyVisionProfileDvheDen},
247 {4, DolbyVisionProfileDvheDtr},
248 {5, DolbyVisionProfileDvheStn},
249 {6, DolbyVisionProfileDvheDth},
250 {7, DolbyVisionProfileDvheDtb},
251 {8, DolbyVisionProfileDvheSt},
252 {9, DolbyVisionProfileDvavSe},
253 {10, DolbyVisionProfileDvav110},
254 };
255 return profileTable;
256 }
257
getDolbyVisionLevelsTable()258 static const ALookup<uint8_t, int32_t>& getDolbyVisionLevelsTable() {
259 static const ALookup<uint8_t, int32_t> levelsTable = {
260 {0, DolbyVisionLevelUnknown},
261 {1, DolbyVisionLevelHd24},
262 {2, DolbyVisionLevelHd30},
263 {3, DolbyVisionLevelFhd24},
264 {4, DolbyVisionLevelFhd30},
265 {5, DolbyVisionLevelFhd60},
266 {6, DolbyVisionLevelUhd24},
267 {7, DolbyVisionLevelUhd30},
268 {8, DolbyVisionLevelUhd48},
269 {9, DolbyVisionLevelUhd60},
270 {10, DolbyVisionLevelUhd120},
271 {11, DolbyVisionLevel8k30},
272 {12, DolbyVisionLevel8k60},
273 };
274 return levelsTable;
275 }
parseDolbyVisionProfileLevelFromDvcc(const uint8_t * ptr,size_t size,sp<AMessage> & format)276 static void parseDolbyVisionProfileLevelFromDvcc(const uint8_t *ptr, size_t size, sp<AMessage> &format) {
277 // dv_major.dv_minor Should be 1.0 or 2.1
278 if (size != 24 || ((ptr[0] != 1 || ptr[1] != 0) && (ptr[0] != 2 || ptr[1] != 1))) {
279 ALOGV("Size %zu, dv_major %d, dv_minor %d", size, ptr[0], ptr[1]);
280 return;
281 }
282
283 const uint8_t profile = ptr[2] >> 1;
284 const uint8_t level = ((ptr[2] & 0x1) << 5) | ((ptr[3] >> 3) & 0x1f);
285 const uint8_t rpu_present_flag = (ptr[3] >> 2) & 0x01;
286 const uint8_t el_present_flag = (ptr[3] >> 1) & 0x01;
287 const uint8_t bl_present_flag = (ptr[3] & 0x01);
288 const int32_t bl_compatibility_id = (int32_t)(ptr[4] >> 4);
289
290 ALOGV("profile-level-compatibility value in dv(c|v)c box %d-%d-%d",
291 profile, level, bl_compatibility_id);
292
293 // All Dolby Profiles will have profile and level info in MediaFormat
294 // Profile 8 and 9 will have bl_compatibility_id too.
295 const ALookup<uint8_t, int32_t> &profiles = getDolbyVisionProfileTable();
296 const ALookup<uint8_t, int32_t> &levels = getDolbyVisionLevelsTable();
297
298 // set rpuAssoc
299 if (rpu_present_flag && el_present_flag && !bl_present_flag) {
300 format->setInt32("rpuAssoc", 1);
301 }
302 // set profile & level if they are recognized
303 int32_t codecProfile;
304 int32_t codecLevel;
305 if (profiles.map(profile, &codecProfile)) {
306 format->setInt32("profile", codecProfile);
307 if (codecProfile == DolbyVisionProfileDvheSt ||
308 codecProfile == DolbyVisionProfileDvavSe) {
309 format->setInt32("bl_compatibility_id", bl_compatibility_id);
310 }
311 if (levels.map(level, &codecLevel)) {
312 format->setInt32("level", codecLevel);
313 }
314 }
315 }
316
parseH263ProfileLevelFromD263(const uint8_t * ptr,size_t size,sp<AMessage> & format)317 static void parseH263ProfileLevelFromD263(const uint8_t *ptr, size_t size, sp<AMessage> &format) {
318 if (size < 7) {
319 return;
320 }
321
322 const uint8_t profile = ptr[6];
323 const uint8_t level = ptr[5];
324
325 const static ALookup<uint8_t, int32_t> profiles {
326 { 0, H263ProfileBaseline },
327 { 1, H263ProfileH320Coding },
328 { 2, H263ProfileBackwardCompatible },
329 { 3, H263ProfileISWV2 },
330 { 4, H263ProfileISWV3 },
331 { 5, H263ProfileHighCompression },
332 { 6, H263ProfileInternet },
333 { 7, H263ProfileInterlace },
334 { 8, H263ProfileHighLatency },
335 };
336
337 const static ALookup<uint8_t, int32_t> levels {
338 { 10, H263Level10 },
339 { 20, H263Level20 },
340 { 30, H263Level30 },
341 { 40, H263Level40 },
342 { 45, H263Level45 },
343 { 50, H263Level50 },
344 { 60, H263Level60 },
345 { 70, H263Level70 },
346 };
347
348 // set profile & level if they are recognized
349 int32_t codecProfile;
350 int32_t codecLevel;
351 if (profiles.map(profile, &codecProfile)) {
352 format->setInt32("profile", codecProfile);
353 if (levels.map(level, &codecLevel)) {
354 format->setInt32("level", codecLevel);
355 }
356 }
357 }
358
parseHevcProfileLevelFromHvcc(const uint8_t * ptr,size_t size,sp<AMessage> & format)359 static void parseHevcProfileLevelFromHvcc(const uint8_t *ptr, size_t size, sp<AMessage> &format) {
360 if (size < 13 || ptr[0] != 1) { // configurationVersion == 1
361 return;
362 }
363
364 const uint8_t profile = ptr[1] & 0x1F;
365 const uint8_t tier = (ptr[1] & 0x20) >> 5;
366 const uint8_t level = ptr[12];
367
368 const static ALookup<std::pair<uint8_t, uint8_t>, int32_t> levels {
369 { { 0, 30 }, HEVCMainTierLevel1 },
370 { { 0, 60 }, HEVCMainTierLevel2 },
371 { { 0, 63 }, HEVCMainTierLevel21 },
372 { { 0, 90 }, HEVCMainTierLevel3 },
373 { { 0, 93 }, HEVCMainTierLevel31 },
374 { { 0, 120 }, HEVCMainTierLevel4 },
375 { { 0, 123 }, HEVCMainTierLevel41 },
376 { { 0, 150 }, HEVCMainTierLevel5 },
377 { { 0, 153 }, HEVCMainTierLevel51 },
378 { { 0, 156 }, HEVCMainTierLevel52 },
379 { { 0, 180 }, HEVCMainTierLevel6 },
380 { { 0, 183 }, HEVCMainTierLevel61 },
381 { { 0, 186 }, HEVCMainTierLevel62 },
382 { { 1, 30 }, HEVCHighTierLevel1 },
383 { { 1, 60 }, HEVCHighTierLevel2 },
384 { { 1, 63 }, HEVCHighTierLevel21 },
385 { { 1, 90 }, HEVCHighTierLevel3 },
386 { { 1, 93 }, HEVCHighTierLevel31 },
387 { { 1, 120 }, HEVCHighTierLevel4 },
388 { { 1, 123 }, HEVCHighTierLevel41 },
389 { { 1, 150 }, HEVCHighTierLevel5 },
390 { { 1, 153 }, HEVCHighTierLevel51 },
391 { { 1, 156 }, HEVCHighTierLevel52 },
392 { { 1, 180 }, HEVCHighTierLevel6 },
393 { { 1, 183 }, HEVCHighTierLevel61 },
394 { { 1, 186 }, HEVCHighTierLevel62 },
395 };
396
397 const static ALookup<uint8_t, int32_t> profiles {
398 { 1, HEVCProfileMain },
399 { 2, HEVCProfileMain10 },
400 // use Main for Main Still Picture decoding
401 { 3, HEVCProfileMain },
402 };
403
404 // set profile & level if they are recognized
405 int32_t codecProfile;
406 int32_t codecLevel;
407 if (!profiles.map(profile, &codecProfile)) {
408 if (ptr[2] & 0x40 /* general compatibility flag 1 */) {
409 // Note that this case covers Main Still Picture too
410 codecProfile = HEVCProfileMain;
411 } else if (ptr[2] & 0x20 /* general compatibility flag 2 */) {
412 codecProfile = HEVCProfileMain10;
413 } else {
414 return;
415 }
416 }
417
418 // bump to HDR profile
419 if (isHdr10or10Plus(format) && codecProfile == HEVCProfileMain10) {
420 if (format->contains("hdr10-plus-info")) {
421 codecProfile = HEVCProfileMain10HDR10Plus;
422 } else {
423 codecProfile = HEVCProfileMain10HDR10;
424 }
425 }
426
427 format->setInt32("profile", codecProfile);
428 if (levels.map(std::make_pair(tier, level), &codecLevel)) {
429 format->setInt32("level", codecLevel);
430 }
431 }
432
parseMpeg2ProfileLevelFromHeader(const uint8_t * data,size_t size,sp<AMessage> & format)433 static void parseMpeg2ProfileLevelFromHeader(
434 const uint8_t *data, size_t size, sp<AMessage> &format) {
435 // find sequence extension
436 const uint8_t *seq = (const uint8_t*)memmem(data, size, "\x00\x00\x01\xB5", 4);
437 if (seq != NULL && seq + 5 < data + size) {
438 const uint8_t start_code = seq[4] >> 4;
439 if (start_code != 1 /* sequence extension ID */) {
440 return;
441 }
442 const uint8_t indication = ((seq[4] & 0xF) << 4) | ((seq[5] & 0xF0) >> 4);
443
444 const static ALookup<uint8_t, int32_t> profiles {
445 { 0x50, MPEG2ProfileSimple },
446 { 0x40, MPEG2ProfileMain },
447 { 0x30, MPEG2ProfileSNR },
448 { 0x20, MPEG2ProfileSpatial },
449 { 0x10, MPEG2ProfileHigh },
450 };
451
452 const static ALookup<uint8_t, int32_t> levels {
453 { 0x0A, MPEG2LevelLL },
454 { 0x08, MPEG2LevelML },
455 { 0x06, MPEG2LevelH14 },
456 { 0x04, MPEG2LevelHL },
457 { 0x02, MPEG2LevelHP },
458 };
459
460 const static ALookup<uint8_t,
461 std::pair<int32_t, int32_t>> escapes {
462 /* unsupported
463 { 0x8E, { XXX_MPEG2ProfileMultiView, MPEG2LevelLL } },
464 { 0x8D, { XXX_MPEG2ProfileMultiView, MPEG2LevelML } },
465 { 0x8B, { XXX_MPEG2ProfileMultiView, MPEG2LevelH14 } },
466 { 0x8A, { XXX_MPEG2ProfileMultiView, MPEG2LevelHL } }, */
467 { 0x85, { MPEG2Profile422, MPEG2LevelML } },
468 { 0x82, { MPEG2Profile422, MPEG2LevelHL } },
469 };
470
471 int32_t profile;
472 int32_t level;
473 std::pair<int32_t, int32_t> profileLevel;
474 if (escapes.map(indication, &profileLevel)) {
475 format->setInt32("profile", profileLevel.first);
476 format->setInt32("level", profileLevel.second);
477 } else if (profiles.map(indication & 0x70, &profile)) {
478 format->setInt32("profile", profile);
479 if (levels.map(indication & 0xF, &level)) {
480 format->setInt32("level", level);
481 }
482 }
483 }
484 }
485
parseMpeg2ProfileLevelFromEsds(ESDS & esds,sp<AMessage> & format)486 static void parseMpeg2ProfileLevelFromEsds(ESDS &esds, sp<AMessage> &format) {
487 // esds seems to only contain the profile for MPEG-2
488 uint8_t objType;
489 if (esds.getObjectTypeIndication(&objType) == OK) {
490 const static ALookup<uint8_t, int32_t> profiles{
491 { 0x60, MPEG2ProfileSimple },
492 { 0x61, MPEG2ProfileMain },
493 { 0x62, MPEG2ProfileSNR },
494 { 0x63, MPEG2ProfileSpatial },
495 { 0x64, MPEG2ProfileHigh },
496 { 0x65, MPEG2Profile422 },
497 };
498
499 int32_t profile;
500 if (profiles.map(objType, &profile)) {
501 format->setInt32("profile", profile);
502 }
503 }
504 }
505
parseMpeg4ProfileLevelFromCsd(const sp<ABuffer> & csd,sp<AMessage> & format)506 static void parseMpeg4ProfileLevelFromCsd(const sp<ABuffer> &csd, sp<AMessage> &format) {
507 const uint8_t *data = csd->data();
508 // find visual object sequence
509 const uint8_t *seq = (const uint8_t*)memmem(data, csd->size(), "\x00\x00\x01\xB0", 4);
510 if (seq != NULL && seq + 4 < data + csd->size()) {
511 const uint8_t indication = seq[4];
512
513 const static ALookup<uint8_t,
514 std::pair<int32_t, int32_t>> table {
515 { 0b00000001, { MPEG4ProfileSimple, MPEG4Level1 } },
516 { 0b00000010, { MPEG4ProfileSimple, MPEG4Level2 } },
517 { 0b00000011, { MPEG4ProfileSimple, MPEG4Level3 } },
518 { 0b00000100, { MPEG4ProfileSimple, MPEG4Level4a } },
519 { 0b00000101, { MPEG4ProfileSimple, MPEG4Level5 } },
520 { 0b00000110, { MPEG4ProfileSimple, MPEG4Level6 } },
521 { 0b00001000, { MPEG4ProfileSimple, MPEG4Level0 } },
522 { 0b00001001, { MPEG4ProfileSimple, MPEG4Level0b } },
523 { 0b00010000, { MPEG4ProfileSimpleScalable, MPEG4Level0 } },
524 { 0b00010001, { MPEG4ProfileSimpleScalable, MPEG4Level1 } },
525 { 0b00010010, { MPEG4ProfileSimpleScalable, MPEG4Level2 } },
526 /* unsupported
527 { 0b00011101, { XXX_MPEG4ProfileSimpleScalableER, MPEG4Level0 } },
528 { 0b00011110, { XXX_MPEG4ProfileSimpleScalableER, MPEG4Level1 } },
529 { 0b00011111, { XXX_MPEG4ProfileSimpleScalableER, MPEG4Level2 } }, */
530 { 0b00100001, { MPEG4ProfileCore, MPEG4Level1 } },
531 { 0b00100010, { MPEG4ProfileCore, MPEG4Level2 } },
532 { 0b00110010, { MPEG4ProfileMain, MPEG4Level2 } },
533 { 0b00110011, { MPEG4ProfileMain, MPEG4Level3 } },
534 { 0b00110100, { MPEG4ProfileMain, MPEG4Level4 } },
535 /* deprecated
536 { 0b01000010, { MPEG4ProfileNbit, MPEG4Level2 } }, */
537 { 0b01010001, { MPEG4ProfileScalableTexture, MPEG4Level1 } },
538 { 0b01100001, { MPEG4ProfileSimpleFace, MPEG4Level1 } },
539 { 0b01100010, { MPEG4ProfileSimpleFace, MPEG4Level2 } },
540 { 0b01100011, { MPEG4ProfileSimpleFBA, MPEG4Level1 } },
541 { 0b01100100, { MPEG4ProfileSimpleFBA, MPEG4Level2 } },
542 { 0b01110001, { MPEG4ProfileBasicAnimated, MPEG4Level1 } },
543 { 0b01110010, { MPEG4ProfileBasicAnimated, MPEG4Level2 } },
544 { 0b10000001, { MPEG4ProfileHybrid, MPEG4Level1 } },
545 { 0b10000010, { MPEG4ProfileHybrid, MPEG4Level2 } },
546 { 0b10010001, { MPEG4ProfileAdvancedRealTime, MPEG4Level1 } },
547 { 0b10010010, { MPEG4ProfileAdvancedRealTime, MPEG4Level2 } },
548 { 0b10010011, { MPEG4ProfileAdvancedRealTime, MPEG4Level3 } },
549 { 0b10010100, { MPEG4ProfileAdvancedRealTime, MPEG4Level4 } },
550 { 0b10100001, { MPEG4ProfileCoreScalable, MPEG4Level1 } },
551 { 0b10100010, { MPEG4ProfileCoreScalable, MPEG4Level2 } },
552 { 0b10100011, { MPEG4ProfileCoreScalable, MPEG4Level3 } },
553 { 0b10110001, { MPEG4ProfileAdvancedCoding, MPEG4Level1 } },
554 { 0b10110010, { MPEG4ProfileAdvancedCoding, MPEG4Level2 } },
555 { 0b10110011, { MPEG4ProfileAdvancedCoding, MPEG4Level3 } },
556 { 0b10110100, { MPEG4ProfileAdvancedCoding, MPEG4Level4 } },
557 { 0b11000001, { MPEG4ProfileAdvancedCore, MPEG4Level1 } },
558 { 0b11000010, { MPEG4ProfileAdvancedCore, MPEG4Level2 } },
559 { 0b11010001, { MPEG4ProfileAdvancedScalable, MPEG4Level1 } },
560 { 0b11010010, { MPEG4ProfileAdvancedScalable, MPEG4Level2 } },
561 { 0b11010011, { MPEG4ProfileAdvancedScalable, MPEG4Level3 } },
562 /* unsupported
563 { 0b11100001, { XXX_MPEG4ProfileSimpleStudio, MPEG4Level1 } },
564 { 0b11100010, { XXX_MPEG4ProfileSimpleStudio, MPEG4Level2 } },
565 { 0b11100011, { XXX_MPEG4ProfileSimpleStudio, MPEG4Level3 } },
566 { 0b11100100, { XXX_MPEG4ProfileSimpleStudio, MPEG4Level4 } },
567 { 0b11100101, { XXX_MPEG4ProfileCoreStudio, MPEG4Level1 } },
568 { 0b11100110, { XXX_MPEG4ProfileCoreStudio, MPEG4Level2 } },
569 { 0b11100111, { XXX_MPEG4ProfileCoreStudio, MPEG4Level3 } },
570 { 0b11101000, { XXX_MPEG4ProfileCoreStudio, MPEG4Level4 } },
571 { 0b11101011, { XXX_MPEG4ProfileSimpleStudio, MPEG4Level5 } },
572 { 0b11101100, { XXX_MPEG4ProfileSimpleStudio, MPEG4Level6 } }, */
573 { 0b11110000, { MPEG4ProfileAdvancedSimple, MPEG4Level0 } },
574 { 0b11110001, { MPEG4ProfileAdvancedSimple, MPEG4Level1 } },
575 { 0b11110010, { MPEG4ProfileAdvancedSimple, MPEG4Level2 } },
576 { 0b11110011, { MPEG4ProfileAdvancedSimple, MPEG4Level3 } },
577 { 0b11110100, { MPEG4ProfileAdvancedSimple, MPEG4Level4 } },
578 { 0b11110101, { MPEG4ProfileAdvancedSimple, MPEG4Level5 } },
579 { 0b11110111, { MPEG4ProfileAdvancedSimple, MPEG4Level3b } },
580 /* deprecated
581 { 0b11111000, { XXX_MPEG4ProfileFineGranularityScalable, MPEG4Level0 } },
582 { 0b11111001, { XXX_MPEG4ProfileFineGranularityScalable, MPEG4Level1 } },
583 { 0b11111010, { XXX_MPEG4ProfileFineGranularityScalable, MPEG4Level2 } },
584 { 0b11111011, { XXX_MPEG4ProfileFineGranularityScalable, MPEG4Level3 } },
585 { 0b11111100, { XXX_MPEG4ProfileFineGranularityScalable, MPEG4Level4 } },
586 { 0b11111101, { XXX_MPEG4ProfileFineGranularityScalable, MPEG4Level5 } }, */
587 };
588
589 std::pair<int32_t, int32_t> profileLevel;
590 if (table.map(indication, &profileLevel)) {
591 format->setInt32("profile", profileLevel.first);
592 format->setInt32("level", profileLevel.second);
593 }
594 }
595 }
596
parseVp9ProfileLevelFromCsd(const sp<ABuffer> & csd,sp<AMessage> & format)597 static void parseVp9ProfileLevelFromCsd(const sp<ABuffer> &csd, sp<AMessage> &format) {
598 const uint8_t *data = csd->data();
599 size_t remaining = csd->size();
600
601 while (remaining >= 2) {
602 const uint8_t id = data[0];
603 const uint8_t length = data[1];
604 remaining -= 2;
605 data += 2;
606 if (length > remaining) {
607 break;
608 }
609 switch (id) {
610 case 1 /* profileId */:
611 if (length >= 1) {
612 const static ALookup<uint8_t, int32_t> profiles {
613 { 0, VP9Profile0 },
614 { 1, VP9Profile1 },
615 { 2, VP9Profile2 },
616 { 3, VP9Profile3 },
617 };
618
619 const static ALookup<int32_t, int32_t> toHdr10 {
620 { VP9Profile2, VP9Profile2HDR },
621 { VP9Profile3, VP9Profile3HDR },
622 };
623
624 const static ALookup<int32_t, int32_t> toHdr10Plus {
625 { VP9Profile2, VP9Profile2HDR10Plus },
626 { VP9Profile3, VP9Profile3HDR10Plus },
627 };
628
629 int32_t profile;
630 if (profiles.map(data[0], &profile)) {
631 // convert to HDR profile
632 if (isHdr10or10Plus(format)) {
633 if (format->contains("hdr10-plus-info")) {
634 toHdr10Plus.lookup(profile, &profile);
635 } else {
636 toHdr10.lookup(profile, &profile);
637 }
638 }
639
640 format->setInt32("profile", profile);
641 }
642 }
643 break;
644 case 2 /* levelId */:
645 if (length >= 1) {
646 const static ALookup<uint8_t, int32_t> levels {
647 { 10, VP9Level1 },
648 { 11, VP9Level11 },
649 { 20, VP9Level2 },
650 { 21, VP9Level21 },
651 { 30, VP9Level3 },
652 { 31, VP9Level31 },
653 { 40, VP9Level4 },
654 { 41, VP9Level41 },
655 { 50, VP9Level5 },
656 { 51, VP9Level51 },
657 { 52, VP9Level52 },
658 { 60, VP9Level6 },
659 { 61, VP9Level61 },
660 { 62, VP9Level62 },
661 };
662
663 int32_t level;
664 if (levels.map(data[0], &level)) {
665 format->setInt32("level", level);
666 }
667 }
668 break;
669 default:
670 break;
671 }
672 remaining -= length;
673 data += length;
674 }
675 }
676
parseAV1ProfileLevelFromCsd(const sp<ABuffer> & csd,sp<AMessage> & format)677 static void parseAV1ProfileLevelFromCsd(const sp<ABuffer> &csd, sp<AMessage> &format) {
678 // Parse CSD structure to extract profile level information
679 // https://aomediacodec.github.io/av1-isobmff/#av1codecconfigurationbox
680 const uint8_t *data = csd->data();
681 size_t remaining = csd->size();
682 if (remaining < 4 || data[0] != 0x81) { // configurationVersion == 1
683 return;
684 }
685 uint8_t profileData = (data[1] & 0xE0) >> 5;
686 uint8_t levelData = data[1] & 0x1F;
687 uint8_t highBitDepth = (data[2] & 0x40) >> 6;
688
689 const static ALookup<std::pair<uint8_t, uint8_t>, int32_t> profiles {
690 { { 0, 0 }, AV1ProfileMain8 },
691 { { 1, 0 }, AV1ProfileMain10 },
692 };
693
694 int32_t profile;
695 if (profiles.map(std::make_pair(highBitDepth, profileData), &profile)) {
696 // bump to HDR profile
697 if (isHdr10or10Plus(format) && profile == AV1ProfileMain10) {
698 if (format->contains("hdr10-plus-info")) {
699 profile = AV1ProfileMain10HDR10Plus;
700 } else {
701 profile = AV1ProfileMain10HDR10;
702 }
703 }
704 format->setInt32("profile", profile);
705 }
706 const static ALookup<uint8_t, int32_t> levels {
707 { 0, AV1Level2 },
708 { 1, AV1Level21 },
709 { 2, AV1Level22 },
710 { 3, AV1Level23 },
711 { 4, AV1Level3 },
712 { 5, AV1Level31 },
713 { 6, AV1Level32 },
714 { 7, AV1Level33 },
715 { 8, AV1Level4 },
716 { 9, AV1Level41 },
717 { 10, AV1Level42 },
718 { 11, AV1Level43 },
719 { 12, AV1Level5 },
720 { 13, AV1Level51 },
721 { 14, AV1Level52 },
722 { 15, AV1Level53 },
723 { 16, AV1Level6 },
724 { 17, AV1Level61 },
725 { 18, AV1Level62 },
726 { 19, AV1Level63 },
727 { 20, AV1Level7 },
728 { 21, AV1Level71 },
729 { 22, AV1Level72 },
730 { 23, AV1Level73 },
731 };
732
733 int32_t level;
734 if (levels.map(levelData, &level)) {
735 format->setInt32("level", level);
736 }
737 }
738
parseAPVProfileLevelFromCsd(const sp<ABuffer> & csd,sp<AMessage> & format)739 static void parseAPVProfileLevelFromCsd(const sp<ABuffer>& csd, sp<AMessage>& format) {
740 // Parse CSD structure to extract profile level information
741 // https://github.com/openapv/openapv/blob/main/readme/apv_isobmff.md#syntax-1
742 const uint8_t* data = csd->data();
743 size_t csdSize = csd->size();
744 if (csdSize < 17 || data[0] != 0x01) { // configurationVersion == 1
745 ALOGE("CSD is not according APV Configuration Standard");
746 return;
747 }
748 uint8_t profileData = data[5]; // profile_idc
749 uint8_t levelData = data[6]; // level_idc
750 uint8_t band = data[7]; // band_idc
751 uint8_t bitDepth = (data[16] & 0x0F) + 8; // bit_depth_minus8
752
753 const static ALookup<std::pair<uint8_t, uint8_t>, int32_t> profiles{
754 {{33, 10}, APVProfile422_10},
755 {{44, 12}, APVProfile422_10HDR10Plus},
756 };
757 int32_t profile;
758 if (profiles.map(std::make_pair(profileData, bitDepth), &profile)) {
759 // bump to HDR profile
760 if (isHdr10or10Plus(format) && profile == APVProfile422_10) {
761 if (format->contains("hdr-static-info")) {
762 profile = APVProfile422_10HDR10;
763 }
764 }
765 format->setInt32("profile", profile);
766 }
767 int level_num = (levelData / 30) * 2;
768 if (levelData % 30 == 0) {
769 level_num -= 1;
770 }
771 int32_t level = ((0x100 << (level_num - 1)) | (1 << band));
772 format->setInt32("level", level);
773 }
774
775 static std::vector<std::pair<const char *, uint32_t>> stringMappings {
776 {
777 { "album", kKeyAlbum },
778 { "albumartist", kKeyAlbumArtist },
779 { "artist", kKeyArtist },
780 { "author", kKeyAuthor },
781 { "cdtracknum", kKeyCDTrackNumber },
782 { "compilation", kKeyCompilation },
783 { "composer", kKeyComposer },
784 { "date", kKeyDate },
785 { "discnum", kKeyDiscNumber },
786 { "genre", kKeyGenre },
787 { "location", kKeyLocation },
788 { "lyricist", kKeyWriter },
789 { "manufacturer", kKeyManufacturer },
790 { "title", kKeyTitle },
791 { "year", kKeyYear },
792 }
793 };
794
795 static std::vector<std::pair<const char *, uint32_t>> floatMappings {
796 {
797 { "capture-rate", kKeyCaptureFramerate },
798 }
799 };
800
801 static std::vector<std::pair<const char*, uint32_t>> int64Mappings {
802 {
803 { "exif-offset", kKeyExifOffset},
804 { "exif-size", kKeyExifSize},
805 { "xmp-offset", kKeyXmpOffset},
806 { "xmp-size", kKeyXmpSize},
807 { "target-time", kKeyTargetTime},
808 { "thumbnail-time", kKeyThumbnailTime},
809 { "timeUs", kKeyTime},
810 { "durationUs", kKeyDuration},
811 { "sample-file-offset", kKeySampleFileOffset},
812 { "last-sample-index-in-chunk", kKeyLastSampleIndexInChunk},
813 { "sample-time-before-append", kKeySampleTimeBeforeAppend},
814 }
815 };
816
817 static std::vector<std::pair<const char *, uint32_t>> int32Mappings {
818 {
819 { "loop", kKeyAutoLoop },
820 { "time-scale", kKeyTimeScale },
821 { "crypto-mode", kKeyCryptoMode },
822 { "crypto-default-iv-size", kKeyCryptoDefaultIVSize },
823 { "crypto-encrypted-byte-block", kKeyEncryptedByteBlock },
824 { "crypto-skip-byte-block", kKeySkipByteBlock },
825 { "frame-count", kKeyFrameCount },
826 { "max-bitrate", kKeyMaxBitRate },
827 { "pcm-big-endian", kKeyPcmBigEndian },
828 { "temporal-layer-count", kKeyTemporalLayerCount },
829 { "temporal-layer-id", kKeyTemporalLayerId },
830 { "thumbnail-width", kKeyThumbnailWidth },
831 { "thumbnail-height", kKeyThumbnailHeight },
832 { "track-id", kKeyTrackID },
833 { "valid-samples", kKeyValidSamples },
834 { "dvb-component-tag", kKeyDvbComponentTag},
835 { "dvb-audio-description", kKeyDvbAudioDescription},
836 { "dvb-teletext-magazine-number", kKeyDvbTeletextMagazineNumber},
837 { "dvb-teletext-page-number", kKeyDvbTeletextPageNumber},
838 { "profile", kKeyAudioProfile },
839 { "level", kKeyAudioLevel },
840 }
841 };
842
843 static std::vector<std::pair<const char *, uint32_t>> bufferMappings {
844 {
845 { "albumart", kKeyAlbumArt },
846 { "audio-presentation-info", kKeyAudioPresentationInfo },
847 { "pssh", kKeyPssh },
848 { "crypto-iv", kKeyCryptoIV },
849 { "crypto-key", kKeyCryptoKey },
850 { "crypto-encrypted-sizes", kKeyEncryptedSizes },
851 { "crypto-plain-sizes", kKeyPlainSizes },
852 { "icc-profile", kKeyIccProfile },
853 { "sei", kKeySEI },
854 { "text-format-data", kKeyTextFormatData },
855 { "thumbnail-csd-hevc", kKeyThumbnailHVCC },
856 { "slow-motion-markers", kKeySlowMotionMarkers },
857 { "thumbnail-csd-av1c", kKeyThumbnailAV1C },
858 }
859 };
860
861 static std::vector<std::pair<const char *, uint32_t>> CSDMappings {
862 {
863 { "csd-0", kKeyOpaqueCSD0 },
864 { "csd-1", kKeyOpaqueCSD1 },
865 { "csd-2", kKeyOpaqueCSD2 },
866 }
867 };
868
convertMessageToMetaDataFromMappings(const sp<AMessage> & msg,sp<MetaData> & meta)869 void convertMessageToMetaDataFromMappings(const sp<AMessage> &msg, sp<MetaData> &meta) {
870 for (auto elem : stringMappings) {
871 AString value;
872 if (msg->findString(elem.first, &value)) {
873 meta->setCString(elem.second, value.c_str());
874 }
875 }
876
877 for (auto elem : floatMappings) {
878 float value;
879 if (msg->findFloat(elem.first, &value)) {
880 meta->setFloat(elem.second, value);
881 }
882 }
883
884 for (auto elem : int64Mappings) {
885 int64_t value;
886 if (msg->findInt64(elem.first, &value)) {
887 meta->setInt64(elem.second, value);
888 }
889 }
890
891 for (auto elem : int32Mappings) {
892 int32_t value;
893 if (msg->findInt32(elem.first, &value)) {
894 meta->setInt32(elem.second, value);
895 }
896 }
897
898 for (auto elem : bufferMappings) {
899 sp<ABuffer> value;
900 if (msg->findBuffer(elem.first, &value)) {
901 meta->setData(elem.second,
902 MetaDataBase::Type::TYPE_NONE, value->data(), value->size());
903 }
904 }
905
906 for (auto elem : CSDMappings) {
907 sp<ABuffer> value;
908 if (msg->findBuffer(elem.first, &value)) {
909 meta->setData(elem.second,
910 MetaDataBase::Type::TYPE_NONE, value->data(), value->size());
911 }
912 }
913 }
914
convertMetaDataToMessageFromMappings(const MetaDataBase * meta,sp<AMessage> format)915 void convertMetaDataToMessageFromMappings(const MetaDataBase *meta, sp<AMessage> format) {
916 for (auto elem : stringMappings) {
917 const char *value;
918 if (meta->findCString(elem.second, &value)) {
919 format->setString(elem.first, value, strlen(value));
920 }
921 }
922
923 for (auto elem : floatMappings) {
924 float value;
925 if (meta->findFloat(elem.second, &value)) {
926 format->setFloat(elem.first, value);
927 }
928 }
929
930 for (auto elem : int64Mappings) {
931 int64_t value;
932 if (meta->findInt64(elem.second, &value)) {
933 format->setInt64(elem.first, value);
934 }
935 }
936
937 for (auto elem : int32Mappings) {
938 int32_t value;
939 if (meta->findInt32(elem.second, &value)) {
940 format->setInt32(elem.first, value);
941 }
942 }
943
944 for (auto elem : bufferMappings) {
945 uint32_t type;
946 const void* data;
947 size_t size;
948 if (meta->findData(elem.second, &type, &data, &size)) {
949 sp<ABuffer> buf = ABuffer::CreateAsCopy(data, size);
950 format->setBuffer(elem.first, buf);
951 }
952 }
953
954 for (auto elem : CSDMappings) {
955 uint32_t type;
956 const void* data;
957 size_t size;
958 if (meta->findData(elem.second, &type, &data, &size)) {
959 sp<ABuffer> buf = ABuffer::CreateAsCopy(data, size);
960 buf->meta()->setInt32("csd", true);
961 buf->meta()->setInt64("timeUs", 0);
962 format->setBuffer(elem.first, buf);
963 }
964 }
965 }
966
convertMetaDataToMessage(const sp<MetaData> & meta,sp<AMessage> * format)967 status_t convertMetaDataToMessage(
968 const sp<MetaData> &meta, sp<AMessage> *format) {
969 return convertMetaDataToMessage(meta.get(), format);
970 }
971
convertMetaDataToMessage(const MetaDataBase * meta,sp<AMessage> * format)972 status_t convertMetaDataToMessage(
973 const MetaDataBase *meta, sp<AMessage> *format) {
974
975 format->clear();
976
977 if (meta == NULL) {
978 ALOGE("convertMetaDataToMessage: NULL input");
979 return BAD_VALUE;
980 }
981
982 const char *mime;
983 if (!meta->findCString(kKeyMIMEType, &mime)) {
984 return BAD_VALUE;
985 }
986
987 sp<AMessage> msg = new AMessage;
988 msg->setString("mime", mime);
989
990 convertMetaDataToMessageFromMappings(meta, msg);
991
992 uint32_t type;
993 const void *data;
994 size_t size;
995 if (meta->findData(kKeyCASessionID, &type, &data, &size)) {
996 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
997 if (buffer.get() == NULL || buffer->base() == NULL) {
998 return NO_MEMORY;
999 }
1000
1001 msg->setBuffer("ca-session-id", buffer);
1002 memcpy(buffer->data(), data, size);
1003 }
1004
1005 if (meta->findData(kKeyCAPrivateData, &type, &data, &size)) {
1006 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1007 if (buffer.get() == NULL || buffer->base() == NULL) {
1008 return NO_MEMORY;
1009 }
1010
1011 msg->setBuffer("ca-private-data", buffer);
1012 memcpy(buffer->data(), data, size);
1013 }
1014
1015 int32_t systemId;
1016 if (meta->findInt32(kKeyCASystemID, &systemId)) {
1017 msg->setInt32("ca-system-id", systemId);
1018 }
1019
1020 if (!strncasecmp("video/scrambled", mime, 15)
1021 || !strncasecmp("audio/scrambled", mime, 15)) {
1022
1023 *format = msg;
1024 return OK;
1025 }
1026
1027 int64_t durationUs;
1028 if (meta->findInt64(kKeyDuration, &durationUs)) {
1029 msg->setInt64("durationUs", durationUs);
1030 }
1031
1032 int32_t avgBitRate = 0;
1033 if (meta->findInt32(kKeyBitRate, &avgBitRate) && avgBitRate > 0) {
1034 msg->setInt32("bitrate", avgBitRate);
1035 }
1036
1037 int32_t maxBitRate;
1038 if (meta->findInt32(kKeyMaxBitRate, &maxBitRate)
1039 && maxBitRate > 0 && maxBitRate >= avgBitRate) {
1040 msg->setInt32("max-bitrate", maxBitRate);
1041 }
1042
1043 int32_t isSync;
1044 if (meta->findInt32(kKeyIsSyncFrame, &isSync) && isSync != 0) {
1045 msg->setInt32("is-sync-frame", 1);
1046 }
1047
1048 int32_t dvbComponentTag = 0;
1049 if (meta->findInt32(kKeyDvbComponentTag, &dvbComponentTag)) {
1050 msg->setInt32("dvb-component-tag", dvbComponentTag);
1051 }
1052
1053 int32_t dvbAudioDescription = 0;
1054 if (meta->findInt32(kKeyDvbAudioDescription, &dvbAudioDescription)) {
1055 msg->setInt32("dvb-audio-description", dvbAudioDescription);
1056 }
1057
1058 int32_t dvbTeletextMagazineNumber = 0;
1059 if (meta->findInt32(kKeyDvbTeletextMagazineNumber, &dvbTeletextMagazineNumber)) {
1060 msg->setInt32("dvb-teletext-magazine-number", dvbTeletextMagazineNumber);
1061 }
1062
1063 int32_t dvbTeletextPageNumber = 0;
1064 if (meta->findInt32(kKeyDvbTeletextPageNumber, &dvbTeletextPageNumber)) {
1065 msg->setInt32("dvb-teletext-page-number", dvbTeletextPageNumber);
1066 }
1067
1068 const char *lang;
1069 if (meta->findCString(kKeyMediaLanguage, &lang)) {
1070 msg->setString("language", lang);
1071 }
1072
1073 if (!strncasecmp("video/", mime, 6) ||
1074 !strncasecmp("image/", mime, 6)) {
1075 int32_t width, height;
1076 if (!meta->findInt32(kKeyWidth, &width)
1077 || !meta->findInt32(kKeyHeight, &height)) {
1078 return BAD_VALUE;
1079 }
1080
1081 msg->setInt32("width", width);
1082 msg->setInt32("height", height);
1083
1084 int32_t displayWidth, displayHeight;
1085 if (meta->findInt32(kKeyDisplayWidth, &displayWidth)
1086 && meta->findInt32(kKeyDisplayHeight, &displayHeight)) {
1087 msg->setInt32("display-width", displayWidth);
1088 msg->setInt32("display-height", displayHeight);
1089 }
1090
1091 int32_t sarWidth, sarHeight;
1092 if (meta->findInt32(kKeySARWidth, &sarWidth)
1093 && meta->findInt32(kKeySARHeight, &sarHeight)) {
1094 msg->setInt32("sar-width", sarWidth);
1095 msg->setInt32("sar-height", sarHeight);
1096 }
1097
1098 if (!strncasecmp("image/", mime, 6)) {
1099 int32_t tileWidth, tileHeight, gridRows, gridCols;
1100 if (meta->findInt32(kKeyTileWidth, &tileWidth)
1101 && meta->findInt32(kKeyTileHeight, &tileHeight)
1102 && meta->findInt32(kKeyGridRows, &gridRows)
1103 && meta->findInt32(kKeyGridCols, &gridCols)) {
1104 msg->setInt32("tile-width", tileWidth);
1105 msg->setInt32("tile-height", tileHeight);
1106 msg->setInt32("grid-rows", gridRows);
1107 msg->setInt32("grid-cols", gridCols);
1108 }
1109 int32_t isPrimary;
1110 if (meta->findInt32(kKeyTrackIsDefault, &isPrimary) && isPrimary) {
1111 msg->setInt32("is-default", 1);
1112 }
1113 }
1114
1115 int32_t colorFormat;
1116 if (meta->findInt32(kKeyColorFormat, &colorFormat)) {
1117 msg->setInt32("color-format", colorFormat);
1118 }
1119
1120 int32_t cropLeft, cropTop, cropRight, cropBottom;
1121 if (meta->findRect(kKeyCropRect,
1122 &cropLeft,
1123 &cropTop,
1124 &cropRight,
1125 &cropBottom)) {
1126 msg->setRect("crop", cropLeft, cropTop, cropRight, cropBottom);
1127 }
1128
1129 int32_t rotationDegrees;
1130 if (meta->findInt32(kKeyRotation, &rotationDegrees)) {
1131 msg->setInt32("rotation-degrees", rotationDegrees);
1132 }
1133
1134 uint32_t type;
1135 const void *data;
1136 size_t size;
1137 if (meta->findData(kKeyHdrStaticInfo, &type, &data, &size)
1138 && type == 'hdrS' && size == sizeof(HDRStaticInfo)) {
1139 ColorUtils::setHDRStaticInfoIntoFormat(*(HDRStaticInfo*)data, msg);
1140 }
1141
1142 if (meta->findData(kKeyHdr10PlusInfo, &type, &data, &size)
1143 && size > 0) {
1144 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1145 if (buffer.get() == NULL || buffer->base() == NULL) {
1146 return NO_MEMORY;
1147 }
1148 memcpy(buffer->data(), data, size);
1149 msg->setBuffer("hdr10-plus-info", buffer);
1150 }
1151
1152 convertMetaDataToMessageColorAspects(meta, msg);
1153 } else if (!strncasecmp("audio/", mime, 6)) {
1154 int32_t numChannels, sampleRate;
1155 if (!meta->findInt32(kKeyChannelCount, &numChannels)
1156 || !meta->findInt32(kKeySampleRate, &sampleRate)) {
1157 return BAD_VALUE;
1158 }
1159
1160 msg->setInt32("channel-count", numChannels);
1161 msg->setInt32("sample-rate", sampleRate);
1162
1163 int32_t bitsPerSample;
1164 if (meta->findInt32(kKeyBitsPerSample, &bitsPerSample)) {
1165 msg->setInt32("bits-per-sample", bitsPerSample);
1166 }
1167
1168 int32_t channelMask;
1169 if (meta->findInt32(kKeyChannelMask, &channelMask)) {
1170 msg->setInt32("channel-mask", channelMask);
1171 }
1172
1173 int32_t delay = 0;
1174 if (meta->findInt32(kKeyEncoderDelay, &delay)) {
1175 msg->setInt32("encoder-delay", delay);
1176 }
1177 int32_t padding = 0;
1178 if (meta->findInt32(kKeyEncoderPadding, &padding)) {
1179 msg->setInt32("encoder-padding", padding);
1180 }
1181
1182 int32_t isADTS;
1183 if (meta->findInt32(kKeyIsADTS, &isADTS)) {
1184 msg->setInt32("is-adts", isADTS);
1185 }
1186
1187 int32_t mpeghProfileLevelIndication;
1188 if (meta->findInt32(kKeyMpeghProfileLevelIndication, &mpeghProfileLevelIndication)) {
1189 msg->setInt32(AMEDIAFORMAT_KEY_MPEGH_PROFILE_LEVEL_INDICATION,
1190 mpeghProfileLevelIndication);
1191 }
1192 int32_t mpeghReferenceChannelLayout;
1193 if (meta->findInt32(kKeyMpeghReferenceChannelLayout, &mpeghReferenceChannelLayout)) {
1194 msg->setInt32(AMEDIAFORMAT_KEY_MPEGH_REFERENCE_CHANNEL_LAYOUT,
1195 mpeghReferenceChannelLayout);
1196 }
1197 if (meta->findData(kKeyMpeghCompatibleSets, &type, &data, &size)) {
1198 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1199 if (buffer.get() == NULL || buffer->base() == NULL) {
1200 return NO_MEMORY;
1201 }
1202 msg->setBuffer(AMEDIAFORMAT_KEY_MPEGH_COMPATIBLE_SETS, buffer);
1203 memcpy(buffer->data(), data, size);
1204 }
1205
1206 int32_t aacProfile = -1;
1207 if (meta->findInt32(kKeyAACAOT, &aacProfile)) {
1208 msg->setInt32("aac-profile", aacProfile);
1209 }
1210
1211 int32_t pcmEncoding;
1212 if (meta->findInt32(kKeyPcmEncoding, &pcmEncoding)) {
1213 msg->setInt32("pcm-encoding", pcmEncoding);
1214 }
1215
1216 int32_t hapticChannelCount;
1217 if (meta->findInt32(kKeyHapticChannelCount, &hapticChannelCount)) {
1218 msg->setInt32("haptic-channel-count", hapticChannelCount);
1219 }
1220 }
1221
1222 int32_t maxInputSize;
1223 if (meta->findInt32(kKeyMaxInputSize, &maxInputSize)) {
1224 msg->setInt32("max-input-size", maxInputSize);
1225 }
1226
1227 int32_t maxWidth;
1228 if (meta->findInt32(kKeyMaxWidth, &maxWidth)) {
1229 msg->setInt32("max-width", maxWidth);
1230 }
1231
1232 int32_t maxHeight;
1233 if (meta->findInt32(kKeyMaxHeight, &maxHeight)) {
1234 msg->setInt32("max-height", maxHeight);
1235 }
1236
1237 int32_t rotationDegrees;
1238 if (meta->findInt32(kKeyRotation, &rotationDegrees)) {
1239 msg->setInt32("rotation-degrees", rotationDegrees);
1240 }
1241
1242 int32_t fps;
1243 if (meta->findInt32(kKeyFrameRate, &fps) && fps > 0) {
1244 msg->setInt32("frame-rate", fps);
1245 }
1246
1247 if (meta->findData(kKeyAVCC, &type, &data, &size)) {
1248 // Parse the AVCDecoderConfigurationRecord
1249
1250 const uint8_t *ptr = (const uint8_t *)data;
1251
1252 if (size < 7 || ptr[0] != 1) { // configurationVersion == 1
1253 ALOGE("b/23680780");
1254 return BAD_VALUE;
1255 }
1256
1257 parseAvcProfileLevelFromAvcc(ptr, size, msg);
1258
1259 // There is decodable content out there that fails the following
1260 // assertion, let's be lenient for now...
1261 // CHECK((ptr[4] >> 2) == 0x3f); // reserved
1262
1263 // we can get lengthSize value from 1 + (ptr[4] & 3)
1264
1265 // commented out check below as H264_QVGA_500_NO_AUDIO.3gp
1266 // violates it...
1267 // CHECK((ptr[5] >> 5) == 7); // reserved
1268
1269 size_t numSeqParameterSets = ptr[5] & 31;
1270
1271 ptr += 6;
1272 size -= 6;
1273
1274 sp<ABuffer> buffer = new (std::nothrow) ABuffer(1024);
1275 if (buffer.get() == NULL || buffer->base() == NULL) {
1276 return NO_MEMORY;
1277 }
1278 buffer->setRange(0, 0);
1279
1280 for (size_t i = 0; i < numSeqParameterSets; ++i) {
1281 if (size < 2) {
1282 ALOGE("b/23680780");
1283 return BAD_VALUE;
1284 }
1285 size_t length = U16_AT(ptr);
1286
1287 ptr += 2;
1288 size -= 2;
1289
1290 if (size < length) {
1291 return BAD_VALUE;
1292 }
1293 status_t err = copyNALUToABuffer(&buffer, ptr, length);
1294 if (err != OK) {
1295 return err;
1296 }
1297
1298 ptr += length;
1299 size -= length;
1300 }
1301
1302 buffer->meta()->setInt32("csd", true);
1303 buffer->meta()->setInt64("timeUs", 0);
1304
1305 msg->setBuffer("csd-0", buffer);
1306
1307 buffer = new (std::nothrow) ABuffer(1024);
1308 if (buffer.get() == NULL || buffer->base() == NULL) {
1309 return NO_MEMORY;
1310 }
1311 buffer->setRange(0, 0);
1312
1313 if (size < 1) {
1314 ALOGE("b/23680780");
1315 return BAD_VALUE;
1316 }
1317 size_t numPictureParameterSets = *ptr;
1318 ++ptr;
1319 --size;
1320
1321 for (size_t i = 0; i < numPictureParameterSets; ++i) {
1322 if (size < 2) {
1323 ALOGE("b/23680780");
1324 return BAD_VALUE;
1325 }
1326 size_t length = U16_AT(ptr);
1327
1328 ptr += 2;
1329 size -= 2;
1330
1331 if (size < length) {
1332 return BAD_VALUE;
1333 }
1334 status_t err = copyNALUToABuffer(&buffer, ptr, length);
1335 if (err != OK) {
1336 return err;
1337 }
1338
1339 ptr += length;
1340 size -= length;
1341 }
1342
1343 buffer->meta()->setInt32("csd", true);
1344 buffer->meta()->setInt64("timeUs", 0);
1345 msg->setBuffer("csd-1", buffer);
1346 } else if (meta->findData(kKeyHVCC, &type, &data, &size)) {
1347 const uint8_t *ptr = (const uint8_t *)data;
1348
1349 if (size < 23 || (ptr[0] != 1 && ptr[0] != 0)) {
1350 // configurationVersion == 1 or 0
1351 // 1 is what the standard dictates, but some old muxers may have used 0.
1352 ALOGE("b/23680780");
1353 return BAD_VALUE;
1354 }
1355
1356 const size_t dataSize = size; // save for later
1357 ptr += 22;
1358 size -= 22;
1359
1360 size_t numofArrays = (char)ptr[0];
1361 ptr += 1;
1362 size -= 1;
1363 size_t j = 0, i = 0;
1364
1365 sp<ABuffer> buffer = new (std::nothrow) ABuffer(1024);
1366 if (buffer.get() == NULL || buffer->base() == NULL) {
1367 return NO_MEMORY;
1368 }
1369 buffer->setRange(0, 0);
1370
1371 HevcParameterSets hvcc;
1372
1373 for (i = 0; i < numofArrays; i++) {
1374 if (size < 3) {
1375 ALOGE("b/23680780");
1376 return BAD_VALUE;
1377 }
1378 ptr += 1;
1379 size -= 1;
1380
1381 //Num of nals
1382 size_t numofNals = U16_AT(ptr);
1383
1384 ptr += 2;
1385 size -= 2;
1386
1387 for (j = 0; j < numofNals; j++) {
1388 if (size < 2) {
1389 ALOGE("b/23680780");
1390 return BAD_VALUE;
1391 }
1392 size_t length = U16_AT(ptr);
1393
1394 ptr += 2;
1395 size -= 2;
1396
1397 if (size < length) {
1398 return BAD_VALUE;
1399 }
1400 status_t err = copyNALUToABuffer(&buffer, ptr, length);
1401 if (err != OK) {
1402 return err;
1403 }
1404 (void)hvcc.addNalUnit(ptr, length);
1405
1406 ptr += length;
1407 size -= length;
1408 }
1409 }
1410 buffer->meta()->setInt32("csd", true);
1411 buffer->meta()->setInt64("timeUs", 0);
1412 msg->setBuffer("csd-0", buffer);
1413
1414 // if we saw VUI color information we know whether this is HDR because VUI trumps other
1415 // format parameters for HEVC.
1416 HevcParameterSets::Info info = hvcc.getInfo();
1417 if (info & hvcc.kInfoHasColorDescription) {
1418 msg->setInt32("android._is-hdr", (info & hvcc.kInfoIsHdr) != 0);
1419 }
1420
1421 uint32_t isoPrimaries, isoTransfer, isoMatrix, isoRange;
1422 if (hvcc.findParam32(kColourPrimaries, &isoPrimaries)
1423 && hvcc.findParam32(kTransferCharacteristics, &isoTransfer)
1424 && hvcc.findParam32(kMatrixCoeffs, &isoMatrix)
1425 && hvcc.findParam32(kVideoFullRangeFlag, &isoRange)) {
1426 ALOGV("found iso color aspects : primaris=%d, transfer=%d, matrix=%d, range=%d",
1427 isoPrimaries, isoTransfer, isoMatrix, isoRange);
1428
1429 ColorAspects aspects;
1430 ColorUtils::convertIsoColorAspectsToCodecAspects(
1431 isoPrimaries, isoTransfer, isoMatrix, isoRange, aspects);
1432
1433 if (aspects.mPrimaries == ColorAspects::PrimariesUnspecified) {
1434 int32_t primaries;
1435 if (meta->findInt32(kKeyColorPrimaries, &primaries)) {
1436 ALOGV("unspecified primaries found, replaced to %d", primaries);
1437 aspects.mPrimaries = static_cast<ColorAspects::Primaries>(primaries);
1438 }
1439 }
1440 if (aspects.mTransfer == ColorAspects::TransferUnspecified) {
1441 int32_t transferFunction;
1442 if (meta->findInt32(kKeyTransferFunction, &transferFunction)) {
1443 ALOGV("unspecified transfer found, replaced to %d", transferFunction);
1444 aspects.mTransfer = static_cast<ColorAspects::Transfer>(transferFunction);
1445 }
1446 }
1447 if (aspects.mMatrixCoeffs == ColorAspects::MatrixUnspecified) {
1448 int32_t colorMatrix;
1449 if (meta->findInt32(kKeyColorMatrix, &colorMatrix)) {
1450 ALOGV("unspecified matrix found, replaced to %d", colorMatrix);
1451 aspects.mMatrixCoeffs = static_cast<ColorAspects::MatrixCoeffs>(colorMatrix);
1452 }
1453 }
1454 if (aspects.mRange == ColorAspects::RangeUnspecified) {
1455 int32_t range;
1456 if (meta->findInt32(kKeyColorRange, &range)) {
1457 ALOGV("unspecified range found, replaced to %d", range);
1458 aspects.mRange = static_cast<ColorAspects::Range>(range);
1459 }
1460 }
1461
1462 int32_t standard, transfer, range;
1463 if (ColorUtils::convertCodecColorAspectsToPlatformAspects(
1464 aspects, &range, &standard, &transfer) == OK) {
1465 msg->setInt32("color-standard", standard);
1466 msg->setInt32("color-transfer", transfer);
1467 msg->setInt32("color-range", range);
1468 }
1469 }
1470
1471 parseHevcProfileLevelFromHvcc((const uint8_t *)data, dataSize, msg);
1472 } else if (meta->findData(kKeyAV1C, &type, &data, &size)) {
1473 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1474 if (buffer.get() == NULL || buffer->base() == NULL) {
1475 return NO_MEMORY;
1476 }
1477 memcpy(buffer->data(), data, size);
1478
1479 buffer->meta()->setInt32("csd", true);
1480 buffer->meta()->setInt64("timeUs", 0);
1481 msg->setBuffer("csd-0", buffer);
1482 parseAV1ProfileLevelFromCsd(buffer, msg);
1483 } else if (com::android::media::extractor::flags::extractor_mp4_enable_apv() &&
1484 meta->findData(kKeyAPVC, &type, &data, &size)) {
1485 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1486 if (buffer.get() == NULL || buffer->base() == NULL) {
1487 return NO_MEMORY;
1488 }
1489 memcpy(buffer->data(), data, size);
1490
1491 buffer->meta()->setInt32("csd", true);
1492 buffer->meta()->setInt64("timeUs", 0);
1493 msg->setBuffer("csd-0", buffer);
1494 parseAPVProfileLevelFromCsd(buffer, msg);
1495 } else if (meta->findData(kKeyESDS, &type, &data, &size)) {
1496 ESDS esds((const char *)data, size);
1497 if (esds.InitCheck() != (status_t)OK) {
1498 return BAD_VALUE;
1499 }
1500
1501 const void *codec_specific_data;
1502 size_t codec_specific_data_size;
1503 esds.getCodecSpecificInfo(
1504 &codec_specific_data, &codec_specific_data_size);
1505
1506 sp<ABuffer> buffer = new (std::nothrow) ABuffer(codec_specific_data_size);
1507 if (buffer.get() == NULL || buffer->base() == NULL) {
1508 return NO_MEMORY;
1509 }
1510
1511 memcpy(buffer->data(), codec_specific_data,
1512 codec_specific_data_size);
1513
1514 buffer->meta()->setInt32("csd", true);
1515 buffer->meta()->setInt64("timeUs", 0);
1516 msg->setBuffer("csd-0", buffer);
1517
1518 if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG4)) {
1519 parseMpeg4ProfileLevelFromCsd(buffer, msg);
1520 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_MPEG2)) {
1521 parseMpeg2ProfileLevelFromEsds(esds, msg);
1522 if (meta->findData(kKeyStreamHeader, &type, &data, &size)) {
1523 parseMpeg2ProfileLevelFromHeader((uint8_t*)data, size, msg);
1524 }
1525 } else if (!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_AAC)) {
1526 parseAacProfileFromCsd(buffer, msg);
1527 }
1528
1529 uint32_t maxBitrate, avgBitrate;
1530 if (esds.getBitRate(&maxBitrate, &avgBitrate) == OK) {
1531 if (!meta->hasData(kKeyBitRate)
1532 && avgBitrate > 0 && avgBitrate <= INT32_MAX) {
1533 msg->setInt32("bitrate", (int32_t)avgBitrate);
1534 } else {
1535 (void)msg->findInt32("bitrate", (int32_t*)&avgBitrate);
1536 }
1537 if (!meta->hasData(kKeyMaxBitRate)
1538 && maxBitrate > 0 && maxBitrate <= INT32_MAX && maxBitrate >= avgBitrate) {
1539 msg->setInt32("max-bitrate", (int32_t)maxBitrate);
1540 }
1541 }
1542 } else if (meta->findData(kKeyD263, &type, &data, &size)) {
1543 const uint8_t *ptr = (const uint8_t *)data;
1544 parseH263ProfileLevelFromD263(ptr, size, msg);
1545 } else if (meta->findData(kKeyOpusHeader, &type, &data, &size)) {
1546 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1547 if (buffer.get() == NULL || buffer->base() == NULL) {
1548 return NO_MEMORY;
1549 }
1550 memcpy(buffer->data(), data, size);
1551
1552 buffer->meta()->setInt32("csd", true);
1553 buffer->meta()->setInt64("timeUs", 0);
1554 msg->setBuffer("csd-0", buffer);
1555
1556 if (!meta->findData(kKeyOpusCodecDelay, &type, &data, &size)) {
1557 return -EINVAL;
1558 }
1559
1560 buffer = new (std::nothrow) ABuffer(size);
1561 if (buffer.get() == NULL || buffer->base() == NULL) {
1562 return NO_MEMORY;
1563 }
1564 memcpy(buffer->data(), data, size);
1565
1566 buffer->meta()->setInt32("csd", true);
1567 buffer->meta()->setInt64("timeUs", 0);
1568 msg->setBuffer("csd-1", buffer);
1569
1570 if (!meta->findData(kKeyOpusSeekPreRoll, &type, &data, &size)) {
1571 return -EINVAL;
1572 }
1573
1574 buffer = new (std::nothrow) ABuffer(size);
1575 if (buffer.get() == NULL || buffer->base() == NULL) {
1576 return NO_MEMORY;
1577 }
1578 memcpy(buffer->data(), data, size);
1579
1580 buffer->meta()->setInt32("csd", true);
1581 buffer->meta()->setInt64("timeUs", 0);
1582 msg->setBuffer("csd-2", buffer);
1583 } else if (meta->findData(kKeyVp9CodecPrivate, &type, &data, &size)) {
1584 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1585 if (buffer.get() == NULL || buffer->base() == NULL) {
1586 return NO_MEMORY;
1587 }
1588 memcpy(buffer->data(), data, size);
1589
1590 buffer->meta()->setInt32("csd", true);
1591 buffer->meta()->setInt64("timeUs", 0);
1592 msg->setBuffer("csd-0", buffer);
1593
1594 parseVp9ProfileLevelFromCsd(buffer, msg);
1595 } else if (meta->findData(kKeyAlacMagicCookie, &type, &data, &size)) {
1596 ALOGV("convertMetaDataToMessage found kKeyAlacMagicCookie of size %zu\n", size);
1597 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1598 if (buffer.get() == NULL || buffer->base() == NULL) {
1599 return NO_MEMORY;
1600 }
1601 memcpy(buffer->data(), data, size);
1602
1603 buffer->meta()->setInt32("csd", true);
1604 buffer->meta()->setInt64("timeUs", 0);
1605 msg->setBuffer("csd-0", buffer);
1606 }
1607
1608 if (meta->findData(kKeyDVCC, &type, &data, &size)
1609 || meta->findData(kKeyDVVC, &type, &data, &size)
1610 || meta->findData(kKeyDVWC, &type, &data, &size)) {
1611 const uint8_t *ptr = (const uint8_t *)data;
1612 ALOGV("DV: calling parseDolbyVisionProfileLevelFromDvcc with data size %zu", size);
1613 parseDolbyVisionProfileLevelFromDvcc(ptr, size, msg);
1614 sp<ABuffer> buffer = new (std::nothrow) ABuffer(size);
1615 if (buffer.get() == nullptr || buffer->base() == nullptr) {
1616 return NO_MEMORY;
1617 }
1618 memcpy(buffer->data(), data, size);
1619
1620 buffer->meta()->setInt32("csd", true);
1621 buffer->meta()->setInt64("timeUs", 0);
1622 msg->setBuffer("csd-2", buffer);
1623 }
1624
1625 *format = msg;
1626
1627 return OK;
1628 }
1629
findNextNalStartCode(const uint8_t * data,size_t length)1630 const uint8_t *findNextNalStartCode(const uint8_t *data, size_t length) {
1631 uint8_t *res = NULL;
1632 if (length > 4) {
1633 // minus 1 as to not match NAL start code at end
1634 res = (uint8_t *)memmem(data, length - 1, "\x00\x00\x00\x01", 4);
1635 }
1636 return res != NULL && res < data + length - 4 ? res : &data[length];
1637 }
1638
reassembleAVCC(const sp<ABuffer> & csd0,const sp<ABuffer> & csd1,char * avcc)1639 static size_t reassembleAVCC(const sp<ABuffer> &csd0, const sp<ABuffer> &csd1, char *avcc) {
1640 avcc[0] = 1; // version
1641 avcc[1] = 0x64; // profile (default to high)
1642 avcc[2] = 0; // constraints (default to none)
1643 avcc[3] = 0xd; // level (default to 1.3)
1644 avcc[4] = 0xff; // reserved+size
1645
1646 size_t i = 0;
1647 int numparams = 0;
1648 int lastparamoffset = 0;
1649 int avccidx = 6;
1650 do {
1651 i = findNextNalStartCode(csd0->data() + i, csd0->size() - i) - csd0->data();
1652 ALOGV("block at %zu, last was %d", i, lastparamoffset);
1653 if (lastparamoffset > 0) {
1654 const uint8_t *lastparam = csd0->data() + lastparamoffset;
1655 int size = i - lastparamoffset;
1656 if (size > 3) {
1657 if (numparams && memcmp(avcc + 1, lastparam + 1, 3)) {
1658 ALOGW("Inconsisted profile/level found in SPS: %x,%x,%x vs %x,%x,%x",
1659 avcc[1], avcc[2], avcc[3], lastparam[1], lastparam[2], lastparam[3]);
1660 } else if (!numparams) {
1661 // fill in profile, constraints and level
1662 memcpy(avcc + 1, lastparam + 1, 3);
1663 }
1664 }
1665 avcc[avccidx++] = size >> 8;
1666 avcc[avccidx++] = size & 0xff;
1667 memcpy(avcc+avccidx, lastparam, size);
1668 avccidx += size;
1669 numparams++;
1670 }
1671 i += 4;
1672 lastparamoffset = i;
1673 } while(i < csd0->size());
1674 ALOGV("csd0 contains %d params", numparams);
1675
1676 avcc[5] = 0xe0 | numparams;
1677 //and now csd-1
1678 i = 0;
1679 numparams = 0;
1680 lastparamoffset = 0;
1681 int numpicparamsoffset = avccidx;
1682 avccidx++;
1683 do {
1684 i = findNextNalStartCode(csd1->data() + i, csd1->size() - i) - csd1->data();
1685 ALOGV("block at %zu, last was %d", i, lastparamoffset);
1686 if (lastparamoffset > 0) {
1687 int size = i - lastparamoffset;
1688 avcc[avccidx++] = size >> 8;
1689 avcc[avccidx++] = size & 0xff;
1690 memcpy(avcc+avccidx, csd1->data() + lastparamoffset, size);
1691 avccidx += size;
1692 numparams++;
1693 }
1694 i += 4;
1695 lastparamoffset = i;
1696 } while(i < csd1->size());
1697 avcc[numpicparamsoffset] = numparams;
1698 return avccidx;
1699 }
1700
reassembleESDS(const sp<ABuffer> & csd0,char * esds)1701 static void reassembleESDS(const sp<ABuffer> &csd0, char *esds) {
1702 int csd0size = csd0->size();
1703 esds[0] = 3; // kTag_ESDescriptor;
1704 int esdescriptorsize = 26 + csd0size;
1705 CHECK(esdescriptorsize < 268435456); // 7 bits per byte, so max is 2^28-1
1706 esds[1] = 0x80 | (esdescriptorsize >> 21);
1707 esds[2] = 0x80 | ((esdescriptorsize >> 14) & 0x7f);
1708 esds[3] = 0x80 | ((esdescriptorsize >> 7) & 0x7f);
1709 esds[4] = (esdescriptorsize & 0x7f);
1710 esds[5] = esds[6] = 0; // es id
1711 esds[7] = 0; // flags
1712 esds[8] = 4; // kTag_DecoderConfigDescriptor
1713 int configdescriptorsize = 18 + csd0size;
1714 esds[9] = 0x80 | (configdescriptorsize >> 21);
1715 esds[10] = 0x80 | ((configdescriptorsize >> 14) & 0x7f);
1716 esds[11] = 0x80 | ((configdescriptorsize >> 7) & 0x7f);
1717 esds[12] = (configdescriptorsize & 0x7f);
1718 esds[13] = 0x40; // objectTypeIndication
1719 // bytes 14-25 are examples from a real file. they are unused/overwritten by muxers.
1720 esds[14] = 0x15; // streamType(5), upStream(0),
1721 esds[15] = 0x00; // 15-17: bufferSizeDB (6KB)
1722 esds[16] = 0x18;
1723 esds[17] = 0x00;
1724 esds[18] = 0x00; // 18-21: maxBitrate (64kbps)
1725 esds[19] = 0x00;
1726 esds[20] = 0xfa;
1727 esds[21] = 0x00;
1728 esds[22] = 0x00; // 22-25: avgBitrate (64kbps)
1729 esds[23] = 0x00;
1730 esds[24] = 0xfa;
1731 esds[25] = 0x00;
1732 esds[26] = 5; // kTag_DecoderSpecificInfo;
1733 esds[27] = 0x80 | (csd0size >> 21);
1734 esds[28] = 0x80 | ((csd0size >> 14) & 0x7f);
1735 esds[29] = 0x80 | ((csd0size >> 7) & 0x7f);
1736 esds[30] = (csd0size & 0x7f);
1737 memcpy((void*)&esds[31], csd0->data(), csd0size);
1738 // data following this is ignored, so don't bother appending it
1739 }
1740
reassembleHVCC(const sp<ABuffer> & csd0,uint8_t * hvcc,size_t hvccSize,size_t nalSizeLength)1741 static size_t reassembleHVCC(const sp<ABuffer> &csd0, uint8_t *hvcc, size_t hvccSize, size_t nalSizeLength) {
1742 HevcParameterSets paramSets;
1743 uint8_t* data = csd0->data();
1744 if (csd0->size() < 4) {
1745 ALOGE("csd0 too small");
1746 return 0;
1747 }
1748 if (memcmp(data, "\x00\x00\x00\x01", 4) != 0) {
1749 ALOGE("csd0 doesn't start with a start code");
1750 return 0;
1751 }
1752 size_t prevNalOffset = 4;
1753 status_t err = OK;
1754 for (size_t i = 1; i < csd0->size() - 4; ++i) {
1755 if (memcmp(&data[i], "\x00\x00\x00\x01", 4) != 0) {
1756 continue;
1757 }
1758 err = paramSets.addNalUnit(&data[prevNalOffset], i - prevNalOffset);
1759 if (err != OK) {
1760 return 0;
1761 }
1762 prevNalOffset = i + 4;
1763 }
1764 err = paramSets.addNalUnit(&data[prevNalOffset], csd0->size() - prevNalOffset);
1765 if (err != OK) {
1766 return 0;
1767 }
1768 size_t size = hvccSize;
1769 err = paramSets.makeHvcc(hvcc, &size, nalSizeLength);
1770 if (err != OK) {
1771 return 0;
1772 }
1773 return size;
1774 }
1775
1776 #if 0
1777 static void convertMessageToMetaDataInt32(
1778 const sp<AMessage> &msg, sp<MetaData> &meta, uint32_t key, const char *name) {
1779 int32_t value;
1780 if (msg->findInt32(name, &value)) {
1781 meta->setInt32(key, value);
1782 }
1783 }
1784 #endif
1785
convertMessageToMetaDataColorAspects(const sp<AMessage> & msg,sp<MetaData> & meta)1786 static void convertMessageToMetaDataColorAspects(const sp<AMessage> &msg, sp<MetaData> &meta) {
1787 // 0 values are unspecified
1788 int32_t range = 0, standard = 0, transfer = 0;
1789 (void)msg->findInt32("color-range", &range);
1790 (void)msg->findInt32("color-standard", &standard);
1791 (void)msg->findInt32("color-transfer", &transfer);
1792
1793 ColorAspects colorAspects;
1794 memset(&colorAspects, 0, sizeof(colorAspects));
1795 if (CodecBase::convertPlatformColorAspectsToCodecAspects(
1796 range, standard, transfer, colorAspects) != OK) {
1797 return;
1798 }
1799
1800 // save specified values to meta
1801 if (colorAspects.mRange != 0) {
1802 meta->setInt32(kKeyColorRange, colorAspects.mRange);
1803 }
1804 if (colorAspects.mPrimaries != 0) {
1805 meta->setInt32(kKeyColorPrimaries, colorAspects.mPrimaries);
1806 }
1807 if (colorAspects.mTransfer != 0) {
1808 meta->setInt32(kKeyTransferFunction, colorAspects.mTransfer);
1809 }
1810 if (colorAspects.mMatrixCoeffs != 0) {
1811 meta->setInt32(kKeyColorMatrix, colorAspects.mMatrixCoeffs);
1812 }
1813 }
1814 /* Converts key and value pairs in AMessage format to MetaData format.
1815 * Also checks for the presence of required keys.
1816 */
convertMessageToMetaData(const sp<AMessage> & msg,sp<MetaData> & meta)1817 status_t convertMessageToMetaData(const sp<AMessage> &msg, sp<MetaData> &meta) {
1818 AString mime;
1819 if (msg->findString("mime", &mime)) {
1820 meta->setCString(kKeyMIMEType, mime.c_str());
1821 } else {
1822 ALOGV("did not find mime type");
1823 return BAD_VALUE;
1824 }
1825
1826 convertMessageToMetaDataFromMappings(msg, meta);
1827
1828 int32_t systemId;
1829 if (msg->findInt32("ca-system-id", &systemId)) {
1830 meta->setInt32(kKeyCASystemID, systemId);
1831
1832 sp<ABuffer> caSessionId, caPvtData;
1833 if (msg->findBuffer("ca-session-id", &caSessionId)) {
1834 meta->setData(kKeyCASessionID, 0, caSessionId->data(), caSessionId->size());
1835 }
1836 if (msg->findBuffer("ca-private-data", &caPvtData)) {
1837 meta->setData(kKeyCAPrivateData, 0, caPvtData->data(), caPvtData->size());
1838 }
1839 }
1840
1841 int64_t durationUs;
1842 if (msg->findInt64("durationUs", &durationUs)) {
1843 meta->setInt64(kKeyDuration, durationUs);
1844 }
1845
1846 int32_t isSync;
1847 if (msg->findInt32("is-sync-frame", &isSync) && isSync != 0) {
1848 meta->setInt32(kKeyIsSyncFrame, 1);
1849 }
1850
1851 // Mode for media transcoding.
1852 int32_t isBackgroundMode;
1853 if (msg->findInt32("android._background-mode", &isBackgroundMode) && isBackgroundMode != 0) {
1854 meta->setInt32(isBackgroundMode, 1);
1855 }
1856
1857 int32_t avgBitrate = 0;
1858 int32_t maxBitrate;
1859 if (msg->findInt32("bitrate", &avgBitrate) && avgBitrate > 0) {
1860 meta->setInt32(kKeyBitRate, avgBitrate);
1861 }
1862 if (msg->findInt32("max-bitrate", &maxBitrate) && maxBitrate > 0 && maxBitrate >= avgBitrate) {
1863 meta->setInt32(kKeyMaxBitRate, maxBitrate);
1864 }
1865
1866 int32_t dvbComponentTag = 0;
1867 if (msg->findInt32("dvb-component-tag", &dvbComponentTag) && dvbComponentTag > 0) {
1868 meta->setInt32(kKeyDvbComponentTag, dvbComponentTag);
1869 }
1870
1871 int32_t dvbAudioDescription = 0;
1872 if (msg->findInt32("dvb-audio-description", &dvbAudioDescription)) {
1873 meta->setInt32(kKeyDvbAudioDescription, dvbAudioDescription);
1874 }
1875
1876 int32_t dvbTeletextMagazineNumber = 0;
1877 if (msg->findInt32("dvb-teletext-magazine-number", &dvbTeletextMagazineNumber)) {
1878 meta->setInt32(kKeyDvbTeletextMagazineNumber, dvbTeletextMagazineNumber);
1879 }
1880
1881 int32_t dvbTeletextPageNumber = 0;
1882 if (msg->findInt32("dvb-teletext-page-number", &dvbTeletextPageNumber)) {
1883 meta->setInt32(kKeyDvbTeletextPageNumber, dvbTeletextPageNumber);
1884 }
1885
1886 AString lang;
1887 if (msg->findString("language", &lang)) {
1888 meta->setCString(kKeyMediaLanguage, lang.c_str());
1889 }
1890
1891 if (mime.startsWith("video/") || mime.startsWith("image/")) {
1892 int32_t width;
1893 int32_t height;
1894 if (!msg->findInt32("width", &width) || !msg->findInt32("height", &height)) {
1895 ALOGV("did not find width and/or height");
1896 return BAD_VALUE;
1897 }
1898 if (width <= 0 || height <= 0) {
1899 ALOGE("Invalid value of width: %d and/or height: %d", width, height);
1900 return BAD_VALUE;
1901 }
1902 meta->setInt32(kKeyWidth, width);
1903 meta->setInt32(kKeyHeight, height);
1904
1905 int32_t sarWidth = -1, sarHeight = -1;
1906 bool foundWidth, foundHeight;
1907 foundWidth = msg->findInt32("sar-width", &sarWidth);
1908 foundHeight = msg->findInt32("sar-height", &sarHeight);
1909 if (foundWidth || foundHeight) {
1910 if (sarWidth <= 0 || sarHeight <= 0) {
1911 ALOGE("Invalid value of sarWidth: %d and/or sarHeight: %d", sarWidth, sarHeight);
1912 return BAD_VALUE;
1913 }
1914 meta->setInt32(kKeySARWidth, sarWidth);
1915 meta->setInt32(kKeySARHeight, sarHeight);
1916 }
1917
1918 int32_t displayWidth = -1, displayHeight = -1;
1919 foundWidth = msg->findInt32("display-width", &displayWidth);
1920 foundHeight = msg->findInt32("display-height", &displayHeight);
1921 if (foundWidth || foundHeight) {
1922 if (displayWidth <= 0 || displayHeight <= 0) {
1923 ALOGE("Invalid value of displayWidth: %d and/or displayHeight: %d",
1924 displayWidth, displayHeight);
1925 return BAD_VALUE;
1926 }
1927 meta->setInt32(kKeyDisplayWidth, displayWidth);
1928 meta->setInt32(kKeyDisplayHeight, displayHeight);
1929 }
1930
1931 if (mime.startsWith("image/")){
1932 int32_t isPrimary;
1933 if (msg->findInt32("is-default", &isPrimary) && isPrimary) {
1934 meta->setInt32(kKeyTrackIsDefault, 1);
1935 }
1936 int32_t tileWidth = -1, tileHeight = -1;
1937 foundWidth = msg->findInt32("tile-width", &tileWidth);
1938 foundHeight = msg->findInt32("tile-height", &tileHeight);
1939 if (foundWidth || foundHeight) {
1940 if (tileWidth <= 0 || tileHeight <= 0) {
1941 ALOGE("Invalid value of tileWidth: %d and/or tileHeight: %d",
1942 tileWidth, tileHeight);
1943 return BAD_VALUE;
1944 }
1945 meta->setInt32(kKeyTileWidth, tileWidth);
1946 meta->setInt32(kKeyTileHeight, tileHeight);
1947 }
1948 int32_t gridRows = -1, gridCols = -1;
1949 bool foundRows, foundCols;
1950 foundRows = msg->findInt32("grid-rows", &gridRows);
1951 foundCols = msg->findInt32("grid-cols", &gridCols);
1952 if (foundRows || foundCols) {
1953 if (gridRows <= 0 || gridCols <= 0) {
1954 ALOGE("Invalid value of gridRows: %d and/or gridCols: %d",
1955 gridRows, gridCols);
1956 return BAD_VALUE;
1957 }
1958 meta->setInt32(kKeyGridRows, gridRows);
1959 meta->setInt32(kKeyGridCols, gridCols);
1960 }
1961 }
1962
1963 int32_t colorFormat;
1964 if (msg->findInt32("color-format", &colorFormat)) {
1965 meta->setInt32(kKeyColorFormat, colorFormat);
1966 }
1967
1968 int32_t cropLeft, cropTop, cropRight, cropBottom;
1969 if (msg->findRect("crop",
1970 &cropLeft,
1971 &cropTop,
1972 &cropRight,
1973 &cropBottom)) {
1974 if (cropLeft < 0 || cropLeft > cropRight || cropRight >= width) {
1975 ALOGE("Invalid value of cropLeft: %d and/or cropRight: %d", cropLeft, cropRight);
1976 return BAD_VALUE;
1977 }
1978 if (cropTop < 0 || cropTop > cropBottom || cropBottom >= height) {
1979 ALOGE("Invalid value of cropTop: %d and/or cropBottom: %d", cropTop, cropBottom);
1980 return BAD_VALUE;
1981 }
1982 meta->setRect(kKeyCropRect, cropLeft, cropTop, cropRight, cropBottom);
1983 }
1984
1985 int32_t rotationDegrees;
1986 if (msg->findInt32("rotation-degrees", &rotationDegrees)) {
1987 meta->setInt32(kKeyRotation, rotationDegrees);
1988 }
1989
1990 if (msg->contains("hdr-static-info")) {
1991 HDRStaticInfo info;
1992 if (ColorUtils::getHDRStaticInfoFromFormat(msg, &info)) {
1993 meta->setData(kKeyHdrStaticInfo, 'hdrS', &info, sizeof(info));
1994 }
1995 }
1996
1997 sp<ABuffer> hdr10PlusInfo;
1998 if (msg->findBuffer("hdr10-plus-info", &hdr10PlusInfo)) {
1999 meta->setData(kKeyHdr10PlusInfo, 0,
2000 hdr10PlusInfo->data(), hdr10PlusInfo->size());
2001 }
2002
2003 convertMessageToMetaDataColorAspects(msg, meta);
2004
2005 AString tsSchema;
2006 if (msg->findString("ts-schema", &tsSchema)) {
2007 unsigned int numLayers = 0;
2008 unsigned int numBLayers = 0;
2009 char placeholder;
2010 int tags = sscanf(tsSchema.c_str(), "android.generic.%u%c%u%c",
2011 &numLayers, &placeholder, &numBLayers, &placeholder);
2012 if ((tags == 1 || (tags == 3 && placeholder == '+'))
2013 && numLayers > 0 && numLayers < UINT32_MAX - numBLayers
2014 && numLayers + numBLayers <= INT32_MAX) {
2015 meta->setInt32(kKeyTemporalLayerCount, numLayers + numBLayers);
2016 }
2017 }
2018 } else if (mime.startsWith("audio/")) {
2019 int32_t numChannels, sampleRate;
2020 if (!msg->findInt32("channel-count", &numChannels) ||
2021 !msg->findInt32("sample-rate", &sampleRate)) {
2022 ALOGV("did not find channel-count and/or sample-rate");
2023 return BAD_VALUE;
2024 }
2025 // channel count can be zero in some cases like mpeg h
2026 if (sampleRate <= 0 || numChannels < 0) {
2027 ALOGE("Invalid value of channel-count: %d and/or sample-rate: %d",
2028 numChannels, sampleRate);
2029 return BAD_VALUE;
2030 }
2031 meta->setInt32(kKeyChannelCount, numChannels);
2032 meta->setInt32(kKeySampleRate, sampleRate);
2033 int32_t bitsPerSample;
2034 // TODO:(b/204430952) add appropriate bound check for bitsPerSample
2035 if (msg->findInt32("bits-per-sample", &bitsPerSample)) {
2036 meta->setInt32(kKeyBitsPerSample, bitsPerSample);
2037 }
2038 int32_t channelMask;
2039 if (msg->findInt32("channel-mask", &channelMask)) {
2040 meta->setInt32(kKeyChannelMask, channelMask);
2041 }
2042 int32_t delay = 0;
2043 if (msg->findInt32("encoder-delay", &delay)) {
2044 meta->setInt32(kKeyEncoderDelay, delay);
2045 }
2046 int32_t padding = 0;
2047 if (msg->findInt32("encoder-padding", &padding)) {
2048 meta->setInt32(kKeyEncoderPadding, padding);
2049 }
2050
2051 int32_t isADTS;
2052 if (msg->findInt32("is-adts", &isADTS)) {
2053 meta->setInt32(kKeyIsADTS, isADTS);
2054 }
2055
2056 int32_t mpeghProfileLevelIndication = -1;
2057 if (msg->findInt32(AMEDIAFORMAT_KEY_MPEGH_PROFILE_LEVEL_INDICATION,
2058 &mpeghProfileLevelIndication)) {
2059 meta->setInt32(kKeyMpeghProfileLevelIndication, mpeghProfileLevelIndication);
2060 }
2061 int32_t mpeghReferenceChannelLayout = -1;
2062 if (msg->findInt32(AMEDIAFORMAT_KEY_MPEGH_REFERENCE_CHANNEL_LAYOUT,
2063 &mpeghReferenceChannelLayout)) {
2064 meta->setInt32(kKeyMpeghReferenceChannelLayout, mpeghReferenceChannelLayout);
2065 }
2066 sp<ABuffer> mpeghCompatibleSets;
2067 if (msg->findBuffer(AMEDIAFORMAT_KEY_MPEGH_COMPATIBLE_SETS,
2068 &mpeghCompatibleSets)) {
2069 meta->setData(kKeyMpeghCompatibleSets, kTypeHCOS,
2070 mpeghCompatibleSets->data(), mpeghCompatibleSets->size());
2071 }
2072
2073 int32_t aacProfile = -1;
2074 if (msg->findInt32("aac-profile", &aacProfile)) {
2075 meta->setInt32(kKeyAACAOT, aacProfile);
2076 }
2077
2078 int32_t pcmEncoding;
2079 if (msg->findInt32("pcm-encoding", &pcmEncoding)) {
2080 meta->setInt32(kKeyPcmEncoding, pcmEncoding);
2081 }
2082
2083 int32_t hapticChannelCount;
2084 if (msg->findInt32("haptic-channel-count", &hapticChannelCount)) {
2085 meta->setInt32(kKeyHapticChannelCount, hapticChannelCount);
2086 }
2087 }
2088
2089 int32_t maxInputSize;
2090 if (msg->findInt32("max-input-size", &maxInputSize)) {
2091 meta->setInt32(kKeyMaxInputSize, maxInputSize);
2092 }
2093
2094 int32_t maxWidth;
2095 if (msg->findInt32("max-width", &maxWidth)) {
2096 meta->setInt32(kKeyMaxWidth, maxWidth);
2097 }
2098
2099 int32_t maxHeight;
2100 if (msg->findInt32("max-height", &maxHeight)) {
2101 meta->setInt32(kKeyMaxHeight, maxHeight);
2102 }
2103
2104 int32_t gainmap;
2105 if (msg->findInt32("gainmap", &gainmap)) {
2106 meta->setInt32(kKeyGainmap, gainmap);
2107 }
2108
2109 int32_t fps;
2110 float fpsFloat;
2111 if (msg->findInt32("frame-rate", &fps) && fps > 0) {
2112 meta->setInt32(kKeyFrameRate, fps);
2113 } else if (msg->findFloat("frame-rate", &fpsFloat)
2114 && fpsFloat >= 1 && fpsFloat <= (float)INT32_MAX) {
2115 // truncate values to distinguish between e.g. 24 vs 23.976 fps
2116 meta->setInt32(kKeyFrameRate, (int32_t)fpsFloat);
2117 }
2118
2119 // reassemble the csd data into its original form
2120 sp<ABuffer> csd0, csd1, csd2;
2121 if (msg->findBuffer("csd-0", &csd0)) {
2122 int csd0size = csd0->size();
2123 if (mime == MEDIA_MIMETYPE_VIDEO_AVC) {
2124 sp<ABuffer> csd1;
2125 if (msg->findBuffer("csd-1", &csd1)) {
2126 std::vector<char> avcc(csd0size + csd1->size() + 1024);
2127 size_t outsize = reassembleAVCC(csd0, csd1, avcc.data());
2128 meta->setData(kKeyAVCC, kTypeAVCC, avcc.data(), outsize);
2129 }
2130 } else if (mime == MEDIA_MIMETYPE_AUDIO_AAC ||
2131 mime == MEDIA_MIMETYPE_VIDEO_MPEG4 ||
2132 mime == MEDIA_MIMETYPE_AUDIO_WMA ||
2133 mime == MEDIA_MIMETYPE_AUDIO_MS_ADPCM ||
2134 mime == MEDIA_MIMETYPE_AUDIO_DVI_IMA_ADPCM) {
2135 std::vector<char> esds(csd0size + 31);
2136 // The written ESDS is actually for an audio stream, but it's enough
2137 // for transporting the CSD to muxers.
2138 reassembleESDS(csd0, esds.data());
2139 meta->setData(kKeyESDS, kTypeESDS, esds.data(), esds.size());
2140 } else if (mime == MEDIA_MIMETYPE_VIDEO_HEVC ||
2141 mime == MEDIA_MIMETYPE_IMAGE_ANDROID_HEIC) {
2142 std::vector<uint8_t> hvcc(csd0size + 1024);
2143 size_t outsize = reassembleHVCC(csd0, hvcc.data(), hvcc.size(), 4);
2144 meta->setData(kKeyHVCC, kTypeHVCC, hvcc.data(), outsize);
2145 } else if (mime == MEDIA_MIMETYPE_VIDEO_AV1 ||
2146 mime == MEDIA_MIMETYPE_IMAGE_AVIF) {
2147 meta->setData(kKeyAV1C, 0, csd0->data(), csd0->size());
2148 } else if (com::android::media::extractor::flags::extractor_mp4_enable_apv() &&
2149 mime == MEDIA_MIMETYPE_VIDEO_APV) {
2150 meta->setData(kKeyAPVC, 0, csd0->data(), csd0->size());
2151 } else if (mime == MEDIA_MIMETYPE_VIDEO_DOLBY_VISION) {
2152 int32_t profile = -1;
2153 uint8_t blCompatibilityId = -1;
2154 int32_t level = 0;
2155 uint8_t profileVal = -1;
2156 uint8_t profileVal1 = -1;
2157 uint8_t profileVal2 = -1;
2158 constexpr size_t dvccSize = 24;
2159
2160 const ALookup<uint8_t, int32_t> &profiles =
2161 getDolbyVisionProfileTable();
2162 const ALookup<uint8_t, int32_t> &levels =
2163 getDolbyVisionLevelsTable();
2164
2165 if (!msg->findBuffer("csd-2", &csd2)) {
2166 // MP4 extractors are expected to generate csd buffer
2167 // some encoders might not be generating it, in which
2168 // case we populate the track metadata dv (cc|vc|wc)
2169 // from the 'profile' and 'level' info.
2170 // This is done according to Dolby Vision ISOBMFF spec
2171
2172 if (!msg->findInt32("profile", &profile)) {
2173 ALOGE("Dolby Vision profile not found");
2174 return BAD_VALUE;
2175 }
2176 msg->findInt32("level", &level);
2177
2178 if (profile == DolbyVisionProfileDvheSt) {
2179 if (!profiles.rlookup(DolbyVisionProfileDvheSt, &profileVal)) { // dvhe.08
2180 ALOGE("Dolby Vision profile lookup error");
2181 return BAD_VALUE;
2182 }
2183 blCompatibilityId = 4;
2184 } else if (profile == DolbyVisionProfileDvavSe) {
2185 if (!profiles.rlookup(DolbyVisionProfileDvavSe, &profileVal)) { // dvav.09
2186 ALOGE("Dolby Vision profile lookup error");
2187 return BAD_VALUE;
2188 }
2189 blCompatibilityId = 2;
2190 } else {
2191 ALOGE("Dolby Vision profile look up error");
2192 return BAD_VALUE;
2193 }
2194
2195 profile = (int32_t) profileVal;
2196
2197 uint8_t level_val = 0;
2198 if (!levels.map(level, &level_val)) {
2199 ALOGE("Dolby Vision level lookup error");
2200 return BAD_VALUE;
2201 }
2202
2203 std::vector<uint8_t> dvcc(dvccSize);
2204
2205 dvcc[0] = 1; // major version
2206 dvcc[1] = 0; // minor version
2207 dvcc[2] = (uint8_t)((profile & 0x7f) << 1); // dolby vision profile
2208 dvcc[2] = (uint8_t)((dvcc[2] | (uint8_t)((level_val >> 5) & 0x1)) & 0xff);
2209 dvcc[3] = (uint8_t)((level_val & 0x1f) << 3); // dolby vision level
2210 dvcc[3] = (uint8_t)(dvcc[3] | (1 << 2)); // rpu_present_flag
2211 dvcc[3] = (uint8_t)(dvcc[3] | (1)); // bl_present_flag
2212 dvcc[4] = (uint8_t)(blCompatibilityId << 4); // bl_compatibility id
2213
2214 profiles.rlookup(DolbyVisionProfileDvav110, &profileVal);
2215 profiles.rlookup(DolbyVisionProfileDvheDtb, &profileVal1);
2216 if (profile > (int32_t) profileVal) {
2217 meta->setData(kKeyDVWC, kTypeDVWC, dvcc.data(), dvccSize);
2218 } else if (profile > (int32_t) profileVal1) {
2219 meta->setData(kKeyDVVC, kTypeDVVC, dvcc.data(), dvccSize);
2220 } else {
2221 meta->setData(kKeyDVCC, kTypeDVCC, dvcc.data(), dvccSize);
2222 }
2223
2224 } else {
2225 // we have csd-2, just use that to populate dvcc
2226 if (csd2->size() == dvccSize) {
2227 uint8_t *dvcc = csd2->data();
2228 profile = dvcc[2] >> 1;
2229
2230 profiles.rlookup(DolbyVisionProfileDvav110, &profileVal);
2231 profiles.rlookup(DolbyVisionProfileDvheDtb, &profileVal1);
2232 if (profile > (int32_t) profileVal) {
2233 meta->setData(kKeyDVWC, kTypeDVWC, csd2->data(), csd2->size());
2234 } else if (profile > (int32_t) profileVal1) {
2235 meta->setData(kKeyDVVC, kTypeDVVC, csd2->data(), csd2->size());
2236 } else {
2237 meta->setData(kKeyDVCC, kTypeDVCC, csd2->data(), csd2->size());
2238 }
2239
2240 } else {
2241 ALOGE("Convert MessageToMetadata csd-2 is present but not valid");
2242 return BAD_VALUE;
2243 }
2244 }
2245 profiles.rlookup(DolbyVisionProfileDvavPen, &profileVal);
2246 profiles.rlookup(DolbyVisionProfileDvavSe, &profileVal1);
2247 profiles.rlookup(DolbyVisionProfileDvav110, &profileVal2);
2248 if ((profile > (int32_t) profileVal) && (profile < (int32_t) profileVal1)) {
2249 std::vector<uint8_t> hvcc(csd0size + 1024);
2250 size_t outsize = reassembleHVCC(csd0, hvcc.data(), hvcc.size(), 4);
2251 meta->setData(kKeyHVCC, kTypeHVCC, hvcc.data(), outsize);
2252 } else if (profile == (int32_t) profileVal2) {
2253 meta->setData(kKeyAV1C, 0, csd0->data(), csd0->size());
2254 } else {
2255 sp<ABuffer> csd1;
2256 if (msg->findBuffer("csd-1", &csd1)) {
2257 std::vector<char> avcc(csd0size + csd1->size() + 1024);
2258 size_t outsize = reassembleAVCC(csd0, csd1, avcc.data());
2259 meta->setData(kKeyAVCC, kTypeAVCC, avcc.data(), outsize);
2260 }
2261 else {
2262 // for dolby vision avc, csd0 also holds csd1
2263 size_t i = 0;
2264 int csd0realsize = 0;
2265 do {
2266 i = findNextNalStartCode(csd0->data() + i,
2267 csd0->size() - i) - csd0->data();
2268 if (i > 0) {
2269 csd0realsize = i;
2270 break;
2271 }
2272 i += 4;
2273 } while(i < csd0->size());
2274 // buffer0 -> csd0
2275 sp<ABuffer> buffer0 = new (std::nothrow) ABuffer(csd0realsize);
2276 if (buffer0.get() == NULL || buffer0->base() == NULL) {
2277 return NO_MEMORY;
2278 }
2279 memcpy(buffer0->data(), csd0->data(), csd0realsize);
2280 // buffer1 -> csd1
2281 sp<ABuffer> buffer1 = new (std::nothrow)
2282 ABuffer(csd0->size() - csd0realsize);
2283 if (buffer1.get() == NULL || buffer1->base() == NULL) {
2284 return NO_MEMORY;
2285 }
2286 memcpy(buffer1->data(), csd0->data()+csd0realsize,
2287 csd0->size() - csd0realsize);
2288
2289 std::vector<char> avcc(csd0->size() + 1024);
2290 size_t outsize = reassembleAVCC(buffer0, buffer1, avcc.data());
2291 meta->setData(kKeyAVCC, kTypeAVCC, avcc.data(), outsize);
2292 }
2293 }
2294 } else if (mime == MEDIA_MIMETYPE_VIDEO_VP9) {
2295 meta->setData(kKeyVp9CodecPrivate, 0, csd0->data(), csd0->size());
2296 } else if (mime == MEDIA_MIMETYPE_AUDIO_OPUS) {
2297 size_t opusHeadSize = csd0->size();
2298 size_t codecDelayBufSize = 0;
2299 size_t seekPreRollBufSize = 0;
2300 void *opusHeadBuf = csd0->data();
2301 void *codecDelayBuf = NULL;
2302 void *seekPreRollBuf = NULL;
2303 if (msg->findBuffer("csd-1", &csd1)) {
2304 codecDelayBufSize = csd1->size();
2305 codecDelayBuf = csd1->data();
2306 }
2307 if (msg->findBuffer("csd-2", &csd2)) {
2308 seekPreRollBufSize = csd2->size();
2309 seekPreRollBuf = csd2->data();
2310 }
2311 /* Extract codec delay and seek pre roll from csd-0,
2312 * if csd-1 and csd-2 are not present */
2313 if (!codecDelayBuf && !seekPreRollBuf) {
2314 GetOpusHeaderBuffers(csd0->data(), csd0->size(), &opusHeadBuf,
2315 &opusHeadSize, &codecDelayBuf,
2316 &codecDelayBufSize, &seekPreRollBuf,
2317 &seekPreRollBufSize);
2318 }
2319 meta->setData(kKeyOpusHeader, 0, opusHeadBuf, opusHeadSize);
2320 if (codecDelayBuf) {
2321 meta->setData(kKeyOpusCodecDelay, 0, codecDelayBuf, codecDelayBufSize);
2322 }
2323 if (seekPreRollBuf) {
2324 meta->setData(kKeyOpusSeekPreRoll, 0, seekPreRollBuf, seekPreRollBufSize);
2325 }
2326 } else if (mime == MEDIA_MIMETYPE_AUDIO_ALAC) {
2327 meta->setData(kKeyAlacMagicCookie, 0, csd0->data(), csd0->size());
2328 }
2329 } else if (mime == MEDIA_MIMETYPE_VIDEO_AVC && msg->findBuffer("csd-avc", &csd0)) {
2330 meta->setData(kKeyAVCC, kTypeAVCC, csd0->data(), csd0->size());
2331 } else if ((mime == MEDIA_MIMETYPE_VIDEO_HEVC || mime == MEDIA_MIMETYPE_IMAGE_ANDROID_HEIC)
2332 && msg->findBuffer("csd-hevc", &csd0)) {
2333 meta->setData(kKeyHVCC, kTypeHVCC, csd0->data(), csd0->size());
2334 } else if (msg->findBuffer("esds", &csd0)) {
2335 meta->setData(kKeyESDS, kTypeESDS, csd0->data(), csd0->size());
2336 } else if (msg->findBuffer("mpeg2-stream-header", &csd0)) {
2337 meta->setData(kKeyStreamHeader, 'mdat', csd0->data(), csd0->size());
2338 } else if (msg->findBuffer("d263", &csd0)) {
2339 meta->setData(kKeyD263, kTypeD263, csd0->data(), csd0->size());
2340 } else if (mime == MEDIA_MIMETYPE_VIDEO_DOLBY_VISION && msg->findBuffer("csd-2", &csd2)) {
2341 meta->setData(kKeyDVCC, kTypeDVCC, csd2->data(), csd2->size());
2342
2343 // Remove CSD-2 from the data here to avoid duplicate data in meta
2344 meta->remove(kKeyOpaqueCSD2);
2345
2346 if (msg->findBuffer("csd-avc", &csd0)) {
2347 meta->setData(kKeyAVCC, kTypeAVCC, csd0->data(), csd0->size());
2348 } else if (msg->findBuffer("csd-hevc", &csd0)) {
2349 meta->setData(kKeyHVCC, kTypeHVCC, csd0->data(), csd0->size());
2350 }
2351 }
2352 // XXX TODO add whatever other keys there are
2353
2354 #if 0
2355 ALOGI("converted %s to:", msg->debugString(0).c_str());
2356 meta->dumpToLog();
2357 #endif
2358 return OK;
2359 }
2360
sendMetaDataToHal(sp<MediaPlayerBase::AudioSink> & sink,const sp<MetaData> & meta)2361 status_t sendMetaDataToHal(sp<MediaPlayerBase::AudioSink>& sink,
2362 const sp<MetaData>& meta)
2363 {
2364 int32_t sampleRate = 0;
2365 int32_t bitRate = 0;
2366 int32_t channelMask = 0;
2367 int32_t delaySamples = 0;
2368 int32_t paddingSamples = 0;
2369
2370 AudioParameter param = AudioParameter();
2371
2372 if (meta->findInt32(kKeySampleRate, &sampleRate)) {
2373 param.addInt(String8(AUDIO_OFFLOAD_CODEC_SAMPLE_RATE), sampleRate);
2374 }
2375 if (meta->findInt32(kKeyChannelMask, &channelMask)) {
2376 param.addInt(String8(AUDIO_OFFLOAD_CODEC_NUM_CHANNEL), channelMask);
2377 }
2378 if (meta->findInt32(kKeyBitRate, &bitRate)) {
2379 param.addInt(String8(AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE), bitRate);
2380 }
2381 if (meta->findInt32(kKeyEncoderDelay, &delaySamples)) {
2382 param.addInt(String8(AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES), delaySamples);
2383 }
2384 if (meta->findInt32(kKeyEncoderPadding, &paddingSamples)) {
2385 param.addInt(String8(AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES), paddingSamples);
2386 }
2387
2388 ALOGV("sendMetaDataToHal: bitRate %d, sampleRate %d, chanMask %d,"
2389 "delaySample %d, paddingSample %d", bitRate, sampleRate,
2390 channelMask, delaySamples, paddingSamples);
2391
2392 sink->setParameters(param.toString());
2393 return OK;
2394 }
2395
2396 struct mime_conv_t {
2397 const char* mime;
2398 audio_format_t format;
2399 };
2400
2401 static const struct mime_conv_t mimeLookup[] = {
2402 { MEDIA_MIMETYPE_AUDIO_MPEG, AUDIO_FORMAT_MP3 },
2403 { MEDIA_MIMETYPE_AUDIO_RAW, AUDIO_FORMAT_PCM_16_BIT },
2404 { MEDIA_MIMETYPE_AUDIO_AMR_NB, AUDIO_FORMAT_AMR_NB },
2405 { MEDIA_MIMETYPE_AUDIO_AMR_WB, AUDIO_FORMAT_AMR_WB },
2406 { MEDIA_MIMETYPE_AUDIO_AAC, AUDIO_FORMAT_AAC },
2407 { MEDIA_MIMETYPE_AUDIO_VORBIS, AUDIO_FORMAT_VORBIS },
2408 { MEDIA_MIMETYPE_AUDIO_OPUS, AUDIO_FORMAT_OPUS},
2409 { MEDIA_MIMETYPE_AUDIO_AC3, AUDIO_FORMAT_AC3},
2410 { MEDIA_MIMETYPE_AUDIO_EAC3, AUDIO_FORMAT_E_AC3},
2411 { MEDIA_MIMETYPE_AUDIO_EAC3_JOC, AUDIO_FORMAT_E_AC3_JOC},
2412 { MEDIA_MIMETYPE_AUDIO_AC4, AUDIO_FORMAT_AC4},
2413 { MEDIA_MIMETYPE_AUDIO_FLAC, AUDIO_FORMAT_FLAC},
2414 { MEDIA_MIMETYPE_AUDIO_ALAC, AUDIO_FORMAT_ALAC },
2415 { 0, AUDIO_FORMAT_INVALID }
2416 };
2417
mapMimeToAudioFormat(audio_format_t & format,const char * mime)2418 status_t mapMimeToAudioFormat( audio_format_t& format, const char* mime )
2419 {
2420 const struct mime_conv_t* p = &mimeLookup[0];
2421 while (p->mime != NULL) {
2422 if (0 == strcasecmp(mime, p->mime)) {
2423 format = p->format;
2424 return OK;
2425 }
2426 ++p;
2427 }
2428
2429 return BAD_VALUE;
2430 }
2431
2432 struct aac_format_conv_t {
2433 int32_t eAacProfileType;
2434 audio_format_t format;
2435 };
2436
2437 static const struct aac_format_conv_t profileLookup[] = {
2438 { AACObjectMain, AUDIO_FORMAT_AAC_MAIN},
2439 { AACObjectLC, AUDIO_FORMAT_AAC_LC},
2440 { AACObjectSSR, AUDIO_FORMAT_AAC_SSR},
2441 { AACObjectLTP, AUDIO_FORMAT_AAC_LTP},
2442 { AACObjectHE, AUDIO_FORMAT_AAC_HE_V1},
2443 { AACObjectScalable, AUDIO_FORMAT_AAC_SCALABLE},
2444 { AACObjectERLC, AUDIO_FORMAT_AAC_ERLC},
2445 { AACObjectLD, AUDIO_FORMAT_AAC_LD},
2446 { AACObjectHE_PS, AUDIO_FORMAT_AAC_HE_V2},
2447 { AACObjectELD, AUDIO_FORMAT_AAC_ELD},
2448 { AACObjectXHE, AUDIO_FORMAT_AAC_XHE},
2449 { AACObjectNull, AUDIO_FORMAT_AAC},
2450 };
2451
mapAACProfileToAudioFormat(audio_format_t & format,uint64_t eAacProfile)2452 void mapAACProfileToAudioFormat( audio_format_t& format, uint64_t eAacProfile)
2453 {
2454 const struct aac_format_conv_t* p = &profileLookup[0];
2455 while (p->eAacProfileType != AACObjectNull) {
2456 if (eAacProfile == p->eAacProfileType) {
2457 format = p->format;
2458 return;
2459 }
2460 ++p;
2461 }
2462 format = AUDIO_FORMAT_AAC;
2463 return;
2464 }
2465
audioFormatFromEncoding(int32_t pcmEncoding)2466 audio_format_t audioFormatFromEncoding(int32_t pcmEncoding) {
2467 switch (pcmEncoding) {
2468 case kAudioEncodingPcmFloat:
2469 return AUDIO_FORMAT_PCM_FLOAT;
2470 case kAudioEncodingPcm32bit:
2471 return AUDIO_FORMAT_PCM_32_BIT;
2472 case kAudioEncodingPcm24bitPacked:
2473 return AUDIO_FORMAT_PCM_24_BIT_PACKED;
2474 case kAudioEncodingPcm16bit:
2475 return AUDIO_FORMAT_PCM_16_BIT;
2476 case kAudioEncodingPcm8bit:
2477 return AUDIO_FORMAT_PCM_8_BIT; // TODO: do we want to support this?
2478 default:
2479 ALOGE("%s: Invalid encoding: %d", __func__, pcmEncoding);
2480 return AUDIO_FORMAT_INVALID;
2481 }
2482 }
2483
getAudioOffloadInfo(const sp<MetaData> & meta,bool hasVideo,bool isStreaming,audio_stream_type_t streamType,audio_offload_info_t * info)2484 status_t getAudioOffloadInfo(const sp<MetaData>& meta, bool hasVideo,
2485 bool isStreaming, audio_stream_type_t streamType, audio_offload_info_t *info)
2486 {
2487 const char *mime;
2488 if (meta == NULL) {
2489 return BAD_VALUE;
2490 }
2491 CHECK(meta->findCString(kKeyMIMEType, &mime));
2492
2493 (*info) = AUDIO_INFO_INITIALIZER;
2494
2495 info->format = AUDIO_FORMAT_INVALID;
2496 if (mapMimeToAudioFormat(info->format, mime) != OK) {
2497 ALOGE(" Couldn't map mime type \"%s\" to a valid AudioSystem::audio_format !", mime);
2498 return BAD_VALUE;
2499 } else {
2500 ALOGV("Mime type \"%s\" mapped to audio_format %d", mime, info->format);
2501 }
2502
2503 int32_t pcmEncoding;
2504 if (meta->findInt32(kKeyPcmEncoding, &pcmEncoding)) {
2505 info->format = audioFormatFromEncoding(pcmEncoding);
2506 ALOGV("audio_format use kKeyPcmEncoding value %d first", info->format);
2507 }
2508
2509 if (AUDIO_FORMAT_INVALID == info->format) {
2510 // can't offload if we don't know what the source format is
2511 ALOGE("mime type \"%s\" not a known audio format", mime);
2512 return BAD_VALUE;
2513 }
2514
2515 // Redefine aac format according to its profile
2516 // Offloading depends on audio DSP capabilities.
2517 int32_t aacaot = -1;
2518 if (meta->findInt32(kKeyAACAOT, &aacaot)) {
2519 mapAACProfileToAudioFormat(info->format, aacaot);
2520 }
2521
2522 int32_t srate = -1;
2523 if (!meta->findInt32(kKeySampleRate, &srate)) {
2524 ALOGV("track of type '%s' does not publish sample rate", mime);
2525 }
2526 info->sample_rate = srate;
2527
2528 int32_t rawChannelMask;
2529 audio_channel_mask_t cmask = meta->findInt32(kKeyChannelMask, &rawChannelMask) ?
2530 static_cast<audio_channel_mask_t>(rawChannelMask) : CHANNEL_MASK_USE_CHANNEL_ORDER;
2531 if (cmask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
2532 ALOGV("track of type '%s' does not publish channel mask", mime);
2533
2534 // Try a channel count instead
2535 int32_t channelCount;
2536 if (!meta->findInt32(kKeyChannelCount, &channelCount)) {
2537 ALOGV("track of type '%s' does not publish channel count", mime);
2538 } else {
2539 cmask = audio_channel_out_mask_from_count(channelCount);
2540 }
2541 }
2542 info->channel_mask = cmask;
2543
2544 int64_t duration = 0;
2545 if (!meta->findInt64(kKeyDuration, &duration)) {
2546 ALOGV("track of type '%s' does not publish duration", mime);
2547 }
2548 info->duration_us = duration;
2549
2550 int32_t brate = 0;
2551 if (!meta->findInt32(kKeyBitRate, &brate)) {
2552 ALOGV("track of type '%s' does not publish bitrate", mime);
2553 }
2554 info->bit_rate = brate;
2555
2556
2557 info->stream_type = streamType;
2558 info->has_video = hasVideo;
2559 info->is_streaming = isStreaming;
2560 return OK;
2561 }
2562
canOffloadStream(const sp<MetaData> & meta,bool hasVideo,bool isStreaming,audio_stream_type_t streamType)2563 bool canOffloadStream(const sp<MetaData>& meta, bool hasVideo,
2564 bool isStreaming, audio_stream_type_t streamType)
2565 {
2566 audio_offload_info_t info = AUDIO_INFO_INITIALIZER;
2567 const char *mime;
2568 if (meta != nullptr && meta->findCString(kKeyMIMEType, &mime)
2569 && strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_OPUS) == 0) {
2570 return false;
2571 }
2572 if (OK != getAudioOffloadInfo(meta, hasVideo, isStreaming, streamType, &info)) {
2573 return false;
2574 }
2575 // Check if offload is possible for given format, stream type, sample rate,
2576 // bit rate, duration, video and streaming
2577 #ifdef DISABLE_AUDIO_SYSTEM_OFFLOAD
2578 return false;
2579 #else
2580 return AudioSystem::getOffloadSupport(info) != AUDIO_OFFLOAD_NOT_SUPPORTED;
2581 #endif
2582 }
2583
HLSTime(const sp<AMessage> & meta)2584 HLSTime::HLSTime(const sp<AMessage>& meta) :
2585 mSeq(-1),
2586 mTimeUs(-1LL),
2587 mMeta(meta) {
2588 if (meta != NULL) {
2589 CHECK(meta->findInt32("discontinuitySeq", &mSeq));
2590 CHECK(meta->findInt64("timeUs", &mTimeUs));
2591 }
2592 }
2593
getSegmentTimeUs() const2594 int64_t HLSTime::getSegmentTimeUs() const {
2595 int64_t segmentStartTimeUs = -1LL;
2596 if (mMeta != NULL) {
2597 CHECK(mMeta->findInt64("segmentStartTimeUs", &segmentStartTimeUs));
2598
2599 int64_t segmentFirstTimeUs;
2600 if (mMeta->findInt64("segmentFirstTimeUs", &segmentFirstTimeUs)) {
2601 segmentStartTimeUs += mTimeUs - segmentFirstTimeUs;
2602 }
2603
2604 // adjust segment time by playlist age (for live streaming)
2605 int64_t playlistTimeUs;
2606 if (mMeta->findInt64("playlistTimeUs", &playlistTimeUs)) {
2607 int64_t playlistAgeUs = ALooper::GetNowUs() - playlistTimeUs;
2608
2609 int64_t durationUs;
2610 CHECK(mMeta->findInt64("segmentDurationUs", &durationUs));
2611
2612 // round to nearest whole segment
2613 playlistAgeUs = (playlistAgeUs + durationUs / 2)
2614 / durationUs * durationUs;
2615
2616 segmentStartTimeUs -= playlistAgeUs;
2617 if (segmentStartTimeUs < 0) {
2618 segmentStartTimeUs = 0;
2619 }
2620 }
2621 }
2622 return segmentStartTimeUs;
2623 }
2624
operator <(const HLSTime & t0,const HLSTime & t1)2625 bool operator <(const HLSTime &t0, const HLSTime &t1) {
2626 // we can only compare discontinuity sequence and timestamp.
2627 // (mSegmentTimeUs is not reliable in live streaming case, it's the
2628 // time starting from beginning of playlist but playlist could change.)
2629 return t0.mSeq < t1.mSeq
2630 || (t0.mSeq == t1.mSeq && t0.mTimeUs < t1.mTimeUs);
2631 }
2632
writeToAMessage(const sp<AMessage> & msg,const AudioPlaybackRate & rate)2633 void writeToAMessage(const sp<AMessage> &msg, const AudioPlaybackRate &rate) {
2634 msg->setFloat("speed", rate.mSpeed);
2635 msg->setFloat("pitch", rate.mPitch);
2636 msg->setInt32("audio-fallback-mode", rate.mFallbackMode);
2637 msg->setInt32("audio-stretch-mode", rate.mStretchMode);
2638 }
2639
readFromAMessage(const sp<AMessage> & msg,AudioPlaybackRate * rate)2640 void readFromAMessage(const sp<AMessage> &msg, AudioPlaybackRate *rate /* nonnull */) {
2641 *rate = AUDIO_PLAYBACK_RATE_DEFAULT;
2642 CHECK(msg->findFloat("speed", &rate->mSpeed));
2643 CHECK(msg->findFloat("pitch", &rate->mPitch));
2644 CHECK(msg->findInt32("audio-fallback-mode", (int32_t *)&rate->mFallbackMode));
2645 CHECK(msg->findInt32("audio-stretch-mode", (int32_t *)&rate->mStretchMode));
2646 }
2647
writeToAMessage(const sp<AMessage> & msg,const AVSyncSettings & sync,float videoFpsHint)2648 void writeToAMessage(const sp<AMessage> &msg, const AVSyncSettings &sync, float videoFpsHint) {
2649 msg->setInt32("sync-source", sync.mSource);
2650 msg->setInt32("audio-adjust-mode", sync.mAudioAdjustMode);
2651 msg->setFloat("tolerance", sync.mTolerance);
2652 msg->setFloat("video-fps", videoFpsHint);
2653 }
2654
readFromAMessage(const sp<AMessage> & msg,AVSyncSettings * sync,float * videoFps)2655 void readFromAMessage(
2656 const sp<AMessage> &msg,
2657 AVSyncSettings *sync /* nonnull */,
2658 float *videoFps /* nonnull */) {
2659 AVSyncSettings settings;
2660 CHECK(msg->findInt32("sync-source", (int32_t *)&settings.mSource));
2661 CHECK(msg->findInt32("audio-adjust-mode", (int32_t *)&settings.mAudioAdjustMode));
2662 CHECK(msg->findFloat("tolerance", &settings.mTolerance));
2663 CHECK(msg->findFloat("video-fps", videoFps));
2664 *sync = settings;
2665 }
2666
writeToAMessage(const sp<AMessage> & msg,const BufferingSettings & buffering)2667 void writeToAMessage(const sp<AMessage> &msg, const BufferingSettings &buffering) {
2668 msg->setInt32("init-ms", buffering.mInitialMarkMs);
2669 msg->setInt32("resume-playback-ms", buffering.mResumePlaybackMarkMs);
2670 }
2671
readFromAMessage(const sp<AMessage> & msg,BufferingSettings * buffering)2672 void readFromAMessage(const sp<AMessage> &msg, BufferingSettings *buffering /* nonnull */) {
2673 int32_t value;
2674 if (msg->findInt32("init-ms", &value)) {
2675 buffering->mInitialMarkMs = value;
2676 }
2677 if (msg->findInt32("resume-playback-ms", &value)) {
2678 buffering->mResumePlaybackMarkMs = value;
2679 }
2680 }
2681
2682 } // namespace android
2683