webmenc.cc 3.6 KB

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