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.

216 lines
6.2KB

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