FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
flvenc.c
Go to the documentation of this file.
1 /*
2  * FLV muxer
3  * Copyright (c) 2003 The FFmpeg Project
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/dict.h"
24 #include "libavutil/intfloat.h"
25 #include "libavutil/avassert.h"
26 #include "libavutil/mathematics.h"
27 #include "avc.h"
28 #include "avformat.h"
29 #include "flv.h"
30 #include "internal.h"
31 #include "metadata.h"
32 #include "libavutil/opt.h"
33 #include "libavcodec/put_bits.h"
34 #include "libavcodec/aacenctab.h"
35 
36 
37 static const AVCodecTag flv_video_codec_ids[] = {
47  { AV_CODEC_ID_NONE, 0 }
48 };
49 
50 static const AVCodecTag flv_audio_codec_ids[] = {
61  { AV_CODEC_ID_NONE, 0 }
62 };
63 
64 typedef enum {
66  FLV_NO_SEQUENCE_END = (1 << 1),
67 } FLVFlags;
68 
69 typedef struct FLVContext {
71  int reserved;
72  int64_t duration_offset;
73  int64_t filesize_offset;
74  int64_t duration;
75  int64_t delay; ///< first dts delay (needed for AVC & Speex)
76 
79  double framerate;
81 
82  int flags;
83 } FLVContext;
84 
85 typedef struct FLVStreamContext {
86  int64_t last_ts; ///< last timestamp for each stream
88 
90 {
93 
94  if (par->codec_id == AV_CODEC_ID_AAC) // specs force these parameters
97  else if (par->codec_id == AV_CODEC_ID_SPEEX) {
98  if (par->sample_rate != 16000) {
100  "FLV only supports wideband (16kHz) Speex audio\n");
101  return AVERROR(EINVAL);
102  }
103  if (par->channels != 1) {
104  av_log(s, AV_LOG_ERROR, "FLV only supports mono Speex audio\n");
105  return AVERROR(EINVAL);
106  }
108  } else {
109  switch (par->sample_rate) {
110  case 44100:
111  flags |= FLV_SAMPLERATE_44100HZ;
112  break;
113  case 22050:
114  flags |= FLV_SAMPLERATE_22050HZ;
115  break;
116  case 11025:
117  flags |= FLV_SAMPLERATE_11025HZ;
118  break;
119  case 16000: // nellymoser only
120  case 8000: // nellymoser only
121  case 5512: // not MP3
122  if (par->codec_id != AV_CODEC_ID_MP3) {
123  flags |= FLV_SAMPLERATE_SPECIAL;
124  break;
125  }
126  default:
127  av_log(s, AV_LOG_ERROR,
128  "FLV does not support sample rate %d, "
129  "choose from (44100, 22050, 11025)\n", par->sample_rate);
130  return AVERROR(EINVAL);
131  }
132  }
133 
134  if (par->channels > 1)
135  flags |= FLV_STEREO;
136 
137  switch (par->codec_id) {
138  case AV_CODEC_ID_MP3:
140  break;
141  case AV_CODEC_ID_PCM_U8:
143  break;
146  break;
149  break;
152  break;
154  if (par->sample_rate == 8000)
156  else if (par->sample_rate == 16000)
158  else
160  break;
163  break;
166  break;
167  case 0:
168  flags |= par->codec_tag << 4;
169  break;
170  default:
171  av_log(s, AV_LOG_ERROR, "Audio codec '%s' not compatible with FLV\n",
172  avcodec_get_name(par->codec_id));
173  return AVERROR(EINVAL);
174  }
175 
176  return flags;
177 }
178 
179 static void put_amf_string(AVIOContext *pb, const char *str)
180 {
181  size_t len = strlen(str);
182  avio_wb16(pb, len);
183  avio_write(pb, str, len);
184 }
185 
186 static void put_avc_eos_tag(AVIOContext *pb, unsigned ts)
187 {
189  avio_wb24(pb, 5); /* Tag Data Size */
190  avio_wb24(pb, ts); /* lower 24 bits of timestamp in ms */
191  avio_w8(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms */
192  avio_wb24(pb, 0); /* StreamId = 0 */
193  avio_w8(pb, 23); /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
194  avio_w8(pb, 2); /* AVC end of sequence */
195  avio_wb24(pb, 0); /* Always 0 for AVC EOS. */
196  avio_wb32(pb, 16); /* Size of FLV tag */
197 }
198 
199 static void put_amf_double(AVIOContext *pb, double d)
200 {
202  avio_wb64(pb, av_double2int(d));
203 }
204 
205 static void put_amf_bool(AVIOContext *pb, int b)
206 {
208  avio_w8(pb, !!b);
209 }
210 
211 static void write_metadata(AVFormatContext *s, unsigned int ts)
212 {
213  AVIOContext *pb = s->pb;
214  FLVContext *flv = s->priv_data;
215  int metadata_count = 0;
216  int64_t metadata_size_pos, data_size, metadata_count_pos;
218 
219  /* write meta_tag */
220  avio_w8(pb, 18); // tag type META
221  metadata_size_pos = avio_tell(pb);
222  avio_wb24(pb, 0); // size of data part (sum of all parts below)
223  avio_wb24(pb, ts); // timestamp
224  avio_wb32(pb, 0); // reserved
225 
226  /* now data of data_size size */
227 
228  /* first event name as a string */
230  put_amf_string(pb, "onMetaData"); // 12 bytes
231 
232  /* mixed array (hash) with size and string/type/data tuples */
234  metadata_count_pos = avio_tell(pb);
235  metadata_count = 4 * !!flv->video_par +
236  5 * !!flv->audio_par +
237  1 * !!flv->data_par +
238  2; // +2 for duration and file size
239 
240  avio_wb32(pb, metadata_count);
241 
242  put_amf_string(pb, "duration");
243  flv->duration_offset = avio_tell(pb);
244 
245  // fill in the guessed duration, it'll be corrected later if incorrect
247 
248  if (flv->video_par) {
249  put_amf_string(pb, "width");
250  put_amf_double(pb, flv->video_par->width);
251 
252  put_amf_string(pb, "height");
253  put_amf_double(pb, flv->video_par->height);
254 
255  put_amf_string(pb, "videodatarate");
256  put_amf_double(pb, flv->video_par->bit_rate / 1024.0);
257 
258  if (flv->framerate != 0.0) {
259  put_amf_string(pb, "framerate");
260  put_amf_double(pb, flv->framerate);
261  metadata_count++;
262  }
263 
264  put_amf_string(pb, "videocodecid");
266  }
267 
268  if (flv->audio_par) {
269  put_amf_string(pb, "audiodatarate");
270  put_amf_double(pb, flv->audio_par->bit_rate / 1024.0);
271 
272  put_amf_string(pb, "audiosamplerate");
274 
275  put_amf_string(pb, "audiosamplesize");
276  put_amf_double(pb, flv->audio_par->codec_id == AV_CODEC_ID_PCM_U8 ? 8 : 16);
277 
278  put_amf_string(pb, "stereo");
279  put_amf_bool(pb, flv->audio_par->channels == 2);
280 
281  put_amf_string(pb, "audiocodecid");
283  }
284 
285  if (flv->data_par) {
286  put_amf_string(pb, "datastream");
287  put_amf_double(pb, 0.0);
288  }
289 
291  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
292  if( !strcmp(tag->key, "width")
293  ||!strcmp(tag->key, "height")
294  ||!strcmp(tag->key, "videodatarate")
295  ||!strcmp(tag->key, "framerate")
296  ||!strcmp(tag->key, "videocodecid")
297  ||!strcmp(tag->key, "audiodatarate")
298  ||!strcmp(tag->key, "audiosamplerate")
299  ||!strcmp(tag->key, "audiosamplesize")
300  ||!strcmp(tag->key, "stereo")
301  ||!strcmp(tag->key, "audiocodecid")
302  ||!strcmp(tag->key, "duration")
303  ||!strcmp(tag->key, "onMetaData")
304  ||!strcmp(tag->key, "datasize")
305  ||!strcmp(tag->key, "lasttimestamp")
306  ||!strcmp(tag->key, "totalframes")
307  ||!strcmp(tag->key, "hasAudio")
308  ||!strcmp(tag->key, "hasVideo")
309  ||!strcmp(tag->key, "hasCuePoints")
310  ||!strcmp(tag->key, "hasMetadata")
311  ||!strcmp(tag->key, "hasKeyframes")
312  ){
313  av_log(s, AV_LOG_DEBUG, "Ignoring metadata for %s\n", tag->key);
314  continue;
315  }
316  put_amf_string(pb, tag->key);
318  put_amf_string(pb, tag->value);
319  metadata_count++;
320  }
321 
322  put_amf_string(pb, "filesize");
323  flv->filesize_offset = avio_tell(pb);
324  put_amf_double(pb, 0); // delayed write
325 
326  put_amf_string(pb, "");
328 
329  /* write total size of tag */
330  data_size = avio_tell(pb) - metadata_size_pos - 10;
331 
332  avio_seek(pb, metadata_count_pos, SEEK_SET);
333  avio_wb32(pb, metadata_count);
334 
335  avio_seek(pb, metadata_size_pos, SEEK_SET);
336  avio_wb24(pb, data_size);
337  avio_skip(pb, data_size + 10 - 3);
338  avio_wb32(pb, data_size + 11);
339 }
340 
342  const char* type, int codec_id)
343 {
344  const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
345  av_log(s, AV_LOG_ERROR,
346  "%s codec %s not compatible with flv\n",
347  type,
348  desc ? desc->name : "unknown");
349  return AVERROR(ENOSYS);
350 }
351 
353  int64_t data_size;
354  AVIOContext *pb = s->pb;
355  FLVContext *flv = s->priv_data;
356 
357  if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264
358  || par->codec_id == AV_CODEC_ID_MPEG4) {
359  int64_t pos;
360  avio_w8(pb,
363  avio_wb24(pb, 0); // size patched later
364  avio_wb24(pb, 0); // ts
365  avio_w8(pb, 0); // ts ext
366  avio_wb24(pb, 0); // streamid
367  pos = avio_tell(pb);
368  if (par->codec_id == AV_CODEC_ID_AAC) {
369  avio_w8(pb, get_audio_flags(s, par));
370  avio_w8(pb, 0); // AAC sequence header
371 
372  if (!par->extradata_size && (flv->flags & FLV_AAC_SEQ_HEADER_DETECT)) {
373  PutBitContext pbc;
374  int samplerate_index;
375  int channels = flv->audio_par->channels
376  - (flv->audio_par->channels == 8 ? 1 : 0);
377  uint8_t data[2];
378 
379  for (samplerate_index = 0; samplerate_index < 16;
380  samplerate_index++)
381  if (flv->audio_par->sample_rate
382  == mpeg4audio_sample_rates[samplerate_index])
383  break;
384 
385  init_put_bits(&pbc, data, sizeof(data));
386  put_bits(&pbc, 5, flv->audio_par->profile + 1); //profile
387  put_bits(&pbc, 4, samplerate_index); //sample rate index
388  put_bits(&pbc, 4, channels);
389  put_bits(&pbc, 1, 0); //frame length - 1024 samples
390  put_bits(&pbc, 1, 0); //does not depend on core coder
391  put_bits(&pbc, 1, 0); //is not extension
392  flush_put_bits(&pbc);
393 
394  avio_w8(pb, data[0]);
395  avio_w8(pb, data[1]);
396 
397  av_log(s, AV_LOG_WARNING, "AAC sequence header: %02x %02x.\n",
398  data[0], data[1]);
399  }
400  avio_write(pb, par->extradata, par->extradata_size);
401  } else {
402  avio_w8(pb, par->codec_tag | FLV_FRAME_KEY); // flags
403  avio_w8(pb, 0); // AVC sequence header
404  avio_wb24(pb, 0); // composition time
406  }
407  data_size = avio_tell(pb) - pos;
408  avio_seek(pb, -data_size - 10, SEEK_CUR);
409  avio_wb24(pb, data_size);
410  avio_skip(pb, data_size + 10 - 3);
411  avio_wb32(pb, data_size + 11); // previous tag size
412  }
413 }
414 
416 {
417  int i;
418  AVIOContext *pb = s->pb;
419  FLVContext *flv = s->priv_data;
420 
421  for (i = 0; i < s->nb_streams; i++) {
422  AVCodecParameters *par = s->streams[i]->codecpar;
423  FLVStreamContext *sc;
424  switch (par->codec_type) {
425  case AVMEDIA_TYPE_VIDEO:
426  if (s->streams[i]->avg_frame_rate.den &&
427  s->streams[i]->avg_frame_rate.num) {
428  flv->framerate = av_q2d(s->streams[i]->avg_frame_rate);
429  }
430  if (flv->video_par) {
431  av_log(s, AV_LOG_ERROR,
432  "at most one video stream is supported in flv\n");
433  return AVERROR(EINVAL);
434  }
435  flv->video_par = par;
436  if (!ff_codec_get_tag(flv_video_codec_ids, par->codec_id))
437  return unsupported_codec(s, "Video", par->codec_id);
438 
439  if (par->codec_id == AV_CODEC_ID_MPEG4 ||
440  par->codec_id == AV_CODEC_ID_H263) {
442  av_log(s, error ? AV_LOG_ERROR : AV_LOG_WARNING,
443  "Codec %s is not supported in the official FLV specification,\n", avcodec_get_name(par->codec_id));
444 
445  if (error) {
446  av_log(s, AV_LOG_ERROR,
447  "use vstrict=-1 / -strict -1 to use it anyway.\n");
448  return AVERROR(EINVAL);
449  }
450  } else if (par->codec_id == AV_CODEC_ID_VP6) {
452  "Muxing VP6 in flv will produce flipped video on playback.\n");
453  }
454  break;
455  case AVMEDIA_TYPE_AUDIO:
456  if (flv->audio_par) {
457  av_log(s, AV_LOG_ERROR,
458  "at most one audio stream is supported in flv\n");
459  return AVERROR(EINVAL);
460  }
461  flv->audio_par = par;
462  if (get_audio_flags(s, par) < 0)
463  return unsupported_codec(s, "Audio", par->codec_id);
464  if (par->codec_id == AV_CODEC_ID_PCM_S16BE)
466  "16-bit big-endian audio in flv is valid but most likely unplayable (hardware dependent); use s16le\n");
467  break;
468  case AVMEDIA_TYPE_DATA:
469  if (par->codec_id != AV_CODEC_ID_TEXT && par->codec_id != AV_CODEC_ID_NONE)
470  return unsupported_codec(s, "Data", par->codec_id);
471  flv->data_par = par;
472  break;
474  if (par->codec_id != AV_CODEC_ID_TEXT) {
475  av_log(s, AV_LOG_ERROR, "Subtitle codec '%s' for stream %d is not compatible with FLV\n",
476  avcodec_get_name(par->codec_id), i);
477  return AVERROR_INVALIDDATA;
478  }
479  flv->data_par = par;
480  break;
481  default:
482  av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n",
484  return AVERROR(EINVAL);
485  }
486  avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
487 
488  sc = av_mallocz(sizeof(FLVStreamContext));
489  if (!sc)
490  return AVERROR(ENOMEM);
491  s->streams[i]->priv_data = sc;
492  sc->last_ts = -1;
493  }
494 
495  flv->delay = AV_NOPTS_VALUE;
496 
497  avio_write(pb, "FLV", 3);
498  avio_w8(pb, 1);
501  avio_wb32(pb, 9);
502  avio_wb32(pb, 0);
503 
504  for (i = 0; i < s->nb_streams; i++)
505  if (s->streams[i]->codecpar->codec_tag == 5) {
506  avio_w8(pb, 8); // message type
507  avio_wb24(pb, 0); // include flags
508  avio_wb24(pb, 0); // time stamp
509  avio_wb32(pb, 0); // reserved
510  avio_wb32(pb, 11); // size
511  flv->reserved = 5;
512  }
513 
514  write_metadata(s, 0);
515 
516  for (i = 0; i < s->nb_streams; i++) {
518  }
519 
520  return 0;
521 }
522 
524 {
525  int64_t file_size;
526 
527  AVIOContext *pb = s->pb;
528  FLVContext *flv = s->priv_data;
529  int i;
530 
531  if (flv->flags & FLV_NO_SEQUENCE_END) {
532  av_log(s, AV_LOG_DEBUG, "FLV no sequence end mode open\n");
533  } else {
534  /* Add EOS tag */
535  for (i = 0; i < s->nb_streams; i++) {
536  AVCodecParameters *par = s->streams[i]->codecpar;
537  FLVStreamContext *sc = s->streams[i]->priv_data;
538  if (par->codec_type == AVMEDIA_TYPE_VIDEO &&
540  put_avc_eos_tag(pb, sc->last_ts);
541  }
542  }
543 
544  file_size = avio_tell(pb);
545 
546  /* update information */
547  if (avio_seek(pb, flv->duration_offset, SEEK_SET) < 0)
548  av_log(s, AV_LOG_WARNING, "Failed to update header with correct duration.\n");
549  else
550  put_amf_double(pb, flv->duration / (double)1000);
551  if (avio_seek(pb, flv->filesize_offset, SEEK_SET) < 0)
552  av_log(s, AV_LOG_WARNING, "Failed to update header with correct filesize.\n");
553  else
554  put_amf_double(pb, file_size);
555 
556  avio_seek(pb, file_size, SEEK_SET);
557  return 0;
558 }
559 
561 {
562  AVIOContext *pb = s->pb;
564  FLVContext *flv = s->priv_data;
566  unsigned ts;
567  int size = pkt->size;
568  uint8_t *data = NULL;
569  int flags = -1, flags_size, ret;
570 
571  if (par->codec_id == AV_CODEC_ID_VP6F || par->codec_id == AV_CODEC_ID_VP6A ||
573  flags_size = 2;
574  else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4)
575  flags_size = 5;
576  else
577  flags_size = 1;
578 
579  if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264
580  || par->codec_id == AV_CODEC_ID_MPEG4) {
581  int side_size = 0;
583  if (side && side_size > 0 && (side_size != par->extradata_size || memcmp(side, par->extradata, side_size))) {
584  av_free(par->extradata);
586  memcpy(par->extradata, side, side_size);
587  par->extradata_size = side_size;
588  flv_write_codec_header(s, par);
589  }
590  }
591 
592  if (flv->delay == AV_NOPTS_VALUE)
593  flv->delay = -pkt->dts;
594 
595  if (pkt->dts < -flv->delay) {
597  "Packets are not in the proper order with respect to DTS\n");
598  return AVERROR(EINVAL);
599  }
600 
601  ts = pkt->dts;
602 
604  write_metadata(s, ts);
606  }
607 
610 
611  switch (par->codec_type) {
612  case AVMEDIA_TYPE_VIDEO:
614 
615  flags = ff_codec_get_tag(flv_video_codec_ids, par->codec_id);
616 
618  break;
619  case AVMEDIA_TYPE_AUDIO:
620  flags = get_audio_flags(s, par);
621 
622  av_assert0(size);
623 
625  break;
627  case AVMEDIA_TYPE_DATA:
629  break;
630  default:
631  return AVERROR(EINVAL);
632  }
633 
634  if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4) {
635  /* check if extradata looks like mp4 formatted */
636  if (par->extradata_size > 0 && *(uint8_t*)par->extradata != 1)
637  if ((ret = ff_avc_parse_nal_units_buf(pkt->data, &data, &size)) < 0)
638  return ret;
639  } else if (par->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
640  (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
641  if (!s->streams[pkt->stream_index]->nb_frames) {
642  av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
643  "use the audio bitstream filter 'aac_adtstoasc' to fix it "
644  "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
645  return AVERROR_INVALIDDATA;
646  }
647  av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
648  }
649 
650  /* check Speex packet duration */
651  if (par->codec_id == AV_CODEC_ID_SPEEX && ts - sc->last_ts > 160)
652  av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
653  "8 frames per packet. Adobe Flash "
654  "Player cannot handle this!\n");
655 
656  if (sc->last_ts < ts)
657  sc->last_ts = ts;
658 
659  if (size + flags_size >= 1<<24) {
660  av_log(s, AV_LOG_ERROR, "Too large packet with size %u >= %u\n",
661  size + flags_size, 1<<24);
662  return AVERROR(EINVAL);
663  }
664 
665  avio_wb24(pb, size + flags_size);
666  avio_wb24(pb, ts & 0xFFFFFF);
667  avio_w8(pb, (ts >> 24) & 0x7F); // timestamps are 32 bits _signed_
668  avio_wb24(pb, flv->reserved);
669 
670  if (par->codec_type == AVMEDIA_TYPE_DATA ||
672  int data_size;
673  int64_t metadata_size_pos = avio_tell(pb);
674  if (par->codec_id == AV_CODEC_ID_TEXT) {
675  // legacy FFmpeg magic?
677  put_amf_string(pb, "onTextData");
679  avio_wb32(pb, 2);
680  put_amf_string(pb, "type");
682  put_amf_string(pb, "Text");
683  put_amf_string(pb, "text");
685  put_amf_string(pb, pkt->data);
686  put_amf_string(pb, "");
688  } else {
689  // just pass the metadata through
690  avio_write(pb, data ? data : pkt->data, size);
691  }
692  /* write total size of tag */
693  data_size = avio_tell(pb) - metadata_size_pos;
694  avio_seek(pb, metadata_size_pos - 10, SEEK_SET);
695  avio_wb24(pb, data_size);
696  avio_seek(pb, data_size + 10 - 3, SEEK_CUR);
697  avio_wb32(pb, data_size + 11);
698  } else {
699  av_assert1(flags>=0);
700  avio_w8(pb,flags);
701  if (par->codec_id == AV_CODEC_ID_VP6)
702  avio_w8(pb,0);
703  if (par->codec_id == AV_CODEC_ID_VP6F || par->codec_id == AV_CODEC_ID_VP6A) {
704  if (par->extradata_size)
705  avio_w8(pb, par->extradata[0]);
706  else
707  avio_w8(pb, ((FFALIGN(par->width, 16) - par->width) << 4) |
708  (FFALIGN(par->height, 16) - par->height));
709  } else if (par->codec_id == AV_CODEC_ID_AAC)
710  avio_w8(pb, 1); // AAC raw
711  else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4) {
712  avio_w8(pb, 1); // AVC NALU
713  avio_wb24(pb, pkt->pts - pkt->dts);
714  }
715 
716  avio_write(pb, data ? data : pkt->data, size);
717 
718  avio_wb32(pb, size + flags_size + 11); // previous tag size
719  flv->duration = FFMAX(flv->duration,
720  pkt->pts + flv->delay + pkt->duration);
721  }
722 
723  av_free(data);
724 
725  return pb->error;
726 }
727 
728 static const AVOption options[] = {
729  { "flvflags", "FLV muxer flags", offsetof(FLVContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "flvflags" },
730  { "aac_seq_header_detect", "Put AAC sequence header based on stream data", 0, AV_OPT_TYPE_CONST, {.i64 = FLV_AAC_SEQ_HEADER_DETECT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "flvflags" },
731  { "no_sequence_end", "disable sequence end for FLV", 0, AV_OPT_TYPE_CONST, {.i64 = FLV_NO_SEQUENCE_END}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "flvflags" },
732  { NULL },
733 };
734 
735 static const AVClass flv_muxer_class = {
736  .class_name = "flv muxer",
737  .item_name = av_default_item_name,
738  .option = options,
739  .version = LIBAVUTIL_VERSION_INT,
740 };
741 
743  .name = "flv",
744  .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
745  .mime_type = "video/x-flv",
746  .extensions = "flv",
747  .priv_data_size = sizeof(FLVContext),
748  .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_ADPCM_SWF,
749  .video_codec = AV_CODEC_ID_FLV1,
753  .codec_tag = (const AVCodecTag* const []) {
755  },
758  .priv_class = &flv_muxer_class,
759 };
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:440
#define NULL
Definition: coverity.c:32
static const AVCodecTag flv_audio_codec_ids[]
Definition: flvenc.c:50
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:147
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
enum AVCodecID codec_id
Definition: ffmpeg_vaapi.c:149
#define AVSTREAM_EVENT_FLAG_METADATA_UPDATED
The call resulted in updated metadata.
Definition: avformat.h:1000
AVOutputFormat ff_flv_muxer
Definition: flvenc.c:742
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
double framerate
Definition: flvenc.c:79
static void flv_write_codec_header(AVFormatContext *s, AVCodecParameters *par)
Definition: flvenc.c:352
int64_t delay
first dts delay (needed for AVC & Speex)
Definition: flvenc.c:75
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:206
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
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:4549
const char * desc
Definition: nvenc.c:101
int64_t duration
Definition: flvenc.c:74
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3979
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1601
const char * b
Definition: vf_curves.c:113
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
static int unsupported_codec(AVFormatContext *s, const char *type, int codec_id)
Definition: flvenc.c:341
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
static const AVClass flv_muxer_class
Definition: flvenc.c:735
int event_flags
Flags for the user to detect events happening on the file.
Definition: avformat.h:1621
void * priv_data
Definition: avformat.h:896
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:304
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:2992
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
static AVPacket pkt
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
static int get_audio_flags(AVFormatContext *s, AVCodecParameters *par)
Definition: flvenc.c:89
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1614
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:496
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3971
static const AVCodecTag flv_video_codec_ids[]
Definition: flvenc.c:37
static const AVOption options[]
Definition: flvenc.c:728
Format I/O context.
Definition: avformat.h:1330
static void write_packet(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
Definition: ffmpeg.c:635
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
internal metadata API header see avformat.h or the public API!
Public dictionary API.
uint8_t
Opaque data information usually continuous.
Definition: avutil.h:197
int width
Video only.
Definition: avcodec.h:4045
AVCodecParameters * video_par
Definition: flvenc.c:78
AVOptions.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1618
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
Definition: aviobuf.c:470
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1398
A point in the output bytestream where a demuxer can start parsing (for non self synchronizing bytest...
Definition: avio.h:120
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
uint8_t * data
Definition: avcodec.h:1600
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
uint32_t tag
Definition: movenc.c:1382
AVCodecParameters * audio_par
Definition: flvenc.c:77
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:511
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:204
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:275
Definition: flv.h:74
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:4008
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:2897
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1632
static int flv_write_trailer(AVFormatContext *s)
Definition: flvenc.c:523
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1546
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:517
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3975
static const int mpeg4audio_sample_rates[16]
Definition: aacenctab.h:74
simple assert() macros that are a bit more flexible than ISO C assert().
int ff_standardize_creation_time(AVFormatContext *s)
Standardize creation_time metadata in AVFormatContext to an ISO-8601 timestamp string.
Definition: utils.c:5268
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:959
#define FFMAX(a, b)
Definition: common.h:94
int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
Definition: avc.c:92
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1606
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:2971
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3997
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1386
#define FLV_AUDIO_CODECID_OFFSET
Definition: flv.h:34
inter frame (for AVC, a non-seekable frame)
Definition: flv.h:116
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:248
static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: flvenc.c:560
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:485
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:464
const char * name
Definition: avformat.h:524
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:3111
int reserved
Definition: flvenc.c:71
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: avcodec.h:1371
FLV common header.
key frame (for AVC, a seekable frame)
Definition: flv.h:115
AVIOContext * pb
I/O context.
Definition: avformat.h:1372
static int flv_write_header(AVFormatContext *s)
Definition: flvenc.c:415
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:182
static void put_amf_string(AVIOContext *pb, const char *str)
Definition: flvenc.c:179
GLint GLenum type
Definition: opengl_enc.c:105
static void put_amf_double(AVIOContext *pb, double d)
Definition: flvenc.c:199
AVClass * av_class
Definition: flvenc.c:70
Describe the class of an AVClass context structure.
Definition: log.h:67
AVCodecParameters * data_par
Definition: flvenc.c:80
const char * name
Name of the codec described by this descriptor.
Definition: avcodec.h:667
signifies 5512Hz and 8000Hz in the case of NELLYMOSER
Definition: flv.h:83
int error
contains the error code or 0 if no error happened
Definition: avio.h:228
This struct describes the properties of a single codec described by an AVCodecID. ...
Definition: avcodec.h:659
AAC encoder data.
#define AMF_END_OF_OBJECT
Definition: flv.h:47
static void put_amf_bool(AVIOContext *pb, int b)
Definition: flvenc.c:205
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:452
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
Definition: utils.c:79
static int flags
Definition: cpu.c:47
int sample_rate
Audio only.
Definition: avcodec.h:4089
Main libavformat public API header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
int64_t duration_offset
Definition: flvenc.c:72
raw UTF-8 text
Definition: avcodec.h:606
static void write_metadata(AVFormatContext *s, unsigned int ts)
Definition: flvenc.c:211
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: avcodec.h:4039
int flags
Definition: flvenc.c:82
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:937
char * key
Definition: dict.h:86
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
int den
Denominator.
Definition: rational.h:60
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:733
static void put_avc_eos_tag(AVIOContext *pb, unsigned ts)
Definition: flvenc.c:186
A point in the output bytestream where a decoder can start decoding (i.e.
Definition: avio.h:114
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:489
#define av_free(p)
char * value
Definition: dict.h:87
int len
void * priv_data
Format private data.
Definition: avformat.h:1358
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:344
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:4021
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3993
int channels
Audio only.
Definition: avcodec.h:4085
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1599
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:354
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1425
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:338
AVCodecParameters * codecpar
Definition: avformat.h:1233
int64_t filesize_offset
Definition: flvenc.c:73
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:3983
int stream_index
Definition: avcodec.h:1602
This structure stores compressed data.
Definition: avcodec.h:1577
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1593
int64_t last_ts
last timestamp for each stream
Definition: flvenc.c:86
FLVFlags
Definition: flvenc.c:64
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition: avc.c:106
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:242
bitstream writer API