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.

134 lines
4.1KB

  1. /*
  2. * RTP parser for AC3 payload format (RFC 4184)
  3. * Copyright (c) 2015 Gilles Chanteperdrix <gch@xenomai.org>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "avio_internal.h"
  23. #include "rtpdec_formats.h"
  24. #define RTP_AC3_PAYLOAD_HEADER_SIZE 2
  25. struct PayloadContext {
  26. unsigned nr_frames;
  27. unsigned last_frame;
  28. uint32_t timestamp;
  29. AVIOContext *fragment;
  30. };
  31. static void ac3_close_context(PayloadContext *data)
  32. {
  33. ffio_free_dyn_buf(&data->fragment);
  34. }
  35. static int ac3_handle_packet(AVFormatContext *ctx, PayloadContext *data,
  36. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  37. const uint8_t *buf, int len, uint16_t seq,
  38. int flags)
  39. {
  40. unsigned frame_type;
  41. unsigned nr_frames;
  42. int err;
  43. if (len < RTP_AC3_PAYLOAD_HEADER_SIZE + 1) {
  44. av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
  45. return AVERROR_INVALIDDATA;
  46. }
  47. frame_type = buf[0] & 0x3;
  48. nr_frames = buf[1];
  49. buf += RTP_AC3_PAYLOAD_HEADER_SIZE;
  50. len -= RTP_AC3_PAYLOAD_HEADER_SIZE;
  51. switch (frame_type) {
  52. case 0: /* One or more complete frames */
  53. if (!nr_frames) {
  54. av_log(ctx, AV_LOG_ERROR, "Invalid AC3 packet data\n");
  55. return AVERROR_INVALIDDATA;
  56. }
  57. if (av_new_packet(pkt, len)) {
  58. av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
  59. return AVERROR(ENOMEM);
  60. }
  61. pkt->stream_index = st->index;
  62. memcpy(pkt->data, buf, len);
  63. return 0;
  64. case 1:
  65. case 2: /* First fragment */
  66. ffio_free_dyn_buf(&data->fragment);
  67. data->last_frame = 1;
  68. data->nr_frames = nr_frames;
  69. err = avio_open_dyn_buf(&data->fragment);
  70. if (err < 0)
  71. return err;
  72. avio_write(data->fragment, buf, len);
  73. data->timestamp = *timestamp;
  74. return AVERROR(EAGAIN);
  75. case 3: /* Fragment other than first */
  76. if (!data->fragment) {
  77. av_log(ctx, AV_LOG_WARNING,
  78. "Received packet without a start fragment; dropping.\n");
  79. return AVERROR(EAGAIN);
  80. }
  81. if (nr_frames != data->nr_frames ||
  82. data->timestamp != *timestamp) {
  83. ffio_free_dyn_buf(&data->fragment);
  84. av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
  85. return AVERROR_INVALIDDATA;
  86. }
  87. avio_write(data->fragment, buf, len);
  88. data->last_frame++;
  89. }
  90. if (!(flags & RTP_FLAG_MARKER))
  91. return AVERROR(EAGAIN);
  92. if (data->last_frame != data->nr_frames) {
  93. ffio_free_dyn_buf(&data->fragment);
  94. av_log(ctx, AV_LOG_ERROR, "Missed %d packets\n",
  95. data->nr_frames - data->last_frame);
  96. return AVERROR_INVALIDDATA;
  97. }
  98. err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
  99. if (err < 0) {
  100. av_log(ctx, AV_LOG_ERROR,
  101. "Error occurred when getting fragment buffer.\n");
  102. return err;
  103. }
  104. return 0;
  105. }
  106. RTPDynamicProtocolHandler ff_ac3_dynamic_handler = {
  107. .enc_name = "ac3",
  108. .codec_type = AVMEDIA_TYPE_AUDIO,
  109. .codec_id = AV_CODEC_ID_AC3,
  110. .need_parsing = AVSTREAM_PARSE_FULL,
  111. .priv_data_size = sizeof(PayloadContext),
  112. .close = ac3_close_context,
  113. .parse_packet = ac3_handle_packet,
  114. };