2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
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.
10 #include "./webmenc.h"
14 #include "third_party/libwebm/mkvmuxer/mkvmuxer.h"
15 #include "third_party/libwebm/mkvmuxer/mkvmuxerutil.h"
16 #include "third_party/libwebm/mkvmuxer/mkvwriter.h"
19 const uint64_t kDebugTrackUid = 0xDEADBEEF;
20 const int kVideoTrackNumber = 1;
23 void write_webm_file_header(struct WebmOutputContext *webm_ctx,
24 const vpx_codec_enc_cfg_t *cfg,
25 const struct vpx_rational *fps,
26 stereo_format_t stereo_fmt,
28 const struct VpxRational *par) {
29 mkvmuxer::MkvWriter *const writer = new mkvmuxer::MkvWriter(webm_ctx->stream);
30 mkvmuxer::Segment *const segment = new mkvmuxer::Segment();
31 segment->Init(writer);
32 segment->set_mode(mkvmuxer::Segment::kFile);
33 segment->OutputCues(true);
35 mkvmuxer::SegmentInfo *const info = segment->GetSegmentInfo();
36 const uint64_t kTimecodeScale = 1000000;
37 info->set_timecode_scale(kTimecodeScale);
38 std::string version = "vpxenc";
39 if (!webm_ctx->debug) {
40 version.append(std::string(" ") + vpx_codec_version_str());
42 info->set_writing_app(version.c_str());
44 const uint64_t video_track_id =
45 segment->AddVideoTrack(static_cast<int>(cfg->g_w),
46 static_cast<int>(cfg->g_h),
48 mkvmuxer::VideoTrack* const video_track =
49 static_cast<mkvmuxer::VideoTrack*>(
50 segment->GetTrackByNumber(video_track_id));
51 video_track->SetStereoMode(stereo_fmt);
62 video_track->set_codec_id(codec_id);
63 if (par->numerator > 1 || par->denominator > 1) {
64 // TODO(fgalligan): Add support of DisplayUnit, Display Aspect Ratio type
66 const uint64_t display_width =
67 static_cast<uint64_t>(((cfg->g_w * par->numerator * 1.0) /
68 par->denominator) + .5);
69 video_track->set_display_width(display_width);
70 video_track->set_display_height(cfg->g_h);
72 if (webm_ctx->debug) {
73 video_track->set_uid(kDebugTrackUid);
75 webm_ctx->writer = writer;
76 webm_ctx->segment = segment;
79 void write_webm_block(struct WebmOutputContext *webm_ctx,
80 const vpx_codec_enc_cfg_t *cfg,
81 const vpx_codec_cx_pkt_t *pkt) {
82 mkvmuxer::Segment *const segment =
83 reinterpret_cast<mkvmuxer::Segment*>(webm_ctx->segment);
84 int64_t pts_ns = pkt->data.frame.pts * 1000000000ll *
85 cfg->g_timebase.num / cfg->g_timebase.den;
86 if (pts_ns <= webm_ctx->last_pts_ns)
87 pts_ns = webm_ctx->last_pts_ns + 1000000;
88 webm_ctx->last_pts_ns = pts_ns;
90 segment->AddFrame(static_cast<uint8_t*>(pkt->data.frame.buf),
94 pkt->data.frame.flags & VPX_FRAME_IS_KEY);
97 void write_webm_file_footer(struct WebmOutputContext *webm_ctx) {
98 mkvmuxer::MkvWriter *const writer =
99 reinterpret_cast<mkvmuxer::MkvWriter*>(webm_ctx->writer);
100 mkvmuxer::Segment *const segment =
101 reinterpret_cast<mkvmuxer::Segment*>(webm_ctx->segment);
105 webm_ctx->writer = NULL;
106 webm_ctx->segment = NULL;