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.

588 lines
21KB

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