blob: 2a953aeb7f0a7a6295be4953ba8b515a48b4f9b1 [file] [log] [blame]
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -07001/*
James Zernb7c05bd2024-06-11 19:15:10 -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
Tom Finegandd3e2a52018-05-23 14:33:09 -070012#include "common/webmdec.h"
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070013
Wan-Teh Changfba482a2018-05-19 14:57:43 -070014#include <cassert>
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070015#include <cstring>
16#include <cstdio>
17
Tom Finegan4317ba52016-03-24 13:12:51 -070018#include "third_party/libwebm/mkvparser/mkvparser.h"
19#include "third_party/libwebm/mkvparser/mkvreader.h"
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070020
21namespace {
22
23void reset(struct WebmInputContext *const webm_ctx) {
24 if (webm_ctx->reader != NULL) {
25 mkvparser::MkvReader *const reader =
clang-format01f4c712016-08-12 15:10:25 -070026 reinterpret_cast<mkvparser::MkvReader *>(webm_ctx->reader);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070027 delete reader;
28 }
29 if (webm_ctx->segment != NULL) {
30 mkvparser::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -070031 reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070032 delete segment;
33 }
34 if (webm_ctx->buffer != NULL) {
35 delete[] webm_ctx->buffer;
36 }
37 webm_ctx->reader = NULL;
38 webm_ctx->segment = NULL;
39 webm_ctx->buffer = NULL;
40 webm_ctx->cluster = NULL;
41 webm_ctx->block_entry = NULL;
42 webm_ctx->block = NULL;
43 webm_ctx->block_frame_index = 0;
44 webm_ctx->video_track_index = 0;
45 webm_ctx->timestamp_ns = 0;
hkuangbe6aead2015-01-27 12:26:28 -080046 webm_ctx->is_key_frame = false;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070047}
48
49void get_first_cluster(struct WebmInputContext *const webm_ctx) {
50 mkvparser::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -070051 reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070052 const mkvparser::Cluster *const cluster = segment->GetFirst();
53 webm_ctx->cluster = cluster;
54}
55
56void rewind_and_reset(struct WebmInputContext *const webm_ctx,
Yaowu Xuf883b422016-08-30 14:01:10 -070057 struct AvxInputContext *const aom_ctx) {
58 rewind(aom_ctx->file);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070059 reset(webm_ctx);
60}
61
62} // namespace
63
64int file_is_webm(struct WebmInputContext *webm_ctx,
Yaowu Xuf883b422016-08-30 14:01:10 -070065 struct AvxInputContext *aom_ctx) {
66 mkvparser::MkvReader *const reader = new mkvparser::MkvReader(aom_ctx->file);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070067 webm_ctx->reader = reader;
Vignesh Venkatasubramanian1f05b192015-03-30 12:58:26 -070068 webm_ctx->reached_eos = 0;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070069
70 mkvparser::EBMLHeader header;
71 long long pos = 0;
72 if (header.Parse(reader, pos) < 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -070073 rewind_and_reset(webm_ctx, aom_ctx);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070074 return 0;
75 }
76
clang-format01f4c712016-08-12 15:10:25 -070077 mkvparser::Segment *segment;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070078 if (mkvparser::Segment::CreateInstance(reader, pos, segment)) {
Yaowu Xuf883b422016-08-30 14:01:10 -070079 rewind_and_reset(webm_ctx, aom_ctx);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070080 return 0;
81 }
82 webm_ctx->segment = segment;
83 if (segment->Load() < 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -070084 rewind_and_reset(webm_ctx, aom_ctx);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070085 return 0;
86 }
87
88 const mkvparser::Tracks *const tracks = segment->GetTracks();
clang-format01f4c712016-08-12 15:10:25 -070089 const mkvparser::VideoTrack *video_track = NULL;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070090 for (unsigned long i = 0; i < tracks->GetTracksCount(); ++i) {
clang-format01f4c712016-08-12 15:10:25 -070091 const mkvparser::Track *const track = tracks->GetTrackByIndex(i);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070092 if (track->GetType() == mkvparser::Track::kVideo) {
clang-format01f4c712016-08-12 15:10:25 -070093 video_track = static_cast<const mkvparser::VideoTrack *>(track);
James Zernd92d6032017-03-24 17:30:31 -070094 webm_ctx->video_track_index = static_cast<int>(track->GetNumber());
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070095 break;
96 }
97 }
98
Vignesh Venkatasubramanian09969ac2015-09-10 10:44:59 -070099 if (video_track == NULL || video_track->GetCodecId() == NULL) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700100 rewind_and_reset(webm_ctx, aom_ctx);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700101 return 0;
102 }
103
Yaowu Xuf883b422016-08-30 14:01:10 -0700104 if (!strncmp(video_track->GetCodecId(), "V_AV1", 5)) {
105 aom_ctx->fourcc = AV1_FOURCC;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700106 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700107 rewind_and_reset(webm_ctx, aom_ctx);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700108 return 0;
109 }
110
Yaowu Xuf883b422016-08-30 14:01:10 -0700111 aom_ctx->framerate.denominator = 0;
112 aom_ctx->framerate.numerator = 0;
113 aom_ctx->width = static_cast<uint32_t>(video_track->GetWidth());
114 aom_ctx->height = static_cast<uint32_t>(video_track->GetHeight());
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700115
116 get_first_cluster(webm_ctx);
117
118 return 1;
119}
120
clang-format01f4c712016-08-12 15:10:25 -0700121int webm_read_frame(struct WebmInputContext *webm_ctx, uint8_t **buffer,
Tom Finegan707b5752018-03-29 15:41:08 -0700122 size_t *bytes_read, size_t *buffer_size) {
Wan-Teh Changfba482a2018-05-19 14:57:43 -0700123 assert(webm_ctx->buffer == *buffer);
Vignesh Venkatasubramanian1f05b192015-03-30 12:58:26 -0700124 // This check is needed for frame parallel decoding, in which case this
125 // function could be called even after it has reached end of input stream.
126 if (webm_ctx->reached_eos) {
127 return 1;
128 }
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700129 mkvparser::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -0700130 reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
131 const mkvparser::Cluster *cluster =
132 reinterpret_cast<const mkvparser::Cluster *>(webm_ctx->cluster);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700133 const mkvparser::Block *block =
clang-format01f4c712016-08-12 15:10:25 -0700134 reinterpret_cast<const mkvparser::Block *>(webm_ctx->block);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700135 const mkvparser::BlockEntry *block_entry =
clang-format01f4c712016-08-12 15:10:25 -0700136 reinterpret_cast<const mkvparser::BlockEntry *>(webm_ctx->block_entry);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700137 bool block_entry_eos = false;
138 do {
139 long status = 0;
140 bool get_new_block = false;
141 if (block_entry == NULL && !block_entry_eos) {
142 status = cluster->GetFirst(block_entry);
143 get_new_block = true;
144 } else if (block_entry_eos || block_entry->EOS()) {
145 cluster = segment->GetNext(cluster);
146 if (cluster == NULL || cluster->EOS()) {
Tom Finegan707b5752018-03-29 15:41:08 -0700147 *bytes_read = 0;
Vignesh Venkatasubramanian1f05b192015-03-30 12:58:26 -0700148 webm_ctx->reached_eos = 1;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700149 return 1;
150 }
151 status = cluster->GetFirst(block_entry);
152 block_entry_eos = false;
153 get_new_block = true;
154 } else if (block == NULL ||
155 webm_ctx->block_frame_index == block->GetFrameCount() ||
156 block->GetTrackNumber() != webm_ctx->video_track_index) {
157 status = cluster->GetNext(block_entry, block_entry);
158 if (block_entry == NULL || block_entry->EOS()) {
159 block_entry_eos = true;
160 continue;
161 }
162 get_new_block = true;
163 }
Tom Finegan79d5aac2016-04-04 11:49:42 -0700164 if (status || block_entry == NULL) {
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700165 return -1;
166 }
167 if (get_new_block) {
168 block = block_entry->GetBlock();
James Zern7fe9ce52017-04-22 13:11:16 -0700169 if (block == NULL) return -1;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700170 webm_ctx->block_frame_index = 0;
171 }
James Zern7fe9ce52017-04-22 13:11:16 -0700172 } while (block_entry_eos ||
173 block->GetTrackNumber() != webm_ctx->video_track_index);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700174
175 webm_ctx->cluster = cluster;
176 webm_ctx->block_entry = block_entry;
177 webm_ctx->block = block;
178
clang-format01f4c712016-08-12 15:10:25 -0700179 const mkvparser::Block::Frame &frame =
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700180 block->GetFrame(webm_ctx->block_frame_index);
181 ++webm_ctx->block_frame_index;
182 if (frame.len > static_cast<long>(*buffer_size)) {
clang-format01f4c712016-08-12 15:10:25 -0700183 delete[] * buffer;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700184 *buffer = new uint8_t[frame.len];
Wan-Teh Changfba482a2018-05-19 14:57:43 -0700185 webm_ctx->buffer = *buffer;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700186 if (*buffer == NULL) {
187 return -1;
188 }
Tom Finegan707b5752018-03-29 15:41:08 -0700189 *buffer_size = frame.len;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700190 }
Tom Finegan707b5752018-03-29 15:41:08 -0700191 *bytes_read = frame.len;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700192 webm_ctx->timestamp_ns = block->GetTime(cluster);
hkuangbe6aead2015-01-27 12:26:28 -0800193 webm_ctx->is_key_frame = block->IsKey();
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700194
195 mkvparser::MkvReader *const reader =
clang-format01f4c712016-08-12 15:10:25 -0700196 reinterpret_cast<mkvparser::MkvReader *>(webm_ctx->reader);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700197 return frame.Read(reader, *buffer) ? -1 : 0;
198}
199
Elliott Karpilovsky19af1452019-10-28 13:48:50 -0700200// Calculate the greatest common divisor between two numbers.
201static int gcd(int a, int b) {
202 int remainder;
203 while (b > 0) {
204 remainder = a % b;
205 a = b;
206 b = remainder;
207 }
208 return a;
209}
210
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700211int webm_guess_framerate(struct WebmInputContext *webm_ctx,
Yaowu Xuf883b422016-08-30 14:01:10 -0700212 struct AvxInputContext *aom_ctx) {
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700213 uint32_t i = 0;
214 uint8_t *buffer = NULL;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700215 size_t buffer_size = 0;
Tom Finegan707b5752018-03-29 15:41:08 -0700216 size_t bytes_read = 0;
Wan-Teh Changfba482a2018-05-19 14:57:43 -0700217 assert(webm_ctx->buffer == NULL);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700218 while (webm_ctx->timestamp_ns < 1000000000 && i < 50) {
Tom Finegan707b5752018-03-29 15:41:08 -0700219 if (webm_read_frame(webm_ctx, &buffer, &bytes_read, &buffer_size)) {
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700220 break;
221 }
222 ++i;
223 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700224 aom_ctx->framerate.numerator = (i - 1) * 1000000;
225 aom_ctx->framerate.denominator =
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700226 static_cast<int>(webm_ctx->timestamp_ns / 1000);
Elliott Karpilovsky19af1452019-10-28 13:48:50 -0700227 // Fraction might be represented in large numbers, like 49000000/980000
228 // for 50fps. Simplify as much as possible.
229 int g = gcd(aom_ctx->framerate.numerator, aom_ctx->framerate.denominator);
230 if (g != 0) {
231 aom_ctx->framerate.numerator /= g;
232 aom_ctx->framerate.denominator /= g;
233 }
234
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700235 delete[] buffer;
Wan-Teh Changfba482a2018-05-19 14:57:43 -0700236 webm_ctx->buffer = NULL;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700237
238 get_first_cluster(webm_ctx);
239 webm_ctx->block = NULL;
240 webm_ctx->block_entry = NULL;
241 webm_ctx->block_frame_index = 0;
242 webm_ctx->timestamp_ns = 0;
Vignesh Venkatasubramanian866447a2015-04-03 15:45:14 -0700243 webm_ctx->reached_eos = 0;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700244
245 return 0;
246}
247
clang-format01f4c712016-08-12 15:10:25 -0700248void webm_free(struct WebmInputContext *webm_ctx) { reset(webm_ctx); }