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.

118 lines
3.4KB

  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 "avcodec.h"
  22. #include "libavutil/avassert.h"
  23. void av_destruct_packet_nofree(AVPacket *pkt)
  24. {
  25. pkt->data = NULL; pkt->size = 0;
  26. }
  27. void av_destruct_packet(AVPacket *pkt)
  28. {
  29. av_free(pkt->data);
  30. pkt->data = NULL; pkt->size = 0;
  31. }
  32. void av_init_packet(AVPacket *pkt)
  33. {
  34. pkt->pts = AV_NOPTS_VALUE;
  35. pkt->dts = AV_NOPTS_VALUE;
  36. pkt->pos = -1;
  37. pkt->duration = 0;
  38. pkt->convergence_duration = 0;
  39. pkt->flags = 0;
  40. pkt->stream_index = 0;
  41. pkt->destruct= NULL;
  42. }
  43. int av_new_packet(AVPacket *pkt, int size)
  44. {
  45. uint8_t *data= NULL;
  46. if((unsigned)size < (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
  47. data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  48. if (data){
  49. memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  50. }else
  51. size=0;
  52. av_init_packet(pkt);
  53. pkt->data = data;
  54. pkt->size = size;
  55. pkt->destruct = av_destruct_packet;
  56. if(!data)
  57. return AVERROR(ENOMEM);
  58. return 0;
  59. }
  60. void av_shrink_packet(AVPacket *pkt, int size)
  61. {
  62. if (pkt->size <= size) return;
  63. pkt->size = size;
  64. memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  65. }
  66. int av_grow_packet(AVPacket *pkt, int grow_by)
  67. {
  68. void *new_ptr;
  69. av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
  70. if (!pkt->size)
  71. return av_new_packet(pkt, grow_by);
  72. if ((unsigned)grow_by > INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
  73. return -1;
  74. new_ptr = av_realloc(pkt->data, pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE);
  75. if (!new_ptr)
  76. return AVERROR(ENOMEM);
  77. pkt->data = new_ptr;
  78. pkt->size += grow_by;
  79. memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  80. return 0;
  81. }
  82. int av_dup_packet(AVPacket *pkt)
  83. {
  84. if (((pkt->destruct == av_destruct_packet_nofree) || (pkt->destruct == NULL)) && pkt->data) {
  85. uint8_t *data;
  86. /* We duplicate the packet and don't forget to add the padding again. */
  87. if((unsigned)pkt->size > (unsigned)pkt->size + FF_INPUT_BUFFER_PADDING_SIZE)
  88. return AVERROR(ENOMEM);
  89. data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  90. if (!data) {
  91. return AVERROR(ENOMEM);
  92. }
  93. memcpy(data, pkt->data, pkt->size);
  94. memset(data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  95. pkt->data = data;
  96. pkt->destruct = av_destruct_packet;
  97. }
  98. return 0;
  99. }
  100. void av_free_packet(AVPacket *pkt)
  101. {
  102. if (pkt) {
  103. if (pkt->destruct) pkt->destruct(pkt);
  104. pkt->data = NULL; pkt->size = 0;
  105. }
  106. }