Commit | Line | Data |
---|---|---|
6cea494e | 1 | /* |
7fbde343 | 2 | * MOV demuxer |
406792e7 | 3 | * Copyright (c) 2001 Fabrice Bellard |
7c4502c8 | 4 | * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com> |
6cea494e | 5 | * |
ac9362c5 DB |
6 | * first version by Francois Revol <revol@free.fr> |
7 | * seek function by Gael Chardon <gael.dev@4now.net> | |
8 | * | |
2912e87a | 9 | * This file is part of Libav. |
b78e7197 | 10 | * |
2912e87a | 11 | * Libav is free software; you can redistribute it and/or |
19720f15 FB |
12 | * modify it under the terms of the GNU Lesser General Public |
13 | * License as published by the Free Software Foundation; either | |
b78e7197 | 14 | * version 2.1 of the License, or (at your option) any later version. |
6cea494e | 15 | * |
2912e87a | 16 | * Libav is distributed in the hope that it will be useful, |
6cea494e | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19720f15 FB |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19 | * Lesser General Public License for more details. | |
6cea494e | 20 | * |
19720f15 | 21 | * You should have received a copy of the GNU Lesser General Public |
2912e87a | 22 | * License along with Libav; if not, write to the Free Software |
5509bffa | 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
6cea494e | 24 | */ |
d957696f | 25 | |
d92024f1 | 26 | #include <inttypes.h> |
d957696f | 27 | #include <limits.h> |
8f8bc923 | 28 | #include <stdint.h> |
115329f1 | 29 | |
30b1961c | 30 | #include "libavutil/attributes.h" |
a903f8f0 | 31 | #include "libavutil/channel_layout.h" |
6a5d31ac | 32 | #include "libavutil/intreadwrite.h" |
3383a53e | 33 | #include "libavutil/intfloat.h" |
0ebcdf5c | 34 | #include "libavutil/mathematics.h" |
82ee7d0d | 35 | #include "libavutil/time_internal.h" |
49674dd0 | 36 | #include "libavutil/avstring.h" |
d2d67e42 | 37 | #include "libavutil/dict.h" |
e352b293 | 38 | #include "libavutil/opt.h" |
3798205a | 39 | #include "libavcodec/ac3tab.h" |
6cea494e | 40 | #include "avformat.h" |
c3f9ebf7 | 41 | #include "internal.h" |
e731b8d8 | 42 | #include "avio_internal.h" |
9d9f4119 | 43 | #include "riff.h" |
e40ee6a2 | 44 | #include "isom.h" |
9106a698 | 45 | #include "libavcodec/get_bits.h" |
94395fbf | 46 | #include "id3v1.h" |
1fdf18f4 | 47 | #include "mov_chan.h" |
9a07fac6 | 48 | #include "replaygain.h" |
6cea494e | 49 | |
b250f9c6 | 50 | #if CONFIG_ZLIB |
0147f198 FR |
51 | #include <zlib.h> |
52 | #endif | |
53 | ||
b595afaa MM |
54 | #include "qtpalette.h" |
55 | ||
baf25c9d | 56 | |
cd7352d5 MN |
57 | #undef NDEBUG |
58 | #include <assert.h> | |
59 | ||
6cea494e | 60 | /* those functions parse an atom */ |
6cea494e ZK |
61 | /* links atom IDs to parse functions */ |
62 | typedef struct MOVParseTableEntry { | |
0c1a9eda | 63 | uint32_t type; |
ae628ec1 | 64 | int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom); |
6cea494e ZK |
65 | } MOVParseTableEntry; |
66 | ||
1ce1e636 | 67 | static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom); |
73d07c27 | 68 | |
dff41775 RH |
69 | static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb, |
70 | unsigned len, const char *key) | |
c6026e43 BC |
71 | { |
72 | char buf[16]; | |
73 | ||
fbcaceb1 | 74 | short current, total = 0; |
b7effd4e | 75 | avio_rb16(pb); // unknown |
dff41775 | 76 | current = avio_rb16(pb); |
fbcaceb1 CEH |
77 | if (len >= 6) |
78 | total = avio_rb16(pb); | |
dff41775 RH |
79 | if (!total) |
80 | snprintf(buf, sizeof(buf), "%d", current); | |
81 | else | |
82 | snprintf(buf, sizeof(buf), "%d/%d", current, total); | |
cc3e88a2 | 83 | c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; |
dff41775 | 84 | av_dict_set(&c->fc->metadata, key, buf, 0); |
c6026e43 BC |
85 | |
86 | return 0; | |
87 | } | |
88 | ||
80951f5c RH |
89 | static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb, |
90 | unsigned len, const char *key) | |
f0556353 | 91 | { |
80951f5c | 92 | char buf[16]; |
f0556353 | 93 | |
80951f5c RH |
94 | /* bypass padding bytes */ |
95 | avio_r8(pb); | |
96 | avio_r8(pb); | |
97 | avio_r8(pb); | |
f0556353 | 98 | |
028a2375 | 99 | snprintf(buf, sizeof(buf), "%d", avio_r8(pb)); |
cc3e88a2 | 100 | c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; |
80951f5c | 101 | av_dict_set(&c->fc->metadata, key, buf, 0); |
f0556353 | 102 | |
80951f5c | 103 | return 0; |
f0556353 RH |
104 | } |
105 | ||
5da35d1c RH |
106 | static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb, |
107 | unsigned len, const char *key) | |
f0556353 | 108 | { |
5da35d1c | 109 | char buf[16]; |
f0556353 | 110 | |
028a2375 | 111 | snprintf(buf, sizeof(buf), "%d", avio_r8(pb)); |
cc3e88a2 | 112 | c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; |
5da35d1c | 113 | av_dict_set(&c->fc->metadata, key, buf, 0); |
f0556353 | 114 | |
5da35d1c | 115 | return 0; |
f0556353 RH |
116 | } |
117 | ||
94395fbf RH |
118 | static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb, |
119 | unsigned len, const char *key) | |
120 | { | |
121 | short genre; | |
122 | char buf[20]; | |
123 | ||
124 | avio_r8(pb); // unknown | |
125 | ||
126 | genre = avio_r8(pb); | |
127 | if (genre < 1 || genre > ID3v1_GENRE_MAX) | |
128 | return 0; | |
129 | snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]); | |
cc3e88a2 | 130 | c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; |
94395fbf RH |
131 | av_dict_set(&c->fc->metadata, key, buf, 0); |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
08a186c6 BC |
136 | static const uint32_t mac_to_unicode[128] = { |
137 | 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1, | |
138 | 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8, | |
139 | 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3, | |
140 | 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC, | |
141 | 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF, | |
142 | 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8, | |
143 | 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211, | |
144 | 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8, | |
145 | 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB, | |
146 | 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153, | |
147 | 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA, | |
148 | 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02, | |
149 | 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1, | |
150 | 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4, | |
151 | 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC, | |
152 | 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7, | |
153 | }; | |
154 | ||
ae628ec1 | 155 | static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len, |
08a186c6 BC |
156 | char *dst, int dstlen) |
157 | { | |
158 | char *p = dst; | |
159 | char *end = dst+dstlen-1; | |
160 | int i; | |
161 | ||
162 | for (i = 0; i < len; i++) { | |
b7effd4e | 163 | uint8_t t, c = avio_r8(pb); |
08a186c6 BC |
164 | if (c < 0x80 && p < end) |
165 | *p++ = c; | |
166 | else | |
167 | PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;); | |
168 | } | |
169 | *p = 0; | |
170 | return p - dst; | |
171 | } | |
172 | ||
a02b8c69 AK |
173 | static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len) |
174 | { | |
175 | AVPacket pkt; | |
176 | AVStream *st; | |
177 | MOVStreamContext *sc; | |
36ef5369 | 178 | enum AVCodecID id; |
a02b8c69 AK |
179 | int ret; |
180 | ||
181 | switch (type) { | |
36ef5369 AK |
182 | case 0xd: id = AV_CODEC_ID_MJPEG; break; |
183 | case 0xe: id = AV_CODEC_ID_PNG; break; | |
184 | case 0x1b: id = AV_CODEC_ID_BMP; break; | |
a02b8c69 AK |
185 | default: |
186 | av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type); | |
187 | avio_skip(pb, len); | |
188 | return 0; | |
189 | } | |
190 | ||
191 | st = avformat_new_stream(c->fc, NULL); | |
192 | if (!st) | |
193 | return AVERROR(ENOMEM); | |
194 | sc = av_mallocz(sizeof(*sc)); | |
195 | if (!sc) | |
196 | return AVERROR(ENOMEM); | |
197 | st->priv_data = sc; | |
198 | ||
199 | ret = av_get_packet(pb, &pkt, len); | |
200 | if (ret < 0) | |
201 | return ret; | |
202 | ||
203 | st->disposition |= AV_DISPOSITION_ATTACHED_PIC; | |
204 | ||
205 | st->attached_pic = pkt; | |
206 | st->attached_pic.stream_index = st->index; | |
207 | st->attached_pic.flags |= AV_PKT_FLAG_KEY; | |
208 | ||
209 | st->codec->codec_type = AVMEDIA_TYPE_VIDEO; | |
210 | st->codec->codec_id = id; | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
e7d6d0bf MS |
215 | static int mov_metadata_loci(MOVContext *c, AVIOContext *pb, unsigned len) |
216 | { | |
217 | char language[4] = { 0 }; | |
218 | char buf[100]; | |
219 | uint16_t langcode = 0; | |
18fb38fb | 220 | double longitude, latitude; |
e7d6d0bf MS |
221 | const char *key = "location"; |
222 | ||
223 | if (len < 4 + 2 + 1 + 1 + 4 + 4 + 4) | |
224 | return AVERROR_INVALIDDATA; | |
225 | ||
226 | avio_skip(pb, 4); // version+flags | |
227 | langcode = avio_rb16(pb); | |
228 | ff_mov_lang_to_iso639(langcode, language); | |
229 | len -= 6; | |
230 | ||
231 | len -= avio_get_str(pb, len, buf, sizeof(buf)); // place name | |
232 | if (len < 1) | |
233 | return AVERROR_INVALIDDATA; | |
234 | avio_skip(pb, 1); // role | |
235 | len -= 1; | |
236 | ||
237 | if (len < 14) | |
238 | return AVERROR_INVALIDDATA; | |
239 | longitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16); | |
240 | latitude = ((int32_t) avio_rb32(pb)) / (float) (1 << 16); | |
e7d6d0bf MS |
241 | |
242 | // Try to output in the same format as the ?xyz field | |
243 | snprintf(buf, sizeof(buf), "%+08.4f%+09.4f/", latitude, longitude); | |
244 | if (*language && strcmp(language, "und")) { | |
245 | char key2[16]; | |
246 | snprintf(key2, sizeof(key2), "%s-%s", key, language); | |
247 | av_dict_set(&c->fc->metadata, key2, buf, 0); | |
248 | } | |
cc3e88a2 | 249 | c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; |
e7d6d0bf MS |
250 | return av_dict_set(&c->fc->metadata, key, buf, 0); |
251 | } | |
252 | ||
ae628ec1 | 253 | static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
0fb44a4e | 254 | { |
0fb44a4e | 255 | char tmp_key[5]; |
3cec81f4 | 256 | char *str, key2[32], language[4] = {0}; |
0fb44a4e | 257 | const char *key = NULL; |
a7b8ff94 | 258 | uint16_t langcode = 0; |
3cec81f4 | 259 | uint32_t data_type = 0, str_size, str_size_alloc; |
dff41775 | 260 | int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL; |
174c5fde | 261 | int raw = 0; |
0fb44a4e BC |
262 | |
263 | switch (atom.type) { | |
174c5fde VG |
264 | case MKTAG( '@','P','R','M'): key = "premiere_version"; raw = 1; break; |
265 | case MKTAG( '@','P','R','Q'): key = "quicktime_version"; raw = 1; break; | |
b704b648 VG |
266 | case MKTAG( 'X','M','P','_'): |
267 | if (c->export_xmp) { key = "xmp"; raw = 1; } break; | |
2804d320 | 268 | case MKTAG( 'a','A','R','T'): key = "album_artist"; break; |
e2e07dba VG |
269 | case MKTAG( 'a','k','I','D'): key = "account_type"; |
270 | parse = mov_metadata_int8_no_padding; break; | |
271 | case MKTAG( 'a','p','I','D'): key = "account_id"; break; | |
272 | case MKTAG( 'c','a','t','g'): key = "category"; break; | |
273 | case MKTAG( 'c','p','i','l'): key = "compilation"; | |
274 | parse = mov_metadata_int8_no_padding; break; | |
35384934 VG |
275 | case MKTAG( 'c','p','r','t'): key = "copyright"; break; |
276 | case MKTAG( 'd','e','s','c'): key = "description"; break; | |
277 | case MKTAG( 'd','i','s','k'): key = "disc"; | |
278 | parse = mov_metadata_track_or_disc_number; break; | |
e2e07dba VG |
279 | case MKTAG( 'e','g','i','d'): key = "episode_uid"; |
280 | parse = mov_metadata_int8_no_padding; break; | |
94395fbf RH |
281 | case MKTAG( 'g','n','r','e'): key = "genre"; |
282 | parse = mov_metadata_gnre; break; | |
35384934 VG |
283 | case MKTAG( 'h','d','v','d'): key = "hd_video"; |
284 | parse = mov_metadata_int8_no_padding; break; | |
e2e07dba | 285 | case MKTAG( 'k','e','y','w'): key = "keywords"; break; |
7382902b | 286 | case MKTAG( 'l','d','e','s'): key = "synopsis"; break; |
35384934 VG |
287 | case MKTAG( 'l','o','c','i'): |
288 | return mov_metadata_loci(c, pb, atom.size); | |
e2e07dba VG |
289 | case MKTAG( 'p','c','s','t'): key = "podcast"; |
290 | parse = mov_metadata_int8_no_padding; break; | |
35384934 VG |
291 | case MKTAG( 'p','g','a','p'): key = "gapless_playback"; |
292 | parse = mov_metadata_int8_no_padding; break; | |
e2e07dba VG |
293 | case MKTAG( 'p','u','r','d'): key = "purchase_date"; break; |
294 | case MKTAG( 'r','t','n','g'): key = "rating"; | |
295 | parse = mov_metadata_int8_no_padding; break; | |
296 | case MKTAG( 's','o','a','a'): key = "sort_album_artist"; break; | |
297 | case MKTAG( 's','o','a','l'): key = "sort_album"; break; | |
298 | case MKTAG( 's','o','a','r'): key = "sort_artist"; break; | |
299 | case MKTAG( 's','o','c','o'): key = "sort_composer"; break; | |
300 | case MKTAG( 's','o','n','m'): key = "sort_name"; break; | |
301 | case MKTAG( 's','o','s','n'): key = "sort_show"; break; | |
35384934 VG |
302 | case MKTAG( 's','t','i','k'): key = "media_type"; |
303 | parse = mov_metadata_int8_no_padding; break; | |
c6026e43 | 304 | case MKTAG( 't','r','k','n'): key = "track"; |
dff41775 | 305 | parse = mov_metadata_track_or_disc_number; break; |
35384934 | 306 | case MKTAG( 't','v','e','n'): key = "episode_id"; break; |
f0556353 | 307 | case MKTAG( 't','v','e','s'): key = "episode_sort"; |
80951f5c | 308 | parse = mov_metadata_int8_bypass_padding; break; |
35384934 VG |
309 | case MKTAG( 't','v','n','n'): key = "network"; break; |
310 | case MKTAG( 't','v','s','h'): key = "show"; break; | |
f0556353 | 311 | case MKTAG( 't','v','s','n'): key = "season_number"; |
80951f5c | 312 | parse = mov_metadata_int8_bypass_padding; break; |
35384934 | 313 | case MKTAG(0xa9,'A','R','T'): key = "artist"; break; |
3c01039e | 314 | case MKTAG(0xa9,'P','R','D'): key = "producer"; break; |
35384934 VG |
315 | case MKTAG(0xa9,'a','l','b'): key = "album"; break; |
316 | case MKTAG(0xa9,'a','u','t'): key = "artist"; break; | |
3c01039e | 317 | case MKTAG(0xa9,'c','h','p'): key = "chapter"; break; |
35384934 | 318 | case MKTAG(0xa9,'c','m','t'): key = "comment"; break; |
3c01039e | 319 | case MKTAG(0xa9,'c','o','m'): key = "composer"; break; |
35384934 VG |
320 | case MKTAG(0xa9,'c','p','y'): key = "copyright"; break; |
321 | case MKTAG(0xa9,'d','a','y'): key = "date"; break; | |
3c01039e DR |
322 | case MKTAG(0xa9,'d','i','r'): key = "director"; break; |
323 | case MKTAG(0xa9,'d','i','s'): key = "disclaimer"; break; | |
324 | case MKTAG(0xa9,'e','d','1'): key = "edit_date"; break; | |
35384934 | 325 | case MKTAG(0xa9,'e','n','c'): key = "encoder"; break; |
3c01039e | 326 | case MKTAG(0xa9,'f','m','t'): key = "original_format"; break; |
35384934 | 327 | case MKTAG(0xa9,'g','e','n'): key = "genre"; break; |
e2e07dba | 328 | case MKTAG(0xa9,'g','r','p'): key = "grouping"; break; |
3c01039e | 329 | case MKTAG(0xa9,'h','s','t'): key = "host_computer"; break; |
35384934 | 330 | case MKTAG(0xa9,'i','n','f'): key = "comment"; break; |
e2e07dba | 331 | case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break; |
3c01039e DR |
332 | case MKTAG(0xa9,'m','a','k'): key = "make"; break; |
333 | case MKTAG(0xa9,'m','o','d'): key = "model"; break; | |
35384934 | 334 | case MKTAG(0xa9,'n','a','m'): key = "title"; break; |
3c01039e DR |
335 | case MKTAG(0xa9,'o','p','e'): key = "original_artist"; break; |
336 | case MKTAG(0xa9,'p','r','d'): key = "producer"; break; | |
337 | case MKTAG(0xa9,'p','r','f'): key = "performers"; break; | |
338 | case MKTAG(0xa9,'r','e','q'): key = "playback_requirements"; break; | |
339 | case MKTAG(0xa9,'s','r','c'): key = "original_source"; break; | |
e2e07dba | 340 | case MKTAG(0xa9,'s','t','3'): key = "subtitle"; break; |
35384934 VG |
341 | case MKTAG(0xa9,'s','w','r'): key = "encoder"; break; |
342 | case MKTAG(0xa9,'t','o','o'): key = "encoder"; break; | |
3c01039e DR |
343 | case MKTAG(0xa9,'t','r','k'): key = "track"; break; |
344 | case MKTAG(0xa9,'u','r','l'): key = "URL"; break; | |
345 | case MKTAG(0xa9,'w','r','n'): key = "warning"; break; | |
35384934 VG |
346 | case MKTAG(0xa9,'w','r','t'): key = "composer"; break; |
347 | case MKTAG(0xa9,'x','y','z'): key = "location"; break; | |
0fb44a4e BC |
348 | } |
349 | ||
350 | if (c->itunes_metadata && atom.size > 8) { | |
b7effd4e AK |
351 | int data_size = avio_rb32(pb); |
352 | int tag = avio_rl32(pb); | |
0fb44a4e | 353 | if (tag == MKTAG('d','a','t','a')) { |
b7effd4e AK |
354 | data_type = avio_rb32(pb); // type |
355 | avio_rb32(pb); // unknown | |
0fb44a4e BC |
356 | str_size = data_size - 16; |
357 | atom.size -= 16; | |
a02b8c69 AK |
358 | |
359 | if (atom.type == MKTAG('c', 'o', 'v', 'r')) { | |
360 | int ret = mov_read_covr(c, pb, data_type, str_size); | |
361 | if (ret < 0) { | |
362 | av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n"); | |
363 | return ret; | |
364 | } | |
365 | } | |
0fb44a4e | 366 | } else return 0; |
174c5fde | 367 | } else if (atom.size > 4 && key && !c->itunes_metadata && !raw) { |
b7effd4e AK |
368 | str_size = avio_rb16(pb); // string length |
369 | langcode = avio_rb16(pb); | |
08a186c6 | 370 | ff_mov_lang_to_iso639(langcode, language); |
0fb44a4e BC |
371 | atom.size -= 4; |
372 | } else | |
373 | str_size = atom.size; | |
374 | ||
e352b293 | 375 | if (c->export_all && !key) { |
0fb44a4e BC |
376 | snprintf(tmp_key, 5, "%.4s", (char*)&atom.type); |
377 | key = tmp_key; | |
378 | } | |
0fb44a4e BC |
379 | |
380 | if (!key) | |
381 | return 0; | |
382 | if (atom.size < 0) | |
bcd5d979 | 383 | return AVERROR_INVALIDDATA; |
0fb44a4e | 384 | |
3cec81f4 | 385 | // allocate twice as much as worst-case |
6f4364ab | 386 | str_size_alloc = (raw ? str_size : str_size * 2) + 1; |
3cec81f4 TB |
387 | str = av_malloc(str_size_alloc); |
388 | if (!str) | |
389 | return AVERROR(ENOMEM); | |
c6026e43 BC |
390 | |
391 | if (parse) | |
dff41775 | 392 | parse(c, pb, str_size, key); |
c6026e43 | 393 | else { |
174c5fde | 394 | if (!raw && (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff)))) { // MAC Encoded |
3cec81f4 | 395 | mov_read_mac_string(c, pb, str_size, str, str_size_alloc); |
08a186c6 | 396 | } else { |
b7effd4e | 397 | avio_read(pb, str, str_size); |
08a186c6 BC |
398 | str[str_size] = 0; |
399 | } | |
cc3e88a2 | 400 | c->fc->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED; |
d2d67e42 | 401 | av_dict_set(&c->fc->metadata, key, str, 0); |
f584386d BC |
402 | if (*language && strcmp(language, "und")) { |
403 | snprintf(key2, sizeof(key2), "%s-%s", key, language); | |
d2d67e42 | 404 | av_dict_set(&c->fc->metadata, key2, str, 0); |
f584386d | 405 | } |
c6026e43 | 406 | } |
1a3eb042 VG |
407 | av_log(c->fc, AV_LOG_TRACE, "lang \"%3s\" ", language); |
408 | av_log(c->fc, AV_LOG_TRACE, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n", | |
3cec81f4 | 409 | key, str, (char*)&atom.type, str_size_alloc, atom.size); |
0fb44a4e | 410 | |
3cec81f4 | 411 | av_freep(&str); |
0fb44a4e BC |
412 | return 0; |
413 | } | |
873358e5 | 414 | |
ae628ec1 | 415 | static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1cf9f6ed DC |
416 | { |
417 | int64_t start; | |
105b3785 | 418 | int i, nb_chapters, str_len, version; |
1cf9f6ed DC |
419 | char str[256+1]; |
420 | ||
421 | if ((atom.size -= 5) < 0) | |
422 | return 0; | |
423 | ||
b7effd4e AK |
424 | version = avio_r8(pb); |
425 | avio_rb24(pb); | |
105b3785 | 426 | if (version) |
b7effd4e AK |
427 | avio_rb32(pb); // ??? |
428 | nb_chapters = avio_r8(pb); | |
1cf9f6ed DC |
429 | |
430 | for (i = 0; i < nb_chapters; i++) { | |
431 | if (atom.size < 9) | |
432 | return 0; | |
433 | ||
b7effd4e AK |
434 | start = avio_rb64(pb); |
435 | str_len = avio_r8(pb); | |
1cf9f6ed DC |
436 | |
437 | if ((atom.size -= 9+str_len) < 0) | |
438 | return 0; | |
439 | ||
b7effd4e | 440 | avio_read(pb, str, str_len); |
1cf9f6ed | 441 | str[str_len] = 0; |
1fa395e4 | 442 | avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str); |
1cf9f6ed DC |
443 | } |
444 | return 0; | |
445 | } | |
446 | ||
8cc2fa1e | 447 | #define MIN_DATA_ENTRY_BOX_SIZE 12 |
ae628ec1 | 448 | static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
221e21b7 | 449 | { |
6a63ff19 BC |
450 | AVStream *st; |
451 | MOVStreamContext *sc; | |
221e21b7 BC |
452 | int entries, i, j; |
453 | ||
6a63ff19 BC |
454 | if (c->fc->nb_streams < 1) |
455 | return 0; | |
456 | st = c->fc->streams[c->fc->nb_streams-1]; | |
457 | sc = st->priv_data; | |
458 | ||
b7effd4e AK |
459 | avio_rb32(pb); // version + flags |
460 | entries = avio_rb32(pb); | |
8cc2fa1e JG |
461 | if (entries > (atom.size - 1) / MIN_DATA_ENTRY_BOX_SIZE + 1 || |
462 | entries >= UINT_MAX / sizeof(*sc->drefs)) | |
bcd5d979 | 463 | return AVERROR_INVALIDDATA; |
4ebd422c | 464 | av_free(sc->drefs); |
221e21b7 | 465 | sc->drefs = av_mallocz(entries * sizeof(*sc->drefs)); |
0d8f0abf BC |
466 | if (!sc->drefs) |
467 | return AVERROR(ENOMEM); | |
468 | sc->drefs_count = entries; | |
221e21b7 BC |
469 | |
470 | for (i = 0; i < sc->drefs_count; i++) { | |
c3e92a6c | 471 | MOVDref *dref = &sc->drefs[i]; |
b7effd4e | 472 | uint32_t size = avio_rb32(pb); |
a2704c97 | 473 | int64_t next = avio_tell(pb) + size - 4; |
221e21b7 | 474 | |
0e7d436d | 475 | if (size < 12) |
bcd5d979 | 476 | return AVERROR_INVALIDDATA; |
0e7d436d | 477 | |
b7effd4e AK |
478 | dref->type = avio_rl32(pb); |
479 | avio_rb32(pb); // version + flags | |
1a3eb042 | 480 | av_log(c->fc, AV_LOG_TRACE, "type %.4s size %d\n", (char*)&dref->type, size); |
221e21b7 BC |
481 | |
482 | if (dref->type == MKTAG('a','l','i','s') && size > 150) { | |
483 | /* macintosh alias record */ | |
484 | uint16_t volume_len, len; | |
221e21b7 BC |
485 | int16_t type; |
486 | ||
45a8a02a | 487 | avio_skip(pb, 10); |
221e21b7 | 488 | |
b7effd4e | 489 | volume_len = avio_r8(pb); |
221e21b7 | 490 | volume_len = FFMIN(volume_len, 27); |
b7effd4e | 491 | avio_read(pb, dref->volume, 27); |
adeb9071 BC |
492 | dref->volume[volume_len] = 0; |
493 | av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len); | |
221e21b7 | 494 | |
45a8a02a | 495 | avio_skip(pb, 12); |
adeb9071 | 496 | |
b7effd4e | 497 | len = avio_r8(pb); |
adeb9071 | 498 | len = FFMIN(len, 63); |
b7effd4e | 499 | avio_read(pb, dref->filename, 63); |
adeb9071 BC |
500 | dref->filename[len] = 0; |
501 | av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len); | |
502 | ||
45a8a02a | 503 | avio_skip(pb, 16); |
adeb9071 BC |
504 | |
505 | /* read next level up_from_alias/down_to_target */ | |
b7effd4e AK |
506 | dref->nlvl_from = avio_rb16(pb); |
507 | dref->nlvl_to = avio_rb16(pb); | |
adeb9071 BC |
508 | av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n", |
509 | dref->nlvl_from, dref->nlvl_to); | |
510 | ||
45a8a02a | 511 | avio_skip(pb, 16); |
221e21b7 | 512 | |
a2704c97 | 513 | for (type = 0; type != -1 && avio_tell(pb) < next; ) { |
9db67bed MN |
514 | if (pb->eof_reached) |
515 | return AVERROR_EOF; | |
b7effd4e AK |
516 | type = avio_rb16(pb); |
517 | len = avio_rb16(pb); | |
221e21b7 BC |
518 | av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len); |
519 | if (len&1) | |
520 | len += 1; | |
376589e5 | 521 | if (type == 2 || type == 18) { // absolute path |
dbb37657 | 522 | av_free(dref->path); |
221e21b7 | 523 | dref->path = av_mallocz(len+1); |
2f4568e5 BC |
524 | if (!dref->path) |
525 | return AVERROR(ENOMEM); | |
b7effd4e | 526 | avio_read(pb, dref->path, len); |
376589e5 VG |
527 | if (type == 18) // no additional processing needed |
528 | continue; | |
adeb9071 | 529 | if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) { |
221e21b7 BC |
530 | len -= volume_len; |
531 | memmove(dref->path, dref->path+volume_len, len); | |
532 | dref->path[len] = 0; | |
533 | } | |
534 | for (j = 0; j < len; j++) | |
535 | if (dref->path[j] == ':') | |
536 | dref->path[j] = '/'; | |
537 | av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path); | |
adeb9071 BC |
538 | } else if (type == 0) { // directory name |
539 | av_free(dref->dir); | |
540 | dref->dir = av_malloc(len+1); | |
541 | if (!dref->dir) | |
542 | return AVERROR(ENOMEM); | |
b7effd4e | 543 | avio_read(pb, dref->dir, len); |
adeb9071 BC |
544 | dref->dir[len] = 0; |
545 | for (j = 0; j < len; j++) | |
546 | if (dref->dir[j] == ':') | |
547 | dref->dir[j] = '/'; | |
548 | av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir); | |
221e21b7 | 549 | } else |
45a8a02a | 550 | avio_skip(pb, len); |
221e21b7 BC |
551 | } |
552 | } | |
6b4aa5da | 553 | avio_seek(pb, next, SEEK_SET); |
221e21b7 BC |
554 | } |
555 | return 0; | |
556 | } | |
557 | ||
ae628ec1 | 558 | static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
6cea494e | 559 | { |
fefe43ff | 560 | AVStream *st; |
0c1a9eda | 561 | uint32_t type; |
5e1166b3 | 562 | uint32_t av_unused ctype; |
e3528d2a HL |
563 | int64_t title_size; |
564 | char *title_str; | |
b6a17df4 | 565 | |
fefe43ff AC |
566 | if (c->fc->nb_streams < 1) // meta before first trak |
567 | return 0; | |
568 | ||
569 | st = c->fc->streams[c->fc->nb_streams-1]; | |
570 | ||
b7effd4e AK |
571 | avio_r8(pb); /* version */ |
572 | avio_rb24(pb); /* flags */ | |
6cea494e ZK |
573 | |
574 | /* component type */ | |
b7effd4e AK |
575 | ctype = avio_rl32(pb); |
576 | type = avio_rl32(pb); /* component subtype */ | |
6cea494e | 577 | |
1a3eb042 VG |
578 | av_log(c->fc, AV_LOG_TRACE, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype); |
579 | av_log(c->fc, AV_LOG_TRACE, "stype= %.4s\n", (char*)&type); | |
2229a5c6 | 580 | |
37ffe34b | 581 | if (type == MKTAG('v','i','d','e')) |
72415b2a | 582 | st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
5a7ba586 | 583 | else if (type == MKTAG('s','o','u','n')) |
72415b2a | 584 | st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
5a7ba586 | 585 | else if (type == MKTAG('m','1','a',' ')) |
36ef5369 | 586 | st->codec->codec_id = AV_CODEC_ID_MP2; |
5a7ba586 | 587 | else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p'))) |
72415b2a | 588 | st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; |
f05783c2 | 589 | |
b7effd4e AK |
590 | avio_rb32(pb); /* component manufacture */ |
591 | avio_rb32(pb); /* component flags */ | |
592 | avio_rb32(pb); /* component flags mask */ | |
6cea494e | 593 | |
e3528d2a HL |
594 | title_size = atom.size - 24; |
595 | if (title_size > 0) { | |
596 | title_str = av_malloc(title_size + 1); /* Add null terminator */ | |
597 | if (!title_str) | |
598 | return AVERROR(ENOMEM); | |
599 | avio_read(pb, title_str, title_size); | |
600 | title_str[title_size] = 0; | |
601 | if (title_str[0]) { | |
602 | int off = (!c->isom && title_str[0] == title_size - 1); | |
603 | av_dict_set(&st->metadata, "handler_name", title_str + off, 0); | |
604 | } | |
605 | av_freep(&title_str); | |
606 | } | |
607 | ||
6cea494e ZK |
608 | return 0; |
609 | } | |
610 | ||
86dfcfd0 | 611 | int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb) |
5cd62665 | 612 | { |
6a63ff19 | 613 | AVStream *st; |
e65ab9d9 | 614 | int tag; |
b6a17df4 | 615 | |
2d423666 | 616 | if (fc->nb_streams < 1) |
6a63ff19 | 617 | return 0; |
2d423666 | 618 | st = fc->streams[fc->nb_streams-1]; |
6a63ff19 | 619 | |
b7effd4e | 620 | avio_rb32(pb); /* version + flags */ |
e65ab9d9 | 621 | ff_mp4_read_descr(fc, pb, &tag); |
5cd62665 | 622 | if (tag == MP4ESDescrTag) { |
ad6c7c1b | 623 | ff_mp4_parse_es_descr(pb, NULL); |
5cd62665 | 624 | } else |
b7effd4e | 625 | avio_rb16(pb); /* ID */ |
5cd62665 | 626 | |
e65ab9d9 | 627 | ff_mp4_read_descr(fc, pb, &tag); |
798c6fac BC |
628 | if (tag == MP4DecConfigDescrTag) |
629 | ff_mp4_read_dec_config_descr(fc, st, pb); | |
5cd62665 ZK |
630 | return 0; |
631 | } | |
632 | ||
ae628ec1 | 633 | static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2d423666 | 634 | { |
86dfcfd0 | 635 | return ff_mov_read_esds(c->fc, pb); |
2d423666 PR |
636 | } |
637 | ||
ae628ec1 | 638 | static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
bdecdd2b JR |
639 | { |
640 | AVStream *st; | |
32125781 | 641 | enum AVAudioServiceType *ast; |
eb81cf86 | 642 | int ac3info, acmod, lfeon, bsmod; |
bdecdd2b | 643 | |
6d19fd5c BC |
644 | if (c->fc->nb_streams < 1) |
645 | return 0; | |
bdecdd2b JR |
646 | st = c->fc->streams[c->fc->nb_streams-1]; |
647 | ||
32125781 AK |
648 | ast = (enum AVAudioServiceType*)ff_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE, |
649 | sizeof(*ast)); | |
650 | if (!ast) | |
651 | return AVERROR(ENOMEM); | |
652 | ||
b7effd4e | 653 | ac3info = avio_rb24(pb); |
eb81cf86 | 654 | bsmod = (ac3info >> 14) & 0x7; |
bdecdd2b JR |
655 | acmod = (ac3info >> 11) & 0x7; |
656 | lfeon = (ac3info >> 10) & 0x1; | |
657 | st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon; | |
3798205a JR |
658 | st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod]; |
659 | if (lfeon) | |
660 | st->codec->channel_layout |= AV_CH_LOW_FREQUENCY; | |
32125781 | 661 | *ast = bsmod; |
eb81cf86 | 662 | if (st->codec->channels > 1 && bsmod == 0x7) |
32125781 AK |
663 | *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE; |
664 | ||
665 | st->codec->audio_service_type = *ast; | |
bdecdd2b JR |
666 | |
667 | return 0; | |
668 | } | |
669 | ||
546adc1f YN |
670 | static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
671 | { | |
672 | AVStream *st; | |
32125781 | 673 | enum AVAudioServiceType *ast; |
546adc1f YN |
674 | int eac3info, acmod, lfeon, bsmod; |
675 | ||
676 | if (c->fc->nb_streams < 1) | |
677 | return 0; | |
678 | st = c->fc->streams[c->fc->nb_streams-1]; | |
679 | ||
32125781 AK |
680 | ast = (enum AVAudioServiceType*)ff_stream_new_side_data(st, AV_PKT_DATA_AUDIO_SERVICE_TYPE, |
681 | sizeof(*ast)); | |
682 | if (!ast) | |
683 | return AVERROR(ENOMEM); | |
684 | ||
546adc1f YN |
685 | /* No need to parse fields for additional independent substreams and its |
686 | * associated dependent substreams since libavcodec's E-AC-3 decoder | |
687 | * does not support them yet. */ | |
688 | avio_rb16(pb); /* data_rate and num_ind_sub */ | |
689 | eac3info = avio_rb24(pb); | |
690 | bsmod = (eac3info >> 12) & 0x1f; | |
691 | acmod = (eac3info >> 9) & 0x7; | |
692 | lfeon = (eac3info >> 8) & 0x1; | |
693 | st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod]; | |
694 | if (lfeon) | |
695 | st->codec->channel_layout |= AV_CH_LOW_FREQUENCY; | |
696 | st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout); | |
32125781 | 697 | *ast = bsmod; |
546adc1f | 698 | if (st->codec->channels > 1 && bsmod == 0x7) |
32125781 AK |
699 | *ast = AV_AUDIO_SERVICE_TYPE_KARAOKE; |
700 | ||
701 | st->codec->audio_service_type = *ast; | |
546adc1f YN |
702 | |
703 | return 0; | |
704 | } | |
705 | ||
1fdf18f4 JR |
706 | static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
707 | { | |
708 | AVStream *st; | |
1fdf18f4 JR |
709 | |
710 | if (c->fc->nb_streams < 1) | |
711 | return 0; | |
712 | st = c->fc->streams[c->fc->nb_streams-1]; | |
713 | ||
714 | if (atom.size < 16) | |
715 | return 0; | |
716 | ||
8c0a865a MB |
717 | /* skip version and flags */ |
718 | avio_skip(pb, 4); | |
719 | ||
9afb7061 | 720 | ff_mov_read_chan(c->fc, pb, st, atom.size - 4); |
1fdf18f4 JR |
721 | |
722 | return 0; | |
723 | } | |
724 | ||
85e9e3a9 AC |
725 | static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
726 | { | |
727 | AVStream *st; | |
728 | ||
729 | if (c->fc->nb_streams < 1) | |
730 | return 0; | |
731 | st = c->fc->streams[c->fc->nb_streams-1]; | |
732 | ||
2007082d | 733 | return ff_get_wav_header(pb, st->codec, atom.size); |
85e9e3a9 AC |
734 | } |
735 | ||
ae628ec1 | 736 | static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
6da54074 | 737 | { |
b7effd4e AK |
738 | const int num = avio_rb32(pb); |
739 | const int den = avio_rb32(pb); | |
6a63ff19 BC |
740 | AVStream *st; |
741 | ||
742 | if (c->fc->nb_streams < 1) | |
743 | return 0; | |
744 | st = c->fc->streams[c->fc->nb_streams-1]; | |
745 | ||
9044dd83 BC |
746 | if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default |
747 | (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) { | |
748 | av_log(c->fc, AV_LOG_WARNING, | |
749 | "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n", | |
750 | st->sample_aspect_ratio.num, st->sample_aspect_ratio.den, | |
751 | num, den); | |
752 | } else if (den != 0) { | |
6da54074 BC |
753 | st->sample_aspect_ratio.num = num; |
754 | st->sample_aspect_ratio.den = den; | |
755 | } | |
756 | return 0; | |
757 | } | |
758 | ||
5ca1d879 | 759 | /* this atom contains actual media data */ |
ae628ec1 | 760 | static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
6cea494e | 761 | { |
5a7ba586 | 762 | if (atom.size == 0) /* wrong one (MP4) */ |
5ca1d879 ZK |
763 | return 0; |
764 | c->found_mdat=1; | |
5ca1d879 ZK |
765 | return 0; /* now go for moov */ |
766 | } | |
767 | ||
7b5252ce | 768 | /* read major brand, minor version and compatible brands and store them as metadata */ |
ae628ec1 | 769 | static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
4ea28253 | 770 | { |
7b5252ce | 771 | uint32_t minor_ver; |
772 | int comp_brand_size; | |
773 | char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */ | |
774 | char* comp_brands_str; | |
775 | uint8_t type[5] = {0}; | |
776 | ||
b7effd4e | 777 | avio_read(pb, type, 4); |
a42bf191 | 778 | if (strcmp(type, "qt ")) |
152e9a43 | 779 | c->isom = 1; |
a512446e | 780 | av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type); |
d2d67e42 | 781 | av_dict_set(&c->fc->metadata, "major_brand", type, 0); |
b7effd4e | 782 | minor_ver = avio_rb32(pb); /* minor version */ |
d92024f1 | 783 | snprintf(minor_ver_str, sizeof(minor_ver_str), "%"PRIu32"", minor_ver); |
d2d67e42 | 784 | av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0); |
7b5252ce | 785 | |
786 | comp_brand_size = atom.size - 8; | |
787 | if (comp_brand_size < 0) | |
bcd5d979 | 788 | return AVERROR_INVALIDDATA; |
7b5252ce | 789 | comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */ |
790 | if (!comp_brands_str) | |
791 | return AVERROR(ENOMEM); | |
b7effd4e | 792 | avio_read(pb, comp_brands_str, comp_brand_size); |
7b5252ce | 793 | comp_brands_str[comp_brand_size] = 0; |
d2d67e42 | 794 | av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0); |
7b5252ce | 795 | av_freep(&comp_brands_str); |
796 | ||
4ea28253 BC |
797 | return 0; |
798 | } | |
799 | ||
5ca1d879 | 800 | /* this atom should contain all header atoms */ |
ae628ec1 | 801 | static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
5ca1d879 | 802 | { |
bcd5d979 DH |
803 | int ret; |
804 | ||
805 | if ((ret = mov_read_default(c, pb, atom)) < 0) | |
806 | return ret; | |
5ca1d879 ZK |
807 | /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */ |
808 | /* so we don't parse the whole file if over a network */ | |
809 | c->found_moov=1; | |
5ca1d879 ZK |
810 | return 0; /* now go for mdat */ |
811 | } | |
812 | ||
ae628ec1 | 813 | static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
61aedb0f | 814 | { |
20f95f21 | 815 | c->fragment.moof_offset = c->fragment.implicit_offset = avio_tell(pb) - 8; |
1a3eb042 | 816 | av_log(c->fc, AV_LOG_TRACE, "moof offset %"PRIx64"\n", c->fragment.moof_offset); |
61aedb0f BC |
817 | return mov_read_default(c, pb, atom); |
818 | } | |
5ca1d879 | 819 | |
d2d67e42 | 820 | static void mov_metadata_creation_time(AVDictionary **metadata, time_t time) |
7df22143 AJ |
821 | { |
822 | char buffer[32]; | |
823 | if (time) { | |
82ee7d0d | 824 | struct tm *ptm, tmbuf; |
7df22143 | 825 | time -= 2082844800; /* seconds between 1904-01-01 and Epoch */ |
82ee7d0d | 826 | ptm = gmtime_r(&time, &tmbuf); |
5e2202d6 | 827 | if (!ptm) return; |
9dcf2397 MS |
828 | if (strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm)) |
829 | av_dict_set(metadata, "creation_time", buffer, 0); | |
7df22143 AJ |
830 | } |
831 | } | |
832 | ||
ae628ec1 | 833 | static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
5ca1d879 | 834 | { |
6a63ff19 BC |
835 | AVStream *st; |
836 | MOVStreamContext *sc; | |
837 | int version; | |
49674dd0 | 838 | char language[4] = {0}; |
dce25564 | 839 | unsigned lang; |
7df22143 | 840 | time_t creation_time; |
5ca1d879 | 841 | |
6a63ff19 BC |
842 | if (c->fc->nb_streams < 1) |
843 | return 0; | |
844 | st = c->fc->streams[c->fc->nb_streams-1]; | |
845 | sc = st->priv_data; | |
846 | ||
31931520 MS |
847 | if (sc->time_scale) { |
848 | av_log(c->fc, AV_LOG_ERROR, "Multiple mdhd?\n"); | |
849 | return AVERROR_INVALIDDATA; | |
850 | } | |
851 | ||
b7effd4e | 852 | version = avio_r8(pb); |
bcd5d979 | 853 | if (version > 1) { |
1ecdf891 | 854 | avpriv_request_sample(c->fc, "Version %d", version); |
bcd5d979 DH |
855 | return AVERROR_PATCHWELCOME; |
856 | } | |
b7effd4e | 857 | avio_rb24(pb); /* flags */ |
f444b977 | 858 | if (version == 1) { |
b7effd4e AK |
859 | creation_time = avio_rb64(pb); |
860 | avio_rb64(pb); | |
f444b977 | 861 | } else { |
b7effd4e AK |
862 | creation_time = avio_rb32(pb); |
863 | avio_rb32(pb); /* modification time */ | |
f444b977 | 864 | } |
7df22143 | 865 | mov_metadata_creation_time(&st->metadata, creation_time); |
5ca1d879 | 866 | |
b7effd4e AK |
867 | sc->time_scale = avio_rb32(pb); |
868 | st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */ | |
5ca1d879 | 869 | |
b7effd4e | 870 | lang = avio_rb16(pb); /* language */ |
49674dd0 | 871 | if (ff_mov_lang_to_iso639(lang, language)) |
d2d67e42 | 872 | av_dict_set(&st->metadata, "language", language, 0); |
b7effd4e | 873 | avio_rb16(pb); /* quality */ |
5ca1d879 ZK |
874 | |
875 | return 0; | |
876 | } | |
877 | ||
ae628ec1 | 878 | static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
5ca1d879 | 879 | { |
7df22143 | 880 | time_t creation_time; |
b7effd4e AK |
881 | int version = avio_r8(pb); /* version */ |
882 | avio_rb24(pb); /* flags */ | |
5ca1d879 | 883 | |
1175561e | 884 | if (version == 1) { |
b7effd4e AK |
885 | creation_time = avio_rb64(pb); |
886 | avio_rb64(pb); | |
1175561e | 887 | } else { |
b7effd4e AK |
888 | creation_time = avio_rb32(pb); |
889 | avio_rb32(pb); /* modification time */ | |
1175561e | 890 | } |
7df22143 | 891 | mov_metadata_creation_time(&c->fc->metadata, creation_time); |
b7effd4e | 892 | c->time_scale = avio_rb32(pb); /* time scale */ |
f8c18cd7 | 893 | |
1a3eb042 | 894 | av_log(c->fc, AV_LOG_TRACE, "time scale = %i\n", c->time_scale); |
f8c18cd7 | 895 | |
b7effd4e AK |
896 | c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */ |
897 | avio_rb32(pb); /* preferred scale */ | |
5ca1d879 | 898 | |
b7effd4e | 899 | avio_rb16(pb); /* preferred volume */ |
5ca1d879 | 900 | |
45a8a02a | 901 | avio_skip(pb, 10); /* reserved */ |
5ca1d879 | 902 | |
45a8a02a | 903 | avio_skip(pb, 36); /* display matrix */ |
5ca1d879 | 904 | |
b7effd4e AK |
905 | avio_rb32(pb); /* preview time */ |
906 | avio_rb32(pb); /* preview duration */ | |
907 | avio_rb32(pb); /* poster time */ | |
908 | avio_rb32(pb); /* selection time */ | |
909 | avio_rb32(pb); /* selection duration */ | |
910 | avio_rb32(pb); /* current time */ | |
911 | avio_rb32(pb); /* next track ID */ | |
5ca1d879 ZK |
912 | |
913 | return 0; | |
914 | } | |
915 | ||
ae628ec1 | 916 | static int mov_read_smi(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
9ed83b0a | 917 | { |
6a63ff19 BC |
918 | AVStream *st; |
919 | ||
920 | if (c->fc->nb_streams < 1) | |
921 | return 0; | |
922 | st = c->fc->streams[c->fc->nb_streams-1]; | |
9ed83b0a | 923 | |
5a7ba586 | 924 | if ((uint64_t)atom.size > (1<<30)) |
bcd5d979 | 925 | return AVERROR_INVALIDDATA; |
115329f1 | 926 | |
9ed83b0a ZK |
927 | // currently SVQ3 decoder expect full STSD header - so let's fake it |
928 | // this should be fixed and just SMI header should be passed | |
01f4895c | 929 | av_free(st->codec->extradata); |
b014dd76 BC |
930 | st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE); |
931 | if (!st->codec->extradata) | |
932 | return AVERROR(ENOMEM); | |
17871a02 BC |
933 | st->codec->extradata_size = 0x5a + atom.size; |
934 | memcpy(st->codec->extradata, "SVQ3", 4); // fake | |
b7effd4e | 935 | avio_read(pb, st->codec->extradata + 0x5a, atom.size); |
1a3eb042 | 936 | av_log(c->fc, AV_LOG_TRACE, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a); |
9ed83b0a ZK |
937 | return 0; |
938 | } | |
5ca1d879 | 939 | |
ae628ec1 | 940 | static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
de23f234 | 941 | { |
6a63ff19 BC |
942 | AVStream *st; |
943 | int little_endian; | |
944 | ||
945 | if (c->fc->nb_streams < 1) | |
946 | return 0; | |
947 | st = c->fc->streams[c->fc->nb_streams-1]; | |
de23f234 | 948 | |
423f5d50 | 949 | little_endian = !!avio_rb16(pb); |
1a3eb042 | 950 | av_log(c->fc, AV_LOG_TRACE, "enda %d\n", little_endian); |
fa50a027 | 951 | if (little_endian == 1) { |
de23f234 | 952 | switch (st->codec->codec_id) { |
36ef5369 AK |
953 | case AV_CODEC_ID_PCM_S24BE: |
954 | st->codec->codec_id = AV_CODEC_ID_PCM_S24LE; | |
de23f234 | 955 | break; |
36ef5369 AK |
956 | case AV_CODEC_ID_PCM_S32BE: |
957 | st->codec->codec_id = AV_CODEC_ID_PCM_S32LE; | |
de23f234 | 958 | break; |
36ef5369 AK |
959 | case AV_CODEC_ID_PCM_F32BE: |
960 | st->codec->codec_id = AV_CODEC_ID_PCM_F32LE; | |
a1ef2c4b | 961 | break; |
36ef5369 AK |
962 | case AV_CODEC_ID_PCM_F64BE: |
963 | st->codec->codec_id = AV_CODEC_ID_PCM_F64LE; | |
a1ef2c4b | 964 | break; |
de23f234 BC |
965 | default: |
966 | break; | |
967 | } | |
968 | } | |
969 | return 0; | |
970 | } | |
971 | ||
0d8a3656 VG |
972 | static int mov_read_colr(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
973 | { | |
974 | AVStream *st; | |
975 | char color_parameter_type[5] = { 0 }; | |
976 | int color_primaries, color_trc, color_matrix; | |
977 | ||
978 | if (c->fc->nb_streams < 1) | |
979 | return 0; | |
980 | st = c->fc->streams[c->fc->nb_streams - 1]; | |
981 | ||
982 | avio_read(pb, color_parameter_type, 4); | |
983 | if (strncmp(color_parameter_type, "nclx", 4) && | |
984 | strncmp(color_parameter_type, "nclc", 4)) { | |
985 | av_log(c->fc, AV_LOG_WARNING, "unsupported color_parameter_type %s\n", | |
986 | color_parameter_type); | |
987 | return 0; | |
988 | } | |
989 | ||
990 | color_primaries = avio_rb16(pb); | |
991 | color_trc = avio_rb16(pb); | |
992 | color_matrix = avio_rb16(pb); | |
993 | ||
1a3eb042 VG |
994 | av_log(c->fc, AV_LOG_TRACE, |
995 | "%s: pri %"PRIu16" trc %"PRIu16" matrix %"PRIu16"", | |
996 | color_parameter_type, color_primaries, color_trc, color_matrix); | |
0d8a3656 | 997 | |
be089af3 | 998 | if (!strncmp(color_parameter_type, "nclx", 4)) { |
0d8a3656 | 999 | uint8_t color_range = avio_r8(pb) >> 7; |
1a3eb042 | 1000 | av_log(c->fc, AV_LOG_TRACE, " full %"PRIu8"", color_range); |
0d8a3656 VG |
1001 | if (color_range) |
1002 | st->codec->color_range = AVCOL_RANGE_JPEG; | |
1003 | else | |
1004 | st->codec->color_range = AVCOL_RANGE_MPEG; | |
1005 | /* 14496-12 references JPEG XR specs (rather than the more complete | |
1006 | * 23001-8) so some adjusting is required */ | |
1007 | if (color_primaries >= AVCOL_PRI_FILM) | |
1008 | color_primaries = AVCOL_PRI_UNSPECIFIED; | |
74b02377 VG |
1009 | if ((color_trc >= AVCOL_TRC_LINEAR && |
1010 | color_trc <= AVCOL_TRC_LOG_SQRT) || | |
0d8a3656 VG |
1011 | color_trc >= AVCOL_TRC_BT2020_10) |
1012 | color_trc = AVCOL_TRC_UNSPECIFIED; | |
1013 | if (color_matrix >= AVCOL_SPC_BT2020_NCL) | |
1014 | color_matrix = AVCOL_SPC_UNSPECIFIED; | |
1015 | st->codec->color_primaries = color_primaries; | |
1016 | st->codec->color_trc = color_trc; | |
1017 | st->codec->colorspace = color_matrix; | |
be089af3 | 1018 | } else if (!strncmp(color_parameter_type, "nclc", 4)) { |
0d8a3656 VG |
1019 | /* color primaries, Table 4-4 */ |
1020 | switch (color_primaries) { | |
1021 | case 1: st->codec->color_primaries = AVCOL_PRI_BT709; break; | |
1022 | case 5: st->codec->color_primaries = AVCOL_PRI_SMPTE170M; break; | |
1023 | case 6: st->codec->color_primaries = AVCOL_PRI_SMPTE240M; break; | |
1024 | } | |
1025 | /* color transfer, Table 4-5 */ | |
1026 | switch (color_trc) { | |
1027 | case 1: st->codec->color_trc = AVCOL_TRC_BT709; break; | |
1028 | case 7: st->codec->color_trc = AVCOL_TRC_SMPTE240M; break; | |
1029 | } | |
1030 | /* color matrix, Table 4-6 */ | |
1031 | switch (color_matrix) { | |
1032 | case 1: st->codec->colorspace = AVCOL_SPC_BT709; break; | |
1033 | case 6: st->codec->colorspace = AVCOL_SPC_BT470BG; break; | |
1034 | case 7: st->codec->colorspace = AVCOL_SPC_SMPTE240M; break; | |
1035 | } | |
1036 | } | |
1a3eb042 | 1037 | av_log(c->fc, AV_LOG_TRACE, "\n"); |
0d8a3656 VG |
1038 | |
1039 | return 0; | |
1040 | } | |
1041 | ||
4bf3c8f2 AC |
1042 | static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1043 | { | |
1044 | AVStream *st; | |
1045 | unsigned mov_field_order; | |
1046 | enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN; | |
1047 | ||
1048 | if (c->fc->nb_streams < 1) // will happen with jp2 files | |
1049 | return 0; | |
1050 | st = c->fc->streams[c->fc->nb_streams-1]; | |
1051 | if (atom.size < 2) | |
1052 | return AVERROR_INVALIDDATA; | |
1053 | mov_field_order = avio_rb16(pb); | |
1054 | if ((mov_field_order & 0xFF00) == 0x0100) | |
1055 | decoded_field_order = AV_FIELD_PROGRESSIVE; | |
1056 | else if ((mov_field_order & 0xFF00) == 0x0200) { | |
1057 | switch (mov_field_order & 0xFF) { | |
1058 | case 0x01: decoded_field_order = AV_FIELD_TT; | |
1059 | break; | |
1060 | case 0x06: decoded_field_order = AV_FIELD_BB; | |
1061 | break; | |
1062 | case 0x09: decoded_field_order = AV_FIELD_TB; | |
1063 | break; | |
1064 | case 0x0E: decoded_field_order = AV_FIELD_BT; | |
1065 | break; | |
1066 | } | |
1067 | } | |
1068 | if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) { | |
1069 | av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order); | |
1070 | } | |
1071 | st->codec->field_order = decoded_field_order; | |
1072 | ||
1073 | return 0; | |
1074 | } | |
1075 | ||
014a5102 | 1076 | /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */ |
ae628ec1 | 1077 | static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
de23f234 | 1078 | { |
27134437 BC |
1079 | AVStream *st; |
1080 | uint64_t size; | |
68bc33fa | 1081 | uint8_t *buf; |
5626f994 | 1082 | int err; |
27134437 BC |
1083 | |
1084 | if (c->fc->nb_streams < 1) // will happen with jp2 files | |
1085 | return 0; | |
1086 | st= c->fc->streams[c->fc->nb_streams-1]; | |
1087 | size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE; | |
5a7ba586 | 1088 | if (size > INT_MAX || (uint64_t)atom.size > INT_MAX) |
bcd5d979 | 1089 | return AVERROR_INVALIDDATA; |
d872fb0f MS |
1090 | if ((err = av_reallocp(&st->codec->extradata, size)) < 0) { |
1091 | st->codec->extradata_size = 0; | |
5626f994 | 1092 | return err; |
d872fb0f | 1093 | } |
5626f994 | 1094 | buf = st->codec->extradata + st->codec->extradata_size; |
68bc33fa BC |
1095 | st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE; |
1096 | AV_WB32( buf , atom.size + 8); | |
1097 | AV_WL32( buf + 4, atom.type); | |
b7effd4e | 1098 | avio_read(pb, buf + 8, atom.size); |
de23f234 BC |
1099 | return 0; |
1100 | } | |
1101 | ||
ae628ec1 | 1102 | static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
d9b1c197 | 1103 | { |
6a63ff19 BC |
1104 | AVStream *st; |
1105 | ||
1106 | if (c->fc->nb_streams < 1) | |
1107 | return 0; | |
1108 | st = c->fc->streams[c->fc->nb_streams-1]; | |
d9b1c197 | 1109 | |
5a7ba586 | 1110 | if ((uint64_t)atom.size > (1<<30)) |
bcd5d979 | 1111 | return AVERROR_INVALIDDATA; |
115329f1 | 1112 | |
36ef5369 | 1113 | if (st->codec->codec_id == AV_CODEC_ID_QDM2 || st->codec->codec_id == AV_CODEC_ID_QDMC) { |
ed19fafd | 1114 | // pass all frma atom to codec, needed at least for QDMC and QDM2 |
3840147e | 1115 | av_free(st->codec->extradata); |
b014dd76 BC |
1116 | st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE); |
1117 | if (!st->codec->extradata) | |
1118 | return AVERROR(ENOMEM); | |
3840147e | 1119 | st->codec->extradata_size = atom.size; |
b7effd4e | 1120 | avio_read(pb, st->codec->extradata, atom.size); |
3840147e | 1121 | } else if (atom.size > 8) { /* to read frma, esds atoms */ |
bcd5d979 DH |
1122 | int ret; |
1123 | if ((ret = mov_read_default(c, pb, atom)) < 0) | |
1124 | return ret; | |
5c72cad8 | 1125 | } else |
45a8a02a | 1126 | avio_skip(pb, atom.size); |
d9b1c197 RT |
1127 | return 0; |
1128 | } | |
1129 | ||
bde24601 BC |
1130 | /** |
1131 | * This function reads atom content and puts data in extradata without tag | |
1132 | * nor size unlike mov_read_extradata. | |
1133 | */ | |
ae628ec1 | 1134 | static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
169eb021 | 1135 | { |
6a63ff19 BC |
1136 | AVStream *st; |
1137 | ||
1138 | if (c->fc->nb_streams < 1) | |
1139 | return 0; | |
1140 | st = c->fc->streams[c->fc->nb_streams-1]; | |
169eb021 | 1141 | |
5a7ba586 | 1142 | if ((uint64_t)atom.size > (1<<30)) |
bcd5d979 | 1143 | return AVERROR_INVALIDDATA; |
568e18b1 | 1144 | |
4bf3c8f2 | 1145 | if (atom.size >= 10) { |
59cbc4ee | 1146 | // Broken files created by legacy versions of libavformat will |
4bf3c8f2 AC |
1147 | // wrap a whole fiel atom inside of a glbl atom. |
1148 | unsigned size = avio_rb32(pb); | |
1149 | unsigned type = avio_rl32(pb); | |
1150 | avio_seek(pb, -8, SEEK_CUR); | |
1151 | if (type == MKTAG('f','i','e','l') && size == atom.size) | |
1152 | return mov_read_default(c, pb, atom); | |
1153 | } | |
01f4895c | 1154 | av_free(st->codec->extradata); |
b014dd76 BC |
1155 | st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE); |
1156 | if (!st->codec->extradata) | |
1157 | return AVERROR(ENOMEM); | |
01f4895c | 1158 | st->codec->extradata_size = atom.size; |
b7effd4e | 1159 | avio_read(pb, st->codec->extradata, atom.size); |
169eb021 MM |
1160 | return 0; |
1161 | } | |
1162 | ||
89415b8e MS |
1163 | static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
1164 | { | |
1165 | AVStream *st; | |
1166 | uint8_t profile_level; | |
1167 | ||
1168 | if (c->fc->nb_streams < 1) | |
1169 | return 0; | |
1170 | st = c->fc->streams[c->fc->nb_streams-1]; | |
1171 | ||
1172 | if (atom.size >= (1<<28) || atom.size < 7) | |
1173 | return AVERROR_INVALIDDATA; | |
1174 | ||
1175 | profile_level = avio_r8(pb); | |
f1f37c70 | 1176 | if ((profile_level & 0xf0) != 0xc0) |
89415b8e MS |
1177 | return 0; |
1178 | ||
1179 | av_free(st->codec->extradata); | |
1180 | st->codec->extradata = av_mallocz(atom.size - 7 + FF_INPUT_BUFFER_PADDING_SIZE); | |
1181 | if (!st->codec->extradata) | |
1182 | return AVERROR(ENOMEM); | |
1183 | st->codec->extradata_size = atom.size - 7; | |
1184 | avio_seek(pb, 6, SEEK_CUR); | |
1185 | avio_read(pb, st->codec->extradata, st->codec->extradata_size); | |
1186 | return 0; | |
1187 | } | |
1188 | ||
653d7aeb MS |
1189 | /** |
1190 | * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself, | |
1191 | * but can have extradata appended at the end after the 40 bytes belonging | |
1192 | * to the struct. | |
1193 | */ | |
ae628ec1 | 1194 | static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
653d7aeb MS |
1195 | { |
1196 | AVStream *st; | |
1197 | ||
1198 | if (c->fc->nb_streams < 1) | |
1199 | return 0; | |
1200 | if (atom.size <= 40) | |
1201 | return 0; | |
1202 | st = c->fc->streams[c->fc->nb_streams-1]; | |
1203 | ||
5a7ba586 | 1204 | if ((uint64_t)atom.size > (1<<30)) |
bcd5d979 | 1205 | return AVERROR_INVALIDDATA; |
653d7aeb MS |
1206 | |
1207 | av_free(st->codec->extradata); | |
1208 | st->codec->extradata = av_mallocz(atom.size - 40 + FF_INPUT_BUFFER_PADDING_SIZE); | |
1209 | if (!st->codec->extradata) | |
1210 | return AVERROR(ENOMEM); | |
1211 | st->codec->extradata_size = atom.size - 40; | |
45a8a02a | 1212 | avio_skip(pb, 40); |
b7effd4e | 1213 | avio_read(pb, st->codec->extradata, atom.size - 40); |
653d7aeb MS |
1214 | return 0; |
1215 | } | |
1216 | ||
ae628ec1 | 1217 | static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
5ca1d879 | 1218 | { |
6a63ff19 BC |
1219 | AVStream *st; |
1220 | MOVStreamContext *sc; | |
568e18b1 | 1221 | unsigned int i, entries; |
5ca1d879 | 1222 | |
6a63ff19 BC |
1223 | if (c->fc->nb_streams < 1) |
1224 | return 0; | |
1225 | st = c->fc->streams[c->fc->nb_streams-1]; | |
1226 | sc = st->priv_data; | |
1227 | ||
b7effd4e AK |
1228 | avio_r8(pb); /* version */ |
1229 | avio_rb24(pb); /* flags */ | |
5ca1d879 | 1230 | |
b7effd4e | 1231 | entries = avio_rb32(pb); |
115329f1 | 1232 | |
52401b82 AC |
1233 | if (!entries) |
1234 | return 0; | |
5a7ba586 | 1235 | if (entries >= UINT_MAX/sizeof(int64_t)) |
bcd5d979 | 1236 | return AVERROR_INVALIDDATA; |
115329f1 | 1237 | |
9a630c25 | 1238 | sc->chunk_offsets = av_malloc(entries * sizeof(int64_t)); |
5ca1d879 | 1239 | if (!sc->chunk_offsets) |
0d8f0abf BC |
1240 | return AVERROR(ENOMEM); |
1241 | sc->chunk_count = entries; | |
1242 | ||
37ffe34b | 1243 | if (atom.type == MKTAG('s','t','c','o')) |
9888ffb1 | 1244 | for (i = 0; i < entries && !pb->eof_reached; i++) |
b7effd4e | 1245 | sc->chunk_offsets[i] = avio_rb32(pb); |
37ffe34b | 1246 | else if (atom.type == MKTAG('c','o','6','4')) |
9888ffb1 | 1247 | for (i = 0; i < entries && !pb->eof_reached; i++) |
b7effd4e | 1248 | sc->chunk_offsets[i] = avio_rb64(pb); |
37ffe34b | 1249 | else |
bcd5d979 | 1250 | return AVERROR_INVALIDDATA; |
115329f1 | 1251 | |
9888ffb1 LB |
1252 | sc->chunk_count = i; |
1253 | ||
1254 | if (pb->eof_reached) | |
1255 | return AVERROR_EOF; | |
1256 | ||
5ca1d879 ZK |
1257 | return 0; |
1258 | } | |
1259 | ||
2288834f BC |
1260 | /** |
1261 | * Compute codec id for 'lpcm' tag. | |
1262 | * See CoreAudioTypes and AudioStreamBasicDescription at Apple. | |
1263 | */ | |
36ef5369 | 1264 | enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags) |
2288834f | 1265 | { |
261e9348 JR |
1266 | /* lpcm flags: |
1267 | * 0x1 = float | |
1268 | * 0x2 = big-endian | |
1269 | * 0x4 = signed | |
1270 | */ | |
1271 | return ff_get_pcm_codec_id(bps, flags & 1, flags & 2, flags & 4 ? -1 : 0); | |
2288834f BC |
1272 | } |
1273 | ||
bf985625 LB |
1274 | static int mov_codec_id(AVStream *st, uint32_t format) |
1275 | { | |
1276 | int id = ff_codec_get_id(ff_codec_movaudio_tags, format); | |
1277 | ||
1278 | if (id <= 0 && | |
1279 | ((format & 0xFFFF) == 'm' + ('s' << 8) || | |
1280 | (format & 0xFFFF) == 'T' + ('S' << 8))) | |
1281 | id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format) & 0xFFFF); | |
1282 | ||
1283 | if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) { | |
1284 | st->codec->codec_type = AVMEDIA_TYPE_AUDIO; | |
1285 | } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO && | |
1286 | /* skip old asf mpeg4 tag */ | |
1287 | format && format != MKTAG('m','p','4','s')) { | |
1288 | id = ff_codec_get_id(ff_codec_movvideo_tags, format); | |
1289 | if (id <= 0) | |
1290 | id = ff_codec_get_id(ff_codec_bmp_tags, format); | |
1291 | if (id > 0) | |
1292 | st->codec->codec_type = AVMEDIA_TYPE_VIDEO; | |
1293 | else if (st->codec->codec_type == AVMEDIA_TYPE_DATA) { | |
1294 | id = ff_codec_get_id(ff_codec_movsubtitle_tags, format); | |
1295 | if (id > 0) | |
1296 | st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; | |
1297 | } | |
1298 | } | |
1299 | ||
1300 | st->codec->codec_tag = format; | |
1301 | ||
1302 | return id; | |
1303 | } | |
1304 | ||
a3b53ff0 LB |
1305 | static void mov_parse_stsd_video(MOVContext *c, AVIOContext *pb, |
1306 | AVStream *st, MOVStreamContext *sc) | |
1307 | { | |
18f2514c | 1308 | uint8_t codec_name[32]; |
a3b53ff0 LB |
1309 | unsigned int color_depth, len, j; |
1310 | int color_greyscale; | |
1311 | int color_table_id; | |
1312 | ||
1313 | avio_rb16(pb); /* version */ | |
1314 | avio_rb16(pb); /* revision level */ | |
1315 | avio_rb32(pb); /* vendor */ | |
1316 | avio_rb32(pb); /* temporal quality */ | |
1317 | avio_rb32(pb); /* spatial quality */ | |
1318 | ||
1319 | st->codec->width = avio_rb16(pb); /* width */ | |
1320 | st->codec->height = avio_rb16(pb); /* height */ | |
1321 | ||
1322 | avio_rb32(pb); /* horiz resolution */ | |
1323 | avio_rb32(pb); /* vert resolution */ | |
1324 | avio_rb32(pb); /* data size, always 0 */ | |
1325 | avio_rb16(pb); /* frames per samples */ | |
1326 | ||
1327 | len = avio_r8(pb); /* codec name, pascal string */ | |
1328 | if (len > 31) | |
1329 | len = 31; | |
18f2514c | 1330 | mov_read_mac_string(c, pb, len, codec_name, sizeof(codec_name)); |
a3b53ff0 LB |
1331 | if (len < 31) |
1332 | avio_skip(pb, 31 - len); | |
18f2514c AK |
1333 | |
1334 | if (codec_name[0]) | |
1335 | av_dict_set(&st->metadata, "encoder", codec_name, 0); | |
1336 | ||
a3b53ff0 | 1337 | /* codec_tag YV12 triggers an UV swap in rawdec.c */ |
18f2514c | 1338 | if (!memcmp(codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25)) |
a3b53ff0 LB |
1339 | st->codec->codec_tag = MKTAG('I', '4', '2', '0'); |
1340 | /* Flash Media Server uses tag H263 with Sorenson Spark */ | |
1341 | if (st->codec->codec_tag == MKTAG('H','2','6','3') && | |
18f2514c | 1342 | !memcmp(codec_name, "Sorenson H263", 13)) |
a3b53ff0 LB |
1343 | st->codec->codec_id = AV_CODEC_ID_FLV1; |
1344 | ||
1345 | st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */ | |
1346 | color_table_id = avio_rb16(pb); /* colortable id */ | |
1a3eb042 | 1347 | av_log(c->fc, AV_LOG_TRACE, "depth %d, ctab id %d\n", |
a3b53ff0 LB |
1348 | st->codec->bits_per_coded_sample, color_table_id); |
1349 | /* figure out the palette situation */ | |
1350 | color_depth = st->codec->bits_per_coded_sample & 0x1F; | |
1351 | color_greyscale = st->codec->bits_per_coded_sample & 0x20; | |
1352 | ||
1353 | /* if the depth is 2, 4, or 8 bpp, file is palettized */ | |
1354 | if ((color_depth == 2) || (color_depth == 4) || (color_depth == 8)) { | |
1355 | /* for palette traversal */ | |
1356 | unsigned int color_start, color_count, color_end; | |
1357 | unsigned char r, g, b; | |
1358 | ||
1359 | if (color_greyscale) { | |
1360 | int color_index, color_dec; | |
1361 | /* compute the greyscale palette */ | |
1362 | st->codec->bits_per_coded_sample = color_depth; | |
1363 | color_count = 1 << color_depth; | |
1364 | color_index = 255; | |
1365 | color_dec = 256 / (color_count - 1); | |
1366 | for (j = 0; j < color_count; j++) { | |
1367 | r = g = b = color_index; | |
1368 | sc->palette[j] = (r << 16) | (g << 8) | (b); | |
1369 | color_index -= color_dec; | |
1370 | if (color_index < 0) | |
1371 | color_index = 0; | |
1372 | } | |
1373 | } else if (color_table_id) { | |
1374 | const uint8_t *color_table; | |
1375 | /* if flag bit 3 is set, use the default palette */ | |
1376 | color_count = 1 << color_depth; | |
1377 | if (color_depth == 2) | |
1378 | color_table = ff_qt_default_palette_4; | |
1379 | else if (color_depth == 4) | |
1380 | color_table = ff_qt_default_palette_16; | |
1381 | else | |
1382 | color_table = ff_qt_default_palette_256; | |
1383 | ||
1384 | for (j = 0; j < color_count; j++) { | |
1385 | r = color_table[j * 3 + 0]; | |
1386 | g = color_table[j * 3 + 1]; | |
1387 | b = color_table[j * 3 + 2]; | |
1388 | sc->palette[j] = (r << 16) | (g << 8) | (b); | |
1389 | } | |
1390 | } else { | |
1391 | /* load the palette from the file */ | |
1392 | color_start = avio_rb32(pb); | |
1393 | color_count = avio_rb16(pb); | |
1394 | color_end = avio_rb16(pb); | |
1395 | if ((color_start <= 255) && (color_end <= 255)) { | |
1396 | for (j = color_start; j <= color_end; j++) { | |
1397 | /* each R, G, or B component is 16 bits; | |
1398 | * only use the top 8 bits; skip alpha bytes | |
1399 | * up front */ | |
1400 | avio_r8(pb); | |
1401 | avio_r8(pb); | |
1402 | r = avio_r8(pb); | |
1403 | avio_r8(pb); | |
1404 | g = avio_r8(pb); | |
1405 | avio_r8(pb); | |
1406 | b = avio_r8(pb); | |
1407 | avio_r8(pb); | |
1408 | sc->palette[j] = (r << 16) | (g << 8) | (b); | |
1409 | } | |
1410 | } | |
1411 | } | |
1412 | sc->has_palette = 1; | |
1413 | } | |
1414 | } | |
1415 | ||
ef196bee LB |
1416 | static void mov_parse_stsd_audio(MOVContext *c, AVIOContext *pb, |
1417 | AVStream *st, MOVStreamContext *sc) | |
1418 | { | |
1419 | int bits_per_sample, flags; | |
1420 | uint16_t version = avio_rb16(pb); | |
1421 | ||
1422 | avio_rb16(pb); /* revision level */ | |
1423 | avio_rb32(pb); /* vendor */ | |
1424 | ||
1425 | st->codec->channels = avio_rb16(pb); /* channel count */ | |
1426 | st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */ | |
1a3eb042 | 1427 | av_log(c->fc, AV_LOG_TRACE, "audio channels %d\n", st->codec->channels); |
ef196bee LB |
1428 | |
1429 | sc->audio_cid = avio_rb16(pb); | |
1430 | avio_rb16(pb); /* packet size = 0 */ | |
1431 | ||
1432 | st->codec->sample_rate = ((avio_rb32(pb) >> 16)); | |
1433 | ||
1434 | // Read QT version 1 fields. In version 0 these do not exist. | |
1a3eb042 | 1435 | av_log(c->fc, AV_LOG_TRACE, "version =%d, isom =%d\n", version, c->isom); |
ef196bee LB |
1436 | if (!c->isom) { |
1437 | if (version == 1) { | |
1438 | sc->samples_per_frame = avio_rb32(pb); | |
1439 | avio_rb32(pb); /* bytes per packet */ | |
1440 | sc->bytes_per_frame = avio_rb32(pb); | |
1441 | avio_rb32(pb); /* bytes per sample */ | |
1442 | } else if (version == 2) { | |
1443 | avio_rb32(pb); /* sizeof struct only */ | |
1444 | st->codec->sample_rate = av_int2double(avio_rb64(pb)); | |
1445 | st->codec->channels = avio_rb32(pb); | |
1446 | avio_rb32(pb); /* always 0x7F000000 */ | |
1447 | st->codec->bits_per_coded_sample = avio_rb32(pb); | |
1448 | ||
1449 | flags = avio_rb32(pb); /* lpcm format specific flag */ | |
1450 | sc->bytes_per_frame = avio_rb32(pb); | |
1451 | sc->samples_per_frame = avio_rb32(pb); | |
1452 | if (st->codec->codec_tag == MKTAG('l','p','c','m')) | |
1453 | st->codec->codec_id = | |
1454 | ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, | |
1455 | flags); | |
1456 | } | |
1457 | } | |
1458 | ||
1459 | switch (st->codec->codec_id) { | |
1460 | case AV_CODEC_ID_PCM_S8: | |
1461 | case AV_CODEC_ID_PCM_U8: | |
1462 | if (st->codec->bits_per_coded_sample == 16) | |
1463 | st->codec->codec_id = AV_CODEC_ID_PCM_S16BE; | |
1464 | break; | |
1465 | case AV_CODEC_ID_PCM_S16LE: | |
1466 | case AV_CODEC_ID_PCM_S16BE: | |
1467 | if (st->codec->bits_per_coded_sample == 8) | |
1468 | st->codec->codec_id = AV_CODEC_ID_PCM_S8; | |
1469 | else if (st->codec->bits_per_coded_sample == 24) | |
1470 | st->codec->codec_id = | |
1471 | st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ? | |
1472 | AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE; | |
1473 | break; | |
1474 | /* set values for old format before stsd version 1 appeared */ | |
1475 | case AV_CODEC_ID_MACE3: | |
1476 | sc->samples_per_frame = 6; | |
1477 | sc->bytes_per_frame = 2 * st->codec->channels; | |
1478 | break; | |
1479 | case AV_CODEC_ID_MACE6: | |
1480 | sc->samples_per_frame = 6; | |
1481 | sc->bytes_per_frame = 1 * st->codec->channels; | |
1482 | break; | |
1483 | case AV_CODEC_ID_ADPCM_IMA_QT: | |
1484 | sc->samples_per_frame = 64; | |
1485 | sc->bytes_per_frame = 34 * st->codec->channels; | |
1486 | break; | |
1487 | case AV_CODEC_ID_GSM: | |
1488 | sc->samples_per_frame = 160; | |
1489 | sc->bytes_per_frame = 33; | |
1490 | break; | |
1491 | default: | |
1492 | break; | |
1493 | } | |
1494 | ||
1495 | bits_per_sample = av_get_bits_per_sample(st->codec->codec_id); | |
1496 | if (bits_per_sample) { | |
1497 | st->codec->bits_per_coded_sample = bits_per_sample; | |
1498 | sc->sample_size = (bits_per_sample >> 3) * st->codec->channels; | |
1499 | } | |
1500 | } | |
1501 | ||
dc518a3a LB |
1502 | static void mov_parse_stsd_subtitle(MOVContext *c, AVIOContext *pb, |
1503 | AVStream *st, MOVStreamContext *sc, | |
1504 | int size) | |
1505 | { | |
1506 | // ttxt stsd contains display flags, justification, background | |
1507 | // color, fonts, and default styles, so fake an atom to read it | |
1508 | MOVAtom fake_atom = { .size = size }; | |
1509 | // mp4s contains a regular esds atom | |
1510 | if (st->codec->codec_tag != AV_RL32("mp4s")) | |
1511 | mov_read_glbl(c, pb, fake_atom); | |
1512 | st->codec->width = sc->width; | |
1513 | st->codec->height = sc->height; | |
1514 | } | |
1515 | ||
12789d96 MN |
1516 | static uint32_t yuv_to_rgba(uint32_t ycbcr) |
1517 | { | |
1518 | uint8_t r, g, b; | |
1519 | int y, cb, cr; | |
1520 | ||
1521 | y = (ycbcr >> 16) & 0xFF; | |
1522 | cr = (ycbcr >> 8) & 0xFF; | |
1523 | cb = ycbcr & 0xFF; | |
1524 | ||
1525 | b = av_clip_uint8(1.164 * (y - 16) + 2.018 * (cb - 128)); | |
1526 | g = av_clip_uint8(1.164 * (y - 16) - 0.813 * (cr - 128) - 0.391 * (cb - 128)); | |
1527 | r = av_clip_uint8(1.164 * (y - 16) + 1.596 * (cr - 128)); | |
1528 | ||
1529 | return (r << 16) | (g << 8) | b; | |
1530 | } | |
1531 | ||
1532 | static int mov_rewrite_dvd_sub_extradata(AVStream *st) | |
1533 | { | |
1534 | char buf[256] = {0}; | |
1535 | uint8_t *src = st->codec->extradata; | |
1536 | int i; | |
1537 | ||
1538 | if (st->codec->extradata_size != 64) | |
1539 | return 0; | |
1540 | ||
1541 | if (st->codec->width > 0 && st->codec->height > 0) | |
1542 | snprintf(buf, sizeof(buf), "size: %dx%d\n", | |
1543 | st->codec->width, st->codec->height); | |
1544 | av_strlcat(buf, "palette: ", sizeof(buf)); | |
1545 | ||
1546 | for (i = 0; i < 16; i++) { | |
1547 | uint32_t yuv = AV_RB32(src + i * 4); | |
1548 | uint32_t rgba = yuv_to_rgba(yuv); | |
1549 | ||
cba4e606 | 1550 | av_strlcatf(buf, sizeof(buf), "%06"PRIx32"%s", rgba, i != 15 ? ", " : ""); |
12789d96 MN |
1551 | } |
1552 | ||
1553 | if (av_strlcat(buf, "\n", sizeof(buf)) >= sizeof(buf)) | |
1554 | return 0; | |
1555 | ||
1556 | av_freep(&st->codec->extradata); | |
1557 | st->codec->extradata_size = 0; | |
1558 | st->codec->extradata = av_mallocz(strlen(buf) + FF_INPUT_BUFFER_PADDING_SIZE); | |
1559 | if (!st->codec->extradata) | |
1560 | return AVERROR(ENOMEM); | |
1561 | st->codec->extradata_size = strlen(buf); | |
1562 | memcpy(st->codec->extradata, buf, st->codec->extradata_size); | |
1563 | ||
1564 | return 0; | |
1565 | } | |
1566 | ||
5b41eb91 LB |
1567 | static int mov_parse_stsd_data(MOVContext *c, AVIOContext *pb, |
1568 | AVStream *st, MOVStreamContext *sc, | |
1569 | int size) | |
1570 | { | |
1571 | if (st->codec->codec_tag == MKTAG('t','m','c','d')) { | |
1572 | st->codec->extradata_size = size; | |
1573 | st->codec->extradata = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); | |
1574 | if (!st->codec->extradata) | |
1575 | return AVERROR(ENOMEM); | |
1576 | avio_read(pb, st->codec->extradata, size); | |
1577 | } else { | |
1578 | /* other codec type, just skip (rtp, mp4s ...) */ | |
1579 | avio_skip(pb, size); | |
1580 | } | |
1581 | return 0; | |
1582 | } | |
1583 | ||
08504380 LB |
1584 | static int mov_finalize_stsd_codec(MOVContext *c, AVIOContext *pb, |
1585 | AVStream *st, MOVStreamContext *sc) | |
1586 | { | |
1587 | if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && | |
1588 | !st->codec->sample_rate && sc->time_scale > 1) | |
1589 | st->codec->sample_rate = sc->time_scale; | |
1590 | ||
1591 | /* special codec parameters handling */ | |
1592 | switch (st->codec->codec_id) { | |
1593 | #if CONFIG_DV_DEMUXER | |
1594 | case AV_CODEC_ID_DVAUDIO: | |
1595 | c->dv_fctx = avformat_alloc_context(); | |
1596 | c->dv_demux = avpriv_dv_init_demux(c->dv_fctx); | |
1597 | if (!c->dv_demux) { | |
1598 | av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n"); | |
1599 | return AVERROR(ENOMEM); | |
1600 | } | |
1601 | sc->dv_audio_container = 1; | |
1602 | st->codec->codec_id = AV_CODEC_ID_PCM_S16LE; | |
1603 | break; | |
1604 | #endif | |
1605 | /* no ifdef since parameters are always those */ | |
1606 | case AV_CODEC_ID_QCELP: | |
1607 | st->codec->channels = 1; | |
1608 | // force sample rate for qcelp when not stored in mov | |
1609 | if (st->codec->codec_tag != MKTAG('Q','c','l','p')) | |
1610 | st->codec->sample_rate = 8000; | |
1611 | break; | |
1612 | case AV_CODEC_ID_AMR_NB: | |
1613 | st->codec->channels = 1; | |
1614 | /* force sample rate for amr, stsd in 3gp does not store sample rate */ | |
1615 | st->codec->sample_rate = 8000; | |
1616 | break; | |
1617 | case AV_CODEC_ID_AMR_WB: | |
1618 | st->codec->channels = 1; | |
1619 | st->codec->sample_rate = 16000; | |
1620 | break; | |
1621 | case AV_CODEC_ID_MP2: | |
1622 | case AV_CODEC_ID_MP3: | |
1623 | /* force type after stsd for m1a hdlr */ | |
1624 | st->codec->codec_type = AVMEDIA_TYPE_AUDIO; | |
1625 | st->need_parsing = AVSTREAM_PARSE_FULL; | |
1626 | break; | |
1627 | case AV_CODEC_ID_GSM: | |
1628 | case AV_CODEC_ID_ADPCM_MS: | |
1629 | case AV_CODEC_ID_ADPCM_IMA_WAV: | |
1630 | case AV_CODEC_ID_ILBC: | |
1631 | st->codec->block_align = sc->bytes_per_frame; | |
1632 | break; | |
1633 | case AV_CODEC_ID_ALAC: | |
1634 | if (st->codec->extradata_size == 36) { | |
1635 | st->codec->channels = AV_RB8 (st->codec->extradata + 21); | |
1636 | st->codec->sample_rate = AV_RB32(st->codec->extradata + 32); | |
1637 | } | |
1638 | break; | |
1639 | case AV_CODEC_ID_VC1: | |
1640 | st->need_parsing = AVSTREAM_PARSE_FULL; | |
1641 | break; | |
1642 | default: | |
1643 | break; | |
1644 | } | |
1645 | return 0; | |
1646 | } | |
1647 | ||
0b5af5cf LB |
1648 | static int mov_skip_multiple_stsd(MOVContext *c, AVIOContext *pb, |
1649 | int codec_tag, int format, | |
1650 | int size) | |
1651 | { | |
1652 | int video_codec_id = ff_codec_get_id(ff_codec_movvideo_tags, format); | |
1653 | ||
1654 | if (codec_tag && | |
1655 | (codec_tag == AV_RL32("avc1") || | |
ea29f965 YN |
1656 | codec_tag == AV_RL32("hvc1") || |
1657 | codec_tag == AV_RL32("hev1") || | |
0b5af5cf LB |
1658 | (codec_tag != format && |
1659 | (c->fc->video_codec_id ? video_codec_id != c->fc->video_codec_id | |
1660 | : codec_tag != MKTAG('j','p','e','g'))))) { | |
1661 | /* Multiple fourcc, we skip JPEG. This is not correct, we should | |
1662 | * export it as a separate AVStream but this needs a few changes | |
1663 | * in the MOV demuxer, patch welcome. */ | |
1664 | ||
1665 | av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n"); | |
1666 | avio_skip(pb, size); | |
1667 | return 1; | |
1668 | } | |
1669 | ||
1670 | return 0; | |
1671 | } | |
1672 | ||
ae628ec1 | 1673 | int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries) |
5ca1d879 | 1674 | { |
6a63ff19 BC |
1675 | AVStream *st; |
1676 | MOVStreamContext *sc; | |
a3b53ff0 | 1677 | int pseudo_stream_id; |
b595afaa | 1678 | |
6a63ff19 BC |
1679 | if (c->fc->nb_streams < 1) |
1680 | return 0; | |
1681 | st = c->fc->streams[c->fc->nb_streams-1]; | |
1682 | sc = st->priv_data; | |
1683 | ||
9888ffb1 LB |
1684 | for (pseudo_stream_id = 0; |
1685 | pseudo_stream_id < entries && !pb->eof_reached; | |
1686 | pseudo_stream_id++) { | |
117a9190 | 1687 | //Parsing Sample description table |
36ef5369 | 1688 | enum AVCodecID id; |
5b41eb91 | 1689 | int ret, dref_id = 1; |
3491866a | 1690 | MOVAtom a = { AV_RL32("stsd") }; |
a2704c97 | 1691 | int64_t start_pos = avio_tell(pb); |
a5ea623b | 1692 | uint32_t size = avio_rb32(pb); /* size */ |
b7effd4e | 1693 | uint32_t format = avio_rl32(pb); /* data format */ |
5cd62665 | 1694 | |
28155113 | 1695 | if (size >= 16) { |
b7effd4e AK |
1696 | avio_rb32(pb); /* reserved */ |
1697 | avio_rb16(pb); /* reserved */ | |
1698 | dref_id = avio_rb16(pb); | |
a5ea623b | 1699 | } else { |
d92024f1 | 1700 | av_log(c->fc, AV_LOG_ERROR, "invalid size %"PRIu32" in stsd\n", size); |
a5ea623b | 1701 | return AVERROR_INVALIDDATA; |
28155113 | 1702 | } |
0e7eed09 | 1703 | |
0b5af5cf LB |
1704 | if (mov_skip_multiple_stsd(c, pb, st->codec->codec_tag, format, |
1705 | size - (avio_tell(pb) - start_pos))) | |
e7cc4b52 | 1706 | continue; |
0b5af5cf | 1707 | |
978677a7 | 1708 | sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id; |
221e21b7 | 1709 | sc->dref_id= dref_id; |
e7cc4b52 | 1710 | |
bf985625 | 1711 | id = mov_codec_id(st, format); |
99487f42 | 1712 | |
1a3eb042 | 1713 | av_log(c->fc, AV_LOG_TRACE, "size=%"PRIu32" 4CC= %"PRIu8"%"PRIu8"%"PRIu8"%"PRIu8" codec_type=%d\n", size, |
29c90869 BC |
1714 | (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff, |
1715 | (format >> 24) & 0xff, st->codec->codec_type); | |
a20da52c | 1716 | |
5a7ba586 | 1717 | if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) { |
01f4895c | 1718 | st->codec->codec_id = id; |
a3b53ff0 | 1719 | mov_parse_stsd_video(c, pb, st, sc); |
5a7ba586 | 1720 | } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) { |
99487f42 | 1721 | st->codec->codec_id = id; |
ef196bee | 1722 | mov_parse_stsd_audio(c, pb, st, sc); |
5a7ba586 | 1723 | } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){ |
dc518a3a LB |
1724 | st->codec->codec_id = id; |
1725 | mov_parse_stsd_subtitle(c, pb, st, sc, | |
1726 | size - (avio_tell(pb) - start_pos)); | |
de23f234 | 1727 | } else { |
5b41eb91 LB |
1728 | ret = mov_parse_stsd_data(c, pb, st, sc, |
1729 | size - (avio_tell(pb) - start_pos)); | |
1730 | if (ret < 0) | |
1731 | return ret; | |
6cea494e | 1732 | } |
ea29f965 | 1733 | /* this will read extra atoms at the end (wave, alac, damr, avcC, hvcC, SMI ...) */ |
a2704c97 | 1734 | a.size = size - (avio_tell(pb) - start_pos); |
9cf0419b | 1735 | if (a.size > 8) { |
bcd5d979 DH |
1736 | if ((ret = mov_read_default(c, pb, a)) < 0) |
1737 | return ret; | |
9cf0419b | 1738 | } else if (a.size > 0) |
45a8a02a | 1739 | avio_skip(pb, a.size); |
6cea494e | 1740 | } |
115329f1 | 1741 | |
9888ffb1 LB |
1742 | if (pb->eof_reached) |
1743 | return AVERROR_EOF; | |
1744 | ||
08504380 | 1745 | return mov_finalize_stsd_codec(c, pb, st, sc); |
6cea494e ZK |
1746 | } |
1747 | ||
ae628ec1 | 1748 | static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
dc2cabd0 MS |
1749 | { |
1750 | int entries; | |
1751 | ||
b7effd4e AK |
1752 | avio_r8(pb); /* version */ |
1753 | avio_rb24(pb); /* flags */ | |
1754 | entries = avio_rb32(pb); | |
dc2cabd0 MS |
1755 | |
1756 | return ff_mov_read_stsd_entries(c, pb, entries); | |
1757 | } | |
1758 | ||
ae628ec1 | 1759 | static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
6cea494e | 1760 | { |
6a63ff19 BC |
1761 | AVStream *st; |
1762 | MOVStreamContext *sc; | |
568e18b1 | 1763 | unsigned int i, entries; |
b6a17df4 | 1764 | |
6a63ff19 BC |
1765 | if (c->fc->nb_streams < 1) |
1766 | return 0; | |
1767 | st = c->fc->streams[c->fc->nb_streams-1]; | |
1768 | sc = st->priv_data; | |
1769 | ||
b7effd4e AK |
1770 | avio_r8(pb); /* version */ |
1771 | avio_rb24(pb); /* flags */ | |
6cea494e | 1772 | |
b7effd4e | 1773 | entries = avio_rb32(pb); |
115329f1 | 1774 | |
1a3eb042 | 1775 | av_log(c->fc, AV_LOG_TRACE, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries); |
f8c18cd7 | 1776 | |
52401b82 AC |
1777 | if (!entries) |
1778 | return 0; | |
5a7ba586 | 1779 | if (entries >= UINT_MAX / sizeof(*sc->stsc_data)) |
bcd5d979 | 1780 | return AVERROR_INVALIDDATA; |
04e06cfa BC |
1781 | sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data)); |
1782 | if (!sc->stsc_data) | |
0d8f0abf | 1783 | return AVERROR(ENOMEM); |
0d8f0abf | 1784 | |
9888ffb1 | 1785 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
b7effd4e AK |
1786 | sc->stsc_data[i].first = avio_rb32(pb); |
1787 | sc->stsc_data[i].count = avio_rb32(pb); | |
1788 | sc->stsc_data[i].id = avio_rb32(pb); | |
6cea494e | 1789 | } |
9888ffb1 LB |
1790 | |
1791 | sc->stsc_count = i; | |
1792 | ||
1793 | if (pb->eof_reached) | |
1794 | return AVERROR_EOF; | |
1795 | ||
6cea494e ZK |
1796 | return 0; |
1797 | } | |
1798 | ||
ae628ec1 | 1799 | static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
171a3531 BC |
1800 | { |
1801 | AVStream *st; | |
1802 | MOVStreamContext *sc; | |
1803 | unsigned i, entries; | |
1804 | ||
1805 | if (c->fc->nb_streams < 1) | |
1806 | return 0; | |
1807 | st = c->fc->streams[c->fc->nb_streams-1]; | |
1808 | sc = st->priv_data; | |
1809 | ||
b7effd4e | 1810 | avio_rb32(pb); // version + flags |
171a3531 | 1811 | |
b7effd4e | 1812 | entries = avio_rb32(pb); |
171a3531 | 1813 | if (entries >= UINT_MAX / sizeof(*sc->stps_data)) |
bcd5d979 | 1814 | return AVERROR_INVALIDDATA; |
171a3531 BC |
1815 | sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data)); |
1816 | if (!sc->stps_data) | |
1817 | return AVERROR(ENOMEM); | |
171a3531 | 1818 | |
9888ffb1 | 1819 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
b7effd4e | 1820 | sc->stps_data[i] = avio_rb32(pb); |
1a3eb042 | 1821 | //av_log(c->fc, AV_LOG_TRACE, "stps %d\n", sc->stps_data[i]); |
171a3531 BC |
1822 | } |
1823 | ||
9888ffb1 LB |
1824 | sc->stps_count = i; |
1825 | ||
1826 | if (pb->eof_reached) | |
1827 | return AVERROR_EOF; | |
1828 | ||
171a3531 BC |
1829 | return 0; |
1830 | } | |
1831 | ||
ae628ec1 | 1832 | static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
247d56f5 | 1833 | { |
6a63ff19 BC |
1834 | AVStream *st; |
1835 | MOVStreamContext *sc; | |
568e18b1 | 1836 | unsigned int i, entries; |
247d56f5 | 1837 | |
6a63ff19 BC |
1838 | if (c->fc->nb_streams < 1) |
1839 | return 0; | |
1840 | st = c->fc->streams[c->fc->nb_streams-1]; | |
1841 | sc = st->priv_data; | |
1842 | ||
b7effd4e AK |
1843 | avio_r8(pb); /* version */ |
1844 | avio_rb24(pb); /* flags */ | |
247d56f5 | 1845 | |
b7effd4e | 1846 | entries = avio_rb32(pb); |
115329f1 | 1847 | |
1a3eb042 | 1848 | av_log(c->fc, AV_LOG_TRACE, "keyframe_count = %d\n", entries); |
0d8f0abf | 1849 | |
29a20ac4 | 1850 | if (!entries) |
accea4d9 YN |
1851 | { |
1852 | sc->keyframe_absent = 1; | |
29a20ac4 | 1853 | return 0; |
accea4d9 | 1854 | } |
5a7ba586 | 1855 | if (entries >= UINT_MAX / sizeof(int)) |
bcd5d979 | 1856 | return AVERROR_INVALIDDATA; |
64f7575f | 1857 | av_freep(&sc->keyframes); |
1c02d96f | 1858 | sc->keyframes = av_malloc(entries * sizeof(int)); |
247d56f5 | 1859 | if (!sc->keyframes) |
0d8f0abf | 1860 | return AVERROR(ENOMEM); |
0d8f0abf | 1861 | |
9888ffb1 | 1862 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
b7effd4e | 1863 | sc->keyframes[i] = avio_rb32(pb); |
1a3eb042 | 1864 | //av_log(c->fc, AV_LOG_TRACE, "keyframes[]=%d\n", sc->keyframes[i]); |
247d56f5 | 1865 | } |
9888ffb1 LB |
1866 | |
1867 | sc->keyframe_count = i; | |
1868 | ||
1869 | if (pb->eof_reached) | |
1870 | return AVERROR_EOF; | |
1871 | ||
247d56f5 BB |
1872 | return 0; |
1873 | } | |
1874 | ||
ae628ec1 | 1875 | static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
6cea494e | 1876 | { |
6a63ff19 BC |
1877 | AVStream *st; |
1878 | MOVStreamContext *sc; | |
bd27eed6 AC |
1879 | unsigned int i, entries, sample_size, field_size, num_bytes; |
1880 | GetBitContext gb; | |
1881 | unsigned char* buf; | |
b6a17df4 | 1882 | |
6a63ff19 BC |
1883 | if (c->fc->nb_streams < 1) |
1884 | return 0; | |
1885 | st = c->fc->streams[c->fc->nb_streams-1]; | |
1886 | sc = st->priv_data; | |
1887 | ||
b7effd4e AK |
1888 | avio_r8(pb); /* version */ |
1889 | avio_rb24(pb); /* flags */ | |
5cd62665 | 1890 | |
bd27eed6 | 1891 | if (atom.type == MKTAG('s','t','s','z')) { |
b7effd4e | 1892 | sample_size = avio_rb32(pb); |
d0dab0ec AC |
1893 | if (!sc->sample_size) /* do not overwrite value computed in stsd */ |
1894 | sc->sample_size = sample_size; | |
1895 | field_size = 32; | |
bd27eed6 AC |
1896 | } else { |
1897 | sample_size = 0; | |
b7effd4e AK |
1898 | avio_rb24(pb); /* reserved */ |
1899 | field_size = avio_r8(pb); | |
bd27eed6 | 1900 | } |
b7effd4e | 1901 | entries = avio_rb32(pb); |
0d8f0abf | 1902 | |
1a3eb042 | 1903 | av_log(c->fc, AV_LOG_TRACE, "sample_size = %d sample_count = %d\n", sc->sample_size, entries); |
568e18b1 | 1904 | |
6cea494e | 1905 | sc->sample_count = entries; |
b72708f8 BC |
1906 | if (sample_size) |
1907 | return 0; | |
1908 | ||
bd27eed6 AC |
1909 | if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) { |
1910 | av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size); | |
bcd5d979 | 1911 | return AVERROR_INVALIDDATA; |
bd27eed6 AC |
1912 | } |
1913 | ||
52401b82 AC |
1914 | if (!entries) |
1915 | return 0; | |
e4bc8af1 | 1916 | if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size) |
bcd5d979 | 1917 | return AVERROR_INVALIDDATA; |
1c02d96f | 1918 | sc->sample_sizes = av_malloc(entries * sizeof(int)); |
b6a17df4 | 1919 | if (!sc->sample_sizes) |
0d8f0abf BC |
1920 | return AVERROR(ENOMEM); |
1921 | ||
bd27eed6 AC |
1922 | num_bytes = (entries*field_size+4)>>3; |
1923 | ||
08a5b4e9 | 1924 | buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE); |
bd27eed6 AC |
1925 | if (!buf) { |
1926 | av_freep(&sc->sample_sizes); | |
1927 | return AVERROR(ENOMEM); | |
1928 | } | |
1929 | ||
b7effd4e | 1930 | if (avio_read(pb, buf, num_bytes) < num_bytes) { |
bd27eed6 AC |
1931 | av_freep(&sc->sample_sizes); |
1932 | av_free(buf); | |
bcd5d979 | 1933 | return AVERROR_INVALIDDATA; |
bd27eed6 AC |
1934 | } |
1935 | ||
1936 | init_get_bits(&gb, buf, 8*num_bytes); | |
1937 | ||
9888ffb1 | 1938 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
bd27eed6 | 1939 | sc->sample_sizes[i] = get_bits_long(&gb, field_size); |
bc7d0517 MS |
1940 | sc->data_size += sc->sample_sizes[i]; |
1941 | } | |
bd27eed6 | 1942 | |
9888ffb1 LB |
1943 | sc->sample_count = i; |
1944 | ||
f261a55d AC |
1945 | av_free(buf); |
1946 | ||
9888ffb1 LB |
1947 | if (pb->eof_reached) |
1948 | return AVERROR_EOF; | |
1949 | ||
6cea494e ZK |
1950 | return 0; |
1951 | } | |
1952 | ||
ae628ec1 | 1953 | static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
0147f198 | 1954 | { |
6a63ff19 BC |
1955 | AVStream *st; |
1956 | MOVStreamContext *sc; | |
568e18b1 | 1957 | unsigned int i, entries; |
d957696f MN |
1958 | int64_t duration=0; |
1959 | int64_t total_sample_count=0; | |
b6a17df4 | 1960 | |
6a63ff19 BC |
1961 | if (c->fc->nb_streams < 1) |
1962 | return 0; | |
1963 | st = c->fc->streams[c->fc->nb_streams-1]; | |
1964 | sc = st->priv_data; | |
1965 | ||
b7effd4e AK |
1966 | avio_r8(pb); /* version */ |
1967 | avio_rb24(pb); /* flags */ | |
1968 | entries = avio_rb32(pb); | |
0d8f0abf | 1969 | |
1a3eb042 | 1970 | av_log(c->fc, AV_LOG_TRACE, "track[%i].stts.entries = %i\n", |
5a7ba586 | 1971 | c->fc->nb_streams-1, entries); |
0d8f0abf | 1972 | |
6d23d197 AC |
1973 | if (!entries) |
1974 | return 0; | |
1975 | if (entries >= UINT_MAX / sizeof(*sc->stts_data)) | |
30c3d976 | 1976 | return AVERROR(EINVAL); |
5a7ba586 | 1977 | |
2620df13 | 1978 | av_free(sc->stts_data); |
c3e92a6c | 1979 | sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data)); |
c1da59fa | 1980 | if (!sc->stts_data) |
0d8f0abf | 1981 | return AVERROR(ENOMEM); |
5a7ba586 | 1982 | |
9888ffb1 | 1983 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
cd461d48 MN |
1984 | int sample_duration; |
1985 | int sample_count; | |
0147f198 | 1986 | |
b7effd4e AK |
1987 | sample_count=avio_rb32(pb); |
1988 | sample_duration = avio_rb32(pb); | |
c2319876 MS |
1989 | if (sample_count < 0) { |
1990 | av_log(c->fc, AV_LOG_ERROR, "Invalid sample_count=%d\n", sample_count); | |
1991 | return AVERROR_INVALIDDATA; | |
1992 | } | |
cd7352d5 MN |
1993 | sc->stts_data[i].count= sample_count; |
1994 | sc->stts_data[i].duration= sample_duration; | |
1995 | ||
1a3eb042 | 1996 | av_log(c->fc, AV_LOG_TRACE, "sample_count=%d, sample_duration=%d\n", |
5a7ba586 | 1997 | sample_count, sample_duration); |
4e5ef14f | 1998 | |
7e815047 | 1999 | duration+=(int64_t)sample_duration*sample_count; |
891f64b3 | 2000 | total_sample_count+=sample_count; |
891f64b3 | 2001 | } |
2002 | ||
9888ffb1 LB |
2003 | sc->stts_count = i; |
2004 | ||
2005 | if (pb->eof_reached) | |
2006 | return AVERROR_EOF; | |
2007 | ||
961e0ccd | 2008 | st->nb_frames= total_sample_count; |
5a7ba586 | 2009 | if (duration) |
961e0ccd | 2010 | st->duration= duration; |
7f19bdc2 | 2011 | sc->track_end = duration; |
0147f198 FR |
2012 | return 0; |
2013 | } | |
2014 | ||
ae628ec1 | 2015 | static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
c4ac052b | 2016 | { |
6a63ff19 BC |
2017 | AVStream *st; |
2018 | MOVStreamContext *sc; | |
c4ac052b MN |
2019 | unsigned int i, entries; |
2020 | ||
6a63ff19 BC |
2021 | if (c->fc->nb_streams < 1) |
2022 | return 0; | |
2023 | st = c->fc->streams[c->fc->nb_streams-1]; | |
2024 | sc = st->priv_data; | |
2025 | ||
b7effd4e AK |
2026 | avio_r8(pb); /* version */ |
2027 | avio_rb24(pb); /* flags */ | |
2028 | entries = avio_rb32(pb); | |
0d8f0abf | 2029 | |
1a3eb042 | 2030 | av_log(c->fc, AV_LOG_TRACE, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries); |
0d8f0abf | 2031 | |
f261a55d AC |
2032 | av_freep(&sc->ctts_data); |
2033 | ||
52401b82 AC |
2034 | if (!entries) |
2035 | return 0; | |
5a7ba586 | 2036 | if (entries >= UINT_MAX / sizeof(*sc->ctts_data)) |
bcd5d979 | 2037 | return AVERROR_INVALIDDATA; |
a8a90906 | 2038 | sc->ctts_data = av_realloc(NULL, entries * sizeof(*sc->ctts_data)); |
c1da59fa | 2039 | if (!sc->ctts_data) |
0d8f0abf | 2040 | return AVERROR(ENOMEM); |
4e5ef14f | 2041 | |
9888ffb1 | 2042 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
b7effd4e AK |
2043 | int count =avio_rb32(pb); |
2044 | int duration =avio_rb32(pb); | |
70a61ed4 MN |
2045 | |
2046 | sc->ctts_data[i].count = count; | |
2047 | sc->ctts_data[i].duration= duration; | |
e7452721 BC |
2048 | if (duration < 0) |
2049 | sc->dts_shift = FFMAX(sc->dts_shift, -duration); | |
c4ac052b | 2050 | } |
e7452721 | 2051 | |
9888ffb1 LB |
2052 | sc->ctts_count = i; |
2053 | ||
2054 | if (pb->eof_reached) | |
2055 | return AVERROR_EOF; | |
2056 | ||
1a3eb042 | 2057 | av_log(c->fc, AV_LOG_TRACE, "dts shift %d\n", sc->dts_shift); |
e7452721 | 2058 | |
c4ac052b MN |
2059 | return 0; |
2060 | } | |
2061 | ||
d17d0ec8 YN |
2062 | static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2063 | { | |
2064 | AVStream *st; | |
2065 | MOVStreamContext *sc; | |
2066 | unsigned int i, entries; | |
2067 | uint8_t version; | |
2068 | uint32_t grouping_type; | |
2069 | ||
2070 | if (c->fc->nb_streams < 1) | |
2071 | return 0; | |
2072 | st = c->fc->streams[c->fc->nb_streams-1]; | |
2073 | sc = st->priv_data; | |
2074 | ||
2075 | version = avio_r8(pb); /* version */ | |
2076 | avio_rb24(pb); /* flags */ | |
2077 | grouping_type = avio_rl32(pb); | |
2078 | if (grouping_type != MKTAG( 'r','a','p',' ')) | |
2079 | return 0; /* only support 'rap ' grouping */ | |
2080 | if (version == 1) | |
2081 | avio_rb32(pb); /* grouping_type_parameter */ | |
2082 | ||
2083 | entries = avio_rb32(pb); | |
2084 | if (!entries) | |
2085 | return 0; | |
2086 | if (entries >= UINT_MAX / sizeof(*sc->rap_group)) | |
2087 | return AVERROR_INVALIDDATA; | |
2088 | sc->rap_group = av_malloc(entries * sizeof(*sc->rap_group)); | |
2089 | if (!sc->rap_group) | |
2090 | return AVERROR(ENOMEM); | |
2091 | ||
2092 | for (i = 0; i < entries && !pb->eof_reached; i++) { | |
2093 | sc->rap_group[i].count = avio_rb32(pb); /* sample_count */ | |
2094 | sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */ | |
2095 | } | |
2096 | ||
2097 | sc->rap_group_count = i; | |
2098 | ||
2099 | return pb->eof_reached ? AVERROR_EOF : 0; | |
2100 | } | |
2101 | ||
1e77810d BC |
2102 | static void mov_build_index(MOVContext *mov, AVStream *st) |
2103 | { | |
2104 | MOVStreamContext *sc = st->priv_data; | |
bc5c918e | 2105 | int64_t current_offset; |
1e77810d BC |
2106 | int64_t current_dts = 0; |
2107 | unsigned int stts_index = 0; | |
2108 | unsigned int stsc_index = 0; | |
2109 | unsigned int stss_index = 0; | |
171a3531 | 2110 | unsigned int stps_index = 0; |
1e77810d | 2111 | unsigned int i, j; |
c5898e86 | 2112 | uint64_t stream_size = 0; |
1e77810d | 2113 | |
baf2ffd3 | 2114 | /* adjust first dts according to edit list */ |
c3024f9e | 2115 | if (sc->time_offset && mov->time_scale > 0) { |
f33a6a22 YN |
2116 | if (sc->time_offset < 0) |
2117 | sc->time_offset = av_rescale(sc->time_offset, sc->time_scale, mov->time_scale); | |
2118 | current_dts = -sc->time_offset; | |
69e7ad8d | 2119 | if (sc->ctts_data && sc->stts_data && sc->stts_data[0].duration && |
4af7166f | 2120 | sc->ctts_data[0].duration / sc->stts_data[0].duration > 16) { |
f03a081d BC |
2121 | /* more than 16 frames delay, dts are likely wrong |
2122 | this happens with files created by iMovie */ | |
2123 | sc->wrong_dts = 1; | |
2124 | st->codec->has_b_frames = 1; | |
2125 | } | |
baf2ffd3 BC |
2126 | } |
2127 | ||
d3bc61ac | 2128 | /* only use old uncompressed audio chunk demuxing when stts specifies it */ |
72415b2a | 2129 | if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && |
d3bc61ac | 2130 | sc->stts_count == 1 && sc->stts_data[0].duration == 1)) { |
1e77810d BC |
2131 | unsigned int current_sample = 0; |
2132 | unsigned int stts_sample = 0; | |
171a3531 | 2133 | unsigned int sample_size; |
1e77810d | 2134 | unsigned int distance = 0; |
d17d0ec8 YN |
2135 | unsigned int rap_group_index = 0; |
2136 | unsigned int rap_group_sample = 0; | |
2137 | int rap_group_present = sc->rap_group_count && sc->rap_group; | |
94c9bf88 | 2138 | int key_off = (sc->keyframes && sc->keyframes[0] > 0) || (sc->stps_data && sc->stps_data[0] > 0); |
1e77810d | 2139 | |
91eee2af MS |
2140 | current_dts -= sc->dts_shift; |
2141 | ||
52401b82 AC |
2142 | if (!sc->sample_count) |
2143 | return; | |
4f7c7624 | 2144 | if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries) |
987e857f | 2145 | return; |
f369b935 AK |
2146 | if (av_reallocp_array(&st->index_entries, |
2147 | st->nb_index_entries + sc->sample_count, | |
2148 | sizeof(*st->index_entries)) < 0) { | |
2149 | st->nb_index_entries = 0; | |
987e857f | 2150 | return; |
f369b935 | 2151 | } |
4f7c7624 | 2152 | st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries); |
987e857f | 2153 | |
1e77810d BC |
2154 | for (i = 0; i < sc->chunk_count; i++) { |
2155 | current_offset = sc->chunk_offsets[i]; | |
9dd94f83 | 2156 | while (stsc_index + 1 < sc->stsc_count && |
04e06cfa | 2157 | i + 1 == sc->stsc_data[stsc_index + 1].first) |
1e77810d | 2158 | stsc_index++; |
04e06cfa | 2159 | for (j = 0; j < sc->stsc_data[stsc_index].count; j++) { |
171a3531 | 2160 | int keyframe = 0; |
1e77810d BC |
2161 | if (current_sample >= sc->sample_count) { |
2162 | av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n"); | |
f0f2218d | 2163 | return; |
1e77810d | 2164 | } |
171a3531 | 2165 | |
accea4d9 | 2166 | if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) { |
171a3531 | 2167 | keyframe = 1; |
1e77810d BC |
2168 | if (stss_index + 1 < sc->keyframe_count) |
2169 | stss_index++; | |
171a3531 BC |
2170 | } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) { |
2171 | keyframe = 1; | |
2172 | if (stps_index + 1 < sc->stps_count) | |
2173 | stps_index++; | |
1e77810d | 2174 | } |
d17d0ec8 YN |
2175 | if (rap_group_present && rap_group_index < sc->rap_group_count) { |
2176 | if (sc->rap_group[rap_group_index].index > 0) | |
2177 | keyframe = 1; | |
2178 | if (++rap_group_sample == sc->rap_group[rap_group_index].count) { | |
2179 | rap_group_sample = 0; | |
2180 | rap_group_index++; | |
2181 | } | |
2182 | } | |
171a3531 BC |
2183 | if (keyframe) |
2184 | distance = 0; | |
1e77810d | 2185 | sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample]; |
5a7ba586 | 2186 | if (sc->pseudo_stream_id == -1 || |
04e06cfa | 2187 | sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) { |
987e857f BC |
2188 | AVIndexEntry *e = &st->index_entries[st->nb_index_entries++]; |
2189 | e->pos = current_offset; | |
91eee2af | 2190 | e->timestamp = current_dts; |
987e857f BC |
2191 | e->size = sample_size; |
2192 | e->min_distance = distance; | |
2193 | e->flags = keyframe ? AVINDEX_KEYFRAME : 0; | |
1a3eb042 | 2194 | av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", " |
585dac65 | 2195 | "size %d, distance %d, keyframe %d\n", st->index, current_sample, |
91eee2af | 2196 | current_offset, current_dts, sample_size, distance, keyframe); |
585dac65 | 2197 | } |
6c00a9de | 2198 | |
1e77810d | 2199 | current_offset += sample_size; |
c5898e86 | 2200 | stream_size += sample_size; |
bbe46bc4 | 2201 | current_dts += sc->stts_data[stts_index].duration; |
1e77810d BC |
2202 | distance++; |
2203 | stts_sample++; | |
2204 | current_sample++; | |
2205 | if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) { | |
2206 | stts_sample = 0; | |
2207 | stts_index++; | |
2208 | } | |
2209 | } | |
2210 | } | |
fedb1eca BC |
2211 | if (st->duration > 0) |
2212 | st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration; | |
7e69621f | 2213 | } else { |
987e857f | 2214 | unsigned chunk_samples, total = 0; |
7e69621f | 2215 | |
987e857f BC |
2216 | // compute total chunk count |
2217 | for (i = 0; i < sc->stsc_count; i++) { | |
2218 | unsigned count, chunk_count; | |
7e69621f | 2219 | |
987e857f | 2220 | chunk_samples = sc->stsc_data[i].count; |
a294a7a1 CEH |
2221 | if (i != sc->stsc_count - 1 && |
2222 | sc->samples_per_frame && chunk_samples % sc->samples_per_frame) { | |
7e69621f BC |
2223 | av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n"); |
2224 | return; | |
2225 | } | |
2226 | ||
987e857f BC |
2227 | if (sc->samples_per_frame >= 160) { // gsm |
2228 | count = chunk_samples / sc->samples_per_frame; | |
2229 | } else if (sc->samples_per_frame > 1) { | |
2230 | unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame; | |
2231 | count = (chunk_samples+samples-1) / samples; | |
2232 | } else { | |
2233 | count = (chunk_samples+1023) / 1024; | |
2234 | } | |
2235 | ||
2236 | if (i < sc->stsc_count - 1) | |
2237 | chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first; | |
2238 | else | |
2239 | chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1); | |
2240 | total += chunk_count * count; | |
2241 | } | |
2242 | ||
1a3eb042 | 2243 | av_log(mov->fc, AV_LOG_TRACE, "chunk count %d\n", total); |
4f7c7624 | 2244 | if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries) |
987e857f | 2245 | return; |
f369b935 AK |
2246 | if (av_reallocp_array(&st->index_entries, |
2247 | st->nb_index_entries + total, | |
2248 | sizeof(*st->index_entries)) < 0) { | |
2249 | st->nb_index_entries = 0; | |
987e857f | 2250 | return; |
f369b935 | 2251 | } |
4f7c7624 | 2252 | st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries); |
987e857f BC |
2253 | |
2254 | // populate index | |
2255 | for (i = 0; i < sc->chunk_count; i++) { | |
2256 | current_offset = sc->chunk_offsets[i]; | |
2257 | if (stsc_index + 1 < sc->stsc_count && | |
2258 | i + 1 == sc->stsc_data[stsc_index + 1].first) | |
2259 | stsc_index++; | |
2260 | chunk_samples = sc->stsc_data[stsc_index].count; | |
2261 | ||
7e69621f | 2262 | while (chunk_samples > 0) { |
987e857f | 2263 | AVIndexEntry *e; |
7e69621f BC |
2264 | unsigned size, samples; |
2265 | ||
2266 | if (sc->samples_per_frame >= 160) { // gsm | |
2267 | samples = sc->samples_per_frame; | |
2268 | size = sc->bytes_per_frame; | |
2269 | } else { | |
2270 | if (sc->samples_per_frame > 1) { | |
2271 | samples = FFMIN((1024 / sc->samples_per_frame)* | |
2272 | sc->samples_per_frame, chunk_samples); | |
2273 | size = (samples / sc->samples_per_frame) * sc->bytes_per_frame; | |
1e77810d | 2274 | } else { |
7e69621f BC |
2275 | samples = FFMIN(1024, chunk_samples); |
2276 | size = samples * sc->sample_size; | |
1e77810d BC |
2277 | } |
2278 | } | |
7e69621f | 2279 | |
987e857f BC |
2280 | if (st->nb_index_entries >= total) { |
2281 | av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total); | |
2282 | return; | |
2283 | } | |
2284 | e = &st->index_entries[st->nb_index_entries++]; | |
2285 | e->pos = current_offset; | |
2286 | e->timestamp = current_dts; | |
2287 | e->size = size; | |
2288 | e->min_distance = 0; | |
2289 | e->flags = AVINDEX_KEYFRAME; | |
1a3eb042 | 2290 | av_log(mov->fc, AV_LOG_TRACE, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", " |
117a9190 | 2291 | "size %d, duration %d\n", st->index, i, current_offset, current_dts, |
7e69621f BC |
2292 | size, samples); |
2293 | ||
2294 | current_offset += size; | |
bbe46bc4 | 2295 | current_dts += samples; |
7e69621f | 2296 | chunk_samples -= samples; |
1e77810d BC |
2297 | } |
2298 | } | |
2299 | } | |
1e77810d | 2300 | } |
bd991df2 | 2301 | |
9d77a8fa MS |
2302 | static int mov_open_dref(AVIOContext **pb, char *src, MOVDref *ref, |
2303 | AVIOInterruptCB *int_cb) | |
adeb9071 | 2304 | { |
6a245905 MN |
2305 | /* try relative path, we do not try the absolute because it can leak information about our |
2306 | system to an attacker */ | |
9286de04 | 2307 | if (ref->nlvl_to > 0 && ref->nlvl_from > 0 && ref->path[0] != '/') { |
adeb9071 BC |
2308 | char filename[1024]; |
2309 | char *src_path; | |
2310 | int i, l; | |
2311 | ||
2312 | /* find a source dir */ | |
2313 | src_path = strrchr(src, '/'); | |
2314 | if (src_path) | |
2315 | src_path++; | |
2316 | else | |
2317 | src_path = src; | |
2318 | ||
2319 | /* find a next level down to target */ | |
2320 | for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--) | |
2321 | if (ref->path[l] == '/') { | |
2322 | if (i == ref->nlvl_to - 1) | |
2323 | break; | |
2324 | else | |
2325 | i++; | |
2326 | } | |
2327 | ||
2328 | /* compose filename if next level down to target was found */ | |
bbdf0d22 | 2329 | if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) { |
adeb9071 BC |
2330 | memcpy(filename, src, src_path - src); |
2331 | filename[src_path - src] = 0; | |
2332 | ||
2333 | for (i = 1; i < ref->nlvl_from; i++) | |
2334 | av_strlcat(filename, "../", 1024); | |
2335 | ||
2336 | av_strlcat(filename, ref->path + l + 1, 1024); | |
2337 | ||
9d77a8fa | 2338 | if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL)) |
adeb9071 BC |
2339 | return 0; |
2340 | } | |
2341 | } | |
2342 | ||
2343 | return AVERROR(ENOENT); | |
628b16f4 | 2344 | } |
adeb9071 | 2345 | |
ae628ec1 | 2346 | static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
5ca1d879 ZK |
2347 | { |
2348 | AVStream *st; | |
2349 | MOVStreamContext *sc; | |
bd991df2 | 2350 | int ret; |
5ca1d879 | 2351 | |
84ad31ff | 2352 | st = avformat_new_stream(c->fc, NULL); |
3efe8848 | 2353 | if (!st) return AVERROR(ENOMEM); |
84ad31ff | 2354 | st->id = c->fc->nb_streams; |
9a630c25 | 2355 | sc = av_mallocz(sizeof(MOVStreamContext)); |
2922cbdb | 2356 | if (!sc) return AVERROR(ENOMEM); |
5ca1d879 | 2357 | |
5ca1d879 | 2358 | st->priv_data = sc; |
72415b2a | 2359 | st->codec->codec_type = AVMEDIA_TYPE_DATA; |
64d50fa5 | 2360 | sc->ffindex = st->index; |
5ca1d879 | 2361 | |
bd991df2 BC |
2362 | if ((ret = mov_read_default(c, pb, atom)) < 0) |
2363 | return ret; | |
2364 | ||
2365 | /* sanity checks */ | |
6c00a9de BC |
2366 | if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count || |
2367 | (!sc->sample_size && !sc->sample_count))) { | |
6282c5f4 BC |
2368 | av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n", |
2369 | st->index); | |
bd991df2 BC |
2370 | return 0; |
2371 | } | |
6c00a9de | 2372 | |
c3024f9e | 2373 | if (sc->time_scale <= 0) { |
507330b9 | 2374 | av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index); |
6c00a9de | 2375 | sc->time_scale = c->time_scale; |
c3024f9e | 2376 | if (sc->time_scale <= 0) |
507330b9 BC |
2377 | sc->time_scale = 1; |
2378 | } | |
6c00a9de | 2379 | |
c3f9ebf7 | 2380 | avpriv_set_pts_info(st, 64, 1, sc->time_scale); |
bd991df2 | 2381 | |
bd991df2 BC |
2382 | mov_build_index(c, st); |
2383 | ||
2384 | if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) { | |
adeb9071 | 2385 | MOVDref *dref = &sc->drefs[sc->dref_id - 1]; |
9d77a8fa | 2386 | if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback) < 0) |
adeb9071 BC |
2387 | av_log(c->fc, AV_LOG_ERROR, |
2388 | "stream %d, error opening alias: path='%s', dir='%s', " | |
2389 | "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n", | |
2390 | st->index, dref->path, dref->dir, dref->filename, | |
2391 | dref->volume, dref->nlvl_from, dref->nlvl_to); | |
bd991df2 BC |
2392 | } else |
2393 | sc->pb = c->fc->pb; | |
2394 | ||
72415b2a | 2395 | if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { |
525fcb27 AW |
2396 | if (!st->sample_aspect_ratio.num && |
2397 | (st->codec->width != sc->width || st->codec->height != sc->height)) { | |
2398 | st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) / | |
2399 | ((double)st->codec->width * sc->height), INT_MAX); | |
c3aeaa54 | 2400 | } |
c9566115 BC |
2401 | } |
2402 | ||
ecf442a5 RD |
2403 | // done for ai5q, ai52, ai55, ai1q, ai12 and ai15. |
2404 | if (!st->codec->extradata_size && st->codec->codec_id == AV_CODEC_ID_H264 && | |
2405 | TAG_IS_AVCI(st->codec->codec_tag)) { | |
2406 | ret = ff_generate_avci_extradata(st); | |
2407 | if (ret < 0) | |
2408 | return ret; | |
2409 | } | |
2410 | ||
bd991df2 | 2411 | switch (st->codec->codec_id) { |
b250f9c6 | 2412 | #if CONFIG_H261_DECODER |
36ef5369 | 2413 | case AV_CODEC_ID_H261: |
bd991df2 | 2414 | #endif |
b250f9c6 | 2415 | #if CONFIG_H263_DECODER |
36ef5369 | 2416 | case AV_CODEC_ID_H263: |
bd991df2 | 2417 | #endif |
b250f9c6 | 2418 | #if CONFIG_MPEG4_DECODER |
36ef5369 | 2419 | case AV_CODEC_ID_MPEG4: |
bd991df2 | 2420 | #endif |
6c00a9de | 2421 | st->codec->width = 0; /* let decoder init width/height */ |
bd991df2 BC |
2422 | st->codec->height= 0; |
2423 | break; | |
bd991df2 | 2424 | } |
f9900374 BC |
2425 | |
2426 | /* Do not need those anymore. */ | |
2427 | av_freep(&sc->chunk_offsets); | |
04e06cfa | 2428 | av_freep(&sc->stsc_data); |
f9900374 BC |
2429 | av_freep(&sc->sample_sizes); |
2430 | av_freep(&sc->keyframes); | |
2431 | av_freep(&sc->stts_data); | |
171a3531 | 2432 | av_freep(&sc->stps_data); |
d17d0ec8 | 2433 | av_freep(&sc->rap_group); |
f9900374 | 2434 | |
bd991df2 | 2435 | return 0; |
5ca1d879 ZK |
2436 | } |
2437 | ||
ae628ec1 | 2438 | static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
51c15201 BC |
2439 | { |
2440 | int ret; | |
2441 | c->itunes_metadata = 1; | |
2442 | ret = mov_read_default(c, pb, atom); | |
2443 | c->itunes_metadata = 0; | |
2444 | return ret; | |
2445 | } | |
2446 | ||
9a07fac6 AK |
2447 | static int mov_read_replaygain(MOVContext *c, AVIOContext *pb, int size) |
2448 | { | |
2449 | int64_t end = avio_tell(pb) + size; | |
2450 | uint8_t *key = NULL, *val = NULL; | |
2451 | int i; | |
2452 | ||
2453 | for (i = 0; i < 2; i++) { | |
2454 | uint8_t **p; | |
2455 | uint32_t len, tag; | |
2456 | ||
2457 | if (end - avio_tell(pb) <= 12) | |
2458 | break; | |
2459 | ||
2460 | len = avio_rb32(pb); | |
2461 | tag = avio_rl32(pb); | |
2462 | avio_skip(pb, 4); // flags | |
2463 | ||
2464 | if (len < 12 || len - 12 > end - avio_tell(pb)) | |
2465 | break; | |
2466 | len -= 12; | |
2467 | ||
2468 | if (tag == MKTAG('n', 'a', 'm', 'e')) | |
2469 | p = &key; | |
2470 | else if (tag == MKTAG('d', 'a', 't', 'a') && len > 4) { | |
2471 | avio_skip(pb, 4); | |
2472 | len -= 4; | |
2473 | p = &val; | |
2474 | } else | |
2475 | break; | |
2476 | ||
2477 | *p = av_malloc(len + 1); | |
2478 | if (!*p) | |
2479 | break; | |
2480 | avio_read(pb, *p, len); | |
2481 | (*p)[len] = 0; | |
2482 | } | |
2483 | ||
2484 | if (key && val) { | |
2485 | av_dict_set(&c->fc->metadata, key, val, | |
2486 | AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL); | |
2487 | key = val = NULL; | |
2488 | } | |
2489 | ||
2490 | avio_seek(pb, end, SEEK_SET); | |
2491 | av_freep(&key); | |
2492 | av_freep(&val); | |
2493 | return 0; | |
2494 | } | |
2495 | ||
2496 | static int mov_read_custom(MOVContext *c, AVIOContext *pb, MOVAtom atom) | |
2497 | { | |
2498 | int64_t end = avio_tell(pb) + atom.size; | |
2499 | uint32_t tag, len; | |
2500 | ||
2501 | if (atom.size < 8) | |
2502 | goto fail; | |
2503 | ||
2504 | len = avio_rb32(pb); | |
2505 | tag = avio_rl32(pb); | |
2506 | ||
2507 | if (len > atom.size) | |
2508 | goto fail; | |
2509 | ||
2510 | if (tag == MKTAG('m', 'e', 'a', 'n') && len > 12) { | |
2511 | uint8_t domain[128]; | |
2512 | int domain_len; | |
2513 | ||
2514 | avio_skip(pb, 4); // flags | |
2515 | len -= 12; | |
2516 | ||
2517 | domain_len = avio_get_str(pb, len, domain, sizeof(domain)); | |
2518 | avio_skip(pb, len - domain_len); | |
2519 | if (!strcmp(domain, "org.hydrogenaudio.replaygain")) | |
2520 | return mov_read_replaygain(c, pb, end - avio_tell(pb)); | |
2521 | } | |
2522 | ||
2523 | fail: | |
2524 | av_log(c->fc, AV_LOG_VERBOSE, | |
2525 | "Unhandled or malformed custom metadata of size %"PRId64"\n", atom.size); | |
2526 | return 0; | |
2527 | } | |
2528 | ||
ae628ec1 | 2529 | static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
51c15201 | 2530 | { |
df8843c6 | 2531 | while (atom.size > 8) { |
b7effd4e | 2532 | uint32_t tag = avio_rl32(pb); |
df8843c6 BC |
2533 | atom.size -= 4; |
2534 | if (tag == MKTAG('h','d','l','r')) { | |
6b4aa5da | 2535 | avio_seek(pb, -8, SEEK_CUR); |
df8843c6 BC |
2536 | atom.size += 8; |
2537 | return mov_read_default(c, pb, atom); | |
2538 | } | |
2539 | } | |
2540 | return 0; | |
51c15201 BC |
2541 | } |
2542 | ||
ae628ec1 | 2543 | static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
5ca1d879 | 2544 | { |
ec072669 JS |
2545 | int i; |
2546 | int width; | |
2547 | int height; | |
2548 | int64_t disp_transform[2]; | |
853cc025 | 2549 | int display_matrix[3][3]; |
6a63ff19 BC |
2550 | AVStream *st; |
2551 | MOVStreamContext *sc; | |
2552 | int version; | |
1f70a5ad | 2553 | int flags; |
6a63ff19 BC |
2554 | |
2555 | if (c->fc->nb_streams < 1) | |
2556 | return 0; | |
2557 | st = c->fc->streams[c->fc->nb_streams-1]; | |
2558 | sc = st->priv_data; | |
5ca1d879 | 2559 | |
b7effd4e | 2560 | version = avio_r8(pb); |
1f70a5ad JS |
2561 | flags = avio_rb24(pb); |
2562 | st->disposition |= (flags & MOV_TKHD_FLAG_ENABLED) ? AV_DISPOSITION_DEFAULT : 0; | |
5ca1d879 | 2563 | |
1175561e | 2564 | if (version == 1) { |
b7effd4e AK |
2565 | avio_rb64(pb); |
2566 | avio_rb64(pb); | |
1175561e | 2567 | } else { |
b7effd4e AK |
2568 | avio_rb32(pb); /* creation time */ |
2569 | avio_rb32(pb); /* modification time */ | |
1175561e | 2570 | } |
b7effd4e AK |
2571 | st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/ |
2572 | avio_rb32(pb); /* reserved */ | |
fcdd622c | 2573 | |
117a9190 | 2574 | /* highlevel (considering edits) duration in movie timebase */ |
b7effd4e AK |
2575 | (version == 1) ? avio_rb64(pb) : avio_rb32(pb); |
2576 | avio_rb32(pb); /* reserved */ | |
2577 | avio_rb32(pb); /* reserved */ | |
5ca1d879 | 2578 | |
b7effd4e AK |
2579 | avio_rb16(pb); /* layer */ |
2580 | avio_rb16(pb); /* alternate group */ | |
2581 | avio_rb16(pb); /* volume */ | |
2582 | avio_rb16(pb); /* reserved */ | |
5ca1d879 | 2583 | |
ec072669 JS |
2584 | //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2) |
2585 | // they're kept in fixed point format through all calculations | |
853cc025 VG |
2586 | // save u,v,z to store the whole matrix in the AV_PKT_DATA_DISPLAYMATRIX |
2587 | // side data, but the scale factor is not needed to calculate aspect ratio | |
ec072669 | 2588 | for (i = 0; i < 3; i++) { |
b7effd4e AK |
2589 | display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point |
2590 | display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point | |
853cc025 | 2591 | display_matrix[i][2] = avio_rb32(pb); // 2.30 fixed point |
ec072669 | 2592 | } |
5ca1d879 | 2593 | |
b7effd4e AK |
2594 | width = avio_rb32(pb); // 16.16 fixed point track width |
2595 | height = avio_rb32(pb); // 16.16 fixed point track height | |
6cdbff63 DC |
2596 | sc->width = width >> 16; |
2597 | sc->height = height >> 16; | |
ec072669 | 2598 | |
853cc025 VG |
2599 | // save the matrix when it is not the default identity |
2600 | if (display_matrix[0][0] != (1 << 16) || | |
2601 | display_matrix[1][1] != (1 << 16) || | |
2602 | display_matrix[2][2] != (1 << 30) || | |
2603 | display_matrix[0][1] || display_matrix[0][2] || | |
2604 | display_matrix[1][0] || display_matrix[1][2] || | |
2605 | display_matrix[2][0] || display_matrix[2][1]) { | |
2606 | int i, j; | |
2607 | ||
2608 | av_freep(&sc->display_matrix); | |
2609 | sc->display_matrix = av_malloc(sizeof(int32_t) * 9); | |
2610 | if (!sc->display_matrix) | |
2611 | return AVERROR(ENOMEM); | |
2612 | ||
2613 | for (i = 0; i < 3; i++) | |
2614 | for (j = 0; j < 3; j++) | |
e4fe535d | 2615 | sc->display_matrix[i * 3 + j] = display_matrix[i][j]; |
853cc025 VG |
2616 | } |
2617 | ||
d862fce7 | 2618 | // transform the display width/height according to the matrix |
4c5fa628 | 2619 | // skip this when the display matrix is the identity one |
ec072669 | 2620 | // to keep the same scale, use [width height 1<<16] |
4c5fa628 | 2621 | if (width && height && sc->display_matrix) { |
ec072669 JS |
2622 | for (i = 0; i < 2; i++) |
2623 | disp_transform[i] = | |
2624 | (int64_t) width * display_matrix[0][i] + | |
2625 | (int64_t) height * display_matrix[1][i] + | |
2626 | ((int64_t) display_matrix[2][i] << 16); | |
2627 | ||
2628 | //sample aspect ratio is new width/height divided by old width/height | |
4c5fa628 VG |
2629 | if (disp_transform[0] > 0 && disp_transform[1] > 0) |
2630 | st->sample_aspect_ratio = av_d2q( | |
2631 | ((double) disp_transform[0] * height) / | |
2632 | ((double) disp_transform[1] * width), INT_MAX); | |
ec072669 | 2633 | } |
5ca1d879 ZK |
2634 | return 0; |
2635 | } | |
2636 | ||
ae628ec1 | 2637 | static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
61aedb0f BC |
2638 | { |
2639 | MOVFragment *frag = &c->fragment; | |
2640 | MOVTrackExt *trex = NULL; | |
2641 | int flags, track_id, i; | |
2642 | ||
b7effd4e AK |
2643 | avio_r8(pb); /* version */ |
2644 | flags = avio_rb24(pb); | |
61aedb0f | 2645 | |
b7effd4e | 2646 | track_id = avio_rb32(pb); |
63581eb1 | 2647 | if (!track_id) |
bcd5d979 | 2648 | return AVERROR_INVALIDDATA; |
61aedb0f BC |
2649 | frag->track_id = track_id; |
2650 | for (i = 0; i < c->trex_count; i++) | |
2651 | if (c->trex_data[i].track_id == frag->track_id) { | |
2652 | trex = &c->trex_data[i]; | |
2653 | break; | |
2654 | } | |
2655 | if (!trex) { | |
2656 | av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n"); | |
bcd5d979 | 2657 | return AVERROR_INVALIDDATA; |
61aedb0f BC |
2658 | } |
2659 | ||
73328f24 | 2660 | frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ? |
20f95f21 YN |
2661 | avio_rb64(pb) : flags & MOV_TFHD_DEFAULT_BASE_IS_MOOF ? |
2662 | frag->moof_offset : frag->implicit_offset; | |
73328f24 MS |
2663 | frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id; |
2664 | ||
2665 | frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ? | |
2666 | avio_rb32(pb) : trex->duration; | |
2667 | frag->size = flags & MOV_TFHD_DEFAULT_SIZE ? | |
2668 | avio_rb32(pb) : trex->size; | |
2669 | frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ? | |
2670 | avio_rb32(pb) : trex->flags; | |
1a3eb042 | 2671 | av_log(c->fc, AV_LOG_TRACE, "frag flags 0x%x\n", frag->flags); |
61aedb0f BC |
2672 | return 0; |
2673 | } | |
2674 | ||
ae628ec1 | 2675 | static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
7221579b | 2676 | { |
b7effd4e | 2677 | c->chapter_track = avio_rb32(pb); |
7221579b DC |
2678 | return 0; |
2679 | } | |
2680 | ||
ae628ec1 | 2681 | static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
61aedb0f BC |
2682 | { |
2683 | MOVTrackExt *trex; | |
f369b935 | 2684 | int err; |
61aedb0f BC |
2685 | |
2686 | if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data)) | |
bcd5d979 | 2687 | return AVERROR_INVALIDDATA; |
f369b935 AK |
2688 | if ((err = av_reallocp_array(&c->trex_data, c->trex_count + 1, |
2689 | sizeof(*c->trex_data))) < 0) { | |
2690 | c->trex_count = 0; | |
2691 | return err; | |
2692 | } | |
61aedb0f | 2693 | trex = &c->trex_data[c->trex_count++]; |
b7effd4e AK |
2694 | avio_r8(pb); /* version */ |
2695 | avio_rb24(pb); /* flags */ | |
2696 | trex->track_id = avio_rb32(pb); | |
2697 | trex->stsd_id = avio_rb32(pb); | |
2698 | trex->duration = avio_rb32(pb); | |
2699 | trex->size = avio_rb32(pb); | |
2700 | trex->flags = avio_rb32(pb); | |
61aedb0f BC |
2701 | return 0; |
2702 | } | |
2703 | ||
a74f8121 MS |
2704 | static int mov_read_tfdt(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
2705 | { | |
2706 | MOVFragment *frag = &c->fragment; | |
2707 | AVStream *st = NULL; | |
2708 | MOVStreamContext *sc; | |
2709 | int version, i; | |
2710 | ||
2711 | for (i = 0; i < c->fc->nb_streams; i++) { | |
2712 | if (c->fc->streams[i]->id == frag->track_id) { | |
2713 | st = c->fc->streams[i]; | |
2714 | break; | |
2715 | } | |
2716 | } | |
2717 | if (!st) { | |
2718 | av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id); | |
2719 | return AVERROR_INVALIDDATA; | |
2720 | } | |
2721 | sc = st->priv_data; | |
2722 | if (sc->pseudo_stream_id + 1 != frag->stsd_id) | |
2723 | return 0; | |
2724 | version = avio_r8(pb); | |
2725 | avio_rb24(pb); /* flags */ | |
2726 | if (version) { | |
2727 | sc->track_end = avio_rb64(pb); | |
2728 | } else { | |
2729 | sc->track_end = avio_rb32(pb); | |
2730 | } | |
2731 | return 0; | |
2732 | } | |
2733 | ||
ae628ec1 | 2734 | static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
61aedb0f BC |
2735 | { |
2736 | MOVFragment *frag = &c->fragment; | |
63581eb1 | 2737 | AVStream *st = NULL; |
44e43aab | 2738 | MOVStreamContext *sc; |
45a811b5 | 2739 | MOVStts *ctts_data; |
61aedb0f BC |
2740 | uint64_t offset; |
2741 | int64_t dts; | |
2742 | int data_offset = 0; | |
2743 | unsigned entries, first_sample_flags = frag->flags; | |
f369b935 | 2744 | int flags, distance, i, found_keyframe = 0, err; |
61aedb0f | 2745 | |
63581eb1 AC |
2746 | for (i = 0; i < c->fc->nb_streams; i++) { |
2747 | if (c->fc->streams[i]->id == frag->track_id) { | |
2748 | st = c->fc->streams[i]; | |
2749 | break; | |
2750 | } | |
2751 | } | |
2752 | if (!st) { | |
2753 | av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id); | |
bcd5d979 | 2754 | return AVERROR_INVALIDDATA; |
63581eb1 | 2755 | } |
44e43aab | 2756 | sc = st->priv_data; |
61aedb0f BC |
2757 | if (sc->pseudo_stream_id+1 != frag->stsd_id) |
2758 | return 0; | |
b7effd4e AK |
2759 | avio_r8(pb); /* version */ |
2760 | flags = avio_rb24(pb); | |
2761 | entries = avio_rb32(pb); | |
1a3eb042 | 2762 | av_log(c->fc, AV_LOG_TRACE, "flags 0x%x entries %d\n", flags, entries); |
45a811b5 YN |
2763 | |
2764 | /* Always assume the presence of composition time offsets. | |
2765 | * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following. | |
2766 | * 1) in the initial movie, there are no samples. | |
2767 | * 2) in the first movie fragment, there is only one sample without composition time offset. | |
2768 | * 3) in the subsequent movie fragments, there are samples with composition time offset. */ | |
2769 | if (!sc->ctts_count && sc->sample_count) | |
2770 | { | |
2771 | /* Complement ctts table if moov atom doesn't have ctts atom. */ | |
b698542a | 2772 | ctts_data = av_realloc(NULL, sizeof(*sc->ctts_data)); |
0d8f0abf | 2773 | if (!ctts_data) |
61aedb0f | 2774 | return AVERROR(ENOMEM); |
0d8f0abf | 2775 | sc->ctts_data = ctts_data; |
45a811b5 YN |
2776 | sc->ctts_data[sc->ctts_count].count = sc->sample_count; |
2777 | sc->ctts_data[sc->ctts_count].duration = 0; | |
2778 | sc->ctts_count++; | |
61aedb0f | 2779 | } |
45a811b5 | 2780 | if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data)) |
bcd5d979 | 2781 | return AVERROR_INVALIDDATA; |
f369b935 AK |
2782 | if ((err = av_reallocp_array(&sc->ctts_data, entries + sc->ctts_count, |
2783 | sizeof(*sc->ctts_data))) < 0) { | |
2784 | sc->ctts_count = 0; | |
2785 | return err; | |
2786 | } | |
3eec23f3 MS |
2787 | if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb); |
2788 | if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb); | |
7f19bdc2 | 2789 | dts = sc->track_end - sc->time_offset; |
61aedb0f BC |
2790 | offset = frag->base_data_offset + data_offset; |
2791 | distance = 0; | |
1a3eb042 | 2792 | av_log(c->fc, AV_LOG_TRACE, "first sample flags 0x%x\n", first_sample_flags); |
9888ffb1 | 2793 | for (i = 0; i < entries && !pb->eof_reached; i++) { |
61aedb0f BC |
2794 | unsigned sample_size = frag->size; |
2795 | int sample_flags = i ? frag->flags : first_sample_flags; | |
2796 | unsigned sample_duration = frag->duration; | |
a5c50913 | 2797 | int keyframe = 0; |
61aedb0f | 2798 | |
3eec23f3 MS |
2799 | if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb); |
2800 | if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb); | |
2801 | if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb); | |
91eee2af MS |
2802 | sc->ctts_data[sc->ctts_count].count = 1; |
2803 | sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ? | |
2804 | avio_rb32(pb) : 0; | |
45a811b5 | 2805 | sc->ctts_count++; |
a5c50913 MS |
2806 | if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) |
2807 | keyframe = 1; | |
2808 | else if (!found_keyframe) | |
2809 | keyframe = found_keyframe = | |
2810 | !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC | | |
2811 | MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES)); | |
2812 | if (keyframe) | |
61aedb0f | 2813 | distance = 0; |
91eee2af | 2814 | av_add_index_entry(st, offset, dts, sample_size, distance, |
61aedb0f | 2815 | keyframe ? AVINDEX_KEYFRAME : 0); |
1a3eb042 | 2816 | av_log(c->fc, AV_LOG_TRACE, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", " |
61aedb0f | 2817 | "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i, |
91eee2af | 2818 | offset, dts, sample_size, distance, keyframe); |
61aedb0f | 2819 | distance++; |
bbe46bc4 | 2820 | dts += sample_duration; |
61aedb0f | 2821 | offset += sample_size; |
bc7d0517 | 2822 | sc->data_size += sample_size; |
61aedb0f | 2823 | } |
9888ffb1 LB |
2824 | |
2825 | if (pb->eof_reached) | |
2826 | return AVERROR_EOF; | |
2827 | ||
20f95f21 | 2828 | frag->implicit_offset = offset; |
7f19bdc2 | 2829 | st->duration = sc->track_end = dts + sc->time_offset; |
61aedb0f BC |
2830 | return 0; |
2831 | } | |
2832 | ||
5ca1d879 ZK |
2833 | /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */ |
2834 | /* like the files created with Adobe Premiere 5.0, for samples see */ | |
2835 | /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */ | |
ae628ec1 | 2836 | static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
5ca1d879 ZK |
2837 | { |
2838 | int err; | |
5ca1d879 | 2839 | |
5ca1d879 ZK |
2840 | if (atom.size < 8) |
2841 | return 0; /* continue */ | |
b7effd4e | 2842 | if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */ |
45a8a02a | 2843 | avio_skip(pb, atom.size - 4); |
5ca1d879 ZK |
2844 | return 0; |
2845 | } | |
b7effd4e | 2846 | atom.type = avio_rl32(pb); |
5ca1d879 | 2847 | atom.size -= 8; |
1c126b4f | 2848 | if (atom.type != MKTAG('m','d','a','t')) { |
45a8a02a | 2849 | avio_skip(pb, atom.size); |
5ca1d879 ZK |
2850 | return 0; |
2851 | } | |
2852 | err = mov_read_mdat(c, pb, atom); | |
5ca1d879 ZK |
2853 | return err; |
2854 | } | |
2855 | ||
ae628ec1 | 2856 | static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
0147f198 | 2857 | { |
b250f9c6 | 2858 | #if CONFIG_ZLIB |
ae628ec1 | 2859 | AVIOContext ctx; |
b6a17df4 ZK |
2860 | uint8_t *cmov_data; |
2861 | uint8_t *moov_data; /* uncompressed data */ | |
0147f198 | 2862 | long cmov_len, moov_len; |
6f04eb1e | 2863 | int ret = -1; |
b6a17df4 | 2864 | |
b7effd4e AK |
2865 | avio_rb32(pb); /* dcom atom */ |
2866 | if (avio_rl32(pb) != MKTAG('d','c','o','m')) | |
bcd5d979 | 2867 | return AVERROR_INVALIDDATA; |
b7effd4e | 2868 | if (avio_rl32(pb) != MKTAG('z','l','i','b')) { |
f3592353 | 2869 | av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !"); |
bcd5d979 | 2870 | return AVERROR_INVALIDDATA; |
0147f198 | 2871 | } |
b7effd4e AK |
2872 | avio_rb32(pb); /* cmvd atom */ |
2873 | if (avio_rl32(pb) != MKTAG('c','m','v','d')) | |
bcd5d979 | 2874 | return AVERROR_INVALIDDATA; |
b7effd4e | 2875 | moov_len = avio_rb32(pb); /* uncompressed size */ |
5ca1d879 | 2876 | cmov_len = atom.size - 6 * 4; |
5cd62665 | 2877 | |
9a630c25 | 2878 | cmov_data = av_malloc(cmov_len); |
0147f198 | 2879 | if (!cmov_data) |
0d8f0abf | 2880 | return AVERROR(ENOMEM); |
9a630c25 | 2881 | moov_data = av_malloc(moov_len); |
0147f198 FR |
2882 | if (!moov_data) { |
2883 | av_free(cmov_data); | |
0d8f0abf | 2884 | return AVERROR(ENOMEM); |
0147f198 | 2885 | } |
b7effd4e | 2886 | avio_read(pb, cmov_data, cmov_len); |
5a7ba586 | 2887 | if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK) |
6f04eb1e | 2888 | goto free_and_return; |
5a7ba586 | 2889 | if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0) |
6f04eb1e | 2890 | goto free_and_return; |
1c126b4f | 2891 | atom.type = MKTAG('m','o','o','v'); |
5ca1d879 ZK |
2892 | atom.size = moov_len; |
2893 | ret = mov_read_default(c, &ctx, atom); | |
6f04eb1e | 2894 | free_and_return: |
0147f198 FR |
2895 | av_free(moov_data); |
2896 | av_free(cmov_data); | |
2897 | return ret; | |
d966b2f0 BC |
2898 | #else |
2899 | av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n"); | |
bcd5d979 | 2900 | return AVERROR(ENOSYS); |
0147f198 | 2901 | #endif |
d966b2f0 | 2902 | } |
0147f198 | 2903 | |
baf25c9d | 2904 | /* edit list atom */ |
ae628ec1 | 2905 | static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
baf25c9d | 2906 | { |
1c4bf2ec | 2907 | MOVStreamContext *sc; |
5f0bb0ba | 2908 | int i, edit_count, version; |
e0977c80 | 2909 | |
1c4bf2ec BC |
2910 | if (c->fc->nb_streams < 1) |
2911 | return 0; | |
2912 | sc = c->fc->streams[c->fc->nb_streams-1]->priv_data; | |
2913 | ||
5f0bb0ba | 2914 | version = avio_r8(pb); /* version */ |
b7effd4e AK |
2915 | avio_rb24(pb); /* flags */ |
2916 | edit_count = avio_rb32(pb); /* entries */ | |
e0977c80 | 2917 | |
5a7ba586 | 2918 | if ((uint64_t)edit_count*12+8 > atom.size) |
bcd5d979 | 2919 | return AVERROR_INVALIDDATA; |
53e099de | 2920 | |
5a7ba586 | 2921 | for (i=0; i<edit_count; i++){ |
5f0bb0ba YN |
2922 | int64_t time; |
2923 | int64_t duration; | |
2924 | if (version == 1) { | |
2925 | duration = avio_rb64(pb); | |
2926 | time = avio_rb64(pb); | |
2927 | } else { | |
2928 | duration = avio_rb32(pb); /* segment duration */ | |
ae88e9cf | 2929 | time = (int32_t)avio_rb32(pb); /* media time */ |
5f0bb0ba | 2930 | } |
b7effd4e | 2931 | avio_rb32(pb); /* Media rate */ |
2ac736a6 RD |
2932 | if (i == 0 && time >= -1) { |
2933 | sc->time_offset = time != -1 ? time : -duration; | |
a5929abe | 2934 | } |
e0977c80 | 2935 | } |
baf2ffd3 | 2936 | |
5a7ba586 | 2937 | if (edit_count > 1) |
baf2ffd3 BC |
2938 | av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, " |
2939 | "a/v desync might occur, patch welcome\n"); | |
2940 | ||
1a3eb042 | 2941 | av_log(c->fc, AV_LOG_TRACE, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count); |
e0977c80 | 2942 | return 0; |
baf25c9d GC |
2943 | } |
2944 | ||
6cea494e | 2945 | static const MOVParseTableEntry mov_default_parse_table[] = { |
d4fdba0d | 2946 | { MKTAG('a','v','s','s'), mov_read_extradata }, |
1cf9f6ed | 2947 | { MKTAG('c','h','p','l'), mov_read_chpl }, |
1c126b4f | 2948 | { MKTAG('c','o','6','4'), mov_read_stco }, |
0d8a3656 | 2949 | { MKTAG('c','o','l','r'), mov_read_colr }, |
1c126b4f BC |
2950 | { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */ |
2951 | { MKTAG('d','i','n','f'), mov_read_default }, | |
2952 | { MKTAG('d','r','e','f'), mov_read_dref }, | |
2953 | { MKTAG('e','d','t','s'), mov_read_default }, | |
2954 | { MKTAG('e','l','s','t'), mov_read_elst }, | |
2955 | { MKTAG('e','n','d','a'), mov_read_enda }, | |
4bf3c8f2 | 2956 | { MKTAG('f','i','e','l'), mov_read_fiel }, |
1c126b4f BC |
2957 | { MKTAG('f','t','y','p'), mov_read_ftyp }, |
2958 | { MKTAG('g','l','b','l'), mov_read_glbl }, | |
2959 | { MKTAG('h','d','l','r'), mov_read_hdlr }, | |
51c15201 | 2960 | { MKTAG('i','l','s','t'), mov_read_ilst }, |
1c126b4f BC |
2961 | { MKTAG('j','p','2','h'), mov_read_extradata }, |
2962 | { MKTAG('m','d','a','t'), mov_read_mdat }, | |
2963 | { MKTAG('m','d','h','d'), mov_read_mdhd }, | |
2964 | { MKTAG('m','d','i','a'), mov_read_default }, | |
51c15201 | 2965 | { MKTAG('m','e','t','a'), mov_read_meta }, |
1c126b4f BC |
2966 | { MKTAG('m','i','n','f'), mov_read_default }, |
2967 | { MKTAG('m','o','o','f'), mov_read_moof }, | |
2968 | { MKTAG('m','o','o','v'), mov_read_moov }, | |
2969 | { MKTAG('m','v','e','x'), mov_read_default }, | |
2970 | { MKTAG('m','v','h','d'), mov_read_mvhd }, | |
2971 | { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */ | |
2972 | { MKTAG('a','l','a','c'), mov_read_extradata }, /* alac specific atom */ | |
2973 | { MKTAG('a','v','c','C'), mov_read_glbl }, | |
6da54074 | 2974 | { MKTAG('p','a','s','p'), mov_read_pasp }, |
1c126b4f BC |
2975 | { MKTAG('s','t','b','l'), mov_read_default }, |
2976 | { MKTAG('s','t','c','o'), mov_read_stco }, | |
171a3531 | 2977 | { MKTAG('s','t','p','s'), mov_read_stps }, |
653d7aeb | 2978 | { MKTAG('s','t','r','f'), mov_read_strf }, |
1c126b4f BC |
2979 | { MKTAG('s','t','s','c'), mov_read_stsc }, |
2980 | { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */ | |
2981 | { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */ | |
2982 | { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */ | |
2983 | { MKTAG('s','t','t','s'), mov_read_stts }, | |
bd27eed6 | 2984 | { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */ |
1c126b4f | 2985 | { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */ |
a74f8121 | 2986 | { MKTAG('t','f','d','t'), mov_read_tfdt }, |
1c126b4f BC |
2987 | { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */ |
2988 | { MKTAG('t','r','a','k'), mov_read_trak }, | |
2989 | { MKTAG('t','r','a','f'), mov_read_default }, | |
7221579b DC |
2990 | { MKTAG('t','r','e','f'), mov_read_default }, |
2991 | { MKTAG('c','h','a','p'), mov_read_chap }, | |
1c126b4f BC |
2992 | { MKTAG('t','r','e','x'), mov_read_trex }, |
2993 | { MKTAG('t','r','u','n'), mov_read_trun }, | |
86b0affd | 2994 | { MKTAG('u','d','t','a'), mov_read_default }, |
1c126b4f BC |
2995 | { MKTAG('w','a','v','e'), mov_read_wave }, |
2996 | { MKTAG('e','s','d','s'), mov_read_esds }, | |
bdecdd2b | 2997 | { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */ |
546adc1f | 2998 | { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */ |
1c126b4f | 2999 | { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */ |
85e9e3a9 | 3000 | { MKTAG('w','f','e','x'), mov_read_wfex }, |
1c126b4f | 3001 | { MKTAG('c','m','o','v'), mov_read_cmov }, |
1fdf18f4 | 3002 | { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */ |
89415b8e | 3003 | { MKTAG('d','v','c','1'), mov_read_dvc1 }, |
d17d0ec8 | 3004 | { MKTAG('s','b','g','p'), mov_read_sbgp }, |
ea29f965 | 3005 | { MKTAG('h','v','c','C'), mov_read_glbl }, |
9a07fac6 | 3006 | { MKTAG('-','-','-','-'), mov_read_custom }, |
bcfe2ba0 | 3007 | { 0, NULL } |
6cea494e ZK |
3008 | }; |
3009 | ||
1ce1e636 RB |
3010 | static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom) |
3011 | { | |
3012 | int64_t total_size = 0; | |
3013 | MOVAtom a; | |
3014 | int i; | |
3015 | ||
3016 | if (atom.size < 0) | |
3017 | atom.size = INT64_MAX; | |
3018 | while (total_size + 8 < atom.size && !pb->eof_reached) { | |
3019 | int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL; | |
3020 | a.size = atom.size; | |
3021 | a.type=0; | |
3022 | if (atom.size >= 8) { | |
3023 | a.size = avio_rb32(pb); | |
3024 | a.type = avio_rl32(pb); | |
3025 | } | |
1a3eb042 | 3026 | av_log(c->fc, AV_LOG_TRACE, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n", |
1ce1e636 RB |
3027 | a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size); |
3028 | total_size += 8; | |
3029 | if (a.size == 1) { /* 64 bit extended size */ | |
3030 | a.size = avio_rb64(pb) - 8; | |
3031 | total_size += 8; | |
3032 | } | |
3033 | if (a.size == 0) { | |
3034 | a.size = atom.size - total_size; | |
3035 | if (a.size <= 8) | |
3036 | break; | |
3037 | } | |
3038 | a.size -= 8; | |
3039 | if (a.size < 0) | |
3040 | break; | |
3041 | a.size = FFMIN(a.size, atom.size - total_size); | |
3042 | ||
3043 | for (i = 0; mov_default_parse_table[i].type; i++) | |
3044 | if (mov_default_parse_table[i].type == a.type) { | |
3045 | parse = mov_default_parse_table[i].parse; | |
3046 | break; | |
3047 | } | |
3048 | ||
3049 | // container is user data | |
3050 | if (!parse && (atom.type == MKTAG('u','d','t','a') || | |
3051 | atom.type == MKTAG('i','l','s','t'))) | |
3052 | parse = mov_read_udta_string; | |
3053 | ||
3054 | if (!parse) { /* skip leaf atoms data */ | |
3055 | avio_skip(pb, a.size); | |
3056 | } else { | |
3057 | int64_t start_pos = avio_tell(pb); | |
3058 | int64_t left; | |
3059 | int err = parse(c, pb, a); | |
3060 | if (err < 0) | |
3061 | return err; | |
3062 | if (c->found_moov && c->found_mdat && | |
3063 | ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) || | |
3064 | start_pos + a.size == avio_size(pb))) { | |
3065 | if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) | |
3066 | c->next_root_atom = start_pos + a.size; | |
3067 | return 0; | |
3068 | } | |
3069 | left = a.size - avio_tell(pb) + start_pos; | |
3070 | if (left > 0) /* skip garbage at atom end */ | |
3071 | avio_skip(pb, left); | |
5b4eb243 MS |
3072 | else if (left < 0) { |
3073 | av_log(c->fc, AV_LOG_WARNING, | |
3074 | "overread end of atom '%.4s' by %"PRId64" bytes\n", | |
3075 | (char*)&a.type, -left); | |
3076 | avio_seek(pb, left, SEEK_CUR); | |
3077 | } | |
1ce1e636 RB |
3078 | } |
3079 | ||
3080 | total_size += a.size; | |
3081 | } | |
3082 | ||
3083 | if (total_size < atom.size && atom.size < 0x7ffff) | |
3084 | avio_skip(pb, atom.size - total_size); | |
3085 | ||
3086 | return 0; | |
3087 | } | |
3088 | ||
c9a65ca8 FB |
3089 | static int mov_probe(AVProbeData *p) |
3090 | { | |
0e7eed09 FB |
3091 | unsigned int offset; |
3092 | uint32_t tag; | |
2497479f | 3093 | int score = 0; |
3ffe3793 | 3094 | |
c9a65ca8 | 3095 | /* check file header */ |
0e7eed09 | 3096 | offset = 0; |
5a7ba586 | 3097 | for (;;) { |
0e7eed09 FB |
3098 | /* ignore invalid offset */ |
3099 | if ((offset + 8) > (unsigned int)p->buf_size) | |
2497479f | 3100 | return score; |
fead30d4 | 3101 | tag = AV_RL32(p->buf + offset + 4); |
0e7eed09 | 3102 | switch(tag) { |
2497479f | 3103 | /* check for obvious tags */ |
1c126b4f BC |
3104 | case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */ |
3105 | case MKTAG('m','o','o','v'): | |
3106 | case MKTAG('m','d','a','t'): | |
3107 | case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */ | |
3108 | case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */ | |
40e7aaa3 | 3109 | case MKTAG('f','t','y','p'): |
3ffe3793 | 3110 | return AVPROBE_SCORE_MAX; |
2497479f | 3111 | /* those are more common words, so rate then a bit less */ |
1c126b4f BC |
3112 | case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */ |
3113 | case MKTAG('w','i','d','e'): | |
3114 | case MKTAG('f','r','e','e'): | |
3115 | case MKTAG('j','u','n','k'): | |
3116 | case MKTAG('p','i','c','t'): | |
2497479f | 3117 | return AVPROBE_SCORE_MAX - 5; |
5469b788 | 3118 | case MKTAG(0x82,0x82,0x7f,0x7d): |
1c126b4f BC |
3119 | case MKTAG('s','k','i','p'): |
3120 | case MKTAG('u','u','i','d'): | |
3121 | case MKTAG('p','r','f','l'): | |
fead30d4 | 3122 | offset = AV_RB32(p->buf+offset) + offset; |
2497479f | 3123 | /* if we only find those cause probedata is too small at least rate them */ |
e0f8be64 | 3124 | score = AVPROBE_SCORE_EXTENSION; |
0e7eed09 FB |
3125 | break; |
3126 | default: | |
3127 | /* unrecognized tag */ | |
2497479f | 3128 | return score; |
0e7eed09 | 3129 | } |
3ffe3793 | 3130 | } |
c9a65ca8 FB |
3131 | } |
3132 | ||
7221579b DC |
3133 | // must be done after parsing all trak because there's no order requirement |
3134 | static void mov_read_chapters(AVFormatContext *s) | |
3135 | { | |
3136 | MOVContext *mov = s->priv_data; | |
3137 | AVStream *st = NULL; | |
3138 | MOVStreamContext *sc; | |
3139 | int64_t cur_pos; | |
225b6d7f | 3140 | int i; |
7221579b DC |
3141 | |
3142 | for (i = 0; i < s->nb_streams; i++) | |
3143 | if (s->streams[i]->id == mov->chapter_track) { | |
3144 | st = s->streams[i]; | |
3145 | break; | |
3146 | } | |
3147 | if (!st) { | |
3148 | av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n"); | |
3149 | return; | |
3150 | } | |
3151 | ||
3152 | st->discard = AVDISCARD_ALL; | |
3153 | sc = st->priv_data; | |
a2704c97 | 3154 | cur_pos = avio_tell(sc->pb); |
7221579b DC |
3155 | |
3156 | for (i = 0; i < st->nb_index_entries; i++) { | |
3157 | AVIndexEntry *sample = &st->index_entries[i]; | |
3158 | int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration; | |
225b6d7f AK |
3159 | uint8_t *title; |
3160 | uint16_t ch; | |
3161 | int len, title_len; | |
7221579b | 3162 | |
6b4aa5da | 3163 | if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) { |
7221579b DC |
3164 | av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i); |
3165 | goto finish; | |
3166 | } | |
3167 | ||
7221579b | 3168 | // the first two bytes are the length of the title |
b7effd4e | 3169 | len = avio_rb16(sc->pb); |
7221579b DC |
3170 | if (len > sample->size-2) |
3171 | continue; | |
225b6d7f AK |
3172 | title_len = 2*len + 1; |
3173 | if (!(title = av_mallocz(title_len))) | |
3174 | goto finish; | |
7221579b DC |
3175 | |
3176 | // The samples could theoretically be in any encoding if there's an encd | |
3177 | // atom following, but in practice are only utf-8 or utf-16, distinguished | |
3178 | // instead by the presence of a BOM | |
8fb22c3d AC |
3179 | if (!len) { |
3180 | title[0] = 0; | |
3181 | } else { | |
3182 | ch = avio_rb16(sc->pb); | |
3183 | if (ch == 0xfeff) | |
3184 | avio_get_str16be(sc->pb, len, title, title_len); | |
3185 | else if (ch == 0xfffe) | |
3186 | avio_get_str16le(sc->pb, len, title, title_len); | |
3187 | else { | |
3188 | AV_WB16(title, ch); | |
3189 | if (len == 1 || len == 2) | |
7ad06beb | 3190 | title[len] = 0; |
8fb22c3d AC |
3191 | else |
3192 | avio_get_str(sc->pb, len - 2, title + 2, title_len - 2); | |
3193 | } | |
7221579b DC |
3194 | } |
3195 | ||
1fa395e4 | 3196 | avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title); |
c4f8765a | 3197 | av_freep(&title); |
7221579b DC |
3198 | } |
3199 | finish: | |
6b4aa5da | 3200 | avio_seek(sc->pb, cur_pos, SEEK_SET); |
7221579b DC |
3201 | } |
3202 | ||
c788782c DC |
3203 | static int mov_read_close(AVFormatContext *s) |
3204 | { | |
3205 | MOVContext *mov = s->priv_data; | |
3206 | int i, j; | |
3207 | ||
3208 | for (i = 0; i < s->nb_streams; i++) { | |
3209 | AVStream *st = s->streams[i]; | |
3210 | MOVStreamContext *sc = st->priv_data; | |
3211 | ||
3212 | av_freep(&sc->ctts_data); | |
3213 | for (j = 0; j < sc->drefs_count; j++) { | |
3214 | av_freep(&sc->drefs[j].path); | |
3215 | av_freep(&sc->drefs[j].dir); | |
3216 | } | |
3217 | av_freep(&sc->drefs); | |
3218 | if (sc->pb && sc->pb != s->pb) | |
3219 | avio_close(sc->pb); | |
d51f0996 MS |
3220 | |
3221 | av_freep(&sc->chunk_offsets); | |
3222 | av_freep(&sc->stsc_data); | |
3223 | av_freep(&sc->sample_sizes); | |
3224 | av_freep(&sc->keyframes); | |
3225 | av_freep(&sc->stts_data); | |
3226 | av_freep(&sc->stps_data); | |
3227 | av_freep(&sc->rap_group); | |
853cc025 | 3228 | av_freep(&sc->display_matrix); |
c788782c DC |
3229 | } |
3230 | ||
3231 | if (mov->dv_demux) { | |
edb1af7c AK |
3232 | avformat_free_context(mov->dv_fctx); |
3233 | mov->dv_fctx = NULL; | |
c788782c DC |
3234 | } |
3235 | ||
3236 | av_freep(&mov->trex_data); | |
3237 | ||
3238 | return 0; | |
3239 | } | |
3240 | ||
6e9651d1 | 3241 | static int mov_read_header(AVFormatContext *s) |
6cea494e | 3242 | { |
e4141433 | 3243 | MOVContext *mov = s->priv_data; |
ae628ec1 | 3244 | AVIOContext *pb = s->pb; |
f9900374 | 3245 | int err; |
3491866a | 3246 | MOVAtom atom = { AV_RL32("root") }; |
fce28c3c | 3247 | int i; |
6cea494e | 3248 | |
6cea494e |