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.

285 lines
8.5KB

  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/common.h"
  24. #include "libavutil/mem.h"
  25. #include "avcodec.h"
  26. #if FF_API_DESTRUCT_PACKET
  27. void av_destruct_packet(AVPacket *pkt)
  28. {
  29. av_free(pkt->data);
  30. pkt->data = NULL;
  31. pkt->size = 0;
  32. }
  33. /* a dummy destruct callback for the callers that assume AVPacket.destruct ==
  34. * NULL => static data */
  35. static void dummy_destruct_packet(AVPacket *pkt)
  36. {
  37. av_assert0(0);
  38. }
  39. #endif
  40. void av_init_packet(AVPacket *pkt)
  41. {
  42. pkt->pts = AV_NOPTS_VALUE;
  43. pkt->dts = AV_NOPTS_VALUE;
  44. pkt->pos = -1;
  45. pkt->duration = 0;
  46. pkt->convergence_duration = 0;
  47. pkt->flags = 0;
  48. pkt->stream_index = 0;
  49. #if FF_API_DESTRUCT_PACKET
  50. pkt->destruct = NULL;
  51. #endif
  52. pkt->buf = NULL;
  53. pkt->side_data = NULL;
  54. pkt->side_data_elems = 0;
  55. }
  56. int av_new_packet(AVPacket *pkt, int size)
  57. {
  58. AVBufferRef *buf = NULL;
  59. if ((unsigned)size >= (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
  60. return AVERROR(EINVAL);
  61. av_buffer_realloc(&buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
  62. if (!buf)
  63. return AVERROR(ENOMEM);
  64. memset(buf->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  65. av_init_packet(pkt);
  66. pkt->buf = buf;
  67. pkt->data = buf->data;
  68. pkt->size = size;
  69. #if FF_API_DESTRUCT_PACKET
  70. pkt->destruct = dummy_destruct_packet;
  71. #endif
  72. return 0;
  73. }
  74. void av_shrink_packet(AVPacket *pkt, int size)
  75. {
  76. if (pkt->size <= size)
  77. return;
  78. pkt->size = size;
  79. memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  80. }
  81. int av_grow_packet(AVPacket *pkt, int grow_by)
  82. {
  83. int new_size;
  84. av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
  85. if (!pkt->size)
  86. return av_new_packet(pkt, grow_by);
  87. if ((unsigned)grow_by >
  88. INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
  89. return -1;
  90. new_size = pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE;
  91. if (pkt->buf) {
  92. int ret = av_buffer_realloc(&pkt->buf, new_size);
  93. if (ret < 0)
  94. return ret;
  95. } else {
  96. pkt->buf = av_buffer_alloc(new_size);
  97. if (!pkt->buf)
  98. return AVERROR(ENOMEM);
  99. memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
  100. #if FF_API_DESTRUCT_PACKET
  101. pkt->destruct = dummy_destruct_packet;
  102. #endif
  103. }
  104. pkt->data = pkt->buf->data;
  105. pkt->size += grow_by;
  106. memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  107. return 0;
  108. }
  109. int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
  110. {
  111. if (size >= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  112. return AVERROR(EINVAL);
  113. pkt->buf = av_buffer_create(data, size + FF_INPUT_BUFFER_PADDING_SIZE,
  114. av_buffer_default_free, NULL, 0);
  115. if (!pkt->buf)
  116. return AVERROR(ENOMEM);
  117. pkt->data = data;
  118. pkt->size = size;
  119. #if FF_API_DESTRUCT_PACKET
  120. pkt->destruct = dummy_destruct_packet;
  121. #endif
  122. return 0;
  123. }
  124. #define ALLOC_MALLOC(data, size) data = av_malloc(size)
  125. #define ALLOC_BUF(data, size) \
  126. do { \
  127. av_buffer_realloc(&pkt->buf, size); \
  128. data = pkt->buf ? pkt->buf->data : NULL; \
  129. } while (0)
  130. #define DUP_DATA(dst, src, size, padding, ALLOC) \
  131. do { \
  132. void *data; \
  133. if (padding) { \
  134. if ((unsigned)(size) > \
  135. (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE) \
  136. goto failed_alloc; \
  137. ALLOC(data, size + FF_INPUT_BUFFER_PADDING_SIZE); \
  138. } else { \
  139. ALLOC(data, size); \
  140. } \
  141. if (!data) \
  142. goto failed_alloc; \
  143. memcpy(data, src, size); \
  144. if (padding) \
  145. memset((uint8_t *)data + size, 0, \
  146. FF_INPUT_BUFFER_PADDING_SIZE); \
  147. dst = data; \
  148. } while (0)
  149. int av_dup_packet(AVPacket *pkt)
  150. {
  151. AVPacket tmp_pkt;
  152. if (!pkt->buf && pkt->data
  153. #if FF_API_DESTRUCT_PACKET
  154. && !pkt->destruct
  155. #endif
  156. ) {
  157. tmp_pkt = *pkt;
  158. pkt->data = NULL;
  159. pkt->side_data = NULL;
  160. DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1, ALLOC_BUF);
  161. #if FF_API_DESTRUCT_PACKET
  162. pkt->destruct = dummy_destruct_packet;
  163. #endif
  164. if (pkt->side_data_elems) {
  165. int i;
  166. DUP_DATA(pkt->side_data, tmp_pkt.side_data,
  167. pkt->side_data_elems * sizeof(*pkt->side_data), 0, ALLOC_MALLOC);
  168. memset(pkt->side_data, 0,
  169. pkt->side_data_elems * sizeof(*pkt->side_data));
  170. for (i = 0; i < pkt->side_data_elems; i++)
  171. DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
  172. tmp_pkt.side_data[i].size, 1, ALLOC_MALLOC);
  173. }
  174. }
  175. return 0;
  176. failed_alloc:
  177. av_free_packet(pkt);
  178. return AVERROR(ENOMEM);
  179. }
  180. void av_free_packet(AVPacket *pkt)
  181. {
  182. if (pkt) {
  183. int i;
  184. if (pkt->buf)
  185. av_buffer_unref(&pkt->buf);
  186. #if FF_API_DESTRUCT_PACKET
  187. else if (pkt->destruct)
  188. pkt->destruct(pkt);
  189. pkt->destruct = NULL;
  190. #endif
  191. pkt->data = NULL;
  192. pkt->size = 0;
  193. for (i = 0; i < pkt->side_data_elems; i++)
  194. av_free(pkt->side_data[i].data);
  195. av_freep(&pkt->side_data);
  196. pkt->side_data_elems = 0;
  197. }
  198. }
  199. uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  200. int size)
  201. {
  202. int elems = pkt->side_data_elems;
  203. if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
  204. return NULL;
  205. if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  206. return NULL;
  207. pkt->side_data = av_realloc(pkt->side_data,
  208. (elems + 1) * sizeof(*pkt->side_data));
  209. if (!pkt->side_data)
  210. return NULL;
  211. pkt->side_data[elems].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  212. if (!pkt->side_data[elems].data)
  213. return NULL;
  214. pkt->side_data[elems].size = size;
  215. pkt->side_data[elems].type = type;
  216. pkt->side_data_elems++;
  217. return pkt->side_data[elems].data;
  218. }
  219. uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  220. int *size)
  221. {
  222. int i;
  223. for (i = 0; i < pkt->side_data_elems; i++) {
  224. if (pkt->side_data[i].type == type) {
  225. if (size)
  226. *size = pkt->side_data[i].size;
  227. return pkt->side_data[i].data;
  228. }
  229. }
  230. return NULL;
  231. }
  232. int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  233. int size)
  234. {
  235. int i;
  236. for (i = 0; i < pkt->side_data_elems; i++) {
  237. if (pkt->side_data[i].type == type) {
  238. if (size > pkt->side_data[i].size)
  239. return AVERROR(ENOMEM);
  240. pkt->side_data[i].size = size;
  241. return 0;
  242. }
  243. }
  244. return AVERROR(ENOENT);
  245. }