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  if (pb->seekable) {
239  metadata_count += 2; // +2 for duration and file size
240  }
241  avio_wb32(pb, metadata_count);
242 
243  if (pb->seekable) {
244  put_amf_string(pb, "duration");
245  flv->duration_offset = avio_tell(pb);
246  // fill in the guessed duration, it'll be corrected later if incorrect
248  }
249 
250  if (flv->video_par) {
251  put_amf_string(pb, "width");
252  put_amf_double(pb, flv->video_par->width);
253 
254  put_amf_string(pb, "height");
255  put_amf_double(pb, flv->video_par->height);
256 
257  put_amf_string(pb, "videodatarate");
258  put_amf_double(pb, flv->video_par->bit_rate / 1024.0);
259 
260  if (flv->framerate != 0.0) {
261  put_amf_string(pb, "framerate");
262  put_amf_double(pb, flv->framerate);
263  metadata_count++;
264  }
265 
266  put_amf_string(pb, "videocodecid");
268  }
269 
270  if (flv->audio_par) {
271  put_amf_string(pb, "audiodatarate");
272  put_amf_double(pb, flv->audio_par->bit_rate / 1024.0);
273 
274  put_amf_string(pb, "audiosamplerate");
276 
277  put_amf_string(pb, "audiosamplesize");
278  put_amf_double(pb, flv->audio_par->codec_id == AV_CODEC_ID_PCM_U8 ? 8 : 16);
279 
280  put_amf_string(pb, "stereo");
281  put_amf_bool(pb, flv->audio_par->channels == 2);
282 
283  put_amf_string(pb, "audiocodecid");
285  }
286 
287  if (flv->data_par) {
288  put_amf_string(pb, "datastream");
289  put_amf_double(pb, 0.0);
290  }
291 
293  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
294  if( !strcmp(tag->key, "width")
295  ||!strcmp(tag->key, "height")
296  ||!strcmp(tag->key, "videodatarate")
297  ||!strcmp(tag->key, "framerate")
298  ||!strcmp(tag->key, "videocodecid")
299  ||!strcmp(tag->key, "audiodatarate")
300  ||!strcmp(tag->key, "audiosamplerate")
301  ||!strcmp(tag->key, "audiosamplesize")
302  ||!strcmp(tag->key, "stereo")
303  ||!strcmp(tag->key, "audiocodecid")
304  ||!strcmp(tag->key, "duration")
305  ||!strcmp(tag->key, "onMetaData")
306  ||!strcmp(tag->key, "datasize")
307  ||!strcmp(tag->key, "lasttimestamp")
308  ||!strcmp(tag->key, "totalframes")
309  ||!strcmp(tag->key, "hasAudio")
310  ||!strcmp(tag->key, "hasVideo")
311  ||!strcmp(tag->key, "hasCuePoints")
312  ||!strcmp(tag->key, "hasMetadata")
313  ||!strcmp(tag->key, "hasKeyframes")
314  ){
315  av_log(s, AV_LOG_DEBUG, "Ignoring metadata for %s\n", tag->key);
316  continue;
317  }
318  put_amf_string(pb, tag->key);
320  put_amf_string(pb, tag->value);
321  metadata_count++;
322  }
323 
324  if (pb->seekable) {
325  put_amf_string(pb, "filesize");
326  flv->filesize_offset = avio_tell(pb);
327  put_amf_double(pb, 0); // delayed write
328  }
329 
330  put_amf_string(pb, "");
332 
333  /* write total size of tag */
334  data_size = avio_tell(pb) - metadata_size_pos - 10;
335 
336  avio_seek(pb, metadata_count_pos, SEEK_SET);
337  avio_wb32(pb, metadata_count);
338 
339  avio_seek(pb, metadata_size_pos, SEEK_SET);
340  avio_wb24(pb, data_size);
341  avio_skip(pb, data_size + 10 - 3);
342  avio_wb32(pb, data_size + 11);
343 }
344 
346  const char* type, int codec_id)
347 {
348  const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
349  av_log(s, AV_LOG_ERROR,
350  "%s codec %s not compatible with flv\n",
351  type,
352  desc ? desc->name : "unknown");
353  return AVERROR(ENOSYS);
354 }
355 
357  int64_t data_size;
358  AVIOContext *pb = s->pb;
359  FLVContext *flv = s->priv_data;
360 
361  if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264
362  || par->codec_id == AV_CODEC_ID_MPEG4) {
363  int64_t pos;
364  avio_w8(pb,
367  avio_wb24(pb, 0); // size patched later
368  avio_wb24(pb, 0); // ts
369  avio_w8(pb, 0); // ts ext
370  avio_wb24(pb, 0); // streamid
371  pos = avio_tell(pb);
372  if (par->codec_id == AV_CODEC_ID_AAC) {
373  avio_w8(pb, get_audio_flags(s, par));
374  avio_w8(pb, 0); // AAC sequence header
375 
376  if (!par->extradata_size && (flv->flags & FLV_AAC_SEQ_HEADER_DETECT)) {
377  PutBitContext pbc;
378  int samplerate_index;
379  int channels = flv->audio_par->channels
380  - (flv->audio_par->channels == 8 ? 1 : 0);
381  uint8_t data[2];
382 
383  for (samplerate_index = 0; samplerate_index < 16;
384  samplerate_index++)
385  if (flv->audio_par->sample_rate
386  == mpeg4audio_sample_rates[samplerate_index])
387  break;
388 
389  init_put_bits(&pbc, data, sizeof(data));
390  put_bits(&pbc, 5, flv->audio_par->profile + 1); //profile
391  put_bits(&pbc, 4, samplerate_index); //sample rate index
392  put_bits(&pbc, 4, channels);
393  put_bits(&pbc, 1, 0); //frame length - 1024 samples
394  put_bits(&pbc, 1, 0); //does not depend on core coder
395  put_bits(&pbc, 1, 0); //is not extension
396  flush_put_bits(&pbc);
397 
398  avio_w8(pb, data[0]);
399  avio_w8(pb, data[1]);
400 
401  av_log(s, AV_LOG_WARNING, "AAC sequence header: %02x %02x.\n",
402  data[0], data[1]);
403  }
404  avio_write(pb, par->extradata, par->extradata_size);
405  } else {
406  avio_w8(pb, par->codec_tag | FLV_FRAME_KEY); // flags
407  avio_w8(pb, 0); // AVC sequence header
408  avio_wb24(pb, 0); // composition time
410  }
411  data_size = avio_tell(pb) - pos;
412  avio_seek(pb, -data_size - 10, SEEK_CUR);
413  avio_wb24(pb, data_size);
414  avio_skip(pb, data_size + 10 - 3);
415  avio_wb32(pb, data_size + 11); // previous tag size
416  }
417 }
418 
420 {
421  int i;
422  AVIOContext *pb = s->pb;
423  FLVContext *flv = s->priv_data;
424 
425  for (i = 0; i < s->nb_streams; i++) {
426  AVCodecParameters *par = s->streams[i]->codecpar;
427  FLVStreamContext *sc;
428  switch (par->codec_type) {
429  case AVMEDIA_TYPE_VIDEO:
430  if (s->streams[i]->avg_frame_rate.den &&
431  s->streams[i]->avg_frame_rate.num) {
432  flv->framerate = av_q2d(s->streams[i]->avg_frame_rate);
433  }
434  if (flv->video_par) {
435  av_log(s, AV_LOG_ERROR,
436  "at most one video stream is supported in flv\n");
437  return AVERROR(EINVAL);
438  }
439  flv->video_par = par;
440  if (!ff_codec_get_tag(flv_video_codec_ids, par->codec_id))
441  return unsupported_codec(s, "Video", par->codec_id);
442 
443  if (par->codec_id == AV_CODEC_ID_MPEG4 ||
444  par->codec_id == AV_CODEC_ID_H263) {
446  av_log(s, error ? AV_LOG_ERROR : AV_LOG_WARNING,
447  "Codec %s is not supported in the official FLV specification,\n", avcodec_get_name(par->codec_id));
448 
449  if (error) {
450  av_log(s, AV_LOG_ERROR,
451  "use vstrict=-1 / -strict -1 to use it anyway.\n");
452  return AVERROR(EINVAL);
453  }
454  } else if (par->codec_id == AV_CODEC_ID_VP6) {
456  "Muxing VP6 in flv will produce flipped video on playback.\n");
457  }
458  break;
459  case AVMEDIA_TYPE_AUDIO:
460  if (flv->audio_par) {
461  av_log(s, AV_LOG_ERROR,
462  "at most one audio stream is supported in flv\n");
463  return AVERROR(EINVAL);
464  }
465  flv->audio_par = par;
466  if (get_audio_flags(s, par) < 0)
467  return unsupported_codec(s, "Audio", par->codec_id);
468  if (par->codec_id == AV_CODEC_ID_PCM_S16BE)
470  "16-bit big-endian audio in flv is valid but most likely unplayable (hardware dependent); use s16le\n");
471  break;
472  case AVMEDIA_TYPE_DATA:
473  if (par->codec_id != AV_CODEC_ID_TEXT && par->codec_id != AV_CODEC_ID_NONE)
474  return unsupported_codec(s, "Data", par->codec_id);
475  flv->data_par = par;
476  break;
478  if (par->codec_id != AV_CODEC_ID_TEXT) {
479  av_log(s, AV_LOG_ERROR, "Subtitle codec '%s' for stream %d is not compatible with FLV\n",
480  avcodec_get_name(par->codec_id), i);
481  return AVERROR_INVALIDDATA;
482  }
483  flv->data_par = par;
484  break;
485  default:
486  av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n",
488  return AVERROR(EINVAL);
489  }
490  avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
491 
492  sc = av_mallocz(sizeof(FLVStreamContext));
493  if (!sc)
494  return AVERROR(ENOMEM);
495  s->streams[i]->priv_data = sc;
496  sc->last_ts = -1;
497  }
498 
499  flv->delay = AV_NOPTS_VALUE;
500 
501  avio_write(pb, "FLV", 3);
502  avio_w8(pb, 1);
505  avio_wb32(pb, 9);
506  avio_wb32(pb, 0);
507 
508  for (i = 0; i < s->nb_streams; i++)
509  if (s->streams[i]->codecpar->codec_tag == 5) {
510  avio_w8(pb, 8); // message type
511  avio_wb24(pb, 0); // include flags
512  avio_wb24(pb, 0); // time stamp
513  avio_wb32(pb, 0); // reserved
514  avio_wb32(pb, 11); // size
515  flv->reserved = 5;
516  }
517 
518  write_metadata(s, 0);
519 
520  for (i = 0; i < s->nb_streams; i++) {
522  }
523 
524  return 0;
525 }
526 
528 {
529  int64_t file_size;
530 
531  AVIOContext *pb = s->pb;
532  FLVContext *flv = s->priv_data;
533  int i;
534 
535  if (flv->flags & FLV_NO_SEQUENCE_END) {
536  av_log(s, AV_LOG_DEBUG, "FLV no sequence end mode open\n");
537  } else {
538  /* Add EOS tag */
539  for (i = 0; i < s->nb_streams; i++) {
540  AVCodecParameters *par = s->streams[i]->codecpar;
541  FLVStreamContext *sc = s->streams[i]->priv_data;
542  if (par->codec_type == AVMEDIA_TYPE_VIDEO &&
544  put_avc_eos_tag(pb, sc->last_ts);
545  }
546  }
547 
548  file_size = avio_tell(pb);
549 
550  if (pb->seekable) {
551  /* update information */
552  if (avio_seek(pb, flv->duration_offset, SEEK_SET) < 0) {
553  av_log(s, AV_LOG_WARNING, "Failed to update header with correct duration.\n");
554  } else {
555  put_amf_double(pb, flv->duration / (double)1000);
556  }
557  if (avio_seek(pb, flv->filesize_offset, SEEK_SET) < 0) {
558  av_log(s, AV_LOG_WARNING, "Failed to update header with correct filesize.\n");
559  } else {
560  put_amf_double(pb, file_size);
561  }
562  }
563  avio_seek(pb, file_size, SEEK_SET);
564  return 0;
565 }
566 
568 {
569  AVIOContext *pb = s->pb;
571  FLVContext *flv = s->priv_data;
573  unsigned ts;
574  int size = pkt->size;
575  uint8_t *data = NULL;
576  int flags = -1, flags_size, ret;
577 
578  if (par->codec_id == AV_CODEC_ID_VP6F || par->codec_id == AV_CODEC_ID_VP6A ||
580  flags_size = 2;
581  else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4)
582  flags_size = 5;
583  else
584  flags_size = 1;
585 
586  if (par->codec_id == AV_CODEC_ID_AAC || par->codec_id == AV_CODEC_ID_H264
587  || par->codec_id == AV_CODEC_ID_MPEG4) {
588  int side_size = 0;
590  if (side && side_size > 0 && (side_size != par->extradata_size || memcmp(side, par->extradata, side_size))) {
591  av_free(par->extradata);
593  memcpy(par->extradata, side, side_size);
594  par->extradata_size = side_size;
595  flv_write_codec_header(s, par);
596  }
597  }
598 
599  if (flv->delay == AV_NOPTS_VALUE)
600  flv->delay = -pkt->dts;
601 
602  if (pkt->dts < -flv->delay) {
604  "Packets are not in the proper order with respect to DTS\n");
605  return AVERROR(EINVAL);
606  }
607 
608  ts = pkt->dts;
609 
611  write_metadata(s, ts);
613  }
614 
617 
618  switch (par->codec_type) {
619  case AVMEDIA_TYPE_VIDEO:
621 
622  flags = ff_codec_get_tag(flv_video_codec_ids, par->codec_id);
623 
625  break;
626  case AVMEDIA_TYPE_AUDIO:
627  flags = get_audio_flags(s, par);
628 
629  av_assert0(size);
630 
632  break;
634  case AVMEDIA_TYPE_DATA:
636  break;
637  default:
638  return AVERROR(EINVAL);
639  }
640 
641  if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4) {
642  /* check if extradata looks like mp4 formatted */
643  if (par->extradata_size > 0 && *(uint8_t*)par->extradata != 1)
644  if ((ret = ff_avc_parse_nal_units_buf(pkt->data, &data, &size)) < 0)
645  return ret;
646  } else if (par->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
647  (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
648  if (!s->streams[pkt->stream_index]->nb_frames) {
649  av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
650  "use the audio bitstream filter 'aac_adtstoasc' to fix it "
651  "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
652  return AVERROR_INVALIDDATA;
653  }
654  av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
655  }
656 
657  /* check Speex packet duration */
658  if (par->codec_id == AV_CODEC_ID_SPEEX && ts - sc->last_ts > 160)
659  av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
660  "8 frames per packet. Adobe Flash "
661  "Player cannot handle this!\n");
662 
663  if (sc->last_ts < ts)
664  sc->last_ts = ts;
665 
666  if (size + flags_size >= 1<<24) {
667  av_log(s, AV_LOG_ERROR, "Too large packet with size %u >= %u\n",
668  size + flags_size, 1<<24);
669  return AVERROR(EINVAL);
670  }
671 
672  avio_wb24(pb, size + flags_size);
673  avio_wb24(pb, ts & 0xFFFFFF);
674  avio_w8(pb, (ts >> 24) & 0x7F); // timestamps are 32 bits _signed_
675  avio_wb24(pb, flv->reserved);
676 
677  if (par->codec_type == AVMEDIA_TYPE_DATA ||
679  int data_size;
680  int64_t metadata_size_pos = avio_tell(pb);
681  if (par->codec_id == AV_CODEC_ID_TEXT) {
682  // legacy FFmpeg magic?
684  put_amf_string(pb, "onTextData");
686  avio_wb32(pb, 2);
687  put_amf_string(pb, "type");
689  put_amf_string(pb, "Text");
690  put_amf_string(pb, "text");
692  put_amf_string(pb, pkt->data);
693  put_amf_string(pb, "");
695  } else {
696  // just pass the metadata through
697  avio_write(pb, data ? data : pkt->data, size);
698  }
699  /* write total size of tag */
700  data_size = avio_tell(pb) - metadata_size_pos;
701  avio_seek(pb, metadata_size_pos - 10, SEEK_SET);
702  avio_wb24(pb, data_size);
703  avio_seek(pb, data_size + 10 - 3, SEEK_CUR);
704  avio_wb32(pb, data_size + 11);
705  } else {
706  av_assert1(flags>=0);
707  avio_w8(pb,flags);
708  if (par->codec_id == AV_CODEC_ID_VP6)
709  avio_w8(pb,0);
710  if (par->codec_id == AV_CODEC_ID_VP6F || par->codec_id == AV_CODEC_ID_VP6A) {
711  if (par->extradata_size)
712  avio_w8(pb, par->extradata[0]);
713  else
714  avio_w8(pb, ((FFALIGN(par->width, 16) - par->width) << 4) |
715  (FFALIGN(par->height, 16) - par->height));
716  } else if (par->codec_id == AV_CODEC_ID_AAC)
717  avio_w8(pb, 1); // AAC raw
718  else if (par->codec_id == AV_CODEC_ID_H264 || par->codec_id == AV_CODEC_ID_MPEG4) {
719  avio_w8(pb, 1); // AVC NALU
720  avio_wb24(pb, pkt->pts - pkt->dts);
721  }
722 
723  avio_write(pb, data ? data : pkt->data, size);
724 
725  avio_wb32(pb, size + flags_size + 11); // previous tag size
726  flv->duration = FFMAX(flv->duration,
727  pkt->pts + flv->delay + pkt->duration);
728  }
729 
730  av_free(data);
731 
732  return pb->error;
733 }
734 
735 static const AVOption options[] = {
736  { "flvflags", "FLV muxer flags", offsetof(FLVContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "flvflags" },
737  { "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" },
738  { "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" },
739  { NULL },
740 };
741 
742 static const AVClass flv_muxer_class = {
743  .class_name = "flv muxer",
744  .item_name = av_default_item_name,
745  .option = options,
746  .version = LIBAVUTIL_VERSION_INT,
747 };
748 
750  .name = "flv",
751  .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
752  .mime_type = "video/x-flv",
753  .extensions = "flv",
754  .priv_data_size = sizeof(FLVContext),
755  .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_ADPCM_SWF,
756  .video_codec = AV_CODEC_ID_FLV1,
760  .codec_tag = (const AVCodecTag* const []) {
762  },
765  .priv_class = &flv_muxer_class,
766 };
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:1008
AVOutputFormat ff_flv_muxer
Definition: flvenc.c:749
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:356
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:4560
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:3980
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1602
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:345
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:742
int event_flags
Flags for the user to detect events happening on the file.
Definition: avformat.h:1629
void * priv_data
Definition: avformat.h:904
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:3002
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:1622
#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:3972
static const AVCodecTag flv_video_codec_ids[]
Definition: flvenc.c:37
static const AVOption options[]
Definition: flvenc.c:735
Format I/O context.
Definition: avformat.h:1338
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:4046
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:1619
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:1406
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:1601
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:4009
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:2898
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1633
static int flv_write_trailer(AVFormatContext *s)
Definition: flvenc.c:527
#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:1554
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:3976
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:5279
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:967
#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:1607
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:2977
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3998
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1394
#define FLV_AUDIO_CODECID_OFFSET
Definition: flv.h:34
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:243
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:567
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:3146
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:1372
FLV common header.
key frame (for AVC, a seekable frame)
Definition: flv.h:115
AVIOContext * pb
I/O context.
Definition: avformat.h:1380
static int flv_write_header(AVFormatContext *s)
Definition: flvenc.c:419
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
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
Definition: ffmpeg.c:645
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:668
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:660
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:4090
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:4040
int flags
Definition: flvenc.c:82
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:945
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:734
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:1366
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:4022
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3994
int channels
Audio only.
Definition: avcodec.h:4086
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1600
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:1433
#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:1241
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:3984
int stream_index
Definition: avcodec.h:1603
This structure stores compressed data.
Definition: avcodec.h:1578
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1594
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