ffmenc.c 7.5 KB

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