Commit | Line | Data |
---|---|---|
d4f5d74a | 1 | /* |
7fbde343 | 2 | * FLV demuxer |
2912e87a | 3 | * Copyright (c) 2003 The Libav Project |
d4f5d74a | 4 | * |
7b94177e DB |
5 | * This demuxer will generate a 1 byte extradata for VP6F content. |
6 | * It is composed of: | |
7 | * - upper 4bits: difference between encoded width and visible width | |
8 | * - lower 4bits: difference between encoded height and visible height | |
9 | * | |
2912e87a | 10 | * This file is part of Libav. |
b78e7197 | 11 | * |
2912e87a | 12 | * Libav is free software; you can redistribute it and/or |
d4f5d74a GM |
13 | * modify it under the terms of the GNU Lesser General Public |
14 | * License as published by the Free Software Foundation; either | |
b78e7197 | 15 | * version 2.1 of the License, or (at your option) any later version. |
d4f5d74a | 16 | * |
2912e87a | 17 | * Libav is distributed in the hope that it will be useful, |
d4f5d74a GM |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * Lesser General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Lesser General Public | |
2912e87a | 23 | * License along with Libav; if not, write to the Free Software |
5509bffa | 24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
d4f5d74a | 25 | */ |
d2718187 | 26 | |
df2bd71a | 27 | #include "libavutil/avstring.h" |
644d8d2e | 28 | #include "libavutil/channel_layout.h" |
d2d67e42 | 29 | #include "libavutil/dict.h" |
5b54a90c | 30 | #include "libavutil/opt.h" |
3383a53e | 31 | #include "libavutil/intfloat.h" |
d3f751e6 | 32 | #include "libavutil/mathematics.h" |
c1c206b3 | 33 | #include "libavcodec/bytestream.h" |
d2718187 | 34 | #include "libavcodec/mpeg4audio.h" |
d4f5d74a | 35 | #include "avformat.h" |
c3f9ebf7 | 36 | #include "internal.h" |
933e90a6 | 37 | #include "avio_internal.h" |
6cac3a3b | 38 | #include "flv.h" |
d4f5d74a | 39 | |
cb7e2c1c KA |
40 | #define KEYFRAMES_TAG "keyframes" |
41 | #define KEYFRAMES_TIMESTAMP_TAG "times" | |
42 | #define KEYFRAMES_BYTEOFFSET_TAG "filepositions" | |
43 | ||
7e297a46 MS |
44 | #define VALIDATE_INDEX_TS_THRESH 2500 |
45 | ||
ebd61055 | 46 | typedef struct { |
5b54a90c | 47 | const AVClass *class; ///< Class for private options. |
e4529df9 DB |
48 | int trust_metadata; ///< configure streams according onMetaData |
49 | int wrong_dts; ///< wrong dts due to negative cts | |
251f320f | 50 | uint8_t *new_extradata[2]; |
e4529df9 DB |
51 | int new_extradata_size[2]; |
52 | int last_sample_rate; | |
53 | int last_channels; | |
7e297a46 MS |
54 | struct { |
55 | int64_t dts; | |
56 | int64_t pos; | |
57 | } validate_index[2]; | |
58 | int validate_next; | |
59 | int validate_count; | |
ebd61055 BC |
60 | } FLVContext; |
61 | ||
d4f5d74a GM |
62 | static int flv_probe(AVProbeData *p) |
63 | { | |
64 | const uint8_t *d; | |
65 | ||
d4f5d74a | 66 | d = p->buf; |
e4529df9 DB |
67 | if (d[0] == 'F' && |
68 | d[1] == 'L' && | |
69 | d[2] == 'V' && | |
70 | d[3] < 5 && d[5] == 0 && | |
71 | AV_RB32(d + 5) > 8) { | |
74248229 | 72 | return AVPROBE_SCORE_MAX; |
d4f5d74a GM |
73 | } |
74 | return 0; | |
75 | } | |
76 | ||
41f43202 | 77 | static AVStream *create_stream(AVFormatContext *s, int codec_type) |
21e2dc9f LB |
78 | { |
79 | AVStream *st = avformat_new_stream(s, NULL); | |
80 | if (!st) | |
81 | return NULL; | |
21e2dc9f LB |
82 | st->codec->codec_type = codec_type; |
83 | avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */ | |
84 | return st; | |
85 | } | |
e4529df9 | 86 | |
09a445ce LB |
87 | static int flv_same_audio_codec(AVCodecContext *acodec, int flags) |
88 | { | |
89 | int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8; | |
e4529df9 | 90 | int flv_codecid = flags & FLV_AUDIO_CODECID_MASK; |
09a445ce LB |
91 | int codec_id; |
92 | ||
93 | if (!acodec->codec_id && !acodec->codec_tag) | |
94 | return 1; | |
95 | ||
96 | if (acodec->bits_per_coded_sample != bits_per_coded_sample) | |
97 | return 0; | |
98 | ||
e4529df9 DB |
99 | switch (flv_codecid) { |
100 | // no distinction between S16 and S8 PCM codec flags | |
09a445ce | 101 | case FLV_CODECID_PCM: |
e4529df9 DB |
102 | codec_id = bits_per_coded_sample == 8 |
103 | ? AV_CODEC_ID_PCM_U8 | |
09a445ce | 104 | #if HAVE_BIGENDIAN |
e4529df9 | 105 | : AV_CODEC_ID_PCM_S16BE; |
09a445ce | 106 | #else |
e4529df9 | 107 | : AV_CODEC_ID_PCM_S16LE; |
09a445ce LB |
108 | #endif |
109 | return codec_id == acodec->codec_id; | |
110 | case FLV_CODECID_PCM_LE: | |
e4529df9 DB |
111 | codec_id = bits_per_coded_sample == 8 |
112 | ? AV_CODEC_ID_PCM_U8 | |
113 | : AV_CODEC_ID_PCM_S16LE; | |
09a445ce LB |
114 | return codec_id == acodec->codec_id; |
115 | case FLV_CODECID_AAC: | |
36ef5369 | 116 | return acodec->codec_id == AV_CODEC_ID_AAC; |
09a445ce | 117 | case FLV_CODECID_ADPCM: |
36ef5369 | 118 | return acodec->codec_id == AV_CODEC_ID_ADPCM_SWF; |
09a445ce | 119 | case FLV_CODECID_SPEEX: |
36ef5369 | 120 | return acodec->codec_id == AV_CODEC_ID_SPEEX; |
09a445ce | 121 | case FLV_CODECID_MP3: |
36ef5369 | 122 | return acodec->codec_id == AV_CODEC_ID_MP3; |
09a445ce | 123 | case FLV_CODECID_NELLYMOSER_8KHZ_MONO: |
09a445ce | 124 | case FLV_CODECID_NELLYMOSER_16KHZ_MONO: |
09a445ce | 125 | case FLV_CODECID_NELLYMOSER: |
36ef5369 | 126 | return acodec->codec_id == AV_CODEC_ID_NELLYMOSER; |
09a445ce LB |
127 | case FLV_CODECID_PCM_MULAW: |
128 | return acodec->sample_rate == 8000 && | |
e4529df9 | 129 | acodec->codec_id == AV_CODEC_ID_PCM_MULAW; |
09a445ce | 130 | case FLV_CODECID_PCM_ALAW: |
390b4d70 | 131 | return acodec->sample_rate == 8000 && |
e4529df9 | 132 | acodec->codec_id == AV_CODEC_ID_PCM_ALAW; |
09a445ce LB |
133 | default: |
134 | return acodec->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET); | |
135 | } | |
09a445ce | 136 | } |
21e2dc9f | 137 | |
e4529df9 DB |
138 | static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream, |
139 | AVCodecContext *acodec, int flv_codecid) | |
140 | { | |
141 | switch (flv_codecid) { | |
142 | // no distinction between S16 and S8 PCM codec flags | |
143 | case FLV_CODECID_PCM: | |
144 | acodec->codec_id = acodec->bits_per_coded_sample == 8 | |
145 | ? AV_CODEC_ID_PCM_U8 | |
63613fe6 | 146 | #if HAVE_BIGENDIAN |
e4529df9 | 147 | : AV_CODEC_ID_PCM_S16BE; |
58293e57 | 148 | #else |
e4529df9 | 149 | : AV_CODEC_ID_PCM_S16LE; |
58293e57 | 150 | #endif |
e4529df9 DB |
151 | break; |
152 | case FLV_CODECID_PCM_LE: | |
153 | acodec->codec_id = acodec->bits_per_coded_sample == 8 | |
154 | ? AV_CODEC_ID_PCM_U8 | |
155 | : AV_CODEC_ID_PCM_S16LE; | |
156 | break; | |
157 | case FLV_CODECID_AAC: | |
158 | acodec->codec_id = AV_CODEC_ID_AAC; | |
159 | break; | |
160 | case FLV_CODECID_ADPCM: | |
161 | acodec->codec_id = AV_CODEC_ID_ADPCM_SWF; | |
162 | break; | |
163 | case FLV_CODECID_SPEEX: | |
164 | acodec->codec_id = AV_CODEC_ID_SPEEX; | |
165 | acodec->sample_rate = 16000; | |
166 | break; | |
167 | case FLV_CODECID_MP3: | |
168 | acodec->codec_id = AV_CODEC_ID_MP3; | |
169 | astream->need_parsing = AVSTREAM_PARSE_FULL; | |
170 | break; | |
171 | case FLV_CODECID_NELLYMOSER_8KHZ_MONO: | |
172 | // in case metadata does not otherwise declare samplerate | |
173 | acodec->sample_rate = 8000; | |
174 | acodec->codec_id = AV_CODEC_ID_NELLYMOSER; | |
175 | break; | |
176 | case FLV_CODECID_NELLYMOSER_16KHZ_MONO: | |
177 | acodec->sample_rate = 16000; | |
178 | acodec->codec_id = AV_CODEC_ID_NELLYMOSER; | |
179 | break; | |
180 | case FLV_CODECID_NELLYMOSER: | |
181 | acodec->codec_id = AV_CODEC_ID_NELLYMOSER; | |
182 | break; | |
183 | case FLV_CODECID_PCM_MULAW: | |
184 | acodec->sample_rate = 8000; | |
185 | acodec->codec_id = AV_CODEC_ID_PCM_MULAW; | |
186 | break; | |
187 | case FLV_CODECID_PCM_ALAW: | |
188 | acodec->sample_rate = 8000; | |
189 | acodec->codec_id = AV_CODEC_ID_PCM_ALAW; | |
190 | break; | |
191 | default: | |
192 | av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", | |
193 | flv_codecid >> FLV_AUDIO_CODECID_OFFSET); | |
194 | acodec->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET; | |
428cc588 AH |
195 | } |
196 | } | |
197 | ||
09a445ce LB |
198 | static int flv_same_video_codec(AVCodecContext *vcodec, int flags) |
199 | { | |
200 | int flv_codecid = flags & FLV_VIDEO_CODECID_MASK; | |
201 | ||
202 | if (!vcodec->codec_id && !vcodec->codec_tag) | |
203 | return 1; | |
204 | ||
205 | switch (flv_codecid) { | |
e4529df9 DB |
206 | case FLV_CODECID_H263: |
207 | return vcodec->codec_id == AV_CODEC_ID_FLV1; | |
208 | case FLV_CODECID_SCREEN: | |
209 | return vcodec->codec_id == AV_CODEC_ID_FLASHSV; | |
210 | case FLV_CODECID_SCREEN2: | |
211 | return vcodec->codec_id == AV_CODEC_ID_FLASHSV2; | |
212 | case FLV_CODECID_VP6: | |
213 | return vcodec->codec_id == AV_CODEC_ID_VP6F; | |
214 | case FLV_CODECID_VP6A: | |
215 | return vcodec->codec_id == AV_CODEC_ID_VP6A; | |
216 | case FLV_CODECID_H264: | |
217 | return vcodec->codec_id == AV_CODEC_ID_H264; | |
218 | default: | |
219 | return vcodec->codec_tag == flv_codecid; | |
09a445ce | 220 | } |
09a445ce LB |
221 | } |
222 | ||
e4529df9 DB |
223 | static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, |
224 | int flv_codecid, int read) | |
225 | { | |
428cc588 | 226 | AVCodecContext *vcodec = vstream->codec; |
e4529df9 DB |
227 | switch (flv_codecid) { |
228 | case FLV_CODECID_H263: | |
229 | vcodec->codec_id = AV_CODEC_ID_FLV1; | |
230 | break; | |
231 | case FLV_CODECID_SCREEN: | |
232 | vcodec->codec_id = AV_CODEC_ID_FLASHSV; | |
233 | break; | |
234 | case FLV_CODECID_SCREEN2: | |
235 | vcodec->codec_id = AV_CODEC_ID_FLASHSV2; | |
236 | break; | |
237 | case FLV_CODECID_VP6: | |
238 | vcodec->codec_id = AV_CODEC_ID_VP6F; | |
239 | case FLV_CODECID_VP6A: | |
240 | if (flv_codecid == FLV_CODECID_VP6A) | |
241 | vcodec->codec_id = AV_CODEC_ID_VP6A; | |
242 | if (read) { | |
243 | if (vcodec->extradata_size != 1) { | |
244 | vcodec->extradata = av_malloc(1); | |
c5a738ca | 245 | if (vcodec->extradata) |
e4529df9 | 246 | vcodec->extradata_size = 1; |
428cc588 | 247 | } |
e4529df9 DB |
248 | if (vcodec->extradata) |
249 | vcodec->extradata[0] = avio_r8(s->pb); | |
250 | else | |
251 | avio_skip(s->pb, 1); | |
252 | } | |
253 | return 1; // 1 byte body size adjustment for flv_read_packet() | |
254 | case FLV_CODECID_H264: | |
255 | vcodec->codec_id = AV_CODEC_ID_H264; | |
256 | return 3; // not 4, reading packet type will consume one byte | |
257 | default: | |
258 | av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flv_codecid); | |
259 | vcodec->codec_tag = flv_codecid; | |
428cc588 AH |
260 | } |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
e4529df9 DB |
265 | static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize) |
266 | { | |
b7effd4e | 267 | int length = avio_rb16(ioc); |
e4529df9 | 268 | if (length >= buffsize) { |
45a8a02a | 269 | avio_skip(ioc, length); |
cc38e063 MN |
270 | return -1; |
271 | } | |
896bcd2e | 272 | |
b7effd4e | 273 | avio_read(ioc, buffer, length); |
896bcd2e | 274 | |
cc38e063 | 275 | buffer[length] = '\0'; |
896bcd2e | 276 | |
cc38e063 | 277 | return length; |
896bcd2e MN |
278 | } |
279 | ||
e4529df9 DB |
280 | static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, |
281 | AVStream *vstream, int64_t max_pos) | |
282 | { | |
283 | FLVContext *flv = s->priv_data; | |
cb7e2c1c KA |
284 | unsigned int arraylen = 0, timeslen = 0, fileposlen = 0, i; |
285 | double num_val; | |
286 | char str_val[256]; | |
e4529df9 | 287 | int64_t *times = NULL; |
cb7e2c1c | 288 | int64_t *filepositions = NULL; |
e4529df9 DB |
289 | int ret = AVERROR(ENOSYS); |
290 | int64_t initial_pos = avio_tell(ioc); | |
cb7e2c1c | 291 | |
b70f04c2 MS |
292 | if (s->flags & AVFMT_FLAG_IGNIDX) |
293 | return 0; | |
294 | ||
e4529df9 DB |
295 | while (avio_tell(ioc) < max_pos - 2 && |
296 | amf_get_string(ioc, str_val, sizeof(str_val)) > 0) { | |
297 | int64_t *current_array; | |
cb7e2c1c KA |
298 | |
299 | // Expect array object in context | |
300 | if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY) | |
301 | break; | |
302 | ||
303 | arraylen = avio_rb32(ioc); | |
a246cefa MN |
304 | if (arraylen >> 28) |
305 | break; | |
306 | ||
e4529df9 DB |
307 | /* Expect only 'times' or 'filepositions' sub-arrays in other |
308 | * case refuse to use such metadata for indexing. */ | |
cb7e2c1c KA |
309 | if (!strcmp(KEYFRAMES_TIMESTAMP_TAG, str_val) && !times) { |
310 | if (!(times = av_mallocz(sizeof(*times) * arraylen))) { | |
311 | ret = AVERROR(ENOMEM); | |
312 | goto finish; | |
313 | } | |
e4529df9 | 314 | timeslen = arraylen; |
cb7e2c1c | 315 | current_array = times; |
e4529df9 DB |
316 | } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) && |
317 | !filepositions) { | |
cb7e2c1c KA |
318 | if (!(filepositions = av_mallocz(sizeof(*filepositions) * arraylen))) { |
319 | ret = AVERROR(ENOMEM); | |
320 | goto finish; | |
321 | } | |
e4529df9 | 322 | fileposlen = arraylen; |
cb7e2c1c | 323 | current_array = filepositions; |
e4529df9 DB |
324 | } else |
325 | // unexpected metatag inside keyframes, will not use such | |
326 | // metadata for indexing | |
cb7e2c1c KA |
327 | break; |
328 | ||
329 | for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) { | |
330 | if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER) | |
331 | goto finish; | |
e4529df9 | 332 | num_val = av_int2double(avio_rb64(ioc)); |
cb7e2c1c KA |
333 | current_array[i] = num_val; |
334 | } | |
578d6861 MS |
335 | if (times && filepositions) { |
336 | // All done, exiting at a position allowing amf_parse_object | |
337 | // to finish parsing the object | |
338 | ret = 0; | |
339 | break; | |
340 | } | |
cb7e2c1c KA |
341 | } |
342 | ||
7e297a46 MS |
343 | if (!ret && timeslen == fileposlen) { |
344 | for (i = 0; i < fileposlen; i++) { | |
e4529df9 | 345 | av_add_index_entry(vstream, filepositions[i], times[i] * 1000, |
0a7ce3ca | 346 | 0, 0, AVINDEX_KEYFRAME); |
7e297a46 MS |
347 | if (i < 2) { |
348 | flv->validate_index[i].pos = filepositions[i]; | |
349 | flv->validate_index[i].dts = times[i] * 1000; | |
e4529df9 | 350 | flv->validate_count = i + 1; |
7e297a46 MS |
351 | } |
352 | } | |
353 | } else | |
cb7e2c1c KA |
354 | av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n"); |
355 | ||
356 | finish: | |
357 | av_freep(×); | |
358 | av_freep(&filepositions); | |
578d6861 MS |
359 | // If we got unexpected data, but successfully reset back to |
360 | // the start pos, the caller can continue parsing | |
361 | if (ret < 0 && avio_seek(ioc, initial_pos, SEEK_SET) > 0) | |
362 | return 0; | |
cb7e2c1c KA |
363 | return ret; |
364 | } | |
365 | ||
e4529df9 DB |
366 | static int amf_parse_object(AVFormatContext *s, AVStream *astream, |
367 | AVStream *vstream, const char *key, | |
368 | int64_t max_pos, int depth) | |
369 | { | |
428cc588 | 370 | AVCodecContext *acodec, *vcodec; |
5b54a90c | 371 | FLVContext *flv = s->priv_data; |
ae628ec1 | 372 | AVIOContext *ioc; |
428cc588 | 373 | AMFDataType amf_type; |
cc38e063 | 374 | char str_val[256]; |
428cc588 AH |
375 | double num_val; |
376 | ||
e4529df9 DB |
377 | num_val = 0; |
378 | ioc = s->pb; | |
b7effd4e | 379 | amf_type = avio_r8(ioc); |
428cc588 | 380 | |
e4529df9 DB |
381 | switch (amf_type) { |
382 | case AMF_DATA_TYPE_NUMBER: | |
383 | num_val = av_int2double(avio_rb64(ioc)); | |
384 | break; | |
385 | case AMF_DATA_TYPE_BOOL: | |
386 | num_val = avio_r8(ioc); | |
387 | break; | |
388 | case AMF_DATA_TYPE_STRING: | |
389 | if (amf_get_string(ioc, str_val, sizeof(str_val)) < 0) | |
390 | return -1; | |
391 | break; | |
392 | case AMF_DATA_TYPE_OBJECT: | |
393 | if ((vstream || astream) && key && | |
394 | !strcmp(KEYFRAMES_TAG, key) && depth == 1) | |
395 | if (parse_keyframes_index(s, ioc, vstream ? vstream : astream, | |
396 | max_pos) < 0) | |
428cc588 | 397 | return -1; |
428cc588 | 398 | |
e4529df9 DB |
399 | while (avio_tell(ioc) < max_pos - 2 && |
400 | amf_get_string(ioc, str_val, sizeof(str_val)) > 0) | |
401 | if (amf_parse_object(s, astream, vstream, str_val, max_pos, | |
402 | depth + 1) < 0) | |
403 | return -1; // if we couldn't skip, bomb out. | |
404 | if (avio_r8(ioc) != AMF_END_OF_OBJECT) | |
428cc588 | 405 | return -1; |
e4529df9 DB |
406 | break; |
407 | case AMF_DATA_TYPE_NULL: | |
408 | case AMF_DATA_TYPE_UNDEFINED: | |
409 | case AMF_DATA_TYPE_UNSUPPORTED: | |
410 | break; // these take up no additional space | |
411 | case AMF_DATA_TYPE_MIXEDARRAY: | |
412 | avio_skip(ioc, 4); // skip 32-bit max array index | |
413 | while (avio_tell(ioc) < max_pos - 2 && | |
414 | amf_get_string(ioc, str_val, sizeof(str_val)) > 0) | |
415 | // this is the only case in which we would want a nested | |
416 | // parse to not skip over the object | |
417 | if (amf_parse_object(s, astream, vstream, str_val, max_pos, | |
418 | depth + 1) < 0) | |
419 | return -1; | |
420 | if (avio_r8(ioc) != AMF_END_OF_OBJECT) | |
421 | return -1; | |
422 | break; | |
423 | case AMF_DATA_TYPE_ARRAY: | |
424 | { | |
425 | unsigned int arraylen, i; | |
426 | ||
427 | arraylen = avio_rb32(ioc); | |
428 | for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) | |
429 | if (amf_parse_object(s, NULL, NULL, NULL, max_pos, | |
430 | depth + 1) < 0) | |
431 | return -1; // if we couldn't skip, bomb out. | |
432 | } | |
433 | break; | |
434 | case AMF_DATA_TYPE_DATE: | |
435 | avio_skip(ioc, 8 + 2); // timestamp (double) and UTC offset (int16) | |
436 | break; | |
437 | default: // unsupported type, we couldn't skip | |
438 | return -1; | |
428cc588 AH |
439 | } |
440 | ||
e4529df9 DB |
441 | // only look for metadata values when we are not nested and key != NULL |
442 | if (depth == 1 && key) { | |
428cc588 AH |
443 | acodec = astream ? astream->codec : NULL; |
444 | vcodec = vstream ? vstream->codec : NULL; | |
445 | ||
e4529df9 DB |
446 | if (amf_type == AMF_DATA_TYPE_NUMBER || |
447 | amf_type == AMF_DATA_TYPE_BOOL) { | |
b204c46d MS |
448 | if (!strcmp(key, "duration")) |
449 | s->duration = num_val * AV_TIME_BASE; | |
e4529df9 DB |
450 | else if (!strcmp(key, "videodatarate") && vcodec && |
451 | 0 <= (int)(num_val * 1024.0)) | |
b204c46d | 452 | vcodec->bit_rate = num_val * 1024.0; |
e4529df9 DB |
453 | else if (!strcmp(key, "audiodatarate") && acodec && |
454 | 0 <= (int)(num_val * 1024.0)) | |
b204c46d | 455 | acodec->bit_rate = num_val * 1024.0; |
21e2dc9f | 456 | else if (!strcmp(key, "datastream")) { |
41f43202 | 457 | AVStream *st = create_stream(s, AVMEDIA_TYPE_DATA); |
21e2dc9f LB |
458 | if (!st) |
459 | return AVERROR(ENOMEM); | |
36ef5369 | 460 | st->codec->codec_id = AV_CODEC_ID_TEXT; |
5b54a90c LB |
461 | } else if (flv->trust_metadata) { |
462 | if (!strcmp(key, "videocodecid") && vcodec) { | |
c91c63b5 | 463 | flv_set_video_codec(s, vstream, num_val, 0); |
e4529df9 | 464 | } else if (!strcmp(key, "audiocodecid") && acodec) { |
c3d01577 JR |
465 | int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET; |
466 | flv_set_audio_codec(s, astream, acodec, id); | |
e4529df9 | 467 | } else if (!strcmp(key, "audiosamplerate") && acodec) { |
5b54a90c | 468 | acodec->sample_rate = num_val; |
e46a2a73 JR |
469 | } else if (!strcmp(key, "audiosamplesize") && acodec) { |
470 | acodec->bits_per_coded_sample = num_val; | |
471 | } else if (!strcmp(key, "stereo") && acodec) { | |
e4529df9 | 472 | acodec->channels = num_val + 1; |
e46a2a73 JR |
473 | acodec->channel_layout = acodec->channels == 2 ? |
474 | AV_CH_LAYOUT_STEREO : | |
475 | AV_CH_LAYOUT_MONO; | |
e4529df9 | 476 | } else if (!strcmp(key, "width") && vcodec) { |
5b54a90c | 477 | vcodec->width = num_val; |
e4529df9 | 478 | } else if (!strcmp(key, "height") && vcodec) { |
5b54a90c LB |
479 | vcodec->height = num_val; |
480 | } | |
21e2dc9f | 481 | } |
b204c46d MS |
482 | } |
483 | ||
5e87222f MS |
484 | if (!strcmp(key, "duration") || |
485 | !strcmp(key, "filesize") || | |
486 | !strcmp(key, "width") || | |
487 | !strcmp(key, "height") || | |
488 | !strcmp(key, "videodatarate") || | |
489 | !strcmp(key, "framerate") || | |
490 | !strcmp(key, "videocodecid") || | |
491 | !strcmp(key, "audiodatarate") || | |
492 | !strcmp(key, "audiosamplerate") || | |
493 | !strcmp(key, "audiosamplesize") || | |
494 | !strcmp(key, "stereo") || | |
0a9425d7 LB |
495 | !strcmp(key, "audiocodecid") || |
496 | !strcmp(key, "datastream")) | |
5e87222f MS |
497 | return 0; |
498 | ||
e4529df9 DB |
499 | if (amf_type == AMF_DATA_TYPE_BOOL) { |
500 | av_strlcpy(str_val, num_val > 0 ? "true" : "false", | |
501 | sizeof(str_val)); | |
d2d67e42 | 502 | av_dict_set(&s->metadata, key, str_val, 0); |
e4529df9 | 503 | } else if (amf_type == AMF_DATA_TYPE_NUMBER) { |
cc38e063 | 504 | snprintf(str_val, sizeof(str_val), "%.f", num_val); |
d2d67e42 | 505 | av_dict_set(&s->metadata, key, str_val, 0); |
df2bd71a | 506 | } else if (amf_type == AMF_DATA_TYPE_STRING) |
d2d67e42 | 507 | av_dict_set(&s->metadata, key, str_val, 0); |
428cc588 AH |
508 | } |
509 | ||
510 | return 0; | |
511 | } | |
512 | ||
e4529df9 DB |
513 | static int flv_read_metabody(AVFormatContext *s, int64_t next_pos) |
514 | { | |
428cc588 AH |
515 | AMFDataType type; |
516 | AVStream *stream, *astream, *vstream; | |
ae628ec1 | 517 | AVIOContext *ioc; |
4eec2606 | 518 | int i; |
e4529df9 DB |
519 | // only needs to hold the string "onMetaData". |
520 | // Anything longer is something we don't want. | |
521 | char buffer[11]; | |
428cc588 AH |
522 | |
523 | astream = NULL; | |
524 | vstream = NULL; | |
e4529df9 | 525 | ioc = s->pb; |
428cc588 | 526 | |
e4529df9 | 527 | // first object needs to be "onMetaData" string |
b7effd4e | 528 | type = avio_r8(ioc); |
21e2dc9f LB |
529 | if (type != AMF_DATA_TYPE_STRING || |
530 | amf_get_string(ioc, buffer, sizeof(buffer)) < 0) | |
531 | return -1; | |
532 | ||
533 | if (!strcmp(buffer, "onTextData")) | |
534 | return 1; | |
535 | ||
536 | if (strcmp(buffer, "onMetaData")) | |
428cc588 AH |
537 | return -1; |
538 | ||
e4529df9 DB |
539 | // find the streams now so that amf_parse_object doesn't need to do |
540 | // the lookup every time it is called. | |
541 | for (i = 0; i < s->nb_streams; i++) { | |
428cc588 | 542 | stream = s->streams[i]; |
e4529df9 DB |
543 | if (stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) |
544 | astream = stream; | |
545 | else if (stream->codec->codec_type == AVMEDIA_TYPE_VIDEO) | |
546 | vstream = stream; | |
428cc588 AH |
547 | } |
548 | ||
e4529df9 DB |
549 | // parse the second object (we want a mixed array) |
550 | if (amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0) | |
428cc588 AH |
551 | return -1; |
552 | ||
553 | return 0; | |
554 | } | |
555 | ||
6e9651d1 | 556 | static int flv_read_header(AVFormatContext *s) |
d4f5d74a | 557 | { |
15f14fc7 | 558 | int offset, flags; |
d4f5d74a | 559 | |
45a8a02a | 560 | avio_skip(s->pb, 4); |
b7effd4e | 561 | flags = avio_r8(s->pb); |
d4f5d74a | 562 | |
ee0dadc1 | 563 | s->ctx_flags |= AVFMTCTX_NOHEADER; |
b41497e9 | 564 | |
e4529df9 DB |
565 | if (flags & FLV_HEADER_FLAG_HASVIDEO) |
566 | if (!create_stream(s, AVMEDIA_TYPE_VIDEO)) | |
769e10f0 | 567 | return AVERROR(ENOMEM); |
e4529df9 DB |
568 | if (flags & FLV_HEADER_FLAG_HASAUDIO) |
569 | if (!create_stream(s, AVMEDIA_TYPE_AUDIO)) | |
769e10f0 | 570 | return AVERROR(ENOMEM); |
4eb0c665 | 571 | |
b7effd4e | 572 | offset = avio_rb32(s->pb); |
6b4aa5da | 573 | avio_seek(s->pb, offset, SEEK_SET); |
45a8a02a | 574 | avio_skip(s->pb, 4); |
d4f5d74a | 575 | |
aeb20f7f N |
576 | s->start_time = 0; |
577 | ||
d4f5d74a GM |
578 | return 0; |
579 | } | |
580 | ||
251f320f MS |
581 | static int flv_read_close(AVFormatContext *s) |
582 | { | |
583 | FLVContext *flv = s->priv_data; | |
584 | av_freep(&flv->new_extradata[0]); | |
585 | av_freep(&flv->new_extradata[1]); | |
586 | return 0; | |
587 | } | |
588 | ||
04fd3e81 BC |
589 | static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size) |
590 | { | |
591 | av_free(st->codec->extradata); | |
592 | st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE); | |
593 | if (!st->codec->extradata) | |
594 | return AVERROR(ENOMEM); | |
595 | st->codec->extradata_size = size; | |
b7effd4e | 596 | avio_read(s->pb, st->codec->extradata, st->codec->extradata_size); |
04fd3e81 BC |
597 | return 0; |
598 | } | |
599 | ||
251f320f MS |
600 | static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream, |
601 | int size) | |
602 | { | |
603 | av_free(flv->new_extradata[stream]); | |
e4529df9 DB |
604 | flv->new_extradata[stream] = av_mallocz(size + |
605 | FF_INPUT_BUFFER_PADDING_SIZE); | |
251f320f MS |
606 | if (!flv->new_extradata[stream]) |
607 | return AVERROR(ENOMEM); | |
608 | flv->new_extradata_size[stream] = size; | |
609 | avio_read(pb, flv->new_extradata[stream], size); | |
610 | return 0; | |
611 | } | |
612 | ||
7e297a46 MS |
613 | static void clear_index_entries(AVFormatContext *s, int64_t pos) |
614 | { | |
615 | int i, j, out; | |
e4529df9 DB |
616 | av_log(s, AV_LOG_WARNING, |
617 | "Found invalid index entries, clearing the index.\n"); | |
7e297a46 MS |
618 | for (i = 0; i < s->nb_streams; i++) { |
619 | AVStream *st = s->streams[i]; | |
620 | /* Remove all index entries that point to >= pos */ | |
621 | out = 0; | |
e4529df9 | 622 | for (j = 0; j < st->nb_index_entries; j++) |
7e297a46 MS |
623 | if (st->index_entries[j].pos < pos) |
624 | st->index_entries[out++] = st->index_entries[j]; | |
7e297a46 MS |
625 | st->nb_index_entries = out; |
626 | } | |
627 | } | |
628 | ||
c951e4b4 LB |
629 | static int amf_skip_tag(AVIOContext *pb, AMFDataType type) |
630 | { | |
631 | int nb = -1, ret, parse_name = 1; | |
632 | ||
633 | switch (type) { | |
634 | case AMF_DATA_TYPE_NUMBER: | |
635 | avio_skip(pb, 8); | |
636 | break; | |
637 | case AMF_DATA_TYPE_BOOL: | |
638 | avio_skip(pb, 1); | |
639 | break; | |
640 | case AMF_DATA_TYPE_STRING: | |
641 | avio_skip(pb, avio_rb16(pb)); | |
642 | break; | |
643 | case AMF_DATA_TYPE_ARRAY: | |
644 | parse_name = 0; | |
645 | case AMF_DATA_TYPE_MIXEDARRAY: | |
646 | nb = avio_rb32(pb); | |
647 | case AMF_DATA_TYPE_OBJECT: | |
648 | while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) { | |
649 | if (parse_name) { | |
650 | int size = avio_rb16(pb); | |
651 | if (!size) { | |
652 | avio_skip(pb, 1); | |
653 | break; | |
654 | } | |
655 | avio_skip(pb, size); | |
656 | } | |
657 | if ((ret = amf_skip_tag(pb, avio_r8(pb))) < 0) | |
658 | return ret; | |
659 | } | |
660 | break; | |
661 | case AMF_DATA_TYPE_NULL: | |
662 | case AMF_DATA_TYPE_OBJECT_END: | |
663 | break; | |
664 | default: | |
665 | return AVERROR_INVALIDDATA; | |
666 | } | |
667 | return 0; | |
668 | } | |
669 | ||
21e2dc9f LB |
670 | static int flv_data_packet(AVFormatContext *s, AVPacket *pkt, |
671 | int64_t dts, int64_t next) | |
672 | { | |
21e2dc9f | 673 | AVIOContext *pb = s->pb; |
e4529df9 | 674 | AVStream *st = NULL; |
21e2dc9f | 675 | char buf[20]; |
c951e4b4 LB |
676 | int ret = AVERROR_INVALIDDATA; |
677 | int i, length = -1; | |
21e2dc9f | 678 | |
c951e4b4 LB |
679 | switch (avio_r8(pb)) { |
680 | case AMF_DATA_TYPE_MIXEDARRAY: | |
21e2dc9f | 681 | avio_seek(pb, 4, SEEK_CUR); |
c951e4b4 LB |
682 | case AMF_DATA_TYPE_OBJECT: |
683 | break; | |
684 | default: | |
685 | goto skip; | |
686 | } | |
21e2dc9f | 687 | |
c951e4b4 LB |
688 | while ((ret = amf_get_string(pb, buf, sizeof(buf))) > 0) { |
689 | AMFDataType type = avio_r8(pb); | |
690 | if (type == AMF_DATA_TYPE_STRING && !strcmp(buf, "text")) { | |
691 | length = avio_rb16(pb); | |
692 | ret = av_get_packet(pb, pkt, length); | |
693 | if (ret < 0) | |
694 | goto skip; | |
695 | else | |
696 | break; | |
697 | } else { | |
698 | if ((ret = amf_skip_tag(pb, type)) < 0) | |
699 | goto skip; | |
700 | } | |
701 | } | |
21e2dc9f | 702 | |
c951e4b4 LB |
703 | if (length < 0) { |
704 | ret = AVERROR_INVALIDDATA; | |
705 | goto skip; | |
706 | } | |
21e2dc9f LB |
707 | |
708 | for (i = 0; i < s->nb_streams; i++) { | |
709 | st = s->streams[i]; | |
09a445ce | 710 | if (st->codec->codec_type == AVMEDIA_TYPE_DATA) |
21e2dc9f LB |
711 | break; |
712 | } | |
713 | ||
714 | if (i == s->nb_streams) { | |
41f43202 | 715 | st = create_stream(s, AVMEDIA_TYPE_DATA); |
21e2dc9f | 716 | if (!st) |
f900f35a | 717 | return AVERROR_INVALIDDATA; |
36ef5369 | 718 | st->codec->codec_id = AV_CODEC_ID_TEXT; |
21e2dc9f LB |
719 | } |
720 | ||
721 | pkt->dts = dts; | |
722 | pkt->pts = dts; | |
723 | pkt->size = ret; | |
724 | ||
725 | pkt->stream_index = st->index; | |
e4529df9 | 726 | pkt->flags |= AV_PKT_FLAG_KEY; |
21e2dc9f | 727 | |
c951e4b4 | 728 | skip: |
21e2dc9f | 729 | avio_seek(s->pb, next + 4, SEEK_SET); |
e4529df9 | 730 | |
21e2dc9f LB |
731 | return ret; |
732 | } | |
733 | ||
d4f5d74a GM |
734 | static int flv_read_packet(AVFormatContext *s, AVPacket *pkt) |
735 | { | |
ebd61055 | 736 | FLVContext *flv = s->priv_data; |
4fe8a452 PM |
737 | int ret, i, type, size, flags, is_audio; |
738 | int64_t next, pos; | |
ebd61055 | 739 | int64_t dts, pts = AV_NOPTS_VALUE; |
2df73eef | 740 | int sample_rate = 0, channels = 0; |
e4529df9 DB |
741 | AVStream *st = NULL; |
742 | ||
743 | /* pkt size is repeated at end. skip it */ | |
744 | for (;; avio_skip(s->pb, 4)) { | |
745 | pos = avio_tell(s->pb); | |
746 | type = avio_r8(s->pb); | |
747 | size = avio_rb24(s->pb); | |
748 | dts = avio_rb24(s->pb); | |
749 | dts |= avio_r8(s->pb) << 24; | |
750 | av_dlog(s, "type:%d, size:%d, dts:%"PRId64"\n", type, size, dts); | |
751 | if (s->pb->eof_reached) | |
752 | return AVERROR_EOF; | |
753 | avio_skip(s->pb, 3); /* stream id, always 0 */ | |
754 | flags = 0; | |
755 | ||
756 | if (flv->validate_next < flv->validate_count) { | |
757 | int64_t validate_pos = flv->validate_index[flv->validate_next].pos; | |
758 | if (pos == validate_pos) { | |
759 | if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <= | |
760 | VALIDATE_INDEX_TS_THRESH) { | |
761 | flv->validate_next++; | |
762 | } else { | |
763 | clear_index_entries(s, validate_pos); | |
764 | flv->validate_count = 0; | |
765 | } | |
766 | } else if (pos > validate_pos) { | |
7e297a46 MS |
767 | clear_index_entries(s, validate_pos); |
768 | flv->validate_count = 0; | |
769 | } | |
7e297a46 | 770 | } |
115329f1 | 771 | |
e4529df9 DB |
772 | if (size == 0) |
773 | continue; | |
774 | ||
775 | next = size + avio_tell(s->pb); | |
776 | ||
777 | if (type == FLV_TAG_TYPE_AUDIO) { | |
778 | is_audio = 1; | |
779 | flags = avio_r8(s->pb); | |
780 | size--; | |
781 | } else if (type == FLV_TAG_TYPE_VIDEO) { | |
782 | is_audio = 0; | |
783 | flags = avio_r8(s->pb); | |
784 | size--; | |
785 | if ((flags & 0xf0) == 0x50) /* video info / command frame */ | |
786 | goto skip; | |
787 | } else { | |
788 | if (type == FLV_TAG_TYPE_META && size > 13 + 1 + 4) | |
789 | if (flv_read_metabody(s, next) > 0) { | |
790 | return flv_data_packet(s, pkt, dts, next); | |
791 | } else /* skip packet */ | |
792 | av_log(s, AV_LOG_DEBUG, | |
374fdc8c | 793 | "Skipping flv packet: type %d, size %d, flags %d.\n", |
e4529df9 DB |
794 | type, size, flags); |
795 | ||
796 | skip: | |
797 | avio_seek(s->pb, next, SEEK_SET); | |
798 | continue; | |
799 | } | |
ae58b54b | 800 | |
e4529df9 DB |
801 | /* skip empty data packets */ |
802 | if (!size) | |
803 | continue; | |
804 | ||
805 | /* now find stream */ | |
806 | for (i = 0; i < s->nb_streams; i++) { | |
807 | st = s->streams[i]; | |
808 | if (is_audio && st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { | |
809 | if (flv_same_audio_codec(st->codec, flags)) | |
810 | break; | |
811 | } else if (!is_audio && | |
812 | st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { | |
813 | if (flv_same_video_codec(st->codec, flags)) | |
814 | break; | |
09a445ce LB |
815 | } |
816 | } | |
e4529df9 DB |
817 | if (i == s->nb_streams) |
818 | st = create_stream(s, is_audio ? AVMEDIA_TYPE_AUDIO | |
819 | : AVMEDIA_TYPE_VIDEO); | |
820 | av_dlog(s, "%d %X %d \n", is_audio, flags, st->discard); | |
fa14804c LB |
821 | |
822 | if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || | |
823 | is_audio) | |
824 | av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME); | |
825 | ||
e4529df9 DB |
826 | if ((st->discard >= AVDISCARD_NONKEY && |
827 | !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio)) || | |
828 | (st->discard >= AVDISCARD_BIDIR && | |
829 | ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio)) || | |
830 | st->discard >= AVDISCARD_ALL) { | |
831 | avio_seek(s->pb, next, SEEK_SET); | |
832 | continue; | |
833 | } | |
e4529df9 | 834 | break; |
d4f5d74a | 835 | } |
068f2a22 | 836 | |
e4529df9 DB |
837 | // if not streamed and no duration from metadata then seek to end to find |
838 | // the duration from the timestamps | |
839 | if (s->pb->seekable && (!s->duration || s->duration == AV_NOPTS_VALUE)) { | |
15f14fc7 | 840 | int size; |
e4529df9 DB |
841 | const int64_t pos = avio_tell(s->pb); |
842 | const int64_t fsize = avio_size(s->pb); | |
843 | avio_seek(s->pb, fsize - 4, SEEK_SET); | |
844 | size = avio_rb32(s->pb); | |
845 | avio_seek(s->pb, fsize - 3 - size, SEEK_SET); | |
846 | if (size == avio_rb24(s->pb) + 11) { | |
b7effd4e | 847 | uint32_t ts = avio_rb24(s->pb); |
e4529df9 | 848 | ts |= avio_r8(s->pb) << 24; |
b126dee9 | 849 | s->duration = ts * (int64_t)AV_TIME_BASE / 1000; |
15f14fc7 | 850 | } |
6b4aa5da | 851 | avio_seek(s->pb, pos, SEEK_SET); |
15f14fc7 MN |
852 | } |
853 | ||
e4529df9 | 854 | if (is_audio) { |
2215c39e | 855 | int bits_per_coded_sample; |
e4529df9 DB |
856 | channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1; |
857 | sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> | |
858 | FLV_AUDIO_SAMPLERATE_OFFSET) >> 3; | |
2215c39e | 859 | bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8; |
e4529df9 DB |
860 | if (!st->codec->channels || !st->codec->sample_rate || |
861 | !st->codec->bits_per_coded_sample) { | |
2215c39e | 862 | st->codec->channels = channels; |
e4529df9 DB |
863 | st->codec->channel_layout = channels == 1 |
864 | ? AV_CH_LAYOUT_MONO | |
865 | : AV_CH_LAYOUT_STEREO; | |
2215c39e MS |
866 | st->codec->sample_rate = sample_rate; |
867 | st->codec->bits_per_coded_sample = bits_per_coded_sample; | |
2f3d7ea9 | 868 | } |
e4529df9 DB |
869 | if (!st->codec->codec_id) { |
870 | flv_set_audio_codec(s, st, st->codec, | |
871 | flags & FLV_AUDIO_CODECID_MASK); | |
872 | flv->last_sample_rate = | |
873 | sample_rate = st->codec->sample_rate; | |
874 | flv->last_channels = | |
875 | channels = st->codec->channels; | |
2215c39e MS |
876 | } else { |
877 | AVCodecContext ctx; | |
878 | ctx.sample_rate = sample_rate; | |
879 | flv_set_audio_codec(s, st, &ctx, flags & FLV_AUDIO_CODECID_MASK); | |
880 | sample_rate = ctx.sample_rate; | |
068f2a22 | 881 | } |
e4529df9 | 882 | } else { |
c91c63b5 | 883 | size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1); |
bb01a3f0 MN |
884 | } |
885 | ||
36ef5369 AK |
886 | if (st->codec->codec_id == AV_CODEC_ID_AAC || |
887 | st->codec->codec_id == AV_CODEC_ID_H264) { | |
b7effd4e | 888 | int type = avio_r8(s->pb); |
04fd3e81 | 889 | size--; |
36ef5369 | 890 | if (st->codec->codec_id == AV_CODEC_ID_H264) { |
e4529df9 DB |
891 | // sign extension |
892 | int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000; | |
ebd61055 | 893 | pts = dts + cts; |
5d983fdb | 894 | if (cts < 0 && !flv->wrong_dts) { // dts might be wrong |
ebd61055 | 895 | flv->wrong_dts = 1; |
e4529df9 | 896 | av_log(s, AV_LOG_WARNING, |
374fdc8c | 897 | "Negative cts, previous timestamps might be wrong.\n"); |
ebd61055 | 898 | } |
04fd3e81 BC |
899 | } |
900 | if (type == 0) { | |
251f320f MS |
901 | if (st->codec->extradata) { |
902 | if ((ret = flv_queue_extradata(flv, s->pb, is_audio, size)) < 0) | |
903 | return ret; | |
904 | ret = AVERROR(EAGAIN); | |
905 | goto leave; | |
906 | } | |
6298eb81 | 907 | if ((ret = flv_get_extradata(s, st, size)) < 0) |
04fd3e81 | 908 | return ret; |
36ef5369 | 909 | if (st->codec->codec_id == AV_CODEC_ID_AAC) { |
d2718187 | 910 | MPEG4AudioConfig cfg; |
547f8345 BL |
911 | |
912 | /* Workaround for buggy Omnia A/XE encoder */ | |
913 | AVDictionaryEntry *t = av_dict_get(s->metadata, "Encoder", NULL, 0); | |
914 | if (t && !strcmp(t->value, "Omnia A/XE")) | |
915 | st->codec->extradata_size = 2; | |
916 | ||
59a9a235 | 917 | avpriv_mpeg4audio_get_config(&cfg, st->codec->extradata, |
fd095539 | 918 | st->codec->extradata_size * 8, 1); |
e4529df9 | 919 | st->codec->channels = cfg.channels; |
644d8d2e | 920 | st->codec->channel_layout = 0; |
7d6096e4 BC |
921 | if (cfg.ext_sample_rate) |
922 | st->codec->sample_rate = cfg.ext_sample_rate; | |
923 | else | |
924 | st->codec->sample_rate = cfg.sample_rate; | |
dfd2a005 | 925 | av_dlog(s, "mp4a config channels %d sample rate %d\n", |
d2718187 BC |
926 | st->codec->channels, st->codec->sample_rate); |
927 | } | |
928 | ||
527c2e64 HC |
929 | ret = AVERROR(EAGAIN); |
930 | goto leave; | |
04fd3e81 BC |
931 | } |
932 | } | |
933 | ||
fcb4228c | 934 | /* skip empty data packets */ |
527c2e64 HC |
935 | if (!size) { |
936 | ret = AVERROR(EAGAIN); | |
937 | goto leave; | |
938 | } | |
fcb4228c | 939 | |
e4529df9 DB |
940 | ret = av_get_packet(s->pb, pkt, size); |
941 | if (ret < 0) | |
6f3e0b21 | 942 | return AVERROR(EIO); |
d4f5d74a | 943 | /* note: we need to modify the packet size here to handle the last |
e4529df9 DB |
944 | * packet */ |
945 | pkt->size = ret; | |
946 | pkt->dts = dts; | |
947 | pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts; | |
d4f5d74a | 948 | pkt->stream_index = st->index; |
251f320f MS |
949 | if (flv->new_extradata[is_audio]) { |
950 | uint8_t *side = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, | |
951 | flv->new_extradata_size[is_audio]); | |
952 | if (side) { | |
953 | memcpy(side, flv->new_extradata[is_audio], | |
954 | flv->new_extradata_size[is_audio]); | |
955 | av_freep(&flv->new_extradata[is_audio]); | |
956 | flv->new_extradata_size[is_audio] = 0; | |
957 | } | |
958 | } | |
2215c39e | 959 | if (is_audio && (sample_rate != flv->last_sample_rate || |
e4529df9 | 960 | channels != flv->last_channels)) { |
2215c39e MS |
961 | flv->last_sample_rate = sample_rate; |
962 | flv->last_channels = channels; | |
963 | ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0); | |
964 | } | |
115329f1 | 965 | |
6cac3a3b | 966 | if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)) |
cc947f04 | 967 | pkt->flags |= AV_PKT_FLAG_KEY; |
115329f1 | 968 | |
527c2e64 | 969 | leave: |
45a8a02a | 970 | avio_skip(s->pb, 4); |
d4f5d74a GM |
971 | return ret; |
972 | } | |
973 | ||
fc8fa007 | 974 | static int flv_read_seek(AVFormatContext *s, int stream_index, |
e4529df9 | 975 | int64_t ts, int flags) |
fc8fa007 | 976 | { |
7e297a46 MS |
977 | FLVContext *flv = s->priv_data; |
978 | flv->validate_count = 0; | |
ff1ec0c3 | 979 | return avio_seek_time(s->pb, stream_index, ts, flags); |
fc8fa007 HC |
980 | } |
981 | ||
5b54a90c LB |
982 | #define OFFSET(x) offsetof(FLVContext, x) |
983 | #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM | |
984 | static const AVOption options[] = { | |
f4634ae8 | 985 | { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD }, |
5b54a90c LB |
986 | { NULL } |
987 | }; | |
988 | ||
989 | static const AVClass class = { | |
990 | .class_name = "flvdec", | |
991 | .item_name = av_default_item_name, | |
992 | .option = options, | |
993 | .version = LIBAVUTIL_VERSION_INT, | |
994 | }; | |
995 | ||
c6610a21 | 996 | AVInputFormat ff_flv_demuxer = { |
dfc2c4d9 | 997 | .name = "flv", |
0177b7d2 | 998 | .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"), |
dfc2c4d9 AK |
999 | .priv_data_size = sizeof(FLVContext), |
1000 | .read_probe = flv_probe, | |
1001 | .read_header = flv_read_header, | |
1002 | .read_packet = flv_read_packet, | |
20234a4b | 1003 | .read_seek = flv_read_seek, |
20234a4b MS |
1004 | .read_close = flv_read_close, |
1005 | .extensions = "flv", | |
5b54a90c | 1006 | .priv_class = &class, |
d4f5d74a | 1007 | }; |