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