webmdec.cc 6.95 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4 5 6 7 8 9 10 11
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/

12 13 14 15 16 17 18 19 20 21 22 23 24
#include "./webmdec.h"

#include <cstring>
#include <cstdio>

#include "third_party/libwebm/mkvparser.hpp"
#include "third_party/libwebm/mkvreader.hpp"

namespace {

void reset(struct WebmInputContext *const webm_ctx) {
  if (webm_ctx->reader != NULL) {
    mkvparser::MkvReader *const reader =
clang-format's avatar
clang-format committed
25
        reinterpret_cast<mkvparser::MkvReader *>(webm_ctx->reader);
26 27 28 29
    delete reader;
  }
  if (webm_ctx->segment != NULL) {
    mkvparser::Segment *const segment =
clang-format's avatar
clang-format committed
30
        reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
31 32 33 34 35 36 37 38 39 40 41 42 43 44
    delete segment;
  }
  if (webm_ctx->buffer != NULL) {
    delete[] webm_ctx->buffer;
  }
  webm_ctx->reader = NULL;
  webm_ctx->segment = NULL;
  webm_ctx->buffer = NULL;
  webm_ctx->cluster = NULL;
  webm_ctx->block_entry = NULL;
  webm_ctx->block = NULL;
  webm_ctx->block_frame_index = 0;
  webm_ctx->video_track_index = 0;
  webm_ctx->timestamp_ns = 0;
45
  webm_ctx->is_key_frame = false;
46 47 48 49
}

void get_first_cluster(struct WebmInputContext *const webm_ctx) {
  mkvparser::Segment *const segment =
clang-format's avatar
clang-format committed
50
      reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
51 52 53 54 55
  const mkvparser::Cluster *const cluster = segment->GetFirst();
  webm_ctx->cluster = cluster;
}

void rewind_and_reset(struct WebmInputContext *const webm_ctx,
Adrian Grange's avatar
Adrian Grange committed
56
                      struct AvxInputContext *const aom_ctx) {
Adrian Grange's avatar
Adrian Grange committed
57
  rewind(aom_ctx->file);
58 59 60 61 62 63
  reset(webm_ctx);
}

}  // namespace

int file_is_webm(struct WebmInputContext *webm_ctx,
Adrian Grange's avatar
Adrian Grange committed
64
                 struct AvxInputContext *aom_ctx) {
Adrian Grange's avatar
Adrian Grange committed
65
  mkvparser::MkvReader *const reader = new mkvparser::MkvReader(aom_ctx->file);
66
  webm_ctx->reader = reader;
67
  webm_ctx->reached_eos = 0;
68 69 70 71

  mkvparser::EBMLHeader header;
  long long pos = 0;
  if (header.Parse(reader, pos) < 0) {
Adrian Grange's avatar
Adrian Grange committed
72
    rewind_and_reset(webm_ctx, aom_ctx);
73 74 75
    return 0;
  }

clang-format's avatar
clang-format committed
76
  mkvparser::Segment *segment;
77
  if (mkvparser::Segment::CreateInstance(reader, pos, segment)) {
Adrian Grange's avatar
Adrian Grange committed
78
    rewind_and_reset(webm_ctx, aom_ctx);
79 80 81 82
    return 0;
  }
  webm_ctx->segment = segment;
  if (segment->Load() < 0) {
Adrian Grange's avatar
Adrian Grange committed
83
    rewind_and_reset(webm_ctx, aom_ctx);
84 85 86 87
    return 0;
  }

  const mkvparser::Tracks *const tracks = segment->GetTracks();
clang-format's avatar
clang-format committed
88
  const mkvparser::VideoTrack *video_track = NULL;
89
  for (unsigned long i = 0; i < tracks->GetTracksCount(); ++i) {
clang-format's avatar
clang-format committed
90
    const mkvparser::Track *const track = tracks->GetTrackByIndex(i);
91
    if (track->GetType() == mkvparser::Track::kVideo) {
clang-format's avatar
clang-format committed
92
      video_track = static_cast<const mkvparser::VideoTrack *>(track);
93 94 95 96 97
      webm_ctx->video_track_index = track->GetNumber();
      break;
    }
  }

98
  if (video_track == NULL || video_track->GetCodecId() == NULL) {
Adrian Grange's avatar
Adrian Grange committed
99
    rewind_and_reset(webm_ctx, aom_ctx);
100 101 102
    return 0;
  }

Adrian Grange's avatar
Adrian Grange committed
103
  if (!strncmp(video_track->GetCodecId(), "V_AV1", 5)) {
104
    aom_ctx->fourcc = AV1_FOURCC;
105
  } else {
Adrian Grange's avatar
Adrian Grange committed
106
    rewind_and_reset(webm_ctx, aom_ctx);
107 108 109
    return 0;
  }

Adrian Grange's avatar
Adrian Grange committed
110 111 112 113
  aom_ctx->framerate.denominator = 0;
  aom_ctx->framerate.numerator = 0;
  aom_ctx->width = static_cast<uint32_t>(video_track->GetWidth());
  aom_ctx->height = static_cast<uint32_t>(video_track->GetHeight());
114 115 116 117 118 119

  get_first_cluster(webm_ctx);

  return 1;
}

clang-format's avatar
clang-format committed
120 121
int webm_read_frame(struct WebmInputContext *webm_ctx, uint8_t **buffer,
                    size_t *bytes_in_buffer, size_t *buffer_size) {
122 123 124 125 126
  // This check is needed for frame parallel decoding, in which case this
  // function could be called even after it has reached end of input stream.
  if (webm_ctx->reached_eos) {
    return 1;
  }
127
  mkvparser::Segment *const segment =
clang-format's avatar
clang-format committed
128 129 130
      reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
  const mkvparser::Cluster *cluster =
      reinterpret_cast<const mkvparser::Cluster *>(webm_ctx->cluster);
131
  const mkvparser::Block *block =
clang-format's avatar
clang-format committed
132
      reinterpret_cast<const mkvparser::Block *>(webm_ctx->block);
133
  const mkvparser::BlockEntry *block_entry =
clang-format's avatar
clang-format committed
134
      reinterpret_cast<const mkvparser::BlockEntry *>(webm_ctx->block_entry);
135 136 137 138 139 140 141 142 143 144 145
  bool block_entry_eos = false;
  do {
    long status = 0;
    bool get_new_block = false;
    if (block_entry == NULL && !block_entry_eos) {
      status = cluster->GetFirst(block_entry);
      get_new_block = true;
    } else if (block_entry_eos || block_entry->EOS()) {
      cluster = segment->GetNext(cluster);
      if (cluster == NULL || cluster->EOS()) {
        *bytes_in_buffer = 0;
146
        webm_ctx->reached_eos = 1;
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
        return 1;
      }
      status = cluster->GetFirst(block_entry);
      block_entry_eos = false;
      get_new_block = true;
    } else if (block == NULL ||
               webm_ctx->block_frame_index == block->GetFrameCount() ||
               block->GetTrackNumber() != webm_ctx->video_track_index) {
      status = cluster->GetNext(block_entry, block_entry);
      if (block_entry == NULL || block_entry->EOS()) {
        block_entry_eos = true;
        continue;
      }
      get_new_block = true;
    }
    if (status) {
      return -1;
    }
    if (get_new_block) {
      block = block_entry->GetBlock();
      webm_ctx->block_frame_index = 0;
    }
  } while (block->GetTrackNumber() != webm_ctx->video_track_index ||
           block_entry_eos);

  webm_ctx->cluster = cluster;
  webm_ctx->block_entry = block_entry;
  webm_ctx->block = block;

clang-format's avatar
clang-format committed
176
  const mkvparser::Block::Frame &frame =
177 178 179
      block->GetFrame(webm_ctx->block_frame_index);
  ++webm_ctx->block_frame_index;
  if (frame.len > static_cast<long>(*buffer_size)) {
clang-format's avatar
clang-format committed
180
    delete[] * buffer;
181 182 183 184 185 186 187 188 189
    *buffer = new uint8_t[frame.len];
    if (*buffer == NULL) {
      return -1;
    }
    *buffer_size = frame.len;
    webm_ctx->buffer = *buffer;
  }
  *bytes_in_buffer = frame.len;
  webm_ctx->timestamp_ns = block->GetTime(cluster);
190
  webm_ctx->is_key_frame = block->IsKey();
191 192

  mkvparser::MkvReader *const reader =
clang-format's avatar
clang-format committed
193
      reinterpret_cast<mkvparser::MkvReader *>(webm_ctx->reader);
194 195 196 197
  return frame.Read(reader, *buffer) ? -1 : 0;
}

int webm_guess_framerate(struct WebmInputContext *webm_ctx,
Adrian Grange's avatar
Adrian Grange committed
198
                         struct AvxInputContext *aom_ctx) {
199 200 201 202 203 204 205 206 207 208
  uint32_t i = 0;
  uint8_t *buffer = NULL;
  size_t bytes_in_buffer = 0;
  size_t buffer_size = 0;
  while (webm_ctx->timestamp_ns < 1000000000 && i < 50) {
    if (webm_read_frame(webm_ctx, &buffer, &bytes_in_buffer, &buffer_size)) {
      break;
    }
    ++i;
  }
Adrian Grange's avatar
Adrian Grange committed
209 210
  aom_ctx->framerate.numerator = (i - 1) * 1000000;
  aom_ctx->framerate.denominator =
211 212 213 214 215 216 217 218
      static_cast<int>(webm_ctx->timestamp_ns / 1000);
  delete[] buffer;

  get_first_cluster(webm_ctx);
  webm_ctx->block = NULL;
  webm_ctx->block_entry = NULL;
  webm_ctx->block_frame_index = 0;
  webm_ctx->timestamp_ns = 0;
219
  webm_ctx->reached_eos = 0;
220 221 222 223

  return 0;
}

clang-format's avatar
clang-format committed
224
void webm_free(struct WebmInputContext *webm_ctx) { reset(webm_ctx); }