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.

573 lines
19KB

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