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.

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