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.

144 lines
4.2KB

  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 "rtpdec_formats.h"
  23. #define RTP_AC3_PAYLOAD_HEADER_SIZE 2
  24. struct PayloadContext {
  25. unsigned nr_frames;
  26. unsigned last_frame;
  27. uint32_t timestamp;
  28. AVIOContext *fragment;
  29. };
  30. static void free_fragment(PayloadContext *data)
  31. {
  32. if (data->fragment) {
  33. uint8_t *p;
  34. avio_close_dyn_buf(data->fragment, &p);
  35. av_free(p);
  36. data->fragment = NULL;
  37. }
  38. }
  39. static void ac3_free_context(PayloadContext *data)
  40. {
  41. free_fragment(data);
  42. av_free(data);
  43. }
  44. static int ac3_handle_packet(AVFormatContext *ctx, PayloadContext *data,
  45. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  46. const uint8_t *buf, int len, uint16_t seq,
  47. int flags)
  48. {
  49. unsigned frame_type;
  50. unsigned nr_frames;
  51. int err;
  52. if (len < RTP_AC3_PAYLOAD_HEADER_SIZE + 1) {
  53. av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
  54. return AVERROR_INVALIDDATA;
  55. }
  56. frame_type = buf[0] & 0x3;
  57. nr_frames = buf[1];
  58. buf += RTP_AC3_PAYLOAD_HEADER_SIZE;
  59. len -= RTP_AC3_PAYLOAD_HEADER_SIZE;
  60. switch (frame_type) {
  61. case 0: /* One or more complete frames */
  62. if (!nr_frames) {
  63. av_log(ctx, AV_LOG_ERROR, "Invalid AC3 packet data\n");
  64. return AVERROR_INVALIDDATA;
  65. }
  66. if (av_new_packet(pkt, len)) {
  67. av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
  68. return AVERROR(ENOMEM);
  69. }
  70. pkt->stream_index = st->index;
  71. memcpy(pkt->data, buf, len);
  72. return 0;
  73. case 1:
  74. case 2: /* First fragment */
  75. free_fragment(data);
  76. data->last_frame = 1;
  77. data->nr_frames = nr_frames;
  78. err = avio_open_dyn_buf(&data->fragment);
  79. if (err < 0)
  80. return err;
  81. avio_write(data->fragment, buf, len);
  82. data->timestamp = *timestamp;
  83. return AVERROR(EAGAIN);
  84. case 3: /* Fragment other than first */
  85. if (!data->fragment) {
  86. av_log(ctx, AV_LOG_WARNING,
  87. "Received packet without a start fragment; dropping.\n");
  88. return AVERROR(EAGAIN);
  89. }
  90. if (nr_frames != data->nr_frames ||
  91. data->timestamp != *timestamp) {
  92. free_fragment(data);
  93. av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
  94. return AVERROR_INVALIDDATA;
  95. }
  96. avio_write(data->fragment, buf, len);
  97. data->last_frame++;
  98. }
  99. if (!(flags & RTP_FLAG_MARKER))
  100. return AVERROR(EAGAIN);
  101. if (data->last_frame != data->nr_frames) {
  102. free_fragment(data);
  103. av_log(ctx, AV_LOG_ERROR, "Missed %d packets\n",
  104. data->nr_frames - data->last_frame);
  105. return AVERROR_INVALIDDATA;
  106. }
  107. err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
  108. if (err < 0) {
  109. av_log(ctx, AV_LOG_ERROR,
  110. "Error occurred when getting fragment buffer.\n");
  111. return err;
  112. }
  113. return 0;
  114. }
  115. RTPDynamicProtocolHandler ff_ac3_dynamic_handler = {
  116. .enc_name = "ac3",
  117. .codec_type = AVMEDIA_TYPE_AUDIO,
  118. .codec_id = AV_CODEC_ID_AC3,
  119. .need_parsing = AVSTREAM_PARSE_FULL,
  120. .priv_data_size = sizeof(PayloadContext),
  121. .free = ac3_free_context,
  122. .parse_packet = ac3_handle_packet,
  123. };