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.

375 lines
12KB

  1. /*
  2. * FFM (ffserver live feed) muxer
  3. * Copyright (c) 2001 Fabrice Bellard
  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/intfloat.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/parseutils.h"
  25. #include "libavutil/opt.h"
  26. #include "avformat.h"
  27. #include "avio_internal.h"
  28. #include "internal.h"
  29. #include "ffm.h"
  30. static void flush_packet(AVFormatContext *s)
  31. {
  32. FFMContext *ffm = s->priv_data;
  33. int fill_size, h;
  34. AVIOContext *pb = s->pb;
  35. fill_size = ffm->packet_end - ffm->packet_ptr;
  36. memset(ffm->packet_ptr, 0, fill_size);
  37. av_assert1(avio_tell(pb) % ffm->packet_size == 0);
  38. /* put header */
  39. avio_wb16(pb, PACKET_ID);
  40. avio_wb16(pb, fill_size);
  41. avio_wb64(pb, ffm->dts);
  42. h = ffm->frame_offset;
  43. if (ffm->first_packet)
  44. h |= 0x8000;
  45. avio_wb16(pb, h);
  46. avio_write(pb, ffm->packet, ffm->packet_end - ffm->packet);
  47. avio_flush(pb);
  48. /* prepare next packet */
  49. ffm->frame_offset = 0; /* no key frame */
  50. ffm->packet_ptr = ffm->packet;
  51. ffm->first_packet = 0;
  52. }
  53. /* 'first' is true if first data of a frame */
  54. static void ffm_write_data(AVFormatContext *s,
  55. const uint8_t *buf, int size,
  56. int64_t dts, int header)
  57. {
  58. FFMContext *ffm = s->priv_data;
  59. int len;
  60. if (header && ffm->frame_offset == 0) {
  61. ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
  62. ffm->dts = dts;
  63. }
  64. /* write as many packets as needed */
  65. while (size > 0) {
  66. len = ffm->packet_end - ffm->packet_ptr;
  67. if (len > size)
  68. len = size;
  69. memcpy(ffm->packet_ptr, buf, len);
  70. ffm->packet_ptr += len;
  71. buf += len;
  72. size -= len;
  73. if (ffm->packet_ptr >= ffm->packet_end)
  74. flush_packet(s);
  75. }
  76. }
  77. static void write_header_chunk(AVIOContext *pb, AVIOContext *dpb, unsigned id)
  78. {
  79. uint8_t *dyn_buf;
  80. int dyn_size= avio_close_dyn_buf(dpb, &dyn_buf);
  81. avio_wb32(pb, id);
  82. avio_wb32(pb, dyn_size);
  83. avio_write(pb, dyn_buf, dyn_size);
  84. av_free(dyn_buf);
  85. }
  86. static int ffm_write_header_codec_private_ctx(AVFormatContext *s, AVCodecContext *ctx, int type)
  87. {
  88. AVIOContext *pb = s->pb;
  89. AVIOContext *tmp;
  90. char *buf = NULL;
  91. int ret;
  92. const AVCodec *enc = ctx->codec ? ctx->codec : avcodec_find_encoder(ctx->codec_id);
  93. if (!enc) {
  94. av_log(s, AV_LOG_WARNING, "Stream codec is not found. Codec private options are not stored.\n");
  95. return 0;
  96. }
  97. if (ctx->priv_data && enc->priv_class && enc->priv_data_size) {
  98. if ((ret = av_opt_serialize(ctx->priv_data, AV_OPT_FLAG_ENCODING_PARAM | type,
  99. AV_OPT_SERIALIZE_SKIP_DEFAULTS, &buf, '=', ',')) < 0)
  100. return ret;
  101. if (buf && strlen(buf)) {
  102. if (avio_open_dyn_buf(&tmp) < 0) {
  103. av_free(buf);
  104. return AVERROR(ENOMEM);
  105. }
  106. avio_put_str(tmp, buf);
  107. write_header_chunk(pb, tmp, MKBETAG('C', 'P', 'R', 'V'));
  108. }
  109. av_free(buf);
  110. }
  111. return 0;
  112. }
  113. static int ffm_write_header_codec_ctx(AVIOContext *pb, AVCodecContext *ctx, unsigned tag, int type)
  114. {
  115. AVIOContext *tmp;
  116. char *buf = NULL;
  117. int ret, need_coma = 0;
  118. #define SKIP_DEFAULTS AV_OPT_SERIALIZE_SKIP_DEFAULTS
  119. #define OPT_FLAGS_EXACT AV_OPT_SERIALIZE_OPT_FLAGS_EXACT
  120. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  121. if (avio_open_dyn_buf(&tmp) < 0)
  122. return AVERROR(ENOMEM);
  123. if ((ret = av_opt_serialize(ctx, ENC | type, SKIP_DEFAULTS, &buf, '=', ',')) < 0)
  124. goto fail;
  125. if (buf && strlen(buf)) {
  126. avio_write(tmp, buf, strlen(buf));
  127. av_freep(&buf);
  128. need_coma = 1;
  129. }
  130. if ((ret = av_opt_serialize(ctx, 0, SKIP_DEFAULTS | OPT_FLAGS_EXACT, &buf, '=', ',')) < 0)
  131. goto fail;
  132. if (buf && strlen(buf)) {
  133. if (need_coma)
  134. avio_w8(tmp, ',');
  135. avio_write(tmp, buf, strlen(buf));
  136. }
  137. av_freep(&buf);
  138. avio_w8(tmp, 0);
  139. write_header_chunk(pb, tmp, tag);
  140. return 0;
  141. fail:
  142. av_free(buf);
  143. ffio_free_dyn_buf(&tmp);
  144. return ret;
  145. #undef SKIP_DEFAULTS
  146. #undef OPT_FLAGS_EXACT
  147. #undef ENC
  148. }
  149. static int ffm_write_recommended_config(AVIOContext *pb, AVCodecContext *ctx, unsigned tag,
  150. const char *configuration)
  151. {
  152. int ret;
  153. const AVCodec *enc = ctx->codec ? ctx->codec : avcodec_find_encoder(ctx->codec_id);
  154. AVIOContext *tmp;
  155. AVDictionaryEntry *t = NULL;
  156. AVDictionary *all = NULL, *comm = NULL, *prv = NULL;
  157. char *buf = NULL;
  158. if (!enc || !enc->priv_class || !enc->priv_data_size) {
  159. /* codec is not known/has no private options, so save everything as common options */
  160. if (avio_open_dyn_buf(&tmp) < 0)
  161. return AVERROR(ENOMEM);
  162. avio_put_str(tmp, configuration);
  163. write_header_chunk(pb, tmp, tag);
  164. return 0;
  165. }
  166. if ((ret = av_dict_parse_string(&all, configuration, "=", ",", 0)) < 0)
  167. return ret;
  168. while ((t = av_dict_get(all, "", t, AV_DICT_IGNORE_SUFFIX))) {
  169. if (av_opt_find((void *)&enc->priv_class, t->key, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)) {
  170. if ((ret = av_dict_set(&prv, t->key, t->value, 0)) < 0)
  171. goto fail;
  172. } else if ((ret = av_dict_set(&comm, t->key, t->value, 0)) < 0)
  173. goto fail;
  174. }
  175. if (comm) {
  176. if ((ret = av_dict_get_string(comm, &buf, '=', ',')) < 0 ||
  177. (ret = avio_open_dyn_buf(&tmp)) < 0)
  178. goto fail;
  179. avio_put_str(tmp, buf);
  180. av_freep(&buf);
  181. write_header_chunk(pb, tmp, tag);
  182. }
  183. if (prv) {
  184. if ((ret = av_dict_get_string(prv, &buf, '=', ',')) < 0 ||
  185. (ret = avio_open_dyn_buf(&tmp)) < 0)
  186. goto fail;
  187. avio_put_str(tmp, buf);
  188. write_header_chunk(pb, tmp, MKBETAG('C', 'P', 'R', 'V'));
  189. }
  190. fail:
  191. av_free(buf);
  192. av_dict_free(&all);
  193. av_dict_free(&comm);
  194. av_dict_free(&prv);
  195. return ret;
  196. }
  197. static int ffm_write_header(AVFormatContext *s)
  198. {
  199. FFMContext *ffm = s->priv_data;
  200. AVDictionaryEntry *t;
  201. AVStream *st;
  202. AVIOContext *pb = s->pb;
  203. AVCodecContext *codec;
  204. int bit_rate, i, ret;
  205. if (t = av_dict_get(s->metadata, "creation_time", NULL, 0)) {
  206. ret = av_parse_time(&ffm->start_time, t->value, 0);
  207. if (ret < 0)
  208. return ret;
  209. }
  210. ffm->packet_size = FFM_PACKET_SIZE;
  211. /* header */
  212. avio_wl32(pb, MKTAG('F', 'F', 'M', '2'));
  213. avio_wb32(pb, ffm->packet_size);
  214. avio_wb64(pb, 0); /* current write position */
  215. if(avio_open_dyn_buf(&pb) < 0)
  216. return AVERROR(ENOMEM);
  217. avio_wb32(pb, s->nb_streams);
  218. bit_rate = 0;
  219. for(i=0;i<s->nb_streams;i++) {
  220. st = s->streams[i];
  221. bit_rate += st->codec->bit_rate;
  222. }
  223. avio_wb32(pb, bit_rate);
  224. write_header_chunk(s->pb, pb, MKBETAG('M', 'A', 'I', 'N'));
  225. /* list of streams */
  226. for(i=0;i<s->nb_streams;i++) {
  227. st = s->streams[i];
  228. avpriv_set_pts_info(st, 64, 1, 1000000);
  229. if(avio_open_dyn_buf(&pb) < 0)
  230. return AVERROR(ENOMEM);
  231. codec = st->codec;
  232. /* generic info */
  233. avio_wb32(pb, codec->codec_id);
  234. avio_w8(pb, codec->codec_type);
  235. avio_wb32(pb, codec->bit_rate);
  236. avio_wb32(pb, codec->flags);
  237. avio_wb32(pb, codec->flags2);
  238. avio_wb32(pb, codec->debug);
  239. if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
  240. avio_wb32(pb, codec->extradata_size);
  241. avio_write(pb, codec->extradata, codec->extradata_size);
  242. }
  243. write_header_chunk(s->pb, pb, MKBETAG('C', 'O', 'M', 'M'));
  244. /* specific info */
  245. switch(codec->codec_type) {
  246. case AVMEDIA_TYPE_VIDEO:
  247. if (st->recommended_encoder_configuration) {
  248. av_log(NULL, AV_LOG_DEBUG, "writing recommended configuration: %s\n",
  249. st->recommended_encoder_configuration);
  250. if ((ret = ffm_write_recommended_config(s->pb, codec, MKBETAG('S', '2', 'V', 'I'),
  251. st->recommended_encoder_configuration)) < 0)
  252. return ret;
  253. } else if ((ret = ffm_write_header_codec_ctx(s->pb, codec, MKBETAG('S', '2', 'V', 'I'), AV_OPT_FLAG_VIDEO_PARAM)) < 0 ||
  254. (ret = ffm_write_header_codec_private_ctx(s, codec, AV_OPT_FLAG_VIDEO_PARAM)) < 0)
  255. return ret;
  256. break;
  257. case AVMEDIA_TYPE_AUDIO:
  258. if (st->recommended_encoder_configuration) {
  259. av_log(NULL, AV_LOG_DEBUG, "writing recommended configuration: %s\n",
  260. st->recommended_encoder_configuration);
  261. if ((ret = ffm_write_recommended_config(s->pb, codec, MKBETAG('S', '2', 'A', 'U'),
  262. st->recommended_encoder_configuration)) < 0)
  263. return ret;
  264. } else if ((ret = ffm_write_header_codec_ctx(s->pb, codec, MKBETAG('S', '2', 'A', 'U'), AV_OPT_FLAG_AUDIO_PARAM)) < 0 ||
  265. (ret = ffm_write_header_codec_private_ctx(s, codec, AV_OPT_FLAG_AUDIO_PARAM)) < 0)
  266. return ret;
  267. break;
  268. default:
  269. return -1;
  270. }
  271. }
  272. pb = s->pb;
  273. avio_wb64(pb, 0); // end of header
  274. /* flush until end of block reached */
  275. while ((avio_tell(pb) % ffm->packet_size) != 0)
  276. avio_w8(pb, 0);
  277. avio_flush(pb);
  278. /* init packet mux */
  279. ffm->packet_ptr = ffm->packet;
  280. ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
  281. av_assert0(ffm->packet_end >= ffm->packet);
  282. ffm->frame_offset = 0;
  283. ffm->dts = 0;
  284. ffm->first_packet = 1;
  285. return 0;
  286. }
  287. static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
  288. {
  289. FFMContext *ffm = s->priv_data;
  290. int64_t dts;
  291. uint8_t header[FRAME_HEADER_SIZE+4];
  292. int header_size = FRAME_HEADER_SIZE;
  293. dts = ffm->start_time + pkt->dts;
  294. /* packet size & key_frame */
  295. header[0] = pkt->stream_index;
  296. header[1] = 0;
  297. if (pkt->flags & AV_PKT_FLAG_KEY)
  298. header[1] |= FLAG_KEY_FRAME;
  299. AV_WB24(header+2, pkt->size);
  300. AV_WB24(header+5, pkt->duration);
  301. AV_WB64(header+8, ffm->start_time + pkt->pts);
  302. if (pkt->pts != pkt->dts) {
  303. header[1] |= FLAG_DTS;
  304. AV_WB32(header+16, pkt->pts - pkt->dts);
  305. header_size += 4;
  306. }
  307. ffm_write_data(s, header, header_size, dts, 1);
  308. ffm_write_data(s, pkt->data, pkt->size, dts, 0);
  309. return 0;
  310. }
  311. static int ffm_write_trailer(AVFormatContext *s)
  312. {
  313. FFMContext *ffm = s->priv_data;
  314. /* flush packets */
  315. if (ffm->packet_ptr > ffm->packet)
  316. flush_packet(s);
  317. return 0;
  318. }
  319. AVOutputFormat ff_ffm_muxer = {
  320. .name = "ffm",
  321. .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
  322. .extensions = "ffm",
  323. .priv_data_size = sizeof(FFMContext),
  324. .audio_codec = AV_CODEC_ID_MP2,
  325. .video_codec = AV_CODEC_ID_MPEG1VIDEO,
  326. .write_header = ffm_write_header,
  327. .write_packet = ffm_write_packet,
  328. .write_trailer = ffm_write_trailer,
  329. .flags = AVFMT_TS_NEGATIVE,
  330. };