5550
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3 #include <string.h>
|
|
4
|
|
5 #include "../config.h"
|
|
6 #include "../mp_msg.h"
|
|
7
|
|
8 #ifdef HAVE_DIVX4ENCORE
|
|
9
|
|
10 #include "codec-cfg.h"
|
|
11 #include "stream.h"
|
|
12 #include "demuxer.h"
|
|
13 #include "stheader.h"
|
|
14
|
|
15 #include "aviwrite.h"
|
|
16
|
5607
|
17 #include "img_format.h"
|
|
18 #include "mp_image.h"
|
5550
|
19 #include "vf.h"
|
|
20
|
|
21 //===========================================================================//
|
|
22
|
|
23 #include "divx4_vbr.h"
|
|
24
|
|
25 extern int pass;
|
|
26 extern char* passtmpfile;
|
|
27
|
|
28 #include <encore2.h>
|
|
29
|
|
30 ENC_PARAM divx4_param;
|
|
31 int divx4_crispness;
|
|
32
|
|
33 #include "cfgparser.h"
|
|
34
|
|
35 struct config divx4opts_conf[]={
|
|
36 {"br", &divx4_param.bitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
|
|
37 {"rc_period", &divx4_param.rc_period, CONF_TYPE_INT, 0,0,0, NULL},
|
|
38 {"rc_reaction_period", &divx4_param.rc_reaction_period, CONF_TYPE_INT, 0,0,0, NULL},
|
|
39 {"rc_reaction_ratio", &divx4_param.rc_reaction_ratio, CONF_TYPE_INT, 0,0,0, NULL},
|
|
40 {"min_quant", &divx4_param.min_quantizer, CONF_TYPE_INT, CONF_RANGE,0,32, NULL},
|
|
41 {"max_quant", &divx4_param.max_quantizer, CONF_TYPE_INT, CONF_RANGE,0,32, NULL},
|
|
42 {"key", &divx4_param.max_key_interval, CONF_TYPE_INT, CONF_MIN,0,0, NULL},
|
|
43 {"deinterlace", &divx4_param.deinterlace, CONF_TYPE_FLAG, 0,0,1, NULL},
|
|
44 {"q", &divx4_param.quality, CONF_TYPE_INT, CONF_RANGE, 1, 5, NULL},
|
|
45 {"crispness", &divx4_crispness, CONF_TYPE_INT, CONF_RANGE,0,100, NULL},
|
|
46 {"help", "TODO: divx4opts help!\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL},
|
|
47 {NULL, NULL, 0, 0, 0, 0, NULL}
|
|
48 };
|
|
49
|
|
50 struct vf_priv_s {
|
|
51 aviwrite_stream_t* mux;
|
|
52 ENC_RESULT enc_result;
|
|
53 ENC_FRAME enc_frame;
|
|
54 void* enc_handle;
|
|
55 };
|
|
56
|
|
57 #define mux_v (vf->priv->mux)
|
|
58
|
|
59 static int config(struct vf_instance_s* vf,
|
|
60 int width, int height, int d_width, int d_height,
|
|
61 unsigned int flags, unsigned int outfmt){
|
|
62
|
|
63 mux_v->bih->biWidth=width;
|
|
64 mux_v->bih->biHeight=height;
|
|
65
|
|
66 divx4_param.x_dim=width;
|
|
67 divx4_param.y_dim=height;
|
|
68 divx4_param.framerate=(float)mux_v->h.dwRate/mux_v->h.dwScale;
|
|
69 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*3;
|
|
70
|
|
71 if(!divx4_param.bitrate) divx4_param.bitrate=800000;
|
|
72 else if(divx4_param.bitrate<=16000) divx4_param.bitrate*=1000;
|
|
73 if(!divx4_param.quality) divx4_param.quality=5; // the quality of compression ( 1 - fastest, 5 - best )
|
|
74
|
5551
|
75 // set some usefull defaults:
|
|
76 if(!divx4_param.min_quantizer) divx4_param.min_quantizer=2;
|
|
77 if(!divx4_param.max_quantizer) divx4_param.max_quantizer=31;
|
|
78 if(!divx4_param.rc_period) divx4_param.rc_period=2000;
|
|
79 if(!divx4_param.rc_reaction_period) divx4_param.rc_reaction_period=10;
|
|
80 if(!divx4_param.rc_reaction_ratio) divx4_param.rc_reaction_ratio=20;
|
|
81
|
5550
|
82 divx4_param.handle=NULL;
|
|
83 encore(NULL,ENC_OPT_INIT,&divx4_param,NULL);
|
|
84 vf->priv->enc_handle=divx4_param.handle;
|
|
85 switch(outfmt){
|
|
86 case IMGFMT_YV12: vf->priv->enc_frame.colorspace=ENC_CSP_YV12; break;
|
|
87 case IMGFMT_IYUV:
|
|
88 case IMGFMT_I420: vf->priv->enc_frame.colorspace=ENC_CSP_I420; break;
|
|
89 case IMGFMT_YUY2: vf->priv->enc_frame.colorspace=ENC_CSP_YUY2; break;
|
|
90 case IMGFMT_UYVY: vf->priv->enc_frame.colorspace=ENC_CSP_UYVY; break;
|
|
91 case IMGFMT_RGB24:
|
|
92 case IMGFMT_BGR24:
|
|
93 vf->priv->enc_frame.colorspace=ENC_CSP_RGB24; break;
|
|
94 default:
|
|
95 mp_msg(MSGT_MENCODER,MSGL_ERR,"divx4: unsupported picture format (%s)!\n",
|
|
96 vo_format_name(outfmt));
|
|
97 return 0;
|
|
98 }
|
|
99
|
|
100 switch(pass){
|
|
101 case 1:
|
|
102 if (VbrControl_init_2pass_vbr_analysis(passtmpfile, divx4_param.quality) == -1){
|
|
103 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile);
|
|
104 pass=0;
|
|
105 }
|
|
106 break;
|
|
107 case 2:
|
|
108 if (VbrControl_init_2pass_vbr_encoding(passtmpfile,
|
|
109 divx4_param.bitrate,
|
|
110 divx4_param.framerate,
|
|
111 divx4_crispness,
|
|
112 divx4_param.quality) == -1){
|
|
113 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile);
|
|
114 pass=0;
|
|
115 }
|
|
116 break;
|
|
117 }
|
|
118
|
|
119 return 1;
|
|
120 }
|
|
121
|
|
122 static int control(struct vf_instance_s* vf, int request, void* data){
|
|
123
|
|
124 return CONTROL_UNKNOWN;
|
|
125 }
|
|
126
|
|
127 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
|
|
128 switch(fmt){
|
|
129 case IMGFMT_YV12:
|
|
130 case IMGFMT_IYUV:
|
|
131 case IMGFMT_I420:
|
|
132 return 3; // no conversion
|
|
133 case IMGFMT_YUY2:
|
|
134 case IMGFMT_UYVY:
|
5706
|
135 return 1; // conversion
|
5550
|
136 case IMGFMT_RGB24:
|
|
137 case IMGFMT_BGR24:
|
5706
|
138 return 1 | VFCAP_FLIPPED; // conversion+flipped
|
5550
|
139 }
|
|
140 return 0;
|
|
141 }
|
|
142
|
|
143 static void put_image(struct vf_instance_s* vf, mp_image_t *mpi){
|
|
144 ENC_RESULT enc_result;
|
|
145 vf->priv->enc_frame.image=mpi->planes[0];
|
|
146 vf->priv->enc_frame.bitstream=mux_v->buffer;
|
|
147 vf->priv->enc_frame.length=mux_v->buffer_size;
|
|
148 vf->priv->enc_frame.mvs=NULL;
|
|
149 if(pass==2){ // handle 2-pass:
|
|
150 vf->priv->enc_frame.quant = VbrControl_get_quant();
|
|
151 vf->priv->enc_frame.intra = VbrControl_get_intra();
|
|
152 encore(vf->priv->enc_handle,ENC_OPT_ENCODE_VBR,&vf->priv->enc_frame,&enc_result);
|
|
153 VbrControl_update_2pass_vbr_encoding(enc_result.motion_bits,
|
|
154 enc_result.texture_bits,
|
|
155 enc_result.total_bits);
|
|
156 } else {
|
|
157 vf->priv->enc_frame.quant=0;
|
|
158 vf->priv->enc_frame.intra=0;
|
|
159 encore(vf->priv->enc_handle,ENC_OPT_ENCODE,&vf->priv->enc_frame,&enc_result);
|
|
160 if(pass==1){
|
|
161 VbrControl_update_2pass_vbr_analysis(enc_result.is_key_frame,
|
|
162 enc_result.motion_bits,
|
|
163 enc_result.texture_bits,
|
|
164 enc_result.total_bits,
|
|
165 enc_result.quantizer);
|
|
166 }
|
|
167 }
|
5551
|
168 mencoder_write_chunk(mux_v,vf->priv->enc_frame.length,enc_result.is_key_frame?0x10:0);
|
5550
|
169 }
|
|
170
|
|
171 //===========================================================================//
|
|
172
|
|
173 static int vf_open(vf_instance_t *vf, char* args){
|
|
174 vf->config=config;
|
|
175 vf->control=control;
|
|
176 vf->query_format=query_format;
|
|
177 vf->put_image=put_image;
|
|
178 vf->priv=malloc(sizeof(struct vf_priv_s));
|
|
179 memset(vf->priv,0,sizeof(struct vf_priv_s));
|
|
180 vf->priv->mux=args;
|
|
181
|
|
182 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER));
|
|
183 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
|
|
184 mux_v->bih->biWidth=0;
|
|
185 mux_v->bih->biHeight=0;
|
|
186 mux_v->bih->biPlanes=1;
|
|
187 mux_v->bih->biBitCount=24;
|
|
188 mux_v->bih->biCompression=mmioFOURCC('d','i','v','x');
|
|
189
|
|
190 return 1;
|
|
191 }
|
|
192
|
|
193 vf_info_t ve_info_divx4 = {
|
|
194 "divx4 encoder",
|
|
195 "divx4",
|
|
196 "A'rpi",
|
|
197 "for internal use by mencoder",
|
|
198 vf_open
|
|
199 };
|
|
200
|
|
201 //===========================================================================//
|
|
202 #endif
|