1 /*
2  *  transcode.h
3  *
4  *  Copyright (C) Thomas Oestreich - June 2001
5  *
6  *  This file is part of transcode, a video stream processing tool
7  *
8  *  transcode is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2, or (at your option)
11  *  any later version.
12  *
13  *  transcode is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with GNU Make; see the file COPYING.  If not, write to
20  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23 
24 #ifndef _TRANSCODE_H
25 #define _TRANSCODE_H
26 
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 
31 #include <sys/stat.h>
32 #include <sys/time.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <pthread.h>
36 #include <signal.h>
37 #include <stdarg.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include "avilib/avilib.h"
45 #include "aclib/ac.h"
46 #include "libtc/tcglob.h"
47 #include "libtc/framecode.h"
48 #include "libtcvideo/tcvideo.h"
49 
50 
51 #ifdef __bsdi__
52 typedef unsigned int uint32_t;
53 #endif
54 
55 #include "tc_defaults.h"
56 #include "framebuffer.h"
57 #include "libtc/libtc.h"
58 
59 /*************************************************************************/
60 
61 /* ----------------------------
62  *
63  * MPEG profiles for setting
64  * sensible defaults
65  *
66  * ----------------------------*/
67 
68 typedef enum {
69     PROF_NONE = 0,
70     VCD,
71     VCD_PAL,
72     VCD_NTSC,
73     SVCD,
74     SVCD_PAL,
75     SVCD_NTSC,
76     XVCD,
77     XVCD_PAL,
78     XVCD_NTSC,
79     DVD,
80     DVD_PAL,
81     DVD_NTSC
82 } mpeg_profile_t;
83 
84 
85 /* ----------------------------
86  *
87  * Global information structure
88  *
89  * ----------------------------*/
90 
91 typedef struct _transfer_t {
92     int flag;
93     FILE *fd;
94     int size;
95     uint8_t *buffer;
96     uint8_t *buffer2;
97     int attributes;
98 } transfer_t;
99 
100 typedef struct _vob_t {
101 
102     // import info
103 
104     const char *vmod_probed;
105     const char *amod_probed;
106     const char *vmod_probed_xml; // Modules for reading XML data
107     const char *amod_probed_xml;
108 
109     int verbose;
110 
111     TCGlob *video_in_files;
112     TCGlob *audio_in_files;
113     const char *video_in_file;  // Video source file
114     const char *audio_in_file;  // Audio source file
115 
116     const char *nav_seek_file;  // Seek/index information
117 
118     int has_audio;              // Does the stream have audio?
119     int has_audio_track;        // Does the requested audio track exist?
120     int has_video;              // Does the stream have video?
121 
122     int lang_code;              // Language of audio track
123 
124     int a_track;                // Audio track ID
125     int v_track;                // Video track ID
126     int s_track;                // Subtitle track ID
127 
128     int sync;                   // Frame offset for audio/video synchronization
129     int sync_ms;                // Fine-tuning for audio/video synchronization
130     int sync_samples;           // sync_ms converted to samples
131 
132     int dvd_title;
133     int dvd_chapter1;
134     int dvd_chapter2;
135     int dvd_max_chapters;
136     int dvd_angle;
137 
138     int ps_unit;
139     int ps_seq1;
140     int ps_seq2;
141 
142     int ts_pid1;
143     int ts_pid2;
144 
145     int vob_offset;
146     int vob_chunk;
147     int vob_chunk_num1;
148     int vob_chunk_num2;
149     int vob_chunk_max;
150     int vob_percentage;
151 
152     int vob_psu_num1;
153     int vob_psu_num2;
154 
155     const char *vob_info_file;
156 
157     double pts_start;
158 
159     double psu_offset;          // PSU offset to pass to extsub
160 
161     int demuxer;
162 
163     long v_format_flag;         // Video stream format
164     long v_codec_flag;          // Video codec
165     long a_format_flag;         // Audio stream format
166     long a_codec_flag;          // Audio codec
167 
168     int quality;
169 
170     // Audio stream parameters
171 
172     int a_stream_bitrate;       // Source stream bitrate
173 
174     int a_chan;
175     int a_bits;
176     int a_rate;
177 
178     int a_padrate;              // Zero padding rate
179 
180     int im_a_size;              // Import total bytes per audio frame
181     int ex_a_size;              // Export total bytes per audio frame
182 
183     int im_a_codec;             // True frame buffer audio codec
184 
185     int a_leap_frame;
186     int a_leap_bytes;
187 
188     int a_vbr;                  // LAME VBR switch
189 
190     int a52_mode;
191 
192     int dm_bits;
193     int dm_chan;
194 
195     // Video stream parameters
196 
197     int v_stream_bitrate;       // Source stream bitrate
198 
199     double fps;                 // Import frame rate (default 25 fps)
200     int im_frc;                 // Import frame rate code
201     double ex_fps;              // Export frame rate (default 25 fps)
202     int ex_frc;                 // Export frame rate code
203     int hard_fps_flag;          // If this is set, disable demuxer smooth drop
204 
205     int pulldown;               // Set 3:2 pulldown flags on MPEG export
206 
207     int im_v_height;            // Import picture height
208     int im_v_width;             // Import picture width
209     int im_v_size;              // Total number of bytes per frame
210 
211     int im_asr;                 // Import aspect ratio code
212     int im_par;                 // Import pixel aspect (code)
213     int im_par_width;           // Import pixel aspect width
214     int im_par_height;          // Import pixel aspect height
215     int ex_asr;                 // Export aspect ratio code
216     int ex_par;                 // Export pixel aspect (code)
217     int ex_par_width;           // Export pixel aspect width
218     int ex_par_height;          // Export pixel aspect height
219 
220     int attributes;             // More video frame attributes
221 
222     int im_v_codec;             // True frame buffer video codec
223 
224     int encode_fields;          // Interlaced field handling flag
225 
226     int dv_yuy2_mode;           // Decode DV video in YUY2 mode?
227 
228     // Audio frame manipulation info
229 
230     double volume;              // Audio amplitude rescale parameter
231     double ac3_gain[3];         // Audio amplitude rescale parameter for ac3
232     int clip_count;             // # of bytes clipped after volume adjustment
233 
234     // Video frame manipulation info
235 
236     int ex_v_width;             // Export picture width
237     int ex_v_height;            // Export picture height
238     int ex_v_size;              // Total number of bytes per frame
239 
240     int reduce_h;               // Reduction factor for frame height
241     int reduce_w;               // Reduction factor for frame width
242 
243     int resize1_mult;           // Multiplier for {vert,hori}_resize1
244     int vert_resize1;           // Height resize amount (shrink)
245     int hori_resize1;           // Width resize amount (shrink)
246 
247     int resize2_mult;           // Multiplier for {vert,hori}_resize2
248     int vert_resize2;           // Height resize amount (expand)
249     int hori_resize2;           // Width resize amount (expand)
250 
251     int zoom_width;             // Zoom width
252     int zoom_height;            // Zoom height
253     int zoom_interlaced;        // Zoom in interlaced mode?
254 
255     TCVZoomFilter zoom_filter;
256 
257     int antialias;
258     int deinterlace;
259     int decolor;
260 
261     double aa_weight;           // Antialiasing center pixel weight
262     double aa_bias;             // Antialiasing horizontal/vertical bias
263 
264     double gamma;
265 
266     int ex_clip_top;
267     int ex_clip_bottom;
268     int ex_clip_left;
269     int ex_clip_right;
270 
271     int im_clip_top;
272     int im_clip_bottom;
273     int im_clip_left;
274     int im_clip_right;
275 
276     int post_ex_clip_top;
277     int post_ex_clip_bottom;
278     int post_ex_clip_left;
279     int post_ex_clip_right;
280 
281     int pre_im_clip_top;
282     int pre_im_clip_bottom;
283     int pre_im_clip_left;
284     int pre_im_clip_right;
285 
286     // Export info
287 
288     const char *video_out_file;
289     const char *audio_out_file;
290 
291     avi_t *avifile_in;
292     avi_t *avifile_out;
293     int avi_comment_fd;         // Text file to read AVI header comments from
294 
295     int audio_file_flag;        // Nonzero if audio goes to its own file
296 
297     // Encoding parameters
298 
299     int divxbitrate;
300     int divxkeyframes;
301     int divxquality;
302     int divxcrispness;
303     int divxmultipass;
304     int video_max_bitrate;
305     const char *divxlogfile;
306 
307     int min_quantizer;
308     int max_quantizer;
309 
310     int rc_period;
311     int rc_reaction_period;
312     int rc_reaction_ratio;
313 
314     int divx5_vbv_prof;         // Profile number
315     int divx5_vbv_bitrate;      // Video Bitrate Verifier constraint overrides
316     int divx5_vbv_size;
317     int divx5_vbv_occupancy;
318 
319     int mp3bitrate;
320     int mp3frequency;
321     float mp3quality;           // 0=best (very slow), 9=worst (default=5)
322     int mp3mode;                // 0=joint-stereo, 1=full-stereo, 2=mono
323 
324     int bitreservoir;
325     const char *lame_preset;
326 
327     const char *audiologfile;
328 
329     int ex_a_codec;             // Audio codec for export module
330     int ex_v_codec;             // Video codec for export module
331 
332     const char *ex_v_fcc;       // Video fourcc string
333     const char *ex_a_fcc;       // Audio fourcc string/identifier
334     const char *ex_profile_name; // User profile name
335 
336     int pass_flag;
337     int encoder_flush;          // flush encoders on close (yes)
338 
339     const char *mod_path;
340 
341     struct fc_time *ttime;      // For framecode parsing (list of structs)
342 
343     unsigned int frame_interval; // Select every `frame_interval' frames only
344 
345     char *im_v_string;          // Extra options for import video module
346     char *im_a_string;          // Extra options for import audio module
347     char *ex_v_string;          // Extra options for export video module
348     char *ex_a_string;          // Extra options for export audio module
349     char *ex_m_string;          // Extra options for multiplexor module
350 
351     float m2v_requant;          // Requantize factor for mpeg2 video streams
352 
353     mpeg_profile_t mpeg_profile;
354 
355     unsigned int export_attributes;
356 } vob_t;
357 
358 
359 typedef struct subtitle_header_s {
360 
361     unsigned int header_length;
362     unsigned int header_version;
363     unsigned int payload_length;
364 
365     unsigned int lpts;
366     double rpts;
367 
368     unsigned int discont_ctr;
369 
370 } subtitle_header_t;
371 
372 /*************************************************************************/
373 
374 // Module functions
375 
376 int tc_import(int opt, void *para1, void *para2);
377 int tc_export(int opt, void *para1, void *para2);
378 
379 // Some functions exported by transcode
380 
381 vob_t *tc_get_vob(void);
382 
383 int tc_next_video_in_file(vob_t *vob);
384 int tc_next_audio_in_file(vob_t *vob);
385 
386 int tc_has_more_video_in_file(vob_t *vob);
387 int tc_has_more_audio_in_file(vob_t *vob);
388 
389 void tc_outstream_rotate(void);
390 void tc_outstream_rotate_request(void);
391 
392 void version(void);
393 
394 extern int verbose;
395 extern int pcmswap;
396 extern int rescale;
397 extern int im_clip;
398 extern int ex_clip;
399 extern int pre_im_clip;
400 extern int post_ex_clip;
401 extern int flip;
402 extern int mirror;
403 extern int rgbswap;
404 extern int resize1;
405 extern int resize2;
406 extern int decolor;
407 extern int zoom;
408 extern int dgamma;
409 extern int keepasr;
410 extern int fast_resize;
411 
412 // Core parameters
413 
414 extern int tc_buffer_delay_dec;
415 extern int tc_buffer_delay_enc;
416 extern int tc_cluster_mode;
417 extern int tc_decoder_delay;
418 extern int tc_progress_meter;
419 extern int tc_progress_rate;
420 extern int tc_accel;
421 extern unsigned int tc_avi_limit;
422 extern pid_t tc_probe_pid;
423 extern int tc_niceness;
424 
425 extern int max_frame_buffer;
426 extern int max_frame_threads;
427 
428 // Various constants
429 
430 enum {
431     TC_EXPORT_NAME = 10,
432     TC_EXPORT_OPEN,
433     TC_EXPORT_INIT,
434     TC_EXPORT_ENCODE,
435     TC_EXPORT_CLOSE,
436     TC_EXPORT_STOP,
437 };
438 
439 enum {
440     TC_EXPORT_ERROR   = -1,
441     TC_EXPORT_OK      =  0,
442     TC_EXPORT_UNKNOWN =  1,
443 };
444 
445 enum {
446     TC_IMPORT_NAME = 20,
447     TC_IMPORT_OPEN,
448     TC_IMPORT_DECODE,
449     TC_IMPORT_CLOSE,
450 };
451 
452 enum {
453     TC_IMPORT_ERROR    = -1,
454     TC_IMPORT_OK       =  0,
455     TC_IMPORT_UNKNOWN  =  1,
456 };
457 
458 enum {
459     TC_CAP_NONE   =   0,
460     TC_CAP_PCM    =   1,
461     TC_CAP_RGB    =   2,
462     TC_CAP_AC3    =   4,
463     TC_CAP_YUV    =   8,
464     TC_CAP_AUD    =  16,
465     TC_CAP_VID    =  32,
466     TC_CAP_MP3    =  64,
467     TC_CAP_YUY2   = 128,
468     TC_CAP_DV     = 256,
469     TC_CAP_YUV422 = 512,
470 };
471 
472 enum {
473     TC_MODE_DEFAULT     =  0,
474     TC_MODE_AVI_SPLIT   =  1,
475     TC_MODE_DVD_CHAPTER =  2,
476     TC_MODE_PSU         =  4,
477     TC_MODE_DIRECTORY   = 16,
478     TC_MODE_DEBUG       = 32,
479 };
480 
481 enum {
482     TC_ENCODE_FIELDS_PROGRESSIVE = 0,
483     TC_ENCODE_FIELDS_TOP_FIRST,
484     TC_ENCODE_FIELDS_BOTTOM_FIRST,
485     TC_ENCODE_FIELDS_UNKNOWN,
486 };
487 
488 /*************************************************************************/
489 
490 #endif  // _TRANSCODE_H
491 
492 /*
493  * Local variables:
494  *   c-file-style: "stroustrup"
495  *   c-file-offsets: ((case-label . *) (statement-case-intro . *))
496  *   indent-tabs-mode: nil
497  * End:
498  *
499  * vim: expandtab shiftwidth=4:
500  */
501