comparison libmpcodecs/ve_lavc.c @ 5550:7d1dfb59c6c0

encoders
author arpi
date Wed, 10 Apr 2002 23:23:36 +0000
parents
children 73978162b6a1
comparison
equal deleted inserted replaced
5549:7bed3fb6a3be 5550:7d1dfb59c6c0
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "../config.h"
6
7 #ifdef USE_LIBAVCODEC
8
9 #include "../mp_msg.h"
10 #include "../help_mp.h"
11
12 #include "codec-cfg.h"
13 #include "stream.h"
14 #include "demuxer.h"
15 #include "stheader.h"
16
17 #include "aviwrite.h"
18
19 #include "../libvo/img_format.h"
20 #include "../mp_image.h"
21 #include "vf.h"
22
23 #include "divx4_vbr.h"
24
25 extern int pass;
26 extern char* passtmpfile;
27
28 //===========================================================================//
29
30 #ifdef USE_LIBAVCODEC_SO
31 #include <libffmpeg/avcodec.h>
32 #else
33 #include "libavcodec/avcodec.h"
34 #endif
35
36 extern int avcodec_inited;
37
38 /* video options */
39 static char *lavc_param_vcodec = NULL;
40 static int lavc_param_vbitrate = -1;
41 static int lavc_param_vrate_tolerance = 1024*8;
42 static int lavc_param_vhq = 0; /* default is realtime encoding */
43 static int lavc_param_v4mv = 0;
44 static int lavc_param_vme = 3;
45 static int lavc_param_vqscale = 0;
46 static int lavc_param_vqmin = 3;
47 static int lavc_param_vqmax = 15;
48 static int lavc_param_vqdiff = 3;
49 static float lavc_param_vqcompress = 0.5;
50 static float lavc_param_vqblur = 0.5;
51 static int lavc_param_keyint = -1;
52
53 #include "cfgparser.h"
54
55 #ifdef USE_LIBAVCODEC
56 struct config lavcopts_conf[]={
57 {"vcodec", &lavc_param_vcodec, CONF_TYPE_STRING, 0, 0, 0, NULL},
58 {"vbitrate", &lavc_param_vbitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
59 {"vratetol", &lavc_param_vrate_tolerance, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
60 {"vhq", &lavc_param_vhq, CONF_TYPE_FLAG, 0, 0, 1, NULL},
61 {"v4mv", &lavc_param_v4mv, CONF_TYPE_FLAG, 0, 0, 1, NULL},
62 {"vme", &lavc_param_vme, CONF_TYPE_INT, CONF_RANGE, 0, 5, NULL},
63 {"vqscale", &lavc_param_vqscale, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
64 {"vqmin", &lavc_param_vqmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
65 {"vqmax", &lavc_param_vqmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
66 {"vqdiff", &lavc_param_vqdiff, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
67 {"vqcomp", &lavc_param_vqcompress, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
68 {"vqblur", &lavc_param_vqblur, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
69 {"keyint", &lavc_param_keyint, CONF_TYPE_INT, 0, 0, 0, NULL},
70 {NULL, NULL, 0, 0, 0, 0, NULL}
71 };
72 #endif
73
74 struct vf_priv_s {
75 aviwrite_stream_t* mux;
76 AVCodecContext context;
77 AVCodec *codec;
78 };
79
80 #define mux_v (vf->priv->mux)
81 #define lavc_venc_context (vf->priv->context)
82
83 static int config(struct vf_instance_s* vf,
84 int width, int height, int d_width, int d_height,
85 unsigned int flags, unsigned int outfmt){
86
87 mux_v->bih->biWidth=width;
88 mux_v->bih->biHeight=height;
89 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
90
91 memset(&lavc_venc_context, 0, sizeof(lavc_venc_context));
92
93 printf("videocodec: libavcodec (%dx%d fourcc=%x [%.4s])\n",
94 mux_v->bih->biWidth, mux_v->bih->biHeight, mux_v->bih->biCompression,
95 (char *)&mux_v->bih->biCompression);
96
97 lavc_venc_context.width = width;
98 lavc_venc_context.height = height;
99 if (lavc_param_vbitrate >= 0) /* != -1 */
100 lavc_venc_context.bit_rate = lavc_param_vbitrate*1000;
101 else
102 lavc_venc_context.bit_rate = 800000; /* default */
103 lavc_venc_context.bit_rate_tolerance= lavc_param_vrate_tolerance*1000;
104 lavc_venc_context.frame_rate = (float)mux_v->h.dwRate/mux_v->h.dwScale * FRAME_RATE_BASE;
105 lavc_venc_context.qmin= lavc_param_vqmin;
106 lavc_venc_context.qmax= lavc_param_vqmax;
107 lavc_venc_context.max_qdiff= lavc_param_vqdiff;
108 lavc_venc_context.qcompress= lavc_param_vqcompress;
109 lavc_venc_context.qblur= lavc_param_vqblur;
110 /* keyframe interval */
111 if (lavc_param_keyint >= 0) /* != -1 */
112 lavc_venc_context.gop_size = lavc_param_keyint;
113 else
114 lavc_venc_context.gop_size = 250; /* default */
115
116 if (lavc_param_vhq)
117 {
118 printf("High quality encoding selected (non real time)!\n");
119 lavc_venc_context.flags = CODEC_FLAG_HQ;
120 }
121 else
122 lavc_venc_context.flags = 0;
123
124 lavc_venc_context.flags|= lavc_param_v4mv ? CODEC_FLAG_4MV : 0;
125
126 /* motion estimation (0 = none ... 3 = high quality but slow) */
127 /* this is just an extern from libavcodec but it should be in the
128 encoder context - FIXME */
129 motion_estimation_method = lavc_param_vme;
130
131 /* fixed qscale :p */
132 if (lavc_param_vqscale)
133 {
134 printf("Using constant qscale = %d (VBR)\n", lavc_param_vqscale);
135 lavc_venc_context.flags |= CODEC_FLAG_QSCALE;
136 lavc_venc_context.quality = lavc_param_vqscale;
137 }
138
139 switch(pass){
140 case 1:
141 if (VbrControl_init_2pass_vbr_analysis(passtmpfile, 5) == -1){
142 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile);
143 pass=0;
144 }
145 break;
146 case 2:
147 if (VbrControl_init_2pass_vbr_encoding(passtmpfile,
148 lavc_venc_context.bit_rate,
149 (float)mux_v->h.dwRate/mux_v->h.dwScale,
150 100, /* crispness */
151 5) == -1){
152 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile);
153 pass=0;
154 }
155 break;
156 }
157
158 if (avcodec_open(&lavc_venc_context, vf->priv->codec) != 0) {
159 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec);
160 return 0;
161 }
162
163 if (lavc_venc_context.codec->encode == NULL) {
164 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n");
165 return 0;
166 }
167
168 return 1;
169 }
170
171 static int control(struct vf_instance_s* vf, int request, void* data){
172
173 return CONTROL_UNKNOWN;
174 }
175
176 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
177 switch(fmt){
178 case IMGFMT_YV12:
179 case IMGFMT_IYUV:
180 case IMGFMT_I420:
181 return 1;
182 }
183 return 0;
184 }
185
186 static void put_image(struct vf_instance_s* vf, mp_image_t *mpi){
187 int out_size;
188 AVPicture lavc_venc_picture;
189
190 lavc_venc_picture.data[0]=mpi->planes[0];
191 lavc_venc_picture.data[1]=mpi->planes[1];
192 lavc_venc_picture.data[2]=mpi->planes[2];
193 lavc_venc_picture.linesize[0]=mpi->stride[0];
194 lavc_venc_picture.linesize[1]=mpi->stride[1];
195 lavc_venc_picture.linesize[2]=mpi->stride[2];
196
197 if(pass==2){ // handle 2-pass:
198 lavc_venc_context.flags|=CODEC_FLAG_QSCALE; // enable VBR
199 lavc_venc_context.quality=VbrControl_get_quant();
200 #ifdef CODEC_FLAG_TYPE
201 lavc_venc_context.flags|=CODEC_FLAG_TYPE; // force keyframes
202 lavc_venc_context.key_frame=VbrControl_get_intra();
203 lavc_venc_context.gop_size=0x3fffffff;
204 #else
205 #error you should upgrade libavcodec... get latest CVS
206 #endif
207 out_size = avcodec_encode_video(&lavc_venc_context, mux_v->buffer, mux_v->buffer_size,
208 &lavc_venc_picture);
209 VbrControl_update_2pass_vbr_encoding(lavc_venc_context.mv_bits,
210 lavc_venc_context.i_tex_bits+lavc_venc_context.p_tex_bits,
211 8*out_size);
212 } else {
213 out_size = avcodec_encode_video(&lavc_venc_context, mux_v->buffer, mux_v->buffer_size,
214 &lavc_venc_picture);
215
216 if(pass==1){
217 VbrControl_update_2pass_vbr_analysis(lavc_venc_context.key_frame,
218 lavc_venc_context.mv_bits,
219 lavc_venc_context.i_tex_bits+lavc_venc_context.p_tex_bits,
220 8*out_size, lavc_venc_context.quality);
221 }
222
223 }
224
225 mencoder_write_chunk(mux_v,out_size,lavc_venc_context.key_frame?0x10:0);
226 }
227
228 //===========================================================================//
229
230 static int vf_open(vf_instance_t *vf, char* args){
231 vf->config=config;
232 vf->control=control;
233 vf->query_format=query_format;
234 vf->put_image=put_image;
235 vf->priv=malloc(sizeof(struct vf_priv_s));
236 memset(vf->priv,0,sizeof(struct vf_priv_s));
237 vf->priv->mux=args;
238
239 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER));
240 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
241 mux_v->bih->biWidth=0;
242 mux_v->bih->biHeight=0;
243 mux_v->bih->biPlanes=1;
244 mux_v->bih->biBitCount=24;
245 if (!lavc_param_vcodec)
246 {
247 printf("No libavcodec codec specified! It's requested!\n");
248 return 0;
249 }
250
251 if (!strcasecmp(lavc_param_vcodec, "mpeg1") || !strcasecmp(lavc_param_vcodec, "mpeg1video"))
252 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '1');
253 else if (!strcasecmp(lavc_param_vcodec, "h263") || !strcasecmp(lavc_param_vcodec, "h263p"))
254 mux_v->bih->biCompression = mmioFOURCC('h', '2', '6', '3');
255 else if (!strcasecmp(lavc_param_vcodec, "rv10"))
256 mux_v->bih->biCompression = mmioFOURCC('R', 'V', '1', '0');
257 else if (!strcasecmp(lavc_param_vcodec, "mjpeg"))
258 mux_v->bih->biCompression = mmioFOURCC('M', 'J', 'P', 'G');
259 else if (!strcasecmp(lavc_param_vcodec, "mpeg4"))
260 mux_v->bih->biCompression = mmioFOURCC('D', 'I', 'V', 'X');
261 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4"))
262 mux_v->bih->biCompression = mmioFOURCC('d', 'i', 'v', '3');
263 else
264 mux_v->bih->biCompression = mmioFOURCC(lavc_param_vcodec[0],
265 lavc_param_vcodec[1], lavc_param_vcodec[2], lavc_param_vcodec[3]); /* FIXME!!! */
266
267 if (!avcodec_inited){
268 avcodec_init();
269 avcodec_register_all();
270 avcodec_inited=1;
271 }
272
273 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name(lavc_param_vcodec);
274 if (!vf->priv->codec) {
275 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_MissingLAVCcodec, lavc_param_vcodec);
276 return 0;
277 }
278
279 return 1;
280 }
281
282 vf_info_t ve_info_lavc = {
283 "libavcodec encoder",
284 "lavc",
285 "A'rpi",
286 "for internal use by mencoder",
287 vf_open
288 };
289
290 //===========================================================================//
291 #endif