Mercurial > libavcodec.hg
comparison x264.c @ 2556:e5af3bc1d038 libavcodec
H.264 encoding with x264 by (M«©ns Rullg«©rd <mru inprovide com>)
author | michael |
---|---|
date | Wed, 09 Mar 2005 03:04:56 +0000 |
parents | |
children | 1e52ef4887b5 |
comparison
equal
deleted
inserted
replaced
2555:d69789acdbc4 | 2556:e5af3bc1d038 |
---|---|
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 | |
20 #include "avcodec.h" | |
21 #include <x264.h> | |
22 | |
23 typedef struct X264Context { | |
24 x264_param_t params; | |
25 x264_t *enc; | |
26 x264_picture_t pic; | |
27 AVFrame out_pic; | |
28 } X264Context; | |
29 | |
30 static void | |
31 X264_log(void *p, int level, const char *fmt, va_list args) | |
32 { | |
33 static const int level_map[] = { | |
34 [X264_LOG_ERROR] = AV_LOG_ERROR, | |
35 [X264_LOG_WARNING] = AV_LOG_ERROR, | |
36 [X264_LOG_INFO] = AV_LOG_INFO, | |
37 [X264_LOG_DEBUG] = AV_LOG_DEBUG | |
38 }; | |
39 | |
40 if(level < 0 || level > X264_LOG_DEBUG) | |
41 return; | |
42 | |
43 av_vlog(p, level_map[level], fmt, args); | |
44 } | |
45 | |
46 | |
47 static int | |
48 encode_nals(u_char *buf, int size, x264_nal_t *nals, int nnal) | |
49 { | |
50 u_char *p = buf; | |
51 int i; | |
52 | |
53 for(i = 0; i < nnal; i++){ | |
54 int s = x264_nal_encode(p, &size, 1, nals + i); | |
55 if(s < 0) | |
56 return -1; | |
57 p += s; | |
58 } | |
59 | |
60 return p - buf; | |
61 } | |
62 | |
63 extern int | |
64 X264_frame(AVCodecContext *ctx, uint8_t *buf, int bufsize, void *data) | |
65 { | |
66 X264Context *x4 = ctx->priv_data; | |
67 AVFrame *frame = data; | |
68 x264_nal_t *nal; | |
69 int nnal, i; | |
70 x264_picture_t pic_out; | |
71 | |
72 x4->pic.img.i_csp = X264_CSP_I420; | |
73 x4->pic.img.i_plane = 3; | |
74 | |
75 for(i = 0; i < 3; i++){ | |
76 x4->pic.img.plane[i] = frame->data[i]; | |
77 x4->pic.img.i_stride[i] = frame->linesize[i]; | |
78 } | |
79 | |
80 x4->pic.i_pts = frame->pts; | |
81 x4->pic.i_type = X264_TYPE_AUTO; | |
82 | |
83 if(x264_encoder_encode(x4->enc, &nal, &nnal, &x4->pic, &pic_out)) | |
84 return -1; | |
85 | |
86 bufsize = encode_nals(buf, bufsize, nal, nnal); | |
87 if(bufsize < 0) | |
88 return -1; | |
89 | |
90 /* FIXME: dts */ | |
91 x4->out_pic.pts = pic_out.i_pts; | |
92 | |
93 switch(pic_out.i_type){ | |
94 case X264_TYPE_IDR: | |
95 case X264_TYPE_I: | |
96 x4->out_pic.pict_type = FF_I_TYPE; | |
97 break; | |
98 case X264_TYPE_P: | |
99 x4->out_pic.pict_type = FF_P_TYPE; | |
100 break; | |
101 case X264_TYPE_B: | |
102 case X264_TYPE_BREF: | |
103 x4->out_pic.pict_type = FF_B_TYPE; | |
104 break; | |
105 } | |
106 | |
107 x4->out_pic.key_frame = x4->out_pic.key_frame == FF_I_TYPE; | |
108 | |
109 return bufsize; | |
110 } | |
111 | |
112 static int | |
113 X264_close(AVCodecContext *avctx) | |
114 { | |
115 X264Context *x4 = avctx->priv_data; | |
116 | |
117 if(x4->enc) | |
118 x264_encoder_close(x4->enc); | |
119 | |
120 return 0; | |
121 } | |
122 | |
123 extern int | |
124 X264_init(AVCodecContext *avctx) | |
125 { | |
126 X264Context *x4 = avctx->priv_data; | |
127 | |
128 x264_param_default(&x4->params); | |
129 | |
130 x4->params.pf_log = X264_log; | |
131 x4->params.p_log_private = avctx; | |
132 | |
133 x4->params.i_keyint_max = avctx->gop_size; | |
134 x4->params.rc.i_bitrate = avctx->bit_rate / 1000; | |
135 x4->params.rc.i_rc_buffer_size = avctx->rc_buffer_size / 1000; | |
136 if(avctx->rc_buffer_size) | |
137 x4->params.rc.b_cbr = 1; | |
138 x4->params.rc.i_qp_min = avctx->qmin; | |
139 x4->params.rc.i_qp_max = avctx->qmax; | |
140 x4->params.rc.i_qp_step = avctx->max_qdiff; | |
141 | |
142 x4->params.i_width = avctx->width; | |
143 x4->params.i_height = avctx->height; | |
144 x4->params.vui.i_sar_width = avctx->sample_aspect_ratio.num; | |
145 x4->params.vui.i_sar_height = avctx->sample_aspect_ratio.den; | |
146 x4->params.i_fps_num = avctx->frame_rate; | |
147 x4->params.i_fps_den = avctx->frame_rate_base; | |
148 | |
149 x4->enc = x264_encoder_open(&x4->params); | |
150 if(!x4->enc) | |
151 return -1; | |
152 | |
153 avctx->coded_frame = &x4->out_pic; | |
154 | |
155 return 0; | |
156 } | |
157 | |
158 AVCodec x264_encoder = { | |
159 .name = "h264", | |
160 .type = CODEC_TYPE_VIDEO, | |
161 .id = CODEC_ID_H264, | |
162 .priv_data_size = sizeof(X264Context), | |
163 .init = X264_init, | |
164 .encode = X264_frame, | |
165 .close = X264_close, | |
166 .pix_fmts = (enum PixelFormat[]) { PIX_FMT_YUV420P, -1 } | |
167 }; |