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.

188 lines
5.7KB

  1. /**
  2. * RTP Depacketization of MP4A-LATM, RFC 3016
  3. * Copyright (c) 2010 Martin Storsjo
  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 "rtpdec_formats.h"
  22. #include "internal.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavcodec/get_bits.h"
  25. #include <strings.h>
  26. struct PayloadContext {
  27. AVIOContext *dyn_buf;
  28. uint8_t *buf;
  29. int pos, len;
  30. uint32_t timestamp;
  31. };
  32. static PayloadContext *latm_new_context(void)
  33. {
  34. return av_mallocz(sizeof(PayloadContext));
  35. }
  36. static void latm_free_context(PayloadContext *data)
  37. {
  38. if (!data)
  39. return;
  40. if (data->dyn_buf) {
  41. uint8_t *p;
  42. url_close_dyn_buf(data->dyn_buf, &p);
  43. av_free(p);
  44. }
  45. av_free(data->buf);
  46. av_free(data);
  47. }
  48. static int latm_parse_packet(AVFormatContext *ctx, PayloadContext *data,
  49. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  50. const uint8_t *buf, int len, int flags)
  51. {
  52. int ret, cur_len;
  53. if (buf) {
  54. if (!data->dyn_buf || data->timestamp != *timestamp) {
  55. av_freep(&data->buf);
  56. if (data->dyn_buf)
  57. url_close_dyn_buf(data->dyn_buf, &data->buf);
  58. data->dyn_buf = NULL;
  59. av_freep(&data->buf);
  60. data->timestamp = *timestamp;
  61. if ((ret = url_open_dyn_buf(&data->dyn_buf)) < 0)
  62. return ret;
  63. }
  64. avio_write(data->dyn_buf, buf, len);
  65. if (!(flags & RTP_FLAG_MARKER))
  66. return AVERROR(EAGAIN);
  67. av_free(data->buf);
  68. data->len = url_close_dyn_buf(data->dyn_buf, &data->buf);
  69. data->dyn_buf = NULL;
  70. data->pos = 0;
  71. }
  72. if (!data->buf) {
  73. av_log(ctx, AV_LOG_ERROR, "No data available yet\n");
  74. return AVERROR(EIO);
  75. }
  76. cur_len = 0;
  77. while (data->pos < data->len) {
  78. uint8_t val = data->buf[data->pos++];
  79. cur_len += val;
  80. if (val != 0xff)
  81. break;
  82. }
  83. if (data->pos + cur_len > data->len) {
  84. av_log(ctx, AV_LOG_ERROR, "Malformed LATM packet\n");
  85. return AVERROR(EIO);
  86. }
  87. if ((ret = av_new_packet(pkt, cur_len)) < 0)
  88. return ret;
  89. memcpy(pkt->data, data->buf + data->pos, cur_len);
  90. data->pos += cur_len;
  91. pkt->stream_index = st->index;
  92. return data->pos < data->len;
  93. }
  94. static int parse_fmtp_config(AVStream *st, char *value)
  95. {
  96. int len = ff_hex_to_data(NULL, value), i, ret = 0;
  97. GetBitContext gb;
  98. uint8_t *config;
  99. int audio_mux_version, same_time_framing, num_sub_frames,
  100. num_programs, num_layers;
  101. /* Pad this buffer, too, to avoid out of bounds reads with get_bits below */
  102. config = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  103. if (!config)
  104. return AVERROR(ENOMEM);
  105. ff_hex_to_data(config, value);
  106. init_get_bits(&gb, config, len*8);
  107. audio_mux_version = get_bits(&gb, 1);
  108. same_time_framing = get_bits(&gb, 1);
  109. num_sub_frames = get_bits(&gb, 6);
  110. num_programs = get_bits(&gb, 4);
  111. num_layers = get_bits(&gb, 3);
  112. if (audio_mux_version != 0 || same_time_framing != 1 || num_programs != 0 ||
  113. num_layers != 0) {
  114. av_log(NULL, AV_LOG_WARNING, "Unsupported LATM config (%d,%d,%d,%d)\n",
  115. audio_mux_version, same_time_framing,
  116. num_programs, num_layers);
  117. ret = AVERROR_PATCHWELCOME;
  118. goto end;
  119. }
  120. av_freep(&st->codec->extradata);
  121. st->codec->extradata_size = (get_bits_left(&gb) + 7)/8;
  122. st->codec->extradata = av_mallocz(st->codec->extradata_size +
  123. FF_INPUT_BUFFER_PADDING_SIZE);
  124. if (!st->codec->extradata) {
  125. ret = AVERROR(ENOMEM);
  126. goto end;
  127. }
  128. for (i = 0; i < st->codec->extradata_size; i++)
  129. st->codec->extradata[i] = get_bits(&gb, 8);
  130. end:
  131. av_free(config);
  132. return ret;
  133. }
  134. static int parse_fmtp(AVStream *stream, PayloadContext *data,
  135. char *attr, char *value)
  136. {
  137. int res;
  138. if (!strcmp(attr, "config")) {
  139. res = parse_fmtp_config(stream, value);
  140. if (res < 0)
  141. return res;
  142. } else if (!strcmp(attr, "cpresent")) {
  143. int cpresent = atoi(value);
  144. if (cpresent != 0)
  145. av_log_missing_feature(NULL, "RTP MP4A-LATM with in-band "
  146. "configuration", 1);
  147. }
  148. return 0;
  149. }
  150. static int latm_parse_sdp_line(AVFormatContext *s, int st_index,
  151. PayloadContext *data, const char *line)
  152. {
  153. const char *p;
  154. if (av_strstart(line, "fmtp:", &p))
  155. return ff_parse_fmtp(s->streams[st_index], data, p, parse_fmtp);
  156. return 0;
  157. }
  158. RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler = {
  159. .enc_name = "MP4A-LATM",
  160. .codec_type = AVMEDIA_TYPE_AUDIO,
  161. .codec_id = CODEC_ID_AAC,
  162. .parse_sdp_a_line = latm_parse_sdp_line,
  163. .open = latm_new_context,
  164. .close = latm_free_context,
  165. .parse_packet = latm_parse_packet
  166. };