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.

179 lines
4.5KB

  1. /*
  2. * H.264 encoding using the x264 library
  3. * Copyright (C) 2005 Mans Rullgard <mru@inprovide.com>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include <x264.h>
  21. #include <math.h>
  22. typedef struct X264Context {
  23. x264_param_t params;
  24. x264_t *enc;
  25. x264_picture_t pic;
  26. AVFrame out_pic;
  27. } X264Context;
  28. static void
  29. X264_log(void *p, int level, const char *fmt, va_list args)
  30. {
  31. static const int level_map[] = {
  32. [X264_LOG_ERROR] = AV_LOG_ERROR,
  33. [X264_LOG_WARNING] = AV_LOG_ERROR,
  34. [X264_LOG_INFO] = AV_LOG_INFO,
  35. [X264_LOG_DEBUG] = AV_LOG_DEBUG
  36. };
  37. if(level < 0 || level > X264_LOG_DEBUG)
  38. return;
  39. av_vlog(p, level_map[level], fmt, args);
  40. }
  41. static int
  42. encode_nals(uint8_t *buf, int size, x264_nal_t *nals, int nnal)
  43. {
  44. uint8_t *p = buf;
  45. int i;
  46. for(i = 0; i < nnal; i++){
  47. int s = x264_nal_encode(p, &size, 1, nals + i);
  48. if(s < 0)
  49. return -1;
  50. p += s;
  51. }
  52. return p - buf;
  53. }
  54. extern int
  55. X264_frame(AVCodecContext *ctx, uint8_t *buf, int bufsize, void *data)
  56. {
  57. X264Context *x4 = ctx->priv_data;
  58. AVFrame *frame = data;
  59. x264_nal_t *nal;
  60. int nnal, i;
  61. x264_picture_t pic_out;
  62. x4->pic.img.i_csp = X264_CSP_I420;
  63. x4->pic.img.i_plane = 3;
  64. for(i = 0; i < 3; i++){
  65. x4->pic.img.plane[i] = frame->data[i];
  66. x4->pic.img.i_stride[i] = frame->linesize[i];
  67. }
  68. x4->pic.i_pts = frame->pts;
  69. x4->pic.i_type = X264_TYPE_AUTO;
  70. if(x264_encoder_encode(x4->enc, &nal, &nnal, &x4->pic, &pic_out))
  71. return -1;
  72. bufsize = encode_nals(buf, bufsize, nal, nnal);
  73. if(bufsize < 0)
  74. return -1;
  75. /* FIXME: dts */
  76. x4->out_pic.pts = pic_out.i_pts;
  77. switch(pic_out.i_type){
  78. case X264_TYPE_IDR:
  79. case X264_TYPE_I:
  80. x4->out_pic.pict_type = FF_I_TYPE;
  81. break;
  82. case X264_TYPE_P:
  83. x4->out_pic.pict_type = FF_P_TYPE;
  84. break;
  85. case X264_TYPE_B:
  86. case X264_TYPE_BREF:
  87. x4->out_pic.pict_type = FF_B_TYPE;
  88. break;
  89. }
  90. x4->out_pic.key_frame = pic_out.i_type == X264_TYPE_IDR;
  91. x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA;
  92. return bufsize;
  93. }
  94. static int
  95. X264_close(AVCodecContext *avctx)
  96. {
  97. X264Context *x4 = avctx->priv_data;
  98. if(x4->enc)
  99. x264_encoder_close(x4->enc);
  100. return 0;
  101. }
  102. extern int
  103. X264_init(AVCodecContext *avctx)
  104. {
  105. X264Context *x4 = avctx->priv_data;
  106. x264_param_default(&x4->params);
  107. x4->params.pf_log = X264_log;
  108. x4->params.p_log_private = avctx;
  109. x4->params.i_keyint_max = avctx->gop_size;
  110. x4->params.rc.i_bitrate = avctx->bit_rate / 1000;
  111. x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
  112. if(avctx->rc_buffer_size)
  113. x4->params.rc.b_cbr = 1;
  114. x4->params.i_bframe = avctx->max_b_frames;
  115. x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC;
  116. x4->params.rc.i_qp_min = avctx->qmin;
  117. x4->params.rc.i_qp_max = avctx->qmax;
  118. x4->params.rc.i_qp_step = avctx->max_qdiff;
  119. if(avctx->flags & CODEC_FLAG_QSCALE && avctx->global_quality > 0)
  120. x4->params.rc.i_qp_constant =
  121. 12 + 6 * log2((double) avctx->global_quality / FF_QP2LAMBDA);
  122. x4->params.i_width = avctx->width;
  123. x4->params.i_height = avctx->height;
  124. x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num;
  125. x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den;
  126. x4->params.i_fps_num = avctx->time_base.den;
  127. x4->params.i_fps_den = avctx->time_base.num;
  128. x4->params.i_threads = avctx->thread_count;
  129. x4->enc = x264_encoder_open(&x4->params);
  130. if(!x4->enc)
  131. return -1;
  132. avctx->coded_frame = &x4->out_pic;
  133. return 0;
  134. }
  135. AVCodec x264_encoder = {
  136. .name = "h264",
  137. .type = CODEC_TYPE_VIDEO,
  138. .id = CODEC_ID_H264,
  139. .priv_data_size = sizeof(X264Context),
  140. .init = X264_init,
  141. .encode = X264_frame,
  142. .close = X264_close,
  143. .pix_fmts = (enum PixelFormat[]) { PIX_FMT_YUV420P, -1 }
  144. };