flvenc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. } else {
  196. framerate = 1 / av_q2d(s->streams[i]->codec->time_base);
  197. }
  198. video_enc = enc;
  199. if (enc->codec_tag == 0) {
  200. av_log(s, AV_LOG_ERROR, "Video codec '%s' for stream %d is not compatible with FLV\n",
  201. avcodec_get_name(enc->codec_id), i);
  202. return AVERROR(EINVAL);
  203. }
  204. if (enc->codec_id == AV_CODEC_ID_MPEG4 ||
  205. enc->codec_id == AV_CODEC_ID_H263) {
  206. int error = enc->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL;
  207. av_log(s, error ? AV_LOG_ERROR : AV_LOG_WARNING,
  208. "Codec %s is not supported in the official FLV specification,\n", avcodec_get_name(enc->codec_id));
  209. if (error) {
  210. av_log(s, AV_LOG_ERROR,
  211. "use vstrict=-1 / -strict -1 to use it anyway.\n");
  212. return AVERROR(EINVAL);
  213. }
  214. }
  215. break;
  216. case AVMEDIA_TYPE_AUDIO:
  217. audio_enc = enc;
  218. if (get_audio_flags(s, enc) < 0)
  219. return AVERROR_INVALIDDATA;
  220. break;
  221. case AVMEDIA_TYPE_DATA:
  222. if (enc->codec_id != AV_CODEC_ID_TEXT) {
  223. av_log(s, AV_LOG_ERROR, "Data codec '%s' for stream %d is not compatible with FLV\n",
  224. avcodec_get_name(enc->codec_id), i);
  225. return AVERROR_INVALIDDATA;
  226. }
  227. data_enc = enc;
  228. break;
  229. default:
  230. av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n",
  231. av_get_media_type_string(enc->codec_type), i);
  232. return AVERROR(EINVAL);
  233. }
  234. avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
  235. sc = av_mallocz(sizeof(FLVStreamContext));
  236. if (!sc)
  237. return AVERROR(ENOMEM);
  238. s->streams[i]->priv_data = sc;
  239. sc->last_ts = -1;
  240. }
  241. flv->delay = AV_NOPTS_VALUE;
  242. avio_write(pb, "FLV", 3);
  243. avio_w8(pb, 1);
  244. avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc +
  245. FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
  246. avio_wb32(pb, 9);
  247. avio_wb32(pb, 0);
  248. for (i = 0; i < s->nb_streams; i++)
  249. if (s->streams[i]->codec->codec_tag == 5) {
  250. avio_w8(pb, 8); // message type
  251. avio_wb24(pb, 0); // include flags
  252. avio_wb24(pb, 0); // time stamp
  253. avio_wb32(pb, 0); // reserved
  254. avio_wb32(pb, 11); // size
  255. flv->reserved = 5;
  256. }
  257. /* write meta_tag */
  258. avio_w8(pb, 18); // tag type META
  259. metadata_size_pos = avio_tell(pb);
  260. avio_wb24(pb, 0); // size of data part (sum of all parts below)
  261. avio_wb24(pb, 0); // timestamp
  262. avio_wb32(pb, 0); // reserved
  263. /* now data of data_size size */
  264. /* first event name as a string */
  265. avio_w8(pb, AMF_DATA_TYPE_STRING);
  266. put_amf_string(pb, "onMetaData"); // 12 bytes
  267. /* mixed array (hash) with size and string/type/data tuples */
  268. avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
  269. metadata_count_pos = avio_tell(pb);
  270. metadata_count = 5 * !!video_enc +
  271. 5 * !!audio_enc +
  272. 1 * !!data_enc +
  273. 2; // +2 for duration and file size
  274. avio_wb32(pb, metadata_count);
  275. put_amf_string(pb, "duration");
  276. flv->duration_offset= avio_tell(pb);
  277. // fill in the guessed duration, it'll be corrected later if incorrect
  278. put_amf_double(pb, s->duration / AV_TIME_BASE);
  279. if (video_enc) {
  280. put_amf_string(pb, "width");
  281. put_amf_double(pb, video_enc->width);
  282. put_amf_string(pb, "height");
  283. put_amf_double(pb, video_enc->height);
  284. put_amf_string(pb, "videodatarate");
  285. put_amf_double(pb, video_enc->bit_rate / 1024.0);
  286. put_amf_string(pb, "framerate");
  287. put_amf_double(pb, framerate);
  288. put_amf_string(pb, "videocodecid");
  289. put_amf_double(pb, video_enc->codec_tag);
  290. }
  291. if (audio_enc) {
  292. put_amf_string(pb, "audiodatarate");
  293. put_amf_double(pb, audio_enc->bit_rate / 1024.0);
  294. put_amf_string(pb, "audiosamplerate");
  295. put_amf_double(pb, audio_enc->sample_rate);
  296. put_amf_string(pb, "audiosamplesize");
  297. put_amf_double(pb, audio_enc->codec_id == AV_CODEC_ID_PCM_U8 ? 8 : 16);
  298. put_amf_string(pb, "stereo");
  299. put_amf_bool(pb, audio_enc->channels == 2);
  300. put_amf_string(pb, "audiocodecid");
  301. put_amf_double(pb, audio_enc->codec_tag);
  302. }
  303. if (data_enc) {
  304. put_amf_string(pb, "datastream");
  305. put_amf_double(pb, 0.0);
  306. }
  307. while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
  308. if( !strcmp(tag->key, "width")
  309. ||!strcmp(tag->key, "height")
  310. ||!strcmp(tag->key, "videodatarate")
  311. ||!strcmp(tag->key, "framerate")
  312. ||!strcmp(tag->key, "videocodecid")
  313. ||!strcmp(tag->key, "audiodatarate")
  314. ||!strcmp(tag->key, "audiosamplerate")
  315. ||!strcmp(tag->key, "audiosamplesize")
  316. ||!strcmp(tag->key, "stereo")
  317. ||!strcmp(tag->key, "audiocodecid")
  318. ||!strcmp(tag->key, "duration")
  319. ||!strcmp(tag->key, "onMetaData")
  320. ){
  321. av_log(s, AV_LOG_DEBUG, "Ignoring metadata for %s\n", tag->key);
  322. continue;
  323. }
  324. put_amf_string(pb, tag->key);
  325. avio_w8(pb, AMF_DATA_TYPE_STRING);
  326. put_amf_string(pb, tag->value);
  327. metadata_count++;
  328. }
  329. put_amf_string(pb, "filesize");
  330. flv->filesize_offset = avio_tell(pb);
  331. put_amf_double(pb, 0); // delayed write
  332. put_amf_string(pb, "");
  333. avio_w8(pb, AMF_END_OF_OBJECT);
  334. /* write total size of tag */
  335. data_size = avio_tell(pb) - metadata_size_pos - 10;
  336. avio_seek(pb, metadata_count_pos, SEEK_SET);
  337. avio_wb32(pb, metadata_count);
  338. avio_seek(pb, metadata_size_pos, SEEK_SET);
  339. avio_wb24(pb, data_size);
  340. avio_skip(pb, data_size + 10 - 3);
  341. avio_wb32(pb, data_size + 11);
  342. for (i = 0; i < s->nb_streams; i++) {
  343. AVCodecContext *enc = s->streams[i]->codec;
  344. if (enc->codec_id == AV_CODEC_ID_AAC || enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
  345. int64_t pos;
  346. avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
  347. FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
  348. avio_wb24(pb, 0); // size patched later
  349. avio_wb24(pb, 0); // ts
  350. avio_w8(pb, 0); // ts ext
  351. avio_wb24(pb, 0); // streamid
  352. pos = avio_tell(pb);
  353. if (enc->codec_id == AV_CODEC_ID_AAC) {
  354. avio_w8(pb, get_audio_flags(s, enc));
  355. avio_w8(pb, 0); // AAC sequence header
  356. avio_write(pb, enc->extradata, enc->extradata_size);
  357. } else {
  358. avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
  359. avio_w8(pb, 0); // AVC sequence header
  360. avio_wb24(pb, 0); // composition time
  361. ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
  362. }
  363. data_size = avio_tell(pb) - pos;
  364. avio_seek(pb, -data_size - 10, SEEK_CUR);
  365. avio_wb24(pb, data_size);
  366. avio_skip(pb, data_size + 10 - 3);
  367. avio_wb32(pb, data_size + 11); // previous tag size
  368. }
  369. }
  370. return 0;
  371. }
  372. static int flv_write_trailer(AVFormatContext *s)
  373. {
  374. int64_t file_size;
  375. AVIOContext *pb = s->pb;
  376. FLVContext *flv = s->priv_data;
  377. int i;
  378. /* Add EOS tag */
  379. for (i = 0; i < s->nb_streams; i++) {
  380. AVCodecContext *enc = s->streams[i]->codec;
  381. FLVStreamContext *sc = s->streams[i]->priv_data;
  382. if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  383. (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4))
  384. put_avc_eos_tag(pb, sc->last_ts);
  385. }
  386. file_size = avio_tell(pb);
  387. /* update information */
  388. avio_seek(pb, flv->duration_offset, SEEK_SET);
  389. put_amf_double(pb, flv->duration / (double)1000);
  390. avio_seek(pb, flv->filesize_offset, SEEK_SET);
  391. put_amf_double(pb, file_size);
  392. avio_seek(pb, file_size, SEEK_SET);
  393. return 0;
  394. }
  395. static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
  396. {
  397. AVIOContext *pb = s->pb;
  398. AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
  399. FLVContext *flv = s->priv_data;
  400. FLVStreamContext *sc = s->streams[pkt->stream_index]->priv_data;
  401. unsigned ts;
  402. int size = pkt->size;
  403. uint8_t *data = NULL;
  404. int flags = -1, flags_size, ret;
  405. av_dlog(s, "type:%s pts:%"PRId64" dts:%"PRId64" size:%d\n",
  406. av_get_media_type_string(enc->codec_type), pkt->pts, pkt->dts, size);
  407. if (enc->codec_id == AV_CODEC_ID_VP6 || enc->codec_id == AV_CODEC_ID_VP6F ||
  408. enc->codec_id == AV_CODEC_ID_VP6A || enc->codec_id == AV_CODEC_ID_AAC)
  409. flags_size = 2;
  410. else if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4)
  411. flags_size = 5;
  412. else
  413. flags_size = 1;
  414. switch (enc->codec_type) {
  415. case AVMEDIA_TYPE_VIDEO:
  416. avio_w8(pb, FLV_TAG_TYPE_VIDEO);
  417. flags = enc->codec_tag;
  418. if (flags == 0) {
  419. av_log(s, AV_LOG_ERROR,
  420. "Video codec '%s' is not compatible with FLV\n",
  421. avcodec_get_name(enc->codec_id));
  422. return AVERROR(EINVAL);
  423. }
  424. flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
  425. break;
  426. case AVMEDIA_TYPE_AUDIO:
  427. flags = get_audio_flags(s, enc);
  428. av_assert0(size);
  429. avio_w8(pb, FLV_TAG_TYPE_AUDIO);
  430. break;
  431. case AVMEDIA_TYPE_DATA:
  432. avio_w8(pb, FLV_TAG_TYPE_META);
  433. break;
  434. default:
  435. return AVERROR(EINVAL);
  436. }
  437. if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
  438. /* check if extradata looks like mp4 formated */
  439. if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1)
  440. if ((ret = ff_avc_parse_nal_units_buf(pkt->data, &data, &size)) < 0)
  441. return ret;
  442. } else if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
  443. (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
  444. if (!s->streams[pkt->stream_index]->nb_frames) {
  445. av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
  446. "use audio bistream filter 'aac_adtstoasc' to fix it "
  447. "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
  448. return AVERROR_INVALIDDATA;
  449. }
  450. av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
  451. }
  452. if (flv->delay == AV_NOPTS_VALUE)
  453. flv->delay = -pkt->dts;
  454. if (pkt->dts < -flv->delay) {
  455. av_log(s, AV_LOG_WARNING,
  456. "Packets are not in the proper order with respect to DTS\n");
  457. return AVERROR(EINVAL);
  458. }
  459. ts = pkt->dts + flv->delay; // add delay to force positive dts
  460. /* check Speex packet duration */
  461. if (enc->codec_id == AV_CODEC_ID_SPEEX && ts - sc->last_ts > 160)
  462. av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
  463. "8 frames per packet. Adobe Flash "
  464. "Player cannot handle this!\n");
  465. if (sc->last_ts < ts)
  466. sc->last_ts = ts;
  467. avio_wb24(pb, size + flags_size);
  468. avio_wb24(pb, ts);
  469. avio_w8(pb, (ts >> 24) & 0x7F); // timestamps are 32 bits _signed_
  470. avio_wb24(pb, flv->reserved);
  471. if (enc->codec_type == AVMEDIA_TYPE_DATA) {
  472. int data_size;
  473. int metadata_size_pos = avio_tell(pb);
  474. avio_w8(pb, AMF_DATA_TYPE_STRING);
  475. put_amf_string(pb, "onTextData");
  476. avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
  477. avio_wb32(pb, 2);
  478. put_amf_string(pb, "type");
  479. avio_w8(pb, AMF_DATA_TYPE_STRING);
  480. put_amf_string(pb, "Text");
  481. put_amf_string(pb, "text");
  482. avio_w8(pb, AMF_DATA_TYPE_STRING);
  483. put_amf_string(pb, pkt->data);
  484. put_amf_string(pb, "");
  485. avio_w8(pb, AMF_END_OF_OBJECT);
  486. /* write total size of tag */
  487. data_size = avio_tell(pb) - metadata_size_pos;
  488. avio_seek(pb, metadata_size_pos - 10, SEEK_SET);
  489. avio_wb24(pb, data_size);
  490. avio_seek(pb, data_size + 10 - 3, SEEK_CUR);
  491. avio_wb32(pb, data_size + 11);
  492. } else {
  493. av_assert1(flags>=0);
  494. avio_w8(pb,flags);
  495. if (enc->codec_id == AV_CODEC_ID_VP6)
  496. avio_w8(pb,0);
  497. if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A)
  498. avio_w8(pb, enc->extradata_size ? enc->extradata[0] : 0);
  499. else if (enc->codec_id == AV_CODEC_ID_AAC)
  500. avio_w8(pb,1); // AAC raw
  501. else if (enc->codec_id == AV_CODEC_ID_H264 || enc->codec_id == AV_CODEC_ID_MPEG4) {
  502. avio_w8(pb,1); // AVC NALU
  503. avio_wb24(pb,pkt->pts - pkt->dts);
  504. }
  505. avio_write(pb, data ? data : pkt->data, size);
  506. avio_wb32(pb, size + flags_size + 11); // previous tag size
  507. flv->duration = FFMAX(flv->duration,
  508. pkt->pts + flv->delay + pkt->duration);
  509. }
  510. avio_flush(pb);
  511. av_free(data);
  512. return pb->error;
  513. }
  514. AVOutputFormat ff_flv_muxer = {
  515. .name = "flv",
  516. .long_name = NULL_IF_CONFIG_SMALL("FLV (Flash Video)"),
  517. .mime_type = "video/x-flv",
  518. .extensions = "flv",
  519. .priv_data_size = sizeof(FLVContext),
  520. .audio_codec = CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_ADPCM_SWF,
  521. .video_codec = AV_CODEC_ID_FLV1,
  522. .write_header = flv_write_header,
  523. .write_packet = flv_write_packet,
  524. .write_trailer = flv_write_trailer,
  525. .codec_tag = (const AVCodecTag* const []) {
  526. flv_video_codec_ids, flv_audio_codec_ids, 0
  527. },
  528. .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
  529. AVFMT_TS_NONSTRICT,
  530. };