webmenc.cc 3.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#include "./webmenc.h"

#include <string>

14 15 16
#include "third_party/libwebm/mkvmuxer/mkvmuxer.h"
#include "third_party/libwebm/mkvmuxer/mkvmuxerutil.h"
#include "third_party/libwebm/mkvmuxer/mkvwriter.h"
17 18 19 20 21 22

namespace {
const uint64_t kDebugTrackUid = 0xDEADBEEF;
const int kVideoTrackNumber = 1;
}  // namespace

23
void write_webm_file_header(struct WebmOutputContext *webm_ctx,
24 25
                            const vpx_codec_enc_cfg_t *cfg,
                            const struct vpx_rational *fps,
26
                            stereo_format_t stereo_fmt, unsigned int fourcc,
27
                            const struct VpxRational *par) {
28
  mkvmuxer::MkvWriter *const writer = new mkvmuxer::MkvWriter(webm_ctx->stream);
29 30 31 32 33 34 35 36 37
  mkvmuxer::Segment *const segment = new mkvmuxer::Segment();
  segment->Init(writer);
  segment->set_mode(mkvmuxer::Segment::kFile);
  segment->OutputCues(true);

  mkvmuxer::SegmentInfo *const info = segment->GetSegmentInfo();
  const uint64_t kTimecodeScale = 1000000;
  info->set_timecode_scale(kTimecodeScale);
  std::string version = "vpxenc";
38
  if (!webm_ctx->debug) {
39 40 41 42 43 44
    version.append(std::string(" ") + vpx_codec_version_str());
  }
  info->set_writing_app(version.c_str());

  const uint64_t video_track_id =
      segment->AddVideoTrack(static_cast<int>(cfg->g_w),
45 46 47
                             static_cast<int>(cfg->g_h), kVideoTrackNumber);
  mkvmuxer::VideoTrack *const video_track = static_cast<mkvmuxer::VideoTrack *>(
      segment->GetTrackByNumber(video_track_id));
48
  video_track->SetStereoMode(stereo_fmt);
49 50
  const char *codec_id;
  switch (fourcc) {
51 52 53 54
    case VP8_FOURCC: codec_id = "V_VP8"; break;
    case VP9_FOURCC: codec_id = "V_VP9"; break;
    case VP10_FOURCC: codec_id = "V_VP10"; break;
    default: codec_id = "V_VP10"; break;
55 56
  }
  video_track->set_codec_id(codec_id);
57 58 59
  if (par->numerator > 1 || par->denominator > 1) {
    // TODO(fgalligan): Add support of DisplayUnit, Display Aspect Ratio type
    // to WebM format.
60 61
    const uint64_t display_width = static_cast<uint64_t>(
        ((cfg->g_w * par->numerator * 1.0) / par->denominator) + .5);
62 63 64
    video_track->set_display_width(display_width);
    video_track->set_display_height(cfg->g_h);
  }
65
  if (webm_ctx->debug) {
66 67
    video_track->set_uid(kDebugTrackUid);
  }
68 69
  webm_ctx->writer = writer;
  webm_ctx->segment = segment;
70 71
}

72
void write_webm_block(struct WebmOutputContext *webm_ctx,
73 74 75
                      const vpx_codec_enc_cfg_t *cfg,
                      const vpx_codec_cx_pkt_t *pkt) {
  mkvmuxer::Segment *const segment =
76 77 78 79
      reinterpret_cast<mkvmuxer::Segment *>(webm_ctx->segment);
  int64_t pts_ns = pkt->data.frame.pts * 1000000000ll * cfg->g_timebase.num /
                   cfg->g_timebase.den;
  if (pts_ns <= webm_ctx->last_pts_ns) pts_ns = webm_ctx->last_pts_ns + 1000000;
80
  webm_ctx->last_pts_ns = pts_ns;
81

82 83
  segment->AddFrame(static_cast<uint8_t *>(pkt->data.frame.buf),
                    pkt->data.frame.sz, kVideoTrackNumber, pts_ns,
84 85 86
                    pkt->data.frame.flags & VPX_FRAME_IS_KEY);
}

87
void write_webm_file_footer(struct WebmOutputContext *webm_ctx) {
88
  mkvmuxer::MkvWriter *const writer =
89
      reinterpret_cast<mkvmuxer::MkvWriter *>(webm_ctx->writer);
90
  mkvmuxer::Segment *const segment =
91
      reinterpret_cast<mkvmuxer::Segment *>(webm_ctx->segment);
92 93 94
  segment->Finalize();
  delete segment;
  delete writer;
95 96
  webm_ctx->writer = NULL;
  webm_ctx->segment = NULL;
97
}