FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/nutenc.c
Date: 2026-11-08 03:48:22
Exec Total Coverage
Lines: 669 779 85.9%
Functions: 26 26 100.0%
Branches: 365 474 77.0%

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 2808 static AVRational choose_timebase(AVFormatContext *s, AVStream *st, int min_precision)
49 {
50 2808 AVRational q = st->time_base;
51
52
4/4
✓ Branch 0 taken 16848 times.
✓ Branch 1 taken 2808 times.
✓ Branch 2 taken 19656 times.
✓ Branch 3 taken 2808 times.
22464 for (int j = 2; j < 14; j += 1 + (j > 2))
53
4/4
✓ Branch 0 taken 19768 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 119 times.
✓ Branch 3 taken 19649 times.
19775 while (q.den / q.num < min_precision && q.num % j == 0)
54 119 q.num /= j;
55
3/4
✓ Branch 0 taken 30851 times.
✓ Branch 1 taken 2808 times.
✓ Branch 2 taken 30851 times.
✗ Branch 3 not taken.
33659 while (q.den / q.num < min_precision && q.den < (1<<24))
56 30851 q.den <<= 1;
57
58 2808 return q;
59 }
60
61 5753 static int find_expected_header(AVCodecParameters *p, int size, int key_frame,
62 uint8_t out[64])
63 {
64 5753 int sample_rate = p->sample_rate;
65
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5753 times.
5753 if (size > 4096)
67 ✗ return 0;
68
69 5753 AV_WB24(out, 1);
70
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5753 times.
5753 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 5753 times.
✗ Branch 1 not taken.
5753 } else if (p->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5753 times.
5753 p->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
80 ✗ return 3;
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5753 times.
5753 } else if (p->codec_id == AV_CODEC_ID_H264) {
82 ✗ return 3;
83
1/2
✓ Branch 0 taken 5753 times.
✗ Branch 1 not taken.
5753 } else if (p->codec_id == AV_CODEC_ID_MP3 ||
84
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 5627 times.
5753 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 5627 return 0;
126 }
127
128 5753 static int find_header_idx(AVFormatContext *s, AVCodecParameters *p, int size,
129 int frame_type)
130 {
131 5753 NUTContext *nut = s->priv_data;
132 uint8_t out[64];
133 int i;
134 5753 int len = find_expected_header(p, size, frame_type, out);
135
136
2/2
✓ Branch 0 taken 34518 times.
✓ Branch 1 taken 5627 times.
40145 for (i = 1; i < nut->header_count; i++) {
137
4/4
✓ Branch 0 taken 504 times.
✓ Branch 1 taken 34014 times.
✓ Branch 2 taken 126 times.
✓ Branch 3 taken 378 times.
34518 if (len == nut->header_len[i] && !memcmp(out, nut->header[i], len)) {
138 126 return i;
139 }
140 }
141
142 5627 return 0;
143 }
144
145 2828 static void build_elision_headers(AVFormatContext *s)
146 {
147 2828 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 2828 nut->header_count = 7;
161
2/2
✓ Branch 0 taken 16968 times.
✓ Branch 1 taken 2828 times.
19796 for (i = 1; i < nut->header_count; i++) {
162 16968 nut->header_len[i] = headers[i - 1][0];
163 16968 nut->header[i] = &headers[i - 1][1];
164 }
165 2828 }
166
167 2828 static void build_frame_code(AVFormatContext *s)
168 {
169 2828 NUTContext *nut = s->priv_data;
170 int key_frame, index, pred, stream_id;
171 2828 int start = 1;
172 2828 int end = 254;
173 2828 int keyframe_0_esc = s->nb_streams > 2;
174 int pred_table[10];
175 FrameCode *ft;
176
177 2828 ft = &nut->frame_code[start];
178 2828 ft->flags = FLAG_CODED;
179 2828 ft->size_mul = 1;
180 2828 ft->pts_delta = 1;
181 2828 start++;
182
183
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2827 times.
2828 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 /* Each stream needs up to 6 frame codes, streams beyond use the escape frame codes */
192 2828 int nb_frame_code_streams = FFMIN(s->nb_streams, (end - start) / 6);
193
194
2/2
✓ Branch 0 taken 2833 times.
✓ Branch 1 taken 2828 times.
5661 for (stream_id = 0; stream_id < nb_frame_code_streams; stream_id++) {
195 2833 int start2 = start + (end - start) * stream_id / nb_frame_code_streams;
196 2833 int end2 = start + (end - start) * (stream_id + 1) / nb_frame_code_streams;
197 2833 AVCodecParameters *par = s->streams[stream_id]->codecpar;
198 2833 int is_audio = par->codec_type == AVMEDIA_TYPE_AUDIO;
199 2833 int intra_only = /*codec->intra_only || */ is_audio;
200 int pred_count;
201 2833 int frame_size = 0;
202
203
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2808 times.
2833 if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
204 25 frame_size = av_get_audio_frame_duration2(par, 0);
205
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
25 if (par->codec_id == AV_CODEC_ID_VORBIS && !frame_size)
206 ✗ frame_size = 64;
207 } else {
208 2808 AVRational f = av_div_q(av_inv_q(s->streams[stream_id]->avg_frame_rate), *nut->stream[stream_id].time_base);
209
3/4
✓ Branch 0 taken 2807 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2807 times.
✗ Branch 3 not taken.
2808 if (f.den == 1 && f.num>0)
210 2807 frame_size = f.num;
211 }
212
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2808 times.
2833 if (!frame_size)
213 25 frame_size = 1;
214
215
2/2
✓ Branch 0 taken 5666 times.
✓ Branch 1 taken 2833 times.
8499 for (key_frame = 0; key_frame < 2; key_frame++) {
216
6/6
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 5616 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
5666 if (!intra_only || !keyframe_0_esc || key_frame != 0) {
217 5665 FrameCode *ft = &nut->frame_code[start2];
218 5665 ft->flags = FLAG_KEY * key_frame;
219 5665 ft->flags |= FLAG_SIZE_MSB | FLAG_CODED_PTS;
220 5665 ft->stream_id = stream_id;
221 5665 ft->size_mul = 1;
222
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 5616 times.
5665 if (is_audio)
223 49 ft->header_idx = find_header_idx(s, par, -1, key_frame);
224 5665 start2++;
225 }
226 }
227
228 2833 key_frame = intra_only;
229 #if 1
230
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2808 times.
2833 if (is_audio) {
231 int frame_bytes;
232 int pts;
233
234
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 12 times.
25 if (par->block_align > 0) {
235 13 frame_bytes = par->block_align;
236 } else {
237 12 int frame_size = av_get_audio_frame_duration2(par, 0);
238 12 frame_bytes = frame_size * (int64_t)par->bit_rate / (8 * par->sample_rate);
239 }
240
241
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 25 times.
75 for (pts = 0; pts < 2; pts++) {
242
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 50 times.
150 for (pred = 0; pred < 2; pred++) {
243 100 FrameCode *ft = &nut->frame_code[start2];
244 100 ft->flags = FLAG_KEY * key_frame;
245 100 ft->stream_id = stream_id;
246 100 ft->size_mul = frame_bytes + 2;
247 100 ft->size_lsb = frame_bytes + pred;
248 100 ft->pts_delta = pts * frame_size;
249 100 ft->header_idx = find_header_idx(s, par, frame_bytes + pred, key_frame);
250 100 start2++;
251 }
252 }
253 } else {
254 2808 FrameCode *ft = &nut->frame_code[start2];
255 2808 ft->flags = FLAG_KEY | FLAG_SIZE_MSB;
256 2808 ft->stream_id = stream_id;
257 2808 ft->size_mul = 1;
258 2808 ft->pts_delta = frame_size;
259 2808 start2++;
260 }
261 #endif
262
263
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2821 times.
2833 if (par->video_delay) {
264 12 pred_count = 5;
265 12 pred_table[0] = -2;
266 12 pred_table[1] = -1;
267 12 pred_table[2] = 1;
268 12 pred_table[3] = 3;
269 12 pred_table[4] = 4;
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2821 times.
2821 } else if (par->codec_id == AV_CODEC_ID_VORBIS) {
271 ✗ pred_count = 3;
272 ✗ pred_table[0] = 2;
273 ✗ pred_table[1] = 9;
274 ✗ pred_table[2] = 16;
275 } else {
276 2821 pred_count = 1;
277 2821 pred_table[0] = 1;
278 }
279
280
2/2
✓ Branch 0 taken 2881 times.
✓ Branch 1 taken 2833 times.
5714 for (pred = 0; pred < pred_count; pred++) {
281 2881 int start3 = start2 + (end2 - start2) * pred / pred_count;
282 2881 int end3 = start2 + (end2 - start2) * (pred + 1) / pred_count;
283
284 2881 pred_table[pred] *= frame_size;
285
286
2/2
✓ Branch 0 taken 704082 times.
✓ Branch 1 taken 2881 times.
706963 for (index = start3; index < end3; index++) {
287 704082 FrameCode *ft = &nut->frame_code[index];
288 704082 ft->flags = FLAG_KEY * key_frame;
289 704082 ft->flags |= FLAG_SIZE_MSB;
290 704082 ft->stream_id = stream_id;
291 //FIXME use single byte size and pred from last
292 704082 ft->size_mul = end3 - start3;
293 704082 ft->size_lsb = index - start3;
294 704082 ft->pts_delta = pred_table[pred];
295
2/2
✓ Branch 0 taken 5604 times.
✓ Branch 1 taken 698478 times.
704082 if (is_audio)
296 5604 ft->header_idx = find_header_idx(s, par, -1, key_frame);
297 }
298 }
299 }
300 2828 memmove(&nut->frame_code['N' + 1], &nut->frame_code['N'], sizeof(FrameCode) * (255 - 'N'));
301 2828 nut->frame_code[0].flags =
302 2828 nut->frame_code[255].flags =
303 2828 nut->frame_code['N'].flags = FLAG_INVALID;
304 2828 }
305
306 /**
307 * Get the length in bytes which is needed to store val as v.
308 */
309 293717 static int get_v_length(uint64_t val)
310 {
311 293717 int i = 1;
312
313
2/2
✓ Branch 0 taken 89190 times.
✓ Branch 1 taken 293717 times.
382907 while (val >>= 7)
314 89190 i++;
315
316 293717 return i;
317 }
318
319 /**
320 * Put val using a variable number of bytes.
321 */
322 276837 static void put_v(AVIOContext *bc, uint64_t val)
323 {
324 276837 int i = get_v_length(val);
325
326
2/2
✓ Branch 0 taken 69931 times.
✓ Branch 1 taken 276837 times.
346768 while (--i > 0)
327 69931 avio_w8(bc, 128 | (uint8_t)(val >> (7*i)));
328
329 276837 avio_w8(bc, val & 127);
330 276837 }
331
332 8648 static void put_tt(NUTContext *nut, AVRational *time_base, AVIOContext *bc, uint64_t val)
333 {
334 8648 val *= nut->time_base_count;
335 8648 val += time_base - nut->time_base;
336 8648 put_v(bc, val);
337 8648 }
338 /**
339 * Store a string as vb.
340 */
341 11556 static void put_str(AVIOContext *bc, const char *string)
342 {
343 11556 size_t len = strlen(string);
344
345 11556 put_v(bc, len);
346 11556 avio_write(bc, string, len);
347 11556 }
348
349 22851 static void put_s(AVIOContext *bc, int64_t val)
350 {
351 22851 put_v(bc, 2 * FFABS(val) - (val > 0));
352 22851 }
353
354 19970 static void put_packet(NUTContext *nut, AVIOContext *bc, AVIOContext *dyn_bc,
355 uint64_t startcode)
356 {
357 19970 uint8_t *dyn_buf = NULL;
358 19970 int dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
359 19970 int forw_ptr = dyn_size + 4;
360
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19970 times.
19970 if (forw_ptr > 4096)
362 ✗ ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
363 19970 avio_wb64(bc, startcode);
364 19970 put_v(bc, forw_ptr);
365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19970 times.
19970 if (forw_ptr > 4096)
366 ✗ avio_wl32(bc, ffio_get_checksum(bc));
367
368 19970 ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
369 19970 avio_write(bc, dyn_buf, dyn_size);
370 19970 avio_wl32(bc, ffio_get_checksum(bc));
371
372 19970 ffio_reset_dyn_buf(dyn_bc);
373 19970 }
374
375 2828 static void write_mainheader(NUTContext *nut, AVIOContext *bc)
376 {
377 int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields,
378 tmp_head_idx;
379 int64_t tmp_match;
380
381 2828 put_v(bc, nut->version);
382
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2826 times.
2828 if (nut->version > 3)
383 2 put_v(bc, nut->minor_version = 1);
384 2828 put_v(bc, nut->avf->nb_streams);
385 2828 put_v(bc, nut->max_distance);
386 2828 put_v(bc, nut->time_base_count);
387
388
2/2
✓ Branch 0 taken 2842 times.
✓ Branch 1 taken 2828 times.
5670 for (i = 0; i < nut->time_base_count; i++) {
389 2842 put_v(bc, nut->time_base[i].num);
390 2842 put_v(bc, nut->time_base[i].den);
391 }
392
393 2828 tmp_pts = 0;
394 2828 tmp_mul = 1;
395 2828 tmp_stream = 0;
396 2828 tmp_match = 1 - (1LL << 62);
397 2828 tmp_head_idx = 0;
398
2/2
✓ Branch 0 taken 19889 times.
✓ Branch 1 taken 2828 times.
22717 for (i = 0; i < 256; ) {
399 19889 tmp_fields = 0;
400 19889 tmp_size = 0;
401 // tmp_res=0;
402
2/2
✓ Branch 0 taken 11376 times.
✓ Branch 1 taken 8513 times.
19889 if (tmp_pts != nut->frame_code[i].pts_delta ) tmp_fields = 1;
403
2/2
✓ Branch 0 taken 11359 times.
✓ Branch 1 taken 8530 times.
19889 if (tmp_mul != nut->frame_code[i].size_mul ) tmp_fields = 2;
404
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 19880 times.
19889 if (tmp_stream != nut->frame_code[i].stream_id ) tmp_fields = 3;
405
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 19861 times.
19889 if (tmp_size != nut->frame_code[i].size_lsb ) tmp_fields = 4;
406 // if (tmp_res != nut->frame_code[i].res ) tmp_fields=5;
407
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 19887 times.
19889 if (tmp_head_idx != nut->frame_code[i].header_idx) tmp_fields = 8;
408
409 19889 tmp_pts = nut->frame_code[i].pts_delta;
410 19889 tmp_flags = nut->frame_code[i].flags;
411 19889 tmp_stream = nut->frame_code[i].stream_id;
412 19889 tmp_mul = nut->frame_code[i].size_mul;
413 19889 tmp_size = nut->frame_code[i].size_lsb;
414 // tmp_res = nut->frame_code[i].res;
415 19889 tmp_head_idx = nut->frame_code[i].header_idx;
416
417
2/2
✓ Branch 0 taken 741029 times.
✓ Branch 1 taken 2828 times.
743857 for (j = 0; i < 256; j++, i++) {
418
2/2
✓ Branch 0 taken 2828 times.
✓ Branch 1 taken 738201 times.
741029 if (i == 'N') {
419 2828 j--;
420 2828 continue;
421 }
422
2/2
✓ Branch 0 taken 726825 times.
✓ Branch 1 taken 11376 times.
738201 if (nut->frame_code[i].pts_delta != tmp_pts ||
423
2/2
✓ Branch 0 taken 721140 times.
✓ Branch 1 taken 5685 times.
726825 nut->frame_code[i].flags != tmp_flags ||
424
1/2
✓ Branch 0 taken 721140 times.
✗ Branch 1 not taken.
721140 nut->frame_code[i].stream_id != tmp_stream ||
425
1/2
✓ Branch 0 taken 721140 times.
✗ Branch 1 not taken.
721140 nut->frame_code[i].size_mul != tmp_mul ||
426
1/2
✓ Branch 0 taken 721140 times.
✗ Branch 1 not taken.
721140 nut->frame_code[i].size_lsb != tmp_size + j ||
427 // nut->frame_code[i].res != tmp_res ||
428
1/2
✓ Branch 0 taken 721140 times.
✗ Branch 1 not taken.
721140 nut->frame_code[i].header_idx != tmp_head_idx)
429 break;
430 }
431
2/2
✓ Branch 0 taken 5656 times.
✓ Branch 1 taken 14233 times.
19889 if (j != tmp_mul - tmp_size)
432 5656 tmp_fields = 6;
433
434 19889 put_v(bc, tmp_flags);
435 19889 put_v(bc, tmp_fields);
436
2/2
✓ Branch 0 taken 17053 times.
✓ Branch 1 taken 2836 times.
19889 if (tmp_fields > 0) put_s(bc, tmp_pts);
437
2/2
✓ Branch 0 taken 11373 times.
✓ Branch 1 taken 8516 times.
19889 if (tmp_fields > 1) put_v(bc, tmp_mul);
438
2/2
✓ Branch 0 taken 5689 times.
✓ Branch 1 taken 14200 times.
19889 if (tmp_fields > 2) put_v(bc, tmp_stream);
439
2/2
✓ Branch 0 taken 5685 times.
✓ Branch 1 taken 14204 times.
19889 if (tmp_fields > 3) put_v(bc, tmp_size);
440
2/2
✓ Branch 0 taken 5657 times.
✓ Branch 1 taken 14232 times.
19889 if (tmp_fields > 4) put_v(bc, 0 /*tmp_res*/);
441
2/2
✓ Branch 0 taken 5657 times.
✓ Branch 1 taken 14232 times.
19889 if (tmp_fields > 5) put_v(bc, j);
442
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 19888 times.
19889 if (tmp_fields > 6) put_v(bc, tmp_match);
443
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 19888 times.
19889 if (tmp_fields > 7) put_v(bc, tmp_head_idx);
444 }
445 2828 put_v(bc, nut->header_count - 1);
446
2/2
✓ Branch 0 taken 16968 times.
✓ Branch 1 taken 2828 times.
19796 for (i = 1; i < nut->header_count; i++) {
447 16968 put_v(bc, nut->header_len[i]);
448 16968 avio_write(bc, nut->header[i], nut->header_len[i]);
449 }
450 // flags had been effectively introduced in version 4
451
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2826 times.
2828 if (nut->version > 3)
452 2 put_v(bc, nut->flags);
453 2828 }
454
455 2833 static int write_streamheader(AVFormatContext *avctx, AVIOContext *bc,
456 AVStream *st, int i)
457 {
458 2833 NUTContext *nut = avctx->priv_data;
459 2833 AVCodecParameters *par = st->codecpar;
460
461 2833 put_v(bc, i);
462
2/4
✓ Branch 0 taken 2808 times.
✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2833 switch (par->codec_type) {
463 2808 case AVMEDIA_TYPE_VIDEO: put_v(bc, 0); break;
464 25 case AVMEDIA_TYPE_AUDIO: put_v(bc, 1); break;
465 ✗ case AVMEDIA_TYPE_SUBTITLE: put_v(bc, 2); break;
466 ✗ default: put_v(bc, 3); break;
467 }
468 2833 put_v(bc, 4);
469
470
1/2
✓ Branch 0 taken 2833 times.
✗ Branch 1 not taken.
2833 if (par->codec_tag) {
471 2833 avio_wl32(bc, par->codec_tag);
472 } else {
473 ✗ av_log(avctx, AV_LOG_ERROR, "No codec tag defined for stream %d\n", i);
474 ✗ return AVERROR(EINVAL);
475 }
476
477 2833 put_v(bc, nut->stream[i].time_base - nut->time_base);
478 2833 put_v(bc, nut->stream[i].msb_pts_shift);
479 2833 put_v(bc, nut->stream[i].max_pts_distance);
480 2833 put_v(bc, par->video_delay);
481 2833 avio_w8(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
482
483 2833 put_v(bc, par->extradata_size);
484 2833 avio_write(bc, par->extradata, par->extradata_size);
485
486
2/3
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2808 times.
✗ Branch 2 not taken.
2833 switch (par->codec_type) {
487 25 case AVMEDIA_TYPE_AUDIO:
488 25 put_v(bc, par->sample_rate);
489 25 put_v(bc, 1);
490 25 put_v(bc, par->ch_layout.nb_channels);
491 25 break;
492 2808 case AVMEDIA_TYPE_VIDEO:
493 2808 put_v(bc, par->width);
494 2808 put_v(bc, par->height);
495
496
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 2789 times.
2808 if (st->sample_aspect_ratio.num <= 0 ||
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 st->sample_aspect_ratio.den <= 0) {
498 2789 put_v(bc, 0);
499 2789 put_v(bc, 0);
500 } else {
501 19 put_v(bc, st->sample_aspect_ratio.num);
502 19 put_v(bc, st->sample_aspect_ratio.den);
503 }
504 2808 put_v(bc, 0); /* csp type -- unknown */
505 2808 break;
506 ✗ default:
507 ✗ break;
508 }
509 2833 return 0;
510 }
511
512 5777 static int add_info(AVIOContext *bc, const char *type, const char *value)
513 {
514 5777 put_str(bc, type);
515 5777 put_s(bc, -1);
516 5777 put_str(bc, value);
517 5777 return 1;
518 }
519
520 2828 static int write_globalinfo(NUTContext *nut, AVIOContext *bc)
521 {
522 2828 AVFormatContext *s = nut->avf;
523 2828 const AVDictionaryEntry *t = NULL;
524 AVIOContext *dyn_bc;
525 2828 uint8_t *dyn_buf = NULL;
526 2828 int count = 0, dyn_size;
527 2828 int ret = avio_open_dyn_buf(&dyn_bc);
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2828 times.
2828 if (ret < 0)
529 ✗ return ret;
530
531 2828 ff_standardize_creation_time(s);
532
2/2
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 2828 times.
2886 while ((t = av_dict_iterate(s->metadata, t)))
533 58 count += add_info(dyn_bc, t->key, t->value);
534
535 2828 put_v(bc, 0); //stream_if_plus1
536 2828 put_v(bc, 0); //chapter_id
537 2828 put_v(bc, 0); //timestamp_start
538 2828 put_v(bc, 0); //length
539
540 2828 put_v(bc, count);
541
542 2828 dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
543 2828 avio_write(bc, dyn_buf, dyn_size);
544 2828 av_free(dyn_buf);
545 2828 return 0;
546 }
547
548 2833 static int write_streaminfo(NUTContext *nut, AVIOContext *bc, int stream_id) {
549 2833 AVFormatContext *s= nut->avf;
550 2833 AVStream* st = s->streams[stream_id];
551 2833 const AVDictionaryEntry *t = NULL;
552 AVIOContext *dyn_bc;
553 2833 uint8_t *dyn_buf=NULL;
554 2833 int count=0, dyn_size, i;
555 2833 int ret = avio_open_dyn_buf(&dyn_bc);
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2833 times.
2833 if (ret < 0)
557 ✗ return ret;
558
559
2/2
✓ Branch 1 taken 2864 times.
✓ Branch 2 taken 2833 times.
5697 while ((t = av_dict_iterate(st->metadata, t)))
560 2864 count += add_info(dyn_bc, t->key, t->value);
561
2/2
✓ Branch 0 taken 16998 times.
✓ Branch 1 taken 2833 times.
19831 for (i=0; ff_nut_dispositions[i].flag; ++i) {
562
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16982 times.
16998 if (st->disposition & ff_nut_dispositions[i].flag)
563 16 count += add_info(dyn_bc, "Disposition", ff_nut_dispositions[i].str);
564 }
565
2/2
✓ Branch 0 taken 2808 times.
✓ Branch 1 taken 25 times.
2833 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
566 uint8_t buf[256];
567
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2806 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2808 if (st->r_frame_rate.num>0 && st->r_frame_rate.den>0)
568 2 snprintf(buf, sizeof(buf), "%d/%d", st->r_frame_rate.num, st->r_frame_rate.den);
569 else
570 2806 snprintf(buf, sizeof(buf), "%d/%d", st->avg_frame_rate.num, st->avg_frame_rate.den);
571 2808 count += add_info(dyn_bc, "r_frame_rate", buf);
572 }
573 2833 dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
574
575
1/2
✓ Branch 0 taken 2833 times.
✗ Branch 1 not taken.
2833 if (count) {
576 2833 put_v(bc, stream_id + 1); //stream_id_plus1
577 2833 put_v(bc, 0); //chapter_id
578 2833 put_v(bc, 0); //timestamp_start
579 2833 put_v(bc, 0); //length
580
581 2833 put_v(bc, count);
582
583 2833 avio_write(bc, dyn_buf, dyn_size);
584 }
585
586 2833 av_free(dyn_buf);
587 2833 return count;
588 }
589
590 20 static int write_chapter(NUTContext *nut, AVIOContext *bc, int id)
591 {
592 AVIOContext *dyn_bc;
593 20 uint8_t *dyn_buf = NULL;
594 20 const AVDictionaryEntry *t = NULL;
595 20 AVChapter *ch = nut->avf->chapters[id];
596 20 int ret, dyn_size, count = 0;
597
598 20 ret = avio_open_dyn_buf(&dyn_bc);
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0)
600 ✗ return ret;
601
602 20 put_v(bc, 0); // stream_id_plus1
603 20 put_s(bc, id + 1); // chapter_id
604 20 put_tt(nut, nut->chapter[id].time_base, bc, ch->start); // chapter_start
605 20 put_v(bc, ch->end - ch->start); // chapter_len
606
607
2/2
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 20 times.
51 while ((t = av_dict_iterate(ch->metadata, t)))
608 31 count += add_info(dyn_bc, t->key, t->value);
609
610 20 put_v(bc, count);
611
612 20 dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
613 20 avio_write(bc, dyn_buf, dyn_size);
614 20 av_freep(&dyn_buf);
615 20 return 0;
616 }
617
618 2828 static int write_index(NUTContext *nut, AVIOContext *bc) {
619 int i;
620 2828 Syncpoint dummy= { .pos= 0 };
621 2828 Syncpoint *next_node[2] = { NULL };
622 2828 int64_t startpos = avio_tell(bc);
623 int64_t payload_size;
624
625 2828 put_tt(nut, nut->max_pts_tb, bc, nut->max_pts);
626
627 2828 put_v(bc, nut->sp_count);
628
629
2/2
✓ Branch 0 taken 5800 times.
✓ Branch 1 taken 2828 times.
8628 for (i=0; i<nut->sp_count; i++) {
630 5800 av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, (void**)next_node);
631 5800 put_v(bc, (next_node[1]->pos >> 4) - (dummy.pos>>4));
632 5800 dummy.pos = next_node[1]->pos;
633 }
634
635
2/2
✓ Branch 0 taken 2833 times.
✓ Branch 1 taken 2828 times.
5661 for (i=0; i<nut->avf->nb_streams; i++) {
636 2833 StreamContext *nus= &nut->stream[i];
637 2833 int64_t last_pts= -1;
638 int j, k;
639
2/2
✓ Branch 0 taken 3319 times.
✓ Branch 1 taken 2833 times.
6152 for (j=0; j<nut->sp_count; j++) {
640 int flag;
641 3319 int n = 0;
642
643
3/4
✓ Branch 0 taken 486 times.
✓ Branch 1 taken 2833 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 486 times.
3319 if (j && nus->keyframe_pts[j] == nus->keyframe_pts[j-1]) {
644 ✗ av_log(nut->avf, AV_LOG_WARNING, "Multiple keyframes with same PTS\n");
645 ✗ nus->keyframe_pts[j] = AV_NOPTS_VALUE;
646 }
647
648 3319 flag = (nus->keyframe_pts[j] != AV_NOPTS_VALUE) ^ (j+1 == nut->sp_count);
649
4/4
✓ Branch 0 taken 5846 times.
✓ Branch 1 taken 462 times.
✓ Branch 2 taken 2989 times.
✓ Branch 3 taken 2857 times.
6308 for (; j<nut->sp_count && (nus->keyframe_pts[j] != AV_NOPTS_VALUE) == flag; j++)
650 2989 n++;
651
652 3319 put_v(bc, 1 + 2 * flag + 4 * n);
653
4/4
✓ Branch 0 taken 6308 times.
✓ Branch 1 taken 2857 times.
✓ Branch 2 taken 5846 times.
✓ Branch 3 taken 462 times.
9165 for (k= j - n; k<=j && k<nut->sp_count; k++) {
654
2/2
✓ Branch 0 taken 2911 times.
✓ Branch 1 taken 2935 times.
5846 if (nus->keyframe_pts[k] == AV_NOPTS_VALUE)
655 2911 continue;
656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2935 times.
2935 av_assert0(nus->keyframe_pts[k] > last_pts);
657 2935 put_v(bc, nus->keyframe_pts[k] - last_pts);
658 2935 last_pts = nus->keyframe_pts[k];
659 }
660 }
661 }
662
663 2828 payload_size = avio_tell(bc) - startpos + 8 + 4;
664
665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2828 times.
2828 avio_wb64(bc, 8 + payload_size + av_log2(payload_size) / 7 + 1 + 4*(payload_size > 4096));
666
667 2828 return 0;
668 }
669
670 2828 static int write_headers(AVFormatContext *avctx, AVIOContext *bc)
671 {
672 2828 NUTContext *nut = avctx->priv_data;
673 AVIOContext *dyn_bc;
674 int i, ret;
675
676 2828 ff_metadata_conv_ctx(avctx, ff_nut_metadata_conv, NULL);
677
678 2828 ret = avio_open_dyn_buf(&dyn_bc);
679
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2828 times.
2828 if (ret < 0)
680 ✗ return ret;
681 2828 write_mainheader(nut, dyn_bc);
682 2828 put_packet(nut, bc, dyn_bc, MAIN_STARTCODE);
683
684
2/2
✓ Branch 0 taken 2833 times.
✓ Branch 1 taken 2828 times.
5661 for (i = 0; i < nut->avf->nb_streams; i++) {
685 2833 ret = write_streamheader(avctx, dyn_bc, nut->avf->streams[i], i);
686
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2833 times.
2833 if (ret < 0) {
687 ✗ goto fail;
688 }
689 2833 put_packet(nut, bc, dyn_bc, STREAM_STARTCODE);
690 }
691
692 2828 write_globalinfo(nut, dyn_bc);
693 2828 put_packet(nut, bc, dyn_bc, INFO_STARTCODE);
694
695
2/2
✓ Branch 0 taken 2833 times.
✓ Branch 1 taken 2828 times.
5661 for (i = 0; i < nut->avf->nb_streams; i++) {
696 2833 ret = write_streaminfo(nut, dyn_bc, i);
697
1/2
✓ Branch 0 taken 2833 times.
✗ Branch 1 not taken.
2833 if (ret > 0)
698 2833 put_packet(nut, bc, dyn_bc, INFO_STARTCODE);
699 ✗ else if (ret < 0) {
700 ✗ goto fail;
701 }
702 }
703
704
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2828 times.
2848 for (i = 0; i < nut->avf->nb_chapters; i++) {
705 20 ret = write_chapter(nut, dyn_bc, i);
706
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0) {
707 ✗ goto fail;
708 }
709 20 put_packet(nut, bc, dyn_bc, INFO_STARTCODE);
710 }
711
712 2828 nut->last_syncpoint_pos = INT_MIN;
713 2828 nut->header_count++;
714
715 2828 ret = 0;
716 2828 fail:
717 2828 ffio_free_dyn_buf(&dyn_bc);
718
719 2828 return ret;
720 }
721
722 2828 static int nut_write_header(AVFormatContext *s)
723 {
724 2828 NUTContext *nut = s->priv_data;
725 2828 AVIOContext *bc = s->pb;
726 int i, j, ret;
727
728 2828 nut->avf = s;
729
730
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2826 times.
2828 nut->version = FFMAX(NUT_STABLE_VERSION, 3 + !!nut->flags);
731
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2826 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2828 if (nut->version > 3 && s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
732 ✗ av_log(s, AV_LOG_ERROR,
733 "The additional syncpoint modes require version %d, "
734 "that is currently not finalized, "
735 "please set -f_strict experimental in order to enable it.\n",
736 nut->version);
737 ✗ return AVERROR_EXPERIMENTAL;
738 }
739
740 2828 nut->stream = av_calloc(s->nb_streams, sizeof(*nut->stream ));
741 2828 nut->chapter = av_calloc(s->nb_chapters, sizeof(*nut->chapter));
742 5656 nut->time_base= av_calloc(s->nb_streams +
743 2828 s->nb_chapters, sizeof(*nut->time_base));
744
3/6
✓ Branch 0 taken 2828 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2828 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2828 times.
2828 if (!nut->stream || !nut->chapter || !nut->time_base)
745 ✗ return AVERROR(ENOMEM);
746
747
2/2
✓ Branch 0 taken 2833 times.
✓ Branch 1 taken 2828 times.
5661 for (i = 0; i < s->nb_streams; i++) {
748 2833 AVStream *st = s->streams[i];
749 int ssize;
750 AVRational time_base;
751 2833 ff_parse_specific_params(st, &time_base.den, &ssize, &time_base.num);
752
753
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2808 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
2833 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->codecpar->sample_rate) {
754 25 time_base = (AVRational) {1, st->codecpar->sample_rate};
755 } else {
756 2808 time_base = choose_timebase(s, st, 48000);
757 }
758
759 2833 avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
760
761
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2832 times.
2838 for (j = 0; j < nut->time_base_count; j++)
762
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 if (!memcmp(&time_base, &nut->time_base[j], sizeof(AVRational))) {
763 1 break;
764 }
765 2833 nut->time_base[j] = time_base;
766 2833 nut->stream[i].time_base = &nut->time_base[j];
767
2/2
✓ Branch 0 taken 2832 times.
✓ Branch 1 taken 1 times.
2833 if (j == nut->time_base_count)
768 2832 nut->time_base_count++;
769
770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2833 times.
2833 if (INT64_C(1000) * time_base.num >= time_base.den)
771 ✗ nut->stream[i].msb_pts_shift = 7;
772 else
773 2833 nut->stream[i].msb_pts_shift = 14;
774 2833 nut->stream[i].max_pts_distance =
775 2833 FFMAX(time_base.den, time_base.num) / time_base.num;
776 }
777
778
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2828 times.
2848 for (i = 0; i < s->nb_chapters; i++) {
779 20 AVChapter *ch = s->chapters[i];
780
781
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
40 for (j = 0; j < nut->time_base_count; j++)
782
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 20 times.
30 if (!memcmp(&ch->time_base, &nut->time_base[j], sizeof(AVRational)))
783 10 break;
784
785 20 nut->time_base[j] = ch->time_base;
786 20 nut->chapter[i].time_base = &nut->time_base[j];
787
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (j == nut->time_base_count)
788 10 nut->time_base_count++;
789 }
790
791 2828 nut->max_distance = MAX_DISTANCE;
792 2828 build_elision_headers(s);
793 2828 build_frame_code(s);
794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2828 times.
2828 av_assert0(nut->frame_code['N'].flags == FLAG_INVALID);
795
796 2828 avio_write(bc, ID_STRING, strlen(ID_STRING));
797 2828 avio_w8(bc, 0);
798
799
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2828 times.
2828 if ((ret = write_headers(s, bc)) < 0)
800 ✗ return ret;
801
802
1/2
✓ Branch 0 taken 2828 times.
✗ Branch 1 not taken.
2828 if (s->avoid_negative_ts < 0)
803 2828 s->avoid_negative_ts = 1;
804
805 2828 return 0;
806 }
807
808 1648714 static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc,
809 AVPacket *pkt)
810 {
811 1648714 int flags = 0;
812
813
2/2
✓ Branch 0 taken 1594104 times.
✓ Branch 1 taken 54610 times.
1648714 if (pkt->flags & AV_PKT_FLAG_KEY)
814 1594104 flags |= FLAG_KEY;
815
2/2
✓ Branch 0 taken 23364 times.
✓ Branch 1 taken 1625350 times.
1648714 if (pkt->stream_index != fc->stream_id)
816 23364 flags |= FLAG_STREAM_ID;
817
2/2
✓ Branch 0 taken 1607202 times.
✓ Branch 1 taken 41512 times.
1648714 if (pkt->size / fc->size_mul)
818 1607202 flags |= FLAG_SIZE_MSB;
819
2/2
✓ Branch 0 taken 1564056 times.
✓ Branch 1 taken 84658 times.
1648714 if (pkt->pts - nus->last_pts != fc->pts_delta)
820 1564056 flags |= FLAG_CODED_PTS;
821
4/4
✓ Branch 0 taken 48768 times.
✓ Branch 1 taken 1599946 times.
✓ Branch 2 taken 254 times.
✓ Branch 3 taken 48514 times.
1648714 if (pkt->side_data_elems && nut->version > 3)
822 254 flags |= FLAG_SM_DATA;
823
2/2
✓ Branch 0 taken 1217168 times.
✓ Branch 1 taken 431546 times.
1648714 if (pkt->size > 2 * nut->max_distance)
824 1217168 flags |= FLAG_CHECKSUM;
825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1648714 times.
1648714 if (FFABS(pkt->pts - nus->last_pts) > nus->max_pts_distance)
826 ✗ flags |= FLAG_CHECKSUM;
827
2/2
✓ Branch 0 taken 8103 times.
✓ Branch 1 taken 1640611 times.
1648714 if (fc->header_idx)
828
1/2
✓ Branch 0 taken 8103 times.
✗ Branch 1 not taken.
8103 if (pkt->size < nut->header_len[fc->header_idx] ||
829
2/2
✓ Branch 0 taken 4953 times.
✓ Branch 1 taken 3150 times.
8103 pkt->size > 4096 ||
830 4953 memcmp(pkt->data, nut->header [fc->header_idx],
831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4953 times.
4953 nut->header_len[fc->header_idx]))
832 3150 flags |= FLAG_HEADER_IDX;
833
834 1648714 return flags | (fc->flags & FLAG_CODED);
835 }
836
837 6491 static int find_best_header_idx(NUTContext *nut, AVPacket *pkt)
838 {
839 int i;
840 6491 int best_i = 0;
841 6491 int best_len = 0;
842
843
2/2
✓ Branch 0 taken 5865 times.
✓ Branch 1 taken 626 times.
6491 if (pkt->size > 4096)
844 5865 return 0;
845
846
2/2
✓ Branch 0 taken 4382 times.
✓ Branch 1 taken 626 times.
5008 for (i = 1; i < nut->header_count; i++)
847
2/2
✓ Branch 0 taken 4370 times.
✓ Branch 1 taken 12 times.
4382 if (pkt->size >= nut->header_len[i]
848
2/2
✓ Branch 0 taken 3244 times.
✓ Branch 1 taken 1126 times.
4370 && nut->header_len[i] > best_len
849
2/2
✓ Branch 0 taken 209 times.
✓ Branch 1 taken 3035 times.
3244 && !memcmp(pkt->data, nut->header[i], nut->header_len[i])) {
850 209 best_i = i;
851 209 best_len = nut->header_len[i];
852 }
853 626 return best_i;
854 }
855
856 2 static int write_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta)
857 {
858 int ret, i, dyn_size;
859 unsigned flags;
860 AVIOContext *dyn_bc;
861 2 int sm_data_count = 0;
862 uint8_t tmp[256];
863 uint8_t *dyn_buf;
864
865 2 ret = avio_open_dyn_buf(&dyn_bc);
866
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
867 ✗ return ret;
868
869
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (i = 0; i<pkt->side_data_elems; i++) {
870 2 const uint8_t *data = pkt->side_data[i].data;
871 2 int size = pkt->side_data[i].size;
872 2 const uint8_t *data_end = data + size;
873
874
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (is_meta) {
875
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if ( pkt->side_data[i].type == AV_PKT_DATA_METADATA_UPDATE
876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 || pkt->side_data[i].type == AV_PKT_DATA_STRINGS_METADATA) {
877 ✗ if (!size || data[size-1]) {
878 ✗ ret = AVERROR(EINVAL);
879 ✗ goto fail;
880 }
881 ✗ while (data < data_end) {
882 ✗ const uint8_t *key = data;
883 ✗ const uint8_t *val = data + strlen(key) + 1;
884
885 ✗ if(val >= data_end) {
886 ✗ ret = AVERROR(EINVAL);
887 ✗ goto fail;
888 }
889 ✗ put_str(dyn_bc, key);
890 ✗ put_s(dyn_bc, -1);
891 ✗ put_str(dyn_bc, val);
892 ✗ data = val + strlen(val) + 1;
893 ✗ sm_data_count++;
894 }
895 }
896 } else {
897
1/5
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 switch (pkt->side_data[i].type) {
898 1 case AV_PKT_DATA_PALETTE:
899 case AV_PKT_DATA_NEW_EXTRADATA:
900 case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL:
901 default:
902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
903 ✗ put_str(dyn_bc, "Palette");
904
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if(pkt->side_data[i].type == AV_PKT_DATA_NEW_EXTRADATA) {
905 1 put_str(dyn_bc, "Extradata");
906 ✗ } else if(pkt->side_data[i].type == AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL) {
907 ✗ snprintf(tmp, sizeof(tmp), "CodecSpecificSide%"PRId64"", AV_RB64(data));
908 ✗ put_str(dyn_bc, tmp);
909 ✗ data += 8;
910 ✗ size -= 8;
911 } else {
912 ✗ snprintf(tmp, sizeof(tmp), "UserData%s-SD-%d",
913 ✗ (s->flags & AVFMT_FLAG_BITEXACT) ? "Lavf" : LIBAVFORMAT_IDENT,
914 ✗ pkt->side_data[i].type);
915 ✗ put_str(dyn_bc, tmp);
916 }
917 1 put_s(dyn_bc, -2);
918 1 put_str(dyn_bc, "bin");
919 1 put_v(dyn_bc, size);
920 if (HAVE_BIGENDIAN && pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
921 for (int j = 0; j + 4 <= size; j += 4)
922 avio_wl32(dyn_bc, AV_RN32(data + j));
923 avio_write(dyn_bc, data + (size & ~3), size & 3);
924 } else
925 1 avio_write(dyn_bc, data, size);
926 1 sm_data_count++;
927 1 break;
928 ✗ case AV_PKT_DATA_PARAM_CHANGE:
929 ✗ flags = bytestream_get_le32(&data);
930 ✗ if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
931 ✗ put_str(dyn_bc, "SampleRate");
932 ✗ put_s(dyn_bc, bytestream_get_le32(&data));
933 ✗ sm_data_count++;
934 }
935 ✗ if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
936 ✗ put_str(dyn_bc, "Width");
937 ✗ put_s(dyn_bc, bytestream_get_le32(&data));
938 ✗ put_str(dyn_bc, "Height");
939 ✗ put_s(dyn_bc, bytestream_get_le32(&data));
940 ✗ sm_data_count+=2;
941 }
942 ✗ break;
943 ✗ case AV_PKT_DATA_SKIP_SAMPLES:
944 ✗ if (AV_RL32(data)) {
945 ✗ put_str(dyn_bc, "SkipStart");
946 ✗ put_s(dyn_bc, (unsigned)AV_RL32(data));
947 ✗ sm_data_count++;
948 }
949 ✗ if (AV_RL32(data+4)) {
950 ✗ put_str(dyn_bc, "SkipEnd");
951 ✗ put_s(dyn_bc, (unsigned)AV_RL32(data+4));
952 ✗ sm_data_count++;
953 }
954 ✗ break;
955 ✗ case AV_PKT_DATA_METADATA_UPDATE:
956 case AV_PKT_DATA_STRINGS_METADATA:
957 case AV_PKT_DATA_QUALITY_STATS:
958 // belongs into meta, not side data
959 ✗ break;
960 ✗ case AV_PKT_DATA_IAMF_MIX_GAIN_PARAM:
961 case AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM:
962 case AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM:
963 ✗ break;
964 }
965 }
966 }
967
968 2 fail:
969 2 put_v(bc, sm_data_count);
970 2 dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
971 2 avio_write(bc, dyn_buf, dyn_size);
972 2 av_freep(&dyn_buf);
973
974 2 return ret;
975 }
976
977 6491 static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
978 {
979 6491 NUTContext *nut = s->priv_data;
980 6491 StreamContext *nus = &nut->stream[pkt->stream_index];
981 6491 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 6491 int key_frame = !!(pkt->flags & AV_PKT_FLAG_KEY);
987 6491 int store_sp = 0;
988 6491 int ret = 0;
989 6491 int sm_size = 0;
990 6491 int data_size = pkt->size;
991 6491 uint8_t *sm_buf = NULL;
992
993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6491 times.
6491 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 192 times.
✓ Branch 1 taken 6299 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 191 times.
6491 if (pkt->side_data_elems && nut->version > 3) {
1003 1 ret = avio_open_dyn_buf(&sm_bc);
1004
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1005 ✗ return ret;
1006 1 ret = write_sm_data(s, sm_bc, pkt, 0);
1007
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ret >= 0)
1008 1 ret = write_sm_data(s, sm_bc, pkt, 1);
1009 1 sm_size = avio_close_dyn_buf(sm_bc, &sm_buf);
1010
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
1011 ✗ goto fail;
1012 1 data_size += sm_size;
1013 }
1014
1015
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6491 times.
6491 if (1LL << (20 + 3 * nut->header_count) <= avio_tell(bc))
1016 ✗ write_headers(s, bc);
1017
1018
4/4
✓ Branch 0 taken 6276 times.
✓ Branch 1 taken 215 times.
✓ Branch 2 taken 2845 times.
✓ Branch 3 taken 3431 times.
6491 if (key_frame && !(nus->last_flags & FLAG_KEY))
1019 2845 store_sp = 1;
1020
1021
2/2
✓ Branch 1 taken 5809 times.
✓ Branch 2 taken 682 times.
6491 if (data_size + 30 /*FIXME check*/ + avio_tell(bc) >= nut->last_syncpoint_pos + nut->max_distance)
1022 5809 store_sp = 1;
1023
1024 //FIXME: Ensure store_sp is 1 in the first place.
1025
1026
2/2
✓ Branch 0 taken 5813 times.
✓ Branch 1 taken 678 times.
6491 if (store_sp &&
1027
4/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5798 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 13 times.
5813 (!(nut->flags & NUT_PIPE) || nut->last_syncpoint_pos == INT_MIN)) {
1028 5800 int64_t sp_pos = INT64_MAX;
1029
1030 5800 ff_nut_reset_ts(nut, *nus->time_base, pkt->dts);
1031
2/2
✓ Branch 0 taken 5846 times.
✓ Branch 1 taken 5800 times.
11646 for (i = 0; i < s->nb_streams; i++) {
1032 5846 AVStream *st = s->streams[i];
1033 5846 FFStream *const sti = ffstream(st);
1034 5846 int64_t dts_tb = av_rescale_rnd(pkt->dts,
1035 5846 nus->time_base->num * (int64_t)nut->stream[i].time_base->den,
1036 5846 nus->time_base->den * (int64_t)nut->stream[i].time_base->num,
1037 AV_ROUND_DOWN);
1038 5846 int index = av_index_search_timestamp(st, dts_tb,
1039 AVSEEK_FLAG_BACKWARD);
1040
2/2
✓ Branch 0 taken 3007 times.
✓ Branch 1 taken 2839 times.
5846 if (index >= 0) {
1041 3007 sp_pos = FFMIN(sp_pos, sti->index_entries[index].pos);
1042
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3007 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3007 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 5800 nut->last_syncpoint_pos = avio_tell(bc);
1052 5800 ret = avio_open_dyn_buf(&dyn_bc);
1053
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5800 times.
5800 if (ret < 0)
1054 ✗ goto fail;
1055 5800 put_tt(nut, nus->time_base, dyn_bc, pkt->dts);
1056
2/2
✓ Branch 0 taken 2972 times.
✓ Branch 1 taken 2828 times.
5800 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 5800 times.
5800 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 5800 put_packet(nut, bc, dyn_bc, SYNCPOINT_STARTCODE);
1063 5800 ffio_free_dyn_buf(&dyn_bc);
1064
1065
1/2
✓ Branch 0 taken 5800 times.
✗ Branch 1 not taken.
5800 if (nut->write_index) {
1066
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5800 times.
5800 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 3855 times.
✓ Branch 1 taken 1945 times.
5800 if ((1ll<<60) % nut->sp_count == 0)
1070
2/2
✓ Branch 0 taken 3873 times.
✓ Branch 1 taken 3855 times.
7728 for (unsigned i = 0; i < s->nb_streams; i++) {
1071 3873 StreamContext *nus = &nut->stream[i];
1072 3873 av_reallocp_array(&nus->keyframe_pts, 2*nut->sp_count, sizeof(*nus->keyframe_pts));
1073
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3873 times.
3873 if (!nus->keyframe_pts) {
1074 ✗ ret = AVERROR(ENOMEM);
1075 ✗ goto fail;
1076 }
1077
2/2
✓ Branch 0 taken 1040 times.
✓ Branch 1 taken 2833 times.
3873 for (int j = nut->sp_count == 1 ? 0 : nut->sp_count;
1078
2/2
✓ Branch 0 taken 10378 times.
✓ Branch 1 taken 3873 times.
14251 j < 2 * nut->sp_count; j++)
1079 10378 nus->keyframe_pts[j] = AV_NOPTS_VALUE;
1080 }
1081 }
1082 }
1083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6491 times.
6491 av_assert0(nus->last_pts != AV_NOPTS_VALUE);
1084
1085 6491 coded_pts = pkt->pts & ((1 << nus->msb_pts_shift) - 1);
1086
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6491 times.
6491 if (ff_lsb2full(nus, coded_pts) != pkt->pts)
1087 ✗ coded_pts = pkt->pts + (1 << nus->msb_pts_shift);
1088
1089 6491 best_header_idx = find_best_header_idx(nut, pkt);
1090
1091 6491 best_length = INT_MAX;
1092 6491 frame_code = -1;
1093
2/2
✓ Branch 0 taken 1661696 times.
✓ Branch 1 taken 6491 times.
1668187 for (i = 0; i < 256; i++) {
1094 1661696 int length = 0;
1095 1661696 FrameCode *fc = &nut->frame_code[i];
1096 1661696 int flags = fc->flags;
1097
1098
2/2
✓ Branch 0 taken 19473 times.
✓ Branch 1 taken 1642223 times.
1661696 if (flags & FLAG_INVALID)
1099 19473 continue;
1100 1642223 needed_flags = get_needed_flags(nut, nus, fc, pkt);
1101
1102
2/2
✓ Branch 0 taken 6491 times.
✓ Branch 1 taken 1635732 times.
1642223 if (flags & FLAG_CODED) {
1103 6491 length++;
1104 6491 flags = needed_flags;
1105 }
1106
1107
2/2
✓ Branch 0 taken 1618129 times.
✓ Branch 1 taken 24094 times.
1642223 if ((flags & needed_flags) != needed_flags)
1108 1618129 continue;
1109
1110
2/2
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 23775 times.
24094 if ((flags ^ needed_flags) & FLAG_KEY)
1111 319 continue;
1112
1113
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 23683 times.
23775 if (flags & FLAG_STREAM_ID)
1114 92 length += get_v_length(pkt->stream_index);
1115
1116
2/2
✓ Branch 0 taken 15145 times.
✓ Branch 1 taken 8630 times.
23775 if (data_size % fc->size_mul != fc->size_lsb)
1117 15145 continue;
1118
2/2
✓ Branch 0 taken 8599 times.
✓ Branch 1 taken 31 times.
8630 if (flags & FLAG_SIZE_MSB)
1119 8599 length += get_v_length(data_size / fc->size_mul);
1120
1121
2/2
✓ Branch 0 taken 4792 times.
✓ Branch 1 taken 3838 times.
8630 if (flags & FLAG_CHECKSUM)
1122 4792 length += 4;
1123
1124
2/2
✓ Branch 0 taken 8189 times.
✓ Branch 1 taken 441 times.
8630 if (flags & FLAG_CODED_PTS)
1125 8189 length += get_v_length(coded_pts);
1126
1127
2/2
✓ Branch 0 taken 6491 times.
✓ Branch 1 taken 2139 times.
8630 if ( (flags & FLAG_CODED)
1128
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 6327 times.
6491 && nut->header_len[best_header_idx] > nut->header_len[fc->header_idx] + 1) {
1129 164 flags |= FLAG_HEADER_IDX;
1130 }
1131
1132
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 8466 times.
8630 if (flags & FLAG_HEADER_IDX) {
1133 164 length += 1 - nut->header_len[best_header_idx];
1134 } else {
1135 8466 length -= nut->header_len[fc->header_idx];
1136 }
1137
1138 8630 length *= 4;
1139 8630 length += !(flags & FLAG_CODED_PTS);
1140 8630 length += !(flags & FLAG_CHECKSUM);
1141
1142
2/2
✓ Branch 0 taken 8472 times.
✓ Branch 1 taken 158 times.
8630 if (length < best_length) {
1143 8472 best_length = length;
1144 8472 frame_code = i;
1145 }
1146 }
1147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6491 times.
6491 av_assert0(frame_code != -1);
1148
1149 6491 fc = &nut->frame_code[frame_code];
1150 6491 flags = fc->flags;
1151 6491 needed_flags = get_needed_flags(nut, nus, fc, pkt);
1152 6491 header_idx = fc->header_idx;
1153
1154 6491 ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
1155 6491 avio_w8(bc, frame_code);
1156
2/2
✓ Branch 0 taken 4801 times.
✓ Branch 1 taken 1690 times.
6491 if (flags & FLAG_CODED) {
1157 4801 put_v(bc, (flags ^ needed_flags) & ~(FLAG_CODED));
1158 4801 flags = needed_flags;
1159 }
1160
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6487 times.
6491 if (flags & FLAG_STREAM_ID) put_v(bc, pkt->stream_index);
1161
2/2
✓ Branch 0 taken 6081 times.
✓ Branch 1 taken 410 times.
6491 if (flags & FLAG_CODED_PTS) put_v(bc, coded_pts);
1162
2/2
✓ Branch 0 taken 6460 times.
✓ Branch 1 taken 31 times.
6491 if (flags & FLAG_SIZE_MSB ) put_v(bc, data_size / fc->size_mul);
1163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6491 times.
6491 if (flags & FLAG_HEADER_IDX) put_v(bc, header_idx = best_header_idx);
1164
1165
2/2
✓ Branch 0 taken 4792 times.
✓ Branch 1 taken 1699 times.
6491 if (flags & FLAG_CHECKSUM) avio_wl32(bc, ffio_get_checksum(bc));
1166 1699 else ffio_get_checksum(bc);
1167
1168
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6490 times.
6491 if (flags & FLAG_SM_DATA) {
1169 1 avio_write(bc, sm_buf, sm_size);
1170 }
1171 6491 avio_write(bc, pkt->data + nut->header_len[header_idx], pkt->size - nut->header_len[header_idx]);
1172
1173 6491 nus->last_flags = flags;
1174 6491 nus->last_pts = pkt->pts;
1175
1176 //FIXME just store one per syncpoint
1177
4/4
✓ Branch 0 taken 6276 times.
✓ Branch 1 taken 215 times.
✓ Branch 2 taken 6272 times.
✓ Branch 3 taken 4 times.
6491 if (flags & FLAG_KEY && !(nut->flags & NUT_PIPE)) {
1178 6272 av_add_index_entry(
1179 6272 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 6272 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5761 times.
✓ Branch 3 taken 511 times.
6272 if (nus->keyframe_pts && nus->keyframe_pts[nut->sp_count] == AV_NOPTS_VALUE)
1186 5761 nus->keyframe_pts[nut->sp_count] = pkt->pts;
1187 }
1188
1189
4/4
✓ Branch 0 taken 3663 times.
✓ Branch 1 taken 2828 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 3652 times.
6491 if (!nut->max_pts_tb || av_compare_ts(nut->max_pts, *nut->max_pts_tb, pkt->pts, *nus->time_base) < 0) {
1190 6480 nut->max_pts = pkt->pts;
1191 6480 nut->max_pts_tb = nus->time_base;
1192 }
1193
1194 11 fail:
1195 6491 av_freep(&sm_buf);
1196
1197 6491 return ret;
1198 }
1199
1200 2828 static int nut_write_trailer(AVFormatContext *s)
1201 {
1202 2828 NUTContext *nut = s->priv_data;
1203 2828 AVIOContext *bc = s->pb, *dyn_bc;
1204 int ret;
1205
1206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2828 times.
2828 while (nut->header_count < 3)
1207 ✗ write_headers(s, bc);
1208
1209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2828 times.
2828 if (!nut->sp_count)
1210 ✗ return 0;
1211
1212 2828 ret = avio_open_dyn_buf(&dyn_bc);
1213
1/2
✓ Branch 0 taken 2828 times.
✗ Branch 1 not taken.
2828 if (ret >= 0) {
1214 av_assert1(nut->write_index); // sp_count should be 0 if no index is going to be written
1215 2828 write_index(nut, dyn_bc);
1216 2828 put_packet(nut, bc, dyn_bc, INDEX_STARTCODE);
1217 2828 ffio_free_dyn_buf(&dyn_bc);
1218 }
1219
1220 2828 return 0;
1221 }
1222
1223 2828 static void nut_write_deinit(AVFormatContext *s)
1224 {
1225 2828 NUTContext *nut = s->priv_data;
1226 int i;
1227
1228 2828 ff_nut_free_sp(nut);
1229
1/2
✓ Branch 0 taken 2828 times.
✗ Branch 1 not taken.
2828 if (nut->stream)
1230
2/2
✓ Branch 0 taken 2833 times.
✓ Branch 1 taken 2828 times.
5661 for (i=0; i<s->nb_streams; i++)
1231 2833 av_freep(&nut->stream[i].keyframe_pts);
1232
1233 2828 av_freep(&nut->stream);
1234 2828 av_freep(&nut->chapter);
1235 2828 av_freep(&nut->time_base);
1236 2828 }
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, .unit = "syncpoints" },
1242 { "default", "", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, E, .unit = "syncpoints" },
1243 { "none", "Disable syncpoints, low overhead and unseekable", 0, AV_OPT_TYPE_CONST, {.i64 = NUT_PIPE}, INT_MIN, INT_MAX, E, .unit = "syncpoints" },
1244 { "timestamped", "Extend syncpoints with a wallclock timestamp", 0, AV_OPT_TYPE_CONST, {.i64 = NUT_BROADCAST}, INT_MIN, INT_MAX, E, .unit = "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 FFOutputFormat ff_nut_muxer = {
1257 .p.name = "nut",
1258 .p.long_name = NULL_IF_CONFIG_SMALL("NUT"),
1259 .p.mime_type = "video/x-nut",
1260 .p.extensions = "nut",
1261 .priv_data_size = sizeof(NUTContext),
1262 .p.audio_codec = CONFIG_LIBVORBIS ? AV_CODEC_ID_VORBIS :
1263 CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_MP2,
1264 .p.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 .p.flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
1270 .p.codec_tag = ff_nut_codec_tags,
1271 .p.priv_class = &class,
1272 };
1273