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