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.

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