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.

584 lines
17KB

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