2
0

avpacket.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. void av_init_packet(AVPacket *pkt)
  31. {
  32. pkt->pts = AV_NOPTS_VALUE;
  33. pkt->dts = AV_NOPTS_VALUE;
  34. pkt->pos = -1;
  35. pkt->duration = 0;
  36. #if FF_API_CONVERGENCE_DURATION
  37. FF_DISABLE_DEPRECATION_WARNINGS
  38. pkt->convergence_duration = 0;
  39. FF_ENABLE_DEPRECATION_WARNINGS
  40. #endif
  41. pkt->flags = 0;
  42. pkt->stream_index = 0;
  43. pkt->buf = NULL;
  44. pkt->side_data = NULL;
  45. pkt->side_data_elems = 0;
  46. }
  47. AVPacket *av_packet_alloc(void)
  48. {
  49. AVPacket *pkt = av_mallocz(sizeof(AVPacket));
  50. if (!pkt)
  51. return pkt;
  52. av_packet_unref(pkt);
  53. return pkt;
  54. }
  55. void av_packet_free(AVPacket **pkt)
  56. {
  57. if (!pkt || !*pkt)
  58. return;
  59. av_packet_unref(*pkt);
  60. av_freep(pkt);
  61. }
  62. static int packet_alloc(AVBufferRef **buf, int size)
  63. {
  64. int ret;
  65. if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
  66. return AVERROR(EINVAL);
  67. ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
  68. if (ret < 0)
  69. return ret;
  70. memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  71. return 0;
  72. }
  73. int av_new_packet(AVPacket *pkt, int size)
  74. {
  75. AVBufferRef *buf = NULL;
  76. int ret = packet_alloc(&buf, size);
  77. if (ret < 0)
  78. return ret;
  79. av_init_packet(pkt);
  80. pkt->buf = buf;
  81. pkt->data = buf->data;
  82. pkt->size = size;
  83. return 0;
  84. }
  85. void av_shrink_packet(AVPacket *pkt, int size)
  86. {
  87. if (pkt->size <= size)
  88. return;
  89. pkt->size = size;
  90. memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  91. }
  92. int av_grow_packet(AVPacket *pkt, int grow_by)
  93. {
  94. int new_size;
  95. av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
  96. if ((unsigned)grow_by >
  97. INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
  98. return -1;
  99. new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
  100. if (pkt->buf) {
  101. size_t data_offset;
  102. uint8_t *old_data = pkt->data;
  103. if (pkt->data == NULL) {
  104. data_offset = 0;
  105. pkt->data = pkt->buf->data;
  106. } else {
  107. data_offset = pkt->data - pkt->buf->data;
  108. if (data_offset > INT_MAX - new_size)
  109. return -1;
  110. }
  111. if (new_size + data_offset > pkt->buf->size) {
  112. int ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
  113. if (ret < 0) {
  114. pkt->data = old_data;
  115. return ret;
  116. }
  117. pkt->data = pkt->buf->data + data_offset;
  118. }
  119. } else {
  120. pkt->buf = av_buffer_alloc(new_size);
  121. if (!pkt->buf)
  122. return AVERROR(ENOMEM);
  123. if (pkt->size > 0)
  124. memcpy(pkt->buf->data, pkt->data, pkt->size);
  125. pkt->data = pkt->buf->data;
  126. }
  127. pkt->size += grow_by;
  128. memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  129. return 0;
  130. }
  131. int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
  132. {
  133. if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
  134. return AVERROR(EINVAL);
  135. pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
  136. av_buffer_default_free, NULL, 0);
  137. if (!pkt->buf)
  138. return AVERROR(ENOMEM);
  139. pkt->data = data;
  140. pkt->size = size;
  141. return 0;
  142. }
  143. #if FF_API_AVPACKET_OLD_API
  144. FF_DISABLE_DEPRECATION_WARNINGS
  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) + AV_INPUT_BUFFER_PADDING_SIZE) \
  157. goto failed_alloc; \
  158. ALLOC(data, size + AV_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. AV_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 (src->side_data_elems && dup) {
  186. pkt->side_data = src->side_data;
  187. pkt->side_data_elems = src->side_data_elems;
  188. }
  189. if (src->side_data_elems && !dup) {
  190. return av_copy_packet_side_data(pkt, src);
  191. }
  192. return 0;
  193. failed_alloc:
  194. av_packet_unref(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_packet_unref(pkt);
  218. return AVERROR(ENOMEM);
  219. }
  220. int av_dup_packet(AVPacket *pkt)
  221. {
  222. AVPacket tmp_pkt;
  223. if (!pkt->buf && pkt->data) {
  224. tmp_pkt = *pkt;
  225. return copy_packet_data(pkt, &tmp_pkt, 1);
  226. }
  227. return 0;
  228. }
  229. int av_copy_packet(AVPacket *dst, const AVPacket *src)
  230. {
  231. *dst = *src;
  232. return copy_packet_data(dst, src, 0);
  233. }
  234. FF_ENABLE_DEPRECATION_WARNINGS
  235. #endif
  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_freep(&pkt->side_data[i].data);
  241. av_freep(&pkt->side_data);
  242. pkt->side_data_elems = 0;
  243. }
  244. #if FF_API_AVPACKET_OLD_API
  245. FF_DISABLE_DEPRECATION_WARNINGS
  246. void av_free_packet(AVPacket *pkt)
  247. {
  248. if (pkt) {
  249. if (pkt->buf)
  250. av_buffer_unref(&pkt->buf);
  251. pkt->data = NULL;
  252. pkt->size = 0;
  253. av_packet_free_side_data(pkt);
  254. }
  255. }
  256. FF_ENABLE_DEPRECATION_WARNINGS
  257. #endif
  258. int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  259. uint8_t *data, size_t size)
  260. {
  261. AVPacketSideData *tmp;
  262. int i, elems = pkt->side_data_elems;
  263. for (i = 0; i < elems; i++) {
  264. AVPacketSideData *sd = &pkt->side_data[i];
  265. if (sd->type == type) {
  266. av_free(sd->data);
  267. sd->data = data;
  268. sd->size = size;
  269. return 0;
  270. }
  271. }
  272. if ((unsigned)elems + 1 > AV_PKT_DATA_NB)
  273. return AVERROR(ERANGE);
  274. tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
  275. if (!tmp)
  276. return AVERROR(ENOMEM);
  277. pkt->side_data = tmp;
  278. pkt->side_data[elems].data = data;
  279. pkt->side_data[elems].size = size;
  280. pkt->side_data[elems].type = type;
  281. pkt->side_data_elems++;
  282. return 0;
  283. }
  284. uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  285. int size)
  286. {
  287. int ret;
  288. uint8_t *data;
  289. if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
  290. return NULL;
  291. data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
  292. if (!data)
  293. return NULL;
  294. ret = av_packet_add_side_data(pkt, type, data, size);
  295. if (ret < 0) {
  296. av_freep(&data);
  297. return NULL;
  298. }
  299. return data;
  300. }
  301. uint8_t *av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
  302. int *size)
  303. {
  304. int i;
  305. for (i = 0; i < pkt->side_data_elems; i++) {
  306. if (pkt->side_data[i].type == type) {
  307. if (size)
  308. *size = pkt->side_data[i].size;
  309. return pkt->side_data[i].data;
  310. }
  311. }
  312. if (size)
  313. *size = 0;
  314. return NULL;
  315. }
  316. const char *av_packet_side_data_name(enum AVPacketSideDataType type)
  317. {
  318. switch(type) {
  319. case AV_PKT_DATA_PALETTE: return "Palette";
  320. case AV_PKT_DATA_NEW_EXTRADATA: return "New Extradata";
  321. case AV_PKT_DATA_PARAM_CHANGE: return "Param Change";
  322. case AV_PKT_DATA_H263_MB_INFO: return "H263 MB Info";
  323. case AV_PKT_DATA_REPLAYGAIN: return "Replay Gain";
  324. case AV_PKT_DATA_DISPLAYMATRIX: return "Display Matrix";
  325. case AV_PKT_DATA_STEREO3D: return "Stereo 3D";
  326. case AV_PKT_DATA_AUDIO_SERVICE_TYPE: return "Audio Service Type";
  327. case AV_PKT_DATA_QUALITY_STATS: return "Quality stats";
  328. case AV_PKT_DATA_FALLBACK_TRACK: return "Fallback track";
  329. case AV_PKT_DATA_CPB_PROPERTIES: return "CPB properties";
  330. case AV_PKT_DATA_SKIP_SAMPLES: return "Skip Samples";
  331. case AV_PKT_DATA_JP_DUALMONO: return "JP Dual Mono";
  332. case AV_PKT_DATA_STRINGS_METADATA: return "Strings Metadata";
  333. case AV_PKT_DATA_SUBTITLE_POSITION: return "Subtitle Position";
  334. case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL: return "Matroska BlockAdditional";
  335. case AV_PKT_DATA_WEBVTT_IDENTIFIER: return "WebVTT ID";
  336. case AV_PKT_DATA_WEBVTT_SETTINGS: return "WebVTT Settings";
  337. case AV_PKT_DATA_METADATA_UPDATE: return "Metadata Update";
  338. case AV_PKT_DATA_MPEGTS_STREAM_ID: return "MPEGTS Stream ID";
  339. case AV_PKT_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
  340. case AV_PKT_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
  341. case AV_PKT_DATA_SPHERICAL: return "Spherical Mapping";
  342. case AV_PKT_DATA_A53_CC: return "A53 Closed Captions";
  343. case AV_PKT_DATA_ENCRYPTION_INIT_INFO: return "Encryption initialization data";
  344. case AV_PKT_DATA_ENCRYPTION_INFO: return "Encryption info";
  345. case AV_PKT_DATA_AFD: return "Active Format Description data";
  346. }
  347. return NULL;
  348. }
  349. #if FF_API_MERGE_SD_API
  350. #define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
  351. int av_packet_merge_side_data(AVPacket *pkt){
  352. if(pkt->side_data_elems){
  353. AVBufferRef *buf;
  354. int i;
  355. uint8_t *p;
  356. uint64_t size= pkt->size + 8LL + AV_INPUT_BUFFER_PADDING_SIZE;
  357. AVPacket old= *pkt;
  358. for (i=0; i<old.side_data_elems; i++) {
  359. size += old.side_data[i].size + 5LL;
  360. }
  361. if (size > INT_MAX)
  362. return AVERROR(EINVAL);
  363. buf = av_buffer_alloc(size);
  364. if (!buf)
  365. return AVERROR(ENOMEM);
  366. pkt->buf = buf;
  367. pkt->data = p = buf->data;
  368. pkt->size = size - AV_INPUT_BUFFER_PADDING_SIZE;
  369. bytestream_put_buffer(&p, old.data, old.size);
  370. for (i=old.side_data_elems-1; i>=0; i--) {
  371. bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
  372. bytestream_put_be32(&p, old.side_data[i].size);
  373. *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
  374. }
  375. bytestream_put_be64(&p, FF_MERGE_MARKER);
  376. av_assert0(p-pkt->data == pkt->size);
  377. memset(p, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  378. av_packet_unref(&old);
  379. pkt->side_data_elems = 0;
  380. pkt->side_data = NULL;
  381. return 1;
  382. }
  383. return 0;
  384. }
  385. int av_packet_split_side_data(AVPacket *pkt){
  386. if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
  387. int i;
  388. unsigned int size;
  389. uint8_t *p;
  390. p = pkt->data + pkt->size - 8 - 5;
  391. for (i=1; ; i++){
  392. size = AV_RB32(p);
  393. if (size>INT_MAX - 5 || p - pkt->data < size)
  394. return 0;
  395. if (p[4]&128)
  396. break;
  397. if (p - pkt->data < size + 5)
  398. return 0;
  399. p-= size+5;
  400. }
  401. if (i > AV_PKT_DATA_NB)
  402. return AVERROR(ERANGE);
  403. pkt->side_data = av_malloc_array(i, sizeof(*pkt->side_data));
  404. if (!pkt->side_data)
  405. return AVERROR(ENOMEM);
  406. p= pkt->data + pkt->size - 8 - 5;
  407. for (i=0; ; i++){
  408. size= AV_RB32(p);
  409. av_assert0(size<=INT_MAX - 5 && p - pkt->data >= size);
  410. pkt->side_data[i].data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
  411. pkt->side_data[i].size = size;
  412. pkt->side_data[i].type = p[4]&127;
  413. if (!pkt->side_data[i].data)
  414. return AVERROR(ENOMEM);
  415. memcpy(pkt->side_data[i].data, p-size, size);
  416. pkt->size -= size + 5;
  417. if(p[4]&128)
  418. break;
  419. p-= size+5;
  420. }
  421. pkt->size -= 8;
  422. pkt->side_data_elems = i+1;
  423. return 1;
  424. }
  425. return 0;
  426. }
  427. #endif
  428. uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size)
  429. {
  430. AVDictionaryEntry *t = NULL;
  431. uint8_t *data = NULL;
  432. *size = 0;
  433. if (!dict)
  434. return NULL;
  435. while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX))) {
  436. const size_t keylen = strlen(t->key);
  437. const size_t valuelen = strlen(t->value);
  438. const size_t new_size = *size + keylen + 1 + valuelen + 1;
  439. uint8_t *const new_data = av_realloc(data, new_size);
  440. if (!new_data)
  441. goto fail;
  442. data = new_data;
  443. if (new_size > INT_MAX)
  444. goto fail;
  445. memcpy(data + *size, t->key, keylen + 1);
  446. memcpy(data + *size + keylen + 1, t->value, valuelen + 1);
  447. *size = new_size;
  448. }
  449. return data;
  450. fail:
  451. av_freep(&data);
  452. *size = 0;
  453. return NULL;
  454. }
  455. int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict)
  456. {
  457. const uint8_t *end = data + size;
  458. int ret = 0;
  459. if (!dict || !data || !size)
  460. return ret;
  461. if (size && end[-1])
  462. return AVERROR_INVALIDDATA;
  463. while (data < end) {
  464. const uint8_t *key = data;
  465. const uint8_t *val = data + strlen(key) + 1;
  466. if (val >= end || !*key)
  467. return AVERROR_INVALIDDATA;
  468. ret = av_dict_set(dict, key, val, 0);
  469. if (ret < 0)
  470. break;
  471. data = val + strlen(val) + 1;
  472. }
  473. return ret;
  474. }
  475. int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  476. int size)
  477. {
  478. int i;
  479. for (i = 0; i < pkt->side_data_elems; i++) {
  480. if (pkt->side_data[i].type == type) {
  481. if (size > pkt->side_data[i].size)
  482. return AVERROR(ENOMEM);
  483. pkt->side_data[i].size = size;
  484. return 0;
  485. }
  486. }
  487. return AVERROR(ENOENT);
  488. }
  489. int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
  490. {
  491. int i;
  492. dst->pts = src->pts;
  493. dst->dts = src->dts;
  494. dst->pos = src->pos;
  495. dst->duration = src->duration;
  496. #if FF_API_CONVERGENCE_DURATION
  497. FF_DISABLE_DEPRECATION_WARNINGS
  498. dst->convergence_duration = src->convergence_duration;
  499. FF_ENABLE_DEPRECATION_WARNINGS
  500. #endif
  501. dst->flags = src->flags;
  502. dst->stream_index = src->stream_index;
  503. dst->side_data = NULL;
  504. dst->side_data_elems = 0;
  505. for (i = 0; i < src->side_data_elems; i++) {
  506. enum AVPacketSideDataType type = src->side_data[i].type;
  507. int size = src->side_data[i].size;
  508. uint8_t *src_data = src->side_data[i].data;
  509. uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
  510. if (!dst_data) {
  511. av_packet_free_side_data(dst);
  512. return AVERROR(ENOMEM);
  513. }
  514. memcpy(dst_data, src_data, size);
  515. }
  516. return 0;
  517. }
  518. void av_packet_unref(AVPacket *pkt)
  519. {
  520. av_packet_free_side_data(pkt);
  521. av_buffer_unref(&pkt->buf);
  522. av_init_packet(pkt);
  523. pkt->data = NULL;
  524. pkt->size = 0;
  525. }
  526. int av_packet_ref(AVPacket *dst, const AVPacket *src)
  527. {
  528. int ret;
  529. ret = av_packet_copy_props(dst, src);
  530. if (ret < 0)
  531. return ret;
  532. if (!src->buf) {
  533. ret = packet_alloc(&dst->buf, src->size);
  534. if (ret < 0)
  535. goto fail;
  536. if (src->size)
  537. memcpy(dst->buf->data, src->data, src->size);
  538. dst->data = dst->buf->data;
  539. } else {
  540. dst->buf = av_buffer_ref(src->buf);
  541. if (!dst->buf) {
  542. ret = AVERROR(ENOMEM);
  543. goto fail;
  544. }
  545. dst->data = src->data;
  546. }
  547. dst->size = src->size;
  548. return 0;
  549. fail:
  550. av_packet_free_side_data(dst);
  551. return ret;
  552. }
  553. AVPacket *av_packet_clone(const AVPacket *src)
  554. {
  555. AVPacket *ret = av_packet_alloc();
  556. if (!ret)
  557. return ret;
  558. if (av_packet_ref(ret, src))
  559. av_packet_free(&ret);
  560. return ret;
  561. }
  562. void av_packet_move_ref(AVPacket *dst, AVPacket *src)
  563. {
  564. *dst = *src;
  565. av_init_packet(src);
  566. src->data = NULL;
  567. src->size = 0;
  568. }
  569. int av_packet_make_refcounted(AVPacket *pkt)
  570. {
  571. int ret;
  572. if (pkt->buf)
  573. return 0;
  574. ret = packet_alloc(&pkt->buf, pkt->size);
  575. if (ret < 0)
  576. return ret;
  577. if (pkt->size)
  578. memcpy(pkt->buf->data, pkt->data, pkt->size);
  579. pkt->data = pkt->buf->data;
  580. return 0;
  581. }
  582. int av_packet_make_writable(AVPacket *pkt)
  583. {
  584. AVBufferRef *buf = NULL;
  585. int ret;
  586. if (pkt->buf && av_buffer_is_writable(pkt->buf))
  587. return 0;
  588. ret = packet_alloc(&buf, pkt->size);
  589. if (ret < 0)
  590. return ret;
  591. if (pkt->size)
  592. memcpy(buf->data, pkt->data, pkt->size);
  593. av_buffer_unref(&pkt->buf);
  594. pkt->buf = buf;
  595. pkt->data = buf->data;
  596. return 0;
  597. }
  598. void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
  599. {
  600. if (pkt->pts != AV_NOPTS_VALUE)
  601. pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
  602. if (pkt->dts != AV_NOPTS_VALUE)
  603. pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
  604. if (pkt->duration > 0)
  605. pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
  606. #if FF_API_CONVERGENCE_DURATION
  607. FF_DISABLE_DEPRECATION_WARNINGS
  608. if (pkt->convergence_duration > 0)
  609. pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
  610. FF_ENABLE_DEPRECATION_WARNINGS
  611. #endif
  612. }
  613. int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
  614. {
  615. uint8_t *side_data;
  616. int side_data_size;
  617. int i;
  618. side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
  619. if (!side_data) {
  620. side_data_size = 4+4+8*error_count;
  621. side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
  622. side_data_size);
  623. }
  624. if (!side_data || side_data_size < 4+4+8*error_count)
  625. return AVERROR(ENOMEM);
  626. AV_WL32(side_data , quality );
  627. side_data[4] = pict_type;
  628. side_data[5] = error_count;
  629. for (i = 0; i<error_count; i++)
  630. AV_WL64(side_data+8 + 8*i , error[i]);
  631. return 0;
  632. }