FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/flvdec.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 787 1233 63.8%
Functions: 23 27 85.2%
Branches: 540 1005 53.7%

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/mem.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/internal.h"
35 #include "libavutil/intfloat.h"
36 #include "libavutil/intreadwrite.h"
37 #include "libavutil/mastering_display_metadata.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 AVRational framerate;
101 int64_t last_ts;
102 int64_t time_offset;
103 int64_t time_pos;
104
105 FLVMetaVideoColor *metaVideoColor;
106 int meta_color_info_flag;
107
108 uint8_t **mt_extradata;
109 int *mt_extradata_sz;
110 int mt_extradata_cnt;
111 } FLVContext;
112
113 /* AMF date type */
114 typedef struct amf_date {
115 double milliseconds;
116 int16_t timezone;
117 } amf_date;
118
119 14406 static int probe(const AVProbeData *p, int live)
120 {
121 14406 const uint8_t *d = p->buf;
122 14406 unsigned offset = AV_RB32(d + 5);
123
124
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 14274 times.
14406 if (d[0] == 'F' &&
125
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 60 times.
132 d[1] == 'L' &&
126
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 d[2] == 'V' &&
127
2/4
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
72 d[3] < 5 && d[5] == 0 &&
128
2/4
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
72 offset + 100 < p->buf_size &&
129 offset > 8) {
130 72 int is_live = !memcmp(d + offset + 40, "NGINX RTMP", 10);
131
132
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 36 times.
72 if (live == is_live)
133 36 return AVPROBE_SCORE_MAX;
134 }
135 14370 return 0;
136 }
137
138 7203 static int flv_probe(const AVProbeData *p)
139 {
140 7203 return probe(p, 0);
141 }
142
143 7203 static int live_flv_probe(const AVProbeData *p)
144 {
145 7203 return probe(p, 1);
146 }
147
148 7203 static int kux_probe(const AVProbeData *p)
149 {
150 7203 const uint8_t *d = p->buf;
151
152
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7200 times.
7203 if (d[0] == 'K' &&
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 d[1] == 'D' &&
154 d[2] == 'K' &&
155 d[3] == 0 &&
156 d[4] == 0) {
157 return AVPROBE_SCORE_EXTENSION + 1;
158 }
159 7203 return 0;
160 }
161
162 46 static void add_keyframes_index(AVFormatContext *s)
163 {
164 46 FLVContext *flv = s->priv_data;
165 46 AVStream *stream = NULL;
166 46 unsigned int i = 0;
167
168
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 43 times.
46 if (flv->last_keyframe_stream_index < 0) {
169 3 av_log(s, AV_LOG_DEBUG, "keyframe stream hasn't been created\n");
170 3 return;
171 }
172
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 av_assert0(flv->last_keyframe_stream_index <= s->nb_streams);
174 43 stream = s->streams[flv->last_keyframe_stream_index];
175
176
1/2
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
43 if (ffstream(stream)->nb_index_entries == 0) {
177
2/2
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 43 times.
216 for (i = 0; i < flv->keyframe_count; i++) {
178 173 av_log(s, AV_LOG_TRACE, "keyframe filepositions = %"PRId64" times = %"PRId64"\n",
179 173 flv->keyframe_filepositions[i], flv->keyframe_times[i]);
180 173 av_add_index_entry(stream, flv->keyframe_filepositions[i],
181 173 flv->keyframe_times[i], 0, 0, AVINDEX_KEYFRAME);
182 }
183 } else
184 av_log(s, AV_LOG_WARNING, "Skipping duplicate index\n");
185
186
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 12 times.
43 if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
187 31 av_freep(&flv->keyframe_times);
188 31 av_freep(&flv->keyframe_filepositions);
189 31 flv->keyframe_count = 0;
190 }
191 }
192
193 67 static AVStream *create_stream(AVFormatContext *s, int codec_type, int track_idx)
194 {
195 67 FFFormatContext *const si = ffformatcontext(s);
196 67 FLVContext *flv = s->priv_data;
197 67 AVStream *st = avformat_new_stream(s, NULL);
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (!st)
199 return NULL;
200 67 st->codecpar->codec_type = codec_type;
201 67 st->id = track_idx;
202 67 avpriv_set_pts_info(st, 32, 1, 1000); /* 32 bit pts in ms */
203
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 43 times.
67 if (track_idx)
204 24 return st;
205
206
3/4
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 37 times.
43 if (s->nb_streams>=3 ||( s->nb_streams==2
207
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 && s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE
208
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 && s->streams[1]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE
209
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 && s->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_DATA
210
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 && s->streams[1]->codecpar->codec_type != AVMEDIA_TYPE_DATA))
211 6 s->ctx_flags &= ~AVFMTCTX_NOHEADER;
212
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 31 times.
43 if (codec_type == AVMEDIA_TYPE_AUDIO) {
213 12 st->codecpar->bit_rate = flv->audio_bit_rate;
214 12 si->missing_streams &= ~FLV_HEADER_FLAG_HASAUDIO;
215 }
216
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 12 times.
43 if (codec_type == AVMEDIA_TYPE_VIDEO) {
217 31 st->codecpar->bit_rate = flv->video_bit_rate;
218 31 si->missing_streams &= ~FLV_HEADER_FLAG_HASVIDEO;
219 31 st->avg_frame_rate = flv->framerate;
220 }
221
222 43 flv->last_keyframe_stream_index = s->nb_streams - 1;
223 43 add_keyframes_index(s);
224 43 return st;
225 }
226
227 4905 static int flv_same_audio_codec(AVCodecParameters *apar, int flags, uint32_t codec_fourcc)
228 {
229
2/2
✓ Branch 0 taken 2592 times.
✓ Branch 1 taken 2313 times.
4905 int bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
230 4905 int flv_codecid = flags & FLV_AUDIO_CODECID_MASK;
231 int codec_id;
232
233
5/8
✓ Branch 0 taken 1110 times.
✓ Branch 1 taken 465 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 165 times.
✓ Branch 4 taken 573 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2592 times.
✗ Branch 7 not taken.
4905 switch (codec_fourcc) {
234 1110 case MKBETAG('m', 'p', '4', 'a'):
235 1110 return apar->codec_id == AV_CODEC_ID_AAC;
236 465 case MKBETAG('O', 'p', 'u', 's'):
237 465 return apar->codec_id == AV_CODEC_ID_OPUS;
238 case MKBETAG('.', 'm', 'p', '3'):
239 return apar->codec_id == AV_CODEC_ID_MP3;
240 165 case MKBETAG('f', 'L', 'a', 'C'):
241 165 return apar->codec_id == AV_CODEC_ID_FLAC;
242 573 case MKBETAG('a', 'c', '-', '3'):
243 573 return apar->codec_id == AV_CODEC_ID_AC3;
244 case MKBETAG('e', 'c', '-', '3'):
245 return apar->codec_id == AV_CODEC_ID_EAC3;
246 2592 case 0:
247 // Not enhanced flv, continue as normal.
248 2592 break;
249 default:
250 // Unknown FOURCC
251 return 0;
252 }
253
254
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2592 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2592 if (!apar->codec_id && !apar->codec_tag)
255 return 1;
256
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2592 times.
2592 if (apar->bits_per_coded_sample != bits_per_coded_sample)
258 return 0;
259
260
3/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 613 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.
2592 switch (flv_codecid) {
261 // no distinction between S16 and S8 PCM codec flags
262 case FLV_CODECID_PCM:
263 codec_id = bits_per_coded_sample == 8
264 ? AV_CODEC_ID_PCM_U8
265 #if HAVE_BIGENDIAN
266 : AV_CODEC_ID_PCM_S16BE;
267 #else
268 : AV_CODEC_ID_PCM_S16LE;
269 #endif
270 return codec_id == apar->codec_id;
271 case FLV_CODECID_PCM_LE:
272 codec_id = bits_per_coded_sample == 8
273 ? AV_CODEC_ID_PCM_U8
274 : AV_CODEC_ID_PCM_S16LE;
275 return codec_id == apar->codec_id;
276 613 case FLV_CODECID_AAC:
277 613 return apar->codec_id == AV_CODEC_ID_AAC;
278 284 case FLV_CODECID_ADPCM:
279 284 return apar->codec_id == AV_CODEC_ID_ADPCM_SWF;
280 case FLV_CODECID_SPEEX:
281 return apar->codec_id == AV_CODEC_ID_SPEEX;
282 case FLV_CODECID_MP3:
283 return apar->codec_id == AV_CODEC_ID_MP3;
284 1695 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
285 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
286 case FLV_CODECID_NELLYMOSER:
287 1695 return apar->codec_id == AV_CODEC_ID_NELLYMOSER;
288 case FLV_CODECID_PCM_MULAW:
289 return apar->sample_rate == 8000 &&
290 apar->codec_id == AV_CODEC_ID_PCM_MULAW;
291 case FLV_CODECID_PCM_ALAW:
292 return apar->sample_rate == 8000 &&
293 apar->codec_id == AV_CODEC_ID_PCM_ALAW;
294 default:
295 return apar->codec_tag == (flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
296 }
297 }
298
299 2616 static void flv_set_audio_codec(AVFormatContext *s, AVStream *astream,
300 AVCodecParameters *apar, int flv_codecid)
301 {
302
8/18
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 618 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 taken 3 times.
✓ Branch 12 taken 3 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 3 times.
✓ Branch 15 taken 3 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
2616 switch (flv_codecid) {
303 // no distinction between S16 and S8 PCM codec flags
304 case FLV_CODECID_PCM:
305 apar->codec_id = apar->bits_per_coded_sample == 8
306 ? AV_CODEC_ID_PCM_U8
307 #if HAVE_BIGENDIAN
308 : AV_CODEC_ID_PCM_S16BE;
309 #else
310 : AV_CODEC_ID_PCM_S16LE;
311 #endif
312 break;
313 case FLV_CODECID_PCM_LE:
314 apar->codec_id = apar->bits_per_coded_sample == 8
315 ? AV_CODEC_ID_PCM_U8
316 : AV_CODEC_ID_PCM_S16LE;
317 break;
318 618 case FLV_CODECID_AAC:
319 618 apar->codec_id = AV_CODEC_ID_AAC;
320 618 break;
321 288 case FLV_CODECID_ADPCM:
322 288 apar->codec_id = AV_CODEC_ID_ADPCM_SWF;
323 288 break;
324 case FLV_CODECID_SPEEX:
325 apar->codec_id = AV_CODEC_ID_SPEEX;
326 apar->sample_rate = 16000;
327 break;
328 case FLV_CODECID_MP3:
329 apar->codec_id = AV_CODEC_ID_MP3;
330 ffstream(astream)->need_parsing = AVSTREAM_PARSE_FULL;
331 break;
332 case FLV_CODECID_NELLYMOSER_8KHZ_MONO:
333 // in case metadata does not otherwise declare samplerate
334 apar->sample_rate = 8000;
335 apar->codec_id = AV_CODEC_ID_NELLYMOSER;
336 break;
337 376 case FLV_CODECID_NELLYMOSER_16KHZ_MONO:
338 376 apar->sample_rate = 16000;
339 376 apar->codec_id = AV_CODEC_ID_NELLYMOSER;
340 376 break;
341 1322 case FLV_CODECID_NELLYMOSER:
342 1322 apar->codec_id = AV_CODEC_ID_NELLYMOSER;
343 1322 break;
344 case FLV_CODECID_PCM_MULAW:
345 apar->sample_rate = 8000;
346 apar->codec_id = AV_CODEC_ID_PCM_MULAW;
347 break;
348 case FLV_CODECID_PCM_ALAW:
349 apar->sample_rate = 8000;
350 apar->codec_id = AV_CODEC_ID_PCM_ALAW;
351 break;
352 3 case MKBETAG('m', 'p', '4', 'a'):
353 3 apar->codec_id = AV_CODEC_ID_AAC;
354 3 return;
355 3 case MKBETAG('O', 'p', 'u', 's'):
356 3 apar->codec_id = AV_CODEC_ID_OPUS;
357 3 apar->sample_rate = 48000;
358 3 return;
359 case MKBETAG('.', 'm', 'p', '3'):
360 apar->codec_id = AV_CODEC_ID_MP3;
361 return;
362 3 case MKBETAG('f', 'L', 'a', 'C'):
363 3 apar->codec_id = AV_CODEC_ID_FLAC;
364 3 return;
365 3 case MKBETAG('a', 'c', '-', '3'):
366 3 apar->codec_id = AV_CODEC_ID_AC3;
367 3 return;
368 case MKBETAG('e', 'c', '-', '3'):
369 apar->codec_id = AV_CODEC_ID_EAC3;
370 return;
371 default:
372 avpriv_request_sample(s, "Audio codec (%x)",
373 flv_codecid >> FLV_AUDIO_CODECID_OFFSET);
374 apar->codec_tag = flv_codecid >> FLV_AUDIO_CODECID_OFFSET;
375 }
376 }
377
378 3966 static int flv_same_video_codec(AVCodecParameters *vpar, uint32_t flv_codecid)
379 {
380
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3966 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3966 if (!vpar->codec_id && !vpar->codec_tag)
381 return 1;
382
383
8/10
✓ Branch 0 taken 551 times.
✓ Branch 1 taken 330 times.
✓ Branch 2 taken 229 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 1723 times.
✗ Branch 9 not taken.
3966 switch (flv_codecid) {
384 551 case FLV_CODECID_X_HEVC:
385 case MKBETAG('h', 'v', 'c', '1'):
386 551 return vpar->codec_id == AV_CODEC_ID_HEVC;
387 330 case MKBETAG('a', 'v', '0', '1'):
388 330 return vpar->codec_id == AV_CODEC_ID_AV1;
389 229 case MKBETAG('v', 'p', '0', '9'):
390 229 return vpar->codec_id == AV_CODEC_ID_VP9;
391 493 case FLV_CODECID_H263:
392 493 return vpar->codec_id == AV_CODEC_ID_FLV1;
393 271 case FLV_CODECID_SCREEN:
394 271 return vpar->codec_id == AV_CODEC_ID_FLASHSV;
395 196 case FLV_CODECID_SCREEN2:
396 196 return vpar->codec_id == AV_CODEC_ID_FLASHSV2;
397 173 case FLV_CODECID_VP6:
398 173 return vpar->codec_id == AV_CODEC_ID_VP6F;
399 case FLV_CODECID_VP6A:
400 return vpar->codec_id == AV_CODEC_ID_VP6A;
401 1723 case FLV_CODECID_H264:
402 case MKBETAG('a', 'v', 'c', '1'):
403 1723 return vpar->codec_id == AV_CODEC_ID_H264;
404 default:
405 return vpar->codec_tag == flv_codecid;
406 }
407 }
408
409 2651 static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream,
410 uint32_t flv_codecid, int read)
411 {
412 2651 FFStream *const vstreami = ffstream(vstream);
413 2651 int ret = 0;
414 2651 AVCodecParameters *par = vstream->codecpar;
415 2651 enum AVCodecID old_codec_id = vstream->codecpar->codec_id;
416
417
8/12
✓ Branch 0 taken 223 times.
✓ Branch 1 taken 176 times.
✓ Branch 2 taken 122 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 977 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
2651 switch (flv_codecid) {
418 223 case FLV_CODECID_X_HEVC:
419 case MKBETAG('h', 'v', 'c', '1'):
420 223 par->codec_id = AV_CODEC_ID_HEVC;
421 223 vstreami->need_parsing = AVSTREAM_PARSE_HEADERS;
422 223 break;
423 176 case MKBETAG('a', 'v', '0', '1'):
424 176 par->codec_id = AV_CODEC_ID_AV1;
425 176 vstreami->need_parsing = AVSTREAM_PARSE_HEADERS;
426 176 break;
427 122 case MKBETAG('v', 'p', '0', '9'):
428 122 par->codec_id = AV_CODEC_ID_VP9;
429 122 vstreami->need_parsing = AVSTREAM_PARSE_HEADERS;
430 122 break;
431 503 case FLV_CODECID_H263:
432 503 par->codec_id = AV_CODEC_ID_FLV1;
433 503 break;
434 case FLV_CODECID_REALH263:
435 par->codec_id = AV_CODEC_ID_H263;
436 break; // Really mean it this time
437 276 case FLV_CODECID_SCREEN:
438 276 par->codec_id = AV_CODEC_ID_FLASHSV;
439 276 break;
440 200 case FLV_CODECID_SCREEN2:
441 200 par->codec_id = AV_CODEC_ID_FLASHSV2;
442 200 break;
443 174 case FLV_CODECID_VP6:
444 174 par->codec_id = AV_CODEC_ID_VP6F;
445 174 case FLV_CODECID_VP6A:
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 174 times.
174 if (flv_codecid == FLV_CODECID_VP6A)
447 par->codec_id = AV_CODEC_ID_VP6A;
448
1/2
✓ Branch 0 taken 174 times.
✗ Branch 1 not taken.
174 if (read) {
449
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 173 times.
174 if (par->extradata_size != 1) {
450 1 ff_alloc_extradata(par, 1);
451 }
452
1/2
✓ Branch 0 taken 174 times.
✗ Branch 1 not taken.
174 if (par->extradata)
453 174 par->extradata[0] = avio_r8(s->pb);
454 else
455 avio_skip(s->pb, 1);
456 }
457 174 ret = 1; // 1 byte body size adjustment for flv_read_packet()
458 174 break;
459 977 case FLV_CODECID_H264:
460 case MKBETAG('a', 'v', 'c', '1'):
461 977 par->codec_id = AV_CODEC_ID_H264;
462 977 vstreami->need_parsing = AVSTREAM_PARSE_HEADERS;
463 977 break;
464 case FLV_CODECID_MPEG4:
465 par->codec_id = AV_CODEC_ID_MPEG4;
466 break;
467 default:
468 avpriv_request_sample(s, "Video codec (%x)", flv_codecid);
469 par->codec_tag = flv_codecid;
470 }
471
472
3/4
✓ Branch 0 taken 2587 times.
✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2587 times.
2651 if (!vstreami->need_context_update && par->codec_id != old_codec_id) {
473 avpriv_request_sample(s, "Changing the codec id midstream");
474 return AVERROR_PATCHWELCOME;
475 }
476
477 2651 return ret;
478 }
479
480 542 static int amf_get_string(AVIOContext *ioc, char *buffer, int buffsize)
481 {
482 int ret;
483 542 int length = avio_rb16(ioc);
484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 542 times.
542 if (length >= buffsize) {
485 avio_skip(ioc, length);
486 return -1;
487 }
488
489 542 ret = avio_read(ioc, buffer, length);
490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 542 times.
542 if (ret < 0)
491 return ret;
492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 542 times.
542 if (ret < length)
493 return AVERROR_INVALIDDATA;
494
495 542 buffer[length] = '\0';
496
497 542 return length;
498 }
499
500 3 static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, int64_t max_pos)
501 {
502 3 FLVContext *flv = s->priv_data;
503 3 unsigned int timeslen = 0, fileposlen = 0, i;
504 char str_val[256];
505 3 int64_t *times = NULL;
506 3 int64_t *filepositions = NULL;
507 3 int ret = AVERROR(ENOSYS);
508 3 int64_t initial_pos = avio_tell(ioc);
509
510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (flv->keyframe_count > 0) {
511 av_log(s, AV_LOG_DEBUG, "keyframes have been parsed\n");
512 return 0;
513 }
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 av_assert0(!flv->keyframe_times);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 av_assert0(!flv->keyframe_filepositions);
516
517
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->flags & AVFMT_FLAG_IGNIDX)
518 return 0;
519
520
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 &&
521 6 amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
522 int64_t **current_array;
523 unsigned int arraylen;
524 int factor;
525
526 // Expect array object in context
527
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)
528 break;
529
530 6 arraylen = avio_rb32(ioc);
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (arraylen>>28)
532 break;
533
534
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) {
535 3 current_array = &times;
536 3 timeslen = arraylen;
537 3 factor = 1000;
538
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) &&
539
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 !filepositions) {
540 3 current_array = &filepositions;
541 3 fileposlen = arraylen;
542 3 factor = 1;
543 } else
544 // unexpected metatag inside keyframes, will not use such
545 // metadata for indexing
546 break;
547
548
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (!(*current_array = av_mallocz(sizeof(**current_array) * arraylen))) {
549 ret = AVERROR(ENOMEM);
550 goto finish;
551 }
552
553
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++) {
554 double d;
555
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 346 times.
346 if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)
556 goto invalid;
557 346 d = av_int2double(avio_rb64(ioc)) * factor;
558
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)
559 goto invalid;
560
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 346 times.
346 if (avio_feof(ioc))
561 goto invalid;
562 346 current_array[0][i] = d;
563 }
564
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) {
565 // All done, exiting at a position allowing amf_parse_object
566 // to finish parsing the object
567 3 ret = 0;
568 3 break;
569 }
570 }
571
572
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]) {
573
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (i = 0; i < FFMIN(2,fileposlen); i++) {
574 6 flv->validate_index[i].pos = filepositions[i];
575 6 flv->validate_index[i].dts = times[i];
576 6 flv->validate_count = i + 1;
577 }
578 3 flv->keyframe_times = times;
579 3 flv->keyframe_filepositions = filepositions;
580 3 flv->keyframe_count = timeslen;
581 3 times = NULL;
582 3 filepositions = NULL;
583 } else {
584 invalid:
585 av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");
586 }
587
588 3 finish:
589 3 av_freep(&times);
590 3 av_freep(&filepositions);
591 3 avio_seek(ioc, initial_pos, SEEK_SET);
592 3 return ret;
593 }
594
595 786 static int amf_parse_object(AVFormatContext *s, AVStream *astream,
596 AVStream *vstream, const char *key,
597 int64_t max_pos, int depth)
598 {
599 AVCodecParameters *apar, *vpar;
600 786 FLVContext *flv = s->priv_data;
601 AVIOContext *ioc;
602 AMFDataType amf_type;
603 786 FLVMetaVideoColor *meta_video_color = flv->metaVideoColor;
604 char str_val[1024];
605 double num_val;
606 amf_date date;
607
608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 786 times.
786 if (depth > MAX_DEPTH)
609 return AVERROR_PATCHWELCOME;
610
611 786 num_val = 0;
612 786 ioc = s->pb;
613
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 786 times.
786 if (avio_feof(ioc))
614 return AVERROR_EOF;
615 786 amf_type = avio_r8(ioc);
616
617
7/9
✓ Branch 0 taken 655 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 31 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 37 times.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
786 switch (amf_type) {
618 655 case AMF_DATA_TYPE_NUMBER:
619 655 num_val = av_int2double(avio_rb64(ioc));
620 655 break;
621 27 case AMF_DATA_TYPE_BOOL:
622 27 num_val = avio_r8(ioc);
623 27 break;
624 28 case AMF_DATA_TYPE_STRING:
625
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if (amf_get_string(ioc, str_val, sizeof(str_val)) < 0) {
626 av_log(s, AV_LOG_ERROR, "AMF_DATA_TYPE_STRING parsing failed\n");
627 return -1;
628 }
629 28 break;
630 31 case AMF_DATA_TYPE_OBJECT:
631
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (key &&
632
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 (ioc->seekable & AVIO_SEEKABLE_NORMAL) &&
633
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
31 !strcmp(KEYFRAMES_TAG, key) && depth == 1)
634
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (parse_keyframes_index(s, ioc, max_pos) < 0)
635 av_log(s, AV_LOG_ERROR, "Keyframe index parsing failed\n");
636 else
637 3 add_keyframes_index(s);
638
3/4
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 31 times.
140 while (avio_tell(ioc) < max_pos - 2 &&
639 70 amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
640
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if (amf_parse_object(s, astream, vstream, str_val, max_pos,
641 depth + 1) < 0)
642 return -1; // if we couldn't skip, bomb out.
643
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
31 if (avio_r8(ioc) != AMF_END_OF_OBJECT) {
644 av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_OBJECT\n");
645 return -1;
646 }
647 31 break;
648 case AMF_DATA_TYPE_NULL:
649 case AMF_DATA_TYPE_UNDEFINED:
650 case AMF_DATA_TYPE_UNSUPPORTED:
651 break; // these take up no additional space
652 37 case AMF_DATA_TYPE_MIXEDARRAY:
653 {
654 unsigned v;
655 37 avio_skip(ioc, 4); // skip 32-bit max array index
656
3/4
✓ Branch 1 taken 388 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 351 times.
✓ Branch 4 taken 37 times.
776 while (avio_tell(ioc) < max_pos - 2 &&
657 388 amf_get_string(ioc, str_val, sizeof(str_val)) > 0)
658 // this is the only case in which we would want a nested
659 // parse to not skip over the object
660
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 351 times.
351 if (amf_parse_object(s, astream, vstream, str_val, max_pos,
661 depth + 1) < 0)
662 return -1;
663 37 v = avio_r8(ioc);
664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (v != AMF_END_OF_OBJECT) {
665 av_log(s, AV_LOG_ERROR, "Missing AMF_END_OF_OBJECT in AMF_DATA_TYPE_MIXEDARRAY, found %d\n", v);
666 return -1;
667 }
668 37 break;
669 }
670 7 case AMF_DATA_TYPE_ARRAY:
671 {
672 unsigned int arraylen, i;
673
674 7 arraylen = avio_rb32(ioc);
675
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++)
676
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 346 times.
346 if (amf_parse_object(s, NULL, NULL, NULL, max_pos,
677 depth + 1) < 0)
678 return -1; // if we couldn't skip, bomb out.
679 }
680 7 break;
681 1 case AMF_DATA_TYPE_DATE:
682 // timestamp (double) and UTC offset (int16)
683 1 date.milliseconds = av_int2double(avio_rb64(ioc));
684 1 date.timezone = avio_rb16(ioc);
685 1 break;
686 default: // unsupported type, we couldn't skip
687 av_log(s, AV_LOG_ERROR, "unsupported amf type %d\n", amf_type);
688 return -1;
689 }
690
691
2/2
✓ Branch 0 taken 440 times.
✓ Branch 1 taken 346 times.
786 if (key) {
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 440 times.
440 apar = astream ? astream->codecpar : NULL;
693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 440 times.
440 vpar = vstream ? vstream->codecpar : NULL;
694
695 // stream info doesn't live any deeper than the first object
696
2/2
✓ Branch 0 taken 366 times.
✓ Branch 1 taken 74 times.
440 if (depth == 1) {
697
4/4
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 291 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 48 times.
366 if (amf_type == AMF_DATA_TYPE_NUMBER ||
698 amf_type == AMF_DATA_TYPE_BOOL) {
699
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 281 times.
318 if (!strcmp(key, "duration"))
700 37 s->duration = num_val * AV_TIME_BASE;
701
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 250 times.
281 else if (!strcmp(key, "videodatarate") &&
702
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 0 <= (int)(num_val * 1024.0))
703 31 flv->video_bit_rate = num_val * 1024.0;
704
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 238 times.
250 else if (!strcmp(key, "audiodatarate") &&
705
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 0 <= (int)(num_val * 1024.0))
706 12 flv->audio_bit_rate = num_val * 1024.0;
707
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 209 times.
238 else if (!strcmp(key, "framerate")) {
708 29 flv->framerate = av_d2q(num_val, 1000);
709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (vstream)
710 vstream->avg_frame_rate = flv->framerate;
711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 209 times.
209 } else if (flv->trust_metadata) {
712 if (!strcmp(key, "videocodecid") && vpar) {
713 int ret = flv_set_video_codec(s, vstream, num_val, 0);
714 if (ret < 0)
715 return ret;
716 } else if (!strcmp(key, "audiocodecid") && apar) {
717 int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
718 flv_set_audio_codec(s, astream, apar, id);
719 } else if (!strcmp(key, "audiosamplerate") && apar) {
720 apar->sample_rate = num_val;
721 } else if (!strcmp(key, "audiosamplesize") && apar) {
722 apar->bits_per_coded_sample = num_val;
723 } else if (!strcmp(key, "stereo") && apar) {
724 av_channel_layout_default(&apar->ch_layout, num_val + 1);
725 } else if (!strcmp(key, "width") && vpar) {
726 vpar->width = num_val;
727 } else if (!strcmp(key, "height") && vpar) {
728 vpar->height = num_val;
729 } else if (!strcmp(key, "datastream")) {
730 AVStream *st = create_stream(s, AVMEDIA_TYPE_SUBTITLE, 0);
731 if (!st)
732 return AVERROR(ENOMEM);
733 st->codecpar->codec_id = AV_CODEC_ID_TEXT;
734 }
735 }
736 }
737
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 338 times.
366 if (amf_type == AMF_DATA_TYPE_STRING) {
738
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
28 if (!strcmp(key, "encoder")) {
739 4 int version = -1;
740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (1 == sscanf(str_val, "Open Broadcaster Software v0.%d", &version)) {
741 if (version > 0 && version <= 655)
742 flv->broken_sizes = 1;
743 }
744
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 23 times.
24 } else if (!strcmp(key, "metadatacreator")) {
745
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if ( !strcmp (str_val, "MEGA")
746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 || !strncmp(str_val, "FlixEngine", 10))
747 flv->broken_sizes = 1;
748 }
749 }
750 }
751
752
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 394 times.
440 if (meta_video_color) {
753
3/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
46 if (amf_type == AMF_DATA_TYPE_NUMBER ||
754 amf_type == AMF_DATA_TYPE_BOOL) {
755
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
18 if (!strcmp(key, "colorPrimaries")) {
756 2 meta_video_color->primaries = num_val;
757
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 14 times.
16 } else if (!strcmp(key, "transferCharacteristics")) {
758 2 meta_video_color->transfer_characteristics = num_val;
759
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
14 } else if (!strcmp(key, "matrixCoefficients")) {
760 2 meta_video_color->matrix_coefficients = num_val;
761
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
12 } else if (!strcmp(key, "maxFall")) {
762 1 meta_video_color->max_fall = num_val;
763
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 } else if (!strcmp(key, "maxCLL")) {
764 1 meta_video_color->max_cll = num_val;
765
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
10 } else if (!strcmp(key, "redX")) {
766 1 meta_video_color->mastering_meta.r_x = num_val;
767
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 } else if (!strcmp(key, "redY")) {
768 1 meta_video_color->mastering_meta.r_y = num_val;
769
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 } else if (!strcmp(key, "greenX")) {
770 1 meta_video_color->mastering_meta.g_x = num_val;
771
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 } else if (!strcmp(key, "greenY")) {
772 1 meta_video_color->mastering_meta.g_y = num_val;
773
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 } else if (!strcmp(key, "blueX")) {
774 1 meta_video_color->mastering_meta.b_x = num_val;
775
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
5 } else if (!strcmp(key, "blueY")) {
776 1 meta_video_color->mastering_meta.b_y = num_val;
777
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 } else if (!strcmp(key, "whitePointX")) {
778 1 meta_video_color->mastering_meta.white_x = num_val;
779
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 } else if (!strcmp(key, "whitePointY")) {
780 1 meta_video_color->mastering_meta.white_y = num_val;
781
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 } else if (!strcmp(key, "maxLuminance")) {
782 1 meta_video_color->mastering_meta.max_luminance = num_val;
783
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (!strcmp(key, "minLuminance")) {
784 1 meta_video_color->mastering_meta.min_luminance = num_val;
785 }
786 }
787 }
788
789
5/6
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 409 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 23 times.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
440 if (amf_type == AMF_DATA_TYPE_OBJECT && s->nb_streams == 1 &&
790
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")) ||
791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 (!vpar && !strcmp(key, "videocodecid"))))
792 s->ctx_flags &= ~AVFMTCTX_NOHEADER; //If there is either audio/video missing, codecid will be an empty object
793
794
2/2
✓ Branch 0 taken 403 times.
✓ Branch 1 taken 37 times.
440 if ((!strcmp(key, "duration") ||
795
2/2
✓ Branch 0 taken 367 times.
✓ Branch 1 taken 36 times.
403 !strcmp(key, "filesize") ||
796
2/2
✓ Branch 0 taken 336 times.
✓ Branch 1 taken 31 times.
367 !strcmp(key, "width") ||
797
2/2
✓ Branch 0 taken 305 times.
✓ Branch 1 taken 31 times.
336 !strcmp(key, "height") ||
798
2/2
✓ Branch 0 taken 274 times.
✓ Branch 1 taken 31 times.
305 !strcmp(key, "videodatarate") ||
799
2/2
✓ Branch 0 taken 245 times.
✓ Branch 1 taken 29 times.
274 !strcmp(key, "framerate") ||
800
2/2
✓ Branch 0 taken 214 times.
✓ Branch 1 taken 31 times.
245 !strcmp(key, "videocodecid") ||
801
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 12 times.
214 !strcmp(key, "audiodatarate") ||
802
2/2
✓ Branch 0 taken 191 times.
✓ Branch 1 taken 11 times.
202 !strcmp(key, "audiosamplerate") ||
803
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 11 times.
191 !strcmp(key, "audiosamplesize") ||
804
2/2
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 11 times.
180 !strcmp(key, "stereo") ||
805
2/2
✓ Branch 0 taken 157 times.
✓ Branch 1 taken 12 times.
169 !strcmp(key, "audiocodecid") ||
806
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 157 times.
✓ Branch 2 taken 283 times.
✗ Branch 3 not taken.
440 !strcmp(key, "datastream")) && !flv->dump_full_metadata)
807 283 return 0;
808
809 157 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
810
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 141 times.
157 if (amf_type == AMF_DATA_TYPE_BOOL) {
811
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 av_strlcpy(str_val, num_val > 0 ? "true" : "false",
812 sizeof(str_val));
813 16 av_dict_set(&s->metadata, key, str_val, 0);
814
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 104 times.
141 } else if (amf_type == AMF_DATA_TYPE_NUMBER) {
815 37 snprintf(str_val, sizeof(str_val), "%.f", num_val);
816 37 av_dict_set(&s->metadata, key, str_val, 0);
817
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 76 times.
104 } else if (amf_type == AMF_DATA_TYPE_STRING) {
818 28 av_dict_set(&s->metadata, key, str_val, 0);
819
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 75 times.
76 } else if ( amf_type == AMF_DATA_TYPE_DATE
820
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 && isfinite(date.milliseconds)
821
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 && date.milliseconds > INT64_MIN/1000
822
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 && date.milliseconds < INT64_MAX/1000
823 ) {
824 // timezone is ignored, since there is no easy way to offset the UTC
825 // timestamp into the specified timezone
826 1 avpriv_dict_set_timestamp(&s->metadata, key, 1000 * (int64_t)date.milliseconds);
827 }
828 }
829
830 503 return 0;
831 }
832
833 #define TYPE_ONTEXTDATA 1
834 #define TYPE_ONCAPTION 2
835 #define TYPE_ONCAPTIONINFO 3
836 #define TYPE_UNKNOWN 9
837
838 37 static int flv_read_metabody(AVFormatContext *s, int64_t next_pos)
839 {
840 37 FLVContext *flv = s->priv_data;
841 AMFDataType type;
842 AVStream *stream, *astream, *vstream;
843 AVStream av_unused *dstream;
844 AVIOContext *ioc;
845 int i;
846 char buffer[32];
847
848 37 astream = NULL;
849 37 vstream = NULL;
850 37 dstream = NULL;
851 37 ioc = s->pb;
852
853 // first object needs to be "onMetaData" string
854 37 type = avio_r8(ioc);
855
2/4
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37 times.
74 if (type != AMF_DATA_TYPE_STRING ||
856 37 amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
857 return TYPE_UNKNOWN;
858
859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (!strcmp(buffer, "onTextData"))
860 return TYPE_ONTEXTDATA;
861
862
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (!strcmp(buffer, "onCaption"))
863 return TYPE_ONCAPTION;
864
865
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (!strcmp(buffer, "onCaptionInfo"))
866 return TYPE_ONCAPTIONINFO;
867
868
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
37 if (strcmp(buffer, "onMetaData") && strcmp(buffer, "onCuePoint") && strcmp(buffer, "|RtmpSampleAccess")) {
869 av_log(s, AV_LOG_DEBUG, "Unknown type %s\n", buffer);
870 return TYPE_UNKNOWN;
871 }
872
873 // find the streams now so that amf_parse_object doesn't need to do
874 // the lookup every time it is called.
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 for (i = 0; i < s->nb_streams; i++) {
876 stream = s->streams[i];
877 if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
878 vstream = stream;
879 flv->last_keyframe_stream_index = i;
880 } else if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
881 astream = stream;
882 if (flv->last_keyframe_stream_index == -1)
883 flv->last_keyframe_stream_index = i;
884 } else if (stream->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
885 dstream = stream;
886 }
887
888 // parse the second object (we want a mixed array)
889
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 if (amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)
890 return -1;
891
892 37 return 0;
893 }
894
895 37 static int flv_read_header(AVFormatContext *s)
896 {
897 37 FFFormatContext *const si = ffformatcontext(s);
898 int flags;
899 37 FLVContext *flv = s->priv_data;
900 int offset;
901 37 int pre_tag_size = 0;
902
903 /* Actual FLV data at 0xe40000 in KUX file */
904
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if(!strcmp(s->iformat->name, "kux"))
905 avio_skip(s->pb, 0xe40000);
906
907 37 avio_skip(s->pb, 4);
908 37 flags = avio_r8(s->pb);
909
910 37 si->missing_streams = flags & (FLV_HEADER_FLAG_HASVIDEO | FLV_HEADER_FLAG_HASAUDIO);
911
912 37 s->ctx_flags |= AVFMTCTX_NOHEADER;
913
914 37 offset = avio_rb32(s->pb);
915 37 avio_seek(s->pb, offset, SEEK_SET);
916
917 /* Annex E. The FLV File Format
918 * E.3 TheFLVFileBody
919 * Field Type Comment
920 * PreviousTagSize0 UI32 Always 0
921 * */
922 37 pre_tag_size = avio_rb32(s->pb);
923
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (pre_tag_size) {
924 av_log(s, AV_LOG_WARNING, "Read FLV header error, input file is not a standard flv format, first PreviousTagSize0 always is 0\n");
925 }
926
927 37 s->start_time = 0;
928 37 flv->sum_flv_tag_size = 0;
929 37 flv->last_keyframe_stream_index = -1;
930
931 37 return 0;
932 }
933
934 37 static int flv_read_close(AVFormatContext *s)
935 {
936 int i;
937 37 FLVContext *flv = s->priv_data;
938
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 37 times.
185 for (i = 0; i < FLV_STREAM_TYPE_NB; i++)
939 148 av_freep(&flv->new_extradata[i]);
940
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 37 times.
40 for (i = 0; i < flv->mt_extradata_cnt; i++)
941 3 av_freep(&flv->mt_extradata[i]);
942 37 av_freep(&flv->mt_extradata);
943 37 av_freep(&flv->mt_extradata_sz);
944 37 av_freep(&flv->keyframe_times);
945 37 av_freep(&flv->keyframe_filepositions);
946 37 av_freep(&flv->metaVideoColor);
947 37 return 0;
948 }
949
950 37 static int flv_get_extradata(AVFormatContext *s, AVStream *st, int size)
951 {
952 int ret;
953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (!size)
954 return 0;
955
956
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 if ((ret = ff_get_extradata(s, st->codecpar, s->pb, size)) < 0)
957 return ret;
958 37 ffstream(st)->need_context_update = 1;
959 37 return 0;
960 }
961
962 3 static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
963 int size, int multitrack)
964 {
965
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!size)
966 return 0;
967
968
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (!multitrack) {
969 2 av_free(flv->new_extradata[stream]);
970 2 flv->new_extradata[stream] = av_mallocz(size +
971 AV_INPUT_BUFFER_PADDING_SIZE);
972
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!flv->new_extradata[stream])
973 return AVERROR(ENOMEM);
974 2 flv->new_extradata_size[stream] = size;
975 2 avio_read(pb, flv->new_extradata[stream], size);
976 } else {
977 1 int new_count = stream + 1;
978
979
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (flv->mt_extradata_cnt < new_count) {
980 1 void *tmp = av_realloc_array(flv->mt_extradata, new_count,
981 sizeof(*flv->mt_extradata));
982
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!tmp)
983 return AVERROR(ENOMEM);
984 1 flv->mt_extradata = tmp;
985
986 1 tmp = av_realloc_array(flv->mt_extradata_sz, new_count,
987 sizeof(*flv->mt_extradata_sz));
988
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!tmp)
989 return AVERROR(ENOMEM);
990 1 flv->mt_extradata_sz = tmp;
991
992 // Set newly allocated pointers/sizes to 0
993
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (int i = flv->mt_extradata_cnt; i < new_count; i++) {
994 3 flv->mt_extradata[i] = NULL;
995 3 flv->mt_extradata_sz[i] = 0;
996 }
997 1 flv->mt_extradata_cnt = new_count;
998 }
999
1000 1 av_free(flv->mt_extradata[stream]);
1001 1 flv->mt_extradata[stream] = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
1002
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!flv->mt_extradata[stream])
1003 return AVERROR(ENOMEM);
1004 1 flv->mt_extradata_sz[stream] = size;
1005 1 avio_read(pb, flv->mt_extradata[stream], size);
1006 }
1007
1008 3 return 0;
1009 }
1010
1011 1 static void clear_index_entries(AVFormatContext *s, int64_t pos)
1012 {
1013 1 av_log(s, AV_LOG_WARNING,
1014 "Found invalid index entries, clearing the index.\n");
1015
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (unsigned i = 0; i < s->nb_streams; i++) {
1016 1 FFStream *const sti = ffstream(s->streams[i]);
1017 1 int out = 0;
1018 /* Remove all index entries that point to >= pos */
1019
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 1 times.
133 for (int j = 0; j < sti->nb_index_entries; j++)
1020
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 131 times.
132 if (sti->index_entries[j].pos < pos)
1021 1 sti->index_entries[out++] = sti->index_entries[j];
1022 1 sti->nb_index_entries = out;
1023 }
1024 1 }
1025
1026 static int amf_skip_tag(AVIOContext *pb, AMFDataType type, int depth)
1027 {
1028 int nb = -1, ret, parse_name = 1;
1029
1030 if (depth > MAX_DEPTH)
1031 return AVERROR_PATCHWELCOME;
1032
1033 if (avio_feof(pb))
1034 return AVERROR_EOF;
1035
1036 switch (type) {
1037 case AMF_DATA_TYPE_NUMBER:
1038 avio_skip(pb, 8);
1039 break;
1040 case AMF_DATA_TYPE_BOOL:
1041 avio_skip(pb, 1);
1042 break;
1043 case AMF_DATA_TYPE_STRING:
1044 avio_skip(pb, avio_rb16(pb));
1045 break;
1046 case AMF_DATA_TYPE_ARRAY:
1047 parse_name = 0;
1048 case AMF_DATA_TYPE_MIXEDARRAY:
1049 nb = avio_rb32(pb);
1050 if (nb < 0)
1051 return AVERROR_INVALIDDATA;
1052 case AMF_DATA_TYPE_OBJECT:
1053 while(!pb->eof_reached && (nb-- > 0 || type != AMF_DATA_TYPE_ARRAY)) {
1054 if (parse_name) {
1055 int size = avio_rb16(pb);
1056 if (!size) {
1057 avio_skip(pb, 1);
1058 break;
1059 }
1060 avio_skip(pb, size);
1061 }
1062 if ((ret = amf_skip_tag(pb, avio_r8(pb), depth + 1)) < 0)
1063 return ret;
1064 }
1065 break;
1066 case AMF_DATA_TYPE_NULL:
1067 case AMF_DATA_TYPE_OBJECT_END:
1068 break;
1069 default:
1070 return AVERROR_INVALIDDATA;
1071 }
1072 return 0;
1073 }
1074
1075 static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
1076 int64_t dts, int64_t next)
1077 {
1078 AVIOContext *pb = s->pb;
1079 AVStream *st = NULL;
1080 char buf[20];
1081 int ret = AVERROR_INVALIDDATA;
1082 int i, length = -1;
1083 int array = 0;
1084
1085 switch (avio_r8(pb)) {
1086 case AMF_DATA_TYPE_ARRAY:
1087 array = 1;
1088 case AMF_DATA_TYPE_MIXEDARRAY:
1089 avio_seek(pb, 4, SEEK_CUR);
1090 case AMF_DATA_TYPE_OBJECT:
1091 break;
1092 default:
1093 goto skip;
1094 }
1095
1096 while (array || (ret = amf_get_string(pb, buf, sizeof(buf))) > 0) {
1097 AMFDataType type = avio_r8(pb);
1098 if (type == AMF_DATA_TYPE_STRING && (array || !strcmp(buf, "text"))) {
1099 length = avio_rb16(pb);
1100 ret = av_get_packet(pb, pkt, length);
1101 if (ret < 0)
1102 goto skip;
1103 else
1104 break;
1105 } else {
1106 if ((ret = amf_skip_tag(pb, type, 0)) < 0)
1107 goto skip;
1108 }
1109 }
1110
1111 if (length < 0) {
1112 ret = AVERROR_INVALIDDATA;
1113 goto skip;
1114 }
1115
1116 for (i = 0; i < s->nb_streams; i++) {
1117 st = s->streams[i];
1118 if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
1119 break;
1120 }
1121
1122 if (i == s->nb_streams) {
1123 st = create_stream(s, AVMEDIA_TYPE_SUBTITLE, 0);
1124 if (!st)
1125 return AVERROR(ENOMEM);
1126 st->codecpar->codec_id = AV_CODEC_ID_TEXT;
1127 }
1128
1129 pkt->dts = dts;
1130 pkt->pts = dts;
1131 pkt->size = ret;
1132
1133 pkt->stream_index = st->index;
1134 pkt->flags |= AV_PKT_FLAG_KEY;
1135
1136 skip:
1137 avio_seek(s->pb, next + 4, SEEK_SET);
1138
1139 return ret;
1140 }
1141
1142 static int resync(AVFormatContext *s)
1143 {
1144 FLVContext *flv = s->priv_data;
1145 int64_t i;
1146 int64_t pos = avio_tell(s->pb);
1147
1148 for (i=0; !avio_feof(s->pb); i++) {
1149 int j = i & (RESYNC_BUFFER_SIZE-1);
1150 int j1 = j + RESYNC_BUFFER_SIZE;
1151 flv->resync_buffer[j ] =
1152 flv->resync_buffer[j1] = avio_r8(s->pb);
1153
1154 if (i >= 8 && pos) {
1155 uint8_t *d = flv->resync_buffer + j1 - 8;
1156 if (d[0] == 'F' &&
1157 d[1] == 'L' &&
1158 d[2] == 'V' &&
1159 d[3] < 5 && d[5] == 0) {
1160 av_log(s, AV_LOG_WARNING, "Concatenated FLV detected, might fail to demux, decode and seek %"PRId64"\n", flv->last_ts);
1161 flv->time_offset = flv->last_ts + 1;
1162 flv->time_pos = avio_tell(s->pb);
1163 }
1164 }
1165
1166 if (i > 22) {
1167 unsigned lsize2 = AV_RB32(flv->resync_buffer + j1 - 4);
1168 if (lsize2 >= 11 && lsize2 + 8LL < FFMIN(i, RESYNC_BUFFER_SIZE)) {
1169 unsigned size2 = AV_RB24(flv->resync_buffer + j1 - lsize2 + 1 - 4);
1170 unsigned lsize1 = AV_RB32(flv->resync_buffer + j1 - lsize2 - 8);
1171 if (lsize1 >= 11 && lsize1 + 8LL + lsize2 < FFMIN(i, RESYNC_BUFFER_SIZE)) {
1172 unsigned size1 = AV_RB24(flv->resync_buffer + j1 - lsize1 + 1 - lsize2 - 8);
1173 if (size1 == lsize1 - 11 && size2 == lsize2 - 11) {
1174 avio_seek(s->pb, pos + i - lsize1 - lsize2 - 8, SEEK_SET);
1175 return 1;
1176 }
1177 }
1178 }
1179 }
1180 }
1181 return AVERROR_EOF;
1182 }
1183
1184 13 static int flv_parse_video_color_info(AVFormatContext *s, AVStream *st, int64_t next_pos)
1185 {
1186 13 FLVContext *flv = s->priv_data;
1187 AMFDataType type;
1188 AVIOContext *ioc;
1189 char buffer[32];
1190 13 ioc = s->pb;
1191
1192 // first object needs to be "colorInfo" string
1193 13 type = avio_r8(ioc);
1194
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
26 if (type != AMF_DATA_TYPE_STRING ||
1195 13 amf_get_string(ioc, buffer, sizeof(buffer)) < 0)
1196 return TYPE_UNKNOWN;
1197
1198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (strcmp(buffer, "colorInfo")) {
1199 av_log(s, AV_LOG_DEBUG, "Unknown type %s\n", buffer);
1200 return TYPE_UNKNOWN;
1201 }
1202
1203 13 av_free(flv->metaVideoColor);
1204
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
13 if (!(flv->metaVideoColor = av_mallocz(sizeof(FLVMetaVideoColor)))) {
1205 return AVERROR(ENOMEM);
1206 }
1207 13 flv->meta_color_info_flag = 1;
1208 13 amf_parse_object(s, NULL, NULL, buffer, next_pos, 0); // parse metadata
1209 13 return 0;
1210 }
1211
1212 13 static int flv_update_video_color_info(AVFormatContext *s, AVStream *st)
1213 {
1214 13 FLVContext *flv = s->priv_data;
1215 13 const FLVMetaVideoColor* meta_video_color = flv->metaVideoColor;
1216 13 const FLVMasteringMeta *mastering_meta = &meta_video_color->mastering_meta;
1217
1218 int has_mastering_primaries, has_mastering_luminance;
1219 // Mastering primaries are CIE 1931 coords, and must be > 0.
1220 13 has_mastering_primaries =
1221
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 mastering_meta->r_x > 0 && mastering_meta->r_y > 0 &&
1222
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 &&
1223
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 &&
1224
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
14 mastering_meta->white_x > 0 && mastering_meta->white_y > 0;
1225
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
13 has_mastering_luminance = mastering_meta->max_luminance > 0 && mastering_meta->min_luminance > 0;
1226
1227
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (meta_video_color->matrix_coefficients != AVCOL_SPC_RESERVED)
1228 13 st->codecpar->color_space = meta_video_color->matrix_coefficients;
1229
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (meta_video_color->primaries != AVCOL_PRI_RESERVED &&
1230
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11 times.
13 meta_video_color->primaries != AVCOL_PRI_RESERVED0)
1231 2 st->codecpar->color_primaries = meta_video_color->primaries;
1232
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (meta_video_color->transfer_characteristics != AVCOL_TRC_RESERVED &&
1233
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11 times.
13 meta_video_color->transfer_characteristics != AVCOL_TRC_RESERVED0)
1234 2 st->codecpar->color_trc = meta_video_color->transfer_characteristics;
1235
1236
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
13 if (meta_video_color->max_cll && meta_video_color->max_fall) {
1237 1 size_t size = 0;
1238 1 AVContentLightMetadata *metadata = av_content_light_metadata_alloc(&size);
1239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!metadata)
1240 return AVERROR(ENOMEM);
1241
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,
1242 AV_PKT_DATA_CONTENT_LIGHT_LEVEL, metadata, size, 0)) {
1243 av_freep(&metadata);
1244 return AVERROR(ENOMEM);
1245 }
1246 1 metadata->MaxCLL = meta_video_color->max_cll;
1247 1 metadata->MaxFALL = meta_video_color->max_fall;
1248 }
1249
1250
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
13 if (has_mastering_primaries || has_mastering_luminance) {
1251 AVMasteringDisplayMetadata *metadata;
1252 1 AVPacketSideData *sd = av_packet_side_data_new(&st->codecpar->coded_side_data,
1253 1 &st->codecpar->nb_coded_side_data,
1254 AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
1255 sizeof(AVMasteringDisplayMetadata), 0);
1256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!sd)
1257 return AVERROR(ENOMEM);
1258 1 metadata = (AVMasteringDisplayMetadata*)sd->data;
1259 1 memset(metadata, 0, sizeof(AVMasteringDisplayMetadata));
1260 // hdrCll
1261
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (has_mastering_luminance) {
1262 1 metadata->max_luminance = av_d2q(mastering_meta->max_luminance, INT_MAX);
1263 1 metadata->min_luminance = av_d2q(mastering_meta->min_luminance, INT_MAX);
1264 1 metadata->has_luminance = 1;
1265 }
1266 // hdrMdcv
1267
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (has_mastering_primaries) {
1268 1 metadata->display_primaries[0][0] = av_d2q(mastering_meta->r_x, INT_MAX);
1269 1 metadata->display_primaries[0][1] = av_d2q(mastering_meta->r_y, INT_MAX);
1270 1 metadata->display_primaries[1][0] = av_d2q(mastering_meta->g_x, INT_MAX);
1271 1 metadata->display_primaries[1][1] = av_d2q(mastering_meta->g_y, INT_MAX);
1272 1 metadata->display_primaries[2][0] = av_d2q(mastering_meta->b_x, INT_MAX);
1273 1 metadata->display_primaries[2][1] = av_d2q(mastering_meta->b_y, INT_MAX);
1274 1 metadata->white_point[0] = av_d2q(mastering_meta->white_x, INT_MAX);
1275 1 metadata->white_point[1] = av_d2q(mastering_meta->white_y, INT_MAX);
1276 1 metadata->has_primaries = 1;
1277 }
1278 }
1279 13 return 0;
1280 }
1281
1282 static int flv_parse_mod_ex_data(AVFormatContext *s, int *pkt_type, int *size, int64_t *dts)
1283 {
1284 int ex_type, ret;
1285 uint8_t *ex_data;
1286
1287 int ex_size = (uint8_t)avio_r8(s->pb) + 1;
1288 *size -= 1;
1289
1290 if (ex_size == 256) {
1291 ex_size = (uint16_t)avio_rb16(s->pb) + 1;
1292 *size -= 2;
1293 }
1294
1295 if (ex_size >= *size) {
1296 av_log(s, AV_LOG_WARNING, "ModEx size larger than remaining data!\n");
1297 return AVERROR(EINVAL);
1298 }
1299
1300 ex_data = av_malloc(ex_size);
1301 if (!ex_data)
1302 return AVERROR(ENOMEM);
1303
1304 ret = avio_read(s->pb, ex_data, ex_size);
1305 if (ret < 0) {
1306 av_free(ex_data);
1307 return ret;
1308 }
1309 *size -= ex_size;
1310
1311 ex_type = (uint8_t)avio_r8(s->pb);
1312 *size -= 1;
1313
1314 *pkt_type = ex_type & 0x0f;
1315 ex_type &= 0xf0;
1316
1317 if (ex_type == PacketModExTypeTimestampOffsetNano) {
1318 uint32_t nano_offset;
1319
1320 if (ex_size != 3) {
1321 av_log(s, AV_LOG_WARNING, "Invalid ModEx size for Type TimestampOffsetNano!\n");
1322 nano_offset = 0;
1323 } else {
1324 nano_offset = (ex_data[0] << 16) | (ex_data[1] << 8) | ex_data[2];
1325 }
1326
1327 // this is not likely to ever add anything, but right now timestamps are with ms precision
1328 *dts += nano_offset / 1000000;
1329 } else {
1330 av_log(s, AV_LOG_INFO, "Unknown ModEx type: %d", ex_type);
1331 }
1332
1333 av_free(ex_data);
1334
1335 return 0;
1336 }
1337
1338 6328 static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
1339 {
1340 6328 FLVContext *flv = s->priv_data;
1341 6328 int ret = AVERROR_BUG, i, size, flags;
1342 6328 int res = 0;
1343 enum FlvTagType type;
1344 6328 int stream_type = -1;
1345 int64_t next, pos, meta_pos;
1346 6328 int64_t dts, pts = AV_NOPTS_VALUE;
1347 6328 int av_uninit(channels);
1348 6328 int av_uninit(sample_rate);
1349 6328 AVStream *st = NULL;
1350 6328 int last = -1;
1351 int orig_size;
1352 6328 int enhanced_flv = 0;
1353 6328 int multitrack = 0;
1354 6328 int pkt_type = 0;
1355 6328 uint8_t track_idx = 0;
1356 6328 uint32_t codec_id = 0;
1357 6328 int multitrack_type = MultitrackTypeOneTrack;
1358
1359 6328 retry:
1360 /* pkt size is repeated at end. skip it */
1361 6328 pos = avio_tell(s->pb);
1362 6328 type = (avio_r8(s->pb) & 0x1F);
1363 6328 orig_size =
1364 6328 size = avio_rb24(s->pb);
1365 6328 flv->sum_flv_tag_size += size + 11LL;
1366 6328 dts = avio_rb24(s->pb);
1367 6328 dts |= (unsigned)avio_r8(s->pb) << 24;
1368 6328 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));
1369
2/2
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 6256 times.
6328 if (avio_feof(s->pb))
1370 72 return AVERROR_EOF;
1371 6256 avio_skip(s->pb, 3); /* stream id, always 0 */
1372 6256 flags = 0;
1373
1374
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 6237 times.
6256 if (flv->validate_next < flv->validate_count) {
1375 19 int64_t validate_pos = flv->validate_index[flv->validate_next].pos;
1376
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14 times.
19 if (pos == validate_pos) {
1377
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) <=
1378 VALIDATE_INDEX_TS_THRESH) {
1379 5 flv->validate_next++;
1380 } else {
1381 clear_index_entries(s, validate_pos);
1382 flv->validate_count = 0;
1383 }
1384
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
14 } else if (pos > validate_pos) {
1385 1 clear_index_entries(s, validate_pos);
1386 1 flv->validate_count = 0;
1387 }
1388 }
1389
1390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6256 times.
6256 if (size == 0) {
1391 ret = FFERROR_REDO;
1392 goto leave;
1393 }
1394
1395 6256 next = size + avio_tell(s->pb);
1396
1397
2/2
✓ Branch 0 taken 3267 times.
✓ Branch 1 taken 2989 times.
6256 if (type == FLV_TAG_TYPE_AUDIO) {
1398 3267 stream_type = FLV_STREAM_TYPE_AUDIO;
1399 3267 flags = avio_r8(s->pb);
1400 3267 size--;
1401
1402
2/2
✓ Branch 0 taken 663 times.
✓ Branch 1 taken 2604 times.
3267 if ((flags & FLV_AUDIO_CODECID_MASK) == FLV_CODECID_EX_HEADER) {
1403 663 enhanced_flv = 1;
1404 663 pkt_type = flags & ~FLV_AUDIO_CODECID_MASK;
1405
1406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 663 times.
663 while (pkt_type == PacketTypeModEx) {
1407 ret = flv_parse_mod_ex_data(s, &pkt_type, &size, &dts);
1408 if (ret < 0)
1409 goto leave;
1410 }
1411
1412
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 3 times.
663 if (pkt_type == AudioPacketTypeMultitrack) {
1413 660 uint8_t types = avio_r8(s->pb);
1414 660 multitrack_type = types & 0xF0;
1415 660 pkt_type = types & 0xF;
1416
1417 660 multitrack = 1;
1418 660 size--;
1419 }
1420
1421 663 codec_id = avio_rb32(s->pb);
1422 663 size -= 4;
1423
1424
2/2
✓ Branch 0 taken 660 times.
✓ Branch 1 taken 3 times.
663 if (multitrack) {
1425 660 track_idx = avio_r8(s->pb);
1426 660 size--;
1427 }
1428 }
1429
2/2
✓ Branch 0 taken 2952 times.
✓ Branch 1 taken 37 times.
2989 } else if (type == FLV_TAG_TYPE_VIDEO) {
1430 2952 stream_type = FLV_STREAM_TYPE_VIDEO;
1431 2952 flags = avio_r8(s->pb);
1432 2952 codec_id = flags & FLV_VIDEO_CODECID_MASK;
1433 /*
1434 * Reference Enhancing FLV 2023-03-v1.0.0-B.8
1435 * https://github.com/veovera/enhanced-rtmp/blob/main/enhanced-rtmp-v1.pdf
1436 * */
1437 2952 enhanced_flv = (flags >> 7) & 1;
1438
2/2
✓ Branch 0 taken 651 times.
✓ Branch 1 taken 2301 times.
2952 pkt_type = enhanced_flv ? codec_id : 0;
1439 2952 size--;
1440
1441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2952 times.
2952 while (pkt_type == PacketTypeModEx) {
1442 ret = flv_parse_mod_ex_data(s, &pkt_type, &size, &dts);
1443 if (ret < 0)
1444 goto leave;
1445 }
1446
1447
4/4
✓ Branch 0 taken 651 times.
✓ Branch 1 taken 2301 times.
✓ Branch 2 taken 638 times.
✓ Branch 3 taken 13 times.
2952 if (enhanced_flv && pkt_type != PacketTypeMetadata &&
1448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 638 times.
638 (flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_VIDEO_INFO_CMD)
1449 goto skip;
1450
1451
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 2502 times.
2952 if (pkt_type == PacketTypeMultitrack) {
1452 450 uint8_t types = avio_r8(s->pb);
1453 450 multitrack_type = types & 0xF0;
1454 450 pkt_type = types & 0xF;
1455
1456 450 multitrack = 1;
1457 450 size--;
1458 }
1459
1460
2/2
✓ Branch 0 taken 651 times.
✓ Branch 1 taken 2301 times.
2952 if (enhanced_flv) {
1461 651 codec_id = avio_rb32(s->pb);
1462 651 size -= 4;
1463 }
1464
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 2502 times.
2952 if (multitrack) {
1465 450 track_idx = avio_r8(s->pb);
1466 450 size--;
1467 }
1468
1469
4/4
✓ Branch 0 taken 651 times.
✓ Branch 1 taken 2301 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 638 times.
2952 if (enhanced_flv && (flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_VIDEO_INFO_CMD) {
1470
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (pkt_type == PacketTypeMetadata) {
1471 13 int sret = flv_parse_video_color_info(s, st, next);
1472 13 av_log(s, AV_LOG_DEBUG, "enhanced flv parse metadata ret %d and skip\n", sret);
1473 }
1474 13 goto skip;
1475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2939 times.
2939 } else if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_VIDEO_INFO_CMD) {
1476 goto skip;
1477 }
1478
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 } else if (type == FLV_TAG_TYPE_META) {
1479 37 stream_type=FLV_STREAM_TYPE_SUBTITLE;
1480
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 if (size > 13 + 1 + 4) { // Header-type metadata stuff
1481 int type;
1482 37 meta_pos = avio_tell(s->pb);
1483 37 type = flv_read_metabody(s, next);
1484
2/6
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
37 if (type == 0 && dts == 0 || type < 0) {
1485
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
37 if (type < 0 && flv->validate_count &&
1486 flv->validate_index[0].pos > next &&
1487 flv->validate_index[0].pos - 4 < next) {
1488 av_log(s, AV_LOG_WARNING, "Adjusting next position due to index mismatch\n");
1489 next = flv->validate_index[0].pos - 4;
1490 }
1491 37 goto skip;
1492 } else if (type == TYPE_ONTEXTDATA) {
1493 avpriv_request_sample(s, "OnTextData packet");
1494 return flv_data_packet(s, pkt, dts, next);
1495 } else if (type == TYPE_ONCAPTION) {
1496 return flv_data_packet(s, pkt, dts, next);
1497 } else if (type == TYPE_UNKNOWN) {
1498 stream_type = FLV_STREAM_TYPE_DATA;
1499 }
1500 avio_seek(s->pb, meta_pos, SEEK_SET);
1501 }
1502 } else {
1503 av_log(s, AV_LOG_DEBUG,
1504 "Skipping flv packet: type %d, size %d, flags %d.\n",
1505 type, size, flags);
1506 50 skip:
1507
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
50 if (avio_seek(s->pb, next, SEEK_SET) != next) {
1508 // This can happen if flv_read_metabody above read past
1509 // next, on a non-seekable input, and the preceding data has
1510 // been flushed out from the IO buffer.
1511 av_log(s, AV_LOG_ERROR, "Unable to seek to the next packet\n");
1512 return AVERROR_INVALIDDATA;
1513 }
1514 50 ret = FFERROR_REDO;
1515 50 goto leave;
1516 }
1517
1518 /* skip empty data packets */
1519
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6202 times.
6206 if (!size) {
1520 4 ret = FFERROR_REDO;
1521 4 goto leave;
1522 }
1523
1524 for (;;) {
1525 6202 int track_size = size;
1526
1527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6202 times.
6202 if (multitrack_type != MultitrackTypeOneTrack) {
1528 track_size = avio_rb24(s->pb);
1529 size -= 3;
1530 }
1531
1532 /* now find stream */
1533
2/2
✓ Branch 0 taken 13967 times.
✓ Branch 1 taken 67 times.
14034 for (i = 0; i < s->nb_streams; i++) {
1534 13967 st = s->streams[i];
1535
2/2
✓ Branch 0 taken 8803 times.
✓ Branch 1 taken 5164 times.
13967 if (stream_type == FLV_STREAM_TYPE_AUDIO) {
1536
2/2
✓ Branch 0 taken 4905 times.
✓ Branch 1 taken 3898 times.
8803 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
1537
3/4
✓ Branch 0 taken 4905 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3461 times.
✓ Branch 4 taken 1444 times.
4905 (s->audio_codec_id || flv_same_audio_codec(st->codecpar, flags, codec_id)) &&
1538
2/2
✓ Branch 0 taken 3239 times.
✓ Branch 1 taken 222 times.
3461 st->id == track_idx)
1539 3239 break;
1540
1/2
✓ Branch 0 taken 5164 times.
✗ Branch 1 not taken.
5164 } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
1541
2/2
✓ Branch 0 taken 4030 times.
✓ Branch 1 taken 1134 times.
5164 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1542
4/4
✓ Branch 0 taken 3966 times.
✓ Branch 1 taken 64 times.
✓ Branch 3 taken 2949 times.
✓ Branch 4 taken 1017 times.
4030 (s->video_codec_id || flv_same_video_codec(st->codecpar, codec_id)) &&
1543
2/2
✓ Branch 0 taken 2896 times.
✓ Branch 1 taken 117 times.
3013 st->id == track_idx)
1544 2896 break;
1545 } else if (stream_type == FLV_STREAM_TYPE_SUBTITLE) {
1546 if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE)
1547 break;
1548 } else if (stream_type == FLV_STREAM_TYPE_DATA) {
1549 if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
1550 break;
1551 }
1552 }
1553
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 6135 times.
6202 if (i == s->nb_streams) {
1554 static const enum AVMediaType stream_types[] = {AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_SUBTITLE, AVMEDIA_TYPE_DATA};
1555 67 st = create_stream(s, stream_types[stream_type], track_idx);
1556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if (!st)
1557 return AVERROR(ENOMEM);
1558 }
1559 6202 av_log(s, AV_LOG_TRACE, "%d %X %d \n", stream_type, flags, st->discard);
1560
1561
1/2
✓ Branch 0 taken 6202 times.
✗ Branch 1 not taken.
6202 if (flv->time_pos <= pos) {
1562 6202 dts += flv->time_offset;
1563 }
1564
1565
1/2
✓ Branch 0 taken 6202 times.
✗ Branch 1 not taken.
6202 if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
1566
4/4
✓ Branch 0 taken 4725 times.
✓ Branch 1 taken 1477 times.
✓ Branch 2 taken 2316 times.
✓ Branch 3 taken 2409 times.
6202 ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
1567 stream_type == FLV_STREAM_TYPE_AUDIO))
1568 3793 av_add_index_entry(st, pos, dts, track_size, 0, AVINDEX_KEYFRAME);
1569
1570
5/6
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 5914 times.
✓ Branch 2 taken 278 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 278 times.
6202 if ((st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || stream_type == FLV_STREAM_TYPE_AUDIO)) ||
1571
3/6
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5914 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
5924 (st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && stream_type == FLV_STREAM_TYPE_VIDEO)) ||
1572
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5914 times.
5924 st->discard >= AVDISCARD_ALL) {
1573 288 avio_seek(s->pb, next, SEEK_SET);
1574 288 ret = FFERROR_REDO;
1575 288 goto leave;
1576 }
1577
1578 // if not streamed and no duration from metadata then seek to end to find
1579 // the duration from the timestamps
1580
1/2
✓ Branch 0 taken 5914 times.
✗ Branch 1 not taken.
5914 if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) &&
1581
2/4
✓ Branch 0 taken 5914 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5914 times.
5914 (!s->duration || s->duration == AV_NOPTS_VALUE) &&
1582 !flv->searched_for_end) {
1583 int final_size;
1584 const int64_t pos = avio_tell(s->pb);
1585 // Read the last 4 bytes of the file, this should be the size of the
1586 // previous FLV tag. Use the timestamp of its payload as duration.
1587 int64_t fsize = avio_size(s->pb);
1588 retry_duration:
1589 avio_seek(s->pb, fsize - 4, SEEK_SET);
1590 final_size = avio_rb32(s->pb);
1591 if (final_size > 0 && final_size < fsize) {
1592 // Seek to the start of the last FLV tag at position (fsize - 4 - final_size)
1593 // but skip the byte indicating the type.
1594 avio_seek(s->pb, fsize - 3 - final_size, SEEK_SET);
1595 if (final_size == avio_rb24(s->pb) + 11) {
1596 uint32_t ts = avio_rb24(s->pb);
1597 ts |= (unsigned)avio_r8(s->pb) << 24;
1598 if (ts)
1599 s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
1600 else if (fsize >= 8 && fsize - 8 >= final_size) {
1601 fsize -= final_size+4;
1602 goto retry_duration;
1603 }
1604 }
1605 }
1606
1607 avio_seek(s->pb, pos, SEEK_SET);
1608 flv->searched_for_end = 1;
1609 }
1610
1611
4/4
✓ Branch 0 taken 3263 times.
✓ Branch 1 taken 2651 times.
✓ Branch 2 taken 2604 times.
✓ Branch 3 taken 659 times.
8518 if (stream_type == FLV_STREAM_TYPE_AUDIO && !enhanced_flv) {
1612 int bits_per_coded_sample;
1613
2/2
✓ Branch 0 taken 906 times.
✓ Branch 1 taken 1698 times.
2604 channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
1614 2604 sample_rate = 44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >>
1615 FLV_AUDIO_SAMPLERATE_OFFSET) >> 3;
1616
1/2
✓ Branch 0 taken 2604 times.
✗ Branch 1 not taken.
2604 bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
1617
2/2
✓ Branch 1 taken 2592 times.
✓ Branch 2 taken 12 times.
2604 if (!av_channel_layout_check(&st->codecpar->ch_layout) ||
1618
1/2
✓ Branch 0 taken 2592 times.
✗ Branch 1 not taken.
2592 !st->codecpar->sample_rate ||
1619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2592 times.
2592 !st->codecpar->bits_per_coded_sample) {
1620 12 av_channel_layout_default(&st->codecpar->ch_layout, channels);
1621 12 st->codecpar->sample_rate = sample_rate;
1622 12 st->codecpar->bits_per_coded_sample = bits_per_coded_sample;
1623 }
1624
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2592 times.
2604 if (!st->codecpar->codec_id) {
1625 12 flv_set_audio_codec(s, st, st->codecpar,
1626 flags & FLV_AUDIO_CODECID_MASK);
1627 12 flv->last_sample_rate =
1628 12 sample_rate = st->codecpar->sample_rate;
1629 12 flv->last_channels =
1630 12 channels = st->codecpar->ch_layout.nb_channels;
1631 } else {
1632 2592 AVCodecParameters *par = avcodec_parameters_alloc();
1633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2592 times.
2592 if (!par) {
1634 ret = AVERROR(ENOMEM);
1635 goto leave;
1636 }
1637 2592 par->sample_rate = sample_rate;
1638 2592 par->bits_per_coded_sample = bits_per_coded_sample;
1639 2592 flv_set_audio_codec(s, st, par, flags & FLV_AUDIO_CODECID_MASK);
1640 2592 sample_rate = par->sample_rate;
1641 2592 avcodec_parameters_free(&par);
1642 }
1643
2/2
✓ Branch 0 taken 659 times.
✓ Branch 1 taken 2651 times.
3310 } else if (stream_type == FLV_STREAM_TYPE_AUDIO) {
1644
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 647 times.
659 if (!st->codecpar->codec_id)
1645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 flv_set_audio_codec(s, st, st->codecpar,
1646 codec_id ? codec_id : (flags & FLV_AUDIO_CODECID_MASK));
1647
1648 // These are not signalled in the flags anymore
1649 659 channels = 0;
1650 659 sample_rate = 0;
1651
1652
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 643 times.
659 if (pkt_type == AudioPacketTypeMultichannelConfig) {
1653 16 int channel_order = avio_r8(s->pb);
1654 16 channels = avio_r8(s->pb);
1655 16 size -= 2;
1656 16 track_size -= 2;
1657
1658 16 av_channel_layout_uninit(&st->codecpar->ch_layout);
1659
1660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (channel_order == AudioChannelOrderCustom) {
1661 ret = av_channel_layout_custom_init(&st->codecpar->ch_layout, channels);
1662 if (ret < 0)
1663 return ret;
1664
1665 for (i = 0; i < channels; i++) {
1666 uint8_t id = avio_r8(s->pb);
1667 size--;
1668 track_size--;
1669
1670 if (id < 18)
1671 st->codecpar->ch_layout.u.map[i].id = id;
1672 else if (id >= 18 && id <= 23)
1673 st->codecpar->ch_layout.u.map[i].id = id - 18 + AV_CHAN_LOW_FREQUENCY_2;
1674 else if (id == 0xFE)
1675 st->codecpar->ch_layout.u.map[i].id = AV_CHAN_UNUSED;
1676 else
1677 st->codecpar->ch_layout.u.map[i].id = AV_CHAN_UNKNOWN;
1678 }
1679
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 3 times.
16 } else if (channel_order == AudioChannelOrderNative) {
1680 13 uint64_t mask = avio_rb32(s->pb);
1681 13 size -= 4;
1682 13 track_size -= 4;
1683
1684 // The first 18 entries in the mask match ours, but the remaining 6 entries start at AV_CHAN_LOW_FREQUENCY_2
1685 13 mask = (mask & 0x3FFFF) | ((mask & 0xFC0000) << (AV_CHAN_LOW_FREQUENCY_2 - 18));
1686 13 ret = av_channel_layout_from_mask(&st->codecpar->ch_layout, mask);
1687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0)
1688 return ret;
1689 } else {
1690 3 av_channel_layout_default(&st->codecpar->ch_layout, channels);
1691 }
1692
1693 16 av_log(s, AV_LOG_DEBUG, "Set channel data from MultiChannel info.\n");
1694
1695 16 goto next_track;
1696 }
1697
1/2
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
2651 } else if (stream_type == FLV_STREAM_TYPE_VIDEO) {
1698 2651 int sret = flv_set_video_codec(s, st, codec_id, 1);
1699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2651 times.
2651 if (sret < 0)
1700 return sret;
1701 2651 size -= sret;
1702 2651 track_size -= sret;
1703 } else if (stream_type == FLV_STREAM_TYPE_SUBTITLE) {
1704 st->codecpar->codec_id = AV_CODEC_ID_TEXT;
1705 } else if (stream_type == FLV_STREAM_TYPE_DATA) {
1706 st->codecpar->codec_id = AV_CODEC_ID_NONE; // Opaque AMF data
1707 }
1708
1709
2/2
✓ Branch 0 taken 5061 times.
✓ Branch 1 taken 837 times.
5898 if (st->codecpar->codec_id == AV_CODEC_ID_AAC ||
1710
2/2
✓ Branch 0 taken 4830 times.
✓ Branch 1 taken 231 times.
5061 st->codecpar->codec_id == AV_CODEC_ID_OPUS ||
1711
2/2
✓ Branch 0 taken 4778 times.
✓ Branch 1 taken 52 times.
4830 st->codecpar->codec_id == AV_CODEC_ID_FLAC ||
1712
2/2
✓ Branch 0 taken 3801 times.
✓ Branch 1 taken 977 times.
4778 st->codecpar->codec_id == AV_CODEC_ID_H264 ||
1713
1/2
✓ Branch 0 taken 3801 times.
✗ Branch 1 not taken.
3801 st->codecpar->codec_id == AV_CODEC_ID_MPEG4 ||
1714
2/2
✓ Branch 0 taken 3578 times.
✓ Branch 1 taken 223 times.
3801 st->codecpar->codec_id == AV_CODEC_ID_HEVC ||
1715
2/2
✓ Branch 0 taken 3402 times.
✓ Branch 1 taken 176 times.
3578 st->codecpar->codec_id == AV_CODEC_ID_AV1 ||
1716
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 3280 times.
3402 st->codecpar->codec_id == AV_CODEC_ID_VP9) {
1717 2618 int type = 0;
1718
2/2
✓ Branch 0 taken 1140 times.
✓ Branch 1 taken 1478 times.
2618 if (enhanced_flv) {
1719 1140 type = pkt_type;
1720 } else {
1721 1478 type = avio_r8(s->pb);
1722 1478 size--;
1723 1478 track_size--;
1724 }
1725
1726
2/4
✓ Branch 0 taken 2618 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2618 times.
2618 if (size < 0 || track_size < 0) {
1727 ret = AVERROR_INVALIDDATA;
1728 goto leave;
1729 }
1730
1731
6/6
✓ Branch 0 taken 1140 times.
✓ Branch 1 taken 1478 times.
✓ Branch 2 taken 638 times.
✓ Branch 3 taken 502 times.
✓ Branch 4 taken 13 times.
✓ Branch 5 taken 625 times.
2618 if (enhanced_flv && stream_type == FLV_STREAM_TYPE_VIDEO && flv->meta_color_info_flag) {
1732 13 flv_update_video_color_info(s, st); // update av packet side data
1733 13 flv->meta_color_info_flag = 0;
1734 }
1735
1736
1/2
✓ Branch 0 taken 2618 times.
✗ Branch 1 not taken.
2618 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4 ||
1737
6/6
✓ Branch 0 taken 1641 times.
✓ Branch 1 taken 977 times.
✓ Branch 2 taken 223 times.
✓ Branch 3 taken 1418 times.
✓ Branch 4 taken 340 times.
✓ Branch 5 taken 860 times.
2618 ((st->codecpar->codec_id == AV_CODEC_ID_H264 || st->codecpar->codec_id == AV_CODEC_ID_HEVC) &&
1738
2/2
✓ Branch 0 taken 277 times.
✓ Branch 1 taken 63 times.
340 (!enhanced_flv || type == PacketTypeCodedFrames))) {
1739 // sign extension
1740 1137 int32_t cts = (avio_rb24(s->pb) + 0xff800000) ^ 0xff800000;
1741 1137 pts = av_sat_add64(dts, cts);
1742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1137 times.
1137 if (cts < 0) { // dts might be wrong
1743 if (!flv->wrong_dts)
1744 av_log(s, AV_LOG_WARNING,
1745 "Negative cts, previous timestamps might be wrong.\n");
1746 flv->wrong_dts = 1;
1747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1137 times.
1137 } else if (FFABS(dts - pts) > 1000*60*15) {
1748 av_log(s, AV_LOG_WARNING,
1749 "invalid timestamps %"PRId64" %"PRId64"\n", dts, pts);
1750 dts = pts = AV_NOPTS_VALUE;
1751 }
1752 1137 size -= 3;
1753 1137 track_size -= 3;
1754 }
1755
6/6
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 2578 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 37 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 times.
2618 if (type == 0 && (!st->codecpar->extradata || st->codecpar->codec_id == AV_CODEC_ID_AAC ||
1756
3/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
2 st->codecpar->codec_id == AV_CODEC_ID_OPUS || st->codecpar->codec_id == AV_CODEC_ID_FLAC ||
1757
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 ||
1758 st->codecpar->codec_id == AV_CODEC_ID_AV1 || st->codecpar->codec_id == AV_CODEC_ID_VP9)) {
1759 AVDictionaryEntry *t;
1760
1761
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 37 times.
40 if (st->codecpar->extradata) {
1762
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
3 if ((ret = flv_queue_extradata(flv, s->pb, multitrack ? track_idx : stream_type, track_size, multitrack)) < 0)
1763 return ret;
1764 3 ret = FFERROR_REDO;
1765 3 goto leave;
1766 }
1767
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 if ((ret = flv_get_extradata(s, st, track_size)) < 0)
1768 return ret;
1769
1770 /* Workaround for buggy Omnia A/XE encoder */
1771 37 t = av_dict_get(s->metadata, "Encoder", NULL, 0);
1772
5/6
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
37 if (st->codecpar->codec_id == AV_CODEC_ID_AAC && t && !strcmp(t->value, "Omnia A/XE"))
1773 st->codecpar->extradata_size = 2;
1774
1775 37 ret = FFERROR_REDO;
1776 37 goto leave;
1777 }
1778 }
1779
1780 /* skip empty or broken data packets */
1781
3/4
✓ Branch 0 taken 5848 times.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5848 times.
5858 if (size <= 0 || track_size < 0) {
1782 10 ret = FFERROR_REDO;
1783 10 goto leave;
1784 }
1785
1786 /* skip empty data track */
1787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5848 times.
5848 if (!track_size)
1788 goto next_track;
1789
1790 5848 ret = av_get_packet(s->pb, pkt, track_size);
1791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5848 times.
5848 if (ret < 0)
1792 return ret;
1793
1794 5848 track_size -= ret;
1795 5848 size -= ret;
1796
1797 5848 pkt->dts = dts;
1798
2/2
✓ Branch 0 taken 4729 times.
✓ Branch 1 taken 1119 times.
5848 pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
1799 5848 pkt->stream_index = st->index;
1800 5848 pkt->pos = pos;
1801
4/4
✓ Branch 0 taken 4777 times.
✓ Branch 1 taken 1071 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4775 times.
5848 if (!multitrack && flv->new_extradata[stream_type]) {
1802 2 ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
1803 flv->new_extradata[stream_type],
1804 2 flv->new_extradata_size[stream_type]);
1805
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
1806 return ret;
1807
1808 2 flv->new_extradata[stream_type] = NULL;
1809 2 flv->new_extradata_size[stream_type] = 0;
1810
2/2
✓ Branch 0 taken 1071 times.
✓ Branch 1 taken 4775 times.
5846 } else if (multitrack &&
1811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1071 times.
1071 flv->mt_extradata_cnt > track_idx &&
1812 flv->mt_extradata[track_idx]) {
1813 ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
1814 flv->mt_extradata[track_idx],
1815 flv->mt_extradata_sz[track_idx]);
1816 if (ret < 0)
1817 return ret;
1818
1819 flv->mt_extradata[track_idx] = NULL;
1820 flv->mt_extradata_sz[track_idx] = 0;
1821 }
1822
4/4
✓ Branch 0 taken 3231 times.
✓ Branch 1 taken 2617 times.
✓ Branch 2 taken 2598 times.
✓ Branch 3 taken 633 times.
5848 if (stream_type == FLV_STREAM_TYPE_AUDIO && !enhanced_flv &&
1823
1/2
✓ Branch 0 taken 2598 times.
✗ Branch 1 not taken.
2598 (sample_rate != flv->last_sample_rate ||
1824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2598 times.
2598 channels != flv->last_channels)) {
1825 flv->last_sample_rate = sample_rate;
1826 flv->last_channels = channels;
1827 ff_add_param_change(pkt, channels, 0, sample_rate, 0, 0);
1828 }
1829
1830
2/2
✓ Branch 0 taken 2617 times.
✓ Branch 1 taken 3231 times.
5848 if (stream_type == FLV_STREAM_TYPE_AUDIO ||
1831
3/4
✓ Branch 0 taken 2131 times.
✓ Branch 1 taken 486 times.
✓ Branch 2 taken 2131 times.
✗ Branch 3 not taken.
2617 (flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||
1832
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2131 times.
2131 stream_type == FLV_STREAM_TYPE_SUBTITLE ||
1833 stream_type == FLV_STREAM_TYPE_DATA)
1834 3717 pkt->flags |= AV_PKT_FLAG_KEY;
1835
1836 5848 ret = ff_buffer_packet(s, pkt);
1837
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5848 times.
5848 if (ret < 0)
1838 return ret;
1839 5848 res = FFERROR_REDO;
1840
1841 5864 next_track:
1842
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5863 times.
5864 if (track_size) {
1843 1 av_log(s, AV_LOG_WARNING, "Track size mismatch: %d!\n", track_size);
1844 1 avio_skip(s->pb, track_size);
1845 1 size -= track_size;
1846 }
1847
1848
1/2
✓ Branch 0 taken 5864 times.
✗ Branch 1 not taken.
5864 if (!size)
1849 5864 break;
1850
1851 if (multitrack_type == MultitrackTypeOneTrack) {
1852 av_log(s, AV_LOG_ERROR, "Attempted to read next track in single-track mode.\n");
1853 ret = FFERROR_REDO;
1854 goto leave;
1855 }
1856
1857 if (multitrack_type == MultitrackTypeManyTracksManyCodecs) {
1858 codec_id = avio_rb32(s->pb);
1859 size -= 4;
1860 }
1861
1862 track_idx = avio_r8(s->pb);
1863 size--;
1864
1865 if (avio_feof(s->pb)) {
1866 av_log(s, AV_LOG_WARNING, "Premature EOF\n");
1867 /* return REDO so that any potentially queued up packages can be drained first */
1868 return FFERROR_REDO;
1869 }
1870 }
1871
1872 5864 ret = 0;
1873 6256 leave:
1874 6256 last = avio_rb32(s->pb);
1875
1/2
✓ Branch 0 taken 6256 times.
✗ Branch 1 not taken.
6256 if (!flv->trust_datasize) {
1876
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6255 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
6257 if (last != orig_size + 11 && last != orig_size + 10 &&
1877
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
1 !avio_feof(s->pb) &&
1878 (last != orig_size || !last) && last != flv->sum_flv_tag_size &&
1879 !flv->broken_sizes) {
1880 av_log(s, AV_LOG_ERROR, "Packet mismatch %d %d %"PRId64"\n", last, orig_size + 11, flv->sum_flv_tag_size);
1881 avio_seek(s->pb, pos + 1, SEEK_SET);
1882 ret = resync(s);
1883 av_packet_unref(pkt);
1884 if (ret >= 0) {
1885 goto retry;
1886 }
1887 }
1888 }
1889
1890
2/2
✓ Branch 0 taken 5864 times.
✓ Branch 1 taken 392 times.
6256 if (ret >= 0)
1891 5864 flv->last_ts = pkt->dts;
1892
1893
2/2
✓ Branch 0 taken 392 times.
✓ Branch 1 taken 5864 times.
6256 return ret ? ret : res;
1894 }
1895
1896 289 static int flv_read_seek(AVFormatContext *s, int stream_index,
1897 int64_t ts, int flags)
1898 {
1899 289 FLVContext *flv = s->priv_data;
1900 289 flv->validate_count = 0;
1901 289 return avio_seek_time(s->pb, stream_index, ts, flags);
1902 }
1903
1904 #define OFFSET(x) offsetof(FLVContext, x)
1905 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1906 static const AVOption options[] = {
1907 { "flv_metadata", "Allocate streams according to the onMetaData array", OFFSET(trust_metadata), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1908 { "flv_full_metadata", "Dump full metadata of the onMetadata", OFFSET(dump_full_metadata), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1909 { "flv_ignore_prevtag", "Ignore the Size of previous tag", OFFSET(trust_datasize), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
1910 { NULL }
1911 };
1912
1913 static const AVClass flv_kux_class = {
1914 .class_name = "(live) flv/kux demuxer",
1915 .item_name = av_default_item_name,
1916 .option = options,
1917 .version = LIBAVUTIL_VERSION_INT,
1918 };
1919
1920 const FFInputFormat ff_flv_demuxer = {
1921 .p.name = "flv",
1922 .p.long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
1923 .p.extensions = "flv",
1924 .p.priv_class = &flv_kux_class,
1925 .priv_data_size = sizeof(FLVContext),
1926 .read_probe = flv_probe,
1927 .read_header = flv_read_header,
1928 .read_packet = flv_read_packet,
1929 .read_seek = flv_read_seek,
1930 .read_close = flv_read_close,
1931 };
1932
1933 const FFInputFormat ff_live_flv_demuxer = {
1934 .p.name = "live_flv",
1935 .p.long_name = NULL_IF_CONFIG_SMALL("live RTMP FLV (Flash Video)"),
1936 .p.extensions = "flv",
1937 .p.priv_class = &flv_kux_class,
1938 .p.flags = AVFMT_TS_DISCONT,
1939 .priv_data_size = sizeof(FLVContext),
1940 .read_probe = live_flv_probe,
1941 .read_header = flv_read_header,
1942 .read_packet = flv_read_packet,
1943 .read_seek = flv_read_seek,
1944 .read_close = flv_read_close,
1945 };
1946
1947 const FFInputFormat ff_kux_demuxer = {
1948 .p.name = "kux",
1949 .p.long_name = NULL_IF_CONFIG_SMALL("KUX (YouKu)"),
1950 .p.extensions = "kux",
1951 .p.priv_class = &flv_kux_class,
1952 .priv_data_size = sizeof(FLVContext),
1953 .read_probe = kux_probe,
1954 .read_header = flv_read_header,
1955 .read_packet = flv_read_packet,
1956 .read_seek = flv_read_seek,
1957 .read_close = flv_read_close,
1958 };
1959