| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * nut muxer | ||
| 3 | * Copyright (c) 2004-2007 Michael Niedermayer | ||
| 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 <stdint.h> | ||
| 23 | |||
| 24 | #include "libavutil/intreadwrite.h" | ||
| 25 | #include "libavutil/mathematics.h" | ||
| 26 | #include "libavutil/mem.h" | ||
| 27 | #include "libavutil/tree.h" | ||
| 28 | #include "libavutil/dict.h" | ||
| 29 | #include "libavutil/avassert.h" | ||
| 30 | #include "libavutil/time.h" | ||
| 31 | #include "libavutil/opt.h" | ||
| 32 | #include "libavcodec/bytestream.h" | ||
| 33 | #include "libavcodec/mpegaudiodata.h" | ||
| 34 | #include "mux.h" | ||
| 35 | #include "nut.h" | ||
| 36 | #include "internal.h" | ||
| 37 | #include "avio_internal.h" | ||
| 38 | #include "riff.h" | ||
| 39 | #include "version.h" | ||
| 40 | |||
| 41 | /** | ||
| 42 | * Chooses a timebase for muxing the specified stream. | ||
| 43 | * | ||
| 44 | * The chosen timebase allows sample accurate timestamps based | ||
| 45 | * on the framerate or sample rate for audio streams. It also is | ||
| 46 | * at least as precise as 1/min_precision would be. | ||
| 47 | */ | ||
| 48 | 2770 | static AVRational choose_timebase(AVFormatContext *s, AVStream *st, int min_precision) | |
| 49 | { | ||
| 50 | 2770 | AVRational q = st->time_base; | |
| 51 | |||
| 52 |
4/4✓ Branch 0 taken 16620 times.
✓ Branch 1 taken 2770 times.
✓ Branch 2 taken 19390 times.
✓ Branch 3 taken 2770 times.
|
22160 | for (int j = 2; j < 14; j += 1 + (j > 2)) |
| 53 |
3/4✓ Branch 0 taken 19509 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 119 times.
✓ Branch 3 taken 19390 times.
|
19509 | while (q.den / q.num < min_precision && q.num % j == 0) |
| 54 | 119 | q.num /= j; | |
| 55 |
3/4✓ Branch 0 taken 30429 times.
✓ Branch 1 taken 2770 times.
✓ Branch 2 taken 30429 times.
✗ Branch 3 not taken.
|
33199 | while (q.den / q.num < min_precision && q.den < (1<<24)) |
| 56 | 30429 | q.den <<= 1; | |
| 57 | |||
| 58 | 2770 | return q; | |
| 59 | } | ||
| 60 | |||
| 61 | 3107 | static int find_expected_header(AVCodecParameters *p, int size, int key_frame, | |
| 62 | uint8_t out[64]) | ||
| 63 | { | ||
| 64 | 3107 | int sample_rate = p->sample_rate; | |
| 65 | |||
| 66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3107 times.
|
3107 | if (size > 4096) |
| 67 | ✗ | return 0; | |
| 68 | |||
| 69 | 3107 | AV_WB24(out, 1); | |
| 70 | |||
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3107 times.
|
3107 | if (p->codec_id == AV_CODEC_ID_MPEG4) { |
| 72 | ✗ | if (key_frame) { | |
| 73 | ✗ | return 3; | |
| 74 | } else { | ||
| 75 | ✗ | out[3] = 0xB6; | |
| 76 | ✗ | return 4; | |
| 77 | } | ||
| 78 |
1/2✓ Branch 0 taken 3107 times.
✗ Branch 1 not taken.
|
3107 | } else if (p->codec_id == AV_CODEC_ID_MPEG1VIDEO || |
| 79 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3107 times.
|
3107 | p->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
| 80 | ✗ | return 3; | |
| 81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3107 times.
|
3107 | } else if (p->codec_id == AV_CODEC_ID_H264) { |
| 82 | ✗ | return 3; | |
| 83 |
1/2✓ Branch 0 taken 3107 times.
✗ Branch 1 not taken.
|
3107 | } else if (p->codec_id == AV_CODEC_ID_MP3 || |
| 84 |
2/2✓ Branch 0 taken 126 times.
✓ Branch 1 taken 2981 times.
|
3107 | p->codec_id == AV_CODEC_ID_MP2) { |
| 85 | int lsf, mpeg25, sample_rate_index, bitrate_index, frame_size; | ||
| 86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
|
126 | int layer = p->codec_id == AV_CODEC_ID_MP3 ? 3 : 2; |
| 87 | 126 | unsigned int header = 0xFFF00000; | |
| 88 | |||
| 89 | 126 | lsf = sample_rate < (24000 + 32000) / 2; | |
| 90 | 126 | mpeg25 = sample_rate < (12000 + 16000) / 2; | |
| 91 | 126 | sample_rate <<= lsf + mpeg25; | |
| 92 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
|
126 | if (sample_rate < (32000 + 44100) / 2) sample_rate_index = 2; |
| 93 |
1/2✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
|
126 | else if (sample_rate < (44100 + 48000) / 2) sample_rate_index = 0; |
| 94 | ✗ | else sample_rate_index = 1; | |
| 95 | |||
| 96 | 126 | sample_rate = ff_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25); | |
| 97 | |||
| 98 |
2/2✓ Branch 0 taken 3446 times.
✓ Branch 1 taken 122 times.
|
3568 | for (bitrate_index = 2; bitrate_index < 30; bitrate_index++) { |
| 99 | 3446 | frame_size = | |
| 100 | 3446 | ff_mpa_bitrate_tab[lsf][layer - 1][bitrate_index >> 1]; | |
| 101 | 3446 | frame_size = (frame_size * 144000) / (sample_rate << lsf) + | |
| 102 | 3446 | (bitrate_index & 1); | |
| 103 | |||
| 104 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3442 times.
|
3446 | if (frame_size == size) |
| 105 | 4 | break; | |
| 106 | } | ||
| 107 | |||
| 108 |
1/2✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
|
126 | header |= (!lsf) << 19; |
| 109 | 126 | header |= (4 - layer) << 17; | |
| 110 | 126 | header |= 1 << 16; //no crc | |
| 111 | 126 | AV_WB32(out, header); | |
| 112 |
2/2✓ Branch 0 taken 122 times.
✓ Branch 1 taken 4 times.
|
126 | if (size <= 0) |
| 113 | 122 | return 2; //we guess there is no crc, if there is one the user clearly does not care about overhead | |
| 114 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (bitrate_index == 30) |
| 115 | ✗ | return -1; //something is wrong ... | |
| 116 | |||
| 117 | 4 | header |= (bitrate_index >> 1) << 12; | |
| 118 | 4 | header |= sample_rate_index << 10; | |
| 119 | 4 | header |= (bitrate_index & 1) << 9; | |
| 120 | |||
| 121 | 4 | return 2; //FIXME actually put the needed ones in build_elision_headers() | |
| 122 | //return 3; //we guess that the private bit is not set | ||
| 123 | //FIXME the above assumptions should be checked, if these turn out false too often something should be done | ||
| 124 | } | ||
| 125 | 2981 | return 0; | |
| 126 | } | ||
| 127 | |||
| 128 | 3107 | static int find_header_idx(AVFormatContext *s, AVCodecParameters *p, int size, | |
| 129 | int frame_type) | ||
| 130 | { | ||
| 131 | 3107 | NUTContext *nut = s->priv_data; | |
| 132 | uint8_t out[64]; | ||
| 133 | int i; | ||
| 134 | 3107 | int len = find_expected_header(p, size, frame_type, out); | |
| 135 | |||
| 136 |
2/2✓ Branch 0 taken 18642 times.
✓ Branch 1 taken 2981 times.
|
21623 | for (i = 1; i < nut->header_count; i++) { |
| 137 |
4/4✓ Branch 0 taken 504 times.
✓ Branch 1 taken 18138 times.
✓ Branch 2 taken 126 times.
✓ Branch 3 taken 378 times.
|
18642 | if (len == nut->header_len[i] && !memcmp(out, nut->header[i], len)) { |
| 138 | 126 | return i; | |
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | 2981 | return 0; | |
| 143 | } | ||
| 144 | |||
| 145 | 2780 | static void build_elision_headers(AVFormatContext *s) | |
| 146 | { | ||
| 147 | 2780 | NUTContext *nut = s->priv_data; | |
| 148 | int i; | ||
| 149 | //FIXME this is lame | ||
| 150 | //FIXME write a 2pass mode to find the maximal headers | ||
| 151 | static const uint8_t headers[][5] = { | ||
| 152 | { 3, 0x00, 0x00, 0x01 }, | ||
| 153 | { 4, 0x00, 0x00, 0x01, 0xB6}, | ||
| 154 | { 2, 0xFF, 0xFA }, //mp3+crc | ||
| 155 | { 2, 0xFF, 0xFB }, //mp3 | ||
| 156 | { 2, 0xFF, 0xFC }, //mp2+crc | ||
| 157 | { 2, 0xFF, 0xFD }, //mp2 | ||
| 158 | }; | ||
| 159 | |||
| 160 | 2780 | nut->header_count = 7; | |
| 161 |
2/2✓ Branch 0 taken 16680 times.
✓ Branch 1 taken 2780 times.
|
19460 | for (i = 1; i < nut->header_count; i++) { |
| 162 | 16680 | nut->header_len[i] = headers[i - 1][0]; | |
| 163 | 16680 | nut->header[i] = &headers[i - 1][1]; | |
| 164 | } | ||
| 165 | 2780 | } | |
| 166 | |||
| 167 | 2780 | static void build_frame_code(AVFormatContext *s) | |
| 168 | { | ||
| 169 | 2780 | NUTContext *nut = s->priv_data; | |
| 170 | int key_frame, index, pred, stream_id; | ||
| 171 | 2780 | int start = 1; | |
| 172 | 2780 | int end = 254; | |
| 173 | 2780 | int keyframe_0_esc = s->nb_streams > 2; | |
| 174 | int pred_table[10]; | ||
| 175 | FrameCode *ft; | ||
| 176 | |||
| 177 | 2780 | ft = &nut->frame_code[start]; | |
| 178 | 2780 | ft->flags = FLAG_CODED; | |
| 179 | 2780 | ft->size_mul = 1; | |
| 180 | 2780 | ft->pts_delta = 1; | |
| 181 | 2780 | start++; | |
| 182 | |||
| 183 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2779 times.
|
2780 | if (keyframe_0_esc) { |
| 184 | /* keyframe = 0 escape */ | ||
| 185 | 1 | FrameCode *ft = &nut->frame_code[start]; | |
| 186 | 1 | ft->flags = FLAG_STREAM_ID | FLAG_SIZE_MSB | FLAG_CODED_PTS; | |
| 187 | 1 | ft->size_mul = 1; | |
| 188 | 1 | start++; | |
| 189 | } | ||
| 190 | |||
| 191 |
2/2✓ Branch 0 taken 2784 times.
✓ Branch 1 taken 2780 times.
|
5564 | for (stream_id = 0; stream_id < s->nb_streams; stream_id++) { |
| 192 | 2784 | int start2 = start + (end - start) * stream_id / s->nb_streams; | |
| 193 | 2784 | int end2 = start + (end - start) * (stream_id + 1) / s->nb_streams; | |
| 194 | 2784 | AVCodecParameters *par = s->streams[stream_id]->codecpar; | |
| 195 | 2784 | int is_audio = par->codec_type == AVMEDIA_TYPE_AUDIO; | |
| 196 | 2784 | int intra_only = /*codec->intra_only || */ is_audio; | |
| 197 | int pred_count; | ||
| 198 | 2784 | int frame_size = 0; | |
| 199 | |||
| 200 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2770 times.
|
2784 | if (par->codec_type == AVMEDIA_TYPE_AUDIO) { |
| 201 | 14 | frame_size = av_get_audio_frame_duration2(par, 0); | |
| 202 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
14 | if (par->codec_id == AV_CODEC_ID_VORBIS && !frame_size) |
| 203 | ✗ | frame_size = 64; | |
| 204 | } else { | ||
| 205 | 2770 | AVRational f = av_div_q(av_inv_q(s->streams[stream_id]->avg_frame_rate), *nut->stream[stream_id].time_base); | |
| 206 |
3/4✓ Branch 0 taken 2769 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2769 times.
✗ Branch 3 not taken.
|
2770 | if (f.den == 1 && f.num>0) |
| 207 | 2769 | frame_size = f.num; | |
| 208 | } | ||
| 209 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2770 times.
|
2784 | if (!frame_size) |
| 210 | 14 | frame_size = 1; | |
| 211 | |||
| 212 |
2/2✓ Branch 0 taken 5568 times.
✓ Branch 1 taken 2784 times.
|
8352 | for (key_frame = 0; key_frame < 2; key_frame++) { |
| 213 |
6/6✓ Branch 0 taken 28 times.
✓ Branch 1 taken 5540 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
|
5568 | if (!intra_only || !keyframe_0_esc || key_frame != 0) { |
| 214 | 5567 | FrameCode *ft = &nut->frame_code[start2]; | |
| 215 | 5567 | ft->flags = FLAG_KEY * key_frame; | |
| 216 | 5567 | ft->flags |= FLAG_SIZE_MSB | FLAG_CODED_PTS; | |
| 217 | 5567 | ft->stream_id = stream_id; | |
| 218 | 5567 | ft->size_mul = 1; | |
| 219 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 5540 times.
|
5567 | if (is_audio) |
| 220 | 27 | ft->header_idx = find_header_idx(s, par, -1, key_frame); | |
| 221 | 5567 | start2++; | |
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | 2784 | key_frame = intra_only; | |
| 226 | #if 1 | ||
| 227 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2770 times.
|
2784 | if (is_audio) { |
| 228 | int frame_bytes; | ||
| 229 | int pts; | ||
| 230 | |||
| 231 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
|
14 | if (par->block_align > 0) { |
| 232 | 13 | frame_bytes = par->block_align; | |
| 233 | } else { | ||
| 234 | 1 | int frame_size = av_get_audio_frame_duration2(par, 0); | |
| 235 | 1 | frame_bytes = frame_size * (int64_t)par->bit_rate / (8 * par->sample_rate); | |
| 236 | } | ||
| 237 | |||
| 238 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 14 times.
|
42 | for (pts = 0; pts < 2; pts++) { |
| 239 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
|
84 | for (pred = 0; pred < 2; pred++) { |
| 240 | 56 | FrameCode *ft = &nut->frame_code[start2]; | |
| 241 | 56 | ft->flags = FLAG_KEY * key_frame; | |
| 242 | 56 | ft->stream_id = stream_id; | |
| 243 | 56 | ft->size_mul = frame_bytes + 2; | |
| 244 | 56 | ft->size_lsb = frame_bytes + pred; | |
| 245 | 56 | ft->pts_delta = pts * frame_size; | |
| 246 | 56 | ft->header_idx = find_header_idx(s, par, frame_bytes + pred, key_frame); | |
| 247 | 56 | start2++; | |
| 248 | } | ||
| 249 | } | ||
| 250 | } else { | ||
| 251 | 2770 | FrameCode *ft = &nut->frame_code[start2]; | |
| 252 | 2770 | ft->flags = FLAG_KEY | FLAG_SIZE_MSB; | |
| 253 | 2770 | ft->stream_id = stream_id; | |
| 254 | 2770 | ft->size_mul = 1; | |
| 255 | 2770 | ft->pts_delta = frame_size; | |
| 256 | 2770 | start2++; | |
| 257 | } | ||
| 258 | #endif | ||
| 259 | |||
| 260 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2782 times.
|
2784 | if (par->video_delay) { |
| 261 | 2 | pred_count = 5; | |
| 262 | 2 | pred_table[0] = -2; | |
| 263 | 2 | pred_table[1] = -1; | |
| 264 | 2 | pred_table[2] = 1; | |
| 265 | 2 | pred_table[3] = 3; | |
| 266 | 2 | pred_table[4] = 4; | |
| 267 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2782 times.
|
2782 | } else if (par->codec_id == AV_CODEC_ID_VORBIS) { |
| 268 | ✗ | pred_count = 3; | |
| 269 | ✗ | pred_table[0] = 2; | |
| 270 | ✗ | pred_table[1] = 9; | |
| 271 | ✗ | pred_table[2] = 16; | |
| 272 | } else { | ||
| 273 | 2782 | pred_count = 1; | |
| 274 | 2782 | pred_table[0] = 1; | |
| 275 | } | ||
| 276 | |||
| 277 |
2/2✓ Branch 0 taken 2792 times.
✓ Branch 1 taken 2784 times.
|
5576 | for (pred = 0; pred < pred_count; pred++) { |
| 278 | 2792 | int start3 = start2 + (end2 - start2) * pred / pred_count; | |
| 279 | 2792 | int end3 = start2 + (end2 - start2) * (pred + 1) / pred_count; | |
| 280 | |||
| 281 | 2792 | pred_table[pred] *= frame_size; | |
| 282 | |||
| 283 |
2/2✓ Branch 0 taken 692166 times.
✓ Branch 1 taken 2792 times.
|
694958 | for (index = start3; index < end3; index++) { |
| 284 | 692166 | FrameCode *ft = &nut->frame_code[index]; | |
| 285 | 692166 | ft->flags = FLAG_KEY * key_frame; | |
| 286 | 692166 | ft->flags |= FLAG_SIZE_MSB; | |
| 287 | 692166 | ft->stream_id = stream_id; | |
| 288 | //FIXME use single byte size and pred from last | ||
| 289 | 692166 | ft->size_mul = end3 - start3; | |
| 290 | 692166 | ft->size_lsb = index - start3; | |
| 291 | 692166 | ft->pts_delta = pred_table[pred]; | |
| 292 |
2/2✓ Branch 0 taken 3024 times.
✓ Branch 1 taken 689142 times.
|
692166 | if (is_audio) |
| 293 | 3024 | ft->header_idx = find_header_idx(s, par, -1, key_frame); | |
| 294 | } | ||
| 295 | } | ||
| 296 | } | ||
| 297 | 2780 | memmove(&nut->frame_code['N' + 1], &nut->frame_code['N'], sizeof(FrameCode) * (255 - 'N')); | |
| 298 | 2780 | nut->frame_code[0].flags = | |
| 299 | 2780 | nut->frame_code[255].flags = | |
| 300 | 2780 | nut->frame_code['N'].flags = FLAG_INVALID; | |
| 301 | 2780 | } | |
| 302 | |||
| 303 | /** | ||
| 304 | * Get the length in bytes which is needed to store val as v. | ||
| 305 | */ | ||
| 306 | 286728 | static int get_v_length(uint64_t val) | |
| 307 | { | ||
| 308 | 286728 | int i = 1; | |
| 309 | |||
| 310 |
2/2✓ Branch 0 taken 86814 times.
✓ Branch 1 taken 286728 times.
|
373542 | while (val >>= 7) |
| 311 | 86814 | i++; | |
| 312 | |||
| 313 | 286728 | return i; | |
| 314 | } | ||
| 315 | |||
| 316 | /** | ||
| 317 | * Put val using a variable number of bytes. | ||
| 318 | */ | ||
| 319 | 271301 | static void put_v(AVIOContext *bc, uint64_t val) | |
| 320 | { | ||
| 321 | 271301 | int i = get_v_length(val); | |
| 322 | |||
| 323 |
2/2✓ Branch 0 taken 68575 times.
✓ Branch 1 taken 271301 times.
|
339876 | while (--i > 0) |
| 324 | 68575 | avio_w8(bc, 128 | (uint8_t)(val >> (7*i))); | |
| 325 | |||
| 326 | 271301 | avio_w8(bc, val & 127); | |
| 327 | 271301 | } | |
| 328 | |||
| 329 | 8507 | static void put_tt(NUTContext *nut, AVRational *time_base, AVIOContext *bc, uint64_t val) | |
| 330 | { | ||
| 331 | 8507 | val *= nut->time_base_count; | |
| 332 | 8507 | val += time_base - nut->time_base; | |
| 333 | 8507 | put_v(bc, val); | |
| 334 | 8507 | } | |
| 335 | /** | ||
| 336 | * Store a string as vb. | ||
| 337 | */ | ||
| 338 | 11158 | static void put_str(AVIOContext *bc, const char *string) | |
| 339 | { | ||
| 340 | 11158 | size_t len = strlen(string); | |
| 341 | |||
| 342 | 11158 | put_v(bc, len); | |
| 343 | 11158 | avio_write(bc, string, len); | |
| 344 | 11158 | } | |
| 345 | |||
| 346 | 22293 | static void put_s(AVIOContext *bc, int64_t val) | |
| 347 | { | ||
| 348 | 22293 | put_v(bc, 2 * FFABS(val) - (val > 0)); | |
| 349 | 22293 | } | |
| 350 | |||
| 351 | 19635 | static void put_packet(NUTContext *nut, AVIOContext *bc, AVIOContext *dyn_bc, | |
| 352 | uint64_t startcode) | ||
| 353 | { | ||
| 354 | 19635 | uint8_t *dyn_buf = NULL; | |
| 355 | 19635 | int dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf); | |
| 356 | 19635 | int forw_ptr = dyn_size + 4; | |
| 357 | |||
| 358 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19635 times.
|
19635 | if (forw_ptr > 4096) |
| 359 | ✗ | ffio_init_checksum(bc, ff_crc04C11DB7_update, 0); | |
| 360 | 19635 | avio_wb64(bc, startcode); | |
| 361 | 19635 | put_v(bc, forw_ptr); | |
| 362 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19635 times.
|
19635 | if (forw_ptr > 4096) |
| 363 | ✗ | avio_wl32(bc, ffio_get_checksum(bc)); | |
| 364 | |||
| 365 | 19635 | ffio_init_checksum(bc, ff_crc04C11DB7_update, 0); | |
| 366 | 19635 | avio_write(bc, dyn_buf, dyn_size); | |
| 367 | 19635 | avio_wl32(bc, ffio_get_checksum(bc)); | |
| 368 | |||
| 369 | 19635 | ffio_reset_dyn_buf(dyn_bc); | |
| 370 | 19635 | } | |
| 371 | |||
| 372 | 2780 | static void write_mainheader(NUTContext *nut, AVIOContext *bc) | |
| 373 | { | ||
| 374 | int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields, | ||
| 375 | tmp_head_idx; | ||
| 376 | int64_t tmp_match; | ||
| 377 | |||
| 378 | 2780 | put_v(bc, nut->version); | |
| 379 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2778 times.
|
2780 | if (nut->version > 3) |
| 380 | 2 | put_v(bc, nut->minor_version = 1); | |
| 381 | 2780 | put_v(bc, nut->avf->nb_streams); | |
| 382 | 2780 | put_v(bc, nut->max_distance); | |
| 383 | 2780 | put_v(bc, nut->time_base_count); | |
| 384 | |||
| 385 |
2/2✓ Branch 0 taken 2783 times.
✓ Branch 1 taken 2780 times.
|
5563 | for (i = 0; i < nut->time_base_count; i++) { |
| 386 | 2783 | put_v(bc, nut->time_base[i].num); | |
| 387 | 2783 | put_v(bc, nut->time_base[i].den); | |
| 388 | } | ||
| 389 | |||
| 390 | 2780 | tmp_pts = 0; | |
| 391 | 2780 | tmp_mul = 1; | |
| 392 | 2780 | tmp_stream = 0; | |
| 393 | 2780 | tmp_match = 1 - (1LL << 62); | |
| 394 | 2780 | tmp_head_idx = 0; | |
| 395 |
2/2✓ Branch 0 taken 19498 times.
✓ Branch 1 taken 2780 times.
|
22278 | for (i = 0; i < 256; ) { |
| 396 | 19498 | tmp_fields = 0; | |
| 397 | 19498 | tmp_size = 0; | |
| 398 | // tmp_res=0; | ||
| 399 |
2/2✓ Branch 0 taken 11138 times.
✓ Branch 1 taken 8360 times.
|
19498 | if (tmp_pts != nut->frame_code[i].pts_delta ) tmp_fields = 1; |
| 400 |
2/2✓ Branch 0 taken 11144 times.
✓ Branch 1 taken 8354 times.
|
19498 | if (tmp_mul != nut->frame_code[i].size_mul ) tmp_fields = 2; |
| 401 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 19491 times.
|
19498 | if (tmp_stream != nut->frame_code[i].stream_id ) tmp_fields = 3; |
| 402 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 19470 times.
|
19498 | if (tmp_size != nut->frame_code[i].size_lsb ) tmp_fields = 4; |
| 403 | // if (tmp_res != nut->frame_code[i].res ) tmp_fields=5; | ||
| 404 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 19496 times.
|
19498 | if (tmp_head_idx != nut->frame_code[i].header_idx) tmp_fields = 8; |
| 405 | |||
| 406 | 19498 | tmp_pts = nut->frame_code[i].pts_delta; | |
| 407 | 19498 | tmp_flags = nut->frame_code[i].flags; | |
| 408 | 19498 | tmp_stream = nut->frame_code[i].stream_id; | |
| 409 | 19498 | tmp_mul = nut->frame_code[i].size_mul; | |
| 410 | 19498 | tmp_size = nut->frame_code[i].size_lsb; | |
| 411 | // tmp_res = nut->frame_code[i].res; | ||
| 412 | 19498 | tmp_head_idx = nut->frame_code[i].header_idx; | |
| 413 | |||
| 414 |
2/2✓ Branch 0 taken 728398 times.
✓ Branch 1 taken 2780 times.
|
731178 | for (j = 0; i < 256; j++, i++) { |
| 415 |
2/2✓ Branch 0 taken 2780 times.
✓ Branch 1 taken 725618 times.
|
728398 | if (i == 'N') { |
| 416 | 2780 | j--; | |
| 417 | 2780 | continue; | |
| 418 | } | ||
| 419 |
2/2✓ Branch 0 taken 714480 times.
✓ Branch 1 taken 11138 times.
|
725618 | if (nut->frame_code[i].pts_delta != tmp_pts || |
| 420 |
2/2✓ Branch 0 taken 708900 times.
✓ Branch 1 taken 5580 times.
|
714480 | nut->frame_code[i].flags != tmp_flags || |
| 421 |
1/2✓ Branch 0 taken 708900 times.
✗ Branch 1 not taken.
|
708900 | nut->frame_code[i].stream_id != tmp_stream || |
| 422 |
1/2✓ Branch 0 taken 708900 times.
✗ Branch 1 not taken.
|
708900 | nut->frame_code[i].size_mul != tmp_mul || |
| 423 |
1/2✓ Branch 0 taken 708900 times.
✗ Branch 1 not taken.
|
708900 | nut->frame_code[i].size_lsb != tmp_size + j || |
| 424 | // nut->frame_code[i].res != tmp_res || | ||
| 425 |
1/2✓ Branch 0 taken 708900 times.
✗ Branch 1 not taken.
|
708900 | nut->frame_code[i].header_idx != tmp_head_idx) |
| 426 | break; | ||
| 427 | } | ||
| 428 |
2/2✓ Branch 0 taken 5560 times.
✓ Branch 1 taken 13938 times.
|
19498 | if (j != tmp_mul - tmp_size) |
| 429 | 5560 | tmp_fields = 6; | |
| 430 | |||
| 431 | 19498 | put_v(bc, tmp_flags); | |
| 432 | 19498 | put_v(bc, tmp_fields); | |
| 433 |
2/2✓ Branch 0 taken 16714 times.
✓ Branch 1 taken 2784 times.
|
19498 | if (tmp_fields > 0) put_s(bc, tmp_pts); |
| 434 |
2/2✓ Branch 0 taken 11158 times.
✓ Branch 1 taken 8340 times.
|
19498 | if (tmp_fields > 1) put_v(bc, tmp_mul); |
| 435 |
2/2✓ Branch 0 taken 5592 times.
✓ Branch 1 taken 13906 times.
|
19498 | if (tmp_fields > 2) put_v(bc, tmp_stream); |
| 436 |
2/2✓ Branch 0 taken 5589 times.
✓ Branch 1 taken 13909 times.
|
19498 | if (tmp_fields > 3) put_v(bc, tmp_size); |
| 437 |
2/2✓ Branch 0 taken 5561 times.
✓ Branch 1 taken 13937 times.
|
19498 | if (tmp_fields > 4) put_v(bc, 0 /*tmp_res*/); |
| 438 |
2/2✓ Branch 0 taken 5561 times.
✓ Branch 1 taken 13937 times.
|
19498 | if (tmp_fields > 5) put_v(bc, j); |
| 439 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 19497 times.
|
19498 | if (tmp_fields > 6) put_v(bc, tmp_match); |
| 440 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 19497 times.
|
19498 | if (tmp_fields > 7) put_v(bc, tmp_head_idx); |
| 441 | } | ||
| 442 | 2780 | put_v(bc, nut->header_count - 1); | |
| 443 |
2/2✓ Branch 0 taken 16680 times.
✓ Branch 1 taken 2780 times.
|
19460 | for (i = 1; i < nut->header_count; i++) { |
| 444 | 16680 | put_v(bc, nut->header_len[i]); | |
| 445 | 16680 | avio_write(bc, nut->header[i], nut->header_len[i]); | |
| 446 | } | ||
| 447 | // flags had been effectively introduced in version 4 | ||
| 448 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2778 times.
|
2780 | if (nut->version > 3) |
| 449 | 2 | put_v(bc, nut->flags); | |
| 450 | 2780 | } | |
| 451 | |||
| 452 | 2784 | static int write_streamheader(AVFormatContext *avctx, AVIOContext *bc, | |
| 453 | AVStream *st, int i) | ||
| 454 | { | ||
| 455 | 2784 | NUTContext *nut = avctx->priv_data; | |
| 456 | 2784 | AVCodecParameters *par = st->codecpar; | |
| 457 | |||
| 458 | 2784 | put_v(bc, i); | |
| 459 |
2/4✓ Branch 0 taken 2770 times.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2784 | switch (par->codec_type) { |
| 460 | 2770 | case AVMEDIA_TYPE_VIDEO: put_v(bc, 0); break; | |
| 461 | 14 | case AVMEDIA_TYPE_AUDIO: put_v(bc, 1); break; | |
| 462 | ✗ | case AVMEDIA_TYPE_SUBTITLE: put_v(bc, 2); break; | |
| 463 | ✗ | default: put_v(bc, 3); break; | |
| 464 | } | ||
| 465 | 2784 | put_v(bc, 4); | |
| 466 | |||
| 467 |
1/2✓ Branch 0 taken 2784 times.
✗ Branch 1 not taken.
|
2784 | if (par->codec_tag) { |
| 468 | 2784 | avio_wl32(bc, par->codec_tag); | |
| 469 | } else { | ||
| 470 | ✗ | av_log(avctx, AV_LOG_ERROR, "No codec tag defined for stream %d\n", i); | |
| 471 | ✗ | return AVERROR(EINVAL); | |
| 472 | } | ||
| 473 | |||
| 474 | 2784 | put_v(bc, nut->stream[i].time_base - nut->time_base); | |
| 475 | 2784 | put_v(bc, nut->stream[i].msb_pts_shift); | |
| 476 | 2784 | put_v(bc, nut->stream[i].max_pts_distance); | |
| 477 | 2784 | put_v(bc, par->video_delay); | |
| 478 | 2784 | avio_w8(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */ | |
| 479 | |||
| 480 | 2784 | put_v(bc, par->extradata_size); | |
| 481 | 2784 | avio_write(bc, par->extradata, par->extradata_size); | |
| 482 | |||
| 483 |
2/3✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2770 times.
✗ Branch 2 not taken.
|
2784 | switch (par->codec_type) { |
| 484 | 14 | case AVMEDIA_TYPE_AUDIO: | |
| 485 | 14 | put_v(bc, par->sample_rate); | |
| 486 | 14 | put_v(bc, 1); | |
| 487 | 14 | put_v(bc, par->ch_layout.nb_channels); | |
| 488 | 14 | break; | |
| 489 | 2770 | case AVMEDIA_TYPE_VIDEO: | |
| 490 | 2770 | put_v(bc, par->width); | |
| 491 | 2770 | put_v(bc, par->height); | |
| 492 | |||
| 493 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2765 times.
|
2770 | if (st->sample_aspect_ratio.num <= 0 || |
| 494 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | st->sample_aspect_ratio.den <= 0) { |
| 495 | 2765 | put_v(bc, 0); | |
| 496 | 2765 | put_v(bc, 0); | |
| 497 | } else { | ||
| 498 | 5 | put_v(bc, st->sample_aspect_ratio.num); | |
| 499 | 5 | put_v(bc, st->sample_aspect_ratio.den); | |
| 500 | } | ||
| 501 | 2770 | put_v(bc, 0); /* csp type -- unknown */ | |
| 502 | 2770 | break; | |
| 503 | ✗ | default: | |
| 504 | ✗ | break; | |
| 505 | } | ||
| 506 | 2784 | return 0; | |
| 507 | } | ||
| 508 | |||
| 509 | 5578 | static int add_info(AVIOContext *bc, const char *type, const char *value) | |
| 510 | { | ||
| 511 | 5578 | put_str(bc, type); | |
| 512 | 5578 | put_s(bc, -1); | |
| 513 | 5578 | put_str(bc, value); | |
| 514 | 5578 | return 1; | |
| 515 | } | ||
| 516 | |||
| 517 | 2780 | static int write_globalinfo(NUTContext *nut, AVIOContext *bc) | |
| 518 | { | ||
| 519 | 2780 | AVFormatContext *s = nut->avf; | |
| 520 | 2780 | const AVDictionaryEntry *t = NULL; | |
| 521 | AVIOContext *dyn_bc; | ||
| 522 | 2780 | uint8_t *dyn_buf = NULL; | |
| 523 | 2780 | int count = 0, dyn_size; | |
| 524 | 2780 | int ret = avio_open_dyn_buf(&dyn_bc); | |
| 525 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2780 times.
|
2780 | if (ret < 0) |
| 526 | ✗ | return ret; | |
| 527 | |||
| 528 | 2780 | ff_standardize_creation_time(s); | |
| 529 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 2780 times.
|
2790 | while ((t = av_dict_iterate(s->metadata, t))) |
| 530 | 10 | count += add_info(dyn_bc, t->key, t->value); | |
| 531 | |||
| 532 | 2780 | put_v(bc, 0); //stream_if_plus1 | |
| 533 | 2780 | put_v(bc, 0); //chapter_id | |
| 534 | 2780 | put_v(bc, 0); //timestamp_start | |
| 535 | 2780 | put_v(bc, 0); //length | |
| 536 | |||
| 537 | 2780 | put_v(bc, count); | |
| 538 | |||
| 539 | 2780 | dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf); | |
| 540 | 2780 | avio_write(bc, dyn_buf, dyn_size); | |
| 541 | 2780 | av_free(dyn_buf); | |
| 542 | 2780 | return 0; | |
| 543 | } | ||
| 544 | |||
| 545 | 2784 | static int write_streaminfo(NUTContext *nut, AVIOContext *bc, int stream_id) { | |
| 546 | 2784 | AVFormatContext *s= nut->avf; | |
| 547 | 2784 | AVStream* st = s->streams[stream_id]; | |
| 548 | 2784 | const AVDictionaryEntry *t = NULL; | |
| 549 | AVIOContext *dyn_bc; | ||
| 550 | 2784 | uint8_t *dyn_buf=NULL; | |
| 551 | 2784 | int count=0, dyn_size, i; | |
| 552 | 2784 | int ret = avio_open_dyn_buf(&dyn_bc); | |
| 553 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2784 times.
|
2784 | if (ret < 0) |
| 554 | ✗ | return ret; | |
| 555 | |||
| 556 |
2/2✓ Branch 1 taken 2795 times.
✓ Branch 2 taken 2784 times.
|
5579 | while ((t = av_dict_iterate(st->metadata, t))) |
| 557 | 2795 | count += add_info(dyn_bc, t->key, t->value); | |
| 558 |
2/2✓ Branch 0 taken 16704 times.
✓ Branch 1 taken 2784 times.
|
19488 | for (i=0; ff_nut_dispositions[i].flag; ++i) { |
| 559 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16701 times.
|
16704 | if (st->disposition & ff_nut_dispositions[i].flag) |
| 560 | 3 | count += add_info(dyn_bc, "Disposition", ff_nut_dispositions[i].str); | |
| 561 | } | ||
| 562 |
2/2✓ Branch 0 taken 2770 times.
✓ Branch 1 taken 14 times.
|
2784 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 563 | uint8_t buf[256]; | ||
| 564 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2768 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2770 | if (st->r_frame_rate.num>0 && st->r_frame_rate.den>0) |
| 565 | 2 | snprintf(buf, sizeof(buf), "%d/%d", st->r_frame_rate.num, st->r_frame_rate.den); | |
| 566 | else | ||
| 567 | 2768 | snprintf(buf, sizeof(buf), "%d/%d", st->avg_frame_rate.num, st->avg_frame_rate.den); | |
| 568 | 2770 | count += add_info(dyn_bc, "r_frame_rate", buf); | |
| 569 | } | ||
| 570 | 2784 | dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf); | |
| 571 | |||
| 572 |
1/2✓ Branch 0 taken 2784 times.
✗ Branch 1 not taken.
|
2784 | if (count) { |
| 573 | 2784 | put_v(bc, stream_id + 1); //stream_id_plus1 | |
| 574 | 2784 | put_v(bc, 0); //chapter_id | |
| 575 | 2784 | put_v(bc, 0); //timestamp_start | |
| 576 | 2784 | put_v(bc, 0); //length | |
| 577 | |||
| 578 | 2784 | put_v(bc, count); | |
| 579 | |||
| 580 | 2784 | avio_write(bc, dyn_buf, dyn_size); | |
| 581 | } | ||
| 582 | |||
| 583 | 2784 | av_free(dyn_buf); | |
| 584 | 2784 | return count; | |
| 585 | } | ||
| 586 | |||
| 587 | ✗ | static int write_chapter(NUTContext *nut, AVIOContext *bc, int id) | |
| 588 | { | ||
| 589 | AVIOContext *dyn_bc; | ||
| 590 | ✗ | uint8_t *dyn_buf = NULL; | |
| 591 | ✗ | const AVDictionaryEntry *t = NULL; | |
| 592 | ✗ | AVChapter *ch = nut->avf->chapters[id]; | |
| 593 | ✗ | int ret, dyn_size, count = 0; | |
| 594 | |||
| 595 | ✗ | ret = avio_open_dyn_buf(&dyn_bc); | |
| 596 | ✗ | if (ret < 0) | |
| 597 | ✗ | return ret; | |
| 598 | |||
| 599 | ✗ | put_v(bc, 0); // stream_id_plus1 | |
| 600 | ✗ | put_s(bc, id + 1); // chapter_id | |
| 601 | ✗ | put_tt(nut, nut->chapter[id].time_base, bc, ch->start); // chapter_start | |
| 602 | ✗ | put_v(bc, ch->end - ch->start); // chapter_len | |
| 603 | |||
| 604 | ✗ | while ((t = av_dict_iterate(ch->metadata, t))) | |
| 605 | ✗ | count += add_info(dyn_bc, t->key, t->value); | |
| 606 | |||
| 607 | ✗ | put_v(bc, count); | |
| 608 | |||
| 609 | ✗ | dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf); | |
| 610 | ✗ | avio_write(bc, dyn_buf, dyn_size); | |
| 611 | ✗ | av_freep(&dyn_buf); | |
| 612 | ✗ | return 0; | |
| 613 | } | ||
| 614 | |||
| 615 | 2780 | static int write_index(NUTContext *nut, AVIOContext *bc) { | |
| 616 | int i; | ||
| 617 | 2780 | Syncpoint dummy= { .pos= 0 }; | |
| 618 | 2780 | Syncpoint *next_node[2] = { NULL }; | |
| 619 | 2780 | int64_t startpos = avio_tell(bc); | |
| 620 | int64_t payload_size; | ||
| 621 | |||
| 622 | 2780 | put_tt(nut, nut->max_pts_tb, bc, nut->max_pts); | |
| 623 | |||
| 624 | 2780 | put_v(bc, nut->sp_count); | |
| 625 | |||
| 626 |
2/2✓ Branch 0 taken 5727 times.
✓ Branch 1 taken 2780 times.
|
8507 | for (i=0; i<nut->sp_count; i++) { |
| 627 | 5727 | av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, (void**)next_node); | |
| 628 | 5727 | put_v(bc, (next_node[1]->pos >> 4) - (dummy.pos>>4)); | |
| 629 | 5727 | dummy.pos = next_node[1]->pos; | |
| 630 | } | ||
| 631 | |||
| 632 |
2/2✓ Branch 0 taken 2784 times.
✓ Branch 1 taken 2780 times.
|
5564 | for (i=0; i<nut->avf->nb_streams; i++) { |
| 633 | 2784 | StreamContext *nus= &nut->stream[i]; | |
| 634 | 2784 | int64_t last_pts= -1; | |
| 635 | int j, k; | ||
| 636 |
2/2✓ Branch 0 taken 3264 times.
✓ Branch 1 taken 2784 times.
|
6048 | for (j=0; j<nut->sp_count; j++) { |
| 637 | int flag; | ||
| 638 | 3264 | int n = 0; | |
| 639 | |||
| 640 |
3/4✓ Branch 0 taken 480 times.
✓ Branch 1 taken 2784 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 480 times.
|
3264 | if (j && nus->keyframe_pts[j] == nus->keyframe_pts[j-1]) { |
| 641 | ✗ | av_log(nut->avf, AV_LOG_WARNING, "Multiple keyframes with same PTS\n"); | |
| 642 | ✗ | nus->keyframe_pts[j] = AV_NOPTS_VALUE; | |
| 643 | } | ||
| 644 | |||
| 645 | 3264 | flag = (nus->keyframe_pts[j] != AV_NOPTS_VALUE) ^ (j+1 == nut->sp_count); | |
| 646 |
4/4✓ Branch 0 taken 5770 times.
✓ Branch 1 taken 457 times.
✓ Branch 2 taken 2963 times.
✓ Branch 3 taken 2807 times.
|
6227 | for (; j<nut->sp_count && (nus->keyframe_pts[j] != AV_NOPTS_VALUE) == flag; j++) |
| 647 | 2963 | n++; | |
| 648 | |||
| 649 | 3264 | put_v(bc, 1 + 2 * flag + 4 * n); | |
| 650 |
4/4✓ Branch 0 taken 6227 times.
✓ Branch 1 taken 2807 times.
✓ Branch 2 taken 5770 times.
✓ Branch 3 taken 457 times.
|
9034 | for (k= j - n; k<=j && k<nut->sp_count; k++) { |
| 651 |
2/2✓ Branch 0 taken 2860 times.
✓ Branch 1 taken 2910 times.
|
5770 | if (nus->keyframe_pts[k] == AV_NOPTS_VALUE) |
| 652 | 2860 | continue; | |
| 653 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2910 times.
|
2910 | av_assert0(nus->keyframe_pts[k] > last_pts); |
| 654 | 2910 | put_v(bc, nus->keyframe_pts[k] - last_pts); | |
| 655 | 2910 | last_pts = nus->keyframe_pts[k]; | |
| 656 | } | ||
| 657 | } | ||
| 658 | } | ||
| 659 | |||
| 660 | 2780 | payload_size = avio_tell(bc) - startpos + 8 + 4; | |
| 661 | |||
| 662 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2780 times.
|
2780 | avio_wb64(bc, 8 + payload_size + av_log2(payload_size) / 7 + 1 + 4*(payload_size > 4096)); |
| 663 | |||
| 664 | 2780 | return 0; | |
| 665 | } | ||
| 666 | |||
| 667 | 2780 | static int write_headers(AVFormatContext *avctx, AVIOContext *bc) | |
| 668 | { | ||
| 669 | 2780 | NUTContext *nut = avctx->priv_data; | |
| 670 | AVIOContext *dyn_bc; | ||
| 671 | int i, ret; | ||
| 672 | |||
| 673 | 2780 | ff_metadata_conv_ctx(avctx, ff_nut_metadata_conv, NULL); | |
| 674 | |||
| 675 | 2780 | ret = avio_open_dyn_buf(&dyn_bc); | |
| 676 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2780 times.
|
2780 | if (ret < 0) |
| 677 | ✗ | return ret; | |
| 678 | 2780 | write_mainheader(nut, dyn_bc); | |
| 679 | 2780 | put_packet(nut, bc, dyn_bc, MAIN_STARTCODE); | |
| 680 | |||
| 681 |
2/2✓ Branch 0 taken 2784 times.
✓ Branch 1 taken 2780 times.
|
5564 | for (i = 0; i < nut->avf->nb_streams; i++) { |
| 682 | 2784 | ret = write_streamheader(avctx, dyn_bc, nut->avf->streams[i], i); | |
| 683 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2784 times.
|
2784 | if (ret < 0) { |
| 684 | ✗ | goto fail; | |
| 685 | } | ||
| 686 | 2784 | put_packet(nut, bc, dyn_bc, STREAM_STARTCODE); | |
| 687 | } | ||
| 688 | |||
| 689 | 2780 | write_globalinfo(nut, dyn_bc); | |
| 690 | 2780 | put_packet(nut, bc, dyn_bc, INFO_STARTCODE); | |
| 691 | |||
| 692 |
2/2✓ Branch 0 taken 2784 times.
✓ Branch 1 taken 2780 times.
|
5564 | for (i = 0; i < nut->avf->nb_streams; i++) { |
| 693 | 2784 | ret = write_streaminfo(nut, dyn_bc, i); | |
| 694 |
1/2✓ Branch 0 taken 2784 times.
✗ Branch 1 not taken.
|
2784 | if (ret > 0) |
| 695 | 2784 | put_packet(nut, bc, dyn_bc, INFO_STARTCODE); | |
| 696 | ✗ | else if (ret < 0) { | |
| 697 | ✗ | goto fail; | |
| 698 | } | ||
| 699 | } | ||
| 700 | |||
| 701 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2780 times.
|
2780 | for (i = 0; i < nut->avf->nb_chapters; i++) { |
| 702 | ✗ | ret = write_chapter(nut, dyn_bc, i); | |
| 703 | ✗ | if (ret < 0) { | |
| 704 | ✗ | goto fail; | |
| 705 | } | ||
| 706 | ✗ | put_packet(nut, bc, dyn_bc, INFO_STARTCODE); | |
| 707 | } | ||
| 708 | |||
| 709 | 2780 | nut->last_syncpoint_pos = INT_MIN; | |
| 710 | 2780 | nut->header_count++; | |
| 711 | |||
| 712 | 2780 | ret = 0; | |
| 713 | 2780 | fail: | |
| 714 | 2780 | ffio_free_dyn_buf(&dyn_bc); | |
| 715 | |||
| 716 | 2780 | return ret; | |
| 717 | } | ||
| 718 | |||
| 719 | 2780 | static int nut_write_header(AVFormatContext *s) | |
| 720 | { | ||
| 721 | 2780 | NUTContext *nut = s->priv_data; | |
| 722 | 2780 | AVIOContext *bc = s->pb; | |
| 723 | int i, j, ret; | ||
| 724 | |||
| 725 | 2780 | nut->avf = s; | |
| 726 | |||
| 727 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2778 times.
|
2780 | nut->version = FFMAX(NUT_STABLE_VERSION, 3 + !!nut->flags); |
| 728 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2778 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2780 | if (nut->version > 3 && s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) { |
| 729 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 730 | "The additional syncpoint modes require version %d, " | ||
| 731 | "that is currently not finalized, " | ||
| 732 | "please set -f_strict experimental in order to enable it.\n", | ||
| 733 | nut->version); | ||
| 734 | ✗ | return AVERROR_EXPERIMENTAL; | |
| 735 | } | ||
| 736 | |||
| 737 | 2780 | nut->stream = av_calloc(s->nb_streams, sizeof(*nut->stream )); | |
| 738 | 2780 | nut->chapter = av_calloc(s->nb_chapters, sizeof(*nut->chapter)); | |
| 739 | 5560 | nut->time_base= av_calloc(s->nb_streams + | |
| 740 | 2780 | s->nb_chapters, sizeof(*nut->time_base)); | |
| 741 |
3/6✓ Branch 0 taken 2780 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2780 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2780 times.
|
2780 | if (!nut->stream || !nut->chapter || !nut->time_base) |
| 742 | ✗ | return AVERROR(ENOMEM); | |
| 743 | |||
| 744 |
2/2✓ Branch 0 taken 2784 times.
✓ Branch 1 taken 2780 times.
|
5564 | for (i = 0; i < s->nb_streams; i++) { |
| 745 | 2784 | AVStream *st = s->streams[i]; | |
| 746 | int ssize; | ||
| 747 | AVRational time_base; | ||
| 748 | 2784 | ff_parse_specific_params(st, &time_base.den, &ssize, &time_base.num); | |
| 749 | |||
| 750 |
3/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2770 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
2784 | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate) { |
| 751 | 14 | time_base = (AVRational) {1, st->codecpar->sample_rate}; | |
| 752 | } else { | ||
| 753 | 2770 | time_base = choose_timebase(s, st, 48000); | |
| 754 | } | ||
| 755 | |||
| 756 | 2784 | avpriv_set_pts_info(st, 64, time_base.num, time_base.den); | |
| 757 | |||
| 758 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2783 times.
|
2788 | for (j = 0; j < nut->time_base_count; j++) |
| 759 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
|
5 | if (!memcmp(&time_base, &nut->time_base[j], sizeof(AVRational))) { |
| 760 | 1 | break; | |
| 761 | } | ||
| 762 | 2784 | nut->time_base[j] = time_base; | |
| 763 | 2784 | nut->stream[i].time_base = &nut->time_base[j]; | |
| 764 |
2/2✓ Branch 0 taken 2783 times.
✓ Branch 1 taken 1 times.
|
2784 | if (j == nut->time_base_count) |
| 765 | 2783 | nut->time_base_count++; | |
| 766 | |||
| 767 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2784 times.
|
2784 | if (INT64_C(1000) * time_base.num >= time_base.den) |
| 768 | ✗ | nut->stream[i].msb_pts_shift = 7; | |
| 769 | else | ||
| 770 | 2784 | nut->stream[i].msb_pts_shift = 14; | |
| 771 | 2784 | nut->stream[i].max_pts_distance = | |
| 772 | 2784 | FFMAX(time_base.den, time_base.num) / time_base.num; | |
| 773 | } | ||
| 774 | |||
| 775 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2780 times.
|
2780 | for (i = 0; i < s->nb_chapters; i++) { |
| 776 | ✗ | AVChapter *ch = s->chapters[i]; | |
| 777 | |||
| 778 | ✗ | for (j = 0; j < nut->time_base_count; j++) | |
| 779 | ✗ | if (!memcmp(&ch->time_base, &nut->time_base[j], sizeof(AVRational))) | |
| 780 | ✗ | break; | |
| 781 | |||
| 782 | ✗ | nut->time_base[j] = ch->time_base; | |
| 783 | ✗ | nut->chapter[i].time_base = &nut->time_base[j]; | |
| 784 | ✗ | if (j == nut->time_base_count) | |
| 785 | ✗ | nut->time_base_count++; | |
| 786 | } | ||
| 787 | |||
| 788 | 2780 | nut->max_distance = MAX_DISTANCE; | |
| 789 | 2780 | build_elision_headers(s); | |
| 790 | 2780 | build_frame_code(s); | |
| 791 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2780 times.
|
2780 | av_assert0(nut->frame_code['N'].flags == FLAG_INVALID); |
| 792 | |||
| 793 | 2780 | avio_write(bc, ID_STRING, strlen(ID_STRING)); | |
| 794 | 2780 | avio_w8(bc, 0); | |
| 795 | |||
| 796 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2780 times.
|
2780 | if ((ret = write_headers(s, bc)) < 0) |
| 797 | ✗ | return ret; | |
| 798 | |||
| 799 |
1/2✓ Branch 0 taken 2780 times.
✗ Branch 1 not taken.
|
2780 | if (s->avoid_negative_ts < 0) |
| 800 | 2780 | s->avoid_negative_ts = 1; | |
| 801 | |||
| 802 | 2780 | return 0; | |
| 803 | } | ||
| 804 | |||
| 805 | 1558290 | static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc, | |
| 806 | AVPacket *pkt) | ||
| 807 | { | ||
| 808 | 1558290 | int flags = 0; | |
| 809 | |||
| 810 |
2/2✓ Branch 0 taken 1526540 times.
✓ Branch 1 taken 31750 times.
|
1558290 | if (pkt->flags & AV_PKT_FLAG_KEY) |
| 811 | 1526540 | flags |= FLAG_KEY; | |
| 812 |
2/2✓ Branch 0 taken 22355 times.
✓ Branch 1 taken 1535935 times.
|
1558290 | if (pkt->stream_index != fc->stream_id) |
| 813 | 22355 | flags |= FLAG_STREAM_ID; | |
| 814 |
2/2✓ Branch 0 taken 1557855 times.
✓ Branch 1 taken 435 times.
|
1558290 | if (pkt->size / fc->size_mul) |
| 815 | 1557855 | flags |= FLAG_SIZE_MSB; | |
| 816 |
2/2✓ Branch 0 taken 1481513 times.
✓ Branch 1 taken 76777 times.
|
1558290 | if (pkt->pts - nus->last_pts != fc->pts_delta) |
| 817 | 1481513 | flags |= FLAG_CODED_PTS; | |
| 818 |
4/4✓ Branch 0 taken 19304 times.
✓ Branch 1 taken 1538986 times.
✓ Branch 2 taken 254 times.
✓ Branch 3 taken 19050 times.
|
1558290 | if (pkt->side_data_elems && nut->version > 3) |
| 819 | 254 | flags |= FLAG_SM_DATA; | |
| 820 |
2/2✓ Branch 0 taken 1207008 times.
✓ Branch 1 taken 351282 times.
|
1558290 | if (pkt->size > 2 * nut->max_distance) |
| 821 | 1207008 | flags |= FLAG_CHECKSUM; | |
| 822 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1558290 times.
|
1558290 | if (FFABS(pkt->pts - nus->last_pts) > nus->max_pts_distance) |
| 823 | ✗ | flags |= FLAG_CHECKSUM; | |
| 824 |
2/2✓ Branch 0 taken 8103 times.
✓ Branch 1 taken 1550187 times.
|
1558290 | if (fc->header_idx) |
| 825 |
1/2✓ Branch 0 taken 8103 times.
✗ Branch 1 not taken.
|
8103 | if (pkt->size < nut->header_len[fc->header_idx] || |
| 826 |
2/2✓ Branch 0 taken 4953 times.
✓ Branch 1 taken 3150 times.
|
8103 | pkt->size > 4096 || |
| 827 | 4953 | memcmp(pkt->data, nut->header [fc->header_idx], | |
| 828 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4953 times.
|
4953 | nut->header_len[fc->header_idx])) |
| 829 | 3150 | flags |= FLAG_HEADER_IDX; | |
| 830 | |||
| 831 | 1558290 | return flags | (fc->flags & FLAG_CODED); | |
| 832 | } | ||
| 833 | |||
| 834 | 6135 | static int find_best_header_idx(NUTContext *nut, AVPacket *pkt) | |
| 835 | { | ||
| 836 | int i; | ||
| 837 | 6135 | int best_i = 0; | |
| 838 | 6135 | int best_len = 0; | |
| 839 | |||
| 840 |
2/2✓ Branch 0 taken 5795 times.
✓ Branch 1 taken 340 times.
|
6135 | if (pkt->size > 4096) |
| 841 | 5795 | return 0; | |
| 842 | |||
| 843 |
2/2✓ Branch 0 taken 2380 times.
✓ Branch 1 taken 340 times.
|
2720 | for (i = 1; i < nut->header_count; i++) |
| 844 |
1/2✓ Branch 0 taken 2380 times.
✗ Branch 1 not taken.
|
2380 | if (pkt->size >= nut->header_len[i] |
| 845 |
2/2✓ Branch 0 taken 1860 times.
✓ Branch 1 taken 520 times.
|
2380 | && nut->header_len[i] > best_len |
| 846 |
2/2✓ Branch 0 taken 129 times.
✓ Branch 1 taken 1731 times.
|
1860 | && !memcmp(pkt->data, nut->header[i], nut->header_len[i])) { |
| 847 | 129 | best_i = i; | |
| 848 | 129 | best_len = nut->header_len[i]; | |
| 849 | } | ||
| 850 | 340 | return best_i; | |
| 851 | } | ||
| 852 | |||
| 853 | 2 | static int write_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta) | |
| 854 | { | ||
| 855 | int ret, i, dyn_size; | ||
| 856 | unsigned flags; | ||
| 857 | AVIOContext *dyn_bc; | ||
| 858 | 2 | int sm_data_count = 0; | |
| 859 | uint8_t tmp[256]; | ||
| 860 | uint8_t *dyn_buf; | ||
| 861 | |||
| 862 | 2 | ret = avio_open_dyn_buf(&dyn_bc); | |
| 863 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
| 864 | ✗ | return ret; | |
| 865 | |||
| 866 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | for (i = 0; i<pkt->side_data_elems; i++) { |
| 867 | 2 | const uint8_t *data = pkt->side_data[i].data; | |
| 868 | 2 | int size = pkt->side_data[i].size; | |
| 869 | 2 | const uint8_t *data_end = data + size; | |
| 870 | |||
| 871 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (is_meta) { |
| 872 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if ( pkt->side_data[i].type == AV_PKT_DATA_METADATA_UPDATE |
| 873 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | || pkt->side_data[i].type == AV_PKT_DATA_STRINGS_METADATA) { |
| 874 | ✗ | if (!size || data[size-1]) { | |
| 875 | ✗ | ret = AVERROR(EINVAL); | |
| 876 | ✗ | goto fail; | |
| 877 | } | ||
| 878 | ✗ | while (data < data_end) { | |
| 879 | ✗ | const uint8_t *key = data; | |
| 880 | ✗ | const uint8_t *val = data + strlen(key) + 1; | |
| 881 | |||
| 882 | ✗ | if(val >= data_end) { | |
| 883 | ✗ | ret = AVERROR(EINVAL); | |
| 884 | ✗ | goto fail; | |
| 885 | } | ||
| 886 | ✗ | put_str(dyn_bc, key); | |
| 887 | ✗ | put_s(dyn_bc, -1); | |
| 888 | ✗ | put_str(dyn_bc, val); | |
| 889 | ✗ | data = val + strlen(val) + 1; | |
| 890 | ✗ | sm_data_count++; | |
| 891 | } | ||
| 892 | } | ||
| 893 | } else { | ||
| 894 |
1/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | switch (pkt->side_data[i].type) { |
| 895 | 1 | case AV_PKT_DATA_PALETTE: | |
| 896 | case AV_PKT_DATA_NEW_EXTRADATA: | ||
| 897 | case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL: | ||
| 898 | default: | ||
| 899 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (pkt->side_data[i].type == AV_PKT_DATA_PALETTE) { |
| 900 | ✗ | put_str(dyn_bc, "Palette"); | |
| 901 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | } else if(pkt->side_data[i].type == AV_PKT_DATA_NEW_EXTRADATA) { |
| 902 | 1 | put_str(dyn_bc, "Extradata"); | |
| 903 | ✗ | } else if(pkt->side_data[i].type == AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL) { | |
| 904 | ✗ | snprintf(tmp, sizeof(tmp), "CodecSpecificSide%"PRId64"", AV_RB64(data)); | |
| 905 | ✗ | put_str(dyn_bc, tmp); | |
| 906 | } else { | ||
| 907 | ✗ | snprintf(tmp, sizeof(tmp), "UserData%s-SD-%d", | |
| 908 | ✗ | (s->flags & AVFMT_FLAG_BITEXACT) ? "Lavf" : LIBAVFORMAT_IDENT, | |
| 909 | ✗ | pkt->side_data[i].type); | |
| 910 | ✗ | put_str(dyn_bc, tmp); | |
| 911 | } | ||
| 912 | 1 | put_s(dyn_bc, -2); | |
| 913 | 1 | put_str(dyn_bc, "bin"); | |
| 914 | 1 | put_v(dyn_bc, pkt->side_data[i].size); | |
| 915 | 1 | avio_write(dyn_bc, data, pkt->side_data[i].size); | |
| 916 | 1 | sm_data_count++; | |
| 917 | 1 | break; | |
| 918 | ✗ | case AV_PKT_DATA_PARAM_CHANGE: | |
| 919 | ✗ | flags = bytestream_get_le32(&data); | |
| 920 | ✗ | if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) { | |
| 921 | ✗ | put_str(dyn_bc, "SampleRate"); | |
| 922 | ✗ | put_s(dyn_bc, bytestream_get_le32(&data)); | |
| 923 | ✗ | sm_data_count++; | |
| 924 | } | ||
| 925 | ✗ | if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) { | |
| 926 | ✗ | put_str(dyn_bc, "Width"); | |
| 927 | ✗ | put_s(dyn_bc, bytestream_get_le32(&data)); | |
| 928 | ✗ | put_str(dyn_bc, "Height"); | |
| 929 | ✗ | put_s(dyn_bc, bytestream_get_le32(&data)); | |
| 930 | ✗ | sm_data_count+=2; | |
| 931 | } | ||
| 932 | ✗ | break; | |
| 933 | ✗ | case AV_PKT_DATA_SKIP_SAMPLES: | |
| 934 | ✗ | if (AV_RL32(data)) { | |
| 935 | ✗ | put_str(dyn_bc, "SkipStart"); | |
| 936 | ✗ | put_s(dyn_bc, (unsigned)AV_RL32(data)); | |
| 937 | ✗ | sm_data_count++; | |
| 938 | } | ||
| 939 | ✗ | if (AV_RL32(data+4)) { | |
| 940 | ✗ | put_str(dyn_bc, "SkipEnd"); | |
| 941 | ✗ | put_s(dyn_bc, (unsigned)AV_RL32(data+4)); | |
| 942 | ✗ | sm_data_count++; | |
| 943 | } | ||
| 944 | ✗ | break; | |
| 945 | ✗ | case AV_PKT_DATA_METADATA_UPDATE: | |
| 946 | case AV_PKT_DATA_STRINGS_METADATA: | ||
| 947 | case AV_PKT_DATA_QUALITY_STATS: | ||
| 948 | // belongs into meta, not side data | ||
| 949 | ✗ | break; | |
| 950 | } | ||
| 951 | } | ||
| 952 | } | ||
| 953 | |||
| 954 | 2 | fail: | |
| 955 | 2 | put_v(bc, sm_data_count); | |
| 956 | 2 | dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf); | |
| 957 | 2 | avio_write(bc, dyn_buf, dyn_size); | |
| 958 | 2 | av_freep(&dyn_buf); | |
| 959 | |||
| 960 | 2 | return ret; | |
| 961 | } | ||
| 962 | |||
| 963 | 6135 | static int nut_write_packet(AVFormatContext *s, AVPacket *pkt) | |
| 964 | { | ||
| 965 | 6135 | NUTContext *nut = s->priv_data; | |
| 966 | 6135 | StreamContext *nus = &nut->stream[pkt->stream_index]; | |
| 967 | 6135 | AVIOContext *bc = s->pb, *dyn_bc, *sm_bc = NULL; | |
| 968 | FrameCode *fc; | ||
| 969 | int64_t coded_pts; | ||
| 970 | int best_length, frame_code, flags, needed_flags, i, header_idx; | ||
| 971 | int best_header_idx; | ||
| 972 | 6135 | int key_frame = !!(pkt->flags & AV_PKT_FLAG_KEY); | |
| 973 | 6135 | int store_sp = 0; | |
| 974 | 6135 | int ret = 0; | |
| 975 | 6135 | int sm_size = 0; | |
| 976 | 6135 | int data_size = pkt->size; | |
| 977 | 6135 | uint8_t *sm_buf = NULL; | |
| 978 | |||
| 979 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6135 times.
|
6135 | if (pkt->pts < 0) { |
| 980 | ✗ | av_log(s, AV_LOG_ERROR, | |
| 981 | "Negative pts not supported stream %d, pts %"PRId64"\n", | ||
| 982 | pkt->stream_index, pkt->pts); | ||
| 983 | ✗ | if (pkt->pts == AV_NOPTS_VALUE) | |
| 984 | ✗ | av_log(s, AV_LOG_ERROR, "Try to enable the genpts flag\n"); | |
| 985 | ✗ | return AVERROR(EINVAL); | |
| 986 | } | ||
| 987 | |||
| 988 |
4/4✓ Branch 0 taken 76 times.
✓ Branch 1 taken 6059 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 75 times.
|
6135 | if (pkt->side_data_elems && nut->version > 3) { |
| 989 | 1 | ret = avio_open_dyn_buf(&sm_bc); | |
| 990 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
| 991 | ✗ | return ret; | |
| 992 | 1 | ret = write_sm_data(s, sm_bc, pkt, 0); | |
| 993 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ret >= 0) |
| 994 | 1 | ret = write_sm_data(s, sm_bc, pkt, 1); | |
| 995 | 1 | sm_size = avio_close_dyn_buf(sm_bc, &sm_buf); | |
| 996 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) |
| 997 | ✗ | goto fail; | |
| 998 | 1 | data_size += sm_size; | |
| 999 | } | ||
| 1000 | |||
| 1001 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6135 times.
|
6135 | if (1LL << (20 + 3 * nut->header_count) <= avio_tell(bc)) |
| 1002 | ✗ | write_headers(s, bc); | |
| 1003 | |||
| 1004 |
4/4✓ Branch 0 taken 6010 times.
✓ Branch 1 taken 125 times.
✓ Branch 2 taken 2796 times.
✓ Branch 3 taken 3214 times.
|
6135 | if (key_frame && !(nus->last_flags & FLAG_KEY)) |
| 1005 | 2796 | store_sp = 1; | |
| 1006 | |||
| 1007 |
2/2✓ Branch 1 taken 5736 times.
✓ Branch 2 taken 399 times.
|
6135 | if (data_size + 30 /*FIXME check*/ + avio_tell(bc) >= nut->last_syncpoint_pos + nut->max_distance) |
| 1008 | 5736 | store_sp = 1; | |
| 1009 | |||
| 1010 | //FIXME: Ensure store_sp is 1 in the first place. | ||
| 1011 | |||
| 1012 |
2/2✓ Branch 0 taken 5740 times.
✓ Branch 1 taken 395 times.
|
6135 | if (store_sp && |
| 1013 |
4/4✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5725 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 13 times.
|
5740 | (!(nut->flags & NUT_PIPE) || nut->last_syncpoint_pos == INT_MIN)) { |
| 1014 | 5727 | int64_t sp_pos = INT64_MAX; | |
| 1015 | |||
| 1016 | 5727 | ff_nut_reset_ts(nut, *nus->time_base, pkt->dts); | |
| 1017 |
2/2✓ Branch 0 taken 5770 times.
✓ Branch 1 taken 5727 times.
|
11497 | for (i = 0; i < s->nb_streams; i++) { |
| 1018 | 5770 | AVStream *st = s->streams[i]; | |
| 1019 | 5770 | FFStream *const sti = ffstream(st); | |
| 1020 | 5770 | int64_t dts_tb = av_rescale_rnd(pkt->dts, | |
| 1021 | 5770 | nus->time_base->num * (int64_t)nut->stream[i].time_base->den, | |
| 1022 | 5770 | nus->time_base->den * (int64_t)nut->stream[i].time_base->num, | |
| 1023 | AV_ROUND_DOWN); | ||
| 1024 | 5770 | int index = av_index_search_timestamp(st, dts_tb, | |
| 1025 | AVSEEK_FLAG_BACKWARD); | ||
| 1026 |
2/2✓ Branch 0 taken 2981 times.
✓ Branch 1 taken 2789 times.
|
5770 | if (index >= 0) { |
| 1027 | 2981 | sp_pos = FFMIN(sp_pos, sti->index_entries[index].pos); | |
| 1028 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2981 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2981 | if (!nut->write_index && 2*index > sti->nb_index_entries) { |
| 1029 | ✗ | memmove(sti->index_entries, | |
| 1030 | ✗ | sti->index_entries + index, | |
| 1031 | ✗ | sizeof(*sti->index_entries) * (sti->nb_index_entries - index)); | |
| 1032 | ✗ | sti->nb_index_entries -= index; | |
| 1033 | } | ||
| 1034 | } | ||
| 1035 | } | ||
| 1036 | |||
| 1037 | 5727 | nut->last_syncpoint_pos = avio_tell(bc); | |
| 1038 | 5727 | ret = avio_open_dyn_buf(&dyn_bc); | |
| 1039 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5727 times.
|
5727 | if (ret < 0) |
| 1040 | ✗ | goto fail; | |
| 1041 | 5727 | put_tt(nut, nus->time_base, dyn_bc, pkt->dts); | |
| 1042 |
2/2✓ Branch 0 taken 2947 times.
✓ Branch 1 taken 2780 times.
|
5727 | put_v(dyn_bc, sp_pos != INT64_MAX ? (nut->last_syncpoint_pos - sp_pos) >> 4 : 0); |
| 1043 | |||
| 1044 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5727 times.
|
5727 | if (nut->flags & NUT_BROADCAST) { |
| 1045 | ✗ | put_tt(nut, nus->time_base, dyn_bc, | |
| 1046 | ✗ | av_rescale_q(av_gettime(), AV_TIME_BASE_Q, *nus->time_base)); | |
| 1047 | } | ||
| 1048 | 5727 | put_packet(nut, bc, dyn_bc, SYNCPOINT_STARTCODE); | |
| 1049 | 5727 | ffio_free_dyn_buf(&dyn_bc); | |
| 1050 | |||
| 1051 |
1/2✓ Branch 0 taken 5727 times.
✗ Branch 1 not taken.
|
5727 | if (nut->write_index) { |
| 1052 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5727 times.
|
5727 | if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, 0 /*unused*/, pkt->dts)) < 0) |
| 1053 | ✗ | goto fail; | |
| 1054 | |||
| 1055 |
2/2✓ Branch 0 taken 3795 times.
✓ Branch 1 taken 1932 times.
|
5727 | if ((1ll<<60) % nut->sp_count == 0) |
| 1056 |
2/2✓ Branch 0 taken 3811 times.
✓ Branch 1 taken 3795 times.
|
7606 | for (unsigned i = 0; i < s->nb_streams; i++) { |
| 1057 | 3811 | StreamContext *nus = &nut->stream[i]; | |
| 1058 | 3811 | av_reallocp_array(&nus->keyframe_pts, 2*nut->sp_count, sizeof(*nus->keyframe_pts)); | |
| 1059 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3811 times.
|
3811 | if (!nus->keyframe_pts) { |
| 1060 | ✗ | ret = AVERROR(ENOMEM); | |
| 1061 | ✗ | goto fail; | |
| 1062 | } | ||
| 1063 |
2/2✓ Branch 0 taken 1027 times.
✓ Branch 1 taken 2784 times.
|
3811 | for (int j = nut->sp_count == 1 ? 0 : nut->sp_count; |
| 1064 |
2/2✓ Branch 0 taken 10238 times.
✓ Branch 1 taken 3811 times.
|
14049 | j < 2 * nut->sp_count; j++) |
| 1065 | 10238 | nus->keyframe_pts[j] = AV_NOPTS_VALUE; | |
| 1066 | } | ||
| 1067 | } | ||
| 1068 | } | ||
| 1069 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6135 times.
|
6135 | av_assert0(nus->last_pts != AV_NOPTS_VALUE); |
| 1070 | |||
| 1071 | 6135 | coded_pts = pkt->pts & ((1 << nus->msb_pts_shift) - 1); | |
| 1072 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6135 times.
|
6135 | if (ff_lsb2full(nus, coded_pts) != pkt->pts) |
| 1073 | ✗ | coded_pts = pkt->pts + (1 << nus->msb_pts_shift); | |
| 1074 | |||
| 1075 | 6135 | best_header_idx = find_best_header_idx(nut, pkt); | |
| 1076 | |||
| 1077 | 6135 | best_length = INT_MAX; | |
| 1078 | 6135 | frame_code = -1; | |
| 1079 |
2/2✓ Branch 0 taken 1570560 times.
✓ Branch 1 taken 6135 times.
|
1576695 | for (i = 0; i < 256; i++) { |
| 1080 | 1570560 | int length = 0; | |
| 1081 | 1570560 | FrameCode *fc = &nut->frame_code[i]; | |
| 1082 | 1570560 | int flags = fc->flags; | |
| 1083 | |||
| 1084 |
2/2✓ Branch 0 taken 18405 times.
✓ Branch 1 taken 1552155 times.
|
1570560 | if (flags & FLAG_INVALID) |
| 1085 | 18405 | continue; | |
| 1086 | 1552155 | needed_flags = get_needed_flags(nut, nus, fc, pkt); | |
| 1087 | |||
| 1088 |
2/2✓ Branch 0 taken 6135 times.
✓ Branch 1 taken 1546020 times.
|
1552155 | if (flags & FLAG_CODED) { |
| 1089 | 6135 | length++; | |
| 1090 | 6135 | flags = needed_flags; | |
| 1091 | } | ||
| 1092 | |||
| 1093 |
2/2✓ Branch 0 taken 1533433 times.
✓ Branch 1 taken 18722 times.
|
1552155 | if ((flags & needed_flags) != needed_flags) |
| 1094 | 1533433 | continue; | |
| 1095 | |||
| 1096 |
2/2✓ Branch 0 taken 139 times.
✓ Branch 1 taken 18583 times.
|
18722 | if ((flags ^ needed_flags) & FLAG_KEY) |
| 1097 | 139 | continue; | |
| 1098 | |||
| 1099 |
2/2✓ Branch 0 taken 91 times.
✓ Branch 1 taken 18492 times.
|
18583 | if (flags & FLAG_STREAM_ID) |
| 1100 | 91 | length += get_v_length(pkt->stream_index); | |
| 1101 | |||
| 1102 |
2/2✓ Branch 0 taken 10735 times.
✓ Branch 1 taken 7848 times.
|
18583 | if (data_size % fc->size_mul != fc->size_lsb) |
| 1103 | 10735 | continue; | |
| 1104 |
2/2✓ Branch 0 taken 7819 times.
✓ Branch 1 taken 29 times.
|
7848 | if (flags & FLAG_SIZE_MSB) |
| 1105 | 7819 | length += get_v_length(data_size / fc->size_mul); | |
| 1106 | |||
| 1107 |
2/2✓ Branch 0 taken 4752 times.
✓ Branch 1 taken 3096 times.
|
7848 | if (flags & FLAG_CHECKSUM) |
| 1108 | 4752 | length += 4; | |
| 1109 | |||
| 1110 |
2/2✓ Branch 0 taken 7517 times.
✓ Branch 1 taken 331 times.
|
7848 | if (flags & FLAG_CODED_PTS) |
| 1111 | 7517 | length += get_v_length(coded_pts); | |
| 1112 | |||
| 1113 |
2/2✓ Branch 0 taken 6135 times.
✓ Branch 1 taken 1713 times.
|
7848 | if ( (flags & FLAG_CODED) |
| 1114 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 6051 times.
|
6135 | && nut->header_len[best_header_idx] > nut->header_len[fc->header_idx] + 1) { |
| 1115 | 84 | flags |= FLAG_HEADER_IDX; | |
| 1116 | } | ||
| 1117 | |||
| 1118 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 7764 times.
|
7848 | if (flags & FLAG_HEADER_IDX) { |
| 1119 | 84 | length += 1 - nut->header_len[best_header_idx]; | |
| 1120 | } else { | ||
| 1121 | 7764 | length -= nut->header_len[fc->header_idx]; | |
| 1122 | } | ||
| 1123 | |||
| 1124 | 7848 | length *= 4; | |
| 1125 | 7848 | length += !(flags & FLAG_CODED_PTS); | |
| 1126 | 7848 | length += !(flags & FLAG_CHECKSUM); | |
| 1127 | |||
| 1128 |
2/2✓ Branch 0 taken 7772 times.
✓ Branch 1 taken 76 times.
|
7848 | if (length < best_length) { |
| 1129 | 7772 | best_length = length; | |
| 1130 | 7772 | frame_code = i; | |
| 1131 | } | ||
| 1132 | } | ||
| 1133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6135 times.
|
6135 | av_assert0(frame_code != -1); |
| 1134 | |||
| 1135 | 6135 | fc = &nut->frame_code[frame_code]; | |
| 1136 | 6135 | flags = fc->flags; | |
| 1137 | 6135 | needed_flags = get_needed_flags(nut, nus, fc, pkt); | |
| 1138 | 6135 | header_idx = fc->header_idx; | |
| 1139 | |||
| 1140 | 6135 | ffio_init_checksum(bc, ff_crc04C11DB7_update, 0); | |
| 1141 | 6135 | avio_w8(bc, frame_code); | |
| 1142 |
2/2✓ Branch 0 taken 4759 times.
✓ Branch 1 taken 1376 times.
|
6135 | if (flags & FLAG_CODED) { |
| 1143 | 4759 | put_v(bc, (flags ^ needed_flags) & ~(FLAG_CODED)); | |
| 1144 | 4759 | flags = needed_flags; | |
| 1145 | } | ||
| 1146 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6131 times.
|
6135 | if (flags & FLAG_STREAM_ID) put_v(bc, pkt->stream_index); |
| 1147 |
2/2✓ Branch 0 taken 5835 times.
✓ Branch 1 taken 300 times.
|
6135 | if (flags & FLAG_CODED_PTS) put_v(bc, coded_pts); |
| 1148 |
2/2✓ Branch 0 taken 6106 times.
✓ Branch 1 taken 29 times.
|
6135 | if (flags & FLAG_SIZE_MSB ) put_v(bc, data_size / fc->size_mul); |
| 1149 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6135 times.
|
6135 | if (flags & FLAG_HEADER_IDX) put_v(bc, header_idx = best_header_idx); |
| 1150 | |||
| 1151 |
2/2✓ Branch 0 taken 4752 times.
✓ Branch 1 taken 1383 times.
|
6135 | if (flags & FLAG_CHECKSUM) avio_wl32(bc, ffio_get_checksum(bc)); |
| 1152 | 1383 | else ffio_get_checksum(bc); | |
| 1153 | |||
| 1154 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6134 times.
|
6135 | if (flags & FLAG_SM_DATA) { |
| 1155 | 1 | avio_write(bc, sm_buf, sm_size); | |
| 1156 | } | ||
| 1157 | 6135 | avio_write(bc, pkt->data + nut->header_len[header_idx], pkt->size - nut->header_len[header_idx]); | |
| 1158 | |||
| 1159 | 6135 | nus->last_flags = flags; | |
| 1160 | 6135 | nus->last_pts = pkt->pts; | |
| 1161 | |||
| 1162 | //FIXME just store one per syncpoint | ||
| 1163 |
4/4✓ Branch 0 taken 6010 times.
✓ Branch 1 taken 125 times.
✓ Branch 2 taken 6006 times.
✓ Branch 3 taken 4 times.
|
6135 | if (flags & FLAG_KEY && !(nut->flags & NUT_PIPE)) { |
| 1164 | 6006 | av_add_index_entry( | |
| 1165 | 6006 | s->streams[pkt->stream_index], | |
| 1166 | nut->last_syncpoint_pos, | ||
| 1167 | pkt->pts, | ||
| 1168 | 0, | ||
| 1169 | 0, | ||
| 1170 | AVINDEX_KEYFRAME); | ||
| 1171 |
3/4✓ Branch 0 taken 6006 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5688 times.
✓ Branch 3 taken 318 times.
|
6006 | if (nus->keyframe_pts && nus->keyframe_pts[nut->sp_count] == AV_NOPTS_VALUE) |
| 1172 | 5688 | nus->keyframe_pts[nut->sp_count] = pkt->pts; | |
| 1173 | } | ||
| 1174 | |||
| 1175 |
4/4✓ Branch 0 taken 3355 times.
✓ Branch 1 taken 2780 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 3344 times.
|
6135 | if (!nut->max_pts_tb || av_compare_ts(nut->max_pts, *nut->max_pts_tb, pkt->pts, *nus->time_base) < 0) { |
| 1176 | 6124 | nut->max_pts = pkt->pts; | |
| 1177 | 6124 | nut->max_pts_tb = nus->time_base; | |
| 1178 | } | ||
| 1179 | |||
| 1180 | 11 | fail: | |
| 1181 | 6135 | av_freep(&sm_buf); | |
| 1182 | |||
| 1183 | 6135 | return ret; | |
| 1184 | } | ||
| 1185 | |||
| 1186 | 2780 | static int nut_write_trailer(AVFormatContext *s) | |
| 1187 | { | ||
| 1188 | 2780 | NUTContext *nut = s->priv_data; | |
| 1189 | 2780 | AVIOContext *bc = s->pb, *dyn_bc; | |
| 1190 | int ret; | ||
| 1191 | |||
| 1192 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2780 times.
|
2780 | while (nut->header_count < 3) |
| 1193 | ✗ | write_headers(s, bc); | |
| 1194 | |||
| 1195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2780 times.
|
2780 | if (!nut->sp_count) |
| 1196 | ✗ | return 0; | |
| 1197 | |||
| 1198 | 2780 | ret = avio_open_dyn_buf(&dyn_bc); | |
| 1199 |
1/2✓ Branch 0 taken 2780 times.
✗ Branch 1 not taken.
|
2780 | if (ret >= 0) { |
| 1200 | av_assert1(nut->write_index); // sp_count should be 0 if no index is going to be written | ||
| 1201 | 2780 | write_index(nut, dyn_bc); | |
| 1202 | 2780 | put_packet(nut, bc, dyn_bc, INDEX_STARTCODE); | |
| 1203 | 2780 | ffio_free_dyn_buf(&dyn_bc); | |
| 1204 | } | ||
| 1205 | |||
| 1206 | 2780 | return 0; | |
| 1207 | } | ||
| 1208 | |||
| 1209 | 2780 | static void nut_write_deinit(AVFormatContext *s) | |
| 1210 | { | ||
| 1211 | 2780 | NUTContext *nut = s->priv_data; | |
| 1212 | int i; | ||
| 1213 | |||
| 1214 | 2780 | ff_nut_free_sp(nut); | |
| 1215 |
1/2✓ Branch 0 taken 2780 times.
✗ Branch 1 not taken.
|
2780 | if (nut->stream) |
| 1216 |
2/2✓ Branch 0 taken 2784 times.
✓ Branch 1 taken 2780 times.
|
5564 | for (i=0; i<s->nb_streams; i++) |
| 1217 | 2784 | av_freep(&nut->stream[i].keyframe_pts); | |
| 1218 | |||
| 1219 | 2780 | av_freep(&nut->stream); | |
| 1220 | 2780 | av_freep(&nut->chapter); | |
| 1221 | 2780 | av_freep(&nut->time_base); | |
| 1222 | 2780 | } | |
| 1223 | |||
| 1224 | #define OFFSET(x) offsetof(NUTContext, x) | ||
| 1225 | #define E AV_OPT_FLAG_ENCODING_PARAM | ||
| 1226 | static const AVOption options[] = { | ||
| 1227 | { "syncpoints", "NUT syncpoint behaviour", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, E, .unit = "syncpoints" }, | ||
| 1228 | { "default", "", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, E, .unit = "syncpoints" }, | ||
| 1229 | { "none", "Disable syncpoints, low overhead and unseekable", 0, AV_OPT_TYPE_CONST, {.i64 = NUT_PIPE}, INT_MIN, INT_MAX, E, .unit = "syncpoints" }, | ||
| 1230 | { "timestamped", "Extend syncpoints with a wallclock timestamp", 0, AV_OPT_TYPE_CONST, {.i64 = NUT_BROADCAST}, INT_MIN, INT_MAX, E, .unit = "syncpoints" }, | ||
| 1231 | { "write_index", "Write index", OFFSET(write_index), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, E, }, | ||
| 1232 | { NULL }, | ||
| 1233 | }; | ||
| 1234 | |||
| 1235 | static const AVClass class = { | ||
| 1236 | .class_name = "nutenc", | ||
| 1237 | .item_name = av_default_item_name, | ||
| 1238 | .option = options, | ||
| 1239 | .version = LIBAVUTIL_VERSION_INT, | ||
| 1240 | }; | ||
| 1241 | |||
| 1242 | const FFOutputFormat ff_nut_muxer = { | ||
| 1243 | .p.name = "nut", | ||
| 1244 | .p.long_name = NULL_IF_CONFIG_SMALL("NUT"), | ||
| 1245 | .p.mime_type = "video/x-nut", | ||
| 1246 | .p.extensions = "nut", | ||
| 1247 | .priv_data_size = sizeof(NUTContext), | ||
| 1248 | .p.audio_codec = CONFIG_LIBVORBIS ? AV_CODEC_ID_VORBIS : | ||
| 1249 | CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_MP2, | ||
| 1250 | .p.video_codec = AV_CODEC_ID_MPEG4, | ||
| 1251 | .write_header = nut_write_header, | ||
| 1252 | .write_packet = nut_write_packet, | ||
| 1253 | .write_trailer = nut_write_trailer, | ||
| 1254 | .deinit = nut_write_deinit, | ||
| 1255 | .p.flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS, | ||
| 1256 | .p.codec_tag = ff_nut_codec_tags, | ||
| 1257 | .p.priv_class = &class, | ||
| 1258 | }; | ||
| 1259 |