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.

347 lines
10KB

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