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