- return -1;
-}
-
-/*
- * Put a packet into our internal queue. Will be delivered to the
- * user/application during the next get_packet() call.
- */
-
-static void
-matroska_queue_packet (MatroskaDemuxContext *matroska,
- AVPacket *pkt)
-{
- matroska->packets =
- av_realloc(matroska->packets, (matroska->num_packets + 1) *
- sizeof(AVPacket *));
- matroska->packets[matroska->num_packets] = pkt;
- matroska->num_packets++;
-}
-
-/*
- * Autodetecting...
- */
-
-static int
-matroska_probe (AVProbeData *p)
-{
- uint64_t total = 0;
- int len_mask = 0x80, size = 1, n = 1;
- uint8_t probe_data[] = { 'm', 'a', 't', 'r', 'o', 's', 'k', 'a' };
-
- if (p->buf_size < 5)
- return 0;
-
- /* ebml header? */
- if ((p->buf[0] << 24 | p->buf[1] << 16 |
- p->buf[2] << 8 | p->buf[3]) != EBML_ID_HEADER)
- return 0;
-
- /* length of header */
- total = p->buf[4];
- while (size <= 8 && !(total & len_mask)) {
- size++;
- len_mask >>= 1;
- }
- if (size > 8)
- return 0;
- total &= (len_mask - 1);
- while (n < size)
- total = (total << 8) | p->buf[4 + n++];
-
- /* does the probe data contain the whole header? */
- if (p->buf_size < 4 + size + total)
- return 0;
-
- /* the header must contain the document type 'matroska'. For now,
- * we don't parse the whole header but simply check for the
- * availability of that array of characters inside the header.
- * Not fully fool-proof, but good enough. */
- for (n = 4 + size; n < 4 + size + total - sizeof(probe_data); n++)
- if (!memcmp (&p->buf[n], probe_data, sizeof(probe_data)))
- return AVPROBE_SCORE_MAX;
-
- return 0;
-}
-
-/*
- * From here on, it's all XML-style DTD stuff... Needs no comments.
- */
-
-static int
-matroska_parse_info (MatroskaDemuxContext *matroska)
-{
- int res = 0;
- uint32_t id;
-
- av_log(matroska->ctx, AV_LOG_DEBUG, "Parsing info...\n");
-
- while (res == 0) {
- if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- /* cluster timecode */
- case MATROSKA_ID_TIMECODESCALE: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
- break;
- matroska->time_scale = num;
- break;
- }
-
- case MATROSKA_ID_DURATION: {
- double num;
- if ((res = ebml_read_float(matroska, &id, &num)) < 0)
- break;
- matroska->ctx->duration = num * matroska->time_scale * 1000 / AV_TIME_BASE;
- break;
- }
-
- case MATROSKA_ID_WRITINGAPP: {
- char *text;
- if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
- break;
- matroska->writing_app = text;
- break;
- }
-
- case MATROSKA_ID_MUXINGAPP: {
- char *text;
- if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
- break;
- matroska->muxing_app = text;
- break;
- }
-
- case MATROSKA_ID_DATEUTC: {
- int64_t time;
- if ((res = ebml_read_date(matroska, &id, &time)) < 0)
- break;
- matroska->created = time;
- break;
- }
-
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown entry 0x%x in info header\n", id);
- /* fall-through */
-
- case EBML_ID_VOID:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
-
- return res;
-}
-
-static int
-matroska_add_stream (MatroskaDemuxContext *matroska)
-{
- int res = 0;
- uint32_t id;
- MatroskaTrack *track;
-
- av_log(matroska->ctx, AV_LOG_DEBUG, "parsing track, adding stream..,\n");
-
- /* Allocate a generic track. As soon as we know its type we'll realloc. */
- track = av_mallocz(sizeof(MatroskaTrack));
- matroska->num_tracks++;
-
- /* start with the master */
- if ((res = ebml_read_master(matroska, &id)) < 0)
- return res;
-
- /* try reading the trackentry headers */
- while (res == 0) {
- if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up > 0) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- /* track number (unique stream ID) */
- case MATROSKA_ID_TRACKNUMBER: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
- break;
- track->num = num;
- break;
- }
-
- /* track UID (unique identifier) */
- case MATROSKA_ID_TRACKUID: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
- break;
- track->uid = num;
- break;
- }
-
- /* track type (video, audio, combined, subtitle, etc.) */
- case MATROSKA_ID_TRACKTYPE: {
- uint64_t num;
- if (track->type != 0) {
- av_log(matroska->ctx, AV_LOG_INFO,
- "More than one tracktype in an entry - skip\n");
- break;
- }
- if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
- break;
- track->type = num;
-
- /* ok, so we're actually going to reallocate this thing */
- switch (track->type) {
- case MATROSKA_TRACK_TYPE_VIDEO:
- track = (MatroskaTrack *)
- av_realloc(track, sizeof(MatroskaVideoTrack));
- break;
- case MATROSKA_TRACK_TYPE_AUDIO:
- track = (MatroskaTrack *)
- av_realloc(track, sizeof(MatroskaAudioTrack));
- ((MatroskaAudioTrack *)track)->channels = 1;
- ((MatroskaAudioTrack *)track)->samplerate = 8000;
- break;
- case MATROSKA_TRACK_TYPE_SUBTITLE:
- track = (MatroskaTrack *)
- av_realloc(track, sizeof(MatroskaSubtitleTrack));
- break;
- case MATROSKA_TRACK_TYPE_COMPLEX:
- case MATROSKA_TRACK_TYPE_LOGO:
- case MATROSKA_TRACK_TYPE_CONTROL:
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown or unsupported track type 0x%x\n",
- track->type);
- track->type = 0;
- break;
- }
- matroska->tracks[matroska->num_tracks - 1] = track;
- break;
- }
-
- /* tracktype specific stuff for video */
- case MATROSKA_ID_TRACKVIDEO: {
- MatroskaVideoTrack *videotrack;
- if (track->type != MATROSKA_TRACK_TYPE_VIDEO) {
- av_log(matroska->ctx, AV_LOG_INFO,
- "video data in non-video track - ignoring\n");
- res = AVERROR_INVALIDDATA;
- break;
- } else if ((res = ebml_read_master(matroska, &id)) < 0)
- break;
- videotrack = (MatroskaVideoTrack *)track;
-
- while (res == 0) {
- if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up > 0) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- /* fixme, this should be one-up, but I get it here */
- case MATROSKA_ID_TRACKDEFAULTDURATION: {
- uint64_t num;
- if ((res = ebml_read_uint (matroska, &id,
- &num)) < 0)
- break;
- track->default_duration = num;
- break;
- }
-
- /* video framerate */
- case MATROSKA_ID_VIDEOFRAMERATE: {
- double num;
- if ((res = ebml_read_float(matroska, &id,
- &num)) < 0)
- break;
- track->default_duration = 1000000000 * (1. / num);
- break;
- }
-
- /* width of the size to display the video at */
- case MATROSKA_ID_VIDEODISPLAYWIDTH: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id,
- &num)) < 0)
- break;
- videotrack->display_width = num;
- break;
- }
-
- /* height of the size to display the video at */
- case MATROSKA_ID_VIDEODISPLAYHEIGHT: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id,
- &num)) < 0)
- break;
- videotrack->display_height = num;
- break;
- }
-
- /* width of the video in the file */
- case MATROSKA_ID_VIDEOPIXELWIDTH: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id,
- &num)) < 0)
- break;
- videotrack->pixel_width = num;
- break;
- }
-
- /* height of the video in the file */
- case MATROSKA_ID_VIDEOPIXELHEIGHT: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id,
- &num)) < 0)
- break;
- videotrack->pixel_height = num;
- break;
- }
-
- /* whether the video is interlaced */
- case MATROSKA_ID_VIDEOFLAGINTERLACED: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id,
- &num)) < 0)
- break;
- if (num)
- track->flags |=
- MATROSKA_VIDEOTRACK_INTERLACED;
- else
- track->flags &=
- ~MATROSKA_VIDEOTRACK_INTERLACED;
- break;
- }
-
- /* stereo mode (whether the video has two streams,
- * where one is for the left eye and the other for
- * the right eye, which creates a 3D-like
- * effect) */
- case MATROSKA_ID_VIDEOSTEREOMODE: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id,
- &num)) < 0)
- break;
- if (num != MATROSKA_EYE_MODE_MONO &&
- num != MATROSKA_EYE_MODE_LEFT &&
- num != MATROSKA_EYE_MODE_RIGHT &&
- num != MATROSKA_EYE_MODE_BOTH) {
- av_log(matroska->ctx, AV_LOG_INFO,
- "Ignoring unknown eye mode 0x%x\n",
- (uint32_t) num);
- break;
- }
- videotrack->eye_mode = num;
- break;
- }
-
- /* aspect ratio behaviour */
- case MATROSKA_ID_VIDEOASPECTRATIO: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id,
- &num)) < 0)
- break;
- if (num != MATROSKA_ASPECT_RATIO_MODE_FREE &&
- num != MATROSKA_ASPECT_RATIO_MODE_KEEP &&
- num != MATROSKA_ASPECT_RATIO_MODE_FIXED) {
- av_log(matroska->ctx, AV_LOG_INFO,
- "Ignoring unknown aspect ratio 0x%x\n",
- (uint32_t) num);
- break;
- }
- videotrack->ar_mode = num;
- break;
- }
-
- /* colourspace (only matters for raw video)
- * fourcc */
- case MATROSKA_ID_VIDEOCOLOURSPACE: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id,
- &num)) < 0)
- break;
- videotrack->fourcc = num;
- break;
- }
-
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown video track header entry "
- "0x%x - ignoring\n", id);
- /* pass-through */
-
- case EBML_ID_VOID:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
- break;
- }
-
- /* tracktype specific stuff for audio */
- case MATROSKA_ID_TRACKAUDIO: {
- MatroskaAudioTrack *audiotrack;
- if (track->type != MATROSKA_TRACK_TYPE_AUDIO) {
- av_log(matroska->ctx, AV_LOG_INFO,
- "audio data in non-audio track - ignoring\n");
- res = AVERROR_INVALIDDATA;
- break;
- } else if ((res = ebml_read_master(matroska, &id)) < 0)
- break;
- audiotrack = (MatroskaAudioTrack *)track;
-
- while (res == 0) {
- if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up > 0) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- /* samplerate */
- case MATROSKA_ID_AUDIOSAMPLINGFREQ: {
- double num;
- if ((res = ebml_read_float(matroska, &id,
- &num)) < 0)
- break;
- audiotrack->samplerate = num;
- break;
- }
-
- /* bitdepth */
- case MATROSKA_ID_AUDIOBITDEPTH: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id,
- &num)) < 0)
- break;
- audiotrack->bitdepth = num;
- break;
- }
-
- /* channels */
- case MATROSKA_ID_AUDIOCHANNELS: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id,
- &num)) < 0)
- break;
- audiotrack->channels = num;
- break;
- }
-
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown audio track header entry "
- "0x%x - ignoring\n", id);
- /* pass-through */
-
- case EBML_ID_VOID:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
- break;
- }
-
- /* codec identifier */
- case MATROSKA_ID_CODECID: {
- char *text;
- if ((res = ebml_read_ascii(matroska, &id, &text)) < 0)
- break;
- track->codec_id = text;
- break;
- }
-
- /* codec private data */
- case MATROSKA_ID_CODECPRIVATE: {
- uint8_t *data;
- int size;
- if ((res = ebml_read_binary(matroska, &id, &data, &size) < 0))
- break;
- track->codec_priv = data;
- track->codec_priv_size = size;
- break;
- }
-
- /* name of the codec */
- case MATROSKA_ID_CODECNAME: {
- char *text;
- if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
- break;
- track->codec_name = text;
- break;
- }
-
- /* name of this track */
- case MATROSKA_ID_TRACKNAME: {
- char *text;
- if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
- break;
- track->name = text;
- break;
- }
-
- /* language (matters for audio/subtitles, mostly) */
- case MATROSKA_ID_TRACKLANGUAGE: {
- char *text;
- if ((res = ebml_read_utf8(matroska, &id, &text)) < 0)
- break;
- track->language = text;
- break;
- }
-
- /* whether this is actually used */
- case MATROSKA_ID_TRACKFLAGENABLED: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
- break;
- if (num)
- track->flags |= MATROSKA_TRACK_ENABLED;
- else
- track->flags &= ~MATROSKA_TRACK_ENABLED;
- break;
- }
-
- /* whether it's the default for this track type */
- case MATROSKA_ID_TRACKFLAGDEFAULT: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
- break;
- if (num)
- track->flags |= MATROSKA_TRACK_DEFAULT;
- else
- track->flags &= ~MATROSKA_TRACK_DEFAULT;
- break;
- }
-
- /* lacing (like MPEG, where blocks don't end/start on frame
- * boundaries) */
- case MATROSKA_ID_TRACKFLAGLACING: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
- break;
- if (num)
- track->flags |= MATROSKA_TRACK_LACING;
- else
- track->flags &= ~MATROSKA_TRACK_LACING;
- break;
- }
-
- /* default length (in time) of one data block in this track */
- case MATROSKA_ID_TRACKDEFAULTDURATION: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
- break;
- track->default_duration = num;
- break;
- }
-
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown track header entry 0x%x - ignoring\n", id);
- /* pass-through */
-
- case EBML_ID_VOID:
- /* we ignore these because they're nothing useful. */
- case MATROSKA_ID_CODECINFOURL:
- case MATROSKA_ID_CODECDOWNLOADURL:
- case MATROSKA_ID_TRACKMINCACHE:
- case MATROSKA_ID_TRACKMAXCACHE:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
-
- return res;
-}
-
-static int
-matroska_parse_tracks (MatroskaDemuxContext *matroska)
-{
- int res = 0;
- uint32_t id;
-
- av_log(matroska->ctx, AV_LOG_DEBUG, "parsing tracks...\n");
-
- while (res == 0) {
- if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- /* one track within the "all-tracks" header */
- case MATROSKA_ID_TRACKENTRY:
- res = matroska_add_stream(matroska);
- break;
-
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown entry 0x%x in track header\n", id);
- /* fall-through */
-
- case EBML_ID_VOID:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
-
- return res;
-}
-
-static int
-matroska_parse_index (MatroskaDemuxContext *matroska)
-{
- int res = 0;
- uint32_t id;
- MatroskaDemuxIndex idx;
-
- av_log(matroska->ctx, AV_LOG_DEBUG, "parsing index...\n");
-
- while (res == 0) {
- if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- /* one single index entry ('point') */
- case MATROSKA_ID_POINTENTRY:
- if ((res = ebml_read_master(matroska, &id)) < 0)
- break;
-
- /* in the end, we hope to fill one entry with a
- * timestamp, a file position and a tracknum */
- idx.pos = (uint64_t) -1;
- idx.time = (uint64_t) -1;
- idx.track = (uint16_t) -1;
-
- while (res == 0) {
- if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- /* one single index entry ('point') */
- case MATROSKA_ID_CUETIME: {
- int64_t time;
- if ((res = ebml_read_uint(matroska, &id,
- &time)) < 0)
- break;
- idx.time = time * matroska->time_scale;
- break;
- }
-
- /* position in the file + track to which it
- * belongs */
- case MATROSKA_ID_CUETRACKPOSITION:
- if ((res = ebml_read_master(matroska, &id)) < 0)
- break;
-
- while (res == 0) {
- if (!(id = ebml_peek_id (matroska,
- &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- /* track number */
- case MATROSKA_ID_CUETRACK: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska,
- &id, &num)) < 0)
- break;
- idx.track = num;
- break;
- }
-
- /* position in file */
- case MATROSKA_ID_CUECLUSTERPOSITION: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska,
- &id, &num)) < 0)
- break;
- idx.pos = num;
- break;
- }
-
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown entry 0x%x in "
- "CuesTrackPositions\n", id);
- /* fall-through */
-
- case EBML_ID_VOID:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
-
- break;
-
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown entry 0x%x in cuespoint "
- "index\n", id);
- /* fall-through */
-
- case EBML_ID_VOID:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
-
- /* so let's see if we got what we wanted */
- if (idx.pos != (uint64_t) -1 &&
- idx.time != (uint64_t) -1 &&
- idx.track != (uint16_t) -1) {
- if (matroska->num_indexes % 32 == 0) {
- /* re-allocate bigger index */
- matroska->index =
- av_realloc(matroska->index,
- (matroska->num_indexes + 32) *
- sizeof(MatroskaDemuxIndex));
- }
- matroska->index[matroska->num_indexes] = idx;
- matroska->num_indexes++;
- }
- break;
-
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown entry 0x%x in cues header\n", id);
- /* fall-through */
-
- case EBML_ID_VOID:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
-
- return res;
-}
-
-static int
-matroska_parse_metadata (MatroskaDemuxContext *matroska)
-{
- int res = 0;
- uint32_t id;
-
- while (res == 0) {
- if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- /* Hm, this is unsupported... */
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown entry 0x%x in metadata header\n", id);
- /* fall-through */
-
- case EBML_ID_VOID:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
-
- return res;
-}
-
-static int
-matroska_parse_seekhead (MatroskaDemuxContext *matroska)
-{
- int res = 0;
- uint32_t id;
-
- av_log(matroska->ctx, AV_LOG_DEBUG, "parsing seekhead...\n");
-
- while (res == 0) {
- if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- case MATROSKA_ID_SEEKENTRY: {
- uint32_t seek_id = 0, peek_id_cache = 0;
- uint64_t seek_pos = (uint64_t) -1, t;
-
- if ((res = ebml_read_master(matroska, &id)) < 0)
- break;
-
- while (res == 0) {
- if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- case MATROSKA_ID_SEEKID:
- res = ebml_read_uint(matroska, &id, &t);
- seek_id = t;
- break;
-
- case MATROSKA_ID_SEEKPOSITION:
- res = ebml_read_uint(matroska, &id, &seek_pos);
- break;
-
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown seekhead ID 0x%x\n", id);
- /* fall-through */
-
- case EBML_ID_VOID:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
-
- if (!seek_id || seek_pos == (uint64_t) -1) {
- av_log(matroska->ctx, AV_LOG_INFO,
- "Incomplete seekhead entry (0x%x/%"PRIu64")\n",
- seek_id, seek_pos);
- break;
- }
-
- switch (seek_id) {
- case MATROSKA_ID_CUES:
- case MATROSKA_ID_TAGS: {
- uint32_t level_up = matroska->level_up;
- offset_t before_pos;
- uint64_t length;
- MatroskaLevel level;
-
- /* remember the peeked ID and the current position */
- peek_id_cache = matroska->peek_id;
- before_pos = url_ftell(&matroska->ctx->pb);
-
- /* seek */
- if ((res = ebml_read_seek(matroska, seek_pos +
- matroska->segment_start)) < 0)
- return res;
-
- /* we don't want to lose our seekhead level, so we add
- * a dummy. This is a crude hack. */
- if (matroska->num_levels == EBML_MAX_DEPTH) {
- av_log(matroska->ctx, AV_LOG_INFO,
- "Max EBML element depth (%d) reached, "
- "cannot parse further.\n", EBML_MAX_DEPTH);
- return AVERROR_UNKNOWN;
- }
-
- level.start = 0;
- level.length = (uint64_t)-1;
- matroska->levels[matroska->num_levels] = level;
- matroska->num_levels++;
-
- /* check ID */
- if (!(id = ebml_peek_id (matroska,
- &matroska->level_up)))
- break;
- if (id != seek_id) {
- av_log(matroska->ctx, AV_LOG_INFO,
- "We looked for ID=0x%x but got "
- "ID=0x%x (pos=%"PRIu64")",
- seek_id, id, seek_pos +
- matroska->segment_start);
- goto finish;
- }
-
- /* read master + parse */
- if ((res = ebml_read_master(matroska, &id)) < 0)
- break;
- switch (id) {
- case MATROSKA_ID_CUES:
- if (!(res = matroska_parse_index(matroska)) ||
- url_feof(&matroska->ctx->pb)) {
- matroska->index_parsed = 1;
- res = 0;
- }
- break;
- case MATROSKA_ID_TAGS:
- if (!(res = matroska_parse_metadata(matroska)) ||
- url_feof(&matroska->ctx->pb)) {
- matroska->metadata_parsed = 1;
- res = 0;
- }
- break;
- }
- if (res < 0)
- break;
-
- finish:
- /* remove dummy level */
- while (matroska->num_levels) {
- matroska->num_levels--;
- length =
- matroska->levels[matroska->num_levels].length;
- if (length == (uint64_t)-1)
- break;
- }
-
- /* seek back */
- if ((res = ebml_read_seek(matroska, before_pos)) < 0)
- return res;
- matroska->peek_id = peek_id_cache;
- matroska->level_up = level_up;
- break;
- }
-
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Ignoring seekhead entry for ID=0x%x\n",
- seek_id);
- break;
- }
-
- break;
- }
-
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown seekhead ID 0x%x\n", id);
- /* fall-through */
-
- case EBML_ID_VOID:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
-
- return res;
-}
-
-static int
-matroska_read_header (AVFormatContext *s,
- AVFormatParameters *ap)
-{
- MatroskaDemuxContext *matroska = s->priv_data;
- char *doctype;
- int version, last_level, res = 0;
- uint32_t id;
-
- matroska->ctx = s;
-
- /* First read the EBML header. */
- doctype = NULL;
- if ((res = ebml_read_header(matroska, &doctype, &version)) < 0)
- return res;
- if ((doctype == NULL) || strcmp(doctype, "matroska")) {
- av_log(matroska->ctx, AV_LOG_ERROR,
- "Wrong EBML doctype ('%s' != 'matroska').\n",
- doctype ? doctype : "(none)");
- if (doctype)
- av_free(doctype);
- return AVERROR_NOFMT;
- }
- av_free(doctype);
- if (version != 1) {
- av_log(matroska->ctx, AV_LOG_ERROR,
- "Matroska demuxer version 1 too old for file version %d\n",
- version);
- return AVERROR_NOFMT;
- }
-
- /* The next thing is a segment. */
- while (1) {
- if (!(id = ebml_peek_id(matroska, &last_level)))
- return AVERROR_IO;
- if (id == MATROSKA_ID_SEGMENT)
- break;
-
- /* oi! */
- av_log(matroska->ctx, AV_LOG_INFO,
- "Expected a Segment ID (0x%x), but received 0x%x!\n",
- MATROSKA_ID_SEGMENT, id);
- if ((res = ebml_read_skip(matroska)) < 0)
- return res;
- }
-
- /* We now have a Matroska segment.
- * Seeks are from the beginning of the segment,
- * after the segment ID/length. */
- if ((res = ebml_read_master(matroska, &id)) < 0)
- return res;
- matroska->segment_start = url_ftell(&s->pb);
-
- matroska->time_scale = 1000000;
- /* we've found our segment, start reading the different contents in here */
- while (res == 0) {
- if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- /* stream info */
- case MATROSKA_ID_INFO: {
- if ((res = ebml_read_master(matroska, &id)) < 0)
- break;
- res = matroska_parse_info(matroska);
- break;
- }
-
- /* track info headers */
- case MATROSKA_ID_TRACKS: {
- if ((res = ebml_read_master(matroska, &id)) < 0)
- break;
- res = matroska_parse_tracks(matroska);
- break;
- }
-
- /* stream index */
- case MATROSKA_ID_CUES: {
- if (!matroska->index_parsed) {
- if ((res = ebml_read_master(matroska, &id)) < 0)
- break;
- res = matroska_parse_index(matroska);
- } else
- res = ebml_read_skip(matroska);
- break;
- }
-
- /* metadata */
- case MATROSKA_ID_TAGS: {
- if (!matroska->metadata_parsed) {
- if ((res = ebml_read_master(matroska, &id)) < 0)
- break;
- res = matroska_parse_metadata(matroska);
- } else
- res = ebml_read_skip(matroska);
- break;
- }
-
- /* file index (if seekable, seek to Cues/Tags to parse it) */
- case MATROSKA_ID_SEEKHEAD: {
- if ((res = ebml_read_master(matroska, &id)) < 0)
- break;
- res = matroska_parse_seekhead(matroska);
- break;
- }
-
- case MATROSKA_ID_CLUSTER: {
- /* Do not read the master - this will be done in the next
- * call to matroska_read_packet. */
- res = 1;
- break;
- }
-
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown matroska file header ID 0x%x\n", id);
- /* fall-through */
-
- case EBML_ID_VOID:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
-
- if (res < 0)
- return res;
-
- /* Have we found a cluster? */
- if (res == 1) {
- int i, j;
- enum CodecID codec_id= CODEC_ID_NONE;
- MatroskaTrack *track;
- AVStream *st;
-
- for (i = 0; i < matroska->num_tracks; i++) {
- void *extradata = NULL;
- int extradata_size = 0;
- track = matroska->tracks[i];
-
- /* libavformat does not really support subtitles.
- * Also apply some sanity checks. */
- if ((track->type == MATROSKA_TRACK_TYPE_SUBTITLE) ||
- (track->codec_id == NULL))
- continue;
-
- for(j=0; codec_tags[j].str; j++){
- if(!strcmp(codec_tags[j].str, track->codec_id)){
- codec_id= codec_tags[j].id;
- break;
- }
- }
-
- /* Set the FourCC from the CodecID. */
- /* This is the MS compatibility mode which stores a
- * BITMAPINFOHEADER in the CodecPrivate. */
- if (!strcmp(track->codec_id,
- MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC) &&
- (track->codec_priv_size >= 40) &&
- (track->codec_priv != NULL)) {
- unsigned char *p;
-
- /* Offset of biCompression. Stored in LE. */
- p = (unsigned char *)track->codec_priv + 16;
- ((MatroskaVideoTrack *)track)->fourcc = (p[3] << 24) |
- (p[2] << 16) | (p[1] << 8) | p[0];
- codec_id = codec_get_bmp_id(((MatroskaVideoTrack *)track)->fourcc);
-
- }
-
- /* This is the MS compatibility mode which stores a
- * WAVEFORMATEX in the CodecPrivate. */
- else if (!strcmp(track->codec_id,
- MATROSKA_CODEC_ID_AUDIO_ACM) &&
- (track->codec_priv_size >= 18) &&
- (track->codec_priv != NULL)) {
- unsigned char *p;
- uint16_t tag;
-
- /* Offset of wFormatTag. Stored in LE. */
- p = (unsigned char *)track->codec_priv;
- tag = (p[1] << 8) | p[0];
- codec_id = codec_get_wav_id(tag);
-
- }
-
- if (codec_id == CODEC_ID_NONE) {
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown/unsupported CodecID %s.\n",
- track->codec_id);
- }
-
- track->stream_index = matroska->num_streams;
-
- matroska->num_streams++;
- st = av_new_stream(s, track->stream_index);
- if (st == NULL)
- return AVERROR_NOMEM;
- av_set_pts_info(st, 64, matroska->time_scale, 1000*1000*1000); /* 64 bit pts in ns */
-
- st->codec->codec_id = codec_id;
-
- if(extradata){
- st->codec->extradata = extradata;
- st->codec->extradata_size = extradata_size;
- } else if(track->codec_priv && track->codec_priv_size > 0){
- st->codec->extradata = av_malloc(track->codec_priv_size);
- if(st->codec->extradata == NULL)
- return AVERROR_NOMEM;
- st->codec->extradata_size = track->codec_priv_size;
- memcpy(st->codec->extradata, track->codec_priv,
- track->codec_priv_size);
- }
-
- if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
- MatroskaVideoTrack *videotrack = (MatroskaVideoTrack *)track;
-
- st->codec->codec_type = CODEC_TYPE_VIDEO;
- st->codec->codec_tag = videotrack->fourcc;
- st->codec->width = videotrack->pixel_width;
- st->codec->height = videotrack->pixel_height;
- if (videotrack->display_width == 0)
- videotrack->display_width= videotrack->pixel_width;
- if (videotrack->display_height == 0)
- videotrack->display_height= videotrack->pixel_height;
- av_reduce(&st->codec->sample_aspect_ratio.num,
- &st->codec->sample_aspect_ratio.den,
- st->codec->height * videotrack->display_width,
- st->codec-> width * videotrack->display_height,
- 255);
- } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) {
- MatroskaAudioTrack *audiotrack = (MatroskaAudioTrack *)track;
-
- st->codec->codec_type = CODEC_TYPE_AUDIO;
- st->codec->sample_rate = audiotrack->samplerate;
- st->codec->channels = audiotrack->channels;
- } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
- st->codec->codec_type = CODEC_TYPE_SUBTITLE;
- }
-
- /* What do we do with private data? E.g. for Vorbis. */
- }
- }
-
- return 0;
-}
-
-static int
-matroska_find_track_by_num (MatroskaDemuxContext *matroska,
- int num)
-{
- int i;
-
- for (i = 0; i < matroska->num_tracks; i++)
- if (matroska->tracks[i]->num == num)
- return i;
-
- return -1;
-}
-
-static int
-matroska_parse_blockgroup (MatroskaDemuxContext *matroska,
- uint64_t cluster_time)
-{
- int res = 0;
- uint32_t id;
- AVPacket *pkt;
- int is_keyframe = PKT_FLAG_KEY, last_num_packets = matroska->num_packets;
-
- av_log(matroska->ctx, AV_LOG_DEBUG, "parsing blockgroup...\n");
-
- while (res == 0) {
- if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- /* one block inside the group. Note, block parsing is one
- * of the harder things, so this code is a bit complicated.
- * See http://www.matroska.org/ for documentation. */
- case MATROSKA_ID_BLOCK: {
- uint8_t *data, *origdata;
- int size;
- int16_t block_time;
- uint32_t *lace_size = NULL;
- int n, track, flags, laces = 0;
- uint64_t num;
- int64_t pos= url_ftell(&matroska->ctx->pb);
-
- if ((res = ebml_read_binary(matroska, &id, &data, &size)) < 0)
- break;
- origdata = data;
-
- /* first byte(s): blocknum */
- if ((n = matroska_ebmlnum_uint(data, size, &num)) < 0) {
- av_log(matroska->ctx, AV_LOG_ERROR,
- "EBML block data error\n");
- av_free(origdata);
- break;
- }
- data += n;
- size -= n;
-
- /* fetch track from num */
- track = matroska_find_track_by_num(matroska, num);
- if (size <= 3 || track < 0 || track >= matroska->num_tracks) {
- av_log(matroska->ctx, AV_LOG_INFO,
- "Invalid stream %d or size %u\n", track, size);
- av_free(origdata);
- break;
- }
- if(matroska->ctx->streams[ matroska->tracks[track]->stream_index ]->discard >= AVDISCARD_ALL){
- av_free(origdata);
- break;
- }
-
- /* block_time (relative to cluster time) */
- block_time = (data[0] << 8) | data[1];
- data += 2;
- size -= 2;
- flags = *data;
- data += 1;
- size -= 1;
- switch ((flags & 0x06) >> 1) {
- case 0x0: /* no lacing */
- laces = 1;
- lace_size = av_mallocz(sizeof(int));
- lace_size[0] = size;
- break;
-
- case 0x1: /* xiph lacing */
- case 0x2: /* fixed-size lacing */
- case 0x3: /* EBML lacing */
- if (size == 0) {
- res = -1;
- break;
- }
- laces = (*data) + 1;
- data += 1;
- size -= 1;
- lace_size = av_mallocz(laces * sizeof(int));
-
- switch ((flags & 0x06) >> 1) {
- case 0x1: /* xiph lacing */ {
- uint8_t temp;
- uint32_t total = 0;
- for (n = 0; res == 0 && n < laces - 1; n++) {
- while (1) {
- if (size == 0) {
- res = -1;
- break;
- }
- temp = *data;
- lace_size[n] += temp;
- data += 1;
- size -= 1;
- if (temp != 0xff)
- break;
- }
- total += lace_size[n];
- }
- lace_size[n] = size - total;
- break;
- }
-
- case 0x2: /* fixed-size lacing */
- for (n = 0; n < laces; n++)
- lace_size[n] = size / laces;
- break;
-
- case 0x3: /* EBML lacing */ {
- uint32_t total;
- n = matroska_ebmlnum_uint(data, size, &num);
- if (n < 0) {
- av_log(matroska->ctx, AV_LOG_INFO,
- "EBML block data error\n");
- break;
- }
- data += n;
- size -= n;
- total = lace_size[0] = num;
- for (n = 1; res == 0 && n < laces - 1; n++) {
- int64_t snum;
- int r;
- r = matroska_ebmlnum_sint (data, size,
- &snum);
- if (r < 0) {
- av_log(matroska->ctx, AV_LOG_INFO,
- "EBML block data error\n");
- break;
- }
- data += r;
- size -= r;
- lace_size[n] = lace_size[n - 1] + snum;
- total += lace_size[n];
- }
- lace_size[n] = size - total;
- break;
- }
- }
- break;
- }
-
- if (res == 0) {
- for (n = 0; n < laces; n++) {
- uint64_t timecode = AV_NOPTS_VALUE;
-
- pkt = av_mallocz(sizeof(AVPacket));
- /* XXX: prevent data copy... */
- if (av_new_packet(pkt,lace_size[n]) < 0) {
- res = AVERROR_NOMEM;
- break;
- }
- if (cluster_time != (uint64_t)-1 && n == 0) {
- if (cluster_time + block_time >= 0)
- timecode = cluster_time + block_time;
- }
- /* FIXME: duration */
-
- memcpy(pkt->data, data, lace_size[n]);
- data += lace_size[n];
- if (n == 0)
- pkt->flags = is_keyframe;
- pkt->stream_index =
- matroska->tracks[track]->stream_index;
-
- pkt->pts = timecode;
- pkt->pos= pos;
-
- matroska_queue_packet(matroska, pkt);
- }
- }
-
- av_free(lace_size);
- av_free(origdata);
- break;
- }
-
- case MATROSKA_ID_BLOCKDURATION: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
- break;
- av_log(matroska->ctx, AV_LOG_INFO,
- "FIXME: implement support for BlockDuration\n");
- break;
- }
-
- case MATROSKA_ID_BLOCKREFERENCE:
- /* We've found a reference, so not even the first frame in
- * the lace is a key frame. */
- is_keyframe = 0;
- if (last_num_packets != matroska->num_packets)
- matroska->packets[last_num_packets]->flags = 0;
- res = ebml_read_skip(matroska);
- break;
-
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown entry 0x%x in blockgroup data\n", id);
- /* fall-through */
-
- case EBML_ID_VOID:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
-
- return res;
-}
-
-static int
-matroska_parse_cluster (MatroskaDemuxContext *matroska)
-{
- int res = 0;
- uint32_t id;
- uint64_t cluster_time = 0;
-
- av_log(matroska->ctx, AV_LOG_DEBUG,
- "parsing cluster at %"PRId64"\n", url_ftell(&matroska->ctx->pb));
-
- while (res == 0) {
- if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- /* cluster timecode */
- case MATROSKA_ID_CLUSTERTIMECODE: {
- uint64_t num;
- if ((res = ebml_read_uint(matroska, &id, &num)) < 0)
- break;
- cluster_time = num;
- break;
- }
-
- /* a group of blocks inside a cluster */
- case MATROSKA_ID_BLOCKGROUP:
- if ((res = ebml_read_master(matroska, &id)) < 0)
- break;
- res = matroska_parse_blockgroup(matroska, cluster_time);
- break;
-
- default:
- av_log(matroska->ctx, AV_LOG_INFO,
- "Unknown entry 0x%x in cluster data\n", id);
- /* fall-through */
-
- case EBML_ID_VOID:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
-
- return res;
-}
-
-static int
-matroska_read_packet (AVFormatContext *s,
- AVPacket *pkt)
-{
- MatroskaDemuxContext *matroska = s->priv_data;
- int res = 0;
- uint32_t id;
-
- /* Do we still have a packet queued? */
- if (matroska_deliver_packet(matroska, pkt) == 0)
- return 0;
-
- /* Have we already reached the end? */
- if (matroska->done)
- return AVERROR_IO;
-
- while (res == 0) {
- if (!(id = ebml_peek_id(matroska, &matroska->level_up))) {
- res = AVERROR_IO;
- break;
- } else if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
-
- switch (id) {
- case MATROSKA_ID_CLUSTER:
- if ((res = ebml_read_master(matroska, &id)) < 0)
- break;
- if ((res = matroska_parse_cluster(matroska)) == 0)
- res = 1; /* Parsed one cluster, let's get out. */
- break;
-
- default:
- case EBML_ID_VOID:
- res = ebml_read_skip(matroska);
- break;
- }
-
- if (matroska->level_up) {
- matroska->level_up--;
- break;
- }
- }
-
- if (res == -1)
- matroska->done = 1;
-
- return matroska_deliver_packet(matroska, pkt);
-}
-
-static int
-matroska_read_close (AVFormatContext *s)
-{
- MatroskaDemuxContext *matroska = s->priv_data;
- int n = 0;
-
- if (matroska->writing_app)
- av_free(matroska->writing_app);
- if (matroska->muxing_app)
- av_free(matroska->muxing_app);
- if (matroska->index)
- av_free(matroska->index);
-
- if (matroska->packets != NULL) {
- for (n = 0; n < matroska->num_packets; n++) {
- av_free_packet(matroska->packets[n]);
- av_free(matroska->packets[n]);
- }
- av_free(matroska->packets);
- }
-
- for (n = 0; n < matroska->num_tracks; n++) {
- MatroskaTrack *track = matroska->tracks[n];
- if (track->codec_id)
- av_free(track->codec_id);
- if (track->codec_name)
- av_free(track->codec_name);
- if (track->codec_priv)
- av_free(track->codec_priv);
- if (track->name)
- av_free(track->name);
- if (track->language)
- av_free(track->language);
-
- av_free(track);
- }
-
- memset(matroska, 0, sizeof(MatroskaDemuxContext));
-
- return 0;
-}