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