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.

211 lines
6.1KB

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