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 
32 
33 static const AVCodecTag flv_video_codec_ids[] = {
43  { AV_CODEC_ID_NONE, 0 }
44 };
45 
46 static const AVCodecTag flv_audio_codec_ids[] = {
57  { AV_CODEC_ID_NONE, 0 }
58 };
59 
60 typedef struct FLVContext {
61  int reserved;
62  int64_t duration_offset;
63  int64_t filesize_offset;
64  int64_t duration;
65  int64_t delay; ///< first dts delay (needed for AVC & Speex)
66 
69  double framerate;
71 } FLVContext;
72 
73 typedef struct FLVStreamContext {
74  int64_t last_ts; ///< last timestamp for each stream
76 
78 {
81 
82  if (enc->codec_id == AV_CODEC_ID_AAC) // specs force these parameters
85  else if (enc->codec_id == AV_CODEC_ID_SPEEX) {
86  if (enc->sample_rate != 16000) {
88  "FLV only supports wideband (16kHz) Speex audio\n");
89  return AVERROR(EINVAL);
90  }
91  if (enc->channels != 1) {
92  av_log(s, AV_LOG_ERROR, "FLV only supports mono Speex audio\n");
93  return AVERROR(EINVAL);
94  }
96  } else {
97  switch (enc->sample_rate) {
98  case 44100:
99  flags |= FLV_SAMPLERATE_44100HZ;
100  break;
101  case 22050:
102  flags |= FLV_SAMPLERATE_22050HZ;
103  break;
104  case 11025:
105  flags |= FLV_SAMPLERATE_11025HZ;
106  break;
107  case 16000: // nellymoser only
108  case 8000: // nellymoser only
109  case 5512: // not MP3
110  if (enc->codec_id != AV_CODEC_ID_MP3) {
111  flags |= FLV_SAMPLERATE_SPECIAL;
112  break;
113  }
114  default:
115  av_log(s, AV_LOG_ERROR,
116  "FLV does not support sample rate %d, "
117  "choose from (44100, 22050, 11025)\n", enc->sample_rate);
118  return AVERROR(EINVAL);
119  }
120  }
121 
122  if (enc->channels > 1)
123  flags |= FLV_STEREO;
124 
125  switch (enc->codec_id) {
126  case AV_CODEC_ID_MP3:
128  break;
129  case AV_CODEC_ID_PCM_U8:
131  break;
134  break;
137  break;
140  break;
142  if (enc->sample_rate == 8000)
144  else if (enc->sample_rate == 16000)
146  else
148  break;
151  break;
154  break;
155  case 0:
156  flags |= enc->codec_tag << 4;
157  break;
158  default:
159  av_log(s, AV_LOG_ERROR, "Audio codec '%s' not compatible with FLV\n",
160  avcodec_get_name(enc->codec_id));
161  return AVERROR(EINVAL);
162  }
163 
164  return flags;
165 }
166 
167 static void put_amf_string(AVIOContext *pb, const char *str)
168 {
169  size_t len = strlen(str);
170  avio_wb16(pb, len);
171  avio_write(pb, str, len);
172 }
173 
174 static void put_avc_eos_tag(AVIOContext *pb, unsigned ts)
175 {
177  avio_wb24(pb, 5); /* Tag Data Size */
178  avio_wb24(pb, ts); /* lower 24 bits of timestamp in ms */
179  avio_w8(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms */
180  avio_wb24(pb, 0); /* StreamId = 0 */
181  avio_w8(pb, 23); /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
182  avio_w8(pb, 2); /* AVC end of sequence */
183  avio_wb24(pb, 0); /* Always 0 for AVC EOS. */
184  avio_wb32(pb, 16); /* Size of FLV tag */
185 }
186 
187 static void put_amf_double(AVIOContext *pb, double d)
188 {
190  avio_wb64(pb, av_double2int(d));
191 }
192 
193 static void put_amf_bool(AVIOContext *pb, int b)
194 {
196  avio_w8(pb, !!b);
197 }
198 
199 static void write_metadata(AVFormatContext *s, unsigned int ts)
200 {
201  AVIOContext *pb = s->pb;
202  FLVContext *flv = s->priv_data;
203  int metadata_count = 0;
204  int64_t metadata_size_pos, data_size, metadata_count_pos;
205  AVDictionaryEntry *tag = NULL;
206 
207  /* write meta_tag */
208  avio_w8(pb, 18); // tag type META
209  metadata_size_pos = avio_tell(pb);
210  avio_wb24(pb, 0); // size of data part (sum of all parts below)
211  avio_wb24(pb, ts); // timestamp
212  avio_wb32(pb, 0); // reserved
213 
214  /* now data of data_size size */
215 
216  /* first event name as a string */
218  put_amf_string(pb, "onMetaData"); // 12 bytes
219 
220  /* mixed array (hash) with size and string/type/data tuples */
222  metadata_count_pos = avio_tell(pb);
223  metadata_count = 4 * !!flv->video_enc +
224  5 * !!flv->audio_enc +
225  1 * !!flv->data_enc +
226  2; // +2 for duration and file size
227 
228  avio_wb32(pb, metadata_count);
229 
230  put_amf_string(pb, "duration");
231  flv->duration_offset = avio_tell(pb);
232 
233  // fill in the guessed duration, it'll be corrected later if incorrect
235 
236  if (flv->video_enc) {
237  put_amf_string(pb, "width");
238  put_amf_double(pb, flv->video_enc->width);
239 
240  put_amf_string(pb, "height");
241  put_amf_double(pb, flv->video_enc->height);
242 
243  put_amf_string(pb, "videodatarate");
244  put_amf_double(pb, flv->video_enc->bit_rate / 1024.0);
245 
246  if (flv->framerate != 0.0) {
247  put_amf_string(pb, "framerate");
248  put_amf_double(pb, flv->framerate);
249  metadata_count++;
250  }
251 
252  put_amf_string(pb, "videocodecid");
254  }
255 
256  if (flv->audio_enc) {
257  put_amf_string(pb, "audiodatarate");
258  put_amf_double(pb, flv->audio_enc->bit_rate / 1024.0);
259 
260  put_amf_string(pb, "audiosamplerate");
262 
263  put_amf_string(pb, "audiosamplesize");
264  put_amf_double(pb, flv->audio_enc->codec_id == AV_CODEC_ID_PCM_U8 ? 8 : 16);
265 
266  put_amf_string(pb, "stereo");
267  put_amf_bool(pb, flv->audio_enc->channels == 2);
268 
269  put_amf_string(pb, "audiocodecid");
271  }
272 
273  if (flv->data_enc) {
274  put_amf_string(pb, "datastream");
275  put_amf_double(pb, 0.0);
276  }
277 
278  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
279  if( !strcmp(tag->key, "width")
280  ||!strcmp(tag->key, "height")
281  ||!strcmp(tag->key, "videodatarate")
282  ||!strcmp(tag->key, "framerate")
283  ||!strcmp(tag->key, "videocodecid")
284  ||!strcmp(tag->key, "audiodatarate")
285  ||!strcmp(tag->key, "audiosamplerate")
286  ||!strcmp(tag->key, "audiosamplesize")
287  ||!strcmp(tag->key, "stereo")
288  ||!strcmp(tag->key, "audiocodecid")
289  ||!strcmp(tag->key, "duration")
290  ||!strcmp(tag->key, "onMetaData")
291  ||!strcmp(tag->key, "datasize")
292  ||!strcmp(tag->key, "lasttimestamp")
293  ||!strcmp(tag->key, "totalframes")
294  ||!strcmp(tag->key, "hasAudio")
295  ||!strcmp(tag->key, "hasVideo")
296  ||!strcmp(tag->key, "hasCuePoints")
297  ||!strcmp(tag->key, "hasMetadata")
298  ||!strcmp(tag->key, "hasKeyframes")
299  ){
300  av_log(s, AV_LOG_DEBUG, "Ignoring metadata for %s\n", tag->key);
301  continue;
302  }
303  put_amf_string(pb, tag->key);
305  put_amf_string(pb, tag->value);
306  metadata_count++;
307  }
308 
309  put_amf_string(pb, "filesize");
310  flv->filesize_offset = avio_tell(pb);
311  put_amf_double(pb, 0); // delayed write
312 
313  put_amf_string(pb, "");
315 
316  /* write total size of tag */
317  data_size = avio_tell(pb) - metadata_size_pos - 10;
318 
319  avio_seek(pb, metadata_count_pos, SEEK_SET);
320  avio_wb32(pb, metadata_count);
321 
322  avio_seek(pb, metadata_size_pos, SEEK_SET);
323  avio_wb24(pb, data_size);
324  avio_skip(pb, data_size + 10 - 3);
325  avio_wb32(pb, data_size + 11);
326 }
327 
329 {
330  int i;
331  AVIOContext *pb = s->pb;
332  FLVContext *flv = s->priv_data;
333  int64_t data_size;
334 
335  for (i = 0; i < s->nb_streams; i++) {
336  AVCodecContext *enc = s->streams[i]->codec;
337  FLVStreamContext *sc;
338  switch (enc->codec_type) {
339  case AVMEDIA_TYPE_VIDEO:
340  if (s->streams[i]->avg_frame_rate.den &&
341  s->streams[i]->avg_frame_rate.num) {
342  flv->framerate = av_q2d(s->streams[i]->avg_frame_rate);
343  }
344  if (flv->video_enc) {
345  av_log(s, AV_LOG_ERROR,
346  "at most one video stream is supported in flv\n");
347  return AVERROR(EINVAL);
348  }
349  flv->video_enc = enc;
350  if (enc->codec_tag == 0) {
351  av_log(s, AV_LOG_ERROR, "Video codec '%s' for stream %d is not compatible with FLV\n",
352  avcodec_get_name(enc->codec_id), i);
353  return AVERROR(EINVAL);
354  }
355  if (enc->codec_id == AV_CODEC_ID_MPEG4 ||
356  enc->codec_id == AV_CODEC_ID_H263) {
358  av_log(s, error ? AV_LOG_ERROR : AV_LOG_WARNING,
359  "Codec %s is not supported in the official FLV specification,\n", avcodec_get_name(enc->codec_id));
360 
361  if (error) {
362  av_log(s, AV_LOG_ERROR,
363  "use vstrict=-1 / -strict -1 to use it anyway.\n");
364  return AVERROR(EINVAL);
365  }
366  } else if (enc->codec_id == AV_CODEC_ID_VP6) {
368  "Muxing VP6 in flv will produce flipped video on playback.\n");
369  }
370  break;
371  case AVMEDIA_TYPE_AUDIO:
372  if (flv->audio_enc) {
373  av_log(s, AV_LOG_ERROR,
374  "at most one audio stream is supported in flv\n");
375  return AVERROR(EINVAL);
376  }
377  flv->audio_enc = enc;
378  if (get_audio_flags(s, enc) < 0)
379  return AVERROR_INVALIDDATA;
380  if (enc->codec_id == AV_CODEC_ID_PCM_S16BE)
382  "16-bit big-endian audio in flv is valid but most likely unplayable (hardware dependent); use s16le\n");
383  break;
384  case AVMEDIA_TYPE_DATA:
385  if (enc->codec_id != AV_CODEC_ID_TEXT && enc->codec_id != AV_CODEC_ID_NONE) {
386  av_log(s, AV_LOG_ERROR, "Data codec '%s' for stream %d is not compatible with FLV\n",
387  avcodec_get_name(enc->codec_id), i);
388  return AVERROR_INVALIDDATA;
389  }
390  flv->data_enc = enc;
391  break;
393  if (enc->codec_id != AV_CODEC_ID_TEXT) {
394  av_log(s, AV_LOG_ERROR, "Subtitle codec '%s' for stream %d is not compatible with FLV\n",
395  avcodec_get_name(enc->codec_id), i);
396  return AVERROR_INVALIDDATA;
397  }
398  flv->data_enc = enc;
399  break;
400  default:
401  av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n",
403  return AVERROR(EINVAL);
404  }
405  avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
406 
407  sc = av_mallocz(sizeof(FLVStreamContext));
408  if (!sc)
409  return AVERROR(ENOMEM);
410  s->streams[i]->priv_data = sc;
411  sc->last_ts = -1;
412  }
413 
414  flv->delay = AV_NOPTS_VALUE;
415 
416  avio_write(pb, "FLV", 3);
417  avio_w8(pb, 1);
420  avio_wb32(pb, 9);
421  avio_wb32(pb, 0);
422 
423  for (i = 0; i < s->nb_streams; i++)
424  if (s->streams[i]->codec->codec_tag == 5) {
425  avio_w8(pb, 8); // message type
426  avio_wb24(pb, 0); // include flags
427  avio_wb24(pb, 0); // time stamp
428  avio_wb32(pb, 0); // reserved
429  avio_wb32(pb, 11); // size
430  flv->reserved = 5;
431  }
432 
433  write_metadata(s, 0);
434 
435  for (i = 0; i < s->nb_streams; i++) {
436  AVCodecContext *enc = s->streams[i]->codec;
437  if (enc->codec_id == AV_CODEC_ID_AAC || enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
438  int64_t pos;
441  avio_wb24(pb, 0); // size patched later
442  avio_wb24(pb, 0); // ts
443  avio_w8(pb, 0); // ts ext
444  avio_wb24(pb, 0); // streamid
445  pos = avio_tell(pb);
446  if (enc->codec_id == AV_CODEC_ID_AAC) {
447  avio_w8(pb, get_audio_flags(s, enc));
448  avio_w8(pb, 0); // AAC sequence header
449  avio_write(pb, enc->extradata, enc->extradata_size);
450  } else {
451  avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
452  avio_w8(pb, 0); // AVC sequence header
453  avio_wb24(pb, 0); // composition time
455  }
456  data_size = avio_tell(pb) - pos;
457  avio_seek(pb, -data_size - 10, SEEK_CUR);
458  avio_wb24(pb, data_size);
459  avio_skip(pb, data_size + 10 - 3);
460  avio_wb32(pb, data_size + 11); // previous tag size
461  }
462  }
463 
464  return 0;
465 }
466 
468 {
469  int64_t file_size;
470 
471  AVIOContext *pb = s->pb;
472  FLVContext *flv = s->priv_data;
473  int i;
474 
475  /* Add EOS tag */
476  for (i = 0; i < s->nb_streams; i++) {
477  AVCodecContext *enc = s->streams[i]->codec;
478  FLVStreamContext *sc = s->streams[i]->priv_data;
479  if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
481  put_avc_eos_tag(pb, sc->last_ts);
482  }
483 
484  file_size = avio_tell(pb);
485 
486  /* update information */
487  if (avio_seek(pb, flv->duration_offset, SEEK_SET) < 0)
488  av_log(s, AV_LOG_WARNING, "Failed to update header with correct duration.\n");
489  else
490  put_amf_double(pb, flv->duration / (double)1000);
491  if (avio_seek(pb, flv->filesize_offset, SEEK_SET) < 0)
492  av_log(s, AV_LOG_WARNING, "Failed to update header with correct filesize.\n");
493  else
494  put_amf_double(pb, file_size);
495 
496  avio_seek(pb, file_size, SEEK_SET);
497  return 0;
498 }
499 
501 {
502  AVIOContext *pb = s->pb;
503  AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
504  FLVContext *flv = s->priv_data;
506  unsigned ts;
507  int size = pkt->size;
508  uint8_t *data = NULL;
509  int flags = -1, flags_size, ret;
510 
511  if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A ||
513  flags_size = 2;
514  else if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4)
515  flags_size = 5;
516  else
517  flags_size = 1;
518 
519  if (flv->delay == AV_NOPTS_VALUE)
520  flv->delay = -pkt->dts;
521 
522  if (pkt->dts < -flv->delay) {
524  "Packets are not in the proper order with respect to DTS\n");
525  return AVERROR(EINVAL);
526  }
527 
528  ts = pkt->dts + flv->delay; // add delay to force positive dts
529 
531  write_metadata(s, ts);
533  }
534 
535  switch (enc->codec_type) {
536  case AVMEDIA_TYPE_VIDEO:
538 
539  flags = enc->codec_tag;
540  if (flags == 0) {
541  av_log(s, AV_LOG_ERROR,
542  "Video codec '%s' is not compatible with FLV\n",
543  avcodec_get_name(enc->codec_id));
544  return AVERROR(EINVAL);
545  }
546 
548  break;
549  case AVMEDIA_TYPE_AUDIO:
550  flags = get_audio_flags(s, enc);
551 
552  av_assert0(size);
553 
555  break;
557  case AVMEDIA_TYPE_DATA:
559  break;
560  default:
561  return AVERROR(EINVAL);
562  }
563 
564  if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
565  /* check if extradata looks like mp4 formated */
566  if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1)
567  if ((ret = ff_avc_parse_nal_units_buf(pkt->data, &data, &size)) < 0)
568  return ret;
569  } else if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
570  (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
571  if (!s->streams[pkt->stream_index]->nb_frames) {
572  av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
573  "use the audio bitstream filter 'aac_adtstoasc' to fix it "
574  "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
575  return AVERROR_INVALIDDATA;
576  }
577  av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
578  }
579 
580  /* check Speex packet duration */
581  if (enc->codec_id == AV_CODEC_ID_SPEEX && ts - sc->last_ts > 160)
582  av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
583  "8 frames per packet. Adobe Flash "
584  "Player cannot handle this!\n");
585 
586  if (sc->last_ts < ts)
587  sc->last_ts = ts;
588 
589  if (size + flags_size >= 1<<24) {
590  av_log(s, AV_LOG_ERROR, "Too large packet with size %u >= %u\n",
591  size + flags_size, 1<<24);
592  return AVERROR(EINVAL);
593  }
594 
595  avio_wb24(pb, size + flags_size);
596  avio_wb24(pb, ts & 0xFFFFFF);
597  avio_w8(pb, (ts >> 24) & 0x7F); // timestamps are 32 bits _signed_
598  avio_wb24(pb, flv->reserved);
599 
600  if (enc->codec_type == AVMEDIA_TYPE_DATA ||
602  int data_size;
603  int64_t metadata_size_pos = avio_tell(pb);
604  if (enc->codec_id == AV_CODEC_ID_TEXT) {
605  // legacy FFmpeg magic?
607  put_amf_string(pb, "onTextData");
609  avio_wb32(pb, 2);
610  put_amf_string(pb, "type");
612  put_amf_string(pb, "Text");
613  put_amf_string(pb, "text");
615  put_amf_string(pb, pkt->data);
616  put_amf_string(pb, "");
618  } else {
619  // just pass the metadata through
620  avio_write(pb, data ? data : pkt->data, size);
621  }
622  /* write total size of tag */
623  data_size = avio_tell(pb) - metadata_size_pos;
624  avio_seek(pb, metadata_size_pos - 10, SEEK_SET);
625  avio_wb24(pb, data_size);
626  avio_seek(pb, data_size + 10 - 3, SEEK_CUR);
627  avio_wb32(pb, data_size + 11);
628  } else {
629  av_assert1(flags>=0);
630  avio_w8(pb,flags);
631  if (enc->codec_id == AV_CODEC_ID_VP6)
632  avio_w8(pb,0);
633  if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A) {
634  if (enc->extradata_size)
635  avio_w8(pb, enc->extradata[0]);
636  else
637  avio_w8(pb, ((FFALIGN(enc->width, 16) - enc->width) << 4) |
638  (FFALIGN(enc->height, 16) - enc->height));
639  } else if (enc->codec_id == AV_CODEC_ID_AAC)
640  avio_w8(pb, 1); // AAC raw
641  else if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
642  avio_w8(pb, 1); // AVC NALU
643  avio_wb24(pb, pkt->pts - pkt->dts);
644  }
645 
646  avio_write(pb, data ? data : pkt->data, size);
647 
648  avio_wb32(pb, size + flags_size + 11); // previous tag size
649  flv->duration = FFMAX(flv->duration,
650  pkt->pts + flv->delay + pkt->duration);
651  }
652 
653  av_free(data);
654 
655  return pb->error;
656 }
657 
659  .name = "flv",
660  .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
661  .mime_type = "video/x-flv",
662  .extensions = "flv",
663  .priv_data_size = sizeof(FLVContext),
664  .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_ADPCM_SWF,
665  .video_codec = AV_CODEC_ID_FLV1,
669  .codec_tag = (const AVCodecTag* const []) {
671  },
674 };