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.

437 lines
13KB

  1. /*
  2. * AVPacket functions for libavcodec
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/mem.h"
  26. #include "avcodec.h"
  27. #include "bytestream.h"
  28. #include "internal.h"
  29. void ff_packet_free_side_data(AVPacket *pkt)
  30. {
  31. int i;
  32. for (i = 0; i < pkt->side_data_elems; i++)
  33. av_free(pkt->side_data[i].data);
  34. av_freep(&pkt->side_data);
  35. pkt->side_data_elems = 0;
  36. }
  37. #if FF_API_DESTRUCT_PACKET
  38. void av_destruct_packet(AVPacket *pkt)
  39. {
  40. av_free(pkt->data);
  41. pkt->data = NULL;
  42. pkt->size = 0;
  43. }
  44. /* a dummy destruct callback for the callers that assume AVPacket.destruct ==
  45. * NULL => static data */
  46. static void dummy_destruct_packet(AVPacket *pkt)
  47. {
  48. av_assert0(0);
  49. }
  50. #endif
  51. void av_init_packet(AVPacket *pkt)
  52. {
  53. pkt->pts = AV_NOPTS_VALUE;
  54. pkt->dts = AV_NOPTS_VALUE;
  55. pkt->pos = -1;
  56. pkt->duration = 0;
  57. pkt->convergence_duration = 0;
  58. pkt->flags = 0;
  59. pkt->stream_index = 0;
  60. #if FF_API_DESTRUCT_PACKET
  61. FF_DISABLE_DEPRECATION_WARNINGS
  62. pkt->destruct = NULL;
  63. FF_ENABLE_DEPRECATION_WARNINGS
  64. #endif
  65. pkt->buf = NULL;
  66. pkt->side_data = NULL;
  67. pkt->side_data_elems = 0;
  68. }
  69. int av_new_packet(AVPacket *pkt, int size)
  70. {
  71. AVBufferRef *buf = NULL;
  72. if ((unsigned)size >= (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
  73. return AVERROR(EINVAL);
  74. av_buffer_realloc(&buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
  75. if (!buf)
  76. return AVERROR(ENOMEM);
  77. memset(buf->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  78. av_init_packet(pkt);
  79. pkt->buf = buf;
  80. pkt->data = buf->data;
  81. pkt->size = size;
  82. #if FF_API_DESTRUCT_PACKET
  83. FF_DISABLE_DEPRECATION_WARNINGS
  84. pkt->destruct = dummy_destruct_packet;
  85. FF_ENABLE_DEPRECATION_WARNINGS
  86. #endif
  87. return 0;
  88. }
  89. void av_shrink_packet(AVPacket *pkt, int size)
  90. {
  91. if (pkt->size <= size)
  92. return;
  93. pkt->size = size;
  94. memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  95. }
  96. int av_grow_packet(AVPacket *pkt, int grow_by)
  97. {
  98. int new_size;
  99. av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
  100. if (!pkt->size)
  101. return av_new_packet(pkt, grow_by);
  102. if ((unsigned)grow_by >
  103. INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
  104. return -1;
  105. new_size = pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE;
  106. if (pkt->buf) {
  107. int ret = av_buffer_realloc(&pkt->buf, new_size);
  108. if (ret < 0)
  109. return ret;
  110. } else {
  111. pkt->buf = av_buffer_alloc(new_size);
  112. if (!pkt->buf)
  113. return AVERROR(ENOMEM);
  114. memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
  115. #if FF_API_DESTRUCT_PACKET
  116. FF_DISABLE_DEPRECATION_WARNINGS
  117. pkt->destruct = dummy_destruct_packet;
  118. FF_ENABLE_DEPRECATION_WARNINGS
  119. #endif
  120. }
  121. pkt->data = pkt->buf->data;
  122. pkt->size += grow_by;
  123. memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  124. return 0;
  125. }
  126. int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
  127. {
  128. if (size >= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  129. return AVERROR(EINVAL);
  130. pkt->buf = av_buffer_create(data, size + FF_INPUT_BUFFER_PADDING_SIZE,
  131. av_buffer_default_free, NULL, 0);
  132. if (!pkt->buf)
  133. return AVERROR(ENOMEM);
  134. pkt->data = data;
  135. pkt->size = size;
  136. #if FF_API_DESTRUCT_PACKET
  137. FF_DISABLE_DEPRECATION_WARNINGS
  138. pkt->destruct = dummy_destruct_packet;
  139. FF_ENABLE_DEPRECATION_WARNINGS
  140. #endif
  141. return 0;
  142. }
  143. #define ALLOC_MALLOC(data, size) data = av_malloc(size)
  144. #define ALLOC_BUF(data, size) \
  145. do { \
  146. av_buffer_realloc(&pkt->buf, size); \
  147. data = pkt->buf ? pkt->buf->data : NULL; \
  148. } while (0)
  149. #define DUP_DATA(dst, src, size, padding, ALLOC) \
  150. do { \
  151. void *data; \
  152. if (padding) { \
  153. if ((unsigned)(size) > \
  154. (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE) \
  155. goto failed_alloc; \
  156. ALLOC(data, size + FF_INPUT_BUFFER_PADDING_SIZE); \
  157. } else { \
  158. ALLOC(data, size); \
  159. } \
  160. if (!data) \
  161. goto failed_alloc; \
  162. memcpy(data, src, size); \
  163. if (padding) \
  164. memset((uint8_t *)data + size, 0, \
  165. FF_INPUT_BUFFER_PADDING_SIZE); \
  166. dst = data; \
  167. } while (0)
  168. /* Makes duplicates of data, side_data, but does not copy any other fields */
  169. static int copy_packet_data(AVPacket *pkt, AVPacket *src, int dup)
  170. {
  171. pkt->data = NULL;
  172. pkt->side_data = NULL;
  173. if (pkt->buf) {
  174. AVBufferRef *ref = av_buffer_ref(src->buf);
  175. if (!ref)
  176. return AVERROR(ENOMEM);
  177. pkt->buf = ref;
  178. pkt->data = ref->data;
  179. } else {
  180. DUP_DATA(pkt->data, src->data, pkt->size, 1, ALLOC_BUF);
  181. }
  182. #if FF_API_DESTRUCT_PACKET
  183. FF_DISABLE_DEPRECATION_WARNINGS
  184. pkt->destruct = dummy_destruct_packet;
  185. FF_ENABLE_DEPRECATION_WARNINGS
  186. #endif
  187. if (pkt->side_data_elems && dup)
  188. pkt->side_data = src->side_data;
  189. if (pkt->side_data_elems && !dup) {
  190. return av_copy_packet_side_data(pkt, src);
  191. }
  192. return 0;
  193. failed_alloc:
  194. av_destruct_packet(pkt);
  195. return AVERROR(ENOMEM);
  196. }
  197. int av_copy_packet_side_data(AVPacket *pkt, AVPacket *src)
  198. {
  199. if (src->side_data_elems) {
  200. int i;
  201. DUP_DATA(pkt->side_data, src->side_data,
  202. src->side_data_elems * sizeof(*src->side_data), 0, ALLOC_MALLOC);
  203. memset(pkt->side_data, 0,
  204. src->side_data_elems * sizeof(*src->side_data));
  205. for (i = 0; i < src->side_data_elems; i++) {
  206. DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
  207. src->side_data[i].size, 1, ALLOC_MALLOC);
  208. pkt->side_data[i].size = src->side_data[i].size;
  209. pkt->side_data[i].type = src->side_data[i].type;
  210. }
  211. }
  212. return 0;
  213. failed_alloc:
  214. av_destruct_packet(pkt);
  215. return AVERROR(ENOMEM);
  216. }
  217. int av_dup_packet(AVPacket *pkt)
  218. {
  219. AVPacket tmp_pkt;
  220. FF_DISABLE_DEPRECATION_WARNINGS
  221. if (!pkt->buf && pkt->data
  222. #if FF_API_DESTRUCT_PACKET
  223. && !pkt->destruct
  224. #endif
  225. ) {
  226. FF_ENABLE_DEPRECATION_WARNINGS
  227. tmp_pkt = *pkt;
  228. return copy_packet_data(pkt, &tmp_pkt, 1);
  229. }
  230. return 0;
  231. }
  232. int av_copy_packet(AVPacket *dst, AVPacket *src)
  233. {
  234. *dst = *src;
  235. return copy_packet_data(dst, src, 0);
  236. }
  237. void av_free_packet(AVPacket *pkt)
  238. {
  239. if (pkt) {
  240. int i;
  241. FF_DISABLE_DEPRECATION_WARNINGS
  242. if (pkt->buf)
  243. av_buffer_unref(&pkt->buf);
  244. #if FF_API_DESTRUCT_PACKET
  245. else if (pkt->destruct)
  246. pkt->destruct(pkt);
  247. pkt->destruct = NULL;
  248. #endif
  249. FF_ENABLE_DEPRECATION_WARNINGS
  250. pkt->data = NULL;
  251. pkt->size = 0;
  252. for (i = 0; i < pkt->side_data_elems; i++)
  253. av_free(pkt->side_data[i].data);
  254. av_freep(&pkt->side_data);
  255. pkt->side_data_elems = 0;
  256. }
  257. }
  258. uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  259. int size)
  260. {
  261. int elems = pkt->side_data_elems;
  262. if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
  263. return NULL;
  264. if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  265. return NULL;
  266. pkt->side_data = av_realloc(pkt->side_data,
  267. (elems + 1) * sizeof(*pkt->side_data));
  268. if (!pkt->side_data)
  269. return NULL;
  270. pkt->side_data[elems].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  271. if (!pkt->side_data[elems].data)
  272. return NULL;
  273. pkt->side_data[elems].size = size;
  274. pkt->side_data[elems].type = type;
  275. pkt->side_data_elems++;
  276. return pkt->side_data[elems].data;
  277. }
  278. uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  279. int *size)
  280. {
  281. int i;
  282. for (i = 0; i < pkt->side_data_elems; i++) {
  283. if (pkt->side_data[i].type == type) {
  284. if (size)
  285. *size = pkt->side_data[i].size;
  286. return pkt->side_data[i].data;
  287. }
  288. }
  289. return NULL;
  290. }
  291. #define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
  292. int av_packet_merge_side_data(AVPacket *pkt){
  293. if(pkt->side_data_elems){
  294. AVBufferRef *buf;
  295. int i;
  296. uint8_t *p;
  297. uint64_t size= pkt->size + 8LL + FF_INPUT_BUFFER_PADDING_SIZE;
  298. AVPacket old= *pkt;
  299. for (i=0; i<old.side_data_elems; i++) {
  300. size += old.side_data[i].size + 5LL;
  301. }
  302. if (size > INT_MAX)
  303. return AVERROR(EINVAL);
  304. buf = av_buffer_alloc(size);
  305. if (!buf)
  306. return AVERROR(ENOMEM);
  307. pkt->buf = buf;
  308. pkt->data = p = buf->data;
  309. #if FF_API_DESTRUCT_PACKET
  310. FF_DISABLE_DEPRECATION_WARNINGS
  311. pkt->destruct = dummy_destruct_packet;
  312. FF_ENABLE_DEPRECATION_WARNINGS
  313. #endif
  314. pkt->size = size - FF_INPUT_BUFFER_PADDING_SIZE;
  315. bytestream_put_buffer(&p, old.data, old.size);
  316. for (i=old.side_data_elems-1; i>=0; i--) {
  317. bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
  318. bytestream_put_be32(&p, old.side_data[i].size);
  319. *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
  320. }
  321. bytestream_put_be64(&p, FF_MERGE_MARKER);
  322. av_assert0(p-pkt->data == pkt->size);
  323. memset(p, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  324. av_free_packet(&old);
  325. pkt->side_data_elems = 0;
  326. pkt->side_data = NULL;
  327. return 1;
  328. }
  329. return 0;
  330. }
  331. int av_packet_split_side_data(AVPacket *pkt){
  332. if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
  333. int i;
  334. unsigned int size, orig_pktsize = pkt->size;
  335. uint8_t *p;
  336. p = pkt->data + pkt->size - 8 - 5;
  337. for (i=1; ; i++){
  338. size = AV_RB32(p);
  339. if (size>INT_MAX || p - pkt->data < size)
  340. return 0;
  341. if (p[4]&128)
  342. break;
  343. p-= size+5;
  344. }
  345. pkt->side_data = av_malloc(i * sizeof(*pkt->side_data));
  346. if (!pkt->side_data)
  347. return AVERROR(ENOMEM);
  348. p= pkt->data + pkt->size - 8 - 5;
  349. for (i=0; ; i++){
  350. size= AV_RB32(p);
  351. av_assert0(size<=INT_MAX && p - pkt->data >= size);
  352. pkt->side_data[i].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  353. pkt->side_data[i].size = size;
  354. pkt->side_data[i].type = p[4]&127;
  355. if (!pkt->side_data[i].data)
  356. return AVERROR(ENOMEM);
  357. memcpy(pkt->side_data[i].data, p-size, size);
  358. pkt->size -= size + 5;
  359. if(p[4]&128)
  360. break;
  361. p-= size+5;
  362. }
  363. pkt->size -= 8;
  364. /* FFMIN() prevents overflow in case the packet wasn't allocated with
  365. * proper padding.
  366. * If the side data is smaller than the buffer padding size, the
  367. * remaining bytes should have already been filled with zeros by the
  368. * original packet allocation anyway. */
  369. memset(pkt->data + pkt->size, 0,
  370. FFMIN(orig_pktsize - pkt->size, FF_INPUT_BUFFER_PADDING_SIZE));
  371. pkt->side_data_elems = i+1;
  372. return 1;
  373. }
  374. return 0;
  375. }
  376. int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  377. int size)
  378. {
  379. int i;
  380. for (i = 0; i < pkt->side_data_elems; i++) {
  381. if (pkt->side_data[i].type == type) {
  382. if (size > pkt->side_data[i].size)
  383. return AVERROR(ENOMEM);
  384. pkt->side_data[i].size = size;
  385. return 0;
  386. }
  387. }
  388. return AVERROR(ENOENT);
  389. }