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.

149 lines
4.3KB

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