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(AVCodecContext *c, int size, int key_frame,
39  uint8_t out[64])
40 {
41  int sample_rate = c->sample_rate;
42 
43  if (size > 4096)
44  return 0;
45 
46  AV_WB24(out, 1);
47 
48  if (c->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 (c->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
57  return 3;
58  } else if (c->codec_id == AV_CODEC_ID_H264) {
59  return 3;
60  } else if (c->codec_id == AV_CODEC_ID_MP3 ||
61  c->codec_id == AV_CODEC_ID_MP2) {
62  int lsf, mpeg25, sample_rate_index, bitrate_index, frame_size;
63  int layer = c->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 
105 static int find_header_idx(AVFormatContext *s, AVCodecContext *c, int size, int frame_type)
106 {
107  NUTContext *nut = s->priv_data;
108  uint8_t out[64];
109  int i;
110  int len = find_expected_header(c, size, frame_type, out);
111 
112  for (i = 1; i < nut->header_count; i++) {
113  if (len == nut->header_len[i] && !memcmp(out, nut->header[i], len)) {
114  return i;
115  }
116  }
117 
118  return 0;
119 }
120 
122 {
123  NUTContext *nut = s->priv_data;
124  int i;
125  //FIXME this is lame
126  //FIXME write a 2pass mode to find the maximal headers
127  static const uint8_t headers[][5] = {
128  { 3, 0x00, 0x00, 0x01 },
129  { 4, 0x00, 0x00, 0x01, 0xB6},
130  { 2, 0xFF, 0xFA }, //mp3+crc
131  { 2, 0xFF, 0xFB }, //mp3
132  { 2, 0xFF, 0xFC }, //mp2+crc
133  { 2, 0xFF, 0xFD }, //mp2
134  };
135 
136  nut->header_count = 7;
137  for (i = 1; i < nut->header_count; i++) {
138  nut->header_len[i] = headers[i - 1][0];
139  nut->header[i] = &headers[i - 1][1];
140  }
141 }
142 
144 {
145  NUTContext *nut = s->priv_data;
146  int key_frame, index, pred, stream_id;
147  int start = 1;
148  int end = 254;
149  int keyframe_0_esc = s->nb_streams > 2;
150  int pred_table[10];
151  FrameCode *ft;
152 
153  ft = &nut->frame_code[start];
154  ft->flags = FLAG_CODED;
155  ft->size_mul = 1;
156  ft->pts_delta = 1;
157  start++;
158 
159  if (keyframe_0_esc) {
160  /* keyframe = 0 escape */
161  FrameCode *ft = &nut->frame_code[start];
163  ft->size_mul = 1;
164  start++;
165  }
166 
167  for (stream_id = 0; stream_id < s->nb_streams; stream_id++) {
168  int start2 = start + (end - start) * stream_id / s->nb_streams;
169  int end2 = start + (end - start) * (stream_id + 1) / s->nb_streams;
170  AVCodecContext *codec = s->streams[stream_id]->codec;
171  int is_audio = codec->codec_type == AVMEDIA_TYPE_AUDIO;
172  int intra_only = /*codec->intra_only || */ is_audio;
173  int pred_count;
174  int frame_size = 0;
175 
176  if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
178  if (codec->codec_id == AV_CODEC_ID_VORBIS && !frame_size)
179  frame_size = 64;
180  } else {
181  AVRational f = av_div_q(codec->time_base, *nut->stream[stream_id].time_base);
182  if (f.den == 1 && f.num>0)
183  frame_size = f.num;
184  }
185  if (!frame_size)
186  frame_size = 1;
187 
188  for (key_frame = 0; key_frame < 2; key_frame++) {
189  if (!intra_only || !keyframe_0_esc || key_frame != 0) {
190  FrameCode *ft = &nut->frame_code[start2];
191  ft->flags = FLAG_KEY * key_frame;
193  ft->stream_id = stream_id;
194  ft->size_mul = 1;
195  if (is_audio)
196  ft->header_idx = find_header_idx(s, codec, -1, key_frame);
197  start2++;
198  }
199  }
200 
201  key_frame = intra_only;
202 #if 1
203  if (is_audio) {
204  int frame_bytes = codec->frame_size * (int64_t)codec->bit_rate /
205  (8 * codec->sample_rate);
206  int pts;
207  for (pts = 0; pts < 2; pts++) {
208  for (pred = 0; pred < 2; pred++) {
209  FrameCode *ft = &nut->frame_code[start2];
210  ft->flags = FLAG_KEY * key_frame;
211  ft->stream_id = stream_id;
212  ft->size_mul = frame_bytes + 2;
213  ft->size_lsb = frame_bytes + pred;
214  ft->pts_delta = pts * frame_size;
215  ft->header_idx = find_header_idx(s, codec, frame_bytes + pred, key_frame);
216  start2++;
217  }
218  }
219  } else {
220  FrameCode *ft = &nut->frame_code[start2];
221  ft->flags = FLAG_KEY | FLAG_SIZE_MSB;
222  ft->stream_id = stream_id;
223  ft->size_mul = 1;
224  ft->pts_delta = frame_size;
225  start2++;
226  }
227 #endif
228 
229  if (codec->has_b_frames) {
230  pred_count = 5;
231  pred_table[0] = -2;
232  pred_table[1] = -1;
233  pred_table[2] = 1;
234  pred_table[3] = 3;
235  pred_table[4] = 4;
236  } else if (codec->codec_id == AV_CODEC_ID_VORBIS) {
237  pred_count = 3;
238  pred_table[0] = 2;
239  pred_table[1] = 9;
240  pred_table[2] = 16;
241  } else {
242  pred_count = 1;
243  pred_table[0] = 1;
244  }
245 
246  for (pred = 0; pred < pred_count; pred++) {
247  int start3 = start2 + (end2 - start2) * pred / pred_count;
248  int end3 = start2 + (end2 - start2) * (pred + 1) / pred_count;
249 
250  pred_table[pred] *= frame_size;
251 
252  for (index = start3; index < end3; index++) {
253  FrameCode *ft = &nut->frame_code[index];
254  ft->flags = FLAG_KEY * key_frame;
255  ft->flags |= FLAG_SIZE_MSB;
256  ft->stream_id = stream_id;
257 //FIXME use single byte size and pred from last
258  ft->size_mul = end3 - start3;
259  ft->size_lsb = index - start3;
260  ft->pts_delta = pred_table[pred];
261  if (is_audio)
262  ft->header_idx = find_header_idx(s, codec, -1, key_frame);
263  }
264  }
265  }
266  memmove(&nut->frame_code['N' + 1], &nut->frame_code['N'], sizeof(FrameCode) * (255 - 'N'));
267  nut->frame_code[0].flags =
268  nut->frame_code[255].flags =
269  nut->frame_code['N'].flags = FLAG_INVALID;
270 }
271 
272 static void put_tt(NUTContext *nut, AVRational *time_base, AVIOContext *bc, uint64_t val)
273 {
274  val *= nut->time_base_count;
275  val += time_base - nut->time_base;
276  ff_put_v(bc, val);
277 }
278 /**
279  * Store a string as vb.
280  */
281 static void put_str(AVIOContext *bc, const char *string)
282 {
283  size_t len = strlen(string);
284 
285  ff_put_v(bc, len);
286  avio_write(bc, string, len);
287 }
288 
289 static void put_s(AVIOContext *bc, int64_t val)
290 {
291  ff_put_v(bc, 2 * FFABS(val) - (val > 0));
292 }
293 
294 #ifdef TRACE
295 static inline void ff_put_v_trace(AVIOContext *bc, uint64_t v, const char *file,
296  const char *func, int line)
297 {
298  av_log(NULL, AV_LOG_DEBUG, "ff_put_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
299 
300  ff_put_v(bc, v);
301 }
302 
303 static inline void put_s_trace(AVIOContext *bc, int64_t v, const char *file, const char *func, int line)
304 {
305  av_log(NULL, AV_LOG_DEBUG, "put_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
306 
307  put_s(bc, v);
308 }
309 #define ff_put_v(bc, v) ff_put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
310 #define put_s(bc, v) put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
311 #endif
312 
313 //FIXME remove calculate_checksum
314 static void put_packet(NUTContext *nut, AVIOContext *bc, AVIOContext *dyn_bc,
315  int calculate_checksum, uint64_t startcode)
316 {
317  uint8_t *dyn_buf = NULL;
318  int dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
319  int forw_ptr = dyn_size + 4 * calculate_checksum;
320 
321  if (forw_ptr > 4096)
323  avio_wb64(bc, startcode);
324  ff_put_v(bc, forw_ptr);
325  if (forw_ptr > 4096)
326  avio_wl32(bc, ffio_get_checksum(bc));
327 
328  if (calculate_checksum)
330  avio_write(bc, dyn_buf, dyn_size);
331  if (calculate_checksum)
332  avio_wl32(bc, ffio_get_checksum(bc));
333 
334  av_free(dyn_buf);
335 }
336 
338 {
339  int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields,
340  tmp_head_idx;
341  int64_t tmp_match;
342 
343  ff_put_v(bc, nut->version);
344  if (nut->version > 3)
345  ff_put_v(bc, nut->minor_version = 1);
346  ff_put_v(bc, nut->avf->nb_streams);
347  ff_put_v(bc, nut->max_distance);
348  ff_put_v(bc, nut->time_base_count);
349 
350  for (i = 0; i < nut->time_base_count; i++) {
351  ff_put_v(bc, nut->time_base[i].num);
352  ff_put_v(bc, nut->time_base[i].den);
353  }
354 
355  tmp_pts = 0;
356  tmp_mul = 1;
357  tmp_stream = 0;
358  tmp_match = 1 - (1LL << 62);
359  tmp_head_idx = 0;
360  for (i = 0; i < 256; ) {
361  tmp_fields = 0;
362  tmp_size = 0;
363 // tmp_res=0;
364  if (tmp_pts != nut->frame_code[i].pts_delta ) tmp_fields = 1;
365  if (tmp_mul != nut->frame_code[i].size_mul ) tmp_fields = 2;
366  if (tmp_stream != nut->frame_code[i].stream_id ) tmp_fields = 3;
367  if (tmp_size != nut->frame_code[i].size_lsb ) tmp_fields = 4;
368 // if (tmp_res != nut->frame_code[i].res ) tmp_fields=5;
369  if (tmp_head_idx != nut->frame_code[i].header_idx) tmp_fields = 8;
370 
371  tmp_pts = nut->frame_code[i].pts_delta;
372  tmp_flags = nut->frame_code[i].flags;
373  tmp_stream = nut->frame_code[i].stream_id;
374  tmp_mul = nut->frame_code[i].size_mul;
375  tmp_size = nut->frame_code[i].size_lsb;
376 // tmp_res = nut->frame_code[i].res;
377  tmp_head_idx = nut->frame_code[i].header_idx;
378 
379  for (j = 0; i < 256; j++, i++) {
380  if (i == 'N') {
381  j--;
382  continue;
383  }
384  if (nut->frame_code[i].pts_delta != tmp_pts ||
385  nut->frame_code[i].flags != tmp_flags ||
386  nut->frame_code[i].stream_id != tmp_stream ||
387  nut->frame_code[i].size_mul != tmp_mul ||
388  nut->frame_code[i].size_lsb != tmp_size + j ||
389 // nut->frame_code[i].res != tmp_res ||
390  nut->frame_code[i].header_idx != tmp_head_idx)
391  break;
392  }
393  if (j != tmp_mul - tmp_size)
394  tmp_fields = 6;
395 
396  ff_put_v(bc, tmp_flags);
397  ff_put_v(bc, tmp_fields);
398  if (tmp_fields > 0) put_s(bc, tmp_pts);
399  if (tmp_fields > 1) ff_put_v(bc, tmp_mul);
400  if (tmp_fields > 2) ff_put_v(bc, tmp_stream);
401  if (tmp_fields > 3) ff_put_v(bc, tmp_size);
402  if (tmp_fields > 4) ff_put_v(bc, 0 /*tmp_res*/);
403  if (tmp_fields > 5) ff_put_v(bc, j);
404  if (tmp_fields > 6) ff_put_v(bc, tmp_match);
405  if (tmp_fields > 7) ff_put_v(bc, tmp_head_idx);
406  }
407  ff_put_v(bc, nut->header_count - 1);
408  for (i = 1; i < nut->header_count; i++) {
409  ff_put_v(bc, nut->header_len[i]);
410  avio_write(bc, nut->header[i], nut->header_len[i]);
411  }
412  // flags had been effectively introduced in version 4
413  if (nut->version > 3)
414  ff_put_v(bc, nut->flags);
415 }
416 
418  AVStream *st, int i)
419 {
420  NUTContext *nut = avctx->priv_data;
421  AVCodecContext *codec = st->codec;
422 
423  ff_put_v(bc, i);
424  switch (codec->codec_type) {
425  case AVMEDIA_TYPE_VIDEO: ff_put_v(bc, 0); break;
426  case AVMEDIA_TYPE_AUDIO: ff_put_v(bc, 1); break;
427  case AVMEDIA_TYPE_SUBTITLE: ff_put_v(bc, 2); break;
428  default: ff_put_v(bc, 3); break;
429  }
430  ff_put_v(bc, 4);
431  if (codec->codec_tag) {
432  avio_wl32(bc, codec->codec_tag);
433  } else {
434  av_log(avctx, AV_LOG_ERROR, "No codec tag defined for stream %d\n", i);
435  return AVERROR(EINVAL);
436  }
437 
438  ff_put_v(bc, nut->stream[i].time_base - nut->time_base);
439  ff_put_v(bc, nut->stream[i].msb_pts_shift);
440  ff_put_v(bc, nut->stream[i].max_pts_distance);
441  ff_put_v(bc, codec->has_b_frames);
442  avio_w8(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
443 
444  ff_put_v(bc, codec->extradata_size);
445  avio_write(bc, codec->extradata, codec->extradata_size);
446 
447  switch (codec->codec_type) {
448  case AVMEDIA_TYPE_AUDIO:
449  ff_put_v(bc, codec->sample_rate);
450  ff_put_v(bc, 1);
451  ff_put_v(bc, codec->channels);
452  break;
453  case AVMEDIA_TYPE_VIDEO:
454  ff_put_v(bc, codec->width);
455  ff_put_v(bc, codec->height);
456 
457  if (st->sample_aspect_ratio.num <= 0 ||
458  st->sample_aspect_ratio.den <= 0) {
459  ff_put_v(bc, 0);
460  ff_put_v(bc, 0);
461  } else {
464  }
465  ff_put_v(bc, 0); /* csp type -- unknown */
466  break;
467  default:
468  break;
469  }
470  return 0;
471 }
472 
473 static int add_info(AVIOContext *bc, const char *type, const char *value)
474 {
475  put_str(bc, type);
476  put_s(bc, -1);
477  put_str(bc, value);
478  return 1;
479 }
480 
482 {
483  AVFormatContext *s = nut->avf;
484  AVDictionaryEntry *t = NULL;
485  AVIOContext *dyn_bc;
486  uint8_t *dyn_buf = NULL;
487  int count = 0, dyn_size;
488  int ret = avio_open_dyn_buf(&dyn_bc);
489  if (ret < 0)
490  return ret;
491 
492  while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
493  count += add_info(dyn_bc, t->key, t->value);
494 
495  ff_put_v(bc, 0); //stream_if_plus1
496  ff_put_v(bc, 0); //chapter_id
497  ff_put_v(bc, 0); //timestamp_start
498  ff_put_v(bc, 0); //length
499 
500  ff_put_v(bc, count);
501 
502  dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
503  avio_write(bc, dyn_buf, dyn_size);
504  av_free(dyn_buf);
505  return 0;
506 }
507 
508 static int write_streaminfo(NUTContext *nut, AVIOContext *bc, int stream_id) {
509  AVFormatContext *s= nut->avf;
510  AVStream* st = s->streams[stream_id];
511  AVDictionaryEntry *t = NULL;
512  AVIOContext *dyn_bc;
513  uint8_t *dyn_buf=NULL;
514  int count=0, dyn_size, i;
515  int ret = avio_open_dyn_buf(&dyn_bc);
516  if (ret < 0)
517  return ret;
518 
519  while ((t = av_dict_get(st->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
520  count += add_info(dyn_bc, t->key, t->value);
521  for (i=0; ff_nut_dispositions[i].flag; ++i) {
522  if (st->disposition & ff_nut_dispositions[i].flag)
523  count += add_info(dyn_bc, "Disposition", ff_nut_dispositions[i].str);
524  }
525  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
526  uint8_t buf[256];
527  if (st->r_frame_rate.num>0 && st->r_frame_rate.den>0)
528  snprintf(buf, sizeof(buf), "%d/%d", st->r_frame_rate.num, st->r_frame_rate.den);
529  else
530  snprintf(buf, sizeof(buf), "%d/%d", st->codec->time_base.den, st->codec->time_base.num);
531  count += add_info(dyn_bc, "r_frame_rate", buf);
532  }
533  dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
534 
535  if (count) {
536  ff_put_v(bc, stream_id + 1); //stream_id_plus1
537  ff_put_v(bc, 0); //chapter_id
538  ff_put_v(bc, 0); //timestamp_start
539  ff_put_v(bc, 0); //length
540 
541  ff_put_v(bc, count);
542 
543  avio_write(bc, dyn_buf, dyn_size);
544  }
545 
546  av_free(dyn_buf);
547  return count;
548 }
549 
550 static int write_chapter(NUTContext *nut, AVIOContext *bc, int id)
551 {
552  AVIOContext *dyn_bc;
553  uint8_t *dyn_buf = NULL;
554  AVDictionaryEntry *t = NULL;
555  AVChapter *ch = nut->avf->chapters[id];
556  int ret, dyn_size, count = 0;
557 
558  ret = avio_open_dyn_buf(&dyn_bc);
559  if (ret < 0)
560  return ret;
561 
562  ff_put_v(bc, 0); // stream_id_plus1
563  put_s(bc, id + 1); // chapter_id
564  put_tt(nut, nut->chapter[id].time_base, bc, ch->start); // chapter_start
565  ff_put_v(bc, ch->end - ch->start); // chapter_len
566 
567  while ((t = av_dict_get(ch->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
568  count += add_info(dyn_bc, t->key, t->value);
569 
570  ff_put_v(bc, count);
571 
572  dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
573  avio_write(bc, dyn_buf, dyn_size);
574  av_freep(&dyn_buf);
575  return 0;
576 }
577 
578 static int write_index(NUTContext *nut, AVIOContext *bc) {
579  int i;
580  Syncpoint dummy= { .pos= 0 };
581  Syncpoint *next_node[2] = { NULL };
582  int64_t startpos = avio_tell(bc);
583  int64_t payload_size;
584 
585  put_tt(nut, nut->max_pts_tb, bc, nut->max_pts);
586 
587  ff_put_v(bc, nut->sp_count);
588 
589  for (i=0; i<nut->sp_count; i++) {
590  av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, (void**)next_node);
591  ff_put_v(bc, (next_node[1]->pos >> 4) - (dummy.pos>>4));
592  dummy.pos = next_node[1]->pos;
593  }
594 
595  for (i=0; i<nut->avf->nb_streams; i++) {
596  StreamContext *nus= &nut->stream[i];
597  int64_t last_pts= -1;
598  int j, k;
599  for (j=0; j<nut->sp_count; j++) {
600  int flag;
601  int n = 0;
602 
603  if (j && nus->keyframe_pts[j] == nus->keyframe_pts[j-1]) {
604  av_log(nut->avf, AV_LOG_WARNING, "Multiple keyframes with same PTS\n");
605  nus->keyframe_pts[j] = AV_NOPTS_VALUE;
606  }
607 
608  flag = (nus->keyframe_pts[j] != AV_NOPTS_VALUE) ^ (j+1 == nut->sp_count);
609  for (; j<nut->sp_count && (nus->keyframe_pts[j] != AV_NOPTS_VALUE) == flag; j++)
610  n++;
611 
612  ff_put_v(bc, 1 + 2*flag + 4*n);
613  for (k= j - n; k<=j && k<nut->sp_count; k++) {
614  if (nus->keyframe_pts[k] == AV_NOPTS_VALUE)
615  continue;
616  av_assert0(nus->keyframe_pts[k] > last_pts);
617  ff_put_v(bc, nus->keyframe_pts[k] - last_pts);
618  last_pts = nus->keyframe_pts[k];
619  }
620  }
621  }
622 
623  payload_size = avio_tell(bc) - startpos + 8 + 4;
624 
625  avio_wb64(bc, 8 + payload_size + av_log2(payload_size) / 7 + 1 + 4*(payload_size > 4096));
626 
627  return 0;
628 }
629 
631 {
632  NUTContext *nut = avctx->priv_data;
633  AVIOContext *dyn_bc;
634  int i, ret;
635 
637 
638  ret = avio_open_dyn_buf(&dyn_bc);
639  if (ret < 0)
640  return ret;
641  write_mainheader(nut, dyn_bc);
642  put_packet(nut, bc, dyn_bc, 1, MAIN_STARTCODE);
643 
644  for (i = 0; i < nut->avf->nb_streams; i++) {
645  ret = avio_open_dyn_buf(&dyn_bc);
646  if (ret < 0)
647  return ret;
648  ret = write_streamheader(avctx, dyn_bc, nut->avf->streams[i], i);
649  if (ret < 0)
650  return ret;
651  put_packet(nut, bc, dyn_bc, 1, STREAM_STARTCODE);
652  }
653 
654  ret = avio_open_dyn_buf(&dyn_bc);
655  if (ret < 0)
656  return ret;
657  write_globalinfo(nut, dyn_bc);
658  put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
659 
660  for (i = 0; i < nut->avf->nb_streams; i++) {
661  ret = avio_open_dyn_buf(&dyn_bc);
662  if (ret < 0)
663  return ret;
664  ret = write_streaminfo(nut, dyn_bc, i);
665  if (ret < 0)
666  return ret;
667  if (ret > 0)
668  put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
669  else
670  ffio_free_dyn_buf(&dyn_bc);
671  }
672 
673  for (i = 0; i < nut->avf->nb_chapters; i++) {
674  ret = avio_open_dyn_buf(&dyn_bc);
675  if (ret < 0)
676  return ret;
677  ret = write_chapter(nut, dyn_bc, i);
678  if (ret < 0) {
679  ffio_free_dyn_buf(&dyn_bc);
680  return ret;
681  }
682  put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
683  }
684 
685  nut->last_syncpoint_pos = INT_MIN;
686  nut->header_count++;
687  return 0;
688 }
689 
691 {
692  NUTContext *nut = s->priv_data;
693  AVIOContext *bc = s->pb;
694  int i, j, ret;
695 
696  nut->avf = s;
697 
698  nut->version = FFMAX(NUT_STABLE_VERSION, 3 + !!nut->flags);
700  av_log(s, AV_LOG_ERROR,
701  "The additional syncpoint modes require version %d, "
702  "that is currently not finalized, "
703  "please set -f_strict experimental in order to enable it.\n",
704  nut->version);
705  return AVERROR_EXPERIMENTAL;
706  }
707 
708  nut->stream = av_calloc(s->nb_streams, sizeof(*nut->stream ));
709  nut->chapter = av_calloc(s->nb_chapters, sizeof(*nut->chapter));
710  nut->time_base= av_calloc(s->nb_streams +
711  s->nb_chapters, sizeof(*nut->time_base));
712  if (!nut->stream || !nut->chapter || !nut->time_base) {
713  av_freep(&nut->stream);
714  av_freep(&nut->chapter);
715  av_freep(&nut->time_base);
716  return AVERROR(ENOMEM);
717  }
718 
719  for (i = 0; i < s->nb_streams; i++) {
720  AVStream *st = s->streams[i];
721  int ssize;
722  AVRational time_base;
723  ff_parse_specific_params(st, &time_base.den, &ssize, &time_base.num);
724 
725  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->codec->sample_rate) {
726  time_base = (AVRational) {1, st->codec->sample_rate};
727  } else {
728  time_base = ff_choose_timebase(s, st, 48000);
729  }
730 
731  avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
732 
733  for (j = 0; j < nut->time_base_count; j++)
734  if (!memcmp(&time_base, &nut->time_base[j], sizeof(AVRational))) {
735  break;
736  }
737  nut->time_base[j] = time_base;
738  nut->stream[i].time_base = &nut->time_base[j];
739  if (j == nut->time_base_count)
740  nut->time_base_count++;
741 
742  if (INT64_C(1000) * time_base.num >= time_base.den)
743  nut->stream[i].msb_pts_shift = 7;
744  else
745  nut->stream[i].msb_pts_shift = 14;
746  nut->stream[i].max_pts_distance =
747  FFMAX(time_base.den, time_base.num) / time_base.num;
748  }
749 
750  for (i = 0; i < s->nb_chapters; i++) {
751  AVChapter *ch = s->chapters[i];
752 
753  for (j = 0; j < nut->time_base_count; j++)
754  if (!memcmp(&ch->time_base, &nut->time_base[j], sizeof(AVRational)))
755  break;
756 
757  nut->time_base[j] = ch->time_base;
758  nut->chapter[i].time_base = &nut->time_base[j];
759  if (j == nut->time_base_count)
760  nut->time_base_count++;
761  }
762 
763  nut->max_distance = MAX_DISTANCE;
765  build_frame_code(s);
766  av_assert0(nut->frame_code['N'].flags == FLAG_INVALID);
767 
768  avio_write(bc, ID_STRING, strlen(ID_STRING));
769  avio_w8(bc, 0);
770 
771  if ((ret = write_headers(s, bc)) < 0)
772  return ret;
773 
774  if (s->avoid_negative_ts < 0)
775  s->avoid_negative_ts = 1;
776 
777  avio_flush(bc);
778 
779  return 0;
780 }
781 
783  AVPacket *pkt)
784 {
785  int flags = 0;
786 
787  if (pkt->flags & AV_PKT_FLAG_KEY)
788  flags |= FLAG_KEY;
789  if (pkt->stream_index != fc->stream_id)
790  flags |= FLAG_STREAM_ID;
791  if (pkt->size / fc->size_mul)
792  flags |= FLAG_SIZE_MSB;
793  if (pkt->pts - nus->last_pts != fc->pts_delta)
794  flags |= FLAG_CODED_PTS;
795  if (pkt->side_data_elems && nut->version > 3)
796  flags |= FLAG_SM_DATA;
797  if (pkt->size > 2 * nut->max_distance)
798  flags |= FLAG_CHECKSUM;
799  if (FFABS(pkt->pts - nus->last_pts) > nus->max_pts_distance)
800  flags |= FLAG_CHECKSUM;
801  if (pkt->size < nut->header_len[fc->header_idx] ||
802  (pkt->size > 4096 && fc->header_idx) ||
803  memcmp(pkt->data, nut->header[fc->header_idx],
804  nut->header_len[fc->header_idx]))
805  flags |= FLAG_HEADER_IDX;
806 
807  return flags | (fc->flags & FLAG_CODED);
808 }
809 
811 {
812  int i;
813  int best_i = 0;
814  int best_len = 0;
815 
816  if (pkt->size > 4096)
817  return 0;
818 
819  for (i = 1; i < nut->header_count; i++)
820  if (pkt->size >= nut->header_len[i]
821  && nut->header_len[i] > best_len
822  && !memcmp(pkt->data, nut->header[i], nut->header_len[i])) {
823  best_i = i;
824  best_len = nut->header_len[i];
825  }
826  return best_i;
827 }
828 
829 static int write_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta)
830 {
831  int ret, i, dyn_size;
832  unsigned flags;
833  AVIOContext *dyn_bc;
834  int sm_data_count = 0;
835  uint8_t tmp[256];
836  uint8_t *dyn_buf;
837 
838  ret = avio_open_dyn_buf(&dyn_bc);
839  if (ret < 0)
840  return ret;
841 
842  for (i = 0; i<pkt->side_data_elems; i++) {
843  const uint8_t *data = pkt->side_data[i].data;
844  int size = pkt->side_data[i].size;
845  const uint8_t *data_end = data + size;
846 
847  if (is_meta) {
850  if (!size || data[size-1]) {
851  ret = AVERROR(EINVAL);
852  goto fail;
853  }
854  while (data < data_end) {
855  const uint8_t *key = data;
856  const uint8_t *val = data + strlen(key) + 1;
857 
858  if(val >= data_end) {
859  ret = AVERROR(EINVAL);
860  goto fail;
861  }
862  put_str(dyn_bc, key);
863  put_s(dyn_bc, -1);
864  put_str(dyn_bc, val);
865  data = val + strlen(val) + 1;
866  sm_data_count++;
867  }
868  }
869  } else {
870  switch (pkt->side_data[i].type) {
871  case AV_PKT_DATA_PALETTE:
874  default:
875  if (pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
876  put_str(dyn_bc, "Palette");
877  } else if(pkt->side_data[i].type == AV_PKT_DATA_NEW_EXTRADATA) {
878  put_str(dyn_bc, "Extradata");
879  } else if(pkt->side_data[i].type == AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL) {
880  snprintf(tmp, sizeof(tmp), "CodecSpecificSide%"PRId64"", AV_RB64(data));
881  put_str(dyn_bc, tmp);
882  } else {
883  snprintf(tmp, sizeof(tmp), "UserData%s-SD-%d",
884  (s->flags & AVFMT_FLAG_BITEXACT) ? "Lavf" : LIBAVFORMAT_IDENT,
885  pkt->side_data[i].type);
886  put_str(dyn_bc, tmp);
887  }
888  put_s(dyn_bc, -2);
889  put_str(dyn_bc, "bin");
890  ff_put_v(dyn_bc, pkt->side_data[i].size);
891  avio_write(dyn_bc, data, pkt->side_data[i].size);
892  sm_data_count++;
893  break;
895  flags = bytestream_get_le32(&data);
897  put_str(dyn_bc, "Channels");
898  put_s(dyn_bc, bytestream_get_le32(&data));
899  sm_data_count++;
900  }
902  put_str(dyn_bc, "ChannelLayout");
903  put_s(dyn_bc, -2);
904  put_str(dyn_bc, "u64");
905  ff_put_v(bc, 8);
906  avio_write(dyn_bc, data, 8); data+=8;
907  sm_data_count++;
908  }
910  put_str(dyn_bc, "SampleRate");
911  put_s(dyn_bc, bytestream_get_le32(&data));
912  sm_data_count++;
913  }
915  put_str(dyn_bc, "Width");
916  put_s(dyn_bc, bytestream_get_le32(&data));
917  put_str(dyn_bc, "Height");
918  put_s(dyn_bc, bytestream_get_le32(&data));
919  sm_data_count+=2;
920  }
921  break;
923  if (AV_RL32(data)) {
924  put_str(dyn_bc, "SkipStart");
925  put_s(dyn_bc, (unsigned)AV_RL32(data));
926  sm_data_count++;
927  }
928  if (AV_RL32(data+4)) {
929  put_str(dyn_bc, "SkipEnd");
930  put_s(dyn_bc, (unsigned)AV_RL32(data+4));
931  sm_data_count++;
932  }
933  break;
937  // belongs into meta, not side data
938  break;
939  }
940  }
941  }
942 
943 fail:
944  ff_put_v(bc, sm_data_count);
945  dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
946  avio_write(bc, dyn_buf, dyn_size);
947  av_freep(&dyn_buf);
948 
949  return ret;
950 }
951 
953 {
954  NUTContext *nut = s->priv_data;
955  StreamContext *nus = &nut->stream[pkt->stream_index];
956  AVIOContext *bc = s->pb, *dyn_bc, *sm_bc = NULL;
957  FrameCode *fc;
958  int64_t coded_pts;
959  int best_length, frame_code, flags, needed_flags, i, header_idx;
960  int best_header_idx;
961  int key_frame = !!(pkt->flags & AV_PKT_FLAG_KEY);
962  int store_sp = 0;
963  int ret = 0;
964  int sm_size = 0;
965  int data_size = pkt->size;
966  uint8_t *sm_buf = NULL;
967 
968  if (pkt->pts < 0) {
969  av_log(s, AV_LOG_ERROR,
970  "Negative pts not supported stream %d, pts %"PRId64"\n",
971  pkt->stream_index, pkt->pts);
972  if (pkt->pts == AV_NOPTS_VALUE)
973  av_log(s, AV_LOG_ERROR, "Try to enable the genpts flag\n");
974  return AVERROR(EINVAL);
975  }
976 
977  if (pkt->side_data_elems && nut->version > 3) {
978  ret = avio_open_dyn_buf(&sm_bc);
979  if (ret < 0)
980  return ret;
981  ret = write_sm_data(s, sm_bc, pkt, 0);
982  if (ret >= 0)
983  ret = write_sm_data(s, sm_bc, pkt, 1);
984  sm_size = avio_close_dyn_buf(sm_bc, &sm_buf);
985  if (ret < 0)
986  goto fail;
987  data_size += sm_size;
988  }
989 
990  if (1LL << (20 + 3 * nut->header_count) <= avio_tell(bc))
991  write_headers(s, bc);
992 
993  if (key_frame && !(nus->last_flags & FLAG_KEY))
994  store_sp = 1;
995 
996  if (data_size + 30 /*FIXME check*/ + avio_tell(bc) >= nut->last_syncpoint_pos + nut->max_distance)
997  store_sp = 1;
998 
999 //FIXME: Ensure store_sp is 1 in the first place.
1000 
1001  if (store_sp &&
1002  (!(nut->flags & NUT_PIPE) || nut->last_syncpoint_pos == INT_MIN)) {
1003  int64_t sp_pos = INT64_MAX;
1004 
1005  ff_nut_reset_ts(nut, *nus->time_base, pkt->dts);
1006  for (i = 0; i < s->nb_streams; i++) {
1007  AVStream *st = s->streams[i];
1008  int64_t dts_tb = av_rescale_rnd(pkt->dts,
1009  nus->time_base->num * (int64_t)nut->stream[i].time_base->den,
1010  nus->time_base->den * (int64_t)nut->stream[i].time_base->num,
1011  AV_ROUND_DOWN);
1012  int index = av_index_search_timestamp(st, dts_tb,
1014  if (index >= 0) {
1015  sp_pos = FFMIN(sp_pos, st->index_entries[index].pos);
1016  if (!nut->write_index && 2*index > st->nb_index_entries) {
1017  memmove(st->index_entries,
1018  st->index_entries + index,
1019  sizeof(*st->index_entries) * (st->nb_index_entries - index));
1020  st->nb_index_entries -= index;
1021  }
1022  }
1023  }
1024 
1025  nut->last_syncpoint_pos = avio_tell(bc);
1026  ret = avio_open_dyn_buf(&dyn_bc);
1027  if (ret < 0)
1028  goto fail;
1029  put_tt(nut, nus->time_base, dyn_bc, pkt->dts);
1030  ff_put_v(dyn_bc, sp_pos != INT64_MAX ? (nut->last_syncpoint_pos - sp_pos) >> 4 : 0);
1031 
1032  if (nut->flags & NUT_BROADCAST) {
1033  put_tt(nut, nus->time_base, dyn_bc,
1035  }
1036  put_packet(nut, bc, dyn_bc, 1, SYNCPOINT_STARTCODE);
1037 
1038  if (nut->write_index) {
1039  if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, 0 /*unused*/, pkt->dts)) < 0)
1040  goto fail;
1041 
1042  if ((1ll<<60) % nut->sp_count == 0)
1043  for (i=0; i<s->nb_streams; i++) {
1044  int j;
1045  StreamContext *nus = &nut->stream[i];
1046  av_reallocp_array(&nus->keyframe_pts, 2*nut->sp_count, sizeof(*nus->keyframe_pts));
1047  if (!nus->keyframe_pts) {
1048  ret = AVERROR(ENOMEM);
1049  goto fail;
1050  }
1051  for (j=nut->sp_count == 1 ? 0 : nut->sp_count; j<2*nut->sp_count; j++)
1052  nus->keyframe_pts[j] = AV_NOPTS_VALUE;
1053  }
1054  }
1055  }
1057 
1058  coded_pts = pkt->pts & ((1 << nus->msb_pts_shift) - 1);
1059  if (ff_lsb2full(nus, coded_pts) != pkt->pts)
1060  coded_pts = pkt->pts + (1 << nus->msb_pts_shift);
1061 
1062  best_header_idx = find_best_header_idx(nut, pkt);
1063 
1064  best_length = INT_MAX;
1065  frame_code = -1;
1066  for (i = 0; i < 256; i++) {
1067  int length = 0;
1068  FrameCode *fc = &nut->frame_code[i];
1069  int flags = fc->flags;
1070 
1071  if (flags & FLAG_INVALID)
1072  continue;
1073  needed_flags = get_needed_flags(nut, nus, fc, pkt);
1074 
1075  if (flags & FLAG_CODED) {
1076  length++;
1077  flags = needed_flags;
1078  }
1079 
1080  if ((flags & needed_flags) != needed_flags)
1081  continue;
1082 
1083  if ((flags ^ needed_flags) & FLAG_KEY)
1084  continue;
1085 
1086  if (flags & FLAG_STREAM_ID)
1087  length += ff_get_v_length(pkt->stream_index);
1088 
1089  if (data_size % fc->size_mul != fc->size_lsb)
1090  continue;
1091  if (flags & FLAG_SIZE_MSB)
1092  length += ff_get_v_length(data_size / fc->size_mul);
1093 
1094  if (flags & FLAG_CHECKSUM)
1095  length += 4;
1096 
1097  if (flags & FLAG_CODED_PTS)
1098  length += ff_get_v_length(coded_pts);
1099 
1100  if ( (flags & FLAG_CODED)
1101  && nut->header_len[best_header_idx] > nut->header_len[fc->header_idx] + 1) {
1102  flags |= FLAG_HEADER_IDX;
1103  }
1104 
1105  if (flags & FLAG_HEADER_IDX) {
1106  length += 1 - nut->header_len[best_header_idx];
1107  } else {
1108  length -= nut->header_len[fc->header_idx];
1109  }
1110 
1111  length *= 4;
1112  length += !(flags & FLAG_CODED_PTS);
1113  length += !(flags & FLAG_CHECKSUM);
1114 
1115  if (length < best_length) {
1116  best_length = length;
1117  frame_code = i;
1118  }
1119  }
1120  av_assert0(frame_code != -1);
1121 
1122  fc = &nut->frame_code[frame_code];
1123  flags = fc->flags;
1124  needed_flags = get_needed_flags(nut, nus, fc, pkt);
1125  header_idx = fc->header_idx;
1126 
1128  avio_w8(bc, frame_code);
1129  if (flags & FLAG_CODED) {
1130  ff_put_v(bc, (flags ^ needed_flags) & ~(FLAG_CODED));
1131  flags = needed_flags;
1132  }
1133  if (flags & FLAG_STREAM_ID) ff_put_v(bc, pkt->stream_index);
1134  if (flags & FLAG_CODED_PTS) ff_put_v(bc, coded_pts);
1135  if (flags & FLAG_SIZE_MSB ) ff_put_v(bc, data_size / fc->size_mul);
1136  if (flags & FLAG_HEADER_IDX) ff_put_v(bc, header_idx = best_header_idx);
1137 
1138  if (flags & FLAG_CHECKSUM) avio_wl32(bc, ffio_get_checksum(bc));
1139  else ffio_get_checksum(bc);
1140 
1141  if (flags & FLAG_SM_DATA) {
1142  avio_write(bc, sm_buf, sm_size);
1143  }
1144  avio_write(bc, pkt->data + nut->header_len[header_idx], pkt->size - nut->header_len[header_idx]);
1145 
1146  nus->last_flags = flags;
1147  nus->last_pts = pkt->pts;
1148 
1149  //FIXME just store one per syncpoint
1150  if (flags & FLAG_KEY && !(nut->flags & NUT_PIPE)) {
1152  s->streams[pkt->stream_index],
1153  nut->last_syncpoint_pos,
1154  pkt->pts,
1155  0,
1156  0,
1158  if (nus->keyframe_pts && nus->keyframe_pts[nut->sp_count] == AV_NOPTS_VALUE)
1159  nus->keyframe_pts[nut->sp_count] = pkt->pts;
1160  }
1161 
1162  if (!nut->max_pts_tb || av_compare_ts(nut->max_pts, *nut->max_pts_tb, pkt->pts, *nus->time_base) < 0) {
1163  nut->max_pts = pkt->pts;
1164  nut->max_pts_tb = nus->time_base;
1165  }
1166 
1167 fail:
1168  av_freep(&sm_buf);
1169 
1170  return ret;
1171 }
1172 
1174 {
1175  NUTContext *nut = s->priv_data;
1176  AVIOContext *bc = s->pb, *dyn_bc;
1177  int ret;
1178 
1179  while (nut->header_count < 3)
1180  write_headers(s, bc);
1181 
1182  ret = avio_open_dyn_buf(&dyn_bc);
1183  if (ret >= 0 && nut->sp_count) {
1184  av_assert1(nut->write_index);
1185  write_index(nut, dyn_bc);
1186  put_packet(nut, bc, dyn_bc, 1, INDEX_STARTCODE);
1187  }
1188 
1189  return 0;
1190 }
1191 
1193 {
1194  NUTContext *nut = s->priv_data;
1195  int i;
1196 
1197  ff_nut_free_sp(nut);
1198  if (nut->stream)
1199  for (i=0; i<s->nb_streams; i++)
1200  av_freep(&nut->stream[i].keyframe_pts);
1201 
1202  av_freep(&nut->stream);
1203  av_freep(&nut->chapter);
1204  av_freep(&nut->time_base);
1205 }
1206 
1207 #define OFFSET(x) offsetof(NUTContext, x)
1208 #define E AV_OPT_FLAG_ENCODING_PARAM
1209 static const AVOption options[] = {
1210  { "syncpoints", "NUT syncpoint behaviour", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, E, "syncpoints" },
1211  { "default", "", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, E, "syncpoints" },
1212  { "none", "Disable syncpoints, low overhead and unseekable", 0, AV_OPT_TYPE_CONST, {.i64 = NUT_PIPE}, INT_MIN, INT_MAX, E, "syncpoints" },
1213  { "timestamped", "Extend syncpoints with a wallclock timestamp", 0, AV_OPT_TYPE_CONST, {.i64 = NUT_BROADCAST}, INT_MIN, INT_MAX, E, "syncpoints" },
1214  { "write_index", "Write index", OFFSET(write_index), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, E, },
1215  { NULL },
1216 };
1217 
1218 static const AVClass class = {
1219  .class_name = "nutenc",
1220  .item_name = av_default_item_name,
1221  .option = options,
1223 };
1224 
1226  .name = "nut",
1227  .long_name = NULL_IF_CONFIG_SMALL("NUT"),
1228  .mime_type = "video/x-nut",
1229  .extensions = "nut",
1230  .priv_data_size = sizeof(NUTContext),
1231  .audio_codec = CONFIG_LIBVORBIS ? AV_CODEC_ID_VORBIS :
1232  CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_MP2,
1233  .video_codec = AV_CODEC_ID_MPEG4,
1237  .deinit = nut_write_deinit,
1239  .codec_tag = ff_nut_codec_tags,
1240  .priv_class = &class,
1241 };
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1517
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:2746
#define NUT_STABLE_VERSION
Definition: nut.h:40
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2348
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:418
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:1405
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:1168
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:1778
enum AVCodecID id
Definition: mxfenc.c:104
static void nut_write_deinit(AVFormatContext *s)
Definition: nutenc.c:1192
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:4149
int64_t pos
Definition: avformat.h:817
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:949
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1468
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1080
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:248
uint8_t stream_id
Definition: nut.h:67
AVDictionary * metadata
Definition: avformat.h:1281
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1156
mpeg audio layer common tables.
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1596
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1661
const uint8_t * header[128]
Definition: nut.h:98
Format I/O context.
Definition: avformat.h:1314
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:324
uint8_t
AVRational * time_base
Definition: nut.h:107
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:121
static void build_frame_code(AVFormatContext *s)
Definition: nutenc.c:143
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
static const AVOption options[]
Definition: nutenc.c:1209
const uint16_t avpriv_mpa_freq_tab[3]
Definition: mpegaudiodata.c:40
static int64_t last_pts
#define STREAM_STARTCODE
Definition: nut.h:30
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1647
static void put_tt(NUTContext *nut, AVRational *time_base, AVIOContext *bc, uint64_t val)
Definition: nutenc.c:272
#define NUT_PIPE
Definition: nut.h:114
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1382
const AVMetadataConv ff_nut_metadata_conv[]
Definition: nut.c:309
static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: nutenc.c:952
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:1425
uint8_t * data
Definition: avcodec.h:1467
int last_flags
Definition: nut.h:76
#define MAX_DISTANCE
Definition: nut.h:37
uint8_t * data
Definition: avcodec.h:1411
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:442
static const uint8_t header[24]
Definition: sdr2.c:67
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:182
#define AVFMT_FLAG_BITEXACT
When muxing, try to avoid writing any random/volatile data to the output.
Definition: avformat.h:1442
#define av_log(a,...)
AVFormatContext * avf
Definition: nut.h:93
int flag
Definition: checkasm.c:115
int64_t last_pts
Definition: nut.h:78
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1499
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:1225
#define AVINDEX_KEYFRAME
Definition: avformat.h:824
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:1528
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1846
void ff_nut_free_sp(NUTContext *nut)
Definition: nut.c:291
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1877
void ff_parse_specific_params(AVStream *st, int *au_rate, int *au_ssize, int *au_scale)
Definition: riffenc.c:238
#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:1268
#define AVERROR(e)
Definition: error.h:43
static int nut_write_header(AVFormatContext *s)
Definition: nutenc.c:690
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
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define OFFSET(x)
Definition: nutenc.c:1207
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:420
AVChapter ** chapters
Definition: avformat.h:1518
Definition: graph2dot.c:48
static int write_chapter(NUTContext *nut, AVIOContext *bc, int id)
Definition: nutenc.c:550
simple assert() macros that are a bit more flexible than ISO C assert().
enum AVPacketSideDataType type
Definition: avcodec.h:1413
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:1479
GLsizei count
Definition: opengl_enc.c:109
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:80
Definition: nut.h:44
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1473
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
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
static void write_mainheader(NUTContext *nut, AVIOContext *bc)
Definition: nutenc.c:337
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1370
#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:524
static const uint16_t fc[]
Definition: dcaenc.h:41
static int find_expected_header(AVCodecContext *c, int size, int key_frame, uint8_t out[64])
Definition: nutenc.c:38
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:202
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:230
int flags
Definition: nut.h:115
#define FFMIN(a, b)
Definition: common.h:96
#define E
Definition: nutenc.c:1208
uint8_t header_idx
Definition: nut.h:72
This side data contains quality related information from the encoder.
Definition: avcodec.h:1328
int width
picture width / height.
Definition: avcodec.h:1711
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
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:484
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:504
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:241
const char * name
Definition: avformat.h:523
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:1619
ChapterContext * chapter
Definition: nut.h:101
int n
Definition: avisynth_c.h:547
AVDictionary * metadata
Definition: avformat.h:951
#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:106
static int write_globalinfo(NUTContext *nut, AVIOContext *bc)
Definition: nutenc.c:481
#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:1196
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:877
int msb_pts_shift
Definition: nut.h:81
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1280
sample_rate
int frame_size
Definition: mxfenc.c:1821
enum AVMediaType codec_type
Definition: avcodec.h:1540
A list of zero terminated key/value strings.
Definition: avcodec.h:1368
enum AVCodecID codec_id
Definition: avcodec.h:1549
static int intra_only
Definition: ffmpeg_opt.c:117
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:252
int sample_rate
samples per second
Definition: avcodec.h:2287
AVIOContext * pb
I/O context.
Definition: avformat.h:1356
int max_pts_distance
Definition: nut.h:82
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:160
main external API structure.
Definition: avcodec.h:1532
static int write_streaminfo(NUTContext *nut, AVIOContext *bc, int stream_id)
Definition: nutenc.c:508
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1564
static int write_index(NUTContext *nut, AVIOContext *bc)
Definition: nutenc.c:578
Definition: nut.h:54
void * buf
Definition: avisynth_c.h:553
Data found in BlockAdditional element of matroska container.
Definition: avcodec.h:1387
GLint GLenum type
Definition: opengl_enc.c:105
int extradata_size
Definition: avcodec.h:1648
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:1082
static int write_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta)
Definition: nutenc.c:829
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:1173
Recommmends skipping the specified number of samples.
Definition: avcodec.h:1352
AVRational * time_base
Definition: nut.h:88
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:516
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:289
static int write_streamheader(AVFormatContext *avctx, AVIOContext *bc, AVStream *st, int i)
Definition: nutenc.c:417
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
Return audio frame duration.
Definition: utils.c:3024
Round toward -infinity.
Definition: mathematics.h:73
static int find_header_idx(AVFormatContext *s, AVCodecContext *c, int size, int frame_type)
Definition: nutenc.c:105
#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:392
int version
Definition: nut.h:116
static int write_headers(AVFormatContext *avctx, AVIOContext *bc)
Definition: nutenc.c:630
static int flags
Definition: cpu.c:47
int64_t start
Definition: avformat.h:1280
const Dispositions ff_nut_dispositions[]
Definition: nut.c:299
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:1478
static int find_best_header_idx(NUTContext *nut, AVPacket *pkt)
Definition: nutenc.c:810
if(ret< 0)
Definition: vf_mcdeint.c:282
static double c[64]
FrameCode frame_code[256]
Definition: nut.h:96
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:940
int ff_nut_add_sp(NUTContext *nut, int64_t pos, int64_t back_ptr, int64_t ts)
Definition: nut.c:260
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1279
char * key
Definition: dict.h:87
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:281
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:488
#define av_free(p)
char * value
Definition: dict.h:88
static void put_packet(NUTContext *nut, AVIOContext *bc, AVIOContext *dyn_bc, int calculate_checksum, uint64_t startcode)
Definition: nutenc.c:314
void ff_put_v(AVIOContext *bc, uint64_t val)
Put val using a variable number of bytes.
Definition: aviobuf.c:402
int len
int channels
number of audio channels
Definition: avcodec.h:2288
void * priv_data
Format private data.
Definition: avformat.h:1342
static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc, AVPacket *pkt)
Definition: nutenc.c:782
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:497
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1466
#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:473
#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:72
const uint16_t avpriv_mpa_bitrate_tab[2][3][15]
Definition: mpegaudiodata.c:30
int stream_index
Definition: avcodec.h:1469
int dummy
Definition: motion-test.c:64
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1096
const AVCodecTag *const ff_nut_codec_tags[]
Definition: nut.c:225
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1444
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:1460
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