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.

158 lines
4.5KB

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