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.

363 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. #define ALLOC_MALLOC(data, size) data = av_malloc(size)
  113. #define ALLOC_BUF(data, size) \
  114. do { \
  115. av_buffer_realloc(&pkt->buf, size); \
  116. data = pkt->buf ? pkt->buf->data : NULL; \
  117. } while (0)
  118. #define DUP_DATA(dst, src, size, padding, ALLOC) \
  119. do { \
  120. void *data; \
  121. if (padding) { \
  122. if ((unsigned)(size) > \
  123. (unsigned)(size) + AV_INPUT_BUFFER_PADDING_SIZE) \
  124. goto failed_alloc; \
  125. ALLOC(data, size + AV_INPUT_BUFFER_PADDING_SIZE); \
  126. } else { \
  127. ALLOC(data, size); \
  128. } \
  129. if (!data) \
  130. goto failed_alloc; \
  131. memcpy(data, src, size); \
  132. if (padding) \
  133. memset((uint8_t *)data + size, 0, \
  134. AV_INPUT_BUFFER_PADDING_SIZE); \
  135. dst = data; \
  136. } while (0)
  137. int av_dup_packet(AVPacket *pkt)
  138. {
  139. AVPacket tmp_pkt;
  140. if (!pkt->buf && pkt->data) {
  141. tmp_pkt = *pkt;
  142. pkt->data = NULL;
  143. pkt->side_data = NULL;
  144. DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1, ALLOC_BUF);
  145. if (pkt->side_data_elems) {
  146. int i;
  147. DUP_DATA(pkt->side_data, tmp_pkt.side_data,
  148. pkt->side_data_elems * sizeof(*pkt->side_data), 0, ALLOC_MALLOC);
  149. memset(pkt->side_data, 0,
  150. pkt->side_data_elems * sizeof(*pkt->side_data));
  151. for (i = 0; i < pkt->side_data_elems; i++) {
  152. DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
  153. tmp_pkt.side_data[i].size, 1, ALLOC_MALLOC);
  154. pkt->side_data[i].size = tmp_pkt.side_data[i].size;
  155. pkt->side_data[i].type = tmp_pkt.side_data[i].type;
  156. }
  157. }
  158. }
  159. return 0;
  160. failed_alloc:
  161. av_packet_unref(pkt);
  162. return AVERROR(ENOMEM);
  163. }
  164. void av_packet_free_side_data(AVPacket *pkt)
  165. {
  166. int i;
  167. for (i = 0; i < pkt->side_data_elems; i++)
  168. av_free(pkt->side_data[i].data);
  169. av_freep(&pkt->side_data);
  170. pkt->side_data_elems = 0;
  171. }
  172. #if FF_API_AVPACKET_OLD_API
  173. FF_DISABLE_DEPRECATION_WARNINGS
  174. void av_free_packet(AVPacket *pkt)
  175. {
  176. if (pkt) {
  177. if (pkt->buf)
  178. av_buffer_unref(&pkt->buf);
  179. pkt->data = NULL;
  180. pkt->size = 0;
  181. av_packet_free_side_data(pkt);
  182. }
  183. }
  184. FF_ENABLE_DEPRECATION_WARNINGS
  185. #endif
  186. uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  187. int size)
  188. {
  189. int elems = pkt->side_data_elems;
  190. if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
  191. return NULL;
  192. if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
  193. return NULL;
  194. pkt->side_data = av_realloc(pkt->side_data,
  195. (elems + 1) * sizeof(*pkt->side_data));
  196. if (!pkt->side_data)
  197. return NULL;
  198. pkt->side_data[elems].data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  199. if (!pkt->side_data[elems].data)
  200. return NULL;
  201. pkt->side_data[elems].size = size;
  202. pkt->side_data[elems].type = type;
  203. pkt->side_data_elems++;
  204. return pkt->side_data[elems].data;
  205. }
  206. uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  207. int *size)
  208. {
  209. int i;
  210. for (i = 0; i < pkt->side_data_elems; i++) {
  211. if (pkt->side_data[i].type == type) {
  212. if (size)
  213. *size = pkt->side_data[i].size;
  214. return pkt->side_data[i].data;
  215. }
  216. }
  217. return NULL;
  218. }
  219. int av_packet_shrink_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 > pkt->side_data[i].size)
  226. return AVERROR(ENOMEM);
  227. pkt->side_data[i].size = size;
  228. return 0;
  229. }
  230. }
  231. return AVERROR(ENOENT);
  232. }
  233. int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
  234. {
  235. int i;
  236. dst->pts = src->pts;
  237. dst->dts = src->dts;
  238. dst->pos = src->pos;
  239. dst->duration = src->duration;
  240. #if FF_API_CONVERGENCE_DURATION
  241. FF_DISABLE_DEPRECATION_WARNINGS
  242. dst->convergence_duration = src->convergence_duration;
  243. FF_ENABLE_DEPRECATION_WARNINGS
  244. #endif
  245. dst->flags = src->flags;
  246. dst->stream_index = src->stream_index;
  247. for (i = 0; i < src->side_data_elems; i++) {
  248. enum AVPacketSideDataType type = src->side_data[i].type;
  249. int size = src->side_data[i].size;
  250. uint8_t *src_data = src->side_data[i].data;
  251. uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
  252. if (!dst_data) {
  253. av_packet_free_side_data(dst);
  254. return AVERROR(ENOMEM);
  255. }
  256. memcpy(dst_data, src_data, size);
  257. }
  258. return 0;
  259. }
  260. void av_packet_unref(AVPacket *pkt)
  261. {
  262. av_packet_free_side_data(pkt);
  263. av_buffer_unref(&pkt->buf);
  264. av_init_packet(pkt);
  265. pkt->data = NULL;
  266. pkt->size = 0;
  267. }
  268. int av_packet_ref(AVPacket *dst, AVPacket *src)
  269. {
  270. int ret;
  271. ret = av_packet_copy_props(dst, src);
  272. if (ret < 0)
  273. return ret;
  274. if (!src->buf) {
  275. ret = packet_alloc(&dst->buf, src->size);
  276. if (ret < 0)
  277. goto fail;
  278. memcpy(dst->buf->data, src->data, src->size);
  279. } else {
  280. dst->buf = av_buffer_ref(src->buf);
  281. if (!dst->buf) {
  282. ret = AVERROR(ENOMEM);
  283. goto fail;
  284. }
  285. }
  286. dst->size = src->size;
  287. dst->data = dst->buf->data;
  288. return 0;
  289. fail:
  290. av_packet_free_side_data(dst);
  291. return ret;
  292. }
  293. void av_packet_move_ref(AVPacket *dst, AVPacket *src)
  294. {
  295. *dst = *src;
  296. av_init_packet(src);
  297. }
  298. void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
  299. {
  300. if (pkt->pts != AV_NOPTS_VALUE)
  301. pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
  302. if (pkt->dts != AV_NOPTS_VALUE)
  303. pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
  304. if (pkt->duration > 0)
  305. pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
  306. #if FF_API_CONVERGENCE_DURATION
  307. FF_DISABLE_DEPRECATION_WARNINGS
  308. if (pkt->convergence_duration > 0)
  309. pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
  310. FF_ENABLE_DEPRECATION_WARNINGS
  311. #endif
  312. }