gst_plugins_good/gst/qtmux/properties.c
branchRCL_3
changeset 30 7e817e7e631c
parent 29 567bb019e3e3
equal deleted inserted replaced
29:567bb019e3e3 30:7e817e7e631c
     1 /* Quicktime muxer plugin for GStreamer
       
     2  * Copyright (C) 2008 Thiago Sousa Santos <thiagoss@embedded.ufcg.edu.br>
       
     3  *
       
     4  * This library is free software; you can redistribute it and/or
       
     5  * modify it under the terms of the GNU Library General Public
       
     6  * License as published by the Free Software Foundation; either
       
     7  * version 2 of the License, or (at your option) any later version.
       
     8  *
       
     9  * This library is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    12  * Library General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU Library General Public
       
    15  * License along with this library; if not, write to the
       
    16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    17  * Boston, MA 02111-1307, USA.
       
    18  */
       
    19 /*
       
    20  * Unless otherwise indicated, Source Code is licensed under MIT license.
       
    21  * See further explanation attached in License Statement (distributed in the file
       
    22  * LICENSE).
       
    23  *
       
    24  * Permission is hereby granted, free of charge, to any person obtaining a copy of
       
    25  * this software and associated documentation files (the "Software"), to deal in
       
    26  * the Software without restriction, including without limitation the rights to
       
    27  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
       
    28  * of the Software, and to permit persons to whom the Software is furnished to do
       
    29  * so, subject to the following conditions:
       
    30  *
       
    31  * The above copyright notice and this permission notice shall be included in all
       
    32  * copies or substantial portions of the Software.
       
    33  *
       
    34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
       
    35  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
       
    36  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
       
    37  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
       
    38  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
       
    39  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
       
    40  * SOFTWARE.
       
    41  */
       
    42 
       
    43 #include "properties.h"
       
    44 
       
    45 /* if needed, re-allocate buffer to ensure size bytes can be written into it
       
    46  * at offset */
       
    47 void
       
    48 prop_copy_ensure_buffer (guint8 ** buffer, guint64 * bsize, guint64 * offset,
       
    49     guint64 size)
       
    50 {
       
    51   if (buffer && *bsize - *offset < size) {
       
    52     *bsize += size + 10 * 1024;
       
    53     *buffer = g_realloc (*buffer, *bsize);
       
    54   }
       
    55 }
       
    56 
       
    57 static guint64
       
    58 copy_func (void *prop, guint size, guint8 ** buffer, guint64 * bsize,
       
    59     guint64 * offset)
       
    60 {
       
    61   if (buffer) {
       
    62     prop_copy_ensure_buffer (buffer, bsize, offset, size);
       
    63     memcpy (*buffer + *offset, prop, size);
       
    64   }
       
    65   *offset += size;
       
    66   return size;
       
    67 }
       
    68 
       
    69 #define INT_ARRAY_COPY_FUNC_FAST(name, datatype) 			\
       
    70 guint64 prop_copy_ ## name ## _array (datatype *prop, guint size,	\
       
    71     guint8 ** buffer, guint64 * bsize, guint64 * offset) { 		\
       
    72   return copy_func (prop, sizeof (datatype) * size, buffer, bsize, offset);\
       
    73 }
       
    74 
       
    75 #define INT_ARRAY_COPY_FUNC(name, datatype) 				\
       
    76 guint64 prop_copy_ ## name ## _array (datatype *prop, guint size,	\
       
    77     guint8 ** buffer, guint64 * bsize, guint64 * offset) { 		\
       
    78   guint i;								\
       
    79 									\
       
    80   for (i = 0; i < size; i++) {						\
       
    81     prop_copy_ ## name (prop[i], buffer, bsize, offset);		\
       
    82   }									\
       
    83   return sizeof (datatype) * size;					\
       
    84 }
       
    85 
       
    86 /* INTEGERS */
       
    87 guint64
       
    88 prop_copy_uint8 (guint8 prop, guint8 ** buffer, guint64 * size,
       
    89     guint64 * offset)
       
    90 {
       
    91   return copy_func (&prop, sizeof (guint8), buffer, size, offset);
       
    92 }
       
    93 
       
    94 guint64
       
    95 prop_copy_uint16 (guint16 prop, guint8 ** buffer, guint64 * size,
       
    96     guint64 * offset)
       
    97 {
       
    98   prop = GUINT16_TO_BE (prop);
       
    99   return copy_func (&prop, sizeof (guint16), buffer, size, offset);
       
   100 }
       
   101 
       
   102 guint64
       
   103 prop_copy_uint32 (guint32 prop, guint8 ** buffer, guint64 * size,
       
   104     guint64 * offset)
       
   105 {
       
   106   prop = GUINT32_TO_BE (prop);
       
   107   return copy_func (&prop, sizeof (guint32), buffer, size, offset);
       
   108 }
       
   109 
       
   110 guint64
       
   111 prop_copy_uint64 (guint64 prop, guint8 ** buffer, guint64 * size,
       
   112     guint64 * offset)
       
   113 {
       
   114   prop = GUINT64_TO_BE (prop);
       
   115   return copy_func (&prop, sizeof (guint64), buffer, size, offset);
       
   116 }
       
   117 
       
   118 guint64
       
   119 prop_copy_int32 (gint32 prop, guint8 ** buffer, guint64 * size,
       
   120     guint64 * offset)
       
   121 {
       
   122   prop = GINT32_TO_BE (prop);
       
   123   return copy_func (&prop, sizeof (guint32), buffer, size, offset);
       
   124 }
       
   125 
       
   126 /* uint8 can use direct copy in any case, and may be used for large quantity */
       
   127 INT_ARRAY_COPY_FUNC_FAST (uint8, guint8);
       
   128 /* not used in large quantity anyway */
       
   129 INT_ARRAY_COPY_FUNC (uint16, guint16);
       
   130 INT_ARRAY_COPY_FUNC (uint32, guint32);
       
   131 INT_ARRAY_COPY_FUNC (uint64, guint64);
       
   132 
       
   133 /* FOURCC */
       
   134 guint64
       
   135 prop_copy_fourcc (guint32 prop, guint8 ** buffer, guint64 * size,
       
   136     guint64 * offset)
       
   137 {
       
   138   prop = GINT32_TO_LE (prop);
       
   139   return copy_func (&prop, sizeof (guint32), buffer, size, offset);
       
   140 }
       
   141 
       
   142 INT_ARRAY_COPY_FUNC (fourcc, guint32);
       
   143 
       
   144 /**
       
   145  * Copies a string of bytes without placing its size at the beginning.
       
   146  *
       
   147  * @string: the string to be copied
       
   148  * @str_size: size of the string
       
   149  * @buffer: the array to copy the string to
       
   150  * @offset: the position in the buffer array.
       
   151  * This value is updated to the point right after the copied string.
       
   152  *
       
   153  * Returns: the number of bytes copied
       
   154  */
       
   155 guint64
       
   156 prop_copy_fixed_size_string (guint8 * string, guint str_size, guint8 ** buffer,
       
   157     guint64 * size, guint64 * offset)
       
   158 {
       
   159   return copy_func (string, str_size * sizeof (guint8), buffer, size, offset);
       
   160 }
       
   161 
       
   162 /**
       
   163  * Copies a string and its size to an array. Example:
       
   164  * string = 'abc\0'
       
   165  * result in the array: [3][a][b][c]  (each [x] represents a position)
       
   166  *
       
   167  * @string: the string to be copied
       
   168  * @str_size: size of the string
       
   169  * @buffer: the array to copy the string to
       
   170  * @offset: the position in the buffer array.
       
   171  * This value is updated to the point right after the copied string.
       
   172  *
       
   173  * Returns: the number of bytes copied
       
   174  */
       
   175 guint64
       
   176 prop_copy_size_string (guint8 * string, guint str_size, guint8 ** buffer,
       
   177     guint64 * size, guint64 * offset)
       
   178 {
       
   179   guint64 original_offset = *offset;
       
   180 
       
   181   prop_copy_uint8 (str_size, buffer, size, offset);
       
   182   prop_copy_fixed_size_string (string, str_size, buffer, size, offset);
       
   183   return *offset - original_offset;
       
   184 }
       
   185 
       
   186 /**
       
   187  * Copies a string including its null terminating char to an array.
       
   188  *
       
   189  * @string: the string to be copied
       
   190  * @buffer: the array to copy the string to
       
   191  * @offset: the position in the buffer array.
       
   192  * This value is updated to the point right after the copied string.
       
   193  *
       
   194  * Returns: the number of bytes copied
       
   195  */
       
   196 guint64
       
   197 prop_copy_null_terminated_string (gchar * string, guint8 ** buffer,
       
   198     guint64 * size, guint64 * offset)
       
   199 {
       
   200   guint64 original_offset = *offset;
       
   201   guint len = strlen (string);
       
   202 
       
   203   prop_copy_fixed_size_string ((guint8 *) string, len, buffer, size, offset);
       
   204   prop_copy_uint8 ('\0', buffer, size, offset);
       
   205   return *offset - original_offset;
       
   206 }