2014-04-03 11:41:14 +04:00
|
|
|
/*
|
2016-09-02 00:32:49 +03:00
|
|
|
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
2014-04-03 11:41:14 +04:00
|
|
|
*
|
2016-09-02 00:32:49 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-04-03 11:41:14 +04:00
|
|
|
|
|
|
|
#include "./webmdec.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdio>
|
|
|
|
|
2016-03-24 23:12:51 +03:00
|
|
|
#include "third_party/libwebm/mkvparser/mkvparser.h"
|
|
|
|
#include "third_party/libwebm/mkvparser/mkvreader.h"
|
2014-04-03 11:41:14 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void reset(struct WebmInputContext *const webm_ctx) {
|
|
|
|
if (webm_ctx->reader != NULL) {
|
|
|
|
mkvparser::MkvReader *const reader =
|
2016-08-13 01:10:25 +03:00
|
|
|
reinterpret_cast<mkvparser::MkvReader *>(webm_ctx->reader);
|
2014-04-03 11:41:14 +04:00
|
|
|
delete reader;
|
|
|
|
}
|
|
|
|
if (webm_ctx->segment != NULL) {
|
|
|
|
mkvparser::Segment *const segment =
|
2016-08-13 01:10:25 +03:00
|
|
|
reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
|
2014-04-03 11:41:14 +04:00
|
|
|
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;
|
2015-01-27 23:26:28 +03:00
|
|
|
webm_ctx->is_key_frame = false;
|
2014-04-03 11:41:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void get_first_cluster(struct WebmInputContext *const webm_ctx) {
|
|
|
|
mkvparser::Segment *const segment =
|
2016-08-13 01:10:25 +03:00
|
|
|
reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
|
2014-04-03 11:41:14 +04:00
|
|
|
const mkvparser::Cluster *const cluster = segment->GetFirst();
|
|
|
|
webm_ctx->cluster = cluster;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rewind_and_reset(struct WebmInputContext *const webm_ctx,
|
2016-08-31 00:01:10 +03:00
|
|
|
struct AvxInputContext *const aom_ctx) {
|
|
|
|
rewind(aom_ctx->file);
|
2014-04-03 11:41:14 +04:00
|
|
|
reset(webm_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int file_is_webm(struct WebmInputContext *webm_ctx,
|
2016-08-31 00:01:10 +03:00
|
|
|
struct AvxInputContext *aom_ctx) {
|
|
|
|
mkvparser::MkvReader *const reader = new mkvparser::MkvReader(aom_ctx->file);
|
2014-04-03 11:41:14 +04:00
|
|
|
webm_ctx->reader = reader;
|
2015-03-30 22:58:26 +03:00
|
|
|
webm_ctx->reached_eos = 0;
|
2014-04-03 11:41:14 +04:00
|
|
|
|
|
|
|
mkvparser::EBMLHeader header;
|
|
|
|
long long pos = 0;
|
|
|
|
if (header.Parse(reader, pos) < 0) {
|
2016-08-31 00:01:10 +03:00
|
|
|
rewind_and_reset(webm_ctx, aom_ctx);
|
2014-04-03 11:41:14 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-13 01:10:25 +03:00
|
|
|
mkvparser::Segment *segment;
|
2014-04-03 11:41:14 +04:00
|
|
|
if (mkvparser::Segment::CreateInstance(reader, pos, segment)) {
|
2016-08-31 00:01:10 +03:00
|
|
|
rewind_and_reset(webm_ctx, aom_ctx);
|
2014-04-03 11:41:14 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
webm_ctx->segment = segment;
|
|
|
|
if (segment->Load() < 0) {
|
2016-08-31 00:01:10 +03:00
|
|
|
rewind_and_reset(webm_ctx, aom_ctx);
|
2014-04-03 11:41:14 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mkvparser::Tracks *const tracks = segment->GetTracks();
|
2016-08-13 01:10:25 +03:00
|
|
|
const mkvparser::VideoTrack *video_track = NULL;
|
2014-04-03 11:41:14 +04:00
|
|
|
for (unsigned long i = 0; i < tracks->GetTracksCount(); ++i) {
|
2016-08-13 01:10:25 +03:00
|
|
|
const mkvparser::Track *const track = tracks->GetTrackByIndex(i);
|
2014-04-03 11:41:14 +04:00
|
|
|
if (track->GetType() == mkvparser::Track::kVideo) {
|
2016-08-13 01:10:25 +03:00
|
|
|
video_track = static_cast<const mkvparser::VideoTrack *>(track);
|
2014-04-03 11:41:14 +04:00
|
|
|
webm_ctx->video_track_index = track->GetNumber();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 20:44:59 +03:00
|
|
|
if (video_track == NULL || video_track->GetCodecId() == NULL) {
|
2016-08-31 00:01:10 +03:00
|
|
|
rewind_and_reset(webm_ctx, aom_ctx);
|
2014-04-03 11:41:14 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
if (!strncmp(video_track->GetCodecId(), "V_AV1", 5)) {
|
|
|
|
aom_ctx->fourcc = AV1_FOURCC;
|
2014-04-03 11:41:14 +04:00
|
|
|
} else {
|
2016-08-31 00:01:10 +03:00
|
|
|
rewind_and_reset(webm_ctx, aom_ctx);
|
2014-04-03 11:41:14 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
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());
|
2014-04-03 11:41:14 +04:00
|
|
|
|
|
|
|
get_first_cluster(webm_ctx);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-08-13 01:10:25 +03:00
|
|
|
int webm_read_frame(struct WebmInputContext *webm_ctx, uint8_t **buffer,
|
2014-04-03 11:41:14 +04:00
|
|
|
size_t *buffer_size) {
|
2015-03-30 22:58:26 +03:00
|
|
|
// 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;
|
|
|
|
}
|
2014-04-03 11:41:14 +04:00
|
|
|
mkvparser::Segment *const segment =
|
2016-08-13 01:10:25 +03:00
|
|
|
reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
|
|
|
|
const mkvparser::Cluster *cluster =
|
|
|
|
reinterpret_cast<const mkvparser::Cluster *>(webm_ctx->cluster);
|
2014-04-03 11:41:14 +04:00
|
|
|
const mkvparser::Block *block =
|
2016-08-13 01:10:25 +03:00
|
|
|
reinterpret_cast<const mkvparser::Block *>(webm_ctx->block);
|
2014-04-03 11:41:14 +04:00
|
|
|
const mkvparser::BlockEntry *block_entry =
|
2016-08-13 01:10:25 +03:00
|
|
|
reinterpret_cast<const mkvparser::BlockEntry *>(webm_ctx->block_entry);
|
2014-04-03 11:41:14 +04:00
|
|
|
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()) {
|
2016-04-25 23:46:42 +03:00
|
|
|
*buffer_size = 0;
|
2015-03-30 22:58:26 +03:00
|
|
|
webm_ctx->reached_eos = 1;
|
2014-04-03 11:41:14 +04:00
|
|
|
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;
|
|
|
|
}
|
2016-04-04 21:49:42 +03:00
|
|
|
if (status || block_entry == NULL) {
|
2014-04-03 11:41:14 +04:00
|
|
|
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;
|
|
|
|
|
2016-08-13 01:10:25 +03:00
|
|
|
const mkvparser::Block::Frame &frame =
|
2014-04-03 11:41:14 +04:00
|
|
|
block->GetFrame(webm_ctx->block_frame_index);
|
|
|
|
++webm_ctx->block_frame_index;
|
|
|
|
if (frame.len > static_cast<long>(*buffer_size)) {
|
2016-08-13 01:10:25 +03:00
|
|
|
delete[] * buffer;
|
2014-04-03 11:41:14 +04:00
|
|
|
*buffer = new uint8_t[frame.len];
|
|
|
|
if (*buffer == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
webm_ctx->buffer = *buffer;
|
|
|
|
}
|
2016-04-25 23:46:42 +03:00
|
|
|
*buffer_size = frame.len;
|
2014-04-03 11:41:14 +04:00
|
|
|
webm_ctx->timestamp_ns = block->GetTime(cluster);
|
2015-01-27 23:26:28 +03:00
|
|
|
webm_ctx->is_key_frame = block->IsKey();
|
2014-04-03 11:41:14 +04:00
|
|
|
|
|
|
|
mkvparser::MkvReader *const reader =
|
2016-08-13 01:10:25 +03:00
|
|
|
reinterpret_cast<mkvparser::MkvReader *>(webm_ctx->reader);
|
2014-04-03 11:41:14 +04:00
|
|
|
return frame.Read(reader, *buffer) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int webm_guess_framerate(struct WebmInputContext *webm_ctx,
|
2016-08-31 00:01:10 +03:00
|
|
|
struct AvxInputContext *aom_ctx) {
|
2014-04-03 11:41:14 +04:00
|
|
|
uint32_t i = 0;
|
|
|
|
uint8_t *buffer = NULL;
|
|
|
|
size_t buffer_size = 0;
|
|
|
|
while (webm_ctx->timestamp_ns < 1000000000 && i < 50) {
|
2016-04-25 23:46:42 +03:00
|
|
|
if (webm_read_frame(webm_ctx, &buffer, &buffer_size)) {
|
2014-04-03 11:41:14 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_ctx->framerate.numerator = (i - 1) * 1000000;
|
|
|
|
aom_ctx->framerate.denominator =
|
2014-04-03 11:41:14 +04:00
|
|
|
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;
|
2015-04-04 01:45:14 +03:00
|
|
|
webm_ctx->reached_eos = 0;
|
2014-04-03 11:41:14 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-13 01:10:25 +03:00
|
|
|
void webm_free(struct WebmInputContext *webm_ctx) { reset(webm_ctx); }
|