FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/flvdec.c
Date: 2023-12-07 21:54:23
Exec Total Coverage
Lines: 549 897 61.2%
Functions: 21 24 87.5%
Branches: 364 757 48.1%

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