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.

225 lines
6.4KB

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