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.

509 lines
15KB

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