FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/nutenc.c
Date: 2026-10-17 12:12:06
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 2781 static AVRational choose_timebase(AVFormatContext *s, AVStream *st, int min_precision)
49 {
50 2781 AVRational q = st->time_base;
51
52
4/4
✓ Branch 0 taken 16686 times.
✓ Branch 1 taken 2781 times.
✓ Branch 2 taken 19467 times.
✓ Branch 3 taken 2781 times.
22248 for (int j = 2; j < 14; j += 1 + (j > 2))
53
4/4
✓ Branch 0 taken 19579 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 119 times.
✓ Branch 3 taken 19460 times.
19586 while (q.den / q.num < min_precision && q.num % j == 0)
54 119 q.num /= j;
55
3/4
✓ Branch 0 taken 30539 times.
✓ Branch 1 taken 2781 times.
✓ Branch 2 taken 30539 times.
✗ Branch 3 not taken.
33320 while (q.den / q.num < min_precision && q.den < (1<<24))
56 30539 q.den <<= 1;
57
58 2781 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 2801 static void build_elision_headers(AVFormatContext *s)
146 {
147 2801 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 2801 nut->header_count = 7;
161
2/2
✓ Branch 0 taken 16806 times.
✓ Branch 1 taken 2801 times.
19607 for (i = 1; i < nut->header_count; i++) {
162 16806 nut->header_len[i] = headers[i - 1][0];
163 16806 nut->header[i] = &headers[i - 1][1];
164 }
165 2801 }
166
167 2801 static void build_frame_code(AVFormatContext *s)
168 {
169 2801 NUTContext *nut = s->priv_data;
170 int key_frame, index, pred, stream_id;
171 2801 int start = 1;
172 2801 int end = 254;
173 2801 int keyframe_0_esc = s->nb_streams > 2;
174 int pred_table[10];
175 FrameCode *ft;
176
177 2801 ft = &nut->frame_code[start];
178 2801 ft->flags = FLAG_CODED;
179 2801 ft->size_mul = 1;
180 2801 ft->pts_delta = 1;
181 2801 start++;
182
183
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2800 times.
2801 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 2801 int nb_frame_code_streams = FFMIN(s->nb_streams, (end - start) / 6);
193
194
2/2
✓ Branch 0 taken 2806 times.
✓ Branch 1 taken 2801 times.
5607 for (stream_id = 0; stream_id < nb_frame_code_streams; stream_id++) {
195 2806 int start2 = start + (end - start) * stream_id / nb_frame_code_streams;
196 2806 int end2 = start + (end - start) * (stream_id + 1) / nb_frame_code_streams;
197 2806 AVCodecParameters *par = s->streams[stream_id]->codecpar;
198 2806 int is_audio = par->codec_type == AVMEDIA_TYPE_AUDIO;
199 2806 int intra_only = /*codec->intra_only || */ is_audio;
200 int pred_count;
201 2806 int frame_size = 0;
202
203
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2781 times.
2806 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 2781 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 2780 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2780 times.
✗ Branch 3 not taken.
2781 if (f.den == 1 && f.num>0)
210 2780 frame_size = f.num;
211 }
212
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2781 times.
2806 if (!frame_size)
213 25 frame_size = 1;
214
215
2/2
✓ Branch 0 taken 5612 times.
✓ Branch 1 taken 2806 times.
8418 for (key_frame = 0; key_frame < 2; key_frame++) {
216
6/6
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 5562 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
5612 if (!intra_only || !keyframe_0_esc || key_frame != 0) {
217 5611 FrameCode *ft = &nut->frame_code[start2];
218 5611 ft->flags = FLAG_KEY * key_frame;
219 5611 ft->flags |= FLAG_SIZE_MSB | FLAG_CODED_PTS;
220 5611 ft->stream_id = stream_id;
221 5611 ft->size_mul = 1;
222
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 5562 times.
5611 if (is_audio)
223 49 ft->header_idx = find_header_idx(s, par, -1, key_frame);
224 5611 start2++;
225 }
226 }
227
228 2806 key_frame = intra_only;
229 #if 1
230
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2781 times.
2806 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 2781 FrameCode *ft = &nut->frame_code[start2];
255 2781 ft->flags = FLAG_KEY | FLAG_SIZE_MSB;
256 2781 ft->stream_id = stream_id;
257 2781 ft->size_mul = 1;
258 2781 ft->pts_delta = frame_size;
259 2781 start2++;
260 }
261 #endif
262
263
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2794 times.
2806 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 2794 times.
2794 } 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 2794 pred_count = 1;
277 2794 pred_table[0] = 1;
278 }
279
280
2/2
✓ Branch 0 taken 2854 times.
✓ Branch 1 taken 2806 times.
5660 for (pred = 0; pred < pred_count; pred++) {
281 2854 int start3 = start2 + (end2 - start2) * pred / pred_count;
282 2854 int end3 = start2 + (end2 - start2) * (pred + 1) / pred_count;
283
284 2854 pred_table[pred] *= frame_size;
285
286
2/2
✓ Branch 0 taken 697359 times.
✓ Branch 1 taken 2854 times.
700213 for (index = start3; index < end3; index++) {
287 697359 FrameCode *ft = &nut->frame_code[index];
288 697359 ft->flags = FLAG_KEY * key_frame;
289 697359 ft->flags |= FLAG_SIZE_MSB;
290 697359 ft->stream_id = stream_id;
291 //FIXME use single byte size and pred from last
292 697359 ft->size_mul = end3 - start3;
293 697359 ft->size_lsb = index - start3;
294 697359 ft->pts_delta = pred_table[pred];
295
2/2
✓ Branch 0 taken 5604 times.
✓ Branch 1 taken 691755 times.
697359 if (is_audio)
296 5604 ft->header_idx = find_header_idx(s, par, -1, key_frame);
297 }
298 }
299 }
300 2801 memmove(&nut->frame_code['N' + 1], &nut->frame_code['N'], sizeof(FrameCode) * (255 - 'N'));
301 2801 nut->frame_code[0].flags =
302 2801 nut->frame_code[255].flags =
303 2801 nut->frame_code['N'].flags = FLAG_INVALID;
304 2801 }
305
306 /**
307 * Get the length in bytes which is needed to store val as v.
308 */
309 291018 static int get_v_length(uint64_t val)
310 {
311 291018 int i = 1;
312
313
2/2
✓ Branch 0 taken 88424 times.
✓ Branch 1 taken 291018 times.
379442 while (val >>= 7)
314 88424 i++;
315
316 291018 return i;
317 }
318
319 /**
320 * Put val using a variable number of bytes.
321 */
322 274268 static void put_v(AVIOContext *bc, uint64_t val)
323 {
324 274268 int i = get_v_length(val);
325
326
2/2
✓ Branch 0 taken 69315 times.
✓ Branch 1 taken 274268 times.
343583 while (--i > 0)
327 69315 avio_w8(bc, 128 | (uint8_t)(val >> (7*i)));
328
329 274268 avio_w8(bc, val & 127);
330 274268 }
331
332 8571 static void put_tt(NUTContext *nut, AVRational *time_base, AVIOContext *bc, uint64_t val)
333 {
334 8571 val *= nut->time_base_count;
335 8571 val += time_base - nut->time_base;
336 8571 put_v(bc, val);
337 8571 }
338 /**
339 * Store a string as vb.
340 */
341 11448 static void put_str(AVIOContext *bc, const char *string)
342 {
343 11448 size_t len = strlen(string);
344
345 11448 put_v(bc, len);
346 11448 avio_write(bc, string, len);
347 11448 }
348
349 22638 static void put_s(AVIOContext *bc, int64_t val)
350 {
351 22638 put_v(bc, 2 * FFABS(val) - (val > 0));
352 22638 }
353
354 19785 static void put_packet(NUTContext *nut, AVIOContext *bc, AVIOContext *dyn_bc,
355 uint64_t startcode)
356 {
357 19785 uint8_t *dyn_buf = NULL;
358 19785 int dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
359 19785 int forw_ptr = dyn_size + 4;
360
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19785 times.
19785 if (forw_ptr > 4096)
362 ✗ ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
363 19785 avio_wb64(bc, startcode);
364 19785 put_v(bc, forw_ptr);
365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19785 times.
19785 if (forw_ptr > 4096)
366 ✗ avio_wl32(bc, ffio_get_checksum(bc));
367
368 19785 ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
369 19785 avio_write(bc, dyn_buf, dyn_size);
370 19785 avio_wl32(bc, ffio_get_checksum(bc));
371
372 19785 ffio_reset_dyn_buf(dyn_bc);
373 19785 }
374
375 2801 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 2801 put_v(bc, nut->version);
382
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2799 times.
2801 if (nut->version > 3)
383 2 put_v(bc, nut->minor_version = 1);
384 2801 put_v(bc, nut->avf->nb_streams);
385 2801 put_v(bc, nut->max_distance);
386 2801 put_v(bc, nut->time_base_count);
387
388
2/2
✓ Branch 0 taken 2815 times.
✓ Branch 1 taken 2801 times.
5616 for (i = 0; i < nut->time_base_count; i++) {
389 2815 put_v(bc, nut->time_base[i].num);
390 2815 put_v(bc, nut->time_base[i].den);
391 }
392
393 2801 tmp_pts = 0;
394 2801 tmp_mul = 1;
395 2801 tmp_stream = 0;
396 2801 tmp_match = 1 - (1LL << 62);
397 2801 tmp_head_idx = 0;
398
2/2
✓ Branch 0 taken 19700 times.
✓ Branch 1 taken 2801 times.
22501 for (i = 0; i < 256; ) {
399 19700 tmp_fields = 0;
400 19700 tmp_size = 0;
401 // tmp_res=0;
402
2/2
✓ Branch 0 taken 11274 times.
✓ Branch 1 taken 8426 times.
19700 if (tmp_pts != nut->frame_code[i].pts_delta ) tmp_fields = 1;
403
2/2
✓ Branch 0 taken 11251 times.
✓ Branch 1 taken 8449 times.
19700 if (tmp_mul != nut->frame_code[i].size_mul ) tmp_fields = 2;
404
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 19691 times.
19700 if (tmp_stream != nut->frame_code[i].stream_id ) tmp_fields = 3;
405
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 19672 times.
19700 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 19698 times.
19700 if (tmp_head_idx != nut->frame_code[i].header_idx) tmp_fields = 8;
408
409 19700 tmp_pts = nut->frame_code[i].pts_delta;
410 19700 tmp_flags = nut->frame_code[i].flags;
411 19700 tmp_stream = nut->frame_code[i].stream_id;
412 19700 tmp_mul = nut->frame_code[i].size_mul;
413 19700 tmp_size = nut->frame_code[i].size_lsb;
414 // tmp_res = nut->frame_code[i].res;
415 19700 tmp_head_idx = nut->frame_code[i].header_idx;
416
417
2/2
✓ Branch 0 taken 733955 times.
✓ Branch 1 taken 2801 times.
736756 for (j = 0; i < 256; j++, i++) {
418
2/2
✓ Branch 0 taken 2801 times.
✓ Branch 1 taken 731154 times.
733955 if (i == 'N') {
419 2801 j--;
420 2801 continue;
421 }
422
2/2
✓ Branch 0 taken 719880 times.
✓ Branch 1 taken 11274 times.
731154 if (nut->frame_code[i].pts_delta != tmp_pts ||
423
2/2
✓ Branch 0 taken 714255 times.
✓ Branch 1 taken 5625 times.
719880 nut->frame_code[i].flags != tmp_flags ||
424
1/2
✓ Branch 0 taken 714255 times.
✗ Branch 1 not taken.
714255 nut->frame_code[i].stream_id != tmp_stream ||
425
1/2
✓ Branch 0 taken 714255 times.
✗ Branch 1 not taken.
714255 nut->frame_code[i].size_mul != tmp_mul ||
426
1/2
✓ Branch 0 taken 714255 times.
✗ Branch 1 not taken.
714255 nut->frame_code[i].size_lsb != tmp_size + j ||
427 // nut->frame_code[i].res != tmp_res ||
428
1/2
✓ Branch 0 taken 714255 times.
✗ Branch 1 not taken.
714255 nut->frame_code[i].header_idx != tmp_head_idx)
429 break;
430 }
431
2/2
✓ Branch 0 taken 5602 times.
✓ Branch 1 taken 14098 times.
19700 if (j != tmp_mul - tmp_size)
432 5602 tmp_fields = 6;
433
434 19700 put_v(bc, tmp_flags);
435 19700 put_v(bc, tmp_fields);
436
2/2
✓ Branch 0 taken 16894 times.
✓ Branch 1 taken 2806 times.
19700 if (tmp_fields > 0) put_s(bc, tmp_pts);
437
2/2
✓ Branch 0 taken 11265 times.
✓ Branch 1 taken 8435 times.
19700 if (tmp_fields > 1) put_v(bc, tmp_mul);
438
2/2
✓ Branch 0 taken 5635 times.
✓ Branch 1 taken 14065 times.
19700 if (tmp_fields > 2) put_v(bc, tmp_stream);
439
2/2
✓ Branch 0 taken 5631 times.
✓ Branch 1 taken 14069 times.
19700 if (tmp_fields > 3) put_v(bc, tmp_size);
440
2/2
✓ Branch 0 taken 5603 times.
✓ Branch 1 taken 14097 times.
19700 if (tmp_fields > 4) put_v(bc, 0 /*tmp_res*/);
441
2/2
✓ Branch 0 taken 5603 times.
✓ Branch 1 taken 14097 times.
19700 if (tmp_fields > 5) put_v(bc, j);
442
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 19699 times.
19700 if (tmp_fields > 6) put_v(bc, tmp_match);
443
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 19699 times.
19700 if (tmp_fields > 7) put_v(bc, tmp_head_idx);
444 }
445 2801 put_v(bc, nut->header_count - 1);
446
2/2
✓ Branch 0 taken 16806 times.
✓ Branch 1 taken 2801 times.
19607 for (i = 1; i < nut->header_count; i++) {
447 16806 put_v(bc, nut->header_len[i]);
448 16806 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 2799 times.
2801 if (nut->version > 3)
452 2 put_v(bc, nut->flags);
453 2801 }
454
455 2806 static int write_streamheader(AVFormatContext *avctx, AVIOContext *bc,
456 AVStream *st, int i)
457 {
458 2806 NUTContext *nut = avctx->priv_data;
459 2806 AVCodecParameters *par = st->codecpar;
460
461 2806 put_v(bc, i);
462
2/4
✓ Branch 0 taken 2781 times.
✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2806 switch (par->codec_type) {
463 2781 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 2806 put_v(bc, 4);
469
470
1/2
✓ Branch 0 taken 2806 times.
✗ Branch 1 not taken.
2806 if (par->codec_tag) {
471 2806 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 2806 put_v(bc, nut->stream[i].time_base - nut->time_base);
478 2806 put_v(bc, nut->stream[i].msb_pts_shift);
479 2806 put_v(bc, nut->stream[i].max_pts_distance);
480 2806 put_v(bc, par->video_delay);
481 2806 avio_w8(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
482
483 2806 put_v(bc, par->extradata_size);
484 2806 avio_write(bc, par->extradata, par->extradata_size);
485
486
2/3
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2781 times.
✗ Branch 2 not taken.
2806 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 2781 case AVMEDIA_TYPE_VIDEO:
493 2781 put_v(bc, par->width);
494 2781 put_v(bc, par->height);
495
496
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2765 times.
2781 if (st->sample_aspect_ratio.num <= 0 ||
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 st->sample_aspect_ratio.den <= 0) {
498 2765 put_v(bc, 0);
499 2765 put_v(bc, 0);
500 } else {
501 16 put_v(bc, st->sample_aspect_ratio.num);
502 16 put_v(bc, st->sample_aspect_ratio.den);
503 }
504 2781 put_v(bc, 0); /* csp type -- unknown */
505 2781 break;
506 ✗ default:
507 ✗ break;
508 }
509 2806 return 0;
510 }
511
512 5723 static int add_info(AVIOContext *bc, const char *type, const char *value)
513 {
514 5723 put_str(bc, type);
515 5723 put_s(bc, -1);
516 5723 put_str(bc, value);
517 5723 return 1;
518 }
519
520 2801 static int write_globalinfo(NUTContext *nut, AVIOContext *bc)
521 {
522 2801 AVFormatContext *s = nut->avf;
523 2801 const AVDictionaryEntry *t = NULL;
524 AVIOContext *dyn_bc;
525 2801 uint8_t *dyn_buf = NULL;
526 2801 int count = 0, dyn_size;
527 2801 int ret = avio_open_dyn_buf(&dyn_bc);
528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2801 times.
2801 if (ret < 0)
529 ✗ return ret;
530
531 2801 ff_standardize_creation_time(s);
532
2/2
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 2801 times.
2859 while ((t = av_dict_iterate(s->metadata, t)))
533 58 count += add_info(dyn_bc, t->key, t->value);
534
535 2801 put_v(bc, 0); //stream_if_plus1
536 2801 put_v(bc, 0); //chapter_id
537 2801 put_v(bc, 0); //timestamp_start
538 2801 put_v(bc, 0); //length
539
540 2801 put_v(bc, count);
541
542 2801 dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
543 2801 avio_write(bc, dyn_buf, dyn_size);
544 2801 av_free(dyn_buf);
545 2801 return 0;
546 }
547
548 2806 static int write_streaminfo(NUTContext *nut, AVIOContext *bc, int stream_id) {
549 2806 AVFormatContext *s= nut->avf;
550 2806 AVStream* st = s->streams[stream_id];
551 2806 const AVDictionaryEntry *t = NULL;
552 AVIOContext *dyn_bc;
553 2806 uint8_t *dyn_buf=NULL;
554 2806 int count=0, dyn_size, i;
555 2806 int ret = avio_open_dyn_buf(&dyn_bc);
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2806 times.
2806 if (ret < 0)
557 ✗ return ret;
558
559
2/2
✓ Branch 1 taken 2837 times.
✓ Branch 2 taken 2806 times.
5643 while ((t = av_dict_iterate(st->metadata, t)))
560 2837 count += add_info(dyn_bc, t->key, t->value);
561
2/2
✓ Branch 0 taken 16836 times.
✓ Branch 1 taken 2806 times.
19642 for (i=0; ff_nut_dispositions[i].flag; ++i) {
562
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16820 times.
16836 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 2781 times.
✓ Branch 1 taken 25 times.
2806 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
566 uint8_t buf[256];
567
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2779 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2781 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 2779 snprintf(buf, sizeof(buf), "%d/%d", st->avg_frame_rate.num, st->avg_frame_rate.den);
571 2781 count += add_info(dyn_bc, "r_frame_rate", buf);
572 }
573 2806 dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
574
575
1/2
✓ Branch 0 taken 2806 times.
✗ Branch 1 not taken.
2806 if (count) {
576 2806 put_v(bc, stream_id + 1); //stream_id_plus1
577 2806 put_v(bc, 0); //chapter_id
578 2806 put_v(bc, 0); //timestamp_start
579 2806 put_v(bc, 0); //length
580
581 2806 put_v(bc, count);
582
583 2806 avio_write(bc, dyn_buf, dyn_size);
584 }
585
586 2806 av_free(dyn_buf);
587 2806 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 2801 static int write_index(NUTContext *nut, AVIOContext *bc) {
619 int i;
620 2801 Syncpoint dummy= { .pos= 0 };
621 2801 Syncpoint *next_node[2] = { NULL };
622 2801 int64_t startpos = avio_tell(bc);
623 int64_t payload_size;
624
625 2801 put_tt(nut, nut->max_pts_tb, bc, nut->max_pts);
626
627 2801 put_v(bc, nut->sp_count);
628
629
2/2
✓ Branch 0 taken 5750 times.
✓ Branch 1 taken 2801 times.
8551 for (i=0; i<nut->sp_count; i++) {
630 5750 av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, (void**)next_node);
631 5750 put_v(bc, (next_node[1]->pos >> 4) - (dummy.pos>>4));
632 5750 dummy.pos = next_node[1]->pos;
633 }
634
635
2/2
✓ Branch 0 taken 2806 times.
✓ Branch 1 taken 2801 times.
5607 for (i=0; i<nut->avf->nb_streams; i++) {
636 2806 StreamContext *nus= &nut->stream[i];
637 2806 int64_t last_pts= -1;
638 int j, k;
639
2/2
✓ Branch 0 taken 3287 times.
✓ Branch 1 taken 2806 times.
6093 for (j=0; j<nut->sp_count; j++) {
640 int flag;
641 3287 int n = 0;
642
643
3/4
✓ Branch 0 taken 481 times.
✓ Branch 1 taken 2806 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 481 times.
3287 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 3287 flag = (nus->keyframe_pts[j] != AV_NOPTS_VALUE) ^ (j+1 == nut->sp_count);
649
4/4
✓ Branch 0 taken 5796 times.
✓ Branch 1 taken 457 times.
✓ Branch 2 taken 2966 times.
✓ Branch 3 taken 2830 times.
6253 for (; j<nut->sp_count && (nus->keyframe_pts[j] != AV_NOPTS_VALUE) == flag; j++)
650 2966 n++;
651
652 3287 put_v(bc, 1 + 2 * flag + 4 * n);
653
4/4
✓ Branch 0 taken 6253 times.
✓ Branch 1 taken 2830 times.
✓ Branch 2 taken 5796 times.
✓ Branch 3 taken 457 times.
9083 for (k= j - n; k<=j && k<nut->sp_count; k++) {
654
2/2
✓ Branch 0 taken 2884 times.
✓ Branch 1 taken 2912 times.
5796 if (nus->keyframe_pts[k] == AV_NOPTS_VALUE)
655 2884 continue;
656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2912 times.
2912 av_assert0(nus->keyframe_pts[k] > last_pts);
657 2912 put_v(bc, nus->keyframe_pts[k] - last_pts);
658 2912 last_pts = nus->keyframe_pts[k];
659 }
660 }
661 }
662
663 2801 payload_size = avio_tell(bc) - startpos + 8 + 4;
664
665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2801 times.
2801 avio_wb64(bc, 8 + payload_size + av_log2(payload_size) / 7 + 1 + 4*(payload_size > 4096));
666
667 2801 return 0;
668 }
669
670 2801 static int write_headers(AVFormatContext *avctx, AVIOContext *bc)
671 {
672 2801 NUTContext *nut = avctx->priv_data;
673 AVIOContext *dyn_bc;
674 int i, ret;
675
676 2801 ff_metadata_conv_ctx(avctx, ff_nut_metadata_conv, NULL);
677
678 2801 ret = avio_open_dyn_buf(&dyn_bc);
679
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2801 times.
2801 if (ret < 0)
680 ✗ return ret;
681 2801 write_mainheader(nut, dyn_bc);
682 2801 put_packet(nut, bc, dyn_bc, MAIN_STARTCODE);
683
684
2/2
✓ Branch 0 taken 2806 times.
✓ Branch 1 taken 2801 times.
5607 for (i = 0; i < nut->avf->nb_streams; i++) {
685 2806 ret = write_streamheader(avctx, dyn_bc, nut->avf->streams[i], i);
686
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2806 times.
2806 if (ret < 0) {
687 ✗ goto fail;
688 }
689 2806 put_packet(nut, bc, dyn_bc, STREAM_STARTCODE);
690 }
691
692 2801 write_globalinfo(nut, dyn_bc);
693 2801 put_packet(nut, bc, dyn_bc, INFO_STARTCODE);
694
695
2/2
✓ Branch 0 taken 2806 times.
✓ Branch 1 taken 2801 times.
5607 for (i = 0; i < nut->avf->nb_streams; i++) {
696 2806 ret = write_streaminfo(nut, dyn_bc, i);
697
1/2
✓ Branch 0 taken 2806 times.
✗ Branch 1 not taken.
2806 if (ret > 0)
698 2806 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 2801 times.
2821 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 2801 nut->last_syncpoint_pos = INT_MIN;
713 2801 nut->header_count++;
714
715 2801 ret = 0;
716 2801 fail:
717 2801 ffio_free_dyn_buf(&dyn_bc);
718
719 2801 return ret;
720 }
721
722 2801 static int nut_write_header(AVFormatContext *s)
723 {
724 2801 NUTContext *nut = s->priv_data;
725 2801 AVIOContext *bc = s->pb;
726 int i, j, ret;
727
728 2801 nut->avf = s;
729
730
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2799 times.
2801 nut->version = FFMAX(NUT_STABLE_VERSION, 3 + !!nut->flags);
731
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2799 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2801 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 2801 nut->stream = av_calloc(s->nb_streams, sizeof(*nut->stream ));
741 2801 nut->chapter = av_calloc(s->nb_chapters, sizeof(*nut->chapter));
742 5602 nut->time_base= av_calloc(s->nb_streams +
743 2801 s->nb_chapters, sizeof(*nut->time_base));
744
3/6
✓ Branch 0 taken 2801 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2801 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2801 times.
2801 if (!nut->stream || !nut->chapter || !nut->time_base)
745 ✗ return AVERROR(ENOMEM);
746
747
2/2
✓ Branch 0 taken 2806 times.
✓ Branch 1 taken 2801 times.
5607 for (i = 0; i < s->nb_streams; i++) {
748 2806 AVStream *st = s->streams[i];
749 int ssize;
750 AVRational time_base;
751 2806 ff_parse_specific_params(st, &time_base.den, &ssize, &time_base.num);
752
753
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2781 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
2806 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 2781 time_base = choose_timebase(s, st, 48000);
757 }
758
759 2806 avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
760
761
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2805 times.
2811 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 2806 nut->time_base[j] = time_base;
766 2806 nut->stream[i].time_base = &nut->time_base[j];
767
2/2
✓ Branch 0 taken 2805 times.
✓ Branch 1 taken 1 times.
2806 if (j == nut->time_base_count)
768 2805 nut->time_base_count++;
769
770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2806 times.
2806 if (INT64_C(1000) * time_base.num >= time_base.den)
771 ✗ nut->stream[i].msb_pts_shift = 7;
772 else
773 2806 nut->stream[i].msb_pts_shift = 14;
774 2806 nut->stream[i].max_pts_distance =
775 2806 FFMAX(time_base.den, time_base.num) / time_base.num;
776 }
777
778
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2801 times.
2821 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 2801 nut->max_distance = MAX_DISTANCE;
792 2801 build_elision_headers(s);
793 2801 build_frame_code(s);
794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2801 times.
2801 av_assert0(nut->frame_code['N'].flags == FLAG_INVALID);
795
796 2801 avio_write(bc, ID_STRING, strlen(ID_STRING));
797 2801 avio_w8(bc, 0);
798
799
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2801 times.
2801 if ((ret = write_headers(s, bc)) < 0)
800 ✗ return ret;
801
802
1/2
✓ Branch 0 taken 2801 times.
✗ Branch 1 not taken.
2801 if (s->avoid_negative_ts < 0)
803 2801 s->avoid_negative_ts = 1;
804
805 2801 return 0;
806 }
807
808 1636014 static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc,
809 AVPacket *pkt)
810 {
811 1636014 int flags = 0;
812
813
2/2
✓ Branch 0 taken 1581404 times.
✓ Branch 1 taken 54610 times.
1636014 if (pkt->flags & AV_PKT_FLAG_KEY)
814 1581404 flags |= FLAG_KEY;
815
2/2
✓ Branch 0 taken 23364 times.
✓ Branch 1 taken 1612650 times.
1636014 if (pkt->stream_index != fc->stream_id)
816 23364 flags |= FLAG_STREAM_ID;
817
2/2
✓ Branch 0 taken 1594502 times.
✓ Branch 1 taken 41512 times.
1636014 if (pkt->size / fc->size_mul)
818 1594502 flags |= FLAG_SIZE_MSB;
819
2/2
✓ Branch 0 taken 1553966 times.
✓ Branch 1 taken 82048 times.
1636014 if (pkt->pts - nus->last_pts != fc->pts_delta)
820 1553966 flags |= FLAG_CODED_PTS;
821
4/4
✓ Branch 0 taken 48768 times.
✓ Branch 1 taken 1587246 times.
✓ Branch 2 taken 254 times.
✓ Branch 3 taken 48514 times.
1636014 if (pkt->side_data_elems && nut->version > 3)
822 254 flags |= FLAG_SM_DATA;
823
2/2
✓ Branch 0 taken 1207008 times.
✓ Branch 1 taken 429006 times.
1636014 if (pkt->size > 2 * nut->max_distance)
824 1207008 flags |= FLAG_CHECKSUM;
825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1636014 times.
1636014 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 1627911 times.
1636014 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 1636014 return flags | (fc->flags & FLAG_CODED);
835 }
836
837 6441 static int find_best_header_idx(NUTContext *nut, AVPacket *pkt)
838 {
839 int i;
840 6441 int best_i = 0;
841 6441 int best_len = 0;
842
843
2/2
✓ Branch 0 taken 5816 times.
✓ Branch 1 taken 625 times.
6441 if (pkt->size > 4096)
844 5816 return 0;
845
846
2/2
✓ Branch 0 taken 4375 times.
✓ Branch 1 taken 625 times.
5000 for (i = 1; i < nut->header_count; i++)
847
2/2
✓ Branch 0 taken 4363 times.
✓ Branch 1 taken 12 times.
4375 if (pkt->size >= nut->header_len[i]
848
2/2
✓ Branch 0 taken 3238 times.
✓ Branch 1 taken 1125 times.
4363 && nut->header_len[i] > best_len
849
2/2
✓ Branch 0 taken 209 times.
✓ Branch 1 taken 3029 times.
3238 && !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 625 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 6441 static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
978 {
979 6441 NUTContext *nut = s->priv_data;
980 6441 StreamContext *nus = &nut->stream[pkt->stream_index];
981 6441 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 6441 int key_frame = !!(pkt->flags & AV_PKT_FLAG_KEY);
987 6441 int store_sp = 0;
988 6441 int ret = 0;
989 6441 int sm_size = 0;
990 6441 int data_size = pkt->size;
991 6441 uint8_t *sm_buf = NULL;
992
993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6441 times.
6441 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 6249 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 191 times.
6441 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 6441 times.
6441 if (1LL << (20 + 3 * nut->header_count) <= avio_tell(bc))
1016 ✗ write_headers(s, bc);
1017
1018
4/4
✓ Branch 0 taken 6226 times.
✓ Branch 1 taken 215 times.
✓ Branch 2 taken 2818 times.
✓ Branch 3 taken 3408 times.
6441 if (key_frame && !(nus->last_flags & FLAG_KEY))
1019 2818 store_sp = 1;
1020
1021
2/2
✓ Branch 1 taken 5759 times.
✓ Branch 2 taken 682 times.
6441 if (data_size + 30 /*FIXME check*/ + avio_tell(bc) >= nut->last_syncpoint_pos + nut->max_distance)
1022 5759 store_sp = 1;
1023
1024 //FIXME: Ensure store_sp is 1 in the first place.
1025
1026
2/2
✓ Branch 0 taken 5763 times.
✓ Branch 1 taken 678 times.
6441 if (store_sp &&
1027
4/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5748 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 13 times.
5763 (!(nut->flags & NUT_PIPE) || nut->last_syncpoint_pos == INT_MIN)) {
1028 5750 int64_t sp_pos = INT64_MAX;
1029
1030 5750 ff_nut_reset_ts(nut, *nus->time_base, pkt->dts);
1031
2/2
✓ Branch 0 taken 5796 times.
✓ Branch 1 taken 5750 times.
11546 for (i = 0; i < s->nb_streams; i++) {
1032 5796 AVStream *st = s->streams[i];
1033 5796 FFStream *const sti = ffstream(st);
1034 5796 int64_t dts_tb = av_rescale_rnd(pkt->dts,
1035 5796 nus->time_base->num * (int64_t)nut->stream[i].time_base->den,
1036 5796 nus->time_base->den * (int64_t)nut->stream[i].time_base->num,
1037 AV_ROUND_DOWN);
1038 5796 int index = av_index_search_timestamp(st, dts_tb,
1039 AVSEEK_FLAG_BACKWARD);
1040
2/2
✓ Branch 0 taken 2984 times.
✓ Branch 1 taken 2812 times.
5796 if (index >= 0) {
1041 2984 sp_pos = FFMIN(sp_pos, sti->index_entries[index].pos);
1042
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2984 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2984 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 5750 nut->last_syncpoint_pos = avio_tell(bc);
1052 5750 ret = avio_open_dyn_buf(&dyn_bc);
1053
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5750 times.
5750 if (ret < 0)
1054 ✗ goto fail;
1055 5750 put_tt(nut, nus->time_base, dyn_bc, pkt->dts);
1056
2/2
✓ Branch 0 taken 2949 times.
✓ Branch 1 taken 2801 times.
5750 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 5750 times.
5750 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 5750 put_packet(nut, bc, dyn_bc, SYNCPOINT_STARTCODE);
1063 5750 ffio_free_dyn_buf(&dyn_bc);
1064
1065
1/2
✓ Branch 0 taken 5750 times.
✗ Branch 1 not taken.
5750 if (nut->write_index) {
1066
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5750 times.
5750 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 3817 times.
✓ Branch 1 taken 1933 times.
5750 if ((1ll<<60) % nut->sp_count == 0)
1070
2/2
✓ Branch 0 taken 3835 times.
✓ Branch 1 taken 3817 times.
7652 for (unsigned i = 0; i < s->nb_streams; i++) {
1071 3835 StreamContext *nus = &nut->stream[i];
1072 3835 av_reallocp_array(&nus->keyframe_pts, 2*nut->sp_count, sizeof(*nus->keyframe_pts));
1073
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3835 times.
3835 if (!nus->keyframe_pts) {
1074 ✗ ret = AVERROR(ENOMEM);
1075 ✗ goto fail;
1076 }
1077
2/2
✓ Branch 0 taken 1029 times.
✓ Branch 1 taken 2806 times.
3835 for (int j = nut->sp_count == 1 ? 0 : nut->sp_count;
1078
2/2
✓ Branch 0 taken 10286 times.
✓ Branch 1 taken 3835 times.
14121 j < 2 * nut->sp_count; j++)
1079 10286 nus->keyframe_pts[j] = AV_NOPTS_VALUE;
1080 }
1081 }
1082 }
1083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6441 times.
6441 av_assert0(nus->last_pts != AV_NOPTS_VALUE);
1084
1085 6441 coded_pts = pkt->pts & ((1 << nus->msb_pts_shift) - 1);
1086
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6441 times.
6441 if (ff_lsb2full(nus, coded_pts) != pkt->pts)
1087 ✗ coded_pts = pkt->pts + (1 << nus->msb_pts_shift);
1088
1089 6441 best_header_idx = find_best_header_idx(nut, pkt);
1090
1091 6441 best_length = INT_MAX;
1092 6441 frame_code = -1;
1093
2/2
✓ Branch 0 taken 1648896 times.
✓ Branch 1 taken 6441 times.
1655337 for (i = 0; i < 256; i++) {
1094 1648896 int length = 0;
1095 1648896 FrameCode *fc = &nut->frame_code[i];
1096 1648896 int flags = fc->flags;
1097
1098
2/2
✓ Branch 0 taken 19323 times.
✓ Branch 1 taken 1629573 times.
1648896 if (flags & FLAG_INVALID)
1099 19323 continue;
1100 1629573 needed_flags = get_needed_flags(nut, nus, fc, pkt);
1101
1102
2/2
✓ Branch 0 taken 6441 times.
✓ Branch 1 taken 1623132 times.
1629573 if (flags & FLAG_CODED) {
1103 6441 length++;
1104 6441 flags = needed_flags;
1105 }
1106
1107
2/2
✓ Branch 0 taken 1605549 times.
✓ Branch 1 taken 24024 times.
1629573 if ((flags & needed_flags) != needed_flags)
1108 1605549 continue;
1109
1110
2/2
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 23705 times.
24024 if ((flags ^ needed_flags) & FLAG_KEY)
1111 319 continue;
1112
1113
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 23613 times.
23705 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 8560 times.
23705 if (data_size % fc->size_mul != fc->size_lsb)
1117 15145 continue;
1118
2/2
✓ Branch 0 taken 8529 times.
✓ Branch 1 taken 31 times.
8560 if (flags & FLAG_SIZE_MSB)
1119 8529 length += get_v_length(data_size / fc->size_mul);
1120
1121
2/2
✓ Branch 0 taken 4752 times.
✓ Branch 1 taken 3808 times.
8560 if (flags & FLAG_CHECKSUM)
1122 4752 length += 4;
1123
1124
2/2
✓ Branch 0 taken 8129 times.
✓ Branch 1 taken 431 times.
8560 if (flags & FLAG_CODED_PTS)
1125 8129 length += get_v_length(coded_pts);
1126
1127
2/2
✓ Branch 0 taken 6441 times.
✓ Branch 1 taken 2119 times.
8560 if ( (flags & FLAG_CODED)
1128
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 6277 times.
6441 && 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 8396 times.
8560 if (flags & FLAG_HEADER_IDX) {
1133 164 length += 1 - nut->header_len[best_header_idx];
1134 } else {
1135 8396 length -= nut->header_len[fc->header_idx];
1136 }
1137
1138 8560 length *= 4;
1139 8560 length += !(flags & FLAG_CODED_PTS);
1140 8560 length += !(flags & FLAG_CHECKSUM);
1141
1142
2/2
✓ Branch 0 taken 8402 times.
✓ Branch 1 taken 158 times.
8560 if (length < best_length) {
1143 8402 best_length = length;
1144 8402 frame_code = i;
1145 }
1146 }
1147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6441 times.
6441 av_assert0(frame_code != -1);
1148
1149 6441 fc = &nut->frame_code[frame_code];
1150 6441 flags = fc->flags;
1151 6441 needed_flags = get_needed_flags(nut, nus, fc, pkt);
1152 6441 header_idx = fc->header_idx;
1153
1154 6441 ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
1155 6441 avio_w8(bc, frame_code);
1156
2/2
✓ Branch 0 taken 4761 times.
✓ Branch 1 taken 1680 times.
6441 if (flags & FLAG_CODED) {
1157 4761 put_v(bc, (flags ^ needed_flags) & ~(FLAG_CODED));
1158 4761 flags = needed_flags;
1159 }
1160
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6437 times.
6441 if (flags & FLAG_STREAM_ID) put_v(bc, pkt->stream_index);
1161
2/2
✓ Branch 0 taken 6041 times.
✓ Branch 1 taken 400 times.
6441 if (flags & FLAG_CODED_PTS) put_v(bc, coded_pts);
1162
2/2
✓ Branch 0 taken 6410 times.
✓ Branch 1 taken 31 times.
6441 if (flags & FLAG_SIZE_MSB ) put_v(bc, data_size / fc->size_mul);
1163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6441 times.
6441 if (flags & FLAG_HEADER_IDX) put_v(bc, header_idx = best_header_idx);
1164
1165
2/2
✓ Branch 0 taken 4752 times.
✓ Branch 1 taken 1689 times.
6441 if (flags & FLAG_CHECKSUM) avio_wl32(bc, ffio_get_checksum(bc));
1166 1689 else ffio_get_checksum(bc);
1167
1168
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6440 times.
6441 if (flags & FLAG_SM_DATA) {
1169 1 avio_write(bc, sm_buf, sm_size);
1170 }
1171 6441 avio_write(bc, pkt->data + nut->header_len[header_idx], pkt->size - nut->header_len[header_idx]);
1172
1173 6441 nus->last_flags = flags;
1174 6441 nus->last_pts = pkt->pts;
1175
1176 //FIXME just store one per syncpoint
1177
4/4
✓ Branch 0 taken 6226 times.
✓ Branch 1 taken 215 times.
✓ Branch 2 taken 6222 times.
✓ Branch 3 taken 4 times.
6441 if (flags & FLAG_KEY && !(nut->flags & NUT_PIPE)) {
1178 6222 av_add_index_entry(
1179 6222 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 6222 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5711 times.
✓ Branch 3 taken 511 times.
6222 if (nus->keyframe_pts && nus->keyframe_pts[nut->sp_count] == AV_NOPTS_VALUE)
1186 5711 nus->keyframe_pts[nut->sp_count] = pkt->pts;
1187 }
1188
1189
4/4
✓ Branch 0 taken 3640 times.
✓ Branch 1 taken 2801 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 3629 times.
6441 if (!nut->max_pts_tb || av_compare_ts(nut->max_pts, *nut->max_pts_tb, pkt->pts, *nus->time_base) < 0) {
1190 6430 nut->max_pts = pkt->pts;
1191 6430 nut->max_pts_tb = nus->time_base;
1192 }
1193
1194 11 fail:
1195 6441 av_freep(&sm_buf);
1196
1197 6441 return ret;
1198 }
1199
1200 2801 static int nut_write_trailer(AVFormatContext *s)
1201 {
1202 2801 NUTContext *nut = s->priv_data;
1203 2801 AVIOContext *bc = s->pb, *dyn_bc;
1204 int ret;
1205
1206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2801 times.
2801 while (nut->header_count < 3)
1207 ✗ write_headers(s, bc);
1208
1209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2801 times.
2801 if (!nut->sp_count)
1210 ✗ return 0;
1211
1212 2801 ret = avio_open_dyn_buf(&dyn_bc);
1213
1/2
✓ Branch 0 taken 2801 times.
✗ Branch 1 not taken.
2801 if (ret >= 0) {
1214 av_assert1(nut->write_index); // sp_count should be 0 if no index is going to be written
1215 2801 write_index(nut, dyn_bc);
1216 2801 put_packet(nut, bc, dyn_bc, INDEX_STARTCODE);
1217 2801 ffio_free_dyn_buf(&dyn_bc);
1218 }
1219
1220 2801 return 0;
1221 }
1222
1223 2801 static void nut_write_deinit(AVFormatContext *s)
1224 {
1225 2801 NUTContext *nut = s->priv_data;
1226 int i;
1227
1228 2801 ff_nut_free_sp(nut);
1229
1/2
✓ Branch 0 taken 2801 times.
✗ Branch 1 not taken.
2801 if (nut->stream)
1230
2/2
✓ Branch 0 taken 2806 times.
✓ Branch 1 taken 2801 times.
5607 for (i=0; i<s->nb_streams; i++)
1231 2806 av_freep(&nut->stream[i].keyframe_pts);
1232
1233 2801 av_freep(&nut->stream);
1234 2801 av_freep(&nut->chapter);
1235 2801 av_freep(&nut->time_base);
1236 2801 }
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