Mercurial > mplayer.hg
annotate libmpcodecs/ve_xvid.c @ 8510:43a716922d41
1l (%d->%f), cosmetics
author | arpi |
---|---|
date | Sat, 21 Dec 2002 20:39:40 +0000 |
parents | ce25d80dd6c3 |
children | 36da960c94e3 |
rev | line source |
---|---|
7456 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
8078 | 4 #include <errno.h> |
7456 | 5 |
6 #include "../config.h" | |
7 #include "../mp_msg.h" | |
8 | |
9 #ifdef HAVE_XVID | |
10 | |
11 #include "codec-cfg.h" | |
12 #include "stream.h" | |
13 #include "demuxer.h" | |
14 #include "stheader.h" | |
15 | |
16 #include "aviwrite.h" | |
17 | |
18 #include "img_format.h" | |
19 #include "mp_image.h" | |
20 #include "vf.h" | |
21 | |
22 #include <xvid.h> | |
23 #include "xvid_vbr.h" | |
24 | |
8491
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
25 #include "cfgparser.h" |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
26 |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
27 |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
28 #ifdef XVID_API_UNSTABLE |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
29 #warning ******************************************************************* |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
30 #warning ** ** |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
31 #warning ** Y O U '' R E U S I N G U N S T A B L E S O F T W A R E ** |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
32 #warning ** ** |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
33 #warning ** Streams produced by this version aren''nt probably compatible ** |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
34 #warning ** with anything else, even the xvid decoder itself. There are ** |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
35 #warning ** bugs, this code could crash, could blow up your PC or the ** |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
36 #warning ** whole building ! ** |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
37 #warning ** If you want stable code and compatible streams, use stable ** |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
38 #warning ** XViD releases (currently 0.9.x). ** |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
39 #warning ** ** |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
40 #warning ******************************************************************* |
8462
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
41 #endif |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
42 |
7456 | 43 /**********************************************************************/ |
8462
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
44 /* motion estimation quality presets */ |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
45 static int const motion_presets[7] = { |
8491
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
46 #ifdef XVID_API_UNSTABLE |
8462
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
47 0, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
48 PMV_QUICKSTOP16, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
49 0, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
50 0, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
51 PMV_HALFPELREFINE16 | PMV_HALFPELDIAMOND8, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
52 PMV_HALFPELREFINE16 | PMV_HALFPELDIAMOND8 | PMV_ADVANCEDDIAMOND16, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
53 PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8 | PMV_USESQUARES16 |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
54 #else |
8078 | 55 0, |
56 PMV_QUICKSTOP16, | |
7456 | 57 PMV_EARLYSTOP16, |
8078 | 58 PMV_EARLYSTOP16 | PMV_EARLYSTOP8, |
59 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8, | |
60 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8 | PMV_ADVANCEDDIAMOND16, | |
61 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 | | |
62 PMV_HALFPELDIAMOND8 | PMV_USESQUARES16 | |
63 | |
8462
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
64 #endif |
7456 | 65 }; |
66 | |
8078 | 67 extern char* passtmpfile; |
8123
9fc45fe0d444
*HUGE* set of compiler warning fixes, unused variables removal
arpi
parents:
8078
diff
changeset
|
68 extern void mencoder_write_chunk(aviwrite_stream_t *s,int len,unsigned int flags); |
7456 | 69 |
8078 | 70 static int xvidenc_pass = 0; |
8462
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
71 static int xvidenc_quality = 4; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
72 static int xvidenc_4mv = 0; |
8078 | 73 static int xvidenc_bitrate = -1; |
74 static int xvidenc_rc_reaction_delay_factor = -1; | |
75 static int xvidenc_rc_averaging_period = -1; | |
76 static int xvidenc_rc_buffer = -1; | |
8247
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
77 static char* xvidenc_quant_range = "2-31/2-31"; |
8221 | 78 static int xvidenc_min_key_interval = -1; |
8078 | 79 static int xvidenc_max_key_interval = -1; |
80 static int xvidenc_mpeg_quant = 0; | |
8192 | 81 static int xvidenc_mod_quant = 0; |
8078 | 82 static int xvidenc_lumi_mask = 0; |
8221 | 83 static int xvidenc_keyframe_boost = -1; |
8192 | 84 static int xvidenc_kfthreshold = -1; |
85 static int xvidenc_kfreduction = -1; | |
8078 | 86 static int xvidenc_fixed_quant = 0; |
87 static int xvidenc_debug = 0; | |
88 static int xvidenc_hintedme = 0; | |
89 static char* xvidenc_hintfile = "xvid_hint_me.dat"; | |
8491
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
90 #ifdef XVID_API_UNSTABLE |
8462
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
91 static int xvidenc_qpel = 0; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
92 static int xvidenc_max_bframes = 0; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
93 static int xvidenc_bquant_ratio = 125; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
94 static int xvidenc_bquant_offset = 60; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
95 static int xvidenc_gmc = 0; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
96 static int xvidenc_me_colour = 0; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
97 static int xvidenc_reduced = 0; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
98 #endif |
7458 | 99 |
7456 | 100 struct config xvidencopts_conf[] = { |
8078 | 101 { "pass", &xvidenc_pass, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL}, |
8462
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
102 { "me_quality", &xvidenc_quality, CONF_TYPE_INT, CONF_RANGE, 0, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
103 sizeof(motion_presets) / sizeof(motion_presets[0]) - 1, NULL}, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
104 { "4mv", &xvidenc_4mv, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
105 { "bitrate", &xvidenc_bitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, |
8078 | 106 { "rc_reaction_delay_factor", &xvidenc_rc_reaction_delay_factor, CONF_TYPE_INT, 0, 0, 0, NULL}, |
107 { "rc_averaging_period", &xvidenc_rc_averaging_period, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
108 { "rc_buffer", &xvidenc_rc_buffer, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
8247
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
109 { "quant_range", &xvidenc_quant_range, CONF_TYPE_STRING, 0, 0, 0, NULL}, |
8078 | 110 { "min_key_interval", &xvidenc_min_key_interval, CONF_TYPE_INT, 0, 0, 0, NULL}, /* for XVID_MODE_2PASS_2 */ |
111 { "max_key_interval", &xvidenc_max_key_interval, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
112 { "mpeg_quant", &xvidenc_mpeg_quant, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
8192 | 113 { "mod_quant", &xvidenc_mod_quant, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
8078 | 114 { "lumi_mask", &xvidenc_lumi_mask, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
8193 | 115 { "keyframe_boost", &xvidenc_keyframe_boost, CONF_TYPE_INT, CONF_RANGE, 0, 1000, NULL}, /* for XVID_MODE_2PASS_2 */ |
8078 | 116 { "kfthreshold", &xvidenc_kfthreshold, CONF_TYPE_INT, 0, 0, 0, NULL}, /* for XVID_MODE_2PASS_2 */ |
8192 | 117 { "kfreduction", &xvidenc_kfreduction, CONF_TYPE_INT, CONF_RANGE, 0, 100, NULL}, /* for XVID_MODE_2PASS_2 */ |
8078 | 118 { "fixed_quant", &xvidenc_fixed_quant, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, /* for XVID_MODE_FIXED_QUANT */ |
119 { "debug", &xvidenc_debug, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
120 { "hintedme", &xvidenc_hintedme, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
121 { "hintfile", &xvidenc_hintfile, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
8491
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
122 #ifdef XVID_API_UNSTABLE |
8462
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
123 { "qpel", &xvidenc_qpel, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
124 { "max_bframes", &xvidenc_max_bframes, CONF_TYPE_INT, CONF_RANGE, 0, 4, NULL}, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
125 { "bquant_ratio", &xvidenc_bquant_ratio, CONF_TYPE_INT, CONF_RANGE, 0, 1000, NULL}, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
126 { "bquant_offset", &xvidenc_bquant_offset, CONF_TYPE_INT, CONF_RANGE, -1000, 1000, NULL}, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
127 { "reduced", &xvidenc_reduced, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
128 { "gmc", &xvidenc_gmc, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
129 { "me_colour", &xvidenc_me_colour, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
130 #endif |
7456 | 131 { NULL, NULL, 0, 0, 0, 0, NULL} |
132 }; | |
133 | |
134 struct vf_priv_s { | |
135 aviwrite_stream_t* mux; | |
136 XVID_ENC_FRAME enc_frame; | |
137 void* enc_handle; | |
138 vbr_control_t vbr_state; | |
139 FILE *hintfile; | |
8078 | 140 void *hintstream; |
7456 | 141 }; |
142 | |
143 static int | |
144 config(struct vf_instance_s* vf, | |
145 int width, int height, int d_width, int d_height, | |
146 unsigned int flags, unsigned int outfmt) | |
147 { | |
148 XVID_ENC_PARAM enc_param; | |
8078 | 149 struct vf_priv_s *fp = vf->priv; |
8247
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
150 unsigned int min_iq, max_iq, min_pq, max_pq; |
7456 | 151 |
8078 | 152 fp->mux->bih->biWidth = width; |
153 fp->mux->bih->biHeight = height; | |
154 fp->mux->bih->biSizeImage = fp->mux->bih->biWidth * fp->mux->bih->biHeight * 3; | |
155 mp_msg(MSGT_MENCODER,MSGL_INFO,"videocodec: XViD (%dx%d fourcc=%x [%.4s])\n", | |
156 width, height, fp->mux->bih->biCompression, (char *)&fp->mux->bih->biCompression); | |
7456 | 157 |
8247
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
158 // {min,max}_{i,p}quantizer parsing & validation |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
159 if (sscanf (xvidenc_quant_range, "%u-%u/%u-%u", &min_iq, &max_iq, &min_pq, &max_pq) < 4) { |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
160 mp_msg (MSGT_MENCODER, MSGL_ERR, |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
161 "xvid: ERROR: cannot parse \"quant_range=%s\"\n", xvidenc_quant_range); |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
162 return 0; |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
163 } |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
164 if (min_iq < 1 || min_iq > 31 || max_iq < 1 || max_iq > 31 || min_iq > max_iq || |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
165 min_pq < 1 || min_pq > 31 || max_pq < 1 || max_pq > 31 || min_pq > max_pq) { |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
166 mp_msg (MSGT_MENCODER, MSGL_ERR, |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
167 "xvid: ERROR: {min,max} {I,P} quantizer must be in [1,31] and min must be <= max.\n"); |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
168 mp_msg (MSGT_MENCODER, MSGL_ERR, |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
169 "xvid: ERROR: cannot use \"quant_range=%s\"\n", xvidenc_quant_range); |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
170 return -1; |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
171 } |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
172 |
8491
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
173 #ifdef XVID_API_UNSTABLE |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
174 mp_msg (MSGT_MENCODER, MSGL_WARN, |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
175 "\n" |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
176 "*******************************************************************\n" |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
177 "** **\n" |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
178 "** Y O U ' R E U S I N G U N S T A B L E S O F T W A R E **\n" |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
179 "** **\n" |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
180 "** Streams produced by this version aren'nt probably compatible **\n" |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
181 "** with anything else, even the xvid decoder itself. There are **\n" |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
182 "** bugs, this code could crash, could blow up your PC or the **\n" |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
183 "** whole building ! **\n" |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
184 "** If you want stable code and compatible streams, use stable **\n" |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
185 "** XViD releases (currently 0.9.x). **\n" |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
186 "** **\n" |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
187 "*******************************************************************\n" |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
188 "\n"); |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
189 #endif |
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
190 |
8078 | 191 // initialize XViD core parameters |
192 // =============================== | |
7456 | 193 memset(&enc_param, 0, sizeof(enc_param)); |
194 enc_param.width = width; | |
195 enc_param.height = height; | |
8078 | 196 enc_param.fincr = fp->mux->h.dwScale; |
197 enc_param.fbase = fp->mux->h.dwRate; | |
198 if (xvidenc_bitrate > 16000) | |
199 enc_param.rc_bitrate = xvidenc_bitrate; | |
200 else if (xvidenc_bitrate > 0) | |
201 enc_param.rc_bitrate = xvidenc_bitrate * 1000; | |
202 else | |
203 enc_param.rc_bitrate = -1; | |
8491
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
204 #ifdef XVID_API_UNSTABLE |
8462
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
205 if (xvidenc_max_bframes >= 1 && xvidenc_pass >= 1) { |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
206 mp_msg(MSGT_MENCODER,MSGL_WARN, "xvid: cannot use bframes with 2-pass, disabling bframes\n"); |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
207 xvidenc_max_bframes = 0; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
208 } |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
209 enc_param.max_bframes = xvidenc_max_bframes; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
210 enc_param.bquant_ratio = xvidenc_bquant_ratio; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
211 enc_param.bquant_offset = xvidenc_bquant_offset; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
212 if (xvidenc_reduced) |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
213 enc_param.global |= XVID_GLOBAL_REDUCED; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
214 #endif |
8078 | 215 enc_param.rc_reaction_delay_factor = xvidenc_rc_reaction_delay_factor; |
216 enc_param.rc_averaging_period = xvidenc_rc_averaging_period; | |
217 enc_param.rc_buffer = xvidenc_rc_buffer; | |
8247
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
218 enc_param.min_quantizer = min_iq; |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
219 enc_param.max_quantizer = max_iq; |
8221 | 220 if( xvidenc_max_key_interval <= 0 ) |
221 xvidenc_max_key_interval = 10 * enc_param.fbase / enc_param.fincr; | |
222 enc_param.max_key_interval = xvidenc_max_key_interval; | |
7456 | 223 switch (xvid_encore(NULL, XVID_ENC_CREATE, &enc_param, NULL)) { |
224 case XVID_ERR_FAIL: | |
8078 | 225 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: encoder creation failed\n"); |
7456 | 226 return 0; |
227 case XVID_ERR_MEMORY: | |
8078 | 228 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: encoder creation failed, out of memory\n"); |
7456 | 229 return 0; |
230 case XVID_ERR_FORMAT: | |
8078 | 231 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: encoder creation failed, bad format\n"); |
7456 | 232 return 0; |
233 } | |
8078 | 234 fp->enc_handle = enc_param.handle; |
7456 | 235 |
8078 | 236 // initialize XViD per-frame static parameters |
237 // =========================================== | |
8462
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
238 fp->enc_frame.motion = motion_presets[xvidenc_quality]; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
239 fp->enc_frame.general = XVID_HALFPEL | (xvidenc_mpeg_quant ? XVID_MPEGQUANT : XVID_H263QUANT); |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
240 if (xvidenc_4mv) |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
241 fp->enc_frame.general |= XVID_INTER4V; |
8078 | 242 if (xvidenc_lumi_mask) |
243 fp->enc_frame.general |= XVID_LUMIMASKING; | |
8491
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
244 #ifdef XVID_API_UNSTABLE |
8462
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
245 if (xvidenc_qpel) { |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
246 fp->enc_frame.general |= XVID_QUARTERPEL; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
247 fp->enc_frame.motion |= PMV_QUARTERPELREFINE16 | PMV_QUARTERPELREFINE8; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
248 } |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
249 if (xvidenc_gmc) |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
250 fp->enc_frame.general |= XVID_GMC; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
251 if (xvidenc_me_colour) |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
252 fp->enc_frame.general |= XVID_ME_COLOUR; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
253 if(xvidenc_reduced) |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
254 fp->enc_frame.general |= XVID_REDUCED; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
255 #endif |
8078 | 256 |
7456 | 257 switch (outfmt) { |
258 case IMGFMT_YV12: | |
8078 | 259 fp->enc_frame.colorspace = XVID_CSP_YV12; |
7456 | 260 break; |
261 case IMGFMT_IYUV: case IMGFMT_I420: | |
8078 | 262 fp->enc_frame.colorspace = XVID_CSP_I420; |
7456 | 263 break; |
264 case IMGFMT_YUY2: | |
8078 | 265 fp->enc_frame.colorspace = XVID_CSP_YUY2; |
7456 | 266 break; |
267 case IMGFMT_UYVY: | |
8078 | 268 fp->enc_frame.colorspace = XVID_CSP_UYVY; |
7456 | 269 break; |
270 case IMGFMT_RGB24: case IMGFMT_BGR24: | |
8078 | 271 fp->enc_frame.colorspace = XVID_CSP_RGB24; |
7456 | 272 break; |
273 default: | |
274 mp_msg(MSGT_MENCODER,MSGL_ERR,"xvid: unsupported picture format (%s)!\n", | |
275 vo_format_name(outfmt)); | |
276 return 0; | |
277 } | |
8078 | 278 fp->enc_frame.quant_intra_matrix = 0; |
279 fp->enc_frame.quant_inter_matrix = 0; | |
280 | |
281 // hinted ME | |
282 fp->hintstream = NULL; | |
283 fp->hintfile = NULL; | |
284 if (xvidenc_hintedme && (xvidenc_pass == 1 || xvidenc_pass == 2)) { | |
285 fp->hintstream = malloc( 100000 ); // this is what the vfw code in XViD CVS allocates | |
286 if (fp->hintstream == NULL) | |
287 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: cannot allocate memory for hinted ME\n"); | |
288 else { | |
289 fp->hintfile = fopen(xvidenc_hintfile, xvidenc_pass == 1 ? "w" : "r"); | |
290 if (fp->hintfile == NULL) { | |
291 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: %s: %s\n", strerror(errno), xvidenc_hintfile); | |
292 free(fp->hintstream); | |
293 } | |
294 } | |
295 if (fp->hintstream == NULL || fp->hintfile == NULL) | |
296 xvidenc_hintedme = 0; | |
297 } | |
7456 | 298 |
8078 | 299 // initialize VBR engine |
300 // ===================== | |
301 vbrSetDefaults(&fp->vbr_state); | |
8221 | 302 if (xvidenc_min_key_interval < 0) |
303 xvidenc_min_key_interval = fp->vbr_state.min_key_interval; | |
304 | |
305 // pass | |
8078 | 306 if (xvidenc_pass == 0) { |
307 if (xvidenc_fixed_quant >= 1) { | |
308 fp->vbr_state.mode = VBR_MODE_FIXED_QUANT; | |
309 fp->vbr_state.fixed_quant = xvidenc_fixed_quant; | |
310 } else | |
311 fp->vbr_state.mode = VBR_MODE_1PASS; | |
7456 | 312 } |
8078 | 313 else if (xvidenc_pass == 1) |
314 fp->vbr_state.mode = VBR_MODE_2PASS_1; | |
315 else if (xvidenc_pass == 2) | |
316 fp->vbr_state.mode = VBR_MODE_2PASS_2; | |
317 else | |
318 return -1; | |
8221 | 319 |
320 // misc | |
8078 | 321 fp->vbr_state.fps = (double)enc_param.fbase / enc_param.fincr; |
322 fp->vbr_state.filename = passtmpfile; | |
323 fp->vbr_state.desired_bitrate = enc_param.rc_bitrate; | |
8247
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
324 fp->vbr_state.min_iquant = min_iq; |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
325 fp->vbr_state.max_iquant = max_iq; |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
326 fp->vbr_state.min_pquant = min_pq; |
91edcb091dc6
Instead of min_quantizer, max_quantizer, min_iquantizer, etc... use a
rguyom
parents:
8221
diff
changeset
|
327 fp->vbr_state.max_pquant = max_pq; |
8221 | 328 if (xvidenc_keyframe_boost >= 0) |
8078 | 329 fp->vbr_state.keyframe_boost = xvidenc_keyframe_boost; |
8192 | 330 if (xvidenc_kfthreshold >= 0) |
8078 | 331 fp->vbr_state.kftreshold = xvidenc_kfthreshold; |
8192 | 332 if (xvidenc_kfreduction >= 0) |
8078 | 333 fp->vbr_state.kfreduction = xvidenc_kfreduction; |
8221 | 334 if (xvidenc_min_key_interval >= 0) |
8078 | 335 fp->vbr_state.min_key_interval = xvidenc_min_key_interval; |
336 fp->vbr_state.max_key_interval = enc_param.max_key_interval; | |
337 fp->vbr_state.debug = xvidenc_debug; | |
8221 | 338 |
8078 | 339 vbrInit(&fp->vbr_state); |
340 | |
7456 | 341 return 1; |
342 } | |
343 | |
344 static void | |
345 uninit(struct vf_instance_s* vf) | |
346 { | |
8078 | 347 struct vf_priv_s *fp = vf->priv; |
348 | |
349 if (fp->hintfile) | |
350 fclose(fp->hintfile); | |
351 if (fp->hintstream) | |
352 free(fp->hintstream); | |
353 vbrFinish(&fp->vbr_state); | |
7456 | 354 } |
355 | |
356 static int | |
357 control(struct vf_instance_s* vf, int request, void* data) | |
358 { | |
359 return CONTROL_UNKNOWN; | |
360 } | |
361 | |
362 static int | |
363 query_format(struct vf_instance_s* vf, unsigned int fmt) | |
364 { | |
365 switch(fmt){ | |
366 case IMGFMT_YV12: case IMGFMT_IYUV: case IMGFMT_I420: | |
367 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW; | |
368 case IMGFMT_YUY2: case IMGFMT_UYVY: | |
369 return VFCAP_CSP_SUPPORTED; | |
370 case IMGFMT_RGB24: case IMGFMT_BGR24: | |
371 return VFCAP_CSP_SUPPORTED | VFCAP_FLIPPED; | |
372 } | |
373 return 0; | |
374 } | |
375 | |
376 static int | |
377 put_image(struct vf_instance_s* vf, mp_image_t *mpi) | |
378 { | |
379 XVID_ENC_STATS enc_stats; | |
8078 | 380 struct vf_priv_s *fp = vf->priv; |
7456 | 381 |
8078 | 382 fp->enc_frame.bitstream = fp->mux->buffer; |
383 fp->enc_frame.length = -1 /* fp->mux->buffer_size */; | |
384 fp->enc_frame.image = mpi->planes[0]; | |
8192 | 385 |
386 // get quantizers & I/P decision from the VBR engine | |
8491
ce25d80dd6c3
Use the XVID_API_UNSTABLE macro instead of a home-made one.
rguyom
parents:
8462
diff
changeset
|
387 #ifdef XVID_API_UNSTABLE |
8462
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
388 if (xvidenc_max_bframes >= 1) { |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
389 if (!xvidenc_fixed_quant) { |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
390 // hack, the internal VBR engine isn't fixed-quant aware |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
391 fp->enc_frame.quant = xvidenc_fixed_quant; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
392 fp->enc_frame.intra = -1; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
393 fp->enc_frame.bquant = (xvidenc_fixed_quant * xvidenc_bquant_ratio + xvidenc_bquant_offset) / 100; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
394 } else |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
395 // use the internal VBR engine since the external one isn't bframe aware |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
396 fp->enc_frame.quant = fp->enc_frame.intra = fp->enc_frame.bquant = -1; |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
397 } else { |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
398 fp->enc_frame.quant = vbrGetQuant(&fp->vbr_state); |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
399 fp->enc_frame.intra = vbrGetIntra(&fp->vbr_state); |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
400 } |
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
401 #else |
8078 | 402 fp->enc_frame.quant = vbrGetQuant(&fp->vbr_state); |
403 fp->enc_frame.intra = vbrGetIntra(&fp->vbr_state); | |
8462
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
404 #endif |
8192 | 405 |
406 // modulated quantizer type | |
407 if (xvidenc_mod_quant && xvidenc_pass == 2) { | |
408 fp->enc_frame.general |= (fp->enc_frame.quant < 4) ? XVID_MPEGQUANT : XVID_H263QUANT; | |
409 fp->enc_frame.general &= (fp->enc_frame.quant < 4) ? ~XVID_H263QUANT : ~XVID_MPEGQUANT; | |
410 } | |
411 | |
412 // hinted ME, 1st part | |
8078 | 413 if (xvidenc_hintedme && xvidenc_pass == 1) { |
414 fp->enc_frame.hint.hintstream = fp->hintstream; | |
415 fp->enc_frame.hint.rawhints = 0; | |
416 fp->enc_frame.general |= XVID_HINTEDME_GET; | |
7456 | 417 } |
8078 | 418 else if (xvidenc_hintedme && xvidenc_pass == 2) { |
419 size_t read; | |
420 int blocksize; | |
421 fp->enc_frame.general &= ~XVID_HINTEDME_SET; | |
422 read = fread(&blocksize, sizeof(blocksize), 1, fp->hintfile); | |
423 if (read == 1) { | |
424 read = fread(fp->hintstream, (size_t)blocksize, 1, fp->hintfile); | |
7456 | 425 if (read == 1) { |
8078 | 426 fp->enc_frame.hint.hintstream = fp->hintstream; |
427 fp->enc_frame.hint.hintlength = 0; | |
428 fp->enc_frame.hint.rawhints = 0; | |
429 fp->enc_frame.general |= XVID_HINTEDME_SET; | |
430 } | |
7456 | 431 else |
8078 | 432 perror("xvid: hint file read block failure"); |
433 } | |
434 else | |
435 perror("xvid: hint file read failure"); | |
7456 | 436 } |
8192 | 437 |
438 // encode frame | |
8078 | 439 switch (xvid_encore(fp->enc_handle, XVID_ENC_ENCODE, &fp->enc_frame, &enc_stats)) { |
7456 | 440 case XVID_ERR_OK: |
441 break; | |
442 case XVID_ERR_MEMORY: | |
443 mp_msg(MSGT_MENCODER, MSGL_ERR, "xvid: out of memory\n"); | |
444 break; | |
445 case XVID_ERR_FORMAT: | |
446 mp_msg(MSGT_MENCODER, MSGL_ERR, "xvid: bad format\n"); | |
447 break; | |
448 default: | |
449 mp_msg(MSGT_MENCODER, MSGL_ERR, "xvid: failure\n"); | |
450 break; | |
451 } | |
8192 | 452 |
453 // write output | |
8462
800d77666843
Support the latest development code from XViD dev-api-3 CVS
rguyom
parents:
8247
diff
changeset
|
454 mencoder_write_chunk(fp->mux, fp->enc_frame.length, fp->enc_frame.intra==1 ? 0x10 : 0); |
8192 | 455 |
456 // update the VBR engine | |
8078 | 457 vbrUpdate(&fp->vbr_state, enc_stats.quant, fp->enc_frame.intra, |
458 enc_stats.hlength, fp->enc_frame.length, enc_stats.kblks, enc_stats.mblks, enc_stats.ublks); | |
8192 | 459 |
460 // hinted ME, 2nd part | |
8078 | 461 if (fp->enc_frame.general & XVID_HINTEDME_GET) { |
462 size_t wrote = fwrite(&fp->enc_frame.hint.hintlength, sizeof(fp->enc_frame.hint.hintlength), 1, fp->hintfile); | |
463 if (wrote == 1) { | |
464 wrote = fwrite(fp->enc_frame.hint.hintstream, fp->enc_frame.hint.hintlength, 1, fp->hintfile); | |
465 if (wrote != 1) | |
466 perror("xvid: hint write block failure"); | |
7456 | 467 } |
8078 | 468 else |
469 perror("xvid: hint write failure"); | |
7456 | 470 } |
471 return 1; | |
472 } | |
473 | |
474 //===========================================================================// | |
475 | |
476 static int | |
477 vf_open(vf_instance_t *vf, char* args) | |
478 { | |
479 XVID_INIT_PARAM params = { 0, 0, 0}; | |
480 vf->config = config; | |
481 vf->control = control; | |
482 vf->uninit = uninit; | |
483 vf->query_format = query_format; | |
484 vf->put_image = put_image; | |
485 vf->priv = malloc(sizeof(struct vf_priv_s)); | |
486 memset(vf->priv, 0, sizeof(struct vf_priv_s)); | |
487 vf->priv->mux = (aviwrite_stream_t*)args; | |
488 | |
489 vf->priv->mux->bih = malloc(sizeof(BITMAPINFOHEADER)); | |
490 vf->priv->mux->bih->biSize = sizeof(BITMAPINFOHEADER); | |
491 vf->priv->mux->bih->biWidth = 0; | |
492 vf->priv->mux->bih->biHeight = 0; | |
493 vf->priv->mux->bih->biPlanes = 1; | |
494 vf->priv->mux->bih->biBitCount = 24; | |
495 vf->priv->mux->bih->biCompression = mmioFOURCC('X','V','I','D'); | |
496 | |
497 if (xvid_init(NULL, 0, ¶ms, NULL) != XVID_ERR_OK) { | |
8078 | 498 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: initialisation failure\n"); |
7456 | 499 abort(); |
500 } | |
501 if (params.api_version != API_VERSION) { | |
8078 | 502 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: XviD library API version mismatch\n" |
7456 | 503 "\texpected %d.%d, got %d.%d, you should recompile MPlayer.\n", |
504 API_VERSION >> 16, API_VERSION & 0xff, | |
505 params.api_version >> 16, params.api_version & 0xff); | |
506 abort(); | |
507 } | |
508 | |
509 return 1; | |
510 } | |
511 | |
512 vf_info_t ve_info_xvid = { | |
513 "XviD encoder", | |
514 "xvid", | |
8078 | 515 "Kim Minh Kaplan & Rémi Guyomarch", |
7456 | 516 "for internal use by mencoder", |
517 vf_open | |
518 }; | |
519 | |
520 //===========================================================================// | |
521 #endif |