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 |
Mans Rullgard | 2912e87 | 2011-03-18 17:35:10 | [diff] [blame] | 3 | * Copyright (c) 2003-2008 The Libav Project |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 4 | * |
Mans Rullgard | 2912e87 | 2011-03-18 17:35:10 | [diff] [blame] | 5 | * This file is part of Libav. |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 6 | * |
Mans Rullgard | 2912e87 | 2011-03-18 17:35:10 | [diff] [blame] | 7 | * Libav is free software; you can redistribute it and/or |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 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 | * |
Mans Rullgard | 2912e87 | 2011-03-18 17:35:10 | [diff] [blame] | 12 | * Libav is distributed in the hope that it will be useful, |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 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 |
Mans Rullgard | 2912e87 | 2011-03-18 17:35:10 | [diff] [blame] | 18 | * License along with Libav; if not, write to the Free Software |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | /** |
Diego Biurrun | ba87f08 | 2010-04-20 14:45:34 | [diff] [blame] | 23 | * @file |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 24 | * Matroska file demuxer |
Diego Biurrun | e873c03 | 2011-10-30 18:10:50 | [diff] [blame] | 25 | * @author Ronald Bultje <[email protected]> |
| 26 | * @author with a little help from Moritz Bunkus <[email protected]> |
| 27 | * @author totally reworked by Aurelien Jacobs <[email protected]> |
| 28 | * @see specs available on the Matroska project page: http://www.matroska.org/ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 29 | */ |
| 30 | |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 31 | #include <stdio.h> |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 32 | #include "avformat.h" |
Stefano Sabatini | 9a2cb05 | 2010-04-11 21:44:23 | [diff] [blame] | 33 | #include "internal.h" |
Anton Khirnov | e731b8d | 2011-02-20 10:04:13 | [diff] [blame] | 34 | #include "avio_internal.h" |
Daniel Verkamp | 1a40491 | 2009-06-22 23:09:34 | [diff] [blame] | 35 | /* For ff_codec_get_id(). */ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 36 | #include "riff.h" |
Aurelien Jacobs | f009e36 | 2008-07-27 15:11:04 | [diff] [blame] | 37 | #include "isom.h" |
Aurelien Jacobs | 6b10228 | 2010-03-12 23:49:06 | [diff] [blame] | 38 | #include "rm.h" |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 39 | #include "matroska.h" |
Aurelien Jacobs | 7bfacd4 | 2008-04-02 21:41:48 | [diff] [blame] | 40 | #include "libavcodec/mpeg4audio.h" |
Mans Rullgard | 3383a53 | 2011-11-27 14:04:16 | [diff] [blame] | 41 | #include "libavutil/intfloat.h" |
Diego Biurrun | 6a5d31a | 2009-01-11 22:19:48 | [diff] [blame] | 42 | #include "libavutil/intreadwrite.h" |
Aurelien Jacobs | 5f8e022 | 2008-08-05 00:39:47 | [diff] [blame] | 43 | #include "libavutil/avstring.h" |
Aurelien Jacobs | de3230f | 2008-05-09 01:53:59 | [diff] [blame] | 44 | #include "libavutil/lzo.h" |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 45 | #include "libavutil/dict.h" |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame] | 46 | #if CONFIG_ZLIB |
Aurelien Jacobs | fbb878c | 2008-05-13 23:32:13 | [diff] [blame] | 47 | #include <zlib.h> |
| 48 | #endif |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame] | 49 | #if CONFIG_BZLIB |
Aurelien Jacobs | 54dddf0 | 2008-05-15 23:12:41 | [diff] [blame] | 50 | #include <bzlib.h> |
| 51 | #endif |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 52 | |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 53 | typedef enum { |
| 54 | EBML_NONE, |
| 55 | EBML_UINT, |
| 56 | EBML_FLOAT, |
| 57 | EBML_STR, |
| 58 | EBML_UTF8, |
| 59 | EBML_BIN, |
| 60 | EBML_NEST, |
| 61 | EBML_PASS, |
| 62 | EBML_STOP, |
Reimar Döffinger | 95ec3d4 | 2011-02-06 10:32:03 | [diff] [blame] | 63 | EBML_TYPE_COUNT |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 64 | } EbmlType; |
| 65 | |
| 66 | typedef const struct EbmlSyntax { |
| 67 | uint32_t id; |
| 68 | EbmlType type; |
| 69 | int list_elem_size; |
| 70 | int data_offset; |
| 71 | union { |
| 72 | uint64_t u; |
| 73 | double f; |
| 74 | const char *s; |
| 75 | const struct EbmlSyntax *n; |
| 76 | } def; |
| 77 | } EbmlSyntax; |
| 78 | |
| 79 | typedef struct { |
| 80 | int nb_elem; |
| 81 | void *elem; |
| 82 | } EbmlList; |
| 83 | |
| 84 | typedef struct { |
| 85 | int size; |
| 86 | uint8_t *data; |
| 87 | int64_t pos; |
| 88 | } EbmlBin; |
| 89 | |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 90 | typedef struct { |
| 91 | uint64_t version; |
| 92 | uint64_t max_size; |
| 93 | uint64_t id_length; |
| 94 | char *doctype; |
| 95 | uint64_t doctype_version; |
| 96 | } Ebml; |
| 97 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 98 | typedef struct { |
| 99 | uint64_t algo; |
| 100 | EbmlBin settings; |
| 101 | } MatroskaTrackCompression; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 102 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 103 | typedef struct { |
| 104 | uint64_t scope; |
| 105 | uint64_t type; |
| 106 | MatroskaTrackCompression compression; |
| 107 | } MatroskaTrackEncoding; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 108 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 109 | typedef struct { |
| 110 | double frame_rate; |
| 111 | uint64_t display_width; |
| 112 | uint64_t display_height; |
| 113 | uint64_t pixel_width; |
| 114 | uint64_t pixel_height; |
| 115 | uint64_t fourcc; |
| 116 | } MatroskaTrackVideo; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 117 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 118 | typedef struct { |
| 119 | double samplerate; |
| 120 | double out_samplerate; |
| 121 | uint64_t bitdepth; |
| 122 | uint64_t channels; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 123 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 124 | /* real audio header (extracted from extradata) */ |
| 125 | int coded_framesize; |
| 126 | int sub_packet_h; |
| 127 | int frame_size; |
| 128 | int sub_packet_size; |
| 129 | int sub_packet_cnt; |
| 130 | int pkt_cnt; |
Reimar Döffinger | 4654420 | 2011-02-26 11:52:01 | [diff] [blame] | 131 | uint64_t buf_timecode; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 132 | uint8_t *buf; |
| 133 | } MatroskaTrackAudio; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 134 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 135 | typedef struct { |
| 136 | uint64_t num; |
Aurelien Jacobs | 325ace3 | 2009-02-15 15:34:22 | [diff] [blame] | 137 | uint64_t uid; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 138 | uint64_t type; |
Aurelien Jacobs | 38766e0 | 2009-02-15 15:29:09 | [diff] [blame] | 139 | char *name; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 140 | char *codec_id; |
| 141 | EbmlBin codec_priv; |
| 142 | char *language; |
Anton Khirnov | 7ff9708 | 2008-06-01 13:54:11 | [diff] [blame] | 143 | double time_scale; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 144 | uint64_t default_duration; |
Aurelien Jacobs | 4eff974 | 2008-08-05 00:39:53 | [diff] [blame] | 145 | uint64_t flag_default; |
Aurelien Jacobs | 7a617a8 | 2010-07-02 16:38:44 | [diff] [blame] | 146 | uint64_t flag_forced; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 147 | MatroskaTrackVideo video; |
| 148 | MatroskaTrackAudio audio; |
| 149 | EbmlList encodings; |
Aurelien Jacobs | fc4d335 | 2008-08-05 00:40:06 | [diff] [blame] | 150 | |
| 151 | AVStream *stream; |
Aurelien Jacobs | 82360e6 | 2008-09-09 12:07:10 | [diff] [blame] | 152 | int64_t end_timecode; |
Joakim Plate | 3e93c8e | 2010-03-03 21:46:43 | [diff] [blame] | 153 | int ms_compat; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 154 | } MatroskaTrack; |
| 155 | |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 156 | typedef struct { |
Aurelien Jacobs | 325ace3 | 2009-02-15 15:34:22 | [diff] [blame] | 157 | uint64_t uid; |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 158 | char *filename; |
| 159 | char *mime; |
| 160 | EbmlBin bin; |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 161 | |
| 162 | AVStream *stream; |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 163 | } MatroskaAttachement; |
| 164 | |
| 165 | typedef struct { |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 166 | uint64_t start; |
| 167 | uint64_t end; |
| 168 | uint64_t uid; |
| 169 | char *title; |
Aurelien Jacobs | 6cb6e15 | 2009-02-15 15:25:14 | [diff] [blame] | 170 | |
| 171 | AVChapter *chapter; |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 172 | } MatroskaChapter; |
| 173 | |
| 174 | typedef struct { |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 175 | uint64_t track; |
| 176 | uint64_t pos; |
| 177 | } MatroskaIndexPos; |
| 178 | |
| 179 | typedef struct { |
| 180 | uint64_t time; |
| 181 | EbmlList pos; |
| 182 | } MatroskaIndex; |
| 183 | |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 184 | typedef struct { |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 185 | char *name; |
| 186 | char *string; |
Aurelien Jacobs | f702df3 | 2009-02-15 16:05:37 | [diff] [blame] | 187 | char *lang; |
| 188 | uint64_t def; |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 189 | EbmlList sub; |
| 190 | } MatroskaTag; |
| 191 | |
| 192 | typedef struct { |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 193 | char *type; |
| 194 | uint64_t typevalue; |
| 195 | uint64_t trackuid; |
| 196 | uint64_t chapteruid; |
| 197 | uint64_t attachuid; |
| 198 | } MatroskaTagTarget; |
| 199 | |
| 200 | typedef struct { |
| 201 | MatroskaTagTarget target; |
| 202 | EbmlList tag; |
| 203 | } MatroskaTags; |
| 204 | |
| 205 | typedef struct { |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 206 | uint64_t id; |
| 207 | uint64_t pos; |
| 208 | } MatroskaSeekhead; |
| 209 | |
Aurelien Jacobs | c171af9 | 2008-08-05 00:41:19 | [diff] [blame] | 210 | typedef struct { |
Aurelien Jacobs | 8d75b5a | 2007-06-04 22:35:16 | [diff] [blame] | 211 | uint64_t start; |
| 212 | uint64_t length; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 213 | } MatroskaLevel; |
| 214 | |
Aurelien Jacobs | c171af9 | 2008-08-05 00:41:19 | [diff] [blame] | 215 | typedef struct { |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 216 | uint64_t timecode; |
| 217 | EbmlList blocks; |
| 218 | } MatroskaCluster; |
| 219 | |
| 220 | typedef struct { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 221 | AVFormatContext *ctx; |
| 222 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 223 | /* EBML stuff */ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 224 | int num_levels; |
| 225 | MatroskaLevel levels[EBML_MAX_DEPTH]; |
| 226 | int level_up; |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 227 | uint32_t current_id; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 228 | |
Aurelien Jacobs | 2970858 | 2008-08-05 00:40:27 | [diff] [blame] | 229 | uint64_t time_scale; |
| 230 | double duration; |
| 231 | char *title; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 232 | EbmlList tracks; |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 233 | EbmlList attachments; |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 234 | EbmlList chapters; |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 235 | EbmlList index; |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 236 | EbmlList tags; |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 237 | EbmlList seekhead; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 238 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 239 | /* byte position of the segment inside the stream */ |
Diego Biurrun | bc5c918 | 2008-10-03 10:16:29 | [diff] [blame] | 240 | int64_t segment_start; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 241 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 242 | /* the packet queue */ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 243 | AVPacket **packets; |
| 244 | int num_packets; |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 245 | AVPacket *prev_pkt; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 246 | |
Aurelien Jacobs | 8d75b5a | 2007-06-04 22:35:16 | [diff] [blame] | 247 | int done; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 248 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 249 | /* What to skip before effectively reading a packet. */ |
| 250 | int skip_to_keyframe; |
Aurelien Jacobs | 20f7466 | 2008-09-09 12:01:51 | [diff] [blame] | 251 | uint64_t skip_to_timecode; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 252 | |
| 253 | /* File has a CUES element, but we defer parsing until it is needed. */ |
| 254 | int cues_parsing_deferred; |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 255 | |
| 256 | int current_cluster_num_blocks; |
| 257 | int64_t current_cluster_pos; |
| 258 | MatroskaCluster current_cluster; |
| 259 | |
| 260 | /* File has SSA subtitles which prevent incremental cluster parsing. */ |
| 261 | int contains_ssa; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 262 | } MatroskaDemuxContext; |
| 263 | |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 264 | typedef struct { |
| 265 | uint64_t duration; |
| 266 | int64_t reference; |
Aurelien Jacobs | 194d4b4 | 2009-08-10 18:06:14 | [diff] [blame] | 267 | uint64_t non_simple; |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 268 | EbmlBin bin; |
| 269 | } MatroskaBlock; |
| 270 | |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 271 | static EbmlSyntax ebml_header[] = { |
| 272 | { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, offsetof(Ebml,version), {.u=EBML_VERSION} }, |
| 273 | { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, offsetof(Ebml,max_size), {.u=8} }, |
| 274 | { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, offsetof(Ebml,id_length), {.u=4} }, |
| 275 | { EBML_ID_DOCTYPE, EBML_STR, 0, offsetof(Ebml,doctype), {.s="(none)"} }, |
| 276 | { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, offsetof(Ebml,doctype_version), {.u=1} }, |
| 277 | { EBML_ID_EBMLVERSION, EBML_NONE }, |
| 278 | { EBML_ID_DOCTYPEVERSION, EBML_NONE }, |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 279 | { 0 } |
| 280 | }; |
| 281 | |
| 282 | static EbmlSyntax ebml_syntax[] = { |
| 283 | { EBML_ID_HEADER, EBML_NEST, 0, 0, {.n=ebml_header} }, |
| 284 | { 0 } |
| 285 | }; |
| 286 | |
Aurelien Jacobs | 2970858 | 2008-08-05 00:40:27 | [diff] [blame] | 287 | static EbmlSyntax matroska_info[] = { |
| 288 | { MATROSKA_ID_TIMECODESCALE, EBML_UINT, 0, offsetof(MatroskaDemuxContext,time_scale), {.u=1000000} }, |
| 289 | { MATROSKA_ID_DURATION, EBML_FLOAT, 0, offsetof(MatroskaDemuxContext,duration) }, |
| 290 | { MATROSKA_ID_TITLE, EBML_UTF8, 0, offsetof(MatroskaDemuxContext,title) }, |
| 291 | { MATROSKA_ID_WRITINGAPP, EBML_NONE }, |
| 292 | { MATROSKA_ID_MUXINGAPP, EBML_NONE }, |
| 293 | { MATROSKA_ID_DATEUTC, EBML_NONE }, |
| 294 | { MATROSKA_ID_SEGMENTUID, EBML_NONE }, |
Aurelien Jacobs | 2970858 | 2008-08-05 00:40:27 | [diff] [blame] | 295 | { 0 } |
| 296 | }; |
| 297 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 298 | static EbmlSyntax matroska_track_video[] = { |
| 299 | { MATROSKA_ID_VIDEOFRAMERATE, EBML_FLOAT,0, offsetof(MatroskaTrackVideo,frame_rate) }, |
| 300 | { MATROSKA_ID_VIDEODISPLAYWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_width) }, |
| 301 | { MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_height) }, |
| 302 | { MATROSKA_ID_VIDEOPIXELWIDTH, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_width) }, |
| 303 | { MATROSKA_ID_VIDEOPIXELHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_height) }, |
| 304 | { MATROSKA_ID_VIDEOCOLORSPACE, EBML_UINT, 0, offsetof(MatroskaTrackVideo,fourcc) }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 305 | { MATROSKA_ID_VIDEOPIXELCROPB, EBML_NONE }, |
| 306 | { MATROSKA_ID_VIDEOPIXELCROPT, EBML_NONE }, |
| 307 | { MATROSKA_ID_VIDEOPIXELCROPL, EBML_NONE }, |
| 308 | { MATROSKA_ID_VIDEOPIXELCROPR, EBML_NONE }, |
| 309 | { MATROSKA_ID_VIDEODISPLAYUNIT, EBML_NONE }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 310 | { MATROSKA_ID_VIDEOFLAGINTERLACED,EBML_NONE }, |
| 311 | { MATROSKA_ID_VIDEOSTEREOMODE, EBML_NONE }, |
| 312 | { MATROSKA_ID_VIDEOASPECTRATIO, EBML_NONE }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 313 | { 0 } |
| 314 | }; |
| 315 | |
| 316 | static EbmlSyntax matroska_track_audio[] = { |
| 317 | { MATROSKA_ID_AUDIOSAMPLINGFREQ, EBML_FLOAT,0, offsetof(MatroskaTrackAudio,samplerate), {.f=8000.0} }, |
| 318 | { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ,EBML_FLOAT,0,offsetof(MatroskaTrackAudio,out_samplerate) }, |
| 319 | { MATROSKA_ID_AUDIOBITDEPTH, EBML_UINT, 0, offsetof(MatroskaTrackAudio,bitdepth) }, |
| 320 | { MATROSKA_ID_AUDIOCHANNELS, EBML_UINT, 0, offsetof(MatroskaTrackAudio,channels), {.u=1} }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 321 | { 0 } |
| 322 | }; |
| 323 | |
| 324 | static EbmlSyntax matroska_track_encoding_compression[] = { |
| 325 | { MATROSKA_ID_ENCODINGCOMPALGO, EBML_UINT, 0, offsetof(MatroskaTrackCompression,algo), {.u=0} }, |
| 326 | { MATROSKA_ID_ENCODINGCOMPSETTINGS,EBML_BIN, 0, offsetof(MatroskaTrackCompression,settings) }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 327 | { 0 } |
| 328 | }; |
| 329 | |
| 330 | static EbmlSyntax matroska_track_encoding[] = { |
| 331 | { MATROSKA_ID_ENCODINGSCOPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,scope), {.u=1} }, |
| 332 | { MATROSKA_ID_ENCODINGTYPE, EBML_UINT, 0, offsetof(MatroskaTrackEncoding,type), {.u=0} }, |
| 333 | { 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] | 334 | { MATROSKA_ID_ENCODINGORDER, EBML_NONE }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 335 | { 0 } |
| 336 | }; |
| 337 | |
| 338 | static EbmlSyntax matroska_track_encodings[] = { |
| 339 | { 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] | 340 | { 0 } |
| 341 | }; |
| 342 | |
| 343 | static EbmlSyntax matroska_track[] = { |
| 344 | { MATROSKA_ID_TRACKNUMBER, EBML_UINT, 0, offsetof(MatroskaTrack,num) }, |
Aurelien Jacobs | 38766e0 | 2009-02-15 15:29:09 | [diff] [blame] | 345 | { MATROSKA_ID_TRACKNAME, EBML_UTF8, 0, offsetof(MatroskaTrack,name) }, |
Aurelien Jacobs | 325ace3 | 2009-02-15 15:34:22 | [diff] [blame] | 346 | { MATROSKA_ID_TRACKUID, EBML_UINT, 0, offsetof(MatroskaTrack,uid) }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 347 | { MATROSKA_ID_TRACKTYPE, EBML_UINT, 0, offsetof(MatroskaTrack,type) }, |
| 348 | { MATROSKA_ID_CODECID, EBML_STR, 0, offsetof(MatroskaTrack,codec_id) }, |
| 349 | { MATROSKA_ID_CODECPRIVATE, EBML_BIN, 0, offsetof(MatroskaTrack,codec_priv) }, |
| 350 | { MATROSKA_ID_TRACKLANGUAGE, EBML_UTF8, 0, offsetof(MatroskaTrack,language), {.s="eng"} }, |
| 351 | { MATROSKA_ID_TRACKDEFAULTDURATION, EBML_UINT, 0, offsetof(MatroskaTrack,default_duration) }, |
| 352 | { MATROSKA_ID_TRACKTIMECODESCALE, EBML_FLOAT,0, offsetof(MatroskaTrack,time_scale), {.f=1.0} }, |
| 353 | { MATROSKA_ID_TRACKFLAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTrack,flag_default), {.u=1} }, |
Aurelien Jacobs | 7a617a8 | 2010-07-02 16:38:44 | [diff] [blame] | 354 | { MATROSKA_ID_TRACKFLAGFORCED, EBML_UINT, 0, offsetof(MatroskaTrack,flag_forced), {.u=0} }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 355 | { MATROSKA_ID_TRACKVIDEO, EBML_NEST, 0, offsetof(MatroskaTrack,video), {.n=matroska_track_video} }, |
| 356 | { MATROSKA_ID_TRACKAUDIO, EBML_NEST, 0, offsetof(MatroskaTrack,audio), {.n=matroska_track_audio} }, |
| 357 | { MATROSKA_ID_TRACKCONTENTENCODINGS,EBML_NEST, 0, 0, {.n=matroska_track_encodings} }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 358 | { MATROSKA_ID_TRACKFLAGENABLED, EBML_NONE }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 359 | { MATROSKA_ID_TRACKFLAGLACING, EBML_NONE }, |
| 360 | { MATROSKA_ID_CODECNAME, EBML_NONE }, |
| 361 | { MATROSKA_ID_CODECDECODEALL, EBML_NONE }, |
| 362 | { MATROSKA_ID_CODECINFOURL, EBML_NONE }, |
| 363 | { MATROSKA_ID_CODECDOWNLOADURL, EBML_NONE }, |
| 364 | { MATROSKA_ID_TRACKMINCACHE, EBML_NONE }, |
| 365 | { MATROSKA_ID_TRACKMAXCACHE, EBML_NONE }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 366 | { MATROSKA_ID_TRACKMAXBLKADDID, EBML_NONE }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 367 | { 0 } |
| 368 | }; |
| 369 | |
| 370 | static EbmlSyntax matroska_tracks[] = { |
| 371 | { MATROSKA_ID_TRACKENTRY, EBML_NEST, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext,tracks), {.n=matroska_track} }, |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 372 | { 0 } |
| 373 | }; |
| 374 | |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 375 | static EbmlSyntax matroska_attachment[] = { |
Aurelien Jacobs | 325ace3 | 2009-02-15 15:34:22 | [diff] [blame] | 376 | { MATROSKA_ID_FILEUID, EBML_UINT, 0, offsetof(MatroskaAttachement,uid) }, |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 377 | { MATROSKA_ID_FILENAME, EBML_UTF8, 0, offsetof(MatroskaAttachement,filename) }, |
| 378 | { MATROSKA_ID_FILEMIMETYPE, EBML_STR, 0, offsetof(MatroskaAttachement,mime) }, |
| 379 | { MATROSKA_ID_FILEDATA, EBML_BIN, 0, offsetof(MatroskaAttachement,bin) }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 380 | { MATROSKA_ID_FILEDESC, EBML_NONE }, |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 381 | { 0 } |
| 382 | }; |
| 383 | |
| 384 | static EbmlSyntax matroska_attachments[] = { |
| 385 | { MATROSKA_ID_ATTACHEDFILE, EBML_NEST, sizeof(MatroskaAttachement), offsetof(MatroskaDemuxContext,attachments), {.n=matroska_attachment} }, |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 386 | { 0 } |
| 387 | }; |
| 388 | |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 389 | static EbmlSyntax matroska_chapter_display[] = { |
| 390 | { MATROSKA_ID_CHAPSTRING, EBML_UTF8, 0, offsetof(MatroskaChapter,title) }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 391 | { MATROSKA_ID_CHAPLANG, EBML_NONE }, |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 392 | { 0 } |
| 393 | }; |
| 394 | |
| 395 | static EbmlSyntax matroska_chapter_entry[] = { |
| 396 | { MATROSKA_ID_CHAPTERTIMESTART, EBML_UINT, 0, offsetof(MatroskaChapter,start), {.u=AV_NOPTS_VALUE} }, |
| 397 | { MATROSKA_ID_CHAPTERTIMEEND, EBML_UINT, 0, offsetof(MatroskaChapter,end), {.u=AV_NOPTS_VALUE} }, |
| 398 | { MATROSKA_ID_CHAPTERUID, EBML_UINT, 0, offsetof(MatroskaChapter,uid) }, |
| 399 | { MATROSKA_ID_CHAPTERDISPLAY, EBML_NEST, 0, 0, {.n=matroska_chapter_display} }, |
| 400 | { MATROSKA_ID_CHAPTERFLAGHIDDEN, EBML_NONE }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 401 | { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE }, |
| 402 | { MATROSKA_ID_CHAPTERPHYSEQUIV, EBML_NONE }, |
| 403 | { MATROSKA_ID_CHAPTERATOM, EBML_NONE }, |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 404 | { 0 } |
| 405 | }; |
| 406 | |
| 407 | static EbmlSyntax matroska_chapter[] = { |
| 408 | { MATROSKA_ID_CHAPTERATOM, EBML_NEST, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext,chapters), {.n=matroska_chapter_entry} }, |
| 409 | { MATROSKA_ID_EDITIONUID, EBML_NONE }, |
| 410 | { MATROSKA_ID_EDITIONFLAGHIDDEN, EBML_NONE }, |
| 411 | { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 412 | { MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE }, |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 413 | { 0 } |
| 414 | }; |
| 415 | |
| 416 | static EbmlSyntax matroska_chapters[] = { |
| 417 | { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, {.n=matroska_chapter} }, |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 418 | { 0 } |
| 419 | }; |
| 420 | |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 421 | static EbmlSyntax matroska_index_pos[] = { |
| 422 | { MATROSKA_ID_CUETRACK, EBML_UINT, 0, offsetof(MatroskaIndexPos,track) }, |
| 423 | { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, offsetof(MatroskaIndexPos,pos) }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 424 | { MATROSKA_ID_CUEBLOCKNUMBER, EBML_NONE }, |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 425 | { 0 } |
| 426 | }; |
| 427 | |
| 428 | static EbmlSyntax matroska_index_entry[] = { |
| 429 | { MATROSKA_ID_CUETIME, EBML_UINT, 0, offsetof(MatroskaIndex,time) }, |
| 430 | { 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] | 431 | { 0 } |
| 432 | }; |
| 433 | |
| 434 | static EbmlSyntax matroska_index[] = { |
| 435 | { 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] | 436 | { 0 } |
| 437 | }; |
| 438 | |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 439 | static EbmlSyntax matroska_simpletag[] = { |
| 440 | { MATROSKA_ID_TAGNAME, EBML_UTF8, 0, offsetof(MatroskaTag,name) }, |
| 441 | { MATROSKA_ID_TAGSTRING, EBML_UTF8, 0, offsetof(MatroskaTag,string) }, |
Aurelien Jacobs | f702df3 | 2009-02-15 16:05:37 | [diff] [blame] | 442 | { MATROSKA_ID_TAGLANG, EBML_STR, 0, offsetof(MatroskaTag,lang), {.s="und"} }, |
| 443 | { MATROSKA_ID_TAGDEFAULT, EBML_UINT, 0, offsetof(MatroskaTag,def) }, |
Reimar Döffinger | c31f00e | 2010-06-08 19:28:22 | [diff] [blame] | 444 | { MATROSKA_ID_TAGDEFAULT_BUG, EBML_UINT, 0, offsetof(MatroskaTag,def) }, |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 445 | { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTag,sub), {.n=matroska_simpletag} }, |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 446 | { 0 } |
| 447 | }; |
| 448 | |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 449 | static EbmlSyntax matroska_tagtargets[] = { |
| 450 | { MATROSKA_ID_TAGTARGETS_TYPE, EBML_STR, 0, offsetof(MatroskaTagTarget,type) }, |
| 451 | { MATROSKA_ID_TAGTARGETS_TYPEVALUE, EBML_UINT, 0, offsetof(MatroskaTagTarget,typevalue), {.u=50} }, |
| 452 | { MATROSKA_ID_TAGTARGETS_TRACKUID, EBML_UINT, 0, offsetof(MatroskaTagTarget,trackuid) }, |
| 453 | { MATROSKA_ID_TAGTARGETS_CHAPTERUID,EBML_UINT, 0, offsetof(MatroskaTagTarget,chapteruid) }, |
| 454 | { MATROSKA_ID_TAGTARGETS_ATTACHUID, EBML_UINT, 0, offsetof(MatroskaTagTarget,attachuid) }, |
| 455 | { 0 } |
| 456 | }; |
| 457 | |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 458 | static EbmlSyntax matroska_tag[] = { |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 459 | { MATROSKA_ID_SIMPLETAG, EBML_NEST, sizeof(MatroskaTag), offsetof(MatroskaTags,tag), {.n=matroska_simpletag} }, |
| 460 | { MATROSKA_ID_TAGTARGETS, EBML_NEST, 0, offsetof(MatroskaTags,target), {.n=matroska_tagtargets} }, |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 461 | { 0 } |
| 462 | }; |
| 463 | |
Aurelien Jacobs | 434d496 | 2008-08-05 00:40:18 | [diff] [blame] | 464 | static EbmlSyntax matroska_tags[] = { |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 465 | { MATROSKA_ID_TAG, EBML_NEST, sizeof(MatroskaTags), offsetof(MatroskaDemuxContext,tags), {.n=matroska_tag} }, |
Aurelien Jacobs | 434d496 | 2008-08-05 00:40:18 | [diff] [blame] | 466 | { 0 } |
| 467 | }; |
| 468 | |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 469 | static EbmlSyntax matroska_seekhead_entry[] = { |
| 470 | { MATROSKA_ID_SEEKID, EBML_UINT, 0, offsetof(MatroskaSeekhead,id) }, |
| 471 | { MATROSKA_ID_SEEKPOSITION, EBML_UINT, 0, offsetof(MatroskaSeekhead,pos), {.u=-1} }, |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 472 | { 0 } |
| 473 | }; |
| 474 | |
| 475 | static EbmlSyntax matroska_seekhead[] = { |
| 476 | { 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] | 477 | { 0 } |
| 478 | }; |
| 479 | |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 480 | static EbmlSyntax matroska_segment[] = { |
| 481 | { MATROSKA_ID_INFO, EBML_NEST, 0, 0, {.n=matroska_info } }, |
| 482 | { MATROSKA_ID_TRACKS, EBML_NEST, 0, 0, {.n=matroska_tracks } }, |
| 483 | { MATROSKA_ID_ATTACHMENTS, EBML_NEST, 0, 0, {.n=matroska_attachments} }, |
| 484 | { MATROSKA_ID_CHAPTERS, EBML_NEST, 0, 0, {.n=matroska_chapters } }, |
| 485 | { MATROSKA_ID_CUES, EBML_NEST, 0, 0, {.n=matroska_index } }, |
| 486 | { MATROSKA_ID_TAGS, EBML_NEST, 0, 0, {.n=matroska_tags } }, |
| 487 | { MATROSKA_ID_SEEKHEAD, EBML_NEST, 0, 0, {.n=matroska_seekhead } }, |
Aurelien Jacobs | 8070203 | 2010-06-11 16:36:51 | [diff] [blame] | 488 | { MATROSKA_ID_CLUSTER, EBML_STOP }, |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 489 | { 0 } |
| 490 | }; |
| 491 | |
| 492 | static EbmlSyntax matroska_segments[] = { |
| 493 | { MATROSKA_ID_SEGMENT, EBML_NEST, 0, 0, {.n=matroska_segment } }, |
| 494 | { 0 } |
| 495 | }; |
| 496 | |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 497 | static EbmlSyntax matroska_blockgroup[] = { |
| 498 | { MATROSKA_ID_BLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) }, |
| 499 | { MATROSKA_ID_SIMPLEBLOCK, EBML_BIN, 0, offsetof(MatroskaBlock,bin) }, |
| 500 | { MATROSKA_ID_BLOCKDURATION, EBML_UINT, 0, offsetof(MatroskaBlock,duration), {.u=AV_NOPTS_VALUE} }, |
| 501 | { MATROSKA_ID_BLOCKREFERENCE, EBML_UINT, 0, offsetof(MatroskaBlock,reference) }, |
Aurelien Jacobs | 194d4b4 | 2009-08-10 18:06:14 | [diff] [blame] | 502 | { 1, EBML_UINT, 0, offsetof(MatroskaBlock,non_simple), {.u=1} }, |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 503 | { 0 } |
| 504 | }; |
| 505 | |
| 506 | static EbmlSyntax matroska_cluster[] = { |
| 507 | { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) }, |
| 508 | { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} }, |
| 509 | { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 510 | { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE }, |
| 511 | { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE }, |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 512 | { 0 } |
| 513 | }; |
| 514 | |
| 515 | static EbmlSyntax matroska_clusters[] = { |
| 516 | { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster} }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 517 | { MATROSKA_ID_INFO, EBML_NONE }, |
| 518 | { MATROSKA_ID_CUES, EBML_NONE }, |
| 519 | { MATROSKA_ID_TAGS, EBML_NONE }, |
| 520 | { MATROSKA_ID_SEEKHEAD, EBML_NONE }, |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 521 | { 0 } |
| 522 | }; |
| 523 | |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 524 | static EbmlSyntax matroska_cluster_incremental_parsing[] = { |
| 525 | { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) }, |
| 526 | { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} }, |
| 527 | { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} }, |
| 528 | { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE }, |
| 529 | { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE }, |
| 530 | { MATROSKA_ID_INFO, EBML_NONE }, |
| 531 | { MATROSKA_ID_CUES, EBML_NONE }, |
| 532 | { MATROSKA_ID_TAGS, EBML_NONE }, |
| 533 | { MATROSKA_ID_SEEKHEAD, EBML_NONE }, |
| 534 | { MATROSKA_ID_CLUSTER, EBML_STOP }, |
| 535 | { 0 } |
| 536 | }; |
| 537 | |
| 538 | static EbmlSyntax matroska_cluster_incremental[] = { |
| 539 | { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) }, |
| 540 | { MATROSKA_ID_BLOCKGROUP, EBML_STOP }, |
| 541 | { MATROSKA_ID_SIMPLEBLOCK, EBML_STOP }, |
| 542 | { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE }, |
| 543 | { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE }, |
| 544 | { 0 } |
| 545 | }; |
| 546 | |
| 547 | static EbmlSyntax matroska_clusters_incremental[] = { |
| 548 | { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster_incremental} }, |
| 549 | { MATROSKA_ID_INFO, EBML_NONE }, |
| 550 | { MATROSKA_ID_CUES, EBML_NONE }, |
| 551 | { MATROSKA_ID_TAGS, EBML_NONE }, |
| 552 | { MATROSKA_ID_SEEKHEAD, EBML_NONE }, |
| 553 | { 0 } |
| 554 | }; |
| 555 | |
Alex Converse | b0f29db | 2012-02-20 08:42:33 | [diff] [blame] | 556 | static const char *const matroska_doctypes[] = { "matroska", "webm" }; |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 557 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 558 | /* |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 559 | * 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] | 560 | */ |
Aurelien Jacobs | 592110c | 2008-08-05 00:42:08 | [diff] [blame] | 561 | static int ebml_level_end(MatroskaDemuxContext *matroska) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 562 | { |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 563 | AVIOContext *pb = matroska->ctx->pb; |
Anton Khirnov | a2704c9 | 2011-03-03 19:11:45 | [diff] [blame] | 564 | int64_t pos = avio_tell(pb); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 565 | |
Aurelien Jacobs | 592110c | 2008-08-05 00:42:08 | [diff] [blame] | 566 | if (matroska->num_levels > 0) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 567 | MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1]; |
Aurelien Jacobs | 8dbe48f | 2010-06-11 16:43:47 | [diff] [blame] | 568 | if (pos - level->start >= level->length || matroska->current_id) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 569 | matroska->num_levels--; |
Aurelien Jacobs | 592110c | 2008-08-05 00:42:08 | [diff] [blame] | 570 | return 1; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 571 | } |
| 572 | } |
Aurelien Jacobs | 592110c | 2008-08-05 00:42:08 | [diff] [blame] | 573 | return 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Read: an "EBML number", which is defined as a variable-length |
| 578 | * array of bytes. The first byte indicates the length by giving a |
| 579 | * number of 0-bits followed by a one. The position of the first |
| 580 | * "one" bit inside the first byte indicates the length of this |
| 581 | * number. |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 582 | * Returns: number of bytes read, < 0 on error |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 583 | */ |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 584 | static int ebml_read_num(MatroskaDemuxContext *matroska, AVIOContext *pb, |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 585 | int max_size, uint64_t *number) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 586 | { |
Reimar Döffinger | ff6a5fc | 2010-09-02 19:17:46 | [diff] [blame] | 587 | int read = 1, n = 1; |
| 588 | uint64_t total = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 589 | |
Anton Khirnov | b7effd4 | 2011-02-21 15:43:01 | [diff] [blame] | 590 | /* The first byte tells us the length in bytes - avio_r8() can normally |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 591 | * return 0, but since that's not a valid first ebmlID byte, we can |
| 592 | * use it safely here to catch EOS. */ |
Anton Khirnov | b7effd4 | 2011-02-21 15:43:01 | [diff] [blame] | 593 | if (!(total = avio_r8(pb))) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 594 | /* we might encounter EOS here */ |
Anton Khirnov | 66e5b1d | 2011-03-07 20:50:25 | [diff] [blame] | 595 | if (!pb->eof_reached) { |
Anton Khirnov | a2704c9 | 2011-03-03 19:11:45 | [diff] [blame] | 596 | int64_t pos = avio_tell(pb); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 597 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 598 | "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", |
| 599 | pos, pos); |
| 600 | } |
Panagiotis Issaris | 6f3e0b2 | 2007-07-19 15:23:32 | [diff] [blame] | 601 | return AVERROR(EIO); /* EOS or actual I/O error */ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* get the length of the EBML number */ |
Reimar Döffinger | ff6a5fc | 2010-09-02 19:17:46 | [diff] [blame] | 605 | read = 8 - ff_log2_tab[total]; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 606 | if (read > max_size) { |
Anton Khirnov | a2704c9 | 2011-03-03 19:11:45 | [diff] [blame] | 607 | int64_t pos = avio_tell(pb) - 1; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 608 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 609 | "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n", |
| 610 | (uint8_t) total, pos, pos); |
| 611 | return AVERROR_INVALIDDATA; |
| 612 | } |
| 613 | |
| 614 | /* read out length */ |
Reimar Döffinger | ff6a5fc | 2010-09-02 19:17:46 | [diff] [blame] | 615 | total ^= 1 << ff_log2_tab[total]; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 616 | while (n++ < read) |
Anton Khirnov | b7effd4 | 2011-02-21 15:43:01 | [diff] [blame] | 617 | total = (total << 8) | avio_r8(pb); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 618 | |
| 619 | *number = total; |
| 620 | |
| 621 | return read; |
| 622 | } |
| 623 | |
Reimar Döffinger | 1b4d327 | 2010-09-06 17:51:44 | [diff] [blame] | 624 | /** |
| 625 | * Read a EBML length value. |
| 626 | * This needs special handling for the "unknown length" case which has multiple |
| 627 | * encodings. |
| 628 | */ |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 629 | static int ebml_read_length(MatroskaDemuxContext *matroska, AVIOContext *pb, |
Reimar Döffinger | 1b4d327 | 2010-09-06 17:51:44 | [diff] [blame] | 630 | uint64_t *number) |
| 631 | { |
| 632 | int res = ebml_read_num(matroska, pb, 8, number); |
| 633 | if (res > 0 && *number + 1 == 1ULL << (7 * res)) |
| 634 | *number = 0xffffffffffffffULL; |
| 635 | return res; |
| 636 | } |
| 637 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 638 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 639 | * Read the next element as an unsigned int. |
| 640 | * 0 is success, < 0 is failure. |
| 641 | */ |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 642 | static int ebml_read_uint(AVIOContext *pb, int size, uint64_t *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 643 | { |
Aurelien Jacobs | c6cd2b3 | 2008-08-05 00:41:55 | [diff] [blame] | 644 | int n = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 645 | |
Aurelien Jacobs | 4a194c8 | 2010-09-05 21:37:40 | [diff] [blame] | 646 | if (size > 8) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 647 | return AVERROR_INVALIDDATA; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 648 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 649 | /* big-endian ordering; build up number */ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 650 | *num = 0; |
| 651 | while (n++ < size) |
Anton Khirnov | b7effd4 | 2011-02-21 15:43:01 | [diff] [blame] | 652 | *num = (*num << 8) | avio_r8(pb); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 658 | * Read the next element as a float. |
| 659 | * 0 is success, < 0 is failure. |
| 660 | */ |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 661 | static int ebml_read_float(AVIOContext *pb, int size, double *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 662 | { |
Aurelien Jacobs | 4a194c8 | 2010-09-05 21:37:40 | [diff] [blame] | 663 | if (size == 0) { |
| 664 | *num = 0; |
| 665 | } else if (size == 4) { |
Mans Rullgard | 3383a53 | 2011-11-27 14:04:16 | [diff] [blame] | 666 | *num = av_int2float(avio_rb32(pb)); |
| 667 | } else if (size == 8){ |
| 668 | *num = av_int2double(avio_rb64(pb)); |
Aurelien Jacobs | ba5a1f9 | 2008-08-05 00:41:52 | [diff] [blame] | 669 | } else |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 670 | return AVERROR_INVALIDDATA; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * Read the next element as an ASCII string. |
| 677 | * 0 is success, < 0 is failure. |
| 678 | */ |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 679 | static int ebml_read_ascii(AVIOContext *pb, int size, char **str) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 680 | { |
Ronald S. Bultje | cd40c31 | 2012-02-25 00:12:18 | [diff] [blame] | 681 | char *res; |
| 682 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 683 | /* EBML strings are usually not 0-terminated, so we allocate one |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 684 | * byte more, read the string and NULL-terminate it ourselves. */ |
Ronald S. Bultje | cd40c31 | 2012-02-25 00:12:18 | [diff] [blame] | 685 | if (!(res = av_malloc(size + 1))) |
Panagiotis Issaris | 769e10f | 2007-07-19 15:21:30 | [diff] [blame] | 686 | return AVERROR(ENOMEM); |
Ronald S. Bultje | cd40c31 | 2012-02-25 00:12:18 | [diff] [blame] | 687 | if (avio_read(pb, (uint8_t *) res, size) != size) { |
| 688 | av_free(res); |
Panagiotis Issaris | 6f3e0b2 | 2007-07-19 15:23:32 | [diff] [blame] | 689 | return AVERROR(EIO); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 690 | } |
Ronald S. Bultje | cd40c31 | 2012-02-25 00:12:18 | [diff] [blame] | 691 | (res)[size] = '\0'; |
| 692 | av_free(*str); |
| 693 | *str = res; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | /* |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 699 | * Read the next element as binary data. |
| 700 | * 0 is success, < 0 is failure. |
| 701 | */ |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 702 | static int ebml_read_binary(AVIOContext *pb, int length, EbmlBin *bin) |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 703 | { |
| 704 | av_free(bin->data); |
| 705 | if (!(bin->data = av_malloc(length))) |
| 706 | return AVERROR(ENOMEM); |
| 707 | |
| 708 | bin->size = length; |
Anton Khirnov | a2704c9 | 2011-03-03 19:11:45 | [diff] [blame] | 709 | bin->pos = avio_tell(pb); |
Anton Khirnov | b7effd4 | 2011-02-21 15:43:01 | [diff] [blame] | 710 | if (avio_read(pb, bin->data, length) != length) { |
David Conrad | 5549aa6 | 2010-05-18 21:21:37 | [diff] [blame] | 711 | av_freep(&bin->data); |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 712 | return AVERROR(EIO); |
David Conrad | 5549aa6 | 2010-05-18 21:21:37 | [diff] [blame] | 713 | } |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 719 | * Read the next element, but only the header. The contents |
| 720 | * are supposed to be sub-elements which can be read separately. |
| 721 | * 0 is success, < 0 is failure. |
| 722 | */ |
Aurelien Jacobs | bddd1d9 | 2010-06-11 17:16:43 | [diff] [blame] | 723 | static int ebml_read_master(MatroskaDemuxContext *matroska, uint64_t length) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 724 | { |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 725 | AVIOContext *pb = matroska->ctx->pb; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 726 | MatroskaLevel *level; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 727 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 728 | if (matroska->num_levels >= EBML_MAX_DEPTH) { |
| 729 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 730 | "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH); |
Panagiotis Issaris | 85565db | 2007-07-19 15:38:33 | [diff] [blame] | 731 | return AVERROR(ENOSYS); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 732 | } |
| 733 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 734 | level = &matroska->levels[matroska->num_levels++]; |
Anton Khirnov | a2704c9 | 2011-03-03 19:11:45 | [diff] [blame] | 735 | level->start = avio_tell(pb); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 736 | level->length = length; |
| 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 742 | * Read signed/unsigned "EBML" numbers. |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 743 | * Return: number of bytes processed, < 0 on error |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 744 | */ |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 745 | static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska, |
| 746 | uint8_t *data, uint32_t size, uint64_t *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 747 | { |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 748 | AVIOContext pb; |
Anton Khirnov | e731b8d | 2011-02-20 10:04:13 | [diff] [blame] | 749 | ffio_init_context(&pb, data, size, 0, NULL, NULL, NULL, NULL); |
David Conrad | 465c28b | 2010-05-18 21:21:32 | [diff] [blame] | 750 | return ebml_read_num(matroska, &pb, FFMIN(size, 8), num); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /* |
| 754 | * Same as above, but signed. |
| 755 | */ |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 756 | static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska, |
| 757 | uint8_t *data, uint32_t size, int64_t *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 758 | { |
| 759 | uint64_t unum; |
| 760 | int res; |
| 761 | |
| 762 | /* read as unsigned number first */ |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 763 | if ((res = matroska_ebmlnum_uint(matroska, data, size, &unum)) < 0) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 764 | return res; |
| 765 | |
| 766 | /* make signed (weird way) */ |
Aurelien Jacobs | 33ac07e | 2008-08-05 00:42:55 | [diff] [blame] | 767 | *num = unum - ((1LL << (7*res - 1)) - 1); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 768 | |
| 769 | return res; |
| 770 | } |
| 771 | |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 772 | static int ebml_parse_elem(MatroskaDemuxContext *matroska, |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 773 | EbmlSyntax *syntax, void *data); |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 774 | |
| 775 | static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, |
| 776 | uint32_t id, void *data) |
| 777 | { |
| 778 | int i; |
| 779 | for (i=0; syntax[i].id; i++) |
| 780 | if (id == syntax[i].id) |
| 781 | break; |
Aurelien Jacobs | 8dbe48f | 2010-06-11 16:43:47 | [diff] [blame] | 782 | if (!syntax[i].id && id == MATROSKA_ID_CLUSTER && |
| 783 | matroska->num_levels > 0 && |
Aurelien Jacobs | bddd1d9 | 2010-06-11 17:16:43 | [diff] [blame] | 784 | matroska->levels[matroska->num_levels-1].length == 0xffffffffffffff) |
Aurelien Jacobs | 8dbe48f | 2010-06-11 16:43:47 | [diff] [blame] | 785 | return 0; // we reached the end of an unknown size cluster |
Aurelien Jacobs | b49d17b | 2008-08-20 00:44:25 | [diff] [blame] | 786 | if (!syntax[i].id && id != EBML_ID_VOID && id != EBML_ID_CRC32) |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 787 | av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id); |
| 788 | return ebml_parse_elem(matroska, &syntax[i], data); |
| 789 | } |
| 790 | |
Aurelien Jacobs | 66a37e0 | 2008-08-05 00:42:17 | [diff] [blame] | 791 | static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, |
| 792 | void *data) |
| 793 | { |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 794 | if (!matroska->current_id) { |
Aurelien Jacobs | 7391781 | 2010-06-11 16:45:38 | [diff] [blame] | 795 | uint64_t id; |
| 796 | int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id); |
| 797 | if (res < 0) |
| 798 | return res; |
| 799 | matroska->current_id = id | 1 << 7*res; |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 800 | } |
| 801 | return ebml_parse_id(matroska, syntax, matroska->current_id, data); |
Aurelien Jacobs | 66a37e0 | 2008-08-05 00:42:17 | [diff] [blame] | 802 | } |
| 803 | |
Aurelien Jacobs | 9bcb92c | 2008-08-05 00:42:13 | [diff] [blame] | 804 | static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, |
Aurelien Jacobs | 6314cca | 2008-08-05 00:42:23 | [diff] [blame] | 805 | void *data) |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 806 | { |
Aurelien Jacobs | c005b3f | 2008-08-05 00:42:10 | [diff] [blame] | 807 | int i, res = 0; |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 808 | |
| 809 | for (i=0; syntax[i].id; i++) |
| 810 | switch (syntax[i].type) { |
| 811 | case EBML_UINT: |
| 812 | *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u; |
| 813 | break; |
| 814 | case EBML_FLOAT: |
| 815 | *(double *)((char *)data+syntax[i].data_offset) = syntax[i].def.f; |
| 816 | break; |
| 817 | case EBML_STR: |
| 818 | case EBML_UTF8: |
| 819 | *(char **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s); |
| 820 | break; |
| 821 | } |
| 822 | |
Aurelien Jacobs | 6314cca | 2008-08-05 00:42:23 | [diff] [blame] | 823 | while (!res && !ebml_level_end(matroska)) |
Aurelien Jacobs | 66a37e0 | 2008-08-05 00:42:17 | [diff] [blame] | 824 | res = ebml_parse(matroska, syntax, data); |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 825 | |
| 826 | return res; |
| 827 | } |
| 828 | |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 829 | static int ebml_parse_elem(MatroskaDemuxContext *matroska, |
| 830 | EbmlSyntax *syntax, void *data) |
| 831 | { |
Reimar Döffinger | 95ec3d4 | 2011-02-06 10:32:03 | [diff] [blame] | 832 | static const uint64_t max_lengths[EBML_TYPE_COUNT] = { |
| 833 | [EBML_UINT] = 8, |
| 834 | [EBML_FLOAT] = 8, |
| 835 | // max. 16 MB for strings |
| 836 | [EBML_STR] = 0x1000000, |
| 837 | [EBML_UTF8] = 0x1000000, |
| 838 | // max. 256 MB for binary data |
| 839 | [EBML_BIN] = 0x10000000, |
| 840 | // no limits for anything else |
| 841 | }; |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 842 | AVIOContext *pb = matroska->ctx->pb; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 843 | uint32_t id = syntax->id; |
| 844 | uint64_t length; |
| 845 | int res; |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 846 | void *newelem; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 847 | |
| 848 | data = (char *)data + syntax->data_offset; |
| 849 | if (syntax->list_elem_size) { |
| 850 | EbmlList *list = data; |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 851 | newelem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size); |
| 852 | if (!newelem) |
| 853 | return AVERROR(ENOMEM); |
| 854 | list->elem = newelem; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 855 | data = (char*)list->elem + list->nb_elem*syntax->list_elem_size; |
| 856 | memset(data, 0, syntax->list_elem_size); |
| 857 | list->nb_elem++; |
| 858 | } |
| 859 | |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 860 | if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) { |
| 861 | matroska->current_id = 0; |
Reimar Döffinger | 1b4d327 | 2010-09-06 17:51:44 | [diff] [blame] | 862 | if ((res = ebml_read_length(matroska, pb, &length)) < 0) |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 863 | return res; |
Reimar Döffinger | 95ec3d4 | 2011-02-06 10:32:03 | [diff] [blame] | 864 | if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) { |
| 865 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 866 | "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for syntax element %i\n", |
| 867 | length, max_lengths[syntax->type], syntax->type); |
| 868 | return AVERROR_INVALIDDATA; |
| 869 | } |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 870 | } |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 871 | |
| 872 | switch (syntax->type) { |
| 873 | case EBML_UINT: res = ebml_read_uint (pb, length, data); break; |
| 874 | case EBML_FLOAT: res = ebml_read_float (pb, length, data); break; |
| 875 | case EBML_STR: |
| 876 | case EBML_UTF8: res = ebml_read_ascii (pb, length, data); break; |
| 877 | case EBML_BIN: res = ebml_read_binary(pb, length, data); break; |
| 878 | case EBML_NEST: if ((res=ebml_read_master(matroska, length)) < 0) |
| 879 | return res; |
| 880 | if (id == MATROSKA_ID_SEGMENT) |
Anton Khirnov | a2704c9 | 2011-03-03 19:11:45 | [diff] [blame] | 881 | matroska->segment_start = avio_tell(matroska->ctx->pb); |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 882 | return ebml_parse_nest(matroska, syntax->def.n, data); |
| 883 | case EBML_PASS: return ebml_parse_id(matroska, syntax->def.n, id, data); |
Aurelien Jacobs | 8070203 | 2010-06-11 16:36:51 | [diff] [blame] | 884 | case EBML_STOP: return 1; |
Anton Khirnov | 45a8a02 | 2011-03-15 08:14:38 | [diff] [blame] | 885 | default: return avio_skip(pb,length)<0 ? AVERROR(EIO) : 0; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 886 | } |
| 887 | if (res == AVERROR_INVALIDDATA) |
| 888 | av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n"); |
| 889 | else if (res == AVERROR(EIO)) |
| 890 | av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n"); |
| 891 | return res; |
| 892 | } |
| 893 | |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 894 | static void ebml_free(EbmlSyntax *syntax, void *data) |
| 895 | { |
| 896 | int i, j; |
| 897 | for (i=0; syntax[i].id; i++) { |
| 898 | void *data_off = (char *)data + syntax[i].data_offset; |
| 899 | switch (syntax[i].type) { |
| 900 | case EBML_STR: |
| 901 | case EBML_UTF8: av_freep(data_off); break; |
| 902 | case EBML_BIN: av_freep(&((EbmlBin *)data_off)->data); break; |
| 903 | case EBML_NEST: |
| 904 | if (syntax[i].list_elem_size) { |
| 905 | EbmlList *list = data_off; |
| 906 | char *ptr = list->elem; |
| 907 | for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size) |
| 908 | ebml_free(syntax[i].def.n, ptr); |
| 909 | av_free(list->elem); |
| 910 | } else |
| 911 | ebml_free(syntax[i].def.n, data_off); |
| 912 | default: break; |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 917 | |
| 918 | /* |
| 919 | * Autodetecting... |
| 920 | */ |
| 921 | static int matroska_probe(AVProbeData *p) |
| 922 | { |
| 923 | uint64_t total = 0; |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 924 | int len_mask = 0x80, size = 1, n = 1, i; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 925 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 926 | /* EBML header? */ |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 927 | if (AV_RB32(p->buf) != EBML_ID_HEADER) |
| 928 | return 0; |
| 929 | |
| 930 | /* length of header */ |
| 931 | total = p->buf[4]; |
| 932 | while (size <= 8 && !(total & len_mask)) { |
| 933 | size++; |
| 934 | len_mask >>= 1; |
| 935 | } |
| 936 | if (size > 8) |
| 937 | return 0; |
| 938 | total &= (len_mask - 1); |
| 939 | while (n < size) |
| 940 | total = (total << 8) | p->buf[4 + n++]; |
| 941 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 942 | /* Does the probe data contain the whole header? */ |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 943 | if (p->buf_size < 4 + size + total) |
| 944 | return 0; |
| 945 | |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 946 | /* The header should contain a known document type. For now, |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 947 | * we don't parse the whole header but simply check for the |
| 948 | * availability of that array of characters inside the header. |
| 949 | * Not fully fool-proof, but good enough. */ |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 950 | for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) { |
| 951 | int probelen = strlen(matroska_doctypes[i]); |
Chris Evans | 69619a1 | 2011-07-20 00:51:48 | [diff] [blame] | 952 | if (total < probelen) |
| 953 | continue; |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 954 | for (n = 4+size; n <= 4+size+total-probelen; n++) |
| 955 | if (!memcmp(p->buf+n, matroska_doctypes[i], probelen)) |
| 956 | return AVPROBE_SCORE_MAX; |
| 957 | } |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 958 | |
David Conrad | c7b913c | 2010-05-22 01:41:35 | [diff] [blame] | 959 | // probably valid EBML header but no recognized doctype |
| 960 | return AVPROBE_SCORE_MAX/2; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska, |
| 964 | int num) |
| 965 | { |
| 966 | MatroskaTrack *tracks = matroska->tracks.elem; |
| 967 | int i; |
| 968 | |
| 969 | for (i=0; i < matroska->tracks.nb_elem; i++) |
| 970 | if (tracks[i].num == num) |
| 971 | return &tracks[i]; |
| 972 | |
| 973 | av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num); |
| 974 | return NULL; |
| 975 | } |
| 976 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 977 | static int matroska_decode_buffer(uint8_t** buf, int* buf_size, |
| 978 | MatroskaTrack *track) |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 979 | { |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 980 | MatroskaTrackEncoding *encodings = track->encodings.elem; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 981 | uint8_t* data = *buf; |
| 982 | int isize = *buf_size; |
| 983 | uint8_t* pkt_data = NULL; |
Diego Biurrun | 529506b | 2012-02-12 09:58:46 | [diff] [blame] | 984 | uint8_t av_unused *newpktdata; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 985 | int pkt_size = isize; |
| 986 | int result = 0; |
| 987 | int olen; |
| 988 | |
Aurelien Jacobs | 4f90688 | 2010-08-17 14:05:23 | [diff] [blame] | 989 | if (pkt_size >= 10000000) |
| 990 | return -1; |
| 991 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 992 | switch (encodings[0].compression.algo) { |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 993 | case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP: |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 994 | return encodings[0].compression.settings.size; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 995 | case MATROSKA_TRACK_ENCODING_COMP_LZO: |
| 996 | do { |
| 997 | olen = pkt_size *= 3; |
Aurelien Jacobs | 69b6d53 | 2009-02-02 21:32:11 | [diff] [blame] | 998 | pkt_data = av_realloc(pkt_data, pkt_size+AV_LZO_OUTPUT_PADDING); |
Reimar Döffinger | 0b178e5 | 2009-02-02 20:16:00 | [diff] [blame] | 999 | result = av_lzo1x_decode(pkt_data, &olen, data, &isize); |
| 1000 | } while (result==AV_LZO_OUTPUT_FULL && pkt_size<10000000); |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1001 | if (result) |
| 1002 | goto failed; |
| 1003 | pkt_size -= olen; |
| 1004 | break; |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame] | 1005 | #if CONFIG_ZLIB |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1006 | case MATROSKA_TRACK_ENCODING_COMP_ZLIB: { |
| 1007 | z_stream zstream = {0}; |
| 1008 | if (inflateInit(&zstream) != Z_OK) |
| 1009 | return -1; |
| 1010 | zstream.next_in = data; |
| 1011 | zstream.avail_in = isize; |
| 1012 | do { |
| 1013 | pkt_size *= 3; |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1014 | newpktdata = av_realloc(pkt_data, pkt_size); |
| 1015 | if (!newpktdata) { |
| 1016 | inflateEnd(&zstream); |
| 1017 | goto failed; |
| 1018 | } |
| 1019 | pkt_data = newpktdata; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1020 | zstream.avail_out = pkt_size - zstream.total_out; |
| 1021 | zstream.next_out = pkt_data + zstream.total_out; |
| 1022 | result = inflate(&zstream, Z_NO_FLUSH); |
| 1023 | } while (result==Z_OK && pkt_size<10000000); |
| 1024 | pkt_size = zstream.total_out; |
| 1025 | inflateEnd(&zstream); |
| 1026 | if (result != Z_STREAM_END) |
| 1027 | goto failed; |
| 1028 | break; |
| 1029 | } |
| 1030 | #endif |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame] | 1031 | #if CONFIG_BZLIB |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1032 | case MATROSKA_TRACK_ENCODING_COMP_BZLIB: { |
| 1033 | bz_stream bzstream = {0}; |
| 1034 | if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK) |
| 1035 | return -1; |
| 1036 | bzstream.next_in = data; |
| 1037 | bzstream.avail_in = isize; |
| 1038 | do { |
| 1039 | pkt_size *= 3; |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1040 | newpktdata = av_realloc(pkt_data, pkt_size); |
| 1041 | if (!newpktdata) { |
| 1042 | BZ2_bzDecompressEnd(&bzstream); |
| 1043 | goto failed; |
| 1044 | } |
| 1045 | pkt_data = newpktdata; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1046 | bzstream.avail_out = pkt_size - bzstream.total_out_lo32; |
| 1047 | bzstream.next_out = pkt_data + bzstream.total_out_lo32; |
| 1048 | result = BZ2_bzDecompress(&bzstream); |
| 1049 | } while (result==BZ_OK && pkt_size<10000000); |
| 1050 | pkt_size = bzstream.total_out_lo32; |
| 1051 | BZ2_bzDecompressEnd(&bzstream); |
| 1052 | if (result != BZ_STREAM_END) |
| 1053 | goto failed; |
| 1054 | break; |
| 1055 | } |
| 1056 | #endif |
Aurelien Jacobs | 28f27e0 | 2008-08-20 23:08:07 | [diff] [blame] | 1057 | default: |
| 1058 | return -1; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | *buf = pkt_data; |
| 1062 | *buf_size = pkt_size; |
| 1063 | return 0; |
| 1064 | failed: |
| 1065 | av_free(pkt_data); |
| 1066 | return -1; |
| 1067 | } |
| 1068 | |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 1069 | static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska, |
Aurelien Jacobs | e7d4b74 | 2008-09-28 22:55:28 | [diff] [blame] | 1070 | AVPacket *pkt, uint64_t display_duration) |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 1071 | { |
| 1072 | char *line, *layer, *ptr = pkt->data, *end = ptr+pkt->size; |
| 1073 | for (; *ptr!=',' && ptr<end-1; ptr++); |
| 1074 | if (*ptr == ',') |
| 1075 | layer = ++ptr; |
| 1076 | for (; *ptr!=',' && ptr<end-1; ptr++); |
| 1077 | if (*ptr == ',') { |
Aurelien Jacobs | e7d4b74 | 2008-09-28 22:55:28 | [diff] [blame] | 1078 | int64_t end_pts = pkt->pts + display_duration; |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 1079 | int sc = matroska->time_scale * pkt->pts / 10000000; |
| 1080 | int ec = matroska->time_scale * end_pts / 10000000; |
| 1081 | int sh, sm, ss, eh, em, es, len; |
| 1082 | sh = sc/360000; sc -= 360000*sh; |
| 1083 | sm = sc/ 6000; sc -= 6000*sm; |
| 1084 | ss = sc/ 100; sc -= 100*ss; |
| 1085 | eh = ec/360000; ec -= 360000*eh; |
| 1086 | em = ec/ 6000; ec -= 6000*em; |
| 1087 | es = ec/ 100; ec -= 100*es; |
| 1088 | *ptr++ = '\0'; |
| 1089 | len = 50 + end-ptr + FF_INPUT_BUFFER_PADDING_SIZE; |
| 1090 | if (!(line = av_malloc(len))) |
| 1091 | return; |
Aurelien Jacobs | 3df2be9 | 2008-09-28 23:01:07 | [diff] [blame] | 1092 | snprintf(line,len,"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n", |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 1093 | layer, sh, sm, ss, sc, eh, em, es, ec, ptr); |
| 1094 | av_free(pkt->data); |
| 1095 | pkt->data = line; |
| 1096 | pkt->size = strlen(line); |
| 1097 | } |
| 1098 | } |
| 1099 | |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1100 | static int matroska_merge_packets(AVPacket *out, AVPacket *in) |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 1101 | { |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1102 | void *newdata = av_realloc(out->data, out->size+in->size); |
| 1103 | if (!newdata) |
| 1104 | return AVERROR(ENOMEM); |
| 1105 | out->data = newdata; |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 1106 | memcpy(out->data+out->size, in->data, in->size); |
| 1107 | out->size += in->size; |
| 1108 | av_destruct_packet(in); |
| 1109 | av_free(in); |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1110 | return 0; |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 1111 | } |
| 1112 | |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1113 | static void matroska_convert_tag(AVFormatContext *s, EbmlList *list, |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1114 | AVDictionary **metadata, char *prefix) |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 1115 | { |
| 1116 | MatroskaTag *tags = list->elem; |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1117 | char key[1024]; |
| 1118 | int i; |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 1119 | |
| 1120 | for (i=0; i < list->nb_elem; i++) { |
Aurelien Jacobs | f702df3 | 2009-02-15 16:05:37 | [diff] [blame] | 1121 | const char *lang = strcmp(tags[i].lang, "und") ? tags[i].lang : NULL; |
Anton Khirnov | bf800c7 | 2010-11-03 06:29:04 | [diff] [blame] | 1122 | |
| 1123 | if (!tags[i].name) { |
| 1124 | av_log(s, AV_LOG_WARNING, "Skipping invalid tag with no TagName.\n"); |
| 1125 | continue; |
| 1126 | } |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1127 | if (prefix) snprintf(key, sizeof(key), "%s/%s", prefix, tags[i].name); |
| 1128 | else av_strlcpy(key, tags[i].name, sizeof(key)); |
Aurelien Jacobs | f702df3 | 2009-02-15 16:05:37 | [diff] [blame] | 1129 | if (tags[i].def || !lang) { |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1130 | av_dict_set(metadata, key, tags[i].string, 0); |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 1131 | if (tags[i].sub.nb_elem) |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1132 | matroska_convert_tag(s, &tags[i].sub, metadata, key); |
Aurelien Jacobs | f702df3 | 2009-02-15 16:05:37 | [diff] [blame] | 1133 | } |
| 1134 | if (lang) { |
| 1135 | av_strlcat(key, "-", sizeof(key)); |
| 1136 | av_strlcat(key, lang, sizeof(key)); |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1137 | av_dict_set(metadata, key, tags[i].string, 0); |
Aurelien Jacobs | f702df3 | 2009-02-15 16:05:37 | [diff] [blame] | 1138 | if (tags[i].sub.nb_elem) |
| 1139 | matroska_convert_tag(s, &tags[i].sub, metadata, key); |
| 1140 | } |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1141 | } |
Anton Khirnov | ad7768f | 2010-10-16 13:20:41 | [diff] [blame] | 1142 | ff_metadata_conv(metadata, NULL, ff_mkv_metadata_conv); |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | static void matroska_convert_tags(AVFormatContext *s) |
| 1146 | { |
| 1147 | MatroskaDemuxContext *matroska = s->priv_data; |
| 1148 | MatroskaTags *tags = matroska->tags.elem; |
| 1149 | int i, j; |
| 1150 | |
| 1151 | for (i=0; i < matroska->tags.nb_elem; i++) { |
| 1152 | if (tags[i].target.attachuid) { |
| 1153 | MatroskaAttachement *attachment = matroska->attachments.elem; |
| 1154 | for (j=0; j<matroska->attachments.nb_elem; j++) |
Aurelien Jacobs | 9c569ef | 2011-03-23 23:28:19 | [diff] [blame] | 1155 | if (attachment[j].uid == tags[i].target.attachuid |
| 1156 | && attachment[j].stream) |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1157 | matroska_convert_tag(s, &tags[i].tag, |
| 1158 | &attachment[j].stream->metadata, NULL); |
| 1159 | } else if (tags[i].target.chapteruid) { |
| 1160 | MatroskaChapter *chapter = matroska->chapters.elem; |
| 1161 | for (j=0; j<matroska->chapters.nb_elem; j++) |
Aurelien Jacobs | 9c569ef | 2011-03-23 23:28:19 | [diff] [blame] | 1162 | if (chapter[j].uid == tags[i].target.chapteruid |
| 1163 | && chapter[j].chapter) |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1164 | matroska_convert_tag(s, &tags[i].tag, |
| 1165 | &chapter[j].chapter->metadata, NULL); |
| 1166 | } else if (tags[i].target.trackuid) { |
| 1167 | MatroskaTrack *track = matroska->tracks.elem; |
| 1168 | for (j=0; j<matroska->tracks.nb_elem; j++) |
Aurelien Jacobs | 9c569ef | 2011-03-23 23:28:19 | [diff] [blame] | 1169 | if (track[j].uid == tags[i].target.trackuid && track[j].stream) |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1170 | matroska_convert_tag(s, &tags[i].tag, |
| 1171 | &track[j].stream->metadata, NULL); |
| 1172 | } else { |
Aurelien Jacobs | 4f909c7 | 2009-06-13 22:29:38 | [diff] [blame] | 1173 | matroska_convert_tag(s, &tags[i].tag, &s->metadata, |
| 1174 | tags[i].target.type); |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1175 | } |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1179 | static int matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska, int idx) |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 1180 | { |
| 1181 | EbmlList *seekhead_list = &matroska->seekhead; |
| 1182 | MatroskaSeekhead *seekhead = seekhead_list->elem; |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 1183 | uint32_t level_up = matroska->level_up; |
Anton Khirnov | a2704c9 | 2011-03-03 19:11:45 | [diff] [blame] | 1184 | int64_t before_pos = avio_tell(matroska->ctx->pb); |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 1185 | uint32_t saved_id = matroska->current_id; |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 1186 | MatroskaLevel level; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1187 | int64_t offset; |
| 1188 | int ret = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1189 | |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1190 | if (idx >= seekhead_list->nb_elem |
| 1191 | || seekhead[idx].id == MATROSKA_ID_SEEKHEAD |
| 1192 | || seekhead[idx].id == MATROSKA_ID_CLUSTER) |
| 1193 | return 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1194 | |
Anton Khirnov | f47ac3c | 2011-07-09 06:11:30 | [diff] [blame] | 1195 | /* seek */ |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1196 | offset = seekhead[idx].pos + matroska->segment_start; |
| 1197 | if (avio_seek(matroska->ctx->pb, offset, SEEK_SET) == offset) { |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 1198 | /* We don't want to lose our seekhead level, so we add |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1199 | * a dummy. This is a crude hack. */ |
| 1200 | if (matroska->num_levels == EBML_MAX_DEPTH) { |
| 1201 | av_log(matroska->ctx, AV_LOG_INFO, |
| 1202 | "Max EBML element depth (%d) reached, " |
| 1203 | "cannot parse further.\n", EBML_MAX_DEPTH); |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1204 | ret = AVERROR_INVALIDDATA; |
| 1205 | } else { |
Anton Khirnov | f47ac3c | 2011-07-09 06:11:30 | [diff] [blame] | 1206 | level.start = 0; |
| 1207 | level.length = (uint64_t)-1; |
| 1208 | matroska->levels[matroska->num_levels] = level; |
| 1209 | matroska->num_levels++; |
| 1210 | matroska->current_id = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1211 | |
Dustin Brody | 4a9628f | 2011-09-08 22:43:32 | [diff] [blame] | 1212 | ret = ebml_parse(matroska, matroska_segment, matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1213 | |
Anton Khirnov | f47ac3c | 2011-07-09 06:11:30 | [diff] [blame] | 1214 | /* remove dummy level */ |
| 1215 | while (matroska->num_levels) { |
| 1216 | uint64_t length = matroska->levels[--matroska->num_levels].length; |
| 1217 | if (length == (uint64_t)-1) |
| 1218 | break; |
| 1219 | } |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1220 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1221 | } |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1222 | /* seek back */ |
Anton Khirnov | 6b4aa5d | 2011-02-28 13:57:54 | [diff] [blame] | 1223 | avio_seek(matroska->ctx->pb, before_pos, SEEK_SET); |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1224 | matroska->level_up = level_up; |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 1225 | matroska->current_id = saved_id; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1226 | |
| 1227 | return ret; |
| 1228 | } |
| 1229 | |
| 1230 | static void matroska_execute_seekhead(MatroskaDemuxContext *matroska) |
| 1231 | { |
| 1232 | EbmlList *seekhead_list = &matroska->seekhead; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1233 | int64_t before_pos = avio_tell(matroska->ctx->pb); |
| 1234 | int i; |
| 1235 | |
| 1236 | // we should not do any seeking in the streaming case |
| 1237 | if (!matroska->ctx->pb->seekable || |
| 1238 | (matroska->ctx->flags & AVFMT_FLAG_IGNIDX)) |
| 1239 | return; |
| 1240 | |
| 1241 | for (i = 0; i < seekhead_list->nb_elem; i++) { |
Chris Evans | faaec46 | 2012-01-05 20:19:30 | [diff] [blame] | 1242 | MatroskaSeekhead *seekhead = seekhead_list->elem; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1243 | if (seekhead[i].pos <= before_pos) |
| 1244 | continue; |
| 1245 | |
| 1246 | // defer cues parsing until we actually need cue data. |
| 1247 | if (seekhead[i].id == MATROSKA_ID_CUES) { |
| 1248 | matroska->cues_parsing_deferred = 1; |
| 1249 | continue; |
| 1250 | } |
| 1251 | |
| 1252 | if (matroska_parse_seekhead_entry(matroska, i) < 0) |
| 1253 | break; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | static void matroska_parse_cues(MatroskaDemuxContext *matroska) { |
| 1258 | EbmlList *seekhead_list = &matroska->seekhead; |
| 1259 | MatroskaSeekhead *seekhead = seekhead_list->elem; |
| 1260 | EbmlList *index_list; |
| 1261 | MatroskaIndex *index; |
| 1262 | int index_scale = 1; |
| 1263 | int i, j; |
| 1264 | |
| 1265 | for (i = 0; i < seekhead_list->nb_elem; i++) |
Anton Khirnov | fdb9444 | 2011-07-10 19:48:23 | [diff] [blame] | 1266 | if (seekhead[i].id == MATROSKA_ID_CUES) |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1267 | break; |
| 1268 | assert(i <= seekhead_list->nb_elem); |
| 1269 | |
| 1270 | matroska_parse_seekhead_entry(matroska, i); |
| 1271 | |
| 1272 | index_list = &matroska->index; |
| 1273 | index = index_list->elem; |
| 1274 | if (index_list->nb_elem |
| 1275 | && index[0].time > 1E14/matroska->time_scale) { |
| 1276 | av_log(matroska->ctx, AV_LOG_WARNING, "Working around broken index.\n"); |
| 1277 | index_scale = matroska->time_scale; |
| 1278 | } |
| 1279 | for (i = 0; i < index_list->nb_elem; i++) { |
| 1280 | EbmlList *pos_list = &index[i].pos; |
| 1281 | MatroskaIndexPos *pos = pos_list->elem; |
| 1282 | for (j = 0; j < pos_list->nb_elem; j++) { |
| 1283 | MatroskaTrack *track = matroska_find_track_by_num(matroska, pos[j].track); |
| 1284 | if (track && track->stream) |
| 1285 | av_add_index_entry(track->stream, |
| 1286 | pos[j].pos + matroska->segment_start, |
| 1287 | index[i].time/index_scale, 0, 0, |
| 1288 | AVINDEX_KEYFRAME); |
| 1289 | } |
| 1290 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1291 | } |
| 1292 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 1293 | static int matroska_aac_profile(char *codec_id) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1294 | { |
Aurelien Jacobs | ba18b99 | 2008-08-24 13:12:41 | [diff] [blame] | 1295 | static const char * const aac_profiles[] = { "MAIN", "LC", "SSR" }; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1296 | int profile; |
| 1297 | |
Aurelien Jacobs | 37d3e06 | 2008-10-21 21:40:24 | [diff] [blame] | 1298 | for (profile=0; profile<FF_ARRAY_ELEMS(aac_profiles); profile++) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1299 | if (strstr(codec_id, aac_profiles[profile])) |
| 1300 | break; |
| 1301 | return profile + 1; |
| 1302 | } |
| 1303 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 1304 | static int matroska_aac_sri(int samplerate) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1305 | { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1306 | int sri; |
| 1307 | |
Anton Khirnov | 59a9a23 | 2011-10-17 07:28:53 | [diff] [blame] | 1308 | for (sri=0; sri<FF_ARRAY_ELEMS(avpriv_mpeg4audio_sample_rates); sri++) |
| 1309 | if (avpriv_mpeg4audio_sample_rates[sri] == samplerate) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1310 | break; |
| 1311 | return sri; |
| 1312 | } |
| 1313 | |
Anton Khirnov | 6e9651d | 2012-01-12 12:20:36 | [diff] [blame] | 1314 | static int matroska_read_header(AVFormatContext *s) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1315 | { |
| 1316 | MatroskaDemuxContext *matroska = s->priv_data; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1317 | EbmlList *attachements_list = &matroska->attachments; |
| 1318 | MatroskaAttachement *attachements; |
| 1319 | EbmlList *chapters_list = &matroska->chapters; |
| 1320 | MatroskaChapter *chapters; |
Aurelien Jacobs | 9a9a3b0 | 2008-08-05 00:40:49 | [diff] [blame] | 1321 | MatroskaTrack *tracks; |
Aurelien Jacobs | e0e4be5 | 2009-01-15 00:42:57 | [diff] [blame] | 1322 | uint64_t max_start = 0; |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 1323 | Ebml ebml = { 0 }; |
Aurelien Jacobs | 9a9a3b0 | 2008-08-05 00:40:49 | [diff] [blame] | 1324 | AVStream *st; |
Aurelien Jacobs | 0ade7bb | 2010-06-15 19:53:15 | [diff] [blame] | 1325 | int i, j, res; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1326 | |
| 1327 | matroska->ctx = s; |
| 1328 | |
| 1329 | /* First read the EBML header. */ |
Aurelien Jacobs | c4d3d9b | 2008-08-05 00:42:20 | [diff] [blame] | 1330 | if (ebml_parse(matroska, ebml_syntax, &ebml) |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 1331 | || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t) |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 1332 | || ebml.id_length > sizeof(uint32_t) || ebml.doctype_version > 2) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1333 | av_log(matroska->ctx, AV_LOG_ERROR, |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 1334 | "EBML header using unsupported features\n" |
| 1335 | "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n", |
| 1336 | ebml.version, ebml.doctype, ebml.doctype_version); |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 1337 | ebml_free(ebml_syntax, &ebml); |
Aurelien Jacobs | e536ccd | 2010-03-15 00:19:22 | [diff] [blame] | 1338 | return AVERROR_PATCHWELCOME; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1339 | } |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 1340 | for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) |
| 1341 | if (!strcmp(ebml.doctype, matroska_doctypes[i])) |
| 1342 | break; |
| 1343 | if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) { |
David Conrad | c7b913c | 2010-05-22 01:41:35 | [diff] [blame] | 1344 | av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype); |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 1345 | } |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 1346 | ebml_free(ebml_syntax, &ebml); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1347 | |
| 1348 | /* The next thing is a segment. */ |
Aurelien Jacobs | 0ade7bb | 2010-06-15 19:53:15 | [diff] [blame] | 1349 | if ((res = ebml_parse(matroska, matroska_segments, matroska)) < 0) |
| 1350 | return res; |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 1351 | matroska_execute_seekhead(matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1352 | |
David Conrad | d98bd80 | 2010-05-18 21:21:28 | [diff] [blame] | 1353 | if (!matroska->time_scale) |
| 1354 | matroska->time_scale = 1000000; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1355 | if (matroska->duration) |
| 1356 | matroska->ctx->duration = matroska->duration * matroska->time_scale |
| 1357 | * 1000 / AV_TIME_BASE; |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1358 | av_dict_set(&s->metadata, "title", matroska->title, 0); |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1359 | |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1360 | tracks = matroska->tracks.elem; |
| 1361 | for (i=0; i < matroska->tracks.nb_elem; i++) { |
| 1362 | MatroskaTrack *track = &tracks[i]; |
| 1363 | enum CodecID codec_id = CODEC_ID_NONE; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1364 | EbmlList *encodings_list = &tracks->encodings; |
| 1365 | MatroskaTrackEncoding *encodings = encodings_list->elem; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1366 | uint8_t *extradata = NULL; |
| 1367 | int extradata_size = 0; |
| 1368 | int extradata_offset = 0; |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 1369 | AVIOContext b; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1370 | |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1371 | /* Apply some sanity checks. */ |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1372 | if (track->type != MATROSKA_TRACK_TYPE_VIDEO && |
| 1373 | track->type != MATROSKA_TRACK_TYPE_AUDIO && |
| 1374 | track->type != MATROSKA_TRACK_TYPE_SUBTITLE) { |
| 1375 | av_log(matroska->ctx, AV_LOG_INFO, |
| 1376 | "Unknown or unsupported track type %"PRIu64"\n", |
| 1377 | track->type); |
| 1378 | continue; |
| 1379 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1380 | if (track->codec_id == NULL) |
| 1381 | continue; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1382 | |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1383 | if (track->type == MATROSKA_TRACK_TYPE_VIDEO) { |
Mans Rullgard | 3c58300 | 2012-04-18 12:48:20 | [diff] [blame] | 1384 | if (!track->default_duration && track->video.frame_rate > 0) |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1385 | track->default_duration = 1000000000/track->video.frame_rate; |
| 1386 | if (!track->video.display_width) |
| 1387 | track->video.display_width = track->video.pixel_width; |
| 1388 | if (!track->video.display_height) |
| 1389 | track->video.display_height = track->video.pixel_height; |
| 1390 | } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { |
| 1391 | if (!track->audio.out_samplerate) |
| 1392 | track->audio.out_samplerate = track->audio.samplerate; |
| 1393 | } |
| 1394 | if (encodings_list->nb_elem > 1) { |
| 1395 | av_log(matroska->ctx, AV_LOG_ERROR, |
Dustin Brody | d7d2f0e | 2011-09-15 07:34:38 | [diff] [blame] | 1396 | "Multiple combined encodings not supported"); |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1397 | } else if (encodings_list->nb_elem == 1) { |
| 1398 | if (encodings[0].type || |
| 1399 | (encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP && |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame] | 1400 | #if CONFIG_ZLIB |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1401 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB && |
| 1402 | #endif |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame] | 1403 | #if CONFIG_BZLIB |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1404 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB && |
| 1405 | #endif |
| 1406 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO)) { |
| 1407 | encodings[0].scope = 0; |
| 1408 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1409 | "Unsupported encoding type"); |
| 1410 | } else if (track->codec_priv.size && encodings[0].scope&2) { |
| 1411 | uint8_t *codec_priv = track->codec_priv.data; |
| 1412 | int offset = matroska_decode_buffer(&track->codec_priv.data, |
| 1413 | &track->codec_priv.size, |
| 1414 | track); |
| 1415 | if (offset < 0) { |
| 1416 | track->codec_priv.data = NULL; |
| 1417 | track->codec_priv.size = 0; |
| 1418 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1419 | "Failed to decode codec private data\n"); |
| 1420 | } else if (offset > 0) { |
| 1421 | track->codec_priv.data = av_malloc(track->codec_priv.size + offset); |
| 1422 | memcpy(track->codec_priv.data, |
| 1423 | encodings[0].compression.settings.data, offset); |
| 1424 | memcpy(track->codec_priv.data+offset, codec_priv, |
| 1425 | track->codec_priv.size); |
| 1426 | track->codec_priv.size += offset; |
| 1427 | } |
| 1428 | if (codec_priv != track->codec_priv.data) |
| 1429 | av_free(codec_priv); |
| 1430 | } |
| 1431 | } |
| 1432 | |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1433 | for(j=0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++){ |
| 1434 | if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id, |
| 1435 | strlen(ff_mkv_codec_tags[j].str))){ |
| 1436 | codec_id= ff_mkv_codec_tags[j].id; |
| 1437 | break; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1438 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1439 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1440 | |
Anton Khirnov | 3b3bbdd | 2011-06-18 09:43:24 | [diff] [blame] | 1441 | st = track->stream = avformat_new_stream(s, NULL); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1442 | if (st == NULL) |
| 1443 | return AVERROR(ENOMEM); |
| 1444 | |
Aurelien Jacobs | cc8be50 | 2008-08-05 00:42:49 | [diff] [blame] | 1445 | if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC") |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1446 | && track->codec_priv.size >= 40 |
| 1447 | && track->codec_priv.data != NULL) { |
Joakim Plate | 3e93c8e | 2010-03-03 21:46:43 | [diff] [blame] | 1448 | track->ms_compat = 1; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1449 | track->video.fourcc = AV_RL32(track->codec_priv.data + 16); |
Daniel Verkamp | 1a40491 | 2009-06-22 23:09:34 | [diff] [blame] | 1450 | codec_id = ff_codec_get_id(ff_codec_bmp_tags, track->video.fourcc); |
Aurelien Jacobs | 429eeec | 2009-08-24 13:43:33 | [diff] [blame] | 1451 | extradata_offset = 40; |
Aurelien Jacobs | cc8be50 | 2008-08-05 00:42:49 | [diff] [blame] | 1452 | } else if (!strcmp(track->codec_id, "A_MS/ACM") |
Aurelien Jacobs | 038146e | 2009-10-01 21:14:05 | [diff] [blame] | 1453 | && track->codec_priv.size >= 14 |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1454 | && track->codec_priv.data != NULL) { |
Max Horn | ca402f3 | 2011-04-12 15:44:20 | [diff] [blame] | 1455 | int ret; |
Anton Khirnov | e731b8d | 2011-02-20 10:04:13 | [diff] [blame] | 1456 | ffio_init_context(&b, track->codec_priv.data, track->codec_priv.size, |
Stefano Sabatini | 59d9694 | 2011-04-15 14:42:09 | [diff] [blame] | 1457 | AVIO_FLAG_READ, NULL, NULL, NULL, NULL); |
Max Horn | ca402f3 | 2011-04-12 15:44:20 | [diff] [blame] | 1458 | ret = ff_get_wav_header(&b, st->codec, track->codec_priv.size); |
| 1459 | if (ret < 0) |
| 1460 | return ret; |
Aurelien Jacobs | 68a7145 | 2009-01-18 17:13:12 | [diff] [blame] | 1461 | codec_id = st->codec->codec_id; |
Aurelien Jacobs | 038146e | 2009-10-01 21:14:05 | [diff] [blame] | 1462 | extradata_offset = FFMIN(track->codec_priv.size, 18); |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1463 | } else if (!strcmp(track->codec_id, "V_QUICKTIME") |
| 1464 | && (track->codec_priv.size >= 86) |
| 1465 | && (track->codec_priv.data != NULL)) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1466 | track->video.fourcc = AV_RL32(track->codec_priv.data); |
Martin Storsjö | 167f3b8 | 2012-02-15 09:28:39 | [diff] [blame] | 1467 | codec_id=ff_codec_get_id(ff_codec_movvideo_tags, track->video.fourcc); |
Aurelien Jacobs | eb9cf50 | 2008-08-20 00:49:45 | [diff] [blame] | 1468 | } else if (codec_id == CODEC_ID_PCM_S16BE) { |
| 1469 | switch (track->audio.bitdepth) { |
| 1470 | case 8: codec_id = CODEC_ID_PCM_U8; break; |
| 1471 | case 24: codec_id = CODEC_ID_PCM_S24BE; break; |
| 1472 | case 32: codec_id = CODEC_ID_PCM_S32BE; break; |
| 1473 | } |
| 1474 | } else if (codec_id == CODEC_ID_PCM_S16LE) { |
| 1475 | switch (track->audio.bitdepth) { |
| 1476 | case 8: codec_id = CODEC_ID_PCM_U8; break; |
| 1477 | case 24: codec_id = CODEC_ID_PCM_S24LE; break; |
| 1478 | case 32: codec_id = CODEC_ID_PCM_S32LE; break; |
| 1479 | } |
| 1480 | } else if (codec_id==CODEC_ID_PCM_F32LE && track->audio.bitdepth==64) { |
| 1481 | codec_id = CODEC_ID_PCM_F64LE; |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1482 | } else if (codec_id == CODEC_ID_AAC && !track->codec_priv.size) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1483 | int profile = matroska_aac_profile(track->codec_id); |
| 1484 | int sri = matroska_aac_sri(track->audio.samplerate); |
Alex Converse | d2ee8c1 | 2012-01-25 22:34:21 | [diff] [blame] | 1485 | extradata = av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1486 | if (extradata == NULL) |
Aurelien Jacobs | 28f450a | 2008-08-05 00:40:09 | [diff] [blame] | 1487 | return AVERROR(ENOMEM); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1488 | extradata[0] = (profile << 3) | ((sri&0x0E) >> 1); |
| 1489 | extradata[1] = ((sri&0x01) << 7) | (track->audio.channels<<3); |
| 1490 | if (strstr(track->codec_id, "SBR")) { |
| 1491 | sri = matroska_aac_sri(track->audio.out_samplerate); |
| 1492 | extradata[2] = 0x56; |
| 1493 | extradata[3] = 0xE5; |
| 1494 | extradata[4] = 0x80 | (sri<<3); |
| 1495 | extradata_size = 5; |
Aurelien Jacobs | 16f97ab | 2008-08-05 00:41:10 | [diff] [blame] | 1496 | } else |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1497 | extradata_size = 2; |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1498 | } else if (codec_id == CODEC_ID_TTA) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1499 | extradata_size = 30; |
| 1500 | extradata = av_mallocz(extradata_size); |
| 1501 | if (extradata == NULL) |
| 1502 | return AVERROR(ENOMEM); |
Anton Khirnov | e731b8d | 2011-02-20 10:04:13 | [diff] [blame] | 1503 | ffio_init_context(&b, extradata, extradata_size, 1, |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1504 | NULL, NULL, NULL, NULL); |
Anton Khirnov | 77eb550 | 2011-02-21 18:28:17 | [diff] [blame] | 1505 | avio_write(&b, "TTA1", 4); |
| 1506 | avio_wl16(&b, 1); |
| 1507 | avio_wl16(&b, track->audio.channels); |
| 1508 | avio_wl16(&b, track->audio.bitdepth); |
| 1509 | avio_wl32(&b, track->audio.out_samplerate); |
| 1510 | avio_wl32(&b, matroska->ctx->duration * track->audio.out_samplerate); |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1511 | } else if (codec_id == CODEC_ID_RV10 || codec_id == CODEC_ID_RV20 || |
| 1512 | codec_id == CODEC_ID_RV30 || codec_id == CODEC_ID_RV40) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1513 | extradata_offset = 26; |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1514 | } else if (codec_id == CODEC_ID_RA_144) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1515 | track->audio.out_samplerate = 8000; |
| 1516 | track->audio.channels = 1; |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1517 | } else if (codec_id == CODEC_ID_RA_288 || codec_id == CODEC_ID_COOK || |
Aurelien Jacobs | 6b10228 | 2010-03-12 23:49:06 | [diff] [blame] | 1518 | codec_id == CODEC_ID_ATRAC3 || codec_id == CODEC_ID_SIPR) { |
| 1519 | int flavor; |
Anton Khirnov | e731b8d | 2011-02-20 10:04:13 | [diff] [blame] | 1520 | ffio_init_context(&b, track->codec_priv.data,track->codec_priv.size, |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1521 | 0, NULL, NULL, NULL, NULL); |
Anton Khirnov | 45a8a02 | 2011-03-15 08:14:38 | [diff] [blame] | 1522 | avio_skip(&b, 22); |
Anton Khirnov | b7effd4 | 2011-02-21 15:43:01 | [diff] [blame] | 1523 | flavor = avio_rb16(&b); |
| 1524 | track->audio.coded_framesize = avio_rb32(&b); |
Anton Khirnov | 45a8a02 | 2011-03-15 08:14:38 | [diff] [blame] | 1525 | avio_skip(&b, 12); |
Anton Khirnov | b7effd4 | 2011-02-21 15:43:01 | [diff] [blame] | 1526 | track->audio.sub_packet_h = avio_rb16(&b); |
| 1527 | track->audio.frame_size = avio_rb16(&b); |
| 1528 | track->audio.sub_packet_size = avio_rb16(&b); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1529 | track->audio.buf = av_malloc(track->audio.frame_size * track->audio.sub_packet_h); |
| 1530 | if (codec_id == CODEC_ID_RA_288) { |
| 1531 | st->codec->block_align = track->audio.coded_framesize; |
| 1532 | track->codec_priv.size = 0; |
| 1533 | } else { |
Aurelien Jacobs | 6b10228 | 2010-03-12 23:49:06 | [diff] [blame] | 1534 | if (codec_id == CODEC_ID_SIPR && flavor < 4) { |
| 1535 | const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 }; |
| 1536 | track->audio.sub_packet_size = ff_sipr_subpk_size[flavor]; |
| 1537 | st->codec->bit_rate = sipr_bit_rate[flavor]; |
| 1538 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1539 | st->codec->block_align = track->audio.sub_packet_size; |
| 1540 | extradata_offset = 78; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1541 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1542 | } |
Aurelien Jacobs | e264440 | 2009-08-24 13:40:30 | [diff] [blame] | 1543 | track->codec_priv.size -= extradata_offset; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1544 | |
Aurelien Jacobs | 16f97ab | 2008-08-05 00:41:10 | [diff] [blame] | 1545 | if (codec_id == CODEC_ID_NONE) |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1546 | av_log(matroska->ctx, AV_LOG_INFO, |
Aurelien Jacobs | 8f35a2c | 2008-08-05 00:41:22 | [diff] [blame] | 1547 | "Unknown/unsupported CodecID %s.\n", track->codec_id); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1548 | |
Aurelien Jacobs | 3fc9d7c | 2008-09-09 11:23:48 | [diff] [blame] | 1549 | if (track->time_scale < 0.01) |
| 1550 | track->time_scale = 1.0; |
Anton Khirnov | c3f9ebf | 2011-11-29 18:28:15 | [diff] [blame] | 1551 | avpriv_set_pts_info(st, 64, matroska->time_scale*track->time_scale, 1000*1000*1000); /* 64 bit pts in ns */ |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1552 | |
| 1553 | st->codec->codec_id = codec_id; |
| 1554 | st->start_time = 0; |
| 1555 | if (strcmp(track->language, "und")) |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1556 | av_dict_set(&st->metadata, "language", track->language, 0); |
| 1557 | av_dict_set(&st->metadata, "title", track->name, 0); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1558 | |
| 1559 | if (track->flag_default) |
| 1560 | st->disposition |= AV_DISPOSITION_DEFAULT; |
Aurelien Jacobs | 7a617a8 | 2010-07-02 16:38:44 | [diff] [blame] | 1561 | if (track->flag_forced) |
| 1562 | st->disposition |= AV_DISPOSITION_FORCED; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1563 | |
Aurelien Jacobs | ff0d5a7 | 2009-10-01 21:14:46 | [diff] [blame] | 1564 | if (!st->codec->extradata) { |
Aurelien Jacobs | 553e9f7 | 2009-10-01 21:15:36 | [diff] [blame] | 1565 | if(extradata){ |
| 1566 | st->codec->extradata = extradata; |
| 1567 | st->codec->extradata_size = extradata_size; |
| 1568 | } else if(track->codec_priv.data && track->codec_priv.size > 0){ |
| 1569 | st->codec->extradata = av_mallocz(track->codec_priv.size + |
| 1570 | FF_INPUT_BUFFER_PADDING_SIZE); |
| 1571 | if(st->codec->extradata == NULL) |
| 1572 | return AVERROR(ENOMEM); |
| 1573 | st->codec->extradata_size = track->codec_priv.size; |
| 1574 | memcpy(st->codec->extradata, |
| 1575 | track->codec_priv.data + extradata_offset, |
| 1576 | track->codec_priv.size); |
| 1577 | } |
Aurelien Jacobs | ff0d5a7 | 2009-10-01 21:14:46 | [diff] [blame] | 1578 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1579 | |
| 1580 | if (track->type == MATROSKA_TRACK_TYPE_VIDEO) { |
Stefano Sabatini | 72415b2 | 2010-03-30 23:30:55 | [diff] [blame] | 1581 | st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1582 | st->codec->codec_tag = track->video.fourcc; |
| 1583 | st->codec->width = track->video.pixel_width; |
| 1584 | st->codec->height = track->video.pixel_height; |
Aurelien Jacobs | 5972945 | 2008-08-23 23:43:20 | [diff] [blame] | 1585 | av_reduce(&st->sample_aspect_ratio.num, |
| 1586 | &st->sample_aspect_ratio.den, |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1587 | st->codec->height * track->video.display_width, |
| 1588 | st->codec-> width * track->video.display_height, |
| 1589 | 255); |
Aurelien Jacobs | 8306be9 | 2009-08-10 18:12:02 | [diff] [blame] | 1590 | if (st->codec->codec_id != CODEC_ID_H264) |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1591 | st->need_parsing = AVSTREAM_PARSE_HEADERS; |
Luca Barbato | ac97d47 | 2012-04-17 23:32:07 | [diff] [blame] | 1592 | if (track->default_duration) { |
| 1593 | av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, |
| 1594 | 1000000000, track->default_duration, 30000); |
| 1595 | st->avg_frame_rate = st->r_frame_rate; |
| 1596 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1597 | } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { |
Stefano Sabatini | 72415b2 | 2010-03-30 23:30:55 | [diff] [blame] | 1598 | st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1599 | st->codec->sample_rate = track->audio.out_samplerate; |
| 1600 | st->codec->channels = track->audio.channels; |
Baptiste Coudurier | f7501a7 | 2010-04-20 21:23:49 | [diff] [blame] | 1601 | if (st->codec->codec_id != CODEC_ID_AAC) |
Baptiste Coudurier | dc1c26d | 2010-04-20 21:19:27 | [diff] [blame] | 1602 | st->need_parsing = AVSTREAM_PARSE_HEADERS; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1603 | } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) { |
Stefano Sabatini | 72415b2 | 2010-03-30 23:30:55 | [diff] [blame] | 1604 | st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 1605 | if (st->codec->codec_id == CODEC_ID_SSA) |
| 1606 | matroska->contains_ssa = 1; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1607 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1608 | } |
| 1609 | |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1610 | attachements = attachements_list->elem; |
| 1611 | for (j=0; j<attachements_list->nb_elem; j++) { |
| 1612 | if (!(attachements[j].filename && attachements[j].mime && |
| 1613 | attachements[j].bin.data && attachements[j].bin.size > 0)) { |
| 1614 | av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n"); |
| 1615 | } else { |
Anton Khirnov | 3b3bbdd | 2011-06-18 09:43:24 | [diff] [blame] | 1616 | AVStream *st = avformat_new_stream(s, NULL); |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1617 | if (st == NULL) |
| 1618 | break; |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1619 | av_dict_set(&st->metadata, "filename",attachements[j].filename, 0); |
Anton Khirnov | 98cfe22 | 2011-09-27 08:36:53 | [diff] [blame] | 1620 | av_dict_set(&st->metadata, "mimetype", attachements[j].mime, 0); |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1621 | st->codec->codec_id = CODEC_ID_NONE; |
Stefano Sabatini | 72415b2 | 2010-03-30 23:30:55 | [diff] [blame] | 1622 | st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1623 | st->codec->extradata = av_malloc(attachements[j].bin.size); |
| 1624 | if(st->codec->extradata == NULL) |
| 1625 | break; |
| 1626 | st->codec->extradata_size = attachements[j].bin.size; |
| 1627 | memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size); |
| 1628 | |
| 1629 | for (i=0; ff_mkv_mime_tags[i].id != CODEC_ID_NONE; i++) { |
| 1630 | if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime, |
| 1631 | strlen(ff_mkv_mime_tags[i].str))) { |
| 1632 | st->codec->codec_id = ff_mkv_mime_tags[i].id; |
| 1633 | break; |
| 1634 | } |
| 1635 | } |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1636 | attachements[j].stream = st; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | chapters = chapters_list->elem; |
| 1641 | for (i=0; i<chapters_list->nb_elem; i++) |
Aurelien Jacobs | e0e4be5 | 2009-01-15 00:42:57 | [diff] [blame] | 1642 | if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid |
| 1643 | && (max_start==0 || chapters[i].start > max_start)) { |
Aurelien Jacobs | 6cb6e15 | 2009-02-15 15:25:14 | [diff] [blame] | 1644 | chapters[i].chapter = |
Anton Khirnov | 1fa395e4 | 2011-10-17 06:58:50 | [diff] [blame] | 1645 | avpriv_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000}, |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1646 | chapters[i].start, chapters[i].end, |
| 1647 | chapters[i].title); |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1648 | av_dict_set(&chapters[i].chapter->metadata, |
Stefano Sabatini | 2ef6c12 | 2010-04-25 14:27:42 | [diff] [blame] | 1649 | "title", chapters[i].title, 0); |
Aurelien Jacobs | e0e4be5 | 2009-01-15 00:42:57 | [diff] [blame] | 1650 | max_start = chapters[i].start; |
| 1651 | } |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1652 | |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1653 | matroska_convert_tags(s); |
| 1654 | |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 1655 | return 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1656 | } |
| 1657 | |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1658 | /* |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1659 | * Put one packet in an application-supplied AVPacket struct. |
| 1660 | * Returns 0 on success or -1 on failure. |
| 1661 | */ |
| 1662 | static int matroska_deliver_packet(MatroskaDemuxContext *matroska, |
| 1663 | AVPacket *pkt) |
| 1664 | { |
| 1665 | if (matroska->num_packets > 0) { |
| 1666 | memcpy(pkt, matroska->packets[0], sizeof(AVPacket)); |
| 1667 | av_free(matroska->packets[0]); |
| 1668 | if (matroska->num_packets > 1) { |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1669 | void *newpackets; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1670 | memmove(&matroska->packets[0], &matroska->packets[1], |
| 1671 | (matroska->num_packets - 1) * sizeof(AVPacket *)); |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1672 | newpackets = av_realloc(matroska->packets, |
| 1673 | (matroska->num_packets - 1) * sizeof(AVPacket *)); |
| 1674 | if (newpackets) |
| 1675 | matroska->packets = newpackets; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1676 | } else { |
| 1677 | av_freep(&matroska->packets); |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 1678 | matroska->prev_pkt = NULL; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1679 | } |
| 1680 | matroska->num_packets--; |
| 1681 | return 0; |
| 1682 | } |
| 1683 | |
| 1684 | return -1; |
| 1685 | } |
| 1686 | |
| 1687 | /* |
| 1688 | * Free all packets in our internal queue. |
| 1689 | */ |
| 1690 | static void matroska_clear_queue(MatroskaDemuxContext *matroska) |
| 1691 | { |
| 1692 | if (matroska->packets) { |
| 1693 | int n; |
| 1694 | for (n = 0; n < matroska->num_packets; n++) { |
| 1695 | av_free_packet(matroska->packets[n]); |
| 1696 | av_free(matroska->packets[n]); |
| 1697 | } |
Aurelien Jacobs | 00a3431 | 2008-08-06 00:21:10 | [diff] [blame] | 1698 | av_freep(&matroska->packets); |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1699 | matroska->num_packets = 0; |
| 1700 | } |
| 1701 | } |
| 1702 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 1703 | static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, |
| 1704 | int size, int64_t pos, uint64_t cluster_time, |
Aurelien Jacobs | 24c3da1 | 2008-09-06 23:39:59 | [diff] [blame] | 1705 | uint64_t duration, int is_keyframe, |
| 1706 | int64_t cluster_pos) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1707 | { |
Aurelien Jacobs | f14a201 | 2008-09-09 11:54:35 | [diff] [blame] | 1708 | uint64_t timecode = AV_NOPTS_VALUE; |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 1709 | MatroskaTrack *track; |
Aurelien Jacobs | a3467f8 | 2008-09-06 23:44:29 | [diff] [blame] | 1710 | int res = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1711 | AVStream *st; |
| 1712 | AVPacket *pkt; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1713 | int16_t block_time; |
| 1714 | uint32_t *lace_size = NULL; |
| 1715 | int n, flags, laces = 0; |
| 1716 | uint64_t num; |
| 1717 | |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 1718 | if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1719 | av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n"); |
Luca Barbato | 721af29 | 2012-04-30 00:39:31 | [diff] [blame] | 1720 | return n; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1721 | } |
| 1722 | data += n; |
| 1723 | size -= n; |
| 1724 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1725 | track = matroska_find_track_by_num(matroska, num); |
Ronald S. Bultje | d31fb1a | 2011-10-29 23:17:51 | [diff] [blame] | 1726 | if (!track || !track->stream) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1727 | av_log(matroska->ctx, AV_LOG_INFO, |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 1728 | "Invalid stream %"PRIu64" or size %u\n", num, size); |
Ami Fischman | 5dd514a | 2011-06-29 20:54:49 | [diff] [blame] | 1729 | return AVERROR_INVALIDDATA; |
Ronald S. Bultje | d31fb1a | 2011-10-29 23:17:51 | [diff] [blame] | 1730 | } else if (size <= 3) |
| 1731 | return 0; |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 1732 | st = track->stream; |
Aurelien Jacobs | 16f97ab | 2008-08-05 00:41:10 | [diff] [blame] | 1733 | if (st->discard >= AVDISCARD_ALL) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1734 | return res; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1735 | if (duration == AV_NOPTS_VALUE) |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 1736 | duration = track->default_duration / matroska->time_scale; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1737 | |
Aurelien Jacobs | 2ce746c | 2007-06-23 12:32:19 | [diff] [blame] | 1738 | block_time = AV_RB16(data); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1739 | data += 2; |
Aurelien Jacobs | 1607c53 | 2007-06-23 12:49:36 | [diff] [blame] | 1740 | flags = *data++; |
| 1741 | size -= 3; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1742 | if (is_keyframe == -1) |
Jean-Daniel Dupas | cc947f0 | 2010-03-31 12:29:58 | [diff] [blame] | 1743 | is_keyframe = flags & 0x80 ? AV_PKT_FLAG_KEY : 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1744 | |
Aurelien Jacobs | f14a201 | 2008-09-09 11:54:35 | [diff] [blame] | 1745 | if (cluster_time != (uint64_t)-1 |
| 1746 | && (block_time >= 0 || cluster_time >= -block_time)) { |
| 1747 | timecode = cluster_time + block_time; |
Aurelien Jacobs | 82360e6 | 2008-09-09 12:07:10 | [diff] [blame] | 1748 | if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE |
| 1749 | && timecode < track->end_timecode) |
| 1750 | is_keyframe = 0; /* overlapping subtitles are not key frame */ |
Aurelien Jacobs | a8fd7e7 | 2008-09-12 00:06:06 | [diff] [blame] | 1751 | if (is_keyframe) |
Aurelien Jacobs | f14a201 | 2008-09-09 11:54:35 | [diff] [blame] | 1752 | av_add_index_entry(st, cluster_pos, timecode, 0,0,AVINDEX_KEYFRAME); |
Aurelien Jacobs | 82360e6 | 2008-09-09 12:07:10 | [diff] [blame] | 1753 | track->end_timecode = FFMAX(track->end_timecode, timecode+duration); |
Aurelien Jacobs | f14a201 | 2008-09-09 11:54:35 | [diff] [blame] | 1754 | } |
| 1755 | |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 1756 | if (matroska->skip_to_keyframe && track->type != MATROSKA_TRACK_TYPE_SUBTITLE) { |
Aurelien Jacobs | 20f7466 | 2008-09-09 12:01:51 | [diff] [blame] | 1757 | if (!is_keyframe || timecode < matroska->skip_to_timecode) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1758 | return res; |
| 1759 | matroska->skip_to_keyframe = 0; |
| 1760 | } |
| 1761 | |
| 1762 | switch ((flags & 0x06) >> 1) { |
| 1763 | case 0x0: /* no lacing */ |
| 1764 | laces = 1; |
| 1765 | lace_size = av_mallocz(sizeof(int)); |
| 1766 | lace_size[0] = size; |
| 1767 | break; |
| 1768 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 1769 | case 0x1: /* Xiph lacing */ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1770 | case 0x2: /* fixed-size lacing */ |
| 1771 | case 0x3: /* EBML lacing */ |
Michael Niedermayer | 9bf8b56 | 2008-05-28 21:22:08 | [diff] [blame] | 1772 | assert(size>0); // size <=3 is checked before size-=3 above |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1773 | laces = (*data) + 1; |
| 1774 | data += 1; |
| 1775 | size -= 1; |
| 1776 | lace_size = av_mallocz(laces * sizeof(int)); |
| 1777 | |
| 1778 | switch ((flags & 0x06) >> 1) { |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 1779 | case 0x1: /* Xiph lacing */ { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1780 | uint8_t temp; |
| 1781 | uint32_t total = 0; |
| 1782 | for (n = 0; res == 0 && n < laces - 1; n++) { |
| 1783 | while (1) { |
| 1784 | if (size == 0) { |
| 1785 | res = -1; |
| 1786 | break; |
| 1787 | } |
| 1788 | temp = *data; |
| 1789 | lace_size[n] += temp; |
| 1790 | data += 1; |
| 1791 | size -= 1; |
| 1792 | if (temp != 0xff) |
| 1793 | break; |
| 1794 | } |
| 1795 | total += lace_size[n]; |
| 1796 | } |
| 1797 | lace_size[n] = size - total; |
| 1798 | break; |
| 1799 | } |
| 1800 | |
| 1801 | case 0x2: /* fixed-size lacing */ |
| 1802 | for (n = 0; n < laces; n++) |
| 1803 | lace_size[n] = size / laces; |
| 1804 | break; |
| 1805 | |
| 1806 | case 0x3: /* EBML lacing */ { |
| 1807 | uint32_t total; |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 1808 | n = matroska_ebmlnum_uint(matroska, data, size, &num); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1809 | if (n < 0) { |
| 1810 | av_log(matroska->ctx, AV_LOG_INFO, |
| 1811 | "EBML block data error\n"); |
| 1812 | break; |
| 1813 | } |
| 1814 | data += n; |
| 1815 | size -= n; |
| 1816 | total = lace_size[0] = num; |
| 1817 | for (n = 1; res == 0 && n < laces - 1; n++) { |
| 1818 | int64_t snum; |
| 1819 | int r; |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 1820 | r = matroska_ebmlnum_sint(matroska, data, size, &snum); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1821 | if (r < 0) { |
| 1822 | av_log(matroska->ctx, AV_LOG_INFO, |
| 1823 | "EBML block data error\n"); |
| 1824 | break; |
| 1825 | } |
| 1826 | data += r; |
| 1827 | size -= r; |
| 1828 | lace_size[n] = lace_size[n - 1] + snum; |
| 1829 | total += lace_size[n]; |
| 1830 | } |
Ronald S. Bultje | 723229c | 2011-10-14 22:03:55 | [diff] [blame] | 1831 | lace_size[laces - 1] = size - total; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1832 | break; |
| 1833 | } |
| 1834 | } |
| 1835 | break; |
| 1836 | } |
| 1837 | |
| 1838 | if (res == 0) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1839 | for (n = 0; n < laces; n++) { |
David Conrad | 18ca491 | 2010-01-11 00:31:55 | [diff] [blame] | 1840 | if ((st->codec->codec_id == CODEC_ID_RA_288 || |
| 1841 | st->codec->codec_id == CODEC_ID_COOK || |
Aurelien Jacobs | 6b10228 | 2010-03-12 23:49:06 | [diff] [blame] | 1842 | st->codec->codec_id == CODEC_ID_SIPR || |
David Conrad | 18ca491 | 2010-01-11 00:31:55 | [diff] [blame] | 1843 | st->codec->codec_id == CODEC_ID_ATRAC3) && |
| 1844 | st->codec->block_align && track->audio.sub_packet_size) { |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1845 | int a = st->codec->block_align; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1846 | int sps = track->audio.sub_packet_size; |
| 1847 | int cfs = track->audio.coded_framesize; |
| 1848 | int h = track->audio.sub_packet_h; |
| 1849 | int y = track->audio.sub_packet_cnt; |
| 1850 | int w = track->audio.frame_size; |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1851 | int x; |
Aurelien Jacobs | eabb8ba | 2007-06-04 22:19:17 | [diff] [blame] | 1852 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1853 | if (!track->audio.pkt_cnt) { |
Reimar Döffinger | 4654420 | 2011-02-26 11:52:01 | [diff] [blame] | 1854 | if (track->audio.sub_packet_cnt == 0) |
| 1855 | track->audio.buf_timecode = timecode; |
Ronald S. Bultje | 9c239f6 | 2012-03-02 01:01:22 | [diff] [blame] | 1856 | if (st->codec->codec_id == CODEC_ID_RA_288) { |
| 1857 | if (size < cfs * h / 2) { |
| 1858 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1859 | "Corrupt int4 RM-style audio packet size\n"); |
Dale Curtis | 3116858 | 2012-04-13 04:24:04 | [diff] [blame] | 1860 | res = AVERROR_INVALIDDATA; |
| 1861 | goto end; |
Ronald S. Bultje | 9c239f6 | 2012-03-02 01:01:22 | [diff] [blame] | 1862 | } |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1863 | for (x=0; x<h/2; x++) |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1864 | memcpy(track->audio.buf+x*2*w+y*cfs, |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1865 | data+x*cfs, cfs); |
Ronald S. Bultje | 9c239f6 | 2012-03-02 01:01:22 | [diff] [blame] | 1866 | } else if (st->codec->codec_id == CODEC_ID_SIPR) { |
| 1867 | if (size < w) { |
| 1868 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1869 | "Corrupt sipr RM-style audio packet size\n"); |
Dale Curtis | 3116858 | 2012-04-13 04:24:04 | [diff] [blame] | 1870 | res = AVERROR_INVALIDDATA; |
| 1871 | goto end; |
Ronald S. Bultje | 9c239f6 | 2012-03-02 01:01:22 | [diff] [blame] | 1872 | } |
Aurelien Jacobs | 6b10228 | 2010-03-12 23:49:06 | [diff] [blame] | 1873 | memcpy(track->audio.buf + y*w, data, w); |
Ronald S. Bultje | 9c239f6 | 2012-03-02 01:01:22 | [diff] [blame] | 1874 | } else { |
| 1875 | if (size < sps * w / sps) { |
| 1876 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1877 | "Corrupt generic RM-style audio packet size\n"); |
Dale Curtis | 3116858 | 2012-04-13 04:24:04 | [diff] [blame] | 1878 | res = AVERROR_INVALIDDATA; |
| 1879 | goto end; |
Ronald S. Bultje | 9c239f6 | 2012-03-02 01:01:22 | [diff] [blame] | 1880 | } |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1881 | for (x=0; x<w/sps; x++) |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1882 | memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps); |
Ronald S. Bultje | 9c239f6 | 2012-03-02 01:01:22 | [diff] [blame] | 1883 | } |
Aurelien Jacobs | eabb8ba | 2007-06-04 22:19:17 | [diff] [blame] | 1884 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1885 | if (++track->audio.sub_packet_cnt >= h) { |
Aurelien Jacobs | 6b10228 | 2010-03-12 23:49:06 | [diff] [blame] | 1886 | if (st->codec->codec_id == CODEC_ID_SIPR) |
| 1887 | ff_rm_reorder_sipr_data(track->audio.buf, h, w); |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1888 | track->audio.sub_packet_cnt = 0; |
| 1889 | track->audio.pkt_cnt = h*w / a; |
Aurelien Jacobs | eabb8ba | 2007-06-04 22:19:17 | [diff] [blame] | 1890 | } |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1891 | } |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1892 | while (track->audio.pkt_cnt) { |
Aurelien Jacobs | 77abe5e | 2007-06-04 22:21:29 | [diff] [blame] | 1893 | pkt = av_mallocz(sizeof(AVPacket)); |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1894 | av_new_packet(pkt, a); |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1895 | memcpy(pkt->data, track->audio.buf |
| 1896 | + a * (h*w / a - track->audio.pkt_cnt--), a); |
Reimar Döffinger | 4654420 | 2011-02-26 11:52:01 | [diff] [blame] | 1897 | pkt->pts = track->audio.buf_timecode; |
| 1898 | track->audio.buf_timecode = AV_NOPTS_VALUE; |
Aurelien Jacobs | 77abe5e | 2007-06-04 22:21:29 | [diff] [blame] | 1899 | pkt->pos = pos; |
Aurelien Jacobs | fc4d335 | 2008-08-05 00:40:06 | [diff] [blame] | 1900 | pkt->stream_index = st->index; |
Anton Khirnov | b870253 | 2008-08-06 00:17:47 | [diff] [blame] | 1901 | dynarray_add(&matroska->packets,&matroska->num_packets,pkt); |
Aurelien Jacobs | eabb8ba | 2007-06-04 22:19:17 | [diff] [blame] | 1902 | } |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1903 | } else { |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1904 | MatroskaTrackEncoding *encodings = track->encodings.elem; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1905 | int offset = 0, pkt_size = lace_size[n]; |
Aurelien Jacobs | de3230f | 2008-05-09 01:53:59 | [diff] [blame] | 1906 | uint8_t *pkt_data = data; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1907 | |
Aurelien Jacobs | e110e96 | 2010-08-17 14:25:14 | [diff] [blame] | 1908 | if (pkt_size > size) { |
David Conrad | e48f7ff | 2010-03-07 02:26:30 | [diff] [blame] | 1909 | av_log(matroska->ctx, AV_LOG_ERROR, "Invalid packet size\n"); |
| 1910 | break; |
| 1911 | } |
| 1912 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1913 | if (encodings && encodings->scope & 1) { |
Aurelien Jacobs | 8f35a2c | 2008-08-05 00:41:22 | [diff] [blame] | 1914 | offset = matroska_decode_buffer(&pkt_data,&pkt_size, track); |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1915 | if (offset < 0) |
| 1916 | continue; |
Aurelien Jacobs | 53a1e82 | 2008-05-08 21:47:31 | [diff] [blame] | 1917 | } |
| 1918 | |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1919 | pkt = av_mallocz(sizeof(AVPacket)); |
| 1920 | /* XXX: prevent data copy... */ |
Aurelien Jacobs | de3230f | 2008-05-09 01:53:59 | [diff] [blame] | 1921 | if (av_new_packet(pkt, pkt_size+offset) < 0) { |
Aurelien Jacobs | 34ae409 | 2008-06-02 23:27:14 | [diff] [blame] | 1922 | av_free(pkt); |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1923 | res = AVERROR(ENOMEM); |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1924 | break; |
| 1925 | } |
Aurelien Jacobs | 53a1e82 | 2008-05-08 21:47:31 | [diff] [blame] | 1926 | if (offset) |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1927 | memcpy (pkt->data, encodings->compression.settings.data, offset); |
Aurelien Jacobs | de3230f | 2008-05-09 01:53:59 | [diff] [blame] | 1928 | memcpy (pkt->data+offset, pkt_data, pkt_size); |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1929 | |
Aurelien Jacobs | 51e1cc1 | 2008-06-22 15:46:36 | [diff] [blame] | 1930 | if (pkt_data != data) |
| 1931 | av_free(pkt_data); |
| 1932 | |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1933 | if (n == 0) |
| 1934 | pkt->flags = is_keyframe; |
Aurelien Jacobs | fc4d335 | 2008-08-05 00:40:06 | [diff] [blame] | 1935 | pkt->stream_index = st->index; |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1936 | |
Joakim Plate | 3e93c8e | 2010-03-03 21:46:43 | [diff] [blame] | 1937 | if (track->ms_compat) |
| 1938 | pkt->dts = timecode; |
| 1939 | else |
Aurelien Jacobs | 68b0fd7 | 2010-03-03 21:49:24 | [diff] [blame] | 1940 | pkt->pts = timecode; |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1941 | pkt->pos = pos; |
Aurelien Jacobs | 1bb4a1a | 2008-09-28 22:58:53 | [diff] [blame] | 1942 | if (st->codec->codec_id == CODEC_ID_TEXT) |
Aurelien Jacobs | 62c2470 | 2008-09-04 23:08:19 | [diff] [blame] | 1943 | pkt->convergence_duration = duration; |
Aurelien Jacobs | 1bb4a1a | 2008-09-28 22:58:53 | [diff] [blame] | 1944 | else if (track->type != MATROSKA_TRACK_TYPE_SUBTITLE) |
Aurelien Jacobs | 62c2470 | 2008-09-04 23:08:19 | [diff] [blame] | 1945 | pkt->duration = duration; |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1946 | |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 1947 | if (st->codec->codec_id == CODEC_ID_SSA) |
Aurelien Jacobs | e7d4b74 | 2008-09-28 22:55:28 | [diff] [blame] | 1948 | matroska_fix_ass_packet(matroska, pkt, duration); |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 1949 | |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 1950 | if (matroska->prev_pkt && |
Aurelien Jacobs | 21a115d | 2008-10-02 21:14:56 | [diff] [blame] | 1951 | timecode != AV_NOPTS_VALUE && |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 1952 | matroska->prev_pkt->pts == timecode && |
David Conrad | 41c1ccc | 2010-07-02 16:41:38 | [diff] [blame] | 1953 | matroska->prev_pkt->stream_index == st->index && |
| 1954 | st->codec->codec_id == CODEC_ID_SSA) |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 1955 | matroska_merge_packets(matroska->prev_pkt, pkt); |
| 1956 | else { |
Aurelien Jacobs | c58e8bd | 2008-10-02 21:15:48 | [diff] [blame] | 1957 | dynarray_add(&matroska->packets,&matroska->num_packets,pkt); |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 1958 | matroska->prev_pkt = pkt; |
| 1959 | } |
Aurelien Jacobs | ba8a76b | 2007-10-21 22:27:24 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | if (timecode != AV_NOPTS_VALUE) |
| 1963 | timecode = duration ? timecode + duration : AV_NOPTS_VALUE; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1964 | data += lace_size[n]; |
David Conrad | e48f7ff | 2010-03-07 02:26:30 | [diff] [blame] | 1965 | size -= lace_size[n]; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1966 | } |
| 1967 | } |
| 1968 | |
Dale Curtis | 3116858 | 2012-04-13 04:24:04 | [diff] [blame] | 1969 | end: |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1970 | av_free(lace_size); |
Aurelien Jacobs | a3467f8 | 2008-09-06 23:44:29 | [diff] [blame] | 1971 | return res; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1972 | } |
| 1973 | |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 1974 | static int matroska_parse_cluster_incremental(MatroskaDemuxContext *matroska) |
| 1975 | { |
| 1976 | EbmlList *blocks_list; |
| 1977 | MatroskaBlock *blocks; |
| 1978 | int i, res; |
| 1979 | res = ebml_parse(matroska, |
| 1980 | matroska_cluster_incremental_parsing, |
| 1981 | &matroska->current_cluster); |
| 1982 | if (res == 1) { |
| 1983 | /* New Cluster */ |
| 1984 | if (matroska->current_cluster_pos) |
| 1985 | ebml_level_end(matroska); |
| 1986 | ebml_free(matroska_cluster, &matroska->current_cluster); |
| 1987 | memset(&matroska->current_cluster, 0, sizeof(MatroskaCluster)); |
| 1988 | matroska->current_cluster_num_blocks = 0; |
| 1989 | matroska->current_cluster_pos = avio_tell(matroska->ctx->pb); |
| 1990 | matroska->prev_pkt = NULL; |
| 1991 | /* sizeof the ID which was already read */ |
| 1992 | if (matroska->current_id) |
| 1993 | matroska->current_cluster_pos -= 4; |
| 1994 | res = ebml_parse(matroska, |
| 1995 | matroska_clusters_incremental, |
| 1996 | &matroska->current_cluster); |
| 1997 | /* Try parsing the block again. */ |
| 1998 | if (res == 1) |
| 1999 | res = ebml_parse(matroska, |
| 2000 | matroska_cluster_incremental_parsing, |
| 2001 | &matroska->current_cluster); |
| 2002 | } |
| 2003 | |
| 2004 | if (!res && |
| 2005 | matroska->current_cluster_num_blocks < |
| 2006 | matroska->current_cluster.blocks.nb_elem) { |
| 2007 | blocks_list = &matroska->current_cluster.blocks; |
| 2008 | blocks = blocks_list->elem; |
| 2009 | |
| 2010 | matroska->current_cluster_num_blocks = blocks_list->nb_elem; |
| 2011 | i = blocks_list->nb_elem - 1; |
| 2012 | if (blocks[i].bin.size > 0 && blocks[i].bin.data) { |
| 2013 | int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1; |
| 2014 | if (!blocks[i].non_simple) |
| 2015 | blocks[i].duration = AV_NOPTS_VALUE; |
| 2016 | res = matroska_parse_block(matroska, |
| 2017 | blocks[i].bin.data, blocks[i].bin.size, |
| 2018 | blocks[i].bin.pos, |
| 2019 | matroska->current_cluster.timecode, |
| 2020 | blocks[i].duration, is_keyframe, |
| 2021 | matroska->current_cluster_pos); |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | if (res < 0) matroska->done = 1; |
| 2026 | return res; |
| 2027 | } |
| 2028 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 2029 | static int matroska_parse_cluster(MatroskaDemuxContext *matroska) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2030 | { |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 2031 | MatroskaCluster cluster = { 0 }; |
| 2032 | EbmlList *blocks_list; |
| 2033 | MatroskaBlock *blocks; |
Aurelien Jacobs | 24c3da1 | 2008-09-06 23:39:59 | [diff] [blame] | 2034 | int i, res; |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 2035 | int64_t pos; |
| 2036 | if (!matroska->contains_ssa) |
| 2037 | return matroska_parse_cluster_incremental(matroska); |
| 2038 | pos = avio_tell(matroska->ctx->pb); |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 2039 | matroska->prev_pkt = NULL; |
Aurelien Jacobs | 8070203 | 2010-06-11 16:36:51 | [diff] [blame] | 2040 | if (matroska->current_id) |
Aurelien Jacobs | 8bc98ba | 2008-08-25 00:09:08 | [diff] [blame] | 2041 | pos -= 4; /* sizeof the ID which was already read */ |
Aurelien Jacobs | 7391781 | 2010-06-11 16:45:38 | [diff] [blame] | 2042 | res = ebml_parse(matroska, matroska_clusters, &cluster); |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 2043 | blocks_list = &cluster.blocks; |
| 2044 | blocks = blocks_list->elem; |
Ami Fischman | 5dd514a | 2011-06-29 20:54:49 | [diff] [blame] | 2045 | for (i=0; i<blocks_list->nb_elem && !res; i++) |
Aurelien Jacobs | 37dd235 | 2010-05-25 22:55:12 | [diff] [blame] | 2046 | if (blocks[i].bin.size > 0 && blocks[i].bin.data) { |
Aurelien Jacobs | 194d4b4 | 2009-08-10 18:06:14 | [diff] [blame] | 2047 | int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1; |
John Stebbins | 98a7d56 | 2011-03-24 23:34:18 | [diff] [blame] | 2048 | if (!blocks[i].non_simple) |
| 2049 | blocks[i].duration = AV_NOPTS_VALUE; |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 2050 | res=matroska_parse_block(matroska, |
| 2051 | blocks[i].bin.data, blocks[i].bin.size, |
| 2052 | blocks[i].bin.pos, cluster.timecode, |
Aurelien Jacobs | 194d4b4 | 2009-08-10 18:06:14 | [diff] [blame] | 2053 | blocks[i].duration, is_keyframe, |
Aurelien Jacobs | 24c3da1 | 2008-09-06 23:39:59 | [diff] [blame] | 2054 | pos); |
Aurelien Jacobs | 194d4b4 | 2009-08-10 18:06:14 | [diff] [blame] | 2055 | } |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 2056 | ebml_free(matroska_cluster, &cluster); |
Aurelien Jacobs | 653fb2f | 2008-08-24 23:54:14 | [diff] [blame] | 2057 | if (res < 0) matroska->done = 1; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2058 | return res; |
| 2059 | } |
| 2060 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 2061 | static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2062 | { |
| 2063 | MatroskaDemuxContext *matroska = s->priv_data; |
Ami Fischman | 5dd514a | 2011-06-29 20:54:49 | [diff] [blame] | 2064 | int ret = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2065 | |
Ami Fischman | 5dd514a | 2011-06-29 20:54:49 | [diff] [blame] | 2066 | while (!ret && matroska_deliver_packet(matroska, pkt)) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2067 | if (matroska->done) |
Aurelien Jacobs | 9ebeea8 | 2009-02-19 21:01:45 | [diff] [blame] | 2068 | return AVERROR_EOF; |
Ami Fischman | 5dd514a | 2011-06-29 20:54:49 | [diff] [blame] | 2069 | ret = matroska_parse_cluster(matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2070 | } |
| 2071 | |
Luca Barbato | 0ca4642 | 2012-04-30 00:55:51 | [diff] [blame^] | 2072 | if (ret == AVERROR_INVALIDDATA) { |
| 2073 | pkt->flags |= AV_PKT_FLAG_CORRUPT; |
| 2074 | return 0; |
| 2075 | } |
| 2076 | |
Ami Fischman | 5dd514a | 2011-06-29 20:54:49 | [diff] [blame] | 2077 | return ret; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2078 | } |
| 2079 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 2080 | static int matroska_read_seek(AVFormatContext *s, int stream_index, |
| 2081 | int64_t timestamp, int flags) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2082 | { |
| 2083 | MatroskaDemuxContext *matroska = s->priv_data; |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 2084 | MatroskaTrack *tracks = matroska->tracks.elem; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2085 | AVStream *st = s->streams[stream_index]; |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 2086 | int i, index, index_sub, index_min; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2087 | |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 2088 | /* Parse the CUES now since we need the index data to seek. */ |
| 2089 | if (matroska->cues_parsing_deferred) { |
| 2090 | matroska_parse_cues(matroska); |
| 2091 | matroska->cues_parsing_deferred = 0; |
| 2092 | } |
| 2093 | |
Aurelien Jacobs | a8fd7e7 | 2008-09-12 00:06:06 | [diff] [blame] | 2094 | if (!st->nb_index_entries) |
| 2095 | return 0; |
| 2096 | timestamp = FFMAX(timestamp, st->index_entries[0].timestamp); |
Aurelien Jacobs | dfbbbdc | 2008-08-24 23:57:29 | [diff] [blame] | 2097 | |
Aurelien Jacobs | 6bef5f9 | 2008-08-27 19:57:42 | [diff] [blame] | 2098 | if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) { |
Anton Khirnov | 6b4aa5d | 2011-02-28 13:57:54 | [diff] [blame] | 2099 | avio_seek(s->pb, st->index_entries[st->nb_index_entries-1].pos, SEEK_SET); |
John Stebbins | cdc2c1c | 2011-07-01 15:57:42 | [diff] [blame] | 2100 | matroska->current_id = 0; |
Aurelien Jacobs | 0dbddda | 2008-08-27 19:58:55 | [diff] [blame] | 2101 | while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) { |
Dale Curtis | 7521c4b | 2012-04-23 16:15:31 | [diff] [blame] | 2102 | matroska->prev_pkt = NULL; |
Aurelien Jacobs | 0dbddda | 2008-08-27 19:58:55 | [diff] [blame] | 2103 | matroska_clear_queue(matroska); |
| 2104 | if (matroska_parse_cluster(matroska) < 0) |
| 2105 | break; |
| 2106 | } |
Aurelien Jacobs | 6bef5f9 | 2008-08-27 19:57:42 | [diff] [blame] | 2107 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2108 | |
Aurelien Jacobs | 243cc4c | 2007-12-29 18:35:38 | [diff] [blame] | 2109 | matroska_clear_queue(matroska); |
Aurelien Jacobs | 0f646a2 | 2008-08-25 00:15:49 | [diff] [blame] | 2110 | if (index < 0) |
| 2111 | return 0; |
Aurelien Jacobs | 243cc4c | 2007-12-29 18:35:38 | [diff] [blame] | 2112 | |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 2113 | index_min = index; |
| 2114 | for (i=0; i < matroska->tracks.nb_elem; i++) { |
Reimar Döffinger | 4654420 | 2011-02-26 11:52:01 | [diff] [blame] | 2115 | tracks[i].audio.pkt_cnt = 0; |
| 2116 | tracks[i].audio.sub_packet_cnt = 0; |
| 2117 | tracks[i].audio.buf_timecode = AV_NOPTS_VALUE; |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 2118 | tracks[i].end_timecode = 0; |
| 2119 | if (tracks[i].type == MATROSKA_TRACK_TYPE_SUBTITLE |
| 2120 | && !tracks[i].stream->discard != AVDISCARD_ALL) { |
| 2121 | index_sub = av_index_search_timestamp(tracks[i].stream, st->index_entries[index].timestamp, AVSEEK_FLAG_BACKWARD); |
| 2122 | if (index_sub >= 0 |
| 2123 | && st->index_entries[index_sub].pos < st->index_entries[index_min].pos |
| 2124 | && st->index_entries[index].timestamp - st->index_entries[index_sub].timestamp < 30000000000/matroska->time_scale) |
| 2125 | index_min = index_sub; |
| 2126 | } |
| 2127 | } |
| 2128 | |
Anton Khirnov | 6b4aa5d | 2011-02-28 13:57:54 | [diff] [blame] | 2129 | avio_seek(s->pb, st->index_entries[index_min].pos, SEEK_SET); |
John Stebbins | cdc2c1c | 2011-07-01 15:57:42 | [diff] [blame] | 2130 | matroska->current_id = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2131 | matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY); |
Aurelien Jacobs | 20f7466 | 2008-09-09 12:01:51 | [diff] [blame] | 2132 | matroska->skip_to_timecode = st->index_entries[index].timestamp; |
Aurelien Jacobs | 244ee48 | 2008-08-25 00:17:31 | [diff] [blame] | 2133 | matroska->done = 0; |
Anton Khirnov | a2faa95 | 2011-10-16 13:03:30 | [diff] [blame] | 2134 | ff_update_cur_dts(s, st, st->index_entries[index].timestamp); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2135 | return 0; |
| 2136 | } |
| 2137 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 2138 | static int matroska_read_close(AVFormatContext *s) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2139 | { |
| 2140 | MatroskaDemuxContext *matroska = s->priv_data; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 2141 | MatroskaTrack *tracks = matroska->tracks.elem; |
Aurelien Jacobs | 70109c0 | 2008-08-05 00:41:13 | [diff] [blame] | 2142 | int n; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2143 | |
Aurelien Jacobs | 34c9c1b | 2007-12-29 18:32:47 | [diff] [blame] | 2144 | matroska_clear_queue(matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2145 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 2146 | for (n=0; n < matroska->tracks.nb_elem; n++) |
| 2147 | if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO) |
| 2148 | av_free(tracks[n].audio.buf); |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 2149 | ebml_free(matroska_cluster, &matroska->current_cluster); |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 2150 | ebml_free(matroska_segment, matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2151 | |
| 2152 | return 0; |
| 2153 | } |
| 2154 | |
Diego Elio Pettenò | c6610a2 | 2011-01-25 22:03:28 | [diff] [blame] | 2155 | AVInputFormat ff_matroska_demuxer = { |
Anton Khirnov | dfc2c4d | 2011-07-16 20:18:12 | [diff] [blame] | 2156 | .name = "matroska,webm", |
| 2157 | .long_name = NULL_IF_CONFIG_SMALL("Matroska/WebM file format"), |
| 2158 | .priv_data_size = sizeof(MatroskaDemuxContext), |
| 2159 | .read_probe = matroska_probe, |
| 2160 | .read_header = matroska_read_header, |
| 2161 | .read_packet = matroska_read_packet, |
| 2162 | .read_close = matroska_read_close, |
| 2163 | .read_seek = matroska_read_seek, |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2164 | }; |