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.

363 lines
11KB

  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_ctx(AVIOContext *pb, AVCodecParameters *ctxpar, unsigned tag, int type)
  87. {
  88. AVIOContext *tmp;
  89. char *buf = NULL;
  90. int ret, need_coma = 0;
  91. AVCodecContext *ctx = NULL;
  92. #define SKIP_DEFAULTS AV_OPT_SERIALIZE_SKIP_DEFAULTS
  93. #define OPT_FLAGS_EXACT AV_OPT_SERIALIZE_OPT_FLAGS_EXACT
  94. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  95. if (avio_open_dyn_buf(&tmp) < 0)
  96. return AVERROR(ENOMEM);
  97. // AVCodecParameters does not suport AVOptions, we thus must copy it over to a context that does
  98. // otherwise it could be used directly and this would be much simpler
  99. ctx = avcodec_alloc_context3(NULL);
  100. if (!ctx) {
  101. ret = AVERROR(ENOMEM);
  102. goto fail;
  103. }
  104. avcodec_parameters_to_context(ctx, ctxpar);
  105. if ((ret = av_opt_serialize(ctx, ENC | type, SKIP_DEFAULTS, &buf, '=', ',')) < 0)
  106. goto fail;
  107. if (buf && strlen(buf)) {
  108. avio_write(tmp, buf, strlen(buf));
  109. av_freep(&buf);
  110. need_coma = 1;
  111. }
  112. if ((ret = av_opt_serialize(ctx, 0, SKIP_DEFAULTS | OPT_FLAGS_EXACT, &buf, '=', ',')) < 0)
  113. goto fail;
  114. if (buf && strlen(buf)) {
  115. if (need_coma)
  116. avio_w8(tmp, ',');
  117. avio_write(tmp, buf, strlen(buf));
  118. }
  119. av_freep(&buf);
  120. avio_w8(tmp, 0);
  121. write_header_chunk(pb, tmp, tag);
  122. avcodec_free_context(&ctx);
  123. return 0;
  124. fail:
  125. av_free(buf);
  126. ffio_free_dyn_buf(&tmp);
  127. avcodec_free_context(&ctx);
  128. return ret;
  129. #undef SKIP_DEFAULTS
  130. #undef OPT_FLAGS_EXACT
  131. #undef ENC
  132. }
  133. static int ffm_write_recommended_config(AVIOContext *pb, AVCodecParameters *codecpar, unsigned tag,
  134. const char *configuration)
  135. {
  136. int ret;
  137. const AVCodec *enc = avcodec_find_encoder(codecpar->codec_id);
  138. AVIOContext *tmp;
  139. AVDictionaryEntry *t = NULL;
  140. AVDictionary *all = NULL, *comm = NULL, *prv = NULL;
  141. char *buf = NULL;
  142. if (!enc || !enc->priv_class || !enc->priv_data_size) {
  143. /* codec is not known/has no private options, so save everything as common options */
  144. if (avio_open_dyn_buf(&tmp) < 0)
  145. return AVERROR(ENOMEM);
  146. avio_put_str(tmp, configuration);
  147. write_header_chunk(pb, tmp, tag);
  148. return 0;
  149. }
  150. if ((ret = av_dict_parse_string(&all, configuration, "=", ",", 0)) < 0)
  151. return ret;
  152. while ((t = av_dict_get(all, "", t, AV_DICT_IGNORE_SUFFIX))) {
  153. if (av_opt_find((void *)&enc->priv_class, t->key, NULL, 0, AV_OPT_SEARCH_FAKE_OBJ)) {
  154. if ((ret = av_dict_set(&prv, t->key, t->value, 0)) < 0)
  155. goto fail;
  156. } else if ((ret = av_dict_set(&comm, t->key, t->value, 0)) < 0)
  157. goto fail;
  158. }
  159. if (comm) {
  160. if ((ret = av_dict_get_string(comm, &buf, '=', ',')) < 0 ||
  161. (ret = avio_open_dyn_buf(&tmp)) < 0)
  162. goto fail;
  163. avio_put_str(tmp, buf);
  164. av_freep(&buf);
  165. write_header_chunk(pb, tmp, tag);
  166. }
  167. if (prv) {
  168. if ((ret = av_dict_get_string(prv, &buf, '=', ',')) < 0 ||
  169. (ret = avio_open_dyn_buf(&tmp)) < 0)
  170. goto fail;
  171. avio_put_str(tmp, buf);
  172. write_header_chunk(pb, tmp, MKBETAG('C', 'P', 'R', 'V'));
  173. }
  174. fail:
  175. av_free(buf);
  176. av_dict_free(&all);
  177. av_dict_free(&comm);
  178. av_dict_free(&prv);
  179. return ret;
  180. }
  181. static int ffm_write_header(AVFormatContext *s)
  182. {
  183. FFMContext *ffm = s->priv_data;
  184. AVStream *st;
  185. AVIOContext *pb = s->pb;
  186. AVCodecParameters *codecpar;
  187. int bit_rate, i, ret;
  188. if ((ret = ff_parse_creation_time_metadata(s, &ffm->start_time, 0)) < 0)
  189. return ret;
  190. ffm->packet_size = FFM_PACKET_SIZE;
  191. /* header */
  192. avio_wl32(pb, MKTAG('F', 'F', 'M', '2'));
  193. avio_wb32(pb, ffm->packet_size);
  194. avio_wb64(pb, 0); /* current write position */
  195. if(avio_open_dyn_buf(&pb) < 0)
  196. return AVERROR(ENOMEM);
  197. avio_wb32(pb, s->nb_streams);
  198. bit_rate = 0;
  199. for(i=0;i<s->nb_streams;i++) {
  200. st = s->streams[i];
  201. bit_rate += st->codecpar->bit_rate;
  202. }
  203. avio_wb32(pb, bit_rate);
  204. write_header_chunk(s->pb, pb, MKBETAG('M', 'A', 'I', 'N'));
  205. /* list of streams */
  206. for(i=0;i<s->nb_streams;i++) {
  207. int flags = 0;
  208. st = s->streams[i];
  209. avpriv_set_pts_info(st, 64, 1, 1000000);
  210. if(avio_open_dyn_buf(&pb) < 0)
  211. return AVERROR(ENOMEM);
  212. codecpar = st->codecpar;
  213. /* generic info */
  214. avio_wb32(pb, codecpar->codec_id);
  215. avio_w8(pb, codecpar->codec_type);
  216. avio_wb32(pb, codecpar->bit_rate);
  217. if (codecpar->extradata_size)
  218. flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  219. // If the user is not providing us with a configuration we have to fill it in as we cannot access the encoder
  220. if (!st->recommended_encoder_configuration) {
  221. if (s->flags & AVFMT_FLAG_BITEXACT)
  222. flags |= AV_CODEC_FLAG_BITEXACT;
  223. }
  224. avio_wb32(pb, flags);
  225. avio_wb32(pb, 0); // flags2
  226. avio_wb32(pb, 0); // debug
  227. if (codecpar->extradata_size) {
  228. avio_wb32(pb, codecpar->extradata_size);
  229. avio_write(pb, codecpar->extradata, codecpar->extradata_size);
  230. }
  231. write_header_chunk(s->pb, pb, MKBETAG('C', 'O', 'M', 'M'));
  232. /* specific info */
  233. switch(codecpar->codec_type) {
  234. case AVMEDIA_TYPE_VIDEO:
  235. if (st->recommended_encoder_configuration) {
  236. av_log(NULL, AV_LOG_DEBUG, "writing recommended configuration: %s\n",
  237. st->recommended_encoder_configuration);
  238. if ((ret = ffm_write_recommended_config(s->pb, codecpar, MKBETAG('S', '2', 'V', 'I'),
  239. st->recommended_encoder_configuration)) < 0)
  240. return ret;
  241. } else if ((ret = ffm_write_header_codec_ctx(s->pb, codecpar, MKBETAG('S', '2', 'V', 'I'), AV_OPT_FLAG_VIDEO_PARAM)) < 0)
  242. return ret;
  243. break;
  244. case AVMEDIA_TYPE_AUDIO:
  245. if (st->recommended_encoder_configuration) {
  246. av_log(NULL, AV_LOG_DEBUG, "writing recommended configuration: %s\n",
  247. st->recommended_encoder_configuration);
  248. if ((ret = ffm_write_recommended_config(s->pb, codecpar, MKBETAG('S', '2', 'A', 'U'),
  249. st->recommended_encoder_configuration)) < 0)
  250. return ret;
  251. } else if ((ret = ffm_write_header_codec_ctx(s->pb, codecpar, MKBETAG('S', '2', 'A', 'U'), AV_OPT_FLAG_AUDIO_PARAM)) < 0)
  252. return ret;
  253. break;
  254. default:
  255. return -1;
  256. }
  257. }
  258. pb = s->pb;
  259. avio_wb64(pb, 0); // end of header
  260. /* flush until end of block reached */
  261. while ((avio_tell(pb) % ffm->packet_size) != 0)
  262. avio_w8(pb, 0);
  263. avio_flush(pb);
  264. /* init packet mux */
  265. ffm->packet_ptr = ffm->packet;
  266. ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
  267. av_assert0(ffm->packet_end >= ffm->packet);
  268. ffm->frame_offset = 0;
  269. ffm->dts = 0;
  270. ffm->first_packet = 1;
  271. return 0;
  272. }
  273. static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
  274. {
  275. FFMContext *ffm = s->priv_data;
  276. int64_t dts;
  277. uint8_t header[FRAME_HEADER_SIZE+4];
  278. int header_size = FRAME_HEADER_SIZE;
  279. dts = ffm->start_time + pkt->dts;
  280. /* packet size & key_frame */
  281. header[0] = pkt->stream_index;
  282. header[1] = 0;
  283. if (pkt->flags & AV_PKT_FLAG_KEY)
  284. header[1] |= FLAG_KEY_FRAME;
  285. AV_WB24(header+2, pkt->size);
  286. AV_WB24(header+5, pkt->duration);
  287. AV_WB64(header+8, ffm->start_time + pkt->pts);
  288. if (pkt->pts != pkt->dts) {
  289. header[1] |= FLAG_DTS;
  290. AV_WB32(header+16, pkt->pts - pkt->dts);
  291. header_size += 4;
  292. }
  293. ffm_write_data(s, header, header_size, dts, 1);
  294. ffm_write_data(s, pkt->data, pkt->size, dts, 0);
  295. return 0;
  296. }
  297. static int ffm_write_trailer(AVFormatContext *s)
  298. {
  299. FFMContext *ffm = s->priv_data;
  300. /* flush packets */
  301. if (ffm->packet_ptr > ffm->packet)
  302. flush_packet(s);
  303. return 0;
  304. }
  305. AVOutputFormat ff_ffm_muxer = {
  306. .name = "ffm",
  307. .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed)"),
  308. .extensions = "ffm",
  309. .priv_data_size = sizeof(FFMContext),
  310. .audio_codec = AV_CODEC_ID_MP2,
  311. .video_codec = AV_CODEC_ID_MPEG1VIDEO,
  312. .write_header = ffm_write_header,
  313. .write_packet = ffm_write_packet,
  314. .write_trailer = ffm_write_trailer,
  315. .flags = AVFMT_TS_NEGATIVE,
  316. };