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.

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