~ubuntu-branches/ubuntu/hardy/avidemux/hardy

« back to all changes in this revision

Viewing changes to avidemux/mpeg2enc/mpeg2enc_if.cc

  • Committer: Bazaar Package Importer
  • Author(s): Matvey Kozhev
  • Date: 2007-12-18 13:53:04 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20071218135304-cdqec2lg2bglyz15
Tags: 1:2.4~preview3-0.0ubuntu1
* Upload to Ubuntu. (LP: #163287, LP: #126572)
* debian/changelog: re-added Ubuntu releases.
* debian/control:
  - Require debhelper >= 5.0.51 (for dh_icons) and imagemagick.
  - Build-depend on libsdl1.2-dev instead of libsdl-dev.
  - Build against newer libx264-dev. (LP: #138854)
  - Removed libamrnb-dev, not in Ubuntu yet.
* debian/rules:
  - Install all icon sizes, using convert (upstream installs none).
  - Added missing calls to dh_installmenu, dh_installman, dh_icons and
    dh_desktop.
* debian/menu, debian/avidemux-qt.menu:
  - Corrected package and executable names.
* debian/avidemux-common.install: Install icons.
* debian/avidemux.common.manpages: Install man/avidemux.1.
* debian/links, debian/avidemux-cli.links, debian/avidemux-gtk.links:
  - Link manpages to avidemux.1.gz.
* debian/install, debian/avidemux-qt.install, debian/avidemux-gtk.desktop,
  debian/avidemux-qt.desktop: Install desktop files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
        Strongly derivated from mpeg2enc, mean
3
 
         Modifications and enhancements (C) 2000/2001 Andrew Stevens
4
 
 
5
 
 
6
 
 * These modifications are free software; you can redistribute it
7
 
 *  and/or modify it under the terms of the GNU General Public License
8
 
 *  as published by the Free Software Foundation; either version 2 of
9
 
 *  the License, or (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 *  General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19
 
 * 02111-1307, USA.
20
 
 *
21
 
 */
22
 
 
23
 
#include <config.h>
24
 
#include <stdio.h>
25
 
#include <stdlib.h>
26
 
#include <errno.h>
27
 
#include <string.h>
28
 
#ifdef HAVE_GETOPT_H
29
 
#include <getopt.h>
30
 
#endif
31
 
 
32
 
 
33
 
#define GLOBAL /* used by global.h */
34
 
#include "global.h"
35
 
#include "motionsearch.h"
36
 
#include "predict_ref.h"
37
 
#include "transfrm_ref.h"
38
 
#include "quantize_ref.h"
39
 
#include "format_codes.h"
40
 
#include "mpegconsts.h"
41
 
#include "fastintfns.h"
42
 
#if defined( HAVE_ALTIVEC) && defined(USE_ALTIVEC)
43
 
/* needed for ALTIVEC_BENCHMARK and print_benchmark_statistics() */
44
 
//#include "../utils/altivec/altivec_conf.h"
45
 
#endif
46
 
#include "mpeg2parm.h"
47
 
#include "ADM_codecs/ADM_codec.h"
48
 
//int verbose = 1;
49
 
#define aprintf printf
50
 
/* private prototypes */
51
 
extern void set_format_presets(mpeg2parm *param,Mpeg2Settings *opt);
52
 
extern int  infer_default_params(mpeg2parm *param,Mpeg2Settings *opt);
53
 
extern int  check_param_constraints(mpeg2parm *param);
54
 
extern void init_mpeg_parms(mpeg2parm *param,Mpeg2Settings *opt);
55
 
extern int  infer_mpeg1_aspect_code(  mpeg2parm *param );
56
 
extern void init_encoder(void);
57
 
extern void init_quantmat(mpeg2parm *param,Mpeg2Settings *opt);
58
 
extern void border_mark( uint8_t *frame,
59
 
                         int w1, int h1, int w2, int h2);
60
 
extern void init_encoder(mpeg2parm *param,Mpeg2Settings *opt);
61
 
extern void DisplayFrameRates(void);
62
 
extern void DisplayAspectRatios(void);
63
 
extern int  parse_custom_matrixfile(char *fname, int dbug);
64
 
extern void Usage(void);
65
 
extern void parse_custom_option(char *arg,mpeg2parm *param);
66
 
 
67
 
extern int push_init( void );
68
 
extern int push_cleanup( void );
69
 
extern void mpeg_freebuffers(void);
70
 
 
71
 
mpeg2parm  myParam,*param;
72
 
t_control  myControl;
73
 
 int rateCtlDisablePadding;
74
 
#define MAX(a,b) ( (a)>(b) ? (a) : (b) )
75
 
#define MIN(a,b) ( (a)<(b) ? (a) : (b) )
76
 
 
77
 
 
78
 
#ifndef O_BINARY
79
 
# define O_BINARY 0
80
 
#endif
81
 
static int  mpeg_cleanup( void );
82
 
 
83
 
/* Command Parameter values.  These are checked and processed for
84
 
   defaults etc after parsing.  The resulting values then set the opt->
85
 
   variables that control the actual encoder.
86
 
*/
87
 
 
88
 
 
89
 
/* Input Stream parameter values that have to be further processed to
90
 
   set encoding options */
91
 
 
92
 
 
93
 
static unsigned int strm_frame_rate_code;
94
 
 
95
 
 
96
 
 
97
 
uint16_t custom_intra_quantizer_matrix[64];
98
 
uint16_t custom_nonintra_quantizer_matrix[64];
99
 
int nerr = 0;
100
 
 
101
 
/* MeanX stuff */
102
 
void feedOneFrame(char *y, char *u,char *v);
103
 
 
104
 
static  int fedPictures=0;
105
 
/* MeanX stuff */
106
 
 unsigned char *mpeg2enc_buffer=NULL;
107
 
int mpegenc_init(mpeg2parm *incoming,int  width, int  height, int  fps1000)
108
 
{
109
 
/*
110
 
        Reset a whole bunch of vars
111
 
*/
112
 
istrm_nframes=lum_buffer_size=chrom_buffer_size=block_count=mb_width=mb_height=mb_height2=0;
113
 
qsubsample_offset=fsubsample_offset=0;
114
 
mb_per_pict=0;
115
 
 
116
 
/*-----------------------------------------*/
117
 
 
118
 
        if(incoming->noPadding) 
119
 
        {
120
 
                rateCtlDisablePadding=1;
121
 
                printf("Padding disabled\n");
122
 
        }
123
 
                else rateCtlDisablePadding=0;
124
 
        fedPictures=0;
125
 
        memset(&myControl,0,sizeof(myControl));
126
 
        ctl=&myControl;
127
 
        // 1 we memset the opt
128
 
        memset(opt,0,sizeof(*opt));
129
 
        param=&myParam;
130
 
        memcpy(param,incoming, sizeof(*param));
131
 
#define MPG_FILM        1
132
 
#define MPG_PAL         3
133
 
#define MPG_NTSC        4
134
 
 
135
 
        if(fps1000 > 25500 || fps1000 < 24000)
136
 
        {
137
 
 
138
 
                                        param->norm='n';
139
 
                                        if(fps1000<25000)
140
 
                                        {
141
 
                                                param->frame_rate=MPG_FILM;
142
 
                                                printf("Detecting FILM format\n");
143
 
                                                if(param->format!=1) // not VCD -> SVCD or DVD
144
 
                                                {
145
 
                                                        param->_32_pulldown=1;
146
 
                                                        printf("****Activating pulldown\n");
147
 
                                                }
148
 
                                        }
149
 
                                        else
150
 
                                        {
151
 
 
152
 
                                                param->frame_rate=MPG_NTSC;
153
 
 
154
 
                                                printf("Detecting NTSC format\n");
155
 
                                        }
156
 
        }
157
 
        else
158
 
        {
159
 
                                        param->norm='p';
160
 
                                        param->frame_rate=MPG_PAL;
161
 
                                        printf("Detecting PAL format\n");
162
 
 
163
 
        }
164
 
 
165
 
 
166
 
        opt->horizontal_size=width;
167
 
        opt->vertical_size=height;
168
 
//      param->fieldenc=0; // progressive
169
 
 
170
 
        strm_frame_rate_code=2;
171
 
        opt->frame_rate_code=1;
172
 
 
173
 
 
174
 
 
175
 
        set_format_presets(param,opt);
176
 
 
177
 
        infer_default_params(param,opt);
178
 
 
179
 
        check_param_constraints(param);
180
 
 
181
 
 
182
 
 
183
 
        aprintf("[mpeg2enc]Encoding MPEG-%d video \n",param->mpeg);
184
 
        aprintf("[mpeg2enc]Horizontal size: %d pe \nl",opt->horizontal_size);
185
 
        aprintf("[mpeg2enc]Vertical size: %d pel \n",opt->vertical_size);
186
 
        aprintf("[mpeg2enc]Aspect ratio code: %d = %s \n",
187
 
                        param->aspect_ratio,
188
 
                        mpeg_aspect_code_definition(param->mpeg,param->aspect_ratio));
189
 
        aprintf("[mpeg2enc]Frame rate code:   %d = %s \n",
190
 
                        param->frame_rate,
191
 
                        mpeg_framerate_code_definition(param->frame_rate));
192
 
 
193
 
        if(param->bitrate)
194
 
          aprintf("[mpeg2enc]Bitrate: %d KBit/s \n",param->bitrate/1000);
195
 
        else
196
 
          aprintf( "[mpeg2enc]Bitrate: VCD \n");
197
 
        if(param->quant)
198
 
          aprintf("[mpeg2enc]Quality factor: %d (Quantisation = %d) (1=best, 31=worst) \n",
199
 
                   param->quant,
200
 
                   (int)(inv_scale_quant( param->mpeg == 1 ? 0 : 1,
201
 
                                          param->quant))
202
 
            );
203
 
 
204
 
        aprintf("[mpeg2enc]Field order for input: %s \n",
205
 
                           mpeg_interlace_code_definition(param->input_interlacing) );
206
 
 
207
 
        if( param->seq_length_limit )
208
 
        {
209
 
          aprintf( "[mpeg2enc]New Sequence every %d Mbytes \n", param->seq_length_limit );
210
 
          aprintf( "[mpeg2enc]Assuming non-video stream of %d Kbps \n", param->nonvid_bitrate );
211
 
        }
212
 
        else
213
 
          aprintf( "[mpeg2enc]Sequence unlimited length \n" );
214
 
 
215
 
        aprintf("[mpeg2enc]Search radius: %d \n",param->searchrad);
216
 
 
217
 
        /* set params */
218
 
        init_mpeg_parms(param,opt);
219
 
 
220
 
        /* read quantization matrices */
221
 
        init_quantmat(param,opt);
222
 
 
223
 
        init_encoder(param,opt);
224
 
        init_quantizer();
225
 
        init_motion();
226
 
        init_transform();
227
 
        init_predict();
228
 
        //putseq();
229
 
        push_init();
230
 
        putseq_init();
231
 
        aprintf("opt->enc_height2 :%d opt->enc_width: %d opt->enc_height2:%d \n",
232
 
                opt->enc_height2,opt->enc_width,opt->enc_height);
233
 
        return 1;
234
 
}
235
 
int mpegenc_encode(  char *in,   char *out, int *size,int *flags,int *quant)
236
 
{
237
 
                int type;
238
 
 
239
 
                mpeg2enc_buffer=(unsigned char *)out;
240
 
                *size=0;
241
 
 
242
 
 
243
 
                feedOneFrame(in,
244
 
                                        in+((opt->horizontal_size*opt->vertical_size*5)>>2),
245
 
                                        in+(opt->horizontal_size*opt->vertical_size)
246
 
 
247
 
                                        );
248
 
 
249
 
                fedPictures++;
250
 
 
251
 
                if(fedPictures<PREFILL)
252
 
                {
253
 
                        *size=mpeg2enc_buffer-(unsigned char *)out;
254
 
                        *quant=2;
255
 
                        return 1;;
256
 
                }
257
 
                putseq_next(&type,quant);
258
 
                #warning : Approximate..
259
 
                *quant=map_non_linear_mquant[*quant];
260
 
                *size=mpeg2enc_buffer-(unsigned char *)out;
261
 
                switch(type)
262
 
                {
263
 
                        case I_TYPE:
264
 
                                                *flags=AVI_KEY_FRAME;
265
 
                                                break;
266
 
                        case B_TYPE:
267
 
                                                *flags=AVI_B_FRAME;
268
 
                                                break;
269
 
                        default:
270
 
                                                *flags=0;
271
 
                                                break;
272
 
                }
273
 
                return 1;
274
 
 
275
 
}
276
 
int mpegenc_end(void)
277
 
{
278
 
  uint8_t out[20]; // Temporary buffer to store SEQ_END_CODE
279
 
         mpeg2enc_buffer=(unsigned char *)out;
280
 
         putseq_end();
281
 
         mpeg_cleanup(  );
282
 
         mpeg_freebuffers();
283
 
         return 1;
284
 
}
285
 
/*
286
 
        Release (most) memory consumed by mpeg2enc
287
 
 
288
 
*/
289
 
int  mpeg_cleanup( void )
290
 
{
291
 
int n,i;
292
 
        if(!frame_buffers)
293
 
        {
294
 
                printf("Trying to clean already cleaned frame_buffers!!!\n");
295
 
                return 0;
296
 
        }
297
 
        // frame buffers is cleaned by mpeg_free
298
 
        frame_buffers=NULL;
299
 
        push_cleanup();
300
 
        if(opt->motion_data)
301
 
        {
302
 
                delete [] opt->motion_data;
303
 
                opt->motion_data=NULL;
304
 
        }
305
 
 
306
 
        if(lum_mean) delete [] lum_mean;
307
 
        lum_mean=NULL;
308
 
        printf("frame_buffers cleaned up\n");
309
 
        return 1;
310
 
}
311
 
int mpegenc_setQuantizer(int newQz)
312
 
{
313
 
        if(newQz<2) newQz=2;
314
 
        if(newQz>31) newQz=31;
315
 
 
316
 
        param->quant=newQz;
317
 
        ctl->quant_floor = inv_scale_quant( param->mpeg == 1 ? 0 : 1,
318
 
                                           param->quant );
319
 
        return 1;
320
 
}