2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
4 * 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.
12 #include "./webmenc.h"
16 #include "third_party/libwebm/mkvmuxer/mkvmuxer.h"
17 #include "third_party/libwebm/mkvmuxer/mkvmuxerutil.h"
18 #include "third_party/libwebm/mkvmuxer/mkvwriter.h"
21 const uint64_t kDebugTrackUid
= 0xDEADBEEF;
22 const int kVideoTrackNumber
= 1;
25 void write_webm_file_header(struct WebmOutputContext
*webm_ctx
,
26 const aom_codec_enc_cfg_t
*cfg
,
27 stereo_format_t stereo_fmt
, unsigned int fourcc
,
28 const struct AvxRational
*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
= "aomenc";
39 if (!webm_ctx
->debug
) {
40 version
.append(std::string(" ") + aom_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
), kVideoTrackNumber
);
47 mkvmuxer::VideoTrack
*const video_track
= static_cast<mkvmuxer::VideoTrack
*>(
48 segment
->GetTrackByNumber(video_track_id
));
49 video_track
->SetStereoMode(stereo_fmt
);
52 case AV1_FOURCC
: codec_id
= "V_AV1"; break;
53 default: codec_id
= "V_AV1"; break;
55 video_track
->set_codec_id(codec_id
);
56 if (par
->numerator
> 1 || par
->denominator
> 1) {
57 // TODO(fgalligan): Add support of DisplayUnit, Display Aspect Ratio type
59 const uint64_t display_width
= static_cast<uint64_t>(
60 ((cfg
->g_w
* par
->numerator
* 1.0) / par
->denominator
) + .5);
61 video_track
->set_display_width(display_width
);
62 video_track
->set_display_height(cfg
->g_h
);
64 if (webm_ctx
->debug
) {
65 video_track
->set_uid(kDebugTrackUid
);
67 webm_ctx
->writer
= writer
;
68 webm_ctx
->segment
= segment
;
71 void write_webm_block(struct WebmOutputContext
*webm_ctx
,
72 const aom_codec_enc_cfg_t
*cfg
,
73 const aom_codec_cx_pkt_t
*pkt
) {
74 mkvmuxer::Segment
*const segment
=
75 reinterpret_cast<mkvmuxer::Segment
*>(webm_ctx
->segment
);
76 int64_t pts_ns
= pkt
->data
.frame
.pts
* 1000000000ll * cfg
->g_timebase
.num
/
78 if (pts_ns
<= webm_ctx
->last_pts_ns
) pts_ns
= webm_ctx
->last_pts_ns
+ 1000000;
79 webm_ctx
->last_pts_ns
= pts_ns
;
81 segment
->AddFrame(static_cast<uint8_t *>(pkt
->data
.frame
.buf
),
82 pkt
->data
.frame
.sz
, kVideoTrackNumber
, pts_ns
,
83 pkt
->data
.frame
.flags
& AOM_FRAME_IS_KEY
);
86 void write_webm_file_footer(struct WebmOutputContext
*webm_ctx
) {
87 mkvmuxer::MkvWriter
*const writer
=
88 reinterpret_cast<mkvmuxer::MkvWriter
*>(webm_ctx
->writer
);
89 mkvmuxer::Segment
*const segment
=
90 reinterpret_cast<mkvmuxer::Segment
*>(webm_ctx
->segment
);
94 webm_ctx
->writer
= NULL
;
95 webm_ctx
->segment
= NULL
;