blob: da44940be8b3e72b4fcfb8c4f73aa6848c579747 [file] [log] [blame]
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Johann123e8a62017-12-28 14:40:49 -080010 */
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070011
12#include "./webmdec.h"
13
14#include <cstring>
15#include <cstdio>
16
Tom Finegan4317ba52016-03-24 13:12:51 -070017#include "third_party/libwebm/mkvparser/mkvparser.h"
18#include "third_party/libwebm/mkvparser/mkvreader.h"
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070019
20namespace {
21
22void reset(struct WebmInputContext *const webm_ctx) {
23 if (webm_ctx->reader != NULL) {
24 mkvparser::MkvReader *const reader =
clang-format01f4c712016-08-12 15:10:25 -070025 reinterpret_cast<mkvparser::MkvReader *>(webm_ctx->reader);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070026 delete reader;
27 }
28 if (webm_ctx->segment != NULL) {
29 mkvparser::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -070030 reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070031 delete segment;
32 }
33 if (webm_ctx->buffer != NULL) {
34 delete[] webm_ctx->buffer;
35 }
36 webm_ctx->reader = NULL;
37 webm_ctx->segment = NULL;
38 webm_ctx->buffer = NULL;
39 webm_ctx->cluster = NULL;
40 webm_ctx->block_entry = NULL;
41 webm_ctx->block = NULL;
42 webm_ctx->block_frame_index = 0;
43 webm_ctx->video_track_index = 0;
44 webm_ctx->timestamp_ns = 0;
hkuangbe6aead2015-01-27 12:26:28 -080045 webm_ctx->is_key_frame = false;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070046}
47
48void get_first_cluster(struct WebmInputContext *const webm_ctx) {
49 mkvparser::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -070050 reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070051 const mkvparser::Cluster *const cluster = segment->GetFirst();
52 webm_ctx->cluster = cluster;
53}
54
55void rewind_and_reset(struct WebmInputContext *const webm_ctx,
Yaowu Xuf883b422016-08-30 14:01:10 -070056 struct AvxInputContext *const aom_ctx) {
57 rewind(aom_ctx->file);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070058 reset(webm_ctx);
59}
60
61} // namespace
62
63int file_is_webm(struct WebmInputContext *webm_ctx,
Yaowu Xuf883b422016-08-30 14:01:10 -070064 struct AvxInputContext *aom_ctx) {
65 mkvparser::MkvReader *const reader = new mkvparser::MkvReader(aom_ctx->file);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070066 webm_ctx->reader = reader;
Vignesh Venkatasubramanian1f05b192015-03-30 12:58:26 -070067 webm_ctx->reached_eos = 0;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070068
69 mkvparser::EBMLHeader header;
70 long long pos = 0;
71 if (header.Parse(reader, pos) < 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -070072 rewind_and_reset(webm_ctx, aom_ctx);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070073 return 0;
74 }
75
clang-format01f4c712016-08-12 15:10:25 -070076 mkvparser::Segment *segment;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070077 if (mkvparser::Segment::CreateInstance(reader, pos, segment)) {
Yaowu Xuf883b422016-08-30 14:01:10 -070078 rewind_and_reset(webm_ctx, aom_ctx);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070079 return 0;
80 }
81 webm_ctx->segment = segment;
82 if (segment->Load() < 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -070083 rewind_and_reset(webm_ctx, aom_ctx);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070084 return 0;
85 }
86
87 const mkvparser::Tracks *const tracks = segment->GetTracks();
clang-format01f4c712016-08-12 15:10:25 -070088 const mkvparser::VideoTrack *video_track = NULL;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070089 for (unsigned long i = 0; i < tracks->GetTracksCount(); ++i) {
clang-format01f4c712016-08-12 15:10:25 -070090 const mkvparser::Track *const track = tracks->GetTrackByIndex(i);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070091 if (track->GetType() == mkvparser::Track::kVideo) {
clang-format01f4c712016-08-12 15:10:25 -070092 video_track = static_cast<const mkvparser::VideoTrack *>(track);
James Zernd92d6032017-03-24 17:30:31 -070093 webm_ctx->video_track_index = static_cast<int>(track->GetNumber());
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070094 break;
95 }
96 }
97
Vignesh Venkatasubramanian09969ac2015-09-10 10:44:59 -070098 if (video_track == NULL || video_track->GetCodecId() == NULL) {
Yaowu Xuf883b422016-08-30 14:01:10 -070099 rewind_and_reset(webm_ctx, aom_ctx);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700100 return 0;
101 }
102
Yaowu Xuf883b422016-08-30 14:01:10 -0700103 if (!strncmp(video_track->GetCodecId(), "V_AV1", 5)) {
104 aom_ctx->fourcc = AV1_FOURCC;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700105 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700106 rewind_and_reset(webm_ctx, aom_ctx);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700107 return 0;
108 }
109
Yaowu Xuf883b422016-08-30 14:01:10 -0700110 aom_ctx->framerate.denominator = 0;
111 aom_ctx->framerate.numerator = 0;
112 aom_ctx->width = static_cast<uint32_t>(video_track->GetWidth());
113 aom_ctx->height = static_cast<uint32_t>(video_track->GetHeight());
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700114
115 get_first_cluster(webm_ctx);
116
117 return 1;
118}
119
clang-format01f4c712016-08-12 15:10:25 -0700120int webm_read_frame(struct WebmInputContext *webm_ctx, uint8_t **buffer,
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700121 size_t *buffer_size) {
Vignesh Venkatasubramanian1f05b192015-03-30 12:58:26 -0700122 // This check is needed for frame parallel decoding, in which case this
123 // function could be called even after it has reached end of input stream.
124 if (webm_ctx->reached_eos) {
125 return 1;
126 }
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700127 mkvparser::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -0700128 reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
129 const mkvparser::Cluster *cluster =
130 reinterpret_cast<const mkvparser::Cluster *>(webm_ctx->cluster);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700131 const mkvparser::Block *block =
clang-format01f4c712016-08-12 15:10:25 -0700132 reinterpret_cast<const mkvparser::Block *>(webm_ctx->block);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700133 const mkvparser::BlockEntry *block_entry =
clang-format01f4c712016-08-12 15:10:25 -0700134 reinterpret_cast<const mkvparser::BlockEntry *>(webm_ctx->block_entry);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700135 bool block_entry_eos = false;
136 do {
137 long status = 0;
138 bool get_new_block = false;
139 if (block_entry == NULL && !block_entry_eos) {
140 status = cluster->GetFirst(block_entry);
141 get_new_block = true;
142 } else if (block_entry_eos || block_entry->EOS()) {
143 cluster = segment->GetNext(cluster);
144 if (cluster == NULL || cluster->EOS()) {
Vignesh Venkatasubramanianfa99c372016-04-25 13:46:42 -0700145 *buffer_size = 0;
Vignesh Venkatasubramanian1f05b192015-03-30 12:58:26 -0700146 webm_ctx->reached_eos = 1;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700147 return 1;
148 }
149 status = cluster->GetFirst(block_entry);
150 block_entry_eos = false;
151 get_new_block = true;
152 } else if (block == NULL ||
153 webm_ctx->block_frame_index == block->GetFrameCount() ||
154 block->GetTrackNumber() != webm_ctx->video_track_index) {
155 status = cluster->GetNext(block_entry, block_entry);
156 if (block_entry == NULL || block_entry->EOS()) {
157 block_entry_eos = true;
158 continue;
159 }
160 get_new_block = true;
161 }
Tom Finegan79d5aac2016-04-04 11:49:42 -0700162 if (status || block_entry == NULL) {
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700163 return -1;
164 }
165 if (get_new_block) {
166 block = block_entry->GetBlock();
James Zern7fe9ce52017-04-22 13:11:16 -0700167 if (block == NULL) return -1;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700168 webm_ctx->block_frame_index = 0;
169 }
James Zern7fe9ce52017-04-22 13:11:16 -0700170 } while (block_entry_eos ||
171 block->GetTrackNumber() != webm_ctx->video_track_index);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700172
173 webm_ctx->cluster = cluster;
174 webm_ctx->block_entry = block_entry;
175 webm_ctx->block = block;
176
clang-format01f4c712016-08-12 15:10:25 -0700177 const mkvparser::Block::Frame &frame =
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700178 block->GetFrame(webm_ctx->block_frame_index);
179 ++webm_ctx->block_frame_index;
180 if (frame.len > static_cast<long>(*buffer_size)) {
clang-format01f4c712016-08-12 15:10:25 -0700181 delete[] * buffer;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700182 *buffer = new uint8_t[frame.len];
183 if (*buffer == NULL) {
184 return -1;
185 }
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700186 webm_ctx->buffer = *buffer;
187 }
Vignesh Venkatasubramanianfa99c372016-04-25 13:46:42 -0700188 *buffer_size = frame.len;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700189 webm_ctx->timestamp_ns = block->GetTime(cluster);
hkuangbe6aead2015-01-27 12:26:28 -0800190 webm_ctx->is_key_frame = block->IsKey();
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700191
192 mkvparser::MkvReader *const reader =
clang-format01f4c712016-08-12 15:10:25 -0700193 reinterpret_cast<mkvparser::MkvReader *>(webm_ctx->reader);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700194 return frame.Read(reader, *buffer) ? -1 : 0;
195}
196
197int webm_guess_framerate(struct WebmInputContext *webm_ctx,
Yaowu Xuf883b422016-08-30 14:01:10 -0700198 struct AvxInputContext *aom_ctx) {
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700199 uint32_t i = 0;
200 uint8_t *buffer = NULL;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700201 size_t buffer_size = 0;
202 while (webm_ctx->timestamp_ns < 1000000000 && i < 50) {
Vignesh Venkatasubramanianfa99c372016-04-25 13:46:42 -0700203 if (webm_read_frame(webm_ctx, &buffer, &buffer_size)) {
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700204 break;
205 }
206 ++i;
207 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700208 aom_ctx->framerate.numerator = (i - 1) * 1000000;
209 aom_ctx->framerate.denominator =
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700210 static_cast<int>(webm_ctx->timestamp_ns / 1000);
211 delete[] buffer;
212
213 get_first_cluster(webm_ctx);
214 webm_ctx->block = NULL;
215 webm_ctx->block_entry = NULL;
216 webm_ctx->block_frame_index = 0;
217 webm_ctx->timestamp_ns = 0;
Vignesh Venkatasubramanian866447a2015-04-03 15:45:14 -0700218 webm_ctx->reached_eos = 0;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700219
220 return 0;
221}
222
clang-format01f4c712016-08-12 15:10:25 -0700223void webm_free(struct WebmInputContext *webm_ctx) { reset(webm_ctx); }