1 /* 2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved. 3 * 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. 9 */ 10 #include "./webmenc.h" 11 12 #include <string> 13 14 #include "third_party/libwebm/mkvmuxer/mkvmuxer.h" 15 #include "third_party/libwebm/mkvmuxer/mkvmuxerutil.h" 16 #include "third_party/libwebm/mkvmuxer/mkvwriter.h" 17 18 namespace { 19 const uint64_t kDebugTrackUid = 0xDEADBEEF; 20 const int kVideoTrackNumber = 1; 21 } // namespace 22 23 void write_webm_file_header(struct WebmOutputContext *webm_ctx, 24 const vpx_codec_enc_cfg_t *cfg, 25 stereo_format_t stereo_fmt, unsigned int fourcc, 26 const struct VpxRational *par) { 27 mkvmuxer::MkvWriter *const writer = new mkvmuxer::MkvWriter(webm_ctx->stream); 28 mkvmuxer::Segment *const segment = new mkvmuxer::Segment(); 29 segment->Init(writer); 30 segment->set_mode(mkvmuxer::Segment::kFile); 31 segment->OutputCues(true); 32 33 mkvmuxer::SegmentInfo *const info = segment->GetSegmentInfo(); 34 const uint64_t kTimecodeScale = 1000000; 35 info->set_timecode_scale(kTimecodeScale); 36 std::string version = "vpxenc"; 37 if (!webm_ctx->debug) { 38 version.append(std::string(" ") + vpx_codec_version_str()); 39 } 40 info->set_writing_app(version.c_str()); 41 42 const uint64_t video_track_id = 43 segment->AddVideoTrack(static_cast<int>(cfg->g_w), 44 static_cast<int>(cfg->g_h), kVideoTrackNumber); 45 mkvmuxer::VideoTrack *const video_track = static_cast<mkvmuxer::VideoTrack *>( 46 segment->GetTrackByNumber(video_track_id)); 47 video_track->SetStereoMode(stereo_fmt); 48 const char *codec_id; 49 switch (fourcc) { 50 case VP8_FOURCC: codec_id = "V_VP8"; break; 51 case VP9_FOURCC: 52 default: codec_id = "V_VP9"; break; 53 } 54 video_track->set_codec_id(codec_id); 55 if (par->numerator > 1 || par->denominator > 1) { 56 // TODO(fgalligan): Add support of DisplayUnit, Display Aspect Ratio type 57 // to WebM format. 58 const uint64_t display_width = static_cast<uint64_t>( 59 ((cfg->g_w * par->numerator * 1.0) / par->denominator) + .5); 60 video_track->set_display_width(display_width); 61 video_track->set_display_height(cfg->g_h); 62 } 63 if (webm_ctx->debug) { 64 video_track->set_uid(kDebugTrackUid); 65 } 66 webm_ctx->writer = writer; 67 webm_ctx->segment = segment; 68 } 69 70 void write_webm_block(struct WebmOutputContext *webm_ctx, 71 const vpx_codec_enc_cfg_t *cfg, 72 const vpx_codec_cx_pkt_t *pkt) { 73 mkvmuxer::Segment *const segment = 74 reinterpret_cast<mkvmuxer::Segment *>(webm_ctx->segment); 75 int64_t pts_ns = pkt->data.frame.pts * 1000000000ll * cfg->g_timebase.num / 76 cfg->g_timebase.den; 77 if (pts_ns <= webm_ctx->last_pts_ns) pts_ns = webm_ctx->last_pts_ns + 1000000; 78 webm_ctx->last_pts_ns = pts_ns; 79 80 segment->AddFrame(static_cast<uint8_t *>(pkt->data.frame.buf), 81 pkt->data.frame.sz, kVideoTrackNumber, pts_ns, 82 pkt->data.frame.flags & VPX_FRAME_IS_KEY); 83 } 84 85 void write_webm_file_footer(struct WebmOutputContext *webm_ctx) { 86 mkvmuxer::MkvWriter *const writer = 87 reinterpret_cast<mkvmuxer::MkvWriter *>(webm_ctx->writer); 88 mkvmuxer::Segment *const segment = 89 reinterpret_cast<mkvmuxer::Segment *>(webm_ctx->segment); 90 segment->Finalize(); 91 delete segment; 92 delete writer; 93 webm_ctx->writer = NULL; 94 webm_ctx->segment = NULL; 95 } 96