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 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 31 | #include "config.h" |
Martin Storsjö | a78f136 | 2022-02-23 12:56:49 | [diff] [blame] | 32 | #include "config_components.h" |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 33 | |
Diego Biurrun | d92024f | 2014-03-10 14:35:59 | [diff] [blame] | 34 | #include <inttypes.h> |
Aurelien Jacobs | 3eb9bfb | 2008-09-04 23:26:12 | [diff] [blame] | 35 | #include <stdio.h> |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 36 | |
| 37 | #include "libavutil/avstring.h" |
Michael Niedermayer | b3d9ab1 | 2014-03-07 22:46:37 | [diff] [blame] | 38 | #include "libavutil/base64.h" |
Andreas Rheinhardt | b2d61d0 | 2021-08-25 14:13:12 | [diff] [blame] | 39 | #include "libavutil/bprint.h" |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 40 | #include "libavutil/dict.h" |
Andreas Rheinhardt | 04b7217 | 2022-08-27 15:49:52 | [diff] [blame] | 41 | #include "libavutil/dict_internal.h" |
Andreas Rheinhardt | 937bb6b | 2022-01-21 18:42:50 | [diff] [blame] | 42 | #include "libavutil/display.h" |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 43 | #include "libavutil/intfloat.h" |
| 44 | #include "libavutil/intreadwrite.h" |
| 45 | #include "libavutil/lzo.h" |
Neil Birkbeck | bbda13a | 2016-03-01 00:41:04 | [diff] [blame] | 46 | #include "libavutil/mastering_display_metadata.h" |
Anton Khirnov | eb3b550 | 2014-04-29 10:03:13 | [diff] [blame] | 47 | #include "libavutil/mathematics.h" |
Vignesh Venkatasubramanian | 2171b7c | 2015-03-31 23:51:57 | [diff] [blame] | 48 | #include "libavutil/opt.h" |
Andreas Rheinhardt | 832e656 | 2022-09-21 00:36:04 | [diff] [blame] | 49 | #include "libavutil/pixdesc.h" |
Michael Niedermayer | a52cb42 | 2014-11-02 18:19:07 | [diff] [blame] | 50 | #include "libavutil/time_internal.h" |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 51 | #include "libavutil/spherical.h" |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 52 | |
rcombs | f758d4f | 2023-01-13 17:25:49 | [diff] [blame] | 53 | #include "libavcodec/avcodec.h" |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 54 | #include "libavcodec/bytestream.h" |
Anton Khirnov | 4efdadc | 2014-05-25 12:05:51 | [diff] [blame] | 55 | #include "libavcodec/flac.h" |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 56 | #include "libavcodec/mpeg4audio.h" |
James Almer | 8a81820 | 2020-08-17 15:03:50 | [diff] [blame] | 57 | #include "libavcodec/packet_internal.h" |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 58 | |
| 59 | #include "avformat.h" |
| 60 | #include "avio_internal.h" |
Andreas Rheinhardt | 35ec5c8 | 2022-05-06 16:28:08 | [diff] [blame] | 61 | #include "demux.h" |
quietvoid | 05c0794 | 2022-01-01 16:51:50 | [diff] [blame] | 62 | #include "dovi_isom.h" |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 63 | #include "internal.h" |
| 64 | #include "isom.h" |
| 65 | #include "matroska.h" |
Anton Khirnov | 23f741f | 2014-05-26 10:48:56 | [diff] [blame] | 66 | #include "oggdec.h" |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 67 | /* For ff_codec_get_id(). */ |
| 68 | #include "riff.h" |
| 69 | #include "rmsipr.h" |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 70 | |
Matthew Oliver | 0167fa0 | 2014-11-27 08:00:36 | [diff] [blame] | 71 | #if CONFIG_BZLIB |
| 72 | #include <bzlib.h> |
| 73 | #endif |
| 74 | #if CONFIG_ZLIB |
| 75 | #include <zlib.h> |
| 76 | #endif |
| 77 | |
Mats Peterson | 7973603 | 2015-12-27 20:28:09 | [diff] [blame] | 78 | #include "qtpalette.h" |
| 79 | |
Steve Lhomme | a2c7702 | 2019-02-23 10:14:33 | [diff] [blame] | 80 | #define EBML_UNKNOWN_LENGTH UINT64_MAX /* EBML unknown length, in uint64_t */ |
Andreas Rheinhardt | a569a7b | 2019-06-23 23:42:30 | [diff] [blame] | 81 | #define NEEDS_CHECKING 2 /* Indicates that some error checks |
| 82 | * still need to be performed */ |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 83 | #define LEVEL_ENDED 3 /* return value of ebml_parse when the |
| 84 | * syntax level used for parsing ended. */ |
Andreas Rheinhardt | 5120305 | 2019-05-16 22:30:14 | [diff] [blame] | 85 | #define SKIP_THRESHOLD 1024 * 1024 /* In non-seekable mode, if more than SKIP_THRESHOLD |
| 86 | * of unkown, potentially damaged data is encountered, |
| 87 | * it is considered an error. */ |
| 88 | #define UNKNOWN_EQUIV 50 * 1024 /* An unknown element is considered equivalent |
| 89 | * to this many bytes of unknown data for the |
| 90 | * SKIP_THRESHOLD check. */ |
Steve Lhomme | a2c7702 | 2019-02-23 10:14:33 | [diff] [blame] | 91 | |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 92 | typedef enum { |
| 93 | EBML_NONE, |
| 94 | EBML_UINT, |
Andreas Rheinhardt | 60f75c9 | 2019-05-16 22:30:13 | [diff] [blame] | 95 | EBML_SINT, |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 96 | EBML_FLOAT, |
| 97 | EBML_STR, |
| 98 | EBML_UTF8, |
| 99 | EBML_BIN, |
| 100 | EBML_NEST, |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 101 | EBML_LEVEL1, |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 102 | EBML_STOP, |
Reimar Döffinger | 14d735b | 2011-02-06 10:32:03 | [diff] [blame] | 103 | EBML_TYPE_COUNT |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 104 | } EbmlType; |
| 105 | |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 106 | typedef struct CountedElement { |
| 107 | union { |
| 108 | uint64_t u; |
| 109 | int64_t i; |
| 110 | double f; |
| 111 | char *s; |
| 112 | } el; |
| 113 | unsigned count; |
| 114 | } CountedElement; |
| 115 | |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 116 | typedef const struct EbmlSyntax { |
| 117 | uint32_t id; |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 118 | uint8_t type; |
| 119 | uint8_t is_counted; |
James Almer | f34aabf | 2019-09-03 21:52:51 | [diff] [blame] | 120 | size_t list_elem_size; |
| 121 | size_t data_offset; |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 122 | union { |
Chris Cunningham | ac25840 | 2017-02-03 22:42:44 | [diff] [blame] | 123 | int64_t i; |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 124 | uint64_t u; |
| 125 | double f; |
| 126 | const char *s; |
| 127 | const struct EbmlSyntax *n; |
| 128 | } def; |
| 129 | } EbmlSyntax; |
| 130 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 131 | typedef struct EbmlList { |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 132 | int nb_elem; |
James Almer | 3b3150c | 2019-09-03 21:45:04 | [diff] [blame] | 133 | unsigned int alloc_elem_size; |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 134 | void *elem; |
| 135 | } EbmlList; |
| 136 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 137 | typedef struct EbmlBin { |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 138 | int size; |
James Almer | a618866 | 2018-04-04 17:12:54 | [diff] [blame] | 139 | AVBufferRef *buf; |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 140 | uint8_t *data; |
| 141 | int64_t pos; |
| 142 | } EbmlBin; |
| 143 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 144 | typedef struct Ebml { |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 145 | uint64_t version; |
| 146 | uint64_t max_size; |
| 147 | uint64_t id_length; |
| 148 | char *doctype; |
| 149 | uint64_t doctype_version; |
| 150 | } Ebml; |
| 151 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 152 | typedef struct MatroskaTrackCompression { |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 153 | uint64_t algo; |
| 154 | EbmlBin settings; |
| 155 | } MatroskaTrackCompression; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 156 | |
Michael Niedermayer | a0fe1a2 | 2015-02-14 20:07:40 | [diff] [blame] | 157 | typedef struct MatroskaTrackEncryption { |
Frank Galligan | b853103 | 2013-03-07 16:11:38 | [diff] [blame] | 158 | uint64_t algo; |
| 159 | EbmlBin key_id; |
| 160 | } MatroskaTrackEncryption; |
| 161 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 162 | typedef struct MatroskaTrackEncoding { |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 163 | uint64_t scope; |
| 164 | uint64_t type; |
| 165 | MatroskaTrackCompression compression; |
Frank Galligan | b853103 | 2013-03-07 16:11:38 | [diff] [blame] | 166 | MatroskaTrackEncryption encryption; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 167 | } MatroskaTrackEncoding; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 168 | |
Neil Birkbeck | bbda13a | 2016-03-01 00:41:04 | [diff] [blame] | 169 | typedef struct MatroskaMasteringMeta { |
| 170 | double r_x; |
| 171 | double r_y; |
| 172 | double g_x; |
| 173 | double g_y; |
| 174 | double b_x; |
| 175 | double b_y; |
| 176 | double white_x; |
| 177 | double white_y; |
| 178 | double max_luminance; |
Andreas Rheinhardt | e8f3901 | 2021-02-16 14:34:06 | [diff] [blame] | 179 | CountedElement min_luminance; |
Neil Birkbeck | bbda13a | 2016-03-01 00:41:04 | [diff] [blame] | 180 | } MatroskaMasteringMeta; |
| 181 | |
| 182 | typedef struct MatroskaTrackVideoColor { |
| 183 | uint64_t matrix_coefficients; |
| 184 | uint64_t bits_per_channel; |
| 185 | uint64_t chroma_sub_horz; |
| 186 | uint64_t chroma_sub_vert; |
| 187 | uint64_t cb_sub_horz; |
| 188 | uint64_t cb_sub_vert; |
| 189 | uint64_t chroma_siting_horz; |
| 190 | uint64_t chroma_siting_vert; |
| 191 | uint64_t range; |
| 192 | uint64_t transfer_characteristics; |
| 193 | uint64_t primaries; |
| 194 | uint64_t max_cll; |
| 195 | uint64_t max_fall; |
| 196 | MatroskaMasteringMeta mastering_meta; |
| 197 | } MatroskaTrackVideoColor; |
| 198 | |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 199 | typedef struct MatroskaTrackVideoProjection { |
| 200 | uint64_t type; |
| 201 | EbmlBin private; |
| 202 | double yaw; |
| 203 | double pitch; |
| 204 | double roll; |
| 205 | } MatroskaTrackVideoProjection; |
| 206 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 207 | typedef struct MatroskaTrackVideo { |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 208 | double frame_rate; |
| 209 | uint64_t display_width; |
| 210 | uint64_t display_height; |
| 211 | uint64_t pixel_width; |
| 212 | uint64_t pixel_height; |
Andreas Rheinhardt | 60f75c9 | 2019-05-16 22:30:13 | [diff] [blame] | 213 | EbmlBin color_space; |
James Almer | bad8bbc | 2016-10-15 21:01:50 | [diff] [blame] | 214 | uint64_t display_unit; |
Luca Barbato | 5f02266 | 2016-03-28 18:29:54 | [diff] [blame] | 215 | uint64_t interlaced; |
| 216 | uint64_t field_order; |
Aurelien Jacobs | 4c509fe | 2011-05-23 23:09:24 | [diff] [blame] | 217 | uint64_t stereo_mode; |
Vignesh Venkatasubramanian | ce6a8e5 | 2013-02-04 23:17:52 | [diff] [blame] | 218 | uint64_t alpha_mode; |
James Almer | 4e75907 | 2016-12-05 02:22:39 | [diff] [blame] | 219 | EbmlList color; |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 220 | MatroskaTrackVideoProjection projection; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 221 | } MatroskaTrackVideo; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 222 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 223 | typedef struct MatroskaTrackAudio { |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 224 | double samplerate; |
| 225 | double out_samplerate; |
| 226 | uint64_t bitdepth; |
| 227 | uint64_t channels; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 228 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 229 | /* real audio header (extracted from extradata) */ |
| 230 | int coded_framesize; |
| 231 | int sub_packet_h; |
| 232 | int frame_size; |
| 233 | int sub_packet_size; |
| 234 | int sub_packet_cnt; |
| 235 | int pkt_cnt; |
Reimar Döffinger | b09e506 | 2011-02-26 11:52:01 | [diff] [blame] | 236 | uint64_t buf_timecode; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 237 | uint8_t *buf; |
| 238 | } MatroskaTrackAudio; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 239 | |
Michael Niedermayer | a0fe1a2 | 2015-02-14 20:07:40 | [diff] [blame] | 240 | typedef struct MatroskaTrackPlane { |
Kirill Gavrilov | e6ec921 | 2011-05-21 15:14:14 | [diff] [blame] | 241 | uint64_t uid; |
| 242 | uint64_t type; |
| 243 | } MatroskaTrackPlane; |
| 244 | |
Michael Niedermayer | a0fe1a2 | 2015-02-14 20:07:40 | [diff] [blame] | 245 | typedef struct MatroskaTrackOperation { |
Kirill Gavrilov | e6ec921 | 2011-05-21 15:14:14 | [diff] [blame] | 246 | EbmlList combine_planes; |
Kirill Gavrilov | e6ec921 | 2011-05-21 15:14:14 | [diff] [blame] | 247 | } MatroskaTrackOperation; |
| 248 | |
quietvoid | 05c0794 | 2022-01-01 16:51:50 | [diff] [blame] | 249 | typedef struct MatroskaBlockAdditionMapping { |
| 250 | uint64_t value; |
| 251 | char *name; |
| 252 | uint64_t type; |
| 253 | EbmlBin extradata; |
| 254 | } MatroskaBlockAdditionMapping; |
| 255 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 256 | typedef struct MatroskaTrack { |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 257 | uint64_t num; |
Aurelien Jacobs | 325ace3 | 2009-02-15 15:34:22 | [diff] [blame] | 258 | uint64_t uid; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 259 | uint64_t type; |
Aurelien Jacobs | 38766e0 | 2009-02-15 15:29:09 | [diff] [blame] | 260 | char *name; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 261 | char *codec_id; |
| 262 | EbmlBin codec_priv; |
| 263 | char *language; |
Anton Khirnov | 7ff9708 | 2008-06-01 13:54:11 | [diff] [blame] | 264 | double time_scale; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 265 | uint64_t default_duration; |
Aurelien Jacobs | 4eff974 | 2008-08-05 00:39:53 | [diff] [blame] | 266 | uint64_t flag_default; |
Aurelien Jacobs | 7a617a8 | 2010-07-02 16:38:44 | [diff] [blame] | 267 | uint64_t flag_forced; |
Andreas Rheinhardt | 18b3dee | 2021-02-16 17:23:59 | [diff] [blame] | 268 | uint64_t flag_comment; |
Andreas Rheinhardt | 51e1067 | 2021-02-16 18:08:51 | [diff] [blame] | 269 | uint64_t flag_hearingimpaired; |
| 270 | uint64_t flag_visualimpaired; |
Andreas Rheinhardt | 48cf1d8 | 2021-02-16 18:38:19 | [diff] [blame] | 271 | uint64_t flag_textdescriptions; |
Andreas Rheinhardt | b243be8 | 2021-02-16 17:41:30 | [diff] [blame] | 272 | CountedElement flag_original; |
Vignesh Venkatasubramanian | d6f86d7 | 2013-10-14 17:42:08 | [diff] [blame] | 273 | uint64_t seek_preroll; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 274 | MatroskaTrackVideo video; |
| 275 | MatroskaTrackAudio audio; |
Kirill Gavrilov | e6ec921 | 2011-05-21 15:14:14 | [diff] [blame] | 276 | MatroskaTrackOperation operation; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 277 | EbmlList encodings; |
Anton Khirnov | eb3b550 | 2014-04-29 10:03:13 | [diff] [blame] | 278 | uint64_t codec_delay; |
Michael Niedermayer | b5bc436 | 2016-06-06 02:23:16 | [diff] [blame] | 279 | uint64_t codec_delay_in_track_tb; |
Aurelien Jacobs | fc4d335 | 2008-08-05 00:40:06 | [diff] [blame] | 280 | |
| 281 | AVStream *stream; |
Aurelien Jacobs | 82360e6 | 2008-09-09 12:07:10 | [diff] [blame] | 282 | int64_t end_timecode; |
Joakim Plate | 3e93c8e | 2010-03-03 21:46:43 | [diff] [blame] | 283 | int ms_compat; |
Andreas Rheinhardt | 96012d1 | 2019-12-04 16:54:45 | [diff] [blame] | 284 | int needs_decoding; |
Vignesh Venkatasubramanian | 30c5c45 | 2013-02-13 21:51:48 | [diff] [blame] | 285 | uint64_t max_block_additional_id; |
quietvoid | 05c0794 | 2022-01-01 16:51:50 | [diff] [blame] | 286 | EbmlList block_addition_mappings; |
Mats Peterson | 6aac43f | 2016-02-24 17:14:05 | [diff] [blame] | 287 | |
| 288 | uint32_t palette[AVPALETTE_COUNT]; |
| 289 | int has_palette; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 290 | } MatroskaTrack; |
| 291 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 292 | typedef struct MatroskaAttachment { |
Aurelien Jacobs | 325ace3 | 2009-02-15 15:34:22 | [diff] [blame] | 293 | uint64_t uid; |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 294 | char *filename; |
Andreas Rheinhardt | 3bd26b2 | 2020-05-06 13:24:33 | [diff] [blame] | 295 | char *description; |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 296 | char *mime; |
| 297 | EbmlBin bin; |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 298 | |
| 299 | AVStream *stream; |
Diego Biurrun | f69befe | 2014-03-07 12:54:18 | [diff] [blame] | 300 | } MatroskaAttachment; |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 301 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 302 | typedef struct MatroskaChapter { |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 303 | uint64_t start; |
| 304 | uint64_t end; |
| 305 | uint64_t uid; |
| 306 | char *title; |
Aurelien Jacobs | 6cb6e15 | 2009-02-15 15:25:14 | [diff] [blame] | 307 | |
| 308 | AVChapter *chapter; |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 309 | } MatroskaChapter; |
| 310 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 311 | typedef struct MatroskaIndexPos { |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 312 | uint64_t track; |
| 313 | uint64_t pos; |
| 314 | } MatroskaIndexPos; |
| 315 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 316 | typedef struct MatroskaIndex { |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 317 | uint64_t time; |
| 318 | EbmlList pos; |
| 319 | } MatroskaIndex; |
| 320 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 321 | typedef struct MatroskaTag { |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 322 | char *name; |
| 323 | char *string; |
Aurelien Jacobs | f702df3 | 2009-02-15 16:05:37 | [diff] [blame] | 324 | char *lang; |
| 325 | uint64_t def; |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 326 | EbmlList sub; |
| 327 | } MatroskaTag; |
| 328 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 329 | typedef struct MatroskaTagTarget { |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 330 | char *type; |
| 331 | uint64_t typevalue; |
| 332 | uint64_t trackuid; |
| 333 | uint64_t chapteruid; |
| 334 | uint64_t attachuid; |
| 335 | } MatroskaTagTarget; |
| 336 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 337 | typedef struct MatroskaTags { |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 338 | MatroskaTagTarget target; |
| 339 | EbmlList tag; |
| 340 | } MatroskaTags; |
| 341 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 342 | typedef struct MatroskaSeekhead { |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 343 | uint64_t id; |
| 344 | uint64_t pos; |
| 345 | } MatroskaSeekhead; |
| 346 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 347 | typedef struct MatroskaLevel { |
Aurelien Jacobs | 8d75b5a | 2007-06-04 22:35:16 | [diff] [blame] | 348 | uint64_t start; |
| 349 | uint64_t length; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 350 | } MatroskaLevel; |
| 351 | |
James Almer | 4451315 | 2023-03-12 18:02:06 | [diff] [blame^] | 352 | typedef struct MatroskaBlockMore { |
| 353 | uint64_t additional_id; |
| 354 | EbmlBin additional; |
| 355 | } MatroskaBlockMore; |
| 356 | |
Andreas Rheinhardt | ffa64a4 | 2019-05-16 22:29:59 | [diff] [blame] | 357 | typedef struct MatroskaBlock { |
| 358 | uint64_t duration; |
Andreas Rheinhardt | 2f1a562 | 2021-02-16 15:18:02 | [diff] [blame] | 359 | CountedElement reference; |
Andreas Rheinhardt | ffa64a4 | 2019-05-16 22:29:59 | [diff] [blame] | 360 | uint64_t non_simple; |
| 361 | EbmlBin bin; |
James Almer | 4451315 | 2023-03-12 18:02:06 | [diff] [blame^] | 362 | EbmlList blockmore; |
Andreas Rheinhardt | 60f75c9 | 2019-05-16 22:30:13 | [diff] [blame] | 363 | int64_t discard_padding; |
Andreas Rheinhardt | ffa64a4 | 2019-05-16 22:29:59 | [diff] [blame] | 364 | } MatroskaBlock; |
| 365 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 366 | typedef struct MatroskaCluster { |
Andreas Rheinhardt | ffa64a4 | 2019-05-16 22:29:59 | [diff] [blame] | 367 | MatroskaBlock block; |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 368 | uint64_t timecode; |
Andreas Rheinhardt | ffa64a4 | 2019-05-16 22:29:59 | [diff] [blame] | 369 | int64_t pos; |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 370 | } MatroskaCluster; |
| 371 | |
Michael Niedermayer | a0fe1a2 | 2015-02-14 20:07:40 | [diff] [blame] | 372 | typedef struct MatroskaLevel1Element { |
Andreas Rheinhardt | 730ac1a | 2019-05-16 22:30:20 | [diff] [blame] | 373 | int64_t pos; |
Andreas Rheinhardt | 410a082 | 2019-05-16 22:29:47 | [diff] [blame] | 374 | uint32_t id; |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 375 | int parsed; |
| 376 | } MatroskaLevel1Element; |
| 377 | |
Diego Biurrun | daf8cf3 | 2014-09-22 07:19:33 | [diff] [blame] | 378 | typedef struct MatroskaDemuxContext { |
Vignesh Venkatasubramanian | 2171b7c | 2015-03-31 23:51:57 | [diff] [blame] | 379 | const AVClass *class; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 380 | AVFormatContext *ctx; |
| 381 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 382 | /* EBML stuff */ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 383 | MatroskaLevel levels[EBML_MAX_DEPTH]; |
Andreas Rheinhardt | 60f75c9 | 2019-05-16 22:30:13 | [diff] [blame] | 384 | int num_levels; |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 385 | uint32_t current_id; |
Andreas Rheinhardt | a3db9f6 | 2019-05-16 22:30:05 | [diff] [blame] | 386 | int64_t resync_pos; |
Andreas Rheinhardt | 5120305 | 2019-05-16 22:30:14 | [diff] [blame] | 387 | int unknown_count; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 388 | |
Aurelien Jacobs | 2970858 | 2008-08-05 00:40:27 | [diff] [blame] | 389 | uint64_t time_scale; |
| 390 | double duration; |
| 391 | char *title; |
James Almer | 2c759d7 | 2013-11-24 08:31:48 | [diff] [blame] | 392 | char *muxingapp; |
Andreas Rheinhardt | 60f75c9 | 2019-05-16 22:30:13 | [diff] [blame] | 393 | EbmlBin date_utc; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 394 | EbmlList tracks; |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 395 | EbmlList attachments; |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 396 | EbmlList chapters; |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 397 | EbmlList index; |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 398 | EbmlList tags; |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 399 | EbmlList seekhead; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 400 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 401 | /* byte position of the segment inside the stream */ |
Diego Biurrun | bc5c918 | 2008-10-03 10:16:29 | [diff] [blame] | 402 | int64_t segment_start; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 403 | |
Andreas Rheinhardt | fed0282 | 2021-08-24 12:58:07 | [diff] [blame] | 404 | /* This packet coincides with FFFormatContext.parse_pkt |
Andreas Rheinhardt | a4d0369 | 2021-03-18 05:04:17 | [diff] [blame] | 405 | * and is not owned by us. */ |
James Almer | 8d78e90 | 2021-01-29 13:42:48 | [diff] [blame] | 406 | AVPacket *pkt; |
| 407 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 408 | /* the packet queue */ |
Andreas Rheinhardt | d61240f | 2021-12-16 00:49:39 | [diff] [blame] | 409 | PacketList queue; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 410 | |
Aurelien Jacobs | 8d75b5a | 2007-06-04 22:35:16 | [diff] [blame] | 411 | int done; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 412 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 413 | /* What to skip before effectively reading a packet. */ |
| 414 | int skip_to_keyframe; |
Aurelien Jacobs | 20f7466 | 2008-09-09 12:01:51 | [diff] [blame] | 415 | uint64_t skip_to_timecode; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 416 | |
| 417 | /* File has a CUES element, but we defer parsing until it is needed. */ |
| 418 | int cues_parsing_deferred; |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 419 | |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 420 | /* Level1 elements and whether they were read yet */ |
| 421 | MatroskaLevel1Element level1_elems[64]; |
| 422 | int num_level1_elems; |
| 423 | |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 424 | MatroskaCluster current_cluster; |
| 425 | |
James Zern | 20aeee4 | 2017-04-17 17:59:31 | [diff] [blame] | 426 | /* WebM DASH Manifest live flag */ |
Vignesh Venkatasubramanian | 2171b7c | 2015-03-31 23:51:57 | [diff] [blame] | 427 | int is_live; |
Vignesh Venkatasubramanian | 62c27fd | 2017-04-12 04:33:28 | [diff] [blame] | 428 | |
| 429 | /* Bandwidth value for WebM DASH Manifest */ |
| 430 | int bandwidth; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 431 | } MatroskaDemuxContext; |
| 432 | |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 433 | #define CHILD_OF(parent) { .def = { .n = parent } } |
| 434 | |
Andreas Rheinhardt | ab4795a | 2019-07-17 03:29:40 | [diff] [blame] | 435 | // The following forward declarations need their size because |
| 436 | // a tentative definition with internal linkage must not be an |
| 437 | // incomplete type (6.7.2 in C90, 6.9.2 in C99). |
| 438 | // Removing the sizes breaks MSVC. |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 439 | static EbmlSyntax ebml_syntax[3], matroska_segment[9], matroska_track_video_color[15], matroska_track_video[19], |
quietvoid | 05c0794 | 2022-01-01 16:51:50 | [diff] [blame] | 440 | matroska_track[33], matroska_track_encoding[6], matroska_track_encodings[2], |
| 441 | matroska_track_combine_planes[2], matroska_track_operation[2], matroska_block_addition_mapping[5], matroska_tracks[2], |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 442 | matroska_attachments[2], matroska_chapter_entry[9], matroska_chapter[6], matroska_chapters[2], |
| 443 | matroska_index_entry[3], matroska_index[2], matroska_tag[3], matroska_tags[2], matroska_seekhead[2], |
| 444 | matroska_blockadditions[2], matroska_blockgroup[8], matroska_cluster_parsing[8]; |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 445 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 446 | static EbmlSyntax ebml_header[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 447 | { EBML_ID_EBMLREADVERSION, EBML_UINT, 0, 0, offsetof(Ebml, version), { .u = EBML_VERSION } }, |
| 448 | { EBML_ID_EBMLMAXSIZELENGTH, EBML_UINT, 0, 0, offsetof(Ebml, max_size), { .u = 8 } }, |
| 449 | { EBML_ID_EBMLMAXIDLENGTH, EBML_UINT, 0, 0, offsetof(Ebml, id_length), { .u = 4 } }, |
| 450 | { EBML_ID_DOCTYPE, EBML_STR, 0, 0, offsetof(Ebml, doctype), { .s = "(none)" } }, |
| 451 | { EBML_ID_DOCTYPEREADVERSION, EBML_UINT, 0, 0, offsetof(Ebml, doctype_version), { .u = 1 } }, |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 452 | { EBML_ID_EBMLVERSION, EBML_NONE }, |
| 453 | { EBML_ID_DOCTYPEVERSION, EBML_NONE }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 454 | CHILD_OF(ebml_syntax) |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 455 | }; |
| 456 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 457 | static EbmlSyntax ebml_syntax[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 458 | { EBML_ID_HEADER, EBML_NEST, 0, 0, 0, { .n = ebml_header } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 459 | { MATROSKA_ID_SEGMENT, EBML_STOP }, |
Aurelien Jacobs | 6351132 | 2008-08-05 00:40:02 | [diff] [blame] | 460 | { 0 } |
| 461 | }; |
| 462 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 463 | static EbmlSyntax matroska_info[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 464 | { MATROSKA_ID_TIMECODESCALE, EBML_UINT, 0, 0, offsetof(MatroskaDemuxContext, time_scale), { .u = 1000000 } }, |
| 465 | { MATROSKA_ID_DURATION, EBML_FLOAT, 0, 0, offsetof(MatroskaDemuxContext, duration) }, |
| 466 | { MATROSKA_ID_TITLE, EBML_UTF8, 0, 0, offsetof(MatroskaDemuxContext, title) }, |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 467 | { MATROSKA_ID_WRITINGAPP, EBML_NONE }, |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 468 | { MATROSKA_ID_MUXINGAPP, EBML_UTF8, 0, 0, offsetof(MatroskaDemuxContext, muxingapp) }, |
| 469 | { MATROSKA_ID_DATEUTC, EBML_BIN, 0, 0, offsetof(MatroskaDemuxContext, date_utc) }, |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 470 | { MATROSKA_ID_SEGMENTUID, EBML_NONE }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 471 | CHILD_OF(matroska_segment) |
Aurelien Jacobs | 2970858 | 2008-08-05 00:40:27 | [diff] [blame] | 472 | }; |
| 473 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 474 | static EbmlSyntax matroska_mastering_meta[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 475 | { MATROSKA_ID_VIDEOCOLOR_RX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, r_x) }, |
| 476 | { MATROSKA_ID_VIDEOCOLOR_RY, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, r_y) }, |
| 477 | { MATROSKA_ID_VIDEOCOLOR_GX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, g_x) }, |
| 478 | { MATROSKA_ID_VIDEOCOLOR_GY, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, g_y) }, |
| 479 | { MATROSKA_ID_VIDEOCOLOR_BX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, b_x) }, |
| 480 | { MATROSKA_ID_VIDEOCOLOR_BY, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, b_y) }, |
| 481 | { MATROSKA_ID_VIDEOCOLOR_WHITEX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, white_x) }, |
| 482 | { MATROSKA_ID_VIDEOCOLOR_WHITEY, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, white_y) }, |
Andreas Rheinhardt | e8f3901 | 2021-02-16 14:34:06 | [diff] [blame] | 483 | { MATROSKA_ID_VIDEOCOLOR_LUMINANCEMIN, EBML_FLOAT, 1, 0, offsetof(MatroskaMasteringMeta, min_luminance) }, |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 484 | { MATROSKA_ID_VIDEOCOLOR_LUMINANCEMAX, EBML_FLOAT, 0, 0, offsetof(MatroskaMasteringMeta, max_luminance) }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 485 | CHILD_OF(matroska_track_video_color) |
Neil Birkbeck | bbda13a | 2016-03-01 00:41:04 | [diff] [blame] | 486 | }; |
| 487 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 488 | static EbmlSyntax matroska_track_video_color[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 489 | { MATROSKA_ID_VIDEOCOLORMATRIXCOEFF, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, matrix_coefficients), { .u = AVCOL_SPC_UNSPECIFIED } }, |
| 490 | { MATROSKA_ID_VIDEOCOLORBITSPERCHANNEL, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, bits_per_channel), { .u = 0 } }, |
| 491 | { MATROSKA_ID_VIDEOCOLORCHROMASUBHORZ, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, chroma_sub_horz) }, |
| 492 | { MATROSKA_ID_VIDEOCOLORCHROMASUBVERT, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, chroma_sub_vert) }, |
| 493 | { MATROSKA_ID_VIDEOCOLORCBSUBHORZ, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, cb_sub_horz) }, |
| 494 | { MATROSKA_ID_VIDEOCOLORCBSUBVERT, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, cb_sub_vert) }, |
| 495 | { MATROSKA_ID_VIDEOCOLORCHROMASITINGHORZ, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, chroma_siting_horz), { .u = MATROSKA_COLOUR_CHROMASITINGHORZ_UNDETERMINED } }, |
| 496 | { MATROSKA_ID_VIDEOCOLORCHROMASITINGVERT, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, chroma_siting_vert), { .u = MATROSKA_COLOUR_CHROMASITINGVERT_UNDETERMINED } }, |
| 497 | { MATROSKA_ID_VIDEOCOLORRANGE, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, range), { .u = AVCOL_RANGE_UNSPECIFIED } }, |
| 498 | { MATROSKA_ID_VIDEOCOLORTRANSFERCHARACTERISTICS, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, transfer_characteristics), { .u = AVCOL_TRC_UNSPECIFIED } }, |
| 499 | { MATROSKA_ID_VIDEOCOLORPRIMARIES, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, primaries), { .u = AVCOL_PRI_UNSPECIFIED } }, |
| 500 | { MATROSKA_ID_VIDEOCOLORMAXCLL, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, max_cll) }, |
| 501 | { MATROSKA_ID_VIDEOCOLORMAXFALL, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoColor, max_fall) }, |
| 502 | { MATROSKA_ID_VIDEOCOLORMASTERINGMETA, EBML_NEST, 0, 0, offsetof(MatroskaTrackVideoColor, mastering_meta), { .n = matroska_mastering_meta } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 503 | CHILD_OF(matroska_track_video) |
Neil Birkbeck | bbda13a | 2016-03-01 00:41:04 | [diff] [blame] | 504 | }; |
| 505 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 506 | static EbmlSyntax matroska_track_video_projection[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 507 | { MATROSKA_ID_VIDEOPROJECTIONTYPE, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideoProjection, type), { .u = MATROSKA_VIDEO_PROJECTION_TYPE_RECTANGULAR } }, |
| 508 | { MATROSKA_ID_VIDEOPROJECTIONPRIVATE, EBML_BIN, 0, 0, offsetof(MatroskaTrackVideoProjection, private) }, |
| 509 | { MATROSKA_ID_VIDEOPROJECTIONPOSEYAW, EBML_FLOAT, 0, 0, offsetof(MatroskaTrackVideoProjection, yaw), { .f = 0.0 } }, |
| 510 | { MATROSKA_ID_VIDEOPROJECTIONPOSEPITCH, EBML_FLOAT, 0, 0, offsetof(MatroskaTrackVideoProjection, pitch), { .f = 0.0 } }, |
| 511 | { MATROSKA_ID_VIDEOPROJECTIONPOSEROLL, EBML_FLOAT, 0, 0, offsetof(MatroskaTrackVideoProjection, roll), { .f = 0.0 } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 512 | CHILD_OF(matroska_track_video) |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 513 | }; |
| 514 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 515 | static EbmlSyntax matroska_track_video[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 516 | { MATROSKA_ID_VIDEOFRAMERATE, EBML_FLOAT, 0, 0, offsetof(MatroskaTrackVideo, frame_rate) }, |
| 517 | { MATROSKA_ID_VIDEODISPLAYWIDTH, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, display_width), { .u=-1 } }, |
| 518 | { MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, display_height), { .u=-1 } }, |
| 519 | { MATROSKA_ID_VIDEOPIXELWIDTH, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, pixel_width) }, |
| 520 | { MATROSKA_ID_VIDEOPIXELHEIGHT, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, pixel_height) }, |
| 521 | { MATROSKA_ID_VIDEOCOLORSPACE, EBML_BIN, 0, 0, offsetof(MatroskaTrackVideo, color_space) }, |
| 522 | { MATROSKA_ID_VIDEOALPHAMODE, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, alpha_mode), { .u = 0 } }, |
| 523 | { MATROSKA_ID_VIDEOCOLOR, EBML_NEST, 0, sizeof(MatroskaTrackVideoColor), offsetof(MatroskaTrackVideo, color), { .n = matroska_track_video_color } }, |
| 524 | { MATROSKA_ID_VIDEOPROJECTION, EBML_NEST, 0, 0, offsetof(MatroskaTrackVideo, projection), { .n = matroska_track_video_projection } }, |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 525 | { MATROSKA_ID_VIDEOPIXELCROPB, EBML_NONE }, |
| 526 | { MATROSKA_ID_VIDEOPIXELCROPT, EBML_NONE }, |
| 527 | { MATROSKA_ID_VIDEOPIXELCROPL, EBML_NONE }, |
| 528 | { MATROSKA_ID_VIDEOPIXELCROPR, EBML_NONE }, |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 529 | { MATROSKA_ID_VIDEODISPLAYUNIT, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, display_unit), { .u= MATROSKA_VIDEO_DISPLAYUNIT_PIXELS } }, |
| 530 | { MATROSKA_ID_VIDEOFLAGINTERLACED, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, interlaced), { .u = MATROSKA_VIDEO_INTERLACE_FLAG_UNDETERMINED } }, |
| 531 | { MATROSKA_ID_VIDEOFIELDORDER, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, field_order), { .u = MATROSKA_VIDEO_FIELDORDER_UNDETERMINED } }, |
| 532 | { MATROSKA_ID_VIDEOSTEREOMODE, EBML_UINT, 0, 0, offsetof(MatroskaTrackVideo, stereo_mode), { .u = MATROSKA_VIDEO_STEREOMODE_TYPE_NB } }, |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 533 | { MATROSKA_ID_VIDEOASPECTRATIO, EBML_NONE }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 534 | CHILD_OF(matroska_track) |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 535 | }; |
| 536 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 537 | static EbmlSyntax matroska_track_audio[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 538 | { MATROSKA_ID_AUDIOSAMPLINGFREQ, EBML_FLOAT, 0, 0, offsetof(MatroskaTrackAudio, samplerate), { .f = 8000.0 } }, |
| 539 | { MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, EBML_FLOAT, 0, 0, offsetof(MatroskaTrackAudio, out_samplerate) }, |
| 540 | { MATROSKA_ID_AUDIOBITDEPTH, EBML_UINT, 0, 0, offsetof(MatroskaTrackAudio, bitdepth) }, |
| 541 | { MATROSKA_ID_AUDIOCHANNELS, EBML_UINT, 0, 0, offsetof(MatroskaTrackAudio, channels), { .u = 1 } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 542 | CHILD_OF(matroska_track) |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 543 | }; |
| 544 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 545 | static EbmlSyntax matroska_track_encoding_compression[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 546 | { MATROSKA_ID_ENCODINGCOMPALGO, EBML_UINT, 0, 0, offsetof(MatroskaTrackCompression, algo), { .u = MATROSKA_TRACK_ENCODING_COMP_ZLIB } }, |
| 547 | { MATROSKA_ID_ENCODINGCOMPSETTINGS, EBML_BIN, 0, 0, offsetof(MatroskaTrackCompression, settings) }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 548 | CHILD_OF(matroska_track_encoding) |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 549 | }; |
| 550 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 551 | static EbmlSyntax matroska_track_encoding_encryption[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 552 | { MATROSKA_ID_ENCODINGENCALGO, EBML_UINT, 0, 0, offsetof(MatroskaTrackEncryption,algo), {.u = 0} }, |
| 553 | { MATROSKA_ID_ENCODINGENCKEYID, EBML_BIN, 0, 0, offsetof(MatroskaTrackEncryption,key_id) }, |
Frank Galligan | b853103 | 2013-03-07 16:11:38 | [diff] [blame] | 554 | { MATROSKA_ID_ENCODINGENCAESSETTINGS, EBML_NONE }, |
| 555 | { MATROSKA_ID_ENCODINGSIGALGO, EBML_NONE }, |
| 556 | { MATROSKA_ID_ENCODINGSIGHASHALGO, EBML_NONE }, |
| 557 | { MATROSKA_ID_ENCODINGSIGKEYID, EBML_NONE }, |
| 558 | { MATROSKA_ID_ENCODINGSIGNATURE, EBML_NONE }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 559 | CHILD_OF(matroska_track_encoding) |
Frank Galligan | b853103 | 2013-03-07 16:11:38 | [diff] [blame] | 560 | }; |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 561 | static EbmlSyntax matroska_track_encoding[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 562 | { MATROSKA_ID_ENCODINGSCOPE, EBML_UINT, 0, 0, offsetof(MatroskaTrackEncoding, scope), { .u = 1 } }, |
| 563 | { MATROSKA_ID_ENCODINGTYPE, EBML_UINT, 0, 0, offsetof(MatroskaTrackEncoding, type), { .u = 0 } }, |
| 564 | { MATROSKA_ID_ENCODINGCOMPRESSION, EBML_NEST, 0, 0, offsetof(MatroskaTrackEncoding, compression), { .n = matroska_track_encoding_compression } }, |
| 565 | { MATROSKA_ID_ENCODINGENCRYPTION, EBML_NEST, 0, 0, offsetof(MatroskaTrackEncoding, encryption), { .n = matroska_track_encoding_encryption } }, |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 566 | { MATROSKA_ID_ENCODINGORDER, EBML_NONE }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 567 | CHILD_OF(matroska_track_encodings) |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 568 | }; |
| 569 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 570 | static EbmlSyntax matroska_track_encodings[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 571 | { MATROSKA_ID_TRACKCONTENTENCODING, EBML_NEST, 0, sizeof(MatroskaTrackEncoding), offsetof(MatroskaTrack, encodings), { .n = matroska_track_encoding } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 572 | CHILD_OF(matroska_track) |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 573 | }; |
| 574 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 575 | static EbmlSyntax matroska_track_plane[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 576 | { MATROSKA_ID_TRACKPLANEUID, EBML_UINT, 0, 0, offsetof(MatroskaTrackPlane,uid) }, |
| 577 | { MATROSKA_ID_TRACKPLANETYPE, EBML_UINT, 0, 0, offsetof(MatroskaTrackPlane,type) }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 578 | CHILD_OF(matroska_track_combine_planes) |
Kirill Gavrilov | e6ec921 | 2011-05-21 15:14:14 | [diff] [blame] | 579 | }; |
| 580 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 581 | static EbmlSyntax matroska_track_combine_planes[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 582 | { MATROSKA_ID_TRACKPLANE, EBML_NEST, 0, sizeof(MatroskaTrackPlane), offsetof(MatroskaTrackOperation,combine_planes), {.n = matroska_track_plane} }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 583 | CHILD_OF(matroska_track_operation) |
Kirill Gavrilov | e6ec921 | 2011-05-21 15:14:14 | [diff] [blame] | 584 | }; |
| 585 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 586 | static EbmlSyntax matroska_track_operation[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 587 | { MATROSKA_ID_TRACKCOMBINEPLANES, EBML_NEST, 0, 0, 0, {.n = matroska_track_combine_planes} }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 588 | CHILD_OF(matroska_track) |
Kirill Gavrilov | e6ec921 | 2011-05-21 15:14:14 | [diff] [blame] | 589 | }; |
| 590 | |
quietvoid | 05c0794 | 2022-01-01 16:51:50 | [diff] [blame] | 591 | static EbmlSyntax matroska_block_addition_mapping[] = { |
| 592 | { MATROSKA_ID_BLKADDIDVALUE, EBML_UINT, 0, 0, offsetof(MatroskaBlockAdditionMapping, value) }, |
| 593 | { MATROSKA_ID_BLKADDIDNAME, EBML_STR, 0, 0, offsetof(MatroskaBlockAdditionMapping, name) }, |
| 594 | { MATROSKA_ID_BLKADDIDTYPE, EBML_UINT, 0, 0, offsetof(MatroskaBlockAdditionMapping, type) }, |
| 595 | { MATROSKA_ID_BLKADDIDEXTRADATA, EBML_BIN, 0, 0, offsetof(MatroskaBlockAdditionMapping, extradata) }, |
| 596 | CHILD_OF(matroska_track) |
| 597 | }; |
| 598 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 599 | static EbmlSyntax matroska_track[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 600 | { MATROSKA_ID_TRACKNUMBER, EBML_UINT, 0, 0, offsetof(MatroskaTrack, num) }, |
| 601 | { MATROSKA_ID_TRACKNAME, EBML_UTF8, 0, 0, offsetof(MatroskaTrack, name) }, |
| 602 | { MATROSKA_ID_TRACKUID, EBML_UINT, 0, 0, offsetof(MatroskaTrack, uid) }, |
| 603 | { MATROSKA_ID_TRACKTYPE, EBML_UINT, 0, 0, offsetof(MatroskaTrack, type) }, |
| 604 | { MATROSKA_ID_CODECID, EBML_STR, 0, 0, offsetof(MatroskaTrack, codec_id) }, |
| 605 | { MATROSKA_ID_CODECPRIVATE, EBML_BIN, 0, 0, offsetof(MatroskaTrack, codec_priv) }, |
| 606 | { MATROSKA_ID_CODECDELAY, EBML_UINT, 0, 0, offsetof(MatroskaTrack, codec_delay), { .u = 0 } }, |
| 607 | { MATROSKA_ID_TRACKLANGUAGE, EBML_STR, 0, 0, offsetof(MatroskaTrack, language), { .s = "eng" } }, |
| 608 | { MATROSKA_ID_TRACKDEFAULTDURATION, EBML_UINT, 0, 0, offsetof(MatroskaTrack, default_duration) }, |
| 609 | { MATROSKA_ID_TRACKTIMECODESCALE, EBML_FLOAT, 0, 0, offsetof(MatroskaTrack, time_scale), { .f = 1.0 } }, |
Andreas Rheinhardt | 18b3dee | 2021-02-16 17:23:59 | [diff] [blame] | 610 | { MATROSKA_ID_TRACKFLAGCOMMENTARY, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_comment), { .u = 0 } }, |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 611 | { MATROSKA_ID_TRACKFLAGDEFAULT, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_default), { .u = 1 } }, |
| 612 | { MATROSKA_ID_TRACKFLAGFORCED, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_forced), { .u = 0 } }, |
Andreas Rheinhardt | 51e1067 | 2021-02-16 18:08:51 | [diff] [blame] | 613 | { MATROSKA_ID_TRACKFLAGHEARINGIMPAIRED, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_hearingimpaired), { .u = 0 } }, |
| 614 | { MATROSKA_ID_TRACKFLAGVISUALIMPAIRED, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_visualimpaired), { .u = 0 } }, |
Andreas Rheinhardt | 48cf1d8 | 2021-02-16 18:38:19 | [diff] [blame] | 615 | { MATROSKA_ID_TRACKFLAGTEXTDESCRIPTIONS, EBML_UINT, 0, 0, offsetof(MatroskaTrack, flag_textdescriptions), { .u = 0 } }, |
Andreas Rheinhardt | b243be8 | 2021-02-16 17:41:30 | [diff] [blame] | 616 | { MATROSKA_ID_TRACKFLAGORIGINAL, EBML_UINT, 1, 0, offsetof(MatroskaTrack, flag_original), {.u = 0 } }, |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 617 | { MATROSKA_ID_TRACKVIDEO, EBML_NEST, 0, 0, offsetof(MatroskaTrack, video), { .n = matroska_track_video } }, |
| 618 | { MATROSKA_ID_TRACKAUDIO, EBML_NEST, 0, 0, offsetof(MatroskaTrack, audio), { .n = matroska_track_audio } }, |
| 619 | { MATROSKA_ID_TRACKOPERATION, EBML_NEST, 0, 0, offsetof(MatroskaTrack, operation), { .n = matroska_track_operation } }, |
| 620 | { MATROSKA_ID_TRACKCONTENTENCODINGS, EBML_NEST, 0, 0, 0, { .n = matroska_track_encodings } }, |
| 621 | { MATROSKA_ID_TRACKMAXBLKADDID, EBML_UINT, 0, 0, offsetof(MatroskaTrack, max_block_additional_id), { .u = 0 } }, |
quietvoid | 05c0794 | 2022-01-01 16:51:50 | [diff] [blame] | 622 | { MATROSKA_ID_TRACKBLKADDMAPPING, EBML_NEST, 0, sizeof(MatroskaBlockAdditionMapping), offsetof(MatroskaTrack, block_addition_mappings), { .n = matroska_block_addition_mapping } }, |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 623 | { MATROSKA_ID_SEEKPREROLL, EBML_UINT, 0, 0, offsetof(MatroskaTrack, seek_preroll), { .u = 0 } }, |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 624 | { MATROSKA_ID_TRACKFLAGENABLED, EBML_NONE }, |
| 625 | { MATROSKA_ID_TRACKFLAGLACING, EBML_NONE }, |
| 626 | { MATROSKA_ID_CODECNAME, EBML_NONE }, |
| 627 | { MATROSKA_ID_CODECDECODEALL, EBML_NONE }, |
| 628 | { MATROSKA_ID_CODECINFOURL, EBML_NONE }, |
| 629 | { MATROSKA_ID_CODECDOWNLOADURL, EBML_NONE }, |
| 630 | { MATROSKA_ID_TRACKMINCACHE, EBML_NONE }, |
| 631 | { MATROSKA_ID_TRACKMAXCACHE, EBML_NONE }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 632 | CHILD_OF(matroska_tracks) |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 633 | }; |
| 634 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 635 | static EbmlSyntax matroska_tracks[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 636 | { MATROSKA_ID_TRACKENTRY, EBML_NEST, 0, sizeof(MatroskaTrack), offsetof(MatroskaDemuxContext, tracks), { .n = matroska_track } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 637 | CHILD_OF(matroska_segment) |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 638 | }; |
| 639 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 640 | static EbmlSyntax matroska_attachment[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 641 | { MATROSKA_ID_FILEUID, EBML_UINT, 0, 0, offsetof(MatroskaAttachment, uid) }, |
| 642 | { MATROSKA_ID_FILENAME, EBML_UTF8, 0, 0, offsetof(MatroskaAttachment, filename) }, |
| 643 | { MATROSKA_ID_FILEMIMETYPE, EBML_STR, 0, 0, offsetof(MatroskaAttachment, mime) }, |
| 644 | { MATROSKA_ID_FILEDATA, EBML_BIN, 0, 0, offsetof(MatroskaAttachment, bin) }, |
| 645 | { MATROSKA_ID_FILEDESC, EBML_UTF8, 0, 0, offsetof(MatroskaAttachment, description) }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 646 | CHILD_OF(matroska_attachments) |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 647 | }; |
| 648 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 649 | static EbmlSyntax matroska_attachments[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 650 | { MATROSKA_ID_ATTACHEDFILE, EBML_NEST, 0, sizeof(MatroskaAttachment), offsetof(MatroskaDemuxContext, attachments), { .n = matroska_attachment } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 651 | CHILD_OF(matroska_segment) |
Aurelien Jacobs | b414cb8 | 2008-08-05 00:40:24 | [diff] [blame] | 652 | }; |
| 653 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 654 | static EbmlSyntax matroska_chapter_display[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 655 | { MATROSKA_ID_CHAPSTRING, EBML_UTF8, 0, 0, offsetof(MatroskaChapter, title) }, |
Rodger Combs | cf2719a | 2015-09-20 14:34:05 | [diff] [blame] | 656 | { MATROSKA_ID_CHAPLANG, EBML_NONE }, |
| 657 | { MATROSKA_ID_CHAPCOUNTRY, EBML_NONE }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 658 | CHILD_OF(matroska_chapter_entry) |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 659 | }; |
| 660 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 661 | static EbmlSyntax matroska_chapter_entry[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 662 | { MATROSKA_ID_CHAPTERTIMESTART, EBML_UINT, 0, 0, offsetof(MatroskaChapter, start), { .u = AV_NOPTS_VALUE } }, |
| 663 | { MATROSKA_ID_CHAPTERTIMEEND, EBML_UINT, 0, 0, offsetof(MatroskaChapter, end), { .u = AV_NOPTS_VALUE } }, |
| 664 | { MATROSKA_ID_CHAPTERUID, EBML_UINT, 0, 0, offsetof(MatroskaChapter, uid) }, |
| 665 | { MATROSKA_ID_CHAPTERDISPLAY, EBML_NEST, 0, 0, 0, { .n = matroska_chapter_display } }, |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 666 | { MATROSKA_ID_CHAPTERFLAGHIDDEN, EBML_NONE }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 667 | { MATROSKA_ID_CHAPTERFLAGENABLED, EBML_NONE }, |
| 668 | { MATROSKA_ID_CHAPTERPHYSEQUIV, EBML_NONE }, |
| 669 | { MATROSKA_ID_CHAPTERATOM, EBML_NONE }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 670 | CHILD_OF(matroska_chapter) |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 671 | }; |
| 672 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 673 | static EbmlSyntax matroska_chapter[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 674 | { MATROSKA_ID_CHAPTERATOM, EBML_NEST, 0, sizeof(MatroskaChapter), offsetof(MatroskaDemuxContext, chapters), { .n = matroska_chapter_entry } }, |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 675 | { MATROSKA_ID_EDITIONUID, EBML_NONE }, |
| 676 | { MATROSKA_ID_EDITIONFLAGHIDDEN, EBML_NONE }, |
| 677 | { MATROSKA_ID_EDITIONFLAGDEFAULT, EBML_NONE }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 678 | { MATROSKA_ID_EDITIONFLAGORDERED, EBML_NONE }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 679 | CHILD_OF(matroska_chapters) |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 680 | }; |
| 681 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 682 | static EbmlSyntax matroska_chapters[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 683 | { MATROSKA_ID_EDITIONENTRY, EBML_NEST, 0, 0, 0, { .n = matroska_chapter } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 684 | CHILD_OF(matroska_segment) |
Aurelien Jacobs | 6bbd7c7 | 2008-08-05 00:40:21 | [diff] [blame] | 685 | }; |
| 686 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 687 | static EbmlSyntax matroska_index_pos[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 688 | { MATROSKA_ID_CUETRACK, EBML_UINT, 0, 0, offsetof(MatroskaIndexPos, track) }, |
| 689 | { MATROSKA_ID_CUECLUSTERPOSITION, EBML_UINT, 0, 0, offsetof(MatroskaIndexPos, pos) }, |
James Almer | 5ab7b3b | 2013-09-15 09:32:36 | [diff] [blame] | 690 | { MATROSKA_ID_CUERELATIVEPOSITION,EBML_NONE }, |
James Almer | 56f1740 | 2013-09-21 01:15:49 | [diff] [blame] | 691 | { MATROSKA_ID_CUEDURATION, EBML_NONE }, |
Aurelien Jacobs | 5df3cc6 | 2008-08-13 21:15:15 | [diff] [blame] | 692 | { MATROSKA_ID_CUEBLOCKNUMBER, EBML_NONE }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 693 | CHILD_OF(matroska_index_entry) |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 694 | }; |
| 695 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 696 | static EbmlSyntax matroska_index_entry[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 697 | { MATROSKA_ID_CUETIME, EBML_UINT, 0, 0, offsetof(MatroskaIndex, time) }, |
| 698 | { MATROSKA_ID_CUETRACKPOSITION, EBML_NEST, 0, sizeof(MatroskaIndexPos), offsetof(MatroskaIndex, pos), { .n = matroska_index_pos } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 699 | CHILD_OF(matroska_index) |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 700 | }; |
| 701 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 702 | static EbmlSyntax matroska_index[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 703 | { MATROSKA_ID_POINTENTRY, EBML_NEST, 0, sizeof(MatroskaIndex), offsetof(MatroskaDemuxContext, index), { .n = matroska_index_entry } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 704 | CHILD_OF(matroska_segment) |
Aurelien Jacobs | e5929fd | 2008-08-05 00:40:15 | [diff] [blame] | 705 | }; |
| 706 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 707 | static EbmlSyntax matroska_simpletag[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 708 | { MATROSKA_ID_TAGNAME, EBML_UTF8, 0, 0, offsetof(MatroskaTag, name) }, |
| 709 | { MATROSKA_ID_TAGSTRING, EBML_UTF8, 0, 0, offsetof(MatroskaTag, string) }, |
| 710 | { MATROSKA_ID_TAGLANG, EBML_STR, 0, 0, offsetof(MatroskaTag, lang), { .s = "und" } }, |
| 711 | { MATROSKA_ID_TAGDEFAULT, EBML_UINT, 0, 0, offsetof(MatroskaTag, def) }, |
| 712 | { MATROSKA_ID_TAGDEFAULT_BUG, EBML_UINT, 0, 0, offsetof(MatroskaTag, def) }, |
| 713 | { MATROSKA_ID_SIMPLETAG, EBML_NEST, 0, sizeof(MatroskaTag), offsetof(MatroskaTag, sub), { .n = matroska_simpletag } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 714 | CHILD_OF(matroska_tag) |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 715 | }; |
| 716 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 717 | static EbmlSyntax matroska_tagtargets[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 718 | { MATROSKA_ID_TAGTARGETS_TYPE, EBML_STR, 0, 0, offsetof(MatroskaTagTarget, type) }, |
| 719 | { MATROSKA_ID_TAGTARGETS_TYPEVALUE, EBML_UINT, 0, 0, offsetof(MatroskaTagTarget, typevalue), { .u = 50 } }, |
| 720 | { MATROSKA_ID_TAGTARGETS_TRACKUID, EBML_UINT, 0, 0, offsetof(MatroskaTagTarget, trackuid), { .u = 0 } }, |
| 721 | { MATROSKA_ID_TAGTARGETS_CHAPTERUID, EBML_UINT, 0, 0, offsetof(MatroskaTagTarget, chapteruid), { .u = 0 } }, |
| 722 | { MATROSKA_ID_TAGTARGETS_ATTACHUID, EBML_UINT, 0, 0, offsetof(MatroskaTagTarget, attachuid), { .u = 0 } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 723 | CHILD_OF(matroska_tag) |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 724 | }; |
| 725 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 726 | static EbmlSyntax matroska_tag[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 727 | { MATROSKA_ID_SIMPLETAG, EBML_NEST, 0, sizeof(MatroskaTag), offsetof(MatroskaTags, tag), { .n = matroska_simpletag } }, |
| 728 | { MATROSKA_ID_TAGTARGETS, EBML_NEST, 0, 0, offsetof(MatroskaTags, target), { .n = matroska_tagtargets } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 729 | CHILD_OF(matroska_tags) |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 730 | }; |
| 731 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 732 | static EbmlSyntax matroska_tags[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 733 | { MATROSKA_ID_TAG, EBML_NEST, 0, sizeof(MatroskaTags), offsetof(MatroskaDemuxContext, tags), { .n = matroska_tag } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 734 | CHILD_OF(matroska_segment) |
Aurelien Jacobs | 434d496 | 2008-08-05 00:40:18 | [diff] [blame] | 735 | }; |
| 736 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 737 | static EbmlSyntax matroska_seekhead_entry[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 738 | { MATROSKA_ID_SEEKID, EBML_UINT, 0, 0, offsetof(MatroskaSeekhead, id) }, |
| 739 | { MATROSKA_ID_SEEKPOSITION, EBML_UINT, 0, 0, offsetof(MatroskaSeekhead, pos), { .u = -1 } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 740 | CHILD_OF(matroska_seekhead) |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 741 | }; |
| 742 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 743 | static EbmlSyntax matroska_seekhead[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 744 | { MATROSKA_ID_SEEKENTRY, EBML_NEST, 0, sizeof(MatroskaSeekhead), offsetof(MatroskaDemuxContext, seekhead), { .n = matroska_seekhead_entry } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 745 | CHILD_OF(matroska_segment) |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 746 | }; |
| 747 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 748 | static EbmlSyntax matroska_segment[] = { |
Andreas Rheinhardt | 71c9088 | 2019-05-16 22:30:18 | [diff] [blame] | 749 | { MATROSKA_ID_CLUSTER, EBML_STOP }, |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 750 | { MATROSKA_ID_INFO, EBML_LEVEL1, 0, 0, 0, { .n = matroska_info } }, |
| 751 | { MATROSKA_ID_TRACKS, EBML_LEVEL1, 0, 0, 0, { .n = matroska_tracks } }, |
| 752 | { MATROSKA_ID_ATTACHMENTS, EBML_LEVEL1, 0, 0, 0, { .n = matroska_attachments } }, |
| 753 | { MATROSKA_ID_CHAPTERS, EBML_LEVEL1, 0, 0, 0, { .n = matroska_chapters } }, |
| 754 | { MATROSKA_ID_CUES, EBML_LEVEL1, 0, 0, 0, { .n = matroska_index } }, |
| 755 | { MATROSKA_ID_TAGS, EBML_LEVEL1, 0, 0, 0, { .n = matroska_tags } }, |
| 756 | { MATROSKA_ID_SEEKHEAD, EBML_LEVEL1, 0, 0, 0, { .n = matroska_seekhead } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 757 | { 0 } /* We don't want to go back to level 0, so don't add the parent. */ |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 758 | }; |
| 759 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 760 | static EbmlSyntax matroska_segments[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 761 | { MATROSKA_ID_SEGMENT, EBML_NEST, 0, 0, 0, { .n = matroska_segment } }, |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 762 | { 0 } |
| 763 | }; |
| 764 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 765 | static EbmlSyntax matroska_blockmore[] = { |
James Almer | 4451315 | 2023-03-12 18:02:06 | [diff] [blame^] | 766 | { MATROSKA_ID_BLOCKADDID, EBML_UINT, 0, 0, offsetof(MatroskaBlockMore,additional_id), { .u = 1 } }, |
| 767 | { MATROSKA_ID_BLOCKADDITIONAL, EBML_BIN, 0, 0, offsetof(MatroskaBlockMore,additional) }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 768 | CHILD_OF(matroska_blockadditions) |
Vignesh Venkatasubramanian | 30c5c45 | 2013-02-13 21:51:48 | [diff] [blame] | 769 | }; |
| 770 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 771 | static EbmlSyntax matroska_blockadditions[] = { |
James Almer | 4451315 | 2023-03-12 18:02:06 | [diff] [blame^] | 772 | { MATROSKA_ID_BLOCKMORE, EBML_NEST, 0, sizeof(MatroskaBlockMore), offsetof(MatroskaBlock, blockmore), { .n = matroska_blockmore } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 773 | CHILD_OF(matroska_blockgroup) |
Vignesh Venkatasubramanian | 30c5c45 | 2013-02-13 21:51:48 | [diff] [blame] | 774 | }; |
| 775 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 776 | static EbmlSyntax matroska_blockgroup[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 777 | { MATROSKA_ID_BLOCK, EBML_BIN, 0, 0, offsetof(MatroskaBlock, bin) }, |
| 778 | { MATROSKA_ID_BLOCKADDITIONS, EBML_NEST, 0, 0, 0, { .n = matroska_blockadditions} }, |
| 779 | { MATROSKA_ID_BLOCKDURATION, EBML_UINT, 0, 0, offsetof(MatroskaBlock, duration) }, |
| 780 | { MATROSKA_ID_DISCARDPADDING, EBML_SINT, 0, 0, offsetof(MatroskaBlock, discard_padding) }, |
Andreas Rheinhardt | 2f1a562 | 2021-02-16 15:18:02 | [diff] [blame] | 781 | { MATROSKA_ID_BLOCKREFERENCE, EBML_SINT, 1, 0, offsetof(MatroskaBlock, reference) }, |
Anton Khirnov | 564b7e0 | 2013-05-15 13:48:15 | [diff] [blame] | 782 | { MATROSKA_ID_CODECSTATE, EBML_NONE }, |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 783 | { 1, EBML_UINT, 0, 0, offsetof(MatroskaBlock, non_simple), { .u = 1 } }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 784 | CHILD_OF(matroska_cluster_parsing) |
Aurelien Jacobs | 209472b | 2008-08-05 00:41:05 | [diff] [blame] | 785 | }; |
| 786 | |
Andreas Rheinhardt | 38255cd | 2019-05-16 22:30:10 | [diff] [blame] | 787 | // The following array contains SimpleBlock and BlockGroup twice |
| 788 | // in order to reuse the other values for matroska_cluster_enter. |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 789 | static EbmlSyntax matroska_cluster_parsing[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 790 | { MATROSKA_ID_SIMPLEBLOCK, EBML_BIN, 0, 0, offsetof(MatroskaBlock, bin) }, |
| 791 | { MATROSKA_ID_BLOCKGROUP, EBML_NEST, 0, 0, 0, { .n = matroska_blockgroup } }, |
| 792 | { MATROSKA_ID_CLUSTERTIMECODE, EBML_UINT, 0, 0, offsetof(MatroskaCluster, timecode) }, |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 793 | { MATROSKA_ID_SIMPLEBLOCK, EBML_STOP }, |
Andreas Rheinhardt | 38255cd | 2019-05-16 22:30:10 | [diff] [blame] | 794 | { MATROSKA_ID_BLOCKGROUP, EBML_STOP }, |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 795 | { MATROSKA_ID_CLUSTERPOSITION, EBML_NONE }, |
| 796 | { MATROSKA_ID_CLUSTERPREVSIZE, EBML_NONE }, |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 797 | CHILD_OF(matroska_segment) |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 798 | }; |
| 799 | |
Andreas Rheinhardt | 9869e21 | 2019-07-18 19:07:20 | [diff] [blame] | 800 | static EbmlSyntax matroska_cluster_enter[] = { |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 801 | { MATROSKA_ID_CLUSTER, EBML_NEST, 0, 0, 0, { .n = &matroska_cluster_parsing[2] } }, |
Andreas Rheinhardt | 865c537 | 2019-05-16 22:30:08 | [diff] [blame] | 802 | { 0 } |
| 803 | }; |
Andreas Rheinhardt | c1abd95 | 2019-05-16 22:30:06 | [diff] [blame] | 804 | #undef CHILD_OF |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 805 | |
Andreas Rheinhardt | 67e957b | 2020-04-16 01:39:05 | [diff] [blame] | 806 | static const CodecMime mkv_image_mime_tags[] = { |
| 807 | {"image/gif" , AV_CODEC_ID_GIF}, |
| 808 | {"image/jpeg" , AV_CODEC_ID_MJPEG}, |
| 809 | {"image/png" , AV_CODEC_ID_PNG}, |
| 810 | {"image/tiff" , AV_CODEC_ID_TIFF}, |
| 811 | |
| 812 | {"" , AV_CODEC_ID_NONE} |
| 813 | }; |
| 814 | |
| 815 | static const CodecMime mkv_mime_tags[] = { |
Andreas Rheinhardt | 67e957b | 2020-04-16 01:39:05 | [diff] [blame] | 816 | {"application/x-truetype-font", AV_CODEC_ID_TTF}, |
| 817 | {"application/x-font" , AV_CODEC_ID_TTF}, |
| 818 | {"application/vnd.ms-opentype", AV_CODEC_ID_OTF}, |
| 819 | {"binary" , AV_CODEC_ID_BIN_DATA}, |
| 820 | |
| 821 | {"" , AV_CODEC_ID_NONE} |
| 822 | }; |
| 823 | |
Alex Converse | b0f29db | 2012-02-20 08:42:33 | [diff] [blame] | 824 | static const char *const matroska_doctypes[] = { "matroska", "webm" }; |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 825 | |
Andreas Rheinhardt | 310f326 | 2019-05-16 22:30:02 | [diff] [blame] | 826 | /* |
| 827 | * This function prepares the status for parsing of level 1 elements. |
| 828 | */ |
| 829 | static int matroska_reset_status(MatroskaDemuxContext *matroska, |
| 830 | uint32_t id, int64_t position) |
| 831 | { |
Michael Niedermayer | d115eec | 2021-06-24 17:53:47 | [diff] [blame] | 832 | int64_t err = 0; |
Andreas Rheinhardt | 310f326 | 2019-05-16 22:30:02 | [diff] [blame] | 833 | if (position >= 0) { |
Michael Niedermayer | d115eec | 2021-06-24 17:53:47 | [diff] [blame] | 834 | err = avio_seek(matroska->ctx->pb, position, SEEK_SET); |
| 835 | if (err > 0) |
| 836 | err = 0; |
| 837 | } else |
| 838 | position = avio_tell(matroska->ctx->pb); |
Andreas Rheinhardt | 310f326 | 2019-05-16 22:30:02 | [diff] [blame] | 839 | |
Andreas Rheinhardt | 5120305 | 2019-05-16 22:30:14 | [diff] [blame] | 840 | matroska->current_id = id; |
| 841 | matroska->num_levels = 1; |
| 842 | matroska->unknown_count = 0; |
Michael Niedermayer | d115eec | 2021-06-24 17:53:47 | [diff] [blame] | 843 | matroska->resync_pos = position; |
Andreas Rheinhardt | a3db9f6 | 2019-05-16 22:30:05 | [diff] [blame] | 844 | if (id) |
| 845 | matroska->resync_pos -= (av_log2(id) + 7) / 8; |
Andreas Rheinhardt | 310f326 | 2019-05-16 22:30:02 | [diff] [blame] | 846 | |
Michael Niedermayer | d115eec | 2021-06-24 17:53:47 | [diff] [blame] | 847 | return err; |
Andreas Rheinhardt | 310f326 | 2019-05-16 22:30:02 | [diff] [blame] | 848 | } |
| 849 | |
Reimar Döffinger | d493170 | 2012-02-13 22:06:19 | [diff] [blame] | 850 | static int matroska_resync(MatroskaDemuxContext *matroska, int64_t last_pos) |
| 851 | { |
| 852 | AVIOContext *pb = matroska->ctx->pb; |
| 853 | uint32_t id; |
Reimar Döffinger | d493170 | 2012-02-13 22:06:19 | [diff] [blame] | 854 | |
Andreas Rheinhardt | 27f40b1 | 2019-05-16 22:30:01 | [diff] [blame] | 855 | /* Try to seek to the last position to resync from. If this doesn't work, |
| 856 | * we resync from the earliest position available: The start of the buffer. */ |
| 857 | if (last_pos < avio_tell(pb) && avio_seek(pb, last_pos + 1, SEEK_SET) < 0) { |
| 858 | av_log(matroska->ctx, AV_LOG_WARNING, |
| 859 | "Seek to desired resync point failed. Seeking to " |
| 860 | "earliest point available instead.\n"); |
| 861 | avio_seek(pb, FFMAX(avio_tell(pb) + (pb->buffer - pb->buf_ptr), |
| 862 | last_pos + 1), SEEK_SET); |
Sophia Wang | 8c83062 | 2016-09-27 19:00:29 | [diff] [blame] | 863 | } |
Reimar Döffinger | d493170 | 2012-02-13 22:06:19 | [diff] [blame] | 864 | |
| 865 | id = avio_rb32(pb); |
| 866 | |
| 867 | // try to find a toplevel element |
James Almer | d34ec64 | 2014-08-07 20:12:41 | [diff] [blame] | 868 | while (!avio_feof(pb)) { |
Sean McGovern | 8835c55 | 2013-05-27 22:11:50 | [diff] [blame] | 869 | if (id == MATROSKA_ID_INFO || id == MATROSKA_ID_TRACKS || |
| 870 | id == MATROSKA_ID_CUES || id == MATROSKA_ID_TAGS || |
Reimar Döffinger | d493170 | 2012-02-13 22:06:19 | [diff] [blame] | 871 | id == MATROSKA_ID_SEEKHEAD || id == MATROSKA_ID_ATTACHMENTS || |
Sean McGovern | 8835c55 | 2013-05-27 22:11:50 | [diff] [blame] | 872 | id == MATROSKA_ID_CLUSTER || id == MATROSKA_ID_CHAPTERS) { |
Andreas Rheinhardt | 8a286e7 | 2019-05-16 22:30:03 | [diff] [blame] | 873 | /* Prepare the context for parsing of a level 1 element. */ |
| 874 | matroska_reset_status(matroska, id, -1); |
Michael Niedermayer | fccc37c | 2019-08-28 17:04:42 | [diff] [blame] | 875 | /* Given that we are here means that an error has occurred, |
Andreas Rheinhardt | 8a286e7 | 2019-05-16 22:30:03 | [diff] [blame] | 876 | * so treat the segment as unknown length in order not to |
| 877 | * discard valid data that happens to be beyond the designated |
| 878 | * end of the segment. */ |
| 879 | matroska->levels[0].length = EBML_UNKNOWN_LENGTH; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 880 | return 0; |
Reimar Döffinger | d493170 | 2012-02-13 22:06:19 | [diff] [blame] | 881 | } |
| 882 | id = (id << 8) | avio_r8(pb); |
| 883 | } |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 884 | |
Reimar Döffinger | d493170 | 2012-02-13 22:06:19 | [diff] [blame] | 885 | matroska->done = 1; |
Andreas Rheinhardt | 27f40b1 | 2019-05-16 22:30:01 | [diff] [blame] | 886 | return pb->error ? pb->error : AVERROR_EOF; |
Reimar Döffinger | d493170 | 2012-02-13 22:06:19 | [diff] [blame] | 887 | } |
| 888 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 889 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 890 | * Read: an "EBML number", which is defined as a variable-length |
| 891 | * array of bytes. The first byte indicates the length by giving a |
| 892 | * number of 0-bits followed by a one. The position of the first |
| 893 | * "one" bit inside the first byte indicates the length of this |
| 894 | * number. |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 895 | * Returns: number of bytes read, < 0 on error |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 896 | */ |
Anton Khirnov | 471fe57 | 2011-02-20 10:04:12 | [diff] [blame] | 897 | static int ebml_read_num(MatroskaDemuxContext *matroska, AVIOContext *pb, |
Andreas Rheinhardt | 239c736 | 2019-06-25 01:08:56 | [diff] [blame] | 898 | int max_size, uint64_t *number, int eof_forbidden) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 899 | { |
Andreas Rheinhardt | 239c736 | 2019-06-25 01:08:56 | [diff] [blame] | 900 | int read, n = 1; |
| 901 | uint64_t total; |
| 902 | int64_t pos; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 903 | |
Andreas Rheinhardt | 239c736 | 2019-06-25 01:08:56 | [diff] [blame] | 904 | /* The first byte tells us the length in bytes - except when it is zero. */ |
| 905 | total = avio_r8(pb); |
| 906 | if (pb->eof_reached) |
| 907 | goto err; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 908 | |
| 909 | /* get the length of the EBML number */ |
Reimar Döffinger | ff6a5fc | 2010-09-02 19:17:46 | [diff] [blame] | 910 | read = 8 - ff_log2_tab[total]; |
Andreas Rheinhardt | 239c736 | 2019-06-25 01:08:56 | [diff] [blame] | 911 | |
| 912 | if (!total || read > max_size) { |
| 913 | pos = avio_tell(pb) - 1; |
| 914 | if (!total) { |
| 915 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 916 | "0x00 at pos %"PRId64" (0x%"PRIx64") invalid as first byte " |
| 917 | "of an EBML number\n", pos, pos); |
| 918 | } else { |
| 919 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 920 | "Length %d indicated by an EBML number's first byte 0x%02x " |
| 921 | "at pos %"PRId64" (0x%"PRIx64") exceeds max length %d.\n", |
| 922 | read, (uint8_t) total, pos, pos, max_size); |
| 923 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 924 | return AVERROR_INVALIDDATA; |
| 925 | } |
| 926 | |
| 927 | /* read out length */ |
Reimar Döffinger | ff6a5fc | 2010-09-02 19:17:46 | [diff] [blame] | 928 | total ^= 1 << ff_log2_tab[total]; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 929 | while (n++ < read) |
Anton Khirnov | e63a362 | 2011-02-21 15:43:01 | [diff] [blame] | 930 | total = (total << 8) | avio_r8(pb); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 931 | |
Andreas Rheinhardt | 239c736 | 2019-06-25 01:08:56 | [diff] [blame] | 932 | if (pb->eof_reached) { |
| 933 | eof_forbidden = 1; |
| 934 | goto err; |
| 935 | } |
| 936 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 937 | *number = total; |
| 938 | |
| 939 | return read; |
Andreas Rheinhardt | 239c736 | 2019-06-25 01:08:56 | [diff] [blame] | 940 | |
| 941 | err: |
| 942 | pos = avio_tell(pb); |
| 943 | if (pb->error) { |
| 944 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 945 | "Read error at pos. %"PRIu64" (0x%"PRIx64")\n", |
| 946 | pos, pos); |
| 947 | return pb->error; |
| 948 | } |
| 949 | if (eof_forbidden) { |
| 950 | av_log(matroska->ctx, AV_LOG_ERROR, "File ended prematurely " |
| 951 | "at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos); |
| 952 | return AVERROR(EIO); |
| 953 | } |
| 954 | return AVERROR_EOF; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 955 | } |
| 956 | |
Reimar Döffinger | 1b4d327 | 2010-09-06 17:51:44 | [diff] [blame] | 957 | /** |
| 958 | * Read a EBML length value. |
| 959 | * This needs special handling for the "unknown length" case which has multiple |
| 960 | * encodings. |
| 961 | */ |
Anton Khirnov | 471fe57 | 2011-02-20 10:04:12 | [diff] [blame] | 962 | static int ebml_read_length(MatroskaDemuxContext *matroska, AVIOContext *pb, |
Reimar Döffinger | 1b4d327 | 2010-09-06 17:51:44 | [diff] [blame] | 963 | uint64_t *number) |
| 964 | { |
Andreas Rheinhardt | 239c736 | 2019-06-25 01:08:56 | [diff] [blame] | 965 | int res = ebml_read_num(matroska, pb, 8, number, 1); |
Reimar Döffinger | 1b4d327 | 2010-09-06 17:51:44 | [diff] [blame] | 966 | if (res > 0 && *number + 1 == 1ULL << (7 * res)) |
Steve Lhomme | a2c7702 | 2019-02-23 10:14:33 | [diff] [blame] | 967 | *number = EBML_UNKNOWN_LENGTH; |
Reimar Döffinger | 1b4d327 | 2010-09-06 17:51:44 | [diff] [blame] | 968 | return res; |
| 969 | } |
| 970 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 971 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 972 | * Read the next element as an unsigned int. |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 973 | * Returns NEEDS_CHECKING unless size == 0. |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 974 | */ |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 975 | static int ebml_read_uint(AVIOContext *pb, int size, |
| 976 | uint64_t default_value, uint64_t *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 977 | { |
Aurelien Jacobs | c6cd2b3 | 2008-08-05 00:41:55 | [diff] [blame] | 978 | int n = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 979 | |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 980 | if (size == 0) { |
| 981 | *num = default_value; |
| 982 | return 0; |
| 983 | } |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 984 | /* big-endian ordering; build up number */ |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 985 | *num = 0; |
| 986 | while (n++ < size) |
Anton Khirnov | e63a362 | 2011-02-21 15:43:01 | [diff] [blame] | 987 | *num = (*num << 8) | avio_r8(pb); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 988 | |
Andreas Rheinhardt | a569a7b | 2019-06-23 23:42:30 | [diff] [blame] | 989 | return NEEDS_CHECKING; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /* |
Jan Gerber | d03eea3 | 2013-11-15 18:00:37 | [diff] [blame] | 993 | * Read the next element as a signed int. |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 994 | * Returns NEEDS_CHECKING unless size == 0. |
Jan Gerber | d03eea3 | 2013-11-15 18:00:37 | [diff] [blame] | 995 | */ |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 996 | static int ebml_read_sint(AVIOContext *pb, int size, |
| 997 | int64_t default_value, int64_t *num) |
Jan Gerber | d03eea3 | 2013-11-15 18:00:37 | [diff] [blame] | 998 | { |
| 999 | int n = 1; |
| 1000 | |
Jan Gerber | d03eea3 | 2013-11-15 18:00:37 | [diff] [blame] | 1001 | if (size == 0) { |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 1002 | *num = default_value; |
| 1003 | return 0; |
Jan Gerber | d03eea3 | 2013-11-15 18:00:37 | [diff] [blame] | 1004 | } else { |
Michael Niedermayer | cddd15b | 2013-11-15 20:30:30 | [diff] [blame] | 1005 | *num = sign_extend(avio_r8(pb), 8); |
Jan Gerber | d03eea3 | 2013-11-15 18:00:37 | [diff] [blame] | 1006 | |
| 1007 | /* big-endian ordering; build up number */ |
| 1008 | while (n++ < size) |
Michael Niedermayer | 2f8c816 | 2015-07-01 09:59:57 | [diff] [blame] | 1009 | *num = ((uint64_t)*num << 8) | avio_r8(pb); |
Jan Gerber | d03eea3 | 2013-11-15 18:00:37 | [diff] [blame] | 1010 | } |
| 1011 | |
Andreas Rheinhardt | a569a7b | 2019-06-23 23:42:30 | [diff] [blame] | 1012 | return NEEDS_CHECKING; |
Jan Gerber | d03eea3 | 2013-11-15 18:00:37 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1016 | * Read the next element as a float. |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 1017 | * Returns 0 if size == 0, NEEDS_CHECKING or < 0 on obvious failure. |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1018 | */ |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 1019 | static int ebml_read_float(AVIOContext *pb, int size, |
| 1020 | double default_value, double *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1021 | { |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 1022 | if (size == 0) { |
| 1023 | *num = default_value; |
| 1024 | return 0; |
| 1025 | } else if (size == 4) { |
Mans Rullgard | 3383a53 | 2011-11-27 14:04:16 | [diff] [blame] | 1026 | *num = av_int2float(avio_rb32(pb)); |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 1027 | } else if (size == 8) { |
Mans Rullgard | 3383a53 | 2011-11-27 14:04:16 | [diff] [blame] | 1028 | *num = av_int2double(avio_rb64(pb)); |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 1029 | } else |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1030 | return AVERROR_INVALIDDATA; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1031 | |
Andreas Rheinhardt | a569a7b | 2019-06-23 23:42:30 | [diff] [blame] | 1032 | return NEEDS_CHECKING; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * Read the next element as an ASCII string. |
Andreas Rheinhardt | a569a7b | 2019-06-23 23:42:30 | [diff] [blame] | 1037 | * 0 is success, < 0 or NEEDS_CHECKING is failure. |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1038 | */ |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 1039 | static int ebml_read_ascii(AVIOContext *pb, int size, |
| 1040 | const char *default_value, char **str) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1041 | { |
Ronald S. Bultje | cd40c31 | 2012-02-25 00:12:18 | [diff] [blame] | 1042 | char *res; |
Andreas Rheinhardt | a569a7b | 2019-06-23 23:42:30 | [diff] [blame] | 1043 | int ret; |
Ronald S. Bultje | cd40c31 | 2012-02-25 00:12:18 | [diff] [blame] | 1044 | |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 1045 | if (size == 0 && default_value) { |
| 1046 | res = av_strdup(default_value); |
| 1047 | if (!res) |
| 1048 | return AVERROR(ENOMEM); |
| 1049 | } else { |
Andreas Rheinhardt | 7471f47 | 2021-02-16 16:14:03 | [diff] [blame] | 1050 | /* EBML strings are usually not 0-terminated, so we allocate one |
| 1051 | * byte more, read the string and NUL-terminate it ourselves. */ |
| 1052 | if (!(res = av_malloc(size + 1))) |
| 1053 | return AVERROR(ENOMEM); |
| 1054 | if ((ret = avio_read(pb, (uint8_t *) res, size)) != size) { |
| 1055 | av_free(res); |
| 1056 | return ret < 0 ? ret : NEEDS_CHECKING; |
| 1057 | } |
| 1058 | (res)[size] = '\0'; |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 1059 | } |
Ronald S. Bultje | cd40c31 | 2012-02-25 00:12:18 | [diff] [blame] | 1060 | av_free(*str); |
| 1061 | *str = res; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1062 | |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
| 1066 | /* |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1067 | * Read the next element as binary data. |
Andreas Rheinhardt | a569a7b | 2019-06-23 23:42:30 | [diff] [blame] | 1068 | * 0 is success, < 0 or NEEDS_CHECKING is failure. |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1069 | */ |
Andreas Rheinhardt | 7087fc9 | 2019-05-16 22:30:12 | [diff] [blame] | 1070 | static int ebml_read_binary(AVIOContext *pb, int length, |
| 1071 | int64_t pos, EbmlBin *bin) |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1072 | { |
James Almer | a618866 | 2018-04-04 17:12:54 | [diff] [blame] | 1073 | int ret; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1074 | |
James Almer | a618866 | 2018-04-04 17:12:54 | [diff] [blame] | 1075 | ret = av_buffer_realloc(&bin->buf, length + AV_INPUT_BUFFER_PADDING_SIZE); |
| 1076 | if (ret < 0) |
| 1077 | return ret; |
| 1078 | memset(bin->buf->data + length, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
| 1079 | |
| 1080 | bin->data = bin->buf->data; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1081 | bin->size = length; |
Andreas Rheinhardt | 7087fc9 | 2019-05-16 22:30:12 | [diff] [blame] | 1082 | bin->pos = pos; |
Andreas Rheinhardt | a569a7b | 2019-06-23 23:42:30 | [diff] [blame] | 1083 | if ((ret = avio_read(pb, bin->data, length)) != length) { |
James Almer | a618866 | 2018-04-04 17:12:54 | [diff] [blame] | 1084 | av_buffer_unref(&bin->buf); |
| 1085 | bin->data = NULL; |
Michael Niedermayer | 5e1bacf | 2012-12-04 02:30:40 | [diff] [blame] | 1086 | bin->size = 0; |
Andreas Rheinhardt | a569a7b | 2019-06-23 23:42:30 | [diff] [blame] | 1087 | return ret < 0 ? ret : NEEDS_CHECKING; |
David Conrad | 5549aa6 | 2010-05-18 21:21:37 | [diff] [blame] | 1088 | } |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1089 | |
| 1090 | return 0; |
| 1091 | } |
| 1092 | |
| 1093 | /* |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1094 | * Read the next element, but only the header. The contents |
| 1095 | * are supposed to be sub-elements which can be read separately. |
| 1096 | * 0 is success, < 0 is failure. |
| 1097 | */ |
Andreas Rheinhardt | 7087fc9 | 2019-05-16 22:30:12 | [diff] [blame] | 1098 | static int ebml_read_master(MatroskaDemuxContext *matroska, |
| 1099 | uint64_t length, int64_t pos) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1100 | { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1101 | MatroskaLevel *level; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1102 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1103 | if (matroska->num_levels >= EBML_MAX_DEPTH) { |
| 1104 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1105 | "File moves beyond max. allowed depth (%d)\n", EBML_MAX_DEPTH); |
Panagiotis Issaris | 85565db | 2007-07-19 15:38:33 | [diff] [blame] | 1106 | return AVERROR(ENOSYS); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1107 | } |
| 1108 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1109 | level = &matroska->levels[matroska->num_levels++]; |
Andreas Rheinhardt | 7087fc9 | 2019-05-16 22:30:12 | [diff] [blame] | 1110 | level->start = pos; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1111 | level->length = length; |
| 1112 | |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | /* |
Andreas Rheinhardt | d5274f8 | 2019-12-03 17:09:10 | [diff] [blame] | 1117 | * Read a signed "EBML number" |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 1118 | * Return: number of bytes processed, < 0 on error |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1119 | */ |
Aurelien Jacobs | c1e0113 | 2008-08-05 00:42:52 | [diff] [blame] | 1120 | static int matroska_ebmlnum_sint(MatroskaDemuxContext *matroska, |
Andreas Rheinhardt | d5274f8 | 2019-12-03 17:09:10 | [diff] [blame] | 1121 | AVIOContext *pb, int64_t *num) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1122 | { |
| 1123 | uint64_t unum; |
| 1124 | int res; |
| 1125 | |
| 1126 | /* read as unsigned number first */ |
Andreas Rheinhardt | d5274f8 | 2019-12-03 17:09:10 | [diff] [blame] | 1127 | if ((res = ebml_read_num(matroska, pb, 8, &unum, 1)) < 0) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1128 | return res; |
| 1129 | |
| 1130 | /* make signed (weird way) */ |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1131 | *num = unum - ((1LL << (7 * res - 1)) - 1); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1132 | |
| 1133 | return res; |
| 1134 | } |
| 1135 | |
Andreas Rheinhardt | 559e342 | 2019-05-16 22:30:04 | [diff] [blame] | 1136 | static int ebml_parse(MatroskaDemuxContext *matroska, |
| 1137 | EbmlSyntax *syntax, void *data); |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1138 | |
Andreas Rheinhardt | 559e342 | 2019-05-16 22:30:04 | [diff] [blame] | 1139 | static EbmlSyntax *ebml_parse_id(EbmlSyntax *syntax, uint32_t id) |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1140 | { |
| 1141 | int i; |
Andreas Rheinhardt | 38255cd | 2019-05-16 22:30:10 | [diff] [blame] | 1142 | |
| 1143 | // Whoever touches this should be aware of the duplication |
| 1144 | // existing in matroska_cluster_parsing. |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1145 | for (i = 0; syntax[i].id; i++) |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1146 | if (id == syntax[i].id) |
| 1147 | break; |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1148 | |
Andreas Rheinhardt | 559e342 | 2019-05-16 22:30:04 | [diff] [blame] | 1149 | return &syntax[i]; |
Aurelien Jacobs | 66a37e0 | 2008-08-05 00:42:17 | [diff] [blame] | 1150 | } |
| 1151 | |
Aurelien Jacobs | 9bcb92c | 2008-08-05 00:42:13 | [diff] [blame] | 1152 | static int ebml_parse_nest(MatroskaDemuxContext *matroska, EbmlSyntax *syntax, |
Aurelien Jacobs | 6314cca | 2008-08-05 00:42:23 | [diff] [blame] | 1153 | void *data) |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1154 | { |
Andreas Rheinhardt | 6854127 | 2019-05-16 22:30:19 | [diff] [blame] | 1155 | int res; |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1156 | |
Andreas Rheinhardt | 71c9088 | 2019-05-16 22:30:18 | [diff] [blame] | 1157 | if (data) { |
Andreas Rheinhardt | 05ae0b2 | 2021-02-16 16:21:14 | [diff] [blame] | 1158 | for (int i = 0; syntax[i].id; i++) { |
| 1159 | void *dst = (char *)data + syntax[i].data_offset; |
Andreas Rheinhardt | 6854127 | 2019-05-16 22:30:19 | [diff] [blame] | 1160 | switch (syntax[i].type) { |
| 1161 | case EBML_UINT: |
Andreas Rheinhardt | 05ae0b2 | 2021-02-16 16:21:14 | [diff] [blame] | 1162 | *(uint64_t *)dst = syntax[i].def.u; |
Andreas Rheinhardt | 6854127 | 2019-05-16 22:30:19 | [diff] [blame] | 1163 | break; |
| 1164 | case EBML_SINT: |
Andreas Rheinhardt | 05ae0b2 | 2021-02-16 16:21:14 | [diff] [blame] | 1165 | *(int64_t *) dst = syntax[i].def.i; |
Andreas Rheinhardt | 6854127 | 2019-05-16 22:30:19 | [diff] [blame] | 1166 | break; |
| 1167 | case EBML_FLOAT: |
Andreas Rheinhardt | 05ae0b2 | 2021-02-16 16:21:14 | [diff] [blame] | 1168 | *(double *) dst = syntax[i].def.f; |
Andreas Rheinhardt | 6854127 | 2019-05-16 22:30:19 | [diff] [blame] | 1169 | break; |
| 1170 | case EBML_STR: |
| 1171 | case EBML_UTF8: |
| 1172 | // the default may be NULL |
| 1173 | if (syntax[i].def.s) { |
Andreas Rheinhardt | 05ae0b2 | 2021-02-16 16:21:14 | [diff] [blame] | 1174 | *(char**)dst = av_strdup(syntax[i].def.s); |
| 1175 | if (!*(char**)dst) |
Andreas Rheinhardt | 6854127 | 2019-05-16 22:30:19 | [diff] [blame] | 1176 | return AVERROR(ENOMEM); |
| 1177 | } |
| 1178 | break; |
Anton Khirnov | 668643b | 2013-09-04 06:55:17 | [diff] [blame] | 1179 | } |
Andreas Rheinhardt | 05ae0b2 | 2021-02-16 16:21:14 | [diff] [blame] | 1180 | } |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1181 | |
Andreas Rheinhardt | 6854127 | 2019-05-16 22:30:19 | [diff] [blame] | 1182 | if (!matroska->levels[matroska->num_levels - 1].length) { |
| 1183 | matroska->num_levels--; |
| 1184 | return 0; |
| 1185 | } |
Andreas Rheinhardt | 71c9088 | 2019-05-16 22:30:18 | [diff] [blame] | 1186 | } |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1187 | |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1188 | do { |
| 1189 | res = ebml_parse(matroska, syntax, data); |
| 1190 | } while (!res); |
| 1191 | |
| 1192 | return res == LEVEL_ENDED ? 0 : res; |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1193 | } |
| 1194 | |
wm4 | 7e240f9 | 2015-06-12 11:11:41 | [diff] [blame] | 1195 | static int is_ebml_id_valid(uint32_t id) |
| 1196 | { |
| 1197 | // Due to endian nonsense in Matroska, the highest byte with any bits set |
| 1198 | // will contain the leading length bit. This bit in turn identifies the |
| 1199 | // total byte length of the element by its position within the byte. |
| 1200 | unsigned int bits = av_log2(id); |
| 1201 | return id && (bits + 7) / 8 == (8 - bits % 8); |
| 1202 | } |
| 1203 | |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1204 | /* |
| 1205 | * Allocate and return the entry for the level1 element with the given ID. If |
| 1206 | * an entry already exists, return the existing entry. |
| 1207 | */ |
| 1208 | static MatroskaLevel1Element *matroska_find_level1_elem(MatroskaDemuxContext *matroska, |
Andreas Rheinhardt | 7e91035 | 2020-04-30 21:16:44 | [diff] [blame] | 1209 | uint32_t id, int64_t pos) |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1210 | { |
| 1211 | int i; |
| 1212 | MatroskaLevel1Element *elem; |
| 1213 | |
wm4 | 7e240f9 | 2015-06-12 11:11:41 | [diff] [blame] | 1214 | if (!is_ebml_id_valid(id)) |
| 1215 | return NULL; |
| 1216 | |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1217 | // Some files link to all clusters; useless. |
| 1218 | if (id == MATROSKA_ID_CLUSTER) |
| 1219 | return NULL; |
| 1220 | |
Andreas Rheinhardt | ff4da60 | 2020-04-30 22:49:49 | [diff] [blame] | 1221 | // There can be multiple SeekHeads and Tags. |
Andreas Rheinhardt | 7e91035 | 2020-04-30 21:16:44 | [diff] [blame] | 1222 | for (i = 0; i < matroska->num_level1_elems; i++) { |
| 1223 | if (matroska->level1_elems[i].id == id) { |
| 1224 | if (matroska->level1_elems[i].pos == pos || |
Andreas Rheinhardt | ff4da60 | 2020-04-30 22:49:49 | [diff] [blame] | 1225 | id != MATROSKA_ID_SEEKHEAD && id != MATROSKA_ID_TAGS) |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1226 | return &matroska->level1_elems[i]; |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | // Only a completely broken file would have more elements. |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1231 | if (matroska->num_level1_elems >= FF_ARRAY_ELEMS(matroska->level1_elems)) { |
Andreas Rheinhardt | 7e91035 | 2020-04-30 21:16:44 | [diff] [blame] | 1232 | av_log(matroska->ctx, AV_LOG_ERROR, "Too many level1 elements.\n"); |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1233 | return NULL; |
| 1234 | } |
| 1235 | |
| 1236 | elem = &matroska->level1_elems[matroska->num_level1_elems++]; |
| 1237 | *elem = (MatroskaLevel1Element){.id = id}; |
| 1238 | |
| 1239 | return elem; |
| 1240 | } |
| 1241 | |
Andreas Rheinhardt | 559e342 | 2019-05-16 22:30:04 | [diff] [blame] | 1242 | static int ebml_parse(MatroskaDemuxContext *matroska, |
| 1243 | EbmlSyntax *syntax, void *data) |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1244 | { |
Reimar Döffinger | 14d735b | 2011-02-06 10:32:03 | [diff] [blame] | 1245 | static const uint64_t max_lengths[EBML_TYPE_COUNT] = { |
Andreas Rheinhardt | 04b62bd | 2019-05-16 22:30:15 | [diff] [blame] | 1246 | // Forbid unknown-length EBML_NONE elements. |
| 1247 | [EBML_NONE] = EBML_UNKNOWN_LENGTH - 1, |
Reimar Döffinger | 14d735b | 2011-02-06 10:32:03 | [diff] [blame] | 1248 | [EBML_UINT] = 8, |
Andreas Rheinhardt | e5ec131 | 2019-05-16 22:29:50 | [diff] [blame] | 1249 | [EBML_SINT] = 8, |
Reimar Döffinger | 14d735b | 2011-02-06 10:32:03 | [diff] [blame] | 1250 | [EBML_FLOAT] = 8, |
| 1251 | // max. 16 MB for strings |
| 1252 | [EBML_STR] = 0x1000000, |
| 1253 | [EBML_UTF8] = 0x1000000, |
| 1254 | // max. 256 MB for binary data |
| 1255 | [EBML_BIN] = 0x10000000, |
| 1256 | // no limits for anything else |
| 1257 | }; |
Anton Khirnov | 471fe57 | 2011-02-20 10:04:12 | [diff] [blame] | 1258 | AVIOContext *pb = matroska->ctx->pb; |
Andreas Rheinhardt | 559e342 | 2019-05-16 22:30:04 | [diff] [blame] | 1259 | uint32_t id; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1260 | uint64_t length; |
Andreas Rheinhardt | 7087fc9 | 2019-05-16 22:30:12 | [diff] [blame] | 1261 | int64_t pos = avio_tell(pb), pos_alt; |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1262 | int res, update_pos = 1, level_check; |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1263 | MatroskaLevel1Element *level1_elem; |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1264 | MatroskaLevel *level = matroska->num_levels ? &matroska->levels[matroska->num_levels - 1] : NULL; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1265 | |
Andreas Rheinhardt | 559e342 | 2019-05-16 22:30:04 | [diff] [blame] | 1266 | if (!matroska->current_id) { |
| 1267 | uint64_t id; |
| 1268 | res = ebml_read_num(matroska, pb, 4, &id, 0); |
| 1269 | if (res < 0) { |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1270 | if (pb->eof_reached && res == AVERROR_EOF) { |
| 1271 | if (matroska->is_live) |
| 1272 | // in live mode, finish parsing if EOF is reached. |
| 1273 | return 1; |
Andreas Rheinhardt | 3ed2755 | 2019-05-16 22:30:11 | [diff] [blame] | 1274 | if (level && pos == avio_tell(pb)) { |
| 1275 | if (level->length == EBML_UNKNOWN_LENGTH) { |
| 1276 | // Unknown-length levels automatically end at EOF. |
| 1277 | matroska->num_levels--; |
| 1278 | return LEVEL_ENDED; |
| 1279 | } else { |
| 1280 | av_log(matroska->ctx, AV_LOG_ERROR, "File ended prematurely " |
| 1281 | "at pos. %"PRIu64" (0x%"PRIx64")\n", pos, pos); |
| 1282 | } |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1283 | } |
| 1284 | } |
| 1285 | return res; |
Andreas Rheinhardt | 559e342 | 2019-05-16 22:30:04 | [diff] [blame] | 1286 | } |
| 1287 | matroska->current_id = id | 1 << 7 * res; |
Andreas Rheinhardt | 7087fc9 | 2019-05-16 22:30:12 | [diff] [blame] | 1288 | pos_alt = pos + res; |
| 1289 | } else { |
| 1290 | pos_alt = pos; |
| 1291 | pos -= (av_log2(matroska->current_id) + 7) / 8; |
| 1292 | } |
Andreas Rheinhardt | 559e342 | 2019-05-16 22:30:04 | [diff] [blame] | 1293 | |
| 1294 | id = matroska->current_id; |
| 1295 | |
| 1296 | syntax = ebml_parse_id(syntax, id); |
Andreas Rheinhardt | 559e342 | 2019-05-16 22:30:04 | [diff] [blame] | 1297 | if (!syntax->id && id != EBML_ID_VOID && id != EBML_ID_CRC32) { |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1298 | if (level && level->length == EBML_UNKNOWN_LENGTH) { |
| 1299 | // Unknown-length levels end when an element from an upper level |
| 1300 | // in the hierarchy is encountered. |
| 1301 | while (syntax->def.n) { |
| 1302 | syntax = ebml_parse_id(syntax->def.n, id); |
| 1303 | if (syntax->id) { |
| 1304 | matroska->num_levels--; |
| 1305 | return LEVEL_ENDED; |
| 1306 | } |
| 1307 | }; |
| 1308 | } |
| 1309 | |
| 1310 | av_log(matroska->ctx, AV_LOG_DEBUG, "Unknown entry 0x%"PRIX32" at pos. " |
| 1311 | "%"PRId64"\n", id, pos); |
| 1312 | update_pos = 0; /* Don't update resync_pos as an error might have happened. */ |
Andreas Rheinhardt | 559e342 | 2019-05-16 22:30:04 | [diff] [blame] | 1313 | } |
| 1314 | |
Andreas Rheinhardt | 71c9088 | 2019-05-16 22:30:18 | [diff] [blame] | 1315 | if (data) { |
Andreas Rheinhardt | 6854127 | 2019-05-16 22:30:19 | [diff] [blame] | 1316 | data = (char *) data + syntax->data_offset; |
| 1317 | if (syntax->list_elem_size) { |
| 1318 | EbmlList *list = data; |
James Almer | 3b3150c | 2019-09-03 21:45:04 | [diff] [blame] | 1319 | void *newelem; |
| 1320 | |
| 1321 | if ((unsigned)list->nb_elem + 1 >= UINT_MAX / syntax->list_elem_size) |
| 1322 | return AVERROR(ENOMEM); |
| 1323 | newelem = av_fast_realloc(list->elem, |
| 1324 | &list->alloc_elem_size, |
| 1325 | (list->nb_elem + 1) * syntax->list_elem_size); |
Andreas Rheinhardt | 6854127 | 2019-05-16 22:30:19 | [diff] [blame] | 1326 | if (!newelem) |
| 1327 | return AVERROR(ENOMEM); |
| 1328 | list->elem = newelem; |
| 1329 | data = (char *) list->elem + list->nb_elem * syntax->list_elem_size; |
| 1330 | memset(data, 0, syntax->list_elem_size); |
| 1331 | list->nb_elem++; |
| 1332 | } |
Andreas Rheinhardt | 71c9088 | 2019-05-16 22:30:18 | [diff] [blame] | 1333 | } |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1334 | |
Andreas Rheinhardt | bc3306f | 2019-05-16 22:30:00 | [diff] [blame] | 1335 | if (syntax->type != EBML_STOP) { |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 1336 | matroska->current_id = 0; |
Reimar Döffinger | 1b4d327 | 2010-09-06 17:51:44 | [diff] [blame] | 1337 | if ((res = ebml_read_length(matroska, pb, &length)) < 0) |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1338 | return res; |
Andreas Rheinhardt | 7087fc9 | 2019-05-16 22:30:12 | [diff] [blame] | 1339 | |
| 1340 | pos_alt += res; |
| 1341 | |
Steve Lhomme | 9326117 | 2019-02-13 12:20:45 | [diff] [blame] | 1342 | if (matroska->num_levels > 0) { |
Andreas Rheinhardt via ffmpeg-devel | 18a851a | 2019-03-27 11:18:44 | [diff] [blame] | 1343 | if (length != EBML_UNKNOWN_LENGTH && |
| 1344 | level->length != EBML_UNKNOWN_LENGTH) { |
Andreas Rheinhardt | 7087fc9 | 2019-05-16 22:30:12 | [diff] [blame] | 1345 | uint64_t elem_end = pos_alt + length, |
Andreas Rheinhardt via ffmpeg-devel | 18a851a | 2019-03-27 11:18:44 | [diff] [blame] | 1346 | level_end = level->start + level->length; |
| 1347 | |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1348 | if (elem_end < level_end) { |
| 1349 | level_check = 0; |
| 1350 | } else if (elem_end == level_end) { |
| 1351 | level_check = LEVEL_ENDED; |
| 1352 | } else { |
Andreas Rheinhardt via ffmpeg-devel | 18a851a | 2019-03-27 11:18:44 | [diff] [blame] | 1353 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1354 | "Element at 0x%"PRIx64" ending at 0x%"PRIx64" exceeds " |
| 1355 | "containing master element ending at 0x%"PRIx64"\n", |
| 1356 | pos, elem_end, level_end); |
| 1357 | return AVERROR_INVALIDDATA; |
| 1358 | } |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1359 | } else if (length != EBML_UNKNOWN_LENGTH) { |
| 1360 | level_check = 0; |
Andreas Rheinhardt via ffmpeg-devel | 18a851a | 2019-03-27 11:18:44 | [diff] [blame] | 1361 | } else if (level->length != EBML_UNKNOWN_LENGTH) { |
| 1362 | av_log(matroska->ctx, AV_LOG_ERROR, "Unknown-sized element " |
| 1363 | "at 0x%"PRIx64" inside parent with finite size\n", pos); |
| 1364 | return AVERROR_INVALIDDATA; |
Andreas Rheinhardt | 3c70b94 | 2019-05-16 22:30:16 | [diff] [blame] | 1365 | } else { |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1366 | level_check = 0; |
Andreas Rheinhardt | 3c70b94 | 2019-05-16 22:30:16 | [diff] [blame] | 1367 | if (id != MATROSKA_ID_CLUSTER && (syntax->type == EBML_LEVEL1 |
| 1368 | || syntax->type == EBML_NEST)) { |
| 1369 | // According to the current specifications only clusters and |
| 1370 | // segments are allowed to be unknown-length. We also accept |
| 1371 | // other unknown-length master elements. |
| 1372 | av_log(matroska->ctx, AV_LOG_WARNING, |
| 1373 | "Found unknown-length element 0x%"PRIX32" other than " |
| 1374 | "a cluster at 0x%"PRIx64". Spec-incompliant, but " |
| 1375 | "parsing will nevertheless be attempted.\n", id, pos); |
| 1376 | update_pos = -1; |
| 1377 | } |
| 1378 | } |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1379 | } else |
| 1380 | level_check = 0; |
Andreas Rheinhardt | a3db9f6 | 2019-05-16 22:30:05 | [diff] [blame] | 1381 | |
Andreas Rheinhardt | 04b62bd | 2019-05-16 22:30:15 | [diff] [blame] | 1382 | if (max_lengths[syntax->type] && length > max_lengths[syntax->type]) { |
| 1383 | if (length != EBML_UNKNOWN_LENGTH) { |
| 1384 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1385 | "Invalid length 0x%"PRIx64" > 0x%"PRIx64" for element " |
| 1386 | "with ID 0x%"PRIX32" at 0x%"PRIx64"\n", |
| 1387 | length, max_lengths[syntax->type], id, pos); |
| 1388 | } else if (syntax->type != EBML_NONE) { |
| 1389 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1390 | "Element with ID 0x%"PRIX32" at pos. 0x%"PRIx64" has " |
| 1391 | "unknown length, yet the length of an element of its " |
| 1392 | "type must be known.\n", id, pos); |
| 1393 | } else { |
| 1394 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1395 | "Found unknown-length element with ID 0x%"PRIX32" at " |
| 1396 | "pos. 0x%"PRIx64" for which no syntax for parsing is " |
| 1397 | "available.\n", id, pos); |
| 1398 | } |
| 1399 | return AVERROR_INVALIDDATA; |
| 1400 | } |
| 1401 | |
Andreas Rheinhardt | 5120305 | 2019-05-16 22:30:14 | [diff] [blame] | 1402 | if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) { |
| 1403 | // Loosing sync will likely manifest itself as encountering unknown |
| 1404 | // elements which are not reliably distinguishable from elements |
| 1405 | // belonging to future extensions of the format. |
| 1406 | // We use a heuristic to detect such situations: If the current |
| 1407 | // element is not expected at the current syntax level and there |
| 1408 | // were only a few unknown elements in a row, then the element is |
| 1409 | // skipped or considered defective based upon the length of the |
| 1410 | // current element (i.e. how much would be skipped); if there were |
| 1411 | // more than a few skipped elements in a row and skipping the current |
| 1412 | // element would lead us more than SKIP_THRESHOLD away from the last |
Michael Niedermayer | fccc37c | 2019-08-28 17:04:42 | [diff] [blame] | 1413 | // known good position, then it is inferred that an error occurred. |
Andreas Rheinhardt | 5120305 | 2019-05-16 22:30:14 | [diff] [blame] | 1414 | // The dependency on the number of unknown elements in a row exists |
| 1415 | // because the distance to the last known good position is |
| 1416 | // automatically big if the last parsed element was big. |
| 1417 | // In both cases, each unknown element is considered equivalent to |
| 1418 | // UNKNOWN_EQUIV of skipped bytes for the check. |
| 1419 | // The whole check is only done for non-seekable output, because |
| 1420 | // in this situation skipped data can't simply be rechecked later. |
| 1421 | // This is especially important when using unkown length elements |
| 1422 | // as the check for whether a child exceeds its containing master |
| 1423 | // element is not effective in this situation. |
| 1424 | if (update_pos) { |
| 1425 | matroska->unknown_count = 0; |
| 1426 | } else { |
| 1427 | int64_t dist = length + UNKNOWN_EQUIV * matroska->unknown_count++; |
| 1428 | |
| 1429 | if (matroska->unknown_count > 3) |
| 1430 | dist += pos_alt - matroska->resync_pos; |
| 1431 | |
| 1432 | if (dist > SKIP_THRESHOLD) { |
| 1433 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 1434 | "Unknown element %"PRIX32" at pos. 0x%"PRIx64" with " |
| 1435 | "length 0x%"PRIx64" considered as invalid data. Last " |
| 1436 | "known good position 0x%"PRIx64", %d unknown elements" |
| 1437 | " in a row\n", id, pos, length, matroska->resync_pos, |
| 1438 | matroska->unknown_count); |
| 1439 | return AVERROR_INVALIDDATA; |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | |
Andreas Rheinhardt | 3c70b94 | 2019-05-16 22:30:16 | [diff] [blame] | 1444 | if (update_pos > 0) { |
Andreas Rheinhardt | a3db9f6 | 2019-05-16 22:30:05 | [diff] [blame] | 1445 | // We have found an element that is allowed at this place |
| 1446 | // in the hierarchy and it passed all checks, so treat the beginning |
| 1447 | // of the element as the "last known good" position. |
| 1448 | matroska->resync_pos = pos; |
| 1449 | } |
Andreas Rheinhardt | 71c9088 | 2019-05-16 22:30:18 | [diff] [blame] | 1450 | |
| 1451 | if (!data && length != EBML_UNKNOWN_LENGTH) |
| 1452 | goto skip; |
Aurelien Jacobs | c3ade62 | 2010-06-11 16:34:01 | [diff] [blame] | 1453 | } |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1454 | |
| 1455 | switch (syntax->type) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1456 | case EBML_UINT: |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 1457 | res = ebml_read_uint(pb, length, syntax->def.u, data); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1458 | break; |
Michael Niedermayer | b3d9ab1 | 2014-03-07 22:46:37 | [diff] [blame] | 1459 | case EBML_SINT: |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 1460 | res = ebml_read_sint(pb, length, syntax->def.i, data); |
Michael Niedermayer | b3d9ab1 | 2014-03-07 22:46:37 | [diff] [blame] | 1461 | break; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1462 | case EBML_FLOAT: |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 1463 | res = ebml_read_float(pb, length, syntax->def.f, data); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1464 | break; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1465 | case EBML_STR: |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1466 | case EBML_UTF8: |
Andreas Rheinhardt | 0a99c3d | 2021-02-16 15:51:40 | [diff] [blame] | 1467 | res = ebml_read_ascii(pb, length, syntax->def.s, data); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1468 | break; |
| 1469 | case EBML_BIN: |
Andreas Rheinhardt | 7087fc9 | 2019-05-16 22:30:12 | [diff] [blame] | 1470 | res = ebml_read_binary(pb, length, pos_alt, data); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1471 | break; |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1472 | case EBML_LEVEL1: |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1473 | case EBML_NEST: |
Andreas Rheinhardt | 7087fc9 | 2019-05-16 22:30:12 | [diff] [blame] | 1474 | if ((res = ebml_read_master(matroska, length, pos_alt)) < 0) |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1475 | return res; |
| 1476 | if (id == MATROSKA_ID_SEGMENT) |
Andreas Rheinhardt | 7087fc9 | 2019-05-16 22:30:12 | [diff] [blame] | 1477 | matroska->segment_start = pos_alt; |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1478 | if (id == MATROSKA_ID_CUES) |
| 1479 | matroska->cues_parsing_deferred = 0; |
| 1480 | if (syntax->type == EBML_LEVEL1 && |
Andreas Rheinhardt | 7e91035 | 2020-04-30 21:16:44 | [diff] [blame] | 1481 | (level1_elem = matroska_find_level1_elem(matroska, syntax->id, pos))) { |
Andreas Rheinhardt | 806ac7d | 2019-05-16 22:30:21 | [diff] [blame] | 1482 | if (!level1_elem->pos) { |
| 1483 | // Zero is not a valid position for a level 1 element. |
| 1484 | level1_elem->pos = pos; |
| 1485 | } else if (level1_elem->pos != pos) |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1486 | av_log(matroska->ctx, AV_LOG_ERROR, "Duplicate element\n"); |
| 1487 | level1_elem->parsed = 1; |
| 1488 | } |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1489 | if (res = ebml_parse_nest(matroska, syntax->def.n, data)) |
| 1490 | return res; |
| 1491 | break; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1492 | case EBML_STOP: |
| 1493 | return 1; |
Andreas Rheinhardt | 71c9088 | 2019-05-16 22:30:18 | [diff] [blame] | 1494 | skip: |
Michael Niedermayer | 668c873 | 2012-08-04 00:27:51 | [diff] [blame] | 1495 | default: |
Andreas Rheinhardt | ff5ea59 | 2019-06-23 23:42:31 | [diff] [blame] | 1496 | if (length) { |
Andreas Rheinhardt | eb33be1 | 2019-07-06 16:59:22 | [diff] [blame] | 1497 | int64_t res2; |
Andreas Rheinhardt | ff5ea59 | 2019-06-23 23:42:31 | [diff] [blame] | 1498 | if (ffio_limit(pb, length) != length) { |
| 1499 | // ffio_limit emits its own error message, |
| 1500 | // so we don't have to. |
| 1501 | return AVERROR(EIO); |
| 1502 | } |
Andreas Rheinhardt | eb33be1 | 2019-07-06 16:59:22 | [diff] [blame] | 1503 | if ((res2 = avio_skip(pb, length - 1)) >= 0) { |
Andreas Rheinhardt | ff5ea59 | 2019-06-23 23:42:31 | [diff] [blame] | 1504 | // avio_skip might take us past EOF. We check for this |
| 1505 | // by skipping only length - 1 bytes, reading a byte and |
| 1506 | // checking the error flags. This is done in order to check |
| 1507 | // that the element has been properly skipped even when |
| 1508 | // no filesize (that ffio_limit relies on) is available. |
| 1509 | avio_r8(pb); |
| 1510 | res = NEEDS_CHECKING; |
Andreas Rheinhardt | eb33be1 | 2019-07-06 16:59:22 | [diff] [blame] | 1511 | } else |
| 1512 | res = res2; |
Andreas Rheinhardt | ff5ea59 | 2019-06-23 23:42:31 | [diff] [blame] | 1513 | } else |
| 1514 | res = 0; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1515 | } |
Andreas Rheinhardt | a569a7b | 2019-06-23 23:42:30 | [diff] [blame] | 1516 | if (res) { |
| 1517 | if (res == NEEDS_CHECKING) { |
| 1518 | if (pb->eof_reached) { |
| 1519 | if (pb->error) |
| 1520 | res = pb->error; |
| 1521 | else |
| 1522 | res = AVERROR_EOF; |
| 1523 | } else |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1524 | goto level_check; |
Andreas Rheinhardt | a569a7b | 2019-06-23 23:42:30 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | if (res == AVERROR_INVALIDDATA) |
| 1528 | av_log(matroska->ctx, AV_LOG_ERROR, "Invalid element\n"); |
| 1529 | else if (res == AVERROR(EIO)) |
| 1530 | av_log(matroska->ctx, AV_LOG_ERROR, "Read error\n"); |
| 1531 | else if (res == AVERROR_EOF) { |
| 1532 | av_log(matroska->ctx, AV_LOG_ERROR, "File ended prematurely\n"); |
| 1533 | res = AVERROR(EIO); |
| 1534 | } |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1535 | |
| 1536 | return res; |
Andreas Rheinhardt | a569a7b | 2019-06-23 23:42:30 | [diff] [blame] | 1537 | } |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1538 | |
| 1539 | level_check: |
Andreas Rheinhardt | 5b9a0e6 | 2021-02-16 13:33:51 | [diff] [blame] | 1540 | if (syntax->is_counted && data) { |
| 1541 | CountedElement *elem = data; |
| 1542 | if (elem->count != UINT_MAX) |
| 1543 | elem->count++; |
| 1544 | } |
| 1545 | |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1546 | if (level_check == LEVEL_ENDED && matroska->num_levels) { |
| 1547 | level = &matroska->levels[matroska->num_levels - 1]; |
| 1548 | pos = avio_tell(pb); |
| 1549 | |
| 1550 | // Given that pos >= level->start no check for |
| 1551 | // level->length != EBML_UNKNOWN_LENGTH is necessary. |
| 1552 | while (matroska->num_levels && pos == level->start + level->length) { |
| 1553 | matroska->num_levels--; |
| 1554 | level--; |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | return level_check; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1559 | } |
| 1560 | |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1561 | static void ebml_free(EbmlSyntax *syntax, void *data) |
| 1562 | { |
| 1563 | int i, j; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1564 | for (i = 0; syntax[i].id; i++) { |
| 1565 | void *data_off = (char *) data + syntax[i].data_offset; |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1566 | switch (syntax[i].type) { |
| 1567 | case EBML_STR: |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1568 | case EBML_UTF8: |
| 1569 | av_freep(data_off); |
| 1570 | break; |
| 1571 | case EBML_BIN: |
James Almer | a618866 | 2018-04-04 17:12:54 | [diff] [blame] | 1572 | av_buffer_unref(&((EbmlBin *) data_off)->buf); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1573 | break; |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1574 | case EBML_LEVEL1: |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1575 | case EBML_NEST: |
| 1576 | if (syntax[i].list_elem_size) { |
| 1577 | EbmlList *list = data_off; |
| 1578 | char *ptr = list->elem; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1579 | for (j = 0; j < list->nb_elem; |
| 1580 | j++, ptr += syntax[i].list_elem_size) |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1581 | ebml_free(syntax[i].def.n, ptr); |
Michael Niedermayer | 6e70e4a | 2015-01-06 11:48:38 | [diff] [blame] | 1582 | av_freep(&list->elem); |
Michael Schenk | 18b9466 | 2016-11-25 08:36:20 | [diff] [blame] | 1583 | list->nb_elem = 0; |
James Almer | 3b3150c | 2019-09-03 21:45:04 | [diff] [blame] | 1584 | list->alloc_elem_size = 0; |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1585 | } else |
| 1586 | ebml_free(syntax[i].def.n, data_off); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1587 | default: |
| 1588 | break; |
Aurelien Jacobs | 789ed10 | 2008-08-05 00:40:00 | [diff] [blame] | 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1593 | /* |
| 1594 | * Autodetecting... |
| 1595 | */ |
Carl Eugen Hoyos | 4d8875e | 2019-03-21 00:18:37 | [diff] [blame] | 1596 | static int matroska_probe(const AVProbeData *p) |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1597 | { |
| 1598 | uint64_t total = 0; |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 1599 | int len_mask = 0x80, size = 1, n = 1, i; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1600 | |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 1601 | /* EBML header? */ |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1602 | if (AV_RB32(p->buf) != EBML_ID_HEADER) |
| 1603 | return 0; |
| 1604 | |
| 1605 | /* length of header */ |
| 1606 | total = p->buf[4]; |
| 1607 | while (size <= 8 && !(total & len_mask)) { |
| 1608 | size++; |
| 1609 | len_mask >>= 1; |
| 1610 | } |
| 1611 | if (size > 8) |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1612 | return 0; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1613 | total &= (len_mask - 1); |
| 1614 | while (n < size) |
| 1615 | total = (total << 8) | p->buf[4 + n++]; |
| 1616 | |
Andreas Rheinhardt | 9c6d14a | 2019-05-16 22:30:17 | [diff] [blame] | 1617 | if (total + 1 == 1ULL << (7 * size)){ |
| 1618 | /* Unknown-length header - simply parse the whole buffer. */ |
| 1619 | total = p->buf_size - 4 - size; |
| 1620 | } else { |
| 1621 | /* Does the probe data contain the whole header? */ |
| 1622 | if (p->buf_size < 4 + size + total) |
| 1623 | return 0; |
| 1624 | } |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1625 | |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 1626 | /* The header should contain a known document type. For now, |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1627 | * we don't parse the whole header but simply check for the |
| 1628 | * availability of that array of characters inside the header. |
| 1629 | * Not fully fool-proof, but good enough. */ |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 1630 | for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) { |
Michael Niedermayer | 17b2630 | 2015-05-11 01:51:17 | [diff] [blame] | 1631 | size_t probelen = strlen(matroska_doctypes[i]); |
Chris Evans | 69619a1 | 2011-07-20 00:51:48 | [diff] [blame] | 1632 | if (total < probelen) |
| 1633 | continue; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1634 | for (n = 4 + size; n <= 4 + size + total - probelen; n++) |
| 1635 | if (!memcmp(p->buf + n, matroska_doctypes[i], probelen)) |
James Zern | 470491f | 2010-05-22 01:41:32 | [diff] [blame] | 1636 | return AVPROBE_SCORE_MAX; |
| 1637 | } |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1638 | |
David Conrad | c7b913c | 2010-05-22 01:41:35 | [diff] [blame] | 1639 | // probably valid EBML header but no recognized doctype |
Diego Biurrun | e0f8be6 | 2013-03-25 15:12:51 | [diff] [blame] | 1640 | return AVPROBE_SCORE_EXTENSION; |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | static MatroskaTrack *matroska_find_track_by_num(MatroskaDemuxContext *matroska, |
Andreas Rheinhardt | ba36a07 | 2019-12-26 03:17:24 | [diff] [blame] | 1644 | uint64_t num) |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1645 | { |
| 1646 | MatroskaTrack *tracks = matroska->tracks.elem; |
| 1647 | int i; |
| 1648 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1649 | for (i = 0; i < matroska->tracks.nb_elem; i++) |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1650 | if (tracks[i].num == num) |
| 1651 | return &tracks[i]; |
| 1652 | |
Andreas Rheinhardt | ba36a07 | 2019-12-26 03:17:24 | [diff] [blame] | 1653 | av_log(matroska->ctx, AV_LOG_ERROR, "Invalid track number %"PRIu64"\n", num); |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 1654 | return NULL; |
| 1655 | } |
| 1656 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1657 | static int matroska_decode_buffer(uint8_t **buf, int *buf_size, |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 1658 | MatroskaTrack *track) |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1659 | { |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1660 | MatroskaTrackEncoding *encodings = track->encodings.elem; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1661 | uint8_t *data = *buf; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1662 | int isize = *buf_size; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1663 | uint8_t *pkt_data = NULL; |
Diego Biurrun | 529506b | 2012-02-12 09:58:46 | [diff] [blame] | 1664 | uint8_t av_unused *newpktdata; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1665 | int pkt_size = isize; |
| 1666 | int result = 0; |
| 1667 | int olen; |
| 1668 | |
Michael Niedermayer | 4b7c523 | 2012-06-14 23:29:30 | [diff] [blame] | 1669 | if (pkt_size >= 10000000U) |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1670 | return AVERROR_INVALIDDATA; |
Aurelien Jacobs | 4f90688 | 2010-08-17 14:05:23 | [diff] [blame] | 1671 | |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 1672 | switch (encodings[0].compression.algo) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1673 | case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP: |
| 1674 | { |
Luca Barbato | 8d4dd55 | 2012-09-14 16:51:49 | [diff] [blame] | 1675 | int header_size = encodings[0].compression.settings.size; |
| 1676 | uint8_t *header = encodings[0].compression.settings.data; |
| 1677 | |
Michael Niedermayer | 1df2e3c | 2012-09-20 11:30:44 | [diff] [blame] | 1678 | if (header_size && !header) { |
Michael Niedermayer | 0efcf16 | 2012-11-23 17:10:02 | [diff] [blame] | 1679 | 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] | 1680 | return -1; |
| 1681 | } |
Michael Niedermayer | 1df2e3c | 2012-09-20 11:30:44 | [diff] [blame] | 1682 | |
Luca Barbato | 8d4dd55 | 2012-09-14 16:51:49 | [diff] [blame] | 1683 | if (!header_size) |
| 1684 | return 0; |
| 1685 | |
| 1686 | pkt_size = isize + header_size; |
James Almer | 9703b7d | 2018-04-04 21:12:53 | [diff] [blame] | 1687 | pkt_data = av_malloc(pkt_size + AV_INPUT_BUFFER_PADDING_SIZE); |
Luca Barbato | 8d4dd55 | 2012-09-14 16:51:49 | [diff] [blame] | 1688 | if (!pkt_data) |
| 1689 | return AVERROR(ENOMEM); |
| 1690 | |
| 1691 | memcpy(pkt_data, header, header_size); |
| 1692 | memcpy(pkt_data + header_size, data, isize); |
| 1693 | break; |
| 1694 | } |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1695 | case MATROSKA_TRACK_ENCODING_COMP_LZO: |
| 1696 | do { |
Andreas Rheinhardt | 2ff687c | 2019-12-28 02:40:33 | [diff] [blame] | 1697 | int insize = isize; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1698 | olen = pkt_size *= 3; |
James Almer | 9703b7d | 2018-04-04 21:12:53 | [diff] [blame] | 1699 | newpktdata = av_realloc(pkt_data, pkt_size + AV_LZO_OUTPUT_PADDING |
| 1700 | + AV_INPUT_BUFFER_PADDING_SIZE); |
Luca Barbato | 581281e | 2012-09-14 16:39:58 | [diff] [blame] | 1701 | if (!newpktdata) { |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1702 | result = AVERROR(ENOMEM); |
Luca Barbato | 581281e | 2012-09-14 16:39:58 | [diff] [blame] | 1703 | goto failed; |
| 1704 | } |
| 1705 | pkt_data = newpktdata; |
Andreas Rheinhardt | 2ff687c | 2019-12-28 02:40:33 | [diff] [blame] | 1706 | result = av_lzo1x_decode(pkt_data, &olen, data, &insize); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1707 | } while (result == AV_LZO_OUTPUT_FULL && pkt_size < 10000000); |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1708 | if (result) { |
| 1709 | result = AVERROR_INVALIDDATA; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1710 | goto failed; |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1711 | } |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1712 | pkt_size -= olen; |
| 1713 | break; |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame] | 1714 | #if CONFIG_ZLIB |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1715 | case MATROSKA_TRACK_ENCODING_COMP_ZLIB: |
| 1716 | { |
| 1717 | z_stream zstream = { 0 }; |
Michael Niedermayer | 9c3d2cb | 2022-02-03 21:46:55 | [diff] [blame] | 1718 | if (!pkt_size || inflateInit(&zstream) != Z_OK) |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1719 | return -1; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1720 | zstream.next_in = data; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1721 | zstream.avail_in = isize; |
| 1722 | do { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1723 | pkt_size *= 3; |
James Almer | 9703b7d | 2018-04-04 21:12:53 | [diff] [blame] | 1724 | newpktdata = av_realloc(pkt_data, pkt_size + AV_INPUT_BUFFER_PADDING_SIZE); |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1725 | if (!newpktdata) { |
| 1726 | inflateEnd(&zstream); |
Michael Niedermayer | 171af59 | 2015-05-12 16:32:12 | [diff] [blame] | 1727 | result = AVERROR(ENOMEM); |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1728 | goto failed; |
| 1729 | } |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1730 | pkt_data = newpktdata; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1731 | zstream.avail_out = pkt_size - zstream.total_out; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1732 | zstream.next_out = pkt_data + zstream.total_out; |
Michael Niedermayer | 171af59 | 2015-05-12 16:32:12 | [diff] [blame] | 1733 | result = inflate(&zstream, Z_NO_FLUSH); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1734 | } while (result == Z_OK && pkt_size < 10000000); |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1735 | pkt_size = zstream.total_out; |
| 1736 | inflateEnd(&zstream); |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1737 | if (result != Z_STREAM_END) { |
| 1738 | if (result == Z_MEM_ERROR) |
| 1739 | result = AVERROR(ENOMEM); |
| 1740 | else |
| 1741 | result = AVERROR_INVALIDDATA; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1742 | goto failed; |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1743 | } |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1744 | break; |
| 1745 | } |
| 1746 | #endif |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame] | 1747 | #if CONFIG_BZLIB |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1748 | case MATROSKA_TRACK_ENCODING_COMP_BZLIB: |
| 1749 | { |
| 1750 | bz_stream bzstream = { 0 }; |
Michael Niedermayer | 9c3d2cb | 2022-02-03 21:46:55 | [diff] [blame] | 1751 | if (!pkt_size || BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK) |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1752 | return -1; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1753 | bzstream.next_in = data; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1754 | bzstream.avail_in = isize; |
| 1755 | do { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1756 | pkt_size *= 3; |
James Almer | 9703b7d | 2018-04-04 21:12:53 | [diff] [blame] | 1757 | newpktdata = av_realloc(pkt_data, pkt_size + AV_INPUT_BUFFER_PADDING_SIZE); |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1758 | if (!newpktdata) { |
| 1759 | BZ2_bzDecompressEnd(&bzstream); |
Michael Niedermayer | 171af59 | 2015-05-12 16:32:12 | [diff] [blame] | 1760 | result = AVERROR(ENOMEM); |
Michael Niedermayer | 77d2ef1 | 2011-07-28 12:59:54 | [diff] [blame] | 1761 | goto failed; |
| 1762 | } |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1763 | pkt_data = newpktdata; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1764 | bzstream.avail_out = pkt_size - bzstream.total_out_lo32; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1765 | bzstream.next_out = pkt_data + bzstream.total_out_lo32; |
Michael Niedermayer | 171af59 | 2015-05-12 16:32:12 | [diff] [blame] | 1766 | result = BZ2_bzDecompress(&bzstream); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1767 | } while (result == BZ_OK && pkt_size < 10000000); |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1768 | pkt_size = bzstream.total_out_lo32; |
| 1769 | BZ2_bzDecompressEnd(&bzstream); |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1770 | if (result != BZ_STREAM_END) { |
| 1771 | if (result == BZ_MEM_ERROR) |
| 1772 | result = AVERROR(ENOMEM); |
| 1773 | else |
| 1774 | result = AVERROR_INVALIDDATA; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1775 | goto failed; |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1776 | } |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1777 | break; |
| 1778 | } |
| 1779 | #endif |
Aurelien Jacobs | 28f27e0 | 2008-08-20 23:08:07 | [diff] [blame] | 1780 | default: |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1781 | return AVERROR_INVALIDDATA; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1782 | } |
| 1783 | |
James Almer | 9703b7d | 2018-04-04 21:12:53 | [diff] [blame] | 1784 | memset(pkt_data + pkt_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
| 1785 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1786 | *buf = pkt_data; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1787 | *buf_size = pkt_size; |
| 1788 | return 0; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1789 | |
| 1790 | failed: |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1791 | av_free(pkt_data); |
Luca Barbato | c9a39ce | 2012-09-14 18:03:37 | [diff] [blame] | 1792 | return result; |
Evgeniy Stepanov | 935ec5a | 2008-06-22 15:49:44 | [diff] [blame] | 1793 | } |
| 1794 | |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1795 | static void matroska_convert_tag(AVFormatContext *s, EbmlList *list, |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1796 | AVDictionary **metadata, char *prefix) |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 1797 | { |
| 1798 | MatroskaTag *tags = list->elem; |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1799 | char key[1024]; |
| 1800 | int i; |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 1801 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1802 | for (i = 0; i < list->nb_elem; i++) { |
| 1803 | const char *lang = tags[i].lang && |
| 1804 | strcmp(tags[i].lang, "und") ? tags[i].lang : NULL; |
Anton Khirnov | bf800c7 | 2010-11-03 06:29:04 | [diff] [blame] | 1805 | |
| 1806 | if (!tags[i].name) { |
| 1807 | av_log(s, AV_LOG_WARNING, "Skipping invalid tag with no TagName.\n"); |
| 1808 | continue; |
| 1809 | } |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1810 | if (prefix) |
| 1811 | snprintf(key, sizeof(key), "%s/%s", prefix, tags[i].name); |
| 1812 | else |
| 1813 | av_strlcpy(key, tags[i].name, sizeof(key)); |
Aurelien Jacobs | f702df3 | 2009-02-15 16:05:37 | [diff] [blame] | 1814 | if (tags[i].def || !lang) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1815 | av_dict_set(metadata, key, tags[i].string, 0); |
| 1816 | if (tags[i].sub.nb_elem) |
| 1817 | matroska_convert_tag(s, &tags[i].sub, metadata, key); |
Aurelien Jacobs | f702df3 | 2009-02-15 16:05:37 | [diff] [blame] | 1818 | } |
| 1819 | if (lang) { |
| 1820 | av_strlcat(key, "-", sizeof(key)); |
| 1821 | av_strlcat(key, lang, sizeof(key)); |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 1822 | av_dict_set(metadata, key, tags[i].string, 0); |
Aurelien Jacobs | f702df3 | 2009-02-15 16:05:37 | [diff] [blame] | 1823 | if (tags[i].sub.nb_elem) |
| 1824 | matroska_convert_tag(s, &tags[i].sub, metadata, key); |
| 1825 | } |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1826 | } |
Anton Khirnov | ad7768f | 2010-10-16 13:20:41 | [diff] [blame] | 1827 | ff_metadata_conv(metadata, NULL, ff_mkv_metadata_conv); |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | static void matroska_convert_tags(AVFormatContext *s) |
| 1831 | { |
| 1832 | MatroskaDemuxContext *matroska = s->priv_data; |
| 1833 | MatroskaTags *tags = matroska->tags.elem; |
| 1834 | int i, j; |
| 1835 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1836 | for (i = 0; i < matroska->tags.nb_elem; i++) { |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1837 | if (tags[i].target.attachuid) { |
Diego Biurrun | f69befe | 2014-03-07 12:54:18 | [diff] [blame] | 1838 | MatroskaAttachment *attachment = matroska->attachments.elem; |
Luca Barbato | a0fa6d0 | 2015-10-31 18:45:27 | [diff] [blame] | 1839 | int found = 0; |
| 1840 | for (j = 0; j < matroska->attachments.nb_elem; j++) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1841 | if (attachment[j].uid == tags[i].target.attachuid && |
Luca Barbato | a0fa6d0 | 2015-10-31 18:45:27 | [diff] [blame] | 1842 | attachment[j].stream) { |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1843 | matroska_convert_tag(s, &tags[i].tag, |
| 1844 | &attachment[j].stream->metadata, NULL); |
Luca Barbato | a0fa6d0 | 2015-10-31 18:45:27 | [diff] [blame] | 1845 | found = 1; |
| 1846 | } |
| 1847 | } |
| 1848 | if (!found) { |
Andreas Rheinhardt | 39f5bb6 | 2020-05-17 22:25:48 | [diff] [blame] | 1849 | av_log(s, AV_LOG_WARNING, |
Luca Barbato | a0fa6d0 | 2015-10-31 18:45:27 | [diff] [blame] | 1850 | "The tags at index %d refer to a " |
| 1851 | "non-existent attachment %"PRId64".\n", |
| 1852 | i, tags[i].target.attachuid); |
| 1853 | } |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1854 | } else if (tags[i].target.chapteruid) { |
| 1855 | MatroskaChapter *chapter = matroska->chapters.elem; |
Luca Barbato | a0fa6d0 | 2015-10-31 18:45:27 | [diff] [blame] | 1856 | int found = 0; |
| 1857 | for (j = 0; j < matroska->chapters.nb_elem; j++) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1858 | if (chapter[j].uid == tags[i].target.chapteruid && |
Luca Barbato | a0fa6d0 | 2015-10-31 18:45:27 | [diff] [blame] | 1859 | chapter[j].chapter) { |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1860 | matroska_convert_tag(s, &tags[i].tag, |
| 1861 | &chapter[j].chapter->metadata, NULL); |
Luca Barbato | a0fa6d0 | 2015-10-31 18:45:27 | [diff] [blame] | 1862 | found = 1; |
| 1863 | } |
| 1864 | } |
| 1865 | if (!found) { |
Andreas Rheinhardt | 39f5bb6 | 2020-05-17 22:25:48 | [diff] [blame] | 1866 | av_log(s, AV_LOG_WARNING, |
Luca Barbato | a0fa6d0 | 2015-10-31 18:45:27 | [diff] [blame] | 1867 | "The tags at index %d refer to a non-existent chapter " |
| 1868 | "%"PRId64".\n", |
| 1869 | i, tags[i].target.chapteruid); |
| 1870 | } |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1871 | } else if (tags[i].target.trackuid) { |
| 1872 | MatroskaTrack *track = matroska->tracks.elem; |
Luca Barbato | a0fa6d0 | 2015-10-31 18:45:27 | [diff] [blame] | 1873 | int found = 0; |
| 1874 | for (j = 0; j < matroska->tracks.nb_elem; j++) { |
| 1875 | if (track[j].uid == tags[i].target.trackuid && |
| 1876 | track[j].stream) { |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1877 | matroska_convert_tag(s, &tags[i].tag, |
| 1878 | &track[j].stream->metadata, NULL); |
Luca Barbato | a0fa6d0 | 2015-10-31 18:45:27 | [diff] [blame] | 1879 | found = 1; |
| 1880 | } |
| 1881 | } |
| 1882 | if (!found) { |
Andreas Rheinhardt | 39f5bb6 | 2020-05-17 22:25:48 | [diff] [blame] | 1883 | av_log(s, AV_LOG_WARNING, |
Luca Barbato | a0fa6d0 | 2015-10-31 18:45:27 | [diff] [blame] | 1884 | "The tags at index %d refer to a non-existent track " |
| 1885 | "%"PRId64".\n", |
| 1886 | i, tags[i].target.trackuid); |
| 1887 | } |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1888 | } else { |
Aurelien Jacobs | 4f909c7 | 2009-06-13 22:29:38 | [diff] [blame] | 1889 | matroska_convert_tag(s, &tags[i].tag, &s->metadata, |
| 1890 | tags[i].target.type); |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 1891 | } |
Aurelien Jacobs | 44015c5 | 2008-08-08 23:50:38 | [diff] [blame] | 1892 | } |
| 1893 | } |
| 1894 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1895 | static int matroska_parse_seekhead_entry(MatroskaDemuxContext *matroska, |
Andreas Rheinhardt | 730ac1a | 2019-05-16 22:30:20 | [diff] [blame] | 1896 | int64_t pos) |
Aurelien Jacobs | 13b350a | 2008-08-05 00:40:36 | [diff] [blame] | 1897 | { |
Andreas Rheinhardt | 60f75c9 | 2019-05-16 22:30:13 | [diff] [blame] | 1898 | uint32_t saved_id = matroska->current_id; |
Anton Khirnov | 384c9c2 | 2011-03-03 19:11:45 | [diff] [blame] | 1899 | int64_t before_pos = avio_tell(matroska->ctx->pb); |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1900 | int ret = 0; |
Michael Niedermayer | d115eec | 2021-06-24 17:53:47 | [diff] [blame] | 1901 | int ret2; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1902 | |
Anton Khirnov | f47ac3c | 2011-07-09 06:11:30 | [diff] [blame] | 1903 | /* seek */ |
Andreas Rheinhardt | 730ac1a | 2019-05-16 22:30:20 | [diff] [blame] | 1904 | if (avio_seek(matroska->ctx->pb, pos, SEEK_SET) == pos) { |
Diego Biurrun | 5968d2d | 2008-08-05 08:28:57 | [diff] [blame] | 1905 | /* We don't want to lose our seekhead level, so we add |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1906 | * a dummy. This is a crude hack. */ |
| 1907 | if (matroska->num_levels == EBML_MAX_DEPTH) { |
| 1908 | av_log(matroska->ctx, AV_LOG_INFO, |
| 1909 | "Max EBML element depth (%d) reached, " |
| 1910 | "cannot parse further.\n", EBML_MAX_DEPTH); |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1911 | ret = AVERROR_INVALIDDATA; |
| 1912 | } else { |
Andreas Rheinhardt | 60f75c9 | 2019-05-16 22:30:13 | [diff] [blame] | 1913 | matroska->levels[matroska->num_levels] = (MatroskaLevel) { 0, EBML_UNKNOWN_LENGTH }; |
Anton Khirnov | f47ac3c | 2011-07-09 06:11:30 | [diff] [blame] | 1914 | matroska->num_levels++; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1915 | matroska->current_id = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1916 | |
Dustin Brody | 4a9628f | 2011-09-08 22:43:32 | [diff] [blame] | 1917 | ret = ebml_parse(matroska, matroska_segment, matroska); |
Andreas Rheinhardt | b31c9b7 | 2019-06-23 23:46:54 | [diff] [blame] | 1918 | if (ret == LEVEL_ENDED) { |
| 1919 | /* This can only happen if the seek brought us beyond EOF. */ |
| 1920 | ret = AVERROR_EOF; |
| 1921 | } |
Aurelien Jacobs | 4348571 | 2008-08-05 00:40:43 | [diff] [blame] | 1922 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1923 | } |
Andreas Rheinhardt | a9f0515 | 2019-05-16 22:30:09 | [diff] [blame] | 1924 | /* Seek back - notice that in all instances where this is used |
| 1925 | * it is safe to set the level to 1. */ |
Michael Niedermayer | d115eec | 2021-06-24 17:53:47 | [diff] [blame] | 1926 | ret2 = matroska_reset_status(matroska, saved_id, before_pos); |
| 1927 | if (ret >= 0) |
| 1928 | ret = ret2; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1929 | |
| 1930 | return ret; |
| 1931 | } |
| 1932 | |
| 1933 | static void matroska_execute_seekhead(MatroskaDemuxContext *matroska) |
| 1934 | { |
| 1935 | EbmlList *seekhead_list = &matroska->seekhead; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1936 | int i; |
| 1937 | |
| 1938 | // we should not do any seeking in the streaming case |
James Almer | 4de591e | 2017-03-21 20:02:30 | [diff] [blame] | 1939 | if (!(matroska->ctx->pb->seekable & AVIO_SEEKABLE_NORMAL)) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1940 | return; |
| 1941 | |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1942 | for (i = 0; i < seekhead_list->nb_elem; i++) { |
| 1943 | MatroskaSeekhead *seekheads = seekhead_list->elem; |
Andreas Rheinhardt | 730ac1a | 2019-05-16 22:30:20 | [diff] [blame] | 1944 | uint32_t id = seekheads[i].id; |
| 1945 | int64_t pos = seekheads[i].pos + matroska->segment_start; |
Andreas Rheinhardt | 7c243ee | 2019-08-30 11:20:26 | [diff] [blame] | 1946 | MatroskaLevel1Element *elem; |
wm4 | 6551aca | 2014-12-06 15:53:30 | [diff] [blame] | 1947 | |
Andreas Rheinhardt | 7c243ee | 2019-08-30 11:20:26 | [diff] [blame] | 1948 | if (id != seekheads[i].id || pos < matroska->segment_start) |
| 1949 | continue; |
| 1950 | |
Andreas Rheinhardt | 7e91035 | 2020-04-30 21:16:44 | [diff] [blame] | 1951 | elem = matroska_find_level1_elem(matroska, id, pos); |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1952 | if (!elem || elem->parsed) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1953 | continue; |
| 1954 | |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1955 | elem->pos = pos; |
| 1956 | |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1957 | // defer cues parsing until we actually need cue data. |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1958 | if (id == MATROSKA_ID_CUES) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1959 | continue; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1960 | |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1961 | if (matroska_parse_seekhead_entry(matroska, pos) < 0) { |
Reimar Döffinger | 47e015e | 2012-02-12 13:09:03 | [diff] [blame] | 1962 | // mark index as broken |
| 1963 | matroska->cues_parsing_deferred = -1; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1964 | break; |
Reimar Döffinger | 47e015e | 2012-02-12 13:09:03 | [diff] [blame] | 1965 | } |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 1966 | |
| 1967 | elem->parsed = 1; |
Michael Niedermayer | 72c9844 | 2014-12-07 21:45:34 | [diff] [blame] | 1968 | } |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1969 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1970 | |
Michael Niedermayer | b3d9ab1 | 2014-03-07 22:46:37 | [diff] [blame] | 1971 | static void matroska_add_index_entries(MatroskaDemuxContext *matroska) |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1972 | { |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1973 | EbmlList *index_list; |
| 1974 | MatroskaIndex *index; |
Andreas Cadhalpun | eb9fb50 | 2015-05-03 21:07:20 | [diff] [blame] | 1975 | uint64_t index_scale = 1; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1976 | int i, j; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 1977 | |
wm4 | 7862325 | 2015-02-09 19:39:01 | [diff] [blame] | 1978 | if (matroska->ctx->flags & AVFMT_FLAG_IGNIDX) |
| 1979 | return; |
| 1980 | |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1981 | index_list = &matroska->index; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1982 | index = index_list->elem; |
Rodger Combs | 4f7d9b7 | 2015-10-08 20:34:59 | [diff] [blame] | 1983 | if (index_list->nb_elem < 2) |
| 1984 | return; |
| 1985 | if (index[1].time > 1E14 / matroska->time_scale) { |
| 1986 | av_log(matroska->ctx, AV_LOG_WARNING, "Dropping apparently-broken index.\n"); |
| 1987 | return; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1988 | } |
| 1989 | for (i = 0; i < index_list->nb_elem; i++) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1990 | EbmlList *pos_list = &index[i].pos; |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1991 | MatroskaIndexPos *pos = pos_list->elem; |
| 1992 | for (j = 0; j < pos_list->nb_elem; j++) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1993 | MatroskaTrack *track = matroska_find_track_by_num(matroska, |
| 1994 | pos[j].track); |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1995 | if (track && track->stream) |
| 1996 | av_add_index_entry(track->stream, |
| 1997 | pos[j].pos + matroska->segment_start, |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 1998 | index[i].time / index_scale, 0, 0, |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 1999 | AVINDEX_KEYFRAME); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2000 | } |
| 2001 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2002 | } |
| 2003 | |
Aaron Colwell | 6c4cc0f | 2011-09-22 14:51:00 | [diff] [blame] | 2004 | static void matroska_parse_cues(MatroskaDemuxContext *matroska) { |
Aaron Colwell | 6c4cc0f | 2011-09-22 14:51:00 | [diff] [blame] | 2005 | int i; |
| 2006 | |
wm4 | 7862325 | 2015-02-09 19:39:01 | [diff] [blame] | 2007 | if (matroska->ctx->flags & AVFMT_FLAG_IGNIDX) |
| 2008 | return; |
| 2009 | |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 2010 | for (i = 0; i < matroska->num_level1_elems; i++) { |
| 2011 | MatroskaLevel1Element *elem = &matroska->level1_elems[i]; |
| 2012 | if (elem->id == MATROSKA_ID_CUES && !elem->parsed) { |
| 2013 | if (matroska_parse_seekhead_entry(matroska, elem->pos) < 0) |
| 2014 | matroska->cues_parsing_deferred = -1; |
| 2015 | elem->parsed = 1; |
Aaron Colwell | 6c4cc0f | 2011-09-22 14:51:00 | [diff] [blame] | 2016 | break; |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 2017 | } |
| 2018 | } |
Aaron Colwell | 6c4cc0f | 2011-09-22 14:51:00 | [diff] [blame] | 2019 | |
Aaron Colwell | 6c4cc0f | 2011-09-22 14:51:00 | [diff] [blame] | 2020 | matroska_add_index_entries(matroska); |
| 2021 | } |
| 2022 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 2023 | static int matroska_aac_profile(char *codec_id) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2024 | { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2025 | static const char *const aac_profiles[] = { "MAIN", "LC", "SSR" }; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2026 | int profile; |
| 2027 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2028 | for (profile = 0; profile < FF_ARRAY_ELEMS(aac_profiles); profile++) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2029 | if (strstr(codec_id, aac_profiles[profile])) |
| 2030 | break; |
| 2031 | return profile + 1; |
| 2032 | } |
| 2033 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 2034 | static int matroska_aac_sri(int samplerate) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2035 | { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2036 | int sri; |
| 2037 | |
Andreas Rheinhardt | 49bf945 | 2021-02-10 18:37:37 | [diff] [blame] | 2038 | for (sri = 0; sri < FF_ARRAY_ELEMS(ff_mpeg4audio_sample_rates); sri++) |
| 2039 | if (ff_mpeg4audio_sample_rates[sri] == samplerate) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2040 | break; |
| 2041 | return sri; |
| 2042 | } |
| 2043 | |
Aaron Colwell | 2e06113 | 2012-03-05 18:02:48 | [diff] [blame] | 2044 | static void matroska_metadata_creation_time(AVDictionary **metadata, int64_t date_utc) |
| 2045 | { |
Aaron Colwell | 2e06113 | 2012-03-05 18:02:48 | [diff] [blame] | 2046 | /* Convert to seconds and adjust by number of seconds between 2001-01-01 and Epoch */ |
Marton Balint | 13b90ff | 2016-06-29 22:55:48 | [diff] [blame] | 2047 | avpriv_dict_set_timestamp(metadata, "creation_time", date_utc / 1000 + 978307200000000LL); |
Aaron Colwell | 2e06113 | 2012-03-05 18:02:48 | [diff] [blame] | 2048 | } |
| 2049 | |
Anton Khirnov | 4efdadc | 2014-05-25 12:05:51 | [diff] [blame] | 2050 | static int matroska_parse_flac(AVFormatContext *s, |
| 2051 | MatroskaTrack *track, |
| 2052 | int *offset) |
| 2053 | { |
Anton Khirnov | 23f741f | 2014-05-26 10:48:56 | [diff] [blame] | 2054 | AVStream *st = track->stream; |
Anton Khirnov | 4efdadc | 2014-05-25 12:05:51 | [diff] [blame] | 2055 | uint8_t *p = track->codec_priv.data; |
| 2056 | int size = track->codec_priv.size; |
| 2057 | |
| 2058 | if (size < 8 + FLAC_STREAMINFO_SIZE || p[4] & 0x7f) { |
| 2059 | av_log(s, AV_LOG_WARNING, "Invalid FLAC private data\n"); |
| 2060 | track->codec_priv.size = 0; |
| 2061 | return 0; |
| 2062 | } |
| 2063 | *offset = 8; |
| 2064 | track->codec_priv.size = 8 + FLAC_STREAMINFO_SIZE; |
| 2065 | |
Anton Khirnov | 23f741f | 2014-05-26 10:48:56 | [diff] [blame] | 2066 | p += track->codec_priv.size; |
| 2067 | size -= track->codec_priv.size; |
| 2068 | |
| 2069 | /* parse the remaining metadata blocks if present */ |
| 2070 | while (size >= 4) { |
| 2071 | int block_last, block_type, block_size; |
| 2072 | |
| 2073 | flac_parse_block_header(p, &block_last, &block_type, &block_size); |
| 2074 | |
| 2075 | p += 4; |
| 2076 | size -= 4; |
| 2077 | if (block_size > size) |
| 2078 | return 0; |
| 2079 | |
| 2080 | /* check for the channel mask */ |
| 2081 | if (block_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) { |
| 2082 | AVDictionary *dict = NULL; |
| 2083 | AVDictionaryEntry *chmask; |
| 2084 | |
| 2085 | ff_vorbis_comment(s, &dict, p, block_size, 0); |
| 2086 | chmask = av_dict_get(dict, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0); |
| 2087 | if (chmask) { |
| 2088 | uint64_t mask = strtol(chmask->value, NULL, 0); |
| 2089 | if (!mask || mask & ~0x3ffffULL) { |
| 2090 | av_log(s, AV_LOG_WARNING, |
| 2091 | "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n"); |
| 2092 | } else |
Vittorio Giovara | 8e3fab8 | 2017-03-31 14:57:00 | [diff] [blame] | 2093 | av_channel_layout_from_mask(&st->codecpar->ch_layout, mask); |
Anton Khirnov | 23f741f | 2014-05-26 10:48:56 | [diff] [blame] | 2094 | } |
| 2095 | av_dict_free(&dict); |
| 2096 | } |
| 2097 | |
| 2098 | p += block_size; |
| 2099 | size -= block_size; |
| 2100 | } |
| 2101 | |
Anton Khirnov | 4efdadc | 2014-05-25 12:05:51 | [diff] [blame] | 2102 | return 0; |
| 2103 | } |
| 2104 | |
Andreas Rheinhardt | dd17c86 | 2022-01-19 22:20:05 | [diff] [blame] | 2105 | static int mkv_field_order(MatroskaDemuxContext *matroska, uint64_t field_order) |
Luca Barbato | 5f02266 | 2016-03-28 18:29:54 | [diff] [blame] | 2106 | { |
Andreas Rheinhardt | 6f134c6 | 2020-07-20 17:14:38 | [diff] [blame] | 2107 | int minor, micro, bttb = 0; |
James Almer | 1273bc6 | 2016-10-13 17:22:07 | [diff] [blame] | 2108 | |
| 2109 | /* workaround a bug in our Matroska muxer, introduced in version 57.36 alongside |
| 2110 | * this function, and fixed in 57.52 */ |
Andreas Rheinhardt | 6f134c6 | 2020-07-20 17:14:38 | [diff] [blame] | 2111 | if (matroska->muxingapp && sscanf(matroska->muxingapp, "Lavf57.%d.%d", &minor, µ) == 2) |
| 2112 | bttb = (minor >= 36 && minor <= 51 && micro >= 100); |
James Almer | 1273bc6 | 2016-10-13 17:22:07 | [diff] [blame] | 2113 | |
Luca Barbato | 5f02266 | 2016-03-28 18:29:54 | [diff] [blame] | 2114 | switch (field_order) { |
| 2115 | case MATROSKA_VIDEO_FIELDORDER_PROGRESSIVE: |
| 2116 | return AV_FIELD_PROGRESSIVE; |
| 2117 | case MATROSKA_VIDEO_FIELDORDER_UNDETERMINED: |
| 2118 | return AV_FIELD_UNKNOWN; |
| 2119 | case MATROSKA_VIDEO_FIELDORDER_TT: |
| 2120 | return AV_FIELD_TT; |
| 2121 | case MATROSKA_VIDEO_FIELDORDER_BB: |
| 2122 | return AV_FIELD_BB; |
| 2123 | case MATROSKA_VIDEO_FIELDORDER_BT: |
James Almer | 1273bc6 | 2016-10-13 17:22:07 | [diff] [blame] | 2124 | return bttb ? AV_FIELD_TB : AV_FIELD_BT; |
Luca Barbato | 5f02266 | 2016-03-28 18:29:54 | [diff] [blame] | 2125 | case MATROSKA_VIDEO_FIELDORDER_TB: |
James Almer | 1273bc6 | 2016-10-13 17:22:07 | [diff] [blame] | 2126 | return bttb ? AV_FIELD_BT : AV_FIELD_TB; |
Luca Barbato | 5f02266 | 2016-03-28 18:29:54 | [diff] [blame] | 2127 | default: |
| 2128 | return AV_FIELD_UNKNOWN; |
| 2129 | } |
| 2130 | } |
| 2131 | |
Aaron Colwell | febfb49 | 2015-12-02 23:13:18 | [diff] [blame] | 2132 | static void mkv_stereo_mode_display_mul(int stereo_mode, |
| 2133 | int *h_width, int *h_height) |
Aaron Colwell | ec83efd | 2015-11-23 20:06:14 | [diff] [blame] | 2134 | { |
| 2135 | switch (stereo_mode) { |
| 2136 | case MATROSKA_VIDEO_STEREOMODE_TYPE_MONO: |
| 2137 | case MATROSKA_VIDEO_STEREOMODE_TYPE_CHECKERBOARD_RL: |
| 2138 | case MATROSKA_VIDEO_STEREOMODE_TYPE_CHECKERBOARD_LR: |
| 2139 | case MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_RL: |
| 2140 | case MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_LR: |
| 2141 | break; |
| 2142 | case MATROSKA_VIDEO_STEREOMODE_TYPE_RIGHT_LEFT: |
| 2143 | case MATROSKA_VIDEO_STEREOMODE_TYPE_LEFT_RIGHT: |
| 2144 | case MATROSKA_VIDEO_STEREOMODE_TYPE_COL_INTERLEAVED_RL: |
| 2145 | case MATROSKA_VIDEO_STEREOMODE_TYPE_COL_INTERLEAVED_LR: |
| 2146 | *h_width = 2; |
| 2147 | break; |
| 2148 | case MATROSKA_VIDEO_STEREOMODE_TYPE_BOTTOM_TOP: |
| 2149 | case MATROSKA_VIDEO_STEREOMODE_TYPE_TOP_BOTTOM: |
| 2150 | case MATROSKA_VIDEO_STEREOMODE_TYPE_ROW_INTERLEAVED_RL: |
| 2151 | case MATROSKA_VIDEO_STEREOMODE_TYPE_ROW_INTERLEAVED_LR: |
| 2152 | *h_height = 2; |
| 2153 | break; |
| 2154 | } |
| 2155 | } |
| 2156 | |
Neil Birkbeck | bbda13a | 2016-03-01 00:41:04 | [diff] [blame] | 2157 | static int mkv_parse_video_color(AVStream *st, const MatroskaTrack *track) { |
James Almer | 4e75907 | 2016-12-05 02:22:39 | [diff] [blame] | 2158 | const MatroskaTrackVideoColor *color = track->video.color.elem; |
| 2159 | const MatroskaMasteringMeta *mastering_meta; |
| 2160 | int has_mastering_primaries, has_mastering_luminance; |
| 2161 | |
| 2162 | if (!track->video.color.nb_elem) |
| 2163 | return 0; |
| 2164 | |
| 2165 | mastering_meta = &color->mastering_meta; |
Neil Birkbeck | bbda13a | 2016-03-01 00:41:04 | [diff] [blame] | 2166 | // Mastering primaries are CIE 1931 coords, and must be > 0. |
James Almer | 4e75907 | 2016-12-05 02:22:39 | [diff] [blame] | 2167 | has_mastering_primaries = |
Neil Birkbeck | bbda13a | 2016-03-01 00:41:04 | [diff] [blame] | 2168 | mastering_meta->r_x > 0 && mastering_meta->r_y > 0 && |
| 2169 | mastering_meta->g_x > 0 && mastering_meta->g_y > 0 && |
| 2170 | mastering_meta->b_x > 0 && mastering_meta->b_y > 0 && |
| 2171 | mastering_meta->white_x > 0 && mastering_meta->white_y > 0; |
Andreas Rheinhardt | e8f3901 | 2021-02-16 14:34:06 | [diff] [blame] | 2172 | has_mastering_luminance = mastering_meta->max_luminance > |
| 2173 | mastering_meta->min_luminance.el.f && |
| 2174 | mastering_meta->min_luminance.el.f >= 0 && |
| 2175 | mastering_meta->min_luminance.count; |
Neil Birkbeck | bbda13a | 2016-03-01 00:41:04 | [diff] [blame] | 2176 | |
James Almer | 4e75907 | 2016-12-05 02:22:39 | [diff] [blame] | 2177 | if (color->matrix_coefficients != AVCOL_SPC_RESERVED) |
| 2178 | st->codecpar->color_space = color->matrix_coefficients; |
| 2179 | if (color->primaries != AVCOL_PRI_RESERVED && |
| 2180 | color->primaries != AVCOL_PRI_RESERVED0) |
| 2181 | st->codecpar->color_primaries = color->primaries; |
| 2182 | if (color->transfer_characteristics != AVCOL_TRC_RESERVED && |
| 2183 | color->transfer_characteristics != AVCOL_TRC_RESERVED0) |
| 2184 | st->codecpar->color_trc = color->transfer_characteristics; |
| 2185 | if (color->range != AVCOL_RANGE_UNSPECIFIED && |
| 2186 | color->range <= AVCOL_RANGE_JPEG) |
| 2187 | st->codecpar->color_range = color->range; |
| 2188 | if (color->chroma_siting_horz != MATROSKA_COLOUR_CHROMASITINGHORZ_UNDETERMINED && |
| 2189 | color->chroma_siting_vert != MATROSKA_COLOUR_CHROMASITINGVERT_UNDETERMINED && |
| 2190 | color->chroma_siting_horz < MATROSKA_COLOUR_CHROMASITINGHORZ_NB && |
| 2191 | color->chroma_siting_vert < MATROSKA_COLOUR_CHROMASITINGVERT_NB) { |
James Almer | 47ee6f1 | 2016-10-18 02:50:14 | [diff] [blame] | 2192 | st->codecpar->chroma_location = |
Andreas Rheinhardt | 832e656 | 2022-09-21 00:36:04 | [diff] [blame] | 2193 | av_chroma_location_pos_to_enum((color->chroma_siting_horz - 1) << 7, |
| 2194 | (color->chroma_siting_vert - 1) << 7); |
James Almer | 47ee6f1 | 2016-10-18 02:50:14 | [diff] [blame] | 2195 | } |
James Almer | 095147a | 2017-04-25 18:04:00 | [diff] [blame] | 2196 | if (color->max_cll && color->max_fall) { |
| 2197 | size_t size = 0; |
| 2198 | int ret; |
| 2199 | AVContentLightMetadata *metadata = av_content_light_metadata_alloc(&size); |
| 2200 | if (!metadata) |
| 2201 | return AVERROR(ENOMEM); |
| 2202 | ret = av_stream_add_side_data(st, AV_PKT_DATA_CONTENT_LIGHT_LEVEL, |
| 2203 | (uint8_t *)metadata, size); |
| 2204 | if (ret < 0) { |
| 2205 | av_freep(&metadata); |
| 2206 | return ret; |
| 2207 | } |
| 2208 | metadata->MaxCLL = color->max_cll; |
| 2209 | metadata->MaxFALL = color->max_fall; |
| 2210 | } |
Neil Birkbeck | bbda13a | 2016-03-01 00:41:04 | [diff] [blame] | 2211 | |
| 2212 | if (has_mastering_primaries || has_mastering_luminance) { |
Neil Birkbeck | bbda13a | 2016-03-01 00:41:04 | [diff] [blame] | 2213 | AVMasteringDisplayMetadata *metadata = |
| 2214 | (AVMasteringDisplayMetadata*) av_stream_new_side_data( |
| 2215 | st, AV_PKT_DATA_MASTERING_DISPLAY_METADATA, |
| 2216 | sizeof(AVMasteringDisplayMetadata)); |
| 2217 | if (!metadata) { |
| 2218 | return AVERROR(ENOMEM); |
| 2219 | } |
| 2220 | memset(metadata, 0, sizeof(AVMasteringDisplayMetadata)); |
| 2221 | if (has_mastering_primaries) { |
James Almer | 3b4e9a3 | 2019-10-03 23:52:20 | [diff] [blame] | 2222 | metadata->display_primaries[0][0] = av_d2q(mastering_meta->r_x, INT_MAX); |
| 2223 | metadata->display_primaries[0][1] = av_d2q(mastering_meta->r_y, INT_MAX); |
| 2224 | metadata->display_primaries[1][0] = av_d2q(mastering_meta->g_x, INT_MAX); |
| 2225 | metadata->display_primaries[1][1] = av_d2q(mastering_meta->g_y, INT_MAX); |
| 2226 | metadata->display_primaries[2][0] = av_d2q(mastering_meta->b_x, INT_MAX); |
| 2227 | metadata->display_primaries[2][1] = av_d2q(mastering_meta->b_y, INT_MAX); |
| 2228 | metadata->white_point[0] = av_d2q(mastering_meta->white_x, INT_MAX); |
| 2229 | metadata->white_point[1] = av_d2q(mastering_meta->white_y, INT_MAX); |
Neil Birkbeck | bbda13a | 2016-03-01 00:41:04 | [diff] [blame] | 2230 | metadata->has_primaries = 1; |
| 2231 | } |
| 2232 | if (has_mastering_luminance) { |
James Almer | 3b4e9a3 | 2019-10-03 23:52:20 | [diff] [blame] | 2233 | metadata->max_luminance = av_d2q(mastering_meta->max_luminance, INT_MAX); |
Andreas Rheinhardt | e8f3901 | 2021-02-16 14:34:06 | [diff] [blame] | 2234 | metadata->min_luminance = av_d2q(mastering_meta->min_luminance.el.f, INT_MAX); |
Neil Birkbeck | bbda13a | 2016-03-01 00:41:04 | [diff] [blame] | 2235 | metadata->has_luminance = 1; |
| 2236 | } |
| 2237 | } |
| 2238 | return 0; |
| 2239 | } |
| 2240 | |
Andreas Rheinhardt | 937bb6b | 2022-01-21 18:42:50 | [diff] [blame] | 2241 | static int mkv_create_display_matrix(AVStream *st, |
| 2242 | const MatroskaTrackVideoProjection *proj, |
| 2243 | void *logctx) |
| 2244 | { |
| 2245 | double pitch = proj->pitch, yaw = proj->yaw, roll = proj->roll; |
| 2246 | int32_t *matrix; |
| 2247 | int hflip; |
| 2248 | |
| 2249 | if (pitch == 0.0 && yaw == 0.0 && roll == 0.0) |
| 2250 | return 0; |
| 2251 | |
| 2252 | /* Note: The following constants are exactly representable |
| 2253 | * as floating-point numbers. */ |
| 2254 | if (pitch != 0.0 || (yaw != 0.0 && yaw != 180.0 && yaw != -180.0) || |
| 2255 | isnan(roll)) { |
| 2256 | av_log(logctx, AV_LOG_WARNING, "Ignoring non-2D rectangular " |
| 2257 | "projection in stream %u (yaw %f, pitch %f, roll %f)\n", |
| 2258 | st->index, yaw, pitch, roll); |
| 2259 | return 0; |
| 2260 | } |
| 2261 | matrix = (int32_t*)av_stream_new_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, |
| 2262 | 9 * sizeof(*matrix)); |
| 2263 | if (!matrix) |
| 2264 | return AVERROR(ENOMEM); |
| 2265 | |
| 2266 | hflip = yaw != 0.0; |
| 2267 | /* ProjectionPoseRoll is in the counter-clockwise direction |
| 2268 | * whereas av_display_rotation_set() expects its argument |
| 2269 | * to be oriented clockwise, so we need to negate roll. |
| 2270 | * Furthermore, if hflip is set, we need to negate it again |
| 2271 | * to account for the fact that the Matroska specifications |
| 2272 | * require the yaw rotation to be applied first. */ |
| 2273 | av_display_rotation_set(matrix, roll * (2 * hflip - 1)); |
| 2274 | av_display_matrix_flip(matrix, hflip, 0); |
| 2275 | |
| 2276 | return 0; |
| 2277 | } |
| 2278 | |
Andreas Rheinhardt | 39f5bb6 | 2020-05-17 22:25:48 | [diff] [blame] | 2279 | static int mkv_parse_video_projection(AVStream *st, const MatroskaTrack *track, |
| 2280 | void *logctx) |
| 2281 | { |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2282 | AVSphericalMapping *spherical; |
Andreas Rheinhardt | 880519c | 2020-07-19 08:16:33 | [diff] [blame] | 2283 | const MatroskaTrackVideoProjection *mkv_projection = &track->video.projection; |
| 2284 | const uint8_t *priv_data = mkv_projection->private.data; |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2285 | enum AVSphericalProjection projection; |
| 2286 | size_t spherical_size; |
Vittorio Giovara | f20bcec | 2017-03-16 21:20:47 | [diff] [blame] | 2287 | uint32_t l = 0, t = 0, r = 0, b = 0; |
| 2288 | uint32_t padding = 0; |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2289 | int ret; |
Vittorio Giovara | bde9642 | 2017-02-10 21:02:22 | [diff] [blame] | 2290 | |
Andreas Rheinhardt | 880519c | 2020-07-19 08:16:33 | [diff] [blame] | 2291 | if (mkv_projection->private.size && priv_data[0] != 0) { |
Andreas Rheinhardt | 39f5bb6 | 2020-05-17 22:25:48 | [diff] [blame] | 2292 | av_log(logctx, AV_LOG_WARNING, "Unknown spherical metadata\n"); |
Vittorio Giovara | bde9642 | 2017-02-10 21:02:22 | [diff] [blame] | 2293 | return 0; |
| 2294 | } |
| 2295 | |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2296 | switch (track->video.projection.type) { |
Andreas Rheinhardt | 937bb6b | 2022-01-21 18:42:50 | [diff] [blame] | 2297 | case MATROSKA_VIDEO_PROJECTION_TYPE_RECTANGULAR: |
| 2298 | return mkv_create_display_matrix(st, mkv_projection, logctx); |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2299 | case MATROSKA_VIDEO_PROJECTION_TYPE_EQUIRECTANGULAR: |
Vittorio Giovara | 9ae3506 | 2017-03-07 22:34:32 | [diff] [blame] | 2300 | if (track->video.projection.private.size == 20) { |
Andreas Rheinhardt | 880519c | 2020-07-19 08:16:33 | [diff] [blame] | 2301 | t = AV_RB32(priv_data + 4); |
| 2302 | b = AV_RB32(priv_data + 8); |
| 2303 | l = AV_RB32(priv_data + 12); |
| 2304 | r = AV_RB32(priv_data + 16); |
Vittorio Giovara | bde9642 | 2017-02-10 21:02:22 | [diff] [blame] | 2305 | |
| 2306 | if (b >= UINT_MAX - t || r >= UINT_MAX - l) { |
Andreas Rheinhardt | 39f5bb6 | 2020-05-17 22:25:48 | [diff] [blame] | 2307 | av_log(logctx, AV_LOG_ERROR, |
Vittorio Giovara | bde9642 | 2017-02-10 21:02:22 | [diff] [blame] | 2308 | "Invalid bounding rectangle coordinates " |
Vittorio Giovara | f20bcec | 2017-03-16 21:20:47 | [diff] [blame] | 2309 | "%"PRIu32",%"PRIu32",%"PRIu32",%"PRIu32"\n", |
Vittorio Giovara | bde9642 | 2017-02-10 21:02:22 | [diff] [blame] | 2310 | l, t, r, b); |
| 2311 | return AVERROR_INVALIDDATA; |
| 2312 | } |
Vittorio Giovara | 9ae3506 | 2017-03-07 22:34:32 | [diff] [blame] | 2313 | } else if (track->video.projection.private.size != 0) { |
Andreas Rheinhardt | 39f5bb6 | 2020-05-17 22:25:48 | [diff] [blame] | 2314 | av_log(logctx, AV_LOG_ERROR, "Unknown spherical metadata\n"); |
Vittorio Giovara | bde9642 | 2017-02-10 21:02:22 | [diff] [blame] | 2315 | return AVERROR_INVALIDDATA; |
| 2316 | } |
Vittorio Giovara | 9ae3506 | 2017-03-07 22:34:32 | [diff] [blame] | 2317 | |
| 2318 | if (l || t || r || b) |
| 2319 | projection = AV_SPHERICAL_EQUIRECTANGULAR_TILE; |
| 2320 | else |
| 2321 | projection = AV_SPHERICAL_EQUIRECTANGULAR; |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2322 | break; |
| 2323 | case MATROSKA_VIDEO_PROJECTION_TYPE_CUBEMAP: |
Vittorio Giovara | bde9642 | 2017-02-10 21:02:22 | [diff] [blame] | 2324 | if (track->video.projection.private.size < 4) { |
Andreas Rheinhardt | 39f5bb6 | 2020-05-17 22:25:48 | [diff] [blame] | 2325 | av_log(logctx, AV_LOG_ERROR, "Missing projection private properties\n"); |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2326 | return AVERROR_INVALIDDATA; |
Vittorio Giovara | bde9642 | 2017-02-10 21:02:22 | [diff] [blame] | 2327 | } else if (track->video.projection.private.size == 12) { |
Andreas Rheinhardt | 880519c | 2020-07-19 08:16:33 | [diff] [blame] | 2328 | uint32_t layout = AV_RB32(priv_data + 4); |
Vittorio Giovara | 9ae3506 | 2017-03-07 22:34:32 | [diff] [blame] | 2329 | if (layout) { |
Andreas Rheinhardt | 39f5bb6 | 2020-05-17 22:25:48 | [diff] [blame] | 2330 | av_log(logctx, AV_LOG_WARNING, |
Vittorio Giovara | bde9642 | 2017-02-10 21:02:22 | [diff] [blame] | 2331 | "Unknown spherical cubemap layout %"PRIu32"\n", layout); |
| 2332 | return 0; |
| 2333 | } |
Vittorio Giovara | 9ae3506 | 2017-03-07 22:34:32 | [diff] [blame] | 2334 | projection = AV_SPHERICAL_CUBEMAP; |
Andreas Rheinhardt | 880519c | 2020-07-19 08:16:33 | [diff] [blame] | 2335 | padding = AV_RB32(priv_data + 8); |
Vittorio Giovara | bde9642 | 2017-02-10 21:02:22 | [diff] [blame] | 2336 | } else { |
Andreas Rheinhardt | 39f5bb6 | 2020-05-17 22:25:48 | [diff] [blame] | 2337 | av_log(logctx, AV_LOG_ERROR, "Unknown spherical metadata\n"); |
Vittorio Giovara | bde9642 | 2017-02-10 21:02:22 | [diff] [blame] | 2338 | return AVERROR_INVALIDDATA; |
| 2339 | } |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2340 | break; |
| 2341 | default: |
Andreas Rheinhardt | 39f5bb6 | 2020-05-17 22:25:48 | [diff] [blame] | 2342 | av_log(logctx, AV_LOG_WARNING, |
James Almer | 251849f | 2016-12-06 17:48:45 | [diff] [blame] | 2343 | "Unknown spherical metadata type %"PRIu64"\n", |
| 2344 | track->video.projection.type); |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2345 | return 0; |
| 2346 | } |
| 2347 | |
| 2348 | spherical = av_spherical_alloc(&spherical_size); |
| 2349 | if (!spherical) |
| 2350 | return AVERROR(ENOMEM); |
James Almer | 251849f | 2016-12-06 17:48:45 | [diff] [blame] | 2351 | |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2352 | spherical->projection = projection; |
| 2353 | |
James Almer | 251849f | 2016-12-06 17:48:45 | [diff] [blame] | 2354 | spherical->yaw = (int32_t) (track->video.projection.yaw * (1 << 16)); |
| 2355 | spherical->pitch = (int32_t) (track->video.projection.pitch * (1 << 16)); |
| 2356 | spherical->roll = (int32_t) (track->video.projection.roll * (1 << 16)); |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2357 | |
Vittorio Giovara | bde9642 | 2017-02-10 21:02:22 | [diff] [blame] | 2358 | spherical->padding = padding; |
| 2359 | |
| 2360 | spherical->bound_left = l; |
| 2361 | spherical->bound_top = t; |
| 2362 | spherical->bound_right = r; |
| 2363 | spherical->bound_bottom = b; |
| 2364 | |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2365 | ret = av_stream_add_side_data(st, AV_PKT_DATA_SPHERICAL, (uint8_t *)spherical, |
| 2366 | spherical_size); |
James Almer | 8396d54 | 2016-12-08 04:21:11 | [diff] [blame] | 2367 | if (ret < 0) { |
| 2368 | av_freep(&spherical); |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2369 | return ret; |
James Almer | 8396d54 | 2016-12-08 04:21:11 | [diff] [blame] | 2370 | } |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2371 | |
| 2372 | return 0; |
| 2373 | } |
| 2374 | |
quietvoid | 05c0794 | 2022-01-01 16:51:50 | [diff] [blame] | 2375 | static int mkv_parse_dvcc_dvvc(AVFormatContext *s, AVStream *st, const MatroskaTrack *track, |
| 2376 | EbmlBin *bin) |
| 2377 | { |
| 2378 | return ff_isom_parse_dvcc_dvvc(s, st, bin->data, bin->size); |
| 2379 | } |
| 2380 | |
| 2381 | static int mkv_parse_block_addition_mappings(AVFormatContext *s, AVStream *st, const MatroskaTrack *track) |
| 2382 | { |
| 2383 | const EbmlList *mappings_list = &track->block_addition_mappings; |
| 2384 | MatroskaBlockAdditionMapping *mappings = mappings_list->elem; |
| 2385 | int ret; |
| 2386 | |
| 2387 | for (int i = 0; i < mappings_list->nb_elem; i++) { |
| 2388 | MatroskaBlockAdditionMapping *mapping = &mappings[i]; |
| 2389 | |
| 2390 | switch (mapping->type) { |
| 2391 | case MKBETAG('d','v','c','C'): |
| 2392 | case MKBETAG('d','v','v','C'): |
| 2393 | if ((ret = mkv_parse_dvcc_dvvc(s, st, track, &mapping->extradata)) < 0) |
| 2394 | return ret; |
| 2395 | |
| 2396 | break; |
| 2397 | default: |
| 2398 | av_log(s, AV_LOG_DEBUG, |
| 2399 | "Unknown block additional mapping type 0x%"PRIx64", value %"PRIu64", name \"%s\"\n", |
| 2400 | mapping->type, mapping->value, mapping->name ? mapping->name : ""); |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | return 0; |
| 2405 | } |
| 2406 | |
Mats Peterson | 71f73ee | 2016-01-10 02:59:21 | [diff] [blame] | 2407 | static int get_qt_codec(MatroskaTrack *track, uint32_t *fourcc, enum AVCodecID *codec_id) |
| 2408 | { |
| 2409 | const AVCodecTag *codec_tags; |
| 2410 | |
| 2411 | codec_tags = track->type == MATROSKA_TRACK_TYPE_VIDEO ? |
| 2412 | ff_codec_movvideo_tags : ff_codec_movaudio_tags; |
| 2413 | |
| 2414 | /* Normalize noncompliant private data that starts with the fourcc |
| 2415 | * by expanding/shifting the data by 4 bytes and storing the data |
| 2416 | * size at the start. */ |
| 2417 | if (ff_codec_get_id(codec_tags, AV_RL32(track->codec_priv.data))) { |
James Almer | a618866 | 2018-04-04 17:12:54 | [diff] [blame] | 2418 | int ret = av_buffer_realloc(&track->codec_priv.buf, |
| 2419 | track->codec_priv.size + 4 + AV_INPUT_BUFFER_PADDING_SIZE); |
| 2420 | if (ret < 0) |
| 2421 | return ret; |
| 2422 | |
| 2423 | track->codec_priv.data = track->codec_priv.buf->data; |
| 2424 | memmove(track->codec_priv.data + 4, track->codec_priv.data, track->codec_priv.size); |
Mats Peterson | 71f73ee | 2016-01-10 02:59:21 | [diff] [blame] | 2425 | track->codec_priv.size += 4; |
| 2426 | AV_WB32(track->codec_priv.data, track->codec_priv.size); |
| 2427 | } |
| 2428 | |
| 2429 | *fourcc = AV_RL32(track->codec_priv.data + 4); |
| 2430 | *codec_id = ff_codec_get_id(codec_tags, *fourcc); |
| 2431 | |
| 2432 | return 0; |
| 2433 | } |
| 2434 | |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 2435 | static int matroska_parse_tracks(AVFormatContext *s) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2436 | { |
| 2437 | MatroskaDemuxContext *matroska = s->priv_data; |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 2438 | MatroskaTrack *tracks = matroska->tracks.elem; |
Anton Khirnov | 4efdadc | 2014-05-25 12:05:51 | [diff] [blame] | 2439 | int i, j, ret; |
Michael Niedermayer | 69de229 | 2014-05-28 10:41:35 | [diff] [blame] | 2440 | int k; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2441 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2442 | for (i = 0; i < matroska->tracks.nb_elem; i++) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2443 | MatroskaTrack *track = &tracks[i]; |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 2444 | enum AVCodecID codec_id = AV_CODEC_ID_NONE; |
Aurelien Jacobs | dc6c36c | 2011-08-17 22:21:21 | [diff] [blame] | 2445 | EbmlList *encodings_list = &track->encodings; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2446 | MatroskaTrackEncoding *encodings = encodings_list->elem; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2447 | uint8_t *extradata = NULL; |
| 2448 | int extradata_size = 0; |
| 2449 | int extradata_offset = 0; |
Aurelien Jacobs | 5fec3a2 | 2011-06-13 23:58:11 | [diff] [blame] | 2450 | uint32_t fourcc = 0; |
Andreas Rheinhardt | 45bfe8b | 2021-08-04 14:52:07 | [diff] [blame] | 2451 | FFIOContext b; |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 2452 | AVStream *st; |
| 2453 | FFStream *sti; |
Frank Galligan | b853103 | 2013-03-07 16:11:38 | [diff] [blame] | 2454 | char* key_id_base64 = NULL; |
Carl Eugen Hoyos | 96fc290 | 2014-02-25 23:02:51 | [diff] [blame] | 2455 | int bit_depth = -1; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2456 | |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2457 | /* Apply some sanity checks. */ |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2458 | if (track->type != MATROSKA_TRACK_TYPE_VIDEO && |
| 2459 | track->type != MATROSKA_TRACK_TYPE_AUDIO && |
Matthew Heaney | 818ebe9 | 2013-08-08 22:40:03 | [diff] [blame] | 2460 | track->type != MATROSKA_TRACK_TYPE_SUBTITLE && |
| 2461 | track->type != MATROSKA_TRACK_TYPE_METADATA) { |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2462 | av_log(matroska->ctx, AV_LOG_INFO, |
| 2463 | "Unknown or unsupported track type %"PRIu64"\n", |
| 2464 | track->type); |
| 2465 | continue; |
| 2466 | } |
Gabriel Dume | f929ab0 | 2014-08-14 20:31:24 | [diff] [blame] | 2467 | if (!track->codec_id) |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2468 | continue; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2469 | |
Michael Niedermayer | 7b88dd8 | 2020-12-06 23:37:25 | [diff] [blame] | 2470 | if ( track->type == MATROSKA_TRACK_TYPE_AUDIO && track->codec_id[0] != 'A' |
| 2471 | || track->type == MATROSKA_TRACK_TYPE_VIDEO && track->codec_id[0] != 'V' |
| 2472 | || track->type == MATROSKA_TRACK_TYPE_SUBTITLE && track->codec_id[0] != 'D' && track->codec_id[0] != 'S' |
| 2473 | || track->type == MATROSKA_TRACK_TYPE_METADATA && track->codec_id[0] != 'D' && track->codec_id[0] != 'S' |
| 2474 | ) { |
| 2475 | av_log(matroska->ctx, AV_LOG_INFO, "Inconsistent track type\n"); |
| 2476 | continue; |
| 2477 | } |
| 2478 | |
Andreas Cadhalpun | 5b76c82 | 2015-06-15 18:59:22 | [diff] [blame] | 2479 | if (track->audio.samplerate < 0 || track->audio.samplerate > INT_MAX || |
| 2480 | isnan(track->audio.samplerate)) { |
| 2481 | av_log(matroska->ctx, AV_LOG_WARNING, |
| 2482 | "Invalid sample rate %f, defaulting to 8000 instead.\n", |
| 2483 | track->audio.samplerate); |
| 2484 | track->audio.samplerate = 8000; |
| 2485 | } |
| 2486 | |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2487 | if (track->type == MATROSKA_TRACK_TYPE_VIDEO) { |
Nikolas Bowe | e07649e | 2018-01-18 23:21:56 | [diff] [blame] | 2488 | if (!track->default_duration && track->video.frame_rate > 0) { |
| 2489 | double default_duration = 1000000000 / track->video.frame_rate; |
| 2490 | if (default_duration > UINT64_MAX || default_duration < 0) { |
| 2491 | av_log(matroska->ctx, AV_LOG_WARNING, |
| 2492 | "Invalid frame rate %e. Cannot calculate default duration.\n", |
| 2493 | track->video.frame_rate); |
| 2494 | } else { |
| 2495 | track->default_duration = default_duration; |
| 2496 | } |
| 2497 | } |
Michael Niedermayer | f51ce34 | 2013-04-03 21:40:13 | [diff] [blame] | 2498 | if (track->video.display_width == -1) |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2499 | track->video.display_width = track->video.pixel_width; |
Michael Niedermayer | f51ce34 | 2013-04-03 21:40:13 | [diff] [blame] | 2500 | if (track->video.display_height == -1) |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2501 | track->video.display_height = track->video.pixel_height; |
Aurelien Jacobs | fdb5e02 | 2011-06-14 00:00:06 | [diff] [blame] | 2502 | if (track->video.color_space.size == 4) |
| 2503 | fourcc = AV_RL32(track->video.color_space.data); |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2504 | } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { |
| 2505 | if (!track->audio.out_samplerate) |
| 2506 | track->audio.out_samplerate = track->audio.samplerate; |
| 2507 | } |
| 2508 | if (encodings_list->nb_elem > 1) { |
| 2509 | av_log(matroska->ctx, AV_LOG_ERROR, |
Dustin Brody | d7d2f0e | 2011-09-15 07:34:38 | [diff] [blame] | 2510 | "Multiple combined encodings not supported"); |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2511 | } else if (encodings_list->nb_elem == 1) { |
Frank Galligan | b853103 | 2013-03-07 16:11:38 | [diff] [blame] | 2512 | if (encodings[0].type) { |
| 2513 | if (encodings[0].encryption.key_id.size > 0) { |
| 2514 | /* Save the encryption key id to be stored later as a |
| 2515 | metadata tag. */ |
| 2516 | const int b64_size = AV_BASE64_SIZE(encodings[0].encryption.key_id.size); |
| 2517 | key_id_base64 = av_malloc(b64_size); |
| 2518 | if (key_id_base64 == NULL) |
| 2519 | return AVERROR(ENOMEM); |
| 2520 | |
| 2521 | av_base64_encode(key_id_base64, b64_size, |
| 2522 | encodings[0].encryption.key_id.data, |
| 2523 | encodings[0].encryption.key_id.size); |
| 2524 | } else { |
| 2525 | encodings[0].scope = 0; |
| 2526 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 2527 | "Unsupported encoding type"); |
| 2528 | } |
| 2529 | } else if ( |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame] | 2530 | #if CONFIG_ZLIB |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2531 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB && |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2532 | #endif |
Aurelien Jacobs | b250f9c | 2009-01-13 23:44:16 | [diff] [blame] | 2533 | #if CONFIG_BZLIB |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2534 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_BZLIB && |
| 2535 | #endif |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2536 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO && |
Frank Galligan | b853103 | 2013-03-07 16:11:38 | [diff] [blame] | 2537 | encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP) { |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2538 | encodings[0].scope = 0; |
| 2539 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 2540 | "Unsupported encoding type"); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2541 | } else if (track->codec_priv.size && encodings[0].scope & 2) { |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2542 | uint8_t *codec_priv = track->codec_priv.data; |
Luca Barbato | 8d4dd55 | 2012-09-14 16:51:49 | [diff] [blame] | 2543 | int ret = matroska_decode_buffer(&track->codec_priv.data, |
| 2544 | &track->codec_priv.size, |
| 2545 | track); |
| 2546 | if (ret < 0) { |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2547 | track->codec_priv.data = NULL; |
| 2548 | track->codec_priv.size = 0; |
| 2549 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 2550 | "Failed to decode codec private data\n"); |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2551 | } |
Luca Barbato | 8d4dd55 | 2012-09-14 16:51:49 | [diff] [blame] | 2552 | |
James Almer | a618866 | 2018-04-04 17:12:54 | [diff] [blame] | 2553 | if (codec_priv != track->codec_priv.data) { |
| 2554 | av_buffer_unref(&track->codec_priv.buf); |
| 2555 | if (track->codec_priv.data) { |
| 2556 | track->codec_priv.buf = av_buffer_create(track->codec_priv.data, |
| 2557 | track->codec_priv.size + AV_INPUT_BUFFER_PADDING_SIZE, |
| 2558 | NULL, NULL, 0); |
| 2559 | if (!track->codec_priv.buf) { |
| 2560 | av_freep(&track->codec_priv.data); |
| 2561 | track->codec_priv.size = 0; |
| 2562 | return AVERROR(ENOMEM); |
| 2563 | } |
| 2564 | } |
| 2565 | } |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2566 | } |
| 2567 | } |
Andreas Rheinhardt | 96012d1 | 2019-12-04 16:54:45 | [diff] [blame] | 2568 | track->needs_decoding = encodings && !encodings[0].type && |
| 2569 | encodings[0].scope & 1 && |
| 2570 | (encodings[0].compression.algo != |
| 2571 | MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP || |
| 2572 | encodings[0].compression.settings.size); |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 2573 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2574 | for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) { |
Andreas Rheinhardt | 0f906be | 2021-02-24 06:44:31 | [diff] [blame] | 2575 | if (av_strstart(track->codec_id, ff_mkv_codec_tags[j].str, NULL)) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2576 | codec_id = ff_mkv_codec_tags[j].id; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2577 | break; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2578 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2579 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2580 | |
Anton Khirnov | 3b3bbdd | 2011-06-18 09:43:24 | [diff] [blame] | 2581 | st = track->stream = avformat_new_stream(s, NULL); |
Michael Niedermayer | fb33bff | 2014-08-15 18:33:21 | [diff] [blame] | 2582 | if (!st) { |
Frank Galligan | b853103 | 2013-03-07 16:11:38 | [diff] [blame] | 2583 | av_free(key_id_base64); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2584 | return AVERROR(ENOMEM); |
Frank Galligan | b853103 | 2013-03-07 16:11:38 | [diff] [blame] | 2585 | } |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 2586 | sti = ffstream(st); |
Frank Galligan | b853103 | 2013-03-07 16:11:38 | [diff] [blame] | 2587 | |
| 2588 | if (key_id_base64) { |
| 2589 | /* export encryption key id as base64 metadata tag */ |
Andreas Rheinhardt | 40d9cbd | 2019-11-10 04:07:31 | [diff] [blame] | 2590 | av_dict_set(&st->metadata, "enc_key_id", key_id_base64, |
| 2591 | AV_DICT_DONT_STRDUP_VAL); |
Frank Galligan | b853103 | 2013-03-07 16:11:38 | [diff] [blame] | 2592 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2593 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2594 | if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC") && |
Michael Niedermayer | b3d9ab1 | 2014-03-07 22:46:37 | [diff] [blame] | 2595 | track->codec_priv.size >= 40 && |
Gabriel Dume | 4b1f5e5 | 2014-08-14 20:31:25 | [diff] [blame] | 2596 | track->codec_priv.data) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2597 | track->ms_compat = 1; |
Michael Niedermayer | b3d9ab1 | 2014-03-07 22:46:37 | [diff] [blame] | 2598 | bit_depth = AV_RL16(track->codec_priv.data + 14); |
| 2599 | fourcc = AV_RL32(track->codec_priv.data + 16); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2600 | codec_id = ff_codec_get_id(ff_codec_bmp_tags, |
Michael Niedermayer | b3d9ab1 | 2014-03-07 22:46:37 | [diff] [blame] | 2601 | fourcc); |
Carl Eugen Hoyos | ade5851 | 2014-04-27 07:34:13 | [diff] [blame] | 2602 | if (!codec_id) |
| 2603 | codec_id = ff_codec_get_id(ff_codec_movvideo_tags, |
| 2604 | fourcc); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2605 | extradata_offset = 40; |
| 2606 | } else if (!strcmp(track->codec_id, "A_MS/ACM") && |
| 2607 | track->codec_priv.size >= 14 && |
Gabriel Dume | 4b1f5e5 | 2014-08-14 20:31:25 | [diff] [blame] | 2608 | track->codec_priv.data) { |
Max Horn | ca402f3 | 2011-04-12 15:44:20 | [diff] [blame] | 2609 | int ret; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2610 | ffio_init_context(&b, track->codec_priv.data, |
| 2611 | track->codec_priv.size, |
Michael Niedermayer | d0b4504 | 2013-01-04 12:00:14 | [diff] [blame] | 2612 | 0, NULL, NULL, NULL, NULL); |
Andreas Rheinhardt | 45bfe8b | 2021-08-04 14:52:07 | [diff] [blame] | 2613 | ret = ff_get_wav_header(s, &b.pub, st->codecpar, |
| 2614 | track->codec_priv.size, 0); |
Max Horn | ca402f3 | 2011-04-12 15:44:20 | [diff] [blame] | 2615 | if (ret < 0) |
| 2616 | return ret; |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 2617 | codec_id = st->codecpar->codec_id; |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 2618 | fourcc = st->codecpar->codec_tag; |
Aurelien Jacobs | 038146e | 2009-10-01 21:14:05 | [diff] [blame] | 2619 | extradata_offset = FFMIN(track->codec_priv.size, 18); |
Michael Niedermayer | 4821858 | 2014-01-19 17:35:33 | [diff] [blame] | 2620 | } else if (!strcmp(track->codec_id, "A_QUICKTIME") |
Mats Peterson | 71f73ee | 2016-01-10 02:59:21 | [diff] [blame] | 2621 | /* Normally 36, but allow noncompliant private data */ |
| 2622 | && (track->codec_priv.size >= 32) |
Michael Niedermayer | 81a663f | 2014-08-15 19:31:59 | [diff] [blame] | 2623 | && (track->codec_priv.data)) { |
Mats Peterson | 535d09a | 2016-01-15 22:25:38 | [diff] [blame] | 2624 | uint16_t sample_size; |
Mats Peterson | 71f73ee | 2016-01-10 02:59:21 | [diff] [blame] | 2625 | int ret = get_qt_codec(track, &fourcc, &codec_id); |
| 2626 | if (ret < 0) |
| 2627 | return ret; |
Mats Peterson | 535d09a | 2016-01-15 22:25:38 | [diff] [blame] | 2628 | sample_size = AV_RB16(track->codec_priv.data + 26); |
Mats Peterson | 6f1466d | 2016-01-08 11:55:59 | [diff] [blame] | 2629 | if (fourcc == 0) { |
Mats Peterson | 535d09a | 2016-01-15 22:25:38 | [diff] [blame] | 2630 | if (sample_size == 8) { |
Mats Peterson | 6f1466d | 2016-01-08 11:55:59 | [diff] [blame] | 2631 | fourcc = MKTAG('r','a','w',' '); |
| 2632 | codec_id = ff_codec_get_id(ff_codec_movaudio_tags, fourcc); |
Mats Peterson | 535d09a | 2016-01-15 22:25:38 | [diff] [blame] | 2633 | } else if (sample_size == 16) { |
Mats Peterson | 6f1466d | 2016-01-08 11:55:59 | [diff] [blame] | 2634 | fourcc = MKTAG('t','w','o','s'); |
| 2635 | codec_id = ff_codec_get_id(ff_codec_movaudio_tags, fourcc); |
| 2636 | } |
| 2637 | } |
Mats Peterson | 535d09a | 2016-01-15 22:25:38 | [diff] [blame] | 2638 | if ((fourcc == MKTAG('t','w','o','s') || |
| 2639 | fourcc == MKTAG('s','o','w','t')) && |
| 2640 | sample_size == 8) |
| 2641 | codec_id = AV_CODEC_ID_PCM_S8; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2642 | } else if (!strcmp(track->codec_id, "V_QUICKTIME") && |
Michael Niedermayer | b3d9ab1 | 2014-03-07 22:46:37 | [diff] [blame] | 2643 | (track->codec_priv.size >= 21) && |
Gabriel Dume | 4b1f5e5 | 2014-08-14 20:31:25 | [diff] [blame] | 2644 | (track->codec_priv.data)) { |
Mats Peterson | 71f73ee | 2016-01-10 02:59:21 | [diff] [blame] | 2645 | int ret = get_qt_codec(track, &fourcc, &codec_id); |
| 2646 | if (ret < 0) |
| 2647 | return ret; |
Mats Peterson | 6a975cb | 2016-01-06 03:16:32 | [diff] [blame] | 2648 | if (codec_id == AV_CODEC_ID_NONE && AV_RL32(track->codec_priv.data+4) == AV_RL32("SMI ")) { |
| 2649 | fourcc = MKTAG('S','V','Q','3'); |
| 2650 | codec_id = ff_codec_get_id(ff_codec_movvideo_tags, fourcc); |
| 2651 | } |
Clément Bœsch | cd4d6cb | 2017-03-26 23:31:52 | [diff] [blame] | 2652 | if (codec_id == AV_CODEC_ID_NONE) |
Vittorio Giovara | 8fc11ab | 2015-05-11 16:23:43 | [diff] [blame] | 2653 | av_log(matroska->ctx, AV_LOG_ERROR, |
Clément Bœsch | cd4d6cb | 2017-03-26 23:31:52 | [diff] [blame] | 2654 | "mov FourCC not found %s.\n", av_fourcc2str(fourcc)); |
Mats Peterson | 7973603 | 2015-12-27 20:28:09 | [diff] [blame] | 2655 | if (track->codec_priv.size >= 86) { |
| 2656 | bit_depth = AV_RB16(track->codec_priv.data + 82); |
| 2657 | ffio_init_context(&b, track->codec_priv.data, |
| 2658 | track->codec_priv.size, |
| 2659 | 0, NULL, NULL, NULL, NULL); |
Andreas Rheinhardt | 45bfe8b | 2021-08-04 14:52:07 | [diff] [blame] | 2660 | if (ff_get_qtpalette(codec_id, &b.pub, track->palette)) { |
Mats Peterson | 7973603 | 2015-12-27 20:28:09 | [diff] [blame] | 2661 | bit_depth &= 0x1F; |
Mats Peterson | 6aac43f | 2016-02-24 17:14:05 | [diff] [blame] | 2662 | track->has_palette = 1; |
Mats Peterson | 7973603 | 2015-12-27 20:28:09 | [diff] [blame] | 2663 | } |
| 2664 | } |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 2665 | } else if (codec_id == AV_CODEC_ID_PCM_S16BE) { |
Aurelien Jacobs | eb9cf50 | 2008-08-20 00:49:45 | [diff] [blame] | 2666 | switch (track->audio.bitdepth) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2667 | case 8: |
| 2668 | codec_id = AV_CODEC_ID_PCM_U8; |
| 2669 | break; |
| 2670 | case 24: |
| 2671 | codec_id = AV_CODEC_ID_PCM_S24BE; |
| 2672 | break; |
| 2673 | case 32: |
| 2674 | codec_id = AV_CODEC_ID_PCM_S32BE; |
| 2675 | break; |
Aurelien Jacobs | eb9cf50 | 2008-08-20 00:49:45 | [diff] [blame] | 2676 | } |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 2677 | } else if (codec_id == AV_CODEC_ID_PCM_S16LE) { |
Aurelien Jacobs | eb9cf50 | 2008-08-20 00:49:45 | [diff] [blame] | 2678 | switch (track->audio.bitdepth) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2679 | case 8: |
| 2680 | codec_id = AV_CODEC_ID_PCM_U8; |
| 2681 | break; |
| 2682 | case 24: |
| 2683 | codec_id = AV_CODEC_ID_PCM_S24LE; |
| 2684 | break; |
| 2685 | case 32: |
| 2686 | codec_id = AV_CODEC_ID_PCM_S32LE; |
| 2687 | break; |
Aurelien Jacobs | eb9cf50 | 2008-08-20 00:49:45 | [diff] [blame] | 2688 | } |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2689 | } else if (codec_id == AV_CODEC_ID_PCM_F32LE && |
| 2690 | track->audio.bitdepth == 64) { |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 2691 | codec_id = AV_CODEC_ID_PCM_F64LE; |
| 2692 | } else if (codec_id == AV_CODEC_ID_AAC && !track->codec_priv.size) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2693 | int profile = matroska_aac_profile(track->codec_id); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2694 | int sri = matroska_aac_sri(track->audio.samplerate); |
Vittorio Giovara | 059a934 | 2015-06-29 21:48:34 | [diff] [blame] | 2695 | extradata = av_mallocz(5 + AV_INPUT_BUFFER_PADDING_SIZE); |
Gabriel Dume | f929ab0 | 2014-08-14 20:31:24 | [diff] [blame] | 2696 | if (!extradata) |
Aurelien Jacobs | 28f450a | 2008-08-05 00:40:09 | [diff] [blame] | 2697 | return AVERROR(ENOMEM); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2698 | extradata[0] = (profile << 3) | ((sri & 0x0E) >> 1); |
| 2699 | extradata[1] = ((sri & 0x01) << 7) | (track->audio.channels << 3); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2700 | if (strstr(track->codec_id, "SBR")) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2701 | sri = matroska_aac_sri(track->audio.out_samplerate); |
| 2702 | extradata[2] = 0x56; |
| 2703 | extradata[3] = 0xE5; |
| 2704 | extradata[4] = 0x80 | (sri << 3); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2705 | extradata_size = 5; |
Aurelien Jacobs | 16f97ab | 2008-08-05 00:41:10 | [diff] [blame] | 2706 | } else |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2707 | extradata_size = 2; |
Michael Niedermayer | 29d147c | 2015-07-27 20:53:16 | [diff] [blame] | 2708 | } else if (codec_id == AV_CODEC_ID_ALAC && track->codec_priv.size && track->codec_priv.size < INT_MAX - 12 - AV_INPUT_BUFFER_PADDING_SIZE) { |
Paul B Mahol | 9644fc9 | 2013-06-07 10:06:15 | [diff] [blame] | 2709 | /* Only ALAC's magic cookie is stored in Matroska's track headers. |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2710 | * Create the "atom size", "tag", and "tag version" fields the |
| 2711 | * decoder expects manually. */ |
Paul B Mahol | 9644fc9 | 2013-06-07 10:06:15 | [diff] [blame] | 2712 | extradata_size = 12 + track->codec_priv.size; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2713 | extradata = av_mallocz(extradata_size + |
Vittorio Giovara | 059a934 | 2015-06-29 21:48:34 | [diff] [blame] | 2714 | AV_INPUT_BUFFER_PADDING_SIZE); |
Gabriel Dume | f929ab0 | 2014-08-14 20:31:24 | [diff] [blame] | 2715 | if (!extradata) |
Paul B Mahol | 9644fc9 | 2013-06-07 10:06:15 | [diff] [blame] | 2716 | return AVERROR(ENOMEM); |
| 2717 | AV_WB32(extradata, extradata_size); |
| 2718 | memcpy(&extradata[4], "alac", 4); |
| 2719 | AV_WB32(&extradata[8], 0); |
| 2720 | memcpy(&extradata[12], track->codec_priv.data, |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2721 | track->codec_priv.size); |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 2722 | } else if (codec_id == AV_CODEC_ID_TTA) { |
Andreas Rheinhardt | 668490a | 2019-12-03 17:09:05 | [diff] [blame] | 2723 | uint8_t *ptr; |
Andreas Rheinhardt | f7bf59b | 2019-12-03 17:09:03 | [diff] [blame] | 2724 | if (track->audio.channels > UINT16_MAX || |
| 2725 | track->audio.bitdepth > UINT16_MAX) { |
| 2726 | av_log(matroska->ctx, AV_LOG_WARNING, |
| 2727 | "Too large audio channel number %"PRIu64 |
| 2728 | " or bitdepth %"PRIu64". Skipping track.\n", |
| 2729 | track->audio.channels, track->audio.bitdepth); |
| 2730 | if (matroska->ctx->error_recognition & AV_EF_EXPLODE) |
| 2731 | return AVERROR_INVALIDDATA; |
| 2732 | else |
| 2733 | continue; |
| 2734 | } |
| 2735 | if (track->audio.out_samplerate < 0 || track->audio.out_samplerate > INT_MAX) |
| 2736 | return AVERROR_INVALIDDATA; |
Andreas Rheinhardt | eec26b5 | 2019-12-03 17:09:04 | [diff] [blame] | 2737 | extradata_size = 22; |
Michael Niedermayer | 29d147c | 2015-07-27 20:53:16 | [diff] [blame] | 2738 | extradata = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); |
Gabriel Dume | f929ab0 | 2014-08-14 20:31:24 | [diff] [blame] | 2739 | if (!extradata) |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2740 | return AVERROR(ENOMEM); |
Andreas Rheinhardt | 668490a | 2019-12-03 17:09:05 | [diff] [blame] | 2741 | ptr = extradata; |
| 2742 | bytestream_put_be32(&ptr, AV_RB32("TTA1")); |
| 2743 | bytestream_put_le16(&ptr, 1); |
| 2744 | bytestream_put_le16(&ptr, track->audio.channels); |
| 2745 | bytestream_put_le16(&ptr, track->audio.bitdepth); |
| 2746 | bytestream_put_le32(&ptr, track->audio.out_samplerate); |
| 2747 | bytestream_put_le32(&ptr, av_rescale(matroska->duration * matroska->time_scale, |
| 2748 | track->audio.out_samplerate, |
| 2749 | AV_TIME_BASE * 1000)); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2750 | } else if (codec_id == AV_CODEC_ID_RV10 || |
| 2751 | codec_id == AV_CODEC_ID_RV20 || |
| 2752 | codec_id == AV_CODEC_ID_RV30 || |
| 2753 | codec_id == AV_CODEC_ID_RV40) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2754 | extradata_offset = 26; |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 2755 | } else if (codec_id == AV_CODEC_ID_RA_144) { |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2756 | track->audio.out_samplerate = 8000; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2757 | track->audio.channels = 1; |
Michael Niedermayer | b3d9ab1 | 2014-03-07 22:46:37 | [diff] [blame] | 2758 | } else if ((codec_id == AV_CODEC_ID_RA_288 || |
| 2759 | codec_id == AV_CODEC_ID_COOK || |
| 2760 | codec_id == AV_CODEC_ID_ATRAC3 || |
| 2761 | codec_id == AV_CODEC_ID_SIPR) |
| 2762 | && track->codec_priv.data) { |
Andreas Rheinhardt | e75ab15 | 2021-08-04 20:20:46 | [diff] [blame] | 2763 | const uint8_t *ptr = track->codec_priv.data; |
Aurelien Jacobs | 6b10228 | 2010-03-12 23:49:06 | [diff] [blame] | 2764 | int flavor; |
Michael Niedermayer | 0b27136 | 2012-04-14 17:33:24 | [diff] [blame] | 2765 | |
Andreas Rheinhardt | e75ab15 | 2021-08-04 20:20:46 | [diff] [blame] | 2766 | if (track->codec_priv.size < 46) |
| 2767 | return AVERROR_INVALIDDATA; |
| 2768 | ptr += 22; |
| 2769 | flavor = bytestream_get_be16(&ptr); |
| 2770 | track->audio.coded_framesize = bytestream_get_be32(&ptr); |
| 2771 | ptr += 12; |
| 2772 | track->audio.sub_packet_h = bytestream_get_be16(&ptr); |
| 2773 | track->audio.frame_size = bytestream_get_be16(&ptr); |
| 2774 | track->audio.sub_packet_size = bytestream_get_be16(&ptr); |
Andreas Rheinhardt | 8287c20 | 2019-12-02 09:41:12 | [diff] [blame] | 2775 | if (track->audio.coded_framesize <= 0 || |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2776 | track->audio.sub_packet_h <= 0 || |
Andreas Rheinhardt | c91e369 | 2020-04-20 05:47:06 | [diff] [blame] | 2777 | track->audio.frame_size <= 0) |
Martin Storsjö | 569d18a | 2013-09-16 12:36:24 | [diff] [blame] | 2778 | return AVERROR_INVALIDDATA; |
Andreas Rheinhardt | 8287c20 | 2019-12-02 09:41:12 | [diff] [blame] | 2779 | |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 2780 | if (codec_id == AV_CODEC_ID_RA_288) { |
Andreas Rheinhardt | bdaa98d | 2020-04-22 13:17:41 | [diff] [blame] | 2781 | if (track->audio.sub_packet_h & 1 || 2 * track->audio.frame_size |
| 2782 | != (int64_t)track->audio.sub_packet_h * track->audio.coded_framesize) |
Andreas Rheinhardt | 4f5c6c1 | 2020-04-21 22:15:54 | [diff] [blame] | 2783 | return AVERROR_INVALIDDATA; |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 2784 | st->codecpar->block_align = track->audio.coded_framesize; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2785 | track->codec_priv.size = 0; |
| 2786 | } else { |
Andreas Rheinhardt | 8287c20 | 2019-12-02 09:41:12 | [diff] [blame] | 2787 | if (codec_id == AV_CODEC_ID_SIPR) { |
Michael Niedermayer | 62cf5c1 | 2013-08-04 19:18:49 | [diff] [blame] | 2788 | static const int sipr_bit_rate[4] = { 6504, 8496, 5000, 16000 }; |
Andreas Rheinhardt | 8287c20 | 2019-12-02 09:41:12 | [diff] [blame] | 2789 | if (flavor > 3) |
| 2790 | return AVERROR_INVALIDDATA; |
Aurelien Jacobs | 6b10228 | 2010-03-12 23:49:06 | [diff] [blame] | 2791 | track->audio.sub_packet_size = ff_sipr_subpk_size[flavor]; |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 2792 | st->codecpar->bit_rate = sipr_bit_rate[flavor]; |
Andreas Rheinhardt | 4b1c19a | 2020-04-20 06:54:23 | [diff] [blame] | 2793 | } else if (track->audio.sub_packet_size <= 0 || |
| 2794 | track->audio.frame_size % track->audio.sub_packet_size) |
Andreas Rheinhardt | c91e369 | 2020-04-20 05:47:06 | [diff] [blame] | 2795 | return AVERROR_INVALIDDATA; |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 2796 | st->codecpar->block_align = track->audio.sub_packet_size; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2797 | extradata_offset = 78; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2798 | } |
Andreas Rheinhardt | 8287c20 | 2019-12-02 09:41:12 | [diff] [blame] | 2799 | track->audio.buf = av_malloc_array(track->audio.sub_packet_h, |
| 2800 | track->audio.frame_size); |
| 2801 | if (!track->audio.buf) |
| 2802 | return AVERROR(ENOMEM); |
Anton Khirnov | 4efdadc | 2014-05-25 12:05:51 | [diff] [blame] | 2803 | } else if (codec_id == AV_CODEC_ID_FLAC && track->codec_priv.size) { |
| 2804 | ret = matroska_parse_flac(s, track, &extradata_offset); |
| 2805 | if (ret < 0) |
| 2806 | return ret; |
Andreas Rheinhardt | 048bc3f | 2019-12-14 00:36:54 | [diff] [blame] | 2807 | } else if (codec_id == AV_CODEC_ID_WAVPACK && track->codec_priv.size < 2) { |
| 2808 | av_log(matroska->ctx, AV_LOG_INFO, "Assuming WavPack version 4.10 " |
| 2809 | "in absence of valid CodecPrivate.\n"); |
| 2810 | extradata_size = 2; |
| 2811 | extradata = av_mallocz(2 + AV_INPUT_BUFFER_PADDING_SIZE); |
| 2812 | if (!extradata) |
| 2813 | return AVERROR(ENOMEM); |
| 2814 | AV_WL16(extradata, 0x410); |
Carl Eugen Hoyos | 3d5c859 | 2014-04-06 18:54:48 | [diff] [blame] | 2815 | } else if (codec_id == AV_CODEC_ID_PRORES && track->codec_priv.size == 4) { |
| 2816 | fourcc = AV_RL32(track->codec_priv.data); |
James Almer | acdea9e | 2018-01-13 19:04:21 | [diff] [blame] | 2817 | } else if (codec_id == AV_CODEC_ID_VP9 && track->codec_priv.size) { |
| 2818 | /* we don't need any value stored in CodecPrivate. |
| 2819 | make sure that it's not exported as extradata. */ |
| 2820 | track->codec_priv.size = 0; |
rcombs | f758d4f | 2023-01-13 17:25:49 | [diff] [blame] | 2821 | } else if (codec_id == AV_CODEC_ID_ARIB_CAPTION && track->codec_priv.size == 3) { |
| 2822 | int component_tag = track->codec_priv.data[0]; |
| 2823 | int data_component_id = AV_RB16(track->codec_priv.data + 1); |
| 2824 | |
| 2825 | switch (data_component_id) { |
| 2826 | case 0x0008: |
| 2827 | // [0x30..0x37] are component tags utilized for |
| 2828 | // non-mobile captioning service ("profile A"). |
| 2829 | if (component_tag >= 0x30 && component_tag <= 0x37) { |
| 2830 | st->codecpar->profile = FF_PROFILE_ARIB_PROFILE_A; |
| 2831 | } |
| 2832 | break; |
| 2833 | case 0x0012: |
| 2834 | // component tag 0x87 signifies a mobile/partial reception |
| 2835 | // (1seg) captioning service ("profile C"). |
| 2836 | if (component_tag == 0x87) { |
| 2837 | st->codecpar->profile = FF_PROFILE_ARIB_PROFILE_C; |
| 2838 | } |
| 2839 | break; |
| 2840 | default: |
| 2841 | break; |
| 2842 | } |
| 2843 | |
| 2844 | if (st->codecpar->profile == FF_PROFILE_UNKNOWN) |
| 2845 | av_log(matroska->ctx, AV_LOG_WARNING, |
| 2846 | "Unknown ARIB caption profile utilized: %02x / %04x\n", |
| 2847 | component_tag, data_component_id); |
| 2848 | |
| 2849 | track->codec_priv.size = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 2850 | } |
Aurelien Jacobs | e264440 | 2009-08-24 13:40:30 | [diff] [blame] | 2851 | track->codec_priv.size -= extradata_offset; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2852 | |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 2853 | if (codec_id == AV_CODEC_ID_NONE) |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2854 | av_log(matroska->ctx, AV_LOG_INFO, |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 2855 | "Unknown/unsupported AVCodecID %s.\n", track->codec_id); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2856 | |
Steve Lhomme | b00d221 | 2020-11-15 08:59:47 | [diff] [blame] | 2857 | if (track->time_scale < 0.01) { |
| 2858 | av_log(matroska->ctx, AV_LOG_WARNING, |
| 2859 | "Track TimestampScale too small %f, assuming 1.0.\n", |
| 2860 | track->time_scale); |
Aurelien Jacobs | 3fc9d7c | 2008-09-09 11:23:48 | [diff] [blame] | 2861 | track->time_scale = 1.0; |
Steve Lhomme | b00d221 | 2020-11-15 08:59:47 | [diff] [blame] | 2862 | } |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2863 | avpriv_set_pts_info(st, 64, matroska->time_scale * track->time_scale, |
| 2864 | 1000 * 1000 * 1000); /* 64 bit pts in ns */ |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2865 | |
Anton Khirnov | eb3b550 | 2014-04-29 10:03:13 | [diff] [blame] | 2866 | /* convert the delay from ns to the track timebase */ |
Michael Niedermayer | b5bc436 | 2016-06-06 02:23:16 | [diff] [blame] | 2867 | track->codec_delay_in_track_tb = av_rescale_q(track->codec_delay, |
Anton Khirnov | eb3b550 | 2014-04-29 10:03:13 | [diff] [blame] | 2868 | (AVRational){ 1, 1000000000 }, |
| 2869 | st->time_base); |
| 2870 | |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 2871 | st->codecpar->codec_id = codec_id; |
Alex Sukhanov | 251c96a | 2013-12-23 09:41:35 | [diff] [blame] | 2872 | |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2873 | if (strcmp(track->language, "und")) |
Anton Khirnov | d2d67e4 | 2011-05-22 10:46:29 | [diff] [blame] | 2874 | av_dict_set(&st->metadata, "language", track->language, 0); |
| 2875 | av_dict_set(&st->metadata, "title", track->name, 0); |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2876 | |
| 2877 | if (track->flag_default) |
| 2878 | st->disposition |= AV_DISPOSITION_DEFAULT; |
Aurelien Jacobs | 7a617a8 | 2010-07-02 16:38:44 | [diff] [blame] | 2879 | if (track->flag_forced) |
| 2880 | st->disposition |= AV_DISPOSITION_FORCED; |
Andreas Rheinhardt | 18b3dee | 2021-02-16 17:23:59 | [diff] [blame] | 2881 | if (track->flag_comment) |
| 2882 | st->disposition |= AV_DISPOSITION_COMMENT; |
Andreas Rheinhardt | 51e1067 | 2021-02-16 18:08:51 | [diff] [blame] | 2883 | if (track->flag_hearingimpaired) |
| 2884 | st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; |
| 2885 | if (track->flag_visualimpaired) |
| 2886 | st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; |
Andreas Rheinhardt | b243be8 | 2021-02-16 17:41:30 | [diff] [blame] | 2887 | if (track->flag_original.count > 0) |
| 2888 | st->disposition |= track->flag_original.el.u ? AV_DISPOSITION_ORIGINAL |
| 2889 | : AV_DISPOSITION_DUB; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2890 | |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 2891 | if (!st->codecpar->extradata) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2892 | if (extradata) { |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 2893 | st->codecpar->extradata = extradata; |
| 2894 | st->codecpar->extradata_size = extradata_size; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 2895 | } else if (track->codec_priv.data && track->codec_priv.size > 0) { |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 2896 | if (ff_alloc_extradata(st->codecpar, track->codec_priv.size)) |
Aurelien Jacobs | 553e9f7 | 2009-10-01 21:15:36 | [diff] [blame] | 2897 | return AVERROR(ENOMEM); |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 2898 | memcpy(st->codecpar->extradata, |
Aurelien Jacobs | 553e9f7 | 2009-10-01 21:15:36 | [diff] [blame] | 2899 | track->codec_priv.data + extradata_offset, |
| 2900 | track->codec_priv.size); |
| 2901 | } |
Aurelien Jacobs | ff0d5a7 | 2009-10-01 21:14:46 | [diff] [blame] | 2902 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2903 | |
| 2904 | if (track->type == MATROSKA_TRACK_TYPE_VIDEO) { |
Aurelien Jacobs | 4c509fe | 2011-05-23 23:09:24 | [diff] [blame] | 2905 | MatroskaTrackPlane *planes = track->operation.combine_planes.elem; |
Aaron Colwell | febfb49 | 2015-12-02 23:13:18 | [diff] [blame] | 2906 | int display_width_mul = 1; |
Aaron Colwell | ec83efd | 2015-11-23 20:06:14 | [diff] [blame] | 2907 | int display_height_mul = 1; |
Aurelien Jacobs | 4c509fe | 2011-05-23 23:09:24 | [diff] [blame] | 2908 | |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 2909 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 2910 | st->codecpar->codec_tag = fourcc; |
Carl Eugen Hoyos | 96fc290 | 2014-02-25 23:02:51 | [diff] [blame] | 2911 | if (bit_depth >= 0) |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 2912 | st->codecpar->bits_per_coded_sample = bit_depth; |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 2913 | st->codecpar->width = track->video.pixel_width; |
| 2914 | st->codecpar->height = track->video.pixel_height; |
Aaron Colwell | ec83efd | 2015-11-23 20:06:14 | [diff] [blame] | 2915 | |
Luca Barbato | 5f02266 | 2016-03-28 18:29:54 | [diff] [blame] | 2916 | if (track->video.interlaced == MATROSKA_VIDEO_INTERLACE_FLAG_INTERLACED) |
James Almer | 1273bc6 | 2016-10-13 17:22:07 | [diff] [blame] | 2917 | st->codecpar->field_order = mkv_field_order(matroska, track->video.field_order); |
James Almer | 29b6c2b | 2016-09-27 17:48:33 | [diff] [blame] | 2918 | else if (track->video.interlaced == MATROSKA_VIDEO_INTERLACE_FLAG_PROGRESSIVE) |
| 2919 | st->codecpar->field_order = AV_FIELD_PROGRESSIVE; |
Luca Barbato | 5f02266 | 2016-03-28 18:29:54 | [diff] [blame] | 2920 | |
Aaron Colwell | ec83efd | 2015-11-23 20:06:14 | [diff] [blame] | 2921 | if (track->video.stereo_mode && track->video.stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB) |
| 2922 | mkv_stereo_mode_display_mul(track->video.stereo_mode, &display_width_mul, &display_height_mul); |
| 2923 | |
James Almer | 8b59ce0 | 2016-10-16 13:13:45 | [diff] [blame] | 2924 | if (track->video.display_unit < MATROSKA_VIDEO_DISPLAYUNIT_UNKNOWN) { |
Michael Niedermayer | e6cad01 | 2022-04-01 10:46:08 | [diff] [blame] | 2925 | if (track->video.display_width && track->video.display_height && |
| 2926 | st->codecpar->height < INT64_MAX / track->video.display_width / display_width_mul && |
| 2927 | st->codecpar->width < INT64_MAX / track->video.display_height / display_height_mul) |
| 2928 | av_reduce(&st->sample_aspect_ratio.num, |
| 2929 | &st->sample_aspect_ratio.den, |
| 2930 | st->codecpar->height * track->video.display_width * display_width_mul, |
| 2931 | st->codecpar->width * track->video.display_height * display_height_mul, |
| 2932 | INT_MAX); |
James Almer | bad8bbc | 2016-10-15 21:01:50 | [diff] [blame] | 2933 | } |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 2934 | if (st->codecpar->codec_id != AV_CODEC_ID_HEVC) |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 2935 | sti->need_parsing = AVSTREAM_PARSE_HEADERS; |
Michael Niedermayer | 0fbeeb9 | 2013-11-01 16:57:57 | [diff] [blame] | 2936 | |
Luca Barbato | ac97d47 | 2012-04-17 23:32:07 | [diff] [blame] | 2937 | if (track->default_duration) { |
Michael Niedermayer | 343d950 | 2021-05-11 16:40:32 | [diff] [blame] | 2938 | int div = track->default_duration <= INT64_MAX ? 1 : 2; |
Anton Khirnov | aba232c | 2012-06-26 11:10:01 | [diff] [blame] | 2939 | av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den, |
Michael Niedermayer | 343d950 | 2021-05-11 16:40:32 | [diff] [blame] | 2940 | 1000000000 / div, track->default_duration / div, 30000); |
Anton Khirnov | aba232c | 2012-06-26 11:10:01 | [diff] [blame] | 2941 | #if FF_API_R_FRAME_RATE |
Michael Niedermayer | fc3cdb0 | 2015-02-01 18:34:52 | [diff] [blame] | 2942 | if ( st->avg_frame_rate.num < st->avg_frame_rate.den * 1000LL |
| 2943 | && st->avg_frame_rate.num > st->avg_frame_rate.den * 5LL) |
Michael Niedermayer | 6853e40 | 2013-10-05 22:07:28 | [diff] [blame] | 2944 | st->r_frame_rate = st->avg_frame_rate; |
Anton Khirnov | aba232c | 2012-06-26 11:10:01 | [diff] [blame] | 2945 | #endif |
Luca Barbato | ac97d47 | 2012-04-17 23:32:07 | [diff] [blame] | 2946 | } |
Kirill Gavrilov | e6ec921 | 2011-05-21 15:14:14 | [diff] [blame] | 2947 | |
Aurelien Jacobs | 4c509fe | 2011-05-23 23:09:24 | [diff] [blame] | 2948 | /* export stereo mode flag as metadata tag */ |
Michael Niedermayer | 37520a9 | 2014-08-28 23:26:52 | [diff] [blame] | 2949 | if (track->video.stereo_mode && track->video.stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB) |
Clément Bœsch | ca81e3b | 2012-09-16 00:58:40 | [diff] [blame] | 2950 | 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] | 2951 | |
Vignesh Venkatasubramanian | ce6a8e5 | 2013-02-04 23:17:52 | [diff] [blame] | 2952 | /* export alpha mode flag as metadata tag */ |
| 2953 | if (track->video.alpha_mode) |
| 2954 | av_dict_set(&st->metadata, "alpha_mode", "1", 0); |
| 2955 | |
Aurelien Jacobs | 4c509fe | 2011-05-23 23:09:24 | [diff] [blame] | 2956 | /* if we have virtual track, mark the real tracks */ |
| 2957 | for (j=0; j < track->operation.combine_planes.nb_elem; j++) { |
| 2958 | char buf[32]; |
Aurelien Jacobs | b44bbf9 | 2011-05-24 21:26:24 | [diff] [blame] | 2959 | if (planes[j].type >= MATROSKA_VIDEO_STEREO_PLANE_COUNT) |
Aurelien Jacobs | 4c509fe | 2011-05-23 23:09:24 | [diff] [blame] | 2960 | continue; |
| 2961 | snprintf(buf, sizeof(buf), "%s_%d", |
Clément Bœsch | ca81e3b | 2012-09-16 00:58:40 | [diff] [blame] | 2962 | ff_matroska_video_stereo_plane[planes[j].type], i); |
Aurelien Jacobs | 4c509fe | 2011-05-23 23:09:24 | [diff] [blame] | 2963 | for (k=0; k < matroska->tracks.nb_elem; k++) |
Michael Niedermayer | 5d309d3 | 2015-05-04 13:47:54 | [diff] [blame] | 2964 | if (planes[j].uid == tracks[k].uid && tracks[k].stream) { |
| 2965 | av_dict_set(&tracks[k].stream->metadata, |
Aurelien Jacobs | e6ba3d4 | 2011-06-13 17:02:50 | [diff] [blame] | 2966 | "stereo_mode", buf, 0); |
Kirill Gavrilov | e6ec921 | 2011-05-21 15:14:14 | [diff] [blame] | 2967 | break; |
| 2968 | } |
Kirill Gavrilov | e6ec921 | 2011-05-21 15:14:14 | [diff] [blame] | 2969 | } |
Vittorio Giovara | d4ae8ac | 2014-08-12 21:28:49 | [diff] [blame] | 2970 | // add stream level stereo3d side data if it is a supported format |
| 2971 | if (track->video.stereo_mode < MATROSKA_VIDEO_STEREOMODE_TYPE_NB && |
| 2972 | track->video.stereo_mode != 10 && track->video.stereo_mode != 12) { |
| 2973 | int ret = ff_mkv_stereo3d_conv(st, track->video.stereo_mode); |
| 2974 | if (ret < 0) |
| 2975 | return ret; |
| 2976 | } |
Neil Birkbeck | bbda13a | 2016-03-01 00:41:04 | [diff] [blame] | 2977 | |
James Almer | edb4f5d | 2016-12-05 02:27:54 | [diff] [blame] | 2978 | ret = mkv_parse_video_color(st, track); |
| 2979 | if (ret < 0) |
| 2980 | return ret; |
Andreas Rheinhardt | 39f5bb6 | 2020-05-17 22:25:48 | [diff] [blame] | 2981 | ret = mkv_parse_video_projection(st, track, matroska->ctx); |
James Almer | 445204c | 2016-12-06 17:48:45 | [diff] [blame] | 2982 | if (ret < 0) |
| 2983 | return ret; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 2984 | } else if (track->type == MATROSKA_TRACK_TYPE_AUDIO) { |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 2985 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 2986 | st->codecpar->codec_tag = fourcc; |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 2987 | st->codecpar->sample_rate = track->audio.out_samplerate; |
Vittorio Giovara | 8e3fab8 | 2017-03-31 14:57:00 | [diff] [blame] | 2988 | // channel layout may be already set by codec private checks above |
James Almer | 94901a9 | 2022-07-04 17:00:21 | [diff] [blame] | 2989 | if (!av_channel_layout_check(&st->codecpar->ch_layout)) { |
Vittorio Giovara | 8e3fab8 | 2017-03-31 14:57:00 | [diff] [blame] | 2990 | st->codecpar->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; |
James Almer | 94901a9 | 2022-07-04 17:00:21 | [diff] [blame] | 2991 | st->codecpar->ch_layout.nb_channels = track->audio.channels; |
| 2992 | } |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 2993 | if (!st->codecpar->bits_per_coded_sample) |
| 2994 | st->codecpar->bits_per_coded_sample = track->audio.bitdepth; |
James Almer | 63b5d04 | 2018-01-30 01:59:52 | [diff] [blame] | 2995 | if (st->codecpar->codec_id == AV_CODEC_ID_MP3 || |
| 2996 | st->codecpar->codec_id == AV_CODEC_ID_MLP || |
| 2997 | st->codecpar->codec_id == AV_CODEC_ID_TRUEHD) |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 2998 | sti->need_parsing = AVSTREAM_PARSE_FULL; |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 2999 | else if (st->codecpar->codec_id != AV_CODEC_ID_AAC) |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 3000 | sti->need_parsing = AVSTREAM_PARSE_HEADERS; |
Vignesh Venkatasubramanian | d6f86d7 | 2013-10-14 17:42:08 | [diff] [blame] | 3001 | if (track->codec_delay > 0) { |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 3002 | st->codecpar->initial_padding = av_rescale_q(track->codec_delay, |
Michael Niedermayer | b5bc436 | 2016-06-06 02:23:16 | [diff] [blame] | 3003 | (AVRational){1, 1000000000}, |
James Almer | 49b0246 | 2016-06-06 04:14:11 | [diff] [blame] | 3004 | (AVRational){1, st->codecpar->codec_id == AV_CODEC_ID_OPUS ? |
| 3005 | 48000 : st->codecpar->sample_rate}); |
Vignesh Venkatasubramanian | d6f86d7 | 2013-10-14 17:42:08 | [diff] [blame] | 3006 | } |
| 3007 | if (track->seek_preroll > 0) { |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 3008 | st->codecpar->seek_preroll = av_rescale_q(track->seek_preroll, |
| 3009 | (AVRational){1, 1000000000}, |
| 3010 | (AVRational){1, st->codecpar->sample_rate}); |
Vignesh Venkatasubramanian | d6f86d7 | 2013-10-14 17:42:08 | [diff] [blame] | 3011 | } |
Matthew Heaney | 818ebe9 | 2013-08-08 22:40:03 | [diff] [blame] | 3012 | } else if (codec_id == AV_CODEC_ID_WEBVTT) { |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 3013 | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; |
Matthew Heaney | 818ebe9 | 2013-08-08 22:40:03 | [diff] [blame] | 3014 | |
| 3015 | if (!strcmp(track->codec_id, "D_WEBVTT/CAPTIONS")) { |
| 3016 | st->disposition |= AV_DISPOSITION_CAPTIONS; |
| 3017 | } else if (!strcmp(track->codec_id, "D_WEBVTT/DESCRIPTIONS")) { |
| 3018 | st->disposition |= AV_DISPOSITION_DESCRIPTIONS; |
| 3019 | } else if (!strcmp(track->codec_id, "D_WEBVTT/METADATA")) { |
| 3020 | st->disposition |= AV_DISPOSITION_METADATA; |
| 3021 | } |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 3022 | } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) { |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 3023 | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; |
Andreas Rheinhardt | 48cf1d8 | 2021-02-16 18:38:19 | [diff] [blame] | 3024 | |
| 3025 | if (track->flag_textdescriptions) |
| 3026 | st->disposition |= AV_DISPOSITION_DESCRIPTIONS; |
Aurelien Jacobs | d88d806 | 2008-08-05 00:40:52 | [diff] [blame] | 3027 | } |
quietvoid | 05c0794 | 2022-01-01 16:51:50 | [diff] [blame] | 3028 | |
| 3029 | ret = mkv_parse_block_addition_mappings(s, st, track); |
| 3030 | if (ret < 0) |
| 3031 | return ret; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3032 | } |
| 3033 | |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 3034 | return 0; |
| 3035 | } |
| 3036 | |
| 3037 | static int matroska_read_header(AVFormatContext *s) |
| 3038 | { |
Andreas Rheinhardt | fed0282 | 2021-08-24 12:58:07 | [diff] [blame] | 3039 | FFFormatContext *const si = ffformatcontext(s); |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 3040 | MatroskaDemuxContext *matroska = s->priv_data; |
| 3041 | EbmlList *attachments_list = &matroska->attachments; |
| 3042 | EbmlList *chapters_list = &matroska->chapters; |
| 3043 | MatroskaAttachment *attachments; |
| 3044 | MatroskaChapter *chapters; |
| 3045 | uint64_t max_start = 0; |
| 3046 | int64_t pos; |
| 3047 | Ebml ebml = { 0 }; |
| 3048 | int i, j, res; |
| 3049 | |
| 3050 | matroska->ctx = s; |
wm4 | cac2295 | 2015-02-09 19:39:00 | [diff] [blame] | 3051 | matroska->cues_parsing_deferred = 1; |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 3052 | |
| 3053 | /* First read the EBML header. */ |
Thomas Guillem | b8d7f31 | 2015-04-10 17:04:51 | [diff] [blame] | 3054 | if (ebml_parse(matroska, ebml_syntax, &ebml) || !ebml.doctype) { |
| 3055 | av_log(matroska->ctx, AV_LOG_ERROR, "EBML header parsing failed\n"); |
| 3056 | ebml_free(ebml_syntax, &ebml); |
| 3057 | return AVERROR_INVALIDDATA; |
| 3058 | } |
| 3059 | if (ebml.version > EBML_VERSION || |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 3060 | ebml.max_size > sizeof(uint64_t) || |
| 3061 | ebml.id_length > sizeof(uint32_t) || |
Michael Niedermayer | 0cab093 | 2015-04-19 14:45:24 | [diff] [blame] | 3062 | ebml.doctype_version > 3) { |
Diego Biurrun | 67deba8 | 2015-12-16 17:01:34 | [diff] [blame] | 3063 | avpriv_report_missing_feature(matroska->ctx, |
| 3064 | "EBML version %"PRIu64", doctype %s, doc version %"PRIu64, |
| 3065 | ebml.version, ebml.doctype, ebml.doctype_version); |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 3066 | ebml_free(ebml_syntax, &ebml); |
| 3067 | return AVERROR_PATCHWELCOME; |
Michael Niedermayer | 69de229 | 2014-05-28 10:41:35 | [diff] [blame] | 3068 | } else if (ebml.doctype_version == 3) { |
| 3069 | av_log(matroska->ctx, AV_LOG_WARNING, |
| 3070 | "EBML header using unsupported features\n" |
| 3071 | "(EBML version %"PRIu64", doctype %s, doc version %"PRIu64")\n", |
| 3072 | ebml.version, ebml.doctype, ebml.doctype_version); |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 3073 | } |
| 3074 | for (i = 0; i < FF_ARRAY_ELEMS(matroska_doctypes); i++) |
| 3075 | if (!strcmp(ebml.doctype, matroska_doctypes[i])) |
| 3076 | break; |
| 3077 | if (i >= FF_ARRAY_ELEMS(matroska_doctypes)) { |
| 3078 | av_log(s, AV_LOG_WARNING, "Unknown EBML doctype '%s'\n", ebml.doctype); |
| 3079 | if (matroska->ctx->error_recognition & AV_EF_EXPLODE) { |
| 3080 | ebml_free(ebml_syntax, &ebml); |
| 3081 | return AVERROR_INVALIDDATA; |
| 3082 | } |
| 3083 | } |
| 3084 | ebml_free(ebml_syntax, &ebml); |
| 3085 | |
Andreas Rheinhardt | fed0282 | 2021-08-24 12:58:07 | [diff] [blame] | 3086 | matroska->pkt = si->parse_pkt; |
James Almer | 8d78e90 | 2021-01-29 13:42:48 | [diff] [blame] | 3087 | |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 3088 | /* The next thing is a segment. */ |
| 3089 | pos = avio_tell(matroska->ctx->pb); |
| 3090 | res = ebml_parse(matroska, matroska_segments, matroska); |
Andreas Rheinhardt | 60f75c9 | 2019-05-16 22:30:13 | [diff] [blame] | 3091 | // Try resyncing until we find an EBML_STOP type element. |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 3092 | while (res != 1) { |
| 3093 | res = matroska_resync(matroska, pos); |
| 3094 | if (res < 0) |
Andreas Rheinhardt | d32a403 | 2020-03-21 17:31:06 | [diff] [blame] | 3095 | return res; |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 3096 | pos = avio_tell(matroska->ctx->pb); |
| 3097 | res = ebml_parse(matroska, matroska_segment, matroska); |
Michael Niedermayer | 5282147 | 2021-01-29 20:18:36 | [diff] [blame] | 3098 | if (res == AVERROR(EIO)) // EOF is translated to EIO, this exists the loop on EOF |
Andreas Rheinhardt | d32a403 | 2020-03-21 17:31:06 | [diff] [blame] | 3099 | return res; |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 3100 | } |
Andreas Rheinhardt | 43c3ceb | 2019-05-16 22:29:51 | [diff] [blame] | 3101 | /* Set data_offset as it might be needed later by seek_frame_generic. */ |
| 3102 | if (matroska->current_id == MATROSKA_ID_CLUSTER) |
Andreas Rheinhardt | fed0282 | 2021-08-24 12:58:07 | [diff] [blame] | 3103 | si->data_offset = avio_tell(matroska->ctx->pb) - 4; |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 3104 | matroska_execute_seekhead(matroska); |
| 3105 | |
| 3106 | if (!matroska->time_scale) |
| 3107 | matroska->time_scale = 1000000; |
Michael Niedermayer | 3668007 | 2022-02-14 19:01:35 | [diff] [blame] | 3108 | if (isnan(matroska->duration)) |
| 3109 | matroska->duration = 0; |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 3110 | if (matroska->duration) |
| 3111 | matroska->ctx->duration = matroska->duration * matroska->time_scale * |
| 3112 | 1000 / AV_TIME_BASE; |
| 3113 | av_dict_set(&s->metadata, "title", matroska->title, 0); |
Michael Niedermayer | 69de229 | 2014-05-28 10:41:35 | [diff] [blame] | 3114 | av_dict_set(&s->metadata, "encoder", matroska->muxingapp, 0); |
| 3115 | |
| 3116 | if (matroska->date_utc.size == 8) |
| 3117 | matroska_metadata_creation_time(&s->metadata, AV_RB64(matroska->date_utc.data)); |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 3118 | |
| 3119 | res = matroska_parse_tracks(s); |
| 3120 | if (res < 0) |
Andreas Rheinhardt | d32a403 | 2020-03-21 17:31:06 | [diff] [blame] | 3121 | return res; |
Anton Khirnov | 6df478b | 2014-05-25 07:07:32 | [diff] [blame] | 3122 | |
Diego Biurrun | f69befe | 2014-03-07 12:54:18 | [diff] [blame] | 3123 | attachments = attachments_list->elem; |
| 3124 | for (j = 0; j < attachments_list->nb_elem; j++) { |
| 3125 | if (!(attachments[j].filename && attachments[j].mime && |
| 3126 | attachments[j].bin.data && attachments[j].bin.size > 0)) { |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 3127 | av_log(matroska->ctx, AV_LOG_ERROR, "incomplete attachment\n"); |
| 3128 | } else { |
Anton Khirnov | 3b3bbdd | 2011-06-18 09:43:24 | [diff] [blame] | 3129 | AVStream *st = avformat_new_stream(s, NULL); |
Gabriel Dume | f929ab0 | 2014-08-14 20:31:24 | [diff] [blame] | 3130 | if (!st) |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 3131 | break; |
Diego Biurrun | f69befe | 2014-03-07 12:54:18 | [diff] [blame] | 3132 | av_dict_set(&st->metadata, "filename", attachments[j].filename, 0); |
| 3133 | av_dict_set(&st->metadata, "mimetype", attachments[j].mime, 0); |
Andreas Rheinhardt | 3bd26b2 | 2020-05-06 13:24:33 | [diff] [blame] | 3134 | if (attachments[j].description) |
| 3135 | av_dict_set(&st->metadata, "title", attachments[j].description, 0); |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 3136 | st->codecpar->codec_id = AV_CODEC_ID_NONE; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 3137 | |
Andreas Rheinhardt | 67e957b | 2020-04-16 01:39:05 | [diff] [blame] | 3138 | for (i = 0; mkv_image_mime_tags[i].id != AV_CODEC_ID_NONE; i++) { |
Andreas Rheinhardt | 0f906be | 2021-02-24 06:44:31 | [diff] [blame] | 3139 | if (av_strstart(attachments[j].mime, mkv_image_mime_tags[i].str, NULL)) { |
Andreas Rheinhardt | 67e957b | 2020-04-16 01:39:05 | [diff] [blame] | 3140 | st->codecpar->codec_id = mkv_image_mime_tags[i].id; |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 3141 | break; |
| 3142 | } |
| 3143 | } |
wm4 | 511585c | 2015-04-03 14:11:53 | [diff] [blame] | 3144 | |
Diego Biurrun | f69befe | 2014-03-07 12:54:18 | [diff] [blame] | 3145 | attachments[j].stream = st; |
wm4 | c4d37cd | 2015-04-03 14:11:53 | [diff] [blame] | 3146 | |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 3147 | if (st->codecpar->codec_id != AV_CODEC_ID_NONE) { |
Andreas Rheinhardt | 39ecb63 | 2021-03-29 05:58:56 | [diff] [blame] | 3148 | res = ff_add_attached_pic(s, st, NULL, &attachments[j].bin.buf, 0); |
| 3149 | if (res < 0) |
Andreas Rheinhardt | d32a403 | 2020-03-21 17:31:06 | [diff] [blame] | 3150 | return res; |
wm4 | 511585c | 2015-04-03 14:11:53 | [diff] [blame] | 3151 | } else { |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 3152 | st->codecpar->codec_type = AVMEDIA_TYPE_ATTACHMENT; |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 3153 | if (ff_alloc_extradata(st->codecpar, attachments[j].bin.size)) |
wm4 | 511585c | 2015-04-03 14:11:53 | [diff] [blame] | 3154 | break; |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 3155 | memcpy(st->codecpar->extradata, attachments[j].bin.data, |
wm4 | c4d37cd | 2015-04-03 14:11:53 | [diff] [blame] | 3156 | attachments[j].bin.size); |
wm4 | 511585c | 2015-04-03 14:11:53 | [diff] [blame] | 3157 | |
Andreas Rheinhardt | 67e957b | 2020-04-16 01:39:05 | [diff] [blame] | 3158 | for (i = 0; mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++) { |
Andreas Rheinhardt | 0f906be | 2021-02-24 06:44:31 | [diff] [blame] | 3159 | if (av_strstart(attachments[j].mime, mkv_mime_tags[i].str, NULL)) { |
Andreas Rheinhardt | 67e957b | 2020-04-16 01:39:05 | [diff] [blame] | 3160 | st->codecpar->codec_id = mkv_mime_tags[i].id; |
wm4 | 511585c | 2015-04-03 14:11:53 | [diff] [blame] | 3161 | break; |
| 3162 | } |
| 3163 | } |
wm4 | 511585c | 2015-04-03 14:11:53 | [diff] [blame] | 3164 | } |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | chapters = chapters_list->elem; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3169 | for (i = 0; i < chapters_list->nb_elem; i++) |
| 3170 | if (chapters[i].start != AV_NOPTS_VALUE && chapters[i].uid && |
| 3171 | (max_start == 0 || chapters[i].start > max_start)) { |
Aurelien Jacobs | 6cb6e15 | 2009-02-15 15:25:14 | [diff] [blame] | 3172 | chapters[i].chapter = |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3173 | avpriv_new_chapter(s, chapters[i].uid, |
| 3174 | (AVRational) { 1, 1000000000 }, |
| 3175 | chapters[i].start, chapters[i].end, |
| 3176 | chapters[i].title); |
Aurelien Jacobs | e0e4be5 | 2009-01-15 00:42:57 | [diff] [blame] | 3177 | max_start = chapters[i].start; |
| 3178 | } |
Aurelien Jacobs | 9c25baf | 2008-08-05 00:40:55 | [diff] [blame] | 3179 | |
Aaron Colwell | 6c4cc0f | 2011-09-22 14:51:00 | [diff] [blame] | 3180 | matroska_add_index_entries(matroska); |
| 3181 | |
Aurelien Jacobs | 929e9de | 2009-02-15 15:53:55 | [diff] [blame] | 3182 | matroska_convert_tags(s); |
| 3183 | |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 3184 | return 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3185 | } |
| 3186 | |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 3187 | /* |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 3188 | * Put one packet in an application-supplied AVPacket struct. |
| 3189 | * Returns 0 on success or -1 on failure. |
| 3190 | */ |
| 3191 | static int matroska_deliver_packet(MatroskaDemuxContext *matroska, |
| 3192 | AVPacket *pkt) |
| 3193 | { |
Andreas Rheinhardt | d61240f | 2021-12-16 00:49:39 | [diff] [blame] | 3194 | if (matroska->queue.head) { |
Mats Peterson | 6aac43f | 2016-02-24 17:14:05 | [diff] [blame] | 3195 | MatroskaTrack *tracks = matroska->tracks.elem; |
| 3196 | MatroskaTrack *track; |
James Almer | 78b96be | 2018-03-26 18:02:37 | [diff] [blame] | 3197 | |
Andreas Rheinhardt | d61240f | 2021-12-16 00:49:39 | [diff] [blame] | 3198 | avpriv_packet_list_get(&matroska->queue, pkt); |
Mats Peterson | 6aac43f | 2016-02-24 17:14:05 | [diff] [blame] | 3199 | track = &tracks[pkt->stream_index]; |
| 3200 | if (track->has_palette) { |
Mats Peterson | 7973603 | 2015-12-27 20:28:09 | [diff] [blame] | 3201 | uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE); |
| 3202 | if (!pal) { |
| 3203 | av_log(matroska->ctx, AV_LOG_ERROR, "Cannot append palette to packet\n"); |
| 3204 | } else { |
Mats Peterson | 6aac43f | 2016-02-24 17:14:05 | [diff] [blame] | 3205 | memcpy(pal, track->palette, AVPALETTE_SIZE); |
Mats Peterson | 7973603 | 2015-12-27 20:28:09 | [diff] [blame] | 3206 | } |
Mats Peterson | 6aac43f | 2016-02-24 17:14:05 | [diff] [blame] | 3207 | track->has_palette = 0; |
Mats Peterson | 7973603 | 2015-12-27 20:28:09 | [diff] [blame] | 3208 | } |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 3209 | return 0; |
| 3210 | } |
| 3211 | |
| 3212 | return -1; |
| 3213 | } |
| 3214 | |
| 3215 | /* |
| 3216 | * Free all packets in our internal queue. |
| 3217 | */ |
| 3218 | static void matroska_clear_queue(MatroskaDemuxContext *matroska) |
| 3219 | { |
Andreas Rheinhardt | d61240f | 2021-12-16 00:49:39 | [diff] [blame] | 3220 | avpriv_packet_list_free(&matroska->queue); |
Aurelien Jacobs | 737c40d | 2008-08-05 00:42:39 | [diff] [blame] | 3221 | } |
| 3222 | |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3223 | static int matroska_parse_laces(MatroskaDemuxContext *matroska, uint8_t **buf, |
Andreas Rheinhardt | d5274f8 | 2019-12-03 17:09:10 | [diff] [blame] | 3224 | int size, int type, AVIOContext *pb, |
Andreas Rheinhardt | 9ad1a6d | 2019-12-03 17:09:06 | [diff] [blame] | 3225 | uint32_t lace_size[256], int *laces) |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3226 | { |
Andreas Rheinhardt | f74eaa1 | 2019-12-03 17:09:08 | [diff] [blame] | 3227 | int n; |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3228 | uint8_t *data = *buf; |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3229 | |
| 3230 | if (!type) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3231 | *laces = 1; |
Andreas Rheinhardt | 9ad1a6d | 2019-12-03 17:09:06 | [diff] [blame] | 3232 | lace_size[0] = size; |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3233 | return 0; |
| 3234 | } |
| 3235 | |
Andreas Rheinhardt | e471faf | 2020-03-25 05:00:53 | [diff] [blame] | 3236 | if (size <= 0) |
| 3237 | return AVERROR_INVALIDDATA; |
| 3238 | |
Andreas Rheinhardt | 1fd8528 | 2020-05-20 19:24:34 | [diff] [blame] | 3239 | *laces = *data + 1; |
| 3240 | data += 1; |
| 3241 | size -= 1; |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3242 | |
| 3243 | switch (type) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3244 | case 0x1: /* Xiph lacing */ |
| 3245 | { |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3246 | uint8_t temp; |
| 3247 | uint32_t total = 0; |
Andreas Rheinhardt | a69f92a | 2019-12-03 17:09:07 | [diff] [blame] | 3248 | for (n = 0; n < *laces - 1; n++) { |
Andreas Rheinhardt | 1215b3a | 2019-05-16 22:29:48 | [diff] [blame] | 3249 | lace_size[n] = 0; |
| 3250 | |
Andreas Rheinhardt | 1fd8528 | 2020-05-20 19:24:34 | [diff] [blame] | 3251 | do { |
| 3252 | if (size <= total) |
Andreas Rheinhardt | a69f92a | 2019-12-03 17:09:07 | [diff] [blame] | 3253 | return AVERROR_INVALIDDATA; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3254 | temp = *data; |
Michael Niedermayer | b3d9ab1 | 2014-03-07 22:46:37 | [diff] [blame] | 3255 | total += temp; |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3256 | lace_size[n] += temp; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3257 | data += 1; |
| 3258 | size -= 1; |
Andreas Rheinhardt | 1fd8528 | 2020-05-20 19:24:34 | [diff] [blame] | 3259 | } while (temp == 0xff); |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3260 | } |
Andreas Rheinhardt | 1fd8528 | 2020-05-20 19:24:34 | [diff] [blame] | 3261 | if (size < total) |
Andreas Rheinhardt | a69f92a | 2019-12-03 17:09:07 | [diff] [blame] | 3262 | return AVERROR_INVALIDDATA; |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3263 | |
| 3264 | lace_size[n] = size - total; |
| 3265 | break; |
| 3266 | } |
| 3267 | |
| 3268 | case 0x2: /* fixed-size lacing */ |
Andreas Rheinhardt | 1fd8528 | 2020-05-20 19:24:34 | [diff] [blame] | 3269 | if (size % (*laces)) |
Andreas Rheinhardt | a69f92a | 2019-12-03 17:09:07 | [diff] [blame] | 3270 | return AVERROR_INVALIDDATA; |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3271 | for (n = 0; n < *laces; n++) |
| 3272 | lace_size[n] = size / *laces; |
| 3273 | break; |
| 3274 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3275 | case 0x3: /* EBML lacing */ |
| 3276 | { |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3277 | uint64_t num; |
Luca Barbato | 8a96df7 | 2013-03-28 10:52:52 | [diff] [blame] | 3278 | uint64_t total; |
Andreas Rheinhardt | d5274f8 | 2019-12-03 17:09:10 | [diff] [blame] | 3279 | int offset; |
| 3280 | |
| 3281 | avio_skip(pb, 4); |
| 3282 | |
| 3283 | n = ebml_read_num(matroska, pb, 8, &num, 1); |
Andreas Rheinhardt | dbe3be6 | 2019-12-03 17:09:09 | [diff] [blame] | 3284 | if (n < 0) |
| 3285 | return n; |
| 3286 | if (num > INT_MAX) |
| 3287 | return AVERROR_INVALIDDATA; |
Andreas Rheinhardt | d5274f8 | 2019-12-03 17:09:10 | [diff] [blame] | 3288 | |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3289 | total = lace_size[0] = num; |
Andreas Rheinhardt | d5274f8 | 2019-12-03 17:09:10 | [diff] [blame] | 3290 | offset = n; |
Andreas Rheinhardt | a69f92a | 2019-12-03 17:09:07 | [diff] [blame] | 3291 | for (n = 1; n < *laces - 1; n++) { |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3292 | int64_t snum; |
| 3293 | int r; |
Andreas Rheinhardt | d5274f8 | 2019-12-03 17:09:10 | [diff] [blame] | 3294 | r = matroska_ebmlnum_sint(matroska, pb, &snum); |
Andreas Rheinhardt | dbe3be6 | 2019-12-03 17:09:09 | [diff] [blame] | 3295 | if (r < 0) |
| 3296 | return r; |
| 3297 | if (lace_size[n - 1] + snum > (uint64_t)INT_MAX) |
| 3298 | return AVERROR_INVALIDDATA; |
Andreas Rheinhardt | d5274f8 | 2019-12-03 17:09:10 | [diff] [blame] | 3299 | |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3300 | lace_size[n] = lace_size[n - 1] + snum; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3301 | total += lace_size[n]; |
Andreas Rheinhardt | d5274f8 | 2019-12-03 17:09:10 | [diff] [blame] | 3302 | offset += r; |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3303 | } |
Andreas Rheinhardt | d5274f8 | 2019-12-03 17:09:10 | [diff] [blame] | 3304 | data += offset; |
| 3305 | size -= offset; |
Andreas Rheinhardt | 1fd8528 | 2020-05-20 19:24:34 | [diff] [blame] | 3306 | if (size < total) |
Andreas Rheinhardt | a69f92a | 2019-12-03 17:09:07 | [diff] [blame] | 3307 | return AVERROR_INVALIDDATA; |
Andreas Rheinhardt | 1fd8528 | 2020-05-20 19:24:34 | [diff] [blame] | 3308 | |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3309 | lace_size[*laces - 1] = size - total; |
| 3310 | break; |
| 3311 | } |
| 3312 | } |
| 3313 | |
Andreas Rheinhardt | 1fd8528 | 2020-05-20 19:24:34 | [diff] [blame] | 3314 | *buf = data; |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3315 | |
Andreas Rheinhardt | a69f92a | 2019-12-03 17:09:07 | [diff] [blame] | 3316 | return 0; |
Luca Barbato | 2d0e771 | 2012-09-16 23:58:32 | [diff] [blame] | 3317 | } |
| 3318 | |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3319 | static int matroska_parse_rm_audio(MatroskaDemuxContext *matroska, |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3320 | MatroskaTrack *track, AVStream *st, |
| 3321 | uint8_t *data, int size, uint64_t timecode, |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3322 | int64_t pos) |
| 3323 | { |
Andreas Rheinhardt | 39fb1e9 | 2019-12-06 23:11:01 | [diff] [blame] | 3324 | const int a = st->codecpar->block_align; |
| 3325 | const int sps = track->audio.sub_packet_size; |
| 3326 | const int cfs = track->audio.coded_framesize; |
| 3327 | const int h = track->audio.sub_packet_h; |
| 3328 | const int w = track->audio.frame_size; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3329 | int y = track->audio.sub_packet_cnt; |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3330 | int x; |
| 3331 | |
| 3332 | if (!track->audio.pkt_cnt) { |
| 3333 | if (track->audio.sub_packet_cnt == 0) |
| 3334 | track->audio.buf_timecode = timecode; |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 3335 | if (st->codecpar->codec_id == AV_CODEC_ID_RA_288) { |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3336 | if (size < cfs * h / 2) { |
| 3337 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 3338 | "Corrupt int4 RM-style audio packet size\n"); |
| 3339 | return AVERROR_INVALIDDATA; |
| 3340 | } |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3341 | for (x = 0; x < h / 2; x++) |
| 3342 | memcpy(track->audio.buf + x * 2 * w + y * cfs, |
| 3343 | data + x * cfs, cfs); |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 3344 | } else if (st->codecpar->codec_id == AV_CODEC_ID_SIPR) { |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3345 | if (size < w) { |
| 3346 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 3347 | "Corrupt sipr RM-style audio packet size\n"); |
| 3348 | return AVERROR_INVALIDDATA; |
| 3349 | } |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3350 | memcpy(track->audio.buf + y * w, data, w); |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3351 | } else { |
Andreas Rheinhardt | 4b1c19a | 2020-04-20 06:54:23 | [diff] [blame] | 3352 | if (size < w) { |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3353 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 3354 | "Corrupt generic RM-style audio packet size\n"); |
| 3355 | return AVERROR_INVALIDDATA; |
| 3356 | } |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3357 | for (x = 0; x < w / sps; x++) |
| 3358 | memcpy(track->audio.buf + |
| 3359 | sps * (h * x + ((h + 1) / 2) * (y & 1) + (y >> 1)), |
| 3360 | data + x * sps, sps); |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | if (++track->audio.sub_packet_cnt >= h) { |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 3364 | if (st->codecpar->codec_id == AV_CODEC_ID_SIPR) |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3365 | ff_rm_reorder_sipr_data(track->audio.buf, h, w); |
| 3366 | track->audio.sub_packet_cnt = 0; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3367 | track->audio.pkt_cnt = h * w / a; |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3368 | } |
| 3369 | } |
| 3370 | |
| 3371 | while (track->audio.pkt_cnt) { |
Vittorio Giovara | e0caa1e | 2014-10-23 23:05:53 | [diff] [blame] | 3372 | int ret; |
James Almer | 8d78e90 | 2021-01-29 13:42:48 | [diff] [blame] | 3373 | AVPacket *pkt = matroska->pkt; |
Vittorio Giovara | e0caa1e | 2014-10-23 23:05:53 | [diff] [blame] | 3374 | |
| 3375 | ret = av_new_packet(pkt, a); |
| 3376 | if (ret < 0) { |
Vittorio Giovara | e0caa1e | 2014-10-23 23:05:53 | [diff] [blame] | 3377 | return ret; |
Michael Niedermayer | 1116491 | 2012-10-20 14:55:45 | [diff] [blame] | 3378 | } |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3379 | memcpy(pkt->data, |
| 3380 | track->audio.buf + a * (h * w / a - track->audio.pkt_cnt--), |
| 3381 | a); |
| 3382 | pkt->pts = track->audio.buf_timecode; |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3383 | track->audio.buf_timecode = AV_NOPTS_VALUE; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3384 | pkt->pos = pos; |
| 3385 | pkt->stream_index = st->index; |
Andreas Rheinhardt | d61240f | 2021-12-16 00:49:39 | [diff] [blame] | 3386 | ret = avpriv_packet_list_put(&matroska->queue, pkt, NULL, 0); |
James Almer | 78b96be | 2018-03-26 18:02:37 | [diff] [blame] | 3387 | if (ret < 0) { |
James Almer | 4f55b94 | 2018-04-04 16:53:12 | [diff] [blame] | 3388 | av_packet_unref(pkt); |
James Almer | 78b96be | 2018-03-26 18:02:37 | [diff] [blame] | 3389 | return AVERROR(ENOMEM); |
| 3390 | } |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | return 0; |
| 3394 | } |
Anton Khirnov | 9b6f47c | 2013-05-27 07:44:27 | [diff] [blame] | 3395 | |
| 3396 | /* reconstruct full wavpack blocks from mangled matroska ones */ |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3397 | static int matroska_parse_wavpack(MatroskaTrack *track, |
| 3398 | uint8_t **data, int *size) |
Anton Khirnov | 9b6f47c | 2013-05-27 07:44:27 | [diff] [blame] | 3399 | { |
| 3400 | uint8_t *dst = NULL; |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3401 | uint8_t *src = *data; |
Anton Khirnov | 9b6f47c | 2013-05-27 07:44:27 | [diff] [blame] | 3402 | int dstlen = 0; |
| 3403 | int srclen = *size; |
| 3404 | uint32_t samples; |
| 3405 | uint16_t ver; |
| 3406 | int ret, offset = 0; |
| 3407 | |
Andreas Rheinhardt | 048bc3f | 2019-12-14 00:36:54 | [diff] [blame] | 3408 | if (srclen < 12) |
Anton Khirnov | 9b6f47c | 2013-05-27 07:44:27 | [diff] [blame] | 3409 | return AVERROR_INVALIDDATA; |
| 3410 | |
Andreas Rheinhardt | 048bc3f | 2019-12-14 00:36:54 | [diff] [blame] | 3411 | av_assert1(track->stream->codecpar->extradata_size >= 2); |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 3412 | ver = AV_RL16(track->stream->codecpar->extradata); |
Anton Khirnov | 9b6f47c | 2013-05-27 07:44:27 | [diff] [blame] | 3413 | |
| 3414 | samples = AV_RL32(src); |
| 3415 | src += 4; |
| 3416 | srclen -= 4; |
| 3417 | |
| 3418 | while (srclen >= 8) { |
| 3419 | int multiblock; |
| 3420 | uint32_t blocksize; |
| 3421 | uint8_t *tmp; |
| 3422 | |
| 3423 | uint32_t flags = AV_RL32(src); |
| 3424 | uint32_t crc = AV_RL32(src + 4); |
| 3425 | src += 8; |
| 3426 | srclen -= 8; |
| 3427 | |
| 3428 | multiblock = (flags & 0x1800) != 0x1800; |
| 3429 | if (multiblock) { |
| 3430 | if (srclen < 4) { |
| 3431 | ret = AVERROR_INVALIDDATA; |
| 3432 | goto fail; |
| 3433 | } |
| 3434 | blocksize = AV_RL32(src); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3435 | src += 4; |
| 3436 | srclen -= 4; |
Anton Khirnov | 9b6f47c | 2013-05-27 07:44:27 | [diff] [blame] | 3437 | } else |
| 3438 | blocksize = srclen; |
| 3439 | |
| 3440 | if (blocksize > srclen) { |
| 3441 | ret = AVERROR_INVALIDDATA; |
| 3442 | goto fail; |
| 3443 | } |
| 3444 | |
James Almer | 9703b7d | 2018-04-04 21:12:53 | [diff] [blame] | 3445 | tmp = av_realloc(dst, dstlen + blocksize + 32 + AV_INPUT_BUFFER_PADDING_SIZE); |
Anton Khirnov | 9b6f47c | 2013-05-27 07:44:27 | [diff] [blame] | 3446 | if (!tmp) { |
| 3447 | ret = AVERROR(ENOMEM); |
| 3448 | goto fail; |
| 3449 | } |
| 3450 | dst = tmp; |
| 3451 | dstlen += blocksize + 32; |
| 3452 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3453 | AV_WL32(dst + offset, MKTAG('w', 'v', 'p', 'k')); // tag |
| 3454 | AV_WL32(dst + offset + 4, blocksize + 24); // blocksize - 8 |
| 3455 | AV_WL16(dst + offset + 8, ver); // version |
| 3456 | AV_WL16(dst + offset + 10, 0); // track/index_no |
| 3457 | AV_WL32(dst + offset + 12, 0); // total samples |
| 3458 | AV_WL32(dst + offset + 16, 0); // block index |
| 3459 | AV_WL32(dst + offset + 20, samples); // number of samples |
| 3460 | AV_WL32(dst + offset + 24, flags); // flags |
| 3461 | AV_WL32(dst + offset + 28, crc); // crc |
| 3462 | memcpy(dst + offset + 32, src, blocksize); // block data |
Anton Khirnov | 9b6f47c | 2013-05-27 07:44:27 | [diff] [blame] | 3463 | |
| 3464 | src += blocksize; |
| 3465 | srclen -= blocksize; |
| 3466 | offset += blocksize + 32; |
| 3467 | } |
| 3468 | |
James Almer | 9703b7d | 2018-04-04 21:12:53 | [diff] [blame] | 3469 | memset(dst + dstlen, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
| 3470 | |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3471 | *data = dst; |
Anton Khirnov | 9b6f47c | 2013-05-27 07:44:27 | [diff] [blame] | 3472 | *size = dstlen; |
| 3473 | |
| 3474 | return 0; |
| 3475 | |
| 3476 | fail: |
| 3477 | av_freep(&dst); |
| 3478 | return ret; |
| 3479 | } |
| 3480 | |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3481 | static int matroska_parse_prores(MatroskaTrack *track, |
| 3482 | uint8_t **data, int *size) |
James Almer | b8e75a2 | 2018-04-04 19:07:36 | [diff] [blame] | 3483 | { |
Andreas Rheinhardt | af50f0a | 2019-12-06 23:16:19 | [diff] [blame] | 3484 | uint8_t *dst; |
| 3485 | int dstlen = *size + 8; |
Andreas Rheinhardt | 581419e | 2019-09-28 17:54:25 | [diff] [blame] | 3486 | |
Andreas Rheinhardt | 39fb1e9 | 2019-12-06 23:11:01 | [diff] [blame] | 3487 | dst = av_malloc(dstlen + AV_INPUT_BUFFER_PADDING_SIZE); |
| 3488 | if (!dst) |
| 3489 | return AVERROR(ENOMEM); |
James Almer | b8e75a2 | 2018-04-04 19:07:36 | [diff] [blame] | 3490 | |
Andreas Rheinhardt | 39fb1e9 | 2019-12-06 23:11:01 | [diff] [blame] | 3491 | AV_WB32(dst, dstlen); |
| 3492 | AV_WB32(dst + 4, MKBETAG('i', 'c', 'p', 'f')); |
| 3493 | memcpy(dst + 8, *data, dstlen - 8); |
| 3494 | memset(dst + dstlen, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
James Almer | b8e75a2 | 2018-04-04 19:07:36 | [diff] [blame] | 3495 | |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3496 | *data = dst; |
James Almer | b8e75a2 | 2018-04-04 19:07:36 | [diff] [blame] | 3497 | *size = dstlen; |
| 3498 | |
| 3499 | return 0; |
| 3500 | } |
| 3501 | |
Matthew Heaney | 818ebe9 | 2013-08-08 22:40:03 | [diff] [blame] | 3502 | static int matroska_parse_webvtt(MatroskaDemuxContext *matroska, |
| 3503 | MatroskaTrack *track, |
| 3504 | AVStream *st, |
| 3505 | uint8_t *data, int data_len, |
| 3506 | uint64_t timecode, |
| 3507 | uint64_t duration, |
| 3508 | int64_t pos) |
| 3509 | { |
James Almer | 8d78e90 | 2021-01-29 13:42:48 | [diff] [blame] | 3510 | AVPacket *pkt = matroska->pkt; |
Matthew Heaney | 818ebe9 | 2013-08-08 22:40:03 | [diff] [blame] | 3511 | uint8_t *id, *settings, *text, *buf; |
| 3512 | int id_len, settings_len, text_len; |
| 3513 | uint8_t *p, *q; |
| 3514 | int err; |
| 3515 | |
| 3516 | if (data_len <= 0) |
| 3517 | return AVERROR_INVALIDDATA; |
| 3518 | |
| 3519 | p = data; |
| 3520 | q = data + data_len; |
| 3521 | |
| 3522 | id = p; |
| 3523 | id_len = -1; |
| 3524 | while (p < q) { |
| 3525 | if (*p == '\r' || *p == '\n') { |
| 3526 | id_len = p - id; |
| 3527 | if (*p == '\r') |
| 3528 | p++; |
| 3529 | break; |
| 3530 | } |
| 3531 | p++; |
| 3532 | } |
| 3533 | |
| 3534 | if (p >= q || *p != '\n') |
| 3535 | return AVERROR_INVALIDDATA; |
| 3536 | p++; |
| 3537 | |
| 3538 | settings = p; |
| 3539 | settings_len = -1; |
| 3540 | while (p < q) { |
| 3541 | if (*p == '\r' || *p == '\n') { |
| 3542 | settings_len = p - settings; |
| 3543 | if (*p == '\r') |
| 3544 | p++; |
| 3545 | break; |
| 3546 | } |
| 3547 | p++; |
| 3548 | } |
| 3549 | |
| 3550 | if (p >= q || *p != '\n') |
| 3551 | return AVERROR_INVALIDDATA; |
| 3552 | p++; |
| 3553 | |
| 3554 | text = p; |
| 3555 | text_len = q - p; |
| 3556 | while (text_len > 0) { |
| 3557 | const int len = text_len - 1; |
| 3558 | const uint8_t c = p[len]; |
| 3559 | if (c != '\r' && c != '\n') |
| 3560 | break; |
| 3561 | text_len = len; |
| 3562 | } |
| 3563 | |
| 3564 | if (text_len <= 0) |
| 3565 | return AVERROR_INVALIDDATA; |
| 3566 | |
Matthew Heaney | 818ebe9 | 2013-08-08 22:40:03 | [diff] [blame] | 3567 | err = av_new_packet(pkt, text_len); |
| 3568 | if (err < 0) { |
James Almer | f4f3958 | 2018-02-20 13:26:21 | [diff] [blame] | 3569 | return err; |
Matthew Heaney | 818ebe9 | 2013-08-08 22:40:03 | [diff] [blame] | 3570 | } |
| 3571 | |
| 3572 | memcpy(pkt->data, text, text_len); |
| 3573 | |
| 3574 | if (id_len > 0) { |
| 3575 | buf = av_packet_new_side_data(pkt, |
| 3576 | AV_PKT_DATA_WEBVTT_IDENTIFIER, |
| 3577 | id_len); |
Michael Niedermayer | fb33bff | 2014-08-15 18:33:21 | [diff] [blame] | 3578 | if (!buf) { |
James Almer | 88eb368 | 2018-02-20 13:25:54 | [diff] [blame] | 3579 | av_packet_unref(pkt); |
Matthew Heaney | 818ebe9 | 2013-08-08 22:40:03 | [diff] [blame] | 3580 | return AVERROR(ENOMEM); |
| 3581 | } |
| 3582 | memcpy(buf, id, id_len); |
| 3583 | } |
| 3584 | |
| 3585 | if (settings_len > 0) { |
| 3586 | buf = av_packet_new_side_data(pkt, |
| 3587 | AV_PKT_DATA_WEBVTT_SETTINGS, |
| 3588 | settings_len); |
Michael Niedermayer | fb33bff | 2014-08-15 18:33:21 | [diff] [blame] | 3589 | if (!buf) { |
James Almer | 88eb368 | 2018-02-20 13:25:54 | [diff] [blame] | 3590 | av_packet_unref(pkt); |
Matthew Heaney | 818ebe9 | 2013-08-08 22:40:03 | [diff] [blame] | 3591 | return AVERROR(ENOMEM); |
| 3592 | } |
| 3593 | memcpy(buf, settings, settings_len); |
| 3594 | } |
| 3595 | |
| 3596 | // Do we need this for subtitles? |
| 3597 | // pkt->flags = AV_PKT_FLAG_KEY; |
| 3598 | |
| 3599 | pkt->stream_index = st->index; |
| 3600 | pkt->pts = timecode; |
| 3601 | |
| 3602 | // Do we need this for subtitles? |
| 3603 | // pkt->dts = timecode; |
| 3604 | |
| 3605 | pkt->duration = duration; |
| 3606 | pkt->pos = pos; |
| 3607 | |
Andreas Rheinhardt | d61240f | 2021-12-16 00:49:39 | [diff] [blame] | 3608 | err = avpriv_packet_list_put(&matroska->queue, pkt, NULL, 0); |
James Almer | 78b96be | 2018-03-26 18:02:37 | [diff] [blame] | 3609 | if (err < 0) { |
James Almer | 4f55b94 | 2018-04-04 16:53:12 | [diff] [blame] | 3610 | av_packet_unref(pkt); |
James Almer | 78b96be | 2018-03-26 18:02:37 | [diff] [blame] | 3611 | return AVERROR(ENOMEM); |
| 3612 | } |
Matthew Heaney | 818ebe9 | 2013-08-08 22:40:03 | [diff] [blame] | 3613 | |
| 3614 | return 0; |
| 3615 | } |
| 3616 | |
James Almer | 4451315 | 2023-03-12 18:02:06 | [diff] [blame^] | 3617 | static int matroska_parse_block_additional(MatroskaDemuxContext *matroska, |
| 3618 | AVPacket *pkt, |
| 3619 | const uint8_t *data, int size, uint64_t id) |
| 3620 | { |
| 3621 | uint8_t *side_data = av_packet_new_side_data(pkt, |
| 3622 | AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL, |
| 3623 | size + (size_t)8); |
| 3624 | if (!side_data) |
| 3625 | return AVERROR(ENOMEM); |
| 3626 | |
| 3627 | AV_WB64(side_data, id); |
| 3628 | memcpy(side_data + 8, data, size); |
| 3629 | |
| 3630 | return 0; |
| 3631 | } |
| 3632 | |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3633 | static int matroska_parse_frame(MatroskaDemuxContext *matroska, |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3634 | MatroskaTrack *track, AVStream *st, |
Andreas Rheinhardt | 5767a2e | 2020-05-03 06:35:25 | [diff] [blame] | 3635 | AVBufferRef *buf, uint8_t *data, int pkt_size, |
Michael Niedermayer | 8c51ea5 | 2012-09-20 18:37:26 | [diff] [blame] | 3636 | uint64_t timecode, uint64_t lace_duration, |
Vignesh Venkatasubramanian | 30c5c45 | 2013-02-13 21:51:48 | [diff] [blame] | 3637 | int64_t pos, int is_keyframe, |
James Almer | 4451315 | 2023-03-12 18:02:06 | [diff] [blame^] | 3638 | MatroskaBlockMore *blockmore, int nb_blockmore, |
Jan Gerber | f4b1ca9 | 2013-11-14 11:58:28 | [diff] [blame] | 3639 | int64_t discard_padding) |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3640 | { |
Andreas Rheinhardt | 5767a2e | 2020-05-03 06:35:25 | [diff] [blame] | 3641 | uint8_t *pkt_data = data; |
Andreas Rheinhardt | e471faf | 2020-03-25 05:00:53 | [diff] [blame] | 3642 | int res = 0; |
James Almer | 8d78e90 | 2021-01-29 13:42:48 | [diff] [blame] | 3643 | AVPacket *pkt = matroska->pkt; |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3644 | |
Anton Khirnov | 9200514 | 2014-06-18 18:42:52 | [diff] [blame] | 3645 | if (st->codecpar->codec_id == AV_CODEC_ID_WAVPACK) { |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3646 | res = matroska_parse_wavpack(track, &pkt_data, &pkt_size); |
Anton Khirnov | 9b6f47c | 2013-05-27 07:44:27 | [diff] [blame] | 3647 | if (res < 0) { |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3648 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 3649 | "Error parsing a wavpack block.\n"); |
Anton Khirnov | 9b6f47c | 2013-05-27 07:44:27 | [diff] [blame] | 3650 | goto fail; |
| 3651 | } |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3652 | if (!buf) |
Andreas Rheinhardt | 5767a2e | 2020-05-03 06:35:25 | [diff] [blame] | 3653 | av_freep(&data); |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3654 | buf = NULL; |
Anton Khirnov | 9b6f47c | 2013-05-27 07:44:27 | [diff] [blame] | 3655 | } |
| 3656 | |
Andreas Rheinhardt | af50f0a | 2019-12-06 23:16:19 | [diff] [blame] | 3657 | if (st->codecpar->codec_id == AV_CODEC_ID_PRORES && |
| 3658 | AV_RB32(pkt_data + 4) != MKBETAG('i', 'c', 'p', 'f')) { |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3659 | res = matroska_parse_prores(track, &pkt_data, &pkt_size); |
James Almer | b8e75a2 | 2018-04-04 19:07:36 | [diff] [blame] | 3660 | if (res < 0) { |
| 3661 | av_log(matroska->ctx, AV_LOG_ERROR, |
| 3662 | "Error parsing a prores block.\n"); |
| 3663 | goto fail; |
| 3664 | } |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3665 | if (!buf) |
Andreas Rheinhardt | 5767a2e | 2020-05-03 06:35:25 | [diff] [blame] | 3666 | av_freep(&data); |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3667 | buf = NULL; |
James Almer | b8e75a2 | 2018-04-04 19:07:36 | [diff] [blame] | 3668 | } |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3669 | |
James Almer | 4451315 | 2023-03-12 18:02:06 | [diff] [blame^] | 3670 | if (!pkt_size && !nb_blockmore) |
Andreas Rheinhardt | e471faf | 2020-03-25 05:00:53 | [diff] [blame] | 3671 | goto no_output; |
| 3672 | |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3673 | if (!buf) |
James Almer | 9703b7d | 2018-04-04 21:12:53 | [diff] [blame] | 3674 | pkt->buf = av_buffer_create(pkt_data, pkt_size + AV_INPUT_BUFFER_PADDING_SIZE, |
| 3675 | NULL, NULL, 0); |
| 3676 | else |
| 3677 | pkt->buf = av_buffer_ref(buf); |
| 3678 | |
| 3679 | if (!pkt->buf) { |
Michael Niedermayer | 2fe4b62 | 2013-06-03 14:07:06 | [diff] [blame] | 3680 | res = AVERROR(ENOMEM); |
| 3681 | goto fail; |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3682 | } |
| 3683 | |
James Almer | 9703b7d | 2018-04-04 21:12:53 | [diff] [blame] | 3684 | pkt->data = pkt_data; |
| 3685 | pkt->size = pkt_size; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3686 | pkt->flags = is_keyframe; |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3687 | pkt->stream_index = st->index; |
| 3688 | |
James Almer | 4451315 | 2023-03-12 18:02:06 | [diff] [blame^] | 3689 | for (int i = 0; i < nb_blockmore; i++) { |
| 3690 | MatroskaBlockMore *more = &blockmore[i]; |
| 3691 | |
| 3692 | if (!more->additional.size) |
| 3693 | continue; |
| 3694 | |
| 3695 | res = matroska_parse_block_additional(matroska, pkt, more->additional.data, |
| 3696 | more->additional.size, more->additional_id); |
| 3697 | if (res < 0) { |
Hendrik Leppkes | c2f861c | 2015-10-27 13:35:30 | [diff] [blame] | 3698 | av_packet_unref(pkt); |
James Almer | 4451315 | 2023-03-12 18:02:06 | [diff] [blame^] | 3699 | return res; |
Vignesh Venkatasubramanian | 30c5c45 | 2013-02-13 21:51:48 | [diff] [blame] | 3700 | } |
Vignesh Venkatasubramanian | 30c5c45 | 2013-02-13 21:51:48 | [diff] [blame] | 3701 | } |
| 3702 | |
Vignesh Venkatasubramanian | 7b0a839 | 2013-09-10 18:12:21 | [diff] [blame] | 3703 | if (discard_padding) { |
| 3704 | uint8_t *side_data = av_packet_new_side_data(pkt, |
| 3705 | AV_PKT_DATA_SKIP_SAMPLES, |
| 3706 | 10); |
Michael Niedermayer | fb33bff | 2014-08-15 18:33:21 | [diff] [blame] | 3707 | if (!side_data) { |
Hendrik Leppkes | c2f861c | 2015-10-27 13:35:30 | [diff] [blame] | 3708 | av_packet_unref(pkt); |
Vignesh Venkatasubramanian | 7b0a839 | 2013-09-10 18:12:21 | [diff] [blame] | 3709 | return AVERROR(ENOMEM); |
| 3710 | } |
James Almer | 70c6a1b | 2016-11-05 20:46:52 | [diff] [blame] | 3711 | discard_padding = av_rescale_q(discard_padding, |
Vignesh Venkatasubramanian | 7b0a839 | 2013-09-10 18:12:21 | [diff] [blame] | 3712 | (AVRational){1, 1000000000}, |
James Almer | 70c6a1b | 2016-11-05 20:46:52 | [diff] [blame] | 3713 | (AVRational){1, st->codecpar->sample_rate}); |
| 3714 | if (discard_padding > 0) { |
| 3715 | AV_WL32(side_data + 4, discard_padding); |
| 3716 | } else { |
| 3717 | AV_WL32(side_data, -discard_padding); |
| 3718 | } |
Vignesh Venkatasubramanian | 7b0a839 | 2013-09-10 18:12:21 | [diff] [blame] | 3719 | } |
| 3720 | |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3721 | if (track->ms_compat) |
| 3722 | pkt->dts = timecode; |
| 3723 | else |
| 3724 | pkt->pts = timecode; |
| 3725 | pkt->pos = pos; |
Hendrik Leppkes | b01891a | 2015-09-29 13:14:59 | [diff] [blame] | 3726 | pkt->duration = lace_duration; |
| 3727 | |
Andreas Rheinhardt | d61240f | 2021-12-16 00:49:39 | [diff] [blame] | 3728 | res = avpriv_packet_list_put(&matroska->queue, pkt, NULL, 0); |
James Almer | 78b96be | 2018-03-26 18:02:37 | [diff] [blame] | 3729 | if (res < 0) { |
James Almer | 2f0e0de | 2018-04-04 13:54:14 | [diff] [blame] | 3730 | av_packet_unref(pkt); |
James Almer | 78b96be | 2018-03-26 18:02:37 | [diff] [blame] | 3731 | return AVERROR(ENOMEM); |
| 3732 | } |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3733 | |
| 3734 | return 0; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3735 | |
Andreas Rheinhardt | e471faf | 2020-03-25 05:00:53 | [diff] [blame] | 3736 | no_output: |
Anton Khirnov | 9b6f47c | 2013-05-27 07:44:27 | [diff] [blame] | 3737 | fail: |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3738 | if (!buf) |
Andreas Rheinhardt | 5767a2e | 2020-05-03 06:35:25 | [diff] [blame] | 3739 | av_free(pkt_data); |
Anton Khirnov | 9b6f47c | 2013-05-27 07:44:27 | [diff] [blame] | 3740 | return res; |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3741 | } |
| 3742 | |
James Almer | 9703b7d | 2018-04-04 21:12:53 | [diff] [blame] | 3743 | static int matroska_parse_block(MatroskaDemuxContext *matroska, AVBufferRef *buf, uint8_t *data, |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 3744 | int size, int64_t pos, uint64_t cluster_time, |
Luca Barbato | 7d84310 | 2012-09-17 00:48:02 | [diff] [blame] | 3745 | uint64_t block_duration, int is_keyframe, |
James Almer | 4451315 | 2023-03-12 18:02:06 | [diff] [blame^] | 3746 | MatroskaBlockMore *blockmore, int nb_blockmore, |
Jan Gerber | f4b1ca9 | 2013-11-14 11:58:28 | [diff] [blame] | 3747 | int64_t cluster_pos, int64_t discard_padding) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3748 | { |
Aurelien Jacobs | f14a201 | 2008-09-09 11:54:35 | [diff] [blame] | 3749 | uint64_t timecode = AV_NOPTS_VALUE; |
Aurelien Jacobs | 009ecd5 | 2008-08-05 00:40:12 | [diff] [blame] | 3750 | MatroskaTrack *track; |
Andreas Rheinhardt | 45bfe8b | 2021-08-04 14:52:07 | [diff] [blame] | 3751 | FFIOContext pb; |
Aurelien Jacobs | a3467f8 | 2008-09-06 23:44:29 | [diff] [blame] | 3752 | int res = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3753 | AVStream *st; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3754 | int16_t block_time; |
Andreas Rheinhardt | 9ad1a6d | 2019-12-03 17:09:06 | [diff] [blame] | 3755 | uint32_t lace_size[256]; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3756 | int n, flags, laces = 0; |
| 3757 | uint64_t num; |
Steve Lhomme | 5bd870a | 2020-11-15 09:00:35 | [diff] [blame] | 3758 | int trust_default_duration; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3759 | |
Michael Niedermayer | d84f036 | 2022-05-19 22:36:34 | [diff] [blame] | 3760 | av_assert1(buf); |
| 3761 | |
Andreas Rheinhardt | d5274f8 | 2019-12-03 17:09:10 | [diff] [blame] | 3762 | ffio_init_context(&pb, data, size, 0, NULL, NULL, NULL, NULL); |
| 3763 | |
Andreas Rheinhardt | 45bfe8b | 2021-08-04 14:52:07 | [diff] [blame] | 3764 | if ((n = ebml_read_num(matroska, &pb.pub, 8, &num, 1)) < 0) |
Luca Barbato | 721af29 | 2012-04-30 00:39:31 | [diff] [blame] | 3765 | return n; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3766 | data += n; |
| 3767 | size -= n; |
| 3768 | |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3769 | track = matroska_find_track_by_num(matroska, num); |
Andreas Rheinhardt | b577968 | 2020-03-25 05:52:09 | [diff] [blame] | 3770 | if (!track || size < 3) |
Reimar Döffinger | d493170 | 2012-02-13 22:06:19 | [diff] [blame] | 3771 | return AVERROR_INVALIDDATA; |
Andreas Rheinhardt | b577968 | 2020-03-25 05:52:09 | [diff] [blame] | 3772 | |
| 3773 | if (!(st = track->stream)) { |
| 3774 | av_log(matroska->ctx, AV_LOG_VERBOSE, |
| 3775 | "No stream associated to TrackNumber %"PRIu64". " |
| 3776 | "Ignoring Block with this TrackNumber.\n", num); |
| 3777 | return 0; |
| 3778 | } |
| 3779 | |
Aurelien Jacobs | 16f97ab | 2008-08-05 00:41:10 | [diff] [blame] | 3780 | if (st->discard >= AVDISCARD_ALL) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3781 | return res; |
Andreas Rheinhardt | 3714d45 | 2019-09-03 22:50:11 | [diff] [blame] | 3782 | if (block_duration > INT64_MAX) |
| 3783 | block_duration = INT64_MAX; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3784 | |
Michael Niedermayer | 729fa55 | 2013-05-19 21:38:01 | [diff] [blame] | 3785 | block_time = sign_extend(AV_RB16(data), 16); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3786 | data += 2; |
| 3787 | flags = *data++; |
| 3788 | size -= 3; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3789 | if (is_keyframe == -1) |
Jean-Daniel Dupas | cc947f0 | 2010-03-31 12:29:58 | [diff] [blame] | 3790 | is_keyframe = flags & 0x80 ? AV_PKT_FLAG_KEY : 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3791 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3792 | if (cluster_time != (uint64_t) -1 && |
| 3793 | (block_time >= 0 || cluster_time >= -block_time)) { |
Steve Lhomme | 3a2b786 | 2020-11-15 08:59:48 | [diff] [blame] | 3794 | uint64_t timecode_cluster_in_track_tb = (double) cluster_time / track->time_scale; |
| 3795 | timecode = timecode_cluster_in_track_tb + block_time - track->codec_delay_in_track_tb; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3796 | if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE && |
| 3797 | timecode < track->end_timecode) |
Aurelien Jacobs | 82360e6 | 2008-09-09 12:07:10 | [diff] [blame] | 3798 | is_keyframe = 0; /* overlapping subtitles are not key frame */ |
Michael Niedermayer | b347ca9 | 2016-12-26 02:16:01 | [diff] [blame] | 3799 | if (is_keyframe) { |
| 3800 | ff_reduce_index(matroska->ctx, st->index); |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3801 | av_add_index_entry(st, cluster_pos, timecode, 0, 0, |
| 3802 | AVINDEX_KEYFRAME); |
Michael Niedermayer | b347ca9 | 2016-12-26 02:16:01 | [diff] [blame] | 3803 | } |
Aurelien Jacobs | f14a201 | 2008-09-09 11:54:35 | [diff] [blame] | 3804 | } |
| 3805 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 3806 | if (matroska->skip_to_keyframe && |
| 3807 | track->type != MATROSKA_TRACK_TYPE_SUBTITLE) { |
Chris Cunningham | d59820f | 2016-07-21 19:01:45 | [diff] [blame] | 3808 | // Compare signed timecodes. Timecode may be negative due to codec delay |
| 3809 | // offset. We don't support timestamps greater than int64_t anyway - see |
| 3810 | // AVPacket's pts. |
Chris Cunningham | 52ec4cc | 2016-07-28 01:33:30 | [diff] [blame] | 3811 | if ((int64_t)timecode < (int64_t)matroska->skip_to_timecode) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3812 | return res; |
Monty Montgomery | f6622f9 | 2013-09-11 06:00:31 | [diff] [blame] | 3813 | if (is_keyframe) |
| 3814 | matroska->skip_to_keyframe = 0; |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 3815 | else if (!ffstream(st)->skip_to_keyframe) { |
Reimar Döffinger | 4a95876 | 2012-04-13 22:17:30 | [diff] [blame] | 3816 | av_log(matroska->ctx, AV_LOG_ERROR, "File is broken, keyframes not correctly marked!\n"); |
| 3817 | matroska->skip_to_keyframe = 0; |
| 3818 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3819 | } |
| 3820 | |
Andreas Rheinhardt | f74eaa1 | 2019-12-03 17:09:08 | [diff] [blame] | 3821 | res = matroska_parse_laces(matroska, &data, size, (flags & 0x06) >> 1, |
Andreas Rheinhardt | 45bfe8b | 2021-08-04 14:52:07 | [diff] [blame] | 3822 | &pb.pub, lace_size, &laces); |
Andreas Rheinhardt | dbe3be6 | 2019-12-03 17:09:09 | [diff] [blame] | 3823 | if (res < 0) { |
| 3824 | av_log(matroska->ctx, AV_LOG_ERROR, "Error parsing frame sizes.\n"); |
Andreas Rheinhardt | 9ad1a6d | 2019-12-03 17:09:06 | [diff] [blame] | 3825 | return res; |
Andreas Rheinhardt | dbe3be6 | 2019-12-03 17:09:09 | [diff] [blame] | 3826 | } |
Aurelien Jacobs | eabb8ba | 2007-06-04 22:19:17 | [diff] [blame] | 3827 | |
Steve Lhomme | 5bd870a | 2020-11-15 09:00:35 | [diff] [blame] | 3828 | trust_default_duration = track->default_duration != 0; |
| 3829 | if (track->audio.samplerate == 8000 && trust_default_duration) { |
Michael Niedermayer | 6158a3b | 2013-07-15 15:13:45 | [diff] [blame] | 3830 | // If this is needed for more codecs, then add them here |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 3831 | if (st->codecpar->codec_id == AV_CODEC_ID_AC3) { |
| 3832 | if (track->audio.samplerate != st->codecpar->sample_rate || !st->codecpar->frame_size) |
Michael Niedermayer | 6158a3b | 2013-07-15 15:13:45 | [diff] [blame] | 3833 | trust_default_duration = 0; |
| 3834 | } |
| 3835 | } |
| 3836 | |
| 3837 | if (!block_duration && trust_default_duration) |
Michael Niedermayer | 5864ce1 | 2012-09-20 19:46:35 | [diff] [blame] | 3838 | block_duration = track->default_duration * laces / matroska->time_scale; |
Michael Niedermayer | 8c51ea5 | 2012-09-20 18:37:26 | [diff] [blame] | 3839 | |
| 3840 | if (cluster_time != (uint64_t)-1 && (block_time >= 0 || cluster_time >= -block_time)) |
Luca Barbato | 7d84310 | 2012-09-17 00:48:02 | [diff] [blame] | 3841 | track->end_timecode = |
| 3842 | FFMAX(track->end_timecode, timecode + block_duration); |
Michael Niedermayer | 8c51ea5 | 2012-09-20 18:37:26 | [diff] [blame] | 3843 | |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3844 | for (n = 0; n < laces; n++) { |
Michael Niedermayer | 5864ce1 | 2012-09-20 19:46:35 | [diff] [blame] | 3845 | int64_t lace_duration = block_duration*(n+1) / laces - block_duration*n / laces; |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3846 | uint8_t *out_data = data; |
| 3847 | int out_size = lace_size[n]; |
| 3848 | |
| 3849 | if (track->needs_decoding) { |
| 3850 | res = matroska_decode_buffer(&out_data, &out_size, track); |
| 3851 | if (res < 0) |
| 3852 | return res; |
| 3853 | /* Given that we are here means that out_data is no longer |
| 3854 | * owned by buf, so set it to NULL. This depends upon |
| 3855 | * zero-length header removal compression being ignored. */ |
| 3856 | av_assert1(out_data != data); |
| 3857 | buf = NULL; |
| 3858 | } |
Michael Niedermayer | 8c51ea5 | 2012-09-20 18:37:26 | [diff] [blame] | 3859 | |
Andreas Rheinhardt | c6f60b9 | 2019-12-02 10:04:55 | [diff] [blame] | 3860 | if (track->audio.buf) { |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3861 | res = matroska_parse_rm_audio(matroska, track, st, |
| 3862 | out_data, out_size, |
Michael Niedermayer | a6ec1e4 | 2012-09-20 21:43:20 | [diff] [blame] | 3863 | timecode, pos); |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3864 | if (!buf) |
| 3865 | av_free(out_data); |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3866 | if (res) |
Andreas Rheinhardt | 9ad1a6d | 2019-12-03 17:09:06 | [diff] [blame] | 3867 | return res; |
Derek Buitenhuis | 6f69f7a | 2016-04-10 19:58:15 | [diff] [blame] | 3868 | } else if (st->codecpar->codec_id == AV_CODEC_ID_WEBVTT) { |
Matthew Heaney | 818ebe9 | 2013-08-08 22:40:03 | [diff] [blame] | 3869 | res = matroska_parse_webvtt(matroska, track, st, |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3870 | out_data, out_size, |
Matthew Heaney | 818ebe9 | 2013-08-08 22:40:03 | [diff] [blame] | 3871 | timecode, lace_duration, |
| 3872 | pos); |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3873 | if (!buf) |
| 3874 | av_free(out_data); |
Matthew Heaney | 818ebe9 | 2013-08-08 22:40:03 | [diff] [blame] | 3875 | if (res) |
Andreas Rheinhardt | 9ad1a6d | 2019-12-03 17:09:06 | [diff] [blame] | 3876 | return res; |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3877 | } else { |
Andreas Rheinhardt | 5767a2e | 2020-05-03 06:35:25 | [diff] [blame] | 3878 | res = matroska_parse_frame(matroska, track, st, buf, out_data, |
Andreas Rheinhardt | 979b5b8 | 2019-12-06 08:53:34 | [diff] [blame] | 3879 | out_size, timecode, lace_duration, |
| 3880 | pos, !n ? is_keyframe : 0, |
James Almer | 4451315 | 2023-03-12 18:02:06 | [diff] [blame^] | 3881 | blockmore, nb_blockmore, |
Michael Niedermayer | b3d9ab1 | 2014-03-07 22:46:37 | [diff] [blame] | 3882 | discard_padding); |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3883 | if (res) |
Andreas Rheinhardt | 9ad1a6d | 2019-12-03 17:09:06 | [diff] [blame] | 3884 | return res; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3885 | } |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3886 | |
| 3887 | if (timecode != AV_NOPTS_VALUE) |
Michael Niedermayer | 8c51ea5 | 2012-09-20 18:37:26 | [diff] [blame] | 3888 | timecode = lace_duration ? timecode + lace_duration : AV_NOPTS_VALUE; |
Luca Barbato | c831ebf | 2012-09-16 23:28:13 | [diff] [blame] | 3889 | data += lace_size[n]; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3890 | } |
| 3891 | |
Andreas Rheinhardt | 9ad1a6d | 2019-12-03 17:09:06 | [diff] [blame] | 3892 | return 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3893 | } |
| 3894 | |
Andreas Rheinhardt | 70baf72 | 2019-05-16 22:29:58 | [diff] [blame] | 3895 | static int matroska_parse_cluster(MatroskaDemuxContext *matroska) |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 3896 | { |
Andreas Rheinhardt | ffa64a4 | 2019-05-16 22:29:59 | [diff] [blame] | 3897 | MatroskaCluster *cluster = &matroska->current_cluster; |
| 3898 | MatroskaBlock *block = &cluster->block; |
| 3899 | int res; |
Andreas Rheinhardt | 865c537 | 2019-05-16 22:30:08 | [diff] [blame] | 3900 | |
| 3901 | av_assert0(matroska->num_levels <= 2); |
| 3902 | |
| 3903 | if (matroska->num_levels == 1) { |
Andreas Rheinhardt | 71c9088 | 2019-05-16 22:30:18 | [diff] [blame] | 3904 | res = ebml_parse(matroska, matroska_segment, NULL); |
Andreas Rheinhardt | 865c537 | 2019-05-16 22:30:08 | [diff] [blame] | 3905 | |
| 3906 | if (res == 1) { |
| 3907 | /* Found a cluster: subtract the size of the ID already read. */ |
| 3908 | cluster->pos = avio_tell(matroska->ctx->pb) - 4; |
| 3909 | |
| 3910 | res = ebml_parse(matroska, matroska_cluster_enter, cluster); |
| 3911 | if (res < 0) |
| 3912 | return res; |
| 3913 | } |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 3914 | } |
| 3915 | |
Andreas Rheinhardt | 865c537 | 2019-05-16 22:30:08 | [diff] [blame] | 3916 | if (matroska->num_levels == 2) { |
Andreas Rheinhardt | 865c537 | 2019-05-16 22:30:08 | [diff] [blame] | 3917 | /* We are inside a cluster. */ |
| 3918 | res = ebml_parse(matroska, matroska_cluster_parsing, cluster); |
| 3919 | |
| 3920 | if (res >= 0 && block->bin.size > 0) { |
Andreas Rheinhardt | 2f1a562 | 2021-02-16 15:18:02 | [diff] [blame] | 3921 | int is_keyframe = block->non_simple ? block->reference.count == 0 : -1; |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 3922 | |
Andreas Rheinhardt | a9f0515 | 2019-05-16 22:30:09 | [diff] [blame] | 3923 | res = matroska_parse_block(matroska, block->bin.buf, block->bin.data, |
Andreas Rheinhardt | ffa64a4 | 2019-05-16 22:29:59 | [diff] [blame] | 3924 | block->bin.size, block->bin.pos, |
Andreas Rheinhardt | 865c537 | 2019-05-16 22:30:08 | [diff] [blame] | 3925 | cluster->timecode, block->duration, |
James Almer | 4451315 | 2023-03-12 18:02:06 | [diff] [blame^] | 3926 | is_keyframe, block->blockmore.elem, |
| 3927 | block->blockmore.nb_elem, cluster->pos, |
Andreas Rheinhardt | ffa64a4 | 2019-05-16 22:29:59 | [diff] [blame] | 3928 | block->discard_padding); |
Andreas Rheinhardt | 865c537 | 2019-05-16 22:30:08 | [diff] [blame] | 3929 | } |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 3930 | |
Andreas Rheinhardt | 60f75c9 | 2019-05-16 22:30:13 | [diff] [blame] | 3931 | ebml_free(matroska_blockgroup, block); |
| 3932 | memset(block, 0, sizeof(*block)); |
Andreas Rheinhardt | 865c537 | 2019-05-16 22:30:08 | [diff] [blame] | 3933 | } else if (!matroska->num_levels) { |
Andreas Rheinhardt | 3ed2755 | 2019-05-16 22:30:11 | [diff] [blame] | 3934 | if (!avio_feof(matroska->ctx->pb)) { |
| 3935 | avio_r8(matroska->ctx->pb); |
| 3936 | if (!avio_feof(matroska->ctx->pb)) { |
| 3937 | av_log(matroska->ctx, AV_LOG_WARNING, "File extends beyond " |
| 3938 | "end of segment.\n"); |
| 3939 | return AVERROR_INVALIDDATA; |
| 3940 | } |
| 3941 | } |
Andreas Rheinhardt | 865c537 | 2019-05-16 22:30:08 | [diff] [blame] | 3942 | matroska->done = 1; |
| 3943 | return AVERROR_EOF; |
| 3944 | } |
| 3945 | |
Dale Curtis | 8336eb6 | 2012-04-19 18:12:24 | [diff] [blame] | 3946 | return res; |
| 3947 | } |
| 3948 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 3949 | static int matroska_read_packet(AVFormatContext *s, AVPacket *pkt) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3950 | { |
| 3951 | MatroskaDemuxContext *matroska = s->priv_data; |
Sophia Wang | 8c83062 | 2016-09-27 19:00:29 | [diff] [blame] | 3952 | int ret = 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3953 | |
Andreas Rheinhardt | a3db9f6 | 2019-05-16 22:30:05 | [diff] [blame] | 3954 | if (matroska->resync_pos == -1) { |
| 3955 | // This can only happen if generic seeking has been used. |
| 3956 | matroska->resync_pos = avio_tell(s->pb); |
| 3957 | } |
| 3958 | |
Hendrik Leppkes | 8689d87 | 2011-07-06 17:57:11 | [diff] [blame] | 3959 | while (matroska_deliver_packet(matroska, pkt)) { |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3960 | if (matroska->done) |
Sophia Wang | 8c83062 | 2016-09-27 19:00:29 | [diff] [blame] | 3961 | return (ret < 0) ? ret : AVERROR_EOF; |
Andreas Rheinhardt | 3ed2755 | 2019-05-16 22:30:11 | [diff] [blame] | 3962 | if (matroska_parse_cluster(matroska) < 0 && !matroska->done) |
Andreas Rheinhardt | a3db9f6 | 2019-05-16 22:30:05 | [diff] [blame] | 3963 | ret = matroska_resync(matroska, matroska->resync_pos); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3964 | } |
| 3965 | |
Michael Niedermayer | d1afa72 | 2019-02-06 14:29:38 | [diff] [blame] | 3966 | return 0; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3967 | } |
| 3968 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 3969 | static int matroska_read_seek(AVFormatContext *s, int stream_index, |
| 3970 | int64_t timestamp, int flags) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3971 | { |
| 3972 | MatroskaDemuxContext *matroska = s->priv_data; |
Xiaohan Wang | 33301f0 | 2014-11-06 20:59:54 | [diff] [blame] | 3973 | MatroskaTrack *tracks = NULL; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3974 | AVStream *st = s->streams[stream_index]; |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 3975 | FFStream *const sti = ffstream(st); |
Andreas Rheinhardt | c6bb825 | 2019-05-16 22:29:45 | [diff] [blame] | 3976 | int i, index; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3977 | |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 3978 | /* Parse the CUES now since we need the index data to seek. */ |
Reimar Döffinger | 47e015e | 2012-02-12 13:09:03 | [diff] [blame] | 3979 | if (matroska->cues_parsing_deferred > 0) { |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 3980 | matroska->cues_parsing_deferred = 0; |
Reimar Döffinger | 47e015e | 2012-02-12 13:09:03 | [diff] [blame] | 3981 | matroska_parse_cues(matroska); |
Aaron Colwell | 31ad14c | 2011-07-09 05:48:43 | [diff] [blame] | 3982 | } |
| 3983 | |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 3984 | if (!sti->nb_index_entries) |
Reimar Döffinger | 47e015e | 2012-02-12 13:09:03 | [diff] [blame] | 3985 | goto err; |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 3986 | timestamp = FFMAX(timestamp, sti->index_entries[0].timestamp); |
Aurelien Jacobs | dfbbbdc | 2008-08-24 23:57:29 | [diff] [blame] | 3987 | |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 3988 | if ((index = av_index_search_timestamp(st, timestamp, flags)) < 0 || |
| 3989 | index == sti->nb_index_entries - 1) { |
| 3990 | matroska_reset_status(matroska, 0, sti->index_entries[sti->nb_index_entries - 1].pos); |
| 3991 | while ((index = av_index_search_timestamp(st, timestamp, flags)) < 0 || |
| 3992 | index == sti->nb_index_entries - 1) { |
Aurelien Jacobs | 0dbddda | 2008-08-27 19:58:55 | [diff] [blame] | 3993 | matroska_clear_queue(matroska); |
| 3994 | if (matroska_parse_cluster(matroska) < 0) |
| 3995 | break; |
| 3996 | } |
Aurelien Jacobs | 6bef5f9 | 2008-08-27 19:57:42 | [diff] [blame] | 3997 | } |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 3998 | |
Aurelien Jacobs | 243cc4c | 2007-12-29 18:35:38 | [diff] [blame] | 3999 | matroska_clear_queue(matroska); |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4000 | if (index < 0 || (matroska->cues_parsing_deferred < 0 && |
| 4001 | index == sti->nb_index_entries - 1)) |
Reimar Döffinger | 47e015e | 2012-02-12 13:09:03 | [diff] [blame] | 4002 | goto err; |
Aurelien Jacobs | 243cc4c | 2007-12-29 18:35:38 | [diff] [blame] | 4003 | |
Xiaohan Wang | 33301f0 | 2014-11-06 20:59:54 | [diff] [blame] | 4004 | tracks = matroska->tracks.elem; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 4005 | for (i = 0; i < matroska->tracks.nb_elem; i++) { |
| 4006 | tracks[i].audio.pkt_cnt = 0; |
Reimar Döffinger | b09e506 | 2011-02-26 11:52:01 | [diff] [blame] | 4007 | tracks[i].audio.sub_packet_cnt = 0; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 4008 | tracks[i].audio.buf_timecode = AV_NOPTS_VALUE; |
| 4009 | tracks[i].end_timecode = 0; |
Aurelien Jacobs | c165825 | 2008-09-09 12:10:25 | [diff] [blame] | 4010 | } |
| 4011 | |
Andreas Rheinhardt | 8a286e7 | 2019-05-16 22:30:03 | [diff] [blame] | 4012 | /* We seek to a level 1 element, so set the appropriate status. */ |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4013 | matroska_reset_status(matroska, 0, sti->index_entries[index].pos); |
Michael Niedermayer | 7c00d85 | 2013-02-07 20:34:35 | [diff] [blame] | 4014 | if (flags & AVSEEK_FLAG_ANY) { |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4015 | sti->skip_to_keyframe = 0; |
Michael Niedermayer | 7c00d85 | 2013-02-07 20:34:35 | [diff] [blame] | 4016 | matroska->skip_to_timecode = timestamp; |
| 4017 | } else { |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4018 | sti->skip_to_keyframe = 1; |
| 4019 | matroska->skip_to_timecode = sti->index_entries[index].timestamp; |
Michael Niedermayer | 7c00d85 | 2013-02-07 20:34:35 | [diff] [blame] | 4020 | } |
| 4021 | matroska->skip_to_keyframe = 1; |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 4022 | matroska->done = 0; |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4023 | avpriv_update_cur_dts(s, st, sti->index_entries[index].timestamp); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 4024 | return 0; |
Reimar Döffinger | 47e015e | 2012-02-12 13:09:03 | [diff] [blame] | 4025 | err: |
| 4026 | // slightly hackish but allows proper fallback to |
| 4027 | // the generic seeking code. |
Andreas Rheinhardt | 8a286e7 | 2019-05-16 22:30:03 | [diff] [blame] | 4028 | matroska_reset_status(matroska, 0, -1); |
Andreas Rheinhardt | a3db9f6 | 2019-05-16 22:30:05 | [diff] [blame] | 4029 | matroska->resync_pos = -1; |
Reimar Döffinger | 47e015e | 2012-02-12 13:09:03 | [diff] [blame] | 4030 | matroska_clear_queue(matroska); |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4031 | sti->skip_to_keyframe = |
Reimar Döffinger | 47e015e | 2012-02-12 13:09:03 | [diff] [blame] | 4032 | matroska->skip_to_keyframe = 0; |
| 4033 | matroska->done = 0; |
Reimar Döffinger | 47e015e | 2012-02-12 13:09:03 | [diff] [blame] | 4034 | return -1; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 4035 | } |
| 4036 | |
Aurelien Jacobs | f7b9687 | 2008-08-05 00:42:05 | [diff] [blame] | 4037 | static int matroska_read_close(AVFormatContext *s) |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 4038 | { |
| 4039 | MatroskaDemuxContext *matroska = s->priv_data; |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 4040 | MatroskaTrack *tracks = matroska->tracks.elem; |
Aurelien Jacobs | 70109c0 | 2008-08-05 00:41:13 | [diff] [blame] | 4041 | int n; |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 4042 | |
Aurelien Jacobs | 34c9c1b | 2007-12-29 18:32:47 | [diff] [blame] | 4043 | matroska_clear_queue(matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 4044 | |
Keiji Costantini | 84cfce9 | 2014-03-01 16:28:15 | [diff] [blame] | 4045 | for (n = 0; n < matroska->tracks.nb_elem; n++) |
Aurelien Jacobs | 2cbc881 | 2008-08-05 00:40:31 | [diff] [blame] | 4046 | if (tracks[n].type == MATROSKA_TRACK_TYPE_AUDIO) |
Michael Niedermayer | 6e70e4a | 2015-01-06 11:48:38 | [diff] [blame] | 4047 | av_freep(&tracks[n].audio.buf); |
Aurelien Jacobs | ce6f28b | 2008-08-05 00:40:58 | [diff] [blame] | 4048 | ebml_free(matroska_segment, matroska); |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 4049 | |
| 4050 | return 0; |
| 4051 | } |
| 4052 | |
Andreas Rheinhardt | 201b8a4 | 2021-08-20 07:31:34 | [diff] [blame] | 4053 | #if CONFIG_WEBM_DASH_MANIFEST_DEMUXER |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4054 | typedef struct { |
| 4055 | int64_t start_time_ns; |
| 4056 | int64_t end_time_ns; |
| 4057 | int64_t start_offset; |
| 4058 | int64_t end_offset; |
| 4059 | } CueDesc; |
| 4060 | |
James Zern | 20aeee4 | 2017-04-17 17:59:31 | [diff] [blame] | 4061 | /* This function searches all the Cues and returns the CueDesc corresponding to |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4062 | * the timestamp ts. Returned CueDesc will be such that start_time_ns <= ts < |
Michael Niedermayer | aa441ac | 2022-09-18 16:35:19 | [diff] [blame] | 4063 | * end_time_ns. All 4 fields will be set to -1 if ts >= file's duration or |
| 4064 | * if an error occurred. |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4065 | */ |
| 4066 | static CueDesc get_cue_desc(AVFormatContext *s, int64_t ts, int64_t cues_start) { |
| 4067 | MatroskaDemuxContext *matroska = s->priv_data; |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4068 | FFStream *const sti = ffstream(s->streams[0]); |
| 4069 | AVIndexEntry *const index_entries = sti->index_entries; |
| 4070 | int nb_index_entries = sti->nb_index_entries; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4071 | CueDesc cue_desc; |
| 4072 | int i; |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4073 | |
Michael Niedermayer | bd3a03d | 2022-03-10 22:24:49 | [diff] [blame] | 4074 | if (ts >= (int64_t)(matroska->duration * matroska->time_scale)) |
| 4075 | return (CueDesc) {-1, -1, -1, -1}; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4076 | for (i = 1; i < nb_index_entries; i++) { |
| 4077 | if (index_entries[i - 1].timestamp * matroska->time_scale <= ts && |
| 4078 | index_entries[i].timestamp * matroska->time_scale > ts) { |
| 4079 | break; |
| 4080 | } |
| 4081 | } |
| 4082 | --i; |
Michael Niedermayer | aa441ac | 2022-09-18 16:35:19 | [diff] [blame] | 4083 | if (index_entries[i].timestamp > matroska->duration) |
| 4084 | return (CueDesc) {-1, -1, -1, -1}; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4085 | cue_desc.start_time_ns = index_entries[i].timestamp * matroska->time_scale; |
| 4086 | cue_desc.start_offset = index_entries[i].pos - matroska->segment_start; |
| 4087 | if (i != nb_index_entries - 1) { |
| 4088 | cue_desc.end_time_ns = index_entries[i + 1].timestamp * matroska->time_scale; |
| 4089 | cue_desc.end_offset = index_entries[i + 1].pos - matroska->segment_start; |
| 4090 | } else { |
| 4091 | cue_desc.end_time_ns = matroska->duration * matroska->time_scale; |
| 4092 | // FIXME: this needs special handling for files where Cues appear |
| 4093 | // before Clusters. the current logic assumes Cues appear after |
| 4094 | // Clusters. |
| 4095 | cue_desc.end_offset = cues_start - matroska->segment_start; |
| 4096 | } |
| 4097 | return cue_desc; |
| 4098 | } |
| 4099 | |
| 4100 | static int webm_clusters_start_with_keyframe(AVFormatContext *s) |
| 4101 | { |
| 4102 | MatroskaDemuxContext *matroska = s->priv_data; |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4103 | AVStream *const st = s->streams[0]; |
| 4104 | FFStream *const sti = ffstream(st); |
Andreas Rheinhardt | 8a286e7 | 2019-05-16 22:30:03 | [diff] [blame] | 4105 | uint32_t id = matroska->current_id; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4106 | int64_t cluster_pos, before_pos; |
| 4107 | int index, rv = 1; |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4108 | |
| 4109 | if (sti->nb_index_entries <= 0) |
| 4110 | return 0; |
| 4111 | |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4112 | // seek to the first cluster using cues. |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4113 | index = av_index_search_timestamp(st, 0, 0); |
| 4114 | if (index < 0) |
| 4115 | return 0; |
| 4116 | cluster_pos = sti->index_entries[index].pos; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4117 | before_pos = avio_tell(s->pb); |
| 4118 | while (1) { |
Andreas Rheinhardt | 36aceb6 | 2019-05-16 22:29:49 | [diff] [blame] | 4119 | uint64_t cluster_id, cluster_length; |
| 4120 | int read; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4121 | AVPacket *pkt; |
| 4122 | avio_seek(s->pb, cluster_pos, SEEK_SET); |
| 4123 | // read cluster id and length |
Andreas Rheinhardt | 239c736 | 2019-06-25 01:08:56 | [diff] [blame] | 4124 | read = ebml_read_num(matroska, matroska->ctx->pb, 4, &cluster_id, 1); |
Andreas Rheinhardt | 36aceb6 | 2019-05-16 22:29:49 | [diff] [blame] | 4125 | if (read < 0 || cluster_id != 0xF43B675) // done with all clusters |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4126 | break; |
Andreas Rheinhardt | 36aceb6 | 2019-05-16 22:29:49 | [diff] [blame] | 4127 | read = ebml_read_length(matroska, matroska->ctx->pb, &cluster_length); |
| 4128 | if (read < 0) |
| 4129 | break; |
Andreas Rheinhardt | 8a286e7 | 2019-05-16 22:30:03 | [diff] [blame] | 4130 | |
| 4131 | matroska_reset_status(matroska, 0, cluster_pos); |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4132 | matroska_clear_queue(matroska); |
| 4133 | if (matroska_parse_cluster(matroska) < 0 || |
Andreas Rheinhardt | d61240f | 2021-12-16 00:49:39 | [diff] [blame] | 4134 | !matroska->queue.head) { |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4135 | break; |
| 4136 | } |
Andreas Rheinhardt | d61240f | 2021-12-16 00:49:39 | [diff] [blame] | 4137 | pkt = &matroska->queue.head->pkt; |
Andreas Rheinhardt | 36aceb6 | 2019-05-16 22:29:49 | [diff] [blame] | 4138 | // 4 + read is the length of the cluster id and the cluster length field. |
| 4139 | cluster_pos += 4 + read + cluster_length; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4140 | if (!(pkt->flags & AV_PKT_FLAG_KEY)) { |
| 4141 | rv = 0; |
| 4142 | break; |
| 4143 | } |
| 4144 | } |
Andreas Rheinhardt | 8a286e7 | 2019-05-16 22:30:03 | [diff] [blame] | 4145 | |
| 4146 | /* Restore the status after matroska_read_header: */ |
| 4147 | matroska_reset_status(matroska, id, before_pos); |
| 4148 | |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4149 | return rv; |
| 4150 | } |
| 4151 | |
| 4152 | static int buffer_size_after_time_downloaded(int64_t time_ns, double search_sec, int64_t bps, |
| 4153 | double min_buffer, double* buffer, |
| 4154 | double* sec_to_download, AVFormatContext *s, |
| 4155 | int64_t cues_start) |
| 4156 | { |
| 4157 | double nano_seconds_per_second = 1000000000.0; |
| 4158 | double time_sec = time_ns / nano_seconds_per_second; |
| 4159 | int rv = 0; |
| 4160 | int64_t time_to_search_ns = (int64_t)(search_sec * nano_seconds_per_second); |
| 4161 | int64_t end_time_ns = time_ns + time_to_search_ns; |
| 4162 | double sec_downloaded = 0.0; |
| 4163 | CueDesc desc_curr = get_cue_desc(s, time_ns, cues_start); |
| 4164 | if (desc_curr.start_time_ns == -1) |
| 4165 | return -1; |
| 4166 | *sec_to_download = 0.0; |
| 4167 | |
| 4168 | // Check for non cue start time. |
| 4169 | if (time_ns > desc_curr.start_time_ns) { |
| 4170 | int64_t cue_nano = desc_curr.end_time_ns - time_ns; |
| 4171 | double percent = (double)(cue_nano) / (desc_curr.end_time_ns - desc_curr.start_time_ns); |
| 4172 | double cueBytes = (desc_curr.end_offset - desc_curr.start_offset) * percent; |
| 4173 | double timeToDownload = (cueBytes * 8.0) / bps; |
| 4174 | |
| 4175 | sec_downloaded += (cue_nano / nano_seconds_per_second) - timeToDownload; |
| 4176 | *sec_to_download += timeToDownload; |
| 4177 | |
| 4178 | // Check if the search ends within the first cue. |
| 4179 | if (desc_curr.end_time_ns >= end_time_ns) { |
| 4180 | double desc_end_time_sec = desc_curr.end_time_ns / nano_seconds_per_second; |
| 4181 | double percent_to_sub = search_sec / (desc_end_time_sec - time_sec); |
| 4182 | sec_downloaded = percent_to_sub * sec_downloaded; |
| 4183 | *sec_to_download = percent_to_sub * *sec_to_download; |
| 4184 | } |
| 4185 | |
| 4186 | if ((sec_downloaded + *buffer) <= min_buffer) { |
| 4187 | return 1; |
| 4188 | } |
| 4189 | |
| 4190 | // Get the next Cue. |
| 4191 | desc_curr = get_cue_desc(s, desc_curr.end_time_ns, cues_start); |
| 4192 | } |
| 4193 | |
| 4194 | while (desc_curr.start_time_ns != -1) { |
| 4195 | int64_t desc_bytes = desc_curr.end_offset - desc_curr.start_offset; |
| 4196 | int64_t desc_ns = desc_curr.end_time_ns - desc_curr.start_time_ns; |
| 4197 | double desc_sec = desc_ns / nano_seconds_per_second; |
| 4198 | double bits = (desc_bytes * 8.0); |
| 4199 | double time_to_download = bits / bps; |
| 4200 | |
| 4201 | sec_downloaded += desc_sec - time_to_download; |
| 4202 | *sec_to_download += time_to_download; |
| 4203 | |
| 4204 | if (desc_curr.end_time_ns >= end_time_ns) { |
| 4205 | double desc_end_time_sec = desc_curr.end_time_ns / nano_seconds_per_second; |
| 4206 | double percent_to_sub = search_sec / (desc_end_time_sec - time_sec); |
| 4207 | sec_downloaded = percent_to_sub * sec_downloaded; |
| 4208 | *sec_to_download = percent_to_sub * *sec_to_download; |
| 4209 | |
| 4210 | if ((sec_downloaded + *buffer) <= min_buffer) |
| 4211 | rv = 1; |
| 4212 | break; |
| 4213 | } |
| 4214 | |
| 4215 | if ((sec_downloaded + *buffer) <= min_buffer) { |
| 4216 | rv = 1; |
| 4217 | break; |
| 4218 | } |
| 4219 | |
| 4220 | desc_curr = get_cue_desc(s, desc_curr.end_time_ns, cues_start); |
| 4221 | } |
| 4222 | *buffer = *buffer + sec_downloaded; |
| 4223 | return rv; |
| 4224 | } |
| 4225 | |
| 4226 | /* This function computes the bandwidth of the WebM file with the help of |
| 4227 | * buffer_size_after_time_downloaded() function. Both of these functions are |
| 4228 | * adapted from WebM Tools project and are adapted to work with FFmpeg's |
| 4229 | * Matroska parsing mechanism. |
| 4230 | * |
| 4231 | * Returns the bandwidth of the file on success; -1 on error. |
| 4232 | * */ |
| 4233 | static int64_t webm_dash_manifest_compute_bandwidth(AVFormatContext *s, int64_t cues_start) |
| 4234 | { |
| 4235 | MatroskaDemuxContext *matroska = s->priv_data; |
| 4236 | AVStream *st = s->streams[0]; |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4237 | FFStream *const sti = ffstream(st); |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4238 | double bandwidth = 0.0; |
Michael Niedermayer | e240d01 | 2014-07-15 22:06:15 | [diff] [blame] | 4239 | |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4240 | for (int i = 0; i < sti->nb_index_entries; i++) { |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4241 | int64_t prebuffer_ns = 1000000000; |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4242 | int64_t time_ns = sti->index_entries[i].timestamp * matroska->time_scale; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4243 | double nano_seconds_per_second = 1000000000.0; |
| 4244 | int64_t prebuffered_ns = time_ns + prebuffer_ns; |
| 4245 | double prebuffer_bytes = 0.0; |
| 4246 | int64_t temp_prebuffer_ns = prebuffer_ns; |
| 4247 | int64_t pre_bytes, pre_ns; |
| 4248 | double pre_sec, prebuffer, bits_per_second; |
| 4249 | CueDesc desc_beg = get_cue_desc(s, time_ns, cues_start); |
| 4250 | |
| 4251 | // Start with the first Cue. |
| 4252 | CueDesc desc_end = desc_beg; |
| 4253 | |
| 4254 | // Figure out how much data we have downloaded for the prebuffer. This will |
| 4255 | // be used later to adjust the bits per sample to try. |
| 4256 | while (desc_end.start_time_ns != -1 && desc_end.end_time_ns < prebuffered_ns) { |
| 4257 | // Prebuffered the entire Cue. |
| 4258 | prebuffer_bytes += desc_end.end_offset - desc_end.start_offset; |
| 4259 | temp_prebuffer_ns -= desc_end.end_time_ns - desc_end.start_time_ns; |
| 4260 | desc_end = get_cue_desc(s, desc_end.end_time_ns, cues_start); |
| 4261 | } |
| 4262 | if (desc_end.start_time_ns == -1) { |
| 4263 | // The prebuffer is larger than the duration. |
Vignesh Venkatasubramanian | b1071db | 2014-10-01 17:13:31 | [diff] [blame] | 4264 | if (matroska->duration * matroska->time_scale >= prebuffered_ns) |
| 4265 | return -1; |
| 4266 | bits_per_second = 0.0; |
| 4267 | } else { |
| 4268 | // The prebuffer ends in the last Cue. Estimate how much data was |
| 4269 | // prebuffered. |
| 4270 | pre_bytes = desc_end.end_offset - desc_end.start_offset; |
| 4271 | pre_ns = desc_end.end_time_ns - desc_end.start_time_ns; |
Michael Niedermayer | 710e516 | 2022-02-13 14:20:02 | [diff] [blame] | 4272 | if (pre_ns <= 0) |
| 4273 | return -1; |
Vignesh Venkatasubramanian | b1071db | 2014-10-01 17:13:31 | [diff] [blame] | 4274 | pre_sec = pre_ns / nano_seconds_per_second; |
| 4275 | prebuffer_bytes += |
| 4276 | pre_bytes * ((temp_prebuffer_ns / nano_seconds_per_second) / pre_sec); |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4277 | |
Vignesh Venkatasubramanian | b1071db | 2014-10-01 17:13:31 | [diff] [blame] | 4278 | prebuffer = prebuffer_ns / nano_seconds_per_second; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4279 | |
Vignesh Venkatasubramanian | b1071db | 2014-10-01 17:13:31 | [diff] [blame] | 4280 | // Set this to 0.0 in case our prebuffer buffers the entire video. |
| 4281 | bits_per_second = 0.0; |
| 4282 | do { |
| 4283 | int64_t desc_bytes = desc_end.end_offset - desc_beg.start_offset; |
| 4284 | int64_t desc_ns = desc_end.end_time_ns - desc_beg.start_time_ns; |
Michael Niedermayer | 5038933 | 2022-02-05 19:37:22 | [diff] [blame] | 4285 | double desc_sec, calc_bits_per_second, percent, mod_bits_per_second; |
| 4286 | if (desc_bytes <= 0) |
| 4287 | return -1; |
| 4288 | |
| 4289 | desc_sec = desc_ns / nano_seconds_per_second; |
| 4290 | calc_bits_per_second = (desc_bytes * 8) / desc_sec; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4291 | |
Vignesh Venkatasubramanian | b1071db | 2014-10-01 17:13:31 | [diff] [blame] | 4292 | // Drop the bps by the percentage of bytes buffered. |
Michael Niedermayer | 5038933 | 2022-02-05 19:37:22 | [diff] [blame] | 4293 | percent = (desc_bytes - prebuffer_bytes) / desc_bytes; |
| 4294 | mod_bits_per_second = calc_bits_per_second * percent; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4295 | |
Vignesh Venkatasubramanian | b1071db | 2014-10-01 17:13:31 | [diff] [blame] | 4296 | if (prebuffer < desc_sec) { |
| 4297 | double search_sec = |
| 4298 | (double)(matroska->duration * matroska->time_scale) / nano_seconds_per_second; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4299 | |
Vignesh Venkatasubramanian | b1071db | 2014-10-01 17:13:31 | [diff] [blame] | 4300 | // Add 1 so the bits per second should be a little bit greater than file |
| 4301 | // datarate. |
| 4302 | int64_t bps = (int64_t)(mod_bits_per_second) + 1; |
| 4303 | const double min_buffer = 0.0; |
| 4304 | double buffer = prebuffer; |
| 4305 | double sec_to_download = 0.0; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4306 | |
Vignesh Venkatasubramanian | b1071db | 2014-10-01 17:13:31 | [diff] [blame] | 4307 | int rv = buffer_size_after_time_downloaded(prebuffered_ns, search_sec, bps, |
| 4308 | min_buffer, &buffer, &sec_to_download, |
| 4309 | s, cues_start); |
| 4310 | if (rv < 0) { |
| 4311 | return -1; |
| 4312 | } else if (rv == 0) { |
| 4313 | bits_per_second = (double)(bps); |
| 4314 | break; |
| 4315 | } |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4316 | } |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4317 | |
Vignesh Venkatasubramanian | b1071db | 2014-10-01 17:13:31 | [diff] [blame] | 4318 | desc_end = get_cue_desc(s, desc_end.end_time_ns, cues_start); |
| 4319 | } while (desc_end.start_time_ns != -1); |
| 4320 | } |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4321 | if (bandwidth < bits_per_second) bandwidth = bits_per_second; |
| 4322 | } |
| 4323 | return (int64_t)bandwidth; |
| 4324 | } |
| 4325 | |
Derek Buitenhuis | 6ba1c9b | 2017-04-21 15:40:35 | [diff] [blame] | 4326 | static int webm_dash_manifest_cues(AVFormatContext *s, int64_t init_range) |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4327 | { |
| 4328 | MatroskaDemuxContext *matroska = s->priv_data; |
| 4329 | EbmlList *seekhead_list = &matroska->seekhead; |
| 4330 | MatroskaSeekhead *seekhead = seekhead_list->elem; |
Andreas Rheinhardt | b2d61d0 | 2021-08-25 14:13:12 | [diff] [blame] | 4331 | AVStream *const st = s->streams[0]; |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4332 | FFStream *const sti = ffstream(st); |
Andreas Rheinhardt | b2d61d0 | 2021-08-25 14:13:12 | [diff] [blame] | 4333 | AVBPrint bprint; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4334 | char *buf; |
Vignesh Venkatasubramanian | 080acf7 | 2014-08-25 16:15:13 | [diff] [blame] | 4335 | int64_t cues_start = -1, cues_end = -1, before_pos, bandwidth; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4336 | int i; |
Andreas Rheinhardt | b2d61d0 | 2021-08-25 14:13:12 | [diff] [blame] | 4337 | int ret; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4338 | |
| 4339 | // determine cues start and end positions |
| 4340 | for (i = 0; i < seekhead_list->nb_elem; i++) |
| 4341 | if (seekhead[i].id == MATROSKA_ID_CUES) |
| 4342 | break; |
| 4343 | |
| 4344 | if (i >= seekhead_list->nb_elem) return -1; |
| 4345 | |
| 4346 | before_pos = avio_tell(matroska->ctx->pb); |
| 4347 | cues_start = seekhead[i].pos + matroska->segment_start; |
| 4348 | if (avio_seek(matroska->ctx->pb, cues_start, SEEK_SET) == cues_start) { |
Vignesh Venkatasubramanian | 8acb765 | 2014-10-01 17:13:30 | [diff] [blame] | 4349 | // cues_end is computed as cues_start + cues_length + length of the |
Andreas Rheinhardt | a27e539 | 2019-05-16 22:29:54 | [diff] [blame] | 4350 | // Cues element ID (i.e. 4) + EBML length of the Cues element. |
| 4351 | // cues_end is inclusive and the above sum is reduced by 1. |
| 4352 | uint64_t cues_length, cues_id; |
| 4353 | int bytes_read; |
Andreas Rheinhardt | 239c736 | 2019-06-25 01:08:56 | [diff] [blame] | 4354 | bytes_read = ebml_read_num (matroska, matroska->ctx->pb, 4, &cues_id, 1); |
Andreas Rheinhardt | a27e539 | 2019-05-16 22:29:54 | [diff] [blame] | 4355 | if (bytes_read < 0 || cues_id != (MATROSKA_ID_CUES & 0xfffffff)) |
| 4356 | return bytes_read < 0 ? bytes_read : AVERROR_INVALIDDATA; |
| 4357 | bytes_read = ebml_read_length(matroska, matroska->ctx->pb, &cues_length); |
| 4358 | if (bytes_read < 0) |
| 4359 | return bytes_read; |
| 4360 | cues_end = cues_start + 4 + bytes_read + cues_length - 1; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4361 | } |
| 4362 | avio_seek(matroska->ctx->pb, before_pos, SEEK_SET); |
Vignesh Venkatasubramanian | 080acf7 | 2014-08-25 16:15:13 | [diff] [blame] | 4363 | if (cues_start == -1 || cues_end == -1) return -1; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4364 | |
| 4365 | // parse the cues |
| 4366 | matroska_parse_cues(matroska); |
| 4367 | |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4368 | if (!sti->nb_index_entries) |
Andreas Rheinhardt | b2d61d0 | 2021-08-25 14:13:12 | [diff] [blame] | 4369 | return AVERROR_INVALIDDATA; |
| 4370 | |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4371 | // cues start |
Reimar Döffinger | a0941c8 | 2014-07-29 19:10:39 | [diff] [blame] | 4372 | av_dict_set_int(&s->streams[0]->metadata, CUES_START, cues_start, 0); |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4373 | |
| 4374 | // cues end |
Reimar Döffinger | a0941c8 | 2014-07-29 19:10:39 | [diff] [blame] | 4375 | av_dict_set_int(&s->streams[0]->metadata, CUES_END, cues_end, 0); |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4376 | |
Andreas Rheinhardt | 60f75c9 | 2019-05-16 22:30:13 | [diff] [blame] | 4377 | // if the file has cues at the start, fix up the init range so that |
Derek Buitenhuis | 6ba1c9b | 2017-04-21 15:40:35 | [diff] [blame] | 4378 | // it does not include it |
| 4379 | if (cues_start <= init_range) |
| 4380 | av_dict_set_int(&s->streams[0]->metadata, INITIALIZATION_RANGE, cues_start - 1, 0); |
| 4381 | |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4382 | // bandwidth |
| 4383 | bandwidth = webm_dash_manifest_compute_bandwidth(s, cues_start); |
| 4384 | if (bandwidth < 0) return -1; |
Reimar Döffinger | a0941c8 | 2014-07-29 19:10:39 | [diff] [blame] | 4385 | av_dict_set_int(&s->streams[0]->metadata, BANDWIDTH, bandwidth, 0); |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4386 | |
| 4387 | // check if all clusters start with key frames |
Reimar Döffinger | a0941c8 | 2014-07-29 19:10:39 | [diff] [blame] | 4388 | av_dict_set_int(&s->streams[0]->metadata, CLUSTER_KEYFRAME, webm_clusters_start_with_keyframe(s), 0); |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4389 | |
Andreas Rheinhardt | b2d61d0 | 2021-08-25 14:13:12 | [diff] [blame] | 4390 | // Store cue point timestamps as a comma separated list |
| 4391 | // for checking subsegment alignment in the muxer. |
| 4392 | av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED); |
Andreas Rheinhardt | 40bdd8c | 2021-08-24 17:41:16 | [diff] [blame] | 4393 | for (int i = 0; i < sti->nb_index_entries; i++) |
| 4394 | av_bprintf(&bprint, "%" PRId64",", sti->index_entries[i].timestamp); |
Andreas Rheinhardt | b2d61d0 | 2021-08-25 14:13:12 | [diff] [blame] | 4395 | if (!av_bprint_is_complete(&bprint)) { |
| 4396 | av_bprint_finalize(&bprint, NULL); |
| 4397 | return AVERROR(ENOMEM); |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4398 | } |
Andreas Rheinhardt | b2d61d0 | 2021-08-25 14:13:12 | [diff] [blame] | 4399 | // Remove the trailing ',' |
| 4400 | bprint.str[--bprint.len] = '\0'; |
| 4401 | if ((ret = av_bprint_finalize(&bprint, &buf)) < 0) |
| 4402 | return ret; |
Andreas Rheinhardt | 40d9cbd | 2019-11-10 04:07:31 | [diff] [blame] | 4403 | av_dict_set(&s->streams[0]->metadata, CUE_TIMESTAMPS, |
| 4404 | buf, AV_DICT_DONT_STRDUP_VAL); |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4405 | |
| 4406 | return 0; |
| 4407 | } |
| 4408 | |
| 4409 | static int webm_dash_manifest_read_header(AVFormatContext *s) |
| 4410 | { |
| 4411 | char *buf; |
| 4412 | int ret = matroska_read_header(s); |
Derek Buitenhuis | 6ba1c9b | 2017-04-21 15:40:35 | [diff] [blame] | 4413 | int64_t init_range; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4414 | MatroskaTrack *tracks; |
| 4415 | MatroskaDemuxContext *matroska = s->priv_data; |
| 4416 | if (ret) { |
| 4417 | av_log(s, AV_LOG_ERROR, "Failed to read file headers\n"); |
| 4418 | return -1; |
| 4419 | } |
Andreas Rheinhardt | 1ef3057 | 2019-08-30 13:18:29 | [diff] [blame] | 4420 | if (!matroska->tracks.nb_elem || !s->nb_streams) { |
Andreas Rheinhardt | 1ef3057 | 2019-08-30 13:18:29 | [diff] [blame] | 4421 | av_log(s, AV_LOG_ERROR, "No track found\n"); |
Andreas Rheinhardt | d32a403 | 2020-03-21 17:31:06 | [diff] [blame] | 4422 | return AVERROR_INVALIDDATA; |
Andreas Cadhalpun | ff100c9 | 2016-11-07 23:42:23 | [diff] [blame] | 4423 | } |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4424 | |
Vignesh Venkatasubramanian | 2171b7c | 2015-03-31 23:51:57 | [diff] [blame] | 4425 | if (!matroska->is_live) { |
| 4426 | buf = av_asprintf("%g", matroska->duration); |
Andreas Rheinhardt | d32a403 | 2020-03-21 17:31:06 | [diff] [blame] | 4427 | if (!buf) |
| 4428 | return AVERROR(ENOMEM); |
Andreas Rheinhardt | 40d9cbd | 2019-11-10 04:07:31 | [diff] [blame] | 4429 | av_dict_set(&s->streams[0]->metadata, DURATION, |
| 4430 | buf, AV_DICT_DONT_STRDUP_VAL); |
Vignesh Venkatasubramanian | 2171b7c | 2015-03-31 23:51:57 | [diff] [blame] | 4431 | |
| 4432 | // initialization range |
| 4433 | // 5 is the offset of Cluster ID. |
Derek Buitenhuis | 6ba1c9b | 2017-04-21 15:40:35 | [diff] [blame] | 4434 | init_range = avio_tell(s->pb) - 5; |
| 4435 | av_dict_set_int(&s->streams[0]->metadata, INITIALIZATION_RANGE, init_range, 0); |
Vignesh Venkatasubramanian | 2171b7c | 2015-03-31 23:51:57 | [diff] [blame] | 4436 | } |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4437 | |
| 4438 | // basename of the file |
Marton Balint | 18ac642 | 2017-12-29 22:30:14 | [diff] [blame] | 4439 | buf = strrchr(s->url, '/'); |
| 4440 | av_dict_set(&s->streams[0]->metadata, FILENAME, buf ? ++buf : s->url, 0); |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4441 | |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4442 | // track number |
| 4443 | tracks = matroska->tracks.elem; |
Reimar Döffinger | a0941c8 | 2014-07-29 19:10:39 | [diff] [blame] | 4444 | av_dict_set_int(&s->streams[0]->metadata, TRACK_NUMBER, tracks[0].num, 0); |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4445 | |
| 4446 | // parse the cues and populate Cue related fields |
Vignesh Venkatasubramanian | 62c27fd | 2017-04-12 04:33:28 | [diff] [blame] | 4447 | if (!matroska->is_live) { |
Derek Buitenhuis | 6ba1c9b | 2017-04-21 15:40:35 | [diff] [blame] | 4448 | ret = webm_dash_manifest_cues(s, init_range); |
Vignesh Venkatasubramanian | 62c27fd | 2017-04-12 04:33:28 | [diff] [blame] | 4449 | if (ret < 0) { |
| 4450 | av_log(s, AV_LOG_ERROR, "Error parsing Cues\n"); |
Andreas Rheinhardt | d32a403 | 2020-03-21 17:31:06 | [diff] [blame] | 4451 | return ret; |
Vignesh Venkatasubramanian | 62c27fd | 2017-04-12 04:33:28 | [diff] [blame] | 4452 | } |
| 4453 | } |
| 4454 | |
| 4455 | // use the bandwidth from the command line if it was provided |
| 4456 | if (matroska->bandwidth > 0) { |
| 4457 | av_dict_set_int(&s->streams[0]->metadata, BANDWIDTH, |
| 4458 | matroska->bandwidth, 0); |
| 4459 | } |
| 4460 | return 0; |
Vignesh Venkatasubramanian | 5a20656 | 2014-07-07 19:52:37 | [diff] [blame] | 4461 | } |
| 4462 | |
| 4463 | static int webm_dash_manifest_read_packet(AVFormatContext *s, AVPacket *pkt) |
| 4464 | { |
| 4465 | return AVERROR_EOF; |
| 4466 | } |
| 4467 | |
Vignesh Venkatasubramanian | 2171b7c | 2015-03-31 23:51:57 | [diff] [blame] | 4468 | #define OFFSET(x) offsetof(MatroskaDemuxContext, x) |
| 4469 | static const AVOption options[] = { |
Clément Bœsch | 43ecec0 | 2015-11-21 21:05:07 | [diff] [blame] | 4470 | { "live", "flag indicating that the input is a live file that only has the headers.", OFFSET(is_live), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM }, |
Vignesh Venkatasubramanian | 62c27fd | 2017-04-12 04:33:28 | [diff] [blame] | 4471 | { "bandwidth", "bandwidth of this stream to be specified in the DASH manifest.", OFFSET(bandwidth), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, |
Vignesh Venkatasubramanian | 2171b7c | 2015-03-31 23:51:57 | [diff] [blame] | 4472 | { NULL }, |
| 4473 | }; |
| 4474 | |
| 4475 | static const AVClass webm_dash_class = { |
| 4476 | .class_name = "WebM DASH Manifest demuxer", |
| 4477 | .item_name = av_default_item_name, |
| 4478 | .option = options, |
| 4479 | .version = LIBAVUTIL_VERSION_INT, |
| 4480 | }; |
| 4481 | |
Andreas Rheinhardt | 201b8a4 | 2021-08-20 07:31:34 | [diff] [blame] | 4482 | const AVInputFormat ff_webm_dash_manifest_demuxer = { |
| 4483 | .name = "webm_dash_manifest", |
| 4484 | .long_name = NULL_IF_CONFIG_SMALL("WebM DASH Manifest"), |
| 4485 | .priv_class = &webm_dash_class, |
| 4486 | .priv_data_size = sizeof(MatroskaDemuxContext), |
| 4487 | .flags_internal = FF_FMT_INIT_CLEANUP, |
| 4488 | .read_header = webm_dash_manifest_read_header, |
| 4489 | .read_packet = webm_dash_manifest_read_packet, |
| 4490 | .read_close = matroska_read_close, |
| 4491 | }; |
| 4492 | #endif |
| 4493 | |
Andreas Rheinhardt | bc70684 | 2021-04-19 17:45:24 | [diff] [blame] | 4494 | const AVInputFormat ff_matroska_demuxer = { |
Anton Khirnov | dfc2c4d | 2011-07-16 20:18:12 | [diff] [blame] | 4495 | .name = "matroska,webm", |
Diego Biurrun | 6774247 | 2012-07-24 21:51:41 | [diff] [blame] | 4496 | .long_name = NULL_IF_CONFIG_SMALL("Matroska / WebM"), |
Andreas Rheinhardt | 173d902 | 2019-12-28 03:05:44 | [diff] [blame] | 4497 | .extensions = "mkv,mk3d,mka,mks,webm", |
Anton Khirnov | dfc2c4d | 2011-07-16 20:18:12 | [diff] [blame] | 4498 | .priv_data_size = sizeof(MatroskaDemuxContext), |
Andreas Rheinhardt | d32a403 | 2020-03-21 17:31:06 | [diff] [blame] | 4499 | .flags_internal = FF_FMT_INIT_CLEANUP, |
Anton Khirnov | dfc2c4d | 2011-07-16 20:18:12 | [diff] [blame] | 4500 | .read_probe = matroska_probe, |
| 4501 | .read_header = matroska_read_header, |
| 4502 | .read_packet = matroska_read_packet, |
| 4503 | .read_close = matroska_read_close, |
| 4504 | .read_seek = matroska_read_seek, |
Luca Barbato | fa38573 | 2014-03-13 21:14:43 | [diff] [blame] | 4505 | .mime_type = "audio/webm,audio/x-matroska,video/webm,video/x-matroska" |
David Conrad | b061d89 | 2007-06-04 22:10:54 | [diff] [blame] | 4506 | }; |