FFmpeg  2.1.1
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 "libavutil/intreadwrite.h"
23 #include "libavutil/mathematics.h"
24 #include "libavutil/tree.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/avassert.h"
28 #include "nut.h"
29 #include "internal.h"
30 #include "avio_internal.h"
31 #include "riff.h"
32 
33 static int find_expected_header(AVCodecContext *c, int size, int key_frame,
34  uint8_t out[64])
35 {
36  int sample_rate = c->sample_rate;
37 
38  if (size > 4096)
39  return 0;
40 
41  AV_WB24(out, 1);
42 
43  if (c->codec_id == AV_CODEC_ID_MPEG4) {
44  if (key_frame) {
45  return 3;
46  } else {
47  out[3] = 0xB6;
48  return 4;
49  }
50  } else if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
52  return 3;
53  } else if (c->codec_id == AV_CODEC_ID_H264) {
54  return 3;
55  } else if (c->codec_id == AV_CODEC_ID_MP3 ||
56  c->codec_id == AV_CODEC_ID_MP2) {
57  int lsf, mpeg25, sample_rate_index, bitrate_index, frame_size;
58  int layer = c->codec_id == AV_CODEC_ID_MP3 ? 3 : 2;
59  unsigned int header = 0xFFF00000;
60 
61  lsf = sample_rate < (24000 + 32000) / 2;
62  mpeg25 = sample_rate < (12000 + 16000) / 2;
63  sample_rate <<= lsf + mpeg25;
64  if (sample_rate < (32000 + 44100) / 2) sample_rate_index = 2;
65  else if (sample_rate < (44100 + 48000) / 2) sample_rate_index = 0;
66  else sample_rate_index = 1;
67 
68  sample_rate = avpriv_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25);
69 
70  for (bitrate_index = 2; bitrate_index < 30; bitrate_index++) {
71  frame_size =
72  avpriv_mpa_bitrate_tab[lsf][layer - 1][bitrate_index >> 1];
73  frame_size = (frame_size * 144000) / (sample_rate << lsf) +
74  (bitrate_index & 1);
75 
76  if (frame_size == size)
77  break;
78  }
79 
80  header |= (!lsf) << 19;
81  header |= (4 - layer) << 17;
82  header |= 1 << 16; //no crc
83  AV_WB32(out, header);
84  if (size <= 0)
85  return 2; //we guess there is no crc, if there is one the user clearly does not care about overhead
86  if (bitrate_index == 30)
87  return -1; //something is wrong ...
88 
89  header |= (bitrate_index >> 1) << 12;
90  header |= sample_rate_index << 10;
91  header |= (bitrate_index & 1) << 9;
92 
93  return 2; //FIXME actually put the needed ones in build_elision_headers()
94  //return 3; //we guess that the private bit is not set
95 //FIXME the above assumptions should be checked, if these turn out false too often something should be done
96  }
97  return 0;
98 }
99 
100 static int find_header_idx(AVFormatContext *s, AVCodecContext *c, int size, int frame_type)
101 {
102  NUTContext *nut = s->priv_data;
103  uint8_t out[64];
104  int i;
105  int len = find_expected_header(c, size, frame_type, out);
106 
107  for (i = 1; i < nut->header_count; i++) {
108  if (len == nut->header_len[i] && !memcmp(out, nut->header[i], len)) {
109  return i;
110  }
111  }
112 
113  return 0;
114 }
115 
117 {
118  NUTContext *nut = s->priv_data;
119  int i;
120  //FIXME this is lame
121  //FIXME write a 2pass mode to find the maximal headers
122  static const uint8_t headers[][5] = {
123  { 3, 0x00, 0x00, 0x01 },
124  { 4, 0x00, 0x00, 0x01, 0xB6},
125  { 2, 0xFF, 0xFA }, //mp3+crc
126  { 2, 0xFF, 0xFB }, //mp3
127  { 2, 0xFF, 0xFC }, //mp2+crc
128  { 2, 0xFF, 0xFD }, //mp2
129  };
130 
131  nut->header_count = 7;
132  for (i = 1; i < nut->header_count; i++) {
133  nut->header_len[i] = headers[i - 1][0];
134  nut->header[i] = &headers[i - 1][1];
135  }
136 }
137 
139 {
140  NUTContext *nut = s->priv_data;
141  int key_frame, index, pred, stream_id;
142  int start = 1;
143  int end = 254;
144  int keyframe_0_esc = s->nb_streams > 2;
145  int pred_table[10];
146  FrameCode *ft;
147 
148  ft = &nut->frame_code[start];
149  ft->flags = FLAG_CODED;
150  ft->size_mul = 1;
151  ft->pts_delta = 1;
152  start++;
153 
154  if (keyframe_0_esc) {
155  /* keyframe = 0 escape */
156  FrameCode *ft = &nut->frame_code[start];
158  ft->size_mul = 1;
159  start++;
160  }
161 
162  for (stream_id = 0; stream_id < s->nb_streams; stream_id++) {
163  int start2 = start + (end - start) * stream_id / s->nb_streams;
164  int end2 = start + (end - start) * (stream_id + 1) / s->nb_streams;
165  AVCodecContext *codec = s->streams[stream_id]->codec;
166  int is_audio = codec->codec_type == AVMEDIA_TYPE_AUDIO;
167  int intra_only = /*codec->intra_only || */ is_audio;
168  int pred_count;
169  int frame_size = 0;
170 
171  if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
173  if (codec->codec_id == AV_CODEC_ID_VORBIS && !frame_size)
174  frame_size = 64;
175  } else {
176  AVRational f = av_div_q(codec->time_base, *nut->stream[stream_id].time_base);
177  if (f.den == 1 && f.num>0)
178  frame_size = f.num;
179  }
180  if (!frame_size)
181  frame_size = 1;
182 
183  for (key_frame = 0; key_frame < 2; key_frame++) {
184  if (!intra_only || !keyframe_0_esc || key_frame != 0) {
185  FrameCode *ft = &nut->frame_code[start2];
186  ft->flags = FLAG_KEY * key_frame;
188  ft->stream_id = stream_id;
189  ft->size_mul = 1;
190  if (is_audio)
191  ft->header_idx = find_header_idx(s, codec, -1, key_frame);
192  start2++;
193  }
194  }
195 
196  key_frame = intra_only;
197 #if 1
198  if (is_audio) {
199  int frame_bytes = codec->frame_size * (int64_t)codec->bit_rate /
200  (8 * codec->sample_rate);
201  int pts;
202  for (pts = 0; pts < 2; pts++) {
203  for (pred = 0; pred < 2; pred++) {
204  FrameCode *ft = &nut->frame_code[start2];
205  ft->flags = FLAG_KEY * key_frame;
206  ft->stream_id = stream_id;
207  ft->size_mul = frame_bytes + 2;
208  ft->size_lsb = frame_bytes + pred;
209  ft->pts_delta = pts * frame_size;
210  ft->header_idx = find_header_idx(s, codec, frame_bytes + pred, key_frame);
211  start2++;
212  }
213  }
214  } else {
215  FrameCode *ft = &nut->frame_code[start2];
216  ft->flags = FLAG_KEY | FLAG_SIZE_MSB;
217  ft->stream_id = stream_id;
218  ft->size_mul = 1;
219  ft->pts_delta = frame_size;
220  start2++;
221  }
222 #endif
223 
224  if (codec->has_b_frames) {
225  pred_count = 5;
226  pred_table[0] = -2;
227  pred_table[1] = -1;
228  pred_table[2] = 1;
229  pred_table[3] = 3;
230  pred_table[4] = 4;
231  } else if (codec->codec_id == AV_CODEC_ID_VORBIS) {
232  pred_count = 3;
233  pred_table[0] = 2;
234  pred_table[1] = 9;
235  pred_table[2] = 16;
236  } else {
237  pred_count = 1;
238  pred_table[0] = 1;
239  }
240 
241  for (pred = 0; pred < pred_count; pred++) {
242  int start3 = start2 + (end2 - start2) * pred / pred_count;
243  int end3 = start2 + (end2 - start2) * (pred + 1) / pred_count;
244 
245  pred_table[pred] *= frame_size;
246 
247  for (index = start3; index < end3; index++) {
248  FrameCode *ft = &nut->frame_code[index];
249  ft->flags = FLAG_KEY * key_frame;
250  ft->flags |= FLAG_SIZE_MSB;
251  ft->stream_id = stream_id;
252 //FIXME use single byte size and pred from last
253  ft->size_mul = end3 - start3;
254  ft->size_lsb = index - start3;
255  ft->pts_delta = pred_table[pred];
256  if (is_audio)
257  ft->header_idx = find_header_idx(s, codec, -1, key_frame);
258  }
259  }
260  }
261  memmove(&nut->frame_code['N' + 1], &nut->frame_code['N'], sizeof(FrameCode) * (255 - 'N'));
262  nut->frame_code[0].flags =
263  nut->frame_code[255].flags =
264  nut->frame_code['N'].flags = FLAG_INVALID;
265 }
266 
267 static void put_tt(NUTContext *nut, AVRational *time_base, AVIOContext *bc, uint64_t val)
268 {
269  val *= nut->time_base_count;
270  val += time_base - nut->time_base;
271  ff_put_v(bc, val);
272 }
273 /**
274  * Store a string as vb.
275  */
276 static void put_str(AVIOContext *bc, const char *string)
277 {
278  int len = strlen(string);
279 
280  ff_put_v(bc, len);
281  avio_write(bc, string, len);
282 }
283 
284 static void put_s(AVIOContext *bc, int64_t val)
285 {
286  ff_put_v(bc, 2 * FFABS(val) - (val > 0));
287 }
288 
289 #ifdef TRACE
290 static inline void ff_put_v_trace(AVIOContext *bc, uint64_t v, const char *file,
291  const char *func, int line)
292 {
293  av_log(NULL, AV_LOG_DEBUG, "ff_put_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
294 
295  ff_put_v(bc, v);
296 }
297 
298 static inline void put_s_trace(AVIOContext *bc, int64_t v, const char *file, const char *func, int line)
299 {
300  av_log(NULL, AV_LOG_DEBUG, "put_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
301 
302  put_s(bc, v);
303 }
304 #define ff_put_v(bc, v) ff_put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
305 #define put_s(bc, v) put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
306 #endif
307 
308 //FIXME remove calculate_checksum
309 static void put_packet(NUTContext *nut, AVIOContext *bc, AVIOContext *dyn_bc,
310  int calculate_checksum, uint64_t startcode)
311 {
312  uint8_t *dyn_buf = NULL;
313  int dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
314  int forw_ptr = dyn_size + 4 * calculate_checksum;
315 
316  if (forw_ptr > 4096)
318  avio_wb64(bc, startcode);
319  ff_put_v(bc, forw_ptr);
320  if (forw_ptr > 4096)
321  avio_wl32(bc, ffio_get_checksum(bc));
322 
323  if (calculate_checksum)
325  avio_write(bc, dyn_buf, dyn_size);
326  if (calculate_checksum)
327  avio_wl32(bc, ffio_get_checksum(bc));
328 
329  av_free(dyn_buf);
330 }
331 
333 {
334  int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields,
335  tmp_head_idx;
336  int64_t tmp_match;
337 
338  ff_put_v(bc, NUT_VERSION);
339  ff_put_v(bc, nut->avf->nb_streams);
340  ff_put_v(bc, nut->max_distance);
341  ff_put_v(bc, nut->time_base_count);
342 
343  for (i = 0; i < nut->time_base_count; i++) {
344  ff_put_v(bc, nut->time_base[i].num);
345  ff_put_v(bc, nut->time_base[i].den);
346  }
347 
348  tmp_pts = 0;
349  tmp_mul = 1;
350  tmp_stream = 0;
351  tmp_match = 1 - (1LL << 62);
352  tmp_head_idx = 0;
353  for (i = 0; i < 256; ) {
354  tmp_fields = 0;
355  tmp_size = 0;
356 // tmp_res=0;
357  if (tmp_pts != nut->frame_code[i].pts_delta ) tmp_fields = 1;
358  if (tmp_mul != nut->frame_code[i].size_mul ) tmp_fields = 2;
359  if (tmp_stream != nut->frame_code[i].stream_id ) tmp_fields = 3;
360  if (tmp_size != nut->frame_code[i].size_lsb ) tmp_fields = 4;
361 // if (tmp_res != nut->frame_code[i].res ) tmp_fields=5;
362  if (tmp_head_idx != nut->frame_code[i].header_idx) tmp_fields = 8;
363 
364  tmp_pts = nut->frame_code[i].pts_delta;
365  tmp_flags = nut->frame_code[i].flags;
366  tmp_stream = nut->frame_code[i].stream_id;
367  tmp_mul = nut->frame_code[i].size_mul;
368  tmp_size = nut->frame_code[i].size_lsb;
369 // tmp_res = nut->frame_code[i].res;
370  tmp_head_idx = nut->frame_code[i].header_idx;
371 
372  for (j = 0; i < 256; j++, i++) {
373  if (i == 'N') {
374  j--;
375  continue;
376  }
377  if (nut->frame_code[i].pts_delta != tmp_pts ||
378  nut->frame_code[i].flags != tmp_flags ||
379  nut->frame_code[i].stream_id != tmp_stream ||
380  nut->frame_code[i].size_mul != tmp_mul ||
381  nut->frame_code[i].size_lsb != tmp_size + j ||
382 // nut->frame_code[i].res != tmp_res ||
383  nut->frame_code[i].header_idx != tmp_head_idx)
384  break;
385  }
386  if (j != tmp_mul - tmp_size)
387  tmp_fields = 6;
388 
389  ff_put_v(bc, tmp_flags);
390  ff_put_v(bc, tmp_fields);
391  if (tmp_fields > 0) put_s(bc, tmp_pts);
392  if (tmp_fields > 1) ff_put_v(bc, tmp_mul);
393  if (tmp_fields > 2) ff_put_v(bc, tmp_stream);
394  if (tmp_fields > 3) ff_put_v(bc, tmp_size);
395  if (tmp_fields > 4) ff_put_v(bc, 0 /*tmp_res*/);
396  if (tmp_fields > 5) ff_put_v(bc, j);
397  if (tmp_fields > 6) ff_put_v(bc, tmp_match);
398  if (tmp_fields > 7) ff_put_v(bc, tmp_head_idx);
399  }
400  ff_put_v(bc, nut->header_count - 1);
401  for (i = 1; i < nut->header_count; i++) {
402  ff_put_v(bc, nut->header_len[i]);
403  avio_write(bc, nut->header[i], nut->header_len[i]);
404  }
405 }
406 
408  AVStream *st, int i)
409 {
410  NUTContext *nut = avctx->priv_data;
411  AVCodecContext *codec = st->codec;
412 
413  ff_put_v(bc, i);
414  switch (codec->codec_type) {
415  case AVMEDIA_TYPE_VIDEO: ff_put_v(bc, 0); break;
416  case AVMEDIA_TYPE_AUDIO: ff_put_v(bc, 1); break;
417  case AVMEDIA_TYPE_SUBTITLE: ff_put_v(bc, 2); break;
418  default: ff_put_v(bc, 3); break;
419  }
420  ff_put_v(bc, 4);
421  if (codec->codec_tag) {
422  avio_wl32(bc, codec->codec_tag);
423  } else {
424  av_log(avctx, AV_LOG_ERROR, "No codec tag defined for stream %d\n", i);
425  return AVERROR(EINVAL);
426  }
427 
428  ff_put_v(bc, nut->stream[i].time_base - nut->time_base);
429  ff_put_v(bc, nut->stream[i].msb_pts_shift);
430  ff_put_v(bc, nut->stream[i].max_pts_distance);
431  ff_put_v(bc, codec->has_b_frames);
432  avio_w8(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
433 
434  ff_put_v(bc, codec->extradata_size);
435  avio_write(bc, codec->extradata, codec->extradata_size);
436 
437  switch (codec->codec_type) {
438  case AVMEDIA_TYPE_AUDIO:
439  ff_put_v(bc, codec->sample_rate);
440  ff_put_v(bc, 1);
441  ff_put_v(bc, codec->channels);
442  break;
443  case AVMEDIA_TYPE_VIDEO:
444  ff_put_v(bc, codec->width);
445  ff_put_v(bc, codec->height);
446 
447  if (st->sample_aspect_ratio.num <= 0 ||
448  st->sample_aspect_ratio.den <= 0) {
449  ff_put_v(bc, 0);
450  ff_put_v(bc, 0);
451  } else {
454  }
455  ff_put_v(bc, 0); /* csp type -- unknown */
456  break;
457  default:
458  break;
459  }
460  return 0;
461 }
462 
463 static int add_info(AVIOContext *bc, const char *type, const char *value)
464 {
465  put_str(bc, type);
466  put_s(bc, -1);
467  put_str(bc, value);
468  return 1;
469 }
470 
472 {
473  AVFormatContext *s = nut->avf;
474  AVDictionaryEntry *t = NULL;
475  AVIOContext *dyn_bc;
476  uint8_t *dyn_buf = NULL;
477  int count = 0, dyn_size;
478  int ret = avio_open_dyn_buf(&dyn_bc);
479  if (ret < 0)
480  return ret;
481 
482  while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
483  count += add_info(dyn_bc, t->key, t->value);
484 
485  ff_put_v(bc, 0); //stream_if_plus1
486  ff_put_v(bc, 0); //chapter_id
487  ff_put_v(bc, 0); //timestamp_start
488  ff_put_v(bc, 0); //length
489 
490  ff_put_v(bc, count);
491 
492  dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
493  avio_write(bc, dyn_buf, dyn_size);
494  av_free(dyn_buf);
495  return 0;
496 }
497 
498 static int write_streaminfo(NUTContext *nut, AVIOContext *bc, int stream_id) {
499  AVFormatContext *s= nut->avf;
500  AVStream* st = s->streams[stream_id];
501  AVDictionaryEntry *t = NULL;
502  AVIOContext *dyn_bc;
503  uint8_t *dyn_buf=NULL;
504  int count=0, dyn_size, i;
505  int ret = avio_open_dyn_buf(&dyn_bc);
506  if (ret < 0)
507  return ret;
508 
509  while ((t = av_dict_get(st->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
510  count += add_info(dyn_bc, t->key, t->value);
511  for (i=0; ff_nut_dispositions[i].flag; ++i) {
512  if (st->disposition & ff_nut_dispositions[i].flag)
513  count += add_info(dyn_bc, "Disposition", ff_nut_dispositions[i].str);
514  }
515  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
516  uint8_t buf[256];
517  snprintf(buf, sizeof(buf), "%d/%d", st->codec->time_base.den, st->codec->time_base.num);
518  count += add_info(dyn_bc, "r_frame_rate", buf);
519  }
520  dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
521 
522  if (count) {
523  ff_put_v(bc, stream_id + 1); //stream_id_plus1
524  ff_put_v(bc, 0); //chapter_id
525  ff_put_v(bc, 0); //timestamp_start
526  ff_put_v(bc, 0); //length
527 
528  ff_put_v(bc, count);
529 
530  avio_write(bc, dyn_buf, dyn_size);
531  }
532 
533  av_free(dyn_buf);
534  return count;
535 }
536 
537 static int write_chapter(NUTContext *nut, AVIOContext *bc, int id)
538 {
539  AVIOContext *dyn_bc;
540  uint8_t *dyn_buf = NULL;
541  AVDictionaryEntry *t = NULL;
542  AVChapter *ch = nut->avf->chapters[id];
543  int ret, dyn_size, count = 0;
544 
545  ret = avio_open_dyn_buf(&dyn_bc);
546  if (ret < 0)
547  return ret;
548 
549  ff_put_v(bc, 0); // stream_id_plus1
550  put_s(bc, id + 1); // chapter_id
551  put_tt(nut, nut->chapter[id].time_base, bc, ch->start); // chapter_start
552  ff_put_v(bc, ch->end - ch->start); // chapter_len
553 
554  while ((t = av_dict_get(ch->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
555  count += add_info(dyn_bc, t->key, t->value);
556 
557  ff_put_v(bc, count);
558 
559  dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
560  avio_write(bc, dyn_buf, dyn_size);
561  av_freep(&dyn_buf);
562  return 0;
563 }
564 
565 static int write_index(NUTContext *nut, AVIOContext *bc) {
566  int i;
567  Syncpoint dummy= { .pos= 0 };
568  Syncpoint *next_node[2] = { NULL };
569  int64_t startpos = avio_tell(bc);
570  int64_t payload_size;
571 
572  put_tt(nut, nut->max_pts_tb, bc, nut->max_pts);
573 
574  ff_put_v(bc, nut->sp_count);
575 
576  for (i=0; i<nut->sp_count; i++) {
577  av_tree_find(nut->syncpoints, &dummy, (void *) ff_nut_sp_pos_cmp, (void**)next_node);
578  ff_put_v(bc, (next_node[1]->pos >> 4) - (dummy.pos>>4));
579  dummy.pos = next_node[1]->pos;
580  }
581 
582  for (i=0; i<nut->avf->nb_streams; i++) {
583  StreamContext *nus= &nut->stream[i];
584  int64_t last_pts= -1;
585  int j, k;
586  for (j=0; j<nut->sp_count; j++) {
587  int flag = (nus->keyframe_pts[j] != AV_NOPTS_VALUE) ^ (j+1 == nut->sp_count);
588  int n = 0;
589  for (; j<nut->sp_count && (nus->keyframe_pts[j] != AV_NOPTS_VALUE) == flag; j++)
590  n++;
591 
592  ff_put_v(bc, 1 + 2*flag + 4*n);
593  for (k= j - n; k<=j && k<nut->sp_count; k++) {
594  if (nus->keyframe_pts[k] == AV_NOPTS_VALUE)
595  continue;
596  av_assert0(nus->keyframe_pts[k] > last_pts);
597  ff_put_v(bc, nus->keyframe_pts[k] - last_pts);
598  last_pts = nus->keyframe_pts[k];
599  }
600  }
601  }
602 
603  payload_size = avio_tell(bc) - startpos + 8 + 4;
604 
605  avio_wb64(bc, 8 + payload_size + av_log2(payload_size) / 7 + 1 + 4*(payload_size > 4096));
606 
607  return 0;
608 }
609 
611 {
612  NUTContext *nut = avctx->priv_data;
613  AVIOContext *dyn_bc;
614  int i, ret;
615 
617 
618  ret = avio_open_dyn_buf(&dyn_bc);
619  if (ret < 0)
620  return ret;
621  write_mainheader(nut, dyn_bc);
622  put_packet(nut, bc, dyn_bc, 1, MAIN_STARTCODE);
623 
624  for (i = 0; i < nut->avf->nb_streams; i++) {
625  ret = avio_open_dyn_buf(&dyn_bc);
626  if (ret < 0)
627  return ret;
628  ret = write_streamheader(avctx, dyn_bc, nut->avf->streams[i], i);
629  if (ret < 0)
630  return ret;
631  put_packet(nut, bc, dyn_bc, 1, STREAM_STARTCODE);
632  }
633 
634  ret = avio_open_dyn_buf(&dyn_bc);
635  if (ret < 0)
636  return ret;
637  write_globalinfo(nut, dyn_bc);
638  put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
639 
640  for (i = 0; i < nut->avf->nb_streams; i++) {
641  ret = avio_open_dyn_buf(&dyn_bc);
642  if (ret < 0)
643  return ret;
644  ret = write_streaminfo(nut, dyn_bc, i);
645  if (ret < 0)
646  return ret;
647  if (ret > 0)
648  put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
649  else {
650  uint8_t *buf;
651  avio_close_dyn_buf(dyn_bc, &buf);
652  av_free(buf);
653  }
654  }
655 
656  for (i = 0; i < nut->avf->nb_chapters; i++) {
657  ret = avio_open_dyn_buf(&dyn_bc);
658  if (ret < 0)
659  return ret;
660  ret = write_chapter(nut, dyn_bc, i);
661  if (ret < 0) {
662  uint8_t *buf;
663  avio_close_dyn_buf(dyn_bc, &buf);
664  av_freep(&buf);
665  return ret;
666  }
667  put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
668  }
669 
670  nut->last_syncpoint_pos = INT_MIN;
671  nut->header_count++;
672  return 0;
673 }
674 
676 {
677  NUTContext *nut = s->priv_data;
678  AVIOContext *bc = s->pb;
679  int i, j, ret;
680 
681  nut->avf = s;
682 
683  nut->stream = av_calloc(s->nb_streams, sizeof(*nut->stream ));
684  nut->chapter = av_calloc(s->nb_chapters, sizeof(*nut->chapter));
685  nut->time_base= av_calloc(s->nb_streams +
686  s->nb_chapters, sizeof(*nut->time_base));
687  if (!nut->stream || !nut->chapter || !nut->time_base) {
688  av_freep(&nut->stream);
689  av_freep(&nut->chapter);
690  av_freep(&nut->time_base);
691  return AVERROR(ENOMEM);
692  }
693 
694  for (i = 0; i < s->nb_streams; i++) {
695  AVStream *st = s->streams[i];
696  int ssize;
697  AVRational time_base;
698  ff_parse_specific_params(st->codec, &time_base.den, &ssize, &time_base.num);
699 
700  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->codec->sample_rate) {
701  time_base = (AVRational) {1, st->codec->sample_rate};
702  } else {
703  time_base = ff_choose_timebase(s, st, 48000);
704  }
705 
706  avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
707 
708  for (j = 0; j < nut->time_base_count; j++)
709  if (!memcmp(&time_base, &nut->time_base[j], sizeof(AVRational))) {
710  break;
711  }
712  nut->time_base[j] = time_base;
713  nut->stream[i].time_base = &nut->time_base[j];
714  if (j == nut->time_base_count)
715  nut->time_base_count++;
716 
717  if (INT64_C(1000) * time_base.num >= time_base.den)
718  nut->stream[i].msb_pts_shift = 7;
719  else
720  nut->stream[i].msb_pts_shift = 14;
721  nut->stream[i].max_pts_distance =
722  FFMAX(time_base.den, time_base.num) / time_base.num;
723  }
724 
725  for (i = 0; i < s->nb_chapters; i++) {
726  AVChapter *ch = s->chapters[i];
727 
728  for (j = 0; j < nut->time_base_count; j++)
729  if (!memcmp(&ch->time_base, &nut->time_base[j], sizeof(AVRational)))
730  break;
731 
732  nut->time_base[j] = ch->time_base;
733  nut->chapter[i].time_base = &nut->time_base[j];
734  if (j == nut->time_base_count)
735  nut->time_base_count++;
736  }
737 
738  nut->max_distance = MAX_DISTANCE;
740  build_frame_code(s);
741  av_assert0(nut->frame_code['N'].flags == FLAG_INVALID);
742 
743  avio_write(bc, ID_STRING, strlen(ID_STRING));
744  avio_w8(bc, 0);
745 
746  if ((ret = write_headers(s, bc)) < 0)
747  return ret;
748 
749  if (s->avoid_negative_ts < 0)
750  s->avoid_negative_ts = 1;
751 
752  avio_flush(bc);
753 
754  return 0;
755 }
756 
758  AVPacket *pkt)
759 {
760  int flags = 0;
761 
762  if (pkt->flags & AV_PKT_FLAG_KEY)
763  flags |= FLAG_KEY;
764  if (pkt->stream_index != fc->stream_id)
765  flags |= FLAG_STREAM_ID;
766  if (pkt->size / fc->size_mul)
767  flags |= FLAG_SIZE_MSB;
768  if (pkt->pts - nus->last_pts != fc->pts_delta)
769  flags |= FLAG_CODED_PTS;
770  if (pkt->size > 2 * nut->max_distance)
771  flags |= FLAG_CHECKSUM;
772  if (FFABS(pkt->pts - nus->last_pts) > nus->max_pts_distance)
773  flags |= FLAG_CHECKSUM;
774  if (pkt->size < nut->header_len[fc->header_idx] ||
775  (pkt->size > 4096 && fc->header_idx) ||
776  memcmp(pkt->data, nut->header[fc->header_idx],
777  nut->header_len[fc->header_idx]))
778  flags |= FLAG_HEADER_IDX;
779 
780  return flags | (fc->flags & FLAG_CODED);
781 }
782 
784 {
785  int i;
786  int best_i = 0;
787  int best_len = 0;
788 
789  if (pkt->size > 4096)
790  return 0;
791 
792  for (i = 1; i < nut->header_count; i++)
793  if (pkt->size >= nut->header_len[i]
794  && nut->header_len[i] > best_len
795  && !memcmp(pkt->data, nut->header[i], nut->header_len[i])) {
796  best_i = i;
797  best_len = nut->header_len[i];
798  }
799  return best_i;
800 }
801 
803 {
804  NUTContext *nut = s->priv_data;
805  StreamContext *nus = &nut->stream[pkt->stream_index];
806  AVIOContext *bc = s->pb, *dyn_bc;
807  FrameCode *fc;
808  int64_t coded_pts;
809  int best_length, frame_code, flags, needed_flags, i, header_idx;
810  int best_header_idx;
811  int key_frame = !!(pkt->flags & AV_PKT_FLAG_KEY);
812  int store_sp = 0;
813  int ret;
814 
815  if (pkt->pts < 0) {
816  av_log(s, AV_LOG_ERROR,
817  "Negative pts not supported stream %d, pts %"PRId64"\n",
818  pkt->stream_index, pkt->pts);
819  return AVERROR(EINVAL);
820  }
821 
822  if (1LL << (20 + 3 * nut->header_count) <= avio_tell(bc))
823  write_headers(s, bc);
824 
825  if (key_frame && !(nus->last_flags & FLAG_KEY))
826  store_sp = 1;
827 
828  if (pkt->size + 30 /*FIXME check*/ + avio_tell(bc) >= nut->last_syncpoint_pos + nut->max_distance)
829  store_sp = 1;
830 
831 //FIXME: Ensure store_sp is 1 in the first place.
832 
833  if (store_sp) {
834  Syncpoint *sp, dummy = { .pos = INT64_MAX };
835 
836  ff_nut_reset_ts(nut, *nus->time_base, pkt->dts);
837  for (i = 0; i < s->nb_streams; i++) {
838  AVStream *st = s->streams[i];
839  int64_t dts_tb = av_rescale_rnd(pkt->dts,
840  nus->time_base->num * (int64_t)nut->stream[i].time_base->den,
841  nus->time_base->den * (int64_t)nut->stream[i].time_base->num,
842  AV_ROUND_DOWN);
843  int index = av_index_search_timestamp(st, dts_tb,
845  if (index >= 0)
846  dummy.pos = FFMIN(dummy.pos, st->index_entries[index].pos);
847  }
848  if (dummy.pos == INT64_MAX)
849  dummy.pos = 0;
850  sp = av_tree_find(nut->syncpoints, &dummy, (void *)ff_nut_sp_pos_cmp,
851  NULL);
852 
853  nut->last_syncpoint_pos = avio_tell(bc);
854  ret = avio_open_dyn_buf(&dyn_bc);
855  if (ret < 0)
856  return ret;
857  put_tt(nut, nus->time_base, dyn_bc, pkt->dts);
858  ff_put_v(dyn_bc, sp ? (nut->last_syncpoint_pos - sp->pos) >> 4 : 0);
859  put_packet(nut, bc, dyn_bc, 1, SYNCPOINT_STARTCODE);
860 
861  if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, 0 /*unused*/, pkt->dts)) < 0)
862  return ret;
863 
864  if ((1ll<<60) % nut->sp_count == 0)
865  for (i=0; i<s->nb_streams; i++) {
866  int j;
867  StreamContext *nus = &nut->stream[i];
868  av_reallocp_array(&nus->keyframe_pts, 2*nut->sp_count, sizeof(*nus->keyframe_pts));
869  if (!nus->keyframe_pts)
870  return AVERROR(ENOMEM);
871  for (j=nut->sp_count == 1 ? 0 : nut->sp_count; j<2*nut->sp_count; j++)
872  nus->keyframe_pts[j] = AV_NOPTS_VALUE;
873  }
874  }
876 
877  coded_pts = pkt->pts & ((1 << nus->msb_pts_shift) - 1);
878  if (ff_lsb2full(nus, coded_pts) != pkt->pts)
879  coded_pts = pkt->pts + (1 << nus->msb_pts_shift);
880 
881  best_header_idx = find_best_header_idx(nut, pkt);
882 
883  best_length = INT_MAX;
884  frame_code = -1;
885  for (i = 0; i < 256; i++) {
886  int length = 0;
887  FrameCode *fc = &nut->frame_code[i];
888  int flags = fc->flags;
889 
890  if (flags & FLAG_INVALID)
891  continue;
892  needed_flags = get_needed_flags(nut, nus, fc, pkt);
893 
894  if (flags & FLAG_CODED) {
895  length++;
896  flags = needed_flags;
897  }
898 
899  if ((flags & needed_flags) != needed_flags)
900  continue;
901 
902  if ((flags ^ needed_flags) & FLAG_KEY)
903  continue;
904 
905  if (flags & FLAG_STREAM_ID)
906  length += ff_get_v_length(pkt->stream_index);
907 
908  if (pkt->size % fc->size_mul != fc->size_lsb)
909  continue;
910  if (flags & FLAG_SIZE_MSB)
911  length += ff_get_v_length(pkt->size / fc->size_mul);
912 
913  if (flags & FLAG_CHECKSUM)
914  length += 4;
915 
916  if (flags & FLAG_CODED_PTS)
917  length += ff_get_v_length(coded_pts);
918 
919  if ( (flags & FLAG_CODED)
920  && nut->header_len[best_header_idx] > nut->header_len[fc->header_idx] + 1) {
921  flags |= FLAG_HEADER_IDX;
922  }
923 
924  if (flags & FLAG_HEADER_IDX) {
925  length += 1 - nut->header_len[best_header_idx];
926  } else {
927  length -= nut->header_len[fc->header_idx];
928  }
929 
930  length *= 4;
931  length += !(flags & FLAG_CODED_PTS);
932  length += !(flags & FLAG_CHECKSUM);
933 
934  if (length < best_length) {
935  best_length = length;
936  frame_code = i;
937  }
938  }
939  av_assert0(frame_code != -1);
940  fc = &nut->frame_code[frame_code];
941  flags = fc->flags;
942  needed_flags = get_needed_flags(nut, nus, fc, pkt);
943  header_idx = fc->header_idx;
944 
946  avio_w8(bc, frame_code);
947  if (flags & FLAG_CODED) {
948  ff_put_v(bc, (flags ^ needed_flags) & ~(FLAG_CODED));
949  flags = needed_flags;
950  }
951  if (flags & FLAG_STREAM_ID) ff_put_v(bc, pkt->stream_index);
952  if (flags & FLAG_CODED_PTS) ff_put_v(bc, coded_pts);
953  if (flags & FLAG_SIZE_MSB ) ff_put_v(bc, pkt->size / fc->size_mul);
954  if (flags & FLAG_HEADER_IDX) ff_put_v(bc, header_idx = best_header_idx);
955 
956  if (flags & FLAG_CHECKSUM) avio_wl32(bc, ffio_get_checksum(bc));
957  else ffio_get_checksum(bc);
958 
959  avio_write(bc, pkt->data + nut->header_len[header_idx], pkt->size - nut->header_len[header_idx]);
960  nus->last_flags = flags;
961  nus->last_pts = pkt->pts;
962 
963  //FIXME just store one per syncpoint
964  if (flags & FLAG_KEY) {
966  s->streams[pkt->stream_index],
967  nut->last_syncpoint_pos,
968  pkt->pts,
969  0,
970  0,
972  if (nus->keyframe_pts && nus->keyframe_pts[nut->sp_count] == AV_NOPTS_VALUE)
973  nus->keyframe_pts[nut->sp_count] = pkt->pts;
974  }
975 
976  if (!nut->max_pts_tb || av_compare_ts(nut->max_pts, *nut->max_pts_tb, pkt->pts, *nus->time_base) < 0) {
977  nut->max_pts = pkt->pts;
978  nut->max_pts_tb = nus->time_base;
979  }
980 
981  return 0;
982 }
983 
985 {
986  NUTContext *nut = s->priv_data;
987  AVIOContext *bc = s->pb, *dyn_bc;
988  int i, ret;
989 
990  while (nut->header_count < 3)
991  write_headers(s, bc);
992 
993  ret = avio_open_dyn_buf(&dyn_bc);
994  if (ret >= 0 && nut->sp_count) {
995  write_index(nut, dyn_bc);
996  put_packet(nut, bc, dyn_bc, 1, INDEX_STARTCODE);
997  }
998 
999  ff_nut_free_sp(nut);
1000  for (i=0; i<s->nb_streams; i++)
1001  av_freep(&nut->stream[i].keyframe_pts);
1002 
1003  av_freep(&nut->stream);
1004  av_freep(&nut->chapter);
1005  av_freep(&nut->time_base);
1006 
1007  return 0;
1008 }
1009 
1011  .name = "nut",
1012  .long_name = NULL_IF_CONFIG_SMALL("NUT"),
1013  .mime_type = "video/x-nut",
1014  .extensions = "nut",
1015  .priv_data_size = sizeof(NUTContext),
1016  .audio_codec = CONFIG_LIBVORBIS ? AV_CODEC_ID_VORBIS :
1018  .video_codec = AV_CODEC_ID_MPEG4,
1023  .codec_tag = ff_nut_codec_tags,
1024 };
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1125
uint8_t header_len[128]
Definition: nut.h:93
const char const char void * val
Definition: avisynth_c.h:671
#define AV_WB24(p, d)
Definition: intreadwrite.h:442
float v
const char * s
Definition: avisynth_c.h:668
#define CONFIG_LIBMP3LAME
Definition: config.h:335
Bytestream IO Context.
Definition: avio.h:68
void * av_calloc(size_t nmemb, size_t size) av_malloc_attrib
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:249
#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
char * key
Definition: dict.h:81
int size
int64_t last_syncpoint_pos
Definition: nut.h:100
Definition: nut.h:62
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:1675
enum AVCodecID id
Definition: mxfenc.c:90
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding) av_const
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:60
void ff_parse_specific_params(AVCodecContext *stream, int *au_rate, int *au_ssize, int *au_scale)
Definition: riffenc.c:229
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:3922
int64_t pos
Definition: avformat.h:609
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:733
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1064
Definition: nut.h:55
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
int void avio_flush(AVIOContext *s)
Force flushing of buffered data to the output s.
Definition: aviobuf.c:193
uint8_t stream_id
Definition: nut.h:64
mpeg audio layer common tables.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1254
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1265
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
const uint8_t * header[128]
Definition: nut.h:94
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
Definition: avfilter.c:965
AVDictionary * metadata
Definition: avformat.h:735
Format I/O context.
Definition: avformat.h:968
void * av_tree_find(const AVTreeNode *t, void *key, int(*cmp)(void *key, const void *b), void *next[2])
Definition: tree.c:39
uint8_t
AVRational * time_base
Definition: nut.h:102
uint16_t flags
Definition: nut.h:63
A tree container.
#define ID_STRING
Definition: ffmeta.h:25
static void build_elision_headers(AVFormatContext *s)
Definition: nutenc.c:116
static void build_frame_code(AVFormatContext *s)
Definition: nutenc.c:138
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
if set, coded_pts is in the frame header
Definition: nut.h:44
const uint16_t avpriv_mpa_freq_tab[3]
Definition: mpegaudiodata.c:40
#define STREAM_STARTCODE
Definition: nut.h:30
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:291
static void put_tt(NUTContext *nut, AVRational *time_base, AVIOContext *bc, uint64_t val)
Definition: nutenc.c:267
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
const AVMetadataConv ff_nut_metadata_conv[]
Definition: nut.c:277
static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: nutenc.c:802
int last_flags
Definition: nut.h:73
#define AV_WB32(p, darg)
Definition: intreadwrite.h:265
#define MAX_DISTANCE
Definition: nut.h:37
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:823
#define sp
Definition: regdef.h:63
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1030
int ff_nut_sp_pos_cmp(const Syncpoint *a, const Syncpoint *b)
Definition: nut.c:220
AVFormatContext * avf
Definition: nut.h:89
int64_t last_pts
Definition: nut.h:75
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1113
static const uint8_t frame_size[4]
Definition: g723_1_data.h:58
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:353
AVOutputFormat ff_nut_muxer
Definition: nutenc.c:1010
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:210
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1427
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
void ff_nut_free_sp(NUTContext *nut)
Definition: nut.c:261
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1718
void * priv_data
Format private data.
Definition: avformat.h:988
static int nut_write_header(AVFormatContext *s)
Definition: nutenc.c:675
uint64_t pos
Definition: nut.h:56
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
AVRational av_div_q(AVRational b, AVRational c) av_const
Divide one rational by another.
Definition: rational.c:87
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: avcodec.h:4168
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:398
Definition: graph2dot.c:48
static int write_chapter(NUTContext *nut, AVIOContext *bc, int id)
Definition: nutenc.c:537
int sp_count
Definition: nut.h:104
int header_count
Definition: nut.h:101
AVRational * time_base
Definition: nut.h:77
if set, frame is keyframe
Definition: nut.h:42
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1069
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:135
static void write_mainheader(NUTContext *nut, AVIOContext *bc)
Definition: nutenc.c:332
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:1015
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:459
static int find_expected_header(AVCodecContext *c, int size, int key_frame, uint8_t out[64])
Definition: nutenc.c:33
AVRational * max_pts_tb
Definition: nut.h:106
void ff_nut_reset_ts(NUTContext *nut, AVRational time_base, int64_t val)
Definition: nut.c:202
uint8_t header_idx
Definition: nut.h:69
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
static uint16_t fc[]
Definition: dcaenc.h:41
uint16_t size_lsb
Definition: nut.h:66
AVStream ** streams
Definition: avformat.h:1016
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:93
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:445
int16_t pts_delta
Definition: nut.h:67
int64_t * keyframe_pts
Definition: nut.h:81
int64_t ff_lsb2full(StreamContext *stream, int64_t lsb)
Definition: nut.c:213
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:357
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
AVDictionary * metadata
Definition: avformat.h:946
char * value
Definition: dict.h:82
#define FFMIN(a, b)
Definition: avcodec.h:925
if set, frame_code is invalid
Definition: nut.h:52
const char * name
Definition: avformat.h:395
struct AVTreeNode * syncpoints
Definition: nut.h:103
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1216
if set, data_size_msb is at frame header, otherwise data_size_msb is 0
Definition: nut.h:46
ChapterContext * chapter
Definition: nut.h:97
int n
Definition: avisynth_c.h:588
if set, the frame header contains a checksum
Definition: nut.h:47
#define INDEX_STARTCODE
Definition: nut.h:32
uint16_t size_mul
Definition: nut.h:65
#define NUT_VERSION
Definition: nut.h:39
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:151
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:107
static int write_globalinfo(NUTContext *nut, AVIOContext *bc)
Definition: nutenc.c:471
static const float pred[4]
Definition: siprdata.h:259
Stream structure.
Definition: avformat.h:667
int msb_pts_shift
Definition: nut.h:78
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:945
sample_rate
enum AVMediaType codec_type
Definition: avcodec.h:1154
#define FFMAX(a, b)
Definition: avcodec.h:923
enum AVCodecID codec_id
Definition: avcodec.h:1157
static int intra_only
Definition: ffmpeg_opt.c:90
int sample_rate
samples per second
Definition: avcodec.h:1873
int max_pts_distance
Definition: nut.h:79
main external API structure.
Definition: avcodec.h:1146
static int write_streaminfo(NUTContext *nut, AVIOContext *bc, int stream_id)
Definition: nutenc.c:498
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:361
unsigned int codec_tag
fourcc (LSB first, so &quot;ABCD&quot; -&gt; (&#39;D&#39;&lt;&lt;24) + (&#39;C&#39;&lt;&lt;16) + (&#39;B&#39;&lt;&lt;8) + &#39;A&#39;).
Definition: avcodec.h:1172
static int write_index(NUTContext *nut, AVIOContext *bc)
Definition: nutenc.c:565
if set, coded_flags are stored in the frame header
Definition: nut.h:51
AVIOContext * pb
I/O context.
Definition: avformat.h:1001
void * buf
Definition: avisynth_c.h:594
int extradata_size
Definition: avcodec.h:1255
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1018
double value
Definition: eval.c:83
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:1838
int index
Definition: gxfenc.c:89
rational number numerator/denominator
Definition: rational.h:43
static int nut_write_trailer(AVFormatContext *s)
Definition: nutenc.c:984
uint8_t * data
Definition: avcodec.h:1063
AVRational * time_base
Definition: nut.h:85
AVDictionary * metadata
Definition: avformat.h:1128
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:451
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:70
StreamContext * stream
Definition: nut.h:96
#define snprintf
Definition: snprintf.h:34
static void put_s(AVIOContext *bc, int64_t val)
Definition: nutenc.c:284
#define AVINDEX_KEYFRAME
Definition: avformat.h:616
static int write_streamheader(AVFormatContext *avctx, AVIOContext *bc, AVStream *st, int i)
Definition: nutenc.c:407
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
Return audio frame duration.
Definition: utils.c:2946
#define type
Round toward -infinity.
Definition: mathematics.h:70
static int find_header_idx(AVFormatContext *s, AVCodecContext *c, int size, int frame_type)
Definition: nutenc.c:100
#define FFABS(a)
Definition: avcodec.h:920
#define INFO_STARTCODE
Definition: nut.h:33
int ff_get_v_length(uint64_t val)
Get the length in bytes which is needed to store val as v.
Definition: aviobuf.c:335
static int write_headers(AVFormatContext *avctx, AVIOContext *bc)
Definition: nutenc.c:610
static int flags
Definition: cpu.c:45
AVRational ff_choose_timebase(AVFormatContext *s, AVStream *st, int min_precission)
Chooses a timebase for muxing the specified stream.
Definition: mux.c:105
int64_t start
Definition: avformat.h:945
const Dispositions ff_nut_dispositions[]
Definition: nut.c:267
int64_t max_pts
Definition: nut.h:105
static int find_best_header_idx(NUTContext *nut, AVPacket *pkt)
Definition: nutenc.c:783
static float t
Definition: muxing.c:123
static double c[64]
FrameCode frame_code[256]
Definition: nut.h:92
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:724
int ff_nut_add_sp(NUTContext *nut, int64_t pos, int64_t back_ptr, int64_t ts)
Definition: nut.c:230
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:944
int den
denominator
Definition: rational.h:45
#define CONFIG_LIBVORBIS
Definition: config.h:358
#define SYNCPOINT_STARTCODE
Definition: nut.h:31
int flag
Definition: nut.h:118
static void put_str(AVIOContext *bc, const char *string)
Store a string as vb.
Definition: nutenc.c:276
static void put_packet(NUTContext *nut, AVIOContext *bc, AVIOContext *dyn_bc, int calculate_checksum, uint64_t startcode)
Definition: nutenc.c:309
void ff_put_v(AVIOContext *bc, uint64_t val)
Put val using a variable number of bytes.
Definition: aviobuf.c:345
If set, header_idx is coded in the frame header.
Definition: nut.h:49
int len
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
int channels
number of audio channels
Definition: avcodec.h:1874
#define av_log2
Definition: intmath.h:89
static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc, AVPacket *pkt)
Definition: nutenc.c:757
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:492
#define AVERROR(e)
int64_t dts
Decompression timestamp in AVStream-&gt;time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1062
void INT64 INT64 count
Definition: avisynth_c.h:594
if set, stream_id is coded in the frame header
Definition: nut.h:45
void INT64 start
Definition: avisynth_c.h:594
static int add_info(AVIOContext *bc, const char *type, const char *value)
Definition: nutenc.c:463
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:68
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
const uint16_t avpriv_mpa_bitrate_tab[2][3][15]
Definition: mpegaudiodata.c:30
static AVPacket pkt
Definition: demuxing.c:52
const char int length
Definition: avisynth_c.h:668
int stream_index
Definition: avcodec.h:1065
int dummy
Definition: motion-test.c:64
AVChapter ** chapters
Definition: avformat.h:1126
const AVCodecTag *const ff_nut_codec_tags[]
Definition: nut.c:197
This structure stores compressed data.
Definition: avcodec.h:1040
unsigned int time_base_count
Definition: nut.h:99
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:85
int64_t pts
Presentation timestamp in AVStream-&gt;time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1056
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avcodec.h:2278
static int64_t last_pts
unsigned int max_distance
Definition: nut.h:98