You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
6.9KB

  1. /*
  2. * AVPacket functions for libavcodec
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <string.h>
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/mem.h"
  24. #include "avcodec.h"
  25. void av_destruct_packet(AVPacket *pkt)
  26. {
  27. int i;
  28. av_free(pkt->data);
  29. pkt->data = NULL;
  30. pkt->size = 0;
  31. for (i = 0; i < pkt->side_data_elems; i++)
  32. av_free(pkt->side_data[i].data);
  33. av_freep(&pkt->side_data);
  34. pkt->side_data_elems = 0;
  35. }
  36. void av_init_packet(AVPacket *pkt)
  37. {
  38. pkt->pts = AV_NOPTS_VALUE;
  39. pkt->dts = AV_NOPTS_VALUE;
  40. pkt->pos = -1;
  41. pkt->duration = 0;
  42. pkt->convergence_duration = 0;
  43. pkt->flags = 0;
  44. pkt->stream_index = 0;
  45. pkt->destruct = NULL;
  46. pkt->side_data = NULL;
  47. pkt->side_data_elems = 0;
  48. }
  49. int av_new_packet(AVPacket *pkt, int size)
  50. {
  51. uint8_t *data = NULL;
  52. if ((unsigned)size < (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
  53. data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  54. if (data) {
  55. memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  56. } else
  57. size = 0;
  58. av_init_packet(pkt);
  59. pkt->data = data;
  60. pkt->size = size;
  61. pkt->destruct = av_destruct_packet;
  62. if (!data)
  63. return AVERROR(ENOMEM);
  64. return 0;
  65. }
  66. void av_shrink_packet(AVPacket *pkt, int size)
  67. {
  68. if (pkt->size <= size)
  69. return;
  70. pkt->size = size;
  71. memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  72. }
  73. int av_grow_packet(AVPacket *pkt, int grow_by)
  74. {
  75. void *new_ptr;
  76. av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
  77. if (!pkt->size)
  78. return av_new_packet(pkt, grow_by);
  79. if ((unsigned)grow_by >
  80. INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
  81. return -1;
  82. new_ptr = av_realloc(pkt->data,
  83. pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE);
  84. if (!new_ptr)
  85. return AVERROR(ENOMEM);
  86. pkt->data = new_ptr;
  87. pkt->size += grow_by;
  88. memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  89. return 0;
  90. }
  91. #define DUP_DATA(dst, src, size, padding) \
  92. do { \
  93. void *data; \
  94. if (padding) { \
  95. if ((unsigned)(size) > \
  96. (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE) \
  97. goto failed_alloc; \
  98. data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); \
  99. } else { \
  100. data = av_malloc(size); \
  101. } \
  102. if (!data) \
  103. goto failed_alloc; \
  104. memcpy(data, src, size); \
  105. if (padding) \
  106. memset((uint8_t *)data + size, 0, \
  107. FF_INPUT_BUFFER_PADDING_SIZE); \
  108. dst = data; \
  109. } while (0)
  110. int av_dup_packet(AVPacket *pkt)
  111. {
  112. AVPacket tmp_pkt;
  113. if (pkt->destruct == NULL && pkt->data) {
  114. tmp_pkt = *pkt;
  115. pkt->data = NULL;
  116. pkt->side_data = NULL;
  117. DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1);
  118. pkt->destruct = av_destruct_packet;
  119. if (pkt->side_data_elems) {
  120. int i;
  121. DUP_DATA(pkt->side_data, tmp_pkt.side_data,
  122. pkt->side_data_elems * sizeof(*pkt->side_data), 0);
  123. memset(pkt->side_data, 0,
  124. pkt->side_data_elems * sizeof(*pkt->side_data));
  125. for (i = 0; i < pkt->side_data_elems; i++)
  126. DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
  127. tmp_pkt.side_data[i].size, 1);
  128. }
  129. }
  130. return 0;
  131. failed_alloc:
  132. av_destruct_packet(pkt);
  133. return AVERROR(ENOMEM);
  134. }
  135. void av_free_packet(AVPacket *pkt)
  136. {
  137. if (pkt) {
  138. if (pkt->destruct)
  139. pkt->destruct(pkt);
  140. pkt->data = NULL;
  141. pkt->size = 0;
  142. pkt->side_data = NULL;
  143. pkt->side_data_elems = 0;
  144. }
  145. }
  146. uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  147. int size)
  148. {
  149. int elems = pkt->side_data_elems;
  150. if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
  151. return NULL;
  152. if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  153. return NULL;
  154. pkt->side_data = av_realloc(pkt->side_data,
  155. (elems + 1) * sizeof(*pkt->side_data));
  156. if (!pkt->side_data)
  157. return NULL;
  158. pkt->side_data[elems].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  159. if (!pkt->side_data[elems].data)
  160. return NULL;
  161. pkt->side_data[elems].size = size;
  162. pkt->side_data[elems].type = type;
  163. pkt->side_data_elems++;
  164. return pkt->side_data[elems].data;
  165. }
  166. uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  167. int *size)
  168. {
  169. int i;
  170. for (i = 0; i < pkt->side_data_elems; i++) {
  171. if (pkt->side_data[i].type == type) {
  172. if (size)
  173. *size = pkt->side_data[i].size;
  174. return pkt->side_data[i].data;
  175. }
  176. }
  177. return NULL;
  178. }
  179. int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  180. int size)
  181. {
  182. int i;
  183. for (i = 0; i < pkt->side_data_elems; i++) {
  184. if (pkt->side_data[i].type == type) {
  185. if (size > pkt->side_data[i].size)
  186. return AVERROR(ENOMEM);
  187. pkt->side_data[i].size = size;
  188. return 0;
  189. }
  190. }
  191. return AVERROR(ENOENT);
  192. }