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