FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
nutenc.c
Go to the documentation of this file.
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"
33 #include "nut.h"
34 #include "internal.h"
35 #include "avio_internal.h"
36 #include "riff.h"
37 
38 static int find_expected_header(AVCodecParameters *p, int size, int key_frame,
39  uint8_t out[64])
40 {
41  int sample_rate = p->sample_rate;
42 
43  if (size > 4096)
44  return 0;
45 
46  AV_WB24(out, 1);
47 
48  if (p->codec_id == AV_CODEC_ID_MPEG4) {
49  if (key_frame) {
50  return 3;
51  } else {
52  out[3] = 0xB6;
53  return 4;
54  }
55  } else if (p->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
57  return 3;
58  } else if (p->codec_id == AV_CODEC_ID_H264) {
59  return 3;
60  } else if (p->codec_id == AV_CODEC_ID_MP3 ||
61  p->codec_id == AV_CODEC_ID_MP2) {
62  int lsf, mpeg25, sample_rate_index, bitrate_index, frame_size;
63  int layer = p->codec_id == AV_CODEC_ID_MP3 ? 3 : 2;
64  unsigned int header = 0xFFF00000;
65 
66  lsf = sample_rate < (24000 + 32000) / 2;
67  mpeg25 = sample_rate < (12000 + 16000) / 2;
68  sample_rate <<= lsf + mpeg25;
69  if (sample_rate < (32000 + 44100) / 2) sample_rate_index = 2;
70  else if (sample_rate < (44100 + 48000) / 2) sample_rate_index = 0;
71  else sample_rate_index = 1;
72 
73  sample_rate = avpriv_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25);
74 
75  for (bitrate_index = 2; bitrate_index < 30; bitrate_index++) {
76  frame_size =
77  avpriv_mpa_bitrate_tab[lsf][layer - 1][bitrate_index >> 1];
78  frame_size = (frame_size * 144000) / (sample_rate << lsf) +
79  (bitrate_index & 1);
80 
81  if (frame_size == size)
82  break;
83  }
84 
85  header |= (!lsf) << 19;
86  header |= (4 - layer) << 17;
87  header |= 1 << 16; //no crc
88  AV_WB32(out, header);
89  if (size <= 0)
90  return 2; //we guess there is no crc, if there is one the user clearly does not care about overhead
91  if (bitrate_index == 30)
92  return -1; //something is wrong ...
93 
94  header |= (bitrate_index >> 1) << 12;
95  header |= sample_rate_index << 10;
96  header |= (bitrate_index & 1) << 9;
97 
98  return 2; //FIXME actually put the needed ones in build_elision_headers()
99  //return 3; //we guess that the private bit is not set
100 //FIXME the above assumptions should be checked, if these turn out false too often something should be done
101  }
102  return 0;
103 }
104 
106  int frame_type)
107 {
108  NUTContext *nut = s->priv_data;
109  uint8_t out[64];
110  int i;
111  int len = find_expected_header(p, size, frame_type, out);
112 
113  for (i = 1; i < nut->header_count; i++) {
114  if (len == nut->header_len[i] && !memcmp(out, nut->header[i], len)) {
115  return i;
116  }
117  }
118 
119  return 0;
120 }
121 
123 {
124  NUTContext *nut = s->priv_data;
125  int i;
126  //FIXME this is lame
127  //FIXME write a 2pass mode to find the maximal headers
128  static const uint8_t headers[][5] = {
129  { 3, 0x00, 0x00, 0x01 },
130  { 4, 0x00, 0x00, 0x01, 0xB6},
131  { 2, 0xFF, 0xFA }, //mp3+crc
132  { 2, 0xFF, 0xFB }, //mp3
133  { 2, 0xFF, 0xFC }, //mp2+crc
134  { 2, 0xFF, 0xFD }, //mp2
135  };
136 
137  nut->header_count = 7;
138  for (i = 1; i < nut->header_count; i++) {
139  nut->header_len[i] = headers[i - 1][0];
140  nut->header[i] = &headers[i - 1][1];
141  }
142 }
143 
145 {
146  NUTContext *nut = s->priv_data;
147  int key_frame, index, pred, stream_id;
148  int start = 1;
149  int end = 254;
150  int keyframe_0_esc = s->nb_streams > 2;
151  int pred_table[10];
152  FrameCode *ft;
153 
154  ft = &nut->frame_code[start];
155  ft->flags = FLAG_CODED;
156  ft->size_mul = 1;
157  ft->pts_delta = 1;
158  start++;
159 
160  if (keyframe_0_esc) {
161  /* keyframe = 0 escape */
162  FrameCode *ft = &nut->frame_code[start];
164  ft->size_mul = 1;
165  start++;
166  }
167 
168  for (stream_id = 0; stream_id < s->nb_streams; stream_id++) {
169  int start2 = start + (end - start) * stream_id / s->nb_streams;
170  int end2 = start + (end - start) * (stream_id + 1) / s->nb_streams;
171  AVCodecParameters *par = s->streams[stream_id]->codecpar;
172  int is_audio = par->codec_type == AVMEDIA_TYPE_AUDIO;
173  int intra_only = /*codec->intra_only || */ is_audio;
174  int pred_count;
175  int frame_size = 0;
176 
177  if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
179  if (par->codec_id == AV_CODEC_ID_VORBIS && !frame_size)
180  frame_size = 64;
181  } else {
182  AVRational f = av_div_q(av_inv_q(s->streams[stream_id]->avg_frame_rate), *nut->stream[stream_id].time_base);
183  if (f.den == 1 && f.num>0)
184  frame_size = f.num;
185  }
186  if (!frame_size)
187  frame_size = 1;
188 
189  for (key_frame = 0; key_frame < 2; key_frame++) {
190  if (!intra_only || !keyframe_0_esc || key_frame != 0) {
191  FrameCode *ft = &nut->frame_code[start2];
192  ft->flags = FLAG_KEY * key_frame;
194  ft->stream_id = stream_id;
195  ft->size_mul = 1;
196  if (is_audio)
197  ft->header_idx = find_header_idx(s, par, -1, key_frame);
198  start2++;
199  }
200  }
201 
202  key_frame = intra_only;
203 #if 1
204  if (is_audio) {
205  int frame_bytes;
206  int pts;
207 
208  if (par->block_align > 0) {
209  frame_bytes = par->block_align;
210  } else {
212  frame_bytes = frame_size * (int64_t)par->bit_rate / (8 * par->sample_rate);
213  }
214 
215  for (pts = 0; pts < 2; pts++) {
216  for (pred = 0; pred < 2; pred++) {
217  FrameCode *ft = &nut->frame_code[start2];
218  ft->flags = FLAG_KEY * key_frame;
219  ft->stream_id = stream_id;
220  ft->size_mul = frame_bytes + 2;
221  ft->size_lsb = frame_bytes + pred;
222  ft->pts_delta = pts * frame_size;
223  ft->header_idx = find_header_idx(s, par, frame_bytes + pred, key_frame);
224  start2++;
225  }
226  }
227  } else {
228  FrameCode *ft = &nut->frame_code[start2];
229  ft->flags = FLAG_KEY | FLAG_SIZE_MSB;
230  ft->stream_id = stream_id;
231  ft->size_mul = 1;
232  ft->pts_delta = frame_size;
233  start2++;
234  }
235 #endif
236 
237  if (par->video_delay) {
238  pred_count = 5;
239  pred_table[0] = -2;
240  pred_table[1] = -1;
241  pred_table[2] = 1;
242  pred_table[3] = 3;
243  pred_table[4] = 4;
244  } else if (par->codec_id == AV_CODEC_ID_VORBIS) {
245  pred_count = 3;
246  pred_table[0] = 2;
247  pred_table[1] = 9;
248  pred_table[2] = 16;
249  } else {
250  pred_count = 1;
251  pred_table[0] = 1;
252  }
253 
254  for (pred = 0; pred < pred_count; pred++) {
255  int start3 = start2 + (end2 - start2) * pred / pred_count;
256  int end3 = start2 + (end2 - start2) * (pred + 1) / pred_count;
257 
258  pred_table[pred] *= frame_size;
259 
260  for (index = start3; index < end3; index++) {
261  FrameCode *ft = &nut->frame_code[index];
262  ft->flags = FLAG_KEY * key_frame;
263  ft->flags |= FLAG_SIZE_MSB;
264  ft->stream_id = stream_id;
265 //FIXME use single byte size and pred from last
266  ft->size_mul = end3 - start3;
267  ft->size_lsb = index - start3;
268  ft->pts_delta = pred_table[pred];
269  if (is_audio)
270  ft->header_idx = find_header_idx(s, par, -1, key_frame);
271  }
272  }
273  }
274  memmove(&nut->frame_code['N' + 1], &nut->frame_code['N'], sizeof(FrameCode) * (255 - 'N'));
275  nut->frame_code[0].flags =
276  nut->frame_code[255].flags =
277  nut->frame_code['N'].flags = FLAG_INVALID;
278 }
279 
280 static void put_tt(NUTContext *nut, AVRational *time_base, AVIOContext *bc, uint64_t val)
281 {
282  val *= nut->time_base_count;
283  val += time_base - nut->time_base;
284  ff_put_v(bc, val);
285 }
286 /**
287  * Store a string as vb.
288  */
289 static void put_str(AVIOContext *bc, const char *string)
290 {
291  size_t len = strlen(string);
292 
293  ff_put_v(bc, len);
294  avio_write(bc, string, len);
295 }
296 
297 static void put_s(AVIOContext *bc, int64_t val)
298 {
299  ff_put_v(bc, 2 * FFABS(val) - (val > 0));
300 }
301 
302 #ifdef TRACE
303 static inline void ff_put_v_trace(AVIOContext *bc, uint64_t v, const char *file,
304  const char *func, int line)
305 {
306  av_log(NULL, AV_LOG_DEBUG, "ff_put_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
307 
308  ff_put_v(bc, v);
309 }
310 
311 static inline void put_s_trace(AVIOContext *bc, int64_t v, const char *file, const char *func, int line)
312 {
313  av_log(NULL, AV_LOG_DEBUG, "put_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
314 
315  put_s(bc, v);
316 }
317 #define ff_put_v(bc, v) ff_put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
318 #define put_s(bc, v) put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
319 #endif
320 
321 //FIXME remove calculate_checksum
322 static void put_packet(NUTContext *nut, AVIOContext *bc, AVIOContext *dyn_bc,
323  int calculate_checksum, uint64_t startcode)
324 {
325  uint8_t *dyn_buf = NULL;
326  int dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
327  int forw_ptr = dyn_size + 4 * calculate_checksum;
328 
329  if (forw_ptr > 4096)
331  avio_wb64(bc, startcode);
332  ff_put_v(bc, forw_ptr);
333  if (forw_ptr > 4096)
334  avio_wl32(bc, ffio_get_checksum(bc));
335 
336  if (calculate_checksum)
338  avio_write(bc, dyn_buf, dyn_size);
339  if (calculate_checksum)
340  avio_wl32(bc, ffio_get_checksum(bc));
341 
342  av_free(dyn_buf);
343 }
344 
346 {
347  int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields,
348  tmp_head_idx;
349  int64_t tmp_match;
350 
351  ff_put_v(bc, nut->version);
352  if (nut->version > 3)
353  ff_put_v(bc, nut->minor_version = 1);
354  ff_put_v(bc, nut->avf->nb_streams);
355  ff_put_v(bc, nut->max_distance);
356  ff_put_v(bc, nut->time_base_count);
357 
358  for (i = 0; i < nut->time_base_count; i++) {
359  ff_put_v(bc, nut->time_base[i].num);
360  ff_put_v(bc, nut->time_base[i].den);
361  }
362 
363  tmp_pts = 0;
364  tmp_mul = 1;
365  tmp_stream = 0;
366  tmp_match = 1 - (1LL << 62);
367  tmp_head_idx = 0;
368  for (i = 0; i < 256; ) {
369  tmp_fields = 0;
370  tmp_size = 0;
371 // tmp_res=0;
372  if (tmp_pts != nut->frame_code[i].pts_delta ) tmp_fields = 1;
373  if (tmp_mul != nut->frame_code[i].size_mul ) tmp_fields = 2;
374  if (tmp_stream != nut->frame_code[i].stream_id ) tmp_fields = 3;
375  if (tmp_size != nut->frame_code[i].size_lsb ) tmp_fields = 4;
376 // if (tmp_res != nut->frame_code[i].res ) tmp_fields=5;
377  if (tmp_head_idx != nut->frame_code[i].header_idx) tmp_fields = 8;
378 
379  tmp_pts = nut->frame_code[i].pts_delta;
380  tmp_flags = nut->frame_code[i].flags;
381  tmp_stream = nut->frame_code[i].stream_id;
382  tmp_mul = nut->frame_code[i].size_mul;
383  tmp_size = nut->frame_code[i].size_lsb;
384 // tmp_res = nut->frame_code[i].res;
385  tmp_head_idx = nut->frame_code[i].header_idx;
386 
387  for (j = 0; i < 256; j++, i++) {
388  if (i == 'N') {
389  j--;
390  continue;
391  }
392  if (nut->frame_code[i].pts_delta != tmp_pts ||
393  nut->frame_code[i].flags != tmp_flags ||
394  nut->frame_code[i].stream_id != tmp_stream ||
395  nut->frame_code[i].size_mul != tmp_mul ||
396  nut->frame_code[i].size_lsb != tmp_size + j ||
397 // nut->frame_code[i].res != tmp_res ||
398  nut->frame_code[i].header_idx != tmp_head_idx)
399  break;
400  }
401  if (j != tmp_mul - tmp_size)
402  tmp_fields = 6;
403 
404  ff_put_v(bc, tmp_flags);
405  ff_put_v(bc, tmp_fields);
406  if (tmp_fields > 0) put_s(bc, tmp_pts);
407  if (tmp_fields > 1) ff_put_v(bc, tmp_mul);
408  if (tmp_fields > 2) ff_put_v(bc, tmp_stream);
409  if (tmp_fields > 3) ff_put_v(bc, tmp_size);
410  if (tmp_fields > 4) ff_put_v(bc, 0 /*tmp_res*/);
411  if (tmp_fields > 5) ff_put_v(bc, j);
412  if (tmp_fields > 6) ff_put_v(bc, tmp_match);
413  if (tmp_fields > 7) ff_put_v(bc, tmp_head_idx);
414  }
415  ff_put_v(bc, nut->header_count - 1);
416  for (i = 1; i < nut->header_count; i++) {
417  ff_put_v(bc, nut->header_len[i]);
418  avio_write(bc, nut->header[i], nut->header_len[i]);
419  }
420  // flags had been effectively introduced in version 4
421  if (nut->version > 3)
422  ff_put_v(bc, nut->flags);
423 }
424 
426  AVStream *st, int i)
427 {
428  NUTContext *nut = avctx->priv_data;
429  AVCodecParameters *par = st->codecpar;
430 
431  ff_put_v(bc, i);
432  switch (par->codec_type) {
433  case AVMEDIA_TYPE_VIDEO: ff_put_v(bc, 0); break;
434  case AVMEDIA_TYPE_AUDIO: ff_put_v(bc, 1); break;
435  case AVMEDIA_TYPE_SUBTITLE: ff_put_v(bc, 2); break;
436  default: ff_put_v(bc, 3); break;
437  }
438  ff_put_v(bc, 4);
439 
440  if (par->codec_tag) {
441  avio_wl32(bc, par->codec_tag);
442  } else {
443  av_log(avctx, AV_LOG_ERROR, "No codec tag defined for stream %d\n", i);
444  return AVERROR(EINVAL);
445  }
446 
447  ff_put_v(bc, nut->stream[i].time_base - nut->time_base);
448  ff_put_v(bc, nut->stream[i].msb_pts_shift);
449  ff_put_v(bc, nut->stream[i].max_pts_distance);
450  ff_put_v(bc, par->video_delay);
451  avio_w8(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
452 
453  ff_put_v(bc, par->extradata_size);
454  avio_write(bc, par->extradata, par->extradata_size);
455 
456  switch (par->codec_type) {
457  case AVMEDIA_TYPE_AUDIO:
458  ff_put_v(bc, par->sample_rate);
459  ff_put_v(bc, 1);
460  ff_put_v(bc, par->channels);
461  break;
462  case AVMEDIA_TYPE_VIDEO:
463  ff_put_v(bc, par->width);
464  ff_put_v(bc, par->height);
465 
466  if (st->sample_aspect_ratio.num <= 0 ||
467  st->sample_aspect_ratio.den <= 0) {
468  ff_put_v(bc, 0);
469  ff_put_v(bc, 0);
470  } else {
473  }
474  ff_put_v(bc, 0); /* csp type -- unknown */
475  break;
476  default:
477  break;
478  }
479  return 0;
480 }
481 
482 static int add_info(AVIOContext *bc, const char *type, const char *value)
483 {
484  put_str(bc, type);
485  put_s(bc, -1);
486  put_str(bc, value);
487  return 1;
488 }
489 
491 {
492  AVFormatContext *s = nut->avf;
493  AVDictionaryEntry *t = NULL;
494  AVIOContext *dyn_bc;
495  uint8_t *dyn_buf = NULL;
496  int count = 0, dyn_size;
497  int ret = avio_open_dyn_buf(&dyn_bc);
498  if (ret < 0)
499  return ret;
500 
502  while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
503  count += add_info(dyn_bc, t->key, t->value);
504 
505  ff_put_v(bc, 0); //stream_if_plus1
506  ff_put_v(bc, 0); //chapter_id
507  ff_put_v(bc, 0); //timestamp_start
508  ff_put_v(bc, 0); //length
509 
510  ff_put_v(bc, count);
511 
512  dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
513  avio_write(bc, dyn_buf, dyn_size);
514  av_free(dyn_buf);
515  return 0;
516 }
517 
518 static int write_streaminfo(NUTContext *nut, AVIOContext *bc, int stream_id) {
519  AVFormatContext *s= nut->avf;
520  AVStream* st = s->streams[stream_id];
521  AVDictionaryEntry *t = NULL;
522  AVIOContext *dyn_bc;
523  uint8_t *dyn_buf=NULL;
524  int count=0, dyn_size, i;
525  int ret = avio_open_dyn_buf(&dyn_bc);
526  if (ret < 0)
527  return ret;
528 
529  while ((t = av_dict_get(st->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
530  count += add_info(dyn_bc, t->key, t->value);
531  for (i=0; ff_nut_dispositions[i].flag; ++i) {
532  if (st->disposition & ff_nut_dispositions[i].flag)
533  count += add_info(dyn_bc, "Disposition", ff_nut_dispositions[i].str);
534  }
535  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
536  uint8_t buf[256];
537  if (st->r_frame_rate.num>0 && st->r_frame_rate.den>0)
538  snprintf(buf, sizeof(buf), "%d/%d", st->r_frame_rate.num, st->r_frame_rate.den);
539  else
540  snprintf(buf, sizeof(buf), "%d/%d", st->avg_frame_rate.num, st->avg_frame_rate.den);
541  count += add_info(dyn_bc, "r_frame_rate", buf);
542  }
543  dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
544 
545  if (count) {
546  ff_put_v(bc, stream_id + 1); //stream_id_plus1
547  ff_put_v(bc, 0); //chapter_id
548  ff_put_v(bc, 0); //timestamp_start
549  ff_put_v(bc, 0); //length
550 
551  ff_put_v(bc, count);
552 
553  avio_write(bc, dyn_buf, dyn_size);
554  }
555 
556  av_free(dyn_buf);
557  return count;
558 }
559 
560 static int write_chapter(NUTContext *nut, AVIOContext *bc, int id)
561 {
562  AVIOContext *dyn_bc;
563  uint8_t *dyn_buf = NULL;
564  AVDictionaryEntry *t = NULL;
565  AVChapter *ch = nut->avf->chapters[id];
566  int ret, dyn_size, count = 0;
567 
568  ret = avio_open_dyn_buf(&dyn_bc);
569  if (ret < 0)
570  return ret;
571 
572  ff_put_v(bc, 0); // stream_id_plus1
573  put_s(bc, id + 1); // chapter_id
574  put_tt(nut, nut->chapter[id].time_base, bc, ch->start); // chapter_start
575  ff_put_v(bc, ch->end - ch->start); // chapter_len
576 
577  while ((t = av_dict_get(ch->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
578  count += add_info(dyn_bc, t->key, t->value);
579 
580  ff_put_v(bc, count);
581 
582  dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
583  avio_write(bc, dyn_buf, dyn_size);
584  av_freep(&dyn_buf);
585  return 0;
586 }
587 
588 static int write_index(NUTContext *nut, AVIOContext *bc) {
589  int i;
590  Syncpoint dummy= { .pos= 0 };
591  Syncpoint *next_node[2] = { NULL };
592  int64_t startpos = avio_tell(bc);
593  int64_t payload_size;
594 
595  put_tt(nut, nut->max_pts_tb, bc, nut->max_pts);
596 
597  ff_put_v(bc, nut->sp_count);
598 
599  for (i=0; i<nut->sp_count; i++) {
600  av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, (void**)next_node);
601  ff_put_v(bc, (next_node[1]->pos >> 4) - (dummy.pos>>4));
602  dummy.pos = next_node[1]->pos;
603  }
604 
605  for (i=0; i<nut->avf->nb_streams; i++) {
606  StreamContext *nus= &nut->stream[i];
607  int64_t last_pts= -1;
608  int j, k;
609  for (j=0; j<nut->sp_count; j++) {
610  int flag;
611  int n = 0;
612 
613  if (j && nus->keyframe_pts[j] == nus->keyframe_pts[j-1]) {
614  av_log(nut->avf, AV_LOG_WARNING, "Multiple keyframes with same PTS\n");
615  nus->keyframe_pts[j] = AV_NOPTS_VALUE;
616  }
617 
618  flag = (nus->keyframe_pts[j] != AV_NOPTS_VALUE) ^ (j+1 == nut->sp_count);
619  for (; j<nut->sp_count && (nus->keyframe_pts[j] != AV_NOPTS_VALUE) == flag; j++)
620  n++;
621 
622  ff_put_v(bc, 1 + 2*flag + 4*n);
623  for (k= j - n; k<=j && k<nut->sp_count; k++) {
624  if (nus->keyframe_pts[k] == AV_NOPTS_VALUE)
625  continue;
626  av_assert0(nus->keyframe_pts[k] > last_pts);
627  ff_put_v(bc, nus->keyframe_pts[k] - last_pts);
628  last_pts = nus->keyframe_pts[k];
629  }
630  }
631  }
632 
633  payload_size = avio_tell(bc) - startpos + 8 + 4;
634 
635  avio_wb64(bc, 8 + payload_size + av_log2(payload_size) / 7 + 1 + 4*(payload_size > 4096));
636 
637  return 0;
638 }
639 
641 {
642  NUTContext *nut = avctx->priv_data;
643  AVIOContext *dyn_bc;
644  int i, ret;
645 
647 
648  ret = avio_open_dyn_buf(&dyn_bc);
649  if (ret < 0)
650  return ret;
651  write_mainheader(nut, dyn_bc);
652  put_packet(nut, bc, dyn_bc, 1, MAIN_STARTCODE);
653 
654  for (i = 0; i < nut->avf->nb_streams; i++) {
655  ret = avio_open_dyn_buf(&dyn_bc);
656  if (ret < 0)
657  return ret;
658  ret = write_streamheader(avctx, dyn_bc, nut->avf->streams[i], i);
659  if (ret < 0)
660  return ret;
661  put_packet(nut, bc, dyn_bc, 1, STREAM_STARTCODE);
662  }
663 
664  ret = avio_open_dyn_buf(&dyn_bc);
665  if (ret < 0)
666  return ret;
667  write_globalinfo(nut, dyn_bc);
668  put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
669 
670  for (i = 0; i < nut->avf->nb_streams; i++) {
671  ret = avio_open_dyn_buf(&dyn_bc);
672  if (ret < 0)
673  return ret;
674  ret = write_streaminfo(nut, dyn_bc, i);
675  if (ret < 0)
676  return ret;
677  if (ret > 0)
678  put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
679  else
680  ffio_free_dyn_buf(&dyn_bc);
681  }
682 
683  for (i = 0; i < nut->avf->nb_chapters; i++) {
684  ret = avio_open_dyn_buf(&dyn_bc);
685  if (ret < 0)
686  return ret;
687  ret = write_chapter(nut, dyn_bc, i);
688  if (ret < 0) {
689  ffio_free_dyn_buf(&dyn_bc);
690  return ret;
691  }
692  put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
693  }
694 
695  nut->last_syncpoint_pos = INT_MIN;
696  nut->header_count++;
697  return 0;
698 }
699 
701 {
702  NUTContext *nut = s->priv_data;
703  AVIOContext *bc = s->pb;
704  int i, j, ret;
705 
706  nut->avf = s;
707 
708  nut->version = FFMAX(NUT_STABLE_VERSION, 3 + !!nut->flags);
710  av_log(s, AV_LOG_ERROR,
711  "The additional syncpoint modes require version %d, "
712  "that is currently not finalized, "
713  "please set -f_strict experimental in order to enable it.\n",
714  nut->version);
715  return AVERROR_EXPERIMENTAL;
716  }
717 
718  nut->stream = av_calloc(s->nb_streams, sizeof(*nut->stream ));
719  nut->chapter = av_calloc(s->nb_chapters, sizeof(*nut->chapter));
720  nut->time_base= av_calloc(s->nb_streams +
721  s->nb_chapters, sizeof(*nut->time_base));
722  if (!nut->stream || !nut->chapter || !nut->time_base) {
723  av_freep(&nut->stream);
724  av_freep(&nut->chapter);
725  av_freep(&nut->time_base);
726  return AVERROR(ENOMEM);
727  }
728 
729  for (i = 0; i < s->nb_streams; i++) {
730  AVStream *st = s->streams[i];
731  int ssize;
732  AVRational time_base;
733  ff_parse_specific_params(st, &time_base.den, &ssize, &time_base.num);
734 
736  time_base = (AVRational) {1, st->codecpar->sample_rate};
737  } else {
738  time_base = ff_choose_timebase(s, st, 48000);
739  }
740 
741  avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
742 
743  for (j = 0; j < nut->time_base_count; j++)
744  if (!memcmp(&time_base, &nut->time_base[j], sizeof(AVRational))) {
745  break;
746  }
747  nut->time_base[j] = time_base;
748  nut->stream[i].time_base = &nut->time_base[j];
749  if (j == nut->time_base_count)
750  nut->time_base_count++;
751 
752  if (INT64_C(1000) * time_base.num >= time_base.den)
753  nut->stream[i].msb_pts_shift = 7;
754  else
755  nut->stream[i].msb_pts_shift = 14;
756  nut->stream[i].max_pts_distance =
757  FFMAX(time_base.den, time_base.num) / time_base.num;
758  }
759 
760  for (i = 0; i < s->nb_chapters; i++) {
761  AVChapter *ch = s->chapters[i];
762 
763  for (j = 0; j < nut->time_base_count; j++)
764  if (!memcmp(&ch->time_base, &nut->time_base[j], sizeof(AVRational)))
765  break;
766 
767  nut->time_base[j] = ch->time_base;
768  nut->chapter[i].time_base = &nut->time_base[j];
769  if (j == nut->time_base_count)
770  nut->time_base_count++;
771  }
772 
773  nut->max_distance = MAX_DISTANCE;
775  build_frame_code(s);
776  av_assert0(nut->frame_code['N'].flags == FLAG_INVALID);
777 
778  avio_write(bc, ID_STRING, strlen(ID_STRING));
779  avio_w8(bc, 0);
780 
781  if ((ret = write_headers(s, bc)) < 0)
782  return ret;
783 
784  if (s->avoid_negative_ts < 0)
785  s->avoid_negative_ts = 1;
786 
787  avio_flush(bc);
788 
789  return 0;
790 }
791 
793  AVPacket *pkt)
794 {
795  int flags = 0;
796 
797  if (pkt->flags & AV_PKT_FLAG_KEY)
798  flags |= FLAG_KEY;
799  if (pkt->stream_index != fc->stream_id)
800  flags |= FLAG_STREAM_ID;
801  if (pkt->size / fc->size_mul)
802  flags |= FLAG_SIZE_MSB;
803  if (pkt->pts - nus->last_pts != fc->pts_delta)
804  flags |= FLAG_CODED_PTS;
805  if (pkt->side_data_elems && nut->version > 3)
806  flags |= FLAG_SM_DATA;
807  if (pkt->size > 2 * nut->max_distance)
808  flags |= FLAG_CHECKSUM;
809  if (FFABS(pkt->pts - nus->last_pts) > nus->max_pts_distance)
810  flags |= FLAG_CHECKSUM;
811  if (pkt->size < nut->header_len[fc->header_idx] ||
812  (pkt->size > 4096 && fc->header_idx) ||
813  memcmp(pkt->data, nut->header[fc->header_idx],
814  nut->header_len[fc->header_idx]))
815  flags |= FLAG_HEADER_IDX;
816 
817  return flags | (fc->flags & FLAG_CODED);
818 }
819 
821 {
822  int i;
823  int best_i = 0;
824  int best_len = 0;
825 
826  if (pkt->size > 4096)
827  return 0;
828 
829  for (i = 1; i < nut->header_count; i++)
830  if (pkt->size >= nut->header_len[i]
831  && nut->header_len[i] > best_len
832  && !memcmp(pkt->data, nut->header[i], nut->header_len[i])) {
833  best_i = i;
834  best_len = nut->header_len[i];
835  }
836  return best_i;
837 }
838 
839 static int write_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta)
840 {
841  int ret, i, dyn_size;
842  unsigned flags;
843  AVIOContext *dyn_bc;
844  int sm_data_count = 0;
845  uint8_t tmp[256];
846  uint8_t *dyn_buf;
847 
848  ret = avio_open_dyn_buf(&dyn_bc);
849  if (ret < 0)
850  return ret;
851 
852  for (i = 0; i<pkt->side_data_elems; i++) {
853  const uint8_t *data = pkt->side_data[i].data;
854  int size = pkt->side_data[i].size;
855  const uint8_t *data_end = data + size;
856 
857  if (is_meta) {
860  if (!size || data[size-1]) {
861  ret = AVERROR(EINVAL);
862  goto fail;
863  }
864  while (data < data_end) {
865  const uint8_t *key = data;
866  const uint8_t *val = data + strlen(key) + 1;
867 
868  if(val >= data_end) {
869  ret = AVERROR(EINVAL);
870  goto fail;
871  }
872  put_str(dyn_bc, key);
873  put_s(dyn_bc, -1);
874  put_str(dyn_bc, val);
875  data = val + strlen(val) + 1;
876  sm_data_count++;
877  }
878  }
879  } else {
880  switch (pkt->side_data[i].type) {
881  case AV_PKT_DATA_PALETTE:
884  default:
885  if (pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
886  put_str(dyn_bc, "Palette");
887  } else if(pkt->side_data[i].type == AV_PKT_DATA_NEW_EXTRADATA) {
888  put_str(dyn_bc, "Extradata");
889  } else if(pkt->side_data[i].type == AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL) {
890  snprintf(tmp, sizeof(tmp), "CodecSpecificSide%"PRId64"", AV_RB64(data));
891  put_str(dyn_bc, tmp);
892  } else {
893  snprintf(tmp, sizeof(tmp), "UserData%s-SD-%d",
894  (s->flags & AVFMT_FLAG_BITEXACT) ? "Lavf" : LIBAVFORMAT_IDENT,
895  pkt->side_data[i].type);
896  put_str(dyn_bc, tmp);
897  }
898  put_s(dyn_bc, -2);
899  put_str(dyn_bc, "bin");
900  ff_put_v(dyn_bc, pkt->side_data[i].size);
901  avio_write(dyn_bc, data, pkt->side_data[i].size);
902  sm_data_count++;
903  break;
905  flags = bytestream_get_le32(&data);
907  put_str(dyn_bc, "Channels");
908  put_s(dyn_bc, bytestream_get_le32(&data));
909  sm_data_count++;
910  }
912  put_str(dyn_bc, "ChannelLayout");
913  put_s(dyn_bc, -2);
914  put_str(dyn_bc, "u64");
915  ff_put_v(bc, 8);
916  avio_write(dyn_bc, data, 8); data+=8;
917  sm_data_count++;
918  }
920  put_str(dyn_bc, "SampleRate");
921  put_s(dyn_bc, bytestream_get_le32(&data));
922  sm_data_count++;
923  }
925  put_str(dyn_bc, "Width");
926  put_s(dyn_bc, bytestream_get_le32(&data));
927  put_str(dyn_bc, "Height");
928  put_s(dyn_bc, bytestream_get_le32(&data));
929  sm_data_count+=2;
930  }
931  break;
933  if (AV_RL32(data)) {
934  put_str(dyn_bc, "SkipStart");
935  put_s(dyn_bc, (unsigned)AV_RL32(data));
936  sm_data_count++;
937  }
938  if (AV_RL32(data+4)) {
939  put_str(dyn_bc, "SkipEnd");
940  put_s(dyn_bc, (unsigned)AV_RL32(data+4));
941  sm_data_count++;
942  }
943  break;
947  // belongs into meta, not side data
948  break;
949  }
950  }
951  }
952 
953 fail:
954  ff_put_v(bc, sm_data_count);
955  dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
956  avio_write(bc, dyn_buf, dyn_size);
957  av_freep(&dyn_buf);
958 
959  return ret;
960 }
961 
963 {
964  NUTContext *nut = s->priv_data;
965  StreamContext *nus = &nut->stream[pkt->stream_index];
966  AVIOContext *bc = s->pb, *dyn_bc, *sm_bc = NULL;
967  FrameCode *fc;
968  int64_t coded_pts;
969  int best_length, frame_code, flags, needed_flags, i, header_idx;
970  int best_header_idx;
971  int key_frame = !!(pkt->flags & AV_PKT_FLAG_KEY);
972  int store_sp = 0;
973  int ret = 0;
974  int sm_size = 0;
975  int data_size = pkt->size;
976  uint8_t *sm_buf = NULL;
977 
978  if (pkt->pts < 0) {
979  av_log(s, AV_LOG_ERROR,
980  "Negative pts not supported stream %d, pts %"PRId64"\n",
981  pkt->stream_index, pkt->pts);
982  if (pkt->pts == AV_NOPTS_VALUE)
983  av_log(s, AV_LOG_ERROR, "Try to enable the genpts flag\n");
984  return AVERROR(EINVAL);
985  }
986 
987  if (pkt->side_data_elems && nut->version > 3) {
988  ret = avio_open_dyn_buf(&sm_bc);
989  if (ret < 0)
990  return ret;
991  ret = write_sm_data(s, sm_bc, pkt, 0);
992  if (ret >= 0)
993  ret = write_sm_data(s, sm_bc, pkt, 1);
994  sm_size = avio_close_dyn_buf(sm_bc, &sm_buf);
995  if (ret < 0)
996  goto fail;
997  data_size += sm_size;
998  }
999 
1000  if (1LL << (20 + 3 * nut->header_count) <= avio_tell(bc))
1001  write_headers(s, bc);
1002 
1003  if (key_frame && !(nus->last_flags & FLAG_KEY))
1004  store_sp = 1;
1005 
1006  if (data_size + 30 /*FIXME check*/ + avio_tell(bc) >= nut->last_syncpoint_pos + nut->max_distance)
1007  store_sp = 1;
1008 
1009 //FIXME: Ensure store_sp is 1 in the first place.
1010 
1011  if (store_sp &&
1012  (!(nut->flags & NUT_PIPE) || nut->last_syncpoint_pos == INT_MIN)) {
1013  int64_t sp_pos = INT64_MAX;
1014 
1015  ff_nut_reset_ts(nut, *nus->time_base, pkt->dts);
1016  for (i = 0; i < s->nb_streams; i++) {
1017  AVStream *st = s->streams[i];
1018  int64_t dts_tb = av_rescale_rnd(pkt->dts,
1019  nus->time_base->num * (int64_t)nut->stream[i].time_base->den,
1020  nus->time_base->den * (int64_t)nut->stream[i].time_base->num,
1021  AV_ROUND_DOWN);
1022  int index = av_index_search_timestamp(st, dts_tb,
1024  if (index >= 0) {
1025  sp_pos = FFMIN(sp_pos, st->index_entries[index].pos);
1026  if (!nut->write_index && 2*index > st->nb_index_entries) {
1027  memmove(st->index_entries,
1028  st->index_entries + index,
1029  sizeof(*st->index_entries) * (st->nb_index_entries - index));
1030  st->nb_index_entries -= index;
1031  }
1032  }
1033  }
1034 
1035  nut->last_syncpoint_pos = avio_tell(bc);
1036  ret = avio_open_dyn_buf(&dyn_bc);
1037  if (ret < 0)
1038  goto fail;
1039  put_tt(nut, nus->time_base, dyn_bc, pkt->dts);
1040  ff_put_v(dyn_bc, sp_pos != INT64_MAX ? (nut->last_syncpoint_pos - sp_pos) >> 4 : 0);
1041 
1042  if (nut->flags & NUT_BROADCAST) {
1043  put_tt(nut, nus->time_base, dyn_bc,
1045  }
1046  put_packet(nut, bc, dyn_bc, 1, SYNCPOINT_STARTCODE);
1047 
1048  if (nut->write_index) {
1049  if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, 0 /*unused*/, pkt->dts)) < 0)
1050  goto fail;
1051 
1052  if ((1ll<<60) % nut->sp_count == 0)
1053  for (i=0; i<s->nb_streams; i++) {
1054  int j;
1055  StreamContext *nus = &nut->stream[i];
1056  av_reallocp_array(&nus->keyframe_pts, 2*nut->sp_count, sizeof(*nus->keyframe_pts));
1057  if (!nus->keyframe_pts) {
1058  ret = AVERROR(ENOMEM);
1059  goto fail;
1060  }
1061  for (j=nut->sp_count == 1 ? 0 : nut->sp_count; j<2*nut->sp_count; j++)
1062  nus->keyframe_pts[j] = AV_NOPTS_VALUE;
1063  }
1064  }
1065  }
1067 
1068  coded_pts = pkt->pts & ((1 << nus->msb_pts_shift) - 1);
1069  if (ff_lsb2full(nus, coded_pts) != pkt->pts)
1070  coded_pts = pkt->pts + (1 << nus->msb_pts_shift);
1071 
1072  best_header_idx = find_best_header_idx(nut, pkt);
1073 
1074  best_length = INT_MAX;
1075  frame_code = -1;
1076  for (i = 0; i < 256; i++) {
1077  int length = 0;
1078  FrameCode *fc = &nut->frame_code[i];
1079  int flags = fc->flags;
1080 
1081  if (flags & FLAG_INVALID)
1082  continue;
1083  needed_flags = get_needed_flags(nut, nus, fc, pkt);
1084 
1085  if (flags & FLAG_CODED) {
1086  length++;
1087  flags = needed_flags;
1088  }
1089 
1090  if ((flags & needed_flags) != needed_flags)
1091  continue;
1092 
1093  if ((flags ^ needed_flags) & FLAG_KEY)
1094  continue;
1095 
1096  if (flags & FLAG_STREAM_ID)
1097  length += ff_get_v_length(pkt->stream_index);
1098 
1099  if (data_size % fc->size_mul != fc->size_lsb)
1100  continue;
1101  if (flags & FLAG_SIZE_MSB)
1102  length += ff_get_v_length(data_size / fc->size_mul);
1103 
1104  if (flags & FLAG_CHECKSUM)
1105  length += 4;
1106 
1107  if (flags & FLAG_CODED_PTS)
1108  length += ff_get_v_length(coded_pts);
1109 
1110  if ( (flags & FLAG_CODED)
1111  && nut->header_len[best_header_idx] > nut->header_len[fc->header_idx] + 1) {
1112  flags |= FLAG_HEADER_IDX;
1113  }
1114 
1115  if (flags & FLAG_HEADER_IDX) {
1116  length += 1 - nut->header_len[best_header_idx];
1117  } else {
1118  length -= nut->header_len[fc->header_idx];
1119  }
1120 
1121  length *= 4;
1122  length += !(flags & FLAG_CODED_PTS);
1123  length += !(flags & FLAG_CHECKSUM);
1124 
1125  if (length < best_length) {
1126  best_length = length;
1127  frame_code = i;
1128  }
1129  }
1130  av_assert0(frame_code != -1);
1131 
1132  fc = &nut->frame_code[frame_code];
1133  flags = fc->flags;
1134  needed_flags = get_needed_flags(nut, nus, fc, pkt);
1135  header_idx = fc->header_idx;
1136 
1138  avio_w8(bc, frame_code);
1139  if (flags & FLAG_CODED) {
1140  ff_put_v(bc, (flags ^ needed_flags) & ~(FLAG_CODED));
1141  flags = needed_flags;
1142  }
1143  if (flags & FLAG_STREAM_ID) ff_put_v(bc, pkt->stream_index);
1144  if (flags & FLAG_CODED_PTS) ff_put_v(bc, coded_pts);
1145  if (flags & FLAG_SIZE_MSB ) ff_put_v(bc, data_size / fc->size_mul);
1146  if (flags & FLAG_HEADER_IDX) ff_put_v(bc, header_idx = best_header_idx);
1147 
1148  if (flags & FLAG_CHECKSUM) avio_wl32(bc, ffio_get_checksum(bc));
1149  else ffio_get_checksum(bc);
1150 
1151  if (flags & FLAG_SM_DATA) {
1152  avio_write(bc, sm_buf, sm_size);
1153  }
1154  avio_write(bc, pkt->data + nut->header_len[header_idx], pkt->size - nut->header_len[header_idx]);
1155 
1156  nus->last_flags = flags;
1157  nus->last_pts = pkt->pts;
1158 
1159  //FIXME just store one per syncpoint
1160  if (flags & FLAG_KEY && !(nut->flags & NUT_PIPE)) {
1162  s->streams[pkt->stream_index],
1163  nut->last_syncpoint_pos,
1164  pkt->pts,
1165  0,
1166  0,
1168  if (nus->keyframe_pts && nus->keyframe_pts[nut->sp_count] == AV_NOPTS_VALUE)
1169  nus->keyframe_pts[nut->sp_count] = pkt->pts;
1170  }
1171 
1172  if (!nut->max_pts_tb || av_compare_ts(nut->max_pts, *nut->max_pts_tb, pkt->pts, *nus->time_base) < 0) {
1173  nut->max_pts = pkt->pts;
1174  nut->max_pts_tb = nus->time_base;
1175  }
1176 
1177 fail:
1178  av_freep(&sm_buf);
1179 
1180  return ret;
1181 }
1182 
1184 {
1185  NUTContext *nut = s->priv_data;
1186  AVIOContext *bc = s->pb, *dyn_bc;
1187  int ret;
1188 
1189  while (nut->header_count < 3)
1190  write_headers(s, bc);
1191 
1192  ret = avio_open_dyn_buf(&dyn_bc);
1193  if (ret >= 0 && nut->sp_count) {
1194  av_assert1(nut->write_index);
1195  write_index(nut, dyn_bc);
1196  put_packet(nut, bc, dyn_bc, 1, INDEX_STARTCODE);
1197  }
1198 
1199  return 0;
1200 }
1201 
1203 {
1204  NUTContext *nut = s->priv_data;
1205  int i;
1206 
1207  ff_nut_free_sp(nut);
1208  if (nut->stream)
1209  for (i=0; i<s->nb_streams; i++)
1210  av_freep(&nut->stream[i].keyframe_pts);
1211 
1212  av_freep(&nut->stream);
1213  av_freep(&nut->chapter);
1214  av_freep(&nut->time_base);
1215 }
1216 
1217 #define OFFSET(x) offsetof(NUTContext, x)
1218 #define E AV_OPT_FLAG_ENCODING_PARAM
1219 static const AVOption options[] = {
1220  { "syncpoints", "NUT syncpoint behaviour", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, E, "syncpoints" },
1221  { "default", "", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, E, "syncpoints" },
1222  { "none", "Disable syncpoints, low overhead and unseekable", 0, AV_OPT_TYPE_CONST, {.i64 = NUT_PIPE}, INT_MIN, INT_MAX, E, "syncpoints" },
1223  { "timestamped", "Extend syncpoints with a wallclock timestamp", 0, AV_OPT_TYPE_CONST, {.i64 = NUT_BROADCAST}, INT_MIN, INT_MAX, E, "syncpoints" },
1224  { "write_index", "Write index", OFFSET(write_index), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, E, },
1225  { NULL },
1226 };
1227 
1228 static const AVClass class = {
1229  .class_name = "nutenc",
1230  .item_name = av_default_item_name,
1231  .option = options,
1233 };
1234 
1236  .name = "nut",
1237  .long_name = NULL_IF_CONFIG_SMALL("NUT"),
1238  .mime_type = "video/x-nut",
1239  .extensions = "nut",
1240  .priv_data_size = sizeof(NUTContext),
1241  .audio_codec = CONFIG_LIBVORBIS ? AV_CODEC_ID_VORBIS :
1242  CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_MP2,
1243  .video_codec = AV_CODEC_ID_MPEG4,
1247  .deinit = nut_write_deinit,
1249  .codec_tag = ff_nut_codec_tags,
1250  .priv_class = &class,
1251 };
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1522
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:2864
#define NUT_STABLE_VERSION
Definition: nut.h:40
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2360
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:423
uint8_t header_len[128]
Definition: nut.h:97
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define MAIN_STARTCODE
Definition: nut.h:29
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
A list of zero terminated key/value strings.
Definition: avcodec.h:1502
int64_t last_syncpoint_pos
Definition: nut.h:104
Definition: nut.h:65
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1224
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition: utils.c:1900
enum AVCodecID id
Definition: mxfenc.c:104
static void nut_write_deinit(AVFormatContext *s)
Definition: nutenc.c:1202
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4405
int64_t pos
Definition: avformat.h:816
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3915
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:943
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1578
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1085
Definition: nut.h:58
int av_log2(unsigned v)
Definition: intmath.c:26
void * av_tree_find(const AVTreeNode *t, void *key, int(*cmp)(const void *key, const void *b), void *next[2])
Definition: tree.c:39
static AVPacket pkt
int ff_nut_sp_pos_cmp(const void *a, const void *b)
Definition: nut.c:247
uint8_t stream_id
Definition: nut.h:67
AVDictionary * metadata
Definition: avformat.h:1286
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1212
mpeg audio layer common tables.
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1601
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3907
const uint8_t * header[128]
Definition: nut.h:98
Format I/O context.
Definition: avformat.h:1319
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Public dictionary API.
int write_index
Definition: nut.h:110
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:329
uint8_t
AVRational * time_base
Definition: nut.h:107
int width
Video only.
Definition: avcodec.h:3981
uint16_t flags
Definition: nut.h:66
AVOptions.
A tree container.
#define ID_STRING
Definition: ffmeta.h:25
static void build_elision_headers(AVFormatContext *s)
Definition: nutenc.c:122
static void build_frame_code(AVFormatContext *s)
Definition: nutenc.c:144
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
static const AVOption options[]
Definition: nutenc.c:1219
const uint16_t avpriv_mpa_freq_tab[3]
Definition: mpegaudiodata.c:40
static int64_t last_pts
#define STREAM_STARTCODE
Definition: nut.h:30
static void put_tt(NUTContext *nut, AVRational *time_base, AVIOContext *bc, uint64_t val)
Definition: nutenc.c:280
#define NUT_PIPE
Definition: nut.h:114
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1387
const AVMetadataConv ff_nut_metadata_conv[]
Definition: nut.c:308
static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: nutenc.c:962
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1430
uint8_t * data
Definition: avcodec.h:1577
int last_flags
Definition: nut.h:76
#define MAX_DISTANCE
Definition: nut.h:37
uint8_t * data
Definition: avcodec.h:1521
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:447
static const uint8_t header[24]
Definition: sdr2.c:67
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:187
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1447
#define av_log(a,...)
AVFormatContext * avf
Definition: nut.h:93
int64_t last_pts
Definition: nut.h:78
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1609
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
AVOutputFormat ff_nut_muxer
Definition: nutenc.c:1235
#define AVINDEX_KEYFRAME
Definition: avformat.h:823
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1533
void ff_nut_free_sp(NUTContext *nut)
Definition: nut.c:290
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1999
void ff_parse_specific_params(AVStream *st, int *au_rate, int *au_ssize, int *au_scale)
Definition: riffenc.c:263
#define NUT_BROADCAST
Definition: nut.h:113
av_default_item_name
An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows:
Definition: avcodec.h:1365
#define AVERROR(e)
Definition: error.h:43
static int nut_write_header(AVFormatContext *s)
Definition: nutenc.c:700
uint64_t pos
Definition: nut.h:59
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
int video_delay
Video only.
Definition: avcodec.h:4010
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define OFFSET(x)
Definition: nutenc.c:1217
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:512
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3911
AVChapter ** chapters
Definition: avformat.h:1523
Definition: graph2dot.c:48
static int write_chapter(NUTContext *nut, AVIOContext *bc, int id)
Definition: nutenc.c:560
simple assert() macros that are a bit more flexible than ISO C assert().
enum AVPacketSideDataType type
Definition: avcodec.h:1523
GLsizei GLsizei * length
Definition: opengl_enc.c:115
int sp_count
Definition: nut.h:109
int header_count
Definition: nut.h:106
AVRational * time_base
Definition: nut.h:80
int side_data_elems
Definition: avcodec.h:1589
int ff_standardize_creation_time(AVFormatContext *s)
Standardize creation_time metadata in AVFormatContext to an ISO-8601 timestamp string.
Definition: utils.c:5069
GLsizei count
Definition: opengl_enc.c:109
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:954
#define FFMAX(a, b)
Definition: common.h:94
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
This function is the same as av_get_audio_frame_duration(), except it works with AVCodecParameters in...
Definition: utils.c:3650
#define fail()
Definition: checkasm.h:81
Definition: nut.h:44
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1583
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare 2 timestamps each in its own timebases.
Definition: mathematics.c:147
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3933
static void write_mainheader(NUTContext *nut, AVIOContext *bc)
Definition: nutenc.c:345
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1375
#define LIBAVFORMAT_IDENT
Definition: version.h:44
void ffio_init_checksum(AVIOContext *s, unsigned long(*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum)
Definition: aviobuf.c:529
static const uint16_t fc[]
Definition: dcaenc.h:41
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:207
AVRational * max_pts_tb
Definition: nut.h:112
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
void ff_nut_reset_ts(NUTContext *nut, AVRational time_base, int64_t val)
Definition: nut.c:229
int flags
Definition: nut.h:115
int flag
Definition: cpu-test.c:33
#define FFMIN(a, b)
Definition: common.h:96
#define E
Definition: nutenc.c:1218
uint8_t header_idx
Definition: nut.h:72
This side data contains quality related information from the encoder.
Definition: avcodec.h:1425
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
static uint8_t tmp[8]
Definition: des-test.c:38
uint16_t size_lsb
Definition: nut.h:69
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:483
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:509
int16_t pts_delta
Definition: nut.h:70
int64_t * keyframe_pts
Definition: nut.h:84
int64_t ff_lsb2full(StreamContext *stream, int64_t lsb)
Definition: nut.c:240
const char * name
Definition: avformat.h:522
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
#define AV_WB24(p, d)
Definition: intreadwrite.h:450
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
struct AVTreeNode * syncpoints
Definition: nut.h:108
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1624
ChapterContext * chapter
Definition: nut.h:101
int n
Definition: avisynth_c.h:547
AVDictionary * metadata
Definition: avformat.h:945
#define INDEX_STARTCODE
Definition: nut.h:32
uint16_t size_mul
Definition: nut.h:68
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:194
static int write_globalinfo(NUTContext *nut, AVIOContext *bc)
Definition: nutenc.c:490
#define AVERROR_EXPERIMENTAL
Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it...
Definition: error.h:72
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1252
static const float pred[4]
Definition: siprdata.h:259
FILE * out
Definition: movenc-test.c:54
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
Stream structure.
Definition: avformat.h:876
int msb_pts_shift
Definition: nut.h:81
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1285
sample_rate
int frame_size
Definition: mxfenc.c:1821
A list of zero terminated key/value strings.
Definition: avcodec.h:1465
static int intra_only
Definition: ffmpeg_opt.c:121
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:252
AVIOContext * pb
I/O context.
Definition: avformat.h:1361
int max_pts_distance
Definition: nut.h:82
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:165
static int write_streaminfo(NUTContext *nut, AVIOContext *bc, int stream_id)
Definition: nutenc.c:518
static int write_index(NUTContext *nut, AVIOContext *bc)
Definition: nutenc.c:588
static int find_expected_header(AVCodecParameters *p, int size, int key_frame, uint8_t out[64])
Definition: nutenc.c:38
Definition: nut.h:54
void * buf
Definition: avisynth_c.h:553
Data found in BlockAdditional element of matroska container.
Definition: avcodec.h:1484
GLint GLenum type
Definition: opengl_enc.c:105
AVRational ff_choose_timebase(AVFormatContext *s, AVStream *st, int min_precision)
Chooses a timebase for muxing the specified stream.
Definition: mux.c:102
int nb_index_entries
Definition: avformat.h:1087
static int write_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta)
Definition: nutenc.c:839
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
int index
Definition: gxfenc.c:89
rational number numerator/denominator
Definition: rational.h:43
static int nut_write_trailer(AVFormatContext *s)
Definition: nutenc.c:1183
Recommmends skipping the specified number of samples.
Definition: avcodec.h:1449
AVRational * time_base
Definition: nut.h:88
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:521
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:67
StreamContext * stream
Definition: nut.h:100
#define snprintf
Definition: snprintf.h:34
static void put_s(AVIOContext *bc, int64_t val)
Definition: nutenc.c:297
static int write_streamheader(AVFormatContext *avctx, AVIOContext *bc, AVStream *st, int i)
Definition: nutenc.c:425
Round toward -infinity.
Definition: mathematics.h:73
#define INFO_STARTCODE
Definition: nut.h:33
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:260
static int64_t pts
Global timestamp for the audio frames.
int ff_get_v_length(uint64_t val)
Get the length in bytes which is needed to store val as v.
Definition: aviobuf.c:397
int version
Definition: nut.h:116
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:133
static int write_headers(AVFormatContext *avctx, AVIOContext *bc)
Definition: nutenc.c:640
static int flags
Definition: cpu.c:47
int64_t start
Definition: avformat.h:1285
int sample_rate
Audio only.
Definition: avcodec.h:4025
const Dispositions ff_nut_dispositions[]
Definition: nut.c:298
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
Definition: bytestream.h:87
int64_t max_pts
Definition: nut.h:111
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition: avcodec.h:1588
static int find_best_header_idx(NUTContext *nut, AVPacket *pkt)
Definition: nutenc.c:820
if(ret< 0)
Definition: vf_mcdeint.c:282
FrameCode frame_code[256]
Definition: nut.h:96
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:934
int ff_nut_add_sp(NUTContext *nut, int64_t pos, int64_t back_ptr, int64_t ts)
Definition: nut.c:259
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1284
char * key
Definition: dict.h:86
int den
denominator
Definition: rational.h:45
#define SYNCPOINT_STARTCODE
Definition: nut.h:31
int flag
Definition: nut.h:130
static void put_str(AVIOContext *bc, const char *string)
Store a string as vb.
Definition: nutenc.c:289
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:487
#define av_free(p)
char * value
Definition: dict.h:87
static void put_packet(NUTContext *nut, AVIOContext *bc, AVIOContext *dyn_bc, int calculate_checksum, uint64_t startcode)
Definition: nutenc.c:322
void ff_put_v(AVIOContext *bc, uint64_t val)
Put val using a variable number of bytes.
Definition: aviobuf.c:407
int len
void * priv_data
Format private data.
Definition: avformat.h:1347
static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc, AVPacket *pkt)
Definition: nutenc.c:792
static int find_header_idx(AVFormatContext *s, AVCodecParameters *p, int size, int frame_type)
Definition: nutenc.c:105
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:497
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3929
int channels
Audio only.
Definition: avcodec.h:4021
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1576
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:553
static int add_info(AVIOContext *bc, const char *type, const char *value)
Definition: nutenc.c:482
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
AVCodecParameters * codecpar
Definition: avformat.h:1006
const uint16_t avpriv_mpa_bitrate_tab[2][3][15]
Definition: mpegaudiodata.c:30
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3919
int stream_index
Definition: avcodec.h:1579
int dummy
Definition: motion-test.c:64
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1101
const AVCodecTag *const ff_nut_codec_tags[]
Definition: nut.c:224
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1554
unsigned int time_base_count
Definition: nut.h:103
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1570
int minor_version
Definition: nut.h:117
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
unsigned int max_distance
Definition: nut.h:102