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