FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/nutenc.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 638 784 81.4%
Functions: 25 26 96.2%
Branches: 350 477 73.4%

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