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