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.

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