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.

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