Write AV1 CodecPrivate when outputting WebM.

Per https://github.com/Matroska-Org/matroska-specification/blob/master/codec/av1.md
the presence of the CodecPrivate is mandatory.

STATS_CHANGED:
This change slightly increases the size of all WebM output from aomenc
due to the addition of the CodecPrivate element that contains the AV1
configuration information.

BUG=aomedia:2027

Change-Id: I1e320c9ecfb5e2c8ddb4840deaab2bce16fcbe9a
diff --git a/apps/aomenc.c b/apps/aomenc.c
index 2e5d35c..a9c6f78 100644
--- a/apps/aomenc.c
+++ b/apps/aomenc.c
@@ -1463,8 +1463,9 @@
 #if CONFIG_WEBM_IO
   if (stream->config.write_webm) {
     stream->webm_ctx.stream = stream->file;
-    write_webm_file_header(&stream->webm_ctx, cfg, stream->config.stereo_fmt,
-                           global->codec->fourcc, pixel_aspect_ratio);
+    write_webm_file_header(&stream->webm_ctx, &stream->encoder, cfg,
+                           stream->config.stereo_fmt, global->codec->fourcc,
+                           pixel_aspect_ratio);
   }
 #else
   (void)pixel_aspect_ratio;
@@ -2153,10 +2154,11 @@
     }
 
     FOREACH_STREAM(stream, streams) { setup_pass(stream, &global, pass); }
+    FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
     FOREACH_STREAM(stream, streams) {
       open_output_file(stream, &global, &input.pixel_aspect_ratio);
     }
-    FOREACH_STREAM(stream, streams) { initialize_encoder(stream, &global); }
+
     if (strcmp(global.codec->name, "av1") == 0 ||
         strcmp(global.codec->name, "av1") == 0) {
       // Check to see if at least one stream uses 16 bit internal.
diff --git a/common/webmenc.cc b/common/webmenc.cc
index 58ab336..02f9850 100644
--- a/common/webmenc.cc
+++ b/common/webmenc.cc
@@ -13,6 +13,7 @@
 
 #include <string>
 
+#include "common/av1_config.h"
 #include "third_party/libwebm/mkvmuxer/mkvmuxer.h"
 #include "third_party/libwebm/mkvmuxer/mkvmuxerutil.h"
 #include "third_party/libwebm/mkvmuxer/mkvwriter.h"
@@ -23,6 +24,7 @@
 }  // namespace
 
 void write_webm_file_header(struct WebmOutputContext *webm_ctx,
+                            aom_codec_ctx_t *encoder_ctx,
                             const aom_codec_enc_cfg_t *cfg,
                             stereo_format_t stereo_fmt, unsigned int fourcc,
                             const struct AvxRational *par) {
@@ -46,6 +48,26 @@
                              static_cast<int>(cfg->g_h), kVideoTrackNumber);
   mkvmuxer::VideoTrack *const video_track = static_cast<mkvmuxer::VideoTrack *>(
       segment->GetTrackByNumber(video_track_id));
+
+  aom_fixed_buf_t *obu_sequence_header =
+      aom_codec_get_global_headers(encoder_ctx);
+  if (obu_sequence_header != NULL) {
+    Av1Config av1_config;
+    if (get_av1config_from_obu(
+            reinterpret_cast<const uint8_t *>(obu_sequence_header->buf),
+            obu_sequence_header->sz, false, &av1_config) == 0) {
+      uint8_t av1_config_buffer[4] = { 0 };
+      size_t bytes_written = 0;
+      if (write_av1config(&av1_config, sizeof(av1_config_buffer),
+                          &bytes_written, av1_config_buffer) == 0) {
+        video_track->SetCodecPrivate(av1_config_buffer,
+                                     sizeof(av1_config_buffer));
+      }
+    }
+    free(obu_sequence_header->buf);
+    free(obu_sequence_header);
+  }
+
   video_track->SetStereoMode(stereo_fmt);
   const char *codec_id;
   switch (fourcc) {
diff --git a/common/webmenc.h b/common/webmenc.h
index aa9832f..9d459a7 100644
--- a/common/webmenc.h
+++ b/common/webmenc.h
@@ -39,6 +39,7 @@
 } stereo_format_t;
 
 void write_webm_file_header(struct WebmOutputContext *webm_ctx,
+                            aom_codec_ctx_t *encoder_ctx,
                             const aom_codec_enc_cfg_t *cfg,
                             stereo_format_t stereo_fmt, unsigned int fourcc,
                             const struct AvxRational *par);