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