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) }, |
Aurelien Jacobs | 194d4b4 | 2009-08-10 18:06:14 | [diff] [blame] | 503 | { 1, EBML_UINT, 0, offsetof(MatroskaBlock,non_simple), {.u=1} }, |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 504 | { 0 } |
| 505 | }; |
| 506 | |
| 507 | static EbmlSyntax matroska_cluster[] = { |
| 508 | { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) }, |
| 509 | { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} }, |
| 510 | { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 511 | { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE }, |
| 512 | { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE }, |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 513 | { 0 } |
| 514 | }; |
| 515 | |
| 516 | static EbmlSyntax matroska_clusters[] = { |
| 517 | { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster} }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 518 | { MATROSKA_ID_INFO, EBML_NONE }, |
| 519 | { MATROSKA_ID_CUES, EBML_NONE }, |
| 520 | { MATROSKA_ID_TAGS, EBML_NONE }, |
| 521 | { MATROSKA_ID_SEEKHEAD, EBML_NONE }, |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 522 | { 0 } |
| 523 | }; |
| 524 | |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 525 | static EbmlSyntax matroska_cluster_incremental_parsing[] = { |
| 526 | { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) }, |
| 527 | { MATROSKA_ID_BLOCKGROUP, EBML_NEST, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} }, |
| 528 | { MATROSKA_ID_SIMPLEBLOCK, EBML_PASS, sizeof(MatroskaBlock), offsetof(MatroskaCluster,blocks), {.n=matroska_blockgroup} }, |
| 529 | { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE }, |
| 530 | { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE }, |
| 531 | { MATROSKA_ID_INFO, EBML_NONE }, |
| 532 | { MATROSKA_ID_CUES, EBML_NONE }, |
| 533 | { MATROSKA_ID_TAGS, EBML_NONE }, |
| 534 | { MATROSKA_ID_SEEKHEAD, EBML_NONE }, |
| 535 | { MATROSKA_ID_CLUSTER, EBML_STOP }, |
| 536 | { 0 } |
| 537 | }; |
| 538 | |
| 539 | static EbmlSyntax matroska_cluster_incremental[] = { |
| 540 | { MATROSKA_ID_CLUSTERTIMECODE,EBML_UINT,0, offsetof(MatroskaCluster,timecode) }, |
| 541 | { MATROSKA_ID_BLOCKGROUP, EBML_STOP }, |
| 542 | { MATROSKA_ID_SIMPLEBLOCK, EBML_STOP }, |
| 543 | { MATROSKA_ID_CLUSTERPOSITION,EBML_NONE }, |
| 544 | { MATROSKA_ID_CLUSTERPREVSIZE,EBML_NONE }, |
| 545 | { 0 } |
| 546 | }; |
| 547 | |
| 548 | static EbmlSyntax matroska_clusters_incremental[] = { |
| 549 | { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, {.n=matroska_cluster_incremental} }, |
| 550 | { MATROSKA_ID_INFO, EBML_NONE }, |
| 551 | { MATROSKA_ID_CUES, EBML_NONE }, |
| 552 | { MATROSKA_ID_TAGS, EBML_NONE }, |
| 553 | { MATROSKA_ID_SEEKHEAD, EBML_NONE }, |
| 554 | { 0 } |
| 555 | }; |
| 556 | |
Alex Converse | b0f29db | 2012-02-20 08:42:33 | [diff] [blame] | 557 | static const char *const matroska_doctypes[] = { "matroska", "webm" }; |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 558 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 559 | /* |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 560 | * 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] | 561 | */ |
Aurelien Jacobs | 592110c | 2008-08-05 00:42:08 | [diff] [blame] | 562 | static int ebml_level_end(MatroskaDemuxContext *matroska) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 563 | { |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 564 | AVIOContext *pb = matroska->ctx->pb; |
Anton Khirnov | a2704c9 | 2011-03-03 19:11:45 | [diff] [blame] | 565 | int64_t pos = avio_tell(pb); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 566 | |
Aurelien Jacobs | 592110c | 2008-08-05 00:42:08 | [diff] [blame] | 567 | if (matroska->num_levels > 0) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 568 | MatroskaLevel *level = &matroska->levels[matroska->num_levels - 1]; |
Aurelien Jacobs | 8dbe48f | 2010-06-11 16:43:47 | [diff] [blame] | 569 | if (pos - level->start >= level->length || matroska->current_id) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 570 | matroska->num_levels--; |
Aurelien Jacobs | 592110c | 2008-08-05 00:42:08 | [diff] [blame] | 571 | return 1; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 572 | } |
| 573 | } |
Aurelien Jacobs | 592110c | 2008-08-05 00:42:08 | [diff] [blame] | 574 | return 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* |
| 578 | * Read: an "EBML number", which is defined as a variable-length |
| 579 | * array of bytes. The first byte indicates the length by giving a |
| 580 | * number of 0-bits followed by a one. The position of the first |
| 581 | * "one" bit inside the first byte indicates the length of this |
| 582 | * number. |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 583 | * Returns: number of bytes read, < 0 on error |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 584 | */ |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 585 | static int ebml_read_num(MatroskaDemuxContext *matroska, AVIOContext *pb, |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 586 | int max_size, uint64_t *number) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 587 | { |
Reimar Döffinger | ff6a5fc | 2010-09-02 19:17:46 | [diff] [blame] | 588 | int read = 1, n = 1; |
| 589 | uint64_t total = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 590 | |
Anton Khirnov | b7effd4 | 2011-02-21 15:43:01 | [diff] [blame] | 591 | /* The first byte tells us the length in bytes - avio_r8() can normally |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 592 | * return 0, but since that's not a valid first ebmlID byte, we can |
| 593 | * use it safely here to catch EOS. */ |
Anton Khirnov | b7effd4 | 2011-02-21 15:43:01 | [diff] [blame] | 594 | if (!(total = avio_r8(pb))) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 595 | /* we might encounter EOS here */ |
Anton Khirnov | 66e5b1d | 2011-03-07 20:50:25 | [diff] [blame] | 596 | if (!pb->eof_reached) { |
Anton Khirnov | a2704c9 | 2011-03-03 19:11:45 | [diff] [blame] | 597 | int64_t pos = avio_tell(pb); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 598 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 599 | "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", |
| 600 | pos, pos); |
Anton Khirnov | 721113b | 2012-07-21 08:48:39 | [diff] [blame] | 601 | return pb->error ? pb->error : AVERROR(EIO); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 602 | } |
Anton Khirnov | 721113b | 2012-07-21 08:48:39 | [diff] [blame] | 603 | return AVERROR_EOF; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /* get the length of the EBML number */ |
Reimar Döffinger | ff6a5fc | 2010-09-02 19:17:46 | [diff] [blame] | 607 | read = 8 - ff_log2_tab[total]; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 608 | if (read > max_size) { |
Anton Khirnov | a2704c9 | 2011-03-03 19:11:45 | [diff] [blame] | 609 | int64_t pos = avio_tell(pb) - 1; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 610 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 611 | "Invalid EBML number size tag 0x%02x at pos %"PRIu64" (0x%"PRIx64")\n", |
| 612 | (uint8_t) total, pos, pos); |
| 613 | return AVERROR_INVALIDDATA; |
| 614 | } |
| 615 | |
| 616 | /* read out length */ |
Reimar Döffinger | ff6a5fc | 2010-09-02 19:17:46 | [diff] [blame] | 617 | total ^= 1 << ff_log2_tab[total]; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 618 | while (n++ < read) |
Anton Khirnov | b7effd4 | 2011-02-21 15:43:01 | [diff] [blame] | 619 | total = (total << 8) | avio_r8(pb); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 620 | |
| 621 | *number = total; |
| 622 | |
| 623 | return read; |
| 624 | } |
| 625 | |
Reimar Döffinger | 1b4d327 | 2010-09-06 17:51:44 | [diff] [blame] | 626 | /** |
| 627 | * Read a EBML length value. |
| 628 | * This needs special handling for the "unknown length" case which has multiple |
| 629 | * encodings. |
| 630 | */ |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 631 | static int ebml_read_length(MatroskaDemuxContext *matroska, AVIOContext *pb, |
Reimar Döffinger | 1b4d327 | 2010-09-06 17:51:44 | [diff] [blame] | 632 | uint64_t *number) |
| 633 | { |
| 634 | int res = ebml_read_num(matroska, pb, 8, number); |
| 635 | if (res > 0 && *number + 1 == 1ULL << (7 * res)) |
| 636 | *number = 0xffffffffffffffULL; |
| 637 | return res; |
| 638 | } |
| 639 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 640 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 641 | * Read the next element as an unsigned int. |
| 642 | * 0 is success, < 0 is failure. |
| 643 | */ |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 644 | static int ebml_read_uint(AVIOContext *pb, int size, uint64_t *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 645 | { |
Aurelien Jacobs | c6cd2b3 | 2008-08-05 00:41:55 | [diff] [blame] | 646 | int n = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 647 | |
Aurelien Jacobs | 4a194c8 | 2010-09-05 21:37:40 | [diff] [blame] | 648 | if (size > 8) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 649 | return AVERROR_INVALIDDATA; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 650 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 651 | /* big-endian ordering; build up number */ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 652 | *num = 0; |
| 653 | while (n++ < size) |
Anton Khirnov | b7effd4 | 2011-02-21 15:43:01 | [diff] [blame] | 654 | *num = (*num << 8) | avio_r8(pb); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 660 | * Read the next element as a float. |
| 661 | * 0 is success, < 0 is failure. |
| 662 | */ |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 663 | static int ebml_read_float(AVIOContext *pb, int size, double *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 664 | { |
Aurelien Jacobs | 4a194c8 | 2010-09-05 21:37:40 | [diff] [blame] | 665 | if (size == 0) { |
| 666 | *num = 0; |
| 667 | } else if (size == 4) { |
Mans Rullgard | 3383a53 | 2011-11-27 14:04:16 | [diff] [blame] | 668 | *num = av_int2float(avio_rb32(pb)); |
| 669 | } else if (size == 8){ |
| 670 | *num = av_int2double(avio_rb64(pb)); |
Aurelien Jacobs | ba5a1f9 | 2008-08-05 00:41:52 | [diff] [blame] | 671 | } else |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 672 | return AVERROR_INVALIDDATA; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Read the next element as an ASCII string. |
| 679 | * 0 is success, < 0 is failure. |
| 680 | */ |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 681 | static int ebml_read_ascii(AVIOContext *pb, int size, char **str) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 682 | { |
Ronald S. Bultje | cd40c31 | 2012-02-25 00:12:18 | [diff] [blame] | 683 | char *res; |
| 684 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 685 | /* EBML strings are usually not 0-terminated, so we allocate one |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 686 | * byte more, read the string and NULL-terminate it ourselves. */ |
Ronald S. Bultje | cd40c31 | 2012-02-25 00:12:18 | [diff] [blame] | 687 | if (!(res = av_malloc(size + 1))) |
Panagiotis Issaris | 769e10f | 2007-07-19 15:21:30 | [diff] [blame] | 688 | return AVERROR(ENOMEM); |
Ronald S. Bultje | cd40c31 | 2012-02-25 00:12:18 | [diff] [blame] | 689 | if (avio_read(pb, (uint8_t *) res, size) != size) { |
| 690 | av_free(res); |
Panagiotis Issaris | 6f3e0b2 | 2007-07-19 15:23:32 | [diff] [blame] | 691 | return AVERROR(EIO); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 692 | } |
Ronald S. Bultje | cd40c31 | 2012-02-25 00:12:18 | [diff] [blame] | 693 | (res)[size] = '\0'; |
| 694 | av_free(*str); |
| 695 | *str = res; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | /* |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 701 | * Read the next element as binary data. |
| 702 | * 0 is success, < 0 is failure. |
| 703 | */ |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 704 | static int ebml_read_binary(AVIOContext *pb, int length, EbmlBin *bin) |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 705 | { |
| 706 | av_free(bin->data); |
| 707 | if (!(bin->data = av_malloc(length))) |
| 708 | return AVERROR(ENOMEM); |
| 709 | |
| 710 | bin->size = length; |
Anton Khirnov | a2704c9 | 2011-03-03 19:11:45 | [diff] [blame] | 711 | bin->pos = avio_tell(pb); |
Anton Khirnov | b7effd4 | 2011-02-21 15:43:01 | [diff] [blame] | 712 | if (avio_read(pb, bin->data, length) != length) { |
David Conrad | 5549aa6 | 2010-05-18 21:21:37 | [diff] [blame] | 713 | av_freep(&bin->data); |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 714 | return AVERROR(EIO); |
David Conrad | 5549aa6 | 2010-05-18 21:21:37 | [diff] [blame] | 715 | } |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 721 | * Read the next element, but only the header. The contents |
| 722 | * are supposed to be sub-elements which can be read separately. |
| 723 | * 0 is success, < 0 is failure. |
| 724 | */ |
Aurelien Jacobs | bddd1d9 | 2010-06-11 17:16:43 | [diff] [blame] | 725 | static int ebml_read_master(MatroskaDemuxContext *matroska, uint64_t length) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 726 | { |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 727 | AVIOContext *pb = matroska->ctx->pb; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 728 | MatroskaLevel *level; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 729 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 730 | if (matroska->num_levels >= EBML_MAX_DEPTH) { |
| 731 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 732 | "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH); |
Panagiotis Issaris | 85565db | 2007-07-19 15:38:33 | [diff] [blame] | 733 | return AVERROR(ENOSYS); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 734 | } |
| 735 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 736 | level = &matroska->levels[matroska->num_levels++]; |
Anton Khirnov | a2704c9 | 2011-03-03 19:11:45 | [diff] [blame] | 737 | level->start = avio_tell(pb); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 738 | level->length = length; |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 744 | * Read signed/unsigned "EBML" numbers. |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 745 | * Return: number of bytes processed, < 0 on error |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 746 | */ |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 747 | static int matroska_ebmlnum_uint(MatroskaDemuxContext *matroska, |
| 748 | uint8_t *data, uint32_t size, uint64_t *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 749 | { |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 750 | AVIOContext pb; |
Anton Khirnov | e731b8d | 2011-02-20 10:04:13 | [diff] [blame] | 751 | ffio_init_context(&pb, data, size, 0, NULL, NULL, NULL, NULL); |
David Conrad | 465c28b | 2010-05-18 21:21:32 | [diff] [blame] | 752 | return ebml_read_num(matroska, &pb, FFMIN(size, 8), num); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /* |
| 756 | * Same as above, but signed. |
| 757 | */ |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 758 | static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska, |
| 759 | uint8_t *data, uint32_t size, int64_t *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 760 | { |
| 761 | uint64_t unum; |
| 762 | int res; |
| 763 | |
| 764 | /* read as unsigned number first */ |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 765 | if ((res = matroska_ebmlnum_uint(matroska, data, size, &unum)) < 0) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 766 | return res; |
| 767 | |
| 768 | /* make signed (weird way) */ |
Aurelien Jacobs | 33ac07e | 2008-08-05 00:42:55 | [diff] [blame] | 769 | *num = unum - ((1LL << (7*res - 1)) - 1); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 770 | |
| 771 | return res; |
| 772 | } |
| 773 | |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 774 | static int ebml_parse_elem(MatroskaDemuxContext *matroska, |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 775 | EbmlSyntax *syntax, void *data); |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 776 | |
| 777 | static int ebml_parse_id(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, |
| 778 | uint32_t id, void *data) |
| 779 | { |
| 780 | int i; |
| 781 | for (i=0; syntax[i].id; i++) |
| 782 | if (id == syntax[i].id) |
| 783 | break; |
Aurelien Jacobs | 8dbe48f | 2010-06-11 16:43:47 | [diff] [blame] | 784 | if (!syntax[i].id && id == MATROSKA_ID_CLUSTER && |
| 785 | matroska->num_levels > 0 && |
Aurelien Jacobs | bddd1d9 | 2010-06-11 17:16:43 | [diff] [blame] | 786 | matroska->levels[matroska->num_levels-1].length == 0xffffffffffffff) |
Aurelien Jacobs | 8dbe48f | 2010-06-11 16:43:47 | [diff] [blame] | 787 | return 0; // we reached the end of an unknown size cluster |
Anton Khirnov | 5b7a88f | 2012-07-07 15:15:27 | [diff] [blame] | 788 | if (!syntax[i].id && id != EBML_ID_VOID && id != EBML_ID_CRC32) { |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 789 | av_log(matroska->ctx, AV_LOG_INFO, "Unknown entry 0x%X\n", id); |
Anton Khirnov | 5b7a88f | 2012-07-07 15:15:27 | [diff] [blame] | 790 | if (matroska->ctx->error_recognition & AV_EF_EXPLODE) |
| 791 | return AVERROR_INVALIDDATA; |
| 792 | } |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 793 | return ebml_parse_elem(matroska, &syntax[i], data); |
| 794 | } |
| 795 | |
Aurelien Jacobs | 66a37e0 | 2008-08-05 00:42:17 | [diff] [blame] | 796 | static int ebml_parse(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, |
| 797 | void *data) |
| 798 | { |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 799 | if (!matroska->current_id) { |
Aurelien Jacobs | 7391781 | 2010-06-11 16:45:38 | [diff] [blame] | 800 | uint64_t id; |
| 801 | int res = ebml_read_num(matroska, matroska->ctx->pb, 4, &id); |
| 802 | if (res < 0) |
| 803 | return res; |
| 804 | matroska->current_id = id | 1 << 7*res; |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 805 | } |
| 806 | return ebml_parse_id(matroska, syntax, matroska->current_id, data); |
Aurelien Jacobs | 66a37e0 | 2008-08-05 00:42:17 | [diff] [blame] | 807 | } |
| 808 | |
Aurelien Jacobs | 9bcb92c | 2008-08-05 00:42:13 | [diff] [blame] | 809 | static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, |
Aurelien Jacobs | 6314cca | 2008-08-05 00:42:23 | [diff] [blame] | 810 | void *data) |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 811 | { |
Aurelien Jacobs | c005b3f | 2008-08-05 00:42:10 | [diff] [blame] | 812 | int i, res = 0; |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 813 | |
| 814 | for (i=0; syntax[i].id; i++) |
| 815 | switch (syntax[i].type) { |
| 816 | case EBML_UINT: |
| 817 | *(uint64_t *)((char *)data+syntax[i].data_offset) = syntax[i].def.u; |
| 818 | break; |
| 819 | case EBML_FLOAT: |
| 820 | *(double *)((char *)data+syntax[i].data_offset) = syntax[i].def.f; |
| 821 | break; |
| 822 | case EBML_STR: |
| 823 | case EBML_UTF8: |
| 824 | *(char **)((char *)data+syntax[i].data_offset) = av_strdup(syntax[i].def.s); |
| 825 | break; |
| 826 | } |
| 827 | |
Aurelien Jacobs | 6314cca | 2008-08-05 00:42:23 | [diff] [blame] | 828 | while (!res && !ebml_level_end(matroska)) |
Aurelien Jacobs | 66a37e0 | 2008-08-05 00:42:17 | [diff] [blame] | 829 | res = ebml_parse(matroska, syntax, data); |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 830 | |
| 831 | return res; |
| 832 | } |
| 833 | |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 834 | static int ebml_parse_elem(MatroskaDemuxContext *matroska, |
| 835 | EbmlSyntax *syntax, void *data) |
| 836 | { |
Reimar Döffinger | 95ec3d4 | 2011-02-06 10:32:03 | [diff] [blame] | 837 | static const uint64_t max_lengths[EBML_TYPE_COUNT] = { |
| 838 | [EBML_UINT] = 8, |
| 839 | [EBML_FLOAT] = 8, |
| 840 | // max. 16 MB for strings |
| 841 | [EBML_STR] = 0x1000000, |
| 842 | [EBML_UTF8] = 0x1000000, |
| 843 | // max. 256 MB for binary data |
| 844 | [EBML_BIN] = 0x10000000, |
| 845 | // no limits for anything else |
| 846 | }; |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 847 | AVIOContext *pb = matroska->ctx->pb; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 848 | uint32_t id = syntax->id; |
| 849 | uint64_t length; |
| 850 | int res; |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 851 | void *newelem; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 852 | |
| 853 | data = (char *)data + syntax->data_offset; |
| 854 | if (syntax->list_elem_size) { |
| 855 | EbmlList *list = data; |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 856 | newelem = av_realloc(list->elem, (list->nb_elem+1)*syntax->list_elem_size); |
| 857 | if (!newelem) |
| 858 | return AVERROR(ENOMEM); |
| 859 | list->elem = newelem; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 860 | data = (char*)list->elem + list->nb_elem*syntax->list_elem_size; |
| 861 | memset(data, 0, syntax->list_elem_size); |
| 862 | list->nb_elem++; |
| 863 | } |
| 864 | |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 865 | if (syntax->type != EBML_PASS && syntax->type != EBML_STOP) { |
| 866 | matroska->current_id = 0; |
Reimar Döffinger | 1b4d327 | 2010-09-06 17:51:44 | [diff] [blame] | 867 | if ((res = ebml_read_length(matroska, pb, &length)) < 0) |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 868 | return res; |
Reimar Döffinger | 95ec3d4 | 2011-02-06 10:32:03 | [diff] [blame] | 869 | if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) { |
| 870 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 871 | "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for syntax element %i\n", |
| 872 | length, max_lengths[syntax->type], syntax->type); |
| 873 | return AVERROR_INVALIDDATA; |
| 874 | } |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 875 | } |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 876 | |
| 877 | switch (syntax->type) { |
| 878 | case EBML_UINT: res = ebml_read_uint (pb, length, data); break; |
| 879 | case EBML_FLOAT: res = ebml_read_float (pb, length, data); break; |
| 880 | case EBML_STR: |
| 881 | case EBML_UTF8: res = ebml_read_ascii (pb, length, data); break; |
| 882 | case EBML_BIN: res = ebml_read_binary(pb, length, data); break; |
| 883 | case EBML_NEST: if ((res=ebml_read_master(matroska, length)) < 0) |
| 884 | return res; |
| 885 | if (id == MATROSKA_ID_SEGMENT) |
Anton Khirnov | a2704c9 | 2011-03-03 19:11:45 | [diff] [blame] | 886 | matroska->segment_start = avio_tell(matroska->ctx->pb); |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 887 | return ebml_parse_nest(matroska, syntax->def.n, data); |
| 888 | case EBML_PASS: return ebml_parse_id(matroska, syntax->def.n, id, data); |
Aurelien Jacobs | 8070203 | 2010-06-11 16:36:51 | [diff] [blame] | 889 | case EBML_STOP: return 1; |
Anton Khirnov | 45a8a02 | 2011-03-15 08:14:38 | [diff] [blame] | 890 | default: return avio_skip(pb,length)<0 ? AVERROR(EIO) : 0; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 891 | } |
| 892 | if (res == AVERROR_INVALIDDATA) |
| 893 | av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n"); |
| 894 | else if (res == AVERROR(EIO)) |
| 895 | av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n"); |
| 896 | return res; |
| 897 | } |
| 898 | |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 899 | static void ebml_free(EbmlSyntax *syntax, void *data) |
| 900 | { |
| 901 | int i, j; |
| 902 | for (i=0; syntax[i].id; i++) { |
| 903 | void *data_off = (char *)data + syntax[i].data_offset; |
| 904 | switch (syntax[i].type) { |
| 905 | case EBML_STR: |
| 906 | case EBML_UTF8: av_freep(data_off); break; |
| 907 | case EBML_BIN: av_freep(&((EbmlBin *)data_off)->data); break; |
| 908 | case EBML_NEST: |
| 909 | if (syntax[i].list_elem_size) { |
| 910 | EbmlList *list = data_off; |
| 911 | char *ptr = list->elem; |
| 912 | for (j=0; j<list->nb_elem; j++, ptr+=syntax[i].list_elem_size) |
| 913 | ebml_free(syntax[i].def.n, ptr); |
| 914 | av_free(list->elem); |
| 915 | } else |
| 916 | ebml_free(syntax[i].def.n, data_off); |
| 917 | default: break; |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 922 | |
| 923 | /* |
| 924 | * Autodetecting... |
| 925 | */ |
| 926 | static int matroska_probe(AVProbeData *p) |
| 927 | { |
| 928 | uint64_t total = 0; |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 929 | int len_mask = 0x80, size = 1, n = 1, i; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 930 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 931 | /* EBML header? */ |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 932 | if (AV_RB32(p->buf) != EBML_ID_HEADER) |
| 933 | return 0; |
| 934 | |
| 935 | /* length of header */ |
| 936 | total = p->buf[4]; |
| 937 | while (size <= 8 && !(total & len_mask)) { |
| 938 | size++; |
| 939 | len_mask >>= 1; |
| 940 | } |
| 941 | if (size > 8) |
| 942 | return 0; |
| 943 | total &= (len_mask - 1); |
| 944 | while (n < size) |
| 945 | total = (total << 8) | p->buf[4 + n++]; |
| 946 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 947 | /* Does the probe data contain the whole header? */ |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 948 | if (p->buf_size < 4 + size + total) |
| 949 | return 0; |
| 950 | |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 951 | /* The header should contain a known document type. For now, |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 952 | * we don't parse the whole header but simply check for the |
| 953 | * availability of that array of characters inside the header. |
| 954 | * Not fully fool-proof, but good enough. */ |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 955 | for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) { |
| 956 | int probelen = strlen(matroska_doctypes[i]); |
Chris Evans | 69619a1 | 2011-07-20 00:51:48 | [diff] [blame] | 957 | if (total < probelen) |
| 958 | continue; |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 959 | for (n = 4+size; n <= 4+size+total-probelen; n++) |
| 960 | if (!memcmp(p->buf+n, matroska_doctypes[i], probelen)) |
| 961 | return AVPROBE_SCORE_MAX; |
| 962 | } |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 963 | |
David Conrad | c7b913c | 2010-05-22 01:41:35 | [diff] [blame] | 964 | // probably valid EBML header but no recognized doctype |
| 965 | return AVPROBE_SCORE_MAX/2; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska, |
| 969 | int num) |
| 970 | { |
| 971 | MatroskaTrack *tracks = matroska->tracks.elem; |
| 972 | int i; |
| 973 | |
| 974 | for (i=0; i < matroska->tracks.nb_elem; i++) |
| 975 | if (tracks[i].num == num) |
| 976 | return &tracks[i]; |
| 977 | |
| 978 | av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %d\n", num); |
| 979 | return NULL; |
| 980 | } |
| 981 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 982 | static int matroska_decode_buffer(uint8_t** buf, int* buf_size, |
| 983 | MatroskaTrack *track) |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 984 | { |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 985 | MatroskaTrackEncoding *encodings = track->encodings.elem; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 986 | uint8_t* data = *buf; |
| 987 | int isize = *buf_size; |
| 988 | uint8_t* pkt_data = NULL; |
Diego Biurrun | 529506b | 2012-02-12 09:58:46 | [diff] [blame] | 989 | uint8_t av_unused *newpktdata; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 990 | int pkt_size = isize; |
| 991 | int result = 0; |
| 992 | int olen; |
| 993 | |
Aurelien Jacobs | 4f90688 | 2010-08-17 14:05:23 | [diff] [blame] | 994 | if (pkt_size >= 10000000) |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 995 | return AVERROR_INVALIDDATA; |
Aurelien Jacobs | 4f90688 | 2010-08-17 14:05:23 | [diff] [blame] | 996 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 997 | switch (encodings[0].compression.algo) { |
Luca Barbato | 8d4dd55 | 2012-09-14 16:51:49 | [diff] [blame] | 998 | case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP: { |
| 999 | int header_size = encodings[0].compression.settings.size; |
| 1000 | uint8_t *header = encodings[0].compression.settings.data; |
| 1001 | |
| 1002 | if (!header_size) |
| 1003 | return 0; |
| 1004 | |
| 1005 | pkt_size = isize + header_size; |
| 1006 | pkt_data = av_malloc(pkt_size); |
| 1007 | if (!pkt_data) |
| 1008 | return AVERROR(ENOMEM); |
| 1009 | |
| 1010 | memcpy(pkt_data, header, header_size); |
| 1011 | memcpy(pkt_data + header_size, data, isize); |
| 1012 | break; |
| 1013 | } |
Diego Biurrun | 2a91ada | 2012-10-18 17:48:27 | [diff] [blame] | 1014 | #if CONFIG_LZO |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1015 | case MATROSKA_TRACK_ENCODING_COMP_LZO: |
| 1016 | do { |
| 1017 | olen = pkt_size *= 3; |
Luca Barbato | 581281e | 2012-09-14 16:39:58 | [diff] [blame] | 1018 | newpktdata = av_realloc(pkt_data, pkt_size + AV_LZO_OUTPUT_PADDING); |
| 1019 | if (!newpktdata) { |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1020 | result = AVERROR(ENOMEM); |
Luca Barbato | 581281e | 2012-09-14 16:39:58 | [diff] [blame] | 1021 | goto failed; |
| 1022 | } |
| 1023 | pkt_data = newpktdata; |
Reimar Döffinger | 0b178e5 | 2009-02-02 20:16:00 | [diff] [blame] | 1024 | result = av_lzo1x_decode(pkt_data, &olen, data, &isize); |
| 1025 | } while (result==AV_LZO_OUTPUT_FULL && pkt_size<10000000); |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1026 | if (result) { |
| 1027 | result = AVERROR_INVALIDDATA; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1028 | goto failed; |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1029 | } |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1030 | pkt_size -= olen; |
| 1031 | break; |
Diego Biurrun | 2a91ada | 2012-10-18 17:48:27 | [diff] [blame] | 1032 | #endif |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame] | 1033 | #if CONFIG_ZLIB |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1034 | case MATROSKA_TRACK_ENCODING_COMP_ZLIB: { |
| 1035 | z_stream zstream = {0}; |
| 1036 | if (inflateInit(&zstream) != Z_OK) |
| 1037 | return -1; |
| 1038 | zstream.next_in = data; |
| 1039 | zstream.avail_in = isize; |
| 1040 | do { |
| 1041 | pkt_size *= 3; |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1042 | newpktdata = av_realloc(pkt_data, pkt_size); |
| 1043 | if (!newpktdata) { |
| 1044 | inflateEnd(&zstream); |
| 1045 | goto failed; |
| 1046 | } |
| 1047 | pkt_data = newpktdata; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1048 | zstream.avail_out = pkt_size - zstream.total_out; |
| 1049 | zstream.next_out = pkt_data + zstream.total_out; |
| 1050 | result = inflate(&zstream, Z_NO_FLUSH); |
| 1051 | } while (result==Z_OK && pkt_size<10000000); |
| 1052 | pkt_size = zstream.total_out; |
| 1053 | inflateEnd(&zstream); |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1054 | if (result != Z_STREAM_END) { |
| 1055 | if (result == Z_MEM_ERROR) |
| 1056 | result = AVERROR(ENOMEM); |
| 1057 | else |
| 1058 | result = AVERROR_INVALIDDATA; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1059 | goto failed; |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1060 | } |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1061 | break; |
| 1062 | } |
| 1063 | #endif |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame] | 1064 | #if CONFIG_BZLIB |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1065 | case MATROSKA_TRACK_ENCODING_COMP_BZLIB: { |
| 1066 | bz_stream bzstream = {0}; |
| 1067 | if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK) |
| 1068 | return -1; |
| 1069 | bzstream.next_in = data; |
| 1070 | bzstream.avail_in = isize; |
| 1071 | do { |
| 1072 | pkt_size *= 3; |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1073 | newpktdata = av_realloc(pkt_data, pkt_size); |
| 1074 | if (!newpktdata) { |
| 1075 | BZ2_bzDecompressEnd(&bzstream); |
| 1076 | goto failed; |
| 1077 | } |
| 1078 | pkt_data = newpktdata; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1079 | bzstream.avail_out = pkt_size - bzstream.total_out_lo32; |
| 1080 | bzstream.next_out = pkt_data + bzstream.total_out_lo32; |
| 1081 | result = BZ2_bzDecompress(&bzstream); |
| 1082 | } while (result==BZ_OK && pkt_size<10000000); |
| 1083 | pkt_size = bzstream.total_out_lo32; |
| 1084 | BZ2_bzDecompressEnd(&bzstream); |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1085 | if (result != BZ_STREAM_END) { |
| 1086 | if (result == BZ_MEM_ERROR) |
| 1087 | result = AVERROR(ENOMEM); |
| 1088 | else |
| 1089 | result = AVERROR_INVALIDDATA; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1090 | goto failed; |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1091 | } |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1092 | break; |
| 1093 | } |
| 1094 | #endif |
Aurelien Jacobs | 28f27e0 | 2008-08-20 23:08:07 | [diff] [blame] | 1095 | default: |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1096 | return AVERROR_INVALIDDATA; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | *buf = pkt_data; |
| 1100 | *buf_size = pkt_size; |
| 1101 | return 0; |
| 1102 | failed: |
| 1103 | av_free(pkt_data); |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1104 | return result; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1105 | } |
| 1106 | |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 1107 | static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska, |
Aurelien Jacobs | e7d4b74 | 2008-09-28 22:55:28 | [diff] [blame] | 1108 | AVPacket *pkt, uint64_t display_duration) |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 1109 | { |
Anton Khirnov | 1afddbe | 2012-10-31 07:53:18 | [diff] [blame] | 1110 | AVBufferRef *line; |
| 1111 | char *layer, *ptr = pkt->data, *end = ptr+pkt->size; |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 1112 | for (; *ptr!=',' && ptr<end-1; ptr++); |
| 1113 | if (*ptr == ',') |
| 1114 | layer = ++ptr; |
| 1115 | for (; *ptr!=',' && ptr<end-1; ptr++); |
| 1116 | if (*ptr == ',') { |
Aurelien Jacobs | e7d4b74 | 2008-09-28 22:55:28 | [diff] [blame] | 1117 | int64_t end_pts = pkt->pts + display_duration; |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 1118 | int sc = matroska->time_scale * pkt->pts / 10000000; |
| 1119 | int ec = matroska->time_scale * end_pts / 10000000; |
| 1120 | int sh, sm, ss, eh, em, es, len; |
| 1121 | sh = sc/360000; sc -= 360000*sh; |
| 1122 | sm = sc/ 6000; sc -= 6000*sm; |
| 1123 | ss = sc/ 100; sc -= 100*ss; |
| 1124 | eh = ec/360000; ec -= 360000*eh; |
| 1125 | em = ec/ 6000; ec -= 6000*em; |
| 1126 | es = ec/ 100; ec -= 100*es; |
| 1127 | *ptr++ = '\0'; |
| 1128 | len = 50 + end-ptr + FF_INPUT_BUFFER_PADDING_SIZE; |
Anton Khirnov | 1afddbe | 2012-10-31 07:53:18 | [diff] [blame] | 1129 | if (!(line = av_buffer_alloc(len))) |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 1130 | return; |
Anton Khirnov | 1afddbe | 2012-10-31 07:53:18 | [diff] [blame] | 1131 | 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] | 1132 | layer, sh, sm, ss, sc, eh, em, es, ec, ptr); |
Anton Khirnov | 1afddbe | 2012-10-31 07:53:18 | [diff] [blame] | 1133 | av_buffer_unref(&pkt->buf); |
| 1134 | pkt->buf = line; |
| 1135 | pkt->data = line->data; |
| 1136 | pkt->size = strlen(line->data); |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1140 | static int matroska_merge_packets(AVPacket *out, AVPacket *in) |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 1141 | { |
Anton Khirnov | 34871be | 2012-11-01 05:34:41 | [diff] [blame] | 1142 | int old_size = out->size; |
| 1143 | int ret = av_grow_packet(out, in->size); |
| 1144 | if (ret < 0) |
| 1145 | return ret; |
| 1146 | |
| 1147 | memcpy(out->data + old_size, in->data, in->size); |
| 1148 | |
| 1149 | av_free_packet(in); |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 1150 | av_free(in); |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1151 | return 0; |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 1152 | } |
| 1153 | |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1154 | static void matroska_convert_tag(AVFormatContext *s, EbmlList *list, |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1155 | AVDictionary **metadata, char *prefix) |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 1156 | { |
| 1157 | MatroskaTag *tags = list->elem; |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1158 | char key[1024]; |
| 1159 | int i; |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 1160 | |
| 1161 | for (i=0; i < list->nb_elem; i++) { |
Aurelien Jacobs | f702df3 | 2009-02-15 16:05:37 | [diff] [blame] | 1162 | const char *lang = strcmp(tags[i].lang, "und") ? tags[i].lang : NULL; |
Anton Khirnov | bf800c7 | 2010-11-03 06:29:04 | [diff] [blame] | 1163 | |
| 1164 | if (!tags[i].name) { |
| 1165 | av_log(s, AV_LOG_WARNING, "Skipping invalid tag with no TagName.\n"); |
| 1166 | continue; |
| 1167 | } |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1168 | if (prefix) snprintf(key, sizeof(key), "%s/%s", prefix, tags[i].name); |
| 1169 | else av_strlcpy(key, tags[i].name, sizeof(key)); |
Aurelien Jacobs | f702df3 | 2009-02-15 16:05:37 | [diff] [blame] | 1170 | if (tags[i].def || !lang) { |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1171 | av_dict_set(metadata, key, tags[i].string, 0); |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 1172 | if (tags[i].sub.nb_elem) |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1173 | matroska_convert_tag(s, &tags[i].sub, metadata, key); |
Aurelien Jacobs | f702df3 | 2009-02-15 16:05:37 | [diff] [blame] | 1174 | } |
| 1175 | if (lang) { |
| 1176 | av_strlcat(key, "-", sizeof(key)); |
| 1177 | av_strlcat(key, lang, sizeof(key)); |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1178 | av_dict_set(metadata, key, tags[i].string, 0); |
Aurelien Jacobs | f702df3 | 2009-02-15 16:05:37 | [diff] [blame] | 1179 | if (tags[i].sub.nb_elem) |
| 1180 | matroska_convert_tag(s, &tags[i].sub, metadata, key); |
| 1181 | } |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1182 | } |
Anton Khirnov | ad7768f | 2010-10-16 13:20:41 | [diff] [blame] | 1183 | ff_metadata_conv(metadata, NULL, ff_mkv_metadata_conv); |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | static void matroska_convert_tags(AVFormatContext *s) |
| 1187 | { |
| 1188 | MatroskaDemuxContext *matroska = s->priv_data; |
| 1189 | MatroskaTags *tags = matroska->tags.elem; |
| 1190 | int i, j; |
| 1191 | |
| 1192 | for (i=0; i < matroska->tags.nb_elem; i++) { |
| 1193 | if (tags[i].target.attachuid) { |
| 1194 | MatroskaAttachement *attachment = matroska->attachments.elem; |
| 1195 | for (j=0; j<matroska->attachments.nb_elem; j++) |
Aurelien Jacobs | 9c569ef | 2011-03-23 23:28:19 | [diff] [blame] | 1196 | if (attachment[j].uid == tags[i].target.attachuid |
| 1197 | && attachment[j].stream) |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1198 | matroska_convert_tag(s, &tags[i].tag, |
| 1199 | &attachment[j].stream->metadata, NULL); |
| 1200 | } else if (tags[i].target.chapteruid) { |
| 1201 | MatroskaChapter *chapter = matroska->chapters.elem; |
| 1202 | for (j=0; j<matroska->chapters.nb_elem; j++) |
Aurelien Jacobs | 9c569ef | 2011-03-23 23:28:19 | [diff] [blame] | 1203 | if (chapter[j].uid == tags[i].target.chapteruid |
| 1204 | && chapter[j].chapter) |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1205 | matroska_convert_tag(s, &tags[i].tag, |
| 1206 | &chapter[j].chapter->metadata, NULL); |
| 1207 | } else if (tags[i].target.trackuid) { |
| 1208 | MatroskaTrack *track = matroska->tracks.elem; |
| 1209 | for (j=0; j<matroska->tracks.nb_elem; j++) |
Aurelien Jacobs | 9c569ef | 2011-03-23 23:28:19 | [diff] [blame] | 1210 | if (track[j].uid == tags[i].target.trackuid && track[j].stream) |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1211 | matroska_convert_tag(s, &tags[i].tag, |
| 1212 | &track[j].stream->metadata, NULL); |
| 1213 | } else { |
Aurelien Jacobs | 4f909c7 | 2009-06-13 22:29:38 | [diff] [blame] | 1214 | matroska_convert_tag(s, &tags[i].tag, &s->metadata, |
| 1215 | tags[i].target.type); |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1216 | } |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 1217 | } |
| 1218 | } |
| 1219 | |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1220 | static int matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska, int idx) |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 1221 | { |
| 1222 | EbmlList *seekhead_list = &matroska->seekhead; |
| 1223 | MatroskaSeekhead *seekhead = seekhead_list->elem; |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 1224 | uint32_t level_up = matroska->level_up; |
Anton Khirnov | a2704c9 | 2011-03-03 19:11:45 | [diff] [blame] | 1225 | int64_t before_pos = avio_tell(matroska->ctx->pb); |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 1226 | uint32_t saved_id = matroska->current_id; |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 1227 | MatroskaLevel level; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1228 | int64_t offset; |
| 1229 | int ret = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1230 | |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1231 | if (idx >= seekhead_list->nb_elem |
| 1232 | || seekhead[idx].id == MATROSKA_ID_SEEKHEAD |
| 1233 | || seekhead[idx].id == MATROSKA_ID_CLUSTER) |
| 1234 | return 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1235 | |
Anton Khirnov | f47ac3c | 2011-07-09 06:11:30 | [diff] [blame] | 1236 | /* seek */ |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1237 | offset = seekhead[idx].pos + matroska->segment_start; |
| 1238 | if (avio_seek(matroska->ctx->pb, offset, SEEK_SET) == offset) { |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 1239 | /* We don't want to lose our seekhead level, so we add |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1240 | * a dummy. This is a crude hack. */ |
| 1241 | if (matroska->num_levels == EBML_MAX_DEPTH) { |
| 1242 | av_log(matroska->ctx, AV_LOG_INFO, |
| 1243 | "Max EBML element depth (%d) reached, " |
| 1244 | "cannot parse further.\n", EBML_MAX_DEPTH); |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1245 | ret = AVERROR_INVALIDDATA; |
| 1246 | } else { |
Anton Khirnov | f47ac3c | 2011-07-09 06:11:30 | [diff] [blame] | 1247 | level.start = 0; |
| 1248 | level.length = (uint64_t)-1; |
| 1249 | matroska->levels[matroska->num_levels] = level; |
| 1250 | matroska->num_levels++; |
| 1251 | matroska->current_id = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1252 | |
Dustin Brody | 4a9628f | 2011-09-08 22:43:32 | [diff] [blame] | 1253 | ret = ebml_parse(matroska, matroska_segment, matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1254 | |
Anton Khirnov | f47ac3c | 2011-07-09 06:11:30 | [diff] [blame] | 1255 | /* remove dummy level */ |
| 1256 | while (matroska->num_levels) { |
| 1257 | uint64_t length = matroska->levels[--matroska->num_levels].length; |
| 1258 | if (length == (uint64_t)-1) |
| 1259 | break; |
| 1260 | } |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1261 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1262 | } |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1263 | /* seek back */ |
Anton Khirnov | 6b4aa5d | 2011-02-28 13:57:54 | [diff] [blame] | 1264 | avio_seek(matroska->ctx->pb, before_pos, SEEK_SET); |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1265 | matroska->level_up = level_up; |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 1266 | matroska->current_id = saved_id; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1267 | |
| 1268 | return ret; |
| 1269 | } |
| 1270 | |
| 1271 | static void matroska_execute_seekhead(MatroskaDemuxContext *matroska) |
| 1272 | { |
| 1273 | EbmlList *seekhead_list = &matroska->seekhead; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1274 | int64_t before_pos = avio_tell(matroska->ctx->pb); |
| 1275 | int i; |
| 1276 | |
| 1277 | // we should not do any seeking in the streaming case |
| 1278 | if (!matroska->ctx->pb->seekable || |
| 1279 | (matroska->ctx->flags & AVFMT_FLAG_IGNIDX)) |
| 1280 | return; |
| 1281 | |
| 1282 | for (i = 0; i < seekhead_list->nb_elem; i++) { |
Chris Evans | faaec46 | 2012-01-05 20:19:30 | [diff] [blame] | 1283 | MatroskaSeekhead *seekhead = seekhead_list->elem; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1284 | if (seekhead[i].pos <= before_pos) |
| 1285 | continue; |
| 1286 | |
| 1287 | // defer cues parsing until we actually need cue data. |
| 1288 | if (seekhead[i].id == MATROSKA_ID_CUES) { |
| 1289 | matroska->cues_parsing_deferred = 1; |
| 1290 | continue; |
| 1291 | } |
| 1292 | |
| 1293 | if (matroska_parse_seekhead_entry(matroska, i) < 0) |
| 1294 | break; |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | static void matroska_parse_cues(MatroskaDemuxContext *matroska) { |
| 1299 | EbmlList *seekhead_list = &matroska->seekhead; |
| 1300 | MatroskaSeekhead *seekhead = seekhead_list->elem; |
| 1301 | EbmlList *index_list; |
| 1302 | MatroskaIndex *index; |
| 1303 | int index_scale = 1; |
| 1304 | int i, j; |
| 1305 | |
| 1306 | for (i = 0; i < seekhead_list->nb_elem; i++) |
Anton Khirnov | fdb9444 | 2011-07-10 19:48:23 | [diff] [blame] | 1307 | if (seekhead[i].id == MATROSKA_ID_CUES) |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1308 | break; |
| 1309 | assert(i <= seekhead_list->nb_elem); |
| 1310 | |
| 1311 | matroska_parse_seekhead_entry(matroska, i); |
| 1312 | |
| 1313 | index_list = &matroska->index; |
| 1314 | index = index_list->elem; |
| 1315 | if (index_list->nb_elem |
| 1316 | && index[0].time > 1E14/matroska->time_scale) { |
| 1317 | av_log(matroska->ctx, AV_LOG_WARNING, "Working around broken index.\n"); |
| 1318 | index_scale = matroska->time_scale; |
| 1319 | } |
| 1320 | for (i = 0; i < index_list->nb_elem; i++) { |
| 1321 | EbmlList *pos_list = &index[i].pos; |
| 1322 | MatroskaIndexPos *pos = pos_list->elem; |
| 1323 | for (j = 0; j < pos_list->nb_elem; j++) { |
| 1324 | MatroskaTrack *track = matroska_find_track_by_num(matroska, pos[j].track); |
| 1325 | if (track && track->stream) |
| 1326 | av_add_index_entry(track->stream, |
| 1327 | pos[j].pos + matroska->segment_start, |
| 1328 | index[i].time/index_scale, 0, 0, |
| 1329 | AVINDEX_KEYFRAME); |
| 1330 | } |
| 1331 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1332 | } |
| 1333 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 1334 | static int matroska_aac_profile(char *codec_id) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1335 | { |
Aurelien Jacobs | ba18b99 | 2008-08-24 13:12:41 | [diff] [blame] | 1336 | static const char * const aac_profiles[] = { "MAIN", "LC", "SSR" }; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1337 | int profile; |
| 1338 | |
Aurelien Jacobs | 37d3e06 | 2008-10-21 21:40:24 | [diff] [blame] | 1339 | for (profile=0; profile<FF_ARRAY_ELEMS(aac_profiles); profile++) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1340 | if (strstr(codec_id, aac_profiles[profile])) |
| 1341 | break; |
| 1342 | return profile + 1; |
| 1343 | } |
| 1344 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 1345 | static int matroska_aac_sri(int samplerate) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1346 | { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1347 | int sri; |
| 1348 | |
Anton Khirnov | 59a9a23 | 2011-10-17 07:28:53 | [diff] [blame] | 1349 | for (sri=0; sri<FF_ARRAY_ELEMS(avpriv_mpeg4audio_sample_rates); sri++) |
| 1350 | if (avpriv_mpeg4audio_sample_rates[sri] == samplerate) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1351 | break; |
| 1352 | return sri; |
| 1353 | } |
| 1354 | |
Anton Khirnov | 6e9651d | 2012-01-12 12:20:36 | [diff] [blame] | 1355 | static int matroska_read_header(AVFormatContext *s) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1356 | { |
| 1357 | MatroskaDemuxContext *matroska = s->priv_data; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1358 | EbmlList *attachements_list = &matroska->attachments; |
| 1359 | MatroskaAttachement *attachements; |
| 1360 | EbmlList *chapters_list = &matroska->chapters; |
| 1361 | MatroskaChapter *chapters; |
Aurelien Jacobs | 9a9a3b0 | 2008-08-05 00:40:49 | [diff] [blame] | 1362 | MatroskaTrack *tracks; |
Aurelien Jacobs | e0e4be5 | 2009-01-15 00:42:57 | [diff] [blame] | 1363 | uint64_t max_start = 0; |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 1364 | Ebml ebml = { 0 }; |
Aurelien Jacobs | 9a9a3b0 | 2008-08-05 00:40:49 | [diff] [blame] | 1365 | AVStream *st; |
Aurelien Jacobs | 0ade7bb | 2010-06-15 19:53:15 | [diff] [blame] | 1366 | int i, j, res; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1367 | |
| 1368 | matroska->ctx = s; |
| 1369 | |
| 1370 | /* First read the EBML header. */ |
Aurelien Jacobs | c4d3d9b | 2008-08-05 00:42:20 | [diff] [blame] | 1371 | if (ebml_parse(matroska, ebml_syntax, &ebml) |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 1372 | || ebml.version > EBML_VERSION || ebml.max_size > sizeof(uint64_t) |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 1373 | || ebml.id_length > sizeof(uint32_t) || ebml.doctype_version > 2) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1374 | av_log(matroska->ctx, AV_LOG_ERROR, |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 1375 | "EBML header using unsupported features\n" |
| 1376 | "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n", |
| 1377 | ebml.version, ebml.doctype, ebml.doctype_version); |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 1378 | ebml_free(ebml_syntax, &ebml); |
Aurelien Jacobs | e536ccd | 2010-03-15 00:19:22 | [diff] [blame] | 1379 | return AVERROR_PATCHWELCOME; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1380 | } |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 1381 | for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) |
| 1382 | if (!strcmp(ebml.doctype, matroska_doctypes[i])) |
| 1383 | break; |
| 1384 | if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) { |
David Conrad | c7b913c | 2010-05-22 01:41:35 | [diff] [blame] | 1385 | av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype); |
Luca Barbato | cd4739c | 2012-07-12 11:38:50 | [diff] [blame] | 1386 | if (matroska->ctx->error_recognition & AV_EF_EXPLODE) { |
| 1387 | ebml_free(ebml_syntax, &ebml); |
| 1388 | return AVERROR_INVALIDDATA; |
| 1389 | } |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 1390 | } |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 1391 | ebml_free(ebml_syntax, &ebml); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1392 | |
| 1393 | /* The next thing is a segment. */ |
Aurelien Jacobs | 0ade7bb | 2010-06-15 19:53:15 | [diff] [blame] | 1394 | if ((res = ebml_parse(matroska, matroska_segments, matroska)) < 0) |
| 1395 | return res; |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 1396 | matroska_execute_seekhead(matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1397 | |
David Conrad | d98bd80 | 2010-05-18 21:21:28 | [diff] [blame] | 1398 | if (!matroska->time_scale) |
| 1399 | matroska->time_scale = 1000000; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1400 | if (matroska->duration) |
| 1401 | matroska->ctx->duration = matroska->duration * matroska->time_scale |
| 1402 | * 1000 / AV_TIME_BASE; |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1403 | av_dict_set(&s->metadata, "title", matroska->title, 0); |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1404 | |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1405 | tracks = matroska->tracks.elem; |
| 1406 | for (i=0; i < matroska->tracks.nb_elem; i++) { |
| 1407 | MatroskaTrack *track = &tracks[i]; |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1408 | enum AVCodecID codec_id = AV_CODEC_ID_NONE; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1409 | EbmlList *encodings_list = &tracks->encodings; |
| 1410 | MatroskaTrackEncoding *encodings = encodings_list->elem; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1411 | uint8_t *extradata = NULL; |
| 1412 | int extradata_size = 0; |
| 1413 | int extradata_offset = 0; |
Anton Khirnov | ae628ec | 2011-02-20 10:04:12 | [diff] [blame] | 1414 | AVIOContext b; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1415 | |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1416 | /* Apply some sanity checks. */ |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1417 | if (track->type != MATROSKA_TRACK_TYPE_VIDEO && |
| 1418 | track->type != MATROSKA_TRACK_TYPE_AUDIO && |
| 1419 | track->type != MATROSKA_TRACK_TYPE_SUBTITLE) { |
| 1420 | av_log(matroska->ctx, AV_LOG_INFO, |
| 1421 | "Unknown or unsupported track type %"PRIu64"\n", |
| 1422 | track->type); |
| 1423 | continue; |
| 1424 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1425 | if (track->codec_id == NULL) |
| 1426 | continue; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1427 | |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1428 | if (track->type == MATROSKA_TRACK_TYPE_VIDEO) { |
Mans Rullgard | 3c58300 | 2012-04-18 12:48:20 | [diff] [blame] | 1429 | if (!track->default_duration && track->video.frame_rate > 0) |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1430 | track->default_duration = 1000000000/track->video.frame_rate; |
| 1431 | if (!track->video.display_width) |
| 1432 | track->video.display_width = track->video.pixel_width; |
| 1433 | if (!track->video.display_height) |
| 1434 | track->video.display_height = track->video.pixel_height; |
| 1435 | } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { |
| 1436 | if (!track->audio.out_samplerate) |
| 1437 | track->audio.out_samplerate = track->audio.samplerate; |
| 1438 | } |
| 1439 | if (encodings_list->nb_elem > 1) { |
| 1440 | av_log(matroska->ctx, AV_LOG_ERROR, |
Dustin Brody | d7d2f0e | 2011-09-15 07:34:38 | [diff] [blame] | 1441 | "Multiple combined encodings not supported"); |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1442 | } else if (encodings_list->nb_elem == 1) { |
| 1443 | if (encodings[0].type || |
Diego Biurrun | 2a91ada | 2012-10-18 17:48:27 | [diff] [blame] | 1444 | ( |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame] | 1445 | #if CONFIG_ZLIB |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1446 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB && |
| 1447 | #endif |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame] | 1448 | #if CONFIG_BZLIB |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1449 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB && |
| 1450 | #endif |
Diego Biurrun | 2a91ada | 2012-10-18 17:48:27 | [diff] [blame] | 1451 | #if CONFIG_LZO |
| 1452 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO && |
| 1453 | #endif |
| 1454 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP)) { |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1455 | encodings[0].scope = 0; |
| 1456 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1457 | "Unsupported encoding type"); |
| 1458 | } else if (track->codec_priv.size && encodings[0].scope&2) { |
| 1459 | uint8_t *codec_priv = track->codec_priv.data; |
Luca Barbato | 8d4dd55 | 2012-09-14 16:51:49 | [diff] [blame] | 1460 | int ret = matroska_decode_buffer(&track->codec_priv.data, |
| 1461 | &track->codec_priv.size, |
| 1462 | track); |
| 1463 | if (ret < 0) { |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1464 | track->codec_priv.data = NULL; |
| 1465 | track->codec_priv.size = 0; |
| 1466 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1467 | "Failed to decode codec private data\n"); |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1468 | } |
Luca Barbato | 8d4dd55 | 2012-09-14 16:51:49 | [diff] [blame] | 1469 | |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1470 | if (codec_priv != track->codec_priv.data) |
| 1471 | av_free(codec_priv); |
| 1472 | } |
| 1473 | } |
| 1474 | |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1475 | 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] | 1476 | if(!strncmp(ff_mkv_codec_tags[j].str, track->codec_id, |
| 1477 | strlen(ff_mkv_codec_tags[j].str))){ |
| 1478 | codec_id= ff_mkv_codec_tags[j].id; |
| 1479 | break; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1480 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1481 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1482 | |
Anton Khirnov | 3b3bbdd | 2011-06-18 09:43:24 | [diff] [blame] | 1483 | st = track->stream = avformat_new_stream(s, NULL); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1484 | if (st == NULL) |
| 1485 | return AVERROR(ENOMEM); |
| 1486 | |
Aurelien Jacobs | cc8be50 | 2008-08-05 00:42:49 | [diff] [blame] | 1487 | if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC") |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1488 | && track->codec_priv.size >= 40 |
| 1489 | && track->codec_priv.data != NULL) { |
Joakim Plate | 3e93c8e | 2010-03-03 21:46:43 | [diff] [blame] | 1490 | track->ms_compat = 1; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1491 | track->video.fourcc = AV_RL32(track->codec_priv.data + 16); |
Daniel Verkamp | 1a40491 | 2009-06-22 23:09:34 | [diff] [blame] | 1492 | codec_id = ff_codec_get_id(ff_codec_bmp_tags, track->video.fourcc); |
Aurelien Jacobs | 429eeec | 2009-08-24 13:43:33 | [diff] [blame] | 1493 | extradata_offset = 40; |
Aurelien Jacobs | cc8be50 | 2008-08-05 00:42:49 | [diff] [blame] | 1494 | } else if (!strcmp(track->codec_id, "A_MS/ACM") |
Aurelien Jacobs | 038146e | 2009-10-01 21:14:05 | [diff] [blame] | 1495 | && track->codec_priv.size >= 14 |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1496 | && track->codec_priv.data != NULL) { |
Max Horn | ca402f3 | 2011-04-12 15:44:20 | [diff] [blame] | 1497 | int ret; |
Anton Khirnov | e731b8d | 2011-02-20 10:04:13 | [diff] [blame] | 1498 | ffio_init_context(&b, track->codec_priv.data, track->codec_priv.size, |
Luca Barbato | 37cb3b1 | 2013-03-12 17:56:28 | [diff] [blame] | 1499 | 0, NULL, NULL, NULL, NULL); |
Max Horn | ca402f3 | 2011-04-12 15:44:20 | [diff] [blame] | 1500 | ret = ff_get_wav_header(&b, st->codec, track->codec_priv.size); |
| 1501 | if (ret < 0) |
| 1502 | return ret; |
Aurelien Jacobs | 68a7145 | 2009-01-18 17:13:12 | [diff] [blame] | 1503 | codec_id = st->codec->codec_id; |
Aurelien Jacobs | 038146e | 2009-10-01 21:14:05 | [diff] [blame] | 1504 | extradata_offset = FFMIN(track->codec_priv.size, 18); |
Aurelien Jacobs | 28ba69e | 2008-08-05 00:41:31 | [diff] [blame] | 1505 | } else if (!strcmp(track->codec_id, "V_QUICKTIME") |
| 1506 | && (track->codec_priv.size >= 86) |
| 1507 | && (track->codec_priv.data != NULL)) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1508 | track->video.fourcc = AV_RL32(track->codec_priv.data); |
Martin Storsjö | 167f3b8 | 2012-02-15 09:28:39 | [diff] [blame] | 1509 | codec_id=ff_codec_get_id(ff_codec_movvideo_tags, track->video.fourcc); |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1510 | } else if (codec_id == AV_CODEC_ID_PCM_S16BE) { |
Aurelien Jacobs | eb9cf50 | 2008-08-20 00:49:45 | [diff] [blame] | 1511 | switch (track->audio.bitdepth) { |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1512 | case 8: codec_id = AV_CODEC_ID_PCM_U8; break; |
| 1513 | case 24: codec_id = AV_CODEC_ID_PCM_S24BE; break; |
| 1514 | case 32: codec_id = AV_CODEC_ID_PCM_S32BE; break; |
Aurelien Jacobs | eb9cf50 | 2008-08-20 00:49:45 | [diff] [blame] | 1515 | } |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1516 | } else if (codec_id == AV_CODEC_ID_PCM_S16LE) { |
Aurelien Jacobs | eb9cf50 | 2008-08-20 00:49:45 | [diff] [blame] | 1517 | switch (track->audio.bitdepth) { |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1518 | case 8: codec_id = AV_CODEC_ID_PCM_U8; break; |
| 1519 | case 24: codec_id = AV_CODEC_ID_PCM_S24LE; break; |
| 1520 | case 32: codec_id = AV_CODEC_ID_PCM_S32LE; break; |
Aurelien Jacobs | eb9cf50 | 2008-08-20 00:49:45 | [diff] [blame] | 1521 | } |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1522 | } else if (codec_id==AV_CODEC_ID_PCM_F32LE && track->audio.bitdepth==64) { |
| 1523 | codec_id = AV_CODEC_ID_PCM_F64LE; |
| 1524 | } else if (codec_id == AV_CODEC_ID_AAC && !track->codec_priv.size) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1525 | int profile = matroska_aac_profile(track->codec_id); |
| 1526 | int sri = matroska_aac_sri(track->audio.samplerate); |
Alex Converse | d2ee8c1 | 2012-01-25 22:34:21 | [diff] [blame] | 1527 | extradata = av_mallocz(5 + FF_INPUT_BUFFER_PADDING_SIZE); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1528 | if (extradata == NULL) |
Aurelien Jacobs | 28f450a | 2008-08-05 00:40:09 | [diff] [blame] | 1529 | return AVERROR(ENOMEM); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1530 | extradata[0] = (profile << 3) | ((sri&0x0E) >> 1); |
| 1531 | extradata[1] = ((sri&0x01) << 7) | (track->audio.channels<<3); |
| 1532 | if (strstr(track->codec_id, "SBR")) { |
| 1533 | sri = matroska_aac_sri(track->audio.out_samplerate); |
| 1534 | extradata[2] = 0x56; |
| 1535 | extradata[3] = 0xE5; |
| 1536 | extradata[4] = 0x80 | (sri<<3); |
| 1537 | extradata_size = 5; |
Aurelien Jacobs | 16f97ab | 2008-08-05 00:41:10 | [diff] [blame] | 1538 | } else |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1539 | extradata_size = 2; |
Moritz Bunkus | 8071dca | 2012-09-14 20:26:14 | [diff] [blame] | 1540 | } else if (codec_id == AV_CODEC_ID_ALAC && track->codec_priv.size) { |
| 1541 | /* Only ALAC's magic cookie is stored in Matroska's track headers. |
| 1542 | Create the "atom size", "tag", and "tag version" fields the |
| 1543 | decoder expects manually. */ |
| 1544 | extradata_size = 12 + track->codec_priv.size; |
| 1545 | extradata = av_mallocz(extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); |
| 1546 | if (extradata == NULL) |
| 1547 | return AVERROR(ENOMEM); |
| 1548 | AV_WB32(extradata, extradata_size); |
| 1549 | memcpy(&extradata[4], "alac", 4); |
| 1550 | AV_WB32(&extradata[8], 0); |
| 1551 | memcpy(&extradata[12], track->codec_priv.data, |
| 1552 | track->codec_priv.size); |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1553 | } else if (codec_id == AV_CODEC_ID_TTA) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1554 | extradata_size = 30; |
| 1555 | extradata = av_mallocz(extradata_size); |
| 1556 | if (extradata == NULL) |
| 1557 | return AVERROR(ENOMEM); |
Anton Khirnov | e731b8d | 2011-02-20 10:04:13 | [diff] [blame] | 1558 | ffio_init_context(&b, extradata, extradata_size, 1, |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1559 | NULL, NULL, NULL, NULL); |
Anton Khirnov | 77eb550 | 2011-02-21 18:28:17 | [diff] [blame] | 1560 | avio_write(&b, "TTA1", 4); |
| 1561 | avio_wl16(&b, 1); |
| 1562 | avio_wl16(&b, track->audio.channels); |
| 1563 | avio_wl16(&b, track->audio.bitdepth); |
| 1564 | avio_wl32(&b, track->audio.out_samplerate); |
| 1565 | avio_wl32(&b, matroska->ctx->duration * track->audio.out_samplerate); |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1566 | } else if (codec_id == AV_CODEC_ID_RV10 || codec_id == AV_CODEC_ID_RV20 || |
| 1567 | codec_id == AV_CODEC_ID_RV30 || codec_id == AV_CODEC_ID_RV40) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1568 | extradata_offset = 26; |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1569 | } else if (codec_id == AV_CODEC_ID_RA_144) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1570 | track->audio.out_samplerate = 8000; |
| 1571 | track->audio.channels = 1; |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1572 | } else if (codec_id == AV_CODEC_ID_RA_288 || codec_id == AV_CODEC_ID_COOK || |
| 1573 | codec_id == AV_CODEC_ID_ATRAC3 || codec_id == AV_CODEC_ID_SIPR) { |
Aurelien Jacobs | 6b10228 | 2010-03-12 23:49:06 | [diff] [blame] | 1574 | int flavor; |
Anton Khirnov | e731b8d | 2011-02-20 10:04:13 | [diff] [blame] | 1575 | ffio_init_context(&b, track->codec_priv.data,track->codec_priv.size, |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1576 | 0, NULL, NULL, NULL, NULL); |
Anton Khirnov | 45a8a02 | 2011-03-15 08:14:38 | [diff] [blame] | 1577 | avio_skip(&b, 22); |
Anton Khirnov | b7effd4 | 2011-02-21 15:43:01 | [diff] [blame] | 1578 | flavor = avio_rb16(&b); |
| 1579 | track->audio.coded_framesize = avio_rb32(&b); |
Anton Khirnov | 45a8a02 | 2011-03-15 08:14:38 | [diff] [blame] | 1580 | avio_skip(&b, 12); |
Anton Khirnov | b7effd4 | 2011-02-21 15:43:01 | [diff] [blame] | 1581 | track->audio.sub_packet_h = avio_rb16(&b); |
| 1582 | track->audio.frame_size = avio_rb16(&b); |
| 1583 | track->audio.sub_packet_size = avio_rb16(&b); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1584 | 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] | 1585 | if (codec_id == AV_CODEC_ID_RA_288) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1586 | st->codec->block_align = track->audio.coded_framesize; |
| 1587 | track->codec_priv.size = 0; |
| 1588 | } else { |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1589 | if (codec_id == AV_CODEC_ID_SIPR && flavor < 4) { |
Aurelien Jacobs | 6b10228 | 2010-03-12 23:49:06 | [diff] [blame] | 1590 | const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 }; |
| 1591 | track->audio.sub_packet_size = ff_sipr_subpk_size[flavor]; |
| 1592 | st->codec->bit_rate = sipr_bit_rate[flavor]; |
| 1593 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1594 | st->codec->block_align = track->audio.sub_packet_size; |
| 1595 | extradata_offset = 78; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1596 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1597 | } |
Aurelien Jacobs | e264440 | 2009-08-24 13:40:30 | [diff] [blame] | 1598 | track->codec_priv.size -= extradata_offset; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1599 | |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1600 | if (codec_id == AV_CODEC_ID_NONE) |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1601 | av_log(matroska->ctx, AV_LOG_INFO, |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1602 | "Unknown/unsupported AVCodecID %s.\n", track->codec_id); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1603 | |
Aurelien Jacobs | 3fc9d7c | 2008-09-09 11:23:48 | [diff] [blame] | 1604 | if (track->time_scale < 0.01) |
| 1605 | track->time_scale = 1.0; |
Anton Khirnov | c3f9ebf | 2011-11-29 18:28:15 | [diff] [blame] | 1606 | 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] | 1607 | |
| 1608 | st->codec->codec_id = codec_id; |
| 1609 | st->start_time = 0; |
| 1610 | if (strcmp(track->language, "und")) |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1611 | av_dict_set(&st->metadata, "language", track->language, 0); |
| 1612 | av_dict_set(&st->metadata, "title", track->name, 0); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1613 | |
| 1614 | if (track->flag_default) |
| 1615 | st->disposition |= AV_DISPOSITION_DEFAULT; |
Aurelien Jacobs | 7a617a8 | 2010-07-02 16:38:44 | [diff] [blame] | 1616 | if (track->flag_forced) |
| 1617 | st->disposition |= AV_DISPOSITION_FORCED; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1618 | |
Aurelien Jacobs | ff0d5a7 | 2009-10-01 21:14:46 | [diff] [blame] | 1619 | if (!st->codec->extradata) { |
Aurelien Jacobs | 553e9f7 | 2009-10-01 21:15:36 | [diff] [blame] | 1620 | if(extradata){ |
| 1621 | st->codec->extradata = extradata; |
| 1622 | st->codec->extradata_size = extradata_size; |
| 1623 | } else if(track->codec_priv.data && track->codec_priv.size > 0){ |
| 1624 | st->codec->extradata = av_mallocz(track->codec_priv.size + |
| 1625 | FF_INPUT_BUFFER_PADDING_SIZE); |
| 1626 | if(st->codec->extradata == NULL) |
| 1627 | return AVERROR(ENOMEM); |
| 1628 | st->codec->extradata_size = track->codec_priv.size; |
| 1629 | memcpy(st->codec->extradata, |
| 1630 | track->codec_priv.data + extradata_offset, |
| 1631 | track->codec_priv.size); |
| 1632 | } |
Aurelien Jacobs | ff0d5a7 | 2009-10-01 21:14:46 | [diff] [blame] | 1633 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1634 | |
| 1635 | if (track->type == MATROSKA_TRACK_TYPE_VIDEO) { |
Stefano Sabatini | 72415b2 | 2010-03-30 23:30:55 | [diff] [blame] | 1636 | st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1637 | st->codec->codec_tag = track->video.fourcc; |
| 1638 | st->codec->width = track->video.pixel_width; |
| 1639 | st->codec->height = track->video.pixel_height; |
Aurelien Jacobs | 5972945 | 2008-08-23 23:43:20 | [diff] [blame] | 1640 | av_reduce(&st->sample_aspect_ratio.num, |
| 1641 | &st->sample_aspect_ratio.den, |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1642 | st->codec->height * track->video.display_width, |
| 1643 | st->codec-> width * track->video.display_height, |
| 1644 | 255); |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1645 | if (st->codec->codec_id != AV_CODEC_ID_H264) |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1646 | st->need_parsing = AVSTREAM_PARSE_HEADERS; |
Luca Barbato | ac97d47 | 2012-04-17 23:32:07 | [diff] [blame] | 1647 | if (track->default_duration) { |
Anton Khirnov | aba232c | 2012-06-26 11:10:01 | [diff] [blame] | 1648 | av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den, |
Luca Barbato | ac97d47 | 2012-04-17 23:32:07 | [diff] [blame] | 1649 | 1000000000, track->default_duration, 30000); |
Luca Barbato | ac97d47 | 2012-04-17 23:32:07 | [diff] [blame] | 1650 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1651 | } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { |
Stefano Sabatini | 72415b2 | 2010-03-30 23:30:55 | [diff] [blame] | 1652 | st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1653 | st->codec->sample_rate = track->audio.out_samplerate; |
| 1654 | st->codec->channels = track->audio.channels; |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1655 | if (st->codec->codec_id != AV_CODEC_ID_AAC) |
Baptiste Coudurier | dc1c26d | 2010-04-20 21:19:27 | [diff] [blame] | 1656 | st->need_parsing = AVSTREAM_PARSE_HEADERS; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1657 | } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) { |
Stefano Sabatini | 72415b2 | 2010-03-30 23:30:55 | [diff] [blame] | 1658 | st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1659 | if (st->codec->codec_id == AV_CODEC_ID_SSA) |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 1660 | matroska->contains_ssa = 1; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 1661 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1662 | } |
| 1663 | |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1664 | attachements = attachements_list->elem; |
| 1665 | for (j=0; j<attachements_list->nb_elem; j++) { |
| 1666 | if (!(attachements[j].filename && attachements[j].mime && |
| 1667 | attachements[j].bin.data && attachements[j].bin.size > 0)) { |
| 1668 | av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n"); |
| 1669 | } else { |
Anton Khirnov | 3b3bbdd | 2011-06-18 09:43:24 | [diff] [blame] | 1670 | AVStream *st = avformat_new_stream(s, NULL); |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1671 | if (st == NULL) |
| 1672 | break; |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1673 | av_dict_set(&st->metadata, "filename",attachements[j].filename, 0); |
Anton Khirnov | 98cfe22 | 2011-09-27 08:36:53 | [diff] [blame] | 1674 | av_dict_set(&st->metadata, "mimetype", attachements[j].mime, 0); |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1675 | st->codec->codec_id = AV_CODEC_ID_NONE; |
Stefano Sabatini | 72415b2 | 2010-03-30 23:30:55 | [diff] [blame] | 1676 | st->codec->codec_type = AVMEDIA_TYPE_ATTACHMENT; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1677 | st->codec->extradata = av_malloc(attachements[j].bin.size); |
| 1678 | if(st->codec->extradata == NULL) |
| 1679 | break; |
| 1680 | st->codec->extradata_size = attachements[j].bin.size; |
| 1681 | memcpy(st->codec->extradata, attachements[j].bin.data, attachements[j].bin.size); |
| 1682 | |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 1683 | 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] | 1684 | if (!strncmp(ff_mkv_mime_tags[i].str, attachements[j].mime, |
| 1685 | strlen(ff_mkv_mime_tags[i].str))) { |
| 1686 | st->codec->codec_id = ff_mkv_mime_tags[i].id; |
| 1687 | break; |
| 1688 | } |
| 1689 | } |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1690 | attachements[j].stream = st; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | chapters = chapters_list->elem; |
| 1695 | for (i=0; i<chapters_list->nb_elem; i++) |
Aurelien Jacobs | e0e4be5 | 2009-01-15 00:42:57 | [diff] [blame] | 1696 | if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid |
| 1697 | && (max_start==0 || chapters[i].start > max_start)) { |
Aurelien Jacobs | 6cb6e15 | 2009-02-15 15:25:14 | [diff] [blame] | 1698 | chapters[i].chapter = |
Anton Khirnov | 1fa395e4 | 2011-10-17 06:58:50 | [diff] [blame] | 1699 | avpriv_new_chapter(s, chapters[i].uid, (AVRational){1, 1000000000}, |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1700 | chapters[i].start, chapters[i].end, |
| 1701 | chapters[i].title); |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1702 | av_dict_set(&chapters[i].chapter->metadata, |
Stefano Sabatini | 2ef6c12 | 2010-04-25 14:27:42 | [diff] [blame] | 1703 | "title", chapters[i].title, 0); |
Aurelien Jacobs | e0e4be5 | 2009-01-15 00:42:57 | [diff] [blame] | 1704 | max_start = chapters[i].start; |
| 1705 | } |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 1706 | |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1707 | matroska_convert_tags(s); |
| 1708 | |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 1709 | return 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1710 | } |
| 1711 | |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1712 | /* |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1713 | * Put one packet in an application-supplied AVPacket struct. |
| 1714 | * Returns 0 on success or -1 on failure. |
| 1715 | */ |
| 1716 | static int matroska_deliver_packet(MatroskaDemuxContext *matroska, |
| 1717 | AVPacket *pkt) |
| 1718 | { |
| 1719 | if (matroska->num_packets > 0) { |
| 1720 | memcpy(pkt, matroska->packets[0], sizeof(AVPacket)); |
| 1721 | av_free(matroska->packets[0]); |
| 1722 | if (matroska->num_packets > 1) { |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1723 | void *newpackets; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1724 | memmove(&matroska->packets[0], &matroska->packets[1], |
| 1725 | (matroska->num_packets - 1) * sizeof(AVPacket *)); |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1726 | newpackets = av_realloc(matroska->packets, |
| 1727 | (matroska->num_packets - 1) * sizeof(AVPacket *)); |
| 1728 | if (newpackets) |
| 1729 | matroska->packets = newpackets; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1730 | } else { |
| 1731 | av_freep(&matroska->packets); |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 1732 | matroska->prev_pkt = NULL; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1733 | } |
| 1734 | matroska->num_packets--; |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
| 1738 | return -1; |
| 1739 | } |
| 1740 | |
| 1741 | /* |
| 1742 | * Free all packets in our internal queue. |
| 1743 | */ |
| 1744 | static void matroska_clear_queue(MatroskaDemuxContext *matroska) |
| 1745 | { |
Dale Curtis | ae3d416 | 2013-01-10 19:05:29 | [diff] [blame] | 1746 | matroska->prev_pkt = NULL; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1747 | if (matroska->packets) { |
| 1748 | int n; |
| 1749 | for (n = 0; n < matroska->num_packets; n++) { |
| 1750 | av_free_packet(matroska->packets[n]); |
| 1751 | av_free(matroska->packets[n]); |
| 1752 | } |
Aurelien Jacobs | 00a3431 | 2008-08-06 00:21:10 | [diff] [blame] | 1753 | av_freep(&matroska->packets); |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1754 | matroska->num_packets = 0; |
| 1755 | } |
| 1756 | } |
| 1757 | |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 1758 | static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf, |
Dale Curtis | fc43c19 | 2013-03-27 21:02:03 | [diff] [blame^] | 1759 | int* buf_size, int type, |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 1760 | uint32_t **lace_buf, int *laces) |
| 1761 | { |
Dale Curtis | fc43c19 | 2013-03-27 21:02:03 | [diff] [blame^] | 1762 | int res = 0, n, size = *buf_size; |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 1763 | uint8_t *data = *buf; |
| 1764 | uint32_t *lace_size; |
| 1765 | |
| 1766 | if (!type) { |
| 1767 | *laces = 1; |
| 1768 | *lace_buf = av_mallocz(sizeof(int)); |
| 1769 | if (!*lace_buf) |
| 1770 | return AVERROR(ENOMEM); |
| 1771 | |
| 1772 | *lace_buf[0] = size; |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
| 1776 | assert(size > 0); |
| 1777 | *laces = *data + 1; |
| 1778 | data += 1; |
| 1779 | size -= 1; |
| 1780 | lace_size = av_mallocz(*laces * sizeof(int)); |
| 1781 | if (!lace_size) |
| 1782 | return AVERROR(ENOMEM); |
| 1783 | |
| 1784 | switch (type) { |
| 1785 | case 0x1: /* Xiph lacing */ { |
| 1786 | uint8_t temp; |
| 1787 | uint32_t total = 0; |
| 1788 | for (n = 0; res == 0 && n < *laces - 1; n++) { |
| 1789 | while (1) { |
| 1790 | if (size == 0) { |
| 1791 | res = AVERROR_EOF; |
| 1792 | break; |
| 1793 | } |
| 1794 | temp = *data; |
| 1795 | lace_size[n] += temp; |
| 1796 | data += 1; |
| 1797 | size -= 1; |
| 1798 | if (temp != 0xff) |
| 1799 | break; |
| 1800 | } |
| 1801 | total += lace_size[n]; |
| 1802 | } |
| 1803 | if (size <= total) { |
| 1804 | res = AVERROR_INVALIDDATA; |
| 1805 | break; |
| 1806 | } |
| 1807 | |
| 1808 | lace_size[n] = size - total; |
| 1809 | break; |
| 1810 | } |
| 1811 | |
| 1812 | case 0x2: /* fixed-size lacing */ |
Anton Khirnov | 87b017a | 2012-09-20 18:04:56 | [diff] [blame] | 1813 | if (size % (*laces)) { |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 1814 | res = AVERROR_INVALIDDATA; |
| 1815 | break; |
| 1816 | } |
| 1817 | for (n = 0; n < *laces; n++) |
| 1818 | lace_size[n] = size / *laces; |
| 1819 | break; |
| 1820 | |
| 1821 | case 0x3: /* EBML lacing */ { |
| 1822 | uint64_t num; |
| 1823 | uint32_t total; |
| 1824 | n = matroska_ebmlnum_uint(matroska, data, size, &num); |
| 1825 | if (n < 0) { |
| 1826 | av_log(matroska->ctx, AV_LOG_INFO, |
| 1827 | "EBML block data error\n"); |
| 1828 | res = n; |
| 1829 | break; |
| 1830 | } |
| 1831 | data += n; |
| 1832 | size -= n; |
| 1833 | total = lace_size[0] = num; |
| 1834 | for (n = 1; res == 0 && n < *laces - 1; n++) { |
| 1835 | int64_t snum; |
| 1836 | int r; |
| 1837 | r = matroska_ebmlnum_sint(matroska, data, size, &snum); |
| 1838 | if (r < 0) { |
| 1839 | av_log(matroska->ctx, AV_LOG_INFO, |
| 1840 | "EBML block data error\n"); |
| 1841 | res = r; |
| 1842 | break; |
| 1843 | } |
| 1844 | data += r; |
| 1845 | size -= r; |
| 1846 | lace_size[n] = lace_size[n - 1] + snum; |
| 1847 | total += lace_size[n]; |
| 1848 | } |
| 1849 | if (size <= total) { |
| 1850 | res = AVERROR_INVALIDDATA; |
| 1851 | break; |
| 1852 | } |
| 1853 | lace_size[*laces - 1] = size - total; |
| 1854 | break; |
| 1855 | } |
| 1856 | } |
| 1857 | |
| 1858 | *buf = data; |
| 1859 | *lace_buf = lace_size; |
Dale Curtis | fc43c19 | 2013-03-27 21:02:03 | [diff] [blame^] | 1860 | *buf_size = size; |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 1861 | |
| 1862 | return res; |
| 1863 | } |
| 1864 | |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 1865 | static int matroska_parse_rm_audio(MatroskaDemuxContext *matroska, |
| 1866 | MatroskaTrack *track, |
| 1867 | AVStream *st, |
| 1868 | uint8_t *data, int size, |
| 1869 | uint64_t timecode, uint64_t duration, |
| 1870 | int64_t pos) |
| 1871 | { |
| 1872 | int a = st->codec->block_align; |
| 1873 | int sps = track->audio.sub_packet_size; |
| 1874 | int cfs = track->audio.coded_framesize; |
| 1875 | int h = track->audio.sub_packet_h; |
| 1876 | int y = track->audio.sub_packet_cnt; |
| 1877 | int w = track->audio.frame_size; |
| 1878 | int x; |
| 1879 | |
| 1880 | if (!track->audio.pkt_cnt) { |
| 1881 | if (track->audio.sub_packet_cnt == 0) |
| 1882 | track->audio.buf_timecode = timecode; |
| 1883 | if (st->codec->codec_id == AV_CODEC_ID_RA_288) { |
| 1884 | if (size < cfs * h / 2) { |
| 1885 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1886 | "Corrupt int4 RM-style audio packet size\n"); |
| 1887 | return AVERROR_INVALIDDATA; |
| 1888 | } |
| 1889 | for (x=0; x<h/2; x++) |
| 1890 | memcpy(track->audio.buf+x*2*w+y*cfs, |
| 1891 | data+x*cfs, cfs); |
| 1892 | } else if (st->codec->codec_id == AV_CODEC_ID_SIPR) { |
| 1893 | if (size < w) { |
| 1894 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1895 | "Corrupt sipr RM-style audio packet size\n"); |
| 1896 | return AVERROR_INVALIDDATA; |
| 1897 | } |
| 1898 | memcpy(track->audio.buf + y*w, data, w); |
| 1899 | } else { |
| 1900 | if (size < sps * w / sps) { |
| 1901 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1902 | "Corrupt generic RM-style audio packet size\n"); |
| 1903 | return AVERROR_INVALIDDATA; |
| 1904 | } |
| 1905 | for (x=0; x<w/sps; x++) |
| 1906 | memcpy(track->audio.buf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), data+x*sps, sps); |
| 1907 | } |
| 1908 | |
| 1909 | if (++track->audio.sub_packet_cnt >= h) { |
| 1910 | if (st->codec->codec_id == AV_CODEC_ID_SIPR) |
| 1911 | ff_rm_reorder_sipr_data(track->audio.buf, h, w); |
| 1912 | track->audio.sub_packet_cnt = 0; |
| 1913 | track->audio.pkt_cnt = h*w / a; |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | while (track->audio.pkt_cnt) { |
| 1918 | AVPacket *pkt = av_mallocz(sizeof(AVPacket)); |
| 1919 | av_new_packet(pkt, a); |
| 1920 | memcpy(pkt->data, track->audio.buf |
| 1921 | + a * (h*w / a - track->audio.pkt_cnt--), a); |
| 1922 | pkt->pts = track->audio.buf_timecode; |
| 1923 | track->audio.buf_timecode = AV_NOPTS_VALUE; |
| 1924 | pkt->pos = pos; |
| 1925 | pkt->stream_index = st->index; |
| 1926 | dynarray_add(&matroska->packets,&matroska->num_packets,pkt); |
| 1927 | } |
| 1928 | |
| 1929 | return 0; |
| 1930 | } |
| 1931 | static int matroska_parse_frame(MatroskaDemuxContext *matroska, |
| 1932 | MatroskaTrack *track, |
| 1933 | AVStream *st, |
| 1934 | uint8_t *data, int pkt_size, |
| 1935 | uint64_t timecode, uint64_t duration, |
| 1936 | int64_t pos, int is_keyframe) |
| 1937 | { |
| 1938 | MatroskaTrackEncoding *encodings = track->encodings.elem; |
| 1939 | uint8_t *pkt_data = data; |
| 1940 | int offset = 0, res; |
| 1941 | AVPacket *pkt; |
| 1942 | |
| 1943 | if (encodings && encodings->scope & 1) { |
| 1944 | res = matroska_decode_buffer(&pkt_data, &pkt_size, track); |
| 1945 | if (res < 0) |
| 1946 | return res; |
| 1947 | } |
| 1948 | |
| 1949 | if (st->codec->codec_id == AV_CODEC_ID_PRORES) |
| 1950 | offset = 8; |
| 1951 | |
| 1952 | pkt = av_mallocz(sizeof(AVPacket)); |
| 1953 | /* XXX: prevent data copy... */ |
| 1954 | if (av_new_packet(pkt, pkt_size + offset) < 0) { |
| 1955 | av_free(pkt); |
| 1956 | return AVERROR(ENOMEM); |
| 1957 | } |
| 1958 | |
| 1959 | if (st->codec->codec_id == AV_CODEC_ID_PRORES) { |
| 1960 | uint8_t *buf = pkt->data; |
| 1961 | bytestream_put_be32(&buf, pkt_size); |
| 1962 | bytestream_put_be32(&buf, MKBETAG('i', 'c', 'p', 'f')); |
| 1963 | } |
| 1964 | |
| 1965 | memcpy(pkt->data + offset, pkt_data, pkt_size); |
| 1966 | |
| 1967 | if (pkt_data != data) |
| 1968 | av_free(pkt_data); |
| 1969 | |
| 1970 | pkt->flags = is_keyframe; |
| 1971 | pkt->stream_index = st->index; |
| 1972 | |
| 1973 | if (track->ms_compat) |
| 1974 | pkt->dts = timecode; |
| 1975 | else |
| 1976 | pkt->pts = timecode; |
| 1977 | pkt->pos = pos; |
| 1978 | if (st->codec->codec_id == AV_CODEC_ID_TEXT) |
| 1979 | pkt->convergence_duration = duration; |
| 1980 | else if (track->type != MATROSKA_TRACK_TYPE_SUBTITLE) |
| 1981 | pkt->duration = duration; |
| 1982 | |
| 1983 | if (st->codec->codec_id == AV_CODEC_ID_SSA) |
| 1984 | matroska_fix_ass_packet(matroska, pkt, duration); |
| 1985 | |
| 1986 | if (matroska->prev_pkt && |
| 1987 | timecode != AV_NOPTS_VALUE && |
| 1988 | matroska->prev_pkt->pts == timecode && |
| 1989 | matroska->prev_pkt->stream_index == st->index && |
| 1990 | st->codec->codec_id == AV_CODEC_ID_SSA) |
| 1991 | matroska_merge_packets(matroska->prev_pkt, pkt); |
| 1992 | else { |
| 1993 | dynarray_add(&matroska->packets,&matroska->num_packets,pkt); |
| 1994 | matroska->prev_pkt = pkt; |
| 1995 | } |
| 1996 | |
| 1997 | return 0; |
| 1998 | } |
| 1999 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 2000 | static int matroska_parse_block(MatroskaDemuxContext *matroska, uint8_t *data, |
| 2001 | int size, int64_t pos, uint64_t cluster_time, |
Luca Barbato | 7d84310 | 2012-09-17 00:48:02 | [diff] [blame] | 2002 | uint64_t block_duration, int is_keyframe, |
Aurelien Jacobs | 24c3da1 | 2008-09-06 23:39:59 | [diff] [blame] | 2003 | int64_t cluster_pos) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2004 | { |
Aurelien Jacobs | f14a201 | 2008-09-09 11:54:35 | [diff] [blame] | 2005 | uint64_t timecode = AV_NOPTS_VALUE; |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 2006 | MatroskaTrack *track; |
Aurelien Jacobs | a3467f8 | 2008-09-06 23:44:29 | [diff] [blame] | 2007 | int res = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2008 | AVStream *st; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2009 | int16_t block_time; |
| 2010 | uint32_t *lace_size = NULL; |
| 2011 | int n, flags, laces = 0; |
Luca Barbato | 7d84310 | 2012-09-17 00:48:02 | [diff] [blame] | 2012 | uint64_t num, duration; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2013 | |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 2014 | if ((n = matroska_ebmlnum_uint(matroska, data, size, &num)) < 0) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2015 | av_log(matroska->ctx, AV_LOG_ERROR, "EBML block data error\n"); |
Luca Barbato | 721af29 | 2012-04-30 00:39:31 | [diff] [blame] | 2016 | return n; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2017 | } |
| 2018 | data += n; |
| 2019 | size -= n; |
| 2020 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2021 | track = matroska_find_track_by_num(matroska, num); |
Ronald S. Bultje | d31fb1a | 2011-10-29 23:17:51 | [diff] [blame] | 2022 | if (!track || !track->stream) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2023 | av_log(matroska->ctx, AV_LOG_INFO, |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 2024 | "Invalid stream %"PRIu64" or size %u\n", num, size); |
Ami Fischman | 5dd514a | 2011-06-29 20:54:49 | [diff] [blame] | 2025 | return AVERROR_INVALIDDATA; |
Ronald S. Bultje | d31fb1a | 2011-10-29 23:17:51 | [diff] [blame] | 2026 | } else if (size <= 3) |
| 2027 | return 0; |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 2028 | st = track->stream; |
Aurelien Jacobs | 16f97ab | 2008-08-05 00:41:10 | [diff] [blame] | 2029 | if (st->discard >= AVDISCARD_ALL) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2030 | return res; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2031 | |
Aurelien Jacobs | 2ce746c | 2007-06-23 12:32:19 | [diff] [blame] | 2032 | block_time = AV_RB16(data); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2033 | data += 2; |
Aurelien Jacobs | 1607c53 | 2007-06-23 12:49:36 | [diff] [blame] | 2034 | flags = *data++; |
| 2035 | size -= 3; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2036 | if (is_keyframe == -1) |
Jean-Daniel Dupas | cc947f0 | 2010-03-31 12:29:58 | [diff] [blame] | 2037 | is_keyframe = flags & 0x80 ? AV_PKT_FLAG_KEY : 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2038 | |
Aurelien Jacobs | f14a201 | 2008-09-09 11:54:35 | [diff] [blame] | 2039 | if (cluster_time != (uint64_t)-1 |
| 2040 | && (block_time >= 0 || cluster_time >= -block_time)) { |
| 2041 | timecode = cluster_time + block_time; |
Aurelien Jacobs | 82360e6 | 2008-09-09 12:07:10 | [diff] [blame] | 2042 | if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE |
| 2043 | && timecode < track->end_timecode) |
| 2044 | is_keyframe = 0; /* overlapping subtitles are not key frame */ |
Aurelien Jacobs | a8fd7e7 | 2008-09-12 00:06:06 | [diff] [blame] | 2045 | if (is_keyframe) |
Aurelien Jacobs | f14a201 | 2008-09-09 11:54:35 | [diff] [blame] | 2046 | av_add_index_entry(st, cluster_pos, timecode, 0,0,AVINDEX_KEYFRAME); |
Aurelien Jacobs | f14a201 | 2008-09-09 11:54:35 | [diff] [blame] | 2047 | } |
| 2048 | |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 2049 | if (matroska->skip_to_keyframe && track->type != MATROSKA_TRACK_TYPE_SUBTITLE) { |
Aurelien Jacobs | 20f7466 | 2008-09-09 12:01:51 | [diff] [blame] | 2050 | if (!is_keyframe || timecode < matroska->skip_to_timecode) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2051 | return res; |
| 2052 | matroska->skip_to_keyframe = 0; |
| 2053 | } |
| 2054 | |
Dale Curtis | fc43c19 | 2013-03-27 21:02:03 | [diff] [blame^] | 2055 | res = matroska_parse_laces(matroska, &data, &size, (flags & 0x06) >> 1, |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 2056 | &lace_size, &laces); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2057 | |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 2058 | if (res) |
| 2059 | goto end; |
Aurelien Jacobs | eabb8ba | 2007-06-04 22:19:17 | [diff] [blame] | 2060 | |
Luca Barbato | 7d84310 | 2012-09-17 00:48:02 | [diff] [blame] | 2061 | if (block_duration != AV_NOPTS_VALUE) { |
| 2062 | duration = block_duration / laces; |
| 2063 | if (block_duration != duration * laces) { |
| 2064 | av_log(matroska->ctx, AV_LOG_WARNING, |
| 2065 | "Incorrect block_duration, possibly corrupted container"); |
| 2066 | } |
| 2067 | } else { |
| 2068 | duration = track->default_duration / matroska->time_scale; |
| 2069 | block_duration = duration * laces; |
| 2070 | } |
| 2071 | |
| 2072 | if (timecode != AV_NOPTS_VALUE) |
| 2073 | track->end_timecode = |
| 2074 | FFMAX(track->end_timecode, timecode + block_duration); |
| 2075 | |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 2076 | for (n = 0; n < laces; n++) { |
| 2077 | if ((st->codec->codec_id == AV_CODEC_ID_RA_288 || |
| 2078 | st->codec->codec_id == AV_CODEC_ID_COOK || |
| 2079 | st->codec->codec_id == AV_CODEC_ID_SIPR || |
| 2080 | st->codec->codec_id == AV_CODEC_ID_ATRAC3) && |
| 2081 | st->codec->block_align && track->audio.sub_packet_size) { |
Aurelien Jacobs | eabb8ba | 2007-06-04 22:19:17 | [diff] [blame] | 2082 | |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 2083 | res = matroska_parse_rm_audio(matroska, track, st, data, size, |
| 2084 | timecode, duration, pos); |
| 2085 | if (res) |
| 2086 | goto end; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2087 | |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 2088 | } else { |
| 2089 | res = matroska_parse_frame(matroska, track, st, data, lace_size[n], |
| 2090 | timecode, duration, |
| 2091 | pos, !n? is_keyframe : 0); |
| 2092 | if (res) |
| 2093 | goto end; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2094 | } |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 2095 | |
| 2096 | if (timecode != AV_NOPTS_VALUE) |
| 2097 | timecode = duration ? timecode + duration : AV_NOPTS_VALUE; |
| 2098 | data += lace_size[n]; |
| 2099 | size -= lace_size[n]; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2100 | } |
| 2101 | |
Dale Curtis | 3116858 | 2012-04-13 04:24:04 | [diff] [blame] | 2102 | end: |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2103 | av_free(lace_size); |
Aurelien Jacobs | a3467f8 | 2008-09-06 23:44:29 | [diff] [blame] | 2104 | return res; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2105 | } |
| 2106 | |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 2107 | static int matroska_parse_cluster_incremental(MatroskaDemuxContext *matroska) |
| 2108 | { |
| 2109 | EbmlList *blocks_list; |
| 2110 | MatroskaBlock *blocks; |
| 2111 | int i, res; |
| 2112 | res = ebml_parse(matroska, |
| 2113 | matroska_cluster_incremental_parsing, |
| 2114 | &matroska->current_cluster); |
| 2115 | if (res == 1) { |
| 2116 | /* New Cluster */ |
| 2117 | if (matroska->current_cluster_pos) |
| 2118 | ebml_level_end(matroska); |
| 2119 | ebml_free(matroska_cluster, &matroska->current_cluster); |
| 2120 | memset(&matroska->current_cluster, 0, sizeof(MatroskaCluster)); |
| 2121 | matroska->current_cluster_num_blocks = 0; |
| 2122 | matroska->current_cluster_pos = avio_tell(matroska->ctx->pb); |
| 2123 | matroska->prev_pkt = NULL; |
| 2124 | /* sizeof the ID which was already read */ |
| 2125 | if (matroska->current_id) |
| 2126 | matroska->current_cluster_pos -= 4; |
| 2127 | res = ebml_parse(matroska, |
| 2128 | matroska_clusters_incremental, |
| 2129 | &matroska->current_cluster); |
| 2130 | /* Try parsing the block again. */ |
| 2131 | if (res == 1) |
| 2132 | res = ebml_parse(matroska, |
| 2133 | matroska_cluster_incremental_parsing, |
| 2134 | &matroska->current_cluster); |
| 2135 | } |
| 2136 | |
| 2137 | if (!res && |
| 2138 | matroska->current_cluster_num_blocks < |
| 2139 | matroska->current_cluster.blocks.nb_elem) { |
| 2140 | blocks_list = &matroska->current_cluster.blocks; |
| 2141 | blocks = blocks_list->elem; |
| 2142 | |
| 2143 | matroska->current_cluster_num_blocks = blocks_list->nb_elem; |
| 2144 | i = blocks_list->nb_elem - 1; |
| 2145 | if (blocks[i].bin.size > 0 && blocks[i].bin.data) { |
| 2146 | int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1; |
| 2147 | if (!blocks[i].non_simple) |
| 2148 | blocks[i].duration = AV_NOPTS_VALUE; |
| 2149 | res = matroska_parse_block(matroska, |
| 2150 | blocks[i].bin.data, blocks[i].bin.size, |
| 2151 | blocks[i].bin.pos, |
| 2152 | matroska->current_cluster.timecode, |
| 2153 | blocks[i].duration, is_keyframe, |
| 2154 | matroska->current_cluster_pos); |
| 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | if (res < 0) matroska->done = 1; |
| 2159 | return res; |
| 2160 | } |
| 2161 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 2162 | static int matroska_parse_cluster(MatroskaDemuxContext *matroska) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2163 | { |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 2164 | MatroskaCluster cluster = { 0 }; |
| 2165 | EbmlList *blocks_list; |
| 2166 | MatroskaBlock *blocks; |
Aurelien Jacobs | 24c3da1 | 2008-09-06 23:39:59 | [diff] [blame] | 2167 | int i, res; |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 2168 | int64_t pos; |
| 2169 | if (!matroska->contains_ssa) |
| 2170 | return matroska_parse_cluster_incremental(matroska); |
| 2171 | pos = avio_tell(matroska->ctx->pb); |
Aurelien Jacobs | d5e34dc | 2008-09-28 23:06:25 | [diff] [blame] | 2172 | matroska->prev_pkt = NULL; |
Aurelien Jacobs | 8070203 | 2010-06-11 16:36:51 | [diff] [blame] | 2173 | if (matroska->current_id) |
Aurelien Jacobs | 8bc98ba | 2008-08-25 00:09:08 | [diff] [blame] | 2174 | pos -= 4; /* sizeof the ID which was already read */ |
Aurelien Jacobs | 7391781 | 2010-06-11 16:45:38 | [diff] [blame] | 2175 | res = ebml_parse(matroska, matroska_clusters, &cluster); |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 2176 | blocks_list = &cluster.blocks; |
| 2177 | blocks = blocks_list->elem; |
Ami Fischman | 5dd514a | 2011-06-29 20:54:49 | [diff] [blame] | 2178 | for (i=0; i<blocks_list->nb_elem && !res; i++) |
Aurelien Jacobs | 37dd235 | 2010-05-25 22:55:12 | [diff] [blame] | 2179 | if (blocks[i].bin.size > 0 && blocks[i].bin.data) { |
Aurelien Jacobs | 194d4b4 | 2009-08-10 18:06:14 | [diff] [blame] | 2180 | int is_keyframe = blocks[i].non_simple ? !blocks[i].reference : -1; |
John Stebbins | 98a7d56 | 2011-03-24 23:34:18 | [diff] [blame] | 2181 | if (!blocks[i].non_simple) |
| 2182 | blocks[i].duration = AV_NOPTS_VALUE; |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 2183 | res=matroska_parse_block(matroska, |
| 2184 | blocks[i].bin.data, blocks[i].bin.size, |
| 2185 | blocks[i].bin.pos, cluster.timecode, |
Aurelien Jacobs | 194d4b4 | 2009-08-10 18:06:14 | [diff] [blame] | 2186 | blocks[i].duration, is_keyframe, |
Aurelien Jacobs | 24c3da1 | 2008-09-06 23:39:59 | [diff] [blame] | 2187 | pos); |
Aurelien Jacobs | 194d4b4 | 2009-08-10 18:06:14 | [diff] [blame] | 2188 | } |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 2189 | ebml_free(matroska_cluster, &cluster); |
Aurelien Jacobs | 653fb2f | 2008-08-24 23:54:14 | [diff] [blame] | 2190 | if (res < 0) matroska->done = 1; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2191 | return res; |
| 2192 | } |
| 2193 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 2194 | static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2195 | { |
| 2196 | MatroskaDemuxContext *matroska = s->priv_data; |
Ami Fischman | 5dd514a | 2011-06-29 20:54:49 | [diff] [blame] | 2197 | int ret = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2198 | |
Ami Fischman | 5dd514a | 2011-06-29 20:54:49 | [diff] [blame] | 2199 | while (!ret && matroska_deliver_packet(matroska, pkt)) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2200 | if (matroska->done) |
Aurelien Jacobs | 9ebeea8 | 2009-02-19 21:01:45 | [diff] [blame] | 2201 | return AVERROR_EOF; |
Ami Fischman | 5dd514a | 2011-06-29 20:54:49 | [diff] [blame] | 2202 | ret = matroska_parse_cluster(matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2203 | } |
| 2204 | |
Anton Khirnov | bdb939a | 2012-09-20 18:01:43 | [diff] [blame] | 2205 | if (ret == AVERROR_INVALIDDATA && pkt->data) { |
Luca Barbato | 0ca4642 | 2012-04-30 00:55:51 | [diff] [blame] | 2206 | pkt->flags |= AV_PKT_FLAG_CORRUPT; |
| 2207 | return 0; |
| 2208 | } |
| 2209 | |
Ami Fischman | 5dd514a | 2011-06-29 20:54:49 | [diff] [blame] | 2210 | return ret; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2211 | } |
| 2212 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 2213 | static int matroska_read_seek(AVFormatContext *s, int stream_index, |
| 2214 | int64_t timestamp, int flags) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2215 | { |
| 2216 | MatroskaDemuxContext *matroska = s->priv_data; |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 2217 | MatroskaTrack *tracks = matroska->tracks.elem; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2218 | AVStream *st = s->streams[stream_index]; |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 2219 | int i, index, index_sub, index_min; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2220 | |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 2221 | /* Parse the CUES now since we need the index data to seek. */ |
| 2222 | if (matroska->cues_parsing_deferred) { |
| 2223 | matroska_parse_cues(matroska); |
| 2224 | matroska->cues_parsing_deferred = 0; |
| 2225 | } |
| 2226 | |
Aurelien Jacobs | a8fd7e7 | 2008-09-12 00:06:06 | [diff] [blame] | 2227 | if (!st->nb_index_entries) |
| 2228 | return 0; |
| 2229 | timestamp = FFMAX(timestamp, st->index_entries[0].timestamp); |
Aurelien Jacobs | dfbbbdc | 2008-08-24 23:57:29 | [diff] [blame] | 2230 | |
Aurelien Jacobs | 6bef5f9 | 2008-08-27 19:57:42 | [diff] [blame] | 2231 | if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) { |
Anton Khirnov | 6b4aa5d | 2011-02-28 13:57:54 | [diff] [blame] | 2232 | 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] | 2233 | matroska->current_id = 0; |
Aurelien Jacobs | 0dbddda | 2008-08-27 19:58:55 | [diff] [blame] | 2234 | while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0) { |
Aurelien Jacobs | 0dbddda | 2008-08-27 19:58:55 | [diff] [blame] | 2235 | matroska_clear_queue(matroska); |
| 2236 | if (matroska_parse_cluster(matroska) < 0) |
| 2237 | break; |
| 2238 | } |
Aurelien Jacobs | 6bef5f9 | 2008-08-27 19:57:42 | [diff] [blame] | 2239 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2240 | |
Aurelien Jacobs | 243cc4c | 2007-12-29 18:35:38 | [diff] [blame] | 2241 | matroska_clear_queue(matroska); |
Aurelien Jacobs | 0f646a2 | 2008-08-25 00:15:49 | [diff] [blame] | 2242 | if (index < 0) |
| 2243 | return 0; |
Aurelien Jacobs | 243cc4c | 2007-12-29 18:35:38 | [diff] [blame] | 2244 | |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 2245 | index_min = index; |
| 2246 | for (i=0; i < matroska->tracks.nb_elem; i++) { |
Reimar Döffinger | 4654420 | 2011-02-26 11:52:01 | [diff] [blame] | 2247 | tracks[i].audio.pkt_cnt = 0; |
| 2248 | tracks[i].audio.sub_packet_cnt = 0; |
| 2249 | tracks[i].audio.buf_timecode = AV_NOPTS_VALUE; |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 2250 | tracks[i].end_timecode = 0; |
| 2251 | if (tracks[i].type == MATROSKA_TRACK_TYPE_SUBTITLE |
| 2252 | && !tracks[i].stream->discard != AVDISCARD_ALL) { |
| 2253 | index_sub = av_index_search_timestamp(tracks[i].stream, st->index_entries[index].timestamp, AVSEEK_FLAG_BACKWARD); |
| 2254 | if (index_sub >= 0 |
| 2255 | && st->index_entries[index_sub].pos < st->index_entries[index_min].pos |
| 2256 | && st->index_entries[index].timestamp - st->index_entries[index_sub].timestamp < 30000000000/matroska->time_scale) |
| 2257 | index_min = index_sub; |
| 2258 | } |
| 2259 | } |
| 2260 | |
Anton Khirnov | 6b4aa5d | 2011-02-28 13:57:54 | [diff] [blame] | 2261 | avio_seek(s->pb, st->index_entries[index_min].pos, SEEK_SET); |
John Stebbins | cdc2c1c | 2011-07-01 15:57:42 | [diff] [blame] | 2262 | matroska->current_id = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2263 | matroska->skip_to_keyframe = !(flags & AVSEEK_FLAG_ANY); |
Aurelien Jacobs | 20f7466 | 2008-09-09 12:01:51 | [diff] [blame] | 2264 | matroska->skip_to_timecode = st->index_entries[index].timestamp; |
Aurelien Jacobs | 244ee48 | 2008-08-25 00:17:31 | [diff] [blame] | 2265 | matroska->done = 0; |
Anton Khirnov | a2faa95 | 2011-10-16 13:03:30 | [diff] [blame] | 2266 | ff_update_cur_dts(s, st, st->index_entries[index].timestamp); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2267 | return 0; |
| 2268 | } |
| 2269 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 2270 | static int matroska_read_close(AVFormatContext *s) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2271 | { |
| 2272 | MatroskaDemuxContext *matroska = s->priv_data; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 2273 | MatroskaTrack *tracks = matroska->tracks.elem; |
Aurelien Jacobs | 70109c0 | 2008-08-05 00:41:13 | [diff] [blame] | 2274 | int n; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2275 | |
Aurelien Jacobs | 34c9c1b | 2007-12-29 18:32:47 | [diff] [blame] | 2276 | matroska_clear_queue(matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2277 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 2278 | for (n=0; n < matroska->tracks.nb_elem; n++) |
| 2279 | if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO) |
| 2280 | av_free(tracks[n].audio.buf); |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 2281 | ebml_free(matroska_cluster, &matroska->current_cluster); |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 2282 | ebml_free(matroska_segment, matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2283 | |
| 2284 | return 0; |
| 2285 | } |
| 2286 | |
Diego Elio Pettenò | c6610a2 | 2011-01-25 22:03:28 | [diff] [blame] | 2287 | AVInputFormat ff_matroska_demuxer = { |
Anton Khirnov | dfc2c4d | 2011-07-16 20:18:12 | [diff] [blame] | 2288 | .name = "matroska,webm", |
Diego Biurrun | 6774247 | 2012-07-24 21:51:41 | [diff] [blame] | 2289 | .long_name = NULL_IF_CONFIG_SMALL("Matroska / WebM"), |
Anton Khirnov | dfc2c4d | 2011-07-16 20:18:12 | [diff] [blame] | 2290 | .priv_data_size = sizeof(MatroskaDemuxContext), |
| 2291 | .read_probe = matroska_probe, |
| 2292 | .read_header = matroska_read_header, |
| 2293 | .read_packet = matroska_read_packet, |
| 2294 | .read_close = matroska_read_close, |
| 2295 | .read_seek = matroska_read_seek, |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2296 | }; |