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.

98 lines
2.7KB

  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. void av_destruct_packet_nofree(AVPacket *pkt)
  23. {
  24. pkt->data = NULL; pkt->size = 0;
  25. }
  26. void av_destruct_packet(AVPacket *pkt)
  27. {
  28. av_free(pkt->data);
  29. pkt->data = NULL; pkt->size = 0;
  30. }
  31. void av_init_packet(AVPacket *pkt)
  32. {
  33. pkt->pts = AV_NOPTS_VALUE;
  34. pkt->dts = AV_NOPTS_VALUE;
  35. pkt->pos = -1;
  36. pkt->duration = 0;
  37. pkt->convergence_duration = 0;
  38. pkt->flags = 0;
  39. pkt->stream_index = 0;
  40. pkt->destruct= NULL;
  41. }
  42. int av_new_packet(AVPacket *pkt, int size)
  43. {
  44. uint8_t *data;
  45. if((unsigned)size > (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
  46. return AVERROR(ENOMEM);
  47. data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  48. if (!data)
  49. return AVERROR(ENOMEM);
  50. memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  51. av_init_packet(pkt);
  52. pkt->data = data;
  53. pkt->size = size;
  54. pkt->destruct = av_destruct_packet;
  55. return 0;
  56. }
  57. void av_shrink_packet(AVPacket *pkt, int size)
  58. {
  59. if (pkt->size <= size) return;
  60. pkt->size = size;
  61. memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  62. }
  63. int av_dup_packet(AVPacket *pkt)
  64. {
  65. if (((pkt->destruct == av_destruct_packet_nofree) || (pkt->destruct == NULL)) && pkt->data) {
  66. uint8_t *data;
  67. /* We duplicate the packet and don't forget to add the padding again. */
  68. if((unsigned)pkt->size > (unsigned)pkt->size + FF_INPUT_BUFFER_PADDING_SIZE)
  69. return AVERROR(ENOMEM);
  70. data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  71. if (!data) {
  72. return AVERROR(ENOMEM);
  73. }
  74. memcpy(data, pkt->data, pkt->size);
  75. memset(data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  76. pkt->data = data;
  77. pkt->destruct = av_destruct_packet;
  78. }
  79. return 0;
  80. }
  81. void av_free_packet(AVPacket *pkt)
  82. {
  83. if (pkt) {
  84. if (pkt->destruct) pkt->destruct(pkt);
  85. pkt->data = NULL; pkt->size = 0;
  86. }
  87. }