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