blob: 4f3054072da61f79c1e007a50ede33568e88d1f6 [file] [log] [blame]
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -07001/*
2 * Copyright (c) 2013 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "./webmdec.h"
12
13#include <cstring>
14#include <cstdio>
15
Tom Finegan4317ba52016-03-24 13:12:51 -070016#include "third_party/libwebm/mkvparser/mkvparser.h"
17#include "third_party/libwebm/mkvparser/mkvreader.h"
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070018
19namespace {
20
21void reset(struct WebmInputContext *const webm_ctx) {
22 if (webm_ctx->reader != NULL) {
23 mkvparser::MkvReader *const reader =
clang-format01f4c712016-08-12 15:10:25 -070024 reinterpret_cast<mkvparser::MkvReader *>(webm_ctx->reader);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070025 delete reader;
26 }
27 if (webm_ctx->segment != NULL) {
28 mkvparser::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -070029 reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070030 delete segment;
31 }
32 if (webm_ctx->buffer != NULL) {
33 delete[] webm_ctx->buffer;
34 }
35 webm_ctx->reader = NULL;
36 webm_ctx->segment = NULL;
37 webm_ctx->buffer = NULL;
38 webm_ctx->cluster = NULL;
39 webm_ctx->block_entry = NULL;
40 webm_ctx->block = NULL;
41 webm_ctx->block_frame_index = 0;
42 webm_ctx->video_track_index = 0;
43 webm_ctx->timestamp_ns = 0;
hkuangbe6aead2015-01-27 12:26:28 -080044 webm_ctx->is_key_frame = false;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070045}
46
47void get_first_cluster(struct WebmInputContext *const webm_ctx) {
48 mkvparser::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -070049 reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070050 const mkvparser::Cluster *const cluster = segment->GetFirst();
51 webm_ctx->cluster = cluster;
52}
53
54void rewind_and_reset(struct WebmInputContext *const webm_ctx,
55 struct VpxInputContext *const vpx_ctx) {
56 rewind(vpx_ctx->file);
57 reset(webm_ctx);
58}
59
60} // namespace
61
62int file_is_webm(struct WebmInputContext *webm_ctx,
63 struct VpxInputContext *vpx_ctx) {
64 mkvparser::MkvReader *const reader = new mkvparser::MkvReader(vpx_ctx->file);
65 webm_ctx->reader = reader;
Vignesh Venkatasubramanian1f05b192015-03-30 12:58:26 -070066 webm_ctx->reached_eos = 0;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070067
68 mkvparser::EBMLHeader header;
69 long long pos = 0;
70 if (header.Parse(reader, pos) < 0) {
71 rewind_and_reset(webm_ctx, vpx_ctx);
72 return 0;
73 }
74
clang-format01f4c712016-08-12 15:10:25 -070075 mkvparser::Segment *segment;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070076 if (mkvparser::Segment::CreateInstance(reader, pos, segment)) {
77 rewind_and_reset(webm_ctx, vpx_ctx);
78 return 0;
79 }
80 webm_ctx->segment = segment;
81 if (segment->Load() < 0) {
82 rewind_and_reset(webm_ctx, vpx_ctx);
83 return 0;
84 }
85
86 const mkvparser::Tracks *const tracks = segment->GetTracks();
clang-format01f4c712016-08-12 15:10:25 -070087 const mkvparser::VideoTrack *video_track = NULL;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070088 for (unsigned long i = 0; i < tracks->GetTracksCount(); ++i) {
clang-format01f4c712016-08-12 15:10:25 -070089 const mkvparser::Track *const track = tracks->GetTrackByIndex(i);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070090 if (track->GetType() == mkvparser::Track::kVideo) {
clang-format01f4c712016-08-12 15:10:25 -070091 video_track = static_cast<const mkvparser::VideoTrack *>(track);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070092 webm_ctx->video_track_index = track->GetNumber();
93 break;
94 }
95 }
96
Vignesh Venkatasubramanian09969ac2015-09-10 10:44:59 -070097 if (video_track == NULL || video_track->GetCodecId() == NULL) {
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -070098 rewind_and_reset(webm_ctx, vpx_ctx);
99 return 0;
100 }
101
102 if (!strncmp(video_track->GetCodecId(), "V_VP8", 5)) {
103 vpx_ctx->fourcc = VP8_FOURCC;
104 } else if (!strncmp(video_track->GetCodecId(), "V_VP9", 5)) {
105 vpx_ctx->fourcc = VP9_FOURCC;
Yaowu Xu3d1bb972015-08-14 12:30:10 -0700106 } else if (!strncmp(video_track->GetCodecId(), "V_VP10", 6)) {
107 vpx_ctx->fourcc = VP10_FOURCC;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700108 } else {
109 rewind_and_reset(webm_ctx, vpx_ctx);
110 return 0;
111 }
112
113 vpx_ctx->framerate.denominator = 0;
114 vpx_ctx->framerate.numerator = 0;
Vignesh Venkatasubramaniane39b9a62014-04-30 15:15:47 -0700115 vpx_ctx->width = static_cast<uint32_t>(video_track->GetWidth());
116 vpx_ctx->height = static_cast<uint32_t>(video_track->GetHeight());
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700117
118 get_first_cluster(webm_ctx);
119
120 return 1;
121}
122
clang-format01f4c712016-08-12 15:10:25 -0700123int webm_read_frame(struct WebmInputContext *webm_ctx, uint8_t **buffer,
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700124 size_t *buffer_size) {
Vignesh Venkatasubramanian1f05b192015-03-30 12:58:26 -0700125 // This check is needed for frame parallel decoding, in which case this
126 // function could be called even after it has reached end of input stream.
127 if (webm_ctx->reached_eos) {
128 return 1;
129 }
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700130 mkvparser::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -0700131 reinterpret_cast<mkvparser::Segment *>(webm_ctx->segment);
132 const mkvparser::Cluster *cluster =
133 reinterpret_cast<const mkvparser::Cluster *>(webm_ctx->cluster);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700134 const mkvparser::Block *block =
clang-format01f4c712016-08-12 15:10:25 -0700135 reinterpret_cast<const mkvparser::Block *>(webm_ctx->block);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700136 const mkvparser::BlockEntry *block_entry =
clang-format01f4c712016-08-12 15:10:25 -0700137 reinterpret_cast<const mkvparser::BlockEntry *>(webm_ctx->block_entry);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700138 bool block_entry_eos = false;
139 do {
140 long status = 0;
141 bool get_new_block = false;
142 if (block_entry == NULL && !block_entry_eos) {
143 status = cluster->GetFirst(block_entry);
144 get_new_block = true;
145 } else if (block_entry_eos || block_entry->EOS()) {
146 cluster = segment->GetNext(cluster);
147 if (cluster == NULL || cluster->EOS()) {
Vignesh Venkatasubramanianfa99c372016-04-25 13:46:42 -0700148 *buffer_size = 0;
Vignesh Venkatasubramanian1f05b192015-03-30 12:58:26 -0700149 webm_ctx->reached_eos = 1;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700150 return 1;
151 }
152 status = cluster->GetFirst(block_entry);
153 block_entry_eos = false;
154 get_new_block = true;
155 } else if (block == NULL ||
156 webm_ctx->block_frame_index == block->GetFrameCount() ||
157 block->GetTrackNumber() != webm_ctx->video_track_index) {
158 status = cluster->GetNext(block_entry, block_entry);
159 if (block_entry == NULL || block_entry->EOS()) {
160 block_entry_eos = true;
161 continue;
162 }
163 get_new_block = true;
164 }
Tom Finegan79d5aac2016-04-04 11:49:42 -0700165 if (status || block_entry == NULL) {
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700166 return -1;
167 }
168 if (get_new_block) {
169 block = block_entry->GetBlock();
170 webm_ctx->block_frame_index = 0;
171 }
172 } while (block->GetTrackNumber() != webm_ctx->video_track_index ||
173 block_entry_eos);
174
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];
185 if (*buffer == NULL) {
186 return -1;
187 }
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700188 webm_ctx->buffer = *buffer;
189 }
Vignesh Venkatasubramanianfa99c372016-04-25 13:46:42 -0700190 *buffer_size = frame.len;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700191 webm_ctx->timestamp_ns = block->GetTime(cluster);
hkuangbe6aead2015-01-27 12:26:28 -0800192 webm_ctx->is_key_frame = block->IsKey();
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700193
194 mkvparser::MkvReader *const reader =
clang-format01f4c712016-08-12 15:10:25 -0700195 reinterpret_cast<mkvparser::MkvReader *>(webm_ctx->reader);
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700196 return frame.Read(reader, *buffer) ? -1 : 0;
197}
198
199int webm_guess_framerate(struct WebmInputContext *webm_ctx,
200 struct VpxInputContext *vpx_ctx) {
201 uint32_t i = 0;
202 uint8_t *buffer = NULL;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700203 size_t buffer_size = 0;
204 while (webm_ctx->timestamp_ns < 1000000000 && i < 50) {
Vignesh Venkatasubramanianfa99c372016-04-25 13:46:42 -0700205 if (webm_read_frame(webm_ctx, &buffer, &buffer_size)) {
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700206 break;
207 }
208 ++i;
209 }
210 vpx_ctx->framerate.numerator = (i - 1) * 1000000;
211 vpx_ctx->framerate.denominator =
212 static_cast<int>(webm_ctx->timestamp_ns / 1000);
213 delete[] buffer;
214
215 get_first_cluster(webm_ctx);
216 webm_ctx->block = NULL;
217 webm_ctx->block_entry = NULL;
218 webm_ctx->block_frame_index = 0;
219 webm_ctx->timestamp_ns = 0;
Vignesh Venkatasubramanian866447a2015-04-03 15:45:14 -0700220 webm_ctx->reached_eos = 0;
Vignesh Venkatasubramaniandbd24712014-04-03 00:41:14 -0700221
222 return 0;
223}
224
clang-format01f4c712016-08-12 15:10:25 -0700225void webm_free(struct WebmInputContext *webm_ctx) { reset(webm_ctx); }