4mv + iter ME 10l fix (still not bugfree but better)
[libav.git] / libavcodec / x264.c
CommitLineData
6662ec29
MN
1/*
2 * H.264 encoding using the x264 library
f9791751 3 * Copyright (C) 2005 Mans Rullgard <mru@inprovide.com>
6662ec29
MN
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
20#include "avcodec.h"
21#include <x264.h>
337afd14 22#include <math.h>
6662ec29
MN
23
24typedef struct X264Context {
25 x264_param_t params;
26 x264_t *enc;
27 x264_picture_t pic;
28 AVFrame out_pic;
29} X264Context;
30
31static void
32X264_log(void *p, int level, const char *fmt, va_list args)
33{
34 static const int level_map[] = {
bb270c08
DB
35 [X264_LOG_ERROR] = AV_LOG_ERROR,
36 [X264_LOG_WARNING] = AV_LOG_ERROR,
37 [X264_LOG_INFO] = AV_LOG_INFO,
38 [X264_LOG_DEBUG] = AV_LOG_DEBUG
6662ec29
MN
39 };
40
41 if(level < 0 || level > X264_LOG_DEBUG)
bb270c08 42 return;
6662ec29
MN
43
44 av_vlog(p, level_map[level], fmt, args);
45}
46
47
48static int
f40487c2 49encode_nals(uint8_t *buf, int size, x264_nal_t *nals, int nnal)
6662ec29 50{
f40487c2 51 uint8_t *p = buf;
6662ec29
MN
52 int i;
53
54 for(i = 0; i < nnal; i++){
bb270c08
DB
55 int s = x264_nal_encode(p, &size, 1, nals + i);
56 if(s < 0)
57 return -1;
58 p += s;
6662ec29
MN
59 }
60
61 return p - buf;
62}
63
64extern int
65X264_frame(AVCodecContext *ctx, uint8_t *buf, int bufsize, void *data)
66{
67 X264Context *x4 = ctx->priv_data;
68 AVFrame *frame = data;
69 x264_nal_t *nal;
70 int nnal, i;
71 x264_picture_t pic_out;
72
73 x4->pic.img.i_csp = X264_CSP_I420;
74 x4->pic.img.i_plane = 3;
75
76 for(i = 0; i < 3; i++){
bb270c08
DB
77 x4->pic.img.plane[i] = frame->data[i];
78 x4->pic.img.i_stride[i] = frame->linesize[i];
6662ec29
MN
79 }
80
81 x4->pic.i_pts = frame->pts;
82 x4->pic.i_type = X264_TYPE_AUTO;
83
84 if(x264_encoder_encode(x4->enc, &nal, &nnal, &x4->pic, &pic_out))
bb270c08 85 return -1;
6662ec29
MN
86
87 bufsize = encode_nals(buf, bufsize, nal, nnal);
88 if(bufsize < 0)
bb270c08 89 return -1;
6662ec29
MN
90
91 /* FIXME: dts */
92 x4->out_pic.pts = pic_out.i_pts;
93
94 switch(pic_out.i_type){
95 case X264_TYPE_IDR:
96 case X264_TYPE_I:
97 x4->out_pic.pict_type = FF_I_TYPE;
98 break;
99 case X264_TYPE_P:
100 x4->out_pic.pict_type = FF_P_TYPE;
101 break;
102 case X264_TYPE_B:
103 case X264_TYPE_BREF:
104 x4->out_pic.pict_type = FF_B_TYPE;
105 break;
106 }
107
68c6d60f
MR
108 x4->out_pic.key_frame = pic_out.i_type == X264_TYPE_IDR;
109 x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA;
6662ec29
MN
110
111 return bufsize;
112}
113
114static int
115X264_close(AVCodecContext *avctx)
116{
117 X264Context *x4 = avctx->priv_data;
118
119 if(x4->enc)
bb270c08 120 x264_encoder_close(x4->enc);
6662ec29
MN
121
122 return 0;
123}
124
125extern int
126X264_init(AVCodecContext *avctx)
127{
128 X264Context *x4 = avctx->priv_data;
129
130 x264_param_default(&x4->params);
131
132 x4->params.pf_log = X264_log;
133 x4->params.p_log_private = avctx;
134
135 x4->params.i_keyint_max = avctx->gop_size;
136 x4->params.rc.i_bitrate = avctx->bit_rate / 1000;
3f9d3777 137 x4->params.rc.i_vbv_buffer_size = avctx->rc_buffer_size / 1000;
18ae520b 138 x4->params.rc.i_vbv_max_bitrate = avctx->rc_max_rate / 1000;
6662ec29
MN
139 if(avctx->rc_buffer_size)
140 x4->params.rc.b_cbr = 1;
d4e87f79
MR
141 x4->params.i_bframe = avctx->max_b_frames;
142 x4->params.b_cabac = avctx->coder_type == FF_CODER_TYPE_AC;
f9791751 143
6662ec29
MN
144 x4->params.rc.i_qp_min = avctx->qmin;
145 x4->params.rc.i_qp_max = avctx->qmax;
146 x4->params.rc.i_qp_step = avctx->max_qdiff;
147
18ae520b
DA
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
68c6d60f
MR
151 if(avctx->flags & CODEC_FLAG_QSCALE && avctx->global_quality > 0)
152 x4->params.rc.i_qp_constant =
68dce79b 153 12 + 6 * log2((double) avctx->global_quality / FF_QP2LAMBDA);
68c6d60f 154
6662ec29
MN
155 x4->params.i_width = avctx->width;
156 x4->params.i_height = avctx->height;
157 x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num;
158 x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den;
c0df9d75
MN
159 x4->params.i_fps_num = avctx->time_base.den;
160 x4->params.i_fps_den = avctx->time_base.num;
6662ec29 161
568d4b81
MR
162 x4->params.i_threads = avctx->thread_count;
163
6662ec29
MN
164 x4->enc = x264_encoder_open(&x4->params);
165 if(!x4->enc)
166 return -1;
167
168 avctx->coded_frame = &x4->out_pic;
169
170 return 0;
171}
172
173AVCodec x264_encoder = {
174 .name = "h264",
175 .type = CODEC_TYPE_VIDEO,
176 .id = CODEC_ID_H264,
177 .priv_data_size = sizeof(X264Context),
178 .init = X264_init,
179 .encode = X264_frame,
180 .close = X264_close,
181 .pix_fmts = (enum PixelFormat[]) { PIX_FMT_YUV420P, -1 }
182};