Mercurial > mplayer.hg
annotate libmpdemux/demux_mkv.c @ 31525:dd2b64511f05
Move dvd_speed extern variable declaration from stream_dvd_common.h to stream.h.
All other dvd_ extern variable declarations are there already and this avoids
#ifdefs around an #include of stream_dvd_common.h in cfg-common.h, which would
be necessary because stream_dvd_common.h contains dvdread #includes.
author | diego |
---|---|
date | Wed, 30 Jun 2010 10:09:19 +0000 |
parents | 6428a4b38763 |
children | 31eb98126392 |
rev | line source |
---|---|
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1 /* |
29238
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
2 * Matroska demuxer |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
3 * Copyright (C) 2004 Aurelien Jacobs <aurel@gnuage.org> |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
4 * Based on the one written by Ronald Bultje for gstreamer |
29238
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
5 * and on demux_mkv.cpp from Moritz Bunkus. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
6 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
7 * This file is part of MPlayer. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
8 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
9 * MPlayer is free software; you can redistribute it and/or modify |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
10 * it under the terms of the GNU General Public License as published by |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
11 * the Free Software Foundation; either version 2 of the License, or |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
12 * (at your option) any later version. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
13 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
14 * MPlayer is distributed in the hope that it will be useful, |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
17 * GNU General Public License for more details. |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
18 * |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
19 * You should have received a copy of the GNU General Public License along |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
20 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
d643e4643313
Add standard license header to all files in libmpdemux.
diego
parents:
28468
diff
changeset
|
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
22 */ |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
23 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
24 #include "config.h" |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
25 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
26 #include <stdlib.h> |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
27 #include <stdio.h> |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
28 #include <ctype.h> |
18558
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18507
diff
changeset
|
29 #include <inttypes.h> |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
30 |
22605
4d81dbdf46b9
Add explicit location for headers from the stream/ directory.
diego
parents:
22445
diff
changeset
|
31 #include "stream/stream.h" |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
32 #include "demuxer.h" |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
33 #include "stheader.h" |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
34 #include "ebml.h" |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
35 #include "matroska.h" |
30573
02420b32391b
Add separate header for real_fix_timestamp(); avoids forward declarations.
diego
parents:
30085
diff
changeset
|
36 #include "demux_real.h" |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
37 |
20680 | 38 #include "mp_msg.h" |
39 #include "help_mp.h" | |
40 | |
25292
a6a49a7a4be0
Move vobsub palette->yuv convert code into a common function.
ulion
parents:
25289
diff
changeset
|
41 #include "vobsub.h" |
17012 | 42 #include "subreader.h" |
43 #include "libvo/sub.h" | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
44 |
18937
9e95ac641e77
Initial libass release (without mencoder support).
eugeni
parents:
18934
diff
changeset
|
45 #include "libass/ass_mp.h" |
9e95ac641e77
Initial libass release (without mencoder support).
eugeni
parents:
18934
diff
changeset
|
46 |
22616 | 47 #include "libavutil/common.h" |
48 | |
27341
e7c989f7a7c9
Start unifying names of internal preprocessor directives.
diego
parents:
26752
diff
changeset
|
49 #ifdef CONFIG_QTX_CODECS |
22606
97343cedd966
Use explicit path for headers from the loader/ directory.
diego
parents:
22605
diff
changeset
|
50 #include "loader/qtx/qtxsdk/components.h" |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
51 #endif |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
52 |
28440 | 53 #if CONFIG_ZLIB |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
54 #include <zlib.h> |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
55 #endif |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
56 |
22081 | 57 #include "libavutil/lzo.h" |
23386
585d2136f018
Get rid of __attribute__((__packed__)) in Matroska demuxer
reimar
parents:
23310
diff
changeset
|
58 #include "libavutil/intreadwrite.h" |
23703
9fb716ab06a3
Avoid code duplication and ugly config.h hack by using av_strlcat/av_strlcpy
reimar
parents:
23630
diff
changeset
|
59 #include "libavutil/avstring.h" |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
60 |
31177 | 61 static const unsigned char sipr_swaps[38][2] = { |
18036 | 62 {0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68}, |
63 {13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46}, | |
64 {25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56}, | |
65 {42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83}, | |
31177 | 66 {77,80} |
67 }; | |
18036 | 68 |
69 // Map flavour to bytes per second | |
70 #define SIPR_FLAVORS 4 | |
71 #define ATRC_FLAVORS 8 | |
72 #define COOK_FLAVORS 34 | |
31177 | 73 static const int sipr_fl2bps[SIPR_FLAVORS] = { 813, 1062, 625, 2000 }; |
74 static const int atrc_fl2bps[ATRC_FLAVORS] = { | |
75 8269, 11714, 13092, 16538, 18260, 22050, 33075, 44100 }; | |
76 static const int cook_fl2bps[COOK_FLAVORS] = { | |
77 1000, 1378, 2024, 2584, 4005, 5513, 8010, 4005, 750, 2498, | |
78 4048, 5513, 8010, 11973, 8010, 2584, 4005, 2067, 2584, 2584, | |
79 4005, 4005, 5513, 5513, 8010, 12059, 1550, 8010, 12059, 5513, | |
80 12016, 16408, 22911, 33506 | |
81 }; | |
82 | |
83 typedef struct { | |
84 uint32_t order, type, scope; | |
85 uint32_t comp_algo; | |
86 uint8_t *comp_settings; | |
87 int comp_settings_len; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
88 } mkv_content_encoding_t; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
89 |
31177 | 90 typedef struct mkv_track { |
91 int tnum; | |
92 char *name; | |
93 | |
94 char *codec_id; | |
95 int ms_compat; | |
96 char *language; | |
97 | |
98 int type; | |
99 | |
100 uint32_t v_width, v_height, v_dwidth, v_dheight; | |
101 float v_frate; | |
102 | |
103 uint32_t a_formattag; | |
104 uint32_t a_channels, a_bps; | |
105 float a_sfreq; | |
106 | |
107 float default_duration; | |
108 | |
109 int default_track; | |
110 | |
111 void *private_data; | |
112 unsigned int private_size; | |
113 | |
114 /* stuff for realmedia */ | |
115 int realmedia; | |
116 int64_t rv_kf_base; | |
117 int rv_kf_pts; | |
118 float rv_pts; /* previous video timestamp */ | |
119 float ra_pts; /* previous audio timestamp */ | |
120 | |
121 /** realaudio descrambling */ | |
122 int sub_packet_size; ///< sub packet size, per stream | |
123 int sub_packet_h; ///< number of coded frames per block | |
124 int coded_framesize; ///< coded frame size, per stream | |
125 int audiopk_size; ///< audio packet size | |
126 unsigned char *audio_buf; ///< place to store reordered audio data | |
127 float *audio_timestamp; ///< timestamp for each audio packet | |
128 int sub_packet_cnt; ///< number of subpacket already received | |
129 int audio_filepos; ///< file position of first audio packet in block | |
130 | |
131 /* stuff for quicktime */ | |
132 int fix_i_bps; | |
133 float qt_last_a_pts; | |
134 | |
135 int subtitle_type; | |
136 | |
137 /* The timecodes of video frames might have to be reordered if they're | |
138 in display order (the timecodes, not the frames themselves!). In this | |
139 case demux packets have to be cached with the help of these variables. */ | |
140 int reorder_timecodes; | |
141 demux_packet_t **cached_dps; | |
142 int num_cached_dps, num_allocated_dps; | |
143 float max_pts; | |
144 | |
145 /* generic content encoding support */ | |
146 mkv_content_encoding_t *encodings; | |
147 int num_encodings; | |
148 | |
149 /* For VobSubs and SSA/ASS */ | |
150 sh_sub_t *sh_sub; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
151 } mkv_track_t; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
152 |
31177 | 153 typedef struct mkv_index { |
154 int tnum; | |
155 uint64_t timecode, filepos; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
156 } mkv_index_t; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
157 |
31177 | 158 typedef struct mkv_demuxer { |
159 off_t segment_start; | |
160 | |
161 float duration, last_pts; | |
162 uint64_t last_filepos; | |
163 | |
164 mkv_track_t **tracks; | |
165 int num_tracks; | |
166 | |
167 uint64_t tc_scale, cluster_tc, first_tc; | |
168 int has_first_tc; | |
169 | |
170 uint64_t cluster_size; | |
171 uint64_t blockgroup_size; | |
172 | |
173 mkv_index_t *indexes; | |
174 int num_indexes; | |
175 | |
176 off_t *parsed_cues; | |
177 int parsed_cues_num; | |
178 off_t *parsed_seekhead; | |
179 int parsed_seekhead_num; | |
180 | |
181 uint64_t *cluster_positions; | |
182 int num_cluster_pos; | |
183 | |
184 int64_t skip_to_timecode; | |
185 int v_skip_to_keyframe, a_skip_to_keyframe; | |
186 | |
187 int64_t stop_timecode; | |
188 | |
189 int last_aid; | |
190 int audio_tracks[MAX_A_STREAMS]; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
191 } mkv_demuxer_t; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
192 |
23386
585d2136f018
Get rid of __attribute__((__packed__)) in Matroska demuxer
reimar
parents:
23310
diff
changeset
|
193 #define REALHEADER_SIZE 16 |
585d2136f018
Get rid of __attribute__((__packed__)) in Matroska demuxer
reimar
parents:
23310
diff
changeset
|
194 #define RVPROPERTIES_SIZE 34 |
585d2136f018
Get rid of __attribute__((__packed__)) in Matroska demuxer
reimar
parents:
23310
diff
changeset
|
195 #define RAPROPERTIES4_SIZE 56 |
585d2136f018
Get rid of __attribute__((__packed__)) in Matroska demuxer
reimar
parents:
23310
diff
changeset
|
196 #define RAPROPERTIES5_SIZE 70 |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
197 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
198 /* for e.g. "-slang ger" */ |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
199 extern char *dvdsub_lang; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
200 extern char *audio_lang; |
13501
a5004eb92a79
fix sub_select fiasco with global sub numbering. now multiple sub sources can be managed in essentially one list.
joey
parents:
13424
diff
changeset
|
201 extern int dvdsub_id; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
202 |
20140
1861d40674ca
Simplify (by using realloc with NULL parameter) and refactor code to grow
reimar
parents:
20137
diff
changeset
|
203 /** |
1861d40674ca
Simplify (by using realloc with NULL parameter) and refactor code to grow
reimar
parents:
20137
diff
changeset
|
204 * \brief ensures there is space for at least one additional element |
29691
183ea012e25b
Change grow_array argument from void ** to void *, this avoids a aliasing
reimar
parents:
29490
diff
changeset
|
205 * \param arrayp array to grow |
20140
1861d40674ca
Simplify (by using realloc with NULL parameter) and refactor code to grow
reimar
parents:
20137
diff
changeset
|
206 * \param nelem current number of elements in array |
1861d40674ca
Simplify (by using realloc with NULL parameter) and refactor code to grow
reimar
parents:
20137
diff
changeset
|
207 * \param elsize size of one array element |
1861d40674ca
Simplify (by using realloc with NULL parameter) and refactor code to grow
reimar
parents:
20137
diff
changeset
|
208 */ |
31177 | 209 static void av_noinline grow_array(void *arrayp, int nelem, size_t elsize) |
210 { | |
211 void **array = arrayp; | |
212 void *oldp = *array; | |
213 if (nelem & 31) | |
214 return; | |
215 if (nelem > UINT_MAX / elsize - 32) | |
216 *array = NULL; | |
217 else | |
218 *array = realloc(*array, (nelem + 32) * elsize); | |
219 if (!*array) | |
220 free(oldp); | |
20140
1861d40674ca
Simplify (by using realloc with NULL parameter) and refactor code to grow
reimar
parents:
20137
diff
changeset
|
221 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
222 |
31177 | 223 static mkv_track_t *demux_mkv_find_track_by_num(mkv_demuxer_t *d, int n, |
224 int type) | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
225 { |
31177 | 226 int i, id; |
227 | |
228 for (i = 0, id = 0; i < d->num_tracks; i++) | |
229 if (d->tracks[i] != NULL && d->tracks[i]->type == type) | |
230 if (id++ == n) | |
231 return d->tracks[i]; | |
232 | |
233 return NULL; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
234 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
235 |
31177 | 236 static void add_cluster_position(mkv_demuxer_t *mkv_d, uint64_t position) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
237 { |
31177 | 238 int i = mkv_d->num_cluster_pos; |
239 | |
240 while (i--) | |
241 if (mkv_d->cluster_positions[i] == position) | |
242 return; | |
243 | |
244 grow_array(&mkv_d->cluster_positions, mkv_d->num_cluster_pos, | |
245 sizeof(uint64_t)); | |
246 if (!mkv_d->cluster_positions) { | |
247 mkv_d->num_cluster_pos = 0; | |
248 return; | |
249 } | |
250 mkv_d->cluster_positions[mkv_d->num_cluster_pos++] = position; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
251 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
252 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
253 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
254 #define AAC_SYNC_EXTENSION_TYPE 0x02b7 |
31177 | 255 static int aac_get_sample_rate_index(uint32_t sample_rate) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
256 { |
31177 | 257 static const int srates[] = { |
258 92017, 75132, 55426, 46009, 37566, 27713, | |
259 23004, 18783, 13856, 11502, 9391, 0 | |
260 }; | |
261 int i = 0; | |
262 while (sample_rate < srates[i]) | |
263 i++; | |
264 return i; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
265 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
266 |
14054 | 267 /** \brief Free cached demux packets |
268 * | |
269 * Reordering the timecodes requires caching of demux packets. This function | |
270 * frees all these cached packets and the memory for the cached pointers | |
271 * itself. | |
272 * | |
273 * \param demuxer The demuxer for which the cache is to be freed. | |
274 */ | |
31177 | 275 static void free_cached_dps(demuxer_t *demuxer) |
14054 | 276 { |
31177 | 277 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; |
278 mkv_track_t *track; | |
279 int i, k; | |
280 | |
281 for (k = 0; k < mkv_d->num_tracks; k++) { | |
282 track = mkv_d->tracks[k]; | |
283 for (i = 0; i < track->num_cached_dps; i++) | |
284 free_demux_packet(track->cached_dps[i]); | |
285 free(track->cached_dps); | |
286 track->cached_dps = NULL; | |
287 track->num_cached_dps = 0; | |
288 track->num_allocated_dps = 0; | |
289 track->max_pts = 0; | |
14054 | 290 } |
291 } | |
292 | |
31177 | 293 static int demux_mkv_decode(mkv_track_t *track, uint8_t *src, |
294 uint8_t **dest, uint32_t *size, uint32_t type) | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
295 { |
31177 | 296 int i, result; |
297 int modified = 0; | |
298 | |
299 *dest = src; | |
300 if (track->num_encodings <= 0) | |
301 return 0; | |
302 | |
303 for (i = 0; i < track->num_encodings; i++) { | |
304 if (!(track->encodings[i].scope & type)) | |
305 continue; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
306 |
28440 | 307 #if CONFIG_ZLIB |
31177 | 308 if (track->encodings[i].comp_algo == 0) { |
309 /* zlib encoded track */ | |
310 z_stream zstream; | |
311 | |
312 zstream.zalloc = (alloc_func) 0; | |
313 zstream.zfree = (free_func) 0; | |
314 zstream.opaque = (voidpf) 0; | |
315 if (inflateInit(&zstream) != Z_OK) { | |
316 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
317 MSGTR_MPDEMUX_MKV_ZlibInitializationFailed); | |
318 return modified; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
319 } |
31177 | 320 zstream.next_in = (Bytef *) src; |
321 zstream.avail_in = *size; | |
322 | |
323 modified = 1; | |
324 *dest = NULL; | |
325 zstream.avail_out = *size; | |
326 do { | |
327 *size += 4000; | |
328 *dest = realloc(*dest, *size); | |
329 zstream.next_out = (Bytef *) (*dest + zstream.total_out); | |
330 result = inflate(&zstream, Z_NO_FLUSH); | |
331 if (result != Z_OK && result != Z_STREAM_END) { | |
332 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
333 MSGTR_MPDEMUX_MKV_ZlibDecompressionFailed); | |
334 free(*dest); | |
335 *dest = NULL; | |
336 inflateEnd(&zstream); | |
337 return modified; | |
338 } | |
339 zstream.avail_out += 4000; | |
340 } while (zstream.avail_out == 4000 && zstream.avail_in != 0 | |
341 && result != Z_STREAM_END); | |
342 | |
343 *size = zstream.total_out; | |
344 inflateEnd(&zstream); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
345 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
346 #endif |
31177 | 347 if (track->encodings[i].comp_algo == 2) { |
348 /* lzo encoded track */ | |
349 int dstlen = *size * 3; | |
350 | |
351 *dest = NULL; | |
352 while (1) { | |
353 int srclen = *size; | |
354 if (dstlen > SIZE_MAX - AV_LZO_OUTPUT_PADDING) | |
355 goto lzo_fail; | |
356 *dest = realloc(*dest, dstlen + AV_LZO_OUTPUT_PADDING); | |
357 result = av_lzo1x_decode(*dest, &dstlen, src, &srclen); | |
358 if (result == 0) | |
359 break; | |
360 if (!(result & AV_LZO_OUTPUT_FULL)) { | |
361 lzo_fail: | |
362 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
363 MSGTR_MPDEMUX_MKV_LzoDecompressionFailed); | |
364 free(*dest); | |
365 *dest = NULL; | |
366 return modified; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
367 } |
31177 | 368 mp_msg(MSGT_DEMUX, MSGL_DBG2, |
369 "[mkv] lzo decompression buffer too small.\n"); | |
370 dstlen *= 2; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
371 } |
31177 | 372 *size = dstlen; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
373 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
374 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
375 |
31177 | 376 return modified; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
377 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
378 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
379 |
31177 | 380 static int demux_mkv_read_info(demuxer_t *demuxer) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
381 { |
31177 | 382 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; |
383 stream_t *s = demuxer->stream; | |
384 uint64_t length, l; | |
385 int il; | |
386 uint64_t tc_scale = 1000000; | |
387 long double duration = 0.; | |
388 | |
389 length = ebml_read_length(s, NULL); | |
390 while (length > 0) { | |
391 switch (ebml_read_id(s, &il)) { | |
392 case MATROSKA_ID_TIMECODESCALE: | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
393 { |
31177 | 394 uint64_t num = ebml_read_uint(s, &l); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
395 if (num == EBML_UINT_INVALID) |
31177 | 396 return 1; |
19809
99d375aab4db
Fix movie duration calculation in case when TimecodeScale element comes after
eugeni
parents:
19645
diff
changeset
|
397 tc_scale = num; |
31177 | 398 mp_msg(MSGT_DEMUX, MSGL_V, |
399 "[mkv] | + timecode scale: %" PRIu64 "\n", tc_scale); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
400 break; |
31177 | 401 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
402 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
403 case MATROSKA_ID_DURATION: |
31177 | 404 { |
405 long double num = ebml_read_float(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
406 if (num == EBML_FLOAT_INVALID) |
31177 | 407 return 1; |
19809
99d375aab4db
Fix movie duration calculation in case when TimecodeScale element comes after
eugeni
parents:
19645
diff
changeset
|
408 duration = num; |
31177 | 409 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + duration: %.3Lfs\n", |
410 duration * tc_scale / 1000000000.0); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
411 break; |
31177 | 412 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
413 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
414 default: |
31177 | 415 ebml_read_skip(s, &l); |
416 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
417 } |
31177 | 418 length -= l + il; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
419 } |
31177 | 420 mkv_d->tc_scale = tc_scale; |
421 mkv_d->duration = duration * tc_scale / 1000000000.0; | |
422 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
423 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
424 |
20133 | 425 /** |
426 * \brief free array of kv_content_encoding_t | |
427 * \param encodings pointer to array | |
428 * \param numencodings number of encodings in array | |
429 */ | |
31177 | 430 static void demux_mkv_free_encodings(mkv_content_encoding_t *encodings, |
431 int numencodings) | |
20133 | 432 { |
31177 | 433 while (numencodings-- > 0) |
434 free(encodings[numencodings].comp_settings); | |
435 free(encodings); | |
20133 | 436 } |
437 | |
31177 | 438 static int demux_mkv_read_trackencodings(demuxer_t *demuxer, |
439 mkv_track_t *track) | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
440 { |
31177 | 441 stream_t *s = demuxer->stream; |
442 mkv_content_encoding_t *ce, e; | |
443 uint64_t len, length, l; | |
444 int il, n; | |
445 | |
446 ce = malloc(sizeof(*ce)); | |
447 n = 0; | |
448 | |
449 len = length = ebml_read_length(s, &il); | |
450 len += il; | |
451 while (length > 0) { | |
452 switch (ebml_read_id(s, &il)) { | |
453 case MATROSKA_ID_CONTENTENCODING: | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
454 { |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
455 uint64_t len; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
456 int i; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
457 |
31177 | 458 memset(&e, 0, sizeof(e)); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
459 e.scope = 1; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
460 |
31177 | 461 len = ebml_read_length(s, &i); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
462 l = len + i; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
463 |
31177 | 464 while (len > 0) { |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
465 uint64_t num, l; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
466 int il; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
467 |
31177 | 468 switch (ebml_read_id(s, &il)) { |
469 case MATROSKA_ID_CONTENTENCODINGORDER: | |
470 num = ebml_read_uint(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
471 if (num == EBML_UINT_INVALID) |
31177 | 472 goto err_out; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
473 e.order = num; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
474 break; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
475 |
31177 | 476 case MATROSKA_ID_CONTENTENCODINGSCOPE: |
477 num = ebml_read_uint(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
478 if (num == EBML_UINT_INVALID) |
31177 | 479 goto err_out; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
480 e.scope = num; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
481 break; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
482 |
31177 | 483 case MATROSKA_ID_CONTENTENCODINGTYPE: |
484 num = ebml_read_uint(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
485 if (num == EBML_UINT_INVALID) |
31177 | 486 goto err_out; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
487 e.type = num; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
488 break; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
489 |
31177 | 490 case MATROSKA_ID_CONTENTCOMPRESSION: |
491 { | |
492 uint64_t le; | |
493 | |
494 le = ebml_read_length(s, &i); | |
495 l = le + i; | |
496 | |
497 while (le > 0) { | |
498 uint64_t l; | |
499 int il; | |
500 | |
501 switch (ebml_read_id(s, &il)) { | |
502 case MATROSKA_ID_CONTENTCOMPALGO: | |
503 num = ebml_read_uint(s, &l); | |
504 if (num == EBML_UINT_INVALID) | |
20133 | 505 goto err_out; |
31177 | 506 e.comp_algo = num; |
507 break; | |
508 | |
509 case MATROSKA_ID_CONTENTCOMPSETTINGS: | |
510 l = ebml_read_length(s, &i); | |
511 e.comp_settings = malloc(l); | |
512 stream_read(s, e.comp_settings, l); | |
513 e.comp_settings_len = l; | |
514 l += i; | |
515 break; | |
516 | |
517 default: | |
518 ebml_read_skip(s, &l); | |
519 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
520 } |
31177 | 521 le -= l + il; |
522 } | |
523 | |
524 if (e.type == 1) { | |
525 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
526 MSGTR_MPDEMUX_MKV_TrackEncrypted, | |
527 track->tnum); | |
528 } else if (e.type != 0) { | |
529 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
530 MSGTR_MPDEMUX_MKV_UnknownContentEncoding, | |
531 track->tnum); | |
532 } | |
533 | |
534 if (e.comp_algo != 0 && e.comp_algo != 2) { | |
535 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
536 MSGTR_MPDEMUX_MKV_UnknownCompression, | |
537 track->tnum, e.comp_algo); | |
538 } | |
28440 | 539 #if !CONFIG_ZLIB |
31177 | 540 else if (e.comp_algo == 0) { |
541 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
542 MSGTR_MPDEMUX_MKV_ZlibCompressionUnsupported, | |
543 track->tnum); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
544 } |
31177 | 545 #endif |
546 | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
547 break; |
31177 | 548 } |
549 | |
550 default: | |
551 ebml_read_skip(s, &l); | |
552 break; | |
553 } | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
554 len -= l + il; |
31177 | 555 } |
556 for (i = 0; i < n; i++) | |
557 if (e.order <= ce[i].order) | |
558 break; | |
559 ce = realloc(ce, (n + 1) * sizeof(*ce)); | |
560 memmove(ce + i + 1, ce + i, (n - i) * sizeof(*ce)); | |
561 memcpy(ce + i, &e, sizeof(e)); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
562 n++; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
563 break; |
31177 | 564 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
565 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
566 default: |
31177 | 567 ebml_read_skip(s, &l); |
568 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
569 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
570 |
31177 | 571 length -= l + il; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
572 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
573 |
31177 | 574 track->encodings = ce; |
575 track->num_encodings = n; | |
576 return len; | |
20133 | 577 |
578 err_out: | |
31177 | 579 demux_mkv_free_encodings(ce, n); |
580 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
581 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
582 |
31177 | 583 static int demux_mkv_read_trackaudio(demuxer_t *demuxer, mkv_track_t *track) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
584 { |
31177 | 585 stream_t *s = demuxer->stream; |
586 uint64_t len, length, l; | |
587 int il; | |
588 | |
589 track->a_sfreq = 8000.0; | |
590 track->a_channels = 1; | |
591 | |
592 len = length = ebml_read_length(s, &il); | |
593 len += il; | |
594 while (length > 0) { | |
595 switch (ebml_read_id(s, &il)) { | |
596 case MATROSKA_ID_AUDIOSAMPLINGFREQ: | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
597 { |
31177 | 598 long double num = ebml_read_float(s, &l); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
599 if (num == EBML_FLOAT_INVALID) |
31177 | 600 return 0; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
601 track->a_sfreq = num; |
31177 | 602 mp_msg(MSGT_DEMUX, MSGL_V, |
603 "[mkv] | + Sampling frequency: %f\n", track->a_sfreq); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
604 break; |
31177 | 605 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
606 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
607 case MATROSKA_ID_AUDIOBITDEPTH: |
31177 | 608 { |
609 uint64_t num = ebml_read_uint(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
610 if (num == EBML_UINT_INVALID) |
31177 | 611 return 0; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
612 track->a_bps = num; |
31177 | 613 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Bit depth: %u\n", |
614 track->a_bps); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
615 break; |
31177 | 616 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
617 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
618 case MATROSKA_ID_AUDIOCHANNELS: |
31177 | 619 { |
620 uint64_t num = ebml_read_uint(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
621 if (num == EBML_UINT_INVALID) |
31177 | 622 return 0; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
623 track->a_channels = num; |
31177 | 624 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Channels: %u\n", |
625 track->a_channels); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
626 break; |
31177 | 627 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
628 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
629 default: |
31177 | 630 ebml_read_skip(s, &l); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
631 break; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
632 } |
31177 | 633 length -= l + il; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
634 } |
31177 | 635 return len; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
636 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
637 |
31177 | 638 static int demux_mkv_read_trackvideo(demuxer_t *demuxer, mkv_track_t *track) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
639 { |
31177 | 640 stream_t *s = demuxer->stream; |
641 uint64_t len, length, l; | |
642 int il; | |
643 | |
644 len = length = ebml_read_length(s, &il); | |
645 len += il; | |
646 while (length > 0) { | |
647 switch (ebml_read_id(s, &il)) { | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
648 case MATROSKA_ID_VIDEOFRAMERATE: |
31177 | 649 { |
650 long double num = ebml_read_float(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
651 if (num == EBML_FLOAT_INVALID) |
31177 | 652 return 0; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
653 track->v_frate = num; |
31177 | 654 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Frame rate: %f\n", |
655 track->v_frate); | |
14058
4dee22ed8608
Make use of the default duration for one frame if it is present in the file. This produces much smoother timecodes for laced audio frames. And I REALLY don't know why I missed that before...
mosu
parents:
14054
diff
changeset
|
656 if (track->v_frate > 0) |
31177 | 657 track->default_duration = 1 / track->v_frate; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
658 break; |
31177 | 659 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
660 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
661 case MATROSKA_ID_VIDEODISPLAYWIDTH: |
31177 | 662 { |
663 uint64_t num = ebml_read_uint(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
664 if (num == EBML_UINT_INVALID) |
31177 | 665 return 0; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
666 track->v_dwidth = num; |
31177 | 667 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Display width: %u\n", |
668 track->v_dwidth); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
669 break; |
31177 | 670 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
671 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
672 case MATROSKA_ID_VIDEODISPLAYHEIGHT: |
31177 | 673 { |
674 uint64_t num = ebml_read_uint(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
675 if (num == EBML_UINT_INVALID) |
31177 | 676 return 0; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
677 track->v_dheight = num; |
31177 | 678 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Display height: %u\n", |
679 track->v_dheight); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
680 break; |
31177 | 681 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
682 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
683 case MATROSKA_ID_VIDEOPIXELWIDTH: |
31177 | 684 { |
685 uint64_t num = ebml_read_uint(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
686 if (num == EBML_UINT_INVALID) |
31177 | 687 return 0; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
688 track->v_width = num; |
31177 | 689 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Pixel width: %u\n", |
690 track->v_width); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
691 break; |
31177 | 692 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
693 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
694 case MATROSKA_ID_VIDEOPIXELHEIGHT: |
31177 | 695 { |
696 uint64_t num = ebml_read_uint(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
697 if (num == EBML_UINT_INVALID) |
31177 | 698 return 0; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
699 track->v_height = num; |
31177 | 700 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Pixel height: %u\n", |
701 track->v_height); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
702 break; |
31177 | 703 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
704 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
705 default: |
31177 | 706 ebml_read_skip(s, &l); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
707 break; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
708 } |
31177 | 709 length -= l + il; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
710 } |
31177 | 711 return len; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
712 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
713 |
20135
7079d029d27c
Free track data if error occurs while reading trackentry
reimar
parents:
20133
diff
changeset
|
714 /** |
7079d029d27c
Free track data if error occurs while reading trackentry
reimar
parents:
20133
diff
changeset
|
715 * \brief free any data associated with given track |
7079d029d27c
Free track data if error occurs while reading trackentry
reimar
parents:
20133
diff
changeset
|
716 * \param track track of which to free data |
7079d029d27c
Free track data if error occurs while reading trackentry
reimar
parents:
20133
diff
changeset
|
717 */ |
31177 | 718 static void demux_mkv_free_trackentry(mkv_track_t *track) |
719 { | |
720 free(track->name); | |
721 free(track->codec_id); | |
722 free(track->language); | |
723 free(track->private_data); | |
724 free(track->audio_buf); | |
725 free(track->audio_timestamp); | |
726 demux_mkv_free_encodings(track->encodings, track->num_encodings); | |
727 free(track); | |
20135
7079d029d27c
Free track data if error occurs while reading trackentry
reimar
parents:
20133
diff
changeset
|
728 } |
7079d029d27c
Free track data if error occurs while reading trackentry
reimar
parents:
20133
diff
changeset
|
729 |
31177 | 730 static int demux_mkv_read_trackentry(demuxer_t *demuxer) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
731 { |
31177 | 732 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; |
733 stream_t *s = demuxer->stream; | |
734 mkv_track_t *track; | |
735 uint64_t len, length, l; | |
736 int il; | |
737 | |
738 track = calloc(1, sizeof(*track)); | |
739 /* set default values */ | |
740 track->default_track = 1; | |
741 track->name = 0; | |
742 track->language = strdup("eng"); | |
743 | |
744 len = length = ebml_read_length(s, &il); | |
745 len += il; | |
746 while (length > 0) { | |
747 switch (ebml_read_id(s, &il)) { | |
748 case MATROSKA_ID_TRACKNUMBER: | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
749 { |
31177 | 750 uint64_t num = ebml_read_uint(s, &l); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
751 if (num == EBML_UINT_INVALID) |
31177 | 752 goto err_out; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
753 track->tnum = num; |
31177 | 754 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Track number: %u\n", |
755 track->tnum); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
756 break; |
31177 | 757 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
758 |
19640 | 759 case MATROSKA_ID_TRACKNAME: |
31177 | 760 track->name = ebml_read_utf8(s, &l); |
19640 | 761 if (track->name == NULL) |
31177 | 762 goto err_out; |
763 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Name: %s\n", | |
764 track->name); | |
19640 | 765 break; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
766 |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
767 case MATROSKA_ID_TRACKTYPE: |
31177 | 768 { |
769 uint64_t num = ebml_read_uint(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
770 if (num == EBML_UINT_INVALID) |
31177 | 771 return 0; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
772 track->type = num; |
31177 | 773 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Track type: "); |
774 switch (track->type) { | |
775 case MATROSKA_TRACK_AUDIO: | |
776 mp_msg(MSGT_DEMUX, MSGL_V, "Audio\n"); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
777 break; |
31177 | 778 case MATROSKA_TRACK_VIDEO: |
779 mp_msg(MSGT_DEMUX, MSGL_V, "Video\n"); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
780 break; |
31177 | 781 case MATROSKA_TRACK_SUBTITLE: |
782 mp_msg(MSGT_DEMUX, MSGL_V, "Subtitle\n"); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
783 break; |
31177 | 784 default: |
785 mp_msg(MSGT_DEMUX, MSGL_V, "unknown\n"); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
786 break; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
787 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
788 break; |
31177 | 789 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
790 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
791 case MATROSKA_ID_TRACKAUDIO: |
31177 | 792 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Audio track\n"); |
793 l = demux_mkv_read_trackaudio(demuxer, track); | |
794 if (l == 0) | |
795 goto err_out; | |
796 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
797 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
798 case MATROSKA_ID_TRACKVIDEO: |
31177 | 799 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Video track\n"); |
800 l = demux_mkv_read_trackvideo(demuxer, track); | |
801 if (l == 0) | |
802 goto err_out; | |
803 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
804 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
805 case MATROSKA_ID_CODECID: |
31177 | 806 track->codec_id = ebml_read_ascii(s, &l); |
807 if (track->codec_id == NULL) | |
808 goto err_out; | |
809 if (!strcmp(track->codec_id, MKV_V_MSCOMP) | |
810 || !strcmp(track->codec_id, MKV_A_ACM)) | |
811 track->ms_compat = 1; | |
812 else if (!strcmp(track->codec_id, MKV_S_VOBSUB)) | |
813 track->subtitle_type = MATROSKA_SUBTYPE_VOBSUB; | |
814 else if (!strcmp(track->codec_id, MKV_S_TEXTSSA) | |
815 || !strcmp(track->codec_id, MKV_S_TEXTASS) | |
816 || !strcmp(track->codec_id, MKV_S_SSA) | |
817 || !strcmp(track->codec_id, MKV_S_ASS)) { | |
818 track->subtitle_type = MATROSKA_SUBTYPE_SSA; | |
819 } else if (!strcmp(track->codec_id, MKV_S_TEXTASCII)) | |
820 track->subtitle_type = MATROSKA_SUBTYPE_TEXT; | |
821 if (!strcmp(track->codec_id, MKV_S_TEXTUTF8)) { | |
822 track->subtitle_type = MATROSKA_SUBTYPE_TEXT; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
823 } |
31177 | 824 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Codec ID: %s\n", |
825 track->codec_id); | |
826 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
827 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
828 case MATROSKA_ID_CODECPRIVATE: |
31177 | 829 { |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
830 int x; |
31177 | 831 uint64_t num = ebml_read_length(s, &x); |
832 // audit: cheap guard against overflows later.. | |
833 if (num > SIZE_MAX - 1000) | |
834 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
835 l = x + num; |
31177 | 836 track->private_data = malloc(num + AV_LZO_INPUT_PADDING); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
837 if (stream_read(s, track->private_data, num) != (int) num) |
31177 | 838 goto err_out; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
839 track->private_size = num; |
31177 | 840 mp_msg(MSGT_DEMUX, MSGL_V, |
841 "[mkv] | + CodecPrivate, length " "%u\n", | |
842 track->private_size); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
843 break; |
31177 | 844 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
845 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
846 case MATROSKA_ID_TRACKLANGUAGE: |
31177 | 847 free(track->language); |
848 track->language = ebml_read_utf8(s, &l); | |
849 if (track->language == NULL) | |
850 goto err_out; | |
851 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Language: %s\n", | |
852 track->language); | |
853 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
854 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
855 case MATROSKA_ID_TRACKFLAGDEFAULT: |
31177 | 856 { |
857 uint64_t num = ebml_read_uint(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
858 if (num == EBML_UINT_INVALID) |
31177 | 859 goto err_out; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
860 track->default_track = num; |
31177 | 861 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + Default flag: %u\n", |
862 track->default_track); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
863 break; |
31177 | 864 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
865 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
866 case MATROSKA_ID_TRACKDEFAULTDURATION: |
31177 | 867 { |
868 uint64_t num = ebml_read_uint(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
869 if (num == EBML_UINT_INVALID) |
31177 | 870 goto err_out; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
871 if (num == 0) |
31177 | 872 mp_msg(MSGT_DEMUX, MSGL_V, |
873 "[mkv] | + Default duration: 0"); | |
874 else { | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
875 track->v_frate = 1000000000.0 / num; |
14058
4dee22ed8608
Make use of the default duration for one frame if it is present in the file. This produces much smoother timecodes for laced audio frames. And I REALLY don't know why I missed that before...
mosu
parents:
14054
diff
changeset
|
876 track->default_duration = num / 1000000000.0; |
31177 | 877 mp_msg(MSGT_DEMUX, MSGL_V, |
878 "[mkv] | + Default duration: " | |
879 "%.3fms ( = %.3f fps)\n", num / 1000000.0, | |
880 track->v_frate); | |
881 } | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
882 break; |
31177 | 883 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
884 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
885 case MATROSKA_ID_TRACKENCODINGS: |
31177 | 886 l = demux_mkv_read_trackencodings(demuxer, track); |
887 if (l == 0) | |
888 goto err_out; | |
889 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
890 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
891 default: |
31177 | 892 ebml_read_skip(s, &l); |
893 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
894 } |
31177 | 895 length -= l + il; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
896 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
897 |
31177 | 898 mkv_d->tracks[mkv_d->num_tracks++] = track; |
899 return len; | |
20135
7079d029d27c
Free track data if error occurs while reading trackentry
reimar
parents:
20133
diff
changeset
|
900 |
7079d029d27c
Free track data if error occurs while reading trackentry
reimar
parents:
20133
diff
changeset
|
901 err_out: |
31177 | 902 demux_mkv_free_trackentry(track); |
903 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
904 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
905 |
31177 | 906 static int demux_mkv_read_tracks(demuxer_t *demuxer) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
907 { |
31177 | 908 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; |
909 stream_t *s = demuxer->stream; | |
910 uint64_t length, l; | |
911 int il; | |
912 | |
913 mkv_d->tracks = malloc(sizeof(*mkv_d->tracks)); | |
914 mkv_d->num_tracks = 0; | |
915 | |
916 length = ebml_read_length(s, NULL); | |
917 while (length > 0) { | |
918 switch (ebml_read_id(s, &il)) { | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
919 case MATROSKA_ID_TRACKENTRY: |
31177 | 920 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + a track...\n"); |
921 mkv_d->tracks = realloc(mkv_d->tracks, (mkv_d->num_tracks + 1) | |
922 * sizeof(*mkv_d->tracks)); | |
923 l = demux_mkv_read_trackentry(demuxer); | |
924 if (l == 0) | |
925 return 1; | |
926 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
927 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
928 default: |
31177 | 929 ebml_read_skip(s, &l); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
930 break; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
931 } |
31177 | 932 length -= l + il; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
933 } |
31177 | 934 return 0; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
935 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
936 |
31177 | 937 static int demux_mkv_read_cues(demuxer_t *demuxer) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
938 { |
31177 | 939 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; |
940 stream_t *s = demuxer->stream; | |
941 uint64_t length, l, time, track, pos; | |
942 off_t off; | |
943 int i, il; | |
944 | |
945 if (index_mode == 0) { | |
946 ebml_read_skip(s, NULL); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
947 return 0; |
31177 | 948 } |
949 off = stream_tell(s); | |
950 for (i = 0; i < mkv_d->parsed_cues_num; i++) | |
951 if (mkv_d->parsed_cues[i] == off) { | |
952 ebml_read_skip(s, NULL); | |
953 return 0; | |
954 } | |
955 mkv_d->parsed_cues = realloc(mkv_d->parsed_cues, | |
956 (mkv_d->parsed_cues_num + 1) * sizeof(off_t)); | |
957 mkv_d->parsed_cues[mkv_d->parsed_cues_num++] = off; | |
958 | |
959 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing cues ] -----------\n"); | |
960 length = ebml_read_length(s, NULL); | |
961 | |
962 while (length > 0) { | |
963 time = track = pos = EBML_UINT_INVALID; | |
964 | |
965 switch (ebml_read_id(s, &il)) { | |
966 case MATROSKA_ID_POINTENTRY: | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
967 { |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
968 uint64_t len; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
969 |
31177 | 970 len = ebml_read_length(s, &i); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
971 l = len + i; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
972 |
31177 | 973 while (len > 0) { |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
974 uint64_t l; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
975 int il; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
976 |
31177 | 977 switch (ebml_read_id(s, &il)) { |
978 case MATROSKA_ID_CUETIME: | |
979 time = ebml_read_uint(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
980 break; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
981 |
31177 | 982 case MATROSKA_ID_CUETRACKPOSITION: |
983 { | |
984 uint64_t le; | |
985 | |
986 le = ebml_read_length(s, &i); | |
987 l = le + i; | |
988 | |
989 while (le > 0) { | |
990 uint64_t l; | |
991 int il; | |
992 | |
993 switch (ebml_read_id(s, &il)) { | |
994 case MATROSKA_ID_CUETRACK: | |
995 track = ebml_read_uint(s, &l); | |
996 break; | |
997 | |
998 case MATROSKA_ID_CUECLUSTERPOSITION: | |
999 pos = ebml_read_uint(s, &l); | |
1000 break; | |
1001 | |
1002 default: | |
1003 ebml_read_skip(s, &l); | |
1004 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1005 } |
31177 | 1006 le -= l + il; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1007 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1008 break; |
31177 | 1009 } |
1010 | |
1011 default: | |
1012 ebml_read_skip(s, &l); | |
1013 break; | |
1014 } | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1015 len -= l + il; |
31177 | 1016 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1017 break; |
31177 | 1018 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1019 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1020 default: |
31177 | 1021 ebml_read_skip(s, &l); |
1022 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1023 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1024 |
31177 | 1025 length -= l + il; |
1026 | |
1027 if (time != EBML_UINT_INVALID && track != EBML_UINT_INVALID | |
1028 && pos != EBML_UINT_INVALID) { | |
1029 grow_array(&mkv_d->indexes, mkv_d->num_indexes, | |
1030 sizeof(mkv_index_t)); | |
1031 if (!mkv_d->indexes) { | |
1032 mkv_d->num_indexes = 0; | |
1033 break; | |
1034 } | |
1035 mkv_d->indexes[mkv_d->num_indexes].tnum = track; | |
1036 mkv_d->indexes[mkv_d->num_indexes].timecode = time; | |
1037 mkv_d->indexes[mkv_d->num_indexes].filepos = mkv_d->segment_start | |
1038 + pos; | |
1039 mp_msg(MSGT_DEMUX, MSGL_DBG2, | |
1040 "[mkv] |+ found cue point " "for track %" PRIu64 | |
1041 ": timecode %" PRIu64 ", filepos: %" PRIu64 "\n", track, | |
1042 time, mkv_d->segment_start + pos); | |
1043 mkv_d->num_indexes++; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1044 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1045 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1046 |
31177 | 1047 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] \\---- [ parsing cues ] -----------\n"); |
1048 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1049 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1050 |
31177 | 1051 static int demux_mkv_read_chapters(demuxer_t *demuxer) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1052 { |
31177 | 1053 stream_t *s = demuxer->stream; |
1054 uint64_t length, l; | |
1055 int il; | |
1056 | |
1057 if (demuxer->chapters) { | |
1058 ebml_read_skip(s, NULL); | |
1059 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1060 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1061 |
31177 | 1062 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] /---- [ parsing chapters ] ---------\n"); |
1063 length = ebml_read_length(s, NULL); | |
1064 | |
1065 while (length > 0) { | |
1066 switch (ebml_read_id(s, &il)) { | |
1067 case MATROSKA_ID_EDITIONENTRY: | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1068 { |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1069 uint64_t len; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1070 int i; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1071 |
31177 | 1072 len = ebml_read_length(s, &i); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1073 l = len + i; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1074 |
31177 | 1075 while (len > 0) { |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1076 uint64_t l; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1077 int il; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1078 |
31177 | 1079 switch (ebml_read_id(s, &il)) { |
1080 case MATROSKA_ID_CHAPTERATOM: | |
1081 { | |
1082 uint64_t len, start = 0, end = 0; | |
1083 char *name = 0; | |
1084 int i; | |
1085 int cid; | |
1086 | |
1087 len = ebml_read_length(s, &i); | |
1088 l = len + i; | |
1089 | |
1090 while (len > 0) { | |
1091 uint64_t l; | |
1092 int il; | |
1093 | |
1094 switch (ebml_read_id(s, &il)) { | |
1095 case MATROSKA_ID_CHAPTERTIMESTART: | |
1096 start = ebml_read_uint(s, &l) / 1000000; | |
1097 break; | |
1098 | |
1099 case MATROSKA_ID_CHAPTERTIMEEND: | |
1100 end = ebml_read_uint(s, &l) / 1000000; | |
1101 break; | |
1102 | |
1103 case MATROSKA_ID_CHAPTERDISPLAY: | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1104 { |
31177 | 1105 uint64_t len; |
1106 int i; | |
1107 | |
1108 len = ebml_read_length(s, &i); | |
1109 l = len + i; | |
1110 while (len > 0) { | |
1111 uint64_t l; | |
1112 int il; | |
1113 | |
1114 switch (ebml_read_id(s, &il)) { | |
1115 case MATROSKA_ID_CHAPSTRING: | |
1116 name = ebml_read_utf8(s, &l); | |
1117 break; | |
1118 default: | |
1119 ebml_read_skip(s, &l); | |
1120 break; | |
1121 } | |
1122 len -= l + il; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1123 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1124 } |
31177 | 1125 break; |
1126 | |
1127 default: | |
1128 ebml_read_skip(s, &l); | |
1129 break; | |
1130 } | |
1131 len -= l + il; | |
1132 } | |
1133 | |
1134 if (!name) | |
19342 | 1135 name = strdup("(unnamed)"); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29238
diff
changeset
|
1136 |
31177 | 1137 cid = demuxer_add_chapter(demuxer, name, start, end); |
1138 | |
1139 mp_msg(MSGT_DEMUX, MSGL_V, | |
1140 "[mkv] Chapter %u from %02d:%02d:%02d." | |
1141 "%03d to %02d:%02d:%02d.%03d, %s\n", cid, | |
1142 (int) (start / 60 / 60 / 1000), | |
1143 (int) ((start / 60 / 1000) % 60), | |
1144 (int) ((start / 1000) % 60), | |
1145 (int) (start % 1000), | |
1146 (int) (end / 60 / 60 / 1000), | |
1147 (int) ((end / 60 / 1000) % 60), | |
1148 (int) ((end / 1000) % 60), | |
1149 (int) (end % 1000), name); | |
1150 | |
1151 free(name); | |
1152 break; | |
1153 } | |
1154 | |
1155 default: | |
1156 ebml_read_skip(s, &l); | |
1157 break; | |
1158 } | |
1159 len -= l + il; | |
1160 } | |
1161 break; | |
1162 } | |
1163 | |
1164 default: | |
1165 ebml_read_skip(s, &l); | |
1166 break; | |
1167 } | |
1168 | |
1169 length -= l + il; | |
1170 } | |
1171 | |
1172 mp_msg(MSGT_DEMUX, MSGL_V, | |
1173 "[mkv] \\---- [ parsing chapters ] ---------\n"); | |
1174 return 0; | |
1175 } | |
1176 | |
1177 static int demux_mkv_read_tags(demuxer_t *demuxer) | |
1178 { | |
1179 ebml_read_skip(demuxer->stream, NULL); | |
1180 return 0; | |
1181 } | |
1182 | |
1183 static int demux_mkv_read_attachments(demuxer_t *demuxer) | |
1184 { | |
1185 stream_t *s = demuxer->stream; | |
1186 uint64_t length, l; | |
1187 int il; | |
1188 | |
1189 mp_msg(MSGT_DEMUX, MSGL_V, | |
1190 "[mkv] /---- [ parsing attachments ] ---------\n"); | |
1191 length = ebml_read_length(s, NULL); | |
1192 | |
1193 while (length > 0) { | |
1194 switch (ebml_read_id(s, &il)) { | |
1195 case MATROSKA_ID_ATTACHEDFILE: | |
1196 { | |
1197 uint64_t len; | |
1198 int i; | |
1199 char *name = NULL; | |
1200 char *mime = NULL; | |
1201 char *data = NULL; | |
1202 int data_size = 0; | |
1203 | |
1204 len = ebml_read_length(s, &i); | |
1205 l = len + i; | |
1206 | |
1207 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + an attachment...\n"); | |
1208 | |
1209 while (len > 0) { | |
1210 uint64_t l; | |
1211 int il; | |
1212 | |
1213 switch (ebml_read_id(s, &il)) { | |
1214 case MATROSKA_ID_FILENAME: | |
1215 name = ebml_read_utf8(s, &l); | |
1216 if (name == NULL) | |
1217 return 0; | |
1218 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] | + FileName: %s\n", | |
1219 name); | |
1220 break; | |
1221 | |
1222 case MATROSKA_ID_FILEMIMETYPE: | |
1223 mime = ebml_read_ascii(s, &l); | |
1224 if (mime == NULL) | |
1225 return 0; | |
1226 mp_msg(MSGT_DEMUX, MSGL_V, | |
1227 "[mkv] | + FileMimeType: %s\n", mime); | |
1228 break; | |
1229 | |
1230 case MATROSKA_ID_FILEDATA: | |
1231 { | |
1232 int x; | |
1233 uint64_t num = ebml_read_length(s, &x); | |
1234 l = x + num; | |
1235 free(data); | |
1236 data = malloc(num); | |
1237 if (stream_read(s, data, num) != (int) num) { | |
1238 free(data); | |
1239 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1240 } |
31177 | 1241 data_size = num; |
1242 mp_msg(MSGT_DEMUX, MSGL_V, | |
1243 "[mkv] | + FileData, length " "%u\n", | |
1244 data_size); | |
1245 break; | |
1246 } | |
1247 | |
1248 default: | |
1249 ebml_read_skip(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1250 break; |
31177 | 1251 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1252 len -= l + il; |
31177 | 1253 } |
1254 | |
1255 demuxer_add_attachment(demuxer, name, mime, data, data_size); | |
1256 mp_msg(MSGT_DEMUX, MSGL_V, | |
1257 "[mkv] Attachment: %s, %s, %u bytes\n", name, mime, | |
1258 data_size); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1259 break; |
31177 | 1260 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1261 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1262 default: |
31177 | 1263 ebml_read_skip(s, &l); |
1264 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1265 } |
31177 | 1266 length -= l + il; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1267 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1268 |
31177 | 1269 mp_msg(MSGT_DEMUX, MSGL_V, |
1270 "[mkv] \\---- [ parsing attachments ] ---------\n"); | |
1271 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1272 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1273 |
31177 | 1274 static int demux_mkv_read_seekhead(demuxer_t *demuxer) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1275 { |
31177 | 1276 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; |
1277 stream_t *s = demuxer->stream; | |
1278 uint64_t length, l, seek_pos, saved_pos, num; | |
1279 uint32_t seek_id; | |
1280 int i, il, res = 0; | |
1281 off_t off; | |
1282 | |
1283 off = stream_tell(s); | |
1284 for (i = 0; i < mkv_d->parsed_seekhead_num; i++) | |
1285 if (mkv_d->parsed_seekhead[i] == off) { | |
1286 ebml_read_skip(s, NULL); | |
1287 return 0; | |
1288 } | |
1289 mkv_d->parsed_seekhead = realloc(mkv_d->parsed_seekhead, | |
1290 (mkv_d->parsed_seekhead_num + 1) | |
1291 * sizeof(off_t)); | |
1292 mkv_d->parsed_seekhead[mkv_d->parsed_seekhead_num++] = off; | |
1293 | |
1294 mp_msg(MSGT_DEMUX, MSGL_V, | |
1295 "[mkv] /---- [ parsing seek head ] ---------\n"); | |
1296 length = ebml_read_length(s, NULL); | |
1297 /* off now holds the position of the next element after the seek head. */ | |
1298 off = stream_tell(s) + length; | |
1299 while (length > 0 && !res) { | |
1300 | |
1301 seek_id = 0; | |
1302 seek_pos = EBML_UINT_INVALID; | |
1303 | |
1304 switch (ebml_read_id(s, &il)) { | |
1305 case MATROSKA_ID_SEEKENTRY: | |
18937
9e95ac641e77
Initial libass release (without mencoder support).
eugeni
parents:
18934
diff
changeset
|
1306 { |
31177 | 1307 uint64_t len; |
1308 | |
1309 len = ebml_read_length(s, &i); | |
1310 l = len + i; | |
1311 | |
1312 while (len > 0) { | |
1313 uint64_t l; | |
1314 int il; | |
1315 | |
1316 switch (ebml_read_id(s, &il)) { | |
1317 case MATROSKA_ID_SEEKID: | |
1318 num = ebml_read_uint(s, &l); | |
1319 if (num != EBML_UINT_INVALID) | |
1320 seek_id = num; | |
1321 break; | |
1322 | |
1323 case MATROSKA_ID_SEEKPOSITION: | |
1324 seek_pos = ebml_read_uint(s, &l); | |
1325 break; | |
1326 | |
1327 default: | |
1328 ebml_read_skip(s, &l); | |
1329 break; | |
18937
9e95ac641e77
Initial libass release (without mencoder support).
eugeni
parents:
18934
diff
changeset
|
1330 } |
31177 | 1331 len -= l + il; |
18937
9e95ac641e77
Initial libass release (without mencoder support).
eugeni
parents:
18934
diff
changeset
|
1332 } |
9e95ac641e77
Initial libass release (without mencoder support).
eugeni
parents:
18934
diff
changeset
|
1333 |
31177 | 1334 break; |
1335 } | |
1336 | |
1337 default: | |
1338 ebml_read_skip(s, &l); | |
18937
9e95ac641e77
Initial libass release (without mencoder support).
eugeni
parents:
18934
diff
changeset
|
1339 break; |
9e95ac641e77
Initial libass release (without mencoder support).
eugeni
parents:
18934
diff
changeset
|
1340 } |
31177 | 1341 length -= l + il; |
1342 | |
1343 if (seek_id == 0 || seek_id == MATROSKA_ID_CLUSTER | |
1344 || seek_pos == EBML_UINT_INVALID | |
1345 || ((mkv_d->segment_start + seek_pos) >= | |
1346 (uint64_t) demuxer->movi_end)) | |
1347 continue; | |
1348 | |
1349 saved_pos = stream_tell(s); | |
1350 if (!stream_seek(s, mkv_d->segment_start + seek_pos)) | |
1351 res = 1; | |
1352 else { | |
1353 if (ebml_read_id(s, &il) != seek_id) | |
1354 res = 1; | |
1355 else | |
1356 switch (seek_id) { | |
1357 case MATROSKA_ID_CUES: | |
1358 if (demux_mkv_read_cues(demuxer)) | |
1359 res = 1; | |
1360 break; | |
1361 | |
1362 case MATROSKA_ID_TAGS: | |
1363 if (demux_mkv_read_tags(demuxer)) | |
1364 res = 1; | |
1365 break; | |
1366 | |
1367 case MATROSKA_ID_SEEKHEAD: | |
1368 if (demux_mkv_read_seekhead(demuxer)) | |
1369 res = 1; | |
1370 break; | |
1371 | |
1372 case MATROSKA_ID_CHAPTERS: | |
1373 if (demux_mkv_read_chapters(demuxer)) | |
1374 res = 1; | |
1375 break; | |
1376 } | |
1377 } | |
1378 | |
1379 stream_seek(s, saved_pos); | |
18937
9e95ac641e77
Initial libass release (without mencoder support).
eugeni
parents:
18934
diff
changeset
|
1380 } |
31177 | 1381 if (res) { |
1382 /* If there was an error then try to skip this seek head. */ | |
1383 if (stream_seek(s, off)) | |
1384 res = 0; | |
1385 } else if (length > 0) | |
1386 stream_seek(s, stream_tell(s) + length); | |
1387 mp_msg(MSGT_DEMUX, MSGL_V, | |
1388 "[mkv] \\---- [ parsing seek head ] ---------\n"); | |
1389 return res; | |
18937
9e95ac641e77
Initial libass release (without mencoder support).
eugeni
parents:
18934
diff
changeset
|
1390 } |
9e95ac641e77
Initial libass release (without mencoder support).
eugeni
parents:
18934
diff
changeset
|
1391 |
31177 | 1392 static int demux_mkv_open_video(demuxer_t *demuxer, mkv_track_t *track, |
1393 int vid); | |
1394 static int demux_mkv_open_audio(demuxer_t *demuxer, mkv_track_t *track, | |
1395 int aid); | |
1396 static int demux_mkv_open_sub(demuxer_t *demuxer, mkv_track_t *track, | |
1397 int sid); | |
1398 | |
1399 static void display_create_tracks(demuxer_t *demuxer) | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1400 { |
31177 | 1401 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; |
1402 int i, vid = 0, aid = 0, sid = 0; | |
1403 | |
1404 for (i = 0; i < mkv_d->num_tracks; i++) { | |
1405 char *type = "unknown", str[32]; | |
1406 *str = '\0'; | |
1407 switch (mkv_d->tracks[i]->type) { | |
1408 case MATROSKA_TRACK_VIDEO: | |
1409 type = "video"; | |
1410 demux_mkv_open_video(demuxer, mkv_d->tracks[i], vid); | |
1411 if (mkv_d->tracks[i]->name) | |
1412 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_VID_%d_NAME=%s\n", vid, | |
1413 mkv_d->tracks[i]->name); | |
1414 sprintf(str, "-vid %u", vid++); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1415 break; |
31177 | 1416 case MATROSKA_TRACK_AUDIO: |
1417 type = "audio"; | |
1418 demux_mkv_open_audio(demuxer, mkv_d->tracks[i], aid); | |
1419 if (mkv_d->tracks[i]->name) | |
1420 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_NAME=%s\n", aid, | |
1421 mkv_d->tracks[i]->name); | |
1422 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", aid, | |
1423 mkv_d->tracks[i]->language); | |
1424 sprintf(str, "-aid %u, -alang %.5s", aid++, | |
1425 mkv_d->tracks[i]->language); | |
1426 break; | |
1427 case MATROSKA_TRACK_SUBTITLE: | |
1428 type = "subtitles"; | |
1429 demux_mkv_open_sub(demuxer, mkv_d->tracks[i], sid); | |
1430 if (mkv_d->tracks[i]->name) | |
1431 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_NAME=%s\n", sid, | |
1432 mkv_d->tracks[i]->name); | |
1433 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", sid, | |
1434 mkv_d->tracks[i]->language); | |
1435 sprintf(str, "-sid %u, -slang %.5s", sid++, | |
1436 mkv_d->tracks[i]->language); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1437 break; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1438 } |
31177 | 1439 if (mkv_d->tracks[i]->name) |
1440 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_TrackIDName, | |
1441 mkv_d->tracks[i]->tnum, type, mkv_d->tracks[i]->codec_id, | |
1442 mkv_d->tracks[i]->name, str); | |
1443 else | |
1444 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_TrackID, | |
1445 mkv_d->tracks[i]->tnum, type, mkv_d->tracks[i]->codec_id, | |
1446 str); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1447 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1448 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1449 |
23270
f094c9256137
Avoid code duplication in mkv demuxer for standard fourcc/extradata handling
reimar
parents:
23187
diff
changeset
|
1450 typedef struct { |
31177 | 1451 char *id; |
1452 int fourcc; | |
1453 int extradata; | |
23270
f094c9256137
Avoid code duplication in mkv demuxer for standard fourcc/extradata handling
reimar
parents:
23187
diff
changeset
|
1454 } videocodec_info_t; |
f094c9256137
Avoid code duplication in mkv demuxer for standard fourcc/extradata handling
reimar
parents:
23187
diff
changeset
|
1455 |
f094c9256137
Avoid code duplication in mkv demuxer for standard fourcc/extradata handling
reimar
parents:
23187
diff
changeset
|
1456 static const videocodec_info_t vinfo[] = { |
31177 | 1457 {MKV_V_MPEG1, mmioFOURCC('m', 'p', 'g', '1'), 0}, |
1458 {MKV_V_MPEG2, mmioFOURCC('m', 'p', 'g', '2'), 0}, | |
1459 {MKV_V_MPEG4_SP, mmioFOURCC('m', 'p', '4', 'v'), 1}, | |
1460 {MKV_V_MPEG4_ASP, mmioFOURCC('m', 'p', '4', 'v'), 1}, | |
1461 {MKV_V_MPEG4_AP, mmioFOURCC('m', 'p', '4', 'v'), 1}, | |
1462 {MKV_V_MPEG4_AVC, mmioFOURCC('a', 'v', 'c', '1'), 1}, | |
1463 {MKV_V_THEORA, mmioFOURCC('t', 'h', 'e', 'o'), 1}, | |
31263 | 1464 {MKV_V_VP8, mmioFOURCC('V', 'P', '8', '0'), 0}, |
31177 | 1465 {NULL, 0, 0} |
23270
f094c9256137
Avoid code duplication in mkv demuxer for standard fourcc/extradata handling
reimar
parents:
23187
diff
changeset
|
1466 }; |
f094c9256137
Avoid code duplication in mkv demuxer for standard fourcc/extradata handling
reimar
parents:
23187
diff
changeset
|
1467 |
31177 | 1468 static int demux_mkv_open_video(demuxer_t *demuxer, mkv_track_t *track, |
1469 int vid) | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1470 { |
31177 | 1471 BITMAPINFOHEADER *bih; |
1472 void *ImageDesc = NULL; | |
1473 sh_video_t *sh_v; | |
1474 | |
1475 if (track->ms_compat) { /* MS compatibility mode */ | |
1476 BITMAPINFOHEADER *src; | |
1477 | |
1478 if (track->private_data == NULL | |
1479 || track->private_size < sizeof(BITMAPINFOHEADER)) | |
1480 return 1; | |
1481 | |
1482 src = (BITMAPINFOHEADER *) track->private_data; | |
1483 bih = calloc(1, track->private_size); | |
1484 bih->biSize = le2me_32(src->biSize); | |
1485 bih->biWidth = le2me_32(src->biWidth); | |
1486 bih->biHeight = le2me_32(src->biHeight); | |
1487 bih->biPlanes = le2me_16(src->biPlanes); | |
1488 bih->biBitCount = le2me_16(src->biBitCount); | |
1489 bih->biCompression = le2me_32(src->biCompression); | |
1490 bih->biSizeImage = le2me_32(src->biSizeImage); | |
1491 bih->biXPelsPerMeter = le2me_32(src->biXPelsPerMeter); | |
1492 bih->biYPelsPerMeter = le2me_32(src->biYPelsPerMeter); | |
1493 bih->biClrUsed = le2me_32(src->biClrUsed); | |
1494 bih->biClrImportant = le2me_32(src->biClrImportant); | |
1495 memcpy((char *) bih + sizeof(BITMAPINFOHEADER), | |
1496 (char *) src + sizeof(BITMAPINFOHEADER), | |
1497 track->private_size - sizeof(BITMAPINFOHEADER)); | |
1498 | |
1499 if (track->v_width == 0) | |
1500 track->v_width = bih->biWidth; | |
1501 if (track->v_height == 0) | |
1502 track->v_height = bih->biHeight; | |
1503 } else { | |
1504 bih = calloc(1, sizeof(BITMAPINFOHEADER)); | |
1505 bih->biSize = sizeof(BITMAPINFOHEADER); | |
1506 bih->biWidth = track->v_width; | |
1507 bih->biHeight = track->v_height; | |
1508 bih->biBitCount = 24; | |
1509 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount / 8; | |
1510 | |
1511 if (track->private_size >= RVPROPERTIES_SIZE | |
1512 && (!strcmp(track->codec_id, MKV_V_REALV10) | |
1513 || !strcmp(track->codec_id, MKV_V_REALV20) | |
1514 || !strcmp(track->codec_id, MKV_V_REALV30) | |
1515 || !strcmp(track->codec_id, MKV_V_REALV40))) { | |
1516 unsigned char *dst, *src; | |
1517 uint32_t type2; | |
1518 unsigned int cnt; | |
1519 | |
1520 src = (uint8_t *) track->private_data + RVPROPERTIES_SIZE; | |
1521 | |
1522 cnt = track->private_size - RVPROPERTIES_SIZE; | |
1523 bih = realloc(bih, sizeof(BITMAPINFOHEADER) + 8 + cnt); | |
1524 bih->biSize = 48 + cnt; | |
1525 bih->biPlanes = 1; | |
1526 type2 = AV_RB32(src - 4); | |
1527 if (type2 == 0x10003000 || type2 == 0x10003001) | |
1528 bih->biCompression = mmioFOURCC('R', 'V', '1', '3'); | |
1529 else | |
1530 bih->biCompression = | |
1531 mmioFOURCC('R', 'V', track->codec_id[9], '0'); | |
1532 dst = (unsigned char *) (bih + 1); | |
1533 // copy type1 and type2 info from rv properties | |
1534 memcpy(dst, src - 8, 8); | |
1535 stream_read(demuxer->stream, dst + 8, cnt); | |
1536 track->realmedia = 1; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1537 |
27341
e7c989f7a7c9
Start unifying names of internal preprocessor directives.
diego
parents:
26752
diff
changeset
|
1538 #ifdef CONFIG_QTX_CODECS |
31177 | 1539 } else if (track->private_size >= sizeof(ImageDescription) |
1540 && !strcmp(track->codec_id, MKV_V_QUICKTIME)) { | |
1541 ImageDescriptionPtr idesc; | |
1542 | |
1543 idesc = (ImageDescriptionPtr) track->private_data; | |
1544 idesc->idSize = be2me_32(idesc->idSize); | |
1545 idesc->cType = be2me_32(idesc->cType); | |
1546 idesc->version = be2me_16(idesc->version); | |
1547 idesc->revisionLevel = be2me_16(idesc->revisionLevel); | |
1548 idesc->vendor = be2me_32(idesc->vendor); | |
1549 idesc->temporalQuality = be2me_32(idesc->temporalQuality); | |
1550 idesc->spatialQuality = be2me_32(idesc->spatialQuality); | |
1551 idesc->width = be2me_16(idesc->width); | |
1552 idesc->height = be2me_16(idesc->height); | |
1553 idesc->hRes = be2me_32(idesc->hRes); | |
1554 idesc->vRes = be2me_32(idesc->vRes); | |
1555 idesc->dataSize = be2me_32(idesc->dataSize); | |
1556 idesc->frameCount = be2me_16(idesc->frameCount); | |
1557 idesc->depth = be2me_16(idesc->depth); | |
1558 idesc->clutID = be2me_16(idesc->clutID); | |
1559 bih->biPlanes = 1; | |
1560 bih->biCompression = idesc->cType; | |
1561 ImageDesc = idesc; | |
1562 #endif /* CONFIG_QTX_CODECS */ | |
1563 | |
1564 } else { | |
1565 const videocodec_info_t *vi = vinfo; | |
1566 while (vi->id && strcmp(vi->id, track->codec_id)) | |
1567 vi++; | |
1568 bih->biCompression = vi->fourcc; | |
1569 if (vi->extradata && track->private_data | |
1570 && (track->private_size > 0)) { | |
1571 bih->biSize += track->private_size; | |
1572 bih = realloc(bih, bih->biSize); | |
1573 memcpy(bih + 1, track->private_data, track->private_size); | |
19154 | 1574 } |
31177 | 1575 track->reorder_timecodes = user_correct_pts == 0; |
1576 if (!vi->id) { | |
1577 mp_msg(MSGT_DEMUX, MSGL_WARN, MSGTR_MPDEMUX_MKV_UnknownCodecID, | |
1578 track->codec_id, track->tnum); | |
1579 free(bih); | |
1580 return 1; | |
1581 } | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1582 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1583 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1584 |
31177 | 1585 sh_v = new_sh_video_vid(demuxer, track->tnum, vid); |
1586 sh_v->bih = bih; | |
1587 sh_v->format = sh_v->bih->biCompression; | |
1588 if (track->v_frate == 0.0) | |
1589 track->v_frate = 25.0; | |
1590 sh_v->fps = track->v_frate; | |
1591 sh_v->frametime = 1 / track->v_frate; | |
1592 sh_v->aspect = 0; | |
1593 if (!track->realmedia) { | |
1594 sh_v->disp_w = track->v_width; | |
1595 sh_v->disp_h = track->v_height; | |
1596 if (track->v_dheight) | |
1597 sh_v->aspect = (float) track->v_dwidth / (float) track->v_dheight; | |
1598 } else { | |
1599 // vd_realvid.c will set aspect to disp_w/disp_h and rederive | |
1600 // disp_w and disp_h from the RealVideo stream contents returned | |
1601 // by the Real DLLs. If DisplayWidth/DisplayHeight was not set in | |
1602 // the Matroska file then it has already been set to PixelWidth/Height | |
1603 // by check_track_information. | |
1604 sh_v->disp_w = track->v_dwidth; | |
1605 sh_v->disp_h = track->v_dheight; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1606 } |
31177 | 1607 sh_v->ImageDesc = ImageDesc; |
1608 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Aspect: %f\n", sh_v->aspect); | |
1609 | |
1610 sh_v->ds = demuxer->video; | |
1611 return 0; | |
1612 } | |
1613 | |
1614 static int demux_mkv_open_audio(demuxer_t *demuxer, mkv_track_t *track, | |
1615 int aid) | |
1616 { | |
1617 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; | |
1618 sh_audio_t *sh_a = new_sh_audio_aid(demuxer, track->tnum, aid); | |
1619 demux_packet_t *dp; | |
1620 if (!sh_a) | |
1621 return 1; | |
1622 mkv_d->audio_tracks[mkv_d->last_aid] = track->tnum; | |
1623 | |
1624 if (track->language && (strcmp(track->language, "und") != 0)) | |
1625 sh_a->lang = strdup(track->language); | |
1626 sh_a->default_track = track->default_track; | |
1627 sh_a->ds = demuxer->audio; | |
1628 sh_a->wf = malloc(sizeof(WAVEFORMATEX)); | |
1629 if (track->ms_compat && (track->private_size >= sizeof(WAVEFORMATEX))) { | |
1630 WAVEFORMATEX *wf = (WAVEFORMATEX *) track->private_data; | |
1631 sh_a->wf = realloc(sh_a->wf, track->private_size); | |
1632 sh_a->wf->wFormatTag = le2me_16(wf->wFormatTag); | |
1633 sh_a->wf->nChannels = le2me_16(wf->nChannels); | |
1634 sh_a->wf->nSamplesPerSec = le2me_32(wf->nSamplesPerSec); | |
1635 sh_a->wf->nAvgBytesPerSec = le2me_32(wf->nAvgBytesPerSec); | |
1636 sh_a->wf->nBlockAlign = le2me_16(wf->nBlockAlign); | |
1637 sh_a->wf->wBitsPerSample = le2me_16(wf->wBitsPerSample); | |
1638 sh_a->wf->cbSize = track->private_size - sizeof(WAVEFORMATEX); | |
1639 memcpy(sh_a->wf + 1, wf + 1, | |
1640 track->private_size - sizeof(WAVEFORMATEX)); | |
1641 if (track->a_sfreq == 0.0) | |
1642 track->a_sfreq = sh_a->wf->nSamplesPerSec; | |
1643 if (track->a_channels == 0) | |
1644 track->a_channels = sh_a->wf->nChannels; | |
1645 if (track->a_bps == 0) | |
1646 track->a_bps = sh_a->wf->wBitsPerSample; | |
1647 track->a_formattag = sh_a->wf->wFormatTag; | |
1648 } else { | |
1649 memset(sh_a->wf, 0, sizeof(WAVEFORMATEX)); | |
1650 if (!strcmp(track->codec_id, MKV_A_MP3) | |
1651 || !strcmp(track->codec_id, MKV_A_MP2)) | |
1652 track->a_formattag = 0x0055; | |
1653 else if (!strncmp(track->codec_id, MKV_A_AC3, strlen(MKV_A_AC3))) | |
1654 track->a_formattag = 0x2000; | |
1655 else if (!strcmp(track->codec_id, MKV_A_DTS)) | |
1656 track->a_formattag = 0x2001; | |
1657 else if (!strcmp(track->codec_id, MKV_A_PCM) | |
1658 || !strcmp(track->codec_id, MKV_A_PCM_BE)) | |
1659 track->a_formattag = 0x0001; | |
1660 else if (!strcmp(track->codec_id, MKV_A_AAC_2MAIN) | |
1661 || !strncmp(track->codec_id, MKV_A_AAC_2LC, | |
1662 strlen(MKV_A_AAC_2LC)) | |
1663 || !strcmp(track->codec_id, MKV_A_AAC_2SSR) | |
1664 || !strcmp(track->codec_id, MKV_A_AAC_4MAIN) | |
1665 || !strncmp(track->codec_id, MKV_A_AAC_4LC, | |
1666 strlen(MKV_A_AAC_4LC)) | |
1667 || !strcmp(track->codec_id, MKV_A_AAC_4SSR) | |
1668 || !strcmp(track->codec_id, MKV_A_AAC_4LTP) | |
1669 || !strcmp(track->codec_id, MKV_A_AAC)) | |
1670 track->a_formattag = mmioFOURCC('M', 'P', '4', 'A'); | |
1671 else if (!strcmp(track->codec_id, MKV_A_VORBIS)) { | |
1672 if (track->private_data == NULL) | |
1673 return 1; | |
1674 track->a_formattag = mmioFOURCC('v', 'r', 'b', 's'); | |
1675 } else if (!strcmp(track->codec_id, MKV_A_QDMC)) | |
1676 track->a_formattag = mmioFOURCC('Q', 'D', 'M', 'C'); | |
1677 else if (!strcmp(track->codec_id, MKV_A_QDMC2)) | |
1678 track->a_formattag = mmioFOURCC('Q', 'D', 'M', '2'); | |
1679 else if (!strcmp(track->codec_id, MKV_A_WAVPACK)) | |
1680 track->a_formattag = mmioFOURCC('W', 'V', 'P', 'K'); | |
1681 else if (!strcmp(track->codec_id, MKV_A_TRUEHD)) | |
1682 track->a_formattag = mmioFOURCC('T', 'R', 'H', 'D'); | |
1683 else if (!strcmp(track->codec_id, MKV_A_FLAC)) { | |
1684 if (track->private_data == NULL || track->private_size == 0) { | |
1685 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
1686 MSGTR_MPDEMUX_MKV_FlacTrackDoesNotContainValidHeaders); | |
1687 return 1; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1688 } |
31177 | 1689 track->a_formattag = mmioFOURCC('f', 'L', 'a', 'C'); |
1690 } else if (track->private_size >= RAPROPERTIES4_SIZE) { | |
1691 if (!strcmp(track->codec_id, MKV_A_REAL28)) | |
1692 track->a_formattag = mmioFOURCC('2', '8', '_', '8'); | |
1693 else if (!strcmp(track->codec_id, MKV_A_REALATRC)) | |
1694 track->a_formattag = mmioFOURCC('a', 't', 'r', 'c'); | |
1695 else if (!strcmp(track->codec_id, MKV_A_REALCOOK)) | |
1696 track->a_formattag = mmioFOURCC('c', 'o', 'o', 'k'); | |
1697 else if (!strcmp(track->codec_id, MKV_A_REALDNET)) | |
1698 track->a_formattag = mmioFOURCC('d', 'n', 'e', 't'); | |
1699 else if (!strcmp(track->codec_id, MKV_A_REALSIPR)) | |
1700 track->a_formattag = mmioFOURCC('s', 'i', 'p', 'r'); | |
1701 } else { | |
1702 mp_msg(MSGT_DEMUX, MSGL_WARN, MSGTR_MPDEMUX_MKV_UnknownAudioCodec, | |
1703 track->codec_id, track->tnum); | |
1704 free_sh_audio(demuxer, track->tnum); | |
1705 return 1; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1706 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1707 } |
31177 | 1708 |
1709 sh_a->format = track->a_formattag; | |
1710 sh_a->wf->wFormatTag = track->a_formattag; | |
1711 sh_a->channels = track->a_channels; | |
1712 sh_a->wf->nChannels = track->a_channels; | |
1713 sh_a->samplerate = (uint32_t) track->a_sfreq; | |
1714 sh_a->wf->nSamplesPerSec = (uint32_t) track->a_sfreq; | |
1715 if (track->a_bps == 0) { | |
1716 sh_a->samplesize = 2; | |
1717 sh_a->wf->wBitsPerSample = 16; | |
1718 } else { | |
1719 sh_a->samplesize = track->a_bps / 8; | |
1720 sh_a->wf->wBitsPerSample = track->a_bps; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1721 } |
31177 | 1722 if (track->a_formattag == 0x0055) { /* MP3 || MP2 */ |
1723 sh_a->wf->nAvgBytesPerSec = 16000; | |
1724 sh_a->wf->nBlockAlign = 1152; | |
1725 } else if ((track->a_formattag == 0x2000) || /* AC3 */ | |
1726 (track->a_formattag == 0x2001)) { /* DTS */ | |
1727 free(sh_a->wf); | |
1728 sh_a->wf = NULL; | |
1729 } else if (track->a_formattag == 0x0001) { /* PCM || PCM_BE */ | |
1730 sh_a->wf->nAvgBytesPerSec = sh_a->channels * sh_a->samplerate * 2; | |
1731 sh_a->wf->nBlockAlign = sh_a->wf->nAvgBytesPerSec; | |
1732 if (!strcmp(track->codec_id, MKV_A_PCM_BE)) | |
1733 sh_a->format = mmioFOURCC('t', 'w', 'o', 's'); | |
1734 } else if (!strcmp(track->codec_id, MKV_A_QDMC) | |
1735 || !strcmp(track->codec_id, MKV_A_QDMC2)) { | |
1736 sh_a->wf->nAvgBytesPerSec = 16000; | |
1737 sh_a->wf->nBlockAlign = 1486; | |
1738 track->fix_i_bps = 1; | |
1739 track->qt_last_a_pts = 0.0; | |
1740 if (track->private_data != NULL) { | |
1741 sh_a->codecdata = malloc(track->private_size); | |
1742 memcpy(sh_a->codecdata, track->private_data, track->private_size); | |
1743 sh_a->codecdata_len = track->private_size; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1744 } |
31177 | 1745 } else if (track->a_formattag == mmioFOURCC('M', 'P', '4', 'A')) { |
1746 int profile, srate_idx; | |
1747 | |
1748 sh_a->wf->nAvgBytesPerSec = 16000; | |
1749 sh_a->wf->nBlockAlign = 1024; | |
1750 | |
1751 if (!strcmp(track->codec_id, MKV_A_AAC) | |
1752 && (NULL != track->private_data)) { | |
1753 sh_a->codecdata = malloc(track->private_size); | |
1754 memcpy(sh_a->codecdata, track->private_data, track->private_size); | |
1755 sh_a->codecdata_len = track->private_size; | |
1756 return 0; | |
1757 } | |
1758 | |
1759 /* Recreate the 'private data' */ | |
1760 /* which faad2 uses in its initialization */ | |
1761 srate_idx = aac_get_sample_rate_index(sh_a->samplerate); | |
1762 if (!strncmp(&track->codec_id[12], "MAIN", 4)) | |
1763 profile = 0; | |
1764 else if (!strncmp(&track->codec_id[12], "LC", 2)) | |
1765 profile = 1; | |
1766 else if (!strncmp(&track->codec_id[12], "SSR", 3)) | |
1767 profile = 2; | |
1768 else | |
1769 profile = 3; | |
1770 sh_a->codecdata = malloc(5); | |
1771 sh_a->codecdata[0] = ((profile + 1) << 3) | ((srate_idx & 0xE) >> 1); | |
1772 sh_a->codecdata[1] = | |
1773 ((srate_idx & 0x1) << 7) | (track->a_channels << 3); | |
1774 | |
1775 if (strstr(track->codec_id, "SBR") != NULL) { | |
1776 /* HE-AAC (aka SBR AAC) */ | |
1777 sh_a->codecdata_len = 5; | |
1778 | |
1779 sh_a->samplerate *= 2; | |
1780 sh_a->wf->nSamplesPerSec *= 2; | |
1781 srate_idx = aac_get_sample_rate_index(sh_a->samplerate); | |
1782 sh_a->codecdata[2] = AAC_SYNC_EXTENSION_TYPE >> 3; | |
1783 sh_a->codecdata[3] = ((AAC_SYNC_EXTENSION_TYPE & 0x07) << 5) | 5; | |
1784 sh_a->codecdata[4] = (1 << 7) | (srate_idx << 3); | |
1785 track->default_duration = 1024.0 / (sh_a->samplerate / 2); | |
1786 } else { | |
1787 sh_a->codecdata_len = 2; | |
1788 track->default_duration = 1024.0 / (float) sh_a->samplerate; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1789 } |
31177 | 1790 } else if (track->a_formattag == mmioFOURCC('v', 'r', 'b', 's')) { /* VORBIS */ |
1791 sh_a->wf->cbSize = track->private_size; | |
1792 sh_a->wf = realloc(sh_a->wf, sizeof(WAVEFORMATEX) + sh_a->wf->cbSize); | |
1793 memcpy((unsigned char *) (sh_a->wf + 1), track->private_data, | |
1794 sh_a->wf->cbSize); | |
1795 } else if (track->private_size >= RAPROPERTIES4_SIZE | |
1796 && !strncmp(track->codec_id, MKV_A_REALATRC, 7)) { | |
1797 /* Common initialization for all RealAudio codecs */ | |
1798 unsigned char *src = track->private_data; | |
1799 int codecdata_length, version; | |
1800 int flavor; | |
1801 | |
1802 sh_a->wf->nAvgBytesPerSec = 0; /* FIXME !? */ | |
1803 | |
1804 version = AV_RB16(src + 4); | |
1805 flavor = AV_RB16(src + 22); | |
1806 track->coded_framesize = AV_RB32(src + 24); | |
1807 track->sub_packet_h = AV_RB16(src + 40); | |
1808 sh_a->wf->nBlockAlign = track->audiopk_size = AV_RB16(src + 42); | |
1809 track->sub_packet_size = AV_RB16(src + 44); | |
1810 if (version == 4) { | |
1811 src += RAPROPERTIES4_SIZE; | |
1812 src += src[0] + 1; | |
1813 src += src[0] + 1; | |
1814 } else | |
1815 src += RAPROPERTIES5_SIZE; | |
1816 | |
1817 src += 3; | |
1818 if (version == 5) | |
1819 src++; | |
1820 codecdata_length = AV_RB32(src); | |
1821 src += 4; | |
1822 sh_a->wf->cbSize = codecdata_length; | |
1823 sh_a->wf = realloc(sh_a->wf, sizeof(WAVEFORMATEX) + sh_a->wf->cbSize); | |
1824 memcpy(((char *) (sh_a->wf + 1)), src, codecdata_length); | |
1825 | |
1826 switch (track->a_formattag) { | |
1827 case mmioFOURCC('a', 't', 'r', 'c'): | |
1828 sh_a->wf->nAvgBytesPerSec = atrc_fl2bps[flavor]; | |
1829 sh_a->wf->nBlockAlign = track->sub_packet_size; | |
1830 track->audio_buf = | |
1831 malloc(track->sub_packet_h * track->audiopk_size); | |
1832 track->audio_timestamp = | |
1833 malloc(track->sub_packet_h * sizeof(float)); | |
1834 break; | |
1835 case mmioFOURCC('c', 'o', 'o', 'k'): | |
1836 sh_a->wf->nAvgBytesPerSec = cook_fl2bps[flavor]; | |
1837 sh_a->wf->nBlockAlign = track->sub_packet_size; | |
1838 track->audio_buf = | |
1839 malloc(track->sub_packet_h * track->audiopk_size); | |
1840 track->audio_timestamp = | |
1841 malloc(track->sub_packet_h * sizeof(float)); | |
1842 break; | |
1843 case mmioFOURCC('s', 'i', 'p', 'r'): | |
1844 sh_a->wf->nAvgBytesPerSec = sipr_fl2bps[flavor]; | |
1845 sh_a->wf->nBlockAlign = track->coded_framesize; | |
1846 track->audio_buf = | |
1847 malloc(track->sub_packet_h * track->audiopk_size); | |
1848 track->audio_timestamp = | |
1849 malloc(track->sub_packet_h * sizeof(float)); | |
1850 break; | |
1851 case mmioFOURCC('2', '8', '_', '8'): | |
1852 sh_a->wf->nAvgBytesPerSec = 3600; | |
1853 sh_a->wf->nBlockAlign = track->coded_framesize; | |
1854 track->audio_buf = | |
1855 malloc(track->sub_packet_h * track->audiopk_size); | |
1856 track->audio_timestamp = | |
1857 malloc(track->sub_packet_h * sizeof(float)); | |
1858 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1859 } |
31177 | 1860 |
1861 track->realmedia = 1; | |
1862 } else if (!strcmp(track->codec_id, MKV_A_FLAC) | |
1863 || (track->a_formattag == 0xf1ac)) { | |
1864 unsigned char *ptr; | |
1865 int size; | |
1866 free(sh_a->wf); | |
1867 sh_a->wf = NULL; | |
1868 | |
1869 if (track->a_formattag == mmioFOURCC('f', 'L', 'a', 'C')) { | |
1870 ptr = track->private_data; | |
1871 size = track->private_size; | |
1872 } else { | |
1873 sh_a->format = mmioFOURCC('f', 'L', 'a', 'C'); | |
31210
8314717ef431
Cast void* pointer to appropriate type before using it in an addition; fixes
diego
parents:
31177
diff
changeset
|
1874 ptr = (unsigned char *) track->private_data + sizeof(WAVEFORMATEX); |
31177 | 1875 size = track->private_size - sizeof(WAVEFORMATEX); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1876 } |
31177 | 1877 if (size < 4 || ptr[0] != 'f' || ptr[1] != 'L' || ptr[2] != 'a' |
1878 || ptr[3] != 'C') { | |
1879 dp = new_demux_packet(4); | |
1880 memcpy(dp->buffer, "fLaC", 4); | |
1881 } else { | |
1882 dp = new_demux_packet(size); | |
1883 memcpy(dp->buffer, ptr, size); | |
1884 } | |
1885 dp->pts = 0; | |
1886 dp->flags = 0; | |
1887 ds_add_packet(demuxer->audio, dp); | |
1888 } else if (track->a_formattag == mmioFOURCC('W', 'V', 'P', 'K') || | |
1889 track->a_formattag == mmioFOURCC('T', 'R', 'H', 'D')) { | |
1890 /* do nothing, still works */ | |
1891 } else if (!track->ms_compat | |
1892 || (track->private_size < sizeof(WAVEFORMATEX))) { | |
1893 free_sh_audio(demuxer, track->tnum); | |
1894 return 1; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1895 } |
31177 | 1896 |
1897 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1898 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1899 |
31177 | 1900 static int demux_mkv_open_sub(demuxer_t *demuxer, mkv_track_t *track, |
1901 int sid) | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1902 { |
31177 | 1903 if (track->subtitle_type != MATROSKA_SUBTYPE_UNKNOWN) { |
1904 int size, m; | |
1905 uint8_t *buffer; | |
1906 sh_sub_t *sh = new_sh_sub_sid(demuxer, track->tnum, sid); | |
1907 track->sh_sub = sh; | |
1908 sh->type = 't'; | |
1909 if (track->subtitle_type == MATROSKA_SUBTYPE_VOBSUB) | |
1910 sh->type = 'v'; | |
1911 if (track->subtitle_type == MATROSKA_SUBTYPE_SSA) | |
1912 sh->type = 'a'; | |
1913 size = track->private_size; | |
1914 m = demux_mkv_decode(track, track->private_data, &buffer, &size, 2); | |
1915 if (buffer && m) { | |
1916 free(track->private_data); | |
1917 track->private_data = buffer; | |
1918 track->private_size = size; | |
25651
3d5fb36693ac
Factorize private data decoding for subtitle tracks in mkv demuxer.
eugeni
parents:
25432
diff
changeset
|
1919 } |
31177 | 1920 sh->extradata = malloc(track->private_size); |
1921 memcpy(sh->extradata, track->private_data, track->private_size); | |
1922 sh->extradata_len = track->private_size; | |
1923 if (track->language && (strcmp(track->language, "und") != 0)) | |
1924 sh->lang = strdup(track->language); | |
1925 sh->default_track = track->default_track; | |
1926 } else { | |
1927 mp_msg(MSGT_DEMUX, MSGL_ERR, | |
1928 MSGTR_MPDEMUX_MKV_SubtitleTypeNotSupported, track->codec_id); | |
1929 return 1; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1930 } |
31177 | 1931 |
1932 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1933 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1934 |
31177 | 1935 static int demux_mkv_open(demuxer_t *demuxer) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1936 { |
31177 | 1937 stream_t *s = demuxer->stream; |
1938 mkv_demuxer_t *mkv_d; | |
1939 mkv_track_t *track; | |
1940 int i, version, cont = 0; | |
1941 char *str; | |
1942 | |
1943 stream_seek(s, s->start_pos); | |
1944 str = ebml_read_header(s, &version); | |
31263 | 1945 if (str == NULL || (strcmp(str, "matroska") && strcmp(str, "webm")) || version > 2) { |
31177 | 1946 mp_msg(MSGT_DEMUX, MSGL_DBG2, "[mkv] no head found\n"); |
1947 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1948 } |
31177 | 1949 free(str); |
1950 | |
1951 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] Found the head...\n"); | |
1952 | |
1953 if (ebml_read_id(s, NULL) != MATROSKA_ID_SEGMENT) { | |
1954 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] but no segment :(\n"); | |
1955 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1956 } |
31177 | 1957 ebml_read_length(s, NULL); /* return bytes number until EOF */ |
1958 | |
1959 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] + a segment...\n"); | |
1960 | |
1961 mkv_d = calloc(1, sizeof(mkv_demuxer_t)); | |
1962 demuxer->priv = mkv_d; | |
1963 mkv_d->tc_scale = 1000000; | |
1964 mkv_d->segment_start = stream_tell(s); | |
1965 mkv_d->parsed_cues = malloc(sizeof(off_t)); | |
1966 mkv_d->parsed_seekhead = malloc(sizeof(off_t)); | |
1967 | |
1968 while (!cont) { | |
1969 switch (ebml_read_id(s, NULL)) { | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1970 case MATROSKA_ID_INFO: |
31177 | 1971 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] |+ segment information...\n"); |
1972 cont = demux_mkv_read_info(demuxer); | |
1973 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1974 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1975 case MATROSKA_ID_TRACKS: |
31177 | 1976 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] |+ segment tracks...\n"); |
1977 cont = demux_mkv_read_tracks(demuxer); | |
1978 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1979 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1980 case MATROSKA_ID_CUES: |
31177 | 1981 cont = demux_mkv_read_cues(demuxer); |
1982 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1983 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1984 case MATROSKA_ID_TAGS: |
31177 | 1985 cont = demux_mkv_read_tags(demuxer); |
1986 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1987 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1988 case MATROSKA_ID_SEEKHEAD: |
31177 | 1989 cont = demux_mkv_read_seekhead(demuxer); |
1990 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1991 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1992 case MATROSKA_ID_CHAPTERS: |
31177 | 1993 cont = demux_mkv_read_chapters(demuxer); |
1994 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
1995 |
18937
9e95ac641e77
Initial libass release (without mencoder support).
eugeni
parents:
18934
diff
changeset
|
1996 case MATROSKA_ID_ATTACHMENTS: |
31177 | 1997 cont = demux_mkv_read_attachments(demuxer); |
1998 break; | |
18937
9e95ac641e77
Initial libass release (without mencoder support).
eugeni
parents:
18934
diff
changeset
|
1999 |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2000 case MATROSKA_ID_CLUSTER: |
31177 | 2001 { |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2002 int p, l; |
31177 | 2003 mp_msg(MSGT_DEMUX, MSGL_V, |
2004 "[mkv] |+ found cluster, headers are " | |
2005 "parsed completely :)\n"); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2006 /* get the first cluster timecode */ |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2007 p = stream_tell(s); |
31177 | 2008 l = ebml_read_length(s, NULL); |
2009 while (ebml_read_id(s, NULL) != MATROSKA_ID_CLUSTERTIMECODE) { | |
2010 ebml_read_skip(s, NULL); | |
2011 if (stream_tell(s) >= p + l) | |
2012 break; | |
2013 } | |
2014 if (stream_tell(s) < p + l) { | |
2015 uint64_t num = ebml_read_uint(s, NULL); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2016 if (num == EBML_UINT_INVALID) |
31177 | 2017 return 0; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2018 mkv_d->first_tc = num * mkv_d->tc_scale / 1000000.0; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2019 mkv_d->has_first_tc = 1; |
31177 | 2020 } |
2021 stream_seek(s, p - 4); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2022 cont = 1; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2023 break; |
31177 | 2024 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2025 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2026 default: |
31177 | 2027 cont = 1; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2028 case EBML_ID_VOID: |
31177 | 2029 ebml_read_skip(s, NULL); |
18754
e0077bdf5ee5
feed any audio track present in the mux; switch to any of the available audio tracks
nicodvb
parents:
18708
diff
changeset
|
2030 break; |
e0077bdf5ee5
feed any audio track present in the mux; switch to any of the available audio tracks
nicodvb
parents:
18708
diff
changeset
|
2031 } |
e0077bdf5ee5
feed any audio track present in the mux; switch to any of the available audio tracks
nicodvb
parents:
18708
diff
changeset
|
2032 } |
e0077bdf5ee5
feed any audio track present in the mux; switch to any of the available audio tracks
nicodvb
parents:
18708
diff
changeset
|
2033 |
31177 | 2034 display_create_tracks(demuxer); |
2035 | |
2036 /* select video track */ | |
2037 track = NULL; | |
2038 if (demuxer->video->id == -1) { /* automatically select a video track */ | |
2039 /* search for a video track that has the 'default' flag set */ | |
2040 for (i = 0; i < mkv_d->num_tracks; i++) | |
2041 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_VIDEO | |
2042 && mkv_d->tracks[i]->default_track) { | |
2043 track = mkv_d->tracks[i]; | |
2044 break; | |
2045 } | |
2046 | |
2047 if (track == NULL) | |
2048 /* no track has the 'default' flag set */ | |
2049 /* let's take the first video track */ | |
2050 for (i = 0; i < mkv_d->num_tracks; i++) | |
2051 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_VIDEO) { | |
2052 track = mkv_d->tracks[i]; | |
2053 break; | |
2054 } | |
2055 } else if (demuxer->video->id != -2) /* -2 = no video at all */ | |
2056 track = demux_mkv_find_track_by_num(mkv_d, demuxer->video->id, | |
2057 MATROSKA_TRACK_VIDEO); | |
2058 | |
2059 if (track && demuxer->v_streams[track->tnum]) { | |
2060 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_WillPlayVideoTrack, | |
2061 track->tnum); | |
2062 demuxer->video->id = track->tnum; | |
2063 demuxer->video->sh = demuxer->v_streams[track->tnum]; | |
2064 } else { | |
2065 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_NoVideoTrackFound); | |
2066 demuxer->video->id = -2; | |
2067 } | |
2068 | |
2069 /* select audio track */ | |
2070 track = NULL; | |
2071 if (track == NULL) | |
2072 /* search for an audio track that has the 'default' flag set */ | |
2073 for (i = 0; i < mkv_d->num_tracks; i++) | |
2074 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_AUDIO | |
2075 && mkv_d->tracks[i]->default_track) { | |
2076 track = mkv_d->tracks[i]; | |
2077 break; | |
2078 } | |
2079 | |
2080 if (track == NULL) | |
2081 /* no track has the 'default' flag set */ | |
2082 /* let's take the first audio track */ | |
2083 for (i = 0; i < mkv_d->num_tracks; i++) | |
2084 if (mkv_d->tracks[i]->type == MATROSKA_TRACK_AUDIO) { | |
2085 track = mkv_d->tracks[i]; | |
2086 break; | |
2087 } | |
2088 | |
2089 if (track && demuxer->a_streams[track->tnum]) { | |
2090 demuxer->audio->id = track->tnum; | |
2091 demuxer->audio->sh = demuxer->a_streams[track->tnum]; | |
2092 } else { | |
2093 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_MPDEMUX_MKV_NoAudioTrackFound); | |
2094 demuxer->audio->id = -2; | |
2095 } | |
2096 | |
2097 | |
2098 if (demuxer->audio->id != -2) | |
2099 for (i = 0; i < mkv_d->num_tracks; i++) { | |
2100 if (mkv_d->tracks[i]->type != MATROSKA_TRACK_AUDIO) | |
2101 continue; | |
2102 if (demuxer->a_streams[track->tnum]) { | |
2103 mkv_d->last_aid++; | |
2104 if (mkv_d->last_aid == MAX_A_STREAMS) | |
2105 break; | |
2106 } | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2107 } |
31177 | 2108 |
2109 if (demuxer->chapters) { | |
2110 for (i = 0; i < (int) demuxer->num_chapters; i++) { | |
2111 demuxer->chapters[i].start -= mkv_d->first_tc; | |
2112 demuxer->chapters[i].end -= mkv_d->first_tc; | |
2113 } | |
2114 if (dvd_last_chapter > 0 && dvd_last_chapter <= demuxer->num_chapters) { | |
2115 if (demuxer->chapters[dvd_last_chapter - 1].end != 0) | |
2116 mkv_d->stop_timecode = | |
2117 demuxer->chapters[dvd_last_chapter - 1].end; | |
2118 else if (dvd_last_chapter + 1 <= demuxer->num_chapters) | |
2119 mkv_d->stop_timecode = | |
2120 demuxer->chapters[dvd_last_chapter].start; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2121 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2122 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2123 |
31177 | 2124 if (s->end_pos == 0 || (mkv_d->indexes == NULL && index_mode < 0)) |
2125 demuxer->seekable = 0; | |
2126 else { | |
2127 demuxer->movi_start = s->start_pos; | |
2128 demuxer->movi_end = s->end_pos; | |
2129 demuxer->seekable = 1; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2130 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2131 |
31177 | 2132 return DEMUXER_TYPE_MATROSKA; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2133 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2134 |
31177 | 2135 static void demux_close_mkv(demuxer_t *demuxer) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2136 { |
31177 | 2137 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; |
2138 | |
2139 if (mkv_d) { | |
2140 int i; | |
2141 free_cached_dps(demuxer); | |
2142 if (mkv_d->tracks) { | |
2143 for (i = 0; i < mkv_d->num_tracks; i++) | |
2144 demux_mkv_free_trackentry(mkv_d->tracks[i]); | |
2145 free(mkv_d->tracks); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2146 } |
31177 | 2147 free(mkv_d->indexes); |
2148 free(mkv_d->cluster_positions); | |
2149 free(mkv_d->parsed_cues); | |
2150 free(mkv_d->parsed_seekhead); | |
2151 free(mkv_d); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2152 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2153 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2154 |
31177 | 2155 static int demux_mkv_read_block_lacing(uint8_t *buffer, uint64_t *size, |
2156 uint8_t *laces, | |
2157 uint32_t **all_lace_sizes) | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2158 { |
31177 | 2159 uint32_t total = 0, *lace_size; |
2160 uint8_t flags; | |
2161 int i; | |
2162 | |
2163 *all_lace_sizes = NULL; | |
2164 lace_size = NULL; | |
2165 /* lacing flags */ | |
2166 flags = *buffer++; | |
2167 (*size)--; | |
2168 | |
2169 switch ((flags & 0x06) >> 1) { | |
2170 case 0: /* no lacing */ | |
2171 *laces = 1; | |
2172 lace_size = calloc(*laces, sizeof(uint32_t)); | |
2173 lace_size[0] = *size; | |
2174 break; | |
2175 | |
2176 case 1: /* xiph lacing */ | |
2177 case 2: /* fixed-size lacing */ | |
2178 case 3: /* EBML lacing */ | |
2179 *laces = *buffer++; | |
2180 (*size)--; | |
2181 (*laces)++; | |
2182 lace_size = calloc(*laces, sizeof(uint32_t)); | |
2183 | |
2184 switch ((flags & 0x06) >> 1) { | |
2185 case 1: /* xiph lacing */ | |
2186 for (i = 0; i < *laces - 1; i++) { | |
2187 lace_size[i] = 0; | |
2188 do { | |
2189 lace_size[i] += *buffer; | |
2190 (*size)--; | |
2191 } while (*buffer++ == 0xFF); | |
2192 total += lace_size[i]; | |
2193 } | |
2194 lace_size[i] = *size - total; | |
2195 break; | |
2196 | |
2197 case 2: /* fixed-size lacing */ | |
2198 for (i = 0; i < *laces; i++) | |
2199 lace_size[i] = *size / *laces; | |
2200 break; | |
2201 | |
2202 case 3: /* EBML lacing */ | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2203 { |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2204 int l; |
31177 | 2205 uint64_t num = ebml_read_vlen_uint(buffer, &l); |
12065 | 2206 if (num == EBML_UINT_INVALID) { |
31177 | 2207 free(lace_size); |
2208 return 1; | |
12065 | 2209 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2210 buffer += l; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2211 *size -= l; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2212 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2213 total = lace_size[0] = num; |
31177 | 2214 for (i = 1; i < *laces - 1; i++) { |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2215 int64_t snum; |
31177 | 2216 snum = ebml_read_vlen_int(buffer, &l); |
12065 | 2217 if (snum == EBML_INT_INVALID) { |
31177 | 2218 free(lace_size); |
2219 return 1; | |
12065 | 2220 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2221 buffer += l; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2222 *size -= l; |
31177 | 2223 lace_size[i] = lace_size[i - 1] + snum; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2224 total += lace_size[i]; |
31177 | 2225 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2226 lace_size[i] = *size - total; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2227 break; |
31177 | 2228 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2229 } |
31177 | 2230 break; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2231 } |
31177 | 2232 *all_lace_sizes = lace_size; |
2233 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2234 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2235 |
31177 | 2236 static void handle_subtitles(demuxer_t *demuxer, mkv_track_t *track, |
2237 char *block, int64_t size, | |
2238 uint64_t block_duration, uint64_t timecode) | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2239 { |
31177 | 2240 demux_packet_t *dp; |
2241 | |
2242 if (block_duration == 0) { | |
2243 mp_msg(MSGT_DEMUX, MSGL_WARN, | |
2244 MSGTR_MPDEMUX_MKV_NoBlockDurationForSubtitleTrackFound); | |
2245 return; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2246 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2247 |
31177 | 2248 sub_utf8 = 1; |
2249 dp = new_demux_packet(size); | |
2250 memcpy(dp->buffer, block, size); | |
2251 dp->pts = timecode / 1000.0f; | |
2252 dp->endpts = (timecode + block_duration) / 1000.0f; | |
2253 ds_add_packet(demuxer->sub, dp); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2254 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2255 |
31177 | 2256 static void handle_realvideo(demuxer_t *demuxer, mkv_track_t *track, |
2257 uint8_t *buffer, uint32_t size, int block_bref) | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2258 { |
31177 | 2259 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; |
2260 demux_packet_t *dp; | |
2261 uint32_t timestamp = mkv_d->last_pts * 1000; | |
2262 | |
2263 dp = new_demux_packet(size); | |
2264 memcpy(dp->buffer, buffer, size); | |
2265 | |
2266 if (mkv_d->v_skip_to_keyframe) { | |
2267 dp->pts = mkv_d->last_pts; | |
2268 track->rv_kf_base = 0; | |
2269 track->rv_kf_pts = timestamp; | |
2270 } else | |
2271 dp->pts = | |
2272 real_fix_timestamp(dp->buffer, timestamp, | |
2273 ((sh_video_t *) demuxer->video->sh)->bih-> | |
2274 biCompression, &track->rv_kf_base, | |
2275 &track->rv_kf_pts, NULL); | |
2276 dp->pos = demuxer->filepos; | |
2277 dp->flags = block_bref ? 0 : 0x10; | |
2278 | |
2279 ds_add_packet(demuxer->video, dp); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2280 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2281 |
31177 | 2282 static void handle_realaudio(demuxer_t *demuxer, mkv_track_t *track, |
2283 uint8_t *buffer, uint32_t size, int block_bref) | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2284 { |
31177 | 2285 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; |
2286 int sps = track->sub_packet_size; | |
2287 int sph = track->sub_packet_h; | |
2288 int cfs = track->coded_framesize; | |
2289 int w = track->audiopk_size; | |
2290 int spc = track->sub_packet_cnt; | |
2291 demux_packet_t *dp; | |
2292 int x; | |
2293 | |
2294 if ((track->a_formattag == mmioFOURCC('2', '8', '_', '8')) | |
2295 || (track->a_formattag == mmioFOURCC('c', 'o', 'o', 'k')) | |
2296 || (track->a_formattag == mmioFOURCC('a', 't', 'r', 'c')) | |
2297 || (track->a_formattag == mmioFOURCC('s', 'i', 'p', 'r'))) { | |
18036 | 2298 // if(!block_bref) |
2299 // spc = track->sub_packet_cnt = 0; | |
31177 | 2300 switch (track->a_formattag) { |
18036 | 2301 case mmioFOURCC('2', '8', '_', '8'): |
31177 | 2302 for (x = 0; x < sph / 2; x++) |
2303 memcpy(track->audio_buf + x * 2 * w + spc * cfs, | |
2304 buffer + cfs * x, cfs); | |
2305 break; | |
18036 | 2306 case mmioFOURCC('c', 'o', 'o', 'k'): |
2307 case mmioFOURCC('a', 't', 'r', 'c'): | |
31177 | 2308 for (x = 0; x < w / sps; x++) |
2309 memcpy(track->audio_buf + | |
2310 sps * (sph * x + ((sph + 1) / 2) * (spc & 1) + | |
2311 (spc >> 1)), buffer + sps * x, sps); | |
2312 break; | |
18036 | 2313 case mmioFOURCC('s', 'i', 'p', 'r'): |
31177 | 2314 memcpy(track->audio_buf + spc * w, buffer, w); |
2315 if (spc == sph - 1) { | |
2316 int n; | |
2317 int bs = sph * w * 2 / 96; // nibbles per subpacket | |
2318 // Perform reordering | |
2319 for (n = 0; n < 38; n++) { | |
2320 int j; | |
2321 int i = bs * sipr_swaps[n][0]; | |
2322 int o = bs * sipr_swaps[n][1]; | |
2323 // swap nibbles of block 'i' with 'o' TODO: optimize | |
2324 for (j = 0; j < bs; j++) { | |
2325 int x = (i & 1) ? | |
2326 (track->audio_buf[i >> 1] >> 4) : | |
2327 (track->audio_buf[i >> 1] & 0x0F); | |
2328 int y = (o & 1) ? | |
2329 (track->audio_buf[o >> 1] >> 4) : | |
2330 (track->audio_buf[o >> 1] & 0x0F); | |
2331 if (o & 1) | |
2332 track->audio_buf[o >> 1] = | |
2333 (track->audio_buf[o >> 1] & 0x0F) | (x << 4); | |
2334 else | |
2335 track->audio_buf[o >> 1] = | |
2336 (track->audio_buf[o >> 1] & 0xF0) | x; | |
2337 if (i & 1) | |
2338 track->audio_buf[i >> 1] = | |
2339 (track->audio_buf[i >> 1] & 0x0F) | (y << 4); | |
2340 else | |
2341 track->audio_buf[i >> 1] = | |
2342 (track->audio_buf[i >> 1] & 0xF0) | y; | |
2343 ++i; | |
2344 ++o; | |
18036 | 2345 } |
2346 } | |
2347 } | |
31177 | 2348 break; |
18036 | 2349 } |
31177 | 2350 track->audio_timestamp[track->sub_packet_cnt] = |
2351 (track->ra_pts == mkv_d->last_pts) ? 0 : (mkv_d->last_pts); | |
2352 track->ra_pts = mkv_d->last_pts; | |
2353 if (track->sub_packet_cnt == 0) | |
2354 track->audio_filepos = demuxer->filepos; | |
2355 if (++(track->sub_packet_cnt) == sph) { | |
2356 int apk_usize = | |
2357 ((WAVEFORMATEX *) ((sh_audio_t *) demuxer->audio->sh)->wf)->nBlockAlign; | |
2358 track->sub_packet_cnt = 0; | |
2359 // Release all the audio packets | |
2360 for (x = 0; x < sph * w / apk_usize; x++) { | |
2361 dp = new_demux_packet(apk_usize); | |
2362 memcpy(dp->buffer, track->audio_buf + x * apk_usize, | |
2363 apk_usize); | |
2364 /* Put timestamp only on packets that correspond to original | |
2365 * audio packets in file */ | |
2366 dp->pts = (x * apk_usize % w) ? 0 : | |
2367 track->audio_timestamp[x * apk_usize / w]; | |
2368 dp->pos = track->audio_filepos; // all equal | |
2369 dp->flags = x ? 0 : 0x10; // Mark first packet as keyframe | |
2370 ds_add_packet(demuxer->audio, dp); | |
2371 } | |
2372 } | |
2373 } else { // Not a codec that require reordering | |
2374 dp = new_demux_packet(size); | |
2375 memcpy(dp->buffer, buffer, size); | |
2376 if (track->ra_pts == mkv_d->last_pts && !mkv_d->a_skip_to_keyframe) | |
2377 dp->pts = 0; | |
2378 else | |
2379 dp->pts = mkv_d->last_pts; | |
2380 track->ra_pts = mkv_d->last_pts; | |
2381 | |
2382 dp->pos = demuxer->filepos; | |
2383 dp->flags = block_bref ? 0 : 0x10; | |
2384 ds_add_packet(demuxer->audio, dp); | |
2385 } | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2386 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2387 |
14054 | 2388 /** Reorder timecodes and add cached demux packets to the queues. |
2389 * | |
2390 * Timecode reordering is needed if a video track contains B frames that | |
2391 * are timestamped in display order (e.g. MPEG-1, MPEG-2 or "native" MPEG-4). | |
2392 * MPlayer doesn't like timestamps in display order. This function adjusts | |
2393 * the timestamp of cached frames (which are exactly one I/P frame followed | |
2394 * by one or more B frames) so that they are in coding order again. | |
2395 * | |
2396 * Example: The track with 25 FPS contains four frames with the timecodes | |
2397 * I at 0ms, P at 120ms, B at 40ms and B at 80ms. As soon as the next I | |
2398 * or P frame arrives these timecodes can be changed to I at 0ms, P at 40ms, | |
2399 * B at 80ms and B at 120ms. | |
2400 * | |
16912
4dea8b8f3b65
sort timestamps instead of assuming conventional B-frame order. (fixes x264 B-pyramid)
lorenm
parents:
16877
diff
changeset
|
2401 * This works for simple H.264 B-frame pyramids, but not for arbitrary orders. |
4dea8b8f3b65
sort timestamps instead of assuming conventional B-frame order. (fixes x264 B-pyramid)
lorenm
parents:
16877
diff
changeset
|
2402 * |
14054 | 2403 * \param demuxer The Matroska demuxer struct for this instance. |
2404 * \param track The track structure whose cache should be handled. | |
2405 */ | |
31177 | 2406 static void flush_cached_dps(demuxer_t *demuxer, mkv_track_t *track) |
14054 | 2407 { |
31177 | 2408 int i, ok; |
2409 | |
2410 if (track->num_cached_dps == 0) | |
2411 return; | |
2412 | |
2413 do { | |
2414 ok = 1; | |
2415 for (i = 1; i < track->num_cached_dps; i++) | |
2416 if (track->cached_dps[i - 1]->pts > track->cached_dps[i]->pts) { | |
2417 float tmp_pts = track->cached_dps[i - 1]->pts; | |
2418 track->cached_dps[i - 1]->pts = track->cached_dps[i]->pts; | |
2419 track->cached_dps[i]->pts = tmp_pts; | |
2420 ok = 0; | |
2421 } | |
2422 } while (!ok); | |
2423 | |
2424 for (i = 0; i < track->num_cached_dps; i++) | |
2425 ds_add_packet(demuxer->video, track->cached_dps[i]); | |
2426 track->num_cached_dps = 0; | |
14054 | 2427 } |
2428 | |
2429 /** Cache video frames if timecodes have to be reordered. | |
2430 * | |
2431 * Timecode reordering is needed if a video track contains B frames that | |
2432 * are timestamped in display order (e.g. MPEG-1, MPEG-2 or "native" MPEG-4). | |
2433 * This function takes in a Matroska block read from the file, allocates a | |
2434 * demux packet for it, fills in its values, allocates space for storing | |
2435 * pointers to the cached demux packets and adds the packet to it. If | |
2436 * the packet contains an I or a P frame then ::flush_cached_dps is called | |
2437 * in order to send the old cached frames downstream. | |
2438 * | |
2439 * \param demuxer The Matroska demuxer struct for this instance. | |
2440 * \param track The packet is meant for this track. | |
2441 * \param buffer The actual frame contents. | |
2442 * \param size The frame size in bytes. | |
2443 * \param block_bref A relative timecode (backward reference). If it is \c 0 | |
2444 * then the frame is an I frame. | |
2445 * \param block_fref A relative timecode (forward reference). If it is \c 0 | |
2446 * then the frame is either an I frame or a P frame depending on the value | |
2447 * of \a block_bref. Otherwise it's a B frame. | |
2448 */ | |
31177 | 2449 static void handle_video_bframes(demuxer_t *demuxer, mkv_track_t *track, |
2450 uint8_t *buffer, uint32_t size, | |
2451 int block_bref, int block_fref) | |
14054 | 2452 { |
31177 | 2453 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; |
2454 demux_packet_t *dp; | |
2455 | |
2456 dp = new_demux_packet(size); | |
2457 memcpy(dp->buffer, buffer, size); | |
2458 dp->pos = demuxer->filepos; | |
2459 dp->pts = mkv_d->last_pts; | |
2460 if ((track->num_cached_dps > 0) && (dp->pts < track->max_pts)) | |
2461 block_fref = 1; | |
2462 if (block_fref == 0) /* I or P frame */ | |
2463 flush_cached_dps(demuxer, track); | |
2464 if (block_bref != 0) /* I frame, don't cache it */ | |
2465 dp->flags = 0x10; | |
2466 if ((track->num_cached_dps + 1) > track->num_allocated_dps) { | |
2467 track->cached_dps = (demux_packet_t **) | |
2468 realloc(track->cached_dps, | |
2469 (track->num_cached_dps + 10) * sizeof(demux_packet_t *)); | |
2470 track->num_allocated_dps += 10; | |
14054 | 2471 } |
31177 | 2472 track->cached_dps[track->num_cached_dps] = dp; |
2473 track->num_cached_dps++; | |
2474 if (dp->pts > track->max_pts) | |
2475 track->max_pts = dp->pts; | |
14054 | 2476 } |
2477 | |
31177 | 2478 static int handle_block(demuxer_t *demuxer, uint8_t *block, uint64_t length, |
2479 uint64_t block_duration, int64_t block_bref, | |
2480 int64_t block_fref, uint8_t simpleblock) | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2481 { |
31177 | 2482 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; |
2483 mkv_track_t *track = NULL; | |
2484 demux_stream_t *ds = NULL; | |
2485 uint64_t old_length; | |
2486 int64_t tc; | |
2487 uint32_t *lace_size; | |
2488 uint8_t laces, flags; | |
2489 int i, num, tmp, use_this_block = 1; | |
2490 float current_pts; | |
2491 int16_t time; | |
2492 | |
2493 /* first byte(s): track num */ | |
2494 num = ebml_read_vlen_uint(block, &tmp); | |
2495 block += tmp; | |
2496 /* time (relative to cluster time) */ | |
2497 time = block[0] << 8 | block[1]; | |
2498 block += 2; | |
2499 length -= tmp + 2; | |
2500 old_length = length; | |
2501 flags = block[0]; | |
2502 if (demux_mkv_read_block_lacing(block, &length, &laces, &lace_size)) | |
2503 return 0; | |
2504 block += old_length - length; | |
2505 | |
2506 tc = ((time * mkv_d->tc_scale + mkv_d->cluster_tc) / 1000000.0 - | |
2507 mkv_d->first_tc); | |
2508 if (tc < 0) | |
2509 tc = 0; | |
2510 if (mkv_d->stop_timecode > 0 && tc > mkv_d->stop_timecode) { | |
2511 free(lace_size); | |
2512 return -1; | |
12721
44895a7161c8
Do not dereference NULL if no track could be found for a block.
mosu
parents:
12550
diff
changeset
|
2513 } |
31177 | 2514 current_pts = tc / 1000.0; |
2515 | |
2516 for (i = 0; i < mkv_d->num_tracks; i++) | |
2517 if (mkv_d->tracks[i]->tnum == num) { | |
2518 track = mkv_d->tracks[i]; | |
2519 break; | |
19977 | 2520 } |
31177 | 2521 if (track == NULL) { |
2522 free(lace_size); | |
2523 return 1; | |
2524 } | |
2525 if (num == demuxer->audio->id) { | |
2526 ds = demuxer->audio; | |
2527 | |
2528 if (mkv_d->a_skip_to_keyframe) { | |
2529 if (simpleblock) { | |
2530 if (!(flags & 0x80)) /*current frame isn't a keyframe */ | |
2531 use_this_block = 0; | |
2532 } else if (block_bref != 0) | |
2533 use_this_block = 0; | |
2534 } else if (mkv_d->v_skip_to_keyframe) | |
2535 use_this_block = 0; | |
2536 | |
2537 if (track->fix_i_bps && use_this_block) { | |
2538 sh_audio_t *sh = (sh_audio_t *) ds->sh; | |
2539 | |
2540 if (block_duration != 0) { | |
2541 sh->i_bps = length * 1000 / block_duration; | |
2542 track->fix_i_bps = 0; | |
2543 } else if (track->qt_last_a_pts == 0.0) | |
2544 track->qt_last_a_pts = current_pts; | |
2545 else if (track->qt_last_a_pts != current_pts) { | |
2546 sh->i_bps = length / (current_pts - track->qt_last_a_pts); | |
2547 track->fix_i_bps = 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2548 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2549 } |
31177 | 2550 } else if (tc < mkv_d->skip_to_timecode) |
2551 use_this_block = 0; | |
2552 else if (num == demuxer->video->id) { | |
2553 ds = demuxer->video; | |
2554 if (mkv_d->v_skip_to_keyframe) { | |
2555 if (simpleblock) { | |
2556 if (!(flags & 0x80)) /*current frame isn't a keyframe */ | |
2557 use_this_block = 0; | |
2558 } else if (block_bref != 0 || block_fref != 0) | |
19977 | 2559 use_this_block = 0; |
31177 | 2560 } |
2561 } else if (num == demuxer->sub->id) { | |
2562 ds = demuxer->sub; | |
2563 if (track->subtitle_type != MATROSKA_SUBTYPE_VOBSUB) { | |
2564 if (!mkv_d->v_skip_to_keyframe) | |
2565 handle_subtitles(demuxer, track, block, length, block_duration, | |
2566 tc); | |
19977 | 2567 use_this_block = 0; |
2568 } | |
31177 | 2569 } else |
2570 use_this_block = 0; | |
2571 | |
2572 if (use_this_block) { | |
2573 mkv_d->last_pts = current_pts; | |
2574 mkv_d->last_filepos = demuxer->filepos; | |
2575 | |
2576 for (i = 0; i < laces; i++) { | |
2577 if (ds == demuxer->video && track->realmedia) | |
2578 handle_realvideo(demuxer, track, block, lace_size[i], | |
2579 block_bref); | |
2580 else if (ds == demuxer->audio && track->realmedia) | |
2581 handle_realaudio(demuxer, track, block, lace_size[i], | |
2582 block_bref); | |
2583 else if (ds == demuxer->video && track->reorder_timecodes) | |
2584 handle_video_bframes(demuxer, track, block, lace_size[i], | |
2585 block_bref, block_fref); | |
2586 else { | |
2587 int modified, size = lace_size[i]; | |
2588 demux_packet_t *dp; | |
2589 uint8_t *buffer; | |
2590 modified = demux_mkv_decode(track, block, &buffer, &size, 1); | |
2591 if (buffer) { | |
2592 dp = new_demux_packet(size); | |
2593 memcpy(dp->buffer, buffer, size); | |
2594 if (modified) | |
2595 free(buffer); | |
2596 dp->flags = (block_bref == 0 | |
2597 && block_fref == 0) ? 0x10 : 0; | |
2598 /* If default_duration is 0, assume no pts value is known | |
2599 * for packets after the first one (rather than all pts | |
2600 * values being the same) */ | |
2601 if (i == 0 || track->default_duration) | |
2602 dp->pts = | |
2603 mkv_d->last_pts + i * track->default_duration; | |
2604 ds_add_packet(ds, dp); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2605 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2606 } |
31177 | 2607 block += lace_size[i]; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2608 } |
31177 | 2609 |
2610 if (ds == demuxer->video) { | |
2611 mkv_d->v_skip_to_keyframe = 0; | |
2612 mkv_d->skip_to_timecode = 0; | |
2613 } else if (ds == demuxer->audio) | |
2614 mkv_d->a_skip_to_keyframe = 0; | |
2615 | |
2616 free(lace_size); | |
2617 return 1; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2618 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2619 |
31177 | 2620 free(lace_size); |
2621 return 0; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2622 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2623 |
31177 | 2624 static int demux_mkv_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2625 { |
31177 | 2626 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; |
2627 stream_t *s = demuxer->stream; | |
2628 uint64_t l; | |
2629 int il, tmp; | |
2630 | |
2631 while (1) { | |
2632 while (mkv_d->cluster_size > 0) { | |
2633 uint64_t block_duration = 0, block_length = 0; | |
2634 int64_t block_bref = 0, block_fref = 0; | |
2635 uint8_t *block = NULL; | |
2636 | |
2637 while (mkv_d->blockgroup_size > 0) { | |
2638 switch (ebml_read_id(s, &il)) { | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2639 case MATROSKA_ID_BLOCKDURATION: |
31177 | 2640 block_duration = ebml_read_uint(s, &l); |
23704 | 2641 if (block_duration == EBML_UINT_INVALID) { |
31177 | 2642 free(block); |
2643 return 0; | |
23704 | 2644 } |
19810 | 2645 block_duration *= mkv_d->tc_scale / 1000000.0; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2646 break; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2647 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2648 case MATROSKA_ID_BLOCK: |
31177 | 2649 block_length = ebml_read_length(s, &tmp); |
20155 | 2650 free(block); |
31177 | 2651 if (block_length > SIZE_MAX - AV_LZO_INPUT_PADDING) |
2652 return 0; | |
2653 block = malloc(block_length + AV_LZO_INPUT_PADDING); | |
2654 demuxer->filepos = stream_tell(s); | |
2655 if (stream_read(s, block, block_length) != | |
2656 (int) block_length) { | |
2657 free(block); | |
2658 return 0; | |
2659 } | |
2660 l = tmp + block_length; | |
2661 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2662 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2663 case MATROSKA_ID_REFERENCEBLOCK: |
31177 | 2664 { |
2665 int64_t num = ebml_read_int(s, &l); | |
23704 | 2666 if (num == EBML_INT_INVALID) { |
31177 | 2667 free(block); |
2668 return 0; | |
23704 | 2669 } |
14054 | 2670 if (num <= 0) |
31177 | 2671 block_bref = num; |
14054 | 2672 else |
31177 | 2673 block_fref = num; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2674 break; |
31177 | 2675 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2676 |
11934
bef061911390
Exit when an invalid EBML ID has been read (e.g. for files that have been cut off).
mosu
parents:
11901
diff
changeset
|
2677 case EBML_ID_INVALID: |
31177 | 2678 free(block); |
2679 return 0; | |
11934
bef061911390
Exit when an invalid EBML ID has been read (e.g. for files that have been cut off).
mosu
parents:
11901
diff
changeset
|
2680 |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2681 default: |
31177 | 2682 ebml_read_skip(s, &l); |
2683 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2684 } |
31177 | 2685 mkv_d->blockgroup_size -= l + il; |
2686 mkv_d->cluster_size -= l + il; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2687 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2688 |
31177 | 2689 if (block) { |
2690 int res = handle_block(demuxer, block, block_length, | |
2691 block_duration, block_bref, block_fref, | |
2692 0); | |
2693 free(block); | |
2694 if (res < 0) | |
2695 return 0; | |
2696 if (res) | |
2697 return 1; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2698 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2699 |
31177 | 2700 if (mkv_d->cluster_size > 0) { |
2701 switch (ebml_read_id(s, &il)) { | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2702 case MATROSKA_ID_CLUSTERTIMECODE: |
31177 | 2703 { |
2704 uint64_t num = ebml_read_uint(s, &l); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2705 if (num == EBML_UINT_INVALID) |
31177 | 2706 return 0; |
2707 if (!mkv_d->has_first_tc) { | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2708 mkv_d->first_tc = num * mkv_d->tc_scale / 1000000.0; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2709 mkv_d->has_first_tc = 1; |
31177 | 2710 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2711 mkv_d->cluster_tc = num * mkv_d->tc_scale; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2712 break; |
31177 | 2713 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2714 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2715 case MATROSKA_ID_BLOCKGROUP: |
31177 | 2716 mkv_d->blockgroup_size = ebml_read_length(s, &tmp); |
2717 l = tmp; | |
2718 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2719 |
19977 | 2720 case MATROSKA_ID_SIMPLEBLOCK: |
31177 | 2721 { |
19977 | 2722 int res; |
31177 | 2723 block_length = ebml_read_length(s, &tmp); |
2724 block = malloc(block_length); | |
2725 demuxer->filepos = stream_tell(s); | |
2726 if (stream_read(s, block, block_length) != | |
2727 (int) block_length) { | |
2728 free(block); | |
2729 return 0; | |
20155 | 2730 } |
19977 | 2731 l = tmp + block_length; |
31177 | 2732 res = handle_block(demuxer, block, block_length, |
2733 block_duration, block_bref, | |
2734 block_fref, 1); | |
2735 free(block); | |
19977 | 2736 mkv_d->cluster_size -= l + il; |
2737 if (res < 0) | |
31177 | 2738 return 0; |
19977 | 2739 else if (res) |
31177 | 2740 return 1; |
2741 else | |
2742 mkv_d->cluster_size += l + il; | |
19977 | 2743 break; |
31177 | 2744 } |
11934
bef061911390
Exit when an invalid EBML ID has been read (e.g. for files that have been cut off).
mosu
parents:
11901
diff
changeset
|
2745 case EBML_ID_INVALID: |
31177 | 2746 return 0; |
11934
bef061911390
Exit when an invalid EBML ID has been read (e.g. for files that have been cut off).
mosu
parents:
11901
diff
changeset
|
2747 |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2748 default: |
31177 | 2749 ebml_read_skip(s, &l); |
2750 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2751 } |
31177 | 2752 mkv_d->cluster_size -= l + il; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2753 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2754 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2755 |
31177 | 2756 if (ebml_read_id(s, &il) != MATROSKA_ID_CLUSTER) |
2757 return 0; | |
2758 add_cluster_position(mkv_d, stream_tell(s) - il); | |
2759 mkv_d->cluster_size = ebml_read_length(s, NULL); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2760 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2761 |
31177 | 2762 return 0; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2763 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2764 |
31177 | 2765 static void demux_mkv_seek(demuxer_t *demuxer, float rel_seek_secs, |
2766 float audio_delay, int flags) | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2767 { |
31177 | 2768 free_cached_dps(demuxer); |
2769 if (!(flags & SEEK_FACTOR)) { /* time in secs */ | |
2770 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; | |
2771 stream_t *s = demuxer->stream; | |
2772 int64_t target_timecode = 0, diff, min_diff = 0xFFFFFFFFFFFFFFFLL; | |
2773 int i; | |
2774 | |
2775 if (!(flags & SEEK_ABSOLUTE)) /* relative seek */ | |
2776 target_timecode = (int64_t) (mkv_d->last_pts * 1000.0); | |
2777 target_timecode += (int64_t) (rel_seek_secs * 1000.0); | |
2778 if (target_timecode < 0) | |
2779 target_timecode = 0; | |
2780 | |
2781 if (mkv_d->indexes == NULL) { /* no index was found */ | |
2782 uint64_t target_filepos, cluster_pos, max_pos; | |
2783 | |
2784 target_filepos = | |
2785 (uint64_t) (target_timecode * mkv_d->last_filepos / | |
2786 (mkv_d->last_pts * 1000.0)); | |
2787 | |
2788 max_pos = mkv_d->num_cluster_pos ? | |
2789 mkv_d->cluster_positions[mkv_d->num_cluster_pos - 1] : 0; | |
2790 if (target_filepos > max_pos) { | |
2791 if ((off_t) max_pos > stream_tell(s)) | |
2792 stream_seek(s, max_pos); | |
2793 else | |
2794 stream_seek(s, stream_tell(s) + mkv_d->cluster_size); | |
2795 /* parse all the clusters upto target_filepos */ | |
2796 while (!s->eof && stream_tell(s) < (off_t) target_filepos) { | |
2797 switch (ebml_read_id(s, &i)) { | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2798 case MATROSKA_ID_CLUSTER: |
31177 | 2799 add_cluster_position(mkv_d, |
2800 (uint64_t) stream_tell(s) - i); | |
2801 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2802 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2803 case MATROSKA_ID_CUES: |
31177 | 2804 demux_mkv_read_cues(demuxer); |
2805 break; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2806 } |
31177 | 2807 ebml_read_skip(s, NULL); |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2808 } |
31177 | 2809 if (s->eof) |
2810 stream_reset(s); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2811 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2812 |
31177 | 2813 if (mkv_d->indexes == NULL) { |
2814 cluster_pos = mkv_d->cluster_positions[0]; | |
2815 /* Let's find the nearest cluster */ | |
2816 for (i = 0; i < mkv_d->num_cluster_pos; i++) { | |
2817 diff = mkv_d->cluster_positions[i] - target_filepos; | |
2818 if (rel_seek_secs < 0 && diff < 0 && -diff < min_diff) { | |
2819 cluster_pos = mkv_d->cluster_positions[i]; | |
2820 min_diff = -diff; | |
2821 } else if (rel_seek_secs > 0 | |
2822 && (diff < 0 ? -1 * diff : diff) < min_diff) { | |
2823 cluster_pos = mkv_d->cluster_positions[i]; | |
2824 min_diff = diff < 0 ? -1 * diff : diff; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2825 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2826 } |
31177 | 2827 mkv_d->cluster_size = mkv_d->blockgroup_size = 0; |
2828 stream_seek(s, cluster_pos); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2829 } |
31177 | 2830 } else { |
2831 mkv_index_t *index = NULL; | |
2832 int seek_id = (demuxer->video->id < 0) ? | |
2833 demuxer->audio->id : demuxer->video->id; | |
2834 | |
2835 /* let's find the entry in the indexes with the smallest */ | |
2836 /* difference to the wanted timecode. */ | |
2837 for (i = 0; i < mkv_d->num_indexes; i++) | |
2838 if (mkv_d->indexes[i].tnum == seek_id) { | |
2839 diff = | |
2840 target_timecode + mkv_d->first_tc - | |
2841 (int64_t) mkv_d->indexes[i].timecode * | |
2842 mkv_d->tc_scale / 1000000.0; | |
2843 | |
2844 if ((flags & SEEK_ABSOLUTE | |
2845 || target_timecode <= mkv_d->last_pts * 1000)) { | |
2846 // Absolute seek or seek backward: find the last index | |
2847 // position before target time | |
2848 if (diff < 0 || diff >= min_diff) | |
2849 continue; | |
2850 } else { | |
2851 // Relative seek forward: find the first index position | |
2852 // after target time. If no such index exists, find last | |
2853 // position between current position and target time. | |
2854 if (diff <= 0) { | |
2855 if (min_diff <= 0 && diff <= min_diff) | |
2856 continue; | |
2857 } else if (diff >= | |
2858 FFMIN(target_timecode - mkv_d->last_pts, | |
2859 min_diff)) | |
22616 | 2860 continue; |
2861 } | |
31177 | 2862 min_diff = diff; |
2863 index = mkv_d->indexes + i; | |
22616 | 2864 } |
31177 | 2865 |
2866 if (index) { /* We've found an entry. */ | |
2867 mkv_d->cluster_size = mkv_d->blockgroup_size = 0; | |
2868 stream_seek(s, index->filepos); | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2869 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2870 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2871 |
31177 | 2872 if (demuxer->video->id >= 0) |
2873 mkv_d->v_skip_to_keyframe = 1; | |
2874 if (rel_seek_secs > 0.0) | |
2875 mkv_d->skip_to_timecode = target_timecode; | |
2876 mkv_d->a_skip_to_keyframe = 1; | |
2877 | |
2878 demux_mkv_fill_buffer(demuxer, NULL); | |
2879 } else if ((demuxer->movi_end <= 0) || !(flags & SEEK_ABSOLUTE)) | |
2880 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] seek unsupported flags\n"); | |
2881 else { | |
2882 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; | |
2883 stream_t *s = demuxer->stream; | |
2884 uint64_t target_filepos; | |
2885 mkv_index_t *index = NULL; | |
2886 int i; | |
2887 | |
2888 if (mkv_d->indexes == NULL) { /* no index was found *//* I'm lazy... */ | |
2889 mp_msg(MSGT_DEMUX, MSGL_V, "[mkv] seek unsupported flags\n"); | |
2890 return; | |
12073 | 2891 } |
2892 | |
31177 | 2893 target_filepos = (uint64_t) (demuxer->movi_end * rel_seek_secs); |
2894 for (i = 0; i < mkv_d->num_indexes; i++) | |
2895 if (mkv_d->indexes[i].tnum == demuxer->video->id) | |
2896 if ((index == NULL) | |
2897 || ((mkv_d->indexes[i].filepos >= target_filepos) | |
2898 && ((index->filepos < target_filepos) | |
2899 || (mkv_d->indexes[i].filepos < index->filepos)))) | |
2900 index = &mkv_d->indexes[i]; | |
2901 | |
2902 if (!index) | |
2903 return; | |
2904 | |
2905 mkv_d->cluster_size = mkv_d->blockgroup_size = 0; | |
2906 stream_seek(s, index->filepos); | |
2907 | |
2908 if (demuxer->video->id >= 0) | |
2909 mkv_d->v_skip_to_keyframe = 1; | |
2910 mkv_d->skip_to_timecode = index->timecode; | |
2911 mkv_d->a_skip_to_keyframe = 1; | |
2912 | |
2913 demux_mkv_fill_buffer(demuxer, NULL); | |
12073 | 2914 } |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2915 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2916 |
31177 | 2917 static int demux_mkv_control(demuxer_t *demuxer, int cmd, void *arg) |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2918 { |
31177 | 2919 mkv_demuxer_t *mkv_d = (mkv_demuxer_t *) demuxer->priv; |
2920 | |
2921 switch (cmd) { | |
25919
382672c7480a
Allow demuxers to choose a default value for correct_pts
reimar
parents:
25883
diff
changeset
|
2922 case DEMUXER_CTRL_CORRECT_PTS: |
31177 | 2923 return DEMUXER_CTRL_OK; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2924 case DEMUXER_CTRL_GET_TIME_LENGTH: |
31177 | 2925 if (mkv_d->duration == 0) |
2926 return DEMUXER_CTRL_DONTKNOW; | |
2927 | |
2928 *((double *) arg) = (double) mkv_d->duration; | |
2929 return DEMUXER_CTRL_OK; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2930 |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2931 case DEMUXER_CTRL_GET_PERCENT_POS: |
31177 | 2932 if (mkv_d->duration == 0) { |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2933 return DEMUXER_CTRL_DONTKNOW; |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2934 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2935 |
31177 | 2936 *((int *) arg) = (int) (100 * mkv_d->last_pts / mkv_d->duration); |
2937 return DEMUXER_CTRL_OK; | |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2938 |
15154
898f68adad2b
Online audio switching now supports Matroska too. Patch by Michael Behrisch
gpoirier
parents:
14843
diff
changeset
|
2939 case DEMUXER_CTRL_SWITCH_AUDIO: |
31177 | 2940 if (demuxer->audio && demuxer->audio->sh) { |
2941 sh_audio_t *sh = demuxer->a_streams[demuxer->audio->id]; | |
2942 int aid = *(int *) arg; | |
2943 if (aid < 0) | |
2944 aid = (sh->aid + 1) % mkv_d->last_aid; | |
2945 if (aid != sh->aid) { | |
2946 mkv_track_t *track = | |
2947 demux_mkv_find_track_by_num(mkv_d, aid, | |
2948 MATROSKA_TRACK_AUDIO); | |
2949 if (track) { | |
2950 demuxer->audio->id = track->tnum; | |
2951 sh = demuxer->a_streams[demuxer->audio->id]; | |
2952 ds_free_packs(demuxer->audio); | |
2953 } | |
2954 } | |
2955 *(int *) arg = sh->aid; | |
2956 } else | |
2957 *(int *) arg = -2; | |
2958 return DEMUXER_CTRL_OK; | |
15154
898f68adad2b
Online audio switching now supports Matroska too. Patch by Michael Behrisch
gpoirier
parents:
14843
diff
changeset
|
2959 |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2960 default: |
31177 | 2961 return DEMUXER_CTRL_NOTIMPL; |
11807
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2962 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2963 } |
9a81d7b4c0b6
Added the new C based Matroska demuxer by Aurelien Jacobs.
mosu
parents:
diff
changeset
|
2964 |
25707
d4fe6e23283e
Make all demuxer_desc_t const, thus moving them to .rodata
reimar
parents:
25658
diff
changeset
|
2965 const demuxer_desc_t demuxer_desc_matroska = { |
31177 | 2966 "Matroska demuxer", |
2967 "mkv", | |
2968 "Matroska", | |
2969 "Aurelien Jacobs", | |
2970 "", | |
2971 DEMUXER_TYPE_MATROSKA, | |
2972 1, // safe autodetect | |
2973 demux_mkv_open, | |
2974 demux_mkv_fill_buffer, | |
2975 NULL, | |
2976 demux_close_mkv, | |
2977 demux_mkv_seek, | |
2978 demux_mkv_control | |
16175 | 2979 }; |