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 "avc.h"
27 #include "avformat.h"
28 #include "flv.h"
29 #include "internal.h"
30 #include "metadata.h"
31 #include "libavutil/opt.h"
32 #include "libavcodec/put_bits.h"
33 #include "libavcodec/aacenctab.h"
34 
35 
36 static const AVCodecTag flv_video_codec_ids[] = {
46  { AV_CODEC_ID_NONE, 0 }
47 };
48 
49 static const AVCodecTag flv_audio_codec_ids[] = {
60  { AV_CODEC_ID_NONE, 0 }
61 };
62 
63 typedef struct FLVContext {
65  int reserved;
66  int64_t duration_offset;
67  int64_t filesize_offset;
68  int64_t duration;
69  int64_t delay; ///< first dts delay (needed for AVC & Speex)
70 
73  double framerate;
75 
76  int flags;
77 } FLVContext;
78 
79 typedef struct FLVStreamContext {
80  int64_t last_ts; ///< last timestamp for each stream
82 
84 {
87 
88  if (enc->codec_id == AV_CODEC_ID_AAC) // specs force these parameters
91  else if (enc->codec_id == AV_CODEC_ID_SPEEX) {
92  if (enc->sample_rate != 16000) {
94  "FLV only supports wideband (16kHz) Speex audio\n");
95  return AVERROR(EINVAL);
96  }
97  if (enc->channels != 1) {
98  av_log(s, AV_LOG_ERROR, "FLV only supports mono Speex audio\n");
99  return AVERROR(EINVAL);
100  }
102  } else {
103  switch (enc->sample_rate) {
104  case 44100:
105  flags |= FLV_SAMPLERATE_44100HZ;
106  break;
107  case 22050:
108  flags |= FLV_SAMPLERATE_22050HZ;
109  break;
110  case 11025:
111  flags |= FLV_SAMPLERATE_11025HZ;
112  break;
113  case 16000: // nellymoser only
114  case 8000: // nellymoser only
115  case 5512: // not MP3
116  if (enc->codec_id != AV_CODEC_ID_MP3) {
117  flags |= FLV_SAMPLERATE_SPECIAL;
118  break;
119  }
120  default:
121  av_log(s, AV_LOG_ERROR,
122  "FLV does not support sample rate %d, "
123  "choose from (44100, 22050, 11025)\n", enc->sample_rate);
124  return AVERROR(EINVAL);
125  }
126  }
127 
128  if (enc->channels > 1)
129  flags |= FLV_STEREO;
130 
131  switch (enc->codec_id) {
132  case AV_CODEC_ID_MP3:
134  break;
135  case AV_CODEC_ID_PCM_U8:
137  break;
140  break;
143  break;
146  break;
148  if (enc->sample_rate == 8000)
150  else if (enc->sample_rate == 16000)
152  else
154  break;
157  break;
160  break;
161  case 0:
162  flags |= enc->codec_tag << 4;
163  break;
164  default:
165  av_log(s, AV_LOG_ERROR, "Audio codec '%s' not compatible with FLV\n",
166  avcodec_get_name(enc->codec_id));
167  return AVERROR(EINVAL);
168  }
169 
170  return flags;
171 }
172 
173 static void put_amf_string(AVIOContext *pb, const char *str)
174 {
175  size_t len = strlen(str);
176  avio_wb16(pb, len);
177  avio_write(pb, str, len);
178 }
179 
180 static void put_avc_eos_tag(AVIOContext *pb, unsigned ts)
181 {
183  avio_wb24(pb, 5); /* Tag Data Size */
184  avio_wb24(pb, ts); /* lower 24 bits of timestamp in ms */
185  avio_w8(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms */
186  avio_wb24(pb, 0); /* StreamId = 0 */
187  avio_w8(pb, 23); /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
188  avio_w8(pb, 2); /* AVC end of sequence */
189  avio_wb24(pb, 0); /* Always 0 for AVC EOS. */
190  avio_wb32(pb, 16); /* Size of FLV tag */
191 }
192 
193 static void put_amf_double(AVIOContext *pb, double d)
194 {
196  avio_wb64(pb, av_double2int(d));
197 }
198 
199 static void put_amf_bool(AVIOContext *pb, int b)
200 {
202  avio_w8(pb, !!b);
203 }
204 
205 static void write_metadata(AVFormatContext *s, unsigned int ts)
206 {
207  AVIOContext *pb = s->pb;
208  FLVContext *flv = s->priv_data;
209  int metadata_count = 0;
210  int64_t metadata_size_pos, data_size, metadata_count_pos;
212 
213  /* write meta_tag */
214  avio_w8(pb, 18); // tag type META
215  metadata_size_pos = avio_tell(pb);
216  avio_wb24(pb, 0); // size of data part (sum of all parts below)
217  avio_wb24(pb, ts); // timestamp
218  avio_wb32(pb, 0); // reserved
219 
220  /* now data of data_size size */
221 
222  /* first event name as a string */
224  put_amf_string(pb, "onMetaData"); // 12 bytes
225 
226  /* mixed array (hash) with size and string/type/data tuples */
228  metadata_count_pos = avio_tell(pb);
229  metadata_count = 4 * !!flv->video_enc +
230  5 * !!flv->audio_enc +
231  1 * !!flv->data_enc +
232  2; // +2 for duration and file size
233 
234  avio_wb32(pb, metadata_count);
235 
236  put_amf_string(pb, "duration");
237  flv->duration_offset = avio_tell(pb);
238 
239  // fill in the guessed duration, it'll be corrected later if incorrect
241 
242  if (flv->video_enc) {
243  put_amf_string(pb, "width");
244  put_amf_double(pb, flv->video_enc->width);
245 
246  put_amf_string(pb, "height");
247  put_amf_double(pb, flv->video_enc->height);
248 
249  put_amf_string(pb, "videodatarate");
250  put_amf_double(pb, flv->video_enc->bit_rate / 1024.0);
251 
252  if (flv->framerate != 0.0) {
253  put_amf_string(pb, "framerate");
254  put_amf_double(pb, flv->framerate);
255  metadata_count++;
256  }
257 
258  put_amf_string(pb, "videocodecid");
260  }
261 
262  if (flv->audio_enc) {
263  put_amf_string(pb, "audiodatarate");
264  put_amf_double(pb, flv->audio_enc->bit_rate / 1024.0);
265 
266  put_amf_string(pb, "audiosamplerate");
268 
269  put_amf_string(pb, "audiosamplesize");
270  put_amf_double(pb, flv->audio_enc->codec_id == AV_CODEC_ID_PCM_U8 ? 8 : 16);
271 
272  put_amf_string(pb, "stereo");
273  put_amf_bool(pb, flv->audio_enc->channels == 2);
274 
275  put_amf_string(pb, "audiocodecid");
277  }
278 
279  if (flv->data_enc) {
280  put_amf_string(pb, "datastream");
281  put_amf_double(pb, 0.0);
282  }
283 
284  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
285  if( !strcmp(tag->key, "width")
286  ||!strcmp(tag->key, "height")
287  ||!strcmp(tag->key, "videodatarate")
288  ||!strcmp(tag->key, "framerate")
289  ||!strcmp(tag->key, "videocodecid")
290  ||!strcmp(tag->key, "audiodatarate")
291  ||!strcmp(tag->key, "audiosamplerate")
292  ||!strcmp(tag->key, "audiosamplesize")
293  ||!strcmp(tag->key, "stereo")
294  ||!strcmp(tag->key, "audiocodecid")
295  ||!strcmp(tag->key, "duration")
296  ||!strcmp(tag->key, "onMetaData")
297  ||!strcmp(tag->key, "datasize")
298  ||!strcmp(tag->key, "lasttimestamp")
299  ||!strcmp(tag->key, "totalframes")
300  ||!strcmp(tag->key, "hasAudio")
301  ||!strcmp(tag->key, "hasVideo")
302  ||!strcmp(tag->key, "hasCuePoints")
303  ||!strcmp(tag->key, "hasMetadata")
304  ||!strcmp(tag->key, "hasKeyframes")
305  ){
306  av_log(s, AV_LOG_DEBUG, "Ignoring metadata for %s\n", tag->key);
307  continue;
308  }
309  put_amf_string(pb, tag->key);
311  put_amf_string(pb, tag->value);
312  metadata_count++;
313  }
314 
315  put_amf_string(pb, "filesize");
316  flv->filesize_offset = avio_tell(pb);
317  put_amf_double(pb, 0); // delayed write
318 
319  put_amf_string(pb, "");
321 
322  /* write total size of tag */
323  data_size = avio_tell(pb) - metadata_size_pos - 10;
324 
325  avio_seek(pb, metadata_count_pos, SEEK_SET);
326  avio_wb32(pb, metadata_count);
327 
328  avio_seek(pb, metadata_size_pos, SEEK_SET);
329  avio_wb24(pb, data_size);
330  avio_skip(pb, data_size + 10 - 3);
331  avio_wb32(pb, data_size + 11);
332 }
333 
335  const char* type, int codec_id)
336 {
337  const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
338  av_log(s, AV_LOG_ERROR,
339  "%s codec %s not compatible with flv\n",
340  type,
341  desc ? desc->name : "unknown");
342  return AVERROR(ENOSYS);
343 }
344 
346 {
347  int i;
348  AVIOContext *pb = s->pb;
349  FLVContext *flv = s->priv_data;
350  int64_t data_size;
351 
352  for (i = 0; i < s->nb_streams; i++) {
353  AVCodecContext *enc = s->streams[i]->codec;
354  FLVStreamContext *sc;
355  switch (enc->codec_type) {
356  case AVMEDIA_TYPE_VIDEO:
357  if (s->streams[i]->avg_frame_rate.den &&
358  s->streams[i]->avg_frame_rate.num) {
359  flv->framerate = av_q2d(s->streams[i]->avg_frame_rate);
360  }
361  if (flv->video_enc) {
362  av_log(s, AV_LOG_ERROR,
363  "at most one video stream is supported in flv\n");
364  return AVERROR(EINVAL);
365  }
366  flv->video_enc = enc;
367  if (!ff_codec_get_tag(flv_video_codec_ids, enc->codec_id))
368  return unsupported_codec(s, "Video", enc->codec_id);
369 
370  if (enc->codec_id == AV_CODEC_ID_MPEG4 ||
371  enc->codec_id == AV_CODEC_ID_H263) {
373  av_log(s, error ? AV_LOG_ERROR : AV_LOG_WARNING,
374  "Codec %s is not supported in the official FLV specification,\n", avcodec_get_name(enc->codec_id));
375 
376  if (error) {
377  av_log(s, AV_LOG_ERROR,
378  "use vstrict=-1 / -strict -1 to use it anyway.\n");
379  return AVERROR(EINVAL);
380  }
381  } else if (enc->codec_id == AV_CODEC_ID_VP6) {
383  "Muxing VP6 in flv will produce flipped video on playback.\n");
384  }
385  break;
386  case AVMEDIA_TYPE_AUDIO:
387  if (flv->audio_enc) {
388  av_log(s, AV_LOG_ERROR,
389  "at most one audio stream is supported in flv\n");
390  return AVERROR(EINVAL);
391  }
392  flv->audio_enc = enc;
393  if (get_audio_flags(s, enc) < 0)
394  return unsupported_codec(s, "Audio", enc->codec_id);
395  if (enc->codec_id == AV_CODEC_ID_PCM_S16BE)
397  "16-bit big-endian audio in flv is valid but most likely unplayable (hardware dependent); use s16le\n");
398  break;
399  case AVMEDIA_TYPE_DATA:
400  if (enc->codec_id != AV_CODEC_ID_TEXT && enc->codec_id != AV_CODEC_ID_NONE)
401  return unsupported_codec(s, "Data", enc->codec_id);
402  flv->data_enc = enc;
403  break;
405  if (enc->codec_id != AV_CODEC_ID_TEXT) {
406  av_log(s, AV_LOG_ERROR, "Subtitle codec '%s' for stream %d is not compatible with FLV\n",
407  avcodec_get_name(enc->codec_id), i);
408  return AVERROR_INVALIDDATA;
409  }
410  flv->data_enc = enc;
411  break;
412  default:
413  av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n",
415  return AVERROR(EINVAL);
416  }
417  avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
418 
419  sc = av_mallocz(sizeof(FLVStreamContext));
420  if (!sc)
421  return AVERROR(ENOMEM);
422  s->streams[i]->priv_data = sc;
423  sc->last_ts = -1;
424  }
425 
426  flv->delay = AV_NOPTS_VALUE;
427 
428  avio_write(pb, "FLV", 3);
429  avio_w8(pb, 1);
432  avio_wb32(pb, 9);
433  avio_wb32(pb, 0);
434 
435  for (i = 0; i < s->nb_streams; i++)
436  if (s->streams[i]->codec->codec_tag == 5) {
437  avio_w8(pb, 8); // message type
438  avio_wb24(pb, 0); // include flags
439  avio_wb24(pb, 0); // time stamp
440  avio_wb32(pb, 0); // reserved
441  avio_wb32(pb, 11); // size
442  flv->reserved = 5;
443  }
444 
445  write_metadata(s, 0);
446 
447  for (i = 0; i < s->nb_streams; i++) {
448  AVCodecContext *enc = s->streams[i]->codec;
449  if (enc->codec_id == AV_CODEC_ID_AAC || enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
450  int64_t pos;
453  avio_wb24(pb, 0); // size patched later
454  avio_wb24(pb, 0); // ts
455  avio_w8(pb, 0); // ts ext
456  avio_wb24(pb, 0); // streamid
457  pos = avio_tell(pb);
458  if (enc->codec_id == AV_CODEC_ID_AAC) {
459  avio_w8(pb, get_audio_flags(s, enc));
460  avio_w8(pb, 0); // AAC sequence header
461 
462  if (!enc->extradata_size && flv->flags & 1) {
463  PutBitContext pbc;
464  int samplerate_index;
465  int channels = flv->audio_enc->channels - (flv->audio_enc->channels == 8 ? 1 : 0);
466  uint8_t data[2];
467 
468  for (samplerate_index = 0; samplerate_index < 16; samplerate_index++)
469  if (flv->audio_enc->sample_rate == mpeg4audio_sample_rates[samplerate_index])
470  break;
471 
472  init_put_bits(&pbc, data, sizeof(data));
473  put_bits(&pbc, 5, flv->audio_enc->profile + 1); //profile
474  put_bits(&pbc, 4, samplerate_index); //sample rate index
475  put_bits(&pbc, 4, channels);
476  put_bits(&pbc, 1, 0); //frame length - 1024 samples
477  put_bits(&pbc, 1, 0); //does not depend on core coder
478  put_bits(&pbc, 1, 0); //is not extension
479  flush_put_bits(&pbc);
480 
481  avio_w8(pb, data[0]);
482  avio_w8(pb, data[1]);
483 
484  av_log(s, AV_LOG_WARNING, "AAC sequence header: %02x %02x.\n", data[0], data[1]);
485  }
486  avio_write(pb, enc->extradata, enc->extradata_size);
487  } else {
488  avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
489  avio_w8(pb, 0); // AVC sequence header
490  avio_wb24(pb, 0); // composition time
492  }
493  data_size = avio_tell(pb) - pos;
494  avio_seek(pb, -data_size - 10, SEEK_CUR);
495  avio_wb24(pb, data_size);
496  avio_skip(pb, data_size + 10 - 3);
497  avio_wb32(pb, data_size + 11); // previous tag size
498  }
499  }
500 
501  return 0;
502 }
503 
505 {
506  int64_t file_size;
507 
508  AVIOContext *pb = s->pb;
509  FLVContext *flv = s->priv_data;
510  int i;
511 
512  /* Add EOS tag */
513  for (i = 0; i < s->nb_streams; i++) {
514  AVCodecContext *enc = s->streams[i]->codec;
515  FLVStreamContext *sc = s->streams[i]->priv_data;
516  if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
518  put_avc_eos_tag(pb, sc->last_ts);
519  }
520 
521  file_size = avio_tell(pb);
522 
523  /* update information */
524  if (avio_seek(pb, flv->duration_offset, SEEK_SET) < 0)
525  av_log(s, AV_LOG_WARNING, "Failed to update header with correct duration.\n");
526  else
527  put_amf_double(pb, flv->duration / (double)1000);
528  if (avio_seek(pb, flv->filesize_offset, SEEK_SET) < 0)
529  av_log(s, AV_LOG_WARNING, "Failed to update header with correct filesize.\n");
530  else
531  put_amf_double(pb, file_size);
532 
533  avio_seek(pb, file_size, SEEK_SET);
534  return 0;
535 }
536 
538 {
539  AVIOContext *pb = s->pb;
540  AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
541  FLVContext *flv = s->priv_data;
543  unsigned ts;
544  int size = pkt->size;
545  uint8_t *data = NULL;
546  int flags = -1, flags_size, ret;
547 
548  if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A ||
550  flags_size = 2;
551  else if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4)
552  flags_size = 5;
553  else
554  flags_size = 1;
555 
556  if (flv->delay == AV_NOPTS_VALUE)
557  flv->delay = -pkt->dts;
558 
559  if (pkt->dts < -flv->delay) {
561  "Packets are not in the proper order with respect to DTS\n");
562  return AVERROR(EINVAL);
563  }
564 
565  ts = pkt->dts;
566 
568  write_metadata(s, ts);
570  }
571 
572  switch (enc->codec_type) {
573  case AVMEDIA_TYPE_VIDEO:
575 
576  flags = ff_codec_get_tag(flv_video_codec_ids, enc->codec_id);
577 
579  break;
580  case AVMEDIA_TYPE_AUDIO:
581  flags = get_audio_flags(s, enc);
582 
583  av_assert0(size);
584 
586  break;
588  case AVMEDIA_TYPE_DATA:
590  break;
591  default:
592  return AVERROR(EINVAL);
593  }
594 
595  if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
596  /* check if extradata looks like mp4 formatted */
597  if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1)
598  if ((ret = ff_avc_parse_nal_units_buf(pkt->data, &data, &size)) < 0)
599  return ret;
600  } else if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
601  (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
602  if (!s->streams[pkt->stream_index]->nb_frames) {
603  av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
604  "use the audio bitstream filter 'aac_adtstoasc' to fix it "
605  "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
606  return AVERROR_INVALIDDATA;
607  }
608  av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
609  }
610 
611  /* check Speex packet duration */
612  if (enc->codec_id == AV_CODEC_ID_SPEEX && ts - sc->last_ts > 160)
613  av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
614  "8 frames per packet. Adobe Flash "
615  "Player cannot handle this!\n");
616 
617  if (sc->last_ts < ts)
618  sc->last_ts = ts;
619 
620  if (size + flags_size >= 1<<24) {
621  av_log(s, AV_LOG_ERROR, "Too large packet with size %u >= %u\n",
622  size + flags_size, 1<<24);
623  return AVERROR(EINVAL);
624  }
625 
626  avio_wb24(pb, size + flags_size);
627  avio_wb24(pb, ts & 0xFFFFFF);
628  avio_w8(pb, (ts >> 24) & 0x7F); // timestamps are 32 bits _signed_
629  avio_wb24(pb, flv->reserved);
630 
631  if (enc->codec_type == AVMEDIA_TYPE_DATA ||
633  int data_size;
634  int64_t metadata_size_pos = avio_tell(pb);
635  if (enc->codec_id == AV_CODEC_ID_TEXT) {
636  // legacy FFmpeg magic?
638  put_amf_string(pb, "onTextData");
640  avio_wb32(pb, 2);
641  put_amf_string(pb, "type");
643  put_amf_string(pb, "Text");
644  put_amf_string(pb, "text");
646  put_amf_string(pb, pkt->data);
647  put_amf_string(pb, "");
649  } else {
650  // just pass the metadata through
651  avio_write(pb, data ? data : pkt->data, size);
652  }
653  /* write total size of tag */
654  data_size = avio_tell(pb) - metadata_size_pos;
655  avio_seek(pb, metadata_size_pos - 10, SEEK_SET);
656  avio_wb24(pb, data_size);
657  avio_seek(pb, data_size + 10 - 3, SEEK_CUR);
658  avio_wb32(pb, data_size + 11);
659  } else {
660  av_assert1(flags>=0);
661  avio_w8(pb,flags);
662  if (enc->codec_id == AV_CODEC_ID_VP6)
663  avio_w8(pb,0);
664  if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A) {
665  if (enc->extradata_size)
666  avio_w8(pb, enc->extradata[0]);
667  else
668  avio_w8(pb, ((FFALIGN(enc->width, 16) - enc->width) << 4) |
669  (FFALIGN(enc->height, 16) - enc->height));
670  } else if (enc->codec_id == AV_CODEC_ID_AAC)
671  avio_w8(pb, 1); // AAC raw
672  else if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
673  avio_w8(pb, 1); // AVC NALU
674  avio_wb24(pb, pkt->pts - pkt->dts);
675  }
676 
677  avio_write(pb, data ? data : pkt->data, size);
678 
679  avio_wb32(pb, size + flags_size + 11); // previous tag size
680  flv->duration = FFMAX(flv->duration,
681  pkt->pts + flv->delay + pkt->duration);
682  }
683 
684  av_free(data);
685 
686  return pb->error;
687 }
688 
689 static const AVOption options[] = {
690  { "flvflags", "FLV muxer flags", offsetof(FLVContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "flvflags" },
691  { "aac_seq_header_detect", "Put AAC sequence header based on stream data", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "flvflags" },
692  { NULL },
693 };
694 
695 static const AVClass flv_muxer_class = {
696  .class_name = "flv muxer",
697  .item_name = av_default_item_name,
698  .option = options,
699  .version = LIBAVUTIL_VERSION_INT,
700 };
701 
703  .name = "flv",
704  .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
705  .mime_type = "video/x-flv",
706  .extensions = "flv",
707  .priv_data_size = sizeof(FLVContext),
708  .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_ADPCM_SWF,
709  .video_codec = AV_CODEC_ID_FLV1,
713  .codec_tag = (const AVCodecTag* const []) {
715  },
718  .priv_class = &flv_muxer_class,
719 };
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:418
#define NULL
Definition: coverity.c:32
static const AVCodecTag flv_audio_codec_ids[]
Definition: flvenc.c:49
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVSTREAM_EVENT_FLAG_METADATA_UPDATED
The call resulted in updated metadata.
Definition: avformat.h:1001
AVOutputFormat ff_flv_muxer
Definition: flvenc.c:702
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
double framerate
Definition: flvenc.c:73
int64_t delay
first dts delay (needed for AVC & Speex)
Definition: flvenc.c:69
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:168
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1597
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
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:4149
int64_t duration
Definition: flvenc.c:68
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1468
const char * b
Definition: vf_curves.c:109
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:208
static int unsupported_codec(AVFormatContext *s, const char *type, int codec_id)
Definition: flvenc.c:334
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:695
int event_flags
Flags for the user to detect events happening on the file.
Definition: avformat.h:1603
void * priv_data
Definition: avformat.h:897
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:282
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:2811
key frame (for AVC, a seekable frame)
Definition: flv.h:115
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
int profile
profile
Definition: avcodec.h:3028
static int get_audio_flags(AVFormatContext *s, AVCodecContext *enc)
Definition: flvenc.c:83
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1596
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:495
static const AVCodecTag flv_video_codec_ids[]
Definition: flvenc.c:36
static const AVOption options[]
Definition: flvenc.c:689
Format I/O context.
Definition: avformat.h:1314
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:195
AVOptions.
Definition: flv.h:74
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1485
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1647
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1382
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:80
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:39
uint8_t * data
Definition: avcodec.h:1467
uint32_t tag
Definition: movenc.c:1348
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:442
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2917
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:182
#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
#define FF_COMPLIANCE_UNOFFICIAL
Allow unofficial extensions.
Definition: avcodec.h:2745
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1499
static int flv_write_trailer(AVFormatContext *s)
Definition: flvenc.c:504
#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:1528
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:420
static const int mpeg4audio_sample_rates[16]
Definition: aacenctab.h:74
simple assert() macros that are a bit more flexible than ISO C assert().
AVCodecContext * data_enc
Definition: flvenc.c:74
enum AVCodecID codec_id
Definition: mov_chan.c:433
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:960
#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:1473
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:2900
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1370
#define FLV_AUDIO_CODECID_OFFSET
Definition: flv.h:34
signifies 5512Hz and 8000Hz in the case of NELLYMOSER
Definition: flv.h:83
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:246
static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: flvenc.c:537
int width
picture width / height.
Definition: avcodec.h:1711
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:484
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:442
const char * name
Definition: avformat.h:523
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:2648
int reserved
Definition: flvenc.c:65
FLV common header.
enum AVMediaType codec_type
Definition: avcodec.h:1540
enum AVCodecID codec_id
Definition: avcodec.h:1549
int sample_rate
samples per second
Definition: avcodec.h:2287
AVIOContext * pb
I/O context.
Definition: avformat.h:1356
static int flv_write_header(AVFormatContext *s)
Definition: flvenc.c:345
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:160
main external API structure.
Definition: avcodec.h:1532
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1564
static void put_amf_string(AVIOContext *pb, const char *str)
Definition: flvenc.c:173
GLint GLenum type
Definition: opengl_enc.c:105
int extradata_size
Definition: avcodec.h:1648
static void put_amf_double(AVIOContext *pb, double d)
Definition: flvenc.c:193
AVClass * av_class
Definition: flvenc.c:64
Describe the class of an AVClass context structure.
Definition: log.h:67
AVCodecContext * audio_enc
Definition: flvenc.c:71
AVCodecContext * video_enc
Definition: flvenc.c:72
const char * name
Name of the codec described by this descriptor.
Definition: avcodec.h:569
int error
contains the error code or 0 if no error happened
Definition: avio.h:192
This struct describes the properties of a single codec described by an AVCodecID. ...
Definition: avcodec.h:561
AAC encoder data.
#define AMF_END_OF_OBJECT
Definition: flv.h:47
static void put_amf_bool(AVIOContext *pb, int b)
Definition: flvenc.c:199
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:430
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
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:66
raw UTF-8 text
Definition: avcodec.h:508
static void write_metadata(AVFormatContext *s, unsigned int ts)
Definition: flvenc.c:205
int flags
Definition: flvenc.c:76
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:938
char * key
Definition: dict.h:87
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:45
static void put_avc_eos_tag(AVIOContext *pb, unsigned ts)
Definition: flvenc.c:180
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:488
#define av_free(p)
char * value
Definition: dict.h:88
int len
int channels
number of audio channels
Definition: avcodec.h:2288
void * priv_data
Format private data.
Definition: avformat.h:1342
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:497
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1466
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:332
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1409
#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:72
int64_t filesize_offset
Definition: flvenc.c:67
int stream_index
Definition: avcodec.h:1469
This structure stores compressed data.
Definition: avcodec.h:1444
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1460
int64_t last_ts
last timestamp for each stream
Definition: flvenc.c:80
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:240
inter frame (for AVC, a non-seekable frame)
Definition: flv.h:116
bitstream writer API