#include "matroska.h"
#include "avc.h"
#include "flacenc.h"
+#include "avlanguage.h"
+#include "libavutil/samplefmt.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/random_seed.h"
#include "libavutil/lfg.h"
#include "libavcodec/xiph.h"
#include "libavcodec/mpeg4audio.h"
+#include <strings.h>
typedef struct ebml_master {
int64_t pos; ///< absolute offset in the file where the master's elements start
typedef struct MatroskaMuxContext {
int mode;
- ByteIOContext *dyn_bc;
+ AVIOContext *dyn_bc;
ebml_master segment;
int64_t segment_offset;
ebml_master cluster;
return (av_log2(id+1)-1)/7+1;
}
-static void put_ebml_id(ByteIOContext *pb, unsigned int id)
+static void put_ebml_id(AVIOContext *pb, unsigned int id)
{
int i = ebml_id_size(id);
while (i--)
- put_byte(pb, id >> (i*8));
+ avio_w8(pb, id >> (i*8));
}
/**
*
* @param bytes The number of bytes the size should occupy (maximum: 8).
*/
-static void put_ebml_size_unknown(ByteIOContext *pb, int bytes)
+static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
{
assert(bytes <= 8);
- put_byte(pb, 0x1ff >> bytes);
+ avio_w8(pb, 0x1ff >> bytes);
while (--bytes)
- put_byte(pb, 0xff);
+ avio_w8(pb, 0xff);
}
/**
* @param bytes The number of bytes that need to be used to write the number.
* If zero, any number of bytes can be used.
*/
-static void put_ebml_num(ByteIOContext *pb, uint64_t num, int bytes)
+static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
{
int i, needed_bytes = ebml_num_size(num);
num |= 1ULL << bytes*7;
for (i = bytes - 1; i >= 0; i--)
- put_byte(pb, num >> i*8);
+ avio_w8(pb, num >> i*8);
}
-static void put_ebml_uint(ByteIOContext *pb, unsigned int elementid, uint64_t val)
+static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
{
int i, bytes = 1;
uint64_t tmp = val;
put_ebml_id(pb, elementid);
put_ebml_num(pb, bytes, 0);
for (i = bytes - 1; i >= 0; i--)
- put_byte(pb, val >> i*8);
+ avio_w8(pb, val >> i*8);
}
-static void put_ebml_float(ByteIOContext *pb, unsigned int elementid, double val)
+static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
{
put_ebml_id(pb, elementid);
put_ebml_num(pb, 8, 0);
- put_be64(pb, av_dbl2int(val));
+ avio_wb64(pb, av_dbl2int(val));
}
-static void put_ebml_binary(ByteIOContext *pb, unsigned int elementid,
+static void put_ebml_binary(AVIOContext *pb, unsigned int elementid,
const void *buf, int size)
{
put_ebml_id(pb, elementid);
put_ebml_num(pb, size, 0);
- put_buffer(pb, buf, size);
+ avio_write(pb, buf, size);
}
-static void put_ebml_string(ByteIOContext *pb, unsigned int elementid, const char *str)
+static void put_ebml_string(AVIOContext *pb, unsigned int elementid, const char *str)
{
put_ebml_binary(pb, elementid, str, strlen(str));
}
/**
- * Writes a void element of a given size. Useful for reserving space in
+ * Write a void element of a given size. Useful for reserving space in
* the file to be written to later.
*
* @param size The number of bytes to reserve, which must be at least 2.
*/
-static void put_ebml_void(ByteIOContext *pb, uint64_t size)
+static void put_ebml_void(AVIOContext *pb, uint64_t size)
{
- int64_t currentpos = url_ftell(pb);
+ int64_t currentpos = avio_tell(pb);
assert(size >= 2);
put_ebml_num(pb, size-1, 0);
else
put_ebml_num(pb, size-9, 8);
- while(url_ftell(pb) < currentpos + size)
- put_byte(pb, 0);
+ while(avio_tell(pb) < currentpos + size)
+ avio_w8(pb, 0);
}
-static ebml_master start_ebml_master(ByteIOContext *pb, unsigned int elementid, uint64_t expectedsize)
+static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize)
{
int bytes = expectedsize ? ebml_num_size(expectedsize) : 8;
put_ebml_id(pb, elementid);
put_ebml_size_unknown(pb, bytes);
- return (ebml_master){ url_ftell(pb), bytes };
+ return (ebml_master){ avio_tell(pb), bytes };
}
-static void end_ebml_master(ByteIOContext *pb, ebml_master master)
+static void end_ebml_master(AVIOContext *pb, ebml_master master)
{
- int64_t pos = url_ftell(pb);
+ int64_t pos = avio_tell(pb);
- if (url_fseek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
+ if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
return;
put_ebml_num(pb, pos - master.pos, master.sizebytes);
- url_fseek(pb, pos, SEEK_SET);
+ avio_seek(pb, pos, SEEK_SET);
}
-static void put_xiph_size(ByteIOContext *pb, int size)
+static void put_xiph_size(AVIOContext *pb, int size)
{
int i;
for (i = 0; i < size / 255; i++)
- put_byte(pb, 255);
- put_byte(pb, size % 255);
+ avio_w8(pb, 255);
+ avio_w8(pb, size % 255);
}
/**
* @param numelements The maximum number of elements that will be indexed
* by this seek head, 0 if unlimited.
*/
-static mkv_seekhead * mkv_start_seekhead(ByteIOContext *pb, int64_t segment_offset, int numelements)
+static mkv_seekhead * mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset, int numelements)
{
mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead));
if (new_seekhead == NULL)
new_seekhead->segment_offset = segment_offset;
if (numelements > 0) {
- new_seekhead->filepos = url_ftell(pb);
+ new_seekhead->filepos = avio_tell(pb);
// 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID
// and size, and 3 bytes to guarantee that an EBML void element
// will fit afterwards
* @return The file offset where the seekhead was written,
* -1 if an error occurred.
*/
-static int64_t mkv_write_seekhead(ByteIOContext *pb, mkv_seekhead *seekhead)
+static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
{
ebml_master metaseek, seekentry;
int64_t currentpos;
int i;
- currentpos = url_ftell(pb);
+ currentpos = avio_tell(pb);
if (seekhead->reserved_size > 0)
- if (url_fseek(pb, seekhead->filepos, SEEK_SET) < 0)
+ if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0)
return -1;
metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);
end_ebml_master(pb, metaseek);
if (seekhead->reserved_size > 0) {
- uint64_t remaining = seekhead->filepos + seekhead->reserved_size - url_ftell(pb);
+ uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
put_ebml_void(pb, remaining);
- url_fseek(pb, currentpos, SEEK_SET);
+ avio_seek(pb, currentpos, SEEK_SET);
currentpos = seekhead->filepos;
}
return 0;
}
-static int64_t mkv_write_cues(ByteIOContext *pb, mkv_cues *cues, int num_tracks)
+static int64_t mkv_write_cues(AVIOContext *pb, mkv_cues *cues, int num_tracks)
{
ebml_master cues_element;
int64_t currentpos;
int i, j;
- currentpos = url_ftell(pb);
+ currentpos = avio_tell(pb);
cues_element = start_ebml_master(pb, MATROSKA_ID_CUES, 0);
for (i = 0; i < cues->num_entries; i++) {
return currentpos;
}
-static int put_xiph_codecpriv(AVFormatContext *s, ByteIOContext *pb, AVCodecContext *codec)
+static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec)
{
uint8_t *header_start[3];
int header_len[3];
return -1;
}
- put_byte(pb, 2); // number packets - 1
+ avio_w8(pb, 2); // number packets - 1
for (j = 0; j < 2; j++) {
put_xiph_size(pb, header_len[j]);
}
for (j = 0; j < 3; j++)
- put_buffer(pb, header_start[j], header_len[j]);
+ avio_write(pb, header_start[j], header_len[j]);
return 0;
}
static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
{
- int sri;
+ MPEG4AudioConfig mp4ac;
- if (codec->extradata_size < 2) {
- av_log(s, AV_LOG_WARNING, "No AAC extradata, unable to determine samplerate.\n");
+ if (ff_mpeg4audio_get_config(&mp4ac, codec->extradata, codec->extradata_size) < 0) {
+ av_log(s, AV_LOG_WARNING, "Error parsing AAC extradata, unable to determine samplerate.\n");
return;
}
- sri = ((codec->extradata[0] << 1) & 0xE) | (codec->extradata[1] >> 7);
- if (sri > 12) {
- av_log(s, AV_LOG_WARNING, "AAC samplerate index out of bounds\n");
- return;
- }
- *sample_rate = ff_mpeg4audio_sample_rates[sri];
-
- // if sbr, get output sample rate as well
- if (codec->extradata_size == 5) {
- sri = (codec->extradata[4] >> 3) & 0xF;
- if (sri > 12) {
- av_log(s, AV_LOG_WARNING, "AAC output samplerate index out of bounds\n");
- return;
- }
- *output_sample_rate = ff_mpeg4audio_sample_rates[sri];
- }
+ *sample_rate = mp4ac.sample_rate;
+ *output_sample_rate = mp4ac.ext_sample_rate;
}
-static int mkv_write_codecprivate(AVFormatContext *s, ByteIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
+static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
{
- ByteIOContext *dyn_cp;
+ AVIOContext *dyn_cp;
uint8_t *codecpriv;
int ret, codecpriv_size;
else if (codec->codec_id == CODEC_ID_H264)
ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size);
else if (codec->extradata_size)
- put_buffer(dyn_cp, codec->extradata, codec->extradata_size);
+ avio_write(dyn_cp, codec->extradata, codec->extradata_size);
} else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
if (qt_id) {
if (!codec->codec_tag)
codec->codec_tag = ff_codec_get_tag(codec_movvideo_tags, codec->codec_id);
if (codec->extradata_size)
- put_buffer(dyn_cp, codec->extradata, codec->extradata_size);
+ avio_write(dyn_cp, codec->extradata, codec->extradata_size);
} else {
if (!codec->codec_tag)
codec->codec_tag = ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id);
static int mkv_write_tracks(AVFormatContext *s)
{
MatroskaMuxContext *mkv = s->priv_data;
- ByteIOContext *pb = s->pb;
+ AVIOContext *pb = s->pb;
ebml_master tracks;
int i, j, ret;
- ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TRACKS, url_ftell(pb));
+ ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TRACKS, avio_tell(pb));
if (ret < 0) return ret;
tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
AVMetadataTag *tag;
if (!bit_depth)
- bit_depth = av_get_bits_per_sample_format(codec->sample_fmt);
+ bit_depth = av_get_bits_per_sample_fmt(codec->sample_fmt);
if (codec->codec_id == CODEC_ID_AAC)
get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate);
int d_width = codec->width*av_q2d(st->sample_aspect_ratio);
put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width);
put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height);
+ put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYUNIT, 3);
}
end_ebml_master(pb, subinfo);
break;
case AVMEDIA_TYPE_SUBTITLE:
put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_SUBTITLE);
+ if (!native_id) {
+ av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id);
+ return AVERROR_NOTSUPP;
+ }
break;
default:
- av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.");
+ av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
break;
}
ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
static int mkv_write_chapters(AVFormatContext *s)
{
MatroskaMuxContext *mkv = s->priv_data;
- ByteIOContext *pb = s->pb;
+ AVIOContext *pb = s->pb;
ebml_master chapters, editionentry;
AVRational scale = {1, 1E9};
int i, ret;
if (!s->nb_chapters)
return 0;
- ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CHAPTERS, url_ftell(pb));
+ ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CHAPTERS, avio_tell(pb));
if (ret < 0) return ret;
chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
return 0;
}
+static void mkv_write_simpletag(AVIOContext *pb, AVMetadataTag *t)
+{
+ uint8_t *key = av_strdup(t->key);
+ uint8_t *p = key;
+ const uint8_t *lang = NULL;
+ ebml_master tag;
+
+ if ((p = strrchr(p, '-')) &&
+ (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
+ *p = 0;
+
+ p = key;
+ while (*p) {
+ if (*p == ' ')
+ *p = '_';
+ else if (*p >= 'a' && *p <= 'z')
+ *p -= 'a' - 'A';
+ p++;
+ }
+
+ tag = start_ebml_master(pb, MATROSKA_ID_SIMPLETAG, 0);
+ put_ebml_string(pb, MATROSKA_ID_TAGNAME, key);
+ if (lang)
+ put_ebml_string(pb, MATROSKA_ID_TAGLANG, lang);
+ put_ebml_string(pb, MATROSKA_ID_TAGSTRING, t->value);
+ end_ebml_master(pb, tag);
+
+ av_freep(&key);
+}
+
+static int mkv_write_tag(AVFormatContext *s, AVMetadata *m, unsigned int elementid,
+ unsigned int uid, ebml_master *tags)
+{
+ MatroskaMuxContext *mkv = s->priv_data;
+ ebml_master tag, targets;
+ AVMetadataTag *t = NULL;
+ int ret;
+
+ if (!tags->pos) {
+ ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TAGS, avio_tell(s->pb));
+ if (ret < 0) return ret;
+
+ *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
+ }
+
+ tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
+ targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
+ if (elementid)
+ put_ebml_uint(s->pb, elementid, uid);
+ end_ebml_master(s->pb, targets);
+
+ while ((t = av_metadata_get(m, "", t, AV_METADATA_IGNORE_SUFFIX)))
+ if (strcasecmp(t->key, "title"))
+ mkv_write_simpletag(s->pb, t);
+
+ end_ebml_master(s->pb, tag);
+ return 0;
+}
+
+static int mkv_write_tags(AVFormatContext *s)
+{
+ ebml_master tags = {0};
+ int i, ret;
+
+ ff_metadata_conv_ctx(s, ff_mkv_metadata_conv, NULL);
+
+ if (av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX)) {
+ ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
+ if (ret < 0) return ret;
+ }
+
+ for (i = 0; i < s->nb_streams; i++) {
+ AVStream *st = s->streams[i];
+
+ if (!av_metadata_get(st->metadata, "", 0, AV_METADATA_IGNORE_SUFFIX))
+ continue;
+
+ ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
+ if (ret < 0) return ret;
+ }
+
+ for (i = 0; i < s->nb_chapters; i++) {
+ AVChapter *ch = s->chapters[i];
+
+ if (!av_metadata_get(ch->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
+ continue;
+
+ ret = mkv_write_tag(s, ch->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID, ch->id, &tags);
+ if (ret < 0) return ret;
+ }
+
+ if (tags.pos)
+ end_ebml_master(s->pb, tags);
+ return 0;
+}
+
static int mkv_write_header(AVFormatContext *s)
{
MatroskaMuxContext *mkv = s->priv_data;
- ByteIOContext *pb = s->pb;
+ AVIOContext *pb = s->pb;
ebml_master ebml_header, segment_info;
AVMetadataTag *tag;
int ret, i;
end_ebml_master(pb, ebml_header);
mkv->segment = start_ebml_master(pb, MATROSKA_ID_SEGMENT, 0);
- mkv->segment_offset = url_ftell(pb);
+ mkv->segment_offset = avio_tell(pb);
// we write 2 seek heads - one at the end of the file to point to each
// cluster, and one at the beginning to point to all other level one
if (!mkv->main_seekhead)
return AVERROR(ENOMEM);
- ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_INFO, url_ftell(pb));
+ ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_INFO, avio_tell(pb));
if (ret < 0) return ret;
segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
// reserve space for the duration
mkv->duration = 0;
- mkv->duration_offset = url_ftell(pb);
+ mkv->duration_offset = avio_tell(pb);
put_ebml_void(pb, 11); // assumes double-precision float to be written
end_ebml_master(pb, segment_info);
if (mkv->mode != MODE_WEBM) {
ret = mkv_write_chapters(s);
if (ret < 0) return ret;
+
+ ret = mkv_write_tags(s);
+ if (ret < 0) return ret;
}
if (url_is_streamed(s->pb))
mkv->cur_audio_pkt.size = 0;
mkv->audio_buffer_size = 0;
- put_flush_packet(pb);
+ avio_flush(pb);
return 0;
}
return end - start;
}
-static int mkv_write_ass_blocks(AVFormatContext *s, ByteIOContext *pb, AVPacket *pkt)
+static int mkv_write_ass_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
{
MatroskaMuxContext *mkv = s->priv_data;
int i, layer = 0, max_duration = 0, size, line_size, data_size = pkt->size;
av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
"pts %" PRId64 ", duration %d\n",
- url_ftell(pb), size, pkt->pts, duration);
+ avio_tell(pb), size, pkt->pts, duration);
blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(size));
put_ebml_id(pb, MATROSKA_ID_BLOCK);
put_ebml_num(pb, size+4, 0);
- put_byte(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
- put_be16(pb, pkt->pts - mkv->cluster_pts);
- put_byte(pb, 0);
- put_buffer(pb, buffer, size);
+ avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
+ avio_wb16(pb, pkt->pts - mkv->cluster_pts);
+ avio_w8(pb, 0);
+ avio_write(pb, buffer, size);
put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);
end_ebml_master(pb, blockgroup);
return max_duration;
}
-static void mkv_write_block(AVFormatContext *s, ByteIOContext *pb,
+static void mkv_write_block(AVFormatContext *s, AVIOContext *pb,
unsigned int blockid, AVPacket *pkt, int flags)
{
MatroskaMuxContext *mkv = s->priv_data;
av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
"pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
- url_ftell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
+ avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
if (codec->codec_id == CODEC_ID_H264 && codec->extradata_size > 0 &&
(AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
data = pkt->data;
put_ebml_id(pb, blockid);
put_ebml_num(pb, size+4, 0);
- put_byte(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
- put_be16(pb, ts - mkv->cluster_pts);
- put_byte(pb, flags);
- put_buffer(pb, data, size);
+ avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
+ avio_wb16(pb, ts - mkv->cluster_pts);
+ avio_w8(pb, flags);
+ avio_write(pb, data, size);
if (data != pkt->data)
av_free(data);
}
+static int srt_get_duration(uint8_t **buf)
+{
+ int i, duration = 0;
+
+ for (i=0; i<2 && !duration; i++) {
+ int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec;
+ if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
+ &s_hour, &s_min, &s_sec, &s_hsec,
+ &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
+ s_min += 60*s_hour; e_min += 60*e_hour;
+ s_sec += 60*s_min; e_sec += 60*e_min;
+ s_hsec += 1000*s_sec; e_hsec += 1000*e_sec;
+ duration = e_hsec - s_hsec;
+ }
+ *buf += strcspn(*buf, "\n") + 1;
+ }
+ return duration;
+}
+
+static int mkv_write_srt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
+{
+ ebml_master blockgroup;
+ AVPacket pkt2 = *pkt;
+ int64_t duration = srt_get_duration(&pkt2.data);
+ pkt2.size -= pkt2.data - pkt->data;
+
+ blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,
+ mkv_blockgroup_size(pkt2.size));
+ mkv_write_block(s, pb, MATROSKA_ID_BLOCK, &pkt2, 0);
+ put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);
+ end_ebml_master(pb, blockgroup);
+
+ return duration;
+}
+
static void mkv_flush_dynbuf(AVFormatContext *s)
{
MatroskaMuxContext *mkv = s->priv_data;
return;
bufsize = url_close_dyn_buf(mkv->dyn_bc, &dyn_buf);
- put_buffer(s->pb, dyn_buf, bufsize);
+ avio_write(s->pb, dyn_buf, bufsize);
av_free(dyn_buf);
mkv->dyn_bc = NULL;
}
static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
{
MatroskaMuxContext *mkv = s->priv_data;
- ByteIOContext *pb = s->pb;
+ AVIOContext *pb = s->pb;
AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
int duration = pkt->duration;
}
if (!mkv->cluster_pos) {
- mkv->cluster_pos = url_ftell(s->pb);
+ mkv->cluster_pos = avio_tell(s->pb);
mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER, 0);
put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, FFMAX(0, ts));
mkv->cluster_pts = FFMAX(0, ts);
mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
} else if (codec->codec_id == CODEC_ID_SSA) {
duration = mkv_write_ass_blocks(s, pb, pkt);
+ } else if (codec->codec_id == CODEC_ID_SRT) {
+ duration = mkv_write_srt_blocks(s, pb, pkt);
} else {
ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(pkt->size));
duration = pkt->convergence_duration;
static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
{
MatroskaMuxContext *mkv = s->priv_data;
- ByteIOContext *pb = url_is_streamed(s->pb) ? mkv->dyn_bc : s->pb;
+ AVIOContext *pb = url_is_streamed(s->pb) ? mkv->dyn_bc : s->pb;
AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
int ret, keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
- int cluster_size = url_ftell(pb) - (url_is_streamed(s->pb) ? 0 : mkv->cluster_pos);
+ int cluster_size = avio_tell(pb) - (url_is_streamed(s->pb) ? 0 : mkv->cluster_pos);
// start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
// after 4k and on a keyframe
|| cluster_size > 5*1024*1024 || ts > mkv->cluster_pts + 5000
|| (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe && cluster_size > 4*1024))) {
av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64
- " bytes, pts %" PRIu64 "\n", url_ftell(pb), ts);
+ " bytes, pts %" PRIu64 "\n", avio_tell(pb), ts);
end_ebml_master(pb, mkv->cluster);
mkv->cluster_pos = 0;
if (mkv->dyn_bc)
static int mkv_write_trailer(AVFormatContext *s)
{
MatroskaMuxContext *mkv = s->priv_data;
- ByteIOContext *pb = s->pb;
+ AVIOContext *pb = s->pb;
int64_t currentpos, cuespos;
int ret;
// update the duration
av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
- currentpos = url_ftell(pb);
- url_fseek(pb, mkv->duration_offset, SEEK_SET);
+ currentpos = avio_tell(pb);
+ avio_seek(pb, mkv->duration_offset, SEEK_SET);
put_ebml_float(pb, MATROSKA_ID_DURATION, mkv->duration);
- url_fseek(pb, currentpos, SEEK_SET);
+ avio_seek(pb, currentpos, SEEK_SET);
}
end_ebml_master(pb, mkv->segment);
av_free(mkv->tracks);
av_destruct_packet(&mkv->cur_audio_pkt);
- put_flush_packet(pb);
+ avio_flush(pb);
return 0;
}
#if CONFIG_MATROSKA_MUXER
-AVOutputFormat matroska_muxer = {
+AVOutputFormat ff_matroska_muxer = {
"matroska",
NULL_IF_CONFIG_SMALL("Matroska file format"),
"video/x-matroska",
#endif
#if CONFIG_WEBM_MUXER
-AVOutputFormat webm_muxer = {
+AVOutputFormat ff_webm_muxer = {
"webm",
NULL_IF_CONFIG_SMALL("WebM file format"),
"video/webm",
#endif
#if CONFIG_MATROSKA_AUDIO_MUXER
-AVOutputFormat matroska_audio_muxer = {
+AVOutputFormat ff_matroska_audio_muxer = {
"matroska",
NULL_IF_CONFIG_SMALL("Matroska file format"),
"audio/x-matroska",