libx264.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * H.264 encoding using the x264 library
  3. * Copyright (C) 2005 Mans Rullgard <mans@mansr.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. uint8_t *sei;
  32. int sei_size;
  33. AVFrame out_pic;
  34. } X264Context;
  35. static void X264_log(void *p, int level, const char *fmt, va_list args)
  36. {
  37. static const int level_map[] = {
  38. [X264_LOG_ERROR] = AV_LOG_ERROR,
  39. [X264_LOG_WARNING] = AV_LOG_WARNING,
  40. [X264_LOG_INFO] = AV_LOG_INFO,
  41. [X264_LOG_DEBUG] = AV_LOG_DEBUG
  42. };
  43. if (level < 0 || level > X264_LOG_DEBUG)
  44. return;
  45. av_vlog(p, level_map[level], fmt, args);
  46. }
  47. #if X264_BUILD >= 76
  48. static int encode_nals(AVCodecContext *ctx, uint8_t *buf, int size,
  49. x264_nal_t *nals, int nnal, int skip_sei)
  50. {
  51. X264Context *x4 = ctx->priv_data;
  52. uint8_t *p = buf;
  53. int i;
  54. /* Write the SEI as part of the first frame. */
  55. if (x4->sei_size > 0 && nnal > 0) {
  56. memcpy(p, x4->sei, x4->sei_size);
  57. p += x4->sei_size;
  58. x4->sei_size = 0;
  59. }
  60. for (i = 0; i < nnal; i++){
  61. /* Don't put the SEI in extradata. */
  62. if (skip_sei && nals[i].i_type == NAL_SEI) {
  63. x4->sei_size = nals[i].i_payload;
  64. x4->sei = av_malloc(x4->sei_size);
  65. memcpy(x4->sei, nals[i].p_payload, nals[i].i_payload);
  66. continue;
  67. }
  68. memcpy(p, nals[i].p_payload, nals[i].i_payload);
  69. p += nals[i].i_payload;
  70. }
  71. return p - buf;
  72. }
  73. #else
  74. static int encode_nals(AVCodecContext *ctx, uint8_t *buf, int size, x264_nal_t *nals, int nnal, int skip_sei)
  75. {
  76. X264Context *x4 = ctx->priv_data;
  77. uint8_t *p = buf;
  78. int i, s;
  79. /* Write the SEI as part of the first frame. */
  80. if (x4->sei_size > 0 && nnal > 0) {
  81. memcpy(p, x4->sei, x4->sei_size);
  82. p += x4->sei_size;
  83. x4->sei_size = 0;
  84. }
  85. for (i = 0; i < nnal; i++) {
  86. /* Don't put the SEI in extradata. */
  87. if (skip_sei && nals[i].i_type == NAL_SEI) {
  88. x4->sei = av_malloc( 5 + nals[i].i_payload * 4 / 3 );
  89. if(x264_nal_encode(x4->sei, &x4->sei_size, 1, nals + i) < 0)
  90. return -1;
  91. continue;
  92. }
  93. s = x264_nal_encode(p, &size, 1, nals + i);
  94. if (s < 0)
  95. return -1;
  96. p += s;
  97. }
  98. return p - buf;
  99. }
  100. #endif
  101. static int X264_frame(AVCodecContext *ctx, uint8_t *buf,
  102. int bufsize, void *data)
  103. {
  104. X264Context *x4 = ctx->priv_data;
  105. AVFrame *frame = data;
  106. x264_nal_t *nal;
  107. int nnal, i;
  108. x264_picture_t pic_out;
  109. x4->pic.img.i_csp = X264_CSP_I420;
  110. x4->pic.img.i_plane = 3;
  111. if (frame) {
  112. for (i = 0; i < 3; i++) {
  113. x4->pic.img.plane[i] = frame->data[i];
  114. x4->pic.img.i_stride[i] = frame->linesize[i];
  115. }
  116. x4->pic.i_pts = frame->pts;
  117. x4->pic.i_type = X264_TYPE_AUTO;
  118. }
  119. if (x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL, &pic_out) < 0)
  120. return -1;
  121. bufsize = encode_nals(ctx, buf, bufsize, nal, nnal, 0);
  122. if (bufsize < 0)
  123. return -1;
  124. /* FIXME: libx264 now provides DTS, but AVFrame doesn't have a field for it. */
  125. x4->out_pic.pts = pic_out.i_pts;
  126. switch (pic_out.i_type) {
  127. case X264_TYPE_IDR:
  128. case X264_TYPE_I:
  129. x4->out_pic.pict_type = FF_I_TYPE;
  130. break;
  131. case X264_TYPE_P:
  132. x4->out_pic.pict_type = FF_P_TYPE;
  133. break;
  134. case X264_TYPE_B:
  135. case X264_TYPE_BREF:
  136. x4->out_pic.pict_type = FF_B_TYPE;
  137. break;
  138. }
  139. #if X264_BUILD < 82
  140. x4->out_pic.key_frame = pic_out.i_type == X264_TYPE_IDR;
  141. #else
  142. x4->out_pic.key_frame = pic_out.b_keyframe;
  143. #endif
  144. x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA;
  145. return bufsize;
  146. }
  147. static av_cold int X264_close(AVCodecContext *avctx)
  148. {
  149. X264Context *x4 = avctx->priv_data;
  150. av_freep(&avctx->extradata);
  151. av_free(x4->sei);
  152. if (x4->enc)
  153. x264_encoder_close(x4->enc);
  154. return 0;
  155. }
  156. static av_cold int X264_init(AVCodecContext *avctx)
  157. {
  158. X264Context *x4 = avctx->priv_data;
  159. x4->sei_size = 0;
  160. x264_param_default(&x4->params);
  161. x4->params.pf_log = X264_log;
  162. x4->params.p_log_private = avctx;
  163. x4->params.i_keyint_max = avctx->gop_size;
  164. x4->params.rc.i_bitrate = avctx->bit_rate / 1000;
  165. x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
  166. x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate / 1000;
  167. x4->params.rc.b_stat_write = avctx->flags & CODEC_FLAG_PASS1;
  168. if (avctx->flags & CODEC_FLAG_PASS2) {
  169. x4->params.rc.b_stat_read = 1;
  170. } else {
  171. if (avctx->crf) {
  172. x4->params.rc.i_rc_method = X264_RC_CRF;
  173. x4->params.rc.f_rf_constant = avctx->crf;
  174. } else if (avctx->cqp > -1) {
  175. x4->params.rc.i_rc_method = X264_RC_CQP;
  176. x4->params.rc.i_qp_constant = avctx->cqp;
  177. }
  178. }
  179. // if neither crf nor cqp modes are selected we have to enable the RC
  180. // we do it this way because we cannot check if the bitrate has been set
  181. if (!(avctx->crf || (avctx->cqp > -1)))
  182. x4->params.rc.i_rc_method = X264_RC_ABR;
  183. x4->params.i_bframe = avctx->max_b_frames;
  184. x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC;
  185. x4->params.i_bframe_adaptive = avctx->b_frame_strategy;
  186. x4->params.i_bframe_bias = avctx->bframebias;
  187. #if X264_BUILD >= 78
  188. x4->params.i_bframe_pyramid = avctx->flags2 & CODEC_FLAG2_BPYRAMID ? X264_B_PYRAMID_NORMAL : X264_B_PYRAMID_NONE;
  189. #else
  190. x4->params.b_bframe_pyramid = avctx->flags2 & CODEC_FLAG2_BPYRAMID;
  191. #endif
  192. avctx->has_b_frames = avctx->flags2 & CODEC_FLAG2_BPYRAMID ? 2 : !!avctx->max_b_frames;
  193. x4->params.i_keyint_min = avctx->keyint_min;
  194. if (x4->params.i_keyint_min > x4->params.i_keyint_max)
  195. x4->params.i_keyint_min = x4->params.i_keyint_max;
  196. x4->params.i_scenecut_threshold = avctx->scenechange_threshold;
  197. x4->params.b_deblocking_filter = avctx->flags & CODEC_FLAG_LOOP_FILTER;
  198. x4->params.i_deblocking_filter_alphac0 = avctx->deblockalpha;
  199. x4->params.i_deblocking_filter_beta = avctx->deblockbeta;
  200. x4->params.rc.i_qp_min = avctx->qmin;
  201. x4->params.rc.i_qp_max = avctx->qmax;
  202. x4->params.rc.i_qp_step = avctx->max_qdiff;
  203. x4->params.rc.f_qcompress = avctx->qcompress; /* 0.0 => cbr, 1.0 => constant qp */
  204. x4->params.rc.f_qblur = avctx->qblur; /* temporally blur quants */
  205. x4->params.rc.f_complexity_blur = avctx->complexityblur;
  206. x4->params.i_frame_reference = avctx->refs;
  207. x4->params.i_width = avctx->width;
  208. x4->params.i_height = avctx->height;
  209. x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num;
  210. x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den;
  211. #if X264_BUILD >= 81
  212. x4->params.i_fps_num = x4->params.i_timebase_den = avctx->time_base.den;
  213. x4->params.i_fps_den = x4->params.i_timebase_num = avctx->time_base.num;
  214. #endif
  215. x4->params.analyse.inter = 0;
  216. if (avctx->partitions) {
  217. if (avctx->partitions & X264_PART_I4X4)
  218. x4->params.analyse.inter |= X264_ANALYSE_I4x4;
  219. if (avctx->partitions & X264_PART_I8X8)
  220. x4->params.analyse.inter |= X264_ANALYSE_I8x8;
  221. if (avctx->partitions & X264_PART_P8X8)
  222. x4->params.analyse.inter |= X264_ANALYSE_PSUB16x16;
  223. if (avctx->partitions & X264_PART_P4X4)
  224. x4->params.analyse.inter |= X264_ANALYSE_PSUB8x8;
  225. if (avctx->partitions & X264_PART_B8X8)
  226. x4->params.analyse.inter |= X264_ANALYSE_BSUB16x16;
  227. }
  228. x4->params.analyse.i_direct_mv_pred = avctx->directpred;
  229. x4->params.analyse.b_weighted_bipred = avctx->flags2 & CODEC_FLAG2_WPRED;
  230. #if X264_BUILD >= 79
  231. x4->params.analyse.i_weighted_pred = avctx->weighted_p_pred;
  232. #endif
  233. if (avctx->me_method == ME_EPZS)
  234. x4->params.analyse.i_me_method = X264_ME_DIA;
  235. else if (avctx->me_method == ME_HEX)
  236. x4->params.analyse.i_me_method = X264_ME_HEX;
  237. else if (avctx->me_method == ME_UMH)
  238. x4->params.analyse.i_me_method = X264_ME_UMH;
  239. else if (avctx->me_method == ME_FULL)
  240. x4->params.analyse.i_me_method = X264_ME_ESA;
  241. else if (avctx->me_method == ME_TESA)
  242. x4->params.analyse.i_me_method = X264_ME_TESA;
  243. else x4->params.analyse.i_me_method = X264_ME_HEX;
  244. x4->params.analyse.i_me_range = avctx->me_range;
  245. x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality;
  246. x4->params.analyse.b_mixed_references = avctx->flags2 & CODEC_FLAG2_MIXED_REFS;
  247. x4->params.analyse.b_chroma_me = avctx->me_cmp & FF_CMP_CHROMA;
  248. x4->params.analyse.b_transform_8x8 = avctx->flags2 & CODEC_FLAG2_8X8DCT;
  249. x4->params.analyse.b_fast_pskip = avctx->flags2 & CODEC_FLAG2_FASTPSKIP;
  250. x4->params.analyse.i_trellis = avctx->trellis;
  251. x4->params.analyse.i_noise_reduction = avctx->noise_reduction;
  252. if (avctx->level > 0)
  253. x4->params.i_level_idc = avctx->level;
  254. x4->params.rc.f_rate_tolerance =
  255. (float)avctx->bit_rate_tolerance/avctx->bit_rate;
  256. if ((avctx->rc_buffer_size != 0) &&
  257. (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)) {
  258. x4->params.rc.f_vbv_buffer_init =
  259. (float)avctx->rc_initial_buffer_occupancy / avctx->rc_buffer_size;
  260. } else
  261. x4->params.rc.f_vbv_buffer_init = 0.9;
  262. #if X264_BUILD >= 69
  263. x4->params.rc.b_mb_tree = !!(avctx->flags2 & CODEC_FLAG2_MBTREE);
  264. #endif
  265. x4->params.rc.f_ip_factor = 1 / fabs(avctx->i_quant_factor);
  266. x4->params.rc.f_pb_factor = avctx->b_quant_factor;
  267. x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
  268. x4->params.analyse.b_psnr = avctx->flags & CODEC_FLAG_PSNR;
  269. x4->params.i_log_level = X264_LOG_DEBUG;
  270. x4->params.b_aud = avctx->flags2 & CODEC_FLAG2_AUD;
  271. x4->params.i_threads = avctx->thread_count;
  272. x4->params.b_interlaced = avctx->flags & CODEC_FLAG_INTERLACED_DCT;
  273. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER)
  274. x4->params.b_repeat_headers = 0;
  275. x4->enc = x264_encoder_open(&x4->params);
  276. if (!x4->enc)
  277. return -1;
  278. avctx->coded_frame = &x4->out_pic;
  279. #if X264_BUILD >= 76
  280. if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
  281. x264_nal_t *nal;
  282. int nnal, s;
  283. s = x264_encoder_headers(x4->enc, &nal, &nnal);
  284. avctx->extradata = av_malloc(s);
  285. avctx->extradata_size = encode_nals(avctx, avctx->extradata, s, nal, nnal, 1);
  286. }
  287. #else
  288. if(avctx->flags & CODEC_FLAG_GLOBAL_HEADER){
  289. x264_nal_t *nal;
  290. int nnal, i, s = 0;
  291. x264_encoder_headers(x4->enc, &nal, &nnal);
  292. /* 5 bytes NAL header + worst case escaping */
  293. for (i = 0; i < nnal; i++)
  294. s += 5 + nal[i].i_payload * 4 / 3;
  295. avctx->extradata = av_malloc(s);
  296. avctx->extradata_size = encode_nals(avctx, avctx->extradata, s, nal, nnal, 1);
  297. }
  298. #endif
  299. return 0;
  300. }
  301. AVCodec libx264_encoder = {
  302. .name = "libx264",
  303. .type = CODEC_TYPE_VIDEO,
  304. .id = CODEC_ID_H264,
  305. .priv_data_size = sizeof(X264Context),
  306. .init = X264_init,
  307. .encode = X264_frame,
  308. .close = X264_close,
  309. .capabilities = CODEC_CAP_DELAY,
  310. .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_YUV420P, PIX_FMT_NONE },
  311. .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  312. };