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