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