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