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.

109 lines
3.4KB

  1. /*
  2. * RTP MPEG2TS depacketizer
  3. * Copyright (c) 2003 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 "libavutil/attributes.h"
  22. #include "mpegts.h"
  23. #include "rtpdec_formats.h"
  24. struct PayloadContext {
  25. struct MpegTSContext *ts;
  26. int read_buf_index;
  27. int read_buf_size;
  28. uint8_t buf[RTP_MAX_PACKET_LENGTH];
  29. };
  30. static PayloadContext *mpegts_new_context(void)
  31. {
  32. return av_mallocz(sizeof(PayloadContext));
  33. }
  34. static void mpegts_free_context(PayloadContext *data)
  35. {
  36. if (!data)
  37. return;
  38. if (data->ts)
  39. ff_mpegts_parse_close(data->ts);
  40. av_free(data);
  41. }
  42. static av_cold int mpegts_init(AVFormatContext *ctx, int st_index,
  43. PayloadContext *data)
  44. {
  45. data->ts = ff_mpegts_parse_open(ctx);
  46. if (!data->ts)
  47. return AVERROR(ENOMEM);
  48. return 0;
  49. }
  50. static int mpegts_handle_packet(AVFormatContext *ctx, PayloadContext *data,
  51. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  52. const uint8_t *buf, int len, uint16_t seq,
  53. int flags)
  54. {
  55. int ret;
  56. // We don't want to use the RTP timestamps at all. If the mpegts demuxer
  57. // doesn't set any pts/dts, the generic rtpdec code shouldn't try to
  58. // fill it in either, since the mpegts and RTP timestamps are in totally
  59. // different ranges.
  60. *timestamp = RTP_NOTS_VALUE;
  61. if (!data->ts)
  62. return AVERROR(EINVAL);
  63. if (!buf) {
  64. if (data->read_buf_index >= data->read_buf_size)
  65. return AVERROR(EAGAIN);
  66. ret = ff_mpegts_parse_packet(data->ts, pkt, data->buf + data->read_buf_index,
  67. data->read_buf_size - data->read_buf_index);
  68. if (ret < 0)
  69. return AVERROR(EAGAIN);
  70. data->read_buf_index += ret;
  71. if (data->read_buf_index < data->read_buf_size)
  72. return 1;
  73. else
  74. return 0;
  75. }
  76. ret = ff_mpegts_parse_packet(data->ts, pkt, buf, len);
  77. /* The only error that can be returned from ff_mpegts_parse_packet
  78. * is "no more data to return from the provided buffer", so return
  79. * AVERROR(EAGAIN) for all errors */
  80. if (ret < 0)
  81. return AVERROR(EAGAIN);
  82. if (ret < len) {
  83. data->read_buf_size = FFMIN(len - ret, sizeof(data->buf));
  84. memcpy(data->buf, buf + ret, data->read_buf_size);
  85. data->read_buf_index = 0;
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. RTPDynamicProtocolHandler ff_mpegts_dynamic_handler = {
  91. .codec_type = AVMEDIA_TYPE_DATA,
  92. .parse_packet = mpegts_handle_packet,
  93. .alloc = mpegts_new_context,
  94. .init = mpegts_init,
  95. .free = mpegts_free_context,
  96. .static_payload_id = 33,
  97. };