Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/ogg/OggCodecState.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 <string.h>
8
9
#include "mozilla/EndianUtils.h"
10
#include <stdint.h>
11
#include <algorithm>
12
#include <opus/opus.h>
13
14
#include "OggCodecState.h"
15
#include "OpusDecoder.h"
16
#include "OpusParser.h"
17
#include "VideoUtils.h"
18
#include "XiphExtradata.h"
19
#include "nsDebug.h"
20
#include "opus/opus_multistream.h"
21
22
namespace mozilla {
23
24
extern LazyLogModule gMediaDecoderLog;
25
0
#define LOG(type, msg) MOZ_LOG(gMediaDecoderLog, type, msg)
26
27
using media::TimeUnit;
28
29
/** Decoder base class for Ogg-encapsulated streams. */
30
OggCodecState*
31
OggCodecState::Create(ogg_page* aPage)
32
0
{
33
0
  NS_ASSERTION(ogg_page_bos(aPage), "Only call on BOS page!");
34
0
  nsAutoPtr<OggCodecState> codecState;
35
0
  if (aPage->body_len > 6 && memcmp(aPage->body+1, "theora", 6) == 0) {
36
0
    codecState = new TheoraState(aPage);
37
0
  } else if (aPage->body_len > 6 && memcmp(aPage->body+1, "vorbis", 6) == 0) {
38
0
    codecState = new VorbisState(aPage);
39
0
  } else if (aPage->body_len > 8 && memcmp(aPage->body, "OpusHead", 8) == 0) {
40
0
    codecState = new OpusState(aPage);
41
0
  } else if (aPage->body_len > 8 && memcmp(aPage->body, "fishead\0", 8) == 0) {
42
0
    codecState = new SkeletonState(aPage);
43
0
  } else if (aPage->body_len > 5 && memcmp(aPage->body, "\177FLAC", 5) == 0) {
44
0
    codecState = new FlacState(aPage);
45
0
  } else {
46
0
    codecState = new OggCodecState(aPage, false);
47
0
  }
48
0
  return codecState->OggCodecState::InternalInit() ? codecState.forget() : nullptr;
49
0
}
50
51
OggCodecState::OggCodecState(ogg_page* aBosPage, bool aActive)
52
  : mPacketCount(0)
53
  , mSerial(ogg_page_serialno(aBosPage))
54
  , mActive(aActive)
55
  , mDoneReadingHeaders(!aActive)
56
0
{
57
0
  MOZ_COUNT_CTOR(OggCodecState);
58
0
  memset(&mState, 0, sizeof(ogg_stream_state));
59
0
}
60
61
OggCodecState::~OggCodecState()
62
0
{
63
0
  MOZ_COUNT_DTOR(OggCodecState);
64
0
  Reset();
65
#ifdef DEBUG
66
  int ret =
67
#endif
68
  ogg_stream_clear(&mState);
69
0
  NS_ASSERTION(ret == 0, "ogg_stream_clear failed");
70
0
}
71
72
nsresult
73
OggCodecState::Reset()
74
0
{
75
0
  if (ogg_stream_reset(&mState) != 0) {
76
0
    return NS_ERROR_FAILURE;
77
0
  }
78
0
  mPackets.Erase();
79
0
  ClearUnstamped();
80
0
  return NS_OK;
81
0
}
82
83
void
84
OggCodecState::ClearUnstamped()
85
0
{
86
0
  mUnstamped.Clear();
87
0
}
88
89
bool
90
OggCodecState::InternalInit()
91
0
{
92
0
  int ret = ogg_stream_init(&mState, mSerial);
93
0
  return ret == 0;
94
0
}
95
96
bool
97
OggCodecState::IsValidVorbisTagName(nsCString& aName)
98
0
{
99
0
  // Tag names must consist of ASCII 0x20 through 0x7D,
100
0
  // excluding 0x3D '=' which is the separator.
101
0
  uint32_t length = aName.Length();
102
0
  const char* data = aName.Data();
103
0
  for (uint32_t i = 0; i < length; i++) {
104
0
    if (data[i] < 0x20 || data[i] > 0x7D || data[i] == '=') {
105
0
      return false;
106
0
    }
107
0
  }
108
0
  return true;
109
0
}
110
111
bool
112
OggCodecState::AddVorbisComment(MetadataTags* aTags,
113
                                const char* aComment,
114
                                uint32_t aLength)
115
0
{
116
0
  const char* div = (const char*)memchr(aComment, '=', aLength);
117
0
  if (!div) {
118
0
    LOG(LogLevel::Debug, ("Skipping comment: no separator"));
119
0
    return false;
120
0
  }
121
0
  nsCString key = nsCString(aComment, div-aComment);
122
0
  if (!IsValidVorbisTagName(key)) {
123
0
    LOG(LogLevel::Debug, ("Skipping comment: invalid tag name"));
124
0
    return false;
125
0
  }
126
0
  uint32_t valueLength = aLength - (div-aComment);
127
0
  nsCString value = nsCString(div + 1, valueLength);
128
0
  if (!IsUTF8(value)) {
129
0
    LOG(LogLevel::Debug, ("Skipping comment: invalid UTF-8 in value"));
130
0
    return false;
131
0
  }
132
0
  aTags->Put(key, value);
133
0
  return true;
134
0
}
135
136
bool
137
OggCodecState::SetCodecSpecificConfig(MediaByteBuffer* aBuffer,
138
                                      OggPacketQueue& aHeaders)
139
0
{
140
0
  nsTArray<const unsigned char*> headers;
141
0
  nsTArray<size_t> headerLens;
142
0
  for (size_t i = 0; i < aHeaders.Length(); i++) {
143
0
    headers.AppendElement(aHeaders[i]->packet);
144
0
    headerLens.AppendElement(aHeaders[i]->bytes);
145
0
  }
146
0
  // Save header packets for the decoder
147
0
  if (!XiphHeadersToExtradata(aBuffer, headers, headerLens)) {
148
0
    return false;
149
0
  }
150
0
  aHeaders.Erase();
151
0
  return true;
152
0
}
153
154
void
155
VorbisState::RecordVorbisPacketSamples(ogg_packet* aPacket, long aSamples)
156
0
{
157
0
#ifdef VALIDATE_VORBIS_SAMPLE_CALCULATION
158
0
  mVorbisPacketSamples[aPacket] = aSamples;
159
0
#endif
160
0
}
161
162
void
163
VorbisState::ValidateVorbisPacketSamples(ogg_packet* aPacket, long aSamples)
164
0
{
165
0
#ifdef VALIDATE_VORBIS_SAMPLE_CALCULATION
166
0
  NS_ASSERTION(mVorbisPacketSamples[aPacket] == aSamples,
167
0
    "Decoded samples for Vorbis packet don't match expected!");
168
0
  mVorbisPacketSamples.erase(aPacket);
169
0
#endif
170
0
}
171
172
void
173
VorbisState::AssertHasRecordedPacketSamples(ogg_packet* aPacket)
174
0
{
175
0
#ifdef VALIDATE_VORBIS_SAMPLE_CALCULATION
176
0
  NS_ASSERTION(mVorbisPacketSamples.count(aPacket) == 1,
177
0
    "Must have recorded packet samples");
178
0
#endif
179
0
}
180
181
static OggPacketPtr
182
Clone(ogg_packet* aPacket)
183
0
{
184
0
  ogg_packet* p = new ogg_packet();
185
0
  memcpy(p, aPacket, sizeof(ogg_packet));
186
0
  p->packet = new unsigned char[p->bytes];
187
0
  memcpy(p->packet, aPacket->packet, p->bytes);
188
0
  return OggPacketPtr(p);
189
0
}
190
191
void
192
OggPacketQueue::Append(OggPacketPtr aPacket)
193
0
{
194
0
  nsDeque::Push(aPacket.release());
195
0
}
196
197
bool
198
OggCodecState::IsPacketReady()
199
0
{
200
0
  return !mPackets.IsEmpty();
201
0
}
202
203
OggPacketPtr
204
OggCodecState::PacketOut()
205
0
{
206
0
  if (mPackets.IsEmpty()) {
207
0
    return nullptr;
208
0
  }
209
0
  return mPackets.PopFront();
210
0
}
211
212
ogg_packet*
213
OggCodecState::PacketPeek()
214
0
{
215
0
  if (mPackets.IsEmpty()) {
216
0
    return nullptr;
217
0
  }
218
0
  return mPackets.PeekFront();
219
0
}
220
221
void
222
OggCodecState::PushFront(OggPacketQueue&& aOther)
223
0
{
224
0
  while (!aOther.IsEmpty()) {
225
0
    mPackets.PushFront(aOther.Pop());
226
0
  }
227
0
}
228
229
already_AddRefed<MediaRawData>
230
OggCodecState::PacketOutAsMediaRawData()
231
0
{
232
0
  OggPacketPtr packet = PacketOut();
233
0
  if (!packet) {
234
0
    return nullptr;
235
0
  }
236
0
237
0
  NS_ASSERTION(
238
0
    !IsHeader(packet.get()),
239
0
    "PacketOutAsMediaRawData can only be called on non-header packets");
240
0
  RefPtr<MediaRawData> sample = new MediaRawData(packet->packet, packet->bytes);
241
0
  if (packet->bytes && !sample->Data()) {
242
0
    // OOM.
243
0
    return nullptr;
244
0
  }
245
0
246
0
  int64_t end_tstamp = Time(packet->granulepos);
247
0
  NS_ASSERTION(end_tstamp >= 0, "timestamp invalid");
248
0
249
0
  int64_t duration = PacketDuration(packet.get());
250
0
  NS_ASSERTION(duration >= 0, "duration invalid");
251
0
252
0
  sample->mTimecode = TimeUnit::FromMicroseconds(packet->granulepos);
253
0
  sample->mTime = TimeUnit::FromMicroseconds(end_tstamp - duration);
254
0
  sample->mDuration = TimeUnit::FromMicroseconds(duration);
255
0
  sample->mKeyframe = IsKeyframe(packet.get());
256
0
  sample->mEOS = packet->e_o_s;
257
0
258
0
  return sample.forget();
259
0
}
260
261
nsresult
262
OggCodecState::PageIn(ogg_page* aPage)
263
0
{
264
0
  if (!mActive) {
265
0
    return NS_OK;
266
0
  }
267
0
  NS_ASSERTION(static_cast<uint32_t>(ogg_page_serialno(aPage)) == mSerial,
268
0
               "Page must be for this stream!");
269
0
  if (ogg_stream_pagein(&mState, aPage) == -1) {
270
0
    return NS_ERROR_FAILURE;
271
0
  }
272
0
  int r;
273
0
  do {
274
0
    ogg_packet packet;
275
0
    r = ogg_stream_packetout(&mState, &packet);
276
0
    if (r == 1) {
277
0
      mPackets.Append(Clone(&packet));
278
0
    }
279
0
  } while (r != 0);
280
0
  if (ogg_stream_check(&mState)) {
281
0
    NS_WARNING("Unrecoverable error in ogg_stream_packetout");
282
0
    return NS_ERROR_FAILURE;
283
0
  }
284
0
  return NS_OK;
285
0
}
286
287
nsresult
288
OggCodecState::PacketOutUntilGranulepos(bool& aFoundGranulepos)
289
0
{
290
0
  int r;
291
0
  aFoundGranulepos = false;
292
0
  // Extract packets from the sync state until either no more packets
293
0
  // come out, or we get a data packet with non -1 granulepos.
294
0
  do {
295
0
    ogg_packet packet;
296
0
    r = ogg_stream_packetout(&mState, &packet);
297
0
    if (r == 1) {
298
0
      OggPacketPtr clone = Clone(&packet);
299
0
      if (IsHeader(&packet)) {
300
0
        // Header packets go straight into the packet queue.
301
0
        mPackets.Append(std::move(clone));
302
0
      } else {
303
0
        // We buffer data packets until we encounter a granulepos. We'll
304
0
        // then use the granulepos to figure out the granulepos of the
305
0
        // preceeding packets.
306
0
        mUnstamped.AppendElement(std::move(clone));
307
0
        aFoundGranulepos = packet.granulepos > 0;
308
0
      }
309
0
    }
310
0
  } while (r != 0 && !aFoundGranulepos);
311
0
  if (ogg_stream_check(&mState)) {
312
0
    NS_WARNING("Unrecoverable error in ogg_stream_packetout");
313
0
    return NS_ERROR_FAILURE;
314
0
  }
315
0
  return NS_OK;
316
0
}
317
318
TheoraState::TheoraState(ogg_page* aBosPage)
319
  : OggCodecState(aBosPage, true)
320
  , mSetup(0)
321
  , mCtx(0)
322
0
{
323
0
  MOZ_COUNT_CTOR(TheoraState);
324
0
  th_info_init(&mTheoraInfo);
325
0
  th_comment_init(&mComment);
326
0
}
327
328
TheoraState::~TheoraState()
329
0
{
330
0
  MOZ_COUNT_DTOR(TheoraState);
331
0
  th_setup_free(mSetup);
332
0
  th_decode_free(mCtx);
333
0
  th_comment_clear(&mComment);
334
0
  th_info_clear(&mTheoraInfo);
335
0
  Reset();
336
0
}
337
338
bool
339
TheoraState::Init()
340
0
{
341
0
  if (!mActive) {
342
0
    return false;
343
0
  }
344
0
345
0
  int64_t n = mTheoraInfo.aspect_numerator;
346
0
  int64_t d = mTheoraInfo.aspect_denominator;
347
0
348
0
  float aspectRatio =
349
0
    (n == 0 || d == 0) ? 1.0f : static_cast<float>(n) / static_cast<float>(d);
350
0
351
0
  // Ensure the frame and picture regions aren't larger than our prescribed
352
0
  // maximum, or zero sized.
353
0
  gfx::IntSize frame(mTheoraInfo.frame_width, mTheoraInfo.frame_height);
354
0
  gfx::IntRect picture(mTheoraInfo.pic_x,
355
0
                       mTheoraInfo.pic_y,
356
0
                       mTheoraInfo.pic_width,
357
0
                       mTheoraInfo.pic_height);
358
0
  gfx::IntSize display(mTheoraInfo.pic_width, mTheoraInfo.pic_height);
359
0
  ScaleDisplayByAspectRatio(display, aspectRatio);
360
0
  if (!IsValidVideoRegion(frame, picture, display)) {
361
0
    return mActive = false;
362
0
  }
363
0
364
0
  mCtx = th_decode_alloc(&mTheoraInfo, mSetup);
365
0
  if (!mCtx) {
366
0
    return mActive = false;
367
0
  }
368
0
369
0
  // Video track's frame sizes will not overflow. Activate the video track.
370
0
  mInfo.mMimeType = NS_LITERAL_CSTRING("video/theora");
371
0
  mInfo.mDisplay = display;
372
0
  mInfo.mImage = frame;
373
0
  mInfo.SetImageRect(picture);
374
0
375
0
  return mActive = SetCodecSpecificConfig(mInfo.mCodecSpecificConfig, mHeaders);
376
0
}
377
378
nsresult
379
TheoraState::Reset()
380
0
{
381
0
  mHeaders.Erase();
382
0
  return OggCodecState::Reset();
383
0
}
384
385
bool
386
TheoraState::DecodeHeader(OggPacketPtr aPacket)
387
0
{
388
0
  ogg_packet* packet = aPacket.get(); // Will be owned by mHeaders.
389
0
  mHeaders.Append(std::move(aPacket));
390
0
  mPacketCount++;
391
0
  int ret = th_decode_headerin(&mTheoraInfo,
392
0
                               &mComment,
393
0
                               &mSetup,
394
0
                               packet);
395
0
396
0
  // We must determine when we've read the last header packet.
397
0
  // th_decode_headerin() does not tell us when it's read the last header, so
398
0
  // we must keep track of the headers externally.
399
0
  //
400
0
  // There are 3 header packets, the Identification, Comment, and Setup
401
0
  // headers, which must be in that order. If they're out of order, the file
402
0
  // is invalid. If we've successfully read a header, and it's the setup
403
0
  // header, then we're done reading headers. The first byte of each packet
404
0
  // determines it's type as follows:
405
0
  //    0x80 -> Identification header
406
0
  //    0x81 -> Comment header
407
0
  //    0x82 -> Setup header
408
0
  // See http://www.theora.org/doc/Theora.pdf Chapter 6, "Bitstream Headers",
409
0
  // for more details of the Ogg/Theora containment scheme.
410
0
  bool isSetupHeader = packet->bytes > 0 && packet->packet[0] == 0x82;
411
0
  if (ret < 0 || mPacketCount > 3) {
412
0
    // We've received an error, or the first three packets weren't valid
413
0
    // header packets. Assume bad input.
414
0
    // Our caller will deactivate the bitstream.
415
0
    return false;
416
0
  } else if (ret > 0 && isSetupHeader && mPacketCount == 3) {
417
0
    // Successfully read the three header packets.
418
0
    mDoneReadingHeaders = true;
419
0
  }
420
0
  return true;
421
0
}
422
423
int64_t
424
TheoraState::Time(int64_t granulepos)
425
0
{
426
0
  if (!mActive) {
427
0
    return -1;
428
0
  }
429
0
  return TheoraState::Time(&mTheoraInfo, granulepos);
430
0
}
431
432
bool
433
TheoraState::IsHeader(ogg_packet* aPacket)
434
0
{
435
0
  return th_packet_isheader(aPacket);
436
0
}
437
438
#define TH_VERSION_CHECK(_info, _maj, _min, _sub)                              \
439
0
  (((_info)->version_major > (_maj) || (_info)->version_major == (_maj)) &&    \
440
0
   (((_info)->version_minor > (_min) || (_info)->version_minor == (_min)) &&   \
441
0
    (_info)->version_subminor >= (_sub)))
442
443
int64_t
444
TheoraState::Time(th_info* aInfo, int64_t aGranulepos)
445
0
{
446
0
  if (aGranulepos < 0 || aInfo->fps_numerator == 0) {
447
0
    return -1;
448
0
  }
449
0
  // Implementation of th_granule_frame inlined here to operate
450
0
  // on the th_info structure instead of the theora_state.
451
0
  int shift = aInfo->keyframe_granule_shift;
452
0
  ogg_int64_t iframe = aGranulepos >> shift;
453
0
  ogg_int64_t pframe = aGranulepos - (iframe << shift);
454
0
  int64_t frameno = iframe + pframe - TH_VERSION_CHECK(aInfo, 3, 2, 1);
455
0
  CheckedInt64 t =
456
0
    ((CheckedInt64(frameno) + 1) * USECS_PER_S) * aInfo->fps_denominator;
457
0
  if (!t.isValid()) {
458
0
    return -1;
459
0
  }
460
0
  t /= aInfo->fps_numerator;
461
0
  return t.isValid() ? t.value() : -1;
462
0
}
463
464
int64_t TheoraState::StartTime(int64_t granulepos)
465
0
{
466
0
  if (granulepos < 0 || !mActive || mTheoraInfo.fps_numerator == 0) {
467
0
    return -1;
468
0
  }
469
0
  CheckedInt64 t =
470
0
    (CheckedInt64(th_granule_frame(mCtx, granulepos)) * USECS_PER_S)
471
0
    * mTheoraInfo.fps_denominator;
472
0
  if (!t.isValid()) {
473
0
    return -1;
474
0
  }
475
0
  return t.value() / mTheoraInfo.fps_numerator;
476
0
}
477
478
int64_t
479
TheoraState::PacketDuration(ogg_packet* aPacket)
480
0
{
481
0
  if (!mActive || mTheoraInfo.fps_numerator == 0) {
482
0
    return -1;
483
0
  }
484
0
  CheckedInt64 t = SaferMultDiv(mTheoraInfo.fps_denominator, USECS_PER_S,
485
0
                                mTheoraInfo.fps_numerator);
486
0
  return t.isValid() ? t.value() : -1;
487
0
}
488
489
int64_t
490
TheoraState::MaxKeyframeOffset()
491
0
{
492
0
  // Determine the maximum time in microseconds by which a key frame could
493
0
  // offset for the theora bitstream. Theora granulepos encode time as:
494
0
  // ((key_frame_number << granule_shift) + frame_offset).
495
0
  // Therefore the maximum possible time by which any frame could be offset
496
0
  // from a keyframe is the duration of (1 << granule_shift) - 1) frames.
497
0
  int64_t frameDuration;
498
0
499
0
  // Max number of frames keyframe could possibly be offset.
500
0
  int64_t keyframeDiff = (1 << mTheoraInfo.keyframe_granule_shift) - 1;
501
0
502
0
  // Length of frame in usecs.
503
0
  frameDuration =
504
0
    (mTheoraInfo.fps_denominator * USECS_PER_S) / mTheoraInfo.fps_numerator;
505
0
506
0
  // Total time in usecs keyframe can be offset from any given frame.
507
0
  return frameDuration * keyframeDiff;
508
0
}
509
510
bool
511
TheoraState::IsKeyframe(ogg_packet* pkt)
512
0
{
513
0
  // first bit of packet is 1 for header, 0 for data
514
0
  // second bit of packet is 1 for inter frame, 0 for intra frame
515
0
  return (pkt->bytes >= 1 && (pkt->packet[0] & 0x40) == 0x00);
516
0
}
517
518
nsresult
519
TheoraState::PageIn(ogg_page* aPage)
520
0
{
521
0
  if (!mActive)
522
0
    return NS_OK;
523
0
  NS_ASSERTION(static_cast<uint32_t>(ogg_page_serialno(aPage)) == mSerial,
524
0
               "Page must be for this stream!");
525
0
  if (ogg_stream_pagein(&mState, aPage) == -1)
526
0
    return NS_ERROR_FAILURE;
527
0
  bool foundGp;
528
0
  nsresult res = PacketOutUntilGranulepos(foundGp);
529
0
  if (NS_FAILED(res))
530
0
    return res;
531
0
  if (foundGp && mDoneReadingHeaders) {
532
0
    // We've found a packet with a granulepos, and we've loaded our metadata
533
0
    // and initialized our decoder. Determine granulepos of buffered packets.
534
0
    ReconstructTheoraGranulepos();
535
0
    for (uint32_t i = 0; i < mUnstamped.Length(); ++i) {
536
0
      OggPacketPtr packet = std::move(mUnstamped[i]);
537
#ifdef DEBUG
538
      NS_ASSERTION(!IsHeader(packet.get()), "Don't try to recover header packet gp");
539
      NS_ASSERTION(packet->granulepos != -1, "Packet must have gp by now");
540
#endif
541
      mPackets.Append(std::move(packet));
542
0
    }
543
0
    mUnstamped.Clear();
544
0
  }
545
0
  return NS_OK;
546
0
}
547
548
// Returns 1 if the Theora info struct is decoding a media of Theora
549
// version (maj,min,sub) or later, otherwise returns 0.
550
int
551
TheoraVersion(th_info* info,
552
              unsigned char maj,
553
              unsigned char min,
554
              unsigned char sub)
555
0
{
556
0
  ogg_uint32_t ver = (maj << 16) + (min << 8) + sub;
557
0
  ogg_uint32_t th_ver = (info->version_major << 16)
558
0
                        + (info->version_minor << 8)
559
0
                        + info->version_subminor;
560
0
  return (th_ver >= ver) ? 1 : 0;
561
0
}
562
563
void
564
TheoraState::ReconstructTheoraGranulepos()
565
0
{
566
0
  if (mUnstamped.Length() == 0) {
567
0
    return;
568
0
  }
569
0
  ogg_int64_t lastGranulepos = mUnstamped[mUnstamped.Length() - 1]->granulepos;
570
0
  NS_ASSERTION(lastGranulepos != -1, "Must know last granulepos");
571
0
572
0
  // Reconstruct the granulepos (and thus timestamps) of the decoded
573
0
  // frames. Granulepos are stored as ((keyframe<<shift)+offset). We
574
0
  // know the granulepos of the last frame in the list, so we can infer
575
0
  // the granulepos of the intermediate frames using their frame numbers.
576
0
  ogg_int64_t shift = mTheoraInfo.keyframe_granule_shift;
577
0
  ogg_int64_t version_3_2_1 = TheoraVersion(&mTheoraInfo,3,2,1);
578
0
  ogg_int64_t lastFrame =
579
0
    th_granule_frame(mCtx, lastGranulepos) + version_3_2_1;
580
0
  ogg_int64_t firstFrame = lastFrame - mUnstamped.Length() + 1;
581
0
582
0
  // Until we encounter a keyframe, we'll assume that the "keyframe"
583
0
  // segment of the granulepos is the first frame, or if that causes
584
0
  // the "offset" segment to overflow, we assume the required
585
0
  // keyframe is maximumally offset. Until we encounter a keyframe
586
0
  // the granulepos will probably be wrong, but we can't decode the
587
0
  // frame anyway (since we don't have its keyframe) so it doesn't really
588
0
  // matter.
589
0
  ogg_int64_t keyframe = lastGranulepos >> shift;
590
0
591
0
  // The lastFrame, firstFrame, keyframe variables, as well as the frame
592
0
  // variable in the loop below, store the frame number for Theora
593
0
  // version >= 3.2.1 streams, and store the frame index for Theora
594
0
  // version < 3.2.1 streams.
595
0
  for (uint32_t i = 0; i < mUnstamped.Length() - 1; ++i) {
596
0
    ogg_int64_t frame = firstFrame + i;
597
0
    ogg_int64_t granulepos;
598
0
    auto& packet = mUnstamped[i];
599
0
    bool isKeyframe = th_packet_iskeyframe(packet.get()) == 1;
600
0
601
0
    if (isKeyframe) {
602
0
      granulepos = frame << shift;
603
0
      keyframe = frame;
604
0
    } else if (frame >= keyframe &&
605
0
               frame - keyframe < ((ogg_int64_t)1 << shift)) {
606
0
      // (frame - keyframe) won't overflow the "offset" segment of the
607
0
      // granulepos, so it's safe to calculate the granulepos.
608
0
      granulepos = (keyframe << shift) + (frame - keyframe);
609
0
    } else {
610
0
      // (frame - keyframeno) will overflow the "offset" segment of the
611
0
      // granulepos, so we take "keyframe" to be the max possible offset
612
0
      // frame instead.
613
0
      ogg_int64_t k =
614
0
        std::max(frame - (((ogg_int64_t)1 << shift) - 1), version_3_2_1);
615
0
      granulepos = (k << shift) + (frame - k);
616
0
    }
617
0
    // Theora 3.2.1+ granulepos store frame number [1..N], so granulepos
618
0
    // should be > 0.
619
0
    // Theora 3.2.0 granulepos store the frame index [0..(N-1)], so
620
0
    // granulepos should be >= 0.
621
0
    NS_ASSERTION(granulepos >= version_3_2_1,
622
0
                  "Invalid granulepos for Theora version");
623
0
624
0
    // Check that the frame's granule number is one more than the
625
0
    // previous frame's.
626
0
    NS_ASSERTION(i == 0 ||
627
0
                 th_granule_frame(mCtx, granulepos) ==
628
0
                 th_granule_frame(mCtx, mUnstamped[i - 1]->granulepos) + 1,
629
0
                 "Granulepos calculation is incorrect!");
630
0
631
0
    packet->granulepos = granulepos;
632
0
  }
633
0
634
0
  // Check that the second to last frame's granule number is one less than
635
0
  // the last frame's (the known granule number). If not our granulepos
636
0
  // recovery missed a beat.
637
0
  NS_ASSERTION(
638
0
    mUnstamped.Length() < 2 ||
639
0
    (th_granule_frame(mCtx, mUnstamped[mUnstamped.Length() - 2]->granulepos)
640
0
     + 1) == th_granule_frame(mCtx, lastGranulepos),
641
0
    "Granulepos recovery should catch up with packet->granulepos!");
642
0
}
643
644
nsresult
645
VorbisState::Reset()
646
0
{
647
0
  nsresult res = NS_OK;
648
0
  if (mActive && vorbis_synthesis_restart(&mDsp) != 0) {
649
0
    res = NS_ERROR_FAILURE;
650
0
  }
651
0
  mHeaders.Erase();
652
0
  if (NS_FAILED(OggCodecState::Reset())) {
653
0
    return NS_ERROR_FAILURE;
654
0
  }
655
0
656
0
  mGranulepos = 0;
657
0
  mPrevVorbisBlockSize = 0;
658
0
659
0
  return res;
660
0
}
661
662
VorbisState::VorbisState(ogg_page* aBosPage)
663
  : OggCodecState(aBosPage, true)
664
  , mPrevVorbisBlockSize(0)
665
  , mGranulepos(0)
666
0
{
667
0
  MOZ_COUNT_CTOR(VorbisState);
668
0
  vorbis_info_init(&mVorbisInfo);
669
0
  vorbis_comment_init(&mComment);
670
0
  memset(&mDsp, 0, sizeof(vorbis_dsp_state));
671
0
  memset(&mBlock, 0, sizeof(vorbis_block));
672
0
}
673
674
VorbisState::~VorbisState()
675
0
{
676
0
  MOZ_COUNT_DTOR(VorbisState);
677
0
  Reset();
678
0
  vorbis_block_clear(&mBlock);
679
0
  vorbis_dsp_clear(&mDsp);
680
0
  vorbis_info_clear(&mVorbisInfo);
681
0
  vorbis_comment_clear(&mComment);
682
0
}
683
684
bool
685
VorbisState::DecodeHeader(OggPacketPtr aPacket)
686
0
{
687
0
  ogg_packet* packet = aPacket.get(); // Will be owned by mHeaders.
688
0
  mHeaders.Append(std::move(aPacket));
689
0
  mPacketCount++;
690
0
  int ret = vorbis_synthesis_headerin(&mVorbisInfo,
691
0
                                      &mComment,
692
0
                                      packet);
693
0
  // We must determine when we've read the last header packet.
694
0
  // vorbis_synthesis_headerin() does not tell us when it's read the last
695
0
  // header, so we must keep track of the headers externally.
696
0
  //
697
0
  // There are 3 header packets, the Identification, Comment, and Setup
698
0
  // headers, which must be in that order. If they're out of order, the file
699
0
  // is invalid. If we've successfully read a header, and it's the setup
700
0
  // header, then we're done reading headers. The first byte of each packet
701
0
  // determines it's type as follows:
702
0
  //    0x1 -> Identification header
703
0
  //    0x3 -> Comment header
704
0
  //    0x5 -> Setup header
705
0
  // For more details of the Vorbis/Ogg containment scheme, see the Vorbis I
706
0
  // Specification, Chapter 4, Codec Setup and Packet Decode:
707
0
  // http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-580004
708
0
709
0
  bool isSetupHeader = packet->bytes > 0 && packet->packet[0] == 0x5;
710
0
711
0
  if (ret < 0 || mPacketCount > 3) {
712
0
    // We've received an error, or the first three packets weren't valid
713
0
    // header packets. Assume bad input. Our caller will deactivate the
714
0
    // bitstream.
715
0
    return false;
716
0
  } else if (!ret && isSetupHeader && mPacketCount == 3) {
717
0
    // Successfully read the three header packets.
718
0
    // The bitstream remains active.
719
0
    mDoneReadingHeaders = true;
720
0
  }
721
0
722
0
  return true;
723
0
}
724
725
bool
726
VorbisState::Init()
727
0
{
728
0
  if (!mActive) {
729
0
    return false;
730
0
  }
731
0
732
0
  int ret = vorbis_synthesis_init(&mDsp, &mVorbisInfo);
733
0
  if (ret != 0) {
734
0
    NS_WARNING("vorbis_synthesis_init() failed initializing vorbis bitstream");
735
0
    return mActive = false;
736
0
  }
737
0
  ret = vorbis_block_init(&mDsp, &mBlock);
738
0
  if (ret != 0) {
739
0
    NS_WARNING("vorbis_block_init() failed initializing vorbis bitstream");
740
0
    if (mActive) {
741
0
      vorbis_dsp_clear(&mDsp);
742
0
    }
743
0
    return mActive = false;
744
0
  }
745
0
746
0
  nsTArray<const unsigned char*> headers;
747
0
  nsTArray<size_t> headerLens;
748
0
  for (size_t i = 0; i < mHeaders.Length(); i++) {
749
0
    headers.AppendElement(mHeaders[i]->packet);
750
0
    headerLens.AppendElement(mHeaders[i]->bytes);
751
0
  }
752
0
  // Save header packets for the decoder
753
0
  if (!XiphHeadersToExtradata(mInfo.mCodecSpecificConfig,
754
0
                              headers, headerLens)) {
755
0
    return mActive = false;
756
0
  }
757
0
  mHeaders.Erase();
758
0
  mInfo.mMimeType = NS_LITERAL_CSTRING("audio/vorbis");
759
0
  mInfo.mRate = mVorbisInfo.rate;
760
0
  mInfo.mChannels = mVorbisInfo.channels;
761
0
  mInfo.mBitDepth = 16;
762
0
763
0
  return true;
764
0
}
765
766
int64_t
767
VorbisState::Time(int64_t granulepos)
768
0
{
769
0
  if (!mActive) {
770
0
    return -1;
771
0
  }
772
0
773
0
  return VorbisState::Time(&mVorbisInfo, granulepos);
774
0
}
775
776
int64_t
777
VorbisState::Time(vorbis_info* aInfo, int64_t aGranulepos)
778
0
{
779
0
  if (aGranulepos == -1 || aInfo->rate == 0) {
780
0
    return -1;
781
0
  }
782
0
  CheckedInt64 t = SaferMultDiv(aGranulepos, USECS_PER_S, aInfo->rate);
783
0
  return t.isValid() ? t.value() : 0;
784
0
}
785
786
int64_t
787
VorbisState::PacketDuration(ogg_packet* aPacket)
788
0
{
789
0
  if (!mActive) {
790
0
    return -1;
791
0
  }
792
0
  if (aPacket->granulepos == -1) {
793
0
    return -1;
794
0
  }
795
0
  // @FIXME store these in a more stable place
796
0
  if (mVorbisPacketSamples.count(aPacket) == 0) {
797
0
    // We haven't seen this packet, don't know its size?
798
0
    return -1;
799
0
  }
800
0
801
0
  long samples = mVorbisPacketSamples[aPacket];
802
0
  return Time(samples);
803
0
}
804
805
bool
806
VorbisState::IsHeader(ogg_packet* aPacket)
807
0
{
808
0
  // The first byte in each Vorbis header packet is either 0x01, 0x03, or 0x05,
809
0
  // i.e. the first bit is odd. Audio data packets have their first bit as 0x0.
810
0
  // Any packet with its first bit set cannot be a data packet, it's a
811
0
  // (possibly invalid) header packet.
812
0
  // See: http://xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-610004.2.1
813
0
  return aPacket->bytes > 0 ? (aPacket->packet[0] & 0x1) : false;
814
0
}
815
816
MetadataTags*
817
VorbisState::GetTags()
818
0
{
819
0
  MetadataTags* tags;
820
0
  NS_ASSERTION(mComment.user_comments, "no vorbis comment strings!");
821
0
  NS_ASSERTION(mComment.comment_lengths, "no vorbis comment lengths!");
822
0
  tags = new MetadataTags;
823
0
  for (int i = 0; i < mComment.comments; i++) {
824
0
    AddVorbisComment(tags, mComment.user_comments[i],
825
0
                     mComment.comment_lengths[i]);
826
0
  }
827
0
  return tags;
828
0
}
829
830
nsresult
831
VorbisState::PageIn(ogg_page* aPage)
832
0
{
833
0
  if (!mActive) {
834
0
    return NS_OK;
835
0
  }
836
0
  NS_ASSERTION(static_cast<uint32_t>(ogg_page_serialno(aPage)) == mSerial,
837
0
               "Page must be for this stream!");
838
0
  if (ogg_stream_pagein(&mState, aPage) == -1)
839
0
    return NS_ERROR_FAILURE;
840
0
  bool foundGp;
841
0
  nsresult res = PacketOutUntilGranulepos(foundGp);
842
0
  if (NS_FAILED(res)) {
843
0
    return res;
844
0
  }
845
0
  if (foundGp && mDoneReadingHeaders) {
846
0
    // We've found a packet with a granulepos, and we've loaded our metadata
847
0
    // and initialized our decoder. Determine granulepos of buffered packets.
848
0
    ReconstructVorbisGranulepos();
849
0
    for (uint32_t i = 0; i < mUnstamped.Length(); ++i) {
850
0
      OggPacketPtr packet = std::move(mUnstamped[i]);
851
0
      AssertHasRecordedPacketSamples(packet.get());
852
0
      NS_ASSERTION(!IsHeader(packet.get()), "Don't try to recover header packet gp");
853
0
      NS_ASSERTION(packet->granulepos != -1, "Packet must have gp by now");
854
0
      mPackets.Append(std::move(packet));
855
0
    }
856
0
    mUnstamped.Clear();
857
0
  }
858
0
  return NS_OK;
859
0
}
860
861
nsresult
862
VorbisState::ReconstructVorbisGranulepos()
863
0
{
864
0
  // The number of samples in a Vorbis packet is:
865
0
  // window_blocksize(previous_packet)/4+window_blocksize(current_packet)/4
866
0
  // See: http://xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-230001.3.2
867
0
  // So we maintain mPrevVorbisBlockSize, the block size of the last packet
868
0
  // encountered. We also maintain mGranulepos, which is the granulepos of
869
0
  // the last encountered packet. This enables us to give granulepos to
870
0
  // packets when the last packet in mUnstamped doesn't have a granulepos
871
0
  // (for example if the stream was truncated).
872
0
  //
873
0
  // We validate our prediction of the number of samples decoded when
874
0
  // VALIDATE_VORBIS_SAMPLE_CALCULATION is defined by recording the predicted
875
0
  // number of samples, and verifing we extract that many when decoding
876
0
  // each packet.
877
0
878
0
  NS_ASSERTION(mUnstamped.Length() > 0, "Length must be > 0");
879
0
  auto& last = mUnstamped.LastElement();
880
0
  NS_ASSERTION(last->e_o_s || last->granulepos >= 0,
881
0
    "Must know last granulepos!");
882
0
  if (mUnstamped.Length() == 1) {
883
0
    auto& packet = mUnstamped[0];
884
0
    long blockSize = vorbis_packet_blocksize(&mVorbisInfo, packet.get());
885
0
    if (blockSize < 0) {
886
0
      // On failure vorbis_packet_blocksize returns < 0. If we've got
887
0
      // a bad packet, we just assume that decode will have to skip this
888
0
      // packet, i.e. assume 0 samples are decodable from this packet.
889
0
      blockSize = 0;
890
0
      mPrevVorbisBlockSize = 0;
891
0
    }
892
0
    long samples = mPrevVorbisBlockSize / 4 + blockSize / 4;
893
0
    mPrevVorbisBlockSize = blockSize;
894
0
    if (packet->granulepos == -1) {
895
0
      packet->granulepos = mGranulepos + samples;
896
0
    }
897
0
898
0
    // Account for a partial last frame
899
0
    if (packet->e_o_s && packet->granulepos >= mGranulepos) {
900
0
       samples = packet->granulepos - mGranulepos;
901
0
    }
902
0
903
0
    mGranulepos = packet->granulepos;
904
0
    RecordVorbisPacketSamples(packet.get(), samples);
905
0
    return NS_OK;
906
0
  }
907
0
908
0
  bool unknownGranulepos = last->granulepos == -1;
909
0
  int totalSamples = 0;
910
0
  for (int32_t i = mUnstamped.Length() - 1; i > 0; i--) {
911
0
    auto& packet = mUnstamped[i];
912
0
    auto& prev = mUnstamped[i-1];
913
0
    ogg_int64_t granulepos = packet->granulepos;
914
0
    NS_ASSERTION(granulepos != -1, "Must know granulepos!");
915
0
    long prevBlockSize = vorbis_packet_blocksize(&mVorbisInfo, prev.get());
916
0
    long blockSize = vorbis_packet_blocksize(&mVorbisInfo, packet.get());
917
0
918
0
    if (blockSize < 0 || prevBlockSize < 0) {
919
0
      // On failure vorbis_packet_blocksize returns < 0. If we've got
920
0
      // a bad packet, we just assume that decode will have to skip this
921
0
      // packet, i.e. assume 0 samples are decodable from this packet.
922
0
      blockSize = 0;
923
0
      prevBlockSize = 0;
924
0
    }
925
0
926
0
    long samples = prevBlockSize / 4 + blockSize / 4;
927
0
    totalSamples += samples;
928
0
    prev->granulepos = granulepos - samples;
929
0
    RecordVorbisPacketSamples(packet.get(), samples);
930
0
  }
931
0
932
0
  if (unknownGranulepos) {
933
0
    for (uint32_t i = 0; i < mUnstamped.Length(); i++) {
934
0
      mUnstamped[i]->granulepos += mGranulepos + totalSamples + 1;
935
0
    }
936
0
  }
937
0
938
0
  auto& first = mUnstamped[0];
939
0
  long blockSize = vorbis_packet_blocksize(&mVorbisInfo, first.get());
940
0
  if (blockSize < 0) {
941
0
    mPrevVorbisBlockSize = 0;
942
0
    blockSize = 0;
943
0
  }
944
0
945
0
  long samples = (mPrevVorbisBlockSize == 0)
946
0
                 ? 0
947
0
                 : mPrevVorbisBlockSize / 4 + blockSize / 4;
948
0
  int64_t start = first->granulepos - samples;
949
0
  RecordVorbisPacketSamples(first.get(), samples);
950
0
951
0
  if (last->e_o_s && start < mGranulepos) {
952
0
    // We've calculated that there are more samples in this page than its
953
0
    // granulepos claims, and it's the last page in the stream. This is legal,
954
0
    // and we will need to prune the trailing samples when we come to decode it.
955
0
    // We must correct the timestamps so that they follow the last Vorbis page's
956
0
    // samples.
957
0
    int64_t pruned = mGranulepos - start;
958
0
    for (uint32_t i = 0; i < mUnstamped.Length() - 1; i++) {
959
0
      mUnstamped[i]->granulepos += pruned;
960
0
    }
961
0
#ifdef VALIDATE_VORBIS_SAMPLE_CALCULATION
962
0
    mVorbisPacketSamples[last.get()] -= pruned;
963
0
#endif
964
0
  }
965
0
966
0
  mPrevVorbisBlockSize = vorbis_packet_blocksize(&mVorbisInfo, last.get());
967
0
  mPrevVorbisBlockSize = std::max(static_cast<long>(0), mPrevVorbisBlockSize);
968
0
  mGranulepos = last->granulepos;
969
0
970
0
  return NS_OK;
971
0
}
972
973
OpusState::OpusState(ogg_page* aBosPage)
974
  : OggCodecState(aBosPage, true)
975
  , mParser(nullptr)
976
  , mDecoder(nullptr)
977
  , mPrevPacketGranulepos(0)
978
  , mPrevPageGranulepos(0)
979
0
{
980
0
  MOZ_COUNT_CTOR(OpusState);
981
0
}
982
983
OpusState::~OpusState()
984
0
{
985
0
  MOZ_COUNT_DTOR(OpusState);
986
0
  Reset();
987
0
988
0
  if (mDecoder) {
989
0
    opus_multistream_decoder_destroy(mDecoder);
990
0
    mDecoder = nullptr;
991
0
  }
992
0
}
993
994
nsresult
995
OpusState::Reset()
996
0
{
997
0
  return Reset(false);
998
0
}
999
1000
nsresult
1001
OpusState::Reset(bool aStart)
1002
0
{
1003
0
  nsresult res = NS_OK;
1004
0
1005
0
  if (mActive && mDecoder) {
1006
0
    // Reset the decoder.
1007
0
    opus_multistream_decoder_ctl(mDecoder, OPUS_RESET_STATE);
1008
0
    // This lets us distinguish the first page being the last page vs. just
1009
0
    // not having processed the previous page when we encounter the last page.
1010
0
    mPrevPageGranulepos = aStart ? 0 : -1;
1011
0
    mPrevPacketGranulepos = aStart ? 0 : -1;
1012
0
  }
1013
0
1014
0
  // Clear queued data.
1015
0
  if (NS_FAILED(OggCodecState::Reset())) {
1016
0
    return NS_ERROR_FAILURE;
1017
0
  }
1018
0
1019
0
  LOG(LogLevel::Debug, ("Opus decoder reset"));
1020
0
1021
0
  return res;
1022
0
}
1023
1024
bool
1025
OpusState::Init(void)
1026
0
{
1027
0
  if (!mActive) {
1028
0
    return false;
1029
0
  }
1030
0
1031
0
  int error;
1032
0
1033
0
  NS_ASSERTION(mDecoder == nullptr, "leaking OpusDecoder");
1034
0
1035
0
  mDecoder = opus_multistream_decoder_create(mParser->mRate,
1036
0
                                             mParser->mChannels,
1037
0
                                             mParser->mStreams,
1038
0
                                             mParser->mCoupledStreams,
1039
0
                                             mParser->mMappingTable,
1040
0
                                             &error);
1041
0
1042
0
  mInfo.mMimeType = NS_LITERAL_CSTRING("audio/opus");
1043
0
  mInfo.mRate = mParser->mRate;
1044
0
  mInfo.mChannels = mParser->mChannels;
1045
0
  mInfo.mBitDepth = 16;
1046
0
  // Save preskip & the first header packet for the Opus decoder
1047
0
  OpusDataDecoder::AppendCodecDelay(mInfo.mCodecSpecificConfig,
1048
0
                                    Time(0, mParser->mPreSkip));
1049
0
  if (!mHeaders.PeekFront()) {
1050
0
    return false;
1051
0
  }
1052
0
  mInfo.mCodecSpecificConfig->AppendElements(mHeaders.PeekFront()->packet,
1053
0
                                             mHeaders.PeekFront()->bytes);
1054
0
  mHeaders.Erase();
1055
0
  LOG(LogLevel::Debug, ("Opus decoder init"));
1056
0
1057
0
  return error == OPUS_OK;
1058
0
}
1059
1060
bool
1061
OpusState::DecodeHeader(OggPacketPtr aPacket)
1062
0
{
1063
0
  switch(mPacketCount++) {
1064
0
    // Parse the id header.
1065
0
    case 0:
1066
0
      mParser = new OpusParser;
1067
0
      if (!mParser->DecodeHeader(aPacket->packet, aPacket->bytes)) {
1068
0
        return false;
1069
0
      }
1070
0
      mHeaders.Append(std::move(aPacket));
1071
0
      break;
1072
0
1073
0
    // Parse the metadata header.
1074
0
    case 1:
1075
0
      if (!mParser->DecodeTags(aPacket->packet, aPacket->bytes)) {
1076
0
        return false;
1077
0
      }
1078
0
      break;
1079
0
1080
0
    // We made it to the first data packet (which includes reconstructing
1081
0
    // timestamps for it in PageIn). Success!
1082
0
    default:
1083
0
      mDoneReadingHeaders = true;
1084
0
      // Put it back on the queue so we can decode it.
1085
0
      mPackets.PushFront(std::move(aPacket));
1086
0
      break;
1087
0
  }
1088
0
  return true;
1089
0
}
1090
1091
/* Construct and return a tags hashmap from our internal array */
1092
MetadataTags*
1093
OpusState::GetTags()
1094
0
{
1095
0
  MetadataTags* tags;
1096
0
1097
0
  tags = new MetadataTags;
1098
0
  for (uint32_t i = 0; i < mParser->mTags.Length(); i++) {
1099
0
    AddVorbisComment(tags, mParser->mTags[i].Data(),
1100
0
                     mParser->mTags[i].Length());
1101
0
  }
1102
0
1103
0
  return tags;
1104
0
}
1105
1106
/* Return the timestamp (in microseconds) equivalent to a granulepos. */
1107
int64_t
1108
OpusState::Time(int64_t aGranulepos)
1109
0
{
1110
0
  if (!mActive) {
1111
0
    return -1;
1112
0
  }
1113
0
1114
0
  return Time(mParser->mPreSkip, aGranulepos);
1115
0
}
1116
1117
int64_t
1118
OpusState::Time(int aPreSkip, int64_t aGranulepos)
1119
0
{
1120
0
  if (aGranulepos < 0) {
1121
0
    return -1;
1122
0
  }
1123
0
1124
0
  // Ogg Opus always runs at a granule rate of 48 kHz.
1125
0
  CheckedInt64 t = SaferMultDiv(aGranulepos - aPreSkip, USECS_PER_S, 48000);
1126
0
  return t.isValid() ? t.value() : -1;
1127
0
}
1128
1129
bool
1130
OpusState::IsHeader(ogg_packet* aPacket)
1131
0
{
1132
0
  return aPacket->bytes >= 16 && (!memcmp(aPacket->packet, "OpusHead", 8) ||
1133
0
                                  !memcmp(aPacket->packet, "OpusTags", 8));
1134
0
}
1135
1136
nsresult
1137
OpusState::PageIn(ogg_page* aPage)
1138
0
{
1139
0
  if (!mActive) {
1140
0
    return NS_OK;
1141
0
  }
1142
0
  NS_ASSERTION(static_cast<uint32_t>(ogg_page_serialno(aPage)) == mSerial,
1143
0
               "Page must be for this stream!");
1144
0
  if (ogg_stream_pagein(&mState, aPage) == -1)
1145
0
    return NS_ERROR_FAILURE;
1146
0
1147
0
  bool haveGranulepos;
1148
0
  nsresult rv = PacketOutUntilGranulepos(haveGranulepos);
1149
0
  if (NS_FAILED(rv) || !haveGranulepos || mPacketCount < 2) {
1150
0
    return rv;
1151
0
  }
1152
0
  if (!ReconstructOpusGranulepos()) {
1153
0
    return NS_ERROR_FAILURE;
1154
0
  }
1155
0
  for (uint32_t i = 0; i < mUnstamped.Length(); i++) {
1156
0
    OggPacketPtr packet = std::move(mUnstamped[i]);
1157
0
    NS_ASSERTION(!IsHeader(packet.get()), "Don't try to play a header packet");
1158
0
    NS_ASSERTION(packet->granulepos != -1, "Packet should have a granulepos");
1159
0
    mPackets.Append(std::move(packet));
1160
0
  }
1161
0
  mUnstamped.Clear();
1162
0
  return NS_OK;
1163
0
}
1164
1165
// Helper method to return the change in granule position due to an Opus packet
1166
// (as distinct from the number of samples in the packet, which depends on the
1167
// decoder rate). It should work with a multistream Opus file, and continue to
1168
// work should we ever allow the decoder to decode at a rate other than 48 kHz.
1169
// It even works before we've created the actual Opus decoder.
1170
static int
1171
GetOpusDeltaGP(ogg_packet* packet)
1172
0
{
1173
0
  int nframes;
1174
0
  nframes = opus_packet_get_nb_frames(packet->packet, packet->bytes);
1175
0
  if (nframes > 0) {
1176
0
    return nframes*opus_packet_get_samples_per_frame(packet->packet, 48000);
1177
0
  }
1178
0
  NS_WARNING("Invalid Opus packet.");
1179
0
  return nframes;
1180
0
}
1181
1182
int64_t
1183
OpusState::PacketDuration(ogg_packet* aPacket)
1184
0
{
1185
0
  CheckedInt64 t = SaferMultDiv(GetOpusDeltaGP(aPacket), USECS_PER_S, 48000);
1186
0
  return t.isValid() ? t.value() : -1;
1187
0
}
1188
1189
bool
1190
OpusState::ReconstructOpusGranulepos(void)
1191
0
{
1192
0
  NS_ASSERTION(mUnstamped.Length() > 0, "Must have unstamped packets");
1193
0
  NS_ASSERTION(mUnstamped.LastElement()->e_o_s ||
1194
0
               mUnstamped.LastElement()->granulepos > 0,
1195
0
               "Must know last granulepos!");
1196
0
  int64_t gp;
1197
0
  // If this is the last page, and we've seen at least one previous page (or
1198
0
  // this is the first page)...
1199
0
  if (mUnstamped.LastElement()->e_o_s) {
1200
0
    auto& last = mUnstamped.LastElement();
1201
0
    if (mPrevPageGranulepos != -1) {
1202
0
      // If this file only has one page and the final granule position is
1203
0
      // smaller than the pre-skip amount, we MUST reject the stream.
1204
0
      if (!mDoneReadingHeaders && last->granulepos < mParser->mPreSkip)
1205
0
        return false;
1206
0
      int64_t last_gp = last->granulepos;
1207
0
      gp = mPrevPageGranulepos;
1208
0
      // Loop through the packets forwards, adding the current packet's
1209
0
      // duration to the previous granulepos to get the value for the
1210
0
      // current packet.
1211
0
      for (uint32_t i = 0; i < mUnstamped.Length() - 1; ++i) {
1212
0
        auto& packet = mUnstamped[i];
1213
0
        int offset = GetOpusDeltaGP(packet.get());
1214
0
        // Check for error (negative offset) and overflow.
1215
0
        if (offset >= 0 && gp <= INT64_MAX - offset) {
1216
0
          gp += offset;
1217
0
          if (gp >= last_gp) {
1218
0
            NS_WARNING("Opus end trimming removed more than a full packet.");
1219
0
            // We were asked to remove a full packet's worth of data or more.
1220
0
            // Encoders SHOULD NOT produce streams like this, but we'll handle
1221
0
            // it for them anyway.
1222
0
            gp = last_gp;
1223
0
            mUnstamped.RemoveElementsAt(i+1, mUnstamped.Length() - (i+1));
1224
0
            packet->e_o_s = 1;
1225
0
          }
1226
0
        }
1227
0
        packet->granulepos = gp;
1228
0
      }
1229
0
      mPrevPageGranulepos = last_gp;
1230
0
      return true;
1231
0
    } else {
1232
0
      NS_WARNING("No previous granule position to use for Opus end trimming.");
1233
0
      // If we don't have a previous granule position, fall through.
1234
0
      // We simply won't trim any samples from the end.
1235
0
      // TODO: Are we guaranteed to have seen a previous page if there is one?
1236
0
    }
1237
0
  }
1238
0
1239
0
  auto& last = mUnstamped.LastElement();
1240
0
  gp = last->granulepos;
1241
0
  // Loop through the packets backwards, subtracting the next
1242
0
  // packet's duration from its granulepos to get the value
1243
0
  // for the current packet.
1244
0
  for (uint32_t i = mUnstamped.Length() - 1; i > 0; i--) {
1245
0
    int offset = GetOpusDeltaGP(mUnstamped[i].get());
1246
0
    // Check for error (negative offset) and overflow.
1247
0
    if (offset >= 0) {
1248
0
      if (offset <= gp) {
1249
0
        gp -= offset;
1250
0
      } else {
1251
0
        // If the granule position of the first data page is smaller than the
1252
0
        // number of decodable audio samples on that page, then we MUST reject
1253
0
        // the stream.
1254
0
        if (!mDoneReadingHeaders)
1255
0
          return false;
1256
0
        // It's too late to reject the stream.
1257
0
        // If we get here, this almost certainly means the file has screwed-up
1258
0
        // timestamps somewhere after the first page.
1259
0
        NS_WARNING("Clamping negative Opus granulepos to zero.");
1260
0
        gp = 0;
1261
0
      }
1262
0
    }
1263
0
    mUnstamped[i - 1]->granulepos = gp;
1264
0
  }
1265
0
1266
0
  // Check to make sure the first granule position is at least as large as the
1267
0
  // total number of samples decodable from the first page with completed
1268
0
  // packets. This requires looking at the duration of the first packet, too.
1269
0
  // We MUST reject such streams.
1270
0
  if (!mDoneReadingHeaders && GetOpusDeltaGP(mUnstamped[0].get()) > gp) {
1271
0
    return false;
1272
0
  }
1273
0
  mPrevPageGranulepos = last->granulepos;
1274
0
  return true;
1275
0
}
1276
1277
already_AddRefed<MediaRawData>
1278
OpusState::PacketOutAsMediaRawData()
1279
0
{
1280
0
  ogg_packet* packet = PacketPeek();
1281
0
  if (!packet) {
1282
0
    return nullptr;
1283
0
  }
1284
0
1285
0
  uint32_t frames = 0;
1286
0
  const int64_t endFrame = packet->granulepos;
1287
0
1288
0
  if (packet->e_o_s) {
1289
0
    frames = GetOpusDeltaGP(packet);
1290
0
  }
1291
0
1292
0
  RefPtr<MediaRawData> data = OggCodecState::PacketOutAsMediaRawData();
1293
0
  if (!data) {
1294
0
    return nullptr;
1295
0
  }
1296
0
1297
0
  if (data->mEOS && mPrevPacketGranulepos != -1) {
1298
0
    // If this is the last packet, perform end trimming.
1299
0
    int64_t startFrame = mPrevPacketGranulepos;
1300
0
    frames -= std::max<int64_t>(
1301
0
      0, std::min(endFrame - startFrame, static_cast<int64_t>(frames)));
1302
0
    data->mDiscardPadding = frames;
1303
0
  }
1304
0
1305
0
  // Save this packet's granule position in case we need to perform end
1306
0
  // trimming on the next packet.
1307
0
  mPrevPacketGranulepos = endFrame;
1308
0
1309
0
  return data.forget();
1310
0
}
1311
1312
FlacState::FlacState(ogg_page* aBosPage)
1313
  : OggCodecState(aBosPage, true)
1314
0
{
1315
0
}
1316
1317
bool
1318
FlacState::DecodeHeader(OggPacketPtr aPacket)
1319
0
{
1320
0
  if (mParser.DecodeHeaderBlock(aPacket->packet, aPacket->bytes).isErr()) {
1321
0
    return false;
1322
0
  }
1323
0
  if (mParser.HasFullMetadata()) {
1324
0
    mDoneReadingHeaders = true;
1325
0
  }
1326
0
  return true;
1327
0
}
1328
1329
int64_t
1330
FlacState::Time(int64_t granulepos)
1331
0
{
1332
0
  if (!mParser.mInfo.IsValid()) {
1333
0
    return -1;
1334
0
  }
1335
0
  CheckedInt64 t =
1336
0
      SaferMultDiv(granulepos, USECS_PER_S, mParser.mInfo.mRate);
1337
0
  if (!t.isValid()) {
1338
0
    return -1;
1339
0
  }
1340
0
  return t.value();
1341
0
}
1342
1343
int64_t
1344
FlacState::PacketDuration(ogg_packet* aPacket)
1345
0
{
1346
0
  return mParser.BlockDuration(aPacket->packet, aPacket->bytes);
1347
0
}
1348
1349
bool
1350
FlacState::IsHeader(ogg_packet* aPacket)
1351
0
{
1352
0
  auto res = mParser.IsHeaderBlock(aPacket->packet, aPacket->bytes);
1353
0
  return res.isOk() ? res.unwrap() : false;
1354
0
}
1355
1356
nsresult
1357
FlacState::PageIn(ogg_page* aPage)
1358
0
{
1359
0
  if (!mActive) {
1360
0
    return NS_OK;
1361
0
  }
1362
0
  NS_ASSERTION(static_cast<uint32_t>(ogg_page_serialno(aPage)) == mSerial,
1363
0
               "Page must be for this stream!");
1364
0
  if (ogg_stream_pagein(&mState, aPage) == -1)
1365
0
    return NS_ERROR_FAILURE;
1366
0
  bool foundGp;
1367
0
  nsresult res = PacketOutUntilGranulepos(foundGp);
1368
0
  if (NS_FAILED(res)) {
1369
0
    return res;
1370
0
  }
1371
0
  if (foundGp && mDoneReadingHeaders) {
1372
0
    // We've found a packet with a granulepos, and we've loaded our metadata
1373
0
    // and initialized our decoder. Determine granulepos of buffered packets.
1374
0
    ReconstructFlacGranulepos();
1375
0
    for (uint32_t i = 0; i < mUnstamped.Length(); ++i) {
1376
0
      OggPacketPtr packet = std::move(mUnstamped[i]);
1377
0
      NS_ASSERTION(!IsHeader(packet.get()), "Don't try to recover header packet gp");
1378
0
      NS_ASSERTION(packet->granulepos != -1, "Packet must have gp by now");
1379
0
      mPackets.Append(std::move(packet));
1380
0
    }
1381
0
    mUnstamped.Clear();
1382
0
  }
1383
0
  return NS_OK;
1384
0
}
1385
1386
// Return a hash table with tag metadata.
1387
MetadataTags*
1388
FlacState::GetTags()
1389
0
{
1390
0
  return mParser.GetTags();
1391
0
}
1392
1393
const TrackInfo*
1394
FlacState::GetInfo() const
1395
0
{
1396
0
  return &mParser.mInfo;
1397
0
}
1398
1399
bool
1400
FlacState::ReconstructFlacGranulepos(void)
1401
0
{
1402
0
  NS_ASSERTION(mUnstamped.Length() > 0, "Must have unstamped packets");
1403
0
  auto& last = mUnstamped.LastElement();
1404
0
  NS_ASSERTION(last->e_o_s || last->granulepos > 0,
1405
0
      "Must know last granulepos!");
1406
0
  int64_t gp;
1407
0
1408
0
  gp = last->granulepos;
1409
0
  // Loop through the packets backwards, subtracting the next
1410
0
  // packet's duration from its granulepos to get the value
1411
0
  // for the current packet.
1412
0
  for (uint32_t i = mUnstamped.Length() - 1; i > 0; i--) {
1413
0
    int offset =
1414
0
      mParser.BlockDuration(mUnstamped[i]->packet, mUnstamped[i]->bytes);
1415
0
    // Check for error (negative offset) and overflow.
1416
0
    if (offset >= 0) {
1417
0
      if (offset <= gp) {
1418
0
        gp -= offset;
1419
0
      } else {
1420
0
        // If the granule position of the first data page is smaller than the
1421
0
        // number of decodable audio samples on that page, then we MUST reject
1422
0
        // the stream.
1423
0
        if (!mDoneReadingHeaders) {
1424
0
          return false;
1425
0
        }
1426
0
        // It's too late to reject the stream.
1427
0
        // If we get here, this almost certainly means the file has screwed-up
1428
0
        // timestamps somewhere after the first page.
1429
0
        NS_WARNING("Clamping negative granulepos to zero.");
1430
0
        gp = 0;
1431
0
      }
1432
0
    }
1433
0
    mUnstamped[i - 1]->granulepos = gp;
1434
0
  }
1435
0
1436
0
  return true;
1437
0
}
1438
1439
SkeletonState::SkeletonState(ogg_page* aBosPage)
1440
  : OggCodecState(aBosPage, true)
1441
  , mVersion(0)
1442
  , mPresentationTime(0)
1443
  , mLength(0)
1444
0
{
1445
0
  MOZ_COUNT_CTOR(SkeletonState);
1446
0
}
1447
1448
SkeletonState::~SkeletonState()
1449
0
{
1450
0
  MOZ_COUNT_DTOR(SkeletonState);
1451
0
}
1452
1453
// Support for Ogg Skeleton 4.0, as per specification at:
1454
// http://wiki.xiph.org/Ogg_Skeleton_4
1455
1456
// Minimum length in bytes of a Skeleton header packet.
1457
static const long SKELETON_MIN_HEADER_LEN = 28;
1458
static const long SKELETON_4_0_MIN_HEADER_LEN = 80;
1459
1460
// Minimum length in bytes of a Skeleton 4.0 index packet.
1461
static const long SKELETON_4_0_MIN_INDEX_LEN = 42;
1462
1463
// Minimum length in bytes of a Skeleton 3.0/4.0 Fisbone packet.
1464
static const long SKELETON_MIN_FISBONE_LEN = 52;
1465
1466
// Minimum possible size of a compressed index keypoint.
1467
static const size_t MIN_KEY_POINT_SIZE = 2;
1468
1469
// Byte offset of the major and minor version numbers in the
1470
// Ogg Skeleton 4.0 header packet.
1471
static const size_t SKELETON_VERSION_MAJOR_OFFSET = 8;
1472
static const size_t SKELETON_VERSION_MINOR_OFFSET = 10;
1473
1474
// Byte-offsets of the presentation time numerator and denominator
1475
static const size_t SKELETON_PRESENTATION_TIME_NUMERATOR_OFFSET = 12;
1476
static const size_t SKELETON_PRESENTATION_TIME_DENOMINATOR_OFFSET = 20;
1477
1478
// Byte-offsets of the length of file field in the Skeleton 4.0 header packet.
1479
static const size_t SKELETON_FILE_LENGTH_OFFSET = 64;
1480
1481
// Byte-offsets of the fields in the Skeleton index packet.
1482
static const size_t INDEX_SERIALNO_OFFSET = 6;
1483
static const size_t INDEX_NUM_KEYPOINTS_OFFSET = 10;
1484
static const size_t INDEX_TIME_DENOM_OFFSET = 18;
1485
static const size_t INDEX_FIRST_NUMER_OFFSET = 26;
1486
static const size_t INDEX_LAST_NUMER_OFFSET = 34;
1487
static const size_t INDEX_KEYPOINT_OFFSET = 42;
1488
1489
// Byte-offsets of the fields in the Skeleton Fisbone packet.
1490
static const size_t FISBONE_MSG_FIELDS_OFFSET = 8;
1491
static const size_t FISBONE_SERIALNO_OFFSET = 12;
1492
1493
static bool
1494
IsSkeletonBOS(ogg_packet* aPacket)
1495
0
{
1496
0
  static_assert(SKELETON_MIN_HEADER_LEN >= 8,
1497
0
                "Minimum length of skeleton BOS header incorrect");
1498
0
  return aPacket->bytes >= SKELETON_MIN_HEADER_LEN &&
1499
0
         memcmp(reinterpret_cast<char*>(aPacket->packet), "fishead", 8) == 0;
1500
0
}
1501
1502
static bool
1503
IsSkeletonIndex(ogg_packet* aPacket)
1504
0
{
1505
0
  static_assert(SKELETON_4_0_MIN_INDEX_LEN >= 5,
1506
0
                "Minimum length of skeleton index header incorrect");
1507
0
  return aPacket->bytes >= SKELETON_4_0_MIN_INDEX_LEN &&
1508
0
         memcmp(reinterpret_cast<char*>(aPacket->packet), "index", 5) == 0;
1509
0
}
1510
1511
static bool
1512
IsSkeletonFisbone(ogg_packet* aPacket)
1513
0
{
1514
0
  static_assert(SKELETON_MIN_FISBONE_LEN >= 8,
1515
0
                "Minimum length of skeleton fisbone header incorrect");
1516
0
  return aPacket->bytes >= SKELETON_MIN_FISBONE_LEN &&
1517
0
         memcmp(reinterpret_cast<char*>(aPacket->packet), "fisbone", 8) == 0;
1518
0
}
1519
1520
// Reads a variable length encoded integer at p. Will not read
1521
// past aLimit. Returns pointer to character after end of integer.
1522
static const unsigned char*
1523
ReadVariableLengthInt(const unsigned char* p,
1524
                      const unsigned char* aLimit,
1525
                      int64_t& n)
1526
0
{
1527
0
  int shift = 0;
1528
0
  int64_t byte = 0;
1529
0
  n = 0;
1530
0
  while (p < aLimit && (byte & 0x80) != 0x80 && shift < 57) {
1531
0
    byte = static_cast<int64_t>(*p);
1532
0
    n |= ((byte & 0x7f) << shift);
1533
0
    shift += 7;
1534
0
    p++;
1535
0
  }
1536
0
  return p;
1537
0
}
1538
1539
bool
1540
SkeletonState::DecodeIndex(ogg_packet* aPacket)
1541
0
{
1542
0
  NS_ASSERTION(aPacket->bytes >= SKELETON_4_0_MIN_INDEX_LEN,
1543
0
               "Index must be at least minimum size");
1544
0
  if (!mActive) {
1545
0
    return false;
1546
0
  }
1547
0
1548
0
  uint32_t serialno =
1549
0
    LittleEndian::readUint32(aPacket->packet + INDEX_SERIALNO_OFFSET);
1550
0
  int64_t numKeyPoints =
1551
0
    LittleEndian::readInt64(aPacket->packet + INDEX_NUM_KEYPOINTS_OFFSET);
1552
0
1553
0
  int64_t endTime = 0, startTime = 0;
1554
0
  const unsigned char* p = aPacket->packet;
1555
0
1556
0
  int64_t timeDenom =
1557
0
    LittleEndian::readInt64(aPacket->packet + INDEX_TIME_DENOM_OFFSET);
1558
0
  if (timeDenom == 0) {
1559
0
    LOG(LogLevel::Debug, ("Ogg Skeleton Index packet for stream %u has 0 "
1560
0
                       "timestamp denominator.", serialno));
1561
0
    return (mActive = false);
1562
0
  }
1563
0
1564
0
  // Extract the start time.
1565
0
  int64_t timeRawInt = LittleEndian::readInt64(p + INDEX_FIRST_NUMER_OFFSET);
1566
0
  CheckedInt64 t = SaferMultDiv(timeRawInt, USECS_PER_S, timeDenom);
1567
0
  if (!t.isValid()) {
1568
0
    return (mActive = false);
1569
0
  } else {
1570
0
    startTime = t.value();
1571
0
  }
1572
0
1573
0
  // Extract the end time.
1574
0
  timeRawInt = LittleEndian::readInt64(p + INDEX_LAST_NUMER_OFFSET);
1575
0
  t = SaferMultDiv(timeRawInt, USECS_PER_S, timeDenom);
1576
0
  if (!t.isValid()) {
1577
0
    return (mActive = false);
1578
0
  } else {
1579
0
    endTime = t.value();
1580
0
  }
1581
0
1582
0
  // Check the numKeyPoints value read, ensure we're not going to run out of
1583
0
  // memory while trying to decode the index packet.
1584
0
  CheckedInt64 minPacketSize =
1585
0
    (CheckedInt64(numKeyPoints) * MIN_KEY_POINT_SIZE) + INDEX_KEYPOINT_OFFSET;
1586
0
  if (!minPacketSize.isValid())
1587
0
  {
1588
0
    return (mActive = false);
1589
0
  }
1590
0
1591
0
  int64_t sizeofIndex = aPacket->bytes - INDEX_KEYPOINT_OFFSET;
1592
0
  int64_t maxNumKeyPoints = sizeofIndex / MIN_KEY_POINT_SIZE;
1593
0
  if (aPacket->bytes < minPacketSize.value() ||
1594
0
      numKeyPoints > maxNumKeyPoints ||
1595
0
      numKeyPoints < 0) {
1596
0
    // Packet size is less than the theoretical minimum size, or the packet is
1597
0
    // claiming to store more keypoints than it's capable of storing. This means
1598
0
    // that the numKeyPoints field is too large or small for the packet to
1599
0
    // possibly contain as many packets as it claims to, so the numKeyPoints
1600
0
    // field is possibly malicious. Don't try decoding this index, we may run
1601
0
    // out of memory.
1602
0
    LOG(LogLevel::Debug, ("Possibly malicious number of key points reported "
1603
0
                       "(%" PRId64 ") in index packet for stream %u.",
1604
0
                       numKeyPoints,
1605
0
                       serialno));
1606
0
    return (mActive = false);
1607
0
  }
1608
0
1609
0
  nsAutoPtr<nsKeyFrameIndex> keyPoints(new nsKeyFrameIndex(startTime, endTime));
1610
0
1611
0
  p = aPacket->packet + INDEX_KEYPOINT_OFFSET;
1612
0
  const unsigned char* limit = aPacket->packet + aPacket->bytes;
1613
0
  int64_t numKeyPointsRead = 0;
1614
0
  CheckedInt64 offset = 0;
1615
0
  CheckedInt64 time = 0;
1616
0
  while (p < limit && numKeyPointsRead < numKeyPoints) {
1617
0
    int64_t delta = 0;
1618
0
    p = ReadVariableLengthInt(p, limit, delta);
1619
0
    offset += delta;
1620
0
    if (p == limit ||
1621
0
        !offset.isValid() ||
1622
0
        offset.value() > mLength ||
1623
0
        offset.value() < 0) {
1624
0
      return (mActive = false);
1625
0
    }
1626
0
    p = ReadVariableLengthInt(p, limit, delta);
1627
0
    time += delta;
1628
0
    if (!time.isValid() ||
1629
0
        time.value() > endTime ||
1630
0
        time.value() < startTime) {
1631
0
      return (mActive = false);
1632
0
    }
1633
0
    CheckedInt64 timeUsecs = SaferMultDiv(time.value(), USECS_PER_S, timeDenom);
1634
0
    if (!timeUsecs.isValid()) {
1635
0
      return (mActive = false);
1636
0
    }
1637
0
    keyPoints->Add(offset.value(), timeUsecs.value());
1638
0
    numKeyPointsRead++;
1639
0
  }
1640
0
1641
0
  int32_t keyPointsRead = keyPoints->Length();
1642
0
  if (keyPointsRead > 0) {
1643
0
    mIndex.Put(serialno, keyPoints.forget());
1644
0
  }
1645
0
1646
0
  LOG(LogLevel::Debug, ("Loaded %d keypoints for Skeleton on stream %u",
1647
0
                     keyPointsRead, serialno));
1648
0
  return true;
1649
0
}
1650
1651
nsresult
1652
SkeletonState::IndexedSeekTargetForTrack(uint32_t aSerialno,
1653
                                         int64_t aTarget,
1654
                                         nsKeyPoint& aResult)
1655
0
{
1656
0
  nsKeyFrameIndex* index = nullptr;
1657
0
  mIndex.Get(aSerialno, &index);
1658
0
1659
0
  if (!index ||
1660
0
      index->Length() == 0 ||
1661
0
      aTarget < index->mStartTime ||
1662
0
      aTarget > index->mEndTime) {
1663
0
    return NS_ERROR_FAILURE;
1664
0
  }
1665
0
1666
0
  // Binary search to find the last key point with time less than target.
1667
0
  int start = 0;
1668
0
  int end = index->Length() - 1;
1669
0
  while (end > start) {
1670
0
    int mid = start + ((end - start + 1) >> 1);
1671
0
    if (index->Get(mid).mTime == aTarget) {
1672
0
       start = mid;
1673
0
       break;
1674
0
    } else if (index->Get(mid).mTime < aTarget) {
1675
0
      start = mid;
1676
0
    } else {
1677
0
      end = mid - 1;
1678
0
    }
1679
0
  }
1680
0
1681
0
  aResult = index->Get(start);
1682
0
  NS_ASSERTION(aResult.mTime <= aTarget, "Result should have time <= target");
1683
0
  return NS_OK;
1684
0
}
1685
1686
nsresult
1687
SkeletonState::IndexedSeekTarget(int64_t aTarget,
1688
                                 nsTArray<uint32_t>& aTracks,
1689
                                 nsSeekTarget& aResult)
1690
0
{
1691
0
  if (!mActive || mVersion < SKELETON_VERSION(4,0)) {
1692
0
    return NS_ERROR_FAILURE;
1693
0
  }
1694
0
  // Loop over all requested tracks' indexes, and get the keypoint for that
1695
0
  // seek target. Record the keypoint with the lowest offset, this will be
1696
0
  // our seek result. User must seek to the one with lowest offset to ensure we
1697
0
  // pass "keyframes" on all tracks when we decode forwards to the seek target.
1698
0
  nsSeekTarget r;
1699
0
  for (uint32_t i=0; i<aTracks.Length(); i++) {
1700
0
    nsKeyPoint k;
1701
0
    if (NS_SUCCEEDED(IndexedSeekTargetForTrack(aTracks[i], aTarget, k)) &&
1702
0
        k.mOffset < r.mKeyPoint.mOffset) {
1703
0
      r.mKeyPoint = k;
1704
0
      r.mSerial = aTracks[i];
1705
0
    }
1706
0
  }
1707
0
  if (r.IsNull()) {
1708
0
    return NS_ERROR_FAILURE;
1709
0
  }
1710
0
  LOG(LogLevel::Debug, ("Indexed seek target for time %" PRId64 " is offset %" PRId64,
1711
0
                     aTarget, r.mKeyPoint.mOffset));
1712
0
  aResult = r;
1713
0
  return NS_OK;
1714
0
}
1715
1716
nsresult
1717
SkeletonState::GetDuration(const nsTArray<uint32_t>& aTracks,
1718
                           int64_t& aDuration)
1719
0
{
1720
0
  if (!mActive ||
1721
0
      mVersion < SKELETON_VERSION(4,0) ||
1722
0
      !HasIndex() ||
1723
0
      aTracks.Length() == 0) {
1724
0
    return NS_ERROR_FAILURE;
1725
0
  }
1726
0
  int64_t endTime = INT64_MIN;
1727
0
  int64_t startTime = INT64_MAX;
1728
0
  for (uint32_t i=0; i<aTracks.Length(); i++) {
1729
0
    nsKeyFrameIndex* index = nullptr;
1730
0
    mIndex.Get(aTracks[i], &index);
1731
0
    if (!index) {
1732
0
      // Can't get the timestamps for one of the required tracks, fail.
1733
0
      return NS_ERROR_FAILURE;
1734
0
    }
1735
0
    if (index->mEndTime > endTime) {
1736
0
      endTime = index->mEndTime;
1737
0
    }
1738
0
    if (index->mStartTime < startTime) {
1739
0
      startTime = index->mStartTime;
1740
0
    }
1741
0
  }
1742
0
  NS_ASSERTION(endTime > startTime, "Duration must be positive");
1743
0
  CheckedInt64 duration = CheckedInt64(endTime) - startTime;
1744
0
  aDuration = duration.isValid() ? duration.value() : 0;
1745
0
  return duration.isValid() ? NS_OK : NS_ERROR_FAILURE;
1746
0
}
1747
1748
bool
1749
SkeletonState::DecodeFisbone(ogg_packet* aPacket)
1750
0
{
1751
0
  if (aPacket->bytes < static_cast<long>(FISBONE_MSG_FIELDS_OFFSET + 4)) {
1752
0
    return false;
1753
0
  }
1754
0
  uint32_t offsetMsgField =
1755
0
    LittleEndian::readUint32(aPacket->packet + FISBONE_MSG_FIELDS_OFFSET);
1756
0
1757
0
  if (aPacket->bytes < static_cast<long>(FISBONE_SERIALNO_OFFSET + 4)) {
1758
0
      return false;
1759
0
  }
1760
0
  uint32_t serialno =
1761
0
    LittleEndian::readUint32(aPacket->packet + FISBONE_SERIALNO_OFFSET);
1762
0
1763
0
  CheckedUint32 checked_fields_pos =
1764
0
    CheckedUint32(FISBONE_MSG_FIELDS_OFFSET) + offsetMsgField;
1765
0
  if (!checked_fields_pos.isValid() ||
1766
0
      aPacket->bytes < static_cast<int64_t>(checked_fields_pos.value())) {
1767
0
    return false;
1768
0
  }
1769
0
  int64_t msgLength = aPacket->bytes - checked_fields_pos.value();
1770
0
  char* msgProbe = (char*)aPacket->packet + checked_fields_pos.value();
1771
0
  char* msgHead = msgProbe;
1772
0
  nsAutoPtr<MessageField> field(new MessageField());
1773
0
1774
0
  const static FieldPatternType kFieldTypeMaps[] = {
1775
0
      {"Content-Type:", eContentType},
1776
0
      {"Role:", eRole},
1777
0
      {"Name:", eName},
1778
0
      {"Language:", eLanguage},
1779
0
      {"Title:", eTitle},
1780
0
      {"Display-hint:", eDisplayHint},
1781
0
      {"Altitude:", eAltitude},
1782
0
      {"TrackOrder:", eTrackOrder},
1783
0
      {"Track dependencies:", eTrackDependencies}
1784
0
  };
1785
0
1786
0
  bool isContentTypeParsed = false;
1787
0
  while (msgLength > 1) {
1788
0
    if (*msgProbe == '\r' && *(msgProbe+1) == '\n') {
1789
0
      nsAutoCString strMsg(msgHead, msgProbe-msgHead);
1790
0
      for (size_t i = 0; i < ArrayLength(kFieldTypeMaps); i++) {
1791
0
        if (strMsg.Find(kFieldTypeMaps[i].mPatternToRecognize) != -1) {
1792
0
          // The content of message header fields follows [RFC2822], and the
1793
0
          // mandatory message field must be encoded in US-ASCII, others
1794
0
          // must be be encoded in UTF-8. "Content-Type" must come first
1795
0
          // for all of message header fields.
1796
0
          // See http://svn.annodex.net/standards/draft-pfeiffer-oggskeleton-current.txt.
1797
0
          if (i != 0 && !isContentTypeParsed) {
1798
0
            return false;
1799
0
          }
1800
0
1801
0
          if ((i == 0 && IsASCII(strMsg)) || (i != 0 && IsUTF8(strMsg))) {
1802
0
            EMsgHeaderType eHeaderType = kFieldTypeMaps[i].mMsgHeaderType;
1803
0
            field->mValuesStore.LookupForAdd(eHeaderType).OrInsert(
1804
0
              [i, msgHead, msgProbe] () {
1805
0
                uint32_t nameLen = strlen(kFieldTypeMaps[i].mPatternToRecognize);
1806
0
                return new nsCString(msgHead + nameLen, msgProbe - msgHead - nameLen);
1807
0
              });
1808
0
            isContentTypeParsed = i == 0 ? true : isContentTypeParsed;
1809
0
          }
1810
0
          break;
1811
0
        }
1812
0
      }
1813
0
      msgProbe += 2;
1814
0
      msgLength -= 2;
1815
0
      msgHead = msgProbe;
1816
0
      continue;
1817
0
    }
1818
0
    msgLength--;
1819
0
    msgProbe++;
1820
0
  }
1821
0
1822
0
  auto entry = mMsgFieldStore.LookupForAdd(serialno);
1823
0
  if (entry) {
1824
0
    // mMsgFieldStore has an entry for serialno already.
1825
0
    return false;
1826
0
  }
1827
0
  entry.OrInsert([&field]() { return field.forget(); });
1828
0
  return true;
1829
0
}
1830
1831
bool
1832
SkeletonState::DecodeHeader(OggPacketPtr aPacket)
1833
0
{
1834
0
  if (IsSkeletonBOS(aPacket.get())) {
1835
0
    uint16_t verMajor =
1836
0
      LittleEndian::readUint16(aPacket->packet + SKELETON_VERSION_MAJOR_OFFSET);
1837
0
    uint16_t verMinor =
1838
0
      LittleEndian::readUint16(aPacket->packet + SKELETON_VERSION_MINOR_OFFSET);
1839
0
1840
0
    // Read the presentation time. We read this before the version check as the
1841
0
    // presentation time exists in all versions.
1842
0
    int64_t n = LittleEndian::readInt64(
1843
0
      aPacket->packet + SKELETON_PRESENTATION_TIME_NUMERATOR_OFFSET);
1844
0
    int64_t d = LittleEndian::readInt64(
1845
0
      aPacket->packet + SKELETON_PRESENTATION_TIME_DENOMINATOR_OFFSET);
1846
0
    mPresentationTime =
1847
0
      d == 0 ? 0
1848
0
             : (static_cast<float>(n) / static_cast<float>(d)) * USECS_PER_S;
1849
0
1850
0
    mVersion = SKELETON_VERSION(verMajor, verMinor);
1851
0
    // We can only care to parse Skeleton version 4.0+.
1852
0
    if (mVersion < SKELETON_VERSION(4,0) ||
1853
0
        mVersion >= SKELETON_VERSION(5,0) ||
1854
0
        aPacket->bytes < SKELETON_4_0_MIN_HEADER_LEN) {
1855
0
      return false;
1856
0
    }
1857
0
1858
0
    // Extract the segment length.
1859
0
    mLength =
1860
0
      LittleEndian::readInt64(aPacket->packet + SKELETON_FILE_LENGTH_OFFSET);
1861
0
1862
0
    LOG(LogLevel::Debug, ("Skeleton segment length: %" PRId64, mLength));
1863
0
1864
0
    // Initialize the serialno-to-index map.
1865
0
    return true;
1866
0
  } else if (IsSkeletonIndex(aPacket.get()) && mVersion >= SKELETON_VERSION(4,0)) {
1867
0
    return DecodeIndex(aPacket.get());
1868
0
  } else if (IsSkeletonFisbone(aPacket.get())) {
1869
0
    return DecodeFisbone(aPacket.get());
1870
0
  } else if (aPacket->e_o_s) {
1871
0
    mDoneReadingHeaders = true;
1872
0
    return true;
1873
0
  }
1874
0
  return true;
1875
0
}
1876
1877
} // namespace mozilla
1878