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.

296 lines
9.3KB

  1. /*
  2. * H.264 encoding using the x264 library
  3. * Copyright (C) 2005 Mans Rullgard <mru@inprovide.com>
  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 "avcodec.h"
  22. #include <x264.h>
  23. #include <math.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. typedef struct X264Context {
  28. x264_param_t params;
  29. x264_t *enc;
  30. x264_picture_t pic;
  31. AVFrame out_pic;
  32. } X264Context;
  33. static void
  34. X264_log(void *p, int level, const char *fmt, va_list args)
  35. {
  36. static const int level_map[] = {
  37. [X264_LOG_ERROR] = AV_LOG_ERROR,
  38. [X264_LOG_WARNING] = AV_LOG_ERROR,
  39. [X264_LOG_INFO] = AV_LOG_INFO,
  40. [X264_LOG_DEBUG] = AV_LOG_DEBUG
  41. };
  42. if(level < 0 || level > X264_LOG_DEBUG)
  43. return;
  44. av_vlog(p, level_map[level], fmt, args);
  45. }
  46. static int
  47. encode_nals(uint8_t *buf, int size, x264_nal_t *nals, int nnal)
  48. {
  49. uint8_t *p = buf;
  50. int i;
  51. for(i = 0; i < nnal; i++){
  52. int s = x264_nal_encode(p, &size, 1, nals + i);
  53. if(s < 0)
  54. return -1;
  55. p += s;
  56. }
  57. return p - buf;
  58. }
  59. extern int
  60. X264_frame(AVCodecContext *ctx, uint8_t *buf, int bufsize, void *data)
  61. {
  62. X264Context *x4 = ctx->priv_data;
  63. AVFrame *frame = data;
  64. x264_nal_t *nal;
  65. int nnal, i;
  66. x264_picture_t pic_out;
  67. x4->pic.img.i_csp = X264_CSP_I420;
  68. x4->pic.img.i_plane = 3;
  69. for(i = 0; i < 3; i++){
  70. x4->pic.img.plane[i] = frame->data[i];
  71. x4->pic.img.i_stride[i] = frame->linesize[i];
  72. }
  73. x4->pic.i_pts = frame->pts;
  74. x4->pic.i_type = X264_TYPE_AUTO;
  75. if(x264_encoder_encode(x4->enc, &nal, &nnal, &x4->pic, &pic_out))
  76. return -1;
  77. bufsize = encode_nals(buf, bufsize, nal, nnal);
  78. if(bufsize < 0)
  79. return -1;
  80. /* FIXME: dts */
  81. x4->out_pic.pts = pic_out.i_pts;
  82. switch(pic_out.i_type){
  83. case X264_TYPE_IDR:
  84. case X264_TYPE_I:
  85. x4->out_pic.pict_type = FF_I_TYPE;
  86. break;
  87. case X264_TYPE_P:
  88. x4->out_pic.pict_type = FF_P_TYPE;
  89. break;
  90. case X264_TYPE_B:
  91. case X264_TYPE_BREF:
  92. x4->out_pic.pict_type = FF_B_TYPE;
  93. break;
  94. }
  95. x4->out_pic.key_frame = pic_out.i_type == X264_TYPE_IDR;
  96. x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA;
  97. return bufsize;
  98. }
  99. static int
  100. X264_close(AVCodecContext *avctx)
  101. {
  102. X264Context *x4 = avctx->priv_data;
  103. if(x4->enc)
  104. x264_encoder_close(x4->enc);
  105. return 0;
  106. }
  107. extern int
  108. X264_init(AVCodecContext *avctx)
  109. {
  110. X264Context *x4 = avctx->priv_data;
  111. x264_param_default(&x4->params);
  112. x4->params.pf_log = X264_log;
  113. x4->params.p_log_private = avctx;
  114. x4->params.i_keyint_max = avctx->gop_size;
  115. x4->params.rc.i_bitrate = avctx->bit_rate / 1000;
  116. x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
  117. x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate / 1000;
  118. x4->params.rc.b_stat_write = (avctx->flags & CODEC_FLAG_PASS1);
  119. if(avctx->flags & CODEC_FLAG_PASS2) x4->params.rc.b_stat_read = 1;
  120. else{
  121. if(avctx->crf){
  122. x4->params.rc.i_rc_method = X264_RC_CRF;
  123. x4->params.rc.i_rf_constant = avctx->crf;
  124. }else if(avctx->cqp > -1){
  125. x4->params.rc.i_rc_method = X264_RC_CQP;
  126. x4->params.rc.i_qp_constant = avctx->cqp;
  127. }
  128. }
  129. // if neither crf nor cqp modes are selected we have to enable the RC
  130. // we do it this way because we cannot check if the bitrate has been set
  131. if(!(avctx->crf || (avctx->cqp > -1))) x4->params.rc.i_rc_method = X264_RC_ABR;
  132. x4->params.i_bframe = avctx->max_b_frames;
  133. x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC;
  134. x4->params.b_bframe_adaptive = avctx->b_frame_strategy;
  135. x4->params.i_bframe_bias = avctx->bframebias;
  136. x4->params.b_bframe_pyramid = (avctx->flags2 & CODEC_FLAG2_BPYRAMID);
  137. avctx->has_b_frames= (avctx->flags2 & CODEC_FLAG2_BPYRAMID) ? 2 : !!avctx->max_b_frames;
  138. x4->params.i_keyint_min = avctx->keyint_min;
  139. if(x4->params.i_keyint_min > x4->params.i_keyint_max)
  140. x4->params.i_keyint_min = x4->params.i_keyint_max;
  141. x4->params.i_scenecut_threshold = avctx->scenechange_threshold;
  142. x4->params.b_deblocking_filter = (avctx->flags & CODEC_FLAG_LOOP_FILTER);
  143. x4->params.i_deblocking_filter_alphac0 = avctx->deblockalpha;
  144. x4->params.i_deblocking_filter_beta = avctx->deblockbeta;
  145. x4->params.rc.i_qp_min = avctx->qmin;
  146. x4->params.rc.i_qp_max = avctx->qmax;
  147. x4->params.rc.i_qp_step = avctx->max_qdiff;
  148. x4->params.rc.f_qcompress = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */
  149. x4->params.rc.f_qblur = avctx->qblur; /* temporally blur quants */
  150. x4->params.rc.f_complexity_blur = avctx->complexityblur;
  151. x4->params.i_frame_reference = avctx->refs;
  152. x4->params.i_width = avctx->width;
  153. x4->params.i_height = avctx->height;
  154. x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num;
  155. x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den;
  156. x4->params.i_fps_num = avctx->time_base.den;
  157. x4->params.i_fps_den = avctx->time_base.num;
  158. x4->params.analyse.inter = 0;
  159. if(avctx->partitions){
  160. if(avctx->partitions & X264_PART_I4X4)
  161. x4->params.analyse.inter |= X264_ANALYSE_I4x4;
  162. if(avctx->partitions & X264_PART_I8X8)
  163. x4->params.analyse.inter |= X264_ANALYSE_I8x8;
  164. if(avctx->partitions & X264_PART_P8X8)
  165. x4->params.analyse.inter |= X264_ANALYSE_PSUB16x16;
  166. if(avctx->partitions & X264_PART_P4X4)
  167. x4->params.analyse.inter |= X264_ANALYSE_PSUB8x8;
  168. if(avctx->partitions & X264_PART_B8X8)
  169. x4->params.analyse.inter |= X264_ANALYSE_BSUB16x16;
  170. }
  171. x4->params.analyse.i_direct_mv_pred = avctx->directpred;
  172. x4->params.analyse.b_weighted_bipred = (avctx->flags2 & CODEC_FLAG2_WPRED);
  173. if(avctx->me_method == ME_EPZS)
  174. x4->params.analyse.i_me_method = X264_ME_DIA;
  175. else if(avctx->me_method == ME_HEX)
  176. x4->params.analyse.i_me_method = X264_ME_HEX;
  177. else if(avctx->me_method == ME_UMH)
  178. x4->params.analyse.i_me_method = X264_ME_UMH;
  179. else if(avctx->me_method == ME_FULL)
  180. x4->params.analyse.i_me_method = X264_ME_ESA;
  181. else x4->params.analyse.i_me_method = X264_ME_HEX;
  182. x4->params.analyse.i_me_range = avctx->me_range;
  183. x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality;
  184. x4->params.analyse.b_bframe_rdo = (avctx->flags2 & CODEC_FLAG2_BRDO);
  185. x4->params.analyse.b_mixed_references =
  186. (avctx->flags2 & CODEC_FLAG2_MIXED_REFS);
  187. x4->params.analyse.b_chroma_me = (avctx->me_cmp & FF_CMP_CHROMA);
  188. x4->params.analyse.b_transform_8x8 = (avctx->flags2 & CODEC_FLAG2_8X8DCT);
  189. x4->params.analyse.b_fast_pskip = (avctx->flags2 & CODEC_FLAG2_FASTPSKIP);
  190. x4->params.analyse.i_trellis = avctx->trellis;
  191. x4->params.analyse.i_noise_reduction = avctx->noise_reduction;
  192. if(avctx->level > 0) x4->params.i_level_idc = avctx->level;
  193. x4->params.rc.f_rate_tolerance =
  194. (float)avctx->bit_rate_tolerance/avctx->bit_rate;
  195. if((avctx->rc_buffer_size != 0) &&
  196. (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)){
  197. x4->params.rc.f_vbv_buffer_init =
  198. (float)avctx->rc_initial_buffer_occupancy/avctx->rc_buffer_size;
  199. }
  200. else x4->params.rc.f_vbv_buffer_init = 0.9;
  201. x4->params.rc.f_ip_factor = 1/fabs(avctx->i_quant_factor);
  202. x4->params.rc.f_pb_factor = avctx->b_quant_factor;
  203. x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
  204. x4->params.rc.psz_rc_eq = avctx->rc_eq;
  205. x4->params.analyse.b_psnr = (avctx->flags & CODEC_FLAG_PSNR);
  206. x4->params.i_log_level = X264_LOG_DEBUG;
  207. x4->params.b_aud = (avctx->flags2 & CODEC_FLAG2_AUD);
  208. x4->params.i_threads = avctx->thread_count;
  209. if(avctx->flags & CODEC_FLAG_GLOBAL_HEADER){
  210. x4->params.b_repeat_headers = 0;
  211. }
  212. x4->enc = x264_encoder_open(&x4->params);
  213. if(!x4->enc)
  214. return -1;
  215. avctx->coded_frame = &x4->out_pic;
  216. if(avctx->flags & CODEC_FLAG_GLOBAL_HEADER){
  217. x264_nal_t *nal;
  218. int nnal, i, s = 0;
  219. x264_encoder_headers(x4->enc, &nal, &nnal);
  220. /* 5 bytes NAL header + worst case escaping */
  221. for(i = 0; i < nnal; i++)
  222. s += 5 + nal[i].i_payload * 4 / 3;
  223. avctx->extradata = av_malloc(s);
  224. avctx->extradata_size = encode_nals(avctx->extradata, s, nal, nnal);
  225. }
  226. return 0;
  227. }
  228. AVCodec x264_encoder = {
  229. .name = "h264",
  230. .type = CODEC_TYPE_VIDEO,
  231. .id = CODEC_ID_H264,
  232. .priv_data_size = sizeof(X264Context),
  233. .init = X264_init,
  234. .encode = X264_frame,
  235. .close = X264_close,
  236. .pix_fmts = (enum PixelFormat[]) { PIX_FMT_YUV420P, -1 }
  237. };