00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include <x264.h>
00024 #include <math.h>
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028
00029 typedef struct X264Context {
00030 x264_param_t params;
00031 x264_t *enc;
00032 x264_picture_t pic;
00033 AVFrame out_pic;
00034 } X264Context;
00035
00036 static void
00037 X264_log(void *p, int level, const char *fmt, va_list args)
00038 {
00039 static const int level_map[] = {
00040 [X264_LOG_ERROR] = AV_LOG_ERROR,
00041 [X264_LOG_WARNING] = AV_LOG_WARNING,
00042 [X264_LOG_INFO] = AV_LOG_INFO,
00043 [X264_LOG_DEBUG] = AV_LOG_DEBUG
00044 };
00045
00046 if(level < 0 || level > X264_LOG_DEBUG)
00047 return;
00048
00049 av_vlog(p, level_map[level], fmt, args);
00050 }
00051
00052
00053 static int
00054 encode_nals(uint8_t *buf, int size, x264_nal_t *nals, int nnal)
00055 {
00056 uint8_t *p = buf;
00057 int i;
00058
00059 for(i = 0; i < nnal; i++){
00060 int s = x264_nal_encode(p, &size, 1, nals + i);
00061 if(s < 0)
00062 return -1;
00063 p += s;
00064 }
00065
00066 return p - buf;
00067 }
00068
00069 static int
00070 X264_frame(AVCodecContext *ctx, uint8_t *buf, int bufsize, void *data)
00071 {
00072 X264Context *x4 = ctx->priv_data;
00073 AVFrame *frame = data;
00074 x264_nal_t *nal;
00075 int nnal, i;
00076 x264_picture_t pic_out;
00077
00078 x4->pic.img.i_csp = X264_CSP_I420;
00079 x4->pic.img.i_plane = 3;
00080
00081 if (frame) {
00082 for(i = 0; i < 3; i++){
00083 x4->pic.img.plane[i] = frame->data[i];
00084 x4->pic.img.i_stride[i] = frame->linesize[i];
00085 }
00086
00087 x4->pic.i_pts = frame->pts;
00088 x4->pic.i_type = X264_TYPE_AUTO;
00089 }
00090
00091 if(x264_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL,
00092 &pic_out))
00093 return -1;
00094
00095 bufsize = encode_nals(buf, bufsize, nal, nnal);
00096 if(bufsize < 0)
00097 return -1;
00098
00099
00100 x4->out_pic.pts = pic_out.i_pts;
00101
00102 switch(pic_out.i_type){
00103 case X264_TYPE_IDR:
00104 case X264_TYPE_I:
00105 x4->out_pic.pict_type = FF_I_TYPE;
00106 break;
00107 case X264_TYPE_P:
00108 x4->out_pic.pict_type = FF_P_TYPE;
00109 break;
00110 case X264_TYPE_B:
00111 case X264_TYPE_BREF:
00112 x4->out_pic.pict_type = FF_B_TYPE;
00113 break;
00114 }
00115
00116 x4->out_pic.key_frame = pic_out.i_type == X264_TYPE_IDR;
00117 x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA;
00118
00119 return bufsize;
00120 }
00121
00122 static av_cold int
00123 X264_close(AVCodecContext *avctx)
00124 {
00125 X264Context *x4 = avctx->priv_data;
00126
00127 av_freep(&avctx->extradata);
00128
00129 if(x4->enc)
00130 x264_encoder_close(x4->enc);
00131
00132 return 0;
00133 }
00134
00135 static av_cold int
00136 X264_init(AVCodecContext *avctx)
00137 {
00138 X264Context *x4 = avctx->priv_data;
00139
00140 x264_param_default(&x4->params);
00141
00142 x4->params.pf_log = X264_log;
00143 x4->params.p_log_private = avctx;
00144
00145 x4->params.i_keyint_max = avctx->gop_size;
00146 x4->params.rc.i_bitrate = avctx->bit_rate / 1000;
00147 x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
00148 x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate / 1000;
00149 x4->params.rc.b_stat_write = avctx->flags & CODEC_FLAG_PASS1;
00150 if(avctx->flags & CODEC_FLAG_PASS2) x4->params.rc.b_stat_read = 1;
00151 else{
00152 if(avctx->crf){
00153 x4->params.rc.i_rc_method = X264_RC_CRF;
00154 x4->params.rc.f_rf_constant = avctx->crf;
00155 }else if(avctx->cqp > -1){
00156 x4->params.rc.i_rc_method = X264_RC_CQP;
00157 x4->params.rc.i_qp_constant = avctx->cqp;
00158 }
00159 }
00160
00161
00162
00163 if(!(avctx->crf || (avctx->cqp > -1))) x4->params.rc.i_rc_method = X264_RC_ABR;
00164
00165 x4->params.i_bframe = avctx->max_b_frames;
00166 x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC;
00167 x4->params.i_bframe_adaptive = avctx->b_frame_strategy;
00168 x4->params.i_bframe_bias = avctx->bframebias;
00169 x4->params.b_bframe_pyramid = avctx->flags2 & CODEC_FLAG2_BPYRAMID;
00170 avctx->has_b_frames= avctx->flags2 & CODEC_FLAG2_BPYRAMID ? 2 : !!avctx->max_b_frames;
00171
00172 x4->params.i_keyint_min = avctx->keyint_min;
00173 if(x4->params.i_keyint_min > x4->params.i_keyint_max)
00174 x4->params.i_keyint_min = x4->params.i_keyint_max;
00175
00176 x4->params.i_scenecut_threshold = avctx->scenechange_threshold;
00177
00178 x4->params.b_deblocking_filter = avctx->flags & CODEC_FLAG_LOOP_FILTER;
00179 x4->params.i_deblocking_filter_alphac0 = avctx->deblockalpha;
00180 x4->params.i_deblocking_filter_beta = avctx->deblockbeta;
00181
00182 x4->params.rc.i_qp_min = avctx->qmin;
00183 x4->params.rc.i_qp_max = avctx->qmax;
00184 x4->params.rc.i_qp_step = avctx->max_qdiff;
00185
00186 x4->params.rc.f_qcompress = avctx->qcompress;
00187 x4->params.rc.f_qblur = avctx->qblur;
00188 x4->params.rc.f_complexity_blur = avctx->complexityblur;
00189
00190 x4->params.i_frame_reference = avctx->refs;
00191
00192 x4->params.i_width = avctx->width;
00193 x4->params.i_height = avctx->height;
00194 x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num;
00195 x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den;
00196 x4->params.i_fps_num = avctx->time_base.den;
00197 x4->params.i_fps_den = avctx->time_base.num;
00198
00199 x4->params.analyse.inter = 0;
00200 if(avctx->partitions){
00201 if(avctx->partitions & X264_PART_I4X4)
00202 x4->params.analyse.inter |= X264_ANALYSE_I4x4;
00203 if(avctx->partitions & X264_PART_I8X8)
00204 x4->params.analyse.inter |= X264_ANALYSE_I8x8;
00205 if(avctx->partitions & X264_PART_P8X8)
00206 x4->params.analyse.inter |= X264_ANALYSE_PSUB16x16;
00207 if(avctx->partitions & X264_PART_P4X4)
00208 x4->params.analyse.inter |= X264_ANALYSE_PSUB8x8;
00209 if(avctx->partitions & X264_PART_B8X8)
00210 x4->params.analyse.inter |= X264_ANALYSE_BSUB16x16;
00211 }
00212
00213 x4->params.analyse.i_direct_mv_pred = avctx->directpred;
00214
00215 x4->params.analyse.b_weighted_bipred = avctx->flags2 & CODEC_FLAG2_WPRED;
00216
00217 if(avctx->me_method == ME_EPZS)
00218 x4->params.analyse.i_me_method = X264_ME_DIA;
00219 else if(avctx->me_method == ME_HEX)
00220 x4->params.analyse.i_me_method = X264_ME_HEX;
00221 else if(avctx->me_method == ME_UMH)
00222 x4->params.analyse.i_me_method = X264_ME_UMH;
00223 else if(avctx->me_method == ME_FULL)
00224 x4->params.analyse.i_me_method = X264_ME_ESA;
00225 else if(avctx->me_method == ME_TESA)
00226 x4->params.analyse.i_me_method = X264_ME_TESA;
00227 else x4->params.analyse.i_me_method = X264_ME_HEX;
00228
00229 x4->params.analyse.i_me_range = avctx->me_range;
00230 x4->params.analyse.i_subpel_refine = avctx->me_subpel_quality;
00231
00232 x4->params.analyse.b_mixed_references =
00233 avctx->flags2 & CODEC_FLAG2_MIXED_REFS;
00234 x4->params.analyse.b_chroma_me = avctx->me_cmp & FF_CMP_CHROMA;
00235 x4->params.analyse.b_transform_8x8 = avctx->flags2 & CODEC_FLAG2_8X8DCT;
00236 x4->params.analyse.b_fast_pskip = avctx->flags2 & CODEC_FLAG2_FASTPSKIP;
00237
00238 x4->params.analyse.i_trellis = avctx->trellis;
00239 x4->params.analyse.i_noise_reduction = avctx->noise_reduction;
00240
00241 if(avctx->level > 0) x4->params.i_level_idc = avctx->level;
00242
00243 x4->params.rc.f_rate_tolerance =
00244 (float)avctx->bit_rate_tolerance/avctx->bit_rate;
00245
00246 if((avctx->rc_buffer_size != 0) &&
00247 (avctx->rc_initial_buffer_occupancy <= avctx->rc_buffer_size)){
00248 x4->params.rc.f_vbv_buffer_init =
00249 (float)avctx->rc_initial_buffer_occupancy/avctx->rc_buffer_size;
00250 }
00251 else x4->params.rc.f_vbv_buffer_init = 0.9;
00252
00253 x4->params.rc.f_ip_factor = 1/fabs(avctx->i_quant_factor);
00254 x4->params.rc.f_pb_factor = avctx->b_quant_factor;
00255 x4->params.analyse.i_chroma_qp_offset = avctx->chromaoffset;
00256
00257 x4->params.analyse.b_psnr = avctx->flags & CODEC_FLAG_PSNR;
00258 x4->params.i_log_level = X264_LOG_DEBUG;
00259
00260 x4->params.b_aud = avctx->flags2 & CODEC_FLAG2_AUD;
00261
00262 x4->params.i_threads = avctx->thread_count;
00263
00264 x4->params.b_interlaced = avctx->flags & CODEC_FLAG_INTERLACED_DCT;
00265
00266 if(avctx->flags & CODEC_FLAG_GLOBAL_HEADER){
00267 x4->params.b_repeat_headers = 0;
00268 }
00269
00270 x4->enc = x264_encoder_open(&x4->params);
00271 if(!x4->enc)
00272 return -1;
00273
00274 avctx->coded_frame = &x4->out_pic;
00275
00276 if(avctx->flags & CODEC_FLAG_GLOBAL_HEADER){
00277 x264_nal_t *nal;
00278 int nnal, i, s = 0;
00279
00280 x264_encoder_headers(x4->enc, &nal, &nnal);
00281
00282
00283 for(i = 0; i < nnal; i++)
00284 s += 5 + nal[i].i_payload * 4 / 3;
00285
00286 avctx->extradata = av_malloc(s);
00287 avctx->extradata_size = encode_nals(avctx->extradata, s, nal, nnal);
00288 }
00289
00290 return 0;
00291 }
00292
00293 AVCodec libx264_encoder = {
00294 .name = "libx264",
00295 .type = CODEC_TYPE_VIDEO,
00296 .id = CODEC_ID_H264,
00297 .priv_data_size = sizeof(X264Context),
00298 .init = X264_init,
00299 .encode = X264_frame,
00300 .close = X264_close,
00301 .capabilities = CODEC_CAP_DELAY,
00302 .pix_fmts = (enum PixelFormat[]) { PIX_FMT_YUV420P, PIX_FMT_NONE },
00303 .long_name = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
00304 };