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 4bits: difference between encoded width and visible width
8 * - lower 4bits: 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
39
40 #define VALIDATE_INDEX_TS_THRESH 2500
41
43 const AVClass *
class;
///< Class for private options.
50 struct {
58
60 {
63
64 if (d[0] == 'F' &&
65 d[1] == 'L' &&
66 d[2] == 'V' &&
67 d[3] < 5 && d[5] == 0 &&
69 offset > 8) {
70 int is_live = !memcmp(d + offset + 40, "NGINX RTMP", 10);
71
72 if (live == is_live)
74 }
75 return 0;
76 }
77
79 {
81 }
82
84 {
86 }
87
89 {
91 if (!st)
98
100 return st;
101 }
102
104 {
108
110 return 1;
111
113 return 0;
114
115 switch (flv_codecid) {
116 // no distinction between S16 and S8 PCM codec flags
118 codec_id = bits_per_coded_sample == 8
120 #if HAVE_BIGENDIAN
122 #else
124 #endif
125 return codec_id == acodec->
codec_id;
127 codec_id = bits_per_coded_sample == 8
130 return codec_id == acodec->
codec_id;
149 default:
151 }
152 }
153
156 {
157 switch (flv_codecid) {
158 // no distinction between S16 and S8 PCM codec flags
162 #if HAVE_BIGENDIAN
164 #else
166 #endif
167 break;
172 break;
175 break;
178 break;
182 break;
186 break;
188 // in case metadata does not otherwise declare samplerate
191 break;
195 break;
198 break;
202 break;
206 break;
207 default:
211 }
212 }
213
215 {
217
219 return 1;
220
221 switch (flv_codecid) {
234 default:
236 }
237 }
238
240 int flv_codecid, int read)
241 {
243 switch (flv_codecid) {
246 break;
249 break; // Really mean it this time
252 break;
255 break;
261 if (read) {
264 }
267 else
269 }
270 return 1; // 1 byte body size adjustment for flv_read_packet()
274 return 3; // not 4, reading packet type will consume one byte
277 return 3;
278 default:
281 }
282
283 return 0;
284 }
285
287 {
289 if (length >= buffsize) {
291 return -1;
292 }
293
295
297
299 }
300
303 {
305 unsigned int timeslen = 0, fileposlen = 0, i;
306 char str_val[256];
307 int64_t *times =
NULL;
308 int64_t *filepositions =
NULL;
311
314 return 0;
315 }
316
318 return 0;
319
322 int64_t **current_array;
323 unsigned int arraylen;
324
325 // Expect array object in context
327 break;
328
330 if (arraylen>>28)
331 break;
332
334 current_array = ×
335 timeslen = arraylen;
337 !filepositions) {
338 current_array = &filepositions;
339 fileposlen = arraylen;
340 } else
341 // unexpected metatag inside keyframes, will not use such
342 // metadata for indexing
343 break;
344
345 if (!(*current_array =
av_mallocz(
sizeof(**current_array) * arraylen))) {
348 }
349
350 for (i = 0; i < arraylen &&
avio_tell(ioc) < max_pos - 1; i++) {
352 goto invalid;
354 }
355 if (times && filepositions) {
356 // All done, exiting at a position allowing amf_parse_object
357 // to finish parsing the object
358 ret = 0;
359 break;
360 }
361 }
362
363 if (timeslen == fileposlen && fileposlen>1 && max_pos <= filepositions[0]) {
364 for (i = 0; i < fileposlen; i++) {
367 if (i < 2) {
371 }
372 }
373 } else {
374 invalid:
376 }
377
383 }
384
388 {
393 char str_val[1024];
394 double num_val;
395
396 num_val = 0;
399
400 switch (amf_type) {
403 break;
406 break;
410 return -1;
411 }
412 break;
414 if ((vstream || astream) && key &&
418 max_pos) < 0)
420
424 depth + 1) < 0)
425 return -1; // if we couldn't skip, bomb out.
428 return -1;
429 }
430 break;
434 break; // these take up no additional space
436 avio_skip(ioc, 4);
// skip 32-bit max array index
439 // this is the only case in which we would want a nested
440 // parse to not skip over the object
442 depth + 1) < 0)
443 return -1;
446 return -1;
447 }
448 break;
450 {
451 unsigned int arraylen, i;
452
454 for (i = 0; i < arraylen &&
avio_tell(ioc) < max_pos - 1; i++)
456 depth + 1) < 0)
457 return -1; // if we couldn't skip, bomb out.
458 }
459 break;
461 avio_skip(ioc, 8 + 2);
// timestamp (double) and UTC offset (int16)
462 break;
463 default: // unsupported type, we couldn't skip
465 return -1;
466 }
467
468 if (key) {
469 acodec = astream ? astream->
codec :
NULL;
470 vcodec = vstream ? vstream->
codec :
NULL;
471
472 // stream info doesn't live any deeper than the first object
473 if (depth == 1) {
476 if (!strcmp(key, "duration"))
478 else if (!strcmp(key, "videodatarate") && vcodec &&
479 0 <= (int)(num_val * 1024.0))
480 vcodec->
bit_rate = num_val * 1024.0;
481 else if (!strcmp(key, "audiodatarate") && acodec &&
482 0 <= (int)(num_val * 1024.0))
483 acodec->
bit_rate = num_val * 1024.0;
484 else if (!strcmp(key, "datastream")) {
486 if (!st)
490 if (!strcmp(key, "videocodecid") && vcodec) {
492 } else if (!strcmp(key, "audiocodecid") && acodec) {
495 } else if (!strcmp(key, "audiosamplerate") && acodec) {
497 } else if (!strcmp(key, "audiosamplesize") && acodec) {
499 } else if (!strcmp(key, "stereo") && acodec) {
504 } else if (!strcmp(key, "width") && vcodec) {
505 vcodec->
width = num_val;
506 } else if (!strcmp(key, "height") && vcodec) {
508 }
509 }
510 }
511 }
512
514 ((!acodec && !strcmp(key, "audiocodecid")) ||
515 (!vcodec && !strcmp(key, "videocodecid"))))
517
518 if (!strcmp(key, "duration") ||
519 !strcmp(key, "filesize") ||
520 !strcmp(key, "width") ||
521 !strcmp(key, "height") ||
522 !strcmp(key, "videodatarate") ||
523 !strcmp(key, "framerate") ||
524 !strcmp(key, "videocodecid") ||
525 !strcmp(key, "audiodatarate") ||
526 !strcmp(key, "audiosamplerate") ||
527 !strcmp(key, "audiosamplesize") ||
528 !strcmp(key, "stereo") ||
529 !strcmp(key, "audiocodecid") ||
530 !strcmp(key, "datastream"))
531 return 0;
532
535 av_strlcpy(str_val, num_val > 0 ?
"true" :
"false",
536 sizeof(str_val));
539 snprintf(str_val,
sizeof(str_val),
"%.f", num_val);
543 }
544
545 return 0;
546 }
547
548 #define TYPE_ONTEXTDATA 1
549 #define TYPE_UNKNOWN 2
550
552 {
554 AVStream *stream, *astream, *vstream;
557 int i;
558 // only needs to hold the string "onMetaData".
559 // Anything longer is something we don't want.
561
566
567 // first object needs to be "onMetaData" string
572
573 if (!strcmp(buffer, "onTextData"))
575
576 if (strcmp(buffer, "onMetaData") && strcmp(buffer, "onCuePoint"))
578
579 // find the streams now so that amf_parse_object doesn't need to do
580 // the lookup every time it is called.
584 vstream = stream;
586 astream = stream;
588 dstream = stream;
589 }
590
591 // parse the second object (we want a mixed array)
593 return -1;
594
595 return 0;
596 }
597
599 {
601
604
606
613 // Flag doesn't indicate whether or not there is script-data present. Must
614 // create that stream if it's encountered.
615
619
621
622 return 0;
623 }
624
626 {
627 int i;
631 return 0;
632 }
633
635 {
639 return 0;
640 }
641
644 {
652 return 0;
653 }
654
656 {
659 "Found invalid index entries, clearing the index.\n");
662 /* Remove all index entries that point to >= pos */
663 out = 0;
668 }
669 }
670
672 {
673 int nb = -1,
ret, parse_name = 1;
674
675 switch (type) {
678 break;
681 break;
684 break;
686 parse_name = 0;
691 if (parse_name) {
693 if (!size) {
695 break;
696 }
698 }
701 }
702 break;
705 break;
706 default:
708 }
709 return 0;
710 }
711
713 int64_t dts, int64_t next)
714 {
720
725 break;
726 default:
727 goto skip;
728 }
729
735 if (ret < 0)
736 goto skip;
737 else
738 break;
739 } else {
741 goto skip;
742 }
743 }
744
745 if (length < 0) {
747 goto skip;
748 }
749
753 break;
754 }
755
758 if (!st)
761 }
762
766
769
770 skip:
772
774 }
775
777 {
780 int stream_type=-1;
781 int64_t next, pos, meta_pos;
786
787 /* pkt size is repeated at end. skip it */
794 av_dlog(s,
"type:%d, size:%d, dts:%"PRId64
" pos:%"PRId64
"\n", type, size, dts,
avio_tell(s->
pb));
798 flags = 0;
799
802 if (pos == validate_pos) {
806 } else {
809 }
810 } else if (pos > validate_pos) {
813 }
814 }
815
816 if (size == 0)
817 continue;
818
820
824 size--;
828 size--;
830 goto skip;
833 if (size > 13 + 1 + 4 && dts == 0) { // Header-type metadata stuff
836 goto skip;
837 }
839 }
840 } else {
842 "Skipping flv packet: type %d, size %d, flags %d.\n",
843 type, size, flags);
844 skip:
846 continue;
847 }
848
849 /* skip empty data packets */
850 if (!size)
851 continue;
852
853 /* now find stream */
859 break;
863 break;
866 break;
867 }
868 }
873 if (!st)
875
876 }
878
882
886 ) {
888 continue;
889 }
890 break;
891 }
892
893 // if not streamed and no duration from metadata then seek to end to find
894 // the duration from the timestamps
898 // Read the last 4 bytes of the file, this should be the size of the
899 // previous FLV tag. Use the timestamp of its payload as duration.
901 retry_duration:
904 // Seek to the start of the last FLV tag at position (fsize - 4 - size)
905 // but skip the byte indicating the type.
910 if (ts)
912 else if (fsize >= 8 && fsize - 8 >= size) {
913 fsize -= size+4;
914 goto retry_duration;
915 }
916 }
917
920 }
921
923 int bits_per_coded_sample;
936 }
944 } else {
950 }
953 }
954
959 size--;
961 // sign extension
963 pts = dts + cts;
964 if (cts < 0) { // dts might be wrong
967 "Negative cts, previous timestamps might be wrong.\n");
969 }
else if (
FFABS(dts - pts) > 1000*60*15) {
971 "invalid timestamps %"PRId64" %"PRId64"\n", dts, pts);
973 }
974 }
978
983 goto leave;
984 }
987
988 /* Workaround for buggy Omnia A/XE encoder */
992
995
1002 else
1004 av_dlog(s,
"mp4a config channels %d sample rate %d\n",
1006 }
1007 }
1008
1010 goto leave;
1011 }
1012 }
1013
1014 /* skip empty data packets */
1015 if (!size) {
1017 goto leave;
1018 }
1019
1021 if (ret < 0)
1029 if (side) {
1034 }
1035 }
1042 }
1043
1048
1049 leave:
1052 }
1053
1055 int64_t ts,
int flags)
1056 {
1060 }
1061
1062 #define OFFSET(x) offsetof(FLVContext, x)
1063 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1065 {
"flv_metadata",
"Allocate streams according to the onMetaData array",
OFFSET(trust_metadata),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
VD },
1067 };
1068
1074 };
1075
1085 .extensions = "flv",
1086 .priv_class = &flv_class,
1087 };
1088
1094 };
1095
1105 .extensions = "flv",
1106 .priv_class = &live_flv_class,
1108 };