os/mm/mmplugins/lib3gp/impl/src/buffer.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include "mp4atom.h"
sl@0
    17
#include <3gplibrary/mp4config.h>
sl@0
    18
#include "mp4buffer.h"
sl@0
    19
#include "mp4memwrap.h"
sl@0
    20
#include "mp4file.h"
sl@0
    21
#include "mp4list.h"
sl@0
    22
sl@0
    23
sl@0
    24
sl@0
    25
/*
sl@0
    26
 * Function:
sl@0
    27
 *
sl@0
    28
 *   mp4_i32 addData(MP4HandleImp handle,
sl@0
    29
 *                   mp4_u8 *buffer,
sl@0
    30
 *                   mp4_u32 bytestowrite)
sl@0
    31
 *
sl@0
    32
 * Description:
sl@0
    33
 *
sl@0
    34
 *   This function allocates memory for the data and copies it from
sl@0
    35
 *   buffer to the new allocated buffer.
sl@0
    36
 *
sl@0
    37
 * Parameters:
sl@0
    38
 *
sl@0
    39
 *   handle        MP4 library handle
sl@0
    40
 *   buffer        Buffer containing data
sl@0
    41
 *   bytestowrite  Size of buffer in bytes
sl@0
    42
 *
sl@0
    43
 * Return value:
sl@0
    44
 *
sl@0
    45
 *   Negative      Error
sl@0
    46
 *   0             Success
sl@0
    47
 *
sl@0
    48
 */
sl@0
    49
mp4_i32 addData(MP4HandleImp handle, mp4_u8 *buffer, mp4_u32 bytestowrite)
sl@0
    50
{
sl@0
    51
  void *newBuffer;
sl@0
    52
sl@0
    53
  newBuffer = mp4malloc(bytestowrite);
sl@0
    54
  if (newBuffer == NULL)
sl@0
    55
    return -1;
sl@0
    56
sl@0
    57
  mp4memcpy(newBuffer, buffer, bytestowrite);
sl@0
    58
sl@0
    59
  if (listAppend(handle->mem, newBuffer, bytestowrite)) /* Success */
sl@0
    60
    return 0;
sl@0
    61
  else
sl@0
    62
	  {
sl@0
    63
	  if (newBuffer)
sl@0
    64
		  {
sl@0
    65
		  delete newBuffer;
sl@0
    66
		  newBuffer = NULL;
sl@0
    67
		  }
sl@0
    68
	  return -1;
sl@0
    69
	  }
sl@0
    70
}
sl@0
    71
sl@0
    72
sl@0
    73
/*
sl@0
    74
 * Function:
sl@0
    75
 *
sl@0
    76
 *   mp4_u32 getBufferedBytes(MP4HandleImp handle)
sl@0
    77
 *
sl@0
    78
 * Description:
sl@0
    79
 *
sl@0
    80
 *   This function returns the number of bytes in the library internal
sl@0
    81
 *   buffers.
sl@0
    82
 *
sl@0
    83
 * Parameters:
sl@0
    84
 *
sl@0
    85
 *   handle   MP4 library handle
sl@0
    86
 *
sl@0
    87
 * Return value:
sl@0
    88
 *
sl@0
    89
 *   0   The input is in a file and therefore no memory is used to store MP4
sl@0
    90
 *       data or no memory in buffers.
sl@0
    91
 *   >0  Number of bytes stored in the library internal buffers
sl@0
    92
 *
sl@0
    93
 */
sl@0
    94
mp4_u32 getBufferedBytes(MP4HandleImp handle)
sl@0
    95
{
sl@0
    96
  if (handle->file)
sl@0
    97
    return 0;
sl@0
    98
sl@0
    99
  return listBytesInList(handle->mem);
sl@0
   100
}
sl@0
   101
sl@0
   102
sl@0
   103
/*
sl@0
   104
 * Function:
sl@0
   105
 *
sl@0
   106
 *   mp4_u32 getCumulativeBufferedBytes(MP4HandleImp handle)
sl@0
   107
 *
sl@0
   108
 * Description:
sl@0
   109
 *
sl@0
   110
 *   This function returns the number of bytes passed through the library
sl@0
   111
 *   internal buffers.
sl@0
   112
 *
sl@0
   113
 * Parameters:
sl@0
   114
 *
sl@0
   115
 *   handle   MP4 library handle
sl@0
   116
 *
sl@0
   117
 * Return value:
sl@0
   118
 *
sl@0
   119
 *   0   The input is in a file and therefore no memory is used to store MP4
sl@0
   120
 *       data or no memory in buffers.
sl@0
   121
 *   >0  Number of bytes stored in the library internal buffers
sl@0
   122
 *
sl@0
   123
 */
sl@0
   124
mp4_u32 getCumulativeBufferedBytes(MP4HandleImp handle)
sl@0
   125
{
sl@0
   126
  if (handle->file)
sl@0
   127
    return 0;
sl@0
   128
sl@0
   129
  return listCumulativeBytesInList(handle->mem);
sl@0
   130
}
sl@0
   131
sl@0
   132
sl@0
   133
/*
sl@0
   134
 * Function:
sl@0
   135
 *
sl@0
   136
 *   mp4_i32 readData(MP4HandleImp handle,
sl@0
   137
 *                    mp4_u8 *buffer,
sl@0
   138
 *                    mp4_u32 bytestoread)
sl@0
   139
 *
sl@0
   140
 * Description:
sl@0
   141
 *
sl@0
   142
 *   This function reads bytestoread bytes from memory buffers or file
sl@0
   143
 *   to buffer.
sl@0
   144
 *
sl@0
   145
 * Parameters:
sl@0
   146
 *
sl@0
   147
 *   handle       MP4 library handle
sl@0
   148
 *   buffer       Caller allocated buffer for the data
sl@0
   149
 *   bytestoread  Number of bytes to read
sl@0
   150
 *
sl@0
   151
 * Return value:
sl@0
   152
 *
sl@0
   153
 *   >= 0         Success. Value tells the number of bytes read.
sl@0
   154
 *   -1           File has not been opened
sl@0
   155
 *   -2           End of file or file error
sl@0
   156
 *   -10          Not enough data in memory
sl@0
   157
 *
sl@0
   158
 */
sl@0
   159
mp4_i32 readData(MP4HandleImp handle, mp4_u8 *buffer, mp4_u32 bytestoread)
sl@0
   160
{
sl@0
   161
  if (handle->file) /* Input is in a file */
sl@0
   162
  {
sl@0
   163
    switch (readFile(handle, buffer, bytestoread))
sl@0
   164
    {
sl@0
   165
    case -2: /* EOF or error */
sl@0
   166
      return -2;
sl@0
   167
    case -1: /* File not open */
sl@0
   168
      return -1;
sl@0
   169
    case 0: /* Ok */
sl@0
   170
      return bytestoread;
sl@0
   171
    default:
sl@0
   172
      break;
sl@0
   173
    }
sl@0
   174
  }
sl@0
   175
  else /* Input is in memory list */
sl@0
   176
  {
sl@0
   177
    mp4_u32 i, j;
sl@0
   178
    node_s *node;
sl@0
   179
sl@0
   180
    if (handle->mem->bytesInList - handle->absPosition < bytestoread)
sl@0
   181
      return -10;
sl@0
   182
sl@0
   183
    i = 0;
sl@0
   184
    j = handle->absPosition;
sl@0
   185
sl@0
   186
    node = handle->mem->first;
sl@0
   187
sl@0
   188
    while (i < bytestoread)
sl@0
   189
    {
sl@0
   190
      if ((mp4_i32)(node->dataSize - j) <= 0)
sl@0
   191
      {
sl@0
   192
        j -= node->dataSize;
sl@0
   193
        node = node->next;
sl@0
   194
        continue;
sl@0
   195
      }
sl@0
   196
sl@0
   197
      {
sl@0
   198
        mp4_u32 k;
sl@0
   199
sl@0
   200
        k = node->dataSize - j >= bytestoread - i ? bytestoread - i : node->dataSize - j;
sl@0
   201
sl@0
   202
        mp4memcpy(buffer + i, ((mp4_u8 *)node->data) + j, k);
sl@0
   203
        i += k;
sl@0
   204
        j += k;
sl@0
   205
      }
sl@0
   206
    }
sl@0
   207
sl@0
   208
    handle->position = j;
sl@0
   209
    handle->absPosition += bytestoread;
sl@0
   210
sl@0
   211
    node = handle->mem->first;
sl@0
   212
  }
sl@0
   213
sl@0
   214
  return bytestoread;
sl@0
   215
}
sl@0
   216
sl@0
   217
/*
sl@0
   218
 * Function:
sl@0
   219
 *
sl@0
   220
 *   mp4_i32 peekData(MP4HandleImp handle,
sl@0
   221
 *                    mp4_u8 *buffer,
sl@0
   222
 *                    mp4_u32 bytestoread)
sl@0
   223
 *
sl@0
   224
 * Description:
sl@0
   225
 *
sl@0
   226
 *   This function reads bytestoread bytes from memory buffers or file
sl@0
   227
 *   to buffer but doesn't change the internal position in the file/stream.
sl@0
   228
 *
sl@0
   229
 * Parameters:
sl@0
   230
 *
sl@0
   231
 *   handle       MP4 library handle
sl@0
   232
 *   buffer       Caller allocated buffer for the data
sl@0
   233
 *   bytestoread  Number of bytes to read
sl@0
   234
 *
sl@0
   235
 * Return value:
sl@0
   236
 *
sl@0
   237
 *   >= 0         Success. Value tells the number of bytes read.
sl@0
   238
 *   -1           File has not been opened
sl@0
   239
 *   -2           End of file or file error
sl@0
   240
 *   -3           fseek failed
sl@0
   241
 *   -10          Not enough data in memory
sl@0
   242
 *
sl@0
   243
 */
sl@0
   244
mp4_i32 peekData(MP4HandleImp handle, mp4_u8 *buffer, mp4_u32 bytestoread)
sl@0
   245
{
sl@0
   246
  if (handle->file) /* Input is in a file */
sl@0
   247
  {
sl@0
   248
    switch (peekFile(handle, buffer, bytestoread))
sl@0
   249
    {
sl@0
   250
    case -3: /* fseek failed */
sl@0
   251
      return -3;
sl@0
   252
    case -2: /* EOF or error */
sl@0
   253
      return -2;
sl@0
   254
    case -1: /* File not open */
sl@0
   255
      return -1;
sl@0
   256
    case 0: /* Ok */
sl@0
   257
      return bytestoread;
sl@0
   258
    default:
sl@0
   259
      break;
sl@0
   260
    }
sl@0
   261
  }
sl@0
   262
  else /* Input is in memory list */
sl@0
   263
  {
sl@0
   264
    mp4_u32 i, j;
sl@0
   265
    node_s *node;
sl@0
   266
sl@0
   267
    if ((mp4_i32)(handle->mem->bytesInList - handle->absPosition) < (mp4_i32)bytestoread)
sl@0
   268
      return -10;
sl@0
   269
sl@0
   270
    i = 0;
sl@0
   271
    j = handle->absPosition;
sl@0
   272
sl@0
   273
    node = handle->mem->first;
sl@0
   274
sl@0
   275
    while (i < bytestoread)
sl@0
   276
    {
sl@0
   277
      if ((mp4_i32)(node->dataSize - j) <= 0)
sl@0
   278
      {
sl@0
   279
        j -= node->dataSize;
sl@0
   280
        node = node->next;
sl@0
   281
        continue;
sl@0
   282
      }
sl@0
   283
sl@0
   284
      {
sl@0
   285
        mp4_u32 k;
sl@0
   286
sl@0
   287
        k = node->dataSize - j >= bytestoread - i ? bytestoread - i : node->dataSize - j;
sl@0
   288
sl@0
   289
        mp4memcpy(buffer + i, ((mp4_u8 *)node->data) + j, k);
sl@0
   290
        i += k;
sl@0
   291
        j += k;
sl@0
   292
      }
sl@0
   293
    }
sl@0
   294
  }
sl@0
   295
sl@0
   296
  return bytestoread;
sl@0
   297
}
sl@0
   298
sl@0
   299
sl@0
   300
/*
sl@0
   301
 * Function:
sl@0
   302
 *
sl@0
   303
 *   mp4_i32 discardData(MP4HandleImp handle,
sl@0
   304
 *                       mp4_i32 bytesToDiscard)
sl@0
   305
 *
sl@0
   306
 * Description:
sl@0
   307
 *
sl@0
   308
 *   This function reads and discards bytesToDiscard bytes from file/stream.
sl@0
   309
 *
sl@0
   310
 * Parameters:
sl@0
   311
 *
sl@0
   312
 *   handle             MP4 library handle
sl@0
   313
 *   bytesToDiscard     This many bytes are discarded
sl@0
   314
 *
sl@0
   315
 * Return value:
sl@0
   316
 *
sl@0
   317
 *   Negative integer   Error
sl@0
   318
 *   >= 0               Success. Value tells how many bytes were read.
sl@0
   319
 *
sl@0
   320
 */
sl@0
   321
mp4_i32 discardData(MP4HandleImp handle, mp4_i32 bytesToDiscard)
sl@0
   322
{
sl@0
   323
  mp4_i32 bytesread;
sl@0
   324
  mp4_i32 bytestoread;
sl@0
   325
  mp4_i32 totalbytesread = 0;
sl@0
   326
sl@0
   327
  while (totalbytesread < bytesToDiscard)
sl@0
   328
  {
sl@0
   329
    bytestoread = bytesToDiscard - totalbytesread;
sl@0
   330
    if (bytestoread > TMPBUFSIZE)
sl@0
   331
      bytestoread = TMPBUFSIZE;
sl@0
   332
sl@0
   333
    bytesread = readData(handle, handle->buf, bytestoread);
sl@0
   334
    if (bytesread < 0)
sl@0
   335
      return -1;
sl@0
   336
    totalbytesread += bytesread;
sl@0
   337
  }
sl@0
   338
sl@0
   339
  return totalbytesread;
sl@0
   340
}
sl@0
   341
// End of File