You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

624 lines
22KB

  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. #include "libavutil/intreadwrite.h"
  22. #include "libavutil/dict.h"
  23. #include "libavutil/intfloat.h"
  24. #include "libavutil/avassert.h"
  25. #include "avc.h"
  26. #include "avformat.h"
  27. #include "flv.h"
  28. #include "internal.h"
  29. #include "metadata.h"
  30. static const AVCodecTag flv_video_codec_ids[] = {
  31. { AV_CODEC_ID_FLV1, FLV_CODECID_H263 },
  32. { AV_CODEC_ID_H263, FLV_CODECID_REALH263 },
  33. { AV_CODEC_ID_MPEG4, FLV_CODECID_MPEG4 },
  34. { AV_CODEC_ID_FLASHSV, FLV_CODECID_SCREEN },
  35. { AV_CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2 },
  36. { AV_CODEC_ID_VP6F, FLV_CODECID_VP6 },
  37. { AV_CODEC_ID_VP6, FLV_CODECID_VP6 },
  38. { AV_CODEC_ID_VP6A, FLV_CODECID_VP6A },
  39. { AV_CODEC_ID_H264, FLV_CODECID_H264 },
  40. { AV_CODEC_ID_NONE, 0 }
  41. };
  42. static const AVCodecTag flv_audio_codec_ids[] = {
  43. { AV_CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET },
  44. { AV_CODEC_ID_PCM_U8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET },
  45. { AV_CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET },
  46. { AV_CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET },
  47. { AV_CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET },
  48. { AV_CODEC_ID_AAC, FLV_CODECID_AAC >> FLV_AUDIO_CODECID_OFFSET },
  49. { AV_CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET },
  50. { AV_CODEC_ID_PCM_MULAW, FLV_CODECID_PCM_MULAW >> FLV_AUDIO_CODECID_OFFSET },
  51. { AV_CODEC_ID_PCM_ALAW, FLV_CODECID_PCM_ALAW >> FLV_AUDIO_CODECID_OFFSET },
  52. { AV_CODEC_ID_SPEEX, FLV_CODECID_SPEEX >> FLV_AUDIO_CODECID_OFFSET },
  53. { AV_CODEC_ID_NONE, 0 }
  54. };
  55. typedef struct FLVContext {
  56. int reserved;
  57. int64_t duration_offset;
  58. int64_t filesize_offset;
  59. int64_t duration;
  60. int64_t delay; ///< first dts delay (needed for AVC & Speex)
  61. } FLVContext;
  62. typedef struct FLVStreamContext {
  63. int64_t last_ts; ///< last timestamp for each stream
  64. } FLVStreamContext;
  65. static int get_audio_flags(AVFormatContext *s, AVCodecContext *enc)
  66. {
  67. int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT
  68. : FLV_SAMPLESSIZE_8BIT;
  69. if (enc->codec_id == AV_CODEC_ID_AAC) // specs force these parameters
  70. return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ |
  71. FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
  72. else if (enc->codec_id == AV_CODEC_ID_SPEEX) {
  73. if (enc->sample_rate != 16000) {
  74. av_log(s, AV_LOG_ERROR,
  75. "FLV only supports wideband (16kHz) Speex audio\n");
  76. return AVERROR(EINVAL);
  77. }
  78. if (enc->channels != 1) {
  79. av_log(s, AV_LOG_ERROR, "FLV only supports mono Speex audio\n");
  80. return AVERROR(EINVAL);
  81. }
  82. return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
  83. } else {
  84. switch (enc->sample_rate) {
  85. case 44100:
  86. flags |= FLV_SAMPLERATE_44100HZ;
  87. break;
  88. case 22050:
  89. flags |= FLV_SAMPLERATE_22050HZ;
  90. break;
  91. case 11025:
  92. flags |= FLV_SAMPLERATE_11025HZ;
  93. break;
  94. case 16000: // nellymoser only
  95. case 8000: // nellymoser only
  96. case 5512: // not MP3
  97. if (enc->codec_id != AV_CODEC_ID_MP3) {
  98. flags |= FLV_SAMPLERATE_SPECIAL;
  99. break;
  100. }
  101. default:
  102. av_log(s, AV_LOG_ERROR,
  103. "FLV does not support sample rate %d, "
  104. "choose from (44100, 22050, 11025)\n", enc->sample_rate);
  105. return AVERROR(EINVAL);
  106. }
  107. }
  108. if (enc->channels > 1)
  109. flags |= FLV_STEREO;
  110. switch (enc->codec_id) {
  111. case AV_CODEC_ID_MP3:
  112. flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
  113. break;
  114. case AV_CODEC_ID_PCM_U8:
  115. flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
  116. break;
  117. case AV_CODEC_ID_PCM_S16BE:
  118. flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
  119. break;
  120. case AV_CODEC_ID_PCM_S16LE:
  121. flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
  122. break;
  123. case AV_CODEC_ID_ADPCM_SWF:
  124. flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
  125. break;
  126. case AV_CODEC_ID_NELLYMOSER:
  127. if (enc->sample_rate == 8000)
  128. flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
  129. else if (enc->sample_rate == 16000)
  130. flags |= FLV_CODECID_NELLYMOSER_16KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
  131. else
  132. flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
  133. break;
  134. case AV_CODEC_ID_PCM_MULAW:
  135. flags = FLV_CODECID_PCM_MULAW | FLV_SAMPLERATE_SPECIAL | FLV_SAMPLESSIZE_16BIT;
  136. break;
  137. case AV_CODEC_ID_PCM_ALAW:
  138. flags = FLV_CODECID_PCM_ALAW | FLV_SAMPLERATE_SPECIAL | FLV_SAMPLESSIZE_16BIT;
  139. break;
  140. case 0:
  141. flags |= enc->codec_tag << 4;
  142. break;
  143. default:
  144. av_log(s, AV_LOG_ERROR, "Audio codec '%s' not compatible with FLV\n",
  145. avcodec_get_name(enc->codec_id));
  146. return AVERROR(EINVAL);
  147. }
  148. return flags;
  149. }
  150. static void put_amf_string(AVIOContext *pb, const char *str)
  151. {
  152. size_t len = strlen(str);
  153. avio_wb16(pb, len);
  154. avio_write(pb, str, len);
  155. }
  156. static void put_avc_eos_tag(AVIOContext *pb, unsigned ts)
  157. {
  158. avio_w8(pb, FLV_TAG_TYPE_VIDEO);
  159. avio_wb24(pb, 5); /* Tag Data Size */
  160. avio_wb24(pb, ts); /* lower 24 bits of timestamp in ms */
  161. avio_w8(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms */
  162. avio_wb24(pb, 0); /* StreamId = 0 */
  163. avio_w8(pb, 23); /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
  164. avio_w8(pb, 2); /* AVC end of sequence */
  165. avio_wb24(pb, 0); /* Always 0 for AVC EOS. */
  166. avio_wb32(pb, 16); /* Size of FLV tag */
  167. }
  168. static void put_amf_double(AVIOContext *pb, double d)
  169. {
  170. avio_w8(pb, AMF_DATA_TYPE_NUMBER);
  171. avio_wb64(pb, av_double2int(d));
  172. }
  173. static void put_amf_bool(AVIOContext *pb, int b)
  174. {
  175. avio_w8(pb, AMF_DATA_TYPE_BOOL);
  176. avio_w8(pb, !!b);
  177. }
  178. static int flv_write_header(AVFormatContext *s)
  179. {
  180. AVIOContext *pb = s->pb;
  181. FLVContext *flv = s->priv_data;
  182. AVCodecContext *audio_enc = NULL, *video_enc = NULL, *data_enc = NULL;
  183. int i, metadata_count = 0;
  184. double framerate = 0.0;
  185. int64_t metadata_size_pos, data_size, metadata_count_pos;
  186. AVDictionaryEntry *tag = NULL;
  187. for (i = 0; i < s->nb_streams; i++) {
  188. AVCodecContext *enc = s->streams[i]->codec;
  189. FLVStreamContext *sc;
  190. switch (enc->codec_type) {
  191. case AVMEDIA_TYPE_VIDEO:
  192. if (s->streams[i]->avg_frame_rate.den &&
  193. s->streams[i]->avg_frame_rate.num) {
  194. framerate = av_q2d(s->streams[i]->avg_frame_rate);
  195. }
  196. if (video_enc) {
  197. av_log(s, AV_LOG_ERROR,
  198. "at most one video stream is supported in flv\n");
  199. return AVERROR(EINVAL);
  200. }
  201. video_enc = enc;
  202. if (enc->codec_tag == 0) {
  203. av_log(s, AV_LOG_ERROR, "Video codec '%s' for stream %d is not compatible with FLV\n",
  204. avcodec_get_name(enc->codec_id), i);
  205. return AVERROR(EINVAL);
  206. }
  207. if (enc->codec_id == AV_CODEC_ID_MPEG4 ||
  208. enc->codec_id == AV_CODEC_ID_H263) {
  209. int error = enc->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL;
  210. av_log(s, error ? AV_LOG_ERROR : AV_LOG_WARNING,
  211. "Codec %s is not supported in the official FLV specification,\n", avcodec_get_name(enc->codec_id));
  212. if (error) {
  213. av_log(s, AV_LOG_ERROR,
  214. "use vstrict=-1 / -strict -1 to use it anyway.\n");
  215. return AVERROR(EINVAL);
  216. }
  217. }
  218. break;
  219. case AVMEDIA_TYPE_AUDIO:
  220. if (audio_enc) {
  221. av_log(s, AV_LOG_ERROR,
  222. "at most one audio stream is supported in flv\n");
  223. return AVERROR(EINVAL);
  224. }
  225. audio_enc = enc;
  226. if (get_audio_flags(s, enc) < 0)
  227. return AVERROR_INVALIDDATA;
  228. if (enc->codec_id == AV_CODEC_ID_PCM_S16BE)
  229. av_log(s, AV_LOG_WARNING,
  230. "16-bit big-endian audio in flv is valid but most likely unplayable (hardware dependent); use s16le\n");
  231. break;
  232. case AVMEDIA_TYPE_DATA:
  233. if (enc->codec_id != AV_CODEC_ID_TEXT) {
  234. av_log(s, AV_LOG_ERROR, "Data codec '%s' for stream %d is not compatible with FLV\n",
  235. avcodec_get_name(enc->codec_id), i);
  236. return AVERROR_INVALIDDATA;
  237. }
  238. data_enc = enc;
  239. break;
  240. default:
  241. av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n",
  242. av_get_media_type_string(enc->codec_type), i);
  243. return AVERROR(EINVAL);
  244. }
  245. avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
  246. sc = av_mallocz(sizeof(FLVStreamContext));
  247. if (!sc)
  248. return AVERROR(ENOMEM);
  249. s->streams[i]->priv_data = sc;
  250. sc->last_ts = -1;
  251. }
  252. flv->delay = AV_NOPTS_VALUE;
  253. avio_write(pb, "FLV", 3);
  254. avio_w8(pb, 1);
  255. avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc +
  256. FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
  257. avio_wb32(pb, 9);
  258. avio_wb32(pb, 0);
  259. for (i = 0; i < s->nb_streams; i++)
  260. if (s->streams[i]->codec->codec_tag == 5) {
  261. avio_w8(pb, 8); // message type
  262. avio_wb24(pb, 0); // include flags
  263. avio_wb24(pb, 0); // time stamp
  264. avio_wb32(pb, 0); // reserved
  265. avio_wb32(pb, 11); // size
  266. flv->reserved = 5;
  267. }
  268. /* write meta_tag */
  269. avio_w8(pb, 18); // tag type META
  270. metadata_size_pos = avio_tell(pb);
  271. avio_wb24(pb, 0); // size of data part (sum of all parts below)
  272. avio_wb24(pb, 0); // timestamp
  273. avio_wb32(pb, 0); // reserved
  274. /* now data of data_size size */
  275. /* first event name as a string */
  276. avio_w8(pb, AMF_DATA_TYPE_STRING);
  277. put_amf_string(pb, "onMetaData"); // 12 bytes
  278. /* mixed array (hash) with size and string/type/data tuples */
  279. avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
  280. metadata_count_pos = avio_tell(pb);
  281. metadata_count = 4 * !!video_enc +
  282. 5 * !!audio_enc +
  283. 1 * !!data_enc +
  284. 2; // +2 for duration and file size
  285. avio_wb32(pb, metadata_count);
  286. put_amf_string(pb, "duration");
  287. flv->duration_offset= avio_tell(pb);
  288. // fill in the guessed duration, it'll be corrected later if incorrect
  289. put_amf_double(pb, s->duration / AV_TIME_BASE);
  290. if (video_enc) {
  291. put_amf_string(pb, "width");
  292. put_amf_double(pb, video_enc->width);
  293. put_amf_string(pb, "height");
  294. put_amf_double(pb, video_enc->height);
  295. put_amf_string(pb, "videodatarate");
  296. put_amf_double(pb, video_enc->bit_rate / 1024.0);
  297. if (framerate != 0.0) {
  298. put_amf_string(pb, "framerate");
  299. put_amf_double(pb, framerate);
  300. metadata_count++;
  301. }
  302. put_amf_string(pb, "videocodecid");
  303. put_amf_double(pb, video_enc->codec_tag);
  304. }
  305. if (audio_enc) {
  306. put_amf_string(pb, "audiodatarate");
  307. put_amf_double(pb, audio_enc->bit_rate / 1024.0);
  308. put_amf_string(pb, "audiosamplerate");
  309. put_amf_double(pb, audio_enc->sample_rate);
  310. put_amf_string(pb, "audiosamplesize");
  311. put_amf_double(pb, audio_enc->codec_id == AV_CODEC_ID_PCM_U8 ? 8 : 16);
  312. put_amf_string(pb, "stereo");
  313. put_amf_bool(pb, audio_enc->channels == 2);
  314. put_amf_string(pb, "audiocodecid");
  315. put_amf_double(pb, audio_enc->codec_tag);
  316. }
  317. if (data_enc) {
  318. put_amf_string(pb, "datastream");
  319. put_amf_double(pb, 0.0);
  320. }
  321. while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  322. if( !strcmp(tag->key, "width")
  323. ||!strcmp(tag->key, "height")
  324. ||!strcmp(tag->key, "videodatarate")
  325. ||!strcmp(tag->key, "framerate")
  326. ||!strcmp(tag->key, "videocodecid")
  327. ||!strcmp(tag->key, "audiodatarate")
  328. ||!strcmp(tag->key, "audiosamplerate")
  329. ||!strcmp(tag->key, "audiosamplesize")
  330. ||!strcmp(tag->key, "stereo")
  331. ||!strcmp(tag->key, "audiocodecid")
  332. ||!strcmp(tag->key, "duration")
  333. ||!strcmp(tag->key, "onMetaData")
  334. ){
  335. av_log(s, AV_LOG_DEBUG, "Ignoring metadata for %s\n", tag->key);
  336. continue;
  337. }
  338. put_amf_string(pb, tag->key);
  339. avio_w8(pb, AMF_DATA_TYPE_STRING);
  340. put_amf_string(pb, tag->value);
  341. metadata_count++;
  342. }
  343. put_amf_string(pb, "filesize");
  344. flv->filesize_offset = avio_tell(pb);
  345. put_amf_double(pb, 0); // delayed write
  346. put_amf_string(pb, "");
  347. avio_w8(pb, AMF_END_OF_OBJECT);
  348. /* write total size of tag */
  349. data_size = avio_tell(pb) - metadata_size_pos - 10;
  350. avio_seek(pb, metadata_count_pos, SEEK_SET);
  351. avio_wb32(pb, metadata_count);
  352. avio_seek(pb, metadata_size_pos, SEEK_SET);
  353. avio_wb24(pb, data_size);
  354. avio_skip(pb, data_size + 10 - 3);
  355. avio_wb32(pb, data_size + 11);
  356. for (i = 0; i < s->nb_streams; i++) {
  357. AVCodecContext *enc = s->streams[i]->codec;
  358. if (enc->codec_id == AV_CODEC_ID_AAC || enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
  359. int64_t pos;
  360. avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
  361. FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
  362. avio_wb24(pb, 0); // size patched later
  363. avio_wb24(pb, 0); // ts
  364. avio_w8(pb, 0); // ts ext
  365. avio_wb24(pb, 0); // streamid
  366. pos = avio_tell(pb);
  367. if (enc->codec_id == AV_CODEC_ID_AAC) {
  368. avio_w8(pb, get_audio_flags(s, enc));
  369. avio_w8(pb, 0); // AAC sequence header
  370. avio_write(pb, enc->extradata, enc->extradata_size);
  371. } else {
  372. avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
  373. avio_w8(pb, 0); // AVC sequence header
  374. avio_wb24(pb, 0); // composition time
  375. ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
  376. }
  377. data_size = avio_tell(pb) - pos;
  378. avio_seek(pb, -data_size - 10, SEEK_CUR);
  379. avio_wb24(pb, data_size);
  380. avio_skip(pb, data_size + 10 - 3);
  381. avio_wb32(pb, data_size + 11); // previous tag size
  382. }
  383. }
  384. return 0;
  385. }
  386. static int flv_write_trailer(AVFormatContext *s)
  387. {
  388. int64_t file_size;
  389. AVIOContext *pb = s->pb;
  390. FLVContext *flv = s->priv_data;
  391. int i;
  392. /* Add EOS tag */
  393. for (i = 0; i < s->nb_streams; i++) {
  394. AVCodecContext *enc = s->streams[i]->codec;
  395. FLVStreamContext *sc = s->streams[i]->priv_data;
  396. if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  397. (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4))
  398. put_avc_eos_tag(pb, sc->last_ts);
  399. }
  400. file_size = avio_tell(pb);
  401. /* update information */
  402. if (avio_seek(pb, flv->duration_offset, SEEK_SET) < 0)
  403. av_log(s, AV_LOG_WARNING, "Failed to update header with correct duration.\n");
  404. else
  405. put_amf_double(pb, flv->duration / (double)1000);
  406. if (avio_seek(pb, flv->filesize_offset, SEEK_SET) < 0)
  407. av_log(s, AV_LOG_WARNING, "Failed to update header with correct filesize.\n");
  408. else
  409. put_amf_double(pb, file_size);
  410. avio_seek(pb, file_size, SEEK_SET);
  411. return 0;
  412. }
  413. static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
  414. {
  415. AVIOContext *pb = s->pb;
  416. AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
  417. FLVContext *flv = s->priv_data;
  418. FLVStreamContext *sc = s->streams[pkt->stream_index]->priv_data;
  419. unsigned ts;
  420. int size = pkt->size;
  421. uint8_t *data = NULL;
  422. int flags = -1, flags_size, ret;
  423. if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A ||
  424. enc->codec_id == AV_CODEC_ID_VP6 || enc->codec_id == AV_CODEC_ID_AAC)
  425. flags_size = 2;
  426. else if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4)
  427. flags_size = 5;
  428. else
  429. flags_size = 1;
  430. switch (enc->codec_type) {
  431. case AVMEDIA_TYPE_VIDEO:
  432. avio_w8(pb, FLV_TAG_TYPE_VIDEO);
  433. flags = enc->codec_tag;
  434. if (flags == 0) {
  435. av_log(s, AV_LOG_ERROR,
  436. "Video codec '%s' is not compatible with FLV\n",
  437. avcodec_get_name(enc->codec_id));
  438. return AVERROR(EINVAL);
  439. }
  440. flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
  441. break;
  442. case AVMEDIA_TYPE_AUDIO:
  443. flags = get_audio_flags(s, enc);
  444. av_assert0(size);
  445. avio_w8(pb, FLV_TAG_TYPE_AUDIO);
  446. break;
  447. case AVMEDIA_TYPE_DATA:
  448. avio_w8(pb, FLV_TAG_TYPE_META);
  449. break;
  450. default:
  451. return AVERROR(EINVAL);
  452. }
  453. if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
  454. /* check if extradata looks like mp4 formated */
  455. if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1)
  456. if ((ret = ff_avc_parse_nal_units_buf(pkt->data, &data, &size)) < 0)
  457. return ret;
  458. } else if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
  459. (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
  460. if (!s->streams[pkt->stream_index]->nb_frames) {
  461. av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
  462. "use audio bitstream filter 'aac_adtstoasc' to fix it "
  463. "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
  464. return AVERROR_INVALIDDATA;
  465. }
  466. av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
  467. }
  468. if (flv->delay == AV_NOPTS_VALUE)
  469. flv->delay = -pkt->dts;
  470. if (pkt->dts < -flv->delay) {
  471. av_log(s, AV_LOG_WARNING,
  472. "Packets are not in the proper order with respect to DTS\n");
  473. return AVERROR(EINVAL);
  474. }
  475. ts = pkt->dts + flv->delay; // add delay to force positive dts
  476. /* check Speex packet duration */
  477. if (enc->codec_id == AV_CODEC_ID_SPEEX && ts - sc->last_ts > 160)
  478. av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
  479. "8 frames per packet. Adobe Flash "
  480. "Player cannot handle this!\n");
  481. if (sc->last_ts < ts)
  482. sc->last_ts = ts;
  483. avio_wb24(pb, size + flags_size);
  484. avio_wb24(pb, ts & 0xFFFFFF);
  485. avio_w8(pb, (ts >> 24) & 0x7F); // timestamps are 32 bits _signed_
  486. avio_wb24(pb, flv->reserved);
  487. if (enc->codec_type == AVMEDIA_TYPE_DATA) {
  488. int data_size;
  489. int64_t metadata_size_pos = avio_tell(pb);
  490. avio_w8(pb, AMF_DATA_TYPE_STRING);
  491. put_amf_string(pb, "onTextData");
  492. avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
  493. avio_wb32(pb, 2);
  494. put_amf_string(pb, "type");
  495. avio_w8(pb, AMF_DATA_TYPE_STRING);
  496. put_amf_string(pb, "Text");
  497. put_amf_string(pb, "text");
  498. avio_w8(pb, AMF_DATA_TYPE_STRING);
  499. put_amf_string(pb, pkt->data);
  500. put_amf_string(pb, "");
  501. avio_w8(pb, AMF_END_OF_OBJECT);
  502. /* write total size of tag */
  503. data_size = avio_tell(pb) - metadata_size_pos;
  504. avio_seek(pb, metadata_size_pos - 10, SEEK_SET);
  505. avio_wb24(pb, data_size);
  506. avio_seek(pb, data_size + 10 - 3, SEEK_CUR);
  507. avio_wb32(pb, data_size + 11);
  508. } else {
  509. av_assert1(flags>=0);
  510. avio_w8(pb,flags);
  511. if (enc->codec_id == AV_CODEC_ID_VP6)
  512. avio_w8(pb,0);
  513. if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A) {
  514. if (enc->extradata_size)
  515. avio_w8(pb, enc->extradata[0]);
  516. else
  517. avio_w8(pb, ((FFALIGN(enc->width, 16) - enc->width) << 4) |
  518. (FFALIGN(enc->height, 16) - enc->height));
  519. } else if (enc->codec_id == AV_CODEC_ID_AAC)
  520. avio_w8(pb, 1); // AAC raw
  521. else if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
  522. avio_w8(pb, 1); // AVC NALU
  523. avio_wb24(pb, pkt->pts - pkt->dts);
  524. }
  525. avio_write(pb, data ? data : pkt->data, size);
  526. avio_wb32(pb, size + flags_size + 11); // previous tag size
  527. flv->duration = FFMAX(flv->duration,
  528. pkt->pts + flv->delay + pkt->duration);
  529. }
  530. av_free(data);
  531. return pb->error;
  532. }
  533. AVOutputFormat ff_flv_muxer = {
  534. .name = "flv",
  535. .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
  536. .mime_type = "video/x-flv",
  537. .extensions = "flv",
  538. .priv_data_size = sizeof(FLVContext),
  539. .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_ADPCM_SWF,
  540. .video_codec = AV_CODEC_ID_FLV1,
  541. .write_header = flv_write_header,
  542. .write_packet = flv_write_packet,
  543. .write_trailer = flv_write_trailer,
  544. .codec_tag = (const AVCodecTag* const []) {
  545. flv_video_codec_ids, flv_audio_codec_ids, 0
  546. },
  547. .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
  548. AVFMT_TS_NONSTRICT,
  549. };