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.

565 lines
16KB

  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. int ret;
  64. if ((unsigned)size >= (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
  65. return AVERROR(EINVAL);
  66. ret = av_buffer_realloc(buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
  67. if (ret < 0)
  68. return ret;
  69. memset((*buf)->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  70. return 0;
  71. }
  72. int av_new_packet(AVPacket *pkt, int size)
  73. {
  74. AVBufferRef *buf = NULL;
  75. int ret = packet_alloc(&buf, size);
  76. if (ret < 0)
  77. return ret;
  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, const 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_free_packet(pkt);
  195. return AVERROR(ENOMEM);
  196. }
  197. int av_copy_packet_side_data(AVPacket *pkt, const 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. if (src != pkt) {
  204. memset(pkt->side_data, 0,
  205. src->side_data_elems * sizeof(*src->side_data));
  206. }
  207. for (i = 0; i < src->side_data_elems; i++) {
  208. DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
  209. src->side_data[i].size, 1, ALLOC_MALLOC);
  210. pkt->side_data[i].size = src->side_data[i].size;
  211. pkt->side_data[i].type = src->side_data[i].type;
  212. }
  213. }
  214. pkt->side_data_elems = src->side_data_elems;
  215. return 0;
  216. failed_alloc:
  217. av_free_packet(pkt);
  218. return AVERROR(ENOMEM);
  219. }
  220. int av_dup_packet(AVPacket *pkt)
  221. {
  222. AVPacket tmp_pkt;
  223. FF_DISABLE_DEPRECATION_WARNINGS
  224. if (!pkt->buf && pkt->data
  225. #if FF_API_DESTRUCT_PACKET
  226. && !pkt->destruct
  227. #endif
  228. ) {
  229. FF_ENABLE_DEPRECATION_WARNINGS
  230. tmp_pkt = *pkt;
  231. return copy_packet_data(pkt, &tmp_pkt, 1);
  232. }
  233. return 0;
  234. }
  235. int av_copy_packet(AVPacket *dst, const AVPacket *src)
  236. {
  237. *dst = *src;
  238. return copy_packet_data(dst, src, 0);
  239. }
  240. void av_packet_free_side_data(AVPacket *pkt)
  241. {
  242. int i;
  243. for (i = 0; i < pkt->side_data_elems; i++)
  244. av_free(pkt->side_data[i].data);
  245. av_freep(&pkt->side_data);
  246. pkt->side_data_elems = 0;
  247. }
  248. void av_free_packet(AVPacket *pkt)
  249. {
  250. if (pkt) {
  251. FF_DISABLE_DEPRECATION_WARNINGS
  252. if (pkt->buf)
  253. av_buffer_unref(&pkt->buf);
  254. #if FF_API_DESTRUCT_PACKET
  255. else if (pkt->destruct)
  256. pkt->destruct(pkt);
  257. pkt->destruct = NULL;
  258. #endif
  259. FF_ENABLE_DEPRECATION_WARNINGS
  260. pkt->data = NULL;
  261. pkt->size = 0;
  262. av_packet_free_side_data(pkt);
  263. }
  264. }
  265. uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  266. int size)
  267. {
  268. int elems = pkt->side_data_elems;
  269. if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
  270. return NULL;
  271. if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
  272. return NULL;
  273. pkt->side_data = av_realloc(pkt->side_data,
  274. (elems + 1) * sizeof(*pkt->side_data));
  275. if (!pkt->side_data)
  276. return NULL;
  277. pkt->side_data[elems].data = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  278. if (!pkt->side_data[elems].data)
  279. return NULL;
  280. pkt->side_data[elems].size = size;
  281. pkt->side_data[elems].type = type;
  282. pkt->side_data_elems++;
  283. return pkt->side_data[elems].data;
  284. }
  285. uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  286. int *size)
  287. {
  288. int i;
  289. for (i = 0; i < pkt->side_data_elems; i++) {
  290. if (pkt->side_data[i].type == type) {
  291. if (size)
  292. *size = pkt->side_data[i].size;
  293. return pkt->side_data[i].data;
  294. }
  295. }
  296. return NULL;
  297. }
  298. #define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
  299. int av_packet_merge_side_data(AVPacket *pkt){
  300. if(pkt->side_data_elems){
  301. AVBufferRef *buf;
  302. int i;
  303. uint8_t *p;
  304. uint64_t size= pkt->size + 8LL + FF_INPUT_BUFFER_PADDING_SIZE;
  305. AVPacket old= *pkt;
  306. for (i=0; i<old.side_data_elems; i++) {
  307. size += old.side_data[i].size + 5LL;
  308. }
  309. if (size > INT_MAX)
  310. return AVERROR(EINVAL);
  311. buf = av_buffer_alloc(size);
  312. if (!buf)
  313. return AVERROR(ENOMEM);
  314. pkt->buf = buf;
  315. pkt->data = p = buf->data;
  316. #if FF_API_DESTRUCT_PACKET
  317. FF_DISABLE_DEPRECATION_WARNINGS
  318. pkt->destruct = dummy_destruct_packet;
  319. FF_ENABLE_DEPRECATION_WARNINGS
  320. #endif
  321. pkt->size = size - FF_INPUT_BUFFER_PADDING_SIZE;
  322. bytestream_put_buffer(&p, old.data, old.size);
  323. for (i=old.side_data_elems-1; i>=0; i--) {
  324. bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
  325. bytestream_put_be32(&p, old.side_data[i].size);
  326. *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
  327. }
  328. bytestream_put_be64(&p, FF_MERGE_MARKER);
  329. av_assert0(p-pkt->data == pkt->size);
  330. memset(p, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  331. av_free_packet(&old);
  332. pkt->side_data_elems = 0;
  333. pkt->side_data = NULL;
  334. return 1;
  335. }
  336. return 0;
  337. }
  338. int av_packet_split_side_data(AVPacket *pkt){
  339. if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
  340. int i;
  341. unsigned int size;
  342. uint8_t *p;
  343. p = pkt->data + pkt->size - 8 - 5;
  344. for (i=1; ; i++){
  345. size = AV_RB32(p);
  346. if (size>INT_MAX || p - pkt->data < size)
  347. return 0;
  348. if (p[4]&128)
  349. break;
  350. p-= size+5;
  351. }
  352. pkt->side_data = av_malloc_array(i, sizeof(*pkt->side_data));
  353. if (!pkt->side_data)
  354. return AVERROR(ENOMEM);
  355. p= pkt->data + pkt->size - 8 - 5;
  356. for (i=0; ; i++){
  357. size= AV_RB32(p);
  358. av_assert0(size<=INT_MAX && p - pkt->data >= size);
  359. pkt->side_data[i].data = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
  360. pkt->side_data[i].size = size;
  361. pkt->side_data[i].type = p[4]&127;
  362. if (!pkt->side_data[i].data)
  363. return AVERROR(ENOMEM);
  364. memcpy(pkt->side_data[i].data, p-size, size);
  365. pkt->size -= size + 5;
  366. if(p[4]&128)
  367. break;
  368. p-= size+5;
  369. }
  370. pkt->size -= 8;
  371. pkt->side_data_elems = i+1;
  372. return 1;
  373. }
  374. return 0;
  375. }
  376. uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size)
  377. {
  378. AVDictionaryEntry *t = NULL;
  379. uint8_t *data = NULL;
  380. *size = 0;
  381. if (!dict)
  382. return NULL;
  383. while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX))) {
  384. const size_t keylen = strlen(t->key);
  385. const size_t valuelen = strlen(t->value);
  386. const size_t new_size = *size + keylen + 1 + valuelen + 1;
  387. uint8_t *const new_data = av_realloc(data, new_size);
  388. if (!new_data)
  389. goto fail;
  390. data = new_data;
  391. if (new_size > INT_MAX)
  392. goto fail;
  393. memcpy(data + *size, t->key, keylen + 1);
  394. memcpy(data + *size + keylen + 1, t->value, valuelen + 1);
  395. *size = new_size;
  396. }
  397. return data;
  398. fail:
  399. av_freep(&data);
  400. *size = 0;
  401. return NULL;
  402. }
  403. int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict)
  404. {
  405. const uint8_t *end = data + size;
  406. int ret = 0;
  407. if (!dict || !data || !size)
  408. return ret;
  409. if (size && end[-1])
  410. return AVERROR_INVALIDDATA;
  411. while (data < end) {
  412. const uint8_t *key = data;
  413. const uint8_t *val = data + strlen(key) + 1;
  414. if (val >= end)
  415. return AVERROR_INVALIDDATA;
  416. ret = av_dict_set(dict, key, val, 0);
  417. if (ret < 0)
  418. break;
  419. data = val + strlen(val) + 1;
  420. }
  421. return ret;
  422. }
  423. int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  424. int size)
  425. {
  426. int i;
  427. for (i = 0; i < pkt->side_data_elems; i++) {
  428. if (pkt->side_data[i].type == type) {
  429. if (size > pkt->side_data[i].size)
  430. return AVERROR(ENOMEM);
  431. pkt->side_data[i].size = size;
  432. return 0;
  433. }
  434. }
  435. return AVERROR(ENOENT);
  436. }
  437. int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
  438. {
  439. int i;
  440. dst->pts = src->pts;
  441. dst->dts = src->dts;
  442. dst->pos = src->pos;
  443. dst->duration = src->duration;
  444. dst->convergence_duration = src->convergence_duration;
  445. dst->flags = src->flags;
  446. dst->stream_index = src->stream_index;
  447. for (i = 0; i < src->side_data_elems; i++) {
  448. enum AVPacketSideDataType type = src->side_data[i].type;
  449. int size = src->side_data[i].size;
  450. uint8_t *src_data = src->side_data[i].data;
  451. uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
  452. if (!dst_data) {
  453. av_packet_free_side_data(dst);
  454. return AVERROR(ENOMEM);
  455. }
  456. memcpy(dst_data, src_data, size);
  457. }
  458. return 0;
  459. }
  460. void av_packet_unref(AVPacket *pkt)
  461. {
  462. av_packet_free_side_data(pkt);
  463. av_buffer_unref(&pkt->buf);
  464. av_init_packet(pkt);
  465. pkt->data = NULL;
  466. pkt->size = 0;
  467. }
  468. int av_packet_ref(AVPacket *dst, const AVPacket *src)
  469. {
  470. int ret;
  471. ret = av_packet_copy_props(dst, src);
  472. if (ret < 0)
  473. return ret;
  474. if (!src->buf) {
  475. ret = packet_alloc(&dst->buf, src->size);
  476. if (ret < 0)
  477. goto fail;
  478. memcpy(dst->buf->data, src->data, src->size);
  479. } else
  480. dst->buf = av_buffer_ref(src->buf);
  481. dst->size = src->size;
  482. dst->data = dst->buf->data;
  483. return 0;
  484. fail:
  485. av_packet_free_side_data(dst);
  486. return ret;
  487. }
  488. void av_packet_move_ref(AVPacket *dst, AVPacket *src)
  489. {
  490. *dst = *src;
  491. av_init_packet(src);
  492. }