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.

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