ffmenc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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_readwrite.h"
  23. #include "avformat.h"
  24. #include "ffm.h"
  25. static void flush_packet(AVFormatContext *s)
  26. {
  27. FFMContext *ffm = s->priv_data;
  28. int fill_size, h;
  29. AVIOContext *pb = s->pb;
  30. fill_size = ffm->packet_end - ffm->packet_ptr;
  31. memset(ffm->packet_ptr, 0, fill_size);
  32. if (avio_tell(pb) % ffm->packet_size)
  33. av_abort();
  34. /* put header */
  35. avio_wb16(pb, PACKET_ID);
  36. avio_wb16(pb, fill_size);
  37. avio_wb64(pb, ffm->dts);
  38. h = ffm->frame_offset;
  39. if (ffm->first_packet)
  40. h |= 0x8000;
  41. avio_wb16(pb, h);
  42. avio_write(pb, ffm->packet, ffm->packet_end - ffm->packet);
  43. avio_flush(pb);
  44. /* prepare next packet */
  45. ffm->frame_offset = 0; /* no key frame */
  46. ffm->packet_ptr = ffm->packet;
  47. ffm->first_packet = 0;
  48. }
  49. /* 'first' is true if first data of a frame */
  50. static void ffm_write_data(AVFormatContext *s,
  51. const uint8_t *buf, int size,
  52. int64_t dts, int header)
  53. {
  54. FFMContext *ffm = s->priv_data;
  55. int len;
  56. if (header && ffm->frame_offset == 0) {
  57. ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
  58. ffm->dts = dts;
  59. }
  60. /* write as many packets as needed */
  61. while (size > 0) {
  62. len = ffm->packet_end - ffm->packet_ptr;
  63. if (len > size)
  64. len = size;
  65. memcpy(ffm->packet_ptr, buf, len);
  66. ffm->packet_ptr += len;
  67. buf += len;
  68. size -= len;
  69. if (ffm->packet_ptr >= ffm->packet_end)
  70. flush_packet(s);
  71. }
  72. }
  73. static int ffm_write_header(AVFormatContext *s)
  74. {
  75. FFMContext *ffm = s->priv_data;
  76. AVStream *st;
  77. AVIOContext *pb = s->pb;
  78. AVCodecContext *codec;
  79. int bit_rate, i;
  80. ffm->packet_size = FFM_PACKET_SIZE;
  81. /* header */
  82. avio_wl32(pb, MKTAG('F', 'F', 'M', '1'));
  83. avio_wb32(pb, ffm->packet_size);
  84. avio_wb64(pb, 0); /* current write position */
  85. avio_wb32(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. avio_wb32(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. avio_wb32(pb, codec->codec_id);
  99. avio_w8(pb, codec->codec_type);
  100. avio_wb32(pb, codec->bit_rate);
  101. avio_wb32(pb, codec->flags);
  102. avio_wb32(pb, codec->flags2);
  103. avio_wb32(pb, codec->debug);
  104. /* specific info */
  105. switch(codec->codec_type) {
  106. case AVMEDIA_TYPE_VIDEO:
  107. avio_wb32(pb, codec->time_base.num);
  108. avio_wb32(pb, codec->time_base.den);
  109. avio_wb16(pb, codec->width);
  110. avio_wb16(pb, codec->height);
  111. avio_wb16(pb, codec->gop_size);
  112. avio_wb32(pb, codec->pix_fmt);
  113. avio_w8(pb, codec->qmin);
  114. avio_w8(pb, codec->qmax);
  115. avio_w8(pb, codec->max_qdiff);
  116. avio_wb16(pb, (int) (codec->qcompress * 10000.0));
  117. avio_wb16(pb, (int) (codec->qblur * 10000.0));
  118. avio_wb32(pb, codec->bit_rate_tolerance);
  119. avio_put_str(pb, codec->rc_eq ? codec->rc_eq : "tex^qComp");
  120. avio_wb32(pb, codec->rc_max_rate);
  121. avio_wb32(pb, codec->rc_min_rate);
  122. avio_wb32(pb, codec->rc_buffer_size);
  123. avio_wb64(pb, av_dbl2int(codec->i_quant_factor));
  124. avio_wb64(pb, av_dbl2int(codec->b_quant_factor));
  125. avio_wb64(pb, av_dbl2int(codec->i_quant_offset));
  126. avio_wb64(pb, av_dbl2int(codec->b_quant_offset));
  127. avio_wb32(pb, codec->dct_algo);
  128. avio_wb32(pb, codec->strict_std_compliance);
  129. avio_wb32(pb, codec->max_b_frames);
  130. avio_wb32(pb, codec->luma_elim_threshold);
  131. avio_wb32(pb, codec->chroma_elim_threshold);
  132. avio_wb32(pb, codec->mpeg_quant);
  133. avio_wb32(pb, codec->intra_dc_precision);
  134. avio_wb32(pb, codec->me_method);
  135. avio_wb32(pb, codec->mb_decision);
  136. avio_wb32(pb, codec->nsse_weight);
  137. avio_wb32(pb, codec->frame_skip_cmp);
  138. avio_wb64(pb, av_dbl2int(codec->rc_buffer_aggressivity));
  139. avio_wb32(pb, codec->codec_tag);
  140. avio_w8(pb, codec->thread_count);
  141. avio_wb32(pb, codec->coder_type);
  142. avio_wb32(pb, codec->me_cmp);
  143. avio_wb32(pb, codec->partitions);
  144. avio_wb32(pb, codec->me_subpel_quality);
  145. avio_wb32(pb, codec->me_range);
  146. avio_wb32(pb, codec->keyint_min);
  147. avio_wb32(pb, codec->scenechange_threshold);
  148. avio_wb32(pb, codec->b_frame_strategy);
  149. avio_wb64(pb, av_dbl2int(codec->qcompress));
  150. avio_wb64(pb, av_dbl2int(codec->qblur));
  151. avio_wb32(pb, codec->max_qdiff);
  152. avio_wb32(pb, codec->refs);
  153. avio_wb32(pb, codec->directpred);
  154. break;
  155. case AVMEDIA_TYPE_AUDIO:
  156. avio_wb32(pb, codec->sample_rate);
  157. avio_wl16(pb, codec->channels);
  158. avio_wl16(pb, codec->frame_size);
  159. avio_wl16(pb, codec->sample_fmt);
  160. break;
  161. default:
  162. return -1;
  163. }
  164. if (codec->flags & CODEC_FLAG_GLOBAL_HEADER) {
  165. avio_wb32(pb, codec->extradata_size);
  166. avio_write(pb, codec->extradata, codec->extradata_size);
  167. }
  168. }
  169. /* flush until end of block reached */
  170. while ((avio_tell(pb) % ffm->packet_size) != 0)
  171. avio_w8(pb, 0);
  172. avio_flush(pb);
  173. /* init packet mux */
  174. ffm->packet_ptr = ffm->packet;
  175. ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
  176. assert(ffm->packet_end >= ffm->packet);
  177. ffm->frame_offset = 0;
  178. ffm->dts = 0;
  179. ffm->first_packet = 1;
  180. return 0;
  181. }
  182. static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
  183. {
  184. int64_t dts;
  185. uint8_t header[FRAME_HEADER_SIZE+4];
  186. int header_size = FRAME_HEADER_SIZE;
  187. dts = s->timestamp + pkt->dts;
  188. /* packet size & key_frame */
  189. header[0] = pkt->stream_index;
  190. header[1] = 0;
  191. if (pkt->flags & AV_PKT_FLAG_KEY)
  192. header[1] |= FLAG_KEY_FRAME;
  193. AV_WB24(header+2, pkt->size);
  194. AV_WB24(header+5, pkt->duration);
  195. AV_WB64(header+8, s->timestamp + pkt->pts);
  196. if (pkt->pts != pkt->dts) {
  197. header[1] |= FLAG_DTS;
  198. AV_WB32(header+16, pkt->pts - pkt->dts);
  199. header_size += 4;
  200. }
  201. ffm_write_data(s, header, header_size, dts, 1);
  202. ffm_write_data(s, pkt->data, pkt->size, dts, 0);
  203. return 0;
  204. }
  205. static int ffm_write_trailer(AVFormatContext *s)
  206. {
  207. AVIOContext *pb = s->pb;
  208. FFMContext *ffm = s->priv_data;
  209. /* flush packets */
  210. if (ffm->packet_ptr > ffm->packet)
  211. flush_packet(s);
  212. avio_flush(pb);
  213. return 0;
  214. }
  215. AVOutputFormat ff_ffm_muxer = {
  216. .name = "ffm",
  217. .long_name = NULL_IF_CONFIG_SMALL("FFM (FFserver live feed) format"),
  218. .mime_type = "",
  219. .extensions = "ffm",
  220. .priv_data_size = sizeof(FFMContext),
  221. .audio_codec = CODEC_ID_MP2,
  222. .video_codec = CODEC_ID_MPEG1VIDEO,
  223. .write_header = ffm_write_header,
  224. .write_packet = ffm_write_packet,
  225. .write_trailer = ffm_write_trailer,
  226. };