summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/osal/src/M4OSA_FileWriter_RAM.c
blob: 4a8ff6fc3a6a1c332389b62c2ac1f61c7cc49ddd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 * Copyright (C) 2004-2011 NXP Software
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 ******************************************************************************
 * @file         M4OSA_FileReaderRam.c

 * @brief        File reader from RAM
 * @note         This file implements functions to read a "file" stored in RAM.
******************************************************************************
*/

#include "M4OSA_Debug.h"
#include "M4OSA_FileWriterRam.h"
#include "M4OSA_FileReaderRam.h"
#include "M4OSA_Memory.h"


/**
 ******************************************************************************
 * structure    M4OSA_FileWriteRam_Context
 * @brief        This structure defines the File writer context (private)
 * @note        This structure is used for all File writer calls to store the context
 ******************************************************************************
*/
typedef struct
{
    M4OSA_MemAddr8    pFileDesc;    /* Pointer on file data */
    M4OSA_UInt32    dataSize;    /* Size of data to write */
    M4OSA_UInt32    dataOffset;    /* Actual offset */
    M4OSA_Bool        IsOpened;    /* Micro state machine */
    M4OSA_UInt32    bufferSize;    /* Actual used size inside the buffer */
} M4OSA_FileWriterRam_Context;


/**
 ******************************************************************************
 * @brief    This method "opens" the provided fileDescriptor (in fact address)
 *            and returns its context.
 * @param    pContext:    (OUT) File writer context.
 * @param    pFileDescriptor :    (IN) File Descriptor of the input file.
 * @param    FileModeAccess :    (IN) File mode access.
 * @return    M4NO_ERROR: there is no error
 * @return    M4ERR_PARAMETER    pContext or fileDescriptor is NULL
 * @return    M4ERR_ALLOC    there is no more memory available
 * @return    M4ERR_FILE_BAD_MODE_ACCESS    the file mode access is not correct
 * @return    M4ERR_FILE_NOT_FOUND The file can not be opened.
 ******************************************************************************
*/
M4OSA_ERR M4OSA_fileWriteRamOpen(M4OSA_Context* context, M4OSA_Void* fileDescriptor,
                                                    M4OSA_UInt32 fileModeAccess)
{
    M4OSA_FileWriterRam_Context* pContext=M4OSA_NULL;
    M4OSA_FileWriterRam_Descriptor* pDescriptor = fileDescriptor;
    M4OSA_Int32 aFileDesc=-1;
    M4OSA_ERR   err=M4NO_ERROR;

    /*    Check input parameters */
    if(context == M4OSA_NULL)
    {
        return M4ERR_PARAMETER;
    }
    *context = M4OSA_NULL;
    if(fileDescriptor == M4OSA_NULL)
    {
        return M4ERR_PARAMETER;
    }

    /*    Allocate memory for the File writer context. */
    pContext = (M4OSA_FileWriterRam_Context *)M4OSA_32bitAlignedMalloc(sizeof(M4OSA_FileWriterRam_Context),
                          M4OSA_FILE_WRITER, (M4OSA_Char*)"Context allocation");
    if(pContext == M4OSA_NULL)
    {
        return M4ERR_ALLOC;
    }

    if ((fileModeAccess & M4OSA_kFileWrite))
    {
        pContext->pFileDesc = (M4OSA_MemAddr8)(pDescriptor->pFileDesc);
        pContext->dataSize = (M4OSA_Int32)(pDescriptor->dataSize);
        pContext->dataOffset = 0;
        pContext->bufferSize = 0;
        pContext->IsOpened = M4OSA_TRUE;
    }
    else
    {
        err = M4ERR_FILE_BAD_MODE_ACCESS;
    }
    if (M4NO_ERROR != err)
    {
          if (M4OSA_NULL != pContext)
        {
            free(pContext);
        }
        *context=M4OSA_NULL;
    }
    else
    {
        *context = pContext;
    }

    return err;
}

/**
 ******************************************************************************
 * @brief    This method writes the 'size' bytes stored at 'data' memory at the end
 *            of the file selected by its context.
 *            The caller is responsible for allocating/de-allocating the memory for 'data' parameter.
 *            Moreover, the data pointer must be allocated to store at least 'size' bytes.
 * @param    pContext:    (IN) File writer context.
 * @param    pData :    (IN) Data pointer of the written data.
 * @param    Size :    (IN) Size of the data to write (in bytes).
 * @return    M4NO_ERROR: there is no error
 * @return    M4ERR_PARAMETER     pData is NULL
 * @return    M4ERR_ALLOC    there is no more memory available
 * @return    M4ERR_BAD_CONTEXT    provided context is not a valid one.
 ******************************************************************************
*/
M4OSA_ERR M4OSA_fileWriteRamData(M4OSA_Context context,M4OSA_MemAddr8 data, M4OSA_UInt32 Size)
{
    M4OSA_FileWriterRam_Context* pContext=(M4OSA_FileWriterRam_Context*)context;
    M4OSA_ERR err=M4NO_ERROR;

    /*    Check input parameters */
    if(context == M4OSA_NULL || data == M4OSA_NULL)
    {
        return M4ERR_PARAMETER;
    }

    if (pContext->IsOpened != M4OSA_TRUE)
    {
        return M4ERR_BAD_CONTEXT;    /* The context can not be correct */
    }

    /* Check if there is enough room to write or not */
    if (pContext->dataOffset + Size < pContext->dataSize )
    {
        M4OSA_memcpy((pContext->pFileDesc + pContext->dataOffset), data, Size);
        pContext->dataOffset += Size;
        if(pContext->dataOffset> pContext->bufferSize) pContext->bufferSize = pContext->dataOffset;
        err = M4NO_ERROR;
    }
    else
    {
        err = M4ERR_FILE_INVALID_POSITION;
    }

    return err;
}

/**
 ******************************************************************************
 * @brief    This method seeks at the provided position in the core file writer (selected by its 'context').
 *            The position is related to the seekMode parameter it can be either :
 *                From the beginning (position MUST be positive) : end position = position
 *                From the end (position MUST be negative) : end position = file size + position
 *                From the current position (signed offset) : end position = current position + position.
 * @param    pContext:    (IN) File reader context.
 * @param    SeekMode :    (IN) Seek access mode.
 * @param    pPosition :    (IN) Position in the file.
 * @return    M4NO_ERROR: there is no error
 * @return    M4ERR_PARAMETER    Seekmode or fileDescriptor is NULL
 * @return    M4ERR_ALLOC    there is no more memory available
 * @return    M4ERR_BAD_CONTEXT    provided context is not a valid one.
 * @return    M4ERR_FILE_INVALID_POSITION the position cannot be reached.
 ******************************************************************************
*/
M4OSA_ERR M4OSA_fileWriteRamSeek(M4OSA_Context context, M4OSA_FileSeekAccessMode SeekMode,
                                                   M4OSA_FilePosition* position)
{
    M4OSA_FileWriterRam_Context* pContext=(M4OSA_FileWriterRam_Context*)context;
    M4OSA_ERR err=M4NO_ERROR;

    /*    Check input parameters */
    if(context == M4OSA_NULL || SeekMode == M4OSA_NULL || position == M4OSA_NULL)
    {
        return M4ERR_PARAMETER;
    }

    if (pContext->IsOpened != M4OSA_TRUE)
    {
        return M4ERR_BAD_CONTEXT;    /* The context can not be correct */
    }

    /* Go to the desired position */
    switch(SeekMode)
    {
        case M4OSA_kFileSeekBeginning:
            /* Check if position is reachable and update dataOffset */
            if (((*position) >= 0) && ((M4OSA_UInt32)(*position) <= pContext->dataSize))
            {
                pContext->dataOffset = *position;
                err = M4NO_ERROR;
            }
            else
            {
                err = M4ERR_FILE_INVALID_POSITION;
            }
            break;

        case M4OSA_kFileSeekEnd:
            /* Check if position is reachable and update dataOffset */
            if ((*position) < 0)
            {
                if (pContext->dataSize >= (M4OSA_UInt32)(-(*position)))
                {
                    pContext->dataOffset = (M4OSA_UInt32) (pContext->pFileDesc + pContext->dataSize + (*position));
                    err = M4NO_ERROR;
                }
                else
                {
                    err = M4ERR_FILE_INVALID_POSITION;
                }
            }
            else if ((*position) == 0)
            {
                 pContext->dataOffset = (M4OSA_UInt32)(pContext->pFileDesc + pContext->dataSize);
                 err = M4NO_ERROR;
            }
            else
            {
                err = M4ERR_FILE_INVALID_POSITION;
            }
            break;

        case M4OSA_kFileSeekCurrent:
            /* Check if position is reachable and update dataOffset */
            if ((*position) < 0)
            {
                if (pContext->dataOffset >= (M4OSA_UInt32)(-(*position)))
                {
                    pContext->dataOffset = (M4OSA_UInt32) (pContext->dataOffset + (*position));
                    err = M4NO_ERROR;
                }
                else
                {
                    err = M4ERR_FILE_INVALID_POSITION;
                }
            }
            else
            {
                if (pContext->dataSize >= (M4OSA_UInt32)(pContext->dataOffset + (*position)))
                {
                    pContext->dataOffset = (M4OSA_UInt32) (pContext->dataOffset + (*position));
                    err = M4NO_ERROR;
                }
                else
                {
                    err = M4ERR_FILE_INVALID_POSITION;
                }
            }
            break;

        default:
            err = M4ERR_PARAMETER;
            break;
    }

    return err;
}

/**
 ******************************************************************************
 * @brief    This method asks the core file writer to close the file (associated to the context).
 *            The context of the core file reader must be freed.
 * @param    pContext:    (IN) File reader context.
 * @return    M4NO_ERROR: there is no error
 * @return    M4ERR_BAD_CONTEXT    provided context is not a valid one.
 ******************************************************************************
*/
M4OSA_ERR M4OSA_fileWriteRamClose(M4OSA_Context context)
{
    M4OSA_FileWriterRam_Context* pContext=(M4OSA_FileWriterRam_Context*)context;
    M4OSA_ERR err=M4NO_ERROR;

    /*    Check input parameters */
    if(pContext == M4OSA_NULL)
    {
        return M4ERR_PARAMETER;
    }

    if (pContext->IsOpened != M4OSA_TRUE)
    {
        return M4ERR_BAD_CONTEXT;    /* The context can not be correct */
    }

    pContext->IsOpened = M4OSA_FALSE;

    /* Free the context */
    free(pContext);

    /*    Return error */
    return err;
}

/**
 ******************************************************************************
 * @brief    This method asks the core file writer to flush the pending data
 *            to the file (associated to the context).
 *            All pending written data are written in the file.
 * @param    pContext:    (IN) File reader context.
 * @return    M4NO_ERROR: there is no error
 * @return    M4ERR_BAD_CONTEXT    provided context is not a valid one.
 ******************************************************************************
*/
M4OSA_ERR M4OSA_fileWriteRamFlush(M4OSA_Context context)
{
    M4OSA_FileWriterRam_Context* pContext=(M4OSA_FileWriterRam_Context*)context;

    /*    Check input parameters */
    if(context == M4OSA_NULL)
    {
        return M4ERR_PARAMETER;
    }

    if (pContext->IsOpened != M4OSA_TRUE)
    {
        return M4ERR_BAD_CONTEXT;    /* The context can not be correct */
    }

    /*
     * DO NOTHING */

    /**
     *    Return without error */
    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * @brief    This method asks the core file writer to set the value associated with the optionID.
 *            The caller is responsible for allocating/de-allocating the memory of the value field.
 * @note    The options handled by the component depend on the implementation of the component.
 * @param    pContext:    (IN) Execution context.
 * @param    OptionId :    (IN) Id of the option to set.
 * @param    OptionValue :    (IN) Value of the option.
 * @return    M4NO_ERROR: there is no error
 * @return    M4ERR_BAD_CONTEXT    pContext is NULL
 * @return    M4ERR_READ_ONLY The option is not implemented yet.
 * @return    M4ERR_BAD_OPTION_ID the option id is not valid.
 * @return    M4ERR_NOT_IMPLEMENTED The option is not implemented yet.
 ******************************************************************************
*/
M4OSA_ERR M4OSA_fileWriteRamSetOption(M4OSA_Context context,
                                      M4OSA_OptionID OptionID,
                                      M4OSA_DataOption OptionValue)
{
    M4OSA_FileWriterRam_Context* pContext=(M4OSA_FileWriterRam_Context*)context;

    /*    Check input parameters */
    if(context == M4OSA_NULL)
    {
        return M4ERR_PARAMETER;
    }

    if (pContext->IsOpened != M4OSA_TRUE)
    {
        return M4ERR_BAD_CONTEXT;    /**< The context can not be correct */
    }

    /*    Set the desired option if it is avalaible */
    switch(OptionID)
    {
        case M4OSA_kFileWriteGetReaderContext :    /* Get the file attribute*/
        case M4OSA_kFileWriteGetURL :            /* Get the directory + name of the file */
        case M4OSA_kFileWriteGetFilePosition :    /* Get file position */
            return M4ERR_READ_ONLY;
            break;

        case M4OSA_kFileWriteGetAttribute :
            /**
             * Get the reader context for read & write file. It is NULL if the file is opened
             * with write attribute only */
            return M4ERR_NOT_IMPLEMENTED;

        default :                                /* Bad option ID */
            return    M4ERR_BAD_OPTION_ID;
    }

    /*    Return without error */
    return M4NO_ERROR;
}

/**
 ******************************************************************************
 * @brief    This method asks the core file reader to return the value associated with the optionID.
 *            The caller is responsible for allocating/de-allocating the memory of the value field.
 * @note    The options handled by the component depend on the implementation of the component.
 * @param    pContext:    (IN) Execution context.
 * @param    OptionId :    (IN) Id of the option to set.
 * @param    pOptionValue :    (OUT) Value of the option.
 * @return    M4NO_ERROR: there is no error
 * @return    M4ERR_BAD_CONTEXT    pContext is NULL
 * @return    M4ERR_BAD_OPTION_ID the option id is not valid.
 * @return    M4ERR_ALLOC    there is no more memory available
 * @return    M4ERR_NOT_IMPLEMENTED The option is not implemented yet.
 ******************************************************************************
*/
M4OSA_ERR M4OSA_fileWriteRamGetOption(M4OSA_Context context,
                                      M4OSA_OptionID OptionID,
                                      M4OSA_DataOption* optionValue)
{
    M4OSA_FileWriterRam_Context* pContext=(M4OSA_FileWriterRam_Context*)context;
    M4OSA_ERR   err=M4NO_ERROR;

    /*    Check input parameters */
    if(context == M4OSA_NULL)
    {
        return M4ERR_PARAMETER;
    }

    if (pContext->IsOpened != M4OSA_TRUE)
    {
        return M4ERR_BAD_CONTEXT;    /**< The context can not be correct */
    }

    /*    Get the desired option if it is avalaible */
    switch(OptionID)
    {
        case M4OSA_kFileWriteGetFileSize:/* Get size of the file, limited to 32 bit size */
            (*(M4OSA_UInt32 *)optionValue) = (pContext->bufferSize);
            break;

        case M4OSA_kFileWriteGetURL :    /* Get the directory + name of the file */
            return M4ERR_NOT_IMPLEMENTED;

        default :                                /**< Bad option ID */
            return M4ERR_BAD_OPTION_ID;
            break;
    }

    /*    Return without error */
    return err;
}