webmdec.cc 6.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "./webmdec.h"

#include <cstring>
#include <cstdio>

16 17
#include "third_party/libwebm/mkvparser/mkvparser.h"
#include "third_party/libwebm/mkvparser/mkvreader.h"
18 19 20 21 22 23

namespace {

void reset(struct WebmInputContext *const webm_ctx) {
  if (webm_ctx->reader != NULL) {
    mkvparser::MkvReader *const reader =
24
        reinterpret_cast<mkvparser::MkvReader *>(webm_ctx->reader);
25 26 27 28
    delete reader;
  }
  if (webm_ctx->segment != NULL) {
    mkvparser::Segment *const segment =
29
        reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
30 31 32 33 34 35 36 37 38 39 40 41 42 43
    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;
44
  webm_ctx->is_key_frame = false;
45 46 47 48
}

void get_first_cluster(struct WebmInputContext *const webm_ctx) {
  mkvparser::Segment *const segment =
49
      reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  const mkvparser::Cluster *const cluster = segment->GetFirst();
  webm_ctx->cluster = cluster;
}

void rewind_and_reset(struct WebmInputContext *const webm_ctx,
                      struct VpxInputContext *const vpx_ctx) {
  rewind(vpx_ctx->file);
  reset(webm_ctx);
}

}  // namespace

int file_is_webm(struct WebmInputContext *webm_ctx,
                 struct VpxInputContext *vpx_ctx) {
  mkvparser::MkvReader *const reader = new mkvparser::MkvReader(vpx_ctx->file);
  webm_ctx->reader = reader;
66
  webm_ctx->reached_eos = 0;
67 68 69 70 71 72 73 74

  mkvparser::EBMLHeader header;
  long long pos = 0;
  if (header.Parse(reader, pos) < 0) {
    rewind_and_reset(webm_ctx, vpx_ctx);
    return 0;
  }

75
  mkvparser::Segment *segment;
76 77 78 79 80 81 82 83 84 85 86
  if (mkvparser::Segment::CreateInstance(reader, pos, segment)) {
    rewind_and_reset(webm_ctx, vpx_ctx);
    return 0;
  }
  webm_ctx->segment = segment;
  if (segment->Load() < 0) {
    rewind_and_reset(webm_ctx, vpx_ctx);
    return 0;
  }

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

97
  if (video_track == NULL || video_track->GetCodecId() == NULL) {
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    rewind_and_reset(webm_ctx, vpx_ctx);
    return 0;
  }

  if (!strncmp(video_track->GetCodecId(), "V_VP8", 5)) {
    vpx_ctx->fourcc = VP8_FOURCC;
  } else if (!strncmp(video_track->GetCodecId(), "V_VP9", 5)) {
    vpx_ctx->fourcc = VP9_FOURCC;
  } else {
    rewind_and_reset(webm_ctx, vpx_ctx);
    return 0;
  }

  vpx_ctx->framerate.denominator = 0;
  vpx_ctx->framerate.numerator = 0;
113 114
  vpx_ctx->width = static_cast<uint32_t>(video_track->GetWidth());
  vpx_ctx->height = static_cast<uint32_t>(video_track->GetHeight());
115 116 117 118 119 120

  get_first_cluster(webm_ctx);

  return 1;
}

121
int webm_read_frame(struct WebmInputContext *webm_ctx, uint8_t **buffer,
122
                    size_t *buffer_size) {
123 124 125 126 127
  // 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;
  }
128
  mkvparser::Segment *const segment =
129 130 131
      reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
  const mkvparser::Cluster *cluster =
      reinterpret_cast<const mkvparser::Cluster *>(webm_ctx->cluster);
132
  const mkvparser::Block *block =
133
      reinterpret_cast<const mkvparser::Block *>(webm_ctx->block);
134
  const mkvparser::BlockEntry *block_entry =
135
      reinterpret_cast<const mkvparser::BlockEntry *>(webm_ctx->block_entry);
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()) {
146
        *buffer_size = 0;
147
        webm_ctx->reached_eos = 1;
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
        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;
    }
163
    if (status || block_entry == NULL) {
164 165 166 167
      return -1;
    }
    if (get_new_block) {
      block = block_entry->GetBlock();
168
      if (block == NULL) return -1;
169 170
      webm_ctx->block_frame_index = 0;
    }
171 172
  } while (block_entry_eos ||
           block->GetTrackNumber() != webm_ctx->video_track_index);
173 174 175 176 177

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

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

  mkvparser::MkvReader *const reader =
194
      reinterpret_cast<mkvparser::MkvReader *>(webm_ctx->reader);
195 196 197 198 199 200 201 202 203
  return frame.Read(reader, *buffer) ? -1 : 0;
}

int webm_guess_framerate(struct WebmInputContext *webm_ctx,
                         struct VpxInputContext *vpx_ctx) {
  uint32_t i = 0;
  uint8_t *buffer = NULL;
  size_t buffer_size = 0;
  while (webm_ctx->timestamp_ns < 1000000000 && i < 50) {
204
    if (webm_read_frame(webm_ctx, &buffer, &buffer_size)) {
205 206 207 208 209 210 211 212 213 214 215 216 217 218
      break;
    }
    ++i;
  }
  vpx_ctx->framerate.numerator = (i - 1) * 1000000;
  vpx_ctx->framerate.denominator =
      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;
}

224
void webm_free(struct WebmInputContext *webm_ctx) { reset(webm_ctx); }