Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 4 | * 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. |
Johann | 123e8a6 | 2017-12-28 14:40:49 -0800 | [diff] [blame] | 10 | */ |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 11 | |
| 12 | #include "./webmdec.h" |
| 13 | |
| 14 | #include <cstring> |
| 15 | #include <cstdio> |
| 16 | |
Tom Finegan | 4317ba5 | 2016-03-24 13:12:51 -0700 | [diff] [blame] | 17 | #include "third_party/libwebm/mkvparser/mkvparser.h" |
| 18 | #include "third_party/libwebm/mkvparser/mkvreader.h" |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 19 | |
| 20 | namespace { |
| 21 | |
| 22 | void reset(struct WebmInputContext *const webm_ctx) { |
| 23 | if (webm_ctx->reader != NULL) { |
| 24 | mkvparser::MkvReader *const reader = |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 25 | reinterpret_cast<mkvparser::MkvReader *>(webm_ctx->reader); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 26 | delete reader; |
| 27 | } |
| 28 | if (webm_ctx->segment != NULL) { |
| 29 | mkvparser::Segment *const segment = |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 30 | reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 31 | 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; |
hkuang | be6aead | 2015-01-27 12:26:28 -0800 | [diff] [blame] | 45 | webm_ctx->is_key_frame = false; |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void get_first_cluster(struct WebmInputContext *const webm_ctx) { |
| 49 | mkvparser::Segment *const segment = |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 50 | reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 51 | const mkvparser::Cluster *const cluster = segment->GetFirst(); |
| 52 | webm_ctx->cluster = cluster; |
| 53 | } |
| 54 | |
| 55 | void rewind_and_reset(struct WebmInputContext *const webm_ctx, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 56 | struct AvxInputContext *const aom_ctx) { |
| 57 | rewind(aom_ctx->file); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 58 | reset(webm_ctx); |
| 59 | } |
| 60 | |
| 61 | } // namespace |
| 62 | |
| 63 | int file_is_webm(struct WebmInputContext *webm_ctx, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 64 | struct AvxInputContext *aom_ctx) { |
| 65 | mkvparser::MkvReader *const reader = new mkvparser::MkvReader(aom_ctx->file); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 66 | webm_ctx->reader = reader; |
Vignesh Venkatasubramanian | 1f05b19 | 2015-03-30 12:58:26 -0700 | [diff] [blame] | 67 | webm_ctx->reached_eos = 0; |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 68 | |
| 69 | mkvparser::EBMLHeader header; |
| 70 | long long pos = 0; |
| 71 | if (header.Parse(reader, pos) < 0) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 72 | rewind_and_reset(webm_ctx, aom_ctx); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 76 | mkvparser::Segment *segment; |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 77 | if (mkvparser::Segment::CreateInstance(reader, pos, segment)) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 78 | rewind_and_reset(webm_ctx, aom_ctx); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | webm_ctx->segment = segment; |
| 82 | if (segment->Load() < 0) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 83 | rewind_and_reset(webm_ctx, aom_ctx); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | const mkvparser::Tracks *const tracks = segment->GetTracks(); |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 88 | const mkvparser::VideoTrack *video_track = NULL; |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 89 | for (unsigned long i = 0; i < tracks->GetTracksCount(); ++i) { |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 90 | const mkvparser::Track *const track = tracks->GetTrackByIndex(i); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 91 | if (track->GetType() == mkvparser::Track::kVideo) { |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 92 | video_track = static_cast<const mkvparser::VideoTrack *>(track); |
James Zern | d92d603 | 2017-03-24 17:30:31 -0700 | [diff] [blame] | 93 | webm_ctx->video_track_index = static_cast<int>(track->GetNumber()); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 94 | break; |
| 95 | } |
| 96 | } |
| 97 | |
Vignesh Venkatasubramanian | 09969ac | 2015-09-10 10:44:59 -0700 | [diff] [blame] | 98 | if (video_track == NULL || video_track->GetCodecId() == NULL) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 99 | rewind_and_reset(webm_ctx, aom_ctx); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 103 | if (!strncmp(video_track->GetCodecId(), "V_AV1", 5)) { |
| 104 | aom_ctx->fourcc = AV1_FOURCC; |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 105 | } else { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 106 | rewind_and_reset(webm_ctx, aom_ctx); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 110 | 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 Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 114 | |
| 115 | get_first_cluster(webm_ctx); |
| 116 | |
| 117 | return 1; |
| 118 | } |
| 119 | |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 120 | int webm_read_frame(struct WebmInputContext *webm_ctx, uint8_t **buffer, |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 121 | size_t *buffer_size) { |
Vignesh Venkatasubramanian | 1f05b19 | 2015-03-30 12:58:26 -0700 | [diff] [blame] | 122 | // 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 Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 127 | mkvparser::Segment *const segment = |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 128 | reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment); |
| 129 | const mkvparser::Cluster *cluster = |
| 130 | reinterpret_cast<const mkvparser::Cluster *>(webm_ctx->cluster); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 131 | const mkvparser::Block *block = |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 132 | reinterpret_cast<const mkvparser::Block *>(webm_ctx->block); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 133 | const mkvparser::BlockEntry *block_entry = |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 134 | reinterpret_cast<const mkvparser::BlockEntry *>(webm_ctx->block_entry); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 135 | 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 Venkatasubramanian | fa99c37 | 2016-04-25 13:46:42 -0700 | [diff] [blame] | 145 | *buffer_size = 0; |
Vignesh Venkatasubramanian | 1f05b19 | 2015-03-30 12:58:26 -0700 | [diff] [blame] | 146 | webm_ctx->reached_eos = 1; |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 147 | 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 Finegan | 79d5aac | 2016-04-04 11:49:42 -0700 | [diff] [blame] | 162 | if (status || block_entry == NULL) { |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 163 | return -1; |
| 164 | } |
| 165 | if (get_new_block) { |
| 166 | block = block_entry->GetBlock(); |
James Zern | 7fe9ce5 | 2017-04-22 13:11:16 -0700 | [diff] [blame] | 167 | if (block == NULL) return -1; |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 168 | webm_ctx->block_frame_index = 0; |
| 169 | } |
James Zern | 7fe9ce5 | 2017-04-22 13:11:16 -0700 | [diff] [blame] | 170 | } while (block_entry_eos || |
| 171 | block->GetTrackNumber() != webm_ctx->video_track_index); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 172 | |
| 173 | webm_ctx->cluster = cluster; |
| 174 | webm_ctx->block_entry = block_entry; |
| 175 | webm_ctx->block = block; |
| 176 | |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 177 | const mkvparser::Block::Frame &frame = |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 178 | block->GetFrame(webm_ctx->block_frame_index); |
| 179 | ++webm_ctx->block_frame_index; |
| 180 | if (frame.len > static_cast<long>(*buffer_size)) { |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 181 | delete[] * buffer; |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 182 | *buffer = new uint8_t[frame.len]; |
| 183 | if (*buffer == NULL) { |
| 184 | return -1; |
| 185 | } |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 186 | webm_ctx->buffer = *buffer; |
| 187 | } |
Vignesh Venkatasubramanian | fa99c37 | 2016-04-25 13:46:42 -0700 | [diff] [blame] | 188 | *buffer_size = frame.len; |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 189 | webm_ctx->timestamp_ns = block->GetTime(cluster); |
hkuang | be6aead | 2015-01-27 12:26:28 -0800 | [diff] [blame] | 190 | webm_ctx->is_key_frame = block->IsKey(); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 191 | |
| 192 | mkvparser::MkvReader *const reader = |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 193 | reinterpret_cast<mkvparser::MkvReader *>(webm_ctx->reader); |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 194 | return frame.Read(reader, *buffer) ? -1 : 0; |
| 195 | } |
| 196 | |
| 197 | int webm_guess_framerate(struct WebmInputContext *webm_ctx, |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 198 | struct AvxInputContext *aom_ctx) { |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 199 | uint32_t i = 0; |
| 200 | uint8_t *buffer = NULL; |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 201 | size_t buffer_size = 0; |
| 202 | while (webm_ctx->timestamp_ns < 1000000000 && i < 50) { |
Vignesh Venkatasubramanian | fa99c37 | 2016-04-25 13:46:42 -0700 | [diff] [blame] | 203 | if (webm_read_frame(webm_ctx, &buffer, &buffer_size)) { |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | ++i; |
| 207 | } |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 208 | aom_ctx->framerate.numerator = (i - 1) * 1000000; |
| 209 | aom_ctx->framerate.denominator = |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 210 | 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 Venkatasubramanian | 866447a | 2015-04-03 15:45:14 -0700 | [diff] [blame] | 218 | webm_ctx->reached_eos = 0; |
Vignesh Venkatasubramanian | dbd2471 | 2014-04-03 00:41:14 -0700 | [diff] [blame] | 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
clang-format | 01f4c71 | 2016-08-12 15:10:25 -0700 | [diff] [blame] | 223 | void webm_free(struct WebmInputContext *webm_ctx) { reset(webm_ctx); } |