95 #define MODE_MATROSKAv2 0x01 
   96 #define MODE_WEBM       0x02 
  131 #define MAX_SEEKENTRY_SIZE 21 
  135 #define MAX_CUETRACKPOS_SIZE 42 
  138 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE * num_tracks 
  141 #define OPUS_SEEK_PREROLL 80000000 
  145     return (
av_log2(
id + 1) - 1) / 7 + 1;
 
  173     while ((num + 1) >> bytes * 7)
 
  193         bytes = needed_bytes;
 
  198     num |= 1ULL << bytes * 7;
 
  199     for (i = bytes - 1; i >= 0; i--)
 
  212     for (i = bytes - 1; i >= 0; i--)
 
  219     uint64_t tmp = 2*(val < 0 ? val^-1 : 
val);
 
  221     while (tmp>>=8) bytes++;
 
  225     for (i = bytes - 1; i >= 0; i--)
 
  274                                      uint64_t expectedsize)
 
  318     if (numelements > 0) {
 
  393         currentpos = seekhead->
filepos;
 
  413                             int64_t cluster_pos, int64_t relative_pos, int64_t 
duration)
 
  448         uint64_t pts = entry->
pts;
 
  452         for (j = 0; j < num_tracks; j++)
 
  453             tracks[j].has_cue = 0;
 
  454         for (j = 0; j < cues->
num_entries - i && entry[j].
pts == pts; j++) {
 
  456             av_assert0(tracknum>=0 && tracknum<num_tracks);
 
  468         for (j = 0; j < num_tracks; j++)
 
  469             tracks[j].has_cue = 0;
 
  470         for (j = 0; j < cues->
num_entries - i && entry[j].
pts == pts; j++) {
 
  472             av_assert0(tracknum>=0 && tracknum<num_tracks);
 
  496     int first_header_size;
 
  500         first_header_size = 30;
 
  502         first_header_size = 42;
 
  505                               first_header_size, header_start, header_len) < 0) {
 
  511     for (j = 0; j < 2; j++) {
 
  514     for (j = 0; j < 3; j++)
 
  515         avio_write(pb, header_start[j], header_len[j]);
 
  549         av_dict_set(&dict, 
"WAVEFORMATEXTENSIBLE_CHANNEL_MASK", buf, 0);
 
  581                "Error parsing AAC extradata, unable to determine samplerate.\n");
 
  610                    "Invalid extradata found, ALAC expects a 36-byte " 
  634     int ret, codecpriv_size;
 
  654                     for(i = 0; i < 0x5a - 8; i++)
 
  711         int stereo_mode = atoi(tag->
value);
 
  719         if (stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB &&
 
  720             stereo_mode != 10 && stereo_mode != 12) {
 
  733             switch (stereo->
type) {
 
  785                "The specified stereo mode is not valid.\n");
 
  806     int output_sample_rate = 0;
 
  807     int display_width_div = 1;
 
  808     int display_height_div = 1;
 
  840     } 
else if (tag && tag->
value) {
 
  855             codec_id = 
"D_WEBVTT/CAPTIONS";
 
  858             codec_id = 
"D_WEBVTT/DESCRIPTIONS";
 
  861             codec_id = 
"D_WEBVTT/METADATA";
 
  864             codec_id = 
"D_WEBVTT/SUBTITLES";
 
  900                "Only VP8 or VP9 video and Vorbis or Opus audio and WebVTT subtitles are supported for WebM.\n");
 
  904     switch (codec->codec_type) {
 
  908         if(   st->avg_frame_rate.num > 0 && st->avg_frame_rate.den > 0
 
  909            && 1.0/
av_q2d(st->avg_frame_rate) > 
av_q2d(codec->time_base))
 
  924         else if (!native_id) {
 
  927             mkv->tracks[i].write_dts = 1;
 
  939                                     &display_height_div);
 
  943         if (((
tag = 
av_dict_get(st->metadata, 
"alpha_mode", NULL, 0)) && atoi(
tag->value)) ||
 
  951         if (st->sample_aspect_ratio.num) {
 
  952             int64_t d_width = 
av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
 
  953             if (d_width > INT_MAX) {
 
  959         } 
else if (display_width_div != 1 || display_height_div != 1) {
 
  965             uint32_t color_space = 
av_le2ne32(codec->codec_tag);
 
  982         if (output_sample_rate)
 
 1001         av_log(
s, 
AV_LOG_ERROR, 
"Only audio, video, and subtitles are supported for Matroska.\n");
 
 1021     int i, 
ret, default_stream_exists = 0;
 
 1053     if (ret < 0) 
return ret;
 
 1094     if ((p = strrchr(p, 
'-')) &&
 
 1102         else if (*p >= 
'a' && *p <= 
'z')
 
 1127         if (ret < 0) 
return ret;
 
 1169         if (ret < 0) 
return ret;
 
 1179         if (ret < 0) 
return ret;
 
 1189         if (ret < 0) 
return ret;
 
 1211     if (ret < 0) 
return ret;
 
 1219         const char *mimetype = NULL;
 
 1235             mimetype = t->
value;
 
 1246                                     "it cannot be deduced from the codec id.\n", i);
 
 1305                    "The Matroska muxer does not yet support muxing %s\n",
 
 1342     if (ret < 0) 
return ret;
 
 1349         uint32_t segment_uid[4];
 
 1354         for (i = 0; i < 4; i++)
 
 1364         const char *ident = 
"Lavf";
 
 1373         AV_WB64(date_utc_buf, date_utc);
 
 1441     int size = pkt_size + 4;
 
 1488         memcpy(dst + offset, src, header.
blocksize);
 
 1511     uint64_t additional_id = 0;
 
 1512     int64_t discard_padding = 0;
 
 1513     ebml_master block_group, block_additions, block_more;
 
 1516            "pts %" PRId64 
", dts %" PRId64 
", duration %d, flags %d\n",
 
 1545     if (side_data && side_data_size >= 10) {
 
 1555         additional_id = 
AV_RB64(side_data);
 
 1557         side_data_size -= 8;
 
 1560     if ((side_data_size && additional_id == 1) || discard_padding) {
 
 1572     if (data != pkt->
data)
 
 1575     if (discard_padding) {
 
 1579     if (side_data_size && additional_id == 1) {
 
 1589     if ((side_data_size && additional_id == 1) || discard_padding) {
 
 1598     int id_size, settings_size, 
size;
 
 1601     const int flags = 0;
 
 1611     size = id_size + 1 + settings_size + 1 + pkt->
size;
 
 1614            "pts %" PRId64 
", dts %" PRId64 
", duration %d, flags %d\n",
 
 1624     avio_printf(pb, 
"%.*s\n%.*s\n%.*s", id_size, 
id, settings_size, settings, pkt->
size, pkt->
data);
 
 1659             "Starting new cluster at offset %" PRIu64 
" bytes, " 
 1660             "pts %" PRIu64 
"dts %" PRIu64 
"\n",
 
 1678     int64_t relative_packet_pos;
 
 1689         if ((int16_t)cluster_time != cluster_time) {
 
 1719             if (ret < 0) 
return ret;
 
 1752     int64_t cluster_time;
 
 1754     int start_new_cluster;
 
 1774         start_new_cluster = keyframe;
 
 1779         start_new_cluster = 1;
 
 1784                  cluster_size > 4 * 1024))) {
 
 1785         start_new_cluster = 1;
 
 1787         start_new_cluster = 0;
 
 1790     if (mkv->
cluster_pos != -1 && start_new_cluster) {
 
 1798                                         mkv->
is_dash ? start_new_cluster : 0);
 
 1802                    "Could not write cached audio packet ret:%d\n", ret);
 
 1835                    "Flushing cluster at offset %" PRIu64 
" bytes\n",
 
 1852     int64_t currentpos, cuespos;
 
 1861                    "Could not write cached audio packet ret:%d\n", ret);
 
 1891                            "Insufficient space reserved for cues: %d " 
 1892                            "(needed: %" PRId64 
").\n",
 
 1897                 if (cues_end < cuespos + mkv->reserve_cues_space)
 
 1899                                   (cues_end - cuespos));
 
 1973 #define OFFSET(x) offsetof(MatroskaMuxContext, x) 
 1974 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM 
 1976     { 
"reserve_index_space", 
"Reserve a given amount of space (in bytes) at the beginning of the file for the index (cues).", 
OFFSET(reserve_cues_space), 
AV_OPT_TYPE_INT,   { .i64 = 0 },   0, INT_MAX,   
FLAGS },
 
 1977     { 
"cluster_size_limit",  
"Store at most the provided amount of bytes in a cluster. ",                                     
OFFSET(cluster_size_limit), 
AV_OPT_TYPE_INT  , { .i64 = -1 }, -1, INT_MAX,   
FLAGS },
 
 1978     { 
"cluster_time_limit",  
"Store at most the provided number of milliseconds in a cluster.",                               
OFFSET(cluster_time_limit), 
AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, 
FLAGS },
 
 1979     { 
"dash", 
"Create a WebM file conforming to WebM DASH specification", 
OFFSET(is_dash), 
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, 
FLAGS },
 
 1980     { 
"dash_track_number", 
"Track number for the DASH stream", 
OFFSET(dash_track_number), 
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 127, 
FLAGS },
 
 1984 #if CONFIG_MATROSKA_MUXER 
 1985 static const AVClass matroska_class = {
 
 1995     .mime_type         = 
"video/x-matroska",
 
 1996     .extensions        = 
"mkv",
 
 1998     .audio_codec       = CONFIG_LIBVORBIS_ENCODER ?
 
 2000     .video_codec       = CONFIG_LIBX264_ENCODER ?
 
 2013     .priv_class        = &matroska_class,
 
 2017 #if CONFIG_WEBM_MUXER 
 2018 static const AVClass webm_class = {
 
 2028     .mime_type         = 
"video/webm",
 
 2029     .extensions        = 
"webm",
 
 2039     .priv_class        = &webm_class,
 
 2043 #if CONFIG_MATROSKA_AUDIO_MUXER 
 2044 static const AVClass mka_class = {
 
 2053     .mime_type         = 
"audio/x-matroska",
 
 2054     .extensions        = 
"mka",
 
 2056     .audio_codec       = CONFIG_LIBVORBIS_ENCODER ?
 
 2067     .priv_class        = &mka_class,