David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1 | /* |
Aurelien Jacobs | ff33c5c | 2008-08-05 00:42:43 | [diff] [blame] | 2 | * Matroska file demuxer |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 3 | * Copyright (c) 2003-2008 The FFmpeg Project |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 4 | * |
| 5 | * This file is part of FFmpeg. |
| 6 | * |
| 7 | * FFmpeg is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * FFmpeg is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with FFmpeg; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | /** |
Reimar Döffinger | 6de4aec | 2007-06-20 17:37:11 | [diff] [blame] | 23 | * @file matroskadec.c |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 24 | * Matroska file demuxer |
| 25 | * by Ronald Bultje <[email protected]> |
| 26 | * with a little help from Moritz Bunkus <[email protected]> |
Aurelien Jacobs | ff33c5c | 2008-08-05 00:42:43 | [diff] [blame] | 27 | * totally reworked by Aurelien Jacobs <[email protected]> |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 28 | * Specs available on the Matroska project page: http://www.matroska.org/. |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 29 | */ |
| 30 | |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 31 | #include <stdio.h> |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 32 | #include "avformat.h" |
| 33 | /* For codec_get_id(). */ |
| 34 | #include "riff.h" |
Aurelien Jacobs | f009e36 | 2008-07-27 15:11:04 | [diff] [blame] | 35 | #include "isom.h" |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 36 | #include "matroska.h" |
Aurelien Jacobs | 7bfacd4 | 2008-04-02 21:41:48 | [diff] [blame] | 37 | #include "libavcodec/mpeg4audio.h" |
Diego Biurrun | 245976d | 2008-05-09 11:56:36 | [diff] [blame] | 38 | #include "libavutil/intfloat_readwrite.h" |
Diego Biurrun | 6a5d31a | 2009-01-11 22:19:48 | [diff] [blame] | 39 | #include "libavutil/intreadwrite.h" |
Aurelien Jacobs | 5f8e022 | 2008-08-05 00:39:47 | [diff] [blame] | 40 | #include "libavutil/avstring.h" |
Aurelien Jacobs | de3230f | 2008-05-09 01:53:59 | [diff] [blame] | 41 | #include "libavutil/lzo.h" |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame^] | 42 | #if CONFIG_ZLIB |
Aurelien Jacobs | fbb878c | 2008-05-13 23:32:13 | [diff] [blame] | 43 | #include <zlib.h> |
| 44 | #endif |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame^] | 45 | #if CONFIG_BZLIB |
Aurelien Jacobs | 54dddf0 | 2008-05-15 23:12:41 | [diff] [blame] | 46 | #include <bzlib.h> |
| 47 | #endif |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 48 | |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 49 | typedef enum { |
| 50 | EBML_NONE, |
| 51 | EBML_UINT, |
| 52 | EBML_FLOAT, |
| 53 | EBML_STR, |
| 54 | EBML_UTF8, |
| 55 | EBML_BIN, |
| 56 | EBML_NEST, |
| 57 | EBML_PASS, |
| 58 | EBML_STOP, |
| 59 | } EbmlType; |
| 60 | |
| 61 | typedef const struct EbmlSyntax { |
| 62 | uint32_t id; |
| 63 | EbmlType type; |
| 64 | int list_elem_size; |
| 65 | int data_offset; |
| 66 | union { |
| 67 | uint64_t u; |
| 68 | double f; |
| 69 | const char *s; |
| 70 | const struct EbmlSyntax *n; |
| 71 | } def; |
| 72 | } EbmlSyntax; |
| 73 | |
| 74 | typedef struct { |
| 75 | int nb_elem; |
| 76 | void *elem; |
| 77 | } EbmlList; |
| 78 | |
| 79 | typedef struct { |
| 80 | int size; |
| 81 | uint8_t *data; |
| 82 | int64_t pos; |
| 83 | } EbmlBin; |
| 84 | |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 85 | typedef struct { |
| 86 | uint64_t version; |
| 87 | uint64_t max_size; |
| 88 | uint64_t id_length; |
| 89 | char *doctype; |
| 90 | uint64_t doctype_version; |
| 91 | } Ebml; |
| 92 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 93 | typedef struct { |
| 94 | uint64_t algo; |
| 95 | EbmlBin settings; |
| 96 | } MatroskaTrackCompression; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 97 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 98 | typedef struct { |
| 99 | uint64_t scope; |
| 100 | uint64_t type; |
| 101 | MatroskaTrackCompression compression; |
| 102 | } MatroskaTrackEncoding; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 103 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 104 | typedef struct { |
| 105 | double frame_rate; |
| 106 | uint64_t display_width; |
| 107 | uint64_t display_height; |
| 108 | uint64_t pixel_width; |
| 109 | uint64_t pixel_height; |
| 110 | uint64_t fourcc; |
| 111 | } MatroskaTrackVideo; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 112 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 113 | typedef struct { |
| 114 | double samplerate; |
| 115 | double out_samplerate; |
| 116 | uint64_t bitdepth; |
| 117 | uint64_t channels; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 118 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 119 | /* real audio header (extracted from extradata) */ |
| 120 | int coded_framesize; |
| 121 | int sub_packet_h; |
| 122 | int frame_size; |
| 123 | int sub_packet_size; |
| 124 | int sub_packet_cnt; |
| 125 | int pkt_cnt; |
| 126 | uint8_t *buf; |
| 127 | } MatroskaTrackAudio; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 128 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 129 | typedef struct { |
| 130 | uint64_t num; |
| 131 | uint64_t type; |
| 132 | char *codec_id; |
| 133 | EbmlBin codec_priv; |
| 134 | char *language; |
Anton Khirnov | 7ff9708 | 2008-06-01 13:54:11 | [diff] [blame] | 135 | double time_scale; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 136 | uint64_t default_duration; |
Aurelien Jacobs | 4eff974 | 2008-08-05 00:39:53 | [diff] [blame] | 137 | uint64_t flag_default; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 138 | MatroskaTrackVideo video; |
| 139 | MatroskaTrackAudio audio; |
| 140 | EbmlList encodings; |
Aurelien Jacobs | fc4d335 | 2008-08-05 00:40:06 | [diff] [blame] | 141 | |
| 142 | AVStream *stream; |
Aurelien Jacobs | 82360e6 | 2008-09-09 12:07:10 | [diff] [blame] | 143 | int64_t end_timecode; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 144 | } MatroskaTrack; |
| 145 | |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 146 | typedef struct { |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 147 | char *filename; |
| 148 | char *mime; |
| 149 | EbmlBin bin; |
| 150 | } MatroskaAttachement; |
| 151 | |
| 152 | typedef struct { |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 153 | uint64_t start; |
| 154 | uint64_t end; |
| 155 | uint64_t uid; |
| 156 | char *title; |
| 157 | } MatroskaChapter; |
| 158 | |
| 159 | typedef struct { |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 160 | uint64_t track; |
| 161 | uint64_t pos; |
| 162 | } MatroskaIndexPos; |
| 163 | |
| 164 | typedef struct { |
| 165 | uint64_t time; |
| 166 | EbmlList pos; |
| 167 | } MatroskaIndex; |
| 168 | |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 169 | typedef struct { |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 170 | char *name; |
| 171 | char *string; |
| 172 | EbmlList sub; |
| 173 | } MatroskaTag; |
| 174 | |
| 175 | typedef struct { |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 176 | uint64_t id; |
| 177 | uint64_t pos; |
| 178 | } MatroskaSeekhead; |
| 179 | |
Aurelien Jacobs | c171af9 | 2008-08-05 00:41:19 | [diff] [blame] | 180 | typedef struct { |
Aurelien Jacobs | 8d75b5a | 2007-06-04 22:35:16 | [diff] [blame] | 181 | uint64_t start; |
| 182 | uint64_t length; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 183 | } MatroskaLevel; |
| 184 | |
Aurelien Jacobs | c171af9 | 2008-08-05 00:41:19 | [diff] [blame] | 185 | typedef struct { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 186 | AVFormatContext *ctx; |
| 187 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 188 | /* EBML stuff */ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 189 | int num_levels; |
| 190 | MatroskaLevel levels[EBML_MAX_DEPTH]; |
| 191 | int level_up; |
| 192 | |
Aurelien Jacobs | 2970858 | 2008-08-05 00:40:27 | [diff] [blame] | 193 | uint64_t time_scale; |
| 194 | double duration; |
| 195 | char *title; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 196 | EbmlList tracks; |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 197 | EbmlList attachments; |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 198 | EbmlList chapters; |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 199 | EbmlList index; |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 200 | EbmlList tags; |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 201 | EbmlList seekhead; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 202 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 203 | /* byte position of the segment inside the stream */ |
Diego Biurrun | bc5c918 | 2008-10-03 10:16:29 | [diff] [blame] | 204 | int64_t segment_start; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 205 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 206 | /* the packet queue */ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 207 | AVPacket **packets; |
| 208 | int num_packets; |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 209 | AVPacket *prev_pkt; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 210 | |
Aurelien Jacobs | 8d75b5a | 2007-06-04 22:35:16 | [diff] [blame] | 211 | int done; |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 212 | int has_cluster_id; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 213 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 214 | /* What to skip before effectively reading a packet. */ |
| 215 | int skip_to_keyframe; |
Aurelien Jacobs | 20f7466 | 2008-09-09 12:01:51 | [diff] [blame] | 216 | uint64_t skip_to_timecode; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 217 | } MatroskaDemuxContext; |
| 218 | |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 219 | typedef struct { |
| 220 | uint64_t duration; |
| 221 | int64_t reference; |
| 222 | EbmlBin bin; |
| 223 | } MatroskaBlock; |
| 224 | |
| 225 | typedef struct { |
| 226 | uint64_t timecode; |
| 227 | EbmlList blocks; |
| 228 | } MatroskaCluster; |
| 229 | |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 230 | static EbmlSyntax ebml_header[] = { |
| 231 | { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} }, |
| 232 | { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} }, |
| 233 | { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} }, |
| 234 | { EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml,doctype), {.s="(none)"} }, |
| 235 | { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} }, |
| 236 | { EBML_ID_EBMLVERSION, EBML_NONE }, |
| 237 | { EBML_ID_DOCTYPEVERSION, EBML_NONE }, |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 238 | { 0 } |
| 239 | }; |
| 240 | |
| 241 | static EbmlSyntax ebml_syntax[] = { |
| 242 | { EBML_ID_HEADER, EBML_NEST, 0, 0, {.n=ebml_header} }, |
| 243 | { 0 } |
| 244 | }; |
| 245 | |
Aurelien Jacobs | 2970858 | 2008-08-05 00:40:27 | [diff] [blame] | 246 | static EbmlSyntax matroska_info[] = { |
| 247 | { MATROSKA_ID_TIMECODESCALE, EBML_UINT, 0, offsetof(MatroskaDemuxContext,time_scale), {.u=1000000} }, |
| 248 | { MATROSKA_ID_DURATION, EBML_FLOAT, 0, offsetof(MatroskaDemuxContext,duration) }, |
| 249 | { MATROSKA_ID_TITLE, EBML_UTF8, 0, offsetof(MatroskaDemuxContext,title) }, |
| 250 | { MATROSKA_ID_WRITINGAPP, EBML_NONE }, |
| 251 | { MATROSKA_ID_MUXINGAPP, EBML_NONE }, |
| 252 | { MATROSKA_ID_DATEUTC, EBML_NONE }, |
| 253 | { MATROSKA_ID_SEGMENTUID, EBML_NONE }, |
Aurelien Jacobs | 2970858 | 2008-08-05 00:40:27 | [diff] [blame] | 254 | { 0 } |
| 255 | }; |
| 256 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 257 | static EbmlSyntax matroska_track_video[] = { |
| 258 | { MATROSKA_ID_VIDEOFRAMERATE, EBML_FLOAT,0, offsetof(MatroskaTrackVideo,frame_rate) }, |
| 259 | { MATROSKA_ID_VIDEODISPLAYWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_width) }, |
| 260 | { MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_height) }, |
| 261 | { MATROSKA_ID_VIDEOPIXELWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_width) }, |
| 262 | { MATROSKA_ID_VIDEOPIXELHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_height) }, |
| 263 | { MATROSKA_ID_VIDEOCOLORSPACE, EBML_UINT, 0, offsetof(MatroskaTrackVideo,fourcc) }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 264 | { MATROSKA_ID_VIDEOPIXELCROPB, EBML_NONE }, |
| 265 | { MATROSKA_ID_VIDEOPIXELCROPT, EBML_NONE }, |
| 266 | { MATROSKA_ID_VIDEOPIXELCROPL, EBML_NONE }, |
| 267 | { MATROSKA_ID_VIDEOPIXELCROPR, EBML_NONE }, |
| 268 | { MATROSKA_ID_VIDEODISPLAYUNIT, EBML_NONE }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 269 | { MATROSKA_ID_VIDEOFLAGINTERLACED,EBML_NONE }, |
| 270 | { MATROSKA_ID_VIDEOSTEREOMODE, EBML_NONE }, |
| 271 | { MATROSKA_ID_VIDEOASPECTRATIO, EBML_NONE }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 272 | { 0 } |
| 273 | }; |
| 274 | |
| 275 | static EbmlSyntax matroska_track_audio[] = { |
| 276 | { MATROSKA_ID_AUDIOSAMPLINGFREQ, EBML_FLOAT,0, offsetof(MatroskaTrackAudio,samplerate), {.f=8000.0} }, |
| 277 | { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ,EBML_FLOAT,0,offsetof(MatroskaTrackAudio,out_samplerate) }, |
| 278 | { MATROSKA_ID_AUDIOBITDEPTH, EBML_UINT, 0, offsetof(MatroskaTrackAudio,bitdepth) }, |
| 279 | { MATROSKA_ID_AUDIOCHANNELS, EBML_UINT, 0, offsetof(MatroskaTrackAudio,channels), {.u=1} }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 280 | { 0 } |
| 281 | }; |
| 282 | |
| 283 | static EbmlSyntax matroska_track_encoding_compression[] = { |
| 284 | { MATROSKA_ID_ENCODINGCOMPALGO, EBML_UINT, 0, offsetof(MatroskaTrackCompression,algo), {.u=0} }, |
| 285 | { MATROSKA_ID_ENCODINGCOMPSETTINGS,EBML_BIN, 0, offsetof(MatroskaTrackCompression,settings) }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 286 | { 0 } |
| 287 | }; |
| 288 | |
| 289 | static EbmlSyntax matroska_track_encoding[] = { |
| 290 | { MATROSKA_ID_ENCODINGSCOPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,scope), {.u=1} }, |
| 291 | { MATROSKA_ID_ENCODINGTYPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,type), {.u=0} }, |
| 292 | { MATROSKA_ID_ENCODINGCOMPRESSION,EBML_NEST, 0, offsetof(MatroskaTrackEncoding,compression), {.n=matroska_track_encoding_compression} }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 293 | { MATROSKA_ID_ENCODINGORDER, EBML_NONE }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 294 | { 0 } |
| 295 | }; |
| 296 | |
| 297 | static EbmlSyntax matroska_track_encodings[] = { |
| 298 | { MATROSKA_ID_TRACKCONTENTENCODING, EBML_NEST, sizeof(MatroskaTrackEncoding), offsetof(MatroskaTrack,encodings), {.n=matroska_track_encoding} }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 299 | { 0 } |
| 300 | }; |
| 301 | |
| 302 | static EbmlSyntax matroska_track[] = { |
| 303 | { MATROSKA_ID_TRACKNUMBER, EBML_UINT, 0, offsetof(MatroskaTrack,num) }, |
| 304 | { MATROSKA_ID_TRACKTYPE, EBML_UINT, 0, offsetof(MatroskaTrack,type) }, |
| 305 | { MATROSKA_ID_CODECID, EBML_STR, 0, offsetof(MatroskaTrack,codec_id) }, |
| 306 | { MATROSKA_ID_CODECPRIVATE, EBML_BIN, 0, offsetof(MatroskaTrack,codec_priv) }, |
| 307 | { MATROSKA_ID_TRACKLANGUAGE, EBML_UTF8, 0, offsetof(MatroskaTrack,language), {.s="eng"} }, |
| 308 | { MATROSKA_ID_TRACKDEFAULTDURATION, EBML_UINT, 0, offsetof(MatroskaTrack,default_duration) }, |
| 309 | { MATROSKA_ID_TRACKTIMECODESCALE, EBML_FLOAT,0, offsetof(MatroskaTrack,time_scale), {.f=1.0} }, |
| 310 | { MATROSKA_ID_TRACKFLAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTrack,flag_default), {.u=1} }, |
| 311 | { MATROSKA_ID_TRACKVIDEO, EBML_NEST, 0, offsetof(MatroskaTrack,video), {.n=matroska_track_video} }, |
| 312 | { MATROSKA_ID_TRACKAUDIO, EBML_NEST, 0, offsetof(MatroskaTrack,audio), {.n=matroska_track_audio} }, |
| 313 | { MATROSKA_ID_TRACKCONTENTENCODINGS,EBML_NEST, 0, 0, {.n=matroska_track_encodings} }, |
| 314 | { MATROSKA_ID_TRACKUID, EBML_NONE }, |
| 315 | { MATROSKA_ID_TRACKNAME, EBML_NONE }, |
| 316 | { MATROSKA_ID_TRACKFLAGENABLED, EBML_NONE }, |
| 317 | { MATROSKA_ID_TRACKFLAGFORCED, EBML_NONE }, |
| 318 | { MATROSKA_ID_TRACKFLAGLACING, EBML_NONE }, |
| 319 | { MATROSKA_ID_CODECNAME, EBML_NONE }, |
| 320 | { MATROSKA_ID_CODECDECODEALL, EBML_NONE }, |
| 321 | { MATROSKA_ID_CODECINFOURL, EBML_NONE }, |
| 322 | { MATROSKA_ID_CODECDOWNLOADURL, EBML_NONE }, |
| 323 | { MATROSKA_ID_TRACKMINCACHE, EBML_NONE }, |
| 324 | { MATROSKA_ID_TRACKMAXCACHE, EBML_NONE }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 325 | { MATROSKA_ID_TRACKMAXBLKADDID, EBML_NONE }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 326 | { 0 } |
| 327 | }; |
| 328 | |
| 329 | static EbmlSyntax matroska_tracks[] = { |
| 330 | { MATROSKA_ID_TRACKENTRY, EBML_NEST, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext,tracks), {.n=matroska_track} }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 331 | { 0 } |
| 332 | }; |
| 333 | |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 334 | static EbmlSyntax matroska_attachment[] = { |
| 335 | { MATROSKA_ID_FILENAME, EBML_UTF8, 0, offsetof(MatroskaAttachement,filename) }, |
| 336 | { MATROSKA_ID_FILEMIMETYPE, EBML_STR, 0, offsetof(MatroskaAttachement,mime) }, |
| 337 | { MATROSKA_ID_FILEDATA, EBML_BIN, 0, offsetof(MatroskaAttachement,bin) }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 338 | { MATROSKA_ID_FILEDESC, EBML_NONE }, |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 339 | { MATROSKA_ID_FILEUID, EBML_NONE }, |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 340 | { 0 } |
| 341 | }; |
| 342 | |
| 343 | static EbmlSyntax matroska_attachments[] = { |
| 344 | { MATROSKA_ID_ATTACHEDFILE, EBML_NEST, sizeof(MatroskaAttachement), offsetof(MatroskaDemuxContext,attachments), {.n=matroska_attachment} }, |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 345 | { 0 } |
| 346 | }; |
| 347 | |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 348 | static EbmlSyntax matroska_chapter_display[] = { |
| 349 | { MATROSKA_ID_CHAPSTRING, EBML_UTF8, 0, offsetof(MatroskaChapter,title) }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 350 | { MATROSKA_ID_CHAPLANG, EBML_NONE }, |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 351 | { 0 } |
| 352 | }; |
| 353 | |
| 354 | static EbmlSyntax matroska_chapter_entry[] = { |
| 355 | { MATROSKA_ID_CHAPTERTIMESTART, EBML_UINT, 0, offsetof(MatroskaChapter,start), {.u=AV_NOPTS_VALUE} }, |
| 356 | { MATROSKA_ID_CHAPTERTIMEEND, EBML_UINT, 0, offsetof(MatroskaChapter,end), {.u=AV_NOPTS_VALUE} }, |
| 357 | { MATROSKA_ID_CHAPTERUID, EBML_UINT, 0, offsetof(MatroskaChapter,uid) }, |
| 358 | { MATROSKA_ID_CHAPTERDISPLAY, EBML_NEST, 0, 0, {.n=matroska_chapter_display} }, |
| 359 | { MATROSKA_ID_CHAPTERFLAGHIDDEN, EBML_NONE }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 360 | { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE }, |
| 361 | { MATROSKA_ID_CHAPTERPHYSEQUIV, EBML_NONE }, |
| 362 | { MATROSKA_ID_CHAPTERATOM, EBML_NONE }, |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 363 | { 0 } |
| 364 | }; |
| 365 | |
| 366 | static EbmlSyntax matroska_chapter[] = { |
| 367 | { MATROSKA_ID_CHAPTERATOM, EBML_NEST, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext,chapters), {.n=matroska_chapter_entry} }, |
| 368 | { MATROSKA_ID_EDITIONUID, EBML_NONE }, |
| 369 | { MATROSKA_ID_EDITIONFLAGHIDDEN, EBML_NONE }, |
| 370 | { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 371 | { MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE }, |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 372 | { 0 } |
| 373 | }; |
| 374 | |
| 375 | static EbmlSyntax matroska_chapters[] = { |
| 376 | { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, {.n=matroska_chapter} }, |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 377 | { 0 } |
| 378 | }; |
| 379 | |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 380 | static EbmlSyntax matroska_index_pos[] = { |
| 381 | { MATROSKA_ID_CUETRACK, EBML_UINT, 0, offsetof(MatroskaIndexPos,track) }, |
| 382 | { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, offsetof(MatroskaIndexPos,pos) }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 383 | { MATROSKA_ID_CUEBLOCKNUMBER, EBML_NONE }, |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 384 | { 0 } |
| 385 | }; |
| 386 | |
| 387 | static EbmlSyntax matroska_index_entry[] = { |
| 388 | { MATROSKA_ID_CUETIME, EBML_UINT, 0, offsetof(MatroskaIndex,time) }, |
| 389 | { MATROSKA_ID_CUETRACKPOSITION, EBML_NEST, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex,pos), {.n=matroska_index_pos} }, |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 390 | { 0 } |
| 391 | }; |
| 392 | |
| 393 | static EbmlSyntax matroska_index[] = { |
| 394 | { MATROSKA_ID_POINTENTRY, EBML_NEST, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext,index), {.n=matroska_index_entry} }, |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 395 | { 0 } |
| 396 | }; |
| 397 | |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 398 | static EbmlSyntax matroska_simpletag[] = { |
| 399 | { MATROSKA_ID_TAGNAME, EBML_UTF8, 0, offsetof(MatroskaTag,name) }, |
| 400 | { MATROSKA_ID_TAGSTRING, EBML_UTF8, 0, offsetof(MatroskaTag,string) }, |
| 401 | { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTag,sub), {.n=matroska_simpletag} }, |
| 402 | { MATROSKA_ID_TAGLANG, EBML_NONE }, |
| 403 | { MATROSKA_ID_TAGDEFAULT, EBML_NONE }, |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 404 | { 0 } |
| 405 | }; |
| 406 | |
| 407 | static EbmlSyntax matroska_tag[] = { |
| 408 | { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), 0, {.n=matroska_simpletag} }, |
| 409 | { MATROSKA_ID_TAGTARGETS, EBML_NONE }, |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 410 | { 0 } |
| 411 | }; |
| 412 | |
Aurelien Jacobs | 434d496 | 2008-08-05 00:40:18 | [diff] [blame] | 413 | static EbmlSyntax matroska_tags[] = { |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 414 | { MATROSKA_ID_TAG, EBML_NEST, 0, offsetof(MatroskaDemuxContext,tags), {.n=matroska_tag} }, |
Aurelien Jacobs | 434d496 | 2008-08-05 00:40:18 | [diff] [blame] | 415 | { 0 } |
| 416 | }; |
| 417 | |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 418 | static EbmlSyntax matroska_seekhead_entry[] = { |
| 419 | { MATROSKA_ID_SEEKID, EBML_UINT, 0, offsetof(MatroskaSeekhead,id) }, |
| 420 | { MATROSKA_ID_SEEKPOSITION, EBML_UINT, 0, offsetof(MatroskaSeekhead,pos), {.u=-1} }, |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 421 | { 0 } |
| 422 | }; |
| 423 | |
| 424 | static EbmlSyntax matroska_seekhead[] = { |
| 425 | { MATROSKA_ID_SEEKENTRY, EBML_NEST, sizeof(MatroskaSeekhead), offsetof(MatroskaDemuxContext,seekhead), {.n=matroska_seekhead_entry} }, |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 426 | { 0 } |
| 427 | }; |
| 428 | |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 429 | static EbmlSyntax matroska_segment[] = { |
| 430 | { MATROSKA_ID_INFO, EBML_NEST, 0, 0, {.n=matroska_info } }, |
| 431 | { MATROSKA_ID_TRACKS, EBML_NEST, 0, 0, {.n=matroska_tracks } }, |
| 432 | { MATROSKA_ID_ATTACHMENTS, EBML_NEST, 0, 0, {.n=matroska_attachments} }, |
| 433 | { MATROSKA_ID_CHAPTERS, EBML_NEST, 0, 0, {.n=matroska_chapters } }, |
| 434 | { MATROSKA_ID_CUES, EBML_NEST, 0, 0, {.n=matroska_index } }, |
| 435 | { MATROSKA_ID_TAGS, EBML_NEST, 0, 0, {.n=matroska_tags } }, |
| 436 | { MATROSKA_ID_SEEKHEAD, EBML_NEST, 0, 0, {.n=matroska_seekhead } }, |
| 437 | { MATROSKA_ID_CLUSTER, EBML_STOP, 0, offsetof(MatroskaDemuxContext,has_cluster_id) }, |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 438 | { 0 } |
| 439 | }; |
| 440 | |
| 441 | static EbmlSyntax matroska_segments[] = { |
| 442 | { MATROSKA_ID_SEGMENT, EBML_NEST, 0, 0, {.n=matroska_segment } }, |
| 443 | { 0 } |
| 444 | }; |
| 445 | |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 446 | static EbmlSyntax matroska_blockgroup[] = { |
| 447 | { MATROSKA_ID_BLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) }, |
| 448 | { MATROSKA_ID_SIMPLEBLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) }, |
| 449 | { MATROSKA_ID_BLOCKDURATION, EBML_UINT, 0, offsetof(MatroskaBlock,duration), {.u=AV_NOPTS_VALUE} }, |
| 450 | { MATROSKA_ID_BLOCKREFERENCE, EBML_UINT, 0, offsetof(MatroskaBlock,reference) }, |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 451 | { 0 } |
| 452 | }; |
| 453 | |
| 454 | static EbmlSyntax matroska_cluster[] = { |
| 455 | { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) }, |
| 456 | { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} }, |
| 457 | { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 458 | { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE }, |
| 459 | { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE }, |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 460 | { 0 } |
| 461 | }; |
| 462 | |
| 463 | static EbmlSyntax matroska_clusters[] = { |
| 464 | { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster} }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 465 | { MATROSKA_ID_INFO, EBML_NONE }, |
| 466 | { MATROSKA_ID_CUES, EBML_NONE }, |
| 467 | { MATROSKA_ID_TAGS, EBML_NONE }, |
| 468 | { MATROSKA_ID_SEEKHEAD, EBML_NONE }, |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 469 | { 0 } |
| 470 | }; |
| 471 | |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 472 | #define SIZE_OFF(x) sizeof(((AVFormatContext*)0)->x),offsetof(AVFormatContext,x) |
| 473 | const struct { |
| 474 | const char name[16]; |
| 475 | int size; |
| 476 | int offset; |
| 477 | } metadata[] = { |
| 478 | { "TITLE", SIZE_OFF(title) }, |
| 479 | { "ARTIST", SIZE_OFF(author) }, |
| 480 | { "WRITTEN_BY", SIZE_OFF(author) }, |
| 481 | { "LEAD_PERFORMER", SIZE_OFF(author) }, |
| 482 | { "COPYRIGHT", SIZE_OFF(copyright) }, |
| 483 | { "COMMENT", SIZE_OFF(comment) }, |
| 484 | { "ALBUM", SIZE_OFF(album) }, |
| 485 | { "DATE_WRITTEN", SIZE_OFF(year) }, |
| 486 | { "DATE_RELEASED", SIZE_OFF(year) }, |
| 487 | { "PART_NUMBER", SIZE_OFF(track) }, |
| 488 | { "GENRE", SIZE_OFF(genre) }, |
| 489 | }; |
| 490 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 491 | /* |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 492 | * Return: Whether we reached the end of a level in the hierarchy or not. |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 493 | */ |
Aurelien Jacobs | 592110c | 2008-08-05 00:42:08 | [diff] [blame] | 494 | static int ebml_level_end(MatroskaDemuxContext *matroska) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 495 | { |
Björn Axelsson | 899681c | 2007-11-21 07:41:00 | [diff] [blame] | 496 | ByteIOContext *pb = matroska->ctx->pb; |
Diego Biurrun | bc5c918 | 2008-10-03 10:16:29 | [diff] [blame] | 497 | int64_t pos = url_ftell(pb); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 498 | |
Aurelien Jacobs | 592110c | 2008-08-05 00:42:08 | [diff] [blame] | 499 | if (matroska->num_levels > 0) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 500 | MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1]; |
Aurelien Jacobs | 592110c | 2008-08-05 00:42:08 | [diff] [blame] | 501 | if (pos - level->start >= level->length) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 502 | matroska->num_levels--; |
Aurelien Jacobs | 592110c | 2008-08-05 00:42:08 | [diff] [blame] | 503 | return 1; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 504 | } |
| 505 | } |
Aurelien Jacobs | 592110c | 2008-08-05 00:42:08 | [diff] [blame] | 506 | return 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Read: an "EBML number", which is defined as a variable-length |
| 511 | * array of bytes. The first byte indicates the length by giving a |
| 512 | * number of 0-bits followed by a one. The position of the first |
| 513 | * "one" bit inside the first byte indicates the length of this |
| 514 | * number. |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 515 | * Returns: number of bytes read, < 0 on error |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 516 | */ |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 517 | static int ebml_read_num(MatroskaDemuxContext *matroska, ByteIOContext *pb, |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 518 | int max_size, uint64_t *number) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 519 | { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 520 | int len_mask = 0x80, read = 1, n = 1; |
| 521 | int64_t total = 0; |
| 522 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 523 | /* The first byte tells us the length in bytes - get_byte() can normally |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 524 | * return 0, but since that's not a valid first ebmlID byte, we can |
| 525 | * use it safely here to catch EOS. */ |
| 526 | if (!(total = get_byte(pb))) { |
| 527 | /* we might encounter EOS here */ |
| 528 | if (!url_feof(pb)) { |
Diego Biurrun | bc5c918 | 2008-10-03 10:16:29 | [diff] [blame] | 529 | int64_t pos = url_ftell(pb); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 530 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 531 | "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", |
| 532 | pos, pos); |
| 533 | } |
Panagiotis Issaris | 6f3e0b2 | 2007-07-19 15:23:32 | [diff] [blame] | 534 | return AVERROR(EIO); /* EOS or actual I/O error */ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* get the length of the EBML number */ |
| 538 | while (read <= max_size && !(total & len_mask)) { |
| 539 | read++; |
| 540 | len_mask >>= 1; |
| 541 | } |
| 542 | if (read > max_size) { |
Diego Biurrun | bc5c918 | 2008-10-03 10:16:29 | [diff] [blame] | 543 | int64_t pos = url_ftell(pb) - 1; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 544 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 545 | "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n", |
| 546 | (uint8_t) total, pos, pos); |
| 547 | return AVERROR_INVALIDDATA; |
| 548 | } |
| 549 | |
| 550 | /* read out length */ |
| 551 | total &= ~len_mask; |
| 552 | while (n++ < read) |
| 553 | total = (total << 8) | get_byte(pb); |
| 554 | |
| 555 | *number = total; |
| 556 | |
| 557 | return read; |
| 558 | } |
| 559 | |
| 560 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 561 | * Read the next element as an unsigned int. |
| 562 | * 0 is success, < 0 is failure. |
| 563 | */ |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 564 | static int ebml_read_uint(ByteIOContext *pb, int size, uint64_t *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 565 | { |
Aurelien Jacobs | c6cd2b3 | 2008-08-05 00:41:55 | [diff] [blame] | 566 | int n = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 567 | |
Aurelien Jacobs | ba5a1f9 | 2008-08-05 00:41:52 | [diff] [blame] | 568 | if (size < 1 || size > 8) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 569 | return AVERROR_INVALIDDATA; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 570 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 571 | /* big-endian ordering; build up number */ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 572 | *num = 0; |
| 573 | while (n++ < size) |
| 574 | *num = (*num << 8) | get_byte(pb); |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 580 | * Read the next element as a float. |
| 581 | * 0 is success, < 0 is failure. |
| 582 | */ |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 583 | static int ebml_read_float(ByteIOContext *pb, int size, double *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 584 | { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 585 | if (size == 4) { |
| 586 | *num= av_int2flt(get_be32(pb)); |
| 587 | } else if(size==8){ |
| 588 | *num= av_int2dbl(get_be64(pb)); |
Aurelien Jacobs | ba5a1f9 | 2008-08-05 00:41:52 | [diff] [blame] | 589 | } else |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 590 | return AVERROR_INVALIDDATA; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | /* |
| 596 | * Read the next element as an ASCII string. |
| 597 | * 0 is success, < 0 is failure. |
| 598 | */ |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 599 | static int ebml_read_ascii(ByteIOContext *pb, int size, char **str) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 600 | { |
Aurelien Jacobs | c6cd2b3 | 2008-08-05 00:41:55 | [diff] [blame] | 601 | av_free(*str); |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 602 | /* EBML strings are usually not 0-terminated, so we allocate one |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 603 | * byte more, read the string and NULL-terminate it ourselves. */ |
Aurelien Jacobs | c6cd2b3 | 2008-08-05 00:41:55 | [diff] [blame] | 604 | if (!(*str = av_malloc(size + 1))) |
Panagiotis Issaris | 769e10f | 2007-07-19 15:21:30 | [diff] [blame] | 605 | return AVERROR(ENOMEM); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 606 | if (get_buffer(pb, (uint8_t *) *str, size) != size) { |
Aurelien Jacobs | ff2c222 | 2008-06-02 23:37:14 | [diff] [blame] | 607 | av_free(*str); |
Panagiotis Issaris | 6f3e0b2 | 2007-07-19 15:23:32 | [diff] [blame] | 608 | return AVERROR(EIO); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 609 | } |
| 610 | (*str)[size] = '\0'; |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | /* |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 616 | * Read the next element as binary data. |
| 617 | * 0 is success, < 0 is failure. |
| 618 | */ |
| 619 | static int ebml_read_binary(ByteIOContext *pb, int length, EbmlBin *bin) |
| 620 | { |
| 621 | av_free(bin->data); |
| 622 | if (!(bin->data = av_malloc(length))) |
| 623 | return AVERROR(ENOMEM); |
| 624 | |
| 625 | bin->size = length; |
| 626 | bin->pos = url_ftell(pb); |
| 627 | if (get_buffer(pb, bin->data, length) != length) |
| 628 | return AVERROR(EIO); |
| 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 634 | * Read the next element, but only the header. The contents |
| 635 | * are supposed to be sub-elements which can be read separately. |
| 636 | * 0 is success, < 0 is failure. |
| 637 | */ |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 638 | static int ebml_read_master(MatroskaDemuxContext *matroska, int length) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 639 | { |
Björn Axelsson | 899681c | 2007-11-21 07:41:00 | [diff] [blame] | 640 | ByteIOContext *pb = matroska->ctx->pb; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 641 | MatroskaLevel *level; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 642 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 643 | if (matroska->num_levels >= EBML_MAX_DEPTH) { |
| 644 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 645 | "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH); |
Panagiotis Issaris | 85565db | 2007-07-19 15:38:33 | [diff] [blame] | 646 | return AVERROR(ENOSYS); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 647 | } |
| 648 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 649 | level = &matroska->levels[matroska->num_levels++]; |
| 650 | level->start = url_ftell(pb); |
| 651 | level->length = length; |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 657 | * Read signed/unsigned "EBML" numbers. |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 658 | * Return: number of bytes processed, < 0 on error |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 659 | */ |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 660 | static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska, |
| 661 | uint8_t *data, uint32_t size, uint64_t *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 662 | { |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 663 | ByteIOContext pb; |
| 664 | init_put_byte(&pb, data, size, 0, NULL, NULL, NULL, NULL); |
| 665 | return ebml_read_num(matroska, &pb, 8, num); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /* |
| 669 | * Same as above, but signed. |
| 670 | */ |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 671 | static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska, |
| 672 | uint8_t *data, uint32_t size, int64_t *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 673 | { |
| 674 | uint64_t unum; |
| 675 | int res; |
| 676 | |
| 677 | /* read as unsigned number first */ |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 678 | if ((res = matroska_ebmlnum_uint(matroska, data, size, &unum)) < 0) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 679 | return res; |
| 680 | |
| 681 | /* make signed (weird way) */ |
Aurelien Jacobs | 33ac07e | 2008-08-05 00:42:55 | [diff] [blame] | 682 | *num = unum - ((1LL << (7*res - 1)) - 1); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 683 | |
| 684 | return res; |
| 685 | } |
| 686 | |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 687 | static int ebml_parse_elem(MatroskaDemuxContext *matroska, |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 688 | EbmlSyntax *syntax, void *data); |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 689 | |
| 690 | static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, |
| 691 | uint32_t id, void *data) |
| 692 | { |
| 693 | int i; |
| 694 | for (i=0; syntax[i].id; i++) |
| 695 | if (id == syntax[i].id) |
| 696 | break; |
Aurelien Jacobs | b49d17b | 2008-08-20 00:44:25 | [diff] [blame] | 697 | if (!syntax[i].id && id != EBML_ID_VOID && id != EBML_ID_CRC32) |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 698 | av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id); |
| 699 | return ebml_parse_elem(matroska, &syntax[i], data); |
| 700 | } |
| 701 | |
Aurelien Jacobs | 66a37e0 | 2008-08-05 00:42:17 | [diff] [blame] | 702 | static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, |
| 703 | void *data) |
| 704 | { |
Aurelien Jacobs | 88cca98 | 2008-08-05 00:42:58 | [diff] [blame] | 705 | uint64_t id; |
| 706 | int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id); |
| 707 | id |= 1 << 7*res; |
Aurelien Jacobs | 66a37e0 | 2008-08-05 00:42:17 | [diff] [blame] | 708 | return res < 0 ? res : ebml_parse_id(matroska, syntax, id, data); |
| 709 | } |
| 710 | |
Aurelien Jacobs | 9bcb92c | 2008-08-05 00:42:13 | [diff] [blame] | 711 | static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, |
Aurelien Jacobs | 6314cca | 2008-08-05 00:42:23 | [diff] [blame] | 712 | void *data) |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 713 | { |
Aurelien Jacobs | c005b3f | 2008-08-05 00:42:10 | [diff] [blame] | 714 | int i, res = 0; |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 715 | |
| 716 | for (i=0; syntax[i].id; i++) |
| 717 | switch (syntax[i].type) { |
| 718 | case EBML_UINT: |
| 719 | *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u; |
| 720 | break; |
| 721 | case EBML_FLOAT: |
| 722 | *(double *)((char *)data+syntax[i].data_offset) = syntax[i].def.f; |
| 723 | break; |
| 724 | case EBML_STR: |
| 725 | case EBML_UTF8: |
| 726 | *(char **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s); |
| 727 | break; |
| 728 | } |
| 729 | |
Aurelien Jacobs | 6314cca | 2008-08-05 00:42:23 | [diff] [blame] | 730 | while (!res && !ebml_level_end(matroska)) |
Aurelien Jacobs | 66a37e0 | 2008-08-05 00:42:17 | [diff] [blame] | 731 | res = ebml_parse(matroska, syntax, data); |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 732 | |
| 733 | return res; |
| 734 | } |
| 735 | |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 736 | static int ebml_parse_elem(MatroskaDemuxContext *matroska, |
| 737 | EbmlSyntax *syntax, void *data) |
| 738 | { |
| 739 | ByteIOContext *pb = matroska->ctx->pb; |
| 740 | uint32_t id = syntax->id; |
| 741 | uint64_t length; |
| 742 | int res; |
| 743 | |
| 744 | data = (char *)data + syntax->data_offset; |
| 745 | if (syntax->list_elem_size) { |
| 746 | EbmlList *list = data; |
| 747 | list->elem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size); |
| 748 | data = (char*)list->elem + list->nb_elem*syntax->list_elem_size; |
| 749 | memset(data, 0, syntax->list_elem_size); |
| 750 | list->nb_elem++; |
| 751 | } |
| 752 | |
| 753 | if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 754 | if ((res = ebml_read_num(matroska, pb, 8, &length)) < 0) |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 755 | return res; |
| 756 | |
| 757 | switch (syntax->type) { |
| 758 | case EBML_UINT: res = ebml_read_uint (pb, length, data); break; |
| 759 | case EBML_FLOAT: res = ebml_read_float (pb, length, data); break; |
| 760 | case EBML_STR: |
| 761 | case EBML_UTF8: res = ebml_read_ascii (pb, length, data); break; |
| 762 | case EBML_BIN: res = ebml_read_binary(pb, length, data); break; |
| 763 | case EBML_NEST: if ((res=ebml_read_master(matroska, length)) < 0) |
| 764 | return res; |
| 765 | if (id == MATROSKA_ID_SEGMENT) |
| 766 | matroska->segment_start = url_ftell(matroska->ctx->pb); |
| 767 | return ebml_parse_nest(matroska, syntax->def.n, data); |
| 768 | case EBML_PASS: return ebml_parse_id(matroska, syntax->def.n, id, data); |
| 769 | case EBML_STOP: *(int *)data = 1; return 1; |
Aurelien Jacobs | dc3e021 | 2008-08-20 22:30:15 | [diff] [blame] | 770 | default: return url_fseek(pb,length,SEEK_CUR)<0 ? AVERROR(EIO) : 0; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 771 | } |
| 772 | if (res == AVERROR_INVALIDDATA) |
| 773 | av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n"); |
| 774 | else if (res == AVERROR(EIO)) |
| 775 | av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n"); |
| 776 | return res; |
| 777 | } |
| 778 | |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 779 | static void ebml_free(EbmlSyntax *syntax, void *data) |
| 780 | { |
| 781 | int i, j; |
| 782 | for (i=0; syntax[i].id; i++) { |
| 783 | void *data_off = (char *)data + syntax[i].data_offset; |
| 784 | switch (syntax[i].type) { |
| 785 | case EBML_STR: |
| 786 | case EBML_UTF8: av_freep(data_off); break; |
| 787 | case EBML_BIN: av_freep(&((EbmlBin *)data_off)->data); break; |
| 788 | case EBML_NEST: |
| 789 | if (syntax[i].list_elem_size) { |
| 790 | EbmlList *list = data_off; |
| 791 | char *ptr = list->elem; |
| 792 | for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size) |
| 793 | ebml_free(syntax[i].def.n, ptr); |
| 794 | av_free(list->elem); |
| 795 | } else |
| 796 | ebml_free(syntax[i].def.n, data_off); |
| 797 | default: break; |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 802 | |
| 803 | /* |
| 804 | * Autodetecting... |
| 805 | */ |
| 806 | static int matroska_probe(AVProbeData *p) |
| 807 | { |
| 808 | uint64_t total = 0; |
| 809 | int len_mask = 0x80, size = 1, n = 1; |
Reimar Döffinger | 7b571fd | 2008-08-24 17:19:46 | [diff] [blame] | 810 | static const char probe_data[] = "matroska"; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 811 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 812 | /* EBML header? */ |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 813 | if (AV_RB32(p->buf) != EBML_ID_HEADER) |
| 814 | return 0; |
| 815 | |
| 816 | /* length of header */ |
| 817 | total = p->buf[4]; |
| 818 | while (size <= 8 && !(total & len_mask)) { |
| 819 | size++; |
| 820 | len_mask >>= 1; |
| 821 | } |
| 822 | if (size > 8) |
| 823 | return 0; |
| 824 | total &= (len_mask - 1); |
| 825 | while (n < size) |
| 826 | total = (total << 8) | p->buf[4 + n++]; |
| 827 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 828 | /* Does the probe data contain the whole header? */ |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 829 | if (p->buf_size < 4 + size + total) |
| 830 | return 0; |
| 831 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 832 | /* The header must contain the document type 'matroska'. For now, |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 833 | * we don't parse the whole header but simply check for the |
| 834 | * availability of that array of characters inside the header. |
| 835 | * Not fully fool-proof, but good enough. */ |
| 836 | for (n = 4+size; n <= 4+size+total-(sizeof(probe_data)-1); n++) |
| 837 | if (!memcmp(p->buf+n, probe_data, sizeof(probe_data)-1)) |
| 838 | return AVPROBE_SCORE_MAX; |
| 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska, |
| 844 | int num) |
| 845 | { |
| 846 | MatroskaTrack *tracks = matroska->tracks.elem; |
| 847 | int i; |
| 848 | |
| 849 | for (i=0; i < matroska->tracks.nb_elem; i++) |
| 850 | if (tracks[i].num == num) |
| 851 | return &tracks[i]; |
| 852 | |
| 853 | av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num); |
| 854 | return NULL; |
| 855 | } |
| 856 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 857 | static int matroska_decode_buffer(uint8_t** buf, int* buf_size, |
| 858 | MatroskaTrack *track) |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 859 | { |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 860 | MatroskaTrackEncoding *encodings = track->encodings.elem; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 861 | uint8_t* data = *buf; |
| 862 | int isize = *buf_size; |
| 863 | uint8_t* pkt_data = NULL; |
| 864 | int pkt_size = isize; |
| 865 | int result = 0; |
| 866 | int olen; |
| 867 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 868 | switch (encodings[0].compression.algo) { |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 869 | case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP: |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 870 | return encodings[0].compression.settings.size; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 871 | case MATROSKA_TRACK_ENCODING_COMP_LZO: |
| 872 | do { |
| 873 | olen = pkt_size *= 3; |
| 874 | pkt_data = av_realloc(pkt_data, |
| 875 | pkt_size+LZO_OUTPUT_PADDING); |
| 876 | result = lzo1x_decode(pkt_data, &olen, data, &isize); |
| 877 | } while (result==LZO_OUTPUT_FULL && pkt_size<10000000); |
| 878 | if (result) |
| 879 | goto failed; |
| 880 | pkt_size -= olen; |
| 881 | break; |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame^] | 882 | #if CONFIG_ZLIB |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 883 | case MATROSKA_TRACK_ENCODING_COMP_ZLIB: { |
| 884 | z_stream zstream = {0}; |
| 885 | if (inflateInit(&zstream) != Z_OK) |
| 886 | return -1; |
| 887 | zstream.next_in = data; |
| 888 | zstream.avail_in = isize; |
| 889 | do { |
| 890 | pkt_size *= 3; |
| 891 | pkt_data = av_realloc(pkt_data, pkt_size); |
| 892 | zstream.avail_out = pkt_size - zstream.total_out; |
| 893 | zstream.next_out = pkt_data + zstream.total_out; |
| 894 | result = inflate(&zstream, Z_NO_FLUSH); |
| 895 | } while (result==Z_OK && pkt_size<10000000); |
| 896 | pkt_size = zstream.total_out; |
| 897 | inflateEnd(&zstream); |
| 898 | if (result != Z_STREAM_END) |
| 899 | goto failed; |
| 900 | break; |
| 901 | } |
| 902 | #endif |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame^] | 903 | #if CONFIG_BZLIB |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 904 | case MATROSKA_TRACK_ENCODING_COMP_BZLIB: { |
| 905 | bz_stream bzstream = {0}; |
| 906 | if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK) |
| 907 | return -1; |
| 908 | bzstream.next_in = data; |
| 909 | bzstream.avail_in = isize; |
| 910 | do { |
| 911 | pkt_size *= 3; |
| 912 | pkt_data = av_realloc(pkt_data, pkt_size); |
| 913 | bzstream.avail_out = pkt_size - bzstream.total_out_lo32; |
| 914 | bzstream.next_out = pkt_data + bzstream.total_out_lo32; |
| 915 | result = BZ2_bzDecompress(&bzstream); |
| 916 | } while (result==BZ_OK && pkt_size<10000000); |
| 917 | pkt_size = bzstream.total_out_lo32; |
| 918 | BZ2_bzDecompressEnd(&bzstream); |
| 919 | if (result != BZ_STREAM_END) |
| 920 | goto failed; |
| 921 | break; |
| 922 | } |
| 923 | #endif |
Aurelien Jacobs | 28f27e0 | 2008-08-20 23:08:07 | [diff] [blame] | 924 | default: |
| 925 | return -1; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | *buf = pkt_data; |
| 929 | *buf_size = pkt_size; |
| 930 | return 0; |
| 931 | failed: |
| 932 | av_free(pkt_data); |
| 933 | return -1; |
| 934 | } |
| 935 | |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 936 | static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska, |
Aurelien Jacobs | e7d4b74 | 2008-09-28 22:55:28 | [diff] [blame] | 937 | AVPacket *pkt, uint64_t display_duration) |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 938 | { |
| 939 | char *line, *layer, *ptr = pkt->data, *end = ptr+pkt->size; |
| 940 | for (; *ptr!=',' && ptr<end-1; ptr++); |
| 941 | if (*ptr == ',') |
| 942 | layer = ++ptr; |
| 943 | for (; *ptr!=',' && ptr<end-1; ptr++); |
| 944 | if (*ptr == ',') { |
Aurelien Jacobs | e7d4b74 | 2008-09-28 22:55:28 | [diff] [blame] | 945 | int64_t end_pts = pkt->pts + display_duration; |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 946 | int sc = matroska->time_scale * pkt->pts / 10000000; |
| 947 | int ec = matroska->time_scale * end_pts / 10000000; |
| 948 | int sh, sm, ss, eh, em, es, len; |
| 949 | sh = sc/360000; sc -= 360000*sh; |
| 950 | sm = sc/ 6000; sc -= 6000*sm; |
| 951 | ss = sc/ 100; sc -= 100*ss; |
| 952 | eh = ec/360000; ec -= 360000*eh; |
| 953 | em = ec/ 6000; ec -= 6000*em; |
| 954 | es = ec/ 100; ec -= 100*es; |
| 955 | *ptr++ = '\0'; |
| 956 | len = 50 + end-ptr + FF_INPUT_BUFFER_PADDING_SIZE; |
| 957 | if (!(line = av_malloc(len))) |
| 958 | return; |
Aurelien Jacobs | 3df2be9 | 2008-09-28 23:01:07 | [diff] [blame] | 959 | snprintf(line,len,"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n", |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 960 | layer, sh, sm, ss, sc, eh, em, es, ec, ptr); |
| 961 | av_free(pkt->data); |
| 962 | pkt->data = line; |
| 963 | pkt->size = strlen(line); |
| 964 | } |
| 965 | } |
| 966 | |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 967 | static void matroska_merge_packets(AVPacket *out, AVPacket *in) |
| 968 | { |
| 969 | out->data = av_realloc(out->data, out->size+in->size); |
| 970 | memcpy(out->data+out->size, in->data, in->size); |
| 971 | out->size += in->size; |
| 972 | av_destruct_packet(in); |
| 973 | av_free(in); |
| 974 | } |
| 975 | |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 976 | static void matroska_convert_tags(AVFormatContext *s, EbmlList *list) |
| 977 | { |
| 978 | MatroskaTag *tags = list->elem; |
| 979 | int i, j; |
| 980 | |
| 981 | for (i=0; i < list->nb_elem; i++) { |
Aurelien Jacobs | 37d3e06 | 2008-10-21 21:40:24 | [diff] [blame] | 982 | for (j=0; j < FF_ARRAY_ELEMS(metadata); j++){ |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 983 | if (!strcmp(tags[i].name, metadata[j].name)) { |
| 984 | int *ptr = (int *)((char *)s + metadata[j].offset); |
| 985 | if (*ptr) continue; |
| 986 | if (metadata[j].size > sizeof(int)) |
| 987 | av_strlcpy((char *)ptr, tags[i].string, metadata[j].size); |
| 988 | else |
| 989 | *ptr = atoi(tags[i].string); |
| 990 | } |
| 991 | } |
| 992 | if (tags[i].sub.nb_elem) |
| 993 | matroska_convert_tags(s, &tags[i].sub); |
| 994 | } |
| 995 | } |
| 996 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 997 | static void matroska_execute_seekhead(MatroskaDemuxContext *matroska) |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 998 | { |
| 999 | EbmlList *seekhead_list = &matroska->seekhead; |
| 1000 | MatroskaSeekhead *seekhead = seekhead_list->elem; |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 1001 | uint32_t level_up = matroska->level_up; |
Diego Biurrun | bc5c918 | 2008-10-03 10:16:29 | [diff] [blame] | 1002 | int64_t before_pos = url_ftell(matroska->ctx->pb); |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 1003 | MatroskaLevel level; |
Aurelien Jacobs | f06a488 | 2008-08-05 00:41:01 | [diff] [blame] | 1004 | int i; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1005 | |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 1006 | for (i=0; i<seekhead_list->nb_elem; i++) { |
Diego Biurrun | bc5c918 | 2008-10-03 10:16:29 | [diff] [blame] | 1007 | int64_t offset = seekhead[i].pos + matroska->segment_start; |
Aurelien Jacobs | 66cfc38 | 2008-08-05 00:42:33 | [diff] [blame] | 1008 | |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 1009 | if (seekhead[i].pos <= before_pos |
| 1010 | || seekhead[i].id == MATROSKA_ID_SEEKHEAD |
| 1011 | || seekhead[i].id == MATROSKA_ID_CLUSTER) |
| 1012 | continue; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1013 | |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1014 | /* seek */ |
Aurelien Jacobs | 66cfc38 | 2008-08-05 00:42:33 | [diff] [blame] | 1015 | if (url_fseek(matroska->ctx->pb, offset, SEEK_SET) != offset) |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1016 | continue; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1017 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 1018 | /* We don't want to lose our seekhead level, so we add |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1019 | * a dummy. This is a crude hack. */ |
| 1020 | if (matroska->num_levels == EBML_MAX_DEPTH) { |
| 1021 | av_log(matroska->ctx, AV_LOG_INFO, |
| 1022 | "Max EBML element depth (%d) reached, " |
| 1023 | "cannot parse further.\n", EBML_MAX_DEPTH); |
| 1024 | break; |
| 1025 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1026 | |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1027 | level.start = 0; |
| 1028 | level.length = (uint64_t)-1; |
| 1029 | matroska->levels[matroska->num_levels] = level; |
| 1030 | matroska->num_levels++; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1031 | |
Aurelien Jacobs | 66a37e0 | 2008-08-05 00:42:17 | [diff] [blame] | 1032 | ebml_parse(matroska, matroska_segment, matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1033 | |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1034 | /* remove dummy level */ |
| 1035 | while (matroska->num_levels) { |
| 1036 | uint64_t length = matroska->levels[--matroska->num_levels].length; |
| 1037 | if (length == (uint64_t)-1) |
| 1038 | break; |
| 1039 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1040 | } |
| 1041 | |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1042 | /* seek back */ |
Aurelien Jacobs | 66cfc38 | 2008-08-05 00:42:33 | [diff] [blame] | 1043 | url_fseek(matroska->ctx->pb, before_pos, SEEK_SET); |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1044 | matroska->level_up = level_up; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1045 | } |
| 1046 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 1047 | static int matroska_aac_profile(char *codec_id) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1048 | { |
Aurelien Jacobs | ba18b99 | 2008-08-24 13:12:41 | [diff] [blame] | 1049 | static const char * const aac_profiles[] = { "MAIN", "LC", "SSR" }; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1050 | int profile; |
| 1051 | |
Aurelien Jacobs | 37d3e06 | 2008-10-21 21:40:24 | [diff] [blame] | 1052 | for (profile=0; profile<FF_ARRAY_ELEMS(aac_profiles); profile++) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1053 | if (strstr(codec_id, aac_profiles[profile])) |
| 1054 | break; |
| 1055 | return profile + 1; |
| 1056 | } |
| 1057 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 1058 | static int matroska_aac_sri(int samplerate) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1059 | { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1060 | int sri; |
| 1061 | |
Aurelien Jacobs | 37d3e06 | 2008-10-21 21:40:24 | [diff] [blame] | 1062 | for (sri=0; sri<FF_ARRAY_ELEMS(ff_mpeg4audio_sample_rates); sri++) |
Aurelien Jacobs | 7bfacd4 | 2008-04-02 21:41:48 | [diff] [blame] | 1063 | if (ff_mpeg4audio_sample_rates[sri] == samplerate) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1064 | break; |
| 1065 | return sri; |
| 1066 | } |
| 1067 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 1068 | static int matroska_read_header(AVFormatContext *s, AVFormatParameters *ap) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1069 | { |
| 1070 | MatroskaDemuxContext *matroska = s->priv_data; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1071 | EbmlList *attachements_list = &matroska->attachments; |
| 1072 | MatroskaAttachement *attachements; |
| 1073 | EbmlList *chapters_list = &matroska->chapters; |
| 1074 | MatroskaChapter *chapters; |
Aurelien Jacobs | 9a9a3b0 | 2008-08-05 00:40:49 | [diff] [blame] | 1075 | MatroskaTrack *tracks; |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 1076 | EbmlList *index_list; |
| 1077 | MatroskaIndex *index; |
Aurelien Jacobs | 8f569ed | 2008-11-15 15:34:51 | [diff] [blame] | 1078 | int index_scale = 1; |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 1079 | Ebml ebml = { 0 }; |
Aurelien Jacobs | 9a9a3b0 | 2008-08-05 00:40:49 | [diff] [blame] | 1080 | AVStream *st; |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 1081 | int i, j; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1082 | |
| 1083 | matroska->ctx = s; |
| 1084 | |
| 1085 | /* First read the EBML header. */ |
Aurelien Jacobs | c4d3d9b | 2008-08-05 00:42:20 | [diff] [blame] | 1086 | if (ebml_parse(matroska, ebml_syntax, &ebml) |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 1087 | || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t) |
| 1088 | || ebml.id_length > sizeof(uint32_t) || strcmp(ebml.doctype, "matroska") |
| 1089 | || ebml.doctype_version > 2) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1090 | av_log(matroska->ctx, AV_LOG_ERROR, |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 1091 | "EBML header using unsupported features\n" |
| 1092 | "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n", |
| 1093 | ebml.version, ebml.doctype, ebml.doctype_version); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1094 | return AVERROR_NOFMT; |
| 1095 | } |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 1096 | ebml_free(ebml_syntax, &ebml); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1097 | |
| 1098 | /* The next thing is a segment. */ |
Aurelien Jacobs | c4d3d9b | 2008-08-05 00:42:20 | [diff] [blame] | 1099 | if (ebml_parse(matroska, matroska_segments, matroska) < 0) |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 1100 | return -1; |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 1101 | matroska_execute_seekhead(matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1102 | |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1103 | if (matroska->duration) |
| 1104 | matroska->ctx->duration = matroska->duration * matroska->time_scale |
| 1105 | * 1000 / AV_TIME_BASE; |
| 1106 | if (matroska->title) |
| 1107 | strncpy(matroska->ctx->title, matroska->title, |
| 1108 | sizeof(matroska->ctx->title)-1); |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 1109 | matroska_convert_tags(s, &matroska->tags); |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1110 | |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1111 | tracks = matroska->tracks.elem; |
| 1112 | for (i=0; i < matroska->tracks.nb_elem; i++) { |
| 1113 | MatroskaTrack *track = &tracks[i]; |
| 1114 | enum CodecID codec_id = CODEC_ID_NONE; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1115 | EbmlList *encodings_list = &tracks->encodings; |
| 1116 | MatroskaTrackEncoding *encodings = encodings_list->elem; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1117 | uint8_t *extradata = NULL; |
| 1118 | int extradata_size = 0; |
| 1119 | int extradata_offset = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1120 | |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1121 | /* Apply some sanity checks. */ |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1122 | if (track->type != MATROSKA_TRACK_TYPE_VIDEO && |
| 1123 | track->type != MATROSKA_TRACK_TYPE_AUDIO && |
| 1124 | track->type != MATROSKA_TRACK_TYPE_SUBTITLE) { |
| 1125 | av_log(matroska->ctx, AV_LOG_INFO, |
| 1126 | "Unknown or unsupported track type %"PRIu64"\n", |
| 1127 | track->type); |
| 1128 | continue; |
| 1129 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1130 | if (track->codec_id == NULL) |
| 1131 | continue; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1132 | |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1133 | if (track->type == MATROSKA_TRACK_TYPE_VIDEO) { |
| 1134 | if (!track->default_duration) |
| 1135 | track->default_duration = 1000000000/track->video.frame_rate; |
| 1136 | if (!track->video.display_width) |
| 1137 | track->video.display_width = track->video.pixel_width; |
| 1138 | if (!track->video.display_height) |
| 1139 | track->video.display_height = track->video.pixel_height; |
| 1140 | } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { |
| 1141 | if (!track->audio.out_samplerate) |
| 1142 | track->audio.out_samplerate = track->audio.samplerate; |
| 1143 | } |
| 1144 | if (encodings_list->nb_elem > 1) { |
| 1145 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1146 | "Multiple combined encodings no supported"); |
| 1147 | } else if (encodings_list->nb_elem == 1) { |
| 1148 | if (encodings[0].type || |
| 1149 | (encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP && |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame^] | 1150 | #if CONFIG_ZLIB |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1151 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB && |
| 1152 | #endif |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame^] | 1153 | #if CONFIG_BZLIB |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1154 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB && |
| 1155 | #endif |
| 1156 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO)) { |
| 1157 | encodings[0].scope = 0; |
| 1158 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1159 | "Unsupported encoding type"); |
| 1160 | } else if (track->codec_priv.size && encodings[0].scope&2) { |
| 1161 | uint8_t *codec_priv = track->codec_priv.data; |
| 1162 | int offset = matroska_decode_buffer(&track->codec_priv.data, |
| 1163 | &track->codec_priv.size, |
| 1164 | track); |
| 1165 | if (offset < 0) { |
| 1166 | track->codec_priv.data = NULL; |
| 1167 | track->codec_priv.size = 0; |
| 1168 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1169 | "Failed to decode codec private data\n"); |
| 1170 | } else if (offset > 0) { |
| 1171 | track->codec_priv.data = av_malloc(track->codec_priv.size + offset); |
| 1172 | memcpy(track->codec_priv.data, |
| 1173 | encodings[0].compression.settings.data, offset); |
| 1174 | memcpy(track->codec_priv.data+offset, codec_priv, |
| 1175 | track->codec_priv.size); |
| 1176 | track->codec_priv.size += offset; |
| 1177 | } |
| 1178 | if (codec_priv != track->codec_priv.data) |
| 1179 | av_free(codec_priv); |
| 1180 | } |
| 1181 | } |
| 1182 | |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1183 | for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){ |
| 1184 | if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id, |
| 1185 | strlen(ff_mkv_codec_tags[j].str))){ |
| 1186 | codec_id= ff_mkv_codec_tags[j].id; |
| 1187 | break; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1188 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1189 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1190 | |
Aurelien Jacobs | cc70d14 | 2008-08-05 00:43:01 | [diff] [blame] | 1191 | st = track->stream = av_new_stream(s, 0); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1192 | if (st == NULL) |
| 1193 | return AVERROR(ENOMEM); |
| 1194 | |
Aurelien Jacobs | cc8be50 | 2008-08-05 00:42:49 | [diff] [blame] | 1195 | if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC") |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1196 | && track->codec_priv.size >= 40 |
| 1197 | && track->codec_priv.data != NULL) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1198 | track->video.fourcc = AV_RL32(track->codec_priv.data + 16); |
| 1199 | codec_id = codec_get_id(codec_bmp_tags, track->video.fourcc); |
Aurelien Jacobs | cc8be50 | 2008-08-05 00:42:49 | [diff] [blame] | 1200 | } else if (!strcmp(track->codec_id, "A_MS/ACM") |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1201 | && track->codec_priv.size >= 18 |
| 1202 | && track->codec_priv.data != NULL) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1203 | uint16_t tag = AV_RL16(track->codec_priv.data); |
| 1204 | codec_id = codec_get_id(codec_wav_tags, tag); |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1205 | } else if (!strcmp(track->codec_id, "V_QUICKTIME") |
| 1206 | && (track->codec_priv.size >= 86) |
| 1207 | && (track->codec_priv.data != NULL)) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1208 | track->video.fourcc = AV_RL32(track->codec_priv.data); |
| 1209 | codec_id=codec_get_id(codec_movvideo_tags, track->video.fourcc); |
Aurelien Jacobs | eb9cf50 | 2008-08-20 00:49:45 | [diff] [blame] | 1210 | } else if (codec_id == CODEC_ID_PCM_S16BE) { |
| 1211 | switch (track->audio.bitdepth) { |
| 1212 | case 8: codec_id = CODEC_ID_PCM_U8; break; |
| 1213 | case 24: codec_id = CODEC_ID_PCM_S24BE; break; |
| 1214 | case 32: codec_id = CODEC_ID_PCM_S32BE; break; |
| 1215 | } |
| 1216 | } else if (codec_id == CODEC_ID_PCM_S16LE) { |
| 1217 | switch (track->audio.bitdepth) { |
| 1218 | case 8: codec_id = CODEC_ID_PCM_U8; break; |
| 1219 | case 24: codec_id = CODEC_ID_PCM_S24LE; break; |
| 1220 | case 32: codec_id = CODEC_ID_PCM_S32LE; break; |
| 1221 | } |
| 1222 | } else if (codec_id==CODEC_ID_PCM_F32LE && track->audio.bitdepth==64) { |
| 1223 | codec_id = CODEC_ID_PCM_F64LE; |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1224 | } else if (codec_id == CODEC_ID_AAC && !track->codec_priv.size) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1225 | int profile = matroska_aac_profile(track->codec_id); |
| 1226 | int sri = matroska_aac_sri(track->audio.samplerate); |
| 1227 | extradata = av_malloc(5); |
| 1228 | if (extradata == NULL) |
Aurelien Jacobs | 28f450a | 2008-08-05 00:40:09 | [diff] [blame] | 1229 | return AVERROR(ENOMEM); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1230 | extradata[0] = (profile << 3) | ((sri&0x0E) >> 1); |
| 1231 | extradata[1] = ((sri&0x01) << 7) | (track->audio.channels<<3); |
| 1232 | if (strstr(track->codec_id, "SBR")) { |
| 1233 | sri = matroska_aac_sri(track->audio.out_samplerate); |
| 1234 | extradata[2] = 0x56; |
| 1235 | extradata[3] = 0xE5; |
| 1236 | extradata[4] = 0x80 | (sri<<3); |
| 1237 | extradata_size = 5; |
Aurelien Jacobs | 16f97ab | 2008-08-05 00:41:10 | [diff] [blame] | 1238 | } else |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1239 | extradata_size = 2; |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1240 | } else if (codec_id == CODEC_ID_TTA) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1241 | ByteIOContext b; |
| 1242 | extradata_size = 30; |
| 1243 | extradata = av_mallocz(extradata_size); |
| 1244 | if (extradata == NULL) |
| 1245 | return AVERROR(ENOMEM); |
| 1246 | init_put_byte(&b, extradata, extradata_size, 1, |
| 1247 | NULL, NULL, NULL, NULL); |
| 1248 | put_buffer(&b, "TTA1", 4); |
| 1249 | put_le16(&b, 1); |
| 1250 | put_le16(&b, track->audio.channels); |
| 1251 | put_le16(&b, track->audio.bitdepth); |
| 1252 | put_le32(&b, track->audio.out_samplerate); |
| 1253 | put_le32(&b, matroska->ctx->duration * track->audio.out_samplerate); |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1254 | } else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 || |
| 1255 | codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1256 | extradata_offset = 26; |
| 1257 | track->codec_priv.size -= extradata_offset; |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1258 | } else if (codec_id == CODEC_ID_RA_144) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1259 | track->audio.out_samplerate = 8000; |
| 1260 | track->audio.channels = 1; |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1261 | } else if (codec_id == CODEC_ID_RA_288 || codec_id == CODEC_ID_COOK || |
| 1262 | codec_id == CODEC_ID_ATRAC3) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1263 | ByteIOContext b; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1264 | |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1265 | init_put_byte(&b, track->codec_priv.data,track->codec_priv.size, |
| 1266 | 0, NULL, NULL, NULL, NULL); |
| 1267 | url_fskip(&b, 24); |
| 1268 | track->audio.coded_framesize = get_be32(&b); |
| 1269 | url_fskip(&b, 12); |
| 1270 | track->audio.sub_packet_h = get_be16(&b); |
| 1271 | track->audio.frame_size = get_be16(&b); |
| 1272 | track->audio.sub_packet_size = get_be16(&b); |
| 1273 | track->audio.buf = av_malloc(track->audio.frame_size * track->audio.sub_packet_h); |
| 1274 | if (codec_id == CODEC_ID_RA_288) { |
| 1275 | st->codec->block_align = track->audio.coded_framesize; |
| 1276 | track->codec_priv.size = 0; |
| 1277 | } else { |
| 1278 | st->codec->block_align = track->audio.sub_packet_size; |
| 1279 | extradata_offset = 78; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1280 | track->codec_priv.size -= extradata_offset; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1281 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1282 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1283 | |
Aurelien Jacobs | 16f97ab | 2008-08-05 00:41:10 | [diff] [blame] | 1284 | if (codec_id == CODEC_ID_NONE) |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1285 | av_log(matroska->ctx, AV_LOG_INFO, |
Aurelien Jacobs | 8f35a2c | 2008-08-05 00:41:22 | [diff] [blame] | 1286 | "Unknown/unsupported CodecID %s.\n", track->codec_id); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1287 | |
Aurelien Jacobs | 3fc9d7c | 2008-09-09 11:23:48 | [diff] [blame] | 1288 | if (track->time_scale < 0.01) |
| 1289 | track->time_scale = 1.0; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1290 | av_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */ |
| 1291 | |
| 1292 | st->codec->codec_id = codec_id; |
| 1293 | st->start_time = 0; |
| 1294 | if (strcmp(track->language, "und")) |
| 1295 | av_strlcpy(st->language, track->language, 4); |
| 1296 | |
| 1297 | if (track->flag_default) |
| 1298 | st->disposition |= AV_DISPOSITION_DEFAULT; |
| 1299 | |
| 1300 | if (track->default_duration) |
| 1301 | av_reduce(&st->codec->time_base.num, &st->codec->time_base.den, |
| 1302 | track->default_duration, 1000000000, 30000); |
| 1303 | |
| 1304 | if(extradata){ |
| 1305 | st->codec->extradata = extradata; |
| 1306 | st->codec->extradata_size = extradata_size; |
| 1307 | } else if(track->codec_priv.data && track->codec_priv.size > 0){ |
Alexander Strange | 1ca610c | 2008-12-19 01:00:44 | [diff] [blame] | 1308 | st->codec->extradata = av_mallocz(track->codec_priv.size + |
| 1309 | FF_INPUT_BUFFER_PADDING_SIZE); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1310 | if(st->codec->extradata == NULL) |
| 1311 | return AVERROR(ENOMEM); |
| 1312 | st->codec->extradata_size = track->codec_priv.size; |
| 1313 | memcpy(st->codec->extradata, |
| 1314 | track->codec_priv.data + extradata_offset, |
| 1315 | track->codec_priv.size); |
| 1316 | } |
| 1317 | |
| 1318 | if (track->type == MATROSKA_TRACK_TYPE_VIDEO) { |
| 1319 | st->codec->codec_type = CODEC_TYPE_VIDEO; |
| 1320 | st->codec->codec_tag = track->video.fourcc; |
| 1321 | st->codec->width = track->video.pixel_width; |
| 1322 | st->codec->height = track->video.pixel_height; |
Aurelien Jacobs | 5972945 | 2008-08-23 23:43:20 | [diff] [blame] | 1323 | av_reduce(&st->sample_aspect_ratio.num, |
| 1324 | &st->sample_aspect_ratio.den, |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1325 | st->codec->height * track->video.display_width, |
| 1326 | st->codec-> width * track->video.display_height, |
| 1327 | 255); |
| 1328 | st->need_parsing = AVSTREAM_PARSE_HEADERS; |
| 1329 | } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { |
| 1330 | st->codec->codec_type = CODEC_TYPE_AUDIO; |
| 1331 | st->codec->sample_rate = track->audio.out_samplerate; |
| 1332 | st->codec->channels = track->audio.channels; |
| 1333 | } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) { |
| 1334 | st->codec->codec_type = CODEC_TYPE_SUBTITLE; |
| 1335 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1336 | } |
| 1337 | |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1338 | attachements = attachements_list->elem; |
| 1339 | for (j=0; j<attachements_list->nb_elem; j++) { |
| 1340 | if (!(attachements[j].filename && attachements[j].mime && |
| 1341 | attachements[j].bin.data && attachements[j].bin.size > 0)) { |
| 1342 | av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n"); |
| 1343 | } else { |
Aurelien Jacobs | cc70d14 | 2008-08-05 00:43:01 | [diff] [blame] | 1344 | AVStream *st = av_new_stream(s, 0); |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1345 | if (st == NULL) |
| 1346 | break; |
| 1347 | st->filename = av_strdup(attachements[j].filename); |
| 1348 | st->codec->codec_id = CODEC_ID_NONE; |
| 1349 | st->codec->codec_type = CODEC_TYPE_ATTACHMENT; |
| 1350 | st->codec->extradata = av_malloc(attachements[j].bin.size); |
| 1351 | if(st->codec->extradata == NULL) |
| 1352 | break; |
| 1353 | st->codec->extradata_size = attachements[j].bin.size; |
| 1354 | memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size); |
| 1355 | |
| 1356 | for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) { |
| 1357 | if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime, |
| 1358 | strlen(ff_mkv_mime_tags[i].str))) { |
| 1359 | st->codec->codec_id = ff_mkv_mime_tags[i].id; |
| 1360 | break; |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | chapters = chapters_list->elem; |
| 1367 | for (i=0; i<chapters_list->nb_elem; i++) |
| 1368 | if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid) |
| 1369 | ff_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000}, |
| 1370 | chapters[i].start, chapters[i].end, |
| 1371 | chapters[i].title); |
| 1372 | |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 1373 | index_list = &matroska->index; |
| 1374 | index = index_list->elem; |
Aurelien Jacobs | 8f569ed | 2008-11-15 15:34:51 | [diff] [blame] | 1375 | if (index_list->nb_elem |
| 1376 | && index[0].time > 100000000000000/matroska->time_scale) { |
| 1377 | av_log(matroska->ctx, AV_LOG_WARNING, "Working around broken index.\n"); |
| 1378 | index_scale = matroska->time_scale; |
| 1379 | } |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 1380 | for (i=0; i<index_list->nb_elem; i++) { |
| 1381 | EbmlList *pos_list = &index[i].pos; |
| 1382 | MatroskaIndexPos *pos = pos_list->elem; |
| 1383 | for (j=0; j<pos_list->nb_elem; j++) { |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 1384 | MatroskaTrack *track = matroska_find_track_by_num(matroska, |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 1385 | pos[j].track); |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 1386 | if (track && track->stream) |
| 1387 | av_add_index_entry(track->stream, |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 1388 | pos[j].pos + matroska->segment_start, |
Aurelien Jacobs | 8f569ed | 2008-11-15 15:34:51 | [diff] [blame] | 1389 | index[i].time/index_scale, 0, 0, |
| 1390 | AVINDEX_KEYFRAME); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1391 | } |
| 1392 | } |
| 1393 | |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 1394 | return 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1395 | } |
| 1396 | |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1397 | /* |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1398 | * Put one packet in an application-supplied AVPacket struct. |
| 1399 | * Returns 0 on success or -1 on failure. |
| 1400 | */ |
| 1401 | static int matroska_deliver_packet(MatroskaDemuxContext *matroska, |
| 1402 | AVPacket *pkt) |
| 1403 | { |
| 1404 | if (matroska->num_packets > 0) { |
| 1405 | memcpy(pkt, matroska->packets[0], sizeof(AVPacket)); |
| 1406 | av_free(matroska->packets[0]); |
| 1407 | if (matroska->num_packets > 1) { |
| 1408 | memmove(&matroska->packets[0], &matroska->packets[1], |
| 1409 | (matroska->num_packets - 1) * sizeof(AVPacket *)); |
| 1410 | matroska->packets = |
| 1411 | av_realloc(matroska->packets, (matroska->num_packets - 1) * |
| 1412 | sizeof(AVPacket *)); |
| 1413 | } else { |
| 1414 | av_freep(&matroska->packets); |
| 1415 | } |
| 1416 | matroska->num_packets--; |
| 1417 | return 0; |
| 1418 | } |
| 1419 | |
| 1420 | return -1; |
| 1421 | } |
| 1422 | |
| 1423 | /* |
| 1424 | * Free all packets in our internal queue. |
| 1425 | */ |
| 1426 | static void matroska_clear_queue(MatroskaDemuxContext *matroska) |
| 1427 | { |
| 1428 | if (matroska->packets) { |
| 1429 | int n; |
| 1430 | for (n = 0; n < matroska->num_packets; n++) { |
| 1431 | av_free_packet(matroska->packets[n]); |
| 1432 | av_free(matroska->packets[n]); |
| 1433 | } |
Aurelien Jacobs | 00a3431 | 2008-08-06 00:21:10 | [diff] [blame] | 1434 | av_freep(&matroska->packets); |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1435 | matroska->num_packets = 0; |
| 1436 | } |
| 1437 | } |
| 1438 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 1439 | static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, |
| 1440 | int size, int64_t pos, uint64_t cluster_time, |
Aurelien Jacobs | 24c3da1 | 2008-09-06 23:39:59 | [diff] [blame] | 1441 | uint64_t duration, int is_keyframe, |
| 1442 | int64_t cluster_pos) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1443 | { |
Aurelien Jacobs | f14a201 | 2008-09-09 11:54:35 | [diff] [blame] | 1444 | uint64_t timecode = AV_NOPTS_VALUE; |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 1445 | MatroskaTrack *track; |
Aurelien Jacobs | a3467f8 | 2008-09-06 23:44:29 | [diff] [blame] | 1446 | int res = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1447 | AVStream *st; |
| 1448 | AVPacket *pkt; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1449 | int16_t block_time; |
| 1450 | uint32_t *lace_size = NULL; |
| 1451 | int n, flags, laces = 0; |
| 1452 | uint64_t num; |
| 1453 | |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 1454 | if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1455 | av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n"); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1456 | return res; |
| 1457 | } |
| 1458 | data += n; |
| 1459 | size -= n; |
| 1460 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1461 | track = matroska_find_track_by_num(matroska, num); |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 1462 | if (size <= 3 || !track || !track->stream) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1463 | av_log(matroska->ctx, AV_LOG_INFO, |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 1464 | "Invalid stream %"PRIu64" or size %u\n", num, size); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1465 | return res; |
| 1466 | } |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 1467 | st = track->stream; |
Aurelien Jacobs | 16f97ab | 2008-08-05 00:41:10 | [diff] [blame] | 1468 | if (st->discard >= AVDISCARD_ALL) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1469 | return res; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1470 | if (duration == AV_NOPTS_VALUE) |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 1471 | duration = track->default_duration / matroska->time_scale; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1472 | |
Aurelien Jacobs | 2ce746c | 2007-06-23 12:32:19 | [diff] [blame] | 1473 | block_time = AV_RB16(data); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1474 | data += 2; |
Aurelien Jacobs | 1607c53 | 2007-06-23 12:49:36 | [diff] [blame] | 1475 | flags = *data++; |
| 1476 | size -= 3; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1477 | if (is_keyframe == -1) |
David Conrad | 84fa6e2 | 2007-08-31 18:24:09 | [diff] [blame] | 1478 | is_keyframe = flags & 0x80 ? PKT_FLAG_KEY : 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1479 | |
Aurelien Jacobs | f14a201 | 2008-09-09 11:54:35 | [diff] [blame] | 1480 | if (cluster_time != (uint64_t)-1 |
| 1481 | && (block_time >= 0 || cluster_time >= -block_time)) { |
| 1482 | timecode = cluster_time + block_time; |
Aurelien Jacobs | 82360e6 | 2008-09-09 12:07:10 | [diff] [blame] | 1483 | if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE |
| 1484 | && timecode < track->end_timecode) |
| 1485 | is_keyframe = 0; /* overlapping subtitles are not key frame */ |
Aurelien Jacobs | a8fd7e7 | 2008-09-12 00:06:06 | [diff] [blame] | 1486 | if (is_keyframe) |
Aurelien Jacobs | f14a201 | 2008-09-09 11:54:35 | [diff] [blame] | 1487 | av_add_index_entry(st, cluster_pos, timecode, 0,0,AVINDEX_KEYFRAME); |
Aurelien Jacobs | 82360e6 | 2008-09-09 12:07:10 | [diff] [blame] | 1488 | track->end_timecode = FFMAX(track->end_timecode, timecode+duration); |
Aurelien Jacobs | f14a201 | 2008-09-09 11:54:35 | [diff] [blame] | 1489 | } |
| 1490 | |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 1491 | if (matroska->skip_to_keyframe && track->type != MATROSKA_TRACK_TYPE_SUBTITLE) { |
Aurelien Jacobs | 20f7466 | 2008-09-09 12:01:51 | [diff] [blame] | 1492 | if (!is_keyframe || timecode < matroska->skip_to_timecode) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1493 | return res; |
| 1494 | matroska->skip_to_keyframe = 0; |
| 1495 | } |
| 1496 | |
| 1497 | switch ((flags & 0x06) >> 1) { |
| 1498 | case 0x0: /* no lacing */ |
| 1499 | laces = 1; |
| 1500 | lace_size = av_mallocz(sizeof(int)); |
| 1501 | lace_size[0] = size; |
| 1502 | break; |
| 1503 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 1504 | case 0x1: /* Xiph lacing */ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1505 | case 0x2: /* fixed-size lacing */ |
| 1506 | case 0x3: /* EBML lacing */ |
Michael Niedermayer | 9bf8b56 | 2008-05-28 21:22:08 | [diff] [blame] | 1507 | assert(size>0); // size <=3 is checked before size-=3 above |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1508 | laces = (*data) + 1; |
| 1509 | data += 1; |
| 1510 | size -= 1; |
| 1511 | lace_size = av_mallocz(laces * sizeof(int)); |
| 1512 | |
| 1513 | switch ((flags & 0x06) >> 1) { |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 1514 | case 0x1: /* Xiph lacing */ { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1515 | uint8_t temp; |
| 1516 | uint32_t total = 0; |
| 1517 | for (n = 0; res == 0 && n < laces - 1; n++) { |
| 1518 | while (1) { |
| 1519 | if (size == 0) { |
| 1520 | res = -1; |
| 1521 | break; |
| 1522 | } |
| 1523 | temp = *data; |
| 1524 | lace_size[n] += temp; |
| 1525 | data += 1; |
| 1526 | size -= 1; |
| 1527 | if (temp != 0xff) |
| 1528 | break; |
| 1529 | } |
| 1530 | total += lace_size[n]; |
| 1531 | } |
| 1532 | lace_size[n] = size - total; |
| 1533 | break; |
| 1534 | } |
| 1535 | |
| 1536 | case 0x2: /* fixed-size lacing */ |
| 1537 | for (n = 0; n < laces; n++) |
| 1538 | lace_size[n] = size / laces; |
| 1539 | break; |
| 1540 | |
| 1541 | case 0x3: /* EBML lacing */ { |
| 1542 | uint32_t total; |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 1543 | n = matroska_ebmlnum_uint(matroska, data, size, &num); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1544 | if (n < 0) { |
| 1545 | av_log(matroska->ctx, AV_LOG_INFO, |
| 1546 | "EBML block data error\n"); |
| 1547 | break; |
| 1548 | } |
| 1549 | data += n; |
| 1550 | size -= n; |
| 1551 | total = lace_size[0] = num; |
| 1552 | for (n = 1; res == 0 && n < laces - 1; n++) { |
| 1553 | int64_t snum; |
| 1554 | int r; |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 1555 | r = matroska_ebmlnum_sint(matroska, data, size, &snum); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1556 | if (r < 0) { |
| 1557 | av_log(matroska->ctx, AV_LOG_INFO, |
| 1558 | "EBML block data error\n"); |
| 1559 | break; |
| 1560 | } |
| 1561 | data += r; |
| 1562 | size -= r; |
| 1563 | lace_size[n] = lace_size[n - 1] + snum; |
| 1564 | total += lace_size[n]; |
| 1565 | } |
| 1566 | lace_size[n] = size - total; |
| 1567 | break; |
| 1568 | } |
| 1569 | } |
| 1570 | break; |
| 1571 | } |
| 1572 | |
| 1573 | if (res == 0) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1574 | for (n = 0; n < laces; n++) { |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1575 | if (st->codec->codec_id == CODEC_ID_RA_288 || |
| 1576 | st->codec->codec_id == CODEC_ID_COOK || |
| 1577 | st->codec->codec_id == CODEC_ID_ATRAC3) { |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1578 | int a = st->codec->block_align; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1579 | int sps = track->audio.sub_packet_size; |
| 1580 | int cfs = track->audio.coded_framesize; |
| 1581 | int h = track->audio.sub_packet_h; |
| 1582 | int y = track->audio.sub_packet_cnt; |
| 1583 | int w = track->audio.frame_size; |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1584 | int x; |
Aurelien Jacobs | eabb8ba | 2007-06-04 22:19:17 | [diff] [blame] | 1585 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1586 | if (!track->audio.pkt_cnt) { |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1587 | if (st->codec->codec_id == CODEC_ID_RA_288) |
| 1588 | for (x=0; x<h/2; x++) |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1589 | memcpy(track->audio.buf+x*2*w+y*cfs, |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1590 | data+x*cfs, cfs); |
| 1591 | else |
| 1592 | for (x=0; x<w/sps; x++) |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1593 | memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps); |
Aurelien Jacobs | eabb8ba | 2007-06-04 22:19:17 | [diff] [blame] | 1594 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1595 | if (++track->audio.sub_packet_cnt >= h) { |
| 1596 | track->audio.sub_packet_cnt = 0; |
| 1597 | track->audio.pkt_cnt = h*w / a; |
Aurelien Jacobs | eabb8ba | 2007-06-04 22:19:17 | [diff] [blame] | 1598 | } |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1599 | } |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1600 | while (track->audio.pkt_cnt) { |
Aurelien Jacobs | 77abe5e | 2007-06-04 22:21:29 | [diff] [blame] | 1601 | pkt = av_mallocz(sizeof(AVPacket)); |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1602 | av_new_packet(pkt, a); |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1603 | memcpy(pkt->data, track->audio.buf |
| 1604 | + a * (h*w / a - track->audio.pkt_cnt--), a); |
Aurelien Jacobs | 77abe5e | 2007-06-04 22:21:29 | [diff] [blame] | 1605 | pkt->pos = pos; |
Aurelien Jacobs | fc4d335 | 2008-08-05 00:40:06 | [diff] [blame] | 1606 | pkt->stream_index = st->index; |
Anton Khirnov | b870253 | 2008-08-06 00:17:47 | [diff] [blame] | 1607 | dynarray_add(&matroska->packets,&matroska->num_packets,pkt); |
Aurelien Jacobs | eabb8ba | 2007-06-04 22:19:17 | [diff] [blame] | 1608 | } |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1609 | } else { |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1610 | MatroskaTrackEncoding *encodings = track->encodings.elem; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1611 | int offset = 0, pkt_size = lace_size[n]; |
Aurelien Jacobs | de3230f | 2008-05-09 01:53:59 | [diff] [blame] | 1612 | uint8_t *pkt_data = data; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1613 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1614 | if (encodings && encodings->scope & 1) { |
Aurelien Jacobs | 8f35a2c | 2008-08-05 00:41:22 | [diff] [blame] | 1615 | offset = matroska_decode_buffer(&pkt_data,&pkt_size, track); |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1616 | if (offset < 0) |
| 1617 | continue; |
Aurelien Jacobs | 53a1e82 | 2008-05-08 21:47:31 | [diff] [blame] | 1618 | } |
| 1619 | |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1620 | pkt = av_mallocz(sizeof(AVPacket)); |
| 1621 | /* XXX: prevent data copy... */ |
Aurelien Jacobs | de3230f | 2008-05-09 01:53:59 | [diff] [blame] | 1622 | if (av_new_packet(pkt, pkt_size+offset) < 0) { |
Aurelien Jacobs | 34ae409 | 2008-06-02 23:27:14 | [diff] [blame] | 1623 | av_free(pkt); |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1624 | res = AVERROR(ENOMEM); |
| 1625 | n = laces-1; |
| 1626 | break; |
| 1627 | } |
Aurelien Jacobs | 53a1e82 | 2008-05-08 21:47:31 | [diff] [blame] | 1628 | if (offset) |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1629 | memcpy (pkt->data, encodings->compression.settings.data, offset); |
Aurelien Jacobs | de3230f | 2008-05-09 01:53:59 | [diff] [blame] | 1630 | memcpy (pkt->data+offset, pkt_data, pkt_size); |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1631 | |
Aurelien Jacobs | 51e1cc1 | 2008-06-22 15:46:36 | [diff] [blame] | 1632 | if (pkt_data != data) |
| 1633 | av_free(pkt_data); |
| 1634 | |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1635 | if (n == 0) |
| 1636 | pkt->flags = is_keyframe; |
Aurelien Jacobs | fc4d335 | 2008-08-05 00:40:06 | [diff] [blame] | 1637 | pkt->stream_index = st->index; |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1638 | |
| 1639 | pkt->pts = timecode; |
| 1640 | pkt->pos = pos; |
Aurelien Jacobs | 1bb4a1a | 2008-09-28 22:58:53 | [diff] [blame] | 1641 | if (st->codec->codec_id == CODEC_ID_TEXT) |
Aurelien Jacobs | 62c2470 | 2008-09-04 23:08:19 | [diff] [blame] | 1642 | pkt->convergence_duration = duration; |
Aurelien Jacobs | 1bb4a1a | 2008-09-28 22:58:53 | [diff] [blame] | 1643 | else if (track->type != MATROSKA_TRACK_TYPE_SUBTITLE) |
Aurelien Jacobs | 62c2470 | 2008-09-04 23:08:19 | [diff] [blame] | 1644 | pkt->duration = duration; |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1645 | |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 1646 | if (st->codec->codec_id == CODEC_ID_SSA) |
Aurelien Jacobs | e7d4b74 | 2008-09-28 22:55:28 | [diff] [blame] | 1647 | matroska_fix_ass_packet(matroska, pkt, duration); |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 1648 | |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 1649 | if (matroska->prev_pkt && |
Aurelien Jacobs | 21a115d | 2008-10-02 21:14:56 | [diff] [blame] | 1650 | timecode != AV_NOPTS_VALUE && |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 1651 | matroska->prev_pkt->pts == timecode && |
| 1652 | matroska->prev_pkt->stream_index == st->index) |
| 1653 | matroska_merge_packets(matroska->prev_pkt, pkt); |
| 1654 | else { |
Aurelien Jacobs | c58e8bd | 2008-10-02 21:15:48 | [diff] [blame] | 1655 | dynarray_add(&matroska->packets,&matroska->num_packets,pkt); |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 1656 | matroska->prev_pkt = pkt; |
| 1657 | } |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | if (timecode != AV_NOPTS_VALUE) |
| 1661 | timecode = duration ? timecode + duration : AV_NOPTS_VALUE; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1662 | data += lace_size[n]; |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | av_free(lace_size); |
Aurelien Jacobs | a3467f8 | 2008-09-06 23:44:29 | [diff] [blame] | 1667 | return res; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1668 | } |
| 1669 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 1670 | static int matroska_parse_cluster(MatroskaDemuxContext *matroska) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1671 | { |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 1672 | MatroskaCluster cluster = { 0 }; |
| 1673 | EbmlList *blocks_list; |
| 1674 | MatroskaBlock *blocks; |
Aurelien Jacobs | 24c3da1 | 2008-09-06 23:39:59 | [diff] [blame] | 1675 | int i, res; |
Diego Biurrun | bc5c918 | 2008-10-03 10:16:29 | [diff] [blame] | 1676 | int64_t pos = url_ftell(matroska->ctx->pb); |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 1677 | matroska->prev_pkt = NULL; |
Aurelien Jacobs | 3879763 | 2008-08-05 00:42:26 | [diff] [blame] | 1678 | if (matroska->has_cluster_id){ |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 1679 | /* For the first cluster we parse, its ID was already read as |
Aurelien Jacobs | 3879763 | 2008-08-05 00:42:26 | [diff] [blame] | 1680 | part of matroska_read_header(), so don't read it again */ |
| 1681 | res = ebml_parse_id(matroska, matroska_clusters, |
| 1682 | MATROSKA_ID_CLUSTER, &cluster); |
Aurelien Jacobs | 8bc98ba | 2008-08-25 00:09:08 | [diff] [blame] | 1683 | pos -= 4; /* sizeof the ID which was already read */ |
Aurelien Jacobs | 3879763 | 2008-08-05 00:42:26 | [diff] [blame] | 1684 | matroska->has_cluster_id = 0; |
| 1685 | } else |
| 1686 | res = ebml_parse(matroska, matroska_clusters, &cluster); |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 1687 | blocks_list = &cluster.blocks; |
| 1688 | blocks = blocks_list->elem; |
Aurelien Jacobs | 131f1cb | 2008-08-13 09:36:45 | [diff] [blame] | 1689 | for (i=0; i<blocks_list->nb_elem; i++) |
Aurelien Jacobs | dbfb0e5 | 2008-09-06 23:43:24 | [diff] [blame] | 1690 | if (blocks[i].bin.size > 0) |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 1691 | res=matroska_parse_block(matroska, |
| 1692 | blocks[i].bin.data, blocks[i].bin.size, |
| 1693 | blocks[i].bin.pos, cluster.timecode, |
Aurelien Jacobs | 24c3da1 | 2008-09-06 23:39:59 | [diff] [blame] | 1694 | blocks[i].duration, !blocks[i].reference, |
| 1695 | pos); |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 1696 | ebml_free(matroska_cluster, &cluster); |
Aurelien Jacobs | 653fb2f | 2008-08-24 23:54:14 | [diff] [blame] | 1697 | if (res < 0) matroska->done = 1; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1698 | return res; |
| 1699 | } |
| 1700 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 1701 | static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1702 | { |
| 1703 | MatroskaDemuxContext *matroska = s->priv_data; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1704 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1705 | while (matroska_deliver_packet(matroska, pkt)) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1706 | if (matroska->done) |
Panagiotis Issaris | 6f3e0b2 | 2007-07-19 15:23:32 | [diff] [blame] | 1707 | return AVERROR(EIO); |
Aurelien Jacobs | 653fb2f | 2008-08-24 23:54:14 | [diff] [blame] | 1708 | matroska_parse_cluster(matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | return 0; |
| 1712 | } |
| 1713 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 1714 | static int matroska_read_seek(AVFormatContext *s, int stream_index, |
| 1715 | int64_t timestamp, int flags) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1716 | { |
| 1717 | MatroskaDemuxContext *matroska = s->priv_data; |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 1718 | MatroskaTrack *tracks = matroska->tracks.elem; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1719 | AVStream *st = s->streams[stream_index]; |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 1720 | int i, index, index_sub, index_min; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1721 | |
Aurelien Jacobs | a8fd7e7 | 2008-09-12 00:06:06 | [diff] [blame] | 1722 | if (!st->nb_index_entries) |
| 1723 | return 0; |
| 1724 | timestamp = FFMAX(timestamp, st->index_entries[0].timestamp); |
Aurelien Jacobs | dfbbbdc | 2008-08-24 23:57:29 | [diff] [blame] | 1725 | |
Aurelien Jacobs | 6bef5f9 | 2008-08-27 19:57:42 | [diff] [blame] | 1726 | if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) { |
Aurelien Jacobs | a8fd7e7 | 2008-09-12 00:06:06 | [diff] [blame] | 1727 | url_fseek(s->pb, st->index_entries[st->nb_index_entries-1].pos, SEEK_SET); |
Aurelien Jacobs | 0dbddda | 2008-08-27 19:58:55 | [diff] [blame] | 1728 | while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) { |
| 1729 | matroska_clear_queue(matroska); |
| 1730 | if (matroska_parse_cluster(matroska) < 0) |
| 1731 | break; |
| 1732 | } |
Aurelien Jacobs | 6bef5f9 | 2008-08-27 19:57:42 | [diff] [blame] | 1733 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1734 | |
Aurelien Jacobs | 243cc4c | 2007-12-29 18:35:38 | [diff] [blame] | 1735 | matroska_clear_queue(matroska); |
Aurelien Jacobs | 0f646a2 | 2008-08-25 00:15:49 | [diff] [blame] | 1736 | if (index < 0) |
| 1737 | return 0; |
Aurelien Jacobs | 243cc4c | 2007-12-29 18:35:38 | [diff] [blame] | 1738 | |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 1739 | index_min = index; |
| 1740 | for (i=0; i < matroska->tracks.nb_elem; i++) { |
| 1741 | tracks[i].end_timecode = 0; |
| 1742 | if (tracks[i].type == MATROSKA_TRACK_TYPE_SUBTITLE |
| 1743 | && !tracks[i].stream->discard != AVDISCARD_ALL) { |
| 1744 | index_sub = av_index_search_timestamp(tracks[i].stream, st->index_entries[index].timestamp, AVSEEK_FLAG_BACKWARD); |
| 1745 | if (index_sub >= 0 |
| 1746 | && st->index_entries[index_sub].pos < st->index_entries[index_min].pos |
| 1747 | && st->index_entries[index].timestamp - st->index_entries[index_sub].timestamp < 30000000000/matroska->time_scale) |
| 1748 | index_min = index_sub; |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | url_fseek(s->pb, st->index_entries[index_min].pos, SEEK_SET); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1753 | matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY); |
Aurelien Jacobs | 20f7466 | 2008-09-09 12:01:51 | [diff] [blame] | 1754 | matroska->skip_to_timecode = st->index_entries[index].timestamp; |
Aurelien Jacobs | 244ee48 | 2008-08-25 00:17:31 | [diff] [blame] | 1755 | matroska->done = 0; |
Joakim Plate | de6a9a2 | 2008-06-11 19:54:17 | [diff] [blame] | 1756 | av_update_cur_dts(s, st, st->index_entries[index].timestamp); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1757 | return 0; |
| 1758 | } |
| 1759 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 1760 | static int matroska_read_close(AVFormatContext *s) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1761 | { |
| 1762 | MatroskaDemuxContext *matroska = s->priv_data; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1763 | MatroskaTrack *tracks = matroska->tracks.elem; |
Aurelien Jacobs | 70109c0 | 2008-08-05 00:41:13 | [diff] [blame] | 1764 | int n; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1765 | |
Aurelien Jacobs | 34c9c1b | 2007-12-29 18:32:47 | [diff] [blame] | 1766 | matroska_clear_queue(matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1767 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1768 | for (n=0; n < matroska->tracks.nb_elem; n++) |
| 1769 | if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO) |
| 1770 | av_free(tracks[n].audio.buf); |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 1771 | ebml_free(matroska_segment, matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1772 | |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
| 1776 | AVInputFormat matroska_demuxer = { |
| 1777 | "matroska", |
Stefano Sabatini | bde15e7 | 2008-06-03 16:20:54 | [diff] [blame] | 1778 | NULL_IF_CONFIG_SMALL("Matroska file format"), |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1779 | sizeof(MatroskaDemuxContext), |
| 1780 | matroska_probe, |
| 1781 | matroska_read_header, |
| 1782 | matroska_read_packet, |
| 1783 | matroska_read_close, |
| 1784 | matroska_read_seek, |
| 1785 | }; |