Skip to content

Commit

Permalink
Update mime_utils to group similar codec independently of their codec…
Browse files Browse the repository at this point in the history
… string.

Also fix issue in getCodecParts_.
  • Loading branch information
ghouet committed May 11, 2022
1 parent 786105a commit ac6992c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 13 deletions.
52 changes: 39 additions & 13 deletions lib/util/mime_utils.js
Expand Up @@ -97,26 +97,52 @@ shaka.util.MimeUtils = class {
}

/**
* Get the codec from a codec string, ignoring differences in base sample
* types declared in the fourCC.
* Get the normalized codec from a codec string,
* ignoring multiple names of a same codec.
*
* @param {string} codecString
* @return {string}
*/
static getNormalizedCodec(codecString) {
const cb = shaka.util.MimeUtils.getCodecBase(codecString);
switch (cb) {
case 'avc1':
case 'avc3':
return 'avc'; // H264
case 'hvc1':
case 'hev1':
const parts =
shaka.util.MimeUtils.getCodecParts_(codecString.toLowerCase());
const base = parts[0];
const profile = parts[1];
switch (true) {
case base === 'mp4a' && profile === '66':
case base === 'mp4a' && profile === '67':
case base === 'mp4a' && profile === '68':
return 'mpeg2_aac';
case base === 'mp4a' && profile === '69':
case base === 'mp4a' && profile === '6b':
return 'mp3';
case base === 'mp4a' && profile === '40.2':
case base === 'mp4a' && profile === '40.02':
case base === 'mp4a' && profile === '40.5':
case base === 'mp4a' && profile === '40.05':
case base === 'mp4a' && profile === '40.29':
return 'mpeg4_aac';
case base === 'mp4a' && profile === '40.42':
return 'mpeg4_xhe_aac'; // Extended HE-AAC
case base === 'mp4a' && profile === 'a5':
return 'ac-3'; // Dolby Digital
case base === 'mp4a' && profile === 'a6':
return 'ec-3'; // Dolby Digital Plus
case base === 'mp4a' && profile === 'b2':
return 'dtsx'; // DTS:X
case base === 'mp4a' && profile === 'a9':
return 'dtsc'; // DTS Digital Surround
case base === 'avc1':
case base === 'avc3':
return 'avc'; // H264
case base === 'hvc1':
case base === 'hev1':
return 'hevc'; // H265
case 'dvh1':
case 'dvhe':
case base === 'dvh1':
case base === 'dvhe':
return 'dovi'; // Dolby Vision
}
return cb;
return base;
}

/**
Expand Down Expand Up @@ -175,7 +201,7 @@ shaka.util.MimeUtils = class {

const base = parts[0];

parts.pop();
parts.shift();
const profile = parts.join('.');

// Make sure that we always return a "base" and "profile".
Expand Down
55 changes: 55 additions & 0 deletions test/util/mime_utils_unit.js
@@ -0,0 +1,55 @@
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

describe('MimeUtils', () => {
const getNormalizedCodec = (codecs) =>
shaka.util.MimeUtils.getNormalizedCodec(codecs);

it('normalizes codecs', () => {
expect(getNormalizedCodec('mp4a.66')).toBe('mpeg2_aac');
expect(getNormalizedCodec('mp4a.67')).toBe('mpeg2_aac');
expect(getNormalizedCodec('mp4a.68')).toBe('mpeg2_aac');

expect(getNormalizedCodec('mp3')).toBe('mp3');
expect(getNormalizedCodec('mp4a.69')).toBe('mp3');
expect(getNormalizedCodec('mp4a.6B')).toBe('mp3');
expect(getNormalizedCodec('mp4a.6b')).toBe('mp3');

expect(getNormalizedCodec('mp4a.40.2')).toBe('mpeg4_aac');
expect(getNormalizedCodec('mp4a.40.02')).toBe('mpeg4_aac');
expect(getNormalizedCodec('mp4a.40.5')).toBe('mpeg4_aac');
expect(getNormalizedCodec('mp4a.40.05')).toBe('mpeg4_aac');
expect(getNormalizedCodec('mp4a.40.29')).toBe('mpeg4_aac');

expect(getNormalizedCodec('mp4a.40.42')).toBe('mpeg4_xhe_aac');

expect(getNormalizedCodec('ac-3')).toBe('ac-3');
expect(getNormalizedCodec('mp4a.a5')).toBe('ac-3');
expect(getNormalizedCodec('mp4a.A5')).toBe('ac-3');

expect(getNormalizedCodec('ec-3')).toBe('ec-3');
expect(getNormalizedCodec('mp4a.a6')).toBe('ec-3');
expect(getNormalizedCodec('mp4a.A6')).toBe('ec-3');

expect(getNormalizedCodec('vp8')).toBe('vp8');
expect(getNormalizedCodec('vp8.0')).toBe('vp8');

expect(getNormalizedCodec('dtsc')).toBe('dtsc');
expect(getNormalizedCodec('mp4a.a9')).toBe('dtsc');

expect(getNormalizedCodec('dtsx')).toBe('dtsx');
expect(getNormalizedCodec('mp4a.b2')).toBe('dtsx');

expect(getNormalizedCodec('avc1')).toBe('avc');
expect(getNormalizedCodec('avc3')).toBe('avc');

expect(getNormalizedCodec('hvc1')).toBe('hevc');
expect(getNormalizedCodec('hev1')).toBe('hevc');

expect(getNormalizedCodec('dvh1.05')).toBe('dovi');
expect(getNormalizedCodec('dvhe.05')).toBe('dovi');
});
});

0 comments on commit ac6992c

Please sign in to comment.