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.

256 lines
7.5KB

  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 "avformat.h"
  22. #include "ffm.h"
  23. static void flush_packet(AVFormatContext *s)
  24. {
  25. FFMContext *ffm = s->priv_data;
  26. int fill_size, h;
  27. ByteIOContext *pb = s->pb;
  28. fill_size = ffm->packet_end - ffm->packet_ptr;
  29. memset(ffm->packet_ptr, 0, fill_size);
  30. if (url_ftell(pb) % ffm->packet_size)
  31. av_abort();
  32. /* put header */
  33. put_be16(pb, PACKET_ID);
  34. put_be16(pb, fill_size);
  35. put_be64(pb, ffm->pts);
  36. h = ffm->frame_offset;
  37. if (ffm->first_packet)
  38. h |= 0x8000;
  39. put_be16(pb, h);
  40. put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
  41. put_flush_packet(pb);
  42. /* prepare next packet */
  43. ffm->frame_offset = 0; /* no key frame */
  44. ffm->pts = 0; /* no pts */
  45. ffm->packet_ptr = ffm->packet;
  46. ffm->first_packet = 0;
  47. }
  48. /* 'first' is true if first data of a frame */
  49. static void ffm_write_data(AVFormatContext *s,
  50. const uint8_t *buf, int size,
  51. int64_t pts, int header)
  52. {
  53. FFMContext *ffm = s->priv_data;
  54. int len;
  55. if (header && ffm->frame_offset == 0)
  56. ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
  57. if (header && ffm->pts == 0)
  58. ffm->pts = pts;
  59. /* write as many packets as needed */
  60. while (size > 0) {
  61. len = ffm->packet_end - ffm->packet_ptr;
  62. if (len > size)
  63. len = size;
  64. memcpy(ffm->packet_ptr, buf, len);
  65. ffm->packet_ptr += len;
  66. buf += len;
  67. size -= len;
  68. if (ffm->packet_ptr >= ffm->packet_end) {
  69. /* special case : no pts in packet : we leave the current one */
  70. if (ffm->pts == 0)
  71. ffm->pts = pts;
  72. flush_packet(s);
  73. }
  74. }
  75. }
  76. static int ffm_write_header(AVFormatContext *s)
  77. {
  78. FFMContext *ffm = s->priv_data;
  79. AVStream *st;
  80. ByteIOContext *pb = s->pb;
  81. AVCodecContext *codec;
  82. int bit_rate, i;
  83. ffm->packet_size = FFM_PACKET_SIZE;
  84. /* header */
  85. put_le32(pb, MKTAG('F', 'F', 'M', '1'));
  86. put_be32(pb, ffm->packet_size);
  87. /* XXX: store write position in other file ? */
  88. put_be64(pb, ffm->packet_size); /* current write position */
  89. put_be32(pb, s->nb_streams);
  90. bit_rate = 0;
  91. for(i=0;i<s->nb_streams;i++) {
  92. st = s->streams[i];
  93. bit_rate += st->codec->bit_rate;
  94. }
  95. put_be32(pb, bit_rate);
  96. /* list of streams */
  97. for(i=0;i<s->nb_streams;i++) {
  98. st = s->streams[i];
  99. av_set_pts_info(st, 64, 1, 1000000);
  100. codec = st->codec;
  101. /* generic info */
  102. put_be32(pb, codec->codec_id);
  103. put_byte(pb, codec->codec_type);
  104. put_be32(pb, codec->bit_rate);
  105. put_be32(pb, st->quality);
  106. put_be32(pb, codec->flags);
  107. put_be32(pb, codec->flags2);
  108. put_be32(pb, codec->debug);
  109. /* specific info */
  110. switch(codec->codec_type) {
  111. case CODEC_TYPE_VIDEO:
  112. put_be32(pb, codec->time_base.num);
  113. put_be32(pb, codec->time_base.den);
  114. put_be16(pb, codec->width);
  115. put_be16(pb, codec->height);
  116. put_be16(pb, codec->gop_size);
  117. put_be32(pb, codec->pix_fmt);
  118. put_byte(pb, codec->qmin);
  119. put_byte(pb, codec->qmax);
  120. put_byte(pb, codec->max_qdiff);
  121. put_be16(pb, (int) (codec->qcompress * 10000.0));
  122. put_be16(pb, (int) (codec->qblur * 10000.0));
  123. put_be32(pb, codec->bit_rate_tolerance);
  124. put_strz(pb, codec->rc_eq);
  125. put_be32(pb, codec->rc_max_rate);
  126. put_be32(pb, codec->rc_min_rate);
  127. put_be32(pb, codec->rc_buffer_size);
  128. put_be64(pb, av_dbl2int(codec->i_quant_factor));
  129. put_be64(pb, av_dbl2int(codec->b_quant_factor));
  130. put_be64(pb, av_dbl2int(codec->i_quant_offset));
  131. put_be64(pb, av_dbl2int(codec->b_quant_offset));
  132. put_be32(pb, codec->dct_algo);
  133. put_be32(pb, codec->strict_std_compliance);
  134. put_be32(pb, codec->max_b_frames);
  135. put_be32(pb, codec->luma_elim_threshold);
  136. put_be32(pb, codec->chroma_elim_threshold);
  137. put_be32(pb, codec->mpeg_quant);
  138. put_be32(pb, codec->intra_dc_precision);
  139. put_be32(pb, codec->me_method);
  140. put_be32(pb, codec->mb_decision);
  141. put_be32(pb, codec->nsse_weight);
  142. put_be32(pb, codec->frame_skip_cmp);
  143. put_be64(pb, av_dbl2int(codec->rc_buffer_aggressivity));
  144. put_be32(pb, codec->codec_tag);
  145. break;
  146. case CODEC_TYPE_AUDIO:
  147. put_be32(pb, codec->sample_rate);
  148. put_le16(pb, codec->channels);
  149. put_le16(pb, codec->frame_size);
  150. break;
  151. default:
  152. return -1;
  153. }
  154. if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
  155. put_be32(pb, codec->extradata_size);
  156. put_buffer(pb, codec->extradata, codec->extradata_size);
  157. }
  158. }
  159. /* flush until end of block reached */
  160. while ((url_ftell(pb) % ffm->packet_size) != 0)
  161. put_byte(pb, 0);
  162. put_flush_packet(pb);
  163. /* init packet mux */
  164. ffm->packet_ptr = ffm->packet;
  165. ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
  166. assert(ffm->packet_end >= ffm->packet);
  167. ffm->frame_offset = 0;
  168. ffm->pts = 0;
  169. ffm->first_packet = 1;
  170. return 0;
  171. }
  172. static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
  173. {
  174. int64_t pts;
  175. uint8_t header[FRAME_HEADER_SIZE];
  176. int header_size = FRAME_HEADER_SIZE;
  177. pts = s->timestamp + pkt->pts;
  178. /* packet size & key_frame */
  179. header[0] = pkt->stream_index;
  180. header[1] = 0;
  181. if (pkt->flags & PKT_FLAG_KEY)
  182. header[1] |= FLAG_KEY_FRAME;
  183. AV_WB24(header+2, pkt->size);
  184. AV_WB24(header+5, pkt->duration);
  185. AV_WB64(header+8, pts);
  186. if (pkt->pts != pkt->dts) {
  187. header[1] |= FLAG_DTS;
  188. AV_WB32(header+16, pkt->pts - pkt->dts);
  189. header_size += 4;
  190. }
  191. ffm_write_data(s, header, header_size, pts, 1);
  192. ffm_write_data(s, pkt->data, pkt->size, pts, 0);
  193. return 0;
  194. }
  195. static int ffm_write_trailer(AVFormatContext *s)
  196. {
  197. ByteIOContext *pb = s->pb;
  198. FFMContext *ffm = s->priv_data;
  199. /* flush packets */
  200. if (ffm->packet_ptr > ffm->packet)
  201. flush_packet(s);
  202. put_flush_packet(pb);
  203. if (!url_is_streamed(pb)) {
  204. int64_t size;
  205. /* update the write offset */
  206. size = url_ftell(pb);
  207. url_fseek(pb, 8, SEEK_SET);
  208. put_be64(pb, size);
  209. put_flush_packet(pb);
  210. }
  211. return 0;
  212. }
  213. AVOutputFormat ffm_muxer = {
  214. "ffm",
  215. NULL_IF_CONFIG_SMALL("ffm format"),
  216. "",
  217. "ffm",
  218. sizeof(FFMContext),
  219. /* not really used */
  220. CODEC_ID_MP2,
  221. CODEC_ID_MPEG1VIDEO,
  222. ffm_write_header,
  223. ffm_write_packet,
  224. ffm_write_trailer,
  225. };