blob: f78f027000aa5192fcf8bacce1da418c2aa92a5b [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,
27 const struct aom_rational *fps,
clang-format01f4c712016-08-12 15:10:25 -070028 stereo_format_t stereo_fmt, unsigned int fourcc,
Yaowu Xuf883b422016-08-30 14:01:10 -070029 const struct AvxRational *par) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070030 mkvmuxer::MkvWriter *const writer = new mkvmuxer::MkvWriter(webm_ctx->stream);
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070031 mkvmuxer::Segment *const segment = new mkvmuxer::Segment();
32 segment->Init(writer);
33 segment->set_mode(mkvmuxer::Segment::kFile);
34 segment->OutputCues(true);
35
36 mkvmuxer::SegmentInfo *const info = segment->GetSegmentInfo();
37 const uint64_t kTimecodeScale = 1000000;
38 info->set_timecode_scale(kTimecodeScale);
Yaowu Xuf883b422016-08-30 14:01:10 -070039 std::string version = "aomenc";
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070040 if (!webm_ctx->debug) {
Yaowu Xuf883b422016-08-30 14:01:10 -070041 version.append(std::string(" ") + aom_codec_version_str());
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070042 }
43 info->set_writing_app(version.c_str());
44
45 const uint64_t video_track_id =
46 segment->AddVideoTrack(static_cast<int>(cfg->g_w),
clang-format01f4c712016-08-12 15:10:25 -070047 static_cast<int>(cfg->g_h), kVideoTrackNumber);
48 mkvmuxer::VideoTrack *const video_track = static_cast<mkvmuxer::VideoTrack *>(
49 segment->GetTrackByNumber(video_track_id));
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070050 video_track->SetStereoMode(stereo_fmt);
Yaowu Xu3d1bb972015-08-14 12:30:10 -070051 const char *codec_id;
52 switch (fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -070053 case AV1_FOURCC: codec_id = "V_AV1"; break;
54 default: codec_id = "V_AV1"; break;
Yaowu Xu3d1bb972015-08-14 12:30:10 -070055 }
56 video_track->set_codec_id(codec_id);
Frank Galligan09acd262015-06-01 10:20:58 -070057 if (par->numerator > 1 || par->denominator > 1) {
58 // TODO(fgalligan): Add support of DisplayUnit, Display Aspect Ratio type
59 // to WebM format.
clang-format01f4c712016-08-12 15:10:25 -070060 const uint64_t display_width = static_cast<uint64_t>(
61 ((cfg->g_w * par->numerator * 1.0) / par->denominator) + .5);
Frank Galligan09acd262015-06-01 10:20:58 -070062 video_track->set_display_width(display_width);
63 video_track->set_display_height(cfg->g_h);
64 }
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070065 if (webm_ctx->debug) {
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070066 video_track->set_uid(kDebugTrackUid);
67 }
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070068 webm_ctx->writer = writer;
69 webm_ctx->segment = segment;
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070070}
71
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070072void write_webm_block(struct WebmOutputContext *webm_ctx,
Yaowu Xuf883b422016-08-30 14:01:10 -070073 const aom_codec_enc_cfg_t *cfg,
74 const aom_codec_cx_pkt_t *pkt) {
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070075 mkvmuxer::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -070076 reinterpret_cast<mkvmuxer::Segment *>(webm_ctx->segment);
77 int64_t pts_ns = pkt->data.frame.pts * 1000000000ll * cfg->g_timebase.num /
78 cfg->g_timebase.den;
79 if (pts_ns <= webm_ctx->last_pts_ns) pts_ns = webm_ctx->last_pts_ns + 1000000;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070080 webm_ctx->last_pts_ns = pts_ns;
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070081
clang-format01f4c712016-08-12 15:10:25 -070082 segment->AddFrame(static_cast<uint8_t *>(pkt->data.frame.buf),
83 pkt->data.frame.sz, kVideoTrackNumber, pts_ns,
Yaowu Xuf883b422016-08-30 14:01:10 -070084 pkt->data.frame.flags & AOM_FRAME_IS_KEY);
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070085}
86
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070087void write_webm_file_footer(struct WebmOutputContext *webm_ctx) {
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070088 mkvmuxer::MkvWriter *const writer =
clang-format01f4c712016-08-12 15:10:25 -070089 reinterpret_cast<mkvmuxer::MkvWriter *>(webm_ctx->writer);
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070090 mkvmuxer::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -070091 reinterpret_cast<mkvmuxer::Segment *>(webm_ctx->segment);
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070092 segment->Finalize();
93 delete segment;
94 delete writer;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070095 webm_ctx->writer = NULL;
96 webm_ctx->segment = NULL;
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070097}