1 /*****************************************************************************
2  * lwinput.h
3  *****************************************************************************
4  * Copyright (C) 2011-2015 L-SMASH Works project
5  *
6  * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
7  *
8  * Permission to use, copy, modify, and/or distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  *****************************************************************************/
20 
21 /* This file is available under an ISC license.
22  * However, when distributing its binary file, it will be under LGPL or GPL.
23  * Don't distribute it if its license is GPL. */
24 
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <inttypes.h>
28 #include <windows.h>
29 #include <mmreg.h>
30 
31 #include "input.h"
32 
33 #include "lwcolor.h"
34 #include "../common/utils.h"
35 
36 #define MAKE_AVIUTL_PITCH( x ) ((((x) + 31) & ~31) >> 3)
37 #define PREFERRED_DECODER_NAMES_BUFSIZE 512
38 
39 #define MESSAGE_BOX_DESKTOP( uType, ... ) \
40 do \
41 { \
42     char temp[256]; \
43     wsprintf( temp, __VA_ARGS__ ); \
44     MessageBox( HWND_DESKTOP, temp, "lwinput", uType ); \
45 } while( 0 )
46 
47 /* Macros for debug */
48 #if defined( DEBUG_VIDEO ) || defined( DEBUG_AUDIO )
49 #define DEBUG_MESSAGE_BOX_DESKTOP( uType, ... ) MESSAGE_BOX_DESKTOP( uType, __VA_ARGS__ )
50 #else
51 #define DEBUG_MESSAGE_BOX_DESKTOP( uType, ... )
52 #endif
53 
54 #ifdef DEBUG_VIDEO
55 #define DEBUG_VIDEO_MESSAGE_BOX_DESKTOP( uType, ... ) DEBUG_MESSAGE_BOX_DESKTOP( uType, __VA_ARGS__ )
56 #else
57 #define DEBUG_VIDEO_MESSAGE_BOX_DESKTOP( uType, ... )
58 #endif
59 
60 #ifdef DEBUG_AUDIO
61 #define DEBUG_AUDIO_MESSAGE_BOX_DESKTOP( uType, ... ) DEBUG_MESSAGE_BOX_DESKTOP( uType, __VA_ARGS__ )
62 #else
63 #define DEBUG_AUDIO_MESSAGE_BOX_DESKTOP( uType, ... )
64 #endif
65 
66 typedef enum
67 {
68     READER_NONE       = 0,
69     LIBAVSMASH_READER = 1,
70     AVS_READER        = 2,
71     VPY_READER        = 3,
72     LIBAV_READER      = 4,
73     DUMMY_READER      = 5,
74 } reader_type;
75 
76 typedef struct
77 {
78     int seek_mode;
79     int forward_seek_threshold;
80     int scaler;
81     int apply_repeat_flag;
82     int field_dominance;
83     struct
84     {
85         int active;
86         int framerate_num;
87         int framerate_den;
88     } vfr2cfr;
89     output_colorspace_index colorspace;
90     struct
91     {
92         int width;
93         int height;
94         int framerate_num;
95         int framerate_den;
96         output_colorspace_index colorspace;
97     } dummy;
98     struct
99     {
100         int bit_depth;
101     } avs;
102 } video_option_t;
103 
104 enum
105 {
106     MIX_LEVEL_INDEX_CENTER = 0,
107     MIX_LEVEL_INDEX_SURROUND,
108     MIX_LEVEL_INDEX_LFE,
109 };
110 
111 typedef struct
112 {
113     uint64_t channel_layout;
114     int      sample_rate;
115     int      mix_level[3];      /* { Center, Surround, LFE } */
116 } audio_option_t;
117 
118 typedef struct
119 {
120     int threads;
121     int av_sync;
122     char         preferred_decoder_names_buf[PREFERRED_DECODER_NAMES_BUFSIZE];
123     const char **preferred_decoder_names;
124     /* for libav reader */
125     int no_create_index;
126     int force_video;
127     int force_video_index;
128     int force_audio;
129     int force_audio_index;
130     /* for video stream */
131     video_option_t video_opt;
132     /* for audio stream */
133     audio_option_t audio_opt;
134 } reader_option_t;
135 
136 typedef struct lsmash_handler_tag lsmash_handler_t;
137 
138 typedef struct
139 {
140     reader_type type;
141     void *(*open_file)             ( char *file_name, reader_option_t *opt );
142     int   (*get_video_track)       ( lsmash_handler_t *h, video_option_t *opt );
143     int   (*get_audio_track)       ( lsmash_handler_t *h, audio_option_t *opt );
144     void  (*destroy_disposable)    ( void *private_stuff );
145     int   (*read_video)            ( lsmash_handler_t *h, int sample_number, void *buf );
146     int   (*read_audio)            ( lsmash_handler_t *h, int start, int wanted_length, void *buf );
147     int   (*is_keyframe)           ( lsmash_handler_t *h, int sample_number );
148     int   (*delay_audio)           ( lsmash_handler_t *h, int *start, int wanted_length, int audio_delay );
149     void  (*video_cleanup)         ( lsmash_handler_t *h );
150     void  (*audio_cleanup)         ( lsmash_handler_t *h );
151     void  (*close_file)            ( void *private_stuff );
152 } lsmash_reader_t;
153 
154 struct lsmash_handler_tag
155 {
156     void                *global_private;
157     void (*close_file)( void *private_stuff );
158     /* Video stuff */
159     reader_type          video_reader;
160     void                *video_private;
161     BITMAPINFOHEADER     video_format;
162     int                  framerate_num;
163     int                  framerate_den;
164     uint32_t             video_sample_count;
165     int  (*read_video)      ( lsmash_handler_t *h, int sample_number, void *buf );
166     int  (*is_keyframe)     ( lsmash_handler_t *h, int sample_number );
167     void (*video_cleanup)   ( lsmash_handler_t *h );
168     void (*close_video_file)( void *private_stuff );
169     /* Audio stuff */
170     reader_type          audio_reader;
171     void                *audio_private;
172     WAVEFORMATEXTENSIBLE audio_format;
173     uint32_t             audio_pcm_sample_count;
174     int  (*read_audio)      ( lsmash_handler_t *h, int start, int wanted_length, void *buf );
175     int  (*delay_audio)     ( lsmash_handler_t *h, int *start, int wanted_length, int audio_delay );
176     void (*audio_cleanup)   ( lsmash_handler_t *h );
177     void (*close_audio_file)( void *private_stuff );
178 };
179 
180 typedef void func_get_output( uint8_t *out_data, int out_linesize, uint8_t **in_data, int in_linesize, int height, int full_range );
181 
182 void au_message_box_desktop
183 (
184     lw_log_handler_t *lhp,
185     lw_log_level      level,
186     const char       *message
187 );
188