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.

591 lines
20KB

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