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.

218 lines
6.8KB

  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. av_free(pkt->data);
  28. pkt->data = NULL;
  29. pkt->size = 0;
  30. }
  31. void av_init_packet(AVPacket *pkt)
  32. {
  33. pkt->pts = AV_NOPTS_VALUE;
  34. pkt->dts = AV_NOPTS_VALUE;
  35. pkt->pos = -1;
  36. pkt->duration = 0;
  37. pkt->convergence_duration = 0;
  38. pkt->flags = 0;
  39. pkt->stream_index = 0;
  40. pkt->destruct = NULL;
  41. pkt->side_data = NULL;
  42. pkt->side_data_elems = 0;
  43. }
  44. int av_new_packet(AVPacket *pkt, int size)
  45. {
  46. uint8_t *data = NULL;
  47. if ((unsigned)size < (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
  48. data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  49. if (data) {
  50. memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  51. } else
  52. size = 0;
  53. av_init_packet(pkt);
  54. pkt->data = data;
  55. pkt->size = size;
  56. pkt->destruct = av_destruct_packet;
  57. if (!data)
  58. return AVERROR(ENOMEM);
  59. return 0;
  60. }
  61. void av_shrink_packet(AVPacket *pkt, int size)
  62. {
  63. if (pkt->size <= size)
  64. return;
  65. pkt->size = size;
  66. memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  67. }
  68. int av_grow_packet(AVPacket *pkt, int grow_by)
  69. {
  70. void *new_ptr;
  71. av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
  72. if (!pkt->size)
  73. return av_new_packet(pkt, grow_by);
  74. if ((unsigned)grow_by >
  75. INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
  76. return -1;
  77. new_ptr = av_realloc(pkt->data,
  78. pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE);
  79. if (!new_ptr)
  80. return AVERROR(ENOMEM);
  81. pkt->data = new_ptr;
  82. pkt->size += grow_by;
  83. memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  84. return 0;
  85. }
  86. #define DUP_DATA(dst, src, size, padding) \
  87. do { \
  88. void *data; \
  89. if (padding) { \
  90. if ((unsigned)(size) > \
  91. (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE) \
  92. goto failed_alloc; \
  93. data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); \
  94. } else { \
  95. data = av_malloc(size); \
  96. } \
  97. if (!data) \
  98. goto failed_alloc; \
  99. memcpy(data, src, size); \
  100. if (padding) \
  101. memset((uint8_t *)data + size, 0, \
  102. FF_INPUT_BUFFER_PADDING_SIZE); \
  103. dst = data; \
  104. } while (0)
  105. int av_dup_packet(AVPacket *pkt)
  106. {
  107. AVPacket tmp_pkt;
  108. if (pkt->destruct == NULL && pkt->data) {
  109. tmp_pkt = *pkt;
  110. pkt->data = NULL;
  111. pkt->side_data = NULL;
  112. DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1);
  113. pkt->destruct = av_destruct_packet;
  114. if (pkt->side_data_elems) {
  115. int i;
  116. DUP_DATA(pkt->side_data, tmp_pkt.side_data,
  117. pkt->side_data_elems * sizeof(*pkt->side_data), 0);
  118. memset(pkt->side_data, 0,
  119. pkt->side_data_elems * sizeof(*pkt->side_data));
  120. for (i = 0; i < pkt->side_data_elems; i++)
  121. DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
  122. tmp_pkt.side_data[i].size, 1);
  123. }
  124. }
  125. return 0;
  126. failed_alloc:
  127. av_destruct_packet(pkt);
  128. return AVERROR(ENOMEM);
  129. }
  130. void av_free_packet(AVPacket *pkt)
  131. {
  132. if (pkt) {
  133. int i;
  134. if (pkt->destruct)
  135. pkt->destruct(pkt);
  136. pkt->data = NULL;
  137. pkt->size = 0;
  138. for (i = 0; i < pkt->side_data_elems; i++)
  139. av_free(pkt->side_data[i].data);
  140. av_freep(&pkt->side_data);
  141. pkt->side_data_elems = 0;
  142. }
  143. }
  144. uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  145. int size)
  146. {
  147. int elems = pkt->side_data_elems;
  148. if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
  149. return NULL;
  150. if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  151. return NULL;
  152. pkt->side_data = av_realloc(pkt->side_data,
  153. (elems + 1) * sizeof(*pkt->side_data));
  154. if (!pkt->side_data)
  155. return NULL;
  156. pkt->side_data[elems].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  157. if (!pkt->side_data[elems].data)
  158. return NULL;
  159. pkt->side_data[elems].size = size;
  160. pkt->side_data[elems].type = type;
  161. pkt->side_data_elems++;
  162. return pkt->side_data[elems].data;
  163. }
  164. uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  165. int *size)
  166. {
  167. int i;
  168. for (i = 0; i < pkt->side_data_elems; i++) {
  169. if (pkt->side_data[i].type == type) {
  170. if (size)
  171. *size = pkt->side_data[i].size;
  172. return pkt->side_data[i].data;
  173. }
  174. }
  175. return NULL;
  176. }
  177. int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  178. int size)
  179. {
  180. int i;
  181. for (i = 0; i < pkt->side_data_elems; i++) {
  182. if (pkt->side_data[i].type == type) {
  183. if (size > pkt->side_data[i].size)
  184. return AVERROR(ENOMEM);
  185. pkt->side_data[i].size = size;
  186. return 0;
  187. }
  188. }
  189. return AVERROR(ENOENT);
  190. }