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