blob: 02f9850fed7a4e1c6f853f5251bf5a7078f7b4ba [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.
Johann123e8a62017-12-28 14:40:49 -080010 */
Yaowu Xu9c01aa12016-09-01 14:32:49 -070011
Tom Finegandd3e2a52018-05-23 14:33:09 -070012#include "common/webmenc.h"
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070013
14#include <string>
15
Tom Fineganec0833f2018-09-19 14:28:20 -070016#include "common/av1_config.h"
Tom Finegan4317ba52016-03-24 13:12:51 -070017#include "third_party/libwebm/mkvmuxer/mkvmuxer.h"
18#include "third_party/libwebm/mkvmuxer/mkvmuxerutil.h"
19#include "third_party/libwebm/mkvmuxer/mkvwriter.h"
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070020
21namespace {
22const uint64_t kDebugTrackUid = 0xDEADBEEF;
23const int kVideoTrackNumber = 1;
24} // namespace
25
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070026void write_webm_file_header(struct WebmOutputContext *webm_ctx,
Tom Fineganec0833f2018-09-19 14:28:20 -070027 aom_codec_ctx_t *encoder_ctx,
Yaowu Xuf883b422016-08-30 14:01:10 -070028 const aom_codec_enc_cfg_t *cfg,
clang-format01f4c712016-08-12 15:10:25 -070029 stereo_format_t stereo_fmt, unsigned int fourcc,
Yaowu Xuf883b422016-08-30 14:01:10 -070030 const struct AvxRational *par) {
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070031 mkvmuxer::MkvWriter *const writer = new mkvmuxer::MkvWriter(webm_ctx->stream);
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070032 mkvmuxer::Segment *const segment = new mkvmuxer::Segment();
33 segment->Init(writer);
34 segment->set_mode(mkvmuxer::Segment::kFile);
35 segment->OutputCues(true);
36
37 mkvmuxer::SegmentInfo *const info = segment->GetSegmentInfo();
38 const uint64_t kTimecodeScale = 1000000;
39 info->set_timecode_scale(kTimecodeScale);
Yaowu Xuf883b422016-08-30 14:01:10 -070040 std::string version = "aomenc";
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070041 if (!webm_ctx->debug) {
Yaowu Xuf883b422016-08-30 14:01:10 -070042 version.append(std::string(" ") + aom_codec_version_str());
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070043 }
44 info->set_writing_app(version.c_str());
45
46 const uint64_t video_track_id =
47 segment->AddVideoTrack(static_cast<int>(cfg->g_w),
clang-format01f4c712016-08-12 15:10:25 -070048 static_cast<int>(cfg->g_h), kVideoTrackNumber);
49 mkvmuxer::VideoTrack *const video_track = static_cast<mkvmuxer::VideoTrack *>(
50 segment->GetTrackByNumber(video_track_id));
Tom Fineganec0833f2018-09-19 14:28:20 -070051
52 aom_fixed_buf_t *obu_sequence_header =
53 aom_codec_get_global_headers(encoder_ctx);
54 if (obu_sequence_header != NULL) {
55 Av1Config av1_config;
56 if (get_av1config_from_obu(
57 reinterpret_cast<const uint8_t *>(obu_sequence_header->buf),
58 obu_sequence_header->sz, false, &av1_config) == 0) {
59 uint8_t av1_config_buffer[4] = { 0 };
60 size_t bytes_written = 0;
61 if (write_av1config(&av1_config, sizeof(av1_config_buffer),
62 &bytes_written, av1_config_buffer) == 0) {
63 video_track->SetCodecPrivate(av1_config_buffer,
64 sizeof(av1_config_buffer));
65 }
66 }
67 free(obu_sequence_header->buf);
68 free(obu_sequence_header);
69 }
70
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070071 video_track->SetStereoMode(stereo_fmt);
Yaowu Xu3d1bb972015-08-14 12:30:10 -070072 const char *codec_id;
73 switch (fourcc) {
Yaowu Xuf883b422016-08-30 14:01:10 -070074 case AV1_FOURCC: codec_id = "V_AV1"; break;
75 default: codec_id = "V_AV1"; break;
Yaowu Xu3d1bb972015-08-14 12:30:10 -070076 }
77 video_track->set_codec_id(codec_id);
Frank Galligan09acd262015-06-01 10:20:58 -070078 if (par->numerator > 1 || par->denominator > 1) {
79 // TODO(fgalligan): Add support of DisplayUnit, Display Aspect Ratio type
80 // to WebM format.
clang-format01f4c712016-08-12 15:10:25 -070081 const uint64_t display_width = static_cast<uint64_t>(
82 ((cfg->g_w * par->numerator * 1.0) / par->denominator) + .5);
Frank Galligan09acd262015-06-01 10:20:58 -070083 video_track->set_display_width(display_width);
84 video_track->set_display_height(cfg->g_h);
85 }
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070086 if (webm_ctx->debug) {
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070087 video_track->set_uid(kDebugTrackUid);
88 }
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070089 webm_ctx->writer = writer;
90 webm_ctx->segment = segment;
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070091}
92
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -070093void write_webm_block(struct WebmOutputContext *webm_ctx,
Yaowu Xuf883b422016-08-30 14:01:10 -070094 const aom_codec_enc_cfg_t *cfg,
95 const aom_codec_cx_pkt_t *pkt) {
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -070096 mkvmuxer::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -070097 reinterpret_cast<mkvmuxer::Segment *>(webm_ctx->segment);
98 int64_t pts_ns = pkt->data.frame.pts * 1000000000ll * cfg->g_timebase.num /
99 cfg->g_timebase.den;
100 if (pts_ns <= webm_ctx->last_pts_ns) pts_ns = webm_ctx->last_pts_ns + 1000000;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700101 webm_ctx->last_pts_ns = pts_ns;
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -0700102
clang-format01f4c712016-08-12 15:10:25 -0700103 segment->AddFrame(static_cast<uint8_t *>(pkt->data.frame.buf),
104 pkt->data.frame.sz, kVideoTrackNumber, pts_ns,
Yaowu Xuf883b422016-08-30 14:01:10 -0700105 pkt->data.frame.flags & AOM_FRAME_IS_KEY);
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -0700106}
107
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700108void write_webm_file_footer(struct WebmOutputContext *webm_ctx) {
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -0700109 mkvmuxer::MkvWriter *const writer =
clang-format01f4c712016-08-12 15:10:25 -0700110 reinterpret_cast<mkvmuxer::MkvWriter *>(webm_ctx->writer);
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -0700111 mkvmuxer::Segment *const segment =
clang-format01f4c712016-08-12 15:10:25 -0700112 reinterpret_cast<mkvmuxer::Segment *>(webm_ctx->segment);
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -0700113 segment->Finalize();
114 delete segment;
115 delete writer;
Vignesh Venkatasubramanian9441f102016-04-25 13:28:24 -0700116 webm_ctx->writer = NULL;
117 webm_ctx->segment = NULL;
Vignesh Venkatasubramanian2dcbf8c2014-03-19 11:56:02 -0700118}