blob: e3d209a27622ea0f5369ea373ad1d34a219d1ea0 [file] [log] [blame]
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -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.
10*/
11
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070012#include "./webmenc.h"
13
14#include <string>
15
Tom Finegan4317ba52016-03-24 13:12:51 -070016#include "third_party/libwebm/mkvmuxer/mkvmuxer.h"
17#include "third_party/libwebm/mkvmuxer/mkvmuxerutil.h"
18#include "third_party/libwebm/mkvmuxer/mkvwriter.h"
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070019
20namespace {
21const uint64_t kDebugTrackUid = 0xDEADBEEF;
22const int kVideoTrackNumber = 1;
23} // namespace
24
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070025void write_webm_file_header(struct WebmOutputContext *webm_ctx,
Yaowu Xuf883b422016-08-30 14:01:10 -070026 const aom_codec_enc_cfg_t *cfg,
clang-format01f4c712016-08-12 15:10:25 -070027 stereo_format_t stereo_fmt, unsigned int fourcc,
Yaowu Xuf883b422016-08-30 14:01:10 -070028 const struct AvxRational *par) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070029 mkvmuxer::MkvWriter *const writer = new mkvmuxer::MkvWriter(webm_ctx->stream);
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070030 mkvmuxer::Segment *const segment = new mkvmuxer::Segment();
31 segment->Init(writer);
32 segment->set_mode(mkvmuxer::Segment::kFile);
33 segment->OutputCues(true);
34
35 mkvmuxer::SegmentInfo *const info = segment->GetSegmentInfo();
36 const uint64_t kTimecodeScale = 1000000;
37 info->set_timecode_scale(kTimecodeScale);
Yaowu Xuf883b422016-08-30 14:01:10 -070038 std::string version = "aomenc";
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070039 if (!webm_ctx->debug) {
Yaowu Xuf883b422016-08-30 14:01:10 -070040 version.append(std::string(" ") + aom_codec_version_str());
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070041 }
42 info->set_writing_app(version.c_str());
43
44 const uint64_t video_track_id =
45 segment->AddVideoTrack(static_cast<int>(cfg->g_w),
clang-format01f4c712016-08-12 15:10:25 -070046 static_cast<int>(cfg->g_h), kVideoTrackNumber);
47 mkvmuxer::VideoTrack *const video_track = static_cast<mkvmuxer::VideoTrack *>(
48 segment->GetTrackByNumber(video_track_id));
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070049 video_track->SetStereoMode(stereo_fmt);
Yaowu Xu3d1bb972015-08-14 12:30:10 -070050 const char *codec_id;
51 switch (fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -070052 case AV1_FOURCC: codec_id = "V_AV1"; break;
53 default: codec_id = "V_AV1"; break;
Yaowu Xu3d1bb972015-08-14 12:30:10 -070054 }
55 video_track->set_codec_id(codec_id);
Frank Galligan09acd262015-06-01 10:20:58 -070056 if (par->numerator > 1 || par->denominator > 1) {
57 // TODO(fgalligan): Add support of DisplayUnit, Display Aspect Ratio type
58 // to WebM format.
clang-format01f4c712016-08-12 15:10:25 -070059 const uint64_t display_width = static_cast<uint64_t>(
60 ((cfg->g_w * par->numerator * 1.0) / par->denominator) + .5);
Frank Galligan09acd262015-06-01 10:20:58 -070061 video_track->set_display_width(display_width);
62 video_track->set_display_height(cfg->g_h);
63 }
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070064 if (webm_ctx->debug) {
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070065 video_track->set_uid(kDebugTrackUid);
66 }
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070067 webm_ctx->writer = writer;
68 webm_ctx->segment = segment;
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070069}
70
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070071void write_webm_block(struct WebmOutputContext *webm_ctx,
Yaowu Xuf883b422016-08-30 14:01:10 -070072 const aom_codec_enc_cfg_t *cfg,
73 const aom_codec_cx_pkt_t *pkt) {
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070074 mkvmuxer::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -070075 reinterpret_cast<mkvmuxer::Segment *>(webm_ctx->segment);
76 int64_t pts_ns = pkt->data.frame.pts * 1000000000ll * cfg->g_timebase.num /
77 cfg->g_timebase.den;
78 if (pts_ns <= webm_ctx->last_pts_ns) pts_ns = webm_ctx->last_pts_ns + 1000000;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070079 webm_ctx->last_pts_ns = pts_ns;
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070080
clang-format01f4c712016-08-12 15:10:25 -070081 segment->AddFrame(static_cast<uint8_t *>(pkt->data.frame.buf),
82 pkt->data.frame.sz, kVideoTrackNumber, pts_ns,
Yaowu Xuf883b422016-08-30 14:01:10 -070083 pkt->data.frame.flags & AOM_FRAME_IS_KEY);
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070084}
85
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070086void write_webm_file_footer(struct WebmOutputContext *webm_ctx) {
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070087 mkvmuxer::MkvWriter *const writer =
clang-format01f4c712016-08-12 15:10:25 -070088 reinterpret_cast<mkvmuxer::MkvWriter *>(webm_ctx->writer);
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070089 mkvmuxer::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -070090 reinterpret_cast<mkvmuxer::Segment *>(webm_ctx->segment);
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070091 segment->Finalize();
92 delete segment;
93 delete writer;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070094 webm_ctx->writer = NULL;
95 webm_ctx->segment = NULL;
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070096}