]> git.sesse.net Git - vlc/blob - modules/codec/omxil/omxil_utils.h
mediacodec: fix comparison between signed and unsigned warning
[vlc] / modules / codec / omxil / omxil_utils.h
1 /*****************************************************************************
2  * omxil_utils.h: helper functions
3  *****************************************************************************
4  * Copyright (C) 2010 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * OMX macros
26  *****************************************************************************/
27 #ifdef __ANDROID__
28 #define OMX_VERSION_MAJOR 1
29 #define OMX_VERSION_MINOR 0
30 #define OMX_VERSION_REV   0
31 #define OMX_VERSION_STEP  0
32 #elif defined(RPI_OMX)
33 #define OMX_VERSION_MAJOR 1
34 #define OMX_VERSION_MINOR 1
35 #define OMX_VERSION_REV   2
36 #define OMX_VERSION_STEP  0
37 #else
38 #define OMX_VERSION_MAJOR 1
39 #define OMX_VERSION_MINOR 1
40 #define OMX_VERSION_REV   1
41 #define OMX_VERSION_STEP  0
42 #endif
43
44 #define OMX_INIT_COMMON(a) \
45   (a).nSize = sizeof(a); \
46   (a).nVersion.s.nVersionMajor = OMX_VERSION_MAJOR; \
47   (a).nVersion.s.nVersionMinor = OMX_VERSION_MINOR; \
48   (a).nVersion.s.nRevision = OMX_VERSION_REV; \
49   (a).nVersion.s.nStep = OMX_VERSION_STEP
50
51 #define OMX_INIT_STRUCTURE(a) \
52   memset(&(a), 0, sizeof(a)); \
53   OMX_INIT_COMMON(a)
54
55 #define OMX_ComponentRoleEnum(hComponent, cRole, nIndex) \
56     ((OMX_COMPONENTTYPE*)hComponent)->ComponentRoleEnum ? \
57     ((OMX_COMPONENTTYPE*)hComponent)->ComponentRoleEnum(  \
58         hComponent, cRole, nIndex ) : OMX_ErrorNotImplemented
59
60 #define CHECK_ERROR(a, ...) \
61     if(a != OMX_ErrorNone) {msg_Dbg( p_dec, __VA_ARGS__ ); goto error;}
62
63 #ifdef OMX_SKIP64BIT
64 static inline int64_t FromOmxTicks(OMX_TICKS value)
65 {
66     return (((int64_t)value.nHighPart) << 32) | value.nLowPart;
67 }
68 static inline OMX_TICKS ToOmxTicks(int64_t value)
69 {
70     OMX_TICKS s;
71     s.nLowPart = value;
72     s.nHighPart = value >> 32;
73     return s;
74 }
75 #else
76 #define FromOmxTicks(x) (x)
77 #define ToOmxTicks(x) (x)
78 #endif
79
80 /*****************************************************************************
81  * OMX buffer FIFO macros
82  *****************************************************************************/
83 #define OMX_FIFO_INIT(p_fifo, next) \
84     do { vlc_mutex_init( &(p_fifo)->lock ); \
85          vlc_cond_init( &(p_fifo)->wait ); \
86          (p_fifo)->offset = offsetof(OMX_BUFFERHEADERTYPE, next) / sizeof(void *); \
87          (p_fifo)->pp_last = &(p_fifo)->p_first; } while(0)
88
89 #define OMX_FIFO_DESTROY(p_fifo) \
90     do { vlc_mutex_destroy( &(p_fifo)->lock ); \
91          vlc_cond_destroy (&(p_fifo)->wait); } while(0)
92
93 #define OMX_FIFO_PEEK(p_fifo, p_buffer) \
94          p_buffer = (p_fifo)->p_first;
95
96 #define OMX_FIFO_GET(p_fifo, p_buffer) \
97     do { vlc_mutex_lock( &(p_fifo)->lock ); \
98          while( !(p_fifo)->p_first ) \
99              vlc_cond_wait( &(p_fifo)->wait, &(p_fifo)->lock ); \
100          p_buffer = (p_fifo)->p_first; \
101          OMX_BUFFERHEADERTYPE **pp_next = (OMX_BUFFERHEADERTYPE **) \
102              ((void **)p_buffer + (p_fifo)->offset); \
103          (p_fifo)->p_first = *pp_next; *pp_next = 0; \
104          if( !(p_fifo)->p_first ) (p_fifo)->pp_last = &(p_fifo)->p_first; \
105          vlc_mutex_unlock( &(p_fifo)->lock ); } while(0)
106
107 #define OMX_FIFO_GET_TIMEOUT(p_fifo, p_buffer, timeout) \
108     do { vlc_mutex_lock( &(p_fifo)->lock ); \
109          mtime_t end = mdate() + timeout; \
110          if( !(p_fifo)->p_first ) \
111              vlc_cond_timedwait( &(p_fifo)->wait, &(p_fifo)->lock, end ); \
112          p_buffer = (p_fifo)->p_first; \
113          if( p_buffer ) { \
114              OMX_BUFFERHEADERTYPE **pp_next = (OMX_BUFFERHEADERTYPE **) \
115                  ((void **)p_buffer + (p_fifo)->offset); \
116              (p_fifo)->p_first = *pp_next; *pp_next = 0; \
117              if( !(p_fifo)->p_first ) (p_fifo)->pp_last = &(p_fifo)->p_first; \
118          } \
119          vlc_mutex_unlock( &(p_fifo)->lock ); } while(0)
120
121 #define OMX_FIFO_PUT(p_fifo, p_buffer) \
122     do { vlc_mutex_lock (&(p_fifo)->lock);              \
123          OMX_BUFFERHEADERTYPE **pp_next = (OMX_BUFFERHEADERTYPE **) \
124              ((void **)p_buffer + (p_fifo)->offset); \
125          *(p_fifo)->pp_last = p_buffer; \
126          (p_fifo)->pp_last = pp_next; *pp_next = 0; \
127          vlc_cond_signal( &(p_fifo)->wait ); \
128          vlc_mutex_unlock( &(p_fifo)->lock ); } while(0)
129
130 /*****************************************************************************
131  * OMX format parameters
132  *****************************************************************************/
133 typedef union {
134     OMX_PARAM_U32TYPE common;
135     OMX_AUDIO_PARAM_PCMMODETYPE pcm;
136     OMX_AUDIO_PARAM_MP3TYPE mp3;
137     OMX_AUDIO_PARAM_AACPROFILETYPE aac;
138     OMX_AUDIO_PARAM_VORBISTYPE vorbis;
139     OMX_AUDIO_PARAM_WMATYPE wma;
140     OMX_AUDIO_PARAM_RATYPE ra;
141     OMX_AUDIO_PARAM_ADPCMTYPE adpcm;
142     OMX_AUDIO_PARAM_G723TYPE g723;
143     OMX_AUDIO_PARAM_G726TYPE g726;
144     OMX_AUDIO_PARAM_G729TYPE g729;
145     OMX_AUDIO_PARAM_AMRTYPE amr;
146
147     OMX_VIDEO_PARAM_H263TYPE h263;
148     OMX_VIDEO_PARAM_MPEG2TYPE mpeg2;
149     OMX_VIDEO_PARAM_MPEG4TYPE mpeg4;
150     OMX_VIDEO_PARAM_WMVTYPE wmv;
151     OMX_VIDEO_PARAM_RVTYPE rv;
152     OMX_VIDEO_PARAM_AVCTYPE avc;
153
154 } OmxFormatParam;
155
156 /*****************************************************************************
157  * Events utility functions
158  *****************************************************************************/
159 typedef struct OmxEvent
160 {
161     OMX_EVENTTYPE event;
162     OMX_U32 data_1;
163     OMX_U32 data_2;
164     OMX_PTR event_data;
165
166     struct OmxEvent *next;
167 } OmxEvent;
168
169 typedef struct OmxEventQueue
170 {
171     OmxEvent *p_events;
172     OmxEvent **pp_last_event;
173
174     vlc_mutex_t mutex;
175     vlc_cond_t cond;
176 } OmxEventQueue;
177
178 void InitOmxEventQueue(OmxEventQueue *queue);
179 void DeinitOmxEventQueue(OmxEventQueue *queue);
180 OMX_ERRORTYPE PostOmxEvent(OmxEventQueue *queue, OMX_EVENTTYPE event,
181     OMX_U32 data_1, OMX_U32 data_2, OMX_PTR event_data);
182 OMX_ERRORTYPE WaitForOmxEvent(OmxEventQueue *queue, OMX_EVENTTYPE *event,
183     OMX_U32 *data_1, OMX_U32 *data_2, OMX_PTR *event_data);
184 OMX_ERRORTYPE WaitForSpecificOmxEvent(OmxEventQueue *queue,
185     OMX_EVENTTYPE specific_event, OMX_U32 *data_1, OMX_U32 *data_2,
186     OMX_PTR *event_data);
187 void PrintOmxEvent(vlc_object_t *p_this, OMX_EVENTTYPE event, OMX_U32 data_1,
188     OMX_U32 data_2, OMX_PTR event_data);
189
190 /*****************************************************************************
191  * Picture utility functions
192  *****************************************************************************/
193 typedef struct ArchitectureSpecificCopyData
194 {
195     void *data;
196 } ArchitectureSpecificCopyData;
197
198 void ArchitectureSpecificCopyHooks( decoder_t *p_dec, int i_color_format,
199                                     int i_slice_height, int i_src_stride,
200                                     ArchitectureSpecificCopyData *p_architecture_specific );
201
202 void ArchitectureSpecificCopyHooksDestroy( int i_color_format,
203                                            ArchitectureSpecificCopyData *p_architecture_specific );
204
205 void CopyOmxPicture( int i_color_format, picture_t *p_pic,
206                      int i_slice_height,
207                      int i_src_stride, uint8_t *p_src, int i_chroma_div,
208                      ArchitectureSpecificCopyData *p_architecture_specific );
209
210 void CopyVlcPicture( decoder_t *, OMX_BUFFERHEADERTYPE *, picture_t * );
211
212 int IgnoreOmxDecoderPadding(const char *psz_name);
213
214 /*****************************************************************************
215  * Logging utility functions
216  *****************************************************************************/
217 const char *StateToString(OMX_STATETYPE state);
218 const char *CommandToString(OMX_COMMANDTYPE command);
219 const char *EventToString(OMX_EVENTTYPE event);
220 const char *ErrorToString(OMX_ERRORTYPE error);
221
222 void PrintOmx(decoder_t *p_dec, OMX_HANDLETYPE omx_handle, OMX_U32 i_port);
223
224 /*****************************************************************************
225  * fourcc -> omx id mapping
226  *****************************************************************************/
227 int GetOmxVideoFormat( vlc_fourcc_t i_fourcc,
228                        OMX_VIDEO_CODINGTYPE *pi_omx_codec,
229                        const char **ppsz_name );
230 int GetVlcVideoFormat( OMX_VIDEO_CODINGTYPE i_omx_codec,
231                        vlc_fourcc_t *pi_fourcc, const char **ppsz_name );
232 int GetOmxAudioFormat( vlc_fourcc_t i_fourcc,
233                        OMX_AUDIO_CODINGTYPE *pi_omx_codec,
234                        const char **ppsz_name );
235 int OmxToVlcAudioFormat( OMX_AUDIO_CODINGTYPE i_omx_codec,
236                        vlc_fourcc_t *pi_fourcc, const char **ppsz_name );
237 const char *GetOmxRole( vlc_fourcc_t i_fourcc, int i_cat, bool b_enc );
238 int GetOmxChromaFormat( vlc_fourcc_t i_fourcc,
239                         OMX_COLOR_FORMATTYPE *pi_omx_codec,
240                         const char **ppsz_name );
241 int GetVlcChromaFormat( OMX_COLOR_FORMATTYPE i_omx_codec,
242                         vlc_fourcc_t *pi_fourcc, const char **ppsz_name );
243 int GetVlcChromaSizes( vlc_fourcc_t i_fourcc,
244                        unsigned int width, unsigned int height,
245                        unsigned int *size, unsigned int *pitch,
246                        unsigned int *chroma_pitch_div );
247
248 /*****************************************************************************
249  * Functions to deal with audio format parameters
250  *****************************************************************************/
251 OMX_ERRORTYPE SetAudioParameters(OMX_HANDLETYPE handle,
252     OmxFormatParam *param, OMX_U32 i_port, OMX_AUDIO_CODINGTYPE encoding,
253     vlc_fourcc_t i_codec, uint8_t i_channels, unsigned int i_samplerate,
254     unsigned int i_bitrate, unsigned int i_bps, unsigned int i_blocksize);
255 OMX_ERRORTYPE GetAudioParameters(OMX_HANDLETYPE handle,
256     OmxFormatParam *param, OMX_U32 i_port, OMX_AUDIO_CODINGTYPE encoding,
257     uint8_t *pi_channels, unsigned int *pi_samplerate,
258     unsigned int *pi_bitrate, unsigned int *pi_bps, unsigned int *pi_blocksize);
259 unsigned int GetAudioParamSize(OMX_INDEXTYPE index);
260
261 /*****************************************************************************
262  * Vendor specific color formats
263  *****************************************************************************/
264 #define OMX_QCOM_COLOR_FormatYVU420SemiPlanar 0x7FA30C00
265 #define OMX_TI_COLOR_FormatYUV420PackedSemiPlanar 0x7F000100
266 #define QOMX_COLOR_FormatYUV420PackedSemiPlanar64x32Tile2m8ka 0x7FA30C03
267 #define OMX_QCOM_COLOR_FormatYUV420PackedSemiPlanar32m 0x7FA30C04
268 #define OMX_IndexVendorSetYUV420pMode 0x7f000003
269
270 /*****************************************************************************
271  * H264 specific code
272  *****************************************************************************/
273 size_t convert_omx_to_profile_idc(OMX_VIDEO_AVCPROFILETYPE profile_type);
274
275 size_t convert_omx_to_level_idc(OMX_VIDEO_AVCLEVELTYPE level_type);