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.

384 lines
11KB

  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/internal.h"
  25. #include "libavutil/mem.h"
  26. #include "avcodec.h"
  27. #if FF_API_DESTRUCT_PACKET
  28. void av_destruct_packet(AVPacket *pkt)
  29. {
  30. av_free(pkt->data);
  31. pkt->data = NULL;
  32. pkt->size = 0;
  33. }
  34. /* a dummy destruct callback for the callers that assume AVPacket.destruct ==
  35. * NULL => static data */
  36. static void dummy_destruct_packet(AVPacket *pkt)
  37. {
  38. av_assert0(0);
  39. }
  40. #endif
  41. void av_init_packet(AVPacket *pkt)
  42. {
  43. pkt->pts = AV_NOPTS_VALUE;
  44. pkt->dts = AV_NOPTS_VALUE;
  45. pkt->pos = -1;
  46. pkt->duration = 0;
  47. pkt->convergence_duration = 0;
  48. pkt->flags = 0;
  49. pkt->stream_index = 0;
  50. #if FF_API_DESTRUCT_PACKET
  51. FF_DISABLE_DEPRECATION_WARNINGS
  52. pkt->destruct = NULL;
  53. FF_ENABLE_DEPRECATION_WARNINGS
  54. #endif
  55. pkt->buf = NULL;
  56. pkt->side_data = NULL;
  57. pkt->side_data_elems = 0;
  58. }
  59. static int packet_alloc(AVBufferRef **buf, int size)
  60. {
  61. int ret;
  62. if ((unsigned)size >= (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
  63. return AVERROR(EINVAL);
  64. ret = av_buffer_realloc(buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
  65. if (ret < 0)
  66. return ret;
  67. memset((*buf)->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  68. return 0;
  69. }
  70. int av_new_packet(AVPacket *pkt, int size)
  71. {
  72. AVBufferRef *buf = NULL;
  73. int ret = packet_alloc(&buf, size);
  74. if (ret < 0)
  75. return ret;
  76. av_init_packet(pkt);
  77. pkt->buf = buf;
  78. pkt->data = buf->data;
  79. pkt->size = size;
  80. #if FF_API_DESTRUCT_PACKET
  81. FF_DISABLE_DEPRECATION_WARNINGS
  82. pkt->destruct = dummy_destruct_packet;
  83. FF_ENABLE_DEPRECATION_WARNINGS
  84. #endif
  85. return 0;
  86. }
  87. void av_shrink_packet(AVPacket *pkt, int size)
  88. {
  89. if (pkt->size <= size)
  90. return;
  91. pkt->size = size;
  92. memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  93. }
  94. int av_grow_packet(AVPacket *pkt, int grow_by)
  95. {
  96. int new_size;
  97. av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
  98. if (!pkt->size)
  99. return av_new_packet(pkt, grow_by);
  100. if ((unsigned)grow_by >
  101. INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
  102. return -1;
  103. new_size = pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE;
  104. if (pkt->buf) {
  105. int ret = av_buffer_realloc(&pkt->buf, new_size);
  106. if (ret < 0)
  107. return ret;
  108. } else {
  109. pkt->buf = av_buffer_alloc(new_size);
  110. if (!pkt->buf)
  111. return AVERROR(ENOMEM);
  112. memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
  113. #if FF_API_DESTRUCT_PACKET
  114. FF_DISABLE_DEPRECATION_WARNINGS
  115. pkt->destruct = dummy_destruct_packet;
  116. FF_ENABLE_DEPRECATION_WARNINGS
  117. #endif
  118. }
  119. pkt->data = pkt->buf->data;
  120. pkt->size += grow_by;
  121. memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  122. return 0;
  123. }
  124. int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
  125. {
  126. if (size >= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  127. return AVERROR(EINVAL);
  128. pkt->buf = av_buffer_create(data, size + FF_INPUT_BUFFER_PADDING_SIZE,
  129. av_buffer_default_free, NULL, 0);
  130. if (!pkt->buf)
  131. return AVERROR(ENOMEM);
  132. pkt->data = data;
  133. pkt->size = size;
  134. #if FF_API_DESTRUCT_PACKET
  135. FF_DISABLE_DEPRECATION_WARNINGS
  136. pkt->destruct = dummy_destruct_packet;
  137. FF_ENABLE_DEPRECATION_WARNINGS
  138. #endif
  139. return 0;
  140. }
  141. #define ALLOC_MALLOC(data, size) data = av_malloc(size)
  142. #define ALLOC_BUF(data, size) \
  143. do { \
  144. av_buffer_realloc(&pkt->buf, size); \
  145. data = pkt->buf ? pkt->buf->data : NULL; \
  146. } while (0)
  147. #define DUP_DATA(dst, src, size, padding, ALLOC) \
  148. do { \
  149. void *data; \
  150. if (padding) { \
  151. if ((unsigned)(size) > \
  152. (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE) \
  153. goto failed_alloc; \
  154. ALLOC(data, size + FF_INPUT_BUFFER_PADDING_SIZE); \
  155. } else { \
  156. ALLOC(data, size); \
  157. } \
  158. if (!data) \
  159. goto failed_alloc; \
  160. memcpy(data, src, size); \
  161. if (padding) \
  162. memset((uint8_t *)data + size, 0, \
  163. FF_INPUT_BUFFER_PADDING_SIZE); \
  164. dst = data; \
  165. } while (0)
  166. int av_dup_packet(AVPacket *pkt)
  167. {
  168. AVPacket tmp_pkt;
  169. FF_DISABLE_DEPRECATION_WARNINGS
  170. if (!pkt->buf && pkt->data
  171. #if FF_API_DESTRUCT_PACKET
  172. && !pkt->destruct
  173. #endif
  174. ) {
  175. FF_ENABLE_DEPRECATION_WARNINGS
  176. tmp_pkt = *pkt;
  177. pkt->data = NULL;
  178. pkt->side_data = NULL;
  179. DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1, ALLOC_BUF);
  180. #if FF_API_DESTRUCT_PACKET
  181. FF_DISABLE_DEPRECATION_WARNINGS
  182. pkt->destruct = dummy_destruct_packet;
  183. FF_ENABLE_DEPRECATION_WARNINGS
  184. #endif
  185. if (pkt->side_data_elems) {
  186. int i;
  187. DUP_DATA(pkt->side_data, tmp_pkt.side_data,
  188. pkt->side_data_elems * sizeof(*pkt->side_data), 0, ALLOC_MALLOC);
  189. memset(pkt->side_data, 0,
  190. pkt->side_data_elems * sizeof(*pkt->side_data));
  191. for (i = 0; i < pkt->side_data_elems; i++) {
  192. DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
  193. tmp_pkt.side_data[i].size, 1, ALLOC_MALLOC);
  194. pkt->side_data[i].size = tmp_pkt.side_data[i].size;
  195. pkt->side_data[i].type = tmp_pkt.side_data[i].type;
  196. }
  197. }
  198. }
  199. return 0;
  200. failed_alloc:
  201. av_free_packet(pkt);
  202. return AVERROR(ENOMEM);
  203. }
  204. void av_packet_free_side_data(AVPacket *pkt)
  205. {
  206. int i;
  207. for (i = 0; i < pkt->side_data_elems; i++)
  208. av_free(pkt->side_data[i].data);
  209. av_freep(&pkt->side_data);
  210. pkt->side_data_elems = 0;
  211. }
  212. void av_free_packet(AVPacket *pkt)
  213. {
  214. if (pkt) {
  215. FF_DISABLE_DEPRECATION_WARNINGS
  216. if (pkt->buf)
  217. av_buffer_unref(&pkt->buf);
  218. #if FF_API_DESTRUCT_PACKET
  219. else if (pkt->destruct)
  220. pkt->destruct(pkt);
  221. pkt->destruct = NULL;
  222. #endif
  223. FF_ENABLE_DEPRECATION_WARNINGS
  224. pkt->data = NULL;
  225. pkt->size = 0;
  226. av_packet_free_side_data(pkt);
  227. }
  228. }
  229. uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  230. int size)
  231. {
  232. int elems = pkt->side_data_elems;
  233. if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
  234. return NULL;
  235. if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  236. return NULL;
  237. pkt->side_data = av_realloc(pkt->side_data,
  238. (elems + 1) * sizeof(*pkt->side_data));
  239. if (!pkt->side_data)
  240. return NULL;
  241. pkt->side_data[elems].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  242. if (!pkt->side_data[elems].data)
  243. return NULL;
  244. pkt->side_data[elems].size = size;
  245. pkt->side_data[elems].type = type;
  246. pkt->side_data_elems++;
  247. return pkt->side_data[elems].data;
  248. }
  249. uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  250. int *size)
  251. {
  252. int i;
  253. for (i = 0; i < pkt->side_data_elems; i++) {
  254. if (pkt->side_data[i].type == type) {
  255. if (size)
  256. *size = pkt->side_data[i].size;
  257. return pkt->side_data[i].data;
  258. }
  259. }
  260. return NULL;
  261. }
  262. int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  263. int size)
  264. {
  265. int i;
  266. for (i = 0; i < pkt->side_data_elems; i++) {
  267. if (pkt->side_data[i].type == type) {
  268. if (size > pkt->side_data[i].size)
  269. return AVERROR(ENOMEM);
  270. pkt->side_data[i].size = size;
  271. return 0;
  272. }
  273. }
  274. return AVERROR(ENOENT);
  275. }
  276. int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
  277. {
  278. int i;
  279. dst->pts = src->pts;
  280. dst->dts = src->dts;
  281. dst->pos = src->pos;
  282. dst->duration = src->duration;
  283. dst->convergence_duration = src->convergence_duration;
  284. dst->flags = src->flags;
  285. dst->stream_index = src->stream_index;
  286. dst->side_data_elems = src->side_data_elems;
  287. for (i = 0; i < src->side_data_elems; i++) {
  288. enum AVPacketSideDataType type = src->side_data[i].type;
  289. int size = src->side_data[i].size;
  290. uint8_t *src_data = src->side_data[i].data;
  291. uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
  292. if (!dst_data) {
  293. av_packet_free_side_data(dst);
  294. return AVERROR(ENOMEM);
  295. }
  296. memcpy(dst_data, src_data, size);
  297. }
  298. return 0;
  299. }
  300. void av_packet_unref(AVPacket *pkt)
  301. {
  302. av_packet_free_side_data(pkt);
  303. av_buffer_unref(&pkt->buf);
  304. av_init_packet(pkt);
  305. pkt->data = NULL;
  306. pkt->size = 0;
  307. }
  308. int av_packet_ref(AVPacket *dst, AVPacket *src)
  309. {
  310. int ret;
  311. ret = av_packet_copy_props(dst, src);
  312. if (ret < 0)
  313. return ret;
  314. if (!src->buf) {
  315. ret = packet_alloc(&dst->buf, src->size);
  316. if (ret < 0)
  317. goto fail;
  318. memcpy(dst->buf->data, src->data, src->size);
  319. } else
  320. dst->buf = av_buffer_ref(src->buf);
  321. dst->size = src->size;
  322. dst->data = dst->buf->data;
  323. return 0;
  324. fail:
  325. av_packet_free_side_data(dst);
  326. return ret;
  327. }
  328. void av_packet_move_ref(AVPacket *dst, AVPacket *src)
  329. {
  330. *dst = *src;
  331. av_init_packet(src);
  332. }