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