Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/mp4/MP4Decoder.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set ts=2 sw=2 sts=2 et cindent: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "MP4Decoder.h"
8
#include "H264.h"
9
#include "MP4Demuxer.h"
10
#include "MediaContainerType.h"
11
#include "PDMFactory.h"
12
#include "VideoUtils.h"
13
#include "mozilla/StaticPrefs.h"
14
#include "nsMimeTypes.h"
15
16
namespace mozilla {
17
18
static bool
19
IsWhitelistedH264Codec(const nsAString& aCodec)
20
0
{
21
0
  uint8_t profile = 0, constraint = 0, level = 0;
22
0
23
0
  if (!ExtractH264CodecDetails(aCodec, profile, constraint, level)) {
24
0
    return false;
25
0
  }
26
0
27
0
  // Just assume what we can play on all platforms the codecs/formats that
28
0
  // WMF can play, since we don't have documentation about what other
29
0
  // platforms can play... According to the WMF documentation:
30
0
  // http://msdn.microsoft.com/en-us/library/windows/desktop/dd797815%28v=vs.85%29.aspx
31
0
  // "The Media Foundation H.264 video decoder is a Media Foundation Transform
32
0
  // that supports decoding of Baseline, Main, and High profiles, up to level
33
0
  // 5.1.". We extend the limit to level 5.2, relying on the decoder to handle
34
0
  // any potential errors, the level limit being rather arbitrary.
35
0
  // We also report that we can play Extended profile, as there are
36
0
  // bitstreams that are Extended compliant that are also Baseline compliant.
37
0
  return level >= H264_LEVEL_1 &&
38
0
         level <= H264_LEVEL_5_2 &&
39
0
         (profile == H264_PROFILE_BASE ||
40
0
          profile == H264_PROFILE_MAIN ||
41
0
          profile == H264_PROFILE_EXTENDED ||
42
0
          profile == H264_PROFILE_HIGH);
43
0
}
44
45
/* static */
46
bool
47
MP4Decoder::IsSupportedTypeWithoutDiagnostics(
48
  const MediaContainerType& aContainerType)
49
0
{
50
0
  return IsSupportedType(aContainerType, nullptr);
51
0
}
52
53
static bool IsTypeValid(const MediaContainerType& aType)
54
0
{
55
0
  // Whitelist MP4 types, so they explicitly match what we encounter on
56
0
  // the web, as opposed to what we use internally (i.e. what our demuxers
57
0
  // etc output).
58
0
  return aType.Type() == MEDIAMIMETYPE("audio/mp4") ||
59
0
         aType.Type() == MEDIAMIMETYPE("audio/x-m4a") ||
60
0
         aType.Type() == MEDIAMIMETYPE("video/mp4") ||
61
0
         aType.Type() == MEDIAMIMETYPE("video/quicktime") ||
62
0
         aType.Type() == MEDIAMIMETYPE("video/x-m4v");
63
0
}
64
65
/* statis */ nsTArray<UniquePtr<TrackInfo>>
66
MP4Decoder::GetTracksInfo(const MediaContainerType& aType, MediaResult& aError)
67
0
{
68
0
  nsTArray<UniquePtr<TrackInfo>> tracks;
69
0
70
0
  if (!IsTypeValid(aType)) {
71
0
    aError = MediaResult(
72
0
      NS_ERROR_DOM_MEDIA_FATAL_ERR,
73
0
      RESULT_DETAIL("Invalid type:%s", aType.Type().AsString().get()));
74
0
    return tracks;
75
0
  }
76
0
77
0
  aError = NS_OK;
78
0
79
0
  const MediaCodecs& codecs = aType.ExtendedType().Codecs();
80
0
  if (codecs.IsEmpty()) {
81
0
    return tracks;
82
0
  }
83
0
84
0
  const bool isVideo = aType.Type() == MEDIAMIMETYPE("video/mp4") ||
85
0
                       aType.Type() == MEDIAMIMETYPE("video/quicktime") ||
86
0
                       aType.Type() == MEDIAMIMETYPE("video/x-m4v");
87
0
88
0
  for (const auto& codec : codecs.Range()) {
89
0
    if (IsAACCodecString(codec)) {
90
0
      tracks.AppendElement(
91
0
        CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
92
0
          NS_LITERAL_CSTRING("audio/mp4a-latm"), aType));
93
0
      continue;
94
0
    }
95
0
    if (codec.EqualsLiteral("mp3")) {
96
0
      tracks.AppendElement(
97
0
        CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
98
0
          NS_LITERAL_CSTRING("audio/mpeg"), aType));
99
0
      continue;
100
0
    }
101
0
    if (codec.EqualsLiteral("opus") || codec.EqualsLiteral("flac")) {
102
0
      tracks.AppendElement(
103
0
        CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
104
0
          NS_LITERAL_CSTRING("audio/") + NS_ConvertUTF16toUTF8(codec), aType));
105
0
      continue;
106
0
    }
107
0
    if (IsVP9CodecString(codec)) {
108
0
      auto trackInfo =
109
0
        CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
110
0
          NS_LITERAL_CSTRING("video/vp9"), aType);
111
0
      uint8_t profile = 0;
112
0
      uint8_t level = 0;
113
0
      uint8_t bitDepth = 0;
114
0
      if (ExtractVPXCodecDetails(codec, profile, level, bitDepth)) {
115
0
        trackInfo->GetAsVideoInfo()->mBitDepth = bitDepth;
116
0
      }
117
0
      tracks.AppendElement(std::move(trackInfo));
118
0
      continue;
119
0
    }
120
0
#ifdef MOZ_AV1
121
0
    if (IsAV1CodecString(codec)) {
122
0
      tracks.AppendElement(
123
0
        CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
124
0
          NS_LITERAL_CSTRING("video/av1"), aType));
125
0
      continue;
126
0
    }
127
0
#endif
128
0
    if (isVideo && IsWhitelistedH264Codec(codec)) {
129
0
      auto trackInfo =
130
0
        CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
131
0
          NS_LITERAL_CSTRING("video/avc"), aType);
132
0
      uint8_t profile = 0, constraint = 0, level = 0;
133
0
      MOZ_ALWAYS_TRUE(
134
0
        ExtractH264CodecDetails(codec, profile, constraint, level));
135
0
      uint32_t width = aType.ExtendedType().GetWidth().refOr(1280);
136
0
      uint32_t height = aType.ExtendedType().GetHeight().refOr(720);
137
0
      trackInfo->GetAsVideoInfo()->mExtraData =
138
0
        H264::CreateExtraData(profile, constraint, level, { width, height });
139
0
      tracks.AppendElement(std::move(trackInfo));
140
0
      continue;
141
0
    }
142
0
    // Unknown codec
143
0
    aError =
144
0
      MediaResult(NS_ERROR_DOM_MEDIA_FATAL_ERR,
145
0
                  RESULT_DETAIL("Unknown codec:%s",
146
0
                                NS_ConvertUTF16toUTF8(codec).get()));
147
0
  }
148
0
  return tracks;
149
0
}
150
151
/* static */
152
bool
153
MP4Decoder::IsSupportedType(const MediaContainerType& aType,
154
                            DecoderDoctorDiagnostics* aDiagnostics)
155
0
{
156
0
  if (!IsEnabled()) {
157
0
    return false;
158
0
  }
159
0
160
0
  MediaResult rv = NS_OK;
161
0
  auto tracks = GetTracksInfo(aType, rv);
162
0
  if (NS_FAILED(rv)) {
163
0
    return false;
164
0
  }
165
0
166
0
  if (tracks.IsEmpty()) {
167
0
    // No codecs specified. Assume H.264 or AAC
168
0
    if (aType.Type() == MEDIAMIMETYPE("audio/mp4") ||
169
0
        aType.Type() == MEDIAMIMETYPE("audio/x-m4a")) {
170
0
      tracks.AppendElement(
171
0
        CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
172
0
          NS_LITERAL_CSTRING("audio/mp4a-latm"), aType));
173
0
    } else {
174
0
      tracks.AppendElement(
175
0
        CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
176
0
          NS_LITERAL_CSTRING("video/avc"), aType));
177
0
    }
178
0
  }
179
0
180
0
  // Verify that we have a PDM that supports the whitelisted types.
181
0
  RefPtr<PDMFactory> platform = new PDMFactory();
182
0
  for (const auto& track : tracks) {
183
0
    if (!track || !platform->Supports(*track, aDiagnostics)) {
184
0
      return false;
185
0
    }
186
0
  }
187
0
188
0
  return true;
189
0
}
190
191
/* static */
192
bool
193
MP4Decoder::IsH264(const nsACString& aMimeType)
194
0
{
195
0
  return aMimeType.EqualsLiteral("video/mp4") ||
196
0
         aMimeType.EqualsLiteral("video/avc");
197
0
}
198
199
/* static */
200
bool
201
MP4Decoder::IsAAC(const nsACString& aMimeType)
202
0
{
203
0
  return aMimeType.EqualsLiteral("audio/mp4a-latm");
204
0
}
205
206
/* static */
207
bool
208
MP4Decoder::IsEnabled()
209
0
{
210
0
  return StaticPrefs::MediaMp4Enabled();
211
0
}
212
213
/* static */ nsTArray<UniquePtr<TrackInfo>>
214
MP4Decoder::GetTracksInfo(const MediaContainerType& aType)
215
0
{
216
0
  MediaResult rv = NS_OK;
217
0
  return GetTracksInfo(aType, rv);
218
0
}
219
220
} // namespace mozilla