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.

168 lines
4.1KB

  1. /*
  2. * H.264 encoding using the x264 library
  3. * Copyright (C) 2005 Måns Rullgård <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. typedef struct X264Context {
  22. x264_param_t params;
  23. x264_t *enc;
  24. x264_picture_t pic;
  25. AVFrame out_pic;
  26. } X264Context;
  27. static void
  28. X264_log(void *p, int level, const char *fmt, va_list args)
  29. {
  30. static const int level_map[] = {
  31. [X264_LOG_ERROR] = AV_LOG_ERROR,
  32. [X264_LOG_WARNING] = AV_LOG_ERROR,
  33. [X264_LOG_INFO] = AV_LOG_INFO,
  34. [X264_LOG_DEBUG] = AV_LOG_DEBUG
  35. };
  36. if(level < 0 || level > X264_LOG_DEBUG)
  37. return;
  38. av_vlog(p, level_map[level], fmt, args);
  39. }
  40. static int
  41. encode_nals(u_char *buf, int size, x264_nal_t *nals, int nnal)
  42. {
  43. u_char *p = buf;
  44. int i;
  45. for(i = 0; i < nnal; i++){
  46. int s = x264_nal_encode(p, &size, 1, nals + i);
  47. if(s < 0)
  48. return -1;
  49. p += s;
  50. }
  51. return p - buf;
  52. }
  53. extern int
  54. X264_frame(AVCodecContext *ctx, uint8_t *buf, int bufsize, void *data)
  55. {
  56. X264Context *x4 = ctx->priv_data;
  57. AVFrame *frame = data;
  58. x264_nal_t *nal;
  59. int nnal, i;
  60. x264_picture_t pic_out;
  61. x4->pic.img.i_csp = X264_CSP_I420;
  62. x4->pic.img.i_plane = 3;
  63. for(i = 0; i < 3; i++){
  64. x4->pic.img.plane[i] = frame->data[i];
  65. x4->pic.img.i_stride[i] = frame->linesize[i];
  66. }
  67. x4->pic.i_pts = frame->pts;
  68. x4->pic.i_type = X264_TYPE_AUTO;
  69. if(x264_encoder_encode(x4->enc, &nal, &nnal, &x4->pic, &pic_out))
  70. return -1;
  71. bufsize = encode_nals(buf, bufsize, nal, nnal);
  72. if(bufsize < 0)
  73. return -1;
  74. /* FIXME: dts */
  75. x4->out_pic.pts = pic_out.i_pts;
  76. switch(pic_out.i_type){
  77. case X264_TYPE_IDR:
  78. case X264_TYPE_I:
  79. x4->out_pic.pict_type = FF_I_TYPE;
  80. break;
  81. case X264_TYPE_P:
  82. x4->out_pic.pict_type = FF_P_TYPE;
  83. break;
  84. case X264_TYPE_B:
  85. case X264_TYPE_BREF:
  86. x4->out_pic.pict_type = FF_B_TYPE;
  87. break;
  88. }
  89. x4->out_pic.key_frame = x4->out_pic.key_frame == FF_I_TYPE;
  90. return bufsize;
  91. }
  92. static int
  93. X264_close(AVCodecContext *avctx)
  94. {
  95. X264Context *x4 = avctx->priv_data;
  96. if(x4->enc)
  97. x264_encoder_close(x4->enc);
  98. return 0;
  99. }
  100. extern int
  101. X264_init(AVCodecContext *avctx)
  102. {
  103. X264Context *x4 = avctx->priv_data;
  104. x264_param_default(&x4->params);
  105. x4->params.pf_log = X264_log;
  106. x4->params.p_log_private = avctx;
  107. x4->params.i_keyint_max = avctx->gop_size;
  108. x4->params.rc.i_bitrate = avctx->bit_rate / 1000;
  109. x4->params.rc.i_rc_buffer_size = avctx->rc_buffer_size / 1000;
  110. if(avctx->rc_buffer_size)
  111. x4->params.rc.b_cbr = 1;
  112. x4->params.rc.i_qp_min = avctx->qmin;
  113. x4->params.rc.i_qp_max = avctx->qmax;
  114. x4->params.rc.i_qp_step = avctx->max_qdiff;
  115. x4->params.i_width = avctx->width;
  116. x4->params.i_height = avctx->height;
  117. x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num;
  118. x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den;
  119. x4->params.i_fps_num = avctx->frame_rate;
  120. x4->params.i_fps_den = avctx->frame_rate_base;
  121. x4->enc = x264_encoder_open(&x4->params);
  122. if(!x4->enc)
  123. return -1;
  124. avctx->coded_frame = &x4->out_pic;
  125. return 0;
  126. }
  127. AVCodec x264_encoder = {
  128. .name = "h264",
  129. .type = CODEC_TYPE_VIDEO,
  130. .id = CODEC_ID_H264,
  131. .priv_data_size = sizeof(X264Context),
  132. .init = X264_init,
  133. .encode = X264_frame,
  134. .close = X264_close,
  135. .pix_fmts = (enum PixelFormat[]) { PIX_FMT_YUV420P, -1 }
  136. };