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[] = {
42  { AV_CODEC_ID_NONE, 0 }
43 };
44 
45 static const AVCodecTag flv_audio_codec_ids[] = {
56  { AV_CODEC_ID_NONE, 0 }
57 };
58 
59 typedef struct FLVContext {
60  int reserved;
61  int64_t duration_offset;
62  int64_t filesize_offset;
63  int64_t duration;
64  int64_t delay; ///< first dts delay (needed for AVC & Speex)
65 } FLVContext;
66 
67 typedef struct FLVStreamContext {
68  int64_t last_ts; ///< last timestamp for each stream
70 
72 {
75 
76  if (enc->codec_id == AV_CODEC_ID_AAC) // specs force these parameters
79  else if (enc->codec_id == AV_CODEC_ID_SPEEX) {
80  if (enc->sample_rate != 16000) {
82  "FLV only supports wideband (16kHz) Speex audio\n");
83  return AVERROR(EINVAL);
84  }
85  if (enc->channels != 1) {
86  av_log(s, AV_LOG_ERROR, "FLV only supports mono Speex audio\n");
87  return AVERROR(EINVAL);
88  }
90  } else {
91  switch (enc->sample_rate) {
92  case 44100:
93  flags |= FLV_SAMPLERATE_44100HZ;
94  break;
95  case 22050:
96  flags |= FLV_SAMPLERATE_22050HZ;
97  break;
98  case 11025:
99  flags |= FLV_SAMPLERATE_11025HZ;
100  break;
101  case 16000: // nellymoser only
102  case 8000: // nellymoser only
103  case 5512: // not MP3
104  if (enc->codec_id != AV_CODEC_ID_MP3) {
105  flags |= FLV_SAMPLERATE_SPECIAL;
106  break;
107  }
108  default:
109  av_log(s, AV_LOG_ERROR,
110  "FLV does not support sample rate %d, "
111  "choose from (44100, 22050, 11025)\n", enc->sample_rate);
112  return AVERROR(EINVAL);
113  }
114  }
115 
116  if (enc->channels > 1)
117  flags |= FLV_STEREO;
118 
119  switch (enc->codec_id) {
120  case AV_CODEC_ID_MP3:
122  break;
123  case AV_CODEC_ID_PCM_U8:
125  break;
128  break;
131  break;
134  break;
136  if (enc->sample_rate == 8000)
138  else if (enc->sample_rate == 16000)
140  else
142  break;
145  break;
148  break;
149  case 0:
150  flags |= enc->codec_tag << 4;
151  break;
152  default:
153  av_log(s, AV_LOG_ERROR, "Audio codec '%s' not compatible with FLV\n",
154  avcodec_get_name(enc->codec_id));
155  return AVERROR(EINVAL);
156  }
157 
158  return flags;
159 }
160 
161 static void put_amf_string(AVIOContext *pb, const char *str)
162 {
163  size_t len = strlen(str);
164  avio_wb16(pb, len);
165  avio_write(pb, str, len);
166 }
167 
168 static void put_avc_eos_tag(AVIOContext *pb, unsigned ts)
169 {
171  avio_wb24(pb, 5); /* Tag Data Size */
172  avio_wb24(pb, ts); /* lower 24 bits of timestamp in ms */
173  avio_w8(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms */
174  avio_wb24(pb, 0); /* StreamId = 0 */
175  avio_w8(pb, 23); /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
176  avio_w8(pb, 2); /* AVC end of sequence */
177  avio_wb24(pb, 0); /* Always 0 for AVC EOS. */
178  avio_wb32(pb, 16); /* Size of FLV tag */
179 }
180 
181 static void put_amf_double(AVIOContext *pb, double d)
182 {
184  avio_wb64(pb, av_double2int(d));
185 }
186 
187 static void put_amf_bool(AVIOContext *pb, int b)
188 {
190  avio_w8(pb, !!b);
191 }
192 
194 {
195  AVIOContext *pb = s->pb;
196  FLVContext *flv = s->priv_data;
197  AVCodecContext *audio_enc = NULL, *video_enc = NULL, *data_enc = NULL;
198  int i, metadata_count = 0;
199  double framerate = 0.0;
200  int64_t metadata_size_pos, data_size, metadata_count_pos;
201  AVDictionaryEntry *tag = NULL;
202 
203  for (i = 0; i < s->nb_streams; i++) {
204  AVCodecContext *enc = s->streams[i]->codec;
205  FLVStreamContext *sc;
206  switch (enc->codec_type) {
207  case AVMEDIA_TYPE_VIDEO:
208  if (s->streams[i]->avg_frame_rate.den &&
209  s->streams[i]->avg_frame_rate.num) {
210  framerate = av_q2d(s->streams[i]->avg_frame_rate);
211  } else {
212  framerate = 1 / av_q2d(s->streams[i]->codec->time_base);
213  }
214  if (video_enc) {
215  av_log(s, AV_LOG_ERROR,
216  "at most one video stream is supported in flv\n");
217  return AVERROR(EINVAL);
218  }
219  video_enc = enc;
220  if (enc->codec_tag == 0) {
221  av_log(s, AV_LOG_ERROR, "Video codec '%s' for stream %d is not compatible with FLV\n",
222  avcodec_get_name(enc->codec_id), i);
223  return AVERROR(EINVAL);
224  }
225  break;
226  case AVMEDIA_TYPE_AUDIO:
227  if (audio_enc) {
228  av_log(s, AV_LOG_ERROR,
229  "at most one audio stream is supported in flv\n");
230  return AVERROR(EINVAL);
231  }
232  audio_enc = enc;
233  if (get_audio_flags(s, enc) < 0)
234  return AVERROR_INVALIDDATA;
235  break;
236  case AVMEDIA_TYPE_DATA:
237  if (enc->codec_id != AV_CODEC_ID_TEXT) {
238  av_log(s, AV_LOG_ERROR, "Data codec '%s' for stream %d is not compatible with FLV\n",
239  avcodec_get_name(enc->codec_id), i);
240  return AVERROR_INVALIDDATA;
241  }
242  data_enc = enc;
243  break;
244  default:
245  av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n",
247  return AVERROR(EINVAL);
248  }
249  avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
250 
251  sc = av_mallocz(sizeof(FLVStreamContext));
252  if (!sc)
253  return AVERROR(ENOMEM);
254  s->streams[i]->priv_data = sc;
255  sc->last_ts = -1;
256  }
257 
258  flv->delay = AV_NOPTS_VALUE;
259 
260  avio_write(pb, "FLV", 3);
261  avio_w8(pb, 1);
262  avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc +
263  FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
264  avio_wb32(pb, 9);
265  avio_wb32(pb, 0);
266 
267  for (i = 0; i < s->nb_streams; i++)
268  if (s->streams[i]->codec->codec_tag == 5) {
269  avio_w8(pb, 8); // message type
270  avio_wb24(pb, 0); // include flags
271  avio_wb24(pb, 0); // time stamp
272  avio_wb32(pb, 0); // reserved
273  avio_wb32(pb, 11); // size
274  flv->reserved = 5;
275  }
276 
277  /* write meta_tag */
278  avio_w8(pb, 18); // tag type META
279  metadata_size_pos = avio_tell(pb);
280  avio_wb24(pb, 0); // size of data part (sum of all parts below)
281  avio_wb24(pb, 0); // timestamp
282  avio_wb32(pb, 0); // reserved
283 
284  /* now data of data_size size */
285 
286  /* first event name as a string */
288  put_amf_string(pb, "onMetaData"); // 12 bytes
289 
290  /* mixed array (hash) with size and string/type/data tuples */
292  metadata_count_pos = avio_tell(pb);
293  metadata_count = 5 * !!video_enc +
294  5 * !!audio_enc +
295  1 * !!data_enc +
296  2; // +2 for duration and file size
297 
298  avio_wb32(pb, metadata_count);
299 
300  put_amf_string(pb, "duration");
301  flv->duration_offset= avio_tell(pb);
302 
303  // fill in the guessed duration, it'll be corrected later if incorrect
305 
306  if (video_enc) {
307  put_amf_string(pb, "width");
308  put_amf_double(pb, video_enc->width);
309 
310  put_amf_string(pb, "height");
311  put_amf_double(pb, video_enc->height);
312 
313  put_amf_string(pb, "videodatarate");
314  put_amf_double(pb, video_enc->bit_rate / 1024.0);
315 
316  put_amf_string(pb, "framerate");
317  put_amf_double(pb, framerate);
318 
319  put_amf_string(pb, "videocodecid");
320  put_amf_double(pb, video_enc->codec_tag);
321  }
322 
323  if (audio_enc) {
324  put_amf_string(pb, "audiodatarate");
325  put_amf_double(pb, audio_enc->bit_rate / 1024.0);
326 
327  put_amf_string(pb, "audiosamplerate");
328  put_amf_double(pb, audio_enc->sample_rate);
329 
330  put_amf_string(pb, "audiosamplesize");
331  put_amf_double(pb, audio_enc->codec_id == AV_CODEC_ID_PCM_U8 ? 8 : 16);
332 
333  put_amf_string(pb, "stereo");
334  put_amf_bool(pb, audio_enc->channels == 2);
335 
336  put_amf_string(pb, "audiocodecid");
337  put_amf_double(pb, audio_enc->codec_tag);
338  }
339 
340  if (data_enc) {
341  put_amf_string(pb, "datastream");
342  put_amf_double(pb, 0.0);
343  }
344 
345  while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
346  if( !strcmp(tag->key, "width")
347  ||!strcmp(tag->key, "height")
348  ||!strcmp(tag->key, "videodatarate")
349  ||!strcmp(tag->key, "framerate")
350  ||!strcmp(tag->key, "videocodecid")
351  ||!strcmp(tag->key, "audiodatarate")
352  ||!strcmp(tag->key, "audiosamplerate")
353  ||!strcmp(tag->key, "audiosamplesize")
354  ||!strcmp(tag->key, "stereo")
355  ||!strcmp(tag->key, "audiocodecid")
356  ||!strcmp(tag->key, "duration")
357  ||!strcmp(tag->key, "onMetaData")
358  ){
359  av_log(s, AV_LOG_DEBUG, "Ignoring metadata for %s\n", tag->key);
360  continue;
361  }
362  put_amf_string(pb, tag->key);
364  put_amf_string(pb, tag->value);
365  metadata_count++;
366  }
367 
368  put_amf_string(pb, "filesize");
369  flv->filesize_offset = avio_tell(pb);
370  put_amf_double(pb, 0); // delayed write
371 
372  put_amf_string(pb, "");
374 
375  /* write total size of tag */
376  data_size = avio_tell(pb) - metadata_size_pos - 10;
377 
378  avio_seek(pb, metadata_count_pos, SEEK_SET);
379  avio_wb32(pb, metadata_count);
380 
381  avio_seek(pb, metadata_size_pos, SEEK_SET);
382  avio_wb24(pb, data_size);
383  avio_skip(pb, data_size + 10 - 3);
384  avio_wb32(pb, data_size + 11);
385 
386  for (i = 0; i < s->nb_streams; i++) {
387  AVCodecContext *enc = s->streams[i]->codec;
388  if (enc->codec_id == AV_CODEC_ID_AAC || enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
389  int64_t pos;
392  avio_wb24(pb, 0); // size patched later
393  avio_wb24(pb, 0); // ts
394  avio_w8(pb, 0); // ts ext
395  avio_wb24(pb, 0); // streamid
396  pos = avio_tell(pb);
397  if (enc->codec_id == AV_CODEC_ID_AAC) {
398  avio_w8(pb, get_audio_flags(s, enc));
399  avio_w8(pb, 0); // AAC sequence header
400  avio_write(pb, enc->extradata, enc->extradata_size);
401  } else {
402  avio_w8(pb, enc->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 
415  return 0;
416 }
417 
419 {
420  int64_t file_size;
421 
422  AVIOContext *pb = s->pb;
423  FLVContext *flv = s->priv_data;
424  int i;
425 
426  /* Add EOS tag */
427  for (i = 0; i < s->nb_streams; i++) {
428  AVCodecContext *enc = s->streams[i]->codec;
429  FLVStreamContext *sc = s->streams[i]->priv_data;
430  if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
432  put_avc_eos_tag(pb, sc->last_ts);
433  }
434 
435  file_size = avio_tell(pb);
436 
437  /* update information */
438  if (avio_seek(pb, flv->duration_offset, SEEK_SET) < 0)
439  av_log(s, AV_LOG_WARNING, "Failed to update header with correct duration.\n");
440  else
441  put_amf_double(pb, flv->duration / (double)1000);
442  if (avio_seek(pb, flv->filesize_offset, SEEK_SET) < 0)
443  av_log(s, AV_LOG_WARNING, "Failed to update header with correct filesize.\n");
444  else
445  put_amf_double(pb, file_size);
446 
447  avio_seek(pb, file_size, SEEK_SET);
448  return 0;
449 }
450 
452 {
453  AVIOContext *pb = s->pb;
454  AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
455  FLVContext *flv = s->priv_data;
457  unsigned ts;
458  int size = pkt->size;
459  uint8_t *data = NULL;
460  int flags = -1, flags_size, ret;
461 
462  if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A ||
463  enc->codec_id == AV_CODEC_ID_AAC)
464  flags_size = 2;
465  else if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4)
466  flags_size = 5;
467  else
468  flags_size = 1;
469 
470  switch (enc->codec_type) {
471  case AVMEDIA_TYPE_VIDEO:
473 
474  flags = enc->codec_tag;
475  if (flags == 0) {
476  av_log(s, AV_LOG_ERROR,
477  "Video codec '%s' is not compatible with FLV\n",
478  avcodec_get_name(enc->codec_id));
479  return AVERROR(EINVAL);
480  }
481 
483  break;
484  case AVMEDIA_TYPE_AUDIO:
485  flags = get_audio_flags(s, enc);
486 
487  av_assert0(size);
488 
490  break;
491  case AVMEDIA_TYPE_DATA:
493  break;
494  default:
495  return AVERROR(EINVAL);
496  }
497 
498  if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
499  /* check if extradata looks like mp4 formated */
500  if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1)
501  if ((ret = ff_avc_parse_nal_units_buf(pkt->data, &data, &size)) < 0)
502  return ret;
503  } else if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
504  (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
505  if (!s->streams[pkt->stream_index]->nb_frames) {
506  av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
507  "use audio bitstream filter 'aac_adtstoasc' to fix it "
508  "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
509  return AVERROR_INVALIDDATA;
510  }
511  av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
512  }
513 
514  if (flv->delay == AV_NOPTS_VALUE)
515  flv->delay = -pkt->dts;
516 
517  if (pkt->dts < -flv->delay) {
519  "Packets are not in the proper order with respect to DTS\n");
520  return AVERROR(EINVAL);
521  }
522 
523  ts = pkt->dts + flv->delay; // add delay to force positive dts
524 
525  /* check Speex packet duration */
526  if (enc->codec_id == AV_CODEC_ID_SPEEX && ts - sc->last_ts > 160)
527  av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
528  "8 frames per packet. Adobe Flash "
529  "Player cannot handle this!\n");
530 
531  if (sc->last_ts < ts)
532  sc->last_ts = ts;
533 
534  avio_wb24(pb, size + flags_size);
535  avio_wb24(pb, ts);
536  avio_w8(pb, (ts >> 24) & 0x7F); // timestamps are 32 bits _signed_
537  avio_wb24(pb, flv->reserved);
538 
539  if (enc->codec_type == AVMEDIA_TYPE_DATA) {
540  int data_size;
541  int metadata_size_pos = avio_tell(pb);
543  put_amf_string(pb, "onTextData");
545  avio_wb32(pb, 2);
546  put_amf_string(pb, "type");
548  put_amf_string(pb, "Text");
549  put_amf_string(pb, "text");
551  put_amf_string(pb, pkt->data);
552  put_amf_string(pb, "");
554  /* write total size of tag */
555  data_size = avio_tell(pb) - metadata_size_pos;
556  avio_seek(pb, metadata_size_pos - 10, SEEK_SET);
557  avio_wb24(pb, data_size);
558  avio_seek(pb, data_size + 10 - 3, SEEK_CUR);
559  avio_wb32(pb, data_size + 11);
560  } else {
561  av_assert1(flags>=0);
562  avio_w8(pb,flags);
563  if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A) {
564  if (enc->extradata_size)
565  avio_w8(pb, enc->extradata[0]);
566  else
567  avio_w8(pb, ((FFALIGN(enc->width, 16) - enc->width) << 4) |
568  (FFALIGN(enc->height, 16) - enc->height));
569  } else if (enc->codec_id == AV_CODEC_ID_AAC)
570  avio_w8(pb, 1); // AAC raw
571  else if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
572  avio_w8(pb, 1); // AVC NALU
573  avio_wb24(pb, pkt->pts - pkt->dts);
574  }
575 
576  avio_write(pb, data ? data : pkt->data, size);
577 
578  avio_wb32(pb, size + flags_size + 11); // previous tag size
579  flv->duration = FFMAX(flv->duration,
580  pkt->pts + flv->delay + pkt->duration);
581  }
582 
583  av_free(data);
584 
585  return pb->error;
586 }
587 
589  .name = "flv",
590  .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
591  .mime_type = "video/x-flv",
592  .extensions = "flv",
593  .priv_data_size = sizeof(FLVContext),
594  .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_ADPCM_SWF,
595  .video_codec = AV_CODEC_ID_FLV1,
599  .codec_tag = (const AVCodecTag* const []) {
601  },
604 };