FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/flvdec.c
Date: 2024-03-29 01:21:52
Exec Total Coverage
Lines: 648 1005 64.5%
Functions: 23 26 88.5%
Branches: 448 863 51.9%

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