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.

401 lines
12KB

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