| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | * FLV muxer | ||
| 3 | * Copyright (c) 2003 The FFmpeg Project | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "libavutil/intreadwrite.h" | ||
| 23 | #include "libavutil/dict.h" | ||
| 24 | #include "libavutil/intfloat.h" | ||
| 25 | #include "libavutil/avassert.h" | ||
| 26 | #include "libavutil/mastering_display_metadata.h" | ||
| 27 | #include "libavutil/mathematics.h" | ||
| 28 | #include "libavutil/mem.h" | ||
| 29 | #include "libavcodec/codec_desc.h" | ||
| 30 | #include "libavcodec/mpeg4audio.h" | ||
| 31 | #include "avio.h" | ||
| 32 | #include "avc.h" | ||
| 33 | #include "av1.h" | ||
| 34 | #include "vpcc.h" | ||
| 35 | #include "hevc.h" | ||
| 36 | #include "avformat.h" | ||
| 37 | #include "flv.h" | ||
| 38 | #include "internal.h" | ||
| 39 | #include "nal.h" | ||
| 40 | #include "mux.h" | ||
| 41 | #include "libavutil/opt.h" | ||
| 42 | #include "libavcodec/put_bits.h" | ||
| 43 | |||
| 44 | |||
| 45 | static const AVCodecTag flv_video_codec_ids[] = { | ||
| 46 | { AV_CODEC_ID_FLV1, FLV_CODECID_H263 }, | ||
| 47 | { AV_CODEC_ID_H263, FLV_CODECID_REALH263 }, | ||
| 48 | { AV_CODEC_ID_MPEG4, FLV_CODECID_MPEG4 }, | ||
| 49 | { AV_CODEC_ID_FLASHSV, FLV_CODECID_SCREEN }, | ||
| 50 | { AV_CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2 }, | ||
| 51 | { AV_CODEC_ID_VP6F, FLV_CODECID_VP6 }, | ||
| 52 | { AV_CODEC_ID_VP6, FLV_CODECID_VP6 }, | ||
| 53 | { AV_CODEC_ID_VP6A, FLV_CODECID_VP6A }, | ||
| 54 | { AV_CODEC_ID_H264, FLV_CODECID_H264 }, | ||
| 55 | { AV_CODEC_ID_HEVC, MKBETAG('h', 'v', 'c', '1') }, | ||
| 56 | { AV_CODEC_ID_AV1, MKBETAG('a', 'v', '0', '1') }, | ||
| 57 | { AV_CODEC_ID_VP9, MKBETAG('v', 'p', '0', '9') }, | ||
| 58 | { AV_CODEC_ID_NONE, 0 } | ||
| 59 | }; | ||
| 60 | |||
| 61 | static const AVCodecTag flv_audio_codec_ids[] = { | ||
| 62 | { AV_CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET }, | ||
| 63 | { AV_CODEC_ID_PCM_U8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET }, | ||
| 64 | { AV_CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET }, | ||
| 65 | { AV_CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET }, | ||
| 66 | { AV_CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET }, | ||
| 67 | { AV_CODEC_ID_AAC, FLV_CODECID_AAC >> FLV_AUDIO_CODECID_OFFSET }, | ||
| 68 | { AV_CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET }, | ||
| 69 | { AV_CODEC_ID_PCM_MULAW, FLV_CODECID_PCM_MULAW >> FLV_AUDIO_CODECID_OFFSET }, | ||
| 70 | { AV_CODEC_ID_PCM_ALAW, FLV_CODECID_PCM_ALAW >> FLV_AUDIO_CODECID_OFFSET }, | ||
| 71 | { AV_CODEC_ID_SPEEX, FLV_CODECID_SPEEX >> FLV_AUDIO_CODECID_OFFSET }, | ||
| 72 | { AV_CODEC_ID_OPUS, MKBETAG('O', 'p', 'u', 's') }, | ||
| 73 | { AV_CODEC_ID_FLAC, MKBETAG('f', 'L', 'a', 'C') }, | ||
| 74 | { AV_CODEC_ID_AC3, MKBETAG('a', 'c', '-', '3') }, | ||
| 75 | { AV_CODEC_ID_EAC3, MKBETAG('e', 'c', '-', '3') }, | ||
| 76 | { AV_CODEC_ID_NONE, 0 } | ||
| 77 | }; | ||
| 78 | |||
| 79 | typedef enum { | ||
| 80 | FLV_AAC_SEQ_HEADER_DETECT = (1 << 0), | ||
| 81 | FLV_NO_SEQUENCE_END = (1 << 1), | ||
| 82 | FLV_ADD_KEYFRAME_INDEX = (1 << 2), | ||
| 83 | FLV_NO_METADATA = (1 << 3), | ||
| 84 | FLV_NO_DURATION_FILESIZE = (1 << 4), | ||
| 85 | } FLVFlags; | ||
| 86 | |||
| 87 | typedef struct FLVFileposition { | ||
| 88 | int64_t keyframe_position; | ||
| 89 | double keyframe_timestamp; | ||
| 90 | struct FLVFileposition *next; | ||
| 91 | } FLVFileposition; | ||
| 92 | |||
| 93 | typedef struct FLVContext { | ||
| 94 | AVClass *av_class; | ||
| 95 | int reserved; | ||
| 96 | int64_t duration_offset; | ||
| 97 | int64_t filesize_offset; | ||
| 98 | int64_t duration; | ||
| 99 | int64_t delay; ///< first dts delay (needed for AVC & Speex) | ||
| 100 | |||
| 101 | int64_t datastart_offset; | ||
| 102 | int64_t datasize_offset; | ||
| 103 | int64_t datasize; | ||
| 104 | int64_t videosize_offset; | ||
| 105 | int64_t videosize; | ||
| 106 | int64_t audiosize_offset; | ||
| 107 | int64_t audiosize; | ||
| 108 | |||
| 109 | int64_t metadata_size_pos; | ||
| 110 | int64_t metadata_totalsize_pos; | ||
| 111 | int64_t metadata_totalsize; | ||
| 112 | int64_t keyframe_index_size; | ||
| 113 | |||
| 114 | int64_t lasttimestamp_offset; | ||
| 115 | double lasttimestamp; | ||
| 116 | int64_t lastkeyframetimestamp_offset; | ||
| 117 | double lastkeyframetimestamp; | ||
| 118 | int64_t lastkeyframelocation_offset; | ||
| 119 | int64_t lastkeyframelocation; | ||
| 120 | |||
| 121 | int64_t keyframes_info_offset; | ||
| 122 | |||
| 123 | int64_t filepositions_count; | ||
| 124 | FLVFileposition *filepositions; | ||
| 125 | FLVFileposition *head_filepositions; | ||
| 126 | |||
| 127 | AVCodecParameters *audio_par; | ||
| 128 | AVCodecParameters *video_par; | ||
| 129 | double framerate; | ||
| 130 | AVCodecParameters *data_par; | ||
| 131 | |||
| 132 | int flags; | ||
| 133 | int64_t *last_ts; | ||
| 134 | int *metadata_pkt_written; | ||
| 135 | int *track_idx_map; | ||
| 136 | } FLVContext; | ||
| 137 | |||
| 138 | 944 | static int get_audio_flags(AVFormatContext *s, AVCodecParameters *par) | |
| 139 | { | ||
| 140 | 1888 | int flags = (par->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT | |
| 141 | 
        2/2✓ Branch 0 taken 220 times. 
          ✓ Branch 1 taken 724 times. 
         | 
      944 | : FLV_SAMPLESSIZE_8BIT; | 
| 142 | |||
| 143 | 
        2/2✓ Branch 0 taken 293 times. 
          ✓ Branch 1 taken 651 times. 
         | 
      944 | if (par->codec_id == AV_CODEC_ID_AAC) // specs force these parameters | 
| 144 | 293 | return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ | | |
| 145 | FLV_SAMPLESSIZE_16BIT | FLV_STEREO; | ||
| 146 | 
        4/4✓ Branch 0 taken 574 times. 
          ✓ Branch 1 taken 77 times. 
          ✓ Branch 2 taken 557 times. 
          ✓ Branch 3 taken 17 times. 
         | 
      651 | if (par->codec_id == AV_CODEC_ID_OPUS || par->codec_id == AV_CODEC_ID_FLAC | 
| 147 | 
        3/4✓ Branch 0 taken 509 times. 
          ✓ Branch 1 taken 48 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 509 times. 
         | 
      557 | || par->codec_id == AV_CODEC_ID_AC3 || par->codec_id == AV_CODEC_ID_EAC3) | 
| 148 | 142 | return FLV_CODECID_EX_HEADER; // only needed for codec support check | |
| 149 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 509 times. 
         | 
      509 | else if (par->codec_id == AV_CODEC_ID_SPEEX) { | 
| 150 | ✗ | if (par->sample_rate != 16000) { | |
| 151 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 152 | "FLV only supports wideband (16kHz) Speex audio\n"); | ||
| 153 | ✗ | return AVERROR(EINVAL); | |
| 154 | } | ||
| 155 | ✗ | if (par->ch_layout.nb_channels != 1) { | |
| 156 | ✗ | av_log(s, AV_LOG_ERROR, "FLV only supports mono Speex audio\n"); | |
| 157 | ✗ | return AVERROR(EINVAL); | |
| 158 | } | ||
| 159 | ✗ | return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT; | |
| 160 | } else { | ||
| 161 | 
        2/6✗ Branch 0 not taken. 
          ✓ Branch 1 taken 132 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
          ✓ Branch 4 taken 377 times. 
          ✗ Branch 5 not taken. 
         | 
      509 | switch (par->sample_rate) { | 
| 162 | ✗ | case 48000: | |
| 163 | // 48khz mp3 is stored with 44k1 samplerate identifier | ||
| 164 | ✗ | if (par->codec_id == AV_CODEC_ID_MP3) { | |
| 165 | ✗ | flags |= FLV_SAMPLERATE_44100HZ; | |
| 166 | ✗ | break; | |
| 167 | } else { | ||
| 168 | ✗ | goto error; | |
| 169 | } | ||
| 170 | 132 | case 44100: | |
| 171 | 132 | flags |= FLV_SAMPLERATE_44100HZ; | |
| 172 | 132 | break; | |
| 173 | ✗ | case 22050: | |
| 174 | ✗ | flags |= FLV_SAMPLERATE_22050HZ; | |
| 175 | ✗ | break; | |
| 176 | ✗ | case 11025: | |
| 177 | ✗ | flags |= FLV_SAMPLERATE_11025HZ; | |
| 178 | ✗ | break; | |
| 179 | 377 | case 16000: // nellymoser only | |
| 180 | case 8000: // nellymoser only | ||
| 181 | case 5512: // not MP3 | ||
| 182 | 
        1/2✓ Branch 0 taken 377 times. 
          ✗ Branch 1 not taken. 
         | 
      377 | if (par->codec_id != AV_CODEC_ID_MP3) { | 
| 183 | 377 | flags |= FLV_SAMPLERATE_SPECIAL; | |
| 184 | 377 | break; | |
| 185 | } | ||
| 186 | default: | ||
| 187 | ✗ | error: | |
| 188 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 189 | "FLV does not support sample rate %d, " | ||
| 190 | "choose from (44100, 22050, 11025)\n", par->sample_rate); | ||
| 191 | ✗ | return AVERROR(EINVAL); | |
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | 
        2/2✓ Branch 0 taken 132 times. 
          ✓ Branch 1 taken 377 times. 
         | 
      509 | if (par->ch_layout.nb_channels > 1) | 
| 196 | 132 | flags |= FLV_STEREO; | |
| 197 | |||
| 198 | 
        2/10✗ Branch 0 not taken. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
          ✓ Branch 4 taken 132 times. 
          ✓ Branch 5 taken 377 times. 
          ✗ Branch 6 not taken. 
          ✗ Branch 7 not taken. 
          ✗ Branch 8 not taken. 
          ✗ Branch 9 not taken. 
         | 
      509 | switch (par->codec_id) { | 
| 199 | ✗ | case AV_CODEC_ID_MP3: | |
| 200 | ✗ | flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT; | |
| 201 | ✗ | break; | |
| 202 | ✗ | case AV_CODEC_ID_PCM_U8: | |
| 203 | ✗ | flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT; | |
| 204 | ✗ | break; | |
| 205 | ✗ | case AV_CODEC_ID_PCM_S16BE: | |
| 206 | ✗ | flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT; | |
| 207 | ✗ | break; | |
| 208 | ✗ | case AV_CODEC_ID_PCM_S16LE: | |
| 209 | ✗ | flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT; | |
| 210 | ✗ | break; | |
| 211 | 132 | case AV_CODEC_ID_ADPCM_SWF: | |
| 212 | 132 | flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT; | |
| 213 | 132 | break; | |
| 214 | 377 | case AV_CODEC_ID_NELLYMOSER: | |
| 215 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 377 times. 
         | 
      377 | if (par->sample_rate == 8000) | 
| 216 | ✗ | flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT; | |
| 217 | 
        1/2✓ Branch 0 taken 377 times. 
          ✗ Branch 1 not taken. 
         | 
      377 | else if (par->sample_rate == 16000) | 
| 218 | 377 | flags |= FLV_CODECID_NELLYMOSER_16KHZ_MONO | FLV_SAMPLESSIZE_16BIT; | |
| 219 | else | ||
| 220 | ✗ | flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT; | |
| 221 | 377 | break; | |
| 222 | ✗ | case AV_CODEC_ID_PCM_MULAW: | |
| 223 | ✗ | flags |= FLV_CODECID_PCM_MULAW | FLV_SAMPLESSIZE_16BIT; | |
| 224 | ✗ | break; | |
| 225 | ✗ | case AV_CODEC_ID_PCM_ALAW: | |
| 226 | ✗ | flags |= FLV_CODECID_PCM_ALAW | FLV_SAMPLESSIZE_16BIT; | |
| 227 | ✗ | break; | |
| 228 | ✗ | case 0: | |
| 229 | ✗ | flags |= par->codec_tag << 4; | |
| 230 | ✗ | break; | |
| 231 | ✗ | default: | |
| 232 | ✗ | av_log(s, AV_LOG_ERROR, "Audio codec '%s' not compatible with FLV\n", | |
| 233 | avcodec_get_name(par->codec_id)); | ||
| 234 | ✗ | return AVERROR(EINVAL); | |
| 235 | } | ||
| 236 | |||
| 237 | 509 | return flags; | |
| 238 | } | ||
| 239 | |||
| 240 | 318 | static void put_amf_string(AVIOContext *pb, const char *str) | |
| 241 | { | ||
| 242 | 318 | size_t len = strlen(str); | |
| 243 | 318 | avio_wb16(pb, len); | |
| 244 | // Avoid avio_write() if put_amf_string(pb, "") is inlined. | ||
| 245 | if (av_builtin_constant_p(len == 0) && len == 0) | ||
| 246 | return; | ||
| 247 | 318 | avio_write(pb, str, len); | |
| 248 | } | ||
| 249 | |||
| 250 | // FLV timestamps are 32 bits signed, RTMP timestamps should be 32-bit unsigned | ||
| 251 | 2121 | static void put_timestamp(AVIOContext *pb, int64_t ts) { | |
| 252 | 2121 | avio_wb24(pb, ts & 0xFFFFFF); | |
| 253 | 2121 | avio_w8(pb, (ts >> 24) & 0x7F); | |
| 254 | 2121 | } | |
| 255 | |||
| 256 | 2 | static void put_eos_tag(AVIOContext *pb, unsigned ts, enum AVCodecID codec_id) | |
| 257 | { | ||
| 258 | 2 | uint32_t tag = ff_codec_get_tag(flv_video_codec_ids, codec_id); | |
| 259 | /* ub[4] FrameType = 1, ub[4] CodecId */ | ||
| 260 | 2 | tag |= 1 << 4; | |
| 261 | 2 | avio_w8(pb, FLV_TAG_TYPE_VIDEO); | |
| 262 | 2 | avio_wb24(pb, 5); /* Tag Data Size */ | |
| 263 | 2 | put_timestamp(pb, ts); | |
| 264 | 2 | avio_wb24(pb, 0); /* StreamId = 0 */ | |
| 265 | 2 | avio_w8(pb, tag); | |
| 266 | 2 | avio_w8(pb, 2); /* AVC end of sequence */ | |
| 267 | 2 | avio_wb24(pb, 0); /* Always 0 for AVC EOS. */ | |
| 268 | 2 | avio_wb32(pb, 16); /* Size of FLV tag */ | |
| 269 | 2 | } | |
| 270 | |||
| 271 | 286 | static void put_amf_double(AVIOContext *pb, double d) | |
| 272 | { | ||
| 273 | 286 | avio_w8(pb, AMF_DATA_TYPE_NUMBER); | |
| 274 | 286 | avio_wb64(pb, av_double2int(d)); | |
| 275 | 286 | } | |
| 276 | |||
| 277 | 1 | static void put_amf_byte(AVIOContext *pb, unsigned char abyte) | |
| 278 | { | ||
| 279 | 1 | avio_w8(pb, abyte); | |
| 280 | 1 | } | |
| 281 | |||
| 282 | 2 | static void put_amf_dword_array(AVIOContext *pb, uint32_t dw) | |
| 283 | { | ||
| 284 | 2 | avio_w8(pb, AMF_DATA_TYPE_ARRAY); | |
| 285 | 2 | avio_wb32(pb, dw); | |
| 286 | 2 | } | |
| 287 | |||
| 288 | 10 | static void put_amf_bool(AVIOContext *pb, int b) | |
| 289 | { | ||
| 290 | 10 | avio_w8(pb, AMF_DATA_TYPE_BOOL); | |
| 291 | 10 | avio_w8(pb, !!b); | |
| 292 | 10 | } | |
| 293 | |||
| 294 | 23 | static void write_metadata(AVFormatContext *s, unsigned int ts) | |
| 295 | { | ||
| 296 | 23 | AVIOContext *pb = s->pb; | |
| 297 | 23 | FLVContext *flv = s->priv_data; | |
| 298 | 23 | int write_duration_filesize = !(flv->flags & FLV_NO_DURATION_FILESIZE); | |
| 299 | 23 | int metadata_count = 0; | |
| 300 | int64_t metadata_count_pos; | ||
| 301 | 23 | const AVDictionaryEntry *tag = NULL; | |
| 302 | |||
| 303 | /* write meta_tag */ | ||
| 304 | 23 | avio_w8(pb, FLV_TAG_TYPE_META); // tag type META | |
| 305 | 23 | flv->metadata_size_pos = avio_tell(pb); | |
| 306 | 23 | avio_wb24(pb, 0); // size of data part (sum of all parts below) | |
| 307 | 23 | put_timestamp(pb, ts); // timestamp | |
| 308 | 23 | avio_wb24(pb, 0); // reserved | |
| 309 | |||
| 310 | /* now data of data_size size */ | ||
| 311 | |||
| 312 | /* first event name as a string */ | ||
| 313 | 23 | avio_w8(pb, AMF_DATA_TYPE_STRING); | |
| 314 | 23 | put_amf_string(pb, "onMetaData"); // 12 bytes | |
| 315 | |||
| 316 | /* mixed array (hash) with size and string/type/data tuples */ | ||
| 317 | 23 | avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY); | |
| 318 | 23 | metadata_count_pos = avio_tell(pb); | |
| 319 | 
        2/2✓ Branch 0 taken 20 times. 
          ✓ Branch 1 taken 3 times. 
         | 
      23 | metadata_count = 4 * !!flv->video_par + | 
| 320 | 
        2/2✓ Branch 0 taken 5 times. 
          ✓ Branch 1 taken 18 times. 
         | 
      23 | 5 * !!flv->audio_par + | 
| 321 | 23 | 1 * !!flv->data_par; | |
| 322 | 
        1/2✓ Branch 0 taken 23 times. 
          ✗ Branch 1 not taken. 
         | 
      23 | if (write_duration_filesize) { | 
| 323 | 23 | metadata_count += 2; // +2 for duration and file size | |
| 324 | } | ||
| 325 | 23 | avio_wb32(pb, metadata_count); | |
| 326 | |||
| 327 | 
        1/2✓ Branch 0 taken 23 times. 
          ✗ Branch 1 not taken. 
         | 
      23 | if (write_duration_filesize) { | 
| 328 | 23 | put_amf_string(pb, "duration"); | |
| 329 | 23 | flv->duration_offset = avio_tell(pb); | |
| 330 | // fill in the guessed duration, it'll be corrected later if incorrect | ||
| 331 | 23 | put_amf_double(pb, s->duration / AV_TIME_BASE); | |
| 332 | } | ||
| 333 | |||
| 334 | 
        2/2✓ Branch 0 taken 20 times. 
          ✓ Branch 1 taken 3 times. 
         | 
      23 | if (flv->video_par) { | 
| 335 | 20 | put_amf_string(pb, "width"); | |
| 336 | 20 | put_amf_double(pb, flv->video_par->width); | |
| 337 | |||
| 338 | 20 | put_amf_string(pb, "height"); | |
| 339 | 20 | put_amf_double(pb, flv->video_par->height); | |
| 340 | |||
| 341 | 20 | put_amf_string(pb, "videodatarate"); | |
| 342 | 20 | put_amf_double(pb, flv->video_par->bit_rate / 1024.0); | |
| 343 | |||
| 344 | 
        2/2✓ Branch 0 taken 18 times. 
          ✓ Branch 1 taken 2 times. 
         | 
      20 | if (flv->framerate != 0.0) { | 
| 345 | 18 | put_amf_string(pb, "framerate"); | |
| 346 | 18 | put_amf_double(pb, flv->framerate); | |
| 347 | 18 | metadata_count++; | |
| 348 | } | ||
| 349 | |||
| 350 | 20 | put_amf_string(pb, "videocodecid"); | |
| 351 | 20 | put_amf_double(pb, flv->video_par->codec_tag); | |
| 352 | } | ||
| 353 | |||
| 354 | 
        2/2✓ Branch 0 taken 5 times. 
          ✓ Branch 1 taken 18 times. 
         | 
      23 | if (flv->audio_par) { | 
| 355 | 5 | put_amf_string(pb, "audiodatarate"); | |
| 356 | 5 | put_amf_double(pb, flv->audio_par->bit_rate / 1024.0); | |
| 357 | |||
| 358 | 5 | put_amf_string(pb, "audiosamplerate"); | |
| 359 | 5 | put_amf_double(pb, flv->audio_par->sample_rate); | |
| 360 | |||
| 361 | 5 | put_amf_string(pb, "audiosamplesize"); | |
| 362 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 5 times. 
         | 
      5 | put_amf_double(pb, flv->audio_par->codec_id == AV_CODEC_ID_PCM_U8 ? 8 : 16); | 
| 363 | |||
| 364 | 5 | put_amf_string(pb, "stereo"); | |
| 365 | 5 | put_amf_bool(pb, flv->audio_par->ch_layout.nb_channels == 2); | |
| 366 | |||
| 367 | 5 | put_amf_string(pb, "audiocodecid"); | |
| 368 | 5 | put_amf_double(pb, flv->audio_par->codec_tag); | |
| 369 | } | ||
| 370 | |||
| 371 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 23 times. 
         | 
      23 | if (flv->data_par) { | 
| 372 | ✗ | put_amf_string(pb, "datastream"); | |
| 373 | ✗ | put_amf_double(pb, 0.0); | |
| 374 | } | ||
| 375 | |||
| 376 | 23 | ff_standardize_creation_time(s); | |
| 377 | 
        2/2✓ Branch 1 taken 14 times. 
          ✓ Branch 2 taken 23 times. 
         | 
      37 | while ((tag = av_dict_iterate(s->metadata, tag))) { | 
| 378 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | if( !strcmp(tag->key, "width") | 
| 379 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "height") | 
| 380 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "videodatarate") | 
| 381 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "framerate") | 
| 382 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "videocodecid") | 
| 383 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "audiodatarate") | 
| 384 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "audiosamplerate") | 
| 385 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "audiosamplesize") | 
| 386 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "stereo") | 
| 387 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "audiocodecid") | 
| 388 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "duration") | 
| 389 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "onMetaData") | 
| 390 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "datasize") | 
| 391 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "lasttimestamp") | 
| 392 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "totalframes") | 
| 393 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "hasAudio") | 
| 394 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "hasVideo") | 
| 395 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "hasCuePoints") | 
| 396 | 
        1/2✓ Branch 0 taken 14 times. 
          ✗ Branch 1 not taken. 
         | 
      14 | ||!strcmp(tag->key, "hasMetadata") | 
| 397 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 14 times. 
         | 
      14 | ||!strcmp(tag->key, "hasKeyframes") | 
| 398 | ){ | ||
| 399 | ✗ | av_log(s, AV_LOG_DEBUG, "Ignoring metadata for %s\n", tag->key); | |
| 400 | ✗ | continue; | |
| 401 | } | ||
| 402 | 14 | put_amf_string(pb, tag->key); | |
| 403 | 14 | avio_w8(pb, AMF_DATA_TYPE_STRING); | |
| 404 | 14 | put_amf_string(pb, tag->value); | |
| 405 | 14 | metadata_count++; | |
| 406 | } | ||
| 407 | |||
| 408 | 
        1/2✓ Branch 0 taken 23 times. 
          ✗ Branch 1 not taken. 
         | 
      23 | if (write_duration_filesize) { | 
| 409 | 23 | put_amf_string(pb, "filesize"); | |
| 410 | 23 | flv->filesize_offset = avio_tell(pb); | |
| 411 | 23 | put_amf_double(pb, 0); // delayed write | |
| 412 | } | ||
| 413 | |||
| 414 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 22 times. 
         | 
      23 | if (flv->flags & FLV_ADD_KEYFRAME_INDEX) { | 
| 415 | 1 | flv->keyframe_index_size = 0; | |
| 416 | |||
| 417 | 1 | put_amf_string(pb, "hasVideo"); | |
| 418 | 1 | put_amf_bool(pb, !!flv->video_par); | |
| 419 | 1 | metadata_count++; | |
| 420 | |||
| 421 | 1 | put_amf_string(pb, "hasKeyframes"); | |
| 422 | 1 | put_amf_bool(pb, 1); | |
| 423 | 1 | metadata_count++; | |
| 424 | |||
| 425 | 1 | put_amf_string(pb, "hasAudio"); | |
| 426 | 1 | put_amf_bool(pb, !!flv->audio_par); | |
| 427 | 1 | metadata_count++; | |
| 428 | |||
| 429 | 1 | put_amf_string(pb, "hasMetadata"); | |
| 430 | 1 | put_amf_bool(pb, 1); | |
| 431 | 1 | metadata_count++; | |
| 432 | |||
| 433 | 1 | put_amf_string(pb, "canSeekToEnd"); | |
| 434 | 1 | put_amf_bool(pb, 1); | |
| 435 | 1 | metadata_count++; | |
| 436 | |||
| 437 | 1 | put_amf_string(pb, "datasize"); | |
| 438 | 1 | flv->datasize_offset = avio_tell(pb); | |
| 439 | 1 | flv->datasize = 0; | |
| 440 | 1 | put_amf_double(pb, flv->datasize); | |
| 441 | 1 | metadata_count++; | |
| 442 | |||
| 443 | 1 | put_amf_string(pb, "videosize"); | |
| 444 | 1 | flv->videosize_offset = avio_tell(pb); | |
| 445 | 1 | flv->videosize = 0; | |
| 446 | 1 | put_amf_double(pb, flv->videosize); | |
| 447 | 1 | metadata_count++; | |
| 448 | |||
| 449 | 1 | put_amf_string(pb, "audiosize"); | |
| 450 | 1 | flv->audiosize_offset = avio_tell(pb); | |
| 451 | 1 | flv->audiosize = 0; | |
| 452 | 1 | put_amf_double(pb, flv->audiosize); | |
| 453 | 1 | metadata_count++; | |
| 454 | |||
| 455 | 1 | put_amf_string(pb, "lasttimestamp"); | |
| 456 | 1 | flv->lasttimestamp_offset = avio_tell(pb); | |
| 457 | 1 | flv->lasttimestamp = 0; | |
| 458 | 1 | put_amf_double(pb, 0); | |
| 459 | 1 | metadata_count++; | |
| 460 | |||
| 461 | 1 | put_amf_string(pb, "lastkeyframetimestamp"); | |
| 462 | 1 | flv->lastkeyframetimestamp_offset = avio_tell(pb); | |
| 463 | 1 | flv->lastkeyframetimestamp = 0; | |
| 464 | 1 | put_amf_double(pb, 0); | |
| 465 | 1 | metadata_count++; | |
| 466 | |||
| 467 | 1 | put_amf_string(pb, "lastkeyframelocation"); | |
| 468 | 1 | flv->lastkeyframelocation_offset = avio_tell(pb); | |
| 469 | 1 | flv->lastkeyframelocation = 0; | |
| 470 | 1 | put_amf_double(pb, 0); | |
| 471 | 1 | metadata_count++; | |
| 472 | |||
| 473 | 1 | put_amf_string(pb, "keyframes"); | |
| 474 | 1 | put_amf_byte(pb, AMF_DATA_TYPE_OBJECT); | |
| 475 | 1 | metadata_count++; | |
| 476 | |||
| 477 | 1 | flv->keyframes_info_offset = avio_tell(pb); | |
| 478 | } | ||
| 479 | |||
| 480 | 23 | put_amf_string(pb, ""); | |
| 481 | 23 | avio_w8(pb, AMF_END_OF_OBJECT); | |
| 482 | |||
| 483 | /* write total size of tag */ | ||
| 484 | 23 | flv->metadata_totalsize = avio_tell(pb) - flv->metadata_size_pos - 10; | |
| 485 | |||
| 486 | 23 | avio_seek(pb, metadata_count_pos, SEEK_SET); | |
| 487 | 23 | avio_wb32(pb, metadata_count); | |
| 488 | |||
| 489 | 23 | avio_seek(pb, flv->metadata_size_pos, SEEK_SET); | |
| 490 | 23 | avio_wb24(pb, flv->metadata_totalsize); | |
| 491 | 23 | avio_skip(pb, flv->metadata_totalsize + 10 - 3); | |
| 492 | 23 | flv->metadata_totalsize_pos = avio_tell(pb); | |
| 493 | 23 | avio_wb32(pb, flv->metadata_totalsize + 11); | |
| 494 | 23 | } | |
| 495 | |||
| 496 | 568 | static void write_codec_fourcc(AVIOContext *pb, enum AVCodecID codec_id) | |
| 497 | { | ||
| 498 | 
        8/11✓ Branch 0 taken 75 times. 
          ✓ Branch 1 taken 78 times. 
          ✓ Branch 2 taken 18 times. 
          ✗ Branch 3 not taken. 
          ✓ Branch 4 taken 49 times. 
          ✗ Branch 5 not taken. 
          ✓ Branch 6 taken 39 times. 
          ✓ Branch 7 taken 155 times. 
          ✓ Branch 8 taken 104 times. 
          ✓ Branch 9 taken 50 times. 
          ✗ Branch 10 not taken. 
         | 
      568 | switch (codec_id) { | 
| 499 | 75 | case AV_CODEC_ID_AAC: | |
| 500 | 75 | avio_write(pb, "mp4a", 4); | |
| 501 | 75 | return; | |
| 502 | 78 | case AV_CODEC_ID_OPUS: | |
| 503 | 78 | avio_write(pb, "Opus", 4); | |
| 504 | 78 | return; | |
| 505 | 18 | case AV_CODEC_ID_FLAC: | |
| 506 | 18 | avio_write(pb, "fLaC", 4); | |
| 507 | 18 | return; | |
| 508 | ✗ | case AV_CODEC_ID_MP3: | |
| 509 | ✗ | avio_write(pb, ".mp3", 4); | |
| 510 | ✗ | return; | |
| 511 | 49 | case AV_CODEC_ID_AC3: | |
| 512 | 49 | avio_write(pb, "ac-3", 4); | |
| 513 | 49 | return; | |
| 514 | ✗ | case AV_CODEC_ID_EAC3: | |
| 515 | ✗ | avio_write(pb, "ec-3", 4); | |
| 516 | ✗ | return; | |
| 517 | 39 | case AV_CODEC_ID_H264: | |
| 518 | 39 | avio_write(pb, "avc1", 4); | |
| 519 | 39 | return; | |
| 520 | 155 | case AV_CODEC_ID_HEVC: | |
| 521 | 155 | avio_write(pb, "hvc1", 4); | |
| 522 | 155 | return; | |
| 523 | 104 | case AV_CODEC_ID_AV1: | |
| 524 | 104 | avio_write(pb, "av01", 4); | |
| 525 | 104 | return; | |
| 526 | 50 | case AV_CODEC_ID_VP9: | |
| 527 | 50 | avio_write(pb, "vp09", 4); | |
| 528 | 50 | return; | |
| 529 | ✗ | default: | |
| 530 | ✗ | av_log(NULL, AV_LOG_ERROR, "Invalid codec FourCC write requested.\n"); | |
| 531 | ✗ | av_assert0(0); | |
| 532 | } | ||
| 533 | } | ||
| 534 | |||
| 535 | 749 | static void flv_write_metadata_packet(AVFormatContext *s, AVCodecParameters *par, unsigned int ts, int stream_idx) | |
| 536 | { | ||
| 537 | 749 | AVIOContext *pb = s->pb; | |
| 538 | 749 | FLVContext *flv = s->priv_data; | |
| 539 | 749 | AVContentLightMetadata *lightMetadata = NULL; | |
| 540 | 749 | AVMasteringDisplayMetadata *displayMetadata = NULL; | |
| 541 | 749 | int64_t metadata_size_pos = 0; | |
| 542 | 749 | int64_t total_size = 0; | |
| 543 | 749 | const AVPacketSideData *side_data = NULL; | |
| 544 | |||
| 545 | 
        2/2✓ Branch 0 taken 285 times. 
          ✓ Branch 1 taken 464 times. 
         | 
      749 | if (flv->metadata_pkt_written[stream_idx]) | 
| 546 | 285 | return; | |
| 547 | |||
| 548 | 
        4/4✓ Branch 0 taken 460 times. 
          ✓ Branch 1 taken 4 times. 
          ✓ Branch 2 taken 458 times. 
          ✓ Branch 3 taken 2 times. 
         | 
      464 | if (par->codec_id == AV_CODEC_ID_HEVC || par->codec_id == AV_CODEC_ID_AV1 || | 
| 549 | 
        2/2✓ Branch 0 taken 2 times. 
          ✓ Branch 1 taken 456 times. 
         | 
      458 | par->codec_id == AV_CODEC_ID_VP9) { | 
| 550 | 8 | int flags_size = 5; | |
| 551 | 8 | side_data = av_packet_side_data_get(par->coded_side_data, par->nb_coded_side_data, | |
| 552 | AV_PKT_DATA_CONTENT_LIGHT_LEVEL); | ||
| 553 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 7 times. 
         | 
      8 | if (side_data) | 
| 554 | 1 | lightMetadata = (AVContentLightMetadata *)side_data->data; | |
| 555 | |||
| 556 | 8 | side_data = av_packet_side_data_get(par->coded_side_data, par->nb_coded_side_data, | |
| 557 | AV_PKT_DATA_MASTERING_DISPLAY_METADATA); | ||
| 558 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 7 times. 
         | 
      8 | if (side_data) | 
| 559 | 1 | displayMetadata = (AVMasteringDisplayMetadata *)side_data->data; | |
| 560 | |||
| 561 | /* | ||
| 562 | * Reference Enhancing FLV | ||
| 563 | * https://github.com/veovera/enhanced-rtmp/blob/main/enhanced-rtmp.pdf | ||
| 564 | * */ | ||
| 565 | 8 | avio_w8(pb, FLV_TAG_TYPE_VIDEO); //write video tag type | |
| 566 | 8 | metadata_size_pos = avio_tell(pb); | |
| 567 | 8 | avio_wb24(pb, 0 + flags_size); | |
| 568 | 8 | put_timestamp(pb, ts); //ts = pkt->dts, gen | |
| 569 | 8 | avio_wb24(pb, flv->reserved); | |
| 570 | |||
| 571 | 8 | avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeMetadata | FLV_FRAME_VIDEO_INFO_CMD); // ExVideoTagHeader mode with PacketTypeMetadata | |
| 572 | 8 | write_codec_fourcc(pb, par->codec_id); | |
| 573 | |||
| 574 | 8 | avio_w8(pb, AMF_DATA_TYPE_STRING); | |
| 575 | 8 | put_amf_string(pb, "colorInfo"); | |
| 576 | |||
| 577 | 8 | avio_w8(pb, AMF_DATA_TYPE_OBJECT); | |
| 578 | |||
| 579 | 8 | put_amf_string(pb, "colorConfig"); // colorConfig | |
| 580 | |||
| 581 | 8 | avio_w8(pb, AMF_DATA_TYPE_OBJECT); | |
| 582 | |||
| 583 | 
        2/2✓ Branch 0 taken 3 times. 
          ✓ Branch 1 taken 5 times. 
         | 
      8 | if (par->color_trc != AVCOL_TRC_UNSPECIFIED && | 
| 584 | 
        1/2✓ Branch 0 taken 3 times. 
          ✗ Branch 1 not taken. 
         | 
      3 | par->color_trc < AVCOL_TRC_NB) { | 
| 585 | 3 | put_amf_string(pb, "transferCharacteristics"); // color_trc | |
| 586 | 3 | put_amf_double(pb, par->color_trc); | |
| 587 | } | ||
| 588 | |||
| 589 | 
        2/2✓ Branch 0 taken 6 times. 
          ✓ Branch 1 taken 2 times. 
         | 
      8 | if (par->color_space != AVCOL_SPC_UNSPECIFIED && | 
| 590 | 
        1/2✓ Branch 0 taken 6 times. 
          ✗ Branch 1 not taken. 
         | 
      6 | par->color_space < AVCOL_SPC_NB) { | 
| 591 | 6 | put_amf_string(pb, "matrixCoefficients"); // colorspace | |
| 592 | 6 | put_amf_double(pb, par->color_space); | |
| 593 | } | ||
| 594 | |||
| 595 | 
        2/2✓ Branch 0 taken 3 times. 
          ✓ Branch 1 taken 5 times. 
         | 
      8 | if (par->color_primaries != AVCOL_PRI_UNSPECIFIED && | 
| 596 | 
        1/2✓ Branch 0 taken 3 times. 
          ✗ Branch 1 not taken. 
         | 
      3 | par->color_primaries < AVCOL_PRI_NB) { | 
| 597 | 3 | put_amf_string(pb, "colorPrimaries"); // color_primaries | |
| 598 | 3 | put_amf_double(pb, par->color_primaries); | |
| 599 | } | ||
| 600 | |||
| 601 | 8 | put_amf_string(pb, ""); | |
| 602 | 8 | avio_w8(pb, AMF_END_OF_OBJECT); | |
| 603 | |||
| 604 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 7 times. 
         | 
      8 | if (lightMetadata) { | 
| 605 | 1 | put_amf_string(pb, "hdrCll"); | |
| 606 | 1 | avio_w8(pb, AMF_DATA_TYPE_OBJECT); | |
| 607 | |||
| 608 | 1 | put_amf_string(pb, "maxFall"); | |
| 609 | 1 | put_amf_double(pb, lightMetadata->MaxFALL); | |
| 610 | |||
| 611 | 1 | put_amf_string(pb, "maxCLL"); | |
| 612 | 1 | put_amf_double(pb, lightMetadata->MaxCLL); | |
| 613 | |||
| 614 | 1 | put_amf_string(pb, ""); | |
| 615 | 1 | avio_w8(pb, AMF_END_OF_OBJECT); | |
| 616 | } | ||
| 617 | |||
| 618 | 
        3/6✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 7 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 1 times. 
          ✗ Branch 4 not taken. 
          ✗ Branch 5 not taken. 
         | 
      8 | if (displayMetadata && (displayMetadata->has_primaries || displayMetadata->has_luminance)) { | 
| 619 | 1 | put_amf_string(pb, "hdrMdcv"); | |
| 620 | 1 | avio_w8(pb, AMF_DATA_TYPE_OBJECT); | |
| 621 | 
        1/2✓ Branch 0 taken 1 times. 
          ✗ Branch 1 not taken. 
         | 
      1 | if (displayMetadata->has_primaries) { | 
| 622 | 1 | put_amf_string(pb, "redX"); | |
| 623 | 1 | put_amf_double(pb, av_q2d(displayMetadata->display_primaries[0][0])); | |
| 624 | |||
| 625 | 1 | put_amf_string(pb, "redY"); | |
| 626 | 1 | put_amf_double(pb, av_q2d(displayMetadata->display_primaries[0][1])); | |
| 627 | |||
| 628 | 1 | put_amf_string(pb, "greenX"); | |
| 629 | 1 | put_amf_double(pb, av_q2d(displayMetadata->display_primaries[1][0])); | |
| 630 | |||
| 631 | 1 | put_amf_string(pb, "greenY"); | |
| 632 | 1 | put_amf_double(pb, av_q2d(displayMetadata->display_primaries[1][1])); | |
| 633 | |||
| 634 | 1 | put_amf_string(pb, "blueX"); | |
| 635 | 1 | put_amf_double(pb, av_q2d(displayMetadata->display_primaries[2][0])); | |
| 636 | |||
| 637 | 1 | put_amf_string(pb, "blueY"); | |
| 638 | 1 | put_amf_double(pb, av_q2d(displayMetadata->display_primaries[2][1])); | |
| 639 | |||
| 640 | 1 | put_amf_string(pb, "whitePointX"); | |
| 641 | 1 | put_amf_double(pb, av_q2d(displayMetadata->white_point[0])); | |
| 642 | |||
| 643 | 1 | put_amf_string(pb, "whitePointY"); | |
| 644 | 1 | put_amf_double(pb, av_q2d(displayMetadata->white_point[1])); | |
| 645 | } | ||
| 646 | 
        1/2✓ Branch 0 taken 1 times. 
          ✗ Branch 1 not taken. 
         | 
      1 | if (displayMetadata->has_luminance) { | 
| 647 | 1 | put_amf_string(pb, "maxLuminance"); | |
| 648 | 1 | put_amf_double(pb, av_q2d(displayMetadata->max_luminance)); | |
| 649 | |||
| 650 | 1 | put_amf_string(pb, "minLuminance"); | |
| 651 | 1 | put_amf_double(pb, av_q2d(displayMetadata->min_luminance)); | |
| 652 | } | ||
| 653 | 1 | put_amf_string(pb, ""); | |
| 654 | 1 | avio_w8(pb, AMF_END_OF_OBJECT); | |
| 655 | } | ||
| 656 | 8 | put_amf_string(pb, ""); | |
| 657 | 8 | avio_w8(pb, AMF_END_OF_OBJECT); | |
| 658 | |||
| 659 | 8 | total_size = avio_tell(pb) - metadata_size_pos - 10; | |
| 660 | 8 | avio_seek(pb, metadata_size_pos, SEEK_SET); | |
| 661 | 8 | avio_wb24(pb, total_size); | |
| 662 | 8 | avio_skip(pb, total_size + 10 - 3); | |
| 663 | 8 | avio_wb32(pb, total_size + 11); // previous tag size | |
| 664 | 8 | flv->metadata_pkt_written[stream_idx] = 1; | |
| 665 | } | ||
| 666 | } | ||
| 667 | |||
| 668 | ✗ | static int unsupported_codec(AVFormatContext *s, | |
| 669 | const char* type, int codec_id) | ||
| 670 | { | ||
| 671 | ✗ | const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id); | |
| 672 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 673 | "%s codec %s not compatible with flv\n", | ||
| 674 | type, | ||
| 675 | desc ? desc->name : "unknown"); | ||
| 676 | ✗ | return AVERROR(ENOSYS); | |
| 677 | } | ||
| 678 | |||
| 679 | 3 | static void flv_write_aac_header(AVFormatContext* s, AVCodecParameters* par) | |
| 680 | { | ||
| 681 | 3 | AVIOContext *pb = s->pb; | |
| 682 | 3 | FLVContext *flv = s->priv_data; | |
| 683 | |||
| 684 | 
        1/4✗ Branch 0 not taken. 
          ✓ Branch 1 taken 3 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
         | 
      3 | if (!par->extradata_size && (flv->flags & FLV_AAC_SEQ_HEADER_DETECT)) { | 
| 685 | PutBitContext pbc; | ||
| 686 | int samplerate_index; | ||
| 687 | ✗ | int channels = par->ch_layout.nb_channels | |
| 688 | ✗ | - (par->ch_layout.nb_channels == 8 ? 1 : 0); | |
| 689 | uint8_t data[2]; | ||
| 690 | |||
| 691 | ✗ | for (samplerate_index = 0; samplerate_index < 16; | |
| 692 | ✗ | samplerate_index++) | |
| 693 | ✗ | if (par->sample_rate | |
| 694 | ✗ | == ff_mpeg4audio_sample_rates[samplerate_index]) | |
| 695 | ✗ | break; | |
| 696 | |||
| 697 | ✗ | init_put_bits(&pbc, data, sizeof(data)); | |
| 698 | ✗ | put_bits(&pbc, 5, par->profile + 1); //profile | |
| 699 | ✗ | put_bits(&pbc, 4, samplerate_index); //sample rate index | |
| 700 | ✗ | put_bits(&pbc, 4, channels); | |
| 701 | ✗ | put_bits(&pbc, 1, 0); //frame length - 1024 samples | |
| 702 | ✗ | put_bits(&pbc, 1, 0); //does not depend on core coder | |
| 703 | ✗ | put_bits(&pbc, 1, 0); //is not extension | |
| 704 | ✗ | flush_put_bits(&pbc); | |
| 705 | |||
| 706 | ✗ | avio_w8(pb, data[0]); | |
| 707 | ✗ | avio_w8(pb, data[1]); | |
| 708 | |||
| 709 | ✗ | av_log(s, AV_LOG_WARNING, "AAC sequence header: %02x %02x.\n", | |
| 710 | ✗ | data[0], data[1]); | |
| 711 | } | ||
| 712 | 3 | avio_write(pb, par->extradata, par->extradata_size); | |
| 713 | 3 | } | |
| 714 | |||
| 715 | 5 | static void flv_write_multichannel_body(AVFormatContext* s, AVCodecParameters* par) | |
| 716 | { | ||
| 717 | 5 | AVIOContext *pb = s->pb; | |
| 718 | |||
| 719 | 
        1/3✓ Branch 0 taken 5 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
         | 
      5 | switch (par->ch_layout.order) { | 
| 720 | 5 | case AV_CHANNEL_ORDER_NATIVE: | |
| 721 | 5 | avio_w8(pb, AudioChannelOrderNative); | |
| 722 | 5 | break; | |
| 723 | ✗ | case AV_CHANNEL_ORDER_CUSTOM: | |
| 724 | ✗ | avio_w8(pb, AudioChannelOrderCustom); | |
| 725 | ✗ | break; | |
| 726 | ✗ | default: | |
| 727 | ✗ | avio_w8(pb, AudioChannelOrderUnspecified); | |
| 728 | ✗ | break; | |
| 729 | } | ||
| 730 | |||
| 731 | 5 | avio_w8(pb, par->ch_layout.nb_channels); | |
| 732 | |||
| 733 | 
        1/2✓ Branch 0 taken 5 times. 
          ✗ Branch 1 not taken. 
         | 
      5 | if (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE) { | 
| 734 | // The first 18 entries are identical between FFmpeg and flv | ||
| 735 | 5 | uint32_t mask = par->ch_layout.u.mask & 0x03FFFF; | |
| 736 | // The remaining 6 flv entries are in the right order, but start at AV_CHAN_LOW_FREQUENCY_2 | ||
| 737 | 5 | mask |= (par->ch_layout.u.mask >> (AV_CHAN_LOW_FREQUENCY_2 - 18)) & 0xFC0000; | |
| 738 | |||
| 739 | 5 | avio_wb32(pb, mask); | |
| 740 | ✗ | } else if (par->ch_layout.order == AV_CHANNEL_ORDER_CUSTOM) { | |
| 741 | ✗ | for (int i = 0; i < par->ch_layout.nb_channels; i++) { | |
| 742 | ✗ | enum AVChannel id = par->ch_layout.u.map[i].id; | |
| 743 | ✗ | if (id >= AV_CHAN_FRONT_LEFT && id <= AV_CHAN_TOP_BACK_RIGHT) { | |
| 744 | ✗ | avio_w8(pb, id - AV_CHAN_FRONT_LEFT + 0); | |
| 745 | ✗ | } else if (id >= AV_CHAN_LOW_FREQUENCY_2 && id <= AV_CHAN_BOTTOM_FRONT_RIGHT) { | |
| 746 | ✗ | avio_w8(pb, id - AV_CHAN_LOW_FREQUENCY_2 + 18); | |
| 747 | ✗ | } else if (id == AV_CHAN_UNUSED) { | |
| 748 | ✗ | avio_w8(pb, 0xFE); | |
| 749 | } else { | ||
| 750 | ✗ | avio_w8(pb, 0xFF); // unknown | |
| 751 | } | ||
| 752 | } | ||
| 753 | } | ||
| 754 | 5 | } | |
| 755 | |||
| 756 | 5 | static int flv_get_multichannel_body_size(AVCodecParameters* par) | |
| 757 | { | ||
| 758 | 5 | int res = 2; | |
| 759 | |||
| 760 | 
        1/2✓ Branch 0 taken 5 times. 
          ✗ Branch 1 not taken. 
         | 
      5 | if (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE) | 
| 761 | 5 | res += 4; | |
| 762 | ✗ | else if (par->ch_layout.order == AV_CHANNEL_ORDER_CUSTOM) | |
| 763 | ✗ | res += par->ch_layout.nb_channels; | |
| 764 | |||
| 765 | 5 | return res; | |
| 766 | } | ||
| 767 | |||
| 768 | 5 | static void flv_write_multichannel_header(AVFormatContext* s, AVCodecParameters* par, int64_t ts, int stream_index) | |
| 769 | { | ||
| 770 | 5 | AVIOContext *pb = s->pb; | |
| 771 | 5 | FLVContext *flv = s->priv_data; | |
| 772 | |||
| 773 | 5 | int track_idx = flv->track_idx_map[stream_index]; | |
| 774 | 5 | int data_size = flv_get_multichannel_body_size(par); | |
| 775 | 
        2/2✓ Branch 0 taken 4 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      5 | if (track_idx) | 
| 776 | 4 | data_size += 2; | |
| 777 | |||
| 778 | 5 | avio_w8(pb, FLV_TAG_TYPE_AUDIO); | |
| 779 | 5 | avio_wb24(pb, data_size + 5); // size | |
| 780 | 5 | put_timestamp(pb, ts); | |
| 781 | 5 | avio_wb24(pb, 0); // streamid | |
| 782 | |||
| 783 | 
        2/2✓ Branch 0 taken 4 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      5 | if (track_idx) { | 
| 784 | 4 | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeMultitrack); | |
| 785 | 4 | avio_w8(pb, MultitrackTypeOneTrack | AudioPacketTypeMultichannelConfig); | |
| 786 | } else { | ||
| 787 | 1 | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeMultichannelConfig); | |
| 788 | } | ||
| 789 | |||
| 790 | 5 | write_codec_fourcc(pb, par->codec_id); | |
| 791 | |||
| 792 | 
        2/2✓ Branch 0 taken 4 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      5 | if (track_idx) | 
| 793 | 4 | avio_w8(pb, track_idx); | |
| 794 | |||
| 795 | 5 | flv_write_multichannel_body(s, par); | |
| 796 | |||
| 797 | 5 | avio_wb32(pb, data_size + 5 + 11); // previous tag size | |
| 798 | 5 | } | |
| 799 | |||
| 800 | 33 | static void flv_write_codec_header(AVFormatContext* s, AVCodecParameters* par, int64_t ts, int stream_index) { | |
| 801 | int64_t data_size; | ||
| 802 | 33 | AVIOContext *pb = s->pb; | |
| 803 | 33 | FLVContext *flv = s->priv_data; | |
| 804 | 33 | int track_idx = flv->track_idx_map[stream_index]; | |
| 805 | 33 | int extended_flv = 0; | |
| 806 | |||
| 807 | 
        4/4✓ Branch 0 taken 30 times. 
          ✓ Branch 1 taken 3 times. 
          ✓ Branch 2 taken 28 times. 
          ✓ Branch 3 taken 2 times. 
         | 
      33 | if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264 | 
| 808 | 
        3/4✓ Branch 0 taken 28 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 24 times. 
          ✓ Branch 3 taken 4 times. 
         | 
      28 | || par->codec_id == AV_CODEC_ID_MPEG4 || par->codec_id == AV_CODEC_ID_HEVC | 
| 809 | 
        4/4✓ Branch 0 taken 22 times. 
          ✓ Branch 1 taken 2 times. 
          ✓ Branch 2 taken 20 times. 
          ✓ Branch 3 taken 2 times. 
         | 
      24 | || par->codec_id == AV_CODEC_ID_AV1 || par->codec_id == AV_CODEC_ID_VP9 | 
| 810 | 
        1/4✗ Branch 0 not taken. 
          ✓ Branch 1 taken 20 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
         | 
      20 | || (par->codec_id == AV_CODEC_ID_MP3 && track_idx) | 
| 811 | 
        4/4✓ Branch 0 taken 19 times. 
          ✓ Branch 1 taken 1 times. 
          ✓ Branch 2 taken 18 times. 
          ✓ Branch 3 taken 1 times. 
         | 
      20 | || par->codec_id == AV_CODEC_ID_OPUS || par->codec_id == AV_CODEC_ID_FLAC | 
| 812 | 
        3/4✓ Branch 0 taken 17 times. 
          ✓ Branch 1 taken 1 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 17 times. 
         | 
      18 | || par->codec_id == AV_CODEC_ID_AC3 || par->codec_id == AV_CODEC_ID_EAC3) { | 
| 813 | int64_t pos; | ||
| 814 | 16 | avio_w8(pb, | |
| 815 | 
        2/2✓ Branch 0 taken 10 times. 
          ✓ Branch 1 taken 6 times. 
         | 
      16 | par->codec_type == AVMEDIA_TYPE_VIDEO ? | 
| 816 | FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO); | ||
| 817 | 16 | avio_wb24(pb, 0); // size patched later | |
| 818 | 16 | put_timestamp(pb, ts); | |
| 819 | 16 | avio_wb24(pb, 0); // streamid | |
| 820 | 16 | pos = avio_tell(pb); | |
| 821 | 
        2/2✓ Branch 0 taken 6 times. 
          ✓ Branch 1 taken 10 times. 
         | 
      16 | if (par->codec_type == AVMEDIA_TYPE_AUDIO) { | 
| 822 | 
        2/2✓ Branch 0 taken 2 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      3 | extended_flv = (par->codec_id == AV_CODEC_ID_AAC && track_idx) | 
| 823 | 
        1/4✗ Branch 0 not taken. 
          ✓ Branch 1 taken 5 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
         | 
      5 | || (par->codec_id == AV_CODEC_ID_MP3 && track_idx) | 
| 824 | 
        2/2✓ Branch 0 taken 4 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      5 | || par->codec_id == AV_CODEC_ID_OPUS | 
| 825 | 
        2/2✓ Branch 0 taken 3 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      4 | || par->codec_id == AV_CODEC_ID_FLAC | 
| 826 | 
        2/2✓ Branch 0 taken 2 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      3 | || par->codec_id == AV_CODEC_ID_AC3 | 
| 827 | 
        3/4✓ Branch 0 taken 3 times. 
          ✓ Branch 1 taken 3 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 2 times. 
         | 
      12 | || par->codec_id == AV_CODEC_ID_EAC3; | 
| 828 | |||
| 829 | 
        2/2✓ Branch 0 taken 4 times. 
          ✓ Branch 1 taken 2 times. 
         | 
      6 | if (extended_flv) { | 
| 830 | 
        1/2✓ Branch 0 taken 4 times. 
          ✗ Branch 1 not taken. 
         | 
      4 | if (track_idx) { | 
| 831 | 4 | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeMultitrack); | |
| 832 | 4 | avio_w8(pb, MultitrackTypeOneTrack | AudioPacketTypeSequenceStart); | |
| 833 | } else { | ||
| 834 | ✗ | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeSequenceStart); | |
| 835 | } | ||
| 836 | |||
| 837 | 4 | write_codec_fourcc(pb, par->codec_id); | |
| 838 | |||
| 839 | 
        1/2✓ Branch 0 taken 4 times. 
          ✗ Branch 1 not taken. 
         | 
      4 | if (track_idx) | 
| 840 | 4 | avio_w8(pb, track_idx); | |
| 841 | |||
| 842 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 3 times. 
         | 
      4 | if (par->codec_id == AV_CODEC_ID_AAC) { | 
| 843 | 1 | flv_write_aac_header(s, par); | |
| 844 | 
        4/4✓ Branch 0 taken 2 times. 
          ✓ Branch 1 taken 1 times. 
          ✓ Branch 2 taken 1 times. 
          ✓ Branch 3 taken 1 times. 
         | 
      3 | } else if (par->codec_id == AV_CODEC_ID_OPUS || par->codec_id == AV_CODEC_ID_FLAC) { | 
| 845 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 2 times. 
         | 
      2 | av_assert0(par->extradata_size); | 
| 846 | 2 | avio_write(pb, par->extradata, par->extradata_size); | |
| 847 | } | ||
| 848 | 
        1/2✓ Branch 0 taken 2 times. 
          ✗ Branch 1 not taken. 
         | 
      2 | } else if (par->codec_id == AV_CODEC_ID_AAC) { | 
| 849 | 2 | avio_w8(pb, get_audio_flags(s, par)); | |
| 850 | 2 | avio_w8(pb, 0); // AAC sequence header | |
| 851 | |||
| 852 | 2 | flv_write_aac_header(s, par); | |
| 853 | } | ||
| 854 | } else { | ||
| 855 | // If video stream has track_idx > 0 we need to send H.264 as extended video packet | ||
| 856 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      2 | extended_flv = (par->codec_id == AV_CODEC_ID_H264 && track_idx) || | 
| 857 | 
        2/2✓ Branch 0 taken 5 times. 
          ✓ Branch 1 taken 4 times. 
         | 
      9 | par->codec_id == AV_CODEC_ID_HEVC || | 
| 858 | 
        4/4✓ Branch 0 taken 2 times. 
          ✓ Branch 1 taken 8 times. 
          ✓ Branch 2 taken 3 times. 
          ✓ Branch 3 taken 2 times. 
         | 
      23 | par->codec_id == AV_CODEC_ID_AV1 || | 
| 859 | 
        2/2✓ Branch 0 taken 2 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      3 | par->codec_id == AV_CODEC_ID_VP9; | 
| 860 | |||
| 861 | 
        2/2✓ Branch 0 taken 9 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      10 | if (extended_flv) { | 
| 862 | 
        2/2✓ Branch 0 taken 4 times. 
          ✓ Branch 1 taken 5 times. 
         | 
      9 | if (track_idx) { | 
| 863 | 4 | avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeMultitrack | FLV_FRAME_KEY); | |
| 864 | 4 | avio_w8(pb, MultitrackTypeOneTrack | PacketTypeSequenceStart); | |
| 865 | } else { | ||
| 866 | 5 | avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeSequenceStart | FLV_FRAME_KEY); | |
| 867 | } | ||
| 868 | |||
| 869 | 9 | write_codec_fourcc(pb, par->codec_id); | |
| 870 | |||
| 871 | 
        2/2✓ Branch 0 taken 4 times. 
          ✓ Branch 1 taken 5 times. 
         | 
      9 | if (track_idx) | 
| 872 | 4 | avio_w8(pb, track_idx); | |
| 873 | } else { | ||
| 874 | 1 | avio_w8(pb, par->codec_tag | FLV_FRAME_KEY); // flags | |
| 875 | 1 | avio_w8(pb, 0); // AVC sequence header | |
| 876 | 1 | avio_wb24(pb, 0); // composition time | |
| 877 | } | ||
| 878 | |||
| 879 | 
        2/2✓ Branch 0 taken 4 times. 
          ✓ Branch 1 taken 6 times. 
         | 
      10 | if (par->codec_id == AV_CODEC_ID_HEVC) | 
| 880 | 4 | ff_isom_write_hvcc(pb, par->extradata, par->extradata_size, 0, s); | |
| 881 | 
        2/2✓ Branch 0 taken 2 times. 
          ✓ Branch 1 taken 4 times. 
         | 
      6 | else if (par->codec_id == AV_CODEC_ID_AV1) | 
| 882 | 2 | ff_isom_write_av1c(pb, par->extradata, par->extradata_size, 1); | |
| 883 | 
        2/2✓ Branch 0 taken 2 times. 
          ✓ Branch 1 taken 2 times. 
         | 
      4 | else if (par->codec_id == AV_CODEC_ID_VP9) | 
| 884 | 2 | ff_isom_write_vpcc(s, pb, par->extradata, par->extradata_size, par); | |
| 885 | 
        1/2✓ Branch 0 taken 2 times. 
          ✗ Branch 1 not taken. 
         | 
      2 | else if (par->codec_id == AV_CODEC_ID_H264) | 
| 886 | 2 | ff_isom_write_avcc(pb, par->extradata, par->extradata_size); | |
| 887 | ✗ | else if (par->codec_id == AV_CODEC_ID_MPEG4) | |
| 888 | ✗ | avio_write(pb, par->extradata, par->extradata_size); | |
| 889 | else | ||
| 890 | ✗ | av_assert0(0); | |
| 891 | } | ||
| 892 | 16 | data_size = avio_tell(pb) - pos; | |
| 893 | 16 | avio_seek(pb, -data_size - 10, SEEK_CUR); | |
| 894 | 16 | avio_wb24(pb, data_size); | |
| 895 | 16 | avio_skip(pb, data_size + 10 - 3); | |
| 896 | 16 | avio_wb32(pb, data_size + 11); // previous tag size | |
| 897 | } | ||
| 898 | |||
| 899 | 
        6/6✓ Branch 0 taken 9 times. 
          ✓ Branch 1 taken 24 times. 
          ✓ Branch 2 taken 5 times. 
          ✓ Branch 3 taken 4 times. 
          ✓ Branch 4 taken 2 times. 
          ✓ Branch 5 taken 3 times. 
         | 
      38 | if (par->codec_type == AVMEDIA_TYPE_AUDIO && (extended_flv || | 
| 900 | 
        2/2✓ Branch 1 taken 1 times. 
          ✓ Branch 2 taken 1 times. 
         | 
      7 | (av_channel_layout_compare(&par->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO) == 1 && | 
| 901 | 2 | av_channel_layout_compare(&par->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_MONO) == 1))) | |
| 902 | 5 | flv_write_multichannel_header(s, par, ts, stream_index); | |
| 903 | 33 | } | |
| 904 | |||
| 905 | 20 | static int flv_append_keyframe_info(AVFormatContext *s, FLVContext *flv, double ts, int64_t pos) | |
| 906 | { | ||
| 907 | 20 | FLVFileposition *position = av_malloc(sizeof(FLVFileposition)); | |
| 908 | |||
| 909 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 20 times. 
         | 
      20 | if (!position) { | 
| 910 | ✗ | av_log(s, AV_LOG_WARNING, "no mem for add keyframe index!\n"); | |
| 911 | ✗ | return AVERROR(ENOMEM); | |
| 912 | } | ||
| 913 | |||
| 914 | 20 | position->keyframe_timestamp = ts; | |
| 915 | 20 | position->keyframe_position = pos; | |
| 916 | |||
| 917 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 19 times. 
         | 
      20 | if (!flv->filepositions_count) { | 
| 918 | 1 | flv->filepositions = position; | |
| 919 | 1 | flv->head_filepositions = flv->filepositions; | |
| 920 | 1 | position->next = NULL; | |
| 921 | } else { | ||
| 922 | 19 | flv->filepositions->next = position; | |
| 923 | 19 | position->next = NULL; | |
| 924 | 19 | flv->filepositions = flv->filepositions->next; | |
| 925 | } | ||
| 926 | |||
| 927 | 20 | flv->filepositions_count++; | |
| 928 | |||
| 929 | 20 | return 0; | |
| 930 | } | ||
| 931 | |||
| 932 | 1 | static int shift_data(AVFormatContext *s) | |
| 933 | { | ||
| 934 | int ret; | ||
| 935 | 1 | int64_t metadata_size = 0; | |
| 936 | 1 | FLVContext *flv = s->priv_data; | |
| 937 | |||
| 938 | 1 | metadata_size = flv->filepositions_count * 9 * 2 + 10; /* filepositions and times value */ | |
| 939 | 1 | metadata_size += 2 + 13; /* filepositions String */ | |
| 940 | 1 | metadata_size += 2 + 5; /* times String */ | |
| 941 | 1 | metadata_size += 3; /* Object end */ | |
| 942 | |||
| 943 | 1 | flv->keyframe_index_size = metadata_size; | |
| 944 | |||
| 945 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1 times. 
         | 
      1 | if (metadata_size < 0) | 
| 946 | ✗ | return metadata_size; | |
| 947 | |||
| 948 | 1 | ret = ff_format_shift_data(s, flv->keyframes_info_offset, metadata_size); | |
| 949 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1 times. 
         | 
      1 | if (ret < 0) | 
| 950 | ✗ | return ret; | |
| 951 | |||
| 952 | 1 | avio_seek(s->pb, flv->metadata_size_pos, SEEK_SET); | |
| 953 | 1 | avio_wb24(s->pb, flv->metadata_totalsize + metadata_size); | |
| 954 | |||
| 955 | 1 | avio_seek(s->pb, flv->metadata_totalsize_pos + metadata_size, SEEK_SET); | |
| 956 | 1 | avio_wb32(s->pb, flv->metadata_totalsize + 11 + metadata_size); | |
| 957 | |||
| 958 | 1 | return 0; | |
| 959 | } | ||
| 960 | |||
| 961 | 23 | static int flv_init(struct AVFormatContext *s) | |
| 962 | { | ||
| 963 | int i; | ||
| 964 | 23 | int video_ctr = 0, audio_ctr = 0; | |
| 965 | 23 | FLVContext *flv = s->priv_data; | |
| 966 | |||
| 967 | 23 | flv->last_ts = av_calloc(s->nb_streams, sizeof(*flv->last_ts)); | |
| 968 | 23 | flv->metadata_pkt_written = av_calloc(s->nb_streams, sizeof(*flv->metadata_pkt_written)); | |
| 969 | 23 | flv->track_idx_map = av_calloc(s->nb_streams, sizeof(*flv->track_idx_map)); | |
| 970 | 
        3/6✓ Branch 0 taken 23 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 23 times. 
          ✗ Branch 3 not taken. 
          ✗ Branch 4 not taken. 
          ✓ Branch 5 taken 23 times. 
         | 
      23 | if (!flv->last_ts || !flv->metadata_pkt_written || !flv->track_idx_map) | 
| 971 | ✗ | return AVERROR(ENOMEM); | |
| 972 | |||
| 973 | 
        2/2✓ Branch 0 taken 33 times. 
          ✓ Branch 1 taken 23 times. 
         | 
      56 | for (i = 0; i < s->nb_streams; i++) { | 
| 974 | 33 | AVCodecParameters *par = s->streams[i]->codecpar; | |
| 975 | |||
| 976 | 
        2/5✓ Branch 0 taken 24 times. 
          ✓ Branch 1 taken 9 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
          ✗ Branch 4 not taken. 
         | 
      33 | switch (par->codec_type) { | 
| 977 | 24 | case AVMEDIA_TYPE_VIDEO: | |
| 978 | 
        2/2✓ Branch 0 taken 4 times. 
          ✓ Branch 1 taken 20 times. 
         | 
      24 | if (video_ctr && | 
| 979 | 
        1/2✓ Branch 0 taken 4 times. 
          ✗ Branch 1 not taken. 
         | 
      4 | par->codec_id != AV_CODEC_ID_VP8 && | 
| 980 | 
        2/2✓ Branch 0 taken 3 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      4 | par->codec_id != AV_CODEC_ID_VP9 && | 
| 981 | 
        2/2✓ Branch 0 taken 2 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      3 | par->codec_id != AV_CODEC_ID_AV1 && | 
| 982 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      2 | par->codec_id != AV_CODEC_ID_H264 && | 
| 983 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1 times. 
         | 
      1 | par->codec_id != AV_CODEC_ID_HEVC) { | 
| 984 | ✗ | av_log(s, AV_LOG_ERROR, "Unsupported multi-track video codec.\n"); | |
| 985 | ✗ | return AVERROR(EINVAL); | |
| 986 | } | ||
| 987 | 
        2/2✓ Branch 0 taken 22 times. 
          ✓ Branch 1 taken 2 times. 
         | 
      24 | if (s->streams[i]->avg_frame_rate.den && | 
| 988 | 
        1/2✓ Branch 0 taken 22 times. 
          ✗ Branch 1 not taken. 
         | 
      22 | s->streams[i]->avg_frame_rate.num) { | 
| 989 | 22 | flv->framerate = av_q2d(s->streams[i]->avg_frame_rate); | |
| 990 | } | ||
| 991 | 24 | flv->track_idx_map[i] = video_ctr++; | |
| 992 | 
        3/4✓ Branch 0 taken 4 times. 
          ✓ Branch 1 taken 20 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 4 times. 
         | 
      24 | if (flv->video_par && flv->flags & FLV_ADD_KEYFRAME_INDEX) { | 
| 993 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 994 | "at most one video stream is supported in flv with keyframe index\n"); | ||
| 995 | ✗ | return AVERROR(EINVAL); | |
| 996 | 
        2/2✓ Branch 0 taken 4 times. 
          ✓ Branch 1 taken 20 times. 
         | 
      24 | } else if (flv->video_par) { | 
| 997 | 4 | av_log(s, AV_LOG_WARNING, | |
| 998 | "more than one video stream is not supported by most flv demuxers.\n"); | ||
| 999 | } | ||
| 1000 | 
        2/2✓ Branch 0 taken 20 times. 
          ✓ Branch 1 taken 4 times. 
         | 
      24 | if (!flv->video_par) | 
| 1001 | 20 | flv->video_par = par; | |
| 1002 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 24 times. 
         | 
      24 | if (!ff_codec_get_tag(flv_video_codec_ids, par->codec_id)) | 
| 1003 | ✗ | return unsupported_codec(s, "Video", par->codec_id); | |
| 1004 | |||
| 1005 | 
        1/2✓ Branch 0 taken 24 times. 
          ✗ Branch 1 not taken. 
         | 
      24 | if (par->codec_id == AV_CODEC_ID_MPEG4 || | 
| 1006 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 24 times. 
         | 
      24 | par->codec_id == AV_CODEC_ID_H263) { | 
| 1007 | ✗ | int error = s->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL; | |
| 1008 | ✗ | av_log(s, error ? AV_LOG_ERROR : AV_LOG_WARNING, | |
| 1009 | "Codec %s is not supported in the official FLV specification,\n", avcodec_get_name(par->codec_id)); | ||
| 1010 | |||
| 1011 | ✗ | if (error) { | |
| 1012 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 1013 | "use vstrict=-1 / -strict -1 to use it anyway.\n"); | ||
| 1014 | ✗ | return AVERROR(EINVAL); | |
| 1015 | } | ||
| 1016 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 24 times. 
         | 
      24 | } else if (par->codec_id == AV_CODEC_ID_VP6) { | 
| 1017 | ✗ | av_log(s, AV_LOG_WARNING, | |
| 1018 | "Muxing VP6 in flv will produce flipped video on playback.\n"); | ||
| 1019 | } | ||
| 1020 | 24 | break; | |
| 1021 | 9 | case AVMEDIA_TYPE_AUDIO: | |
| 1022 | 
        2/2✓ Branch 0 taken 4 times. 
          ✓ Branch 1 taken 5 times. 
         | 
      9 | if (audio_ctr && | 
| 1023 | 
        2/2✓ Branch 0 taken 3 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      4 | par->codec_id != AV_CODEC_ID_AAC && | 
| 1024 | 
        1/2✓ Branch 0 taken 3 times. 
          ✗ Branch 1 not taken. 
         | 
      3 | par->codec_id != AV_CODEC_ID_MP3 && | 
| 1025 | 
        2/2✓ Branch 0 taken 2 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      3 | par->codec_id != AV_CODEC_ID_OPUS && | 
| 1026 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      2 | par->codec_id != AV_CODEC_ID_FLAC && | 
| 1027 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1 times. 
         | 
      1 | par->codec_id != AV_CODEC_ID_AC3 && | 
| 1028 | ✗ | par->codec_id != AV_CODEC_ID_EAC3) { | |
| 1029 | ✗ | av_log(s, AV_LOG_ERROR, "Unsupported multi-track audio codec.\n"); | |
| 1030 | ✗ | return AVERROR(EINVAL); | |
| 1031 | } | ||
| 1032 | 9 | flv->track_idx_map[i] = audio_ctr++; | |
| 1033 | 
        2/2✓ Branch 0 taken 4 times. 
          ✓ Branch 1 taken 5 times. 
         | 
      9 | if (flv->audio_par) | 
| 1034 | 4 | av_log(s, AV_LOG_WARNING, | |
| 1035 | "more than one audio stream is not supported by most flv demuxers.\n"); | ||
| 1036 | else | ||
| 1037 | 5 | flv->audio_par = par; | |
| 1038 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 9 times. 
         | 
      9 | if (get_audio_flags(s, par) < 0) | 
| 1039 | ✗ | return unsupported_codec(s, "Audio", par->codec_id); | |
| 1040 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 9 times. 
         | 
      9 | if (par->codec_id == AV_CODEC_ID_PCM_S16BE) | 
| 1041 | ✗ | av_log(s, AV_LOG_WARNING, | |
| 1042 | "16-bit big-endian audio in flv is valid but most likely unplayable (hardware dependent); use s16le\n"); | ||
| 1043 | 9 | break; | |
| 1044 | ✗ | case AVMEDIA_TYPE_DATA: | |
| 1045 | ✗ | if (par->codec_id != AV_CODEC_ID_TEXT && par->codec_id != AV_CODEC_ID_NONE) | |
| 1046 | ✗ | return unsupported_codec(s, "Data", par->codec_id); | |
| 1047 | ✗ | flv->data_par = par; | |
| 1048 | ✗ | break; | |
| 1049 | ✗ | case AVMEDIA_TYPE_SUBTITLE: | |
| 1050 | ✗ | if (par->codec_id != AV_CODEC_ID_TEXT) { | |
| 1051 | ✗ | av_log(s, AV_LOG_ERROR, "Subtitle codec '%s' for stream %d is not compatible with FLV\n", | |
| 1052 | avcodec_get_name(par->codec_id), i); | ||
| 1053 | ✗ | return AVERROR_INVALIDDATA; | |
| 1054 | } | ||
| 1055 | ✗ | flv->data_par = par; | |
| 1056 | ✗ | break; | |
| 1057 | ✗ | default: | |
| 1058 | ✗ | av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n", | |
| 1059 | av_get_media_type_string(par->codec_type), i); | ||
| 1060 | ✗ | return AVERROR(EINVAL); | |
| 1061 | } | ||
| 1062 | 33 | avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */ | |
| 1063 | 33 | flv->last_ts[i] = -1; | |
| 1064 | } | ||
| 1065 | |||
| 1066 | 23 | flv->delay = AV_NOPTS_VALUE; | |
| 1067 | |||
| 1068 | 23 | return 0; | |
| 1069 | } | ||
| 1070 | |||
| 1071 | 23 | static int flv_write_header(AVFormatContext *s) | |
| 1072 | { | ||
| 1073 | int i; | ||
| 1074 | 23 | AVIOContext *pb = s->pb; | |
| 1075 | 23 | FLVContext *flv = s->priv_data; | |
| 1076 | |||
| 1077 | 23 | avio_write(pb, "FLV", 3); | |
| 1078 | 23 | avio_w8(pb, 1); | |
| 1079 | 
        2/2✓ Branch 0 taken 5 times. 
          ✓ Branch 1 taken 18 times. 
         | 
      23 | avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!flv->audio_par + | 
| 1080 | 23 | FLV_HEADER_FLAG_HASVIDEO * !!flv->video_par); | |
| 1081 | 23 | avio_wb32(pb, 9); | |
| 1082 | 23 | avio_wb32(pb, 0); | |
| 1083 | |||
| 1084 | 
        2/2✓ Branch 0 taken 33 times. 
          ✓ Branch 1 taken 23 times. 
         | 
      56 | for (i = 0; i < s->nb_streams; i++) | 
| 1085 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 33 times. 
         | 
      33 | if (s->streams[i]->codecpar->codec_tag == 5) { | 
| 1086 | ✗ | avio_w8(pb, 8); // message type | |
| 1087 | ✗ | avio_wb24(pb, 0); // include flags | |
| 1088 | ✗ | avio_wb24(pb, 0); // time stamp | |
| 1089 | ✗ | avio_wb32(pb, 0); // reserved | |
| 1090 | ✗ | avio_wb32(pb, 11); // size | |
| 1091 | ✗ | flv->reserved = 5; | |
| 1092 | } | ||
| 1093 | |||
| 1094 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 23 times. 
         | 
      23 | if (flv->flags & FLV_NO_METADATA) { | 
| 1095 | ✗ | pb->seekable = 0; | |
| 1096 | } else { | ||
| 1097 | 23 | write_metadata(s, 0); | |
| 1098 | } | ||
| 1099 | |||
| 1100 | 
        2/2✓ Branch 0 taken 33 times. 
          ✓ Branch 1 taken 23 times. 
         | 
      56 | for (i = 0; i < s->nb_streams; i++) { | 
| 1101 | 33 | flv_write_codec_header(s, s->streams[i]->codecpar, 0, i); | |
| 1102 | } | ||
| 1103 | |||
| 1104 | 23 | flv->datastart_offset = avio_tell(pb); | |
| 1105 | 23 | return 0; | |
| 1106 | } | ||
| 1107 | |||
| 1108 | 23 | static int flv_write_trailer(AVFormatContext *s) | |
| 1109 | { | ||
| 1110 | int64_t file_size; | ||
| 1111 | 23 | AVIOContext *pb = s->pb; | |
| 1112 | 23 | FLVContext *flv = s->priv_data; | |
| 1113 | 23 | int build_keyframes_idx = flv->flags & FLV_ADD_KEYFRAME_INDEX; | |
| 1114 | int i, res; | ||
| 1115 | 23 | int64_t cur_pos = avio_tell(s->pb); | |
| 1116 | |||
| 1117 | 
        2/2✓ Branch 0 taken 22 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      23 | if (build_keyframes_idx) { | 
| 1118 | const FLVFileposition *newflv_posinfo; | ||
| 1119 | |||
| 1120 | 1 | avio_seek(pb, flv->videosize_offset, SEEK_SET); | |
| 1121 | 1 | put_amf_double(pb, flv->videosize); | |
| 1122 | |||
| 1123 | 1 | avio_seek(pb, flv->audiosize_offset, SEEK_SET); | |
| 1124 | 1 | put_amf_double(pb, flv->audiosize); | |
| 1125 | |||
| 1126 | 1 | avio_seek(pb, flv->lasttimestamp_offset, SEEK_SET); | |
| 1127 | 1 | put_amf_double(pb, flv->lasttimestamp); | |
| 1128 | |||
| 1129 | 1 | avio_seek(pb, flv->lastkeyframetimestamp_offset, SEEK_SET); | |
| 1130 | 1 | put_amf_double(pb, flv->lastkeyframetimestamp); | |
| 1131 | |||
| 1132 | 1 | avio_seek(pb, flv->lastkeyframelocation_offset, SEEK_SET); | |
| 1133 | 1 | put_amf_double(pb, flv->lastkeyframelocation + flv->keyframe_index_size); | |
| 1134 | 1 | avio_seek(pb, cur_pos, SEEK_SET); | |
| 1135 | |||
| 1136 | 1 | res = shift_data(s); | |
| 1137 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1 times. 
         | 
      1 | if (res < 0) { | 
| 1138 | ✗ | goto end; | |
| 1139 | } | ||
| 1140 | 1 | avio_seek(pb, flv->keyframes_info_offset, SEEK_SET); | |
| 1141 | 1 | put_amf_string(pb, "filepositions"); | |
| 1142 | 1 | put_amf_dword_array(pb, flv->filepositions_count); | |
| 1143 | 
        2/2✓ Branch 0 taken 20 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      21 | for (newflv_posinfo = flv->head_filepositions; newflv_posinfo; newflv_posinfo = newflv_posinfo->next) { | 
| 1144 | 20 | put_amf_double(pb, newflv_posinfo->keyframe_position + flv->keyframe_index_size); | |
| 1145 | } | ||
| 1146 | |||
| 1147 | 1 | put_amf_string(pb, "times"); | |
| 1148 | 1 | put_amf_dword_array(pb, flv->filepositions_count); | |
| 1149 | 
        2/2✓ Branch 0 taken 20 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      21 | for (newflv_posinfo = flv->head_filepositions; newflv_posinfo; newflv_posinfo = newflv_posinfo->next) { | 
| 1150 | 20 | put_amf_double(pb, newflv_posinfo->keyframe_timestamp); | |
| 1151 | } | ||
| 1152 | |||
| 1153 | 1 | put_amf_string(pb, ""); | |
| 1154 | 1 | avio_w8(pb, AMF_END_OF_OBJECT); | |
| 1155 | |||
| 1156 | 1 | avio_seek(pb, cur_pos + flv->keyframe_index_size, SEEK_SET); | |
| 1157 | } | ||
| 1158 | |||
| 1159 | 22 | end: | |
| 1160 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 23 times. 
         | 
      23 | if (flv->flags & FLV_NO_SEQUENCE_END) { | 
| 1161 | ✗ | av_log(s, AV_LOG_DEBUG, "FLV no sequence end mode open\n"); | |
| 1162 | } else { | ||
| 1163 | /* Add EOS tag */ | ||
| 1164 | 
        2/2✓ Branch 0 taken 33 times. 
          ✓ Branch 1 taken 23 times. 
         | 
      56 | for (i = 0; i < s->nb_streams; i++) { | 
| 1165 | 33 | AVCodecParameters *par = s->streams[i]->codecpar; | |
| 1166 | 
        2/2✓ Branch 0 taken 24 times. 
          ✓ Branch 1 taken 9 times. 
         | 
      33 | if (par->codec_type == AVMEDIA_TYPE_VIDEO && | 
| 1167 | 
        3/4✓ Branch 0 taken 22 times. 
          ✓ Branch 1 taken 2 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 22 times. 
         | 
      24 | (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4)) | 
| 1168 | 2 | put_eos_tag(pb, flv->last_ts[i], par->codec_id); | |
| 1169 | } | ||
| 1170 | } | ||
| 1171 | |||
| 1172 | 23 | file_size = avio_tell(pb); | |
| 1173 | |||
| 1174 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 22 times. 
         | 
      23 | if (build_keyframes_idx) { | 
| 1175 | 1 | flv->datasize = file_size - flv->datastart_offset; | |
| 1176 | 1 | avio_seek(pb, flv->datasize_offset, SEEK_SET); | |
| 1177 | 1 | put_amf_double(pb, flv->datasize); | |
| 1178 | } | ||
| 1179 | 
        1/2✓ Branch 0 taken 23 times. 
          ✗ Branch 1 not taken. 
         | 
      23 | if (!(flv->flags & FLV_NO_METADATA)) { | 
| 1180 | 
        1/2✓ Branch 0 taken 23 times. 
          ✗ Branch 1 not taken. 
         | 
      23 | if (!(flv->flags & FLV_NO_DURATION_FILESIZE)) { | 
| 1181 | /* update information */ | ||
| 1182 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 23 times. 
         | 
      23 | if (avio_seek(pb, flv->duration_offset, SEEK_SET) < 0) { | 
| 1183 | ✗ | av_log(s, AV_LOG_WARNING, "Failed to update header with correct duration.\n"); | |
| 1184 | } else { | ||
| 1185 | 23 | put_amf_double(pb, flv->duration / (double)1000); | |
| 1186 | } | ||
| 1187 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 23 times. 
         | 
      23 | if (avio_seek(pb, flv->filesize_offset, SEEK_SET) < 0) { | 
| 1188 | ✗ | av_log(s, AV_LOG_WARNING, "Failed to update header with correct filesize.\n"); | |
| 1189 | } else { | ||
| 1190 | 23 | put_amf_double(pb, file_size); | |
| 1191 | } | ||
| 1192 | } | ||
| 1193 | } | ||
| 1194 | |||
| 1195 | 23 | return 0; | |
| 1196 | } | ||
| 1197 | |||
| 1198 | 2067 | static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) | |
| 1199 | { | ||
| 1200 | 2067 | AVIOContext *pb = s->pb; | |
| 1201 | 2067 | AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar; | |
| 1202 | 2067 | FLVContext *flv = s->priv_data; | |
| 1203 | unsigned ts; | ||
| 1204 | 2067 | int size = pkt->size; | |
| 1205 | 2067 | uint8_t *data = NULL; | |
| 1206 | 
        2/2✓ Branch 0 taken 1213 times. 
          ✓ Branch 1 taken 854 times. 
         | 
      2067 | uint8_t frametype = pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER; | 
| 1207 | 2067 | int flags = -1, flags_size, ret = 0; | |
| 1208 | 2067 | int64_t cur_offset = avio_tell(pb); | |
| 1209 | 2067 | int track_idx = flv->track_idx_map[pkt->stream_index]; | |
| 1210 | |||
| 1211 | 
        2/2✓ Branch 0 taken 216 times. 
          ✓ Branch 1 taken 72 times. 
         | 
      288 | int extended_audio = (par->codec_id == AV_CODEC_ID_AAC && track_idx) | 
| 1212 | 
        1/4✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1995 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
         | 
      1995 | || (par->codec_id == AV_CODEC_ID_MP3 && track_idx) | 
| 1213 | 
        2/2✓ Branch 0 taken 1919 times. 
          ✓ Branch 1 taken 76 times. 
         | 
      1995 | || par->codec_id == AV_CODEC_ID_OPUS | 
| 1214 | 
        2/2✓ Branch 0 taken 1903 times. 
          ✓ Branch 1 taken 16 times. 
         | 
      1919 | || par->codec_id == AV_CODEC_ID_FLAC | 
| 1215 | 
        2/2✓ Branch 0 taken 1856 times. 
          ✓ Branch 1 taken 47 times. 
         | 
      1903 | || par->codec_id == AV_CODEC_ID_AC3 | 
| 1216 | 
        3/4✓ Branch 0 taken 288 times. 
          ✓ Branch 1 taken 1779 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 1856 times. 
         | 
      4134 | || par->codec_id == AV_CODEC_ID_EAC3; | 
| 1217 | |||
| 1218 | 
        2/2✓ Branch 0 taken 211 times. 
          ✓ Branch 1 taken 1856 times. 
         | 
      2067 | if (extended_audio) | 
| 1219 | 211 | flags_size = 5; | |
| 1220 | 
        2/4✓ Branch 0 taken 1856 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 1856 times. 
          ✗ Branch 3 not taken. 
         | 
      1856 | else if (par->codec_id == AV_CODEC_ID_VP6F || par->codec_id == AV_CODEC_ID_VP6A || | 
| 1221 | 
        3/4✓ Branch 0 taken 1856 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 216 times. 
          ✓ Branch 3 taken 1640 times. 
         | 
      1856 | par->codec_id == AV_CODEC_ID_VP6 || par->codec_id == AV_CODEC_ID_AAC) | 
| 1222 | 216 | flags_size = 2; | |
| 1223 | 
        3/4✓ Branch 0 taken 1564 times. 
          ✓ Branch 1 taken 76 times. 
          ✓ Branch 2 taken 1564 times. 
          ✗ Branch 3 not taken. 
         | 
      1640 | else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4 || | 
| 1224 | 
        4/4✓ Branch 0 taken 1417 times. 
          ✓ Branch 1 taken 147 times. 
          ✓ Branch 2 taken 1317 times. 
          ✓ Branch 3 taken 100 times. 
         | 
      1564 | par->codec_id == AV_CODEC_ID_HEVC || par->codec_id == AV_CODEC_ID_AV1 || | 
| 1225 | 
        2/2✓ Branch 0 taken 46 times. 
          ✓ Branch 1 taken 1271 times. 
         | 
      1317 | par->codec_id == AV_CODEC_ID_VP9) | 
| 1226 | 369 | flags_size = 5; | |
| 1227 | else | ||
| 1228 | 1271 | flags_size = 1; | |
| 1229 | |||
| 1230 | 
        5/6✓ Branch 0 taken 933 times. 
          ✓ Branch 1 taken 1134 times. 
          ✓ Branch 2 taken 933 times. 
          ✗ Branch 3 not taken. 
          ✓ Branch 4 taken 357 times. 
          ✓ Branch 5 taken 1710 times. 
         | 
      2067 | if ((par->codec_type == AVMEDIA_TYPE_VIDEO || par->codec_type == AVMEDIA_TYPE_AUDIO) && track_idx) | 
| 1231 | 357 | flags_size += 2; // additional header bytes for multi-track flv | |
| 1232 | |||
| 1233 | 
        2/2✓ Branch 0 taken 1920 times. 
          ✓ Branch 1 taken 147 times. 
         | 
      2067 | if ((par->codec_id == AV_CODEC_ID_HEVC || | 
| 1234 | 
        4/4✓ Branch 0 taken 76 times. 
          ✓ Branch 1 taken 1844 times. 
          ✓ Branch 2 taken 38 times. 
          ✓ Branch 3 taken 38 times. 
         | 
      1920 | (par->codec_id == AV_CODEC_ID_H264 && track_idx)) | 
| 1235 | 
        2/2✓ Branch 0 taken 163 times. 
          ✓ Branch 1 taken 22 times. 
         | 
      185 | && pkt->pts != pkt->dts) | 
| 1236 | 163 | flags_size += 3; | |
| 1237 | |||
| 1238 | 
        4/4✓ Branch 0 taken 1779 times. 
          ✓ Branch 1 taken 288 times. 
          ✓ Branch 2 taken 1703 times. 
          ✓ Branch 3 taken 76 times. 
         | 
      2067 | if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264 | 
| 1239 | 
        3/4✓ Branch 0 taken 1703 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 1556 times. 
          ✓ Branch 3 taken 147 times. 
         | 
      1703 | || par->codec_id == AV_CODEC_ID_MPEG4 || par->codec_id == AV_CODEC_ID_HEVC | 
| 1240 | 
        4/4✓ Branch 0 taken 1456 times. 
          ✓ Branch 1 taken 100 times. 
          ✓ Branch 2 taken 1410 times. 
          ✓ Branch 3 taken 46 times. 
         | 
      1556 | || par->codec_id == AV_CODEC_ID_AV1 || par->codec_id == AV_CODEC_ID_VP9 | 
| 1241 | 
        4/4✓ Branch 0 taken 1334 times. 
          ✓ Branch 1 taken 76 times. 
          ✓ Branch 2 taken 16 times. 
          ✓ Branch 3 taken 1318 times. 
         | 
      1410 | || par->codec_id == AV_CODEC_ID_OPUS || par->codec_id == AV_CODEC_ID_FLAC) { | 
| 1242 | size_t side_size; | ||
| 1243 | 749 | uint8_t *side = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size); | |
| 1244 | 
        1/8✗ Branch 0 not taken. 
          ✓ Branch 1 taken 749 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
          ✗ Branch 4 not taken. 
          ✗ Branch 5 not taken. 
          ✗ Branch 6 not taken. 
          ✗ Branch 7 not taken. 
         | 
      749 | if (side && side_size > 0 && (side_size != par->extradata_size || memcmp(side, par->extradata, side_size))) { | 
| 1245 | ✗ | ret = ff_alloc_extradata(par, side_size); | |
| 1246 | ✗ | if (ret < 0) | |
| 1247 | ✗ | return ret; | |
| 1248 | ✗ | memcpy(par->extradata, side, side_size); | |
| 1249 | ✗ | flv_write_codec_header(s, par, pkt->dts, pkt->stream_index); | |
| 1250 | } | ||
| 1251 | 749 | flv_write_metadata_packet(s, par, pkt->dts, pkt->stream_index); | |
| 1252 | } | ||
| 1253 | |||
| 1254 | 
        2/2✓ Branch 0 taken 23 times. 
          ✓ Branch 1 taken 2044 times. 
         | 
      2067 | if (flv->delay == AV_NOPTS_VALUE) | 
| 1255 | 23 | flv->delay = -pkt->dts; | |
| 1256 | |||
| 1257 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 2067 times. 
         | 
      2067 | if (pkt->dts < -flv->delay) { | 
| 1258 | ✗ | av_log(s, AV_LOG_WARNING, | |
| 1259 | "Packets are not in the proper order with respect to DTS\n"); | ||
| 1260 | ✗ | return AVERROR(EINVAL); | |
| 1261 | } | ||
| 1262 | 
        3/4✓ Branch 0 taken 1991 times. 
          ✓ Branch 1 taken 76 times. 
          ✓ Branch 2 taken 1991 times. 
          ✗ Branch 3 not taken. 
         | 
      2067 | if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4 || | 
| 1263 | 
        4/4✓ Branch 0 taken 1844 times. 
          ✓ Branch 1 taken 147 times. 
          ✓ Branch 2 taken 1744 times. 
          ✓ Branch 3 taken 100 times. 
         | 
      1991 | par->codec_id == AV_CODEC_ID_HEVC || par->codec_id == AV_CODEC_ID_AV1 || | 
| 1264 | 
        2/2✓ Branch 0 taken 46 times. 
          ✓ Branch 1 taken 1698 times. 
         | 
      1744 | par->codec_id == AV_CODEC_ID_VP9) { | 
| 1265 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 369 times. 
         | 
      369 | if (pkt->pts == AV_NOPTS_VALUE) { | 
| 1266 | ✗ | av_log(s, AV_LOG_ERROR, "Packet is missing PTS\n"); | |
| 1267 | ✗ | return AVERROR(EINVAL); | |
| 1268 | } | ||
| 1269 | } | ||
| 1270 | |||
| 1271 | 2067 | ts = pkt->dts; | |
| 1272 | |||
| 1273 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 2067 times. 
         | 
      2067 | if (s->event_flags & AVFMT_EVENT_FLAG_METADATA_UPDATED) { | 
| 1274 | ✗ | write_metadata(s, ts); | |
| 1275 | ✗ | s->event_flags &= ~AVFMT_EVENT_FLAG_METADATA_UPDATED; | |
| 1276 | } | ||
| 1277 | |||
| 1278 | 2067 | avio_write_marker(pb, av_rescale(ts, AV_TIME_BASE, 1000), | |
| 1279 | 
        6/6✓ Branch 0 taken 1213 times. 
          ✓ Branch 1 taken 854 times. 
          ✓ Branch 2 taken 707 times. 
          ✓ Branch 3 taken 506 times. 
          ✓ Branch 4 taken 280 times. 
          ✓ Branch 5 taken 427 times. 
         | 
      2067 | pkt->flags & AV_PKT_FLAG_KEY && (flv->video_par ? par->codec_type == AVMEDIA_TYPE_VIDEO : 1) ? AVIO_DATA_MARKER_SYNC_POINT : AVIO_DATA_MARKER_BOUNDARY_POINT); | 
| 1280 | |||
| 1281 | 
        2/4✓ Branch 0 taken 1134 times. 
          ✓ Branch 1 taken 933 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
         | 
      2067 | switch (par->codec_type) { | 
| 1282 | 1134 | case AVMEDIA_TYPE_VIDEO: | |
| 1283 | 1134 | avio_w8(pb, FLV_TAG_TYPE_VIDEO); | |
| 1284 | |||
| 1285 | 1134 | flags = ff_codec_get_tag(flv_video_codec_ids, par->codec_id); | |
| 1286 | |||
| 1287 | 1134 | flags |= frametype; | |
| 1288 | 1134 | break; | |
| 1289 | 933 | case AVMEDIA_TYPE_AUDIO: | |
| 1290 | 933 | flags = get_audio_flags(s, par); | |
| 1291 | |||
| 1292 | 933 | avio_w8(pb, FLV_TAG_TYPE_AUDIO); | |
| 1293 | 933 | break; | |
| 1294 | ✗ | case AVMEDIA_TYPE_SUBTITLE: | |
| 1295 | case AVMEDIA_TYPE_DATA: | ||
| 1296 | ✗ | avio_w8(pb, FLV_TAG_TYPE_META); | |
| 1297 | ✗ | break; | |
| 1298 | ✗ | default: | |
| 1299 | ✗ | return AVERROR(EINVAL); | |
| 1300 | } | ||
| 1301 | |||
| 1302 | 
        3/4✓ Branch 0 taken 1991 times. 
          ✓ Branch 1 taken 76 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 1991 times. 
         | 
      2067 | if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4) { | 
| 1303 | /* check if extradata looks like mp4 formatted */ | ||
| 1304 | 
        2/4✓ Branch 0 taken 76 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 76 times. 
         | 
      76 | if (par->extradata_size > 0 && *(uint8_t*)par->extradata != 1) | 
| 1305 | ✗ | if ((ret = ff_nal_parse_units_buf(pkt->data, &data, &size)) < 0) | |
| 1306 | ✗ | return ret; | |
| 1307 | 
        2/2✓ Branch 0 taken 147 times. 
          ✓ Branch 1 taken 1844 times. 
         | 
      1991 | } else if (par->codec_id == AV_CODEC_ID_HEVC) { | 
| 1308 | 
        3/4✓ Branch 0 taken 147 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 1 times. 
          ✓ Branch 3 taken 146 times. 
         | 
      147 | if (par->extradata_size > 0 && *(uint8_t*)par->extradata != 1) | 
| 1309 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 1 times. 
         | 
      1 | if ((ret = ff_hevc_annexb2mp4_buf(pkt->data, &data, &size, 0, NULL)) < 0) | 
| 1310 | ✗ | return ret; | |
| 1311 | 
        3/4✓ Branch 0 taken 288 times. 
          ✓ Branch 1 taken 1556 times. 
          ✓ Branch 2 taken 288 times. 
          ✗ Branch 3 not taken. 
         | 
      1844 | } else if (par->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 && | 
| 1312 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 288 times. 
         | 
      288 | (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) { | 
| 1313 | ✗ | if (!s->streams[pkt->stream_index]->nb_frames) { | |
| 1314 | ✗ | av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: " | |
| 1315 | "use the audio bitstream filter 'aac_adtstoasc' to fix it " | ||
| 1316 | "('-bsf:a aac_adtstoasc' option with ffmpeg)\n"); | ||
| 1317 | ✗ | return AVERROR_INVALIDDATA; | |
| 1318 | } | ||
| 1319 | ✗ | av_log(s, AV_LOG_WARNING, "aac bitstream error\n"); | |
| 1320 | } | ||
| 1321 | |||
| 1322 | /* check Speex packet duration */ | ||
| 1323 | 
        1/4✗ Branch 0 not taken. 
          ✓ Branch 1 taken 2067 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
         | 
      2067 | if (par->codec_id == AV_CODEC_ID_SPEEX && ts - flv->last_ts[pkt->stream_index] > 160) | 
| 1324 | ✗ | av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than " | |
| 1325 | "8 frames per packet. Adobe Flash " | ||
| 1326 | "Player cannot handle this!\n"); | ||
| 1327 | |||
| 1328 | 
        1/2✓ Branch 0 taken 2067 times. 
          ✗ Branch 1 not taken. 
         | 
      2067 | if (flv->last_ts[pkt->stream_index] < ts) | 
| 1329 | 2067 | flv->last_ts[pkt->stream_index] = ts; | |
| 1330 | |||
| 1331 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 2067 times. 
         | 
      2067 | if (size + flags_size >= 1<<24) { | 
| 1332 | ✗ | av_log(s, AV_LOG_ERROR, "Too large packet with size %u >= %u\n", | |
| 1333 | size + flags_size, 1<<24); | ||
| 1334 | ✗ | ret = AVERROR(EINVAL); | |
| 1335 | ✗ | goto fail; | |
| 1336 | } | ||
| 1337 | |||
| 1338 | 2067 | avio_wb24(pb, size + flags_size); | |
| 1339 | 2067 | put_timestamp(pb, ts); | |
| 1340 | 2067 | avio_wb24(pb, flv->reserved); | |
| 1341 | |||
| 1342 | 
        1/2✓ Branch 0 taken 2067 times. 
          ✗ Branch 1 not taken. 
         | 
      2067 | if (par->codec_type == AVMEDIA_TYPE_DATA || | 
| 1343 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 2067 times. 
         | 
      2067 | par->codec_type == AVMEDIA_TYPE_SUBTITLE ) { | 
| 1344 | int data_size; | ||
| 1345 | ✗ | int64_t metadata_size_pos = avio_tell(pb); | |
| 1346 | ✗ | if (par->codec_id == AV_CODEC_ID_TEXT) { | |
| 1347 | // legacy FFmpeg magic? | ||
| 1348 | ✗ | avio_w8(pb, AMF_DATA_TYPE_STRING); | |
| 1349 | ✗ | put_amf_string(pb, "onTextData"); | |
| 1350 | ✗ | avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY); | |
| 1351 | ✗ | avio_wb32(pb, 2); | |
| 1352 | ✗ | put_amf_string(pb, "type"); | |
| 1353 | ✗ | avio_w8(pb, AMF_DATA_TYPE_STRING); | |
| 1354 | ✗ | put_amf_string(pb, "Text"); | |
| 1355 | ✗ | put_amf_string(pb, "text"); | |
| 1356 | ✗ | avio_w8(pb, AMF_DATA_TYPE_STRING); | |
| 1357 | ✗ | put_amf_string(pb, pkt->data); | |
| 1358 | ✗ | put_amf_string(pb, ""); | |
| 1359 | ✗ | avio_w8(pb, AMF_END_OF_OBJECT); | |
| 1360 | } else { | ||
| 1361 | // just pass the metadata through | ||
| 1362 | ✗ | avio_write(pb, data ? data : pkt->data, size); | |
| 1363 | } | ||
| 1364 | /* write total size of tag */ | ||
| 1365 | ✗ | data_size = avio_tell(pb) - metadata_size_pos; | |
| 1366 | ✗ | avio_seek(pb, metadata_size_pos - 10, SEEK_SET); | |
| 1367 | ✗ | avio_wb24(pb, data_size); | |
| 1368 | ✗ | avio_seek(pb, data_size + 10 - 3, SEEK_CUR); | |
| 1369 | ✗ | avio_wb32(pb, data_size + 11); | |
| 1370 | } else { | ||
| 1371 | 
        2/2✓ Branch 0 taken 38 times. 
          ✓ Branch 1 taken 38 times. 
         | 
      76 | int extended_video = (par->codec_id == AV_CODEC_ID_H264 && track_idx) || | 
| 1372 | 
        2/2✓ Branch 0 taken 1882 times. 
          ✓ Branch 1 taken 147 times. 
         | 
      2029 | par->codec_id == AV_CODEC_ID_HEVC || | 
| 1373 | 
        4/4✓ Branch 0 taken 76 times. 
          ✓ Branch 1 taken 1991 times. 
          ✓ Branch 2 taken 1782 times. 
          ✓ Branch 3 taken 100 times. 
         | 
      5916 | par->codec_id == AV_CODEC_ID_AV1 || | 
| 1374 | 
        2/2✓ Branch 0 taken 46 times. 
          ✓ Branch 1 taken 1736 times. 
         | 
      1782 | par->codec_id == AV_CODEC_ID_VP9; | 
| 1375 | |||
| 1376 | 
        2/2✓ Branch 0 taken 331 times. 
          ✓ Branch 1 taken 1736 times. 
         | 
      2067 | if (extended_video) { | 
| 1377 | 
        2/2✓ Branch 0 taken 293 times. 
          ✓ Branch 1 taken 38 times. 
         | 
      624 | int h2645 = par->codec_id == AV_CODEC_ID_H264 || | 
| 1378 | 
        2/2✓ Branch 0 taken 147 times. 
          ✓ Branch 1 taken 146 times. 
         | 
      293 | par->codec_id == AV_CODEC_ID_HEVC; | 
| 1379 | 331 | int pkttype = PacketTypeCodedFrames; | |
| 1380 | // Optimisation for HEVC/H264: Do not send composition time if DTS == PTS | ||
| 1381 | 
        4/4✓ Branch 0 taken 185 times. 
          ✓ Branch 1 taken 146 times. 
          ✓ Branch 2 taken 22 times. 
          ✓ Branch 3 taken 163 times. 
         | 
      331 | if (h2645 && pkt->pts == pkt->dts) | 
| 1382 | 22 | pkttype = PacketTypeCodedFramesX; | |
| 1383 | |||
| 1384 | 
        2/2✓ Branch 0 taken 146 times. 
          ✓ Branch 1 taken 185 times. 
         | 
      331 | if (track_idx) { | 
| 1385 | 146 | avio_w8(pb, FLV_IS_EX_HEADER | PacketTypeMultitrack | frametype); | |
| 1386 | 146 | avio_w8(pb, MultitrackTypeOneTrack | pkttype); | |
| 1387 | } else { | ||
| 1388 | 185 | avio_w8(pb, FLV_IS_EX_HEADER | pkttype | frametype); | |
| 1389 | } | ||
| 1390 | |||
| 1391 | 331 | write_codec_fourcc(pb, par->codec_id); | |
| 1392 | |||
| 1393 | 
        2/2✓ Branch 0 taken 146 times. 
          ✓ Branch 1 taken 185 times. 
         | 
      331 | if (track_idx) | 
| 1394 | 146 | avio_w8(pb, track_idx); | |
| 1395 | 
        4/4✓ Branch 0 taken 185 times. 
          ✓ Branch 1 taken 146 times. 
          ✓ Branch 2 taken 163 times. 
          ✓ Branch 3 taken 22 times. 
         | 
      331 | if (h2645 && pkttype == PacketTypeCodedFrames) | 
| 1396 | 163 | avio_wb24(pb, pkt->pts - pkt->dts); | |
| 1397 | 
        2/2✓ Branch 0 taken 211 times. 
          ✓ Branch 1 taken 1525 times. 
         | 
      1736 | } else if (extended_audio) { | 
| 1398 | 
        1/2✓ Branch 0 taken 211 times. 
          ✗ Branch 1 not taken. 
         | 
      211 | if (track_idx) { | 
| 1399 | 211 | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeMultitrack); | |
| 1400 | 211 | avio_w8(pb, MultitrackTypeOneTrack | AudioPacketTypeCodedFrames); | |
| 1401 | } else { | ||
| 1402 | ✗ | avio_w8(pb, FLV_CODECID_EX_HEADER | AudioPacketTypeCodedFrames); | |
| 1403 | } | ||
| 1404 | 211 | write_codec_fourcc(pb, par->codec_id); | |
| 1405 | 
        1/2✓ Branch 0 taken 211 times. 
          ✗ Branch 1 not taken. 
         | 
      211 | if (track_idx) | 
| 1406 | 211 | avio_w8(pb, track_idx); | |
| 1407 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1525 times. 
         | 
      1525 | } else if (track_idx) { | 
| 1408 | ✗ | av_log(s, AV_LOG_ERROR, "Attempted to write legacy codec into extended flv track.\n"); | |
| 1409 | ✗ | ret = AVERROR(EINVAL); | |
| 1410 | ✗ | goto fail; | |
| 1411 | } else { | ||
| 1412 | av_assert1(flags >= 0); | ||
| 1413 | 1525 | avio_w8(pb, flags); | |
| 1414 | |||
| 1415 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1525 times. 
         | 
      1525 | if (par->codec_id == AV_CODEC_ID_VP6) { | 
| 1416 | ✗ | avio_w8(pb,0); | |
| 1417 | 
        1/2✓ Branch 0 taken 1525 times. 
          ✗ Branch 1 not taken. 
         | 
      1525 | } else if (par->codec_id == AV_CODEC_ID_VP6F || | 
| 1418 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1525 times. 
         | 
      1525 | par->codec_id == AV_CODEC_ID_VP6A) { | 
| 1419 | ✗ | if (par->extradata_size) | |
| 1420 | ✗ | avio_w8(pb, par->extradata[0]); | |
| 1421 | else | ||
| 1422 | ✗ | avio_w8(pb, ((FFALIGN(par->width, 16) - par->width) << 4) | | |
| 1423 | ✗ | (FFALIGN(par->height, 16) - par->height)); | |
| 1424 | 
        2/2✓ Branch 0 taken 216 times. 
          ✓ Branch 1 taken 1309 times. 
         | 
      1525 | } else if (par->codec_id == AV_CODEC_ID_AAC) { | 
| 1425 | 216 | avio_w8(pb, 1); // AAC raw | |
| 1426 | 
        2/2✓ Branch 0 taken 1271 times. 
          ✓ Branch 1 taken 38 times. 
         | 
      1309 | } else if (par->codec_id == AV_CODEC_ID_H264 || | 
| 1427 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1271 times. 
         | 
      1271 | par->codec_id == AV_CODEC_ID_MPEG4) { | 
| 1428 | 38 | avio_w8(pb, 1); // AVC NALU | |
| 1429 | 38 | avio_wb24(pb, pkt->pts - pkt->dts); | |
| 1430 | } | ||
| 1431 | } | ||
| 1432 | |||
| 1433 | 
        2/2✓ Branch 0 taken 2066 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      2067 | avio_write(pb, data ? data : pkt->data, size); | 
| 1434 | |||
| 1435 | 2067 | avio_wb32(pb, size + flags_size + 11); // previous tag size | |
| 1436 | 2067 | flv->duration = FFMAX(flv->duration, | |
| 1437 | pkt->pts + flv->delay + pkt->duration); | ||
| 1438 | } | ||
| 1439 | |||
| 1440 | 
        2/2✓ Branch 0 taken 1927 times. 
          ✓ Branch 1 taken 140 times. 
         | 
      2067 | if (flv->flags & FLV_ADD_KEYFRAME_INDEX) { | 
| 1441 | 
        1/3✓ Branch 0 taken 140 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
         | 
      140 | switch (par->codec_type) { | 
| 1442 | 140 | case AVMEDIA_TYPE_VIDEO: | |
| 1443 | 140 | flv->videosize += (avio_tell(pb) - cur_offset); | |
| 1444 | 140 | flv->lasttimestamp = pkt->dts / 1000.0; | |
| 1445 | 
        2/2✓ Branch 0 taken 20 times. 
          ✓ Branch 1 taken 120 times. 
         | 
      140 | if (pkt->flags & AV_PKT_FLAG_KEY) { | 
| 1446 | 20 | flv->lastkeyframetimestamp = flv->lasttimestamp; | |
| 1447 | 20 | flv->lastkeyframelocation = cur_offset; | |
| 1448 | 20 | ret = flv_append_keyframe_info(s, flv, flv->lasttimestamp, cur_offset); | |
| 1449 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 20 times. 
         | 
      20 | if (ret < 0) | 
| 1450 | ✗ | goto fail; | |
| 1451 | } | ||
| 1452 | 140 | break; | |
| 1453 | |||
| 1454 | ✗ | case AVMEDIA_TYPE_AUDIO: | |
| 1455 | ✗ | flv->audiosize += (avio_tell(pb) - cur_offset); | |
| 1456 | ✗ | break; | |
| 1457 | |||
| 1458 | ✗ | default: | |
| 1459 | ✗ | av_log(s, AV_LOG_WARNING, "par->codec_type is type = [%d]\n", par->codec_type); | |
| 1460 | ✗ | break; | |
| 1461 | } | ||
| 1462 | } | ||
| 1463 | 1927 | fail: | |
| 1464 | 2067 | av_free(data); | |
| 1465 | |||
| 1466 | 2067 | return ret; | |
| 1467 | } | ||
| 1468 | |||
| 1469 | 33 | static int flv_check_bitstream(AVFormatContext *s, AVStream *st, | |
| 1470 | const AVPacket *pkt) | ||
| 1471 | { | ||
| 1472 | 
        2/2✓ Branch 0 taken 3 times. 
          ✓ Branch 1 taken 30 times. 
         | 
      33 | if (st->codecpar->codec_id == AV_CODEC_ID_AAC) { | 
| 1473 | 
        2/4✓ Branch 0 taken 3 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 3 times. 
         | 
      3 | if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) | 
| 1474 | ✗ | return ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL); | |
| 1475 | } | ||
| 1476 | 
        2/2✓ Branch 0 taken 19 times. 
          ✓ Branch 1 taken 14 times. 
         | 
      33 | if (!st->codecpar->extradata_size && | 
| 1477 | 
        1/2✓ Branch 0 taken 19 times. 
          ✗ Branch 1 not taken. 
         | 
      19 | (st->codecpar->codec_id == AV_CODEC_ID_H264 || | 
| 1478 | 
        1/2✓ Branch 0 taken 19 times. 
          ✗ Branch 1 not taken. 
         | 
      19 | st->codecpar->codec_id == AV_CODEC_ID_HEVC || | 
| 1479 | 
        1/2✓ Branch 0 taken 19 times. 
          ✗ Branch 1 not taken. 
         | 
      19 | st->codecpar->codec_id == AV_CODEC_ID_AV1 || | 
| 1480 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 19 times. 
         | 
      19 | st->codecpar->codec_id == AV_CODEC_ID_MPEG4)) | 
| 1481 | ✗ | return ff_stream_add_bitstream_filter(st, "extract_extradata", NULL); | |
| 1482 | 33 | return 1; | |
| 1483 | } | ||
| 1484 | |||
| 1485 | 23 | static void flv_deinit(AVFormatContext *s) | |
| 1486 | { | ||
| 1487 | 23 | FLVContext *flv = s->priv_data; | |
| 1488 | 23 | FLVFileposition *filepos = flv->head_filepositions; | |
| 1489 | |||
| 1490 | 
        2/2✓ Branch 0 taken 20 times. 
          ✓ Branch 1 taken 23 times. 
         | 
      43 | while (filepos) { | 
| 1491 | 20 | FLVFileposition *next = filepos->next; | |
| 1492 | 20 | av_free(filepos); | |
| 1493 | 20 | filepos = next; | |
| 1494 | } | ||
| 1495 | 23 | flv->filepositions = flv->head_filepositions = NULL; | |
| 1496 | 23 | flv->filepositions_count = 0; | |
| 1497 | |||
| 1498 | 23 | av_freep(&flv->last_ts); | |
| 1499 | 23 | av_freep(&flv->metadata_pkt_written); | |
| 1500 | 23 | av_freep(&flv->track_idx_map); | |
| 1501 | 23 | } | |
| 1502 | |||
| 1503 | static const AVOption options[] = { | ||
| 1504 | { "flvflags", "FLV muxer flags", offsetof(FLVContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "flvflags" }, | ||
| 1505 | { "aac_seq_header_detect", "Put AAC sequence header based on stream data", 0, AV_OPT_TYPE_CONST, {.i64 = FLV_AAC_SEQ_HEADER_DETECT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "flvflags" }, | ||
| 1506 | { "no_sequence_end", "disable sequence end for FLV", 0, AV_OPT_TYPE_CONST, {.i64 = FLV_NO_SEQUENCE_END}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "flvflags" }, | ||
| 1507 | { "no_metadata", "disable metadata for FLV", 0, AV_OPT_TYPE_CONST, {.i64 = FLV_NO_METADATA}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "flvflags" }, | ||
| 1508 | { "no_duration_filesize", "disable duration and filesize zero value metadata for FLV", 0, AV_OPT_TYPE_CONST, {.i64 = FLV_NO_DURATION_FILESIZE}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "flvflags" }, | ||
| 1509 | { "add_keyframe_index", "Add keyframe index metadata", 0, AV_OPT_TYPE_CONST, {.i64 = FLV_ADD_KEYFRAME_INDEX}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, .unit = "flvflags" }, | ||
| 1510 | { NULL }, | ||
| 1511 | }; | ||
| 1512 | |||
| 1513 | static const AVClass flv_muxer_class = { | ||
| 1514 | .class_name = "flv muxer", | ||
| 1515 | .item_name = av_default_item_name, | ||
| 1516 | .option = options, | ||
| 1517 | .version = LIBAVUTIL_VERSION_INT, | ||
| 1518 | }; | ||
| 1519 | |||
| 1520 | const FFOutputFormat ff_flv_muxer = { | ||
| 1521 | .p.name = "flv", | ||
| 1522 | .p.long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"), | ||
| 1523 | .p.mime_type = "video/x-flv", | ||
| 1524 | .p.extensions = "flv", | ||
| 1525 | .priv_data_size = sizeof(FLVContext), | ||
| 1526 | .p.audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_ADPCM_SWF, | ||
| 1527 | .p.video_codec = AV_CODEC_ID_FLV1, | ||
| 1528 | .init = flv_init, | ||
| 1529 | .write_header = flv_write_header, | ||
| 1530 | .write_packet = flv_write_packet, | ||
| 1531 | .write_trailer = flv_write_trailer, | ||
| 1532 | .deinit = flv_deinit, | ||
| 1533 | .check_bitstream= flv_check_bitstream, | ||
| 1534 | .p.codec_tag = (const AVCodecTag* const []) { | ||
| 1535 | flv_video_codec_ids, flv_audio_codec_ids, 0 | ||
| 1536 | }, | ||
| 1537 | .p.flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS | | ||
| 1538 | AVFMT_TS_NONSTRICT, | ||
| 1539 | .p.priv_class = &flv_muxer_class, | ||
| 1540 | }; | ||
| 1541 |