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