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.

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