M4MCS_Codecs.c revision e1f105895f913b19ad848cc90a3cd57dad657abf
1/*
2 * Copyright (C) 2004-2011 NXP Software
3 * Copyright (C) 2011 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17/**
18 ************************************************************************
19 * @file   M4MCS_Codecs.c
20 * @brief  MCS implementation
21 * @note   This file contains all functions related to audio/video
22 *         codec manipulations.
23 ************************************************************************
24 */
25
26/**
27 ********************************************************************
28 * Includes
29 ********************************************************************
30 */
31#include "NXPSW_CompilerSwitches.h"
32#include "M4OSA_Debug.h"            /* Include for OSAL debug services */
33#include "M4MCS_InternalTypes.h"    /* Internal types of the MCS */
34
35
36#ifdef M4MCS_SUPPORT_VIDEC_3GP
37#include "M4_MPEG4VI_VideoHandler.h"  /*needed for renderer error codes*/
38#endif
39
40
41/**
42 ************************************************************************
43 * M4OSA_ERR   M4MCS_clearInterfaceTables()
44 * @brief    Clear encoders, decoders, reader and writers interfaces tables
45 * @param    pContext            (IN/OUT) MCS context.
46 * @return    M4NO_ERROR:            No error
47 * @return    M4ERR_PARAMETER:    The context is null
48 ************************************************************************
49 */
50M4OSA_ERR   M4MCS_clearInterfaceTables(M4MCS_Context pContext)
51{
52    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
53    M4OSA_UInt8 i;
54
55    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, "invalid context pointer");
56
57    /* Initialisation that will allow to check if registering twice */
58    pC->pWriterGlobalFcts = M4OSA_NULL;
59    pC->pWriterDataFcts = M4OSA_NULL;
60    pC->pVideoEncoderGlobalFcts = M4OSA_NULL;
61    pC->pAudioEncoderGlobalFcts = M4OSA_NULL;
62
63    pC->pCurrentVideoEncoderExternalAPI = M4OSA_NULL;
64    pC->pCurrentVideoEncoderUserData = M4OSA_NULL;
65
66    for (i = 0; i < M4WRITER_kType_NB; i++ )
67    {
68        pC->WriterInterface[i].pGlobalFcts = M4OSA_NULL;
69        pC->WriterInterface[i].pDataFcts = M4OSA_NULL;
70    }
71
72    for (i = 0; i < M4ENCODER_kVideo_NB; i++ )
73    {
74        pC->pVideoEncoderInterface[i] = M4OSA_NULL;
75        pC->pVideoEncoderExternalAPITable[i] = M4OSA_NULL;
76        pC->pVideoEncoderUserDataTable[i] = M4OSA_NULL;
77    }
78
79    for (i = 0; i < M4ENCODER_kAudio_NB; i++ )
80    {
81        pC->pAudioEncoderInterface[i] = M4OSA_NULL;
82        pC->pAudioEncoderFlag[i] = M4OSA_FALSE;
83        pC->pAudioEncoderUserDataTable[i] = M4OSA_NULL;
84    }
85
86    /* Initialisation that will allow to check if registering twice */
87    pC->m_pReader = M4OSA_NULL;
88    pC->m_pReaderDataIt   = M4OSA_NULL;
89    pC->m_uiNbRegisteredReaders  = 0;
90
91    for (i = 0; i < M4READER_kMediaType_NB; i++ )
92    {
93        pC->m_pReaderGlobalItTable[i] = M4OSA_NULL;
94        pC->m_pReaderDataItTable[i]   = M4OSA_NULL;
95    }
96
97    pC->m_pVideoDecoder = M4OSA_NULL;
98#ifdef M4VSS_ENABLE_EXTERNAL_DECODERS
99    pC->m_pCurrentVideoDecoderUserData = M4OSA_NULL;
100#endif /* M4VSS_ENABLE_EXTERNAL_DECODERS */
101    pC->m_uiNbRegisteredVideoDec = 0;
102    for (i = 0; i < M4DECODER_kVideoType_NB; i++ )
103    {
104        pC->m_pVideoDecoderItTable[i] = M4OSA_NULL;
105#ifdef M4VSS_ENABLE_EXTERNAL_DECODERS
106        pC->m_pVideoDecoderUserDataTable[i] = M4OSA_NULL;
107#endif /* M4VSS_ENABLE_EXTERNAL_DECODERS */
108    }
109
110    pC->m_pAudioDecoder = M4OSA_NULL;
111    for (i = 0; i < M4AD_kType_NB; i++ )
112    {
113        pC->m_pAudioDecoderItTable[i] = M4OSA_NULL;
114        pC->m_pAudioDecoderFlagTable[i] = M4OSA_FALSE;
115        pC->m_pAudioDecoderUserDataTable[i] = M4OSA_NULL;
116    }
117
118    return M4NO_ERROR;
119}
120
121/**
122 ******************************************************************************
123 * M4OSA_ERR   M4MCS_registerWriter()
124 * @brief    This function will register a specific file format writer.
125 * @note    According to the Mediatype, this function will store in the internal context
126 *          the writer context.
127 * @param    pContext:    (IN) Execution context.
128 * @return    M4NO_ERROR: there is no error
129 * @return      M4ERR_PARAMETER     pContext,pWtrGlobalInterface or
130 *                                  pWtrDataInterface is M4OSA_NULL
131 *                                  (debug only), or invalid MediaType
132 ******************************************************************************
133 */
134M4OSA_ERR   M4MCS_registerWriter(M4MCS_Context pContext, M4WRITER_OutputFileType MediaType,
135                                 M4WRITER_GlobalInterface* pWtrGlobalInterface,
136                                 M4WRITER_DataInterface* pWtrDataInterface)
137{
138    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
139
140    /**
141     *    Check input parameters */
142    M4OSA_DEBUG_IF2((pC == M4OSA_NULL),M4ERR_PARAMETER,
143         "MCS: context is M4OSA_NULL in M4MCS_registerWriter");
144    M4OSA_DEBUG_IF2((pWtrGlobalInterface == M4OSA_NULL),M4ERR_PARAMETER,
145         "pWtrGlobalInterface is M4OSA_NULL in M4MCS_registerWriter");
146    M4OSA_DEBUG_IF2((pWtrDataInterface == M4OSA_NULL),M4ERR_PARAMETER,
147         "pWtrDataInterface is M4OSA_NULL in M4MCS_registerWriter");
148
149    M4OSA_TRACE3_3("MCS: M4MCS_registerWriter called with pContext=0x%x,\
150     pWtrGlobalInterface=0x%x, pWtrDataInterface=0x%x", pC,pWtrGlobalInterface,
151     pWtrDataInterface);
152
153    if((MediaType == M4WRITER_kUnknown) || (MediaType >= M4WRITER_kType_NB))
154    {
155        M4OSA_DEBUG_IF1(M4OSA_TRUE, M4ERR_PARAMETER, "Invalid media type");
156        return M4ERR_PARAMETER;
157    }
158
159    if (pC->WriterInterface[MediaType].pGlobalFcts != M4OSA_NULL)
160    {
161      /* a writer corresponding to this media type has already been registered !*/
162      M4OSA_DEBUG_IF1(M4OSA_TRUE, M4ERR_PARAMETER, "This media type has already been registered");
163      return M4ERR_PARAMETER;
164    }
165
166    /*
167     * Save writer interface in context */
168    pC->WriterInterface[MediaType].pGlobalFcts = pWtrGlobalInterface;
169    pC->WriterInterface[MediaType].pDataFcts = pWtrDataInterface;
170
171    return M4NO_ERROR;
172}
173
174/**
175 ******************************************************************************
176 * M4OSA_ERR   M4MCS_registerEncoder()
177 * @brief    This function will register a specific video encoder.
178 * @note    According to the Mediatype, this function will store in the internal context
179 *           the encoder context.
180 * @param    pContext:    (IN) Execution context.
181 * @return    M4NO_ERROR: there is no error
182 * @return    M4ERR_PARAMETER    pContext or pEncGlobalInterface is M4OSA_NULL (debug only),
183 *                             or invalid MediaType
184 ******************************************************************************
185 */
186M4OSA_ERR   M4MCS_registerVideoEncoder (
187                    M4MCS_Context pContext,
188                    M4ENCODER_Format MediaType,
189                    M4ENCODER_GlobalInterface *pEncGlobalInterface)
190{
191    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
192
193    /**
194     *    Check input parameters */
195    M4OSA_DEBUG_IF2((pC == M4OSA_NULL),M4ERR_PARAMETER,
196         "MCS: context is M4OSA_NULL in M4MCS_registerVideoEncoder");
197    M4OSA_DEBUG_IF2((pEncGlobalInterface == M4OSA_NULL),M4ERR_PARAMETER,
198         "pEncGlobalInterface is M4OSA_NULL in M4MCS_registerVideoEncoder");
199
200    M4OSA_TRACE3_2("MCS: M4MCS_registerVideoEncoder called with pContext=0x%x,\
201         pEncGlobalInterface=0x%x", pC, pEncGlobalInterface);
202
203    if (MediaType >= M4ENCODER_kVideo_NB)
204    {
205      M4OSA_DEBUG_IF1(M4OSA_TRUE, M4ERR_PARAMETER, "Invalid video encoder type");
206      return M4ERR_PARAMETER;
207    }
208
209    if (pC->pVideoEncoderInterface[MediaType] != M4OSA_NULL)
210    {
211        /* can be legitimate, in cases where we have one version that can use external encoders
212        but which still has the built-in one to be able to work without an external encoder; in
213        this case the new encoder simply replaces the old one (i.e. we unregister it first). */
214        M4OSA_free((M4OSA_MemAddr32)pC->pVideoEncoderInterface[MediaType]);
215        pC->pVideoEncoderInterface[MediaType] = M4OSA_NULL;
216    }
217
218    /*
219     * Save encoder interface in context */
220    pC->pVideoEncoderInterface[MediaType] = pEncGlobalInterface;
221    /* The actual userData and external API will be set by the registration function in the case
222    of an external encoder (add it as a parameter to this function in the long run?) */
223    pC->pVideoEncoderUserDataTable[MediaType] = M4OSA_NULL;
224    pC->pVideoEncoderExternalAPITable[MediaType] = M4OSA_NULL;
225
226    return M4NO_ERROR;
227}
228
229/**
230 ******************************************************************************
231 * M4OSA_ERR   M4MCS_registerAudioEncoder()
232 * @brief    This function will register a specific audio encoder.
233 * @note    According to the Mediatype, this function will store in the internal context
234 *           the encoder context.
235 * @param    pContext:                (IN) Execution context.
236 * @param    mediaType:                (IN) The media type.
237 * @param    pEncGlobalInterface:    (OUT) the encoder interface functions.
238 * @return    M4NO_ERROR: there is no error
239 * @return   M4ERR_PARAMETER:   pContext or pEncGlobalInterface is
240 *                              M4OSA_NULL (debug only)
241 ******************************************************************************
242 */
243M4OSA_ERR   M4MCS_registerAudioEncoder(
244                    M4MCS_Context pContext,
245                    M4ENCODER_AudioFormat MediaType,
246                    M4ENCODER_AudioGlobalInterface *pEncGlobalInterface)
247{
248    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
249
250    /**
251     *    Check input parameters */
252    M4OSA_DEBUG_IF2((pC == M4OSA_NULL),M4ERR_PARAMETER,
253         "MCS: context is M4OSA_NULL in M4MCS_registerAudioEncoder");
254    M4OSA_DEBUG_IF2((pEncGlobalInterface == M4OSA_NULL),M4ERR_PARAMETER,
255         "pEncGlobalInterface is M4OSA_NULL in M4MCS_registerAudioEncoder");
256
257    M4OSA_TRACE3_2("MCS: M4MCS_registerAudioEncoder called with pContext=0x%x,\
258         pEncGlobalInterface=0x%x", pC, pEncGlobalInterface);
259
260    if (MediaType >= M4ENCODER_kAudio_NB)
261    {
262        M4OSA_DEBUG_IF1(M4OSA_TRUE, M4ERR_PARAMETER, "Invalid audio encoder type");
263        return M4ERR_PARAMETER;
264    }
265
266    if(M4OSA_NULL != pC->pAudioEncoderInterface[MediaType])
267    {
268        M4OSA_free((M4OSA_MemAddr32)pC->pAudioEncoderInterface[MediaType]);
269        pC->pAudioEncoderInterface[MediaType] = M4OSA_NULL;
270
271        if(M4OSA_NULL != pC->pAudioEncoderUserDataTable[MediaType])
272        {
273            M4OSA_free((M4OSA_MemAddr32)pC->pAudioEncoderUserDataTable[MediaType]);
274            pC->pAudioEncoderUserDataTable[MediaType] = M4OSA_NULL;
275        }
276    }
277
278    /*
279     * Save encoder interface in context */
280    pC->pAudioEncoderInterface[MediaType] = pEncGlobalInterface;
281    pC->pAudioEncoderFlag[MediaType] = M4OSA_FALSE; /* internal encoder */
282
283    return M4NO_ERROR;
284}
285
286/**
287 ************************************************************************
288 * M4OSA_ERR   M4MCS_registerReader()
289 * @brief    Register reader.
290 * @param    pContext            (IN/OUT) MCS context.
291 * @return    M4NO_ERROR:            No error
292 * @return    M4ERR_PARAMETER:    A parameter is null (in DEBUG only)
293 ************************************************************************
294 */
295M4OSA_ERR   M4MCS_registerReader(
296                        M4MCS_Context pContext,
297                        M4READER_MediaType mediaType,
298                        M4READER_GlobalInterface *pRdrGlobalInterface,
299                        M4READER_DataInterface *pRdrDataInterface)
300{
301    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
302
303    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, "invalid context pointer");
304    M4OSA_DEBUG_IF1((M4OSA_NULL == pRdrGlobalInterface),
305         M4ERR_PARAMETER, "M4MCS_registerReader: invalid pointer on global interface");
306    M4OSA_DEBUG_IF1((M4OSA_NULL == pRdrDataInterface),
307         M4ERR_PARAMETER, "M4MCS_registerReader: invalid pointer on data interface");
308
309    if (mediaType == M4READER_kMediaTypeUnknown || mediaType >= M4READER_kMediaType_NB)
310    {
311        M4OSA_DEBUG_IF1(M4OSA_TRUE, M4ERR_PARAMETER, "Invalid media type");
312        return M4ERR_PARAMETER;
313    }
314
315    if (pC->m_pReaderGlobalItTable[mediaType] != M4OSA_NULL)
316    {
317        /* a reader corresponding to this media type has already been registered !*/
318      M4OSA_DEBUG_IF1(M4OSA_TRUE, M4ERR_PARAMETER, "This media type has already been registered");
319      return M4ERR_PARAMETER;
320    }
321
322    pC->m_pReaderGlobalItTable[mediaType] = pRdrGlobalInterface;
323    pC->m_pReaderDataItTable[mediaType]   = pRdrDataInterface;
324
325    pC->m_uiNbRegisteredReaders++;
326
327    return M4NO_ERROR;
328}
329
330/**
331 ************************************************************************
332 * M4OSA_ERR   M4MCS_registerVideoDecoder()
333 * @brief    Register video decoder
334 * @param    pContext                (IN/OUT) MCS context.
335 * @param    decoderType            (IN) Decoder type
336 * @param    pDecoderInterface    (IN) Decoder interface.
337 * @return    M4NO_ERROR:            No error
338 * @return    M4ERR_PARAMETER:    A parameter is null (in DEBUG only), or the decoder
339 *                              type is invalid
340 ************************************************************************
341 */
342M4OSA_ERR   M4MCS_registerVideoDecoder(
343                            M4MCS_Context pContext,
344                            M4DECODER_VideoType decoderType,
345                            M4DECODER_VideoInterface *pDecoderInterface)
346{
347    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
348
349    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, "invalid context pointer");
350    M4OSA_DEBUG_IF1((M4OSA_NULL == pDecoderInterface), M4ERR_PARAMETER,
351         "M4MCS_registerVideoDecoder: invalid pointer on decoder interface");
352
353    if (decoderType >= M4DECODER_kVideoType_NB)
354    {
355      M4OSA_DEBUG_IF1(M4OSA_TRUE, M4ERR_PARAMETER, "Invalid video decoder type");
356      return M4ERR_PARAMETER;
357    }
358
359    if (pC->m_pVideoDecoderItTable[decoderType] != M4OSA_NULL)
360    {
361#ifndef M4VSS_ENABLE_EXTERNAL_DECODERS
362        /* a decoder corresponding to this media type has already been registered !*/
363        M4OSA_DEBUG_IF1(M4OSA_TRUE, M4ERR_PARAMETER, "Decoder has already been registered");
364        return M4ERR_PARAMETER;
365#else /* external decoders are possible */
366        /* can be legitimate, in cases where we have one version that can use external decoders
367        but which still has the built-in one to be able to work without an external decoder; in
368        this case the new decoder simply replaces the old one (i.e. we unregister it first). */
369        M4OSA_free((M4OSA_MemAddr32)pC->m_pVideoDecoderItTable[decoderType]);
370        pC->m_pVideoDecoderItTable[decoderType] = M4OSA_NULL;
371        /* oh, and don't forget the user data, too. */
372        if (pC->m_pVideoDecoderUserDataTable[decoderType] != M4OSA_NULL)
373        {
374            M4OSA_free((M4OSA_MemAddr32)pC->m_pVideoDecoderUserDataTable[decoderType]);
375            pC->m_pVideoDecoderUserDataTable[decoderType] = M4OSA_NULL;
376        }
377#endif /* are external decoders possible? */
378    }
379
380    pC->m_pVideoDecoderItTable[decoderType] = pDecoderInterface;
381#ifdef M4VSS_ENABLE_EXTERNAL_DECODERS
382    pC->m_pVideoDecoderUserDataTable[decoderType] = M4OSA_NULL;
383    /* The actual userData will be set by the registration function in the case
384    of an external decoder (add it as a parameter to this function in the long run?) */
385#endif /* M4VSS_ENABLE_EXTERNAL_DECODERS */
386    pC->m_uiNbRegisteredVideoDec++;
387
388    return M4NO_ERROR;
389}
390
391/**
392 ************************************************************************
393 * M4OSA_ERR   M4MCS_registerAudioDecoder()
394 * @brief    Register audio decoder
395 * @note        This function is used internaly by the MCS to
396 *              register audio decoders,
397 * @param    context                (IN/OUT) MCS context.
398 * @param    decoderType            (IN) Audio decoder type
399 * @param    pDecoderInterface    (IN) Audio decoder interface.
400 * @return    M4NO_ERROR:            No error
401 * @return   M4ERR_PARAMETER:    A parameter is null, or the decoder type is invalid(in DEBUG only)
402 ************************************************************************
403 */
404M4OSA_ERR   M4MCS_registerAudioDecoder(
405                                    M4MCS_Context pContext,
406                                    M4AD_Type decoderType,
407                                    M4AD_Interface *pDecoderInterface)
408{
409    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
410
411    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, "invalid context pointer");
412    M4OSA_DEBUG_IF1((M4OSA_NULL == pDecoderInterface), M4ERR_PARAMETER,
413         "M4MCS_registerAudioDecoder: invalid pointer on decoder interface");
414
415    if (decoderType >= M4AD_kType_NB)
416    {
417        M4OSA_DEBUG_IF1(M4OSA_TRUE, M4ERR_PARAMETER, "Invalid audio decoder type");
418        return M4ERR_PARAMETER;
419    }
420
421    if(M4OSA_NULL != pC->m_pAudioDecoderItTable[decoderType])
422    {
423        M4OSA_free((M4OSA_MemAddr32)pC->m_pAudioDecoderItTable[decoderType]);
424        pC->m_pAudioDecoderItTable[decoderType] = M4OSA_NULL;
425
426        if(M4OSA_NULL != pC->m_pAudioDecoderUserDataTable[decoderType])
427        {
428            M4OSA_free((M4OSA_MemAddr32)pC->m_pAudioDecoderUserDataTable[decoderType]);
429            pC->m_pAudioDecoderUserDataTable[decoderType] = M4OSA_NULL;
430        }
431    }
432    pC->m_pAudioDecoderItTable[decoderType] = pDecoderInterface;
433    pC->m_pAudioDecoderFlagTable[decoderType] = M4OSA_FALSE; /* internal decoder */
434
435    return M4NO_ERROR;
436}
437
438/**
439 ************************************************************************
440 * M4OSA_ERR   M4MCS_unRegisterAllWriters()
441 * @brief    Unregister writer
442 * @param    pContext            (IN/OUT) MCS context.
443 * @return    M4NO_ERROR:            No error
444 * @return    M4ERR_PARAMETER:    A parameter is null (in DEBUG only)
445 ************************************************************************
446 */
447M4OSA_ERR   M4MCS_unRegisterAllWriters(M4MCS_Context pContext)
448{
449    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
450    M4OSA_Int32 i;
451
452    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, "invalid context pointer");
453
454    for (i = 0; i < M4WRITER_kType_NB; i++)
455    {
456        if (pC->WriterInterface[i].pGlobalFcts != M4OSA_NULL)
457        {
458            M4OSA_free((M4OSA_MemAddr32)pC->WriterInterface[i].pGlobalFcts );
459            pC->WriterInterface[i].pGlobalFcts = M4OSA_NULL;
460        }
461        if (pC->WriterInterface[i].pDataFcts != M4OSA_NULL)
462        {
463            M4OSA_free((M4OSA_MemAddr32)pC->WriterInterface[i].pDataFcts );
464            pC->WriterInterface[i].pDataFcts = M4OSA_NULL;
465        }
466    }
467
468    pC->pWriterGlobalFcts = M4OSA_NULL;
469    pC->pWriterDataFcts = M4OSA_NULL;
470
471    return M4NO_ERROR;
472}
473
474/**
475 ************************************************************************
476 * M4OSA_ERR   M4MCS_unRegisterAllEncoders()
477 * @brief    Unregister the encoders
478 * @param    pContext            (IN/OUT) MCS context.
479 * @return    M4NO_ERROR:            No error
480 * @return    M4ERR_PARAMETER:    A parameter is null (in DEBUG only)
481 ************************************************************************
482 */
483M4OSA_ERR   M4MCS_unRegisterAllEncoders(M4MCS_Context pContext)
484{
485    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
486    M4OSA_Int32 i;
487
488    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, "invalid context pointer");
489
490    for (i = 0; i < M4ENCODER_kVideo_NB; i++)
491    {
492        if (pC->pVideoEncoderInterface[i] != M4OSA_NULL)
493        {
494            M4OSA_free( (M4OSA_MemAddr32)pC->pVideoEncoderInterface[i] );
495            pC->pVideoEncoderInterface[i] = M4OSA_NULL;
496        }
497    }
498
499    for (i = 0; i < M4ENCODER_kAudio_NB; i++)
500    {
501        if (pC->pAudioEncoderInterface[i] != M4OSA_NULL)
502        {
503            /*Don't free external audio encoders interfaces*/
504            if (M4OSA_FALSE == pC->pAudioEncoderFlag[i])
505            {
506                M4OSA_free( (M4OSA_MemAddr32)pC->pAudioEncoderInterface[i] );
507            }
508            pC->pAudioEncoderInterface[i] = M4OSA_NULL;
509        }
510    }
511
512    pC->pVideoEncoderGlobalFcts = M4OSA_NULL;
513    pC->pAudioEncoderGlobalFcts = M4OSA_NULL;
514
515    return M4NO_ERROR;
516}
517
518/**
519 ************************************************************************
520 * M4OSA_ERR   M4MCS_unRegisterAllReaders()
521 * @brief    Unregister reader
522 * @param    pContext            (IN/OUT) MCS context.
523 * @return    M4NO_ERROR:            No error
524 * @return    M4ERR_PARAMETER:    A parameter is null (in DEBUG only)
525 ************************************************************************
526 */
527M4OSA_ERR   M4MCS_unRegisterAllReaders(M4MCS_Context pContext)
528{
529    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
530    M4OSA_Int32 i;
531
532    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, "invalid context pointer");
533
534    for (i = 0; i < M4READER_kMediaType_NB; i++)
535    {
536        if (pC->m_pReaderGlobalItTable[i] != M4OSA_NULL)
537        {
538            M4OSA_free((M4OSA_MemAddr32)pC->m_pReaderGlobalItTable[i] );
539            pC->m_pReaderGlobalItTable[i] = M4OSA_NULL;
540        }
541        if (pC->m_pReaderDataItTable[i] != M4OSA_NULL)
542        {
543            M4OSA_free((M4OSA_MemAddr32)pC->m_pReaderDataItTable[i] );
544            pC->m_pReaderDataItTable[i] = M4OSA_NULL;
545        }
546    }
547
548    pC->m_uiNbRegisteredReaders = 0;
549    pC->m_pReader = M4OSA_NULL;
550    pC->m_pReaderDataIt = M4OSA_NULL;
551
552    return M4NO_ERROR;
553}
554
555/**
556 ************************************************************************
557 * M4OSA_ERR   M4MCS_unRegisterAllDecoders()
558 * @brief    Unregister the decoders
559 * @param    pContext            (IN/OUT) MCS context.
560 * @return    M4NO_ERROR:            No error
561 * @return    M4ERR_PARAMETER:    A parameter is null (in DEBUG only)
562 ************************************************************************
563 */
564M4OSA_ERR   M4MCS_unRegisterAllDecoders(M4MCS_Context pContext)
565{
566    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
567    M4OSA_Int32 i;
568
569    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, "invalid context pointer");
570
571    for (i = 0; i < M4DECODER_kVideoType_NB; i++)
572    {
573        if (pC->m_pVideoDecoderItTable[i] != M4OSA_NULL)
574        {
575            M4OSA_free( (M4OSA_MemAddr32)pC->m_pVideoDecoderItTable[i] );
576            pC->m_pVideoDecoderItTable[i] = M4OSA_NULL;
577        }
578    }
579
580    for (i = 0; i < M4AD_kType_NB; i++)
581    {
582        if (pC->m_pAudioDecoderItTable[i] != M4OSA_NULL)
583        {
584            /*Don't free external audio decoders interfaces*/
585            if (M4OSA_FALSE == pC->m_pAudioDecoderFlagTable[i])
586            {
587                M4OSA_free( (M4OSA_MemAddr32)pC->m_pAudioDecoderItTable[i] );
588            }
589            pC->m_pAudioDecoderItTable[i] = M4OSA_NULL;
590        }
591    }
592
593    pC->m_uiNbRegisteredVideoDec = 0;
594    pC->m_pVideoDecoder = M4OSA_NULL;
595
596    pC->m_pAudioDecoder = M4OSA_NULL;
597
598    return M4NO_ERROR;
599}
600
601/**
602 ************************************************************************
603 * M4OSA_ERR   M4MCS_setCurrentWriter()
604 * @brief    Set current writer
605 * @param    pContext            (IN/OUT) MCS context.
606 * @param    mediaType            (IN) Media type.
607 * @return    M4NO_ERROR:            No error
608 * @return    M4ERR_PARAMETER:                    A parameter is null (in DEBUG only)
609 * @return    M4WAR_MCS_MEDIATYPE_NOT_SUPPORTED:    Media type not supported
610 ************************************************************************
611 */
612M4OSA_ERR   M4MCS_setCurrentWriter( M4MCS_Context pContext,
613                                    M4VIDEOEDITING_FileType mediaType)
614{
615    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
616    M4WRITER_OutputFileType writerType;
617
618    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, "invalid context pointer");
619
620    switch (mediaType)
621    {
622        case M4VIDEOEDITING_kFileType_3GPP:
623        case M4VIDEOEDITING_kFileType_MP4:
624        case M4VIDEOEDITING_kFileType_M4V:
625            writerType = M4WRITER_k3GPP;
626            break;
627        case M4VIDEOEDITING_kFileType_AMR:
628            writerType = M4WRITER_kAMR;
629            break;
630        case M4VIDEOEDITING_kFileType_MP3:
631            writerType = M4WRITER_kMP3;
632            break;
633        case M4VIDEOEDITING_kFileType_PCM:
634            pC->b_isRawWriter = M4OSA_TRUE;
635            writerType = M4WRITER_kPCM;
636            break;
637        default:
638            M4OSA_DEBUG_IF1(M4OSA_TRUE, M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED,
639                 "Writer type not supported");
640            return M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED;
641    }
642
643    pC->pWriterGlobalFcts = pC->WriterInterface[writerType].pGlobalFcts;
644    pC->pWriterDataFcts = pC->WriterInterface[writerType].pDataFcts;
645
646    if (pC->pWriterGlobalFcts == M4OSA_NULL || pC->pWriterDataFcts == M4OSA_NULL)
647    {
648        M4OSA_DEBUG_IF1(M4OSA_TRUE, M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED,
649             "Writer type not supported");
650        return M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED;
651    }
652
653    pC->pWriterDataFcts->pWriterContext = M4OSA_NULL;
654
655    return M4NO_ERROR;
656}
657
658/**
659 ************************************************************************
660 * M4OSA_ERR   M4MCS_setCurrentVideoEncoder()
661 * @brief    Set a video encoder
662 * @param    pContext            (IN/OUT) MCS context.
663 * @param    MediaType           (IN) Encoder type
664 * @return    M4NO_ERROR:            No error
665 * @return    M4ERR_PARAMETER:                    A parameter is null (in DEBUG only)
666 * @return    M4WAR_MCS_MEDIATYPE_NOT_SUPPORTED:    Media type not supported
667 ************************************************************************
668 */
669M4OSA_ERR   M4MCS_setCurrentVideoEncoder(
670                                M4MCS_Context pContext,
671                                M4VIDEOEDITING_VideoFormat mediaType)
672{
673    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
674    M4ENCODER_Format encoderType;
675
676    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, "invalid context pointer");
677
678    switch (mediaType)
679    {
680        case M4VIDEOEDITING_kH263:
681            encoderType = M4ENCODER_kH263;
682            break;
683        case M4VIDEOEDITING_kMPEG4:
684        case M4VIDEOEDITING_kMPEG4_EMP:
685            encoderType = M4ENCODER_kMPEG4;
686            break;
687        case M4VIDEOEDITING_kH264:
688#ifdef M4VSS_SUPPORT_ENCODER_AVC
689            encoderType = M4ENCODER_kH264;
690        break;
691#endif
692        default:
693            M4OSA_DEBUG_IF1(M4OSA_TRUE, M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED,
694                 "Video encoder type not supported");
695            return M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED;
696    }
697
698    pC->pVideoEncoderGlobalFcts = pC->pVideoEncoderInterface[encoderType];
699    pC->pCurrentVideoEncoderExternalAPI = pC->pVideoEncoderExternalAPITable[encoderType];
700    pC->pCurrentVideoEncoderUserData = pC->pVideoEncoderUserDataTable[encoderType];
701
702    if (pC->pVideoEncoderGlobalFcts == M4OSA_NULL)
703    {
704        M4OSA_DEBUG_IF1(M4OSA_TRUE, M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED,
705             "Video encoder type not supported");
706        return M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED;
707    }
708
709    return M4NO_ERROR;
710}
711
712/**
713 ************************************************************************
714 * M4OSA_ERR   M4MCS_setCurrentAudioEncoder()
715 * @brief    Set an audio encoder
716 * @param    context            (IN/OUT) MCS context.
717 * @param    MediaType        (IN) Encoder type
718 * @return    M4NO_ERROR:            No error
719 * @return    M4ERR_PARAMETER:    A parameter is null (in DEBUG only)
720 ************************************************************************
721 */
722M4OSA_ERR   M4MCS_setCurrentAudioEncoder(
723                                M4MCS_Context pContext,
724                                M4VIDEOEDITING_AudioFormat mediaType)
725{
726    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
727    M4ENCODER_AudioFormat encoderType;
728
729    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, "invalid context pointer");
730
731    switch (mediaType)
732    {
733        case M4VIDEOEDITING_kAMR_NB:
734            encoderType = M4ENCODER_kAMRNB;
735            break;
736        case M4VIDEOEDITING_kAAC:
737            encoderType = M4ENCODER_kAAC;
738            break;
739        case M4VIDEOEDITING_kMP3:
740            encoderType = M4ENCODER_kMP3;
741            break;
742//EVRC
743//        case M4VIDEOEDITING_kEVRC:
744//            encoderType = M4ENCODER_kEVRC;
745//            break;
746        default:
747            M4OSA_DEBUG_IF1(M4OSA_TRUE, M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED,
748                 "Audio encoder type not supported");
749            return M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED;
750    }
751
752    pC->pAudioEncoderGlobalFcts = pC->pAudioEncoderInterface[encoderType];
753    pC->pCurrentAudioEncoderUserData = pC->pAudioEncoderUserDataTable[encoderType];
754
755    if (pC->pAudioEncoderGlobalFcts == M4OSA_NULL)
756    {
757        M4OSA_DEBUG_IF1(M4OSA_TRUE, M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED,
758             "Audio encoder type not supported");
759        return M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED;
760    }
761
762    return M4NO_ERROR;
763}
764
765/**
766 ************************************************************************
767 * M4OSA_ERR   M4MCS_setCurrentReader()
768 * @brief    Set current reader
769 * @param    pContext            (IN/OUT) MCS context.
770 * @param    mediaType            (IN) Media type.
771 * @return    M4NO_ERROR:            No error
772 * @return    M4ERR_PARAMETER:                    A parameter is null (in DEBUG only)
773 * @return    M4WAR_MCS_MEDIATYPE_NOT_SUPPORTED:    Media type not supported
774 ************************************************************************
775 */
776M4OSA_ERR   M4MCS_setCurrentReader( M4MCS_Context pContext,
777                                    M4VIDEOEDITING_FileType mediaType)
778{
779    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
780    M4READER_MediaType readerType;
781
782    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, "invalid context pointer");
783
784    switch (mediaType)
785    {
786        case M4VIDEOEDITING_kFileType_3GPP:
787        case M4VIDEOEDITING_kFileType_MP4:
788        case M4VIDEOEDITING_kFileType_M4V:
789            readerType = M4READER_kMediaType3GPP;
790            break;
791        case M4VIDEOEDITING_kFileType_AMR:
792            readerType = M4READER_kMediaTypeAMR;
793            break;
794        case M4VIDEOEDITING_kFileType_MP3:
795            readerType = M4READER_kMediaTypeMP3;
796            break;
797        default:
798            M4OSA_DEBUG_IF1(M4OSA_TRUE, M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED,
799                 "Reader type not supported");
800            return M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED;
801    }
802
803    pC->m_pReader       = pC->m_pReaderGlobalItTable[readerType];
804    pC->m_pReaderDataIt = pC->m_pReaderDataItTable[readerType];
805
806    if (pC->m_pReader == M4OSA_NULL || pC->m_pReaderDataIt == M4OSA_NULL)
807    {
808        M4OSA_DEBUG_IF1(M4OSA_TRUE, M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED,
809             "Reader type not supported");
810        return M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED;
811    }
812    return M4NO_ERROR;
813}
814
815/**
816 ************************************************************************
817 * M4OSA_ERR   M4MCS_setCurrentVideoDecoder()
818 * @brief    Set a video decoder
819 * @param    pContext            (IN/OUT) MCS context.
820 * @param    decoderType        (IN) Decoder type
821 * @return    M4NO_ERROR:            No error
822 * @return    M4ERR_PARAMETER:                    A parameter is null (in DEBUG only)
823 * @return    M4WAR_MCS_MEDIATYPE_NOT_SUPPORTED:    Media type not supported
824 ************************************************************************
825 */
826M4OSA_ERR   M4MCS_setCurrentVideoDecoder(   M4MCS_Context pContext,
827                                            M4_StreamType mediaType)
828{
829    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
830    M4DECODER_VideoType decoderType;
831
832    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, "invalid context pointer");
833
834    switch (mediaType)
835    {
836        case M4DA_StreamTypeVideoMpeg4:
837        case M4DA_StreamTypeVideoH263:
838            decoderType = M4DECODER_kVideoTypeMPEG4;
839            break;
840        case M4DA_StreamTypeVideoMpeg4Avc:
841            decoderType = M4DECODER_kVideoTypeAVC;
842            break;
843        default:
844            M4OSA_DEBUG_IF1(M4OSA_TRUE, M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED,
845                 "Video decoder type not supported");
846            return M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED;
847    }
848
849    pC->m_pVideoDecoder = pC->m_pVideoDecoderItTable[decoderType];
850#ifdef M4VSS_ENABLE_EXTERNAL_DECODERS
851    pC->m_pCurrentVideoDecoderUserData =
852            pC->m_pVideoDecoderUserDataTable[decoderType];
853#endif /* M4VSS_ENABLE_EXTERNAL_DECODERS */
854
855    if (pC->m_pVideoDecoder == M4OSA_NULL)
856    {
857        M4OSA_DEBUG_IF1(M4OSA_TRUE, M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED,
858             "Video decoder type not supported");
859        return M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED;
860    }
861
862    return M4NO_ERROR;
863}
864
865/**
866 ************************************************************************
867 * M4OSA_ERR   M4MCS_setCurrentAudioDecoder()
868 * @brief    Set an audio decoder
869 * @param    context            (IN/OUT) MCS context.
870 * @param    decoderType        (IN) Decoder type
871 * @return    M4NO_ERROR:            No error
872 * @return    M4ERR_PARAMETER:    A parameter is null (in DEBUG only)
873 ************************************************************************
874 */
875M4OSA_ERR   M4MCS_setCurrentAudioDecoder(   M4MCS_Context pContext,
876                                            M4_StreamType mediaType)
877{
878    M4MCS_InternalContext* pC = (M4MCS_InternalContext*)pContext;
879    M4AD_Type decoderType;
880
881    M4OSA_DEBUG_IF1((M4OSA_NULL == pC), M4ERR_PARAMETER, "invalid context pointer");
882
883    switch (mediaType)
884    {
885        case M4DA_StreamTypeAudioAmrNarrowBand:
886            decoderType = M4AD_kTypeAMRNB;
887            break;
888        case M4DA_StreamTypeAudioAac:
889        case M4DA_StreamTypeAudioAacADTS:
890        case M4DA_StreamTypeAudioAacADIF:
891            decoderType = M4AD_kTypeAAC;
892            break;
893        case M4DA_StreamTypeAudioMp3:
894            decoderType = M4AD_kTypeMP3;
895            break;
896//EVRC
897//        case M4DA_StreamTypeAudioEvrc:
898//            decoderType = M4AD_kTypeEVRC;
899//            break;
900        default:
901            M4OSA_DEBUG_IF1(M4OSA_TRUE, M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED,
902                 "Audio decoder type not supported");
903            return M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED;
904    }
905
906    pC->m_pAudioDecoder = pC->m_pAudioDecoderItTable[decoderType];
907    pC->m_pCurrentAudioDecoderUserData =
908                    pC->m_pAudioDecoderUserDataTable[decoderType];
909
910    if (pC->m_pAudioDecoder == M4OSA_NULL)
911    {
912        M4OSA_DEBUG_IF1(M4OSA_TRUE, M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED,
913             "Audio decoder type not supported");
914        return M4MCS_WAR_MEDIATYPE_NOT_SUPPORTED;
915    }
916
917    return M4NO_ERROR;
918}
919
920