avcodec/x86/pngdsp: add missing emms at the end of add_png_paeth_prediction
[ffmpeg.git] / libavformat / flvdec.c
1 /*
2 * FLV demuxer
3 * Copyright (c) 2003 The FFmpeg Project
4 *
5 * This demuxer will generate a 1 byte extradata for VP6F content.
6 * It is composed of:
7 * - upper 4 bits: difference between encoded width and visible width
8 * - lower 4 bits: difference between encoded height and visible height
9 *
10 * This file is part of FFmpeg.
11 *
12 * FFmpeg is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * FFmpeg is distributed in the hope that it will be useful,
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
23 * License along with FFmpeg; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 #include "libavutil/avstring.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/intfloat.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/time_internal.h"
34 #include "libavcodec/bytestream.h"
35 #include "avformat.h"
36 #include "internal.h"
37 #include "avio_internal.h"
38 #include "flv.h"
39
40 #define VALIDATE_INDEX_TS_THRESH 2500
41
42 #define RESYNC_BUFFER_SIZE (1<<20)
43
44 #define MAX_DEPTH 16 ///< arbitrary limit to prevent unbounded recursion
45
46 typedef struct FLVContext {
47 const AVClass *class; ///< Class for private options.
48 int trust_metadata; ///< configure streams according onMetaData
49 int trust_datasize; ///< trust data size of FLVTag
50 int dump_full_metadata; ///< Dump full metadata of the onMetadata
51 int wrong_dts; ///< wrong dts due to negative cts
52 uint8_t *new_extradata[FLV_STREAM_TYPE_NB];
53 int new_extradata_size[FLV_STREAM_TYPE_NB];
54 int last_sample_rate;
55 int last_channels;
56 struct {
57 int64_t dts;
58 int64_t pos;
59 } validate_index[2];
60 int validate_next;
61 int validate_count;
62 int searched_for_end;
63
64 uint8_t resync_buffer[2*RESYNC_BUFFER_SIZE];
65
66 int broken_sizes;
67 int64_t sum_flv_tag_size;
68
69 int last_keyframe_stream_index;
70 int keyframe_count;
71 int64_t video_bit_rate;
72 int64_t audio_bit_rate;
73 int64_t *keyframe_times;
74 int64_t *keyframe_filepositions;
75 int missing_streams;
76 AVRational framerate;
77 int64_t last_ts;
78 int64_t time_offset;
79 int64_t time_pos;
80 } FLVContext;
81
82 /* AMF date type */
83 typedef struct amf_date {
84 double milliseconds;
85 int16_t timezone;
86 } amf_date;
87
88 static int probe(const AVProbeData *p, int live)
89 {
90 const uint8_t *d = p->buf;
91 unsigned offset = AV_RB32(d + 5);
92
93 if (d[0] == 'F' &&
94 d[1] == 'L' &&
95 d[2] == 'V' &&
96 d[3] < 5 && d[5] == 0 &&
97 offset + 100 < p->buf_size &&
98 offset > 8) {
99 int is_live = !memcmp(d + offset + 40, "NGINX RTMP", 10);
100
101 if (live == is_live)
102 return AVPROBE_SCORE_MAX;
103 }
104 return 0;
105 }
106
107 static int flv_probe(const AVProbeData *p)
108 {
109 return probe(p, 0);
110 }
111
112 static int live_flv_probe(const AVProbeData *p)
113 {
114 return probe(p, 1);
115 }
116
117 static int kux_probe(const AVProbeData *p)
118 {
119 const uint8_t *d = p->buf;
120
121 if (d[0] == 'K' &&
122 d[1] == 'D' &&
123 d[2] == 'K' &&
124 d[3] == 0 &&
125 d[4] == 0) {
126 return AVPROBE_SCORE_EXTENSION + 1;
127 }
128 return 0;
129 }
130
131 static void add_keyframes_index(AVFormatContext *s)
132 {
133 FLVContext *flv = s->priv_data;
134 AVStream *stream = NULL;
135 unsigned int i = 0;
136
137 if (flv->last_keyframe_stream_index < 0) {
138 av_log(s, AV_LOG_DEBUG, "keyframe stream hasn't been created\n");
139 return;
140 }
141
142 av_assert0(flv->last_keyframe_stream_index <= s->nb_streams);
143 stream = s->streams[flv->last_keyframe_stream_index];
144
145 if (stream->nb_index_entries == 0) {
146 for (i = 0; i < flv->keyframe_count; i++) {
147 av_log(s, AV_LOG_TRACE, "keyframe filepositions = %"PRId64" times = %"PRId64"\n",
148 flv->keyframe_filepositions[i], flv->keyframe_times[i] * 1000);
149 av_add_index_entry(stream, flv->keyframe_filepositions[i],
150 flv->keyframe_times[i] * 1000, 0, 0, AVINDEX_KEYFRAME);
151 }
152 } else
153 av_log(s, AV_LOG_WARNING, "Skipping duplicate index\n");
154
155 if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
156 av_freep(&flv->keyframe_times);
157 av_freep(&flv->keyframe_filepositions);
158 flv->keyframe_count = 0;
159 }
160 }
161
162 static AVStream *create_stream(AVFormatContext *s, int codec_type)
163 {
164 FLVContext *flv = s->priv_data;
165 AVStream *st = avformat_new_stream(s, NULL);
166 if (!st)
167 return NULL;
168 st->codecpar->codec_type = codec_type;
169 if (s->nb_streams>=3 ||( s->nb_streams==2
170 && s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE
171 && s->streams[1]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE
172 && s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_DATA
173 && s->streams[1]->codecpar->codec_type != AVMEDIA_TYPE_DATA))
174 s->ctx_flags &= ~AVFMTCTX_NOHEADER;
175 if (codec_type == AVMEDIA_TYPE_AUDIO) {
176 st->codecpar->bit_rate = flv->audio_bit_rate;
177 flv->missing_streams &= ~FLV_HEADER_FLAG_HASAUDIO;
178 }
179 if (codec_type == AVMEDIA_TYPE_VIDEO) {
180 st->codecpar->bit_rate = flv->video_bit_rate;
181 flv->missing_streams &= ~FLV_HEADER_FLAG_HASVIDEO;
182 st->avg_frame_rate = flv->framerate;
183 }
184
185
186 avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
187 flv->last_keyframe_stream_index = s->nb_streams - 1;
188 add_keyframes_index(s);
189 return st;
190 }
191
192 static int flv_same_audio_codec(AVCodecParameters *apar, int flags)
193 {
194 int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
195 int flv_codecid = flags & FLV_AUDIO_CODECID_MASK;
196 int codec_id;
197
198 if (!apar->codec_id && !apar->codec_tag)
199 return 1;
200
201 if (apar->bits_per_coded_sample != bits_per_coded_sample)
202 return 0;
203
204 switch (flv_codecid) {
205 // no distinction between S16 and S8 PCM codec flags
206 case FLV_CODECID_PCM:
207 codec_id = bits_per_coded_sample == 8
208 ? AV_CODEC_ID_PCM_U8
209 #if HAVE_BIGENDIAN
210 : AV_CODEC_ID_PCM_S16BE;
211 #else
212 : AV_CODEC_ID_PCM_S16LE;
213 #endif
214 return codec_id == apar->codec_id;
215 case FLV_CODECID_PCM_LE:
216 codec_id = bits_per_coded_sample == 8
217 ? AV_CODEC_ID_PCM_U8
218 : AV_CODEC_ID_PCM_S16LE;
219 return codec_id == apar->codec_id;
220 case FLV_CODECID_AAC:
221 return apar->codec_id == AV_CODEC_ID_AAC;
222 case FLV_CODECID_ADPCM:
223 return apar->codec_id == AV_CODEC_ID_ADPCM_SWF;
224 case FLV_CODECID_SPEEX:
225 return apar->codec_id == AV_CODEC_ID_SPEEX;
226 case FLV_CODECID_MP3:
227 return apar->codec_id == AV_CODEC_ID_MP3;
228 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
229 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
230 case FLV_CODECID_NELLYMOSER:
231 return apar->codec_id == AV_CODEC_ID_NELLYMOSER;
232 case FLV_CODECID_PCM_MULAW:
233 return apar->sample_rate == 8000 &&
234 apar->codec_id == AV_CODEC_ID_PCM_MULAW;
235 case FLV_CODECID_PCM_ALAW:
236 return apar->sample_rate == 8000 &&
237 apar->codec_id == AV_CODEC_ID_PCM_ALAW;
238 default:
239 return apar->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
240 }
241 }
242
243 static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream,
244 AVCodecParameters *apar, int flv_codecid)
245 {
246 switch (flv_codecid) {
247 // no distinction between S16 and S8 PCM codec flags
248 case FLV_CODECID_PCM:
249 apar->codec_id = apar->bits_per_coded_sample == 8
250 ? AV_CODEC_ID_PCM_U8
251 #if HAVE_BIGENDIAN
252 : AV_CODEC_ID_PCM_S16BE;
253 #else
254 : AV_CODEC_ID_PCM_S16LE;
255 #endif
256 break;
257 case FLV_CODECID_PCM_LE:
258 apar->codec_id = apar->bits_per_coded_sample == 8
259 ? AV_CODEC_ID_PCM_U8
260 : AV_CODEC_ID_PCM_S16LE;
261 break;
262 case FLV_CODECID_AAC:
263 apar->codec_id = AV_CODEC_ID_AAC;
264 break;
265 case FLV_CODECID_ADPCM:
266 apar->codec_id = AV_CODEC_ID_ADPCM_SWF;
267 break;
268 case FLV_CODECID_SPEEX:
269 apar->codec_id = AV_CODEC_ID_SPEEX;
270 apar->sample_rate = 16000;
271 break;
272 case FLV_CODECID_MP3:
273 apar->codec_id = AV_CODEC_ID_MP3;
274 astream->need_parsing = AVSTREAM_PARSE_FULL;
275 break;
276 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
277 // in case metadata does not otherwise declare samplerate
278 apar->sample_rate = 8000;
279 apar->codec_id = AV_CODEC_ID_NELLYMOSER;
280 break;
281 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
282 apar->sample_rate = 16000;
283 apar->codec_id = AV_CODEC_ID_NELLYMOSER;
284 break;
285 case FLV_CODECID_NELLYMOSER:
286 apar->codec_id = AV_CODEC_ID_NELLYMOSER;
287 break;
288 case FLV_CODECID_PCM_MULAW:
289 apar->sample_rate = 8000;
290 apar->codec_id = AV_CODEC_ID_PCM_MULAW;
291 break;
292 case FLV_CODECID_PCM_ALAW:
293 apar->sample_rate = 8000;
294 apar->codec_id = AV_CODEC_ID_PCM_ALAW;
295 break;
296 default:
297 avpriv_request_sample(s, "Audio codec (%x)",
298 flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
299 apar->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
300 }
301 }
302
303 static int flv_same_video_codec(AVCodecParameters *vpar, int flags)
304 {
305 int flv_codecid = flags & FLV_VIDEO_CODECID_MASK;
306
307 if (!vpar->codec_id && !vpar->codec_tag)
308 return 1;
309
310 switch (flv_codecid) {
311 case FLV_CODECID_H263:
312 return vpar->codec_id == AV_CODEC_ID_FLV1;
313 case FLV_CODECID_SCREEN:
314 return vpar->codec_id == AV_CODEC_ID_FLASHSV;
315 case FLV_CODECID_SCREEN2:
316 return vpar->codec_id == AV_CODEC_ID_FLASHSV2;
317 case FLV_CODECID_VP6:
318 return vpar->codec_id == AV_CODEC_ID_VP6F;
319 case FLV_CODECID_VP6A:
320 return vpar->codec_id == AV_CODEC_ID_VP6A;
321 case FLV_CODECID_H264:
322 return vpar->codec_id == AV_CODEC_ID_H264;
323 default:
324 return vpar->codec_tag == flv_codecid;
325 }
326 }
327
328 static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream,
329 int flv_codecid, int read)
330 {
331 int ret = 0;
332 AVCodecParameters *par = vstream->codecpar;
333 enum AVCodecID old_codec_id = vstream->codecpar->codec_id;
334 switch (flv_codecid) {
335 case FLV_CODECID_H263:
336 par->codec_id = AV_CODEC_ID_FLV1;
337 break;
338 case FLV_CODECID_REALH263:
339 par->codec_id = AV_CODEC_ID_H263;
340 break; // Really mean it this time
341 case FLV_CODECID_SCREEN:
342 par->codec_id = AV_CODEC_ID_FLASHSV;
343 break;
344 case FLV_CODECID_SCREEN2:
345 par->codec_id = AV_CODEC_ID_FLASHSV2;
346 break;
347 case FLV_CODECID_VP6:
348 par->codec_id = AV_CODEC_ID_VP6F;
349 case FLV_CODECID_VP6A:
350 if (flv_codecid == FLV_CODECID_VP6A)
351 par->codec_id = AV_CODEC_ID_VP6A;
352 if (read) {
353 if (par->extradata_size != 1) {
354 ff_alloc_extradata(par, 1);
355 }
356 if (par->extradata)
357 par->extradata[0] = avio_r8(s->pb);
358 else
359 avio_skip(s->pb, 1);
360 }
361 ret = 1; // 1 byte body size adjustment for flv_read_packet()
362 break;
363 case FLV_CODECID_H264:
364 par->codec_id = AV_CODEC_ID_H264;
365 vstream->need_parsing = AVSTREAM_PARSE_HEADERS;
366 ret = 3; // not 4, reading packet type will consume one byte
367 break;
368 case FLV_CODECID_MPEG4:
369 par->codec_id = AV_CODEC_ID_MPEG4;
370 ret = 3;
371 break;
372 default:
373 avpriv_request_sample(s, "Video codec (%x)", flv_codecid);
374 par->codec_tag = flv_codecid;
375 }
376
377 if (!vstream->internal->need_context_update && par->codec_id != old_codec_id) {
378 avpriv_request_sample(s, "Changing the codec id midstream");
379 return AVERROR_PATCHWELCOME;
380 }
381
382 return ret;
383 }
384
385 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
386 {
387 int ret;
388 int length = avio_rb16(ioc);
389 if (length >= buffsize) {
390 avio_skip(ioc, length);
391 return -1;
392 }
393
394 ret = avio_read(ioc, buffer, length);
395 if (ret < 0)
396 return ret;
397 if (ret < length)
398 return AVERROR_INVALIDDATA;
399
400 buffer[length] = '\0';
401
402 return length;
403 }
404
405 static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t max_pos)
406 {
407 FLVContext *flv = s->priv_data;
408 unsigned int timeslen = 0, fileposlen = 0, i;
409 char str_val[256];
410 int64_t *times = NULL;
411 int64_t *filepositions = NULL;
412 int ret = AVERROR(ENOSYS);
413 int64_t initial_pos = avio_tell(ioc);
414
415 if (flv->keyframe_count > 0) {
416 av_log(s, AV_LOG_DEBUG, "keyframes have been parsed\n");
417 return 0;
418 }
419 av_assert0(!flv->keyframe_times);
420 av_assert0(!flv->keyframe_filepositions);
421
422 if (s->flags & AVFMT_FLAG_IGNIDX)
423 return 0;
424
425 while (avio_tell(ioc) < max_pos - 2 &&
426 amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
427 int64_t **current_array;
428 unsigned int arraylen;
429
430 // Expect array object in context
431 if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
432 break;
433
434 arraylen = avio_rb32(ioc);
435 if (arraylen>>28)
436 break;
437
438 if (!strcmp(KEYFRAMES_TIMESTAMP_TAG , str_val) && !times) {
439 current_array = &times;
440 timeslen = arraylen;
441 } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) &&
442 !filepositions) {
443 current_array = &filepositions;
444 fileposlen = arraylen;
445 } else
446 // unexpected metatag inside keyframes, will not use such
447 // metadata for indexing
448 break;
449
450 if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
451 ret = AVERROR(ENOMEM);
452 goto finish;
453 }
454
455 for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {
456 double d;
457 if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
458 goto invalid;
459 d = av_int2double(avio_rb64(ioc));
460 if (isnan(d) || d < INT64_MIN || d > INT64_MAX)
461 goto invalid;
462 if (current_array == &times && (d <= INT64_MIN / 1000 || d >= INT64_MAX / 1000))
463 goto invalid;
464 if (avio_feof(ioc))
465 goto invalid;
466 current_array[0][i] = d;
467 }
468 if (times && filepositions) {
469 // All done, exiting at a position allowing amf_parse_object
470 // to finish parsing the object
471 ret = 0;
472 break;
473 }
474 }
475
476 if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
477 for (i = 0; i < FFMIN(2,fileposlen); i++) {
478 flv->validate_index[i].pos = filepositions[i];
479 flv->validate_index[i].dts = times[i] * 1000;
480 flv->validate_count = i + 1;
481 }
482 flv->keyframe_times = times;
483 flv->keyframe_filepositions = filepositions;
484 flv->keyframe_count = timeslen;
485 times = NULL;
486 filepositions = NULL;
487 } else {
488 invalid:
489 av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
490 }
491
492 finish:
493 av_freep(&times);
494 av_freep(&filepositions);
495 avio_seek(ioc, initial_pos, SEEK_SET);
496 return ret;
497 }
498
499 static int amf_parse_object(AVFormatContext *s, AVStream *astream,
500 AVStream *vstream, const char *key,
501 int64_t max_pos, int depth)
502 {
503 AVCodecParameters *apar, *vpar;
504 FLVContext *flv = s->priv_data;
505 AVIOContext *ioc;
506 AMFDataType amf_type;
507 char str_val[1024];
508 double num_val;
509 amf_date date;
510
511 if (depth > MAX_DEPTH)
512 return AVERROR_PATCHWELCOME;
513
514 num_val = 0;
515 ioc = s->pb;
516 if (avio_feof(ioc))
517 return AVERROR_EOF;
518 amf_type = avio_r8(ioc);
519
520 switch (amf_type) {
521 case AMF_DATA_TYPE_NUMBER:
522 num_val = av_int2double(avio_rb64(ioc));
523 break;
524 case AMF_DATA_TYPE_BOOL:
525 num_val = avio_r8(ioc);
526 break;
527 case AMF_DATA_TYPE_STRING:
528 if (amf_get_string(ioc, str_val, sizeof(str_val)) < 0) {
529 av_log(s, AV_LOG_ERROR, "AMF_DATA_TYPE_STRING parsing failed\n");
530 return -1;
531 }
532 break;
533 case AMF_DATA_TYPE_OBJECT:
534 if (key &&
535 (ioc->seekable & AVIO_SEEKABLE_NORMAL) &&
536 !strcmp(KEYFRAMES_TAG, key) && depth == 1)
537 if (parse_keyframes_index(s, ioc,
538 max_pos) < 0)
539 av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
540 else
541 add_keyframes_index(s);
542 while (avio_tell(ioc) < max_pos - 2 &&
543 amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
544 if (amf_parse_object(s, astream, vstream, str_val, max_pos,
545 depth + 1) < 0)
546 return -1; // if we couldn't skip, bomb out.
547 if (avio_r8(ioc) != AMF_END_OF_OBJECT) {
548 av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_OBJECT\n");
549 return -1;
550 }
551 break;
552 case AMF_DATA_TYPE_NULL:
553 case AMF_DATA_TYPE_UNDEFINED:
554 case AMF_DATA_TYPE_UNSUPPORTED:
555 break; // these take up no additional space
556 case AMF_DATA_TYPE_MIXEDARRAY:
557 {
558 unsigned v;
559 avio_skip(ioc, 4); // skip 32-bit max array index
560 while (avio_tell(ioc) < max_pos - 2 &&
561 amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
562 // this is the only case in which we would want a nested
563 // parse to not skip over the object
564 if (amf_parse_object(s, astream, vstream, str_val, max_pos,
565 depth + 1) < 0)
566 return -1;
567 v = avio_r8(ioc);
568 if (v != AMF_END_OF_OBJECT) {
569 av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_MIXEDARRAY, found %d\n", v);
570 return -1;
571 }
572 break;
573 }
574 case AMF_DATA_TYPE_ARRAY:
575 {
576 unsigned int arraylen, i;
577
578 arraylen = avio_rb32(ioc);
579 for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++)
580 if (amf_parse_object(s, NULL, NULL, NULL, max_pos,
581 depth + 1) < 0)
582 return -1; // if we couldn't skip, bomb out.
583 }
584 break;
585 case AMF_DATA_TYPE_DATE:
586 // timestamp (double) and UTC offset (int16)
587 date.milliseconds = av_int2double(avio_rb64(ioc));
588 date.timezone = avio_rb16(ioc);
589 break;
590 default: // unsupported type, we couldn't skip
591 av_log(s, AV_LOG_ERROR, "unsupported amf type %d\n", amf_type);
592 return -1;
593 }
594
595 if (key) {
596 apar = astream ? astream->codecpar : NULL;
597 vpar = vstream ? vstream->codecpar : NULL;
598
599 // stream info doesn't live any deeper than the first object
600 if (depth == 1) {
601 if (amf_type == AMF_DATA_TYPE_NUMBER ||
602 amf_type == AMF_DATA_TYPE_BOOL) {
603 if (!strcmp(key, "duration"))
604 s->duration = num_val * AV_TIME_BASE;
605 else if (!strcmp(key, "videodatarate") &&
606 0 <= (int)(num_val * 1024.0))
607 flv->video_bit_rate = num_val * 1024.0;
608 else if (!strcmp(key, "audiodatarate") &&
609 0 <= (int)(num_val * 1024.0))
610 flv->audio_bit_rate = num_val * 1024.0;
611 else if (!strcmp(key, "datastream")) {
612 AVStream *st = create_stream(s, AVMEDIA_TYPE_SUBTITLE);
613 if (!st)
614 return AVERROR(ENOMEM);
615 st->codecpar->codec_id = AV_CODEC_ID_TEXT;
616 } else if (!strcmp(key, "framerate")) {
617 flv->framerate = av_d2q(num_val, 1000);
618 if (vstream)
619 vstream->avg_frame_rate = flv->framerate;
620 } else if (flv->trust_metadata) {
621 if (!strcmp(key, "videocodecid") && vpar) {
622 int ret = flv_set_video_codec(s, vstream, num_val, 0);
623 if (ret < 0)
624 return ret;
625 } else if (!strcmp(key, "audiocodecid") && apar) {
626 int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
627 flv_set_audio_codec(s, astream, apar, id);
628 } else if (!strcmp(key, "audiosamplerate") && apar) {
629 apar->sample_rate = num_val;
630 } else if (!strcmp(key, "audiosamplesize") && apar) {
631 apar->bits_per_coded_sample = num_val;
632 } else if (!strcmp(key, "stereo") && apar) {
633 apar->channels = num_val + 1;
634 apar->channel_layout = apar->channels == 2 ?
635 AV_CH_LAYOUT_STEREO :
636 AV_CH_LAYOUT_MONO;
637 } else if (!strcmp(key, "width") && vpar) {
638 vpar->width = num_val;
639 } else if (!strcmp(key, "height") && vpar) {
640 vpar->height = num_val;
641 }
642 }
643 }
644 if (amf_type == AMF_DATA_TYPE_STRING) {
645 if (!strcmp(key, "encoder")) {
646 int version = -1;
647 if (1 == sscanf(str_val, "Open Broadcaster Software v0.%d", &version)) {
648 if (version > 0 && version <= 655)
649 flv->broken_sizes = 1;
650 }
651 } else if (!strcmp(key, "metadatacreator")) {
652 if ( !strcmp (str_val, "MEGA")
653 || !strncmp(str_val, "FlixEngine", 10))
654 flv->broken_sizes = 1;
655 }
656 }
657 }
658
659 if (amf_type == AMF_DATA_TYPE_OBJECT && s->nb_streams == 1 &&
660 ((!apar && !strcmp(key, "audiocodecid")) ||
661 (!vpar && !strcmp(key, "videocodecid"))))
662 s->ctx_flags &= ~AVFMTCTX_NOHEADER; //If there is either audio/video missing, codecid will be an empty object
663
664 if ((!strcmp(key, "duration") ||
665 !strcmp(key, "filesize") ||
666 !strcmp(key, "width") ||
667 !strcmp(key, "height") ||
668 !strcmp(key, "videodatarate") ||
669 !strcmp(key, "framerate") ||
670 !strcmp(key, "videocodecid") ||
671 !strcmp(key, "audiodatarate") ||
672 !strcmp(key, "audiosamplerate") ||
673 !strcmp(key, "audiosamplesize") ||
674 !strcmp(key, "stereo") ||
675 !strcmp(key, "audiocodecid") ||
676 !strcmp(key, "datastream")) && !flv->dump_full_metadata)
677 return 0;
678
679 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
680 if (amf_type == AMF_DATA_TYPE_BOOL) {
681 av_strlcpy(str_val, num_val > 0 ? "true" : "false",
682 sizeof(str_val));
683 av_dict_set(&s->metadata, key, str_val, 0);
684 } else if (amf_type == AMF_DATA_TYPE_NUMBER) {
685 snprintf(str_val, sizeof(str_val), "%.f", num_val);
686 av_dict_set(&s->metadata, key, str_val, 0);
687 } else if (amf_type == AMF_DATA_TYPE_STRING) {
688 av_dict_set(&s->metadata, key, str_val, 0);
689 } else if (amf_type == AMF_DATA_TYPE_DATE) {
690 time_t time;
691 struct tm t;
692 char datestr[128];
693 time = date.milliseconds / 1000; // to seconds
694 localtime_r(&time, &t);
695 strftime(datestr, sizeof(datestr), "%a, %d %b %Y %H:%M:%S %z", &t);
696
697 av_dict_set(&s->metadata, key, datestr, 0);
698 }
699 }
700
701 return 0;
702 }
703
704 #define TYPE_ONTEXTDATA 1
705 #define TYPE_ONCAPTION 2
706 #define TYPE_ONCAPTIONINFO 3
707 #define TYPE_UNKNOWN 9
708
709 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
710 {
711 FLVContext *flv = s->priv_data;
712 AMFDataType type;
713 AVStream *stream, *astream, *vstream;
714 AVStream av_unused *dstream;
715 AVIOContext *ioc;
716 int i;
717 char buffer[32];
718
719 astream = NULL;
720 vstream = NULL;
721 dstream = NULL;
722 ioc = s->pb;
723
724 // first object needs to be "onMetaData" string
725 type = avio_r8(ioc);
726 if (type != AMF_DATA_TYPE_STRING ||
727 amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
728 return TYPE_UNKNOWN;
729
730 if (!strcmp(buffer, "onTextData"))
731 return TYPE_ONTEXTDATA;
732
733 if (!strcmp(buffer, "onCaption"))
734 return TYPE_ONCAPTION;
735
736 if (!strcmp(buffer, "onCaptionInfo"))
737 return TYPE_ONCAPTIONINFO;
738
739 if (strcmp(buffer, "onMetaData") && strcmp(buffer, "onCuePoint")) {
740 av_log(s, AV_LOG_DEBUG, "Unknown type %s\n", buffer);
741 return TYPE_UNKNOWN;
742 }
743
744 // find the streams now so that amf_parse_object doesn't need to do
745 // the lookup every time it is called.
746 for (i = 0; i < s->nb_streams; i++) {
747 stream = s->streams[i];
748 if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
749 vstream = stream;
750 flv->last_keyframe_stream_index = i;
751 } else if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
752 astream = stream;
753 if (flv->last_keyframe_stream_index == -1)
754 flv->last_keyframe_stream_index = i;
755 }
756 else if (stream->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
757 dstream = stream;
758 }
759
760 // parse the second object (we want a mixed array)
761 if (amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
762 return -1;
763
764 return 0;
765 }
766
767 static int flv_read_header(AVFormatContext *s)
768 {
769 int flags;
770 FLVContext *flv = s->priv_data;
771 int offset;
772 int pre_tag_size = 0;
773
774 /* Actual FLV data at 0xe40000 in KUX file */
775 if(!strcmp(s->iformat->name, "kux"))
776 avio_skip(s->pb, 0xe40000);
777
778 avio_skip(s->pb, 4);
779 flags = avio_r8(s->pb);
780
781 flv->missing_streams = flags & (FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO);
782
783 s->ctx_flags |= AVFMTCTX_NOHEADER;
784
785 offset = avio_rb32(s->pb);
786 avio_seek(s->pb, offset, SEEK_SET);
787
788 /* Annex E. The FLV File Format
789 * E.3 TheFLVFileBody
790 * Field Type Comment
791 * PreviousTagSize0 UI32 Always 0
792 * */
793 pre_tag_size = avio_rb32(s->pb);
794 if (pre_tag_size) {
795 av_log(s, AV_LOG_WARNING, "Read FLV header error, input file is not a standard flv format, first PreviousTagSize0 always is 0\n");
796 }
797
798 s->start_time = 0;
799 flv->sum_flv_tag_size = 0;
800 flv->last_keyframe_stream_index = -1;
801
802 return 0;
803 }
804
805 static int flv_read_close(AVFormatContext *s)
806 {
807 int i;
808 FLVContext *flv = s->priv_data;
809 for (i=0; i<FLV_STREAM_TYPE_NB; i++)
810 av_freep(&flv->new_extradata[i]);
811 av_freep(&flv->keyframe_times);
812 av_freep(&flv->keyframe_filepositions);
813 return 0;
814 }
815
816 static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
817 {
818 int ret;
819 if (!size)
820 return 0;
821
822 if ((ret = ff_get_extradata(s, st->codecpar, s->pb, size)) < 0)
823 return ret;
824 st->internal->need_context_update = 1;
825 return 0;
826 }
827
828 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
829 int size)
830 {
831 if (!size)
832 return 0;
833
834 av_free(flv->new_extradata[stream]);
835 flv->new_extradata[stream] = av_mallocz(size +
836 AV_INPUT_BUFFER_PADDING_SIZE);
837 if (!flv->new_extradata[stream])
838 return AVERROR(ENOMEM);
839 flv->new_extradata_size[stream] = size;
840 avio_read(pb, flv->new_extradata[stream], size);
841 return 0;
842 }
843
844 static void clear_index_entries(AVFormatContext *s, int64_t pos)
845 {
846 int i, j, out;
847 av_log(s, AV_LOG_WARNING,
848 "Found invalid index entries, clearing the index.\n");
849 for (i = 0; i < s->nb_streams; i++) {
850 AVStream *st = s->streams[i];
851 /* Remove all index entries that point to >= pos */
852 out = 0;
853 for (j = 0; j < st->nb_index_entries; j++)
854 if (st->index_entries[j].pos < pos)
855 st->index_entries[out++] = st->index_entries[j];
856 st->nb_index_entries = out;
857 }
858 }
859
860 static int amf_skip_tag(AVIOContext *pb, AMFDataType type, int depth)
861 {
862 int nb = -1, ret, parse_name = 1;
863
864 if (depth > MAX_DEPTH)
865 return AVERROR_PATCHWELCOME;
866
867 if (avio_feof(pb))
868 return AVERROR_EOF;
869
870 switch (type) {
871 case AMF_DATA_TYPE_NUMBER:
872 avio_skip(pb, 8);
873 break;
874 case AMF_DATA_TYPE_BOOL:
875 avio_skip(pb, 1);
876 break;
877 case AMF_DATA_TYPE_STRING:
878 avio_skip(pb, avio_rb16(pb));
879 break;
880 case AMF_DATA_TYPE_ARRAY:
881 parse_name = 0;
882 case AMF_DATA_TYPE_MIXEDARRAY:
883 nb = avio_rb32(pb);
884 if (nb < 0)
885 return AVERROR_INVALIDDATA;
886 case AMF_DATA_TYPE_OBJECT:
887 while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) {
888 if (parse_name) {
889 int size = avio_rb16(pb);
890 if (!size) {
891 avio_skip(pb, 1);
892 break;
893 }
894 avio_skip(pb, size);
895 }
896 if ((ret = amf_skip_tag(pb, avio_r8(pb), depth + 1)) < 0)
897 return ret;
898 }
899 break;
900 case AMF_DATA_TYPE_NULL:
901 case AMF_DATA_TYPE_OBJECT_END:
902 break;
903 default:
904 return AVERROR_INVALIDDATA;
905 }
906 return 0;
907 }
908
909 static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
910 int64_t dts, int64_t next)
911 {
912 AVIOContext *pb = s->pb;
913 AVStream *st = NULL;
914 char buf[20];
915 int ret = AVERROR_INVALIDDATA;
916 int i, length = -1;
917 int array = 0;
918
919 switch (avio_r8(pb)) {
920 case AMF_DATA_TYPE_ARRAY:
921 array = 1;
922 case AMF_DATA_TYPE_MIXEDARRAY:
923 avio_seek(pb, 4, SEEK_CUR);
924 case AMF_DATA_TYPE_OBJECT:
925 break;
926 default:
927 goto skip;
928 }
929
930 while (array || (ret = amf_get_string(pb, buf, sizeof(buf))) > 0) {
931 AMFDataType type = avio_r8(pb);
932 if (type == AMF_DATA_TYPE_STRING && (array || !strcmp(buf, "text"))) {
933 length = avio_rb16(pb);
934 ret = av_get_packet(pb, pkt, length);
935 if (ret < 0)
936 goto skip;
937 else
938 break;
939 } else {
940 if ((ret = amf_skip_tag(pb, type, 0)) < 0)
941 goto skip;
942 }
943 }
944
945 if (length < 0) {
946 ret = AVERROR_INVALIDDATA;
947 goto skip;
948 }
949
950 for (i = 0; i < s->nb_streams; i++) {
951 st = s->streams[i];
952 if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
953 break;
954 }
955
956 if (i == s->nb_streams) {
957 st = create_stream(s, AVMEDIA_TYPE_SUBTITLE);
958 if (!st)
959 return AVERROR(ENOMEM);
960 st->codecpar->codec_id = AV_CODEC_ID_TEXT;
961 }
962
963 pkt->dts = dts;
964 pkt->pts = dts;
965 pkt->size = ret;
966
967 pkt->stream_index = st->index;
968 pkt->flags |= AV_PKT_FLAG_KEY;
969
970 skip:
971 avio_seek(s->pb, next + 4, SEEK_SET);
972
973 return ret;
974 }
975
976 static int resync(AVFormatContext *s)
977 {
978 FLVContext *flv = s->priv_data;
979 int64_t i;
980 int64_t pos = avio_tell(s->pb);
981
982 for (i=0; !avio_feof(s->pb); i++) {
983 int j = i & (RESYNC_BUFFER_SIZE-1);
984 int j1 = j + RESYNC_BUFFER_SIZE;
985 flv->resync_buffer[j ] =
986 flv->resync_buffer[j1] = avio_r8(s->pb);
987
988 if (i >= 8 && pos) {
989 uint8_t *d = flv->resync_buffer + j1 - 8;
990 if (d[0] == 'F' &&
991 d[1] == 'L' &&
992 d[2] == 'V' &&
993 d[3] < 5 && d[5] == 0) {
994 av_log(s, AV_LOG_WARNING, "Concatenated FLV detected, might fail to demux, decode and seek %"PRId64"\n", flv->last_ts);
995 flv->time_offset = flv->last_ts + 1;
996 flv->time_pos = avio_tell(s->pb);
997 }
998 }
999
1000 if (i > 22) {
1001 unsigned lsize2 = AV_RB32(flv->resync_buffer + j1 - 4);
1002 if (lsize2 >= 11 && lsize2 + 8LL < FFMIN(i, RESYNC_BUFFER_SIZE)) {
1003 unsigned size2 = AV_RB24(flv->resync_buffer + j1 - lsize2 + 1 - 4);
1004 unsigned lsize1 = AV_RB32(flv->resync_buffer + j1 - lsize2 - 8);
1005 if (lsize1 >= 11 && lsize1 + 8LL + lsize2 < FFMIN(i, RESYNC_BUFFER_SIZE)) {
1006 unsigned size1 = AV_RB24(flv->resync_buffer + j1 - lsize1 + 1 - lsize2 - 8);
1007 if (size1 == lsize1 - 11 && size2 == lsize2 - 11) {
1008 avio_seek(s->pb, pos + i - lsize1 - lsize2 - 8, SEEK_SET);
1009 return 1;
1010 }
1011 }
1012 }
1013 }
1014 }
1015 return AVERROR_EOF;
1016 }
1017
1018 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
1019 {
1020 FLVContext *flv = s->priv_data;
1021 int ret, i, size, flags;
1022 enum FlvTagType type;
1023 int stream_type=-1;
1024 int64_t next, pos, meta_pos;
1025 int64_t dts, pts = AV_NOPTS_VALUE;
1026 int av_uninit(channels);
1027 int av_uninit(sample_rate);
1028 AVStream *st = NULL;
1029 int last = -1;
1030 int orig_size;
1031
1032 retry:
1033 /* pkt size is repeated at end. skip it */
1034 pos = avio_tell(s->pb);
1035 type = (avio_r8(s->pb) & 0x1F);
1036 orig_size =
1037 size = avio_rb24(s->pb);
1038 flv->sum_flv_tag_size += size + 11LL;
1039 dts = avio_rb24(s->pb);
1040 dts |= (unsigned)avio_r8(s->pb) << 24;
1041 av_log(s, AV_LOG_TRACE, "type:%d, size:%d, last:%d, dts:%"PRId64" pos:%"PRId64"\n", type, size, last, dts, avio_tell(s->pb));
1042 if (avio_feof(s->pb))
1043 return AVERROR_EOF;
1044 avio_skip(s->pb, 3); /* stream id, always 0 */
1045 flags = 0;
1046
1047 if (flv->validate_next < flv->validate_count) {
1048 int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
1049 if (pos == validate_pos) {
1050 if (FFABS(dts - flv->validate_index[flv->validate_next].dts) <=
1051 VALIDATE_INDEX_TS_THRESH) {
1052 flv->validate_next++;
1053 } else {
1054 clear_index_entries(s, validate_pos);
1055 flv->validate_count = 0;
1056 }
1057 } else if (pos > validate_pos) {
1058 clear_index_entries(s, validate_pos);
1059 flv->validate_count = 0;
1060 }
1061 }
1062
1063 if (size == 0) {
1064 ret = FFERROR_REDO;
1065 goto leave;
1066 }
1067
1068 next = size + avio_tell(s->pb);
1069
1070 if (type == FLV_TAG_TYPE_AUDIO) {
1071 stream_type = FLV_STREAM_TYPE_AUDIO;
1072 flags = avio_r8(s->pb);
1073 size--;
1074 } else if (type == FLV_TAG_TYPE_VIDEO) {
1075 stream_type = FLV_STREAM_TYPE_VIDEO;
1076 flags = avio_r8(s->pb);
1077 size--;
1078 if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_VIDEO_INFO_CMD)
1079 goto skip;
1080 } else if (type == FLV_TAG_TYPE_META) {
1081 stream_type=FLV_STREAM_TYPE_SUBTITLE;
1082 if (size > 13 + 1 + 4) { // Header-type metadata stuff
1083 int type;
1084 meta_pos = avio_tell(s->pb);
1085 type = flv_read_metabody(s, next);
1086 if (type == 0 && dts == 0 || type < 0) {
1087 if (type < 0 && flv->validate_count &&
1088 flv->validate_index[0].pos > next &&
1089 flv->validate_index[0].pos - 4 < next
1090 ) {
1091 av_log(s, AV_LOG_WARNING, "Adjusting next position due to index mismatch\n");
1092 next = flv->validate_index[0].pos - 4;
1093 }
1094 goto skip;
1095 } else if (type == TYPE_ONTEXTDATA) {
1096 avpriv_request_sample(s, "OnTextData packet");
1097 return flv_data_packet(s, pkt, dts, next);
1098 } else if (type == TYPE_ONCAPTION) {
1099 return flv_data_packet(s, pkt, dts, next);
1100 } else if (type == TYPE_UNKNOWN) {
1101 stream_type = FLV_STREAM_TYPE_DATA;
1102 }
1103 avio_seek(s->pb, meta_pos, SEEK_SET);
1104 }
1105 } else {
1106 av_log(s, AV_LOG_DEBUG,
1107 "Skipping flv packet: type %d, size %d, flags %d.\n",
1108 type, size, flags);
1109 skip:
1110 if (avio_seek(s->pb, next, SEEK_SET) != next) {
1111 // This can happen if flv_read_metabody above read past
1112 // next, on a non-seekable input, and the preceding data has
1113 // been flushed out from the IO buffer.
1114 av_log(s, AV_LOG_ERROR, "Unable to seek to the next packet\n");
1115 return AVERROR_INVALIDDATA;
1116 }
1117 ret = FFERROR_REDO;
1118 goto leave;
1119 }
1120
1121 /* skip empty data packets */
1122 if (!size) {
1123 ret = FFERROR_REDO;
1124 goto leave;
1125 }
1126
1127 /* now find stream */
1128 for (i = 0; i < s->nb_streams; i++) {
1129 st = s->streams[i];
1130 if (stream_type == FLV_STREAM_TYPE_AUDIO) {
1131 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1132 (s->audio_codec_id || flv_same_audio_codec(st->codecpar, flags)))
1133 break;
1134 } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
1135 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1136 (s->video_codec_id || flv_same_video_codec(st->codecpar, flags)))
1137 break;
1138 } else if (stream_type == FLV_STREAM_TYPE_SUBTITLE) {
1139 if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
1140 break;
1141 } else if (stream_type == FLV_STREAM_TYPE_DATA) {
1142 if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
1143 break;
1144 }
1145 }
1146 if (i == s->nb_streams) {
1147 static const enum AVMediaType stream_types[] = {AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_SUBTITLE, AVMEDIA_TYPE_DATA};
1148 st = create_stream(s, stream_types[stream_type]);
1149 if (!st)
1150 return AVERROR(ENOMEM);
1151
1152 }
1153 av_log(s, AV_LOG_TRACE, "%d %X %d \n", stream_type, flags, st->discard);
1154
1155 if (flv->time_pos <= pos) {
1156 dts += flv->time_offset;
1157 }
1158
1159 if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
1160 ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
1161 stream_type == FLV_STREAM_TYPE_AUDIO))
1162 av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
1163
1164 if ( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || (stream_type == FLV_STREAM_TYPE_AUDIO)))
1165 ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && (stream_type == FLV_STREAM_TYPE_VIDEO)))
1166 || st->discard >= AVDISCARD_ALL
1167 ) {
1168 avio_seek(s->pb, next, SEEK_SET);
1169 ret = FFERROR_REDO;
1170 goto leave;
1171 }
1172
1173 // if not streamed and no duration from metadata then seek to end to find
1174 // the duration from the timestamps
1175 if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
1176 (!s->duration || s->duration == AV_NOPTS_VALUE) &&
1177 !flv->searched_for_end) {
1178 int size;
1179 const int64_t pos = avio_tell(s->pb);
1180 // Read the last 4 bytes of the file, this should be the size of the
1181 // previous FLV tag. Use the timestamp of its payload as duration.
1182 int64_t fsize = avio_size(s->pb);
1183 retry_duration:
1184 avio_seek(s->pb, fsize - 4, SEEK_SET);
1185 size = avio_rb32(s->pb);
1186 if (size > 0 && size < fsize) {
1187 // Seek to the start of the last FLV tag at position (fsize - 4 - size)
1188 // but skip the byte indicating the type.
1189 avio_seek(s->pb, fsize - 3 - size, SEEK_SET);
1190 if (size == avio_rb24(s->pb) + 11) {
1191 uint32_t ts = avio_rb24(s->pb);
1192 ts |= (unsigned)avio_r8(s->pb) << 24;
1193 if (ts)
1194 s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
1195 else if (fsize >= 8 && fsize - 8 >= size) {
1196 fsize -= size+4;
1197 goto retry_duration;
1198 }
1199 }
1200 }
1201
1202 avio_seek(s->pb, pos, SEEK_SET);
1203 flv->searched_for_end = 1;
1204 }
1205
1206 if (stream_type == FLV_STREAM_TYPE_AUDIO) {
1207 int bits_per_coded_sample;
1208 channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
1209 sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
1210 FLV_AUDIO_SAMPLERATE_OFFSET) >> 3;
1211 bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
1212 if (!st->codecpar->channels || !st->codecpar->sample_rate ||
1213 !st->codecpar->bits_per_coded_sample) {
1214 st->codecpar->channels = channels;
1215 st->codecpar->channel_layout = channels == 1
1216 ? AV_CH_LAYOUT_MONO
1217 : AV_CH_LAYOUT_STEREO;
1218 st->codecpar->sample_rate = sample_rate;
1219 st->codecpar->bits_per_coded_sample = bits_per_coded_sample;
1220 }
1221 if (!st->codecpar->codec_id) {
1222 flv_set_audio_codec(s, st, st->codecpar,
1223 flags & FLV_AUDIO_CODECID_MASK);
1224 flv->last_sample_rate =
1225 sample_rate = st->codecpar->sample_rate;
1226 flv->last_channels =
1227 channels = st->codecpar->channels;
1228 } else {
1229 AVCodecParameters *par = avcodec_parameters_alloc();
1230 if (!par) {
1231 ret = AVERROR(ENOMEM);
1232 goto leave;
1233 }
1234 par->sample_rate = sample_rate;
1235 par->bits_per_coded_sample = bits_per_coded_sample;
1236 flv_set_audio_codec(s, st, par, flags & FLV_AUDIO_CODECID_MASK);
1237 sample_rate = par->sample_rate;
1238 avcodec_parameters_free(&par);
1239 }
1240 } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
1241 int ret = flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
1242 if (ret < 0)
1243 return ret;
1244 size -= ret;
1245 } else if (stream_type == FLV_STREAM_TYPE_SUBTITLE) {
1246 st->codecpar->codec_id = AV_CODEC_ID_TEXT;
1247 } else if (stream_type == FLV_STREAM_TYPE_DATA) {
1248 st->codecpar->codec_id = AV_CODEC_ID_NONE; // Opaque AMF data
1249 }
1250
1251 if (st->codecpar->codec_id == AV_CODEC_ID_AAC ||
1252 st->codecpar->codec_id == AV_CODEC_ID_H264 ||
1253 st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
1254 int type = avio_r8(s->pb);
1255 size--;
1256
1257 if (size < 0) {
1258 ret = AVERROR_INVALIDDATA;
1259 goto leave;
1260 }
1261
1262 if (st->codecpar->codec_id == AV_CODEC_ID_H264 || st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
1263 // sign extension
1264 int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
1265 pts = av_sat_add64(dts, cts);
1266 if (cts < 0) { // dts might be wrong
1267 if (!flv->wrong_dts)
1268 av_log(s, AV_LOG_WARNING,
1269 "Negative cts, previous timestamps might be wrong.\n");
1270 flv->wrong_dts = 1;
1271 } else if (FFABS(dts - pts) > 1000*60*15) {
1272 av_log(s, AV_LOG_WARNING,
1273 "invalid timestamps %"PRId64" %"PRId64"\n", dts, pts);
1274 dts = pts = AV_NOPTS_VALUE;
1275 }
1276 }
1277 if (type == 0 && (!st->codecpar->extradata || st->codecpar->codec_id == AV_CODEC_ID_AAC ||
1278 st->codecpar->codec_id == AV_CODEC_ID_H264)) {
1279 AVDictionaryEntry *t;
1280
1281 if (st->codecpar->extradata) {
1282 if ((ret = flv_queue_extradata(flv, s->pb, stream_type, size)) < 0)
1283 return ret;
1284 ret = FFERROR_REDO;
1285 goto leave;
1286 }
1287 if ((ret = flv_get_extradata(s, st, size)) < 0)
1288 return ret;
1289
1290 /* Workaround for buggy Omnia A/XE encoder */
1291 t = av_dict_get(s->metadata, "Encoder", NULL, 0);
1292 if (st->codecpar->codec_id == AV_CODEC_ID_AAC && t && !strcmp(t->value, "Omnia A/XE"))
1293 st->codecpar->extradata_size = 2;
1294
1295 ret = FFERROR_REDO;
1296 goto leave;
1297 }
1298 }
1299
1300 /* skip empty data packets */
1301 if (!size) {
1302 ret = FFERROR_REDO;
1303 goto leave;
1304 }
1305
1306 ret = av_get_packet(s->pb, pkt, size);
1307 if (ret < 0)
1308 return ret;
1309 pkt->dts = dts;
1310 pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
1311 pkt->stream_index = st->index;
1312 pkt->pos = pos;
1313 if (flv->new_extradata[stream_type]) {
1314 int ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
1315 flv->new_extradata[stream_type],
1316 flv->new_extradata_size[stream_type]);
1317 if (ret >= 0) {
1318 flv->new_extradata[stream_type] = NULL;
1319 flv->new_extradata_size[stream_type] = 0;
1320 }
1321 }
1322 if (stream_type == FLV_STREAM_TYPE_AUDIO &&
1323 (sample_rate != flv->last_sample_rate ||
1324 channels != flv->last_channels)) {
1325 flv->last_sample_rate = sample_rate;
1326 flv->last_channels = channels;
1327 ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
1328 }
1329
1330 if ( stream_type == FLV_STREAM_TYPE_AUDIO ||
1331 ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY) ||
1332 stream_type == FLV_STREAM_TYPE_SUBTITLE ||
1333 stream_type == FLV_STREAM_TYPE_DATA)
1334 pkt->flags |= AV_PKT_FLAG_KEY;
1335
1336 leave:
1337 last = avio_rb32(s->pb);
1338 if (!flv->trust_datasize) {
1339 if (last != orig_size + 11 && last != orig_size + 10 &&
1340 !avio_feof(s->pb) &&
1341 (last != orig_size || !last) && last != flv->sum_flv_tag_size &&
1342 !flv->broken_sizes) {
1343 av_log(s, AV_LOG_ERROR, "Packet mismatch %d %d %"PRId64"\n", last, orig_size + 11, flv->sum_flv_tag_size);
1344 avio_seek(s->pb, pos + 1, SEEK_SET);
1345 ret = resync(s);
1346 av_packet_unref(pkt);
1347 if (ret >= 0) {
1348 goto retry;
1349 }
1350 }
1351 }
1352
1353 if (ret >= 0)
1354 flv->last_ts = pkt->dts;
1355
1356 return ret;
1357 }
1358
1359 static int flv_read_seek(AVFormatContext *s, int stream_index,
1360 int64_t ts, int flags)
1361 {
1362 FLVContext *flv = s->priv_data;
1363 flv->validate_count = 0;
1364 return avio_seek_time(s->pb, stream_index, ts, flags);
1365 }
1366
1367 #define OFFSET(x) offsetof(FLVContext, x)
1368 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1369 static const AVOption options[] = {
1370 { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1371 { "flv_full_metadata", "Dump full metadata of the onMetadata", OFFSET(dump_full_metadata), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1372 { "flv_ignore_prevtag", "Ignore the Size of previous tag", OFFSET(trust_datasize), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1373 { "missing_streams", "", OFFSET(missing_streams), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 0xFF, VD | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
1374 { NULL }
1375 };
1376
1377 static const AVClass flv_class = {
1378 .class_name = "flvdec",
1379 .item_name = av_default_item_name,
1380 .option = options,
1381 .version = LIBAVUTIL_VERSION_INT,
1382 };
1383
1384 AVInputFormat ff_flv_demuxer = {
1385 .name = "flv",
1386 .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
1387 .priv_data_size = sizeof(FLVContext),
1388 .read_probe = flv_probe,
1389 .read_header = flv_read_header,
1390 .read_packet = flv_read_packet,
1391 .read_seek = flv_read_seek,
1392 .read_close = flv_read_close,
1393 .extensions = "flv",
1394 .priv_class = &flv_class,
1395 };
1396
1397 static const AVClass live_flv_class = {
1398 .class_name = "live_flvdec",
1399 .item_name = av_default_item_name,
1400 .option = options,
1401 .version = LIBAVUTIL_VERSION_INT,
1402 };
1403
1404 AVInputFormat ff_live_flv_demuxer = {
1405 .name = "live_flv",
1406 .long_name = NULL_IF_CONFIG_SMALL("live RTMP FLV (Flash Video)"),
1407 .priv_data_size = sizeof(FLVContext),
1408 .read_probe = live_flv_probe,
1409 .read_header = flv_read_header,
1410 .read_packet = flv_read_packet,
1411 .read_seek = flv_read_seek,
1412 .read_close = flv_read_close,
1413 .extensions = "flv",
1414 .priv_class = &live_flv_class,
1415 .flags = AVFMT_TS_DISCONT
1416 };
1417
1418 static const AVClass kux_class = {
1419 .class_name = "kuxdec",
1420 .item_name = av_default_item_name,
1421 .option = options,
1422 .version = LIBAVUTIL_VERSION_INT,
1423 };
1424
1425 AVInputFormat ff_kux_demuxer = {
1426 .name = "kux",
1427 .long_name = NULL_IF_CONFIG_SMALL("KUX (YouKu)"),
1428 .priv_data_size = sizeof(FLVContext),
1429 .read_probe = kux_probe,
1430 .read_header = flv_read_header,
1431 .read_packet = flv_read_packet,
1432 .read_seek = flv_read_seek,
1433 .read_close = flv_read_close,
1434 .extensions = "kux",
1435 .priv_class = &kux_class,
1436 };