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.

511 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_free_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. if (src != pkt) {
  203. memset(pkt->side_data, 0,
  204. src->side_data_elems * sizeof(*src->side_data));
  205. }
  206. for (i = 0; i < src->side_data_elems; i++) {
  207. DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
  208. src->side_data[i].size, 1, ALLOC_MALLOC);
  209. pkt->side_data[i].size = src->side_data[i].size;
  210. pkt->side_data[i].type = src->side_data[i].type;
  211. }
  212. }
  213. return 0;
  214. failed_alloc:
  215. av_free_packet(pkt);
  216. return AVERROR(ENOMEM);
  217. }
  218. int av_dup_packet(AVPacket *pkt)
  219. {
  220. AVPacket tmp_pkt;
  221. FF_DISABLE_DEPRECATION_WARNINGS
  222. if (!pkt->buf && pkt->data
  223. #if FF_API_DESTRUCT_PACKET
  224. && !pkt->destruct
  225. #endif
  226. ) {
  227. FF_ENABLE_DEPRECATION_WARNINGS
  228. tmp_pkt = *pkt;
  229. return copy_packet_data(pkt, &tmp_pkt, 1);
  230. }
  231. return 0;
  232. }
  233. int av_copy_packet(AVPacket *dst, AVPacket *src)
  234. {
  235. *dst = *src;
  236. return copy_packet_data(dst, src, 0);
  237. }
  238. void av_packet_free_side_data(AVPacket *pkt)
  239. {
  240. int i;
  241. for (i = 0; i < pkt->side_data_elems; i++)
  242. av_free(pkt->side_data[i].data);
  243. av_freep(&pkt->side_data);
  244. pkt->side_data_elems = 0;
  245. }
  246. void av_free_packet(AVPacket *pkt)
  247. {
  248. if (pkt) {
  249. FF_DISABLE_DEPRECATION_WARNINGS
  250. if (pkt->buf)
  251. av_buffer_unref(&pkt->buf);
  252. #if FF_API_DESTRUCT_PACKET
  253. else if (pkt->destruct)
  254. pkt->destruct(pkt);
  255. pkt->destruct = NULL;
  256. #endif
  257. FF_ENABLE_DEPRECATION_WARNINGS
  258. pkt->data = NULL;
  259. pkt->size = 0;
  260. av_packet_free_side_data(pkt);
  261. }
  262. }
  263. uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  264. int size)
  265. {
  266. int elems = pkt->side_data_elems;
  267. if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
  268. return NULL;
  269. if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  270. return NULL;
  271. pkt->side_data = av_realloc(pkt->side_data,
  272. (elems + 1) * sizeof(*pkt->side_data));
  273. if (!pkt->side_data)
  274. return NULL;
  275. pkt->side_data[elems].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  276. if (!pkt->side_data[elems].data)
  277. return NULL;
  278. pkt->side_data[elems].size = size;
  279. pkt->side_data[elems].type = type;
  280. pkt->side_data_elems++;
  281. return pkt->side_data[elems].data;
  282. }
  283. uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  284. int *size)
  285. {
  286. int i;
  287. for (i = 0; i < pkt->side_data_elems; i++) {
  288. if (pkt->side_data[i].type == type) {
  289. if (size)
  290. *size = pkt->side_data[i].size;
  291. return pkt->side_data[i].data;
  292. }
  293. }
  294. return NULL;
  295. }
  296. #define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
  297. int av_packet_merge_side_data(AVPacket *pkt){
  298. if(pkt->side_data_elems){
  299. AVBufferRef *buf;
  300. int i;
  301. uint8_t *p;
  302. uint64_t size= pkt->size + 8LL + FF_INPUT_BUFFER_PADDING_SIZE;
  303. AVPacket old= *pkt;
  304. for (i=0; i<old.side_data_elems; i++) {
  305. size += old.side_data[i].size + 5LL;
  306. }
  307. if (size > INT_MAX)
  308. return AVERROR(EINVAL);
  309. buf = av_buffer_alloc(size);
  310. if (!buf)
  311. return AVERROR(ENOMEM);
  312. pkt->buf = buf;
  313. pkt->data = p = buf->data;
  314. #if FF_API_DESTRUCT_PACKET
  315. FF_DISABLE_DEPRECATION_WARNINGS
  316. pkt->destruct = dummy_destruct_packet;
  317. FF_ENABLE_DEPRECATION_WARNINGS
  318. #endif
  319. pkt->size = size - FF_INPUT_BUFFER_PADDING_SIZE;
  320. bytestream_put_buffer(&p, old.data, old.size);
  321. for (i=old.side_data_elems-1; i>=0; i--) {
  322. bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
  323. bytestream_put_be32(&p, old.side_data[i].size);
  324. *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
  325. }
  326. bytestream_put_be64(&p, FF_MERGE_MARKER);
  327. av_assert0(p-pkt->data == pkt->size);
  328. memset(p, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  329. av_free_packet(&old);
  330. pkt->side_data_elems = 0;
  331. pkt->side_data = NULL;
  332. return 1;
  333. }
  334. return 0;
  335. }
  336. int av_packet_split_side_data(AVPacket *pkt){
  337. if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
  338. int i;
  339. unsigned int size, orig_pktsize = pkt->size;
  340. uint8_t *p;
  341. p = pkt->data + pkt->size - 8 - 5;
  342. for (i=1; ; i++){
  343. size = AV_RB32(p);
  344. if (size>INT_MAX || p - pkt->data < size)
  345. return 0;
  346. if (p[4]&128)
  347. break;
  348. p-= size+5;
  349. }
  350. pkt->side_data = av_malloc(i * sizeof(*pkt->side_data));
  351. if (!pkt->side_data)
  352. return AVERROR(ENOMEM);
  353. p= pkt->data + pkt->size - 8 - 5;
  354. for (i=0; ; i++){
  355. size= AV_RB32(p);
  356. av_assert0(size<=INT_MAX && p - pkt->data >= size);
  357. pkt->side_data[i].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  358. pkt->side_data[i].size = size;
  359. pkt->side_data[i].type = p[4]&127;
  360. if (!pkt->side_data[i].data)
  361. return AVERROR(ENOMEM);
  362. memcpy(pkt->side_data[i].data, p-size, size);
  363. pkt->size -= size + 5;
  364. if(p[4]&128)
  365. break;
  366. p-= size+5;
  367. }
  368. pkt->size -= 8;
  369. /* FFMIN() prevents overflow in case the packet wasn't allocated with
  370. * proper padding.
  371. * If the side data is smaller than the buffer padding size, the
  372. * remaining bytes should have already been filled with zeros by the
  373. * original packet allocation anyway. */
  374. memset(pkt->data + pkt->size, 0,
  375. FFMIN(orig_pktsize - pkt->size, FF_INPUT_BUFFER_PADDING_SIZE));
  376. pkt->side_data_elems = i+1;
  377. return 1;
  378. }
  379. return 0;
  380. }
  381. int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  382. int size)
  383. {
  384. int i;
  385. for (i = 0; i < pkt->side_data_elems; i++) {
  386. if (pkt->side_data[i].type == type) {
  387. if (size > pkt->side_data[i].size)
  388. return AVERROR(ENOMEM);
  389. pkt->side_data[i].size = size;
  390. return 0;
  391. }
  392. }
  393. return AVERROR(ENOENT);
  394. }
  395. int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
  396. {
  397. int i;
  398. dst->pts = src->pts;
  399. dst->dts = src->dts;
  400. dst->pos = src->pos;
  401. dst->duration = src->duration;
  402. dst->convergence_duration = src->convergence_duration;
  403. dst->flags = src->flags;
  404. dst->stream_index = src->stream_index;
  405. dst->side_data_elems = src->side_data_elems;
  406. for (i = 0; i < src->side_data_elems; i++) {
  407. enum AVPacketSideDataType type = src->side_data[i].type;
  408. int size = src->side_data[i].size;
  409. uint8_t *src_data = src->side_data[i].data;
  410. uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
  411. if (!dst_data) {
  412. av_packet_free_side_data(dst);
  413. return AVERROR(ENOMEM);
  414. }
  415. memcpy(dst_data, src_data, size);
  416. }
  417. return 0;
  418. }
  419. void av_packet_unref(AVPacket *pkt)
  420. {
  421. av_packet_free_side_data(pkt);
  422. av_buffer_unref(&pkt->buf);
  423. av_init_packet(pkt);
  424. pkt->data = NULL;
  425. pkt->size = 0;
  426. }
  427. int av_packet_ref(AVPacket *dst, AVPacket *src)
  428. {
  429. int ret;
  430. ret = av_packet_copy_props(dst, src);
  431. if (ret < 0)
  432. return ret;
  433. if (!src->buf) {
  434. ret = packet_alloc(&dst->buf, src->size);
  435. if (ret < 0)
  436. goto fail;
  437. memcpy(dst->buf->data, src->data, src->size);
  438. } else
  439. dst->buf = av_buffer_ref(src->buf);
  440. dst->size = src->size;
  441. dst->data = dst->buf->data;
  442. return 0;
  443. fail:
  444. av_packet_free_side_data(dst);
  445. return ret;
  446. }
  447. void av_packet_move_ref(AVPacket *dst, AVPacket *src)
  448. {
  449. *dst = *src;
  450. av_init_packet(src);
  451. }