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.

359 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_free_packet(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. void av_free_packet(AVPacket *pkt)
  173. {
  174. if (pkt) {
  175. if (pkt->buf)
  176. av_buffer_unref(&pkt->buf);
  177. pkt->data = NULL;
  178. pkt->size = 0;
  179. av_packet_free_side_data(pkt);
  180. }
  181. }
  182. uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  183. int size)
  184. {
  185. int elems = pkt->side_data_elems;
  186. if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
  187. return NULL;
  188. if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
  189. return NULL;
  190. pkt->side_data = av_realloc(pkt->side_data,
  191. (elems + 1) * sizeof(*pkt->side_data));
  192. if (!pkt->side_data)
  193. return NULL;
  194. pkt->side_data[elems].data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
  195. if (!pkt->side_data[elems].data)
  196. return NULL;
  197. pkt->side_data[elems].size = size;
  198. pkt->side_data[elems].type = type;
  199. pkt->side_data_elems++;
  200. return pkt->side_data[elems].data;
  201. }
  202. uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  203. int *size)
  204. {
  205. int i;
  206. for (i = 0; i < pkt->side_data_elems; i++) {
  207. if (pkt->side_data[i].type == type) {
  208. if (size)
  209. *size = pkt->side_data[i].size;
  210. return pkt->side_data[i].data;
  211. }
  212. }
  213. return NULL;
  214. }
  215. int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  216. int size)
  217. {
  218. int i;
  219. for (i = 0; i < pkt->side_data_elems; i++) {
  220. if (pkt->side_data[i].type == type) {
  221. if (size > pkt->side_data[i].size)
  222. return AVERROR(ENOMEM);
  223. pkt->side_data[i].size = size;
  224. return 0;
  225. }
  226. }
  227. return AVERROR(ENOENT);
  228. }
  229. int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
  230. {
  231. int i;
  232. dst->pts = src->pts;
  233. dst->dts = src->dts;
  234. dst->pos = src->pos;
  235. dst->duration = src->duration;
  236. #if FF_API_CONVERGENCE_DURATION
  237. FF_DISABLE_DEPRECATION_WARNINGS
  238. dst->convergence_duration = src->convergence_duration;
  239. FF_ENABLE_DEPRECATION_WARNINGS
  240. #endif
  241. dst->flags = src->flags;
  242. dst->stream_index = src->stream_index;
  243. for (i = 0; i < src->side_data_elems; i++) {
  244. enum AVPacketSideDataType type = src->side_data[i].type;
  245. int size = src->side_data[i].size;
  246. uint8_t *src_data = src->side_data[i].data;
  247. uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
  248. if (!dst_data) {
  249. av_packet_free_side_data(dst);
  250. return AVERROR(ENOMEM);
  251. }
  252. memcpy(dst_data, src_data, size);
  253. }
  254. return 0;
  255. }
  256. void av_packet_unref(AVPacket *pkt)
  257. {
  258. av_packet_free_side_data(pkt);
  259. av_buffer_unref(&pkt->buf);
  260. av_init_packet(pkt);
  261. pkt->data = NULL;
  262. pkt->size = 0;
  263. }
  264. int av_packet_ref(AVPacket *dst, AVPacket *src)
  265. {
  266. int ret;
  267. ret = av_packet_copy_props(dst, src);
  268. if (ret < 0)
  269. return ret;
  270. if (!src->buf) {
  271. ret = packet_alloc(&dst->buf, src->size);
  272. if (ret < 0)
  273. goto fail;
  274. memcpy(dst->buf->data, src->data, src->size);
  275. } else {
  276. dst->buf = av_buffer_ref(src->buf);
  277. if (!dst->buf) {
  278. ret = AVERROR(ENOMEM);
  279. goto fail;
  280. }
  281. }
  282. dst->size = src->size;
  283. dst->data = dst->buf->data;
  284. return 0;
  285. fail:
  286. av_packet_free_side_data(dst);
  287. return ret;
  288. }
  289. void av_packet_move_ref(AVPacket *dst, AVPacket *src)
  290. {
  291. *dst = *src;
  292. av_init_packet(src);
  293. }
  294. void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
  295. {
  296. if (pkt->pts != AV_NOPTS_VALUE)
  297. pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
  298. if (pkt->dts != AV_NOPTS_VALUE)
  299. pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
  300. if (pkt->duration > 0)
  301. pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
  302. #if FF_API_CONVERGENCE_DURATION
  303. FF_DISABLE_DEPRECATION_WARNINGS
  304. if (pkt->convergence_duration > 0)
  305. pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
  306. FF_ENABLE_DEPRECATION_WARNINGS
  307. #endif
  308. }