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.

201 lines
6.0KB

  1. /*
  2. * RTP parser for loss tolerant payload format for MP3 audio (RFC 5219)
  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 "libavutil/attributes.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "avio_internal.h"
  24. #include "rtpdec_formats.h"
  25. struct PayloadContext {
  26. unsigned adu_size;
  27. unsigned cur_size;
  28. uint32_t timestamp;
  29. uint8_t *split_buf;
  30. int split_pos, split_buf_size, split_pkts;
  31. AVIOContext *fragment;
  32. };
  33. static void mpa_robust_close_context(PayloadContext *data)
  34. {
  35. ffio_free_dyn_buf(&data->fragment);
  36. av_free(data->split_buf);
  37. }
  38. static int mpa_robust_parse_rtp_header(AVFormatContext *ctx,
  39. const uint8_t *buf, int len,
  40. unsigned *adu_size, unsigned *cont)
  41. {
  42. unsigned header_size;
  43. if (len < 2) {
  44. av_log(ctx, AV_LOG_ERROR, "Invalid %d bytes packet\n", len);
  45. return AVERROR_INVALIDDATA;
  46. }
  47. *cont = !!(buf[0] & 0x80);
  48. if (!(buf[0] & 0x40)) {
  49. header_size = 1;
  50. *adu_size = buf[0] & ~0xc0;
  51. } else {
  52. header_size = 2;
  53. *adu_size = AV_RB16(buf) & ~0xc000;
  54. }
  55. return header_size;
  56. }
  57. static int mpa_robust_parse_packet(AVFormatContext *ctx, PayloadContext *data,
  58. AVStream *st, AVPacket *pkt,
  59. uint32_t *timestamp, const uint8_t *buf,
  60. int len, uint16_t seq, int flags)
  61. {
  62. unsigned adu_size, continuation;
  63. int err, header_size;
  64. if (!buf) {
  65. buf = &data->split_buf[data->split_pos];
  66. len = data->split_buf_size - data->split_pos;
  67. header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
  68. &continuation);
  69. if (header_size < 0) {
  70. av_freep(&data->split_buf);
  71. return header_size;
  72. }
  73. buf += header_size;
  74. len -= header_size;
  75. if (continuation || adu_size > len) {
  76. av_freep(&data->split_buf);
  77. av_log(ctx, AV_LOG_ERROR, "Invalid frame\n");
  78. return AVERROR_INVALIDDATA;
  79. }
  80. if (av_new_packet(pkt, adu_size)) {
  81. av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
  82. return AVERROR(ENOMEM);
  83. }
  84. pkt->stream_index = st->index;
  85. memcpy(pkt->data, buf, adu_size);
  86. data->split_pos += header_size + adu_size;
  87. if (data->split_pos == data->split_buf_size) {
  88. av_freep(&data->split_buf);
  89. return 0;
  90. }
  91. return 1;
  92. }
  93. header_size = mpa_robust_parse_rtp_header(ctx, buf, len, &adu_size,
  94. &continuation);
  95. if (header_size < 0)
  96. return header_size;
  97. buf += header_size;
  98. len -= header_size;
  99. if (!continuation && adu_size <= len) {
  100. /* One or more complete frames */
  101. if (av_new_packet(pkt, adu_size)) {
  102. av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
  103. return AVERROR(ENOMEM);
  104. }
  105. pkt->stream_index = st->index;
  106. memcpy(pkt->data, buf, adu_size);
  107. buf += adu_size;
  108. len -= adu_size;
  109. if (len) {
  110. data->split_buf_size = len;
  111. data->split_buf = av_malloc(data->split_buf_size);
  112. data->split_pos = 0;
  113. if (!data->split_buf) {
  114. av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
  115. av_packet_unref(pkt);
  116. return AVERROR(ENOMEM);
  117. }
  118. memcpy(data->split_buf, buf, data->split_buf_size);
  119. return 1;
  120. }
  121. return 0;
  122. } else if (!continuation) { /* && adu_size > len */
  123. /* First fragment */
  124. ffio_free_dyn_buf(&data->fragment);
  125. data->adu_size = adu_size;
  126. data->cur_size = len;
  127. data->timestamp = *timestamp;
  128. err = avio_open_dyn_buf(&data->fragment);
  129. if (err < 0)
  130. return err;
  131. avio_write(data->fragment, buf, len);
  132. return AVERROR(EAGAIN);
  133. }
  134. /* else continuation == 1 */
  135. /* Fragment other than first */
  136. if (!data->fragment) {
  137. av_log(ctx, AV_LOG_WARNING,
  138. "Received packet without a start fragment; dropping.\n");
  139. return AVERROR(EAGAIN);
  140. }
  141. if (adu_size = data->adu_size ||
  142. data->timestamp != *timestamp) {
  143. ffio_free_dyn_buf(&data->fragment);
  144. av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
  145. return AVERROR_INVALIDDATA;
  146. }
  147. avio_write(data->fragment, buf, len);
  148. data->cur_size += len;
  149. if (data->cur_size < data->adu_size)
  150. return AVERROR(EAGAIN);
  151. err = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
  152. if (err < 0) {
  153. av_log(ctx, AV_LOG_ERROR,
  154. "Error occurred when getting fragment buffer.\n");
  155. return err;
  156. }
  157. return 0;
  158. }
  159. RTPDynamicProtocolHandler ff_mpeg_audio_robust_dynamic_handler = {
  160. .enc_name = "mpa-robust",
  161. .codec_type = AVMEDIA_TYPE_AUDIO,
  162. .codec_id = AV_CODEC_ID_MP3ADU,
  163. .need_parsing = AVSTREAM_PARSE_HEADERS,
  164. .priv_data_size = sizeof(PayloadContext),
  165. .close = mpa_robust_close_context,
  166. .parse_packet = mpa_robust_parse_packet,
  167. };