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.

107 lines
3.3KB

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